2 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mailbox_controller.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
25 #include <soc/rockchip/rk3368-mailbox.h>
26 #include <soc/rockchip/scpi.h>
28 #define MAILBOX_VERSION "V1.00"
30 #define MAILBOX_A2B_INTEN 0x00
31 #define MAILBOX_A2B_STATUS 0x04
32 #define MAILBOX_A2B_CMD(x) (0x08 + (x) * 8)
33 #define MAILBOX_A2B_DAT(x) (0x0c + (x) * 8)
35 #define MAILBOX_B2A_INTEN 0x28
36 #define MAILBOX_B2A_STATUS 0x2C
37 #define MAILBOX_B2A_CMD(x) (0x30 + (x) * 8)
38 #define MAILBOX_B2A_DAT(x) (0x34 + (x) * 8)
40 #define MAILBOX_ATOMIC_LOCK(x) (0x100 + (x) * 8)
43 #define A2B_BUF(size, idx) ((idx) * (size))
45 #define B2A_BUF(size, idx) (((idx) + 4) * (size))
47 struct rk3368_mbox_drv_data {
51 struct rk3368_mbox_chan {
53 struct rk3368_mbox_msg *msg;
54 struct rk3368_mbox *mb;
58 struct mbox_controller mbox;
60 void __iomem *mbox_base;
61 /* The base address of share memory to transfer data */
62 void __iomem *buf_base;
63 /* The maximum size of buf for each channel */
65 struct rk3368_mbox_chan *chans;
68 #define MBOX_CHAN_NUMS 4
69 int idx_map_irq[MBOX_CHAN_NUMS] = {0, 0, 0, 0};
71 static inline int chan_to_idx(struct rk3368_mbox *mb,
72 struct mbox_chan *chan)
74 return (chan - mb->mbox.chans);
77 static int rk3368_mbox_send_data(struct mbox_chan *chan, void *data)
79 struct rk3368_mbox *mb = dev_get_drvdata(chan->mbox->dev);
80 struct rk3368_mbox_msg *msg = data;
81 int idx = chan_to_idx(mb, chan);
86 if ((msg->tx_size > mb->buf_size) ||
87 (msg->rx_size > mb->buf_size)) {
88 dev_err(mb->mbox.dev, "Transmit size over buf size(%d)\n",
93 dev_dbg(mb->mbox.dev, "Chan[%d]: A2B message, cmd 0x%08x\n",
96 mb->chans[idx].msg = msg;
99 memcpy(mb->buf_base + A2B_BUF(mb->buf_size, idx),
100 msg->tx_buf, msg->tx_size);
102 writel_relaxed(msg->cmd, mb->mbox_base + MAILBOX_A2B_CMD(idx));
103 writel_relaxed(msg->rx_size, mb->mbox_base + MAILBOX_A2B_DAT(idx));
108 static int rk3368_mbox_startup(struct mbox_chan *chan)
113 static void rk3368_mbox_shutdown(struct mbox_chan *chan)
115 struct rk3368_mbox *mb = dev_get_drvdata(chan->mbox->dev);
116 int idx = chan_to_idx(mb, chan);
118 mb->chans[idx].msg = NULL;
121 static struct mbox_chan_ops rk3368_mbox_chan_ops = {
122 .send_data = rk3368_mbox_send_data,
123 .startup = rk3368_mbox_startup,
124 .shutdown = rk3368_mbox_shutdown,
127 static irqreturn_t rk3368_mbox_irq(int irq, void *dev_id)
130 struct rk3368_mbox *mb = (struct rk3368_mbox *)dev_id;
131 u32 status = readl_relaxed(mb->mbox_base + MAILBOX_B2A_STATUS);
133 for (idx = 0; idx < mb->mbox.num_chans; idx++) {
134 if ((status & (1 << idx)) && (irq == idx_map_irq[idx])) {
135 /* Clear mbox interrupt */
136 writel_relaxed(1 << idx,
137 mb->mbox_base + MAILBOX_B2A_STATUS);
138 return IRQ_WAKE_THREAD;
145 static irqreturn_t rk3368_mbox_isr(int irq, void *dev_id)
148 struct rk3368_mbox_msg *msg = NULL;
149 struct rk3368_mbox *mb = (struct rk3368_mbox *)dev_id;
151 for (idx = 0; idx < mb->mbox.num_chans; idx++) {
152 if (irq != idx_map_irq[idx])
155 msg = mb->chans[idx].msg;
157 dev_err(mb->mbox.dev,
158 "Chan[%d]: B2A message is NULL\n", idx);
159 break; /* spurious */
164 mb->buf_base + B2A_BUF(mb->buf_size, idx),
167 mbox_chan_received_data(&mb->mbox.chans[idx], msg);
168 mb->chans[idx].msg = NULL;
170 dev_dbg(mb->mbox.dev, "Chan[%d]: B2A message, cmd 0x%08x\n",
179 static const struct rk3368_mbox_drv_data rk3368_drv_data = {
183 static const struct of_device_id rk3368_mbox_of_match[] = {
185 .compatible = "rockchip,rk3368-mbox-legacy",
186 .data = &rk3368_drv_data,
190 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
193 static int rk3368_mbox_suspend(struct platform_device *pdev,
196 struct rk3368_mbox *mb = platform_get_drvdata(pdev);
198 if (scpi_sys_set_mcu_state_suspend())
199 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
203 static int rk3368_mbox_resume(struct platform_device *pdev)
205 struct rk3368_mbox *mb = platform_get_drvdata(pdev);
207 writel_relaxed((1 << mb->mbox.num_chans) - 1,
208 mb->mbox_base + MAILBOX_B2A_INTEN);
210 if (scpi_sys_set_mcu_state_resume())
211 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
214 #endif /* CONFIG_PM */
216 static int rk3368_mbox_probe(struct platform_device *pdev)
218 struct rk3368_mbox *mb;
219 const struct of_device_id *match;
220 const struct rk3368_mbox_drv_data *drv_data;
221 struct resource *res;
224 dev_info(&pdev->dev, "rk3368-mailbox initialized, version: %s\n",
227 if (!pdev->dev.of_node)
230 match = of_match_node(rk3368_mbox_of_match, pdev->dev.of_node);
231 drv_data = (const struct rk3368_mbox_drv_data *)match->data;
233 mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
237 mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
238 sizeof(*mb->chans), GFP_KERNEL);
242 mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
243 sizeof(*mb->mbox.chans), GFP_KERNEL);
247 platform_set_drvdata(pdev, mb);
249 mb->mbox.dev = &pdev->dev;
250 mb->mbox.num_chans = drv_data->num_chans;
251 mb->mbox.ops = &rk3368_mbox_chan_ops;
252 mb->mbox.txdone_irq = true;
254 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 mb->mbox_base = devm_ioremap_resource(&pdev->dev, res);
262 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
266 /* Each channel has two buffers for A2B and B2A */
267 mb->buf_size = resource_size(res) / (drv_data->num_chans * 2);
268 mb->buf_base = devm_ioremap_resource(&pdev->dev, res);
272 mb->pclk = devm_clk_get(&pdev->dev, "pclk_mailbox");
273 if (IS_ERR(mb->pclk)) {
274 ret = PTR_ERR(mb->pclk);
275 dev_err(&pdev->dev, "failed to get pclk_mailbox clock: %d\n",
280 ret = clk_prepare_enable(mb->pclk);
282 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
286 for (i = 0; i < mb->mbox.num_chans; i++) {
287 irq = platform_get_irq(pdev, i);
291 ret = devm_request_threaded_irq(&pdev->dev, irq,
293 rk3368_mbox_isr, IRQF_ONESHOT,
294 dev_name(&pdev->dev), mb);
298 mb->chans[i].idx = i;
299 mb->chans[i].mb = mb;
300 mb->chans[i].msg = NULL;
301 idx_map_irq[i] = irq;
304 /* Enable all B2A interrupts */
305 writel_relaxed((1 << mb->mbox.num_chans) - 1,
306 mb->mbox_base + MAILBOX_B2A_INTEN);
308 ret = mbox_controller_register(&mb->mbox);
310 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
315 static int rk3368_mbox_remove(struct platform_device *pdev)
317 struct rk3368_mbox *mb = platform_get_drvdata(pdev);
319 mbox_controller_unregister(&mb->mbox);
324 static struct platform_driver rk3368_mbox_driver = {
325 .probe = rk3368_mbox_probe,
326 .remove = rk3368_mbox_remove,
328 .suspend = rk3368_mbox_suspend,
329 .resume = rk3368_mbox_resume,
330 #endif /* CONFIG_PM */
332 .name = "rk3368-mailbox",
333 .of_match_table = of_match_ptr(rk3368_mbox_of_match),
337 static int __init rk3368_mbox_init(void)
339 return platform_driver_register(&rk3368_mbox_driver);
341 subsys_initcall(rk3368_mbox_init);