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>
31 #include <linux/notifier.h>
32 #include <linux/module.h>
34 #include <plat/mailbox.h>
36 static struct omap_mbox **mboxes;
38 static int mbox_configured;
39 static DEFINE_MUTEX(mbox_configured_lock);
41 static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
42 module_param(mbox_kfifo_size, uint, S_IRUGO);
43 MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
45 /* Mailbox FIFO handle functions */
46 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
48 return mbox->ops->fifo_read(mbox);
50 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
52 mbox->ops->fifo_write(mbox, msg);
54 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
56 return mbox->ops->fifo_empty(mbox);
58 static inline int mbox_fifo_full(struct omap_mbox *mbox)
60 return mbox->ops->fifo_full(mbox);
63 /* Mailbox IRQ handle functions */
64 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
66 if (mbox->ops->ack_irq)
67 mbox->ops->ack_irq(mbox, irq);
69 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
71 return mbox->ops->is_irq(mbox, irq);
77 static int __mbox_poll_for_space(struct omap_mbox *mbox)
79 int ret = 0, i = 1000;
81 while (mbox_fifo_full(mbox)) {
82 if (mbox->ops->type == OMAP_MBOX_TYPE2)
91 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
93 struct omap_mbox_queue *mq = mbox->txq;
96 spin_lock_bh(&mq->lock);
98 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
103 if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
104 mbox_fifo_write(mbox, msg);
108 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
109 WARN_ON(len != sizeof(msg));
111 tasklet_schedule(&mbox->txq->tasklet);
114 spin_unlock_bh(&mq->lock);
117 EXPORT_SYMBOL(omap_mbox_msg_send);
119 static void mbox_tx_tasklet(unsigned long tx_data)
121 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
122 struct omap_mbox_queue *mq = mbox->txq;
126 while (kfifo_len(&mq->fifo)) {
127 if (__mbox_poll_for_space(mbox)) {
128 omap_mbox_enable_irq(mbox, IRQ_TX);
132 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
134 WARN_ON(ret != sizeof(msg));
136 mbox_fifo_write(mbox, msg);
141 * Message receiver(workqueue)
143 static void mbox_rx_work(struct work_struct *work)
145 struct omap_mbox_queue *mq =
146 container_of(work, struct omap_mbox_queue, work);
150 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
151 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
152 WARN_ON(len != sizeof(msg));
154 blocking_notifier_call_chain(&mq->mbox->notifier, len,
156 spin_lock_irq(&mq->lock);
159 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
161 spin_unlock_irq(&mq->lock);
166 * Mailbox interrupt handler
168 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
170 omap_mbox_disable_irq(mbox, IRQ_TX);
171 ack_mbox_irq(mbox, IRQ_TX);
172 tasklet_schedule(&mbox->txq->tasklet);
175 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
177 struct omap_mbox_queue *mq = mbox->rxq;
181 while (!mbox_fifo_empty(mbox)) {
182 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
183 omap_mbox_disable_irq(mbox, IRQ_RX);
188 msg = mbox_fifo_read(mbox);
190 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
191 WARN_ON(len != sizeof(msg));
193 if (mbox->ops->type == OMAP_MBOX_TYPE1)
197 /* no more messages in the fifo. clear IRQ source. */
198 ack_mbox_irq(mbox, IRQ_RX);
200 schedule_work(&mbox->rxq->work);
203 static irqreturn_t mbox_interrupt(int irq, void *p)
205 struct omap_mbox *mbox = p;
207 if (is_mbox_irq(mbox, IRQ_TX))
208 __mbox_tx_interrupt(mbox);
210 if (is_mbox_irq(mbox, IRQ_RX))
211 __mbox_rx_interrupt(mbox);
216 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
217 void (*work) (struct work_struct *),
218 void (*tasklet)(unsigned long))
220 struct omap_mbox_queue *mq;
222 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
226 spin_lock_init(&mq->lock);
228 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
232 INIT_WORK(&mq->work, work);
235 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
242 static void mbox_queue_free(struct omap_mbox_queue *q)
244 kfifo_free(&q->fifo);
248 static int omap_mbox_startup(struct omap_mbox *mbox)
251 struct omap_mbox_queue *mq;
253 mutex_lock(&mbox_configured_lock);
254 if (!mbox_configured++) {
255 if (likely(mbox->ops->startup)) {
256 ret = mbox->ops->startup(mbox);
263 if (!mbox->use_count++) {
264 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
267 pr_err("failed to register mailbox interrupt:%d\n",
269 goto fail_request_irq;
271 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
278 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
286 omap_mbox_enable_irq(mbox, IRQ_RX);
288 mutex_unlock(&mbox_configured_lock);
292 mbox_queue_free(mbox->txq);
294 free_irq(mbox->irq, mbox);
296 if (mbox->ops->shutdown)
297 mbox->ops->shutdown(mbox);
301 mutex_unlock(&mbox_configured_lock);
305 static void omap_mbox_fini(struct omap_mbox *mbox)
307 mutex_lock(&mbox_configured_lock);
309 if (!--mbox->use_count) {
310 omap_mbox_disable_irq(mbox, IRQ_RX);
311 free_irq(mbox->irq, mbox);
312 tasklet_kill(&mbox->txq->tasklet);
313 flush_work(&mbox->rxq->work);
314 mbox_queue_free(mbox->txq);
315 mbox_queue_free(mbox->rxq);
318 if (likely(mbox->ops->shutdown)) {
319 if (!--mbox_configured)
320 mbox->ops->shutdown(mbox);
323 mutex_unlock(&mbox_configured_lock);
326 struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
328 struct omap_mbox *_mbox, *mbox = NULL;
332 return ERR_PTR(-EINVAL);
334 for (i = 0; (_mbox = mboxes[i]); i++) {
335 if (!strcmp(_mbox->name, name)) {
342 return ERR_PTR(-ENOENT);
345 blocking_notifier_chain_register(&mbox->notifier, nb);
347 ret = omap_mbox_startup(mbox);
349 blocking_notifier_chain_unregister(&mbox->notifier, nb);
350 return ERR_PTR(-ENODEV);
355 EXPORT_SYMBOL(omap_mbox_get);
357 void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
359 blocking_notifier_chain_unregister(&mbox->notifier, nb);
360 omap_mbox_fini(mbox);
362 EXPORT_SYMBOL(omap_mbox_put);
364 static struct class omap_mbox_class = { .name = "mbox", };
366 int omap_mbox_register(struct device *parent, struct omap_mbox **list)
375 for (i = 0; mboxes[i]; i++) {
376 struct omap_mbox *mbox = mboxes[i];
377 mbox->dev = device_create(&omap_mbox_class,
378 parent, 0, mbox, "%s", mbox->name);
379 if (IS_ERR(mbox->dev)) {
380 ret = PTR_ERR(mbox->dev);
384 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
390 device_unregister(mboxes[i]->dev);
393 EXPORT_SYMBOL(omap_mbox_register);
395 int omap_mbox_unregister(void)
402 for (i = 0; mboxes[i]; i++)
403 device_unregister(mboxes[i]->dev);
407 EXPORT_SYMBOL(omap_mbox_unregister);
409 static int __init omap_mbox_init(void)
413 err = class_register(&omap_mbox_class);
417 /* kfifo size sanity check: alignment and minimal size */
418 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
419 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
424 subsys_initcall(omap_mbox_init);
426 static void __exit omap_mbox_exit(void)
428 class_unregister(&omap_mbox_class);
430 module_exit(omap_mbox_exit);
432 MODULE_LICENSE("GPL v2");
433 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
434 MODULE_AUTHOR("Toshihiro Kobayashi");
435 MODULE_AUTHOR("Hiroshi DOYU");