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 | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 5 | * Copyright (C) 2013-2016 Texas Instruments Incorporated - http://www.ti.com |
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. |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Felipe Contreras | b3e6914 | 2010-06-11 15:51:49 +0000 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 24 | #include <linux/kfifo.h> |
| 25 | #include <linux/err.h> |
Paul Gortmaker | 73017a5 | 2011-07-31 16:14:14 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 27 | #include <linux/of_device.h> |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/pm_runtime.h> |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 30 | #include <linux/omap-mailbox.h> |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 31 | #include <linux/mailbox_controller.h> |
| 32 | #include <linux/mailbox_client.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 33 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 34 | #include "mailbox.h" |
| 35 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 36 | #define MAILBOX_REVISION 0x000 |
| 37 | #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m)) |
| 38 | #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m)) |
| 39 | #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 40 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 41 | #define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u)) |
| 42 | #define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u)) |
| 43 | |
| 44 | #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u)) |
| 45 | #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u)) |
| 46 | #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u)) |
| 47 | |
| 48 | #define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \ |
| 49 | OMAP2_MAILBOX_IRQSTATUS(u)) |
| 50 | #define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \ |
| 51 | OMAP2_MAILBOX_IRQENABLE(u)) |
| 52 | #define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \ |
| 53 | : OMAP2_MAILBOX_IRQENABLE(u)) |
| 54 | |
| 55 | #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m))) |
| 56 | #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1)) |
| 57 | |
| 58 | #define MBOX_REG_SIZE 0x120 |
| 59 | |
| 60 | #define OMAP4_MBOX_REG_SIZE 0x130 |
| 61 | |
| 62 | #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32)) |
| 63 | #define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32)) |
| 64 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 65 | /* Interrupt register configuration types */ |
| 66 | #define MBOX_INTR_CFG_TYPE1 0 |
| 67 | #define MBOX_INTR_CFG_TYPE2 1 |
| 68 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 69 | struct omap_mbox_fifo { |
| 70 | unsigned long msg; |
| 71 | unsigned long fifo_stat; |
| 72 | unsigned long msg_stat; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 73 | unsigned long irqenable; |
| 74 | unsigned long irqstatus; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 75 | unsigned long irqdisable; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 76 | u32 intr_bit; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct omap_mbox_queue { |
| 80 | spinlock_t lock; |
| 81 | struct kfifo fifo; |
| 82 | struct work_struct work; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 83 | struct omap_mbox *mbox; |
| 84 | bool full; |
| 85 | }; |
| 86 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 87 | struct omap_mbox_device { |
| 88 | struct device *dev; |
| 89 | struct mutex cfg_lock; |
| 90 | void __iomem *mbox_base; |
| 91 | u32 num_users; |
| 92 | u32 num_fifos; |
| 93 | struct omap_mbox **mboxes; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 94 | struct mbox_controller controller; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 95 | struct list_head elem; |
| 96 | }; |
| 97 | |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 98 | struct omap_mbox_fifo_info { |
| 99 | int tx_id; |
| 100 | int tx_usr; |
| 101 | int tx_irq; |
| 102 | |
| 103 | int rx_id; |
| 104 | int rx_usr; |
| 105 | int rx_irq; |
| 106 | |
| 107 | const char *name; |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 108 | bool send_no_irq; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 111 | struct omap_mbox { |
| 112 | const char *name; |
| 113 | int irq; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 114 | struct omap_mbox_queue *rxq; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 115 | struct device *dev; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 116 | struct omap_mbox_device *parent; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 117 | struct omap_mbox_fifo tx_fifo; |
| 118 | struct omap_mbox_fifo rx_fifo; |
| 119 | u32 ctx[OMAP4_MBOX_NR_REGS]; |
| 120 | u32 intr_type; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 121 | struct mbox_chan *chan; |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 122 | bool send_no_irq; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 125 | /* global variables for the mailbox devices */ |
| 126 | static DEFINE_MUTEX(omap_mbox_devices_lock); |
| 127 | static LIST_HEAD(omap_mbox_devices); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 128 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 129 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; |
| 130 | module_param(mbox_kfifo_size, uint, S_IRUGO); |
| 131 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); |
| 132 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 133 | static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan) |
| 134 | { |
| 135 | if (!chan || !chan->con_priv) |
| 136 | return NULL; |
| 137 | |
| 138 | return (struct omap_mbox *)chan->con_priv; |
| 139 | } |
| 140 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 141 | static inline |
| 142 | unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 143 | { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 144 | return __raw_readl(mdev->mbox_base + ofs); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 147 | static inline |
| 148 | void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 149 | { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 150 | __raw_writel(val, mdev->mbox_base + ofs); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 153 | /* Mailbox FIFO handle functions */ |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 154 | static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 155 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 156 | struct omap_mbox_fifo *fifo = &mbox->rx_fifo; |
Suman Anna | 2665a4c | 2016-04-06 12:37:40 -0500 | [diff] [blame^] | 157 | |
| 158 | return (mbox_msg_t)mbox_read_reg(mbox->parent, fifo->msg); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 159 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 160 | |
| 161 | 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] | 162 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 163 | struct omap_mbox_fifo *fifo = &mbox->tx_fifo; |
Suman Anna | 2665a4c | 2016-04-06 12:37:40 -0500 | [diff] [blame^] | 164 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 165 | mbox_write_reg(mbox->parent, msg, fifo->msg); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 166 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 167 | |
| 168 | static int mbox_fifo_empty(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 169 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 170 | struct omap_mbox_fifo *fifo = &mbox->rx_fifo; |
Suman Anna | 2665a4c | 2016-04-06 12:37:40 -0500 | [diff] [blame^] | 171 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 172 | return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 173 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 174 | |
| 175 | static int mbox_fifo_full(struct omap_mbox *mbox) |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 176 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 177 | struct omap_mbox_fifo *fifo = &mbox->tx_fifo; |
Suman Anna | 2665a4c | 2016-04-06 12:37:40 -0500 | [diff] [blame^] | 178 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 179 | return mbox_read_reg(mbox->parent, fifo->fifo_stat); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Mailbox IRQ handle functions */ |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 183 | 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] | 184 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 185 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 186 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 187 | u32 bit = fifo->intr_bit; |
| 188 | u32 irqstatus = fifo->irqstatus; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 189 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 190 | mbox_write_reg(mbox->parent, bit, irqstatus); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 191 | |
| 192 | /* Flush posted write for irq status to avoid spurious interrupts */ |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 193 | mbox_read_reg(mbox->parent, irqstatus); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 194 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 195 | |
| 196 | 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] | 197 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 198 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 199 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 200 | u32 bit = fifo->intr_bit; |
| 201 | u32 irqenable = fifo->irqenable; |
| 202 | u32 irqstatus = fifo->irqstatus; |
| 203 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 204 | u32 enable = mbox_read_reg(mbox->parent, irqenable); |
| 205 | u32 status = mbox_read_reg(mbox->parent, irqstatus); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 206 | |
| 207 | return (int)(enable & status & bit); |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 210 | void omap_mbox_save_ctx(struct mbox_chan *chan) |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 211 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 212 | int i; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 213 | int nr_regs; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 214 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 215 | |
| 216 | if (WARN_ON(!mbox)) |
| 217 | return; |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 218 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 219 | if (mbox->intr_type) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 220 | nr_regs = OMAP4_MBOX_NR_REGS; |
| 221 | else |
| 222 | nr_regs = MBOX_NR_REGS; |
| 223 | for (i = 0; i < nr_regs; i++) { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 224 | mbox->ctx[i] = mbox_read_reg(mbox->parent, i * sizeof(u32)); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 225 | |
| 226 | dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__, |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 227 | i, mbox->ctx[i]); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 228 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 229 | } |
| 230 | EXPORT_SYMBOL(omap_mbox_save_ctx); |
| 231 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 232 | void omap_mbox_restore_ctx(struct mbox_chan *chan) |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 233 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 234 | int i; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 235 | int nr_regs; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 236 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 237 | |
| 238 | if (WARN_ON(!mbox)) |
| 239 | return; |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 240 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 241 | if (mbox->intr_type) |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 242 | nr_regs = OMAP4_MBOX_NR_REGS; |
| 243 | else |
| 244 | nr_regs = MBOX_NR_REGS; |
| 245 | for (i = 0; i < nr_regs; i++) { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 246 | mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32)); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 247 | dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__, |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 248 | i, mbox->ctx[i]); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 249 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL(omap_mbox_restore_ctx); |
| 252 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 253 | static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 254 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 255 | u32 l; |
| 256 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 257 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 258 | u32 bit = fifo->intr_bit; |
| 259 | u32 irqenable = fifo->irqenable; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 260 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 261 | l = mbox_read_reg(mbox->parent, irqenable); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 262 | l |= bit; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 263 | mbox_write_reg(mbox->parent, l, irqenable); |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 264 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 265 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 266 | static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 267 | { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 268 | struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ? |
| 269 | &mbox->tx_fifo : &mbox->rx_fifo; |
| 270 | u32 bit = fifo->intr_bit; |
| 271 | u32 irqdisable = fifo->irqdisable; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Read and update the interrupt configuration register for pre-OMAP4. |
| 275 | * OMAP4 and later SoCs have a dedicated interrupt disabling register. |
| 276 | */ |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 277 | if (!mbox->intr_type) |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 278 | bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 279 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 280 | mbox_write_reg(mbox->parent, bit, irqdisable); |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 281 | } |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 282 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 283 | void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 284 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 285 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 286 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 287 | if (WARN_ON(!mbox)) |
| 288 | return; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 289 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 290 | _omap_mbox_enable_irq(mbox, irq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 291 | } |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 292 | EXPORT_SYMBOL(omap_mbox_enable_irq); |
| 293 | |
| 294 | void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq) |
| 295 | { |
| 296 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 297 | |
| 298 | if (WARN_ON(!mbox)) |
| 299 | return; |
| 300 | |
| 301 | _omap_mbox_disable_irq(mbox, irq); |
| 302 | } |
| 303 | EXPORT_SYMBOL(omap_mbox_disable_irq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Message receiver(workqueue) |
| 307 | */ |
| 308 | static void mbox_rx_work(struct work_struct *work) |
| 309 | { |
| 310 | struct omap_mbox_queue *mq = |
| 311 | container_of(work, struct omap_mbox_queue, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 312 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 313 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 314 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 315 | while (kfifo_len(&mq->fifo) >= sizeof(msg)) { |
| 316 | len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 317 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 318 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 319 | mbox_chan_received_data(mq->mbox->chan, (void *)msg); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 320 | spin_lock_irq(&mq->lock); |
| 321 | if (mq->full) { |
| 322 | mq->full = false; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 323 | _omap_mbox_enable_irq(mq->mbox, IRQ_RX); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 324 | } |
| 325 | spin_unlock_irq(&mq->lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Mailbox interrupt handler |
| 331 | */ |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 332 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 333 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 334 | _omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 335 | ack_mbox_irq(mbox, IRQ_TX); |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 336 | mbox_chan_txdone(mbox->chan, 0); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 340 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 341 | struct omap_mbox_queue *mq = mbox->rxq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 342 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 343 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 344 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 345 | while (!mbox_fifo_empty(mbox)) { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 346 | if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 347 | _omap_mbox_disable_irq(mbox, IRQ_RX); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame] | 348 | mq->full = true; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 349 | goto nomem; |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 350 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 351 | |
| 352 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 353 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 354 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 355 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* no more messages in the fifo. clear IRQ source. */ |
| 359 | ack_mbox_irq(mbox, IRQ_RX); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 360 | nomem: |
Tejun Heo | c487300 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 361 | schedule_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 365 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 366 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 367 | |
| 368 | if (is_mbox_irq(mbox, IRQ_TX)) |
| 369 | __mbox_tx_interrupt(mbox); |
| 370 | |
| 371 | if (is_mbox_irq(mbox, IRQ_RX)) |
| 372 | __mbox_rx_interrupt(mbox); |
| 373 | |
| 374 | return IRQ_HANDLED; |
| 375 | } |
| 376 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 377 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 378 | void (*work)(struct work_struct *)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 379 | { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 380 | struct omap_mbox_queue *mq; |
| 381 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 382 | if (!work) |
| 383 | return NULL; |
| 384 | |
Suman Anna | 86f6f5e | 2016-04-06 12:37:38 -0500 | [diff] [blame] | 385 | mq = kzalloc(sizeof(*mq), GFP_KERNEL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 386 | if (!mq) |
| 387 | return NULL; |
| 388 | |
| 389 | spin_lock_init(&mq->lock); |
| 390 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 391 | if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 392 | goto error; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 393 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 394 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 395 | return mq; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 396 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 397 | error: |
| 398 | kfree(mq); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 403 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 404 | kfifo_free(&q->fifo); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 405 | kfree(q); |
| 406 | } |
| 407 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 408 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 409 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 410 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 411 | struct omap_mbox_queue *mq; |
| 412 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 413 | mq = mbox_queue_alloc(mbox, mbox_rx_work); |
| 414 | if (!mq) |
| 415 | return -ENOMEM; |
| 416 | mbox->rxq = mq; |
| 417 | mq->mbox = mbox; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 418 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 419 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
| 420 | mbox->name, mbox); |
| 421 | if (unlikely(ret)) { |
| 422 | pr_err("failed to register mailbox interrupt:%d\n", ret); |
| 423 | goto fail_request_irq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 424 | } |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 425 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 426 | if (mbox->send_no_irq) |
| 427 | mbox->chan->txdone_method = TXDONE_BY_ACK; |
| 428 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 429 | _omap_mbox_enable_irq(mbox, IRQ_RX); |
| 430 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 431 | return 0; |
| 432 | |
Suman Anna | ecf305c | 2013-02-01 20:37:06 -0600 | [diff] [blame] | 433 | fail_request_irq: |
| 434 | mbox_queue_free(mbox->rxq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 439 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 440 | _omap_mbox_disable_irq(mbox, IRQ_RX); |
| 441 | free_irq(mbox->irq, mbox); |
| 442 | flush_work(&mbox->rxq->work); |
| 443 | mbox_queue_free(mbox->rxq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 446 | static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev, |
| 447 | const char *mbox_name) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 448 | { |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 449 | struct omap_mbox *_mbox, *mbox = NULL; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 450 | struct omap_mbox **mboxes = mdev->mboxes; |
| 451 | int i; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 452 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 453 | if (!mboxes) |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 454 | return NULL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 455 | |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 456 | for (i = 0; (_mbox = mboxes[i]); i++) { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 457 | if (!strcmp(_mbox->name, mbox_name)) { |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 458 | mbox = _mbox; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 459 | break; |
Kevin Hilman | c037732 | 2011-02-11 19:56:43 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 462 | return mbox; |
| 463 | } |
| 464 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 465 | struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl, |
| 466 | const char *chan_name) |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 467 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 468 | struct device *dev = cl->dev; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 469 | struct omap_mbox *mbox = NULL; |
| 470 | struct omap_mbox_device *mdev; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 471 | struct mbox_chan *chan; |
| 472 | unsigned long flags; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 473 | int ret; |
| 474 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 475 | if (!dev) |
| 476 | return ERR_PTR(-ENODEV); |
| 477 | |
| 478 | if (dev->of_node) { |
| 479 | pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n", |
| 480 | __func__); |
| 481 | return ERR_PTR(-ENODEV); |
| 482 | } |
| 483 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 484 | mutex_lock(&omap_mbox_devices_lock); |
| 485 | list_for_each_entry(mdev, &omap_mbox_devices, elem) { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 486 | mbox = omap_mbox_device_find(mdev, chan_name); |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 487 | if (mbox) |
| 488 | break; |
| 489 | } |
| 490 | mutex_unlock(&omap_mbox_devices_lock); |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 491 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 492 | if (!mbox || !mbox->chan) |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 493 | return ERR_PTR(-ENOENT); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 494 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 495 | chan = mbox->chan; |
| 496 | spin_lock_irqsave(&chan->lock, flags); |
| 497 | chan->msg_free = 0; |
| 498 | chan->msg_count = 0; |
| 499 | chan->active_req = NULL; |
| 500 | chan->cl = cl; |
| 501 | init_completion(&chan->tx_complete); |
| 502 | spin_unlock_irqrestore(&chan->lock, flags); |
Kanigeri, Hari | 5825630 | 2010-11-29 20:24:14 +0000 | [diff] [blame] | 503 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 504 | ret = chan->mbox->ops->startup(chan); |
Juan Gutierrez | 1d8a0e9 | 2012-05-13 15:33:04 +0300 | [diff] [blame] | 505 | if (ret) { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 506 | pr_err("Unable to startup the chan (%d)\n", ret); |
| 507 | mbox_free_channel(chan); |
| 508 | chan = ERR_PTR(ret); |
Juan Gutierrez | 1d8a0e9 | 2012-05-13 15:33:04 +0300 | [diff] [blame] | 509 | } |
| 510 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 511 | return chan; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 512 | } |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 513 | EXPORT_SYMBOL(omap_mbox_request_channel); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 514 | |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 515 | static struct class omap_mbox_class = { .name = "mbox", }; |
| 516 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 517 | static int omap_mbox_register(struct omap_mbox_device *mdev) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 518 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 519 | int ret; |
| 520 | int i; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 521 | struct omap_mbox **mboxes; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 522 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 523 | if (!mdev || !mdev->mboxes) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 524 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 525 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 526 | mboxes = mdev->mboxes; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 527 | for (i = 0; mboxes[i]; i++) { |
| 528 | struct omap_mbox *mbox = mboxes[i]; |
Suman Anna | 2665a4c | 2016-04-06 12:37:40 -0500 | [diff] [blame^] | 529 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 530 | mbox->dev = device_create(&omap_mbox_class, mdev->dev, |
| 531 | 0, mbox, "%s", mbox->name); |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 532 | if (IS_ERR(mbox->dev)) { |
| 533 | ret = PTR_ERR(mbox->dev); |
| 534 | goto err_out; |
| 535 | } |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 536 | } |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 537 | |
| 538 | mutex_lock(&omap_mbox_devices_lock); |
| 539 | list_add(&mdev->elem, &omap_mbox_devices); |
| 540 | mutex_unlock(&omap_mbox_devices_lock); |
| 541 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 542 | ret = mbox_controller_register(&mdev->controller); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 543 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 544 | err_out: |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 545 | if (ret) { |
| 546 | while (i--) |
| 547 | device_unregister(mboxes[i]->dev); |
| 548 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 549 | return ret; |
| 550 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 551 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 552 | static int omap_mbox_unregister(struct omap_mbox_device *mdev) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 553 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 554 | int i; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 555 | struct omap_mbox **mboxes; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 556 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 557 | if (!mdev || !mdev->mboxes) |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 558 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 559 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 560 | mutex_lock(&omap_mbox_devices_lock); |
| 561 | list_del(&mdev->elem); |
| 562 | mutex_unlock(&omap_mbox_devices_lock); |
| 563 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 564 | mbox_controller_unregister(&mdev->controller); |
| 565 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 566 | mboxes = mdev->mboxes; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 567 | for (i = 0; mboxes[i]; i++) |
| 568 | device_unregister(mboxes[i]->dev); |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 569 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 570 | } |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 571 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 572 | static int omap_mbox_chan_startup(struct mbox_chan *chan) |
| 573 | { |
| 574 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 575 | struct omap_mbox_device *mdev = mbox->parent; |
| 576 | int ret = 0; |
| 577 | |
| 578 | mutex_lock(&mdev->cfg_lock); |
| 579 | pm_runtime_get_sync(mdev->dev); |
| 580 | ret = omap_mbox_startup(mbox); |
| 581 | if (ret) |
| 582 | pm_runtime_put_sync(mdev->dev); |
| 583 | mutex_unlock(&mdev->cfg_lock); |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | static void omap_mbox_chan_shutdown(struct mbox_chan *chan) |
| 588 | { |
| 589 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 590 | struct omap_mbox_device *mdev = mbox->parent; |
| 591 | |
| 592 | mutex_lock(&mdev->cfg_lock); |
| 593 | omap_mbox_fini(mbox); |
| 594 | pm_runtime_put_sync(mdev->dev); |
| 595 | mutex_unlock(&mdev->cfg_lock); |
| 596 | } |
| 597 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 598 | static int omap_mbox_chan_send_noirq(struct omap_mbox *mbox, void *data) |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 599 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 600 | int ret = -EBUSY; |
| 601 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 602 | if (!mbox_fifo_full(mbox)) { |
| 603 | _omap_mbox_enable_irq(mbox, IRQ_RX); |
| 604 | mbox_fifo_write(mbox, (mbox_msg_t)data); |
| 605 | ret = 0; |
| 606 | _omap_mbox_disable_irq(mbox, IRQ_RX); |
| 607 | |
| 608 | /* we must read and ack the interrupt directly from here */ |
| 609 | mbox_fifo_read(mbox); |
| 610 | ack_mbox_irq(mbox, IRQ_RX); |
| 611 | } |
| 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | static int omap_mbox_chan_send(struct omap_mbox *mbox, void *data) |
| 617 | { |
| 618 | int ret = -EBUSY; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 619 | |
| 620 | if (!mbox_fifo_full(mbox)) { |
| 621 | mbox_fifo_write(mbox, (mbox_msg_t)data); |
| 622 | ret = 0; |
| 623 | } |
| 624 | |
| 625 | /* always enable the interrupt */ |
| 626 | _omap_mbox_enable_irq(mbox, IRQ_TX); |
| 627 | return ret; |
| 628 | } |
| 629 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 630 | static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data) |
| 631 | { |
| 632 | struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan); |
| 633 | int ret; |
| 634 | |
| 635 | if (!mbox) |
| 636 | return -EINVAL; |
| 637 | |
| 638 | if (mbox->send_no_irq) |
| 639 | ret = omap_mbox_chan_send_noirq(mbox, data); |
| 640 | else |
| 641 | ret = omap_mbox_chan_send(mbox, data); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
Andrew Bresticker | 05ae797 | 2015-05-04 10:36:35 -0700 | [diff] [blame] | 646 | static const struct mbox_chan_ops omap_mbox_chan_ops = { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 647 | .startup = omap_mbox_chan_startup, |
| 648 | .send_data = omap_mbox_chan_send_data, |
| 649 | .shutdown = omap_mbox_chan_shutdown, |
| 650 | }; |
| 651 | |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 652 | static const struct of_device_id omap_mailbox_of_match[] = { |
| 653 | { |
| 654 | .compatible = "ti,omap2-mailbox", |
| 655 | .data = (void *)MBOX_INTR_CFG_TYPE1, |
| 656 | }, |
| 657 | { |
| 658 | .compatible = "ti,omap3-mailbox", |
| 659 | .data = (void *)MBOX_INTR_CFG_TYPE1, |
| 660 | }, |
| 661 | { |
| 662 | .compatible = "ti,omap4-mailbox", |
| 663 | .data = (void *)MBOX_INTR_CFG_TYPE2, |
| 664 | }, |
| 665 | { |
| 666 | /* end */ |
| 667 | }, |
| 668 | }; |
| 669 | MODULE_DEVICE_TABLE(of, omap_mailbox_of_match); |
| 670 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 671 | static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller, |
| 672 | const struct of_phandle_args *sp) |
| 673 | { |
| 674 | phandle phandle = sp->args[0]; |
| 675 | struct device_node *node; |
| 676 | struct omap_mbox_device *mdev; |
| 677 | struct omap_mbox *mbox; |
| 678 | |
| 679 | mdev = container_of(controller, struct omap_mbox_device, controller); |
| 680 | if (WARN_ON(!mdev)) |
Benson Leung | 2d805fc | 2015-05-04 10:36:36 -0700 | [diff] [blame] | 681 | return ERR_PTR(-EINVAL); |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 682 | |
| 683 | node = of_find_node_by_phandle(phandle); |
| 684 | if (!node) { |
| 685 | pr_err("%s: could not find node phandle 0x%x\n", |
| 686 | __func__, phandle); |
Benson Leung | 2d805fc | 2015-05-04 10:36:36 -0700 | [diff] [blame] | 687 | return ERR_PTR(-ENODEV); |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | mbox = omap_mbox_device_find(mdev, node->name); |
| 691 | of_node_put(node); |
Benson Leung | 2d805fc | 2015-05-04 10:36:36 -0700 | [diff] [blame] | 692 | return mbox ? mbox->chan : ERR_PTR(-ENOENT); |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 693 | } |
| 694 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 695 | static int omap_mbox_probe(struct platform_device *pdev) |
| 696 | { |
| 697 | struct resource *mem; |
| 698 | int ret; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 699 | struct mbox_chan *chnls; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 700 | struct omap_mbox **list, *mbox, *mboxblk; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 701 | struct omap_mbox_fifo_info *finfo, *finfoblk; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 702 | struct omap_mbox_device *mdev; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 703 | struct omap_mbox_fifo *fifo; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 704 | struct device_node *node = pdev->dev.of_node; |
| 705 | struct device_node *child; |
| 706 | const struct of_device_id *match; |
| 707 | u32 intr_type, info_count; |
| 708 | u32 num_users, num_fifos; |
| 709 | u32 tmp[3]; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 710 | u32 l; |
| 711 | int i; |
| 712 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 713 | if (!node) { |
| 714 | pr_err("%s: only DT-based devices are supported\n", __func__); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 715 | return -ENODEV; |
| 716 | } |
| 717 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 718 | match = of_match_device(omap_mailbox_of_match, &pdev->dev); |
| 719 | if (!match) |
| 720 | return -ENODEV; |
| 721 | intr_type = (u32)match->data; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 722 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 723 | if (of_property_read_u32(node, "ti,mbox-num-users", &num_users)) |
| 724 | return -ENODEV; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 725 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 726 | if (of_property_read_u32(node, "ti,mbox-num-fifos", &num_fifos)) |
| 727 | return -ENODEV; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 728 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 729 | info_count = of_get_available_child_count(node); |
| 730 | if (!info_count) { |
| 731 | dev_err(&pdev->dev, "no available mbox devices found\n"); |
| 732 | return -ENODEV; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk), |
| 736 | GFP_KERNEL); |
| 737 | if (!finfoblk) |
| 738 | return -ENOMEM; |
| 739 | |
| 740 | finfo = finfoblk; |
| 741 | child = NULL; |
| 742 | for (i = 0; i < info_count; i++, finfo++) { |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 743 | child = of_get_next_available_child(node, child); |
| 744 | ret = of_property_read_u32_array(child, "ti,mbox-tx", tmp, |
| 745 | ARRAY_SIZE(tmp)); |
| 746 | if (ret) |
| 747 | return ret; |
| 748 | finfo->tx_id = tmp[0]; |
| 749 | finfo->tx_irq = tmp[1]; |
| 750 | finfo->tx_usr = tmp[2]; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 751 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 752 | ret = of_property_read_u32_array(child, "ti,mbox-rx", tmp, |
| 753 | ARRAY_SIZE(tmp)); |
| 754 | if (ret) |
| 755 | return ret; |
| 756 | finfo->rx_id = tmp[0]; |
| 757 | finfo->rx_irq = tmp[1]; |
| 758 | finfo->rx_usr = tmp[2]; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 759 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 760 | finfo->name = child->name; |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 761 | |
Suman Anna | 4899f78a | 2016-04-06 12:37:37 -0500 | [diff] [blame] | 762 | if (of_find_property(child, "ti,mbox-send-noirq", NULL)) |
| 763 | finfo->send_no_irq = true; |
| 764 | |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 765 | if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos || |
| 766 | finfo->tx_usr >= num_users || finfo->rx_usr >= num_users) |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 770 | mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL); |
| 771 | if (!mdev) |
| 772 | return -ENOMEM; |
| 773 | |
| 774 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 775 | mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem); |
| 776 | if (IS_ERR(mdev->mbox_base)) |
| 777 | return PTR_ERR(mdev->mbox_base); |
| 778 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 779 | /* allocate one extra for marking end of list */ |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 780 | list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list), |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 781 | GFP_KERNEL); |
| 782 | if (!list) |
| 783 | return -ENOMEM; |
| 784 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 785 | chnls = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*chnls), |
| 786 | GFP_KERNEL); |
| 787 | if (!chnls) |
| 788 | return -ENOMEM; |
| 789 | |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 790 | mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox), |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 791 | GFP_KERNEL); |
| 792 | if (!mboxblk) |
| 793 | return -ENOMEM; |
| 794 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 795 | mbox = mboxblk; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 796 | finfo = finfoblk; |
| 797 | for (i = 0; i < info_count; i++, finfo++) { |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 798 | fifo = &mbox->tx_fifo; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 799 | fifo->msg = MAILBOX_MESSAGE(finfo->tx_id); |
| 800 | fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id); |
| 801 | fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id); |
| 802 | fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr); |
| 803 | fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr); |
| 804 | fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 805 | |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 806 | fifo = &mbox->rx_fifo; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 807 | fifo->msg = MAILBOX_MESSAGE(finfo->rx_id); |
| 808 | fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id); |
| 809 | fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id); |
| 810 | fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr); |
| 811 | fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr); |
| 812 | fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr); |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 813 | |
Dave Gerlach | 8e3c595 | 2015-09-22 19:14:52 -0500 | [diff] [blame] | 814 | mbox->send_no_irq = finfo->send_no_irq; |
Suman Anna | be3322e | 2014-06-24 19:43:42 -0500 | [diff] [blame] | 815 | mbox->intr_type = intr_type; |
| 816 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 817 | mbox->parent = mdev; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 818 | mbox->name = finfo->name; |
| 819 | mbox->irq = platform_get_irq(pdev, finfo->tx_irq); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 820 | if (mbox->irq < 0) |
| 821 | return mbox->irq; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 822 | mbox->chan = &chnls[i]; |
| 823 | chnls[i].con_priv = mbox; |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 824 | list[i] = mbox++; |
| 825 | } |
| 826 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 827 | mutex_init(&mdev->cfg_lock); |
| 828 | mdev->dev = &pdev->dev; |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 829 | mdev->num_users = num_users; |
| 830 | mdev->num_fifos = num_fifos; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 831 | mdev->mboxes = list; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 832 | |
| 833 | /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */ |
| 834 | mdev->controller.txdone_irq = true; |
| 835 | mdev->controller.dev = mdev->dev; |
| 836 | mdev->controller.ops = &omap_mbox_chan_ops; |
| 837 | mdev->controller.chans = chnls; |
| 838 | mdev->controller.num_chans = info_count; |
| 839 | mdev->controller.of_xlate = omap_mbox_of_xlate; |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 840 | ret = omap_mbox_register(mdev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 841 | if (ret) |
| 842 | return ret; |
| 843 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 844 | platform_set_drvdata(pdev, mdev); |
| 845 | pm_runtime_enable(mdev->dev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 846 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 847 | ret = pm_runtime_get_sync(mdev->dev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 848 | if (ret < 0) { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 849 | pm_runtime_put_noidle(mdev->dev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 850 | goto unregister; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * just print the raw revision register, the format is not |
| 855 | * uniform across all SoCs |
| 856 | */ |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 857 | l = mbox_read_reg(mdev, MAILBOX_REVISION); |
| 858 | dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 859 | |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 860 | ret = pm_runtime_put_sync(mdev->dev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 861 | if (ret < 0) |
| 862 | goto unregister; |
| 863 | |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 864 | devm_kfree(&pdev->dev, finfoblk); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 865 | return 0; |
| 866 | |
| 867 | unregister: |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 868 | pm_runtime_disable(mdev->dev); |
| 869 | omap_mbox_unregister(mdev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | static int omap_mbox_remove(struct platform_device *pdev) |
| 874 | { |
Suman Anna | 72c1c81 | 2014-06-24 19:43:43 -0500 | [diff] [blame] | 875 | struct omap_mbox_device *mdev = platform_get_drvdata(pdev); |
| 876 | |
| 877 | pm_runtime_disable(mdev->dev); |
| 878 | omap_mbox_unregister(mdev); |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | static struct platform_driver omap_mbox_driver = { |
| 884 | .probe = omap_mbox_probe, |
| 885 | .remove = omap_mbox_remove, |
| 886 | .driver = { |
| 887 | .name = "omap-mailbox", |
Suman Anna | 75288cc | 2014-09-10 14:20:59 -0500 | [diff] [blame] | 888 | .of_match_table = of_match_ptr(omap_mailbox_of_match), |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 889 | }, |
| 890 | }; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 891 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 892 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 893 | { |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 894 | int err; |
| 895 | |
| 896 | err = class_register(&omap_mbox_class); |
| 897 | if (err) |
| 898 | return err; |
| 899 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 900 | /* kfifo size sanity check: alignment and minimal size */ |
| 901 | mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); |
Kanigeri, Hari | ab66ac3 | 2010-11-29 20:24:12 +0000 | [diff] [blame] | 902 | mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, |
| 903 | sizeof(mbox_msg_t)); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 904 | |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 905 | return platform_driver_register(&omap_mbox_driver); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 906 | } |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 907 | subsys_initcall(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 908 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 909 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 910 | { |
Suman Anna | 5040f53 | 2014-06-24 19:43:41 -0500 | [diff] [blame] | 911 | platform_driver_unregister(&omap_mbox_driver); |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 912 | class_unregister(&omap_mbox_class); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 913 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 914 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 915 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 916 | MODULE_LICENSE("GPL v2"); |
| 917 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 918 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
| 919 | MODULE_AUTHOR("Hiroshi DOYU"); |