a7e7f022ba4467c60498e94b0f2ab5a488ab0622
[oweals/u-boot.git] / board / theobroma-systems / puma_rk3399 / puma-rk3399.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <environment.h>
9 #include <misc.h>
10 #include <spl.h>
11 #include <syscon.h>
12 #include <usb.h>
13 #include <dm/pinctrl.h>
14 #include <dm/uclass-internal.h>
15 #include <asm/io.h>
16 #include <asm/setup.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/hardware.h>
19 #include <asm/arch-rockchip/grf_rk3399.h>
20 #include <asm/arch-rockchip/periph.h>
21 #include <power/regulator.h>
22 #include <u-boot/sha256.h>
23
24 static void setup_macaddr(void)
25 {
26 #if CONFIG_IS_ENABLED(CMD_NET)
27         int ret;
28         const char *cpuid = env_get("cpuid#");
29         u8 hash[SHA256_SUM_LEN];
30         int size = sizeof(hash);
31         u8 mac_addr[6];
32
33         /* Only generate a MAC address, if none is set in the environment */
34         if (env_get("ethaddr"))
35                 return;
36
37         if (!cpuid) {
38                 debug("%s: could not retrieve 'cpuid#'\n", __func__);
39                 return;
40         }
41
42         ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
43         if (ret) {
44                 debug("%s: failed to calculate SHA256\n", __func__);
45                 return;
46         }
47
48         /* Copy 6 bytes of the hash to base the MAC address on */
49         memcpy(mac_addr, hash, 6);
50
51         /* Make this a valid MAC address and set it */
52         mac_addr[0] &= 0xfe;  /* clear multicast bit */
53         mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
54         eth_env_set_enetaddr("ethaddr", mac_addr);
55 #endif
56 }
57
58 static void setup_serial(void)
59 {
60 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
61         const u32 cpuid_offset = 0x7;
62         const u32 cpuid_length = 0x10;
63
64         struct udevice *dev;
65         int ret, i;
66         u8 cpuid[cpuid_length];
67         u8 low[cpuid_length/2], high[cpuid_length/2];
68         char cpuid_str[cpuid_length * 2 + 1];
69         u64 serialno;
70         char serialno_str[17];
71
72         /* retrieve the device */
73         ret = uclass_get_device_by_driver(UCLASS_MISC,
74                                           DM_GET_DRIVER(rockchip_efuse), &dev);
75         if (ret) {
76                 debug("%s: could not find efuse device\n", __func__);
77                 return;
78         }
79
80         /* read the cpu_id range from the efuses */
81         ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
82         if (ret) {
83                 debug("%s: reading cpuid from the efuses failed\n",
84                       __func__);
85                 return;
86         }
87
88         memset(cpuid_str, 0, sizeof(cpuid_str));
89         for (i = 0; i < 16; i++)
90                 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
91
92         debug("cpuid: %s\n", cpuid_str);
93
94         /*
95          * Mix the cpuid bytes using the same rules as in
96          *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
97          */
98         for (i = 0; i < 8; i++) {
99                 low[i] = cpuid[1 + (i << 1)];
100                 high[i] = cpuid[i << 1];
101         }
102
103         serialno = crc32_no_comp(0, low, 8);
104         serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
105         snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
106
107         env_set("cpuid#", cpuid_str);
108         env_set("serial#", serialno_str);
109 #endif
110 }
111
112 static void setup_iodomain(void)
113 {
114         const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
115         struct rk3399_grf_regs *grf =
116             syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
117
118         /*
119          * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
120          * Linux assumes that PCIE_RST# works out of the box as it probes
121          * PCIe before loading the iodomain driver.
122          */
123         rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
124 }
125
126 /*
127  * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
128  *
129  * If bootsource is uSD-card we can assume that we want to use the
130  * SD-Card instead of the eMMC as first boot_target for distroboot.
131  * We only want to swap the defaults and not any custom environment a
132  * user has set. We exit early if a changed boot_targets environment
133  * is detected.
134  */
135 static int setup_boottargets(void)
136 {
137         const char *boot_device =
138                 ofnode_get_chosen_prop("u-boot,spl-boot-device");
139         char *env_default, *env;
140
141         if (!boot_device) {
142                 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
143                       __func__);
144                 return -1;
145         }
146         debug("%s: booted from %s\n", __func__, boot_device);
147
148         env_default = env_get_default("boot_targets");
149         env = env_get("boot_targets");
150         if (!env) {
151                 debug("%s: boot_targets does not exist\n", __func__);
152                 return -1;
153         }
154         debug("%s: boot_targets current: %s - default: %s\n",
155               __func__, env, env_default);
156
157         if (strcmp(env_default, env) != 0) {
158                 debug("%s: boot_targets not default, don't change it\n",
159                       __func__);
160                 return 0;
161         }
162
163         /*
164          * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and
165          * only consider cases where the default boot-order first
166          * tries to boot from mmc0 (eMMC) and then from mmc1
167          * (i.e. external SD).
168          *
169          * In other words: the SD card will be moved to earlier in the
170          * order, if U-Boot was also loaded from the SD-card.
171          */
172         if (!strcmp(boot_device, "/dwmmc@fe320000")) {
173                 char *mmc0, *mmc1;
174
175                 debug("%s: booted from SD-Card\n", __func__);
176                 mmc0 = strstr(env, "mmc0");
177                 mmc1 = strstr(env, "mmc1");
178
179                 if (!mmc0 || !mmc1) {
180                         debug("%s: only one mmc boot_target found\n", __func__);
181                         return -1;
182                 }
183
184                 /*
185                  * If mmc0 comes first in the boot order, we need to change
186                  * the strings to make mmc1 first.
187                  */
188                 if (mmc0 < mmc1) {
189                         mmc0[3] = '1';
190                         mmc1[3] = '0';
191                         debug("%s: set boot_targets to: %s\n", __func__, env);
192                         env_set("boot_targets", env);
193                 }
194         }
195
196         return 0;
197 }
198
199 int misc_init_r(void)
200 {
201         setup_serial();
202         setup_macaddr();
203         setup_iodomain();
204         setup_boottargets();
205
206         return 0;
207 }
208
209 #ifdef CONFIG_SERIAL_TAG
210 void get_board_serial(struct tag_serialnr *serialnr)
211 {
212         char *serial_string;
213         u64 serial = 0;
214
215         serial_string = env_get("serial#");
216
217         if (serial_string)
218                 serial = simple_strtoull(serial_string, NULL, 16);
219
220         serialnr->high = (u32)(serial >> 32);
221         serialnr->low = (u32)(serial & 0xffffffff);
222 }
223 #endif
224
225 /**
226  * Switch power at an external regulator (for our root hub).
227  *
228  * @param ctrl pointer to the xHCI controller
229  * @param port port number as in the control message (one-based)
230  * @param enable boolean indicating whether to enable or disable power
231  * @return returns 0 on success, an error-code on failure
232  */
233 static int board_usb_port_power_set(struct udevice *dev, int port,
234                                     bool enable)
235 {
236 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
237         /* We start counting ports at 0, while USB counts from 1. */
238         int index = port - 1;
239         const char *regname = NULL;
240         struct udevice *regulator;
241         const char *prop = "tsd,usb-port-power";
242         int ret;
243
244         debug("%s: ctrl '%s' port %d enable %s\n", __func__,
245               dev_read_name(dev), port, enable ? "true" : "false");
246
247         ret = dev_read_string_index(dev, prop, index, &regname);
248         if (ret < 0) {
249                 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
250                       __func__, dev_read_name(dev), port, prop);
251                 return ret;
252         }
253
254         ret = regulator_get_by_platname(regname, &regulator);
255         if (ret) {
256                 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
257                       __func__, dev_read_name(dev), port, regname);
258                 return ret;
259         }
260
261         regulator_set_enable(regulator, enable);
262         return 0;
263 #else
264         return -ENOTSUPP;
265 #endif
266 }
267
268 void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
269 {
270         struct udevice *dev = hub->pusb_dev->dev;
271         struct udevice *ctrl;
272
273         /* We are only interested in our root-hubs */
274         if (usb_hub_is_root_hub(dev) == false)
275                 return;
276
277         ctrl = usb_get_bus(dev);
278         if (!ctrl) {
279                 debug("%s: could not retrieve ctrl for hub\n", __func__);
280                 return;
281         }
282
283         /*
284          * To work around an incompatibility between the single-threaded
285          * USB stack in U-Boot and (a strange low-power mode of) the USB
286          * hub we have on-module, we need to delay powering on the hub
287          * until the first time the port is probed.
288          */
289         board_usb_port_power_set(ctrl, port, true);
290 }