]>
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/interrupt.h> |
b3e69146 FC |
25 | #include <linux/spinlock.h> |
26 | #include <linux/mutex.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
b5bebe41 OBC |
28 | #include <linux/kfifo.h> |
29 | #include <linux/err.h> | |
58256307 | 30 | #include <linux/notifier.h> |
73017a54 | 31 | #include <linux/module.h> |
8dff0fa5 | 32 | |
c869c75c | 33 | #include "omap-mbox.h" |
340a614a | 34 | |
9c80c8cd | 35 | static struct omap_mbox **mboxes; |
340a614a | 36 | |
5f00ec64 | 37 | static int mbox_configured; |
72b917ef | 38 | static DEFINE_MUTEX(mbox_configured_lock); |
5f00ec64 | 39 | |
b5bebe41 OBC |
40 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; |
41 | module_param(mbox_kfifo_size, uint, S_IRUGO); | |
42 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); | |
43 | ||
9ae0ee00 HD |
44 | /* Mailbox FIFO handle functions */ |
45 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) | |
46 | { | |
47 | return mbox->ops->fifo_read(mbox); | |
48 | } | |
49 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) | |
50 | { | |
51 | mbox->ops->fifo_write(mbox, msg); | |
52 | } | |
53 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) | |
54 | { | |
55 | return mbox->ops->fifo_empty(mbox); | |
56 | } | |
57 | static inline int mbox_fifo_full(struct omap_mbox *mbox) | |
58 | { | |
59 | return mbox->ops->fifo_full(mbox); | |
60 | } | |
61 | ||
62 | /* Mailbox IRQ handle functions */ | |
9ae0ee00 HD |
63 | static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
64 | { | |
65 | if (mbox->ops->ack_irq) | |
66 | mbox->ops->ack_irq(mbox, irq); | |
67 | } | |
68 | static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) | |
69 | { | |
70 | return mbox->ops->is_irq(mbox, irq); | |
71 | } | |
72 | ||
340a614a HD |
73 | /* |
74 | * message sender | |
75 | */ | |
b2b6362e | 76 | int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg) |
340a614a | 77 | { |
b5bebe41 OBC |
78 | struct omap_mbox_queue *mq = mbox->txq; |
79 | int ret = 0, len; | |
5ed8d32e | 80 | |
a42743c2 | 81 | spin_lock_bh(&mq->lock); |
ec24751a | 82 | |
b5bebe41 OBC |
83 | if (kfifo_avail(&mq->fifo) < sizeof(msg)) { |
84 | ret = -ENOMEM; | |
85 | goto out; | |
86 | } | |
87 | ||
fe714a46 | 88 | if (kfifo_is_empty(&mq->fifo) && !mbox_fifo_full(mbox)) { |
a42743c2 KH |
89 | mbox_fifo_write(mbox, msg); |
90 | goto out; | |
91 | } | |
92 | ||
b5bebe41 OBC |
93 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
94 | WARN_ON(len != sizeof(msg)); | |
340a614a | 95 | |
5ed8d32e | 96 | tasklet_schedule(&mbox->txq->tasklet); |
340a614a | 97 | |
b5bebe41 | 98 | out: |
a42743c2 | 99 | spin_unlock_bh(&mq->lock); |
b5bebe41 | 100 | return ret; |
340a614a HD |
101 | } |
102 | EXPORT_SYMBOL(omap_mbox_msg_send); | |
103 | ||
c869c75c SA |
104 | void omap_mbox_save_ctx(struct omap_mbox *mbox) |
105 | { | |
106 | if (!mbox->ops->save_ctx) { | |
107 | dev_err(mbox->dev, "%s:\tno save\n", __func__); | |
108 | return; | |
109 | } | |
110 | ||
111 | mbox->ops->save_ctx(mbox); | |
112 | } | |
113 | EXPORT_SYMBOL(omap_mbox_save_ctx); | |
114 | ||
115 | void omap_mbox_restore_ctx(struct omap_mbox *mbox) | |
116 | { | |
117 | if (!mbox->ops->restore_ctx) { | |
118 | dev_err(mbox->dev, "%s:\tno restore\n", __func__); | |
119 | return; | |
120 | } | |
121 | ||
122 | mbox->ops->restore_ctx(mbox); | |
123 | } | |
124 | EXPORT_SYMBOL(omap_mbox_restore_ctx); | |
125 | ||
126 | void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) | |
127 | { | |
128 | mbox->ops->enable_irq(mbox, irq); | |
129 | } | |
130 | EXPORT_SYMBOL(omap_mbox_enable_irq); | |
131 | ||
132 | void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) | |
133 | { | |
134 | mbox->ops->disable_irq(mbox, irq); | |
135 | } | |
136 | EXPORT_SYMBOL(omap_mbox_disable_irq); | |
137 | ||
5ed8d32e | 138 | static void mbox_tx_tasklet(unsigned long tx_data) |
340a614a | 139 | { |
5ed8d32e | 140 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
b5bebe41 OBC |
141 | struct omap_mbox_queue *mq = mbox->txq; |
142 | mbox_msg_t msg; | |
143 | int ret; | |
340a614a | 144 | |
b5bebe41 | 145 | while (kfifo_len(&mq->fifo)) { |
fe714a46 | 146 | if (mbox_fifo_full(mbox)) { |
eb18858e | 147 | omap_mbox_enable_irq(mbox, IRQ_TX); |
b5bebe41 | 148 | break; |
340a614a | 149 | } |
b5bebe41 OBC |
150 | |
151 | ret = kfifo_out(&mq->fifo, (unsigned char *)&msg, | |
152 | sizeof(msg)); | |
153 | WARN_ON(ret != sizeof(msg)); | |
154 | ||
155 | mbox_fifo_write(mbox, msg); | |
340a614a HD |
156 | } |
157 | } | |
158 | ||
159 | /* | |
160 | * Message receiver(workqueue) | |
161 | */ | |
162 | static void mbox_rx_work(struct work_struct *work) | |
163 | { | |
164 | struct omap_mbox_queue *mq = | |
165 | container_of(work, struct omap_mbox_queue, work); | |
340a614a | 166 | mbox_msg_t msg; |
b5bebe41 OBC |
167 | int len; |
168 | ||
169 | while (kfifo_len(&mq->fifo) >= sizeof(msg)) { | |
170 | len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); | |
171 | WARN_ON(len != sizeof(msg)); | |
340a614a | 172 | |
58256307 KH |
173 | blocking_notifier_call_chain(&mq->mbox->notifier, len, |
174 | (void *)msg); | |
d2295042 FGL |
175 | spin_lock_irq(&mq->lock); |
176 | if (mq->full) { | |
177 | mq->full = false; | |
178 | omap_mbox_enable_irq(mq->mbox, IRQ_RX); | |
179 | } | |
180 | spin_unlock_irq(&mq->lock); | |
340a614a HD |
181 | } |
182 | } | |
183 | ||
184 | /* | |
185 | * Mailbox interrupt handler | |
186 | */ | |
340a614a HD |
187 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
188 | { | |
eb18858e | 189 | omap_mbox_disable_irq(mbox, IRQ_TX); |
340a614a | 190 | ack_mbox_irq(mbox, IRQ_TX); |
5ed8d32e | 191 | tasklet_schedule(&mbox->txq->tasklet); |
340a614a HD |
192 | } |
193 | ||
194 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) | |
195 | { | |
b5bebe41 | 196 | struct omap_mbox_queue *mq = mbox->rxq; |
340a614a | 197 | mbox_msg_t msg; |
b5bebe41 | 198 | int len; |
340a614a | 199 | |
340a614a | 200 | while (!mbox_fifo_empty(mbox)) { |
b5bebe41 | 201 | if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { |
1ea5d6d1 | 202 | omap_mbox_disable_irq(mbox, IRQ_RX); |
d2295042 | 203 | mq->full = true; |
340a614a | 204 | goto nomem; |
1ea5d6d1 | 205 | } |
340a614a HD |
206 | |
207 | msg = mbox_fifo_read(mbox); | |
340a614a | 208 | |
b5bebe41 OBC |
209 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
210 | WARN_ON(len != sizeof(msg)); | |
340a614a HD |
211 | } |
212 | ||
213 | /* no more messages in the fifo. clear IRQ source. */ | |
214 | ack_mbox_irq(mbox, IRQ_RX); | |
f48cca87 | 215 | nomem: |
c4873005 | 216 | schedule_work(&mbox->rxq->work); |
340a614a HD |
217 | } |
218 | ||
219 | static irqreturn_t mbox_interrupt(int irq, void *p) | |
220 | { | |
2a7057e3 | 221 | struct omap_mbox *mbox = p; |
340a614a HD |
222 | |
223 | if (is_mbox_irq(mbox, IRQ_TX)) | |
224 | __mbox_tx_interrupt(mbox); | |
225 | ||
226 | if (is_mbox_irq(mbox, IRQ_RX)) | |
227 | __mbox_rx_interrupt(mbox); | |
228 | ||
229 | return IRQ_HANDLED; | |
230 | } | |
231 | ||
340a614a | 232 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
5ed8d32e S |
233 | void (*work) (struct work_struct *), |
234 | void (*tasklet)(unsigned long)) | |
340a614a | 235 | { |
340a614a HD |
236 | struct omap_mbox_queue *mq; |
237 | ||
238 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); | |
239 | if (!mq) | |
240 | return NULL; | |
241 | ||
242 | spin_lock_init(&mq->lock); | |
243 | ||
b5bebe41 | 244 | if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) |
340a614a | 245 | goto error; |
340a614a | 246 | |
5ed8d32e S |
247 | if (work) |
248 | INIT_WORK(&mq->work, work); | |
340a614a | 249 | |
5ed8d32e S |
250 | if (tasklet) |
251 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); | |
340a614a HD |
252 | return mq; |
253 | error: | |
254 | kfree(mq); | |
255 | return NULL; | |
256 | } | |
257 | ||
258 | static void mbox_queue_free(struct omap_mbox_queue *q) | |
259 | { | |
b5bebe41 | 260 | kfifo_free(&q->fifo); |
340a614a HD |
261 | kfree(q); |
262 | } | |
263 | ||
c7c158e5 | 264 | static int omap_mbox_startup(struct omap_mbox *mbox) |
340a614a | 265 | { |
5f00ec64 | 266 | int ret = 0; |
340a614a HD |
267 | struct omap_mbox_queue *mq; |
268 | ||
58256307 KH |
269 | mutex_lock(&mbox_configured_lock); |
270 | if (!mbox_configured++) { | |
271 | if (likely(mbox->ops->startup)) { | |
5f00ec64 | 272 | ret = mbox->ops->startup(mbox); |
58256307 KH |
273 | if (unlikely(ret)) |
274 | goto fail_startup; | |
275 | } else | |
276 | goto fail_startup; | |
340a614a HD |
277 | } |
278 | ||
58256307 | 279 | if (!mbox->use_count++) { |
58256307 KH |
280 | mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet); |
281 | if (!mq) { | |
282 | ret = -ENOMEM; | |
283 | goto fail_alloc_txq; | |
284 | } | |
285 | mbox->txq = mq; | |
340a614a | 286 | |
58256307 KH |
287 | mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL); |
288 | if (!mq) { | |
289 | ret = -ENOMEM; | |
290 | goto fail_alloc_rxq; | |
291 | } | |
292 | mbox->rxq = mq; | |
293 | mq->mbox = mbox; | |
ecf305cf SA |
294 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
295 | mbox->name, mbox); | |
296 | if (unlikely(ret)) { | |
297 | pr_err("failed to register mailbox interrupt:%d\n", | |
298 | ret); | |
299 | goto fail_request_irq; | |
300 | } | |
1d8a0e96 JG |
301 | |
302 | omap_mbox_enable_irq(mbox, IRQ_RX); | |
340a614a | 303 | } |
58256307 | 304 | mutex_unlock(&mbox_configured_lock); |
340a614a HD |
305 | return 0; |
306 | ||
ecf305cf SA |
307 | fail_request_irq: |
308 | mbox_queue_free(mbox->rxq); | |
ab66ac30 | 309 | fail_alloc_rxq: |
340a614a | 310 | mbox_queue_free(mbox->txq); |
ab66ac30 | 311 | fail_alloc_txq: |
01072d8f | 312 | if (mbox->ops->shutdown) |
340a614a | 313 | mbox->ops->shutdown(mbox); |
58256307 KH |
314 | mbox->use_count--; |
315 | fail_startup: | |
316 | mbox_configured--; | |
317 | mutex_unlock(&mbox_configured_lock); | |
340a614a HD |
318 | return ret; |
319 | } | |
320 | ||
321 | static void omap_mbox_fini(struct omap_mbox *mbox) | |
322 | { | |
58256307 KH |
323 | mutex_lock(&mbox_configured_lock); |
324 | ||
325 | if (!--mbox->use_count) { | |
1d8a0e96 | 326 | omap_mbox_disable_irq(mbox, IRQ_RX); |
58256307 KH |
327 | free_irq(mbox->irq, mbox); |
328 | tasklet_kill(&mbox->txq->tasklet); | |
43829731 | 329 | flush_work(&mbox->rxq->work); |
58256307 KH |
330 | mbox_queue_free(mbox->txq); |
331 | mbox_queue_free(mbox->rxq); | |
332 | } | |
340a614a | 333 | |
58256307 KH |
334 | if (likely(mbox->ops->shutdown)) { |
335 | if (!--mbox_configured) | |
5f00ec64 | 336 | mbox->ops->shutdown(mbox); |
5f00ec64 | 337 | } |
58256307 KH |
338 | |
339 | mutex_unlock(&mbox_configured_lock); | |
340a614a HD |
340 | } |
341 | ||
58256307 | 342 | struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb) |
340a614a | 343 | { |
c0377320 KH |
344 | struct omap_mbox *_mbox, *mbox = NULL; |
345 | int i, ret; | |
340a614a | 346 | |
9c80c8cd FC |
347 | if (!mboxes) |
348 | return ERR_PTR(-EINVAL); | |
340a614a | 349 | |
c0377320 KH |
350 | for (i = 0; (_mbox = mboxes[i]); i++) { |
351 | if (!strcmp(_mbox->name, name)) { | |
352 | mbox = _mbox; | |
9c80c8cd | 353 | break; |
c0377320 KH |
354 | } |
355 | } | |
9c80c8cd FC |
356 | |
357 | if (!mbox) | |
358 | return ERR_PTR(-ENOENT); | |
340a614a | 359 | |
58256307 KH |
360 | if (nb) |
361 | blocking_notifier_chain_register(&mbox->notifier, nb); | |
362 | ||
1d8a0e96 JG |
363 | ret = omap_mbox_startup(mbox); |
364 | if (ret) { | |
365 | blocking_notifier_chain_unregister(&mbox->notifier, nb); | |
366 | return ERR_PTR(-ENODEV); | |
367 | } | |
368 | ||
340a614a HD |
369 | return mbox; |
370 | } | |
371 | EXPORT_SYMBOL(omap_mbox_get); | |
372 | ||
58256307 | 373 | void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb) |
340a614a | 374 | { |
58256307 | 375 | blocking_notifier_chain_unregister(&mbox->notifier, nb); |
340a614a HD |
376 | omap_mbox_fini(mbox); |
377 | } | |
378 | EXPORT_SYMBOL(omap_mbox_put); | |
379 | ||
6b233985 HD |
380 | static struct class omap_mbox_class = { .name = "mbox", }; |
381 | ||
9c80c8cd | 382 | int omap_mbox_register(struct device *parent, struct omap_mbox **list) |
340a614a | 383 | { |
9c80c8cd FC |
384 | int ret; |
385 | int i; | |
340a614a | 386 | |
9c80c8cd FC |
387 | mboxes = list; |
388 | if (!mboxes) | |
340a614a | 389 | return -EINVAL; |
340a614a | 390 | |
9c80c8cd FC |
391 | for (i = 0; mboxes[i]; i++) { |
392 | struct omap_mbox *mbox = mboxes[i]; | |
393 | mbox->dev = device_create(&omap_mbox_class, | |
394 | parent, 0, mbox, "%s", mbox->name); | |
395 | if (IS_ERR(mbox->dev)) { | |
396 | ret = PTR_ERR(mbox->dev); | |
397 | goto err_out; | |
398 | } | |
58256307 KH |
399 | |
400 | BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier); | |
9c80c8cd | 401 | } |
f48cca87 HD |
402 | return 0; |
403 | ||
9c80c8cd FC |
404 | err_out: |
405 | while (i--) | |
406 | device_unregister(mboxes[i]->dev); | |
340a614a HD |
407 | return ret; |
408 | } | |
409 | EXPORT_SYMBOL(omap_mbox_register); | |
410 | ||
9c80c8cd | 411 | int omap_mbox_unregister(void) |
340a614a | 412 | { |
9c80c8cd | 413 | int i; |
340a614a | 414 | |
9c80c8cd FC |
415 | if (!mboxes) |
416 | return -EINVAL; | |
417 | ||
418 | for (i = 0; mboxes[i]; i++) | |
419 | device_unregister(mboxes[i]->dev); | |
420 | mboxes = NULL; | |
421 | return 0; | |
340a614a HD |
422 | } |
423 | EXPORT_SYMBOL(omap_mbox_unregister); | |
424 | ||
c7c158e5 | 425 | static int __init omap_mbox_init(void) |
340a614a | 426 | { |
6b233985 HD |
427 | int err; |
428 | ||
429 | err = class_register(&omap_mbox_class); | |
430 | if (err) | |
431 | return err; | |
432 | ||
b5bebe41 OBC |
433 | /* kfifo size sanity check: alignment and minimal size */ |
434 | mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); | |
ab66ac30 KH |
435 | mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, |
436 | sizeof(mbox_msg_t)); | |
b5bebe41 | 437 | |
c7c158e5 | 438 | return 0; |
340a614a | 439 | } |
6b233985 | 440 | subsys_initcall(omap_mbox_init); |
340a614a | 441 | |
c7c158e5 | 442 | static void __exit omap_mbox_exit(void) |
340a614a | 443 | { |
6b233985 | 444 | class_unregister(&omap_mbox_class); |
340a614a | 445 | } |
c7c158e5 | 446 | module_exit(omap_mbox_exit); |
340a614a | 447 | |
f48cca87 HD |
448 | MODULE_LICENSE("GPL v2"); |
449 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); | |
f375325a OBC |
450 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
451 | MODULE_AUTHOR("Hiroshi DOYU"); |