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