blob: 7f2dd65d446ac75ce39a5105733106f285a66651 [file] [log] [blame]
Klaus Gogera13110a2017-04-07 19:13:38 +02001/*
2 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
Philipp Tomsichfb740642017-09-29 19:27:54 +02006
Klaus Gogera13110a2017-04-07 19:13:38 +02007#include <common.h>
8#include <dm.h>
Philipp Tomsiche92e5802017-04-28 17:31:44 +02009#include <misc.h>
Philipp Tomsich614539d2017-11-22 17:15:18 +010010#include <spl.h>
11#include <usb.h>
Klaus Gogera13110a2017-04-07 19:13:38 +020012#include <dm/pinctrl.h>
13#include <dm/uclass-internal.h>
Philipp Tomsich9415b9a2017-05-05 19:21:39 +020014#include <asm/setup.h>
Klaus Gogera13110a2017-04-07 19:13:38 +020015#include <asm/arch/periph.h>
16#include <power/regulator.h>
Philipp Tomsiche92e5802017-04-28 17:31:44 +020017#include <u-boot/sha256.h>
18
Klaus Gogera13110a2017-04-07 19:13:38 +020019DECLARE_GLOBAL_DATA_PTR;
20
21int board_init(void)
22{
Klaus Gogera13110a2017-04-07 19:13:38 +020023 int ret;
24
25 /*
Philipp Tomsich0b5e7aa2017-09-29 19:28:00 +020026 * We need to call into regulators_enable_boot_on() again, as the call
27 * during SPL may have not included all regulators.
Klaus Gogera13110a2017-04-07 19:13:38 +020028 */
Philipp Tomsich0b5e7aa2017-09-29 19:28:00 +020029 ret = regulators_enable_boot_on(false);
Klaus Gogera13110a2017-04-07 19:13:38 +020030 if (ret)
Philipp Tomsich0b5e7aa2017-09-29 19:28:00 +020031 debug("%s: Cannot enable boot on regulator\n", __func__);
Klaus Gogera13110a2017-04-07 19:13:38 +020032
Klaus Gogera13110a2017-04-07 19:13:38 +020033 return 0;
34}
35
Philipp Tomsichfb740642017-09-29 19:27:54 +020036void spl_board_init(void)
37{
Philipp Tomsich482cf222017-09-29 19:28:01 +020038 int ret;
39
40 /*
41 * Turning the eMMC and SPI back on (if disabled via the Qseven
42 * BIOS_ENABLE) signal is done through a always-on regulator).
43 */
44 ret = regulators_enable_boot_on(false);
45 if (ret)
46 debug("%s: Cannot enable boot on regulator\n", __func__);
47
Philipp Tomsichfb740642017-09-29 19:27:54 +020048 preloader_console_init();
49}
50
Klaus Goger8adc9d12017-05-05 19:21:40 +020051static void setup_macaddr(void)
52{
53#if CONFIG_IS_ENABLED(CMD_NET)
54 int ret;
Simon Glass00caae62017-08-03 12:22:12 -060055 const char *cpuid = env_get("cpuid#");
Klaus Goger8adc9d12017-05-05 19:21:40 +020056 u8 hash[SHA256_SUM_LEN];
57 int size = sizeof(hash);
58 u8 mac_addr[6];
59
60 /* Only generate a MAC address, if none is set in the environment */
Simon Glass00caae62017-08-03 12:22:12 -060061 if (env_get("ethaddr"))
Klaus Goger8adc9d12017-05-05 19:21:40 +020062 return;
63
64 if (!cpuid) {
65 debug("%s: could not retrieve 'cpuid#'\n", __func__);
66 return;
67 }
68
69 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
70 if (ret) {
71 debug("%s: failed to calculate SHA256\n", __func__);
72 return;
73 }
74
75 /* Copy 6 bytes of the hash to base the MAC address on */
76 memcpy(mac_addr, hash, 6);
77
78 /* Make this a valid MAC address and set it */
79 mac_addr[0] &= 0xfe; /* clear multicast bit */
80 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
Simon Glassfd1e9592017-08-03 12:22:11 -060081 eth_env_set_enetaddr("ethaddr", mac_addr);
Klaus Goger8adc9d12017-05-05 19:21:40 +020082#endif
Klaus Goger8adc9d12017-05-05 19:21:40 +020083}
84
Philipp Tomsich9415b9a2017-05-05 19:21:39 +020085static void setup_serial(void)
86{
87#if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
Kever Yang26722332017-07-27 19:59:03 +080088 const u32 cpuid_offset = 0x7;
89 const u32 cpuid_length = 0x10;
90
Philipp Tomsich9415b9a2017-05-05 19:21:39 +020091 struct udevice *dev;
92 int ret, i;
Kever Yang26722332017-07-27 19:59:03 +080093 u8 cpuid[cpuid_length];
94 u8 low[cpuid_length/2], high[cpuid_length/2];
95 char cpuid_str[cpuid_length * 2 + 1];
Philipp Tomsich9415b9a2017-05-05 19:21:39 +020096 u64 serialno;
Klaus Goger60d7c502017-09-15 14:46:04 +020097 char serialno_str[17];
Philipp Tomsich9415b9a2017-05-05 19:21:39 +020098
Klaus Goger8adc9d12017-05-05 19:21:40 +020099 /* retrieve the device */
100 ret = uclass_get_device_by_driver(UCLASS_MISC,
101 DM_GET_DRIVER(rockchip_efuse), &dev);
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200102 if (ret) {
103 debug("%s: could not find efuse device\n", __func__);
104 return;
105 }
106
107 /* read the cpu_id range from the efuses */
Kever Yang26722332017-07-27 19:59:03 +0800108 ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200109 if (ret) {
110 debug("%s: reading cpuid from the efuses failed\n",
111 __func__);
112 return;
113 }
114
115 memset(cpuid_str, 0, sizeof(cpuid_str));
116 for (i = 0; i < 16; i++)
117 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
118
119 debug("cpuid: %s\n", cpuid_str);
120
121 /*
122 * Mix the cpuid bytes using the same rules as in
123 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
124 */
125 for (i = 0; i < 8; i++) {
126 low[i] = cpuid[1 + (i << 1)];
127 high[i] = cpuid[i << 1];
128 }
129
130 serialno = crc32_no_comp(0, low, 8);
131 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
132 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
133
Simon Glass382bee52017-08-03 12:22:09 -0600134 env_set("cpuid#", cpuid_str);
135 env_set("serial#", serialno_str);
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200136#endif
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200137}
138
139int misc_init_r(void)
140{
141 setup_serial();
Klaus Goger8adc9d12017-05-05 19:21:40 +0200142 setup_macaddr();
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200143
144 return 0;
145}
146
147#ifdef CONFIG_SERIAL_TAG
148void get_board_serial(struct tag_serialnr *serialnr)
149{
150 char *serial_string;
151 u64 serial = 0;
152
Simon Glass00caae62017-08-03 12:22:12 -0600153 serial_string = env_get("serial#");
Philipp Tomsich9415b9a2017-05-05 19:21:39 +0200154
155 if (serial_string)
156 serial = simple_strtoull(serial_string, NULL, 16);
157
158 serialnr->high = (u32)(serial >> 32);
159 serialnr->low = (u32)(serial & 0xffffffff);
160}
161#endif
Philipp Tomsich614539d2017-11-22 17:15:18 +0100162
163/**
164 * Switch power at an external regulator (for our root hub).
165 *
166 * @param ctrl pointer to the xHCI controller
167 * @param port port number as in the control message (one-based)
168 * @param enable boolean indicating whether to enable or disable power
169 * @return returns 0 on success, an error-code on failure
170 */
171static int board_usb_port_power_set(struct udevice *dev, int port,
172 bool enable)
173{
174#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
175 /* We start counting ports at 0, while USB counts from 1. */
176 int index = port - 1;
177 const char *regname = NULL;
178 struct udevice *regulator;
179 const char *prop = "tsd,usb-port-power";
180 int ret;
181
182 debug("%s: ctrl '%s' port %d enable %s\n", __func__,
183 dev_read_name(dev), port, enable ? "true" : "false");
184
185 ret = dev_read_string_index(dev, prop, index, &regname);
186 if (ret < 0) {
187 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
188 __func__, dev_read_name(dev), port, prop);
189 return ret;
190 }
191
192 ret = regulator_get_by_platname(regname, &regulator);
193 if (ret) {
194 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
195 __func__, dev_read_name(dev), port, regname);
196 return ret;
197 }
198
199 regulator_set_enable(regulator, enable);
200 return 0;
201#else
202 return -ENOTSUPP;
203#endif
204}
205
206void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
207{
208 struct udevice *dev = hub->pusb_dev->dev;
209 struct udevice *ctrl;
210
211 /* We are only interested in our root-hubs */
212 if (usb_hub_is_root_hub(dev) == false)
213 return;
214
215 ctrl = usb_get_bus(dev);
216 if (!ctrl) {
217 debug("%s: could not retrieve ctrl for hub\n", __func__);
218 return;
219 }
220
221 /*
222 * To work around an incompatibility between the single-threaded
223 * USB stack in U-Boot and (a strange low-power mode of) the USB
224 * hub we have on-module, we need to delay powering on the hub
225 * until the first time the port is probed.
226 */
227 board_usb_port_power_set(ctrl, port, true);
228}