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