Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 1 | // 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 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | struct 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 | |
| 36 | struct 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 45 | void mu_hal_init(ulong base) |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 46 | { |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 47 | struct mu_type *mu_base = (struct mu_type *)base; |
| 48 | |
| 49 | writel(0, &mu_base->tcr); |
| 50 | writel(0, &mu_base->rcr); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 53 | int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg) |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 54 | { |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 55 | struct mu_type *mu_base = (struct mu_type *)base; |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 56 | u32 mask = MU_SR_TE0_MASK << reg_index; |
| 57 | u32 val; |
| 58 | int ret; |
| 59 | |
| 60 | assert(reg_index < MU_TR_COUNT); |
| 61 | |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 62 | debug("sendmsg sr 0x%x\n", readl(&mu_base->sr)); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 63 | |
| 64 | /* Wait TX register to be empty. */ |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 65 | ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 66 | 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 73 | writel(msg, &mu_base->tr[reg_index]); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 78 | int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg) |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 79 | { |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 80 | struct mu_type *mu_base = (struct mu_type *)base; |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 81 | u32 mask = MU_SR_RF0_MASK << reg_index; |
| 82 | u32 val; |
| 83 | int ret; |
| 84 | |
| 85 | assert(reg_index < MU_TR_COUNT); |
| 86 | |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 87 | debug("receivemsg sr 0x%x\n", readl(&mu_base->sr)); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 88 | |
| 89 | /* Wait RX register to be full. */ |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 90 | ret = readl_poll_timeout(&mu_base->rsr, val, val & mask, 10000); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 91 | if (ret < 0) { |
| 92 | debug("%s timeout\n", __func__); |
| 93 | return -ETIMEDOUT; |
| 94 | } |
| 95 | |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 96 | *msg = readl(&mu_base->rr[reg_index]); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 97 | |
| 98 | debug("rr[%d] 0x%x\n", reg_index, *msg); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 113 | ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 114 | 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 126 | ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT, |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 127 | &msg->data[count - 1]); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | count++; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 150 | ret = mu_hal_sendmsg((ulong)base, 0, *((u32 *)msg)); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 151 | if (ret) |
| 152 | return ret; |
| 153 | count++; |
| 154 | |
| 155 | /* Write remaining words */ |
| 156 | while (count < msg->size) { |
Ye Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 157 | ret = mu_hal_sendmsg((ulong)base, count % MU_TR_COUNT, |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 158 | 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 | */ |
| 171 | static 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 Li | 79581a6 | 2021-08-07 16:00:51 +0800 | [diff] [blame] | 192 | if ((result & 0xff) == 0xd6) |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 193 | return 0; |
| 194 | |
| 195 | return -EIO; |
| 196 | } |
| 197 | |
| 198 | static 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 Li | 853cc9d | 2021-08-07 16:00:55 +0800 | [diff] [blame] | 214 | mu_hal_init((ulong)priv->base); |
Ye Li | 0db17f4 | 2021-08-07 16:00:41 +0800 | [diff] [blame] | 215 | |
| 216 | gd->arch.s400_dev = dev; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int imx8ulp_mu_remove(struct udevice *dev) |
| 222 | { |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int imx8ulp_mu_bind(struct udevice *dev) |
| 227 | { |
| 228 | debug("%s(dev=%p)\n", __func__, dev); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static struct misc_ops imx8ulp_mu_ops = { |
| 234 | .call = imx8ulp_mu_call, |
| 235 | }; |
| 236 | |
| 237 | static const struct udevice_id imx8ulp_mu_ids[] = { |
| 238 | { .compatible = "fsl,imx8ulp-mu" }, |
| 239 | { } |
| 240 | }; |
| 241 | |
| 242 | U_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 | }; |