]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/mailbox.c
ARM:OMAP: Integrated blk request queues for mbox fwk
[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 (mbox->msg_sender_cb && arg) {
63                 ret = mbox->msg_sender_cb(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         int ret = 0;
78
79         rq = blk_get_request(mbox->txq, WRITE, GFP_ATOMIC);
80         if (unlikely(!rq)) {
81                 ret = -ENOMEM;
82                 goto fail;
83         }
84
85         rq->data = (void *)msg;
86         blk_insert_request(mbox->txq, rq, 0, arg);
87  fail:
88         return ret;
89 }
90 EXPORT_SYMBOL(omap_mbox_msg_send);
91
92 /*
93  * Message receiver(workqueue)
94  */
95 static void mbox_msg_receiver(struct work_struct *work)
96 {
97         struct omap_mbox *mbox =
98             container_of(work, struct omap_mbox, msg_receive);
99         struct request_queue *q = mbox->rxq;
100         struct request *rq;
101         mbox_msg_t msg;
102
103         if (mbox->msg_receive_cb == NULL) {
104                 sysfs_notify(&mbox->dev.kobj, NULL, "mbox");
105                 return;
106         }
107
108         while (1) {
109                 rq = elv_next_request(q);
110                 if (!rq)
111                         break;
112
113                 msg = (mbox_msg_t)rq->data;
114
115                 blkdev_dequeue_request(rq);
116                 end_that_request_last(rq, 0);
117
118                 if (mbox->msg_receive_cb)
119                         mbox->msg_receive_cb(msg);
120         }
121 }
122
123 /*
124  * Mailbox interrupt handler
125  */
126 static void mbox_txq_fn(request_queue_t *q)
127 {
128         struct omap_mbox *mbox = q->queuedata;
129         int ret;
130         struct request *rq;
131
132         while ((rq = elv_next_request(q)) != NULL) {
133                 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->data, rq->special);
134                 if (ret) {
135                         blk_stop_queue(q);
136                         enable_mbox_irq(mbox, IRQ_TX);
137                         return;
138                 }
139                 blkdev_dequeue_request(rq);
140                 end_that_request_last(rq, 0);
141         }
142 }
143
144 static void mbox_rxq_fn(request_queue_t *q)
145 {
146         struct request *rq;
147         struct omap_mbox *mbox = q->queuedata;
148         mbox_msg_t msg;
149
150         while (!mbox_fifo_empty(mbox)) {
151                 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
152                 if (unlikely(!rq))
153                         goto nomem;
154
155                 msg = mbox_fifo_read(mbox);
156                 rq->data = (void *)msg;
157
158                 if (unlikely(mbox_seq_test(mbox, msg))) {
159                         pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
160                         if (mbox->err_notify)
161                                 mbox->err_notify();
162                 }
163
164                 blk_insert_request(mbox->rxq, rq, 0, NULL);
165                 if (mbox->ops->type == OMAP_MBOX_TYPE1)
166                         break;
167         }
168
169         /* no more messages in the fifo. clear IRQ source. */
170         ack_mbox_irq(mbox, IRQ_RX);
171         enable_mbox_irq(mbox, IRQ_RX);
172  nomem:
173         queue_work(mbox->workq, &mbox->msg_receive);
174 }
175
176 static irqreturn_t mbox_interrupt(int irq, void *p)
177 {
178         struct omap_mbox *mbox = (struct omap_mbox *)p;
179
180         if (is_mbox_irq(mbox, IRQ_TX)) {
181                 disable_mbox_irq(mbox, IRQ_TX);
182                 ack_mbox_irq(mbox, IRQ_TX);
183                 blk_start_queue(mbox->txq);
184         }
185
186         if (is_mbox_irq(mbox, IRQ_RX)) {
187                 disable_mbox_irq(mbox, IRQ_RX);
188                 blk_start_queue(mbox->rxq);
189         }
190
191         return IRQ_HANDLED;
192 }
193
194 /*
195  * sysfs files
196  */
197 static ssize_t
198 omap_mbox_write(struct device *dev, struct device_attribute *attr,
199                 const char * buf, size_t count)
200 {
201         int ret;
202         mbox_msg_t *p = (mbox_msg_t *)buf;
203         struct omap_mbox *mbox = dev_get_drvdata(dev);
204
205         for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
206                 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
207                 if (ret)
208                         return -EAGAIN;
209                 p++;
210         }
211
212         return  (size_t)((char *)p - buf);
213 }
214
215 static ssize_t
216 omap_mbox_read(struct device *dev, struct device_attribute *attr,
217                char * buf)
218 {
219         struct request *rq;
220         mbox_msg_t *p = (mbox_msg_t *)buf;
221         struct omap_mbox *mbox = dev_get_drvdata(dev);
222         struct request_queue *q = mbox->rxq;
223
224         while ((rq = elv_next_request(q)) != NULL) {
225                 *p = (mbox_msg_t)rq->data;
226                 blkdev_dequeue_request(rq);
227                 end_that_request_last(rq, 0);
228
229                 if (unlikely(mbox_seq_test(mbox, *p))) {
230                         pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
231                         continue;
232                 }
233                 p++;
234         }
235
236         pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
237
238         return (size_t)((char *)p - buf);
239 }
240
241 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
242
243 static ssize_t mbox_show(struct class *class, char *buf)
244 {
245         return sprintf(buf, "mbox");
246 }
247
248 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
249
250 static struct class omap_mbox_class = {
251         .name = "omap_mbox",
252 };
253
254 static int omap_mbox_init(struct omap_mbox *mbox)
255 {
256         int ret;
257         request_queue_t *q;
258
259         if (likely(mbox->ops->startup)) {
260                 ret = mbox->ops->startup(mbox);
261                 if (unlikely(ret))
262                         return ret;
263         }
264
265         mbox->dev.class = &omap_mbox_class;
266         strlcpy(mbox->dev.bus_id, mbox->name, KOBJ_NAME_LEN);
267         dev_set_drvdata(&mbox->dev, mbox);
268
269         ret = device_register(&mbox->dev);
270         if (unlikely(ret))
271                 goto fail_device_reg;
272
273         ret = device_create_file(&mbox->dev, &dev_attr_mbox);
274         if (unlikely(ret)) {
275                 printk(KERN_ERR
276                        "device_create_file failed: %d\n", ret);
277                 goto fail_create_mbox;
278         }
279
280         spin_lock_init(&mbox->lock);
281         INIT_WORK(&mbox->msg_receive, mbox_msg_receiver);
282
283         ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
284                           mbox->name, mbox);
285         if (unlikely(ret)) {
286                 printk(KERN_ERR
287                        "failed to register mailbox interrupt:%d\n", ret);
288                 goto fail_request_irq;
289         }
290         enable_mbox_irq(mbox, IRQ_RX);
291
292         q = blk_init_queue(mbox_txq_fn, &mbox->lock);
293         if (!q)
294                 goto fail_blkinit_txq;
295         mbox->txq = q;
296         q->queuedata = mbox;
297
298         q = blk_init_queue(mbox_rxq_fn, &mbox->lock);
299         if (!q)
300                 goto fail_blkinit_rxq;
301         mbox->rxq = q;
302         q->queuedata = mbox;
303
304         return 0;
305
306  fail_blkinit_rxq:
307         blk_cleanup_queue(mbox->txq);
308  fail_blkinit_txq:
309         free_irq(mbox->irq, mbox);
310  fail_request_irq:
311         device_remove_file(&mbox->dev, &dev_attr_mbox);
312  fail_create_mbox:
313         device_unregister(&mbox->dev);
314  fail_device_reg:
315         if (unlikely(mbox->ops->shutdown))
316                 mbox->ops->shutdown(mbox);
317
318         return ret;
319 }
320
321 static void omap_mbox_fini(struct omap_mbox *mbox)
322 {
323         blk_cleanup_queue(mbox->txq);
324         blk_cleanup_queue(mbox->rxq);
325
326         flush_workqueue(mbox->workq);
327         destroy_workqueue(mbox->workq);
328
329         free_irq(mbox->irq, mbox);
330         device_remove_file(&mbox->dev, &dev_attr_mbox);
331         class_unregister(&omap_mbox_class);
332
333         if (unlikely(mbox->ops->shutdown))
334                 mbox->ops->shutdown(mbox);
335 }
336
337 static struct omap_mbox **find_mboxes(const char *name)
338 {
339         struct omap_mbox **p;
340
341         for (p = &mboxes; *p; p = &(*p)->next) {
342                 if (strcmp((*p)->name, name) == 0)
343                         break;
344         }
345
346         return p;
347 }
348
349 struct omap_mbox *omap_mbox_get(const char *name)
350 {
351         struct omap_mbox *mbox;
352         char queue_name[MBOX_NAME_LEN];
353         int ret;
354
355         read_lock(&mboxes_lock);
356         mbox = *(find_mboxes(name));
357         if (mbox == NULL) {
358                 read_unlock(&mboxes_lock);
359                 return ERR_PTR(-ENOENT);
360         }
361
362         strcpy(queue_name, "mailbox/");
363         strcat(queue_name, mbox->name);
364         mbox->workq = create_singlethread_workqueue(queue_name);
365
366         read_unlock(&mboxes_lock);
367
368         ret = omap_mbox_init(mbox);
369         if (ret)
370                 return ERR_PTR(-ENODEV);
371
372         return mbox;
373 }
374 EXPORT_SYMBOL(omap_mbox_get);
375
376 void omap_mbox_put(struct omap_mbox *mbox)
377 {
378         omap_mbox_fini(mbox);
379 }
380 EXPORT_SYMBOL(omap_mbox_put);
381
382 int omap_mbox_register(struct omap_mbox *mbox)
383 {
384         int ret = 0;
385         struct omap_mbox **tmp;
386
387         if (!mbox)
388                 return -EINVAL;
389         if (mbox->next)
390                 return -EBUSY;
391
392         write_lock(&mboxes_lock);
393         tmp = find_mboxes(mbox->name);
394         if (*tmp)
395                 ret = -EBUSY;
396         else
397                 *tmp = mbox;
398         write_unlock(&mboxes_lock);
399
400         return ret;
401 }
402 EXPORT_SYMBOL(omap_mbox_register);
403
404 int omap_mbox_unregister(struct omap_mbox *mbox)
405 {
406         struct omap_mbox **tmp;
407
408         write_lock(&mboxes_lock);
409         tmp = &mboxes;
410         while (*tmp) {
411                 if (mbox == *tmp) {
412                         *tmp = mbox->next;
413                         mbox->next = NULL;
414                         write_unlock(&mboxes_lock);
415                         return 0;
416                 }
417                 tmp = &(*tmp)->next;
418         }
419         write_unlock(&mboxes_lock);
420
421         return -EINVAL;
422 }
423 EXPORT_SYMBOL(omap_mbox_unregister);
424
425 static int __init omap_mbox_class_init(void)
426 {
427         int ret = class_register(&omap_mbox_class);
428         if (!ret)
429                 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
430
431         return ret;
432 }
433
434 static void __exit omap_mbox_class_exit(void)
435 {
436         class_remove_file(&omap_mbox_class, &class_attr_mbox);
437         class_unregister(&omap_mbox_class);
438 }
439
440 subsys_initcall(omap_mbox_class_init);
441 module_exit(omap_mbox_class_exit);
442
443 MODULE_LICENSE("GPL");