4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/mutex.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/kfifo.h>
30 #include <linux/err.h>
32 #include <plat/mailbox.h>
34 static struct workqueue_struct *mboxd;
35 static struct omap_mbox **mboxes;
37 static int mbox_configured;
38 static DEFINE_MUTEX(mbox_configured_lock);
40 static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
41 module_param(mbox_kfifo_size, uint, S_IRUGO);
42 MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
44 /* Mailbox FIFO handle functions */
45 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
47 return mbox->ops->fifo_read(mbox);
49 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
51 mbox->ops->fifo_write(mbox, msg);
53 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
55 return mbox->ops->fifo_empty(mbox);
57 static inline int mbox_fifo_full(struct omap_mbox *mbox)
59 return mbox->ops->fifo_full(mbox);
62 /* Mailbox IRQ handle functions */
63 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
65 if (mbox->ops->ack_irq)
66 mbox->ops->ack_irq(mbox, irq);
68 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
70 return mbox->ops->is_irq(mbox, irq);
76 static int __mbox_poll_for_space(struct omap_mbox *mbox)
78 int ret = 0, i = 1000;
80 while (mbox_fifo_full(mbox)) {
81 if (mbox->ops->type == OMAP_MBOX_TYPE2)
90 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
92 struct omap_mbox_queue *mq = mbox->txq;
97 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
102 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
103 WARN_ON(len != sizeof(msg));
105 tasklet_schedule(&mbox->txq->tasklet);
108 spin_unlock(&mq->lock);
111 EXPORT_SYMBOL(omap_mbox_msg_send);
113 static void mbox_tx_tasklet(unsigned long tx_data)
115 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
116 struct omap_mbox_queue *mq = mbox->txq;
120 while (kfifo_len(&mq->fifo)) {
121 if (__mbox_poll_for_space(mbox)) {
122 omap_mbox_enable_irq(mbox, IRQ_TX);
126 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
128 WARN_ON(ret != sizeof(msg));
130 mbox_fifo_write(mbox, msg);
135 * Message receiver(workqueue)
137 static void mbox_rx_work(struct work_struct *work)
139 struct omap_mbox_queue *mq =
140 container_of(work, struct omap_mbox_queue, work);
144 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
145 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
146 WARN_ON(len != sizeof(msg));
149 mq->callback((void *)msg);
150 spin_lock_irq(&mq->lock);
153 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
155 spin_unlock_irq(&mq->lock);
160 * Mailbox interrupt handler
162 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
164 omap_mbox_disable_irq(mbox, IRQ_TX);
165 ack_mbox_irq(mbox, IRQ_TX);
166 tasklet_schedule(&mbox->txq->tasklet);
169 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
171 struct omap_mbox_queue *mq = mbox->rxq;
175 while (!mbox_fifo_empty(mbox)) {
176 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
177 omap_mbox_disable_irq(mbox, IRQ_RX);
182 msg = mbox_fifo_read(mbox);
184 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
185 WARN_ON(len != sizeof(msg));
187 if (mbox->ops->type == OMAP_MBOX_TYPE1)
191 /* no more messages in the fifo. clear IRQ source. */
192 ack_mbox_irq(mbox, IRQ_RX);
194 queue_work(mboxd, &mbox->rxq->work);
197 static irqreturn_t mbox_interrupt(int irq, void *p)
199 struct omap_mbox *mbox = p;
201 if (is_mbox_irq(mbox, IRQ_TX))
202 __mbox_tx_interrupt(mbox);
204 if (is_mbox_irq(mbox, IRQ_RX))
205 __mbox_rx_interrupt(mbox);
210 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
211 void (*work) (struct work_struct *),
212 void (*tasklet)(unsigned long))
214 struct omap_mbox_queue *mq;
216 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
220 spin_lock_init(&mq->lock);
222 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
226 INIT_WORK(&mq->work, work);
229 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
236 static void mbox_queue_free(struct omap_mbox_queue *q)
238 kfifo_free(&q->fifo);
242 static int omap_mbox_startup(struct omap_mbox *mbox)
245 struct omap_mbox_queue *mq;
247 if (mbox->ops->startup) {
248 mutex_lock(&mbox_configured_lock);
249 if (!mbox_configured)
250 ret = mbox->ops->startup(mbox);
253 mutex_unlock(&mbox_configured_lock);
257 mutex_unlock(&mbox_configured_lock);
260 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
264 "failed to register mailbox interrupt:%d\n", ret);
265 goto fail_request_irq;
268 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
275 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
285 mbox_queue_free(mbox->txq);
287 free_irq(mbox->irq, mbox);
289 if (mbox->ops->shutdown)
290 mbox->ops->shutdown(mbox);
295 static void omap_mbox_fini(struct omap_mbox *mbox)
297 free_irq(mbox->irq, mbox);
298 tasklet_kill(&mbox->txq->tasklet);
299 flush_work(&mbox->rxq->work);
300 mbox_queue_free(mbox->txq);
301 mbox_queue_free(mbox->rxq);
303 if (mbox->ops->shutdown) {
304 mutex_lock(&mbox_configured_lock);
305 if (mbox_configured > 0)
307 if (!mbox_configured)
308 mbox->ops->shutdown(mbox);
309 mutex_unlock(&mbox_configured_lock);
313 struct omap_mbox *omap_mbox_get(const char *name)
315 struct omap_mbox *mbox;
319 return ERR_PTR(-EINVAL);
321 for (mbox = *mboxes; mbox; mbox++)
322 if (!strcmp(mbox->name, name))
326 return ERR_PTR(-ENOENT);
328 ret = omap_mbox_startup(mbox);
330 return ERR_PTR(-ENODEV);
334 EXPORT_SYMBOL(omap_mbox_get);
336 void omap_mbox_put(struct omap_mbox *mbox)
338 omap_mbox_fini(mbox);
340 EXPORT_SYMBOL(omap_mbox_put);
342 static struct class omap_mbox_class = { .name = "mbox", };
344 int omap_mbox_register(struct device *parent, struct omap_mbox **list)
353 for (i = 0; mboxes[i]; i++) {
354 struct omap_mbox *mbox = mboxes[i];
355 mbox->dev = device_create(&omap_mbox_class,
356 parent, 0, mbox, "%s", mbox->name);
357 if (IS_ERR(mbox->dev)) {
358 ret = PTR_ERR(mbox->dev);
366 device_unregister(mboxes[i]->dev);
369 EXPORT_SYMBOL(omap_mbox_register);
371 int omap_mbox_unregister(void)
378 for (i = 0; mboxes[i]; i++)
379 device_unregister(mboxes[i]->dev);
383 EXPORT_SYMBOL(omap_mbox_unregister);
385 static int __init omap_mbox_init(void)
389 err = class_register(&omap_mbox_class);
393 mboxd = create_workqueue("mboxd");
397 /* kfifo size sanity check: alignment and minimal size */
398 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
399 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
404 subsys_initcall(omap_mbox_init);
406 static void __exit omap_mbox_exit(void)
408 destroy_workqueue(mboxd);
409 class_unregister(&omap_mbox_class);
411 module_exit(omap_mbox_exit);
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
415 MODULE_AUTHOR("Toshihiro Kobayashi");
416 MODULE_AUTHOR("Hiroshi DOYU");