blob: 776929350f9b5dca7745307209d5ebb11d392b91 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01004 */
Patrice Chotard879cde52019-02-12 16:50:40 +01005#include <common.h>
6#include <adc.h>
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01007#include <config.h>
Patrice Chotard204079b2018-08-10 17:12:14 +02008#include <clk.h>
9#include <dm.h>
Patrice Chotard34320372019-05-02 18:28:05 +020010#include <environment.h>
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +010011#include <g_dnl.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020012#include <generic-phy.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010013#include <i2c.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010014#include <led.h>
15#include <misc.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020016#include <phy.h>
17#include <reset.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010018#include <syscon.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010019#include <usb.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020020#include <asm/io.h>
Patrick Delaunayf2a7b872019-02-27 17:01:18 +010021#include <asm/gpio.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010022#include <asm/arch/stm32.h>
Patrice Chotarddad97bf2019-05-02 18:36:01 +020023#include <asm/arch/sys_proto.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020024#include <power/regulator.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010025#include <usb/dwc2_udc.h>
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010026
Patrick Delaunay4ace1d12019-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 Roullier69ac3f52019-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 Delaunay4ace1d12019-02-27 17:01:24 +010059
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010060/*
61 * Get a global data pointer
62 */
63DECLARE_GLOBAL_DATA_PTR;
64
Patrice Chotardcded32f2019-04-30 18:09:38 +020065#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard879cde52019-02-12 16:50:40 +010066#define USB_WARNING_LOW_THRESHOLD_UV 660000
67#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotardcded32f2019-04-30 18:09:38 +020068#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard879cde52019-02-12 16:50:40 +010069
Patrick Delaunay92dc1022019-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 Delaunay008d3c32019-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 Delaunay58bc0cd2019-03-29 15:42:23 +0100158#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
Patrice Chotard204079b2018-08-10 17:12:14 +0200159
Patrick Delaunay7f3384d2019-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 Delaunay58bc0cd2019-03-29 15:42:23 +0100209int g_dnl_board_usb_cable_connected(void)
Patrice Chotard204079b2018-08-10 17:12:14 +0200210{
Patrick Delaunay7f3384d2019-03-29 15:42:24 +0100211 struct udevice *stusb1600;
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100212 struct udevice *dwc2_udc_otg;
Patrice Chotard204079b2018-08-10 17:12:14 +0200213 int ret;
214
Patrick Delaunay7f3384d2019-03-29 15:42:24 +0100215 if (!stusb1600_init(&stusb1600))
216 return stusb1600_cable_connected(stusb1600);
217
Patrick Delaunay58bc0cd2019-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 Chotard204079b2018-08-10 17:12:14 +0200223
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100224 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard879cde52019-02-12 16:50:40 +0100225}
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100226#endif /* CONFIG_USB_GADGET */
Patrice Chotard879cde52019-02-12 16:50:40 +0100227
228static 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);
Patrice Chotard204079b2018-08-10 17:12:14 +0200258 return ret;
259}
260
Patrice Chotard879cde52019-02-12 16:50:40 +0100261static 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 Chotardcded32f2019-04-30 18:09:38 +0200269 int min_uV = USB_START_HIGH_THRESHOLD_UV;
Patrice Chotard879cde52019-02-12 16:50:40 +0100270 int ret, uV, adc_count;
Patrice Chotardcded32f2019-04-30 18:09:38 +0200271 u32 nb_blink;
272 u8 i;
Patrice Chotard879cde52019-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 Chotardcded32f2019-04-30 18:09:38 +0200324 if (uV < min_uV)
325 min_uV = uV;
Patrice Chotard879cde52019-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 Chotardcded32f2019-04-30 18:09:38 +0200340 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
341 min_uV <= USB_LOW_THRESHOLD_UV)
Patrice Chotard879cde52019-02-12 16:50:40 +0100342 return 0;
343
Patrice Chotardcded32f2019-04-30 18:09:38 +0200344 pr_err("****************************************************\n");
345
346 /*
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 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100361
Patrice Chotardcded32f2019-04-30 18:09:38 +0200362 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 Chotard879cde52019-02-12 16:50:40 +0100366 nb_blink = 2;
Patrice Chotardcded32f2019-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 Chotard879cde52019-02-12 16:50:40 +0100373 nb_blink = 3;
374 }
375
Patrice Chotardcded32f2019-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 Chotard879cde52019-02-12 16:50:40 +0100390
391 ret = get_led(&led, "u-boot,error-led");
Patrice Chotardcded32f2019-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 Chotard879cde52019-02-12 16:50:40 +0100396 return ret;
Patrice Chotardcded32f2019-04-30 18:09:38 +0200397 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100398
Patrice Chotardcded32f2019-04-30 18:09:38 +0200399 /* make u-boot,error-led blinking */
Patrice Chotard879cde52019-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 Delaunay4ace1d12019-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 Delaunay8eb3b1e2018-03-12 10:46:18 +0100501/* board dependent setup after realloc */
502int board_init(void)
503{
Patrice Chotard972723a2019-03-11 11:13:17 +0100504 struct udevice *dev;
505
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100506 /* address of boot parameters */
507 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
508
Patrice Chotard972723a2019-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 Delaunay008d3c32019-02-27 17:01:20 +0100516 board_key_check();
517
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100518 sysconf_init();
519
Patrick Delaunay36e3d112018-07-27 16:37:08 +0200520 if (IS_ENABLED(CONFIG_LED))
521 led_default_state();
522
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100523 return 0;
524}
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100525
526int board_late_init(void)
527{
528#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
529 const void *fdt_compat;
530 int fdt_compat_len;
531
532 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
533 &fdt_compat_len);
534 if (fdt_compat && fdt_compat_len) {
535 if (strncmp(fdt_compat, "st,", 3) != 0)
536 env_set("board_name", fdt_compat);
537 else
538 env_set("board_name", fdt_compat + 3);
539 }
540#endif
541
Patrice Chotard879cde52019-02-12 16:50:40 +0100542 /* for DK1/DK2 boards */
543 board_check_usb_power();
544
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100545 return 0;
546}
Patrice Chotard879cde52019-02-12 16:50:40 +0100547
548void board_quiesce_devices(void)
549{
550 setup_led(LEDST_OFF);
551}
Patrice Chotard41443cf2019-05-02 18:07:14 +0200552
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200553/* board interface eth init */
554/* this is a weak define that we are overriding */
555int board_interface_eth_init(phy_interface_t interface_type,
556 bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
557{
558 u8 *syscfg;
559 u32 value;
560
561 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
562
563 if (!syscfg)
564 return -ENODEV;
565
566 switch (interface_type) {
567 case PHY_INTERFACE_MODE_MII:
568 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
569 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
570 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
571 break;
572 case PHY_INTERFACE_MODE_GMII:
573 if (eth_clk_sel_reg)
574 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
575 SYSCFG_PMCSETR_ETH_CLK_SEL;
576 else
577 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
578 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
579 break;
580 case PHY_INTERFACE_MODE_RMII:
581 if (eth_ref_clk_sel_reg)
582 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
583 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
584 else
585 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
586 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
587 break;
588 case PHY_INTERFACE_MODE_RGMII:
589 case PHY_INTERFACE_MODE_RGMII_ID:
590 case PHY_INTERFACE_MODE_RGMII_RXID:
591 case PHY_INTERFACE_MODE_RGMII_TXID:
592 if (eth_clk_sel_reg)
593 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
594 SYSCFG_PMCSETR_ETH_CLK_SEL;
595 else
596 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
597 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
598 break;
599 default:
600 debug("%s: Do not manage %d interface\n",
601 __func__, interface_type);
602 /* Do not manage others interfaces */
603 return -EINVAL;
604 }
605
606 /* clear and set ETH configuration bits */
607 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
608 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
609 syscfg + SYSCFG_PMCCLRR);
610 writel(value, syscfg + SYSCFG_PMCSETR);
611
612 return 0;
613}
614
Patrice Chotard34320372019-05-02 18:28:05 +0200615enum env_location env_get_location(enum env_operation op, int prio)
616{
617 u32 bootmode = get_bootmode();
618
619 if (prio)
620 return ENVL_UNKNOWN;
621
622 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
623#ifdef CONFIG_ENV_IS_IN_EXT4
624 case BOOT_FLASH_SD:
625 case BOOT_FLASH_EMMC:
626 return ENVL_EXT4;
627#endif
628#ifdef CONFIG_ENV_IS_IN_UBI
629 case BOOT_FLASH_NAND:
630 return ENVL_UBI;
631#endif
Patrice Chotard2c461ec2019-05-09 14:25:36 +0200632#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
633 case BOOT_FLASH_NOR:
634 return ENVL_SPI_FLASH;
635#endif
Patrice Chotard34320372019-05-02 18:28:05 +0200636 default:
637 return ENVL_NOWHERE;
638 }
639}
640
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200641#if defined(CONFIG_ENV_IS_IN_EXT4)
642const char *env_ext4_get_intf(void)
643{
644 u32 bootmode = get_bootmode();
645
646 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
647 case BOOT_FLASH_SD:
648 case BOOT_FLASH_EMMC:
649 return "mmc";
650 default:
651 return "";
652 }
653}
654
655const char *env_ext4_get_dev_part(void)
656{
657 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
658 u32 bootmode = get_bootmode();
659
660 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
661}
662#endif
663
Patrice Chotard41443cf2019-05-02 18:07:14 +0200664#ifdef CONFIG_SYS_MTDPARTS_RUNTIME
665
666#define MTDPARTS_LEN 256
667#define MTDIDS_LEN 128
668
669/**
670 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
671 * If we need to access it before the env is relocated, then we need
672 * to use our own stack buffer. gd->env_buf will be too small.
673 *
674 * @param buf temporary buffer pointer MTDPARTS_LEN long
675 * @return mtdparts variable string, NULL if not found
676 */
677static const char *env_get_mtdparts(const char *str, char *buf)
678{
679 if (gd->flags & GD_FLG_ENV_READY)
680 return env_get(str);
681 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
682 return buf;
683
684 return NULL;
685}
686
687/**
688 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
689 */
690static void board_get_mtdparts(const char *dev,
691 char *mtdids,
692 char *mtdparts)
693{
694 char env_name[32] = "mtdparts_";
695 char tmp_mtdparts[MTDPARTS_LEN];
696 const char *tmp;
697
698 /* name of env variable to read = mtdparts_<dev> */
699 strcat(env_name, dev);
700 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
701 if (tmp) {
702 /* mtdids: "<dev>=<dev>, ...." */
703 if (mtdids[0] != '\0')
704 strcat(mtdids, ",");
705 strcat(mtdids, dev);
706 strcat(mtdids, "=");
707 strcat(mtdids, dev);
708
709 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
710 if (mtdparts[0] != '\0')
711 strncat(mtdparts, ";", MTDPARTS_LEN);
712 else
713 strcat(mtdparts, "mtdparts=");
714 strncat(mtdparts, dev, MTDPARTS_LEN);
715 strncat(mtdparts, ":", MTDPARTS_LEN);
716 strncat(mtdparts, tmp, MTDPARTS_LEN);
717 }
718}
719
720void board_mtdparts_default(const char **mtdids, const char **mtdparts)
721{
722 struct udevice *dev;
723 static char parts[2 * MTDPARTS_LEN + 1];
724 static char ids[MTDIDS_LEN + 1];
725 static bool mtd_initialized;
726
727 if (mtd_initialized) {
728 *mtdids = ids;
729 *mtdparts = parts;
730 return;
731 }
732
733 memset(parts, 0, sizeof(parts));
734 memset(ids, 0, sizeof(ids));
735
736 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
737 board_get_mtdparts("nand0", ids, parts);
738
739 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
740 board_get_mtdparts("nor0", ids, parts);
741
742 mtd_initialized = true;
743 *mtdids = ids;
744 *mtdparts = parts;
745 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
746}
747#endif