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 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 24 | #include <linux/kernel.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/device.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 28 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 30 | #include <linux/kfifo.h> |
| 31 | #include <linux/err.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 32 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 33 | #include <plat/mailbox.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 34 | |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 35 | static struct workqueue_struct *mboxd; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 36 | static struct omap_mbox *mboxes; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(mboxes_lock); |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 38 | static bool rq_full; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 39 | |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 40 | static int mbox_configured; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 41 | static DEFINE_MUTEX(mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 42 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 43 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; |
| 44 | module_param(mbox_kfifo_size, uint, S_IRUGO); |
| 45 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); |
| 46 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 47 | /* Mailbox FIFO handle functions */ |
| 48 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
| 49 | { |
| 50 | return mbox->ops->fifo_read(mbox); |
| 51 | } |
| 52 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) |
| 53 | { |
| 54 | mbox->ops->fifo_write(mbox, msg); |
| 55 | } |
| 56 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) |
| 57 | { |
| 58 | return mbox->ops->fifo_empty(mbox); |
| 59 | } |
| 60 | static inline int mbox_fifo_full(struct omap_mbox *mbox) |
| 61 | { |
| 62 | return mbox->ops->fifo_full(mbox); |
| 63 | } |
| 64 | |
| 65 | /* Mailbox IRQ handle functions */ |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 66 | static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 67 | { |
| 68 | if (mbox->ops->ack_irq) |
| 69 | mbox->ops->ack_irq(mbox, irq); |
| 70 | } |
| 71 | static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 72 | { |
| 73 | return mbox->ops->is_irq(mbox, irq); |
| 74 | } |
| 75 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 76 | /* |
| 77 | * message sender |
| 78 | */ |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 79 | static int __mbox_poll_for_space(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 80 | { |
| 81 | int ret = 0, i = 1000; |
| 82 | |
| 83 | while (mbox_fifo_full(mbox)) { |
| 84 | if (mbox->ops->type == OMAP_MBOX_TYPE2) |
| 85 | return -1; |
| 86 | if (--i == 0) |
| 87 | return -1; |
| 88 | udelay(1); |
| 89 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 90 | return ret; |
| 91 | } |
| 92 | |
C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 93 | 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] | 94 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 95 | struct omap_mbox_queue *mq = mbox->txq; |
| 96 | int ret = 0, len; |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 97 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 98 | spin_lock(&mq->lock); |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 99 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 100 | if (kfifo_avail(&mq->fifo) < sizeof(msg)) { |
| 101 | ret = -ENOMEM; |
| 102 | goto out; |
| 103 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 104 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 105 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 106 | WARN_ON(len != sizeof(msg)); |
| 107 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 108 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 109 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 110 | out: |
| 111 | spin_unlock(&mq->lock); |
| 112 | return ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 115 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 116 | static void mbox_tx_tasklet(unsigned long tx_data) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 117 | { |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 118 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 119 | struct omap_mbox_queue *mq = mbox->txq; |
| 120 | mbox_msg_t msg; |
| 121 | int ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 122 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 123 | while (kfifo_len(&mq->fifo)) { |
| 124 | if (__mbox_poll_for_space(mbox)) { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 125 | omap_mbox_enable_irq(mbox, IRQ_TX); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 126 | break; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 127 | } |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 128 | |
| 129 | ret = kfifo_out(&mq->fifo, (unsigned char *)&msg, |
| 130 | sizeof(msg)); |
| 131 | WARN_ON(ret != sizeof(msg)); |
| 132 | |
| 133 | mbox_fifo_write(mbox, msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Message receiver(workqueue) |
| 139 | */ |
| 140 | static void mbox_rx_work(struct work_struct *work) |
| 141 | { |
| 142 | struct omap_mbox_queue *mq = |
| 143 | container_of(work, struct omap_mbox_queue, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 144 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 145 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 146 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 147 | while (kfifo_len(&mq->fifo) >= sizeof(msg)) { |
| 148 | len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 149 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 150 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 151 | if (mq->callback) |
| 152 | mq->callback((void *)msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Mailbox interrupt handler |
| 158 | */ |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 159 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 160 | { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 161 | omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 162 | ack_mbox_irq(mbox, IRQ_TX); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 163 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 167 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 168 | struct omap_mbox_queue *mq = mbox->rxq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 169 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 170 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 171 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 172 | while (!mbox_fifo_empty(mbox)) { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 173 | if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 174 | omap_mbox_disable_irq(mbox, IRQ_RX); |
| 175 | rq_full = true; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 176 | goto nomem; |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 177 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 178 | |
| 179 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 180 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 181 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 182 | WARN_ON(len != sizeof(msg)); |
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 | 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); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 190 | nomem: |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 191 | queue_work(mboxd, &mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 195 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 196 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 207 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 208 | void (*work) (struct work_struct *), |
| 209 | void (*tasklet)(unsigned long)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 210 | { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 211 | struct omap_mbox_queue *mq; |
| 212 | |
| 213 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); |
| 214 | if (!mq) |
| 215 | return NULL; |
| 216 | |
| 217 | spin_lock_init(&mq->lock); |
| 218 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 219 | if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 220 | goto error; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 221 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 222 | if (work) |
| 223 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 224 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 225 | if (tasklet) |
| 226 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 227 | return mq; |
| 228 | error: |
| 229 | kfree(mq); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 234 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 235 | kfifo_free(&q->fifo); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 236 | kfree(q); |
| 237 | } |
| 238 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 239 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 240 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 241 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 242 | struct omap_mbox_queue *mq; |
| 243 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 244 | if (mbox->ops->startup) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 245 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 246 | if (!mbox_configured) |
| 247 | ret = mbox->ops->startup(mbox); |
| 248 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 249 | if (ret) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 250 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 251 | return ret; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 252 | } |
| 253 | mbox_configured++; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 254 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 255 | } |
| 256 | |
C A Subramaniam | 5e68382 | 2009-11-22 10:11:23 -0800 | [diff] [blame] | 257 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 258 | mbox->name, mbox); |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 259 | if (ret) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 260 | printk(KERN_ERR |
| 261 | "failed to register mailbox interrupt:%d\n", ret); |
| 262 | goto fail_request_irq; |
| 263 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 264 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 265 | mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 266 | if (!mq) { |
| 267 | ret = -ENOMEM; |
| 268 | goto fail_alloc_txq; |
| 269 | } |
| 270 | mbox->txq = mq; |
| 271 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 272 | mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 273 | if (!mq) { |
| 274 | ret = -ENOMEM; |
| 275 | goto fail_alloc_rxq; |
| 276 | } |
| 277 | mbox->rxq = mq; |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | fail_alloc_rxq: |
| 282 | mbox_queue_free(mbox->txq); |
| 283 | fail_alloc_txq: |
| 284 | free_irq(mbox->irq, mbox); |
| 285 | fail_request_irq: |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 286 | if (mbox->ops->shutdown) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 287 | mbox->ops->shutdown(mbox); |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 293 | { |
Fernando Guzman Lugo | ad6d962 | 2010-02-12 19:02:32 -0600 | [diff] [blame] | 294 | free_irq(mbox->irq, mbox); |
Fernando Guzman Lugo | 0e828e8 | 2010-02-12 19:07:14 -0600 | [diff] [blame] | 295 | tasklet_kill(&mbox->txq->tasklet); |
| 296 | flush_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 297 | mbox_queue_free(mbox->txq); |
| 298 | mbox_queue_free(mbox->rxq); |
| 299 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 300 | if (mbox->ops->shutdown) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 301 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 302 | if (mbox_configured > 0) |
| 303 | mbox_configured--; |
| 304 | if (!mbox_configured) |
| 305 | mbox->ops->shutdown(mbox); |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 306 | mutex_unlock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 307 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static struct omap_mbox **find_mboxes(const char *name) |
| 311 | { |
| 312 | struct omap_mbox **p; |
| 313 | |
| 314 | for (p = &mboxes; *p; p = &(*p)->next) { |
| 315 | if (strcmp((*p)->name, name) == 0) |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | return p; |
| 320 | } |
| 321 | |
| 322 | struct omap_mbox *omap_mbox_get(const char *name) |
| 323 | { |
| 324 | struct omap_mbox *mbox; |
| 325 | int ret; |
| 326 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 327 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 328 | mbox = *(find_mboxes(name)); |
| 329 | if (mbox == NULL) { |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 330 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 331 | return ERR_PTR(-ENOENT); |
| 332 | } |
| 333 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 334 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 335 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 336 | ret = omap_mbox_startup(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 337 | if (ret) |
| 338 | return ERR_PTR(-ENODEV); |
| 339 | |
| 340 | return mbox; |
| 341 | } |
| 342 | EXPORT_SYMBOL(omap_mbox_get); |
| 343 | |
| 344 | void omap_mbox_put(struct omap_mbox *mbox) |
| 345 | { |
| 346 | omap_mbox_fini(mbox); |
| 347 | } |
| 348 | EXPORT_SYMBOL(omap_mbox_put); |
| 349 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 350 | int omap_mbox_register(struct device *parent, struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 351 | { |
| 352 | int ret = 0; |
| 353 | struct omap_mbox **tmp; |
| 354 | |
| 355 | if (!mbox) |
| 356 | return -EINVAL; |
| 357 | if (mbox->next) |
| 358 | return -EBUSY; |
| 359 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 360 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 361 | tmp = find_mboxes(mbox->name); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 362 | if (*tmp) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 363 | ret = -EBUSY; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 364 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 365 | goto err_find; |
| 366 | } |
| 367 | *tmp = mbox; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 368 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 369 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | |
| 372 | err_find: |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 373 | return ret; |
| 374 | } |
| 375 | EXPORT_SYMBOL(omap_mbox_register); |
| 376 | |
| 377 | int omap_mbox_unregister(struct omap_mbox *mbox) |
| 378 | { |
| 379 | struct omap_mbox **tmp; |
| 380 | |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 381 | spin_lock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 382 | tmp = &mboxes; |
| 383 | while (*tmp) { |
| 384 | if (mbox == *tmp) { |
| 385 | *tmp = mbox->next; |
| 386 | mbox->next = NULL; |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 387 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | tmp = &(*tmp)->next; |
| 391 | } |
Ohad Ben-Cohen | 10d1a00 | 2010-05-05 15:33:06 +0000 | [diff] [blame] | 392 | spin_unlock(&mboxes_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 393 | |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | EXPORT_SYMBOL(omap_mbox_unregister); |
| 397 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 398 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 399 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 400 | mboxd = create_workqueue("mboxd"); |
| 401 | if (!mboxd) |
| 402 | return -ENOMEM; |
| 403 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame^] | 404 | /* kfifo size sanity check: alignment and minimal size */ |
| 405 | mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); |
| 406 | mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, sizeof(mbox_msg_t)); |
| 407 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 408 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 409 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 410 | module_init(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 411 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 412 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 413 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 414 | destroy_workqueue(mboxd); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 415 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 416 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 417 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 418 | MODULE_LICENSE("GPL v2"); |
| 419 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 420 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
| 421 | MODULE_AUTHOR("Hiroshi DOYU"); |