Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // (C) 2022 Pali Rohár <pali@kernel.org> |
| 3 | |
| 4 | #include <common.h> |
| 5 | #include <dm.h> |
| 6 | #include <i2c.h> |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 7 | #include <turris-omnia-mcu-interface.h> |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 8 | #include <asm/byteorder.h> |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 9 | #include <asm/gpio.h> |
| 10 | #include <linux/log2.h> |
| 11 | |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 12 | struct turris_omnia_mcu_info { |
Marek Behún | c013215 | 2024-04-04 09:51:02 +0200 | [diff] [blame^] | 13 | u32 features; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | static int turris_omnia_mcu_get_function(struct udevice *dev, uint offset) |
| 17 | { |
| 18 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 19 | |
| 20 | switch (offset) { |
| 21 | /* bank 0 */ |
| 22 | case 0 ... 15: |
| 23 | switch (offset) { |
| 24 | case ilog2(STS_USB30_PWRON): |
| 25 | case ilog2(STS_USB31_PWRON): |
| 26 | case ilog2(STS_ENABLE_4V5): |
| 27 | case ilog2(STS_BUTTON_MODE): |
| 28 | return GPIOF_OUTPUT; |
| 29 | default: |
| 30 | return GPIOF_INPUT; |
| 31 | } |
| 32 | |
| 33 | /* bank 1 - supported only when FEAT_EXT_CMDS is set */ |
| 34 | case (16 + 0) ... (16 + 31): |
| 35 | if (!(info->features & FEAT_EXT_CMDS)) |
| 36 | return -EINVAL; |
| 37 | return GPIOF_INPUT; |
| 38 | |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 39 | /* 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] | 40 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 41 | if (!(info->features & FEAT_EXT_CMDS)) |
| 42 | return -EINVAL; |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 43 | if (!(info->features & FEAT_PERIPH_MCU)) |
| 44 | return -EINVAL; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 45 | return GPIOF_OUTPUT; |
| 46 | |
| 47 | default: |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static int turris_omnia_mcu_get_value(struct udevice *dev, uint offset) |
| 53 | { |
| 54 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 55 | u32 val32; |
| 56 | u16 val16; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 57 | int ret; |
| 58 | |
| 59 | switch (offset) { |
| 60 | /* bank 0 */ |
| 61 | case 0 ... 15: |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 62 | ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, (void *)&val16, |
| 63 | sizeof(val16)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 64 | if (ret) |
| 65 | return ret; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 66 | |
| 67 | return !!(le16_to_cpu(val16) & BIT(offset)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 68 | |
| 69 | /* bank 1 - supported only when FEAT_EXT_CMDS is set */ |
| 70 | case (16 + 0) ... (16 + 31): |
| 71 | if (!(info->features & FEAT_EXT_CMDS)) |
| 72 | return -EINVAL; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 73 | |
| 74 | ret = dm_i2c_read(dev, CMD_GET_EXT_STATUS_DWORD, (void *)&val32, |
| 75 | sizeof(val32)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 76 | if (ret) |
| 77 | return ret; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 78 | |
| 79 | return !!(le32_to_cpu(val32) & BIT(offset - 16)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 80 | |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 81 | /* 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] | 82 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 83 | if (!(info->features & FEAT_EXT_CMDS)) |
| 84 | return -EINVAL; |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 85 | if (!(info->features & FEAT_PERIPH_MCU)) |
| 86 | return -EINVAL; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 87 | |
| 88 | ret = dm_i2c_read(dev, CMD_GET_EXT_CONTROL_STATUS, |
| 89 | (void *)&val16, sizeof(val16)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 90 | if (ret) |
| 91 | return ret; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 92 | |
| 93 | return !!(le16_to_cpu(val16) & BIT(offset - 16 - 32)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 94 | |
| 95 | default: |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static int turris_omnia_mcu_set_value(struct udevice *dev, uint offset, int value) |
| 101 | { |
| 102 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 103 | u16 valmask16[2]; |
| 104 | u8 valmask8[2]; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 105 | |
| 106 | switch (offset) { |
| 107 | /* bank 0 */ |
Pali Rohár | a93fa24 | 2022-08-01 12:11:13 +0200 | [diff] [blame] | 108 | case 0 ... 15: |
| 109 | switch (offset) { |
| 110 | case ilog2(STS_USB30_PWRON): |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 111 | valmask8[1] = CTL_USB30_PWRON; |
Pali Rohár | a93fa24 | 2022-08-01 12:11:13 +0200 | [diff] [blame] | 112 | break; |
| 113 | case ilog2(STS_USB31_PWRON): |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 114 | valmask8[1] = CTL_USB31_PWRON; |
Pali Rohár | a93fa24 | 2022-08-01 12:11:13 +0200 | [diff] [blame] | 115 | break; |
| 116 | case ilog2(STS_ENABLE_4V5): |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 117 | valmask8[1] = CTL_ENABLE_4V5; |
Pali Rohár | a93fa24 | 2022-08-01 12:11:13 +0200 | [diff] [blame] | 118 | break; |
| 119 | case ilog2(STS_BUTTON_MODE): |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 120 | valmask8[1] = CTL_BUTTON_MODE; |
Pali Rohár | a93fa24 | 2022-08-01 12:11:13 +0200 | [diff] [blame] | 121 | break; |
| 122 | default: |
| 123 | return -EINVAL; |
| 124 | } |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 125 | |
| 126 | valmask8[0] = value ? valmask8[1] : 0; |
| 127 | |
| 128 | return dm_i2c_write(dev, CMD_GENERAL_CONTROL, valmask8, |
| 129 | sizeof(valmask8)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 130 | |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 131 | /* 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] | 132 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 133 | if (!(info->features & FEAT_EXT_CMDS)) |
| 134 | return -EINVAL; |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 135 | if (!(info->features & FEAT_PERIPH_MCU)) |
| 136 | return -EINVAL; |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 137 | |
| 138 | valmask16[1] = cpu_to_le16(BIT(offset - 16 - 32)); |
| 139 | valmask16[0] = value ? valmask16[1] : 0; |
| 140 | |
| 141 | return dm_i2c_write(dev, CMD_EXT_CONTROL, (void *)valmask16, |
| 142 | sizeof(valmask16)); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 143 | |
| 144 | default: |
| 145 | return -EINVAL; |
| 146 | } |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int turris_omnia_mcu_direction_input(struct udevice *dev, uint offset) |
| 150 | { |
| 151 | int ret; |
| 152 | |
| 153 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | else if (ret != GPIOF_INPUT) |
| 157 | return -EOPNOTSUPP; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int turris_omnia_mcu_direction_output(struct udevice *dev, uint offset, int value) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 167 | if (ret < 0) |
| 168 | return ret; |
| 169 | else if (ret != GPIOF_OUTPUT) |
| 170 | return -EOPNOTSUPP; |
| 171 | |
| 172 | return turris_omnia_mcu_set_value(dev, offset, value); |
| 173 | } |
| 174 | |
| 175 | static int turris_omnia_mcu_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 176 | struct ofnode_phandle_args *args) |
| 177 | { |
| 178 | uint bank, gpio, flags, offset; |
| 179 | int ret; |
| 180 | |
| 181 | if (args->args_count != 3) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | bank = args->args[0]; |
| 185 | gpio = args->args[1]; |
| 186 | flags = args->args[2]; |
| 187 | |
| 188 | switch (bank) { |
| 189 | case 0: |
| 190 | if (gpio >= 16) |
| 191 | return -EINVAL; |
| 192 | offset = gpio; |
| 193 | break; |
| 194 | case 1: |
| 195 | if (gpio >= 32) |
| 196 | return -EINVAL; |
| 197 | offset = 16 + gpio; |
| 198 | break; |
| 199 | case 2: |
| 200 | if (gpio >= 16) |
| 201 | return -EINVAL; |
| 202 | offset = 16 + 32 + gpio; |
| 203 | break; |
| 204 | default: |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 209 | if (ret < 0) |
| 210 | return ret; |
| 211 | |
| 212 | desc->offset = offset; |
| 213 | desc->flags = gpio_flags_xlate(flags); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static const struct dm_gpio_ops turris_omnia_mcu_ops = { |
| 219 | .direction_input = turris_omnia_mcu_direction_input, |
| 220 | .direction_output = turris_omnia_mcu_direction_output, |
| 221 | .get_value = turris_omnia_mcu_get_value, |
| 222 | .set_value = turris_omnia_mcu_set_value, |
| 223 | .get_function = turris_omnia_mcu_get_function, |
| 224 | .xlate = turris_omnia_mcu_xlate, |
| 225 | }; |
| 226 | |
| 227 | static int turris_omnia_mcu_probe(struct udevice *dev) |
| 228 | { |
| 229 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 230 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Marek Behún | c013215 | 2024-04-04 09:51:02 +0200 | [diff] [blame^] | 231 | u32 dword; |
| 232 | u16 word; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 233 | int ret; |
| 234 | |
Marek Behún | c013215 | 2024-04-04 09:51:02 +0200 | [diff] [blame^] | 235 | 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] | 236 | if (ret < 0) { |
| 237 | printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n", |
| 238 | ret); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 239 | return ret; |
| 240 | } |
| 241 | |
Marek Behún | c013215 | 2024-04-04 09:51:02 +0200 | [diff] [blame^] | 242 | if (le16_to_cpu(word) & STS_FEATURES_SUPPORTED) { |
| 243 | /* try read 32-bit features */ |
| 244 | ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&dword, |
| 245 | sizeof(dword)); |
Marek Behún | d38a2be | 2024-04-04 09:51:01 +0200 | [diff] [blame] | 246 | if (ret < 0) { |
Marek Behún | c013215 | 2024-04-04 09:51:02 +0200 | [diff] [blame^] | 247 | /* try read 16-bit features */ |
| 248 | ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&word, |
| 249 | sizeof(word)); |
| 250 | if (ret < 0) { |
| 251 | printf("Error: turris_omnia_mcu CMD_GET_FEATURES failed: %d\n", |
| 252 | ret); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | info->features = le16_to_cpu(word); |
| 257 | } else { |
| 258 | info->features = le32_to_cpu(dword); |
| 259 | if (info->features & FEAT_FROM_BIT_16_INVALID) |
| 260 | info->features &= GENMASK(15, 0); |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 261 | } |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | uc_priv->bank_name = "mcu_"; |
| 265 | |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 266 | if ((info->features & FEAT_EXT_CMDS) && (info->features & FEAT_PERIPH_MCU)) |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 267 | uc_priv->gpio_count = 16 + 32 + 16; |
Pali Rohár | 265f989 | 2022-09-22 13:25:13 +0200 | [diff] [blame] | 268 | else if (info->features & FEAT_EXT_CMDS) |
| 269 | uc_priv->gpio_count = 16 + 32; |
Pali Rohár | eb96c0f | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 270 | else |
| 271 | uc_priv->gpio_count = 16; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static const struct udevice_id turris_omnia_mcu_ids[] = { |
| 277 | { .compatible = "cznic,turris-omnia-mcu" }, |
| 278 | { } |
| 279 | }; |
| 280 | |
| 281 | U_BOOT_DRIVER(turris_omnia_mcu) = { |
| 282 | .name = "turris-omnia-mcu", |
| 283 | .id = UCLASS_GPIO, |
| 284 | .ops = &turris_omnia_mcu_ops, |
| 285 | .probe = turris_omnia_mcu_probe, |
| 286 | .plat_auto = sizeof(struct turris_omnia_mcu_info), |
| 287 | .of_match = turris_omnia_mcu_ids, |
| 288 | }; |