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/init.h>
25 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
29 #include <linux/blkdev.h>
30 #include <linux/err.h>
31 #include <linux/delay.h>
33 #include <mach/mailbox.h>
35 static struct omap_mbox
*mboxes
;
36 static DEFINE_RWLOCK(mboxes_lock
);
39 * Mailbox sequence bit API
41 #if defined(CONFIG_ARCH_OMAP1)
42 # define MBOX_USE_SEQ_BIT
43 #elif defined(CONFIG_ARCH_OMAP2)
44 # define MBOX_USE_SEQ_BIT
47 #ifdef MBOX_USE_SEQ_BIT
48 /* seq_rcv should be initialized with any value other than
49 * 0 and 1 << 31, to allow either value for the first
51 static inline void mbox_seq_init(struct omap_mbox
*mbox
)
53 /* any value other than 0 and 1 << 31 */
54 mbox
->seq_rcv
= 0xffffffff;
57 static inline void mbox_seq_toggle(struct omap_mbox
*mbox
, mbox_msg_t
* msg
)
59 /* add seq_snd to msg */
60 *msg
= (*msg
& 0x7fffffff) | mbox
->seq_snd
;
62 mbox
->seq_snd
^= 1 << 31;
65 static inline int mbox_seq_test(struct omap_mbox
*mbox
, mbox_msg_t msg
)
67 mbox_msg_t seq
= msg
& (1 << 31);
68 if (seq
== mbox
->seq_rcv
)
74 static inline void mbox_seq_init(struct omap_mbox
*mbox
)
77 static inline void mbox_seq_toggle(struct omap_mbox
*mbox
, mbox_msg_t
* msg
)
80 static inline int mbox_seq_test(struct omap_mbox
*mbox
, mbox_msg_t msg
)
86 /* Mailbox FIFO handle functions */
87 static inline mbox_msg_t
mbox_fifo_read(struct omap_mbox
*mbox
)
89 return mbox
->ops
->fifo_read(mbox
);
91 static inline void mbox_fifo_write(struct omap_mbox
*mbox
, mbox_msg_t msg
)
93 mbox
->ops
->fifo_write(mbox
, msg
);
95 static inline int mbox_fifo_empty(struct omap_mbox
*mbox
)
97 return mbox
->ops
->fifo_empty(mbox
);
99 static inline int mbox_fifo_full(struct omap_mbox
*mbox
)
101 return mbox
->ops
->fifo_full(mbox
);
104 /* Mailbox IRQ handle functions */
105 static inline void enable_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
107 mbox
->ops
->enable_irq(mbox
, irq
);
109 static inline void disable_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
111 mbox
->ops
->disable_irq(mbox
, irq
);
113 static inline void ack_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
115 if (mbox
->ops
->ack_irq
)
116 mbox
->ops
->ack_irq(mbox
, irq
);
118 static inline int is_mbox_irq(struct omap_mbox
*mbox
, omap_mbox_irq_t irq
)
120 return mbox
->ops
->is_irq(mbox
, irq
);
123 /* Mailbox Sequence Bit function */
124 void omap_mbox_init_seq(struct omap_mbox
*mbox
)
128 EXPORT_SYMBOL(omap_mbox_init_seq
);
133 static int __mbox_msg_send(struct omap_mbox
*mbox
, mbox_msg_t msg
, void *arg
)
135 int ret
= 0, i
= 1000;
137 while (mbox_fifo_full(mbox
)) {
138 if (mbox
->ops
->type
== OMAP_MBOX_TYPE2
)
145 if (arg
&& mbox
->txq
->callback
) {
146 ret
= mbox
->txq
->callback(arg
);
151 mbox_seq_toggle(mbox
, &msg
);
152 mbox_fifo_write(mbox
, msg
);
157 int omap_mbox_msg_send(struct omap_mbox
*mbox
, mbox_msg_t msg
, void* arg
)
160 struct request_queue
*q
= mbox
->txq
->queue
;
163 rq
= blk_get_request(q
, WRITE
, GFP_ATOMIC
);
169 rq
->data
= (void *)msg
;
170 blk_insert_request(q
, rq
, 0, arg
);
172 schedule_work(&mbox
->txq
->work
);
176 EXPORT_SYMBOL(omap_mbox_msg_send
);
178 static void mbox_tx_work(struct work_struct
*work
)
182 struct omap_mbox_queue
*mq
= container_of(work
,
183 struct omap_mbox_queue
, work
);
184 struct omap_mbox
*mbox
= mq
->queue
->queuedata
;
185 struct request_queue
*q
= mbox
->txq
->queue
;
188 spin_lock(q
->queue_lock
);
189 rq
= elv_next_request(q
);
190 spin_unlock(q
->queue_lock
);
195 ret
= __mbox_msg_send(mbox
, (mbox_msg_t
) rq
->data
, rq
->special
);
197 enable_mbox_irq(mbox
, IRQ_TX
);
201 spin_lock(q
->queue_lock
);
202 if (__blk_end_request(rq
, 0, 0))
204 spin_unlock(q
->queue_lock
);
209 * Message receiver(workqueue)
211 static void mbox_rx_work(struct work_struct
*work
)
213 struct omap_mbox_queue
*mq
=
214 container_of(work
, struct omap_mbox_queue
, work
);
215 struct omap_mbox
*mbox
= mq
->queue
->queuedata
;
216 struct request_queue
*q
= mbox
->rxq
->queue
;
221 if (mbox
->rxq
->callback
== NULL
) {
222 sysfs_notify(&mbox
->dev
->kobj
, NULL
, "mbox");
227 spin_lock_irqsave(q
->queue_lock
, flags
);
228 rq
= elv_next_request(q
);
229 spin_unlock_irqrestore(q
->queue_lock
, flags
);
233 msg
= (mbox_msg_t
) rq
->data
;
235 if (blk_end_request(rq
, 0, 0))
238 mbox
->rxq
->callback((void *)msg
);
243 * Mailbox interrupt handler
245 static void mbox_txq_fn(struct request_queue
* q
)
249 static void mbox_rxq_fn(struct request_queue
* q
)
253 static void __mbox_tx_interrupt(struct omap_mbox
*mbox
)
255 disable_mbox_irq(mbox
, IRQ_TX
);
256 ack_mbox_irq(mbox
, IRQ_TX
);
257 schedule_work(&mbox
->txq
->work
);
260 static void __mbox_rx_interrupt(struct omap_mbox
*mbox
)
264 struct request_queue
*q
= mbox
->rxq
->queue
;
266 disable_mbox_irq(mbox
, IRQ_RX
);
268 while (!mbox_fifo_empty(mbox
)) {
269 rq
= blk_get_request(q
, WRITE
, GFP_ATOMIC
);
273 msg
= mbox_fifo_read(mbox
);
274 rq
->data
= (void *)msg
;
276 if (unlikely(mbox_seq_test(mbox
, msg
))) {
277 pr_info("mbox: Illegal seq bit!(%08x)\n", msg
);
278 if (mbox
->err_notify
)
282 blk_insert_request(q
, rq
, 0, NULL
);
283 if (mbox
->ops
->type
== OMAP_MBOX_TYPE1
)
287 /* no more messages in the fifo. clear IRQ source. */
288 ack_mbox_irq(mbox
, IRQ_RX
);
289 enable_mbox_irq(mbox
, IRQ_RX
);
291 schedule_work(&mbox
->rxq
->work
);
294 static irqreturn_t
mbox_interrupt(int irq
, void *p
)
296 struct omap_mbox
*mbox
= p
;
298 if (is_mbox_irq(mbox
, IRQ_TX
))
299 __mbox_tx_interrupt(mbox
);
301 if (is_mbox_irq(mbox
, IRQ_RX
))
302 __mbox_rx_interrupt(mbox
);
311 omap_mbox_write(struct device
*dev
, struct device_attribute
*attr
,
312 const char * buf
, size_t count
)
315 mbox_msg_t
*p
= (mbox_msg_t
*)buf
;
316 struct omap_mbox
*mbox
= dev_get_drvdata(dev
);
318 for (; count
>= sizeof(mbox_msg_t
); count
-= sizeof(mbox_msg_t
)) {
319 ret
= omap_mbox_msg_send(mbox
, be32_to_cpu(*p
), NULL
);
325 return (size_t)((char *)p
- buf
);
329 omap_mbox_read(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
333 mbox_msg_t
*p
= (mbox_msg_t
*) buf
;
334 struct omap_mbox
*mbox
= dev_get_drvdata(dev
);
335 struct request_queue
*q
= mbox
->rxq
->queue
;
338 spin_lock_irqsave(q
->queue_lock
, flags
);
339 rq
= elv_next_request(q
);
340 spin_unlock_irqrestore(q
->queue_lock
, flags
);
345 *p
= (mbox_msg_t
) rq
->data
;
347 if (blk_end_request(rq
, 0, 0))
350 if (unlikely(mbox_seq_test(mbox
, *p
))) {
351 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p
);
357 pr_debug("%02x %02x %02x %02x\n", buf
[0], buf
[1], buf
[2], buf
[3]);
359 return (size_t) ((char *)p
- buf
);
362 static DEVICE_ATTR(mbox
, S_IRUGO
| S_IWUSR
, omap_mbox_read
, omap_mbox_write
);
364 static ssize_t
mbox_show(struct class *class, char *buf
)
366 return sprintf(buf
, "mbox");
369 static CLASS_ATTR(mbox
, S_IRUGO
, mbox_show
, NULL
);
371 static struct class omap_mbox_class
= {
372 .name
= "omap-mailbox",
375 static struct omap_mbox_queue
*mbox_queue_alloc(struct omap_mbox
*mbox
,
376 request_fn_proc
* proc
,
377 void (*work
) (struct work_struct
*))
379 struct request_queue
*q
;
380 struct omap_mbox_queue
*mq
;
382 mq
= kzalloc(sizeof(struct omap_mbox_queue
), GFP_KERNEL
);
386 spin_lock_init(&mq
->lock
);
388 q
= blk_init_queue(proc
, &mq
->lock
);
394 INIT_WORK(&mq
->work
, work
);
402 static void mbox_queue_free(struct omap_mbox_queue
*q
)
404 blk_cleanup_queue(q
->queue
);
408 static int omap_mbox_init(struct omap_mbox
*mbox
)
411 struct omap_mbox_queue
*mq
;
413 if (likely(mbox
->ops
->startup
)) {
414 ret
= mbox
->ops
->startup(mbox
);
419 ret
= request_irq(mbox
->irq
, mbox_interrupt
, IRQF_DISABLED
,
423 "failed to register mailbox interrupt:%d\n", ret
);
424 goto fail_request_irq
;
427 mq
= mbox_queue_alloc(mbox
, mbox_txq_fn
, mbox_tx_work
);
434 mq
= mbox_queue_alloc(mbox
, mbox_rxq_fn
, mbox_rx_work
);
444 mbox_queue_free(mbox
->txq
);
446 free_irq(mbox
->irq
, mbox
);
448 if (unlikely(mbox
->ops
->shutdown
))
449 mbox
->ops
->shutdown(mbox
);
454 static void omap_mbox_fini(struct omap_mbox
*mbox
)
456 mbox_queue_free(mbox
->txq
);
457 mbox_queue_free(mbox
->rxq
);
459 free_irq(mbox
->irq
, mbox
);
461 if (unlikely(mbox
->ops
->shutdown
))
462 mbox
->ops
->shutdown(mbox
);
465 static struct omap_mbox
**find_mboxes(const char *name
)
467 struct omap_mbox
**p
;
469 for (p
= &mboxes
; *p
; p
= &(*p
)->next
) {
470 if (strcmp((*p
)->name
, name
) == 0)
477 struct omap_mbox
*omap_mbox_get(const char *name
)
479 struct omap_mbox
*mbox
;
482 read_lock(&mboxes_lock
);
483 mbox
= *(find_mboxes(name
));
485 read_unlock(&mboxes_lock
);
486 return ERR_PTR(-ENOENT
);
489 read_unlock(&mboxes_lock
);
491 ret
= omap_mbox_init(mbox
);
493 return ERR_PTR(-ENODEV
);
497 EXPORT_SYMBOL(omap_mbox_get
);
499 void omap_mbox_put(struct omap_mbox
*mbox
)
501 omap_mbox_fini(mbox
);
503 EXPORT_SYMBOL(omap_mbox_put
);
505 int omap_mbox_register(struct device
*parent
, struct omap_mbox
*mbox
)
508 struct omap_mbox
**tmp
;
515 mbox
->dev
= device_create(&omap_mbox_class
,
516 parent
, 0, mbox
, "%s", mbox
->name
);
517 if (IS_ERR(mbox
->dev
))
518 return PTR_ERR(mbox
->dev
);
520 ret
= device_create_file(mbox
->dev
, &dev_attr_mbox
);
524 write_lock(&mboxes_lock
);
525 tmp
= find_mboxes(mbox
->name
);
528 write_unlock(&mboxes_lock
);
532 write_unlock(&mboxes_lock
);
537 device_remove_file(mbox
->dev
, &dev_attr_mbox
);
539 device_unregister(mbox
->dev
);
542 EXPORT_SYMBOL(omap_mbox_register
);
544 int omap_mbox_unregister(struct omap_mbox
*mbox
)
546 struct omap_mbox
**tmp
;
548 write_lock(&mboxes_lock
);
554 write_unlock(&mboxes_lock
);
555 device_remove_file(mbox
->dev
, &dev_attr_mbox
);
556 device_unregister(mbox
->dev
);
561 write_unlock(&mboxes_lock
);
565 EXPORT_SYMBOL(omap_mbox_unregister
);
567 static int __init
omap_mbox_class_init(void)
569 int ret
= class_register(&omap_mbox_class
);
571 ret
= class_create_file(&omap_mbox_class
, &class_attr_mbox
);
576 static void __exit
omap_mbox_class_exit(void)
578 class_remove_file(&omap_mbox_class
, &class_attr_mbox
);
579 class_unregister(&omap_mbox_class
);
582 subsys_initcall(omap_mbox_class_init
);
583 module_exit(omap_mbox_class_exit
);
585 MODULE_LICENSE("GPL v2");
586 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
587 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");