blob: 9b4713164d5da390d8c4411317268cad2f6e71c7 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include "global.h"
2#include <asm/arch/cpu.h>
3#include <asm/io.h>
4#include "common.h"
5
6#define ST_WAIT_SYNC_FLAG (0x0)
7#define ST_WAIT_REG_CONFIG (0x1)
8#define ST_REG_CONFIG (0x2)
9#define ST_WAIT_DOWNLOAD (0x3)
10#define ST_DOWNLOAD_HEAD (0x4)
11#define ST_DOWNLOAD_DATA (0x5)
12#define ST_DOWNLOAD_ADDR (0x6)
13
14#define SYNC_FLAG (0x5a) // "Z"
15#define REG_CONFIG_FLAG (0x6a) // ""
16#define DOWNLOAD_FLAG (0x7a) // "z"
17#define RUN_FLAG (0x8a) // ""
18#define GET_ID (0x9a) // new added
19
20extern void USB3Slave_boot(void);
21extern WORD32 usb_read(BYTE *pchBuf, WORD32 dwLen);
22extern WORD32 usb_write(BYTE *pchBuf, WORD32 dwLen);
23extern int UART_Read(char *pchBuf, int dwLen);
24extern int UART_Write(char *pchBuf, int dwLen);
25
26WORD32 Para_Section SyncACK=0xa5;
27__align(4) BYTE Para_Section CHIP_ID[8]={'Z','X','7','5','2','0','V','2'};
28WORD32 Para_Section UsbACK = 0;
29
30
31
32void Boot_Process(BYTE *pkt,int len)
33{
34 int i=0;
35 static int count=0;
36 static int addr;
37 static int value;
38 char cTmp=0;
39 //WORD32 SyncACK=0xa5;
40 int case_state;
41 if(0==len)
42 return ;
43 while(i<len)
44 {
45 cTmp=pkt[i++];
46
47 case_state = global.g_State;
48 if( ST_WAIT_SYNC_FLAG == case_state)
49 {
50 if(SYNC_FLAG==cTmp)
51 {
52 global.g_State=ST_WAIT_DOWNLOAD;
53#if CFG_USB
54 usb_write((BYTE *)&SyncACK,1);
55#else
56 UART_Write((BYTE *)&SyncACK,1);
57#endif
58 }
59 }
60 else if(ST_REG_CONFIG ==case_state)
61 {
62 if(count<4) // 4 bytes addr
63 {
64 addr<<=8;
65 addr|=cTmp;
66 }
67 else if(count<8) // 4 byte data
68 {
69 value<<=8;
70 value|=cTmp;
71 }
72
73 count++;
74
75 if(4==count)
76 {
77 if(0==addr)
78 {
79 global.g_State=ST_WAIT_DOWNLOAD;
80 UsbACK=0xA6;
81#if CFG_USB
82 usb_write(&UsbACK,1);
83#else
84 UART_Write(&UsbACK,1);
85#endif
86 }
87 }
88 else if(8==count)
89 {
90 REG32(addr)=value;
91 count=0;
92 }
93
94 }
95 else if (ST_WAIT_DOWNLOAD==case_state)
96 {
97 if(REG_CONFIG_FLAG==cTmp)
98 {
99 global.g_State=ST_REG_CONFIG;
100 count=0;
101 }
102 else if(DOWNLOAD_FLAG==cTmp)
103 {
104 global.g_State=ST_DOWNLOAD_HEAD;
105 count=0;
106 value=0;
107 }
108 else if(RUN_FLAG==cTmp) //Ö§³Ö¶à´ÎÏÂÔØ0818
109 {
110 global.g_State=ST_DOWNLOAD_ADDR;
111 count=0;
112 value=0;
113 }
114 }
115 else if( ST_DOWNLOAD_HEAD==case_state)
116 {
117 if(count<4) // 4 byte addr
118 {
119 addr<<=8;
120 addr|=cTmp;
121 }
122 else if(count<8) // 4 byte size
123 {
124 value<<=8;
125 value|=cTmp;
126 }
127
128 count++;
129
130 if(count==8)
131 {
132 count=0;
133 global.g_bootaddr=addr; //µØÖ·
134 global.g_bootsize=value;//³¤¶È
135 global.g_State=ST_DOWNLOAD_DATA;
136 UsbACK=0xA1;
137#if CFG_USB
138
139 usb_write(&UsbACK,1);
140#else
141 UART_Write(&UsbACK,1);
142#endif
143 }
144 }
145 else if( ST_DOWNLOAD_DATA==case_state)
146 {
147 if(global.g_bootsize==len)
148 {
149 if(global.g_bootaddr==(int)pkt)
150 {
151 global.g_State=ST_WAIT_DOWNLOAD;//Ö§³Ö¶à´ÎÏÂÔØ0818
152 UsbACK=0xA7;
153#if CFG_USB
154 usb_write(&UsbACK,1);
155#else
156 UART_Write(&UsbACK,1);
157#endif
158
159 return;
160 }
161 }
162 else
163 {
164 global.g_bootaddr+=len;
165 global.g_bootsize-=len;
166 return ;
167 }
168 }
169 else if( ST_DOWNLOAD_ADDR==case_state)
170 {
171 if(count<4) // 4 byte addr
172 {
173 addr<<=8;
174 addr|=cTmp;
175 }
176
177 count++;
178
179 if(count==4)
180 {
181 count=0;
182 global.g_bootaddr=addr;
183 global.g_bootfinish=1;
184 UsbACK=0xA8;
185#if CFG_USB
186 usb_write(&UsbACK,1);
187#else
188 UART_Write(&UsbACK,1);
189#endif
190 return ;
191 }
192 }
193 else
194 {
195
196 }
197
198 }
199
200}
201__align(4) BYTE Para_Section tmp[64]={0};
202
203void USB_Boot(void)
204{
205 WORD32 dwLen;
206 WORD32 dwMaxLen;
207 BYTE *pbyBuf;
208 printf("USB_Boot\n");
209 global.g_State = ST_WAIT_SYNC_FLAG;
210 pbyBuf = (BYTE*)tmp;
211
212 for(dwMaxLen = 0; dwMaxLen < 64; dwMaxLen++)
213 {
214 tmp[dwMaxLen] = 0;
215 }
216
217 dwMaxLen = 512; //¿ØÖÆÆ÷ÓÐÒªÇóbulk outʱ£¬½ÓÊÕ³¤¶È´óÓÚµÈÓÚ512
218/* µ±usb_mode =1ʱ£¬ÓÃÓÚHSIC,µ±usb_mode =0ʱ£¬ÓÃÓÚUSB */
219 if(global.g_USB_MODE == 0)
220 {
221 dwLen=USB_Check_Sync(pbyBuf,dwMaxLen);
222
223 if(0==dwLen)
224 {
225 return ;
226 }
227
228 Boot_Process(pbyBuf,dwLen);
229 }
230 while(0 == global.g_bootfinish)
231 {
232 dwLen=usb_read(pbyBuf,dwMaxLen);
233
234 Boot_Process(pbyBuf,dwLen);
235
236 if((ST_DOWNLOAD_DATA==global.g_State)&&(global.g_bootaddr!=0))
237 {
238 pbyBuf=(BYTE *)global.g_bootaddr;
239 dwMaxLen= 512;//global.g_bootsize;
240 }
241 else
242 {
243 pbyBuf=tmp;
244 dwMaxLen=512;//½ÓÊÕµØÖ·£¬³¤¶ÈÐÅÏ¢
245 }
246
247 }
248
249 if(1==global.g_bootfinish)
250 {
251#if 0 /*shield dma function*/
252 dwc_otg_core_dev_disconnet(global.g_dwc_otg_pcd_tp.core_if);
253 printf("disconnect usb controller\n");
254#endif
255 writel(0xE59ff000, SYS_IRAM1_BASE); /* Ìø×ªµ½r7Ö´ÐÐtboot */
256 writel(global.g_bootaddr, SYS_IRAM1_BASE + 8);
257 printf("Starting the tboot...\n");
258 writel(0xf, CPU_A9_SUBSYS_CFG);
259 }
260}
261
262void usb_boot(WORD32 USB_ADDR)
263{
264 global.g_USB_TIMEOUT = 0xff;
265
266 //USB3Slave_boot();
267 tsp_usb_init();
268
269 USB_Boot(); /*usb bootÏÂÔØÄ£Ê½*/
270}
271
272
273
274/*******************************************************************************
275 * Function: uart_boot
276 * Description: download from uart-bootrom
277 * Parameters:
278 * Input:
279 *
280 * Output:
281 *
282 * Returns:
283 *
284 *
285 * Others:
286 ********************************************************************************/
287
288void UART_Boot(void)
289{
290 WORD32 dwLen;
291 WORD32 dwMaxLen;
292 BYTE *pbyBuf;
293 printf("UART_Boot\n");
294 global.g_State = ST_WAIT_SYNC_FLAG;
295 pbyBuf = (BYTE*)tmp;
296
297 for(dwMaxLen = 0; dwMaxLen < 64; dwMaxLen++)
298 {
299 tmp[dwMaxLen] = 0;
300 }
301
302 dwMaxLen = 512; //¿ØÖÆÆ÷ÓÐÒªÇóbulk outʱ£¬½ÓÊÕ³¤¶È´óÓÚµÈÓÚ512
303/* µ±usb_mode =1ʱ£¬ÓÃÓÚHSIC,µ±usb_mode =0ʱ£¬ÓÃÓÚUSB */
304 if(global.g_USB_MODE == 0)
305 {
306 dwLen=UART_Check_Sync(pbyBuf,dwMaxLen);
307
308 if(0==dwLen)
309 {
310 return ;
311 }
312
313 Boot_Process(pbyBuf,dwLen);
314 }
315 while(0 == global.g_bootfinish)
316 {
317
318 dwLen=UART_Read(pbyBuf,1); //0x7a uart
319 if(*pbyBuf == 0x7a)
320 {
321 dwLen=UART_Read(pbyBuf,4);
322 global.g_bootaddr = pbyBuf[3]|pbyBuf[2]<<8|pbyBuf[1]<<16|pbyBuf[0]<<24;
323
324 dwLen=UART_Read(pbyBuf,4);
325 global.g_bootsize = pbyBuf[3]|pbyBuf[2]<<8|pbyBuf[1]<<16|pbyBuf[0]<<24;
326 }
327
328 dwLen=UART_Read(global.g_bootaddr,global.g_bootsize);
329 dwLen=UART_Read(pbyBuf,1); //0x8a
330 if(*pbyBuf == 0x8a)
331 {
332 global.g_bootfinish = 1;
333 dwLen=UART_Read(pbyBuf,4);
334 global.g_bootaddr = pbyBuf[3]|pbyBuf[2]<<8|pbyBuf[1]<<16|pbyBuf[0]<<24;
335 }
336
337 }
338
339 if(1==global.g_bootfinish)
340 {
341
342 writel(0xE59ff000, SYS_IRAM1_BASE); /* Ìø×ªµ½r7Ö´ÐÐtboot */
343 writel(global.g_bootaddr, SYS_IRAM1_BASE + 8);
344 printf("Starting the tboot ...\n");
345 writel(0xf, CPU_A9_SUBSYS_CFG);
346 }
347}
348
349
350void uart_boot(void)
351{
352 global.g_USB_TIMEOUT = 0xff;
353
354 UART_Boot(); /*uart bootÏÂÔØÄ£Ê½*/
355}
356
357