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