uboot/board/st/stm32mp1/stm32mp1.c
<<
>>
Prefs
   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 <dfu.h>
  11#include <dm.h>
  12#include <env.h>
  13#include <env_internal.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 <malloc.h>
  21#include <memalign.h>
  22#include <misc.h>
  23#include <mtd.h>
  24#include <mtd_node.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/err.h>
  38#include <power/regulator.h>
  39#include <usb/dwc2_udc.h>
  40
  41/* SYSCFG registers */
  42#define SYSCFG_BOOTR            0x00
  43#define SYSCFG_PMCSETR          0x04
  44#define SYSCFG_IOCTRLSETR       0x18
  45#define SYSCFG_ICNR             0x1C
  46#define SYSCFG_CMPCR            0x20
  47#define SYSCFG_CMPENSETR        0x24
  48#define SYSCFG_PMCCLRR          0x44
  49
  50#define SYSCFG_BOOTR_BOOT_MASK          GENMASK(2, 0)
  51#define SYSCFG_BOOTR_BOOTPD_SHIFT       4
  52
  53#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE          BIT(0)
  54#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI        BIT(1)
  55#define SYSCFG_IOCTRLSETR_HSLVEN_ETH            BIT(2)
  56#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC          BIT(3)
  57#define SYSCFG_IOCTRLSETR_HSLVEN_SPI            BIT(4)
  58
  59#define SYSCFG_CMPCR_SW_CTRL            BIT(1)
  60#define SYSCFG_CMPCR_READY              BIT(8)
  61
  62#define SYSCFG_CMPENSETR_MPU_EN         BIT(0)
  63
  64#define SYSCFG_PMCSETR_ETH_CLK_SEL      BIT(16)
  65#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL  BIT(17)
  66
  67#define SYSCFG_PMCSETR_ETH_SELMII       BIT(20)
  68
  69#define SYSCFG_PMCSETR_ETH_SEL_MASK     GENMASK(23, 21)
  70#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
  71#define SYSCFG_PMCSETR_ETH_SEL_RGMII    BIT(21)
  72#define SYSCFG_PMCSETR_ETH_SEL_RMII     BIT(23)
  73
  74/*
  75 * Get a global data pointer
  76 */
  77DECLARE_GLOBAL_DATA_PTR;
  78
  79#define USB_LOW_THRESHOLD_UV            200000
  80#define USB_WARNING_LOW_THRESHOLD_UV    660000
  81#define USB_START_LOW_THRESHOLD_UV      1230000
  82#define USB_START_HIGH_THRESHOLD_UV     2150000
  83
  84int checkboard(void)
  85{
  86        int ret;
  87        char *mode;
  88        u32 otp;
  89        struct udevice *dev;
  90        const char *fdt_compat;
  91        int fdt_compat_len;
  92
  93        if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
  94                mode = "trusted with OP-TEE";
  95        else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
  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        ret = uclass_get_device_by_driver(UCLASS_MISC,
 108                                          DM_GET_DRIVER(stm32mp_bsec),
 109                                          &dev);
 110
 111        if (!ret)
 112                ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
 113                                &otp, sizeof(otp));
 114        if (ret > 0 && otp) {
 115                printf("Board: MB%04x Var%d Rev.%c-%02d\n",
 116                       otp >> 16,
 117                       (otp >> 12) & 0xF,
 118                       ((otp >> 8) & 0xF) - 1 + 'A',
 119                       otp & 0xF);
 120        }
 121
 122        return 0;
 123}
 124
 125static void board_key_check(void)
 126{
 127#if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
 128        ofnode node;
 129        struct gpio_desc gpio;
 130        enum forced_boot_mode boot_mode = BOOT_NORMAL;
 131
 132        node = ofnode_path("/config");
 133        if (!ofnode_valid(node)) {
 134                debug("%s: no /config node?\n", __func__);
 135                return;
 136        }
 137#ifdef CONFIG_FASTBOOT
 138        if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
 139                                       &gpio, GPIOD_IS_IN)) {
 140                debug("%s: could not find a /config/st,fastboot-gpios\n",
 141                      __func__);
 142        } else {
 143                if (dm_gpio_get_value(&gpio)) {
 144                        puts("Fastboot key pressed, ");
 145                        boot_mode = BOOT_FASTBOOT;
 146                }
 147
 148                dm_gpio_free(NULL, &gpio);
 149        }
 150#endif
 151#ifdef CONFIG_CMD_STM32PROG
 152        if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
 153                                       &gpio, GPIOD_IS_IN)) {
 154                debug("%s: could not find a /config/st,stm32prog-gpios\n",
 155                      __func__);
 156        } else {
 157                if (dm_gpio_get_value(&gpio)) {
 158                        puts("STM32Programmer key pressed, ");
 159                        boot_mode = BOOT_STM32PROG;
 160                }
 161                dm_gpio_free(NULL, &gpio);
 162        }
 163#endif
 164
 165        if (boot_mode != BOOT_NORMAL) {
 166                puts("entering download mode...\n");
 167                clrsetbits_le32(TAMP_BOOT_CONTEXT,
 168                                TAMP_BOOT_FORCED_MASK,
 169                                boot_mode);
 170        }
 171#endif
 172}
 173
 174#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
 175
 176/* STMicroelectronics STUSB1600 Type-C controller */
 177#define STUSB1600_CC_CONNECTION_STATUS          0x0E
 178
 179/* STUSB1600_CC_CONNECTION_STATUS bitfields */
 180#define STUSB1600_CC_ATTACH                     BIT(0)
 181
 182static int stusb1600_init(struct udevice **dev_stusb1600)
 183{
 184        ofnode node;
 185        struct udevice *dev, *bus;
 186        int ret;
 187        u32 chip_addr;
 188
 189        *dev_stusb1600 = NULL;
 190
 191        /* if node stusb1600 is present, means DK1 or DK2 board */
 192        node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
 193        if (!ofnode_valid(node))
 194                return -ENODEV;
 195
 196        ret = ofnode_read_u32(node, "reg", &chip_addr);
 197        if (ret)
 198                return -EINVAL;
 199
 200        ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
 201                                          &bus);
 202        if (ret) {
 203                printf("bus for stusb1600 not found\n");
 204                return -ENODEV;
 205        }
 206
 207        ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
 208        if (!ret)
 209                *dev_stusb1600 = dev;
 210
 211        return ret;
 212}
 213
 214static int stusb1600_cable_connected(struct udevice *dev)
 215{
 216        u8 status;
 217
 218        if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
 219                return 0;
 220
 221        return status & STUSB1600_CC_ATTACH;
 222}
 223
 224#include <usb/dwc2_udc.h>
 225int g_dnl_board_usb_cable_connected(void)
 226{
 227        struct udevice *stusb1600;
 228        struct udevice *dwc2_udc_otg;
 229        int ret;
 230
 231        if (!stusb1600_init(&stusb1600))
 232                return stusb1600_cable_connected(stusb1600);
 233
 234        ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
 235                                          DM_GET_DRIVER(dwc2_udc_otg),
 236                                          &dwc2_udc_otg);
 237        if (!ret)
 238                debug("dwc2_udc_otg init failed\n");
 239
 240        return dwc2_udc_B_session_valid(dwc2_udc_otg);
 241}
 242
 243#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
 244#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
 245
 246int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
 247{
 248        if (!strcmp(name, "usb_dnl_dfu"))
 249                put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
 250        else if (!strcmp(name, "usb_dnl_fastboot"))
 251                put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
 252                              &dev->idProduct);
 253        else
 254                put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
 255
 256        return 0;
 257}
 258
 259#endif /* CONFIG_USB_GADGET */
 260
 261#ifdef CONFIG_LED
 262static int get_led(struct udevice **dev, char *led_string)
 263{
 264        char *led_name;
 265        int ret;
 266
 267        led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
 268        if (!led_name) {
 269                pr_debug("%s: could not find %s config string\n",
 270                         __func__, led_string);
 271                return -ENOENT;
 272        }
 273        ret = led_get_by_label(led_name, dev);
 274        if (ret) {
 275                debug("%s: get=%d\n", __func__, ret);
 276                return ret;
 277        }
 278
 279        return 0;
 280}
 281
 282static int setup_led(enum led_state_t cmd)
 283{
 284        struct udevice *dev;
 285        int ret;
 286
 287        ret = get_led(&dev, "u-boot,boot-led");
 288        if (ret)
 289                return ret;
 290
 291        ret = led_set_state(dev, cmd);
 292        return ret;
 293}
 294#endif
 295
 296static void __maybe_unused led_error_blink(u32 nb_blink)
 297{
 298#ifdef CONFIG_LED
 299        int ret;
 300        struct udevice *led;
 301        u32 i;
 302#endif
 303
 304        if (!nb_blink)
 305                return;
 306
 307#ifdef CONFIG_LED
 308        ret = get_led(&led, "u-boot,error-led");
 309        if (!ret) {
 310                /* make u-boot,error-led blinking */
 311                /* if U32_MAX and 125ms interval, for 17.02 years */
 312                for (i = 0; i < 2 * nb_blink; i++) {
 313                        led_set_state(led, LEDST_TOGGLE);
 314                        mdelay(125);
 315                        WATCHDOG_RESET();
 316                }
 317        }
 318#endif
 319
 320        /* infinite: the boot process must be stopped */
 321        if (nb_blink == U32_MAX)
 322                hang();
 323}
 324
 325#ifdef CONFIG_ADC
 326static int board_check_usb_power(void)
 327{
 328        struct ofnode_phandle_args adc_args;
 329        struct udevice *adc;
 330        ofnode node;
 331        unsigned int raw;
 332        int max_uV = 0;
 333        int min_uV = USB_START_HIGH_THRESHOLD_UV;
 334        int ret, uV, adc_count;
 335        u32 nb_blink;
 336        u8 i;
 337        node = ofnode_path("/config");
 338        if (!ofnode_valid(node)) {
 339                debug("%s: no /config node?\n", __func__);
 340                return -ENOENT;
 341        }
 342
 343        /*
 344         * Retrieve the ADC channels devices and get measurement
 345         * for each of them
 346         */
 347        adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
 348                                                   "#io-channel-cells");
 349        if (adc_count < 0) {
 350                if (adc_count == -ENOENT)
 351                        return 0;
 352
 353                pr_err("%s: can't find adc channel (%d)\n", __func__,
 354                       adc_count);
 355
 356                return adc_count;
 357        }
 358
 359        for (i = 0; i < adc_count; i++) {
 360                if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
 361                                                   "#io-channel-cells", 0, i,
 362                                                   &adc_args)) {
 363                        pr_debug("%s: can't find /config/st,adc_usb_pd\n",
 364                                 __func__);
 365                        return 0;
 366                }
 367
 368                ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
 369                                                  &adc);
 370
 371                if (ret) {
 372                        pr_err("%s: Can't get adc device(%d)\n", __func__,
 373                               ret);
 374                        return ret;
 375                }
 376
 377                ret = adc_channel_single_shot(adc->name, adc_args.args[0],
 378                                              &raw);
 379                if (ret) {
 380                        pr_err("%s: single shot failed for %s[%d]!\n",
 381                               __func__, adc->name, adc_args.args[0]);
 382                        return ret;
 383                }
 384                /* Convert to uV */
 385                if (!adc_raw_to_uV(adc, raw, &uV)) {
 386                        if (uV > max_uV)
 387                                max_uV = uV;
 388                        if (uV < min_uV)
 389                                min_uV = uV;
 390                        pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
 391                                 adc->name, adc_args.args[0], raw, uV);
 392                } else {
 393                        pr_err("%s: Can't get uV value for %s[%d]\n",
 394                               __func__, adc->name, adc_args.args[0]);
 395                }
 396        }
 397
 398        /*
 399         * If highest value is inside 1.23 Volts and 2.10 Volts, that means
 400         * board is plugged on an USB-C 3A power supply and boot process can
 401         * continue.
 402         */
 403        if (max_uV > USB_START_LOW_THRESHOLD_UV &&
 404            max_uV <= USB_START_HIGH_THRESHOLD_UV &&
 405            min_uV <= USB_LOW_THRESHOLD_UV)
 406                return 0;
 407
 408        pr_err("****************************************************\n");
 409
 410        /*
 411         * If highest and lowest value are either both below
 412         * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
 413         * means USB TYPE-C is in unattached mode, this is an issue, make
 414         * u-boot,error-led blinking and stop boot process.
 415         */
 416        if ((max_uV > USB_LOW_THRESHOLD_UV &&
 417             min_uV > USB_LOW_THRESHOLD_UV) ||
 418             (max_uV <= USB_LOW_THRESHOLD_UV &&
 419             min_uV <= USB_LOW_THRESHOLD_UV)) {
 420                pr_err("* ERROR USB TYPE-C connection in unattached mode   *\n");
 421                pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
 422                /* with 125ms interval, led will blink for 17.02 years ....*/
 423                nb_blink = U32_MAX;
 424        }
 425
 426        if (max_uV > USB_LOW_THRESHOLD_UV &&
 427            max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
 428            min_uV <= USB_LOW_THRESHOLD_UV) {
 429                pr_err("*        WARNING 500mA power supply detected       *\n");
 430                nb_blink = 2;
 431        }
 432
 433        if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
 434            max_uV <= USB_START_LOW_THRESHOLD_UV &&
 435            min_uV <= USB_LOW_THRESHOLD_UV) {
 436                pr_err("*       WARNING 1.5mA power supply detected        *\n");
 437                nb_blink = 3;
 438        }
 439
 440        /*
 441         * If highest value is above 2.15 Volts that means that the USB TypeC
 442         * supplies more than 3 Amp, this is not compliant with TypeC specification
 443         */
 444        if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
 445                pr_err("*      USB TYPE-C charger not compliant with       *\n");
 446                pr_err("*                   specification                  *\n");
 447                pr_err("****************************************************\n\n");
 448                /* with 125ms interval, led will blink for 17.02 years ....*/
 449                nb_blink = U32_MAX;
 450        } else {
 451                pr_err("*     Current too low, use a 3A power supply!      *\n");
 452                pr_err("****************************************************\n\n");
 453        }
 454
 455        led_error_blink(nb_blink);
 456
 457        return 0;
 458}
 459#endif /* CONFIG_ADC */
 460
 461static void sysconf_init(void)
 462{
 463#ifndef CONFIG_STM32MP1_TRUSTED
 464        u8 *syscfg;
 465#ifdef CONFIG_DM_REGULATOR
 466        struct udevice *pwr_dev;
 467        struct udevice *pwr_reg;
 468        struct udevice *dev;
 469        int ret;
 470        u32 otp = 0;
 471#endif
 472        u32 bootr;
 473
 474        syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
 475
 476        /* interconnect update : select master using the port 1 */
 477        /* LTDC = AXI_M9 */
 478        /* GPU  = AXI_M8 */
 479        /* today information is hardcoded in U-Boot */
 480        writel(BIT(9), syscfg + SYSCFG_ICNR);
 481
 482        /* disable Pull-Down for boot pin connected to VDD */
 483        bootr = readl(syscfg + SYSCFG_BOOTR);
 484        bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
 485        bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
 486        writel(bootr, syscfg + SYSCFG_BOOTR);
 487
 488#ifdef CONFIG_DM_REGULATOR
 489        /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
 490         * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
 491         * The customer will have to disable this for low frequencies
 492         * or if AFMUX is selected but the function not used, typically for
 493         * TRACE. Otherwise, impact on power consumption.
 494         *
 495         * WARNING:
 496         *   enabling High Speed mode while VDD>2.7V
 497         *   with the OTP product_below_2v5 (OTP 18, BIT 13)
 498         *   erroneously set to 1 can damage the IC!
 499         *   => U-Boot set the register only if VDD < 2.7V (in DT)
 500         *      but this value need to be consistent with board design
 501         */
 502        ret = uclass_get_device_by_driver(UCLASS_PMIC,
 503                                          DM_GET_DRIVER(stm32mp_pwr_pmic),
 504                                          &pwr_dev);
 505        if (!ret) {
 506                ret = uclass_get_device_by_driver(UCLASS_MISC,
 507                                                  DM_GET_DRIVER(stm32mp_bsec),
 508                                                  &dev);
 509                if (ret) {
 510                        pr_err("Can't find stm32mp_bsec driver\n");
 511                        return;
 512                }
 513
 514                ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
 515                if (ret > 0)
 516                        otp = otp & BIT(13);
 517
 518                /* get VDD = vdd-supply */
 519                ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
 520                                                  &pwr_reg);
 521
 522                /* check if VDD is Low Voltage */
 523                if (!ret) {
 524                        if (regulator_get_value(pwr_reg) < 2700000) {
 525                                writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
 526                                       SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
 527                                       SYSCFG_IOCTRLSETR_HSLVEN_ETH |
 528                                       SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
 529                                       SYSCFG_IOCTRLSETR_HSLVEN_SPI,
 530                                       syscfg + SYSCFG_IOCTRLSETR);
 531
 532                                if (!otp)
 533                                        pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
 534                        } else {
 535                                if (otp)
 536                                        pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
 537                        }
 538                } else {
 539                        debug("VDD unknown");
 540                }
 541        }
 542#endif
 543
 544        /* activate automatic I/O compensation
 545         * warning: need to ensure CSI enabled and ready in clock driver
 546         */
 547        writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
 548
 549        while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
 550                ;
 551        clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
 552#endif
 553}
 554
 555#ifdef CONFIG_DM_REGULATOR
 556/* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
 557static int dk2_i2c1_fix(void)
 558{
 559        ofnode node;
 560        struct gpio_desc hdmi, audio;
 561        int ret = 0;
 562
 563        node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
 564        if (!ofnode_valid(node)) {
 565                pr_debug("%s: no hdmi-transmitter@39 ?\n", __func__);
 566                return -ENOENT;
 567        }
 568
 569        if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
 570                                       &hdmi, GPIOD_IS_OUT)) {
 571                pr_debug("%s: could not find reset-gpios\n",
 572                         __func__);
 573                return -ENOENT;
 574        }
 575
 576        node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
 577        if (!ofnode_valid(node)) {
 578                pr_debug("%s: no cs42l51@4a ?\n", __func__);
 579                return -ENOENT;
 580        }
 581
 582        if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
 583                                       &audio, GPIOD_IS_OUT)) {
 584                pr_debug("%s: could not find reset-gpios\n",
 585                         __func__);
 586                return -ENOENT;
 587        }
 588
 589        /* before power up, insure that HDMI and AUDIO IC is under reset */
 590        ret = dm_gpio_set_value(&hdmi, 1);
 591        if (ret) {
 592                pr_err("%s: can't set_value for hdmi_nrst gpio", __func__);
 593                goto error;
 594        }
 595        ret = dm_gpio_set_value(&audio, 1);
 596        if (ret) {
 597                pr_err("%s: can't set_value for audio_nrst gpio", __func__);
 598                goto error;
 599        }
 600
 601        /* power-up audio IC */
 602        regulator_autoset_by_name("v1v8_audio", NULL);
 603
 604        /* power-up HDMI IC */
 605        regulator_autoset_by_name("v1v2_hdmi", NULL);
 606        regulator_autoset_by_name("v3v3_hdmi", NULL);
 607
 608error:
 609        return ret;
 610}
 611
 612static bool board_is_dk2(void)
 613{
 614        if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15x) &&
 615            of_machine_is_compatible("st,stm32mp157c-dk2"))
 616                return true;
 617
 618        return false;
 619}
 620#endif
 621
 622/* board dependent setup after realloc */
 623int board_init(void)
 624{
 625        struct udevice *dev;
 626
 627        /* address of boot parameters */
 628        gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
 629
 630        /* probe all PINCTRL for hog */
 631        for (uclass_first_device(UCLASS_PINCTRL, &dev);
 632             dev;
 633             uclass_next_device(&dev)) {
 634                pr_debug("probe pincontrol = %s\n", dev->name);
 635        }
 636
 637        board_key_check();
 638
 639#ifdef CONFIG_DM_REGULATOR
 640        if (board_is_dk2())
 641                dk2_i2c1_fix();
 642
 643        regulators_enable_boot_on(_DEBUG);
 644#endif
 645
 646        sysconf_init();
 647
 648        if (CONFIG_IS_ENABLED(CONFIG_LED))
 649                led_default_state();
 650
 651        return 0;
 652}
 653
 654int board_late_init(void)
 655{
 656        char *boot_device;
 657#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
 658        const void *fdt_compat;
 659        int fdt_compat_len;
 660        int ret;
 661        u32 otp;
 662        struct udevice *dev;
 663        char buf[10];
 664
 665        fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
 666                                 &fdt_compat_len);
 667        if (fdt_compat && fdt_compat_len) {
 668                if (strncmp(fdt_compat, "st,", 3) != 0)
 669                        env_set("board_name", fdt_compat);
 670                else
 671                        env_set("board_name", fdt_compat + 3);
 672        }
 673        ret = uclass_get_device_by_driver(UCLASS_MISC,
 674                                          DM_GET_DRIVER(stm32mp_bsec),
 675                                          &dev);
 676
 677        if (!ret)
 678                ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
 679                                &otp, sizeof(otp));
 680        if (!ret && otp) {
 681                snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
 682                env_set("board_id", buf);
 683
 684                snprintf(buf, sizeof(buf), "0x%04x",
 685                         ((otp >> 8) & 0xF) - 1 + 0xA);
 686                env_set("board_rev", buf);
 687        }
 688#endif
 689
 690#ifdef CONFIG_ADC
 691        /* for DK1/DK2 boards */
 692        board_check_usb_power();
 693#endif /* CONFIG_ADC */
 694
 695        /* Check the boot-source to disable bootdelay */
 696        boot_device = env_get("boot_device");
 697        if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb"))
 698                env_set("bootdelay", "0");
 699
 700        return 0;
 701}
 702
 703void board_quiesce_devices(void)
 704{
 705#ifdef CONFIG_LED
 706        setup_led(LEDST_OFF);
 707#endif
 708}
 709
 710/* eth init function : weak called in eqos driver */
 711int board_interface_eth_init(struct udevice *dev,
 712                             phy_interface_t interface_type)
 713{
 714        u8 *syscfg;
 715        u32 value;
 716        bool eth_clk_sel_reg = false;
 717        bool eth_ref_clk_sel_reg = false;
 718
 719        /* Gigabit Ethernet 125MHz clock selection. */
 720        eth_clk_sel_reg = dev_read_bool(dev, "st,eth_clk_sel");
 721
 722        /* Ethernet 50Mhz RMII clock selection */
 723        eth_ref_clk_sel_reg =
 724                dev_read_bool(dev, "st,eth_ref_clk_sel");
 725
 726        syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
 727
 728        if (!syscfg)
 729                return -ENODEV;
 730
 731        switch (interface_type) {
 732        case PHY_INTERFACE_MODE_MII:
 733                value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
 734                        SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
 735                debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
 736                break;
 737        case PHY_INTERFACE_MODE_GMII:
 738                if (eth_clk_sel_reg)
 739                        value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
 740                                SYSCFG_PMCSETR_ETH_CLK_SEL;
 741                else
 742                        value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
 743                debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
 744                break;
 745        case PHY_INTERFACE_MODE_RMII:
 746                if (eth_ref_clk_sel_reg)
 747                        value = SYSCFG_PMCSETR_ETH_SEL_RMII |
 748                                SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
 749                else
 750                        value = SYSCFG_PMCSETR_ETH_SEL_RMII;
 751                debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
 752                break;
 753        case PHY_INTERFACE_MODE_RGMII:
 754        case PHY_INTERFACE_MODE_RGMII_ID:
 755        case PHY_INTERFACE_MODE_RGMII_RXID:
 756        case PHY_INTERFACE_MODE_RGMII_TXID:
 757                if (eth_clk_sel_reg)
 758                        value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
 759                                SYSCFG_PMCSETR_ETH_CLK_SEL;
 760                else
 761                        value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
 762                debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
 763                break;
 764        default:
 765                debug("%s: Do not manage %d interface\n",
 766                      __func__, interface_type);
 767                /* Do not manage others interfaces */
 768                return -EINVAL;
 769        }
 770
 771        /* clear and set ETH configuration bits */
 772        writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
 773               SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
 774               syscfg + SYSCFG_PMCCLRR);
 775        writel(value, syscfg + SYSCFG_PMCSETR);
 776
 777        return 0;
 778}
 779
 780enum env_location env_get_location(enum env_operation op, int prio)
 781{
 782        u32 bootmode = get_bootmode();
 783
 784        if (prio)
 785                return ENVL_UNKNOWN;
 786
 787        switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
 788#ifdef CONFIG_ENV_IS_IN_EXT4
 789        case BOOT_FLASH_SD:
 790        case BOOT_FLASH_EMMC:
 791                return ENVL_EXT4;
 792#endif
 793#ifdef CONFIG_ENV_IS_IN_UBI
 794        case BOOT_FLASH_NAND:
 795                return ENVL_UBI;
 796#endif
 797#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
 798        case BOOT_FLASH_NOR:
 799                return ENVL_SPI_FLASH;
 800#endif
 801        default:
 802                return ENVL_NOWHERE;
 803        }
 804}
 805
 806#if defined(CONFIG_ENV_IS_IN_EXT4)
 807const char *env_ext4_get_intf(void)
 808{
 809        u32 bootmode = get_bootmode();
 810
 811        switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
 812        case BOOT_FLASH_SD:
 813        case BOOT_FLASH_EMMC:
 814                return "mmc";
 815        default:
 816                return "";
 817        }
 818}
 819
 820const char *env_ext4_get_dev_part(void)
 821{
 822        static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
 823        u32 bootmode = get_bootmode();
 824
 825        return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
 826}
 827#endif
 828
 829#ifdef CONFIG_SYS_MTDPARTS_RUNTIME
 830
 831#define MTDPARTS_LEN            256
 832#define MTDIDS_LEN              128
 833
 834/**
 835 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
 836 * If we need to access it before the env is relocated, then we need
 837 * to use our own stack buffer. gd->env_buf will be too small.
 838 *
 839 * @param buf temporary buffer pointer MTDPARTS_LEN long
 840 * @return mtdparts variable string, NULL if not found
 841 */
 842static const char *env_get_mtdparts(const char *str, char *buf)
 843{
 844        if (gd->flags & GD_FLG_ENV_READY)
 845                return env_get(str);
 846        if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
 847                return buf;
 848
 849        return NULL;
 850}
 851
 852/**
 853 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
 854 */
 855static void board_get_mtdparts(const char *dev,
 856                               char *mtdids,
 857                               char *mtdparts)
 858{
 859        char env_name[32] = "mtdparts_";
 860        char tmp_mtdparts[MTDPARTS_LEN];
 861        const char *tmp;
 862
 863        /* name of env variable to read = mtdparts_<dev> */
 864        strcat(env_name, dev);
 865        tmp = env_get_mtdparts(env_name, tmp_mtdparts);
 866        if (tmp) {
 867                /* mtdids: "<dev>=<dev>, ...." */
 868                if (mtdids[0] != '\0')
 869                        strcat(mtdids, ",");
 870                strcat(mtdids, dev);
 871                strcat(mtdids, "=");
 872                strcat(mtdids, dev);
 873
 874                /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
 875                if (mtdparts[0] != '\0')
 876                        strncat(mtdparts, ";", MTDPARTS_LEN);
 877                else
 878                        strcat(mtdparts, "mtdparts=");
 879                strncat(mtdparts, dev, MTDPARTS_LEN);
 880                strncat(mtdparts, ":", MTDPARTS_LEN);
 881                strncat(mtdparts, tmp, MTDPARTS_LEN);
 882        }
 883}
 884
 885void board_mtdparts_default(const char **mtdids, const char **mtdparts)
 886{
 887        struct mtd_info *mtd;
 888        struct udevice *dev;
 889        static char parts[3 * MTDPARTS_LEN + 1];
 890        static char ids[MTDIDS_LEN + 1];
 891        static bool mtd_initialized;
 892
 893        if (mtd_initialized) {
 894                *mtdids = ids;
 895                *mtdparts = parts;
 896                return;
 897        }
 898
 899        memset(parts, 0, sizeof(parts));
 900        memset(ids, 0, sizeof(ids));
 901
 902        /* probe all MTD devices */
 903        for (uclass_first_device(UCLASS_MTD, &dev);
 904             dev;
 905             uclass_next_device(&dev)) {
 906                pr_debug("mtd device = %s\n", dev->name);
 907        }
 908
 909        mtd = get_mtd_device_nm("nand0");
 910        if (!IS_ERR_OR_NULL(mtd)) {
 911                board_get_mtdparts("nand0", ids, parts);
 912                put_mtd_device(mtd);
 913        }
 914
 915        mtd = get_mtd_device_nm("spi-nand0");
 916        if (!IS_ERR_OR_NULL(mtd)) {
 917                board_get_mtdparts("spi-nand0", ids, parts);
 918                put_mtd_device(mtd);
 919        }
 920
 921        if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
 922                board_get_mtdparts("nor0", ids, parts);
 923
 924        mtd_initialized = true;
 925        *mtdids = ids;
 926        *mtdparts = parts;
 927        debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
 928}
 929#endif
 930
 931#if defined(CONFIG_OF_BOARD_SETUP)
 932int ft_board_setup(void *blob, bd_t *bd)
 933{
 934#ifdef CONFIG_FDT_FIXUP_PARTITIONS
 935        struct node_info nodes[] = {
 936                { "st,stm32f469-qspi",          MTD_DEV_TYPE_NOR,  },
 937                { "st,stm32mp15-fmc2",          MTD_DEV_TYPE_NAND, },
 938        };
 939        fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
 940#endif
 941
 942        return 0;
 943}
 944#endif
 945
 946#ifdef CONFIG_SET_DFU_ALT_INFO
 947#define DFU_ALT_BUF_LEN SZ_1K
 948
 949static void board_get_alt_info(const char *dev, char *buff)
 950{
 951        char var_name[32] = "dfu_alt_info_";
 952        int ret;
 953
 954        ALLOC_CACHE_ALIGN_BUFFER(char, tmp_alt, DFU_ALT_BUF_LEN);
 955
 956        /* name of env variable to read = dfu_alt_info_<dev> */
 957        strcat(var_name, dev);
 958        ret = env_get_f(var_name, tmp_alt, DFU_ALT_BUF_LEN);
 959        if (ret) {
 960                if (buff[0] != '\0')
 961                        strcat(buff, "&");
 962                strncat(buff, tmp_alt, DFU_ALT_BUF_LEN);
 963        }
 964}
 965
 966void set_dfu_alt_info(char *interface, char *devstr)
 967{
 968        struct udevice *dev;
 969        struct mtd_info *mtd;
 970
 971        ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
 972
 973        if (env_get("dfu_alt_info"))
 974                return;
 975
 976        memset(buf, 0, sizeof(buf));
 977
 978        /* probe all MTD devices */
 979        mtd_probe_devices();
 980
 981        board_get_alt_info("ram", buf);
 982
 983        if (!uclass_get_device(UCLASS_MMC, 0, &dev))
 984                board_get_alt_info("mmc0", buf);
 985
 986        if (!uclass_get_device(UCLASS_MMC, 1, &dev))
 987                board_get_alt_info("mmc1", buf);
 988
 989        if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
 990                board_get_alt_info("nor0", buf);
 991
 992        mtd = get_mtd_device_nm("nand0");
 993        if (!IS_ERR_OR_NULL(mtd))
 994                board_get_alt_info("nand0", buf);
 995
 996        mtd = get_mtd_device_nm("spi-nand0");
 997        if (!IS_ERR_OR_NULL(mtd))
 998                board_get_alt_info("spi-nand0", buf);
 999
1000#ifdef CONFIG_DFU_VIRT
1001        strncat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
1002
1003        if (IS_ENABLED(CONFIG_PMIC_STPMIC1))
1004                strncat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN);
1005#endif
1006
1007        env_set("dfu_alt_info", buf);
1008        puts("DFU alt info setting: done\n");
1009}
1010
1011#if CONFIG_IS_ENABLED(DFU_VIRT)
1012#include <dfu.h>
1013#include <power/stpmic1.h>
1014
1015static int dfu_otp_read(u64 offset, u8 *buffer, long *size)
1016{
1017        struct udevice *dev;
1018        int ret;
1019
1020        ret = uclass_get_device_by_driver(UCLASS_MISC,
1021                                          DM_GET_DRIVER(stm32mp_bsec),
1022                                          &dev);
1023        if (ret)
1024                return ret;
1025
1026        ret = misc_read(dev, offset + STM32_BSEC_OTP_OFFSET, buffer, *size);
1027        if (ret >= 0) {
1028                *size = ret;
1029                ret = 0;
1030        }
1031
1032        return 0;
1033}
1034
1035static int dfu_pmic_read(u64 offset, u8 *buffer, long *size)
1036{
1037        int ret;
1038#ifdef CONFIG_PMIC_STPMIC1
1039        struct udevice *dev;
1040
1041        ret = uclass_get_device_by_driver(UCLASS_MISC,
1042                                          DM_GET_DRIVER(stpmic1_nvm),
1043                                          &dev);
1044        if (ret)
1045                return ret;
1046
1047        ret = misc_read(dev, 0xF8 + offset, buffer, *size);
1048        if (ret >= 0) {
1049                *size = ret;
1050                ret = 0;
1051        }
1052        if (ret == -EACCES) {
1053                *size = 0;
1054                ret = 0;
1055        }
1056#else
1057        pr_err("PMIC update not supported");
1058        ret = -EOPNOTSUPP;
1059#endif
1060
1061        return ret;
1062}
1063
1064int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset,
1065                         void *buf, long *len)
1066{
1067        switch (dfu->data.virt.dev_num) {
1068        case 0x0:
1069                return dfu_otp_read(offset, buf, len);
1070        case 0x1:
1071                return dfu_pmic_read(offset, buf, len);
1072        }
1073        *len = 0;
1074        return 0;
1075}
1076
1077int __weak dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
1078{
1079        *size = SZ_1K;
1080
1081        return 0;
1082}
1083
1084#endif
1085
1086#endif
1087
1088static void board_copro_image_process(ulong fw_image, size_t fw_size)
1089{
1090        int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
1091
1092        if (!rproc_is_initialized())
1093                if (rproc_init()) {
1094                        printf("Remote Processor %d initialization failed\n",
1095                               id);
1096                        return;
1097                }
1098
1099        ret = rproc_load(id, fw_image, fw_size);
1100        printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
1101               id, fw_image, fw_size, ret ? " Failed!" : " Success!");
1102
1103        if (!ret)
1104                rproc_start(id);
1105}
1106
1107U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
1108