1 // SPDX-License-Identifier: GPL-2.0+
13 #include <turris-omnia-mcu-interface.h>
14 #include <asm/byteorder.h>
16 #include <linux/delay.h>
17 #include <linux/log2.h>
19 #define CMD_TRNG_MAX_ENTROPY_LEN 64
21 struct turris_omnia_mcu_info {
25 static int omnia_gpio_get_function(struct udevice *dev, uint offset)
27 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
33 case ilog2(STS_USB30_PWRON):
34 case ilog2(STS_USB31_PWRON):
35 case ilog2(STS_ENABLE_4V5):
36 case ilog2(STS_BUTTON_MODE):
42 /* bank 1 - supported only when FEAT_EXT_CMDS is set */
43 case (16 + 0) ... (16 + 31):
44 if (!(info->features & FEAT_EXT_CMDS))
48 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
49 case (16 + 32 + 0) ... (16 + 32 + 15):
50 if (!(info->features & FEAT_EXT_CMDS))
52 if (!(info->features & FEAT_PERIPH_MCU))
61 static int omnia_gpio_get_value(struct udevice *dev, uint offset)
63 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
71 ret = dm_i2c_read(dev->parent, CMD_GET_STATUS_WORD,
72 (void *)&val16, sizeof(val16));
76 return !!(le16_to_cpu(val16) & BIT(offset));
78 /* bank 1 - supported only when FEAT_EXT_CMDS is set */
79 case (16 + 0) ... (16 + 31):
80 if (!(info->features & FEAT_EXT_CMDS))
83 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_STATUS_DWORD,
84 (void *)&val32, sizeof(val32));
88 return !!(le32_to_cpu(val32) & BIT(offset - 16));
90 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
91 case (16 + 32 + 0) ... (16 + 32 + 15):
92 if (!(info->features & FEAT_EXT_CMDS))
94 if (!(info->features & FEAT_PERIPH_MCU))
97 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_CONTROL_STATUS,
98 (void *)&val16, sizeof(val16));
102 return !!(le16_to_cpu(val16) & BIT(offset - 16 - 32));
109 static int omnia_gpio_set_value(struct udevice *dev, uint offset, int value)
111 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
119 case ilog2(STS_USB30_PWRON):
120 valmask8[1] = CTL_USB30_PWRON;
122 case ilog2(STS_USB31_PWRON):
123 valmask8[1] = CTL_USB31_PWRON;
125 case ilog2(STS_ENABLE_4V5):
126 valmask8[1] = CTL_ENABLE_4V5;
128 case ilog2(STS_BUTTON_MODE):
129 valmask8[1] = CTL_BUTTON_MODE;
135 valmask8[0] = value ? valmask8[1] : 0;
137 return dm_i2c_write(dev->parent, CMD_GENERAL_CONTROL, valmask8,
140 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
141 case (16 + 32 + 0) ... (16 + 32 + 15):
142 if (!(info->features & FEAT_EXT_CMDS))
144 if (!(info->features & FEAT_PERIPH_MCU))
147 valmask16[1] = cpu_to_le16(BIT(offset - 16 - 32));
148 valmask16[0] = value ? valmask16[1] : 0;
150 return dm_i2c_write(dev->parent, CMD_EXT_CONTROL,
151 (void *)valmask16, sizeof(valmask16));
158 static int omnia_gpio_direction_input(struct udevice *dev, uint offset)
162 ret = omnia_gpio_get_function(dev, offset);
165 else if (ret != GPIOF_INPUT)
171 static int omnia_gpio_direction_output(struct udevice *dev, uint offset, int value)
175 ret = omnia_gpio_get_function(dev, offset);
178 else if (ret != GPIOF_OUTPUT)
181 return omnia_gpio_set_value(dev, offset, value);
184 static int omnia_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
185 struct ofnode_phandle_args *args)
187 uint bank, gpio, flags, offset;
190 if (args->args_count != 3)
193 bank = args->args[0];
194 gpio = args->args[1];
195 flags = args->args[2];
211 offset = 16 + 32 + gpio;
217 ret = omnia_gpio_get_function(dev, offset);
221 desc->offset = offset;
222 desc->flags = gpio_flags_xlate(flags);
227 static const struct dm_gpio_ops omnia_gpio_ops = {
228 .direction_input = omnia_gpio_direction_input,
229 .direction_output = omnia_gpio_direction_output,
230 .get_value = omnia_gpio_get_value,
231 .set_value = omnia_gpio_set_value,
232 .get_function = omnia_gpio_get_function,
233 .xlate = omnia_gpio_xlate,
236 static int omnia_gpio_probe(struct udevice *dev)
238 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
239 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
241 uc_priv->bank_name = "mcu_";
243 if ((info->features & FEAT_EXT_CMDS) && (info->features & FEAT_PERIPH_MCU))
244 uc_priv->gpio_count = 16 + 32 + 16;
245 else if (info->features & FEAT_EXT_CMDS)
246 uc_priv->gpio_count = 16 + 32;
248 uc_priv->gpio_count = 16;
253 U_BOOT_DRIVER(turris_omnia_mcu_gpio) = {
254 .name = "turris-omnia-mcu-gpio",
256 .ops = &omnia_gpio_ops,
257 .probe = omnia_gpio_probe,
260 static int omnia_sysreset_request(struct udevice *dev, enum sysreset_t type)
268 if (type != SYSRESET_POWER_OFF)
269 return -EPROTONOSUPPORT;
271 args.magic = CMD_POWER_OFF_MAGIC;
272 args.arg = CMD_POWER_OFF_POWERON_BUTTON;
273 args.csum = 0xba3b7212;
275 return dm_i2c_write(dev->parent, CMD_POWER_OFF, (void *)&args,
279 static const struct sysreset_ops omnia_sysreset_ops = {
280 .request = omnia_sysreset_request,
283 U_BOOT_DRIVER(turris_omnia_mcu_sysreset) = {
284 .name = "turris-omnia-mcu-sysreset",
285 .id = UCLASS_SYSRESET,
286 .ops = &omnia_sysreset_ops,
289 static int omnia_rng_read(struct udevice *dev, void *data, size_t count)
291 u8 buf[1 + CMD_TRNG_MAX_ENTROPY_LEN];
296 ret = dm_i2c_read(dev->parent, CMD_TRNG_COLLECT_ENTROPY, buf,
301 len = min_t(size_t, buf[0],
302 min_t(size_t, CMD_TRNG_MAX_ENTROPY_LEN, count));
305 /* wait 500ms (fail if interrupted), then try again */
306 for (int i = 0; i < 5; ++i) {
314 memcpy(data, &buf[1], len);
322 static const struct dm_rng_ops omnia_rng_ops = {
323 .read = omnia_rng_read,
326 U_BOOT_DRIVER(turris_omnia_mcu_trng) = {
327 .name = "turris-omnia-mcu-trng",
329 .ops = &omnia_rng_ops,
332 static int turris_omnia_mcu_bind(struct udevice *dev)
334 /* bind MCU GPIOs as a child device */
335 return device_bind_driver_to_node(dev, "turris-omnia-mcu-gpio",
336 "turris-omnia-mcu-gpio",
337 dev_ofnode(dev), NULL);
340 static int turris_omnia_mcu_probe(struct udevice *dev)
342 struct turris_omnia_mcu_info *info = dev_get_priv(dev);
347 ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, (void *)&word, sizeof(word));
349 printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n",
354 if (le16_to_cpu(word) & STS_FEATURES_SUPPORTED) {
355 /* try read 32-bit features */
356 ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&dword,
359 /* try read 16-bit features */
360 ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&word,
363 printf("Error: turris_omnia_mcu CMD_GET_FEATURES failed: %d\n",
368 info->features = le16_to_cpu(word);
370 info->features = le32_to_cpu(dword);
371 if (info->features & FEAT_FROM_BIT_16_INVALID)
372 info->features &= GENMASK(15, 0);
376 /* bind sysreset if poweroff is supported */
377 if (info->features & FEAT_POWEROFF_WAKEUP) {
378 ret = device_bind_driver_to_node(dev,
379 "turris-omnia-mcu-sysreset",
380 "turris-omnia-mcu-sysreset",
381 dev_ofnode(dev), NULL);
386 /* bind rng if trng is supported */
387 if (info->features & FEAT_TRNG) {
388 ret = device_bind_driver_to_node(dev, "turris-omnia-mcu-trng",
389 "turris-omnia-mcu-trng",
390 dev_ofnode(dev), NULL);
398 static const struct udevice_id turris_omnia_mcu_ids[] = {
399 { .compatible = "cznic,turris-omnia-mcu" },
403 U_BOOT_DRIVER(turris_omnia_mcu) = {
404 .name = "turris-omnia-mcu",
406 .bind = turris_omnia_mcu_bind,
407 .probe = turris_omnia_mcu_probe,
408 .priv_auto = sizeof(struct turris_omnia_mcu_info),
409 .of_match = turris_omnia_mcu_ids,