OMAP clock: drop .id field; ensure each clock has a unique name
[linux-2.6/x86.git] / arch / arm / plat-omap / mailbox.c
blob8e90633e4cb93e686671100bd1eefeaf54518e7a
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>
29 #include <plat/mailbox.h>
31 static struct omap_mbox *mboxes;
32 static DEFINE_RWLOCK(mboxes_lock);
34 static int mbox_configured;
36 /* Mailbox FIFO handle functions */
37 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
39 return mbox->ops->fifo_read(mbox);
41 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
43 mbox->ops->fifo_write(mbox, msg);
45 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
47 return mbox->ops->fifo_empty(mbox);
49 static inline int mbox_fifo_full(struct omap_mbox *mbox)
51 return mbox->ops->fifo_full(mbox);
54 /* Mailbox IRQ handle functions */
55 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
57 if (mbox->ops->ack_irq)
58 mbox->ops->ack_irq(mbox, irq);
60 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
62 return mbox->ops->is_irq(mbox, irq);
66 * message sender
68 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
70 int ret = 0, i = 1000;
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);
79 mbox_fifo_write(mbox, msg);
80 return ret;
84 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
87 struct request *rq;
88 struct request_queue *q = mbox->txq->queue;
90 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
91 if (unlikely(!rq))
92 return -ENOMEM;
94 blk_insert_request(q, rq, 0, (void *) msg);
95 tasklet_schedule(&mbox->txq->tasklet);
97 return 0;
99 EXPORT_SYMBOL(omap_mbox_msg_send);
101 static void mbox_tx_tasklet(unsigned long tx_data)
103 int ret;
104 struct request *rq;
105 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
106 struct request_queue *q = mbox->txq->queue;
108 while (1) {
110 rq = blk_fetch_request(q);
112 if (!rq)
113 break;
115 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
116 if (ret) {
117 omap_mbox_enable_irq(mbox, IRQ_TX);
118 blk_requeue_request(q, rq);
119 return;
121 blk_end_request_all(rq, 0);
126 * Message receiver(workqueue)
128 static void mbox_rx_work(struct work_struct *work)
130 struct omap_mbox_queue *mq =
131 container_of(work, struct omap_mbox_queue, work);
132 struct omap_mbox *mbox = mq->queue->queuedata;
133 struct request_queue *q = mbox->rxq->queue;
134 struct request *rq;
135 mbox_msg_t msg;
136 unsigned long flags;
138 while (1) {
139 spin_lock_irqsave(q->queue_lock, flags);
140 rq = blk_fetch_request(q);
141 spin_unlock_irqrestore(q->queue_lock, flags);
142 if (!rq)
143 break;
145 msg = (mbox_msg_t)rq->special;
146 blk_end_request_all(rq, 0);
147 mbox->rxq->callback((void *)msg);
152 * Mailbox interrupt handler
154 static void mbox_txq_fn(struct request_queue *q)
158 static void mbox_rxq_fn(struct request_queue *q)
162 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
164 omap_mbox_disable_irq(mbox, IRQ_TX);
165 ack_mbox_irq(mbox, IRQ_TX);
166 tasklet_schedule(&mbox->txq->tasklet);
169 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
171 struct request *rq;
172 mbox_msg_t msg;
173 struct request_queue *q = mbox->rxq->queue;
175 while (!mbox_fifo_empty(mbox)) {
176 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
177 if (unlikely(!rq))
178 goto nomem;
180 msg = mbox_fifo_read(mbox);
183 blk_insert_request(q, rq, 0, (void *)msg);
184 if (mbox->ops->type == OMAP_MBOX_TYPE1)
185 break;
188 /* no more messages in the fifo. clear IRQ source. */
189 ack_mbox_irq(mbox, IRQ_RX);
190 nomem:
191 schedule_work(&mbox->rxq->work);
194 static irqreturn_t mbox_interrupt(int irq, void *p)
196 struct omap_mbox *mbox = p;
198 if (is_mbox_irq(mbox, IRQ_TX))
199 __mbox_tx_interrupt(mbox);
201 if (is_mbox_irq(mbox, IRQ_RX))
202 __mbox_rx_interrupt(mbox);
204 return IRQ_HANDLED;
207 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
208 request_fn_proc *proc,
209 void (*work) (struct work_struct *),
210 void (*tasklet)(unsigned long))
212 struct request_queue *q;
213 struct omap_mbox_queue *mq;
215 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
216 if (!mq)
217 return NULL;
219 spin_lock_init(&mq->lock);
221 q = blk_init_queue(proc, &mq->lock);
222 if (!q)
223 goto error;
224 q->queuedata = mbox;
225 mq->queue = q;
227 if (work)
228 INIT_WORK(&mq->work, work);
230 if (tasklet)
231 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
232 return mq;
233 error:
234 kfree(mq);
235 return NULL;
238 static void mbox_queue_free(struct omap_mbox_queue *q)
240 blk_cleanup_queue(q->queue);
241 kfree(q);
244 static int omap_mbox_startup(struct omap_mbox *mbox)
246 int ret = 0;
247 struct omap_mbox_queue *mq;
249 if (likely(mbox->ops->startup)) {
250 write_lock(&mboxes_lock);
251 if (!mbox_configured)
252 ret = mbox->ops->startup(mbox);
254 if (unlikely(ret)) {
255 write_unlock(&mboxes_lock);
256 return ret;
258 mbox_configured++;
259 write_unlock(&mboxes_lock);
262 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
263 mbox->name, mbox);
264 if (unlikely(ret)) {
265 printk(KERN_ERR
266 "failed to register mailbox interrupt:%d\n", ret);
267 goto fail_request_irq;
270 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
271 if (!mq) {
272 ret = -ENOMEM;
273 goto fail_alloc_txq;
275 mbox->txq = mq;
277 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
278 if (!mq) {
279 ret = -ENOMEM;
280 goto fail_alloc_rxq;
282 mbox->rxq = mq;
284 return 0;
286 fail_alloc_rxq:
287 mbox_queue_free(mbox->txq);
288 fail_alloc_txq:
289 free_irq(mbox->irq, mbox);
290 fail_request_irq:
291 if (unlikely(mbox->ops->shutdown))
292 mbox->ops->shutdown(mbox);
294 return ret;
297 static void omap_mbox_fini(struct omap_mbox *mbox)
299 mbox_queue_free(mbox->txq);
300 mbox_queue_free(mbox->rxq);
302 free_irq(mbox->irq, mbox);
304 if (unlikely(mbox->ops->shutdown)) {
305 write_lock(&mboxes_lock);
306 if (mbox_configured > 0)
307 mbox_configured--;
308 if (!mbox_configured)
309 mbox->ops->shutdown(mbox);
310 write_unlock(&mboxes_lock);
314 static struct omap_mbox **find_mboxes(const char *name)
316 struct omap_mbox **p;
318 for (p = &mboxes; *p; p = &(*p)->next) {
319 if (strcmp((*p)->name, name) == 0)
320 break;
323 return p;
326 struct omap_mbox *omap_mbox_get(const char *name)
328 struct omap_mbox *mbox;
329 int ret;
331 read_lock(&mboxes_lock);
332 mbox = *(find_mboxes(name));
333 if (mbox == NULL) {
334 read_unlock(&mboxes_lock);
335 return ERR_PTR(-ENOENT);
338 read_unlock(&mboxes_lock);
340 ret = omap_mbox_startup(mbox);
341 if (ret)
342 return ERR_PTR(-ENODEV);
344 return mbox;
346 EXPORT_SYMBOL(omap_mbox_get);
348 void omap_mbox_put(struct omap_mbox *mbox)
350 omap_mbox_fini(mbox);
352 EXPORT_SYMBOL(omap_mbox_put);
354 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
356 int ret = 0;
357 struct omap_mbox **tmp;
359 if (!mbox)
360 return -EINVAL;
361 if (mbox->next)
362 return -EBUSY;
364 write_lock(&mboxes_lock);
365 tmp = find_mboxes(mbox->name);
366 if (*tmp) {
367 ret = -EBUSY;
368 write_unlock(&mboxes_lock);
369 goto err_find;
371 *tmp = mbox;
372 write_unlock(&mboxes_lock);
374 return 0;
376 err_find:
377 return ret;
379 EXPORT_SYMBOL(omap_mbox_register);
381 int omap_mbox_unregister(struct omap_mbox *mbox)
383 struct omap_mbox **tmp;
385 write_lock(&mboxes_lock);
386 tmp = &mboxes;
387 while (*tmp) {
388 if (mbox == *tmp) {
389 *tmp = mbox->next;
390 mbox->next = NULL;
391 write_unlock(&mboxes_lock);
392 return 0;
394 tmp = &(*tmp)->next;
396 write_unlock(&mboxes_lock);
398 return -EINVAL;
400 EXPORT_SYMBOL(omap_mbox_unregister);
402 static int __init omap_mbox_init(void)
404 return 0;
406 module_init(omap_mbox_init);
408 static void __exit omap_mbox_exit(void)
411 module_exit(omap_mbox_exit);
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
415 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");