blob: 614a60ee8f9fb4f70c3776ea379456d93ee029fe [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Klaus Goger81039932017-04-07 19:13:38 +02002/*
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
Klaus Goger81039932017-04-07 19:13:38 +02004 */
Philipp Tomsich3df66262017-09-29 19:27:54 +02005
Klaus Goger81039932017-04-07 19:13:38 +02006#include <common.h>
7#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Quentin Schulz0ba32122022-09-15 11:14:27 +02009#include <env_internal.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Philipp Tomsich7674d162017-04-28 17:31:44 +020012#include <misc.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010013#include <spl.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010014#include <syscon.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070015#include <u-boot/crc.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010016#include <usb.h>
Klaus Goger81039932017-04-07 19:13:38 +020017#include <dm/pinctrl.h>
18#include <dm/uclass-internal.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010019#include <asm/io.h>
Philipp Tomsich2ebe2892017-05-05 19:21:39 +020020#include <asm/setup.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080021#include <asm/arch-rockchip/clock.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080022#include <asm/arch-rockchip/hardware.h>
23#include <asm/arch-rockchip/grf_rk3399.h>
24#include <asm/arch-rockchip/periph.h>
Rohan Gargf49ad9d2019-08-12 17:04:36 +020025#include <asm/arch-rockchip/misc.h>
Klaus Goger81039932017-04-07 19:13:38 +020026#include <power/regulator.h>
Philipp Tomsich7674d162017-04-28 17:31:44 +020027#include <u-boot/sha256.h>
Klaus Goger81039932017-04-07 19:13:38 +020028
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010029static 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
Klaus Goger810182f2018-05-24 17:15:53 +020043/*
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 =
Simon Glassf3455962020-01-27 08:49:43 -070055 ofnode_read_chosen_string("u-boot,spl-boot-device");
Klaus Goger810182f2018-05-24 17:15:53 +020056 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 /*
Quentin Schulz11366cd2022-09-15 11:14:21 +020081 * 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.
Klaus Goger810182f2018-05-24 17:15:53 +020084 */
Quentin Schulz11366cd2022-09-15 11:14:21 +020085 if (strcmp(boot_device, "/spi@ff1d0000/flash@0")) {
86 bool sd_booted = !strcmp(boot_device, "/mmc@fe320000");
Klaus Goger810182f2018-05-24 17:15:53 +020087 char *mmc0, *mmc1;
88
Quentin Schulz11366cd2022-09-15 11:14:21 +020089 debug("%s: booted from %s\n", __func__,
90 sd_booted ? "SD-Card" : "eMMC");
Klaus Goger810182f2018-05-24 17:15:53 +020091 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 /*
Quentin Schulz11366cd2022-09-15 11:14:21 +0200100 * 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.
Klaus Goger810182f2018-05-24 17:15:53 +0200104 */
Quentin Schulz11366cd2022-09-15 11:14:21 +0200105 if ((mmc0 < mmc1 && sd_booted) ||
106 (mmc0 > mmc1 && !sd_booted)) {
Klaus Goger810182f2018-05-24 17:15:53 +0200107 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
Quentin Schulz86bd44e2022-09-15 11:14:25 +0200117int 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
Quentin Schulz0ba32122022-09-15 11:14:27 +0200139enum env_location arch_env_get_location(enum env_operation op, int prio)
140{
141 const char *boot_device =
142 ofnode_read_chosen_string("u-boot,spl-boot-device");
143
144 if (prio > 0)
145 return ENVL_UNKNOWN;
146
147 if (!boot_device) {
148 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
149 __func__);
150 return ENVL_NOWHERE;
151 }
152
153 debug("%s: booted from %s\n", __func__, boot_device);
154
155 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH) &&
156 !strcmp(boot_device, "/spi@ff1d0000/flash@0"))
157 return ENVL_SPI_FLASH;
158
159 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC) &&
160 (!strcmp(boot_device, "/mmc@fe320000") ||
161 !strcmp(boot_device, "/mmc@fe330000")))
162 return ENVL_MMC;
163
164 printf("%s: No environment available: booted from %s but U-Boot "
165 "config does not allow loading environment from it.",
166 __func__, boot_device);
167
168 return ENVL_NOWHERE;
169}
170
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200171int misc_init_r(void)
172{
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200173 const u32 cpuid_offset = 0x7;
174 const u32 cpuid_length = 0x10;
175 u8 cpuid[cpuid_length];
176 int ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200177
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200178 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
179 if (ret)
180 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200181
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200182 ret = rockchip_cpuid_set(cpuid, cpuid_length);
183 if (ret)
184 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200185
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200186 ret = rockchip_setup_macaddr();
187 if (ret)
188 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200189
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +0100190 setup_iodomain();
Klaus Goger810182f2018-05-24 17:15:53 +0200191 setup_boottargets();
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200192
193 return 0;
194}