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/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
30 #include <plat/mailbox.h>
32 static struct workqueue_struct *mboxd;
33 static struct omap_mbox *mboxes;
34 static DEFINE_SPINLOCK(mboxes_lock);
37 static int mbox_configured;
38 static DEFINE_MUTEX(mbox_configured_lock);
40 /* Mailbox FIFO handle functions */
41 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
43 return mbox->ops->fifo_read(mbox);
45 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
47 mbox->ops->fifo_write(mbox, msg);
49 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
51 return mbox->ops->fifo_empty(mbox);
53 static inline int mbox_fifo_full(struct omap_mbox *mbox)
55 return mbox->ops->fifo_full(mbox);
58 /* Mailbox IRQ handle functions */
59 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
61 if (mbox->ops->ack_irq)
62 mbox->ops->ack_irq(mbox, irq);
64 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
66 return mbox->ops->is_irq(mbox, irq);
72 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
74 int ret = 0, i = 1000;
76 while (mbox_fifo_full(mbox)) {
77 if (mbox->ops->type == OMAP_MBOX_TYPE2)
83 mbox_fifo_write(mbox, msg);
88 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
92 struct request_queue *q = mbox->txq->queue;
94 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
98 blk_insert_request(q, rq, 0, (void *) msg);
99 tasklet_schedule(&mbox->txq->tasklet);
103 EXPORT_SYMBOL(omap_mbox_msg_send);
105 static void mbox_tx_tasklet(unsigned long tx_data)
109 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
110 struct request_queue *q = mbox->txq->queue;
114 rq = blk_fetch_request(q);
119 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
121 omap_mbox_enable_irq(mbox, IRQ_TX);
122 blk_requeue_request(q, rq);
125 blk_end_request_all(rq, 0);
130 * Message receiver(workqueue)
132 static void mbox_rx_work(struct work_struct *work)
134 struct omap_mbox_queue *mq =
135 container_of(work, struct omap_mbox_queue, work);
136 struct omap_mbox *mbox = mq->queue->queuedata;
137 struct request_queue *q = mbox->rxq->queue;
143 spin_lock_irqsave(q->queue_lock, flags);
144 rq = blk_fetch_request(q);
146 omap_mbox_enable_irq(mbox, IRQ_RX);
149 spin_unlock_irqrestore(q->queue_lock, flags);
153 msg = (mbox_msg_t)rq->special;
154 blk_end_request_all(rq, 0);
155 if (mbox->rxq->callback)
156 mbox->rxq->callback((void *)msg);
161 * Mailbox interrupt handler
163 static void mbox_txq_fn(struct request_queue *q)
167 static void mbox_rxq_fn(struct request_queue *q)
171 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
173 omap_mbox_disable_irq(mbox, IRQ_TX);
174 ack_mbox_irq(mbox, IRQ_TX);
175 tasklet_schedule(&mbox->txq->tasklet);
178 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
182 struct request_queue *q = mbox->rxq->queue;
184 while (!mbox_fifo_empty(mbox)) {
185 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
187 omap_mbox_disable_irq(mbox, IRQ_RX);
192 msg = mbox_fifo_read(mbox);
195 blk_insert_request(q, rq, 0, (void *)msg);
196 if (mbox->ops->type == OMAP_MBOX_TYPE1)
200 /* no more messages in the fifo. clear IRQ source. */
201 ack_mbox_irq(mbox, IRQ_RX);
203 queue_work(mboxd, &mbox->rxq->work);
206 static irqreturn_t mbox_interrupt(int irq, void *p)
208 struct omap_mbox *mbox = p;
210 if (is_mbox_irq(mbox, IRQ_TX))
211 __mbox_tx_interrupt(mbox);
213 if (is_mbox_irq(mbox, IRQ_RX))
214 __mbox_rx_interrupt(mbox);
219 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
220 request_fn_proc *proc,
221 void (*work) (struct work_struct *),
222 void (*tasklet)(unsigned long))
224 struct request_queue *q;
225 struct omap_mbox_queue *mq;
227 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
231 spin_lock_init(&mq->lock);
233 q = blk_init_queue(proc, &mq->lock);
240 INIT_WORK(&mq->work, work);
243 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
250 static void mbox_queue_free(struct omap_mbox_queue *q)
252 blk_cleanup_queue(q->queue);
256 static int omap_mbox_startup(struct omap_mbox *mbox)
259 struct omap_mbox_queue *mq;
261 if (mbox->ops->startup) {
262 mutex_lock(&mbox_configured_lock);
263 if (!mbox_configured)
264 ret = mbox->ops->startup(mbox);
267 mutex_unlock(&mbox_configured_lock);
271 mutex_unlock(&mbox_configured_lock);
274 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
278 "failed to register mailbox interrupt:%d\n", ret);
279 goto fail_request_irq;
282 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
289 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
299 mbox_queue_free(mbox->txq);
301 free_irq(mbox->irq, mbox);
303 if (mbox->ops->shutdown)
304 mbox->ops->shutdown(mbox);
309 static void omap_mbox_fini(struct omap_mbox *mbox)
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);
317 if (mbox->ops->shutdown) {
318 mutex_lock(&mbox_configured_lock);
319 if (mbox_configured > 0)
321 if (!mbox_configured)
322 mbox->ops->shutdown(mbox);
323 mutex_unlock(&mbox_configured_lock);
327 static struct omap_mbox **find_mboxes(const char *name)
329 struct omap_mbox **p;
331 for (p = &mboxes; *p; p = &(*p)->next) {
332 if (strcmp((*p)->name, name) == 0)
339 struct omap_mbox *omap_mbox_get(const char *name)
341 struct omap_mbox *mbox;
344 spin_lock(&mboxes_lock);
345 mbox = *(find_mboxes(name));
347 spin_unlock(&mboxes_lock);
348 return ERR_PTR(-ENOENT);
351 spin_unlock(&mboxes_lock);
353 ret = omap_mbox_startup(mbox);
355 return ERR_PTR(-ENODEV);
359 EXPORT_SYMBOL(omap_mbox_get);
361 void omap_mbox_put(struct omap_mbox *mbox)
363 omap_mbox_fini(mbox);
365 EXPORT_SYMBOL(omap_mbox_put);
367 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
370 struct omap_mbox **tmp;
377 spin_lock(&mboxes_lock);
378 tmp = find_mboxes(mbox->name);
381 spin_unlock(&mboxes_lock);
385 spin_unlock(&mboxes_lock);
392 EXPORT_SYMBOL(omap_mbox_register);
394 int omap_mbox_unregister(struct omap_mbox *mbox)
396 struct omap_mbox **tmp;
398 spin_lock(&mboxes_lock);
404 spin_unlock(&mboxes_lock);
409 spin_unlock(&mboxes_lock);
413 EXPORT_SYMBOL(omap_mbox_unregister);
415 static int __init omap_mbox_init(void)
417 mboxd = create_workqueue("mboxd");
423 module_init(omap_mbox_init);
425 static void __exit omap_mbox_exit(void)
427 destroy_workqueue(mboxd);
429 module_exit(omap_mbox_exit);
431 MODULE_LICENSE("GPL v2");
432 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
433 MODULE_AUTHOR("Toshihiro Kobayashi");
434 MODULE_AUTHOR("Hiroshi DOYU");