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>
29 #include <plat/mailbox.h>
31 static struct workqueue_struct *mboxd;
32 static struct omap_mbox *mboxes;
33 static DEFINE_RWLOCK(mboxes_lock);
35 static int mbox_configured;
37 /* Mailbox FIFO handle functions */
38 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
40 return mbox->ops->fifo_read(mbox);
42 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
44 mbox->ops->fifo_write(mbox, msg);
46 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
48 return mbox->ops->fifo_empty(mbox);
50 static inline int mbox_fifo_full(struct omap_mbox *mbox)
52 return mbox->ops->fifo_full(mbox);
55 /* Mailbox IRQ handle functions */
56 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
58 if (mbox->ops->ack_irq)
59 mbox->ops->ack_irq(mbox, irq);
61 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
63 return mbox->ops->is_irq(mbox, irq);
69 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
71 int ret = 0, i = 1000;
73 while (mbox_fifo_full(mbox)) {
74 if (mbox->ops->type == OMAP_MBOX_TYPE2)
80 mbox_fifo_write(mbox, msg);
85 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
89 struct request_queue *q = mbox->txq->queue;
91 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
95 blk_insert_request(q, rq, 0, (void *) msg);
96 tasklet_schedule(&mbox->txq->tasklet);
100 EXPORT_SYMBOL(omap_mbox_msg_send);
102 static void mbox_tx_tasklet(unsigned long tx_data)
106 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
107 struct request_queue *q = mbox->txq->queue;
111 rq = blk_fetch_request(q);
116 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
118 omap_mbox_enable_irq(mbox, IRQ_TX);
119 blk_requeue_request(q, rq);
122 blk_end_request_all(rq, 0);
127 * Message receiver(workqueue)
129 static void mbox_rx_work(struct work_struct *work)
131 struct omap_mbox_queue *mq =
132 container_of(work, struct omap_mbox_queue, work);
133 struct omap_mbox *mbox = mq->queue->queuedata;
134 struct request_queue *q = mbox->rxq->queue;
140 spin_lock_irqsave(q->queue_lock, flags);
141 rq = blk_fetch_request(q);
142 spin_unlock_irqrestore(q->queue_lock, flags);
146 msg = (mbox_msg_t)rq->special;
147 blk_end_request_all(rq, 0);
148 mbox->rxq->callback((void *)msg);
153 * Mailbox interrupt handler
155 static void mbox_txq_fn(struct request_queue *q)
159 static void mbox_rxq_fn(struct request_queue *q)
163 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
165 omap_mbox_disable_irq(mbox, IRQ_TX);
166 ack_mbox_irq(mbox, IRQ_TX);
167 tasklet_schedule(&mbox->txq->tasklet);
170 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
174 struct request_queue *q = mbox->rxq->queue;
176 while (!mbox_fifo_empty(mbox)) {
177 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
181 msg = mbox_fifo_read(mbox);
184 blk_insert_request(q, rq, 0, (void *)msg);
185 if (mbox->ops->type == OMAP_MBOX_TYPE1)
189 /* no more messages in the fifo. clear IRQ source. */
190 ack_mbox_irq(mbox, IRQ_RX);
192 queue_work(mboxd, &mbox->rxq->work);
195 static irqreturn_t mbox_interrupt(int irq, void *p)
197 struct omap_mbox *mbox = p;
199 if (is_mbox_irq(mbox, IRQ_TX))
200 __mbox_tx_interrupt(mbox);
202 if (is_mbox_irq(mbox, IRQ_RX))
203 __mbox_rx_interrupt(mbox);
208 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
209 request_fn_proc *proc,
210 void (*work) (struct work_struct *),
211 void (*tasklet)(unsigned long))
213 struct request_queue *q;
214 struct omap_mbox_queue *mq;
216 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
220 spin_lock_init(&mq->lock);
222 q = blk_init_queue(proc, &mq->lock);
229 INIT_WORK(&mq->work, work);
232 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
239 static void mbox_queue_free(struct omap_mbox_queue *q)
241 blk_cleanup_queue(q->queue);
245 static int omap_mbox_startup(struct omap_mbox *mbox)
248 struct omap_mbox_queue *mq;
250 if (likely(mbox->ops->startup)) {
251 write_lock(&mboxes_lock);
252 if (!mbox_configured)
253 ret = mbox->ops->startup(mbox);
256 write_unlock(&mboxes_lock);
260 write_unlock(&mboxes_lock);
263 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
267 "failed to register mailbox interrupt:%d\n", ret);
268 goto fail_request_irq;
271 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
278 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
288 mbox_queue_free(mbox->txq);
290 free_irq(mbox->irq, mbox);
292 if (unlikely(mbox->ops->shutdown))
293 mbox->ops->shutdown(mbox);
298 static void omap_mbox_fini(struct omap_mbox *mbox)
300 mbox_queue_free(mbox->txq);
301 mbox_queue_free(mbox->rxq);
303 free_irq(mbox->irq, mbox);
305 if (unlikely(mbox->ops->shutdown)) {
306 write_lock(&mboxes_lock);
307 if (mbox_configured > 0)
309 if (!mbox_configured)
310 mbox->ops->shutdown(mbox);
311 write_unlock(&mboxes_lock);
315 static struct omap_mbox **find_mboxes(const char *name)
317 struct omap_mbox **p;
319 for (p = &mboxes; *p; p = &(*p)->next) {
320 if (strcmp((*p)->name, name) == 0)
327 struct omap_mbox *omap_mbox_get(const char *name)
329 struct omap_mbox *mbox;
332 read_lock(&mboxes_lock);
333 mbox = *(find_mboxes(name));
335 read_unlock(&mboxes_lock);
336 return ERR_PTR(-ENOENT);
339 read_unlock(&mboxes_lock);
341 ret = omap_mbox_startup(mbox);
343 return ERR_PTR(-ENODEV);
347 EXPORT_SYMBOL(omap_mbox_get);
349 void omap_mbox_put(struct omap_mbox *mbox)
351 omap_mbox_fini(mbox);
353 EXPORT_SYMBOL(omap_mbox_put);
355 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
358 struct omap_mbox **tmp;
365 write_lock(&mboxes_lock);
366 tmp = find_mboxes(mbox->name);
369 write_unlock(&mboxes_lock);
373 write_unlock(&mboxes_lock);
380 EXPORT_SYMBOL(omap_mbox_register);
382 int omap_mbox_unregister(struct omap_mbox *mbox)
384 struct omap_mbox **tmp;
386 write_lock(&mboxes_lock);
392 write_unlock(&mboxes_lock);
397 write_unlock(&mboxes_lock);
401 EXPORT_SYMBOL(omap_mbox_unregister);
403 static int __init omap_mbox_init(void)
405 mboxd = create_workqueue("mboxd");
411 module_init(omap_mbox_init);
413 static void __exit omap_mbox_exit(void)
415 destroy_workqueue(mboxd);
417 module_exit(omap_mbox_exit);
419 MODULE_LICENSE("GPL v2");
420 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
421 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");