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 16
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_SECO_TX_TOUT (msecs_to_jiffies(3000))
30 #define IMX_MU_SECO_RX_TOUT (msecs_to_jiffies(3000))
32 /* Please not change TX & RX */
33 enum imx_mu_chan_type {
34 IMX_MU_TYPE_TX = 0, /* Tx */
35 IMX_MU_TYPE_RX = 1, /* Rx */
36 IMX_MU_TYPE_TXDB = 2, /* Tx doorbell */
37 IMX_MU_TYPE_RXDB = 3, /* Rx doorbell */
55 struct imx_sc_rpc_msg_max {
56 struct imx_sc_rpc_msg hdr;
60 struct imx_s4_rpc_msg_max {
61 struct imx_s4_rpc_msg hdr;
65 struct imx_mu_con_priv {
67 char irq_desc[IMX_MU_CHAN_NAME_SIZE];
68 enum imx_mu_chan_type type;
69 struct mbox_chan *chan;
70 struct tasklet_struct txdb_tasklet;
77 spinlock_t xcr_lock; /* control register lock */
79 struct mbox_controller mbox;
80 struct mbox_chan mbox_chans[IMX_MU_CHANS];
82 struct imx_mu_con_priv con_priv[IMX_MU_CHANS];
83 const struct imx_mu_dcfg *dcfg;
85 int irq[IMX_MU_CHANS];
96 IMX_MU_V2_S4 = BIT(15),
97 IMX_MU_V2_IRQ = BIT(16),
101 int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data);
102 int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
103 int (*rxdb)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
104 void (*init)(struct imx_mu_priv *priv);
105 enum imx_mu_type type;
106 u32 xTR; /* Transmit Register0 */
107 u32 xRR; /* Receive Register0 */
108 u32 xSR[4]; /* Status Registers */
109 u32 xCR[4]; /* Control Registers */
112 #define IMX_MU_xSR_GIPn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
113 #define IMX_MU_xSR_RFn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
114 #define IMX_MU_xSR_TEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
116 /* General Purpose Interrupt Enable */
117 #define IMX_MU_xCR_GIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
118 /* Receive Interrupt Enable */
119 #define IMX_MU_xCR_RIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
120 /* Transmit Interrupt Enable */
121 #define IMX_MU_xCR_TIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
122 /* General Purpose Interrupt Request */
123 #define IMX_MU_xCR_GIRn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(16 + (3 - (x))))
126 static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox)
128 return container_of(mbox, struct imx_mu_priv, mbox);
131 static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs)
133 iowrite32(val, priv->base + offs);
136 static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs)
138 return ioread32(priv->base + offs);
141 static int imx_mu_tx_waiting_write(struct imx_mu_priv *priv, u32 val, u32 idx)
143 u64 timeout_time = get_jiffies_64() + IMX_MU_SECO_TX_TOUT;
147 dev_dbg(priv->dev, "Trying to write %.8x to idx %d\n", val, idx);
150 status = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
151 can_write = status & IMX_MU_xSR_TEn(priv->dcfg->type, idx % 4);
152 } while (!can_write && time_is_after_jiffies64(timeout_time));
155 dev_err(priv->dev, "timeout trying to write %.8x at %d(%.8x)\n",
160 imx_mu_write(priv, val, priv->dcfg->xTR + (idx % 4) * 4);
165 static int imx_mu_rx_waiting_read(struct imx_mu_priv *priv, u32 *val, u32 idx)
167 u64 timeout_time = get_jiffies_64() + IMX_MU_SECO_RX_TOUT;
171 dev_dbg(priv->dev, "Trying to read from idx %d\n", idx);
174 status = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
175 can_read = status & IMX_MU_xSR_RFn(priv->dcfg->type, idx % 4);
176 } while (!can_read && time_is_after_jiffies64(timeout_time));
179 dev_err(priv->dev, "timeout trying to read idx %d (%.8x)\n",
184 *val = imx_mu_read(priv, priv->dcfg->xRR + (idx % 4) * 4);
185 dev_dbg(priv->dev, "Read %.8x\n", *val);
190 static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, enum imx_mu_xcr type, u32 set, u32 clr)
195 spin_lock_irqsave(&priv->xcr_lock, flags);
196 val = imx_mu_read(priv, priv->dcfg->xCR[type]);
199 imx_mu_write(priv, val, priv->dcfg->xCR[type]);
200 spin_unlock_irqrestore(&priv->xcr_lock, flags);
205 static int imx_mu_generic_tx(struct imx_mu_priv *priv,
206 struct imx_mu_con_priv *cp,
213 imx_mu_write(priv, *arg, priv->dcfg->xTR + cp->idx * 4);
214 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
216 case IMX_MU_TYPE_TXDB:
217 imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0);
218 tasklet_schedule(&cp->txdb_tasklet);
221 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
228 static int imx_mu_generic_rx(struct imx_mu_priv *priv,
229 struct imx_mu_con_priv *cp)
233 dat = imx_mu_read(priv, priv->dcfg->xRR + (cp->idx) * 4);
234 mbox_chan_received_data(cp->chan, (void *)&dat);
239 static int imx_mu_generic_rxdb(struct imx_mu_priv *priv,
240 struct imx_mu_con_priv *cp)
242 imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx),
243 priv->dcfg->xSR[IMX_MU_GSR]);
244 mbox_chan_received_data(cp->chan, NULL);
249 static int imx_mu_specific_tx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data)
254 u32 size, max_size, num_tr;
256 if (priv->dcfg->type & IMX_MU_V2_S4) {
257 size = ((struct imx_s4_rpc_msg_max *)data)->hdr.size;
258 max_size = sizeof(struct imx_s4_rpc_msg_max);
261 size = ((struct imx_sc_rpc_msg_max *)data)->hdr.size;
262 max_size = sizeof(struct imx_sc_rpc_msg_max);
269 * msg->hdr.size specifies the number of u32 words while
270 * sizeof yields bytes.
273 if (size > max_size / 4) {
275 * The real message size can be different to
276 * struct imx_sc_rpc_msg_max/imx_s4_rpc_msg_max size
278 dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on TX; got: %i bytes\n", max_size, size << 2);
282 for (i = 0; i < num_tr && i < size; i++)
283 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4);
284 for (; i < size; i++) {
285 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_TSR],
287 xsr & IMX_MU_xSR_TEn(priv->dcfg->type, i % num_tr),
288 0, 5 * USEC_PER_SEC);
290 dev_err(priv->dev, "Send data index: %d timeout\n", i);
293 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % num_tr) * 4);
296 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
299 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
306 static int imx_mu_specific_rx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp)
313 data = (u32 *)priv->msg;
315 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, 0));
316 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
318 if (priv->dcfg->type & IMX_MU_V2_S4) {
319 size = ((struct imx_s4_rpc_msg_max *)priv->msg)->hdr.size;
320 max_size = sizeof(struct imx_s4_rpc_msg_max);
322 size = ((struct imx_sc_rpc_msg_max *)priv->msg)->hdr.size;
323 max_size = sizeof(struct imx_sc_rpc_msg_max);
326 if (size > max_size / 4) {
327 dev_err(priv->dev, "Maximal message size (%u bytes) exceeded on RX; got: %i bytes\n", max_size, size << 2);
331 for (i = 1; i < size; i++) {
332 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_RSR], xsr,
333 xsr & IMX_MU_xSR_RFn(priv->dcfg->type, i % 4), 0,
336 dev_err(priv->dev, "timeout read idx %d\n", i);
339 *data++ = imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4);
342 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, 0), 0);
343 mbox_chan_received_data(cp->chan, (void *)priv->msg);
348 static int imx_mu_seco_tx(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp,
351 struct imx_sc_rpc_msg_max *msg = data;
357 dev_dbg(priv->dev, "Sending message\n");
360 case IMX_MU_TYPE_TXDB:
361 byte_size = msg->hdr.size * sizeof(u32);
362 if (byte_size > sizeof(*msg)) {
364 * The real message size can be different to
365 * struct imx_sc_rpc_msg_max size
368 "Exceed max msg size (%zu) on TX, got: %i\n",
369 sizeof(*msg), byte_size);
373 print_hex_dump_debug("from client ", DUMP_PREFIX_OFFSET, 4, 4,
374 data, byte_size, false);
376 /* Send first word */
377 dev_dbg(priv->dev, "Sending header\n");
378 imx_mu_write(priv, *arg++, priv->dcfg->xTR);
381 dev_dbg(priv->dev, "Sending signaling\n");
382 imx_mu_xcr_rmw(priv, IMX_MU_GCR,
383 IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0);
385 /* Send words to fill the mailbox */
386 for (i = 1; i < 4 && i < msg->hdr.size; i++) {
387 dev_dbg(priv->dev, "Sending word %d\n", i);
388 imx_mu_write(priv, *arg++,
389 priv->dcfg->xTR + (i % 4) * 4);
392 /* Send rest of message waiting for remote read */
393 for (; i < msg->hdr.size; i++) {
394 dev_dbg(priv->dev, "Sending word %d\n", i);
395 err = imx_mu_tx_waiting_write(priv, *arg++, i);
397 dev_err(priv->dev, "Timeout tx %d\n", i);
402 /* Simulate hack for mbox framework */
403 tasklet_schedule(&cp->txdb_tasklet);
407 dev_warn_ratelimited(priv->dev,
408 "Send data on wrong channel type: %d\n",
416 static int imx_mu_seco_rxdb(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp)
418 struct imx_sc_rpc_msg_max msg;
419 u32 *data = (u32 *)&msg;
424 dev_dbg(priv->dev, "Receiving message\n");
427 dev_dbg(priv->dev, "Receiving header\n");
428 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
429 byte_size = msg.hdr.size * sizeof(u32);
430 if (byte_size > sizeof(msg)) {
431 dev_err(priv->dev, "Exceed max msg size (%zu) on RX, got: %i\n",
432 sizeof(msg), byte_size);
437 /* Read message waiting they are written */
438 for (i = 1; i < msg.hdr.size; i++) {
439 dev_dbg(priv->dev, "Receiving word %d\n", i);
440 err = imx_mu_rx_waiting_read(priv, data++, i);
442 dev_err(priv->dev, "Timeout rx %d\n", i);
448 imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx),
449 priv->dcfg->xSR[IMX_MU_GSR]);
451 print_hex_dump_debug("to client ", DUMP_PREFIX_OFFSET, 4, 4,
452 &msg, byte_size, false);
454 /* send data to client */
455 dev_dbg(priv->dev, "Sending message to client\n");
456 mbox_chan_received_data(cp->chan, (void *)&msg);
461 mbox_chan_received_data(cp->chan, ERR_PTR(err));
467 static void imx_mu_txdb_tasklet(unsigned long data)
469 struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data;
471 mbox_chan_txdone(cp->chan, 0);
474 static irqreturn_t imx_mu_isr(int irq, void *p)
476 struct mbox_chan *chan = p;
477 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
478 struct imx_mu_con_priv *cp = chan->con_priv;
483 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_TCR]);
484 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
485 val &= IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx) &
486 (ctrl & IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
489 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_RCR]);
490 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
491 val &= IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx) &
492 (ctrl & IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
494 case IMX_MU_TYPE_RXDB:
495 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_GIER]);
496 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]);
497 val &= IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx) &
498 (ctrl & IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
501 dev_warn_ratelimited(priv->dev, "Unhandled channel type %d\n",
509 if ((val == IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx)) &&
510 (cp->type == IMX_MU_TYPE_TX)) {
511 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
512 mbox_chan_txdone(chan, 0);
513 } else if ((val == IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx)) &&
514 (cp->type == IMX_MU_TYPE_RX)) {
515 priv->dcfg->rx(priv, cp);
516 } else if ((val == IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx)) &&
517 (cp->type == IMX_MU_TYPE_RXDB)) {
518 priv->dcfg->rxdb(priv, cp);
520 dev_warn_ratelimited(priv->dev, "Not handled interrupt\n");
530 static int imx_mu_send_data(struct mbox_chan *chan, void *data)
532 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
533 struct imx_mu_con_priv *cp = chan->con_priv;
535 return priv->dcfg->tx(priv, cp, data);
538 static int imx_mu_startup(struct mbox_chan *chan)
540 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
541 struct imx_mu_con_priv *cp = chan->con_priv;
542 unsigned long irq_flag = 0;
545 pm_runtime_get_sync(priv->dev);
546 if (cp->type == IMX_MU_TYPE_TXDB) {
547 /* Tx doorbell don't have ACK support */
548 tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet,
553 /* IPC MU should be with IRQF_NO_SUSPEND set */
554 if (!priv->dev->pm_domain)
555 irq_flag |= IRQF_NO_SUSPEND;
557 if (!(priv->dcfg->type & IMX_MU_V2_IRQ))
558 irq_flag |= IRQF_SHARED;
560 ret = request_irq(priv->irq[cp->type], imx_mu_isr, irq_flag, cp->irq_desc, chan);
562 dev_err(priv->dev, "Unable to acquire IRQ %d\n", priv->irq[cp->type]);
568 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx), 0);
570 case IMX_MU_TYPE_RXDB:
571 imx_mu_xcr_rmw(priv, IMX_MU_GIER, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx), 0);
580 static void imx_mu_shutdown(struct mbox_chan *chan)
582 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
583 struct imx_mu_con_priv *cp = chan->con_priv;
585 if (cp->type == IMX_MU_TYPE_TXDB) {
586 tasklet_kill(&cp->txdb_tasklet);
587 pm_runtime_put_sync(priv->dev);
593 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
596 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
598 case IMX_MU_TYPE_RXDB:
599 imx_mu_xcr_rmw(priv, IMX_MU_GIER, 0, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
605 free_irq(priv->irq[cp->type], chan);
606 pm_runtime_put_sync(priv->dev);
609 static const struct mbox_chan_ops imx_mu_ops = {
610 .send_data = imx_mu_send_data,
611 .startup = imx_mu_startup,
612 .shutdown = imx_mu_shutdown,
615 static struct mbox_chan *imx_mu_specific_xlate(struct mbox_controller *mbox,
616 const struct of_phandle_args *sp)
620 if (sp->args_count != 2) {
621 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
622 return ERR_PTR(-EINVAL);
625 type = sp->args[0]; /* channel type */
626 idx = sp->args[1]; /* index */
632 dev_err(mbox->dev, "Invalid chan idx: %d\n", idx);
635 case IMX_MU_TYPE_RXDB:
639 dev_err(mbox->dev, "Invalid chan type: %d\n", type);
640 return ERR_PTR(-EINVAL);
643 if (chan >= mbox->num_chans) {
644 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
645 return ERR_PTR(-EINVAL);
648 return &mbox->chans[chan];
651 static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox,
652 const struct of_phandle_args *sp)
656 if (sp->args_count != 2) {
657 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
658 return ERR_PTR(-EINVAL);
661 type = sp->args[0]; /* channel type */
662 idx = sp->args[1]; /* index */
663 chan = type * 4 + idx;
665 if (chan >= mbox->num_chans) {
666 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
667 return ERR_PTR(-EINVAL);
670 return &mbox->chans[chan];
673 static struct mbox_chan *imx_mu_seco_xlate(struct mbox_controller *mbox,
674 const struct of_phandle_args *sp)
678 if (sp->args_count < 1) {
679 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
680 return ERR_PTR(-EINVAL);
683 type = sp->args[0]; /* channel type */
685 /* Only supports TXDB and RXDB */
686 if (type == IMX_MU_TYPE_TX || type == IMX_MU_TYPE_RX) {
687 dev_err(mbox->dev, "Invalid type: %d\n", type);
688 return ERR_PTR(-EINVAL);
691 return imx_mu_xlate(mbox, sp);
694 static void imx_mu_init_generic(struct imx_mu_priv *priv)
698 for (i = 0; i < IMX_MU_CHANS; i++) {
699 struct imx_mu_con_priv *cp = &priv->con_priv[i];
703 cp->chan = &priv->mbox_chans[i];
704 priv->mbox_chans[i].con_priv = cp;
705 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
706 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
709 priv->mbox.num_chans = IMX_MU_CHANS;
710 priv->mbox.of_xlate = imx_mu_xlate;
715 /* Set default MU configuration */
716 for (i = 0; i < IMX_MU_xCR_MAX; i++)
717 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
720 static void imx_mu_init_specific(struct imx_mu_priv *priv)
723 int num_chans = priv->dcfg->type & IMX_MU_V2_S4 ? IMX_MU_S4_CHANS : IMX_MU_SCU_CHANS;
725 for (i = 0; i < num_chans; i++) {
726 struct imx_mu_con_priv *cp = &priv->con_priv[i];
728 cp->idx = i < 2 ? 0 : i - 2;
729 cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB;
730 cp->chan = &priv->mbox_chans[i];
731 priv->mbox_chans[i].con_priv = cp;
732 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
733 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
736 priv->mbox.num_chans = num_chans;
737 priv->mbox.of_xlate = imx_mu_specific_xlate;
739 /* Set default MU configuration */
740 for (i = 0; i < IMX_MU_xCR_MAX; i++)
741 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
744 static void imx_mu_init_seco(struct imx_mu_priv *priv)
746 imx_mu_init_generic(priv);
747 priv->mbox.of_xlate = imx_mu_seco_xlate;
750 static int imx_mu_probe(struct platform_device *pdev)
752 struct device *dev = &pdev->dev;
753 struct device_node *np = dev->of_node;
754 struct imx_mu_priv *priv;
755 const struct imx_mu_dcfg *dcfg;
759 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
765 priv->base = devm_platform_ioremap_resource(pdev, 0);
766 if (IS_ERR(priv->base))
767 return PTR_ERR(priv->base);
769 dcfg = of_device_get_match_data(dev);
773 if (priv->dcfg->type & IMX_MU_V2_IRQ) {
774 priv->irq[IMX_MU_TYPE_TX] = platform_get_irq_byname(pdev, "tx");
775 if (priv->irq[IMX_MU_TYPE_TX] < 0)
776 return priv->irq[IMX_MU_TYPE_TX];
777 priv->irq[IMX_MU_TYPE_RX] = platform_get_irq_byname(pdev, "rx");
778 if (priv->irq[IMX_MU_TYPE_RX] < 0)
779 return priv->irq[IMX_MU_TYPE_RX];
781 ret = platform_get_irq(pdev, 0);
785 for (i = 0; i < IMX_MU_CHANS; i++)
789 if (priv->dcfg->type & IMX_MU_V2_S4)
790 size = sizeof(struct imx_s4_rpc_msg_max);
792 size = sizeof(struct imx_sc_rpc_msg_max);
794 priv->msg = devm_kzalloc(dev, size, GFP_KERNEL);
798 priv->clk = devm_clk_get(dev, NULL);
799 if (IS_ERR(priv->clk)) {
800 if (PTR_ERR(priv->clk) != -ENOENT)
801 return PTR_ERR(priv->clk);
806 ret = clk_prepare_enable(priv->clk);
808 dev_err(dev, "Failed to enable clock\n");
812 priv->side_b = of_property_read_bool(np, "fsl,mu-side-b");
814 priv->dcfg->init(priv);
816 spin_lock_init(&priv->xcr_lock);
818 priv->mbox.dev = dev;
819 priv->mbox.ops = &imx_mu_ops;
820 priv->mbox.chans = priv->mbox_chans;
821 priv->mbox.txdone_irq = true;
823 platform_set_drvdata(pdev, priv);
825 ret = devm_mbox_controller_register(dev, &priv->mbox);
827 clk_disable_unprepare(priv->clk);
831 pm_runtime_enable(dev);
833 ret = pm_runtime_resume_and_get(dev);
835 goto disable_runtime_pm;
837 ret = pm_runtime_put_sync(dev);
839 goto disable_runtime_pm;
841 clk_disable_unprepare(priv->clk);
846 pm_runtime_disable(dev);
847 clk_disable_unprepare(priv->clk);
851 static int imx_mu_remove(struct platform_device *pdev)
853 struct imx_mu_priv *priv = platform_get_drvdata(pdev);
855 pm_runtime_disable(priv->dev);
860 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = {
861 .tx = imx_mu_generic_tx,
862 .rx = imx_mu_generic_rx,
863 .rxdb = imx_mu_generic_rxdb,
864 .init = imx_mu_init_generic,
867 .xSR = {0x20, 0x20, 0x20, 0x20},
868 .xCR = {0x24, 0x24, 0x24, 0x24},
871 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = {
872 .tx = imx_mu_generic_tx,
873 .rx = imx_mu_generic_rx,
874 .rxdb = imx_mu_generic_rxdb,
875 .init = imx_mu_init_generic,
878 .xSR = {0x60, 0x60, 0x60, 0x60},
879 .xCR = {0x64, 0x64, 0x64, 0x64},
882 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp = {
883 .tx = imx_mu_generic_tx,
884 .rx = imx_mu_generic_rx,
885 .rxdb = imx_mu_generic_rxdb,
886 .init = imx_mu_init_generic,
890 .xSR = {0xC, 0x118, 0x124, 0x12C},
891 .xCR = {0x110, 0x114, 0x120, 0x128},
894 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp_s4 = {
895 .tx = imx_mu_specific_tx,
896 .rx = imx_mu_specific_rx,
897 .init = imx_mu_init_specific,
898 .type = IMX_MU_V2 | IMX_MU_V2_S4,
901 .xSR = {0xC, 0x118, 0x124, 0x12C},
902 .xCR = {0x110, 0x114, 0x120, 0x128},
905 static const struct imx_mu_dcfg imx_mu_cfg_imx93_s4 = {
906 .tx = imx_mu_specific_tx,
907 .rx = imx_mu_specific_rx,
908 .init = imx_mu_init_specific,
909 .type = IMX_MU_V2 | IMX_MU_V2_S4 | IMX_MU_V2_IRQ,
912 .xSR = {0xC, 0x118, 0x124, 0x12C},
913 .xCR = {0x110, 0x114, 0x120, 0x128},
916 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = {
917 .tx = imx_mu_specific_tx,
918 .rx = imx_mu_specific_rx,
919 .init = imx_mu_init_specific,
920 .rxdb = imx_mu_generic_rxdb,
923 .xSR = {0x20, 0x20, 0x20, 0x20},
924 .xCR = {0x24, 0x24, 0x24, 0x24},
927 static const struct imx_mu_dcfg imx_mu_cfg_imx8_seco = {
928 .tx = imx_mu_seco_tx,
929 .rx = imx_mu_generic_rx,
930 .rxdb = imx_mu_seco_rxdb,
931 .init = imx_mu_init_seco,
934 .xSR = {0x20, 0x20, 0x20, 0x20},
935 .xCR = {0x24, 0x24, 0x24, 0x24},
938 static const struct of_device_id imx_mu_dt_ids[] = {
939 { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp },
940 { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx },
941 { .compatible = "fsl,imx8ulp-mu", .data = &imx_mu_cfg_imx8ulp },
942 { .compatible = "fsl,imx8ulp-mu-s4", .data = &imx_mu_cfg_imx8ulp_s4 },
943 { .compatible = "fsl,imx93-mu-s4", .data = &imx_mu_cfg_imx93_s4 },
944 { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu },
945 { .compatible = "fsl,imx8-mu-seco", .data = &imx_mu_cfg_imx8_seco },
948 MODULE_DEVICE_TABLE(of, imx_mu_dt_ids);
950 static int __maybe_unused imx_mu_suspend_noirq(struct device *dev)
952 struct imx_mu_priv *priv = dev_get_drvdata(dev);
956 for (i = 0; i < IMX_MU_xCR_MAX; i++)
957 priv->xcr[i] = imx_mu_read(priv, priv->dcfg->xCR[i]);
960 priv->suspend = true;
965 static int __maybe_unused imx_mu_resume_noirq(struct device *dev)
967 struct imx_mu_priv *priv = dev_get_drvdata(dev);
971 * ONLY restore MU when context lost, the TIE could
972 * be set during noirq resume as there is MU data
973 * communication going on, and restore the saved
974 * value will overwrite the TIE and cause MU data
975 * send failed, may lead to system freeze. This issue
976 * is observed by testing freeze mode suspend.
978 if (!priv->clk && !imx_mu_read(priv, priv->dcfg->xCR[0])) {
979 for (i = 0; i < IMX_MU_xCR_MAX; i++)
980 imx_mu_write(priv, priv->xcr[i], priv->dcfg->xCR[i]);
983 priv->suspend = false;
988 static int __maybe_unused imx_mu_runtime_suspend(struct device *dev)
990 struct imx_mu_priv *priv = dev_get_drvdata(dev);
992 clk_disable_unprepare(priv->clk);
997 static int __maybe_unused imx_mu_runtime_resume(struct device *dev)
999 struct imx_mu_priv *priv = dev_get_drvdata(dev);
1002 ret = clk_prepare_enable(priv->clk);
1004 dev_err(dev, "failed to enable clock\n");
1009 static const struct dev_pm_ops imx_mu_pm_ops = {
1010 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq,
1011 imx_mu_resume_noirq)
1012 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend,
1013 imx_mu_runtime_resume, NULL)
1016 static struct platform_driver imx_mu_driver = {
1017 .probe = imx_mu_probe,
1018 .remove = imx_mu_remove,
1021 .of_match_table = imx_mu_dt_ids,
1022 .pm = &imx_mu_pm_ops,
1025 module_platform_driver(imx_mu_driver);
1027 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
1028 MODULE_DESCRIPTION("Message Unit driver for i.MX");
1029 MODULE_LICENSE("GPL v2");