blob: ff4907d44aa027e086148d994c5138f3e657c37c [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>
Patrick Delaunaye81f8d12019-07-02 13:26:07 +020016#include <mtd.h>
17#include <mtd_node.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020018#include <phy.h>
19#include <reset.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010020#include <syscon.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010021#include <usb.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020022#include <asm/io.h>
Patrick Delaunay842ebb52019-02-27 17:01:18 +010023#include <asm/gpio.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010024#include <asm/arch/stm32.h>
Patrice Chotard7f90cd62019-05-02 18:36:01 +020025#include <asm/arch/sys_proto.h>
Patrick Delaunaye81f8d12019-07-02 13:26:07 +020026#include <jffs2/load_kernel.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020027#include <power/regulator.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010028#include <usb/dwc2_udc.h>
Patrick Delaunayf8598d92018-03-12 10:46:18 +010029
Patrick Delaunay45459742019-02-27 17:01:24 +010030/* SYSCFG registers */
31#define SYSCFG_BOOTR 0x00
32#define SYSCFG_PMCSETR 0x04
33#define SYSCFG_IOCTRLSETR 0x18
34#define SYSCFG_ICNR 0x1C
35#define SYSCFG_CMPCR 0x20
36#define SYSCFG_CMPENSETR 0x24
37#define SYSCFG_PMCCLRR 0x44
38
39#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
40#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
41
42#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
43#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
44#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
45#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
46#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
47
48#define SYSCFG_CMPCR_SW_CTRL BIT(1)
49#define SYSCFG_CMPCR_READY BIT(8)
50
51#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
52
53#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
54#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
55
56#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
57
58#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Christophe Roullieredacf262019-05-17 15:08:43 +020059#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
60#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
61#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
Patrick Delaunay45459742019-02-27 17:01:24 +010062
Patrick Delaunayf8598d92018-03-12 10:46:18 +010063/*
64 * Get a global data pointer
65 */
66DECLARE_GLOBAL_DATA_PTR;
67
Patrice Chotard28c064e2019-04-30 18:09:38 +020068#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard395f1292019-02-12 16:50:40 +010069#define USB_WARNING_LOW_THRESHOLD_UV 660000
70#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotard28c064e2019-04-30 18:09:38 +020071#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard395f1292019-02-12 16:50:40 +010072
Patrick Delaunayd461f102019-02-12 11:44:41 +010073int checkboard(void)
74{
75 int ret;
76 char *mode;
77 u32 otp;
78 struct udevice *dev;
79 const char *fdt_compat;
80 int fdt_compat_len;
81
Patrick Delaunay152c84b2019-07-02 13:26:06 +020082 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
83 mode = "trusted with OP-TEE";
84 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
Patrick Delaunayd461f102019-02-12 11:44:41 +010085 mode = "trusted";
86 else
87 mode = "basic";
88
89 printf("Board: stm32mp1 in %s mode", mode);
90 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
91 &fdt_compat_len);
92 if (fdt_compat && fdt_compat_len)
93 printf(" (%s)", fdt_compat);
94 puts("\n");
95
96 ret = uclass_get_device_by_driver(UCLASS_MISC,
97 DM_GET_DRIVER(stm32mp_bsec),
98 &dev);
99
100 if (!ret)
101 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
102 &otp, sizeof(otp));
103 if (!ret && otp) {
104 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
105 otp >> 16,
106 (otp >> 12) & 0xF,
107 ((otp >> 8) & 0xF) - 1 + 'A',
108 otp & 0xF);
109 }
110
111 return 0;
112}
113
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100114static void board_key_check(void)
115{
116#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
117 ofnode node;
118 struct gpio_desc gpio;
119 enum forced_boot_mode boot_mode = BOOT_NORMAL;
120
121 node = ofnode_path("/config");
122 if (!ofnode_valid(node)) {
123 debug("%s: no /config node?\n", __func__);
124 return;
125 }
126#ifdef CONFIG_FASTBOOT
127 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
128 &gpio, GPIOD_IS_IN)) {
129 debug("%s: could not find a /config/st,fastboot-gpios\n",
130 __func__);
131 } else {
132 if (dm_gpio_get_value(&gpio)) {
133 puts("Fastboot key pressed, ");
134 boot_mode = BOOT_FASTBOOT;
135 }
136
137 dm_gpio_free(NULL, &gpio);
138 }
139#endif
140#ifdef CONFIG_CMD_STM32PROG
141 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
142 &gpio, GPIOD_IS_IN)) {
143 debug("%s: could not find a /config/st,stm32prog-gpios\n",
144 __func__);
145 } else {
146 if (dm_gpio_get_value(&gpio)) {
147 puts("STM32Programmer key pressed, ");
148 boot_mode = BOOT_STM32PROG;
149 }
150 dm_gpio_free(NULL, &gpio);
151 }
152#endif
153
154 if (boot_mode != BOOT_NORMAL) {
155 puts("entering download mode...\n");
156 clrsetbits_le32(TAMP_BOOT_CONTEXT,
157 TAMP_BOOT_FORCED_MASK,
158 boot_mode);
159 }
160#endif
161}
162
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100163#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200164
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100165/* STMicroelectronics STUSB1600 Type-C controller */
166#define STUSB1600_CC_CONNECTION_STATUS 0x0E
167
168/* STUSB1600_CC_CONNECTION_STATUS bitfields */
169#define STUSB1600_CC_ATTACH BIT(0)
170
171static int stusb1600_init(struct udevice **dev_stusb1600)
172{
173 ofnode node;
174 struct udevice *dev, *bus;
175 int ret;
176 u32 chip_addr;
177
178 *dev_stusb1600 = NULL;
179
180 /* if node stusb1600 is present, means DK1 or DK2 board */
181 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
182 if (!ofnode_valid(node))
183 return -ENODEV;
184
185 ret = ofnode_read_u32(node, "reg", &chip_addr);
186 if (ret)
187 return -EINVAL;
188
189 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
190 &bus);
191 if (ret) {
192 printf("bus for stusb1600 not found\n");
193 return -ENODEV;
194 }
195
196 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
197 if (!ret)
198 *dev_stusb1600 = dev;
199
200 return ret;
201}
202
203static int stusb1600_cable_connected(struct udevice *dev)
204{
205 u8 status;
206
207 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
208 return 0;
209
210 return status & STUSB1600_CC_ATTACH;
211}
212
213#include <usb/dwc2_udc.h>
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100214int g_dnl_board_usb_cable_connected(void)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200215{
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100216 struct udevice *stusb1600;
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100217 struct udevice *dwc2_udc_otg;
Patrice Chotard4c834b92018-08-10 17:12:14 +0200218 int ret;
219
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100220 if (!stusb1600_init(&stusb1600))
221 return stusb1600_cable_connected(stusb1600);
222
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100223 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
224 DM_GET_DRIVER(dwc2_udc_otg),
225 &dwc2_udc_otg);
226 if (!ret)
227 debug("dwc2_udc_otg init failed\n");
Patrice Chotard4c834b92018-08-10 17:12:14 +0200228
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100229 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard4c834b92018-08-10 17:12:14 +0200230}
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100231#endif /* CONFIG_USB_GADGET */
Patrice Chotard4c834b92018-08-10 17:12:14 +0200232
Patrice Chotard395f1292019-02-12 16:50:40 +0100233static int get_led(struct udevice **dev, char *led_string)
234{
235 char *led_name;
236 int ret;
237
238 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
239 if (!led_name) {
240 pr_debug("%s: could not find %s config string\n",
241 __func__, led_string);
242 return -ENOENT;
243 }
244 ret = led_get_by_label(led_name, dev);
245 if (ret) {
246 debug("%s: get=%d\n", __func__, ret);
247 return ret;
248 }
249
250 return 0;
251}
252
253static int setup_led(enum led_state_t cmd)
254{
255 struct udevice *dev;
256 int ret;
257
258 ret = get_led(&dev, "u-boot,boot-led");
259 if (ret)
260 return ret;
261
262 ret = led_set_state(dev, cmd);
263 return ret;
264}
265
266static int board_check_usb_power(void)
267{
268 struct ofnode_phandle_args adc_args;
269 struct udevice *adc;
270 struct udevice *led;
271 ofnode node;
272 unsigned int raw;
273 int max_uV = 0;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200274 int min_uV = USB_START_HIGH_THRESHOLD_UV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100275 int ret, uV, adc_count;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200276 u32 nb_blink;
277 u8 i;
Patrice Chotard395f1292019-02-12 16:50:40 +0100278 node = ofnode_path("/config");
279 if (!ofnode_valid(node)) {
280 debug("%s: no /config node?\n", __func__);
281 return -ENOENT;
282 }
283
284 /*
285 * Retrieve the ADC channels devices and get measurement
286 * for each of them
287 */
288 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
289 "#io-channel-cells");
290 if (adc_count < 0) {
291 if (adc_count == -ENOENT)
292 return 0;
293
294 pr_err("%s: can't find adc channel (%d)\n", __func__,
295 adc_count);
296
297 return adc_count;
298 }
299
300 for (i = 0; i < adc_count; i++) {
301 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
302 "#io-channel-cells", 0, i,
303 &adc_args)) {
304 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
305 __func__);
306 return 0;
307 }
308
309 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
310 &adc);
311
312 if (ret) {
313 pr_err("%s: Can't get adc device(%d)\n", __func__,
314 ret);
315 return ret;
316 }
317
318 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
319 &raw);
320 if (ret) {
321 pr_err("%s: single shot failed for %s[%d]!\n",
322 __func__, adc->name, adc_args.args[0]);
323 return ret;
324 }
325 /* Convert to uV */
326 if (!adc_raw_to_uV(adc, raw, &uV)) {
327 if (uV > max_uV)
328 max_uV = uV;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200329 if (uV < min_uV)
330 min_uV = uV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100331 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
332 adc->name, adc_args.args[0], raw, uV);
333 } else {
334 pr_err("%s: Can't get uV value for %s[%d]\n",
335 __func__, adc->name, adc_args.args[0]);
336 }
337 }
338
339 /*
340 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
341 * board is plugged on an USB-C 3A power supply and boot process can
342 * continue.
343 */
344 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
Patrice Chotard28c064e2019-04-30 18:09:38 +0200345 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
346 min_uV <= USB_LOW_THRESHOLD_UV)
Patrice Chotard395f1292019-02-12 16:50:40 +0100347 return 0;
348
Patrice Chotard28c064e2019-04-30 18:09:38 +0200349 pr_err("****************************************************\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100350
Patrice Chotard28c064e2019-04-30 18:09:38 +0200351 /*
352 * If highest and lowest value are either both below
353 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
354 * means USB TYPE-C is in unattached mode, this is an issue, make
355 * u-boot,error-led blinking and stop boot process.
356 */
357 if ((max_uV > USB_LOW_THRESHOLD_UV &&
358 min_uV > USB_LOW_THRESHOLD_UV) ||
359 (max_uV <= USB_LOW_THRESHOLD_UV &&
360 min_uV <= USB_LOW_THRESHOLD_UV)) {
361 pr_err("* ERROR USB TYPE-C connection in unattached mode *\n");
362 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
363 /* with 125ms interval, led will blink for 17.02 years ....*/
364 nb_blink = U32_MAX;
365 }
366
367 if (max_uV > USB_LOW_THRESHOLD_UV &&
368 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
369 min_uV <= USB_LOW_THRESHOLD_UV) {
370 pr_err("* WARNING 500mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100371 nb_blink = 2;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200372 }
373
374 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
375 max_uV <= USB_START_LOW_THRESHOLD_UV &&
376 min_uV <= USB_LOW_THRESHOLD_UV) {
377 pr_err("* WARNING 1.5mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100378 nb_blink = 3;
379 }
380
Patrice Chotard28c064e2019-04-30 18:09:38 +0200381 /*
382 * If highest value is above 2.15 Volts that means that the USB TypeC
383 * supplies more than 3 Amp, this is not compliant with TypeC specification
384 */
385 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
386 pr_err("* USB TYPE-C charger not compliant with *\n");
387 pr_err("* specification *\n");
388 pr_err("****************************************************\n\n");
389 /* with 125ms interval, led will blink for 17.02 years ....*/
390 nb_blink = U32_MAX;
391 } else {
392 pr_err("* Current too low, use a 3A power supply! *\n");
393 pr_err("****************************************************\n\n");
394 }
Patrice Chotard395f1292019-02-12 16:50:40 +0100395
396 ret = get_led(&led, "u-boot,error-led");
Patrice Chotard28c064e2019-04-30 18:09:38 +0200397 if (ret) {
398 /* in unattached case, the boot process must be stopped */
399 if (nb_blink == U32_MAX)
400 hang();
Patrice Chotard395f1292019-02-12 16:50:40 +0100401 return ret;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200402 }
Patrice Chotard395f1292019-02-12 16:50:40 +0100403
Patrice Chotard28c064e2019-04-30 18:09:38 +0200404 /* make u-boot,error-led blinking */
Patrice Chotard395f1292019-02-12 16:50:40 +0100405 for (i = 0; i < nb_blink * 2; i++) {
406 led_set_state(led, LEDST_TOGGLE);
407 mdelay(125);
408 }
409 led_set_state(led, LEDST_ON);
410
411 return 0;
412}
413
Patrick Delaunay45459742019-02-27 17:01:24 +0100414static void sysconf_init(void)
415{
416#ifndef CONFIG_STM32MP1_TRUSTED
417 u8 *syscfg;
418#ifdef CONFIG_DM_REGULATOR
419 struct udevice *pwr_dev;
420 struct udevice *pwr_reg;
421 struct udevice *dev;
422 int ret;
423 u32 otp = 0;
424#endif
425 u32 bootr;
426
427 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
428
429 /* interconnect update : select master using the port 1 */
430 /* LTDC = AXI_M9 */
431 /* GPU = AXI_M8 */
432 /* today information is hardcoded in U-Boot */
433 writel(BIT(9), syscfg + SYSCFG_ICNR);
434
435 /* disable Pull-Down for boot pin connected to VDD */
436 bootr = readl(syscfg + SYSCFG_BOOTR);
437 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
438 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
439 writel(bootr, syscfg + SYSCFG_BOOTR);
440
441#ifdef CONFIG_DM_REGULATOR
442 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
443 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
444 * The customer will have to disable this for low frequencies
445 * or if AFMUX is selected but the function not used, typically for
446 * TRACE. Otherwise, impact on power consumption.
447 *
448 * WARNING:
449 * enabling High Speed mode while VDD>2.7V
450 * with the OTP product_below_2v5 (OTP 18, BIT 13)
451 * erroneously set to 1 can damage the IC!
452 * => U-Boot set the register only if VDD < 2.7V (in DT)
453 * but this value need to be consistent with board design
454 */
455 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
456 if (!ret) {
457 ret = uclass_get_device_by_driver(UCLASS_MISC,
458 DM_GET_DRIVER(stm32mp_bsec),
459 &dev);
460 if (ret) {
461 pr_err("Can't find stm32mp_bsec driver\n");
462 return;
463 }
464
465 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
466 if (!ret)
467 otp = otp & BIT(13);
468
469 /* get VDD = pwr-supply */
470 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
471 &pwr_reg);
472
473 /* check if VDD is Low Voltage */
474 if (!ret) {
475 if (regulator_get_value(pwr_reg) < 2700000) {
476 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
477 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
478 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
479 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
480 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
481 syscfg + SYSCFG_IOCTRLSETR);
482
483 if (!otp)
484 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
485 } else {
486 if (otp)
487 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
488 }
489 } else {
490 debug("VDD unknown");
491 }
492 }
493#endif
494
495 /* activate automatic I/O compensation
496 * warning: need to ensure CSI enabled and ready in clock driver
497 */
498 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
499
500 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
501 ;
502 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
503#endif
504}
505
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100506/* board dependent setup after realloc */
507int board_init(void)
508{
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100509 struct udevice *dev;
510
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100511 /* address of boot parameters */
512 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
513
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100514 /* probe all PINCTRL for hog */
515 for (uclass_first_device(UCLASS_PINCTRL, &dev);
516 dev;
517 uclass_next_device(&dev)) {
518 pr_debug("probe pincontrol = %s\n", dev->name);
519 }
520
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100521 board_key_check();
522
Patrick Delaunayf59ad452019-07-05 17:20:09 +0200523#ifdef CONFIG_DM_REGULATOR
524 regulators_enable_boot_on(_DEBUG);
525#endif
526
Patrick Delaunay45459742019-02-27 17:01:24 +0100527 sysconf_init();
528
Patrick Delaunay1f5118b2018-07-27 16:37:08 +0200529 if (IS_ENABLED(CONFIG_LED))
530 led_default_state();
531
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100532 return 0;
533}
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100534
535int board_late_init(void)
536{
537#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
538 const void *fdt_compat;
539 int fdt_compat_len;
540
541 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
542 &fdt_compat_len);
543 if (fdt_compat && fdt_compat_len) {
544 if (strncmp(fdt_compat, "st,", 3) != 0)
545 env_set("board_name", fdt_compat);
546 else
547 env_set("board_name", fdt_compat + 3);
548 }
549#endif
550
Patrice Chotard395f1292019-02-12 16:50:40 +0100551 /* for DK1/DK2 boards */
552 board_check_usb_power();
553
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100554 return 0;
555}
Patrice Chotard395f1292019-02-12 16:50:40 +0100556
557void board_quiesce_devices(void)
558{
559 setup_led(LEDST_OFF);
560}
Patrice Chotard87471642019-05-02 18:07:14 +0200561
Christophe Roullieredacf262019-05-17 15:08:43 +0200562/* board interface eth init */
563/* this is a weak define that we are overriding */
564int board_interface_eth_init(phy_interface_t interface_type,
565 bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
566{
567 u8 *syscfg;
568 u32 value;
569
570 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
571
572 if (!syscfg)
573 return -ENODEV;
574
575 switch (interface_type) {
576 case PHY_INTERFACE_MODE_MII:
577 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
578 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
579 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
580 break;
581 case PHY_INTERFACE_MODE_GMII:
582 if (eth_clk_sel_reg)
583 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
584 SYSCFG_PMCSETR_ETH_CLK_SEL;
585 else
586 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
587 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
588 break;
589 case PHY_INTERFACE_MODE_RMII:
590 if (eth_ref_clk_sel_reg)
591 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
592 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
593 else
594 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
595 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
596 break;
597 case PHY_INTERFACE_MODE_RGMII:
598 case PHY_INTERFACE_MODE_RGMII_ID:
599 case PHY_INTERFACE_MODE_RGMII_RXID:
600 case PHY_INTERFACE_MODE_RGMII_TXID:
601 if (eth_clk_sel_reg)
602 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
603 SYSCFG_PMCSETR_ETH_CLK_SEL;
604 else
605 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
606 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
607 break;
608 default:
609 debug("%s: Do not manage %d interface\n",
610 __func__, interface_type);
611 /* Do not manage others interfaces */
612 return -EINVAL;
613 }
614
615 /* clear and set ETH configuration bits */
616 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
617 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
618 syscfg + SYSCFG_PMCCLRR);
619 writel(value, syscfg + SYSCFG_PMCSETR);
620
621 return 0;
622}
623
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200624enum env_location env_get_location(enum env_operation op, int prio)
625{
626 u32 bootmode = get_bootmode();
627
628 if (prio)
629 return ENVL_UNKNOWN;
630
631 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
632#ifdef CONFIG_ENV_IS_IN_EXT4
633 case BOOT_FLASH_SD:
634 case BOOT_FLASH_EMMC:
635 return ENVL_EXT4;
636#endif
637#ifdef CONFIG_ENV_IS_IN_UBI
638 case BOOT_FLASH_NAND:
639 return ENVL_UBI;
640#endif
Patrice Chotarde5c38fd2019-05-09 14:25:36 +0200641#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
642 case BOOT_FLASH_NOR:
643 return ENVL_SPI_FLASH;
644#endif
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200645 default:
646 return ENVL_NOWHERE;
647 }
648}
649
Patrice Chotard7f90cd62019-05-02 18:36:01 +0200650#if defined(CONFIG_ENV_IS_IN_EXT4)
651const char *env_ext4_get_intf(void)
652{
653 u32 bootmode = get_bootmode();
654
655 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
656 case BOOT_FLASH_SD:
657 case BOOT_FLASH_EMMC:
658 return "mmc";
659 default:
660 return "";
661 }
662}
663
664const char *env_ext4_get_dev_part(void)
665{
666 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
667 u32 bootmode = get_bootmode();
668
669 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
670}
671#endif
672
Patrice Chotard87471642019-05-02 18:07:14 +0200673#ifdef CONFIG_SYS_MTDPARTS_RUNTIME
674
675#define MTDPARTS_LEN 256
676#define MTDIDS_LEN 128
677
678/**
679 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
680 * If we need to access it before the env is relocated, then we need
681 * to use our own stack buffer. gd->env_buf will be too small.
682 *
683 * @param buf temporary buffer pointer MTDPARTS_LEN long
684 * @return mtdparts variable string, NULL if not found
685 */
686static const char *env_get_mtdparts(const char *str, char *buf)
687{
688 if (gd->flags & GD_FLG_ENV_READY)
689 return env_get(str);
690 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
691 return buf;
692
693 return NULL;
694}
695
696/**
697 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
698 */
699static void board_get_mtdparts(const char *dev,
700 char *mtdids,
701 char *mtdparts)
702{
703 char env_name[32] = "mtdparts_";
704 char tmp_mtdparts[MTDPARTS_LEN];
705 const char *tmp;
706
707 /* name of env variable to read = mtdparts_<dev> */
708 strcat(env_name, dev);
709 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
710 if (tmp) {
711 /* mtdids: "<dev>=<dev>, ...." */
712 if (mtdids[0] != '\0')
713 strcat(mtdids, ",");
714 strcat(mtdids, dev);
715 strcat(mtdids, "=");
716 strcat(mtdids, dev);
717
718 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
719 if (mtdparts[0] != '\0')
720 strncat(mtdparts, ";", MTDPARTS_LEN);
721 else
722 strcat(mtdparts, "mtdparts=");
723 strncat(mtdparts, dev, MTDPARTS_LEN);
724 strncat(mtdparts, ":", MTDPARTS_LEN);
725 strncat(mtdparts, tmp, MTDPARTS_LEN);
726 }
727}
728
729void board_mtdparts_default(const char **mtdids, const char **mtdparts)
730{
731 struct udevice *dev;
732 static char parts[2 * MTDPARTS_LEN + 1];
733 static char ids[MTDIDS_LEN + 1];
734 static bool mtd_initialized;
735
736 if (mtd_initialized) {
737 *mtdids = ids;
738 *mtdparts = parts;
739 return;
740 }
741
742 memset(parts, 0, sizeof(parts));
743 memset(ids, 0, sizeof(ids));
744
745 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
746 board_get_mtdparts("nand0", ids, parts);
747
748 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
749 board_get_mtdparts("nor0", ids, parts);
750
751 mtd_initialized = true;
752 *mtdids = ids;
753 *mtdparts = parts;
754 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
755}
756#endif
Patrick Delaunaye81f8d12019-07-02 13:26:07 +0200757
758#if defined(CONFIG_OF_BOARD_SETUP)
759int ft_board_setup(void *blob, bd_t *bd)
760{
761#ifdef CONFIG_FDT_FIXUP_PARTITIONS
762 struct node_info nodes[] = {
763 { "st,stm32f469-qspi", MTD_DEV_TYPE_NOR, },
764 { "st,stm32mp15-fmc2", MTD_DEV_TYPE_NAND, },
765 };
766 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
767#endif
768
769 return 0;
770}
771#endif