blob: d30171f1fbe5712edbcfa440008bd759a51b3d88 [file] [log] [blame]
Marek Vasut5ff05292020-01-24 18:39:16 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
Marek Vasut5ff05292020-01-24 18:39:16 +01006#include <adc.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -06008#include <net.h>
Marek Vasut5ff05292020-01-24 18:39:16 +01009#include <asm/arch/stm32.h>
10#include <asm/arch/sys_proto.h>
11#include <asm/gpio.h>
12#include <asm/io.h>
13#include <bootm.h>
14#include <clk.h>
15#include <config.h>
16#include <dm.h>
17#include <dm/device.h>
18#include <dm/uclass.h>
19#include <env.h>
20#include <env_internal.h>
21#include <g_dnl.h>
22#include <generic-phy.h>
23#include <hang.h>
24#include <i2c.h>
25#include <i2c_eeprom.h>
26#include <init.h>
27#include <led.h>
28#include <memalign.h>
29#include <misc.h>
30#include <mtd.h>
31#include <mtd_node.h>
32#include <netdev.h>
33#include <phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060034#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060036#include <linux/printk.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010037#include <power/regulator.h>
38#include <remoteproc.h>
39#include <reset.h>
Marek Vasut35516542024-06-06 15:01:48 +020040#include <spl.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010041#include <syscon.h>
42#include <usb.h>
43#include <usb/dwc2_udc.h>
44#include <watchdog.h>
Simon Glass0034d962021-08-07 07:24:01 -060045#include <dm/ofnode.h>
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020046#include "../common/dh_common.h"
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020047#include "../../st/common/stpmic1.h"
Marek Vasut5ff05292020-01-24 18:39:16 +010048
49/* SYSCFG registers */
50#define SYSCFG_BOOTR 0x00
Marek Vasut5ff05292020-01-24 18:39:16 +010051#define SYSCFG_IOCTRLSETR 0x18
52#define SYSCFG_ICNR 0x1C
53#define SYSCFG_CMPCR 0x20
54#define SYSCFG_CMPENSETR 0x24
Marek Vasut5ff05292020-01-24 18:39:16 +010055
56#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
57#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
58
59#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
60#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
61#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
62#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
63#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
64
65#define SYSCFG_CMPCR_SW_CTRL BIT(1)
66#define SYSCFG_CMPCR_READY BIT(8)
67
68#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
69
Marek Vasut145a8762020-10-08 15:14:58 +020070#define KS_CCR 0x08
71#define KS_CCR_EEPROM BIT(9)
72#define KS_BE0 BIT(12)
73#define KS_BE1 BIT(13)
Marek Vasutb2b31c12021-05-03 13:31:39 +020074#define KS_CIDER 0xC0
75#define CIDER_ID 0x8870
Marek Vasut145a8762020-10-08 15:14:58 +020076
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020077static bool dh_stm32_mac_is_in_ks8851(void)
Marek Vasut5ff05292020-01-24 18:39:16 +010078{
Marek Vasutf4411842024-06-06 15:02:46 +020079 struct udevice *udev;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020080 u32 reg, cider, ccr;
Marek Vasutf4411842024-06-06 15:02:46 +020081 char path[256];
82 ofnode node;
83 int ret;
Marek Vasutb0a2a492020-07-31 01:34:50 +020084
Patrick Delaunay280949c2022-06-06 16:04:15 +020085 node = ofnode_path("ethernet1");
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020086 if (!ofnode_valid(node))
87 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020088
Marek Vasutf4411842024-06-06 15:02:46 +020089 ret = ofnode_get_path(node, path, sizeof(path));
90 if (ret)
91 return false;
92
93 ret = uclass_get_device_by_of_path(UCLASS_ETH, path, &udev);
94 if (ret)
95 return false;
96
97 if (!ofnode_device_is_compatible(node, "micrel,ks8851-mll"))
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +020098 return false;
Marek Vasut145a8762020-10-08 15:14:58 +020099
100 /*
101 * KS8851 with EEPROM may use custom MAC from EEPROM, read
102 * out the KS8851 CCR register to determine whether EEPROM
103 * is present. If EEPROM is present, it must contain valid
104 * MAC address.
105 */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200106 reg = ofnode_get_addr(node);
Marek Vasut145a8762020-10-08 15:14:58 +0200107 if (!reg)
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200108 return false;
Marek Vasut145a8762020-10-08 15:14:58 +0200109
Marek Vasutb2b31c12021-05-03 13:31:39 +0200110 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
111 cider = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200112 if ((cider & 0xfff0) != CIDER_ID)
113 return true;
Marek Vasutb2b31c12021-05-03 13:31:39 +0200114
Marek Vasut145a8762020-10-08 15:14:58 +0200115 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
116 ccr = readw(reg);
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200117 if (ccr & KS_CCR_EEPROM)
118 return true;
119
120 return false;
121}
Marek Vasutb0a2a492020-07-31 01:34:50 +0200122
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200123static int dh_stm32_setup_ethaddr(void)
124{
125 unsigned char enetaddr[6];
126
127 if (dh_mac_is_in_env("ethaddr"))
Marek Vasut5ff05292020-01-24 18:39:16 +0100128 return 0;
129
Marek Vasut29ab1a92024-03-12 22:15:58 +0100130 if (dh_get_mac_is_enabled("ethernet0"))
131 return 0;
132
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200133 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
134 return eth_env_set_enetaddr("ethaddr", enetaddr);
Marek Vasut5ff05292020-01-24 18:39:16 +0100135
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200136 return -ENXIO;
137}
Marek Vasut5ff05292020-01-24 18:39:16 +0100138
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200139static int dh_stm32_setup_eth1addr(void)
140{
141 unsigned char enetaddr[6];
Marek Vasut5ff05292020-01-24 18:39:16 +0100142
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200143 if (dh_mac_is_in_env("eth1addr"))
144 return 0;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200145
Marek Vasut29ab1a92024-03-12 22:15:58 +0100146 if (dh_get_mac_is_enabled("ethernet1"))
147 return 0;
148
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200149 if (dh_stm32_mac_is_in_ks8851())
150 return 0;
151
152 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) {
Marek Vasutb0a2a492020-07-31 01:34:50 +0200153 enetaddr[5]++;
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200154 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutb0a2a492020-07-31 01:34:50 +0200155 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100156
Philip Oberfichtner78fa6b92022-07-26 15:04:53 +0200157 return -ENXIO;
158}
159
160int setup_mac_address(void)
161{
162 if (dh_stm32_setup_ethaddr())
163 log_err("%s: Unable to setup ethaddr!\n", __func__);
164
165 if (dh_stm32_setup_eth1addr())
166 log_err("%s: Unable to setup eth1addr!\n", __func__);
167
Marek Vasut5ff05292020-01-24 18:39:16 +0100168 return 0;
169}
170
171int checkboard(void)
172{
173 char *mode;
174 const char *fdt_compat;
175 int fdt_compat_len;
176
Patrick Delaunay472407a2020-03-18 09:22:49 +0100177 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100178 mode = "trusted";
179 else
180 mode = "basic";
181
182 printf("Board: stm32mp1 in %s mode", mode);
Patrick Delaunay280949c2022-06-06 16:04:15 +0200183 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
184 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100185 if (fdt_compat && fdt_compat_len)
186 printf(" (%s)", fdt_compat);
187 puts("\n");
188
189 return 0;
190}
191
Marek Vasut47b98ba2020-04-22 13:18:11 +0200192#ifdef CONFIG_BOARD_EARLY_INIT_F
Marek Vasute5905ee2023-05-04 21:52:08 +0200193static u8 brdcode __section(".data");
194static u8 ddr3code __section(".data");
195static u8 somcode __section(".data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200196static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200197
198static void board_get_coding_straps(void)
199{
200 struct gpio_desc gpio[4];
201 ofnode node;
202 int i, ret;
203
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100204 brdcode = 0;
205 ddr3code = 0;
206 somcode = 0;
207
Marek Vasut47b98ba2020-04-22 13:18:11 +0200208 node = ofnode_path("/config");
209 if (!ofnode_valid(node)) {
210 printf("%s: no /config node?\n", __func__);
211 return;
212 }
213
Marek Vasut47b98ba2020-04-22 13:18:11 +0200214 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
215 gpio, ARRAY_SIZE(gpio),
216 GPIOD_IS_IN);
217 for (i = 0; i < ret; i++)
218 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
219
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100220 gpio_free_list_nodev(gpio, ret);
221
Marek Vasut39221b52020-04-22 13:18:14 +0200222 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
223 gpio, ARRAY_SIZE(gpio),
224 GPIOD_IS_IN);
225 for (i = 0; i < ret; i++)
226 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
227
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100228 gpio_free_list_nodev(gpio, ret);
229
Marek Vasut47b98ba2020-04-22 13:18:11 +0200230 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
231 gpio, ARRAY_SIZE(gpio),
232 GPIOD_IS_IN);
233 for (i = 0; i < ret; i++)
234 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
235
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100236 gpio_free_list_nodev(gpio, ret);
237
Harald Seiler1768f5d2023-09-27 14:46:25 +0200238 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
239 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
240 somcode, ddr3code, brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200241}
242
243int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
244 const char *name)
245{
Marek Vasut272198e2020-04-29 15:08:38 +0200246 if (ddr3code == 1 &&
247 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
248 return 0;
249
Marek Vasut39221b52020-04-22 13:18:14 +0200250 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200251 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200252 return 0;
253
254 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200255 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200256 return 0;
257
258 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200259}
260
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200261void board_vddcore_init(u32 voltage_mv)
262{
Simon Glass49c24a82024-09-29 19:49:47 -0600263 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200264 opp_voltage_mv = voltage_mv;
265}
266
Marek Vasut47b98ba2020-04-22 13:18:11 +0200267int board_early_init_f(void)
268{
Simon Glass49c24a82024-09-29 19:49:47 -0600269 if (IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200270 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200271 board_get_coding_straps();
272
273 return 0;
274}
275
276#ifdef CONFIG_SPL_LOAD_FIT
277int board_fit_config_name_match(const char *name)
278{
Marek Vasut01711442024-10-05 03:15:50 +0200279 char *cdevice, *ndevice;
Marek Vasut060cb122020-07-31 01:35:33 +0200280 const char *compat;
Marek Vasut060cb122020-07-31 01:35:33 +0200281
Patrick Delaunay280949c2022-06-06 16:04:15 +0200282 compat = ofnode_get_property(ofnode_root(), "compatible", NULL);
Marek Vasut01711442024-10-05 03:15:50 +0200283 if (!compat)
284 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200285
Marek Vasut01711442024-10-05 03:15:50 +0200286 cdevice = strchr(compat, ',');
287 if (!cdevice)
288 return -ENODEV;
289
290 cdevice++; /* Move past the comma right after vendor prefix. */
291
292 ndevice = strchr(name, '/');
293 if (!ndevice)
294 return -ENODEV;
295
296 ndevice++; /* Move past the last slash in DT path */
Marek Vasut47b98ba2020-04-22 13:18:11 +0200297
Marek Vasut01711442024-10-05 03:15:50 +0200298 if (!strcmp(cdevice, ndevice))
Marek Vasut47b98ba2020-04-22 13:18:11 +0200299 return 0;
300
301 return -EINVAL;
302}
303#endif
304#endif
305
Marek Vasut5ff05292020-01-24 18:39:16 +0100306static void board_key_check(void)
307{
308#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
309 ofnode node;
310 struct gpio_desc gpio;
311 enum forced_boot_mode boot_mode = BOOT_NORMAL;
312
313 node = ofnode_path("/config");
314 if (!ofnode_valid(node)) {
315 debug("%s: no /config node?\n", __func__);
316 return;
317 }
318#ifdef CONFIG_FASTBOOT
319 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
320 &gpio, GPIOD_IS_IN)) {
321 debug("%s: could not find a /config/st,fastboot-gpios\n",
322 __func__);
323 } else {
324 if (dm_gpio_get_value(&gpio)) {
325 puts("Fastboot key pressed, ");
326 boot_mode = BOOT_FASTBOOT;
327 }
328
329 dm_gpio_free(NULL, &gpio);
330 }
331#endif
332#ifdef CONFIG_CMD_STM32PROG
333 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
334 &gpio, GPIOD_IS_IN)) {
335 debug("%s: could not find a /config/st,stm32prog-gpios\n",
336 __func__);
337 } else {
338 if (dm_gpio_get_value(&gpio)) {
339 puts("STM32Programmer key pressed, ");
340 boot_mode = BOOT_STM32PROG;
341 }
342 dm_gpio_free(NULL, &gpio);
343 }
344#endif
345
346 if (boot_mode != BOOT_NORMAL) {
347 puts("entering download mode...\n");
348 clrsetbits_le32(TAMP_BOOT_CONTEXT,
349 TAMP_BOOT_FORCED_MASK,
350 boot_mode);
351 }
352#endif
353}
354
355#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
356
357#include <usb/dwc2_udc.h>
358int g_dnl_board_usb_cable_connected(void)
359{
360 struct udevice *dwc2_udc_otg;
361 int ret;
362
363 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700364 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100365 &dwc2_udc_otg);
366 if (!ret)
367 debug("dwc2_udc_otg init failed\n");
368
369 return dwc2_udc_B_session_valid(dwc2_udc_otg);
370}
371
372#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
373#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
374
375int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
376{
377 if (!strcmp(name, "usb_dnl_dfu"))
378 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
379 else if (!strcmp(name, "usb_dnl_fastboot"))
380 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
381 &dev->idProduct);
382 else
383 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
384
385 return 0;
386}
387
388#endif /* CONFIG_USB_GADGET */
389
390#ifdef CONFIG_LED
391static int get_led(struct udevice **dev, char *led_string)
392{
Simon Glass0034d962021-08-07 07:24:01 -0600393 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100394 int ret;
395
Simon Glass0034d962021-08-07 07:24:01 -0600396 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100397 if (!led_name) {
398 pr_debug("%s: could not find %s config string\n",
399 __func__, led_string);
400 return -ENOENT;
401 }
402 ret = led_get_by_label(led_name, dev);
403 if (ret) {
404 debug("%s: get=%d\n", __func__, ret);
405 return ret;
406 }
407
408 return 0;
409}
410
411static int setup_led(enum led_state_t cmd)
412{
413 struct udevice *dev;
414 int ret;
415
416 ret = get_led(&dev, "u-boot,boot-led");
417 if (ret)
418 return ret;
419
420 ret = led_set_state(dev, cmd);
421 return ret;
422}
423#endif
424
425static void __maybe_unused led_error_blink(u32 nb_blink)
426{
427#ifdef CONFIG_LED
428 int ret;
429 struct udevice *led;
430 u32 i;
431#endif
432
433 if (!nb_blink)
434 return;
435
436#ifdef CONFIG_LED
437 ret = get_led(&led, "u-boot,error-led");
438 if (!ret) {
439 /* make u-boot,error-led blinking */
440 /* if U32_MAX and 125ms interval, for 17.02 years */
441 for (i = 0; i < 2 * nb_blink; i++) {
442 led_set_state(led, LEDST_TOGGLE);
443 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200444 schedule();
Marek Vasut5ff05292020-01-24 18:39:16 +0100445 }
446 }
447#endif
448
449 /* infinite: the boot process must be stopped */
450 if (nb_blink == U32_MAX)
451 hang();
452}
453
454static void sysconf_init(void)
455{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200456#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100457 u8 *syscfg;
458#ifdef CONFIG_DM_REGULATOR
459 struct udevice *pwr_dev;
460 struct udevice *pwr_reg;
461 struct udevice *dev;
462 int ret;
463 u32 otp = 0;
464#endif
465 u32 bootr;
466
467 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
468
469 /* interconnect update : select master using the port 1 */
470 /* LTDC = AXI_M9 */
471 /* GPU = AXI_M8 */
472 /* today information is hardcoded in U-Boot */
473 writel(BIT(9), syscfg + SYSCFG_ICNR);
474
475 /* disable Pull-Down for boot pin connected to VDD */
476 bootr = readl(syscfg + SYSCFG_BOOTR);
477 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
478 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
479 writel(bootr, syscfg + SYSCFG_BOOTR);
480
481#ifdef CONFIG_DM_REGULATOR
482 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
483 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
484 * The customer will have to disable this for low frequencies
485 * or if AFMUX is selected but the function not used, typically for
486 * TRACE. Otherwise, impact on power consumption.
487 *
488 * WARNING:
489 * enabling High Speed mode while VDD>2.7V
490 * with the OTP product_below_2v5 (OTP 18, BIT 13)
491 * erroneously set to 1 can damage the IC!
492 * => U-Boot set the register only if VDD < 2.7V (in DT)
493 * but this value need to be consistent with board design
494 */
495 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700496 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100497 &pwr_dev);
498 if (!ret) {
499 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700500 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100501 &dev);
502 if (ret) {
503 pr_err("Can't find stm32mp_bsec driver\n");
504 return;
505 }
506
507 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
508 if (ret > 0)
509 otp = otp & BIT(13);
510
511 /* get VDD = vdd-supply */
512 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
513 &pwr_reg);
514
515 /* check if VDD is Low Voltage */
516 if (!ret) {
517 if (regulator_get_value(pwr_reg) < 2700000) {
518 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
519 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
520 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
521 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
522 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
523 syscfg + SYSCFG_IOCTRLSETR);
524
525 if (!otp)
526 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
527 } else {
528 if (otp)
529 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
530 }
531 } else {
532 debug("VDD unknown");
533 }
534 }
535#endif
536
537 /* activate automatic I/O compensation
538 * warning: need to ensure CSI enabled and ready in clock driver
539 */
540 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
541
542 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
543 ;
544 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
545#endif
546}
547
Marek Vasut7f809fe2022-05-11 23:09:33 +0200548#ifdef CONFIG_DM_REGULATOR
549#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
550#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
551#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
552#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
553#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
554#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
555#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
556static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
557{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200558 struct udevice *dev;
559 u8 bucks_vout = 0;
560 const char *prop;
561 int len, ret;
562
563 /* Check whether this is Avenger96 board. */
Patrick Delaunay280949c2022-06-06 16:04:15 +0200564 prop = ofnode_get_property(ofnode_root(), "compatible", &len);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200565 if (!prop || !len)
566 return -ENODEV;
567
Marek Vasut52784942022-09-26 18:50:00 +0200568 if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench"))
Marek Vasut7f809fe2022-05-11 23:09:33 +0200569 return -EINVAL;
570
571 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
572 ret = uclass_get_device_by_driver(UCLASS_MISC,
573 DM_DRIVER_GET(stpmic1_nvm),
574 &dev);
575 if (ret)
576 return ret;
577
578 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
579 if (ret != 1)
580 return -EINVAL;
581
582 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
583 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
584
Marek Vasut52784942022-09-26 18:50:00 +0200585 if (strstr(prop, "avenger96")) {
586 /*
587 * Avenger96 board comes in multiple regulator configurations:
588 * - rev.100 or rev.200 have Buck3 preconfigured to
589 * 3V3 operation on boot and contains extra Enpirion
590 * EP53A8LQI DCDC converter which supplies the IO.
591 * Reduce Buck3 voltage to 2V9 to not waste power.
592 * - rev.200L have Buck3 preconfigured to 1V8 operation
593 * and have no Enpirion EP53A8LQI DCDC anymore, the
594 * IO is supplied from Buck3.
595 */
596 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
597 *uv = 2900000;
598 else
599 *uv = 1800000;
600 } else {
601 /* Testbench always respects Buck3 NVM settings */
602 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
603 *uv = 3300000;
604 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0)
605 *uv = 3000000;
606 else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8)
607 *uv = 1800000;
608 else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */
609 *uv = 1200000;
610 }
Marek Vasut7f809fe2022-05-11 23:09:33 +0200611
612 return 0;
613}
614
615static void board_init_regulator_av96(void)
616{
617 struct udevice *rdev;
618 int ret, uv;
619
620 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
621 if (ret) /* Not Avenger96 board. */
622 return;
623
624 ret = regulator_get_by_devname("buck3", &rdev);
625 if (ret)
626 return;
627
628 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
629 regulator_set_value(rdev, uv);
Marek Vasut69e89952022-09-23 03:31:22 +0200630 regulator_set_enable(rdev, true);
Marek Vasut7f809fe2022-05-11 23:09:33 +0200631}
632
633static void board_init_regulator(void)
634{
635 board_init_regulator_av96();
Marek Vasut7f809fe2022-05-11 23:09:33 +0200636}
637#else
638static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
639{
640 return -EINVAL;
641}
642
643static inline void board_init_regulator(void) {}
644#endif
645
Marek Vasut5ff05292020-01-24 18:39:16 +0100646/* board dependent setup after realloc */
647int board_init(void)
648{
Marek Vasut5ff05292020-01-24 18:39:16 +0100649 board_key_check();
650
Marek Vasut7f809fe2022-05-11 23:09:33 +0200651 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100652
653 sysconf_init();
654
Marek Vasut5ff05292020-01-24 18:39:16 +0100655 return 0;
656}
657
658int board_late_init(void)
659{
660 char *boot_device;
661#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
662 const void *fdt_compat;
663 int fdt_compat_len;
664
Patrick Delaunay280949c2022-06-06 16:04:15 +0200665 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
666 &fdt_compat_len);
Marek Vasut5ff05292020-01-24 18:39:16 +0100667 if (fdt_compat && fdt_compat_len) {
668 if (strncmp(fdt_compat, "st,", 3) != 0)
669 env_set("board_name", fdt_compat);
670 else
671 env_set("board_name", fdt_compat + 3);
672 }
673#endif
674
675 /* Check the boot-source to disable bootdelay */
676 boot_device = env_get("boot_device");
677 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
678 env_set("bootdelay", "0");
679
Marek Vasut47b98ba2020-04-22 13:18:11 +0200680#ifdef CONFIG_BOARD_EARLY_INIT_F
681 env_set_ulong("dh_som_rev", somcode);
682 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200683 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200684#endif
685
Marek Vasut5ff05292020-01-24 18:39:16 +0100686 return 0;
687}
688
689void board_quiesce_devices(void)
690{
691#ifdef CONFIG_LED
692 setup_led(LEDST_OFF);
693#endif
694}
695
Marek Vasut35516542024-06-06 15:01:48 +0200696static void dh_stm32_ks8851_fixup(void *blob)
697{
698 struct gpio_desc ks8851intrn;
699 bool compatible = false;
700 int ks8851intrn_value;
701 const char *prop;
702 ofnode node;
703 int idx = 0;
704 int offset;
705 int ret;
706
707 /* Do nothing if not STM32MP15xx DHCOM SoM */
708 while ((prop = fdt_stringlist_get(blob, 0, "compatible", idx++, NULL))) {
709 if (!strstr(prop, "dhcom-som"))
710 continue;
711 compatible = true;
712 break;
713 }
714
715 if (!compatible)
716 return;
717
718 /*
719 * Read state of INTRN pull up resistor, if this pull up is populated,
720 * KS8851-16MLL is populated as well and should be enabled, otherwise
721 * it should be disabled.
722 */
723 node = ofnode_path("/config");
724 if (!ofnode_valid(node))
725 return;
726
727 ret = gpio_request_by_name_nodev(node, "dh,mac-coding-gpios", 0,
728 &ks8851intrn, GPIOD_IS_IN);
729 if (ret)
730 return;
731
732 ks8851intrn_value = dm_gpio_get_value(&ks8851intrn);
733
734 dm_gpio_free(NULL, &ks8851intrn);
735
736 /* Set the 'status' property into KS8851-16MLL DT node. */
737 offset = fdt_path_offset(blob, "ethernet1");
738 ret = fdt_node_check_compatible(blob, offset, "micrel,ks8851-mll");
739 if (ret) /* Not compatible */
740 return;
741
742 /* Add a bit of extra space for new 'status' property */
743 ret = fdt_shrink_to_minimum(blob, 4096);
744 if (!ret)
745 return;
746
747 fdt_setprop_string(blob, offset, "status",
748 ks8851intrn_value ? "okay" : "disabled");
749}
750
Marek Vasut5ff05292020-01-24 18:39:16 +0100751#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900752int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100753{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200754 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
755 int buck3off, ret, uv;
756
Marek Vasut35516542024-06-06 15:01:48 +0200757 dh_stm32_ks8851_fixup(blob);
758
Marek Vasut7f809fe2022-05-11 23:09:33 +0200759 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
760 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
761 return 0;
762
763 buck3off = fdt_path_offset(blob, buck3path);
764 if (buck3off < 0) /* No Buck3 regulator found. */
765 return 0;
766
767 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
768 if (ret < 0)
769 return ret;
770
771 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
772 if (ret < 0)
773 return ret;
774
Marek Vasut5ff05292020-01-24 18:39:16 +0100775 return 0;
776}
777#endif
778
Simon Glass49c24a82024-09-29 19:49:47 -0600779#if defined(CONFIG_XPL_BUILD)
Marek Vasut35516542024-06-06 15:01:48 +0200780void spl_perform_fixups(struct spl_image_info *spl_image)
781{
782 dh_stm32_ks8851_fixup(spl_image_fdt_addr(spl_image));
783}
784#endif
785
Marek Vasut5ff05292020-01-24 18:39:16 +0100786static void board_copro_image_process(ulong fw_image, size_t fw_size)
787{
788 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
789
790 if (!rproc_is_initialized())
791 if (rproc_init()) {
792 printf("Remote Processor %d initialization failed\n",
793 id);
794 return;
795 }
796
797 ret = rproc_load(id, fw_image, fw_size);
798 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
799 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
800
801 if (!ret) {
802 rproc_start(id);
803 env_set("copro_state", "booted");
804 }
805}
806
807U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);