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