1139251fcSTimo Alho // SPDX-License-Identifier: GPL-2.0
2139251fcSTimo Alho /*
3139251fcSTimo Alho * Copyright (c) 2018, NVIDIA CORPORATION.
4139251fcSTimo Alho */
5139251fcSTimo Alho
6139251fcSTimo Alho #include <linux/interrupt.h>
7139251fcSTimo Alho #include <linux/irq.h>
8139251fcSTimo Alho #include <linux/io.h>
9139251fcSTimo Alho #include <linux/of.h>
10139251fcSTimo Alho #include <linux/platform_device.h>
11139251fcSTimo Alho
12139251fcSTimo Alho #include <soc/tegra/bpmp.h>
13139251fcSTimo Alho
14139251fcSTimo Alho #include "bpmp-private.h"
15139251fcSTimo Alho
16139251fcSTimo Alho #define TRIGGER_OFFSET 0x000
17139251fcSTimo Alho #define RESULT_OFFSET(id) (0xc00 + id * 4)
18139251fcSTimo Alho #define TRIGGER_ID_SHIFT 16
19139251fcSTimo Alho #define TRIGGER_CMD_GET 4
20139251fcSTimo Alho
21139251fcSTimo Alho #define STA_OFFSET 0
22139251fcSTimo Alho #define SET_OFFSET 4
23139251fcSTimo Alho #define CLR_OFFSET 8
24139251fcSTimo Alho
25139251fcSTimo Alho #define CH_MASK(ch) (0x3 << ((ch) * 2))
26139251fcSTimo Alho #define SL_SIGL(ch) (0x0 << ((ch) * 2))
27139251fcSTimo Alho #define SL_QUED(ch) (0x1 << ((ch) * 2))
28139251fcSTimo Alho #define MA_FREE(ch) (0x2 << ((ch) * 2))
29139251fcSTimo Alho #define MA_ACKD(ch) (0x3 << ((ch) * 2))
30139251fcSTimo Alho
31139251fcSTimo Alho struct tegra210_bpmp {
32139251fcSTimo Alho void __iomem *atomics;
33139251fcSTimo Alho void __iomem *arb_sema;
34139251fcSTimo Alho struct irq_data *tx_irq_data;
35139251fcSTimo Alho };
36139251fcSTimo Alho
bpmp_channel_status(struct tegra_bpmp * bpmp,unsigned int index)37139251fcSTimo Alho static u32 bpmp_channel_status(struct tegra_bpmp *bpmp, unsigned int index)
38139251fcSTimo Alho {
39139251fcSTimo Alho struct tegra210_bpmp *priv = bpmp->priv;
40139251fcSTimo Alho
41139251fcSTimo Alho return __raw_readl(priv->arb_sema + STA_OFFSET) & CH_MASK(index);
42139251fcSTimo Alho }
43139251fcSTimo Alho
tegra210_bpmp_is_response_ready(struct tegra_bpmp_channel * channel)44139251fcSTimo Alho static bool tegra210_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
45139251fcSTimo Alho {
46139251fcSTimo Alho unsigned int index = channel->index;
47139251fcSTimo Alho
48139251fcSTimo Alho return bpmp_channel_status(channel->bpmp, index) == MA_ACKD(index);
49139251fcSTimo Alho }
50139251fcSTimo Alho
tegra210_bpmp_is_request_ready(struct tegra_bpmp_channel * channel)51139251fcSTimo Alho static bool tegra210_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
52139251fcSTimo Alho {
53139251fcSTimo Alho unsigned int index = channel->index;
54139251fcSTimo Alho
55139251fcSTimo Alho return bpmp_channel_status(channel->bpmp, index) == SL_SIGL(index);
56139251fcSTimo Alho }
57139251fcSTimo Alho
58139251fcSTimo Alho static bool
tegra210_bpmp_is_request_channel_free(struct tegra_bpmp_channel * channel)59139251fcSTimo Alho tegra210_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
60139251fcSTimo Alho {
61139251fcSTimo Alho unsigned int index = channel->index;
62139251fcSTimo Alho
63139251fcSTimo Alho return bpmp_channel_status(channel->bpmp, index) == MA_FREE(index);
64139251fcSTimo Alho }
65139251fcSTimo Alho
66139251fcSTimo Alho static bool
tegra210_bpmp_is_response_channel_free(struct tegra_bpmp_channel * channel)67139251fcSTimo Alho tegra210_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
68139251fcSTimo Alho {
69139251fcSTimo Alho unsigned int index = channel->index;
70139251fcSTimo Alho
71139251fcSTimo Alho return bpmp_channel_status(channel->bpmp, index) == SL_QUED(index);
72139251fcSTimo Alho }
73139251fcSTimo Alho
tegra210_bpmp_post_request(struct tegra_bpmp_channel * channel)74139251fcSTimo Alho static int tegra210_bpmp_post_request(struct tegra_bpmp_channel *channel)
75139251fcSTimo Alho {
76139251fcSTimo Alho struct tegra210_bpmp *priv = channel->bpmp->priv;
77139251fcSTimo Alho
78139251fcSTimo Alho __raw_writel(CH_MASK(channel->index), priv->arb_sema + CLR_OFFSET);
79139251fcSTimo Alho
80139251fcSTimo Alho return 0;
81139251fcSTimo Alho }
82139251fcSTimo Alho
tegra210_bpmp_post_response(struct tegra_bpmp_channel * channel)83139251fcSTimo Alho static int tegra210_bpmp_post_response(struct tegra_bpmp_channel *channel)
84139251fcSTimo Alho {
85139251fcSTimo Alho struct tegra210_bpmp *priv = channel->bpmp->priv;
86139251fcSTimo Alho
87139251fcSTimo Alho __raw_writel(MA_ACKD(channel->index), priv->arb_sema + SET_OFFSET);
88139251fcSTimo Alho
89139251fcSTimo Alho return 0;
90139251fcSTimo Alho }
91139251fcSTimo Alho
tegra210_bpmp_ack_response(struct tegra_bpmp_channel * channel)92139251fcSTimo Alho static int tegra210_bpmp_ack_response(struct tegra_bpmp_channel *channel)
93139251fcSTimo Alho {
94139251fcSTimo Alho struct tegra210_bpmp *priv = channel->bpmp->priv;
95139251fcSTimo Alho
96139251fcSTimo Alho __raw_writel(MA_ACKD(channel->index) ^ MA_FREE(channel->index),
97139251fcSTimo Alho priv->arb_sema + CLR_OFFSET);
98139251fcSTimo Alho
99139251fcSTimo Alho return 0;
100139251fcSTimo Alho }
101139251fcSTimo Alho
tegra210_bpmp_ack_request(struct tegra_bpmp_channel * channel)102139251fcSTimo Alho static int tegra210_bpmp_ack_request(struct tegra_bpmp_channel *channel)
103139251fcSTimo Alho {
104139251fcSTimo Alho struct tegra210_bpmp *priv = channel->bpmp->priv;
105139251fcSTimo Alho
106139251fcSTimo Alho __raw_writel(SL_QUED(channel->index), priv->arb_sema + SET_OFFSET);
107139251fcSTimo Alho
108139251fcSTimo Alho return 0;
109139251fcSTimo Alho }
110139251fcSTimo Alho
tegra210_bpmp_ring_doorbell(struct tegra_bpmp * bpmp)111139251fcSTimo Alho static int tegra210_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
112139251fcSTimo Alho {
113139251fcSTimo Alho struct tegra210_bpmp *priv = bpmp->priv;
114139251fcSTimo Alho struct irq_data *irq_data = priv->tx_irq_data;
115139251fcSTimo Alho
116139251fcSTimo Alho /*
117139251fcSTimo Alho * Tegra Legacy Interrupt Controller (LIC) is used to notify BPMP of
118139251fcSTimo Alho * available messages
119139251fcSTimo Alho */
120139251fcSTimo Alho if (irq_data->chip->irq_retrigger)
121139251fcSTimo Alho return irq_data->chip->irq_retrigger(irq_data);
122139251fcSTimo Alho
123139251fcSTimo Alho return -EINVAL;
124139251fcSTimo Alho }
125139251fcSTimo Alho
rx_irq(int irq,void * data)126139251fcSTimo Alho static irqreturn_t rx_irq(int irq, void *data)
127139251fcSTimo Alho {
128139251fcSTimo Alho struct tegra_bpmp *bpmp = data;
129139251fcSTimo Alho
130139251fcSTimo Alho tegra_bpmp_handle_rx(bpmp);
131139251fcSTimo Alho
132139251fcSTimo Alho return IRQ_HANDLED;
133139251fcSTimo Alho }
134139251fcSTimo Alho
tegra210_bpmp_channel_init(struct tegra_bpmp_channel * channel,struct tegra_bpmp * bpmp,unsigned int index)135139251fcSTimo Alho static int tegra210_bpmp_channel_init(struct tegra_bpmp_channel *channel,
136139251fcSTimo Alho struct tegra_bpmp *bpmp,
137139251fcSTimo Alho unsigned int index)
138139251fcSTimo Alho {
139139251fcSTimo Alho struct tegra210_bpmp *priv = bpmp->priv;
1404c1e0a97SThierry Reding void __iomem *p;
141139251fcSTimo Alho u32 address;
142139251fcSTimo Alho
143139251fcSTimo Alho /* Retrieve channel base address from BPMP */
144139251fcSTimo Alho writel(index << TRIGGER_ID_SHIFT | TRIGGER_CMD_GET,
145139251fcSTimo Alho priv->atomics + TRIGGER_OFFSET);
146139251fcSTimo Alho address = readl(priv->atomics + RESULT_OFFSET(index));
147139251fcSTimo Alho
148139251fcSTimo Alho p = devm_ioremap(bpmp->dev, address, 0x80);
149139251fcSTimo Alho if (!p)
150139251fcSTimo Alho return -ENOMEM;
151139251fcSTimo Alho
1524c1e0a97SThierry Reding iosys_map_set_vaddr_iomem(&channel->ib, p);
1534c1e0a97SThierry Reding iosys_map_set_vaddr_iomem(&channel->ob, p);
1544c1e0a97SThierry Reding
155139251fcSTimo Alho channel->index = index;
156139251fcSTimo Alho init_completion(&channel->completion);
157139251fcSTimo Alho channel->bpmp = bpmp;
158139251fcSTimo Alho
159139251fcSTimo Alho return 0;
160139251fcSTimo Alho }
161139251fcSTimo Alho
tegra210_bpmp_init(struct tegra_bpmp * bpmp)162139251fcSTimo Alho static int tegra210_bpmp_init(struct tegra_bpmp *bpmp)
163139251fcSTimo Alho {
164139251fcSTimo Alho struct platform_device *pdev = to_platform_device(bpmp->dev);
165139251fcSTimo Alho struct tegra210_bpmp *priv;
166139251fcSTimo Alho unsigned int i;
167139251fcSTimo Alho int err;
168139251fcSTimo Alho
169139251fcSTimo Alho priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
170139251fcSTimo Alho if (!priv)
171139251fcSTimo Alho return -ENOMEM;
172139251fcSTimo Alho
173139251fcSTimo Alho bpmp->priv = priv;
174139251fcSTimo Alho
175f11c34bdSCai Huoqing priv->atomics = devm_platform_ioremap_resource(pdev, 0);
176139251fcSTimo Alho if (IS_ERR(priv->atomics))
177139251fcSTimo Alho return PTR_ERR(priv->atomics);
178139251fcSTimo Alho
179f11c34bdSCai Huoqing priv->arb_sema = devm_platform_ioremap_resource(pdev, 1);
180139251fcSTimo Alho if (IS_ERR(priv->arb_sema))
181139251fcSTimo Alho return PTR_ERR(priv->arb_sema);
182139251fcSTimo Alho
183139251fcSTimo Alho err = tegra210_bpmp_channel_init(bpmp->tx_channel, bpmp,
184139251fcSTimo Alho bpmp->soc->channels.cpu_tx.offset);
185139251fcSTimo Alho if (err < 0)
186139251fcSTimo Alho return err;
187139251fcSTimo Alho
188139251fcSTimo Alho err = tegra210_bpmp_channel_init(bpmp->rx_channel, bpmp,
189139251fcSTimo Alho bpmp->soc->channels.cpu_rx.offset);
190139251fcSTimo Alho if (err < 0)
191139251fcSTimo Alho return err;
192139251fcSTimo Alho
193139251fcSTimo Alho for (i = 0; i < bpmp->threaded.count; i++) {
194139251fcSTimo Alho unsigned int index = bpmp->soc->channels.thread.offset + i;
195139251fcSTimo Alho
196139251fcSTimo Alho err = tegra210_bpmp_channel_init(&bpmp->threaded_channels[i],
197139251fcSTimo Alho bpmp, index);
198139251fcSTimo Alho if (err < 0)
199139251fcSTimo Alho return err;
200139251fcSTimo Alho }
201139251fcSTimo Alho
202139251fcSTimo Alho err = platform_get_irq_byname(pdev, "tx");
203*198d4649SYang Li if (err < 0)
204139251fcSTimo Alho return err;
205139251fcSTimo Alho
206139251fcSTimo Alho priv->tx_irq_data = irq_get_irq_data(err);
207139251fcSTimo Alho if (!priv->tx_irq_data) {
208139251fcSTimo Alho dev_err(&pdev->dev, "failed to get IRQ data for TX IRQ\n");
2097fea6771SZhen Lei return -ENOENT;
210139251fcSTimo Alho }
211139251fcSTimo Alho
212139251fcSTimo Alho err = platform_get_irq_byname(pdev, "rx");
213*198d4649SYang Li if (err < 0)
214139251fcSTimo Alho return err;
215139251fcSTimo Alho
216139251fcSTimo Alho err = devm_request_irq(&pdev->dev, err, rx_irq,
217139251fcSTimo Alho IRQF_NO_SUSPEND, dev_name(&pdev->dev), bpmp);
218139251fcSTimo Alho if (err < 0) {
219139251fcSTimo Alho dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
220139251fcSTimo Alho return err;
221139251fcSTimo Alho }
222139251fcSTimo Alho
223139251fcSTimo Alho return 0;
224139251fcSTimo Alho }
225139251fcSTimo Alho
226139251fcSTimo Alho const struct tegra_bpmp_ops tegra210_bpmp_ops = {
227139251fcSTimo Alho .init = tegra210_bpmp_init,
228139251fcSTimo Alho .is_response_ready = tegra210_bpmp_is_response_ready,
229139251fcSTimo Alho .is_request_ready = tegra210_bpmp_is_request_ready,
230139251fcSTimo Alho .ack_response = tegra210_bpmp_ack_response,
231139251fcSTimo Alho .ack_request = tegra210_bpmp_ack_request,
232139251fcSTimo Alho .is_response_channel_free = tegra210_bpmp_is_response_channel_free,
233139251fcSTimo Alho .is_request_channel_free = tegra210_bpmp_is_request_channel_free,
234139251fcSTimo Alho .post_response = tegra210_bpmp_post_response,
235139251fcSTimo Alho .post_request = tegra210_bpmp_post_request,
236139251fcSTimo Alho .ring_doorbell = tegra210_bpmp_ring_doorbell,
237139251fcSTimo Alho };
238