blob: d407f0bf5921e28fc1ed7114aa51536720f494ed [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Marek Vasut5ff05292020-01-24 18:39:16 +010013#include <asm/gpio.h>
14#include <asm/io.h>
15#include <bootm.h>
16#include <clk.h>
17#include <config.h>
18#include <dm.h>
19#include <dm/device.h>
20#include <dm/uclass.h>
21#include <env.h>
22#include <env_internal.h>
23#include <g_dnl.h>
24#include <generic-phy.h>
25#include <hang.h>
26#include <i2c.h>
27#include <i2c_eeprom.h>
28#include <init.h>
29#include <led.h>
30#include <memalign.h>
31#include <misc.h>
32#include <mtd.h>
33#include <mtd_node.h>
34#include <netdev.h>
35#include <phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060037#include <linux/delay.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>
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020046#include "../../st/common/stpmic1.h"
Marek Vasut5ff05292020-01-24 18:39:16 +010047
48/* SYSCFG registers */
49#define SYSCFG_BOOTR 0x00
50#define SYSCFG_PMCSETR 0x04
51#define SYSCFG_IOCTRLSETR 0x18
52#define SYSCFG_ICNR 0x1C
53#define SYSCFG_CMPCR 0x20
54#define SYSCFG_CMPENSETR 0x24
55#define SYSCFG_PMCCLRR 0x44
56
57#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
58#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
59
60#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
61#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
62#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
63#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
64#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
65
66#define SYSCFG_CMPCR_SW_CTRL BIT(1)
67#define SYSCFG_CMPCR_READY BIT(8)
68
69#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
70
71#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
72#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
73
74#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
75
76#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
77#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
78#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
79#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
80
81/*
82 * Get a global data pointer
83 */
84DECLARE_GLOBAL_DATA_PTR;
85
Marek Vasut145a8762020-10-08 15:14:58 +020086#define KS_CCR 0x08
87#define KS_CCR_EEPROM BIT(9)
88#define KS_BE0 BIT(12)
89#define KS_BE1 BIT(13)
Marek Vasutb2b31c12021-05-03 13:31:39 +020090#define KS_CIDER 0xC0
91#define CIDER_ID 0x8870
Marek Vasut145a8762020-10-08 15:14:58 +020092
Marek Vasut5ff05292020-01-24 18:39:16 +010093int setup_mac_address(void)
94{
Marek Vasut5ff05292020-01-24 18:39:16 +010095 unsigned char enetaddr[6];
Marek Vasutb0a2a492020-07-31 01:34:50 +020096 bool skip_eth0 = false;
97 bool skip_eth1 = false;
Marek Vasut2ab7a2a2020-03-31 19:51:29 +020098 struct udevice *dev;
99 int off, ret;
Marek Vasut5ff05292020-01-24 18:39:16 +0100100
101 ret = eth_env_get_enetaddr("ethaddr", enetaddr);
102 if (ret) /* ethaddr is already set */
Marek Vasutb0a2a492020-07-31 01:34:50 +0200103 skip_eth0 = true;
104
105 off = fdt_path_offset(gd->fdt_blob, "ethernet1");
106 if (off < 0) {
107 /* ethernet1 is not present in the system */
108 skip_eth1 = true;
Marek Vasut145a8762020-10-08 15:14:58 +0200109 goto out_set_ethaddr;
110 }
111
112 ret = eth_env_get_enetaddr("eth1addr", enetaddr);
113 if (ret) {
114 /* eth1addr is already set */
115 skip_eth1 = true;
116 goto out_set_ethaddr;
117 }
118
119 ret = fdt_node_check_compatible(gd->fdt_blob, off, "micrel,ks8851-mll");
120 if (ret)
121 goto out_set_ethaddr;
122
123 /*
124 * KS8851 with EEPROM may use custom MAC from EEPROM, read
125 * out the KS8851 CCR register to determine whether EEPROM
126 * is present. If EEPROM is present, it must contain valid
127 * MAC address.
128 */
Marek Vasutb2b31c12021-05-03 13:31:39 +0200129 u32 reg, cider, ccr;
Marek Vasut145a8762020-10-08 15:14:58 +0200130 reg = fdt_get_base_address(gd->fdt_blob, off);
131 if (!reg)
132 goto out_set_ethaddr;
133
Marek Vasutb2b31c12021-05-03 13:31:39 +0200134 writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2);
135 cider = readw(reg);
136 if ((cider & 0xfff0) != CIDER_ID) {
137 skip_eth1 = true;
138 goto out_set_ethaddr;
139 }
140
Marek Vasut145a8762020-10-08 15:14:58 +0200141 writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2);
142 ccr = readw(reg);
143 if (ccr & KS_CCR_EEPROM) {
144 skip_eth1 = true;
145 goto out_set_ethaddr;
Marek Vasutb0a2a492020-07-31 01:34:50 +0200146 }
147
Marek Vasut145a8762020-10-08 15:14:58 +0200148out_set_ethaddr:
Marek Vasutb0a2a492020-07-31 01:34:50 +0200149 if (skip_eth0 && skip_eth1)
Marek Vasut5ff05292020-01-24 18:39:16 +0100150 return 0;
151
Marek Vasut2ab7a2a2020-03-31 19:51:29 +0200152 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
153 if (off < 0) {
154 printf("%s: No eeprom0 path offset\n", __func__);
155 return off;
Marek Vasut5ff05292020-01-24 18:39:16 +0100156 }
157
Marek Vasut2ab7a2a2020-03-31 19:51:29 +0200158 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
Marek Vasut5ff05292020-01-24 18:39:16 +0100159 if (ret) {
160 printf("Cannot find EEPROM!\n");
161 return ret;
162 }
163
164 ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6);
165 if (ret) {
166 printf("Error reading configuration EEPROM!\n");
167 return ret;
168 }
169
Marek Vasutb0a2a492020-07-31 01:34:50 +0200170 if (is_valid_ethaddr(enetaddr)) {
171 if (!skip_eth0)
172 eth_env_set_enetaddr("ethaddr", enetaddr);
173
174 enetaddr[5]++;
175 if (!skip_eth1)
176 eth_env_set_enetaddr("eth1addr", enetaddr);
177 }
Marek Vasut5ff05292020-01-24 18:39:16 +0100178
179 return 0;
180}
181
182int checkboard(void)
183{
184 char *mode;
185 const char *fdt_compat;
186 int fdt_compat_len;
187
Patrick Delaunay472407a2020-03-18 09:22:49 +0100188 if (IS_ENABLED(CONFIG_TFABOOT))
Marek Vasut5ff05292020-01-24 18:39:16 +0100189 mode = "trusted";
190 else
191 mode = "basic";
192
193 printf("Board: stm32mp1 in %s mode", mode);
194 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
195 &fdt_compat_len);
196 if (fdt_compat && fdt_compat_len)
197 printf(" (%s)", fdt_compat);
198 puts("\n");
199
200 return 0;
201}
202
Marek Vasut47b98ba2020-04-22 13:18:11 +0200203#ifdef CONFIG_BOARD_EARLY_INIT_F
204static u8 brdcode __section("data");
Marek Vasut39221b52020-04-22 13:18:14 +0200205static u8 ddr3code __section("data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200206static u8 somcode __section("data");
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200207static u32 opp_voltage_mv __section(".data");
Marek Vasut47b98ba2020-04-22 13:18:11 +0200208
209static void board_get_coding_straps(void)
210{
211 struct gpio_desc gpio[4];
212 ofnode node;
213 int i, ret;
214
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100215 brdcode = 0;
216 ddr3code = 0;
217 somcode = 0;
218
Marek Vasut47b98ba2020-04-22 13:18:11 +0200219 node = ofnode_path("/config");
220 if (!ofnode_valid(node)) {
221 printf("%s: no /config node?\n", __func__);
222 return;
223 }
224
Marek Vasut47b98ba2020-04-22 13:18:11 +0200225 ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios",
226 gpio, ARRAY_SIZE(gpio),
227 GPIOD_IS_IN);
228 for (i = 0; i < ret; i++)
229 somcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
230
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100231 gpio_free_list_nodev(gpio, ret);
232
Marek Vasut39221b52020-04-22 13:18:14 +0200233 ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios",
234 gpio, ARRAY_SIZE(gpio),
235 GPIOD_IS_IN);
236 for (i = 0; i < ret; i++)
237 ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i;
238
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100239 gpio_free_list_nodev(gpio, ret);
240
Marek Vasut47b98ba2020-04-22 13:18:11 +0200241 ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios",
242 gpio, ARRAY_SIZE(gpio),
243 GPIOD_IS_IN);
244 for (i = 0; i < ret; i++)
245 brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i;
246
Marek Vasut4bd7a5a2021-11-13 03:26:39 +0100247 gpio_free_list_nodev(gpio, ret);
248
Marek Vasut39221b52020-04-22 13:18:14 +0200249 printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n",
250 somcode, ddr3code, brdcode);
251}
252
253int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
254 const char *name)
255{
Marek Vasut272198e2020-04-29 15:08:38 +0200256 if (ddr3code == 1 &&
257 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz"))
258 return 0;
259
Marek Vasut39221b52020-04-22 13:18:14 +0200260 if (ddr3code == 2 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200261 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200262 return 0;
263
264 if (ddr3code == 3 &&
Marek Vasut272198e2020-04-29 15:08:38 +0200265 !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz"))
Marek Vasut39221b52020-04-22 13:18:14 +0200266 return 0;
267
268 return -EINVAL;
Marek Vasut47b98ba2020-04-22 13:18:11 +0200269}
270
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200271void board_vddcore_init(u32 voltage_mv)
272{
273 if (IS_ENABLED(CONFIG_SPL_BUILD))
274 opp_voltage_mv = voltage_mv;
275}
276
Marek Vasut47b98ba2020-04-22 13:18:11 +0200277int board_early_init_f(void)
278{
Patrick Delaunayf2f25c32020-05-25 12:19:46 +0200279 if (IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay08c891a2020-05-25 12:19:47 +0200280 stpmic1_init(opp_voltage_mv);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200281 board_get_coding_straps();
282
283 return 0;
284}
285
286#ifdef CONFIG_SPL_LOAD_FIT
287int board_fit_config_name_match(const char *name)
288{
Marek Vasut060cb122020-07-31 01:35:33 +0200289 const char *compat;
290 char test[128];
291
292 compat = fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200293
Marek Vasut060cb122020-07-31 01:35:33 +0200294 snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d",
295 compat, somcode, brdcode);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200296
297 if (!strcmp(name, test))
298 return 0;
299
300 return -EINVAL;
301}
302#endif
303#endif
304
Marek Vasut5ff05292020-01-24 18:39:16 +0100305static void board_key_check(void)
306{
307#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
308 ofnode node;
309 struct gpio_desc gpio;
310 enum forced_boot_mode boot_mode = BOOT_NORMAL;
311
312 node = ofnode_path("/config");
313 if (!ofnode_valid(node)) {
314 debug("%s: no /config node?\n", __func__);
315 return;
316 }
317#ifdef CONFIG_FASTBOOT
318 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
319 &gpio, GPIOD_IS_IN)) {
320 debug("%s: could not find a /config/st,fastboot-gpios\n",
321 __func__);
322 } else {
323 if (dm_gpio_get_value(&gpio)) {
324 puts("Fastboot key pressed, ");
325 boot_mode = BOOT_FASTBOOT;
326 }
327
328 dm_gpio_free(NULL, &gpio);
329 }
330#endif
331#ifdef CONFIG_CMD_STM32PROG
332 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
333 &gpio, GPIOD_IS_IN)) {
334 debug("%s: could not find a /config/st,stm32prog-gpios\n",
335 __func__);
336 } else {
337 if (dm_gpio_get_value(&gpio)) {
338 puts("STM32Programmer key pressed, ");
339 boot_mode = BOOT_STM32PROG;
340 }
341 dm_gpio_free(NULL, &gpio);
342 }
343#endif
344
345 if (boot_mode != BOOT_NORMAL) {
346 puts("entering download mode...\n");
347 clrsetbits_le32(TAMP_BOOT_CONTEXT,
348 TAMP_BOOT_FORCED_MASK,
349 boot_mode);
350 }
351#endif
352}
353
354#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
355
356#include <usb/dwc2_udc.h>
357int g_dnl_board_usb_cable_connected(void)
358{
359 struct udevice *dwc2_udc_otg;
360 int ret;
361
362 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700363 DM_DRIVER_GET(dwc2_udc_otg),
Marek Vasut5ff05292020-01-24 18:39:16 +0100364 &dwc2_udc_otg);
365 if (!ret)
366 debug("dwc2_udc_otg init failed\n");
367
368 return dwc2_udc_B_session_valid(dwc2_udc_otg);
369}
370
371#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
372#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
373
374int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
375{
376 if (!strcmp(name, "usb_dnl_dfu"))
377 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
378 else if (!strcmp(name, "usb_dnl_fastboot"))
379 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
380 &dev->idProduct);
381 else
382 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
383
384 return 0;
385}
386
387#endif /* CONFIG_USB_GADGET */
388
389#ifdef CONFIG_LED
390static int get_led(struct udevice **dev, char *led_string)
391{
Simon Glass0034d962021-08-07 07:24:01 -0600392 const char *led_name;
Marek Vasut5ff05292020-01-24 18:39:16 +0100393 int ret;
394
Simon Glass0034d962021-08-07 07:24:01 -0600395 led_name = ofnode_conf_read_str(led_string);
Marek Vasut5ff05292020-01-24 18:39:16 +0100396 if (!led_name) {
397 pr_debug("%s: could not find %s config string\n",
398 __func__, led_string);
399 return -ENOENT;
400 }
401 ret = led_get_by_label(led_name, dev);
402 if (ret) {
403 debug("%s: get=%d\n", __func__, ret);
404 return ret;
405 }
406
407 return 0;
408}
409
410static int setup_led(enum led_state_t cmd)
411{
412 struct udevice *dev;
413 int ret;
414
415 ret = get_led(&dev, "u-boot,boot-led");
416 if (ret)
417 return ret;
418
419 ret = led_set_state(dev, cmd);
420 return ret;
421}
422#endif
423
424static void __maybe_unused led_error_blink(u32 nb_blink)
425{
426#ifdef CONFIG_LED
427 int ret;
428 struct udevice *led;
429 u32 i;
430#endif
431
432 if (!nb_blink)
433 return;
434
435#ifdef CONFIG_LED
436 ret = get_led(&led, "u-boot,error-led");
437 if (!ret) {
438 /* make u-boot,error-led blinking */
439 /* if U32_MAX and 125ms interval, for 17.02 years */
440 for (i = 0; i < 2 * nb_blink; i++) {
441 led_set_state(led, LEDST_TOGGLE);
442 mdelay(125);
443 WATCHDOG_RESET();
444 }
445 }
446#endif
447
448 /* infinite: the boot process must be stopped */
449 if (nb_blink == U32_MAX)
450 hang();
451}
452
453static void sysconf_init(void)
454{
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200455#ifndef CONFIG_TFABOOT
Marek Vasut5ff05292020-01-24 18:39:16 +0100456 u8 *syscfg;
457#ifdef CONFIG_DM_REGULATOR
458 struct udevice *pwr_dev;
459 struct udevice *pwr_reg;
460 struct udevice *dev;
461 int ret;
462 u32 otp = 0;
463#endif
464 u32 bootr;
465
466 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
467
468 /* interconnect update : select master using the port 1 */
469 /* LTDC = AXI_M9 */
470 /* GPU = AXI_M8 */
471 /* today information is hardcoded in U-Boot */
472 writel(BIT(9), syscfg + SYSCFG_ICNR);
473
474 /* disable Pull-Down for boot pin connected to VDD */
475 bootr = readl(syscfg + SYSCFG_BOOTR);
476 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
477 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
478 writel(bootr, syscfg + SYSCFG_BOOTR);
479
480#ifdef CONFIG_DM_REGULATOR
481 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
482 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
483 * The customer will have to disable this for low frequencies
484 * or if AFMUX is selected but the function not used, typically for
485 * TRACE. Otherwise, impact on power consumption.
486 *
487 * WARNING:
488 * enabling High Speed mode while VDD>2.7V
489 * with the OTP product_below_2v5 (OTP 18, BIT 13)
490 * erroneously set to 1 can damage the IC!
491 * => U-Boot set the register only if VDD < 2.7V (in DT)
492 * but this value need to be consistent with board design
493 */
494 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700495 DM_DRIVER_GET(stm32mp_pwr_pmic),
Marek Vasut5ff05292020-01-24 18:39:16 +0100496 &pwr_dev);
497 if (!ret) {
498 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700499 DM_DRIVER_GET(stm32mp_bsec),
Marek Vasut5ff05292020-01-24 18:39:16 +0100500 &dev);
501 if (ret) {
502 pr_err("Can't find stm32mp_bsec driver\n");
503 return;
504 }
505
506 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
507 if (ret > 0)
508 otp = otp & BIT(13);
509
510 /* get VDD = vdd-supply */
511 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
512 &pwr_reg);
513
514 /* check if VDD is Low Voltage */
515 if (!ret) {
516 if (regulator_get_value(pwr_reg) < 2700000) {
517 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
518 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
519 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
520 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
521 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
522 syscfg + SYSCFG_IOCTRLSETR);
523
524 if (!otp)
525 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
526 } else {
527 if (otp)
528 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
529 }
530 } else {
531 debug("VDD unknown");
532 }
533 }
534#endif
535
536 /* activate automatic I/O compensation
537 * warning: need to ensure CSI enabled and ready in clock driver
538 */
539 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
540
541 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
542 ;
543 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
544#endif
545}
546
Marek Vasut0839ea92020-03-28 02:01:58 +0100547static void board_init_fmc2(void)
548{
549#define STM32_FMC2_BCR1 0x0
550#define STM32_FMC2_BTR1 0x4
551#define STM32_FMC2_BWTR1 0x104
552#define STM32_FMC2_BCR(x) ((x) * 0x8 + STM32_FMC2_BCR1)
553#define STM32_FMC2_BCRx_FMCEN BIT(31)
554#define STM32_FMC2_BCRx_WREN BIT(12)
555#define STM32_FMC2_BCRx_RSVD BIT(7)
556#define STM32_FMC2_BCRx_FACCEN BIT(6)
557#define STM32_FMC2_BCRx_MWID(n) ((n) << 4)
558#define STM32_FMC2_BCRx_MTYP(n) ((n) << 2)
559#define STM32_FMC2_BCRx_MUXEN BIT(1)
560#define STM32_FMC2_BCRx_MBKEN BIT(0)
561#define STM32_FMC2_BTR(x) ((x) * 0x8 + STM32_FMC2_BTR1)
562#define STM32_FMC2_BTRx_DATAHLD(n) ((n) << 30)
563#define STM32_FMC2_BTRx_BUSTURN(n) ((n) << 16)
564#define STM32_FMC2_BTRx_DATAST(n) ((n) << 8)
565#define STM32_FMC2_BTRx_ADDHLD(n) ((n) << 4)
566#define STM32_FMC2_BTRx_ADDSET(n) ((n) << 0)
567
568#define RCC_MP_AHB6RSTCLRR 0x218
569#define RCC_MP_AHB6RSTCLRR_FMCRST BIT(12)
570#define RCC_MP_AHB6ENSETR 0x19c
571#define RCC_MP_AHB6ENSETR_FMCEN BIT(12)
572
573 const u32 bcr = STM32_FMC2_BCRx_WREN |STM32_FMC2_BCRx_RSVD |
574 STM32_FMC2_BCRx_FACCEN | STM32_FMC2_BCRx_MWID(1) |
575 STM32_FMC2_BCRx_MTYP(2) | STM32_FMC2_BCRx_MUXEN |
576 STM32_FMC2_BCRx_MBKEN;
577 const u32 btr = STM32_FMC2_BTRx_DATAHLD(3) |
578 STM32_FMC2_BTRx_BUSTURN(2) |
579 STM32_FMC2_BTRx_DATAST(0x22) |
580 STM32_FMC2_BTRx_ADDHLD(2) |
581 STM32_FMC2_BTRx_ADDSET(2);
582
583 /* Set up FMC2 bus for KS8851-16MLL and X11 SRAM */
584 writel(RCC_MP_AHB6RSTCLRR_FMCRST, STM32_RCC_BASE + RCC_MP_AHB6RSTCLRR);
585 writel(RCC_MP_AHB6ENSETR_FMCEN, STM32_RCC_BASE + RCC_MP_AHB6ENSETR);
586
587 /* KS8851-16MLL -- Muxed mode */
588 writel(bcr, STM32_FMC2_BASE + STM32_FMC2_BCR(1));
589 writel(btr, STM32_FMC2_BASE + STM32_FMC2_BTR(1));
590 /* AS7C34098 SRAM on X11 -- Muxed mode */
591 writel(bcr, STM32_FMC2_BASE + STM32_FMC2_BCR(3));
592 writel(btr, STM32_FMC2_BASE + STM32_FMC2_BTR(3));
593
594 setbits_le32(STM32_FMC2_BASE + STM32_FMC2_BCR1, STM32_FMC2_BCRx_FMCEN);
595}
596
Marek Vasut7f809fe2022-05-11 23:09:33 +0200597#ifdef CONFIG_DM_REGULATOR
598#define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc
599#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0
600#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1
601#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2
602#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3
603#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0)
604#define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2)
605static int board_get_regulator_buck3_nvm_uv_av96(int *uv)
606{
607 const void *fdt = gd->fdt_blob;
608 struct udevice *dev;
609 u8 bucks_vout = 0;
610 const char *prop;
611 int len, ret;
612
613 /* Check whether this is Avenger96 board. */
614 prop = fdt_getprop(fdt, 0, "compatible", &len);
615 if (!prop || !len)
616 return -ENODEV;
617
618 if (!strstr(prop, "avenger96"))
619 return -EINVAL;
620
621 /* Read out STPMIC1 NVM and determine default Buck3 voltage. */
622 ret = uclass_get_device_by_driver(UCLASS_MISC,
623 DM_DRIVER_GET(stpmic1_nvm),
624 &dev);
625 if (ret)
626 return ret;
627
628 ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1);
629 if (ret != 1)
630 return -EINVAL;
631
632 bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3);
633 bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK;
634
635 /*
636 * Avenger96 board comes in multiple regulator configurations:
637 * - rev.100 or rev.200 have Buck3 preconfigured to 3V3 operation on
638 * boot and contains extra Enpirion EP53A8LQI DCDC converter which
639 * supplies the IO. Reduce Buck3 voltage to 2V9 to not waste power.
640 * - rev.200L have Buck3 preconfigured to 1V8 operation and have no
641 * Enpirion EP53A8LQI DCDC anymore, the IO is supplied from Buck3.
642 */
643 if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3)
644 *uv = 2900000;
645 else
646 *uv = 1800000;
647
648 return 0;
649}
650
651static void board_init_regulator_av96(void)
652{
653 struct udevice *rdev;
654 int ret, uv;
655
656 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
657 if (ret) /* Not Avenger96 board. */
658 return;
659
660 ret = regulator_get_by_devname("buck3", &rdev);
661 if (ret)
662 return;
663
664 /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */
665 regulator_set_value(rdev, uv);
666}
667
668static void board_init_regulator(void)
669{
670 board_init_regulator_av96();
671
672 regulators_enable_boot_on(_DEBUG);
673}
674#else
675static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv)
676{
677 return -EINVAL;
678}
679
680static inline void board_init_regulator(void) {}
681#endif
682
Marek Vasut5ff05292020-01-24 18:39:16 +0100683/* board dependent setup after realloc */
684int board_init(void)
685{
Marek Vasut5ff05292020-01-24 18:39:16 +0100686 board_key_check();
687
Marek Vasut7f809fe2022-05-11 23:09:33 +0200688 board_init_regulator();
Marek Vasut5ff05292020-01-24 18:39:16 +0100689
690 sysconf_init();
691
Marek Vasut0839ea92020-03-28 02:01:58 +0100692 board_init_fmc2();
693
Marek Vasut5ff05292020-01-24 18:39:16 +0100694 return 0;
695}
696
697int board_late_init(void)
698{
699 char *boot_device;
700#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
701 const void *fdt_compat;
702 int fdt_compat_len;
703
704 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
705 &fdt_compat_len);
706 if (fdt_compat && fdt_compat_len) {
707 if (strncmp(fdt_compat, "st,", 3) != 0)
708 env_set("board_name", fdt_compat);
709 else
710 env_set("board_name", fdt_compat + 3);
711 }
712#endif
713
714 /* Check the boot-source to disable bootdelay */
715 boot_device = env_get("boot_device");
716 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
717 env_set("bootdelay", "0");
718
Marek Vasut47b98ba2020-04-22 13:18:11 +0200719#ifdef CONFIG_BOARD_EARLY_INIT_F
720 env_set_ulong("dh_som_rev", somcode);
721 env_set_ulong("dh_board_rev", brdcode);
Marek Vasut39221b52020-04-22 13:18:14 +0200722 env_set_ulong("dh_ddr3_code", ddr3code);
Marek Vasut47b98ba2020-04-22 13:18:11 +0200723#endif
724
Marek Vasut5ff05292020-01-24 18:39:16 +0100725 return 0;
726}
727
728void board_quiesce_devices(void)
729{
730#ifdef CONFIG_LED
731 setup_led(LEDST_OFF);
732#endif
733}
734
735/* eth init function : weak called in eqos driver */
736int board_interface_eth_init(struct udevice *dev,
737 phy_interface_t interface_type)
738{
739 u8 *syscfg;
740 u32 value;
741 bool eth_clk_sel_reg = false;
742 bool eth_ref_clk_sel_reg = false;
743
744 /* Gigabit Ethernet 125MHz clock selection. */
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200745 eth_clk_sel_reg = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut5ff05292020-01-24 18:39:16 +0100746
747 /* Ethernet 50Mhz RMII clock selection */
748 eth_ref_clk_sel_reg =
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200749 dev_read_bool(dev, "st,eth-ref-clk-sel");
Marek Vasut5ff05292020-01-24 18:39:16 +0100750
751 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
752
753 if (!syscfg)
754 return -ENODEV;
755
756 switch (interface_type) {
757 case PHY_INTERFACE_MODE_MII:
758 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
759 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
760 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
761 break;
762 case PHY_INTERFACE_MODE_GMII:
763 if (eth_clk_sel_reg)
764 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
765 SYSCFG_PMCSETR_ETH_CLK_SEL;
766 else
767 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
768 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
769 break;
770 case PHY_INTERFACE_MODE_RMII:
771 if (eth_ref_clk_sel_reg)
772 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
773 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
774 else
775 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
776 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
777 break;
778 case PHY_INTERFACE_MODE_RGMII:
779 case PHY_INTERFACE_MODE_RGMII_ID:
780 case PHY_INTERFACE_MODE_RGMII_RXID:
781 case PHY_INTERFACE_MODE_RGMII_TXID:
782 if (eth_clk_sel_reg)
783 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
784 SYSCFG_PMCSETR_ETH_CLK_SEL;
785 else
786 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
787 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
788 break;
789 default:
790 debug("%s: Do not manage %d interface\n",
791 __func__, interface_type);
792 /* Do not manage others interfaces */
793 return -EINVAL;
794 }
795
796 /* clear and set ETH configuration bits */
797 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
798 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
799 syscfg + SYSCFG_PMCCLRR);
800 writel(value, syscfg + SYSCFG_PMCSETR);
801
802 return 0;
803}
804
Marek Vasut5ff05292020-01-24 18:39:16 +0100805#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900806int ft_board_setup(void *blob, struct bd_info *bd)
Marek Vasut5ff05292020-01-24 18:39:16 +0100807{
Marek Vasut7f809fe2022-05-11 23:09:33 +0200808 const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3";
809 int buck3off, ret, uv;
810
811 ret = board_get_regulator_buck3_nvm_uv_av96(&uv);
812 if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */
813 return 0;
814
815 buck3off = fdt_path_offset(blob, buck3path);
816 if (buck3off < 0) /* No Buck3 regulator found. */
817 return 0;
818
819 ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv);
820 if (ret < 0)
821 return ret;
822
823 ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv);
824 if (ret < 0)
825 return ret;
826
Marek Vasut5ff05292020-01-24 18:39:16 +0100827 return 0;
828}
829#endif
830
Marek Vasut5ff05292020-01-24 18:39:16 +0100831static void board_copro_image_process(ulong fw_image, size_t fw_size)
832{
833 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
834
835 if (!rproc_is_initialized())
836 if (rproc_init()) {
837 printf("Remote Processor %d initialization failed\n",
838 id);
839 return;
840 }
841
842 ret = rproc_load(id, fw_image, fw_size);
843 printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
844 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
845
846 if (!ret) {
847 rproc_start(id);
848 env_set("copro_state", "booted");
849 }
850}
851
852U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);