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. |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 5 | * Copyright (C) 2013-2014 Texas Instruments Inc. |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 6 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 7 | * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com> |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 8 | * Suman Anna <s-anna@ti.com> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | * |
| 24 | */ |
| 25 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
Felipe Contreras | b3e6914 | 2010-06-11 15:51:49 +0000 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/mutex.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> |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 32 | #include <linux/notifier.h> |
Paul Gortmaker | 73017a5 | 2011-07-31 16:14:14 -0400 | [diff] [blame] | 33 | #include <linux/module.h> |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
| 35 | #include <linux/pm_runtime.h> |
| 36 | #include <linux/platform_data/mailbox-omap.h> |
| 37 | #include <linux/omap-mailbox.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 38 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 39 | #define MAILBOX_REVISION 0x000 |
| 40 | #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m)) |
| 41 | #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m)) |
| 42 | #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 43 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 44 | #define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u)) |
| 45 | #define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u)) |
| 46 | |
| 47 | #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u)) |
| 48 | #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u)) |
| 49 | #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u)) |
| 50 | |
| 51 | #define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \ |
| 52 | OMAP2_MAILBOX_IRQSTATUS(u)) |
| 53 | #define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \ |
| 54 | OMAP2_MAILBOX_IRQENABLE(u)) |
| 55 | #define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \ |
| 56 | : OMAP2_MAILBOX_IRQENABLE(u)) |
| 57 | |
| 58 | #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m))) |
| 59 | #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1)) |
| 60 | |
| 61 | #define MBOX_REG_SIZE 0x120 |
| 62 | |
| 63 | #define OMAP4_MBOX_REG_SIZE 0x130 |
| 64 | |
| 65 | #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32)) |
| 66 | #define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32)) |
| 67 | |
| 68 | struct omap_mbox_fifo { |
| 69 | unsigned long msg; |
| 70 | unsigned long fifo_stat; |
| 71 | unsigned long msg_stat; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 72 | unsigned long irqenable; |
| 73 | unsigned long irqstatus; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 74 | unsigned long irqdisable; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 75 | u32 intr_bit; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct omap_mbox_queue { |
| 79 | spinlock_t lock; |
| 80 | struct kfifo fifo; |
| 81 | struct work_struct work; |
| 82 | struct tasklet_struct tasklet; |
| 83 | struct omap_mbox *mbox; |
| 84 | bool full; |
| 85 | }; |
| 86 | |
| 87 | struct omap_mbox { |
| 88 | const char *name; |
| 89 | int irq; |
| 90 | struct omap_mbox_queue *txq, *rxq; |
| 91 | struct device *dev; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 92 | struct omap_mbox_fifo tx_fifo; |
| 93 | struct omap_mbox_fifo rx_fifo; |
| 94 | u32 ctx[OMAP4_MBOX_NR_REGS]; |
| 95 | u32 intr_type; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 96 | int use_count; |
| 97 | struct blocking_notifier_head notifier; |
| 98 | }; |
| 99 | |
| 100 | static void __iomem *mbox_base; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 101 | static struct omap_mbox **mboxes; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 102 | |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 103 | static DEFINE_MUTEX(mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 104 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 105 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; |
| 106 | module_param(mbox_kfifo_size, uint, S_IRUGO); |
| 107 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); |
| 108 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 109 | static inline unsigned int mbox_read_reg(size_t ofs) |
| 110 | { |
| 111 | return __raw_readl(mbox_base + ofs); |
| 112 | } |
| 113 | |
| 114 | static inline void mbox_write_reg(u32 val, size_t ofs) |
| 115 | { |
| 116 | __raw_writel(val, mbox_base + ofs); |
| 117 | } |
| 118 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 119 | /* Mailbox FIFO handle functions */ |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 120 | static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 121 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 122 | struct omap_mbox_fifo *fifo = &mbox->rx_fifo; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 123 | return (mbox_msg_t) mbox_read_reg(fifo->msg); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 124 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 125 | |
| 126 | static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 127 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 128 | struct omap_mbox_fifo *fifo = &mbox->tx_fifo; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 129 | mbox_write_reg(msg, fifo->msg); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 130 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 131 | |
| 132 | static int mbox_fifo_empty(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 133 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 134 | struct omap_mbox_fifo *fifo = &mbox->rx_fifo; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 135 | return (mbox_read_reg(fifo->msg_stat) == 0); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 136 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 137 | |
| 138 | static int mbox_fifo_full(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 139 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 140 | struct omap_mbox_fifo *fifo = &mbox->tx_fifo; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 141 | return mbox_read_reg(fifo->fifo_stat); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Mailbox IRQ handle functions */ |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 145 | static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 146 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 147 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 148 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 149 | u32 bit = fifo->intr_bit; |
| 150 | u32 irqstatus = fifo->irqstatus; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 151 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 152 | mbox_write_reg(bit, irqstatus); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 153 | |
| 154 | /* Flush posted write for irq status to avoid spurious interrupts */ |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 155 | mbox_read_reg(irqstatus); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 156 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 157 | |
| 158 | static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 159 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 160 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 161 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 162 | u32 bit = fifo->intr_bit; |
| 163 | u32 irqenable = fifo->irqenable; |
| 164 | u32 irqstatus = fifo->irqstatus; |
| 165 | |
| 166 | u32 enable = mbox_read_reg(irqenable); |
| 167 | u32 status = mbox_read_reg(irqstatus); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 168 | |
| 169 | return (int)(enable & status & bit); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 172 | /* |
| 173 | * message sender |
| 174 | */ |
C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 175 | 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] | 176 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 177 | struct omap_mbox_queue *mq = mbox->txq; |
| 178 | int ret = 0, len; |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 179 | |
Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 180 | spin_lock_bh(&mq->lock); |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 181 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 182 | if (kfifo_avail(&mq->fifo) < sizeof(msg)) { |
| 183 | ret = -ENOMEM; |
| 184 | goto out; |
| 185 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 186 | |
Suman Anna | fe714a4 | 2014-06-24 19:43:39 -0500 | [diff] [blame] | 187 | if (kfifo_is_empty(&mq->fifo) && !mbox_fifo_full(mbox)) { |
Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 188 | mbox_fifo_write(mbox, msg); |
| 189 | goto out; |
| 190 | } |
| 191 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 192 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 193 | WARN_ON(len != sizeof(msg)); |
| 194 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 195 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 196 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 197 | out: |
Kanigeri, Hari | a42743c | 2010-11-29 20:24:13 +0000 | [diff] [blame] | 198 | spin_unlock_bh(&mq->lock); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 199 | return ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 200 | } |
| 201 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 202 | |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 203 | void omap_mbox_save_ctx(struct omap_mbox *mbox) |
| 204 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 205 | int i; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 206 | int nr_regs; |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 207 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 208 | if (mbox->intr_type) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 209 | nr_regs = OMAP4_MBOX_NR_REGS; |
| 210 | else |
| 211 | nr_regs = MBOX_NR_REGS; |
| 212 | for (i = 0; i < nr_regs; i++) { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 213 | mbox->ctx[i] = mbox_read_reg(i * sizeof(u32)); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 214 | |
| 215 | dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__, |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 216 | i, mbox->ctx[i]); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 217 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 218 | } |
| 219 | EXPORT_SYMBOL(omap_mbox_save_ctx); |
| 220 | |
| 221 | void omap_mbox_restore_ctx(struct omap_mbox *mbox) |
| 222 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 223 | int i; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 224 | int nr_regs; |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 225 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 226 | if (mbox->intr_type) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 227 | nr_regs = OMAP4_MBOX_NR_REGS; |
| 228 | else |
| 229 | nr_regs = MBOX_NR_REGS; |
| 230 | for (i = 0; i < nr_regs; i++) { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 231 | mbox_write_reg(mbox->ctx[i], i * sizeof(u32)); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 232 | |
| 233 | dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__, |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 234 | i, mbox->ctx[i]); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 235 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 236 | } |
| 237 | EXPORT_SYMBOL(omap_mbox_restore_ctx); |
| 238 | |
| 239 | void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 240 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 241 | u32 l; |
| 242 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 243 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 244 | u32 bit = fifo->intr_bit; |
| 245 | u32 irqenable = fifo->irqenable; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 246 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 247 | l = mbox_read_reg(irqenable); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 248 | l |= bit; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 249 | mbox_write_reg(l, irqenable); |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL(omap_mbox_enable_irq); |
| 252 | |
| 253 | void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 254 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 255 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 256 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 257 | u32 bit = fifo->intr_bit; |
| 258 | u32 irqdisable = fifo->irqdisable; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Read and update the interrupt configuration register for pre-OMAP4. |
| 262 | * OMAP4 and later SoCs have a dedicated interrupt disabling register. |
| 263 | */ |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 264 | if (!mbox->intr_type) |
| 265 | bit = mbox_read_reg(irqdisable) & ~bit; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 266 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 267 | mbox_write_reg(bit, irqdisable); |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 268 | } |
| 269 | EXPORT_SYMBOL(omap_mbox_disable_irq); |
| 270 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 271 | static void mbox_tx_tasklet(unsigned long tx_data) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 272 | { |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 273 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 274 | struct omap_mbox_queue *mq = mbox->txq; |
| 275 | mbox_msg_t msg; |
| 276 | int ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 277 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 278 | while (kfifo_len(&mq->fifo)) { |
Suman Anna | fe714a4 | 2014-06-24 19:43:39 -0500 | [diff] [blame] | 279 | if (mbox_fifo_full(mbox)) { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 280 | omap_mbox_enable_irq(mbox, IRQ_TX); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 281 | break; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 282 | } |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 283 | |
| 284 | ret = kfifo_out(&mq->fifo, (unsigned char *)&msg, |
| 285 | sizeof(msg)); |
| 286 | WARN_ON(ret != sizeof(msg)); |
| 287 | |
| 288 | mbox_fifo_write(mbox, msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Message receiver(workqueue) |
| 294 | */ |
| 295 | static void mbox_rx_work(struct work_struct *work) |
| 296 | { |
| 297 | struct omap_mbox_queue *mq = |
| 298 | container_of(work, struct omap_mbox_queue, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 299 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 300 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 301 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 302 | while (kfifo_len(&mq->fifo) >= sizeof(msg)) { |
| 303 | len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 304 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 305 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 306 | blocking_notifier_call_chain(&mq->mbox->notifier, len, |
| 307 | (void *)msg); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 308 | spin_lock_irq(&mq->lock); |
| 309 | if (mq->full) { |
| 310 | mq->full = false; |
| 311 | omap_mbox_enable_irq(mq->mbox, IRQ_RX); |
| 312 | } |
| 313 | spin_unlock_irq(&mq->lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Mailbox interrupt handler |
| 319 | */ |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 320 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 321 | { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 322 | omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 323 | ack_mbox_irq(mbox, IRQ_TX); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 324 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 328 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 329 | struct omap_mbox_queue *mq = mbox->rxq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 330 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 331 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 332 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 333 | while (!mbox_fifo_empty(mbox)) { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 334 | if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 335 | omap_mbox_disable_irq(mbox, IRQ_RX); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 336 | mq->full = true; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 337 | goto nomem; |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 338 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 339 | |
| 340 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 341 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 342 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 343 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* no more messages in the fifo. clear IRQ source. */ |
| 347 | ack_mbox_irq(mbox, IRQ_RX); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 348 | nomem: |
Tejun Heo | c487300 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 349 | schedule_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 353 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 354 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 355 | |
| 356 | if (is_mbox_irq(mbox, IRQ_TX)) |
| 357 | __mbox_tx_interrupt(mbox); |
| 358 | |
| 359 | if (is_mbox_irq(mbox, IRQ_RX)) |
| 360 | __mbox_rx_interrupt(mbox); |
| 361 | |
| 362 | return IRQ_HANDLED; |
| 363 | } |
| 364 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 365 | 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] | 366 | void (*work) (struct work_struct *), |
| 367 | void (*tasklet)(unsigned long)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 368 | { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 369 | struct omap_mbox_queue *mq; |
| 370 | |
| 371 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); |
| 372 | if (!mq) |
| 373 | return NULL; |
| 374 | |
| 375 | spin_lock_init(&mq->lock); |
| 376 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 377 | if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 378 | goto error; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 379 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 380 | if (work) |
| 381 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 382 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 383 | if (tasklet) |
| 384 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 385 | return mq; |
| 386 | error: |
| 387 | kfree(mq); |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 392 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 393 | kfifo_free(&q->fifo); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 394 | kfree(q); |
| 395 | } |
| 396 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 397 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 398 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 399 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 400 | struct omap_mbox_queue *mq; |
| 401 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 402 | mutex_lock(&mbox_configured_lock); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 403 | ret = pm_runtime_get_sync(mbox->dev->parent); |
| 404 | if (unlikely(ret < 0)) |
| 405 | goto fail_startup; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 406 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 407 | if (!mbox->use_count++) { |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 408 | mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet); |
| 409 | if (!mq) { |
| 410 | ret = -ENOMEM; |
| 411 | goto fail_alloc_txq; |
| 412 | } |
| 413 | mbox->txq = mq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 414 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 415 | mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL); |
| 416 | if (!mq) { |
| 417 | ret = -ENOMEM; |
| 418 | goto fail_alloc_rxq; |
| 419 | } |
| 420 | mbox->rxq = mq; |
| 421 | mq->mbox = mbox; |
Suman Anna | ecf305c | 2013-02-01 20:37:06 -0600 | [diff] [blame] | 422 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
| 423 | mbox->name, mbox); |
| 424 | if (unlikely(ret)) { |
| 425 | pr_err("failed to register mailbox interrupt:%d\n", |
| 426 | ret); |
| 427 | goto fail_request_irq; |
| 428 | } |
Juan Gutierrez | 1d8a0e9 | 2012-05-13 15:33:04 +0300 | [diff] [blame] | 429 | |
| 430 | omap_mbox_enable_irq(mbox, IRQ_RX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 431 | } |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 432 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 433 | return 0; |
| 434 | |
Suman Anna | ecf305c | 2013-02-01 20:37:06 -0600 | [diff] [blame] | 435 | fail_request_irq: |
| 436 | mbox_queue_free(mbox->rxq); |
Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 437 | fail_alloc_rxq: |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 438 | mbox_queue_free(mbox->txq); |
Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 439 | fail_alloc_txq: |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 440 | pm_runtime_put_sync(mbox->dev->parent); |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 441 | mbox->use_count--; |
| 442 | fail_startup: |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 443 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 448 | { |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 449 | mutex_lock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 450 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 451 | if (!--mbox->use_count) { |
Juan Gutierrez | 1d8a0e9 | 2012-05-13 15:33:04 +0300 | [diff] [blame] | 452 | omap_mbox_disable_irq(mbox, IRQ_RX); |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 453 | free_irq(mbox->irq, mbox); |
| 454 | tasklet_kill(&mbox->txq->tasklet); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 455 | flush_work(&mbox->rxq->work); |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 456 | mbox_queue_free(mbox->txq); |
| 457 | mbox_queue_free(mbox->rxq); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 458 | } |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 459 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 460 | pm_runtime_put_sync(mbox->dev->parent); |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 461 | |
| 462 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 465 | 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] | 466 | { |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 467 | struct omap_mbox *_mbox, *mbox = NULL; |
| 468 | int i, ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 469 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 470 | if (!mboxes) |
| 471 | return ERR_PTR(-EINVAL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 472 | |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 473 | for (i = 0; (_mbox = mboxes[i]); i++) { |
| 474 | if (!strcmp(_mbox->name, name)) { |
| 475 | mbox = _mbox; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 476 | break; |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 479 | |
| 480 | if (!mbox) |
| 481 | return ERR_PTR(-ENOENT); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 482 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 483 | if (nb) |
| 484 | blocking_notifier_chain_register(&mbox->notifier, nb); |
| 485 | |
Juan Gutierrez | 1d8a0e9 | 2012-05-13 15:33:04 +0300 | [diff] [blame] | 486 | ret = omap_mbox_startup(mbox); |
| 487 | if (ret) { |
| 488 | blocking_notifier_chain_unregister(&mbox->notifier, nb); |
| 489 | return ERR_PTR(-ENODEV); |
| 490 | } |
| 491 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 492 | return mbox; |
| 493 | } |
| 494 | EXPORT_SYMBOL(omap_mbox_get); |
| 495 | |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 496 | void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 497 | { |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 498 | blocking_notifier_chain_unregister(&mbox->notifier, nb); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 499 | omap_mbox_fini(mbox); |
| 500 | } |
| 501 | EXPORT_SYMBOL(omap_mbox_put); |
| 502 | |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 503 | static struct class omap_mbox_class = { .name = "mbox", }; |
| 504 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 505 | static int omap_mbox_register(struct device *parent, struct omap_mbox **list) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 506 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 507 | int ret; |
| 508 | int i; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 509 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 510 | mboxes = list; |
| 511 | if (!mboxes) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 512 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 513 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 514 | for (i = 0; mboxes[i]; i++) { |
| 515 | struct omap_mbox *mbox = mboxes[i]; |
| 516 | mbox->dev = device_create(&omap_mbox_class, |
| 517 | parent, 0, mbox, "%s", mbox->name); |
| 518 | if (IS_ERR(mbox->dev)) { |
| 519 | ret = PTR_ERR(mbox->dev); |
| 520 | goto err_out; |
| 521 | } |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 522 | |
| 523 | BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 524 | } |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 527 | err_out: |
| 528 | while (i--) |
| 529 | device_unregister(mboxes[i]->dev); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 530 | return ret; |
| 531 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 532 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 533 | static int omap_mbox_unregister(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 534 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 535 | int i; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 536 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 537 | if (!mboxes) |
| 538 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 539 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 540 | for (i = 0; mboxes[i]; i++) |
| 541 | device_unregister(mboxes[i]->dev); |
| 542 | mboxes = NULL; |
| 543 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 544 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 545 | |
| 546 | static int omap_mbox_probe(struct platform_device *pdev) |
| 547 | { |
| 548 | struct resource *mem; |
| 549 | int ret; |
| 550 | struct omap_mbox **list, *mbox, *mboxblk; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 551 | struct omap_mbox_pdata *pdata = pdev->dev.platform_data; |
| 552 | struct omap_mbox_dev_info *info; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 553 | struct omap_mbox_fifo *fifo; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 554 | u32 intr_type; |
| 555 | u32 l; |
| 556 | int i; |
| 557 | |
| 558 | if (!pdata || !pdata->info_cnt || !pdata->info) { |
| 559 | pr_err("%s: platform not supported\n", __func__); |
| 560 | return -ENODEV; |
| 561 | } |
| 562 | |
| 563 | /* allocate one extra for marking end of list */ |
| 564 | list = devm_kzalloc(&pdev->dev, (pdata->info_cnt + 1) * sizeof(*list), |
| 565 | GFP_KERNEL); |
| 566 | if (!list) |
| 567 | return -ENOMEM; |
| 568 | |
| 569 | mboxblk = devm_kzalloc(&pdev->dev, pdata->info_cnt * sizeof(*mbox), |
| 570 | GFP_KERNEL); |
| 571 | if (!mboxblk) |
| 572 | return -ENOMEM; |
| 573 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 574 | info = pdata->info; |
| 575 | intr_type = pdata->intr_type; |
| 576 | mbox = mboxblk; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 577 | for (i = 0; i < pdata->info_cnt; i++, info++) { |
| 578 | fifo = &mbox->tx_fifo; |
| 579 | fifo->msg = MAILBOX_MESSAGE(info->tx_id); |
| 580 | fifo->fifo_stat = MAILBOX_FIFOSTATUS(info->tx_id); |
| 581 | fifo->intr_bit = MAILBOX_IRQ_NOTFULL(info->tx_id); |
| 582 | fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id); |
| 583 | fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id); |
| 584 | fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 585 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame^] | 586 | fifo = &mbox->rx_fifo; |
| 587 | fifo->msg = MAILBOX_MESSAGE(info->rx_id); |
| 588 | fifo->msg_stat = MAILBOX_MSGSTATUS(info->rx_id); |
| 589 | fifo->intr_bit = MAILBOX_IRQ_NEWMSG(info->rx_id); |
| 590 | fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id); |
| 591 | fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id); |
| 592 | fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id); |
| 593 | |
| 594 | mbox->intr_type = intr_type; |
| 595 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 596 | mbox->name = info->name; |
| 597 | mbox->irq = platform_get_irq(pdev, info->irq_id); |
| 598 | if (mbox->irq < 0) |
| 599 | return mbox->irq; |
| 600 | list[i] = mbox++; |
| 601 | } |
| 602 | |
| 603 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 604 | mbox_base = devm_ioremap_resource(&pdev->dev, mem); |
| 605 | if (IS_ERR(mbox_base)) |
| 606 | return PTR_ERR(mbox_base); |
| 607 | |
| 608 | ret = omap_mbox_register(&pdev->dev, list); |
| 609 | if (ret) |
| 610 | return ret; |
| 611 | |
| 612 | platform_set_drvdata(pdev, list); |
| 613 | pm_runtime_enable(&pdev->dev); |
| 614 | |
| 615 | ret = pm_runtime_get_sync(&pdev->dev); |
| 616 | if (ret < 0) { |
| 617 | pm_runtime_put_noidle(&pdev->dev); |
| 618 | goto unregister; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * just print the raw revision register, the format is not |
| 623 | * uniform across all SoCs |
| 624 | */ |
| 625 | l = mbox_read_reg(MAILBOX_REVISION); |
| 626 | dev_info(&pdev->dev, "omap mailbox rev 0x%x\n", l); |
| 627 | |
| 628 | ret = pm_runtime_put_sync(&pdev->dev); |
| 629 | if (ret < 0) |
| 630 | goto unregister; |
| 631 | |
| 632 | return 0; |
| 633 | |
| 634 | unregister: |
| 635 | pm_runtime_disable(&pdev->dev); |
| 636 | omap_mbox_unregister(); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | static int omap_mbox_remove(struct platform_device *pdev) |
| 641 | { |
| 642 | pm_runtime_disable(&pdev->dev); |
| 643 | omap_mbox_unregister(); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static struct platform_driver omap_mbox_driver = { |
| 649 | .probe = omap_mbox_probe, |
| 650 | .remove = omap_mbox_remove, |
| 651 | .driver = { |
| 652 | .name = "omap-mailbox", |
| 653 | .owner = THIS_MODULE, |
| 654 | }, |
| 655 | }; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 656 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 657 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 658 | { |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 659 | int err; |
| 660 | |
| 661 | err = class_register(&omap_mbox_class); |
| 662 | if (err) |
| 663 | return err; |
| 664 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 665 | /* kfifo size sanity check: alignment and minimal size */ |
| 666 | mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); |
Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 667 | mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, |
| 668 | sizeof(mbox_msg_t)); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 669 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 670 | return platform_driver_register(&omap_mbox_driver); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 671 | } |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 672 | subsys_initcall(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 673 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 674 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 675 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 676 | platform_driver_unregister(&omap_mbox_driver); |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 677 | class_unregister(&omap_mbox_class); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 678 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 679 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 680 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 681 | MODULE_LICENSE("GPL v2"); |
| 682 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 683 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
| 684 | MODULE_AUTHOR("Hiroshi DOYU"); |