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