Path: blob/master/drivers/firmware/tegra/bpmp-tegra210.c
26428 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (c) 2018, NVIDIA CORPORATION.3*/45#include <linux/interrupt.h>6#include <linux/irq.h>7#include <linux/io.h>8#include <linux/of.h>9#include <linux/platform_device.h>1011#include <soc/tegra/bpmp.h>1213#include "bpmp-private.h"1415#define TRIGGER_OFFSET 0x00016#define RESULT_OFFSET(id) (0xc00 + id * 4)17#define TRIGGER_ID_SHIFT 1618#define TRIGGER_CMD_GET 41920#define STA_OFFSET 021#define SET_OFFSET 422#define CLR_OFFSET 82324#define CH_MASK(ch) (0x3 << ((ch) * 2))25#define SL_SIGL(ch) (0x0 << ((ch) * 2))26#define SL_QUED(ch) (0x1 << ((ch) * 2))27#define MA_FREE(ch) (0x2 << ((ch) * 2))28#define MA_ACKD(ch) (0x3 << ((ch) * 2))2930struct tegra210_bpmp {31void __iomem *atomics;32void __iomem *arb_sema;33struct irq_data *tx_irq_data;34};3536static u32 bpmp_channel_status(struct tegra_bpmp *bpmp, unsigned int index)37{38struct tegra210_bpmp *priv = bpmp->priv;3940return __raw_readl(priv->arb_sema + STA_OFFSET) & CH_MASK(index);41}4243static bool tegra210_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)44{45unsigned int index = channel->index;4647return bpmp_channel_status(channel->bpmp, index) == MA_ACKD(index);48}4950static bool tegra210_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)51{52unsigned int index = channel->index;5354return bpmp_channel_status(channel->bpmp, index) == SL_SIGL(index);55}5657static bool58tegra210_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)59{60unsigned int index = channel->index;6162return bpmp_channel_status(channel->bpmp, index) == MA_FREE(index);63}6465static bool66tegra210_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)67{68unsigned int index = channel->index;6970return bpmp_channel_status(channel->bpmp, index) == SL_QUED(index);71}7273static int tegra210_bpmp_post_request(struct tegra_bpmp_channel *channel)74{75struct tegra210_bpmp *priv = channel->bpmp->priv;7677__raw_writel(CH_MASK(channel->index), priv->arb_sema + CLR_OFFSET);7879return 0;80}8182static int tegra210_bpmp_post_response(struct tegra_bpmp_channel *channel)83{84struct tegra210_bpmp *priv = channel->bpmp->priv;8586__raw_writel(MA_ACKD(channel->index), priv->arb_sema + SET_OFFSET);8788return 0;89}9091static int tegra210_bpmp_ack_response(struct tegra_bpmp_channel *channel)92{93struct tegra210_bpmp *priv = channel->bpmp->priv;9495__raw_writel(MA_ACKD(channel->index) ^ MA_FREE(channel->index),96priv->arb_sema + CLR_OFFSET);9798return 0;99}100101static int tegra210_bpmp_ack_request(struct tegra_bpmp_channel *channel)102{103struct tegra210_bpmp *priv = channel->bpmp->priv;104105__raw_writel(SL_QUED(channel->index), priv->arb_sema + SET_OFFSET);106107return 0;108}109110static int tegra210_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)111{112struct tegra210_bpmp *priv = bpmp->priv;113struct irq_data *irq_data = priv->tx_irq_data;114115/*116* Tegra Legacy Interrupt Controller (LIC) is used to notify BPMP of117* available messages118*/119if (irq_data->chip->irq_retrigger)120return irq_data->chip->irq_retrigger(irq_data);121122return -EINVAL;123}124125static irqreturn_t rx_irq(int irq, void *data)126{127struct tegra_bpmp *bpmp = data;128129tegra_bpmp_handle_rx(bpmp);130131return IRQ_HANDLED;132}133134static int tegra210_bpmp_channel_init(struct tegra_bpmp_channel *channel,135struct tegra_bpmp *bpmp,136unsigned int index)137{138struct tegra210_bpmp *priv = bpmp->priv;139void __iomem *p;140u32 address;141142/* Retrieve channel base address from BPMP */143writel(index << TRIGGER_ID_SHIFT | TRIGGER_CMD_GET,144priv->atomics + TRIGGER_OFFSET);145address = readl(priv->atomics + RESULT_OFFSET(index));146147p = devm_ioremap(bpmp->dev, address, 0x80);148if (!p)149return -ENOMEM;150151iosys_map_set_vaddr_iomem(&channel->ib, p);152iosys_map_set_vaddr_iomem(&channel->ob, p);153154channel->index = index;155init_completion(&channel->completion);156channel->bpmp = bpmp;157158return 0;159}160161static int tegra210_bpmp_init(struct tegra_bpmp *bpmp)162{163struct platform_device *pdev = to_platform_device(bpmp->dev);164struct tegra210_bpmp *priv;165unsigned int i;166int err;167168priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);169if (!priv)170return -ENOMEM;171172bpmp->priv = priv;173174priv->atomics = devm_platform_ioremap_resource(pdev, 0);175if (IS_ERR(priv->atomics))176return PTR_ERR(priv->atomics);177178priv->arb_sema = devm_platform_ioremap_resource(pdev, 1);179if (IS_ERR(priv->arb_sema))180return PTR_ERR(priv->arb_sema);181182err = tegra210_bpmp_channel_init(bpmp->tx_channel, bpmp,183bpmp->soc->channels.cpu_tx.offset);184if (err < 0)185return err;186187err = tegra210_bpmp_channel_init(bpmp->rx_channel, bpmp,188bpmp->soc->channels.cpu_rx.offset);189if (err < 0)190return err;191192for (i = 0; i < bpmp->threaded.count; i++) {193unsigned int index = bpmp->soc->channels.thread.offset + i;194195err = tegra210_bpmp_channel_init(&bpmp->threaded_channels[i],196bpmp, index);197if (err < 0)198return err;199}200201err = platform_get_irq_byname(pdev, "tx");202if (err < 0)203return err;204205priv->tx_irq_data = irq_get_irq_data(err);206if (!priv->tx_irq_data) {207dev_err(&pdev->dev, "failed to get IRQ data for TX IRQ\n");208return -ENOENT;209}210211err = platform_get_irq_byname(pdev, "rx");212if (err < 0)213return err;214215err = devm_request_irq(&pdev->dev, err, rx_irq,216IRQF_NO_SUSPEND, dev_name(&pdev->dev), bpmp);217if (err < 0) {218dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);219return err;220}221222return 0;223}224225const struct tegra_bpmp_ops tegra210_bpmp_ops = {226.init = tegra210_bpmp_init,227.is_response_ready = tegra210_bpmp_is_response_ready,228.is_request_ready = tegra210_bpmp_is_request_ready,229.ack_response = tegra210_bpmp_ack_response,230.ack_request = tegra210_bpmp_ack_request,231.is_response_channel_free = tegra210_bpmp_is_response_channel_free,232.is_request_channel_free = tegra210_bpmp_is_request_channel_free,233.post_response = tegra210_bpmp_post_response,234.post_request = tegra210_bpmp_post_request,235.ring_doorbell = tegra210_bpmp_ring_doorbell,236};237238239