OMAP: mailbox: send message in process context
[deliverable/linux.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/interrupt.h>
b3e69146
FC
25#include <linux/spinlock.h>
26#include <linux/mutex.h>
340a614a 27#include <linux/delay.h>
5a0e3ad6 28#include <linux/slab.h>
b5bebe41
OBC
29#include <linux/kfifo.h>
30#include <linux/err.h>
8dff0fa5 31
ce491cf8 32#include <plat/mailbox.h>
340a614a 33
8250a5c3 34static struct workqueue_struct *mboxd;
9c80c8cd 35static struct omap_mbox **mboxes;
340a614a 36
5f00ec64 37static int mbox_configured;
72b917ef 38static DEFINE_MUTEX(mbox_configured_lock);
5f00ec64 39
b5bebe41
OBC
40static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
41module_param(mbox_kfifo_size, uint, S_IRUGO);
42MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
43
9ae0ee00
HD
44/* Mailbox FIFO handle functions */
45static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
46{
47 return mbox->ops->fifo_read(mbox);
48}
49static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
50{
51 mbox->ops->fifo_write(mbox, msg);
52}
53static inline int mbox_fifo_empty(struct omap_mbox *mbox)
54{
55 return mbox->ops->fifo_empty(mbox);
56}
57static inline int mbox_fifo_full(struct omap_mbox *mbox)
58{
59 return mbox->ops->fifo_full(mbox);
60}
61
62/* Mailbox IRQ handle functions */
9ae0ee00
HD
63static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
64{
65 if (mbox->ops->ack_irq)
66 mbox->ops->ack_irq(mbox, irq);
67}
68static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
69{
70 return mbox->ops->is_irq(mbox, irq);
71}
72
340a614a
HD
73/*
74 * message sender
75 */
b5bebe41 76static int __mbox_poll_for_space(struct omap_mbox *mbox)
340a614a
HD
77{
78 int ret = 0, i = 1000;
79
80 while (mbox_fifo_full(mbox)) {
81 if (mbox->ops->type == OMAP_MBOX_TYPE2)
82 return -1;
83 if (--i == 0)
84 return -1;
85 udelay(1);
86 }
340a614a
HD
87 return ret;
88}
89
b2b6362e 90int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a 91{
b5bebe41
OBC
92 struct omap_mbox_queue *mq = mbox->txq;
93 int ret = 0, len;
5ed8d32e 94
a42743c2 95 spin_lock_bh(&mq->lock);
ec24751a 96
b5bebe41
OBC
97 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
98 ret = -ENOMEM;
99 goto out;
100 }
101
a42743c2
KH
102 if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
103 mbox_fifo_write(mbox, msg);
104 goto out;
105 }
106
b5bebe41
OBC
107 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
108 WARN_ON(len != sizeof(msg));
340a614a 109
5ed8d32e 110 tasklet_schedule(&mbox->txq->tasklet);
340a614a 111
b5bebe41 112out:
a42743c2 113 spin_unlock_bh(&mq->lock);
b5bebe41 114 return ret;
340a614a
HD
115}
116EXPORT_SYMBOL(omap_mbox_msg_send);
117
5ed8d32e 118static void mbox_tx_tasklet(unsigned long tx_data)
340a614a 119{
5ed8d32e 120 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
b5bebe41
OBC
121 struct omap_mbox_queue *mq = mbox->txq;
122 mbox_msg_t msg;
123 int ret;
340a614a 124
b5bebe41
OBC
125 while (kfifo_len(&mq->fifo)) {
126 if (__mbox_poll_for_space(mbox)) {
eb18858e 127 omap_mbox_enable_irq(mbox, IRQ_TX);
b5bebe41 128 break;
340a614a 129 }
b5bebe41
OBC
130
131 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
132 sizeof(msg));
133 WARN_ON(ret != sizeof(msg));
134
135 mbox_fifo_write(mbox, msg);
340a614a
HD
136 }
137}
138
139/*
140 * Message receiver(workqueue)
141 */
142static void mbox_rx_work(struct work_struct *work)
143{
144 struct omap_mbox_queue *mq =
145 container_of(work, struct omap_mbox_queue, work);
340a614a 146 mbox_msg_t msg;
b5bebe41
OBC
147 int len;
148
149 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
150 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
151 WARN_ON(len != sizeof(msg));
340a614a 152
b5bebe41
OBC
153 if (mq->callback)
154 mq->callback((void *)msg);
d2295042
FGL
155 spin_lock_irq(&mq->lock);
156 if (mq->full) {
157 mq->full = false;
158 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
159 }
160 spin_unlock_irq(&mq->lock);
340a614a
HD
161 }
162}
163
164/*
165 * Mailbox interrupt handler
166 */
340a614a
HD
167static void __mbox_tx_interrupt(struct omap_mbox *mbox)
168{
eb18858e 169 omap_mbox_disable_irq(mbox, IRQ_TX);
340a614a 170 ack_mbox_irq(mbox, IRQ_TX);
5ed8d32e 171 tasklet_schedule(&mbox->txq->tasklet);
340a614a
HD
172}
173
174static void __mbox_rx_interrupt(struct omap_mbox *mbox)
175{
b5bebe41 176 struct omap_mbox_queue *mq = mbox->rxq;
340a614a 177 mbox_msg_t msg;
b5bebe41 178 int len;
340a614a 179
340a614a 180 while (!mbox_fifo_empty(mbox)) {
b5bebe41 181 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
1ea5d6d1 182 omap_mbox_disable_irq(mbox, IRQ_RX);
d2295042 183 mq->full = true;
340a614a 184 goto nomem;
1ea5d6d1 185 }
340a614a
HD
186
187 msg = mbox_fifo_read(mbox);
340a614a 188
b5bebe41
OBC
189 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
190 WARN_ON(len != sizeof(msg));
340a614a 191
340a614a
HD
192 if (mbox->ops->type == OMAP_MBOX_TYPE1)
193 break;
194 }
195
196 /* no more messages in the fifo. clear IRQ source. */
197 ack_mbox_irq(mbox, IRQ_RX);
f48cca87 198nomem:
8250a5c3 199 queue_work(mboxd, &mbox->rxq->work);
340a614a
HD
200}
201
202static irqreturn_t mbox_interrupt(int irq, void *p)
203{
2a7057e3 204 struct omap_mbox *mbox = p;
340a614a
HD
205
206 if (is_mbox_irq(mbox, IRQ_TX))
207 __mbox_tx_interrupt(mbox);
208
209 if (is_mbox_irq(mbox, IRQ_RX))
210 __mbox_rx_interrupt(mbox);
211
212 return IRQ_HANDLED;
213}
214
340a614a 215static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
5ed8d32e
S
216 void (*work) (struct work_struct *),
217 void (*tasklet)(unsigned long))
340a614a 218{
340a614a
HD
219 struct omap_mbox_queue *mq;
220
221 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
222 if (!mq)
223 return NULL;
224
225 spin_lock_init(&mq->lock);
226
b5bebe41 227 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
340a614a 228 goto error;
340a614a 229
5ed8d32e
S
230 if (work)
231 INIT_WORK(&mq->work, work);
340a614a 232
5ed8d32e
S
233 if (tasklet)
234 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
340a614a
HD
235 return mq;
236error:
237 kfree(mq);
238 return NULL;
239}
240
241static void mbox_queue_free(struct omap_mbox_queue *q)
242{
b5bebe41 243 kfifo_free(&q->fifo);
340a614a
HD
244 kfree(q);
245}
246
c7c158e5 247static int omap_mbox_startup(struct omap_mbox *mbox)
340a614a 248{
5f00ec64 249 int ret = 0;
340a614a
HD
250 struct omap_mbox_queue *mq;
251
01072d8f 252 if (mbox->ops->startup) {
72b917ef 253 mutex_lock(&mbox_configured_lock);
5f00ec64
S
254 if (!mbox_configured)
255 ret = mbox->ops->startup(mbox);
256
01072d8f 257 if (ret) {
72b917ef 258 mutex_unlock(&mbox_configured_lock);
340a614a 259 return ret;
5f00ec64
S
260 }
261 mbox_configured++;
72b917ef 262 mutex_unlock(&mbox_configured_lock);
340a614a
HD
263 }
264
5e683825 265 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
340a614a 266 mbox->name, mbox);
01072d8f 267 if (ret) {
340a614a
HD
268 printk(KERN_ERR
269 "failed to register mailbox interrupt:%d\n", ret);
270 goto fail_request_irq;
271 }
340a614a 272
b5bebe41 273 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
340a614a
HD
274 if (!mq) {
275 ret = -ENOMEM;
276 goto fail_alloc_txq;
277 }
278 mbox->txq = mq;
279
b5bebe41 280 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
340a614a
HD
281 if (!mq) {
282 ret = -ENOMEM;
283 goto fail_alloc_rxq;
284 }
285 mbox->rxq = mq;
286
287 return 0;
288
ab66ac30 289fail_alloc_rxq:
340a614a 290 mbox_queue_free(mbox->txq);
ab66ac30 291fail_alloc_txq:
340a614a 292 free_irq(mbox->irq, mbox);
ab66ac30 293fail_request_irq:
01072d8f 294 if (mbox->ops->shutdown)
340a614a
HD
295 mbox->ops->shutdown(mbox);
296
297 return ret;
298}
299
300static void omap_mbox_fini(struct omap_mbox *mbox)
301{
ad6d9627 302 free_irq(mbox->irq, mbox);
0e828e8c
FGL
303 tasklet_kill(&mbox->txq->tasklet);
304 flush_work(&mbox->rxq->work);
340a614a
HD
305 mbox_queue_free(mbox->txq);
306 mbox_queue_free(mbox->rxq);
307
01072d8f 308 if (mbox->ops->shutdown) {
72b917ef 309 mutex_lock(&mbox_configured_lock);
5f00ec64
S
310 if (mbox_configured > 0)
311 mbox_configured--;
312 if (!mbox_configured)
313 mbox->ops->shutdown(mbox);
72b917ef 314 mutex_unlock(&mbox_configured_lock);
5f00ec64 315 }
340a614a
HD
316}
317
340a614a
HD
318struct omap_mbox *omap_mbox_get(const char *name)
319{
320 struct omap_mbox *mbox;
321 int ret;
322
9c80c8cd
FC
323 if (!mboxes)
324 return ERR_PTR(-EINVAL);
340a614a 325
9c80c8cd
FC
326 for (mbox = *mboxes; mbox; mbox++)
327 if (!strcmp(mbox->name, name))
328 break;
329
330 if (!mbox)
331 return ERR_PTR(-ENOENT);
340a614a 332
c7c158e5 333 ret = omap_mbox_startup(mbox);
340a614a
HD
334 if (ret)
335 return ERR_PTR(-ENODEV);
336
337 return mbox;
338}
339EXPORT_SYMBOL(omap_mbox_get);
340
341void omap_mbox_put(struct omap_mbox *mbox)
342{
343 omap_mbox_fini(mbox);
344}
345EXPORT_SYMBOL(omap_mbox_put);
346
6b233985
HD
347static struct class omap_mbox_class = { .name = "mbox", };
348
9c80c8cd 349int omap_mbox_register(struct device *parent, struct omap_mbox **list)
340a614a 350{
9c80c8cd
FC
351 int ret;
352 int i;
340a614a 353
9c80c8cd
FC
354 mboxes = list;
355 if (!mboxes)
340a614a 356 return -EINVAL;
340a614a 357
9c80c8cd
FC
358 for (i = 0; mboxes[i]; i++) {
359 struct omap_mbox *mbox = mboxes[i];
360 mbox->dev = device_create(&omap_mbox_class,
361 parent, 0, mbox, "%s", mbox->name);
362 if (IS_ERR(mbox->dev)) {
363 ret = PTR_ERR(mbox->dev);
364 goto err_out;
365 }
366 }
f48cca87
HD
367 return 0;
368
9c80c8cd
FC
369err_out:
370 while (i--)
371 device_unregister(mboxes[i]->dev);
340a614a
HD
372 return ret;
373}
374EXPORT_SYMBOL(omap_mbox_register);
375
9c80c8cd 376int omap_mbox_unregister(void)
340a614a 377{
9c80c8cd 378 int i;
340a614a 379
9c80c8cd
FC
380 if (!mboxes)
381 return -EINVAL;
382
383 for (i = 0; mboxes[i]; i++)
384 device_unregister(mboxes[i]->dev);
385 mboxes = NULL;
386 return 0;
340a614a
HD
387}
388EXPORT_SYMBOL(omap_mbox_unregister);
389
c7c158e5 390static int __init omap_mbox_init(void)
340a614a 391{
6b233985
HD
392 int err;
393
394 err = class_register(&omap_mbox_class);
395 if (err)
396 return err;
397
8250a5c3
RC
398 mboxd = create_workqueue("mboxd");
399 if (!mboxd)
400 return -ENOMEM;
401
b5bebe41
OBC
402 /* kfifo size sanity check: alignment and minimal size */
403 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
ab66ac30
KH
404 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
405 sizeof(mbox_msg_t));
b5bebe41 406
c7c158e5 407 return 0;
340a614a 408}
6b233985 409subsys_initcall(omap_mbox_init);
340a614a 410
c7c158e5 411static void __exit omap_mbox_exit(void)
340a614a 412{
8250a5c3 413 destroy_workqueue(mboxd);
6b233985 414 class_unregister(&omap_mbox_class);
340a614a 415}
c7c158e5 416module_exit(omap_mbox_exit);
340a614a 417
f48cca87
HD
418MODULE_LICENSE("GPL v2");
419MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
f375325a
OBC
420MODULE_AUTHOR("Toshihiro Kobayashi");
421MODULE_AUTHOR("Hiroshi DOYU");
This page took 0.310698 seconds and 5 git commands to generate.