board: stm32mp1: Add env_ext4_get_dev_part() and env_ext4_get_intf()
[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 <config.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <g_dnl.h>
11 #include <generic-phy.h>
12 #include <i2c.h>
13 #include <led.h>
14 #include <misc.h>
15 #include <phy.h>
16 #include <reset.h>
17 #include <syscon.h>
18 #include <usb.h>
19 #include <asm/io.h>
20 #include <asm/gpio.h>
21 #include <asm/arch/stm32.h>
22 #include <asm/arch/sys_proto.h>
23 #include <power/regulator.h>
24 #include <usb/dwc2_udc.h>
25
26 /* SYSCFG registers */
27 #define SYSCFG_BOOTR            0x00
28 #define SYSCFG_PMCSETR          0x04
29 #define SYSCFG_IOCTRLSETR       0x18
30 #define SYSCFG_ICNR             0x1C
31 #define SYSCFG_CMPCR            0x20
32 #define SYSCFG_CMPENSETR        0x24
33 #define SYSCFG_PMCCLRR          0x44
34
35 #define SYSCFG_BOOTR_BOOT_MASK          GENMASK(2, 0)
36 #define SYSCFG_BOOTR_BOOTPD_SHIFT       4
37
38 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE          BIT(0)
39 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI        BIT(1)
40 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH            BIT(2)
41 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC          BIT(3)
42 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI            BIT(4)
43
44 #define SYSCFG_CMPCR_SW_CTRL            BIT(1)
45 #define SYSCFG_CMPCR_READY              BIT(8)
46
47 #define SYSCFG_CMPENSETR_MPU_EN         BIT(0)
48
49 #define SYSCFG_PMCSETR_ETH_CLK_SEL      BIT(16)
50 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL  BIT(17)
51
52 #define SYSCFG_PMCSETR_ETH_SELMII       BIT(20)
53
54 #define SYSCFG_PMCSETR_ETH_SEL_MASK     GENMASK(23, 21)
55 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII (0 << 21)
56 #define SYSCFG_PMCSETR_ETH_SEL_RGMII    (1 << 21)
57 #define SYSCFG_PMCSETR_ETH_SEL_RMII     (4 << 21)
58
59 /*
60  * Get a global data pointer
61  */
62 DECLARE_GLOBAL_DATA_PTR;
63
64 #define USB_WARNING_LOW_THRESHOLD_UV    660000
65 #define USB_START_LOW_THRESHOLD_UV      1230000
66 #define USB_START_HIGH_THRESHOLD_UV     2100000
67
68 int checkboard(void)
69 {
70         int ret;
71         char *mode;
72         u32 otp;
73         struct udevice *dev;
74         const char *fdt_compat;
75         int fdt_compat_len;
76
77         if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
78                 mode = "trusted";
79         else
80                 mode = "basic";
81
82         printf("Board: stm32mp1 in %s mode", mode);
83         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
84                                  &fdt_compat_len);
85         if (fdt_compat && fdt_compat_len)
86                 printf(" (%s)", fdt_compat);
87         puts("\n");
88
89         ret = uclass_get_device_by_driver(UCLASS_MISC,
90                                           DM_GET_DRIVER(stm32mp_bsec),
91                                           &dev);
92
93         if (!ret)
94                 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
95                                 &otp, sizeof(otp));
96         if (!ret && otp) {
97                 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
98                        otp >> 16,
99                        (otp >> 12) & 0xF,
100                        ((otp >> 8) & 0xF) - 1 + 'A',
101                        otp & 0xF);
102         }
103
104         return 0;
105 }
106
107 static void board_key_check(void)
108 {
109 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
110         ofnode node;
111         struct gpio_desc gpio;
112         enum forced_boot_mode boot_mode = BOOT_NORMAL;
113
114         node = ofnode_path("/config");
115         if (!ofnode_valid(node)) {
116                 debug("%s: no /config node?\n", __func__);
117                 return;
118         }
119 #ifdef CONFIG_FASTBOOT
120         if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
121                                        &gpio, GPIOD_IS_IN)) {
122                 debug("%s: could not find a /config/st,fastboot-gpios\n",
123                       __func__);
124         } else {
125                 if (dm_gpio_get_value(&gpio)) {
126                         puts("Fastboot key pressed, ");
127                         boot_mode = BOOT_FASTBOOT;
128                 }
129
130                 dm_gpio_free(NULL, &gpio);
131         }
132 #endif
133 #ifdef CONFIG_CMD_STM32PROG
134         if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
135                                        &gpio, GPIOD_IS_IN)) {
136                 debug("%s: could not find a /config/st,stm32prog-gpios\n",
137                       __func__);
138         } else {
139                 if (dm_gpio_get_value(&gpio)) {
140                         puts("STM32Programmer key pressed, ");
141                         boot_mode = BOOT_STM32PROG;
142                 }
143                 dm_gpio_free(NULL, &gpio);
144         }
145 #endif
146
147         if (boot_mode != BOOT_NORMAL) {
148                 puts("entering download mode...\n");
149                 clrsetbits_le32(TAMP_BOOT_CONTEXT,
150                                 TAMP_BOOT_FORCED_MASK,
151                                 boot_mode);
152         }
153 #endif
154 }
155
156 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
157
158 /* STMicroelectronics STUSB1600 Type-C controller */
159 #define STUSB1600_CC_CONNECTION_STATUS          0x0E
160
161 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
162 #define STUSB1600_CC_ATTACH                     BIT(0)
163
164 static int stusb1600_init(struct udevice **dev_stusb1600)
165 {
166         ofnode node;
167         struct udevice *dev, *bus;
168         int ret;
169         u32 chip_addr;
170
171         *dev_stusb1600 = NULL;
172
173         /* if node stusb1600 is present, means DK1 or DK2 board */
174         node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
175         if (!ofnode_valid(node))
176                 return -ENODEV;
177
178         ret = ofnode_read_u32(node, "reg", &chip_addr);
179         if (ret)
180                 return -EINVAL;
181
182         ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
183                                           &bus);
184         if (ret) {
185                 printf("bus for stusb1600 not found\n");
186                 return -ENODEV;
187         }
188
189         ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
190         if (!ret)
191                 *dev_stusb1600 = dev;
192
193         return ret;
194 }
195
196 static int stusb1600_cable_connected(struct udevice *dev)
197 {
198         u8 status;
199
200         if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
201                 return 0;
202
203         return status & STUSB1600_CC_ATTACH;
204 }
205
206 #include <usb/dwc2_udc.h>
207 int g_dnl_board_usb_cable_connected(void)
208 {
209         struct udevice *stusb1600;
210         struct udevice *dwc2_udc_otg;
211         int ret;
212
213         if (!stusb1600_init(&stusb1600))
214                 return stusb1600_cable_connected(stusb1600);
215
216         ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
217                                           DM_GET_DRIVER(dwc2_udc_otg),
218                                           &dwc2_udc_otg);
219         if (!ret)
220                 debug("dwc2_udc_otg init failed\n");
221
222         return dwc2_udc_B_session_valid(dwc2_udc_otg);
223 }
224 #endif /* CONFIG_USB_GADGET */
225
226 static int get_led(struct udevice **dev, char *led_string)
227 {
228         char *led_name;
229         int ret;
230
231         led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
232         if (!led_name) {
233                 pr_debug("%s: could not find %s config string\n",
234                          __func__, led_string);
235                 return -ENOENT;
236         }
237         ret = led_get_by_label(led_name, dev);
238         if (ret) {
239                 debug("%s: get=%d\n", __func__, ret);
240                 return ret;
241         }
242
243         return 0;
244 }
245
246 static int setup_led(enum led_state_t cmd)
247 {
248         struct udevice *dev;
249         int ret;
250
251         ret = get_led(&dev, "u-boot,boot-led");
252         if (ret)
253                 return ret;
254
255         ret = led_set_state(dev, cmd);
256         return ret;
257 }
258
259 static int board_check_usb_power(void)
260 {
261         struct ofnode_phandle_args adc_args;
262         struct udevice *adc;
263         struct udevice *led;
264         ofnode node;
265         unsigned int raw;
266         int max_uV = 0;
267         int ret, uV, adc_count;
268         u8 i, nb_blink;
269
270         node = ofnode_path("/config");
271         if (!ofnode_valid(node)) {
272                 debug("%s: no /config node?\n", __func__);
273                 return -ENOENT;
274         }
275
276         /*
277          * Retrieve the ADC channels devices and get measurement
278          * for each of them
279          */
280         adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
281                                                    "#io-channel-cells");
282         if (adc_count < 0) {
283                 if (adc_count == -ENOENT)
284                         return 0;
285
286                 pr_err("%s: can't find adc channel (%d)\n", __func__,
287                        adc_count);
288
289                 return adc_count;
290         }
291
292         for (i = 0; i < adc_count; i++) {
293                 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
294                                                    "#io-channel-cells", 0, i,
295                                                    &adc_args)) {
296                         pr_debug("%s: can't find /config/st,adc_usb_pd\n",
297                                  __func__);
298                         return 0;
299                 }
300
301                 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
302                                                   &adc);
303
304                 if (ret) {
305                         pr_err("%s: Can't get adc device(%d)\n", __func__,
306                                ret);
307                         return ret;
308                 }
309
310                 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
311                                               &raw);
312                 if (ret) {
313                         pr_err("%s: single shot failed for %s[%d]!\n",
314                                __func__, adc->name, adc_args.args[0]);
315                         return ret;
316                 }
317                 /* Convert to uV */
318                 if (!adc_raw_to_uV(adc, raw, &uV)) {
319                         if (uV > max_uV)
320                                 max_uV = uV;
321                         pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
322                                  adc->name, adc_args.args[0], raw, uV);
323                 } else {
324                         pr_err("%s: Can't get uV value for %s[%d]\n",
325                                __func__, adc->name, adc_args.args[0]);
326                 }
327         }
328
329         /*
330          * If highest value is inside 1.23 Volts and 2.10 Volts, that means
331          * board is plugged on an USB-C 3A power supply and boot process can
332          * continue.
333          */
334         if (max_uV > USB_START_LOW_THRESHOLD_UV &&
335             max_uV < USB_START_HIGH_THRESHOLD_UV)
336                 return 0;
337
338         /* Display warning message and make u-boot,error-led blinking */
339         pr_err("\n*******************************************\n");
340
341         if (max_uV < USB_WARNING_LOW_THRESHOLD_UV) {
342                 pr_err("*   WARNING 500mA power supply detected   *\n");
343                 nb_blink = 2;
344         } else {
345                 pr_err("* WARNING 1.5A power supply detected      *\n");
346                 nb_blink = 3;
347         }
348
349         pr_err("* Current too low, use a 3A power supply! *\n");
350         pr_err("*******************************************\n\n");
351
352         ret = get_led(&led, "u-boot,error-led");
353         if (ret)
354                 return ret;
355
356         for (i = 0; i < nb_blink * 2; i++) {
357                 led_set_state(led, LEDST_TOGGLE);
358                 mdelay(125);
359         }
360         led_set_state(led, LEDST_ON);
361
362         return 0;
363 }
364
365 static void sysconf_init(void)
366 {
367 #ifndef CONFIG_STM32MP1_TRUSTED
368         u8 *syscfg;
369 #ifdef CONFIG_DM_REGULATOR
370         struct udevice *pwr_dev;
371         struct udevice *pwr_reg;
372         struct udevice *dev;
373         int ret;
374         u32 otp = 0;
375 #endif
376         u32 bootr;
377
378         syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
379
380         /* interconnect update : select master using the port 1 */
381         /* LTDC = AXI_M9 */
382         /* GPU  = AXI_M8 */
383         /* today information is hardcoded in U-Boot */
384         writel(BIT(9), syscfg + SYSCFG_ICNR);
385
386         /* disable Pull-Down for boot pin connected to VDD */
387         bootr = readl(syscfg + SYSCFG_BOOTR);
388         bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
389         bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
390         writel(bootr, syscfg + SYSCFG_BOOTR);
391
392 #ifdef CONFIG_DM_REGULATOR
393         /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
394          * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
395          * The customer will have to disable this for low frequencies
396          * or if AFMUX is selected but the function not used, typically for
397          * TRACE. Otherwise, impact on power consumption.
398          *
399          * WARNING:
400          *   enabling High Speed mode while VDD>2.7V
401          *   with the OTP product_below_2v5 (OTP 18, BIT 13)
402          *   erroneously set to 1 can damage the IC!
403          *   => U-Boot set the register only if VDD < 2.7V (in DT)
404          *      but this value need to be consistent with board design
405          */
406         ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
407         if (!ret) {
408                 ret = uclass_get_device_by_driver(UCLASS_MISC,
409                                                   DM_GET_DRIVER(stm32mp_bsec),
410                                                   &dev);
411                 if (ret) {
412                         pr_err("Can't find stm32mp_bsec driver\n");
413                         return;
414                 }
415
416                 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
417                 if (!ret)
418                         otp = otp & BIT(13);
419
420                 /* get VDD = pwr-supply */
421                 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
422                                                   &pwr_reg);
423
424                 /* check if VDD is Low Voltage */
425                 if (!ret) {
426                         if (regulator_get_value(pwr_reg) < 2700000) {
427                                 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
428                                        SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
429                                        SYSCFG_IOCTRLSETR_HSLVEN_ETH |
430                                        SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
431                                        SYSCFG_IOCTRLSETR_HSLVEN_SPI,
432                                        syscfg + SYSCFG_IOCTRLSETR);
433
434                                 if (!otp)
435                                         pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
436                         } else {
437                                 if (otp)
438                                         pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
439                         }
440                 } else {
441                         debug("VDD unknown");
442                 }
443         }
444 #endif
445
446         /* activate automatic I/O compensation
447          * warning: need to ensure CSI enabled and ready in clock driver
448          */
449         writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
450
451         while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
452                 ;
453         clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
454 #endif
455 }
456
457 /* board dependent setup after realloc */
458 int board_init(void)
459 {
460         struct udevice *dev;
461
462         /* address of boot parameters */
463         gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
464
465         /* probe all PINCTRL for hog */
466         for (uclass_first_device(UCLASS_PINCTRL, &dev);
467              dev;
468              uclass_next_device(&dev)) {
469                 pr_debug("probe pincontrol = %s\n", dev->name);
470         }
471
472         board_key_check();
473
474         sysconf_init();
475
476         if (IS_ENABLED(CONFIG_LED))
477                 led_default_state();
478
479         return 0;
480 }
481
482 int board_late_init(void)
483 {
484 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
485         const void *fdt_compat;
486         int fdt_compat_len;
487
488         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
489                                  &fdt_compat_len);
490         if (fdt_compat && fdt_compat_len) {
491                 if (strncmp(fdt_compat, "st,", 3) != 0)
492                         env_set("board_name", fdt_compat);
493                 else
494                         env_set("board_name", fdt_compat + 3);
495         }
496 #endif
497
498         /* for DK1/DK2 boards */
499         board_check_usb_power();
500
501         return 0;
502 }
503
504 void board_quiesce_devices(void)
505 {
506         setup_led(LEDST_OFF);
507 }
508
509 #if defined(CONFIG_ENV_IS_IN_EXT4)
510 const char *env_ext4_get_intf(void)
511 {
512         u32 bootmode = get_bootmode();
513
514         switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
515         case BOOT_FLASH_SD:
516         case BOOT_FLASH_EMMC:
517                 return "mmc";
518         default:
519                 return "";
520         }
521 }
522
523 const char *env_ext4_get_dev_part(void)
524 {
525         static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
526         u32 bootmode = get_bootmode();
527
528         return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
529 }
530 #endif
531
532 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
533
534 #define MTDPARTS_LEN            256
535 #define MTDIDS_LEN              128
536
537 /**
538  * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
539  * If we need to access it before the env is relocated, then we need
540  * to use our own stack buffer. gd->env_buf will be too small.
541  *
542  * @param buf temporary buffer pointer MTDPARTS_LEN long
543  * @return mtdparts variable string, NULL if not found
544  */
545 static const char *env_get_mtdparts(const char *str, char *buf)
546 {
547         if (gd->flags & GD_FLG_ENV_READY)
548                 return env_get(str);
549         if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
550                 return buf;
551
552         return NULL;
553 }
554
555 /**
556  * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
557  */
558 static void board_get_mtdparts(const char *dev,
559                                char *mtdids,
560                                char *mtdparts)
561 {
562         char env_name[32] = "mtdparts_";
563         char tmp_mtdparts[MTDPARTS_LEN];
564         const char *tmp;
565
566         /* name of env variable to read = mtdparts_<dev> */
567         strcat(env_name, dev);
568         tmp = env_get_mtdparts(env_name, tmp_mtdparts);
569         if (tmp) {
570                 /* mtdids: "<dev>=<dev>, ...." */
571                 if (mtdids[0] != '\0')
572                         strcat(mtdids, ",");
573                 strcat(mtdids, dev);
574                 strcat(mtdids, "=");
575                 strcat(mtdids, dev);
576
577                 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
578                 if (mtdparts[0] != '\0')
579                         strncat(mtdparts, ";", MTDPARTS_LEN);
580                 else
581                         strcat(mtdparts, "mtdparts=");
582                 strncat(mtdparts, dev, MTDPARTS_LEN);
583                 strncat(mtdparts, ":", MTDPARTS_LEN);
584                 strncat(mtdparts, tmp, MTDPARTS_LEN);
585         }
586 }
587
588 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
589 {
590         struct udevice *dev;
591         static char parts[2 * MTDPARTS_LEN + 1];
592         static char ids[MTDIDS_LEN + 1];
593         static bool mtd_initialized;
594
595         if (mtd_initialized) {
596                 *mtdids = ids;
597                 *mtdparts = parts;
598                 return;
599         }
600
601         memset(parts, 0, sizeof(parts));
602         memset(ids, 0, sizeof(ids));
603
604         if (!uclass_get_device(UCLASS_MTD, 0, &dev))
605                 board_get_mtdparts("nand0", ids, parts);
606
607         if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
608                 board_get_mtdparts("nor0", ids, parts);
609
610         mtd_initialized = true;
611         *mtdids = ids;
612         *mtdparts = parts;
613         debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
614 }
615 #endif