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/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/mutex.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/kfifo.h>
30 #include <linux/err.h>
31 #include <linux/notifier.h>
32 #include <linux/module.h>
34 #include "omap-mbox.h"
36 static struct omap_mbox
**mboxes
;
38 static int mbox_configured
;
39 static DEFINE_MUTEX(mbox_configured_lock
);
41 static unsigned int mbox_kfifo_size
= CONFIG_OMAP_MBOX_KFIFO_SIZE
;
42 module_param(mbox_kfifo_size
, uint
, S_IRUGO
);
43 MODULE_PARM_DESC(mbox_kfifo_size
, "Size of omap's mailbox kfifo (bytes)");
45 /* Mailbox FIFO handle functions */
46 static inline mbox_msg_t
mbox_fifo_read(struct omap_mbox
*mbox
)
48 return mbox
->ops
->fifo_read(mbox
);
50 static inline void mbox_fifo_write(struct omap_mbox
*mbox
, mbox_msg_t msg
)
52 mbox
->ops
->fifo_write(mbox
, msg
);
54 static inline int mbox_fifo_empty(struct omap_mbox
*mbox
)
56 return mbox
->ops
->fifo_empty(mbox
);
58 static inline int mbox_fifo_full(struct omap_mbox
*mbox
)
60 return mbox
->ops
->fifo_full(mbox
);
63 /* Mailbox IRQ handle functions */
64 static inline void ack_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
66 if (mbox
->ops
->ack_irq
)
67 mbox
->ops
->ack_irq(mbox
, irq
);
69 static inline int is_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
71 return mbox
->ops
->is_irq(mbox
, irq
);
77 static int __mbox_poll_for_space(struct omap_mbox
*mbox
)
79 int ret
= 0, i
= 1000;
81 while (mbox_fifo_full(mbox
)) {
82 if (mbox
->ops
->type
== OMAP_MBOX_TYPE2
)
91 int omap_mbox_msg_send(struct omap_mbox
*mbox
, mbox_msg_t msg
)
93 struct omap_mbox_queue
*mq
= mbox
->txq
;
96 spin_lock_bh(&mq
->lock
);
98 if (kfifo_avail(&mq
->fifo
) < sizeof(msg
)) {
103 if (kfifo_is_empty(&mq
->fifo
) && !__mbox_poll_for_space(mbox
)) {
104 mbox_fifo_write(mbox
, msg
);
108 len
= kfifo_in(&mq
->fifo
, (unsigned char *)&msg
, sizeof(msg
));
109 WARN_ON(len
!= sizeof(msg
));
111 tasklet_schedule(&mbox
->txq
->tasklet
);
114 spin_unlock_bh(&mq
->lock
);
117 EXPORT_SYMBOL(omap_mbox_msg_send
);
119 void omap_mbox_save_ctx(struct omap_mbox
*mbox
)
121 if (!mbox
->ops
->save_ctx
) {
122 dev_err(mbox
->dev
, "%s:\tno save\n", __func__
);
126 mbox
->ops
->save_ctx(mbox
);
128 EXPORT_SYMBOL(omap_mbox_save_ctx
);
130 void omap_mbox_restore_ctx(struct omap_mbox
*mbox
)
132 if (!mbox
->ops
->restore_ctx
) {
133 dev_err(mbox
->dev
, "%s:\tno restore\n", __func__
);
137 mbox
->ops
->restore_ctx(mbox
);
139 EXPORT_SYMBOL(omap_mbox_restore_ctx
);
141 void omap_mbox_enable_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
143 mbox
->ops
->enable_irq(mbox
, irq
);
145 EXPORT_SYMBOL(omap_mbox_enable_irq
);
147 void omap_mbox_disable_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
149 mbox
->ops
->disable_irq(mbox
, irq
);
151 EXPORT_SYMBOL(omap_mbox_disable_irq
);
153 static void mbox_tx_tasklet(unsigned long tx_data
)
155 struct omap_mbox
*mbox
= (struct omap_mbox
*)tx_data
;
156 struct omap_mbox_queue
*mq
= mbox
->txq
;
160 while (kfifo_len(&mq
->fifo
)) {
161 if (__mbox_poll_for_space(mbox
)) {
162 omap_mbox_enable_irq(mbox
, IRQ_TX
);
166 ret
= kfifo_out(&mq
->fifo
, (unsigned char *)&msg
,
168 WARN_ON(ret
!= sizeof(msg
));
170 mbox_fifo_write(mbox
, msg
);
175 * Message receiver(workqueue)
177 static void mbox_rx_work(struct work_struct
*work
)
179 struct omap_mbox_queue
*mq
=
180 container_of(work
, struct omap_mbox_queue
, work
);
184 while (kfifo_len(&mq
->fifo
) >= sizeof(msg
)) {
185 len
= kfifo_out(&mq
->fifo
, (unsigned char *)&msg
, sizeof(msg
));
186 WARN_ON(len
!= sizeof(msg
));
188 blocking_notifier_call_chain(&mq
->mbox
->notifier
, len
,
190 spin_lock_irq(&mq
->lock
);
193 omap_mbox_enable_irq(mq
->mbox
, IRQ_RX
);
195 spin_unlock_irq(&mq
->lock
);
200 * Mailbox interrupt handler
202 static void __mbox_tx_interrupt(struct omap_mbox
*mbox
)
204 omap_mbox_disable_irq(mbox
, IRQ_TX
);
205 ack_mbox_irq(mbox
, IRQ_TX
);
206 tasklet_schedule(&mbox
->txq
->tasklet
);
209 static void __mbox_rx_interrupt(struct omap_mbox
*mbox
)
211 struct omap_mbox_queue
*mq
= mbox
->rxq
;
215 while (!mbox_fifo_empty(mbox
)) {
216 if (unlikely(kfifo_avail(&mq
->fifo
) < sizeof(msg
))) {
217 omap_mbox_disable_irq(mbox
, IRQ_RX
);
222 msg
= mbox_fifo_read(mbox
);
224 len
= kfifo_in(&mq
->fifo
, (unsigned char *)&msg
, sizeof(msg
));
225 WARN_ON(len
!= sizeof(msg
));
227 if (mbox
->ops
->type
== OMAP_MBOX_TYPE1
)
231 /* no more messages in the fifo. clear IRQ source. */
232 ack_mbox_irq(mbox
, IRQ_RX
);
234 schedule_work(&mbox
->rxq
->work
);
237 static irqreturn_t
mbox_interrupt(int irq
, void *p
)
239 struct omap_mbox
*mbox
= p
;
241 if (is_mbox_irq(mbox
, IRQ_TX
))
242 __mbox_tx_interrupt(mbox
);
244 if (is_mbox_irq(mbox
, IRQ_RX
))
245 __mbox_rx_interrupt(mbox
);
250 static struct omap_mbox_queue
*mbox_queue_alloc(struct omap_mbox
*mbox
,
251 void (*work
) (struct work_struct
*),
252 void (*tasklet
)(unsigned long))
254 struct omap_mbox_queue
*mq
;
256 mq
= kzalloc(sizeof(struct omap_mbox_queue
), GFP_KERNEL
);
260 spin_lock_init(&mq
->lock
);
262 if (kfifo_alloc(&mq
->fifo
, mbox_kfifo_size
, GFP_KERNEL
))
266 INIT_WORK(&mq
->work
, work
);
269 tasklet_init(&mq
->tasklet
, tasklet
, (unsigned long)mbox
);
276 static void mbox_queue_free(struct omap_mbox_queue
*q
)
278 kfifo_free(&q
->fifo
);
282 static int omap_mbox_startup(struct omap_mbox
*mbox
)
285 struct omap_mbox_queue
*mq
;
287 mutex_lock(&mbox_configured_lock
);
288 if (!mbox_configured
++) {
289 if (likely(mbox
->ops
->startup
)) {
290 ret
= mbox
->ops
->startup(mbox
);
297 if (!mbox
->use_count
++) {
298 mq
= mbox_queue_alloc(mbox
, NULL
, mbox_tx_tasklet
);
305 mq
= mbox_queue_alloc(mbox
, mbox_rx_work
, NULL
);
312 ret
= request_irq(mbox
->irq
, mbox_interrupt
, IRQF_SHARED
,
315 pr_err("failed to register mailbox interrupt:%d\n",
317 goto fail_request_irq
;
320 omap_mbox_enable_irq(mbox
, IRQ_RX
);
322 mutex_unlock(&mbox_configured_lock
);
326 mbox_queue_free(mbox
->rxq
);
328 mbox_queue_free(mbox
->txq
);
330 if (mbox
->ops
->shutdown
)
331 mbox
->ops
->shutdown(mbox
);
335 mutex_unlock(&mbox_configured_lock
);
339 static void omap_mbox_fini(struct omap_mbox
*mbox
)
341 mutex_lock(&mbox_configured_lock
);
343 if (!--mbox
->use_count
) {
344 omap_mbox_disable_irq(mbox
, IRQ_RX
);
345 free_irq(mbox
->irq
, mbox
);
346 tasklet_kill(&mbox
->txq
->tasklet
);
347 flush_work(&mbox
->rxq
->work
);
348 mbox_queue_free(mbox
->txq
);
349 mbox_queue_free(mbox
->rxq
);
352 if (likely(mbox
->ops
->shutdown
)) {
353 if (!--mbox_configured
)
354 mbox
->ops
->shutdown(mbox
);
357 mutex_unlock(&mbox_configured_lock
);
360 struct omap_mbox
*omap_mbox_get(const char *name
, struct notifier_block
*nb
)
362 struct omap_mbox
*_mbox
, *mbox
= NULL
;
366 return ERR_PTR(-EINVAL
);
368 for (i
= 0; (_mbox
= mboxes
[i
]); i
++) {
369 if (!strcmp(_mbox
->name
, name
)) {
376 return ERR_PTR(-ENOENT
);
379 blocking_notifier_chain_register(&mbox
->notifier
, nb
);
381 ret
= omap_mbox_startup(mbox
);
383 blocking_notifier_chain_unregister(&mbox
->notifier
, nb
);
384 return ERR_PTR(-ENODEV
);
389 EXPORT_SYMBOL(omap_mbox_get
);
391 void omap_mbox_put(struct omap_mbox
*mbox
, struct notifier_block
*nb
)
393 blocking_notifier_chain_unregister(&mbox
->notifier
, nb
);
394 omap_mbox_fini(mbox
);
396 EXPORT_SYMBOL(omap_mbox_put
);
398 static struct class omap_mbox_class
= { .name
= "mbox", };
400 int omap_mbox_register(struct device
*parent
, struct omap_mbox
**list
)
409 for (i
= 0; mboxes
[i
]; i
++) {
410 struct omap_mbox
*mbox
= mboxes
[i
];
411 mbox
->dev
= device_create(&omap_mbox_class
,
412 parent
, 0, mbox
, "%s", mbox
->name
);
413 if (IS_ERR(mbox
->dev
)) {
414 ret
= PTR_ERR(mbox
->dev
);
418 BLOCKING_INIT_NOTIFIER_HEAD(&mbox
->notifier
);
424 device_unregister(mboxes
[i
]->dev
);
427 EXPORT_SYMBOL(omap_mbox_register
);
429 int omap_mbox_unregister(void)
436 for (i
= 0; mboxes
[i
]; i
++)
437 device_unregister(mboxes
[i
]->dev
);
441 EXPORT_SYMBOL(omap_mbox_unregister
);
443 static int __init
omap_mbox_init(void)
447 err
= class_register(&omap_mbox_class
);
451 /* kfifo size sanity check: alignment and minimal size */
452 mbox_kfifo_size
= ALIGN(mbox_kfifo_size
, sizeof(mbox_msg_t
));
453 mbox_kfifo_size
= max_t(unsigned int, mbox_kfifo_size
,
458 subsys_initcall(omap_mbox_init
);
460 static void __exit
omap_mbox_exit(void)
462 class_unregister(&omap_mbox_class
);
464 module_exit(omap_mbox_exit
);
466 MODULE_LICENSE("GPL v2");
467 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
468 MODULE_AUTHOR("Toshihiro Kobayashi");
469 MODULE_AUTHOR("Hiroshi DOYU");