blob: f82810eeb3f48ed96fd7506118e55c56d6ddfe7a [file] [log] [blame]
Hiroshi DOYU340a614a2006-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 DOYU340a614a2006-12-07 15:43:59 -08005 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07006 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU340a614a2006-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 DOYU340a614a2006-12-07 15:43:59 -080024#include <linux/module.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080025#include <linux/interrupt.h>
26#include <linux/device.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080027#include <linux/delay.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070028
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/mailbox.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080030
31static struct omap_mbox *mboxes;
32static DEFINE_RWLOCK(mboxes_lock);
33
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070034/* Mailbox FIFO handle functions */
35static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
36{
37 return mbox->ops->fifo_read(mbox);
38}
39static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
40{
41 mbox->ops->fifo_write(mbox, msg);
42}
43static inline int mbox_fifo_empty(struct omap_mbox *mbox)
44{
45 return mbox->ops->fifo_empty(mbox);
46}
47static inline int mbox_fifo_full(struct omap_mbox *mbox)
48{
49 return mbox->ops->fifo_full(mbox);
50}
51
52/* Mailbox IRQ handle functions */
53static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
54{
55 mbox->ops->enable_irq(mbox, irq);
56}
57static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
58{
59 mbox->ops->disable_irq(mbox, irq);
60}
61static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
62{
63 if (mbox->ops->ack_irq)
64 mbox->ops->ack_irq(mbox, irq);
65}
66static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
67{
68 return mbox->ops->is_irq(mbox, irq);
69}
70
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080071/*
72 * message sender
73 */
74static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
75{
76 int ret = 0, i = 1000;
77
78 while (mbox_fifo_full(mbox)) {
79 if (mbox->ops->type == OMAP_MBOX_TYPE2)
80 return -1;
81 if (--i == 0)
82 return -1;
83 udelay(1);
84 }
85
86 if (arg && mbox->txq->callback) {
87 ret = mbox->txq->callback(arg);
88 if (ret)
89 goto out;
90 }
91
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080092 mbox_fifo_write(mbox, msg);
93 out:
94 return ret;
95}
96
Tejun Heoec247512009-04-23 11:05:20 +090097struct omap_msg_tx_data {
98 mbox_msg_t msg;
99 void *arg;
100};
101
102static void omap_msg_tx_end_io(struct request *rq, int error)
103{
104 kfree(rq->special);
105 __blk_put_request(rq->q, rq);
106}
107
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800108int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
109{
Tejun Heoec247512009-04-23 11:05:20 +0900110 struct omap_msg_tx_data *tx_data;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800111 struct request *rq;
112 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +0900113
114 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
115 if (unlikely(!tx_data))
116 return -ENOMEM;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800117
118 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
119 if (unlikely(!rq)) {
Tejun Heoec247512009-04-23 11:05:20 +0900120 kfree(tx_data);
121 return -ENOMEM;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800122 }
123
Tejun Heoec247512009-04-23 11:05:20 +0900124 tx_data->msg = msg;
125 tx_data->arg = arg;
126 rq->end_io = omap_msg_tx_end_io;
127 blk_insert_request(q, rq, 0, tx_data);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800128
129 schedule_work(&mbox->txq->work);
Tejun Heoec247512009-04-23 11:05:20 +0900130 return 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800131}
132EXPORT_SYMBOL(omap_mbox_msg_send);
133
134static void mbox_tx_work(struct work_struct *work)
135{
136 int ret;
137 struct request *rq;
138 struct omap_mbox_queue *mq = container_of(work,
139 struct omap_mbox_queue, work);
140 struct omap_mbox *mbox = mq->queue->queuedata;
141 struct request_queue *q = mbox->txq->queue;
142
143 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900144 struct omap_msg_tx_data *tx_data;
145
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800146 spin_lock(q->queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900147 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800148 spin_unlock(q->queue_lock);
149
150 if (!rq)
151 break;
152
Tejun Heoec247512009-04-23 11:05:20 +0900153 tx_data = rq->special;
154
155 ret = __mbox_msg_send(mbox, tx_data->msg, tx_data->arg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800156 if (ret) {
157 enable_mbox_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900158 spin_lock(q->queue_lock);
159 blk_requeue_request(q, rq);
160 spin_unlock(q->queue_lock);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800161 return;
162 }
163
164 spin_lock(q->queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900165 __blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800166 spin_unlock(q->queue_lock);
167 }
168}
169
170/*
171 * Message receiver(workqueue)
172 */
173static void mbox_rx_work(struct work_struct *work)
174{
175 struct omap_mbox_queue *mq =
176 container_of(work, struct omap_mbox_queue, work);
177 struct omap_mbox *mbox = mq->queue->queuedata;
178 struct request_queue *q = mbox->rxq->queue;
179 struct request *rq;
180 mbox_msg_t msg;
181 unsigned long flags;
182
183 if (mbox->rxq->callback == NULL) {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700184 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800185 return;
186 }
187
188 while (1) {
189 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900190 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800191 spin_unlock_irqrestore(q->queue_lock, flags);
192 if (!rq)
193 break;
194
Tejun Heoec247512009-04-23 11:05:20 +0900195 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900196 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800197 mbox->rxq->callback((void *)msg);
198 }
199}
200
201/*
202 * Mailbox interrupt handler
203 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800204static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800205{
206}
207
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800208static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800209{
210}
211
212static void __mbox_tx_interrupt(struct omap_mbox *mbox)
213{
214 disable_mbox_irq(mbox, IRQ_TX);
215 ack_mbox_irq(mbox, IRQ_TX);
216 schedule_work(&mbox->txq->work);
217}
218
219static void __mbox_rx_interrupt(struct omap_mbox *mbox)
220{
221 struct request *rq;
222 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200223 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800224
225 disable_mbox_irq(mbox, IRQ_RX);
226
227 while (!mbox_fifo_empty(mbox)) {
228 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
229 if (unlikely(!rq))
230 goto nomem;
231
232 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800233
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800234
Tejun Heoec247512009-04-23 11:05:20 +0900235 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800236 if (mbox->ops->type == OMAP_MBOX_TYPE1)
237 break;
238 }
239
240 /* no more messages in the fifo. clear IRQ source. */
241 ack_mbox_irq(mbox, IRQ_RX);
242 enable_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700243nomem:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800244 schedule_work(&mbox->rxq->work);
245}
246
247static irqreturn_t mbox_interrupt(int irq, void *p)
248{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400249 struct omap_mbox *mbox = p;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800250
251 if (is_mbox_irq(mbox, IRQ_TX))
252 __mbox_tx_interrupt(mbox);
253
254 if (is_mbox_irq(mbox, IRQ_RX))
255 __mbox_rx_interrupt(mbox);
256
257 return IRQ_HANDLED;
258}
259
260/*
261 * sysfs files
262 */
263static ssize_t
264omap_mbox_write(struct device *dev, struct device_attribute *attr,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800265 const char *buf, size_t count)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800266{
267 int ret;
268 mbox_msg_t *p = (mbox_msg_t *)buf;
269 struct omap_mbox *mbox = dev_get_drvdata(dev);
270
271 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
272 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
273 if (ret)
274 return -EAGAIN;
275 p++;
276 }
277
278 return (size_t)((char *)p - buf);
279}
280
281static ssize_t
282omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
283{
284 unsigned long flags;
285 struct request *rq;
286 mbox_msg_t *p = (mbox_msg_t *) buf;
287 struct omap_mbox *mbox = dev_get_drvdata(dev);
288 struct request_queue *q = mbox->rxq->queue;
289
290 while (1) {
291 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900292 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800293 spin_unlock_irqrestore(q->queue_lock, flags);
294
295 if (!rq)
296 break;
297
Tejun Heoec247512009-04-23 11:05:20 +0900298 *p = (mbox_msg_t)rq->special;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800299
Tejun Heo40cbbb72009-04-23 11:05:19 +0900300 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800301
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800302 p++;
303 }
304
305 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
306
307 return (size_t) ((char *)p - buf);
308}
309
310static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
311
312static ssize_t mbox_show(struct class *class, char *buf)
313{
314 return sprintf(buf, "mbox");
315}
316
317static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
318
319static struct class omap_mbox_class = {
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700320 .name = "omap-mailbox",
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800321};
322
323static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800324 request_fn_proc *proc,
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800325 void (*work) (struct work_struct *))
326{
Jens Axboe165125e2007-07-24 09:28:11 +0200327 struct request_queue *q;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800328 struct omap_mbox_queue *mq;
329
330 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
331 if (!mq)
332 return NULL;
333
334 spin_lock_init(&mq->lock);
335
336 q = blk_init_queue(proc, &mq->lock);
337 if (!q)
338 goto error;
339 q->queuedata = mbox;
340 mq->queue = q;
341
342 INIT_WORK(&mq->work, work);
343
344 return mq;
345error:
346 kfree(mq);
347 return NULL;
348}
349
350static void mbox_queue_free(struct omap_mbox_queue *q)
351{
352 blk_cleanup_queue(q->queue);
353 kfree(q);
354}
355
356static int omap_mbox_init(struct omap_mbox *mbox)
357{
358 int ret;
359 struct omap_mbox_queue *mq;
360
361 if (likely(mbox->ops->startup)) {
362 ret = mbox->ops->startup(mbox);
363 if (unlikely(ret))
364 return ret;
365 }
366
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800367 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
368 mbox->name, mbox);
369 if (unlikely(ret)) {
370 printk(KERN_ERR
371 "failed to register mailbox interrupt:%d\n", ret);
372 goto fail_request_irq;
373 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800374
375 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
376 if (!mq) {
377 ret = -ENOMEM;
378 goto fail_alloc_txq;
379 }
380 mbox->txq = mq;
381
382 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
383 if (!mq) {
384 ret = -ENOMEM;
385 goto fail_alloc_rxq;
386 }
387 mbox->rxq = mq;
388
389 return 0;
390
391 fail_alloc_rxq:
392 mbox_queue_free(mbox->txq);
393 fail_alloc_txq:
394 free_irq(mbox->irq, mbox);
395 fail_request_irq:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800396 if (unlikely(mbox->ops->shutdown))
397 mbox->ops->shutdown(mbox);
398
399 return ret;
400}
401
402static void omap_mbox_fini(struct omap_mbox *mbox)
403{
404 mbox_queue_free(mbox->txq);
405 mbox_queue_free(mbox->rxq);
406
407 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800408
409 if (unlikely(mbox->ops->shutdown))
410 mbox->ops->shutdown(mbox);
411}
412
413static struct omap_mbox **find_mboxes(const char *name)
414{
415 struct omap_mbox **p;
416
417 for (p = &mboxes; *p; p = &(*p)->next) {
418 if (strcmp((*p)->name, name) == 0)
419 break;
420 }
421
422 return p;
423}
424
425struct omap_mbox *omap_mbox_get(const char *name)
426{
427 struct omap_mbox *mbox;
428 int ret;
429
430 read_lock(&mboxes_lock);
431 mbox = *(find_mboxes(name));
432 if (mbox == NULL) {
433 read_unlock(&mboxes_lock);
434 return ERR_PTR(-ENOENT);
435 }
436
437 read_unlock(&mboxes_lock);
438
439 ret = omap_mbox_init(mbox);
440 if (ret)
441 return ERR_PTR(-ENODEV);
442
443 return mbox;
444}
445EXPORT_SYMBOL(omap_mbox_get);
446
447void omap_mbox_put(struct omap_mbox *mbox)
448{
449 omap_mbox_fini(mbox);
450}
451EXPORT_SYMBOL(omap_mbox_put);
452
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700453int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800454{
455 int ret = 0;
456 struct omap_mbox **tmp;
457
458 if (!mbox)
459 return -EINVAL;
460 if (mbox->next)
461 return -EBUSY;
462
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700463 mbox->dev = device_create(&omap_mbox_class,
464 parent, 0, mbox, "%s", mbox->name);
465 if (IS_ERR(mbox->dev))
466 return PTR_ERR(mbox->dev);
467
468 ret = device_create_file(mbox->dev, &dev_attr_mbox);
469 if (ret)
470 goto err_sysfs;
471
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800472 write_lock(&mboxes_lock);
473 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700474 if (*tmp) {
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800475 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700476 write_unlock(&mboxes_lock);
477 goto err_find;
478 }
479 *tmp = mbox;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800480 write_unlock(&mboxes_lock);
481
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700482 return 0;
483
484err_find:
485 device_remove_file(mbox->dev, &dev_attr_mbox);
486err_sysfs:
487 device_unregister(mbox->dev);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800488 return ret;
489}
490EXPORT_SYMBOL(omap_mbox_register);
491
492int omap_mbox_unregister(struct omap_mbox *mbox)
493{
494 struct omap_mbox **tmp;
495
496 write_lock(&mboxes_lock);
497 tmp = &mboxes;
498 while (*tmp) {
499 if (mbox == *tmp) {
500 *tmp = mbox->next;
501 mbox->next = NULL;
502 write_unlock(&mboxes_lock);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700503 device_remove_file(mbox->dev, &dev_attr_mbox);
504 device_unregister(mbox->dev);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800505 return 0;
506 }
507 tmp = &(*tmp)->next;
508 }
509 write_unlock(&mboxes_lock);
510
511 return -EINVAL;
512}
513EXPORT_SYMBOL(omap_mbox_unregister);
514
515static int __init omap_mbox_class_init(void)
516{
517 int ret = class_register(&omap_mbox_class);
518 if (!ret)
519 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
520
521 return ret;
522}
523
524static void __exit omap_mbox_class_exit(void)
525{
526 class_remove_file(&omap_mbox_class, &class_attr_mbox);
527 class_unregister(&omap_mbox_class);
528}
529
530subsys_initcall(omap_mbox_class_init);
531module_exit(omap_mbox_class_exit);
532
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700533MODULE_LICENSE("GPL v2");
534MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
535MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");