blob: 1d7aec1a691a67b27d7b0f9a0875ada1da7c632f [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
2 * OMAP mailbox driver
3 *
4 * Copyright (C) 2006 Nokia Corporation. All rights reserved.
5 *
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7 * Restructured by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/sched.h>
28#include <linux/interrupt.h>
29#include <linux/device.h>
30#include <linux/blkdev.h>
31#include <linux/err.h>
32#include <linux/delay.h>
33#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/mailbox.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080035#include "mailbox.h"
36
37static struct omap_mbox *mboxes;
38static DEFINE_RWLOCK(mboxes_lock);
39
40/* Mailbox Sequence Bit function */
41void omap_mbox_init_seq(struct omap_mbox *mbox)
42{
43 mbox_seq_init(mbox);
44}
45EXPORT_SYMBOL(omap_mbox_init_seq);
46
47/*
48 * message sender
49 */
50static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
51{
52 int ret = 0, i = 1000;
53
54 while (mbox_fifo_full(mbox)) {
55 if (mbox->ops->type == OMAP_MBOX_TYPE2)
56 return -1;
57 if (--i == 0)
58 return -1;
59 udelay(1);
60 }
61
62 if (arg && mbox->txq->callback) {
63 ret = mbox->txq->callback(arg);
64 if (ret)
65 goto out;
66 }
67
68 mbox_seq_toggle(mbox, &msg);
69 mbox_fifo_write(mbox, msg);
70 out:
71 return ret;
72}
73
74int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
75{
76 struct request *rq;
77 struct request_queue *q = mbox->txq->queue;
78 int ret = 0;
79
80 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
81 if (unlikely(!rq)) {
82 ret = -ENOMEM;
83 goto fail;
84 }
85
86 rq->data = (void *)msg;
87 blk_insert_request(q, rq, 0, arg);
88
89 schedule_work(&mbox->txq->work);
90 fail:
91 return ret;
92}
93EXPORT_SYMBOL(omap_mbox_msg_send);
94
95static void mbox_tx_work(struct work_struct *work)
96{
97 int ret;
98 struct request *rq;
99 struct omap_mbox_queue *mq = container_of(work,
100 struct omap_mbox_queue, work);
101 struct omap_mbox *mbox = mq->queue->queuedata;
102 struct request_queue *q = mbox->txq->queue;
103
104 while (1) {
105 spin_lock(q->queue_lock);
106 rq = elv_next_request(q);
107 spin_unlock(q->queue_lock);
108
109 if (!rq)
110 break;
111
112 ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
113 if (ret) {
114 enable_mbox_irq(mbox, IRQ_TX);
115 return;
116 }
117
118 spin_lock(q->queue_lock);
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500119 if (__blk_end_request(rq, 0, 0))
120 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800121 spin_unlock(q->queue_lock);
122 }
123}
124
125/*
126 * Message receiver(workqueue)
127 */
128static void mbox_rx_work(struct work_struct *work)
129{
130 struct omap_mbox_queue *mq =
131 container_of(work, struct omap_mbox_queue, work);
132 struct omap_mbox *mbox = mq->queue->queuedata;
133 struct request_queue *q = mbox->rxq->queue;
134 struct request *rq;
135 mbox_msg_t msg;
136 unsigned long flags;
137
138 if (mbox->rxq->callback == NULL) {
139 sysfs_notify(&mbox->dev.kobj, NULL, "mbox");
140 return;
141 }
142
143 while (1) {
144 spin_lock_irqsave(q->queue_lock, flags);
145 rq = elv_next_request(q);
146 spin_unlock_irqrestore(q->queue_lock, flags);
147 if (!rq)
148 break;
149
150 msg = (mbox_msg_t) rq->data;
151
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500152 if (blk_end_request(rq, 0, 0))
153 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800154
155 mbox->rxq->callback((void *)msg);
156 }
157}
158
159/*
160 * Mailbox interrupt handler
161 */
Jens Axboe165125e2007-07-24 09:28:11 +0200162static void mbox_txq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800163{
164}
165
Jens Axboe165125e2007-07-24 09:28:11 +0200166static void mbox_rxq_fn(struct request_queue * q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800167{
168}
169
170static void __mbox_tx_interrupt(struct omap_mbox *mbox)
171{
172 disable_mbox_irq(mbox, IRQ_TX);
173 ack_mbox_irq(mbox, IRQ_TX);
174 schedule_work(&mbox->txq->work);
175}
176
177static void __mbox_rx_interrupt(struct omap_mbox *mbox)
178{
179 struct request *rq;
180 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200181 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800182
183 disable_mbox_irq(mbox, IRQ_RX);
184
185 while (!mbox_fifo_empty(mbox)) {
186 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
187 if (unlikely(!rq))
188 goto nomem;
189
190 msg = mbox_fifo_read(mbox);
191 rq->data = (void *)msg;
192
193 if (unlikely(mbox_seq_test(mbox, msg))) {
194 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
195 if (mbox->err_notify)
196 mbox->err_notify();
197 }
198
199 blk_insert_request(q, rq, 0, NULL);
200 if (mbox->ops->type == OMAP_MBOX_TYPE1)
201 break;
202 }
203
204 /* no more messages in the fifo. clear IRQ source. */
205 ack_mbox_irq(mbox, IRQ_RX);
206 enable_mbox_irq(mbox, IRQ_RX);
207 nomem:
208 schedule_work(&mbox->rxq->work);
209}
210
211static irqreturn_t mbox_interrupt(int irq, void *p)
212{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400213 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800214
215 if (is_mbox_irq(mbox, IRQ_TX))
216 __mbox_tx_interrupt(mbox);
217
218 if (is_mbox_irq(mbox, IRQ_RX))
219 __mbox_rx_interrupt(mbox);
220
221 return IRQ_HANDLED;
222}
223
224/*
225 * sysfs files
226 */
227static ssize_t
228omap_mbox_write(struct device *dev, struct device_attribute *attr,
229 const char * buf, size_t count)
230{
231 int ret;
232 mbox_msg_t *p = (mbox_msg_t *)buf;
233 struct omap_mbox *mbox = dev_get_drvdata(dev);
234
235 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
236 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
237 if (ret)
238 return -EAGAIN;
239 p++;
240 }
241
242 return (size_t)((char *)p - buf);
243}
244
245static ssize_t
246omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
247{
248 unsigned long flags;
249 struct request *rq;
250 mbox_msg_t *p = (mbox_msg_t *) buf;
251 struct omap_mbox *mbox = dev_get_drvdata(dev);
252 struct request_queue *q = mbox->rxq->queue;
253
254 while (1) {
255 spin_lock_irqsave(q->queue_lock, flags);
256 rq = elv_next_request(q);
257 spin_unlock_irqrestore(q->queue_lock, flags);
258
259 if (!rq)
260 break;
261
262 *p = (mbox_msg_t) rq->data;
263
Kiyoshi Ueda650e9cf2007-12-11 17:42:27 -0500264 if (blk_end_request(rq, 0, 0))
265 BUG();
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800266
267 if (unlikely(mbox_seq_test(mbox, *p))) {
268 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
269 continue;
270 }
271 p++;
272 }
273
274 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
275
276 return (size_t) ((char *)p - buf);
277}
278
279static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
280
281static ssize_t mbox_show(struct class *class, char *buf)
282{
283 return sprintf(buf, "mbox");
284}
285
286static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
287
288static struct class omap_mbox_class = {
289 .name = "omap_mbox",
290};
291
292static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
293 request_fn_proc * proc,
294 void (*work) (struct work_struct *))
295{
Jens Axboe165125e2007-07-24 09:28:11 +0200296 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800297 struct omap_mbox_queue *mq;
298
299 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
300 if (!mq)
301 return NULL;
302
303 spin_lock_init(&mq->lock);
304
305 q = blk_init_queue(proc, &mq->lock);
306 if (!q)
307 goto error;
308 q->queuedata = mbox;
309 mq->queue = q;
310
311 INIT_WORK(&mq->work, work);
312
313 return mq;
314error:
315 kfree(mq);
316 return NULL;
317}
318
319static void mbox_queue_free(struct omap_mbox_queue *q)
320{
321 blk_cleanup_queue(q->queue);
322 kfree(q);
323}
324
325static int omap_mbox_init(struct omap_mbox *mbox)
326{
327 int ret;
328 struct omap_mbox_queue *mq;
329
330 if (likely(mbox->ops->startup)) {
331 ret = mbox->ops->startup(mbox);
332 if (unlikely(ret))
333 return ret;
334 }
335
336 mbox->dev.class = &omap_mbox_class;
Kay Sievers3f978702008-05-30 17:42:11 +0200337 dev_set_name(&mbox->dev, "%s", mbox->name);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800338 dev_set_drvdata(&mbox->dev, mbox);
339
340 ret = device_register(&mbox->dev);
341 if (unlikely(ret))
342 goto fail_device_reg;
343
344 ret = device_create_file(&mbox->dev, &dev_attr_mbox);
345 if (unlikely(ret)) {
346 printk(KERN_ERR
347 "device_create_file failed: %d\n", ret);
348 goto fail_create_mbox;
349 }
350
351 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
352 mbox->name, mbox);
353 if (unlikely(ret)) {
354 printk(KERN_ERR
355 "failed to register mailbox interrupt:%d\n", ret);
356 goto fail_request_irq;
357 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800358
359 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
360 if (!mq) {
361 ret = -ENOMEM;
362 goto fail_alloc_txq;
363 }
364 mbox->txq = mq;
365
366 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
367 if (!mq) {
368 ret = -ENOMEM;
369 goto fail_alloc_rxq;
370 }
371 mbox->rxq = mq;
372
373 return 0;
374
375 fail_alloc_rxq:
376 mbox_queue_free(mbox->txq);
377 fail_alloc_txq:
378 free_irq(mbox->irq, mbox);
379 fail_request_irq:
380 device_remove_file(&mbox->dev, &dev_attr_mbox);
381 fail_create_mbox:
382 device_unregister(&mbox->dev);
383 fail_device_reg:
384 if (unlikely(mbox->ops->shutdown))
385 mbox->ops->shutdown(mbox);
386
387 return ret;
388}
389
390static void omap_mbox_fini(struct omap_mbox *mbox)
391{
392 mbox_queue_free(mbox->txq);
393 mbox_queue_free(mbox->rxq);
394
395 free_irq(mbox->irq, mbox);
396 device_remove_file(&mbox->dev, &dev_attr_mbox);
397 class_unregister(&omap_mbox_class);
398
399 if (unlikely(mbox->ops->shutdown))
400 mbox->ops->shutdown(mbox);
401}
402
403static struct omap_mbox **find_mboxes(const char *name)
404{
405 struct omap_mbox **p;
406
407 for (p = &mboxes; *p; p = &(*p)->next) {
408 if (strcmp((*p)->name, name) == 0)
409 break;
410 }
411
412 return p;
413}
414
415struct omap_mbox *omap_mbox_get(const char *name)
416{
417 struct omap_mbox *mbox;
418 int ret;
419
420 read_lock(&mboxes_lock);
421 mbox = *(find_mboxes(name));
422 if (mbox == NULL) {
423 read_unlock(&mboxes_lock);
424 return ERR_PTR(-ENOENT);
425 }
426
427 read_unlock(&mboxes_lock);
428
429 ret = omap_mbox_init(mbox);
430 if (ret)
431 return ERR_PTR(-ENODEV);
432
433 return mbox;
434}
435EXPORT_SYMBOL(omap_mbox_get);
436
437void omap_mbox_put(struct omap_mbox *mbox)
438{
439 omap_mbox_fini(mbox);
440}
441EXPORT_SYMBOL(omap_mbox_put);
442
443int omap_mbox_register(struct omap_mbox *mbox)
444{
445 int ret = 0;
446 struct omap_mbox **tmp;
447
448 if (!mbox)
449 return -EINVAL;
450 if (mbox->next)
451 return -EBUSY;
452
453 write_lock(&mboxes_lock);
454 tmp = find_mboxes(mbox->name);
455 if (*tmp)
456 ret = -EBUSY;
457 else
458 *tmp = mbox;
459 write_unlock(&mboxes_lock);
460
461 return ret;
462}
463EXPORT_SYMBOL(omap_mbox_register);
464
465int omap_mbox_unregister(struct omap_mbox *mbox)
466{
467 struct omap_mbox **tmp;
468
469 write_lock(&mboxes_lock);
470 tmp = &mboxes;
471 while (*tmp) {
472 if (mbox == *tmp) {
473 *tmp = mbox->next;
474 mbox->next = NULL;
475 write_unlock(&mboxes_lock);
476 return 0;
477 }
478 tmp = &(*tmp)->next;
479 }
480 write_unlock(&mboxes_lock);
481
482 return -EINVAL;
483}
484EXPORT_SYMBOL(omap_mbox_unregister);
485
486static int __init omap_mbox_class_init(void)
487{
488 int ret = class_register(&omap_mbox_class);
489 if (!ret)
490 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
491
492 return ret;
493}
494
495static void __exit omap_mbox_class_exit(void)
496{
497 class_remove_file(&omap_mbox_class, &class_attr_mbox);
498 class_unregister(&omap_mbox_class);
499}
500
501subsys_initcall(omap_mbox_class_init);
502module_exit(omap_mbox_class_exit);
503
504MODULE_LICENSE("GPL");