2 * Copyright (c) 2015, 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 <linux/rockchip-mailbox.h>
26 #include <linux/scpi_protocol.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 rockchip_mbox_drv_data {
51 struct rockchip_mbox_chan {
53 struct rockchip_mbox_msg *msg;
54 struct rockchip_mbox *mb;
57 struct rockchip_mbox {
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 rockchip_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 rockchip_mbox *mb,
72 struct mbox_chan *chan)
74 return (chan - mb->mbox.chans);
77 static int rockchip_mbox_send_data(struct mbox_chan *chan, void *data)
79 struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
80 struct rockchip_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 rockchip_mbox_startup(struct mbox_chan *chan)
113 static void rockchip_mbox_shutdown(struct mbox_chan *chan)
115 struct rockchip_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 rockchip_mbox_chan_ops = {
122 .send_data = rockchip_mbox_send_data,
123 .startup = rockchip_mbox_startup,
124 .shutdown = rockchip_mbox_shutdown,
127 static irqreturn_t rockchip_mbox_irq(int irq, void *dev_id)
130 struct rockchip_mbox *mb = (struct rockchip_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 rockchip_mbox_isr(int irq, void *dev_id)
148 struct rockchip_mbox_msg *msg = NULL;
149 struct rockchip_mbox *mb = (struct rockchip_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 rockchip_mbox_drv_data rk3368_drv_data = {
183 static struct of_device_id rockchip_mbox_of_match[] = {
184 { .compatible = "rockchip,rk3368-mailbox", .data = &rk3368_drv_data },
187 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
190 static int rockchip_mbox_suspend(struct platform_device *pdev,
193 struct rockchip_mbox *mb = platform_get_drvdata(pdev);
195 if (scpi_sys_set_mcu_state_suspend())
196 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
200 static int rockchip_mbox_resume(struct platform_device *pdev)
202 struct rockchip_mbox *mb = platform_get_drvdata(pdev);
204 writel_relaxed((1 << mb->mbox.num_chans) - 1,
205 mb->mbox_base + MAILBOX_B2A_INTEN);
207 if (scpi_sys_set_mcu_state_resume())
208 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
211 #endif /* CONFIG_PM */
213 static int rockchip_mbox_probe(struct platform_device *pdev)
215 struct rockchip_mbox *mb;
216 const struct of_device_id *match;
217 const struct rockchip_mbox_drv_data *drv_data;
218 struct resource *res;
222 "Rockchip mailbox initialize, version: "MAILBOX_VERSION"\n");
224 if (!pdev->dev.of_node)
227 match = of_match_node(rockchip_mbox_of_match, pdev->dev.of_node);
228 drv_data = (const struct rockchip_mbox_drv_data *)match->data;
230 mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
234 mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
235 sizeof(*mb->chans), GFP_KERNEL);
239 mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
240 sizeof(*mb->mbox.chans), GFP_KERNEL);
244 platform_set_drvdata(pdev, mb);
246 mb->mbox.dev = &pdev->dev;
247 mb->mbox.num_chans = drv_data->num_chans;
248 mb->mbox.ops = &rockchip_mbox_chan_ops;
249 mb->mbox.txdone_irq = true;
251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255 mb->mbox_base = devm_request_and_ioremap(&pdev->dev, res);
259 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
263 mb->pclk = devm_clk_get(&pdev->dev, "pclk_mailbox");
264 if (IS_ERR(mb->pclk)) {
265 ret = PTR_ERR(mb->pclk);
266 dev_err(&pdev->dev, "failed to get pclk_mailbox clock: %d\n",
271 ret = clk_prepare_enable(mb->pclk);
273 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
277 /* Each channel has two buffers for A2B and B2A */
278 mb->buf_size = resource_size(res) / (drv_data->num_chans * 2);
279 mb->buf_base = devm_request_and_ioremap(&pdev->dev, res);
283 for (i = 0; i < mb->mbox.num_chans; i++) {
284 irq = platform_get_irq(pdev, i);
288 ret = devm_request_threaded_irq(&pdev->dev, irq,
290 rockchip_mbox_isr, IRQF_ONESHOT,
291 dev_name(&pdev->dev), mb);
295 mb->chans[i].idx = i;
296 mb->chans[i].mb = mb;
297 mb->chans[i].msg = NULL;
298 idx_map_irq[i] = irq;
301 /* Enable all B2A interrupts */
302 writel_relaxed((1 << mb->mbox.num_chans) - 1,
303 mb->mbox_base + MAILBOX_B2A_INTEN);
305 ret = mbox_controller_register(&mb->mbox);
307 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
312 static int rockchip_mbox_remove(struct platform_device *pdev)
314 struct rockchip_mbox *mb = platform_get_drvdata(pdev);
316 mbox_controller_unregister(&mb->mbox);
321 static struct platform_driver rockchip_mbox_driver = {
322 .probe = rockchip_mbox_probe,
323 .remove = rockchip_mbox_remove,
325 .suspend = rockchip_mbox_suspend,
326 .resume = rockchip_mbox_resume,
327 #endif /* CONFIG_PM */
329 .name = "rockchip-mailbox",
330 .of_match_table = of_match_ptr(rockchip_mbox_of_match),
334 static int __init rockchip_mbox_init(void)
336 return platform_driver_register(&rockchip_mbox_driver);
338 subsys_initcall(rockchip_mbox_init);