blob: 8e90633e4cb93e686671100bd1eefeaf54518e7a [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
2 * OMAP mailbox driver
3 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07004 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
Hiroshi DOYU340a6142006-12-07 15:43:59 -08005 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07006 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -08007 *
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
Hiroshi DOYU340a6142006-12-07 15:43:59 -080024#include <linux/module.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080025#include <linux/interrupt.h>
26#include <linux/device.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080027#include <linux/delay.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070028
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/mailbox.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080030
31static struct omap_mbox *mboxes;
32static DEFINE_RWLOCK(mboxes_lock);
33
C A Subramaniam5f00ec62009-11-22 10:11:22 -080034static int mbox_configured;
35
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070036/* Mailbox FIFO handle functions */
37static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
38{
39 return mbox->ops->fifo_read(mbox);
40}
41static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
42{
43 mbox->ops->fifo_write(mbox, msg);
44}
45static inline int mbox_fifo_empty(struct omap_mbox *mbox)
46{
47 return mbox->ops->fifo_empty(mbox);
48}
49static inline int mbox_fifo_full(struct omap_mbox *mbox)
50{
51 return mbox->ops->fifo_full(mbox);
52}
53
54/* Mailbox IRQ handle functions */
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070055static 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}
60static 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
Hiroshi DOYU340a6142006-12-07 15:43:59 -080065/*
66 * message sender
67 */
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -080068static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080069{
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 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -080079 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080080 return ret;
81}
82
Tejun Heoec247512009-04-23 11:05:20 +090083
C A Subramaniamb2b63622009-11-22 10:11:20 -080084int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080085{
C A Subramaniam5ed8d322009-11-22 10:11:24 -080086
Hiroshi DOYU340a6142006-12-07 15:43:59 -080087 struct request *rq;
88 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +090089
Hiroshi DOYU340a6142006-12-07 15:43:59 -080090 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
C A Subramaniam5ed8d322009-11-22 10:11:24 -080091 if (unlikely(!rq))
Tejun Heoec247512009-04-23 11:05:20 +090092 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080093
C A Subramaniam5ed8d322009-11-22 10:11:24 -080094 blk_insert_request(q, rq, 0, (void *) msg);
95 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080096
Tejun Heoec247512009-04-23 11:05:20 +090097 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080098}
99EXPORT_SYMBOL(omap_mbox_msg_send);
100
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800101static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800102{
103 int ret;
104 struct request *rq;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800105 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800106 struct request_queue *q = mbox->txq->queue;
107
108 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900109
Tejun Heo9934c8c2009-05-08 11:54:16 +0900110 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800111
112 if (!rq)
113 break;
114
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800115 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800116 if (ret) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800117 omap_mbox_enable_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900118 blk_requeue_request(q, rq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800119 return;
120 }
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800121 blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800122 }
123}
124
125/*
126 * Message receiver(workqueue)
127 */
128static 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
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800138 while (1) {
139 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900140 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800141 spin_unlock_irqrestore(q->queue_lock, flags);
142 if (!rq)
143 break;
144
Tejun Heoec247512009-04-23 11:05:20 +0900145 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900146 blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800147 mbox->rxq->callback((void *)msg);
148 }
149}
150
151/*
152 * Mailbox interrupt handler
153 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800154static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800155{
156}
157
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800158static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800159{
160}
161
162static void __mbox_tx_interrupt(struct omap_mbox *mbox)
163{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800164 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800165 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800166 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800167}
168
169static void __mbox_rx_interrupt(struct omap_mbox *mbox)
170{
171 struct request *rq;
172 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200173 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800174
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800175 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);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800181
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800182
Tejun Heoec247512009-04-23 11:05:20 +0900183 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800184 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);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700190nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800191 schedule_work(&mbox->rxq->work);
192}
193
194static irqreturn_t mbox_interrupt(int irq, void *p)
195{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400196 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800197
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
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800207static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800208 request_fn_proc *proc,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800209 void (*work) (struct work_struct *),
210 void (*tasklet)(unsigned long))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800211{
Jens Axboe165125e2007-07-24 09:28:11 +0200212 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800213 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
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800227 if (work)
228 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800229
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800230 if (tasklet)
231 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800232 return mq;
233error:
234 kfree(mq);
235 return NULL;
236}
237
238static void mbox_queue_free(struct omap_mbox_queue *q)
239{
240 blk_cleanup_queue(q->queue);
241 kfree(q);
242}
243
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800244static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800245{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800246 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800247 struct omap_mbox_queue *mq;
248
249 if (likely(mbox->ops->startup)) {
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800250 write_lock(&mboxes_lock);
251 if (!mbox_configured)
252 ret = mbox->ops->startup(mbox);
253
254 if (unlikely(ret)) {
255 write_unlock(&mboxes_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800256 return ret;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800257 }
258 mbox_configured++;
259 write_unlock(&mboxes_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800260 }
261
C A Subramaniam5e683822009-11-22 10:11:23 -0800262 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800263 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 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800269
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800270 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800271 if (!mq) {
272 ret = -ENOMEM;
273 goto fail_alloc_txq;
274 }
275 mbox->txq = mq;
276
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800277 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800278 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:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800291 if (unlikely(mbox->ops->shutdown))
292 mbox->ops->shutdown(mbox);
293
294 return ret;
295}
296
297static 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);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800303
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800304 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 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800312}
313
314static 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
326struct 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
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800340 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800341 if (ret)
342 return ERR_PTR(-ENODEV);
343
344 return mbox;
345}
346EXPORT_SYMBOL(omap_mbox_get);
347
348void omap_mbox_put(struct omap_mbox *mbox)
349{
350 omap_mbox_fini(mbox);
351}
352EXPORT_SYMBOL(omap_mbox_put);
353
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700354int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800355{
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);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700366 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800367 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700368 write_unlock(&mboxes_lock);
369 goto err_find;
370 }
371 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800372 write_unlock(&mboxes_lock);
373
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700374 return 0;
375
376err_find:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800377 return ret;
378}
379EXPORT_SYMBOL(omap_mbox_register);
380
381int 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}
400EXPORT_SYMBOL(omap_mbox_unregister);
401
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800402static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800403{
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800404 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800405}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800406module_init(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800407
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800408static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800409{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800410}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800411module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800412
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700413MODULE_LICENSE("GPL v2");
414MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
415MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");