blob: 341d095d689dd9b83389cb2563c57abef87f7b6d [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
Marek Vasut2d683652020-04-22 13:18:14 +0200233 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
234 somcode, ddr3code, brdcode);
235}
236
237int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
238 const char *name)
239{
Marek Vasut92ca0f72020-04-29 15:08:38 +0200240 if (ddr3code == 1 &&
241 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
242 return 0;
243
Marek Vasut2d683652020-04-22 13:18:14 +0200244 if (ddr3code == 2 &&
Marek Vasut92ca0f72020-04-29 15:08:38 +0200245 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut2d683652020-04-22 13:18:14 +0200246 return 0;
247
248 if (ddr3code == 3 &&
Marek Vasut92ca0f72020-04-29 15:08:38 +0200249 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut2d683652020-04-22 13:18:14 +0200250 return 0;
251
252 return -EINVAL;
Marek Vasut731fd502020-04-22 13:18:11 +0200253}
254
Patrick Delaunay2f238322020-05-25 12:19:47 +0200255void board_vddcore_init(u32 voltage_mv)
256{
257 if (IS_ENABLED(CONFIG_SPL_BUILD))
258 opp_voltage_mv = voltage_mv;
259}
260
Marek Vasut731fd502020-04-22 13:18:11 +0200261int board_early_init_f(void)
262{
Patrick Delaunayd1a4b092020-05-25 12:19:46 +0200263 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay2f238322020-05-25 12:19:47 +0200264 stpmic1_init(opp_voltage_mv);
Marek Vasut731fd502020-04-22 13:18:11 +0200265 board_get_coding_straps();
266
267 return 0;
268}
269
270#ifdef CONFIG_SPL_LOAD_FIT
271int board_fit_config_name_match(const char *name)
272{
Marek Vasut49650c72020-07-31 01:35:33 +0200273 const char *compat;
274 char test[128];
Marek Vasut731fd502020-04-22 13:18:11 +0200275
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200276 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut49650c72020-07-31 01:35:33 +0200277
278 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
279 compat, somcode, brdcode);
Marek Vasut731fd502020-04-22 13:18:11 +0200280
281 if (!strcmp(name, test))
282 return 0;
283
284 return -EINVAL;
285}
286#endif
287#endif
288
Marek Vasut19953732020-01-24 18:39:16 +0100289static void board_key_check(void)
290{
291#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
292 ofnode node;
293 struct gpio_desc gpio;
294 enum forced_boot_mode boot_mode = BOOT_NORMAL;
295
296 node = ofnode_path("/config");
297 if (!ofnode_valid(node)) {
298 debug("%s: no /config node?\n", __func__);
299 return;
300 }
301#ifdef CONFIG_FASTBOOT
302 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
303 &gpio, GPIOD_IS_IN)) {
304 debug("%s: could not find a /config/st,fastboot-gpios\n",
305 __func__);
306 } else {
307 if (dm_gpio_get_value(&gpio)) {
308 puts("Fastboot key pressed, ");
309 boot_mode = BOOT_FASTBOOT;
310 }
311
312 dm_gpio_free(NULL, &gpio);
313 }
314#endif
315#ifdef CONFIG_CMD_STM32PROG
316 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
317 &gpio, GPIOD_IS_IN)) {
318 debug("%s: could not find a /config/st,stm32prog-gpios\n",
319 __func__);
320 } else {
321 if (dm_gpio_get_value(&gpio)) {
322 puts("STM32Programmer key pressed, ");
323 boot_mode = BOOT_STM32PROG;
324 }
325 dm_gpio_free(NULL, &gpio);
326 }
327#endif
328
329 if (boot_mode != BOOT_NORMAL) {
330 puts("entering download mode...\n");
331 clrsetbits_le32(TAMP_BOOT_CONTEXT,
332 TAMP_BOOT_FORCED_MASK,
333 boot_mode);
334 }
335#endif
336}
337
338#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
339
340#include <usb/dwc2_udc.h>
341int g_dnl_board_usb_cable_connected(void)
342{
343 struct udevice *dwc2_udc_otg;
344 int ret;
345
346 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65e25be2020-12-28 20:34:56 -0700347 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut19953732020-01-24 18:39:16 +0100348 &dwc2_udc_otg);
349 if (!ret)
350 debug("dwc2_udc_otg init failed\n");
351
352 return dwc2_udc_B_session_valid(dwc2_udc_otg);
353}
354
355#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
356#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
357
358int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
359{
360 if (!strcmp(name, "usb_dnl_dfu"))
361 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
362 else if (!strcmp(name, "usb_dnl_fastboot"))
363 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
364 &dev->idProduct);
365 else
366 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
367
368 return 0;
369}
370
371#endif /* CONFIG_USB_GADGET */
372
373#ifdef CONFIG_LED
374static int get_led(struct udevice **dev, char *led_string)
375{
Simon Glass7de8bd02021-08-07 07:24:01 -0600376 const char *led_name;
Marek Vasut19953732020-01-24 18:39:16 +0100377 int ret;
378
Simon Glass7de8bd02021-08-07 07:24:01 -0600379 led_name = ofnode_conf_read_str(led_string);
Marek Vasut19953732020-01-24 18:39:16 +0100380 if (!led_name) {
381 pr_debug("%s: could not find %s config string\n",
382 __func__, led_string);
383 return -ENOENT;
384 }
385 ret = led_get_by_label(led_name, dev);
386 if (ret) {
387 debug("%s: get=%d\n", __func__, ret);
388 return ret;
389 }
390
391 return 0;
392}
393
394static int setup_led(enum led_state_t cmd)
395{
396 struct udevice *dev;
397 int ret;
398
399 ret = get_led(&dev, "u-boot,boot-led");
400 if (ret)
401 return ret;
402
403 ret = led_set_state(dev, cmd);
404 return ret;
405}
406#endif
407
408static void __maybe_unused led_error_blink(u32 nb_blink)
409{
410#ifdef CONFIG_LED
411 int ret;
412 struct udevice *led;
413 u32 i;
414#endif
415
416 if (!nb_blink)
417 return;
418
419#ifdef CONFIG_LED
420 ret = get_led(&led, "u-boot,error-led");
421 if (!ret) {
422 /* make u-boot,error-led blinking */
423 /* if U32_MAX and 125ms interval, for 17.02 years */
424 for (i = 0; i < 2 * nb_blink; i++) {
425 led_set_state(led, LEDST_TOGGLE);
426 mdelay(125);
Stefan Roese29caf932022-09-02 14:10:46 +0200427 schedule();
Marek Vasut19953732020-01-24 18:39:16 +0100428 }
429 }
430#endif
431
432 /* infinite: the boot process must be stopped */
433 if (nb_blink == U32_MAX)
434 hang();
435}
436
437static void sysconf_init(void)
438{
Patrick Delaunay654706b2020-04-01 09:07:33 +0200439#ifndef CONFIG_TFABOOT
Marek Vasut19953732020-01-24 18:39:16 +0100440 u8 *syscfg;
441#ifdef CONFIG_DM_REGULATOR
442 struct udevice *pwr_dev;
443 struct udevice *pwr_reg;
444 struct udevice *dev;
445 int ret;
446 u32 otp = 0;
447#endif
448 u32 bootr;
449
450 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
451
452 /* interconnect update : select master using the port 1 */
453 /* LTDC = AXI_M9 */
454 /* GPU = AXI_M8 */
455 /* today information is hardcoded in U-Boot */
456 writel(BIT(9), syscfg + SYSCFG_ICNR);
457
458 /* disable Pull-Down for boot pin connected to VDD */
459 bootr = readl(syscfg + SYSCFG_BOOTR);
460 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
461 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
462 writel(bootr, syscfg + SYSCFG_BOOTR);
463
464#ifdef CONFIG_DM_REGULATOR
465 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
466 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
467 * The customer will have to disable this for low frequencies
468 * or if AFMUX is selected but the function not used, typically for
469 * TRACE. Otherwise, impact on power consumption.
470 *
471 * WARNING:
472 * enabling High Speed mode while VDD>2.7V
473 * with the OTP product_below_2v5 (OTP 18, BIT 13)
474 * erroneously set to 1 can damage the IC!
475 * => U-Boot set the register only if VDD < 2.7V (in DT)
476 * but this value need to be consistent with board design
477 */
478 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65e25be2020-12-28 20:34:56 -0700479 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut19953732020-01-24 18:39:16 +0100480 &pwr_dev);
481 if (!ret) {
482 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700483 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut19953732020-01-24 18:39:16 +0100484 &dev);
485 if (ret) {
486 pr_err("Can't find stm32mp_bsec driver\n");
487 return;
488 }
489
490 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
491 if (ret > 0)
492 otp = otp & BIT(13);
493
494 /* get VDD = vdd-supply */
495 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
496 &pwr_reg);
497
498 /* check if VDD is Low Voltage */
499 if (!ret) {
500 if (regulator_get_value(pwr_reg) < 2700000) {
501 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
502 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
503 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
504 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
505 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
506 syscfg + SYSCFG_IOCTRLSETR);
507
508 if (!otp)
509 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
510 } else {
511 if (otp)
512 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
513 }
514 } else {
515 debug("VDD unknown");
516 }
517 }
518#endif
519
520 /* activate automatic I/O compensation
521 * warning: need to ensure CSI enabled and ready in clock driver
522 */
523 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
524
525 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
526 ;
527 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
528#endif
529}
530
Marek Vasut0adf10a2022-05-11 23:09:33 +0200531#ifdef CONFIG_DM_REGULATOR
532#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
533#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
534#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
535#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
536#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
537#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
538#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
539static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
540{
Marek Vasut0adf10a2022-05-11 23:09:33 +0200541 struct udevice *dev;
542 u8 bucks_vout = 0;
543 const char *prop;
544 int len, ret;
545
546 /* Check whether this is Avenger96 board. */
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200547 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut0adf10a2022-05-11 23:09:33 +0200548 if (!prop || !len)
549 return -ENODEV;
550
Marek Vasut0de10e22022-09-26 18:50:00 +0200551 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut0adf10a2022-05-11 23:09:33 +0200552 return -EINVAL;
553
554 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
555 ret = uclass_get_device_by_driver(UCLASS_MISC,
556 DM_DRIVER_GET(stpmic1_nvm),
557 &dev);
558 if (ret)
559 return ret;
560
561 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
562 if (ret != 1)
563 return -EINVAL;
564
565 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
566 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
567
Marek Vasut0de10e22022-09-26 18:50:00 +0200568 if (strstr(prop, "avenger96")) {
569 /*
570 * Avenger96 board comes in multiple regulator configurations:
571 * - rev.100 or rev.200 have Buck3 preconfigured to
572 * 3V3 operation on boot and contains extra Enpirion
573 * EP53A8LQI DCDC converter which supplies the IO.
574 * Reduce Buck3 voltage to 2V9 to not waste power.
575 * - rev.200L have Buck3 preconfigured to 1V8 operation
576 * and have no Enpirion EP53A8LQI DCDC anymore, the
577 * IO is supplied from Buck3.
578 */
579 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
580 *uv = 2900000;
581 else
582 *uv = 1800000;
583 } else {
584 /* Testbench always respects Buck3 NVM settings */
585 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
586 *uv = 3300000;
587 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
588 *uv = 3000000;
589 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
590 *uv = 1800000;
591 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
592 *uv = 1200000;
593 }
Marek Vasut0adf10a2022-05-11 23:09:33 +0200594
595 return 0;
596}
597
598static void board_init_regulator_av96(void)
599{
600 struct udevice *rdev;
601 int ret, uv;
602
603 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
604 if (ret) /* Not Avenger96 board. */
605 return;
606
607 ret = regulator_get_by_devname("buck3", &rdev);
608 if (ret)
609 return;
610
611 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
612 regulator_set_value(rdev, uv);
Marek Vasut34be2ad2022-09-23 03:31:22 +0200613 regulator_set_enable(rdev, true);
Marek Vasut0adf10a2022-05-11 23:09:33 +0200614}
615
616static void board_init_regulator(void)
617{
618 board_init_regulator_av96();
619
620 regulators_enable_boot_on(_DEBUG);
621}
622#else
623static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
624{
625 return -EINVAL;
626}
627
628static inline void board_init_regulator(void) {}
629#endif
630
Marek Vasut19953732020-01-24 18:39:16 +0100631/* board dependent setup after realloc */
632int board_init(void)
633{
Marek Vasut19953732020-01-24 18:39:16 +0100634 board_key_check();
635
Marek Vasut0adf10a2022-05-11 23:09:33 +0200636 board_init_regulator();
Marek Vasut19953732020-01-24 18:39:16 +0100637
638 sysconf_init();
639
Marek Vasut19953732020-01-24 18:39:16 +0100640 return 0;
641}
642
643int board_late_init(void)
644{
645 char *boot_device;
646#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
647 const void *fdt_compat;
648 int fdt_compat_len;
649
Patrick Delaunay5a605b72022-06-06 16:04:15 +0200650 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
651 &fdt_compat_len);
Marek Vasut19953732020-01-24 18:39:16 +0100652 if (fdt_compat && fdt_compat_len) {
653 if (strncmp(fdt_compat, "st,", 3) != 0)
654 env_set("board_name", fdt_compat);
655 else
656 env_set("board_name", fdt_compat + 3);
657 }
658#endif
659
660 /* Check the boot-source to disable bootdelay */
661 boot_device = env_get("boot_device");
662 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
663 env_set("bootdelay", "0");
664
Marek Vasut731fd502020-04-22 13:18:11 +0200665#ifdef CONFIG_BOARD_EARLY_INIT_F
666 env_set_ulong("dh_som_rev", somcode);
667 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut2d683652020-04-22 13:18:14 +0200668 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut731fd502020-04-22 13:18:11 +0200669#endif
670
Marek Vasut19953732020-01-24 18:39:16 +0100671 return 0;
672}
673
674void board_quiesce_devices(void)
675{
676#ifdef CONFIG_LED
677 setup_led(LEDST_OFF);
678#endif
679}
680
681/* eth init function : weak called in eqos driver */
682int board_interface_eth_init(struct udevice *dev,
683 phy_interface_t interface_type)
684{
685 u8 *syscfg;
686 u32 value;
687 bool eth_clk_sel_reg = false;
688 bool eth_ref_clk_sel_reg = false;
689
690 /* Gigabit Ethernet 125MHz clock selection. */
Patrick Delaunay486808e2021-06-04 18:25:55 +0200691 eth_clk_sel_reg = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut19953732020-01-24 18:39:16 +0100692
693 /* Ethernet 50Mhz RMII clock selection */
694 eth_ref_clk_sel_reg =
Patrick Delaunay486808e2021-06-04 18:25:55 +0200695 dev_read_bool(dev, "st,eth-ref-clk-sel");
Marek Vasut19953732020-01-24 18:39:16 +0100696
697 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
698
699 if (!syscfg)
700 return -ENODEV;
701
702 switch (interface_type) {
703 case PHY_INTERFACE_MODE_MII:
704 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
705 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
706 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
707 break;
708 case PHY_INTERFACE_MODE_GMII:
709 if (eth_clk_sel_reg)
710 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
711 SYSCFG_PMCSETR_ETH_CLK_SEL;
712 else
713 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
714 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
715 break;
716 case PHY_INTERFACE_MODE_RMII:
717 if (eth_ref_clk_sel_reg)
718 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
719 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
720 else
721 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
722 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
723 break;
724 case PHY_INTERFACE_MODE_RGMII:
725 case PHY_INTERFACE_MODE_RGMII_ID:
726 case PHY_INTERFACE_MODE_RGMII_RXID:
727 case PHY_INTERFACE_MODE_RGMII_TXID:
728 if (eth_clk_sel_reg)
729 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
730 SYSCFG_PMCSETR_ETH_CLK_SEL;
731 else
732 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
733 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
734 break;
735 default:
736 debug("%s: Do not manage %d interface\n",
737 __func__, interface_type);
738 /* Do not manage others interfaces */
739 return -EINVAL;
740 }
741
742 /* clear and set ETH configuration bits */
743 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
744 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
745 syscfg + SYSCFG_PMCCLRR);
746 writel(value, syscfg + SYSCFG_PMCSETR);
747
748 return 0;
749}
750
Marek Vasut19953732020-01-24 18:39:16 +0100751#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900752int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut19953732020-01-24 18:39:16 +0100753{
Marek Vasut0adf10a2022-05-11 23:09:33 +0200754 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
755 int buck3off, ret, uv;
756
757 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
758 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
759 return 0;
760
761 buck3off = fdt_path_offset(blob, buck3path);
762 if (buck3off < 0) /* No Buck3 regulator found. */
763 return 0;
764
765 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
766 if (ret < 0)
767 return ret;
768
769 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
770 if (ret < 0)
771 return ret;
772
Marek Vasut19953732020-01-24 18:39:16 +0100773 return 0;
774}
775#endif
776
Marek Vasut19953732020-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);