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> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/io.h> |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 10 | #include <linux/iopoll.h> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/mailbox_controller.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of_device.h> |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 15 | #include <linux/pm_runtime.h> |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 18 | #define IMX_MU_xSR_GIPn(x) BIT(28 + (3 - (x))) |
| 19 | #define IMX_MU_xSR_RFn(x) BIT(24 + (3 - (x))) |
| 20 | #define IMX_MU_xSR_TEn(x) BIT(20 + (3 - (x))) |
| 21 | #define IMX_MU_xSR_BRDIP BIT(9) |
| 22 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 23 | /* General Purpose Interrupt Enable */ |
| 24 | #define IMX_MU_xCR_GIEn(x) BIT(28 + (3 - (x))) |
| 25 | /* Receive Interrupt Enable */ |
| 26 | #define IMX_MU_xCR_RIEn(x) BIT(24 + (3 - (x))) |
| 27 | /* Transmit Interrupt Enable */ |
| 28 | #define IMX_MU_xCR_TIEn(x) BIT(20 + (3 - (x))) |
| 29 | /* General Purpose Interrupt Request */ |
| 30 | #define IMX_MU_xCR_GIRn(x) BIT(16 + (3 - (x))) |
| 31 | |
| 32 | #define IMX_MU_CHANS 16 |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 33 | /* TX0/RX0/RXDB[0-3] */ |
| 34 | #define IMX_MU_SCU_CHANS 6 |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 35 | #define IMX_MU_CHAN_NAME_SIZE 20 |
| 36 | |
| 37 | enum imx_mu_chan_type { |
| 38 | IMX_MU_TYPE_TX, /* Tx */ |
| 39 | IMX_MU_TYPE_RX, /* Rx */ |
| 40 | IMX_MU_TYPE_TXDB, /* Tx doorbell */ |
| 41 | IMX_MU_TYPE_RXDB, /* Rx doorbell */ |
| 42 | }; |
| 43 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 44 | struct imx_sc_rpc_msg_max { |
| 45 | struct imx_sc_rpc_msg hdr; |
| 46 | u32 data[7]; |
| 47 | }; |
| 48 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 49 | struct imx_mu_con_priv { |
| 50 | unsigned int idx; |
| 51 | char irq_desc[IMX_MU_CHAN_NAME_SIZE]; |
| 52 | enum imx_mu_chan_type type; |
| 53 | struct mbox_chan *chan; |
| 54 | struct tasklet_struct txdb_tasklet; |
| 55 | }; |
| 56 | |
| 57 | struct imx_mu_priv { |
| 58 | struct device *dev; |
| 59 | void __iomem *base; |
| 60 | spinlock_t xcr_lock; /* control register lock */ |
| 61 | |
| 62 | struct mbox_controller mbox; |
| 63 | struct mbox_chan mbox_chans[IMX_MU_CHANS]; |
| 64 | |
| 65 | struct imx_mu_con_priv con_priv[IMX_MU_CHANS]; |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 66 | const struct imx_mu_dcfg *dcfg; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 67 | struct clk *clk; |
| 68 | int irq; |
| 69 | |
| 70 | bool side_b; |
| 71 | }; |
| 72 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 73 | struct imx_mu_dcfg { |
| 74 | int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data); |
| 75 | int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp); |
| 76 | void (*init)(struct imx_mu_priv *priv); |
| 77 | u32 xTR[4]; /* Transmit Registers */ |
| 78 | u32 xRR[4]; /* Receive Registers */ |
| 79 | u32 xSR; /* Status Register */ |
| 80 | u32 xCR; /* Control Register */ |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 83 | static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox) |
| 84 | { |
| 85 | return container_of(mbox, struct imx_mu_priv, mbox); |
| 86 | } |
| 87 | |
| 88 | static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs) |
| 89 | { |
| 90 | iowrite32(val, priv->base + offs); |
| 91 | } |
| 92 | |
| 93 | static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs) |
| 94 | { |
| 95 | return ioread32(priv->base + offs); |
| 96 | } |
| 97 | |
| 98 | static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, u32 set, u32 clr) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | u32 val; |
| 102 | |
| 103 | spin_lock_irqsave(&priv->xcr_lock, flags); |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 104 | val = imx_mu_read(priv, priv->dcfg->xCR); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 105 | val &= ~clr; |
| 106 | val |= set; |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 107 | imx_mu_write(priv, val, priv->dcfg->xCR); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 108 | spin_unlock_irqrestore(&priv->xcr_lock, flags); |
| 109 | |
| 110 | return val; |
| 111 | } |
| 112 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 113 | static int imx_mu_generic_tx(struct imx_mu_priv *priv, |
| 114 | struct imx_mu_con_priv *cp, |
| 115 | void *data) |
| 116 | { |
| 117 | u32 *arg = data; |
| 118 | |
| 119 | switch (cp->type) { |
| 120 | case IMX_MU_TYPE_TX: |
| 121 | imx_mu_write(priv, *arg, priv->dcfg->xTR[cp->idx]); |
| 122 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_TIEn(cp->idx), 0); |
| 123 | break; |
| 124 | case IMX_MU_TYPE_TXDB: |
| 125 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_GIRn(cp->idx), 0); |
| 126 | tasklet_schedule(&cp->txdb_tasklet); |
| 127 | break; |
| 128 | default: |
| 129 | dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int imx_mu_generic_rx(struct imx_mu_priv *priv, |
| 137 | struct imx_mu_con_priv *cp) |
| 138 | { |
| 139 | u32 dat; |
| 140 | |
| 141 | dat = imx_mu_read(priv, priv->dcfg->xRR[cp->idx]); |
| 142 | mbox_chan_received_data(cp->chan, (void *)&dat); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 147 | static int imx_mu_scu_tx(struct imx_mu_priv *priv, |
| 148 | struct imx_mu_con_priv *cp, |
| 149 | void *data) |
| 150 | { |
| 151 | struct imx_sc_rpc_msg_max *msg = data; |
| 152 | u32 *arg = data; |
| 153 | int i, ret; |
| 154 | u32 xsr; |
| 155 | |
| 156 | switch (cp->type) { |
| 157 | case IMX_MU_TYPE_TX: |
| 158 | if (msg->hdr.size > sizeof(*msg)) { |
| 159 | /* |
| 160 | * The real message size can be different to |
| 161 | * struct imx_sc_rpc_msg_max size |
| 162 | */ |
| 163 | dev_err(priv->dev, "Exceed max msg size (%zu) on TX, got: %i\n", sizeof(*msg), msg->hdr.size); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
| 167 | for (i = 0; i < 4 && i < msg->hdr.size; i++) |
| 168 | imx_mu_write(priv, *arg++, priv->dcfg->xTR[i % 4]); |
| 169 | for (; i < msg->hdr.size; i++) { |
| 170 | ret = readl_poll_timeout(priv->base + priv->dcfg->xSR, |
| 171 | xsr, |
| 172 | xsr & IMX_MU_xSR_TEn(i % 4), |
| 173 | 0, 100); |
| 174 | if (ret) { |
| 175 | dev_err(priv->dev, "Send data index: %d timeout\n", i); |
| 176 | return ret; |
| 177 | } |
| 178 | imx_mu_write(priv, *arg++, priv->dcfg->xTR[i % 4]); |
| 179 | } |
| 180 | |
| 181 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_TIEn(cp->idx), 0); |
| 182 | break; |
| 183 | default: |
| 184 | dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type); |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int imx_mu_scu_rx(struct imx_mu_priv *priv, |
| 192 | struct imx_mu_con_priv *cp) |
| 193 | { |
| 194 | struct imx_sc_rpc_msg_max msg; |
| 195 | u32 *data = (u32 *)&msg; |
| 196 | int i, ret; |
| 197 | u32 xsr; |
| 198 | |
| 199 | imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_RIEn(0)); |
| 200 | *data++ = imx_mu_read(priv, priv->dcfg->xRR[0]); |
| 201 | |
| 202 | if (msg.hdr.size > sizeof(msg)) { |
| 203 | dev_err(priv->dev, "Exceed max msg size (%zu) on RX, got: %i\n", |
| 204 | sizeof(msg), msg.hdr.size); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | for (i = 1; i < msg.hdr.size; i++) { |
| 209 | ret = readl_poll_timeout(priv->base + priv->dcfg->xSR, xsr, |
| 210 | xsr & IMX_MU_xSR_RFn(i % 4), 0, 100); |
| 211 | if (ret) { |
| 212 | dev_err(priv->dev, "timeout read idx %d\n", i); |
| 213 | return ret; |
| 214 | } |
| 215 | *data++ = imx_mu_read(priv, priv->dcfg->xRR[i % 4]); |
| 216 | } |
| 217 | |
| 218 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_RIEn(0), 0); |
| 219 | mbox_chan_received_data(cp->chan, (void *)&msg); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 224 | static void imx_mu_txdb_tasklet(unsigned long data) |
| 225 | { |
| 226 | struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data; |
| 227 | |
| 228 | mbox_chan_txdone(cp->chan, 0); |
| 229 | } |
| 230 | |
| 231 | static irqreturn_t imx_mu_isr(int irq, void *p) |
| 232 | { |
| 233 | struct mbox_chan *chan = p; |
| 234 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 235 | struct imx_mu_con_priv *cp = chan->con_priv; |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 236 | u32 val, ctrl; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 237 | |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 238 | ctrl = imx_mu_read(priv, priv->dcfg->xCR); |
| 239 | val = imx_mu_read(priv, priv->dcfg->xSR); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 240 | |
| 241 | switch (cp->type) { |
| 242 | case IMX_MU_TYPE_TX: |
| 243 | val &= IMX_MU_xSR_TEn(cp->idx) & |
| 244 | (ctrl & IMX_MU_xCR_TIEn(cp->idx)); |
| 245 | break; |
| 246 | case IMX_MU_TYPE_RX: |
| 247 | val &= IMX_MU_xSR_RFn(cp->idx) & |
| 248 | (ctrl & IMX_MU_xCR_RIEn(cp->idx)); |
| 249 | break; |
| 250 | case IMX_MU_TYPE_RXDB: |
| 251 | val &= IMX_MU_xSR_GIPn(cp->idx) & |
| 252 | (ctrl & IMX_MU_xCR_GIEn(cp->idx)); |
| 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | if (!val) |
| 259 | return IRQ_NONE; |
| 260 | |
| 261 | if (val == IMX_MU_xSR_TEn(cp->idx)) { |
| 262 | imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_TIEn(cp->idx)); |
| 263 | mbox_chan_txdone(chan, 0); |
| 264 | } else if (val == IMX_MU_xSR_RFn(cp->idx)) { |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 265 | priv->dcfg->rx(priv, cp); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 266 | } else if (val == IMX_MU_xSR_GIPn(cp->idx)) { |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 267 | imx_mu_write(priv, IMX_MU_xSR_GIPn(cp->idx), priv->dcfg->xSR); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 268 | mbox_chan_received_data(chan, NULL); |
| 269 | } else { |
| 270 | dev_warn_ratelimited(priv->dev, "Not handled interrupt\n"); |
| 271 | return IRQ_NONE; |
| 272 | } |
| 273 | |
| 274 | return IRQ_HANDLED; |
| 275 | } |
| 276 | |
| 277 | static int imx_mu_send_data(struct mbox_chan *chan, void *data) |
| 278 | { |
| 279 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 280 | struct imx_mu_con_priv *cp = chan->con_priv; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 281 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 282 | return priv->dcfg->tx(priv, cp, data); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int imx_mu_startup(struct mbox_chan *chan) |
| 286 | { |
| 287 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 288 | struct imx_mu_con_priv *cp = chan->con_priv; |
| 289 | int ret; |
| 290 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 291 | pm_runtime_get_sync(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 292 | if (cp->type == IMX_MU_TYPE_TXDB) { |
| 293 | /* Tx doorbell don't have ACK support */ |
| 294 | tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet, |
| 295 | (unsigned long)cp); |
| 296 | return 0; |
| 297 | } |
| 298 | |
Anson Huang | 17b860b | 2019-02-12 12:40:25 +0000 | [diff] [blame] | 299 | ret = request_irq(priv->irq, imx_mu_isr, IRQF_SHARED | |
| 300 | IRQF_NO_SUSPEND, cp->irq_desc, chan); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 301 | if (ret) { |
| 302 | dev_err(priv->dev, |
| 303 | "Unable to acquire IRQ %d\n", priv->irq); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | switch (cp->type) { |
| 308 | case IMX_MU_TYPE_RX: |
| 309 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_RIEn(cp->idx), 0); |
| 310 | break; |
| 311 | case IMX_MU_TYPE_RXDB: |
| 312 | imx_mu_xcr_rmw(priv, IMX_MU_xCR_GIEn(cp->idx), 0); |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static void imx_mu_shutdown(struct mbox_chan *chan) |
| 322 | { |
| 323 | struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox); |
| 324 | struct imx_mu_con_priv *cp = chan->con_priv; |
| 325 | |
Daniel Baluta | bf159d1 | 2019-10-09 16:07:18 +0800 | [diff] [blame] | 326 | if (cp->type == IMX_MU_TYPE_TXDB) { |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 327 | tasklet_kill(&cp->txdb_tasklet); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 328 | pm_runtime_put_sync(priv->dev); |
Daniel Baluta | bf159d1 | 2019-10-09 16:07:18 +0800 | [diff] [blame] | 329 | return; |
| 330 | } |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 331 | |
Daniel Baluta | 5f0af07 | 2019-10-09 16:07:19 +0800 | [diff] [blame] | 332 | switch (cp->type) { |
| 333 | case IMX_MU_TYPE_TX: |
| 334 | imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_TIEn(cp->idx)); |
| 335 | break; |
| 336 | case IMX_MU_TYPE_RX: |
| 337 | imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_RIEn(cp->idx)); |
| 338 | break; |
| 339 | case IMX_MU_TYPE_RXDB: |
| 340 | imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_GIEn(cp->idx)); |
| 341 | break; |
| 342 | default: |
| 343 | break; |
| 344 | } |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 345 | |
| 346 | free_irq(priv->irq, chan); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 347 | pm_runtime_put_sync(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static const struct mbox_chan_ops imx_mu_ops = { |
| 351 | .send_data = imx_mu_send_data, |
| 352 | .startup = imx_mu_startup, |
| 353 | .shutdown = imx_mu_shutdown, |
| 354 | }; |
| 355 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 356 | static struct mbox_chan *imx_mu_scu_xlate(struct mbox_controller *mbox, |
| 357 | const struct of_phandle_args *sp) |
| 358 | { |
| 359 | u32 type, idx, chan; |
| 360 | |
| 361 | if (sp->args_count != 2) { |
| 362 | dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count); |
| 363 | return ERR_PTR(-EINVAL); |
| 364 | } |
| 365 | |
| 366 | type = sp->args[0]; /* channel type */ |
| 367 | idx = sp->args[1]; /* index */ |
| 368 | |
| 369 | switch (type) { |
| 370 | case IMX_MU_TYPE_TX: |
| 371 | case IMX_MU_TYPE_RX: |
| 372 | if (idx != 0) |
| 373 | dev_err(mbox->dev, "Invalid chan idx: %d\n", idx); |
| 374 | chan = type; |
| 375 | break; |
| 376 | case IMX_MU_TYPE_RXDB: |
| 377 | chan = 2 + idx; |
| 378 | break; |
| 379 | default: |
| 380 | dev_err(mbox->dev, "Invalid chan type: %d\n", type); |
Dan Carpenter | 1b3a347 | 2020-04-07 12:27:53 +0300 | [diff] [blame^] | 381 | return ERR_PTR(-EINVAL); |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (chan >= mbox->num_chans) { |
| 385 | dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx); |
| 386 | return ERR_PTR(-EINVAL); |
| 387 | } |
| 388 | |
| 389 | return &mbox->chans[chan]; |
| 390 | } |
| 391 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 392 | static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox, |
| 393 | const struct of_phandle_args *sp) |
| 394 | { |
| 395 | u32 type, idx, chan; |
| 396 | |
| 397 | if (sp->args_count != 2) { |
| 398 | dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count); |
| 399 | return ERR_PTR(-EINVAL); |
| 400 | } |
| 401 | |
| 402 | type = sp->args[0]; /* channel type */ |
| 403 | idx = sp->args[1]; /* index */ |
| 404 | chan = type * 4 + idx; |
| 405 | |
| 406 | if (chan >= mbox->num_chans) { |
| 407 | dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx); |
| 408 | return ERR_PTR(-EINVAL); |
| 409 | } |
| 410 | |
| 411 | return &mbox->chans[chan]; |
| 412 | } |
| 413 | |
| 414 | static void imx_mu_init_generic(struct imx_mu_priv *priv) |
| 415 | { |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 416 | unsigned int i; |
| 417 | |
| 418 | for (i = 0; i < IMX_MU_CHANS; i++) { |
| 419 | struct imx_mu_con_priv *cp = &priv->con_priv[i]; |
| 420 | |
| 421 | cp->idx = i % 4; |
| 422 | cp->type = i >> 2; |
| 423 | cp->chan = &priv->mbox_chans[i]; |
| 424 | priv->mbox_chans[i].con_priv = cp; |
| 425 | snprintf(cp->irq_desc, sizeof(cp->irq_desc), |
| 426 | "imx_mu_chan[%i-%i]", cp->type, cp->idx); |
| 427 | } |
| 428 | |
| 429 | priv->mbox.num_chans = IMX_MU_CHANS; |
| 430 | priv->mbox.of_xlate = imx_mu_xlate; |
| 431 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 432 | if (priv->side_b) |
| 433 | return; |
| 434 | |
| 435 | /* Set default MU configuration */ |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 436 | imx_mu_write(priv, 0, priv->dcfg->xCR); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 439 | static void imx_mu_init_scu(struct imx_mu_priv *priv) |
| 440 | { |
| 441 | unsigned int i; |
| 442 | |
| 443 | for (i = 0; i < IMX_MU_SCU_CHANS; i++) { |
| 444 | struct imx_mu_con_priv *cp = &priv->con_priv[i]; |
| 445 | |
| 446 | cp->idx = i < 2 ? 0 : i - 2; |
| 447 | cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB; |
| 448 | cp->chan = &priv->mbox_chans[i]; |
| 449 | priv->mbox_chans[i].con_priv = cp; |
| 450 | snprintf(cp->irq_desc, sizeof(cp->irq_desc), |
| 451 | "imx_mu_chan[%i-%i]", cp->type, cp->idx); |
| 452 | } |
| 453 | |
| 454 | priv->mbox.num_chans = IMX_MU_SCU_CHANS; |
| 455 | priv->mbox.of_xlate = imx_mu_scu_xlate; |
| 456 | |
| 457 | /* Set default MU configuration */ |
| 458 | imx_mu_write(priv, 0, priv->dcfg->xCR); |
| 459 | } |
| 460 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 461 | static int imx_mu_probe(struct platform_device *pdev) |
| 462 | { |
| 463 | struct device *dev = &pdev->dev; |
| 464 | struct device_node *np = dev->of_node; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 465 | struct imx_mu_priv *priv; |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 466 | const struct imx_mu_dcfg *dcfg; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 467 | int ret; |
| 468 | |
| 469 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 470 | if (!priv) |
| 471 | return -ENOMEM; |
| 472 | |
| 473 | priv->dev = dev; |
| 474 | |
Anson Huang | 0c40e63 | 2019-04-01 05:15:24 +0000 | [diff] [blame] | 475 | priv->base = devm_platform_ioremap_resource(pdev, 0); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 476 | if (IS_ERR(priv->base)) |
| 477 | return PTR_ERR(priv->base); |
| 478 | |
| 479 | priv->irq = platform_get_irq(pdev, 0); |
| 480 | if (priv->irq < 0) |
| 481 | return priv->irq; |
| 482 | |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 483 | dcfg = of_device_get_match_data(dev); |
| 484 | if (!dcfg) |
| 485 | return -EINVAL; |
| 486 | priv->dcfg = dcfg; |
| 487 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 488 | priv->clk = devm_clk_get(dev, NULL); |
| 489 | if (IS_ERR(priv->clk)) { |
| 490 | if (PTR_ERR(priv->clk) != -ENOENT) |
| 491 | return PTR_ERR(priv->clk); |
| 492 | |
| 493 | priv->clk = NULL; |
| 494 | } |
| 495 | |
| 496 | ret = clk_prepare_enable(priv->clk); |
| 497 | if (ret) { |
| 498 | dev_err(dev, "Failed to enable clock\n"); |
| 499 | return ret; |
| 500 | } |
| 501 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 502 | priv->side_b = of_property_read_bool(np, "fsl,mu-side-b"); |
| 503 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 504 | priv->dcfg->init(priv); |
| 505 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 506 | spin_lock_init(&priv->xcr_lock); |
| 507 | |
| 508 | priv->mbox.dev = dev; |
| 509 | priv->mbox.ops = &imx_mu_ops; |
| 510 | priv->mbox.chans = priv->mbox_chans; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 511 | priv->mbox.txdone_irq = true; |
| 512 | |
| 513 | platform_set_drvdata(pdev, priv); |
| 514 | |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 515 | ret = devm_mbox_controller_register(dev, &priv->mbox); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | |
| 519 | pm_runtime_enable(dev); |
| 520 | |
| 521 | ret = pm_runtime_get_sync(dev); |
| 522 | if (ret < 0) { |
| 523 | pm_runtime_put_noidle(dev); |
| 524 | goto disable_runtime_pm; |
| 525 | } |
| 526 | |
| 527 | ret = pm_runtime_put_sync(dev); |
| 528 | if (ret < 0) |
| 529 | goto disable_runtime_pm; |
| 530 | |
| 531 | return 0; |
| 532 | |
| 533 | disable_runtime_pm: |
| 534 | pm_runtime_disable(dev); |
| 535 | return ret; |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int imx_mu_remove(struct platform_device *pdev) |
| 539 | { |
| 540 | struct imx_mu_priv *priv = platform_get_drvdata(pdev); |
| 541 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 542 | clk_disable_unprepare(priv->clk); |
Anson Huang | 676f23e | 2020-04-13 20:25:30 +0800 | [diff] [blame] | 543 | pm_runtime_disable(priv->dev); |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Peng Fan | 63b3835 | 2020-03-19 15:49:51 +0800 | [diff] [blame] | 548 | static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = { |
| 549 | .tx = imx_mu_generic_tx, |
| 550 | .rx = imx_mu_generic_rx, |
| 551 | .init = imx_mu_init_generic, |
| 552 | .xTR = {0x0, 0x4, 0x8, 0xc}, |
| 553 | .xRR = {0x10, 0x14, 0x18, 0x1c}, |
| 554 | .xSR = 0x20, |
| 555 | .xCR = 0x24, |
| 556 | }; |
| 557 | |
| 558 | static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = { |
| 559 | .tx = imx_mu_generic_tx, |
| 560 | .rx = imx_mu_generic_rx, |
| 561 | .init = imx_mu_init_generic, |
| 562 | .xTR = {0x20, 0x24, 0x28, 0x2c}, |
| 563 | .xRR = {0x40, 0x44, 0x48, 0x4c}, |
| 564 | .xSR = 0x60, |
| 565 | .xCR = 0x64, |
| 566 | }; |
| 567 | |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 568 | static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = { |
| 569 | .tx = imx_mu_scu_tx, |
| 570 | .rx = imx_mu_scu_rx, |
| 571 | .init = imx_mu_init_scu, |
| 572 | .xTR = {0x0, 0x4, 0x8, 0xc}, |
| 573 | .xRR = {0x10, 0x14, 0x18, 0x1c}, |
| 574 | .xSR = 0x20, |
| 575 | .xCR = 0x24, |
| 576 | }; |
| 577 | |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 578 | static const struct of_device_id imx_mu_dt_ids[] = { |
Richard Zhu | c6c6bc6 | 2019-10-09 16:07:21 +0800 | [diff] [blame] | 579 | { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp }, |
| 580 | { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx }, |
Peng Fan | 0a67003 | 2020-03-19 15:49:52 +0800 | [diff] [blame] | 581 | { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu }, |
Oleksij Rempel | 2bb7005 | 2018-08-03 07:29:19 +0200 | [diff] [blame] | 582 | { }, |
| 583 | }; |
| 584 | MODULE_DEVICE_TABLE(of, imx_mu_dt_ids); |
| 585 | |
| 586 | static struct platform_driver imx_mu_driver = { |
| 587 | .probe = imx_mu_probe, |
| 588 | .remove = imx_mu_remove, |
| 589 | .driver = { |
| 590 | .name = "imx_mu", |
| 591 | .of_match_table = imx_mu_dt_ids, |
| 592 | }, |
| 593 | }; |
| 594 | module_platform_driver(imx_mu_driver); |
| 595 | |
| 596 | MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); |
| 597 | MODULE_DESCRIPTION("Message Unit driver for i.MX"); |
| 598 | MODULE_LICENSE("GPL v2"); |