blob: 1be1160e0aaa758c34994f05069102ffead30314 [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 Delaunay8e194772019-06-21 15:26:40 +02007#include <bootm.h>
Patrick Delaunayf8598d92018-03-12 10:46:18 +01008#include <config.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +02009#include <clk.h>
10#include <dm.h>
Simon Glass3a7d5572019-08-01 09:46:42 -060011#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060012#include <env_internal.h>
Patrick Delaunayc31000c2019-03-29 15:42:23 +010013#include <g_dnl.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020014#include <generic-phy.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010015#include <i2c.h>
Patrick Delaunayd461f102019-02-12 11:44:41 +010016#include <led.h>
17#include <misc.h>
Patrick Delaunaye81f8d12019-07-02 13:26:07 +020018#include <mtd.h>
19#include <mtd_node.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020020#include <phy.h>
21#include <reset.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010022#include <syscon.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010023#include <usb.h>
Patrick Delaunaydd281082019-07-30 19:16:39 +020024#include <watchdog.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020025#include <asm/io.h>
Patrick Delaunay842ebb52019-02-27 17:01:18 +010026#include <asm/gpio.h>
Patrick Delaunay45459742019-02-27 17:01:24 +010027#include <asm/arch/stm32.h>
Patrice Chotard7f90cd62019-05-02 18:36:01 +020028#include <asm/arch/sys_proto.h>
Patrick Delaunaye81f8d12019-07-02 13:26:07 +020029#include <jffs2/load_kernel.h>
Patrice Chotard4c834b92018-08-10 17:12:14 +020030#include <power/regulator.h>
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +010031#include <usb/dwc2_udc.h>
Patrick Delaunayf8598d92018-03-12 10:46:18 +010032
Patrick Delaunay45459742019-02-27 17:01:24 +010033/* SYSCFG registers */
34#define SYSCFG_BOOTR 0x00
35#define SYSCFG_PMCSETR 0x04
36#define SYSCFG_IOCTRLSETR 0x18
37#define SYSCFG_ICNR 0x1C
38#define SYSCFG_CMPCR 0x20
39#define SYSCFG_CMPENSETR 0x24
40#define SYSCFG_PMCCLRR 0x44
41
42#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
43#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
44
45#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
46#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
47#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
48#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
49#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
50
51#define SYSCFG_CMPCR_SW_CTRL BIT(1)
52#define SYSCFG_CMPCR_READY BIT(8)
53
54#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
55
56#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
57#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
58
59#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
60
61#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Christophe Roullieredacf262019-05-17 15:08:43 +020062#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
63#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
64#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
Patrick Delaunay45459742019-02-27 17:01:24 +010065
Patrick Delaunayf8598d92018-03-12 10:46:18 +010066/*
67 * Get a global data pointer
68 */
69DECLARE_GLOBAL_DATA_PTR;
70
Patrice Chotard28c064e2019-04-30 18:09:38 +020071#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard395f1292019-02-12 16:50:40 +010072#define USB_WARNING_LOW_THRESHOLD_UV 660000
73#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotard28c064e2019-04-30 18:09:38 +020074#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard395f1292019-02-12 16:50:40 +010075
Patrick Delaunayd461f102019-02-12 11:44:41 +010076int checkboard(void)
77{
78 int ret;
79 char *mode;
80 u32 otp;
81 struct udevice *dev;
82 const char *fdt_compat;
83 int fdt_compat_len;
84
Patrick Delaunay152c84b2019-07-02 13:26:06 +020085 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
86 mode = "trusted with OP-TEE";
87 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
Patrick Delaunayd461f102019-02-12 11:44:41 +010088 mode = "trusted";
89 else
90 mode = "basic";
91
92 printf("Board: stm32mp1 in %s mode", mode);
93 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
94 &fdt_compat_len);
95 if (fdt_compat && fdt_compat_len)
96 printf(" (%s)", fdt_compat);
97 puts("\n");
98
99 ret = uclass_get_device_by_driver(UCLASS_MISC,
100 DM_GET_DRIVER(stm32mp_bsec),
101 &dev);
102
103 if (!ret)
104 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
105 &otp, sizeof(otp));
106 if (!ret && otp) {
107 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
108 otp >> 16,
109 (otp >> 12) & 0xF,
110 ((otp >> 8) & 0xF) - 1 + 'A',
111 otp & 0xF);
112 }
113
114 return 0;
115}
116
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100117static void board_key_check(void)
118{
119#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
120 ofnode node;
121 struct gpio_desc gpio;
122 enum forced_boot_mode boot_mode = BOOT_NORMAL;
123
124 node = ofnode_path("/config");
125 if (!ofnode_valid(node)) {
126 debug("%s: no /config node?\n", __func__);
127 return;
128 }
129#ifdef CONFIG_FASTBOOT
130 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
131 &gpio, GPIOD_IS_IN)) {
132 debug("%s: could not find a /config/st,fastboot-gpios\n",
133 __func__);
134 } else {
135 if (dm_gpio_get_value(&gpio)) {
136 puts("Fastboot key pressed, ");
137 boot_mode = BOOT_FASTBOOT;
138 }
139
140 dm_gpio_free(NULL, &gpio);
141 }
142#endif
143#ifdef CONFIG_CMD_STM32PROG
144 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
145 &gpio, GPIOD_IS_IN)) {
146 debug("%s: could not find a /config/st,stm32prog-gpios\n",
147 __func__);
148 } else {
149 if (dm_gpio_get_value(&gpio)) {
150 puts("STM32Programmer key pressed, ");
151 boot_mode = BOOT_STM32PROG;
152 }
153 dm_gpio_free(NULL, &gpio);
154 }
155#endif
156
157 if (boot_mode != BOOT_NORMAL) {
158 puts("entering download mode...\n");
159 clrsetbits_le32(TAMP_BOOT_CONTEXT,
160 TAMP_BOOT_FORCED_MASK,
161 boot_mode);
162 }
163#endif
164}
165
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100166#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200167
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100168/* STMicroelectronics STUSB1600 Type-C controller */
169#define STUSB1600_CC_CONNECTION_STATUS 0x0E
170
171/* STUSB1600_CC_CONNECTION_STATUS bitfields */
172#define STUSB1600_CC_ATTACH BIT(0)
173
174static int stusb1600_init(struct udevice **dev_stusb1600)
175{
176 ofnode node;
177 struct udevice *dev, *bus;
178 int ret;
179 u32 chip_addr;
180
181 *dev_stusb1600 = NULL;
182
183 /* if node stusb1600 is present, means DK1 or DK2 board */
184 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
185 if (!ofnode_valid(node))
186 return -ENODEV;
187
188 ret = ofnode_read_u32(node, "reg", &chip_addr);
189 if (ret)
190 return -EINVAL;
191
192 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
193 &bus);
194 if (ret) {
195 printf("bus for stusb1600 not found\n");
196 return -ENODEV;
197 }
198
199 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
200 if (!ret)
201 *dev_stusb1600 = dev;
202
203 return ret;
204}
205
206static int stusb1600_cable_connected(struct udevice *dev)
207{
208 u8 status;
209
210 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
211 return 0;
212
213 return status & STUSB1600_CC_ATTACH;
214}
215
216#include <usb/dwc2_udc.h>
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100217int g_dnl_board_usb_cable_connected(void)
Patrice Chotard4c834b92018-08-10 17:12:14 +0200218{
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100219 struct udevice *stusb1600;
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100220 struct udevice *dwc2_udc_otg;
Patrice Chotard4c834b92018-08-10 17:12:14 +0200221 int ret;
222
Patrick Delaunay6fe7dd32019-03-29 15:42:24 +0100223 if (!stusb1600_init(&stusb1600))
224 return stusb1600_cable_connected(stusb1600);
225
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100226 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
227 DM_GET_DRIVER(dwc2_udc_otg),
228 &dwc2_udc_otg);
229 if (!ret)
230 debug("dwc2_udc_otg init failed\n");
Patrice Chotard4c834b92018-08-10 17:12:14 +0200231
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100232 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard4c834b92018-08-10 17:12:14 +0200233}
Patrick Delaunayc31000c2019-03-29 15:42:23 +0100234#endif /* CONFIG_USB_GADGET */
Patrice Chotard4c834b92018-08-10 17:12:14 +0200235
Patrick Delaunaydd281082019-07-30 19:16:39 +0200236#ifdef CONFIG_LED
Patrice Chotard395f1292019-02-12 16:50:40 +0100237static int get_led(struct udevice **dev, char *led_string)
238{
239 char *led_name;
240 int ret;
241
242 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
243 if (!led_name) {
244 pr_debug("%s: could not find %s config string\n",
245 __func__, led_string);
246 return -ENOENT;
247 }
248 ret = led_get_by_label(led_name, dev);
249 if (ret) {
250 debug("%s: get=%d\n", __func__, ret);
251 return ret;
252 }
253
254 return 0;
255}
256
257static int setup_led(enum led_state_t cmd)
258{
259 struct udevice *dev;
260 int ret;
261
262 ret = get_led(&dev, "u-boot,boot-led");
263 if (ret)
264 return ret;
265
266 ret = led_set_state(dev, cmd);
267 return ret;
268}
Patrick Delaunaydd281082019-07-30 19:16:39 +0200269#endif
270
271static void __maybe_unused led_error_blink(u32 nb_blink)
272{
273#ifdef CONFIG_LED
274 int ret;
275 struct udevice *led;
276 u32 i;
277#endif
278
279 if (!nb_blink)
280 return;
281
282#ifdef CONFIG_LED
283 ret = get_led(&led, "u-boot,error-led");
284 if (!ret) {
285 /* make u-boot,error-led blinking */
286 /* if U32_MAX and 125ms interval, for 17.02 years */
287 for (i = 0; i < 2 * nb_blink; i++) {
288 led_set_state(led, LEDST_TOGGLE);
289 mdelay(125);
290 WATCHDOG_RESET();
291 }
292 }
293#endif
294
295 /* infinite: the boot process must be stopped */
296 if (nb_blink == U32_MAX)
297 hang();
298}
Patrice Chotard395f1292019-02-12 16:50:40 +0100299
300static int board_check_usb_power(void)
301{
302 struct ofnode_phandle_args adc_args;
303 struct udevice *adc;
Patrice Chotard395f1292019-02-12 16:50:40 +0100304 ofnode node;
305 unsigned int raw;
306 int max_uV = 0;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200307 int min_uV = USB_START_HIGH_THRESHOLD_UV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100308 int ret, uV, adc_count;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200309 u32 nb_blink;
310 u8 i;
Patrice Chotard395f1292019-02-12 16:50:40 +0100311 node = ofnode_path("/config");
312 if (!ofnode_valid(node)) {
313 debug("%s: no /config node?\n", __func__);
314 return -ENOENT;
315 }
316
317 /*
318 * Retrieve the ADC channels devices and get measurement
319 * for each of them
320 */
321 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
322 "#io-channel-cells");
323 if (adc_count < 0) {
324 if (adc_count == -ENOENT)
325 return 0;
326
327 pr_err("%s: can't find adc channel (%d)\n", __func__,
328 adc_count);
329
330 return adc_count;
331 }
332
333 for (i = 0; i < adc_count; i++) {
334 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
335 "#io-channel-cells", 0, i,
336 &adc_args)) {
337 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
338 __func__);
339 return 0;
340 }
341
342 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
343 &adc);
344
345 if (ret) {
346 pr_err("%s: Can't get adc device(%d)\n", __func__,
347 ret);
348 return ret;
349 }
350
351 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
352 &raw);
353 if (ret) {
354 pr_err("%s: single shot failed for %s[%d]!\n",
355 __func__, adc->name, adc_args.args[0]);
356 return ret;
357 }
358 /* Convert to uV */
359 if (!adc_raw_to_uV(adc, raw, &uV)) {
360 if (uV > max_uV)
361 max_uV = uV;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200362 if (uV < min_uV)
363 min_uV = uV;
Patrice Chotard395f1292019-02-12 16:50:40 +0100364 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
365 adc->name, adc_args.args[0], raw, uV);
366 } else {
367 pr_err("%s: Can't get uV value for %s[%d]\n",
368 __func__, adc->name, adc_args.args[0]);
369 }
370 }
371
372 /*
373 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
374 * board is plugged on an USB-C 3A power supply and boot process can
375 * continue.
376 */
377 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
Patrice Chotard28c064e2019-04-30 18:09:38 +0200378 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
379 min_uV <= USB_LOW_THRESHOLD_UV)
Patrice Chotard395f1292019-02-12 16:50:40 +0100380 return 0;
381
Patrice Chotard28c064e2019-04-30 18:09:38 +0200382 pr_err("****************************************************\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100383
Patrice Chotard28c064e2019-04-30 18:09:38 +0200384 /*
385 * If highest and lowest value are either both below
386 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
387 * means USB TYPE-C is in unattached mode, this is an issue, make
388 * u-boot,error-led blinking and stop boot process.
389 */
390 if ((max_uV > USB_LOW_THRESHOLD_UV &&
391 min_uV > USB_LOW_THRESHOLD_UV) ||
392 (max_uV <= USB_LOW_THRESHOLD_UV &&
393 min_uV <= USB_LOW_THRESHOLD_UV)) {
394 pr_err("* ERROR USB TYPE-C connection in unattached mode *\n");
395 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
396 /* with 125ms interval, led will blink for 17.02 years ....*/
397 nb_blink = U32_MAX;
398 }
399
400 if (max_uV > USB_LOW_THRESHOLD_UV &&
401 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
402 min_uV <= USB_LOW_THRESHOLD_UV) {
403 pr_err("* WARNING 500mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100404 nb_blink = 2;
Patrice Chotard28c064e2019-04-30 18:09:38 +0200405 }
406
407 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
408 max_uV <= USB_START_LOW_THRESHOLD_UV &&
409 min_uV <= USB_LOW_THRESHOLD_UV) {
410 pr_err("* WARNING 1.5mA power supply detected *\n");
Patrice Chotard395f1292019-02-12 16:50:40 +0100411 nb_blink = 3;
412 }
413
Patrice Chotard28c064e2019-04-30 18:09:38 +0200414 /*
415 * If highest value is above 2.15 Volts that means that the USB TypeC
416 * supplies more than 3 Amp, this is not compliant with TypeC specification
417 */
418 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
419 pr_err("* USB TYPE-C charger not compliant with *\n");
420 pr_err("* specification *\n");
421 pr_err("****************************************************\n\n");
422 /* with 125ms interval, led will blink for 17.02 years ....*/
423 nb_blink = U32_MAX;
424 } else {
425 pr_err("* Current too low, use a 3A power supply! *\n");
426 pr_err("****************************************************\n\n");
427 }
Patrice Chotard395f1292019-02-12 16:50:40 +0100428
Patrick Delaunaydd281082019-07-30 19:16:39 +0200429 led_error_blink(nb_blink);
Patrice Chotard395f1292019-02-12 16:50:40 +0100430
431 return 0;
432}
433
Patrick Delaunay45459742019-02-27 17:01:24 +0100434static void sysconf_init(void)
435{
436#ifndef CONFIG_STM32MP1_TRUSTED
437 u8 *syscfg;
438#ifdef CONFIG_DM_REGULATOR
439 struct udevice *pwr_dev;
440 struct udevice *pwr_reg;
441 struct udevice *dev;
442 int ret;
443 u32 otp = 0;
444#endif
445 u32 bootr;
446
447 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
448
449 /* interconnect update : select master using the port 1 */
450 /* LTDC = AXI_M9 */
451 /* GPU = AXI_M8 */
452 /* today information is hardcoded in U-Boot */
453 writel(BIT(9), syscfg + SYSCFG_ICNR);
454
455 /* disable Pull-Down for boot pin connected to VDD */
456 bootr = readl(syscfg + SYSCFG_BOOTR);
457 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
458 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
459 writel(bootr, syscfg + SYSCFG_BOOTR);
460
461#ifdef CONFIG_DM_REGULATOR
462 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
463 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
464 * The customer will have to disable this for low frequencies
465 * or if AFMUX is selected but the function not used, typically for
466 * TRACE. Otherwise, impact on power consumption.
467 *
468 * WARNING:
469 * enabling High Speed mode while VDD>2.7V
470 * with the OTP product_below_2v5 (OTP 18, BIT 13)
471 * erroneously set to 1 can damage the IC!
472 * => U-Boot set the register only if VDD < 2.7V (in DT)
473 * but this value need to be consistent with board design
474 */
475 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
476 if (!ret) {
477 ret = uclass_get_device_by_driver(UCLASS_MISC,
478 DM_GET_DRIVER(stm32mp_bsec),
479 &dev);
480 if (ret) {
481 pr_err("Can't find stm32mp_bsec driver\n");
482 return;
483 }
484
485 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
486 if (!ret)
487 otp = otp & BIT(13);
488
489 /* get VDD = pwr-supply */
490 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
491 &pwr_reg);
492
493 /* check if VDD is Low Voltage */
494 if (!ret) {
495 if (regulator_get_value(pwr_reg) < 2700000) {
496 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
497 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
498 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
499 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
500 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
501 syscfg + SYSCFG_IOCTRLSETR);
502
503 if (!otp)
504 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
505 } else {
506 if (otp)
507 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
508 }
509 } else {
510 debug("VDD unknown");
511 }
512 }
513#endif
514
515 /* activate automatic I/O compensation
516 * warning: need to ensure CSI enabled and ready in clock driver
517 */
518 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
519
520 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
521 ;
522 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
523#endif
524}
525
Patrick Delaunayd573e462019-07-30 19:16:38 +0200526#ifdef CONFIG_DM_REGULATOR
527/* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
528static int dk2_i2c1_fix(void)
529{
530 ofnode node;
531 struct gpio_desc hdmi, audio;
532 int ret = 0;
533
534 node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
535 if (!ofnode_valid(node)) {
536 pr_debug("%s: no hdmi-transmitter@39 ?\n", __func__);
537 return -ENOENT;
538 }
539
540 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
541 &hdmi, GPIOD_IS_OUT)) {
542 pr_debug("%s: could not find reset-gpios\n",
543 __func__);
544 return -ENOENT;
545 }
546
547 node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
548 if (!ofnode_valid(node)) {
549 pr_debug("%s: no cs42l51@4a ?\n", __func__);
550 return -ENOENT;
551 }
552
553 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
554 &audio, GPIOD_IS_OUT)) {
555 pr_debug("%s: could not find reset-gpios\n",
556 __func__);
557 return -ENOENT;
558 }
559
560 /* before power up, insure that HDMI and AUDIO IC is under reset */
561 ret = dm_gpio_set_value(&hdmi, 1);
562 if (ret) {
563 pr_err("%s: can't set_value for hdmi_nrst gpio", __func__);
564 goto error;
565 }
566 ret = dm_gpio_set_value(&audio, 1);
567 if (ret) {
568 pr_err("%s: can't set_value for audio_nrst gpio", __func__);
569 goto error;
570 }
571
572 /* power-up audio IC */
573 regulator_autoset_by_name("v1v8_audio", NULL);
574
575 /* power-up HDMI IC */
576 regulator_autoset_by_name("v1v2_hdmi", NULL);
577 regulator_autoset_by_name("v3v3_hdmi", NULL);
578
579error:
580 return ret;
581}
582
583static bool board_is_dk2(void)
584{
585 if (CONFIG_IS_ENABLED(TARGET_STM32MP157C_DK2) &&
586 of_machine_is_compatible("st,stm32mp157c-dk2"))
587 return true;
588
589 return false;
590}
591#endif
592
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100593/* board dependent setup after realloc */
594int board_init(void)
595{
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100596 struct udevice *dev;
597
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100598 /* address of boot parameters */
599 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
600
Patrice Chotard8b4afe82019-03-11 11:13:17 +0100601 /* probe all PINCTRL for hog */
602 for (uclass_first_device(UCLASS_PINCTRL, &dev);
603 dev;
604 uclass_next_device(&dev)) {
605 pr_debug("probe pincontrol = %s\n", dev->name);
606 }
607
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100608 board_key_check();
609
Patrick Delaunayf59ad452019-07-05 17:20:09 +0200610#ifdef CONFIG_DM_REGULATOR
Patrick Delaunayd573e462019-07-30 19:16:38 +0200611 if (board_is_dk2())
612 dk2_i2c1_fix();
613
Patrick Delaunayf59ad452019-07-05 17:20:09 +0200614 regulators_enable_boot_on(_DEBUG);
615#endif
616
Patrick Delaunay45459742019-02-27 17:01:24 +0100617 sysconf_init();
618
Patrick Delaunay4616ff42019-07-30 19:16:40 +0200619 if (CONFIG_IS_ENABLED(CONFIG_LED))
Patrick Delaunay1f5118b2018-07-27 16:37:08 +0200620 led_default_state();
621
Patrick Delaunayf8598d92018-03-12 10:46:18 +0100622 return 0;
623}
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100624
625int board_late_init(void)
626{
Patrick Delaunay55f9cd22019-07-30 19:16:41 +0200627 char *boot_device;
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100628#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
629 const void *fdt_compat;
630 int fdt_compat_len;
Patrick Delaunay8b8b3d62019-07-30 19:16:37 +0200631 int ret;
632 u32 otp;
633 struct udevice *dev;
634 char buf[10];
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100635
636 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
637 &fdt_compat_len);
638 if (fdt_compat && fdt_compat_len) {
639 if (strncmp(fdt_compat, "st,", 3) != 0)
640 env_set("board_name", fdt_compat);
641 else
642 env_set("board_name", fdt_compat + 3);
643 }
Patrick Delaunay8b8b3d62019-07-30 19:16:37 +0200644 ret = uclass_get_device_by_driver(UCLASS_MISC,
645 DM_GET_DRIVER(stm32mp_bsec),
646 &dev);
647
648 if (!ret)
649 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
650 &otp, sizeof(otp));
651 if (!ret && otp) {
652 snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
653 env_set("board_id", buf);
654
655 snprintf(buf, sizeof(buf), "0x%04x",
656 ((otp >> 8) & 0xF) - 1 + 0xA);
657 env_set("board_rev", buf);
658 }
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100659#endif
660
Patrice Chotard395f1292019-02-12 16:50:40 +0100661 /* for DK1/DK2 boards */
662 board_check_usb_power();
663
Patrick Delaunay55f9cd22019-07-30 19:16:41 +0200664 /* Check the boot-source to disable bootdelay */
665 boot_device = env_get("boot_device");
666 if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
667 env_set("bootdelay", "0");
668
Patrick Delaunayb4ae34b2019-02-27 17:01:11 +0100669 return 0;
670}
Patrice Chotard395f1292019-02-12 16:50:40 +0100671
672void board_quiesce_devices(void)
673{
Patrick Delaunay4616ff42019-07-30 19:16:40 +0200674#ifdef CONFIG_LED
Patrice Chotard395f1292019-02-12 16:50:40 +0100675 setup_led(LEDST_OFF);
Patrick Delaunay4616ff42019-07-30 19:16:40 +0200676#endif
Patrice Chotard395f1292019-02-12 16:50:40 +0100677}
Patrice Chotard87471642019-05-02 18:07:14 +0200678
Christophe Roullieredacf262019-05-17 15:08:43 +0200679/* board interface eth init */
680/* this is a weak define that we are overriding */
681int board_interface_eth_init(phy_interface_t interface_type,
682 bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
683{
684 u8 *syscfg;
685 u32 value;
686
687 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
688
689 if (!syscfg)
690 return -ENODEV;
691
692 switch (interface_type) {
693 case PHY_INTERFACE_MODE_MII:
694 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
695 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
696 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
697 break;
698 case PHY_INTERFACE_MODE_GMII:
699 if (eth_clk_sel_reg)
700 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
701 SYSCFG_PMCSETR_ETH_CLK_SEL;
702 else
703 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
704 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
705 break;
706 case PHY_INTERFACE_MODE_RMII:
707 if (eth_ref_clk_sel_reg)
708 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
709 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
710 else
711 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
712 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
713 break;
714 case PHY_INTERFACE_MODE_RGMII:
715 case PHY_INTERFACE_MODE_RGMII_ID:
716 case PHY_INTERFACE_MODE_RGMII_RXID:
717 case PHY_INTERFACE_MODE_RGMII_TXID:
718 if (eth_clk_sel_reg)
719 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
720 SYSCFG_PMCSETR_ETH_CLK_SEL;
721 else
722 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
723 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
724 break;
725 default:
726 debug("%s: Do not manage %d interface\n",
727 __func__, interface_type);
728 /* Do not manage others interfaces */
729 return -EINVAL;
730 }
731
732 /* clear and set ETH configuration bits */
733 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
734 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
735 syscfg + SYSCFG_PMCCLRR);
736 writel(value, syscfg + SYSCFG_PMCSETR);
737
738 return 0;
739}
740
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200741enum env_location env_get_location(enum env_operation op, int prio)
742{
743 u32 bootmode = get_bootmode();
744
745 if (prio)
746 return ENVL_UNKNOWN;
747
748 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
749#ifdef CONFIG_ENV_IS_IN_EXT4
750 case BOOT_FLASH_SD:
751 case BOOT_FLASH_EMMC:
752 return ENVL_EXT4;
753#endif
754#ifdef CONFIG_ENV_IS_IN_UBI
755 case BOOT_FLASH_NAND:
756 return ENVL_UBI;
757#endif
Patrice Chotarde5c38fd2019-05-09 14:25:36 +0200758#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
759 case BOOT_FLASH_NOR:
760 return ENVL_SPI_FLASH;
761#endif
Patrice Chotard8f24b1a2019-05-02 18:28:05 +0200762 default:
763 return ENVL_NOWHERE;
764 }
765}
766
Patrice Chotard7f90cd62019-05-02 18:36:01 +0200767#if defined(CONFIG_ENV_IS_IN_EXT4)
768const char *env_ext4_get_intf(void)
769{
770 u32 bootmode = get_bootmode();
771
772 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
773 case BOOT_FLASH_SD:
774 case BOOT_FLASH_EMMC:
775 return "mmc";
776 default:
777 return "";
778 }
779}
780
781const char *env_ext4_get_dev_part(void)
782{
783 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
784 u32 bootmode = get_bootmode();
785
786 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
787}
788#endif
789
Patrice Chotard87471642019-05-02 18:07:14 +0200790#ifdef CONFIG_SYS_MTDPARTS_RUNTIME
791
792#define MTDPARTS_LEN 256
793#define MTDIDS_LEN 128
794
795/**
796 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
797 * If we need to access it before the env is relocated, then we need
798 * to use our own stack buffer. gd->env_buf will be too small.
799 *
800 * @param buf temporary buffer pointer MTDPARTS_LEN long
801 * @return mtdparts variable string, NULL if not found
802 */
803static const char *env_get_mtdparts(const char *str, char *buf)
804{
805 if (gd->flags & GD_FLG_ENV_READY)
806 return env_get(str);
807 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
808 return buf;
809
810 return NULL;
811}
812
813/**
814 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
815 */
816static void board_get_mtdparts(const char *dev,
817 char *mtdids,
818 char *mtdparts)
819{
820 char env_name[32] = "mtdparts_";
821 char tmp_mtdparts[MTDPARTS_LEN];
822 const char *tmp;
823
824 /* name of env variable to read = mtdparts_<dev> */
825 strcat(env_name, dev);
826 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
827 if (tmp) {
828 /* mtdids: "<dev>=<dev>, ...." */
829 if (mtdids[0] != '\0')
830 strcat(mtdids, ",");
831 strcat(mtdids, dev);
832 strcat(mtdids, "=");
833 strcat(mtdids, dev);
834
835 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
836 if (mtdparts[0] != '\0')
837 strncat(mtdparts, ";", MTDPARTS_LEN);
838 else
839 strcat(mtdparts, "mtdparts=");
840 strncat(mtdparts, dev, MTDPARTS_LEN);
841 strncat(mtdparts, ":", MTDPARTS_LEN);
842 strncat(mtdparts, tmp, MTDPARTS_LEN);
843 }
844}
845
846void board_mtdparts_default(const char **mtdids, const char **mtdparts)
847{
848 struct udevice *dev;
849 static char parts[2 * MTDPARTS_LEN + 1];
850 static char ids[MTDIDS_LEN + 1];
851 static bool mtd_initialized;
852
853 if (mtd_initialized) {
854 *mtdids = ids;
855 *mtdparts = parts;
856 return;
857 }
858
859 memset(parts, 0, sizeof(parts));
860 memset(ids, 0, sizeof(ids));
861
862 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
863 board_get_mtdparts("nand0", ids, parts);
864
865 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
866 board_get_mtdparts("nor0", ids, parts);
867
868 mtd_initialized = true;
869 *mtdids = ids;
870 *mtdparts = parts;
871 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
872}
873#endif
Patrick Delaunaye81f8d12019-07-02 13:26:07 +0200874
875#if defined(CONFIG_OF_BOARD_SETUP)
876int ft_board_setup(void *blob, bd_t *bd)
877{
878#ifdef CONFIG_FDT_FIXUP_PARTITIONS
879 struct node_info nodes[] = {
880 { "st,stm32f469-qspi", MTD_DEV_TYPE_NOR, },
881 { "st,stm32mp15-fmc2", MTD_DEV_TYPE_NAND, },
882 };
883 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
884#endif
885
886 return 0;
887}
888#endif