l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 ZXIC Inc. |
| 3 | * |
| 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <version.h> |
| 7 | #include <asm/io.h> |
| 8 | #include <asm/string.h> |
| 9 | #include <asm/arch/cpu.h> |
| 10 | #include <image.h> |
| 11 | #include <linux/byteorder/generic.h> |
| 12 | #include <load_mode.h> |
| 13 | #include <asm/arch/top_clock.h> |
| 14 | #include <asm/arch/uart.h> |
| 15 | |
| 16 | #include "config.h" |
| 17 | #include "ddr.h" |
| 18 | #include "../drivers/efuse.h" |
| 19 | #include "../drivers/flash.h" |
| 20 | #include "pub_flags.h" |
| 21 | |
| 22 | #define FLAGS_PARTITION_ERROR (0x1111) /*·ÖÇøÒì³£*/ |
| 23 | //xf.li@20230815 add for download without powerkey start |
| 24 | #define GPIO0_REG_BASE 0x0013D000 |
| 25 | #define GPIO24_PSHOLD (24) |
| 26 | #define IO_CFG_BASE (0x0013C000+0x800) |
| 27 | //xf.li@20230815 add for download without powerkey end |
| 28 | typedef short (init_fnc_t) (void); |
| 29 | |
| 30 | int print_info(void) |
| 31 | { |
| 32 | #if defined(CFG_ZLOAD) |
| 33 | printf ("\nInc zloader 1.3.4\n"); |
| 34 | #else |
| 35 | printf ("\nInc tLoader 1.3.4\n"); |
| 36 | #endif |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void copy_to_iram1(void) |
| 41 | { |
| 42 | memcpy(0x100000, 0x8a000, 0x2000); /* TEXT_BASE=0x100000 */ |
| 43 | writel(CFG_START_STAGE1_STATE, CFG_START_STAGE1_ADDR); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | ****************************************************************************** |
| 48 | * Function: |
| 49 | * Description: |
| 50 | * Parameters: |
| 51 | * Input: |
| 52 | * Output: |
| 53 | * Returns: |
| 54 | * Others: IRAM addr is provided from ap |
| 55 | ******************************************************************************* |
| 56 | */ |
| 57 | void clear_iram( void ) |
| 58 | { |
| 59 | uint32_t i = 0; |
| 60 | |
| 61 | for( i=0x82000400; i<=0x82003400; i+=4 ) |
| 62 | { |
| 63 | writel(0x0, i); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | ****************************************************************************** |
| 69 | * Function: |
| 70 | * Description: |
| 71 | * Parameters: |
| 72 | * Input: |
| 73 | * Output: |
| 74 | * Returns: |
| 75 | * Others: |
| 76 | ******************************************************************************* |
| 77 | */ |
| 78 | void write_loader_mode(uint32_t mode) |
| 79 | { |
| 80 | writel(mode, CFG_BOOT_MODE_SAVE_ADDR_FOR_UBOOT); |
| 81 | } |
| 82 | |
| 83 | void hang (void) |
| 84 | { |
| 85 | /* call board specific hang function */ |
| 86 | for (;;); |
| 87 | } |
| 88 | |
| 89 | /******************************************************************************* |
| 90 | * Function: |
| 91 | * Description: |
| 92 | * Parameters: |
| 93 | * Input: |
| 94 | * |
| 95 | * Output: |
| 96 | * |
| 97 | * Returns: |
| 98 | * |
| 99 | * |
| 100 | * Others: |
| 101 | ********************************************************************************/ |
| 102 | void usb_apcore_poweroff(void) |
| 103 | { |
| 104 | u32 tmp; |
| 105 | |
| 106 | tmp =readl(USB_RESET); /*usb hsic reset*/ |
| 107 | tmp &= ~0x7; |
| 108 | writel(tmp, USB_RESET); |
| 109 | |
| 110 | tmp =readl(CORE_OUTPUT_SWITCH_CONFIG_REG);/*ap clk&mg control by sw*/ |
| 111 | tmp &= ~((0x1<<2)|(0x1<<5)); |
| 112 | writel(tmp, CORE_OUTPUT_SWITCH_CONFIG_REG); |
| 113 | |
| 114 | tmp =readl(CORE_OUTPUT_SW_CONFIG_REG1);/*ap clk off*/ |
| 115 | tmp &= ~(0x1<<2); |
| 116 | writel(tmp, CORE_OUTPUT_SW_CONFIG_REG1); |
| 117 | |
| 118 | tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg iso*/ |
| 119 | tmp |= (0x1<<5); |
| 120 | writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2); |
| 121 | |
| 122 | usdelay(1); |
| 123 | |
| 124 | tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg rst*/ |
| 125 | tmp |= (0x1<<4); |
| 126 | writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2); |
| 127 | |
| 128 | usdelay(1); |
| 129 | |
| 130 | tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg off*/ |
| 131 | tmp &= ~(0x1<<3); |
| 132 | writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2); |
| 133 | |
| 134 | //__REG(0x0013a0ac) &= ~((0x1<<2)|(0x1<<5)); /*ap clk&mg control by sw*/ |
| 135 | //__REG(0x0013a0b8) &= ~(0x1<<2); /*ap clk off*/ |
| 136 | //__REG(0x0013a0bc) |= (0x1<<5); /*ap mg iso*/ |
| 137 | //__REG(0x0013a0bc) |= (0x1<<4); /*ap mg rst*/ |
| 138 | //__REG(0x0013a0bc) &= ~(0x1<<3); /*ap mg off*/ |
| 139 | } |
| 140 | |
| 141 | //#ifdef CONFIG_ZX297520V3E_MDL_AB |
| 142 | #if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF) |
| 143 | void start_armboot (void) |
| 144 | { |
| 145 | int32_t ret = 0; |
| 146 | int32_t add; |
| 147 | uint32_t amt_value = 0; |
| 148 | int32_t err_flag = 0; |
| 149 | T_BOOT_TARGET bootTarget; |
| 150 | T_FLAGS flagsData; |
| 151 | T_FLAGS_INFO *fotaFlag; |
| 152 | T_BOOT_DUALSYSTEM_TYPE dualSystemType; |
| 153 | |
| 154 | clk_init(); |
| 155 | timer_init(); |
| 156 | #if CFG_PRINTF |
| 157 | uart_init(); |
| 158 | #endif |
| 159 | //xf.li@20230815 add for download without powerkey start |
| 160 | #if 1//pshold on |
| 161 | amt_value = readl(IO_CFG_BASE+0x4); |
| 162 | amt_value &= ~(3<<28); |
| 163 | writel(amt_value, (IO_CFG_BASE+0x4)); |
| 164 | /*direction: out*/ |
| 165 | amt_value = readl(GPIO0_REG_BASE + (GPIO24_PSHOLD/16)*0x40); |
| 166 | amt_value &= ~(1<<(24%16)); |
| 167 | writel(amt_value, GPIO0_REG_BASE + (GPIO24_PSHOLD/16)*0x40); |
| 168 | /*set out 1*/ |
| 169 | amt_value = readl(GPIO0_REG_BASE + 0x18+ (GPIO24_PSHOLD/16)*0x40); |
| 170 | amt_value |= (1<<(24%16)); |
| 171 | writel(amt_value, GPIO0_REG_BASE + 0x18+ (GPIO24_PSHOLD/16)*0x40); |
| 172 | #endif |
| 173 | //xf.li@20230815 add for download without powerkey end |
| 174 | print_info(); |
| 175 | |
| 176 | /* After reset, Copy zloader forcely. */ |
| 177 | /* Now IRAM1 maybe occupied by dirty data. */ |
| 178 | writel(0, CFG_START_STAGE1_ADDR); |
| 179 | amt_value = readl(CFG_AMT_MODE_SWITCH_ADDR); |
| 180 | clear_iram(); |
| 181 | writel(amt_value, CFG_AMT_MODE_SWITCH_ADDR); |
| 182 | |
| 183 | ret = board_flash_init(); |
| 184 | if(ret != 0) |
| 185 | { |
| 186 | goto error; |
| 187 | } |
| 188 | |
| 189 | efuse_init(); |
| 190 | |
| 191 | if(get_ddr_flag() == CHIP_DDR_IS_32M) |
| 192 | { |
| 193 | ddr_init(CHIP_DDR_IS_32M); |
| 194 | } |
| 195 | else if(get_ddr_flag() == CHIP_DDR_IS_64M) |
| 196 | { |
| 197 | ddr_init(CHIP_DDR_IS_64M); |
| 198 | } |
| 199 | else if(get_ddr_flag() == CHIP_DDR_IS_128M) |
| 200 | { |
| 201 | ddr_init(CHIP_DDR_IS_128M); |
| 202 | } |
| 203 | else if(get_ddr_flag() == CHIP_DDR_IS_256M) |
| 204 | { |
| 205 | ddr_init(CHIP_DDR_IS_256M); |
| 206 | } |
| 207 | else if(get_ddr_flag() == CHIP_DDR_IS_512M) |
| 208 | { |
| 209 | ddr_init(CHIP_DDR_IS_512M); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | ddr_init(CHIP_DDR_IS_128M); |
| 214 | } |
| 215 | |
| 216 | usb_apcore_poweroff(); |
| 217 | /********************* ÔËÐÐT-LOAD,½øÈëÏÂÔØÄ£Ê½ ******************/ |
| 218 | #if defined(CFG_TLOAD) |
| 219 | write_loader_mode(CFG_TLOAD_MODE); |
| 220 | |
| 221 | #if CFG_USB |
| 222 | usb_boot(SYS_USB_BASE); |
| 223 | #endif |
| 224 | |
| 225 | #if CFG_UART |
| 226 | uart_boot(); |
| 227 | #endif |
| 228 | |
| 229 | #endif /* CFG_TLOAD */ |
| 230 | |
| 231 | /********************* ÔËÐÐZ-LOAD,½øÈëÕý³£Æô¶¯Ä£Ê½ **********/ |
| 232 | #if defined(CFG_ZLOAD) |
| 233 | uint32_t uboot_entry_point = 0; |
| 234 | char boot_mode = 0; |
| 235 | |
| 236 | write_loader_mode(CFG_ZLOAD_MODE); |
| 237 | |
| 238 | /*read flags·ÖÇø*/ |
| 239 | ret = read_flags_image((uint8_t *)FLAGS_IMAGE); |
| 240 | |
| 241 | if( ret != 0 ) |
| 242 | { |
| 243 | printf("read flags partition error! Use default parameters\n"); |
| 244 | //goto error; |
| 245 | err_flag = 1; |
| 246 | |
| 247 | /*ĬÈÏflags·ÖÇøÊý¾Ý*/ |
| 248 | flagsData.magic_start = FLAGS_MAGIC; |
| 249 | flagsData.boot_fota_flag.boot_to = DUAL_SYSTEM; |
| 250 | flagsData.boot_fota_flag.fota_status = 1; |
| 251 | flagsData.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE; |
| 252 | flagsData.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE; |
| 253 | flagsData.magic_end = FLAGS_MAGIC; |
| 254 | fotaFlag = &flagsData; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | fotaFlag = (T_FLAGS_INFO *)(CFG_TEMP_ADDR); |
| 259 | } |
| 260 | |
| 261 | bootTarget = fotaFlag->boot_fota_flag.boot_to; |
| 262 | |
| 263 | writel(DUALSYSTEM_STATUS_BOOTABLE, BOOT_FLAG_ADDR);/*ĬÈÏ¿ÉÆô¶¯*/ |
| 264 | |
| 265 | if(bootTarget == DUAL_SYSTEM) |
| 266 | { |
| 267 | if (fotaFlag->boot_fota_flag.system.status == DUALSYSTEM_STATUS_UNBOOTABLE) |
| 268 | { |
| 269 | printf("dual_system status is unbootable!"); |
| 270 | goto error; |
| 271 | } |
| 272 | ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point); |
| 273 | if( ret != 0) |
| 274 | { |
| 275 | printf("read uboot1 image error, goto uboot2!"); |
| 276 | writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR); |
| 277 | ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point); |
| 278 | if( ret != 0) |
| 279 | { |
| 280 | printf("read uboot2 iamge error!"); |
| 281 | goto error; |
| 282 | } |
| 283 | } |
| 284 | else |
| 285 | printf("goto uboot!"); |
| 286 | } |
| 287 | else if(bootTarget == DUAL_SYSTEM2) |
| 288 | { |
| 289 | if (fotaFlag->boot_fota_flag.system2.status == DUALSYSTEM_STATUS_UNBOOTABLE) |
| 290 | { |
| 291 | printf("dual_system2 status is unbootable!"); |
| 292 | goto error; |
| 293 | } |
| 294 | ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point); |
| 295 | if( ret != 0) |
| 296 | { |
| 297 | printf("read uboot2 iamge error, goto uboot!"); |
| 298 | writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR); |
| 299 | ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point); |
| 300 | if( ret != 0) |
| 301 | { |
| 302 | printf("read uboot iamge error!"); |
| 303 | goto error; |
| 304 | } |
| 305 | } |
| 306 | else |
| 307 | printf("goto uboot2!"); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | printf("boot target get error!"); |
| 312 | goto error; |
| 313 | } |
| 314 | |
| 315 | if(err_flag == 1) |
| 316 | { |
| 317 | writel(FLAGS_PARTITION_ERROR, BOOT_FLAG_ADDR); |
| 318 | } |
| 319 | |
| 320 | printf("read uboot ok.\n"); |
| 321 | |
| 322 | /* set arm jump PC start code */ |
| 323 | writel(0xE59ff000, SYS_IRAM1_BASE); |
| 324 | writel(uboot_entry_point, SYS_IRAM1_BASE + 8); |
| 325 | |
| 326 | printf("start uboot...\n"); |
| 327 | /* Relese A9 Core, A9 start to run uboot right now. */ |
| 328 | writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG); |
| 329 | |
| 330 | |
| 331 | /* waiting for uboot read m0 image. */ |
| 332 | uint32_t m0_entry_point = 0; |
| 333 | ret = nand_read_m0(&m0_entry_point); |
| 334 | if(ret != 0) |
| 335 | { |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | /* M0 Core start to run cpurpm right now. */ |
| 340 | ((init_fnc_t *)m0_entry_point)(); |
| 341 | |
| 342 | #endif |
| 343 | |
| 344 | error: |
| 345 | printf("ERR\n"); |
| 346 | hang(); |
| 347 | } |
| 348 | #else |
| 349 | void start_armboot (void) |
| 350 | { |
| 351 | int32_t ret = 0; |
| 352 | uint32_t amt_value = 0; |
| 353 | |
| 354 | |
| 355 | clk_init(); |
| 356 | timer_init(); |
| 357 | #if CFG_PRINTF |
| 358 | uart_init(); |
| 359 | #endif |
| 360 | print_info(); |
| 361 | |
| 362 | /* After reset, Copy zloader forcely. */ |
| 363 | /* Now IRAM1 maybe occupied by dirty data. */ |
| 364 | writel(0, CFG_START_STAGE1_ADDR); |
| 365 | amt_value = readl(CFG_AMT_MODE_SWITCH_ADDR); |
| 366 | clear_iram(); |
| 367 | writel(amt_value, CFG_AMT_MODE_SWITCH_ADDR); |
| 368 | |
| 369 | ret = board_flash_init(); |
| 370 | if(ret != 0) |
| 371 | { |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | efuse_init(); |
| 376 | |
| 377 | if(get_ddr_flag() == CHIP_DDR_IS_32M) |
| 378 | { |
| 379 | ddr_init(CHIP_DDR_IS_32M); |
| 380 | } |
| 381 | else if(get_ddr_flag() == CHIP_DDR_IS_64M) |
| 382 | { |
| 383 | ddr_init(CHIP_DDR_IS_64M); |
| 384 | } |
| 385 | else if(get_ddr_flag() == CHIP_DDR_IS_128M) |
| 386 | { |
| 387 | ddr_init(CHIP_DDR_IS_128M); |
| 388 | } |
| 389 | else if(get_ddr_flag() == CHIP_DDR_IS_256M) |
| 390 | { |
| 391 | ddr_init(CHIP_DDR_IS_256M); |
| 392 | } |
| 393 | else if(get_ddr_flag() == CHIP_DDR_IS_512M) |
| 394 | { |
| 395 | ddr_init(CHIP_DDR_IS_512M); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | ddr_init(CHIP_DDR_IS_128M); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | usb_apcore_poweroff(); |
| 404 | /********************* ÔËÐÐT-LOAD,½øÈëÏÂÔØÄ£Ê½ ******************/ |
| 405 | #if defined(CFG_TLOAD) |
| 406 | write_loader_mode(CFG_TLOAD_MODE); |
| 407 | |
| 408 | #if CFG_USB |
| 409 | usb_boot(SYS_USB_BASE); |
| 410 | #endif |
| 411 | |
| 412 | #if CFG_UART |
| 413 | uart_boot(); |
| 414 | #endif |
| 415 | |
| 416 | #endif /* CFG_TLOAD */ |
| 417 | |
| 418 | /********************* ÔËÐÐZ-LOAD,½øÈëÕý³£Æô¶¯Ä£Ê½ **********/ |
| 419 | #if defined(CFG_ZLOAD) |
| 420 | uint32_t uboot_entry_point = 0; |
| 421 | char boot_mode = 0; |
| 422 | |
| 423 | write_loader_mode(CFG_ZLOAD_MODE); |
| 424 | |
| 425 | ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point); |
| 426 | if( ret != 0 ) |
| 427 | { |
| 428 | boot_mode = get_boot_mode(); |
| 429 | if((boot_mode == SPI_NAND_BOOT) || (boot_mode == NAND_BOOT)) |
| 430 | { |
| 431 | ret = read_uboot_image((uint8_t *)UBOOT_MIRROR_IMAGE, |
| 432 | &uboot_entry_point); |
| 433 | if(ret != 0) |
| 434 | { |
| 435 | goto error; |
| 436 | } |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | goto error; |
| 441 | } |
| 442 | } |
| 443 | //printf("read uboot ok.\n"); |
| 444 | |
| 445 | /* set arm jump PC start code */ |
| 446 | writel(0xE59ff000, SYS_IRAM1_BASE); |
| 447 | writel(uboot_entry_point, SYS_IRAM1_BASE + 8); |
| 448 | |
| 449 | printf("start uboot...\n"); |
| 450 | /* Relese A9 Core, A9 start to run uboot right now. */ |
| 451 | writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG); |
| 452 | |
| 453 | |
| 454 | /* waiting for uboot read m0 image. */ |
| 455 | uint32_t m0_entry_point = 0; |
| 456 | ret = nand_read_m0(&m0_entry_point); |
| 457 | if(ret != 0) |
| 458 | { |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | /* M0 Core start to run cpurpm right now. */ |
| 463 | ((init_fnc_t *)m0_entry_point)(); |
| 464 | |
| 465 | #endif |
| 466 | |
| 467 | error: |
| 468 | printf("ERR\n"); |
| 469 | hang(); |
| 470 | } |
| 471 | #endif |