1 /* 2 * (C) Copyright 2016 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <dm.h> 9 #include <misc.h> 10 #include <ram.h> 11 #include <dm/pinctrl.h> 12 #include <dm/uclass-internal.h> 13 #include <asm/setup.h> 14 #include <asm/arch/periph.h> 15 #include <power/regulator.h> 16 #include <u-boot/sha256.h> 17 #include <usb.h> 18 #include <dwc3-uboot.h> 19 #include <spl.h> 20 21 DECLARE_GLOBAL_DATA_PTR; 22 23 #define RK3399_CPUID_OFF 0x7 24 #define RK3399_CPUID_LEN 0x10 25 26 int board_init(void) 27 { 28 struct udevice *pinctrl, *regulator; 29 int ret; 30 31 /* 32 * The PWM does not have decicated interrupt number in dts and can 33 * not get periph_id by pinctrl framework, so let's init them here. 34 * The PWM2 and PWM3 are for pwm regulators. 35 */ 36 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); 37 if (ret) { 38 debug("%s: Cannot find pinctrl device\n", __func__); 39 goto out; 40 } 41 42 /* Enable pwm0 for panel backlight */ 43 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM0); 44 if (ret) { 45 debug("%s PWM0 pinctrl init fail! (ret=%d)\n", __func__, ret); 46 goto out; 47 } 48 49 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2); 50 if (ret) { 51 debug("%s PWM2 pinctrl init fail!\n", __func__); 52 goto out; 53 } 54 55 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM3); 56 if (ret) { 57 debug("%s PWM3 pinctrl init fail!\n", __func__); 58 goto out; 59 } 60 61 ret = regulators_enable_boot_on(false); 62 if (ret) 63 debug("%s: Cannot enable boot on regulator\n", __func__); 64 65 ret = regulator_get_by_platname("vcc5v0_host", ®ulator); 66 if (ret) { 67 debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret); 68 goto out; 69 } 70 71 ret = regulator_set_enable(regulator, true); 72 if (ret) { 73 debug("%s vcc5v0-host-en set fail!\n", __func__); 74 goto out; 75 } 76 77 out: 78 return 0; 79 } 80 81 static void setup_macaddr(void) 82 { 83 #if CONFIG_IS_ENABLED(CMD_NET) 84 int ret; 85 const char *cpuid = env_get("cpuid#"); 86 u8 hash[SHA256_SUM_LEN]; 87 int size = sizeof(hash); 88 u8 mac_addr[6]; 89 90 /* Only generate a MAC address, if none is set in the environment */ 91 if (env_get("ethaddr")) 92 return; 93 94 if (!cpuid) { 95 debug("%s: could not retrieve 'cpuid#'\n", __func__); 96 return; 97 } 98 99 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); 100 if (ret) { 101 debug("%s: failed to calculate SHA256\n", __func__); 102 return; 103 } 104 105 /* Copy 6 bytes of the hash to base the MAC address on */ 106 memcpy(mac_addr, hash, 6); 107 108 /* Make this a valid MAC address and set it */ 109 mac_addr[0] &= 0xfe; /* clear multicast bit */ 110 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ 111 eth_env_set_enetaddr("ethaddr", mac_addr); 112 #endif 113 114 return; 115 } 116 117 static void setup_serial(void) 118 { 119 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) 120 struct udevice *dev; 121 int ret, i; 122 u8 cpuid[RK3399_CPUID_LEN]; 123 u8 low[RK3399_CPUID_LEN/2], high[RK3399_CPUID_LEN/2]; 124 char cpuid_str[RK3399_CPUID_LEN * 2 + 1]; 125 u64 serialno; 126 char serialno_str[16]; 127 128 /* retrieve the device */ 129 ret = uclass_get_device_by_driver(UCLASS_MISC, 130 DM_GET_DRIVER(rockchip_efuse), &dev); 131 if (ret) { 132 debug("%s: could not find efuse device\n", __func__); 133 return; 134 } 135 136 /* read the cpu_id range from the efuses */ 137 ret = misc_read(dev, RK3399_CPUID_OFF, &cpuid, sizeof(cpuid)); 138 if (ret) { 139 debug("%s: reading cpuid from the efuses failed\n", 140 __func__); 141 return; 142 } 143 144 memset(cpuid_str, 0, sizeof(cpuid_str)); 145 for (i = 0; i < 16; i++) 146 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); 147 148 debug("cpuid: %s\n", cpuid_str); 149 150 /* 151 * Mix the cpuid bytes using the same rules as in 152 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c 153 */ 154 for (i = 0; i < 8; i++) { 155 low[i] = cpuid[1 + (i << 1)]; 156 high[i] = cpuid[i << 1]; 157 } 158 159 serialno = crc32_no_comp(0, low, 8); 160 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 161 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); 162 163 env_set("cpuid#", cpuid_str); 164 env_set("serial#", serialno_str); 165 #endif 166 167 return; 168 } 169 170 int misc_init_r(void) 171 { 172 setup_serial(); 173 setup_macaddr(); 174 175 return 0; 176 } 177 178 #ifdef CONFIG_SERIAL_TAG 179 void get_board_serial(struct tag_serialnr *serialnr) 180 { 181 char *serial_string; 182 u64 serial = 0; 183 184 serial_string = env_get("serial#"); 185 186 if (serial_string) 187 serial = simple_strtoull(serial_string, NULL, 16); 188 189 serialnr->high = (u32)(serial >> 32); 190 serialnr->low = (u32)(serial & 0xffffffff); 191 } 192 #endif 193 194 #ifdef CONFIG_USB_DWC3 195 static struct dwc3_device dwc3_device_data = { 196 .maximum_speed = USB_SPEED_HIGH, 197 .base = 0xfe800000, 198 .dr_mode = USB_DR_MODE_PERIPHERAL, 199 .index = 0, 200 .dis_u2_susphy_quirk = 1, 201 }; 202 203 int usb_gadget_handle_interrupts(void) 204 { 205 dwc3_uboot_handle_interrupt(0); 206 return 0; 207 } 208 209 int board_usb_init(int index, enum usb_init_type init) 210 { 211 return dwc3_uboot_init(&dwc3_device_data); 212 } 213 #endif 214 215 void spl_board_init(void) 216 { 217 struct udevice *pinctrl; 218 int ret; 219 220 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); 221 if (ret) { 222 debug("%s: Cannot find pinctrl device\n", __func__); 223 goto err; 224 } 225 226 /* Enable debug UART */ 227 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); 228 if (ret) { 229 debug("%s: Failed to set up console UART\n", __func__); 230 goto err; 231 } 232 233 preloader_console_init(); 234 return; 235 err: 236 printf("%s: Error %d\n", __func__, ret); 237 238 /* No way to report error here */ 239 hang(); 240 } 241