clk: rockchip: support setting ddr clock via SCPI APIs
[firefly-linux-kernel-4.4.55.git] / drivers / mailbox / rk3368-mailbox.c
1 /*
2  * Copyright (c) 2017, 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 <soc/rockchip/rk3368-mailbox.h>
26 #include <soc/rockchip/scpi.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 rk3368_mbox_drv_data {
48         int num_chans;
49 };
50
51 struct rk3368_mbox_chan {
52         int idx;
53         struct rk3368_mbox_msg *msg;
54         struct rk3368_mbox *mb;
55 };
56
57 struct rk3368_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 rk3368_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 rk3368_mbox *mb,
72                               struct mbox_chan *chan)
73 {
74         return (chan - mb->mbox.chans);
75 }
76
77 static int rk3368_mbox_send_data(struct mbox_chan *chan, void *data)
78 {
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);
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 rk3368_mbox_startup(struct mbox_chan *chan)
109 {
110         return 0;
111 }
112
113 static void rk3368_mbox_shutdown(struct mbox_chan *chan)
114 {
115         struct rk3368_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 rk3368_mbox_chan_ops = {
122         .send_data      = rk3368_mbox_send_data,
123         .startup        = rk3368_mbox_startup,
124         .shutdown       = rk3368_mbox_shutdown,
125 };
126
127 static irqreturn_t rk3368_mbox_irq(int irq, void *dev_id)
128 {
129         int idx;
130         struct rk3368_mbox *mb = (struct rk3368_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 rk3368_mbox_isr(int irq, void *dev_id)
146 {
147         int idx;
148         struct rk3368_mbox_msg *msg = NULL;
149         struct rk3368_mbox *mb = (struct rk3368_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 rk3368_mbox_drv_data rk3368_drv_data = {
180         .num_chans = 4,
181 };
182
183 static const struct of_device_id rk3368_mbox_of_match[] = {
184         {
185                 .compatible = "rockchip,rk3368-mbox-legacy",
186                 .data = &rk3368_drv_data,
187         },
188         { },
189 };
190 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
191
192 #ifdef CONFIG_PM
193 static int rk3368_mbox_suspend(struct platform_device *pdev,
194                                pm_message_t state)
195 {
196         struct rk3368_mbox *mb = platform_get_drvdata(pdev);
197
198         if (scpi_sys_set_mcu_state_suspend())
199                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
200         return 0;
201 }
202
203 static int rk3368_mbox_resume(struct platform_device *pdev)
204 {
205         struct rk3368_mbox *mb = platform_get_drvdata(pdev);
206
207         writel_relaxed((1 << mb->mbox.num_chans) - 1,
208                        mb->mbox_base + MAILBOX_B2A_INTEN);
209
210         if (scpi_sys_set_mcu_state_resume())
211                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
212         return 0;
213 }
214 #endif /* CONFIG_PM */
215
216 static int rk3368_mbox_probe(struct platform_device *pdev)
217 {
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;
222         int ret, irq, i;
223
224         dev_info(&pdev->dev, "rk3368-mailbox initialized, version: %s\n",
225                  MAILBOX_VERSION);
226
227         if (!pdev->dev.of_node)
228                 return -ENODEV;
229
230         match = of_match_node(rk3368_mbox_of_match, pdev->dev.of_node);
231         drv_data = (const struct rk3368_mbox_drv_data *)match->data;
232
233         mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
234         if (!mb)
235                 return -ENOMEM;
236
237         mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
238                                  sizeof(*mb->chans), GFP_KERNEL);
239         if (!mb->chans)
240                 return -ENOMEM;
241
242         mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
243                                       sizeof(*mb->mbox.chans), GFP_KERNEL);
244         if (!mb->mbox.chans)
245                 return -ENOMEM;
246
247         platform_set_drvdata(pdev, mb);
248
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;
253
254         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255         if (!res)
256                 return -ENODEV;
257
258         mb->mbox_base = devm_ioremap_resource(&pdev->dev, res);
259         if (!mb->mbox_base)
260                 return -ENOMEM;
261
262         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
263         if (!res)
264                 return -ENODEV;
265
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);
269         if (!mb->buf_base)
270                 return -ENOMEM;
271
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",
276                         ret);
277                 return ret;
278         }
279
280         ret = clk_prepare_enable(mb->pclk);
281         if (ret) {
282                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
283                 return ret;
284         }
285
286         for (i = 0; i < mb->mbox.num_chans; i++) {
287                 irq = platform_get_irq(pdev, i);
288                 if (irq < 0)
289                         return irq;
290
291                 ret = devm_request_threaded_irq(&pdev->dev, irq,
292                                                 rk3368_mbox_irq,
293                                                 rk3368_mbox_isr, IRQF_ONESHOT,
294                                                 dev_name(&pdev->dev), mb);
295                 if (ret < 0)
296                         return ret;
297
298                 mb->chans[i].idx = i;
299                 mb->chans[i].mb = mb;
300                 mb->chans[i].msg = NULL;
301                 idx_map_irq[i] = irq;
302         }
303
304         /* Enable all B2A interrupts */
305         writel_relaxed((1 << mb->mbox.num_chans) - 1,
306                        mb->mbox_base + MAILBOX_B2A_INTEN);
307
308         ret = mbox_controller_register(&mb->mbox);
309         if (ret < 0)
310                 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
311
312         return ret;
313 }
314
315 static int rk3368_mbox_remove(struct platform_device *pdev)
316 {
317         struct rk3368_mbox *mb = platform_get_drvdata(pdev);
318
319         mbox_controller_unregister(&mb->mbox);
320
321         return 0;
322 }
323
324 static struct platform_driver rk3368_mbox_driver = {
325         .probe  = rk3368_mbox_probe,
326         .remove = rk3368_mbox_remove,
327 #ifdef CONFIG_PM
328         .suspend = rk3368_mbox_suspend,
329         .resume = rk3368_mbox_resume,
330 #endif /* CONFIG_PM */
331         .driver = {
332                 .name = "rk3368-mailbox",
333                 .of_match_table = of_match_ptr(rk3368_mbox_of_match),
334         },
335 };
336
337 static int __init rk3368_mbox_init(void)
338 {
339         return platform_driver_register(&rk3368_mbox_driver);
340 }
341 subsys_initcall(rk3368_mbox_init);