]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm/plat-omap/mailbox.c
omap mailbox: add save_/restore_ctx() for PM
[mirror_ubuntu-artful-kernel.git] / arch / arm / plat-omap / mailbox.c
CommitLineData
340a614a
HD
1/*
2 * OMAP mailbox driver
3 *
f48cca87 4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
340a614a 5 *
f48cca87 6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
340a614a
HD
7 *
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
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/sched.h>
27#include <linux/interrupt.h>
28#include <linux/device.h>
29#include <linux/blkdev.h>
30#include <linux/err.h>
31#include <linux/delay.h>
fced80c7 32#include <linux/io.h>
a09e64fb 33#include <mach/mailbox.h>
340a614a
HD
34#include "mailbox.h"
35
36static struct omap_mbox *mboxes;
37static DEFINE_RWLOCK(mboxes_lock);
38
39/* Mailbox Sequence Bit function */
40void omap_mbox_init_seq(struct omap_mbox *mbox)
41{
42 mbox_seq_init(mbox);
43}
44EXPORT_SYMBOL(omap_mbox_init_seq);
45
46/*
47 * message sender
48 */
49static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
50{
51 int ret = 0, i = 1000;
52
53 while (mbox_fifo_full(mbox)) {
54 if (mbox->ops->type == OMAP_MBOX_TYPE2)
55 return -1;
56 if (--i == 0)
57 return -1;
58 udelay(1);
59 }
60
61 if (arg && mbox->txq->callback) {
62 ret = mbox->txq->callback(arg);
63 if (ret)
64 goto out;
65 }
66
67 mbox_seq_toggle(mbox, &msg);
68 mbox_fifo_write(mbox, msg);
69 out:
70 return ret;
71}
72
73int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
74{
75 struct request *rq;
76 struct request_queue *q = mbox->txq->queue;
77 int ret = 0;
78
79 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
80 if (unlikely(!rq)) {
81 ret = -ENOMEM;
82 goto fail;
83 }
84
85 rq->data = (void *)msg;
86 blk_insert_request(q, rq, 0, arg);
87
88 schedule_work(&mbox->txq->work);
89 fail:
90 return ret;
91}
92EXPORT_SYMBOL(omap_mbox_msg_send);
93
94static void mbox_tx_work(struct work_struct *work)
95{
96 int ret;
97 struct request *rq;
98 struct omap_mbox_queue *mq = container_of(work,
99 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);
650e9cfd
KU
118 if (__blk_end_request(rq, 0, 0))
119 BUG();
340a614a
HD
120 spin_unlock(q->queue_lock);
121 }
122}
123
124/*
125 * Message receiver(workqueue)
126 */
127static void mbox_rx_work(struct work_struct *work)
128{
129 struct omap_mbox_queue *mq =
130 container_of(work, struct omap_mbox_queue, work);
131 struct omap_mbox *mbox = mq->queue->queuedata;
132 struct request_queue *q = mbox->rxq->queue;
133 struct request *rq;
134 mbox_msg_t msg;
135 unsigned long flags;
136
137 if (mbox->rxq->callback == NULL) {
f48cca87 138 sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
340a614a
HD
139 return;
140 }
141
142 while (1) {
143 spin_lock_irqsave(q->queue_lock, flags);
144 rq = elv_next_request(q);
145 spin_unlock_irqrestore(q->queue_lock, flags);
146 if (!rq)
147 break;
148
149 msg = (mbox_msg_t) rq->data;
150
650e9cfd
KU
151 if (blk_end_request(rq, 0, 0))
152 BUG();
340a614a
HD
153
154 mbox->rxq->callback((void *)msg);
155 }
156}
157
158/*
159 * Mailbox interrupt handler
160 */
165125e1 161static void mbox_txq_fn(struct request_queue * q)
340a614a
HD
162{
163}
164
165125e1 165static void mbox_rxq_fn(struct request_queue * q)
340a614a
HD
166{
167}
168
169static void __mbox_tx_interrupt(struct omap_mbox *mbox)
170{
171 disable_mbox_irq(mbox, IRQ_TX);
172 ack_mbox_irq(mbox, IRQ_TX);
173 schedule_work(&mbox->txq->work);
174}
175
176static void __mbox_rx_interrupt(struct omap_mbox *mbox)
177{
178 struct request *rq;
179 mbox_msg_t msg;
165125e1 180 struct request_queue *q = mbox->rxq->queue;
340a614a
HD
181
182 disable_mbox_irq(mbox, IRQ_RX);
183
184 while (!mbox_fifo_empty(mbox)) {
185 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
186 if (unlikely(!rq))
187 goto nomem;
188
189 msg = mbox_fifo_read(mbox);
190 rq->data = (void *)msg;
191
192 if (unlikely(mbox_seq_test(mbox, msg))) {
193 pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
194 if (mbox->err_notify)
195 mbox->err_notify();
196 }
197
198 blk_insert_request(q, rq, 0, NULL);
199 if (mbox->ops->type == OMAP_MBOX_TYPE1)
200 break;
201 }
202
203 /* no more messages in the fifo. clear IRQ source. */
204 ack_mbox_irq(mbox, IRQ_RX);
205 enable_mbox_irq(mbox, IRQ_RX);
f48cca87 206nomem:
340a614a
HD
207 schedule_work(&mbox->rxq->work);
208}
209
210static irqreturn_t mbox_interrupt(int irq, void *p)
211{
2a7057e3 212 struct omap_mbox *mbox = p;
340a614a
HD
213
214 if (is_mbox_irq(mbox, IRQ_TX))
215 __mbox_tx_interrupt(mbox);
216
217 if (is_mbox_irq(mbox, IRQ_RX))
218 __mbox_rx_interrupt(mbox);
219
220 return IRQ_HANDLED;
221}
222
223/*
224 * sysfs files
225 */
226static ssize_t
227omap_mbox_write(struct device *dev, struct device_attribute *attr,
228 const char * buf, size_t count)
229{
230 int ret;
231 mbox_msg_t *p = (mbox_msg_t *)buf;
232 struct omap_mbox *mbox = dev_get_drvdata(dev);
233
234 for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
235 ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
236 if (ret)
237 return -EAGAIN;
238 p++;
239 }
240
241 return (size_t)((char *)p - buf);
242}
243
244static ssize_t
245omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
246{
247 unsigned long flags;
248 struct request *rq;
249 mbox_msg_t *p = (mbox_msg_t *) buf;
250 struct omap_mbox *mbox = dev_get_drvdata(dev);
251 struct request_queue *q = mbox->rxq->queue;
252
253 while (1) {
254 spin_lock_irqsave(q->queue_lock, flags);
255 rq = elv_next_request(q);
256 spin_unlock_irqrestore(q->queue_lock, flags);
257
258 if (!rq)
259 break;
260
261 *p = (mbox_msg_t) rq->data;
262
650e9cfd
KU
263 if (blk_end_request(rq, 0, 0))
264 BUG();
340a614a
HD
265
266 if (unlikely(mbox_seq_test(mbox, *p))) {
267 pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
268 continue;
269 }
270 p++;
271 }
272
273 pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
274
275 return (size_t) ((char *)p - buf);
276}
277
278static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
279
280static ssize_t mbox_show(struct class *class, char *buf)
281{
282 return sprintf(buf, "mbox");
283}
284
285static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
286
287static struct class omap_mbox_class = {
f48cca87 288 .name = "omap-mailbox",
340a614a
HD
289};
290
291static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
292 request_fn_proc * proc,
293 void (*work) (struct work_struct *))
294{
165125e1 295 struct request_queue *q;
340a614a
HD
296 struct omap_mbox_queue *mq;
297
298 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
299 if (!mq)
300 return NULL;
301
302 spin_lock_init(&mq->lock);
303
304 q = blk_init_queue(proc, &mq->lock);
305 if (!q)
306 goto error;
307 q->queuedata = mbox;
308 mq->queue = q;
309
310 INIT_WORK(&mq->work, work);
311
312 return mq;
313error:
314 kfree(mq);
315 return NULL;
316}
317
318static void mbox_queue_free(struct omap_mbox_queue *q)
319{
320 blk_cleanup_queue(q->queue);
321 kfree(q);
322}
323
324static int omap_mbox_init(struct omap_mbox *mbox)
325{
326 int ret;
327 struct omap_mbox_queue *mq;
328
329 if (likely(mbox->ops->startup)) {
330 ret = mbox->ops->startup(mbox);
331 if (unlikely(ret))
332 return ret;
333 }
334
340a614a
HD
335 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
336 mbox->name, mbox);
337 if (unlikely(ret)) {
338 printk(KERN_ERR
339 "failed to register mailbox interrupt:%d\n", ret);
340 goto fail_request_irq;
341 }
340a614a
HD
342
343 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
344 if (!mq) {
345 ret = -ENOMEM;
346 goto fail_alloc_txq;
347 }
348 mbox->txq = mq;
349
350 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
351 if (!mq) {
352 ret = -ENOMEM;
353 goto fail_alloc_rxq;
354 }
355 mbox->rxq = mq;
356
357 return 0;
358
359 fail_alloc_rxq:
360 mbox_queue_free(mbox->txq);
361 fail_alloc_txq:
362 free_irq(mbox->irq, mbox);
363 fail_request_irq:
340a614a
HD
364 if (unlikely(mbox->ops->shutdown))
365 mbox->ops->shutdown(mbox);
366
367 return ret;
368}
369
370static void omap_mbox_fini(struct omap_mbox *mbox)
371{
372 mbox_queue_free(mbox->txq);
373 mbox_queue_free(mbox->rxq);
374
375 free_irq(mbox->irq, mbox);
340a614a
HD
376
377 if (unlikely(mbox->ops->shutdown))
378 mbox->ops->shutdown(mbox);
379}
380
381static struct omap_mbox **find_mboxes(const char *name)
382{
383 struct omap_mbox **p;
384
385 for (p = &mboxes; *p; p = &(*p)->next) {
386 if (strcmp((*p)->name, name) == 0)
387 break;
388 }
389
390 return p;
391}
392
393struct omap_mbox *omap_mbox_get(const char *name)
394{
395 struct omap_mbox *mbox;
396 int ret;
397
398 read_lock(&mboxes_lock);
399 mbox = *(find_mboxes(name));
400 if (mbox == NULL) {
401 read_unlock(&mboxes_lock);
402 return ERR_PTR(-ENOENT);
403 }
404
405 read_unlock(&mboxes_lock);
406
407 ret = omap_mbox_init(mbox);
408 if (ret)
409 return ERR_PTR(-ENODEV);
410
411 return mbox;
412}
413EXPORT_SYMBOL(omap_mbox_get);
414
415void omap_mbox_put(struct omap_mbox *mbox)
416{
417 omap_mbox_fini(mbox);
418}
419EXPORT_SYMBOL(omap_mbox_put);
420
f48cca87 421int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
340a614a
HD
422{
423 int ret = 0;
424 struct omap_mbox **tmp;
425
426 if (!mbox)
427 return -EINVAL;
428 if (mbox->next)
429 return -EBUSY;
430
f48cca87
HD
431 mbox->dev = device_create(&omap_mbox_class,
432 parent, 0, mbox, "%s", mbox->name);
433 if (IS_ERR(mbox->dev))
434 return PTR_ERR(mbox->dev);
435
436 ret = device_create_file(mbox->dev, &dev_attr_mbox);
437 if (ret)
438 goto err_sysfs;
439
340a614a
HD
440 write_lock(&mboxes_lock);
441 tmp = find_mboxes(mbox->name);
f48cca87 442 if (*tmp) {
340a614a 443 ret = -EBUSY;
f48cca87
HD
444 write_unlock(&mboxes_lock);
445 goto err_find;
446 }
447 *tmp = mbox;
340a614a
HD
448 write_unlock(&mboxes_lock);
449
f48cca87
HD
450 return 0;
451
452err_find:
453 device_remove_file(mbox->dev, &dev_attr_mbox);
454err_sysfs:
455 device_unregister(mbox->dev);
340a614a
HD
456 return ret;
457}
458EXPORT_SYMBOL(omap_mbox_register);
459
460int omap_mbox_unregister(struct omap_mbox *mbox)
461{
462 struct omap_mbox **tmp;
463
464 write_lock(&mboxes_lock);
465 tmp = &mboxes;
466 while (*tmp) {
467 if (mbox == *tmp) {
468 *tmp = mbox->next;
469 mbox->next = NULL;
470 write_unlock(&mboxes_lock);
f48cca87
HD
471 device_remove_file(mbox->dev, &dev_attr_mbox);
472 device_unregister(mbox->dev);
340a614a
HD
473 return 0;
474 }
475 tmp = &(*tmp)->next;
476 }
477 write_unlock(&mboxes_lock);
478
479 return -EINVAL;
480}
481EXPORT_SYMBOL(omap_mbox_unregister);
482
483static int __init omap_mbox_class_init(void)
484{
485 int ret = class_register(&omap_mbox_class);
486 if (!ret)
487 ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
488
489 return ret;
490}
491
492static void __exit omap_mbox_class_exit(void)
493{
494 class_remove_file(&omap_mbox_class, &class_attr_mbox);
495 class_unregister(&omap_mbox_class);
496}
497
498subsys_initcall(omap_mbox_class_init);
499module_exit(omap_mbox_class_exit);
500
f48cca87
HD
501MODULE_LICENSE("GPL v2");
502MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
503MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");