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 */
44 struct imx_sc_rpc_msg_max
{
45 struct imx_sc_rpc_msg hdr
;
49 struct imx_mu_con_priv
{
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
;
60 spinlock_t xcr_lock
; /* control register lock */
62 struct mbox_controller mbox
;
63 struct mbox_chan mbox_chans
[IMX_MU_CHANS
];
65 struct imx_mu_con_priv con_priv
[IMX_MU_CHANS
];
66 const struct imx_mu_dcfg
*dcfg
;
76 int (*tx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
, void *data
);
77 int (*rx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
);
78 void (*init
)(struct imx_mu_priv
*priv
);
79 u32 xTR
[4]; /* Transmit Registers */
80 u32 xRR
[4]; /* Receive Registers */
81 u32 xSR
; /* Status Register */
82 u32 xCR
; /* Control Register */
85 static struct imx_mu_priv
*to_imx_mu_priv(struct mbox_controller
*mbox
)
87 return container_of(mbox
, struct imx_mu_priv
, mbox
);
90 static void imx_mu_write(struct imx_mu_priv
*priv
, u32 val
, u32 offs
)
92 iowrite32(val
, priv
->base
+ offs
);
95 static u32
imx_mu_read(struct imx_mu_priv
*priv
, u32 offs
)
97 return ioread32(priv
->base
+ offs
);
100 static u32
imx_mu_xcr_rmw(struct imx_mu_priv
*priv
, u32 set
, u32 clr
)
105 spin_lock_irqsave(&priv
->xcr_lock
, flags
);
106 val
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
109 imx_mu_write(priv
, val
, priv
->dcfg
->xCR
);
110 spin_unlock_irqrestore(&priv
->xcr_lock
, flags
);
115 static int imx_mu_generic_tx(struct imx_mu_priv
*priv
,
116 struct imx_mu_con_priv
*cp
,
123 imx_mu_write(priv
, *arg
, priv
->dcfg
->xTR
[cp
->idx
]);
124 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_TIEn(cp
->idx
), 0);
126 case IMX_MU_TYPE_TXDB
:
127 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_GIRn(cp
->idx
), 0);
128 tasklet_schedule(&cp
->txdb_tasklet
);
131 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
138 static int imx_mu_generic_rx(struct imx_mu_priv
*priv
,
139 struct imx_mu_con_priv
*cp
)
143 dat
= imx_mu_read(priv
, priv
->dcfg
->xRR
[cp
->idx
]);
144 mbox_chan_received_data(cp
->chan
, (void *)&dat
);
149 static int imx_mu_scu_tx(struct imx_mu_priv
*priv
,
150 struct imx_mu_con_priv
*cp
,
153 struct imx_sc_rpc_msg_max
*msg
= data
;
161 * msg->hdr.size specifies the number of u32 words while
162 * sizeof yields bytes.
165 if (msg
->hdr
.size
> sizeof(*msg
) / 4) {
167 * The real message size can be different to
168 * struct imx_sc_rpc_msg_max size
170 dev_err(priv
->dev
, "Maximal message size (%zu bytes) exceeded on TX; got: %i bytes\n", sizeof(*msg
), msg
->hdr
.size
<< 2);
174 for (i
= 0; i
< 4 && i
< msg
->hdr
.size
; i
++)
175 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
[i
% 4]);
176 for (; i
< msg
->hdr
.size
; i
++) {
177 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
,
179 xsr
& IMX_MU_xSR_TEn(i
% 4),
182 dev_err(priv
->dev
, "Send data index: %d timeout\n", i
);
185 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
[i
% 4]);
188 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_TIEn(cp
->idx
), 0);
191 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
198 static int imx_mu_scu_rx(struct imx_mu_priv
*priv
,
199 struct imx_mu_con_priv
*cp
)
201 struct imx_sc_rpc_msg_max msg
;
202 u32
*data
= (u32
*)&msg
;
206 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_RIEn(0));
207 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
[0]);
209 if (msg
.hdr
.size
> sizeof(msg
) / 4) {
210 dev_err(priv
->dev
, "Maximal message size (%zu bytes) exceeded on RX; got: %i bytes\n", sizeof(msg
), msg
.hdr
.size
<< 2);
214 for (i
= 1; i
< msg
.hdr
.size
; i
++) {
215 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
, xsr
,
216 xsr
& IMX_MU_xSR_RFn(i
% 4), 0, 100);
218 dev_err(priv
->dev
, "timeout read idx %d\n", i
);
221 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
[i
% 4]);
224 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_RIEn(0), 0);
225 mbox_chan_received_data(cp
->chan
, (void *)&msg
);
230 static void imx_mu_txdb_tasklet(unsigned long data
)
232 struct imx_mu_con_priv
*cp
= (struct imx_mu_con_priv
*)data
;
234 mbox_chan_txdone(cp
->chan
, 0);
237 static irqreturn_t
imx_mu_isr(int irq
, void *p
)
239 struct mbox_chan
*chan
= p
;
240 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
241 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
244 ctrl
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
245 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
);
249 val
&= IMX_MU_xSR_TEn(cp
->idx
) &
250 (ctrl
& IMX_MU_xCR_TIEn(cp
->idx
));
253 val
&= IMX_MU_xSR_RFn(cp
->idx
) &
254 (ctrl
& IMX_MU_xCR_RIEn(cp
->idx
));
256 case IMX_MU_TYPE_RXDB
:
257 val
&= IMX_MU_xSR_GIPn(cp
->idx
) &
258 (ctrl
& IMX_MU_xCR_GIEn(cp
->idx
));
267 if (val
== IMX_MU_xSR_TEn(cp
->idx
)) {
268 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_TIEn(cp
->idx
));
269 mbox_chan_txdone(chan
, 0);
270 } else if (val
== IMX_MU_xSR_RFn(cp
->idx
)) {
271 priv
->dcfg
->rx(priv
, cp
);
272 } else if (val
== IMX_MU_xSR_GIPn(cp
->idx
)) {
273 imx_mu_write(priv
, IMX_MU_xSR_GIPn(cp
->idx
), priv
->dcfg
->xSR
);
274 mbox_chan_received_data(chan
, NULL
);
276 dev_warn_ratelimited(priv
->dev
, "Not handled interrupt\n");
283 static int imx_mu_send_data(struct mbox_chan
*chan
, void *data
)
285 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
286 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
288 return priv
->dcfg
->tx(priv
, cp
, data
);
291 static int imx_mu_startup(struct mbox_chan
*chan
)
293 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
294 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
295 unsigned long irq_flag
= IRQF_SHARED
;
298 pm_runtime_get_sync(priv
->dev
);
299 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
300 /* Tx doorbell don't have ACK support */
301 tasklet_init(&cp
->txdb_tasklet
, imx_mu_txdb_tasklet
,
306 /* IPC MU should be with IRQF_NO_SUSPEND set */
307 if (!priv
->dev
->pm_domain
)
308 irq_flag
|= IRQF_NO_SUSPEND
;
310 ret
= request_irq(priv
->irq
, imx_mu_isr
, irq_flag
,
314 "Unable to acquire IRQ %d\n", priv
->irq
);
320 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_RIEn(cp
->idx
), 0);
322 case IMX_MU_TYPE_RXDB
:
323 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_GIEn(cp
->idx
), 0);
332 static void imx_mu_shutdown(struct mbox_chan
*chan
)
334 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
335 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
337 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
338 tasklet_kill(&cp
->txdb_tasklet
);
339 pm_runtime_put_sync(priv
->dev
);
345 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_TIEn(cp
->idx
));
348 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_RIEn(cp
->idx
));
350 case IMX_MU_TYPE_RXDB
:
351 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_GIEn(cp
->idx
));
357 free_irq(priv
->irq
, chan
);
358 pm_runtime_put_sync(priv
->dev
);
361 static const struct mbox_chan_ops imx_mu_ops
= {
362 .send_data
= imx_mu_send_data
,
363 .startup
= imx_mu_startup
,
364 .shutdown
= imx_mu_shutdown
,
367 static struct mbox_chan
*imx_mu_scu_xlate(struct mbox_controller
*mbox
,
368 const struct of_phandle_args
*sp
)
372 if (sp
->args_count
!= 2) {
373 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
374 return ERR_PTR(-EINVAL
);
377 type
= sp
->args
[0]; /* channel type */
378 idx
= sp
->args
[1]; /* index */
384 dev_err(mbox
->dev
, "Invalid chan idx: %d\n", idx
);
387 case IMX_MU_TYPE_RXDB
:
391 dev_err(mbox
->dev
, "Invalid chan type: %d\n", type
);
392 return ERR_PTR(-EINVAL
);
395 if (chan
>= mbox
->num_chans
) {
396 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
397 return ERR_PTR(-EINVAL
);
400 return &mbox
->chans
[chan
];
403 static struct mbox_chan
* imx_mu_xlate(struct mbox_controller
*mbox
,
404 const struct of_phandle_args
*sp
)
408 if (sp
->args_count
!= 2) {
409 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
410 return ERR_PTR(-EINVAL
);
413 type
= sp
->args
[0]; /* channel type */
414 idx
= sp
->args
[1]; /* index */
415 chan
= type
* 4 + idx
;
417 if (chan
>= mbox
->num_chans
) {
418 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
419 return ERR_PTR(-EINVAL
);
422 return &mbox
->chans
[chan
];
425 static void imx_mu_init_generic(struct imx_mu_priv
*priv
)
429 for (i
= 0; i
< IMX_MU_CHANS
; i
++) {
430 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
434 cp
->chan
= &priv
->mbox_chans
[i
];
435 priv
->mbox_chans
[i
].con_priv
= cp
;
436 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
437 "imx_mu_chan[%i-%i]", cp
->type
, cp
->idx
);
440 priv
->mbox
.num_chans
= IMX_MU_CHANS
;
441 priv
->mbox
.of_xlate
= imx_mu_xlate
;
446 /* Set default MU configuration */
447 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
);
450 static void imx_mu_init_scu(struct imx_mu_priv
*priv
)
454 for (i
= 0; i
< IMX_MU_SCU_CHANS
; i
++) {
455 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
457 cp
->idx
= i
< 2 ? 0 : i
- 2;
458 cp
->type
= i
< 2 ? i
: IMX_MU_TYPE_RXDB
;
459 cp
->chan
= &priv
->mbox_chans
[i
];
460 priv
->mbox_chans
[i
].con_priv
= cp
;
461 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
462 "imx_mu_chan[%i-%i]", cp
->type
, cp
->idx
);
465 priv
->mbox
.num_chans
= IMX_MU_SCU_CHANS
;
466 priv
->mbox
.of_xlate
= imx_mu_scu_xlate
;
468 /* Set default MU configuration */
469 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
);
472 static int imx_mu_probe(struct platform_device
*pdev
)
474 struct device
*dev
= &pdev
->dev
;
475 struct device_node
*np
= dev
->of_node
;
476 struct imx_mu_priv
*priv
;
477 const struct imx_mu_dcfg
*dcfg
;
480 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
486 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
487 if (IS_ERR(priv
->base
))
488 return PTR_ERR(priv
->base
);
490 priv
->irq
= platform_get_irq(pdev
, 0);
494 dcfg
= of_device_get_match_data(dev
);
499 priv
->clk
= devm_clk_get(dev
, NULL
);
500 if (IS_ERR(priv
->clk
)) {
501 if (PTR_ERR(priv
->clk
) != -ENOENT
)
502 return PTR_ERR(priv
->clk
);
507 ret
= clk_prepare_enable(priv
->clk
);
509 dev_err(dev
, "Failed to enable clock\n");
513 priv
->side_b
= of_property_read_bool(np
, "fsl,mu-side-b");
515 priv
->dcfg
->init(priv
);
517 spin_lock_init(&priv
->xcr_lock
);
519 priv
->mbox
.dev
= dev
;
520 priv
->mbox
.ops
= &imx_mu_ops
;
521 priv
->mbox
.chans
= priv
->mbox_chans
;
522 priv
->mbox
.txdone_irq
= true;
524 platform_set_drvdata(pdev
, priv
);
526 ret
= devm_mbox_controller_register(dev
, &priv
->mbox
);
528 clk_disable_unprepare(priv
->clk
);
532 pm_runtime_enable(dev
);
534 ret
= pm_runtime_get_sync(dev
);
536 pm_runtime_put_noidle(dev
);
537 goto disable_runtime_pm
;
540 ret
= pm_runtime_put_sync(dev
);
542 goto disable_runtime_pm
;
544 clk_disable_unprepare(priv
->clk
);
549 pm_runtime_disable(dev
);
550 clk_disable_unprepare(priv
->clk
);
554 static int imx_mu_remove(struct platform_device
*pdev
)
556 struct imx_mu_priv
*priv
= platform_get_drvdata(pdev
);
558 pm_runtime_disable(priv
->dev
);
563 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx
= {
564 .tx
= imx_mu_generic_tx
,
565 .rx
= imx_mu_generic_rx
,
566 .init
= imx_mu_init_generic
,
567 .xTR
= {0x0, 0x4, 0x8, 0xc},
568 .xRR
= {0x10, 0x14, 0x18, 0x1c},
573 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp
= {
574 .tx
= imx_mu_generic_tx
,
575 .rx
= imx_mu_generic_rx
,
576 .init
= imx_mu_init_generic
,
577 .xTR
= {0x20, 0x24, 0x28, 0x2c},
578 .xRR
= {0x40, 0x44, 0x48, 0x4c},
583 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu
= {
586 .init
= imx_mu_init_scu
,
587 .xTR
= {0x0, 0x4, 0x8, 0xc},
588 .xRR
= {0x10, 0x14, 0x18, 0x1c},
593 static const struct of_device_id imx_mu_dt_ids
[] = {
594 { .compatible
= "fsl,imx7ulp-mu", .data
= &imx_mu_cfg_imx7ulp
},
595 { .compatible
= "fsl,imx6sx-mu", .data
= &imx_mu_cfg_imx6sx
},
596 { .compatible
= "fsl,imx8-mu-scu", .data
= &imx_mu_cfg_imx8_scu
},
599 MODULE_DEVICE_TABLE(of
, imx_mu_dt_ids
);
601 static int __maybe_unused
imx_mu_suspend_noirq(struct device
*dev
)
603 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
606 priv
->xcr
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
611 static int __maybe_unused
imx_mu_resume_noirq(struct device
*dev
)
613 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
616 * ONLY restore MU when context lost, the TIE could
617 * be set during noirq resume as there is MU data
618 * communication going on, and restore the saved
619 * value will overwrite the TIE and cause MU data
620 * send failed, may lead to system freeze. This issue
621 * is observed by testing freeze mode suspend.
623 if (!imx_mu_read(priv
, priv
->dcfg
->xCR
) && !priv
->clk
)
624 imx_mu_write(priv
, priv
->xcr
, priv
->dcfg
->xCR
);
629 static int __maybe_unused
imx_mu_runtime_suspend(struct device
*dev
)
631 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
633 clk_disable_unprepare(priv
->clk
);
638 static int __maybe_unused
imx_mu_runtime_resume(struct device
*dev
)
640 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
643 ret
= clk_prepare_enable(priv
->clk
);
645 dev_err(dev
, "failed to enable clock\n");
650 static const struct dev_pm_ops imx_mu_pm_ops
= {
651 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq
,
653 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend
,
654 imx_mu_runtime_resume
, NULL
)
657 static struct platform_driver imx_mu_driver
= {
658 .probe
= imx_mu_probe
,
659 .remove
= imx_mu_remove
,
662 .of_match_table
= imx_mu_dt_ids
,
663 .pm
= &imx_mu_pm_ops
,
666 module_platform_driver(imx_mu_driver
);
668 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
669 MODULE_DESCRIPTION("Message Unit driver for i.MX");
670 MODULE_LICENSE("GPL v2");