]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/mailbox.c
ARM: OMAP: Restructuring mailbox blk queues
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / mailbox.c
1 /*
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>
34 #include <asm/arch/mailbox.h>
35 #include "mailbox.h"
36
37 static struct omap_mbox *mboxes;
38 static DEFINE_RWLOCK(mboxes_lock);
39
40 /* Mailbox Sequence Bit function */
41 void omap_mbox_init_seq(struct omap_mbox *mbox)
42 {
43         mbox_seq_init(mbox);
44 }
45 EXPORT_SYMBOL(omap_mbox_init_seq);
46
47 /*
48  * message sender
49  */
50 static 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
74 int 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 }
93 EXPORT_SYMBOL(omap_mbox_msg_send);
94
95 static 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, struct omap_mbox_queue, work);
100         struct omap_mbox *mbox = mq->queue->queuedata;
101         struct request_queue *q = mbox->txq->queue;
102
103         while (1) {
104                 spin_lock(q->queue_lock);
105                 rq = elv_next_request(q);
106                 spin_unlock(q->queue_lock);
107
108                 if (!rq)
109                         break;
110
111                 ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
112                 if (ret) {
113                         enable_mbox_irq(mbox, IRQ_TX);
114                         return;
115                 }
116
117                 spin_lock(q->queue_lock);
118                 blkdev_dequeue_request(rq);
119                 end_that_request_last(rq, 0);
120                 spin_unlock(q->queue_lock);
121         }
122 }
123
124 /*
125  * Message receiver(workqueue)
126  */
127 static void mbox_rx_work(struct work_struct *work)
128 {
129         struct omap_mbox_queue *mq = container_of(work, struct omap_mbox_queue, work);
130         struct omap_mbox *mbox = mq->queue->queuedata;
131         struct request_queue *q = mbox->rxq->queue;
132         struct request *rq;
133         mbox_msg_t msg;
134         unsigned long flags;
135
136         if (mbox->rxq->callback == NULL) {
137                 sysfs_notify(&mbox->dev.kobj, NULL, "mbox");
138                 return;
139         }
140
141         while (1) {
142                 spin_lock_irqsave(q->queue_lock, flags);
143                 rq = elv_next_request(q);
144                 spin_unlock_irqrestore(q->queue_lock, flags);
145                 if (!rq)
146                         break;
147
148                 msg = (mbox_msg_t) rq->data;
149
150                 spin_lock_irqsave(q->queue_lock, flags);
151                 blkdev_dequeue_request(rq);
152                 end_that_request_last(rq, 0);
153                 spin_unlock_irqrestore(q->queue_lock, flags);
154
155                 mbox->rxq->callback((void *)msg);
156         }
157 }
158
159 /*
160  * Mailbox interrupt handler
161  */
162 static void mbox_txq_fn(request_queue_t * q)
163 {
164 }
165
166 static void mbox_rxq_fn(request_queue_t * q)
167 {
168 }
169
170 static 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
177 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
178 {
179         struct request *rq;
180         mbox_msg_t msg;
181         request_queue_t *q = mbox->rxq->queue;
182
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
211 static irqreturn_t mbox_interrupt(int irq, void *p)
212 {
213         struct omap_mbox *mbox = (struct omap_mbox *)p;
214
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  */
227 static ssize_t
228 omap_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
245 static ssize_t
246 omap_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
264                 spin_lock_irqsave(q->queue_lock, flags);
265                 blkdev_dequeue_request(rq);
266                 end_that_request_last(rq, 0);
267                 spin_unlock_irqrestore(q->queue_lock, flags);
268
269                 if (unlikely(mbox_seq_test(mbox, *p))) {
270                         pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
271                         continue;
272                 }
273                 p++;
274         }
275
276         pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
277
278         return (size_t) ((char *)p - buf);
279 }
280
281 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
282
283 static ssize_t mbox_show(struct class *class, char *buf)
284 {
285         return sprintf(buf, "mbox");
286 }
287
288 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
289
290 static struct class omap_mbox_class = {
291         .name = "omap_mbox",
292 };
293
294 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
295                                         request_fn_proc * proc,
296                                         void (*work) (struct work_struct *))
297 {
298         request_queue_t *q;
299         struct omap_mbox_queue *mq;
300
301         mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
302         if (!mq)
303                 return NULL;
304
305         spin_lock_init(&mq->lock);
306
307         q = blk_init_queue(proc, &mq->lock);
308         if (!q)
309                 goto error;
310         q->queuedata = mbox;
311         mq->queue = q;
312
313         INIT_WORK(&mq->work, work);
314
315         return mq;
316 error:
317         kfree(mq);
318         return NULL;
319 }
320
321 static void mbox_queue_free(struct omap_mbox_queue *q)
322 {
323         blk_cleanup_queue(q->queue);
324         kfree(q);
325 }
326
327 static int omap_mbox_init(struct omap_mbox *mbox)
328 {
329         int ret;
330         struct omap_mbox_queue *mq;
331
332         if (likely(mbox->ops->startup)) {
333                 ret = mbox->ops->startup(mbox);
334                 if (unlikely(ret))
335                         return ret;
336         }
337
338         mbox->dev.class = &omap_mbox_class;
339         strlcpy(mbox->dev.bus_id, mbox->name, KOBJ_NAME_LEN);
340         dev_set_drvdata(&mbox->dev, mbox);
341
342         ret = device_register(&mbox->dev);
343         if (unlikely(ret))
344                 goto fail_device_reg;
345
346         ret = device_create_file(&mbox->dev, &dev_attr_mbox);
347         if (unlikely(ret)) {
348                 printk(KERN_ERR
349                        "device_create_file failed: %d\n", ret);
350                 goto fail_create_mbox;
351         }
352
353         ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
354                           mbox->name, mbox);
355         if (unlikely(ret)) {
356                 printk(KERN_ERR
357                        "failed to register mailbox interrupt:%d\n", ret);
358                 goto fail_request_irq;
359         }
360         enable_mbox_irq(mbox, IRQ_RX);
361
362         mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
363         if (!mq) {
364                 ret = -ENOMEM;
365                 goto fail_alloc_txq;
366         }
367         mbox->txq = mq;
368
369         mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
370         if (!mq) {
371                 ret = -ENOMEM;
372                 goto fail_alloc_rxq;
373         }
374         mbox->rxq = mq;
375
376         return 0;
377
378  fail_alloc_rxq:
379         mbox_queue_free(mbox->txq);
380  fail_alloc_txq:
381         free_irq(mbox->irq, mbox);
382  fail_request_irq:
383         device_remove_file(&mbox->dev, &dev_attr_mbox);
384  fail_create_mbox:
385         device_unregister(&mbox->dev);
386  fail_device_reg:
387         if (unlikely(mbox->ops->shutdown))
388                 mbox->ops->shutdown(mbox);
389
390         return ret;
391 }
392
393 static void omap_mbox_fini(struct omap_mbox *mbox)
394 {
395         mbox_queue_free(mbox->txq);
396         mbox_queue_free(mbox->rxq);
397
398         free_irq(mbox->irq, mbox);
399         device_remove_file(&mbox->dev, &dev_attr_mbox);
400         class_unregister(&omap_mbox_class);
401
402         if (unlikely(mbox->ops->shutdown))
403                 mbox->ops->shutdown(mbox);
404 }
405
406 static struct omap_mbox **find_mboxes(const char *name)
407 {
408         struct omap_mbox **p;
409
410         for (p = &mboxes; *p; p = &(*p)->next) {
411                 if (strcmp((*p)->name, name) == 0)
412                         break;
413         }
414
415         return p;
416 }
417
418 struct omap_mbox *omap_mbox_get(const char *name)
419 {
420         struct omap_mbox *mbox;
421         int ret;
422
423         read_lock(&mboxes_lock);
424         mbox = *(find_mboxes(name));
425         if (mbox == NULL) {
426                 read_unlock(&mboxes_lock);
427                 return ERR_PTR(-ENOENT);
428         }
429
430         read_unlock(&mboxes_lock);
431
432         ret = omap_mbox_init(mbox);
433         if (ret)
434                 return ERR_PTR(-ENODEV);
435
436         return mbox;
437 }
438 EXPORT_SYMBOL(omap_mbox_get);
439
440 void omap_mbox_put(struct omap_mbox *mbox)
441 {
442         omap_mbox_fini(mbox);
443 }
444 EXPORT_SYMBOL(omap_mbox_put);
445
446 int omap_mbox_register(struct omap_mbox *mbox)
447 {
448         int ret = 0;
449         struct omap_mbox **tmp;
450
451         if (!mbox)
452                 return -EINVAL;
453         if (mbox->next)
454                 return -EBUSY;
455
456         write_lock(&mboxes_lock);
457         tmp = find_mboxes(mbox->name);
458         if (*tmp)
459                 ret = -EBUSY;
460         else
461                 *tmp = mbox;
462         write_unlock(&mboxes_lock);
463
464         return ret;
465 }
466 EXPORT_SYMBOL(omap_mbox_register);
467
468 int omap_mbox_unregister(struct omap_mbox *mbox)
469 {
470         struct omap_mbox **tmp;
471
472         write_lock(&mboxes_lock);
473         tmp = &mboxes;
474         while (*tmp) {
475                 if (mbox == *tmp) {
476                         *tmp = mbox->next;
477                         mbox->next = NULL;
478                         write_unlock(&mboxes_lock);
479                         return 0;
480                 }
481                 tmp = &(*tmp)->next;
482         }
483         write_unlock(&mboxes_lock);
484
485         return -EINVAL;
486 }
487 EXPORT_SYMBOL(omap_mbox_unregister);
488
489 static int __init omap_mbox_class_init(void)
490 {
491         int ret = class_register(&omap_mbox_class);
492         if (!ret)
493                 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
494
495         return ret;
496 }
497
498 static void __exit omap_mbox_class_exit(void)
499 {
500         class_remove_file(&omap_mbox_class, &class_attr_mbox);
501         class_unregister(&omap_mbox_class);
502 }
503
504 subsys_initcall(omap_mbox_class_init);
505 module_exit(omap_mbox_class_exit);
506
507 MODULE_LICENSE("GPL");