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