1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Pengutronix, Oleksij Rempel <o.rempel@pengutronix.de>
7 #include <linux/firmware/imx/ipc.h>
8 #include <linux/interrupt.h>
10 #include <linux/iopoll.h>
11 #include <linux/kernel.h>
12 #include <linux/mailbox_controller.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/suspend.h>
17 #include <linux/slab.h>
19 #define IMX_MU_CHANS 16
20 /* TX0/RX0/RXDB[0-3] */
21 #define IMX_MU_SCU_CHANS 6
22 #define IMX_MU_CHAN_NAME_SIZE 20
24 enum imx_mu_chan_type {
25 IMX_MU_TYPE_TX, /* Tx */
26 IMX_MU_TYPE_RX, /* Rx */
27 IMX_MU_TYPE_TXDB, /* Tx doorbell */
28 IMX_MU_TYPE_RXDB, /* Rx doorbell */
46 struct imx_sc_rpc_msg_max {
47 struct imx_sc_rpc_msg hdr;
51 struct imx_mu_con_priv {
53 char irq_desc[IMX_MU_CHAN_NAME_SIZE];
54 enum imx_mu_chan_type type;
55 struct mbox_chan *chan;
56 struct tasklet_struct txdb_tasklet;
62 spinlock_t xcr_lock; /* control register lock */
64 struct mbox_controller mbox;
65 struct mbox_chan mbox_chans[IMX_MU_CHANS];
67 struct imx_mu_con_priv con_priv[IMX_MU_CHANS];
68 const struct imx_mu_dcfg *dcfg;
84 int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data);
85 int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
86 void (*init)(struct imx_mu_priv *priv);
87 enum imx_mu_type type;
88 u32 xTR; /* Transmit Register0 */
89 u32 xRR; /* Receive Register0 */
90 u32 xSR[4]; /* Status Registers */
91 u32 xCR[4]; /* Control Registers */
94 #define IMX_MU_xSR_GIPn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
95 #define IMX_MU_xSR_RFn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
96 #define IMX_MU_xSR_TEn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
98 /* General Purpose Interrupt Enable */
99 #define IMX_MU_xCR_GIEn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
100 /* Receive Interrupt Enable */
101 #define IMX_MU_xCR_RIEn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
102 /* Transmit Interrupt Enable */
103 #define IMX_MU_xCR_TIEn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
104 /* General Purpose Interrupt Request */
105 #define IMX_MU_xCR_GIRn(type, x) (type == IMX_MU_V2 ? BIT(x) : BIT(16 + (3 - (x))))
108 static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox)
110 return container_of(mbox, struct imx_mu_priv, mbox);
113 static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs)
115 iowrite32(val, priv->base + offs);
118 static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs)
120 return ioread32(priv->base + offs);
123 static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, enum imx_mu_xcr type, u32 set, u32 clr)
128 spin_lock_irqsave(&priv->xcr_lock, flags);
129 val = imx_mu_read(priv, priv->dcfg->xCR[type]);
132 imx_mu_write(priv, val, priv->dcfg->xCR[type]);
133 spin_unlock_irqrestore(&priv->xcr_lock, flags);
138 static int imx_mu_generic_tx(struct imx_mu_priv *priv,
139 struct imx_mu_con_priv *cp,
146 imx_mu_write(priv, *arg, priv->dcfg->xTR + cp->idx * 4);
147 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
149 case IMX_MU_TYPE_TXDB:
150 imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIRn(priv->dcfg->type, cp->idx), 0);
151 tasklet_schedule(&cp->txdb_tasklet);
154 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
161 static int imx_mu_generic_rx(struct imx_mu_priv *priv,
162 struct imx_mu_con_priv *cp)
166 dat = imx_mu_read(priv, priv->dcfg->xRR + (cp->idx) * 4);
167 mbox_chan_received_data(cp->chan, (void *)&dat);
172 static int imx_mu_scu_tx(struct imx_mu_priv *priv,
173 struct imx_mu_con_priv *cp,
176 struct imx_sc_rpc_msg_max *msg = data;
184 * msg->hdr.size specifies the number of u32 words while
185 * sizeof yields bytes.
188 if (msg->hdr.size > sizeof(*msg) / 4) {
190 * The real message size can be different to
191 * struct imx_sc_rpc_msg_max size
193 dev_err(priv->dev, "Maximal message size (%zu bytes) exceeded on TX; got: %i bytes\n", sizeof(*msg), msg->hdr.size << 2);
197 for (i = 0; i < 4 && i < msg->hdr.size; i++)
198 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % 4) * 4);
199 for (; i < msg->hdr.size; i++) {
200 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_TSR],
202 xsr & IMX_MU_xSR_TEn(priv->dcfg->type, i % 4),
205 dev_err(priv->dev, "Send data index: %d timeout\n", i);
208 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % 4) * 4);
211 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx), 0);
214 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
221 static int imx_mu_scu_rx(struct imx_mu_priv *priv,
222 struct imx_mu_con_priv *cp)
224 struct imx_sc_rpc_msg_max msg;
225 u32 *data = (u32 *)&msg;
229 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, 0));
230 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
232 if (msg.hdr.size > sizeof(msg) / 4) {
233 dev_err(priv->dev, "Maximal message size (%zu bytes) exceeded on RX; got: %i bytes\n", sizeof(msg), msg.hdr.size << 2);
237 for (i = 1; i < msg.hdr.size; i++) {
238 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_RSR], xsr,
239 xsr & IMX_MU_xSR_RFn(priv->dcfg->type, i % 4), 0, 100);
241 dev_err(priv->dev, "timeout read idx %d\n", i);
244 *data++ = imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4);
247 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, 0), 0);
248 mbox_chan_received_data(cp->chan, (void *)&msg);
253 static void imx_mu_txdb_tasklet(unsigned long data)
255 struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data;
257 mbox_chan_txdone(cp->chan, 0);
260 static irqreturn_t imx_mu_isr(int irq, void *p)
262 struct mbox_chan *chan = p;
263 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
264 struct imx_mu_con_priv *cp = chan->con_priv;
269 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_TCR]);
270 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
271 val &= IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx) &
272 (ctrl & IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
275 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_RCR]);
276 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
277 val &= IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx) &
278 (ctrl & IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
280 case IMX_MU_TYPE_RXDB:
281 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_GIER]);
282 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]);
283 val &= IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx) &
284 (ctrl & IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
287 dev_warn_ratelimited(priv->dev, "Unhandled channel type %d\n",
295 if ((val == IMX_MU_xSR_TEn(priv->dcfg->type, cp->idx)) &&
296 (cp->type == IMX_MU_TYPE_TX)) {
297 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
298 mbox_chan_txdone(chan, 0);
299 } else if ((val == IMX_MU_xSR_RFn(priv->dcfg->type, cp->idx)) &&
300 (cp->type == IMX_MU_TYPE_RX)) {
301 priv->dcfg->rx(priv, cp);
302 } else if ((val == IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx)) &&
303 (cp->type == IMX_MU_TYPE_RXDB)) {
304 imx_mu_write(priv, IMX_MU_xSR_GIPn(priv->dcfg->type, cp->idx),
305 priv->dcfg->xSR[IMX_MU_GSR]);
306 mbox_chan_received_data(chan, NULL);
308 dev_warn_ratelimited(priv->dev, "Not handled interrupt\n");
318 static int imx_mu_send_data(struct mbox_chan *chan, void *data)
320 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
321 struct imx_mu_con_priv *cp = chan->con_priv;
323 return priv->dcfg->tx(priv, cp, data);
326 static int imx_mu_startup(struct mbox_chan *chan)
328 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
329 struct imx_mu_con_priv *cp = chan->con_priv;
330 unsigned long irq_flag = IRQF_SHARED;
333 pm_runtime_get_sync(priv->dev);
334 if (cp->type == IMX_MU_TYPE_TXDB) {
335 /* Tx doorbell don't have ACK support */
336 tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet,
341 /* IPC MU should be with IRQF_NO_SUSPEND set */
342 if (!priv->dev->pm_domain)
343 irq_flag |= IRQF_NO_SUSPEND;
345 ret = request_irq(priv->irq, imx_mu_isr, irq_flag,
349 "Unable to acquire IRQ %d\n", priv->irq);
355 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx), 0);
357 case IMX_MU_TYPE_RXDB:
358 imx_mu_xcr_rmw(priv, IMX_MU_GIER, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx), 0);
367 static void imx_mu_shutdown(struct mbox_chan *chan)
369 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
370 struct imx_mu_con_priv *cp = chan->con_priv;
372 if (cp->type == IMX_MU_TYPE_TXDB) {
373 tasklet_kill(&cp->txdb_tasklet);
374 pm_runtime_put_sync(priv->dev);
380 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(priv->dcfg->type, cp->idx));
383 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(priv->dcfg->type, cp->idx));
385 case IMX_MU_TYPE_RXDB:
386 imx_mu_xcr_rmw(priv, IMX_MU_GIER, 0, IMX_MU_xCR_GIEn(priv->dcfg->type, cp->idx));
392 free_irq(priv->irq, chan);
393 pm_runtime_put_sync(priv->dev);
396 static const struct mbox_chan_ops imx_mu_ops = {
397 .send_data = imx_mu_send_data,
398 .startup = imx_mu_startup,
399 .shutdown = imx_mu_shutdown,
402 static struct mbox_chan *imx_mu_scu_xlate(struct mbox_controller *mbox,
403 const struct of_phandle_args *sp)
407 if (sp->args_count != 2) {
408 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
409 return ERR_PTR(-EINVAL);
412 type = sp->args[0]; /* channel type */
413 idx = sp->args[1]; /* index */
419 dev_err(mbox->dev, "Invalid chan idx: %d\n", idx);
422 case IMX_MU_TYPE_RXDB:
426 dev_err(mbox->dev, "Invalid chan type: %d\n", type);
427 return ERR_PTR(-EINVAL);
430 if (chan >= mbox->num_chans) {
431 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
432 return ERR_PTR(-EINVAL);
435 return &mbox->chans[chan];
438 static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox,
439 const struct of_phandle_args *sp)
443 if (sp->args_count != 2) {
444 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
445 return ERR_PTR(-EINVAL);
448 type = sp->args[0]; /* channel type */
449 idx = sp->args[1]; /* index */
450 chan = type * 4 + idx;
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);
457 return &mbox->chans[chan];
460 static void imx_mu_init_generic(struct imx_mu_priv *priv)
464 for (i = 0; i < IMX_MU_CHANS; i++) {
465 struct imx_mu_con_priv *cp = &priv->con_priv[i];
469 cp->chan = &priv->mbox_chans[i];
470 priv->mbox_chans[i].con_priv = cp;
471 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
472 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
475 priv->mbox.num_chans = IMX_MU_CHANS;
476 priv->mbox.of_xlate = imx_mu_xlate;
481 /* Set default MU configuration */
482 for (i = 0; i < IMX_MU_xCR_MAX; i++)
483 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
486 static void imx_mu_init_scu(struct imx_mu_priv *priv)
490 for (i = 0; i < IMX_MU_SCU_CHANS; i++) {
491 struct imx_mu_con_priv *cp = &priv->con_priv[i];
493 cp->idx = i < 2 ? 0 : i - 2;
494 cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB;
495 cp->chan = &priv->mbox_chans[i];
496 priv->mbox_chans[i].con_priv = cp;
497 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
498 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
501 priv->mbox.num_chans = IMX_MU_SCU_CHANS;
502 priv->mbox.of_xlate = imx_mu_scu_xlate;
504 /* Set default MU configuration */
505 for (i = 0; i < IMX_MU_xCR_MAX; i++)
506 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
509 static int imx_mu_probe(struct platform_device *pdev)
511 struct device *dev = &pdev->dev;
512 struct device_node *np = dev->of_node;
513 struct imx_mu_priv *priv;
514 const struct imx_mu_dcfg *dcfg;
517 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
523 priv->base = devm_platform_ioremap_resource(pdev, 0);
524 if (IS_ERR(priv->base))
525 return PTR_ERR(priv->base);
527 priv->irq = platform_get_irq(pdev, 0);
531 dcfg = of_device_get_match_data(dev);
536 priv->clk = devm_clk_get(dev, NULL);
537 if (IS_ERR(priv->clk)) {
538 if (PTR_ERR(priv->clk) != -ENOENT)
539 return PTR_ERR(priv->clk);
544 ret = clk_prepare_enable(priv->clk);
546 dev_err(dev, "Failed to enable clock\n");
550 priv->side_b = of_property_read_bool(np, "fsl,mu-side-b");
552 priv->dcfg->init(priv);
554 spin_lock_init(&priv->xcr_lock);
556 priv->mbox.dev = dev;
557 priv->mbox.ops = &imx_mu_ops;
558 priv->mbox.chans = priv->mbox_chans;
559 priv->mbox.txdone_irq = true;
561 platform_set_drvdata(pdev, priv);
563 ret = devm_mbox_controller_register(dev, &priv->mbox);
565 clk_disable_unprepare(priv->clk);
569 pm_runtime_enable(dev);
571 ret = pm_runtime_get_sync(dev);
573 pm_runtime_put_noidle(dev);
574 goto disable_runtime_pm;
577 ret = pm_runtime_put_sync(dev);
579 goto disable_runtime_pm;
581 clk_disable_unprepare(priv->clk);
586 pm_runtime_disable(dev);
587 clk_disable_unprepare(priv->clk);
591 static int imx_mu_remove(struct platform_device *pdev)
593 struct imx_mu_priv *priv = platform_get_drvdata(pdev);
595 pm_runtime_disable(priv->dev);
600 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = {
601 .tx = imx_mu_generic_tx,
602 .rx = imx_mu_generic_rx,
603 .init = imx_mu_init_generic,
606 .xSR = {0x20, 0x20, 0x20, 0x20},
607 .xCR = {0x24, 0x24, 0x24, 0x24},
610 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = {
611 .tx = imx_mu_generic_tx,
612 .rx = imx_mu_generic_rx,
613 .init = imx_mu_init_generic,
616 .xSR = {0x60, 0x60, 0x60, 0x60},
617 .xCR = {0x64, 0x64, 0x64, 0x64},
620 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp = {
621 .tx = imx_mu_generic_tx,
622 .rx = imx_mu_generic_rx,
623 .init = imx_mu_init_generic,
627 .xSR = {0xC, 0x118, 0x124, 0x12C},
628 .xCR = {0x110, 0x114, 0x120, 0x128},
631 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = {
634 .init = imx_mu_init_scu,
637 .xSR = {0x20, 0x20, 0x20, 0x20},
638 .xCR = {0x24, 0x24, 0x24, 0x24},
641 static const struct of_device_id imx_mu_dt_ids[] = {
642 { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp },
643 { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx },
644 { .compatible = "fsl,imx8ulp-mu", .data = &imx_mu_cfg_imx8ulp },
645 { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu },
648 MODULE_DEVICE_TABLE(of, imx_mu_dt_ids);
650 static int __maybe_unused imx_mu_suspend_noirq(struct device *dev)
652 struct imx_mu_priv *priv = dev_get_drvdata(dev);
656 for (i = 0; i < IMX_MU_xCR_MAX; i++)
657 priv->xcr[i] = imx_mu_read(priv, priv->dcfg->xCR[i]);
660 priv->suspend = true;
665 static int __maybe_unused imx_mu_resume_noirq(struct device *dev)
667 struct imx_mu_priv *priv = dev_get_drvdata(dev);
671 * ONLY restore MU when context lost, the TIE could
672 * be set during noirq resume as there is MU data
673 * communication going on, and restore the saved
674 * value will overwrite the TIE and cause MU data
675 * send failed, may lead to system freeze. This issue
676 * is observed by testing freeze mode suspend.
678 if (!priv->clk && !imx_mu_read(priv, priv->dcfg->xCR[0])) {
679 for (i = 0; i < IMX_MU_xCR_MAX; i++)
680 imx_mu_write(priv, priv->xcr[i], priv->dcfg->xCR[i]);
683 priv->suspend = false;
688 static int __maybe_unused imx_mu_runtime_suspend(struct device *dev)
690 struct imx_mu_priv *priv = dev_get_drvdata(dev);
692 clk_disable_unprepare(priv->clk);
697 static int __maybe_unused imx_mu_runtime_resume(struct device *dev)
699 struct imx_mu_priv *priv = dev_get_drvdata(dev);
702 ret = clk_prepare_enable(priv->clk);
704 dev_err(dev, "failed to enable clock\n");
709 static const struct dev_pm_ops imx_mu_pm_ops = {
710 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq,
712 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend,
713 imx_mu_runtime_resume, NULL)
716 static struct platform_driver imx_mu_driver = {
717 .probe = imx_mu_probe,
718 .remove = imx_mu_remove,
721 .of_match_table = imx_mu_dt_ids,
722 .pm = &imx_mu_pm_ops,
725 module_platform_driver(imx_mu_driver);
727 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
728 MODULE_DESCRIPTION("Message Unit driver for i.MX");
729 MODULE_LICENSE("GPL v2");