Sean Anderson | 6b39d35 | 2022-04-22 14:34:18 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
| 4 | * |
| 5 | * This driver supports the Security Fuse Processor device found on some |
| 6 | * Layerscape processors. At the moment, we only support a few processors. |
| 7 | * This driver was written with reference to the Layerscape SDK User |
| 8 | * Guide [1] and the ATF SFP driver [2]. |
| 9 | * |
| 10 | * [1] https://docs.nxp.com/bundle/GUID-487B2E69-BB19-42CB-AC38-7EF18C0FE3AE/page/GUID-27FC40AD-3321-4A82-B29E-7BB49EE94F23.html |
| 11 | * [2] https://source.codeaurora.org/external/qoriq/qoriq-components/atf/tree/drivers/nxp/sfp?h=github.com/master |
| 12 | */ |
| 13 | |
| 14 | #define LOG_CATEGORY UCLASS_MISC |
Sean Anderson | 6b39d35 | 2022-04-22 14:34:18 -0400 | [diff] [blame] | 15 | #include <clk.h> |
| 16 | #include <fuse.h> |
| 17 | #include <misc.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <dm/device_compat.h> |
| 20 | #include <dm/read.h> |
| 21 | #include <linux/bitfield.h> |
| 22 | #include <power/regulator.h> |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | #define SFP_INGR 0x20 |
| 27 | #define SFP_SVHESR 0x24 |
| 28 | #define SFP_SFPCR 0x28 |
| 29 | |
| 30 | #define SFP_START 0x200 |
| 31 | #define SFP_END 0x284 |
| 32 | #define SFP_SIZE (SFP_END - SFP_START + 4) |
| 33 | |
| 34 | #define SFP_INGR_ERR BIT(8) |
| 35 | #define SFP_INGR_INST GENMASK(7, 0) |
| 36 | |
| 37 | #define SFP_INGR_READFB 0x01 |
| 38 | #define SFP_INGR_PROGFB 0x02 |
| 39 | |
| 40 | #define SFP_SFPCR_PPW GENMASK(15, 0) |
| 41 | |
| 42 | enum ls2_sfp_ioctl { |
| 43 | LS2_SFP_IOCTL_READ, |
| 44 | LS2_SFP_IOCTL_PROG, |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * struct ls2_sfp_priv - private data for LS2 SFP |
| 49 | * @base: Base address of SFP |
| 50 | * @supply: The (optional) supply for TA_PROG_SFP |
| 51 | * @programmed: Whether we've already programmed the fuses since the last |
| 52 | * reset. The SFP has a *very* limited amount of programming |
| 53 | * cycles (two to six, depending on the model), so we try and |
| 54 | * prevent accidentally performing additional programming |
| 55 | * cycles. |
| 56 | * @dirty: Whether the mirror registers have been written to (overridden) |
| 57 | * since we've last read the fuses (either as part of the reset |
| 58 | * process or using a READFB instruction). There is a much larger, |
| 59 | * but still finite, limit on the number of SFP read cycles (around |
| 60 | * 300,000), so we try and minimize reads as well. |
| 61 | */ |
| 62 | struct ls2_sfp_priv { |
| 63 | void __iomem *base; |
| 64 | struct udevice *supply; |
| 65 | bool programmed, dirty; |
| 66 | }; |
| 67 | |
| 68 | static u32 ls2_sfp_readl(struct ls2_sfp_priv *priv, ulong off) |
| 69 | { |
| 70 | u32 val = be32_to_cpu(readl(priv->base + off)); |
| 71 | |
| 72 | log_debug("%08x = readl(%p)\n", val, priv->base + off); |
| 73 | return val; |
| 74 | } |
| 75 | |
| 76 | static void ls2_sfp_writel(struct ls2_sfp_priv *priv, ulong val, ulong off) |
| 77 | { |
| 78 | log_debug("writel(%08lx, %p)\n", val, priv->base + off); |
| 79 | writel(cpu_to_be32(val), priv->base + off); |
| 80 | } |
| 81 | |
| 82 | static bool ls2_sfp_validate(struct udevice *dev, int offset, int size) |
| 83 | { |
| 84 | if (offset < 0 || size < 0) { |
| 85 | dev_notice(dev, "size and offset must be positive\n"); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (offset & 3 || size & 3) { |
| 90 | dev_notice(dev, "size and offset must be multiples of 4\n"); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (offset + size > SFP_SIZE) { |
| 95 | dev_notice(dev, "size + offset must be <= %#x\n", SFP_SIZE); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | static int ls2_sfp_read(struct udevice *dev, int offset, void *buf_bytes, |
| 103 | int size) |
| 104 | { |
| 105 | int i; |
| 106 | struct ls2_sfp_priv *priv = dev_get_priv(dev); |
| 107 | u32 *buf = buf_bytes; |
| 108 | |
| 109 | if (!ls2_sfp_validate(dev, offset, size)) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | for (i = 0; i < size; i += 4) |
| 113 | buf[i >> 2] = ls2_sfp_readl(priv, SFP_START + offset + i); |
| 114 | |
| 115 | return size; |
| 116 | } |
| 117 | |
| 118 | static int ls2_sfp_write(struct udevice *dev, int offset, |
| 119 | const void *buf_bytes, int size) |
| 120 | { |
| 121 | int i; |
| 122 | struct ls2_sfp_priv *priv = dev_get_priv(dev); |
| 123 | const u32 *buf = buf_bytes; |
| 124 | |
| 125 | if (!ls2_sfp_validate(dev, offset, size)) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | for (i = 0; i < size; i += 4) |
| 129 | ls2_sfp_writel(priv, buf[i >> 2], SFP_START + offset + i); |
| 130 | |
| 131 | priv->dirty = true; |
| 132 | return size; |
| 133 | } |
| 134 | |
| 135 | static int ls2_sfp_check_secret(struct udevice *dev) |
| 136 | { |
| 137 | struct ls2_sfp_priv *priv = dev_get_priv(dev); |
| 138 | u32 svhesr = ls2_sfp_readl(priv, SFP_SVHESR); |
| 139 | |
| 140 | if (svhesr) { |
| 141 | dev_warn(dev, "secret value hamming error not zero: %08x\n", |
| 142 | svhesr); |
| 143 | return -EIO; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int ls2_sfp_transaction(struct ls2_sfp_priv *priv, ulong inst) |
| 149 | { |
| 150 | u32 ingr; |
| 151 | |
| 152 | ls2_sfp_writel(priv, inst, SFP_INGR); |
| 153 | |
| 154 | do { |
| 155 | ingr = ls2_sfp_readl(priv, SFP_INGR); |
| 156 | } while (FIELD_GET(SFP_INGR_INST, ingr)); |
| 157 | |
| 158 | return FIELD_GET(SFP_INGR_ERR, ingr) ? -EIO : 0; |
| 159 | } |
| 160 | |
| 161 | static int ls2_sfp_ioctl(struct udevice *dev, unsigned long request, void *buf) |
| 162 | { |
| 163 | int ret; |
| 164 | struct ls2_sfp_priv *priv = dev_get_priv(dev); |
| 165 | |
| 166 | switch (request) { |
| 167 | case LS2_SFP_IOCTL_READ: |
| 168 | if (!priv->dirty) { |
| 169 | dev_dbg(dev, "ignoring read request, since fuses are not dirty\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | ret = ls2_sfp_transaction(priv, SFP_INGR_READFB); |
| 174 | if (ret) { |
| 175 | dev_err(dev, "error reading fuses\n"); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | ls2_sfp_check_secret(dev); |
| 180 | priv->dirty = false; |
| 181 | return 0; |
| 182 | case LS2_SFP_IOCTL_PROG: |
| 183 | if (priv->programmed) { |
| 184 | dev_warn(dev, "fuses already programmed\n"); |
| 185 | return -EPERM; |
| 186 | } |
| 187 | |
| 188 | ret = ls2_sfp_check_secret(dev); |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | |
| 192 | if (priv->supply) { |
| 193 | ret = regulator_set_enable(priv->supply, true); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | ret = ls2_sfp_transaction(priv, SFP_INGR_PROGFB); |
| 199 | priv->programmed = true; |
| 200 | if (priv->supply) |
| 201 | regulator_set_enable(priv->supply, false); |
| 202 | |
| 203 | if (ret) |
| 204 | dev_err(dev, "error programming fuses\n"); |
| 205 | return ret; |
| 206 | default: |
| 207 | dev_dbg(dev, "unknown ioctl %lu\n", request); |
| 208 | return -EINVAL; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static const struct misc_ops ls2_sfp_ops = { |
| 213 | .read = ls2_sfp_read, |
| 214 | .write = ls2_sfp_write, |
| 215 | .ioctl = ls2_sfp_ioctl, |
| 216 | }; |
| 217 | |
| 218 | static int ls2_sfp_probe(struct udevice *dev) |
| 219 | { |
| 220 | int ret; |
| 221 | struct clk clk; |
| 222 | struct ls2_sfp_priv *priv = dev_get_priv(dev); |
| 223 | ulong rate; |
| 224 | |
| 225 | priv->base = dev_read_addr_ptr(dev); |
| 226 | if (!priv->base) { |
| 227 | dev_dbg(dev, "could not read register base\n"); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
Sean Anderson | 6ad89c0 | 2023-01-27 11:54:53 -0500 | [diff] [blame] | 231 | ret = device_get_supply_regulator(dev, "ta-sfp-prog-supply", &priv->supply); |
Sean Anderson | 6b39d35 | 2022-04-22 14:34:18 -0400 | [diff] [blame] | 232 | if (ret && ret != -ENODEV && ret != -ENOSYS) { |
| 233 | dev_dbg(dev, "problem getting supply (err %d)\n", ret); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | ret = clk_get_by_name(dev, "sfp", &clk); |
| 238 | if (ret == -ENOSYS) { |
| 239 | rate = gd->bus_clk / 4; |
| 240 | } else if (ret) { |
| 241 | dev_dbg(dev, "could not get clock (err %d)\n", ret); |
| 242 | return ret; |
| 243 | } else { |
| 244 | ret = clk_enable(&clk); |
| 245 | if (ret) { |
| 246 | dev_dbg(dev, "could not enable clock (err %d)\n", ret); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | rate = clk_get_rate(&clk); |
Sean Anderson | 6b39d35 | 2022-04-22 14:34:18 -0400 | [diff] [blame] | 251 | if (!rate || IS_ERR_VALUE(rate)) { |
| 252 | ret = rate ? rate : -ENOENT; |
| 253 | dev_dbg(dev, "could not get clock rate (err %d)\n", |
| 254 | ret); |
| 255 | return ret; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* sfp clock in MHz * 12 */ |
| 260 | ls2_sfp_writel(priv, FIELD_PREP(SFP_SFPCR_PPW, rate * 12 / 1000000), |
| 261 | SFP_SFPCR); |
| 262 | |
| 263 | ls2_sfp_check_secret(dev); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static const struct udevice_id ls2_sfp_ids[] = { |
| 268 | { .compatible = "fsl,ls1021a-sfp" }, |
| 269 | { } |
| 270 | }; |
| 271 | |
| 272 | U_BOOT_DRIVER(ls2_sfp) = { |
| 273 | .name = "ls2_sfp", |
| 274 | .id = UCLASS_MISC, |
| 275 | .of_match = ls2_sfp_ids, |
| 276 | .probe = ls2_sfp_probe, |
| 277 | .ops = &ls2_sfp_ops, |
| 278 | .priv_auto = sizeof(struct ls2_sfp_priv), |
| 279 | }; |
| 280 | |
| 281 | static int ls2_sfp_device(struct udevice **dev) |
| 282 | { |
| 283 | int ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 284 | DM_DRIVER_GET(ls2_sfp), dev); |
| 285 | |
| 286 | if (ret) |
| 287 | log_debug("device not found (err %d)\n", ret); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | int fuse_read(u32 bank, u32 word, u32 *val) |
| 292 | { |
| 293 | int ret; |
| 294 | struct udevice *dev; |
| 295 | |
| 296 | ret = ls2_sfp_device(&dev); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | |
| 300 | ret = misc_ioctl(dev, LS2_SFP_IOCTL_READ, NULL); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | ret = misc_read(dev, word << 2, val, sizeof(*val)); |
| 305 | return ret < 0 ? ret : 0; |
| 306 | } |
| 307 | |
| 308 | int fuse_sense(u32 bank, u32 word, u32 *val) |
| 309 | { |
| 310 | int ret; |
| 311 | struct udevice *dev; |
| 312 | |
| 313 | ret = ls2_sfp_device(&dev); |
| 314 | if (ret) |
| 315 | return ret; |
| 316 | |
| 317 | ret = misc_read(dev, word << 2, val, sizeof(*val)); |
| 318 | return ret < 0 ? ret : 0; |
| 319 | } |
| 320 | |
| 321 | int fuse_prog(u32 bank, u32 word, u32 val) |
| 322 | { |
| 323 | int ret; |
| 324 | struct udevice *dev; |
| 325 | |
| 326 | ret = ls2_sfp_device(&dev); |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | |
| 330 | ret = misc_write(dev, word << 2, &val, sizeof(val)); |
| 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | |
| 334 | return misc_ioctl(dev, LS2_SFP_IOCTL_PROG, NULL); |
| 335 | } |
| 336 | |
| 337 | int fuse_override(u32 bank, u32 word, u32 val) |
| 338 | { |
| 339 | int ret; |
| 340 | struct udevice *dev; |
| 341 | |
| 342 | ret = ls2_sfp_device(&dev); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | |
| 346 | ret = misc_write(dev, word << 2, &val, sizeof(val)); |
| 347 | return ret < 0 ? ret : 0; |
| 348 | } |