ca88773a48ed7dd73237dcec2b33fa08c139eeef
[firefly-linux-kernel-4.4.55.git] / drivers / mailbox / rockchip_mailbox.c
1 /*
2  * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
3  *
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.
7  *
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
11  * more details.
12  *
13  */
14
15 #include <linux/interrupt.h>
16 #include <linux/io.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>
24
25 #include <linux/rockchip-mailbox.h>
26 #include <linux/scpi_protocol.h>
27
28 #define MAILBOX_A2B_INTEN               0x00
29 #define MAILBOX_A2B_STATUS              0x04
30 #define MAILBOX_A2B_CMD(x)              (0x08 + (x) * 8)
31 #define MAILBOX_A2B_DAT(x)              (0x0c + (x) * 8)
32
33 #define MAILBOX_B2A_INTEN               0x28
34 #define MAILBOX_B2A_STATUS              0x2C
35 #define MAILBOX_B2A_CMD(x)              (0x30 + (x) * 8)
36 #define MAILBOX_B2A_DAT(x)              (0x34 + (x) * 8)
37
38 #define MAILBOX_ATOMIC_LOCK(x)          (0x100 + (x) * 8)
39
40 /* A2B: 0 - 2k */
41 #define A2B_BUF(size, idx)              ((idx) * (size))
42 /* B2A: 2k - 4k */
43 #define B2A_BUF(size, idx)              (((idx) + 4) * (size))
44
45 struct rockchip_mbox_drv_data {
46         int num_chans;
47 };
48
49 struct rockchip_mbox_chan {
50         int idx;
51         struct rockchip_mbox_msg *msg;
52         struct rockchip_mbox *mb;
53 };
54
55 struct rockchip_mbox {
56         struct mbox_controller mbox;
57         struct clk *pclk;
58         void __iomem *mbox_base;
59         /* The base address of share memory to transfer data */
60         void __iomem *buf_base;
61         /* The maximum size of buf for each channel */
62         u32 buf_size;
63         struct rockchip_mbox_chan *chans;
64 };
65
66 static inline int chan_to_idx(struct rockchip_mbox *mb,
67                               struct mbox_chan *chan)
68 {
69         return (chan - mb->mbox.chans);
70 }
71
72 static int rockchip_mbox_send_data(struct mbox_chan *chan, void *data)
73 {
74         struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
75         struct rockchip_mbox_msg *msg = data;
76         int idx = chan_to_idx(mb, chan);
77
78         if (!msg)
79                 return -EINVAL;
80
81         if ((msg->tx_size > mb->buf_size) ||
82             (msg->rx_size > mb->buf_size)) {
83                 dev_err(mb->mbox.dev, "Transmit size over buf size(%d)\n",
84                         mb->buf_size);
85                 return -EINVAL;
86         }
87
88         dev_dbg(mb->mbox.dev, "Chan[%d]: A2B message, cmd 0x%08x\n",
89                 idx, msg->cmd);
90
91         mb->chans[idx].msg = msg;
92
93         if (msg->tx_buf)
94                 memcpy(mb->buf_base + A2B_BUF(mb->buf_size, idx),
95                        msg->tx_buf, msg->tx_size);
96
97         writel_relaxed(msg->cmd, mb->mbox_base + MAILBOX_A2B_CMD(idx));
98         writel_relaxed(msg->rx_size, mb->mbox_base + MAILBOX_A2B_DAT(idx));
99
100         return 0;
101 }
102
103 static int rockchip_mbox_startup(struct mbox_chan *chan)
104 {
105         return 0;
106 }
107
108 static void rockchip_mbox_shutdown(struct mbox_chan *chan)
109 {
110         struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
111         int idx = chan_to_idx(mb, chan);
112
113         mb->chans[idx].msg = NULL;
114 }
115
116 static struct mbox_chan_ops rockchip_mbox_chan_ops = {
117         .send_data      = rockchip_mbox_send_data,
118         .startup        = rockchip_mbox_startup,
119         .shutdown       = rockchip_mbox_shutdown,
120 };
121
122 static irqreturn_t rockchip_mbox_irq(int irq, void *dev_id)
123 {
124         struct rockchip_mbox *mb = (struct rockchip_mbox *)dev_id;
125         u32 status = readl_relaxed(mb->mbox_base + MAILBOX_B2A_STATUS);
126         int idx;
127
128         for (idx = 0; idx < mb->mbox.num_chans; idx++) {
129                 struct rockchip_mbox_msg *msg = mb->chans[idx].msg;
130
131           /* Clear mbox interrupt */
132                 writel_relaxed(1 << idx, mb->mbox_base + MAILBOX_B2A_STATUS);
133
134                 if (!(status & (1 << idx)))
135                         continue;
136                 if (!msg)
137                         continue; /* spurious */
138
139                 if (msg->rx_buf)
140                         memcpy(msg->rx_buf,
141                                mb->buf_base + B2A_BUF(mb->buf_size, idx),
142                                msg->rx_size);
143
144                 mbox_chan_received_data(&mb->mbox.chans[idx], msg);
145                 mb->chans[idx].msg = NULL;
146
147                 dev_dbg(mb->mbox.dev, "Chan[%d]: B2A message, cmd 0x%08x\n",
148                         idx, msg->cmd);
149         }
150
151         return IRQ_HANDLED;
152 }
153
154 static const struct rockchip_mbox_drv_data rk3368_drv_data = {
155         .num_chans = 4,
156 };
157
158 static struct of_device_id rockchip_mbox_of_match[] = {
159         { .compatible = "rockchip,rk3368-mailbox", .data = &rk3368_drv_data },
160         { },
161 };
162 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
163
164 #ifdef CONFIG_PM
165 static int rockchip_mbox_suspend(struct platform_device *pdev,
166                                  pm_message_t state)
167 {
168         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
169
170         if (scpi_sys_set_mcu_state_suspend())
171                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
172         return 0;
173 }
174
175 static int rockchip_mbox_resume(struct platform_device *pdev)
176 {
177         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
178
179         writel_relaxed((1 << mb->mbox.num_chans) - 1,
180                        mb->mbox_base + MAILBOX_B2A_INTEN);
181
182         if (scpi_sys_set_mcu_state_resume())
183                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
184         return 0;
185 }
186 #endif /* CONFIG_PM */
187
188 static int rockchip_mbox_probe(struct platform_device *pdev)
189 {
190         struct rockchip_mbox *mb;
191         const struct of_device_id *match;
192         const struct rockchip_mbox_drv_data *drv_data;
193         struct resource *res;
194         int ret, irq, i;
195
196         if (!pdev->dev.of_node)
197                 return -ENODEV;
198
199         match = of_match_node(rockchip_mbox_of_match, pdev->dev.of_node);
200         drv_data = (const struct rockchip_mbox_drv_data *)match->data;
201
202         mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
203         if (!mb)
204                 return -ENOMEM;
205
206         mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
207                                  sizeof(*mb->chans), GFP_KERNEL);
208         if (!mb->chans)
209                 return -ENOMEM;
210
211         mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
212                                       sizeof(*mb->mbox.chans), GFP_KERNEL);
213         if (!mb->mbox.chans)
214                 return -ENOMEM;
215
216         platform_set_drvdata(pdev, mb);
217
218         mb->mbox.dev = &pdev->dev;
219         mb->mbox.num_chans = drv_data->num_chans;
220         mb->mbox.ops = &rockchip_mbox_chan_ops;
221         mb->mbox.txdone_irq = true;
222
223         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
224         if (!res)
225                 return -ENODEV;
226
227         mb->mbox_base = devm_request_and_ioremap(&pdev->dev, res);
228         if (!mb->mbox_base)
229                 return -ENOMEM;
230
231         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
232         if (!res)
233                 return -ENODEV;
234
235         mb->pclk = devm_clk_get(&pdev->dev, "pclk_mailbox");
236         if (IS_ERR(mb->pclk)) {
237                 ret = PTR_ERR(mb->pclk);
238                 dev_err(&pdev->dev, "failed to get pclk_mailbox clock: %d\n",
239                         ret);
240                 return ret;
241         }
242
243         ret = clk_prepare_enable(mb->pclk);
244         if (ret) {
245                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
246                 return ret;
247         }
248
249         /* Each channel has two buffers for A2B and B2A */
250         mb->buf_size = resource_size(res) / (drv_data->num_chans * 2);
251         mb->buf_base = devm_request_and_ioremap(&pdev->dev, res);
252         if (!mb->buf_base)
253                 return -ENOMEM;
254
255         for (i = 0; i < mb->mbox.num_chans; i++) {
256                 irq = platform_get_irq(pdev, i);
257                 if (irq < 0)
258                         return irq;
259
260                 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
261                                                 rockchip_mbox_irq, IRQF_ONESHOT,
262                                                 dev_name(&pdev->dev), mb);
263                 if (ret < 0)
264                         return ret;
265
266                 mb->chans[i].idx = i;
267                 mb->chans[i].mb = mb;
268                 mb->chans[i].msg = NULL;
269         }
270
271         /* Enable all B2A interrupts */
272         writel_relaxed((1 << mb->mbox.num_chans) - 1,
273                        mb->mbox_base + MAILBOX_B2A_INTEN);
274
275         ret = mbox_controller_register(&mb->mbox);
276         if (ret < 0)
277                 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
278
279         return ret;
280 }
281
282 static int rockchip_mbox_remove(struct platform_device *pdev)
283 {
284         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
285
286         mbox_controller_unregister(&mb->mbox);
287
288         return 0;
289 }
290
291 static struct platform_driver rockchip_mbox_driver = {
292         .probe  = rockchip_mbox_probe,
293         .remove = rockchip_mbox_remove,
294 #ifdef CONFIG_PM
295         .suspend = rockchip_mbox_suspend,
296         .resume = rockchip_mbox_resume,
297 #endif /* CONFIG_PM */
298         .driver = {
299                 .name = "rockchip-mailbox",
300                 .of_match_table = of_match_ptr(rockchip_mbox_of_match),
301         },
302 };
303
304 static int __init rockchip_mbox_init(void)
305 {
306         return platform_driver_register(&rockchip_mbox_driver);
307 }
308 subsys_initcall(rockchip_mbox_init);