Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2018 Pengutronix, Oleksij Rempel <o.rempel@pengutronix.de> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/clk.h> |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 7 | #include <linux/firmware/imx/ipc.h> |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 8 | #include <linux/firmware/imx/s4.h> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/io.h> |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 11 | #include <linux/iopoll.h> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mailbox_controller.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_device.h> |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 19 | #define IMX_MU_CHANS 16 |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 20 | /* TX0/RX0/RXDB[0-3] */ |
| 21 | #define IMX_MU_SCU_CHANS 6 |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 22 | /* TX0/RX0 */ |
| 23 | #define IMX_MU_S4_CHANS 2 |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 24 | #define IMX_MU_CHAN_NAME_SIZE 20 |
| 25 | |
| 26 | enum imx_mu_chan_type { |
| 27 | IMX_MU_TYPE_TX, /* Tx */ |
| 28 | IMX_MU_TYPE_RX, /* Rx */ |
| 29 | IMX_MU_TYPE_TXDB, /* Tx doorbell */ |
| 30 | IMX_MU_TYPE_RXDB, /* Rx doorbell */ |
| 31 | }; |
| 32 | |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 33 | enum imx_mu_xcr { |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 34 | IMX_MU_GIER, |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 35 | IMX_MU_GCR, |
| 36 | IMX_MU_TCR, |
| 37 | IMX_MU_RCR, |
| 38 | IMX_MU_xCR_MAX, |
| 39 | }; |
| 40 | |
| 41 | enum imx_mu_xsr { |
| 42 | IMX_MU_SR, |
| 43 | IMX_MU_GSR, |
| 44 | IMX_MU_TSR, |
| 45 | IMX_MU_RSR, |
| 46 | }; |
| 47 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 48 | struct imx_sc_rpc_msg_max { |
| 49 | struct imx_sc_rpc_msg hdr; |
| 50 | u32 data[7]; |
| 51 | }; |
| 52 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 53 | struct imx_s4_rpc_msg_max { |
| 54 | struct imx_s4_rpc_msg hdr; |
| 55 | u32 data[254]; |
| 56 | }; |
| 57 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 58 | struct imx_mu_con_priv { |
| 59 | unsigned int idx; |
| 60 | char irq_desc[IMX_MU_CHAN_NAME_SIZE]; |
| 61 | enum imx_mu_chan_type type; |
| 62 | struct mbox_chan *chan; |
| 63 | struct tasklet_struct txdb_tasklet; |
| 64 | }; |
| 65 | |
| 66 | struct imx_mu_priv { |
| 67 | struct device *dev; |
| 68 | void __iomem *base; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 69 | void *msg; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 70 | spinlock_t xcr_lock; /* control register lock */ |
| 71 | |
| 72 | struct mbox_controller mbox; |
| 73 | struct mbox_chan mbox_chans[IMX_MU_CHANS]; |
| 74 | |
| 75 | struct imx_mu_con_priv con_priv[IMX_MU_CHANS]; |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 76 | const struct imx_mu_dcfg *dcfg; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 77 | struct clk *clk; |
| 78 | int irq; |
| 79 | |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 80 | u32 xcr[4]; |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 81 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 82 | bool side_b; |
| 83 | }; |
| 84 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 85 | enum imx_mu_type { |
| 86 | IMX_MU_V1, |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 87 | IMX_MU_V2 = BIT(1), |
| 88 | IMX_MU_V2_S4 = BIT(15), |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 89 | }; |
| 90 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 91 | struct imx_mu_dcfg { |
| 92 | int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data); |
| 93 | int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp); |
| 94 | void (*init)(struct imx_mu_priv *priv); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 95 | enum imx_mu_type type; |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 96 | u32 xTR; /* Transmit Register0 */ |
| 97 | u32 xRR; /* Receive Register0 */ |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 98 | u32 xSR[4]; /* Status Registers */ |
| 99 | u32 xCR[4]; /* Control Registers */ |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 100 | }; |
| 101 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 102 | #define IMX_MU_xSR_GIPn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x)))) |
| 103 | #define IMX_MU_xSR_RFn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x)))) |
| 104 | #define IMX_MU_xSR_TEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x)))) |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 105 | |
| 106 | /* General Purpose Interrupt Enable */ |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 107 | #define IMX_MU_xCR_GIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x)))) |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 108 | /* Receive Interrupt Enable */ |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 109 | #define IMX_MU_xCR_RIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x)))) |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 110 | /* Transmit Interrupt Enable */ |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 111 | #define IMX_MU_xCR_TIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x)))) |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 112 | /* General Purpose Interrupt Request */ |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 113 | #define IMX_MU_xCR_GIRn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(16 + (3 - (x)))) |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 114 | |
| 115 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 116 | static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox) |
| 117 | { |
| 118 | return container_of(mbox, struct imx_mu_priv, mbox); |
| 119 | } |
| 120 | |
| 121 | static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs) |
| 122 | { |
| 123 | iowrite32(val, priv->base + offs); |
| 124 | } |
| 125 | |
| 126 | static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs) |
| 127 | { |
| 128 | return ioread32(priv->base + offs); |
| 129 | } |
| 130 | |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 131 | static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, enum imx_mu_xcr type, u32 set, u32 clr) |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 132 | { |
| 133 | unsigned long flags; |
| 134 | u32 val; |
| 135 | |
| 136 | spin_lock_irqsave(&priv->xcr_lock, flags); |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 137 | val = imx_mu_read(priv, priv->dcfg->xCR[type]); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 138 | val &= ~clr; |
| 139 | val |= set; |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 140 | imx_mu_write(priv, val, priv->dcfg->xCR[type]); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 141 | spin_unlock_irqrestore(&priv->xcr_lock, flags); |
| 142 | |
| 143 | return val; |
| 144 | } |
| 145 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 146 | static int imx_mu_generic_tx(struct imx_mu_priv *priv, |
| 147 | struct imx_mu_con_priv *cp, |
| 148 | void *data) |
| 149 | { |
| 150 | u32 *arg = data; |
| 151 | |
| 152 | switch (cp->type) { |
| 153 | case IMX_MU_TYPE_TX: |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 154 | imx_mu_write(priv, *arg, priv->dcfg->xTR + cp->idx * 4); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 155 | imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0); |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 156 | break; |
| 157 | case IMX_MU_TYPE_TXDB: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 158 | imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0); |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 159 | tasklet_schedule(&cp->txdb_tasklet); |
| 160 | break; |
| 161 | default: |
| 162 | dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int imx_mu_generic_rx(struct imx_mu_priv *priv, |
| 170 | struct imx_mu_con_priv *cp) |
| 171 | { |
| 172 | u32 dat; |
| 173 | |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 174 | dat = imx_mu_read(priv, priv->dcfg->xRR + (cp->idx) * 4); |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 175 | mbox_chan_received_data(cp->chan, (void *)&dat); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 180 | static int imx_mu_specific_tx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data) |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 181 | { |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 182 | u32 *arg = data; |
| 183 | int i, ret; |
| 184 | u32 xsr; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 185 | u32 size, max_size, num_tr; |
| 186 | |
| 187 | if (priv->dcfg->type & IMX_MU_V2_S4) { |
| 188 | size = ((struct imx_s4_rpc_msg_max *)data)->hdr.size; |
| 189 | max_size = sizeof(struct imx_s4_rpc_msg_max); |
| 190 | num_tr = 8; |
| 191 | } else { |
| 192 | size = ((struct imx_sc_rpc_msg_max *)data)->hdr.size; |
| 193 | max_size = sizeof(struct imx_sc_rpc_msg_max); |
| 194 | num_tr = 4; |
| 195 | } |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 196 | |
| 197 | switch (cp->type) { |
| 198 | case IMX_MU_TYPE_TX: |
Peng Fan | 9d8ca62 | 2020-04-14 21:21:15 +0800 | [diff] [blame] | 199 | /* |
| 200 | * msg->hdr.size specifies the number of u32 words while |
| 201 | * sizeof yields bytes. |
| 202 | */ |
| 203 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 204 | if (size > max_size / 4) { |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 205 | /* |
| 206 | * The real message size can be different to |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 207 | * struct imx_sc_rpc_msg_max/imx_s4_rpc_msg_max size |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 208 | */ |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 209 | dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on TX; got: %i bytes\n", max_size, size << 2); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 210 | return -EINVAL; |
| 211 | } |
| 212 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 213 | for (i = 0; i < num_tr && i < size; i++) |
| 214 | imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4); |
| 215 | for (; i < size; i++) { |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 216 | ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_TSR], |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 217 | xsr, |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 218 | xsr & IMX_MU_xSR_TEn(priv->dcfg->type, i % num_tr), |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 219 | 0, 100); |
| 220 | if (ret) { |
| 221 | dev_err(priv->dev, "Send data index: %d timeout\n", i); |
| 222 | return ret; |
| 223 | } |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 224 | imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 227 | imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 228 | break; |
| 229 | default: |
| 230 | dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type); |
| 231 | return -EINVAL; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 237 | static int imx_mu_specific_rx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp) |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 238 | { |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 239 | u32 *data; |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 240 | int i, ret; |
| 241 | u32 xsr; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 242 | u32 size, max_size; |
| 243 | |
| 244 | data = (u32 *)priv->msg; |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 245 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 246 | imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, 0)); |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 247 | *data++ = imx_mu_read(priv, priv->dcfg->xRR); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 248 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 249 | if (priv->dcfg->type & IMX_MU_V2_S4) { |
| 250 | size = ((struct imx_s4_rpc_msg_max *)priv->msg)->hdr.size; |
| 251 | max_size = sizeof(struct imx_s4_rpc_msg_max); |
| 252 | } else { |
| 253 | size = ((struct imx_sc_rpc_msg_max *)priv->msg)->hdr.size; |
| 254 | max_size = sizeof(struct imx_sc_rpc_msg_max); |
| 255 | } |
| 256 | |
| 257 | if (size > max_size / 4) { |
| 258 | dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on RX; got: %i bytes\n", max_size, size << 2); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 259 | return -EINVAL; |
| 260 | } |
| 261 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 262 | for (i = 1; i < size; i++) { |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 263 | ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_RSR], xsr, |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 264 | xsr & IMX_MU_xSR_RFn(priv->dcfg->type, i % 4), 0, 100); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 265 | if (ret) { |
| 266 | dev_err(priv->dev, "timeout read idx %d\n", i); |
| 267 | return ret; |
| 268 | } |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 269 | *data++ = imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 272 | imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, 0), 0); |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 273 | mbox_chan_received_data(cp->chan, (void *)priv->msg); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 278 | static void imx_mu_txdb_tasklet(unsigned long data) |
| 279 | { |
| 280 | struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data; |
| 281 | |
| 282 | mbox_chan_txdone(cp->chan, 0); |
| 283 | } |
| 284 | |
| 285 | static irqreturn_t imx_mu_isr(int irq, void *p) |
| 286 | { |
| 287 | struct mbox_chan *chan = p; |
| 288 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 289 | struct imx_mu_con_priv *cp = chan->con_priv; |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 290 | u32 val, ctrl; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 291 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 292 | switch (cp->type) { |
| 293 | case IMX_MU_TYPE_TX: |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 294 | ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_TCR]); |
| 295 | val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 296 | val &= IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx) & |
| 297 | (ctrl & IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx)); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 298 | break; |
| 299 | case IMX_MU_TYPE_RX: |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 300 | ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_RCR]); |
| 301 | val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 302 | val &= IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx) & |
| 303 | (ctrl & IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx)); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 304 | break; |
| 305 | case IMX_MU_TYPE_RXDB: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 306 | ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_GIER]); |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 307 | val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 308 | val &= IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx) & |
| 309 | (ctrl & IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx)); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 310 | break; |
| 311 | default: |
Nathan Chancellor | e80a7e7 | 2021-06-21 11:56:45 -0700 | [diff] [blame] | 312 | dev_warn_ratelimited(priv->dev, "Unhandled channel type %d\n", |
| 313 | cp->type); |
| 314 | return IRQ_NONE; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (!val) |
| 318 | return IRQ_NONE; |
| 319 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 320 | if ((val == IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx)) && |
| 321 | (cp->type == IMX_MU_TYPE_TX)) { |
| 322 | imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx)); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 323 | mbox_chan_txdone(chan, 0); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 324 | } else if ((val == IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx)) && |
| 325 | (cp->type == IMX_MU_TYPE_RX)) { |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 326 | priv->dcfg->rx(priv, cp); |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 327 | } else if ((val == IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx)) && |
| 328 | (cp->type == IMX_MU_TYPE_RXDB)) { |
| 329 | imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx), |
| 330 | priv->dcfg->xSR[IMX_MU_GSR]); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 331 | mbox_chan_received_data(chan, NULL); |
| 332 | } else { |
| 333 | dev_warn_ratelimited(priv->dev, "Not handled interrupt\n"); |
| 334 | return IRQ_NONE; |
| 335 | } |
| 336 | |
| 337 | return IRQ_HANDLED; |
| 338 | } |
| 339 | |
| 340 | static int imx_mu_send_data(struct mbox_chan *chan, void *data) |
| 341 | { |
| 342 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 343 | struct imx_mu_con_priv *cp = chan->con_priv; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 344 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 345 | return priv->dcfg->tx(priv, cp, data); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int imx_mu_startup(struct mbox_chan *chan) |
| 349 | { |
| 350 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 351 | struct imx_mu_con_priv *cp = chan->con_priv; |
Anson Huang | b7b2796 | 2020-06-03 13:15:44 +0800 | [diff] [blame] | 352 | unsigned long irq_flag = IRQF_SHARED; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 353 | int ret; |
| 354 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 355 | pm_runtime_get_sync(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 356 | if (cp->type == IMX_MU_TYPE_TXDB) { |
| 357 | /* Tx doorbell don't have ACK support */ |
| 358 | tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet, |
| 359 | (unsigned long)cp); |
| 360 | return 0; |
| 361 | } |
| 362 | |
Anson Huang | b7b2796 | 2020-06-03 13:15:44 +0800 | [diff] [blame] | 363 | /* IPC MU should be with IRQF_NO_SUSPEND set */ |
| 364 | if (!priv->dev->pm_domain) |
| 365 | irq_flag |= IRQF_NO_SUSPEND; |
| 366 | |
| 367 | ret = request_irq(priv->irq, imx_mu_isr, irq_flag, |
| 368 | cp->irq_desc, chan); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 369 | if (ret) { |
| 370 | dev_err(priv->dev, |
| 371 | "Unable to acquire IRQ %d\n", priv->irq); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | switch (cp->type) { |
| 376 | case IMX_MU_TYPE_RX: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 377 | imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx), 0); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 378 | break; |
| 379 | case IMX_MU_TYPE_RXDB: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 380 | imx_mu_xcr_rmw(priv, IMX_MU_GIER, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx), 0); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 381 | break; |
| 382 | default: |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void imx_mu_shutdown(struct mbox_chan *chan) |
| 390 | { |
| 391 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 392 | struct imx_mu_con_priv *cp = chan->con_priv; |
| 393 | |
Daniel Baluta | bf159d1 | 2019-10-09 16:07:18 +0800 | [diff] [blame] | 394 | if (cp->type == IMX_MU_TYPE_TXDB) { |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 395 | tasklet_kill(&cp->txdb_tasklet); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 396 | pm_runtime_put_sync(priv->dev); |
Daniel Baluta | bf159d1 | 2019-10-09 16:07:18 +0800 | [diff] [blame] | 397 | return; |
| 398 | } |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 399 | |
Daniel Baluta | 5f0af07 | 2019-10-09 16:07:19 +0800 | [diff] [blame] | 400 | switch (cp->type) { |
| 401 | case IMX_MU_TYPE_TX: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 402 | imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx)); |
Daniel Baluta | 5f0af07 | 2019-10-09 16:07:19 +0800 | [diff] [blame] | 403 | break; |
| 404 | case IMX_MU_TYPE_RX: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 405 | imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx)); |
Daniel Baluta | 5f0af07 | 2019-10-09 16:07:19 +0800 | [diff] [blame] | 406 | break; |
| 407 | case IMX_MU_TYPE_RXDB: |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 408 | imx_mu_xcr_rmw(priv, IMX_MU_GIER, 0, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx)); |
Daniel Baluta | 5f0af07 | 2019-10-09 16:07:19 +0800 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | break; |
| 412 | } |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 413 | |
| 414 | free_irq(priv->irq, chan); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 415 | pm_runtime_put_sync(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static const struct mbox_chan_ops imx_mu_ops = { |
| 419 | .send_data = imx_mu_send_data, |
| 420 | .startup = imx_mu_startup, |
| 421 | .shutdown = imx_mu_shutdown, |
| 422 | }; |
| 423 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 424 | static struct mbox_chan *imx_mu_specific_xlate(struct mbox_controller *mbox, |
| 425 | const struct of_phandle_args *sp) |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 426 | { |
| 427 | u32 type, idx, chan; |
| 428 | |
| 429 | if (sp->args_count != 2) { |
| 430 | dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count); |
| 431 | return ERR_PTR(-EINVAL); |
| 432 | } |
| 433 | |
| 434 | type = sp->args[0]; /* channel type */ |
| 435 | idx = sp->args[1]; /* index */ |
| 436 | |
| 437 | switch (type) { |
| 438 | case IMX_MU_TYPE_TX: |
| 439 | case IMX_MU_TYPE_RX: |
| 440 | if (idx != 0) |
| 441 | dev_err(mbox->dev, "Invalid chan idx: %d\n", idx); |
| 442 | chan = type; |
| 443 | break; |
| 444 | case IMX_MU_TYPE_RXDB: |
| 445 | chan = 2 + idx; |
| 446 | break; |
| 447 | default: |
| 448 | dev_err(mbox->dev, "Invalid chan type: %d\n", type); |
Dan Carpenter | 1b3a347 | 2020-04-07 12:27:53 +0300 | [diff] [blame] | 449 | return ERR_PTR(-EINVAL); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (chan >= mbox->num_chans) { |
| 453 | dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx); |
| 454 | return ERR_PTR(-EINVAL); |
| 455 | } |
| 456 | |
| 457 | return &mbox->chans[chan]; |
| 458 | } |
| 459 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 460 | static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox, |
| 461 | const struct of_phandle_args *sp) |
| 462 | { |
| 463 | u32 type, idx, chan; |
| 464 | |
| 465 | if (sp->args_count != 2) { |
| 466 | dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count); |
| 467 | return ERR_PTR(-EINVAL); |
| 468 | } |
| 469 | |
| 470 | type = sp->args[0]; /* channel type */ |
| 471 | idx = sp->args[1]; /* index */ |
| 472 | chan = type * 4 + idx; |
| 473 | |
| 474 | if (chan >= mbox->num_chans) { |
| 475 | dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx); |
| 476 | return ERR_PTR(-EINVAL); |
| 477 | } |
| 478 | |
| 479 | return &mbox->chans[chan]; |
| 480 | } |
| 481 | |
| 482 | static void imx_mu_init_generic(struct imx_mu_priv *priv) |
| 483 | { |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 484 | unsigned int i; |
| 485 | |
| 486 | for (i = 0; i < IMX_MU_CHANS; i++) { |
| 487 | struct imx_mu_con_priv *cp = &priv->con_priv[i]; |
| 488 | |
| 489 | cp->idx = i % 4; |
| 490 | cp->type = i >> 2; |
| 491 | cp->chan = &priv->mbox_chans[i]; |
| 492 | priv->mbox_chans[i].con_priv = cp; |
| 493 | snprintf(cp->irq_desc, sizeof(cp->irq_desc), |
| 494 | "imx_mu_chan[%i-%i]", cp->type, cp->idx); |
| 495 | } |
| 496 | |
| 497 | priv->mbox.num_chans = IMX_MU_CHANS; |
| 498 | priv->mbox.of_xlate = imx_mu_xlate; |
| 499 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 500 | if (priv->side_b) |
| 501 | return; |
| 502 | |
| 503 | /* Set default MU configuration */ |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 504 | for (i = 0; i < IMX_MU_xCR_MAX; i++) |
| 505 | imx_mu_write(priv, 0, priv->dcfg->xCR[i]); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 508 | static void imx_mu_init_specific(struct imx_mu_priv *priv) |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 509 | { |
| 510 | unsigned int i; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 511 | int num_chans = priv->dcfg->type & IMX_MU_V2_S4 ? IMX_MU_S4_CHANS : IMX_MU_SCU_CHANS; |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 512 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 513 | for (i = 0; i < num_chans; i++) { |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 514 | struct imx_mu_con_priv *cp = &priv->con_priv[i]; |
| 515 | |
| 516 | cp->idx = i < 2 ? 0 : i - 2; |
| 517 | cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB; |
| 518 | cp->chan = &priv->mbox_chans[i]; |
| 519 | priv->mbox_chans[i].con_priv = cp; |
| 520 | snprintf(cp->irq_desc, sizeof(cp->irq_desc), |
| 521 | "imx_mu_chan[%i-%i]", cp->type, cp->idx); |
| 522 | } |
| 523 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 524 | priv->mbox.num_chans = num_chans; |
| 525 | priv->mbox.of_xlate = imx_mu_specific_xlate; |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 526 | |
| 527 | /* Set default MU configuration */ |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 528 | for (i = 0; i < IMX_MU_xCR_MAX; i++) |
| 529 | imx_mu_write(priv, 0, priv->dcfg->xCR[i]); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 530 | } |
| 531 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 532 | static int imx_mu_probe(struct platform_device *pdev) |
| 533 | { |
| 534 | struct device *dev = &pdev->dev; |
| 535 | struct device_node *np = dev->of_node; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 536 | struct imx_mu_priv *priv; |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 537 | const struct imx_mu_dcfg *dcfg; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 538 | int ret; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 539 | u32 size; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 540 | |
| 541 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 542 | if (!priv) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | priv->dev = dev; |
| 546 | |
Anson Huang | 0c40e63 | 2019-04-01 05:15:24 +0000 | [diff] [blame] | 547 | priv->base = devm_platform_ioremap_resource(pdev, 0); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 548 | if (IS_ERR(priv->base)) |
| 549 | return PTR_ERR(priv->base); |
| 550 | |
| 551 | priv->irq = platform_get_irq(pdev, 0); |
| 552 | if (priv->irq < 0) |
| 553 | return priv->irq; |
| 554 | |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 555 | dcfg = of_device_get_match_data(dev); |
| 556 | if (!dcfg) |
| 557 | return -EINVAL; |
| 558 | priv->dcfg = dcfg; |
| 559 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 560 | if (priv->dcfg->type & IMX_MU_V2_S4) |
| 561 | size = sizeof(struct imx_s4_rpc_msg_max); |
| 562 | else |
| 563 | size = sizeof(struct imx_sc_rpc_msg_max); |
| 564 | |
| 565 | priv->msg = devm_kzalloc(dev, size, GFP_KERNEL); |
Dan Carpenter | 05d06f3 | 2021-11-24 17:51:26 +0300 | [diff] [blame] | 566 | if (!priv->msg) |
| 567 | return -ENOMEM; |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 568 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 569 | priv->clk = devm_clk_get(dev, NULL); |
| 570 | if (IS_ERR(priv->clk)) { |
| 571 | if (PTR_ERR(priv->clk) != -ENOENT) |
| 572 | return PTR_ERR(priv->clk); |
| 573 | |
| 574 | priv->clk = NULL; |
| 575 | } |
| 576 | |
| 577 | ret = clk_prepare_enable(priv->clk); |
| 578 | if (ret) { |
| 579 | dev_err(dev, "Failed to enable clock\n"); |
| 580 | return ret; |
| 581 | } |
| 582 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 583 | priv->side_b = of_property_read_bool(np, "fsl,mu-side-b"); |
| 584 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 585 | priv->dcfg->init(priv); |
| 586 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 587 | spin_lock_init(&priv->xcr_lock); |
| 588 | |
| 589 | priv->mbox.dev = dev; |
| 590 | priv->mbox.ops = &imx_mu_ops; |
| 591 | priv->mbox.chans = priv->mbox_chans; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 592 | priv->mbox.txdone_irq = true; |
| 593 | |
| 594 | platform_set_drvdata(pdev, priv); |
| 595 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 596 | ret = devm_mbox_controller_register(dev, &priv->mbox); |
Fabio Estevam | 47303f9 | 2020-05-30 17:56:28 -0500 | [diff] [blame] | 597 | if (ret) { |
| 598 | clk_disable_unprepare(priv->clk); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 599 | return ret; |
Fabio Estevam | 47303f9 | 2020-05-30 17:56:28 -0500 | [diff] [blame] | 600 | } |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 601 | |
| 602 | pm_runtime_enable(dev); |
| 603 | |
| 604 | ret = pm_runtime_get_sync(dev); |
| 605 | if (ret < 0) { |
| 606 | pm_runtime_put_noidle(dev); |
| 607 | goto disable_runtime_pm; |
| 608 | } |
| 609 | |
| 610 | ret = pm_runtime_put_sync(dev); |
| 611 | if (ret < 0) |
| 612 | goto disable_runtime_pm; |
| 613 | |
Anson Huang | bb2b2624 | 2020-06-03 13:15:43 +0800 | [diff] [blame] | 614 | clk_disable_unprepare(priv->clk); |
| 615 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 616 | return 0; |
| 617 | |
| 618 | disable_runtime_pm: |
| 619 | pm_runtime_disable(dev); |
Anson Huang | bb2b2624 | 2020-06-03 13:15:43 +0800 | [diff] [blame] | 620 | clk_disable_unprepare(priv->clk); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 621 | return ret; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static int imx_mu_remove(struct platform_device *pdev) |
| 625 | { |
| 626 | struct imx_mu_priv *priv = platform_get_drvdata(pdev); |
| 627 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 628 | pm_runtime_disable(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 633 | static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = { |
| 634 | .tx = imx_mu_generic_tx, |
| 635 | .rx = imx_mu_generic_rx, |
| 636 | .init = imx_mu_init_generic, |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 637 | .xTR = 0x0, |
| 638 | .xRR = 0x10, |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 639 | .xSR = {0x20, 0x20, 0x20, 0x20}, |
| 640 | .xCR = {0x24, 0x24, 0x24, 0x24}, |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = { |
| 644 | .tx = imx_mu_generic_tx, |
| 645 | .rx = imx_mu_generic_rx, |
| 646 | .init = imx_mu_init_generic, |
Peng Fan | 32f7443 | 2021-05-28 18:06:01 +0800 | [diff] [blame] | 647 | .xTR = 0x20, |
| 648 | .xRR = 0x40, |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 649 | .xSR = {0x60, 0x60, 0x60, 0x60}, |
| 650 | .xCR = {0x64, 0x64, 0x64, 0x64}, |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 651 | }; |
| 652 | |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 653 | static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp = { |
| 654 | .tx = imx_mu_generic_tx, |
| 655 | .rx = imx_mu_generic_rx, |
| 656 | .init = imx_mu_init_generic, |
| 657 | .type = IMX_MU_V2, |
| 658 | .xTR = 0x200, |
| 659 | .xRR = 0x280, |
| 660 | .xSR = {0xC, 0x118, 0x124, 0x12C}, |
| 661 | .xCR = {0x110, 0x114, 0x120, 0x128}, |
| 662 | }; |
| 663 | |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 664 | static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp_s4 = { |
| 665 | .tx = imx_mu_specific_tx, |
| 666 | .rx = imx_mu_specific_rx, |
| 667 | .init = imx_mu_init_specific, |
| 668 | .type = IMX_MU_V2 | IMX_MU_V2_S4, |
| 669 | .xTR = 0x200, |
| 670 | .xRR = 0x280, |
| 671 | .xSR = {0xC, 0x118, 0x124, 0x12C}, |
| 672 | .xCR = {0x110, 0x114, 0x120, 0x128}, |
| 673 | }; |
| 674 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 675 | static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = { |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 676 | .tx = imx_mu_specific_tx, |
| 677 | .rx = imx_mu_specific_rx, |
| 678 | .init = imx_mu_init_specific, |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 679 | .xTR = 0x0, |
| 680 | .xRR = 0x10, |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 681 | .xSR = {0x20, 0x20, 0x20, 0x20}, |
| 682 | .xCR = {0x24, 0x24, 0x24, 0x24}, |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 683 | }; |
| 684 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 685 | static const struct of_device_id imx_mu_dt_ids[] = { |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 686 | { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp }, |
| 687 | { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx }, |
Peng Fan | 4f0b776 | 2021-05-28 18:06:03 +0800 | [diff] [blame] | 688 | { .compatible = "fsl,imx8ulp-mu", .data = &imx_mu_cfg_imx8ulp }, |
Peng Fan | 97961f78e | 2021-10-22 18:18:56 +0800 | [diff] [blame] | 689 | { .compatible = "fsl,imx8ulp-mu-s4", .data = &imx_mu_cfg_imx8ulp_s4 }, |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 690 | { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu }, |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 691 | { }, |
| 692 | }; |
| 693 | MODULE_DEVICE_TABLE(of, imx_mu_dt_ids); |
| 694 | |
Nathan Chancellor | 03b7013 | 2020-06-22 18:04:03 -0700 | [diff] [blame] | 695 | static int __maybe_unused imx_mu_suspend_noirq(struct device *dev) |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 696 | { |
| 697 | struct imx_mu_priv *priv = dev_get_drvdata(dev); |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 698 | int i; |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 699 | |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 700 | if (!priv->clk) { |
| 701 | for (i = 0; i < IMX_MU_xCR_MAX; i++) |
| 702 | priv->xcr[i] = imx_mu_read(priv, priv->dcfg->xCR[i]); |
| 703 | } |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
Nathan Chancellor | 03b7013 | 2020-06-22 18:04:03 -0700 | [diff] [blame] | 708 | static int __maybe_unused imx_mu_resume_noirq(struct device *dev) |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 709 | { |
| 710 | struct imx_mu_priv *priv = dev_get_drvdata(dev); |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 711 | int i; |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * ONLY restore MU when context lost, the TIE could |
| 715 | * be set during noirq resume as there is MU data |
| 716 | * communication going on, and restore the saved |
| 717 | * value will overwrite the TIE and cause MU data |
| 718 | * send failed, may lead to system freeze. This issue |
| 719 | * is observed by testing freeze mode suspend. |
| 720 | */ |
Peng Fan | f689a7c | 2021-05-28 18:06:02 +0800 | [diff] [blame] | 721 | if (!imx_mu_read(priv, priv->dcfg->xCR[0]) && !priv->clk) { |
| 722 | for (i = 0; i < IMX_MU_xCR_MAX; i++) |
| 723 | imx_mu_write(priv, priv->xcr[i], priv->dcfg->xCR[i]); |
| 724 | } |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
Nathan Chancellor | 03b7013 | 2020-06-22 18:04:03 -0700 | [diff] [blame] | 729 | static int __maybe_unused imx_mu_runtime_suspend(struct device *dev) |
Anson Huang | bb2b2624 | 2020-06-03 13:15:43 +0800 | [diff] [blame] | 730 | { |
| 731 | struct imx_mu_priv *priv = dev_get_drvdata(dev); |
| 732 | |
| 733 | clk_disable_unprepare(priv->clk); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Nathan Chancellor | 03b7013 | 2020-06-22 18:04:03 -0700 | [diff] [blame] | 738 | static int __maybe_unused imx_mu_runtime_resume(struct device *dev) |
Anson Huang | bb2b2624 | 2020-06-03 13:15:43 +0800 | [diff] [blame] | 739 | { |
| 740 | struct imx_mu_priv *priv = dev_get_drvdata(dev); |
| 741 | int ret; |
| 742 | |
| 743 | ret = clk_prepare_enable(priv->clk); |
| 744 | if (ret) |
| 745 | dev_err(dev, "failed to enable clock\n"); |
| 746 | |
| 747 | return ret; |
| 748 | } |
| 749 | |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 750 | static const struct dev_pm_ops imx_mu_pm_ops = { |
| 751 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq, |
| 752 | imx_mu_resume_noirq) |
Anson Huang | bb2b2624 | 2020-06-03 13:15:43 +0800 | [diff] [blame] | 753 | SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend, |
| 754 | imx_mu_runtime_resume, NULL) |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 755 | }; |
| 756 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 757 | static struct platform_driver imx_mu_driver = { |
| 758 | .probe = imx_mu_probe, |
| 759 | .remove = imx_mu_remove, |
| 760 | .driver = { |
| 761 | .name = "imx_mu", |
| 762 | .of_match_table = imx_mu_dt_ids, |
Dong Aisheng | ba5f9fa | 2020-06-03 13:15:42 +0800 | [diff] [blame] | 763 | .pm = &imx_mu_pm_ops, |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 764 | }, |
| 765 | }; |
| 766 | module_platform_driver(imx_mu_driver); |
| 767 | |
| 768 | MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); |
| 769 | MODULE_DESCRIPTION("Message Unit driver for i.MX"); |
| 770 | MODULE_LICENSE("GPL v2"); |