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