blob: f7347bbb826ff857c03a9e220d4b6bdc74690b90 [file] [log] [blame]
Marek Vasut5ff05292020-01-24 18:39:16 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
Marek Vasut5ff05292020-01-24 18:39:16 +01006#include <adc.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -06008#include <net.h>
Marek Vasut5ff05292020-01-24 18:39:16 +01009#include <asm/arch/stm32.h>
10#include <asm/arch/sys_proto.h>
11#include <asm/gpio.h>
12#include <asm/io.h>
13#include <bootm.h>
14#include <clk.h>
15#include <config.h>
16#include <dm.h>
17#include <dm/device.h>
18#include <dm/uclass.h>
19#include <env.h>
20#include <env_internal.h>
21#include <g_dnl.h>
22#include <generic-phy.h>
23#include <hang.h>
24#include <i2c.h>
25#include <i2c_eeprom.h>
26#include <init.h>
27#include <led.h>
28#include <memalign.h>
29#include <misc.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010030#include <mtd_node.h>
31#include <netdev.h>
32#include <phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060033#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060034#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060035#include <linux/printk.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010036#include <power/regulator.h>
37#include <remoteproc.h>
38#include <reset.h>
Marek Vasut35516542024-06-06 15:01:48 +020039#include <spl.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010040#include <syscon.h>
41#include <usb.h>
42#include <usb/dwc2_udc.h>
43#include <watchdog.h>
Simon Glass0034d962021-08-07 07:24:01 -060044#include <dm/ofnode.h>
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020045#include "../common/dh_common.h"
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020046#include "../../st/common/stpmic1.h"
Marek Vasut5ff05292020-01-24 18:39:16 +010047
48/* SYSCFG registers */
49#define SYSCFG_BOOTR 0x00
Marek Vasut5ff05292020-01-24 18:39:16 +010050#define SYSCFG_IOCTRLSETR 0x18
51#define SYSCFG_ICNR 0x1C
52#define SYSCFG_CMPCR 0x20
53#define SYSCFG_CMPENSETR 0x24
Marek Vasut5ff05292020-01-24 18:39:16 +010054
55#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
56#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
57
58#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
59#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
60#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
61#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
62#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
63
64#define SYSCFG_CMPCR_SW_CTRL BIT(1)
65#define SYSCFG_CMPCR_READY BIT(8)
66
67#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
68
Marek Vasut145a8762020-10-08 15:14:58 +020069#define KS_CCR 0x08
70#define KS_CCR_EEPROM BIT(9)
71#define KS_BE0 BIT(12)
72#define KS_BE1 BIT(13)
Marek Vasutb2b31c12021-05-03 13:31:39 +020073#define KS_CIDER 0xC0
74#define CIDER_ID 0x8870
Marek Vasut145a8762020-10-08 15:14:58 +020075
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020076static bool dh_stm32_mac_is_in_ks8851(void)
Marek Vasut5ff05292020-01-24 18:39:16 +010077{
Marek Vasutf4411842024-06-06 15:02:46 +020078 struct udevice *udev;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020079 u32 reg, cider, ccr;
Marek Vasutf4411842024-06-06 15:02:46 +020080 char path[256];
81 ofnode node;
82 int ret;
Marek Vasutb0a2a492020-07-31 01:34:50 +020083
Patrick Delaunay280949c2022-06-06 16:04:15 +020084 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020085 if (!ofnode_valid(node))
86 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020087
Marek Vasutf4411842024-06-06 15:02:46 +020088 ret = ofnode_get_path(node, path, sizeof(path));
89 if (ret)
90 return false;
91
92 ret = uclass_get_device_by_of_path(UCLASS_ETH, path, &udev);
93 if (ret)
94 return false;
95
96 if (!ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020097 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020098
99 /*
100 * KS8851 with EEPROM may use custom MAC from EEPROM, read
101 * out the KS8851 CCR register to determine whether EEPROM
102 * is present. If EEPROM is present, it must contain valid
103 * MAC address.
104 */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200105 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +0200106 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200107 return false;
Marek Vasut145a8762020-10-08 15:14:58 +0200108
Marek Vasutb2b31c12021-05-03 13:31:39 +0200109 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
110 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200111 if ((cider & 0xfff0) != CIDER_ID)
112 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200113
Marek Vasut145a8762020-10-08 15:14:58 +0200114 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
115 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200116 if (ccr & KS_CCR_EEPROM)
117 return true;
118
119 return false;
120}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200121
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200122static int dh_stm32_setup_ethaddr(void)
123{
124 unsigned char enetaddr[6];
125
126 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100127 return 0;
128
Marek Vasut29ab1a92024-03-12 22:15:58 +0100129 if (dh_get_mac_is_enabled("ethernet0"))
130 return 0;
131
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200132 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
133 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100134
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200135 return -ENXIO;
136}
Marek Vasut5ff05292020-01-24 18:39:16 +0100137
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200138static int dh_stm32_setup_eth1addr(void)
139{
140 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100141
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200142 if (dh_mac_is_in_env("eth1addr"))
143 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200144
Marek Vasut29ab1a92024-03-12 22:15:58 +0100145 if (dh_get_mac_is_enabled("ethernet1"))
146 return 0;
147
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200148 if (dh_stm32_mac_is_in_ks8851())
149 return 0;
150
151 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200152 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200153 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200154 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100155
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200156 return -ENXIO;
157}
158
159int setup_mac_address(void)
160{
161 if (dh_stm32_setup_ethaddr())
162 log_err("%s: Unable to setup ethaddr!\n", __func__);
163
164 if (dh_stm32_setup_eth1addr())
165 log_err("%s: Unable to setup eth1addr!\n", __func__);
166
Marek Vasut5ff05292020-01-24 18:39:16 +0100167 return 0;
168}
169
170int checkboard(void)
171{
172 char *mode;
173 const char *fdt_compat;
174 int fdt_compat_len;
175
Patrick Delaunay472407a2020-03-18 09:22:49 +0100176 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100177 mode = "trusted";
178 else
179 mode = "basic";
180
181 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200182 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
183 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100184 if (fdt_compat && fdt_compat_len)
185 printf(" (%s)", fdt_compat);
186 puts("\n");
187
188 return 0;
189}
190
Marek Vasut47b98ba2020-04-22 13:18:11 +0200191#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200192static u8 brdcode __section(".data");
193static u8 ddr3code __section(".data");
194static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200195static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200196
197static void board_get_coding_straps(void)
198{
199 struct gpio_desc gpio[4];
200 ofnode node;
201 int i, ret;
202
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100203 brdcode = 0;
204 ddr3code = 0;
205 somcode = 0;
206
Marek Vasut47b98ba2020-04-22 13:18:11 +0200207 node = ofnode_path("/config");
208 if (!ofnode_valid(node)) {
209 printf("%s: no /config node?\n", __func__);
210 return;
211 }
212
Marek Vasut47b98ba2020-04-22 13:18:11 +0200213 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
214 gpio, ARRAY_SIZE(gpio),
215 GPIOD_IS_IN);
216 for (i = 0; i < ret; i++)
217 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
218
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100219 gpio_free_list_nodev(gpio, ret);
220
Marek Vasut39221b52020-04-22 13:18:14 +0200221 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
222 gpio, ARRAY_SIZE(gpio),
223 GPIOD_IS_IN);
224 for (i = 0; i < ret; i++)
225 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
226
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100227 gpio_free_list_nodev(gpio, ret);
228
Marek Vasut47b98ba2020-04-22 13:18:11 +0200229 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
230 gpio, ARRAY_SIZE(gpio),
231 GPIOD_IS_IN);
232 for (i = 0; i < ret; i++)
233 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
234
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100235 gpio_free_list_nodev(gpio, ret);
236
Harald Seiler1768f5d2023-09-27 14:46:25 +0200237 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
238 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
239 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200240}
241
242int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
243 const char *name)
244{
Marek Vasut272198e2020-04-29 15:08:38 +0200245 if (ddr3code == 1 &&
246 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
247 return 0;
248
Marek Vasut39221b52020-04-22 13:18:14 +0200249 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200250 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200251 return 0;
252
253 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200254 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200255 return 0;
256
257 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200258}
259
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200260void board_vddcore_init(u32 voltage_mv)
261{
Simon Glass49c24a82024-09-29 19:49:47 -0600262 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200263 opp_voltage_mv = voltage_mv;
264}
265
Marek Vasut47b98ba2020-04-22 13:18:11 +0200266int board_early_init_f(void)
267{
Simon Glass49c24a82024-09-29 19:49:47 -0600268 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200269 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200270 board_get_coding_straps();
271
272 return 0;
273}
274
275#ifdef CONFIG_SPL_LOAD_FIT
276int board_fit_config_name_match(const char *name)
277{
Marek Vasut01711442024-10-05 03:15:50 +0200278 char *cdevice, *ndevice;
Marek Vasut060cb122020-07-31 01:35:33 +0200279 const char *compat;
Marek Vasut060cb122020-07-31 01:35:33 +0200280
Patrick Delaunay280949c2022-06-06 16:04:15 +0200281 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut01711442024-10-05 03:15:50 +0200282 if (!compat)
283 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200284
Marek Vasut01711442024-10-05 03:15:50 +0200285 cdevice = strchr(compat, ',');
286 if (!cdevice)
287 return -ENODEV;
288
289 cdevice++; /* Move past the comma right after vendor prefix. */
290
291 ndevice = strchr(name, '/');
292 if (!ndevice)
293 return -ENODEV;
294
295 ndevice++; /* Move past the last slash in DT path */
Marek Vasut47b98ba2020-04-22 13:18:11 +0200296
Marek Vasut01711442024-10-05 03:15:50 +0200297 if (!strcmp(cdevice, ndevice))
Marek Vasut47b98ba2020-04-22 13:18:11 +0200298 return 0;
299
300 return -EINVAL;
301}
302#endif
303#endif
304
Marek Vasut5ff05292020-01-24 18:39:16 +0100305static void board_key_check(void)
306{
307#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
308 ofnode node;
309 struct gpio_desc gpio;
310 enum forced_boot_mode boot_mode = BOOT_NORMAL;
311
312 node = ofnode_path("/config");
313 if (!ofnode_valid(node)) {
314 debug("%s: no /config node?\n", __func__);
315 return;
316 }
317#ifdef CONFIG_FASTBOOT
318 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
319 &gpio, GPIOD_IS_IN)) {
320 debug("%s: could not find a /config/st,fastboot-gpios\n",
321 __func__);
322 } else {
323 if (dm_gpio_get_value(&gpio)) {
324 puts("Fastboot key pressed, ");
325 boot_mode = BOOT_FASTBOOT;
326 }
327
328 dm_gpio_free(NULL, &gpio);
329 }
330#endif
331#ifdef CONFIG_CMD_STM32PROG
332 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
333 &gpio, GPIOD_IS_IN)) {
334 debug("%s: could not find a /config/st,stm32prog-gpios\n",
335 __func__);
336 } else {
337 if (dm_gpio_get_value(&gpio)) {
338 puts("STM32Programmer key pressed, ");
339 boot_mode = BOOT_STM32PROG;
340 }
341 dm_gpio_free(NULL, &gpio);
342 }
343#endif
344
345 if (boot_mode != BOOT_NORMAL) {
346 puts("entering download mode...\n");
347 clrsetbits_le32(TAMP_BOOT_CONTEXT,
348 TAMP_BOOT_FORCED_MASK,
349 boot_mode);
350 }
351#endif
352}
353
354#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
355
356#include <usb/dwc2_udc.h>
357int g_dnl_board_usb_cable_connected(void)
358{
359 struct udevice *dwc2_udc_otg;
360 int ret;
361
362 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700363 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100364 &dwc2_udc_otg);
365 if (!ret)
366 debug("dwc2_udc_otg init failed\n");
367
368 return dwc2_udc_B_session_valid(dwc2_udc_otg);
369}
370
371#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
372#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
373
374int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
375{
376 if (!strcmp(name, "usb_dnl_dfu"))
377 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
378 else if (!strcmp(name, "usb_dnl_fastboot"))
379 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
380 &dev->idProduct);
381 else
382 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
383
384 return 0;
385}
386
387#endif /* CONFIG_USB_GADGET */
388
389#ifdef CONFIG_LED
390static int get_led(struct udevice **dev, char *led_string)
391{
Simon Glass0034d962021-08-07 07:24:01 -0600392 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100393 int ret;
394
Simon Glass0034d962021-08-07 07:24:01 -0600395 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100396 if (!led_name) {
397 pr_debug("%s: could not find %s config string\n",
398 __func__, led_string);
399 return -ENOENT;
400 }
401 ret = led_get_by_label(led_name, dev);
402 if (ret) {
403 debug("%s: get=%d\n", __func__, ret);
404 return ret;
405 }
406
407 return 0;
408}
409
410static int setup_led(enum led_state_t cmd)
411{
412 struct udevice *dev;
413 int ret;
414
415 ret = get_led(&dev, "u-boot,boot-led");
416 if (ret)
417 return ret;
418
419 ret = led_set_state(dev, cmd);
420 return ret;
421}
422#endif
423
424static void __maybe_unused led_error_blink(u32 nb_blink)
425{
426#ifdef CONFIG_LED
427 int ret;
428 struct udevice *led;
429 u32 i;
430#endif
431
432 if (!nb_blink)
433 return;
434
435#ifdef CONFIG_LED
436 ret = get_led(&led, "u-boot,error-led");
437 if (!ret) {
438 /* make u-boot,error-led blinking */
439 /* if U32_MAX and 125ms interval, for 17.02 years */
440 for (i = 0; i < 2 * nb_blink; i++) {
441 led_set_state(led, LEDST_TOGGLE);
442 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200443 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100444 }
445 }
446#endif
447
448 /* infinite: the boot process must be stopped */
449 if (nb_blink == U32_MAX)
450 hang();
451}
452
453static void sysconf_init(void)
454{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200455#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100456 u8 *syscfg;
457#ifdef CONFIG_DM_REGULATOR
458 struct udevice *pwr_dev;
459 struct udevice *pwr_reg;
460 struct udevice *dev;
461 int ret;
462 u32 otp = 0;
463#endif
464 u32 bootr;
465
466 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
467
468 /* interconnect update : select master using the port 1 */
469 /* LTDC = AXI_M9 */
470 /* GPU = AXI_M8 */
471 /* today information is hardcoded in U-Boot */
472 writel(BIT(9), syscfg + SYSCFG_ICNR);
473
474 /* disable Pull-Down for boot pin connected to VDD */
475 bootr = readl(syscfg + SYSCFG_BOOTR);
476 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
477 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
478 writel(bootr, syscfg + SYSCFG_BOOTR);
479
480#ifdef CONFIG_DM_REGULATOR
481 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
482 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
483 * The customer will have to disable this for low frequencies
484 * or if AFMUX is selected but the function not used, typically for
485 * TRACE. Otherwise, impact on power consumption.
486 *
487 * WARNING:
488 * enabling High Speed mode while VDD>2.7V
489 * with the OTP product_below_2v5 (OTP 18, BIT 13)
490 * erroneously set to 1 can damage the IC!
491 * => U-Boot set the register only if VDD < 2.7V (in DT)
492 * but this value need to be consistent with board design
493 */
494 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700495 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100496 &pwr_dev);
497 if (!ret) {
498 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700499 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100500 &dev);
501 if (ret) {
502 pr_err("Can't find stm32mp_bsec driver\n");
503 return;
504 }
505
506 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
507 if (ret > 0)
508 otp = otp & BIT(13);
509
510 /* get VDD = vdd-supply */
511 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
512 &pwr_reg);
513
514 /* check if VDD is Low Voltage */
515 if (!ret) {
516 if (regulator_get_value(pwr_reg) < 2700000) {
517 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
518 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
519 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
520 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
521 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
522 syscfg + SYSCFG_IOCTRLSETR);
523
524 if (!otp)
525 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
526 } else {
527 if (otp)
528 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
529 }
530 } else {
531 debug("VDD unknown");
532 }
533 }
534#endif
535
536 /* activate automatic I/O compensation
537 * warning: need to ensure CSI enabled and ready in clock driver
538 */
539 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
540
541 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
542 ;
543 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
544#endif
545}
546
Marek Vasut7f809fe2022-05-11 23:09:33 +0200547#ifdef CONFIG_DM_REGULATOR
548#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
549#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
550#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
551#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
552#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
553#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
554#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
555static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
556{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200557 struct udevice *dev;
558 u8 bucks_vout = 0;
559 const char *prop;
560 int len, ret;
561
562 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200563 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200564 if (!prop || !len)
565 return -ENODEV;
566
Marek Vasut52784942022-09-26 18:50:00 +0200567 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200568 return -EINVAL;
569
570 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
571 ret = uclass_get_device_by_driver(UCLASS_MISC,
572 DM_DRIVER_GET(stpmic1_nvm),
573 &dev);
574 if (ret)
575 return ret;
576
577 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
578 if (ret != 1)
579 return -EINVAL;
580
581 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
582 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
583
Marek Vasut52784942022-09-26 18:50:00 +0200584 if (strstr(prop, "avenger96")) {
585 /*
586 * Avenger96 board comes in multiple regulator configurations:
587 * - rev.100 or rev.200 have Buck3 preconfigured to
588 * 3V3 operation on boot and contains extra Enpirion
589 * EP53A8LQI DCDC converter which supplies the IO.
590 * Reduce Buck3 voltage to 2V9 to not waste power.
591 * - rev.200L have Buck3 preconfigured to 1V8 operation
592 * and have no Enpirion EP53A8LQI DCDC anymore, the
593 * IO is supplied from Buck3.
594 */
595 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
596 *uv = 2900000;
597 else
598 *uv = 1800000;
599 } else {
600 /* Testbench always respects Buck3 NVM settings */
601 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
602 *uv = 3300000;
603 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
604 *uv = 3000000;
605 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
606 *uv = 1800000;
607 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
608 *uv = 1200000;
609 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200610
611 return 0;
612}
613
614static void board_init_regulator_av96(void)
615{
616 struct udevice *rdev;
617 int ret, uv;
618
619 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
620 if (ret) /* Not Avenger96 board. */
621 return;
622
623 ret = regulator_get_by_devname("buck3", &rdev);
624 if (ret)
625 return;
626
627 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
628 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200629 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200630}
631
632static void board_init_regulator(void)
633{
634 board_init_regulator_av96();
Marek Vasut7f809fe2022-05-11 23:09:33 +0200635}
636#else
637static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
638{
639 return -EINVAL;
640}
641
642static inline void board_init_regulator(void) {}
643#endif
644
Marek Vasut5ff05292020-01-24 18:39:16 +0100645/* board dependent setup after realloc */
646int board_init(void)
647{
Marek Vasut5ff05292020-01-24 18:39:16 +0100648 board_key_check();
649
Marek Vasut7f809fe2022-05-11 23:09:33 +0200650 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100651
652 sysconf_init();
653
Marek Vasut5ff05292020-01-24 18:39:16 +0100654 return 0;
655}
656
657int board_late_init(void)
658{
659 char *boot_device;
660#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
661 const void *fdt_compat;
662 int fdt_compat_len;
663
Patrick Delaunay280949c2022-06-06 16:04:15 +0200664 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
665 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100666 if (fdt_compat && fdt_compat_len) {
667 if (strncmp(fdt_compat, "st,", 3) != 0)
668 env_set("board_name", fdt_compat);
669 else
670 env_set("board_name", fdt_compat + 3);
671 }
672#endif
673
674 /* Check the boot-source to disable bootdelay */
675 boot_device = env_get("boot_device");
676 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
677 env_set("bootdelay", "0");
678
Marek Vasut47b98ba2020-04-22 13:18:11 +0200679#ifdef CONFIG_BOARD_EARLY_INIT_F
680 env_set_ulong("dh_som_rev", somcode);
681 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200682 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200683#endif
684
Marek Vasut5ff05292020-01-24 18:39:16 +0100685 return 0;
686}
687
688void board_quiesce_devices(void)
689{
690#ifdef CONFIG_LED
691 setup_led(LEDST_OFF);
692#endif
693}
694
Marek Vasut35516542024-06-06 15:01:48 +0200695static void dh_stm32_ks8851_fixup(void *blob)
696{
697 struct gpio_desc ks8851intrn;
698 bool compatible = false;
699 int ks8851intrn_value;
700 const char *prop;
701 ofnode node;
702 int idx = 0;
703 int offset;
704 int ret;
705
706 /* Do nothing if not STM32MP15xx DHCOM SoM */
707 while ((prop = fdt_stringlist_get(blob, 0, "compatible", idx++, NULL))) {
708 if (!strstr(prop, "dhcom-som"))
709 continue;
710 compatible = true;
711 break;
712 }
713
714 if (!compatible)
715 return;
716
717 /*
718 * Read state of INTRN pull up resistor, if this pull up is populated,
719 * KS8851-16MLL is populated as well and should be enabled, otherwise
720 * it should be disabled.
721 */
722 node = ofnode_path("/config");
723 if (!ofnode_valid(node))
724 return;
725
726 ret = gpio_request_by_name_nodev(node, "dh,mac-coding-gpios", 0,
727 &ks8851intrn, GPIOD_IS_IN);
728 if (ret)
729 return;
730
731 ks8851intrn_value = dm_gpio_get_value(&ks8851intrn);
732
733 dm_gpio_free(NULL, &ks8851intrn);
734
735 /* Set the 'status' property into KS8851-16MLL DT node. */
736 offset = fdt_path_offset(blob, "ethernet1");
737 ret = fdt_node_check_compatible(blob, offset, "micrel,ks8851-mll");
738 if (ret) /* Not compatible */
739 return;
740
741 /* Add a bit of extra space for new 'status' property */
742 ret = fdt_shrink_to_minimum(blob, 4096);
743 if (!ret)
744 return;
745
746 fdt_setprop_string(blob, offset, "status",
747 ks8851intrn_value ? "okay" : "disabled");
748}
749
Marek Vasut5ff05292020-01-24 18:39:16 +0100750#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900751int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100752{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200753 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
754 int buck3off, ret, uv;
755
Marek Vasut35516542024-06-06 15:01:48 +0200756 dh_stm32_ks8851_fixup(blob);
757
Marek Vasut7f809fe2022-05-11 23:09:33 +0200758 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
759 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
760 return 0;
761
762 buck3off = fdt_path_offset(blob, buck3path);
763 if (buck3off < 0) /* No Buck3 regulator found. */
764 return 0;
765
766 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
767 if (ret < 0)
768 return ret;
769
770 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
771 if (ret < 0)
772 return ret;
773
Marek Vasut5ff05292020-01-24 18:39:16 +0100774 return 0;
775}
776#endif
777
Simon Glass49c24a82024-09-29 19:49:47 -0600778#if defined(CONFIG_XPL_BUILD)
Marek Vasut35516542024-06-06 15:01:48 +0200779void spl_perform_fixups(struct spl_image_info *spl_image)
780{
781 dh_stm32_ks8851_fixup(spl_image_fdt_addr(spl_image));
782}
783#endif
784
Marek Vasut5ff05292020-01-24 18:39:16 +0100785static void board_copro_image_process(ulong fw_image, size_t fw_size)
786{
787 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
788
789 if (!rproc_is_initialized())
790 if (rproc_init()) {
791 printf("Remote Processor %d initialization failed\n",
792 id);
793 return;
794 }
795
796 ret = rproc_load(id, fw_image, fw_size);
797 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
798 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
799
800 if (!ret) {
801 rproc_start(id);
802 env_set("copro_state", "booted");
803 }
804}
805
806U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);