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