1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 #if defined(MSOS_TYPE_NOS)
79
80 #include "../USBHostConfig.h"
81 #include "../../include/hal_arch.h"
82 #include "../../include/hal_cache.h"
83 #ifndef ATV_SERISE_USE
84 #include <stdio.h>
85 #endif
86 #include <string.h>
87 #ifndef ATV_SERISE_USE
88 #include "../../include/datatype.h"
89 #else
90 #include "UsbHostDataDef.h"
91 #endif
92 #include "../drvHostLib.h"
93 #include "../drvUsbMain.h"
94 #ifndef ATV_SERISE_USE
95
96 #endif
97 #ifdef ATV_SERISE_USE
98 #include "Board.h"
99 #include "common.h"
100 #include "uart.h"
101 #endif
102 #include "../../include/_drvUSB.h"
103 #include "../drvscsi.h"
104 #include "../../include/drvGlobal.h"
105
106 #include "MsCommon.h"
107 #include "MsIRQ.h"
108 #include "MsOS.h"
109
110 #define DEBUG_USB(msg) //msg
111 #define MINI_DEBUG2(x) //x;
112 #define HOSTLIB_MSG(x) MS_CRITICAL_MSG(x)
113
114 code UINT8 OTGH_GETDESCRIPTOR_DEVICE_8[] = {0x80,0x06,0x00,0x01,0x00,0x00,0x08,0x00};
115 code UINT8 OTGH_GETDESCRIPTOR_DEVICE[] = {0x80,0x06,0x00,0x01,0x00,0x00,0x12,0x00};
116 code UINT8 OTGH_GETDESCRIPTOR_CONFIG[] = {0x80,0x06,0x00,0x02,0x00,0x00,0x08,0x00};
117
118 code UINT8 OTGH_SETADDRESS[] = {0x00,0x05,03,0x00,0x00,0x00,0x00,0x00};
119
120 code UINT8 OTGH_SETCONFIGURATION[] = {0x00,0x09,0x01,0x00,0x00,0x00,0x00,0x00};
121 code UINT8 OTGH_GETDESCRIPTOR_STR[] = {0x80,0x06,0x00,0x03,0x00,0x00,0x02,0x00}; //Get String Descriptor
122 //code UINT8 OTGH_SET_FEATURE_OTG[] = {0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00};
123 //code UINT8 OTGH_SETDESCRIPTOR_DEVICE[] = {0x00,0x07,0x00,0x01,0x00,0x00,0x12,0x00}; //Set Device Descriptor
124 //code UINT8 OTGH_GETDESCRIPTOR_STR70[] = {0x80,0x06,0x70,0x03,0x00,0x00,0x06,0x00}; //Get String80 Descriptor
125 //code UINT8 OTGH_SETDESCRIPTOR_STR70[] = {0x00,0x07,0x70,0x03,0x00,0x00,0x06,0x00}; //Set String80 Descriptor
126 //code UINT8 OTGH_GETDESCRIPTOR_STR80[] = {0x80,0x06,0x80,0x03,0x00,0x00,0x12,0x00}; //Get String80 Descriptor
127 //code UINT8 OTGH_SETDESCRIPTOR_STR80[] = {0x00,0x07,0x80,0x03,0x00,0x00,0x12,0x00}; //Set String80 Descriptor
128 //code UINT8 waIntervalMap[11]={1,2,4,8,16,32,64,128,256,512,1024};
129
130
131 qHD_Structure Host20_qHD_List_Control0 __attribute__ ((aligned (32)));
132 qHD_Structure Host20_qHD_List_Control1 __attribute__ ((aligned (32)));
133 qHD_Structure Host20_qHD_List_Bulk0 __attribute__ ((aligned (32)));
134 qHD_Structure Host20_qHD_List_Bulk1 __attribute__ ((aligned (32)));
135 qHD_Structure *pHost20_qHD_List_Control0,*pHost20_qHD_List_Control1;
136 qHD_Structure *pHost20_qHD_List_Bulk0,*pHost20_qHD_List_Bulk1;
137
138 UINT8 Host20_qTD_Manage[Host20_qTD_MAX]; //1=>Free 2=>used
139 Host20_Attach_Device_Structure sAttachDevice __attribute__ ((aligned (8)));
140 BOOLEAN FirstBulkOut,FirstBulkIn;
141 //Host20_Attach_Device_Structure xdata *psDevice_AP;
142 U8 *pUsbCtrlBuf;
143 U8 UsbCtrlBuf[CONTROL_DMA_BUF_LEN] __attribute__ ((aligned (4096)));
144 UINT8 pUsbData[CONTROL_BUF_LEN] __attribute__ ((aligned (16))) ;
145 U8 QtdBuf[Host20_qTD_SIZE*Host20_qTD_MAX+0x20] __attribute__ ((aligned (32)));
146 U8 *Host20_STRUCTURE_qTD_BASE_ADDRESS,*Host20_STRUCTURE_qHD_BASE_ADDRESS;
147 U8 *qTD_Base_Buf;
148
149 UINT8 gSpeed;
150 U16 TotalBytes;
151 //volatile UINT32 gwLastqTDSendOK;
152 extern U8 gUsbTimeout;
153 extern U8 gUsbStatus;
154 U8 DataToggleBackup;
155 extern U8 RecoveryFlag;
156 //extern BOOLEAN gDeviceFatalError;
157 extern U8 gUsbDeviceState;
158 extern U8 NowIsHub;
159 extern U16 MDrv_USBGetChipID(void);
160 extern U8 MDrv_USBGetChipVersion(void);
161
162 U8 usb_temp_buf[Scsi_Max_Transfer_Len] __attribute__ ((aligned (128)));
163
164 #define USB_DMA_SIGNATURE1 0x55661234
165
166 extern void msAPI_Timer_ResetWDT(void);
167 #if defined(CPU_TYPE_MIPS) || defined(CPU_TYPE_ARM)
168 extern void MAsm_CPU_Sync(void);
169 #endif
170
171 void EnterTestMode(void);
172
173 //************************************************************************************************************
174 //************************************************************************************************************
175 // *** Group-1:Main Function ***
176 //*************************************************************************************************************
177 //************************************************************************************************************
mwHost20Port(int bOffset)178 unsigned char mwHost20Port(int bOffset)
179 {
180 return UHC_READXBYTE((U8) bOffset);
181 }
mwHost20Port_wr(int bOffset,int value)182 void mwHost20Port_wr(int bOffset,int value)
183 {
184 UHC_SETXBYTE((U8) bOffset, (U8) value);
185 }
186
mwHost20Bit_Rd(int bByte,int wBitNum)187 int mwHost20Bit_Rd(int bByte,int wBitNum)
188 {
189 return (mwHost20Port(bByte)&wBitNum);
190 }
mwHost20Bit_Set(int bByte,int wBitNum)191 void mwHost20Bit_Set(int bByte,int wBitNum)
192 {
193 U8 temp;
194 temp=mwHost20Port(bByte);
195 temp|=wBitNum;
196 mwHost20Port_wr(bByte,temp);
197
198 }
mwHost20Bit_Clr(int bByte,int wBitNum)199 void mwHost20Bit_Clr(int bByte,int wBitNum)
200 {
201 U8 temp;
202 temp=mwHost20Port(bByte);
203 temp&=~wBitNum;
204
205 mwHost20Port_wr(bByte,temp);
206 }
207
208
209 //====================================================================
210 // * Function Name: flib_OTGH_Init
211 // * Description: Init the Host HW and prepare the ED/TD
212 // <1>.Init All the Data Structure
213 // <1.1>.Build control list
214 // <1.2>.Build Bulk list
215 // <1.3>.Build Interrupt list
216 // <1.4>.Build ISO list (Reserved)
217 // <2>.Reset the chip
218 // <3>.Set HW register
219 // <3.1>.Enable FIFO-Enable(0x100->Bit5) & FPGA-Mode-Half-Speed (0x100->Bit1)
220 // <3.2>.Enable interrupts
221 // <3.3>.Periodic List Base Address register
222 // <3.4>.USBCMD (Interrupt/Threshod/Frame List/Run-Stop)
223 //
224 // * Input: wTimeWaitForConnect_ms:The time of waitting for connecting
225 // * OutPut: 0:Device do not connect
226 // 1:Host init ok
227 // 2:Bus Rest Fail
228 //====================================================================
229 #ifdef ATV_SERISE_USE
230 extern U8 jpCVBS5_Get_Device_Define(void);
231 #endif
232
233 #define wTimeWaitForConnect_ms 1500
234
flib_OTGH_Init(UINT8 wForDevice_B)235 UINT8 flib_OTGH_Init(UINT8 wForDevice_B)
236 {
237 UINT8 wValue;
238 UINT16 wTimer_ms;
239
240 MsOS_DelayTask(2);//2ms
241 UTMI_ANDXBYTE(0,0xfe);
242 MsOS_DelayTask(2);//2ms
243
244 //<1>.Waiting for the Device connect
245 #if 1
246 if (wForDevice_B==0) {
247
248 #if 0 //20121109 replace this temp patch with disable BC mode in init code.
249 //20121019, temp patch for Connect Bit missed in USB HW Reset while Run bit = 1
250 if (MDrv_USBGetChipID() == CHIPID_EDEN)
251 {
252 flib_Host20_StopRun_Setting(HOST20_Disable);
253 MsOS_DelayTask(10); //10ms
254 }
255 #endif
256
257 mbHost20_USBCMD_HCReset_Set(); //host controller reset
258 while(mbHost20_USBCMD_HCReset_Rd()>0);
259 }
260 #endif
261
262 if (MDrv_USBGetChipID()==CHIPID_EUCLID) // Euclid
263 {
264 mwHost20Bit_Set(0x81,(BIT6|BIT7));
265 }
266
267 wValue=0;
268 wTimer_ms=0;
269 do {
270 wValue=mwHost20_PORTSC_ConnectStatus_Rd();
271 //printf("wValue:%02bx\n",wValue);
272
273 if (wValue==0) {
274 MsOS_DelayTask(1);//10, wait 1 ms
275 wTimer_ms++;
276 }
277 //if (mwHost20_PORTSC_ConnectStatus_Rd()==0) return 0;
278 if (wTimer_ms>wTimeWaitForConnect_ms) // Case1:Waiting for 10 sec=10000
279 { // Case2:Waiting for 100 ms =100
280 DEBUG_USB(printf("??? Waiting for Peripheral Connecting Fail...\n"));
281 return (0);
282 }
283 }while(wValue==0);
284
285
286
287 //mwHost20_Misc_EOF1Time_Set(Host20_EOF1Time);
288 //printf("34:%02bx\n",XBYTE[0x2434]);
289 //<2>.Init All the Data Structure & write Base Address register
290 flib_Host20_InitStructure();
291
292 //Write Base Address to Register
293 // Host20_STRUCTURE_BASE_ADDRESS
294 mwHost20_CurrentAsynchronousAddr_Set(VirtoPhyAddr((U32)Host20_STRUCTURE_qHD_BASE_ADDRESS));
295
296 //mwHost20_CurrentAsynchronousAddr_Set(VirtoPhyAddr((U16)&Host20_qHD_List_Work));
297 // printf("qhead:%x\n",VirtoPhyAddr(Host20_STRUCTURE_qHD_BASE_ADDRESS));
298
299 //<4>.Enable interrupts
300 mwHost20_USBINTR_Set( HOST20_USBINTR_SystemError |
301
302 HOST20_USBINTR_IntOnAsyncAdvance |
303 HOST20_USBINTR_USBError |
304 HOST20_USBINTR_CompletionOfTransaction);
305
306 #ifdef ATV_SERISE_USE
307 if(jpCVBS5_Get_Device_Define())
308 UHC_XBYTE(0x40)|=0x80;//force enter FSmode
309 #endif
310
311 if (wForDevice_B==0) {
312 if (flib_Host20_PortBusReset()>0)
313 return(2);
314 }
315
316 return (1); //success
317 }
USB_Write_REG32(UINT8 addr,UINT32 val)318 void USB_Write_REG32(UINT8 addr,UINT32 val)
319 {
320 UHC_SETXBYTE(addr, (U8) val & 0xFF);
321 UHC_SETXBYTE(addr+1, (U8) (val>>8) & 0xFF);
322 UHC_SETXBYTE(addr+2, (U8) (val>>16) & 0xFF);
323 UHC_SETXBYTE(addr+3, (U8) (val>>24) & 0xFF);
324
325 // mwHost20Port(addr)=(unsigned char)val & 0xff;
326 // mwHost20Port(addr+1)=(unsigned char)((val >>8));
327 // mwHost20Port(addr+2)=(unsigned char)((val>>16));
328 // mwHost20Port(addr+3)=(unsigned char)((val>>24));
329
330 }
331
332 //====================================================================
333 // * Function Name: flib_Host20_Close
334 // * Description:
335 // <1>.Suspend Host
336 // <2>.Disable the interrupt
337 // <3>.Clear Interrupt Status
338 // <4>.Issue HW Reset
339 // <5>.Free the Allocated Memory
340 // * Input:
341 // * OutPut:
342 //====================================================================
flib_Host20_Close(void)343 UINT8 flib_Host20_Close(void)
344 {
345 #if 0 //We don't need to suspend the bus.
346 UINT32 wTemp;
347
348 if (mwHost20_USBINTR_Rd()>0) {
349 //<1>.Suspend Host
350 flib_Host20_Suspend();
351
352 //<2>.Disable the interrupt
353 mwHost20_USBINTR_Set(0);
354
355 //<3>.Clear Interrupt Status
356 wTemp=mwHost20_USBSTS_Rd();
357 wTemp=wTemp&0x0000003F;
358 mwHost20_USBSTS_Set(wTemp);
359 }
360 #endif
361
362 return (1);
363 }
364
365 //====================================================================
366 // * Function Name: flib_Host20_StopRun_Setting
367 // * Description:
368 // * Input:
369 // * OutPut:
370 //====================================================================
flib_Host20_StopRun_Setting(UINT8 bOption)371 void flib_Host20_StopRun_Setting(UINT8 bOption)
372 {
373 if (bOption==HOST20_Enable) {
374 if (mbHost20_USBCMD_RunStop_Rd()>0)
375 return;
376
377 mbHost20_USBCMD_RunStop_Set();
378
379 while(mbHost20_USBCMD_RunStop_Rd()==0);
380 }
381 else {
382 if (mbHost20_USBCMD_RunStop_Rd()==0)
383 return;
384
385 mbHost20_USBCMD_RunStop_Clr();
386
387 while(mbHost20_USBCMD_RunStop_Rd()>0);
388 }
389
390 }
391 #if 0
392 //====================================================================
393 // * Function Name: flib_Host20_Asynchronous_Setting
394 // * Description:
395 // * Input:
396 // * OutPut:
397 //====================================================================
398 void flib_Host20_Asynchronous_Setting(UINT8 bOption)
399 {
400 if (bOption==HOST20_Enable) {
401 if (mwHost20_USBSTS_AsynchronousStatus_Rd()>0)
402 return;
403
404 mbHost20_USBCMD_AsynchronousEnable_Set();
405
406 while(mwHost20_USBSTS_AsynchronousStatus_Rd()==0);
407 }
408 else{
409 if (mwHost20_USBSTS_AsynchronousStatus_Rd()==0)
410 return;
411
412 mbHost20_USBCMD_AsynchronousEnable_Clr();
413
414 while(mwHost20_USBSTS_AsynchronousStatus_Rd()>0);
415 }
416
417 }
418 #endif
419 //void _noop_()
420 //{
421 // int i=0;
422 //}
423 //====================================================================
424 // * Function Name: flib_Host20_PortBusReset
425 // * Description:
426 // <1>.Waiting for HCHalted=0
427 // <2>.Write PortEnable=0(Reserved for Faraday-IP)
428 // <3>.Write PortReset=0
429 // <4>.Wait time
430 // <5>.Write PortReset=0
431 // <6>.Waiting for IRS->PortChangeDetect
432 // * Input:
433 // * OutPut:
434 //====================================================================
flib_Host20_PortBusReset(void)435 UINT8 flib_Host20_PortBusReset(void)
436 {
437 UINT8 bExitLoop;
438 //UINT8 ttt;
439 UINT32 wTmp;
440 //flib_Host20_ForceSpeed(0);
441
442 //<1>.Disable RunStop
443 // bResetDuringRun=0;
444
445 //printf("disable run\n");
446 //if (mbHost20_USBCMD_RunStop_Rd()>0)
447 flib_Host20_StopRun_Setting(HOST20_Disable);
448 //printf("0x10 ->%02bx \n",XBYTE[0x2410]);
449
450 //<2>.Write PortReset=0
451 if ( (MDrv_USBGetChipID() == CHIPID_NEPTUNE) ||
452 (MDrv_USBGetChipID() == CHIPID_ERIS) ||
453 (MDrv_USBGetChipID() == CHIPID_TITANIA) ||
454 (MDrv_USBGetChipID() == CHIPID_PLUTO) ||
455 (MDrv_USBGetChipID() == CHIPID_TRITON) ||
456 (MDrv_USBGetChipID() == CHIPID_TITANIA2) )
457 {
458 UTMI_ANDXBYTE(0x2c,0x3E);
459 UTMI_ANDXBYTE(0x2d,~0x03);
460 UTMI_ANDXBYTE(0x2f,~0x4A);
461 UTMI_SETXBYTE(0x2a,0xd0);
462 }
463 else //for T3 later
464 {
465 UTMI_SETXBYTE(0x2c,0x10);
466 UTMI_SETXBYTE(0x2d,0);
467 UTMI_SETXBYTE(0x2e,0);
468 UTMI_SETXBYTE(0x2f,0);
469 UTMI_SETXBYTE(0x2a,0x80);//Chirp K detection level: 0x80 => 400mv, 0x20 => 575mv
470 }
471
472 mwHost20_PORTSC_PortReset_Set();
473
474
475 //<3>.Wait time=>55ms
476 //flib_Host20_TimerEnable(55);
477 //MsOS_DelayTask(100);
478 MsOS_DelayTask(80);
479 if ( (MDrv_USBGetChipID() == CHIPID_EINSTEIN) ||
480 (MDrv_USBGetChipID() == CHIPID_NAPOLI) )
481 UTMI_SETXBYTE(0x2a,0x60); // disconnect level 625 mV for 28 nm process
482 else
483 UTMI_SETXBYTE(0x2a,0);
484 MsOS_DelayTask(20);
485 //flib_Debug_LED_Off_All();; //GPIO-High
486
487 mwHost20_PORTSC_PortReset_Clr();
488
489 if ( (MDrv_USBGetChipID() == CHIPID_NEPTUNE) ||
490 (MDrv_USBGetChipID() == CHIPID_ERIS) ||
491 (MDrv_USBGetChipID() == CHIPID_TITANIA) ||
492 (MDrv_USBGetChipID() == CHIPID_PLUTO) ||
493 (MDrv_USBGetChipID() == CHIPID_TRITON) ||
494 (MDrv_USBGetChipID() == CHIPID_TITANIA2) )
495 {
496 UTMI_ORXBYTE(0x2c,0xC1);
497 UTMI_ORXBYTE(0x2d,0x03);
498 UTMI_ORXBYTE(0x2f,0x4a);
499 }
500 else if ((MDrv_USBGetChipID() == CHIPID_TITANIA3) ||
501 (MDrv_USBGetChipID() == CHIPID_TITANIA4) ||
502 (MDrv_USBGetChipID() == CHIPID_TITANIA7) ||
503 (MDrv_USBGetChipID() == CHIPID_TITANIA8) ||
504 (MDrv_USBGetChipID() == CHIPID_TITANIA9) ||
505 (MDrv_USBGetChipID() == CHIPID_TITANIA12) ||
506 (MDrv_USBGetChipID() == CHIPID_TITANIA13) ||
507 (MDrv_USBGetChipID() == CHIPID_JANUS) ||
508 (MDrv_USBGetChipID() == CHIPID_MARIA10) ||
509 (MDrv_USBGetChipID() == CHIPID_JANUS2) ||
510 (MDrv_USBGetChipID() == CHIPID_AMBER6) )
511 {
512 UTMI_SETXBYTE(0x2c,0x10);
513 UTMI_SETXBYTE(0x2d,0x02);
514 UTMI_SETXBYTE(0x2f,0x81);
515 }
516 else if ( (MDrv_USBGetChipID() == CHIPID_MACAW12) )
517 {
518 //PreEmphasis +40%, CM +20%
519 UTMI_SETXBYTE(0x2c,0x90);
520 UTMI_SETXBYTE(0x2d,0x03);
521 UTMI_SETXBYTE(0x2f,0x81);
522 }
523 else if ((MDrv_USBGetChipID()==CHIPID_AMBER1) ||
524 (MDrv_USBGetChipID()==CHIPID_AMBER5) ||
525 (MDrv_USBGetChipID()==CHIPID_AMBER7) ||
526 (MDrv_USBGetChipID()==CHIPID_AMBER3) ||
527 (MDrv_USBGetChipID()==CHIPID_AMETHYST) ||
528 (MDrv_USBGetChipID()==CHIPID_EAGLE))
529 {
530 //for Amber1 later 40nm before Agate
531 UTMI_SETXBYTE(0x2c,0x98);
532 UTMI_SETXBYTE(0x2d,0x02);
533 UTMI_SETXBYTE(0x2e,0x10);
534 UTMI_SETXBYTE(0x2f,0x01);
535 }
536 else if ( (MDrv_USBGetChipID() == CHIPID_EDISON) ||
537 (MDrv_USBGetChipID() == CHIPID_EIFFEL) )
538 {
539 //for Agate later 40nm, same as 55nm setting2
540 UTMI_SETXBYTE(0x2c,0x90);
541 UTMI_SETXBYTE(0x2d,0x02);
542 UTMI_SETXBYTE(0x2f,0x81);
543
544 }
545 else //for Agate later 40nm, same as 55nm setting1
546 {
547 UTMI_SETXBYTE(0x2c,0x10);
548 UTMI_SETXBYTE(0x2d,0x02);
549 UTMI_SETXBYTE(0x2f,0x81);
550 }
551 //printf("Host Speed:%02bx\n",mwOTG20_Control_HOST_SPD_TYP_Rd());
552
553 //<5>.Waiting for IRS->PortChangeDetect
554 //printf("wait reset\n");
555 #if 1
556 bExitLoop=0;
557 wTmp=0;
558
559 do {
560 if (mwHost20_PORTSC_PortReset_Rd()==0)
561 bExitLoop=1;
562 //USBDELAY(100);
563 //if (mwHost20_PORTSC_ConnectStatus_Rd()==0) return 1;
564 wTmp++;
565 if (wTmp>20000) {
566 DEBUG_USB(printf("??? Error waiting for Bus Reset Fail...==> Reset HW Control\n"));
567 mbHost20_USBCMD_HCReset_Set();
568 //while(mbHost20_USBCMD_HCReset_Rd()==1);
569 return (1);
570 }
571 }
572 while(bExitLoop==0);
573 #endif
574 //<6>.Enable RunStop Bit
575 #if 0
576 if (mwHost20_PORTSC_ForceSuspend_Rd())
577 {
578 printf("port suspend\n");
579 mwHost20_PORTSC_ForceResume_Set(); //force resume
580 USBDELAY(14);
581 mwHost20_PORTSC_ForceResume_Clr();
582 }
583 #endif
584 //mwHost20_PORTSC_ConnectChange_Set(); //clear connection change bit
585
586 UTMI_ORXBYTE(0x06,0x03); //reset UTMI
587 UTMI_ANDXBYTE(0x06,0xfc);
588
589 flib_Host20_StopRun_Setting(HOST20_Enable);
590
591 //flib_Debug_LED_On_All(); //GPIO-Low
592 MsOS_DelayTask(50);
593
594 //<7>.Detect Speed
595 gSpeed= mwOTG20_Control_HOST_SPD_TYP_Rd();
596 //ttt=mwOTG20_Control_HOST_SPD_TYP_Rd();
597 HOSTLIB_MSG(printf("\r\n Host type:%x\n", gSpeed));
598 #if 0
599 printf("UTMI_2F: %x, UTMI_2E: %x, UTMI_2D: %x, UTMI_2C: %x\n",
600 UTMI_READXBYTE(0x2F), UTMI_READXBYTE(0x2E),
601 UTMI_READXBYTE(0x2D), UTMI_READXBYTE(0x2C));
602 #endif
603 //<8>.Delay 20 ms
604 //flib_Host20_TimerEnable(5);//After Reset => Host must reserve 20 ms for Device init
605
606 if (gSpeed==2)
607 {
608 UTMI_ORXBYTE(9,0x80); //HS rx robust enable
609 mwHost20_Misc_EOF1Time_Set(3);
610 gUsbDeviceState=USB20_DEVICE;
611 }
612 else
613 {
614 UTMI_ANDXBYTE(9,0x7f);
615 mwHost20_Misc_EOF1Time_Set(2);
616 gUsbDeviceState=USB11_DEVICE;
617 }
618
619 flib_Host20_QHD_Control_Init();
620 //printf("return 1");
621 #if 0 //Try to support HID class
622 if(gSpeed==0x01)
623 {
624 MINI_DEBUG2(printf("low speed device\n"));
625 return (2); //not support Low Speed device
626 }
627 #endif
628
629 return (0);
630 }
631
632 //====================================================================
633 // * Function Name: flib_Host20_Suspend
634 // * Description:
635 // <1>.Make sure PortEnable=1
636 // <2>.Write PORTSC->Suspend=1
637 // <3>.Waiting for the ISR->PORTSC->Suspend=1
638 // * Input:
639 // * OutPut: 0:OK
640 // 1:Fail
641 //====================================================================
flib_Host20_Suspend(void)642 void flib_Host20_Suspend(void)
643 {
644 #if 0
645 if (mbHost20_USBCMD_RunStop_Rd()==0)
646 return(1);
647
648 //<1>.Make sure PortEnable=1
649 if (mwHost20_PORTSC_EnableDisable_Rd()==0)
650 return(1);
651 #endif
652 //<2>.Write PORTSC->Suspend=1
653 flib_Host20_StopRun_Setting(HOST20_Disable);//For Faraday HW request
654
655 //<3>.Write PORTSC->Suspend=1
656 mwHost20_PORTSC_ForceSuspend_Set();
657
658 //<4>.Waiting for the ISR->PORTSC->Suspend=1
659 #if 0
660 flib_Host20_TimerEnable_UnLock(1);//1sec
661 bExitFlag=0;
662
663 do {
664 if (mwHost20_PORTSC_ForceSuspend_Rd()>0)
665 bExitFlag=1;
666
667 if (gwOTG_Timer_Counter>5) {
668 bExitFlag=1;
669 printf(">>> Fail => Time Out for Waiting ForceSuspend...\n");
670 }
671 }
672 while(bExitFlag==0);
673 #else
674 while(mwHost20_PORTSC_ForceSuspend_Rd()==0);
675 #endif
676
677 }
678
679
680
681
682 //====================================================================
683 // * Function Name: flib_Host20_Control_Command_Request
684 // * Description:
685 // * Input: none
686 // * OutPut: none
687 //====================================================================
flib_Host20_Control_Command_Request(Host20_Control_Command_Structure * pbCMD,UINT8 bmRequestType_Temp,UINT8 bRequest_Temp,UINT16 wValue_Temp,UINT16 wIndex_Temp,UINT16 wLength_Temp)688 void flib_Host20_Control_Command_Request(Host20_Control_Command_Structure *pbCMD,UINT8 bmRequestType_Temp,UINT8 bRequest_Temp,UINT16 wValue_Temp,UINT16 wIndex_Temp,UINT16 wLength_Temp)
689 {
690 UINT8 i;
691 UINT8 *pbTemp;
692
693 pbTemp=(UINT8*)pbCMD;
694 for (i=0;i<8;i++)
695 *pbTemp++=0x00;
696 pbCMD->bmRequestType=bmRequestType_Temp; //Byte-0:In/Standard/Device
697 pbCMD->bRequest=bRequest_Temp;
698
699 pbCMD->wValueLow=(UINT8)wValue_Temp;
700 pbCMD->wValueHigh=(UINT8)(wValue_Temp>>8);
701
702 pbCMD->wIndexLow=(UINT8)wIndex_Temp;
703 pbCMD->wIndexHigh=(UINT8)(wIndex_Temp>>8);
704
705 pbCMD->wLengthLow=(UINT8)wLength_Temp;
706 pbCMD->wLengthHigh=(UINT8)(wLength_Temp>>8);
707 }
708 #if 0
709 void FillBufferArray(qTD_Structure xdata *spTempqTD, UINT16 bpDataPage)
710 {
711
712 U32 temp;
713 temp=VirtoPhyAddr(bpDataPage);
714 spTempqTD->ArrayBufferPointer[0].Byte1=(UINT8)(temp>>8)&0xf0;
715 spTempqTD->ArrayBufferPointer[0].Byte2=(UINT8)(temp>>16)&0xff;
716 spTempqTD->ArrayBufferPointer[0].Byte3=(UINT8)(temp>>24)&0xff;
717
718 }
719 #endif
720
721 //void Dump_QTD(UINT16 addr);
722 //void Dump_Data(UINT16 addr,UINT16 sz);
723 #if 0
724 void Dump_Data(UINT16 addr,UINT16 sz)
725 {
726 UINT16 i;
727 printf("addr:%x -> \r\n",addr);
728 for (i=0; i < sz ; i++)
729 printf("%02bx ",*(unsigned char volatile xdata *)(addr+i));
730 }
731 #endif
732
733 #ifdef ENABLE_CBI_HOST
flib_Host20_Issue_Control_CBI(UINT8 * pbCmd,UINT16 hwDataSize,UINT8 * pbData)734 UINT8 flib_Host20_Issue_Control_CBI(UINT8* pbCmd,UINT16 hwDataSize,UINT8* pbData)
735 {
736 qTD_Structure *spTempqTD;
737 // U32 bpDataPage;
738 U8 bReturnValue;
739 // U8 i;
740 qHD_Structure *qh_ptr;
741
742 qh_ptr=pHost20_qHD_List_Control1;
743
744 //<0>.Allocate qTD & Data Buffer
745 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
746 // bpDataPage=sAttachDevice.bDataBuffer; //2//2k buffer
747
748 //<2.1>.Setup packet
749 //<A>.Fill qTD
750 spTempqTD->bPID=HOST20_qTD_PID_SETUP; //Bit8~9
751 spTempqTD->bTotalBytes=8; //Bit16~30
752
753 spTempqTD->bDataToggle=0; //Bit31
754 // FillBufferArray(spTempqTD, bpDataPage);
755 memcpy(pUsbCtrlBuf,pbCmd,8);
756
757 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
758
759 // printf("bpDataPage:%x\n",bpDataPage);
760 // memcpy((U8 *)bpDataPage,pbCmd,8);
761
762 //<B>.Send qTD
763 // Dump_QTD(spTempqTD);
764
765 // Dump_Data(0x2400,0x50);
766 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
767 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
768
769 if (bReturnValue>0)
770 goto exit_issue_control;
771
772 //<4.2>.Out packet
773 //<A>.Fill qTD
774 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
775 spTempqTD->bPID=HOST20_qTD_PID_OUT; //Bit8~9
776 spTempqTD->bTotalBytes=hwDataSize; //Bit16~30
777 spTempqTD->bDataToggle=1; //Bit31
778 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
779 // FillBufferArray(spTempqTD, bpDataPage);
780
781 memcpy(pUsbCtrlBuf,pbData,hwDataSize);
782
783 //<B>.Send qTD
784 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
785 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
786
787 if (bReturnValue>0)
788 goto exit_issue_control;
789
790 //<4.3>.In packet
791 //<A>.Fill qTD
792 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
793 spTempqTD->bPID=HOST20_qTD_PID_IN; //Bit8~9
794 spTempqTD->bTotalBytes=0; //Bit16~30
795 spTempqTD->bDataToggle=1; //Bit31
796
797 //<B>.Send qTD
798 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
799 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
800
801 if (bReturnValue>0)
802 goto exit_issue_control;
803
804 return (0);
805 exit_issue_control:
806
807 return (bReturnValue);
808 }
809 #endif
810
811 //====================================================================
812 //For Control-Single qTD// * Function Name: flib_Host20_Issue_Control
813 // * Description:
814 // <1>.Analysis the Controller Command => 3 type
815 // <2>.Case-1:"Setup/In/Out' Format..."
816 // (get status/get descriptor/get configuration/get interface)
817 // <3>.Case-2:'Setup/In' Format... => Faraday Driver will not need
818 // (clear feature/set feature/set address/set Configuration/set interface )
819 // <4>.Case-3:'Setup/Out/In'
820 // (set descriptor)
821 // * Input:
822 // * OutPut: 0: OK
823 // X:>0 => Fail
824 //====================================================================
flib_Host20_Issue_Control(U8 bEdNum,U8 * pbCmd,U16 hwDataSize,U8 * pbData)825 U8 flib_Host20_Issue_Control ( U8 bEdNum, U8* pbCmd, U16 hwDataSize, U8* pbData)
826 {
827 qTD_Structure *spTempqTD;
828 // U32 bpDataPage;
829 U8 bReturnValue;
830 // U8 i;
831 qHD_Structure *qh_ptr;
832 U32 *dnaptr;
833 U32 dwTimes;
834
835 if (bEdNum==0)
836 qh_ptr=pHost20_qHD_List_Control0;
837 else
838 qh_ptr=pHost20_qHD_List_Control1;
839
840
841 //<0>.Allocate qTD & Data Buffer
842 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
843 // bpDataPage=sAttachDevice.bDataBuffer; //2//2k buffer
844
845 //<2.1>.Setup packet
846 //<A>.Fill qTD
847 spTempqTD->bPID=HOST20_qTD_PID_SETUP; //Bit8~9
848 spTempqTD->bTotalBytes=8; //Bit16~30
849
850 spTempqTD->bDataToggle=0; //Bit31
851 // FillBufferArray(spTempqTD, bpDataPage);
852 memcpy(pUsbCtrlBuf,pbCmd,8);
853
854 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
855
856 // printf("bpDataPage:%x\n",bpDataPage);
857 // memcpy((U8 *)bpDataPage,pbCmd,8);
858
859 #if 1
860 //<B>.Send qTD
861 // Dump_QTD(spTempqTD);
862
863 // Dump_Data(0x2400,0x50);
864 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
865 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
866
867 if (bReturnValue>0)
868 goto exit_issue_control;
869
870 // printf("c3");
871
872 //<1>.Analysis the Controller Command
873 switch (*(pbCmd+1)) { // by Standard Request codes
874 // <2>.Case-1:"Setup/In/Out' Format..."
875 case 0: // get status
876 case 6: // get descriptor
877 case 8: // get configuration
878 case 10: // get interface
879 case 0xfe: //get Max Lun
880 //<2.2>.In packet
881
882 //<A>.Fill qTD
883 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
884 spTempqTD->bPID=HOST20_qTD_PID_IN; //Bit8~9
885 spTempqTD->bTotalBytes=hwDataSize; //Bit16~30
886
887 spTempqTD->bDataToggle=1; //Bit31
888
889 // spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr(bpDataPage);
890 //FillBufferArray(spTempqTD, bpDataPage);
891 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
892
893 //MAKE THE SIGNATURE
894 dnaptr=(U32 *)(U32)pUsbCtrlBuf;
895 if (hwDataSize>3)
896 {
897 dnaptr+=(hwDataSize/4) - 1;
898 *dnaptr=USB_DMA_SIGNATURE1;
899 }
900
901 //<B>.Send qTD
902 //bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
903 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
904 if (bReturnValue>0)
905 goto exit_issue_control;
906 // printf("c4");
907 if (((hwDataSize-TotalBytes)==hwDataSize) && (hwDataSize>3))
908 {
909 dwTimes=MsOS_GetSystemTime();
910 while (*dnaptr==USB_DMA_SIGNATURE1)
911 {
912 if ((MsOS_GetSystemTime()-dwTimes) >= 10)
913 {
914 HOSTLIB_MSG(printf("MIU DMA not finished yet\n"));
915 break; //timeout
916 }
917 }
918 }
919
920 //<C>.Waiting for result
921 memcpy(( U8 *)pbData,pUsbCtrlBuf,hwDataSize);
922
923 // Dump_Data((U16)pbData,hwDataSize);
924
925 //<2.3>.Out packet
926
927 //<A>.Fill qTD
928 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
929 spTempqTD->bPID=HOST20_qTD_PID_OUT; //Bit8~9
930 spTempqTD->bTotalBytes=0; //Bit16~30
931
932 spTempqTD->bDataToggle=1; //Bit31
933
934 //<B>.Send qTD
935 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
936 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
937
938 if (bReturnValue>0)
939 goto exit_issue_control;
940
941
942 break;
943
944 //<3>.Case-2:'Setup/In' Format... => Faraday Driver will not need
945 case 1: // clear feature
946 case 3: // set feature
947 case 5: // set address
948 case 9: // set Configuration
949 case 11: // set interface
950
951 //<3.2>.In packet
952
953 //<A>.Fill qTD
954 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
955 spTempqTD->bPID=HOST20_qTD_PID_IN; //Bit8~9
956 spTempqTD->bTotalBytes=hwDataSize; //Bit16~30
957 spTempqTD->bDataToggle=1; //Bit31
958 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
959 //FillBufferArray(spTempqTD, bpDataPage);
960
961 //<B>.Send qTD
962 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
963 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
964
965 if (bReturnValue>0)
966 goto exit_issue_control;
967 //<C>.Copy Result
968 // memcpy(pbData,pUsbCtrlBuf,hwDataSize);
969 break;
970
971 //<4>.Case-3:'Setup/Out/In'
972 case 7: // set descriptor
973 //<4.2>.Out packet
974 //<A>.Fill qTD
975 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
976 spTempqTD->bPID=HOST20_qTD_PID_OUT; //Bit8~9
977 spTempqTD->bTotalBytes=hwDataSize; //Bit16~30
978 spTempqTD->bDataToggle=1; //Bit31
979 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
980 // FillBufferArray(spTempqTD, bpDataPage);
981
982 memcpy(pUsbCtrlBuf,pbData,hwDataSize);
983
984 //<B>.Send qTD
985 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
986 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
987
988 if (bReturnValue>0)
989 goto exit_issue_control;
990
991
992 //<4.3>.In packet
993 //<A>.Fill qTD
994 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
995 spTempqTD->bPID=HOST20_qTD_PID_IN; //Bit8~9
996 spTempqTD->bTotalBytes=0; //Bit16~30
997 spTempqTD->bDataToggle=1; //Bit31
998
999 //<B>.Send qTD
1000 // bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
1001 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
1002
1003 if (bReturnValue>0)
1004 goto exit_issue_control;
1005
1006 break;
1007
1008 default:
1009 break;
1010 }
1011 #endif
1012 return (0);
1013 exit_issue_control:
1014
1015 return (bReturnValue);
1016
1017 }
1018
1019
1020 #if 0
1021 void Dump_QTD(UINT16 addr)
1022 {
1023 UINT8 i;
1024 // kcj 2007-06-07 printf("QTD:%x -> \n",addr);
1025 // kcj 2007-06-07 for (i=0; i < 0x20 ; i++)
1026 // kcj 2007-06-07 printf("%02bx ",*(unsigned char volatile xdata *)(addr+i));
1027
1028
1029 }
1030 void Dump_Data(UINT16 addr,UINT16 sz)
1031 {
1032 UINT16 i;
1033 // kcj 2007-06-07 printf("addr:%x -> \n",addr);
1034 // kcj 2007-06-07 for (i=0; i < sz ; i++)
1035 // kcj 2007-06-07 printf("%02bx ",*(unsigned char volatile xdata *)(addr+i));
1036
1037
1038 }
1039 #endif
1040 //====================================================================
1041 // * Function Name: flib_Host20_Issue_Bulk
1042 // * Description: Input data must be 4K-Alignment
1043 // <1>.MaxSize=20 K
1044 // <2>.Support Only 1-TD
1045 // * Input:
1046 // * OutPut:
1047 //====================================================================
flib_Host20_Issue_Bulk(U8 bArrayListNum,U16 hwSize,U32 pwBuffer,U8 bDirection)1048 U8 flib_Host20_Issue_Bulk (U8 bArrayListNum,U16 hwSize,U32 pwBuffer,U8 bDirection)
1049 {
1050 qTD_Structure *spTempqTD;
1051 U8 bTemp,i;
1052 U16 count;
1053 U32 addr;
1054 U32 mybuf,workbuf;
1055 qHD_Structure *spTempqH;
1056 U32 *dnaptr=NULL;
1057 U32 dwTimes;
1058
1059
1060
1061 mybuf=0;
1062
1063 if ( pwBuffer !=(U32) KSEG02KSEG1(pwBuffer) )
1064 {
1065 if (pwBuffer & 0x7) //flush should be 8 bytes aligned
1066 {
1067 // printf(" buf ");
1068 #if 0
1069 mybuf=(U32)msAPI_Memory_Allocate(hwSize, BUF_ID_USB_HOST ); //temporarily buffer for USB control
1070 if ( mybuf == 0 )
1071 {
1072 printf("Memory allocate failed\n");
1073 return HOST20_FATAL;
1074 }
1075 mybuf = (U32)KSEG02KSEG1(mybuf);
1076 #else
1077 mybuf = (U32)KSEG02KSEG1(usb_temp_buf);
1078 pwBuffer=(U32)KSEG02KSEG1(pwBuffer);
1079 #endif
1080
1081 }
1082 else
1083 {
1084 // printf(" fuh ");
1085 MY_HAL_DCACHE_FLUSH((U32)pwBuffer,(U32)hwSize); //flush buffer to uncached buffer
1086 pwBuffer=(U32)KSEG02KSEG1(pwBuffer);
1087 }
1088 }
1089 else
1090 {
1091 if (pwBuffer & 0x7)
1092 {
1093 // printf(" buf ");
1094 #if 0
1095 mybuf=(U32)msAPI_Memory_Allocate(hwSize, BUF_ID_USB_HOST ); //temporarily buffer for USB control
1096 if ( mybuf == 0 )
1097 {
1098 printf("Memory allocate failed\n");
1099 return HOST20_FATAL;
1100 }
1101 mybuf = (U32)KSEG02KSEG1(mybuf);
1102 #else
1103 mybuf = (U32)KSEG02KSEG1(usb_temp_buf);
1104 pwBuffer=(U32)KSEG02KSEG1(pwBuffer);
1105 #endif
1106
1107 }
1108 }
1109
1110 spTempqTD =flib_Host20_GetStructure(Host20_MEM_TYPE_qTD); //The qTD will be release in the function "Send"
1111 spTempqTD->bTotalBytes=hwSize ;
1112
1113 // FillBufferArray2(spTempqTD,pwBufferArray,hwSize); //use global buffer , because it is 4k alignment
1114 #if 0
1115 spTempqTD->ArrayBufferPointer_Word[0]=pwBufferArray;
1116 if (((pwBufferArray&0xfff)+hwSize) > 0xfff) //goto page 2
1117 spTempqTD->ArrayBufferPointer_Word[1]=pwBufferArray+0x1000; //+4K
1118 if (hwSize > (0x1000+0x1000-(pwBufferArray&0xfff))
1119 #endif
1120 //not allow buffer over 16K for my usage
1121 if (mybuf==0) //use original buf
1122 workbuf=pwBuffer;
1123 else
1124 workbuf=mybuf;
1125
1126 dnaptr=(U32 *)(U32)workbuf;
1127 spTempqTD->ArrayBufferPointer_Word[0] = VirtoPhyAddr(workbuf);
1128
1129 count = 0x1000 - (workbuf & 0x0fff); /* rest of that page */
1130 if ( hwSize < count) /* ... iff needed */
1131 count = hwSize;
1132 else
1133 {
1134 workbuf += 0x1000;
1135 workbuf &= ~0x0fff;
1136
1137 /* per-qtd limit: from 16K to 20K (best alignment) */
1138 for (i = 1; count < hwSize && i < 5; i++)
1139 {
1140 addr = workbuf;
1141 spTempqTD->ArrayBufferPointer_Word[i] = VirtoPhyAddr(addr);
1142 // lastnums++;
1143 workbuf += 0x1000;
1144 if ((count + 0x1000) < hwSize)
1145 count += 0x1000;
1146 else
1147 count = hwSize;
1148 }
1149
1150 }
1151 if (bArrayListNum==0)
1152 spTempqH=pHost20_qHD_List_Bulk0;
1153 else // if (bArrayListNum==1)
1154 spTempqH=pHost20_qHD_List_Bulk1;
1155
1156
1157 //<2>.Analysis the Direction
1158 if (bDirection==OTGH_Dir_IN)
1159 {
1160 spTempqTD->bPID=HOST20_qTD_PID_IN;
1161 if (FirstBulkIn)
1162 {
1163 spTempqTD->bDataToggle=0;
1164 spTempqH->bDataToggleControl=1;
1165 }
1166
1167 //MAKE THE SIGNATURE
1168 if (hwSize>3)
1169 {
1170 dnaptr+=(hwSize/4) - 1;
1171 *dnaptr=USB_DMA_SIGNATURE1;
1172 }
1173
1174 }
1175 else
1176 {
1177 spTempqTD->bPID=HOST20_qTD_PID_OUT;
1178 if (FirstBulkOut)
1179 {
1180 spTempqTD->bDataToggle=0;
1181 spTempqH->bDataToggleControl=1;
1182 }
1183 if (mybuf) memcpy((void*)mybuf,(void*)pwBuffer,hwSize); //copy buffer
1184 }
1185 //<3>.Send TD
1186 // if (bArrayListNum==0)
1187 // bTemp=flib_Host20_Send_qTD(spTempqTD ,pHost20_qHD_List_Bulk0,Host20_Timeout);
1188 // else if (bArrayListNum==1)
1189 bTemp=flib_Host20_Send_qTD(spTempqTD ,spTempqH,gUsbTimeout);
1190
1191 if ((FirstBulkIn)&&(bDirection==OTGH_Dir_IN))
1192 {
1193 spTempqH->bDataToggleControl=0;
1194 FirstBulkIn=FALSE;
1195 }
1196 if ((FirstBulkOut)&&(bDirection==OTGH_Dir_Out))
1197 {
1198 spTempqH->bDataToggleControl=0;
1199 FirstBulkOut=FALSE;
1200 }
1201
1202 if (bDirection==OTGH_Dir_IN)
1203 {
1204
1205 if(((hwSize-TotalBytes)==hwSize) && (hwSize>3))
1206 {
1207 dwTimes=MsOS_GetSystemTime();
1208 while (*dnaptr==USB_DMA_SIGNATURE1)
1209 {
1210 if ((MsOS_GetSystemTime()-dwTimes) >= 10)
1211 {
1212 //printf("MIU DMA not finished yet\n");
1213 break; //timeout
1214 }
1215 }
1216 }
1217
1218 #if defined(CPU_TYPE_MIPS) || defined(CPU_TYPE_ARM)
1219 MAsm_CPU_Sync();
1220 #endif
1221 if (mybuf)
1222 memcpy((void*)pwBuffer,(void*)mybuf,hwSize); //copy buffer
1223 }
1224
1225 #if 0
1226 if (mybuf)
1227 {
1228 mybuf = (U32)KSEG12KSEG0(mybuf);
1229 msAPI_Memory_Free((void*)mybuf,BUF_ID_USB_HOST);
1230 }
1231 #endif
1232 // printf("Z");
1233 return (bTemp);
1234 }
1235
1236 //====================================================================
1237 // * Function Name: flib_Host20_AnalysisConfigyration
1238 // * Description:
1239 // * Input:
1240 //
1241 // * OutPut:
1242 //====================================================================
flib_Host20_AnalysisConfigyration(UINT8 * pbData)1243 UINT8 flib_Host20_AnalysisConfigyration (UINT8 *pbData)
1244 {
1245 #if 1
1246 UINT8 i = 0;
1247 // UINT8 *pBuf, j;
1248 UINT8 ept_idx = 0;
1249 int RemainLen;
1250
1251 //<1>.Copy Configuration 1~2
1252 if (sAttachDevice.sDD.bCONFIGURATION_NUMBER>HOST20_CONFIGURATION_NUM_MAX)
1253 {
1254 //while(1);
1255 }
1256
1257 memcpy((U8 *)&(sAttachDevice.saCD[i]),pbData,HOST20_CONFIGURATION_LENGTH);
1258 pbData=pbData+HOST20_CONFIGURATION_LENGTH;
1259
1260 #if 0
1261 printf("Got a Config Descriptor: \n");
1262 pBuf = (U8 *)&(sAttachDevice.saCD[i]);
1263 for (j=0; j<HOST20_CONFIGURATION_LENGTH; j++)
1264 printf(" %02X", pBuf[j]);
1265 printf("\n\n");
1266 #endif
1267
1268 RemainLen = (int)sAttachDevice.saCD[i].bTotalLengthLowByte +
1269 ((int)sAttachDevice.saCD[i].bTotalLengthHighByte << 8) - HOST20_CONFIGURATION_LENGTH;
1270
1271 //intf_idx = 0;
1272 while (RemainLen > 0)
1273 {
1274 #if 0
1275 printf("RemainLen: 0x%x\n", RemainLen);
1276
1277 printf("Find a descriptor:\n");
1278 pBuf = pbData;
1279 for (j=0; j<pbData[0]; j++)
1280 printf(" %02X", pBuf[j]);
1281 printf("\n\n");
1282 #endif
1283
1284 if (pbData[1] == 4) /*INTERFACE*/
1285 {
1286 // printf("Get a interface descriptor\n");
1287 memcpy((U8 *)&(sAttachDevice.saCD[i].sInterface[0]),pbData,HOST20_INTERFACE_LENGTH);
1288 ept_idx = 0;
1289 if (sAttachDevice.saCD[i].sInterface[0].bEP_NUMBER>HOST20_ENDPOINT_NUM_MAX)
1290 {
1291 // printf("endpoint number is over HOST20_ENDPOINT_NUM_MAX\n");
1292 }
1293 }
1294 else if (pbData[1] == 5) /*ENDPOINT*/
1295 {
1296 // printf("Get a endpoint descriptor\n");
1297 memcpy((U8 *)&(sAttachDevice.saCD[i].sInterface[0].sED[ept_idx]),pbData,HOST20_ENDPOINT_LENGTHX);
1298 ept_idx++;
1299 }
1300
1301 RemainLen -= pbData[0];
1302 pbData = pbData + pbData[0];
1303
1304 if (ept_idx == sAttachDevice.saCD[i].sInterface[0].bEP_NUMBER) //All endpoints are collected
1305 {
1306 //intf_idx++;
1307 if ( (sAttachDevice.saCD[i].sInterface[0].bInterfaceClass == USB_INTERFACE_CLASS_MSD) ||
1308 (sAttachDevice.saCD[i].sInterface[0].bInterfaceClass == USB_INTERFACE_CLASS_HUB) ||
1309 (sAttachDevice.saCD[i].sInterface[0].bInterfaceClass == USB_INTERFACE_CLASS_IMAGE) ||
1310 (sAttachDevice.saCD[i].sInterface[0].bInterfaceClass == USB_INTERFACE_CLASS_HID) )
1311 break;
1312 }
1313 }
1314
1315 // printf("End the config analyzing\n");
1316
1317 return (1);
1318 #else
1319
1320 UINT8 i,j,k;
1321
1322
1323 //<1>.Copy Configuration 1~2
1324 if (sAttachDevice.sDD.bCONFIGURATION_NUMBER>HOST20_CONFIGURATION_NUM_MAX)
1325 {
1326 // kcj 2007-06-07 printf("??? Analysis Configuration Fail(bCONFIGURATION_NUMBER>Max)...");
1327 //while(1);
1328 }
1329 //for (i=0;i<sAttachDevice.sDD.bCONFIGURATION_NUMBER;i++)
1330 i=0;
1331 {
1332
1333 memcpy((U8 *)&(sAttachDevice.saCD[i]),pbData,HOST20_CONFIGURATION_LENGTH);
1334 pbData=pbData+HOST20_CONFIGURATION_LENGTH;
1335
1336 //<2>.Copy Interface 1~5
1337 if (sAttachDevice.saCD[i].bINTERFACE_NUMBER>HOST20_INTERFACE_NUM_MAX)
1338 {
1339 // kcj 2007-06-07 printf("??? Analysis Interface Fail(bINTERFACE_NUMBER>Max)...");
1340 //while(1);
1341 }
1342 for (j=0;j<sAttachDevice.saCD[i].bINTERFACE_NUMBER;j++)
1343 {
1344 memcpy((U8 *)&(sAttachDevice.saCD[i].sInterface[j]),pbData,HOST20_INTERFACE_LENGTH);
1345 pbData=pbData+HOST20_INTERFACE_LENGTH;
1346
1347 //<3>.Copy Class HID
1348 if (sAttachDevice.saCD[i].sInterface[j].bInterfaceClass ==3)
1349 {//Only support 1 class
1350 memcpy((U8 *)&(sAttachDevice.saCD[i].sInterface[j].sClass[0]),pbData,*pbData);
1351 pbData=pbData+(*pbData);
1352 }
1353
1354 //<4>.Copy Endpoint 1~5
1355 if (sAttachDevice.saCD[i].sInterface[j].bEP_NUMBER>HOST20_ENDPOINT_NUM_MAX)
1356 {
1357 // kcj 2007-06-07 printf("??? Analysis Endpoint Fail(bEP_NUMBER>Max)...");
1358 //while(1);
1359 }
1360 for (k=0;k<sAttachDevice.saCD[i].sInterface[j].bEP_NUMBER;k++)
1361 {
1362 memcpy((U8 *)&(sAttachDevice.saCD[i].sInterface[j].sED[k]),pbData,HOST20_ENDPOINT_LENGTHX);
1363 pbData=pbData+HOST20_ENDPOINT_LENGTHX;
1364 }//Endpoint
1365
1366 }//Interface
1367
1368 }//Configuration
1369
1370
1371 //<4>.Checkong the OTG Descriptor
1372 //if (*pbData==3)
1373 //{ if (*(pbData+1)==9)
1374 // {
1375 // sAttachDevice.sOTG.bED_OTG_Length=3;
1376 // sAttachDevice.sOTG.bED_OTG_bDescriptorType=9;
1377 // sAttachDevice.sOTG.bED_OTG_bAttributes=*(pbData+2);
1378 // }
1379 //}
1380
1381 return (1);
1382 #endif
1383 }
1384
1385 //====================================================================
1386 // * Function Name: flib_Host20_FirstTransfer ==>51ok
1387 // * Description:
1388 // * Input:
1389 //
1390 // * OutPut:
1391 //====================================================================
flib_Host20_FirstTransfer(void)1392 UINT8 flib_Host20_FirstTransfer (void)
1393 {
1394
1395 UINT8 bLoop;
1396 UINT8 bCMD[8];
1397 bLoop=0;
1398 do{
1399 bLoop++;
1400 //<1>.GetDescriptor - Device (8 Bytes)
1401 //while (1)
1402 //{
1403
1404 memcpy(bCMD,OTGH_GETDESCRIPTOR_DEVICE_8,8);
1405
1406 if (flib_Host20_Issue_Control (0,bCMD,8,(UINT8*)&(sAttachDevice.sDD))==0)
1407 {
1408
1409 return (0);//ok
1410 }
1411 //}
1412 //<2>.Suspend the Bus
1413 //printf("first\n");
1414 if (gUsbStatus==USB_TIMEOUT) return 1;
1415 #if 1
1416 flib_Host20_Close();
1417 //
1418 UTMI_ORXBYTE(0x06,0x03); //reset UTMI
1419 UTMI_ANDXBYTE(0x06,0xfc);
1420 mbHost20_USBCMD_HCReset_Set();
1421 MsOS_DelayTask(100);
1422
1423 //<3>.Reset Port
1424 flib_Host20_PortBusReset();
1425 #endif
1426 }while(bLoop<6);
1427 gUsbStatus=USB_INIT_FAIL;
1428
1429 return(1);
1430
1431 }
1432 #if 1
1433 code UINT8 SET_FEATURE_PORT_POWER[] = {0x23,0x03,0x08,0x00,0x01,0x00,0x00,0x00};
1434 code UINT8 CLEAR_FEATURE_C_PORT_CONNECTION[]={0x23,0x01,0x10,0,0x01,0,0,0};
1435 code UINT8 SET_FEATURE_PORT_RESET[] = {0x23,0x03,0x04,0x00,0x01,0x00,0x00,0x00};
1436 code UINT8 CLEAR_FEATURE_C_PORT_RESET[]={0x23,0x01,0x14,0,0x01,0,0,0};
1437 code UINT8 OTGH_GETDESCRIPTOR_HUBCLASS[]={0xA0,0x06,0x00,0x00,0x00,0x00,0x09,0x00};
1438 code UINT8 GET_PORT_STATUS[]={0xA3,0x00,0x00,0x00,0x01,0x00,0x04,0x00};
Usb_Hub_Port_Num()1439 U8 Usb_Hub_Port_Num()
1440 {
1441 UINT8 bCMD[8];
1442 U8 PortNum,i;
1443
1444 memcpy(bCMD,OTGH_GETDESCRIPTOR_HUBCLASS,8);
1445
1446 if (flib_Host20_Issue_Control (1,bCMD,0x09,pUsbData)>0)
1447 {
1448 return FALSE;
1449 }
1450 PortNum=*(pUsbData+2);
1451 #if 1
1452 for(i=1;i<=PortNum;i++)
1453 {
1454 //set feature port power
1455 memcpy(bCMD,SET_FEATURE_PORT_POWER,8);
1456 bCMD[4]=i;
1457 if ( flib_Host20_Issue_Control (1,bCMD,0,NULL)>0)
1458 {
1459 DEBUG_USB(printf("set port power failed\n"));
1460 return PortNum;
1461 }
1462 }
1463 MsOS_DelayTask(125);
1464
1465 for(i=1;i<=PortNum;i++)
1466 {
1467 //clear feature c_port_connection
1468 memcpy(bCMD,CLEAR_FEATURE_C_PORT_CONNECTION,8);
1469 bCMD[4]=i;
1470 if (flib_Host20_Issue_Control (1,bCMD,0,NULL)>0)
1471 {
1472 DEBUG_USB(printf("clear feature failed\n"));
1473 return PortNum;
1474 }
1475 }
1476 #endif
1477 return PortNum;
1478 }
USB_Hub_Handle(U8 port)1479 BOOLEAN USB_Hub_Handle(U8 port)
1480 {
1481 UINT8 bCMD[8];
1482
1483 MsOS_DelayTask(10);//10, wait 1 ms
1484
1485 //get all prot status
1486
1487
1488 //get prot status
1489 memcpy(bCMD,GET_PORT_STATUS,8);
1490 bCMD[4]=port;
1491 if (flib_Host20_Issue_Control (1,bCMD,0x04,pUsbData)>0)
1492 {
1493 DEBUG_USB(printf("Get port status failed\n"));
1494
1495 return FALSE;
1496 }
1497
1498 DEBUG_USB(printf(" Port:%02bx Status=%02bx",port,(*pUsbData)));
1499 if(!((*pUsbData)&0x01))
1500 return FALSE;
1501 //for (i=0 ; i < 3 ; i++)
1502 {
1503 DEBUG_USB(printf("Port %02bx have device.\n",port));
1504 //set feature port_reset
1505 memcpy(bCMD,SET_FEATURE_PORT_RESET,8);
1506 bCMD[4]=port;
1507 if (flib_Host20_Issue_Control (1,bCMD,0,NULL)>0)
1508 return FALSE;
1509 MsOS_DelayTask(10);//10, wait 1 ms
1510
1511 memcpy(bCMD,GET_PORT_STATUS,8);
1512 bCMD[4]=port;
1513 if (flib_Host20_Issue_Control (1,bCMD,0x04,pUsbData)>0)
1514 {
1515 DEBUG_USB(printf("Get port status failed\n"));
1516
1517 return FALSE;
1518 }
1519
1520 #if 1
1521 DEBUG_USB(printf(" Port:%02bx Status=%02bx",port,(*pUsbData)));
1522 //clear feature c_port_reset
1523 memcpy(bCMD,CLEAR_FEATURE_C_PORT_RESET,8);
1524 bCMD[4]=port;
1525 if (flib_Host20_Issue_Control (1,bCMD,0,NULL)>0)
1526 return FALSE;
1527
1528 memcpy(bCMD,GET_PORT_STATUS,8);
1529 bCMD[4]=port;
1530 if (flib_Host20_Issue_Control (1,bCMD,0x04,pUsbData)>0)
1531 {
1532 DEBUG_USB(printf("Get port status failed\n"));
1533
1534 return FALSE;
1535 }
1536 #endif
1537 // flib_Host20_Interrupt_Init(1,1,1);
1538 //if (pUsbData[2]!=0)
1539 //{
1540 // OTGH_PT_Bulk_Init_for_Int();
1541 // result=flib_Host20_Issue_Bulk (0,1,pUsbData,OTGH_Dir_IN);
1542 // // flib_Host20_Issue_Interrupt(1,HOST20_qTD_PID_IN);
1543
1544 MsOS_DelayTask(30);//10, wait 1 ms
1545 }
1546 return TRUE;
1547 }
1548 #endif
1549
1550 #ifdef Enable_Issue_TestPacket
1551 extern void IssueTestPacket(U8 xdata *TestData);
USB_DACHE_FLUSH(U32 addr,U32 length)1552 void USB_DACHE_FLUSH(U32 addr, U32 length)
1553 {
1554 MY_HAL_DCACHE_FLUSH(addr, length);
1555 }
1556
1557 #endif
1558
1559 //====================================================================
1560 // * Function Name: flib_Host20_Enumerate
1561 // * Description:
1562 // * Input:
1563 //
1564 // * OutPut:
1565 //====================================================================
1566 #ifndef DEVICE_ENUM_SEGMENT
flib_Host20_Enumerate(UINT8 bNormalEnumerate,UINT8 bAddress)1567 UINT8 flib_Host20_Enumerate (UINT8 bNormalEnumerate,UINT8 bAddress)
1568 {
1569 UINT8 bCMD[8];
1570 UINT16 wLength;
1571 UINT16 ii, jj;
1572 //UINT32 i;
1573 //UINT8 bFaradayAP[4]={0x10,0x23,0x78,0x56};
1574 //UINT8 pbData[128]; //yuwen ,move to USB global SDRAM
1575 //UINT32 wSizeTemp;
1576 UINT8 bReturn;
1577
1578 //printf("\r\n Enumerate",0);
1579
1580 #if defined(Enable_Issue_TestPacket) && !defined(ENABLE_HOST_TEST)
1581 MY_HAL_DCACHE_FLUSH((U32)pUsbData, 128);
1582 IssueTestPacket(pUsbData);
1583 #endif
1584
1585 bReturn=1;
1586 //sAttachDevice.bISOTransferEnable=0;
1587 gUsbTimeout=3;
1588
1589 //<1>.GetDescriptor - Device (8 Bytes)
1590 MsOS_DelayTask(10);
1591 if (flib_Host20_FirstTransfer()>0)
1592 {
1593 return(0);//Fail
1594 }
1595 gUsbTimeout=11; //extend usb timeout , for some really slow response HD
1596
1597 //Set the ep0 max packet size
1598
1599 pHost20_qHD_List_Control0->bMaxPacketSize=sAttachDevice.sDD.bEP0MAXPACKETSIZE;
1600 //psHost20_qHD_List_Control[0]->bMaxPacketSize1=sAttachDevice.sDD.bEP0MAXPACKETSIZE>>8;
1601
1602 pHost20_qHD_List_Control1->bMaxPacketSize=sAttachDevice.sDD.bEP0MAXPACKETSIZE;
1603 //psHost20_qHD_List_Control[1]->bMaxPacketSize1=sAttachDevice.sDD.bEP0MAXPACKETSIZE>>8;
1604
1605 //printf("max packet:%02bx\n",sAttachDevice.sDD.bEP0MAXPACKETSIZE);
1606 //printf("max packet:%02bx\n",Host20_qHD_List_Control0.bMaxPacketSize0);
1607
1608 MsOS_DelayTask(10);
1609 #if 1
1610 //<2>.bNormalEnumerate =>Issue the Bus Reset
1611 //Issue Bus Reset
1612 //flib_Host20_Suspend();//Bruce;;add;;06102005
1613 //flib_Host20_TimerEnable(55);//Bruce;;add;;06102005
1614 //printf("bus reset again...\n");
1615 if(!NowIsHub)
1616 flib_Host20_PortBusReset();
1617 #endif
1618 //<2.2>.Get Descriptor again
1619 memcpy(bCMD,OTGH_GETDESCRIPTOR_DEVICE,8);
1620
1621 if (flib_Host20_Issue_Control (0,bCMD,0x12,(UINT8*)&(sAttachDevice.sDD))>0)
1622 {
1623 return(0);//Fail
1624 }
1625 MsOS_DelayTask(10);
1626 #if defined(Enable_Issue_TestPacket) && !defined(ENABLE_HOST_TEST)
1627 IssueTestPacket(pUsbData);
1628 #endif
1629 #ifdef Enable_SOF_Only
1630 printf("Repeat FS SOF...\n");
1631 while (1)
1632 {
1633 MsOS_DelayTask(100);
1634 }
1635 #endif
1636
1637
1638 //if (bNormalEnumerate>0)
1639 // if (memcmp(bFaradayAP,&(sAttachDevice.sDD.bVIDLowByte),4)>0)
1640 // {//This is not the Faraday Test AP
1641 // printf(">>>Error:This is not the Faraday Test AP...\n");
1642 // flib_DumpDeviceDescriptor(&(sAttachDevice.sDD));
1643 // bReturn=2;
1644 // }
1645
1646 //<3>.Set Address to i
1647 memcpy(bCMD,OTGH_SETADDRESS,8);
1648 sAttachDevice.bAdd=bAddress;
1649 pHost20_qHD_List_Control1->bDeviceAddress=sAttachDevice.bAdd;
1650 bCMD[2]=sAttachDevice.bAdd;
1651 if (flib_Host20_Issue_Control(0,bCMD,0,OTGH_NULL)>0)
1652 {
1653 return(0);//Fail
1654 }
1655 MsOS_DelayTask(100); //leave some time for device to be ready
1656 //<4>.GetDescriptor - Configuration (8Bytes)
1657 memcpy(bCMD,OTGH_GETDESCRIPTOR_CONFIG,8);
1658 //pbData=USBMALLOC(1000 );
1659 //for (i=0;i<1000;i++)
1660 // *(pbData+i)=0;
1661 if (flib_Host20_Issue_Control (1,bCMD,0x08,pUsbData)>0)
1662 {
1663 return(0);//Fail
1664 }
1665 MsOS_DelayTask(10);
1666
1667 wLength=*(pUsbData+2)+((*(pUsbData+3))<<8);
1668
1669 //<5>.GetDescriptor - Configuration (Y-Bytes = wLength)
1670 if (wLength > CONTROL_BUF_LEN)
1671 {
1672 HOSTLIB_MSG(printf("Warning too long descriptor\n"));
1673 return(0);//Fail
1674 }
1675
1676 bCMD[6]=*(pUsbData+2);//Low Byte
1677 bCMD[7]=*(pUsbData+3) ;//High Byte
1678 if (flib_Host20_Issue_Control (1,bCMD,wLength,pUsbData)>0)
1679 {
1680 return(0);//Fail
1681 }
1682 MsOS_DelayTask(10);
1683
1684
1685 //printf("pUsbData:%x\n",wLength);
1686 //Dump_Data((UINT16)pUsbData,wLength);
1687 flib_Host20_AnalysisConfigyration(pUsbData);
1688
1689 //Give it a try
1690
1691 //-----------do test mode -------------
1692
1693 #ifdef ENABLE_HOST_TEST
1694 if ((sAttachDevice.sDD.bVIDHighByte==0x1a)&&(sAttachDevice.sDD.bVIDLowByte==0x0a))
1695 {
1696 EnterTestMode();
1697 }
1698 #endif
1699 //--- test mode end---------------
1700
1701 //<6>.If OTG-Descriptor exist => then return to issue the HNP_Enable
1702 if (bNormalEnumerate==0)
1703 {//To check the OTG Descriptor
1704 if (sAttachDevice.sOTG.bED_OTG_bDescriptorType==9)
1705 {
1706 return(1);//Fail
1707 }
1708 }
1709 MsOS_DelayTask(10);
1710
1711 //<7>.Get String
1712 //sAttachDevice.bStringLanguage[0]=0;
1713 //sAttachDevice.bStringManufacture[0]=0;
1714 //sAttachDevice.bStringProduct[0]=0;
1715 //sAttachDevice.bStringSerialN[0]=0;
1716
1717 sAttachDevice.bStringLanguage[0]=0;
1718 sAttachDevice.bStringManufacture[0]=0;
1719 sAttachDevice.bStringProduct[0]=0;
1720
1721 if ((sAttachDevice.sDD.bManufacturer == 0) && (sAttachDevice.sDD.bProduct == 0))
1722 goto Get_Str_Done;
1723
1724 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
1725 bCMD[6] = 4;
1726 if (flib_Host20_Issue_Control (1,bCMD,4, pUsbData)>0)
1727 {
1728 goto Get_Str_Done;
1729 //return(0);//Fail
1730 }
1731 MsOS_DelayTask(1);
1732 sAttachDevice.bStringLanguage[0] = pUsbData[2];
1733 sAttachDevice.bStringLanguage[1] = pUsbData[3];
1734
1735 if (sAttachDevice.sDD.bManufacturer != 0)
1736 {
1737 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
1738 bCMD[2] = sAttachDevice.sDD.bManufacturer;
1739 bCMD[4] = sAttachDevice.bStringLanguage[0];
1740 bCMD[5] = sAttachDevice.bStringLanguage[1];
1741 bCMD[6] = 2;
1742
1743 if (flib_Host20_Issue_Control (1,bCMD,2, pUsbData)>0)
1744 {
1745 return(0);//Fail
1746 }
1747 MsOS_DelayTask(1);
1748
1749 bCMD[6] = pUsbData[0]; //real string length
1750 if (flib_Host20_Issue_Control (1,bCMD,bCMD[6], pUsbData)>0)
1751 {
1752 return(0);//Fail
1753 }
1754 MsOS_DelayTask(1);
1755
1756 for (ii=2, jj=0; ii<pUsbData[0]; ii+=2, jj++)
1757 sAttachDevice.bStringManufacture[jj] = pUsbData[ii];
1758
1759 sAttachDevice.bStringManufacture[jj] = 0;
1760
1761 #if 0
1762 printf("bStringManufacture: ");
1763
1764 for (ii=0; ii<64; ii++)
1765 {
1766 printf("%c", sAttachDevice.bStringManufacture[ii]);
1767 if (sAttachDevice.bStringManufacture[ii] == 0)
1768 break;
1769 }
1770
1771 printf("\n");
1772 #endif
1773 }
1774
1775 if (sAttachDevice.sDD.bProduct != 0)
1776 {
1777 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
1778 bCMD[2] = sAttachDevice.sDD.bProduct;
1779 bCMD[4] = sAttachDevice.bStringLanguage[0];
1780 bCMD[5] = sAttachDevice.bStringLanguage[1];
1781 bCMD[6] = 2;
1782
1783 if (flib_Host20_Issue_Control (1,bCMD,2, pUsbData)>0)
1784 {
1785 return(0);//Fail
1786 }
1787 MsOS_DelayTask(1);
1788
1789 bCMD[6] = pUsbData[0]; //real string length
1790 if(bCMD[6] > 128)
1791 {//Alan.yu 2010.12.17 : for some bad devices return wrong length larger then 128, which will over-write the memory after pUsbData[]
1792 bCMD[6] = 128;
1793 }
1794 if (flib_Host20_Issue_Control (1,bCMD,bCMD[6], pUsbData)>0)
1795 {
1796 return(0);//Fail
1797 }
1798 MsOS_DelayTask(1);
1799
1800 for (ii=2, jj=0; ii<pUsbData[0]; ii+=2, jj++)
1801 sAttachDevice.bStringProduct[jj] = pUsbData[ii];
1802
1803 sAttachDevice.bStringProduct[jj] = 0;
1804
1805 #if 0
1806 printf("bStringProduct: ");
1807
1808 for (ii=0; ii<pUsbData[0]-2; ii++)
1809 {
1810 printf("%c", sAttachDevice.bStringProduct[ii]);
1811 if (sAttachDevice.bStringProduct[ii] == 0)
1812 break;
1813 }
1814
1815 printf("\n");
1816 #endif
1817 }
1818 //#endif //Skip Get String
1819
1820 Get_Str_Done:
1821 //<8>.Set Configurarion to 0
1822 memcpy(bCMD,OTGH_SETCONFIGURATION,8);
1823 bCMD[2]=sAttachDevice.saCD[0].bConfigurationValue;
1824 if (flib_Host20_Issue_Control (1,bCMD,0x00,OTGH_NULL)>0)
1825 {
1826 return(0);//Fail
1827 }
1828 MsOS_DelayTask(USB_ENUM_DELAY); //leave some time for device to be ready
1829
1830 //<9>.Printf the Device-Descriptor/Configuration-Descriptor
1831
1832 return (bReturn);
1833 }
1834 #else // support device enumeration dividable
_flib_Host20_Enumerate(UINT8 bNormalEnumerate,UINT8 bAddress)1835 UINT8 _flib_Host20_Enumerate (UINT8 bNormalEnumerate,UINT8 bAddress)
1836 {
1837 UINT8 bCMD[8];
1838 UINT16 wLength;
1839 UINT16 ii, jj;
1840 //UINT32 i;
1841 //UINT8 bFaradayAP[4]={0x10,0x23,0x78,0x56};
1842 //UINT8 pbData[128]; //yuwen ,move to USB global SDRAM
1843 //UINT32 wSizeTemp;
1844 UINT8 bReturn;
1845
1846 //printf("\r\n Enumerate",0);
1847
1848 #if defined(Enable_Issue_TestPacket) && !defined(ENABLE_HOST_TEST)
1849 MY_HAL_DCACHE_FLUSH((U32)pUsbData, 128);
1850 IssueTestPacket(pUsbData);
1851 #endif
1852
1853 bReturn=1;
1854 //sAttachDevice.bISOTransferEnable=0;
1855 gUsbTimeout=3;
1856
1857 //<1>.GetDescriptor - Device (8 Bytes)
1858 MsOS_DelayTask(10);
1859 if (flib_Host20_FirstTransfer()>0)
1860 {
1861 return(0);//Fail
1862 }
1863 gUsbTimeout=11; //extend usb timeout , for some really slow response HD
1864
1865 //Set the ep0 max packet size
1866
1867 pHost20_qHD_List_Control0->bMaxPacketSize=sAttachDevice.sDD.bEP0MAXPACKETSIZE;
1868 //psHost20_qHD_List_Control[0]->bMaxPacketSize1=sAttachDevice.sDD.bEP0MAXPACKETSIZE>>8;
1869
1870 pHost20_qHD_List_Control1->bMaxPacketSize=sAttachDevice.sDD.bEP0MAXPACKETSIZE;
1871 //psHost20_qHD_List_Control[1]->bMaxPacketSize1=sAttachDevice.sDD.bEP0MAXPACKETSIZE>>8;
1872
1873 //printf("max packet:%02bx\n",sAttachDevice.sDD.bEP0MAXPACKETSIZE);
1874 //printf("max packet:%02bx\n",Host20_qHD_List_Control0.bMaxPacketSize0);
1875
1876 MsOS_DelayTask(10);
1877 #if 1
1878 //<2>.bNormalEnumerate =>Issue the Bus Reset
1879 //Issue Bus Reset
1880 //flib_Host20_Suspend();//Bruce;;add;;06102005
1881 //flib_Host20_TimerEnable(55);//Bruce;;add;;06102005
1882 //printf("bus reset again...\n");
1883 if(!NowIsHub)
1884 flib_Host20_PortBusReset();
1885 #endif
1886 //<2.2>.Get Descriptor again
1887 memcpy(bCMD,OTGH_GETDESCRIPTOR_DEVICE,8);
1888
1889 if (flib_Host20_Issue_Control (0,bCMD,0x12,(UINT8*)&(sAttachDevice.sDD))>0)
1890 {
1891 return(0);//Fail
1892 }
1893 MsOS_DelayTask(10);
1894 #if defined(Enable_Issue_TestPacket) && !defined(ENABLE_HOST_TEST)
1895 IssueTestPacket(pUsbData);
1896 #endif
1897 #ifdef Enable_SOF_Only
1898 printf("Repeat FS SOF...\n");
1899 while (1)
1900 {
1901 MsOS_DelayTask(100);
1902 }
1903 #endif
1904
1905
1906 //if (bNormalEnumerate>0)
1907 // if (memcmp(bFaradayAP,&(sAttachDevice.sDD.bVIDLowByte),4)>0)
1908 // {//This is not the Faraday Test AP
1909 // printf(">>>Error:This is not the Faraday Test AP...\n");
1910 // flib_DumpDeviceDescriptor(&(sAttachDevice.sDD));
1911 // bReturn=2;
1912 // }
1913
1914 //<3>.Set Address to i
1915 memcpy(bCMD,OTGH_SETADDRESS,8);
1916 sAttachDevice.bAdd=bAddress;
1917 pHost20_qHD_List_Control1->bDeviceAddress=sAttachDevice.bAdd;
1918 bCMD[2]=sAttachDevice.bAdd;
1919 if (flib_Host20_Issue_Control(0,bCMD,0,OTGH_NULL)>0)
1920 {
1921 return(0);//Fail
1922 }
1923 MsOS_DelayTask(100); //leave some time for device to be ready
1924 //<4>.GetDescriptor - Configuration (8Bytes)
1925 memcpy(bCMD,OTGH_GETDESCRIPTOR_CONFIG,8);
1926 //pbData=USBMALLOC(1000 );
1927 //for (i=0;i<1000;i++)
1928 // *(pbData+i)=0;
1929 if (flib_Host20_Issue_Control (1,bCMD,0x08,pUsbData)>0)
1930 {
1931 return(0);//Fail
1932 }
1933 MsOS_DelayTask(10);
1934
1935 wLength=*(pUsbData+2)+((*(pUsbData+3))<<8);
1936
1937 //<5>.GetDescriptor - Configuration (Y-Bytes = wLength)
1938 if (wLength > CONTROL_BUF_LEN)
1939 {
1940 HOSTLIB_MSG(printf("Warning too long descriptor\n"));
1941 return(0);//Fail
1942 }
1943
1944 bCMD[6]=*(pUsbData+2);//Low Byte
1945 bCMD[7]=*(pUsbData+3) ;//High Byte
1946 if (flib_Host20_Issue_Control (1,bCMD,wLength,pUsbData)>0)
1947 {
1948 return(0);//Fail
1949 }
1950 MsOS_DelayTask(10);
1951
1952
1953 //printf("pUsbData:%x\n",wLength);
1954 //Dump_Data((UINT16)pUsbData,wLength);
1955 flib_Host20_AnalysisConfigyration(pUsbData);
1956
1957 //Give it a try
1958
1959 //-----------do test mode -------------
1960
1961 #ifdef ENABLE_HOST_TEST
1962 if ((sAttachDevice.sDD.bVIDHighByte==0x1a)&&(sAttachDevice.sDD.bVIDLowByte==0x0a))
1963 {
1964 EnterTestMode();
1965 }
1966 #endif
1967 //--- test mode end---------------
1968
1969 //<6>.If OTG-Descriptor exist => then return to issue the HNP_Enable
1970 if (bNormalEnumerate==0)
1971 {//To check the OTG Descriptor
1972 if (sAttachDevice.sOTG.bED_OTG_bDescriptorType==9)
1973 {
1974 return(1);//Fail
1975 }
1976 }
1977 MsOS_DelayTask(10);
1978
1979 //<7>.Get String
1980 //sAttachDevice.bStringLanguage[0]=0;
1981 //sAttachDevice.bStringManufacture[0]=0;
1982 //sAttachDevice.bStringProduct[0]=0;
1983 //sAttachDevice.bStringSerialN[0]=0;
1984
1985 sAttachDevice.bStringLanguage[0]=0;
1986 sAttachDevice.bStringManufacture[0]=0;
1987 sAttachDevice.bStringProduct[0]=0;
1988
1989 if ((sAttachDevice.sDD.bManufacturer == 0) && (sAttachDevice.sDD.bProduct == 0))
1990 goto Get_Str_Done;
1991
1992 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
1993 bCMD[6] = 4;
1994 if (flib_Host20_Issue_Control (1,bCMD,4, pUsbData)>0)
1995 {
1996 goto Get_Str_Done;
1997 //return(0);//Fail
1998 }
1999 MsOS_DelayTask(1);
2000 sAttachDevice.bStringLanguage[0] = pUsbData[2];
2001 sAttachDevice.bStringLanguage[1] = pUsbData[3];
2002
2003 if (sAttachDevice.sDD.bManufacturer != 0)
2004 {
2005 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
2006 bCMD[2] = sAttachDevice.sDD.bManufacturer;
2007 bCMD[4] = sAttachDevice.bStringLanguage[0];
2008 bCMD[5] = sAttachDevice.bStringLanguage[1];
2009 bCMD[6] = 2;
2010
2011 if (flib_Host20_Issue_Control (1,bCMD,2, pUsbData)>0)
2012 {
2013 return(0);//Fail
2014 }
2015 MsOS_DelayTask(1);
2016
2017 bCMD[6] = pUsbData[0]; //real string length
2018 if (flib_Host20_Issue_Control (1,bCMD,bCMD[6], pUsbData)>0)
2019 {
2020 return(0);//Fail
2021 }
2022 MsOS_DelayTask(1);
2023
2024 for (ii=2, jj=0; ii<pUsbData[0]; ii+=2, jj++)
2025 sAttachDevice.bStringManufacture[jj] = pUsbData[ii];
2026
2027 sAttachDevice.bStringManufacture[jj] = 0;
2028
2029 #if 0
2030 printf("bStringManufacture: ");
2031
2032 for (ii=0; ii<64; ii++)
2033 {
2034 printf("%c", sAttachDevice.bStringManufacture[ii]);
2035 if (sAttachDevice.bStringManufacture[ii] == 0)
2036 break;
2037 }
2038
2039 printf("\n");
2040 #endif
2041 }
2042
2043 if (sAttachDevice.sDD.bProduct != 0)
2044 {
2045 memcpy(bCMD, OTGH_GETDESCRIPTOR_STR, 8);
2046 bCMD[2] = sAttachDevice.sDD.bProduct;
2047 bCMD[4] = sAttachDevice.bStringLanguage[0];
2048 bCMD[5] = sAttachDevice.bStringLanguage[1];
2049 bCMD[6] = 2;
2050
2051 if (flib_Host20_Issue_Control (1,bCMD,2, pUsbData)>0)
2052 {
2053 return(0);//Fail
2054 }
2055 MsOS_DelayTask(1);
2056
2057 bCMD[6] = pUsbData[0]; //real string length
2058 if(bCMD[6] > 128)
2059 {//Alan.yu 2010.12.17 : for some bad devices return wrong length larger then 128, which will over-write the memory after pUsbData[]
2060 bCMD[6] = 128;
2061 }
2062 if (flib_Host20_Issue_Control (1,bCMD,bCMD[6], pUsbData)>0)
2063 {
2064 return(0);//Fail
2065 }
2066 MsOS_DelayTask(1);
2067
2068 for (ii=2, jj=0; ii<pUsbData[0]; ii+=2, jj++)
2069 sAttachDevice.bStringProduct[jj] = pUsbData[ii];
2070
2071 sAttachDevice.bStringProduct[jj] = 0;
2072
2073 #if 0
2074 printf("bStringProduct: ");
2075
2076 for (ii=0; ii<pUsbData[0]-2; ii++)
2077 {
2078 printf("%c", sAttachDevice.bStringProduct[ii]);
2079 if (sAttachDevice.bStringProduct[ii] == 0)
2080 break;
2081 }
2082
2083 printf("\n");
2084 #endif
2085 }
2086 //#endif //Skip Get String
2087
2088 Get_Str_Done:
2089 //<8>.Set Configurarion to 0
2090 memcpy(bCMD,OTGH_SETCONFIGURATION,8);
2091 bCMD[2]=sAttachDevice.saCD[0].bConfigurationValue;
2092 if (flib_Host20_Issue_Control (1,bCMD,0x00,OTGH_NULL)>0)
2093 {
2094 return(0);//Fail
2095 }
2096
2097 return (bReturn);
2098 }
flib_Host20_Enumerate(UINT8 bNormalEnumerate,UINT8 bAddress)2099 UINT8 flib_Host20_Enumerate (UINT8 bNormalEnumerate,UINT8 bAddress)
2100 {
2101 UINT8 bReturn;
2102
2103 bReturn = _flib_Host20_Enumerate(bNormalEnumerate, bAddress);
2104 if (!bReturn)
2105 return(bReturn);
2106
2107 MsOS_DelayTask(USB_ENUM_DELAY); //leave some time for device to be ready
2108
2109 return (1);
2110 }
2111 #endif
2112
2113
2114
2115
2116 //************************************************************************************************************
2117 //************************************************************************************************************
2118 // *** Group-4:Structure Function ***
2119 //*************************************************************************************************************
2120 //************************************************************************************************************
2121 //====================================================================
2122 // * Function Name: flib_Host20_InitStructure
2123 // * Description:
2124 // 1.Init qHD for Control
2125 // qHD_C-->qHD_C-->qHD_C
2126 // 2.Init qHD for Bulk
2127 // |-------------------------|
2128 // qHD_C-->qHD_C-->qHD_B-->qHD_B
2129 //
2130 // 3.Init qHD for Interrupt
2131 // 4.Init iTD for ISO (Reserved for feature)
2132 // * Input:Type =0 =>iTD
2133 // =1 =>qTD
2134 // =2
2135 // * OutPut: 0:Fail
2136 // 1:ok
2137 //====================================================================
2138
flib_Host20_InitStructure(void)2139 void flib_Host20_InitStructure(void)
2140 {
2141 U16 i;
2142 U8 *pData;
2143
2144 //<1>.Clear memory
2145
2146 // XBYTE[Host20_STRUCTURE_qHD_BASE_ADDRESS+i]=0x00;
2147
2148 //for ( i=0 ; i < Host20_qTD_SIZE*Host20_qTD_MAX ; i++)
2149 // XBYTE[Host20_STRUCTURE_qTD_BASE_ADDRESS+i]=0x00;
2150 //printf("UsbCtrlBuf size: %d\n", sizeof(UsbCtrlBuf));
2151 pUsbCtrlBuf=(U8*) KSEG02KSEG1(UsbCtrlBuf);
2152 MY_HAL_DCACHE_FLUSH((U32)UsbCtrlBuf, sizeof(UsbCtrlBuf));
2153
2154 // ASSERT(pUsbCtrlBuf != NULL)
2155
2156 //printf("QtdBuf size: %d\n", sizeof(QtdBuf));
2157 qTD_Base_Buf=(U8*) KSEG02KSEG1(QtdBuf);
2158 MY_HAL_DCACHE_FLUSH((U32)QtdBuf, Host20_qTD_SIZE*Host20_qTD_MAX+0x20);
2159
2160 // ASSERT(qTD_Base_Buf != NULL)
2161 Host20_STRUCTURE_qTD_BASE_ADDRESS=qTD_Base_Buf;
2162
2163 pData=qTD_Base_Buf;
2164 for (i=0 ; i < (Host20_qTD_SIZE*Host20_qTD_MAX+0x20) ; i++)
2165 pData[i]=0;
2166
2167 if ((U32)Host20_STRUCTURE_qTD_BASE_ADDRESS& 0x10)
2168 Host20_STRUCTURE_qTD_BASE_ADDRESS+=0x10; //make it aligned with 32
2169 pHost20_qHD_List_Control0=(qHD_Structure*)KSEG02KSEG1(&Host20_qHD_List_Control0);
2170 pHost20_qHD_List_Control1=(qHD_Structure*)KSEG02KSEG1(&Host20_qHD_List_Control1);
2171 pHost20_qHD_List_Bulk0=(qHD_Structure*)KSEG02KSEG1(&Host20_qHD_List_Bulk0);
2172 pHost20_qHD_List_Bulk1=(qHD_Structure*)KSEG02KSEG1(&Host20_qHD_List_Bulk1);
2173 MY_HAL_DCACHE_FLUSH((U32)&Host20_qHD_List_Control0, sizeof(qHD_Structure));
2174 MY_HAL_DCACHE_FLUSH((U32)&Host20_qHD_List_Control1, sizeof(qHD_Structure));
2175 MY_HAL_DCACHE_FLUSH((U32)&Host20_qHD_List_Bulk0, sizeof(qHD_Structure));
2176 MY_HAL_DCACHE_FLUSH((U32)&Host20_qHD_List_Bulk1, sizeof(qHD_Structure));
2177
2178 pData=(U8*)pHost20_qHD_List_Control0;
2179 for ( i=0 ; i < sizeof(qHD_Structure); i++)
2180 pData[i]=0;
2181 pData=(U8*)pHost20_qHD_List_Control1;
2182 for ( i=0 ; i < sizeof(qHD_Structure); i++)
2183 pData[i]=0;
2184 pData=(U8*)pHost20_qHD_List_Bulk0;
2185 for ( i=0 ; i < sizeof(qHD_Structure); i++)
2186 pData[i]=0;
2187 pData=(U8*)pHost20_qHD_List_Bulk1;
2188 for ( i=0 ; i < sizeof(qHD_Structure); i++)
2189 pData[i]=0;
2190
2191 Host20_STRUCTURE_qHD_BASE_ADDRESS=(U8*)pHost20_qHD_List_Control0;
2192
2193 DEBUG_USB(printf("qhd:%lx\n", (U32)pHost20_qHD_List_Control0));
2194 //<2>.For qTD & iTD & 4K-Buffer Manage init
2195 for (i=0;i<Host20_qTD_MAX;i++)
2196 Host20_qTD_Manage[i]=Host20_MEM_FREE;
2197
2198
2199 // sAttachDevice.bDataBuffer=flib_Host20_GetStructure(Host20_MEM_TYPE_4K_BUFFER);//For Control
2200
2201
2202 //
2203 // printf("List_control 0:%x\n",(U16) pHost20_qHD_List_Control0);
2204 #if 1
2205 //psHost20_qHD_List_Control[0]->bType=HOST20_HD_Type_QH;
2206 flib_Host20_Allocate_QHD(pHost20_qHD_List_Control0,HOST20_HD_Type_QH,0,1,0,8);//Address=0,Head=1,EndPt=0,Size
2207
2208 flib_Host20_Allocate_QHD(pHost20_qHD_List_Control1,HOST20_HD_Type_QH,1,0,0,64);//Address=1,Head=0,EndPt=0,Size
2209
2210 #endif
2211
2212
2213
2214 #if 1
2215 flib_Host20_Allocate_QHD(pHost20_qHD_List_Bulk0,HOST20_HD_Type_QH,1,0,1,64);//Address=1,Head=0,EndPt=1,Size
2216 flib_Host20_Allocate_QHD(pHost20_qHD_List_Bulk1,HOST20_HD_Type_QH,1,0,2,64);//Address=1,Head=0,EndPt=2,Size
2217 #endif
2218
2219
2220 //<3.3>.Link the qHD (contol)
2221 #if 1
2222
2223 pHost20_qHD_List_Control0->bNextQHDPointer=(VirtoPhyAddr((U32)pHost20_qHD_List_Control1)>>5);
2224 pHost20_qHD_List_Control1->bNextQHDPointer=(VirtoPhyAddr((U32)pHost20_qHD_List_Control0)>>5);
2225
2226 // SetPointer(&(pHost20_qHD_List_Control0->bNextQHDPointer3),VirtoPhyAddr((U16)pHost20_qHD_List_Control1));
2227 //SetPointer(&(pHost20_qHD_List_Control1->bNextQHDPointer3),VirtoPhyAddr((U16)pHost20_qHD_List_Control0));
2228 // SetPointer(&(Host20_qHD_List_Work.bNextQHDPointer3),VirtoPhyAddr((U16)&Host20_qHD_List_Work));
2229
2230 #endif
2231
2232 }
2233
2234 //====================================================================
2235 // * Function Name: flib_Host20_GetStructure
2236 // * Description:
2237 //
2238 // * Input:Type =0 =>qTD
2239 // =1 =>iTD
2240 // =2 =>4K Buffer
2241 // * OutPut: 0:Fail
2242 // ~0:Addrress
2243 //====================================================================
flib_Host20_GetStructure(U8 Type)2244 qTD_Structure *flib_Host20_GetStructure( U8 Type)
2245 {
2246 U32 i;
2247 U8 bFound;
2248 // U16 spTempqTD;
2249 qTD_Structure *spTempqTD;
2250 // iTD_Structure *spTempiTD;
2251 // siTD_Structure *spTempsiTD;
2252 bFound=0;
2253
2254 switch(Type)
2255 {
2256 case Host20_MEM_TYPE_qTD:
2257
2258 //For qTD
2259
2260 for (i=0;i<Host20_qTD_MAX;i++)
2261 if (Host20_qTD_Manage[i]==Host20_MEM_FREE)
2262 {
2263 bFound=1;
2264 Host20_qTD_Manage[i]=Host20_MEM_USED;
2265 break;
2266 }
2267
2268
2269 if (bFound==1)
2270 {
2271 //printf("USB base:%lx \n",USB_BUFFER_START_ADR);
2272
2273 spTempqTD=(qTD_Structure *)((U32)Host20_STRUCTURE_qTD_BASE_ADDRESS+i*Host20_qTD_SIZE);
2274 memset((unsigned char *)spTempqTD ,0, Host20_qTD_SIZE);
2275 spTempqTD->bTerminate=1; //Bit0
2276 spTempqTD->bStatus_Active=0; //Bit7
2277 spTempqTD->bInterruptOnComplete=1; //Bit15
2278
2279 spTempqTD->bAlternateTerminate=1;
2280 spTempqTD->bErrorCounter=3;
2281 return (spTempqTD);
2282 }
2283 else
2284 {
2285 HOSTLIB_MSG(printf("QTD underrun!\n"));
2286 }
2287 break;
2288
2289 default:
2290 return 0;
2291 break;
2292 }
2293
2294 //Not Found...
2295 // while(1)
2296 //{
2297 // printf("i:%d",i);
2298 // }
2299 return (0);
2300 }
2301
2302 //====================================================================
2303 // * Function Name: flib_Host20_ReleaseStructure
2304 // * Description:
2305 //
2306 // * Input:Type =0 =>qTD
2307 // =1 =>iTD
2308 // =2
2309 // * OutPut: 0:Fail
2310 // ~0:Addrress
2311 //====================================================================
2312
2313
2314
flib_Host20_ReleaseStructure(U8 Type,U32 pwAddress)2315 void flib_Host20_ReleaseStructure(U8 Type,U32 pwAddress)
2316 {
2317 //U8 i;
2318 U16 wReleaseNum;
2319 U8 *pData;
2320
2321 // printf("release QTD:%x\n",pwAddress);
2322 pData=(U8*)pwAddress;
2323
2324 switch(Type)
2325 {
2326 case Host20_MEM_TYPE_qTD:
2327
2328 if (pwAddress<(U32)Host20_STRUCTURE_qTD_BASE_ADDRESS)
2329 {
2330 HOSTLIB_MSG(printf("??? Memory release area fail...\n"));
2331 // while(1);
2332 }
2333
2334 if ((pwAddress-(U32)Host20_STRUCTURE_qTD_BASE_ADDRESS)==0)
2335 wReleaseNum=0;
2336 else
2337 wReleaseNum=(pwAddress-(U32)Host20_STRUCTURE_qTD_BASE_ADDRESS)/Host20_qTD_SIZE;
2338
2339 if (wReleaseNum>=Host20_qTD_MAX)//ERROR FIX Prevent Tool 070522
2340 {
2341 HOSTLIB_MSG(printf("Memory release area fail...\n"));
2342 return;
2343 // while(1);
2344 }
2345
2346 Host20_qTD_Manage[wReleaseNum]=Host20_MEM_FREE;
2347
2348 // Removed, for improve the performance,
2349 // because we will clear qtd structure when we get it, so don��t have to do it when release it.
2350 //for (i=0;i<Host20_qTD_SIZE ;i++) //qTD size=32 bytes
2351 // *(pData+i)=0;
2352 break;
2353
2354 default:
2355 // printf("??? Memory release type fail...\n");
2356 // while(1);
2357 break;
2358
2359 }
2360
2361
2362 }
2363 //====================================================================
2364 // * Function Name: flib_Host20_QHD_Control_Init
2365 // * Description:
2366 //
2367 // * Input:Type =0 =>qTD
2368 // =1 =>iTD
2369 // =2
2370 // * OutPut: 0:Fail
2371 // ~0:Addrress
2372 //====================================================================
flib_Host20_QHD_Control_Init(void)2373 void flib_Host20_QHD_Control_Init(void)
2374 {
2375
2376
2377 //<1>.Init Control-0/1
2378 pHost20_qHD_List_Control0->bEdSpeed=gSpeed;
2379 //printf("bEdSpeed:%bx\n",sAttachDevice.bSpeed);
2380 pHost20_qHD_List_Control0->bInactiveOnNextTransaction=0;
2381 pHost20_qHD_List_Control0->bDataToggleControl=1;
2382
2383 pHost20_qHD_List_Control1->bEdSpeed=gSpeed;
2384 pHost20_qHD_List_Control1->bInactiveOnNextTransaction=0;
2385 pHost20_qHD_List_Control1->bDataToggleControl=1;
2386
2387 //<2>.Init Bulk-0/1
2388 pHost20_qHD_List_Bulk0->bEdSpeed=gSpeed;
2389 pHost20_qHD_List_Bulk0->bInactiveOnNextTransaction=0;
2390 pHost20_qHD_List_Bulk0->bDataToggleControl=0;
2391
2392 pHost20_qHD_List_Bulk1->bEdSpeed=gSpeed;
2393 pHost20_qHD_List_Bulk1->bInactiveOnNextTransaction=0;
2394 pHost20_qHD_List_Bulk1->bDataToggleControl=0;
2395
2396
2397 //printf("enable aynch \n");
2398 //<12>.Enable Asynchronous
2399
2400 //mbHost20_USBCMD_AsynchronousEnable_Set(); //Temp;;Bruce
2401 //printf("0x10:%02bx",XBYTE[0x2410]);
2402
2403
2404 }
2405
2406 //====================================================================
2407 // * Function Name: flib_Host20_Allocate_QHD
2408 // * Description:
2409 //
2410 // * Input:Type =0 =>qTD
2411 // =1 =>iTD
2412 // =2
2413 // * OutPut: 0:Fail
2414 // ~0:Addrress
2415 //====================================================================
2416
flib_Host20_Allocate_QHD(qHD_Structure * psQHTemp,U8 bNextType,U8 bAddress,U8 bHead,U8 bEndPt,U32 wMaxPacketSize)2417 void flib_Host20_Allocate_QHD(qHD_Structure *psQHTemp,U8 bNextType,U8 bAddress,U8 bHead,U8 bEndPt, U32 wMaxPacketSize)
2418 {
2419 qTD_Structure *spTempqTD;
2420 // U32 sp;
2421
2422 psQHTemp->bTerminate=0; //Bit0
2423 psQHTemp->bType=bNextType; //Bit2~1
2424
2425 psQHTemp->bDeviceAddress=bAddress; //Bit0~6
2426 psQHTemp->bEdNumber=bEndPt; //Bit11~8
2427 psQHTemp->bHeadOfReclamationListFlag=bHead; //Bit15
2428 psQHTemp->bMaxPacketSize=wMaxPacketSize; //Bit16~26
2429 psQHTemp->bNakCounter=Host20_QHD_Nat_Counter;
2430
2431 psQHTemp->bOverlay_NextTerminate=1;
2432 psQHTemp->bOverlay_AlternateNextTerminate=1;
2433
2434 //<2>.allocate dumy qTD
2435 #if 1
2436 //<2.1>.Allocate qTD
2437 spTempqTD=flib_Host20_GetStructure(0);//0=>qTD
2438 psQHTemp->bOverlay_NextqTD=(VirtoPhyAddr((U32)spTempqTD)>>5);
2439
2440 #endif
2441 //<2.3>.Active the qTD
2442 psQHTemp->bOverlay_NextTerminate=0;
2443
2444 }
2445
2446 //====================================================================
2447 // * Function Name: flib_Host20_CheckingForResult_QHD
2448 // * Description:
2449 // * Input:Type
2450 // * OutPut:
2451 //====================================================================
flib_Host20_CheckingForResult_QHD(qHD_Structure * spTempqHD)2452 UINT8 flib_Host20_CheckingForResult_QHD(qHD_Structure *spTempqHD)
2453 {
2454 UINT8 bQHStatus;
2455 U16 wIntStatus;
2456
2457 wIntStatus=mwHost20_USBSTS_Rd();
2458 //printf("USB int:%x\n",wIntStatus);
2459
2460 //<2>.Checking for the Error type interrupt => Halt the system
2461 if (wIntStatus&HOST20_USBINTR_SystemError)
2462 {
2463 DEBUG_USB(printf("???System Error... Halt the system...\n "));
2464 sAttachDevice.bSendStatusError=1;
2465
2466 }
2467 if (wIntStatus&HOST20_USBINTR_USBError) {
2468 mwHost20_USBSTS_USBError_Set();
2469 sAttachDevice.bSendStatusError=1;
2470 }
2471
2472 if (sAttachDevice.bSendStatusError==0)
2473 return HOST20_OK;
2474
2475 //<1>.Analysis the qHD Status
2476 sAttachDevice.bSendStatusError=0;
2477 //printf("??? USB Error Interrupt Event...\n");
2478 bQHStatus=spTempqHD->bOverlay_Status;
2479
2480 if (bQHStatus&HOST20_qTD_STATUS_Halted)
2481 {
2482 if (bQHStatus&HOST20_qTD_STATUS_TransactionError)
2483 {
2484 //printf("\r\n transaction error!!",0);
2485 gUsbStatus=USB_TRANS_ERROR; //trans error
2486 return HOST20_TRANSACTION_ERROR;
2487 }
2488
2489 //printf("??? qHD Status => Halted (<1>.Stall/<2>.Babble/<3>.Error Counter=0)...(Device Not Supported...)\n");
2490 spTempqHD->bOverlay_Status=0; //clear halt status
2491 spTempqHD->bOverlay_TotalBytes=0;
2492 spTempqHD->bOverlay_Direction=0;
2493 spTempqHD->bOverlay_CurrentOffset=0; //clear offset
2494 if (bQHStatus==HOST20_qTD_STATUS_Halted) //no other error status
2495 {
2496 //printf("STALL\n");
2497 return HOST20_DEVICE_STALL;
2498 }
2499 }
2500 if (bQHStatus&HOST20_qTD_STATUS_BufferError)
2501 {
2502 // kcj 2007-06-07 printf("??? qHD Status => HOST20_qTD_STATUS_BufferError...\n");
2503 }
2504 if (bQHStatus&HOST20_qTD_STATUS_Babble)
2505 {
2506 DEBUG_USB(printf("??? qHD Status => HOST20_qTD_STATUS_Babble...\n"));
2507 //sAttachDevice.bSendStatusError=0; //don't fix babble error for Bert
2508 #ifdef Enable_Low_Temperature_Patch
2509 if(gSpeed==0)
2510 {
2511 //printf("\r\n CDR toggle!!",0);
2512 UTMI_SETXBYTE(0x0a,UTMI_READXBYTE(0x0a)^0x10); //invert CDR_CLOCK
2513 UTMI_ORXBYTE(0x06,0x03); //reset UTMI
2514 UTMI_ANDXBYTE(0x06,0xfc);
2515 }
2516 #endif
2517 spTempqHD->bOverlay_Status=0;
2518 return HOST20_OK;
2519
2520 }
2521 if (bQHStatus&HOST20_qTD_STATUS_MissMicroFrame)
2522 {
2523 // kcj 2007-06-07 printf("??? qHD Status => HOST20_qTD_STATUS_MissMicroFrame...\n");
2524 }
2525 //<2>.Clear the status
2526 spTempqHD->bOverlay_Status=0;
2527
2528 return HOST20_FAIL;
2529
2530
2531 }
2532
2533 #if defined(CPU_TYPE_MIPS) || defined(CPU_TYPE_ARM)
2534 extern void MsOS_FlushMemory(void);
2535 extern void MsOS_ReadMemory(void);
USB_Chip_Read_Memory(void)2536 void USB_Chip_Read_Memory(void)
2537 {
2538 #if 0
2539 volatile unsigned int *pu8;
2540 volatile unsigned int t ;
2541
2542 // Transfer the memory to noncache memory
2543 pu8 = ((volatile unsigned int *)0xA0380000);
2544 t = pu8[0] ;
2545 t = pu8[64] ;
2546 #else
2547 MsOS_ReadMemory();
2548 #endif
2549 }
USB_Chip_Flush_Memory(void)2550 void USB_Chip_Flush_Memory(void)
2551
2552 {
2553 MsOS_FlushMemory();
2554 }
2555 #endif
2556
2557 #if 1
2558
2559 //====================================================================
2560 // * Function Name: flib_Host20_Send_qTD
2561 // * Description:
2562 // Case-1:1qTD
2563 // Case-2:2qTD
2564 // Case-3:3qTD above
2565 // * Input:Type
2566 // * OutPut: 0 => OK
2567 // 1 => TimeOut
2568 //====================================================================
flib_Host20_Send_qTD(qTD_Structure * spHeadqTD,qHD_Structure * spTempqHD,U16 wTimeOutSec)2569 UINT8 flib_Host20_Send_qTD(qTD_Structure *spHeadqTD ,qHD_Structure *spTempqHD,U16 wTimeOutSec)
2570 {
2571 UINT8 bExitLoop,bReturnValue;
2572 qTD_Structure *spNewDumyqTD;
2573 qTD_Structure *spOldDumyqTD;
2574 qTD_Structure *spReleaseqTD;
2575 qTD_Structure *spReleaseqTDNext;
2576 qTD_Structure *spLastqTD;
2577 //UINT32 wDummyTemp;
2578 //UINT32 xdata *pwData;
2579 #ifdef ATV_SERISE_USE
2580 UINT32 wTimes;
2581 UINT32 StartTime=0;
2582 #else
2583 UINT16 wTimes;
2584 UINT32 StartTime;
2585 UINT32 LastTime;
2586 UINT16 FrameIdx=0;
2587
2588 #endif
2589
2590 #ifdef ATV_SERISE_USE
2591 wTimeOutSec=wTimeOutSec;
2592 #endif
2593
2594 gUsbStatus=USB_OK;
2595 //if (wTimeOutSec==0)
2596 //{
2597 // DEBUG_USB(printf("error, timeout sec is zero\n"));
2598 //}
2599 spOldDumyqTD=(qTD_Structure*)PhytoVirAddr((( U32)(spTempqHD->bOverlay_NextqTD))<<5);
2600
2601 //spTempqHD->bOverlay_Status|=HOST20_qTD_STATUS_Halted;
2602 while (mbHost20_USBCMD_AsynchronousEnable_Rd())
2603 {
2604 mbHost20_USBCMD_AsynchronousEnable_Clr();//pause asynchronous scheduler
2605 }
2606
2607 //spHeadqTD->bTerminate=1; //set to terminate
2608 memcpy(spOldDumyqTD,spHeadqTD,Host20_qTD_SIZE);
2609 //spOldDumyqTD->bStatus_Halted=1;
2610 if(RecoveryFlag&0x1)
2611 spOldDumyqTD->bDataToggle=DataToggleBackup;
2612
2613 //spOldDumyqTD->bStatus_Active=0;
2614
2615 //<2>.Prepare new dumy qTD
2616 spNewDumyqTD=spHeadqTD;
2617 memset((void *)spNewDumyqTD ,0, Host20_qTD_SIZE);
2618 spNewDumyqTD->bTerminate=1;
2619 //spNewDumyqTD->bAlternateTerminate=1;
2620 //spNewDumyqTD->bStatus_Halted=1;
2621 //<3>.Find spLastqTD & link spLastqTD to NewDumyqTD & Set NewDumyqTD->T=1
2622 spLastqTD=spOldDumyqTD;
2623 while(spLastqTD->bTerminate==0) {
2624 spLastqTD=(qTD_Structure*)PhytoVirAddr((( U32)(spLastqTD->bNextQTDPointer))<<5);
2625
2626 };
2627
2628 spLastqTD->bNextQTDPointer=(VirtoPhyAddr((U32)spNewDumyqTD)>>5);
2629
2630 spLastqTD->bTerminate=0;
2631
2632 //Link Alternate qTD pointer
2633 spLastqTD->bAlternateQTDPointer=(VirtoPhyAddr(( U32)spNewDumyqTD)>>5);
2634
2635 spLastqTD->bAlternateTerminate=0;
2636
2637
2638
2639 //<4>.Set OldDumyqTD->Active=1
2640 //gwLastqTDSendOK=0;
2641 //sAttachDevice.psSendLastqTD=spLastqTD;
2642 sAttachDevice.bSendStatusError=0;
2643
2644 //Dump_QTD(spOldDumyqTD);
2645 //Dump_QTD(spNewDumyqTD);
2646
2647 while (mwHost20_USBSTS_Rd() & 0x3b)
2648 {
2649 mwHost20_USBSTS_Set(0x3b); //clear interrupt, don't clear port change int
2650 }
2651 //bExitLoop=0;
2652 //spOldDumyqTD->bStatus_Halted=0;
2653 //spOldDumyqTD->bStatus_Halted=0;
2654
2655 #if 0
2656 printf("spOldDumyqTD: %X\n", spOldDumyqTD);
2657 printf("bTerminate: %X\n", spOldDumyqTD->bTerminate);
2658 printf("bNextQTDPointer: %X\n", spOldDumyqTD->bNextQTDPointer);
2659 printf("bAlternateTerminate: %X\n", spOldDumyqTD->bAlternateTerminate);
2660 printf("bAlternateQTDPointer: %X\n", spOldDumyqTD->bAlternateQTDPointer);
2661 printf("bStatus_PingState: %X\n", spOldDumyqTD->bStatus_PingState);
2662 printf("bStatus_SplitState: %X\n", spOldDumyqTD->bStatus_SplitState);
2663 printf("bStatus_MissMicroFrame: %X\n", spOldDumyqTD->bStatus_MissMicroFrame);
2664 printf("bStatus_Transaction_Err: %X\n", spOldDumyqTD->bStatus_Transaction_Err);
2665 printf("bStatus_Babble: %X\n", spOldDumyqTD->bStatus_Babble);
2666 printf("bStatus_Buffer_Err: %X\n", spOldDumyqTD->bStatus_Buffer_Err);
2667 printf("bStatus_Halted: %X\n", spOldDumyqTD->bStatus_Halted);
2668 printf("bStatus_Active: %X\n", spOldDumyqTD->bStatus_Active);
2669 printf("bPID: %X\n", spOldDumyqTD->bPID);
2670 printf("bErrorCounter: %X\n", spOldDumyqTD->bErrorCounter);
2671 printf("CurrentPage: %X\n", spOldDumyqTD->CurrentPage);
2672 printf("bInterruptOnComplete: %X\n", spOldDumyqTD->bInterruptOnComplete);
2673 printf("bTotalBytes: %X\n", spOldDumyqTD->bTotalBytes);
2674 printf("bDataToggle: %X\n", spOldDumyqTD->bDataToggle);
2675 printf("ArrayBufferPointer_Word: %X\n", spOldDumyqTD->ArrayBufferPointer_Word[0]);
2676 #endif
2677 //flib_Host20_StopRun_Setting(HOST20_Enable);
2678 //printf("RunStop: %X\n", mbHost20_USBCMD_RunStop_Rd());
2679 spOldDumyqTD->bStatus_Active=1;
2680 #if 1
2681 //20120912, temp patch for USB HW can't update currentQTD
2682 if ( (MDrv_USBGetChipID() == CHIPID_EDEN) )
2683 spTempqHD->bOverlay_CurrentqTD=VirtoPhyAddr((U32)spOldDumyqTD);
2684 #endif
2685 #if defined(CPU_TYPE_MIPS) || defined(CPU_TYPE_ARM)
2686 USB_Chip_Flush_Memory();
2687
2688
2689 #endif
2690
2691 while (mbHost20_USBCMD_AsynchronousEnable_Rd()==0)
2692 {
2693 mbHost20_USBCMD_AsynchronousEnable_Set();//re start asynchronous scheduler
2694 }
2695 //spTempqHD->bOverlay_Status&=~HOST20_qTD_STATUS_Halted;
2696
2697 //wait until asynchronous scheduler is idle
2698
2699 //mbHost20_USBCMD_RunStop_Set();
2700
2701 //spTempqHD->bOverlay_Status&=~0x40; //clr HALT bit, start this queue head
2702 //XBYTE[0x2410]|=1;//start run
2703 //wDummyTemp=0;
2704 wTimes=0;
2705 #ifndef ATV_SERISE_USE
2706 StartTime=MsOS_GetSystemTime();
2707 LastTime=StartTime;
2708 FrameIdx=mwHost20_FrameIndex_Rd();
2709 #endif
2710 //<5>.Waiting for result
2711 //EAL=0;
2712 while (1)
2713 {
2714 #ifndef ATV_SERISE_USE
2715 msAPI_Timer_ResetWDT();
2716 #endif
2717
2718 #ifdef ATV_SERISE_USE
2719 wTimes++;
2720 #endif
2721 //if ((spOldDumyqTD->bStatus_Active==0) &&
2722 // ( mwHost20_USBSTS_Rd()& HOST20_USBINTR_CompletionOfTransaction)) //wait until Status_Active become 0
2723 if(mwHost20_USBSTS_Rd()& HOST20_USBINTR_CompletionOfTransaction) //wait until Status_Active become 0
2724 {
2725
2726 #if defined(CPU_TYPE_MIPS) || defined(CPU_TYPE_ARM)
2727 USB_Chip_Read_Memory();
2728 #endif
2729 if (spOldDumyqTD->bStatus_Active==0)
2730 {
2731 bReturnValue=HOST20_OK;
2732 break;
2733 }
2734 //bExitLoop=1;
2735 //printf("bReturn:%02bx\n", bReturnValue);
2736 }
2737 //if (bExitLoop) break;
2738 #if 1
2739 if (mwHost20_USBSTS_Rd()&(HOST20_USBINTR_SystemError+HOST20_USBINTR_USBError))
2740 {
2741 bReturnValue=HOST20_OK;
2742 //if (spOldDumyqTD->bStatus_Active==1)
2743 //{
2744 // printf("something wrong..USBINTR:%02bx\n",mwHost20_USBSTS_Rd());
2745 // printf("QH status:%02bx\n",spTempqHD->bOverlay_Status);
2746 //}
2747 break; //USB interrupt happened
2748 }
2749 #endif
2750 #if 1
2751 //if (gwOTG_Timer_Counter>wTimeOutSec)
2752 // wTimes++;
2753 //if (wTimes > wTimeOutSec*400)
2754 if (mwHost20_PORTSC_ConnectStatus_Rd()==0)
2755 {
2756 //return HOST20_FATAL;
2757 bReturnValue = HOST20_FATAL;
2758 break;
2759 }
2760 //if (mwHost20_PORTSC_ConnectChange_Rd())
2761 //{
2762 // gDeviceFatalError=TRUE;
2763 // mwHost20_PORTSC_ConnectChange_Set();
2764 // return HOST20_FATAL; //usb has been plug out and in
2765 //}
2766 #ifndef ATV_SERISE_USE
2767 #if 1
2768 if (MsOS_Timer_DiffTimeFromNow(LastTime) >(U32)3) //over 1 mini sec
2769 {
2770
2771 if (mwHost20_FrameIndex_Rd()==FrameIdx)
2772 {
2773 HOSTLIB_MSG(printf("1C stop,timeout !!\n"));
2774 gUsbStatus=USB_TIMEOUT; //ESD , USB hang,should be timeout
2775 bReturnValue=HOST20_FATAL;
2776 break;
2777 }
2778 FrameIdx=mwHost20_FrameIndex_Rd();
2779 LastTime=MsOS_GetSystemTime();
2780
2781 }
2782 #endif
2783 if (MsOS_Timer_DiffTimeFromNow(StartTime) > (U32)wTimeOutSec *1000)
2784 #else
2785 if(wTimes>0x80000)
2786 #endif
2787 {
2788 #if 0
2789 printf("\nQH1:\n");
2790 Dump_Data(0xf800,0x30);
2791 printf("\nQH2:\n");
2792 Dump_Data(0xf840,0x30);
2793 printf("\nQH3:\n");
2794 Dump_Data(0xf880,0x30);
2795 printf("\nQH4:\n");
2796 Dump_Data(0xf8c0,0x30);
2797 printf("\n");
2798 Dump_Data(0xfa80,0x20);
2799 printf("\n");
2800 Dump_Data(0xfb00,0x20);
2801 printf("\n");
2802 Dump_Data(0xfae0,0x20);
2803 printf("\n");
2804 Dump_Data(0xfaa0,0x20);
2805 printf("\n");
2806 Dump_Data(0xfac0,0x20);
2807
2808 printf("\nQH->%x\n",(U16)spTempqHD);
2809 //Dump_Data((U16)spTempqHD,0x30);
2810
2811 printf("\nqtd->\n");
2812 Dump_Data((U16)spOldDumyqTD,0x20);
2813 printf("\nEHCI REG:\n");
2814 Dump_Data(0x2400,0x50);
2815 #endif
2816 bExitLoop=2;
2817 bReturnValue=HOST20_FATAL;
2818 gUsbStatus=USB_TIMEOUT;
2819 //gDeviceFatalError=TRUE;
2820
2821 //MINI_DEBUG2(printf("Time Out:%02bx\n",XBYTE[gUHC_BASE+30]));
2822 break;
2823 }
2824
2825 #endif
2826 //}
2827 }
2828 #ifndef ATV_SERISE_USE
2829 msAPI_Timer_ResetWDT(); //in case spend too much time at polling
2830 #endif
2831 while (mbHost20_USBCMD_AsynchronousEnable_Rd())
2832 {
2833 mbHost20_USBCMD_AsynchronousEnable_Clr();//pause asynchronous scheduler
2834 }
2835
2836 //<6>.Checking the Result
2837 if (bReturnValue!=HOST20_FATAL)
2838 bReturnValue=flib_Host20_CheckingForResult_QHD(spTempqHD);
2839 if (bReturnValue==HOST20_TRANSACTION_ERROR)
2840 {
2841 //printf("Transaction Error\n");
2842
2843 spTempqHD->bOverlay_Status=0; //clear halt status
2844 //SetPointer(&(spTempqHD->bOverlay_NextqTD3),0);
2845 //SetPointer(&(spTempqHD->bOverlay_AlternateqTD3),0);
2846 // SetPointer(&(spTempqHD->bOverlay_CurqTD3),VirtoPhyAddr((UINT16)spNewDumyqTD));
2847 spTempqHD->bOverlay_CurrentqTD=(VirtoPhyAddr((U32)spNewDumyqTD)>>5);
2848 //printf("TotalBytes:%x\n",TotalBytes);
2849
2850 //printf("spNewDumyqTD->%x\n",(U16)spNewDumyqTD);
2851 //printf("spOldDumyqTD->%x\n",(U16)spOldDumyqTD);
2852 //Dump_Data((U16)spOldDumyqTD,0x20);
2853 //printf("\nQH->%x\n",(U16)spTempqHD);
2854 //Dump_Data((U16)spTempqHD,0x20);
2855
2856 }
2857 TotalBytes= spOldDumyqTD->bTotalBytes;
2858
2859 //<5>.Release the all the qTD (Not include spNewDumyqTD)
2860 #if 1
2861 spReleaseqTD=spOldDumyqTD;
2862 do {
2863 //spReleaseqTDNext=((UINT32)(spReleaseqTD->bNextQTDPointerL))<<5 + ((UINT32)(spReleaseqTD->bNextQTDPointerH))<<16 ;
2864 spReleaseqTDNext=(qTD_Structure*)PhytoVirAddr(((U32)(spReleaseqTD->bNextQTDPointer))<<5);
2865
2866 DataToggleBackup=spOldDumyqTD->bDataToggle;
2867
2868 flib_Host20_ReleaseStructure(Host20_MEM_TYPE_qTD,(U32)spReleaseqTD);
2869
2870 spReleaseqTD=spReleaseqTDNext;
2871 } while(((UINT32)spReleaseqTD)!=((UINT32)spNewDumyqTD));
2872 #endif
2873
2874 // Double Check the QH overlay status. Adjust it if need.
2875 if( spTempqHD->bOverlay_NextqTD != (VirtoPhyAddr(( U32)spNewDumyqTD)>>5)
2876 || spTempqHD->bOverlay_Status & BIT7 )
2877 {
2878 // If chain qTDs after disconnection/qTD timeout, QH overlay will not be advanced by HW.
2879 // It muss up qTD chain layout. QH doesn't stop at old dumy and stare at new dumy.
2880 // SW advance QH overlay manually no matter HW advancing or not.
2881 // Run bit is cleared by HQ when disconnection, so it is safe to modify the QH.
2882
2883 HOSTLIB_MSG(printf("[Warning] Adjust bad qTD chain..\r\n"));
2884
2885 spTempqHD->bOverlay_Status = spTempqHD->bOverlay_Status & ~BIT7;
2886 spTempqHD->bOverlay_CurrentqTD = (VirtoPhyAddr(( U32)spOldDumyqTD)>>5);
2887 spTempqHD->bOverlay_NextqTD = (VirtoPhyAddr(( U32)spNewDumyqTD)>>5);
2888 spTempqHD->bOverlay_AlternateqTD = (VirtoPhyAddr(( U32)spNewDumyqTD)>>5);
2889 }
2890
2891 return (bReturnValue);
2892 }
2893 #endif
2894
2895 #ifdef ENABLE_HOST_TEST
2896 extern void MDrv_UsbSendSE0(void);
2897 extern void MDrv_UsbSendTestJ(void);
2898 extern void MDrv_UsbSendTestK(void);
2899 extern void MDrv_UsbSendTestPacket(void);
2900
flib_Host20_Issue_Control_Test_Mode(UINT8 bEdNum,UINT8 * pbCmd,UINT16 hwDataSize,UINT8 * pbData)2901 UINT8 flib_Host20_Issue_Control_Test_Mode (UINT8 bEdNum,UINT8* pbCmd,UINT16 hwDataSize,UINT8* pbData)
2902 {
2903 qTD_Structure *spTempqTD;
2904 //UINT16 bpDataPage;
2905 UINT8 bReturnValue;
2906 UINT8 ii;
2907 //UINT8 i;
2908 qHD_Structure xdata *qh_ptr;
2909
2910 if (bEdNum==0)
2911 qh_ptr=pHost20_qHD_List_Control0;
2912 else
2913 qh_ptr=pHost20_qHD_List_Control1;
2914
2915 //<0>.Allocate qTD & Data Buffer
2916 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
2917 //bpDataPage=sAttachDevice.bDataBuffer; //2//2k buffer
2918 //bpDataPage=sAttachDevice.bDataBuffer;
2919 //<2.1>.Setup packet
2920 //<A>.Fill qTD
2921 spTempqTD->bPID=HOST20_qTD_PID_SETUP; //Bit8~9
2922 spTempqTD->bTotalBytes=8; //Bit16~30
2923
2924 spTempqTD->bDataToggle=0; //Bit31
2925 //FillBufferArray(spTempqTD, bpDataPage);
2926 memcpy(pUsbCtrlBuf,pbCmd,8);
2927
2928 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
2929
2930 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
2931
2932 if (bReturnValue>0)
2933 return bReturnValue;
2934
2935 //MsOS_DelayTask(15000); //FOR TEST MODE
2936 for (ii=0; ii<150; ii++)
2937 {
2938 MsOS_DelayTask(100);
2939 msAPI_Timer_ResetWDT();
2940 }
2941 //<A>.Fill qTD
2942 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
2943 spTempqTD->bPID=HOST20_qTD_PID_IN; //Bit8~9
2944 spTempqTD->bTotalBytes=hwDataSize; //Bit16~30
2945
2946 spTempqTD->bDataToggle=1; //Bit31
2947
2948 spTempqTD->ArrayBufferPointer_Word[0]=VirtoPhyAddr((U32)pUsbCtrlBuf);
2949
2950 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
2951
2952 if (bReturnValue>0)
2953 return bReturnValue;
2954 //printf("c4");
2955
2956
2957 //<C>.Waiting for result
2958 memcpy(pbData,pUsbCtrlBuf,hwDataSize);
2959
2960 //Dump_Data((UINT16)pbData,hwDataSize);
2961
2962 //<2.3>.Out packet
2963
2964 //<A>.Fill qTD
2965 spTempqTD=flib_Host20_GetStructure(Host20_MEM_TYPE_qTD);//0=>qTD
2966 spTempqTD->bPID=HOST20_qTD_PID_OUT; //Bit8~9
2967 spTempqTD->bTotalBytes=0; //Bit16~30
2968
2969 spTempqTD->bDataToggle=1; //Bit31
2970
2971 //<B>.Send qTD
2972 //bReturnValue=flib_Host20_Send_qTD(spTempqTD ,psHost20_qHD_List_Control[bEdNum],5);
2973 bReturnValue=flib_Host20_Send_qTD(spTempqTD ,qh_ptr,gUsbTimeout);
2974
2975 return (bReturnValue);
2976 }
2977
GetDeviceDescriptor(void)2978 void GetDeviceDescriptor(void)
2979 {
2980 UINT8 bCMD[8];
2981
2982 memcpy(bCMD,OTGH_GETDESCRIPTOR_DEVICE_8,8);
2983
2984 flib_Host20_Issue_Control (1,bCMD,8,(UINT8*)&(sAttachDevice.sDD));
2985 }
2986
EnterTestMode(void)2987 void EnterTestMode(void)
2988 {
2989 UINT8 ii;
2990 UINT8 bCMD[8];
2991 #if 0
2992 if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x06))
2993 {
2994 //--- do suspend resume test
2995 // first, SOF for 15 seconds
2996 StartTime=MDrv_Timer_GetTime0();
2997
2998 printf("\r\n start sending SOF..",0);
2999 while (1)
3000 {
3001 msAPI_Timer_ResetWDT(); //in case watchdog timer is too short
3002
3003 if ((MDrv_Timer_GetTime0()-StartTime) > 1000) //SOF for 15 sec
3004 break;
3005
3006 }
3007 printf("\r\n suspend",0);
3008 mbHost20_USBCMD_RunStop_Clr();
3009 mwHost20_PORTSC_ForceSuspend_Set(); //suspend the device
3010 StartTime=MDrv_Timer_GetTime0();
3011
3012 while (1)
3013 {
3014 msAPI_Timer_ResetWDT(); //in case watchdog timer is too short
3015
3016 if ((MDrv_Timer_GetTime0()-StartTime) > 1000) //SOF for 15 sec
3017 break;
3018 }
3019 printf("\r\n resume\n",0);
3020 mwHost20_PORTSC_ForceResume_Set();
3021 mbHost20_USBCMD_RunStop_Set();
3022
3023 }
3024 else
3025 #endif
3026 if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x01))
3027 // test_SE0
3028 {
3029 MDrv_UsbSendSE0();
3030 }
3031 else if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x02))
3032 // test_J
3033 {
3034 MDrv_UsbSendTestJ();
3035 }
3036 else if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x03))
3037 // test_J
3038 {
3039 MDrv_UsbSendTestK();
3040 }
3041 else if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x04))
3042 // test_J
3043 {
3044 MDrv_UsbSendTestPacket();
3045 }
3046 else if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x07))
3047 //single step get device descriptor
3048 {
3049 printf("\r\n start sending SOF..\n");
3050
3051 for (ii=0; ii<150; ii++)
3052 {
3053 MsOS_DelayTask(100);
3054 msAPI_Timer_ResetWDT();
3055 }
3056
3057 printf("\r\n Get device descriptor.\n");
3058
3059 GetDeviceDescriptor();
3060 }
3061 else if ((sAttachDevice.sDD.bPIDHighByte==0x01)&&(sAttachDevice.sDD.bPIDLowByte==0x08))
3062 //single step get device descriptor
3063 {
3064 printf("\r\n Get device descriptor.\n");
3065 memcpy(bCMD,OTGH_GETDESCRIPTOR_DEVICE,8);
3066
3067 flib_Host20_Issue_Control_Test_Mode (1,bCMD,0x12,(UINT8*)&(sAttachDevice.sDD));
3068 }
3069
3070 }
3071
3072 #endif //ENABLE_HOST_TEST
3073
3074 #endif //#if defined(MSOS_TYPE_NOS)
3075
3076
3077