blob: 0abfbaa59871313c5ce3857457c3407f6ca4bafc [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
Hiroshi DOYU340a6142006-12-07 15:43:59 -080024#include <linux/module.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080025#include <linux/interrupt.h>
26#include <linux/device.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080027#include <linux/delay.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/mailbox.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080030
Hiroshi DOYU7a781af2009-03-23 18:07:31 -070031static int enable_seq_bit;
32module_param(enable_seq_bit, bool, 0);
33MODULE_PARM_DESC(enable_seq_bit, "Enable sequence bit checking.");
34
Hiroshi DOYU340a6142006-12-07 15:43:59 -080035static struct omap_mbox *mboxes;
36static DEFINE_RWLOCK(mboxes_lock);
37
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070038/*
39 * Mailbox sequence bit API
40 */
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070041
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070042/* seq_rcv should be initialized with any value other than
43 * 0 and 1 << 31, to allow either value for the first
44 * message. */
45static inline void mbox_seq_init(struct omap_mbox *mbox)
46{
Hiroshi DOYU7a781af2009-03-23 18:07:31 -070047 if (!enable_seq_bit)
48 return;
49
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070050 /* any value other than 0 and 1 << 31 */
51 mbox->seq_rcv = 0xffffffff;
52}
53
54static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
55{
Hiroshi DOYU7a781af2009-03-23 18:07:31 -070056 if (!enable_seq_bit)
57 return;
58
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070059 /* 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{
Hiroshi DOYU7a781af2009-03-23 18:07:31 -070067 mbox_msg_t seq;
68
69 if (!enable_seq_bit)
70 return 0;
71
72 seq = msg & (1 << 31);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070073 if (seq == mbox->seq_rcv)
74 return -1;
75 mbox->seq_rcv = seq;
76 return 0;
77}
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070078
79/* Mailbox FIFO handle functions */
80static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
81{
82 return mbox->ops->fifo_read(mbox);
83}
84static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
85{
86 mbox->ops->fifo_write(mbox, msg);
87}
88static inline int mbox_fifo_empty(struct omap_mbox *mbox)
89{
90 return mbox->ops->fifo_empty(mbox);
91}
92static inline int mbox_fifo_full(struct omap_mbox *mbox)
93{
94 return mbox->ops->fifo_full(mbox);
95}
96
97/* Mailbox IRQ handle functions */
98static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
99{
100 mbox->ops->enable_irq(mbox, irq);
101}
102static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
103{
104 mbox->ops->disable_irq(mbox, irq);
105}
106static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
107{
108 if (mbox->ops->ack_irq)
109 mbox->ops->ack_irq(mbox, irq);
110}
111static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
112{
113 return mbox->ops->is_irq(mbox, irq);
114}
115
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800116/* Mailbox Sequence Bit function */
117void omap_mbox_init_seq(struct omap_mbox *mbox)
118{
119 mbox_seq_init(mbox);
120}
121EXPORT_SYMBOL(omap_mbox_init_seq);
122
123/*
124 * message sender
125 */
126static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
127{
128 int ret = 0, i = 1000;
129
130 while (mbox_fifo_full(mbox)) {
131 if (mbox->ops->type == OMAP_MBOX_TYPE2)
132 return -1;
133 if (--i == 0)
134 return -1;
135 udelay(1);
136 }
137
138 if (arg && mbox->txq->callback) {
139 ret = mbox->txq->callback(arg);
140 if (ret)
141 goto out;
142 }
143
144 mbox_seq_toggle(mbox, &msg);
145 mbox_fifo_write(mbox, msg);
146 out:
147 return ret;
148}
149
150int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
151{
152 struct request *rq;
153 struct request_queue *q = mbox->txq->queue;
154 int ret = 0;
155
156 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
157 if (unlikely(!rq)) {
158 ret = -ENOMEM;
159 goto fail;
160 }
161
162 rq->data = (void *)msg;
163 blk_insert_request(q, rq, 0, arg);
164
165 schedule_work(&mbox->txq->work);
166 fail:
167 return ret;
168}
169EXPORT_SYMBOL(omap_mbox_msg_send);
170
171static void mbox_tx_work(struct work_struct *work)
172{
173 int ret;
174 struct request *rq;
175 struct omap_mbox_queue *mq = container_of(work,
176 struct omap_mbox_queue, work);
177 struct omap_mbox *mbox = mq->queue->queuedata;
178 struct request_queue *q = mbox->txq->queue;
179
180 while (1) {
181 spin_lock(q->queue_lock);
182 rq = elv_next_request(q);
183 spin_unlock(q->queue_lock);
184
185 if (!rq)
186 break;
187
188 ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
189 if (ret) {
190 enable_mbox_irq(mbox, IRQ_TX);
191 return;
192 }
193
194 spin_lock(q->queue_lock);
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500195 if (__blk_end_request(rq, 0, 0))
196 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800197 spin_unlock(q->queue_lock);
198 }
199}
200
201/*
202 * Message receiver(workqueue)
203 */
204static void mbox_rx_work(struct work_struct *work)
205{
206 struct omap_mbox_queue *mq =
207 container_of(work, struct omap_mbox_queue, work);
208 struct omap_mbox *mbox = mq->queue->queuedata;
209 struct request_queue *q = mbox->rxq->queue;
210 struct request *rq;
211 mbox_msg_t msg;
212 unsigned long flags;
213
214 if (mbox->rxq->callback == NULL) {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700215 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800216 return;
217 }
218
219 while (1) {
220 spin_lock_irqsave(q->queue_lock, flags);
221 rq = elv_next_request(q);
222 spin_unlock_irqrestore(q->queue_lock, flags);
223 if (!rq)
224 break;
225
226 msg = (mbox_msg_t) rq->data;
227
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500228 if (blk_end_request(rq, 0, 0))
229 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800230
231 mbox->rxq->callback((void *)msg);
232 }
233}
234
235/*
236 * Mailbox interrupt handler
237 */
Jens Axboe165125e2007-07-24 09:28:11 +0200238static void mbox_txq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800239{
240}
241
Jens Axboe165125e2007-07-24 09:28:11 +0200242static void mbox_rxq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800243{
244}
245
246static void __mbox_tx_interrupt(struct omap_mbox *mbox)
247{
248 disable_mbox_irq(mbox, IRQ_TX);
249 ack_mbox_irq(mbox, IRQ_TX);
250 schedule_work(&mbox->txq->work);
251}
252
253static void __mbox_rx_interrupt(struct omap_mbox *mbox)
254{
255 struct request *rq;
256 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200257 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800258
259 disable_mbox_irq(mbox, IRQ_RX);
260
261 while (!mbox_fifo_empty(mbox)) {
262 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
263 if (unlikely(!rq))
264 goto nomem;
265
266 msg = mbox_fifo_read(mbox);
267 rq->data = (void *)msg;
268
269 if (unlikely(mbox_seq_test(mbox, msg))) {
270 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
271 if (mbox->err_notify)
272 mbox->err_notify();
273 }
274
275 blk_insert_request(q, rq, 0, NULL);
276 if (mbox->ops->type == OMAP_MBOX_TYPE1)
277 break;
278 }
279
280 /* no more messages in the fifo. clear IRQ source. */
281 ack_mbox_irq(mbox, IRQ_RX);
282 enable_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700283nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800284 schedule_work(&mbox->rxq->work);
285}
286
287static irqreturn_t mbox_interrupt(int irq, void *p)
288{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400289 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800290
291 if (is_mbox_irq(mbox, IRQ_TX))
292 __mbox_tx_interrupt(mbox);
293
294 if (is_mbox_irq(mbox, IRQ_RX))
295 __mbox_rx_interrupt(mbox);
296
297 return IRQ_HANDLED;
298}
299
300/*
301 * sysfs files
302 */
303static ssize_t
304omap_mbox_write(struct device *dev, struct device_attribute *attr,
305 const char * buf, size_t count)
306{
307 int ret;
308 mbox_msg_t *p = (mbox_msg_t *)buf;
309 struct omap_mbox *mbox = dev_get_drvdata(dev);
310
311 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
312 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
313 if (ret)
314 return -EAGAIN;
315 p++;
316 }
317
318 return (size_t)((char *)p - buf);
319}
320
321static ssize_t
322omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
323{
324 unsigned long flags;
325 struct request *rq;
326 mbox_msg_t *p = (mbox_msg_t *) buf;
327 struct omap_mbox *mbox = dev_get_drvdata(dev);
328 struct request_queue *q = mbox->rxq->queue;
329
330 while (1) {
331 spin_lock_irqsave(q->queue_lock, flags);
332 rq = elv_next_request(q);
333 spin_unlock_irqrestore(q->queue_lock, flags);
334
335 if (!rq)
336 break;
337
338 *p = (mbox_msg_t) rq->data;
339
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500340 if (blk_end_request(rq, 0, 0))
341 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800342
343 if (unlikely(mbox_seq_test(mbox, *p))) {
344 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
345 continue;
346 }
347 p++;
348 }
349
350 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
351
352 return (size_t) ((char *)p - buf);
353}
354
355static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
356
357static ssize_t mbox_show(struct class *class, char *buf)
358{
359 return sprintf(buf, "mbox");
360}
361
362static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
363
364static struct class omap_mbox_class = {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700365 .name = "omap-mailbox",
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800366};
367
368static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
369 request_fn_proc * proc,
370 void (*work) (struct work_struct *))
371{
Jens Axboe165125e2007-07-24 09:28:11 +0200372 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800373 struct omap_mbox_queue *mq;
374
375 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
376 if (!mq)
377 return NULL;
378
379 spin_lock_init(&mq->lock);
380
381 q = blk_init_queue(proc, &mq->lock);
382 if (!q)
383 goto error;
384 q->queuedata = mbox;
385 mq->queue = q;
386
387 INIT_WORK(&mq->work, work);
388
389 return mq;
390error:
391 kfree(mq);
392 return NULL;
393}
394
395static void mbox_queue_free(struct omap_mbox_queue *q)
396{
397 blk_cleanup_queue(q->queue);
398 kfree(q);
399}
400
401static int omap_mbox_init(struct omap_mbox *mbox)
402{
403 int ret;
404 struct omap_mbox_queue *mq;
405
406 if (likely(mbox->ops->startup)) {
407 ret = mbox->ops->startup(mbox);
408 if (unlikely(ret))
409 return ret;
410 }
411
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800412 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
413 mbox->name, mbox);
414 if (unlikely(ret)) {
415 printk(KERN_ERR
416 "failed to register mailbox interrupt:%d\n", ret);
417 goto fail_request_irq;
418 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800419
420 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
421 if (!mq) {
422 ret = -ENOMEM;
423 goto fail_alloc_txq;
424 }
425 mbox->txq = mq;
426
427 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
428 if (!mq) {
429 ret = -ENOMEM;
430 goto fail_alloc_rxq;
431 }
432 mbox->rxq = mq;
433
434 return 0;
435
436 fail_alloc_rxq:
437 mbox_queue_free(mbox->txq);
438 fail_alloc_txq:
439 free_irq(mbox->irq, mbox);
440 fail_request_irq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800441 if (unlikely(mbox->ops->shutdown))
442 mbox->ops->shutdown(mbox);
443
444 return ret;
445}
446
447static void omap_mbox_fini(struct omap_mbox *mbox)
448{
449 mbox_queue_free(mbox->txq);
450 mbox_queue_free(mbox->rxq);
451
452 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800453
454 if (unlikely(mbox->ops->shutdown))
455 mbox->ops->shutdown(mbox);
456}
457
458static struct omap_mbox **find_mboxes(const char *name)
459{
460 struct omap_mbox **p;
461
462 for (p = &mboxes; *p; p = &(*p)->next) {
463 if (strcmp((*p)->name, name) == 0)
464 break;
465 }
466
467 return p;
468}
469
470struct omap_mbox *omap_mbox_get(const char *name)
471{
472 struct omap_mbox *mbox;
473 int ret;
474
475 read_lock(&mboxes_lock);
476 mbox = *(find_mboxes(name));
477 if (mbox == NULL) {
478 read_unlock(&mboxes_lock);
479 return ERR_PTR(-ENOENT);
480 }
481
482 read_unlock(&mboxes_lock);
483
484 ret = omap_mbox_init(mbox);
485 if (ret)
486 return ERR_PTR(-ENODEV);
487
488 return mbox;
489}
490EXPORT_SYMBOL(omap_mbox_get);
491
492void omap_mbox_put(struct omap_mbox *mbox)
493{
494 omap_mbox_fini(mbox);
495}
496EXPORT_SYMBOL(omap_mbox_put);
497
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700498int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800499{
500 int ret = 0;
501 struct omap_mbox **tmp;
502
503 if (!mbox)
504 return -EINVAL;
505 if (mbox->next)
506 return -EBUSY;
507
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700508 mbox->dev = device_create(&omap_mbox_class,
509 parent, 0, mbox, "%s", mbox->name);
510 if (IS_ERR(mbox->dev))
511 return PTR_ERR(mbox->dev);
512
513 ret = device_create_file(mbox->dev, &dev_attr_mbox);
514 if (ret)
515 goto err_sysfs;
516
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800517 write_lock(&mboxes_lock);
518 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700519 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800520 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700521 write_unlock(&mboxes_lock);
522 goto err_find;
523 }
524 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800525 write_unlock(&mboxes_lock);
526
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700527 return 0;
528
529err_find:
530 device_remove_file(mbox->dev, &dev_attr_mbox);
531err_sysfs:
532 device_unregister(mbox->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800533 return ret;
534}
535EXPORT_SYMBOL(omap_mbox_register);
536
537int omap_mbox_unregister(struct omap_mbox *mbox)
538{
539 struct omap_mbox **tmp;
540
541 write_lock(&mboxes_lock);
542 tmp = &mboxes;
543 while (*tmp) {
544 if (mbox == *tmp) {
545 *tmp = mbox->next;
546 mbox->next = NULL;
547 write_unlock(&mboxes_lock);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700548 device_remove_file(mbox->dev, &dev_attr_mbox);
549 device_unregister(mbox->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800550 return 0;
551 }
552 tmp = &(*tmp)->next;
553 }
554 write_unlock(&mboxes_lock);
555
556 return -EINVAL;
557}
558EXPORT_SYMBOL(omap_mbox_unregister);
559
560static int __init omap_mbox_class_init(void)
561{
562 int ret = class_register(&omap_mbox_class);
563 if (!ret)
564 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
565
566 return ret;
567}
568
569static void __exit omap_mbox_class_exit(void)
570{
571 class_remove_file(&omap_mbox_class, &class_attr_mbox);
572 class_unregister(&omap_mbox_class);
573}
574
575subsys_initcall(omap_mbox_class_init);
576module_exit(omap_mbox_class_exit);
577
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700578MODULE_LICENSE("GPL v2");
579MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
580MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");