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/slab.h>
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)
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)))
32 #define IMX_MU_CHANS 16
33 /* TX0/RX0/RXDB[0-3] */
34 #define IMX_MU_SCU_CHANS 6
35 #define IMX_MU_CHAN_NAME_SIZE 20
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 */
59 struct imx_sc_rpc_msg_max {
60 struct imx_sc_rpc_msg hdr;
64 struct imx_mu_con_priv {
66 char irq_desc[IMX_MU_CHAN_NAME_SIZE];
67 enum imx_mu_chan_type type;
68 struct mbox_chan *chan;
69 struct tasklet_struct txdb_tasklet;
75 spinlock_t xcr_lock; /* control register lock */
77 struct mbox_controller mbox;
78 struct mbox_chan mbox_chans[IMX_MU_CHANS];
80 struct imx_mu_con_priv con_priv[IMX_MU_CHANS];
81 const struct imx_mu_dcfg *dcfg;
91 int (*tx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp, void *data);
92 int (*rx)(struct imx_mu_priv *priv, struct imx_mu_con_priv *cp);
93 void (*init)(struct imx_mu_priv *priv);
94 u32 xTR; /* Transmit Register0 */
95 u32 xRR; /* Receive Register0 */
96 u32 xSR[4]; /* Status Registers */
97 u32 xCR[4]; /* Control Registers */
100 static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox)
102 return container_of(mbox, struct imx_mu_priv, mbox);
105 static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs)
107 iowrite32(val, priv->base + offs);
110 static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs)
112 return ioread32(priv->base + offs);
115 static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, enum imx_mu_xcr type, u32 set, u32 clr)
120 spin_lock_irqsave(&priv->xcr_lock, flags);
121 val = imx_mu_read(priv, priv->dcfg->xCR[type]);
124 imx_mu_write(priv, val, priv->dcfg->xCR[type]);
125 spin_unlock_irqrestore(&priv->xcr_lock, flags);
130 static int imx_mu_generic_tx(struct imx_mu_priv *priv,
131 struct imx_mu_con_priv *cp,
138 imx_mu_write(priv, *arg, priv->dcfg->xTR + cp->idx * 4);
139 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(cp->idx), 0);
141 case IMX_MU_TYPE_TXDB:
142 imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIRn(cp->idx), 0);
143 tasklet_schedule(&cp->txdb_tasklet);
146 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
153 static int imx_mu_generic_rx(struct imx_mu_priv *priv,
154 struct imx_mu_con_priv *cp)
158 dat = imx_mu_read(priv, priv->dcfg->xRR + (cp->idx) * 4);
159 mbox_chan_received_data(cp->chan, (void *)&dat);
164 static int imx_mu_scu_tx(struct imx_mu_priv *priv,
165 struct imx_mu_con_priv *cp,
168 struct imx_sc_rpc_msg_max *msg = data;
176 * msg->hdr.size specifies the number of u32 words while
177 * sizeof yields bytes.
180 if (msg->hdr.size > sizeof(*msg) / 4) {
182 * The real message size can be different to
183 * struct imx_sc_rpc_msg_max size
185 dev_err(priv->dev, "Maximal message size (%zu bytes) exceeded on TX; got: %i bytes\n", sizeof(*msg), msg->hdr.size << 2);
189 for (i = 0; i < 4 && i < msg->hdr.size; i++)
190 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % 4) * 4);
191 for (; i < msg->hdr.size; i++) {
192 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_TSR],
194 xsr & IMX_MU_xSR_TEn(i % 4),
197 dev_err(priv->dev, "Send data index: %d timeout\n", i);
200 imx_mu_write(priv, *arg++, priv->dcfg->xTR + (i % 4) * 4);
203 imx_mu_xcr_rmw(priv, IMX_MU_TCR, IMX_MU_xCR_TIEn(cp->idx), 0);
206 dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
213 static int imx_mu_scu_rx(struct imx_mu_priv *priv,
214 struct imx_mu_con_priv *cp)
216 struct imx_sc_rpc_msg_max msg;
217 u32 *data = (u32 *)&msg;
221 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(0));
222 *data++ = imx_mu_read(priv, priv->dcfg->xRR);
224 if (msg.hdr.size > sizeof(msg) / 4) {
225 dev_err(priv->dev, "Maximal message size (%zu bytes) exceeded on RX; got: %i bytes\n", sizeof(msg), msg.hdr.size << 2);
229 for (i = 1; i < msg.hdr.size; i++) {
230 ret = readl_poll_timeout(priv->base + priv->dcfg->xSR[IMX_MU_RSR], xsr,
231 xsr & IMX_MU_xSR_RFn(i % 4), 0, 100);
233 dev_err(priv->dev, "timeout read idx %d\n", i);
236 *data++ = imx_mu_read(priv, priv->dcfg->xRR + (i % 4) * 4);
239 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(0), 0);
240 mbox_chan_received_data(cp->chan, (void *)&msg);
245 static void imx_mu_txdb_tasklet(unsigned long data)
247 struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data;
249 mbox_chan_txdone(cp->chan, 0);
252 static irqreturn_t imx_mu_isr(int irq, void *p)
254 struct mbox_chan *chan = p;
255 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
256 struct imx_mu_con_priv *cp = chan->con_priv;
261 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_TCR]);
262 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_TSR]);
263 val &= IMX_MU_xSR_TEn(cp->idx) &
264 (ctrl & IMX_MU_xCR_TIEn(cp->idx));
267 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_RCR]);
268 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_RSR]);
269 val &= IMX_MU_xSR_RFn(cp->idx) &
270 (ctrl & IMX_MU_xCR_RIEn(cp->idx));
272 case IMX_MU_TYPE_RXDB:
273 ctrl = imx_mu_read(priv, priv->dcfg->xCR[IMX_MU_GCR]);
274 val = imx_mu_read(priv, priv->dcfg->xSR[IMX_MU_GSR]);
275 val &= IMX_MU_xSR_GIPn(cp->idx) &
276 (ctrl & IMX_MU_xCR_GIEn(cp->idx));
285 if (val == IMX_MU_xSR_TEn(cp->idx)) {
286 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(cp->idx));
287 mbox_chan_txdone(chan, 0);
288 } else if (val == IMX_MU_xSR_RFn(cp->idx)) {
289 priv->dcfg->rx(priv, cp);
290 } else if (val == IMX_MU_xSR_GIPn(cp->idx)) {
291 imx_mu_write(priv, IMX_MU_xSR_GIPn(cp->idx), priv->dcfg->xSR[IMX_MU_GSR]);
292 mbox_chan_received_data(chan, NULL);
294 dev_warn_ratelimited(priv->dev, "Not handled interrupt\n");
301 static int imx_mu_send_data(struct mbox_chan *chan, void *data)
303 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
304 struct imx_mu_con_priv *cp = chan->con_priv;
306 return priv->dcfg->tx(priv, cp, data);
309 static int imx_mu_startup(struct mbox_chan *chan)
311 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
312 struct imx_mu_con_priv *cp = chan->con_priv;
313 unsigned long irq_flag = IRQF_SHARED;
316 pm_runtime_get_sync(priv->dev);
317 if (cp->type == IMX_MU_TYPE_TXDB) {
318 /* Tx doorbell don't have ACK support */
319 tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet,
324 /* IPC MU should be with IRQF_NO_SUSPEND set */
325 if (!priv->dev->pm_domain)
326 irq_flag |= IRQF_NO_SUSPEND;
328 ret = request_irq(priv->irq, imx_mu_isr, irq_flag,
332 "Unable to acquire IRQ %d\n", priv->irq);
338 imx_mu_xcr_rmw(priv, IMX_MU_RCR, IMX_MU_xCR_RIEn(cp->idx), 0);
340 case IMX_MU_TYPE_RXDB:
341 imx_mu_xcr_rmw(priv, IMX_MU_GCR, IMX_MU_xCR_GIEn(cp->idx), 0);
350 static void imx_mu_shutdown(struct mbox_chan *chan)
352 struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
353 struct imx_mu_con_priv *cp = chan->con_priv;
355 if (cp->type == IMX_MU_TYPE_TXDB) {
356 tasklet_kill(&cp->txdb_tasklet);
357 pm_runtime_put_sync(priv->dev);
363 imx_mu_xcr_rmw(priv, IMX_MU_TCR, 0, IMX_MU_xCR_TIEn(cp->idx));
366 imx_mu_xcr_rmw(priv, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(cp->idx));
368 case IMX_MU_TYPE_RXDB:
369 imx_mu_xcr_rmw(priv, IMX_MU_GCR, 0, IMX_MU_xCR_GIEn(cp->idx));
375 free_irq(priv->irq, chan);
376 pm_runtime_put_sync(priv->dev);
379 static const struct mbox_chan_ops imx_mu_ops = {
380 .send_data = imx_mu_send_data,
381 .startup = imx_mu_startup,
382 .shutdown = imx_mu_shutdown,
385 static struct mbox_chan *imx_mu_scu_xlate(struct mbox_controller *mbox,
386 const struct of_phandle_args *sp)
390 if (sp->args_count != 2) {
391 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
392 return ERR_PTR(-EINVAL);
395 type = sp->args[0]; /* channel type */
396 idx = sp->args[1]; /* index */
402 dev_err(mbox->dev, "Invalid chan idx: %d\n", idx);
405 case IMX_MU_TYPE_RXDB:
409 dev_err(mbox->dev, "Invalid chan type: %d\n", type);
410 return ERR_PTR(-EINVAL);
413 if (chan >= mbox->num_chans) {
414 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
415 return ERR_PTR(-EINVAL);
418 return &mbox->chans[chan];
421 static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox,
422 const struct of_phandle_args *sp)
426 if (sp->args_count != 2) {
427 dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
428 return ERR_PTR(-EINVAL);
431 type = sp->args[0]; /* channel type */
432 idx = sp->args[1]; /* index */
433 chan = type * 4 + idx;
435 if (chan >= mbox->num_chans) {
436 dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
437 return ERR_PTR(-EINVAL);
440 return &mbox->chans[chan];
443 static void imx_mu_init_generic(struct imx_mu_priv *priv)
447 for (i = 0; i < IMX_MU_CHANS; i++) {
448 struct imx_mu_con_priv *cp = &priv->con_priv[i];
452 cp->chan = &priv->mbox_chans[i];
453 priv->mbox_chans[i].con_priv = cp;
454 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
455 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
458 priv->mbox.num_chans = IMX_MU_CHANS;
459 priv->mbox.of_xlate = imx_mu_xlate;
464 /* Set default MU configuration */
465 for (i = 0; i < IMX_MU_xCR_MAX; i++)
466 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
469 static void imx_mu_init_scu(struct imx_mu_priv *priv)
473 for (i = 0; i < IMX_MU_SCU_CHANS; i++) {
474 struct imx_mu_con_priv *cp = &priv->con_priv[i];
476 cp->idx = i < 2 ? 0 : i - 2;
477 cp->type = i < 2 ? i : IMX_MU_TYPE_RXDB;
478 cp->chan = &priv->mbox_chans[i];
479 priv->mbox_chans[i].con_priv = cp;
480 snprintf(cp->irq_desc, sizeof(cp->irq_desc),
481 "imx_mu_chan[%i-%i]", cp->type, cp->idx);
484 priv->mbox.num_chans = IMX_MU_SCU_CHANS;
485 priv->mbox.of_xlate = imx_mu_scu_xlate;
487 /* Set default MU configuration */
488 for (i = 0; i < IMX_MU_xCR_MAX; i++)
489 imx_mu_write(priv, 0, priv->dcfg->xCR[i]);
492 static int imx_mu_probe(struct platform_device *pdev)
494 struct device *dev = &pdev->dev;
495 struct device_node *np = dev->of_node;
496 struct imx_mu_priv *priv;
497 const struct imx_mu_dcfg *dcfg;
500 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
506 priv->base = devm_platform_ioremap_resource(pdev, 0);
507 if (IS_ERR(priv->base))
508 return PTR_ERR(priv->base);
510 priv->irq = platform_get_irq(pdev, 0);
514 dcfg = of_device_get_match_data(dev);
519 priv->clk = devm_clk_get(dev, NULL);
520 if (IS_ERR(priv->clk)) {
521 if (PTR_ERR(priv->clk) != -ENOENT)
522 return PTR_ERR(priv->clk);
527 ret = clk_prepare_enable(priv->clk);
529 dev_err(dev, "Failed to enable clock\n");
533 priv->side_b = of_property_read_bool(np, "fsl,mu-side-b");
535 priv->dcfg->init(priv);
537 spin_lock_init(&priv->xcr_lock);
539 priv->mbox.dev = dev;
540 priv->mbox.ops = &imx_mu_ops;
541 priv->mbox.chans = priv->mbox_chans;
542 priv->mbox.txdone_irq = true;
544 platform_set_drvdata(pdev, priv);
546 ret = devm_mbox_controller_register(dev, &priv->mbox);
548 clk_disable_unprepare(priv->clk);
552 pm_runtime_enable(dev);
554 ret = pm_runtime_get_sync(dev);
556 pm_runtime_put_noidle(dev);
557 goto disable_runtime_pm;
560 ret = pm_runtime_put_sync(dev);
562 goto disable_runtime_pm;
564 clk_disable_unprepare(priv->clk);
569 pm_runtime_disable(dev);
570 clk_disable_unprepare(priv->clk);
574 static int imx_mu_remove(struct platform_device *pdev)
576 struct imx_mu_priv *priv = platform_get_drvdata(pdev);
578 pm_runtime_disable(priv->dev);
583 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = {
584 .tx = imx_mu_generic_tx,
585 .rx = imx_mu_generic_rx,
586 .init = imx_mu_init_generic,
589 .xSR = {0x20, 0x20, 0x20, 0x20},
590 .xCR = {0x24, 0x24, 0x24, 0x24},
593 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = {
594 .tx = imx_mu_generic_tx,
595 .rx = imx_mu_generic_rx,
596 .init = imx_mu_init_generic,
599 .xSR = {0x60, 0x60, 0x60, 0x60},
600 .xCR = {0x64, 0x64, 0x64, 0x64},
603 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu = {
606 .init = imx_mu_init_scu,
609 .xSR = {0x20, 0x20, 0x20, 0x20},
610 .xCR = {0x24, 0x24, 0x24, 0x24},
613 static const struct of_device_id imx_mu_dt_ids[] = {
614 { .compatible = "fsl,imx7ulp-mu", .data = &imx_mu_cfg_imx7ulp },
615 { .compatible = "fsl,imx6sx-mu", .data = &imx_mu_cfg_imx6sx },
616 { .compatible = "fsl,imx8-mu-scu", .data = &imx_mu_cfg_imx8_scu },
619 MODULE_DEVICE_TABLE(of, imx_mu_dt_ids);
621 static int __maybe_unused imx_mu_suspend_noirq(struct device *dev)
623 struct imx_mu_priv *priv = dev_get_drvdata(dev);
627 for (i = 0; i < IMX_MU_xCR_MAX; i++)
628 priv->xcr[i] = imx_mu_read(priv, priv->dcfg->xCR[i]);
634 static int __maybe_unused imx_mu_resume_noirq(struct device *dev)
636 struct imx_mu_priv *priv = dev_get_drvdata(dev);
640 * ONLY restore MU when context lost, the TIE could
641 * be set during noirq resume as there is MU data
642 * communication going on, and restore the saved
643 * value will overwrite the TIE and cause MU data
644 * send failed, may lead to system freeze. This issue
645 * is observed by testing freeze mode suspend.
647 if (!imx_mu_read(priv, priv->dcfg->xCR[0]) && !priv->clk) {
648 for (i = 0; i < IMX_MU_xCR_MAX; i++)
649 imx_mu_write(priv, priv->xcr[i], priv->dcfg->xCR[i]);
655 static int __maybe_unused imx_mu_runtime_suspend(struct device *dev)
657 struct imx_mu_priv *priv = dev_get_drvdata(dev);
659 clk_disable_unprepare(priv->clk);
664 static int __maybe_unused imx_mu_runtime_resume(struct device *dev)
666 struct imx_mu_priv *priv = dev_get_drvdata(dev);
669 ret = clk_prepare_enable(priv->clk);
671 dev_err(dev, "failed to enable clock\n");
676 static const struct dev_pm_ops imx_mu_pm_ops = {
677 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq,
679 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend,
680 imx_mu_runtime_resume, NULL)
683 static struct platform_driver imx_mu_driver = {
684 .probe = imx_mu_probe,
685 .remove = imx_mu_remove,
688 .of_match_table = imx_mu_dt_ids,
689 .pm = &imx_mu_pm_ops,
692 module_platform_driver(imx_mu_driver);
694 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
695 MODULE_DESCRIPTION("Message Unit driver for i.MX");
696 MODULE_LICENSE("GPL v2");