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