blob: 47259b714961add31b452e110968a2c78905359f [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>
Philipp Tomsich7674d162017-04-28 17:31:44 +02009#include <misc.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010010#include <spl.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010011#include <syscon.h>
Philipp Tomsich25aa2212017-11-22 17:15:18 +010012#include <usb.h>
Klaus Goger81039932017-04-07 19:13:38 +020013#include <dm/pinctrl.h>
14#include <dm/uclass-internal.h>
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010015#include <asm/io.h>
Philipp Tomsich2ebe2892017-05-05 19:21:39 +020016#include <asm/setup.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080017#include <asm/arch-rockchip/clock.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/hardware.h>
19#include <asm/arch-rockchip/grf_rk3399.h>
20#include <asm/arch-rockchip/periph.h>
Rohan Gargf49ad9d2019-08-12 17:04:36 +020021#include <asm/arch-rockchip/misc.h>
Klaus Goger81039932017-04-07 19:13:38 +020022#include <power/regulator.h>
Philipp Tomsich7674d162017-04-28 17:31:44 +020023#include <u-boot/sha256.h>
Klaus Goger81039932017-04-07 19:13:38 +020024
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +010025static void setup_iodomain(void)
26{
27 const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
28 struct rk3399_grf_regs *grf =
29 syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
30
31 /*
32 * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
33 * Linux assumes that PCIE_RST# works out of the box as it probes
34 * PCIe before loading the iodomain driver.
35 */
36 rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
37}
38
Klaus Goger810182f2018-05-24 17:15:53 +020039/*
40 * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
41 *
42 * If bootsource is uSD-card we can assume that we want to use the
43 * SD-Card instead of the eMMC as first boot_target for distroboot.
44 * We only want to swap the defaults and not any custom environment a
45 * user has set. We exit early if a changed boot_targets environment
46 * is detected.
47 */
48static int setup_boottargets(void)
49{
50 const char *boot_device =
51 ofnode_get_chosen_prop("u-boot,spl-boot-device");
52 char *env_default, *env;
53
54 if (!boot_device) {
55 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
56 __func__);
57 return -1;
58 }
59 debug("%s: booted from %s\n", __func__, boot_device);
60
61 env_default = env_get_default("boot_targets");
62 env = env_get("boot_targets");
63 if (!env) {
64 debug("%s: boot_targets does not exist\n", __func__);
65 return -1;
66 }
67 debug("%s: boot_targets current: %s - default: %s\n",
68 __func__, env, env_default);
69
70 if (strcmp(env_default, env) != 0) {
71 debug("%s: boot_targets not default, don't change it\n",
72 __func__);
73 return 0;
74 }
75
76 /*
77 * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and
78 * only consider cases where the default boot-order first
79 * tries to boot from mmc0 (eMMC) and then from mmc1
80 * (i.e. external SD).
81 *
82 * In other words: the SD card will be moved to earlier in the
83 * order, if U-Boot was also loaded from the SD-card.
84 */
85 if (!strcmp(boot_device, "/dwmmc@fe320000")) {
86 char *mmc0, *mmc1;
87
88 debug("%s: booted from SD-Card\n", __func__);
89 mmc0 = strstr(env, "mmc0");
90 mmc1 = strstr(env, "mmc1");
91
92 if (!mmc0 || !mmc1) {
93 debug("%s: only one mmc boot_target found\n", __func__);
94 return -1;
95 }
96
97 /*
98 * If mmc0 comes first in the boot order, we need to change
99 * the strings to make mmc1 first.
100 */
101 if (mmc0 < mmc1) {
102 mmc0[3] = '1';
103 mmc1[3] = '0';
104 debug("%s: set boot_targets to: %s\n", __func__, env);
105 env_set("boot_targets", env);
106 }
107 }
108
109 return 0;
110}
111
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200112int misc_init_r(void)
113{
Rohan Gargf49ad9d2019-08-12 17:04:36 +0200114 const u32 cpuid_offset = 0x7;
115 const u32 cpuid_length = 0x10;
116 u8 cpuid[cpuid_length];
117 int ret;
118
119 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
120 if (ret)
121 return ret;
122
123 ret = rockchip_cpuid_set(cpuid, cpuid_length);
124 if (ret)
125 return ret;
126
127 ret = rockchip_setup_macaddr();
128 if (ret)
129 return ret;
130
Jakob Unterwurzacherb0720632017-12-15 16:23:14 +0100131 setup_iodomain();
Klaus Goger810182f2018-05-24 17:15:53 +0200132 setup_boottargets();
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200133
134 return 0;
135}
136
137#ifdef CONFIG_SERIAL_TAG
138void get_board_serial(struct tag_serialnr *serialnr)
139{
140 char *serial_string;
141 u64 serial = 0;
142
Simon Glass64b723f2017-08-03 12:22:12 -0600143 serial_string = env_get("serial#");
Philipp Tomsich2ebe2892017-05-05 19:21:39 +0200144
145 if (serial_string)
146 serial = simple_strtoull(serial_string, NULL, 16);
147
148 serialnr->high = (u32)(serial >> 32);
149 serialnr->low = (u32)(serial & 0xffffffff);
150}
151#endif
Philipp Tomsich25aa2212017-11-22 17:15:18 +0100152
153/**
154 * Switch power at an external regulator (for our root hub).
155 *
156 * @param ctrl pointer to the xHCI controller
157 * @param port port number as in the control message (one-based)
158 * @param enable boolean indicating whether to enable or disable power
159 * @return returns 0 on success, an error-code on failure
160 */
161static int board_usb_port_power_set(struct udevice *dev, int port,
162 bool enable)
163{
164#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
165 /* We start counting ports at 0, while USB counts from 1. */
166 int index = port - 1;
167 const char *regname = NULL;
168 struct udevice *regulator;
169 const char *prop = "tsd,usb-port-power";
170 int ret;
171
172 debug("%s: ctrl '%s' port %d enable %s\n", __func__,
173 dev_read_name(dev), port, enable ? "true" : "false");
174
175 ret = dev_read_string_index(dev, prop, index, &regname);
176 if (ret < 0) {
177 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
178 __func__, dev_read_name(dev), port, prop);
179 return ret;
180 }
181
182 ret = regulator_get_by_platname(regname, &regulator);
183 if (ret) {
184 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
185 __func__, dev_read_name(dev), port, regname);
186 return ret;
187 }
188
189 regulator_set_enable(regulator, enable);
190 return 0;
191#else
192 return -ENOTSUPP;
193#endif
194}
195
196void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
197{
198 struct udevice *dev = hub->pusb_dev->dev;
199 struct udevice *ctrl;
200
201 /* We are only interested in our root-hubs */
202 if (usb_hub_is_root_hub(dev) == false)
203 return;
204
205 ctrl = usb_get_bus(dev);
206 if (!ctrl) {
207 debug("%s: could not retrieve ctrl for hub\n", __func__);
208 return;
209 }
210
211 /*
212 * To work around an incompatibility between the single-threaded
213 * USB stack in U-Boot and (a strange low-power mode of) the USB
214 * hub we have on-module, we need to delay powering on the hub
215 * until the first time the port is probed.
216 */
217 board_usb_port_power_set(ctrl, port, true);
218}