db9943179476772cb6dee32e04a67eee01823820
[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_VERSION                 "V1.00"
29
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)
34
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)
39
40 #define MAILBOX_ATOMIC_LOCK(x)          (0x100 + (x) * 8)
41
42 /* A2B: 0 - 2k */
43 #define A2B_BUF(size, idx)              ((idx) * (size))
44 /* B2A: 2k - 4k */
45 #define B2A_BUF(size, idx)              (((idx) + 4) * (size))
46
47 struct rockchip_mbox_drv_data {
48         int num_chans;
49 };
50
51 struct rockchip_mbox_chan {
52         int idx;
53         struct rockchip_mbox_msg *msg;
54         struct rockchip_mbox *mb;
55 };
56
57 struct rockchip_mbox {
58         struct mbox_controller mbox;
59         struct clk *pclk;
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 */
64         u32 buf_size;
65         struct rockchip_mbox_chan *chans;
66 };
67
68 #define MBOX_CHAN_NUMS  4
69 int idx_map_irq[MBOX_CHAN_NUMS] = {0, 0, 0, 0};
70
71 static inline int chan_to_idx(struct rockchip_mbox *mb,
72                               struct mbox_chan *chan)
73 {
74         return (chan - mb->mbox.chans);
75 }
76
77 static int rockchip_mbox_send_data(struct mbox_chan *chan, void *data)
78 {
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);
82
83         if (!msg)
84                 return -EINVAL;
85
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",
89                         mb->buf_size);
90                 return -EINVAL;
91         }
92
93         dev_dbg(mb->mbox.dev, "Chan[%d]: A2B message, cmd 0x%08x\n",
94                 idx, msg->cmd);
95
96         mb->chans[idx].msg = msg;
97
98         if (msg->tx_buf)
99                 memcpy(mb->buf_base + A2B_BUF(mb->buf_size, idx),
100                        msg->tx_buf, msg->tx_size);
101
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));
104
105         return 0;
106 }
107
108 static int rockchip_mbox_startup(struct mbox_chan *chan)
109 {
110         return 0;
111 }
112
113 static void rockchip_mbox_shutdown(struct mbox_chan *chan)
114 {
115         struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
116         int idx = chan_to_idx(mb, chan);
117
118         mb->chans[idx].msg = NULL;
119 }
120
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,
125 };
126
127 static irqreturn_t rockchip_mbox_irq(int irq, void *dev_id)
128 {
129         int idx;
130         struct rockchip_mbox *mb = (struct rockchip_mbox *)dev_id;
131         u32 status = readl_relaxed(mb->mbox_base + MAILBOX_B2A_STATUS);
132
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;
139                 }
140         }
141
142         return IRQ_NONE;
143 }
144
145 static irqreturn_t rockchip_mbox_isr(int irq, void *dev_id)
146 {
147         int idx;
148         struct rockchip_mbox_msg *msg = NULL;
149         struct rockchip_mbox *mb = (struct rockchip_mbox *)dev_id;
150
151         for (idx = 0; idx < mb->mbox.num_chans; idx++) {
152                 if (irq != idx_map_irq[idx])
153                         continue;
154
155                 msg = mb->chans[idx].msg;
156                 if (!msg) {
157                         dev_err(mb->mbox.dev,
158                                 "Chan[%d]: B2A message is NULL\n", idx);
159                         break; /* spurious */
160                 }
161
162                 if (msg->rx_buf)
163                         memcpy(msg->rx_buf,
164                                mb->buf_base + B2A_BUF(mb->buf_size, idx),
165                                msg->rx_size);
166
167                 mbox_chan_received_data(&mb->mbox.chans[idx], msg);
168                 mb->chans[idx].msg = NULL;
169
170                 dev_dbg(mb->mbox.dev, "Chan[%d]: B2A message, cmd 0x%08x\n",
171                         idx, msg->cmd);
172
173                 break;
174         }
175
176         return IRQ_HANDLED;
177 }
178
179 static const struct rockchip_mbox_drv_data rk3368_drv_data = {
180         .num_chans = 4,
181 };
182
183 static struct of_device_id rockchip_mbox_of_match[] = {
184         { .compatible = "rockchip,rk3368-mailbox", .data = &rk3368_drv_data },
185         { },
186 };
187 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
188
189 #ifdef CONFIG_PM
190 static int rockchip_mbox_suspend(struct platform_device *pdev,
191                                  pm_message_t state)
192 {
193         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
194
195         if (scpi_sys_set_mcu_state_suspend())
196                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
197         return 0;
198 }
199
200 static int rockchip_mbox_resume(struct platform_device *pdev)
201 {
202         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
203
204         writel_relaxed((1 << mb->mbox.num_chans) - 1,
205                        mb->mbox_base + MAILBOX_B2A_INTEN);
206
207         if (scpi_sys_set_mcu_state_resume())
208                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
209         return 0;
210 }
211 #endif /* CONFIG_PM */
212
213 static int rockchip_mbox_probe(struct platform_device *pdev)
214 {
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;
219         int ret, irq, i;
220
221         dev_info(&pdev->dev,
222                  "Rockchip mailbox initialize, version: "MAILBOX_VERSION"\n");
223
224         if (!pdev->dev.of_node)
225                 return -ENODEV;
226
227         match = of_match_node(rockchip_mbox_of_match, pdev->dev.of_node);
228         drv_data = (const struct rockchip_mbox_drv_data *)match->data;
229
230         mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
231         if (!mb)
232                 return -ENOMEM;
233
234         mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
235                                  sizeof(*mb->chans), GFP_KERNEL);
236         if (!mb->chans)
237                 return -ENOMEM;
238
239         mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
240                                       sizeof(*mb->mbox.chans), GFP_KERNEL);
241         if (!mb->mbox.chans)
242                 return -ENOMEM;
243
244         platform_set_drvdata(pdev, mb);
245
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;
250
251         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
252         if (!res)
253                 return -ENODEV;
254
255         mb->mbox_base = devm_request_and_ioremap(&pdev->dev, res);
256         if (!mb->mbox_base)
257                 return -ENOMEM;
258
259         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
260         if (!res)
261                 return -ENODEV;
262
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",
267                         ret);
268                 return ret;
269         }
270
271         ret = clk_prepare_enable(mb->pclk);
272         if (ret) {
273                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
274                 return ret;
275         }
276
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);
280         if (!mb->buf_base)
281                 return -ENOMEM;
282
283         for (i = 0; i < mb->mbox.num_chans; i++) {
284                 irq = platform_get_irq(pdev, i);
285                 if (irq < 0)
286                         return irq;
287
288                 ret = devm_request_threaded_irq(&pdev->dev, irq,
289                                                 rockchip_mbox_irq,
290                                                 rockchip_mbox_isr, IRQF_ONESHOT,
291                                                 dev_name(&pdev->dev), mb);
292                 if (ret < 0)
293                         return ret;
294
295                 mb->chans[i].idx = i;
296                 mb->chans[i].mb = mb;
297                 mb->chans[i].msg = NULL;
298                 idx_map_irq[i] = irq;
299         }
300
301         /* Enable all B2A interrupts */
302         writel_relaxed((1 << mb->mbox.num_chans) - 1,
303                        mb->mbox_base + MAILBOX_B2A_INTEN);
304
305         ret = mbox_controller_register(&mb->mbox);
306         if (ret < 0)
307                 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
308
309         return ret;
310 }
311
312 static int rockchip_mbox_remove(struct platform_device *pdev)
313 {
314         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
315
316         mbox_controller_unregister(&mb->mbox);
317
318         return 0;
319 }
320
321 static struct platform_driver rockchip_mbox_driver = {
322         .probe  = rockchip_mbox_probe,
323         .remove = rockchip_mbox_remove,
324 #ifdef CONFIG_PM
325         .suspend = rockchip_mbox_suspend,
326         .resume = rockchip_mbox_resume,
327 #endif /* CONFIG_PM */
328         .driver = {
329                 .name = "rockchip-mailbox",
330                 .of_match_table = of_match_ptr(rockchip_mbox_of_match),
331         },
332 };
333
334 static int __init rockchip_mbox_init(void)
335 {
336         return platform_driver_register(&rockchip_mbox_driver);
337 }
338 subsys_initcall(rockchip_mbox_init);