Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 4 | */ |
Philipp Tomsich | 3df6626 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 5 | |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Philipp Tomsich | 7674d16 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 11 | #include <misc.h> |
Philipp Tomsich | 25aa221 | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 12 | #include <spl.h> |
Jakob Unterwurzacher | b072063 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 13 | #include <syscon.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 14 | #include <u-boot/crc.h> |
Philipp Tomsich | 25aa221 | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 15 | #include <usb.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 16 | #include <dm/pinctrl.h> |
| 17 | #include <dm/uclass-internal.h> |
Jakob Unterwurzacher | b072063 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 18 | #include <asm/io.h> |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 19 | #include <asm/setup.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 20 | #include <asm/arch-rockchip/clock.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 21 | #include <asm/arch-rockchip/hardware.h> |
| 22 | #include <asm/arch-rockchip/grf_rk3399.h> |
| 23 | #include <asm/arch-rockchip/periph.h> |
Rohan Garg | f49ad9d | 2019-08-12 17:04:36 +0200 | [diff] [blame] | 24 | #include <asm/arch-rockchip/misc.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 25 | #include <power/regulator.h> |
Philipp Tomsich | 7674d16 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 26 | #include <u-boot/sha256.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 27 | |
Jakob Unterwurzacher | b072063 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 28 | static void setup_iodomain(void) |
| 29 | { |
| 30 | const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3; |
| 31 | struct rk3399_grf_regs *grf = |
| 32 | syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 33 | |
| 34 | /* |
| 35 | * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6). |
| 36 | * Linux assumes that PCIE_RST# works out of the box as it probes |
| 37 | * PCIe before loading the iodomain driver. |
| 38 | */ |
| 39 | rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT); |
| 40 | } |
| 41 | |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card. |
| 44 | * |
| 45 | * If bootsource is uSD-card we can assume that we want to use the |
| 46 | * SD-Card instead of the eMMC as first boot_target for distroboot. |
| 47 | * We only want to swap the defaults and not any custom environment a |
| 48 | * user has set. We exit early if a changed boot_targets environment |
| 49 | * is detected. |
| 50 | */ |
| 51 | static int setup_boottargets(void) |
| 52 | { |
| 53 | const char *boot_device = |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 54 | ofnode_read_chosen_string("u-boot,spl-boot-device"); |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 55 | char *env_default, *env; |
| 56 | |
| 57 | if (!boot_device) { |
| 58 | debug("%s: /chosen/u-boot,spl-boot-device not set\n", |
| 59 | __func__); |
| 60 | return -1; |
| 61 | } |
| 62 | debug("%s: booted from %s\n", __func__, boot_device); |
| 63 | |
| 64 | env_default = env_get_default("boot_targets"); |
| 65 | env = env_get("boot_targets"); |
| 66 | if (!env) { |
| 67 | debug("%s: boot_targets does not exist\n", __func__); |
| 68 | return -1; |
| 69 | } |
| 70 | debug("%s: boot_targets current: %s - default: %s\n", |
| 71 | __func__, env, env_default); |
| 72 | |
| 73 | if (strcmp(env_default, env) != 0) { |
| 74 | debug("%s: boot_targets not default, don't change it\n", |
| 75 | __func__); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
Quentin Schulz | 11366cd | 2022-09-15 11:14:21 +0200 | [diff] [blame] | 80 | * Make the default boot medium between SD Card and eMMC, the one that |
| 81 | * was used to load U-Boot proper. If SPI-NOR flash was used, keep |
| 82 | * original default order. |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 83 | */ |
Quentin Schulz | 11366cd | 2022-09-15 11:14:21 +0200 | [diff] [blame] | 84 | if (strcmp(boot_device, "/spi@ff1d0000/flash@0")) { |
| 85 | bool sd_booted = !strcmp(boot_device, "/mmc@fe320000"); |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 86 | char *mmc0, *mmc1; |
| 87 | |
Quentin Schulz | 11366cd | 2022-09-15 11:14:21 +0200 | [diff] [blame] | 88 | debug("%s: booted from %s\n", __func__, |
| 89 | sd_booted ? "SD-Card" : "eMMC"); |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 90 | mmc0 = strstr(env, "mmc0"); |
| 91 | mmc1 = strstr(env, "mmc1"); |
| 92 | |
| 93 | if (!mmc0 || !mmc1) { |
| 94 | debug("%s: only one mmc boot_target found\n", __func__); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | /* |
Quentin Schulz | 11366cd | 2022-09-15 11:14:21 +0200 | [diff] [blame] | 99 | * If mmc0 comes first in the boot order and U-Boot proper was |
| 100 | * loaded from mmc1, swap mmc0 and mmc1 in the list. |
| 101 | * If mmc1 comes first in the boot order and U-Boot proper was |
| 102 | * loaded from mmc0, swap mmc0 and mmc1 in the list. |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 103 | */ |
Quentin Schulz | 11366cd | 2022-09-15 11:14:21 +0200 | [diff] [blame] | 104 | if ((mmc0 < mmc1 && sd_booted) || |
| 105 | (mmc0 > mmc1 && !sd_booted)) { |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 106 | mmc0[3] = '1'; |
| 107 | mmc1[3] = '0'; |
| 108 | debug("%s: set boot_targets to: %s\n", __func__, env); |
| 109 | env_set("boot_targets", env); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Quentin Schulz | 86bd44e | 2022-09-15 11:14:25 +0200 | [diff] [blame^] | 116 | int mmc_get_env_dev(void) |
| 117 | { |
| 118 | const char *boot_device = |
| 119 | ofnode_read_chosen_string("u-boot,spl-boot-device"); |
| 120 | |
| 121 | if (!boot_device) { |
| 122 | debug("%s: /chosen/u-boot,spl-boot-device not set\n", |
| 123 | __func__); |
| 124 | return CONFIG_SYS_MMC_ENV_DEV; |
| 125 | } |
| 126 | |
| 127 | debug("%s: booted from %s\n", __func__, boot_device); |
| 128 | |
| 129 | if (!strcmp(boot_device, "/mmc@fe320000")) |
| 130 | return 1; |
| 131 | |
| 132 | if (!strcmp(boot_device, "/mmc@fe330000")) |
| 133 | return 0; |
| 134 | |
| 135 | return CONFIG_SYS_MMC_ENV_DEV; |
| 136 | } |
| 137 | |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 138 | int misc_init_r(void) |
| 139 | { |
Heiko Stuebner | 5a57b9b | 2020-06-05 12:06:39 +0200 | [diff] [blame] | 140 | const u32 cpuid_offset = 0x7; |
| 141 | const u32 cpuid_length = 0x10; |
| 142 | u8 cpuid[cpuid_length]; |
| 143 | int ret; |
Rohan Garg | f49ad9d | 2019-08-12 17:04:36 +0200 | [diff] [blame] | 144 | |
Heiko Stuebner | 5a57b9b | 2020-06-05 12:06:39 +0200 | [diff] [blame] | 145 | ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid); |
| 146 | if (ret) |
| 147 | return ret; |
Rohan Garg | f49ad9d | 2019-08-12 17:04:36 +0200 | [diff] [blame] | 148 | |
Heiko Stuebner | 5a57b9b | 2020-06-05 12:06:39 +0200 | [diff] [blame] | 149 | ret = rockchip_cpuid_set(cpuid, cpuid_length); |
| 150 | if (ret) |
| 151 | return ret; |
Rohan Garg | f49ad9d | 2019-08-12 17:04:36 +0200 | [diff] [blame] | 152 | |
Heiko Stuebner | 5a57b9b | 2020-06-05 12:06:39 +0200 | [diff] [blame] | 153 | ret = rockchip_setup_macaddr(); |
| 154 | if (ret) |
| 155 | return ret; |
Rohan Garg | f49ad9d | 2019-08-12 17:04:36 +0200 | [diff] [blame] | 156 | |
Jakob Unterwurzacher | b072063 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 157 | setup_iodomain(); |
Klaus Goger | 810182f | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 158 | setup_boottargets(); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |