blob: 079dfff03895522676d0aac7d004836fa4940e1f [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
6#include <common.h>
7#include <adc.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <net.h>
Marek Vasut5ff05292020-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 Glass4dcacfc2020-05-10 11:40:13 -060035#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060036#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060037#include <linux/printk.h>
Marek Vasut5ff05292020-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 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
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 Vasut145a8762020-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 Vasutb2b31c12021-05-03 13:31:39 +020086#define KS_CIDER 0xC0
87#define CIDER_ID 0x8870
Marek Vasut145a8762020-10-08 15:14:58 +020088
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020089static bool dh_stm32_mac_is_in_ks8851(void)
Marek Vasut5ff05292020-01-24 18:39:16 +010090{
Patrick Delaunay280949c2022-06-06 16:04:15 +020091 ofnode node;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020092 u32 reg, cider, ccr;
Marek Vasutb0a2a492020-07-31 01:34:50 +020093
Patrick Delaunay280949c2022-06-06 16:04:15 +020094 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020095 if (!ofnode_valid(node))
96 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020097
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020098 if (ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
99 return false;
Marek Vasut145a8762020-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 Delaunay280949c2022-06-06 16:04:15 +0200107 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +0200108 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200109 return false;
Marek Vasut145a8762020-10-08 15:14:58 +0200110
Marek Vasutb2b31c12021-05-03 13:31:39 +0200111 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
112 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200113 if ((cider & 0xfff0) != CIDER_ID)
114 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200115
Marek Vasut145a8762020-10-08 15:14:58 +0200116 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
117 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200118 if (ccr & KS_CCR_EEPROM)
119 return true;
120
121 return false;
122}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200123
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200124static int dh_stm32_setup_ethaddr(void)
125{
126 unsigned char enetaddr[6];
127
128 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100129 return 0;
130
Marek Vasut29ab1a92024-03-12 22:15:58 +0100131 if (dh_get_mac_is_enabled("ethernet0"))
132 return 0;
133
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200134 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
135 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100136
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200137 return -ENXIO;
138}
Marek Vasut5ff05292020-01-24 18:39:16 +0100139
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200140static int dh_stm32_setup_eth1addr(void)
141{
142 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100143
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200144 if (dh_mac_is_in_env("eth1addr"))
145 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200146
Marek Vasut29ab1a92024-03-12 22:15:58 +0100147 if (dh_get_mac_is_enabled("ethernet1"))
148 return 0;
149
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200150 if (dh_stm32_mac_is_in_ks8851())
151 return 0;
152
153 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200154 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200155 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200156 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100157
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200158 return -ENXIO;
159}
160
161int setup_mac_address(void)
162{
163 if (dh_stm32_setup_ethaddr())
164 log_err("%s: Unable to setup ethaddr!\n", __func__);
165
166 if (dh_stm32_setup_eth1addr())
167 log_err("%s: Unable to setup eth1addr!\n", __func__);
168
Marek Vasut5ff05292020-01-24 18:39:16 +0100169 return 0;
170}
171
172int checkboard(void)
173{
174 char *mode;
175 const char *fdt_compat;
176 int fdt_compat_len;
177
Patrick Delaunay472407a2020-03-18 09:22:49 +0100178 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100179 mode = "trusted";
180 else
181 mode = "basic";
182
183 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200184 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
185 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100186 if (fdt_compat && fdt_compat_len)
187 printf(" (%s)", fdt_compat);
188 puts("\n");
189
190 return 0;
191}
192
Marek Vasut47b98ba2020-04-22 13:18:11 +0200193#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200194static u8 brdcode __section(".data");
195static u8 ddr3code __section(".data");
196static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200197static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200198
199static void board_get_coding_straps(void)
200{
201 struct gpio_desc gpio[4];
202 ofnode node;
203 int i, ret;
204
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100205 brdcode = 0;
206 ddr3code = 0;
207 somcode = 0;
208
Marek Vasut47b98ba2020-04-22 13:18:11 +0200209 node = ofnode_path("/config");
210 if (!ofnode_valid(node)) {
211 printf("%s: no /config node?\n", __func__);
212 return;
213 }
214
Marek Vasut47b98ba2020-04-22 13:18:11 +0200215 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
216 gpio, ARRAY_SIZE(gpio),
217 GPIOD_IS_IN);
218 for (i = 0; i < ret; i++)
219 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
220
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100221 gpio_free_list_nodev(gpio, ret);
222
Marek Vasut39221b52020-04-22 13:18:14 +0200223 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
224 gpio, ARRAY_SIZE(gpio),
225 GPIOD_IS_IN);
226 for (i = 0; i < ret; i++)
227 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
228
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100229 gpio_free_list_nodev(gpio, ret);
230
Marek Vasut47b98ba2020-04-22 13:18:11 +0200231 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
232 gpio, ARRAY_SIZE(gpio),
233 GPIOD_IS_IN);
234 for (i = 0; i < ret; i++)
235 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
236
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100237 gpio_free_list_nodev(gpio, ret);
238
Harald Seiler1768f5d2023-09-27 14:46:25 +0200239 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
240 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
241 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200242}
243
244int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
245 const char *name)
246{
Marek Vasut272198e2020-04-29 15:08:38 +0200247 if (ddr3code == 1 &&
248 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
249 return 0;
250
Marek Vasut39221b52020-04-22 13:18:14 +0200251 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200252 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200253 return 0;
254
255 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200256 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200257 return 0;
258
259 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200260}
261
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200262void board_vddcore_init(u32 voltage_mv)
263{
264 if (IS_ENABLED(CONFIG_SPL_BUILD))
265 opp_voltage_mv = voltage_mv;
266}
267
Marek Vasut47b98ba2020-04-22 13:18:11 +0200268int board_early_init_f(void)
269{
Patrick Delaunayf2f25c32020-05-25 12:19:46 +0200270 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200271 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200272 board_get_coding_straps();
273
274 return 0;
275}
276
277#ifdef CONFIG_SPL_LOAD_FIT
278int board_fit_config_name_match(const char *name)
279{
Marek Vasut060cb122020-07-31 01:35:33 +0200280 const char *compat;
281 char test[128];
282
Patrick Delaunay280949c2022-06-06 16:04:15 +0200283 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200284
Marek Vasut060cb122020-07-31 01:35:33 +0200285 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
286 compat, somcode, brdcode);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200287
288 if (!strcmp(name, test))
289 return 0;
290
291 return -EINVAL;
292}
293#endif
294#endif
295
Marek Vasut5ff05292020-01-24 18:39:16 +0100296static void board_key_check(void)
297{
298#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
299 ofnode node;
300 struct gpio_desc gpio;
301 enum forced_boot_mode boot_mode = BOOT_NORMAL;
302
303 node = ofnode_path("/config");
304 if (!ofnode_valid(node)) {
305 debug("%s: no /config node?\n", __func__);
306 return;
307 }
308#ifdef CONFIG_FASTBOOT
309 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
310 &gpio, GPIOD_IS_IN)) {
311 debug("%s: could not find a /config/st,fastboot-gpios\n",
312 __func__);
313 } else {
314 if (dm_gpio_get_value(&gpio)) {
315 puts("Fastboot key pressed, ");
316 boot_mode = BOOT_FASTBOOT;
317 }
318
319 dm_gpio_free(NULL, &gpio);
320 }
321#endif
322#ifdef CONFIG_CMD_STM32PROG
323 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
324 &gpio, GPIOD_IS_IN)) {
325 debug("%s: could not find a /config/st,stm32prog-gpios\n",
326 __func__);
327 } else {
328 if (dm_gpio_get_value(&gpio)) {
329 puts("STM32Programmer key pressed, ");
330 boot_mode = BOOT_STM32PROG;
331 }
332 dm_gpio_free(NULL, &gpio);
333 }
334#endif
335
336 if (boot_mode != BOOT_NORMAL) {
337 puts("entering download mode...\n");
338 clrsetbits_le32(TAMP_BOOT_CONTEXT,
339 TAMP_BOOT_FORCED_MASK,
340 boot_mode);
341 }
342#endif
343}
344
345#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
346
347#include <usb/dwc2_udc.h>
348int g_dnl_board_usb_cable_connected(void)
349{
350 struct udevice *dwc2_udc_otg;
351 int ret;
352
353 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700354 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100355 &dwc2_udc_otg);
356 if (!ret)
357 debug("dwc2_udc_otg init failed\n");
358
359 return dwc2_udc_B_session_valid(dwc2_udc_otg);
360}
361
362#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
363#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
364
365int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
366{
367 if (!strcmp(name, "usb_dnl_dfu"))
368 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
369 else if (!strcmp(name, "usb_dnl_fastboot"))
370 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
371 &dev->idProduct);
372 else
373 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
374
375 return 0;
376}
377
378#endif /* CONFIG_USB_GADGET */
379
380#ifdef CONFIG_LED
381static int get_led(struct udevice **dev, char *led_string)
382{
Simon Glass0034d962021-08-07 07:24:01 -0600383 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100384 int ret;
385
Simon Glass0034d962021-08-07 07:24:01 -0600386 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100387 if (!led_name) {
388 pr_debug("%s: could not find %s config string\n",
389 __func__, led_string);
390 return -ENOENT;
391 }
392 ret = led_get_by_label(led_name, dev);
393 if (ret) {
394 debug("%s: get=%d\n", __func__, ret);
395 return ret;
396 }
397
398 return 0;
399}
400
401static int setup_led(enum led_state_t cmd)
402{
403 struct udevice *dev;
404 int ret;
405
406 ret = get_led(&dev, "u-boot,boot-led");
407 if (ret)
408 return ret;
409
410 ret = led_set_state(dev, cmd);
411 return ret;
412}
413#endif
414
415static void __maybe_unused led_error_blink(u32 nb_blink)
416{
417#ifdef CONFIG_LED
418 int ret;
419 struct udevice *led;
420 u32 i;
421#endif
422
423 if (!nb_blink)
424 return;
425
426#ifdef CONFIG_LED
427 ret = get_led(&led, "u-boot,error-led");
428 if (!ret) {
429 /* make u-boot,error-led blinking */
430 /* if U32_MAX and 125ms interval, for 17.02 years */
431 for (i = 0; i < 2 * nb_blink; i++) {
432 led_set_state(led, LEDST_TOGGLE);
433 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200434 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100435 }
436 }
437#endif
438
439 /* infinite: the boot process must be stopped */
440 if (nb_blink == U32_MAX)
441 hang();
442}
443
444static void sysconf_init(void)
445{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200446#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100447 u8 *syscfg;
448#ifdef CONFIG_DM_REGULATOR
449 struct udevice *pwr_dev;
450 struct udevice *pwr_reg;
451 struct udevice *dev;
452 int ret;
453 u32 otp = 0;
454#endif
455 u32 bootr;
456
457 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
458
459 /* interconnect update : select master using the port 1 */
460 /* LTDC = AXI_M9 */
461 /* GPU = AXI_M8 */
462 /* today information is hardcoded in U-Boot */
463 writel(BIT(9), syscfg + SYSCFG_ICNR);
464
465 /* disable Pull-Down for boot pin connected to VDD */
466 bootr = readl(syscfg + SYSCFG_BOOTR);
467 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
468 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
469 writel(bootr, syscfg + SYSCFG_BOOTR);
470
471#ifdef CONFIG_DM_REGULATOR
472 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
473 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
474 * The customer will have to disable this for low frequencies
475 * or if AFMUX is selected but the function not used, typically for
476 * TRACE. Otherwise, impact on power consumption.
477 *
478 * WARNING:
479 * enabling High Speed mode while VDD>2.7V
480 * with the OTP product_below_2v5 (OTP 18, BIT 13)
481 * erroneously set to 1 can damage the IC!
482 * => U-Boot set the register only if VDD < 2.7V (in DT)
483 * but this value need to be consistent with board design
484 */
485 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700486 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100487 &pwr_dev);
488 if (!ret) {
489 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700490 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100491 &dev);
492 if (ret) {
493 pr_err("Can't find stm32mp_bsec driver\n");
494 return;
495 }
496
497 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
498 if (ret > 0)
499 otp = otp & BIT(13);
500
501 /* get VDD = vdd-supply */
502 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
503 &pwr_reg);
504
505 /* check if VDD is Low Voltage */
506 if (!ret) {
507 if (regulator_get_value(pwr_reg) < 2700000) {
508 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
509 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
510 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
511 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
512 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
513 syscfg + SYSCFG_IOCTRLSETR);
514
515 if (!otp)
516 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
517 } else {
518 if (otp)
519 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
520 }
521 } else {
522 debug("VDD unknown");
523 }
524 }
525#endif
526
527 /* activate automatic I/O compensation
528 * warning: need to ensure CSI enabled and ready in clock driver
529 */
530 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
531
532 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
533 ;
534 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
535#endif
536}
537
Marek Vasut7f809fe2022-05-11 23:09:33 +0200538#ifdef CONFIG_DM_REGULATOR
539#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
540#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
541#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
542#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
543#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
544#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
545#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
546static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
547{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200548 struct udevice *dev;
549 u8 bucks_vout = 0;
550 const char *prop;
551 int len, ret;
552
553 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200554 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200555 if (!prop || !len)
556 return -ENODEV;
557
Marek Vasut52784942022-09-26 18:50:00 +0200558 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200559 return -EINVAL;
560
561 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
562 ret = uclass_get_device_by_driver(UCLASS_MISC,
563 DM_DRIVER_GET(stpmic1_nvm),
564 &dev);
565 if (ret)
566 return ret;
567
568 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
569 if (ret != 1)
570 return -EINVAL;
571
572 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
573 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
574
Marek Vasut52784942022-09-26 18:50:00 +0200575 if (strstr(prop, "avenger96")) {
576 /*
577 * Avenger96 board comes in multiple regulator configurations:
578 * - rev.100 or rev.200 have Buck3 preconfigured to
579 * 3V3 operation on boot and contains extra Enpirion
580 * EP53A8LQI DCDC converter which supplies the IO.
581 * Reduce Buck3 voltage to 2V9 to not waste power.
582 * - rev.200L have Buck3 preconfigured to 1V8 operation
583 * and have no Enpirion EP53A8LQI DCDC anymore, the
584 * IO is supplied from Buck3.
585 */
586 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
587 *uv = 2900000;
588 else
589 *uv = 1800000;
590 } else {
591 /* Testbench always respects Buck3 NVM settings */
592 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
593 *uv = 3300000;
594 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
595 *uv = 3000000;
596 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
597 *uv = 1800000;
598 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
599 *uv = 1200000;
600 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200601
602 return 0;
603}
604
605static void board_init_regulator_av96(void)
606{
607 struct udevice *rdev;
608 int ret, uv;
609
610 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
611 if (ret) /* Not Avenger96 board. */
612 return;
613
614 ret = regulator_get_by_devname("buck3", &rdev);
615 if (ret)
616 return;
617
618 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
619 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200620 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200621}
622
623static void board_init_regulator(void)
624{
625 board_init_regulator_av96();
626
627 regulators_enable_boot_on(_DEBUG);
628}
629#else
630static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
631{
632 return -EINVAL;
633}
634
635static inline void board_init_regulator(void) {}
636#endif
637
Marek Vasut5ff05292020-01-24 18:39:16 +0100638/* board dependent setup after realloc */
639int board_init(void)
640{
Marek Vasut5ff05292020-01-24 18:39:16 +0100641 board_key_check();
642
Marek Vasut7f809fe2022-05-11 23:09:33 +0200643 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100644
645 sysconf_init();
646
Marek Vasut5ff05292020-01-24 18:39:16 +0100647 return 0;
648}
649
650int board_late_init(void)
651{
652 char *boot_device;
653#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
654 const void *fdt_compat;
655 int fdt_compat_len;
656
Patrick Delaunay280949c2022-06-06 16:04:15 +0200657 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
658 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100659 if (fdt_compat && fdt_compat_len) {
660 if (strncmp(fdt_compat, "st,", 3) != 0)
661 env_set("board_name", fdt_compat);
662 else
663 env_set("board_name", fdt_compat + 3);
664 }
665#endif
666
667 /* Check the boot-source to disable bootdelay */
668 boot_device = env_get("boot_device");
669 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
670 env_set("bootdelay", "0");
671
Marek Vasut47b98ba2020-04-22 13:18:11 +0200672#ifdef CONFIG_BOARD_EARLY_INIT_F
673 env_set_ulong("dh_som_rev", somcode);
674 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200675 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200676#endif
677
Marek Vasut5ff05292020-01-24 18:39:16 +0100678 return 0;
679}
680
681void board_quiesce_devices(void)
682{
683#ifdef CONFIG_LED
684 setup_led(LEDST_OFF);
685#endif
686}
687
688/* eth init function : weak called in eqos driver */
689int board_interface_eth_init(struct udevice *dev,
690 phy_interface_t interface_type)
691{
692 u8 *syscfg;
693 u32 value;
694 bool eth_clk_sel_reg = false;
695 bool eth_ref_clk_sel_reg = false;
696
697 /* Gigabit Ethernet 125MHz clock selection. */
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200698 eth_clk_sel_reg = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut5ff05292020-01-24 18:39:16 +0100699
700 /* Ethernet 50Mhz RMII clock selection */
701 eth_ref_clk_sel_reg =
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200702 dev_read_bool(dev, "st,eth-ref-clk-sel");
Marek Vasut5ff05292020-01-24 18:39:16 +0100703
704 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
705
706 if (!syscfg)
707 return -ENODEV;
708
709 switch (interface_type) {
710 case PHY_INTERFACE_MODE_MII:
711 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
712 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
713 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
714 break;
715 case PHY_INTERFACE_MODE_GMII:
716 if (eth_clk_sel_reg)
717 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
718 SYSCFG_PMCSETR_ETH_CLK_SEL;
719 else
720 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
721 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
722 break;
723 case PHY_INTERFACE_MODE_RMII:
724 if (eth_ref_clk_sel_reg)
725 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
726 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
727 else
728 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
729 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
730 break;
731 case PHY_INTERFACE_MODE_RGMII:
732 case PHY_INTERFACE_MODE_RGMII_ID:
733 case PHY_INTERFACE_MODE_RGMII_RXID:
734 case PHY_INTERFACE_MODE_RGMII_TXID:
735 if (eth_clk_sel_reg)
736 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
737 SYSCFG_PMCSETR_ETH_CLK_SEL;
738 else
739 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
740 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
741 break;
742 default:
743 debug("%s: Do not manage %d interface\n",
744 __func__, interface_type);
745 /* Do not manage others interfaces */
746 return -EINVAL;
747 }
748
749 /* clear and set ETH configuration bits */
750 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
751 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
752 syscfg + SYSCFG_PMCCLRR);
753 writel(value, syscfg + SYSCFG_PMCSETR);
754
755 return 0;
756}
757
Marek Vasut5ff05292020-01-24 18:39:16 +0100758#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900759int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100760{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200761 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
762 int buck3off, ret, uv;
763
764 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
765 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
766 return 0;
767
768 buck3off = fdt_path_offset(blob, buck3path);
769 if (buck3off < 0) /* No Buck3 regulator found. */
770 return 0;
771
772 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
773 if (ret < 0)
774 return ret;
775
776 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
777 if (ret < 0)
778 return ret;
779
Marek Vasut5ff05292020-01-24 18:39:16 +0100780 return 0;
781}
782#endif
783
Marek Vasut5ff05292020-01-24 18:39:16 +0100784static void board_copro_image_process(ulong fw_image, size_t fw_size)
785{
786 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
787
788 if (!rproc_is_initialized())
789 if (rproc_init()) {
790 printf("Remote Processor %d initialization failed\n",
791 id);
792 return;
793 }
794
795 ret = rproc_load(id, fw_image, fw_size);
796 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
797 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
798
799 if (!ret) {
800 rproc_start(id);
801 env_set("copro_state", "booted");
802 }
803}
804
805U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);