1 // SPDX-License-Identifier: GPL-2.0
5 * Peng Fan <peng.fan@nxp.com>
13 #include <dm/device-internal.h>
14 #include <asm/arch/sci/sci.h>
15 #include <linux/iopoll.h>
18 DECLARE_GLOBAL_DATA_PTR;
30 struct udevice *pinclk;
33 #define MU_CR_GIE_MASK 0xF0000000u
34 #define MU_CR_RIE_MASK 0xF000000u
35 #define MU_CR_GIR_MASK 0xF0000u
36 #define MU_CR_TIE_MASK 0xF00000u
37 #define MU_CR_F_MASK 0x7u
38 #define MU_SR_TE0_MASK BIT(23)
39 #define MU_SR_RF0_MASK BIT(27)
43 static inline void mu_hal_init(struct mu_type *base)
45 /* Clear GIEn, RIEn, TIEn, GIRn and ABFn. */
46 clrbits_le32(&base->cr, MU_CR_GIE_MASK | MU_CR_RIE_MASK |
47 MU_CR_TIE_MASK | MU_CR_GIR_MASK | MU_CR_F_MASK);
50 static int mu_hal_sendmsg(struct mu_type *base, u32 reg_index, u32 msg)
52 u32 mask = MU_SR_TE0_MASK >> reg_index;
56 assert(reg_index < MU_TR_COUNT);
58 /* Wait TX register to be empty. */
59 ret = readl_poll_timeout(&base->sr, val, val & mask, 10000);
61 printf("%s timeout\n", __func__);
65 writel(msg, &base->tr[reg_index]);
70 static int mu_hal_receivemsg(struct mu_type *base, u32 reg_index, u32 *msg)
72 u32 mask = MU_SR_RF0_MASK >> reg_index;
76 assert(reg_index < MU_TR_COUNT);
78 /* Wait RX register to be full. */
79 ret = readl_poll_timeout(&base->sr, val, val & mask, 10000);
81 printf("%s timeout\n", __func__);
85 *msg = readl(&base->rr[reg_index]);
90 static int sc_ipc_read(struct mu_type *base, void *data)
92 struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
100 ret = mu_hal_receivemsg(base, 0, (u32 *)msg);
106 if (msg->size > SC_RPC_MAX_MSG) {
111 /* Read remaining words */
112 while (count < msg->size) {
113 ret = mu_hal_receivemsg(base, count % MU_RR_COUNT,
114 &msg->DATA.u32[count - 1]);
123 static int sc_ipc_write(struct mu_type *base, void *data)
125 struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
133 if (msg->size > SC_RPC_MAX_MSG)
136 /* Write first word */
137 ret = mu_hal_sendmsg(base, 0, *((u32 *)msg));
142 /* Write remaining words */
143 while (count < msg->size) {
144 ret = mu_hal_sendmsg(base, count % MU_TR_COUNT,
145 msg->DATA.u32[count - 1]);
155 * Note the function prototype use msgid as the 2nd parameter, here
156 * we take it as no_resp.
158 static int imx8_scu_call(struct udevice *dev, int no_resp, void *tx_msg,
159 int tx_size, void *rx_msg, int rx_size)
161 struct imx8_scu *priv = dev_get_priv(dev);
165 /* Expect tx_msg, rx_msg are the same value */
166 if (rx_msg && tx_msg != rx_msg)
167 printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg);
169 ret = sc_ipc_write(priv->base, tx_msg);
173 ret = sc_ipc_read(priv->base, rx_msg);
178 result = RPC_R8((struct sc_rpc_msg_s *)tx_msg);
180 return sc_err_to_linux(result);
183 static int imx8_scu_probe(struct udevice *dev)
185 struct imx8_scu *priv = dev_get_priv(dev);
188 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
190 addr = devfdt_get_addr(dev);
191 if (addr == FDT_ADDR_T_NONE)
194 priv->base = (struct mu_type *)addr;
196 /* U-Boot not enable interrupts, so need to enable RX interrupts */
197 mu_hal_init(priv->base);
199 gd->arch.scu_dev = dev;
201 device_probe(priv->clk);
202 device_probe(priv->pinclk);
207 static int imx8_scu_remove(struct udevice *dev)
212 static int imx8_scu_bind(struct udevice *dev)
214 struct imx8_scu *priv = dev_get_priv(dev);
216 struct udevice *child;
219 debug("%s(dev=%p)\n", __func__, dev);
221 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
224 panic("No clk node found\n");
226 ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child);
232 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
233 "fsl,imx8qxp-iomuxc");
235 panic("No iomuxc node found\n");
237 ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child);
241 priv->pinclk = child;
246 static struct misc_ops imx8_scu_ops = {
247 .call = imx8_scu_call,
250 static const struct udevice_id imx8_scu_ids[] = {
251 { .compatible = "fsl,imx8qxp-mu" },
252 { .compatible = "fsl,imx8-mu" },
256 U_BOOT_DRIVER(imx8_scu) = {
259 .of_match = imx8_scu_ids,
260 .probe = imx8_scu_probe,
261 .bind = imx8_scu_bind,
262 .remove = imx8_scu_remove,
263 .ops = &imx8_scu_ops,
264 .priv_auto_alloc_size = sizeof(struct imx8_scu),
265 .flags = DM_FLAG_PRE_RELOC,