blob: 537ce0d1d111449be8a98a32eb4549adc2ecd792 [file] [log] [blame]
Quentin Schulz00a6f602023-01-09 11:36:45 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2022 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>
Quentin Schulz47d3e0b2023-11-03 10:28:12 +010019#include <asm/gpio.h>
Quentin Schulz00a6f602023-01-09 11:36:45 +010020#include <asm/io.h>
21#include <asm/setup.h>
22#include <asm/arch-rockchip/clock.h>
23#include <asm/arch-rockchip/hardware.h>
24#include <asm/arch-rockchip/periph.h>
25#include <asm/arch-rockchip/misc.h>
Quentin Schulz47d3e0b2023-11-03 10:28:12 +010026#include <linux/delay.h>
Quentin Schulz00a6f602023-01-09 11:36:45 +010027#include <power/regulator.h>
28#include <u-boot/sha256.h>
29
30/*
31 * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
32 *
33 * If bootsource is uSD-card we can assume that we want to use the
34 * SD-Card instead of the eMMC as first boot_target for distroboot.
35 * We only want to swap the defaults and not any custom environment a
36 * user has set. We exit early if a changed boot_targets environment
37 * is detected.
38 */
39static int setup_boottargets(void)
40{
41 const char *boot_device =
42 ofnode_read_chosen_string("u-boot,spl-boot-device");
43 char *env_default, *env;
44
45 if (!boot_device) {
46 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
47 __func__);
48 return -1;
49 }
50 debug("%s: booted from %s\n", __func__, boot_device);
51
52 env_default = env_get_default("boot_targets");
53 env = env_get("boot_targets");
54 if (!env) {
55 debug("%s: boot_targets does not exist\n", __func__);
56 return -1;
57 }
58 debug("%s: boot_targets current: %s - default: %s\n",
59 __func__, env, env_default);
60
61 if (strcmp(env_default, env) != 0) {
62 debug("%s: boot_targets not default, don't change it\n",
63 __func__);
64 return 0;
65 }
66
67 /*
68 * Make the default boot medium between SD Card and eMMC, the one that
69 * was used to load U-Boot proper.
70 */
71 bool sd_booted = !strcmp(boot_device, "/mmc@ff370000");
72 char *mmc0, *mmc1;
73
74 debug("%s: booted from %s\n", __func__,
75 sd_booted ? "SD-Card" : "eMMC");
76 mmc0 = strstr(env, "mmc0");
77 mmc1 = strstr(env, "mmc1");
78
79 if (!mmc0 || !mmc1) {
80 debug("%s: only one mmc boot_target found\n", __func__);
81 return -1;
82 }
83
84 /*
85 * If mmc0 comes first in the boot order and U-Boot proper was
86 * loaded from mmc1, swap mmc0 and mmc1 in the list.
87 * If mmc1 comes first in the boot order and U-Boot proper was
88 * loaded from mmc0, swap mmc0 and mmc1 in the list.
89 */
90 if ((mmc0 < mmc1 && sd_booted) ||
91 (mmc0 > mmc1 && !sd_booted)) {
92 mmc0[3] = '1';
93 mmc1[3] = '0';
94 debug("%s: set boot_targets to: %s\n", __func__, env);
95 env_set("boot_targets", env);
96 }
97
98 return 0;
99}
100
101int mmc_get_env_dev(void)
102{
103 const char *boot_device =
104 ofnode_read_chosen_string("u-boot,spl-boot-device");
105
106 if (!boot_device) {
107 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
108 __func__);
109 return CONFIG_SYS_MMC_ENV_DEV;
110 }
111
112 debug("%s: booted from %s\n", __func__, boot_device);
113
114 if (!strcmp(boot_device, "/mmc@ff370000"))
115 return 1;
116
117 if (!strcmp(boot_device, "/mmc@ff390000"))
118 return 0;
119
120 return CONFIG_SYS_MMC_ENV_DEV;
121}
122
Quentin Schulz00a6f602023-01-09 11:36:45 +0100123enum env_location arch_env_get_location(enum env_operation op, int prio)
124{
125 const char *boot_device =
126 ofnode_read_chosen_string("u-boot,spl-boot-device");
127
128 if (prio > 0)
129 return ENVL_UNKNOWN;
130
131 if (!boot_device) {
132 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
133 __func__);
134 return ENVL_NOWHERE;
135 }
136
137 debug("%s: booted from %s\n", __func__, boot_device);
138
139 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC) &&
140 (!strcmp(boot_device, "/mmc@ff370000") ||
141 !strcmp(boot_device, "/mmc@ff390000")))
142 return ENVL_MMC;
143
144 printf("%s: No environment available: booted from %s but U-Boot "
145 "config does not allow loading environment from it.",
146 __func__, boot_device);
147
148 return ENVL_NOWHERE;
149}
150
151int misc_init_r(void)
152{
153 const u32 cpuid_offset = 0x7;
154 const u32 cpuid_length = 0x10;
155 u8 cpuid[cpuid_length];
156 int ret;
157
158 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
159 if (ret)
160 return ret;
161
162 ret = rockchip_cpuid_set(cpuid, cpuid_length);
163 if (ret)
164 return ret;
165
166 ret = rockchip_setup_macaddr();
167 if (ret)
168 return ret;
169
170 setup_boottargets();
171
172 return 0;
173}
Quentin Schulz47d3e0b2023-11-03 10:28:12 +0100174
175#define STM32_RST 100 /* GPIO3_A4 */
176#define STM32_BOOT 101 /* GPIO3_A5 */
177
178void spl_board_init(void)
179{
180 /*
181 * Glitches on STM32_BOOT and STM32_RST lines during poweroff or power
182 * on may put the STM32 companion microcontroller into DFU mode, let's
183 * always reset it into normal mode instead.
184 * Toggling the STM32_RST line is safe to do with the ATtiny companion
185 * microcontroller variant because it will not trigger an MCU reset
186 * since only a UPDI reset command will. Since a UPDI reset is difficult
187 * to mistakenly trigger, glitches to the lines are theoretically also
188 * incapable of triggering an actual ATtiny reset.
189 */
190 int ret;
191
192 ret = gpio_request(STM32_RST, "STM32_RST");
193 if (ret) {
194 debug("Failed to request STM32_RST\n");
195 return;
196 }
197
198 ret = gpio_request(STM32_BOOT, "STM32_BOOT");
199 if (ret) {
200 debug("Failed to request STM32_BOOT\n");
201 return;
202 }
203
204 /* Rely on HW pull-down for inactive level */
205 ret = gpio_direction_input(STM32_BOOT);
206 if (ret) {
207 debug("Failed to configure STM32_BOOT as input\n");
208 return;
209 }
210
211 ret = gpio_direction_output(STM32_RST, 0);
212 if (ret) {
213 debug("Failed to configure STM32_RST as output low\n");
214 return;
215 }
216
217 mdelay(1);
218
219 ret = gpio_direction_output(STM32_RST, 1);
220 if (ret) {
221 debug("Failed to configure STM32_RST as output high\n");
222 return;
223 }
224}