1a13110a9SKlaus Goger /* 2a13110a9SKlaus Goger * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH 3a13110a9SKlaus Goger * 4a13110a9SKlaus Goger * SPDX-License-Identifier: GPL-2.0+ 5a13110a9SKlaus Goger */ 6fb740646SPhilipp Tomsich 7a13110a9SKlaus Goger #include <common.h> 8a13110a9SKlaus Goger #include <dm.h> 9*9925f1dbSAlex Kiernan #include <environment.h> 10e92e5803SPhilipp Tomsich #include <misc.h> 11614539d4SPhilipp Tomsich #include <spl.h> 12aa41220fSJakob Unterwurzacher #include <syscon.h> 13614539d4SPhilipp Tomsich #include <usb.h> 14a13110a9SKlaus Goger #include <dm/pinctrl.h> 15a13110a9SKlaus Goger #include <dm/uclass-internal.h> 16aa41220fSJakob Unterwurzacher #include <asm/io.h> 17ae0d33a7SPhilipp Tomsich #include <asm/gpio.h> 189415b9a7SPhilipp Tomsich #include <asm/setup.h> 19ae0d33a7SPhilipp Tomsich #include <asm/arch/clock.h> 20ae0d33a7SPhilipp Tomsich #include <asm/arch/cru_rk3399.h> 21aa41220fSJakob Unterwurzacher #include <asm/arch/hardware.h> 22aa41220fSJakob Unterwurzacher #include <asm/arch/grf_rk3399.h> 23a13110a9SKlaus Goger #include <asm/arch/periph.h> 24a13110a9SKlaus Goger #include <power/regulator.h> 25e92e5803SPhilipp Tomsich #include <u-boot/sha256.h> 26e92e5803SPhilipp Tomsich 27a13110a9SKlaus Goger DECLARE_GLOBAL_DATA_PTR; 28a13110a9SKlaus Goger 29a13110a9SKlaus Goger int board_init(void) 30a13110a9SKlaus Goger { 31a13110a9SKlaus Goger int ret; 32a13110a9SKlaus Goger 33a13110a9SKlaus Goger /* 340b5e7aabSPhilipp Tomsich * We need to call into regulators_enable_boot_on() again, as the call 350b5e7aabSPhilipp Tomsich * during SPL may have not included all regulators. 36a13110a9SKlaus Goger */ 370b5e7aabSPhilipp Tomsich ret = regulators_enable_boot_on(false); 38a13110a9SKlaus Goger if (ret) 390b5e7aabSPhilipp Tomsich debug("%s: Cannot enable boot on regulator\n", __func__); 40a13110a9SKlaus Goger 41a13110a9SKlaus Goger return 0; 42a13110a9SKlaus Goger } 43a13110a9SKlaus Goger 44ae0d33a7SPhilipp Tomsich static void rk3399_force_power_on_reset(void) 45ae0d33a7SPhilipp Tomsich { 46ae0d33a7SPhilipp Tomsich ofnode node; 47ae0d33a7SPhilipp Tomsich struct gpio_desc sysreset_gpio; 48ae0d33a7SPhilipp Tomsich 49ae0d33a7SPhilipp Tomsich debug("%s: trying to force a power-on reset\n", __func__); 50ae0d33a7SPhilipp Tomsich 51ae0d33a7SPhilipp Tomsich node = ofnode_path("/config"); 52ae0d33a7SPhilipp Tomsich if (!ofnode_valid(node)) { 53ae0d33a7SPhilipp Tomsich debug("%s: no /config node?\n", __func__); 54ae0d33a7SPhilipp Tomsich return; 55ae0d33a7SPhilipp Tomsich } 56ae0d33a7SPhilipp Tomsich 57ae0d33a7SPhilipp Tomsich if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0, 58ae0d33a7SPhilipp Tomsich &sysreset_gpio, GPIOD_IS_OUT)) { 59ae0d33a7SPhilipp Tomsich debug("%s: could not find a /config/sysreset-gpio\n", __func__); 60ae0d33a7SPhilipp Tomsich return; 61ae0d33a7SPhilipp Tomsich } 62ae0d33a7SPhilipp Tomsich 63ae0d33a7SPhilipp Tomsich dm_gpio_set_value(&sysreset_gpio, 1); 64ae0d33a7SPhilipp Tomsich } 65ae0d33a7SPhilipp Tomsich 66fb740646SPhilipp Tomsich void spl_board_init(void) 67fb740646SPhilipp Tomsich { 68482cf223SPhilipp Tomsich int ret; 69ae0d33a7SPhilipp Tomsich struct rk3399_cru *cru = rockchip_get_cru(); 70ae0d33a7SPhilipp Tomsich 71ae0d33a7SPhilipp Tomsich /* 72ae0d33a7SPhilipp Tomsich * The RK3399 resets only 'almost all logic' (see also in the TRM 73ae0d33a7SPhilipp Tomsich * "3.9.4 Global software reset"), when issuing a software reset. 74ae0d33a7SPhilipp Tomsich * This may cause issues during boot-up for some configurations of 75ae0d33a7SPhilipp Tomsich * the application software stack. 76ae0d33a7SPhilipp Tomsich * 77ae0d33a7SPhilipp Tomsich * To work around this, we test whether the last reset reason was 78ae0d33a7SPhilipp Tomsich * a power-on reset and (if not) issue an overtemp-reset to reset 79ae0d33a7SPhilipp Tomsich * the entire module. 80ae0d33a7SPhilipp Tomsich * 81ae0d33a7SPhilipp Tomsich * While this was previously fixed by modifying the various places 82ae0d33a7SPhilipp Tomsich * that could generate a software reset (e.g. U-Boot's sysreset 83ae0d33a7SPhilipp Tomsich * driver, the ATF or Linux), we now have it here to ensure that 84ae0d33a7SPhilipp Tomsich * we no longer have to track this through the various components. 85ae0d33a7SPhilipp Tomsich */ 86ae0d33a7SPhilipp Tomsich if (cru->glb_rst_st != 0) 87ae0d33a7SPhilipp Tomsich rk3399_force_power_on_reset(); 88482cf223SPhilipp Tomsich 89482cf223SPhilipp Tomsich /* 90482cf223SPhilipp Tomsich * Turning the eMMC and SPI back on (if disabled via the Qseven 91482cf223SPhilipp Tomsich * BIOS_ENABLE) signal is done through a always-on regulator). 92482cf223SPhilipp Tomsich */ 93482cf223SPhilipp Tomsich ret = regulators_enable_boot_on(false); 94482cf223SPhilipp Tomsich if (ret) 95482cf223SPhilipp Tomsich debug("%s: Cannot enable boot on regulator\n", __func__); 96482cf223SPhilipp Tomsich 97fb740646SPhilipp Tomsich preloader_console_init(); 98fb740646SPhilipp Tomsich } 99fb740646SPhilipp Tomsich 1008adc9d18SKlaus Goger static void setup_macaddr(void) 1018adc9d18SKlaus Goger { 1028adc9d18SKlaus Goger #if CONFIG_IS_ENABLED(CMD_NET) 1038adc9d18SKlaus Goger int ret; 10400caae6dSSimon Glass const char *cpuid = env_get("cpuid#"); 1058adc9d18SKlaus Goger u8 hash[SHA256_SUM_LEN]; 1068adc9d18SKlaus Goger int size = sizeof(hash); 1078adc9d18SKlaus Goger u8 mac_addr[6]; 1088adc9d18SKlaus Goger 1098adc9d18SKlaus Goger /* Only generate a MAC address, if none is set in the environment */ 11000caae6dSSimon Glass if (env_get("ethaddr")) 1118adc9d18SKlaus Goger return; 1128adc9d18SKlaus Goger 1138adc9d18SKlaus Goger if (!cpuid) { 1148adc9d18SKlaus Goger debug("%s: could not retrieve 'cpuid#'\n", __func__); 1158adc9d18SKlaus Goger return; 1168adc9d18SKlaus Goger } 1178adc9d18SKlaus Goger 1188adc9d18SKlaus Goger ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); 1198adc9d18SKlaus Goger if (ret) { 1208adc9d18SKlaus Goger debug("%s: failed to calculate SHA256\n", __func__); 1218adc9d18SKlaus Goger return; 1228adc9d18SKlaus Goger } 1238adc9d18SKlaus Goger 1248adc9d18SKlaus Goger /* Copy 6 bytes of the hash to base the MAC address on */ 1258adc9d18SKlaus Goger memcpy(mac_addr, hash, 6); 1268adc9d18SKlaus Goger 1278adc9d18SKlaus Goger /* Make this a valid MAC address and set it */ 1288adc9d18SKlaus Goger mac_addr[0] &= 0xfe; /* clear multicast bit */ 1298adc9d18SKlaus Goger mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ 130fd1e959eSSimon Glass eth_env_set_enetaddr("ethaddr", mac_addr); 1318adc9d18SKlaus Goger #endif 1328adc9d18SKlaus Goger } 1338adc9d18SKlaus Goger 1349415b9a7SPhilipp Tomsich static void setup_serial(void) 1359415b9a7SPhilipp Tomsich { 1369415b9a7SPhilipp Tomsich #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) 13726722335SKever Yang const u32 cpuid_offset = 0x7; 13826722335SKever Yang const u32 cpuid_length = 0x10; 13926722335SKever Yang 1409415b9a7SPhilipp Tomsich struct udevice *dev; 1419415b9a7SPhilipp Tomsich int ret, i; 14226722335SKever Yang u8 cpuid[cpuid_length]; 14326722335SKever Yang u8 low[cpuid_length/2], high[cpuid_length/2]; 14426722335SKever Yang char cpuid_str[cpuid_length * 2 + 1]; 1459415b9a7SPhilipp Tomsich u64 serialno; 14660d7c509SKlaus Goger char serialno_str[17]; 1479415b9a7SPhilipp Tomsich 1488adc9d18SKlaus Goger /* retrieve the device */ 1498adc9d18SKlaus Goger ret = uclass_get_device_by_driver(UCLASS_MISC, 1508adc9d18SKlaus Goger DM_GET_DRIVER(rockchip_efuse), &dev); 1519415b9a7SPhilipp Tomsich if (ret) { 1529415b9a7SPhilipp Tomsich debug("%s: could not find efuse device\n", __func__); 1539415b9a7SPhilipp Tomsich return; 1549415b9a7SPhilipp Tomsich } 1559415b9a7SPhilipp Tomsich 1569415b9a7SPhilipp Tomsich /* read the cpu_id range from the efuses */ 15726722335SKever Yang ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); 1589415b9a7SPhilipp Tomsich if (ret) { 1599415b9a7SPhilipp Tomsich debug("%s: reading cpuid from the efuses failed\n", 1609415b9a7SPhilipp Tomsich __func__); 1619415b9a7SPhilipp Tomsich return; 1629415b9a7SPhilipp Tomsich } 1639415b9a7SPhilipp Tomsich 1649415b9a7SPhilipp Tomsich memset(cpuid_str, 0, sizeof(cpuid_str)); 1659415b9a7SPhilipp Tomsich for (i = 0; i < 16; i++) 1669415b9a7SPhilipp Tomsich sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); 1679415b9a7SPhilipp Tomsich 1689415b9a7SPhilipp Tomsich debug("cpuid: %s\n", cpuid_str); 1699415b9a7SPhilipp Tomsich 1709415b9a7SPhilipp Tomsich /* 1719415b9a7SPhilipp Tomsich * Mix the cpuid bytes using the same rules as in 1729415b9a7SPhilipp Tomsich * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c 1739415b9a7SPhilipp Tomsich */ 1749415b9a7SPhilipp Tomsich for (i = 0; i < 8; i++) { 1759415b9a7SPhilipp Tomsich low[i] = cpuid[1 + (i << 1)]; 1769415b9a7SPhilipp Tomsich high[i] = cpuid[i << 1]; 1779415b9a7SPhilipp Tomsich } 1789415b9a7SPhilipp Tomsich 1799415b9a7SPhilipp Tomsich serialno = crc32_no_comp(0, low, 8); 1809415b9a7SPhilipp Tomsich serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 181b32b1bd1SJakob Unterwurzacher snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno); 1829415b9a7SPhilipp Tomsich 183382bee57SSimon Glass env_set("cpuid#", cpuid_str); 184382bee57SSimon Glass env_set("serial#", serialno_str); 1859415b9a7SPhilipp Tomsich #endif 1869415b9a7SPhilipp Tomsich } 1879415b9a7SPhilipp Tomsich 188aa41220fSJakob Unterwurzacher static void setup_iodomain(void) 189aa41220fSJakob Unterwurzacher { 190aa41220fSJakob Unterwurzacher const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3; 191aa41220fSJakob Unterwurzacher struct rk3399_grf_regs *grf = 192aa41220fSJakob Unterwurzacher syscon_get_first_range(ROCKCHIP_SYSCON_GRF); 193aa41220fSJakob Unterwurzacher 194aa41220fSJakob Unterwurzacher /* 195aa41220fSJakob Unterwurzacher * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6). 196aa41220fSJakob Unterwurzacher * Linux assumes that PCIE_RST# works out of the box as it probes 197aa41220fSJakob Unterwurzacher * PCIe before loading the iodomain driver. 198aa41220fSJakob Unterwurzacher */ 199aa41220fSJakob Unterwurzacher rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT); 200aa41220fSJakob Unterwurzacher } 201aa41220fSJakob Unterwurzacher 2029415b9a7SPhilipp Tomsich int misc_init_r(void) 2039415b9a7SPhilipp Tomsich { 2049415b9a7SPhilipp Tomsich setup_serial(); 2058adc9d18SKlaus Goger setup_macaddr(); 206aa41220fSJakob Unterwurzacher setup_iodomain(); 2079415b9a7SPhilipp Tomsich 2089415b9a7SPhilipp Tomsich return 0; 2099415b9a7SPhilipp Tomsich } 2109415b9a7SPhilipp Tomsich 2119415b9a7SPhilipp Tomsich #ifdef CONFIG_SERIAL_TAG 2129415b9a7SPhilipp Tomsich void get_board_serial(struct tag_serialnr *serialnr) 2139415b9a7SPhilipp Tomsich { 2149415b9a7SPhilipp Tomsich char *serial_string; 2159415b9a7SPhilipp Tomsich u64 serial = 0; 2169415b9a7SPhilipp Tomsich 21700caae6dSSimon Glass serial_string = env_get("serial#"); 2189415b9a7SPhilipp Tomsich 2199415b9a7SPhilipp Tomsich if (serial_string) 2209415b9a7SPhilipp Tomsich serial = simple_strtoull(serial_string, NULL, 16); 2219415b9a7SPhilipp Tomsich 2229415b9a7SPhilipp Tomsich serialnr->high = (u32)(serial >> 32); 2239415b9a7SPhilipp Tomsich serialnr->low = (u32)(serial & 0xffffffff); 2249415b9a7SPhilipp Tomsich } 2259415b9a7SPhilipp Tomsich #endif 226614539d4SPhilipp Tomsich 227614539d4SPhilipp Tomsich /** 228614539d4SPhilipp Tomsich * Switch power at an external regulator (for our root hub). 229614539d4SPhilipp Tomsich * 230614539d4SPhilipp Tomsich * @param ctrl pointer to the xHCI controller 231614539d4SPhilipp Tomsich * @param port port number as in the control message (one-based) 232614539d4SPhilipp Tomsich * @param enable boolean indicating whether to enable or disable power 233614539d4SPhilipp Tomsich * @return returns 0 on success, an error-code on failure 234614539d4SPhilipp Tomsich */ 235614539d4SPhilipp Tomsich static int board_usb_port_power_set(struct udevice *dev, int port, 236614539d4SPhilipp Tomsich bool enable) 237614539d4SPhilipp Tomsich { 238614539d4SPhilipp Tomsich #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR) 239614539d4SPhilipp Tomsich /* We start counting ports at 0, while USB counts from 1. */ 240614539d4SPhilipp Tomsich int index = port - 1; 241614539d4SPhilipp Tomsich const char *regname = NULL; 242614539d4SPhilipp Tomsich struct udevice *regulator; 243614539d4SPhilipp Tomsich const char *prop = "tsd,usb-port-power"; 244614539d4SPhilipp Tomsich int ret; 245614539d4SPhilipp Tomsich 246614539d4SPhilipp Tomsich debug("%s: ctrl '%s' port %d enable %s\n", __func__, 247614539d4SPhilipp Tomsich dev_read_name(dev), port, enable ? "true" : "false"); 248614539d4SPhilipp Tomsich 249614539d4SPhilipp Tomsich ret = dev_read_string_index(dev, prop, index, ®name); 250614539d4SPhilipp Tomsich if (ret < 0) { 251614539d4SPhilipp Tomsich debug("%s: ctrl '%s' port %d: no entry in '%s'\n", 252614539d4SPhilipp Tomsich __func__, dev_read_name(dev), port, prop); 253614539d4SPhilipp Tomsich return ret; 254614539d4SPhilipp Tomsich } 255614539d4SPhilipp Tomsich 256614539d4SPhilipp Tomsich ret = regulator_get_by_platname(regname, ®ulator); 257614539d4SPhilipp Tomsich if (ret) { 258614539d4SPhilipp Tomsich debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n", 259614539d4SPhilipp Tomsich __func__, dev_read_name(dev), port, regname); 260614539d4SPhilipp Tomsich return ret; 261614539d4SPhilipp Tomsich } 262614539d4SPhilipp Tomsich 263614539d4SPhilipp Tomsich regulator_set_enable(regulator, enable); 264614539d4SPhilipp Tomsich return 0; 265614539d4SPhilipp Tomsich #else 266614539d4SPhilipp Tomsich return -ENOTSUPP; 267614539d4SPhilipp Tomsich #endif 268614539d4SPhilipp Tomsich } 269614539d4SPhilipp Tomsich 270614539d4SPhilipp Tomsich void usb_hub_reset_devices(struct usb_hub_device *hub, int port) 271614539d4SPhilipp Tomsich { 272614539d4SPhilipp Tomsich struct udevice *dev = hub->pusb_dev->dev; 273614539d4SPhilipp Tomsich struct udevice *ctrl; 274614539d4SPhilipp Tomsich 275614539d4SPhilipp Tomsich /* We are only interested in our root-hubs */ 276614539d4SPhilipp Tomsich if (usb_hub_is_root_hub(dev) == false) 277614539d4SPhilipp Tomsich return; 278614539d4SPhilipp Tomsich 279614539d4SPhilipp Tomsich ctrl = usb_get_bus(dev); 280614539d4SPhilipp Tomsich if (!ctrl) { 281614539d4SPhilipp Tomsich debug("%s: could not retrieve ctrl for hub\n", __func__); 282614539d4SPhilipp Tomsich return; 283614539d4SPhilipp Tomsich } 284614539d4SPhilipp Tomsich 285614539d4SPhilipp Tomsich /* 286614539d4SPhilipp Tomsich * To work around an incompatibility between the single-threaded 287614539d4SPhilipp Tomsich * USB stack in U-Boot and (a strange low-power mode of) the USB 288614539d4SPhilipp Tomsich * hub we have on-module, we need to delay powering on the hub 289614539d4SPhilipp Tomsich * until the first time the port is probed. 290614539d4SPhilipp Tomsich */ 291614539d4SPhilipp Tomsich board_usb_port_power_set(ctrl, port, true); 292614539d4SPhilipp Tomsich } 293