blob: 4229cec531406df095055734ef955ae97152fc89 [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>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070028
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/mailbox.h>
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080030
Rob Clark8250a5c2010-01-04 19:22:03 +053031static struct workqueue_struct *mboxd;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080032static struct omap_mbox *mboxes;
33static DEFINE_RWLOCK(mboxes_lock);
34
C A Subramaniam5f00ec62009-11-22 10:11:22 -080035static int mbox_configured;
36
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070037/* Mailbox FIFO handle functions */
38static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
39{
40 return mbox->ops->fifo_read(mbox);
41}
42static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
43{
44 mbox->ops->fifo_write(mbox, msg);
45}
46static inline int mbox_fifo_empty(struct omap_mbox *mbox)
47{
48 return mbox->ops->fifo_empty(mbox);
49}
50static inline int mbox_fifo_full(struct omap_mbox *mbox)
51{
52 return mbox->ops->fifo_full(mbox);
53}
54
55/* Mailbox IRQ handle functions */
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070056static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
57{
58 if (mbox->ops->ack_irq)
59 mbox->ops->ack_irq(mbox, irq);
60}
61static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
62{
63 return mbox->ops->is_irq(mbox, irq);
64}
65
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080066/*
67 * message sender
68 */
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -080069static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080070{
71 int ret = 0, i = 1000;
72
73 while (mbox_fifo_full(mbox)) {
74 if (mbox->ops->type == OMAP_MBOX_TYPE2)
75 return -1;
76 if (--i == 0)
77 return -1;
78 udelay(1);
79 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080080 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080081 return ret;
82}
83
Tejun Heoec247512009-04-23 11:05:20 +090084
C A Subramaniamb2b63622009-11-22 10:11:20 -080085int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080086{
C A Subramaniam5ed8d322009-11-22 10:11:24 -080087
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080088 struct request *rq;
89 struct request_queue *q = mbox->txq->queue;
Tejun Heoec247512009-04-23 11:05:20 +090090
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080091 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
C A Subramaniam5ed8d322009-11-22 10:11:24 -080092 if (unlikely(!rq))
Tejun Heoec247512009-04-23 11:05:20 +090093 return -ENOMEM;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080094
C A Subramaniam5ed8d322009-11-22 10:11:24 -080095 blk_insert_request(q, rq, 0, (void *) msg);
96 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080097
Tejun Heoec247512009-04-23 11:05:20 +090098 return 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -080099}
100EXPORT_SYMBOL(omap_mbox_msg_send);
101
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800102static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800103{
104 int ret;
105 struct request *rq;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800106 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800107 struct request_queue *q = mbox->txq->queue;
108
109 while (1) {
Tejun Heoec247512009-04-23 11:05:20 +0900110
Tejun Heo9934c8c2009-05-08 11:54:16 +0900111 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800112
113 if (!rq)
114 break;
115
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800116 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800117 if (ret) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800118 omap_mbox_enable_irq(mbox, IRQ_TX);
Tejun Heo296b2f62009-05-08 11:54:15 +0900119 blk_requeue_request(q, rq);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800120 return;
121 }
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800122 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800123 }
124}
125
126/*
127 * Message receiver(workqueue)
128 */
129static void mbox_rx_work(struct work_struct *work)
130{
131 struct omap_mbox_queue *mq =
132 container_of(work, struct omap_mbox_queue, work);
133 struct omap_mbox *mbox = mq->queue->queuedata;
134 struct request_queue *q = mbox->rxq->queue;
135 struct request *rq;
136 mbox_msg_t msg;
137 unsigned long flags;
138
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800139 while (1) {
140 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900141 rq = blk_fetch_request(q);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800142 spin_unlock_irqrestore(q->queue_lock, flags);
143 if (!rq)
144 break;
145
Tejun Heoec247512009-04-23 11:05:20 +0900146 msg = (mbox_msg_t)rq->special;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900147 blk_end_request_all(rq, 0);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800148 mbox->rxq->callback((void *)msg);
149 }
150}
151
152/*
153 * Mailbox interrupt handler
154 */
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800155static void mbox_txq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800156{
157}
158
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800159static void mbox_rxq_fn(struct request_queue *q)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800160{
161}
162
163static void __mbox_tx_interrupt(struct omap_mbox *mbox)
164{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800165 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800166 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800167 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800168}
169
170static void __mbox_rx_interrupt(struct omap_mbox *mbox)
171{
172 struct request *rq;
173 mbox_msg_t msg;
Jens Axboe165125e2007-07-24 09:28:11 +0200174 struct request_queue *q = mbox->rxq->queue;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800175
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800176 while (!mbox_fifo_empty(mbox)) {
177 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
178 if (unlikely(!rq))
179 goto nomem;
180
181 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800182
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800183
Tejun Heoec247512009-04-23 11:05:20 +0900184 blk_insert_request(q, rq, 0, (void *)msg);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800185 if (mbox->ops->type == OMAP_MBOX_TYPE1)
186 break;
187 }
188
189 /* no more messages in the fifo. clear IRQ source. */
190 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700191nomem:
Rob Clark8250a5c2010-01-04 19:22:03 +0530192 queue_work(mboxd, &mbox->rxq->work);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800193}
194
195static irqreturn_t mbox_interrupt(int irq, void *p)
196{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400197 struct omap_mbox *mbox = p;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800198
199 if (is_mbox_irq(mbox, IRQ_TX))
200 __mbox_tx_interrupt(mbox);
201
202 if (is_mbox_irq(mbox, IRQ_RX))
203 __mbox_rx_interrupt(mbox);
204
205 return IRQ_HANDLED;
206}
207
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800208static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Hiroshi DOYUbfe1f6a2009-11-22 10:11:18 -0800209 request_fn_proc *proc,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800210 void (*work) (struct work_struct *),
211 void (*tasklet)(unsigned long))
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800212{
Jens Axboe165125e2007-07-24 09:28:11 +0200213 struct request_queue *q;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800214 struct omap_mbox_queue *mq;
215
216 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
217 if (!mq)
218 return NULL;
219
220 spin_lock_init(&mq->lock);
221
222 q = blk_init_queue(proc, &mq->lock);
223 if (!q)
224 goto error;
225 q->queuedata = mbox;
226 mq->queue = q;
227
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800228 if (work)
229 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800230
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800231 if (tasklet)
232 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800233 return mq;
234error:
235 kfree(mq);
236 return NULL;
237}
238
239static void mbox_queue_free(struct omap_mbox_queue *q)
240{
241 blk_cleanup_queue(q->queue);
242 kfree(q);
243}
244
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800245static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800246{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800247 int ret = 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800248 struct omap_mbox_queue *mq;
249
250 if (likely(mbox->ops->startup)) {
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800251 write_lock(&mboxes_lock);
252 if (!mbox_configured)
253 ret = mbox->ops->startup(mbox);
254
255 if (unlikely(ret)) {
256 write_unlock(&mboxes_lock);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800257 return ret;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800258 }
259 mbox_configured++;
260 write_unlock(&mboxes_lock);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800261 }
262
C A Subramaniam5e683822009-11-22 10:11:23 -0800263 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800264 mbox->name, mbox);
265 if (unlikely(ret)) {
266 printk(KERN_ERR
267 "failed to register mailbox interrupt:%d\n", ret);
268 goto fail_request_irq;
269 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800270
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800271 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800272 if (!mq) {
273 ret = -ENOMEM;
274 goto fail_alloc_txq;
275 }
276 mbox->txq = mq;
277
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800278 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800279 if (!mq) {
280 ret = -ENOMEM;
281 goto fail_alloc_rxq;
282 }
283 mbox->rxq = mq;
284
285 return 0;
286
287 fail_alloc_rxq:
288 mbox_queue_free(mbox->txq);
289 fail_alloc_txq:
290 free_irq(mbox->irq, mbox);
291 fail_request_irq:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800292 if (unlikely(mbox->ops->shutdown))
293 mbox->ops->shutdown(mbox);
294
295 return ret;
296}
297
298static void omap_mbox_fini(struct omap_mbox *mbox)
299{
300 mbox_queue_free(mbox->txq);
301 mbox_queue_free(mbox->rxq);
302
303 free_irq(mbox->irq, mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800304
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800305 if (unlikely(mbox->ops->shutdown)) {
306 write_lock(&mboxes_lock);
307 if (mbox_configured > 0)
308 mbox_configured--;
309 if (!mbox_configured)
310 mbox->ops->shutdown(mbox);
311 write_unlock(&mboxes_lock);
312 }
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800313}
314
315static struct omap_mbox **find_mboxes(const char *name)
316{
317 struct omap_mbox **p;
318
319 for (p = &mboxes; *p; p = &(*p)->next) {
320 if (strcmp((*p)->name, name) == 0)
321 break;
322 }
323
324 return p;
325}
326
327struct omap_mbox *omap_mbox_get(const char *name)
328{
329 struct omap_mbox *mbox;
330 int ret;
331
332 read_lock(&mboxes_lock);
333 mbox = *(find_mboxes(name));
334 if (mbox == NULL) {
335 read_unlock(&mboxes_lock);
336 return ERR_PTR(-ENOENT);
337 }
338
339 read_unlock(&mboxes_lock);
340
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800341 ret = omap_mbox_startup(mbox);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800342 if (ret)
343 return ERR_PTR(-ENODEV);
344
345 return mbox;
346}
347EXPORT_SYMBOL(omap_mbox_get);
348
349void omap_mbox_put(struct omap_mbox *mbox)
350{
351 omap_mbox_fini(mbox);
352}
353EXPORT_SYMBOL(omap_mbox_put);
354
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700355int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800356{
357 int ret = 0;
358 struct omap_mbox **tmp;
359
360 if (!mbox)
361 return -EINVAL;
362 if (mbox->next)
363 return -EBUSY;
364
365 write_lock(&mboxes_lock);
366 tmp = find_mboxes(mbox->name);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700367 if (*tmp) {
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800368 ret = -EBUSY;
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700369 write_unlock(&mboxes_lock);
370 goto err_find;
371 }
372 *tmp = mbox;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800373 write_unlock(&mboxes_lock);
374
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700375 return 0;
376
377err_find:
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800378 return ret;
379}
380EXPORT_SYMBOL(omap_mbox_register);
381
382int omap_mbox_unregister(struct omap_mbox *mbox)
383{
384 struct omap_mbox **tmp;
385
386 write_lock(&mboxes_lock);
387 tmp = &mboxes;
388 while (*tmp) {
389 if (mbox == *tmp) {
390 *tmp = mbox->next;
391 mbox->next = NULL;
392 write_unlock(&mboxes_lock);
393 return 0;
394 }
395 tmp = &(*tmp)->next;
396 }
397 write_unlock(&mboxes_lock);
398
399 return -EINVAL;
400}
401EXPORT_SYMBOL(omap_mbox_unregister);
402
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800403static int __init omap_mbox_init(void)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800404{
Rob Clark8250a5c2010-01-04 19:22:03 +0530405 mboxd = create_workqueue("mboxd");
406 if (!mboxd)
407 return -ENOMEM;
408
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800409 return 0;
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800410}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800411module_init(omap_mbox_init);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800412
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800413static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800414{
Rob Clark8250a5c2010-01-04 19:22:03 +0530415 destroy_workqueue(mboxd);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800416}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800417module_exit(omap_mbox_exit);
Hiroshi DOYU340a614a2006-12-07 15:43:59 -0800418
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700419MODULE_LICENSE("GPL v2");
420MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
421MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");