blob: 6b2f17c0002d89893f1cb5e483d2535d1b8415c4 [file] [log] [blame]
Pali Roháreb96c0f2022-07-28 13:06:24 +02001// SPDX-License-Identifier: GPL-2.0+
Marek Behúnea51ee52024-04-04 09:51:03 +02002/*
3 * Copyright (C) 2022 Pali Rohár <pali@kernel.org>
4 * Copyright (C) 2024 Marek Behún <kabel@kernel.org>
5 */
Pali Roháreb96c0f2022-07-28 13:06:24 +02006
7#include <common.h>
Marek Behún4f552fe2024-04-04 09:51:06 +02008#include <console.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +02009#include <dm.h>
Marek Behúnea51ee52024-04-04 09:51:03 +020010#include <dm/lists.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020011#include <i2c.h>
Marek Behún4f552fe2024-04-04 09:51:06 +020012#include <rng.h>
Marek Behúnea51ee52024-04-04 09:51:03 +020013#include <sysreset.h>
Marek Behúnbb42a5b2024-04-04 09:50:51 +020014#include <turris-omnia-mcu-interface.h>
Marek Behúnd38a2be2024-04-04 09:51:01 +020015#include <asm/byteorder.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020016#include <asm/gpio.h>
Marek Behún4f552fe2024-04-04 09:51:06 +020017#include <linux/delay.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020018#include <linux/log2.h>
19
Marek Behún4f552fe2024-04-04 09:51:06 +020020#define CMD_TRNG_MAX_ENTROPY_LEN 64
21
Pali Roháreb96c0f2022-07-28 13:06:24 +020022struct turris_omnia_mcu_info {
Marek Behúnc0132152024-04-04 09:51:02 +020023 u32 features;
Pali Roháreb96c0f2022-07-28 13:06:24 +020024};
25
Marek Behúnea51ee52024-04-04 09:51:03 +020026static int omnia_gpio_get_function(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +020027{
Marek Behúnea51ee52024-04-04 09:51:03 +020028 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Pali Roháreb96c0f2022-07-28 13:06:24 +020029
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ár265f9892022-09-22 13:25:13 +020049 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +020050 case (16 + 32 + 0) ... (16 + 32 + 15):
51 if (!(info->features & FEAT_EXT_CMDS))
52 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +020053 if (!(info->features & FEAT_PERIPH_MCU))
54 return -EINVAL;
Pali Roháreb96c0f2022-07-28 13:06:24 +020055 return GPIOF_OUTPUT;
56
57 default:
58 return -EINVAL;
59 }
60}
61
Marek Behúnea51ee52024-04-04 09:51:03 +020062static int omnia_gpio_get_value(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +020063{
Marek Behúnea51ee52024-04-04 09:51:03 +020064 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Marek Behúnd38a2be2024-04-04 09:51:01 +020065 u32 val32;
66 u16 val16;
Pali Roháreb96c0f2022-07-28 13:06:24 +020067 int ret;
68
69 switch (offset) {
70 /* bank 0 */
71 case 0 ... 15:
Marek Behúnea51ee52024-04-04 09:51:03 +020072 ret = dm_i2c_read(dev->parent, CMD_GET_STATUS_WORD,
73 (void *)&val16, sizeof(val16));
Pali Roháreb96c0f2022-07-28 13:06:24 +020074 if (ret)
75 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +020076
77 return !!(le16_to_cpu(val16) & BIT(offset));
Pali Roháreb96c0f2022-07-28 13:06:24 +020078
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únd38a2be2024-04-04 09:51:01 +020083
Marek Behúnea51ee52024-04-04 09:51:03 +020084 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_STATUS_DWORD,
85 (void *)&val32, sizeof(val32));
Pali Roháreb96c0f2022-07-28 13:06:24 +020086 if (ret)
87 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +020088
89 return !!(le32_to_cpu(val32) & BIT(offset - 16));
Pali Roháreb96c0f2022-07-28 13:06:24 +020090
Pali Rohár265f9892022-09-22 13:25:13 +020091 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +020092 case (16 + 32 + 0) ... (16 + 32 + 15):
93 if (!(info->features & FEAT_EXT_CMDS))
94 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +020095 if (!(info->features & FEAT_PERIPH_MCU))
96 return -EINVAL;
Marek Behúnd38a2be2024-04-04 09:51:01 +020097
Marek Behúnea51ee52024-04-04 09:51:03 +020098 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_CONTROL_STATUS,
Marek Behúnd38a2be2024-04-04 09:51:01 +020099 (void *)&val16, sizeof(val16));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200100 if (ret)
101 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +0200102
103 return !!(le16_to_cpu(val16) & BIT(offset - 16 - 32));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200104
105 default:
106 return -EINVAL;
107 }
108}
109
Marek Behúnea51ee52024-04-04 09:51:03 +0200110static int omnia_gpio_set_value(struct udevice *dev, uint offset, int value)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200111{
Marek Behúnea51ee52024-04-04 09:51:03 +0200112 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Marek Behúnd38a2be2024-04-04 09:51:01 +0200113 u16 valmask16[2];
114 u8 valmask8[2];
Pali Roháreb96c0f2022-07-28 13:06:24 +0200115
116 switch (offset) {
117 /* bank 0 */
Pali Rohára93fa242022-08-01 12:11:13 +0200118 case 0 ... 15:
119 switch (offset) {
120 case ilog2(STS_USB30_PWRON):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200121 valmask8[1] = CTL_USB30_PWRON;
Pali Rohára93fa242022-08-01 12:11:13 +0200122 break;
123 case ilog2(STS_USB31_PWRON):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200124 valmask8[1] = CTL_USB31_PWRON;
Pali Rohára93fa242022-08-01 12:11:13 +0200125 break;
126 case ilog2(STS_ENABLE_4V5):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200127 valmask8[1] = CTL_ENABLE_4V5;
Pali Rohára93fa242022-08-01 12:11:13 +0200128 break;
129 case ilog2(STS_BUTTON_MODE):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200130 valmask8[1] = CTL_BUTTON_MODE;
Pali Rohára93fa242022-08-01 12:11:13 +0200131 break;
132 default:
133 return -EINVAL;
134 }
Marek Behúnd38a2be2024-04-04 09:51:01 +0200135
136 valmask8[0] = value ? valmask8[1] : 0;
137
Marek Behúnea51ee52024-04-04 09:51:03 +0200138 return dm_i2c_write(dev->parent, CMD_GENERAL_CONTROL, valmask8,
Marek Behúnd38a2be2024-04-04 09:51:01 +0200139 sizeof(valmask8));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200140
Pali Rohár265f9892022-09-22 13:25:13 +0200141 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +0200142 case (16 + 32 + 0) ... (16 + 32 + 15):
143 if (!(info->features & FEAT_EXT_CMDS))
144 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +0200145 if (!(info->features & FEAT_PERIPH_MCU))
146 return -EINVAL;
Marek Behúnd38a2be2024-04-04 09:51:01 +0200147
148 valmask16[1] = cpu_to_le16(BIT(offset - 16 - 32));
149 valmask16[0] = value ? valmask16[1] : 0;
150
Marek Behúnea51ee52024-04-04 09:51:03 +0200151 return dm_i2c_write(dev->parent, CMD_EXT_CONTROL,
152 (void *)valmask16, sizeof(valmask16));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200153
154 default:
155 return -EINVAL;
156 }
Pali Roháreb96c0f2022-07-28 13:06:24 +0200157}
158
Marek Behúnea51ee52024-04-04 09:51:03 +0200159static int omnia_gpio_direction_input(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200160{
161 int ret;
162
Marek Behúnea51ee52024-04-04 09:51:03 +0200163 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200164 if (ret < 0)
165 return ret;
166 else if (ret != GPIOF_INPUT)
167 return -EOPNOTSUPP;
168
169 return 0;
170}
171
Marek Behúnea51ee52024-04-04 09:51:03 +0200172static int omnia_gpio_direction_output(struct udevice *dev, uint offset, int value)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200173{
174 int ret;
175
Marek Behúnea51ee52024-04-04 09:51:03 +0200176 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200177 if (ret < 0)
178 return ret;
179 else if (ret != GPIOF_OUTPUT)
180 return -EOPNOTSUPP;
181
Marek Behúnea51ee52024-04-04 09:51:03 +0200182 return omnia_gpio_set_value(dev, offset, value);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200183}
184
Marek Behúnea51ee52024-04-04 09:51:03 +0200185static int omnia_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Pali Roháreb96c0f2022-07-28 13:06:24 +0200186 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únea51ee52024-04-04 09:51:03 +0200218 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200219 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únea51ee52024-04-04 09:51:03 +0200228static 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áreb96c0f2022-07-28 13:06:24 +0200235};
236
Marek Behúnea51ee52024-04-04 09:51:03 +0200237static int omnia_gpio_probe(struct udevice *dev)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200238{
Marek Behúnea51ee52024-04-04 09:51:03 +0200239 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200240 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Marek Behúnea51ee52024-04-04 09:51:03 +0200241
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
254U_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
261static 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
280static const struct sysreset_ops omnia_sysreset_ops = {
281 .request = omnia_sysreset_request,
282};
283
284U_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ún4f552fe2024-04-04 09:51:06 +0200290static 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
323static const struct dm_rng_ops omnia_rng_ops = {
324 .read = omnia_rng_read,
325};
326
327U_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únea51ee52024-04-04 09:51:03 +0200333static 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
341static int turris_omnia_mcu_probe(struct udevice *dev)
342{
343 struct turris_omnia_mcu_info *info = dev_get_priv(dev);
Marek Behúnc0132152024-04-04 09:51:02 +0200344 u32 dword;
345 u16 word;
Pali Roháreb96c0f2022-07-28 13:06:24 +0200346 int ret;
347
Marek Behúnc0132152024-04-04 09:51:02 +0200348 ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, (void *)&word, sizeof(word));
Marek Behúnd38a2be2024-04-04 09:51:01 +0200349 if (ret < 0) {
350 printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n",
351 ret);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200352 return ret;
353 }
354
Marek Behúnc0132152024-04-04 09:51:02 +0200355 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únd38a2be2024-04-04 09:51:01 +0200359 if (ret < 0) {
Marek Behúnc0132152024-04-04 09:51:02 +0200360 /* 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áreb96c0f2022-07-28 13:06:24 +0200374 }
Pali Roháreb96c0f2022-07-28 13:06:24 +0200375 }
376
Marek Behúnea51ee52024-04-04 09:51:03 +0200377 /* 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áreb96c0f2022-07-28 13:06:24 +0200386
Marek Behún4f552fe2024-04-04 09:51:06 +0200387 /* 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áreb96c0f2022-07-28 13:06:24 +0200396 return 0;
397}
398
399static const struct udevice_id turris_omnia_mcu_ids[] = {
400 { .compatible = "cznic,turris-omnia-mcu" },
401 { }
402};
403
404U_BOOT_DRIVER(turris_omnia_mcu) = {
405 .name = "turris-omnia-mcu",
Marek Behúnea51ee52024-04-04 09:51:03 +0200406 .id = UCLASS_MISC,
407 .bind = turris_omnia_mcu_bind,
Pali Roháreb96c0f2022-07-28 13:06:24 +0200408 .probe = turris_omnia_mcu_probe,
Marek Behúnea51ee52024-04-04 09:51:03 +0200409 .priv_auto = sizeof(struct turris_omnia_mcu_info),
Pali Roháreb96c0f2022-07-28 13:06:24 +0200410 .of_match = turris_omnia_mcu_ids,
411};