xref: /openbmc/linux/drivers/mailbox/sun6i-msgbox.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
125831c44SSamuel Holland // SPDX-License-Identifier: GPL-2.0
225831c44SSamuel Holland //
325831c44SSamuel Holland // Copyright (c) 2017-2019 Samuel Holland <samuel@sholland.org>
425831c44SSamuel Holland 
525831c44SSamuel Holland #include <linux/bitops.h>
625831c44SSamuel Holland #include <linux/clk.h>
725831c44SSamuel Holland #include <linux/device.h>
825831c44SSamuel Holland #include <linux/err.h>
925831c44SSamuel Holland #include <linux/interrupt.h>
1025831c44SSamuel Holland #include <linux/io.h>
1125831c44SSamuel Holland #include <linux/kernel.h>
1225831c44SSamuel Holland #include <linux/mailbox_controller.h>
1325831c44SSamuel Holland #include <linux/module.h>
1425831c44SSamuel Holland #include <linux/of.h>
1525831c44SSamuel Holland #include <linux/of_irq.h>
1625831c44SSamuel Holland #include <linux/platform_device.h>
1725831c44SSamuel Holland #include <linux/reset.h>
1825831c44SSamuel Holland #include <linux/spinlock.h>
1925831c44SSamuel Holland 
2025831c44SSamuel Holland #define NUM_CHANS		8
2125831c44SSamuel Holland 
2225831c44SSamuel Holland #define CTRL_REG(n)		(0x0000 + 0x4 * ((n) / 4))
2325831c44SSamuel Holland #define CTRL_RX(n)		BIT(0 + 8 * ((n) % 4))
2425831c44SSamuel Holland #define CTRL_TX(n)		BIT(4 + 8 * ((n) % 4))
2525831c44SSamuel Holland 
2625831c44SSamuel Holland #define REMOTE_IRQ_EN_REG	0x0040
2725831c44SSamuel Holland #define REMOTE_IRQ_STAT_REG	0x0050
2825831c44SSamuel Holland #define LOCAL_IRQ_EN_REG	0x0060
2925831c44SSamuel Holland #define LOCAL_IRQ_STAT_REG	0x0070
3025831c44SSamuel Holland 
3125831c44SSamuel Holland #define RX_IRQ(n)		BIT(0 + 2 * (n))
3225831c44SSamuel Holland #define RX_IRQ_MASK		0x5555
3325831c44SSamuel Holland #define TX_IRQ(n)		BIT(1 + 2 * (n))
3425831c44SSamuel Holland #define TX_IRQ_MASK		0xaaaa
3525831c44SSamuel Holland 
3625831c44SSamuel Holland #define FIFO_STAT_REG(n)	(0x0100 + 0x4 * (n))
3725831c44SSamuel Holland #define FIFO_STAT_MASK		GENMASK(0, 0)
3825831c44SSamuel Holland 
3925831c44SSamuel Holland #define MSG_STAT_REG(n)		(0x0140 + 0x4 * (n))
4025831c44SSamuel Holland #define MSG_STAT_MASK		GENMASK(2, 0)
4125831c44SSamuel Holland 
4225831c44SSamuel Holland #define MSG_DATA_REG(n)		(0x0180 + 0x4 * (n))
4325831c44SSamuel Holland 
4425831c44SSamuel Holland #define mbox_dbg(mbox, ...)	dev_dbg((mbox)->controller.dev, __VA_ARGS__)
4525831c44SSamuel Holland 
4625831c44SSamuel Holland struct sun6i_msgbox {
4725831c44SSamuel Holland 	struct mbox_controller controller;
4825831c44SSamuel Holland 	struct clk *clk;
4925831c44SSamuel Holland 	spinlock_t lock;
5025831c44SSamuel Holland 	void __iomem *regs;
5125831c44SSamuel Holland };
5225831c44SSamuel Holland 
5325831c44SSamuel Holland static bool sun6i_msgbox_last_tx_done(struct mbox_chan *chan);
5425831c44SSamuel Holland static bool sun6i_msgbox_peek_data(struct mbox_chan *chan);
5525831c44SSamuel Holland 
channel_number(struct mbox_chan * chan)5625831c44SSamuel Holland static inline int channel_number(struct mbox_chan *chan)
5725831c44SSamuel Holland {
5825831c44SSamuel Holland 	return chan - chan->mbox->chans;
5925831c44SSamuel Holland }
6025831c44SSamuel Holland 
to_sun6i_msgbox(struct mbox_chan * chan)6125831c44SSamuel Holland static inline struct sun6i_msgbox *to_sun6i_msgbox(struct mbox_chan *chan)
6225831c44SSamuel Holland {
6325831c44SSamuel Holland 	return chan->con_priv;
6425831c44SSamuel Holland }
6525831c44SSamuel Holland 
sun6i_msgbox_irq(int irq,void * dev_id)6625831c44SSamuel Holland static irqreturn_t sun6i_msgbox_irq(int irq, void *dev_id)
6725831c44SSamuel Holland {
6825831c44SSamuel Holland 	struct sun6i_msgbox *mbox = dev_id;
6925831c44SSamuel Holland 	uint32_t status;
7025831c44SSamuel Holland 	int n;
7125831c44SSamuel Holland 
7225831c44SSamuel Holland 	/* Only examine channels that are currently enabled. */
7325831c44SSamuel Holland 	status = readl(mbox->regs + LOCAL_IRQ_EN_REG) &
7425831c44SSamuel Holland 		 readl(mbox->regs + LOCAL_IRQ_STAT_REG);
7525831c44SSamuel Holland 
7625831c44SSamuel Holland 	if (!(status & RX_IRQ_MASK))
7725831c44SSamuel Holland 		return IRQ_NONE;
7825831c44SSamuel Holland 
7925831c44SSamuel Holland 	for (n = 0; n < NUM_CHANS; ++n) {
8025831c44SSamuel Holland 		struct mbox_chan *chan = &mbox->controller.chans[n];
8125831c44SSamuel Holland 
8225831c44SSamuel Holland 		if (!(status & RX_IRQ(n)))
8325831c44SSamuel Holland 			continue;
8425831c44SSamuel Holland 
8525831c44SSamuel Holland 		while (sun6i_msgbox_peek_data(chan)) {
8625831c44SSamuel Holland 			uint32_t msg = readl(mbox->regs + MSG_DATA_REG(n));
8725831c44SSamuel Holland 
8825831c44SSamuel Holland 			mbox_dbg(mbox, "Channel %d received 0x%08x\n", n, msg);
8925831c44SSamuel Holland 			mbox_chan_received_data(chan, &msg);
9025831c44SSamuel Holland 		}
9125831c44SSamuel Holland 
9225831c44SSamuel Holland 		/* The IRQ can be cleared only once the FIFO is empty. */
9325831c44SSamuel Holland 		writel(RX_IRQ(n), mbox->regs + LOCAL_IRQ_STAT_REG);
9425831c44SSamuel Holland 	}
9525831c44SSamuel Holland 
9625831c44SSamuel Holland 	return IRQ_HANDLED;
9725831c44SSamuel Holland }
9825831c44SSamuel Holland 
sun6i_msgbox_send_data(struct mbox_chan * chan,void * data)9925831c44SSamuel Holland static int sun6i_msgbox_send_data(struct mbox_chan *chan, void *data)
10025831c44SSamuel Holland {
10125831c44SSamuel Holland 	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
10225831c44SSamuel Holland 	int n = channel_number(chan);
10325831c44SSamuel Holland 	uint32_t msg = *(uint32_t *)data;
10425831c44SSamuel Holland 
10525831c44SSamuel Holland 	/* Using a channel backwards gets the hardware into a bad state. */
10625831c44SSamuel Holland 	if (WARN_ON_ONCE(!(readl(mbox->regs + CTRL_REG(n)) & CTRL_TX(n))))
10725831c44SSamuel Holland 		return 0;
10825831c44SSamuel Holland 
10925831c44SSamuel Holland 	writel(msg, mbox->regs + MSG_DATA_REG(n));
11025831c44SSamuel Holland 	mbox_dbg(mbox, "Channel %d sent 0x%08x\n", n, msg);
11125831c44SSamuel Holland 
11225831c44SSamuel Holland 	return 0;
11325831c44SSamuel Holland }
11425831c44SSamuel Holland 
sun6i_msgbox_startup(struct mbox_chan * chan)11525831c44SSamuel Holland static int sun6i_msgbox_startup(struct mbox_chan *chan)
11625831c44SSamuel Holland {
11725831c44SSamuel Holland 	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
11825831c44SSamuel Holland 	int n = channel_number(chan);
11925831c44SSamuel Holland 
12025831c44SSamuel Holland 	/* The coprocessor is responsible for setting channel directions. */
12125831c44SSamuel Holland 	if (readl(mbox->regs + CTRL_REG(n)) & CTRL_RX(n)) {
12225831c44SSamuel Holland 		/* Flush the receive FIFO. */
12325831c44SSamuel Holland 		while (sun6i_msgbox_peek_data(chan))
12425831c44SSamuel Holland 			readl(mbox->regs + MSG_DATA_REG(n));
12525831c44SSamuel Holland 		writel(RX_IRQ(n), mbox->regs + LOCAL_IRQ_STAT_REG);
12625831c44SSamuel Holland 
12725831c44SSamuel Holland 		/* Enable the receive IRQ. */
12825831c44SSamuel Holland 		spin_lock(&mbox->lock);
12925831c44SSamuel Holland 		writel(readl(mbox->regs + LOCAL_IRQ_EN_REG) | RX_IRQ(n),
13025831c44SSamuel Holland 		       mbox->regs + LOCAL_IRQ_EN_REG);
13125831c44SSamuel Holland 		spin_unlock(&mbox->lock);
13225831c44SSamuel Holland 	}
13325831c44SSamuel Holland 
13425831c44SSamuel Holland 	mbox_dbg(mbox, "Channel %d startup complete\n", n);
13525831c44SSamuel Holland 
13625831c44SSamuel Holland 	return 0;
13725831c44SSamuel Holland }
13825831c44SSamuel Holland 
sun6i_msgbox_shutdown(struct mbox_chan * chan)13925831c44SSamuel Holland static void sun6i_msgbox_shutdown(struct mbox_chan *chan)
14025831c44SSamuel Holland {
14125831c44SSamuel Holland 	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
14225831c44SSamuel Holland 	int n = channel_number(chan);
14325831c44SSamuel Holland 
14425831c44SSamuel Holland 	if (readl(mbox->regs + CTRL_REG(n)) & CTRL_RX(n)) {
14525831c44SSamuel Holland 		/* Disable the receive IRQ. */
14625831c44SSamuel Holland 		spin_lock(&mbox->lock);
14725831c44SSamuel Holland 		writel(readl(mbox->regs + LOCAL_IRQ_EN_REG) & ~RX_IRQ(n),
14825831c44SSamuel Holland 		       mbox->regs + LOCAL_IRQ_EN_REG);
14925831c44SSamuel Holland 		spin_unlock(&mbox->lock);
15025831c44SSamuel Holland 
15125831c44SSamuel Holland 		/* Attempt to flush the FIFO until the IRQ is cleared. */
15225831c44SSamuel Holland 		do {
15325831c44SSamuel Holland 			while (sun6i_msgbox_peek_data(chan))
15425831c44SSamuel Holland 				readl(mbox->regs + MSG_DATA_REG(n));
15525831c44SSamuel Holland 			writel(RX_IRQ(n), mbox->regs + LOCAL_IRQ_STAT_REG);
15625831c44SSamuel Holland 		} while (readl(mbox->regs + LOCAL_IRQ_STAT_REG) & RX_IRQ(n));
15725831c44SSamuel Holland 	}
15825831c44SSamuel Holland 
15925831c44SSamuel Holland 	mbox_dbg(mbox, "Channel %d shutdown complete\n", n);
16025831c44SSamuel Holland }
16125831c44SSamuel Holland 
sun6i_msgbox_last_tx_done(struct mbox_chan * chan)16225831c44SSamuel Holland static bool sun6i_msgbox_last_tx_done(struct mbox_chan *chan)
16325831c44SSamuel Holland {
16425831c44SSamuel Holland 	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
16525831c44SSamuel Holland 	int n = channel_number(chan);
16625831c44SSamuel Holland 
16725831c44SSamuel Holland 	/*
16825831c44SSamuel Holland 	 * The hardware allows snooping on the remote user's IRQ statuses.
16925831c44SSamuel Holland 	 * We consider a message to be acknowledged only once the receive IRQ
17025831c44SSamuel Holland 	 * for that channel is cleared. Since the receive IRQ for a channel
17125831c44SSamuel Holland 	 * cannot be cleared until the FIFO for that channel is empty, this
17225831c44SSamuel Holland 	 * ensures that the message has actually been read. It also gives the
17325831c44SSamuel Holland 	 * recipient an opportunity to perform minimal processing before
17425831c44SSamuel Holland 	 * acknowledging the message.
17525831c44SSamuel Holland 	 */
17625831c44SSamuel Holland 	return !(readl(mbox->regs + REMOTE_IRQ_STAT_REG) & RX_IRQ(n));
17725831c44SSamuel Holland }
17825831c44SSamuel Holland 
sun6i_msgbox_peek_data(struct mbox_chan * chan)17925831c44SSamuel Holland static bool sun6i_msgbox_peek_data(struct mbox_chan *chan)
18025831c44SSamuel Holland {
18125831c44SSamuel Holland 	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
18225831c44SSamuel Holland 	int n = channel_number(chan);
18325831c44SSamuel Holland 
18425831c44SSamuel Holland 	return readl(mbox->regs + MSG_STAT_REG(n)) & MSG_STAT_MASK;
18525831c44SSamuel Holland }
18625831c44SSamuel Holland 
18725831c44SSamuel Holland static const struct mbox_chan_ops sun6i_msgbox_chan_ops = {
18825831c44SSamuel Holland 	.send_data    = sun6i_msgbox_send_data,
18925831c44SSamuel Holland 	.startup      = sun6i_msgbox_startup,
19025831c44SSamuel Holland 	.shutdown     = sun6i_msgbox_shutdown,
19125831c44SSamuel Holland 	.last_tx_done = sun6i_msgbox_last_tx_done,
19225831c44SSamuel Holland 	.peek_data    = sun6i_msgbox_peek_data,
19325831c44SSamuel Holland };
19425831c44SSamuel Holland 
sun6i_msgbox_probe(struct platform_device * pdev)19525831c44SSamuel Holland static int sun6i_msgbox_probe(struct platform_device *pdev)
19625831c44SSamuel Holland {
19725831c44SSamuel Holland 	struct device *dev = &pdev->dev;
19825831c44SSamuel Holland 	struct mbox_chan *chans;
19925831c44SSamuel Holland 	struct reset_control *reset;
20025831c44SSamuel Holland 	struct sun6i_msgbox *mbox;
20125831c44SSamuel Holland 	int i, ret;
20225831c44SSamuel Holland 
20325831c44SSamuel Holland 	mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
20425831c44SSamuel Holland 	if (!mbox)
20525831c44SSamuel Holland 		return -ENOMEM;
20625831c44SSamuel Holland 
20725831c44SSamuel Holland 	chans = devm_kcalloc(dev, NUM_CHANS, sizeof(*chans), GFP_KERNEL);
20825831c44SSamuel Holland 	if (!chans)
20925831c44SSamuel Holland 		return -ENOMEM;
21025831c44SSamuel Holland 
21125831c44SSamuel Holland 	for (i = 0; i < NUM_CHANS; ++i)
21225831c44SSamuel Holland 		chans[i].con_priv = mbox;
21325831c44SSamuel Holland 
21425831c44SSamuel Holland 	mbox->clk = devm_clk_get(dev, NULL);
21525831c44SSamuel Holland 	if (IS_ERR(mbox->clk)) {
21625831c44SSamuel Holland 		ret = PTR_ERR(mbox->clk);
21725831c44SSamuel Holland 		dev_err(dev, "Failed to get clock: %d\n", ret);
21825831c44SSamuel Holland 		return ret;
21925831c44SSamuel Holland 	}
22025831c44SSamuel Holland 
22125831c44SSamuel Holland 	ret = clk_prepare_enable(mbox->clk);
22225831c44SSamuel Holland 	if (ret) {
22325831c44SSamuel Holland 		dev_err(dev, "Failed to enable clock: %d\n", ret);
22425831c44SSamuel Holland 		return ret;
22525831c44SSamuel Holland 	}
22625831c44SSamuel Holland 
22725831c44SSamuel Holland 	reset = devm_reset_control_get_exclusive(dev, NULL);
22825831c44SSamuel Holland 	if (IS_ERR(reset)) {
22925831c44SSamuel Holland 		ret = PTR_ERR(reset);
23025831c44SSamuel Holland 		dev_err(dev, "Failed to get reset control: %d\n", ret);
23125831c44SSamuel Holland 		goto err_disable_unprepare;
23225831c44SSamuel Holland 	}
23325831c44SSamuel Holland 
23425831c44SSamuel Holland 	/*
23525831c44SSamuel Holland 	 * NOTE: We rely on platform firmware to preconfigure the channel
23625831c44SSamuel Holland 	 * directions, and we share this hardware block with other firmware
23725831c44SSamuel Holland 	 * that runs concurrently with Linux (e.g. a trusted monitor).
23825831c44SSamuel Holland 	 *
23925831c44SSamuel Holland 	 * Therefore, we do *not* assert the reset line if probing fails or
24025831c44SSamuel Holland 	 * when removing the device.
24125831c44SSamuel Holland 	 */
24225831c44SSamuel Holland 	ret = reset_control_deassert(reset);
24325831c44SSamuel Holland 	if (ret) {
24425831c44SSamuel Holland 		dev_err(dev, "Failed to deassert reset: %d\n", ret);
24525831c44SSamuel Holland 		goto err_disable_unprepare;
24625831c44SSamuel Holland 	}
24725831c44SSamuel Holland 
248*f5e2eeb9SCai Huoqing 	mbox->regs = devm_platform_ioremap_resource(pdev, 0);
24925831c44SSamuel Holland 	if (IS_ERR(mbox->regs)) {
25025831c44SSamuel Holland 		ret = PTR_ERR(mbox->regs);
25125831c44SSamuel Holland 		dev_err(dev, "Failed to map MMIO resource: %d\n", ret);
25225831c44SSamuel Holland 		goto err_disable_unprepare;
25325831c44SSamuel Holland 	}
25425831c44SSamuel Holland 
25525831c44SSamuel Holland 	/* Disable all IRQs for this end of the msgbox. */
25625831c44SSamuel Holland 	writel(0, mbox->regs + LOCAL_IRQ_EN_REG);
25725831c44SSamuel Holland 
25825831c44SSamuel Holland 	ret = devm_request_irq(dev, irq_of_parse_and_map(dev->of_node, 0),
25925831c44SSamuel Holland 			       sun6i_msgbox_irq, 0, dev_name(dev), mbox);
26025831c44SSamuel Holland 	if (ret) {
26125831c44SSamuel Holland 		dev_err(dev, "Failed to register IRQ handler: %d\n", ret);
26225831c44SSamuel Holland 		goto err_disable_unprepare;
26325831c44SSamuel Holland 	}
26425831c44SSamuel Holland 
26525831c44SSamuel Holland 	mbox->controller.dev           = dev;
26625831c44SSamuel Holland 	mbox->controller.ops           = &sun6i_msgbox_chan_ops;
26725831c44SSamuel Holland 	mbox->controller.chans         = chans;
26825831c44SSamuel Holland 	mbox->controller.num_chans     = NUM_CHANS;
26925831c44SSamuel Holland 	mbox->controller.txdone_irq    = false;
27025831c44SSamuel Holland 	mbox->controller.txdone_poll   = true;
27125831c44SSamuel Holland 	mbox->controller.txpoll_period = 5;
27225831c44SSamuel Holland 
27325831c44SSamuel Holland 	spin_lock_init(&mbox->lock);
27425831c44SSamuel Holland 	platform_set_drvdata(pdev, mbox);
27525831c44SSamuel Holland 
27625831c44SSamuel Holland 	ret = mbox_controller_register(&mbox->controller);
27725831c44SSamuel Holland 	if (ret) {
27825831c44SSamuel Holland 		dev_err(dev, "Failed to register controller: %d\n", ret);
27925831c44SSamuel Holland 		goto err_disable_unprepare;
28025831c44SSamuel Holland 	}
28125831c44SSamuel Holland 
28225831c44SSamuel Holland 	return 0;
28325831c44SSamuel Holland 
28425831c44SSamuel Holland err_disable_unprepare:
28525831c44SSamuel Holland 	clk_disable_unprepare(mbox->clk);
28625831c44SSamuel Holland 
28725831c44SSamuel Holland 	return ret;
28825831c44SSamuel Holland }
28925831c44SSamuel Holland 
sun6i_msgbox_remove(struct platform_device * pdev)29025831c44SSamuel Holland static int sun6i_msgbox_remove(struct platform_device *pdev)
29125831c44SSamuel Holland {
29225831c44SSamuel Holland 	struct sun6i_msgbox *mbox = platform_get_drvdata(pdev);
29325831c44SSamuel Holland 
29425831c44SSamuel Holland 	mbox_controller_unregister(&mbox->controller);
29525831c44SSamuel Holland 	/* See the comment in sun6i_msgbox_probe about the reset line. */
29625831c44SSamuel Holland 	clk_disable_unprepare(mbox->clk);
29725831c44SSamuel Holland 
29825831c44SSamuel Holland 	return 0;
29925831c44SSamuel Holland }
30025831c44SSamuel Holland 
30125831c44SSamuel Holland static const struct of_device_id sun6i_msgbox_of_match[] = {
30225831c44SSamuel Holland 	{ .compatible = "allwinner,sun6i-a31-msgbox", },
30325831c44SSamuel Holland 	{},
30425831c44SSamuel Holland };
30525831c44SSamuel Holland MODULE_DEVICE_TABLE(of, sun6i_msgbox_of_match);
30625831c44SSamuel Holland 
30725831c44SSamuel Holland static struct platform_driver sun6i_msgbox_driver = {
30825831c44SSamuel Holland 	.driver = {
30925831c44SSamuel Holland 		.name = "sun6i-msgbox",
31025831c44SSamuel Holland 		.of_match_table = sun6i_msgbox_of_match,
31125831c44SSamuel Holland 	},
31225831c44SSamuel Holland 	.probe  = sun6i_msgbox_probe,
31325831c44SSamuel Holland 	.remove = sun6i_msgbox_remove,
31425831c44SSamuel Holland };
31525831c44SSamuel Holland module_platform_driver(sun6i_msgbox_driver);
31625831c44SSamuel Holland 
31725831c44SSamuel Holland MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>");
31825831c44SSamuel Holland MODULE_DESCRIPTION("Allwinner sun6i/sun8i/sun9i/sun50i Message Box");
31925831c44SSamuel Holland MODULE_LICENSE("GPL v2");
320