omap: mailbox: reorganize registering
[pandora-kernel.git] / arch / arm / mach-omap1 / mailbox.c
1 /*
2  * Mailbox reservation modules for DSP
3  *
4  * Copyright (C) 2006-2009 Nokia Corporation
5  * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/resource.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 #include <linux/io.h>
17 #include <plat/mailbox.h>
18 #include <mach/irqs.h>
19
20 #define MAILBOX_ARM2DSP1                0x00
21 #define MAILBOX_ARM2DSP1b               0x04
22 #define MAILBOX_DSP2ARM1                0x08
23 #define MAILBOX_DSP2ARM1b               0x0c
24 #define MAILBOX_DSP2ARM2                0x10
25 #define MAILBOX_DSP2ARM2b               0x14
26 #define MAILBOX_ARM2DSP1_Flag           0x18
27 #define MAILBOX_DSP2ARM1_Flag           0x1c
28 #define MAILBOX_DSP2ARM2_Flag           0x20
29
30 static void __iomem *mbox_base;
31
32 static struct omap_mbox **list;
33
34 struct omap_mbox1_fifo {
35         unsigned long cmd;
36         unsigned long data;
37         unsigned long flag;
38 };
39
40 struct omap_mbox1_priv {
41         struct omap_mbox1_fifo tx_fifo;
42         struct omap_mbox1_fifo rx_fifo;
43 };
44
45 static inline int mbox_read_reg(size_t ofs)
46 {
47         return __raw_readw(mbox_base + ofs);
48 }
49
50 static inline void mbox_write_reg(u32 val, size_t ofs)
51 {
52         __raw_writew(val, mbox_base + ofs);
53 }
54
55 /* msg */
56 static mbox_msg_t omap1_mbox_fifo_read(struct omap_mbox *mbox)
57 {
58         struct omap_mbox1_fifo *fifo =
59                 &((struct omap_mbox1_priv *)mbox->priv)->rx_fifo;
60         mbox_msg_t msg;
61
62         msg = mbox_read_reg(fifo->data);
63         msg |= ((mbox_msg_t) mbox_read_reg(fifo->cmd)) << 16;
64
65         return msg;
66 }
67
68 static void
69 omap1_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
70 {
71         struct omap_mbox1_fifo *fifo =
72                 &((struct omap_mbox1_priv *)mbox->priv)->tx_fifo;
73
74         mbox_write_reg(msg & 0xffff, fifo->data);
75         mbox_write_reg(msg >> 16, fifo->cmd);
76 }
77
78 static int omap1_mbox_fifo_empty(struct omap_mbox *mbox)
79 {
80         return 0;
81 }
82
83 static int omap1_mbox_fifo_full(struct omap_mbox *mbox)
84 {
85         struct omap_mbox1_fifo *fifo =
86                 &((struct omap_mbox1_priv *)mbox->priv)->rx_fifo;
87
88         return mbox_read_reg(fifo->flag);
89 }
90
91 /* irq */
92 static void
93 omap1_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
94 {
95         if (irq == IRQ_RX)
96                 enable_irq(mbox->irq);
97 }
98
99 static void
100 omap1_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
101 {
102         if (irq == IRQ_RX)
103                 disable_irq(mbox->irq);
104 }
105
106 static int
107 omap1_mbox_is_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
108 {
109         if (irq == IRQ_TX)
110                 return 0;
111         return 1;
112 }
113
114 static struct omap_mbox_ops omap1_mbox_ops = {
115         .type           = OMAP_MBOX_TYPE1,
116         .fifo_read      = omap1_mbox_fifo_read,
117         .fifo_write     = omap1_mbox_fifo_write,
118         .fifo_empty     = omap1_mbox_fifo_empty,
119         .fifo_full      = omap1_mbox_fifo_full,
120         .enable_irq     = omap1_mbox_enable_irq,
121         .disable_irq    = omap1_mbox_disable_irq,
122         .is_irq         = omap1_mbox_is_irq,
123 };
124
125 /* FIXME: the following struct should be created automatically by the user id */
126
127 /* DSP */
128 static struct omap_mbox1_priv omap1_mbox_dsp_priv = {
129         .tx_fifo = {
130                 .cmd    = MAILBOX_ARM2DSP1b,
131                 .data   = MAILBOX_ARM2DSP1,
132                 .flag   = MAILBOX_ARM2DSP1_Flag,
133         },
134         .rx_fifo = {
135                 .cmd    = MAILBOX_DSP2ARM1b,
136                 .data   = MAILBOX_DSP2ARM1,
137                 .flag   = MAILBOX_DSP2ARM1_Flag,
138         },
139 };
140
141 struct omap_mbox mbox_dsp_info = {
142         .name   = "dsp",
143         .ops    = &omap1_mbox_ops,
144         .priv   = &omap1_mbox_dsp_priv,
145 };
146
147 struct omap_mbox *omap1_mboxes[] = { &mbox_dsp_info, NULL };
148
149 static int __devinit omap1_mbox_probe(struct platform_device *pdev)
150 {
151         struct resource *mem;
152         int ret;
153         int i;
154
155         list = omap1_mboxes;
156
157         list[0]->irq = platform_get_irq_byname(pdev, "dsp");
158
159         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160         mbox_base = ioremap(mem->start, resource_size(mem));
161         if (!mbox_base)
162                 return -ENOMEM;
163
164         for (i = 0; list[i]; i++) {
165                 ret = omap_mbox_register(&pdev->dev, list[i]);
166                 if (ret)
167                         goto err_out;
168         }
169         return 0;
170
171 err_out:
172         while (i--)
173                 omap_mbox_unregister(list[i]);
174         iounmap(mbox_base);
175         return ret;
176 }
177
178 static int __devexit omap1_mbox_remove(struct platform_device *pdev)
179 {
180         int i;
181
182         for (i = 0; list[i]; i++)
183                 omap_mbox_unregister(list[i]);
184
185         iounmap(mbox_base);
186         return 0;
187 }
188
189 static struct platform_driver omap1_mbox_driver = {
190         .probe  = omap1_mbox_probe,
191         .remove = __devexit_p(omap1_mbox_remove),
192         .driver = {
193                 .name   = "omap1-mailbox",
194         },
195 };
196
197 static int __init omap1_mbox_init(void)
198 {
199         return platform_driver_register(&omap1_mbox_driver);
200 }
201
202 static void __exit omap1_mbox_exit(void)
203 {
204         platform_driver_unregister(&omap1_mbox_driver);
205 }
206
207 module_init(omap1_mbox_init);
208 module_exit(omap1_mbox_exit);
209
210 MODULE_LICENSE("GPL v2");
211 MODULE_DESCRIPTION("omap mailbox: omap1 architecture specific functions");
212 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
213 MODULE_ALIAS("platform:omap1-mailbox");