blob: deeba3084a8042d6f77116b28ca31032db0a5269 [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>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Philipp Tomsich7674d162017-04-28 17:31:44 +020011#include <misc.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010012#include <spl.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010013#include <syscon.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010015#include <usb.h>
Klaus Goger81039932017-04-07 19:13:38 +020016#include <dm/pinctrl.h>
17#include <dm/uclass-internal.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010018#include <asm/io.h>
Philipp Tomsich2ebe2892017-05-05 19:21:39 +020019#include <asm/setup.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080020#include <asm/arch-rockchip/clock.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080021#include <asm/arch-rockchip/hardware.h>
22#include <asm/arch-rockchip/grf_rk3399.h>
23#include <asm/arch-rockchip/periph.h>
Rohan Gargf49ad9d2019-08-12 17:04:36 +020024#include <asm/arch-rockchip/misc.h>
Klaus Goger81039932017-04-07 19:13:38 +020025#include <power/regulator.h>
Philipp Tomsich7674d162017-04-28 17:31:44 +020026#include <u-boot/sha256.h>
Klaus Goger81039932017-04-07 19:13:38 +020027
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010028static 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 Goger810182f2018-05-24 17:15:53 +020042/*
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 */
51static int setup_boottargets(void)
52{
53 const char *boot_device =
Simon Glassf3455962020-01-27 08:49:43 -070054 ofnode_read_chosen_string("u-boot,spl-boot-device");
Klaus Goger810182f2018-05-24 17:15:53 +020055 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 /*
Jagan Teki95b2b3e2020-05-24 20:26:18 +053080 * Only run, if booting from mmc1 (i.e. /mmc@fe320000) and
Klaus Goger810182f2018-05-24 17:15:53 +020081 * only consider cases where the default boot-order first
82 * tries to boot from mmc0 (eMMC) and then from mmc1
83 * (i.e. external SD).
84 *
85 * In other words: the SD card will be moved to earlier in the
86 * order, if U-Boot was also loaded from the SD-card.
87 */
Jagan Teki95b2b3e2020-05-24 20:26:18 +053088 if (!strcmp(boot_device, "/mmc@fe320000")) {
Klaus Goger810182f2018-05-24 17:15:53 +020089 char *mmc0, *mmc1;
90
91 debug("%s: booted from SD-Card\n", __func__);
92 mmc0 = strstr(env, "mmc0");
93 mmc1 = strstr(env, "mmc1");
94
95 if (!mmc0 || !mmc1) {
96 debug("%s: only one mmc boot_target found\n", __func__);
97 return -1;
98 }
99
100 /*
101 * If mmc0 comes first in the boot order, we need to change
102 * the strings to make mmc1 first.
103 */
104 if (mmc0 < mmc1) {
105 mmc0[3] = '1';
106 mmc1[3] = '0';
107 debug("%s: set boot_targets to: %s\n", __func__, env);
108 env_set("boot_targets", env);
109 }
110 }
111
112 return 0;
113}
114
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200115int misc_init_r(void)
116{
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200117 const u32 cpuid_offset = 0x7;
118 const u32 cpuid_length = 0x10;
119 u8 cpuid[cpuid_length];
120 int ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200121
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200122 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
123 if (ret)
124 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200125
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200126 ret = rockchip_cpuid_set(cpuid, cpuid_length);
127 if (ret)
128 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200129
Heiko Stuebner5a57b9b2020-06-05 12:06:39 +0200130 ret = rockchip_setup_macaddr();
131 if (ret)
132 return ret;
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200133
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +0100134 setup_iodomain();
Klaus Goger810182f2018-05-24 17:15:53 +0200135 setup_boottargets();
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200136
137 return 0;
138}
139
140#ifdef CONFIG_SERIAL_TAG
141void get_board_serial(struct tag_serialnr *serialnr)
142{
143 char *serial_string;
144 u64 serial = 0;
145
Simon Glass64b723f2017-08-03 12:22:12 -0600146 serial_string = env_get("serial#");
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200147
148 if (serial_string)
149 serial = simple_strtoull(serial_string, NULL, 16);
150
151 serialnr->high = (u32)(serial >> 32);
152 serialnr->low = (u32)(serial & 0xffffffff);
153}
154#endif