blob: 913ebe7ad3fe504c12bedc1f187a9990b0826e9b [file] [log] [blame]
Ye Li0db17f42021-08-07 16:00:41 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2020 NXP
4 */
5
6#include <common.h>
7#include <asm/io.h>
8#include <dm.h>
9#include <dm/lists.h>
10#include <dm/root.h>
11#include <dm/device-internal.h>
12#include <asm/arch/s400_api.h>
13#include <linux/iopoll.h>
14#include <misc.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18struct mu_type {
19 u32 ver;
20 u32 par;
21 u32 cr;
22 u32 sr;
23 u32 reserved0[68];
24 u32 tcr;
25 u32 tsr;
26 u32 rcr;
27 u32 rsr;
28 u32 reserved1[52];
29 u32 tr[16];
30 u32 reserved2[16];
31 u32 rr[16];
32 u32 reserved4[14];
33 u32 mu_attr;
34};
35
36struct imx8ulp_mu {
37 struct mu_type *base;
38};
39
40#define MU_SR_TE0_MASK BIT(0)
41#define MU_SR_RF0_MASK BIT(0)
42#define MU_TR_COUNT 4
43#define MU_RR_COUNT 4
44
Ye Li853cc9d2021-08-07 16:00:55 +080045void mu_hal_init(ulong base)
Ye Li0db17f42021-08-07 16:00:41 +080046{
Ye Li853cc9d2021-08-07 16:00:55 +080047 struct mu_type *mu_base = (struct mu_type *)base;
48
49 writel(0, &mu_base->tcr);
50 writel(0, &mu_base->rcr);
Ye Li0db17f42021-08-07 16:00:41 +080051}
52
Ye Li853cc9d2021-08-07 16:00:55 +080053int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg)
Ye Li0db17f42021-08-07 16:00:41 +080054{
Ye Li853cc9d2021-08-07 16:00:55 +080055 struct mu_type *mu_base = (struct mu_type *)base;
Ye Li0db17f42021-08-07 16:00:41 +080056 u32 mask = MU_SR_TE0_MASK << reg_index;
57 u32 val;
58 int ret;
59
60 assert(reg_index < MU_TR_COUNT);
61
Ye Li853cc9d2021-08-07 16:00:55 +080062 debug("sendmsg sr 0x%x\n", readl(&mu_base->sr));
Ye Li0db17f42021-08-07 16:00:41 +080063
64 /* Wait TX register to be empty. */
Ye Li853cc9d2021-08-07 16:00:55 +080065 ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000);
Ye Li0db17f42021-08-07 16:00:41 +080066 if (ret < 0) {
67 debug("%s timeout\n", __func__);
68 return -ETIMEDOUT;
69 }
70
71 debug("tr[%d] 0x%x\n", reg_index, msg);
72
Ye Li853cc9d2021-08-07 16:00:55 +080073 writel(msg, &mu_base->tr[reg_index]);
Ye Li0db17f42021-08-07 16:00:41 +080074
75 return 0;
76}
77
Ye Li853cc9d2021-08-07 16:00:55 +080078int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg)
Ye Li0db17f42021-08-07 16:00:41 +080079{
Ye Li853cc9d2021-08-07 16:00:55 +080080 struct mu_type *mu_base = (struct mu_type *)base;
Ye Li0db17f42021-08-07 16:00:41 +080081 u32 mask = MU_SR_RF0_MASK << reg_index;
82 u32 val;
83 int ret;
84
85 assert(reg_index < MU_TR_COUNT);
86
Ye Li853cc9d2021-08-07 16:00:55 +080087 debug("receivemsg sr 0x%x\n", readl(&mu_base->sr));
Ye Li0db17f42021-08-07 16:00:41 +080088
89 /* Wait RX register to be full. */
Ye Li853cc9d2021-08-07 16:00:55 +080090 ret = readl_poll_timeout(&mu_base->rsr, val, val & mask, 10000);
Ye Li0db17f42021-08-07 16:00:41 +080091 if (ret < 0) {
92 debug("%s timeout\n", __func__);
93 return -ETIMEDOUT;
94 }
95
Ye Li853cc9d2021-08-07 16:00:55 +080096 *msg = readl(&mu_base->rr[reg_index]);
Ye Li0db17f42021-08-07 16:00:41 +080097
98 debug("rr[%d] 0x%x\n", reg_index, *msg);
99
100 return 0;
101}
102
103static int imx8ulp_mu_read(struct mu_type *base, void *data)
104{
105 struct imx8ulp_s400_msg *msg = (struct imx8ulp_s400_msg *)data;
106 int ret;
107 u8 count = 0;
108
109 if (!msg)
110 return -EINVAL;
111
112 /* Read first word */
Ye Li853cc9d2021-08-07 16:00:55 +0800113 ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg);
Ye Li0db17f42021-08-07 16:00:41 +0800114 if (ret)
115 return ret;
116 count++;
117
118 /* Check size */
119 if (msg->size > S400_MAX_MSG) {
120 *((u32 *)msg) = 0;
121 return -EINVAL;
122 }
123
124 /* Read remaining words */
125 while (count < msg->size) {
Ye Li853cc9d2021-08-07 16:00:55 +0800126 ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT,
Ye Li0db17f42021-08-07 16:00:41 +0800127 &msg->data[count - 1]);
128 if (ret)
129 return ret;
130 count++;
131 }
132
133 return 0;
134}
135
136static int imx8ulp_mu_write(struct mu_type *base, void *data)
137{
138 struct imx8ulp_s400_msg *msg = (struct imx8ulp_s400_msg *)data;
139 int ret;
140 u8 count = 0;
141
142 if (!msg)
143 return -EINVAL;
144
145 /* Check size */
146 if (msg->size > S400_MAX_MSG)
147 return -EINVAL;
148
149 /* Write first word */
Ye Li853cc9d2021-08-07 16:00:55 +0800150 ret = mu_hal_sendmsg((ulong)base, 0, *((u32 *)msg));
Ye Li0db17f42021-08-07 16:00:41 +0800151 if (ret)
152 return ret;
153 count++;
154
155 /* Write remaining words */
156 while (count < msg->size) {
Ye Li853cc9d2021-08-07 16:00:55 +0800157 ret = mu_hal_sendmsg((ulong)base, count % MU_TR_COUNT,
Ye Li0db17f42021-08-07 16:00:41 +0800158 msg->data[count - 1]);
159 if (ret)
160 return ret;
161 count++;
162 }
163
164 return 0;
165}
166
167/*
168 * Note the function prototype use msgid as the 2nd parameter, here
169 * we take it as no_resp.
170 */
171static int imx8ulp_mu_call(struct udevice *dev, int no_resp, void *tx_msg,
172 int tx_size, void *rx_msg, int rx_size)
173{
174 struct imx8ulp_mu *priv = dev_get_priv(dev);
175 u32 result;
176 int ret;
177
178 /* Expect tx_msg, rx_msg are the same value */
179 if (rx_msg && tx_msg != rx_msg)
180 printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg);
181
182 ret = imx8ulp_mu_write(priv->base, tx_msg);
183 if (ret)
184 return ret;
185 if (!no_resp) {
186 ret = imx8ulp_mu_read(priv->base, rx_msg);
187 if (ret)
188 return ret;
189 }
190
191 result = ((struct imx8ulp_s400_msg *)rx_msg)->data[0];
Ye Li79581a62021-08-07 16:00:51 +0800192 if ((result & 0xff) == 0xd6)
Ye Li0db17f42021-08-07 16:00:41 +0800193 return 0;
194
195 return -EIO;
196}
197
198static int imx8ulp_mu_probe(struct udevice *dev)
199{
200 struct imx8ulp_mu *priv = dev_get_priv(dev);
201 fdt_addr_t addr;
202
203 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
204
205 addr = devfdt_get_addr(dev);
206 if (addr == FDT_ADDR_T_NONE)
207 return -EINVAL;
208
209 priv->base = (struct mu_type *)addr;
210
211 debug("mu base 0x%lx\n", (ulong)priv->base);
212
213 /* U-Boot not enable interrupts, so need to enable RX interrupts */
Ye Li853cc9d2021-08-07 16:00:55 +0800214 mu_hal_init((ulong)priv->base);
Ye Li0db17f42021-08-07 16:00:41 +0800215
216 gd->arch.s400_dev = dev;
217
218 return 0;
219}
220
221static int imx8ulp_mu_remove(struct udevice *dev)
222{
223 return 0;
224}
225
226static int imx8ulp_mu_bind(struct udevice *dev)
227{
228 debug("%s(dev=%p)\n", __func__, dev);
229
230 return 0;
231}
232
233static struct misc_ops imx8ulp_mu_ops = {
234 .call = imx8ulp_mu_call,
235};
236
237static const struct udevice_id imx8ulp_mu_ids[] = {
238 { .compatible = "fsl,imx8ulp-mu" },
239 { }
240};
241
242U_BOOT_DRIVER(imx8ulp_mu) = {
243 .name = "imx8ulp_mu",
244 .id = UCLASS_MISC,
245 .of_match = imx8ulp_mu_ids,
246 .probe = imx8ulp_mu_probe,
247 .bind = imx8ulp_mu_bind,
248 .remove = imx8ulp_mu_remove,
249 .ops = &imx8ulp_mu_ops,
250 .priv_auto = sizeof(struct imx8ulp_mu),
251};