]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/arm/mach-omap2/mailbox.c
omap: mailbox: only compile for configured archs
[mirror_ubuntu-bionic-kernel.git] / arch / arm / mach-omap2 / mailbox.c
CommitLineData
340a614a 1/*
733ecc5c 2 * Mailbox reservation modules for OMAP2/3
340a614a 3 *
733ecc5c 4 * Copyright (C) 2006-2009 Nokia Corporation
340a614a 5 * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
733ecc5c 6 * and Paul Mundt
340a614a
HD
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/clk.h>
15#include <linux/err.h>
16#include <linux/platform_device.h>
fced80c7 17#include <linux/io.h>
ce491cf8 18#include <plat/mailbox.h>
a09e64fb 19#include <mach/irqs.h>
340a614a 20
5f00ec64
S
21#define DRV_NAME "omap2-mailbox"
22
733ecc5c
HD
23#define MAILBOX_REVISION 0x000
24#define MAILBOX_SYSCONFIG 0x010
25#define MAILBOX_SYSSTATUS 0x014
26#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
27#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
28#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
29#define MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
30#define MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
340a614a 31
5f00ec64
S
32#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 10 * (u))
33#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 10 * (u))
34#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 10 * (u))
35
36#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
37#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
340a614a 38
1ffe627d
HD
39/* SYSCONFIG: register bit definition */
40#define AUTOIDLE (1 << 0)
41#define SOFTRESET (1 << 1)
42#define SMARTIDLE (2 << 3)
a6a60228 43#define OMAP4_SOFTRESET (1 << 0)
4499ce42
SA
44#define OMAP4_NOIDLE (1 << 2)
45#define OMAP4_SMARTIDLE (2 << 2)
1ffe627d
HD
46
47/* SYSSTATUS: register bit definition */
48#define RESETDONE (1 << 0)
49
c75ee752 50#define MBOX_REG_SIZE 0x120
5f00ec64
S
51
52#define OMAP4_MBOX_REG_SIZE 0x130
53
c75ee752 54#define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
5f00ec64 55#define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
c75ee752 56
6c20a683 57static void __iomem *mbox_base;
340a614a
HD
58
59struct omap_mbox2_fifo {
60 unsigned long msg;
61 unsigned long fifo_stat;
62 unsigned long msg_stat;
63};
64
65struct omap_mbox2_priv {
66 struct omap_mbox2_fifo tx_fifo;
67 struct omap_mbox2_fifo rx_fifo;
68 unsigned long irqenable;
69 unsigned long irqstatus;
70 u32 newmsg_bit;
71 u32 notfull_bit;
5f00ec64
S
72 u32 ctx[OMAP4_MBOX_NR_REGS];
73 unsigned long irqdisable;
340a614a
HD
74};
75
76static struct clk *mbox_ick_handle;
77
bfbdcf8a
HD
78static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
79 omap_mbox_type_t irq);
80
6c20a683 81static inline unsigned int mbox_read_reg(size_t ofs)
340a614a 82{
6c20a683 83 return __raw_readl(mbox_base + ofs);
340a614a
HD
84}
85
6c20a683 86static inline void mbox_write_reg(u32 val, size_t ofs)
340a614a 87{
6c20a683 88 __raw_writel(val, mbox_base + ofs);
340a614a
HD
89}
90
91/* Mailbox H/W preparations */
bfbdcf8a 92static int omap2_mbox_startup(struct omap_mbox *mbox)
340a614a 93{
1ffe627d
HD
94 u32 l;
95 unsigned long timeout;
340a614a
HD
96
97 mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
98 if (IS_ERR(mbox_ick_handle)) {
0cd7e1cc 99 printk(KERN_ERR "Could not get mailboxes_ick: %ld\n",
5f00ec64
S
100 PTR_ERR(mbox_ick_handle));
101 return PTR_ERR(mbox_ick_handle);
340a614a
HD
102 }
103 clk_enable(mbox_ick_handle);
104
a6a60228
SA
105 if (cpu_is_omap44xx()) {
106 mbox_write_reg(OMAP4_SOFTRESET, MAILBOX_SYSCONFIG);
107 timeout = jiffies + msecs_to_jiffies(20);
108 do {
109 l = mbox_read_reg(MAILBOX_SYSCONFIG);
110 if (!(l & OMAP4_SOFTRESET))
111 break;
112 } while (!time_after(jiffies, timeout));
113
114 if (l & OMAP4_SOFTRESET) {
115 pr_err("Can't take mailbox out of reset\n");
116 return -ENODEV;
117 }
118 } else {
119 mbox_write_reg(SOFTRESET, MAILBOX_SYSCONFIG);
120 timeout = jiffies + msecs_to_jiffies(20);
121 do {
122 l = mbox_read_reg(MAILBOX_SYSSTATUS);
123 if (l & RESETDONE)
124 break;
125 } while (!time_after(jiffies, timeout));
126
127 if (!(l & RESETDONE)) {
128 pr_err("Can't take mailbox out of reset\n");
129 return -ENODEV;
130 }
1ffe627d
HD
131 }
132
94fc58c6 133 l = mbox_read_reg(MAILBOX_REVISION);
909f9dc7 134 pr_debug("omap mailbox rev %d.%d\n", (l & 0xf0) >> 4, (l & 0x0f));
94fc58c6 135
4499ce42
SA
136 if (cpu_is_omap44xx())
137 l = OMAP4_SMARTIDLE;
138 else
139 l = SMARTIDLE | AUTOIDLE;
340a614a
HD
140 mbox_write_reg(l, MAILBOX_SYSCONFIG);
141
bfbdcf8a
HD
142 omap2_mbox_enable_irq(mbox, IRQ_RX);
143
340a614a
HD
144 return 0;
145}
146
bfbdcf8a 147static void omap2_mbox_shutdown(struct omap_mbox *mbox)
340a614a
HD
148{
149 clk_disable(mbox_ick_handle);
150 clk_put(mbox_ick_handle);
5f00ec64 151 mbox_ick_handle = NULL;
340a614a
HD
152}
153
154/* Mailbox FIFO handle functions */
bfbdcf8a 155static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
340a614a
HD
156{
157 struct omap_mbox2_fifo *fifo =
158 &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
159 return (mbox_msg_t) mbox_read_reg(fifo->msg);
160}
161
bfbdcf8a 162static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a
HD
163{
164 struct omap_mbox2_fifo *fifo =
165 &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
166 mbox_write_reg(msg, fifo->msg);
167}
168
bfbdcf8a 169static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
340a614a
HD
170{
171 struct omap_mbox2_fifo *fifo =
172 &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
173 return (mbox_read_reg(fifo->msg_stat) == 0);
174}
175
bfbdcf8a 176static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
340a614a
HD
177{
178 struct omap_mbox2_fifo *fifo =
179 &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
5f00ec64 180 return mbox_read_reg(fifo->fifo_stat);
340a614a
HD
181}
182
183/* Mailbox IRQ handle functions */
bfbdcf8a 184static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
340a614a
HD
185 omap_mbox_type_t irq)
186{
187 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
188 u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
189
190 l = mbox_read_reg(p->irqenable);
191 l |= bit;
192 mbox_write_reg(l, p->irqenable);
193}
194
bfbdcf8a 195static void omap2_mbox_disable_irq(struct omap_mbox *mbox,
340a614a
HD
196 omap_mbox_type_t irq)
197{
198 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
199 u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
5f00ec64 200 l = mbox_read_reg(p->irqdisable);
340a614a 201 l &= ~bit;
5f00ec64 202 mbox_write_reg(l, p->irqdisable);
340a614a
HD
203}
204
bfbdcf8a 205static void omap2_mbox_ack_irq(struct omap_mbox *mbox,
340a614a
HD
206 omap_mbox_type_t irq)
207{
208 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
209 u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
210
211 mbox_write_reg(bit, p->irqstatus);
8828880d
HD
212
213 /* Flush posted write for irq status to avoid spurious interrupts */
214 mbox_read_reg(p->irqstatus);
340a614a
HD
215}
216
bfbdcf8a 217static int omap2_mbox_is_irq(struct omap_mbox *mbox,
340a614a
HD
218 omap_mbox_type_t irq)
219{
220 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
221 u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
222 u32 enable = mbox_read_reg(p->irqenable);
223 u32 status = mbox_read_reg(p->irqstatus);
224
5f00ec64 225 return (int)(enable & status & bit);
340a614a
HD
226}
227
c75ee752
HD
228static void omap2_mbox_save_ctx(struct omap_mbox *mbox)
229{
230 int i;
231 struct omap_mbox2_priv *p = mbox->priv;
5f00ec64
S
232 int nr_regs;
233 if (cpu_is_omap44xx())
234 nr_regs = OMAP4_MBOX_NR_REGS;
235 else
236 nr_regs = MBOX_NR_REGS;
237 for (i = 0; i < nr_regs; i++) {
c75ee752
HD
238 p->ctx[i] = mbox_read_reg(i * sizeof(u32));
239
240 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
241 i, p->ctx[i]);
242 }
243}
244
245static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
246{
247 int i;
248 struct omap_mbox2_priv *p = mbox->priv;
5f00ec64
S
249 int nr_regs;
250 if (cpu_is_omap44xx())
251 nr_regs = OMAP4_MBOX_NR_REGS;
252 else
253 nr_regs = MBOX_NR_REGS;
254 for (i = 0; i < nr_regs; i++) {
c75ee752
HD
255 mbox_write_reg(p->ctx[i], i * sizeof(u32));
256
257 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
258 i, p->ctx[i]);
259 }
260}
261
340a614a
HD
262static struct omap_mbox_ops omap2_mbox_ops = {
263 .type = OMAP_MBOX_TYPE2,
264 .startup = omap2_mbox_startup,
265 .shutdown = omap2_mbox_shutdown,
266 .fifo_read = omap2_mbox_fifo_read,
267 .fifo_write = omap2_mbox_fifo_write,
268 .fifo_empty = omap2_mbox_fifo_empty,
269 .fifo_full = omap2_mbox_fifo_full,
270 .enable_irq = omap2_mbox_enable_irq,
271 .disable_irq = omap2_mbox_disable_irq,
272 .ack_irq = omap2_mbox_ack_irq,
273 .is_irq = omap2_mbox_is_irq,
c75ee752
HD
274 .save_ctx = omap2_mbox_save_ctx,
275 .restore_ctx = omap2_mbox_restore_ctx,
340a614a
HD
276};
277
278/*
279 * MAILBOX 0: ARM -> DSP,
280 * MAILBOX 1: ARM <- DSP.
281 * MAILBOX 2: ARM -> IVA,
282 * MAILBOX 3: ARM <- IVA.
283 */
284
285/* FIXME: the following structs should be filled automatically by the user id */
07d65d8b 286
14476bd9 287#if defined(CONFIG_ARCH_OMAP3430) || defined(CONFIG_ARCH_OMAP2420)
340a614a
HD
288/* DSP */
289static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
290 .tx_fifo = {
733ecc5c
HD
291 .msg = MAILBOX_MESSAGE(0),
292 .fifo_stat = MAILBOX_FIFOSTATUS(0),
340a614a
HD
293 },
294 .rx_fifo = {
733ecc5c
HD
295 .msg = MAILBOX_MESSAGE(1),
296 .msg_stat = MAILBOX_MSGSTATUS(1),
340a614a 297 },
733ecc5c
HD
298 .irqenable = MAILBOX_IRQENABLE(0),
299 .irqstatus = MAILBOX_IRQSTATUS(0),
340a614a
HD
300 .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
301 .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
5f00ec64
S
302 .irqdisable = MAILBOX_IRQENABLE(0),
303};
304
07d65d8b
FC
305struct omap_mbox mbox_dsp_info = {
306 .name = "dsp",
307 .ops = &omap2_mbox_ops,
308 .priv = &omap2_mbox_dsp_priv,
309};
14476bd9 310#endif
07d65d8b 311
14476bd9 312#if defined(CONFIG_ARCH_OMAP3430)
898ee756 313struct omap_mbox *omap3_mboxes[] = { &mbox_dsp_info, NULL };
14476bd9 314#endif
898ee756 315
07d65d8b 316#if defined(CONFIG_ARCH_OMAP2420)
07d65d8b
FC
317/* IVA */
318static struct omap_mbox2_priv omap2_mbox_iva_priv = {
319 .tx_fifo = {
320 .msg = MAILBOX_MESSAGE(2),
321 .fifo_stat = MAILBOX_FIFOSTATUS(2),
322 },
323 .rx_fifo = {
324 .msg = MAILBOX_MESSAGE(3),
325 .msg_stat = MAILBOX_MSGSTATUS(3),
326 },
327 .irqenable = MAILBOX_IRQENABLE(3),
328 .irqstatus = MAILBOX_IRQSTATUS(3),
329 .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
330 .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
331 .irqdisable = MAILBOX_IRQENABLE(3),
332};
333
334static struct omap_mbox mbox_iva_info = {
335 .name = "iva",
336 .ops = &omap2_mbox_ops,
337 .priv = &omap2_mbox_iva_priv,
338};
898ee756
FC
339
340struct omap_mbox *omap2_mboxes[] = { &mbox_iva_info, &mbox_dsp_info, NULL };
07d65d8b
FC
341#endif
342
14476bd9 343#if defined(CONFIG_ARCH_OMAP4)
07d65d8b 344/* OMAP4 */
5f00ec64
S
345static struct omap_mbox2_priv omap2_mbox_1_priv = {
346 .tx_fifo = {
347 .msg = MAILBOX_MESSAGE(0),
348 .fifo_stat = MAILBOX_FIFOSTATUS(0),
349 },
350 .rx_fifo = {
351 .msg = MAILBOX_MESSAGE(1),
352 .msg_stat = MAILBOX_MSGSTATUS(1),
353 },
354 .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
355 .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
356 .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
357 .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
358 .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
340a614a
HD
359};
360
5f00ec64
S
361struct omap_mbox mbox_1_info = {
362 .name = "mailbox-1",
363 .ops = &omap2_mbox_ops,
364 .priv = &omap2_mbox_1_priv,
365};
5f00ec64 366
5f00ec64
S
367static struct omap_mbox2_priv omap2_mbox_2_priv = {
368 .tx_fifo = {
369 .msg = MAILBOX_MESSAGE(3),
370 .fifo_stat = MAILBOX_FIFOSTATUS(3),
371 },
372 .rx_fifo = {
373 .msg = MAILBOX_MESSAGE(2),
374 .msg_stat = MAILBOX_MSGSTATUS(2),
375 },
376 .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
377 .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
378 .notfull_bit = MAILBOX_IRQ_NOTFULL(3),
379 .newmsg_bit = MAILBOX_IRQ_NEWMSG(2),
380 .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
381};
382
383struct omap_mbox mbox_2_info = {
384 .name = "mailbox-2",
385 .ops = &omap2_mbox_ops,
386 .priv = &omap2_mbox_2_priv,
387};
5f00ec64 388
898ee756 389struct omap_mbox *omap4_mboxes[] = { &mbox_1_info, &mbox_2_info, NULL };
14476bd9 390#endif
898ee756 391
da8cfe03 392static int __devinit omap2_mbox_probe(struct platform_device *pdev)
340a614a 393{
898ee756 394 struct resource *mem;
6c20a683 395 int ret;
9c80c8cd 396 struct omap_mbox **list;
340a614a 397
14476bd9
FC
398 if (false)
399 ;
400#if defined(CONFIG_ARCH_OMAP3430)
401 else if (cpu_is_omap3430()) {
898ee756
FC
402 list = omap3_mboxes;
403
404 list[0]->irq = platform_get_irq_byname(pdev, "dsp");
340a614a 405 }
14476bd9 406#endif
898ee756
FC
407#if defined(CONFIG_ARCH_OMAP2420)
408 else if (cpu_is_omap2420()) {
409 list = omap2_mboxes;
340a614a 410
898ee756
FC
411 list[0]->irq = platform_get_irq_byname(pdev, "dsp");
412 list[1]->irq = platform_get_irq_byname(pdev, "iva");
413 }
414#endif
14476bd9 415#if defined(CONFIG_ARCH_OMAP4)
898ee756
FC
416 else if (cpu_is_omap44xx()) {
417 list = omap4_mboxes;
5f00ec64 418
898ee756
FC
419 list[0]->irq = list[1]->irq =
420 platform_get_irq_byname(pdev, "mbox");
340a614a 421 }
14476bd9 422#endif
898ee756
FC
423 else {
424 pr_err("%s: platform not supported\n", __func__);
425 return -ENODEV;
5f00ec64 426 }
6c20a683 427
898ee756
FC
428 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
429 mbox_base = ioremap(mem->start, resource_size(mem));
430 if (!mbox_base)
431 return -ENOMEM;
432
9c80c8cd
FC
433 ret = omap_mbox_register(&pdev->dev, list);
434 if (ret) {
435 iounmap(mbox_base);
436 return ret;
340a614a 437 }
6c20a683 438 return 0;
340a614a
HD
439
440 return ret;
441}
442
da8cfe03 443static int __devexit omap2_mbox_remove(struct platform_device *pdev)
340a614a 444{
9c80c8cd 445 omap_mbox_unregister();
6c20a683 446 iounmap(mbox_base);
340a614a
HD
447 return 0;
448}
449
450static struct platform_driver omap2_mbox_driver = {
451 .probe = omap2_mbox_probe,
da8cfe03 452 .remove = __devexit_p(omap2_mbox_remove),
340a614a 453 .driver = {
5f00ec64 454 .name = DRV_NAME,
340a614a
HD
455 },
456};
457
458static int __init omap2_mbox_init(void)
459{
460 return platform_driver_register(&omap2_mbox_driver);
461}
462
463static void __exit omap2_mbox_exit(void)
464{
465 platform_driver_unregister(&omap2_mbox_driver);
466}
467
468module_init(omap2_mbox_init);
469module_exit(omap2_mbox_exit);
470
733ecc5c 471MODULE_LICENSE("GPL v2");
5f00ec64 472MODULE_DESCRIPTION("omap mailbox: omap2/3/4 architecture specific functions");
f375325a
OBC
473MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
474MODULE_AUTHOR("Paul Mundt");
5f00ec64 475MODULE_ALIAS("platform:"DRV_NAME);