blob: 0cf81f33ba5e704b45840be8c9e4cb2917946327 [file] [log] [blame]
Ye Li0db17f42021-08-07 16:00:41 +08001// SPDX-License-Identifier: GPL-2.0
2/*
Ye Lic408ed32022-07-26 16:40:49 +08003 * Copyright 2020-2022 NXP
Ye Li0db17f42021-08-07 16:00:41 +08004 */
5
Ye Li0db17f42021-08-07 16:00:41 +08006#include <asm/io.h>
7#include <dm.h>
8#include <dm/lists.h>
9#include <dm/root.h>
10#include <dm/device-internal.h>
Peng Fand5c31832023-06-15 18:09:05 +080011#include <asm/mach-imx/ele_api.h>
Peng Fanf6928f92021-08-07 16:01:09 +080012#include <asm/arch/imx-regs.h>
Ye Li0db17f42021-08-07 16:00:41 +080013#include <linux/iopoll.h>
14#include <misc.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Ye Li0db17f42021-08-07 16:00:41 +080018struct 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)
Ye Li606fbf42023-06-15 18:09:12 +080024#define MU_TR_COUNT 8
Ye Li0db17f42021-08-07 16:00:41 +080025#define MU_RR_COUNT 4
26
Ye Li853cc9d2021-08-07 16:00:55 +080027void mu_hal_init(ulong base)
Ye Li0db17f42021-08-07 16:00:41 +080028{
Ye Li853cc9d2021-08-07 16:00:55 +080029 struct mu_type *mu_base = (struct mu_type *)base;
30
31 writel(0, &mu_base->tcr);
32 writel(0, &mu_base->rcr);
Ye Li0db17f42021-08-07 16:00:41 +080033}
34
Ye Li853cc9d2021-08-07 16:00:55 +080035int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg)
Ye Li0db17f42021-08-07 16:00:41 +080036{
Ye Li853cc9d2021-08-07 16:00:55 +080037 struct mu_type *mu_base = (struct mu_type *)base;
Ye Li0db17f42021-08-07 16:00:41 +080038 u32 mask = MU_SR_TE0_MASK << reg_index;
39 u32 val;
40 int ret;
41
42 assert(reg_index < MU_TR_COUNT);
43
Ye Li58a69282023-06-15 18:09:13 +080044 debug("sendmsg tsr 0x%x\n", readl(&mu_base->tsr));
Ye Li0db17f42021-08-07 16:00:41 +080045
46 /* Wait TX register to be empty. */
Ye Li853cc9d2021-08-07 16:00:55 +080047 ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000);
Ye Li0db17f42021-08-07 16:00:41 +080048 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
Ye Li853cc9d2021-08-07 16:00:55 +080055 writel(msg, &mu_base->tr[reg_index]);
Ye Li0db17f42021-08-07 16:00:41 +080056
57 return 0;
58}
59
Ye Li853cc9d2021-08-07 16:00:55 +080060int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg)
Ye Li0db17f42021-08-07 16:00:41 +080061{
Ye Li853cc9d2021-08-07 16:00:55 +080062 struct mu_type *mu_base = (struct mu_type *)base;
Ye Li0db17f42021-08-07 16:00:41 +080063 u32 mask = MU_SR_RF0_MASK << reg_index;
64 u32 val;
65 int ret;
Ye Li58a69282023-06-15 18:09:13 +080066 u32 count = 10;
Ye Li0db17f42021-08-07 16:00:41 +080067
Ye Li606fbf42023-06-15 18:09:12 +080068 assert(reg_index < MU_RR_COUNT);
Ye Li0db17f42021-08-07 16:00:41 +080069
Ye Li58a69282023-06-15 18:09:13 +080070 debug("receivemsg rsr 0x%x\n", readl(&mu_base->rsr));
Ye Li0db17f42021-08-07 16:00:41 +080071
Ye Li58a69282023-06-15 18:09:13 +080072 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) {
Ye Li0db17f42021-08-07 16:00:41 +080084 debug("%s timeout\n", __func__);
85 return -ETIMEDOUT;
86 }
87
Ye Li853cc9d2021-08-07 16:00:55 +080088 *msg = readl(&mu_base->rr[reg_index]);
Ye Li0db17f42021-08-07 16:00:41 +080089
90 debug("rr[%d] 0x%x\n", reg_index, *msg);
91
92 return 0;
93}
94
95static int imx8ulp_mu_read(struct mu_type *base, void *data)
96{
Peng Fand5c31832023-06-15 18:09:05 +080097 struct ele_msg *msg = (struct ele_msg *)data;
Ye Li0db17f42021-08-07 16:00:41 +080098 int ret;
99 u8 count = 0;
100
101 if (!msg)
102 return -EINVAL;
103
104 /* Read first word */
Ye Li853cc9d2021-08-07 16:00:55 +0800105 ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg);
Ye Li0db17f42021-08-07 16:00:41 +0800106 if (ret)
107 return ret;
108 count++;
109
110 /* Check size */
Peng Fand5c31832023-06-15 18:09:05 +0800111 if (msg->size > ELE_MAX_MSG) {
Ye Li0db17f42021-08-07 16:00:41 +0800112 *((u32 *)msg) = 0;
113 return -EINVAL;
114 }
115
116 /* Read remaining words */
117 while (count < msg->size) {
Ye Li853cc9d2021-08-07 16:00:55 +0800118 ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT,
Ye Li0db17f42021-08-07 16:00:41 +0800119 &msg->data[count - 1]);
120 if (ret)
121 return ret;
122 count++;
123 }
124
125 return 0;
126}
127
128static int imx8ulp_mu_write(struct mu_type *base, void *data)
129{
Peng Fand5c31832023-06-15 18:09:05 +0800130 struct ele_msg *msg = (struct ele_msg *)data;
Ye Li0db17f42021-08-07 16:00:41 +0800131 int ret;
132 u8 count = 0;
133
134 if (!msg)
135 return -EINVAL;
136
137 /* Check size */
Peng Fand5c31832023-06-15 18:09:05 +0800138 if (msg->size > ELE_MAX_MSG)
Ye Li0db17f42021-08-07 16:00:41 +0800139 return -EINVAL;
140
141 /* Write first word */
Ye Li853cc9d2021-08-07 16:00:55 +0800142 ret = mu_hal_sendmsg((ulong)base, 0, *((u32 *)msg));
Ye Li0db17f42021-08-07 16:00:41 +0800143 if (ret)
144 return ret;
145 count++;
146
147 /* Write remaining words */
148 while (count < msg->size) {
Ye Li853cc9d2021-08-07 16:00:55 +0800149 ret = mu_hal_sendmsg((ulong)base, count % MU_TR_COUNT,
Ye Li0db17f42021-08-07 16:00:41 +0800150 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 */
163static 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
Peng Fand5c31832023-06-15 18:09:05 +0800183 result = ((struct ele_msg *)rx_msg)->data[0];
Ye Li79581a62021-08-07 16:00:51 +0800184 if ((result & 0xff) == 0xd6)
Ye Li0db17f42021-08-07 16:00:41 +0800185 return 0;
186
187 return -EIO;
188}
189
190static 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 */
Ye Li853cc9d2021-08-07 16:00:55 +0800206 mu_hal_init((ulong)priv->base);
Ye Li0db17f42021-08-07 16:00:41 +0800207
Peng Fand5c31832023-06-15 18:09:05 +0800208 gd->arch.ele_dev = dev;
Ye Li0db17f42021-08-07 16:00:41 +0800209
210 return 0;
211}
212
213static int imx8ulp_mu_remove(struct udevice *dev)
214{
215 return 0;
216}
217
218static int imx8ulp_mu_bind(struct udevice *dev)
219{
220 debug("%s(dev=%p)\n", __func__, dev);
221
222 return 0;
223}
224
225static struct misc_ops imx8ulp_mu_ops = {
226 .call = imx8ulp_mu_call,
227};
228
229static const struct udevice_id imx8ulp_mu_ids[] = {
230 { .compatible = "fsl,imx8ulp-mu" },
Peng Fan1e9aff12022-07-26 16:40:50 +0800231 { .compatible = "fsl,imx93-mu-s4" },
Ye Li0db17f42021-08-07 16:00:41 +0800232 { }
233};
234
235U_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};