4 * Copyright (C) 2006 Nokia Corporation. All rights reserved.
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7 * Restructured by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
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.
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.
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
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>
34 #include <asm/arch/mailbox.h>
37 static struct omap_mbox *mboxes;
38 static DEFINE_RWLOCK(mboxes_lock);
40 /* Mailbox Sequence Bit function */
41 void omap_mbox_init_seq(struct omap_mbox *mbox)
45 EXPORT_SYMBOL(omap_mbox_init_seq);
50 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
52 int ret = 0, i = 1000;
54 while (mbox_fifo_full(mbox)) {
55 if (mbox->ops->type == OMAP_MBOX_TYPE2)
62 if (mbox->msg_sender_cb && arg) {
63 ret = mbox->msg_sender_cb(arg);
68 mbox_seq_toggle(mbox, &msg);
69 mbox_fifo_write(mbox, msg);
74 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
79 rq = blk_get_request(mbox->txq, WRITE, GFP_ATOMIC);
85 rq->data = (void *)msg;
86 blk_insert_request(mbox->txq, rq, 0, arg);
90 EXPORT_SYMBOL(omap_mbox_msg_send);
93 * Message receiver(workqueue)
95 static void mbox_msg_receiver(struct work_struct *work)
97 struct omap_mbox *mbox =
98 container_of(work, struct omap_mbox, msg_receive);
99 struct request_queue *q = mbox->rxq;
103 if (mbox->msg_receive_cb == NULL) {
104 sysfs_notify(&mbox->dev.kobj, NULL, "mbox");
109 rq = elv_next_request(q);
113 msg = (mbox_msg_t)rq->data;
115 blkdev_dequeue_request(rq);
116 end_that_request_last(rq, 0);
118 if (mbox->msg_receive_cb)
119 mbox->msg_receive_cb(msg);
124 * Mailbox interrupt handler
126 static void mbox_txq_fn(request_queue_t *q)
128 struct omap_mbox *mbox = q->queuedata;
132 while ((rq = elv_next_request(q)) != NULL) {
133 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->data, rq->special);
136 enable_mbox_irq(mbox, IRQ_TX);
139 blkdev_dequeue_request(rq);
140 end_that_request_last(rq, 0);
144 static void mbox_rxq_fn(request_queue_t *q)
147 struct omap_mbox *mbox = q->queuedata;
150 while (!mbox_fifo_empty(mbox)) {
151 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
155 msg = mbox_fifo_read(mbox);
156 rq->data = (void *)msg;
158 if (unlikely(mbox_seq_test(mbox, msg))) {
159 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
160 if (mbox->err_notify)
164 blk_insert_request(mbox->rxq, rq, 0, NULL);
165 if (mbox->ops->type == OMAP_MBOX_TYPE1)
169 /* no more messages in the fifo. clear IRQ source. */
170 ack_mbox_irq(mbox, IRQ_RX);
171 enable_mbox_irq(mbox, IRQ_RX);
173 queue_work(mbox->workq, &mbox->msg_receive);
176 static irqreturn_t mbox_interrupt(int irq, void *p)
178 struct omap_mbox *mbox = (struct omap_mbox *)p;
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);
186 if (is_mbox_irq(mbox, IRQ_RX)) {
187 disable_mbox_irq(mbox, IRQ_RX);
188 blk_start_queue(mbox->rxq);
198 omap_mbox_write(struct device *dev, struct device_attribute *attr,
199 const char * buf, size_t count)
202 mbox_msg_t *p = (mbox_msg_t *)buf;
203 struct omap_mbox *mbox = dev_get_drvdata(dev);
205 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
206 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
212 return (size_t)((char *)p - buf);
216 omap_mbox_read(struct device *dev, struct device_attribute *attr,
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;
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);
229 if (unlikely(mbox_seq_test(mbox, *p))) {
230 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
236 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
238 return (size_t)((char *)p - buf);
241 static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
243 static ssize_t mbox_show(struct class *class, char *buf)
245 return sprintf(buf, "mbox");
248 static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
250 static struct class omap_mbox_class = {
254 static int omap_mbox_init(struct omap_mbox *mbox)
259 if (likely(mbox->ops->startup)) {
260 ret = mbox->ops->startup(mbox);
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);
269 ret = device_register(&mbox->dev);
271 goto fail_device_reg;
273 ret = device_create_file(&mbox->dev, &dev_attr_mbox);
276 "device_create_file failed: %d\n", ret);
277 goto fail_create_mbox;
280 spin_lock_init(&mbox->lock);
281 INIT_WORK(&mbox->msg_receive, mbox_msg_receiver);
283 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
287 "failed to register mailbox interrupt:%d\n", ret);
288 goto fail_request_irq;
290 enable_mbox_irq(mbox, IRQ_RX);
292 q = blk_init_queue(mbox_txq_fn, &mbox->lock);
294 goto fail_blkinit_txq;
298 q = blk_init_queue(mbox_rxq_fn, &mbox->lock);
300 goto fail_blkinit_rxq;
307 blk_cleanup_queue(mbox->txq);
309 free_irq(mbox->irq, mbox);
311 device_remove_file(&mbox->dev, &dev_attr_mbox);
313 device_unregister(&mbox->dev);
315 if (unlikely(mbox->ops->shutdown))
316 mbox->ops->shutdown(mbox);
321 static void omap_mbox_fini(struct omap_mbox *mbox)
323 blk_cleanup_queue(mbox->txq);
324 blk_cleanup_queue(mbox->rxq);
326 flush_workqueue(mbox->workq);
327 destroy_workqueue(mbox->workq);
329 free_irq(mbox->irq, mbox);
330 device_remove_file(&mbox->dev, &dev_attr_mbox);
331 class_unregister(&omap_mbox_class);
333 if (unlikely(mbox->ops->shutdown))
334 mbox->ops->shutdown(mbox);
337 static struct omap_mbox **find_mboxes(const char *name)
339 struct omap_mbox **p;
341 for (p = &mboxes; *p; p = &(*p)->next) {
342 if (strcmp((*p)->name, name) == 0)
349 struct omap_mbox *omap_mbox_get(const char *name)
351 struct omap_mbox *mbox;
352 char queue_name[MBOX_NAME_LEN];
355 read_lock(&mboxes_lock);
356 mbox = *(find_mboxes(name));
358 read_unlock(&mboxes_lock);
359 return ERR_PTR(-ENOENT);
362 strcpy(queue_name, "mailbox/");
363 strcat(queue_name, mbox->name);
364 mbox->workq = create_singlethread_workqueue(queue_name);
366 read_unlock(&mboxes_lock);
368 ret = omap_mbox_init(mbox);
370 return ERR_PTR(-ENODEV);
374 EXPORT_SYMBOL(omap_mbox_get);
376 void omap_mbox_put(struct omap_mbox *mbox)
378 omap_mbox_fini(mbox);
380 EXPORT_SYMBOL(omap_mbox_put);
382 int omap_mbox_register(struct omap_mbox *mbox)
385 struct omap_mbox **tmp;
392 write_lock(&mboxes_lock);
393 tmp = find_mboxes(mbox->name);
398 write_unlock(&mboxes_lock);
402 EXPORT_SYMBOL(omap_mbox_register);
404 int omap_mbox_unregister(struct omap_mbox *mbox)
406 struct omap_mbox **tmp;
408 write_lock(&mboxes_lock);
414 write_unlock(&mboxes_lock);
419 write_unlock(&mboxes_lock);
423 EXPORT_SYMBOL(omap_mbox_unregister);
425 static int __init omap_mbox_class_init(void)
427 int ret = class_register(&omap_mbox_class);
429 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
434 static void __exit omap_mbox_class_exit(void)
436 class_remove_file(&omap_mbox_class, &class_attr_mbox);
437 class_unregister(&omap_mbox_class);
440 subsys_initcall(omap_mbox_class_init);
441 module_exit(omap_mbox_class_exit);
443 MODULE_LICENSE("GPL");