blob: fcac60dedc8a5122c5f250f3a87e1757bd420912 [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
2 * OMAP mailbox driver
3 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07004 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
Hiroshi DOYU340a6142006-12-07 15:43:59 -08005 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07006 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -08007 *
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>
Russell Kingfced80c2008-09-06 12:10:45 +010032#include <linux/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/mailbox.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080034
35static struct omap_mbox *mboxes;
36static DEFINE_RWLOCK(mboxes_lock);
37
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070038/*
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. */
51static 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
57static 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
65static 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
74static inline void mbox_seq_init(struct omap_mbox *mbox)
75{
76}
77static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
78{
79}
80static 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 */
87static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
88{
89 return mbox->ops->fifo_read(mbox);
90}
91static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
92{
93 mbox->ops->fifo_write(mbox, msg);
94}
95static inline int mbox_fifo_empty(struct omap_mbox *mbox)
96{
97 return mbox->ops->fifo_empty(mbox);
98}
99static inline int mbox_fifo_full(struct omap_mbox *mbox)
100{
101 return mbox->ops->fifo_full(mbox);
102}
103
104/* Mailbox IRQ handle functions */
105static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
106{
107 mbox->ops->enable_irq(mbox, irq);
108}
109static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
110{
111 mbox->ops->disable_irq(mbox, irq);
112}
113static 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}
118static 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
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800123/* Mailbox Sequence Bit function */
124void omap_mbox_init_seq(struct omap_mbox *mbox)
125{
126 mbox_seq_init(mbox);
127}
128EXPORT_SYMBOL(omap_mbox_init_seq);
129
130/*
131 * message sender
132 */
133static 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
157int 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}
176EXPORT_SYMBOL(omap_mbox_msg_send);
177
178static 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);
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500202 if (__blk_end_request(rq, 0, 0))
203 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800204 spin_unlock(q->queue_lock);
205 }
206}
207
208/*
209 * Message receiver(workqueue)
210 */
211static 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) {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700222 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800223 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
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500235 if (blk_end_request(rq, 0, 0))
236 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800237
238 mbox->rxq->callback((void *)msg);
239 }
240}
241
242/*
243 * Mailbox interrupt handler
244 */
Jens Axboe165125e2007-07-24 09:28:11 +0200245static void mbox_txq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800246{
247}
248
Jens Axboe165125e2007-07-24 09:28:11 +0200249static void mbox_rxq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800250{
251}
252
253static 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
260static void __mbox_rx_interrupt(struct omap_mbox *mbox)
261{
262 struct request *rq;
263 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200264 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800265
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);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700290nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800291 schedule_work(&mbox->rxq->work);
292}
293
294static irqreturn_t mbox_interrupt(int irq, void *p)
295{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400296 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800297
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 */
310static ssize_t
311omap_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
328static ssize_t
329omap_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
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500347 if (blk_end_request(rq, 0, 0))
348 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800349
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
362static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
363
364static ssize_t mbox_show(struct class *class, char *buf)
365{
366 return sprintf(buf, "mbox");
367}
368
369static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
370
371static struct class omap_mbox_class = {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700372 .name = "omap-mailbox",
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800373};
374
375static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
376 request_fn_proc * proc,
377 void (*work) (struct work_struct *))
378{
Jens Axboe165125e2007-07-24 09:28:11 +0200379 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380 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;
397error:
398 kfree(mq);
399 return NULL;
400}
401
402static void mbox_queue_free(struct omap_mbox_queue *q)
403{
404 blk_cleanup_queue(q->queue);
405 kfree(q);
406}
407
408static 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
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800419 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 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800426
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:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800448 if (unlikely(mbox->ops->shutdown))
449 mbox->ops->shutdown(mbox);
450
451 return ret;
452}
453
454static 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);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800460
461 if (unlikely(mbox->ops->shutdown))
462 mbox->ops->shutdown(mbox);
463}
464
465static 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
477struct 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}
497EXPORT_SYMBOL(omap_mbox_get);
498
499void omap_mbox_put(struct omap_mbox *mbox)
500{
501 omap_mbox_fini(mbox);
502}
503EXPORT_SYMBOL(omap_mbox_put);
504
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700505int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800506{
507 int ret = 0;
508 struct omap_mbox **tmp;
509
510 if (!mbox)
511 return -EINVAL;
512 if (mbox->next)
513 return -EBUSY;
514
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700515 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
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800524 write_lock(&mboxes_lock);
525 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700526 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800527 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700528 write_unlock(&mboxes_lock);
529 goto err_find;
530 }
531 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800532 write_unlock(&mboxes_lock);
533
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700534 return 0;
535
536err_find:
537 device_remove_file(mbox->dev, &dev_attr_mbox);
538err_sysfs:
539 device_unregister(mbox->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800540 return ret;
541}
542EXPORT_SYMBOL(omap_mbox_register);
543
544int 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);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700555 device_remove_file(mbox->dev, &dev_attr_mbox);
556 device_unregister(mbox->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800557 return 0;
558 }
559 tmp = &(*tmp)->next;
560 }
561 write_unlock(&mboxes_lock);
562
563 return -EINVAL;
564}
565EXPORT_SYMBOL(omap_mbox_unregister);
566
567static 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
576static 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
582subsys_initcall(omap_mbox_class_init);
583module_exit(omap_mbox_class_exit);
584
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700585MODULE_LICENSE("GPL v2");
586MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
587MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");