4 * Copyright (C) 2006-2008 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>
36 static struct omap_mbox *mboxes;
37 static DEFINE_RWLOCK(mboxes_lock);
39 /* Mailbox Sequence Bit function */
40 void omap_mbox_init_seq(struct omap_mbox *mbox)
44 EXPORT_SYMBOL(omap_mbox_init_seq);
49 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
51 int ret = 0, i = 1000;
53 while (mbox_fifo_full(mbox)) {
54 if (mbox->ops->type == OMAP_MBOX_TYPE2)
61 if (arg && mbox->txq->callback) {
62 ret = mbox->txq->callback(arg);
67 mbox_seq_toggle(mbox, &msg);
68 mbox_fifo_write(mbox, msg);
73 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
76 struct request_queue *q = mbox->txq->queue;
79 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
85 rq->data = (void *)msg;
86 blk_insert_request(q, rq, 0, arg);
88 schedule_work(&mbox->txq->work);
92 EXPORT_SYMBOL(omap_mbox_msg_send);
94 static void mbox_tx_work(struct work_struct *work)
98 struct omap_mbox_queue *mq = container_of(work,
99 struct omap_mbox_queue, work);
100 struct omap_mbox *mbox = mq->queue->queuedata;
101 struct request_queue *q = mbox->txq->queue;
104 spin_lock(q->queue_lock);
105 rq = elv_next_request(q);
106 spin_unlock(q->queue_lock);
111 ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
113 enable_mbox_irq(mbox, IRQ_TX);
117 spin_lock(q->queue_lock);
118 if (__blk_end_request(rq, 0, 0))
120 spin_unlock(q->queue_lock);
125 * Message receiver(workqueue)
127 static void mbox_rx_work(struct work_struct *work)
129 struct omap_mbox_queue *mq =
130 container_of(work, struct omap_mbox_queue, work);
131 struct omap_mbox *mbox = mq->queue->queuedata;
132 struct request_queue *q = mbox->rxq->queue;
137 if (mbox->rxq->callback == NULL) {
138 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
143 spin_lock_irqsave(q->queue_lock, flags);
144 rq = elv_next_request(q);
145 spin_unlock_irqrestore(q->queue_lock, flags);
149 msg = (mbox_msg_t) rq->data;
151 if (blk_end_request(rq, 0, 0))
154 mbox->rxq->callback((void *)msg);
159 * Mailbox interrupt handler
161 static void mbox_txq_fn(struct request_queue * q)
165 static void mbox_rxq_fn(struct request_queue * q)
169 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
171 disable_mbox_irq(mbox, IRQ_TX);
172 ack_mbox_irq(mbox, IRQ_TX);
173 schedule_work(&mbox->txq->work);
176 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
180 struct request_queue *q = mbox->rxq->queue;
182 disable_mbox_irq(mbox, IRQ_RX);
184 while (!mbox_fifo_empty(mbox)) {
185 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
189 msg = mbox_fifo_read(mbox);
190 rq->data = (void *)msg;
192 if (unlikely(mbox_seq_test(mbox, msg))) {
193 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
194 if (mbox->err_notify)
198 blk_insert_request(q, rq, 0, NULL);
199 if (mbox->ops->type == OMAP_MBOX_TYPE1)
203 /* no more messages in the fifo. clear IRQ source. */
204 ack_mbox_irq(mbox, IRQ_RX);
205 enable_mbox_irq(mbox, IRQ_RX);
207 schedule_work(&mbox->rxq->work);
210 static irqreturn_t mbox_interrupt(int irq, void *p)
212 struct omap_mbox *mbox = p;
214 if (is_mbox_irq(mbox, IRQ_TX))
215 __mbox_tx_interrupt(mbox);
217 if (is_mbox_irq(mbox, IRQ_RX))
218 __mbox_rx_interrupt(mbox);
227 omap_mbox_write(struct device *dev, struct device_attribute *attr,
228 const char * buf, size_t count)
231 mbox_msg_t *p = (mbox_msg_t *)buf;
232 struct omap_mbox *mbox = dev_get_drvdata(dev);
234 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
235 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
241 return (size_t)((char *)p - buf);
245 omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
249 mbox_msg_t *p = (mbox_msg_t *) buf;
250 struct omap_mbox *mbox = dev_get_drvdata(dev);
251 struct request_queue *q = mbox->rxq->queue;
254 spin_lock_irqsave(q->queue_lock, flags);
255 rq = elv_next_request(q);
256 spin_unlock_irqrestore(q->queue_lock, flags);
261 *p = (mbox_msg_t) rq->data;
263 if (blk_end_request(rq, 0, 0))
266 if (unlikely(mbox_seq_test(mbox, *p))) {
267 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
273 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
275 return (size_t) ((char *)p - buf);
278 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
280 static ssize_t mbox_show(struct class *class, char *buf)
282 return sprintf(buf, "mbox");
285 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
287 static struct class omap_mbox_class = {
288 .name = "omap-mailbox",
291 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
292 request_fn_proc * proc,
293 void (*work) (struct work_struct *))
295 struct request_queue *q;
296 struct omap_mbox_queue *mq;
298 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
302 spin_lock_init(&mq->lock);
304 q = blk_init_queue(proc, &mq->lock);
310 INIT_WORK(&mq->work, work);
318 static void mbox_queue_free(struct omap_mbox_queue *q)
320 blk_cleanup_queue(q->queue);
324 static int omap_mbox_init(struct omap_mbox *mbox)
327 struct omap_mbox_queue *mq;
329 if (likely(mbox->ops->startup)) {
330 ret = mbox->ops->startup(mbox);
335 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
339 "failed to register mailbox interrupt:%d\n", ret);
340 goto fail_request_irq;
343 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
350 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
360 mbox_queue_free(mbox->txq);
362 free_irq(mbox->irq, mbox);
364 if (unlikely(mbox->ops->shutdown))
365 mbox->ops->shutdown(mbox);
370 static void omap_mbox_fini(struct omap_mbox *mbox)
372 mbox_queue_free(mbox->txq);
373 mbox_queue_free(mbox->rxq);
375 free_irq(mbox->irq, mbox);
377 if (unlikely(mbox->ops->shutdown))
378 mbox->ops->shutdown(mbox);
381 static struct omap_mbox **find_mboxes(const char *name)
383 struct omap_mbox **p;
385 for (p = &mboxes; *p; p = &(*p)->next) {
386 if (strcmp((*p)->name, name) == 0)
393 struct omap_mbox *omap_mbox_get(const char *name)
395 struct omap_mbox *mbox;
398 read_lock(&mboxes_lock);
399 mbox = *(find_mboxes(name));
401 read_unlock(&mboxes_lock);
402 return ERR_PTR(-ENOENT);
405 read_unlock(&mboxes_lock);
407 ret = omap_mbox_init(mbox);
409 return ERR_PTR(-ENODEV);
413 EXPORT_SYMBOL(omap_mbox_get);
415 void omap_mbox_put(struct omap_mbox *mbox)
417 omap_mbox_fini(mbox);
419 EXPORT_SYMBOL(omap_mbox_put);
421 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
424 struct omap_mbox **tmp;
431 mbox->dev = device_create(&omap_mbox_class,
432 parent, 0, mbox, "%s", mbox->name);
433 if (IS_ERR(mbox->dev))
434 return PTR_ERR(mbox->dev);
436 ret = device_create_file(mbox->dev, &dev_attr_mbox);
440 write_lock(&mboxes_lock);
441 tmp = find_mboxes(mbox->name);
444 write_unlock(&mboxes_lock);
448 write_unlock(&mboxes_lock);
453 device_remove_file(mbox->dev, &dev_attr_mbox);
455 device_unregister(mbox->dev);
458 EXPORT_SYMBOL(omap_mbox_register);
460 int omap_mbox_unregister(struct omap_mbox *mbox)
462 struct omap_mbox **tmp;
464 write_lock(&mboxes_lock);
470 write_unlock(&mboxes_lock);
471 device_remove_file(mbox->dev, &dev_attr_mbox);
472 device_unregister(mbox->dev);
477 write_unlock(&mboxes_lock);
481 EXPORT_SYMBOL(omap_mbox_unregister);
483 static int __init omap_mbox_class_init(void)
485 int ret = class_register(&omap_mbox_class);
487 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
492 static void __exit omap_mbox_class_exit(void)
494 class_remove_file(&omap_mbox_class, &class_attr_mbox);
495 class_unregister(&omap_mbox_class);
498 subsys_initcall(omap_mbox_class_init);
499 module_exit(omap_mbox_class_exit);
501 MODULE_LICENSE("GPL v2");
502 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
503 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");