include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / arch / arm / plat-omap / mailbox.c
CommitLineData
340a614a
HD
1/*
2 * OMAP mailbox driver
3 *
f48cca87 4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
340a614a 5 *
f48cca87 6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
340a614a
HD
7 *
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
340a614a 24#include <linux/module.h>
340a614a
HD
25#include <linux/interrupt.h>
26#include <linux/device.h>
340a614a 27#include <linux/delay.h>
5a0e3ad6 28#include <linux/slab.h>
8dff0fa5 29
ce491cf8 30#include <plat/mailbox.h>
340a614a 31
8250a5c3 32static struct workqueue_struct *mboxd;
340a614a
HD
33static struct omap_mbox *mboxes;
34static DEFINE_RWLOCK(mboxes_lock);
35
5f00ec64
S
36static int mbox_configured;
37
9ae0ee00
HD
38/* 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 */
9ae0ee00
HD
57static 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
340a614a
HD
67/*
68 * message sender
69 */
c7c158e5 70static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a
HD
71{
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 }
340a614a 81 mbox_fifo_write(mbox, msg);
340a614a
HD
82 return ret;
83}
84
ec24751a 85
b2b6362e 86int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a 87{
5ed8d32e 88
340a614a
HD
89 struct request *rq;
90 struct request_queue *q = mbox->txq->queue;
ec24751a 91
340a614a 92 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
5ed8d32e 93 if (unlikely(!rq))
ec24751a 94 return -ENOMEM;
340a614a 95
5ed8d32e
S
96 blk_insert_request(q, rq, 0, (void *) msg);
97 tasklet_schedule(&mbox->txq->tasklet);
340a614a 98
ec24751a 99 return 0;
340a614a
HD
100}
101EXPORT_SYMBOL(omap_mbox_msg_send);
102
5ed8d32e 103static void mbox_tx_tasklet(unsigned long tx_data)
340a614a
HD
104{
105 int ret;
106 struct request *rq;
5ed8d32e 107 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
340a614a
HD
108 struct request_queue *q = mbox->txq->queue;
109
110 while (1) {
ec24751a 111
9934c8c0 112 rq = blk_fetch_request(q);
340a614a
HD
113
114 if (!rq)
115 break;
116
5ed8d32e 117 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
340a614a 118 if (ret) {
eb18858e 119 omap_mbox_enable_irq(mbox, IRQ_TX);
296b2f6a 120 blk_requeue_request(q, rq);
340a614a
HD
121 return;
122 }
5ed8d32e 123 blk_end_request_all(rq, 0);
340a614a
HD
124 }
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
340a614a
HD
140 while (1) {
141 spin_lock_irqsave(q->queue_lock, flags);
9934c8c0 142 rq = blk_fetch_request(q);
340a614a
HD
143 spin_unlock_irqrestore(q->queue_lock, flags);
144 if (!rq)
145 break;
146
ec24751a 147 msg = (mbox_msg_t)rq->special;
40cbbb78 148 blk_end_request_all(rq, 0);
340a614a
HD
149 mbox->rxq->callback((void *)msg);
150 }
151}
152
153/*
154 * Mailbox interrupt handler
155 */
bfe1f6ac 156static void mbox_txq_fn(struct request_queue *q)
340a614a
HD
157{
158}
159
bfe1f6ac 160static void mbox_rxq_fn(struct request_queue *q)
340a614a
HD
161{
162}
163
164static void __mbox_tx_interrupt(struct omap_mbox *mbox)
165{
eb18858e 166 omap_mbox_disable_irq(mbox, IRQ_TX);
340a614a 167 ack_mbox_irq(mbox, IRQ_TX);
5ed8d32e 168 tasklet_schedule(&mbox->txq->tasklet);
340a614a
HD
169}
170
171static void __mbox_rx_interrupt(struct omap_mbox *mbox)
172{
173 struct request *rq;
174 mbox_msg_t msg;
165125e1 175 struct request_queue *q = mbox->rxq->queue;
340a614a 176
340a614a
HD
177 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);
340a614a 183
340a614a 184
ec24751a 185 blk_insert_request(q, rq, 0, (void *)msg);
340a614a
HD
186 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);
f48cca87 192nomem:
8250a5c3 193 queue_work(mboxd, &mbox->rxq->work);
340a614a
HD
194}
195
196static irqreturn_t mbox_interrupt(int irq, void *p)
197{
2a7057e3 198 struct omap_mbox *mbox = p;
340a614a
HD
199
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
340a614a 209static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
bfe1f6ac 210 request_fn_proc *proc,
5ed8d32e
S
211 void (*work) (struct work_struct *),
212 void (*tasklet)(unsigned long))
340a614a 213{
165125e1 214 struct request_queue *q;
340a614a
HD
215 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
5ed8d32e
S
229 if (work)
230 INIT_WORK(&mq->work, work);
340a614a 231
5ed8d32e
S
232 if (tasklet)
233 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
340a614a
HD
234 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
c7c158e5 246static int omap_mbox_startup(struct omap_mbox *mbox)
340a614a 247{
5f00ec64 248 int ret = 0;
340a614a
HD
249 struct omap_mbox_queue *mq;
250
251 if (likely(mbox->ops->startup)) {
5f00ec64
S
252 write_lock(&mboxes_lock);
253 if (!mbox_configured)
254 ret = mbox->ops->startup(mbox);
255
256 if (unlikely(ret)) {
257 write_unlock(&mboxes_lock);
340a614a 258 return ret;
5f00ec64
S
259 }
260 mbox_configured++;
261 write_unlock(&mboxes_lock);
340a614a
HD
262 }
263
5e683825 264 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
340a614a
HD
265 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 }
340a614a 271
5ed8d32e 272 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
340a614a
HD
273 if (!mq) {
274 ret = -ENOMEM;
275 goto fail_alloc_txq;
276 }
277 mbox->txq = mq;
278
5ed8d32e 279 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
340a614a
HD
280 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:
340a614a
HD
293 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{
301 mbox_queue_free(mbox->txq);
302 mbox_queue_free(mbox->rxq);
303
304 free_irq(mbox->irq, mbox);
340a614a 305
5f00ec64
S
306 if (unlikely(mbox->ops->shutdown)) {
307 write_lock(&mboxes_lock);
308 if (mbox_configured > 0)
309 mbox_configured--;
310 if (!mbox_configured)
311 mbox->ops->shutdown(mbox);
312 write_unlock(&mboxes_lock);
313 }
340a614a
HD
314}
315
316static struct omap_mbox **find_mboxes(const char *name)
317{
318 struct omap_mbox **p;
319
320 for (p = &mboxes; *p; p = &(*p)->next) {
321 if (strcmp((*p)->name, name) == 0)
322 break;
323 }
324
325 return p;
326}
327
328struct omap_mbox *omap_mbox_get(const char *name)
329{
330 struct omap_mbox *mbox;
331 int ret;
332
333 read_lock(&mboxes_lock);
334 mbox = *(find_mboxes(name));
335 if (mbox == NULL) {
336 read_unlock(&mboxes_lock);
337 return ERR_PTR(-ENOENT);
338 }
339
340 read_unlock(&mboxes_lock);
341
c7c158e5 342 ret = omap_mbox_startup(mbox);
340a614a
HD
343 if (ret)
344 return ERR_PTR(-ENODEV);
345
346 return mbox;
347}
348EXPORT_SYMBOL(omap_mbox_get);
349
350void omap_mbox_put(struct omap_mbox *mbox)
351{
352 omap_mbox_fini(mbox);
353}
354EXPORT_SYMBOL(omap_mbox_put);
355
f48cca87 356int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
340a614a
HD
357{
358 int ret = 0;
359 struct omap_mbox **tmp;
360
361 if (!mbox)
362 return -EINVAL;
363 if (mbox->next)
364 return -EBUSY;
365
366 write_lock(&mboxes_lock);
367 tmp = find_mboxes(mbox->name);
f48cca87 368 if (*tmp) {
340a614a 369 ret = -EBUSY;
f48cca87
HD
370 write_unlock(&mboxes_lock);
371 goto err_find;
372 }
373 *tmp = mbox;
340a614a
HD
374 write_unlock(&mboxes_lock);
375
f48cca87
HD
376 return 0;
377
378err_find:
340a614a
HD
379 return ret;
380}
381EXPORT_SYMBOL(omap_mbox_register);
382
383int omap_mbox_unregister(struct omap_mbox *mbox)
384{
385 struct omap_mbox **tmp;
386
387 write_lock(&mboxes_lock);
388 tmp = &mboxes;
389 while (*tmp) {
390 if (mbox == *tmp) {
391 *tmp = mbox->next;
392 mbox->next = NULL;
393 write_unlock(&mboxes_lock);
394 return 0;
395 }
396 tmp = &(*tmp)->next;
397 }
398 write_unlock(&mboxes_lock);
399
400 return -EINVAL;
401}
402EXPORT_SYMBOL(omap_mbox_unregister);
403
c7c158e5 404static int __init omap_mbox_init(void)
340a614a 405{
8250a5c3
RC
406 mboxd = create_workqueue("mboxd");
407 if (!mboxd)
408 return -ENOMEM;
409
c7c158e5 410 return 0;
340a614a 411}
c7c158e5 412module_init(omap_mbox_init);
340a614a 413
c7c158e5 414static void __exit omap_mbox_exit(void)
340a614a 415{
8250a5c3 416 destroy_workqueue(mboxd);
340a614a 417}
c7c158e5 418module_exit(omap_mbox_exit);
340a614a 419
f48cca87
HD
420MODULE_LICENSE("GPL v2");
421MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
422MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");