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