d83081d9e2207d8218a894478afc6f6ee98225a7
[oweals/u-boot.git] / board / st / stm32mp1 / stm32mp1.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5 #include <common.h>
6 #include <adc.h>
7 #include <bootm.h>
8 #include <clk.h>
9 #include <config.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <g_dnl.h>
14 #include <generic-phy.h>
15 #include <hang.h>
16 #include <i2c.h>
17 #include <init.h>
18 #include <led.h>
19 #include <malloc.h>
20 #include <misc.h>
21 #include <mtd_node.h>
22 #include <netdev.h>
23 #include <phy.h>
24 #include <remoteproc.h>
25 #include <reset.h>
26 #include <syscon.h>
27 #include <usb.h>
28 #include <watchdog.h>
29 #include <asm/io.h>
30 #include <asm/gpio.h>
31 #include <asm/arch/stm32.h>
32 #include <asm/arch/sys_proto.h>
33 #include <jffs2/load_kernel.h>
34 #include <linux/err.h>
35 #include <power/regulator.h>
36 #include <usb/dwc2_udc.h>
37
38 /* SYSCFG registers */
39 #define SYSCFG_BOOTR            0x00
40 #define SYSCFG_PMCSETR          0x04
41 #define SYSCFG_IOCTRLSETR       0x18
42 #define SYSCFG_ICNR             0x1C
43 #define SYSCFG_CMPCR            0x20
44 #define SYSCFG_CMPENSETR        0x24
45 #define SYSCFG_PMCCLRR          0x44
46
47 #define SYSCFG_BOOTR_BOOT_MASK          GENMASK(2, 0)
48 #define SYSCFG_BOOTR_BOOTPD_SHIFT       4
49
50 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE          BIT(0)
51 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI        BIT(1)
52 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH            BIT(2)
53 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC          BIT(3)
54 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI            BIT(4)
55
56 #define SYSCFG_CMPCR_SW_CTRL            BIT(1)
57 #define SYSCFG_CMPCR_READY              BIT(8)
58
59 #define SYSCFG_CMPENSETR_MPU_EN         BIT(0)
60
61 #define SYSCFG_PMCSETR_ETH_CLK_SEL      BIT(16)
62 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL  BIT(17)
63
64 #define SYSCFG_PMCSETR_ETH_SELMII       BIT(20)
65
66 #define SYSCFG_PMCSETR_ETH_SEL_MASK     GENMASK(23, 21)
67 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
68 #define SYSCFG_PMCSETR_ETH_SEL_RGMII    BIT(21)
69 #define SYSCFG_PMCSETR_ETH_SEL_RMII     BIT(23)
70
71 /*
72  * Get a global data pointer
73  */
74 DECLARE_GLOBAL_DATA_PTR;
75
76 #define USB_LOW_THRESHOLD_UV            200000
77 #define USB_WARNING_LOW_THRESHOLD_UV    660000
78 #define USB_START_LOW_THRESHOLD_UV      1230000
79 #define USB_START_HIGH_THRESHOLD_UV     2150000
80
81 int checkboard(void)
82 {
83         int ret;
84         char *mode;
85         u32 otp;
86         struct udevice *dev;
87         const char *fdt_compat;
88         int fdt_compat_len;
89
90         if (IS_ENABLED(CONFIG_TFABOOT))
91                 mode = "trusted";
92         else
93                 mode = "basic";
94
95         printf("Board: stm32mp1 in %s mode", mode);
96         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
97                                  &fdt_compat_len);
98         if (fdt_compat && fdt_compat_len)
99                 printf(" (%s)", fdt_compat);
100         puts("\n");
101
102         /* display the STMicroelectronics board identification */
103         if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
104                 ret = uclass_get_device_by_driver(UCLASS_MISC,
105                                                   DM_GET_DRIVER(stm32mp_bsec),
106                                                   &dev);
107                 if (!ret)
108                         ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
109                                         &otp, sizeof(otp));
110                 if (ret > 0 && otp)
111                         printf("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
112                                otp >> 16,
113                                (otp >> 12) & 0xF,
114                                (otp >> 4) & 0xF,
115                                ((otp >> 8) & 0xF) - 1 + 'A',
116                                otp & 0xF);
117         }
118
119         return 0;
120 }
121
122 static void board_key_check(void)
123 {
124 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
125         ofnode node;
126         struct gpio_desc gpio;
127         enum forced_boot_mode boot_mode = BOOT_NORMAL;
128
129         node = ofnode_path("/config");
130         if (!ofnode_valid(node)) {
131                 debug("%s: no /config node?\n", __func__);
132                 return;
133         }
134 #ifdef CONFIG_FASTBOOT
135         if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
136                                        &gpio, GPIOD_IS_IN)) {
137                 debug("%s: could not find a /config/st,fastboot-gpios\n",
138                       __func__);
139         } else {
140                 if (dm_gpio_get_value(&gpio)) {
141                         puts("Fastboot key pressed, ");
142                         boot_mode = BOOT_FASTBOOT;
143                 }
144
145                 dm_gpio_free(NULL, &gpio);
146         }
147 #endif
148 #ifdef CONFIG_CMD_STM32PROG
149         if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
150                                        &gpio, GPIOD_IS_IN)) {
151                 debug("%s: could not find a /config/st,stm32prog-gpios\n",
152                       __func__);
153         } else {
154                 if (dm_gpio_get_value(&gpio)) {
155                         puts("STM32Programmer key pressed, ");
156                         boot_mode = BOOT_STM32PROG;
157                 }
158                 dm_gpio_free(NULL, &gpio);
159         }
160 #endif
161
162         if (boot_mode != BOOT_NORMAL) {
163                 puts("entering download mode...\n");
164                 clrsetbits_le32(TAMP_BOOT_CONTEXT,
165                                 TAMP_BOOT_FORCED_MASK,
166                                 boot_mode);
167         }
168 #endif
169 }
170
171 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
172
173 /* STMicroelectronics STUSB1600 Type-C controller */
174 #define STUSB1600_CC_CONNECTION_STATUS          0x0E
175
176 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
177 #define STUSB1600_CC_ATTACH                     BIT(0)
178
179 static int stusb1600_init(struct udevice **dev_stusb1600)
180 {
181         ofnode node;
182         struct udevice *dev, *bus;
183         int ret;
184         u32 chip_addr;
185
186         *dev_stusb1600 = NULL;
187
188         /* if node stusb1600 is present, means DK1 or DK2 board */
189         node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
190         if (!ofnode_valid(node))
191                 return -ENODEV;
192
193         ret = ofnode_read_u32(node, "reg", &chip_addr);
194         if (ret)
195                 return -EINVAL;
196
197         ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
198                                           &bus);
199         if (ret) {
200                 printf("bus for stusb1600 not found\n");
201                 return -ENODEV;
202         }
203
204         ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
205         if (!ret)
206                 *dev_stusb1600 = dev;
207
208         return ret;
209 }
210
211 static int stusb1600_cable_connected(struct udevice *dev)
212 {
213         u8 status;
214
215         if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
216                 return 0;
217
218         return status & STUSB1600_CC_ATTACH;
219 }
220
221 #include <usb/dwc2_udc.h>
222 int g_dnl_board_usb_cable_connected(void)
223 {
224         struct udevice *stusb1600;
225         struct udevice *dwc2_udc_otg;
226         int ret;
227
228         if (!stusb1600_init(&stusb1600))
229                 return stusb1600_cable_connected(stusb1600);
230
231         ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
232                                           DM_GET_DRIVER(dwc2_udc_otg),
233                                           &dwc2_udc_otg);
234         if (!ret)
235                 debug("dwc2_udc_otg init failed\n");
236
237         return dwc2_udc_B_session_valid(dwc2_udc_otg);
238 }
239
240 #define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
241 #define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
242
243 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
244 {
245         if (!strcmp(name, "usb_dnl_dfu"))
246                 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
247         else if (!strcmp(name, "usb_dnl_fastboot"))
248                 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
249                               &dev->idProduct);
250         else
251                 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
252
253         return 0;
254 }
255
256 #endif /* CONFIG_USB_GADGET */
257
258 #ifdef CONFIG_LED
259 static int get_led(struct udevice **dev, char *led_string)
260 {
261         char *led_name;
262         int ret;
263
264         led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
265         if (!led_name) {
266                 pr_debug("%s: could not find %s config string\n",
267                          __func__, led_string);
268                 return -ENOENT;
269         }
270         ret = led_get_by_label(led_name, dev);
271         if (ret) {
272                 debug("%s: get=%d\n", __func__, ret);
273                 return ret;
274         }
275
276         return 0;
277 }
278
279 static int setup_led(enum led_state_t cmd)
280 {
281         struct udevice *dev;
282         int ret;
283
284         ret = get_led(&dev, "u-boot,boot-led");
285         if (ret)
286                 return ret;
287
288         ret = led_set_state(dev, cmd);
289         return ret;
290 }
291 #endif
292
293 static void __maybe_unused led_error_blink(u32 nb_blink)
294 {
295 #ifdef CONFIG_LED
296         int ret;
297         struct udevice *led;
298         u32 i;
299 #endif
300
301         if (!nb_blink)
302                 return;
303
304 #ifdef CONFIG_LED
305         ret = get_led(&led, "u-boot,error-led");
306         if (!ret) {
307                 /* make u-boot,error-led blinking */
308                 /* if U32_MAX and 125ms interval, for 17.02 years */
309                 for (i = 0; i < 2 * nb_blink; i++) {
310                         led_set_state(led, LEDST_TOGGLE);
311                         mdelay(125);
312                         WATCHDOG_RESET();
313                 }
314                 led_set_state(led, LEDST_ON);
315         }
316 #endif
317
318         /* infinite: the boot process must be stopped */
319         if (nb_blink == U32_MAX)
320                 hang();
321 }
322
323 #ifdef CONFIG_ADC
324 static int board_check_usb_power(void)
325 {
326         struct ofnode_phandle_args adc_args;
327         struct udevice *adc;
328         ofnode node;
329         unsigned int raw;
330         int max_uV = 0;
331         int min_uV = USB_START_HIGH_THRESHOLD_UV;
332         int ret, uV, adc_count;
333         u32 nb_blink;
334         u8 i;
335         node = ofnode_path("/config");
336         if (!ofnode_valid(node)) {
337                 debug("%s: no /config node?\n", __func__);
338                 return -ENOENT;
339         }
340
341         /*
342          * Retrieve the ADC channels devices and get measurement
343          * for each of them
344          */
345         adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
346                                                    "#io-channel-cells");
347         if (adc_count < 0) {
348                 if (adc_count == -ENOENT)
349                         return 0;
350
351                 pr_err("%s: can't find adc channel (%d)\n", __func__,
352                        adc_count);
353
354                 return adc_count;
355         }
356
357         for (i = 0; i < adc_count; i++) {
358                 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
359                                                    "#io-channel-cells", 0, i,
360                                                    &adc_args)) {
361                         pr_debug("%s: can't find /config/st,adc_usb_pd\n",
362                                  __func__);
363                         return 0;
364                 }
365
366                 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
367                                                   &adc);
368
369                 if (ret) {
370                         pr_err("%s: Can't get adc device(%d)\n", __func__,
371                                ret);
372                         return ret;
373                 }
374
375                 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
376                                               &raw);
377                 if (ret) {
378                         pr_err("%s: single shot failed for %s[%d]!\n",
379                                __func__, adc->name, adc_args.args[0]);
380                         return ret;
381                 }
382                 /* Convert to uV */
383                 if (!adc_raw_to_uV(adc, raw, &uV)) {
384                         if (uV > max_uV)
385                                 max_uV = uV;
386                         if (uV < min_uV)
387                                 min_uV = uV;
388                         pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
389                                  adc->name, adc_args.args[0], raw, uV);
390                 } else {
391                         pr_err("%s: Can't get uV value for %s[%d]\n",
392                                __func__, adc->name, adc_args.args[0]);
393                 }
394         }
395
396         /*
397          * If highest value is inside 1.23 Volts and 2.10 Volts, that means
398          * board is plugged on an USB-C 3A power supply and boot process can
399          * continue.
400          */
401         if (max_uV > USB_START_LOW_THRESHOLD_UV &&
402             max_uV <= USB_START_HIGH_THRESHOLD_UV &&
403             min_uV <= USB_LOW_THRESHOLD_UV)
404                 return 0;
405
406         pr_err("****************************************************\n");
407
408         /*
409          * If highest and lowest value are either both below
410          * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
411          * means USB TYPE-C is in unattached mode, this is an issue, make
412          * u-boot,error-led blinking and stop boot process.
413          */
414         if ((max_uV > USB_LOW_THRESHOLD_UV &&
415              min_uV > USB_LOW_THRESHOLD_UV) ||
416              (max_uV <= USB_LOW_THRESHOLD_UV &&
417              min_uV <= USB_LOW_THRESHOLD_UV)) {
418                 pr_err("* ERROR USB TYPE-C connection in unattached mode   *\n");
419                 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
420                 /* with 125ms interval, led will blink for 17.02 years ....*/
421                 nb_blink = U32_MAX;
422         }
423
424         if (max_uV > USB_LOW_THRESHOLD_UV &&
425             max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
426             min_uV <= USB_LOW_THRESHOLD_UV) {
427                 pr_err("*        WARNING 500mA power supply detected       *\n");
428                 nb_blink = 2;
429         }
430
431         if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
432             max_uV <= USB_START_LOW_THRESHOLD_UV &&
433             min_uV <= USB_LOW_THRESHOLD_UV) {
434                 pr_err("*       WARNING 1.5mA power supply detected        *\n");
435                 nb_blink = 3;
436         }
437
438         /*
439          * If highest value is above 2.15 Volts that means that the USB TypeC
440          * supplies more than 3 Amp, this is not compliant with TypeC specification
441          */
442         if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
443                 pr_err("*      USB TYPE-C charger not compliant with       *\n");
444                 pr_err("*                   specification                  *\n");
445                 pr_err("****************************************************\n\n");
446                 /* with 125ms interval, led will blink for 17.02 years ....*/
447                 nb_blink = U32_MAX;
448         } else {
449                 pr_err("*     Current too low, use a 3A power supply!      *\n");
450                 pr_err("****************************************************\n\n");
451         }
452
453         led_error_blink(nb_blink);
454
455         return 0;
456 }
457 #endif /* CONFIG_ADC */
458
459 static void sysconf_init(void)
460 {
461 #ifndef CONFIG_TFABOOT
462         u8 *syscfg;
463 #ifdef CONFIG_DM_REGULATOR
464         struct udevice *pwr_dev;
465         struct udevice *pwr_reg;
466         struct udevice *dev;
467         int ret;
468         u32 otp = 0;
469 #endif
470         u32 bootr;
471
472         syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
473
474         /* interconnect update : select master using the port 1 */
475         /* LTDC = AXI_M9 */
476         /* GPU  = AXI_M8 */
477         /* today information is hardcoded in U-Boot */
478         writel(BIT(9), syscfg + SYSCFG_ICNR);
479
480         /* disable Pull-Down for boot pin connected to VDD */
481         bootr = readl(syscfg + SYSCFG_BOOTR);
482         bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
483         bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
484         writel(bootr, syscfg + SYSCFG_BOOTR);
485
486 #ifdef CONFIG_DM_REGULATOR
487         /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
488          * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
489          * The customer will have to disable this for low frequencies
490          * or if AFMUX is selected but the function not used, typically for
491          * TRACE. Otherwise, impact on power consumption.
492          *
493          * WARNING:
494          *   enabling High Speed mode while VDD>2.7V
495          *   with the OTP product_below_2v5 (OTP 18, BIT 13)
496          *   erroneously set to 1 can damage the IC!
497          *   => U-Boot set the register only if VDD < 2.7V (in DT)
498          *      but this value need to be consistent with board design
499          */
500         ret = uclass_get_device_by_driver(UCLASS_PMIC,
501                                           DM_GET_DRIVER(stm32mp_pwr_pmic),
502                                           &pwr_dev);
503         if (!ret) {
504                 ret = uclass_get_device_by_driver(UCLASS_MISC,
505                                                   DM_GET_DRIVER(stm32mp_bsec),
506                                                   &dev);
507                 if (ret) {
508                         pr_err("Can't find stm32mp_bsec driver\n");
509                         return;
510                 }
511
512                 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
513                 if (ret > 0)
514                         otp = otp & BIT(13);
515
516                 /* get VDD = vdd-supply */
517                 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
518                                                   &pwr_reg);
519
520                 /* check if VDD is Low Voltage */
521                 if (!ret) {
522                         if (regulator_get_value(pwr_reg) < 2700000) {
523                                 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
524                                        SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
525                                        SYSCFG_IOCTRLSETR_HSLVEN_ETH |
526                                        SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
527                                        SYSCFG_IOCTRLSETR_HSLVEN_SPI,
528                                        syscfg + SYSCFG_IOCTRLSETR);
529
530                                 if (!otp)
531                                         pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
532                         } else {
533                                 if (otp)
534                                         pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
535                         }
536                 } else {
537                         debug("VDD unknown");
538                 }
539         }
540 #endif
541
542         /* activate automatic I/O compensation
543          * warning: need to ensure CSI enabled and ready in clock driver
544          */
545         writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
546
547         while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
548                 ;
549         clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
550 #endif
551 }
552
553 #ifdef CONFIG_DM_REGULATOR
554 /* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
555 static int dk2_i2c1_fix(void)
556 {
557         ofnode node;
558         struct gpio_desc hdmi, audio;
559         int ret = 0;
560
561         node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
562         if (!ofnode_valid(node)) {
563                 pr_debug("%s: no hdmi-transmitter@39 ?\n", __func__);
564                 return -ENOENT;
565         }
566
567         if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
568                                        &hdmi, GPIOD_IS_OUT)) {
569                 pr_debug("%s: could not find reset-gpios\n",
570                          __func__);
571                 return -ENOENT;
572         }
573
574         node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
575         if (!ofnode_valid(node)) {
576                 pr_debug("%s: no cs42l51@4a ?\n", __func__);
577                 return -ENOENT;
578         }
579
580         if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
581                                        &audio, GPIOD_IS_OUT)) {
582                 pr_debug("%s: could not find reset-gpios\n",
583                          __func__);
584                 return -ENOENT;
585         }
586
587         /* before power up, insure that HDMI and AUDIO IC is under reset */
588         ret = dm_gpio_set_value(&hdmi, 1);
589         if (ret) {
590                 pr_err("%s: can't set_value for hdmi_nrst gpio", __func__);
591                 goto error;
592         }
593         ret = dm_gpio_set_value(&audio, 1);
594         if (ret) {
595                 pr_err("%s: can't set_value for audio_nrst gpio", __func__);
596                 goto error;
597         }
598
599         /* power-up audio IC */
600         regulator_autoset_by_name("v1v8_audio", NULL);
601
602         /* power-up HDMI IC */
603         regulator_autoset_by_name("v1v2_hdmi", NULL);
604         regulator_autoset_by_name("v3v3_hdmi", NULL);
605
606 error:
607         return ret;
608 }
609
610 static bool board_is_dk2(void)
611 {
612         if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15x) &&
613             of_machine_is_compatible("st,stm32mp157c-dk2"))
614                 return true;
615
616         return false;
617 }
618 #endif
619
620 /* board dependent setup after realloc */
621 int board_init(void)
622 {
623         struct udevice *dev;
624
625         /* address of boot parameters */
626         gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
627
628         /* probe all PINCTRL for hog */
629         for (uclass_first_device(UCLASS_PINCTRL, &dev);
630              dev;
631              uclass_next_device(&dev)) {
632                 pr_debug("probe pincontrol = %s\n", dev->name);
633         }
634
635         board_key_check();
636
637 #ifdef CONFIG_DM_REGULATOR
638         if (board_is_dk2())
639                 dk2_i2c1_fix();
640
641         regulators_enable_boot_on(_DEBUG);
642 #endif
643
644         sysconf_init();
645
646         if (CONFIG_IS_ENABLED(LED))
647                 led_default_state();
648
649         return 0;
650 }
651
652 int board_late_init(void)
653 {
654         char *boot_device;
655 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
656         const void *fdt_compat;
657         int fdt_compat_len;
658         int ret;
659         u32 otp;
660         struct udevice *dev;
661         char buf[10];
662
663         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
664                                  &fdt_compat_len);
665         if (fdt_compat && fdt_compat_len) {
666                 if (strncmp(fdt_compat, "st,", 3) != 0)
667                         env_set("board_name", fdt_compat);
668                 else
669                         env_set("board_name", fdt_compat + 3);
670         }
671         ret = uclass_get_device_by_driver(UCLASS_MISC,
672                                           DM_GET_DRIVER(stm32mp_bsec),
673                                           &dev);
674
675         if (!ret)
676                 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
677                                 &otp, sizeof(otp));
678         if (!ret && otp) {
679                 snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
680                 env_set("board_id", buf);
681
682                 snprintf(buf, sizeof(buf), "0x%04x",
683                          ((otp >> 8) & 0xF) - 1 + 0xA);
684                 env_set("board_rev", buf);
685         }
686 #endif
687
688 #ifdef CONFIG_ADC
689         /* for DK1/DK2 boards */
690         board_check_usb_power();
691 #endif /* CONFIG_ADC */
692
693         /* Check the boot-source to disable bootdelay */
694         boot_device = env_get("boot_device");
695         if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
696                 env_set("bootdelay", "0");
697
698         return 0;
699 }
700
701 void board_quiesce_devices(void)
702 {
703 #ifdef CONFIG_LED
704         setup_led(LEDST_OFF);
705 #endif
706 }
707
708 /* eth init function : weak called in eqos driver */
709 int board_interface_eth_init(struct udevice *dev,
710                              phy_interface_t interface_type)
711 {
712         u8 *syscfg;
713         u32 value;
714         bool eth_clk_sel_reg = false;
715         bool eth_ref_clk_sel_reg = false;
716
717         /* Gigabit Ethernet 125MHz clock selection. */
718         eth_clk_sel_reg = dev_read_bool(dev, "st,eth_clk_sel");
719
720         /* Ethernet 50Mhz RMII clock selection */
721         eth_ref_clk_sel_reg =
722                 dev_read_bool(dev, "st,eth_ref_clk_sel");
723
724         syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
725
726         if (!syscfg)
727                 return -ENODEV;
728
729         switch (interface_type) {
730         case PHY_INTERFACE_MODE_MII:
731                 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
732                         SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
733                 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
734                 break;
735         case PHY_INTERFACE_MODE_GMII:
736                 if (eth_clk_sel_reg)
737                         value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
738                                 SYSCFG_PMCSETR_ETH_CLK_SEL;
739                 else
740                         value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
741                 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
742                 break;
743         case PHY_INTERFACE_MODE_RMII:
744                 if (eth_ref_clk_sel_reg)
745                         value = SYSCFG_PMCSETR_ETH_SEL_RMII |
746                                 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
747                 else
748                         value = SYSCFG_PMCSETR_ETH_SEL_RMII;
749                 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
750                 break;
751         case PHY_INTERFACE_MODE_RGMII:
752         case PHY_INTERFACE_MODE_RGMII_ID:
753         case PHY_INTERFACE_MODE_RGMII_RXID:
754         case PHY_INTERFACE_MODE_RGMII_TXID:
755                 if (eth_clk_sel_reg)
756                         value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
757                                 SYSCFG_PMCSETR_ETH_CLK_SEL;
758                 else
759                         value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
760                 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
761                 break;
762         default:
763                 debug("%s: Do not manage %d interface\n",
764                       __func__, interface_type);
765                 /* Do not manage others interfaces */
766                 return -EINVAL;
767         }
768
769         /* clear and set ETH configuration bits */
770         writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
771                SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
772                syscfg + SYSCFG_PMCCLRR);
773         writel(value, syscfg + SYSCFG_PMCSETR);
774
775         return 0;
776 }
777
778 enum env_location env_get_location(enum env_operation op, int prio)
779 {
780         u32 bootmode = get_bootmode();
781
782         if (prio)
783                 return ENVL_UNKNOWN;
784
785         switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
786 #ifdef CONFIG_ENV_IS_IN_EXT4
787         case BOOT_FLASH_SD:
788         case BOOT_FLASH_EMMC:
789                 return ENVL_EXT4;
790 #endif
791 #ifdef CONFIG_ENV_IS_IN_UBI
792         case BOOT_FLASH_NAND:
793         case BOOT_FLASH_SPINAND:
794                 return ENVL_UBI;
795 #endif
796 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
797         case BOOT_FLASH_NOR:
798                 return ENVL_SPI_FLASH;
799 #endif
800         default:
801                 return ENVL_NOWHERE;
802         }
803 }
804
805 #if defined(CONFIG_ENV_IS_IN_EXT4)
806 const char *env_ext4_get_intf(void)
807 {
808         u32 bootmode = get_bootmode();
809
810         switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
811         case BOOT_FLASH_SD:
812         case BOOT_FLASH_EMMC:
813                 return "mmc";
814         default:
815                 return "";
816         }
817 }
818
819 const char *env_ext4_get_dev_part(void)
820 {
821         static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
822         u32 bootmode = get_bootmode();
823
824         return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
825 }
826 #endif
827
828 #if defined(CONFIG_OF_BOARD_SETUP)
829 int ft_board_setup(void *blob, bd_t *bd)
830 {
831 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
832         struct node_info nodes[] = {
833                 { "st,stm32f469-qspi",          MTD_DEV_TYPE_NOR,  },
834                 { "st,stm32f469-qspi",          MTD_DEV_TYPE_SPINAND},
835                 { "st,stm32mp15-fmc2",          MTD_DEV_TYPE_NAND, },
836         };
837         fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
838 #endif
839
840         return 0;
841 }
842 #endif
843
844 static void board_copro_image_process(ulong fw_image, size_t fw_size)
845 {
846         int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
847
848         if (!rproc_is_initialized())
849                 if (rproc_init()) {
850                         printf("Remote Processor %d initialization failed\n",
851                                id);
852                         return;
853                 }
854
855         ret = rproc_load(id, fw_image, fw_size);
856         printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
857                id, fw_image, fw_size, ret ? " Failed!" : " Success!");
858
859         if (!ret)
860                 rproc_start(id);
861 }
862
863 U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);