]>
Commit | Line | Data |
---|---|---|
0c00d03a YL |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
03fcf966 | 3 | * Copyright 2020-2022 NXP |
0c00d03a YL |
4 | */ |
5 | ||
0c00d03a YL |
6 | #include <asm/io.h> |
7 | #include <dm.h> | |
8 | #include <dm/lists.h> | |
9 | #include <dm/root.h> | |
10 | #include <dm/device-internal.h> | |
d3ee9dbd | 11 | #include <asm/mach-imx/ele_api.h> |
4b9423e6 | 12 | #include <asm/arch/imx-regs.h> |
0c00d03a YL |
13 | #include <linux/iopoll.h> |
14 | #include <misc.h> | |
15 | ||
16 | DECLARE_GLOBAL_DATA_PTR; | |
17 | ||
0c00d03a YL |
18 | struct imx8ulp_mu { |
19 | struct mu_type *base; | |
20 | }; | |
21 | ||
22 | #define MU_SR_TE0_MASK BIT(0) | |
23 | #define MU_SR_RF0_MASK BIT(0) | |
71a21425 | 24 | #define MU_TR_COUNT 8 |
0c00d03a YL |
25 | #define MU_RR_COUNT 4 |
26 | ||
ba472a20 | 27 | void mu_hal_init(ulong base) |
0c00d03a | 28 | { |
ba472a20 YL |
29 | struct mu_type *mu_base = (struct mu_type *)base; |
30 | ||
31 | writel(0, &mu_base->tcr); | |
32 | writel(0, &mu_base->rcr); | |
0c00d03a YL |
33 | } |
34 | ||
ba472a20 | 35 | int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg) |
0c00d03a | 36 | { |
ba472a20 | 37 | struct mu_type *mu_base = (struct mu_type *)base; |
0c00d03a YL |
38 | u32 mask = MU_SR_TE0_MASK << reg_index; |
39 | u32 val; | |
40 | int ret; | |
41 | ||
42 | assert(reg_index < MU_TR_COUNT); | |
43 | ||
78b4cf75 | 44 | debug("sendmsg tsr 0x%x\n", readl(&mu_base->tsr)); |
0c00d03a YL |
45 | |
46 | /* Wait TX register to be empty. */ | |
ba472a20 | 47 | ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000); |
0c00d03a YL |
48 | if (ret < 0) { |
49 | debug("%s timeout\n", __func__); | |
50 | return -ETIMEDOUT; | |
51 | } | |
52 | ||
53 | debug("tr[%d] 0x%x\n", reg_index, msg); | |
54 | ||
ba472a20 | 55 | writel(msg, &mu_base->tr[reg_index]); |
0c00d03a YL |
56 | |
57 | return 0; | |
58 | } | |
59 | ||
ba472a20 | 60 | int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg) |
0c00d03a | 61 | { |
ba472a20 | 62 | struct mu_type *mu_base = (struct mu_type *)base; |
0c00d03a YL |
63 | u32 mask = MU_SR_RF0_MASK << reg_index; |
64 | u32 val; | |
65 | int ret; | |
78b4cf75 | 66 | u32 count = 10; |
0c00d03a | 67 | |
71a21425 | 68 | assert(reg_index < MU_RR_COUNT); |
0c00d03a | 69 | |
78b4cf75 | 70 | debug("receivemsg rsr 0x%x\n", readl(&mu_base->rsr)); |
0c00d03a | 71 | |
78b4cf75 YL |
72 | do { |
73 | /* Wait RX register to be full. */ | |
74 | ret = readl_poll_timeout(&mu_base->rsr, val, val & mask, 1000000); | |
75 | if (ret < 0) { | |
76 | count--; | |
77 | printf("mu receive msg wait %us\n", 10 - count); | |
78 | } else { | |
79 | break; | |
80 | } | |
81 | } while (count > 0); | |
82 | ||
83 | if (count == 0) { | |
0c00d03a YL |
84 | debug("%s timeout\n", __func__); |
85 | return -ETIMEDOUT; | |
86 | } | |
87 | ||
ba472a20 | 88 | *msg = readl(&mu_base->rr[reg_index]); |
0c00d03a YL |
89 | |
90 | debug("rr[%d] 0x%x\n", reg_index, *msg); | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static int imx8ulp_mu_read(struct mu_type *base, void *data) | |
96 | { | |
d3ee9dbd | 97 | struct ele_msg *msg = (struct ele_msg *)data; |
0c00d03a YL |
98 | int ret; |
99 | u8 count = 0; | |
100 | ||
101 | if (!msg) | |
102 | return -EINVAL; | |
103 | ||
104 | /* Read first word */ | |
ba472a20 | 105 | ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg); |
0c00d03a YL |
106 | if (ret) |
107 | return ret; | |
108 | count++; | |
109 | ||
110 | /* Check size */ | |
d3ee9dbd | 111 | if (msg->size > ELE_MAX_MSG) { |
0c00d03a YL |
112 | *((u32 *)msg) = 0; |
113 | return -EINVAL; | |
114 | } | |
115 | ||
116 | /* Read remaining words */ | |
117 | while (count < msg->size) { | |
ba472a20 | 118 | ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT, |
0c00d03a YL |
119 | &msg->data[count - 1]); |
120 | if (ret) | |
121 | return ret; | |
122 | count++; | |
123 | } | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | static int imx8ulp_mu_write(struct mu_type *base, void *data) | |
129 | { | |
d3ee9dbd | 130 | struct ele_msg *msg = (struct ele_msg *)data; |
0c00d03a YL |
131 | int ret; |
132 | u8 count = 0; | |
133 | ||
134 | if (!msg) | |
135 | return -EINVAL; | |
136 | ||
137 | /* Check size */ | |
d3ee9dbd | 138 | if (msg->size > ELE_MAX_MSG) |
0c00d03a YL |
139 | return -EINVAL; |
140 | ||
141 | /* Write first word */ | |
ba472a20 | 142 | ret = mu_hal_sendmsg((ulong)base, 0, *((u32 *)msg)); |
0c00d03a YL |
143 | if (ret) |
144 | return ret; | |
145 | count++; | |
146 | ||
147 | /* Write remaining words */ | |
148 | while (count < msg->size) { | |
ba472a20 | 149 | ret = mu_hal_sendmsg((ulong)base, count % MU_TR_COUNT, |
0c00d03a YL |
150 | msg->data[count - 1]); |
151 | if (ret) | |
152 | return ret; | |
153 | count++; | |
154 | } | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | /* | |
160 | * Note the function prototype use msgid as the 2nd parameter, here | |
161 | * we take it as no_resp. | |
162 | */ | |
163 | static int imx8ulp_mu_call(struct udevice *dev, int no_resp, void *tx_msg, | |
164 | int tx_size, void *rx_msg, int rx_size) | |
165 | { | |
166 | struct imx8ulp_mu *priv = dev_get_priv(dev); | |
167 | u32 result; | |
168 | int ret; | |
169 | ||
170 | /* Expect tx_msg, rx_msg are the same value */ | |
171 | if (rx_msg && tx_msg != rx_msg) | |
172 | printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg); | |
173 | ||
174 | ret = imx8ulp_mu_write(priv->base, tx_msg); | |
175 | if (ret) | |
176 | return ret; | |
177 | if (!no_resp) { | |
178 | ret = imx8ulp_mu_read(priv->base, rx_msg); | |
179 | if (ret) | |
180 | return ret; | |
181 | } | |
182 | ||
d3ee9dbd | 183 | result = ((struct ele_msg *)rx_msg)->data[0]; |
a6ffde5e | 184 | if ((result & 0xff) == 0xd6) |
0c00d03a YL |
185 | return 0; |
186 | ||
187 | return -EIO; | |
188 | } | |
189 | ||
190 | static int imx8ulp_mu_probe(struct udevice *dev) | |
191 | { | |
192 | struct imx8ulp_mu *priv = dev_get_priv(dev); | |
193 | fdt_addr_t addr; | |
194 | ||
195 | debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv); | |
196 | ||
197 | addr = devfdt_get_addr(dev); | |
198 | if (addr == FDT_ADDR_T_NONE) | |
199 | return -EINVAL; | |
200 | ||
201 | priv->base = (struct mu_type *)addr; | |
202 | ||
203 | debug("mu base 0x%lx\n", (ulong)priv->base); | |
204 | ||
205 | /* U-Boot not enable interrupts, so need to enable RX interrupts */ | |
ba472a20 | 206 | mu_hal_init((ulong)priv->base); |
0c00d03a | 207 | |
d3ee9dbd | 208 | gd->arch.ele_dev = dev; |
0c00d03a YL |
209 | |
210 | return 0; | |
211 | } | |
212 | ||
213 | static int imx8ulp_mu_remove(struct udevice *dev) | |
214 | { | |
215 | return 0; | |
216 | } | |
217 | ||
218 | static int imx8ulp_mu_bind(struct udevice *dev) | |
219 | { | |
220 | debug("%s(dev=%p)\n", __func__, dev); | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | static struct misc_ops imx8ulp_mu_ops = { | |
226 | .call = imx8ulp_mu_call, | |
227 | }; | |
228 | ||
229 | static const struct udevice_id imx8ulp_mu_ids[] = { | |
230 | { .compatible = "fsl,imx8ulp-mu" }, | |
45fed324 | 231 | { .compatible = "fsl,imx93-mu-s4" }, |
0c00d03a YL |
232 | { } |
233 | }; | |
234 | ||
235 | U_BOOT_DRIVER(imx8ulp_mu) = { | |
236 | .name = "imx8ulp_mu", | |
237 | .id = UCLASS_MISC, | |
238 | .of_match = imx8ulp_mu_ids, | |
239 | .probe = imx8ulp_mu_probe, | |
240 | .bind = imx8ulp_mu_bind, | |
241 | .remove = imx8ulp_mu_remove, | |
242 | .ops = &imx8ulp_mu_ops, | |
243 | .priv_auto = sizeof(struct imx8ulp_mu), | |
244 | }; |