1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Pengutronix, Oleksij Rempel <o.rempel@pengutronix.de>
4 * Copyright 2022 NXP, Peng Fan <peng.fan@nxp.com>
8 #include <linux/firmware/imx/ipc.h>
9 #include <linux/firmware/imx/s4.h>
10 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/mailbox_controller.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/suspend.h>
20 #include <linux/slab.h>
22 #define IMX_MU_CHANS 17
23 /* TX0/RX0/RXDB[0-3] */
24 #define IMX_MU_SCU_CHANS 6
26 #define IMX_MU_S4_CHANS 2
27 #define IMX_MU_CHAN_NAME_SIZE 20
29 #define IMX_MU_NUM_RR 4
31 #define IMX_MU_SECO_TX_TOUT (msecs_to_jiffies(3000))
32 #define IMX_MU_SECO_RX_TOUT (msecs_to_jiffies(3000))
34 /* Please not change TX & RX */
35 enum imx_mu_chan_type {
36 IMX_MU_TYPE_TX = 0, /* Tx */
37 IMX_MU_TYPE_RX = 1, /* Rx */
38 IMX_MU_TYPE_TXDB = 2, /* Tx doorbell */
39 IMX_MU_TYPE_RXDB = 3, /* Rx doorbell */
40 IMX_MU_TYPE_RST = 4, /* Reset */
60 struct imx_sc_rpc_msg_max {
61 struct imx_sc_rpc_msg hdr;
65 struct imx_s4_rpc_msg_max {
66 struct imx_s4_rpc_msg hdr;
70 struct imx_mu_con_priv {
72 char irq_desc[IMX_MU_CHAN_NAME_SIZE];
73 enum imx_mu_chan_type type;
74 struct mbox_chan *chan;
75 struct tasklet_struct txdb_tasklet;
82 spinlock_t xcr_lock; /* control register lock */
84 struct mbox_controller mbox;
85 struct mbox_chan mbox_chans[IMX_MU_CHANS];
87 struct imx_mu_con_priv con_priv[IMX_MU_CHANS];
88 const struct imx_mu_dcfg *dcfg;
90 int irq[IMX_MU_CHANS];
93 u32 xcr[IMX_MU_xCR_MAX];
101 IMX_MU_V2_S4 = BIT(15),
102 IMX_MU_V2_IRQ = BIT(16),
106 int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data);
107 int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
108 int (*rxdb)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
109 void (*init)(struct imx_mu_priv *priv);
110 enum imx_mu_type type;
111 u32 xTR; /* Transmit Register0 */
112 u32 xRR; /* Receive Register0 */
113 u32 xSR[IMX_MU_xSR_MAX]; /* Status Registers */
114 u32 xCR[IMX_MU_xCR_MAX]; /* Control Registers */
117 #define IMX_MU_xSR_GIPn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
118 #define IMX_MU_xSR_RFn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
119 #define IMX_MU_xSR_TEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
121 /* General Purpose Interrupt Enable */
122 #define IMX_MU_xCR_GIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
123 /* Receive Interrupt Enable */
124 #define IMX_MU_xCR_RIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
125 /* Transmit Interrupt Enable */
126 #define IMX_MU_xCR_TIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
127 /* General Purpose Interrupt Request */
128 #define IMX_MU_xCR_GIRn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(16 + (3 - (x))))
130 #define IMX_MU_xCR_RST(type) (type & IMX_MU_V2 ? BIT(0) : BIT(5))
131 #define IMX_MU_xSR_RST(type) (type & IMX_MU_V2 ? BIT(0) : BIT(7))
134 static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox)
136 return container_of(mbox, struct imx_mu_priv, mbox);
139 static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs)
141 iowrite32(val, priv->base + offs);
144 static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs)
146 return ioread32(priv->base + offs);
149 static int imx_mu_tx_waiting_write(struct imx_mu_priv *priv, u32 val, u32 idx)
151 u64 timeout_time = get_jiffies_64() + IMX_MU_SECO_TX_TOUT;
155 dev_dbg(priv->dev, "Trying to write %.8x to idx %d\n", val, idx);
158 status = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
159 can_write = status & IMX_MU_xSR_TEn(priv->dcfg->type, idx % 4);
160 } while (!can_write && time_is_after_jiffies64(timeout_time));
163 dev_err(priv->dev, "timeout trying to write %.8x at %d(%.8x)\n",
168 imx_mu_write(priv, val, priv->dcfg->xTR + (idx % 4) * 4);
173 static int imx_mu_rx_waiting_read(struct imx_mu_priv *priv, u32 *val, u32 idx)
175 u64 timeout_time = get_jiffies_64() + IMX_MU_SECO_RX_TOUT;
179 dev_dbg(priv->dev, "Trying to read from idx %d\n", idx);
182 status = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
183 can_read = status & IMX_MU_xSR_RFn(priv->dcfg->type, idx % 4);
184 } while (!can_read && time_is_after_jiffies64(timeout_time));
187 dev_err(priv->dev, "timeout trying to read idx %d (%.8x)\n",
192 *val = imx_mu_read(priv, priv->dcfg->xRR + (idx % 4) * 4);
193 dev_dbg(priv->dev, "Read %.8x\n", *val);
198 static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, enum imx_mu_xcr type, u32 set, u32 clr)
203 spin_lock_irqsave(&priv->xcr_lock, flags);
204 val = imx_mu_read(priv, priv->dcfg->xCR[type]);
207 imx_mu_write(priv, val, priv->dcfg->xCR[type]);
208 spin_unlock_irqrestore(&priv->xcr_lock, flags);
213 static int imx_mu_generic_tx(struct imx_mu_priv *priv,
214 struct imx_mu_con_priv *cp,
221 imx_mu_write(priv, *arg, priv->dcfg->xTR + cp->idx * 4);
222 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
224 case IMX_MU_TYPE_TXDB:
225 imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0);
226 tasklet_schedule(&cp->txdb_tasklet);
229 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
236 static int imx_mu_generic_rx(struct imx_mu_priv *priv,
237 struct imx_mu_con_priv *cp)
241 dat = imx_mu_read(priv, priv->dcfg->xRR + (cp->idx) * 4);
242 mbox_chan_received_data(cp->chan, (void *)&dat);
247 static int imx_mu_generic_rxdb(struct imx_mu_priv *priv,
248 struct imx_mu_con_priv *cp)
250 imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx),
251 priv->dcfg->xSR[IMX_MU_GSR]);
252 mbox_chan_received_data(cp->chan, NULL);
257 static int imx_mu_specific_tx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data)
262 u32 size, max_size, num_tr;
264 if (priv->dcfg->type & IMX_MU_V2_S4) {
265 size = ((struct imx_s4_rpc_msg_max *)data)->hdr.size;
266 max_size = sizeof(struct imx_s4_rpc_msg_max);
269 size = ((struct imx_sc_rpc_msg_max *)data)->hdr.size;
270 max_size = sizeof(struct imx_sc_rpc_msg_max);
277 * msg->hdr.size specifies the number of u32 words while
278 * sizeof yields bytes.
281 if (size > max_size / 4) {
283 * The real message size can be different to
284 * struct imx_sc_rpc_msg_max/imx_s4_rpc_msg_max size
286 dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on TX; got: %i bytes\n", max_size, size << 2);
290 for (i = 0; i < num_tr && i < size; i++)
291 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4);
292 for (; i < size; i++) {
293 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_TSR],
295 xsr & IMX_MU_xSR_TEn(priv->dcfg->type, i % num_tr),
296 0, 5 * USEC_PER_SEC);
298 dev_err(priv->dev, "Send data index: %d timeout\n", i);
301 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4);
304 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
307 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
314 static int imx_mu_specific_rx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp)
321 data = (u32 *)priv->msg;
323 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, 0));
324 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
326 if (priv->dcfg->type & IMX_MU_V2_S4) {
327 size = ((struct imx_s4_rpc_msg_max *)priv->msg)->hdr.size;
328 max_size = sizeof(struct imx_s4_rpc_msg_max);
330 size = ((struct imx_sc_rpc_msg_max *)priv->msg)->hdr.size;
331 max_size = sizeof(struct imx_sc_rpc_msg_max);
334 if (size > max_size / 4) {
335 dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on RX; got: %i bytes\n", max_size, size << 2);
339 for (i = 1; i < size; i++) {
340 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_RSR], xsr,
341 xsr & IMX_MU_xSR_RFn(priv->dcfg->type, i % 4), 0,
344 dev_err(priv->dev, "timeout read idx %d\n", i);
347 *data++ = imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4);
350 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, 0), 0);
351 mbox_chan_received_data(cp->chan, (void *)priv->msg);
356 static int imx_mu_seco_tx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp,
359 struct imx_sc_rpc_msg_max *msg = data;
365 dev_dbg(priv->dev, "Sending message\n");
368 case IMX_MU_TYPE_TXDB:
369 byte_size = msg->hdr.size * sizeof(u32);
370 if (byte_size > sizeof(*msg)) {
372 * The real message size can be different to
373 * struct imx_sc_rpc_msg_max size
376 "Exceed max msg size (%zu) on TX, got: %i\n",
377 sizeof(*msg), byte_size);
381 print_hex_dump_debug("from client ", DUMP_PREFIX_OFFSET, 4, 4,
382 data, byte_size, false);
384 /* Send first word */
385 dev_dbg(priv->dev, "Sending header\n");
386 imx_mu_write(priv, *arg++, priv->dcfg->xTR);
389 dev_dbg(priv->dev, "Sending signaling\n");
390 imx_mu_xcr_rmw(priv, IMX_MU_GCR,
391 IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0);
393 /* Send words to fill the mailbox */
394 for (i = 1; i < 4 && i < msg->hdr.size; i++) {
395 dev_dbg(priv->dev, "Sending word %d\n", i);
396 imx_mu_write(priv, *arg++,
397 priv->dcfg->xTR + (i % 4) * 4);
400 /* Send rest of message waiting for remote read */
401 for (; i < msg->hdr.size; i++) {
402 dev_dbg(priv->dev, "Sending word %d\n", i);
403 err = imx_mu_tx_waiting_write(priv, *arg++, i);
405 dev_err(priv->dev, "Timeout tx %d\n", i);
410 /* Simulate hack for mbox framework */
411 tasklet_schedule(&cp->txdb_tasklet);
415 dev_warn_ratelimited(priv->dev,
416 "Send data on wrong channel type: %d\n",
424 static int imx_mu_seco_rxdb(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp)
426 struct imx_sc_rpc_msg_max msg;
427 u32 *data = (u32 *)&msg;
432 dev_dbg(priv->dev, "Receiving message\n");
435 dev_dbg(priv->dev, "Receiving header\n");
436 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
437 byte_size = msg.hdr.size * sizeof(u32);
438 if (byte_size > sizeof(msg)) {
439 dev_err(priv->dev, "Exceed max msg size (%zu) on RX, got: %i\n",
440 sizeof(msg), byte_size);
445 /* Read message waiting they are written */
446 for (i = 1; i < msg.hdr.size; i++) {
447 dev_dbg(priv->dev, "Receiving word %d\n", i);
448 err = imx_mu_rx_waiting_read(priv, data++, i);
450 dev_err(priv->dev, "Timeout rx %d\n", i);
456 imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx),
457 priv->dcfg->xSR[IMX_MU_GSR]);
459 print_hex_dump_debug("to client ", DUMP_PREFIX_OFFSET, 4, 4,
460 &msg, byte_size, false);
462 /* send data to client */
463 dev_dbg(priv->dev, "Sending message to client\n");
464 mbox_chan_received_data(cp->chan, (void *)&msg);
469 mbox_chan_received_data(cp->chan, ERR_PTR(err));
475 static void imx_mu_txdb_tasklet(unsigned long data)
477 struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data;
479 mbox_chan_txdone(cp->chan, 0);
482 static irqreturn_t imx_mu_isr(int irq, void *p)
484 struct mbox_chan *chan = p;
485 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
486 struct imx_mu_con_priv *cp = chan->con_priv;
491 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_TCR]);
492 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
493 val &= IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx) &
494 (ctrl & IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
497 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_RCR]);
498 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
499 val &= IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx) &
500 (ctrl & IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
502 case IMX_MU_TYPE_RXDB:
503 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_GIER]);
504 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]);
505 val &= IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx) &
506 (ctrl & IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
508 case IMX_MU_TYPE_RST:
511 dev_warn_ratelimited(priv->dev, "Unhandled channel type %d\n",
519 if ((val == IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx)) &&
520 (cp->type == IMX_MU_TYPE_TX)) {
521 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
522 mbox_chan_txdone(chan, 0);
523 } else if ((val == IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx)) &&
524 (cp->type == IMX_MU_TYPE_RX)) {
525 priv->dcfg->rx(priv, cp);
526 } else if ((val == IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx)) &&
527 (cp->type == IMX_MU_TYPE_RXDB)) {
528 priv->dcfg->rxdb(priv, cp);
530 dev_warn_ratelimited(priv->dev, "Not handled interrupt\n");
540 static int imx_mu_send_data(struct mbox_chan *chan, void *data)
542 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
543 struct imx_mu_con_priv *cp = chan->con_priv;
545 return priv->dcfg->tx(priv, cp, data);
548 static int imx_mu_startup(struct mbox_chan *chan)
550 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
551 struct imx_mu_con_priv *cp = chan->con_priv;
552 unsigned long irq_flag = 0;
555 pm_runtime_get_sync(priv->dev);
556 if (cp->type == IMX_MU_TYPE_TXDB) {
557 /* Tx doorbell don't have ACK support */
558 tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet,
563 /* IPC MU should be with IRQF_NO_SUSPEND set */
564 if (!priv->dev->pm_domain)
565 irq_flag |= IRQF_NO_SUSPEND;
567 if (!(priv->dcfg->type & IMX_MU_V2_IRQ))
568 irq_flag |= IRQF_SHARED;
570 ret = request_irq(priv->irq[cp->type], imx_mu_isr, irq_flag, cp->irq_desc, chan);
572 dev_err(priv->dev, "Unable to acquire IRQ %d\n", priv->irq[cp->type]);
578 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx), 0);
580 case IMX_MU_TYPE_RXDB:
581 imx_mu_xcr_rmw(priv, IMX_MU_GIER, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx), 0);
590 static void imx_mu_shutdown(struct mbox_chan *chan)
592 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
593 struct imx_mu_con_priv *cp = chan->con_priv;
597 if (cp->type == IMX_MU_TYPE_TXDB) {
598 tasklet_kill(&cp->txdb_tasklet);
599 pm_runtime_put_sync(priv->dev);
605 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
608 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
610 case IMX_MU_TYPE_RXDB:
611 imx_mu_xcr_rmw(priv, IMX_MU_GIER, 0, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
613 case IMX_MU_TYPE_RST:
614 imx_mu_xcr_rmw(priv, IMX_MU_CR, IMX_MU_xCR_RST(priv->dcfg->type), 0);
615 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_SR], sr,
616 !(sr & IMX_MU_xSR_RST(priv->dcfg->type)), 1, 5);
618 dev_warn(priv->dev, "RST channel timeout\n");
624 free_irq(priv->irq[cp->type], chan);
625 pm_runtime_put_sync(priv->dev);
628 static const struct mbox_chan_ops imx_mu_ops = {
629 .send_data = imx_mu_send_data,
630 .startup = imx_mu_startup,
631 .shutdown = imx_mu_shutdown,
634 static struct mbox_chan *imx_mu_specific_xlate(struct mbox_controller *mbox,
635 const struct of_phandle_args *sp)
639 if (sp->args_count != 2) {
640 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
641 return ERR_PTR(-EINVAL);
644 type = sp->args[0]; /* channel type */
645 idx = sp->args[1]; /* index */
651 dev_err(mbox->dev, "Invalid chan idx: %d\n", idx);
654 case IMX_MU_TYPE_RXDB:
658 dev_err(mbox->dev, "Invalid chan type: %d\n", type);
659 return ERR_PTR(-EINVAL);
662 if (chan >= mbox->num_chans) {
663 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
664 return ERR_PTR(-EINVAL);
667 return &mbox->chans[chan];
670 static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox,
671 const struct of_phandle_args *sp)
675 if (sp->args_count != 2) {
676 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
677 return ERR_PTR(-EINVAL);
680 type = sp->args[0]; /* channel type */
681 idx = sp->args[1]; /* index */
682 chan = type * 4 + idx;
684 if (chan >= mbox->num_chans) {
685 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
686 return ERR_PTR(-EINVAL);
689 return &mbox->chans[chan];
692 static struct mbox_chan *imx_mu_seco_xlate(struct mbox_controller *mbox,
693 const struct of_phandle_args *sp)
697 if (sp->args_count < 1) {
698 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
699 return ERR_PTR(-EINVAL);
702 type = sp->args[0]; /* channel type */
704 /* Only supports TXDB and RXDB */
705 if (type == IMX_MU_TYPE_TX || type == IMX_MU_TYPE_RX) {
706 dev_err(mbox->dev, "Invalid type: %d\n", type);
707 return ERR_PTR(-EINVAL);
710 return imx_mu_xlate(mbox, sp);
713 static void imx_mu_init_generic(struct imx_mu_priv *priv)
718 for (i = 0; i < IMX_MU_CHANS; i++) {
719 struct imx_mu_con_priv *cp = &priv->con_priv[i];
723 cp->chan = &priv->mbox_chans[i];
724 priv->mbox_chans[i].con_priv = cp;
725 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
726 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
729 priv->mbox.num_chans = IMX_MU_CHANS;
730 priv->mbox.of_xlate = imx_mu_xlate;
735 /* Set default MU configuration */
736 for (i = 0; i < IMX_MU_xCR_MAX; i++)
737 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
739 /* Clear any pending GIP */
740 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]);
741 imx_mu_write(priv, val, priv->dcfg->xSR[IMX_MU_GSR]);
743 /* Clear any pending RSR */
744 for (i = 0; i < IMX_MU_NUM_RR; i++)
745 imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4);
748 static void imx_mu_init_specific(struct imx_mu_priv *priv)
751 int num_chans = priv->dcfg->type & IMX_MU_V2_S4 ? IMX_MU_S4_CHANS : IMX_MU_SCU_CHANS;
753 for (i = 0; i < num_chans; i++) {
754 struct imx_mu_con_priv *cp = &priv->con_priv[i];
756 cp->idx = i < 2 ? 0 : i - 2;
757 cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB;
758 cp->chan = &priv->mbox_chans[i];
759 priv->mbox_chans[i].con_priv = cp;
760 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
761 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
764 priv->mbox.num_chans = num_chans;
765 priv->mbox.of_xlate = imx_mu_specific_xlate;
767 /* Set default MU configuration */
768 for (i = 0; i < IMX_MU_xCR_MAX; i++)
769 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
772 static void imx_mu_init_seco(struct imx_mu_priv *priv)
774 imx_mu_init_generic(priv);
775 priv->mbox.of_xlate = imx_mu_seco_xlate;
778 static int imx_mu_probe(struct platform_device *pdev)
780 struct device *dev = &pdev->dev;
781 struct device_node *np = dev->of_node;
782 struct imx_mu_priv *priv;
783 const struct imx_mu_dcfg *dcfg;
787 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
793 priv->base = devm_platform_ioremap_resource(pdev, 0);
794 if (IS_ERR(priv->base))
795 return PTR_ERR(priv->base);
797 dcfg = of_device_get_match_data(dev);
801 if (priv->dcfg->type & IMX_MU_V2_IRQ) {
802 priv->irq[IMX_MU_TYPE_TX] = platform_get_irq_byname(pdev, "tx");
803 if (priv->irq[IMX_MU_TYPE_TX] < 0)
804 return priv->irq[IMX_MU_TYPE_TX];
805 priv->irq[IMX_MU_TYPE_RX] = platform_get_irq_byname(pdev, "rx");
806 if (priv->irq[IMX_MU_TYPE_RX] < 0)
807 return priv->irq[IMX_MU_TYPE_RX];
809 ret = platform_get_irq(pdev, 0);
813 for (i = 0; i < IMX_MU_CHANS; i++)
817 if (priv->dcfg->type & IMX_MU_V2_S4)
818 size = sizeof(struct imx_s4_rpc_msg_max);
820 size = sizeof(struct imx_sc_rpc_msg_max);
822 priv->msg = devm_kzalloc(dev, size, GFP_KERNEL);
826 priv->clk = devm_clk_get(dev, NULL);
827 if (IS_ERR(priv->clk)) {
828 if (PTR_ERR(priv->clk) != -ENOENT)
829 return PTR_ERR(priv->clk);
834 ret = clk_prepare_enable(priv->clk);
836 dev_err(dev, "Failed to enable clock\n");
840 priv->side_b = of_property_read_bool(np, "fsl,mu-side-b");
842 priv->dcfg->init(priv);
844 spin_lock_init(&priv->xcr_lock);
846 priv->mbox.dev = dev;
847 priv->mbox.ops = &imx_mu_ops;
848 priv->mbox.chans = priv->mbox_chans;
849 priv->mbox.txdone_irq = true;
851 platform_set_drvdata(pdev, priv);
853 ret = devm_mbox_controller_register(dev, &priv->mbox);
855 clk_disable_unprepare(priv->clk);
859 pm_runtime_enable(dev);
861 ret = pm_runtime_resume_and_get(dev);
863 goto disable_runtime_pm;
865 ret = pm_runtime_put_sync(dev);
867 goto disable_runtime_pm;
869 clk_disable_unprepare(priv->clk);
874 pm_runtime_disable(dev);
875 clk_disable_unprepare(priv->clk);
879 static int imx_mu_remove(struct platform_device *pdev)
881 struct imx_mu_priv *priv = platform_get_drvdata(pdev);
883 pm_runtime_disable(priv->dev);
888 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = {
889 .tx = imx_mu_generic_tx,
890 .rx = imx_mu_generic_rx,
891 .rxdb = imx_mu_generic_rxdb,
892 .init = imx_mu_init_generic,
895 .xSR = {0x20, 0x20, 0x20, 0x20},
896 .xCR = {0x24, 0x24, 0x24, 0x24, 0x24},
899 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = {
900 .tx = imx_mu_generic_tx,
901 .rx = imx_mu_generic_rx,
902 .rxdb = imx_mu_generic_rxdb,
903 .init = imx_mu_init_generic,
906 .xSR = {0x60, 0x60, 0x60, 0x60},
907 .xCR = {0x64, 0x64, 0x64, 0x64},
910 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp = {
911 .tx = imx_mu_generic_tx,
912 .rx = imx_mu_generic_rx,
913 .rxdb = imx_mu_generic_rxdb,
914 .init = imx_mu_init_generic,
918 .xSR = {0xC, 0x118, 0x124, 0x12C},
919 .xCR = {0x8, 0x110, 0x114, 0x120, 0x128},
922 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp_s4 = {
923 .tx = imx_mu_specific_tx,
924 .rx = imx_mu_specific_rx,
925 .init = imx_mu_init_specific,
926 .type = IMX_MU_V2 | IMX_MU_V2_S4,
929 .xSR = {0xC, 0x118, 0x124, 0x12C},
930 .xCR = {0x110, 0x114, 0x120, 0x128},
933 static const struct imx_mu_dcfg imx_mu_cfg_imx93_s4 = {
934 .tx = imx_mu_specific_tx,
935 .rx = imx_mu_specific_rx,
936 .init = imx_mu_init_specific,
937 .type = IMX_MU_V2 | IMX_MU_V2_S4 | IMX_MU_V2_IRQ,
940 .xSR = {0xC, 0x118, 0x124, 0x12C},
941 .xCR = {0x110, 0x114, 0x120, 0x128},
944 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = {
945 .tx = imx_mu_specific_tx,
946 .rx = imx_mu_specific_rx,
947 .init = imx_mu_init_specific,
948 .rxdb = imx_mu_generic_rxdb,
951 .xSR = {0x20, 0x20, 0x20, 0x20},
952 .xCR = {0x24, 0x24, 0x24, 0x24},
955 static const struct imx_mu_dcfg imx_mu_cfg_imx8_seco = {
956 .tx = imx_mu_seco_tx,
957 .rx = imx_mu_generic_rx,
958 .rxdb = imx_mu_seco_rxdb,
959 .init = imx_mu_init_seco,
962 .xSR = {0x20, 0x20, 0x20, 0x20},
963 .xCR = {0x24, 0x24, 0x24, 0x24},
966 static const struct of_device_id imx_mu_dt_ids[] = {
967 { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp },
968 { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx },
969 { .compatible = "fsl,imx8ulp-mu", .data = &imx_mu_cfg_imx8ulp },
970 { .compatible = "fsl,imx8ulp-mu-s4", .data = &imx_mu_cfg_imx8ulp_s4 },
971 { .compatible = "fsl,imx93-mu-s4", .data = &imx_mu_cfg_imx93_s4 },
972 { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu },
973 { .compatible = "fsl,imx8-mu-seco", .data = &imx_mu_cfg_imx8_seco },
976 MODULE_DEVICE_TABLE(of, imx_mu_dt_ids);
978 static int __maybe_unused imx_mu_suspend_noirq(struct device *dev)
980 struct imx_mu_priv *priv = dev_get_drvdata(dev);
984 for (i = 0; i < IMX_MU_xCR_MAX; i++)
985 priv->xcr[i] = imx_mu_read(priv, priv->dcfg->xCR[i]);
988 priv->suspend = true;
993 static int __maybe_unused imx_mu_resume_noirq(struct device *dev)
995 struct imx_mu_priv *priv = dev_get_drvdata(dev);
999 * ONLY restore MU when context lost, the TIE could
1000 * be set during noirq resume as there is MU data
1001 * communication going on, and restore the saved
1002 * value will overwrite the TIE and cause MU data
1003 * send failed, may lead to system freeze. This issue
1004 * is observed by testing freeze mode suspend.
1006 if (!priv->clk && !imx_mu_read(priv, priv->dcfg->xCR[0])) {
1007 for (i = 0; i < IMX_MU_xCR_MAX; i++)
1008 imx_mu_write(priv, priv->xcr[i], priv->dcfg->xCR[i]);
1011 priv->suspend = false;
1016 static int __maybe_unused imx_mu_runtime_suspend(struct device *dev)
1018 struct imx_mu_priv *priv = dev_get_drvdata(dev);
1020 clk_disable_unprepare(priv->clk);
1025 static int __maybe_unused imx_mu_runtime_resume(struct device *dev)
1027 struct imx_mu_priv *priv = dev_get_drvdata(dev);
1030 ret = clk_prepare_enable(priv->clk);
1032 dev_err(dev, "failed to enable clock\n");
1037 static const struct dev_pm_ops imx_mu_pm_ops = {
1038 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq,
1039 imx_mu_resume_noirq)
1040 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend,
1041 imx_mu_runtime_resume, NULL)
1044 static struct platform_driver imx_mu_driver = {
1045 .probe = imx_mu_probe,
1046 .remove = imx_mu_remove,
1049 .of_match_table = imx_mu_dt_ids,
1050 .pm = &imx_mu_pm_ops,
1053 module_platform_driver(imx_mu_driver);
1055 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
1056 MODULE_DESCRIPTION("Message Unit driver for i.MX");
1057 MODULE_LICENSE("GPL v2");