blob: 3d138a9d8efe7c0269a97e80b7306a9a2bc68db7 [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"
20
21#define FLAGS_PARTITION_ERROR (0x1111) /*·ÖÇøÒì³£*/
22
23typedef short (init_fnc_t) (void);
24
25int print_info(void)
26{
27#if defined(CFG_ZLOAD)
28 printf ("\nInc zloader 1.3.4\n");
29#else
30 printf ("\nInc tLoader 1.3.4\n");
31#endif
32 return 0;
33}
34
35void copy_to_iram1(void)
36{
37 memcpy(0x100000, 0x8a000, 0x2000); /* TEXT_BASE=0x100000 */
38 writel(CFG_START_STAGE1_STATE, CFG_START_STAGE1_ADDR);
39}
40
41/*
42 ******************************************************************************
43 * Function:
44 * Description:
45 * Parameters:
46 * Input:
47 * Output:
48 * Returns:
49 * Others: IRAM addr is provided from ap
50 *******************************************************************************
51 */
52void clear_iram( void )
53{
54 uint32_t i = 0;
55
56 for( i=0x82000400; i<=0x82003400; i+=4 )
57 {
58 writel(0x0, i);
59 }
60}
61
62/*
63 ******************************************************************************
64 * Function:
65 * Description:
66 * Parameters:
67 * Input:
68 * Output:
69 * Returns:
70 * Others:
71 *******************************************************************************
72 */
73 void write_loader_mode(uint32_t mode)
74{
75 writel(mode, CFG_BOOT_MODE_SAVE_ADDR_FOR_UBOOT);
76}
77
78void hang (void)
79{
80 /* call board specific hang function */
81 for (;;);
82}
83
84/*******************************************************************************
85 * Function:
86 * Description:
87 * Parameters:
88 * Input:
89 *
90 * Output:
91 *
92 * Returns:
93 *
94 *
95 * Others:
96 ********************************************************************************/
97void usb_apcore_poweroff(void)
98{
99 u32 tmp;
100
101 tmp =readl(USB_RESET); /*usb hsic reset*/
102 tmp &= ~0x7;
103 writel(tmp, USB_RESET);
104
105 tmp =readl(CORE_OUTPUT_SWITCH_CONFIG_REG);/*ap clk&mg control by sw*/
106 tmp &= ~((0x1<<2)|(0x1<<5));
107 writel(tmp, CORE_OUTPUT_SWITCH_CONFIG_REG);
108
109 tmp =readl(CORE_OUTPUT_SW_CONFIG_REG1);/*ap clk off*/
110 tmp &= ~(0x1<<2);
111 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG1);
112
113 tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg iso*/
114 tmp |= (0x1<<5);
115 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
116
117 usdelay(1);
118
119 tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg rst*/
120 tmp |= (0x1<<4);
121 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
122
123 usdelay(1);
124
125 tmp =readl(CORE_OUTPUT_SW_CONFIG_REG2);/*ap mg off*/
126 tmp &= ~(0x1<<3);
127 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
128
129 //__REG(0x0013a0ac) &= ~((0x1<<2)|(0x1<<5)); /*ap clk&mg control by sw*/
130 //__REG(0x0013a0b8) &= ~(0x1<<2); /*ap clk off*/
131 //__REG(0x0013a0bc) |= (0x1<<5); /*ap mg iso*/
132 //__REG(0x0013a0bc) |= (0x1<<4); /*ap mg rst*/
133 //__REG(0x0013a0bc) &= ~(0x1<<3); /*ap mg off*/
134}
135
136//#ifdef CONFIG_ZX297520V3E_MDL_AB
137#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC)
138void start_armboot (void)
139{
140 int32_t ret = 0;
141 int32_t add;
142 uint32_t amt_value = 0;
143 int32_t err_flag = 0;
144 T_BOOT_TARGET bootTarget;
145 T_BOOT_FOTA_FLAG flagsData;
146 T_BOOT_FOTA_FLAG *fotaFlag;
147 T_FOTA_FLAG_INFO *fotaFlagInfo;
148 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·ÖÇøÊý¾Ý*/
221 flagsData.magic = FLAGS_MAGIC;
222 flagsData.boot_to = DUAL_SYSTEM;
223 flagsData.fota_status = 1;
224 flagsData.system.status = DUALSYSTEM_STATUS_BOOTABLE;
225 flagsData.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
226 fotaFlag = &flagsData;
227 }
228 else
229 {
230 fotaFlag = (T_BOOT_FOTA_FLAG *)(CFG_TEMP_ADDR);
231 }
232
233 bootTarget = fotaFlag->boot_to;
234
235 writel(DUALSYSTEM_STATUS_BOOTABLE, BOOT_FLAG_ADDR);/*ĬÈÏ¿ÉÆô¶¯*/
236
237 if(bootTarget == DUAL_SYSTEM)
238 {
239 if (fotaFlag->system.status == DUALSYSTEM_STATUS_UNBOOTABLE)
240 {
241 printf("dual_system status is unbootable!");
242 goto error;
243 }
244 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
245 if( ret != 0)
246 {
247 printf("read uboot1 image error, goto uboot2!");
248 writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR);
249 ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point);
250 if( ret != 0)
251 {
252 printf("read uboot2 iamge error!");
253 goto error;
254 }
255 }
256 else
257 printf("goto uboot!");
258 }
259 else if(bootTarget == DUAL_SYSTEM2)
260 {
261 if (fotaFlag->system2.status == DUALSYSTEM_STATUS_UNBOOTABLE)
262 {
263 printf("dual_system2 status is unbootable!");
264 goto error;
265 }
266 ret = read_uboot_image((uint8_t *)UBOOT2_IMAGE, &uboot_entry_point);
267 if( ret != 0)
268 {
269 printf("read uboot2 iamge error, goto uboot!");
270 writel(DUALSYSTEM_STATUS_UNBOOTABLE, BOOT_FLAG_ADDR);
271 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
272 if( ret != 0)
273 {
274 printf("read uboot iamge error!");
275 goto error;
276 }
277 }
278 else
279 printf("goto uboot2!");
280 }
281 else
282 {
283 printf("boot target get error!");
284 goto error;
285 }
286
287 if(err_flag == 1)
288 {
289 writel(FLAGS_PARTITION_ERROR, BOOT_FLAG_ADDR);
290 }
291
292 printf("read uboot ok.\n");
293
294 /* set arm jump PC start code */
295 writel(0xE59ff000, SYS_IRAM1_BASE);
296 writel(uboot_entry_point, SYS_IRAM1_BASE + 8);
297
298 printf("start uboot...\n");
299 /* Relese A9 Core, A9 start to run uboot right now. */
300 writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG);
301
302
303 /* waiting for uboot read m0 image. */
304 uint32_t m0_entry_point = 0;
305 ret = nand_read_m0(&m0_entry_point);
306 if(ret != 0)
307 {
308 goto error;
309 }
310
311 /* M0 Core start to run cpurpm right now. */
312 ((init_fnc_t *)m0_entry_point)();
313
314#endif
315
316error:
317 printf("ERR\n");
318 hang();
319}
320#else
321void start_armboot (void)
322{
323 int32_t ret = 0;
324 uint32_t amt_value = 0;
325
326
327 clk_init();
328 timer_init();
329#if CFG_PRINTF
330 uart_init();
331#endif
332 print_info();
333
334 /* After reset, Copy zloader forcely. */
335 /* Now IRAM1 maybe occupied by dirty data. */
336 writel(0, CFG_START_STAGE1_ADDR);
337 amt_value = readl(CFG_AMT_MODE_SWITCH_ADDR);
338 clear_iram();
339 writel(amt_value, CFG_AMT_MODE_SWITCH_ADDR);
340
341 ret = board_flash_init();
342 if(ret != 0)
343 {
344 goto error;
345 }
346
347 efuse_init();
348
349 if(get_ddr_flag() == CHIP_DDR_IS_32M)
350 {
351 ddr_init(CHIP_DDR_IS_32M);
352 }
353 else if(get_ddr_flag() == CHIP_DDR_IS_64M)
354 {
355 ddr_init(CHIP_DDR_IS_64M);
356 }
357 else if(get_ddr_flag() == CHIP_DDR_IS_128M)
358 {
359 ddr_init(CHIP_DDR_IS_128M);
360 }
361 else
362 {
363 ddr_init(CHIP_DDR_IS_256M);
364 }
365
366
367 usb_apcore_poweroff();
368/********************* ÔËÐÐT-LOAD,½øÈëÏÂÔØÄ£Ê½ ******************/
369#if defined(CFG_TLOAD)
370 write_loader_mode(CFG_TLOAD_MODE);
371
372#if CFG_USB
373 usb_boot(SYS_USB_BASE);
374#endif
375
376#if CFG_UART
377 uart_boot();
378#endif
379
380#endif /* CFG_TLOAD */
381
382/********************* ÔËÐÐZ-LOAD,½øÈëÕý³£Æô¶¯Ä£Ê½ **********/
383#if defined(CFG_ZLOAD)
384 uint32_t uboot_entry_point = 0;
385 char boot_mode = 0;
386
387 write_loader_mode(CFG_ZLOAD_MODE);
388
389 ret = read_uboot_image((uint8_t *)UBOOT_IMAGE, &uboot_entry_point);
390 if( ret != 0 )
391 {
392 boot_mode = get_boot_mode();
393 if((boot_mode == SPI_NAND_BOOT) || (boot_mode == NAND_BOOT))
394 {
395 ret = read_uboot_image((uint8_t *)UBOOT_MIRROR_IMAGE,
396 &uboot_entry_point);
397 if(ret != 0)
398 {
399 goto error;
400 }
401 }
402 else
403 {
404 goto error;
405 }
406 }
407 printf("read uboot ok.\n");
408
409 /* set arm jump PC start code */
410 writel(0xE59ff000, SYS_IRAM1_BASE);
411 writel(uboot_entry_point, SYS_IRAM1_BASE + 8);
412
413 printf("start uboot...\n");
414 /* Relese A9 Core, A9 start to run uboot right now. */
415 writel(CPU_UFI_SW_RSTEN, CPU_A9_SUBSYS_CFG);
416
417
418 /* waiting for uboot read m0 image. */
419 uint32_t m0_entry_point = 0;
420 ret = nand_read_m0(&m0_entry_point);
421 if(ret != 0)
422 {
423 goto error;
424 }
425
426 /* M0 Core start to run cpurpm right now. */
427 ((init_fnc_t *)m0_entry_point)();
428
429#endif
430
431error:
432 printf("ERR\n");
433 hang();
434}
435#endif