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 <mach/mailbox.h>
31 static int enable_seq_bit;
32 module_param(enable_seq_bit, bool, 0);
33 MODULE_PARM_DESC(enable_seq_bit, "Enable sequence bit checking.");
35 static struct omap_mbox *mboxes;
36 static DEFINE_RWLOCK(mboxes_lock);
39 * Mailbox sequence bit API
42 /* seq_rcv should be initialized with any value other than
43 * 0 and 1 << 31, to allow either value for the first
45 static inline void mbox_seq_init(struct omap_mbox *mbox)
50 /* any value other than 0 and 1 << 31 */
51 mbox->seq_rcv = 0xffffffff;
54 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)
72 seq = msg & (1 << 31);
73 if (seq == mbox->seq_rcv)
79 /* Mailbox FIFO handle functions */
80 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
82 return mbox->ops->fifo_read(mbox);
84 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
86 mbox->ops->fifo_write(mbox, msg);
88 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
90 return mbox->ops->fifo_empty(mbox);
92 static inline int mbox_fifo_full(struct omap_mbox *mbox)
94 return mbox->ops->fifo_full(mbox);
97 /* Mailbox IRQ handle functions */
98 static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
100 mbox->ops->enable_irq(mbox, irq);
102 static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
104 mbox->ops->disable_irq(mbox, irq);
106 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
108 if (mbox->ops->ack_irq)
109 mbox->ops->ack_irq(mbox, irq);
111 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
113 return mbox->ops->is_irq(mbox, irq);
116 /* Mailbox Sequence Bit function */
117 void omap_mbox_init_seq(struct omap_mbox *mbox)
121 EXPORT_SYMBOL(omap_mbox_init_seq);
126 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
128 int ret = 0, i = 1000;
130 while (mbox_fifo_full(mbox)) {
131 if (mbox->ops->type == OMAP_MBOX_TYPE2)
138 if (arg && mbox->txq->callback) {
139 ret = mbox->txq->callback(arg);
144 mbox_seq_toggle(mbox, &msg);
145 mbox_fifo_write(mbox, msg);
150 struct omap_msg_tx_data {
155 static void omap_msg_tx_end_io(struct request *rq, int error)
158 __blk_put_request(rq->q, rq);
161 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
163 struct omap_msg_tx_data *tx_data;
165 struct request_queue *q = mbox->txq->queue;
167 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
168 if (unlikely(!tx_data))
171 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
179 rq->end_io = omap_msg_tx_end_io;
180 blk_insert_request(q, rq, 0, tx_data);
182 schedule_work(&mbox->txq->work);
185 EXPORT_SYMBOL(omap_mbox_msg_send);
187 static void mbox_tx_work(struct work_struct *work)
191 struct omap_mbox_queue *mq = container_of(work,
192 struct omap_mbox_queue, work);
193 struct omap_mbox *mbox = mq->queue->queuedata;
194 struct request_queue *q = mbox->txq->queue;
197 struct omap_msg_tx_data *tx_data;
199 spin_lock(q->queue_lock);
200 rq = elv_next_request(q);
202 blkdev_dequeue_request(rq);
203 spin_unlock(q->queue_lock);
208 tx_data = rq->special;
210 ret = __mbox_msg_send(mbox, tx_data->msg, tx_data->arg);
212 enable_mbox_irq(mbox, IRQ_TX);
213 spin_lock(q->queue_lock);
214 blk_requeue_request(q, rq);
215 spin_unlock(q->queue_lock);
219 spin_lock(q->queue_lock);
220 __blk_end_request_all(rq, 0);
221 spin_unlock(q->queue_lock);
226 * Message receiver(workqueue)
228 static void mbox_rx_work(struct work_struct *work)
230 struct omap_mbox_queue *mq =
231 container_of(work, struct omap_mbox_queue, work);
232 struct omap_mbox *mbox = mq->queue->queuedata;
233 struct request_queue *q = mbox->rxq->queue;
238 if (mbox->rxq->callback == NULL) {
239 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
244 spin_lock_irqsave(q->queue_lock, flags);
245 rq = elv_next_request(q);
247 blkdev_dequeue_request(rq);
248 spin_unlock_irqrestore(q->queue_lock, flags);
252 msg = (mbox_msg_t)rq->special;
253 blk_end_request_all(rq, 0);
254 mbox->rxq->callback((void *)msg);
259 * Mailbox interrupt handler
261 static void mbox_txq_fn(struct request_queue * q)
265 static void mbox_rxq_fn(struct request_queue * q)
269 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
271 disable_mbox_irq(mbox, IRQ_TX);
272 ack_mbox_irq(mbox, IRQ_TX);
273 schedule_work(&mbox->txq->work);
276 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
280 struct request_queue *q = mbox->rxq->queue;
282 disable_mbox_irq(mbox, IRQ_RX);
284 while (!mbox_fifo_empty(mbox)) {
285 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
289 msg = mbox_fifo_read(mbox);
291 if (unlikely(mbox_seq_test(mbox, msg))) {
292 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
293 if (mbox->err_notify)
297 blk_insert_request(q, rq, 0, (void *)msg);
298 if (mbox->ops->type == OMAP_MBOX_TYPE1)
302 /* no more messages in the fifo. clear IRQ source. */
303 ack_mbox_irq(mbox, IRQ_RX);
304 enable_mbox_irq(mbox, IRQ_RX);
306 schedule_work(&mbox->rxq->work);
309 static irqreturn_t mbox_interrupt(int irq, void *p)
311 struct omap_mbox *mbox = p;
313 if (is_mbox_irq(mbox, IRQ_TX))
314 __mbox_tx_interrupt(mbox);
316 if (is_mbox_irq(mbox, IRQ_RX))
317 __mbox_rx_interrupt(mbox);
326 omap_mbox_write(struct device *dev, struct device_attribute *attr,
327 const char * buf, size_t count)
330 mbox_msg_t *p = (mbox_msg_t *)buf;
331 struct omap_mbox *mbox = dev_get_drvdata(dev);
333 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
334 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
340 return (size_t)((char *)p - buf);
344 omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
348 mbox_msg_t *p = (mbox_msg_t *) buf;
349 struct omap_mbox *mbox = dev_get_drvdata(dev);
350 struct request_queue *q = mbox->rxq->queue;
353 spin_lock_irqsave(q->queue_lock, flags);
354 rq = elv_next_request(q);
356 blkdev_dequeue_request(rq);
357 spin_unlock_irqrestore(q->queue_lock, flags);
362 *p = (mbox_msg_t)rq->special;
364 blk_end_request_all(rq, 0);
366 if (unlikely(mbox_seq_test(mbox, *p))) {
367 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
373 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
375 return (size_t) ((char *)p - buf);
378 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
380 static ssize_t mbox_show(struct class *class, char *buf)
382 return sprintf(buf, "mbox");
385 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
387 static struct class omap_mbox_class = {
388 .name = "omap-mailbox",
391 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
392 request_fn_proc * proc,
393 void (*work) (struct work_struct *))
395 struct request_queue *q;
396 struct omap_mbox_queue *mq;
398 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
402 spin_lock_init(&mq->lock);
404 q = blk_init_queue(proc, &mq->lock);
410 INIT_WORK(&mq->work, work);
418 static void mbox_queue_free(struct omap_mbox_queue *q)
420 blk_cleanup_queue(q->queue);
424 static int omap_mbox_init(struct omap_mbox *mbox)
427 struct omap_mbox_queue *mq;
429 if (likely(mbox->ops->startup)) {
430 ret = mbox->ops->startup(mbox);
435 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
439 "failed to register mailbox interrupt:%d\n", ret);
440 goto fail_request_irq;
443 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
450 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
460 mbox_queue_free(mbox->txq);
462 free_irq(mbox->irq, mbox);
464 if (unlikely(mbox->ops->shutdown))
465 mbox->ops->shutdown(mbox);
470 static void omap_mbox_fini(struct omap_mbox *mbox)
472 mbox_queue_free(mbox->txq);
473 mbox_queue_free(mbox->rxq);
475 free_irq(mbox->irq, mbox);
477 if (unlikely(mbox->ops->shutdown))
478 mbox->ops->shutdown(mbox);
481 static struct omap_mbox **find_mboxes(const char *name)
483 struct omap_mbox **p;
485 for (p = &mboxes; *p; p = &(*p)->next) {
486 if (strcmp((*p)->name, name) == 0)
493 struct omap_mbox *omap_mbox_get(const char *name)
495 struct omap_mbox *mbox;
498 read_lock(&mboxes_lock);
499 mbox = *(find_mboxes(name));
501 read_unlock(&mboxes_lock);
502 return ERR_PTR(-ENOENT);
505 read_unlock(&mboxes_lock);
507 ret = omap_mbox_init(mbox);
509 return ERR_PTR(-ENODEV);
513 EXPORT_SYMBOL(omap_mbox_get);
515 void omap_mbox_put(struct omap_mbox *mbox)
517 omap_mbox_fini(mbox);
519 EXPORT_SYMBOL(omap_mbox_put);
521 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
524 struct omap_mbox **tmp;
531 mbox->dev = device_create(&omap_mbox_class,
532 parent, 0, mbox, "%s", mbox->name);
533 if (IS_ERR(mbox->dev))
534 return PTR_ERR(mbox->dev);
536 ret = device_create_file(mbox->dev, &dev_attr_mbox);
540 write_lock(&mboxes_lock);
541 tmp = find_mboxes(mbox->name);
544 write_unlock(&mboxes_lock);
548 write_unlock(&mboxes_lock);
553 device_remove_file(mbox->dev, &dev_attr_mbox);
555 device_unregister(mbox->dev);
558 EXPORT_SYMBOL(omap_mbox_register);
560 int omap_mbox_unregister(struct omap_mbox *mbox)
562 struct omap_mbox **tmp;
564 write_lock(&mboxes_lock);
570 write_unlock(&mboxes_lock);
571 device_remove_file(mbox->dev, &dev_attr_mbox);
572 device_unregister(mbox->dev);
577 write_unlock(&mboxes_lock);
581 EXPORT_SYMBOL(omap_mbox_unregister);
583 static int __init omap_mbox_class_init(void)
585 int ret = class_register(&omap_mbox_class);
587 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
592 static void __exit omap_mbox_class_exit(void)
594 class_remove_file(&omap_mbox_class, &class_attr_mbox);
595 class_unregister(&omap_mbox_class);
598 subsys_initcall(omap_mbox_class_init);
599 module_exit(omap_mbox_class_exit);
601 MODULE_LICENSE("GPL v2");
602 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
603 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");