blob: d1f662d97018882f0da14bc7f593dbd7f47d322d [file] [log] [blame]
Marek Vasut19953732020-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
6#include <common.h>
7#include <adc.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -06009#include <net.h>
Marek Vasut19953732020-01-24 18:39:16 +010010#include <asm/arch/stm32.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/gpio.h>
13#include <asm/io.h>
14#include <bootm.h>
15#include <clk.h>
16#include <config.h>
17#include <dm.h>
18#include <dm/device.h>
19#include <dm/uclass.h>
20#include <env.h>
21#include <env_internal.h>
22#include <g_dnl.h>
23#include <generic-phy.h>
24#include <hang.h>
25#include <i2c.h>
26#include <i2c_eeprom.h>
27#include <init.h>
28#include <led.h>
29#include <memalign.h>
30#include <misc.h>
31#include <mtd.h>
32#include <mtd_node.h>
33#include <netdev.h>
34#include <phy.h>
Simon Glasscd93d622020-05-10 11:40:13 -060035#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060036#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060037#include <linux/printk.h>
Marek Vasut19953732020-01-24 18:39:16 +010038#include <power/regulator.h>
39#include <remoteproc.h>
40#include <reset.h>
41#include <syscon.h>
42#include <usb.h>
43#include <usb/dwc2_udc.h>
44#include <watchdog.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060045#include <dm/ofnode.h>
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +020046#include "../common/dh_common.h"
Patrick Delaunayd1a4b092020-05-25 12:19:46 +020047#include "../../st/common/stpmic1.h"
Marek Vasut19953732020-01-24 18:39:16 +010048
49/* SYSCFG registers */
50#define SYSCFG_BOOTR 0x00
51#define SYSCFG_PMCSETR 0x04
52#define SYSCFG_IOCTRLSETR 0x18
53#define SYSCFG_ICNR 0x1C
54#define SYSCFG_CMPCR 0x20
55#define SYSCFG_CMPENSETR 0x24
56#define SYSCFG_PMCCLRR 0x44
57
58#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
59#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
60
61#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
62#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
63#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
64#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
65#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
66
67#define SYSCFG_CMPCR_SW_CTRL BIT(1)
68#define SYSCFG_CMPCR_READY BIT(8)
69
70#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
71
72#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
73#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
74
75#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
76
77#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
78#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
79#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
80#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
81
Marek Vasute07f76b2020-10-08 15:14:58 +020082#define KS_CCR 0x08
83#define KS_CCR_EEPROM BIT(9)
84#define KS_BE0 BIT(12)
85#define KS_BE1 BIT(13)
Marek Vasut5c542602021-05-03 13:31:39 +020086#define KS_CIDER 0xC0
87#define CIDER_ID 0x8870
Marek Vasute07f76b2020-10-08 15:14:58 +020088
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +020089static bool dh_stm32_mac_is_in_ks8851(void)
Marek Vasut19953732020-01-24 18:39:16 +010090{
Patrick Delaunay5a605b72022-06-06 16:04:15 +020091 ofnode node;
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +020092 u32 reg, cider, ccr;
Marek Vasut9ff770b2020-07-31 01:34:50 +020093
Patrick Delaunay5a605b72022-06-06 16:04:15 +020094 node = ofnode_path("ethernet1");
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +020095 if (!ofnode_valid(node))
96 return false;
Marek Vasut9ff770b2020-07-31 01:34:50 +020097
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +020098 if (ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
99 return false;
Marek Vasute07f76b2020-10-08 15:14:58 +0200100
101 /*
102 * KS8851 with EEPROM may use custom MAC from EEPROM, read
103 * out the KS8851 CCR register to determine whether EEPROM
104 * is present. If EEPROM is present, it must contain valid
105 * MAC address.
106 */
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200107 reg = ofnode_get_addr(node);
Marek Vasute07f76b2020-10-08 15:14:58 +0200108 if (!reg)
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200109 return false;
Marek Vasute07f76b2020-10-08 15:14:58 +0200110
Marek Vasut5c542602021-05-03 13:31:39 +0200111 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
112 cider = readw(reg);
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200113 if ((cider & 0xfff0) != CIDER_ID)
114 return true;
Marek Vasut5c542602021-05-03 13:31:39 +0200115
Marek Vasute07f76b2020-10-08 15:14:58 +0200116 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
117 ccr = readw(reg);
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200118 if (ccr & KS_CCR_EEPROM)
119 return true;
Marek Vasute07f76b2020-10-08 15:14:58 +0200120
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200121 return false;
122}
123
124static int dh_stm32_setup_ethaddr(void)
125{
126 unsigned char enetaddr[6];
127
128 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut19953732020-01-24 18:39:16 +0100129 return 0;
130
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200131 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
132 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut19953732020-01-24 18:39:16 +0100133
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200134 return -ENXIO;
135}
Marek Vasut19953732020-01-24 18:39:16 +0100136
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200137static int dh_stm32_setup_eth1addr(void)
138{
139 unsigned char enetaddr[6];
Marek Vasut19953732020-01-24 18:39:16 +0100140
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200141 if (dh_mac_is_in_env("eth1addr"))
142 return 0;
Marek Vasut9ff770b2020-07-31 01:34:50 +0200143
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200144 if (dh_stm32_mac_is_in_ks8851())
145 return 0;
146
147 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasut9ff770b2020-07-31 01:34:50 +0200148 enetaddr[5]++;
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200149 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasut9ff770b2020-07-31 01:34:50 +0200150 }
Marek Vasut19953732020-01-24 18:39:16 +0100151
Philip Oberfichtnerd084a6c2022-07-26 15:04:53 +0200152 return -ENXIO;
153}
154
155int setup_mac_address(void)
156{
157 if (dh_stm32_setup_ethaddr())
158 log_err("%s: Unable to setup ethaddr!\n", __func__);
159
160 if (dh_stm32_setup_eth1addr())
161 log_err("%s: Unable to setup eth1addr!\n", __func__);
162
Marek Vasut19953732020-01-24 18:39:16 +0100163 return 0;
164}
165
166int checkboard(void)
167{
168 char *mode;
169 const char *fdt_compat;
170 int fdt_compat_len;
171
Patrick Delaunay43df0a12020-03-18 09:22:49 +0100172 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut19953732020-01-24 18:39:16 +0100173 mode = "trusted";
174 else
175 mode = "basic";
176
177 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200178 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
179 &fdt_compat_len);
Marek Vasut19953732020-01-24 18:39:16 +0100180 if (fdt_compat && fdt_compat_len)
181 printf(" (%s)", fdt_compat);
182 puts("\n");
183
184 return 0;
185}
186
Marek Vasut731fd502020-04-22 13:18:11 +0200187#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasut0e136ec2023-05-04 21:52:08 +0200188static u8 brdcode __section(".data");
189static u8 ddr3code __section(".data");
190static u8 somcode __section(".data");
Patrick Delaunay2f238322020-05-25 12:19:47 +0200191static u32 opp_voltage_mv __section(".data");
Marek Vasut731fd502020-04-22 13:18:11 +0200192
193static void board_get_coding_straps(void)
194{
195 struct gpio_desc gpio[4];
196 ofnode node;
197 int i, ret;
198
Marek Vasut7c870f82021-11-13 03:26:39 +0100199 brdcode = 0;
200 ddr3code = 0;
201 somcode = 0;
202
Marek Vasut731fd502020-04-22 13:18:11 +0200203 node = ofnode_path("/config");
204 if (!ofnode_valid(node)) {
205 printf("%s: no /config node?\n", __func__);
206 return;
207 }
208
Marek Vasut731fd502020-04-22 13:18:11 +0200209 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
210 gpio, ARRAY_SIZE(gpio),
211 GPIOD_IS_IN);
212 for (i = 0; i < ret; i++)
213 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
214
Marek Vasut7c870f82021-11-13 03:26:39 +0100215 gpio_free_list_nodev(gpio, ret);
216
Marek Vasut2d683652020-04-22 13:18:14 +0200217 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
218 gpio, ARRAY_SIZE(gpio),
219 GPIOD_IS_IN);
220 for (i = 0; i < ret; i++)
221 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
222
Marek Vasut7c870f82021-11-13 03:26:39 +0100223 gpio_free_list_nodev(gpio, ret);
224
Marek Vasut731fd502020-04-22 13:18:11 +0200225 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
226 gpio, ARRAY_SIZE(gpio),
227 GPIOD_IS_IN);
228 for (i = 0; i < ret; i++)
229 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
230
Marek Vasut7c870f82021-11-13 03:26:39 +0100231 gpio_free_list_nodev(gpio, ret);
232
Harald Seiler07cdd222023-09-27 14:46:25 +0200233 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
234 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
235 somcode, ddr3code, brdcode);
Marek Vasut2d683652020-04-22 13:18:14 +0200236}
237
238int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
239 const char *name)
240{
Marek Vasut92ca0f72020-04-29 15:08:38 +0200241 if (ddr3code == 1 &&
242 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
243 return 0;
244
Marek Vasut2d683652020-04-22 13:18:14 +0200245 if (ddr3code == 2 &&
Marek Vasut92ca0f72020-04-29 15:08:38 +0200246 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut2d683652020-04-22 13:18:14 +0200247 return 0;
248
249 if (ddr3code == 3 &&
Marek Vasut92ca0f72020-04-29 15:08:38 +0200250 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut2d683652020-04-22 13:18:14 +0200251 return 0;
252
253 return -EINVAL;
Marek Vasut731fd502020-04-22 13:18:11 +0200254}
255
Patrick Delaunay2f238322020-05-25 12:19:47 +0200256void board_vddcore_init(u32 voltage_mv)
257{
258 if (IS_ENABLED(CONFIG_SPL_BUILD))
259 opp_voltage_mv = voltage_mv;
260}
261
Marek Vasut731fd502020-04-22 13:18:11 +0200262int board_early_init_f(void)
263{
Patrick Delaunayd1a4b092020-05-25 12:19:46 +0200264 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay2f238322020-05-25 12:19:47 +0200265 stpmic1_init(opp_voltage_mv);
Marek Vasut731fd502020-04-22 13:18:11 +0200266 board_get_coding_straps();
267
268 return 0;
269}
270
271#ifdef CONFIG_SPL_LOAD_FIT
272int board_fit_config_name_match(const char *name)
273{
Marek Vasut49650c72020-07-31 01:35:33 +0200274 const char *compat;
275 char test[128];
Marek Vasut731fd502020-04-22 13:18:11 +0200276
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200277 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut49650c72020-07-31 01:35:33 +0200278
279 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
280 compat, somcode, brdcode);
Marek Vasut731fd502020-04-22 13:18:11 +0200281
282 if (!strcmp(name, test))
283 return 0;
284
285 return -EINVAL;
286}
287#endif
288#endif
289
Marek Vasut19953732020-01-24 18:39:16 +0100290static void board_key_check(void)
291{
292#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
293 ofnode node;
294 struct gpio_desc gpio;
295 enum forced_boot_mode boot_mode = BOOT_NORMAL;
296
297 node = ofnode_path("/config");
298 if (!ofnode_valid(node)) {
299 debug("%s: no /config node?\n", __func__);
300 return;
301 }
302#ifdef CONFIG_FASTBOOT
303 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
304 &gpio, GPIOD_IS_IN)) {
305 debug("%s: could not find a /config/st,fastboot-gpios\n",
306 __func__);
307 } else {
308 if (dm_gpio_get_value(&gpio)) {
309 puts("Fastboot key pressed, ");
310 boot_mode = BOOT_FASTBOOT;
311 }
312
313 dm_gpio_free(NULL, &gpio);
314 }
315#endif
316#ifdef CONFIG_CMD_STM32PROG
317 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
318 &gpio, GPIOD_IS_IN)) {
319 debug("%s: could not find a /config/st,stm32prog-gpios\n",
320 __func__);
321 } else {
322 if (dm_gpio_get_value(&gpio)) {
323 puts("STM32Programmer key pressed, ");
324 boot_mode = BOOT_STM32PROG;
325 }
326 dm_gpio_free(NULL, &gpio);
327 }
328#endif
329
330 if (boot_mode != BOOT_NORMAL) {
331 puts("entering download mode...\n");
332 clrsetbits_le32(TAMP_BOOT_CONTEXT,
333 TAMP_BOOT_FORCED_MASK,
334 boot_mode);
335 }
336#endif
337}
338
339#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
340
341#include <usb/dwc2_udc.h>
342int g_dnl_board_usb_cable_connected(void)
343{
344 struct udevice *dwc2_udc_otg;
345 int ret;
346
347 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65e25be2020-12-28 20:34:56 -0700348 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut19953732020-01-24 18:39:16 +0100349 &dwc2_udc_otg);
350 if (!ret)
351 debug("dwc2_udc_otg init failed\n");
352
353 return dwc2_udc_B_session_valid(dwc2_udc_otg);
354}
355
356#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
357#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
358
359int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
360{
361 if (!strcmp(name, "usb_dnl_dfu"))
362 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
363 else if (!strcmp(name, "usb_dnl_fastboot"))
364 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
365 &dev->idProduct);
366 else
367 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
368
369 return 0;
370}
371
372#endif /* CONFIG_USB_GADGET */
373
374#ifdef CONFIG_LED
375static int get_led(struct udevice **dev, char *led_string)
376{
Simon Glass7de8bd02021-08-07 07:24:01 -0600377 const char *led_name;
Marek Vasut19953732020-01-24 18:39:16 +0100378 int ret;
379
Simon Glass7de8bd02021-08-07 07:24:01 -0600380 led_name = ofnode_conf_read_str(led_string);
Marek Vasut19953732020-01-24 18:39:16 +0100381 if (!led_name) {
382 pr_debug("%s: could not find %s config string\n",
383 __func__, led_string);
384 return -ENOENT;
385 }
386 ret = led_get_by_label(led_name, dev);
387 if (ret) {
388 debug("%s: get=%d\n", __func__, ret);
389 return ret;
390 }
391
392 return 0;
393}
394
395static int setup_led(enum led_state_t cmd)
396{
397 struct udevice *dev;
398 int ret;
399
400 ret = get_led(&dev, "u-boot,boot-led");
401 if (ret)
402 return ret;
403
404 ret = led_set_state(dev, cmd);
405 return ret;
406}
407#endif
408
409static void __maybe_unused led_error_blink(u32 nb_blink)
410{
411#ifdef CONFIG_LED
412 int ret;
413 struct udevice *led;
414 u32 i;
415#endif
416
417 if (!nb_blink)
418 return;
419
420#ifdef CONFIG_LED
421 ret = get_led(&led, "u-boot,error-led");
422 if (!ret) {
423 /* make u-boot,error-led blinking */
424 /* if U32_MAX and 125ms interval, for 17.02 years */
425 for (i = 0; i < 2 * nb_blink; i++) {
426 led_set_state(led, LEDST_TOGGLE);
427 mdelay(125);
Stefan Roese29caf932022-09-02 14:10:46 +0200428 schedule();
Marek Vasut19953732020-01-24 18:39:16 +0100429 }
430 }
431#endif
432
433 /* infinite: the boot process must be stopped */
434 if (nb_blink == U32_MAX)
435 hang();
436}
437
438static void sysconf_init(void)
439{
Patrick Delaunay654706b2020-04-01 09:07:33 +0200440#ifndef CONFIG_TFABOOT
Marek Vasut19953732020-01-24 18:39:16 +0100441 u8 *syscfg;
442#ifdef CONFIG_DM_REGULATOR
443 struct udevice *pwr_dev;
444 struct udevice *pwr_reg;
445 struct udevice *dev;
446 int ret;
447 u32 otp = 0;
448#endif
449 u32 bootr;
450
451 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
452
453 /* interconnect update : select master using the port 1 */
454 /* LTDC = AXI_M9 */
455 /* GPU = AXI_M8 */
456 /* today information is hardcoded in U-Boot */
457 writel(BIT(9), syscfg + SYSCFG_ICNR);
458
459 /* disable Pull-Down for boot pin connected to VDD */
460 bootr = readl(syscfg + SYSCFG_BOOTR);
461 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
462 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
463 writel(bootr, syscfg + SYSCFG_BOOTR);
464
465#ifdef CONFIG_DM_REGULATOR
466 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
467 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
468 * The customer will have to disable this for low frequencies
469 * or if AFMUX is selected but the function not used, typically for
470 * TRACE. Otherwise, impact on power consumption.
471 *
472 * WARNING:
473 * enabling High Speed mode while VDD>2.7V
474 * with the OTP product_below_2v5 (OTP 18, BIT 13)
475 * erroneously set to 1 can damage the IC!
476 * => U-Boot set the register only if VDD < 2.7V (in DT)
477 * but this value need to be consistent with board design
478 */
479 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65e25be2020-12-28 20:34:56 -0700480 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut19953732020-01-24 18:39:16 +0100481 &pwr_dev);
482 if (!ret) {
483 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700484 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut19953732020-01-24 18:39:16 +0100485 &dev);
486 if (ret) {
487 pr_err("Can't find stm32mp_bsec driver\n");
488 return;
489 }
490
491 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
492 if (ret > 0)
493 otp = otp & BIT(13);
494
495 /* get VDD = vdd-supply */
496 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
497 &pwr_reg);
498
499 /* check if VDD is Low Voltage */
500 if (!ret) {
501 if (regulator_get_value(pwr_reg) < 2700000) {
502 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
503 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
504 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
505 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
506 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
507 syscfg + SYSCFG_IOCTRLSETR);
508
509 if (!otp)
510 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
511 } else {
512 if (otp)
513 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
514 }
515 } else {
516 debug("VDD unknown");
517 }
518 }
519#endif
520
521 /* activate automatic I/O compensation
522 * warning: need to ensure CSI enabled and ready in clock driver
523 */
524 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
525
526 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
527 ;
528 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
529#endif
530}
531
Marek Vasut0adf10a2022-05-11 23:09:33 +0200532#ifdef CONFIG_DM_REGULATOR
533#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
534#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
535#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
536#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
537#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
538#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
539#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
540static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
541{
Marek Vasut0adf10a2022-05-11 23:09:33 +0200542 struct udevice *dev;
543 u8 bucks_vout = 0;
544 const char *prop;
545 int len, ret;
546
547 /* Check whether this is Avenger96 board. */
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200548 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut0adf10a2022-05-11 23:09:33 +0200549 if (!prop || !len)
550 return -ENODEV;
551
Marek Vasut0de10e22022-09-26 18:50:00 +0200552 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut0adf10a2022-05-11 23:09:33 +0200553 return -EINVAL;
554
555 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
556 ret = uclass_get_device_by_driver(UCLASS_MISC,
557 DM_DRIVER_GET(stpmic1_nvm),
558 &dev);
559 if (ret)
560 return ret;
561
562 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
563 if (ret != 1)
564 return -EINVAL;
565
566 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
567 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
568
Marek Vasut0de10e22022-09-26 18:50:00 +0200569 if (strstr(prop, "avenger96")) {
570 /*
571 * Avenger96 board comes in multiple regulator configurations:
572 * - rev.100 or rev.200 have Buck3 preconfigured to
573 * 3V3 operation on boot and contains extra Enpirion
574 * EP53A8LQI DCDC converter which supplies the IO.
575 * Reduce Buck3 voltage to 2V9 to not waste power.
576 * - rev.200L have Buck3 preconfigured to 1V8 operation
577 * and have no Enpirion EP53A8LQI DCDC anymore, the
578 * IO is supplied from Buck3.
579 */
580 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
581 *uv = 2900000;
582 else
583 *uv = 1800000;
584 } else {
585 /* Testbench always respects Buck3 NVM settings */
586 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
587 *uv = 3300000;
588 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
589 *uv = 3000000;
590 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
591 *uv = 1800000;
592 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
593 *uv = 1200000;
594 }
Marek Vasut0adf10a2022-05-11 23:09:33 +0200595
596 return 0;
597}
598
599static void board_init_regulator_av96(void)
600{
601 struct udevice *rdev;
602 int ret, uv;
603
604 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
605 if (ret) /* Not Avenger96 board. */
606 return;
607
608 ret = regulator_get_by_devname("buck3", &rdev);
609 if (ret)
610 return;
611
612 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
613 regulator_set_value(rdev, uv);
Marek Vasut34be2ad2022-09-23 03:31:22 +0200614 regulator_set_enable(rdev, true);
Marek Vasut0adf10a2022-05-11 23:09:33 +0200615}
616
617static void board_init_regulator(void)
618{
619 board_init_regulator_av96();
620
621 regulators_enable_boot_on(_DEBUG);
622}
623#else
624static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
625{
626 return -EINVAL;
627}
628
629static inline void board_init_regulator(void) {}
630#endif
631
Marek Vasut19953732020-01-24 18:39:16 +0100632/* board dependent setup after realloc */
633int board_init(void)
634{
Marek Vasut19953732020-01-24 18:39:16 +0100635 board_key_check();
636
Marek Vasut0adf10a2022-05-11 23:09:33 +0200637 board_init_regulator();
Marek Vasut19953732020-01-24 18:39:16 +0100638
639 sysconf_init();
640
Marek Vasut19953732020-01-24 18:39:16 +0100641 return 0;
642}
643
644int board_late_init(void)
645{
646 char *boot_device;
647#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
648 const void *fdt_compat;
649 int fdt_compat_len;
650
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200651 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
652 &fdt_compat_len);
Marek Vasut19953732020-01-24 18:39:16 +0100653 if (fdt_compat && fdt_compat_len) {
654 if (strncmp(fdt_compat, "st,", 3) != 0)
655 env_set("board_name", fdt_compat);
656 else
657 env_set("board_name", fdt_compat + 3);
658 }
659#endif
660
661 /* Check the boot-source to disable bootdelay */
662 boot_device = env_get("boot_device");
663 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
664 env_set("bootdelay", "0");
665
Marek Vasut731fd502020-04-22 13:18:11 +0200666#ifdef CONFIG_BOARD_EARLY_INIT_F
667 env_set_ulong("dh_som_rev", somcode);
668 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut2d683652020-04-22 13:18:14 +0200669 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut731fd502020-04-22 13:18:11 +0200670#endif
671
Marek Vasut19953732020-01-24 18:39:16 +0100672 return 0;
673}
674
675void board_quiesce_devices(void)
676{
677#ifdef CONFIG_LED
678 setup_led(LEDST_OFF);
679#endif
680}
681
682/* eth init function : weak called in eqos driver */
683int board_interface_eth_init(struct udevice *dev,
684 phy_interface_t interface_type)
685{
686 u8 *syscfg;
687 u32 value;
688 bool eth_clk_sel_reg = false;
689 bool eth_ref_clk_sel_reg = false;
690
691 /* Gigabit Ethernet 125MHz clock selection. */
Patrick Delaunay486808e2021-06-04 18:25:55 +0200692 eth_clk_sel_reg = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut19953732020-01-24 18:39:16 +0100693
694 /* Ethernet 50Mhz RMII clock selection */
695 eth_ref_clk_sel_reg =
Patrick Delaunay486808e2021-06-04 18:25:55 +0200696 dev_read_bool(dev, "st,eth-ref-clk-sel");
Marek Vasut19953732020-01-24 18:39:16 +0100697
698 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
699
700 if (!syscfg)
701 return -ENODEV;
702
703 switch (interface_type) {
704 case PHY_INTERFACE_MODE_MII:
705 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
706 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
707 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
708 break;
709 case PHY_INTERFACE_MODE_GMII:
710 if (eth_clk_sel_reg)
711 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
712 SYSCFG_PMCSETR_ETH_CLK_SEL;
713 else
714 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
715 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
716 break;
717 case PHY_INTERFACE_MODE_RMII:
718 if (eth_ref_clk_sel_reg)
719 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
720 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
721 else
722 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
723 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
724 break;
725 case PHY_INTERFACE_MODE_RGMII:
726 case PHY_INTERFACE_MODE_RGMII_ID:
727 case PHY_INTERFACE_MODE_RGMII_RXID:
728 case PHY_INTERFACE_MODE_RGMII_TXID:
729 if (eth_clk_sel_reg)
730 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
731 SYSCFG_PMCSETR_ETH_CLK_SEL;
732 else
733 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
734 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
735 break;
736 default:
737 debug("%s: Do not manage %d interface\n",
738 __func__, interface_type);
739 /* Do not manage others interfaces */
740 return -EINVAL;
741 }
742
743 /* clear and set ETH configuration bits */
744 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
745 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
746 syscfg + SYSCFG_PMCCLRR);
747 writel(value, syscfg + SYSCFG_PMCSETR);
748
749 return 0;
750}
751
Marek Vasut19953732020-01-24 18:39:16 +0100752#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900753int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut19953732020-01-24 18:39:16 +0100754{
Marek Vasut0adf10a2022-05-11 23:09:33 +0200755 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
756 int buck3off, ret, uv;
757
758 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 Vasut19953732020-01-24 18:39:16 +0100774 return 0;
775}
776#endif
777
Marek Vasut19953732020-01-24 18:39:16 +0100778static void board_copro_image_process(ulong fw_image, size_t fw_size)
779{
780 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
781
782 if (!rproc_is_initialized())
783 if (rproc_init()) {
784 printf("Remote Processor %d initialization failed\n",
785 id);
786 return;
787 }
788
789 ret = rproc_load(id, fw_image, fw_size);
790 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
791 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
792
793 if (!ret) {
794 rproc_start(id);
795 env_set("copro_state", "booted");
796 }
797}
798
799U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);