blob: 4d7947e5dd952a2eaac0a42ceeb0a669c1b03d0c [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 +090083struct omap_msg_tx_data {
84 mbox_msg_t msg;
Tejun Heoec247512009-04-23 11:05:20 +090085};
86
87static void omap_msg_tx_end_io(struct request *rq, int error)
88{
89 kfree(rq->special);
90 __blk_put_request(rq->q, rq);
91}
92
C A Subramaniamb2b63622009-11-22 10:11:20 -080093int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080094{
Tejun Heoec247512009-04-23 11:05:20 +090095 struct omap_msg_tx_data *tx_data;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080096 struct request *rq;
97 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +090098
99 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
100 if (unlikely(!tx_data))
101 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800102
103 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
104 if (unlikely(!rq)) {
Tejun Heoec247512009-04-23 11:05:20 +0900105 kfree(tx_data);
106 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800107 }
108
Tejun Heoec247512009-04-23 11:05:20 +0900109 tx_data->msg = msg;
Tejun Heoec247512009-04-23 11:05:20 +0900110 rq->end_io = omap_msg_tx_end_io;
111 blk_insert_request(q, rq, 0, tx_data);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800112
113 schedule_work(&mbox->txq->work);
Tejun Heoec247512009-04-23 11:05:20 +0900114 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800115}
116EXPORT_SYMBOL(omap_mbox_msg_send);
117
118static void mbox_tx_work(struct work_struct *work)
119{
120 int ret;
121 struct request *rq;
122 struct omap_mbox_queue *mq = container_of(work,
123 struct omap_mbox_queue, work);
124 struct omap_mbox *mbox = mq->queue->queuedata;
125 struct request_queue *q = mbox->txq->queue;
126
127 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900128 struct omap_msg_tx_data *tx_data;
129
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800130 spin_lock(q->queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900131 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800132 spin_unlock(q->queue_lock);
133
134 if (!rq)
135 break;
136
Tejun Heoec247512009-04-23 11:05:20 +0900137 tx_data = rq->special;
138
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800139 ret = __mbox_msg_send(mbox, tx_data->msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800140 if (ret) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800141 omap_mbox_enable_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900142 spin_lock(q->queue_lock);
143 blk_requeue_request(q, rq);
144 spin_unlock(q->queue_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800145 return;
146 }
147
148 spin_lock(q->queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900149 __blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800150 spin_unlock(q->queue_lock);
151 }
152}
153
154/*
155 * Message receiver(workqueue)
156 */
157static void mbox_rx_work(struct work_struct *work)
158{
159 struct omap_mbox_queue *mq =
160 container_of(work, struct omap_mbox_queue, work);
161 struct omap_mbox *mbox = mq->queue->queuedata;
162 struct request_queue *q = mbox->rxq->queue;
163 struct request *rq;
164 mbox_msg_t msg;
165 unsigned long flags;
166
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800167 while (1) {
168 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900169 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800170 spin_unlock_irqrestore(q->queue_lock, flags);
171 if (!rq)
172 break;
173
Tejun Heoec247512009-04-23 11:05:20 +0900174 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900175 blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800176 mbox->rxq->callback((void *)msg);
177 }
178}
179
180/*
181 * Mailbox interrupt handler
182 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800183static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800184{
185}
186
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800187static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800188{
189}
190
191static void __mbox_tx_interrupt(struct omap_mbox *mbox)
192{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800193 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800194 ack_mbox_irq(mbox, IRQ_TX);
195 schedule_work(&mbox->txq->work);
196}
197
198static void __mbox_rx_interrupt(struct omap_mbox *mbox)
199{
200 struct request *rq;
201 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200202 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800203
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800204 while (!mbox_fifo_empty(mbox)) {
205 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
206 if (unlikely(!rq))
207 goto nomem;
208
209 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800210
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800211
Tejun Heoec247512009-04-23 11:05:20 +0900212 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800213 if (mbox->ops->type == OMAP_MBOX_TYPE1)
214 break;
215 }
216
217 /* no more messages in the fifo. clear IRQ source. */
218 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700219nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800220 schedule_work(&mbox->rxq->work);
221}
222
223static irqreturn_t mbox_interrupt(int irq, void *p)
224{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400225 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800226
227 if (is_mbox_irq(mbox, IRQ_TX))
228 __mbox_tx_interrupt(mbox);
229
230 if (is_mbox_irq(mbox, IRQ_RX))
231 __mbox_rx_interrupt(mbox);
232
233 return IRQ_HANDLED;
234}
235
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800236static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800237 request_fn_proc *proc,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800238 void (*work) (struct work_struct *))
239{
Jens Axboe165125e2007-07-24 09:28:11 +0200240 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800241 struct omap_mbox_queue *mq;
242
243 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
244 if (!mq)
245 return NULL;
246
247 spin_lock_init(&mq->lock);
248
249 q = blk_init_queue(proc, &mq->lock);
250 if (!q)
251 goto error;
252 q->queuedata = mbox;
253 mq->queue = q;
254
255 INIT_WORK(&mq->work, work);
256
257 return mq;
258error:
259 kfree(mq);
260 return NULL;
261}
262
263static void mbox_queue_free(struct omap_mbox_queue *q)
264{
265 blk_cleanup_queue(q->queue);
266 kfree(q);
267}
268
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800269static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800270{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800271 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800272 struct omap_mbox_queue *mq;
273
274 if (likely(mbox->ops->startup)) {
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800275 write_lock(&mboxes_lock);
276 if (!mbox_configured)
277 ret = mbox->ops->startup(mbox);
278
279 if (unlikely(ret)) {
280 write_unlock(&mboxes_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800281 return ret;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800282 }
283 mbox_configured++;
284 write_unlock(&mboxes_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800285 }
286
C A Subramaniam5e683822009-11-22 10:11:23 -0800287 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800288 mbox->name, mbox);
289 if (unlikely(ret)) {
290 printk(KERN_ERR
291 "failed to register mailbox interrupt:%d\n", ret);
292 goto fail_request_irq;
293 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800294
295 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
296 if (!mq) {
297 ret = -ENOMEM;
298 goto fail_alloc_txq;
299 }
300 mbox->txq = mq;
301
302 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
303 if (!mq) {
304 ret = -ENOMEM;
305 goto fail_alloc_rxq;
306 }
307 mbox->rxq = mq;
308
309 return 0;
310
311 fail_alloc_rxq:
312 mbox_queue_free(mbox->txq);
313 fail_alloc_txq:
314 free_irq(mbox->irq, mbox);
315 fail_request_irq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800316 if (unlikely(mbox->ops->shutdown))
317 mbox->ops->shutdown(mbox);
318
319 return ret;
320}
321
322static void omap_mbox_fini(struct omap_mbox *mbox)
323{
324 mbox_queue_free(mbox->txq);
325 mbox_queue_free(mbox->rxq);
326
327 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800328
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800329 if (unlikely(mbox->ops->shutdown)) {
330 write_lock(&mboxes_lock);
331 if (mbox_configured > 0)
332 mbox_configured--;
333 if (!mbox_configured)
334 mbox->ops->shutdown(mbox);
335 write_unlock(&mboxes_lock);
336 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800337}
338
339static struct omap_mbox **find_mboxes(const char *name)
340{
341 struct omap_mbox **p;
342
343 for (p = &mboxes; *p; p = &(*p)->next) {
344 if (strcmp((*p)->name, name) == 0)
345 break;
346 }
347
348 return p;
349}
350
351struct omap_mbox *omap_mbox_get(const char *name)
352{
353 struct omap_mbox *mbox;
354 int ret;
355
356 read_lock(&mboxes_lock);
357 mbox = *(find_mboxes(name));
358 if (mbox == NULL) {
359 read_unlock(&mboxes_lock);
360 return ERR_PTR(-ENOENT);
361 }
362
363 read_unlock(&mboxes_lock);
364
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800365 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800366 if (ret)
367 return ERR_PTR(-ENODEV);
368
369 return mbox;
370}
371EXPORT_SYMBOL(omap_mbox_get);
372
373void omap_mbox_put(struct omap_mbox *mbox)
374{
375 omap_mbox_fini(mbox);
376}
377EXPORT_SYMBOL(omap_mbox_put);
378
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700379int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380{
381 int ret = 0;
382 struct omap_mbox **tmp;
383
384 if (!mbox)
385 return -EINVAL;
386 if (mbox->next)
387 return -EBUSY;
388
389 write_lock(&mboxes_lock);
390 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700391 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800392 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700393 write_unlock(&mboxes_lock);
394 goto err_find;
395 }
396 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800397 write_unlock(&mboxes_lock);
398
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700399 return 0;
400
401err_find:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800402 return ret;
403}
404EXPORT_SYMBOL(omap_mbox_register);
405
406int omap_mbox_unregister(struct omap_mbox *mbox)
407{
408 struct omap_mbox **tmp;
409
410 write_lock(&mboxes_lock);
411 tmp = &mboxes;
412 while (*tmp) {
413 if (mbox == *tmp) {
414 *tmp = mbox->next;
415 mbox->next = NULL;
416 write_unlock(&mboxes_lock);
417 return 0;
418 }
419 tmp = &(*tmp)->next;
420 }
421 write_unlock(&mboxes_lock);
422
423 return -EINVAL;
424}
425EXPORT_SYMBOL(omap_mbox_unregister);
426
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800427static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800428{
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800429 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800430}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800431module_init(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800432
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800433static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800434{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800435}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800436module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800437
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700438MODULE_LICENSE("GPL v2");
439MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
440MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");