]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/mailbox.c
2f1adf37fa0d2ee6515ee7b88f3579c7f17f1d0f
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / mailbox.c
1 /*
2  * OMAP mailbox driver
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
7  *
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.
11  *
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.
16  *
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
21  *
22  */
23
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 <asm/io.h>
33 #include <mach/mailbox.h>
34
35 static struct omap_mbox *mboxes;
36 static DEFINE_RWLOCK(mboxes_lock);
37
38 /*
39  * Mailbox sequence bit API
40  */
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
46
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)
52 {
53         /* any value other than 0 and 1 << 31 */
54         mbox->seq_rcv = 0xffffffff;
55 }
56
57 static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
58 {
59         /* add seq_snd to msg */
60         *msg = (*msg & 0x7fffffff) | mbox->seq_snd;
61         /* flip seq_snd */
62         mbox->seq_snd ^= 1 << 31;
63 }
64
65 static inline int mbox_seq_test(struct omap_mbox *mbox, mbox_msg_t msg)
66 {
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;
72 }
73 #else
74 static inline void mbox_seq_init(struct omap_mbox *mbox)
75 {
76 }
77 static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
78 {
79 }
80 static inline int mbox_seq_test(struct omap_mbox *mbox, mbox_msg_t msg)
81 {
82         return 0;
83 }
84 #endif
85
86 /* Mailbox FIFO handle functions */
87 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
88 {
89         return mbox->ops->fifo_read(mbox);
90 }
91 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
92 {
93         mbox->ops->fifo_write(mbox, msg);
94 }
95 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
96 {
97         return mbox->ops->fifo_empty(mbox);
98 }
99 static inline int mbox_fifo_full(struct omap_mbox *mbox)
100 {
101         return mbox->ops->fifo_full(mbox);
102 }
103
104 /* Mailbox IRQ handle functions */
105 static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
106 {
107         mbox->ops->enable_irq(mbox, irq);
108 }
109 static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
110 {
111         mbox->ops->disable_irq(mbox, irq);
112 }
113 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
114 {
115         if (mbox->ops->ack_irq)
116                 mbox->ops->ack_irq(mbox, irq);
117 }
118 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
119 {
120         return mbox->ops->is_irq(mbox, irq);
121 }
122
123 /* Mailbox Sequence Bit function */
124 void omap_mbox_init_seq(struct omap_mbox *mbox)
125 {
126         mbox_seq_init(mbox);
127 }
128 EXPORT_SYMBOL(omap_mbox_init_seq);
129
130 /*
131  * message sender
132  */
133 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
134 {
135         int ret = 0, i = 1000;
136
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);
143         }
144
145         if (arg && mbox->txq->callback) {
146                 ret = mbox->txq->callback(arg);
147                 if (ret)
148                         goto out;
149         }
150
151         mbox_seq_toggle(mbox, &msg);
152         mbox_fifo_write(mbox, msg);
153  out:
154         return ret;
155 }
156
157 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
158 {
159         struct request *rq;
160         struct request_queue *q = mbox->txq->queue;
161         int ret = 0;
162
163         rq = blk_get_request(q, WRITE, GFP_ATOMIC);
164         if (unlikely(!rq)) {
165                 ret = -ENOMEM;
166                 goto fail;
167         }
168
169         rq->data = (void *)msg;
170         blk_insert_request(q, rq, 0, arg);
171
172         schedule_work(&mbox->txq->work);
173  fail:
174         return ret;
175 }
176 EXPORT_SYMBOL(omap_mbox_msg_send);
177
178 static void mbox_tx_work(struct work_struct *work)
179 {
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;
186
187         while (1) {
188                 spin_lock(q->queue_lock);
189                 rq = elv_next_request(q);
190                 spin_unlock(q->queue_lock);
191
192                 if (!rq)
193                         break;
194
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;
199                 }
200
201                 spin_lock(q->queue_lock);
202                 if (__blk_end_request(rq, 0, 0))
203                         BUG();
204                 spin_unlock(q->queue_lock);
205         }
206 }
207
208 /*
209  * Message receiver(workqueue)
210  */
211 static void mbox_rx_work(struct work_struct *work)
212 {
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;
220
221         if (mbox->rxq->callback == NULL) {
222                 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
223                 return;
224         }
225
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;
232
233                 msg = (mbox_msg_t) rq->data;
234
235                 if (blk_end_request(rq, 0, 0))
236                         BUG();
237
238                 mbox->rxq->callback((void *)msg);
239         }
240 }
241
242 /*
243  * Mailbox interrupt handler
244  */
245 static void mbox_txq_fn(struct request_queue * q)
246 {
247 }
248
249 static void mbox_rxq_fn(struct request_queue * q)
250 {
251 }
252
253 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
254 {
255         disable_mbox_irq(mbox, IRQ_TX);
256         ack_mbox_irq(mbox, IRQ_TX);
257         schedule_work(&mbox->txq->work);
258 }
259
260 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
261 {
262         struct request *rq;
263         mbox_msg_t msg;
264         struct request_queue *q = mbox->rxq->queue;
265
266         disable_mbox_irq(mbox, IRQ_RX);
267
268         while (!mbox_fifo_empty(mbox)) {
269                 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
270                 if (unlikely(!rq))
271                         goto nomem;
272
273                 msg = mbox_fifo_read(mbox);
274                 rq->data = (void *)msg;
275
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();
280                 }
281
282                 blk_insert_request(q, rq, 0, NULL);
283                 if (mbox->ops->type == OMAP_MBOX_TYPE1)
284                         break;
285         }
286
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);
292 }
293
294 static irqreturn_t mbox_interrupt(int irq, void *p)
295 {
296         struct omap_mbox *mbox = p;
297
298         if (is_mbox_irq(mbox, IRQ_TX))
299                 __mbox_tx_interrupt(mbox);
300
301         if (is_mbox_irq(mbox, IRQ_RX))
302                 __mbox_rx_interrupt(mbox);
303
304         return IRQ_HANDLED;
305 }
306
307 /*
308  * sysfs files
309  */
310 static ssize_t
311 omap_mbox_write(struct device *dev, struct device_attribute *attr,
312                 const char * buf, size_t count)
313 {
314         int ret;
315         mbox_msg_t *p = (mbox_msg_t *)buf;
316         struct omap_mbox *mbox = dev_get_drvdata(dev);
317
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++;
323         }
324
325         return (size_t)((char *)p - buf);
326 }
327
328 static ssize_t
329 omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
330 {
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;
336
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);
341
342                 if (!rq)
343                         break;
344
345                 *p = (mbox_msg_t) rq->data;
346
347                 if (blk_end_request(rq, 0, 0))
348                         BUG();
349
350                 if (unlikely(mbox_seq_test(mbox, *p))) {
351                         pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
352                         continue;
353                 }
354                 p++;
355         }
356
357         pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
358
359         return (size_t) ((char *)p - buf);
360 }
361
362 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
363
364 static ssize_t mbox_show(struct class *class, char *buf)
365 {
366         return sprintf(buf, "mbox");
367 }
368
369 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
370
371 static struct class omap_mbox_class = {
372         .name = "omap-mailbox",
373 };
374
375 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
376                                         request_fn_proc * proc,
377                                         void (*work) (struct work_struct *))
378 {
379         struct request_queue *q;
380         struct omap_mbox_queue *mq;
381
382         mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
383         if (!mq)
384                 return NULL;
385
386         spin_lock_init(&mq->lock);
387
388         q = blk_init_queue(proc, &mq->lock);
389         if (!q)
390                 goto error;
391         q->queuedata = mbox;
392         mq->queue = q;
393
394         INIT_WORK(&mq->work, work);
395
396         return mq;
397 error:
398         kfree(mq);
399         return NULL;
400 }
401
402 static void mbox_queue_free(struct omap_mbox_queue *q)
403 {
404         blk_cleanup_queue(q->queue);
405         kfree(q);
406 }
407
408 static int omap_mbox_init(struct omap_mbox *mbox)
409 {
410         int ret;
411         struct omap_mbox_queue *mq;
412
413         if (likely(mbox->ops->startup)) {
414                 ret = mbox->ops->startup(mbox);
415                 if (unlikely(ret))
416                         return ret;
417         }
418
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;
425         }
426
427         mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
428         if (!mq) {
429                 ret = -ENOMEM;
430                 goto fail_alloc_txq;
431         }
432         mbox->txq = mq;
433
434         mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
435         if (!mq) {
436                 ret = -ENOMEM;
437                 goto fail_alloc_rxq;
438         }
439         mbox->rxq = mq;
440
441         return 0;
442
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);
450
451         return ret;
452 }
453
454 static void omap_mbox_fini(struct omap_mbox *mbox)
455 {
456         mbox_queue_free(mbox->txq);
457         mbox_queue_free(mbox->rxq);
458
459         free_irq(mbox->irq, mbox);
460
461         if (unlikely(mbox->ops->shutdown))
462                 mbox->ops->shutdown(mbox);
463 }
464
465 static struct omap_mbox **find_mboxes(const char *name)
466 {
467         struct omap_mbox **p;
468
469         for (p = &mboxes; *p; p = &(*p)->next) {
470                 if (strcmp((*p)->name, name) == 0)
471                         break;
472         }
473
474         return p;
475 }
476
477 struct omap_mbox *omap_mbox_get(const char *name)
478 {
479         struct omap_mbox *mbox;
480         int ret;
481
482         read_lock(&mboxes_lock);
483         mbox = *(find_mboxes(name));
484         if (mbox == NULL) {
485                 read_unlock(&mboxes_lock);
486                 return ERR_PTR(-ENOENT);
487         }
488
489         read_unlock(&mboxes_lock);
490
491         ret = omap_mbox_init(mbox);
492         if (ret)
493                 return ERR_PTR(-ENODEV);
494
495         return mbox;
496 }
497 EXPORT_SYMBOL(omap_mbox_get);
498
499 void omap_mbox_put(struct omap_mbox *mbox)
500 {
501         omap_mbox_fini(mbox);
502 }
503 EXPORT_SYMBOL(omap_mbox_put);
504
505 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
506 {
507         int ret = 0;
508         struct omap_mbox **tmp;
509
510         if (!mbox)
511                 return -EINVAL;
512         if (mbox->next)
513                 return -EBUSY;
514
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);
519
520         ret = device_create_file(mbox->dev, &dev_attr_mbox);
521         if (ret)
522                 goto err_sysfs;
523
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;
530         }
531         *tmp = mbox;
532         write_unlock(&mboxes_lock);
533
534         return 0;
535
536 err_find:
537         device_remove_file(mbox->dev, &dev_attr_mbox);
538 err_sysfs:
539         device_unregister(mbox->dev);
540         return ret;
541 }
542 EXPORT_SYMBOL(omap_mbox_register);
543
544 int omap_mbox_unregister(struct omap_mbox *mbox)
545 {
546         struct omap_mbox **tmp;
547
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;
558                 }
559                 tmp = &(*tmp)->next;
560         }
561         write_unlock(&mboxes_lock);
562
563         return -EINVAL;
564 }
565 EXPORT_SYMBOL(omap_mbox_unregister);
566
567 static int __init omap_mbox_class_init(void)
568 {
569         int ret = class_register(&omap_mbox_class);
570         if (!ret)
571                 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
572
573         return ret;
574 }
575
576 static void __exit omap_mbox_class_exit(void)
577 {
578         class_remove_file(&omap_mbox_class, &class_attr_mbox);
579         class_unregister(&omap_mbox_class);
580 }
581
582 subsys_initcall(omap_mbox_class_init);
583 module_exit(omap_mbox_class_exit);
584
585 MODULE_LICENSE("GPL v2");
586 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
587 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");