blob: ebd45f9053f4171c02220d5ee11f81237e9ecf95 [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{
Patrick Delaunay280949c2022-06-06 16:04:15 +020079 ofnode node;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020080 u32 reg, cider, ccr;
Marek Vasutb0a2a492020-07-31 01:34:50 +020081
Patrick Delaunay280949c2022-06-06 16:04:15 +020082 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020083 if (!ofnode_valid(node))
84 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020085
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020086 if (ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
87 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020088
89 /*
90 * KS8851 with EEPROM may use custom MAC from EEPROM, read
91 * out the KS8851 CCR register to determine whether EEPROM
92 * is present. If EEPROM is present, it must contain valid
93 * MAC address.
94 */
Patrick Delaunay280949c2022-06-06 16:04:15 +020095 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +020096 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020097 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020098
Marek Vasutb2b31c12021-05-03 13:31:39 +020099 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
100 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200101 if ((cider & 0xfff0) != CIDER_ID)
102 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200103
Marek Vasut145a8762020-10-08 15:14:58 +0200104 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
105 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200106 if (ccr & KS_CCR_EEPROM)
107 return true;
108
109 return false;
110}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200111
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200112static int dh_stm32_setup_ethaddr(void)
113{
114 unsigned char enetaddr[6];
115
116 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100117 return 0;
118
Marek Vasut29ab1a92024-03-12 22:15:58 +0100119 if (dh_get_mac_is_enabled("ethernet0"))
120 return 0;
121
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200122 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
123 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100124
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200125 return -ENXIO;
126}
Marek Vasut5ff05292020-01-24 18:39:16 +0100127
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200128static int dh_stm32_setup_eth1addr(void)
129{
130 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100131
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200132 if (dh_mac_is_in_env("eth1addr"))
133 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200134
Marek Vasut29ab1a92024-03-12 22:15:58 +0100135 if (dh_get_mac_is_enabled("ethernet1"))
136 return 0;
137
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200138 if (dh_stm32_mac_is_in_ks8851())
139 return 0;
140
141 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200142 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200143 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200144 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100145
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200146 return -ENXIO;
147}
148
149int setup_mac_address(void)
150{
151 if (dh_stm32_setup_ethaddr())
152 log_err("%s: Unable to setup ethaddr!\n", __func__);
153
154 if (dh_stm32_setup_eth1addr())
155 log_err("%s: Unable to setup eth1addr!\n", __func__);
156
Marek Vasut5ff05292020-01-24 18:39:16 +0100157 return 0;
158}
159
160int checkboard(void)
161{
162 char *mode;
163 const char *fdt_compat;
164 int fdt_compat_len;
165
Patrick Delaunay472407a2020-03-18 09:22:49 +0100166 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100167 mode = "trusted";
168 else
169 mode = "basic";
170
171 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200172 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
173 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100174 if (fdt_compat && fdt_compat_len)
175 printf(" (%s)", fdt_compat);
176 puts("\n");
177
178 return 0;
179}
180
Marek Vasut47b98ba2020-04-22 13:18:11 +0200181#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200182static u8 brdcode __section(".data");
183static u8 ddr3code __section(".data");
184static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200185static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200186
187static void board_get_coding_straps(void)
188{
189 struct gpio_desc gpio[4];
190 ofnode node;
191 int i, ret;
192
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100193 brdcode = 0;
194 ddr3code = 0;
195 somcode = 0;
196
Marek Vasut47b98ba2020-04-22 13:18:11 +0200197 node = ofnode_path("/config");
198 if (!ofnode_valid(node)) {
199 printf("%s: no /config node?\n", __func__);
200 return;
201 }
202
Marek Vasut47b98ba2020-04-22 13:18:11 +0200203 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
204 gpio, ARRAY_SIZE(gpio),
205 GPIOD_IS_IN);
206 for (i = 0; i < ret; i++)
207 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
208
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100209 gpio_free_list_nodev(gpio, ret);
210
Marek Vasut39221b52020-04-22 13:18:14 +0200211 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
212 gpio, ARRAY_SIZE(gpio),
213 GPIOD_IS_IN);
214 for (i = 0; i < ret; i++)
215 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
216
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100217 gpio_free_list_nodev(gpio, ret);
218
Marek Vasut47b98ba2020-04-22 13:18:11 +0200219 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
220 gpio, ARRAY_SIZE(gpio),
221 GPIOD_IS_IN);
222 for (i = 0; i < ret; i++)
223 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
224
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100225 gpio_free_list_nodev(gpio, ret);
226
Harald Seiler1768f5d2023-09-27 14:46:25 +0200227 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
228 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
229 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200230}
231
232int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
233 const char *name)
234{
Marek Vasut272198e2020-04-29 15:08:38 +0200235 if (ddr3code == 1 &&
236 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
237 return 0;
238
Marek Vasut39221b52020-04-22 13:18:14 +0200239 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200240 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200241 return 0;
242
243 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200244 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200245 return 0;
246
247 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200248}
249
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200250void board_vddcore_init(u32 voltage_mv)
251{
252 if (IS_ENABLED(CONFIG_SPL_BUILD))
253 opp_voltage_mv = voltage_mv;
254}
255
Marek Vasut47b98ba2020-04-22 13:18:11 +0200256int board_early_init_f(void)
257{
Patrick Delaunayf2f25c32020-05-25 12:19:46 +0200258 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200259 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200260 board_get_coding_straps();
261
262 return 0;
263}
264
265#ifdef CONFIG_SPL_LOAD_FIT
266int board_fit_config_name_match(const char *name)
267{
Marek Vasut060cb122020-07-31 01:35:33 +0200268 const char *compat;
269 char test[128];
270
Patrick Delaunay280949c2022-06-06 16:04:15 +0200271 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200272
Marek Vasut060cb122020-07-31 01:35:33 +0200273 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
274 compat, somcode, brdcode);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200275
276 if (!strcmp(name, test))
277 return 0;
278
279 return -EINVAL;
280}
281#endif
282#endif
283
Marek Vasut5ff05292020-01-24 18:39:16 +0100284static void board_key_check(void)
285{
286#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
287 ofnode node;
288 struct gpio_desc gpio;
289 enum forced_boot_mode boot_mode = BOOT_NORMAL;
290
291 node = ofnode_path("/config");
292 if (!ofnode_valid(node)) {
293 debug("%s: no /config node?\n", __func__);
294 return;
295 }
296#ifdef CONFIG_FASTBOOT
297 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
298 &gpio, GPIOD_IS_IN)) {
299 debug("%s: could not find a /config/st,fastboot-gpios\n",
300 __func__);
301 } else {
302 if (dm_gpio_get_value(&gpio)) {
303 puts("Fastboot key pressed, ");
304 boot_mode = BOOT_FASTBOOT;
305 }
306
307 dm_gpio_free(NULL, &gpio);
308 }
309#endif
310#ifdef CONFIG_CMD_STM32PROG
311 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
312 &gpio, GPIOD_IS_IN)) {
313 debug("%s: could not find a /config/st,stm32prog-gpios\n",
314 __func__);
315 } else {
316 if (dm_gpio_get_value(&gpio)) {
317 puts("STM32Programmer key pressed, ");
318 boot_mode = BOOT_STM32PROG;
319 }
320 dm_gpio_free(NULL, &gpio);
321 }
322#endif
323
324 if (boot_mode != BOOT_NORMAL) {
325 puts("entering download mode...\n");
326 clrsetbits_le32(TAMP_BOOT_CONTEXT,
327 TAMP_BOOT_FORCED_MASK,
328 boot_mode);
329 }
330#endif
331}
332
333#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
334
335#include <usb/dwc2_udc.h>
336int g_dnl_board_usb_cable_connected(void)
337{
338 struct udevice *dwc2_udc_otg;
339 int ret;
340
341 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700342 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100343 &dwc2_udc_otg);
344 if (!ret)
345 debug("dwc2_udc_otg init failed\n");
346
347 return dwc2_udc_B_session_valid(dwc2_udc_otg);
348}
349
350#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
351#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
352
353int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
354{
355 if (!strcmp(name, "usb_dnl_dfu"))
356 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
357 else if (!strcmp(name, "usb_dnl_fastboot"))
358 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
359 &dev->idProduct);
360 else
361 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
362
363 return 0;
364}
365
366#endif /* CONFIG_USB_GADGET */
367
368#ifdef CONFIG_LED
369static int get_led(struct udevice **dev, char *led_string)
370{
Simon Glass0034d962021-08-07 07:24:01 -0600371 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100372 int ret;
373
Simon Glass0034d962021-08-07 07:24:01 -0600374 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100375 if (!led_name) {
376 pr_debug("%s: could not find %s config string\n",
377 __func__, led_string);
378 return -ENOENT;
379 }
380 ret = led_get_by_label(led_name, dev);
381 if (ret) {
382 debug("%s: get=%d\n", __func__, ret);
383 return ret;
384 }
385
386 return 0;
387}
388
389static int setup_led(enum led_state_t cmd)
390{
391 struct udevice *dev;
392 int ret;
393
394 ret = get_led(&dev, "u-boot,boot-led");
395 if (ret)
396 return ret;
397
398 ret = led_set_state(dev, cmd);
399 return ret;
400}
401#endif
402
403static void __maybe_unused led_error_blink(u32 nb_blink)
404{
405#ifdef CONFIG_LED
406 int ret;
407 struct udevice *led;
408 u32 i;
409#endif
410
411 if (!nb_blink)
412 return;
413
414#ifdef CONFIG_LED
415 ret = get_led(&led, "u-boot,error-led");
416 if (!ret) {
417 /* make u-boot,error-led blinking */
418 /* if U32_MAX and 125ms interval, for 17.02 years */
419 for (i = 0; i < 2 * nb_blink; i++) {
420 led_set_state(led, LEDST_TOGGLE);
421 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200422 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100423 }
424 }
425#endif
426
427 /* infinite: the boot process must be stopped */
428 if (nb_blink == U32_MAX)
429 hang();
430}
431
432static void sysconf_init(void)
433{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200434#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100435 u8 *syscfg;
436#ifdef CONFIG_DM_REGULATOR
437 struct udevice *pwr_dev;
438 struct udevice *pwr_reg;
439 struct udevice *dev;
440 int ret;
441 u32 otp = 0;
442#endif
443 u32 bootr;
444
445 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
446
447 /* interconnect update : select master using the port 1 */
448 /* LTDC = AXI_M9 */
449 /* GPU = AXI_M8 */
450 /* today information is hardcoded in U-Boot */
451 writel(BIT(9), syscfg + SYSCFG_ICNR);
452
453 /* disable Pull-Down for boot pin connected to VDD */
454 bootr = readl(syscfg + SYSCFG_BOOTR);
455 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
456 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
457 writel(bootr, syscfg + SYSCFG_BOOTR);
458
459#ifdef CONFIG_DM_REGULATOR
460 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
461 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
462 * The customer will have to disable this for low frequencies
463 * or if AFMUX is selected but the function not used, typically for
464 * TRACE. Otherwise, impact on power consumption.
465 *
466 * WARNING:
467 * enabling High Speed mode while VDD>2.7V
468 * with the OTP product_below_2v5 (OTP 18, BIT 13)
469 * erroneously set to 1 can damage the IC!
470 * => U-Boot set the register only if VDD < 2.7V (in DT)
471 * but this value need to be consistent with board design
472 */
473 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700474 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100475 &pwr_dev);
476 if (!ret) {
477 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700478 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100479 &dev);
480 if (ret) {
481 pr_err("Can't find stm32mp_bsec driver\n");
482 return;
483 }
484
485 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
486 if (ret > 0)
487 otp = otp & BIT(13);
488
489 /* get VDD = vdd-supply */
490 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
491 &pwr_reg);
492
493 /* check if VDD is Low Voltage */
494 if (!ret) {
495 if (regulator_get_value(pwr_reg) < 2700000) {
496 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
497 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
498 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
499 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
500 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
501 syscfg + SYSCFG_IOCTRLSETR);
502
503 if (!otp)
504 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
505 } else {
506 if (otp)
507 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
508 }
509 } else {
510 debug("VDD unknown");
511 }
512 }
513#endif
514
515 /* activate automatic I/O compensation
516 * warning: need to ensure CSI enabled and ready in clock driver
517 */
518 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
519
520 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
521 ;
522 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
523#endif
524}
525
Marek Vasut7f809fe2022-05-11 23:09:33 +0200526#ifdef CONFIG_DM_REGULATOR
527#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
528#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
529#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
530#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
531#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
532#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
533#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
534static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
535{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200536 struct udevice *dev;
537 u8 bucks_vout = 0;
538 const char *prop;
539 int len, ret;
540
541 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200542 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200543 if (!prop || !len)
544 return -ENODEV;
545
Marek Vasut52784942022-09-26 18:50:00 +0200546 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200547 return -EINVAL;
548
549 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
550 ret = uclass_get_device_by_driver(UCLASS_MISC,
551 DM_DRIVER_GET(stpmic1_nvm),
552 &dev);
553 if (ret)
554 return ret;
555
556 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
557 if (ret != 1)
558 return -EINVAL;
559
560 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
561 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
562
Marek Vasut52784942022-09-26 18:50:00 +0200563 if (strstr(prop, "avenger96")) {
564 /*
565 * Avenger96 board comes in multiple regulator configurations:
566 * - rev.100 or rev.200 have Buck3 preconfigured to
567 * 3V3 operation on boot and contains extra Enpirion
568 * EP53A8LQI DCDC converter which supplies the IO.
569 * Reduce Buck3 voltage to 2V9 to not waste power.
570 * - rev.200L have Buck3 preconfigured to 1V8 operation
571 * and have no Enpirion EP53A8LQI DCDC anymore, the
572 * IO is supplied from Buck3.
573 */
574 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
575 *uv = 2900000;
576 else
577 *uv = 1800000;
578 } else {
579 /* Testbench always respects Buck3 NVM settings */
580 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
581 *uv = 3300000;
582 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
583 *uv = 3000000;
584 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
585 *uv = 1800000;
586 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
587 *uv = 1200000;
588 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200589
590 return 0;
591}
592
593static void board_init_regulator_av96(void)
594{
595 struct udevice *rdev;
596 int ret, uv;
597
598 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
599 if (ret) /* Not Avenger96 board. */
600 return;
601
602 ret = regulator_get_by_devname("buck3", &rdev);
603 if (ret)
604 return;
605
606 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
607 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200608 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200609}
610
611static void board_init_regulator(void)
612{
613 board_init_regulator_av96();
614
615 regulators_enable_boot_on(_DEBUG);
616}
617#else
618static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
619{
620 return -EINVAL;
621}
622
623static inline void board_init_regulator(void) {}
624#endif
625
Marek Vasut5ff05292020-01-24 18:39:16 +0100626/* board dependent setup after realloc */
627int board_init(void)
628{
Marek Vasut5ff05292020-01-24 18:39:16 +0100629 board_key_check();
630
Marek Vasut7f809fe2022-05-11 23:09:33 +0200631 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100632
633 sysconf_init();
634
Marek Vasut5ff05292020-01-24 18:39:16 +0100635 return 0;
636}
637
638int board_late_init(void)
639{
640 char *boot_device;
641#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
642 const void *fdt_compat;
643 int fdt_compat_len;
644
Patrick Delaunay280949c2022-06-06 16:04:15 +0200645 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
646 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100647 if (fdt_compat && fdt_compat_len) {
648 if (strncmp(fdt_compat, "st,", 3) != 0)
649 env_set("board_name", fdt_compat);
650 else
651 env_set("board_name", fdt_compat + 3);
652 }
653#endif
654
655 /* Check the boot-source to disable bootdelay */
656 boot_device = env_get("boot_device");
657 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
658 env_set("bootdelay", "0");
659
Marek Vasut47b98ba2020-04-22 13:18:11 +0200660#ifdef CONFIG_BOARD_EARLY_INIT_F
661 env_set_ulong("dh_som_rev", somcode);
662 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200663 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200664#endif
665
Marek Vasut5ff05292020-01-24 18:39:16 +0100666 return 0;
667}
668
669void board_quiesce_devices(void)
670{
671#ifdef CONFIG_LED
672 setup_led(LEDST_OFF);
673#endif
674}
675
Marek Vasut35516542024-06-06 15:01:48 +0200676static void dh_stm32_ks8851_fixup(void *blob)
677{
678 struct gpio_desc ks8851intrn;
679 bool compatible = false;
680 int ks8851intrn_value;
681 const char *prop;
682 ofnode node;
683 int idx = 0;
684 int offset;
685 int ret;
686
687 /* Do nothing if not STM32MP15xx DHCOM SoM */
688 while ((prop = fdt_stringlist_get(blob, 0, "compatible", idx++, NULL))) {
689 if (!strstr(prop, "dhcom-som"))
690 continue;
691 compatible = true;
692 break;
693 }
694
695 if (!compatible)
696 return;
697
698 /*
699 * Read state of INTRN pull up resistor, if this pull up is populated,
700 * KS8851-16MLL is populated as well and should be enabled, otherwise
701 * it should be disabled.
702 */
703 node = ofnode_path("/config");
704 if (!ofnode_valid(node))
705 return;
706
707 ret = gpio_request_by_name_nodev(node, "dh,mac-coding-gpios", 0,
708 &ks8851intrn, GPIOD_IS_IN);
709 if (ret)
710 return;
711
712 ks8851intrn_value = dm_gpio_get_value(&ks8851intrn);
713
714 dm_gpio_free(NULL, &ks8851intrn);
715
716 /* Set the 'status' property into KS8851-16MLL DT node. */
717 offset = fdt_path_offset(blob, "ethernet1");
718 ret = fdt_node_check_compatible(blob, offset, "micrel,ks8851-mll");
719 if (ret) /* Not compatible */
720 return;
721
722 /* Add a bit of extra space for new 'status' property */
723 ret = fdt_shrink_to_minimum(blob, 4096);
724 if (!ret)
725 return;
726
727 fdt_setprop_string(blob, offset, "status",
728 ks8851intrn_value ? "okay" : "disabled");
729}
730
Marek Vasut5ff05292020-01-24 18:39:16 +0100731#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900732int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100733{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200734 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
735 int buck3off, ret, uv;
736
Marek Vasut35516542024-06-06 15:01:48 +0200737 dh_stm32_ks8851_fixup(blob);
738
Marek Vasut7f809fe2022-05-11 23:09:33 +0200739 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
740 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
741 return 0;
742
743 buck3off = fdt_path_offset(blob, buck3path);
744 if (buck3off < 0) /* No Buck3 regulator found. */
745 return 0;
746
747 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
748 if (ret < 0)
749 return ret;
750
751 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
752 if (ret < 0)
753 return ret;
754
Marek Vasut5ff05292020-01-24 18:39:16 +0100755 return 0;
756}
757#endif
758
Marek Vasut35516542024-06-06 15:01:48 +0200759#if defined(CONFIG_SPL_BUILD)
760void spl_perform_fixups(struct spl_image_info *spl_image)
761{
762 dh_stm32_ks8851_fixup(spl_image_fdt_addr(spl_image));
763}
764#endif
765
Marek Vasut5ff05292020-01-24 18:39:16 +0100766static void board_copro_image_process(ulong fw_image, size_t fw_size)
767{
768 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
769
770 if (!rproc_is_initialized())
771 if (rproc_init()) {
772 printf("Remote Processor %d initialization failed\n",
773 id);
774 return;
775 }
776
777 ret = rproc_load(id, fw_image, fw_size);
778 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
779 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
780
781 if (!ret) {
782 rproc_start(id);
783 env_set("copro_state", "booted");
784 }
785}
786
787U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);