1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 #include <common.h> 7 #include <clk.h> 8 #include <dm.h> 9 #include <debug_uart.h> 10 #include <ram.h> 11 #include <syscon.h> 12 #include <sysmem.h> 13 #include <asm/io.h> 14 #include <asm/arch/vendor.h> 15 #include <misc.h> 16 #include <asm/gpio.h> 17 #include <asm/arch/clock.h> 18 #include <asm/arch/periph.h> 19 #include <asm/arch/boot_mode.h> 20 #include <asm/arch/rk_atags.h> 21 #include <asm/arch/param.h> 22 #ifdef CONFIG_DM_CHARGE_DISPLAY 23 #include <power/charge_display.h> 24 #endif 25 #ifdef CONFIG_DM_DVFS 26 #include <dvfs.h> 27 #endif 28 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN 29 #include <io-domain.h> 30 #endif 31 #ifdef CONFIG_DM_REGULATOR 32 #include <power/regulator.h> 33 #endif 34 #ifdef CONFIG_DRM_ROCKCHIP 35 #include <video_rockchip.h> 36 #endif 37 #ifdef CONFIG_ROCKCHIP_DEBUGGER 38 #include <rockchip_debugger.h> 39 #endif 40 #include <of_live.h> 41 #include <dm/root.h> 42 #include <console.h> 43 44 DECLARE_GLOBAL_DATA_PTR; 45 /* define serialno max length, the max length is 512 Bytes 46 * The remaining bytes are used to ensure that the first 512 bytes 47 * are valid when executing 'env_set("serial#", value)'. 48 */ 49 #define VENDOR_SN_MAX 513 50 #define CPUID_LEN 0x10 51 #define CPUID_OFF 0x7 52 53 static int rockchip_set_ethaddr(void) 54 { 55 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 56 int ret; 57 u8 ethaddr[ARP_HLEN]; 58 char buf[ARP_HLEN_ASCII + 1]; 59 60 ret = vendor_storage_read(VENDOR_LAN_MAC_ID, ethaddr, sizeof(ethaddr)); 61 if (ret > 0 && is_valid_ethaddr(ethaddr)) { 62 sprintf(buf, "%pM", ethaddr); 63 env_set("ethaddr", buf); 64 } 65 #endif 66 return 0; 67 } 68 69 static int rockchip_set_serialno(void) 70 { 71 char serialno_str[VENDOR_SN_MAX]; 72 int ret = 0, i; 73 u8 cpuid[CPUID_LEN] = {0}; 74 u8 low[CPUID_LEN / 2], high[CPUID_LEN / 2]; 75 u64 serialno; 76 77 /* Read serial number from vendor storage part */ 78 memset(serialno_str, 0, VENDOR_SN_MAX); 79 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 80 ret = vendor_storage_read(VENDOR_SN_ID, serialno_str, (VENDOR_SN_MAX-1)); 81 if (ret > 0) { 82 env_set("serial#", serialno_str); 83 } else { 84 #endif 85 #ifdef CONFIG_ROCKCHIP_EFUSE 86 struct udevice *dev; 87 88 /* retrieve the device */ 89 ret = uclass_get_device_by_driver(UCLASS_MISC, 90 DM_GET_DRIVER(rockchip_efuse), &dev); 91 if (ret) { 92 printf("%s: could not find efuse device\n", __func__); 93 return ret; 94 } 95 /* read the cpu_id range from the efuses */ 96 ret = misc_read(dev, CPUID_OFF, &cpuid, sizeof(cpuid)); 97 if (ret) { 98 printf("%s: reading cpuid from the efuses failed\n", __func__); 99 return ret; 100 } 101 #else 102 /* generate random cpuid */ 103 for (i = 0; i < CPUID_LEN; i++) { 104 cpuid[i] = (u8)(rand()); 105 } 106 #endif 107 /* Generate the serial number based on CPU ID */ 108 for (i = 0; i < 8; i++) { 109 low[i] = cpuid[1 + (i << 1)]; 110 high[i] = cpuid[i << 1]; 111 } 112 serialno = crc32_no_comp(0, low, 8); 113 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 114 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); 115 116 env_set("serial#", serialno_str); 117 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 118 } 119 #endif 120 return ret; 121 } 122 123 #if defined(CONFIG_USB_FUNCTION_FASTBOOT) 124 int fb_set_reboot_flag(void) 125 { 126 printf("Setting reboot to fastboot flag ...\n"); 127 /* Set boot mode to fastboot */ 128 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); 129 130 return 0; 131 } 132 #endif 133 134 __weak int rk_board_init(void) 135 { 136 return 0; 137 } 138 139 __weak int rk_board_late_init(void) 140 { 141 return 0; 142 } 143 144 __weak int soc_clk_dump(void) 145 { 146 return 0; 147 } 148 149 __weak int set_armclk_rate(void) 150 { 151 return 0; 152 } 153 154 int board_late_init(void) 155 { 156 rockchip_set_ethaddr(); 157 rockchip_set_serialno(); 158 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 159 setup_boot_mode(); 160 #endif 161 162 #ifdef CONFIG_DM_CHARGE_DISPLAY 163 charge_display(); 164 #endif 165 166 #ifdef CONFIG_DRM_ROCKCHIP 167 rockchip_show_logo(); 168 #endif 169 170 soc_clk_dump(); 171 172 return rk_board_late_init(); 173 } 174 175 #ifdef CONFIG_USING_KERNEL_DTB 176 #include <asm/arch/resource_img.h> 177 178 int init_kernel_dtb(void) 179 { 180 int ret = 0; 181 ulong fdt_addr = 0; 182 183 fdt_addr = env_get_ulong("fdt_addr_r", 16, 0); 184 if (!fdt_addr) { 185 printf("No Found FDT Load Address.\n"); 186 return -1; 187 } 188 189 ret = rockchip_read_dtb_file((void *)fdt_addr); 190 if (ret < 0) { 191 printf("%s dtb in resource read fail\n", __func__); 192 return 0; 193 } 194 195 of_live_build((void *)fdt_addr, (struct device_node **)&gd->of_root); 196 197 dm_scan_fdt((void *)fdt_addr, false); 198 199 gd->fdt_blob = (void *)fdt_addr; 200 201 /* Reserve 'reserved-memory' */ 202 ret = boot_fdt_add_sysmem_rsv_regions((void *)gd->fdt_blob); 203 if (ret) 204 return ret; 205 206 return 0; 207 } 208 #endif 209 210 void board_env_fixup(void) 211 { 212 ulong kernel_addr_r; 213 214 if (gd->flags & GD_FLG_BL32_ENABLED) 215 return; 216 217 /* If bl32 is disabled, maybe kernel can be load to lower address. */ 218 kernel_addr_r = env_get_ulong("kernel_addr_no_bl32_r", 16, -1); 219 if (kernel_addr_r != -1) 220 env_set_hex("kernel_addr_r", kernel_addr_r); 221 } 222 223 static void early_bootrom_download(void) 224 { 225 if (!tstc()) 226 return; 227 228 gd->console_evt = getc(); 229 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 230 /* ctrl+b */ 231 if (gd->console_evt == CONSOLE_EVT_CTRL_B) { 232 printf("Enter bootrom download..."); 233 mdelay(100); 234 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 235 do_reset(NULL, 0, 0, NULL); 236 printf("failed!\n"); 237 } 238 #endif 239 } 240 241 int board_init(void) 242 { 243 int ret; 244 245 board_debug_uart_init(); 246 early_bootrom_download(); 247 248 #ifdef CONFIG_USING_KERNEL_DTB 249 init_kernel_dtb(); 250 #endif 251 /* 252 * pmucru isn't referenced on some platforms, so pmucru driver can't 253 * probe that the "assigned-clocks" is unused. 254 */ 255 clks_probe(); 256 #ifdef CONFIG_DM_REGULATOR 257 ret = regulators_enable_boot_on(false); 258 if (ret) 259 debug("%s: Cannot enable boot on regulator\n", __func__); 260 #endif 261 262 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN 263 io_domain_init(); 264 #endif 265 266 set_armclk_rate(); 267 268 #ifdef CONFIG_DM_DVFS 269 dvfs_init(true); 270 #endif 271 272 return rk_board_init(); 273 } 274 275 int interrupt_debugger_init(void) 276 { 277 int ret = 0; 278 279 #ifdef CONFIG_ROCKCHIP_DEBUGGER 280 ret = rockchip_debugger_init(); 281 #endif 282 return ret; 283 } 284 285 int board_fdt_fixup(void *blob) 286 { 287 __maybe_unused int ret = 0; 288 289 #ifdef CONFIG_DRM_ROCKCHIP 290 rockchip_display_fixup(blob); 291 #endif 292 293 #ifdef CONFIG_ROCKCHIP_RK3288 294 /* RK3288W HDMI Revision ID is 0x1A */ 295 if (readl(0xff980004) == 0x1A) { 296 ret = fdt_setprop_string(blob, 0, 297 "compatible", "rockchip,rk3288w"); 298 if (ret) 299 printf("fdt set compatible failed: %d\n", ret); 300 } 301 #endif 302 303 return ret; 304 } 305 306 void board_quiesce_devices(void) 307 { 308 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS 309 /* Destroy atags makes next warm boot safer */ 310 atags_destroy(); 311 #endif 312 } 313 314 void enable_caches(void) 315 { 316 icache_enable(); 317 dcache_enable(); 318 } 319 320 #ifdef CONFIG_LMB 321 /* 322 * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize". 323 * This makes lmb_alloc_base() always alloc from tail of sdram. 324 * If we don't assign it, bi_dram[0] is used by default and it may cause 325 * lmb_alloc_base() fail when bi_dram[0] range is small. 326 */ 327 void board_lmb_reserve(struct lmb *lmb) 328 { 329 u64 start, size; 330 char bootm_low[32]; 331 char bootm_mapsize[32]; 332 int i; 333 334 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 335 if (!gd->bd->bi_dram[i].size) 336 break; 337 } 338 339 start = gd->bd->bi_dram[i - 1].start; 340 size = gd->bd->bi_dram[i - 1].size; 341 342 /* 343 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+), 344 * otherwise "Unable to handle kernel paging request at virtual address ...". 345 * 346 * So that we hope limit highest address at 768M, but there comes the the 347 * problem: ramdisk is a compressed image and it expands after descompress, 348 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...". 349 * 350 * We make a appointment that the highest memory address is 512MB, it 351 * makes lmb alloc safer. 352 */ 353 #ifndef CONFIG_ARM64 354 if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) { 355 start = gd->bd->bi_dram[i - 2].start; 356 size = gd->bd->bi_dram[i - 2].size; 357 } 358 359 if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) 360 size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start; 361 #endif 362 sprintf(bootm_low, "0x%llx", start); 363 sprintf(bootm_mapsize, "0x%llx", size); 364 env_set("bootm_low", bootm_low); 365 env_set("bootm_mapsize", bootm_mapsize); 366 } 367 #endif 368 369 #ifdef CONFIG_SYSMEM 370 int board_sysmem_reserve(struct sysmem *sysmem) 371 { 372 struct sysmem_property prop; 373 int ret; 374 375 /* ATF */ 376 prop = param_parse_atf_mem(); 377 ret = sysmem_reserve(prop.name, prop.base, prop.size); 378 if (ret) 379 return ret; 380 381 /* PSTORE/ATAGS/SHM */ 382 prop = param_parse_common_resv_mem(); 383 ret = sysmem_reserve(prop.name, prop.base, prop.size); 384 if (ret) 385 return ret; 386 387 /* OP-TEE */ 388 prop = param_parse_optee_mem(); 389 ret = sysmem_reserve(prop.name, prop.base, prop.size); 390 if (ret) 391 return ret; 392 393 return 0; 394 } 395 #endif 396 397 #if defined(CONFIG_ROCKCHIP_PRELOADER_SERIAL) && \ 398 defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) 399 int board_init_f_init_serial(void) 400 { 401 struct tag *t = atags_get_tag(ATAG_SERIAL); 402 403 if (t) { 404 gd->serial.using_pre_serial = t->u.serial.enable; 405 gd->serial.addr = t->u.serial.addr; 406 gd->serial.baudrate = t->u.serial.baudrate; 407 gd->serial.id = t->u.serial.id; 408 409 debug("%s: enable=%d, addr=0x%lx, baudrate=%d, id=%d\n", 410 __func__, gd->serial.using_pre_serial, 411 gd->serial.addr, gd->serial.baudrate, 412 gd->serial.id); 413 } 414 415 return 0; 416 } 417 #endif 418 419 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG) 420 #include <fdt_support.h> 421 #include <usb.h> 422 #include <usb/dwc2_udc.h> 423 424 static struct dwc2_plat_otg_data otg_data = { 425 .rx_fifo_sz = 512, 426 .np_tx_fifo_sz = 16, 427 .tx_fifo_sz = 128, 428 }; 429 430 int board_usb_init(int index, enum usb_init_type init) 431 { 432 int node; 433 fdt_addr_t addr; 434 const fdt32_t *reg; 435 const void *blob = gd->fdt_blob; 436 437 /* find the usb_otg node */ 438 node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2"); 439 440 retry: 441 if (node > 0) { 442 reg = fdt_getprop(blob, node, "reg", NULL); 443 if (!reg) 444 return -EINVAL; 445 446 addr = fdt_translate_address(blob, node, reg); 447 if (addr == OF_BAD_ADDR) { 448 pr_err("Not found usb_otg address\n"); 449 return -EINVAL; 450 } 451 452 #if defined(CONFIG_ROCKCHIP_RK3288) 453 if (addr != 0xff580000) { 454 node = fdt_node_offset_by_compatible(blob, node, 455 "snps,dwc2"); 456 goto retry; 457 } 458 #endif 459 } else { 460 /* 461 * With kernel dtb support, rk3288 dwc2 otg node 462 * use the rockchip legacy dwc2 driver "dwc_otg_310" 463 * with the compatible "rockchip,rk3288_usb20_otg", 464 * and rk3368 also use the "dwc_otg_310" driver with 465 * the compatible "rockchip,rk3368-usb". 466 */ 467 #if defined(CONFIG_ROCKCHIP_RK3288) 468 node = fdt_node_offset_by_compatible(blob, -1, 469 "rockchip,rk3288_usb20_otg"); 470 #elif defined(CONFIG_ROCKCHIP_RK3368) 471 node = fdt_node_offset_by_compatible(blob, -1, 472 "rockchip,rk3368-usb"); 473 #endif 474 if (node > 0) { 475 goto retry; 476 } else { 477 pr_err("Not found usb_otg device\n"); 478 return -ENODEV; 479 } 480 } 481 482 otg_data.regs_otg = (uintptr_t)addr; 483 484 return dwc2_udc_probe(&otg_data); 485 } 486 487 int board_usb_cleanup(int index, enum usb_init_type init) 488 { 489 return 0; 490 } 491 #endif 492