blob: 073f32d1380fe9b3ccfe2c7fcac01888922efdb3 [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>
30#include <mtd.h>
31#include <mtd_node.h>
32#include <netdev.h>
33#include <phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060034#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060036#include <linux/printk.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010037#include <power/regulator.h>
38#include <remoteproc.h>
39#include <reset.h>
Marek Vasut35516542024-06-06 15:01:48 +020040#include <spl.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010041#include <syscon.h>
42#include <usb.h>
43#include <usb/dwc2_udc.h>
44#include <watchdog.h>
Simon Glass0034d962021-08-07 07:24:01 -060045#include <dm/ofnode.h>
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020046#include "../common/dh_common.h"
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020047#include "../../st/common/stpmic1.h"
Marek Vasut5ff05292020-01-24 18:39:16 +010048
49/* SYSCFG registers */
50#define SYSCFG_BOOTR 0x00
Marek Vasut5ff05292020-01-24 18:39:16 +010051#define SYSCFG_IOCTRLSETR 0x18
52#define SYSCFG_ICNR 0x1C
53#define SYSCFG_CMPCR 0x20
54#define SYSCFG_CMPENSETR 0x24
Marek Vasut5ff05292020-01-24 18:39:16 +010055
56#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
57#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
58
59#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
60#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
61#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
62#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
63#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
64
65#define SYSCFG_CMPCR_SW_CTRL BIT(1)
66#define SYSCFG_CMPCR_READY BIT(8)
67
68#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
69
Marek Vasut145a8762020-10-08 15:14:58 +020070#define KS_CCR 0x08
71#define KS_CCR_EEPROM BIT(9)
72#define KS_BE0 BIT(12)
73#define KS_BE1 BIT(13)
Marek Vasutb2b31c12021-05-03 13:31:39 +020074#define KS_CIDER 0xC0
75#define CIDER_ID 0x8870
Marek Vasut145a8762020-10-08 15:14:58 +020076
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020077static bool dh_stm32_mac_is_in_ks8851(void)
Marek Vasut5ff05292020-01-24 18:39:16 +010078{
Marek Vasutf4411842024-06-06 15:02:46 +020079 struct udevice *udev;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020080 u32 reg, cider, ccr;
Marek Vasutf4411842024-06-06 15:02:46 +020081 char path[256];
82 ofnode node;
83 int ret;
Marek Vasutb0a2a492020-07-31 01:34:50 +020084
Patrick Delaunay280949c2022-06-06 16:04:15 +020085 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020086 if (!ofnode_valid(node))
87 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020088
Marek Vasutf4411842024-06-06 15:02:46 +020089 ret = ofnode_get_path(node, path, sizeof(path));
90 if (ret)
91 return false;
92
93 ret = uclass_get_device_by_of_path(UCLASS_ETH, path, &udev);
94 if (ret)
95 return false;
96
97 if (!ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020098 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020099
100 /*
101 * KS8851 with EEPROM may use custom MAC from EEPROM, read
102 * out the KS8851 CCR register to determine whether EEPROM
103 * is present. If EEPROM is present, it must contain valid
104 * MAC address.
105 */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200106 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +0200107 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200108 return false;
Marek Vasut145a8762020-10-08 15:14:58 +0200109
Marek Vasutb2b31c12021-05-03 13:31:39 +0200110 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
111 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200112 if ((cider & 0xfff0) != CIDER_ID)
113 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200114
Marek Vasut145a8762020-10-08 15:14:58 +0200115 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
116 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200117 if (ccr & KS_CCR_EEPROM)
118 return true;
119
120 return false;
121}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200122
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200123static int dh_stm32_setup_ethaddr(void)
124{
125 unsigned char enetaddr[6];
126
127 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100128 return 0;
129
Marek Vasut29ab1a92024-03-12 22:15:58 +0100130 if (dh_get_mac_is_enabled("ethernet0"))
131 return 0;
132
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200133 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
134 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100135
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200136 return -ENXIO;
137}
Marek Vasut5ff05292020-01-24 18:39:16 +0100138
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200139static int dh_stm32_setup_eth1addr(void)
140{
141 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100142
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200143 if (dh_mac_is_in_env("eth1addr"))
144 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200145
Marek Vasut29ab1a92024-03-12 22:15:58 +0100146 if (dh_get_mac_is_enabled("ethernet1"))
147 return 0;
148
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200149 if (dh_stm32_mac_is_in_ks8851())
150 return 0;
151
152 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200153 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200154 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200155 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100156
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200157 return -ENXIO;
158}
159
160int setup_mac_address(void)
161{
162 if (dh_stm32_setup_ethaddr())
163 log_err("%s: Unable to setup ethaddr!\n", __func__);
164
165 if (dh_stm32_setup_eth1addr())
166 log_err("%s: Unable to setup eth1addr!\n", __func__);
167
Marek Vasut5ff05292020-01-24 18:39:16 +0100168 return 0;
169}
170
171int checkboard(void)
172{
173 char *mode;
174 const char *fdt_compat;
175 int fdt_compat_len;
176
Patrick Delaunay472407a2020-03-18 09:22:49 +0100177 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100178 mode = "trusted";
179 else
180 mode = "basic";
181
182 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200183 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
184 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100185 if (fdt_compat && fdt_compat_len)
186 printf(" (%s)", fdt_compat);
187 puts("\n");
188
189 return 0;
190}
191
Marek Vasut47b98ba2020-04-22 13:18:11 +0200192#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200193static u8 brdcode __section(".data");
194static u8 ddr3code __section(".data");
195static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200196static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200197
198static void board_get_coding_straps(void)
199{
200 struct gpio_desc gpio[4];
201 ofnode node;
202 int i, ret;
203
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100204 brdcode = 0;
205 ddr3code = 0;
206 somcode = 0;
207
Marek Vasut47b98ba2020-04-22 13:18:11 +0200208 node = ofnode_path("/config");
209 if (!ofnode_valid(node)) {
210 printf("%s: no /config node?\n", __func__);
211 return;
212 }
213
Marek Vasut47b98ba2020-04-22 13:18:11 +0200214 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
215 gpio, ARRAY_SIZE(gpio),
216 GPIOD_IS_IN);
217 for (i = 0; i < ret; i++)
218 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
219
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100220 gpio_free_list_nodev(gpio, ret);
221
Marek Vasut39221b52020-04-22 13:18:14 +0200222 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
223 gpio, ARRAY_SIZE(gpio),
224 GPIOD_IS_IN);
225 for (i = 0; i < ret; i++)
226 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
227
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100228 gpio_free_list_nodev(gpio, ret);
229
Marek Vasut47b98ba2020-04-22 13:18:11 +0200230 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
231 gpio, ARRAY_SIZE(gpio),
232 GPIOD_IS_IN);
233 for (i = 0; i < ret; i++)
234 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
235
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100236 gpio_free_list_nodev(gpio, ret);
237
Harald Seiler1768f5d2023-09-27 14:46:25 +0200238 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
239 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
240 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200241}
242
243int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
244 const char *name)
245{
Marek Vasut272198e2020-04-29 15:08:38 +0200246 if (ddr3code == 1 &&
247 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
248 return 0;
249
Marek Vasut39221b52020-04-22 13:18:14 +0200250 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200251 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200252 return 0;
253
254 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200255 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200256 return 0;
257
258 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200259}
260
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200261void board_vddcore_init(u32 voltage_mv)
262{
Simon Glass49c24a82024-09-29 19:49:47 -0600263 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200264 opp_voltage_mv = voltage_mv;
265}
266
Marek Vasut47b98ba2020-04-22 13:18:11 +0200267int board_early_init_f(void)
268{
Simon Glass49c24a82024-09-29 19:49:47 -0600269 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200270 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200271 board_get_coding_straps();
272
273 return 0;
274}
275
276#ifdef CONFIG_SPL_LOAD_FIT
277int board_fit_config_name_match(const char *name)
278{
Marek Vasut060cb122020-07-31 01:35:33 +0200279 const char *compat;
280 char test[128];
281
Patrick Delaunay280949c2022-06-06 16:04:15 +0200282 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200283
Marek Vasut060cb122020-07-31 01:35:33 +0200284 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
285 compat, somcode, brdcode);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200286
287 if (!strcmp(name, test))
288 return 0;
289
290 return -EINVAL;
291}
292#endif
293#endif
294
Marek Vasut5ff05292020-01-24 18:39:16 +0100295static void board_key_check(void)
296{
297#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
298 ofnode node;
299 struct gpio_desc gpio;
300 enum forced_boot_mode boot_mode = BOOT_NORMAL;
301
302 node = ofnode_path("/config");
303 if (!ofnode_valid(node)) {
304 debug("%s: no /config node?\n", __func__);
305 return;
306 }
307#ifdef CONFIG_FASTBOOT
308 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
309 &gpio, GPIOD_IS_IN)) {
310 debug("%s: could not find a /config/st,fastboot-gpios\n",
311 __func__);
312 } else {
313 if (dm_gpio_get_value(&gpio)) {
314 puts("Fastboot key pressed, ");
315 boot_mode = BOOT_FASTBOOT;
316 }
317
318 dm_gpio_free(NULL, &gpio);
319 }
320#endif
321#ifdef CONFIG_CMD_STM32PROG
322 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
323 &gpio, GPIOD_IS_IN)) {
324 debug("%s: could not find a /config/st,stm32prog-gpios\n",
325 __func__);
326 } else {
327 if (dm_gpio_get_value(&gpio)) {
328 puts("STM32Programmer key pressed, ");
329 boot_mode = BOOT_STM32PROG;
330 }
331 dm_gpio_free(NULL, &gpio);
332 }
333#endif
334
335 if (boot_mode != BOOT_NORMAL) {
336 puts("entering download mode...\n");
337 clrsetbits_le32(TAMP_BOOT_CONTEXT,
338 TAMP_BOOT_FORCED_MASK,
339 boot_mode);
340 }
341#endif
342}
343
344#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
345
346#include <usb/dwc2_udc.h>
347int g_dnl_board_usb_cable_connected(void)
348{
349 struct udevice *dwc2_udc_otg;
350 int ret;
351
352 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700353 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100354 &dwc2_udc_otg);
355 if (!ret)
356 debug("dwc2_udc_otg init failed\n");
357
358 return dwc2_udc_B_session_valid(dwc2_udc_otg);
359}
360
361#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
362#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
363
364int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
365{
366 if (!strcmp(name, "usb_dnl_dfu"))
367 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
368 else if (!strcmp(name, "usb_dnl_fastboot"))
369 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
370 &dev->idProduct);
371 else
372 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
373
374 return 0;
375}
376
377#endif /* CONFIG_USB_GADGET */
378
379#ifdef CONFIG_LED
380static int get_led(struct udevice **dev, char *led_string)
381{
Simon Glass0034d962021-08-07 07:24:01 -0600382 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100383 int ret;
384
Simon Glass0034d962021-08-07 07:24:01 -0600385 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100386 if (!led_name) {
387 pr_debug("%s: could not find %s config string\n",
388 __func__, led_string);
389 return -ENOENT;
390 }
391 ret = led_get_by_label(led_name, dev);
392 if (ret) {
393 debug("%s: get=%d\n", __func__, ret);
394 return ret;
395 }
396
397 return 0;
398}
399
400static int setup_led(enum led_state_t cmd)
401{
402 struct udevice *dev;
403 int ret;
404
405 ret = get_led(&dev, "u-boot,boot-led");
406 if (ret)
407 return ret;
408
409 ret = led_set_state(dev, cmd);
410 return ret;
411}
412#endif
413
414static void __maybe_unused led_error_blink(u32 nb_blink)
415{
416#ifdef CONFIG_LED
417 int ret;
418 struct udevice *led;
419 u32 i;
420#endif
421
422 if (!nb_blink)
423 return;
424
425#ifdef CONFIG_LED
426 ret = get_led(&led, "u-boot,error-led");
427 if (!ret) {
428 /* make u-boot,error-led blinking */
429 /* if U32_MAX and 125ms interval, for 17.02 years */
430 for (i = 0; i < 2 * nb_blink; i++) {
431 led_set_state(led, LEDST_TOGGLE);
432 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200433 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100434 }
435 }
436#endif
437
438 /* infinite: the boot process must be stopped */
439 if (nb_blink == U32_MAX)
440 hang();
441}
442
443static void sysconf_init(void)
444{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200445#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100446 u8 *syscfg;
447#ifdef CONFIG_DM_REGULATOR
448 struct udevice *pwr_dev;
449 struct udevice *pwr_reg;
450 struct udevice *dev;
451 int ret;
452 u32 otp = 0;
453#endif
454 u32 bootr;
455
456 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
457
458 /* interconnect update : select master using the port 1 */
459 /* LTDC = AXI_M9 */
460 /* GPU = AXI_M8 */
461 /* today information is hardcoded in U-Boot */
462 writel(BIT(9), syscfg + SYSCFG_ICNR);
463
464 /* disable Pull-Down for boot pin connected to VDD */
465 bootr = readl(syscfg + SYSCFG_BOOTR);
466 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
467 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
468 writel(bootr, syscfg + SYSCFG_BOOTR);
469
470#ifdef CONFIG_DM_REGULATOR
471 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
472 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
473 * The customer will have to disable this for low frequencies
474 * or if AFMUX is selected but the function not used, typically for
475 * TRACE. Otherwise, impact on power consumption.
476 *
477 * WARNING:
478 * enabling High Speed mode while VDD>2.7V
479 * with the OTP product_below_2v5 (OTP 18, BIT 13)
480 * erroneously set to 1 can damage the IC!
481 * => U-Boot set the register only if VDD < 2.7V (in DT)
482 * but this value need to be consistent with board design
483 */
484 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700485 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100486 &pwr_dev);
487 if (!ret) {
488 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700489 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100490 &dev);
491 if (ret) {
492 pr_err("Can't find stm32mp_bsec driver\n");
493 return;
494 }
495
496 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
497 if (ret > 0)
498 otp = otp & BIT(13);
499
500 /* get VDD = vdd-supply */
501 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
502 &pwr_reg);
503
504 /* check if VDD is Low Voltage */
505 if (!ret) {
506 if (regulator_get_value(pwr_reg) < 2700000) {
507 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
508 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
509 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
510 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
511 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
512 syscfg + SYSCFG_IOCTRLSETR);
513
514 if (!otp)
515 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
516 } else {
517 if (otp)
518 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
519 }
520 } else {
521 debug("VDD unknown");
522 }
523 }
524#endif
525
526 /* activate automatic I/O compensation
527 * warning: need to ensure CSI enabled and ready in clock driver
528 */
529 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
530
531 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
532 ;
533 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
534#endif
535}
536
Marek Vasut7f809fe2022-05-11 23:09:33 +0200537#ifdef CONFIG_DM_REGULATOR
538#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
539#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
540#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
541#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
542#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
543#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
544#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
545static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
546{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200547 struct udevice *dev;
548 u8 bucks_vout = 0;
549 const char *prop;
550 int len, ret;
551
552 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200553 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200554 if (!prop || !len)
555 return -ENODEV;
556
Marek Vasut52784942022-09-26 18:50:00 +0200557 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200558 return -EINVAL;
559
560 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
561 ret = uclass_get_device_by_driver(UCLASS_MISC,
562 DM_DRIVER_GET(stpmic1_nvm),
563 &dev);
564 if (ret)
565 return ret;
566
567 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
568 if (ret != 1)
569 return -EINVAL;
570
571 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
572 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
573
Marek Vasut52784942022-09-26 18:50:00 +0200574 if (strstr(prop, "avenger96")) {
575 /*
576 * Avenger96 board comes in multiple regulator configurations:
577 * - rev.100 or rev.200 have Buck3 preconfigured to
578 * 3V3 operation on boot and contains extra Enpirion
579 * EP53A8LQI DCDC converter which supplies the IO.
580 * Reduce Buck3 voltage to 2V9 to not waste power.
581 * - rev.200L have Buck3 preconfigured to 1V8 operation
582 * and have no Enpirion EP53A8LQI DCDC anymore, the
583 * IO is supplied from Buck3.
584 */
585 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
586 *uv = 2900000;
587 else
588 *uv = 1800000;
589 } else {
590 /* Testbench always respects Buck3 NVM settings */
591 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
592 *uv = 3300000;
593 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
594 *uv = 3000000;
595 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
596 *uv = 1800000;
597 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
598 *uv = 1200000;
599 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200600
601 return 0;
602}
603
604static void board_init_regulator_av96(void)
605{
606 struct udevice *rdev;
607 int ret, uv;
608
609 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
610 if (ret) /* Not Avenger96 board. */
611 return;
612
613 ret = regulator_get_by_devname("buck3", &rdev);
614 if (ret)
615 return;
616
617 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
618 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200619 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200620}
621
622static void board_init_regulator(void)
623{
624 board_init_regulator_av96();
625
626 regulators_enable_boot_on(_DEBUG);
627}
628#else
629static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
630{
631 return -EINVAL;
632}
633
634static inline void board_init_regulator(void) {}
635#endif
636
Marek Vasut5ff05292020-01-24 18:39:16 +0100637/* board dependent setup after realloc */
638int board_init(void)
639{
Marek Vasut5ff05292020-01-24 18:39:16 +0100640 board_key_check();
641
Marek Vasut7f809fe2022-05-11 23:09:33 +0200642 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100643
644 sysconf_init();
645
Marek Vasut5ff05292020-01-24 18:39:16 +0100646 return 0;
647}
648
649int board_late_init(void)
650{
651 char *boot_device;
652#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
653 const void *fdt_compat;
654 int fdt_compat_len;
655
Patrick Delaunay280949c2022-06-06 16:04:15 +0200656 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
657 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100658 if (fdt_compat && fdt_compat_len) {
659 if (strncmp(fdt_compat, "st,", 3) != 0)
660 env_set("board_name", fdt_compat);
661 else
662 env_set("board_name", fdt_compat + 3);
663 }
664#endif
665
666 /* Check the boot-source to disable bootdelay */
667 boot_device = env_get("boot_device");
668 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
669 env_set("bootdelay", "0");
670
Marek Vasut47b98ba2020-04-22 13:18:11 +0200671#ifdef CONFIG_BOARD_EARLY_INIT_F
672 env_set_ulong("dh_som_rev", somcode);
673 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200674 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200675#endif
676
Marek Vasut5ff05292020-01-24 18:39:16 +0100677 return 0;
678}
679
680void board_quiesce_devices(void)
681{
682#ifdef CONFIG_LED
683 setup_led(LEDST_OFF);
684#endif
685}
686
Marek Vasut35516542024-06-06 15:01:48 +0200687static void dh_stm32_ks8851_fixup(void *blob)
688{
689 struct gpio_desc ks8851intrn;
690 bool compatible = false;
691 int ks8851intrn_value;
692 const char *prop;
693 ofnode node;
694 int idx = 0;
695 int offset;
696 int ret;
697
698 /* Do nothing if not STM32MP15xx DHCOM SoM */
699 while ((prop = fdt_stringlist_get(blob, 0, "compatible", idx++, NULL))) {
700 if (!strstr(prop, "dhcom-som"))
701 continue;
702 compatible = true;
703 break;
704 }
705
706 if (!compatible)
707 return;
708
709 /*
710 * Read state of INTRN pull up resistor, if this pull up is populated,
711 * KS8851-16MLL is populated as well and should be enabled, otherwise
712 * it should be disabled.
713 */
714 node = ofnode_path("/config");
715 if (!ofnode_valid(node))
716 return;
717
718 ret = gpio_request_by_name_nodev(node, "dh,mac-coding-gpios", 0,
719 &ks8851intrn, GPIOD_IS_IN);
720 if (ret)
721 return;
722
723 ks8851intrn_value = dm_gpio_get_value(&ks8851intrn);
724
725 dm_gpio_free(NULL, &ks8851intrn);
726
727 /* Set the 'status' property into KS8851-16MLL DT node. */
728 offset = fdt_path_offset(blob, "ethernet1");
729 ret = fdt_node_check_compatible(blob, offset, "micrel,ks8851-mll");
730 if (ret) /* Not compatible */
731 return;
732
733 /* Add a bit of extra space for new 'status' property */
734 ret = fdt_shrink_to_minimum(blob, 4096);
735 if (!ret)
736 return;
737
738 fdt_setprop_string(blob, offset, "status",
739 ks8851intrn_value ? "okay" : "disabled");
740}
741
Marek Vasut5ff05292020-01-24 18:39:16 +0100742#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900743int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100744{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200745 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
746 int buck3off, ret, uv;
747
Marek Vasut35516542024-06-06 15:01:48 +0200748 dh_stm32_ks8851_fixup(blob);
749
Marek Vasut7f809fe2022-05-11 23:09:33 +0200750 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
751 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
752 return 0;
753
754 buck3off = fdt_path_offset(blob, buck3path);
755 if (buck3off < 0) /* No Buck3 regulator found. */
756 return 0;
757
758 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
759 if (ret < 0)
760 return ret;
761
762 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
763 if (ret < 0)
764 return ret;
765
Marek Vasut5ff05292020-01-24 18:39:16 +0100766 return 0;
767}
768#endif
769
Simon Glass49c24a82024-09-29 19:49:47 -0600770#if defined(CONFIG_XPL_BUILD)
Marek Vasut35516542024-06-06 15:01:48 +0200771void spl_perform_fixups(struct spl_image_info *spl_image)
772{
773 dh_stm32_ks8851_fixup(spl_image_fdt_addr(spl_image));
774}
775#endif
776
Marek Vasut5ff05292020-01-24 18:39:16 +0100777static void board_copro_image_process(ulong fw_image, size_t fw_size)
778{
779 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
780
781 if (!rproc_is_initialized())
782 if (rproc_init()) {
783 printf("Remote Processor %d initialization failed\n",
784 id);
785 return;
786 }
787
788 ret = rproc_load(id, fw_image, fw_size);
789 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
790 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
791
792 if (!ret) {
793 rproc_start(id);
794 env_set("copro_state", "booted");
795 }
796}
797
798U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);