RK3368 Mailbox: add mailbox version information
[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 static inline int chan_to_idx(struct rockchip_mbox *mb,
69                               struct mbox_chan *chan)
70 {
71         return (chan - mb->mbox.chans);
72 }
73
74 static int rockchip_mbox_send_data(struct mbox_chan *chan, void *data)
75 {
76         struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
77         struct rockchip_mbox_msg *msg = data;
78         int idx = chan_to_idx(mb, chan);
79
80         if (!msg)
81                 return -EINVAL;
82
83         if ((msg->tx_size > mb->buf_size) ||
84             (msg->rx_size > mb->buf_size)) {
85                 dev_err(mb->mbox.dev, "Transmit size over buf size(%d)\n",
86                         mb->buf_size);
87                 return -EINVAL;
88         }
89
90         dev_dbg(mb->mbox.dev, "Chan[%d]: A2B message, cmd 0x%08x\n",
91                 idx, msg->cmd);
92
93         mb->chans[idx].msg = msg;
94
95         if (msg->tx_buf)
96                 memcpy(mb->buf_base + A2B_BUF(mb->buf_size, idx),
97                        msg->tx_buf, msg->tx_size);
98
99         writel_relaxed(msg->cmd, mb->mbox_base + MAILBOX_A2B_CMD(idx));
100         writel_relaxed(msg->rx_size, mb->mbox_base + MAILBOX_A2B_DAT(idx));
101
102         return 0;
103 }
104
105 static int rockchip_mbox_startup(struct mbox_chan *chan)
106 {
107         return 0;
108 }
109
110 static void rockchip_mbox_shutdown(struct mbox_chan *chan)
111 {
112         struct rockchip_mbox *mb = dev_get_drvdata(chan->mbox->dev);
113         int idx = chan_to_idx(mb, chan);
114
115         mb->chans[idx].msg = NULL;
116 }
117
118 static struct mbox_chan_ops rockchip_mbox_chan_ops = {
119         .send_data      = rockchip_mbox_send_data,
120         .startup        = rockchip_mbox_startup,
121         .shutdown       = rockchip_mbox_shutdown,
122 };
123
124 static irqreturn_t rockchip_mbox_irq(int irq, void *dev_id)
125 {
126         struct rockchip_mbox *mb = (struct rockchip_mbox *)dev_id;
127         u32 status = readl_relaxed(mb->mbox_base + MAILBOX_B2A_STATUS);
128         int idx;
129
130         for (idx = 0; idx < mb->mbox.num_chans; idx++) {
131                 struct rockchip_mbox_msg *msg = mb->chans[idx].msg;
132
133           /* Clear mbox interrupt */
134                 writel_relaxed(1 << idx, mb->mbox_base + MAILBOX_B2A_STATUS);
135
136                 if (!(status & (1 << idx)))
137                         continue;
138                 if (!msg)
139                         continue; /* spurious */
140
141                 if (msg->rx_buf)
142                         memcpy(msg->rx_buf,
143                                mb->buf_base + B2A_BUF(mb->buf_size, idx),
144                                msg->rx_size);
145
146                 mbox_chan_received_data(&mb->mbox.chans[idx], msg);
147                 mb->chans[idx].msg = NULL;
148
149                 dev_dbg(mb->mbox.dev, "Chan[%d]: B2A message, cmd 0x%08x\n",
150                         idx, msg->cmd);
151         }
152
153         return IRQ_HANDLED;
154 }
155
156 static const struct rockchip_mbox_drv_data rk3368_drv_data = {
157         .num_chans = 4,
158 };
159
160 static struct of_device_id rockchip_mbox_of_match[] = {
161         { .compatible = "rockchip,rk3368-mailbox", .data = &rk3368_drv_data },
162         { },
163 };
164 MODULE_DEVICE_TABLE(of, rockchp_mbox_of_match);
165
166 #ifdef CONFIG_PM
167 static int rockchip_mbox_suspend(struct platform_device *pdev,
168                                  pm_message_t state)
169 {
170         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
171
172         if (scpi_sys_set_mcu_state_suspend())
173                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_suspend timeout.\n");
174         return 0;
175 }
176
177 static int rockchip_mbox_resume(struct platform_device *pdev)
178 {
179         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
180
181         writel_relaxed((1 << mb->mbox.num_chans) - 1,
182                        mb->mbox_base + MAILBOX_B2A_INTEN);
183
184         if (scpi_sys_set_mcu_state_resume())
185                 dev_err(mb->mbox.dev, "scpi_sys_set_mcu_state_resume timeout.\n");
186         return 0;
187 }
188 #endif /* CONFIG_PM */
189
190 static int rockchip_mbox_probe(struct platform_device *pdev)
191 {
192         struct rockchip_mbox *mb;
193         const struct of_device_id *match;
194         const struct rockchip_mbox_drv_data *drv_data;
195         struct resource *res;
196         int ret, irq, i;
197
198         dev_info(&pdev->dev,
199                  "Rockchip mailbox initialize, version: "MAILBOX_VERSION"\n");
200
201         if (!pdev->dev.of_node)
202                 return -ENODEV;
203
204         match = of_match_node(rockchip_mbox_of_match, pdev->dev.of_node);
205         drv_data = (const struct rockchip_mbox_drv_data *)match->data;
206
207         mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
208         if (!mb)
209                 return -ENOMEM;
210
211         mb->chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
212                                  sizeof(*mb->chans), GFP_KERNEL);
213         if (!mb->chans)
214                 return -ENOMEM;
215
216         mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
217                                       sizeof(*mb->mbox.chans), GFP_KERNEL);
218         if (!mb->mbox.chans)
219                 return -ENOMEM;
220
221         platform_set_drvdata(pdev, mb);
222
223         mb->mbox.dev = &pdev->dev;
224         mb->mbox.num_chans = drv_data->num_chans;
225         mb->mbox.ops = &rockchip_mbox_chan_ops;
226         mb->mbox.txdone_irq = true;
227
228         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229         if (!res)
230                 return -ENODEV;
231
232         mb->mbox_base = devm_request_and_ioremap(&pdev->dev, res);
233         if (!mb->mbox_base)
234                 return -ENOMEM;
235
236         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
237         if (!res)
238                 return -ENODEV;
239
240         mb->pclk = devm_clk_get(&pdev->dev, "pclk_mailbox");
241         if (IS_ERR(mb->pclk)) {
242                 ret = PTR_ERR(mb->pclk);
243                 dev_err(&pdev->dev, "failed to get pclk_mailbox clock: %d\n",
244                         ret);
245                 return ret;
246         }
247
248         ret = clk_prepare_enable(mb->pclk);
249         if (ret) {
250                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", ret);
251                 return ret;
252         }
253
254         /* Each channel has two buffers for A2B and B2A */
255         mb->buf_size = resource_size(res) / (drv_data->num_chans * 2);
256         mb->buf_base = devm_request_and_ioremap(&pdev->dev, res);
257         if (!mb->buf_base)
258                 return -ENOMEM;
259
260         for (i = 0; i < mb->mbox.num_chans; i++) {
261                 irq = platform_get_irq(pdev, i);
262                 if (irq < 0)
263                         return irq;
264
265                 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
266                                                 rockchip_mbox_irq, IRQF_ONESHOT,
267                                                 dev_name(&pdev->dev), mb);
268                 if (ret < 0)
269                         return ret;
270
271                 mb->chans[i].idx = i;
272                 mb->chans[i].mb = mb;
273                 mb->chans[i].msg = NULL;
274         }
275
276         /* Enable all B2A interrupts */
277         writel_relaxed((1 << mb->mbox.num_chans) - 1,
278                        mb->mbox_base + MAILBOX_B2A_INTEN);
279
280         ret = mbox_controller_register(&mb->mbox);
281         if (ret < 0)
282                 dev_err(&pdev->dev, "Failed to register mailbox: %d\n", ret);
283
284         return ret;
285 }
286
287 static int rockchip_mbox_remove(struct platform_device *pdev)
288 {
289         struct rockchip_mbox *mb = platform_get_drvdata(pdev);
290
291         mbox_controller_unregister(&mb->mbox);
292
293         return 0;
294 }
295
296 static struct platform_driver rockchip_mbox_driver = {
297         .probe  = rockchip_mbox_probe,
298         .remove = rockchip_mbox_remove,
299 #ifdef CONFIG_PM
300         .suspend = rockchip_mbox_suspend,
301         .resume = rockchip_mbox_resume,
302 #endif /* CONFIG_PM */
303         .driver = {
304                 .name = "rockchip-mailbox",
305                 .of_match_table = of_match_ptr(rockchip_mbox_of_match),
306         },
307 };
308
309 static int __init rockchip_mbox_init(void)
310 {
311         return platform_driver_register(&rockchip_mbox_driver);
312 }
313 subsys_initcall(rockchip_mbox_init);