blob: e6b31159f08b22b197e9f7bbc07cbf94ba9fd642 [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 */
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070053static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
54{
55 if (mbox->ops->ack_irq)
56 mbox->ops->ack_irq(mbox, irq);
57}
58static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
59{
60 return mbox->ops->is_irq(mbox, irq);
61}
62
Hiroshi DOYU340a6142006-12-07 15:43:59 -080063/*
64 * message sender
65 */
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -080066static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080067{
68 int ret = 0, i = 1000;
69
70 while (mbox_fifo_full(mbox)) {
71 if (mbox->ops->type == OMAP_MBOX_TYPE2)
72 return -1;
73 if (--i == 0)
74 return -1;
75 udelay(1);
76 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -080077 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -080078 return ret;
79}
80
Tejun Heoec247512009-04-23 11:05:20 +090081struct omap_msg_tx_data {
82 mbox_msg_t msg;
Tejun Heoec247512009-04-23 11:05:20 +090083};
84
85static void omap_msg_tx_end_io(struct request *rq, int error)
86{
87 kfree(rq->special);
88 __blk_put_request(rq->q, rq);
89}
90
C A Subramaniamb2b63622009-11-22 10:11:20 -080091int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080092{
Tejun Heoec247512009-04-23 11:05:20 +090093 struct omap_msg_tx_data *tx_data;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080094 struct request *rq;
95 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +090096
97 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
98 if (unlikely(!tx_data))
99 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800100
101 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
102 if (unlikely(!rq)) {
Tejun Heoec247512009-04-23 11:05:20 +0900103 kfree(tx_data);
104 return -ENOMEM;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800105 }
106
Tejun Heoec247512009-04-23 11:05:20 +0900107 tx_data->msg = msg;
Tejun Heoec247512009-04-23 11:05:20 +0900108 rq->end_io = omap_msg_tx_end_io;
109 blk_insert_request(q, rq, 0, tx_data);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800110
111 schedule_work(&mbox->txq->work);
Tejun Heoec247512009-04-23 11:05:20 +0900112 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800113}
114EXPORT_SYMBOL(omap_mbox_msg_send);
115
116static void mbox_tx_work(struct work_struct *work)
117{
118 int ret;
119 struct request *rq;
120 struct omap_mbox_queue *mq = container_of(work,
121 struct omap_mbox_queue, work);
122 struct omap_mbox *mbox = mq->queue->queuedata;
123 struct request_queue *q = mbox->txq->queue;
124
125 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900126 struct omap_msg_tx_data *tx_data;
127
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800128 spin_lock(q->queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900129 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800130 spin_unlock(q->queue_lock);
131
132 if (!rq)
133 break;
134
Tejun Heoec247512009-04-23 11:05:20 +0900135 tx_data = rq->special;
136
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800137 ret = __mbox_msg_send(mbox, tx_data->msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800138 if (ret) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800139 omap_mbox_enable_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900140 spin_lock(q->queue_lock);
141 blk_requeue_request(q, rq);
142 spin_unlock(q->queue_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800143 return;
144 }
145
146 spin_lock(q->queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900147 __blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800148 spin_unlock(q->queue_lock);
149 }
150}
151
152/*
153 * Message receiver(workqueue)
154 */
155static void mbox_rx_work(struct work_struct *work)
156{
157 struct omap_mbox_queue *mq =
158 container_of(work, struct omap_mbox_queue, work);
159 struct omap_mbox *mbox = mq->queue->queuedata;
160 struct request_queue *q = mbox->rxq->queue;
161 struct request *rq;
162 mbox_msg_t msg;
163 unsigned long flags;
164
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800165 while (1) {
166 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900167 rq = blk_fetch_request(q);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800168 spin_unlock_irqrestore(q->queue_lock, flags);
169 if (!rq)
170 break;
171
Tejun Heoec247512009-04-23 11:05:20 +0900172 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900173 blk_end_request_all(rq, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800174 mbox->rxq->callback((void *)msg);
175 }
176}
177
178/*
179 * Mailbox interrupt handler
180 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800181static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800182{
183}
184
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800185static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800186{
187}
188
189static void __mbox_tx_interrupt(struct omap_mbox *mbox)
190{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800191 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800192 ack_mbox_irq(mbox, IRQ_TX);
193 schedule_work(&mbox->txq->work);
194}
195
196static void __mbox_rx_interrupt(struct omap_mbox *mbox)
197{
198 struct request *rq;
199 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200200 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800201
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800202 while (!mbox_fifo_empty(mbox)) {
203 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
204 if (unlikely(!rq))
205 goto nomem;
206
207 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800208
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800209
Tejun Heoec247512009-04-23 11:05:20 +0900210 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800211 if (mbox->ops->type == OMAP_MBOX_TYPE1)
212 break;
213 }
214
215 /* no more messages in the fifo. clear IRQ source. */
216 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700217nomem:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800218 schedule_work(&mbox->rxq->work);
219}
220
221static irqreturn_t mbox_interrupt(int irq, void *p)
222{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400223 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800224
225 if (is_mbox_irq(mbox, IRQ_TX))
226 __mbox_tx_interrupt(mbox);
227
228 if (is_mbox_irq(mbox, IRQ_RX))
229 __mbox_rx_interrupt(mbox);
230
231 return IRQ_HANDLED;
232}
233
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800234static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800235 request_fn_proc *proc,
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800236 void (*work) (struct work_struct *))
237{
Jens Axboe165125e2007-07-24 09:28:11 +0200238 struct request_queue *q;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800239 struct omap_mbox_queue *mq;
240
241 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
242 if (!mq)
243 return NULL;
244
245 spin_lock_init(&mq->lock);
246
247 q = blk_init_queue(proc, &mq->lock);
248 if (!q)
249 goto error;
250 q->queuedata = mbox;
251 mq->queue = q;
252
253 INIT_WORK(&mq->work, work);
254
255 return mq;
256error:
257 kfree(mq);
258 return NULL;
259}
260
261static void mbox_queue_free(struct omap_mbox_queue *q)
262{
263 blk_cleanup_queue(q->queue);
264 kfree(q);
265}
266
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800267static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800268{
269 int ret;
270 struct omap_mbox_queue *mq;
271
272 if (likely(mbox->ops->startup)) {
273 ret = mbox->ops->startup(mbox);
274 if (unlikely(ret))
275 return ret;
276 }
277
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800278 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
279 mbox->name, mbox);
280 if (unlikely(ret)) {
281 printk(KERN_ERR
282 "failed to register mailbox interrupt:%d\n", ret);
283 goto fail_request_irq;
284 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800285
286 mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
287 if (!mq) {
288 ret = -ENOMEM;
289 goto fail_alloc_txq;
290 }
291 mbox->txq = mq;
292
293 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
294 if (!mq) {
295 ret = -ENOMEM;
296 goto fail_alloc_rxq;
297 }
298 mbox->rxq = mq;
299
300 return 0;
301
302 fail_alloc_rxq:
303 mbox_queue_free(mbox->txq);
304 fail_alloc_txq:
305 free_irq(mbox->irq, mbox);
306 fail_request_irq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800307 if (unlikely(mbox->ops->shutdown))
308 mbox->ops->shutdown(mbox);
309
310 return ret;
311}
312
313static void omap_mbox_fini(struct omap_mbox *mbox)
314{
315 mbox_queue_free(mbox->txq);
316 mbox_queue_free(mbox->rxq);
317
318 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800319
320 if (unlikely(mbox->ops->shutdown))
321 mbox->ops->shutdown(mbox);
322}
323
324static struct omap_mbox **find_mboxes(const char *name)
325{
326 struct omap_mbox **p;
327
328 for (p = &mboxes; *p; p = &(*p)->next) {
329 if (strcmp((*p)->name, name) == 0)
330 break;
331 }
332
333 return p;
334}
335
336struct omap_mbox *omap_mbox_get(const char *name)
337{
338 struct omap_mbox *mbox;
339 int ret;
340
341 read_lock(&mboxes_lock);
342 mbox = *(find_mboxes(name));
343 if (mbox == NULL) {
344 read_unlock(&mboxes_lock);
345 return ERR_PTR(-ENOENT);
346 }
347
348 read_unlock(&mboxes_lock);
349
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800350 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800351 if (ret)
352 return ERR_PTR(-ENODEV);
353
354 return mbox;
355}
356EXPORT_SYMBOL(omap_mbox_get);
357
358void omap_mbox_put(struct omap_mbox *mbox)
359{
360 omap_mbox_fini(mbox);
361}
362EXPORT_SYMBOL(omap_mbox_put);
363
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700364int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800365{
366 int ret = 0;
367 struct omap_mbox **tmp;
368
369 if (!mbox)
370 return -EINVAL;
371 if (mbox->next)
372 return -EBUSY;
373
374 write_lock(&mboxes_lock);
375 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700376 if (*tmp) {
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800377 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700378 write_unlock(&mboxes_lock);
379 goto err_find;
380 }
381 *tmp = mbox;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800382 write_unlock(&mboxes_lock);
383
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700384 return 0;
385
386err_find:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800387 return ret;
388}
389EXPORT_SYMBOL(omap_mbox_register);
390
391int omap_mbox_unregister(struct omap_mbox *mbox)
392{
393 struct omap_mbox **tmp;
394
395 write_lock(&mboxes_lock);
396 tmp = &mboxes;
397 while (*tmp) {
398 if (mbox == *tmp) {
399 *tmp = mbox->next;
400 mbox->next = NULL;
401 write_unlock(&mboxes_lock);
402 return 0;
403 }
404 tmp = &(*tmp)->next;
405 }
406 write_unlock(&mboxes_lock);
407
408 return -EINVAL;
409}
410EXPORT_SYMBOL(omap_mbox_unregister);
411
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800412static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800413{
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800414 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800415}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800416module_init(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800417
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800418static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800419{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800420}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800421module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800422
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700423MODULE_LICENSE("GPL v2");
424MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
425MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");