blob: 20c9d70737ec6d54d756560a2492bf8ac0395f99 [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>
40#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{
Patrick Delaunay280949c2022-06-06 16:04:15 +020078 ofnode node;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020079 u32 reg, cider, ccr;
Marek Vasutb0a2a492020-07-31 01:34:50 +020080
Patrick Delaunay280949c2022-06-06 16:04:15 +020081 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020082 if (!ofnode_valid(node))
83 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020084
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020085 if (ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
86 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020087
88 /*
89 * KS8851 with EEPROM may use custom MAC from EEPROM, read
90 * out the KS8851 CCR register to determine whether EEPROM
91 * is present. If EEPROM is present, it must contain valid
92 * MAC address.
93 */
Patrick Delaunay280949c2022-06-06 16:04:15 +020094 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +020095 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020096 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020097
Marek Vasutb2b31c12021-05-03 13:31:39 +020098 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
99 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200100 if ((cider & 0xfff0) != CIDER_ID)
101 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200102
Marek Vasut145a8762020-10-08 15:14:58 +0200103 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
104 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200105 if (ccr & KS_CCR_EEPROM)
106 return true;
107
108 return false;
109}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200110
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200111static int dh_stm32_setup_ethaddr(void)
112{
113 unsigned char enetaddr[6];
114
115 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100116 return 0;
117
Marek Vasut29ab1a92024-03-12 22:15:58 +0100118 if (dh_get_mac_is_enabled("ethernet0"))
119 return 0;
120
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200121 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
122 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100123
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200124 return -ENXIO;
125}
Marek Vasut5ff05292020-01-24 18:39:16 +0100126
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200127static int dh_stm32_setup_eth1addr(void)
128{
129 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100130
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200131 if (dh_mac_is_in_env("eth1addr"))
132 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200133
Marek Vasut29ab1a92024-03-12 22:15:58 +0100134 if (dh_get_mac_is_enabled("ethernet1"))
135 return 0;
136
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200137 if (dh_stm32_mac_is_in_ks8851())
138 return 0;
139
140 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200141 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200142 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200143 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100144
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200145 return -ENXIO;
146}
147
148int setup_mac_address(void)
149{
150 if (dh_stm32_setup_ethaddr())
151 log_err("%s: Unable to setup ethaddr!\n", __func__);
152
153 if (dh_stm32_setup_eth1addr())
154 log_err("%s: Unable to setup eth1addr!\n", __func__);
155
Marek Vasut5ff05292020-01-24 18:39:16 +0100156 return 0;
157}
158
159int checkboard(void)
160{
161 char *mode;
162 const char *fdt_compat;
163 int fdt_compat_len;
164
Patrick Delaunay472407a2020-03-18 09:22:49 +0100165 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100166 mode = "trusted";
167 else
168 mode = "basic";
169
170 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200171 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
172 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100173 if (fdt_compat && fdt_compat_len)
174 printf(" (%s)", fdt_compat);
175 puts("\n");
176
177 return 0;
178}
179
Marek Vasut47b98ba2020-04-22 13:18:11 +0200180#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200181static u8 brdcode __section(".data");
182static u8 ddr3code __section(".data");
183static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200184static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200185
186static void board_get_coding_straps(void)
187{
188 struct gpio_desc gpio[4];
189 ofnode node;
190 int i, ret;
191
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100192 brdcode = 0;
193 ddr3code = 0;
194 somcode = 0;
195
Marek Vasut47b98ba2020-04-22 13:18:11 +0200196 node = ofnode_path("/config");
197 if (!ofnode_valid(node)) {
198 printf("%s: no /config node?\n", __func__);
199 return;
200 }
201
Marek Vasut47b98ba2020-04-22 13:18:11 +0200202 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
203 gpio, ARRAY_SIZE(gpio),
204 GPIOD_IS_IN);
205 for (i = 0; i < ret; i++)
206 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
207
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100208 gpio_free_list_nodev(gpio, ret);
209
Marek Vasut39221b52020-04-22 13:18:14 +0200210 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
211 gpio, ARRAY_SIZE(gpio),
212 GPIOD_IS_IN);
213 for (i = 0; i < ret; i++)
214 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
215
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100216 gpio_free_list_nodev(gpio, ret);
217
Marek Vasut47b98ba2020-04-22 13:18:11 +0200218 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
219 gpio, ARRAY_SIZE(gpio),
220 GPIOD_IS_IN);
221 for (i = 0; i < ret; i++)
222 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
223
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100224 gpio_free_list_nodev(gpio, ret);
225
Harald Seiler1768f5d2023-09-27 14:46:25 +0200226 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
227 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
228 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200229}
230
231int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
232 const char *name)
233{
Marek Vasut272198e2020-04-29 15:08:38 +0200234 if (ddr3code == 1 &&
235 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
236 return 0;
237
Marek Vasut39221b52020-04-22 13:18:14 +0200238 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200239 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200240 return 0;
241
242 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200243 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200244 return 0;
245
246 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200247}
248
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200249void board_vddcore_init(u32 voltage_mv)
250{
251 if (IS_ENABLED(CONFIG_SPL_BUILD))
252 opp_voltage_mv = voltage_mv;
253}
254
Marek Vasut47b98ba2020-04-22 13:18:11 +0200255int board_early_init_f(void)
256{
Patrick Delaunayf2f25c32020-05-25 12:19:46 +0200257 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200258 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200259 board_get_coding_straps();
260
261 return 0;
262}
263
264#ifdef CONFIG_SPL_LOAD_FIT
265int board_fit_config_name_match(const char *name)
266{
Marek Vasut060cb122020-07-31 01:35:33 +0200267 const char *compat;
268 char test[128];
269
Patrick Delaunay280949c2022-06-06 16:04:15 +0200270 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200271
Marek Vasut060cb122020-07-31 01:35:33 +0200272 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
273 compat, somcode, brdcode);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200274
275 if (!strcmp(name, test))
276 return 0;
277
278 return -EINVAL;
279}
280#endif
281#endif
282
Marek Vasut5ff05292020-01-24 18:39:16 +0100283static void board_key_check(void)
284{
285#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
286 ofnode node;
287 struct gpio_desc gpio;
288 enum forced_boot_mode boot_mode = BOOT_NORMAL;
289
290 node = ofnode_path("/config");
291 if (!ofnode_valid(node)) {
292 debug("%s: no /config node?\n", __func__);
293 return;
294 }
295#ifdef CONFIG_FASTBOOT
296 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
297 &gpio, GPIOD_IS_IN)) {
298 debug("%s: could not find a /config/st,fastboot-gpios\n",
299 __func__);
300 } else {
301 if (dm_gpio_get_value(&gpio)) {
302 puts("Fastboot key pressed, ");
303 boot_mode = BOOT_FASTBOOT;
304 }
305
306 dm_gpio_free(NULL, &gpio);
307 }
308#endif
309#ifdef CONFIG_CMD_STM32PROG
310 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
311 &gpio, GPIOD_IS_IN)) {
312 debug("%s: could not find a /config/st,stm32prog-gpios\n",
313 __func__);
314 } else {
315 if (dm_gpio_get_value(&gpio)) {
316 puts("STM32Programmer key pressed, ");
317 boot_mode = BOOT_STM32PROG;
318 }
319 dm_gpio_free(NULL, &gpio);
320 }
321#endif
322
323 if (boot_mode != BOOT_NORMAL) {
324 puts("entering download mode...\n");
325 clrsetbits_le32(TAMP_BOOT_CONTEXT,
326 TAMP_BOOT_FORCED_MASK,
327 boot_mode);
328 }
329#endif
330}
331
332#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
333
334#include <usb/dwc2_udc.h>
335int g_dnl_board_usb_cable_connected(void)
336{
337 struct udevice *dwc2_udc_otg;
338 int ret;
339
340 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700341 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100342 &dwc2_udc_otg);
343 if (!ret)
344 debug("dwc2_udc_otg init failed\n");
345
346 return dwc2_udc_B_session_valid(dwc2_udc_otg);
347}
348
349#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
350#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
351
352int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
353{
354 if (!strcmp(name, "usb_dnl_dfu"))
355 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
356 else if (!strcmp(name, "usb_dnl_fastboot"))
357 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
358 &dev->idProduct);
359 else
360 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
361
362 return 0;
363}
364
365#endif /* CONFIG_USB_GADGET */
366
367#ifdef CONFIG_LED
368static int get_led(struct udevice **dev, char *led_string)
369{
Simon Glass0034d962021-08-07 07:24:01 -0600370 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100371 int ret;
372
Simon Glass0034d962021-08-07 07:24:01 -0600373 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100374 if (!led_name) {
375 pr_debug("%s: could not find %s config string\n",
376 __func__, led_string);
377 return -ENOENT;
378 }
379 ret = led_get_by_label(led_name, dev);
380 if (ret) {
381 debug("%s: get=%d\n", __func__, ret);
382 return ret;
383 }
384
385 return 0;
386}
387
388static int setup_led(enum led_state_t cmd)
389{
390 struct udevice *dev;
391 int ret;
392
393 ret = get_led(&dev, "u-boot,boot-led");
394 if (ret)
395 return ret;
396
397 ret = led_set_state(dev, cmd);
398 return ret;
399}
400#endif
401
402static void __maybe_unused led_error_blink(u32 nb_blink)
403{
404#ifdef CONFIG_LED
405 int ret;
406 struct udevice *led;
407 u32 i;
408#endif
409
410 if (!nb_blink)
411 return;
412
413#ifdef CONFIG_LED
414 ret = get_led(&led, "u-boot,error-led");
415 if (!ret) {
416 /* make u-boot,error-led blinking */
417 /* if U32_MAX and 125ms interval, for 17.02 years */
418 for (i = 0; i < 2 * nb_blink; i++) {
419 led_set_state(led, LEDST_TOGGLE);
420 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200421 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100422 }
423 }
424#endif
425
426 /* infinite: the boot process must be stopped */
427 if (nb_blink == U32_MAX)
428 hang();
429}
430
431static void sysconf_init(void)
432{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200433#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100434 u8 *syscfg;
435#ifdef CONFIG_DM_REGULATOR
436 struct udevice *pwr_dev;
437 struct udevice *pwr_reg;
438 struct udevice *dev;
439 int ret;
440 u32 otp = 0;
441#endif
442 u32 bootr;
443
444 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
445
446 /* interconnect update : select master using the port 1 */
447 /* LTDC = AXI_M9 */
448 /* GPU = AXI_M8 */
449 /* today information is hardcoded in U-Boot */
450 writel(BIT(9), syscfg + SYSCFG_ICNR);
451
452 /* disable Pull-Down for boot pin connected to VDD */
453 bootr = readl(syscfg + SYSCFG_BOOTR);
454 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
455 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
456 writel(bootr, syscfg + SYSCFG_BOOTR);
457
458#ifdef CONFIG_DM_REGULATOR
459 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
460 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
461 * The customer will have to disable this for low frequencies
462 * or if AFMUX is selected but the function not used, typically for
463 * TRACE. Otherwise, impact on power consumption.
464 *
465 * WARNING:
466 * enabling High Speed mode while VDD>2.7V
467 * with the OTP product_below_2v5 (OTP 18, BIT 13)
468 * erroneously set to 1 can damage the IC!
469 * => U-Boot set the register only if VDD < 2.7V (in DT)
470 * but this value need to be consistent with board design
471 */
472 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700473 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100474 &pwr_dev);
475 if (!ret) {
476 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700477 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100478 &dev);
479 if (ret) {
480 pr_err("Can't find stm32mp_bsec driver\n");
481 return;
482 }
483
484 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
485 if (ret > 0)
486 otp = otp & BIT(13);
487
488 /* get VDD = vdd-supply */
489 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
490 &pwr_reg);
491
492 /* check if VDD is Low Voltage */
493 if (!ret) {
494 if (regulator_get_value(pwr_reg) < 2700000) {
495 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
496 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
497 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
498 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
499 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
500 syscfg + SYSCFG_IOCTRLSETR);
501
502 if (!otp)
503 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
504 } else {
505 if (otp)
506 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
507 }
508 } else {
509 debug("VDD unknown");
510 }
511 }
512#endif
513
514 /* activate automatic I/O compensation
515 * warning: need to ensure CSI enabled and ready in clock driver
516 */
517 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
518
519 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
520 ;
521 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
522#endif
523}
524
Marek Vasut7f809fe2022-05-11 23:09:33 +0200525#ifdef CONFIG_DM_REGULATOR
526#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
527#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
528#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
529#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
530#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
531#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
532#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
533static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
534{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200535 struct udevice *dev;
536 u8 bucks_vout = 0;
537 const char *prop;
538 int len, ret;
539
540 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200541 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200542 if (!prop || !len)
543 return -ENODEV;
544
Marek Vasut52784942022-09-26 18:50:00 +0200545 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200546 return -EINVAL;
547
548 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
549 ret = uclass_get_device_by_driver(UCLASS_MISC,
550 DM_DRIVER_GET(stpmic1_nvm),
551 &dev);
552 if (ret)
553 return ret;
554
555 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
556 if (ret != 1)
557 return -EINVAL;
558
559 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
560 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
561
Marek Vasut52784942022-09-26 18:50:00 +0200562 if (strstr(prop, "avenger96")) {
563 /*
564 * Avenger96 board comes in multiple regulator configurations:
565 * - rev.100 or rev.200 have Buck3 preconfigured to
566 * 3V3 operation on boot and contains extra Enpirion
567 * EP53A8LQI DCDC converter which supplies the IO.
568 * Reduce Buck3 voltage to 2V9 to not waste power.
569 * - rev.200L have Buck3 preconfigured to 1V8 operation
570 * and have no Enpirion EP53A8LQI DCDC anymore, the
571 * IO is supplied from Buck3.
572 */
573 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
574 *uv = 2900000;
575 else
576 *uv = 1800000;
577 } else {
578 /* Testbench always respects Buck3 NVM settings */
579 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
580 *uv = 3300000;
581 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
582 *uv = 3000000;
583 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
584 *uv = 1800000;
585 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
586 *uv = 1200000;
587 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200588
589 return 0;
590}
591
592static void board_init_regulator_av96(void)
593{
594 struct udevice *rdev;
595 int ret, uv;
596
597 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
598 if (ret) /* Not Avenger96 board. */
599 return;
600
601 ret = regulator_get_by_devname("buck3", &rdev);
602 if (ret)
603 return;
604
605 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
606 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200607 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200608}
609
610static void board_init_regulator(void)
611{
612 board_init_regulator_av96();
613
614 regulators_enable_boot_on(_DEBUG);
615}
616#else
617static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
618{
619 return -EINVAL;
620}
621
622static inline void board_init_regulator(void) {}
623#endif
624
Marek Vasut5ff05292020-01-24 18:39:16 +0100625/* board dependent setup after realloc */
626int board_init(void)
627{
Marek Vasut5ff05292020-01-24 18:39:16 +0100628 board_key_check();
629
Marek Vasut7f809fe2022-05-11 23:09:33 +0200630 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100631
632 sysconf_init();
633
Marek Vasut5ff05292020-01-24 18:39:16 +0100634 return 0;
635}
636
637int board_late_init(void)
638{
639 char *boot_device;
640#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
641 const void *fdt_compat;
642 int fdt_compat_len;
643
Patrick Delaunay280949c2022-06-06 16:04:15 +0200644 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
645 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100646 if (fdt_compat && fdt_compat_len) {
647 if (strncmp(fdt_compat, "st,", 3) != 0)
648 env_set("board_name", fdt_compat);
649 else
650 env_set("board_name", fdt_compat + 3);
651 }
652#endif
653
654 /* Check the boot-source to disable bootdelay */
655 boot_device = env_get("boot_device");
656 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
657 env_set("bootdelay", "0");
658
Marek Vasut47b98ba2020-04-22 13:18:11 +0200659#ifdef CONFIG_BOARD_EARLY_INIT_F
660 env_set_ulong("dh_som_rev", somcode);
661 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200662 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200663#endif
664
Marek Vasut5ff05292020-01-24 18:39:16 +0100665 return 0;
666}
667
668void board_quiesce_devices(void)
669{
670#ifdef CONFIG_LED
671 setup_led(LEDST_OFF);
672#endif
673}
674
Marek Vasut5ff05292020-01-24 18:39:16 +0100675#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900676int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100677{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200678 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
679 int buck3off, ret, uv;
680
681 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
682 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
683 return 0;
684
685 buck3off = fdt_path_offset(blob, buck3path);
686 if (buck3off < 0) /* No Buck3 regulator found. */
687 return 0;
688
689 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
690 if (ret < 0)
691 return ret;
692
693 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
694 if (ret < 0)
695 return ret;
696
Marek Vasut5ff05292020-01-24 18:39:16 +0100697 return 0;
698}
699#endif
700
Marek Vasut5ff05292020-01-24 18:39:16 +0100701static void board_copro_image_process(ulong fw_image, size_t fw_size)
702{
703 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
704
705 if (!rproc_is_initialized())
706 if (rproc_init()) {
707 printf("Remote Processor %d initialization failed\n",
708 id);
709 return;
710 }
711
712 ret = rproc_load(id, fw_image, fw_size);
713 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
714 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
715
716 if (!ret) {
717 rproc_start(id);
718 env_set("copro_state", "booted");
719 }
720}
721
722U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);