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_RWLOCK(mboxes_lock);
36 static int mbox_configured;
38 /* Mailbox FIFO handle functions */
39 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
41 return mbox->ops->fifo_read(mbox);
43 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
45 mbox->ops->fifo_write(mbox, msg);
47 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
49 return mbox->ops->fifo_empty(mbox);
51 static inline int mbox_fifo_full(struct omap_mbox *mbox)
53 return mbox->ops->fifo_full(mbox);
56 /* Mailbox IRQ handle functions */
57 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
59 if (mbox->ops->ack_irq)
60 mbox->ops->ack_irq(mbox, irq);
62 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
64 return mbox->ops->is_irq(mbox, irq);
70 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
72 int ret = 0, i = 1000;
74 while (mbox_fifo_full(mbox)) {
75 if (mbox->ops->type == OMAP_MBOX_TYPE2)
81 mbox_fifo_write(mbox, msg);
86 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
90 struct request_queue *q = mbox->txq->queue;
92 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
96 blk_insert_request(q, rq, 0, (void *) msg);
97 tasklet_schedule(&mbox->txq->tasklet);
101 EXPORT_SYMBOL(omap_mbox_msg_send);
103 static void mbox_tx_tasklet(unsigned long tx_data)
107 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
108 struct request_queue *q = mbox->txq->queue;
112 rq = blk_fetch_request(q);
117 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
119 omap_mbox_enable_irq(mbox, IRQ_TX);
120 blk_requeue_request(q, rq);
123 blk_end_request_all(rq, 0);
128 * Message receiver(workqueue)
130 static void mbox_rx_work(struct work_struct *work)
132 struct omap_mbox_queue *mq =
133 container_of(work, struct omap_mbox_queue, work);
134 struct omap_mbox *mbox = mq->queue->queuedata;
135 struct request_queue *q = mbox->rxq->queue;
141 spin_lock_irqsave(q->queue_lock, flags);
142 rq = blk_fetch_request(q);
143 spin_unlock_irqrestore(q->queue_lock, flags);
147 msg = (mbox_msg_t)rq->special;
148 blk_end_request_all(rq, 0);
149 mbox->rxq->callback((void *)msg);
154 * Mailbox interrupt handler
156 static void mbox_txq_fn(struct request_queue *q)
160 static void mbox_rxq_fn(struct request_queue *q)
164 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
166 omap_mbox_disable_irq(mbox, IRQ_TX);
167 ack_mbox_irq(mbox, IRQ_TX);
168 tasklet_schedule(&mbox->txq->tasklet);
171 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
175 struct request_queue *q = mbox->rxq->queue;
177 while (!mbox_fifo_empty(mbox)) {
178 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
182 msg = mbox_fifo_read(mbox);
185 blk_insert_request(q, rq, 0, (void *)msg);
186 if (mbox->ops->type == OMAP_MBOX_TYPE1)
190 /* no more messages in the fifo. clear IRQ source. */
191 ack_mbox_irq(mbox, IRQ_RX);
193 queue_work(mboxd, &mbox->rxq->work);
196 static irqreturn_t mbox_interrupt(int irq, void *p)
198 struct omap_mbox *mbox = p;
200 if (is_mbox_irq(mbox, IRQ_TX))
201 __mbox_tx_interrupt(mbox);
203 if (is_mbox_irq(mbox, IRQ_RX))
204 __mbox_rx_interrupt(mbox);
209 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
210 request_fn_proc *proc,
211 void (*work) (struct work_struct *),
212 void (*tasklet)(unsigned long))
214 struct request_queue *q;
215 struct omap_mbox_queue *mq;
217 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
221 spin_lock_init(&mq->lock);
223 q = blk_init_queue(proc, &mq->lock);
230 INIT_WORK(&mq->work, work);
233 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
240 static void mbox_queue_free(struct omap_mbox_queue *q)
242 blk_cleanup_queue(q->queue);
246 static int omap_mbox_startup(struct omap_mbox *mbox)
249 struct omap_mbox_queue *mq;
251 if (likely(mbox->ops->startup)) {
252 write_lock(&mboxes_lock);
253 if (!mbox_configured)
254 ret = mbox->ops->startup(mbox);
257 write_unlock(&mboxes_lock);
261 write_unlock(&mboxes_lock);
264 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
268 "failed to register mailbox interrupt:%d\n", ret);
269 goto fail_request_irq;
272 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
279 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
289 mbox_queue_free(mbox->txq);
291 free_irq(mbox->irq, mbox);
293 if (unlikely(mbox->ops->shutdown))
294 mbox->ops->shutdown(mbox);
299 static void omap_mbox_fini(struct omap_mbox *mbox)
301 mbox_queue_free(mbox->txq);
302 mbox_queue_free(mbox->rxq);
304 free_irq(mbox->irq, mbox);
306 if (unlikely(mbox->ops->shutdown)) {
307 write_lock(&mboxes_lock);
308 if (mbox_configured > 0)
310 if (!mbox_configured)
311 mbox->ops->shutdown(mbox);
312 write_unlock(&mboxes_lock);
316 static struct omap_mbox **find_mboxes(const char *name)
318 struct omap_mbox **p;
320 for (p = &mboxes; *p; p = &(*p)->next) {
321 if (strcmp((*p)->name, name) == 0)
328 struct omap_mbox *omap_mbox_get(const char *name)
330 struct omap_mbox *mbox;
333 read_lock(&mboxes_lock);
334 mbox = *(find_mboxes(name));
336 read_unlock(&mboxes_lock);
337 return ERR_PTR(-ENOENT);
340 read_unlock(&mboxes_lock);
342 ret = omap_mbox_startup(mbox);
344 return ERR_PTR(-ENODEV);
348 EXPORT_SYMBOL(omap_mbox_get);
350 void omap_mbox_put(struct omap_mbox *mbox)
352 omap_mbox_fini(mbox);
354 EXPORT_SYMBOL(omap_mbox_put);
356 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
359 struct omap_mbox **tmp;
366 write_lock(&mboxes_lock);
367 tmp = find_mboxes(mbox->name);
370 write_unlock(&mboxes_lock);
374 write_unlock(&mboxes_lock);
381 EXPORT_SYMBOL(omap_mbox_register);
383 int omap_mbox_unregister(struct omap_mbox *mbox)
385 struct omap_mbox **tmp;
387 write_lock(&mboxes_lock);
393 write_unlock(&mboxes_lock);
398 write_unlock(&mboxes_lock);
402 EXPORT_SYMBOL(omap_mbox_unregister);
404 static int __init omap_mbox_init(void)
406 mboxd = create_workqueue("mboxd");
412 module_init(omap_mbox_init);
414 static void __exit omap_mbox_exit(void)
416 destroy_workqueue(mboxd);
418 module_exit(omap_mbox_exit);
420 MODULE_LICENSE("GPL v2");
421 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
422 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");