]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/arm/plat-omap/mailbox.c
omap: mailbox: remove (un)likely macros from cold paths
[mv-sheeva.git] / arch / arm / plat-omap / mailbox.c
1 /*
2  * OMAP mailbox driver
3  *
4  * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
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
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>
29
30 #include <plat/mailbox.h>
31
32 static struct workqueue_struct *mboxd;
33 static struct omap_mbox *mboxes;
34 static DEFINE_SPINLOCK(mboxes_lock);
35 static bool rq_full;
36
37 static int mbox_configured;
38 static DEFINE_MUTEX(mbox_configured_lock);
39
40 /* Mailbox FIFO handle functions */
41 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
42 {
43         return mbox->ops->fifo_read(mbox);
44 }
45 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
46 {
47         mbox->ops->fifo_write(mbox, msg);
48 }
49 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
50 {
51         return mbox->ops->fifo_empty(mbox);
52 }
53 static inline int mbox_fifo_full(struct omap_mbox *mbox)
54 {
55         return mbox->ops->fifo_full(mbox);
56 }
57
58 /* Mailbox IRQ handle functions */
59 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
60 {
61         if (mbox->ops->ack_irq)
62                 mbox->ops->ack_irq(mbox, irq);
63 }
64 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
65 {
66         return mbox->ops->is_irq(mbox, irq);
67 }
68
69 /*
70  * message sender
71  */
72 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
73 {
74         int ret = 0, i = 1000;
75
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);
82         }
83         mbox_fifo_write(mbox, msg);
84         return ret;
85 }
86
87
88 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
89 {
90
91         struct request *rq;
92         struct request_queue *q = mbox->txq->queue;
93
94         rq = blk_get_request(q, WRITE, GFP_ATOMIC);
95         if (unlikely(!rq))
96                 return -ENOMEM;
97
98         blk_insert_request(q, rq, 0, (void *) msg);
99         tasklet_schedule(&mbox->txq->tasklet);
100
101         return 0;
102 }
103 EXPORT_SYMBOL(omap_mbox_msg_send);
104
105 static void mbox_tx_tasklet(unsigned long tx_data)
106 {
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;
111
112         while (1) {
113
114                 rq = blk_fetch_request(q);
115
116                 if (!rq)
117                         break;
118
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;
124                 }
125                 blk_end_request_all(rq, 0);
126         }
127 }
128
129 /*
130  * Message receiver(workqueue)
131  */
132 static void mbox_rx_work(struct work_struct *work)
133 {
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;
141
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;
148                 }
149                 spin_unlock_irqrestore(q->queue_lock, flags);
150                 if (!rq)
151                         break;
152
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);
157         }
158 }
159
160 /*
161  * Mailbox interrupt handler
162  */
163 static void mbox_txq_fn(struct request_queue *q)
164 {
165 }
166
167 static void mbox_rxq_fn(struct request_queue *q)
168 {
169 }
170
171 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
172 {
173         omap_mbox_disable_irq(mbox, IRQ_TX);
174         ack_mbox_irq(mbox, IRQ_TX);
175         tasklet_schedule(&mbox->txq->tasklet);
176 }
177
178 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
179 {
180         struct request *rq;
181         mbox_msg_t msg;
182         struct request_queue *q = mbox->rxq->queue;
183
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;
190                 }
191
192                 msg = mbox_fifo_read(mbox);
193
194
195                 blk_insert_request(q, rq, 0, (void *)msg);
196                 if (mbox->ops->type == OMAP_MBOX_TYPE1)
197                         break;
198         }
199
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);
204 }
205
206 static irqreturn_t mbox_interrupt(int irq, void *p)
207 {
208         struct omap_mbox *mbox = p;
209
210         if (is_mbox_irq(mbox, IRQ_TX))
211                 __mbox_tx_interrupt(mbox);
212
213         if (is_mbox_irq(mbox, IRQ_RX))
214                 __mbox_rx_interrupt(mbox);
215
216         return IRQ_HANDLED;
217 }
218
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))
223 {
224         struct request_queue *q;
225         struct omap_mbox_queue *mq;
226
227         mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
228         if (!mq)
229                 return NULL;
230
231         spin_lock_init(&mq->lock);
232
233         q = blk_init_queue(proc, &mq->lock);
234         if (!q)
235                 goto error;
236         q->queuedata = mbox;
237         mq->queue = q;
238
239         if (work)
240                 INIT_WORK(&mq->work, work);
241
242         if (tasklet)
243                 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
244         return mq;
245 error:
246         kfree(mq);
247         return NULL;
248 }
249
250 static void mbox_queue_free(struct omap_mbox_queue *q)
251 {
252         blk_cleanup_queue(q->queue);
253         kfree(q);
254 }
255
256 static int omap_mbox_startup(struct omap_mbox *mbox)
257 {
258         int ret = 0;
259         struct omap_mbox_queue *mq;
260
261         if (mbox->ops->startup) {
262                 mutex_lock(&mbox_configured_lock);
263                 if (!mbox_configured)
264                         ret = mbox->ops->startup(mbox);
265
266                 if (ret) {
267                         mutex_unlock(&mbox_configured_lock);
268                         return ret;
269                 }
270                 mbox_configured++;
271                 mutex_unlock(&mbox_configured_lock);
272         }
273
274         ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
275                                 mbox->name, mbox);
276         if (ret) {
277                 printk(KERN_ERR
278                         "failed to register mailbox interrupt:%d\n", ret);
279                 goto fail_request_irq;
280         }
281
282         mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
283         if (!mq) {
284                 ret = -ENOMEM;
285                 goto fail_alloc_txq;
286         }
287         mbox->txq = mq;
288
289         mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
290         if (!mq) {
291                 ret = -ENOMEM;
292                 goto fail_alloc_rxq;
293         }
294         mbox->rxq = mq;
295
296         return 0;
297
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 (mbox->ops->shutdown)
304                 mbox->ops->shutdown(mbox);
305
306         return ret;
307 }
308
309 static void omap_mbox_fini(struct omap_mbox *mbox)
310 {
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);
316
317         if (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);
324         }
325 }
326
327 static struct omap_mbox **find_mboxes(const char *name)
328 {
329         struct omap_mbox **p;
330
331         for (p = &mboxes; *p; p = &(*p)->next) {
332                 if (strcmp((*p)->name, name) == 0)
333                         break;
334         }
335
336         return p;
337 }
338
339 struct omap_mbox *omap_mbox_get(const char *name)
340 {
341         struct omap_mbox *mbox;
342         int ret;
343
344         spin_lock(&mboxes_lock);
345         mbox = *(find_mboxes(name));
346         if (mbox == NULL) {
347                 spin_unlock(&mboxes_lock);
348                 return ERR_PTR(-ENOENT);
349         }
350
351         spin_unlock(&mboxes_lock);
352
353         ret = omap_mbox_startup(mbox);
354         if (ret)
355                 return ERR_PTR(-ENODEV);
356
357         return mbox;
358 }
359 EXPORT_SYMBOL(omap_mbox_get);
360
361 void omap_mbox_put(struct omap_mbox *mbox)
362 {
363         omap_mbox_fini(mbox);
364 }
365 EXPORT_SYMBOL(omap_mbox_put);
366
367 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
368 {
369         int ret = 0;
370         struct omap_mbox **tmp;
371
372         if (!mbox)
373                 return -EINVAL;
374         if (mbox->next)
375                 return -EBUSY;
376
377         spin_lock(&mboxes_lock);
378         tmp = find_mboxes(mbox->name);
379         if (*tmp) {
380                 ret = -EBUSY;
381                 spin_unlock(&mboxes_lock);
382                 goto err_find;
383         }
384         *tmp = mbox;
385         spin_unlock(&mboxes_lock);
386
387         return 0;
388
389 err_find:
390         return ret;
391 }
392 EXPORT_SYMBOL(omap_mbox_register);
393
394 int omap_mbox_unregister(struct omap_mbox *mbox)
395 {
396         struct omap_mbox **tmp;
397
398         spin_lock(&mboxes_lock);
399         tmp = &mboxes;
400         while (*tmp) {
401                 if (mbox == *tmp) {
402                         *tmp = mbox->next;
403                         mbox->next = NULL;
404                         spin_unlock(&mboxes_lock);
405                         return 0;
406                 }
407                 tmp = &(*tmp)->next;
408         }
409         spin_unlock(&mboxes_lock);
410
411         return -EINVAL;
412 }
413 EXPORT_SYMBOL(omap_mbox_unregister);
414
415 static int __init omap_mbox_init(void)
416 {
417         mboxd = create_workqueue("mboxd");
418         if (!mboxd)
419                 return -ENOMEM;
420
421         return 0;
422 }
423 module_init(omap_mbox_init);
424
425 static void __exit omap_mbox_exit(void)
426 {
427         destroy_workqueue(mboxd);
428 }
429 module_exit(omap_mbox_exit);
430
431 MODULE_LICENSE("GPL v2");
432 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
433 MODULE_AUTHOR("Toshihiro Kobayashi");
434 MODULE_AUTHOR("Hiroshi DOYU");