Mailbox: disable mailbox interrupt when request queue
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / plat-omap / mailbox.c
blobc3402165488dfed823705c6365897ef1e9b40402
1 /*
2 * OMAP mailbox driver
4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
30 #include <plat/mailbox.h>
32 static struct workqueue_struct *mboxd;
33 static struct omap_mbox *mboxes;
34 static DEFINE_RWLOCK(mboxes_lock);
35 static bool rq_full;
37 static int mbox_configured;
38 static DEFINE_MUTEX(mbox_configured_lock);
40 /* Mailbox FIFO handle functions */
41 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
43 return mbox->ops->fifo_read(mbox);
45 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
47 mbox->ops->fifo_write(mbox, msg);
49 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
51 return mbox->ops->fifo_empty(mbox);
53 static inline int mbox_fifo_full(struct omap_mbox *mbox)
55 return mbox->ops->fifo_full(mbox);
58 /* Mailbox IRQ handle functions */
59 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
61 if (mbox->ops->ack_irq)
62 mbox->ops->ack_irq(mbox, irq);
64 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
66 return mbox->ops->is_irq(mbox, irq);
70 * message sender
72 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
74 int ret = 0, i = 1000;
76 while (mbox_fifo_full(mbox)) {
77 if (mbox->ops->type == OMAP_MBOX_TYPE2)
78 return -1;
79 if (--i == 0)
80 return -1;
81 udelay(1);
83 mbox_fifo_write(mbox, msg);
84 return ret;
88 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
91 struct request *rq;
92 struct request_queue *q = mbox->txq->queue;
94 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
95 if (unlikely(!rq))
96 return -ENOMEM;
98 blk_insert_request(q, rq, 0, (void *) msg);
99 tasklet_schedule(&mbox->txq->tasklet);
101 return 0;
103 EXPORT_SYMBOL(omap_mbox_msg_send);
105 static void mbox_tx_tasklet(unsigned long tx_data)
107 int ret;
108 struct request *rq;
109 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
110 struct request_queue *q = mbox->txq->queue;
112 while (1) {
114 rq = blk_fetch_request(q);
116 if (!rq)
117 break;
119 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
120 if (ret) {
121 omap_mbox_enable_irq(mbox, IRQ_TX);
122 blk_requeue_request(q, rq);
123 return;
125 blk_end_request_all(rq, 0);
130 * Message receiver(workqueue)
132 static void mbox_rx_work(struct work_struct *work)
134 struct omap_mbox_queue *mq =
135 container_of(work, struct omap_mbox_queue, work);
136 struct omap_mbox *mbox = mq->queue->queuedata;
137 struct request_queue *q = mbox->rxq->queue;
138 struct request *rq;
139 mbox_msg_t msg;
140 unsigned long flags;
142 while (1) {
143 spin_lock_irqsave(q->queue_lock, flags);
144 rq = blk_fetch_request(q);
145 if (rq_full) {
146 omap_mbox_enable_irq(mbox, IRQ_RX);
147 rq_full = false;
149 spin_unlock_irqrestore(q->queue_lock, flags);
150 if (!rq)
151 break;
153 msg = (mbox_msg_t)rq->special;
154 blk_end_request_all(rq, 0);
155 if (mbox->rxq->callback)
156 mbox->rxq->callback((void *)msg);
161 * Mailbox interrupt handler
163 static void mbox_txq_fn(struct request_queue *q)
167 static void mbox_rxq_fn(struct request_queue *q)
171 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
173 omap_mbox_disable_irq(mbox, IRQ_TX);
174 ack_mbox_irq(mbox, IRQ_TX);
175 tasklet_schedule(&mbox->txq->tasklet);
178 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
180 struct request *rq;
181 mbox_msg_t msg;
182 struct request_queue *q = mbox->rxq->queue;
184 while (!mbox_fifo_empty(mbox)) {
185 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
186 if (unlikely(!rq)) {
187 omap_mbox_disable_irq(mbox, IRQ_RX);
188 rq_full = true;
189 goto nomem;
192 msg = mbox_fifo_read(mbox);
195 blk_insert_request(q, rq, 0, (void *)msg);
196 if (mbox->ops->type == OMAP_MBOX_TYPE1)
197 break;
200 /* no more messages in the fifo. clear IRQ source. */
201 ack_mbox_irq(mbox, IRQ_RX);
202 nomem:
203 queue_work(mboxd, &mbox->rxq->work);
206 static irqreturn_t mbox_interrupt(int irq, void *p)
208 struct omap_mbox *mbox = p;
210 if (is_mbox_irq(mbox, IRQ_TX))
211 __mbox_tx_interrupt(mbox);
213 if (is_mbox_irq(mbox, IRQ_RX))
214 __mbox_rx_interrupt(mbox);
216 return IRQ_HANDLED;
219 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
220 request_fn_proc *proc,
221 void (*work) (struct work_struct *),
222 void (*tasklet)(unsigned long))
224 struct request_queue *q;
225 struct omap_mbox_queue *mq;
227 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
228 if (!mq)
229 return NULL;
231 spin_lock_init(&mq->lock);
233 q = blk_init_queue(proc, &mq->lock);
234 if (!q)
235 goto error;
236 q->queuedata = mbox;
237 mq->queue = q;
239 if (work)
240 INIT_WORK(&mq->work, work);
242 if (tasklet)
243 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
244 return mq;
245 error:
246 kfree(mq);
247 return NULL;
250 static void mbox_queue_free(struct omap_mbox_queue *q)
252 blk_cleanup_queue(q->queue);
253 kfree(q);
256 static int omap_mbox_startup(struct omap_mbox *mbox)
258 int ret = 0;
259 struct omap_mbox_queue *mq;
261 if (likely(mbox->ops->startup)) {
262 mutex_lock(&mbox_configured_lock);
263 if (!mbox_configured)
264 ret = mbox->ops->startup(mbox);
266 if (unlikely(ret)) {
267 mutex_unlock(&mbox_configured_lock);
268 return ret;
270 mbox_configured++;
271 mutex_unlock(&mbox_configured_lock);
274 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
275 mbox->name, mbox);
276 if (unlikely(ret)) {
277 printk(KERN_ERR
278 "failed to register mailbox interrupt:%d\n", ret);
279 goto fail_request_irq;
282 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
283 if (!mq) {
284 ret = -ENOMEM;
285 goto fail_alloc_txq;
287 mbox->txq = mq;
289 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
290 if (!mq) {
291 ret = -ENOMEM;
292 goto fail_alloc_rxq;
294 mbox->rxq = mq;
296 return 0;
298 fail_alloc_rxq:
299 mbox_queue_free(mbox->txq);
300 fail_alloc_txq:
301 free_irq(mbox->irq, mbox);
302 fail_request_irq:
303 if (unlikely(mbox->ops->shutdown))
304 mbox->ops->shutdown(mbox);
306 return ret;
309 static void omap_mbox_fini(struct omap_mbox *mbox)
311 free_irq(mbox->irq, mbox);
312 tasklet_kill(&mbox->txq->tasklet);
313 flush_work(&mbox->rxq->work);
314 mbox_queue_free(mbox->txq);
315 mbox_queue_free(mbox->rxq);
317 if (unlikely(mbox->ops->shutdown)) {
318 mutex_lock(&mbox_configured_lock);
319 if (mbox_configured > 0)
320 mbox_configured--;
321 if (!mbox_configured)
322 mbox->ops->shutdown(mbox);
323 mutex_unlock(&mbox_configured_lock);
327 static struct omap_mbox **find_mboxes(const char *name)
329 struct omap_mbox **p;
331 for (p = &mboxes; *p; p = &(*p)->next) {
332 if (strcmp((*p)->name, name) == 0)
333 break;
336 return p;
339 struct omap_mbox *omap_mbox_get(const char *name)
341 struct omap_mbox *mbox;
342 int ret;
344 read_lock(&mboxes_lock);
345 mbox = *(find_mboxes(name));
346 if (mbox == NULL) {
347 read_unlock(&mboxes_lock);
348 return ERR_PTR(-ENOENT);
351 read_unlock(&mboxes_lock);
353 ret = omap_mbox_startup(mbox);
354 if (ret)
355 return ERR_PTR(-ENODEV);
357 return mbox;
359 EXPORT_SYMBOL(omap_mbox_get);
361 void omap_mbox_put(struct omap_mbox *mbox)
363 omap_mbox_fini(mbox);
365 EXPORT_SYMBOL(omap_mbox_put);
367 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
369 int ret = 0;
370 struct omap_mbox **tmp;
372 if (!mbox)
373 return -EINVAL;
374 if (mbox->next)
375 return -EBUSY;
377 write_lock(&mboxes_lock);
378 tmp = find_mboxes(mbox->name);
379 if (*tmp) {
380 ret = -EBUSY;
381 write_unlock(&mboxes_lock);
382 goto err_find;
384 *tmp = mbox;
385 write_unlock(&mboxes_lock);
387 return 0;
389 err_find:
390 return ret;
392 EXPORT_SYMBOL(omap_mbox_register);
394 int omap_mbox_unregister(struct omap_mbox *mbox)
396 struct omap_mbox **tmp;
398 write_lock(&mboxes_lock);
399 tmp = &mboxes;
400 while (*tmp) {
401 if (mbox == *tmp) {
402 *tmp = mbox->next;
403 mbox->next = NULL;
404 write_unlock(&mboxes_lock);
405 return 0;
407 tmp = &(*tmp)->next;
409 write_unlock(&mboxes_lock);
411 return -EINVAL;
413 EXPORT_SYMBOL(omap_mbox_unregister);
415 static int __init omap_mbox_init(void)
417 mboxd = create_workqueue("mboxd");
418 if (!mboxd)
419 return -ENOMEM;
421 return 0;
423 module_init(omap_mbox_init);
425 static void __exit omap_mbox_exit(void)
427 destroy_workqueue(mboxd);
429 module_exit(omap_mbox_exit);
431 MODULE_LICENSE("GPL v2");
432 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
433 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");