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 static int mbox_configured
;
36 /* Mailbox FIFO handle functions */
37 static inline mbox_msg_t
mbox_fifo_read(struct omap_mbox
*mbox
)
39 return mbox
->ops
->fifo_read(mbox
);
41 static inline void mbox_fifo_write(struct omap_mbox
*mbox
, mbox_msg_t msg
)
43 mbox
->ops
->fifo_write(mbox
, msg
);
45 static inline int mbox_fifo_empty(struct omap_mbox
*mbox
)
47 return mbox
->ops
->fifo_empty(mbox
);
49 static inline int mbox_fifo_full(struct omap_mbox
*mbox
)
51 return mbox
->ops
->fifo_full(mbox
);
54 /* Mailbox IRQ handle functions */
55 static inline void ack_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
57 if (mbox
->ops
->ack_irq
)
58 mbox
->ops
->ack_irq(mbox
, irq
);
60 static inline int is_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
62 return mbox
->ops
->is_irq(mbox
, irq
);
68 static int __mbox_msg_send(struct omap_mbox
*mbox
, mbox_msg_t msg
)
70 int ret
= 0, i
= 1000;
72 while (mbox_fifo_full(mbox
)) {
73 if (mbox
->ops
->type
== OMAP_MBOX_TYPE2
)
79 mbox_fifo_write(mbox
, msg
);
84 int omap_mbox_msg_send(struct omap_mbox
*mbox
, mbox_msg_t msg
)
88 struct request_queue
*q
= mbox
->txq
->queue
;
90 rq
= blk_get_request(q
, WRITE
, GFP_ATOMIC
);
94 blk_insert_request(q
, rq
, 0, (void *) msg
);
95 tasklet_schedule(&mbox
->txq
->tasklet
);
99 EXPORT_SYMBOL(omap_mbox_msg_send
);
101 static void mbox_tx_tasklet(unsigned long tx_data
)
105 struct omap_mbox
*mbox
= (struct omap_mbox
*)tx_data
;
106 struct request_queue
*q
= mbox
->txq
->queue
;
110 rq
= blk_fetch_request(q
);
115 ret
= __mbox_msg_send(mbox
, (mbox_msg_t
)rq
->special
);
117 omap_mbox_enable_irq(mbox
, IRQ_TX
);
118 blk_requeue_request(q
, rq
);
121 blk_end_request_all(rq
, 0);
126 * Message receiver(workqueue)
128 static void mbox_rx_work(struct work_struct
*work
)
130 struct omap_mbox_queue
*mq
=
131 container_of(work
, struct omap_mbox_queue
, work
);
132 struct omap_mbox
*mbox
= mq
->queue
->queuedata
;
133 struct request_queue
*q
= mbox
->rxq
->queue
;
139 spin_lock_irqsave(q
->queue_lock
, flags
);
140 rq
= blk_fetch_request(q
);
141 spin_unlock_irqrestore(q
->queue_lock
, flags
);
145 msg
= (mbox_msg_t
)rq
->special
;
146 blk_end_request_all(rq
, 0);
147 mbox
->rxq
->callback((void *)msg
);
152 * Mailbox interrupt handler
154 static void mbox_txq_fn(struct request_queue
*q
)
158 static void mbox_rxq_fn(struct request_queue
*q
)
162 static void __mbox_tx_interrupt(struct omap_mbox
*mbox
)
164 omap_mbox_disable_irq(mbox
, IRQ_TX
);
165 ack_mbox_irq(mbox
, IRQ_TX
);
166 tasklet_schedule(&mbox
->txq
->tasklet
);
169 static void __mbox_rx_interrupt(struct omap_mbox
*mbox
)
173 struct request_queue
*q
= mbox
->rxq
->queue
;
175 while (!mbox_fifo_empty(mbox
)) {
176 rq
= blk_get_request(q
, WRITE
, GFP_ATOMIC
);
180 msg
= mbox_fifo_read(mbox
);
183 blk_insert_request(q
, rq
, 0, (void *)msg
);
184 if (mbox
->ops
->type
== OMAP_MBOX_TYPE1
)
188 /* no more messages in the fifo. clear IRQ source. */
189 ack_mbox_irq(mbox
, IRQ_RX
);
191 schedule_work(&mbox
->rxq
->work
);
194 static irqreturn_t
mbox_interrupt(int irq
, void *p
)
196 struct omap_mbox
*mbox
= p
;
198 if (is_mbox_irq(mbox
, IRQ_TX
))
199 __mbox_tx_interrupt(mbox
);
201 if (is_mbox_irq(mbox
, IRQ_RX
))
202 __mbox_rx_interrupt(mbox
);
207 static struct omap_mbox_queue
*mbox_queue_alloc(struct omap_mbox
*mbox
,
208 request_fn_proc
*proc
,
209 void (*work
) (struct work_struct
*),
210 void (*tasklet
)(unsigned long))
212 struct request_queue
*q
;
213 struct omap_mbox_queue
*mq
;
215 mq
= kzalloc(sizeof(struct omap_mbox_queue
), GFP_KERNEL
);
219 spin_lock_init(&mq
->lock
);
221 q
= blk_init_queue(proc
, &mq
->lock
);
228 INIT_WORK(&mq
->work
, work
);
231 tasklet_init(&mq
->tasklet
, tasklet
, (unsigned long)mbox
);
238 static void mbox_queue_free(struct omap_mbox_queue
*q
)
240 blk_cleanup_queue(q
->queue
);
244 static int omap_mbox_startup(struct omap_mbox
*mbox
)
247 struct omap_mbox_queue
*mq
;
249 if (likely(mbox
->ops
->startup
)) {
250 write_lock(&mboxes_lock
);
251 if (!mbox_configured
)
252 ret
= mbox
->ops
->startup(mbox
);
255 write_unlock(&mboxes_lock
);
259 write_unlock(&mboxes_lock
);
262 ret
= request_irq(mbox
->irq
, mbox_interrupt
, IRQF_SHARED
,
266 "failed to register mailbox interrupt:%d\n", ret
);
267 goto fail_request_irq
;
270 mq
= mbox_queue_alloc(mbox
, mbox_txq_fn
, NULL
, mbox_tx_tasklet
);
277 mq
= mbox_queue_alloc(mbox
, mbox_rxq_fn
, mbox_rx_work
, NULL
);
287 mbox_queue_free(mbox
->txq
);
289 free_irq(mbox
->irq
, mbox
);
291 if (unlikely(mbox
->ops
->shutdown
))
292 mbox
->ops
->shutdown(mbox
);
297 static void omap_mbox_fini(struct omap_mbox
*mbox
)
299 mbox_queue_free(mbox
->txq
);
300 mbox_queue_free(mbox
->rxq
);
302 free_irq(mbox
->irq
, mbox
);
304 if (unlikely(mbox
->ops
->shutdown
)) {
305 write_lock(&mboxes_lock
);
306 if (mbox_configured
> 0)
308 if (!mbox_configured
)
309 mbox
->ops
->shutdown(mbox
);
310 write_unlock(&mboxes_lock
);
314 static struct omap_mbox
**find_mboxes(const char *name
)
316 struct omap_mbox
**p
;
318 for (p
= &mboxes
; *p
; p
= &(*p
)->next
) {
319 if (strcmp((*p
)->name
, name
) == 0)
326 struct omap_mbox
*omap_mbox_get(const char *name
)
328 struct omap_mbox
*mbox
;
331 read_lock(&mboxes_lock
);
332 mbox
= *(find_mboxes(name
));
334 read_unlock(&mboxes_lock
);
335 return ERR_PTR(-ENOENT
);
338 read_unlock(&mboxes_lock
);
340 ret
= omap_mbox_startup(mbox
);
342 return ERR_PTR(-ENODEV
);
346 EXPORT_SYMBOL(omap_mbox_get
);
348 void omap_mbox_put(struct omap_mbox
*mbox
)
350 omap_mbox_fini(mbox
);
352 EXPORT_SYMBOL(omap_mbox_put
);
354 int omap_mbox_register(struct device
*parent
, struct omap_mbox
*mbox
)
357 struct omap_mbox
**tmp
;
364 write_lock(&mboxes_lock
);
365 tmp
= find_mboxes(mbox
->name
);
368 write_unlock(&mboxes_lock
);
372 write_unlock(&mboxes_lock
);
379 EXPORT_SYMBOL(omap_mbox_register
);
381 int omap_mbox_unregister(struct omap_mbox
*mbox
)
383 struct omap_mbox
**tmp
;
385 write_lock(&mboxes_lock
);
391 write_unlock(&mboxes_lock
);
396 write_unlock(&mboxes_lock
);
400 EXPORT_SYMBOL(omap_mbox_unregister
);
402 static int __init
omap_mbox_init(void)
406 module_init(omap_mbox_init
);
408 static void __exit
omap_mbox_exit(void)
411 module_exit(omap_mbox_exit
);
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
415 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");