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