blob: 09417af8266bddd9b23638df928bd0c8c26c552e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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"
xf.li6c8fc1e2023-08-12 00:11:09 -070020#include "pub_flags.h"
lh9ed821d2023-04-07 01:36:19 -070021
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
xf.li6c8fc1e2023-08-12 00:11:09 -0700138#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700139void 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 T_BOOT_TARGET bootTarget;
xf.li6c8fc1e2023-08-12 00:11:09 -0700146 T_FLAGS_INFO flagsData;
147 T_FLAGS_INFO *fotaFlag;
lh9ed821d2023-04-07 01:36:19 -0700148 T_BOOT_DUALSYSTEM_TYPE dualSystemType;
149
150 clk_init();
151 timer_init();
152#if CFG_PRINTF
153 uart_init();
154#endif
155 print_info();
156
157 /* After reset, Copy zloader forcely. */
158 /* Now IRAM1 maybe occupied by dirty data. */
159 writel(0, CFG_START_STAGE1_ADDR);
160 amt_value = readl(CFG_AMT_MODE_SWITCH_ADDR);
161 clear_iram();
162 writel(amt_value, CFG_AMT_MODE_SWITCH_ADDR);
163
164 ret = board_flash_init();
165 if(ret != 0)
166 {
167 goto error;
168 }
169
170 efuse_init();
171
172 if(get_ddr_flag() == CHIP_DDR_IS_32M)
173 {
174 ddr_init(CHIP_DDR_IS_32M);
175 }
176 else if(get_ddr_flag() == CHIP_DDR_IS_64M)
177 {
178 ddr_init(CHIP_DDR_IS_64M);
179 }
180 else if(get_ddr_flag() == CHIP_DDR_IS_128M)
181 {
182 ddr_init(CHIP_DDR_IS_128M);
183 }
184 else
185 {
186 ddr_init(CHIP_DDR_IS_256M);
187 }
188
189 usb_apcore_poweroff();
190/********************* ÔËÐÐT-LOAD,½øÈëÏÂÔØÄ£Ê½ ******************/
191#if defined(CFG_TLOAD)
192 write_loader_mode(CFG_TLOAD_MODE);
193
194#if CFG_USB
195 usb_boot(SYS_USB_BASE);
196#endif
197
198#if CFG_UART
199 uart_boot();
200#endif
201
202#endif /* CFG_TLOAD */
203
204/********************* ÔËÐÐZ-LOAD,½øÈëÕý³£Æô¶¯Ä£Ê½ **********/
205#if defined(CFG_ZLOAD)
206 uint32_t uboot_entry_point = 0;
207 char boot_mode = 0;
208
209 write_loader_mode(CFG_ZLOAD_MODE);
210
211 /*read flags·ÖÇø*/
212 ret = read_flags_image((uint8_t *)FLAGS_IMAGE);
213
214 if( ret != 0 )
215 {
216 printf("read flags partition error! Use default parameters\n");
217 //goto error;
218 err_flag = 1;
219
220 /*ĬÈÏflags·ÖÇøÊý¾Ý*/
xf.li6c8fc1e2023-08-12 00:11:09 -0700221 flagsData.magic_start = FLAGS_MAGIC;
222 flagsData.boot_fota_flag.boot_to = DUAL_SYSTEM;
223 flagsData.boot_fota_flag.fota_status = 1;
224 flagsData.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
225 flagsData.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
226 flagsData.magic_end = FLAGS_MAGIC;
lh9ed821d2023-04-07 01:36:19 -0700227 fotaFlag = &flagsData;
228 }
229 else
230 {
xf.li6c8fc1e2023-08-12 00:11:09 -0700231 fotaFlag = (T_FLAGS_INFO *)(CFG_TEMP_ADDR);
lh9ed821d2023-04-07 01:36:19 -0700232 }
233
xf.li6c8fc1e2023-08-12 00:11:09 -0700234 bootTarget = fotaFlag->boot_fota_flag.boot_to;
lh9ed821d2023-04-07 01:36:19 -0700235
236 writel(DUALSYSTEM_STATUS_BOOTABLE, BOOT_FLAG_ADDR);/*ĬÈÏ¿ÉÆô¶¯*/
237
238 if(bootTarget == DUAL_SYSTEM)
239 {
xf.li6c8fc1e2023-08-12 00:11:09 -0700240 if (fotaFlag->boot_fota_flag.system.status == DUALSYSTEM_STATUS_UNBOOTABLE)
lh9ed821d2023-04-07 01:36:19 -0700241 {
242 printf("dual_system status is unbootable!");
243 goto error;
244 }
245 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
246 if( ret != 0)
247 {
248 printf("read uboot1 image error, goto uboot2!");
249 writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR);
250 ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point);
251 if( ret != 0)
252 {
253 printf("read uboot2 iamge error!");
254 goto error;
255 }
256 }
257 else
258 printf("goto uboot!");
259 }
260 else if(bootTarget == DUAL_SYSTEM2)
261 {
xf.li6c8fc1e2023-08-12 00:11:09 -0700262 if (fotaFlag->boot_fota_flag.system2.status == DUALSYSTEM_STATUS_UNBOOTABLE)
lh9ed821d2023-04-07 01:36:19 -0700263 {
264 printf("dual_system2 status is unbootable!");
265 goto error;
266 }
267 ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point);
268 if( ret != 0)
269 {
270 printf("read uboot2 iamge error, goto uboot!");
271 writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR);
272 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
273 if( ret != 0)
274 {
275 printf("read uboot iamge error!");
276 goto error;
277 }
278 }
279 else
280 printf("goto uboot2!");
281 }
282 else
283 {
284 printf("boot target get error!");
285 goto error;
286 }
287
288 if(err_flag == 1)
289 {
290 writel(FLAGS_PARTITION_ERROR, BOOT_FLAG_ADDR);
291 }
292
293 printf("read uboot ok.\n");
294
295 /* set arm jump PC start code */
296 writel(0xE59ff000, SYS_IRAM1_BASE);
297 writel(uboot_entry_point, SYS_IRAM1_BASE + 8);
298
299 printf("start uboot...\n");
300 /* Relese A9 Core, A9 start to run uboot right now. */
301 writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG);
302
303
304 /* waiting for uboot read m0 image. */
305 uint32_t m0_entry_point = 0;
306 ret = nand_read_m0(&m0_entry_point);
307 if(ret != 0)
308 {
309 goto error;
310 }
311
312 /* M0 Core start to run cpurpm right now. */
313 ((init_fnc_t *)m0_entry_point)();
314
315#endif
316
317error:
318 printf("ERR\n");
319 hang();
320}
321#else
322void start_armboot (void)
323{
324 int32_t ret = 0;
325 uint32_t amt_value = 0;
326
327
328 clk_init();
329 timer_init();
330#if CFG_PRINTF
331 uart_init();
332#endif
333 print_info();
334
335 /* After reset, Copy zloader forcely. */
336 /* Now IRAM1 maybe occupied by dirty data. */
337 writel(0, CFG_START_STAGE1_ADDR);
338 amt_value = readl(CFG_AMT_MODE_SWITCH_ADDR);
339 clear_iram();
340 writel(amt_value, CFG_AMT_MODE_SWITCH_ADDR);
341
342 ret = board_flash_init();
343 if(ret != 0)
344 {
345 goto error;
346 }
347
348 efuse_init();
349
350 if(get_ddr_flag() == CHIP_DDR_IS_32M)
351 {
352 ddr_init(CHIP_DDR_IS_32M);
353 }
354 else if(get_ddr_flag() == CHIP_DDR_IS_64M)
355 {
356 ddr_init(CHIP_DDR_IS_64M);
357 }
358 else if(get_ddr_flag() == CHIP_DDR_IS_128M)
359 {
360 ddr_init(CHIP_DDR_IS_128M);
361 }
362 else
363 {
364 ddr_init(CHIP_DDR_IS_256M);
365 }
366
367
368 usb_apcore_poweroff();
369/********************* ÔËÐÐT-LOAD,½øÈëÏÂÔØÄ£Ê½ ******************/
370#if defined(CFG_TLOAD)
371 write_loader_mode(CFG_TLOAD_MODE);
372
373#if CFG_USB
374 usb_boot(SYS_USB_BASE);
375#endif
376
377#if CFG_UART
378 uart_boot();
379#endif
380
381#endif /* CFG_TLOAD */
382
383/********************* ÔËÐÐZ-LOAD,½øÈëÕý³£Æô¶¯Ä£Ê½ **********/
384#if defined(CFG_ZLOAD)
385 uint32_t uboot_entry_point = 0;
386 char boot_mode = 0;
387
388 write_loader_mode(CFG_ZLOAD_MODE);
389
390 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
391 if( ret != 0 )
392 {
393 boot_mode = get_boot_mode();
394 if((boot_mode == SPI_NAND_BOOT) || (boot_mode == NAND_BOOT))
395 {
396 ret = read_uboot_image((uint8_t *)UBOOT_MIRROR_IMAGE,
397 &uboot_entry_point);
398 if(ret != 0)
399 {
400 goto error;
401 }
402 }
403 else
404 {
405 goto error;
406 }
407 }
408 printf("read uboot ok.\n");
409
410 /* set arm jump PC start code */
411 writel(0xE59ff000, SYS_IRAM1_BASE);
412 writel(uboot_entry_point, SYS_IRAM1_BASE + 8);
413
414 printf("start uboot...\n");
415 /* Relese A9 Core, A9 start to run uboot right now. */
416 writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG);
417
418
419 /* waiting for uboot read m0 image. */
420 uint32_t m0_entry_point = 0;
421 ret = nand_read_m0(&m0_entry_point);
422 if(ret != 0)
423 {
424 goto error;
425 }
426
427 /* M0 Core start to run cpurpm right now. */
428 ((init_fnc_t *)m0_entry_point)();
429
430#endif
431
432error:
433 printf("ERR\n");
434 hang();
435}
436#endif