omap mailbox: move mailbox.h into mailbox.c
[linux-ginger.git] / arch / arm / plat-omap / mailbox.c
blobfcac60dedc8a5122c5f250f3a87e1757bd420912
1 /*
2 * OMAP mailbox driver
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
20 * 02110-1301 USA
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>
32 #include <linux/io.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
45 #endif
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
50 * message. */
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;
61 /* flip 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)
69 return -1;
70 mbox->seq_rcv = seq;
71 return 0;
73 #else
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)
82 return 0;
84 #endif
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)
126 mbox_seq_init(mbox);
128 EXPORT_SYMBOL(omap_mbox_init_seq);
131 * message sender
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)
139 return -1;
140 if (--i == 0)
141 return -1;
142 udelay(1);
145 if (arg && mbox->txq->callback) {
146 ret = mbox->txq->callback(arg);
147 if (ret)
148 goto out;
151 mbox_seq_toggle(mbox, &msg);
152 mbox_fifo_write(mbox, msg);
153 out:
154 return ret;
157 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
159 struct request *rq;
160 struct request_queue *q = mbox->txq->queue;
161 int ret = 0;
163 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
164 if (unlikely(!rq)) {
165 ret = -ENOMEM;
166 goto fail;
169 rq->data = (void *)msg;
170 blk_insert_request(q, rq, 0, arg);
172 schedule_work(&mbox->txq->work);
173 fail:
174 return ret;
176 EXPORT_SYMBOL(omap_mbox_msg_send);
178 static void mbox_tx_work(struct work_struct *work)
180 int ret;
181 struct request *rq;
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;
187 while (1) {
188 spin_lock(q->queue_lock);
189 rq = elv_next_request(q);
190 spin_unlock(q->queue_lock);
192 if (!rq)
193 break;
195 ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
196 if (ret) {
197 enable_mbox_irq(mbox, IRQ_TX);
198 return;
201 spin_lock(q->queue_lock);
202 if (__blk_end_request(rq, 0, 0))
203 BUG();
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;
217 struct request *rq;
218 mbox_msg_t msg;
219 unsigned long flags;
221 if (mbox->rxq->callback == NULL) {
222 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
223 return;
226 while (1) {
227 spin_lock_irqsave(q->queue_lock, flags);
228 rq = elv_next_request(q);
229 spin_unlock_irqrestore(q->queue_lock, flags);
230 if (!rq)
231 break;
233 msg = (mbox_msg_t) rq->data;
235 if (blk_end_request(rq, 0, 0))
236 BUG();
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)
262 struct request *rq;
263 mbox_msg_t msg;
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);
270 if (unlikely(!rq))
271 goto nomem;
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)
279 mbox->err_notify();
282 blk_insert_request(q, rq, 0, NULL);
283 if (mbox->ops->type == OMAP_MBOX_TYPE1)
284 break;
287 /* no more messages in the fifo. clear IRQ source. */
288 ack_mbox_irq(mbox, IRQ_RX);
289 enable_mbox_irq(mbox, IRQ_RX);
290 nomem:
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);
304 return IRQ_HANDLED;
308 * sysfs files
310 static ssize_t
311 omap_mbox_write(struct device *dev, struct device_attribute *attr,
312 const char * buf, size_t count)
314 int ret;
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);
320 if (ret)
321 return -EAGAIN;
322 p++;
325 return (size_t)((char *)p - buf);
328 static ssize_t
329 omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
331 unsigned long flags;
332 struct request *rq;
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;
337 while (1) {
338 spin_lock_irqsave(q->queue_lock, flags);
339 rq = elv_next_request(q);
340 spin_unlock_irqrestore(q->queue_lock, flags);
342 if (!rq)
343 break;
345 *p = (mbox_msg_t) rq->data;
347 if (blk_end_request(rq, 0, 0))
348 BUG();
350 if (unlikely(mbox_seq_test(mbox, *p))) {
351 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
352 continue;
354 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);
383 if (!mq)
384 return NULL;
386 spin_lock_init(&mq->lock);
388 q = blk_init_queue(proc, &mq->lock);
389 if (!q)
390 goto error;
391 q->queuedata = mbox;
392 mq->queue = q;
394 INIT_WORK(&mq->work, work);
396 return mq;
397 error:
398 kfree(mq);
399 return NULL;
402 static void mbox_queue_free(struct omap_mbox_queue *q)
404 blk_cleanup_queue(q->queue);
405 kfree(q);
408 static int omap_mbox_init(struct omap_mbox *mbox)
410 int ret;
411 struct omap_mbox_queue *mq;
413 if (likely(mbox->ops->startup)) {
414 ret = mbox->ops->startup(mbox);
415 if (unlikely(ret))
416 return ret;
419 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
420 mbox->name, mbox);
421 if (unlikely(ret)) {
422 printk(KERN_ERR
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);
428 if (!mq) {
429 ret = -ENOMEM;
430 goto fail_alloc_txq;
432 mbox->txq = mq;
434 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
435 if (!mq) {
436 ret = -ENOMEM;
437 goto fail_alloc_rxq;
439 mbox->rxq = mq;
441 return 0;
443 fail_alloc_rxq:
444 mbox_queue_free(mbox->txq);
445 fail_alloc_txq:
446 free_irq(mbox->irq, mbox);
447 fail_request_irq:
448 if (unlikely(mbox->ops->shutdown))
449 mbox->ops->shutdown(mbox);
451 return ret;
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)
471 break;
474 return p;
477 struct omap_mbox *omap_mbox_get(const char *name)
479 struct omap_mbox *mbox;
480 int ret;
482 read_lock(&mboxes_lock);
483 mbox = *(find_mboxes(name));
484 if (mbox == NULL) {
485 read_unlock(&mboxes_lock);
486 return ERR_PTR(-ENOENT);
489 read_unlock(&mboxes_lock);
491 ret = omap_mbox_init(mbox);
492 if (ret)
493 return ERR_PTR(-ENODEV);
495 return mbox;
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)
507 int ret = 0;
508 struct omap_mbox **tmp;
510 if (!mbox)
511 return -EINVAL;
512 if (mbox->next)
513 return -EBUSY;
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);
521 if (ret)
522 goto err_sysfs;
524 write_lock(&mboxes_lock);
525 tmp = find_mboxes(mbox->name);
526 if (*tmp) {
527 ret = -EBUSY;
528 write_unlock(&mboxes_lock);
529 goto err_find;
531 *tmp = mbox;
532 write_unlock(&mboxes_lock);
534 return 0;
536 err_find:
537 device_remove_file(mbox->dev, &dev_attr_mbox);
538 err_sysfs:
539 device_unregister(mbox->dev);
540 return ret;
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);
549 tmp = &mboxes;
550 while (*tmp) {
551 if (mbox == *tmp) {
552 *tmp = mbox->next;
553 mbox->next = NULL;
554 write_unlock(&mboxes_lock);
555 device_remove_file(mbox->dev, &dev_attr_mbox);
556 device_unregister(mbox->dev);
557 return 0;
559 tmp = &(*tmp)->next;
561 write_unlock(&mboxes_lock);
563 return -EINVAL;
565 EXPORT_SYMBOL(omap_mbox_unregister);
567 static int __init omap_mbox_class_init(void)
569 int ret = class_register(&omap_mbox_class);
570 if (!ret)
571 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
573 return ret;
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");