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