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