uboot/board/theobroma-systems/puma_rk3399/puma-rk3399.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
   4 */
   5
   6#include <common.h>
   7#include <dm.h>
   8#include <env.h>
   9#include <env_internal.h>
  10#include <init.h>
  11#include <log.h>
  12#include <misc.h>
  13#include <spl.h>
  14#include <syscon.h>
  15#include <u-boot/crc.h>
  16#include <usb.h>
  17#include <dm/pinctrl.h>
  18#include <dm/uclass-internal.h>
  19#include <asm/io.h>
  20#include <asm/setup.h>
  21#include <asm/arch-rockchip/clock.h>
  22#include <asm/arch-rockchip/hardware.h>
  23#include <asm/arch-rockchip/grf_rk3399.h>
  24#include <asm/arch-rockchip/periph.h>
  25#include <asm/arch-rockchip/misc.h>
  26#include <power/regulator.h>
  27#include <u-boot/sha256.h>
  28
  29static void setup_iodomain(void)
  30{
  31        const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
  32        struct rk3399_grf_regs *grf =
  33            syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  34
  35        /*
  36         * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
  37         * Linux assumes that PCIE_RST# works out of the box as it probes
  38         * PCIe before loading the iodomain driver.
  39         */
  40        rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
  41}
  42
  43/*
  44 * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
  45 *
  46 * If bootsource is uSD-card we can assume that we want to use the
  47 * SD-Card instead of the eMMC as first boot_target for distroboot.
  48 * We only want to swap the defaults and not any custom environment a
  49 * user has set. We exit early if a changed boot_targets environment
  50 * is detected.
  51 */
  52static int setup_boottargets(void)
  53{
  54        const char *boot_device =
  55                ofnode_read_chosen_string("u-boot,spl-boot-device");
  56        char *env_default, *env;
  57
  58        if (!boot_device) {
  59                debug("%s: /chosen/u-boot,spl-boot-device not set\n",
  60                      __func__);
  61                return -1;
  62        }
  63        debug("%s: booted from %s\n", __func__, boot_device);
  64
  65        env_default = env_get_default("boot_targets");
  66        env = env_get("boot_targets");
  67        if (!env) {
  68                debug("%s: boot_targets does not exist\n", __func__);
  69                return -1;
  70        }
  71        debug("%s: boot_targets current: %s - default: %s\n",
  72              __func__, env, env_default);
  73
  74        if (strcmp(env_default, env) != 0) {
  75                debug("%s: boot_targets not default, don't change it\n",
  76                      __func__);
  77                return 0;
  78        }
  79
  80        /*
  81         * Make the default boot medium between SD Card and eMMC, the one that
  82         * was used to load U-Boot proper. If SPI-NOR flash was used, keep
  83         * original default order.
  84         */
  85        if (strcmp(boot_device, "/spi@ff1d0000/flash@0")) {
  86                bool sd_booted = !strcmp(boot_device, "/mmc@fe320000");
  87                char *mmc0, *mmc1;
  88
  89                debug("%s: booted from %s\n", __func__,
  90                      sd_booted ? "SD-Card" : "eMMC");
  91                mmc0 = strstr(env, "mmc0");
  92                mmc1 = strstr(env, "mmc1");
  93
  94                if (!mmc0 || !mmc1) {
  95                        debug("%s: only one mmc boot_target found\n", __func__);
  96                        return -1;
  97                }
  98
  99                /*
 100                 * If mmc0 comes first in the boot order and U-Boot proper was
 101                 * loaded from mmc1, swap mmc0 and mmc1 in the list.
 102                 * If mmc1 comes first in the boot order and U-Boot proper was
 103                 * loaded from mmc0, swap mmc0 and mmc1 in the list.
 104                 */
 105                if ((mmc0 < mmc1 && sd_booted) ||
 106                    (mmc0 > mmc1 && !sd_booted)) {
 107                        mmc0[3] = '1';
 108                        mmc1[3] = '0';
 109                        debug("%s: set boot_targets to: %s\n", __func__, env);
 110                        env_set("boot_targets", env);
 111                }
 112        }
 113
 114        return 0;
 115}
 116
 117int mmc_get_env_dev(void)
 118{
 119        const char *boot_device =
 120                ofnode_read_chosen_string("u-boot,spl-boot-device");
 121
 122        if (!boot_device) {
 123                debug("%s: /chosen/u-boot,spl-boot-device not set\n",
 124                      __func__);
 125                return CONFIG_SYS_MMC_ENV_DEV;
 126        }
 127
 128        debug("%s: booted from %s\n", __func__, boot_device);
 129
 130        if (!strcmp(boot_device, "/mmc@fe320000"))
 131                return 1;
 132
 133        if (!strcmp(boot_device, "/mmc@fe330000"))
 134                return 0;
 135
 136        return CONFIG_SYS_MMC_ENV_DEV;
 137}
 138
 139#if !IS_ENABLED(CONFIG_ENV_IS_NOWHERE)
 140#error Please enable CONFIG_ENV_IS_NOWHERE
 141#endif
 142
 143enum env_location arch_env_get_location(enum env_operation op, int prio)
 144{
 145        const char *boot_device =
 146                ofnode_read_chosen_string("u-boot,spl-boot-device");
 147
 148        if (prio > 0)
 149                return ENVL_UNKNOWN;
 150
 151        if (!boot_device) {
 152                debug("%s: /chosen/u-boot,spl-boot-device not set\n",
 153                      __func__);
 154                return ENVL_NOWHERE;
 155        }
 156
 157        debug("%s: booted from %s\n", __func__, boot_device);
 158
 159        if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH) &&
 160            !strcmp(boot_device, "/spi@ff1d0000/flash@0"))
 161                return ENVL_SPI_FLASH;
 162
 163        if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC) &&
 164            (!strcmp(boot_device, "/mmc@fe320000") ||
 165             !strcmp(boot_device, "/mmc@fe330000")))
 166                return ENVL_MMC;
 167
 168        printf("%s: No environment available: booted from %s but U-Boot "
 169               "config does not allow loading environment from it.",
 170               __func__, boot_device);
 171
 172        return ENVL_NOWHERE;
 173}
 174
 175int misc_init_r(void)
 176{
 177        const u32 cpuid_offset = 0x7;
 178        const u32 cpuid_length = 0x10;
 179        u8 cpuid[cpuid_length];
 180        int ret;
 181
 182        ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
 183        if (ret)
 184                return ret;
 185
 186        ret = rockchip_cpuid_set(cpuid, cpuid_length);
 187        if (ret)
 188                return ret;
 189
 190        ret = rockchip_setup_macaddr();
 191        if (ret)
 192                return ret;
 193
 194        setup_iodomain();
 195        setup_boottargets();
 196
 197        return 0;
 198}
 199