Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * OMAP mailbox driver |
| 3 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved. |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 5 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 6 | * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/device.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 29 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 30 | #include <plat/mailbox.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 31 | |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 32 | static struct workqueue_struct *mboxd; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 33 | static struct omap_mbox *mboxes; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 34 | static DEFINE_SPINLOCK(mboxes_lock); |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 35 | static bool rq_full; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 36 | |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 37 | static int mbox_configured; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 38 | static DEFINE_MUTEX(mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 39 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 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 */ |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 69 | /* |
| 70 | * message sender |
| 71 | */ |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 72 | static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 83 | mbox_fifo_write(mbox, msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 87 | |
C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 88 | int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 89 | { |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 90 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 91 | struct request *rq; |
| 92 | struct request_queue *q = mbox->txq->queue; |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 93 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 94 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 95 | if (unlikely(!rq)) |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 96 | return -ENOMEM; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 97 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 98 | blk_insert_request(q, rq, 0, (void *) msg); |
| 99 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 100 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 101 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 102 | } |
| 103 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 104 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 105 | static void mbox_tx_tasklet(unsigned long tx_data) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 106 | { |
| 107 | int ret; |
| 108 | struct request *rq; |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 109 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 110 | struct request_queue *q = mbox->txq->queue; |
| 111 | |
| 112 | while (1) { |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 113 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 114 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 115 | |
| 116 | if (!rq) |
| 117 | break; |
| 118 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 119 | ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 120 | if (ret) { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 121 | omap_mbox_enable_irq(mbox, IRQ_TX); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 122 | blk_requeue_request(q, rq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 123 | return; |
| 124 | } |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 125 | blk_end_request_all(rq, 0); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 142 | while (1) { |
| 143 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 144 | rq = blk_fetch_request(q); |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 145 | if (rq_full) { |
| 146 | omap_mbox_enable_irq(mbox, IRQ_RX); |
| 147 | rq_full = false; |
| 148 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 149 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 150 | if (!rq) |
| 151 | break; |
| 152 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 153 | msg = (mbox_msg_t)rq->special; |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 154 | blk_end_request_all(rq, 0); |
Fernando Guzman Lugo | 9caae4d | 2010-01-27 20:04:02 -0600 | [diff] [blame] | 155 | if (mbox->rxq->callback) |
| 156 | mbox->rxq->callback((void *)msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Mailbox interrupt handler |
| 162 | */ |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 163 | static void mbox_txq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 164 | { |
| 165 | } |
| 166 | |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 167 | static void mbox_rxq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 168 | { |
| 169 | } |
| 170 | |
| 171 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 172 | { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 173 | omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 174 | ack_mbox_irq(mbox, IRQ_TX); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 175 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 179 | { |
| 180 | struct request *rq; |
| 181 | mbox_msg_t msg; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 182 | struct request_queue *q = mbox->rxq->queue; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 183 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 184 | while (!mbox_fifo_empty(mbox)) { |
| 185 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 186 | if (unlikely(!rq)) { |
| 187 | omap_mbox_disable_irq(mbox, IRQ_RX); |
| 188 | rq_full = true; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 189 | goto nomem; |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 190 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 191 | |
| 192 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 193 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 194 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 195 | blk_insert_request(q, rq, 0, (void *)msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 202 | nomem: |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 203 | queue_work(mboxd, &mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 207 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 208 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 219 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 220 | request_fn_proc *proc, |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 221 | void (*work) (struct work_struct *), |
| 222 | void (*tasklet)(unsigned long)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 223 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 224 | struct request_queue *q; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 239 | if (work) |
| 240 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 241 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 242 | if (tasklet) |
| 243 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 256 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 257 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 258 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 259 | struct omap_mbox_queue *mq; |
| 260 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame^] | 261 | if (mbox->ops->startup) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 262 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 263 | if (!mbox_configured) |
| 264 | ret = mbox->ops->startup(mbox); |
| 265 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame^] | 266 | if (ret) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 267 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 268 | return ret; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 269 | } |
| 270 | mbox_configured++; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 271 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 272 | } |
| 273 | |
C A Subramaniam | 5e68382 | 2009-11-22 10:11:23 -0800 | [diff] [blame] | 274 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 275 | mbox->name, mbox); |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame^] | 276 | if (ret) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 277 | printk(KERN_ERR |
| 278 | "failed to register mailbox interrupt:%d\n", ret); |
| 279 | goto fail_request_irq; |
| 280 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 281 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 282 | mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 283 | if (!mq) { |
| 284 | ret = -ENOMEM; |
| 285 | goto fail_alloc_txq; |
| 286 | } |
| 287 | mbox->txq = mq; |
| 288 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 289 | mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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: |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame^] | 303 | if (mbox->ops->shutdown) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 304 | mbox->ops->shutdown(mbox); |
| 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 310 | { |
Fernando Guzman Lugo | ad6d962 | 2010-02-12 19:02:32 -0600 | [diff] [blame] | 311 | free_irq(mbox->irq, mbox); |
Fernando Guzman Lugo | 0e828e8 | 2010-02-12 19:07:14 -0600 | [diff] [blame] | 312 | tasklet_kill(&mbox->txq->tasklet); |
| 313 | flush_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 314 | mbox_queue_free(mbox->txq); |
| 315 | mbox_queue_free(mbox->rxq); |
| 316 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame^] | 317 | if (mbox->ops->shutdown) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 318 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 319 | if (mbox_configured > 0) |
| 320 | mbox_configured--; |
| 321 | if (!mbox_configured) |
| 322 | mbox->ops->shutdown(mbox); |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 323 | mutex_unlock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 324 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 344 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 345 | mbox = *(find_mboxes(name)); |
| 346 | if (mbox == NULL) { |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 347 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 348 | return ERR_PTR(-ENOENT); |
| 349 | } |
| 350 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 351 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 352 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 353 | ret = omap_mbox_startup(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 367 | int omap_mbox_register(struct device *parent, struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 377 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 378 | tmp = find_mboxes(mbox->name); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 379 | if (*tmp) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 380 | ret = -EBUSY; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 381 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 382 | goto err_find; |
| 383 | } |
| 384 | *tmp = mbox; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 385 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 386 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | |
| 389 | err_find: |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 398 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 399 | tmp = &mboxes; |
| 400 | while (*tmp) { |
| 401 | if (mbox == *tmp) { |
| 402 | *tmp = mbox->next; |
| 403 | mbox->next = NULL; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 404 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | tmp = &(*tmp)->next; |
| 408 | } |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 409 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 410 | |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | EXPORT_SYMBOL(omap_mbox_unregister); |
| 414 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 415 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 416 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 417 | mboxd = create_workqueue("mboxd"); |
| 418 | if (!mboxd) |
| 419 | return -ENOMEM; |
| 420 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 421 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 422 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 423 | module_init(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 424 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 425 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 426 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 427 | destroy_workqueue(mboxd); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 428 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 429 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 430 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 431 | MODULE_LICENSE("GPL v2"); |
| 432 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 433 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
| 434 | MODULE_AUTHOR("Hiroshi DOYU"); |