blob: be77acbd16529d34fdbfa869bfaed849dcc5ca95 [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
Marek Behún4f552fe2024-04-04 09:51:06 +02007#include <console.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +02008#include <dm.h>
Marek Behúnea51ee52024-04-04 09:51:03 +02009#include <dm/lists.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020010#include <i2c.h>
Marek Behún4f552fe2024-04-04 09:51:06 +020011#include <rng.h>
Marek Behúnea51ee52024-04-04 09:51:03 +020012#include <sysreset.h>
Marek Behúnbb42a5b2024-04-04 09:50:51 +020013#include <turris-omnia-mcu-interface.h>
Marek Behúnd38a2be2024-04-04 09:51:01 +020014#include <asm/byteorder.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020015#include <asm/gpio.h>
Marek Behún4f552fe2024-04-04 09:51:06 +020016#include <linux/delay.h>
Pali Roháreb96c0f2022-07-28 13:06:24 +020017#include <linux/log2.h>
18
Marek Behún4f552fe2024-04-04 09:51:06 +020019#define CMD_TRNG_MAX_ENTROPY_LEN 64
20
Pali Roháreb96c0f2022-07-28 13:06:24 +020021struct turris_omnia_mcu_info {
Marek Behúnc0132152024-04-04 09:51:02 +020022 u32 features;
Pali Roháreb96c0f2022-07-28 13:06:24 +020023};
24
Marek Behúnea51ee52024-04-04 09:51:03 +020025static int omnia_gpio_get_function(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +020026{
Marek Behúnea51ee52024-04-04 09:51:03 +020027 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Pali Roháreb96c0f2022-07-28 13:06:24 +020028
29 switch (offset) {
30 /* bank 0 */
31 case 0 ... 15:
32 switch (offset) {
33 case ilog2(STS_USB30_PWRON):
34 case ilog2(STS_USB31_PWRON):
35 case ilog2(STS_ENABLE_4V5):
36 case ilog2(STS_BUTTON_MODE):
37 return GPIOF_OUTPUT;
38 default:
39 return GPIOF_INPUT;
40 }
41
42 /* bank 1 - supported only when FEAT_EXT_CMDS is set */
43 case (16 + 0) ... (16 + 31):
44 if (!(info->features & FEAT_EXT_CMDS))
45 return -EINVAL;
46 return GPIOF_INPUT;
47
Pali Rohár265f9892022-09-22 13:25:13 +020048 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +020049 case (16 + 32 + 0) ... (16 + 32 + 15):
50 if (!(info->features & FEAT_EXT_CMDS))
51 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +020052 if (!(info->features & FEAT_PERIPH_MCU))
53 return -EINVAL;
Pali Roháreb96c0f2022-07-28 13:06:24 +020054 return GPIOF_OUTPUT;
55
56 default:
57 return -EINVAL;
58 }
59}
60
Marek Behúnea51ee52024-04-04 09:51:03 +020061static int omnia_gpio_get_value(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +020062{
Marek Behúnea51ee52024-04-04 09:51:03 +020063 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Marek Behúnd38a2be2024-04-04 09:51:01 +020064 u32 val32;
65 u16 val16;
Pali Roháreb96c0f2022-07-28 13:06:24 +020066 int ret;
67
68 switch (offset) {
69 /* bank 0 */
70 case 0 ... 15:
Marek Behúnea51ee52024-04-04 09:51:03 +020071 ret = dm_i2c_read(dev->parent, CMD_GET_STATUS_WORD,
72 (void *)&val16, sizeof(val16));
Pali Roháreb96c0f2022-07-28 13:06:24 +020073 if (ret)
74 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +020075
76 return !!(le16_to_cpu(val16) & BIT(offset));
Pali Roháreb96c0f2022-07-28 13:06:24 +020077
78 /* bank 1 - supported only when FEAT_EXT_CMDS is set */
79 case (16 + 0) ... (16 + 31):
80 if (!(info->features & FEAT_EXT_CMDS))
81 return -EINVAL;
Marek Behúnd38a2be2024-04-04 09:51:01 +020082
Marek Behúnea51ee52024-04-04 09:51:03 +020083 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_STATUS_DWORD,
84 (void *)&val32, sizeof(val32));
Pali Roháreb96c0f2022-07-28 13:06:24 +020085 if (ret)
86 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +020087
88 return !!(le32_to_cpu(val32) & BIT(offset - 16));
Pali Roháreb96c0f2022-07-28 13:06:24 +020089
Pali Rohár265f9892022-09-22 13:25:13 +020090 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +020091 case (16 + 32 + 0) ... (16 + 32 + 15):
92 if (!(info->features & FEAT_EXT_CMDS))
93 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +020094 if (!(info->features & FEAT_PERIPH_MCU))
95 return -EINVAL;
Marek Behúnd38a2be2024-04-04 09:51:01 +020096
Marek Behúnea51ee52024-04-04 09:51:03 +020097 ret = dm_i2c_read(dev->parent, CMD_GET_EXT_CONTROL_STATUS,
Marek Behúnd38a2be2024-04-04 09:51:01 +020098 (void *)&val16, sizeof(val16));
Pali Roháreb96c0f2022-07-28 13:06:24 +020099 if (ret)
100 return ret;
Marek Behúnd38a2be2024-04-04 09:51:01 +0200101
102 return !!(le16_to_cpu(val16) & BIT(offset - 16 - 32));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200103
104 default:
105 return -EINVAL;
106 }
107}
108
Marek Behúnea51ee52024-04-04 09:51:03 +0200109static int omnia_gpio_set_value(struct udevice *dev, uint offset, int value)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200110{
Marek Behúnea51ee52024-04-04 09:51:03 +0200111 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Marek Behúnd38a2be2024-04-04 09:51:01 +0200112 u16 valmask16[2];
113 u8 valmask8[2];
Pali Roháreb96c0f2022-07-28 13:06:24 +0200114
115 switch (offset) {
116 /* bank 0 */
Pali Rohára93fa242022-08-01 12:11:13 +0200117 case 0 ... 15:
118 switch (offset) {
119 case ilog2(STS_USB30_PWRON):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200120 valmask8[1] = CTL_USB30_PWRON;
Pali Rohára93fa242022-08-01 12:11:13 +0200121 break;
122 case ilog2(STS_USB31_PWRON):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200123 valmask8[1] = CTL_USB31_PWRON;
Pali Rohára93fa242022-08-01 12:11:13 +0200124 break;
125 case ilog2(STS_ENABLE_4V5):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200126 valmask8[1] = CTL_ENABLE_4V5;
Pali Rohára93fa242022-08-01 12:11:13 +0200127 break;
128 case ilog2(STS_BUTTON_MODE):
Marek Behúnd38a2be2024-04-04 09:51:01 +0200129 valmask8[1] = CTL_BUTTON_MODE;
Pali Rohára93fa242022-08-01 12:11:13 +0200130 break;
131 default:
132 return -EINVAL;
133 }
Marek Behúnd38a2be2024-04-04 09:51:01 +0200134
135 valmask8[0] = value ? valmask8[1] : 0;
136
Marek Behúnea51ee52024-04-04 09:51:03 +0200137 return dm_i2c_write(dev->parent, CMD_GENERAL_CONTROL, valmask8,
Marek Behúnd38a2be2024-04-04 09:51:01 +0200138 sizeof(valmask8));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200139
Pali Rohár265f9892022-09-22 13:25:13 +0200140 /* bank 2 - supported only when FEAT_EXT_CMDS and FEAT_PERIPH_MCU is set */
Pali Roháreb96c0f2022-07-28 13:06:24 +0200141 case (16 + 32 + 0) ... (16 + 32 + 15):
142 if (!(info->features & FEAT_EXT_CMDS))
143 return -EINVAL;
Pali Rohár265f9892022-09-22 13:25:13 +0200144 if (!(info->features & FEAT_PERIPH_MCU))
145 return -EINVAL;
Marek Behúnd38a2be2024-04-04 09:51:01 +0200146
147 valmask16[1] = cpu_to_le16(BIT(offset - 16 - 32));
148 valmask16[0] = value ? valmask16[1] : 0;
149
Marek Behúnea51ee52024-04-04 09:51:03 +0200150 return dm_i2c_write(dev->parent, CMD_EXT_CONTROL,
151 (void *)valmask16, sizeof(valmask16));
Pali Roháreb96c0f2022-07-28 13:06:24 +0200152
153 default:
154 return -EINVAL;
155 }
Pali Roháreb96c0f2022-07-28 13:06:24 +0200156}
157
Marek Behúnea51ee52024-04-04 09:51:03 +0200158static int omnia_gpio_direction_input(struct udevice *dev, uint offset)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200159{
160 int ret;
161
Marek Behúnea51ee52024-04-04 09:51:03 +0200162 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200163 if (ret < 0)
164 return ret;
165 else if (ret != GPIOF_INPUT)
166 return -EOPNOTSUPP;
167
168 return 0;
169}
170
Marek Behúnea51ee52024-04-04 09:51:03 +0200171static int omnia_gpio_direction_output(struct udevice *dev, uint offset, int value)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200172{
173 int ret;
174
Marek Behúnea51ee52024-04-04 09:51:03 +0200175 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200176 if (ret < 0)
177 return ret;
178 else if (ret != GPIOF_OUTPUT)
179 return -EOPNOTSUPP;
180
Marek Behúnea51ee52024-04-04 09:51:03 +0200181 return omnia_gpio_set_value(dev, offset, value);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200182}
183
Marek Behúnea51ee52024-04-04 09:51:03 +0200184static int omnia_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Pali Roháreb96c0f2022-07-28 13:06:24 +0200185 struct ofnode_phandle_args *args)
186{
187 uint bank, gpio, flags, offset;
188 int ret;
189
190 if (args->args_count != 3)
191 return -EINVAL;
192
193 bank = args->args[0];
194 gpio = args->args[1];
195 flags = args->args[2];
196
197 switch (bank) {
198 case 0:
199 if (gpio >= 16)
200 return -EINVAL;
201 offset = gpio;
202 break;
203 case 1:
204 if (gpio >= 32)
205 return -EINVAL;
206 offset = 16 + gpio;
207 break;
208 case 2:
209 if (gpio >= 16)
210 return -EINVAL;
211 offset = 16 + 32 + gpio;
212 break;
213 default:
214 return -EINVAL;
215 }
216
Marek Behúnea51ee52024-04-04 09:51:03 +0200217 ret = omnia_gpio_get_function(dev, offset);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200218 if (ret < 0)
219 return ret;
220
221 desc->offset = offset;
222 desc->flags = gpio_flags_xlate(flags);
223
224 return 0;
225}
226
Marek Behúnea51ee52024-04-04 09:51:03 +0200227static 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,
Pali Roháreb96c0f2022-07-28 13:06:24 +0200234};
235
Marek Behúnea51ee52024-04-04 09:51:03 +0200236static int omnia_gpio_probe(struct udevice *dev)
Pali Roháreb96c0f2022-07-28 13:06:24 +0200237{
Marek Behúnea51ee52024-04-04 09:51:03 +0200238 struct turris_omnia_mcu_info *info = dev_get_priv(dev->parent);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200239 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Marek Behúnea51ee52024-04-04 09:51:03 +0200240
241 uc_priv->bank_name = "mcu_";
242
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;
247 else
248 uc_priv->gpio_count = 16;
249
250 return 0;
251}
252
253U_BOOT_DRIVER(turris_omnia_mcu_gpio) = {
254 .name = "turris-omnia-mcu-gpio",
255 .id = UCLASS_GPIO,
256 .ops = &omnia_gpio_ops,
257 .probe = omnia_gpio_probe,
258};
259
260static int omnia_sysreset_request(struct udevice *dev, enum sysreset_t type)
261{
262 struct {
263 u16 magic;
264 u16 arg;
265 u32 csum;
266 } __packed args;
267
268 if (type != SYSRESET_POWER_OFF)
269 return -EPROTONOSUPPORT;
270
271 args.magic = CMD_POWER_OFF_MAGIC;
272 args.arg = CMD_POWER_OFF_POWERON_BUTTON;
273 args.csum = 0xba3b7212;
274
275 return dm_i2c_write(dev->parent, CMD_POWER_OFF, (void *)&args,
276 sizeof(args));
277}
278
279static const struct sysreset_ops omnia_sysreset_ops = {
280 .request = omnia_sysreset_request,
281};
282
283U_BOOT_DRIVER(turris_omnia_mcu_sysreset) = {
284 .name = "turris-omnia-mcu-sysreset",
285 .id = UCLASS_SYSRESET,
286 .ops = &omnia_sysreset_ops,
287};
288
Marek Behún4f552fe2024-04-04 09:51:06 +0200289static int omnia_rng_read(struct udevice *dev, void *data, size_t count)
290{
291 u8 buf[1 + CMD_TRNG_MAX_ENTROPY_LEN];
292 size_t len;
293 int ret;
294
295 while (count) {
296 ret = dm_i2c_read(dev->parent, CMD_TRNG_COLLECT_ENTROPY, buf,
297 sizeof(buf));
298 if (ret)
299 return ret;
300
301 len = min_t(size_t, buf[0],
302 min_t(size_t, CMD_TRNG_MAX_ENTROPY_LEN, count));
303
304 if (!len) {
305 /* wait 500ms (fail if interrupted), then try again */
306 for (int i = 0; i < 5; ++i) {
307 mdelay(100);
308 if (ctrlc())
309 return -EINTR;
310 }
311 continue;
312 }
313
314 memcpy(data, &buf[1], len);
315 data += len;
316 count -= len;
317 }
318
319 return 0;
320}
321
322static const struct dm_rng_ops omnia_rng_ops = {
323 .read = omnia_rng_read,
324};
325
326U_BOOT_DRIVER(turris_omnia_mcu_trng) = {
327 .name = "turris-omnia-mcu-trng",
328 .id = UCLASS_RNG,
329 .ops = &omnia_rng_ops,
330};
331
Marek Behúnea51ee52024-04-04 09:51:03 +0200332static int turris_omnia_mcu_bind(struct udevice *dev)
333{
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);
338}
339
340static int turris_omnia_mcu_probe(struct udevice *dev)
341{
342 struct turris_omnia_mcu_info *info = dev_get_priv(dev);
Marek Behúnc0132152024-04-04 09:51:02 +0200343 u32 dword;
344 u16 word;
Pali Roháreb96c0f2022-07-28 13:06:24 +0200345 int ret;
346
Marek Behúnc0132152024-04-04 09:51:02 +0200347 ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, (void *)&word, sizeof(word));
Marek Behúnd38a2be2024-04-04 09:51:01 +0200348 if (ret < 0) {
349 printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n",
350 ret);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200351 return ret;
352 }
353
Marek Behúnc0132152024-04-04 09:51:02 +0200354 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,
357 sizeof(dword));
Marek Behúnd38a2be2024-04-04 09:51:01 +0200358 if (ret < 0) {
Marek Behúnc0132152024-04-04 09:51:02 +0200359 /* try read 16-bit features */
360 ret = dm_i2c_read(dev, CMD_GET_FEATURES, (void *)&word,
361 sizeof(word));
362 if (ret < 0) {
363 printf("Error: turris_omnia_mcu CMD_GET_FEATURES failed: %d\n",
364 ret);
365 return ret;
366 }
367
368 info->features = le16_to_cpu(word);
369 } else {
370 info->features = le32_to_cpu(dword);
371 if (info->features & FEAT_FROM_BIT_16_INVALID)
372 info->features &= GENMASK(15, 0);
Pali Roháreb96c0f2022-07-28 13:06:24 +0200373 }
Pali Roháreb96c0f2022-07-28 13:06:24 +0200374 }
375
Marek Behúnea51ee52024-04-04 09:51:03 +0200376 /* 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);
382 if (ret < 0)
383 return ret;
384 }
Pali Roháreb96c0f2022-07-28 13:06:24 +0200385
Marek Behún4f552fe2024-04-04 09:51:06 +0200386 /* 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);
391 if (ret < 0)
392 return ret;
393 }
394
Pali Roháreb96c0f2022-07-28 13:06:24 +0200395 return 0;
396}
397
398static const struct udevice_id turris_omnia_mcu_ids[] = {
399 { .compatible = "cznic,turris-omnia-mcu" },
400 { }
401};
402
403U_BOOT_DRIVER(turris_omnia_mcu) = {
404 .name = "turris-omnia-mcu",
Marek Behúnea51ee52024-04-04 09:51:03 +0200405 .id = UCLASS_MISC,
406 .bind = turris_omnia_mcu_bind,
Pali Roháreb96c0f2022-07-28 13:06:24 +0200407 .probe = turris_omnia_mcu_probe,
Marek Behúnea51ee52024-04-04 09:51:03 +0200408 .priv_auto = sizeof(struct turris_omnia_mcu_info),
Pali Roháreb96c0f2022-07-28 13:06:24 +0200409 .of_match = turris_omnia_mcu_ids,
410};