blob: 77c23f5a003750fdecf28f17d4ddab06a2ed3258 [file] [log] [blame]
Hiroshi DOYU340a614a2006-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 DOYU340a614a2006-12-07 15:43:59 -08005 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07006 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU340a614a2006-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 DOYU340a614a2006-12-07 15:43:59 -080024#include <linux/module.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080025#include <linux/interrupt.h>
26#include <linux/device.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080027#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070029
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/mailbox.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080031
Rob Clark8250a5c2010-01-04 19:22:03 +053032static struct workqueue_struct *mboxd;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080033static struct omap_mbox *mboxes;
34static DEFINE_RWLOCK(mboxes_lock);
35
C A Subramaniam5f00ec62009-11-22 10:11:22 -080036static int mbox_configured;
37
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070038/* Mailbox FIFO handle functions */
39static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
40{
41 return mbox->ops->fifo_read(mbox);
42}
43static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
44{
45 mbox->ops->fifo_write(mbox, msg);
46}
47static inline int mbox_fifo_empty(struct omap_mbox *mbox)
48{
49 return mbox->ops->fifo_empty(mbox);
50}
51static inline int mbox_fifo_full(struct omap_mbox *mbox)
52{
53 return mbox->ops->fifo_full(mbox);
54}
55
56/* Mailbox IRQ handle functions */
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070057static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
58{
59 if (mbox->ops->ack_irq)
60 mbox->ops->ack_irq(mbox, irq);
61}
62static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
63{
64 return mbox->ops->is_irq(mbox, irq);
65}
66
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080067/*
68 * message sender
69 */
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -080070static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080071{
72 int ret = 0, i = 1000;
73
74 while (mbox_fifo_full(mbox)) {
75 if (mbox->ops->type == OMAP_MBOX_TYPE2)
76 return -1;
77 if (--i == 0)
78 return -1;
79 udelay(1);
80 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080081 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080082 return ret;
83}
84
Tejun Heoec247512009-04-23 11:05:20 +090085
C A Subramaniamb2b63622009-11-22 10:11:20 -080086int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080087{
C A Subramaniam5ed8d322009-11-22 10:11:24 -080088
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080089 struct request *rq;
90 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +090091
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080092 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
C A Subramaniam5ed8d322009-11-22 10:11:24 -080093 if (unlikely(!rq))
Tejun Heoec247512009-04-23 11:05:20 +090094 return -ENOMEM;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080095
C A Subramaniam5ed8d322009-11-22 10:11:24 -080096 blk_insert_request(q, rq, 0, (void *) msg);
97 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080098
Tejun Heoec247512009-04-23 11:05:20 +090099 return 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800100}
101EXPORT_SYMBOL(omap_mbox_msg_send);
102
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800103static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800104{
105 int ret;
106 struct request *rq;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800107 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800108 struct request_queue *q = mbox->txq->queue;
109
110 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900111
Tejun Heo9934c8c2009-05-08 11:54:16 +0900112 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800113
114 if (!rq)
115 break;
116
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800117 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800118 if (ret) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800119 omap_mbox_enable_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900120 blk_requeue_request(q, rq);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800121 return;
122 }
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800123 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800124 }
125}
126
127/*
128 * Message receiver(workqueue)
129 */
130static void mbox_rx_work(struct work_struct *work)
131{
132 struct omap_mbox_queue *mq =
133 container_of(work, struct omap_mbox_queue, work);
134 struct omap_mbox *mbox = mq->queue->queuedata;
135 struct request_queue *q = mbox->rxq->queue;
136 struct request *rq;
137 mbox_msg_t msg;
138 unsigned long flags;
139
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800140 while (1) {
141 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900142 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800143 spin_unlock_irqrestore(q->queue_lock, flags);
144 if (!rq)
145 break;
146
Tejun Heoec247512009-04-23 11:05:20 +0900147 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900148 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800149 mbox->rxq->callback((void *)msg);
150 }
151}
152
153/*
154 * Mailbox interrupt handler
155 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800156static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800157{
158}
159
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800160static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800161{
162}
163
164static void __mbox_tx_interrupt(struct omap_mbox *mbox)
165{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800166 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800167 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800168 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800169}
170
171static void __mbox_rx_interrupt(struct omap_mbox *mbox)
172{
173 struct request *rq;
174 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200175 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800176
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800177 while (!mbox_fifo_empty(mbox)) {
178 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
179 if (unlikely(!rq))
180 goto nomem;
181
182 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800183
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800184
Tejun Heoec247512009-04-23 11:05:20 +0900185 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800186 if (mbox->ops->type == OMAP_MBOX_TYPE1)
187 break;
188 }
189
190 /* no more messages in the fifo. clear IRQ source. */
191 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700192nomem:
Rob Clark8250a5c2010-01-04 19:22:03 +0530193 queue_work(mboxd, &mbox->rxq->work);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800194}
195
196static irqreturn_t mbox_interrupt(int irq, void *p)
197{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400198 struct omap_mbox *mbox = p;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800199
200 if (is_mbox_irq(mbox, IRQ_TX))
201 __mbox_tx_interrupt(mbox);
202
203 if (is_mbox_irq(mbox, IRQ_RX))
204 __mbox_rx_interrupt(mbox);
205
206 return IRQ_HANDLED;
207}
208
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800209static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800210 request_fn_proc *proc,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800211 void (*work) (struct work_struct *),
212 void (*tasklet)(unsigned long))
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800213{
Jens Axboe165125e2007-07-24 09:28:11 +0200214 struct request_queue *q;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800215 struct omap_mbox_queue *mq;
216
217 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
218 if (!mq)
219 return NULL;
220
221 spin_lock_init(&mq->lock);
222
223 q = blk_init_queue(proc, &mq->lock);
224 if (!q)
225 goto error;
226 q->queuedata = mbox;
227 mq->queue = q;
228
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800229 if (work)
230 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800231
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800232 if (tasklet)
233 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800234 return mq;
235error:
236 kfree(mq);
237 return NULL;
238}
239
240static void mbox_queue_free(struct omap_mbox_queue *q)
241{
242 blk_cleanup_queue(q->queue);
243 kfree(q);
244}
245
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800246static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800247{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800248 int ret = 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800249 struct omap_mbox_queue *mq;
250
251 if (likely(mbox->ops->startup)) {
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800252 write_lock(&mboxes_lock);
253 if (!mbox_configured)
254 ret = mbox->ops->startup(mbox);
255
256 if (unlikely(ret)) {
257 write_unlock(&mboxes_lock);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800258 return ret;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800259 }
260 mbox_configured++;
261 write_unlock(&mboxes_lock);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800262 }
263
C A Subramaniam5e683822009-11-22 10:11:23 -0800264 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800265 mbox->name, mbox);
266 if (unlikely(ret)) {
267 printk(KERN_ERR
268 "failed to register mailbox interrupt:%d\n", ret);
269 goto fail_request_irq;
270 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800271
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800272 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800273 if (!mq) {
274 ret = -ENOMEM;
275 goto fail_alloc_txq;
276 }
277 mbox->txq = mq;
278
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800279 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800280 if (!mq) {
281 ret = -ENOMEM;
282 goto fail_alloc_rxq;
283 }
284 mbox->rxq = mq;
285
286 return 0;
287
288 fail_alloc_rxq:
289 mbox_queue_free(mbox->txq);
290 fail_alloc_txq:
291 free_irq(mbox->irq, mbox);
292 fail_request_irq:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800293 if (unlikely(mbox->ops->shutdown))
294 mbox->ops->shutdown(mbox);
295
296 return ret;
297}
298
299static void omap_mbox_fini(struct omap_mbox *mbox)
300{
Fernando Guzman Lugoad6d9622010-02-12 19:02:32 -0600301 free_irq(mbox->irq, mbox);
Fernando Guzman Lugo0e828e82010-02-12 19:07:14 -0600302 tasklet_kill(&mbox->txq->tasklet);
303 flush_work(&mbox->rxq->work);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800304 mbox_queue_free(mbox->txq);
305 mbox_queue_free(mbox->rxq);
306
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800307 if (unlikely(mbox->ops->shutdown)) {
308 write_lock(&mboxes_lock);
309 if (mbox_configured > 0)
310 mbox_configured--;
311 if (!mbox_configured)
312 mbox->ops->shutdown(mbox);
313 write_unlock(&mboxes_lock);
314 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800315}
316
317static struct omap_mbox **find_mboxes(const char *name)
318{
319 struct omap_mbox **p;
320
321 for (p = &mboxes; *p; p = &(*p)->next) {
322 if (strcmp((*p)->name, name) == 0)
323 break;
324 }
325
326 return p;
327}
328
329struct omap_mbox *omap_mbox_get(const char *name)
330{
331 struct omap_mbox *mbox;
332 int ret;
333
334 read_lock(&mboxes_lock);
335 mbox = *(find_mboxes(name));
336 if (mbox == NULL) {
337 read_unlock(&mboxes_lock);
338 return ERR_PTR(-ENOENT);
339 }
340
341 read_unlock(&mboxes_lock);
342
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800343 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800344 if (ret)
345 return ERR_PTR(-ENODEV);
346
347 return mbox;
348}
349EXPORT_SYMBOL(omap_mbox_get);
350
351void omap_mbox_put(struct omap_mbox *mbox)
352{
353 omap_mbox_fini(mbox);
354}
355EXPORT_SYMBOL(omap_mbox_put);
356
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700357int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800358{
359 int ret = 0;
360 struct omap_mbox **tmp;
361
362 if (!mbox)
363 return -EINVAL;
364 if (mbox->next)
365 return -EBUSY;
366
367 write_lock(&mboxes_lock);
368 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700369 if (*tmp) {
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800370 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700371 write_unlock(&mboxes_lock);
372 goto err_find;
373 }
374 *tmp = mbox;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800375 write_unlock(&mboxes_lock);
376
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700377 return 0;
378
379err_find:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800380 return ret;
381}
382EXPORT_SYMBOL(omap_mbox_register);
383
384int omap_mbox_unregister(struct omap_mbox *mbox)
385{
386 struct omap_mbox **tmp;
387
388 write_lock(&mboxes_lock);
389 tmp = &mboxes;
390 while (*tmp) {
391 if (mbox == *tmp) {
392 *tmp = mbox->next;
393 mbox->next = NULL;
394 write_unlock(&mboxes_lock);
395 return 0;
396 }
397 tmp = &(*tmp)->next;
398 }
399 write_unlock(&mboxes_lock);
400
401 return -EINVAL;
402}
403EXPORT_SYMBOL(omap_mbox_unregister);
404
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800405static int __init omap_mbox_init(void)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800406{
Rob Clark8250a5c2010-01-04 19:22:03 +0530407 mboxd = create_workqueue("mboxd");
408 if (!mboxd)
409 return -ENOMEM;
410
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800411 return 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800412}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800413module_init(omap_mbox_init);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800414
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800415static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800416{
Rob Clark8250a5c2010-01-04 19:22:03 +0530417 destroy_workqueue(mboxd);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800418}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800419module_exit(omap_mbox_exit);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800420
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700421MODULE_LICENSE("GPL v2");
422MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
423MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");