blob: 13ca2365f2791edc678b9f53205861518584b947 [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
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070034/* Mailbox FIFO handle functions */
35static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
36{
37 return mbox->ops->fifo_read(mbox);
38}
39static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
40{
41 mbox->ops->fifo_write(mbox, msg);
42}
43static inline int mbox_fifo_empty(struct omap_mbox *mbox)
44{
45 return mbox->ops->fifo_empty(mbox);
46}
47static inline int mbox_fifo_full(struct omap_mbox *mbox)
48{
49 return mbox->ops->fifo_full(mbox);
50}
51
52/* Mailbox IRQ handle functions */
53static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
54{
55 mbox->ops->enable_irq(mbox, irq);
56}
57static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
58{
59 mbox->ops->disable_irq(mbox, irq);
60}
61static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
62{
63 if (mbox->ops->ack_irq)
64 mbox->ops->ack_irq(mbox, irq);
65}
66static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
67{
68 return mbox->ops->is_irq(mbox, irq);
69}
70
Hiroshi DOYU340a6142006-12-07 15:43:59 -080071/*
72 * message sender
73 */
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -080074static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080075{
76 int ret = 0, i = 1000;
77
78 while (mbox_fifo_full(mbox)) {
79 if (mbox->ops->type == OMAP_MBOX_TYPE2)
80 return -1;
81 if (--i == 0)
82 return -1;
83 udelay(1);
84 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -080085 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080086 return ret;
87}
88
Tejun Heoec247512009-04-23 11:05:20 +090089struct omap_msg_tx_data {
90 mbox_msg_t msg;
91 void *arg;
92};
93
94static void omap_msg_tx_end_io(struct request *rq, int error)
95{
96 kfree(rq->special);
97 __blk_put_request(rq->q, rq);
98}
99
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800100int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
101{
Tejun Heoec247512009-04-23 11:05:20 +0900102 struct omap_msg_tx_data *tx_data;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800103 struct request *rq;
104 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +0900105
106 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
107 if (unlikely(!tx_data))
108 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800109
110 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
111 if (unlikely(!rq)) {
Tejun Heoec247512009-04-23 11:05:20 +0900112 kfree(tx_data);
113 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800114 }
115
Tejun Heoec247512009-04-23 11:05:20 +0900116 tx_data->msg = msg;
117 tx_data->arg = arg;
118 rq->end_io = omap_msg_tx_end_io;
119 blk_insert_request(q, rq, 0, tx_data);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800120
121 schedule_work(&mbox->txq->work);
Tejun Heoec247512009-04-23 11:05:20 +0900122 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800123}
124EXPORT_SYMBOL(omap_mbox_msg_send);
125
126static void mbox_tx_work(struct work_struct *work)
127{
128 int ret;
129 struct request *rq;
130 struct omap_mbox_queue *mq = container_of(work,
131 struct omap_mbox_queue, work);
132 struct omap_mbox *mbox = mq->queue->queuedata;
133 struct request_queue *q = mbox->txq->queue;
134
135 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900136 struct omap_msg_tx_data *tx_data;
137
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800138 spin_lock(q->queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900139 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800140 spin_unlock(q->queue_lock);
141
142 if (!rq)
143 break;
144
Tejun Heoec247512009-04-23 11:05:20 +0900145 tx_data = rq->special;
146
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800147 ret = __mbox_msg_send(mbox, tx_data->msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800148 if (ret) {
149 enable_mbox_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900150 spin_lock(q->queue_lock);
151 blk_requeue_request(q, rq);
152 spin_unlock(q->queue_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800153 return;
154 }
155
156 spin_lock(q->queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900157 __blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800158 spin_unlock(q->queue_lock);
159 }
160}
161
162/*
163 * Message receiver(workqueue)
164 */
165static void mbox_rx_work(struct work_struct *work)
166{
167 struct omap_mbox_queue *mq =
168 container_of(work, struct omap_mbox_queue, work);
169 struct omap_mbox *mbox = mq->queue->queuedata;
170 struct request_queue *q = mbox->rxq->queue;
171 struct request *rq;
172 mbox_msg_t msg;
173 unsigned long flags;
174
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800175 while (1) {
176 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900177 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800178 spin_unlock_irqrestore(q->queue_lock, flags);
179 if (!rq)
180 break;
181
Tejun Heoec247512009-04-23 11:05:20 +0900182 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900183 blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800184 mbox->rxq->callback((void *)msg);
185 }
186}
187
188/*
189 * Mailbox interrupt handler
190 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800191static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800192{
193}
194
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800195static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800196{
197}
198
199static void __mbox_tx_interrupt(struct omap_mbox *mbox)
200{
201 disable_mbox_irq(mbox, IRQ_TX);
202 ack_mbox_irq(mbox, IRQ_TX);
203 schedule_work(&mbox->txq->work);
204}
205
206static void __mbox_rx_interrupt(struct omap_mbox *mbox)
207{
208 struct request *rq;
209 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200210 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800211
212 disable_mbox_irq(mbox, IRQ_RX);
213
214 while (!mbox_fifo_empty(mbox)) {
215 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
216 if (unlikely(!rq))
217 goto nomem;
218
219 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800220
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800221
Tejun Heoec247512009-04-23 11:05:20 +0900222 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800223 if (mbox->ops->type == OMAP_MBOX_TYPE1)
224 break;
225 }
226
227 /* no more messages in the fifo. clear IRQ source. */
228 ack_mbox_irq(mbox, IRQ_RX);
229 enable_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700230nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800231 schedule_work(&mbox->rxq->work);
232}
233
234static irqreturn_t mbox_interrupt(int irq, void *p)
235{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400236 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800237
238 if (is_mbox_irq(mbox, IRQ_TX))
239 __mbox_tx_interrupt(mbox);
240
241 if (is_mbox_irq(mbox, IRQ_RX))
242 __mbox_rx_interrupt(mbox);
243
244 return IRQ_HANDLED;
245}
246
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800247static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800248 request_fn_proc *proc,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800249 void (*work) (struct work_struct *))
250{
Jens Axboe165125e2007-07-24 09:28:11 +0200251 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800252 struct omap_mbox_queue *mq;
253
254 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
255 if (!mq)
256 return NULL;
257
258 spin_lock_init(&mq->lock);
259
260 q = blk_init_queue(proc, &mq->lock);
261 if (!q)
262 goto error;
263 q->queuedata = mbox;
264 mq->queue = q;
265
266 INIT_WORK(&mq->work, work);
267
268 return mq;
269error:
270 kfree(mq);
271 return NULL;
272}
273
274static void mbox_queue_free(struct omap_mbox_queue *q)
275{
276 blk_cleanup_queue(q->queue);
277 kfree(q);
278}
279
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800280static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800281{
282 int ret;
283 struct omap_mbox_queue *mq;
284
285 if (likely(mbox->ops->startup)) {
286 ret = mbox->ops->startup(mbox);
287 if (unlikely(ret))
288 return ret;
289 }
290
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800291 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
292 mbox->name, mbox);
293 if (unlikely(ret)) {
294 printk(KERN_ERR
295 "failed to register mailbox interrupt:%d\n", ret);
296 goto fail_request_irq;
297 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800298
299 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
300 if (!mq) {
301 ret = -ENOMEM;
302 goto fail_alloc_txq;
303 }
304 mbox->txq = mq;
305
306 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
307 if (!mq) {
308 ret = -ENOMEM;
309 goto fail_alloc_rxq;
310 }
311 mbox->rxq = mq;
312
313 return 0;
314
315 fail_alloc_rxq:
316 mbox_queue_free(mbox->txq);
317 fail_alloc_txq:
318 free_irq(mbox->irq, mbox);
319 fail_request_irq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800320 if (unlikely(mbox->ops->shutdown))
321 mbox->ops->shutdown(mbox);
322
323 return ret;
324}
325
326static void omap_mbox_fini(struct omap_mbox *mbox)
327{
328 mbox_queue_free(mbox->txq);
329 mbox_queue_free(mbox->rxq);
330
331 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800332
333 if (unlikely(mbox->ops->shutdown))
334 mbox->ops->shutdown(mbox);
335}
336
337static struct omap_mbox **find_mboxes(const char *name)
338{
339 struct omap_mbox **p;
340
341 for (p = &mboxes; *p; p = &(*p)->next) {
342 if (strcmp((*p)->name, name) == 0)
343 break;
344 }
345
346 return p;
347}
348
349struct omap_mbox *omap_mbox_get(const char *name)
350{
351 struct omap_mbox *mbox;
352 int ret;
353
354 read_lock(&mboxes_lock);
355 mbox = *(find_mboxes(name));
356 if (mbox == NULL) {
357 read_unlock(&mboxes_lock);
358 return ERR_PTR(-ENOENT);
359 }
360
361 read_unlock(&mboxes_lock);
362
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800363 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800364 if (ret)
365 return ERR_PTR(-ENODEV);
366
367 return mbox;
368}
369EXPORT_SYMBOL(omap_mbox_get);
370
371void omap_mbox_put(struct omap_mbox *mbox)
372{
373 omap_mbox_fini(mbox);
374}
375EXPORT_SYMBOL(omap_mbox_put);
376
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700377int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800378{
379 int ret = 0;
380 struct omap_mbox **tmp;
381
382 if (!mbox)
383 return -EINVAL;
384 if (mbox->next)
385 return -EBUSY;
386
387 write_lock(&mboxes_lock);
388 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700389 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800390 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700391 write_unlock(&mboxes_lock);
392 goto err_find;
393 }
394 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800395 write_unlock(&mboxes_lock);
396
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700397 return 0;
398
399err_find:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800400 return ret;
401}
402EXPORT_SYMBOL(omap_mbox_register);
403
404int omap_mbox_unregister(struct omap_mbox *mbox)
405{
406 struct omap_mbox **tmp;
407
408 write_lock(&mboxes_lock);
409 tmp = &mboxes;
410 while (*tmp) {
411 if (mbox == *tmp) {
412 *tmp = mbox->next;
413 mbox->next = NULL;
414 write_unlock(&mboxes_lock);
415 return 0;
416 }
417 tmp = &(*tmp)->next;
418 }
419 write_unlock(&mboxes_lock);
420
421 return -EINVAL;
422}
423EXPORT_SYMBOL(omap_mbox_unregister);
424
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800425static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800426{
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800427 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800428}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800429module_init(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800430
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800431static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800432{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800433}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800434module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800435
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700436MODULE_LICENSE("GPL v2");
437MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
438MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");