blob: d925ef3d7d4841ba648bb0e74cff7dedda962dc7 [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
Hiroshi DOYU733ecc52009-03-23 18:07:23 -07002 * Mailbox reservation modules for OMAP2/3
Hiroshi DOYU340a6142006-12-07 15:43:59 -08003 *
Hiroshi DOYU733ecc52009-03-23 18:07:23 -07004 * Copyright (C) 2006-2009 Nokia Corporation
Hiroshi DOYU340a6142006-12-07 15:43:59 -08005 * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU733ecc52009-03-23 18:07:23 -07006 * and Paul Mundt
Hiroshi DOYU340a6142006-12-07 15:43:59 -08007 *
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>
Russell Kingfced80c2008-09-06 12:10:45 +010017#include <linux/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010018#include <mach/mailbox.h>
19#include <mach/irqs.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080020
Hiroshi DOYU733ecc52009-03-23 18:07:23 -070021#define MAILBOX_REVISION 0x000
22#define MAILBOX_SYSCONFIG 0x010
23#define MAILBOX_SYSSTATUS 0x014
24#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
25#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
26#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
27#define MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
28#define MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
29
30#define MAILBOX_IRQ_NEWMSG(u) (1 << (2 * (u)))
31#define MAILBOX_IRQ_NOTFULL(u) (1 << (2 * (u) + 1))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080032
Hiroshi DOYU6c20a682009-03-23 18:07:23 -070033static void __iomem *mbox_base;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080034
Hiroshi DOYU340a6142006-12-07 15:43:59 -080035struct omap_mbox2_fifo {
36 unsigned long msg;
37 unsigned long fifo_stat;
38 unsigned long msg_stat;
39};
40
41struct omap_mbox2_priv {
42 struct omap_mbox2_fifo tx_fifo;
43 struct omap_mbox2_fifo rx_fifo;
44 unsigned long irqenable;
45 unsigned long irqstatus;
46 u32 newmsg_bit;
47 u32 notfull_bit;
48};
49
50static struct clk *mbox_ick_handle;
51
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +030052static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
53 omap_mbox_type_t irq);
54
Hiroshi DOYU6c20a682009-03-23 18:07:23 -070055static inline unsigned int mbox_read_reg(size_t ofs)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080056{
Hiroshi DOYU6c20a682009-03-23 18:07:23 -070057 return __raw_readl(mbox_base + ofs);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080058}
59
Hiroshi DOYU6c20a682009-03-23 18:07:23 -070060static inline void mbox_write_reg(u32 val, size_t ofs)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080061{
Hiroshi DOYU6c20a682009-03-23 18:07:23 -070062 __raw_writel(val, mbox_base + ofs);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080063}
64
65/* Mailbox H/W preparations */
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +030066static int omap2_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080067{
68 unsigned int l;
69
70 mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
71 if (IS_ERR(mbox_ick_handle)) {
72 printk("Could not get mailboxes_ick\n");
73 return -ENODEV;
74 }
75 clk_enable(mbox_ick_handle);
76
77 /* set smart-idle & autoidle */
78 l = mbox_read_reg(MAILBOX_SYSCONFIG);
79 l |= 0x00000011;
80 mbox_write_reg(l, MAILBOX_SYSCONFIG);
81
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +030082 omap2_mbox_enable_irq(mbox, IRQ_RX);
83
Hiroshi DOYU340a6142006-12-07 15:43:59 -080084 return 0;
85}
86
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +030087static void omap2_mbox_shutdown(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080088{
89 clk_disable(mbox_ick_handle);
90 clk_put(mbox_ick_handle);
91}
92
93/* Mailbox FIFO handle functions */
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +030094static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080095{
96 struct omap_mbox2_fifo *fifo =
97 &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
98 return (mbox_msg_t) mbox_read_reg(fifo->msg);
99}
100
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300101static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800102{
103 struct omap_mbox2_fifo *fifo =
104 &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
105 mbox_write_reg(msg, fifo->msg);
106}
107
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300108static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800109{
110 struct omap_mbox2_fifo *fifo =
111 &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
112 return (mbox_read_reg(fifo->msg_stat) == 0);
113}
114
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300115static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800116{
117 struct omap_mbox2_fifo *fifo =
118 &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
119 return (mbox_read_reg(fifo->fifo_stat));
120}
121
122/* Mailbox IRQ handle functions */
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300123static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800124 omap_mbox_type_t irq)
125{
126 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
127 u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
128
129 l = mbox_read_reg(p->irqenable);
130 l |= bit;
131 mbox_write_reg(l, p->irqenable);
132}
133
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300134static void omap2_mbox_disable_irq(struct omap_mbox *mbox,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800135 omap_mbox_type_t irq)
136{
137 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
138 u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
139
140 l = mbox_read_reg(p->irqenable);
141 l &= ~bit;
142 mbox_write_reg(l, p->irqenable);
143}
144
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300145static void omap2_mbox_ack_irq(struct omap_mbox *mbox,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800146 omap_mbox_type_t irq)
147{
148 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
149 u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
150
151 mbox_write_reg(bit, p->irqstatus);
152}
153
Hiroshi DOYUbfbdcf82007-07-30 14:04:04 +0300154static int omap2_mbox_is_irq(struct omap_mbox *mbox,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800155 omap_mbox_type_t irq)
156{
157 struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
158 u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
159 u32 enable = mbox_read_reg(p->irqenable);
160 u32 status = mbox_read_reg(p->irqstatus);
161
162 return (enable & status & bit);
163}
164
165static struct omap_mbox_ops omap2_mbox_ops = {
166 .type = OMAP_MBOX_TYPE2,
167 .startup = omap2_mbox_startup,
168 .shutdown = omap2_mbox_shutdown,
169 .fifo_read = omap2_mbox_fifo_read,
170 .fifo_write = omap2_mbox_fifo_write,
171 .fifo_empty = omap2_mbox_fifo_empty,
172 .fifo_full = omap2_mbox_fifo_full,
173 .enable_irq = omap2_mbox_enable_irq,
174 .disable_irq = omap2_mbox_disable_irq,
175 .ack_irq = omap2_mbox_ack_irq,
176 .is_irq = omap2_mbox_is_irq,
177};
178
179/*
180 * MAILBOX 0: ARM -> DSP,
181 * MAILBOX 1: ARM <- DSP.
182 * MAILBOX 2: ARM -> IVA,
183 * MAILBOX 3: ARM <- IVA.
184 */
185
186/* FIXME: the following structs should be filled automatically by the user id */
187
188/* DSP */
189static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
190 .tx_fifo = {
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700191 .msg = MAILBOX_MESSAGE(0),
192 .fifo_stat = MAILBOX_FIFOSTATUS(0),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800193 },
194 .rx_fifo = {
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700195 .msg = MAILBOX_MESSAGE(1),
196 .msg_stat = MAILBOX_MSGSTATUS(1),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800197 },
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700198 .irqenable = MAILBOX_IRQENABLE(0),
199 .irqstatus = MAILBOX_IRQSTATUS(0),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800200 .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
201 .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
202};
203
204struct omap_mbox mbox_dsp_info = {
205 .name = "dsp",
206 .ops = &omap2_mbox_ops,
207 .priv = &omap2_mbox_dsp_priv,
208};
209EXPORT_SYMBOL(mbox_dsp_info);
210
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700211#if defined(CONFIG_ARCH_OMAP2420) /* IVA */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800212static struct omap_mbox2_priv omap2_mbox_iva_priv = {
213 .tx_fifo = {
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700214 .msg = MAILBOX_MESSAGE(2),
215 .fifo_stat = MAILBOX_FIFOSTATUS(2),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800216 },
217 .rx_fifo = {
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700218 .msg = MAILBOX_MESSAGE(3),
219 .msg_stat = MAILBOX_MSGSTATUS(3),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800220 },
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700221 .irqenable = MAILBOX_IRQENABLE(3),
222 .irqstatus = MAILBOX_IRQSTATUS(3),
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800223 .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
224 .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
225};
226
227static struct omap_mbox mbox_iva_info = {
228 .name = "iva",
229 .ops = &omap2_mbox_ops,
230 .priv = &omap2_mbox_iva_priv,
231};
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700232#endif
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800233
234static int __init omap2_mbox_probe(struct platform_device *pdev)
235{
236 struct resource *res;
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700237 int ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800238
239 /* MBOX base */
240 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
241 if (unlikely(!res)) {
242 dev_err(&pdev->dev, "invalid mem resource\n");
243 return -ENODEV;
244 }
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700245 mbox_base = ioremap(res->start, res->end - res->start);
246 if (!mbox_base)
247 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800248
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700249 /* DSP or IVA2 IRQ */
250 mbox_dsp_info.irq = platform_get_irq(pdev, 0);
251 if (mbox_dsp_info.irq < 0) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800252 dev_err(&pdev->dev, "invalid irq resource\n");
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700253 ret = -ENODEV;
254 goto err_dsp;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800255 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800256
257 ret = omap_mbox_register(&mbox_dsp_info);
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700258 if (ret)
259 goto err_dsp;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800260
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700261#if defined(CONFIG_ARCH_OMAP2420) /* IVA */
262 if (cpu_is_omap2420()) {
263 /* IVA IRQ */
264 res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
265 if (unlikely(!res)) {
266 dev_err(&pdev->dev, "invalid irq resource\n");
267 ret = -ENODEV;
268 goto err_iva1;
269 }
270 mbox_iva_info.irq = res->start;
271 ret = omap_mbox_register(&mbox_iva_info);
272 if (ret)
273 goto err_iva1;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800274 }
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700275#endif
276 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800277
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700278err_iva1:
279 omap_mbox_unregister(&mbox_dsp_info);
280err_dsp:
281 iounmap(mbox_base);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800282 return ret;
283}
284
285static int omap2_mbox_remove(struct platform_device *pdev)
286{
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700287#if defined(CONFIG_ARCH_OMAP2420)
288 omap_mbox_unregister(&mbox_iva_info);
289#endif
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800290 omap_mbox_unregister(&mbox_dsp_info);
Hiroshi DOYU6c20a682009-03-23 18:07:23 -0700291 iounmap(mbox_base);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800292 return 0;
293}
294
295static struct platform_driver omap2_mbox_driver = {
296 .probe = omap2_mbox_probe,
297 .remove = omap2_mbox_remove,
298 .driver = {
299 .name = "mailbox",
300 },
301};
302
303static int __init omap2_mbox_init(void)
304{
305 return platform_driver_register(&omap2_mbox_driver);
306}
307
308static void __exit omap2_mbox_exit(void)
309{
310 platform_driver_unregister(&omap2_mbox_driver);
311}
312
313module_init(omap2_mbox_init);
314module_exit(omap2_mbox_exit);
315
Hiroshi DOYU733ecc52009-03-23 18:07:23 -0700316MODULE_LICENSE("GPL v2");
317MODULE_DESCRIPTION("omap mailbox: omap2/3 architecture specific functions");
318MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, Paul Mundt");