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/slab.h>
17 #define IMX_MU_xSR_GIPn(x) BIT(28 + (3 - (x)))
18 #define IMX_MU_xSR_RFn(x) BIT(24 + (3 - (x)))
19 #define IMX_MU_xSR_TEn(x) BIT(20 + (3 - (x)))
20 #define IMX_MU_xSR_BRDIP BIT(9)
22 /* General Purpose Interrupt Enable */
23 #define IMX_MU_xCR_GIEn(x) BIT(28 + (3 - (x)))
24 /* Receive Interrupt Enable */
25 #define IMX_MU_xCR_RIEn(x) BIT(24 + (3 - (x)))
26 /* Transmit Interrupt Enable */
27 #define IMX_MU_xCR_TIEn(x) BIT(20 + (3 - (x)))
28 /* General Purpose Interrupt Request */
29 #define IMX_MU_xCR_GIRn(x) BIT(16 + (3 - (x)))
31 #define IMX_MU_CHANS 16
32 /* TX0/RX0/RXDB[0-3] */
33 #define IMX_MU_SCU_CHANS 6
34 #define IMX_MU_CHAN_NAME_SIZE 20
36 enum imx_mu_chan_type
{
37 IMX_MU_TYPE_TX
, /* Tx */
38 IMX_MU_TYPE_RX
, /* Rx */
39 IMX_MU_TYPE_TXDB
, /* Tx doorbell */
40 IMX_MU_TYPE_RXDB
, /* Rx doorbell */
43 struct imx_sc_rpc_msg_max
{
44 struct imx_sc_rpc_msg hdr
;
48 struct imx_mu_con_priv
{
50 char irq_desc
[IMX_MU_CHAN_NAME_SIZE
];
51 enum imx_mu_chan_type type
;
52 struct mbox_chan
*chan
;
53 struct tasklet_struct txdb_tasklet
;
59 spinlock_t xcr_lock
; /* control register lock */
61 struct mbox_controller mbox
;
62 struct mbox_chan mbox_chans
[IMX_MU_CHANS
];
64 struct imx_mu_con_priv con_priv
[IMX_MU_CHANS
];
65 const struct imx_mu_dcfg
*dcfg
;
75 int (*tx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
, void *data
);
76 int (*rx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
);
77 void (*init
)(struct imx_mu_priv
*priv
);
78 u32 xTR
[4]; /* Transmit Registers */
79 u32 xRR
[4]; /* Receive Registers */
80 u32 xSR
; /* Status Register */
81 u32 xCR
; /* Control Register */
84 static struct imx_mu_priv
*to_imx_mu_priv(struct mbox_controller
*mbox
)
86 return container_of(mbox
, struct imx_mu_priv
, mbox
);
89 static void imx_mu_write(struct imx_mu_priv
*priv
, u32 val
, u32 offs
)
91 iowrite32(val
, priv
->base
+ offs
);
94 static u32
imx_mu_read(struct imx_mu_priv
*priv
, u32 offs
)
96 return ioread32(priv
->base
+ offs
);
99 static u32
imx_mu_xcr_rmw(struct imx_mu_priv
*priv
, u32 set
, u32 clr
)
104 spin_lock_irqsave(&priv
->xcr_lock
, flags
);
105 val
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
108 imx_mu_write(priv
, val
, priv
->dcfg
->xCR
);
109 spin_unlock_irqrestore(&priv
->xcr_lock
, flags
);
114 static int imx_mu_generic_tx(struct imx_mu_priv
*priv
,
115 struct imx_mu_con_priv
*cp
,
122 imx_mu_write(priv
, *arg
, priv
->dcfg
->xTR
[cp
->idx
]);
123 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_TIEn(cp
->idx
), 0);
125 case IMX_MU_TYPE_TXDB
:
126 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_GIRn(cp
->idx
), 0);
127 tasklet_schedule(&cp
->txdb_tasklet
);
130 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
137 static int imx_mu_generic_rx(struct imx_mu_priv
*priv
,
138 struct imx_mu_con_priv
*cp
)
142 dat
= imx_mu_read(priv
, priv
->dcfg
->xRR
[cp
->idx
]);
143 mbox_chan_received_data(cp
->chan
, (void *)&dat
);
148 static int imx_mu_scu_tx(struct imx_mu_priv
*priv
,
149 struct imx_mu_con_priv
*cp
,
152 struct imx_sc_rpc_msg_max
*msg
= data
;
159 if (msg
->hdr
.size
> sizeof(*msg
)) {
161 * The real message size can be different to
162 * struct imx_sc_rpc_msg_max size
164 dev_err(priv
->dev
, "Exceed max msg size (%zu) on TX, got: %i\n", sizeof(*msg
), msg
->hdr
.size
);
168 for (i
= 0; i
< 4 && i
< msg
->hdr
.size
; i
++)
169 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
[i
% 4]);
170 for (; i
< msg
->hdr
.size
; i
++) {
171 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
,
173 xsr
& IMX_MU_xSR_TEn(i
% 4),
176 dev_err(priv
->dev
, "Send data index: %d timeout\n", i
);
179 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
[i
% 4]);
182 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_TIEn(cp
->idx
), 0);
185 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
192 static int imx_mu_scu_rx(struct imx_mu_priv
*priv
,
193 struct imx_mu_con_priv
*cp
)
195 struct imx_sc_rpc_msg_max msg
;
196 u32
*data
= (u32
*)&msg
;
200 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_RIEn(0));
201 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
[0]);
203 if (msg
.hdr
.size
> sizeof(msg
)) {
204 dev_err(priv
->dev
, "Exceed max msg size (%zu) on RX, got: %i\n",
205 sizeof(msg
), msg
.hdr
.size
);
209 for (i
= 1; i
< msg
.hdr
.size
; i
++) {
210 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
, xsr
,
211 xsr
& IMX_MU_xSR_RFn(i
% 4), 0, 100);
213 dev_err(priv
->dev
, "timeout read idx %d\n", i
);
216 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
[i
% 4]);
219 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_RIEn(0), 0);
220 mbox_chan_received_data(cp
->chan
, (void *)&msg
);
225 static void imx_mu_txdb_tasklet(unsigned long data
)
227 struct imx_mu_con_priv
*cp
= (struct imx_mu_con_priv
*)data
;
229 mbox_chan_txdone(cp
->chan
, 0);
232 static irqreturn_t
imx_mu_isr(int irq
, void *p
)
234 struct mbox_chan
*chan
= p
;
235 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
236 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
239 ctrl
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
240 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
);
244 val
&= IMX_MU_xSR_TEn(cp
->idx
) &
245 (ctrl
& IMX_MU_xCR_TIEn(cp
->idx
));
248 val
&= IMX_MU_xSR_RFn(cp
->idx
) &
249 (ctrl
& IMX_MU_xCR_RIEn(cp
->idx
));
251 case IMX_MU_TYPE_RXDB
:
252 val
&= IMX_MU_xSR_GIPn(cp
->idx
) &
253 (ctrl
& IMX_MU_xCR_GIEn(cp
->idx
));
262 if (val
== IMX_MU_xSR_TEn(cp
->idx
)) {
263 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_TIEn(cp
->idx
));
264 mbox_chan_txdone(chan
, 0);
265 } else if (val
== IMX_MU_xSR_RFn(cp
->idx
)) {
266 priv
->dcfg
->rx(priv
, cp
);
267 } else if (val
== IMX_MU_xSR_GIPn(cp
->idx
)) {
268 imx_mu_write(priv
, IMX_MU_xSR_GIPn(cp
->idx
), priv
->dcfg
->xSR
);
269 mbox_chan_received_data(chan
, NULL
);
271 dev_warn_ratelimited(priv
->dev
, "Not handled interrupt\n");
278 static int imx_mu_send_data(struct mbox_chan
*chan
, void *data
)
280 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
281 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
283 return priv
->dcfg
->tx(priv
, cp
, data
);
286 static int imx_mu_startup(struct mbox_chan
*chan
)
288 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
289 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
292 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
293 /* Tx doorbell don't have ACK support */
294 tasklet_init(&cp
->txdb_tasklet
, imx_mu_txdb_tasklet
,
299 ret
= request_irq(priv
->irq
, imx_mu_isr
, IRQF_SHARED
|
300 IRQF_NO_SUSPEND
, cp
->irq_desc
, chan
);
303 "Unable to acquire IRQ %d\n", priv
->irq
);
309 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_RIEn(cp
->idx
), 0);
311 case IMX_MU_TYPE_RXDB
:
312 imx_mu_xcr_rmw(priv
, IMX_MU_xCR_GIEn(cp
->idx
), 0);
321 static void imx_mu_shutdown(struct mbox_chan
*chan
)
323 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
324 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
326 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
327 tasklet_kill(&cp
->txdb_tasklet
);
333 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_TIEn(cp
->idx
));
336 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_RIEn(cp
->idx
));
338 case IMX_MU_TYPE_RXDB
:
339 imx_mu_xcr_rmw(priv
, 0, IMX_MU_xCR_GIEn(cp
->idx
));
345 free_irq(priv
->irq
, chan
);
348 static const struct mbox_chan_ops imx_mu_ops
= {
349 .send_data
= imx_mu_send_data
,
350 .startup
= imx_mu_startup
,
351 .shutdown
= imx_mu_shutdown
,
354 static struct mbox_chan
*imx_mu_scu_xlate(struct mbox_controller
*mbox
,
355 const struct of_phandle_args
*sp
)
359 if (sp
->args_count
!= 2) {
360 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
361 return ERR_PTR(-EINVAL
);
364 type
= sp
->args
[0]; /* channel type */
365 idx
= sp
->args
[1]; /* index */
371 dev_err(mbox
->dev
, "Invalid chan idx: %d\n", idx
);
374 case IMX_MU_TYPE_RXDB
:
378 dev_err(mbox
->dev
, "Invalid chan type: %d\n", type
);
379 return ERR_PTR(-EINVAL
);
382 if (chan
>= mbox
->num_chans
) {
383 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
384 return ERR_PTR(-EINVAL
);
387 return &mbox
->chans
[chan
];
390 static struct mbox_chan
* imx_mu_xlate(struct mbox_controller
*mbox
,
391 const struct of_phandle_args
*sp
)
395 if (sp
->args_count
!= 2) {
396 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
397 return ERR_PTR(-EINVAL
);
400 type
= sp
->args
[0]; /* channel type */
401 idx
= sp
->args
[1]; /* index */
402 chan
= type
* 4 + idx
;
404 if (chan
>= mbox
->num_chans
) {
405 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
406 return ERR_PTR(-EINVAL
);
409 return &mbox
->chans
[chan
];
412 static void imx_mu_init_generic(struct imx_mu_priv
*priv
)
416 for (i
= 0; i
< IMX_MU_CHANS
; i
++) {
417 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
421 cp
->chan
= &priv
->mbox_chans
[i
];
422 priv
->mbox_chans
[i
].con_priv
= cp
;
423 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
424 "imx_mu_chan[%i-%i]", cp
->type
, cp
->idx
);
427 priv
->mbox
.num_chans
= IMX_MU_CHANS
;
428 priv
->mbox
.of_xlate
= imx_mu_xlate
;
433 /* Set default MU configuration */
434 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
);
437 static void imx_mu_init_scu(struct imx_mu_priv
*priv
)
441 for (i
= 0; i
< IMX_MU_SCU_CHANS
; i
++) {
442 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
444 cp
->idx
= i
< 2 ? 0 : i
- 2;
445 cp
->type
= i
< 2 ? i
: IMX_MU_TYPE_RXDB
;
446 cp
->chan
= &priv
->mbox_chans
[i
];
447 priv
->mbox_chans
[i
].con_priv
= cp
;
448 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
449 "imx_mu_chan[%i-%i]", cp
->type
, cp
->idx
);
452 priv
->mbox
.num_chans
= IMX_MU_SCU_CHANS
;
453 priv
->mbox
.of_xlate
= imx_mu_scu_xlate
;
455 /* Set default MU configuration */
456 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
);
459 static int imx_mu_probe(struct platform_device
*pdev
)
461 struct device
*dev
= &pdev
->dev
;
462 struct device_node
*np
= dev
->of_node
;
463 struct imx_mu_priv
*priv
;
464 const struct imx_mu_dcfg
*dcfg
;
467 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
473 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
474 if (IS_ERR(priv
->base
))
475 return PTR_ERR(priv
->base
);
477 priv
->irq
= platform_get_irq(pdev
, 0);
481 dcfg
= of_device_get_match_data(dev
);
486 priv
->clk
= devm_clk_get(dev
, NULL
);
487 if (IS_ERR(priv
->clk
)) {
488 if (PTR_ERR(priv
->clk
) != -ENOENT
)
489 return PTR_ERR(priv
->clk
);
494 ret
= clk_prepare_enable(priv
->clk
);
496 dev_err(dev
, "Failed to enable clock\n");
500 priv
->side_b
= of_property_read_bool(np
, "fsl,mu-side-b");
502 priv
->dcfg
->init(priv
);
504 spin_lock_init(&priv
->xcr_lock
);
506 priv
->mbox
.dev
= dev
;
507 priv
->mbox
.ops
= &imx_mu_ops
;
508 priv
->mbox
.chans
= priv
->mbox_chans
;
509 priv
->mbox
.txdone_irq
= true;
511 platform_set_drvdata(pdev
, priv
);
513 return devm_mbox_controller_register(dev
, &priv
->mbox
);
516 static int imx_mu_remove(struct platform_device
*pdev
)
518 struct imx_mu_priv
*priv
= platform_get_drvdata(pdev
);
520 clk_disable_unprepare(priv
->clk
);
525 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx
= {
526 .tx
= imx_mu_generic_tx
,
527 .rx
= imx_mu_generic_rx
,
528 .init
= imx_mu_init_generic
,
529 .xTR
= {0x0, 0x4, 0x8, 0xc},
530 .xRR
= {0x10, 0x14, 0x18, 0x1c},
535 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp
= {
536 .tx
= imx_mu_generic_tx
,
537 .rx
= imx_mu_generic_rx
,
538 .init
= imx_mu_init_generic
,
539 .xTR
= {0x20, 0x24, 0x28, 0x2c},
540 .xRR
= {0x40, 0x44, 0x48, 0x4c},
545 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu
= {
548 .init
= imx_mu_init_scu
,
549 .xTR
= {0x0, 0x4, 0x8, 0xc},
550 .xRR
= {0x10, 0x14, 0x18, 0x1c},
555 static const struct of_device_id imx_mu_dt_ids
[] = {
556 { .compatible
= "fsl,imx7ulp-mu", .data
= &imx_mu_cfg_imx7ulp
},
557 { .compatible
= "fsl,imx6sx-mu", .data
= &imx_mu_cfg_imx6sx
},
558 { .compatible
= "fsl,imx8-mu-scu", .data
= &imx_mu_cfg_imx8_scu
},
561 MODULE_DEVICE_TABLE(of
, imx_mu_dt_ids
);
563 static int imx_mu_suspend_noirq(struct device
*dev
)
565 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
567 priv
->xcr
= imx_mu_read(priv
, priv
->dcfg
->xCR
);
572 static int imx_mu_resume_noirq(struct device
*dev
)
574 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
577 * ONLY restore MU when context lost, the TIE could
578 * be set during noirq resume as there is MU data
579 * communication going on, and restore the saved
580 * value will overwrite the TIE and cause MU data
581 * send failed, may lead to system freeze. This issue
582 * is observed by testing freeze mode suspend.
584 if (!imx_mu_read(priv
, priv
->dcfg
->xCR
))
585 imx_mu_write(priv
, priv
->xcr
, priv
->dcfg
->xCR
);
590 static const struct dev_pm_ops imx_mu_pm_ops
= {
591 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq
,
595 static struct platform_driver imx_mu_driver
= {
596 .probe
= imx_mu_probe
,
597 .remove
= imx_mu_remove
,
600 .of_match_table
= imx_mu_dt_ids
,
601 .pm
= &imx_mu_pm_ops
,
604 module_platform_driver(imx_mu_driver
);
606 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
607 MODULE_DESCRIPTION("Message Unit driver for i.MX");
608 MODULE_LICENSE("GPL v2");