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>
7 #include <linux/bitfield.h>
9 #include <linux/firmware/imx/ipc.h>
10 #include <linux/firmware/imx/s4.h>
11 #include <linux/interrupt.h>
13 #include <linux/iopoll.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/mailbox_controller.h>
17 #include <linux/module.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/suspend.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
28 #define IMX_MU_CHANS 24
29 /* TX0/RX0/RXDB[0-3] */
30 #define IMX_MU_SCU_CHANS 6
32 #define IMX_MU_S4_CHANS 2
33 #define IMX_MU_CHAN_NAME_SIZE 32
35 #define IMX_MU_V2_PAR_OFF 0x4
36 #define IMX_MU_V2_TR_MASK GENMASK(7, 0)
37 #define IMX_MU_V2_RR_MASK GENMASK(15, 8)
39 #define IMX_MU_SECO_TX_TOUT (msecs_to_jiffies(3000))
40 #define IMX_MU_SECO_RX_TOUT (msecs_to_jiffies(3000))
42 /* Please not change TX & RX */
43 enum imx_mu_chan_type
{
44 IMX_MU_TYPE_TX
= 0, /* Tx */
45 IMX_MU_TYPE_RX
= 1, /* Rx */
46 IMX_MU_TYPE_TXDB
= 2, /* Tx doorbell */
47 IMX_MU_TYPE_RXDB
= 3, /* Rx doorbell */
48 IMX_MU_TYPE_RST
= 4, /* Reset */
49 IMX_MU_TYPE_TXDB_V2
= 5, /* Tx doorbell with S/W ACK */
69 struct imx_sc_rpc_msg_max
{
70 struct imx_sc_rpc_msg hdr
;
74 struct imx_s4_rpc_msg_max
{
75 struct imx_s4_rpc_msg hdr
;
79 struct imx_mu_con_priv
{
81 char irq_desc
[IMX_MU_CHAN_NAME_SIZE
];
82 enum imx_mu_chan_type type
;
83 struct mbox_chan
*chan
;
84 struct work_struct txdb_work
;
91 spinlock_t xcr_lock
; /* control register lock */
93 struct mbox_controller mbox
;
94 struct mbox_chan mbox_chans
[IMX_MU_CHANS
];
96 struct imx_mu_con_priv con_priv
[IMX_MU_CHANS
];
97 const struct imx_mu_dcfg
*dcfg
;
99 int irq
[IMX_MU_CHANS
];
103 u32 xcr
[IMX_MU_xCR_MAX
];
111 IMX_MU_V2_S4
= BIT(15),
112 IMX_MU_V2_IRQ
= BIT(16),
116 int (*tx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
, void *data
);
117 int (*rx
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
);
118 int (*rxdb
)(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
);
119 int (*init
)(struct imx_mu_priv
*priv
);
120 enum imx_mu_type type
;
121 u32 xTR
; /* Transmit Register0 */
122 u32 xRR
; /* Receive Register0 */
123 u32 xSR
[IMX_MU_xSR_MAX
]; /* Status Registers */
124 u32 xCR
[IMX_MU_xCR_MAX
]; /* Control Registers */
127 #define IMX_MU_xSR_GIPn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
128 #define IMX_MU_xSR_RFn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
129 #define IMX_MU_xSR_TEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
131 /* General Purpose Interrupt Enable */
132 #define IMX_MU_xCR_GIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(28 + (3 - (x))))
133 /* Receive Interrupt Enable */
134 #define IMX_MU_xCR_RIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
135 /* Transmit Interrupt Enable */
136 #define IMX_MU_xCR_TIEn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(20 + (3 - (x))))
137 /* General Purpose Interrupt Request */
138 #define IMX_MU_xCR_GIRn(type, x) (type & IMX_MU_V2 ? BIT(x) : BIT(16 + (3 - (x))))
140 #define IMX_MU_xCR_RST(type) (type & IMX_MU_V2 ? BIT(0) : BIT(5))
141 #define IMX_MU_xSR_RST(type) (type & IMX_MU_V2 ? BIT(0) : BIT(7))
144 static struct imx_mu_priv
*to_imx_mu_priv(struct mbox_controller
*mbox
)
146 return container_of(mbox
, struct imx_mu_priv
, mbox
);
149 static void imx_mu_write(struct imx_mu_priv
*priv
, u32 val
, u32 offs
)
151 iowrite32(val
, priv
->base
+ offs
);
154 static u32
imx_mu_read(struct imx_mu_priv
*priv
, u32 offs
)
156 return ioread32(priv
->base
+ offs
);
159 static int imx_mu_tx_waiting_write(struct imx_mu_priv
*priv
, u32 val
, u32 idx
)
161 u64 timeout_time
= get_jiffies_64() + IMX_MU_SECO_TX_TOUT
;
165 dev_dbg(priv
->dev
, "Trying to write %.8x to idx %d\n", val
, idx
);
168 status
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_TSR
]);
169 can_write
= status
& IMX_MU_xSR_TEn(priv
->dcfg
->type
, idx
% 4);
170 } while (!can_write
&& time_is_after_jiffies64(timeout_time
));
173 dev_err(priv
->dev
, "timeout trying to write %.8x at %d(%.8x)\n",
178 imx_mu_write(priv
, val
, priv
->dcfg
->xTR
+ (idx
% 4) * 4);
183 static int imx_mu_rx_waiting_read(struct imx_mu_priv
*priv
, u32
*val
, u32 idx
)
185 u64 timeout_time
= get_jiffies_64() + IMX_MU_SECO_RX_TOUT
;
189 dev_dbg(priv
->dev
, "Trying to read from idx %d\n", idx
);
192 status
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_RSR
]);
193 can_read
= status
& IMX_MU_xSR_RFn(priv
->dcfg
->type
, idx
% 4);
194 } while (!can_read
&& time_is_after_jiffies64(timeout_time
));
197 dev_err(priv
->dev
, "timeout trying to read idx %d (%.8x)\n",
202 *val
= imx_mu_read(priv
, priv
->dcfg
->xRR
+ (idx
% 4) * 4);
203 dev_dbg(priv
->dev
, "Read %.8x\n", *val
);
208 static u32
imx_mu_xcr_rmw(struct imx_mu_priv
*priv
, enum imx_mu_xcr type
, u32 set
, u32 clr
)
213 spin_lock_irqsave(&priv
->xcr_lock
, flags
);
214 val
= imx_mu_read(priv
, priv
->dcfg
->xCR
[type
]);
217 imx_mu_write(priv
, val
, priv
->dcfg
->xCR
[type
]);
218 spin_unlock_irqrestore(&priv
->xcr_lock
, flags
);
223 static int imx_mu_generic_tx(struct imx_mu_priv
*priv
,
224 struct imx_mu_con_priv
*cp
,
233 imx_mu_write(priv
, *arg
, priv
->dcfg
->xTR
+ cp
->idx
* 4);
234 imx_mu_xcr_rmw(priv
, IMX_MU_TCR
, IMX_MU_xCR_TIEn(priv
->dcfg
->type
, cp
->idx
), 0);
236 case IMX_MU_TYPE_TXDB
:
237 imx_mu_xcr_rmw(priv
, IMX_MU_GCR
, IMX_MU_xCR_GIRn(priv
->dcfg
->type
, cp
->idx
), 0);
238 queue_work(system_bh_wq
, &cp
->txdb_work
);
240 case IMX_MU_TYPE_TXDB_V2
:
241 imx_mu_write(priv
, IMX_MU_xCR_GIRn(priv
->dcfg
->type
, cp
->idx
),
242 priv
->dcfg
->xCR
[IMX_MU_GCR
]);
243 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xCR
[IMX_MU_GCR
], val
,
244 !(val
& IMX_MU_xCR_GIRn(priv
->dcfg
->type
, cp
->idx
)),
247 dev_warn_ratelimited(priv
->dev
, "channel type: %d failure\n", cp
->type
);
250 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
257 static int imx_mu_generic_rx(struct imx_mu_priv
*priv
,
258 struct imx_mu_con_priv
*cp
)
262 dat
= imx_mu_read(priv
, priv
->dcfg
->xRR
+ (cp
->idx
) * 4);
263 mbox_chan_received_data(cp
->chan
, (void *)&dat
);
268 static int imx_mu_generic_rxdb(struct imx_mu_priv
*priv
,
269 struct imx_mu_con_priv
*cp
)
271 imx_mu_write(priv
, IMX_MU_xSR_GIPn(priv
->dcfg
->type
, cp
->idx
),
272 priv
->dcfg
->xSR
[IMX_MU_GSR
]);
273 mbox_chan_received_data(cp
->chan
, NULL
);
278 static int imx_mu_specific_tx(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
, void *data
)
281 u32 num_tr
= priv
->num_tr
;
286 if (priv
->dcfg
->type
& IMX_MU_V2_S4
) {
287 size
= ((struct imx_s4_rpc_msg_max
*)data
)->hdr
.size
;
288 max_size
= sizeof(struct imx_s4_rpc_msg_max
);
290 size
= ((struct imx_sc_rpc_msg_max
*)data
)->hdr
.size
;
291 max_size
= sizeof(struct imx_sc_rpc_msg_max
);
297 * msg->hdr.size specifies the number of u32 words while
298 * sizeof yields bytes.
301 if (size
> max_size
/ 4) {
303 * The real message size can be different to
304 * struct imx_sc_rpc_msg_max/imx_s4_rpc_msg_max size
306 dev_err(priv
->dev
, "Maximal message size (%u bytes) exceeded on TX; got: %i bytes\n", max_size
, size
<< 2);
310 for (i
= 0; i
< num_tr
&& i
< size
; i
++)
311 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
+ (i
% num_tr
) * 4);
312 for (; i
< size
; i
++) {
313 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
[IMX_MU_TSR
],
315 xsr
& IMX_MU_xSR_TEn(priv
->dcfg
->type
, i
% num_tr
),
316 0, 5 * USEC_PER_SEC
);
318 dev_err(priv
->dev
, "Send data index: %d timeout\n", i
);
321 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
+ (i
% num_tr
) * 4);
324 imx_mu_xcr_rmw(priv
, IMX_MU_TCR
, IMX_MU_xCR_TIEn(priv
->dcfg
->type
, cp
->idx
), 0);
327 dev_warn_ratelimited(priv
->dev
, "Send data on wrong channel type: %d\n", cp
->type
);
334 static int imx_mu_specific_rx(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
)
340 u32 num_rr
= priv
->num_rr
;
342 data
= (u32
*)priv
->msg
;
344 imx_mu_xcr_rmw(priv
, IMX_MU_RCR
, 0, IMX_MU_xCR_RIEn(priv
->dcfg
->type
, 0));
345 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
);
347 if (priv
->dcfg
->type
& IMX_MU_V2_S4
) {
348 size
= ((struct imx_s4_rpc_msg_max
*)priv
->msg
)->hdr
.size
;
349 max_size
= sizeof(struct imx_s4_rpc_msg_max
);
351 size
= ((struct imx_sc_rpc_msg_max
*)priv
->msg
)->hdr
.size
;
352 max_size
= sizeof(struct imx_sc_rpc_msg_max
);
355 if (size
> max_size
/ 4) {
356 dev_err(priv
->dev
, "Maximal message size (%u bytes) exceeded on RX; got: %i bytes\n", max_size
, size
<< 2);
360 for (i
= 1; i
< size
; i
++) {
361 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
[IMX_MU_RSR
], xsr
,
362 xsr
& IMX_MU_xSR_RFn(priv
->dcfg
->type
, i
% num_rr
), 0,
365 dev_err(priv
->dev
, "timeout read idx %d\n", i
);
368 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
+ (i
% num_rr
) * 4);
371 imx_mu_xcr_rmw(priv
, IMX_MU_RCR
, IMX_MU_xCR_RIEn(priv
->dcfg
->type
, 0), 0);
372 mbox_chan_received_data(cp
->chan
, (void *)priv
->msg
);
377 static int imx_mu_seco_tx(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
,
380 struct imx_sc_rpc_msg_max
*msg
= data
;
386 dev_dbg(priv
->dev
, "Sending message\n");
389 case IMX_MU_TYPE_TXDB
:
390 byte_size
= msg
->hdr
.size
* sizeof(u32
);
391 if (byte_size
> sizeof(*msg
)) {
393 * The real message size can be different to
394 * struct imx_sc_rpc_msg_max size
397 "Exceed max msg size (%zu) on TX, got: %i\n",
398 sizeof(*msg
), byte_size
);
402 print_hex_dump_debug("from client ", DUMP_PREFIX_OFFSET
, 4, 4,
403 data
, byte_size
, false);
405 /* Send first word */
406 dev_dbg(priv
->dev
, "Sending header\n");
407 imx_mu_write(priv
, *arg
++, priv
->dcfg
->xTR
);
410 dev_dbg(priv
->dev
, "Sending signaling\n");
411 imx_mu_xcr_rmw(priv
, IMX_MU_GCR
,
412 IMX_MU_xCR_GIRn(priv
->dcfg
->type
, cp
->idx
), 0);
414 /* Send words to fill the mailbox */
415 for (i
= 1; i
< 4 && i
< msg
->hdr
.size
; i
++) {
416 dev_dbg(priv
->dev
, "Sending word %d\n", i
);
417 imx_mu_write(priv
, *arg
++,
418 priv
->dcfg
->xTR
+ (i
% 4) * 4);
421 /* Send rest of message waiting for remote read */
422 for (; i
< msg
->hdr
.size
; i
++) {
423 dev_dbg(priv
->dev
, "Sending word %d\n", i
);
424 err
= imx_mu_tx_waiting_write(priv
, *arg
++, i
);
426 dev_err(priv
->dev
, "Timeout tx %d\n", i
);
431 /* Simulate hack for mbox framework */
432 queue_work(system_bh_wq
, &cp
->txdb_work
);
436 dev_warn_ratelimited(priv
->dev
,
437 "Send data on wrong channel type: %d\n",
445 static int imx_mu_seco_rxdb(struct imx_mu_priv
*priv
, struct imx_mu_con_priv
*cp
)
447 struct imx_sc_rpc_msg_max msg
;
448 u32
*data
= (u32
*)&msg
;
453 dev_dbg(priv
->dev
, "Receiving message\n");
456 dev_dbg(priv
->dev
, "Receiving header\n");
457 *data
++ = imx_mu_read(priv
, priv
->dcfg
->xRR
);
458 byte_size
= msg
.hdr
.size
* sizeof(u32
);
459 if (byte_size
> sizeof(msg
)) {
460 dev_err(priv
->dev
, "Exceed max msg size (%zu) on RX, got: %i\n",
461 sizeof(msg
), byte_size
);
466 /* Read message waiting they are written */
467 for (i
= 1; i
< msg
.hdr
.size
; i
++) {
468 dev_dbg(priv
->dev
, "Receiving word %d\n", i
);
469 err
= imx_mu_rx_waiting_read(priv
, data
++, i
);
471 dev_err(priv
->dev
, "Timeout rx %d\n", i
);
477 imx_mu_write(priv
, IMX_MU_xSR_GIPn(priv
->dcfg
->type
, cp
->idx
),
478 priv
->dcfg
->xSR
[IMX_MU_GSR
]);
480 print_hex_dump_debug("to client ", DUMP_PREFIX_OFFSET
, 4, 4,
481 &msg
, byte_size
, false);
483 /* send data to client */
484 dev_dbg(priv
->dev
, "Sending message to client\n");
485 mbox_chan_received_data(cp
->chan
, (void *)&msg
);
490 mbox_chan_received_data(cp
->chan
, ERR_PTR(err
));
496 static void imx_mu_txdb_work(struct work_struct
*t
)
498 struct imx_mu_con_priv
*cp
= from_work(cp
, t
, txdb_work
);
500 mbox_chan_txdone(cp
->chan
, 0);
503 static irqreturn_t
imx_mu_isr(int irq
, void *p
)
505 struct mbox_chan
*chan
= p
;
506 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
507 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
512 ctrl
= imx_mu_read(priv
, priv
->dcfg
->xCR
[IMX_MU_TCR
]);
513 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_TSR
]);
514 val
&= IMX_MU_xSR_TEn(priv
->dcfg
->type
, cp
->idx
) &
515 (ctrl
& IMX_MU_xCR_TIEn(priv
->dcfg
->type
, cp
->idx
));
518 ctrl
= imx_mu_read(priv
, priv
->dcfg
->xCR
[IMX_MU_RCR
]);
519 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_RSR
]);
520 val
&= IMX_MU_xSR_RFn(priv
->dcfg
->type
, cp
->idx
) &
521 (ctrl
& IMX_MU_xCR_RIEn(priv
->dcfg
->type
, cp
->idx
));
523 case IMX_MU_TYPE_RXDB
:
524 ctrl
= imx_mu_read(priv
, priv
->dcfg
->xCR
[IMX_MU_GIER
]);
525 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_GSR
]);
526 val
&= IMX_MU_xSR_GIPn(priv
->dcfg
->type
, cp
->idx
) &
527 (ctrl
& IMX_MU_xCR_GIEn(priv
->dcfg
->type
, cp
->idx
));
529 case IMX_MU_TYPE_RST
:
532 dev_warn_ratelimited(priv
->dev
, "Unhandled channel type %d\n",
540 if ((val
== IMX_MU_xSR_TEn(priv
->dcfg
->type
, cp
->idx
)) &&
541 (cp
->type
== IMX_MU_TYPE_TX
)) {
542 imx_mu_xcr_rmw(priv
, IMX_MU_TCR
, 0, IMX_MU_xCR_TIEn(priv
->dcfg
->type
, cp
->idx
));
543 mbox_chan_txdone(chan
, 0);
544 } else if ((val
== IMX_MU_xSR_RFn(priv
->dcfg
->type
, cp
->idx
)) &&
545 (cp
->type
== IMX_MU_TYPE_RX
)) {
546 priv
->dcfg
->rx(priv
, cp
);
547 } else if ((val
== IMX_MU_xSR_GIPn(priv
->dcfg
->type
, cp
->idx
)) &&
548 (cp
->type
== IMX_MU_TYPE_RXDB
)) {
549 priv
->dcfg
->rxdb(priv
, cp
);
551 dev_warn_ratelimited(priv
->dev
, "Not handled interrupt\n");
561 static int imx_mu_send_data(struct mbox_chan
*chan
, void *data
)
563 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
564 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
566 return priv
->dcfg
->tx(priv
, cp
, data
);
569 static int imx_mu_startup(struct mbox_chan
*chan
)
571 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
572 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
573 unsigned long irq_flag
= 0;
576 pm_runtime_get_sync(priv
->dev
);
577 if (cp
->type
== IMX_MU_TYPE_TXDB_V2
)
580 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
581 /* Tx doorbell don't have ACK support */
582 INIT_WORK(&cp
->txdb_work
, imx_mu_txdb_work
);
586 /* IPC MU should be with IRQF_NO_SUSPEND set */
587 if (!priv
->dev
->pm_domain
)
588 irq_flag
|= IRQF_NO_SUSPEND
;
590 if (!(priv
->dcfg
->type
& IMX_MU_V2_IRQ
))
591 irq_flag
|= IRQF_SHARED
;
593 ret
= request_irq(priv
->irq
[cp
->type
], imx_mu_isr
, irq_flag
, cp
->irq_desc
, chan
);
595 dev_err(priv
->dev
, "Unable to acquire IRQ %d\n", priv
->irq
[cp
->type
]);
601 imx_mu_xcr_rmw(priv
, IMX_MU_RCR
, IMX_MU_xCR_RIEn(priv
->dcfg
->type
, cp
->idx
), 0);
603 case IMX_MU_TYPE_RXDB
:
604 imx_mu_xcr_rmw(priv
, IMX_MU_GIER
, IMX_MU_xCR_GIEn(priv
->dcfg
->type
, cp
->idx
), 0);
613 static void imx_mu_shutdown(struct mbox_chan
*chan
)
615 struct imx_mu_priv
*priv
= to_imx_mu_priv(chan
->mbox
);
616 struct imx_mu_con_priv
*cp
= chan
->con_priv
;
620 if (cp
->type
== IMX_MU_TYPE_TXDB_V2
) {
621 pm_runtime_put_sync(priv
->dev
);
625 if (cp
->type
== IMX_MU_TYPE_TXDB
) {
626 cancel_work_sync(&cp
->txdb_work
);
627 pm_runtime_put_sync(priv
->dev
);
633 imx_mu_xcr_rmw(priv
, IMX_MU_TCR
, 0, IMX_MU_xCR_TIEn(priv
->dcfg
->type
, cp
->idx
));
636 imx_mu_xcr_rmw(priv
, IMX_MU_RCR
, 0, IMX_MU_xCR_RIEn(priv
->dcfg
->type
, cp
->idx
));
638 case IMX_MU_TYPE_RXDB
:
639 imx_mu_xcr_rmw(priv
, IMX_MU_GIER
, 0, IMX_MU_xCR_GIEn(priv
->dcfg
->type
, cp
->idx
));
641 case IMX_MU_TYPE_RST
:
642 imx_mu_xcr_rmw(priv
, IMX_MU_CR
, IMX_MU_xCR_RST(priv
->dcfg
->type
), 0);
643 ret
= readl_poll_timeout(priv
->base
+ priv
->dcfg
->xSR
[IMX_MU_SR
], sr
,
644 !(sr
& IMX_MU_xSR_RST(priv
->dcfg
->type
)), 1, 5);
646 dev_warn(priv
->dev
, "RST channel timeout\n");
652 free_irq(priv
->irq
[cp
->type
], chan
);
653 pm_runtime_put_sync(priv
->dev
);
656 static const struct mbox_chan_ops imx_mu_ops
= {
657 .send_data
= imx_mu_send_data
,
658 .startup
= imx_mu_startup
,
659 .shutdown
= imx_mu_shutdown
,
662 static struct mbox_chan
*imx_mu_specific_xlate(struct mbox_controller
*mbox
,
663 const struct of_phandle_args
*sp
)
667 if (sp
->args_count
!= 2) {
668 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
669 return ERR_PTR(-EINVAL
);
672 type
= sp
->args
[0]; /* channel type */
673 idx
= sp
->args
[1]; /* index */
679 dev_err(mbox
->dev
, "Invalid chan idx: %d\n", idx
);
682 case IMX_MU_TYPE_RXDB
:
686 dev_err(mbox
->dev
, "Invalid chan type: %d\n", type
);
687 return ERR_PTR(-EINVAL
);
690 if (chan
>= mbox
->num_chans
) {
691 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
692 return ERR_PTR(-EINVAL
);
695 return &mbox
->chans
[chan
];
698 static struct mbox_chan
* imx_mu_xlate(struct mbox_controller
*mbox
,
699 const struct of_phandle_args
*sp
)
701 struct mbox_chan
*p_chan
;
704 if (sp
->args_count
!= 2) {
705 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
706 return ERR_PTR(-EINVAL
);
709 type
= sp
->args
[0]; /* channel type */
710 idx
= sp
->args
[1]; /* index */
712 /* RST only supports 1 channel */
713 if ((type
== IMX_MU_TYPE_RST
) && idx
) {
714 dev_err(mbox
->dev
, "Invalid RST channel %d\n", idx
);
715 return ERR_PTR(-EINVAL
);
718 chan
= type
* 4 + idx
;
719 if (chan
>= mbox
->num_chans
) {
720 dev_err(mbox
->dev
, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan
, type
, idx
);
721 return ERR_PTR(-EINVAL
);
724 p_chan
= &mbox
->chans
[chan
];
726 if (type
== IMX_MU_TYPE_TXDB_V2
)
727 p_chan
->txdone_method
= TXDONE_BY_ACK
;
732 static struct mbox_chan
*imx_mu_seco_xlate(struct mbox_controller
*mbox
,
733 const struct of_phandle_args
*sp
)
737 if (sp
->args_count
< 1) {
738 dev_err(mbox
->dev
, "Invalid argument count %d\n", sp
->args_count
);
739 return ERR_PTR(-EINVAL
);
742 type
= sp
->args
[0]; /* channel type */
744 /* Only supports TXDB and RXDB */
745 if (type
== IMX_MU_TYPE_TX
|| type
== IMX_MU_TYPE_RX
) {
746 dev_err(mbox
->dev
, "Invalid type: %d\n", type
);
747 return ERR_PTR(-EINVAL
);
750 return imx_mu_xlate(mbox
, sp
);
753 static void imx_mu_get_tr_rr(struct imx_mu_priv
*priv
)
757 if (priv
->dcfg
->type
& IMX_MU_V2
) {
758 val
= imx_mu_read(priv
, IMX_MU_V2_PAR_OFF
);
759 priv
->num_tr
= FIELD_GET(IMX_MU_V2_TR_MASK
, val
);
760 priv
->num_rr
= FIELD_GET(IMX_MU_V2_RR_MASK
, val
);
767 static int imx_mu_init_generic(struct imx_mu_priv
*priv
)
772 if (priv
->num_rr
> 4 || priv
->num_tr
> 4) {
773 WARN_ONCE(true, "%s not support TR/RR larger than 4\n", __func__
);
777 for (i
= 0; i
< IMX_MU_CHANS
; i
++) {
778 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
782 cp
->chan
= &priv
->mbox_chans
[i
];
783 priv
->mbox_chans
[i
].con_priv
= cp
;
784 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
785 "%s[%i-%u]", dev_name(priv
->dev
), cp
->type
, cp
->idx
);
788 priv
->mbox
.num_chans
= IMX_MU_CHANS
;
789 priv
->mbox
.of_xlate
= imx_mu_xlate
;
794 /* Set default MU configuration */
795 for (i
= 0; i
< IMX_MU_xCR_MAX
; i
++)
796 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
[i
]);
798 /* Clear any pending GIP */
799 val
= imx_mu_read(priv
, priv
->dcfg
->xSR
[IMX_MU_GSR
]);
800 imx_mu_write(priv
, val
, priv
->dcfg
->xSR
[IMX_MU_GSR
]);
802 /* Clear any pending RSR */
803 for (i
= 0; i
< priv
->num_rr
; i
++)
804 imx_mu_read(priv
, priv
->dcfg
->xRR
+ i
* 4);
809 static int imx_mu_init_specific(struct imx_mu_priv
*priv
)
812 int num_chans
= priv
->dcfg
->type
& IMX_MU_V2_S4
? IMX_MU_S4_CHANS
: IMX_MU_SCU_CHANS
;
814 for (i
= 0; i
< num_chans
; i
++) {
815 struct imx_mu_con_priv
*cp
= &priv
->con_priv
[i
];
817 cp
->idx
= i
< 2 ? 0 : i
- 2;
818 cp
->type
= i
< 2 ? i
: IMX_MU_TYPE_RXDB
;
819 cp
->chan
= &priv
->mbox_chans
[i
];
820 priv
->mbox_chans
[i
].con_priv
= cp
;
821 snprintf(cp
->irq_desc
, sizeof(cp
->irq_desc
),
822 "%s[%i-%u]", dev_name(priv
->dev
), cp
->type
, cp
->idx
);
825 priv
->mbox
.num_chans
= num_chans
;
826 priv
->mbox
.of_xlate
= imx_mu_specific_xlate
;
828 /* Set default MU configuration */
829 for (i
= 0; i
< IMX_MU_xCR_MAX
; i
++)
830 imx_mu_write(priv
, 0, priv
->dcfg
->xCR
[i
]);
835 static int imx_mu_init_seco(struct imx_mu_priv
*priv
)
839 ret
= imx_mu_init_generic(priv
);
842 priv
->mbox
.of_xlate
= imx_mu_seco_xlate
;
847 static int imx_mu_probe(struct platform_device
*pdev
)
849 struct device
*dev
= &pdev
->dev
;
850 struct device_node
*np
= dev
->of_node
;
851 struct imx_mu_priv
*priv
;
852 const struct imx_mu_dcfg
*dcfg
;
856 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
862 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
863 if (IS_ERR(priv
->base
))
864 return PTR_ERR(priv
->base
);
866 dcfg
= of_device_get_match_data(dev
);
870 if (priv
->dcfg
->type
& IMX_MU_V2_IRQ
) {
871 priv
->irq
[IMX_MU_TYPE_TX
] = platform_get_irq_byname(pdev
, "tx");
872 if (priv
->irq
[IMX_MU_TYPE_TX
] < 0)
873 return priv
->irq
[IMX_MU_TYPE_TX
];
874 priv
->irq
[IMX_MU_TYPE_RX
] = platform_get_irq_byname(pdev
, "rx");
875 if (priv
->irq
[IMX_MU_TYPE_RX
] < 0)
876 return priv
->irq
[IMX_MU_TYPE_RX
];
878 ret
= platform_get_irq(pdev
, 0);
882 for (i
= 0; i
< IMX_MU_CHANS
; i
++)
886 if (priv
->dcfg
->type
& IMX_MU_V2_S4
)
887 size
= sizeof(struct imx_s4_rpc_msg_max
);
889 size
= sizeof(struct imx_sc_rpc_msg_max
);
891 priv
->msg
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
895 priv
->clk
= devm_clk_get(dev
, NULL
);
896 if (IS_ERR(priv
->clk
)) {
897 if (PTR_ERR(priv
->clk
) != -ENOENT
)
898 return PTR_ERR(priv
->clk
);
903 ret
= clk_prepare_enable(priv
->clk
);
905 dev_err(dev
, "Failed to enable clock\n");
909 imx_mu_get_tr_rr(priv
);
911 priv
->side_b
= of_property_read_bool(np
, "fsl,mu-side-b");
913 ret
= priv
->dcfg
->init(priv
);
915 dev_err(dev
, "Failed to init MU\n");
919 spin_lock_init(&priv
->xcr_lock
);
921 priv
->mbox
.dev
= dev
;
922 priv
->mbox
.ops
= &imx_mu_ops
;
923 priv
->mbox
.chans
= priv
->mbox_chans
;
924 priv
->mbox
.txdone_irq
= true;
926 platform_set_drvdata(pdev
, priv
);
928 ret
= devm_mbox_controller_register(dev
, &priv
->mbox
);
932 of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
934 pm_runtime_enable(dev
);
936 ret
= pm_runtime_resume_and_get(dev
);
938 goto disable_runtime_pm
;
940 ret
= pm_runtime_put_sync(dev
);
942 goto disable_runtime_pm
;
944 clk_disable_unprepare(priv
->clk
);
949 pm_runtime_disable(dev
);
951 clk_disable_unprepare(priv
->clk
);
955 static void imx_mu_remove(struct platform_device
*pdev
)
957 struct imx_mu_priv
*priv
= platform_get_drvdata(pdev
);
959 pm_runtime_disable(priv
->dev
);
962 static const struct imx_mu_dcfg imx_mu_cfg_imx6sx
= {
963 .tx
= imx_mu_generic_tx
,
964 .rx
= imx_mu_generic_rx
,
965 .rxdb
= imx_mu_generic_rxdb
,
966 .init
= imx_mu_init_generic
,
969 .xSR
= {0x20, 0x20, 0x20, 0x20},
970 .xCR
= {0x24, 0x24, 0x24, 0x24, 0x24},
973 static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp
= {
974 .tx
= imx_mu_generic_tx
,
975 .rx
= imx_mu_generic_rx
,
976 .rxdb
= imx_mu_generic_rxdb
,
977 .init
= imx_mu_init_generic
,
980 .xSR
= {0x60, 0x60, 0x60, 0x60},
981 .xCR
= {0x64, 0x64, 0x64, 0x64, 0x64},
984 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp
= {
985 .tx
= imx_mu_generic_tx
,
986 .rx
= imx_mu_generic_rx
,
987 .rxdb
= imx_mu_generic_rxdb
,
988 .init
= imx_mu_init_generic
,
992 .xSR
= {0xC, 0x118, 0x124, 0x12C},
993 .xCR
= {0x8, 0x110, 0x114, 0x120, 0x128},
996 static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp_s4
= {
997 .tx
= imx_mu_specific_tx
,
998 .rx
= imx_mu_specific_rx
,
999 .init
= imx_mu_init_specific
,
1000 .type
= IMX_MU_V2
| IMX_MU_V2_S4
,
1003 .xSR
= {0xC, 0x118, 0x124, 0x12C},
1004 .xCR
= {0x8, 0x110, 0x114, 0x120, 0x128},
1007 static const struct imx_mu_dcfg imx_mu_cfg_imx93_s4
= {
1008 .tx
= imx_mu_specific_tx
,
1009 .rx
= imx_mu_specific_rx
,
1010 .init
= imx_mu_init_specific
,
1011 .type
= IMX_MU_V2
| IMX_MU_V2_S4
| IMX_MU_V2_IRQ
,
1014 .xSR
= {0xC, 0x118, 0x124, 0x12C},
1015 .xCR
= {0x8, 0x110, 0x114, 0x120, 0x128},
1018 static const struct imx_mu_dcfg imx_mu_cfg_imx8_scu
= {
1019 .tx
= imx_mu_specific_tx
,
1020 .rx
= imx_mu_specific_rx
,
1021 .init
= imx_mu_init_specific
,
1022 .rxdb
= imx_mu_generic_rxdb
,
1025 .xSR
= {0x20, 0x20, 0x20, 0x20},
1026 .xCR
= {0x24, 0x24, 0x24, 0x24, 0x24},
1029 static const struct imx_mu_dcfg imx_mu_cfg_imx8_seco
= {
1030 .tx
= imx_mu_seco_tx
,
1031 .rx
= imx_mu_generic_rx
,
1032 .rxdb
= imx_mu_seco_rxdb
,
1033 .init
= imx_mu_init_seco
,
1036 .xSR
= {0x20, 0x20, 0x20, 0x20},
1037 .xCR
= {0x24, 0x24, 0x24, 0x24, 0x24},
1040 static const struct of_device_id imx_mu_dt_ids
[] = {
1041 { .compatible
= "fsl,imx7ulp-mu", .data
= &imx_mu_cfg_imx7ulp
},
1042 { .compatible
= "fsl,imx6sx-mu", .data
= &imx_mu_cfg_imx6sx
},
1043 { .compatible
= "fsl,imx8ulp-mu", .data
= &imx_mu_cfg_imx8ulp
},
1044 { .compatible
= "fsl,imx8ulp-mu-s4", .data
= &imx_mu_cfg_imx8ulp_s4
},
1045 { .compatible
= "fsl,imx93-mu-s4", .data
= &imx_mu_cfg_imx93_s4
},
1046 { .compatible
= "fsl,imx95-mu", .data
= &imx_mu_cfg_imx8ulp
},
1047 { .compatible
= "fsl,imx95-mu-ele", .data
= &imx_mu_cfg_imx8ulp_s4
},
1048 { .compatible
= "fsl,imx95-mu-v2x", .data
= &imx_mu_cfg_imx8ulp_s4
},
1049 { .compatible
= "fsl,imx8-mu-scu", .data
= &imx_mu_cfg_imx8_scu
},
1050 { .compatible
= "fsl,imx8-mu-seco", .data
= &imx_mu_cfg_imx8_seco
},
1053 MODULE_DEVICE_TABLE(of
, imx_mu_dt_ids
);
1055 static int __maybe_unused
imx_mu_suspend_noirq(struct device
*dev
)
1057 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
1061 for (i
= 0; i
< IMX_MU_xCR_MAX
; i
++)
1062 priv
->xcr
[i
] = imx_mu_read(priv
, priv
->dcfg
->xCR
[i
]);
1065 priv
->suspend
= true;
1070 static int __maybe_unused
imx_mu_resume_noirq(struct device
*dev
)
1072 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
1076 * ONLY restore MU when context lost, the TIE could
1077 * be set during noirq resume as there is MU data
1078 * communication going on, and restore the saved
1079 * value will overwrite the TIE and cause MU data
1080 * send failed, may lead to system freeze. This issue
1081 * is observed by testing freeze mode suspend.
1083 if (!priv
->clk
&& !imx_mu_read(priv
, priv
->dcfg
->xCR
[0])) {
1084 for (i
= 0; i
< IMX_MU_xCR_MAX
; i
++)
1085 imx_mu_write(priv
, priv
->xcr
[i
], priv
->dcfg
->xCR
[i
]);
1088 priv
->suspend
= false;
1093 static int __maybe_unused
imx_mu_runtime_suspend(struct device
*dev
)
1095 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
1097 clk_disable_unprepare(priv
->clk
);
1102 static int __maybe_unused
imx_mu_runtime_resume(struct device
*dev
)
1104 struct imx_mu_priv
*priv
= dev_get_drvdata(dev
);
1107 ret
= clk_prepare_enable(priv
->clk
);
1109 dev_err(dev
, "failed to enable clock\n");
1114 static const struct dev_pm_ops imx_mu_pm_ops
= {
1115 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_mu_suspend_noirq
,
1116 imx_mu_resume_noirq
)
1117 SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend
,
1118 imx_mu_runtime_resume
, NULL
)
1121 static struct platform_driver imx_mu_driver
= {
1122 .probe
= imx_mu_probe
,
1123 .remove
= imx_mu_remove
,
1126 .of_match_table
= imx_mu_dt_ids
,
1127 .pm
= &imx_mu_pm_ops
,
1130 module_platform_driver(imx_mu_driver
);
1132 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
1133 MODULE_DESCRIPTION("Message Unit driver for i.MX");
1134 MODULE_LICENSE("GPL v2");