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 omap_mbox *mboxes;
32 static DEFINE_RWLOCK(mboxes_lock);
34 /* Mailbox FIFO handle functions */
35 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
37 return mbox->ops->fifo_read(mbox);
39 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
41 mbox->ops->fifo_write(mbox, msg);
43 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
45 return mbox->ops->fifo_empty(mbox);
47 static inline int mbox_fifo_full(struct omap_mbox *mbox)
49 return mbox->ops->fifo_full(mbox);
52 /* Mailbox IRQ handle functions */
53 static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
55 mbox->ops->enable_irq(mbox, irq);
57 static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
59 mbox->ops->disable_irq(mbox, irq);
61 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
63 if (mbox->ops->ack_irq)
64 mbox->ops->ack_irq(mbox, irq);
66 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
68 return mbox->ops->is_irq(mbox, irq);
74 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
76 int ret = 0, i = 1000;
78 while (mbox_fifo_full(mbox)) {
79 if (mbox->ops->type == OMAP_MBOX_TYPE2)
85 mbox_fifo_write(mbox, msg);
89 struct omap_msg_tx_data {
94 static void omap_msg_tx_end_io(struct request *rq, int error)
97 __blk_put_request(rq->q, rq);
100 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
102 struct omap_msg_tx_data *tx_data;
104 struct request_queue *q = mbox->txq->queue;
106 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
107 if (unlikely(!tx_data))
110 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
118 rq->end_io = omap_msg_tx_end_io;
119 blk_insert_request(q, rq, 0, tx_data);
121 schedule_work(&mbox->txq->work);
124 EXPORT_SYMBOL(omap_mbox_msg_send);
126 static void mbox_tx_work(struct work_struct *work)
130 struct omap_mbox_queue *mq = container_of(work,
131 struct omap_mbox_queue, work);
132 struct omap_mbox *mbox = mq->queue->queuedata;
133 struct request_queue *q = mbox->txq->queue;
136 struct omap_msg_tx_data *tx_data;
138 spin_lock(q->queue_lock);
139 rq = blk_fetch_request(q);
140 spin_unlock(q->queue_lock);
145 tx_data = rq->special;
147 ret = __mbox_msg_send(mbox, tx_data->msg);
149 enable_mbox_irq(mbox, IRQ_TX);
150 spin_lock(q->queue_lock);
151 blk_requeue_request(q, rq);
152 spin_unlock(q->queue_lock);
156 spin_lock(q->queue_lock);
157 __blk_end_request_all(rq, 0);
158 spin_unlock(q->queue_lock);
163 * Message receiver(workqueue)
165 static void mbox_rx_work(struct work_struct *work)
167 struct omap_mbox_queue *mq =
168 container_of(work, struct omap_mbox_queue, work);
169 struct omap_mbox *mbox = mq->queue->queuedata;
170 struct request_queue *q = mbox->rxq->queue;
176 spin_lock_irqsave(q->queue_lock, flags);
177 rq = blk_fetch_request(q);
178 spin_unlock_irqrestore(q->queue_lock, flags);
182 msg = (mbox_msg_t)rq->special;
183 blk_end_request_all(rq, 0);
184 mbox->rxq->callback((void *)msg);
189 * Mailbox interrupt handler
191 static void mbox_txq_fn(struct request_queue *q)
195 static void mbox_rxq_fn(struct request_queue *q)
199 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
201 disable_mbox_irq(mbox, IRQ_TX);
202 ack_mbox_irq(mbox, IRQ_TX);
203 schedule_work(&mbox->txq->work);
206 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
210 struct request_queue *q = mbox->rxq->queue;
212 disable_mbox_irq(mbox, IRQ_RX);
214 while (!mbox_fifo_empty(mbox)) {
215 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
219 msg = mbox_fifo_read(mbox);
222 blk_insert_request(q, rq, 0, (void *)msg);
223 if (mbox->ops->type == OMAP_MBOX_TYPE1)
227 /* no more messages in the fifo. clear IRQ source. */
228 ack_mbox_irq(mbox, IRQ_RX);
229 enable_mbox_irq(mbox, IRQ_RX);
231 schedule_work(&mbox->rxq->work);
234 static irqreturn_t mbox_interrupt(int irq, void *p)
236 struct omap_mbox *mbox = p;
238 if (is_mbox_irq(mbox, IRQ_TX))
239 __mbox_tx_interrupt(mbox);
241 if (is_mbox_irq(mbox, IRQ_RX))
242 __mbox_rx_interrupt(mbox);
247 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
248 request_fn_proc *proc,
249 void (*work) (struct work_struct *))
251 struct request_queue *q;
252 struct omap_mbox_queue *mq;
254 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
258 spin_lock_init(&mq->lock);
260 q = blk_init_queue(proc, &mq->lock);
266 INIT_WORK(&mq->work, work);
274 static void mbox_queue_free(struct omap_mbox_queue *q)
276 blk_cleanup_queue(q->queue);
280 static int omap_mbox_startup(struct omap_mbox *mbox)
283 struct omap_mbox_queue *mq;
285 if (likely(mbox->ops->startup)) {
286 ret = mbox->ops->startup(mbox);
291 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
295 "failed to register mailbox interrupt:%d\n", ret);
296 goto fail_request_irq;
299 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
306 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
316 mbox_queue_free(mbox->txq);
318 free_irq(mbox->irq, mbox);
320 if (unlikely(mbox->ops->shutdown))
321 mbox->ops->shutdown(mbox);
326 static void omap_mbox_fini(struct omap_mbox *mbox)
328 mbox_queue_free(mbox->txq);
329 mbox_queue_free(mbox->rxq);
331 free_irq(mbox->irq, mbox);
333 if (unlikely(mbox->ops->shutdown))
334 mbox->ops->shutdown(mbox);
337 static struct omap_mbox **find_mboxes(const char *name)
339 struct omap_mbox **p;
341 for (p = &mboxes; *p; p = &(*p)->next) {
342 if (strcmp((*p)->name, name) == 0)
349 struct omap_mbox *omap_mbox_get(const char *name)
351 struct omap_mbox *mbox;
354 read_lock(&mboxes_lock);
355 mbox = *(find_mboxes(name));
357 read_unlock(&mboxes_lock);
358 return ERR_PTR(-ENOENT);
361 read_unlock(&mboxes_lock);
363 ret = omap_mbox_startup(mbox);
365 return ERR_PTR(-ENODEV);
369 EXPORT_SYMBOL(omap_mbox_get);
371 void omap_mbox_put(struct omap_mbox *mbox)
373 omap_mbox_fini(mbox);
375 EXPORT_SYMBOL(omap_mbox_put);
377 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
380 struct omap_mbox **tmp;
387 write_lock(&mboxes_lock);
388 tmp = find_mboxes(mbox->name);
391 write_unlock(&mboxes_lock);
395 write_unlock(&mboxes_lock);
402 EXPORT_SYMBOL(omap_mbox_register);
404 int omap_mbox_unregister(struct omap_mbox *mbox)
406 struct omap_mbox **tmp;
408 write_lock(&mboxes_lock);
414 write_unlock(&mboxes_lock);
419 write_unlock(&mboxes_lock);
423 EXPORT_SYMBOL(omap_mbox_unregister);
425 static int __init omap_mbox_init(void)
429 module_init(omap_mbox_init);
431 static void __exit omap_mbox_exit(void)
434 module_exit(omap_mbox_exit);
436 MODULE_LICENSE("GPL v2");
437 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
438 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");