blob: e4d172322011cf77fa299cc0eccf633964724a08 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunayf8598d92018-03-12 10:46:18 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunayf8598d92018-03-12 10:46:18 +01004 */
Patrice Chotard395f1292019-02-12 16:50:40 +01005#include <common.h>
6#include <adc.h>
Patrick Delaunayf8598d92018-03-12 10:46:18 +01007#include <config.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +02008#include <clk.h>
9#include <dm.h>
Patrice Chotard8f24b1a2019-05-02 18:28:05 +020010#include <environment.h>
Patrick Delaunayc31000c2019-03-29 15:42:23 +010011#include <g_dnl.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020012#include <generic-phy.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010013#include <i2c.h>
Patrick Delaunayd461f102019-02-12 11:44:41 +010014#include <led.h>
15#include <misc.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020016#include <phy.h>
17#include <reset.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010018#include <syscon.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010019#include <usb.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020020#include <asm/io.h>
Patrick Delaunay842ebb52019-02-27 17:01:18 +010021#include <asm/gpio.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010022#include <asm/arch/stm32.h>
Patrice Chotard7f90cd62019-05-02 18:36:01 +020023#include <asm/arch/sys_proto.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020024#include <power/regulator.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010025#include <usb/dwc2_udc.h>
Patrick Delaunayf8598d92018-03-12 10:46:18 +010026
Patrick Delaunay45459742019-02-27 17:01:24 +010027/* SYSCFG registers */
28#define SYSCFG_BOOTR 0x00
29#define SYSCFG_PMCSETR 0x04
30#define SYSCFG_IOCTRLSETR 0x18
31#define SYSCFG_ICNR 0x1C
32#define SYSCFG_CMPCR 0x20
33#define SYSCFG_CMPENSETR 0x24
34#define SYSCFG_PMCCLRR 0x44
35
36#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
37#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
38
39#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
40#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
41#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
42#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
43#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
44
45#define SYSCFG_CMPCR_SW_CTRL BIT(1)
46#define SYSCFG_CMPCR_READY BIT(8)
47
48#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
49
50#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
51#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
52
53#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
54
55#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Christophe Roullieredacf262019-05-17 15:08:43 +020056#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
57#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
58#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
Patrick Delaunay45459742019-02-27 17:01:24 +010059
Patrick Delaunayf8598d92018-03-12 10:46:18 +010060/*
61 * Get a global data pointer
62 */
63DECLARE_GLOBAL_DATA_PTR;
64
Patrice Chotard28c064e2019-04-30 18:09:38 +020065#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard395f1292019-02-12 16:50:40 +010066#define USB_WARNING_LOW_THRESHOLD_UV 660000
67#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotard28c064e2019-04-30 18:09:38 +020068#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard395f1292019-02-12 16:50:40 +010069
Patrick Delaunayd461f102019-02-12 11:44:41 +010070int checkboard(void)
71{
72 int ret;
73 char *mode;
74 u32 otp;
75 struct udevice *dev;
76 const char *fdt_compat;
77 int fdt_compat_len;
78
79 if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
80 mode = "trusted";
81 else
82 mode = "basic";
83
84 printf("Board: stm32mp1 in %s mode", mode);
85 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
86 &fdt_compat_len);
87 if (fdt_compat && fdt_compat_len)
88 printf(" (%s)", fdt_compat);
89 puts("\n");
90
91 ret = uclass_get_device_by_driver(UCLASS_MISC,
92 DM_GET_DRIVER(stm32mp_bsec),
93 &dev);
94
95 if (!ret)
96 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
97 &otp, sizeof(otp));
98 if (!ret && otp) {
99 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
100 otp >> 16,
101 (otp >> 12) & 0xF,
102 ((otp >> 8) & 0xF) - 1 + 'A',
103 otp & 0xF);
104 }
105
106 return 0;
107}
108
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100109static void board_key_check(void)
110{
111#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
112 ofnode node;
113 struct gpio_desc gpio;
114 enum forced_boot_mode boot_mode = BOOT_NORMAL;
115
116 node = ofnode_path("/config");
117 if (!ofnode_valid(node)) {
118 debug("%s: no /config node?\n", __func__);
119 return;
120 }
121#ifdef CONFIG_FASTBOOT
122 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
123 &gpio, GPIOD_IS_IN)) {
124 debug("%s: could not find a /config/st,fastboot-gpios\n",
125 __func__);
126 } else {
127 if (dm_gpio_get_value(&gpio)) {
128 puts("Fastboot key pressed, ");
129 boot_mode = BOOT_FASTBOOT;
130 }
131
132 dm_gpio_free(NULL, &gpio);
133 }
134#endif
135#ifdef CONFIG_CMD_STM32PROG
136 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
137 &gpio, GPIOD_IS_IN)) {
138 debug("%s: could not find a /config/st,stm32prog-gpios\n",
139 __func__);
140 } else {
141 if (dm_gpio_get_value(&gpio)) {
142 puts("STM32Programmer key pressed, ");
143 boot_mode = BOOT_STM32PROG;
144 }
145 dm_gpio_free(NULL, &gpio);
146 }
147#endif
148
149 if (boot_mode != BOOT_NORMAL) {
150 puts("entering download mode...\n");
151 clrsetbits_le32(TAMP_BOOT_CONTEXT,
152 TAMP_BOOT_FORCED_MASK,
153 boot_mode);
154 }
155#endif
156}
157
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100158#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200159
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100160/* STMicroelectronics STUSB1600 Type-C controller */
161#define STUSB1600_CC_CONNECTION_STATUS 0x0E
162
163/* STUSB1600_CC_CONNECTION_STATUS bitfields */
164#define STUSB1600_CC_ATTACH BIT(0)
165
166static int stusb1600_init(struct udevice **dev_stusb1600)
167{
168 ofnode node;
169 struct udevice *dev, *bus;
170 int ret;
171 u32 chip_addr;
172
173 *dev_stusb1600 = NULL;
174
175 /* if node stusb1600 is present, means DK1 or DK2 board */
176 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
177 if (!ofnode_valid(node))
178 return -ENODEV;
179
180 ret = ofnode_read_u32(node, "reg", &chip_addr);
181 if (ret)
182 return -EINVAL;
183
184 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
185 &bus);
186 if (ret) {
187 printf("bus for stusb1600 not found\n");
188 return -ENODEV;
189 }
190
191 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
192 if (!ret)
193 *dev_stusb1600 = dev;
194
195 return ret;
196}
197
198static int stusb1600_cable_connected(struct udevice *dev)
199{
200 u8 status;
201
202 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
203 return 0;
204
205 return status & STUSB1600_CC_ATTACH;
206}
207
208#include <usb/dwc2_udc.h>
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100209int g_dnl_board_usb_cable_connected(void)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200210{
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100211 struct udevice *stusb1600;
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100212 struct udevice *dwc2_udc_otg;
Patrice Chotard4c834b92018-08-10 17:12:14 +0200213 int ret;
214
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100215 if (!stusb1600_init(&stusb1600))
216 return stusb1600_cable_connected(stusb1600);
217
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100218 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
219 DM_GET_DRIVER(dwc2_udc_otg),
220 &dwc2_udc_otg);
221 if (!ret)
222 debug("dwc2_udc_otg init failed\n");
Patrice Chotard4c834b92018-08-10 17:12:14 +0200223
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100224 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard4c834b92018-08-10 17:12:14 +0200225}
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100226#endif /* CONFIG_USB_GADGET */
Patrice Chotard4c834b92018-08-10 17:12:14 +0200227
Patrice Chotard395f1292019-02-12 16:50:40 +0100228static int get_led(struct udevice **dev, char *led_string)
229{
230 char *led_name;
231 int ret;
232
233 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
234 if (!led_name) {
235 pr_debug("%s: could not find %s config string\n",
236 __func__, led_string);
237 return -ENOENT;
238 }
239 ret = led_get_by_label(led_name, dev);
240 if (ret) {
241 debug("%s: get=%d\n", __func__, ret);
242 return ret;
243 }
244
245 return 0;
246}
247
248static int setup_led(enum led_state_t cmd)
249{
250 struct udevice *dev;
251 int ret;
252
253 ret = get_led(&dev, "u-boot,boot-led");
254 if (ret)
255 return ret;
256
257 ret = led_set_state(dev, cmd);
258 return ret;
259}
260
261static int board_check_usb_power(void)
262{
263 struct ofnode_phandle_args adc_args;
264 struct udevice *adc;
265 struct udevice *led;
266 ofnode node;
267 unsigned int raw;
268 int max_uV = 0;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200269 int min_uV = USB_START_HIGH_THRESHOLD_UV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100270 int ret, uV, adc_count;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200271 u32 nb_blink;
272 u8 i;
Patrice Chotard395f1292019-02-12 16:50:40 +0100273 node = ofnode_path("/config");
274 if (!ofnode_valid(node)) {
275 debug("%s: no /config node?\n", __func__);
276 return -ENOENT;
277 }
278
279 /*
280 * Retrieve the ADC channels devices and get measurement
281 * for each of them
282 */
283 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
284 "#io-channel-cells");
285 if (adc_count < 0) {
286 if (adc_count == -ENOENT)
287 return 0;
288
289 pr_err("%s: can't find adc channel (%d)\n", __func__,
290 adc_count);
291
292 return adc_count;
293 }
294
295 for (i = 0; i < adc_count; i++) {
296 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
297 "#io-channel-cells", 0, i,
298 &adc_args)) {
299 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
300 __func__);
301 return 0;
302 }
303
304 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
305 &adc);
306
307 if (ret) {
308 pr_err("%s: Can't get adc device(%d)\n", __func__,
309 ret);
310 return ret;
311 }
312
313 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
314 &raw);
315 if (ret) {
316 pr_err("%s: single shot failed for %s[%d]!\n",
317 __func__, adc->name, adc_args.args[0]);
318 return ret;
319 }
320 /* Convert to uV */
321 if (!adc_raw_to_uV(adc, raw, &uV)) {
322 if (uV > max_uV)
323 max_uV = uV;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200324 if (uV < min_uV)
325 min_uV = uV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100326 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
327 adc->name, adc_args.args[0], raw, uV);
328 } else {
329 pr_err("%s: Can't get uV value for %s[%d]\n",
330 __func__, adc->name, adc_args.args[0]);
331 }
332 }
333
334 /*
335 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
336 * board is plugged on an USB-C 3A power supply and boot process can
337 * continue.
338 */
339 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
Patrice Chotard28c064e2019-04-30 18:09:38 +0200340 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
341 min_uV <= USB_LOW_THRESHOLD_UV)
Patrice Chotard395f1292019-02-12 16:50:40 +0100342 return 0;
343
Patrice Chotard28c064e2019-04-30 18:09:38 +0200344 pr_err("****************************************************\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100345
Patrice Chotard28c064e2019-04-30 18:09:38 +0200346 /*
347 * If highest and lowest value are either both below
348 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
349 * means USB TYPE-C is in unattached mode, this is an issue, make
350 * u-boot,error-led blinking and stop boot process.
351 */
352 if ((max_uV > USB_LOW_THRESHOLD_UV &&
353 min_uV > USB_LOW_THRESHOLD_UV) ||
354 (max_uV <= USB_LOW_THRESHOLD_UV &&
355 min_uV <= USB_LOW_THRESHOLD_UV)) {
356 pr_err("* ERROR USB TYPE-C connection in unattached mode *\n");
357 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
358 /* with 125ms interval, led will blink for 17.02 years ....*/
359 nb_blink = U32_MAX;
360 }
361
362 if (max_uV > USB_LOW_THRESHOLD_UV &&
363 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
364 min_uV <= USB_LOW_THRESHOLD_UV) {
365 pr_err("* WARNING 500mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100366 nb_blink = 2;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200367 }
368
369 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
370 max_uV <= USB_START_LOW_THRESHOLD_UV &&
371 min_uV <= USB_LOW_THRESHOLD_UV) {
372 pr_err("* WARNING 1.5mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100373 nb_blink = 3;
374 }
375
Patrice Chotard28c064e2019-04-30 18:09:38 +0200376 /*
377 * If highest value is above 2.15 Volts that means that the USB TypeC
378 * supplies more than 3 Amp, this is not compliant with TypeC specification
379 */
380 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
381 pr_err("* USB TYPE-C charger not compliant with *\n");
382 pr_err("* specification *\n");
383 pr_err("****************************************************\n\n");
384 /* with 125ms interval, led will blink for 17.02 years ....*/
385 nb_blink = U32_MAX;
386 } else {
387 pr_err("* Current too low, use a 3A power supply! *\n");
388 pr_err("****************************************************\n\n");
389 }
Patrice Chotard395f1292019-02-12 16:50:40 +0100390
391 ret = get_led(&led, "u-boot,error-led");
Patrice Chotard28c064e2019-04-30 18:09:38 +0200392 if (ret) {
393 /* in unattached case, the boot process must be stopped */
394 if (nb_blink == U32_MAX)
395 hang();
Patrice Chotard395f1292019-02-12 16:50:40 +0100396 return ret;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200397 }
Patrice Chotard395f1292019-02-12 16:50:40 +0100398
Patrice Chotard28c064e2019-04-30 18:09:38 +0200399 /* make u-boot,error-led blinking */
Patrice Chotard395f1292019-02-12 16:50:40 +0100400 for (i = 0; i < nb_blink * 2; i++) {
401 led_set_state(led, LEDST_TOGGLE);
402 mdelay(125);
403 }
404 led_set_state(led, LEDST_ON);
405
406 return 0;
407}
408
Patrick Delaunay45459742019-02-27 17:01:24 +0100409static void sysconf_init(void)
410{
411#ifndef CONFIG_STM32MP1_TRUSTED
412 u8 *syscfg;
413#ifdef CONFIG_DM_REGULATOR
414 struct udevice *pwr_dev;
415 struct udevice *pwr_reg;
416 struct udevice *dev;
417 int ret;
418 u32 otp = 0;
419#endif
420 u32 bootr;
421
422 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
423
424 /* interconnect update : select master using the port 1 */
425 /* LTDC = AXI_M9 */
426 /* GPU = AXI_M8 */
427 /* today information is hardcoded in U-Boot */
428 writel(BIT(9), syscfg + SYSCFG_ICNR);
429
430 /* disable Pull-Down for boot pin connected to VDD */
431 bootr = readl(syscfg + SYSCFG_BOOTR);
432 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
433 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
434 writel(bootr, syscfg + SYSCFG_BOOTR);
435
436#ifdef CONFIG_DM_REGULATOR
437 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
438 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
439 * The customer will have to disable this for low frequencies
440 * or if AFMUX is selected but the function not used, typically for
441 * TRACE. Otherwise, impact on power consumption.
442 *
443 * WARNING:
444 * enabling High Speed mode while VDD>2.7V
445 * with the OTP product_below_2v5 (OTP 18, BIT 13)
446 * erroneously set to 1 can damage the IC!
447 * => U-Boot set the register only if VDD < 2.7V (in DT)
448 * but this value need to be consistent with board design
449 */
450 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
451 if (!ret) {
452 ret = uclass_get_device_by_driver(UCLASS_MISC,
453 DM_GET_DRIVER(stm32mp_bsec),
454 &dev);
455 if (ret) {
456 pr_err("Can't find stm32mp_bsec driver\n");
457 return;
458 }
459
460 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
461 if (!ret)
462 otp = otp & BIT(13);
463
464 /* get VDD = pwr-supply */
465 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
466 &pwr_reg);
467
468 /* check if VDD is Low Voltage */
469 if (!ret) {
470 if (regulator_get_value(pwr_reg) < 2700000) {
471 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
472 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
473 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
474 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
475 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
476 syscfg + SYSCFG_IOCTRLSETR);
477
478 if (!otp)
479 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
480 } else {
481 if (otp)
482 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
483 }
484 } else {
485 debug("VDD unknown");
486 }
487 }
488#endif
489
490 /* activate automatic I/O compensation
491 * warning: need to ensure CSI enabled and ready in clock driver
492 */
493 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
494
495 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
496 ;
497 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
498#endif
499}
500
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100501/* board dependent setup after realloc */
502int board_init(void)
503{
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100504 struct udevice *dev;
505
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100506 /* address of boot parameters */
507 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
508
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100509 /* probe all PINCTRL for hog */
510 for (uclass_first_device(UCLASS_PINCTRL, &dev);
511 dev;
512 uclass_next_device(&dev)) {
513 pr_debug("probe pincontrol = %s\n", dev->name);
514 }
515
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100516 board_key_check();
517
Patrick Delaunayf59ad452019-07-05 17:20:09 +0200518#ifdef CONFIG_DM_REGULATOR
519 regulators_enable_boot_on(_DEBUG);
520#endif
521
Patrick Delaunay45459742019-02-27 17:01:24 +0100522 sysconf_init();
523
Patrick Delaunay1f5118b2018-07-27 16:37:08 +0200524 if (IS_ENABLED(CONFIG_LED))
525 led_default_state();
526
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100527 return 0;
528}
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100529
530int board_late_init(void)
531{
532#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
533 const void *fdt_compat;
534 int fdt_compat_len;
535
536 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
537 &fdt_compat_len);
538 if (fdt_compat && fdt_compat_len) {
539 if (strncmp(fdt_compat, "st,", 3) != 0)
540 env_set("board_name", fdt_compat);
541 else
542 env_set("board_name", fdt_compat + 3);
543 }
544#endif
545
Patrice Chotard395f1292019-02-12 16:50:40 +0100546 /* for DK1/DK2 boards */
547 board_check_usb_power();
548
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100549 return 0;
550}
Patrice Chotard395f1292019-02-12 16:50:40 +0100551
552void board_quiesce_devices(void)
553{
554 setup_led(LEDST_OFF);
555}
Patrice Chotard87471642019-05-02 18:07:14 +0200556
Christophe Roullieredacf262019-05-17 15:08:43 +0200557/* board interface eth init */
558/* this is a weak define that we are overriding */
559int board_interface_eth_init(phy_interface_t interface_type,
560 bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
561{
562 u8 *syscfg;
563 u32 value;
564
565 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
566
567 if (!syscfg)
568 return -ENODEV;
569
570 switch (interface_type) {
571 case PHY_INTERFACE_MODE_MII:
572 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
573 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
574 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
575 break;
576 case PHY_INTERFACE_MODE_GMII:
577 if (eth_clk_sel_reg)
578 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
579 SYSCFG_PMCSETR_ETH_CLK_SEL;
580 else
581 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
582 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
583 break;
584 case PHY_INTERFACE_MODE_RMII:
585 if (eth_ref_clk_sel_reg)
586 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
587 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
588 else
589 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
590 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
591 break;
592 case PHY_INTERFACE_MODE_RGMII:
593 case PHY_INTERFACE_MODE_RGMII_ID:
594 case PHY_INTERFACE_MODE_RGMII_RXID:
595 case PHY_INTERFACE_MODE_RGMII_TXID:
596 if (eth_clk_sel_reg)
597 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
598 SYSCFG_PMCSETR_ETH_CLK_SEL;
599 else
600 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
601 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
602 break;
603 default:
604 debug("%s: Do not manage %d interface\n",
605 __func__, interface_type);
606 /* Do not manage others interfaces */
607 return -EINVAL;
608 }
609
610 /* clear and set ETH configuration bits */
611 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
612 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
613 syscfg + SYSCFG_PMCCLRR);
614 writel(value, syscfg + SYSCFG_PMCSETR);
615
616 return 0;
617}
618
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200619enum env_location env_get_location(enum env_operation op, int prio)
620{
621 u32 bootmode = get_bootmode();
622
623 if (prio)
624 return ENVL_UNKNOWN;
625
626 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
627#ifdef CONFIG_ENV_IS_IN_EXT4
628 case BOOT_FLASH_SD:
629 case BOOT_FLASH_EMMC:
630 return ENVL_EXT4;
631#endif
632#ifdef CONFIG_ENV_IS_IN_UBI
633 case BOOT_FLASH_NAND:
634 return ENVL_UBI;
635#endif
Patrice Chotarde5c38fd2019-05-09 14:25:36 +0200636#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
637 case BOOT_FLASH_NOR:
638 return ENVL_SPI_FLASH;
639#endif
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200640 default:
641 return ENVL_NOWHERE;
642 }
643}
644
Patrice Chotard7f90cd62019-05-02 18:36:01 +0200645#if defined(CONFIG_ENV_IS_IN_EXT4)
646const char *env_ext4_get_intf(void)
647{
648 u32 bootmode = get_bootmode();
649
650 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
651 case BOOT_FLASH_SD:
652 case BOOT_FLASH_EMMC:
653 return "mmc";
654 default:
655 return "";
656 }
657}
658
659const char *env_ext4_get_dev_part(void)
660{
661 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
662 u32 bootmode = get_bootmode();
663
664 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
665}
666#endif
667
Patrice Chotard87471642019-05-02 18:07:14 +0200668#ifdef CONFIG_SYS_MTDPARTS_RUNTIME
669
670#define MTDPARTS_LEN 256
671#define MTDIDS_LEN 128
672
673/**
674 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
675 * If we need to access it before the env is relocated, then we need
676 * to use our own stack buffer. gd->env_buf will be too small.
677 *
678 * @param buf temporary buffer pointer MTDPARTS_LEN long
679 * @return mtdparts variable string, NULL if not found
680 */
681static const char *env_get_mtdparts(const char *str, char *buf)
682{
683 if (gd->flags & GD_FLG_ENV_READY)
684 return env_get(str);
685 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
686 return buf;
687
688 return NULL;
689}
690
691/**
692 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
693 */
694static void board_get_mtdparts(const char *dev,
695 char *mtdids,
696 char *mtdparts)
697{
698 char env_name[32] = "mtdparts_";
699 char tmp_mtdparts[MTDPARTS_LEN];
700 const char *tmp;
701
702 /* name of env variable to read = mtdparts_<dev> */
703 strcat(env_name, dev);
704 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
705 if (tmp) {
706 /* mtdids: "<dev>=<dev>, ...." */
707 if (mtdids[0] != '\0')
708 strcat(mtdids, ",");
709 strcat(mtdids, dev);
710 strcat(mtdids, "=");
711 strcat(mtdids, dev);
712
713 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
714 if (mtdparts[0] != '\0')
715 strncat(mtdparts, ";", MTDPARTS_LEN);
716 else
717 strcat(mtdparts, "mtdparts=");
718 strncat(mtdparts, dev, MTDPARTS_LEN);
719 strncat(mtdparts, ":", MTDPARTS_LEN);
720 strncat(mtdparts, tmp, MTDPARTS_LEN);
721 }
722}
723
724void board_mtdparts_default(const char **mtdids, const char **mtdparts)
725{
726 struct udevice *dev;
727 static char parts[2 * MTDPARTS_LEN + 1];
728 static char ids[MTDIDS_LEN + 1];
729 static bool mtd_initialized;
730
731 if (mtd_initialized) {
732 *mtdids = ids;
733 *mtdparts = parts;
734 return;
735 }
736
737 memset(parts, 0, sizeof(parts));
738 memset(ids, 0, sizeof(ids));
739
740 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
741 board_get_mtdparts("nand0", ids, parts);
742
743 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
744 board_get_mtdparts("nor0", ids, parts);
745
746 mtd_initialized = true;
747 *mtdids = ids;
748 *mtdparts = parts;
749 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
750}
751#endif