4 * Copyright (C) 2006 Nokia Corporation. All rights reserved.
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7 * Restructured by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
32 #include <asm/delay.h>
33 #include <asm/arch/mailbox.h>
36 static struct omap_mbox *mboxes;
37 static DEFINE_RWLOCK(mboxes_lock);
39 static struct omap_mbox **find_mboxes(const char *name)
43 for (p = &mboxes; *p; p = &(*p)->next) {
44 if (strcmp((*p)->name, name) == 0)
51 struct omap_mbox *omap_mbox_get(const char *name)
53 struct omap_mbox *mbox;
55 read_lock(&mboxes_lock);
56 mbox = *(find_mboxes(name));
57 read_unlock(&mboxes_lock);
61 EXPORT_SYMBOL(omap_mbox_get);
63 /* Mailbox Sequence Bit function */
64 void omap_mbox_init_seq(struct omap_mbox *mbox)
68 EXPORT_SYMBOL(omap_mbox_init_seq);
73 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
77 static DEFINE_MUTEX(msg_send_lock);
79 while (mbox_fifo_full(mbox)) {
80 if (mbox->ops->type == OMAP_MBOX_TYPE2) {
81 enable_mbox_irq(mbox, IRQ_TX);
82 wait_event_interruptible(mbox->tx_waitq,
83 !mbox_fifo_full(mbox));
92 mutex_lock(&msg_send_lock);
94 if (mbox->msg_sender_cb && arg) {
95 ret = mbox->msg_sender_cb(arg);
100 mbox_seq_toggle(mbox, &msg);
101 mbox_fifo_write(mbox, msg);
103 mutex_unlock(&msg_send_lock);
107 EXPORT_SYMBOL(omap_mbox_msg_send);
110 * Message receiver(workqueue)
112 static void mbox_msg_receiver(void *p)
114 struct omap_mbox *mbox = (struct omap_mbox *)p;
115 struct omap_mbq *mbq = mbox->mbq;
118 while (!mbq_empty(mbq)) {
120 enable_mbox_irq(mbox, IRQ_RX);
122 if (unlikely(mbox_seq_test(mbox, msg))) {
124 "mbox: illegal seq bit! ignoring this command. "
129 if (likely(mbox->msg_receive_cb))
130 mbox->msg_receive_cb(msg);
135 * Mailbox interrupt handler
137 static irqreturn_t mbox_interrupt(int irq, void *p, struct pt_regs *regs)
140 struct omap_mbox *mbox = (struct omap_mbox *)p;
142 if (is_mbox_irq(mbox, IRQ_TX)) {
143 disable_mbox_irq(mbox, IRQ_TX);
145 * NOTE: this doesn't seeem to work as explained in the manual.
146 * IRQSTATUS:NOTFULL can't be cleared even we write 1 to that bit.
147 * It is always set when it's not full, regardless of IRQENABLE setting.
149 ack_mbox_irq(mbox, IRQ_TX);
150 wake_up_interruptible_all(&mbox->tx_waitq);
153 if (!is_mbox_irq(mbox, IRQ_RX))
156 while (!mbox_fifo_empty(mbox)) {
157 msg = mbox_fifo_read(mbox);
158 if (mbq_add(mbox->mbq, msg)) { /* mbq full */
159 disable_mbox_irq(mbox, IRQ_RX);
162 if (mbox->ops->type == OMAP_MBOX_TYPE1)
166 /* no more messages in the fifo. clear IRQ source. */
167 ack_mbox_irq(mbox, IRQ_RX);
169 schedule_work(&mbox->msg_receive);
177 static ssize_t mbox_attr_write(struct class_device *dev, const char *buf,
182 struct omap_mbox *mbox = class_get_devdata(dev);
184 msg = (mbox_msg_t) simple_strtoul(buf, NULL, 16);
186 ret = omap_mbox_msg_send(mbox, msg, NULL);
193 static ssize_t mbox_attr_read(struct class_device *dev, char *buf)
195 struct omap_mbox *mbox = class_get_devdata(dev);
197 return sprintf(buf, mbox->name);
200 static CLASS_DEVICE_ATTR(mbox, S_IALLUGO, mbox_attr_read, mbox_attr_write);
202 static ssize_t mbox_show(struct class *class, char *buf)
204 return sprintf(buf, "mbox");
207 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
209 static struct class omap_mbox_class = {
213 static int omap_mbox_init(struct omap_mbox *mbox)
217 if (likely(mbox->ops->startup)) {
218 ret = mbox->ops->startup(mbox);
223 mbox->class_dev.class = &omap_mbox_class;
224 strlcpy(mbox->class_dev.class_id, mbox->name, KOBJ_NAME_LEN);
225 class_set_devdata(&mbox->class_dev, mbox);
227 ret = class_device_register(&mbox->class_dev);
231 class_device_create_file(&mbox->class_dev, &class_device_attr_mbox);
233 ret = request_irq(mbox->irq, mbox_interrupt, SA_INTERRUPT,
237 "failed to register mailbox interrupt:%d\n", ret);
240 enable_mbox_irq(mbox, IRQ_RX);
242 spin_lock_init(&mbox->lock);
243 INIT_WORK(&mbox->msg_receive, mbox_msg_receiver, mbox);
244 init_waitqueue_head(&mbox->tx_waitq);
246 ret = mbq_init(&mbox->mbq);
252 free_irq(mbox->irq, mbox);
253 class_remove_file(&omap_mbox_class, &class_attr_mbox);
254 class_unregister(&omap_mbox_class);
256 if (unlikely(mbox->ops->shutdown))
257 mbox->ops->shutdown(mbox);
262 static void omap_mbox_shutdown(struct omap_mbox *mbox)
264 free_irq(mbox->irq, mbox);
265 class_remove_file(&omap_mbox_class, &class_attr_mbox);
266 class_unregister(&omap_mbox_class);
268 if (unlikely(mbox->ops->shutdown))
269 mbox->ops->shutdown(mbox);
272 int omap_mbox_register(struct omap_mbox *mbox)
275 struct omap_mbox **tmp;
282 ret = omap_mbox_init(mbox);
286 write_lock(&mboxes_lock);
287 tmp = find_mboxes(mbox->name);
292 write_unlock(&mboxes_lock);
296 EXPORT_SYMBOL(omap_mbox_register);
298 int omap_mbox_unregister(struct omap_mbox *mbox)
300 struct omap_mbox **tmp;
302 write_lock(&mboxes_lock);
308 write_unlock(&mboxes_lock);
310 omap_mbox_shutdown(mbox);
316 write_unlock(&mboxes_lock);
320 EXPORT_SYMBOL(omap_mbox_unregister);
322 static int __init omap_mbox_class_init(void)
324 int ret = class_register(&omap_mbox_class);
326 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
331 static void __exit omap_mbox_class_exit(void)
333 class_remove_file(&omap_mbox_class, &class_attr_mbox);
334 class_unregister(&omap_mbox_class);
337 subsys_initcall(omap_mbox_class_init);
338 module_exit(omap_mbox_class_exit);