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