]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm/plat-omap/mailbox.c
Mailbox: new mutext lock for h/w mailbox configuration
[mirror_ubuntu-artful-kernel.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 36static int mbox_configured;
72b917ef 37static DEFINE_MUTEX(mbox_configured_lock);
5f00ec64 38
9ae0ee00
HD
39/* Mailbox FIFO handle functions */
40static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
41{
42 return mbox->ops->fifo_read(mbox);
43}
44static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
45{
46 mbox->ops->fifo_write(mbox, msg);
47}
48static inline int mbox_fifo_empty(struct omap_mbox *mbox)
49{
50 return mbox->ops->fifo_empty(mbox);
51}
52static inline int mbox_fifo_full(struct omap_mbox *mbox)
53{
54 return mbox->ops->fifo_full(mbox);
55}
56
57/* Mailbox IRQ handle functions */
9ae0ee00
HD
58static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
59{
60 if (mbox->ops->ack_irq)
61 mbox->ops->ack_irq(mbox, irq);
62}
63static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
64{
65 return mbox->ops->is_irq(mbox, irq);
66}
67
340a614a
HD
68/*
69 * message sender
70 */
c7c158e5 71static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a
HD
72{
73 int ret = 0, i = 1000;
74
75 while (mbox_fifo_full(mbox)) {
76 if (mbox->ops->type == OMAP_MBOX_TYPE2)
77 return -1;
78 if (--i == 0)
79 return -1;
80 udelay(1);
81 }
340a614a 82 mbox_fifo_write(mbox, msg);
340a614a
HD
83 return ret;
84}
85
ec24751a 86
b2b6362e 87int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a 88{
5ed8d32e 89
340a614a
HD
90 struct request *rq;
91 struct request_queue *q = mbox->txq->queue;
ec24751a 92
340a614a 93 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
5ed8d32e 94 if (unlikely(!rq))
ec24751a 95 return -ENOMEM;
340a614a 96
5ed8d32e
S
97 blk_insert_request(q, rq, 0, (void *) msg);
98 tasklet_schedule(&mbox->txq->tasklet);
340a614a 99
ec24751a 100 return 0;
340a614a
HD
101}
102EXPORT_SYMBOL(omap_mbox_msg_send);
103
5ed8d32e 104static void mbox_tx_tasklet(unsigned long tx_data)
340a614a
HD
105{
106 int ret;
107 struct request *rq;
5ed8d32e 108 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
340a614a
HD
109 struct request_queue *q = mbox->txq->queue;
110
111 while (1) {
ec24751a 112
9934c8c0 113 rq = blk_fetch_request(q);
340a614a
HD
114
115 if (!rq)
116 break;
117
5ed8d32e 118 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
340a614a 119 if (ret) {
eb18858e 120 omap_mbox_enable_irq(mbox, IRQ_TX);
296b2f6a 121 blk_requeue_request(q, rq);
340a614a
HD
122 return;
123 }
5ed8d32e 124 blk_end_request_all(rq, 0);
340a614a
HD
125 }
126}
127
128/*
129 * Message receiver(workqueue)
130 */
131static void mbox_rx_work(struct work_struct *work)
132{
133 struct omap_mbox_queue *mq =
134 container_of(work, struct omap_mbox_queue, work);
135 struct omap_mbox *mbox = mq->queue->queuedata;
136 struct request_queue *q = mbox->rxq->queue;
137 struct request *rq;
138 mbox_msg_t msg;
139 unsigned long flags;
140
340a614a
HD
141 while (1) {
142 spin_lock_irqsave(q->queue_lock, flags);
9934c8c0 143 rq = blk_fetch_request(q);
340a614a
HD
144 spin_unlock_irqrestore(q->queue_lock, flags);
145 if (!rq)
146 break;
147
ec24751a 148 msg = (mbox_msg_t)rq->special;
40cbbb78 149 blk_end_request_all(rq, 0);
9caae4d8
FGL
150 if (mbox->rxq->callback)
151 mbox->rxq->callback((void *)msg);
340a614a
HD
152 }
153}
154
155/*
156 * Mailbox interrupt handler
157 */
bfe1f6ac 158static void mbox_txq_fn(struct request_queue *q)
340a614a
HD
159{
160}
161
bfe1f6ac 162static void mbox_rxq_fn(struct request_queue *q)
340a614a
HD
163{
164}
165
166static void __mbox_tx_interrupt(struct omap_mbox *mbox)
167{
eb18858e 168 omap_mbox_disable_irq(mbox, IRQ_TX);
340a614a 169 ack_mbox_irq(mbox, IRQ_TX);
5ed8d32e 170 tasklet_schedule(&mbox->txq->tasklet);
340a614a
HD
171}
172
173static void __mbox_rx_interrupt(struct omap_mbox *mbox)
174{
175 struct request *rq;
176 mbox_msg_t msg;
165125e1 177 struct request_queue *q = mbox->rxq->queue;
340a614a 178
340a614a
HD
179 while (!mbox_fifo_empty(mbox)) {
180 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
181 if (unlikely(!rq))
182 goto nomem;
183
184 msg = mbox_fifo_read(mbox);
340a614a 185
340a614a 186
ec24751a 187 blk_insert_request(q, rq, 0, (void *)msg);
340a614a
HD
188 if (mbox->ops->type == OMAP_MBOX_TYPE1)
189 break;
190 }
191
192 /* no more messages in the fifo. clear IRQ source. */
193 ack_mbox_irq(mbox, IRQ_RX);
f48cca87 194nomem:
8250a5c3 195 queue_work(mboxd, &mbox->rxq->work);
340a614a
HD
196}
197
198static irqreturn_t mbox_interrupt(int irq, void *p)
199{
2a7057e3 200 struct omap_mbox *mbox = p;
340a614a
HD
201
202 if (is_mbox_irq(mbox, IRQ_TX))
203 __mbox_tx_interrupt(mbox);
204
205 if (is_mbox_irq(mbox, IRQ_RX))
206 __mbox_rx_interrupt(mbox);
207
208 return IRQ_HANDLED;
209}
210
340a614a 211static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
bfe1f6ac 212 request_fn_proc *proc,
5ed8d32e
S
213 void (*work) (struct work_struct *),
214 void (*tasklet)(unsigned long))
340a614a 215{
165125e1 216 struct request_queue *q;
340a614a
HD
217 struct omap_mbox_queue *mq;
218
219 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
220 if (!mq)
221 return NULL;
222
223 spin_lock_init(&mq->lock);
224
225 q = blk_init_queue(proc, &mq->lock);
226 if (!q)
227 goto error;
228 q->queuedata = mbox;
229 mq->queue = q;
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{
244 blk_cleanup_queue(q->queue);
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
253 if (likely(mbox->ops->startup)) {
72b917ef 254 mutex_lock(&mbox_configured_lock);
5f00ec64
S
255 if (!mbox_configured)
256 ret = mbox->ops->startup(mbox);
257
258 if (unlikely(ret)) {
72b917ef 259 mutex_unlock(&mbox_configured_lock);
340a614a 260 return ret;
5f00ec64
S
261 }
262 mbox_configured++;
72b917ef 263 mutex_unlock(&mbox_configured_lock);
340a614a
HD
264 }
265
5e683825 266 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
340a614a
HD
267 mbox->name, mbox);
268 if (unlikely(ret)) {
269 printk(KERN_ERR
270 "failed to register mailbox interrupt:%d\n", ret);
271 goto fail_request_irq;
272 }
340a614a 273
5ed8d32e 274 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
340a614a
HD
275 if (!mq) {
276 ret = -ENOMEM;
277 goto fail_alloc_txq;
278 }
279 mbox->txq = mq;
280
5ed8d32e 281 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
340a614a
HD
282 if (!mq) {
283 ret = -ENOMEM;
284 goto fail_alloc_rxq;
285 }
286 mbox->rxq = mq;
287
288 return 0;
289
290 fail_alloc_rxq:
291 mbox_queue_free(mbox->txq);
292 fail_alloc_txq:
293 free_irq(mbox->irq, mbox);
294 fail_request_irq:
340a614a
HD
295 if (unlikely(mbox->ops->shutdown))
296 mbox->ops->shutdown(mbox);
297
298 return ret;
299}
300
301static void omap_mbox_fini(struct omap_mbox *mbox)
302{
ad6d9627 303 free_irq(mbox->irq, mbox);
0e828e8c
FGL
304 tasklet_kill(&mbox->txq->tasklet);
305 flush_work(&mbox->rxq->work);
340a614a
HD
306 mbox_queue_free(mbox->txq);
307 mbox_queue_free(mbox->rxq);
308
5f00ec64 309 if (unlikely(mbox->ops->shutdown)) {
72b917ef 310 mutex_lock(&mbox_configured_lock);
5f00ec64
S
311 if (mbox_configured > 0)
312 mbox_configured--;
313 if (!mbox_configured)
314 mbox->ops->shutdown(mbox);
72b917ef 315 mutex_unlock(&mbox_configured_lock);
5f00ec64 316 }
340a614a
HD
317}
318
319static struct omap_mbox **find_mboxes(const char *name)
320{
321 struct omap_mbox **p;
322
323 for (p = &mboxes; *p; p = &(*p)->next) {
324 if (strcmp((*p)->name, name) == 0)
325 break;
326 }
327
328 return p;
329}
330
331struct omap_mbox *omap_mbox_get(const char *name)
332{
333 struct omap_mbox *mbox;
334 int ret;
335
336 read_lock(&mboxes_lock);
337 mbox = *(find_mboxes(name));
338 if (mbox == NULL) {
339 read_unlock(&mboxes_lock);
340 return ERR_PTR(-ENOENT);
341 }
342
343 read_unlock(&mboxes_lock);
344
c7c158e5 345 ret = omap_mbox_startup(mbox);
340a614a
HD
346 if (ret)
347 return ERR_PTR(-ENODEV);
348
349 return mbox;
350}
351EXPORT_SYMBOL(omap_mbox_get);
352
353void omap_mbox_put(struct omap_mbox *mbox)
354{
355 omap_mbox_fini(mbox);
356}
357EXPORT_SYMBOL(omap_mbox_put);
358
f48cca87 359int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
340a614a
HD
360{
361 int ret = 0;
362 struct omap_mbox **tmp;
363
364 if (!mbox)
365 return -EINVAL;
366 if (mbox->next)
367 return -EBUSY;
368
369 write_lock(&mboxes_lock);
370 tmp = find_mboxes(mbox->name);
f48cca87 371 if (*tmp) {
340a614a 372 ret = -EBUSY;
f48cca87
HD
373 write_unlock(&mboxes_lock);
374 goto err_find;
375 }
376 *tmp = mbox;
340a614a
HD
377 write_unlock(&mboxes_lock);
378
f48cca87
HD
379 return 0;
380
381err_find:
340a614a
HD
382 return ret;
383}
384EXPORT_SYMBOL(omap_mbox_register);
385
386int omap_mbox_unregister(struct omap_mbox *mbox)
387{
388 struct omap_mbox **tmp;
389
390 write_lock(&mboxes_lock);
391 tmp = &mboxes;
392 while (*tmp) {
393 if (mbox == *tmp) {
394 *tmp = mbox->next;
395 mbox->next = NULL;
396 write_unlock(&mboxes_lock);
397 return 0;
398 }
399 tmp = &(*tmp)->next;
400 }
401 write_unlock(&mboxes_lock);
402
403 return -EINVAL;
404}
405EXPORT_SYMBOL(omap_mbox_unregister);
406
c7c158e5 407static int __init omap_mbox_init(void)
340a614a 408{
8250a5c3
RC
409 mboxd = create_workqueue("mboxd");
410 if (!mboxd)
411 return -ENOMEM;
412
c7c158e5 413 return 0;
340a614a 414}
c7c158e5 415module_init(omap_mbox_init);
340a614a 416
c7c158e5 417static void __exit omap_mbox_exit(void)
340a614a 418{
8250a5c3 419 destroy_workqueue(mboxd);
340a614a 420}
c7c158e5 421module_exit(omap_mbox_exit);
340a614a 422
f48cca87
HD
423MODULE_LICENSE("GPL v2");
424MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
425MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");