xref: /linux/drivers/mailbox/platform_mhu.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ad3a212cSNeil Armstrong /*
3ad3a212cSNeil Armstrong  * Copyright (C) 2016 BayLibre SAS.
4ad3a212cSNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
5ad3a212cSNeil Armstrong  * Synchronised with arm_mhu.c from :
6ad3a212cSNeil Armstrong  * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd.
7ad3a212cSNeil Armstrong  * Copyright (C) 2015 Linaro Ltd.
8ad3a212cSNeil Armstrong  * Author: Jassi Brar <jaswinder.singh@linaro.org>
9ad3a212cSNeil Armstrong  */
10ad3a212cSNeil Armstrong 
11ad3a212cSNeil Armstrong #include <linux/interrupt.h>
12ad3a212cSNeil Armstrong #include <linux/spinlock.h>
13ad3a212cSNeil Armstrong #include <linux/mutex.h>
14ad3a212cSNeil Armstrong #include <linux/delay.h>
15ad3a212cSNeil Armstrong #include <linux/slab.h>
16ad3a212cSNeil Armstrong #include <linux/err.h>
17ad3a212cSNeil Armstrong #include <linux/io.h>
18*e9803aacSRob Herring #include <linux/mod_devicetable.h>
19ad3a212cSNeil Armstrong #include <linux/module.h>
20ad3a212cSNeil Armstrong #include <linux/platform_device.h>
21ad3a212cSNeil Armstrong #include <linux/mailbox_controller.h>
22ad3a212cSNeil Armstrong 
23ad3a212cSNeil Armstrong #define INTR_SET_OFS	0x0
24ad3a212cSNeil Armstrong #define INTR_STAT_OFS	0x4
25ad3a212cSNeil Armstrong #define INTR_CLR_OFS	0x8
26ad3a212cSNeil Armstrong 
27ad3a212cSNeil Armstrong #define MHU_SEC_OFFSET	0x0
28ad3a212cSNeil Armstrong #define MHU_LP_OFFSET	0xc
29ad3a212cSNeil Armstrong #define MHU_HP_OFFSET	0x18
30ad3a212cSNeil Armstrong #define TX_REG_OFFSET	0x24
31ad3a212cSNeil Armstrong 
32ad3a212cSNeil Armstrong #define MHU_CHANS	3
33ad3a212cSNeil Armstrong 
34ad3a212cSNeil Armstrong struct platform_mhu_link {
35ad3a212cSNeil Armstrong 	int irq;
36ad3a212cSNeil Armstrong 	void __iomem *tx_reg;
37ad3a212cSNeil Armstrong 	void __iomem *rx_reg;
38ad3a212cSNeil Armstrong };
39ad3a212cSNeil Armstrong 
40ad3a212cSNeil Armstrong struct platform_mhu {
41ad3a212cSNeil Armstrong 	void __iomem *base;
42ad3a212cSNeil Armstrong 	struct platform_mhu_link mlink[MHU_CHANS];
43ad3a212cSNeil Armstrong 	struct mbox_chan chan[MHU_CHANS];
44ad3a212cSNeil Armstrong 	struct mbox_controller mbox;
45ad3a212cSNeil Armstrong };
46ad3a212cSNeil Armstrong 
platform_mhu_rx_interrupt(int irq,void * p)47ad3a212cSNeil Armstrong static irqreturn_t platform_mhu_rx_interrupt(int irq, void *p)
48ad3a212cSNeil Armstrong {
49ad3a212cSNeil Armstrong 	struct mbox_chan *chan = p;
50ad3a212cSNeil Armstrong 	struct platform_mhu_link *mlink = chan->con_priv;
51ad3a212cSNeil Armstrong 	u32 val;
52ad3a212cSNeil Armstrong 
53ad3a212cSNeil Armstrong 	val = readl_relaxed(mlink->rx_reg + INTR_STAT_OFS);
54ad3a212cSNeil Armstrong 	if (!val)
55ad3a212cSNeil Armstrong 		return IRQ_NONE;
56ad3a212cSNeil Armstrong 
57ad3a212cSNeil Armstrong 	mbox_chan_received_data(chan, (void *)&val);
58ad3a212cSNeil Armstrong 
59ad3a212cSNeil Armstrong 	writel_relaxed(val, mlink->rx_reg + INTR_CLR_OFS);
60ad3a212cSNeil Armstrong 
61ad3a212cSNeil Armstrong 	return IRQ_HANDLED;
62ad3a212cSNeil Armstrong }
63ad3a212cSNeil Armstrong 
platform_mhu_last_tx_done(struct mbox_chan * chan)64ad3a212cSNeil Armstrong static bool platform_mhu_last_tx_done(struct mbox_chan *chan)
65ad3a212cSNeil Armstrong {
66ad3a212cSNeil Armstrong 	struct platform_mhu_link *mlink = chan->con_priv;
67ad3a212cSNeil Armstrong 	u32 val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS);
68ad3a212cSNeil Armstrong 
69ad3a212cSNeil Armstrong 	return (val == 0);
70ad3a212cSNeil Armstrong }
71ad3a212cSNeil Armstrong 
platform_mhu_send_data(struct mbox_chan * chan,void * data)72ad3a212cSNeil Armstrong static int platform_mhu_send_data(struct mbox_chan *chan, void *data)
73ad3a212cSNeil Armstrong {
74ad3a212cSNeil Armstrong 	struct platform_mhu_link *mlink = chan->con_priv;
75ad3a212cSNeil Armstrong 	u32 *arg = data;
76ad3a212cSNeil Armstrong 
77ad3a212cSNeil Armstrong 	writel_relaxed(*arg, mlink->tx_reg + INTR_SET_OFS);
78ad3a212cSNeil Armstrong 
79ad3a212cSNeil Armstrong 	return 0;
80ad3a212cSNeil Armstrong }
81ad3a212cSNeil Armstrong 
platform_mhu_startup(struct mbox_chan * chan)82ad3a212cSNeil Armstrong static int platform_mhu_startup(struct mbox_chan *chan)
83ad3a212cSNeil Armstrong {
84ad3a212cSNeil Armstrong 	struct platform_mhu_link *mlink = chan->con_priv;
85ad3a212cSNeil Armstrong 	u32 val;
86ad3a212cSNeil Armstrong 	int ret;
87ad3a212cSNeil Armstrong 
88ad3a212cSNeil Armstrong 	val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS);
89ad3a212cSNeil Armstrong 	writel_relaxed(val, mlink->tx_reg + INTR_CLR_OFS);
90ad3a212cSNeil Armstrong 
91ad3a212cSNeil Armstrong 	ret = request_irq(mlink->irq, platform_mhu_rx_interrupt,
92ad3a212cSNeil Armstrong 			  IRQF_SHARED, "platform_mhu_link", chan);
93ad3a212cSNeil Armstrong 	if (ret) {
94ad3a212cSNeil Armstrong 		dev_err(chan->mbox->dev,
95ad3a212cSNeil Armstrong 			"Unable to acquire IRQ %d\n", mlink->irq);
96ad3a212cSNeil Armstrong 		return ret;
97ad3a212cSNeil Armstrong 	}
98ad3a212cSNeil Armstrong 
99ad3a212cSNeil Armstrong 	return 0;
100ad3a212cSNeil Armstrong }
101ad3a212cSNeil Armstrong 
platform_mhu_shutdown(struct mbox_chan * chan)102ad3a212cSNeil Armstrong static void platform_mhu_shutdown(struct mbox_chan *chan)
103ad3a212cSNeil Armstrong {
104ad3a212cSNeil Armstrong 	struct platform_mhu_link *mlink = chan->con_priv;
105ad3a212cSNeil Armstrong 
106ad3a212cSNeil Armstrong 	free_irq(mlink->irq, chan);
107ad3a212cSNeil Armstrong }
108ad3a212cSNeil Armstrong 
109ad3a212cSNeil Armstrong static const struct mbox_chan_ops platform_mhu_ops = {
110ad3a212cSNeil Armstrong 	.send_data = platform_mhu_send_data,
111ad3a212cSNeil Armstrong 	.startup = platform_mhu_startup,
112ad3a212cSNeil Armstrong 	.shutdown = platform_mhu_shutdown,
113ad3a212cSNeil Armstrong 	.last_tx_done = platform_mhu_last_tx_done,
114ad3a212cSNeil Armstrong };
115ad3a212cSNeil Armstrong 
platform_mhu_probe(struct platform_device * pdev)116ad3a212cSNeil Armstrong static int platform_mhu_probe(struct platform_device *pdev)
117ad3a212cSNeil Armstrong {
118ad3a212cSNeil Armstrong 	int i, err;
119ad3a212cSNeil Armstrong 	struct platform_mhu *mhu;
120ad3a212cSNeil Armstrong 	struct device *dev = &pdev->dev;
121ad3a212cSNeil Armstrong 	int platform_mhu_reg[MHU_CHANS] = {
122ad3a212cSNeil Armstrong 		MHU_SEC_OFFSET, MHU_LP_OFFSET, MHU_HP_OFFSET
123ad3a212cSNeil Armstrong 	};
124ad3a212cSNeil Armstrong 
125ad3a212cSNeil Armstrong 	/* Allocate memory for device */
126ad3a212cSNeil Armstrong 	mhu = devm_kzalloc(dev, sizeof(*mhu), GFP_KERNEL);
127ad3a212cSNeil Armstrong 	if (!mhu)
128ad3a212cSNeil Armstrong 		return -ENOMEM;
129ad3a212cSNeil Armstrong 
130b5e3a1feSCai Huoqing 	mhu->base = devm_platform_ioremap_resource(pdev, 0);
131ad3a212cSNeil Armstrong 	if (IS_ERR(mhu->base)) {
132ad3a212cSNeil Armstrong 		dev_err(dev, "ioremap failed\n");
133ad3a212cSNeil Armstrong 		return PTR_ERR(mhu->base);
134ad3a212cSNeil Armstrong 	}
135ad3a212cSNeil Armstrong 
136ad3a212cSNeil Armstrong 	for (i = 0; i < MHU_CHANS; i++) {
137ad3a212cSNeil Armstrong 		mhu->chan[i].con_priv = &mhu->mlink[i];
138ad3a212cSNeil Armstrong 		mhu->mlink[i].irq = platform_get_irq(pdev, i);
13965d9aa31SRuan Jinjie 		if (mhu->mlink[i].irq < 0)
140ad3a212cSNeil Armstrong 			return mhu->mlink[i].irq;
141ad3a212cSNeil Armstrong 		mhu->mlink[i].rx_reg = mhu->base + platform_mhu_reg[i];
142ad3a212cSNeil Armstrong 		mhu->mlink[i].tx_reg = mhu->mlink[i].rx_reg + TX_REG_OFFSET;
143ad3a212cSNeil Armstrong 	}
144ad3a212cSNeil Armstrong 
145ad3a212cSNeil Armstrong 	mhu->mbox.dev = dev;
146ad3a212cSNeil Armstrong 	mhu->mbox.chans = &mhu->chan[0];
147ad3a212cSNeil Armstrong 	mhu->mbox.num_chans = MHU_CHANS;
148ad3a212cSNeil Armstrong 	mhu->mbox.ops = &platform_mhu_ops;
149ad3a212cSNeil Armstrong 	mhu->mbox.txdone_irq = false;
150ad3a212cSNeil Armstrong 	mhu->mbox.txdone_poll = true;
151ad3a212cSNeil Armstrong 	mhu->mbox.txpoll_period = 1;
152ad3a212cSNeil Armstrong 
153ad3a212cSNeil Armstrong 	platform_set_drvdata(pdev, mhu);
154ad3a212cSNeil Armstrong 
15585a55524SThierry Reding 	err = devm_mbox_controller_register(dev, &mhu->mbox);
156ad3a212cSNeil Armstrong 	if (err) {
157ad3a212cSNeil Armstrong 		dev_err(dev, "Failed to register mailboxes %d\n", err);
158ad3a212cSNeil Armstrong 		return err;
159ad3a212cSNeil Armstrong 	}
160ad3a212cSNeil Armstrong 
161ad3a212cSNeil Armstrong 	dev_info(dev, "Platform MHU Mailbox registered\n");
162ad3a212cSNeil Armstrong 	return 0;
163ad3a212cSNeil Armstrong }
164ad3a212cSNeil Armstrong 
165ad3a212cSNeil Armstrong static const struct of_device_id platform_mhu_dt_ids[] = {
166ad3a212cSNeil Armstrong 	{ .compatible = "amlogic,meson-gxbb-mhu", },
167ad3a212cSNeil Armstrong 	{ /* sentinel */ },
168ad3a212cSNeil Armstrong };
169ad3a212cSNeil Armstrong MODULE_DEVICE_TABLE(of, platform_mhu_dt_ids);
170ad3a212cSNeil Armstrong 
171ad3a212cSNeil Armstrong static struct platform_driver platform_mhu_driver = {
172ad3a212cSNeil Armstrong 	.probe	= platform_mhu_probe,
173ad3a212cSNeil Armstrong 	.driver = {
174ad3a212cSNeil Armstrong 		.name = "platform-mhu",
175ad3a212cSNeil Armstrong 		.of_match_table	= platform_mhu_dt_ids,
176ad3a212cSNeil Armstrong 	},
177ad3a212cSNeil Armstrong };
178ad3a212cSNeil Armstrong 
179ad3a212cSNeil Armstrong module_platform_driver(platform_mhu_driver);
180ad3a212cSNeil Armstrong 
181ad3a212cSNeil Armstrong MODULE_LICENSE("GPL v2");
182ad3a212cSNeil Armstrong MODULE_ALIAS("platform:platform-mhu");
183ad3a212cSNeil Armstrong MODULE_DESCRIPTION("Platform MHU Driver");
184ad3a212cSNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
185