| 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/interrupt.h> | 
| Felipe Contreras | b3e6914 | 2010-06-11 15:51:49 +0000 | [diff] [blame] | 25 | #include <linux/spinlock.h> | 
 | 26 | #include <linux/mutex.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> | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 29 | #include <linux/kfifo.h> | 
 | 30 | #include <linux/err.h> | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 31 | #include <linux/notifier.h> | 
| Paul Gortmaker | 73017a5 | 2011-07-31 16:14:14 -0400 | [diff] [blame] | 32 | #include <linux/module.h> | 
| Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 33 |  | 
| Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 34 | #include <plat/mailbox.h> | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 35 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 36 | static struct omap_mbox **mboxes; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 37 |  | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 38 | static int mbox_configured; | 
| Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 39 | static DEFINE_MUTEX(mbox_configured_lock); | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 40 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 41 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; | 
 | 42 | module_param(mbox_kfifo_size, uint, S_IRUGO); | 
 | 43 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); | 
 | 44 |  | 
| Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 45 | /* Mailbox FIFO handle functions */ | 
 | 46 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) | 
 | 47 | { | 
 | 48 | 	return mbox->ops->fifo_read(mbox); | 
 | 49 | } | 
 | 50 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) | 
 | 51 | { | 
 | 52 | 	mbox->ops->fifo_write(mbox, msg); | 
 | 53 | } | 
 | 54 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) | 
 | 55 | { | 
 | 56 | 	return mbox->ops->fifo_empty(mbox); | 
 | 57 | } | 
 | 58 | static inline int mbox_fifo_full(struct omap_mbox *mbox) | 
 | 59 | { | 
 | 60 | 	return mbox->ops->fifo_full(mbox); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | /* Mailbox IRQ handle functions */ | 
| Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 64 | static 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 | } | 
 | 69 | static 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 |  | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 74 | /* | 
 | 75 |  * message sender | 
 | 76 |  */ | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 77 | static int __mbox_poll_for_space(struct omap_mbox *mbox) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | 	} | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 88 | 	return ret; | 
 | 89 | } | 
 | 90 |  | 
| C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 91 | 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] | 92 | { | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 93 | 	struct omap_mbox_queue *mq = mbox->txq; | 
 | 94 | 	int ret = 0, len; | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 95 |  | 
| Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 96 | 	spin_lock_bh(&mq->lock); | 
| Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 97 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 98 | 	if (kfifo_avail(&mq->fifo) < sizeof(msg)) { | 
 | 99 | 		ret = -ENOMEM; | 
 | 100 | 		goto out; | 
 | 101 | 	} | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 102 |  | 
| Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 103 | 	if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) { | 
 | 104 | 		mbox_fifo_write(mbox, msg); | 
 | 105 | 		goto out; | 
 | 106 | 	} | 
 | 107 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 108 | 	len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); | 
 | 109 | 	WARN_ON(len != sizeof(msg)); | 
 | 110 |  | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 111 | 	tasklet_schedule(&mbox->txq->tasklet); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 112 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 113 | out: | 
| Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 114 | 	spin_unlock_bh(&mq->lock); | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 115 | 	return ret; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 116 | } | 
 | 117 | EXPORT_SYMBOL(omap_mbox_msg_send); | 
 | 118 |  | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 119 | static void mbox_tx_tasklet(unsigned long tx_data) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 120 | { | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 121 | 	struct omap_mbox *mbox = (struct omap_mbox *)tx_data; | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 122 | 	struct omap_mbox_queue *mq = mbox->txq; | 
 | 123 | 	mbox_msg_t msg; | 
 | 124 | 	int ret; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 125 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 126 | 	while (kfifo_len(&mq->fifo)) { | 
 | 127 | 		if (__mbox_poll_for_space(mbox)) { | 
| Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 128 | 			omap_mbox_enable_irq(mbox, IRQ_TX); | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 129 | 			break; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 130 | 		} | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 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); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 137 | 	} | 
 | 138 | } | 
 | 139 |  | 
 | 140 | /* | 
 | 141 |  * Message receiver(workqueue) | 
 | 142 |  */ | 
 | 143 | static void mbox_rx_work(struct work_struct *work) | 
 | 144 | { | 
 | 145 | 	struct omap_mbox_queue *mq = | 
 | 146 | 			container_of(work, struct omap_mbox_queue, work); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 147 | 	mbox_msg_t msg; | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 148 | 	int len; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 149 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 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)); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 153 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 154 | 		blocking_notifier_call_chain(&mq->mbox->notifier, len, | 
 | 155 | 								(void *)msg); | 
| Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 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); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 162 | 	} | 
 | 163 | } | 
 | 164 |  | 
 | 165 | /* | 
 | 166 |  * Mailbox interrupt handler | 
 | 167 |  */ | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 168 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) | 
 | 169 | { | 
| Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 170 | 	omap_mbox_disable_irq(mbox, IRQ_TX); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 171 | 	ack_mbox_irq(mbox, IRQ_TX); | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 172 | 	tasklet_schedule(&mbox->txq->tasklet); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 173 | } | 
 | 174 |  | 
 | 175 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) | 
 | 176 | { | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 177 | 	struct omap_mbox_queue *mq = mbox->rxq; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 178 | 	mbox_msg_t msg; | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 179 | 	int len; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 180 |  | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 181 | 	while (!mbox_fifo_empty(mbox)) { | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 182 | 		if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { | 
| Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 183 | 			omap_mbox_disable_irq(mbox, IRQ_RX); | 
| Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 184 | 			mq->full = true; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 185 | 			goto nomem; | 
| Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 186 | 		} | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 187 |  | 
 | 188 | 		msg = mbox_fifo_read(mbox); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 189 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 190 | 		len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); | 
 | 191 | 		WARN_ON(len != sizeof(msg)); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 192 |  | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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); | 
| Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 199 | nomem: | 
| Tejun Heo | c487300 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 200 | 	schedule_work(&mbox->rxq->work); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 201 | } | 
 | 202 |  | 
 | 203 | static irqreturn_t mbox_interrupt(int irq, void *p) | 
 | 204 | { | 
| Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 205 | 	struct omap_mbox *mbox = p; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 |  | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 216 | 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] | 217 | 					void (*work) (struct work_struct *), | 
 | 218 | 					void (*tasklet)(unsigned long)) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 219 | { | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 228 | 	if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 229 | 		goto error; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 230 |  | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 231 | 	if (work) | 
 | 232 | 		INIT_WORK(&mq->work, work); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 233 |  | 
| C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 234 | 	if (tasklet) | 
 | 235 | 		tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 236 | 	return mq; | 
 | 237 | error: | 
 | 238 | 	kfree(mq); | 
 | 239 | 	return NULL; | 
 | 240 | } | 
 | 241 |  | 
 | 242 | static void mbox_queue_free(struct omap_mbox_queue *q) | 
 | 243 | { | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 244 | 	kfifo_free(&q->fifo); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 245 | 	kfree(q); | 
 | 246 | } | 
 | 247 |  | 
| Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 248 | static int omap_mbox_startup(struct omap_mbox *mbox) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 249 | { | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 250 | 	int ret = 0; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 251 | 	struct omap_mbox_queue *mq; | 
 | 252 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 253 | 	mutex_lock(&mbox_configured_lock); | 
 | 254 | 	if (!mbox_configured++) { | 
 | 255 | 		if (likely(mbox->ops->startup)) { | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 256 | 			ret = mbox->ops->startup(mbox); | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 257 | 			if (unlikely(ret)) | 
 | 258 | 				goto fail_startup; | 
 | 259 | 		} else | 
 | 260 | 			goto fail_startup; | 
 | 261 | 	} | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 262 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 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; | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 270 | 		} | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 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; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 277 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 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; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 285 | 	} | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 286 | 	mutex_unlock(&mbox_configured_lock); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 287 | 	return 0; | 
 | 288 |  | 
| Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 289 | fail_alloc_rxq: | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 290 | 	mbox_queue_free(mbox->txq); | 
| Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 291 | fail_alloc_txq: | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 292 | 	free_irq(mbox->irq, mbox); | 
| Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 293 | fail_request_irq: | 
| Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 294 | 	if (mbox->ops->shutdown) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 295 | 		mbox->ops->shutdown(mbox); | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 296 | 	mbox->use_count--; | 
 | 297 | fail_startup: | 
 | 298 | 	mbox_configured--; | 
 | 299 | 	mutex_unlock(&mbox_configured_lock); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 300 | 	return ret; | 
 | 301 | } | 
 | 302 |  | 
 | 303 | static void omap_mbox_fini(struct omap_mbox *mbox) | 
 | 304 | { | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 305 | 	mutex_lock(&mbox_configured_lock); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 306 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 307 | 	if (!--mbox->use_count) { | 
 | 308 | 		free_irq(mbox->irq, mbox); | 
 | 309 | 		tasklet_kill(&mbox->txq->tasklet); | 
| Felipe Contreras | 44c568a | 2012-03-05 16:11:03 -0800 | [diff] [blame] | 310 | 		flush_work_sync(&mbox->rxq->work); | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 311 | 		mbox_queue_free(mbox->txq); | 
 | 312 | 		mbox_queue_free(mbox->rxq); | 
| C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 313 | 	} | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 314 |  | 
 | 315 | 	if (likely(mbox->ops->shutdown)) { | 
 | 316 | 		if (!--mbox_configured) | 
 | 317 | 			mbox->ops->shutdown(mbox); | 
 | 318 | 	} | 
 | 319 |  | 
 | 320 | 	mutex_unlock(&mbox_configured_lock); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 321 | } | 
 | 322 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 323 | struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 324 | { | 
| Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 325 | 	struct omap_mbox *_mbox, *mbox = NULL; | 
 | 326 | 	int i, ret; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 327 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 328 | 	if (!mboxes) | 
 | 329 | 		return ERR_PTR(-EINVAL); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 330 |  | 
| Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 331 | 	for (i = 0; (_mbox = mboxes[i]); i++) { | 
 | 332 | 		if (!strcmp(_mbox->name, name)) { | 
 | 333 | 			mbox = _mbox; | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 334 | 			break; | 
| Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 335 | 		} | 
 | 336 | 	} | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 337 |  | 
 | 338 | 	if (!mbox) | 
 | 339 | 		return ERR_PTR(-ENOENT); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 340 |  | 
| Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 341 | 	ret = omap_mbox_startup(mbox); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 342 | 	if (ret) | 
 | 343 | 		return ERR_PTR(-ENODEV); | 
 | 344 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 345 | 	if (nb) | 
 | 346 | 		blocking_notifier_chain_register(&mbox->notifier, nb); | 
 | 347 |  | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 348 | 	return mbox; | 
 | 349 | } | 
 | 350 | EXPORT_SYMBOL(omap_mbox_get); | 
 | 351 |  | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 352 | void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 353 | { | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 354 | 	blocking_notifier_chain_unregister(&mbox->notifier, nb); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 355 | 	omap_mbox_fini(mbox); | 
 | 356 | } | 
 | 357 | EXPORT_SYMBOL(omap_mbox_put); | 
 | 358 |  | 
| Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 359 | static struct class omap_mbox_class = { .name = "mbox", }; | 
 | 360 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 361 | int omap_mbox_register(struct device *parent, struct omap_mbox **list) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 362 | { | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 363 | 	int ret; | 
 | 364 | 	int i; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 365 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 366 | 	mboxes = list; | 
 | 367 | 	if (!mboxes) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 368 | 		return -EINVAL; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 369 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 370 | 	for (i = 0; mboxes[i]; i++) { | 
 | 371 | 		struct omap_mbox *mbox = mboxes[i]; | 
 | 372 | 		mbox->dev = device_create(&omap_mbox_class, | 
 | 373 | 				parent, 0, mbox, "%s", mbox->name); | 
 | 374 | 		if (IS_ERR(mbox->dev)) { | 
 | 375 | 			ret = PTR_ERR(mbox->dev); | 
 | 376 | 			goto err_out; | 
 | 377 | 		} | 
| Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 378 |  | 
 | 379 | 		BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier); | 
| Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 380 | 	} | 
| Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 381 | 	return 0; | 
 | 382 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 383 | err_out: | 
 | 384 | 	while (i--) | 
 | 385 | 		device_unregister(mboxes[i]->dev); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 386 | 	return ret; | 
 | 387 | } | 
 | 388 | EXPORT_SYMBOL(omap_mbox_register); | 
 | 389 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 390 | int omap_mbox_unregister(void) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 391 | { | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 392 | 	int i; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 393 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 394 | 	if (!mboxes) | 
 | 395 | 		return -EINVAL; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 396 |  | 
| Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 397 | 	for (i = 0; mboxes[i]; i++) | 
 | 398 | 		device_unregister(mboxes[i]->dev); | 
 | 399 | 	mboxes = NULL; | 
 | 400 | 	return 0; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 401 | } | 
 | 402 | EXPORT_SYMBOL(omap_mbox_unregister); | 
 | 403 |  | 
| Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 404 | static int __init omap_mbox_init(void) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 405 | { | 
| Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 406 | 	int err; | 
 | 407 |  | 
 | 408 | 	err = class_register(&omap_mbox_class); | 
 | 409 | 	if (err) | 
 | 410 | 		return err; | 
 | 411 |  | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 412 | 	/* kfifo size sanity check: alignment and minimal size */ | 
 | 413 | 	mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); | 
| Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 414 | 	mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, | 
 | 415 | 							sizeof(mbox_msg_t)); | 
| Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 416 |  | 
| Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 417 | 	return 0; | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 418 | } | 
| Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 419 | subsys_initcall(omap_mbox_init); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 420 |  | 
| Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 421 | static void __exit omap_mbox_exit(void) | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 422 | { | 
| Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 423 | 	class_unregister(&omap_mbox_class); | 
| 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 | module_exit(omap_mbox_exit); | 
| Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 426 |  | 
| Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 427 | MODULE_LICENSE("GPL v2"); | 
 | 428 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); | 
| Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 429 | MODULE_AUTHOR("Toshihiro Kobayashi"); | 
 | 430 | MODULE_AUTHOR("Hiroshi DOYU"); |