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