Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright (C) 2022 Pali Rohár <pali@kernel.org> * Copyright (C) 2024 Marek Behún <kabel@kernel.org> */ #include <console.h> #include <dm.h> #include <dm/lists.h> #include <i2c.h> #include <rng.h> #include <sysreset.h> #include <turris-omnia-mcu-interface.h> #include <asm/byteorder.h> #include <asm/gpio.h> #include <linux/delay.h> #include <linux/log2.h> #define CMD_TRNG_MAX_ENTROPY_LEN 64 struct turris_omnia_mcu_info { u32 features; }; static int omnia_gpio_get_function(struct udevice *dev, uint offset) { struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent); switch (offset) { /* bank 0 */ case 0 ... 15: switch (offset) { case ilog2(STS_USB30_PWRON): case ilog2(STS_USB31_PWRON): case ilog2(STS_ENABLE_4V5): case ilog2(STS_BUTTON_MODE): return GPIOF_OUTPUT; default: return GPIOF_INPUT; } /* bank 1 - supported only when FEAT_EXT_CMDS is set */ case (16 + 0) ... (16 + 31): if (!(info->features & FEAT_EXT_CMDS)) return -EINVAL; return GPIOF_INPUT; /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */ case (16 + 32 + 0) ... (16 + 32 + 15): if (!(info->features & FEAT_EXT_CMDS)) return -EINVAL; if (!(info->features & FEAT_PERIPH_MCU)) return -EINVAL; return GPIOF_OUTPUT; default: return -EINVAL; } } static int omnia_gpio_get_value(struct udevice *dev, uint offset) { struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent); u32 val32; u16 val16; int ret; switch (offset) { /* bank 0 */ case 0 ... 15: ret = dm_i2c_read(dev->parent, CMD_GET_STATUS_WORD, (void *)&val16, sizeof(val16)); if (ret) return ret; return !!(le16_to_cpu(val16) & BIT(offset)); /* bank 1 - supported only when FEAT_EXT_CMDS is set */ case (16 + 0) ... (16 + 31): if (!(info->features & FEAT_EXT_CMDS)) return -EINVAL; ret = dm_i2c_read(dev->parent, CMD_GET_EXT_STATUS_DWORD, (void *)&val32, sizeof(val32)); if (ret) return ret; return !!(le32_to_cpu(val32) & BIT(offset - 16)); /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */ case (16 + 32 + 0) ... (16 + 32 + 15): if (!(info->features & FEAT_EXT_CMDS)) return -EINVAL; if (!(info->features & FEAT_PERIPH_MCU)) return -EINVAL; ret = dm_i2c_read(dev->parent, CMD_GET_EXT_CONTROL_STATUS, (void *)&val16, sizeof(val16)); if (ret) return ret; return !!(le16_to_cpu(val16) & BIT(offset - 16 - 32)); default: return -EINVAL; } } static int omnia_gpio_set_value(struct udevice *dev, uint offset, int value) { struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent); u16 valmask16[2]; u8 valmask8[2]; switch (offset) { /* bank 0 */ case 0 ... 15: switch (offset) { case ilog2(STS_USB30_PWRON): valmask8[1] = CTL_USB30_PWRON; break; case ilog2(STS_USB31_PWRON): valmask8[1] = CTL_USB31_PWRON; break; case ilog2(STS_ENABLE_4V5): valmask8[1] = CTL_ENABLE_4V5; break; case ilog2(STS_BUTTON_MODE): valmask8[1] = CTL_BUTTON_MODE; break; default: return -EINVAL; } valmask8[0] = value ? valmask8[1] : 0; return dm_i2c_write(dev->parent, CMD_GENERAL_CONTROL, valmask8, sizeof(valmask8)); /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */ case (16 + 32 + 0) ... (16 + 32 + 15): if (!(info->features & FEAT_EXT_CMDS)) return -EINVAL; if (!(info->features & FEAT_PERIPH_MCU)) return -EINVAL; valmask16[1] = cpu_to_le16(BIT(offset - 16 - 32)); valmask16[0] = value ? valmask16[1] : 0; return dm_i2c_write(dev->parent, CMD_EXT_CONTROL, (void *)valmask16, sizeof(valmask16)); default: return -EINVAL; } } static int omnia_gpio_direction_input(struct udevice *dev, uint offset) { int ret; ret = omnia_gpio_get_function(dev, offset); if (ret < 0) return ret; else if (ret != GPIOF_INPUT) return -EOPNOTSUPP; return 0; } static int omnia_gpio_direction_output(struct udevice *dev, uint offset, int value) { int ret; ret = omnia_gpio_get_function(dev, offset); if (ret < 0) return ret; else if (ret != GPIOF_OUTPUT) return -EOPNOTSUPP; return omnia_gpio_set_value(dev, offset, value); } static int omnia_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, struct ofnode_phandle_args *args) { uint bank, gpio, flags, offset; int ret; if (args->args_count != 3) return -EINVAL; bank = args->args[0]; gpio = args->args[1]; flags = args->args[2]; switch (bank) { case 0: if (gpio >= 16) return -EINVAL; offset = gpio; break; case 1: if (gpio >= 32) return -EINVAL; offset = 16 + gpio; break; case 2: if (gpio >= 16) return -EINVAL; offset = 16 + 32 + gpio; break; default: return -EINVAL; } ret = omnia_gpio_get_function(dev, offset); if (ret < 0) return ret; desc->offset = offset; desc->flags = gpio_flags_xlate(flags); return 0; } static const struct dm_gpio_ops omnia_gpio_ops = { .direction_input = omnia_gpio_direction_input, .direction_output = omnia_gpio_direction_output, .get_value = omnia_gpio_get_value, .set_value = omnia_gpio_set_value, .get_function = omnia_gpio_get_function, .xlate = omnia_gpio_xlate, }; static int omnia_gpio_probe(struct udevice *dev) { struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent); struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); uc_priv->bank_name = "mcu_"; if ((info->features & FEAT_EXT_CMDS) && (info->features & FEAT_PERIPH_MCU)) uc_priv->gpio_count = 16 + 32 + 16; else if (info->features & FEAT_EXT_CMDS) uc_priv->gpio_count = 16 + 32; else uc_priv->gpio_count = 16; return 0; } U_BOOT_DRIVER(turris_omnia_mcu_gpio) = { .name = "turris-omnia-mcu-gpio", .id = UCLASS_GPIO, .ops = &omnia_gpio_ops, .probe = omnia_gpio_probe, }; static int omnia_sysreset_request(struct udevice *dev, enum sysreset_t type) { struct { u16 magic; u16 arg; u32 csum; } __packed args; if (type != SYSRESET_POWER_OFF) return -EPROTONOSUPPORT; args.magic = CMD_POWER_OFF_MAGIC; args.arg = CMD_POWER_OFF_POWERON_BUTTON; args.csum = 0xba3b7212; return dm_i2c_write(dev->parent, CMD_POWER_OFF, (void *)&args, sizeof(args)); } static const struct sysreset_ops omnia_sysreset_ops = { .request = omnia_sysreset_request, }; U_BOOT_DRIVER(turris_omnia_mcu_sysreset) = { .name = "turris-omnia-mcu-sysreset", .id = UCLASS_SYSRESET, .ops = &omnia_sysreset_ops, }; static int omnia_rng_read(struct udevice *dev, void *data, size_t count) { u8 buf[1 + CMD_TRNG_MAX_ENTROPY_LEN]; size_t len; int ret; while (count) { ret = dm_i2c_read(dev->parent, CMD_TRNG_COLLECT_ENTROPY, buf, sizeof(buf)); if (ret) return ret; len = min_t(size_t, buf[0], min_t(size_t, CMD_TRNG_MAX_ENTROPY_LEN, count)); if (!len) { /* wait 500ms (fail if interrupted), then try again */ for (int i = 0; i < 5; ++i) { mdelay(100); if (ctrlc()) return -EINTR; } continue; } memcpy(data, &buf[1], len); data += len; count -= len; } return 0; } static const struct dm_rng_ops omnia_rng_ops = { .read = omnia_rng_read, }; U_BOOT_DRIVER(turris_omnia_mcu_trng) = { .name = "turris-omnia-mcu-trng", .id = UCLASS_RNG, .ops = &omnia_rng_ops, }; static int turris_omnia_mcu_bind(struct udevice *dev) { /* bind MCU GPIOs as a child device */ return device_bind_driver_to_node(dev, "turris-omnia-mcu-gpio", "turris-omnia-mcu-gpio", dev_ofnode(dev), NULL); } static int turris_omnia_mcu_probe(struct udevice *dev) { struct turris_omnia_mcu_info *info = dev_get_priv(dev); u32 dword; u16 word; int ret; ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, (void *)&word, sizeof(word)); if (ret < 0) { printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n", ret); return ret; } if (le16_to_cpu(word) & STS_FEATURES_SUPPORTED) { /* try read 32-bit features */ ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&dword, sizeof(dword)); if (ret < 0) { /* try read 16-bit features */ ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&word, sizeof(word)); if (ret < 0) { printf("Error: turris_omnia_mcu CMD_GET_FEATURES failed: %d\n", ret); return ret; } info->features = le16_to_cpu(word); } else { info->features = le32_to_cpu(dword); if (info->features & FEAT_FROM_BIT_16_INVALID) info->features &= GENMASK(15, 0); } } /* bind sysreset if poweroff is supported */ if (info->features & FEAT_POWEROFF_WAKEUP) { ret = device_bind_driver_to_node(dev, "turris-omnia-mcu-sysreset", "turris-omnia-mcu-sysreset", dev_ofnode(dev), NULL); if (ret < 0) return ret; } /* bind rng if trng is supported */ if (info->features & FEAT_TRNG) { ret = device_bind_driver_to_node(dev, "turris-omnia-mcu-trng", "turris-omnia-mcu-trng", dev_ofnode(dev), NULL); if (ret < 0) return ret; } return 0; } static const struct udevice_id turris_omnia_mcu_ids[] = { { .compatible = "cznic,turris-omnia-mcu" }, { } }; U_BOOT_DRIVER(turris_omnia_mcu) = { .name = "turris-omnia-mcu", .id = UCLASS_MISC, .bind = turris_omnia_mcu_bind, .probe = turris_omnia_mcu_probe, .priv_auto = sizeof(struct turris_omnia_mcu_info), .of_match = turris_omnia_mcu_ids, }; |