xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/usbhost/drvUSBEntry.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
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 ///////////////////////////////////////////////////////////////////////////////////////////////////
79 /// File name: usbentry.c
80 /// @brief USB host driver Entry function.
81 /// @author MStar Semiconductor Inc.
82 ///////////////////////////////////////////////////////////////////////////////////////////////////
83 
84 #include "include/drvConfig.h"
85 #include "include/drvPorts.h"
86 #include "include/drvKernel.h"
87 #include "include/drvTimer.h"
88 #include "include/drvUSBMsg.h"
89 
90 #include "drvHCD.h"
91 #include "drvMassStor.h"
92 #include "include/drvCPE_EHCI.h"
93 #include "MsCommon.h"
94 //#include "drvpower.h"
95 #include "drvUSB.h"
96 #include "include/drvIO.h"
97 #include "drvUSBHwCtl.h"
98 #include "include/drvCPE_AMBA.h"
99 #include "drvEHCI.h"
100 #include "drvUsbcommon.h"
101 #include "drvSCSI.h"
102 #if USB_HID_SUPPORT
103 #include "drvHIDDev.h"
104 #endif
105 #if USB_CDC_SUPPORT
106 #include "drvCDCDev.h"
107 #endif
108 
109 //#define HSTTASK_DEBUG
110 #ifdef HSTTASK_DEBUG
111 #define UDRV_Dbg		diag_printf
112 #else
113 #define UDRV_Dbg(x,...)
114 #endif
115 
116 #define UDRV_ERR		diag_printf
117 
118 
119 struct s_ChipUsbHostDef *pCurrentChip;
120 struct s_ChipUsbHostDef chipURANUS4;
121 struct s_ChipUsbHostDef chipKRONUS;
122 struct s_ChipUsbHostDef chipKAISERIN;
123 
124 mem_Alloc    pfnAllocCachedMem=NULL, pfnAllocNoncachedMem=NULL;
125 mem_Free     pfnFreeCachedMem=NULL, pfnFreeNoncachedMem=NULL;
126 mem_VA2PA    pfnVA2PA=NULL;
127 mem_PA2VA    pfnPA2VA=NULL;
128 mem_Cached2Noncached     pfnCached2Noncached=NULL;
129 mem_NonCached2Cached     pfnNoncached2Cached=NULL;
130 
131 
132 S32 _s32MutexUSB;
133 S32 _s32MutexUSB_Port2;
134 S32 _s32MutexUSB_Port1;
135 S32 _s32MutexUSB_Port3;
136 
137 MS_S32 _s32UsbEventId = -1;
138 MS_BOOL UsbReady = FALSE;
139 //MS_U16  gIntStatus;
140 MS_BOOL  isRootHubPortReset = TRUE;
141 BOOL  gIsBadDevice = FALSE;
142 MS_U8 gUsbChipID = 0xFF;
143 
144 
145 MS_U32 gBaseUTMI=0, gBaseUSBC=0, gBaseUHC=0;
146 MS_U32 gBaseUTMI2=0, gBaseUSBC2=0, gBaseUHC2=0;
147 MS_U32 gIrqUHC=0xFFFF, gIrqUHC2=0xFFFF;
148 
149 #define MAX_HCD_SUPPORT 2
150 MS_U8  gInitHcdNum=0, gHcdMap[MAX_HCD_SUPPORT]={0};
151 
152 USBCallback _DrvUSBC_CBFun = NULL;
153 
154 static void _DrvUSB_OnInterrupt(InterruptNum eIntNum);
155 void  UsbTask(MS_U32 argc, VOID *argv);
156 void USB_Start(void);
157 USBCallback _DrvUSB_CBFun = NULL;
158 
159 extern BOOL UsbReady;
160 extern struct usb_hcd *g_pUsbHcd;
161 extern struct cpe_dev cpe_ehci_dev;
162 
163 //HID device types
164 #if USB_HID_SUPPORT
165 extern S32 g_Hid_Timer_ID;
166 extern void usb_hid_parse_report(U8 *start, U16 size, struct button_struct *pButton,struct axis_struct *pAxis, struct key_struct *pKeys);
167 #endif
168 extern void init_OS_Port2_Resource(void);
169 extern void USB_Start_Port2(void);
170 void USBCriticalSectionIn(MS_U8 Port);
171 void USBCriticalSectionOut(MS_U8 Port);
172 extern void init_OS_Resource(void);
173 extern int __init ehci_cpe_dev_init(void);
174 extern int __init ehci_cpe_dev_init_Port2(void);
175 
176 
177 //#define ERROR_CONCEALMENT
178 #ifdef ERROR_CONCEALMENT
179 U32 u32UsbTaskTimer = 0;
180 #endif
181 
182 #ifndef  MS_NOSAPI
183 static MS_U8 u8HubStackBuffer[HUB_STACK_SIZE];
184 #endif
185 
186 //-------------------------------------------------------------------------------------------------
187 /// Register a callback function to process the usb plug event
188 /// @param  pCallbackFn \b IN: callback function used in interrupt context.
189 /// @return None
190 /// @note   call after USB has been initialized
191 ///         The last registered callback will overwrite the previous ones.
192 //-------------------------------------------------------------------------------------------------
MDrv_USB_RegisterCallBack(USBCallback pCallbackFn)193 void MDrv_USB_RegisterCallBack (USBCallback pCallbackFn)
194 {
195     //Register a callback function for application to process the data received
196     //MsOS_DisableInterrupt(E_IRQ_UHC);
197     _DrvUSB_CBFun = pCallbackFn;
198     //MsOS_EnableInterrupt(E_IRQ_UHC);
199 }
200 
MDrv_USBGetChipID(void)201 MS_U8 MDrv_USBGetChipID(void)
202 {
203     if (gUsbChipID == 0xFF)
204         gUsbChipID = usb_readb(OS_BASE_ADDR+0x1ecc*2);
205 
206 	//diag_printf("GetChipID: 0x%x\n", gUsbChipID);
207     return gUsbChipID;
208 }
209 
U4_series_usb_init(unsigned int UTMI_base,unsigned int USBC_base,unsigned int UHC_base,unsigned int flag)210 void U4_series_usb_init(unsigned int UTMI_base, unsigned int USBC_base, unsigned int UHC_base, unsigned int flag)
211 {
212     MS_U8 chipID;
213     MS_U16 chipVER = 0x100; // U01
214 
215 	diag_printf("usb_init start, UTMI base %p, USBC base %p, UHC base %p\n", UTMI_base, USBC_base, UHC_base);
216 
217     if (flag & EHCFLAG_TESTPKG)
218     {
219 	    usb_writew(0x2084, (void*) (UTMI_base+0x2*2)); //
220 	    usb_writew(0x0003, (void*) (UTMI_base+0x20*2)); //
221     }
222 
223 	usb_writeb(0x0a, (void*) (USBC_base)); // Disable MAC initial suspend, Reset UHC
224 	usb_writeb(0x28, (void*) (USBC_base)); // Release UHC reset, enable UHC and OTG XIU function
225 
226 	usb_writeb(usb_readb((void*)(UTMI_base+0x22*2)) | 0xE0, (void*) (UTMI_base+0x22*2)); // Set PLL_TEST[23:21] for enable 480MHz clock
227 	usb_writeb(usb_readb((void*)(UTMI_base+0x20*2)) | 0x02, (void*) (UTMI_base+0x20*2)); // Set PLL_TEST[1] for PLL multiplier 20X
228 
229     chipID = MDrv_USBGetChipID();
230     if ((chipID == CHIPID_KRONUS) || (chipID == CHIPID_KAISERIN))
231     {
232             usb_writeb(usb_readb((void*)(UTMI_base+0x0D*2-1)) | 0x01, (void*) (UTMI_base+0x0D*2-1)); // set reg_double_data_rate, To get better jitter performance
233 	    usb_writeb(0,    (void*) (UTMI_base+0x21*2-1));
234 	    usb_writeb(0x10, (void*) (UTMI_base+0x23*2-1));
235 	    usb_writeb(0x01, (void*) (UTMI_base+0x24*2));
236     }
237 
238     if (flag & EHCFLAG_DOUBLE_DATARATE)
239     {
240         if ((flag & EHCFLAG_DDR_MASK) == EHCFLAG_DDR_x15)
241         {
242 	        usb_writeb(usb_readb((void*)(UTMI_base+0x20*2)) | 0x76, (void*) (UTMI_base+0x20*2)); // Set usb bus = 480MHz x 1.5
243         }
244         else if ((flag & EHCFLAG_DDR_MASK) == EHCFLAG_DDR_x18)
245         {
246 	        usb_writeb(usb_readb((void*)(UTMI_base+0x20*2)) | 0x8e, (void*) (UTMI_base+0x20*2)); // Set usb bus = 480MHz x 1.8
247         }
248         //else if ((flag & EHCFLAG_DDR_MASK) == EHCFLAG_DDR_x20)
249         //{
250 	    //    usb_writeb(usb_readb((void*)(UTMI_base+0xD*2-1)) | 0x01, (void*) (UTMI_base+0xD*2-1)); // Set usb bus = 480MHz x2
251         //}
252 
253 	    usb_writeb(usb_readb((void*)(UTMI_base+0x2c*2)) |0x1, (void*) (UTMI_base+0x2c*2));  //Set slew rate control for overspeed (or 960MHz)
254     }
255 
256 	usb_writeb(usb_readb((void*)(UTMI_base+0x09*2-1)) & ~0x08, (void*) (UTMI_base+0x09*2-1)); // Disable force_pll_on
257 	usb_writeb(usb_readb((void*)(UTMI_base+0x08*2)) & ~0x80, (void*) (UTMI_base+0x08*2)); // Enable band-gap current
258 	usb_writeb(0xC3, (void*)(UTMI_base)); // reg_pdn: bit<15>, bit <2> ref_pdn
259 	mdelay(1);	// delay 1ms
260 
261 	usb_writeb(0x69, (void*) (UTMI_base+0x01*2-1)); // Turn on UPLL, reg_pdn: bit<9>
262 	mdelay(2);	// delay 2ms
263 
264 	usb_writeb(0x01, (void*) (UTMI_base)); // Turn all (including hs_current) use override mode
265 	usb_writeb(0, (void*) (UTMI_base+0x01*2-1)); // Turn on UPLL, reg_pdn: bit<9>
266 
267 	usb_writeb(0x0A, (void*) (USBC_base)); // Disable MAC initial suspend, Reset UHC
268 	usb_writeb(0x28, (void*) (USBC_base)); // Release UHC reset, enable UHC XIU function
269 
270 	usb_writeb(usb_readb((void*)(UTMI_base+0x3C*2)) | 0x01, (void*) (UTMI_base+0x3C*2)); // set CA_START as 1
271 	mdelay(10);
272 
273 	usb_writeb(usb_readb((void*)(UTMI_base+0x3C*2)) & ~0x01, (void*) (UTMI_base+0x3C*2)); // release CA_START
274 
275 	while ((usb_readb((void*)(UTMI_base+0x3C*2)) & 0x02) == 0);	// polling bit <1> (CA_END)
276 
277     if (flag & EHCFLAG_DPDM_SWAP)
278     	usb_writeb(usb_readb((void*)(UTMI_base+0x0b*2-1)) |0x20, (void*) (UTMI_base+0x0b*2-1)); // dp dm swap
279 
280 	usb_writeb(usb_readb((void*)(USBC_base+0x02*2)) & ~0x03, (void*) (USBC_base+0x02*2)); //UHC select enable
281 	usb_writeb(usb_readb((void*)(USBC_base+0x02*2)) | 0x01, (void*) (USBC_base+0x02*2)); //UHC select enable
282 
283 	usb_writeb(usb_readb((void*)(UHC_base+0x40*2)) & ~0x10, (void*) (UHC_base+0x40*2)); //0: VBUS On.
284 	mdelay(1);	// delay 1ms
285 
286 	usb_writeb(usb_readb((void*)(UHC_base+0x40*2)) | 0x08, (void*) (UHC_base+0x40*2)); // Active HIGH
287 	mdelay(1);	// delay 1ms
288 
289 	usb_writeb(usb_readb((void*)(UHC_base+0x81*2-1)) | 0x8F, (void*) (UHC_base+0x81*2-1)); //improve the efficiency of USB access MIU when system is busy
290 
291     if ((chipID == CHIPID_KRONUS) || (chipID == CHIPID_URANUS4))
292 	usb_writeb(usb_readb((void*)(UHC_base+0x83*2-1)) | 0x40, (void*) (UHC_base+0x83*2-1)); //set MIU1_sel to 128MB
293     // Kaiserin will keep the defaut "00" to be as 512MB support
294 
295 	usb_writeb((usb_readb((void*)(UTMI_base+0x06*2)) & 0x9F) | 0x40, (void*) (UTMI_base+0x06*2)); //reg_tx_force_hs_current_enable
296 
297 	usb_writeb(usb_readb((void*)(UTMI_base+0x03*2-1)) | 0x28, (void*) (UTMI_base+0x03*2-1)); //Disconnect window select
298 	usb_writeb(usb_readb((void*)(UTMI_base+0x03*2-1)) & 0xef, (void*) (UTMI_base+0x03*2-1)); //Disconnect window select
299 
300 	usb_writeb(usb_readb((void*)(UTMI_base+0x07*2-1)) & 0xfd, (void*) (UTMI_base+0x07*2-1)); //Disable improved CDR
301 	usb_writeb(usb_readb((void*)(UTMI_base+0x09*2-1)) |0x81, (void*) (UTMI_base+0x09*2-1)); // UTMI RX anti-dead-loc, ISI effect improvement
302 	if ((flag & EHCFLAG_DOUBLE_DATARATE)==0)
303 	    usb_writeb(usb_readb((void*)(UTMI_base+0x0b*2-1)) |0x80, (void*) (UTMI_base+0x0b*2-1)); // TX timing select latch path
304 	usb_writeb(usb_readb((void*)(UTMI_base+0x15*2-1)) |0x20, (void*) (UTMI_base+0x15*2-1)); // Chirp signal source select
305 
306 	usb_writeb(usb_readb((void*)(UTMI_base+0x2c*2)) |0x10, (void*) (UTMI_base+0x2c*2));
307 	usb_writeb(usb_readb((void*)(UTMI_base+0x2d*2-1)) |0x02, (void*) (UTMI_base+0x2d*2-1));
308 	usb_writeb(usb_readb((void*)(UTMI_base+0x2f*2-1)) |0x81, (void*) (UTMI_base+0x2f*2-1));
309 
310     // Kaiserin U02 patch code
311     if (chipID == CHIPID_KAISERIN)
312         chipVER = usb_readw((void *)(KAISERIN_CHIP_TOP_BASE+0xCE*2));
313     if ((chipID == CHIPID_KAISERIN) && (chipVER == 0x101)) // && U02
314     {
315         diag_printf("K2S software patch!!!\n");
316 
317         // enable LS cross point ECO
318         usb_writeb(usb_readb((void*)(UTMI_base+0x39*2-1)) | 0x04, (void*) (UTMI_base+0x39*2-1));  //enable deglitch SE0��(low-speed cross point)
319 
320         // enable power noise ECO
321         usb_writeb(usb_readb((void*)(USBC_base+0x02*2)) | 0x40, (void*) (USBC_base+0x02*2)); //enable use eof2 to reset state machine�� (power noise)
322 
323         // enable TX/RX reset clock gating ECO
324         usb_writeb(usb_readb((void*)(UTMI_base+0x39*2-1)) | 0x02, (void*) (UTMI_base+0x39*2-1)); //enable hw auto deassert sw reset(tx/rx reset)
325 
326         // enable loss short packet interrupt ECO, default 0 on
327         //usb_writeb(usb_readb((void*)(USBC_base+0x04*2)) & 0x7f, (void*) (USBC_base+0x04*2)); //enable patch for the assertion of interrupt(Lose short packet interrupt)
328 
329         // enable babble ECO
330         usb_writeb(usb_readb((void*)(USBC_base+0x04*2)) | 0x40, (void*) (USBC_base+0x04*2)); //enable add patch to Period_EOF1(babble problem)
331 
332         // enable MDATA single TT ECO
333         writeb(readb((void*)(USBC_base+0x0A*2)) | 0x40, (void*) (USBC_base+0x0A*2)); //enable short packet MDATA in Split transaction clears ACT bit (LS dev under a HS hub)
334     }
335 
336     if (flag & EHCFLAG_TESTPKG)
337     {
338 	    usb_writew(0x0210, (void*) (UTMI_base+0x2C*2)); //
339 	    usb_writew(0x8100, (void*) (UTMI_base+0x2E*2)); //
340 
341 	    usb_writew(0x0600, (void*) (UTMI_base+0x14*2)); //
342 	    usb_writew(0x0038, (void*) (UTMI_base+0x10*2)); //
343 	    usb_writew(0x0BFE, (void*) (UTMI_base+0x32*2)); //
344     }
345 
346 }
347 
MDrv_Usb_Init(mem_Alloc pfn_Cachedmem_Alloc,mem_Free pfn_Cachedmem_Free,mem_Alloc pfn_NonCachedmem_Alloc,mem_Free pfn_NonCachedmem_Free,mem_VA2PA pfn_mem_VA2PA,mem_PA2VA pfn_mem_PA2VA,mem_Cached2Noncached pfn_mem_Cached2Noncached,mem_NonCached2Cached pfn_mem_NonCached2Cached)348 void MDrv_Usb_Init(
349     mem_Alloc     pfn_Cachedmem_Alloc,
350     mem_Free      pfn_Cachedmem_Free,
351     mem_Alloc     pfn_NonCachedmem_Alloc,
352     mem_Free      pfn_NonCachedmem_Free,
353     mem_VA2PA     pfn_mem_VA2PA,
354     mem_PA2VA     pfn_mem_PA2VA,
355     mem_Cached2Noncached pfn_mem_Cached2Noncached,
356     mem_NonCached2Cached pfn_mem_NonCached2Cached
357 )
358 {
359     MS_U8 cid;
360     MS_U16 chipVER = 0x100; // U01
361     diag_printf("Usb init\n");
362 
363     pfnAllocCachedMem = pfn_Cachedmem_Alloc;
364     pfnFreeCachedMem = pfn_Cachedmem_Free;
365     pfnAllocNoncachedMem = pfn_NonCachedmem_Alloc;
366     pfnFreeNoncachedMem = pfn_NonCachedmem_Free;
367     pfnVA2PA = pfn_mem_VA2PA;
368     pfnPA2VA = pfn_mem_PA2VA;
369     pfnCached2Noncached = pfn_mem_Cached2Noncached;
370     pfnNoncached2Cached = pfn_mem_NonCached2Cached;
371 
372     init_sys();
373 
374 #ifndef  MS_NOSAPI
375     ASSERT(-1 == _s32UsbEventId);
376     _s32UsbEventId = MsOS_CreateEventGroup("UHC_Event");
377 #endif
378 
379     usb_stor_init();
380 
381 #if USB_HID_SUPPORT
382     usb_hid_init();
383 #endif
384 
385 #if USB_CDC_SUPPORT
386     usb_cdc_init();
387 #endif
388 
389     // Decide the current chip
390     cid = MDrv_USBGetChipID();
391     switch (cid)
392     {
393         case CHIPID_URANUS4:
394             pCurrentChip = &chipURANUS4;
395             break;
396         case CHIPID_KRONUS:
397             pCurrentChip = &chipKRONUS;
398             break;
399         case CHIPID_KAISERIN:
400             pCurrentChip = &chipKAISERIN;
401             diag_printf("Chip Kaiserin!\n");
402             chipVER = usb_readw((void *)(KAISERIN_CHIP_TOP_BASE+0xCE*2));
403             if (chipVER == 0x101) // U02
404             {
405                 // chip top performance tuning [11:9]
406                 usb_writew(usb_readw((void*)(KAISERIN_CHIP_TOP_BASE+0x46*2)) | 0xe00, (void*) (KAISERIN_CHIP_TOP_BASE+0x46*2));
407             }
408             break;
409         default:
410             pCurrentChip = NULL;
411             diag_printf("Not allowed chip ID\n");
412     }
413     pCurrentChip->chipID = cid;
414 }
415 
416 
417 extern MS_BOOL MDrv_USB_Port_Init_EX(MS_U8 u8PortNum);
MDrv_USB_Port_Init(MS_U8 u8PortNum)418 MS_BOOL MDrv_USB_Port_Init(MS_U8 u8PortNum)
419 {
420         unsigned int dwFlag=0;
421 	MS_U8 i=0;
422         MS_U8 cid;
423 	MS_U32 *pUTMI, *pUSBC, *pUHC, *pIrq;
424 
425         cid = MDrv_USBGetChipID();
426 	if (cid== CHIPID_KAISERIN)
427 	{
428 	        return( MDrv_USB_Port_Init_EX(u8PortNum));
429 	}
430 
431 	if (gInitHcdNum >= MAX_HCD_SUPPORT)
432 	{
433 		diag_printf(" Assert !! U4_series_usb_init: Exceed max USB number support !!\n");
434 		while(1);
435 	}
436 
437 	if (gInitHcdNum) {
438 		for (i=0; i<gInitHcdNum; i++) {
439 			if (u8PortNum == gHcdMap[i])
440 			{
441 				diag_printf(" Assert !! U4_series_usb_init: Initialize the same port duplicately !!\n");
442 				while(1);
443 			}
444 		}
445 	}
446 
447 	gHcdMap[gInitHcdNum] = u8PortNum;
448 	gInitHcdNum++;
449 
450 	switch (gInitHcdNum)
451 	{
452 		case 1:
453 				pUTMI = &gBaseUTMI;
454 				pUSBC = &gBaseUSBC;
455 				pUHC  = &gBaseUHC;
456 				pIrq  = &gIrqUHC;
457 				break;
458 		case 2:
459 				pUTMI = &gBaseUTMI2;
460 				pUSBC = &gBaseUSBC2;
461 				pUHC  = &gBaseUHC2;
462 				pIrq  = &gIrqUHC2;
463 				break;
464 		default:
465 			diag_printf(" Assert !! U4_series_usb_init: Not handled code !!\n");
466 			while(1);
467 			break;
468 	}
469 
470 
471     if (cid== CHIPID_KRONUS) //K1 only!!
472     {
473         dwFlag |= EHCFLAG_DPDM_SWAP;
474     }
475 
476     switch(u8PortNum)
477     {
478         case 0:
479 			*pUTMI = BASE_UTMI0;
480 			*pUSBC = BASE_USBC0;
481 			*pUHC  = BASE_UHC0;
482 			*pIrq  = E_IRQ_UHC;
483             break;
484 
485         case 1:
486             if (cid== CHIPID_KRONUS)
487             {
488 				*pUTMI = BASE_UTMI2;
489 				*pUSBC = BASE_USBC2;
490 				*pUHC  = BASE_UHC2;
491 				*pIrq  = E_IRQ_UHC2;
492 			}
493 			else
494 			{   //U4
495 				*pUTMI = BASE_UTMI1;
496 				*pUSBC = BASE_USBC1;
497 				*pUHC  = BASE_UHC1;
498 				*pIrq  = E_IRQ_UHC1;
499             }
500 			break;
501 
502         case 2:
503             if (cid!= CHIPID_URANUS4) //U4 only!!
504         	{
505 				diag_printf(" Assert !! U4_series_usb_init: No this port number in the chip !!\n");
506 				while(1);
507         	}
508             if (cid== CHIPID_URANUS4) //U4 only!!
509             {
510                 dwFlag |= EHCFLAG_DPDM_SWAP;
511             }
512 
513 			*pUTMI = BASE_UTMI2;
514 			*pUSBC = BASE_USBC2;
515 			*pUHC  = BASE_UHC2;
516 			*pIrq  = E_IRQ_UHC2;
517             break;
518 
519         default:
520 			diag_printf(" Assert !! U4_series_usb_init: No this port number in the chip !!\n");
521 			while(1);
522             return FALSE;
523             }
524 
525 
526 	switch (gInitHcdNum)
527 	{
528 		case 1:
529             U4_series_usb_init(gBaseUTMI, gBaseUSBC, gBaseUHC, dwFlag);
530             init_OS_Resource();
531             ehci_cpe_dev_init();
532             USB_Start();
533 			break;
534 		case 2:
535             U4_series_usb_init(gBaseUTMI2, gBaseUSBC2, gBaseUHC2, dwFlag);
536             init_OS_Port2_Resource();
537             ehci_cpe_dev_init_Port2();
538             USB_Start_Port2();
539             break;
540         default:
541 			diag_printf(" Assert !! U4_series_usb_init: Not handled code !!\n");
542 			while(1);
543 			break;
544     }
545 
546     return TRUE;
547 }
548 
USB_Start(void)549 void USB_Start(void)
550 {
551 #ifndef  MS_NOSAPI
552     MS_U8 *HubStack;
553     int pid;
554 #endif
555 
556     diag_printf("Usb start..\n");
557 
558 #ifndef  MS_NOSAPI
559     HubStack = u8HubStackBuffer;
560     _s32MutexUSB = MsOS_CreateMutex(E_MSOS_FIFO, "USB_MUTEX", MSOS_PROCESS_SHARED);
561 
562     if (_s32MutexUSB < 0)
563     {
564         GEN_EXCEP;
565         return;
566     }
567 #endif //MS_NOSAPI
568 
569 #ifndef MS_NOSAPI
570     //Create Task
571     pid = MsOS_CreateTask((TaskEntry) UsbTask,
572                          (MS_U32)NULL,
573                          E_TASK_PRI_HIGH,
574                          TRUE,
575                          HubStack,
576                          HUB_STACK_SIZE,
577                          "USB Hub Task");
578     if (pid < 0)
579     {
580         GEN_EXCEP;
581         //MsOS_FreeMemory(HubStack, gs32CachedPoolID);
582         MsOS_DeleteMutex(_s32MutexUSB);
583 
584         //MsOS_DeleteMutex(_s32MutexUSB_Port2);
585 
586         return;
587     }
588 #else
589     //UsbTask();
590 #endif //MS_NOSAPI
591 
592     return;
593 }
594 
595 //void ResetUsbHardware(void) moved to drvUSBHwCtl.c
596 extern void ResetMstarUsb(struct ehci_hcd *ehci);
597 
598 MS_BOOL MDrv_UsbDeviceConnect_EX(struct usb_hcd *hcd);
MDrv_UsbDeviceConnect(struct usb_hcd * hcd)599 MS_BOOL MDrv_UsbDeviceConnect(struct usb_hcd *hcd)
600 {
601 #if 1 // _EX
602     if (MDrv_UsbDeviceConnect_EX(hcd))
603         return TRUE;
604     else
605         return(UsbReady = FALSE);
606 #else
607     struct ehci_hcd *ehci = hcd_to_ehci(hcd);
608     MS_U8 temp_s;
609     MS_U32 temp;
610     static MS_U32  UsbStartTime=0;
611 
612     temp_s = ehci_readb((U32)&ehci->regs->port_status[0]);
613     //temp=mwHost20_PORTSC_ConnectStatus_Rd();
614     //diag_printf("ConnectStatus:%02bx\n",temp);
615     //MsOS_DelayTask(10);
616     if (temp_s & PORT_CONNECT)
617     {
618         // diag_printf("TEMP:%02bx\n",temp);
619         return TRUE;
620     }
621     else
622     {
623 #ifndef MS_NOSAPI
624         if (MsOS_GetSystemTime()-UsbStartTime > 1000 )
625         {
626             UsbStartTime=MsOS_GetSystemTime();
627 
628             temp = ehci_readl((U32)&ehci->regs->bus_control);
629             temp &= ~0x80;
630             ehci_writel(temp, (U32)&ehci->regs->bus_control);
631             //UHC_ANDXBYTE(0x40, 0x7F);//disable force enter FSmode
632             ResetMstarUsb(ehci);
633         }
634 #endif
635         hcd->isRootHubPortReset = TRUE;
636         UsbReady=FALSE;
637         hcd->isBadDevice = FALSE;
638         return FALSE;
639     }
640 #endif
641 }
642 
MDrv_UsbClose(void)643 void MDrv_UsbClose(void)
644 {
645 #ifndef  MS_NOSAPI
646 //    MsOS_DisableInterrupt(E_IRQ_UHC);
647 //    MsOS_DetachInterrupt(E_IRQ_UHC);
648 //    MsOS_DeleteEventGroup(_s32UsbEventId);
649 //    _s32UsbEventId = -1;
650 ///    Host20_ReleaseBuffer();
651 #endif
652 ///	flib_Host20_Close(0);
653 #ifndef MS_NOSAPI
654 #if 0 ///
655     if(usb_temp_buf)
656     {
657         MsOS_FreeMemory((void*)usb_temp_buf,gs32NonCachedPoolID_MIU0);
658         usb_temp_buf = 0;
659     }
660 #endif
661 #endif
662 }
663 
664 /*
665     <1>.Disable RunStop
666     <1.1>. Chirp hardware patch 1
667     <2> .Write PortReset=1
668     <3>.Wait time=>50ms
669     <3.1>. Chirp hardware patch 2
670     <3.2>. Wait time=>20ms
671     <4>.Write PortReset=0
672     <4.1>. Chirp hardware patch 3
673     <5>.Waiting for PortReset==0
674     <6>.Enable RunStop Bit
675     <6.1>.reset UTMI
676     <6.2> Write RunStop Bit=1
677     <6.3>.Wait time 5ms, wait some slow device to be ready
678     <7>.Detect Speed
679 */
680 #define MSTAR_CHIRP_PATCH 1
681 #define MSTAR_EHC_RTERM_PATCH 1
682 
PortReset(unsigned int regUTMI,unsigned int regUHC)683 int PortReset(unsigned int regUTMI, unsigned int regUHC)
684 {
685     MS_U32 wTmp;
686 
687     diag_printf("PortReset: UTMI 0x%x, UHC 0x%x\n", regUTMI, regUHC);
688 
689 	writeb(readb((void*)(regUHC+0x10*2)) | 0x1, (void*)(regUHC+0x10*2)); //Set UHC run
690 
691 #if MSTAR_CHIRP_PATCH
692 	writeb(0x10, (void*)(regUTMI+0x2C*2));
693 	writeb(0x00, (void*)(regUTMI+0x2D*2-1));
694 	writeb(0x00, (void*)(regUTMI+0x2F*2-1));
695 	writeb(0x80 ,(void*)(regUTMI+0x2A*2));
696 	//writeb(0x20 ,(void*)(regUTMI+0x2A*2));
697 #endif
698 
699     #if (MSTAR_EHC_RTERM_PATCH)
700 	writeb(readb((void*)(regUTMI+0x13*2-1))|0x70, (void*)(regUTMI+0x13*2-1));
701     #endif
702 
703         // Write PortReset bit
704 	writeb(readb((void*)(regUHC+0x31*2-1)) | 0x01,(void*)(regUHC+0x31*2-1));
705 	mdelay(50);  //shorten the reset period for Transcend USB3.0 HDD
706 #if MSTAR_CHIRP_PATCH
707 	writeb(0x0 ,(void*)(regUTMI+0x2A*2));
708 #endif
709 	mdelay(20);
710 
711         // Clear PortReset bit
712 	writeb(readb((void*)(regUHC+0x31*2-1)) & 0xfe,(void*)(regUHC+0x31*2-1));
713 
714     wTmp=0;
715 
716     while (1)
717     {
718          if ((readb((void*)(regUHC+0x31*2-1)) & 0x1)==0)  break;
719 
720          wTmp++;
721          //udelay(1);
722          if (wTmp>20000)
723   	     {
724                diag_printf("??? Error waiting for Bus Reset Fail...==> Reset HW Control\n");
725                //mbHost20_USBCMD_HCReset_Set();
726                writeb(readb((void*)(regUHC+0x10*2)) | 0x2,(void*)(regUHC+0x10*2));
727 
728                //while(mbHost20_USBCMD_HCReset_Rd()==1);
729                while(readb((void*)(regUHC+0x10*2)) && 0x2);
730                return (1);
731          }
732     }
733 
734 
735 #if (MSTAR_CHIRP_PATCH)
736 	writeb(readb((void*)(regUTMI+0x2c*2)) |0x10, (void*) (regUTMI+0x2c*2));
737 	writeb(readb((void*)(regUTMI+0x2d*2-1)) |0x02, (void*) (regUTMI+0x2d*2-1));
738 	writeb(readb((void*)(regUTMI+0x2f*2-1)) |0x81, (void*) (regUTMI+0x2f*2-1));
739 #endif
740 
741     #if (MSTAR_EHC_RTERM_PATCH)
742 	writeb(readb((void*)(regUTMI+0x13*2-1))&0x8F, (void*)(regUTMI+0x13*2-1));
743     #endif
744 
745     writeb(readb((void*)(regUTMI+0x06*2))|0x03, (void*)(regUTMI+0x06*2)); //reset UTMI
746     writeb(readb((void*)(regUTMI+0x06*2))&(~0x03), (void*)(regUTMI+0x06*2)); //reset UTMI
747 
748     writeb(readb((void*)(regUHC+0x10*2)) | 0x1, (void*)(regUHC+0x10*2)); //Set UHC run
749 
750     mdelay(5);		//wait some slow device to be ready
751 
752     return (0);
753 }
754 
755 #if USB_HID_SUPPORT
756 extern int usb_hid_get_int_ex (U8 uPort, U8 *Buf, U8 size);
757 extern int usb_hid_get_int_ex_MultipleIntf (U8 uPort, U8 uIntfNum, U8 *Buf, U8 size);
758 #endif
759 
760 //BOOL  gUsbInit;
761 #if USB_HID_SUPPORT
762 extern U8 Hid_Report_Desc[0x100];
763 extern U8 Hid_Data[10];
764 extern struct hid_data *HID_us[MAX_HID_DEV_COUNT][MAX_HID_INTF_COUNT];
765 extern MS_U32  Invalid_HidDevs;
766 BOOL  gScanHIDDevices;
767 #endif
768 extern BOOL usb_get_connected_dev_state(int *pdevstate, unsigned char *pDevClass, struct usb_hcd *hcd);
UsbTask(MS_U32 argc,VOID * argv)769 void  UsbTask(MS_U32 argc, VOID *argv)
770 {
771     MS_U16    PortStatus;
772     MS_BOOL   isExtHubConnect, isFirstEvent, isConnect;
773     int       DevState;
774     MS_U8     DevClass, ii, kk;
775     MS_U8     LunSts[MAX_USB_STOR_LUNS];
776     MS_U8     LunPreSts[MAX_USB_STOR_LUNS];
777     MS_BOOL   isMSCPlugIn;
778     MS_U8     us_id;
779     //struct LUN_Device* LunDevice;
780     struct ehci_hcd *ehci = hcd_to_ehci(g_pUsbHcd);
781 
782     #if (USB_HID_SUPPORT)
783     MS_U8    jj;
784     #endif
785 
786     #if USB_HID_SUPPORT
787     MS_BOOL   isHIDPlugIn, isHIDPlugIntoSamePort;
788     MS_U8     isHIDPlugIn_IntfCnt;
789     #endif
790     #if USB_CDC_SUPPORT
791 	MS_BOOL   isCDCPlugIn;
792     #endif
793 
794     diag_printf("UsbTask..\n");
795     while (1)
796     {
797         //UsbReady=FALSE;   //move to MDrv_UsbDeviceConnect
798         isExtHubConnect = FALSE;
799         isMSCPlugIn = FALSE;
800 #if USB_HID_SUPPORT
801         isHIDPlugIn = FALSE;
802         isHIDPlugIn_IntfCnt = 0;
803 #endif
804 #if USB_CDC_SUPPORT
805 		isCDCPlugIn = FALSE;
806 #endif
807         // Waiting for USB port connection
808         while(1)
809         {
810             USBCriticalSectionIn(0);
811             isConnect = MDrv_UsbDeviceConnect(g_pUsbHcd);
812             USBCriticalSectionOut(0);
813 
814             #if USBC_IP_SUPPORT // USBC IP control
815             if ((pCurrentChip->usbc_ip[0].portNum == 0) && (pCurrentChip->usbc_ip[0].eventFlag))
816                         if (_DrvUSBC_CBFun)
817                         {
818                             if (pCurrentChip->usbc_ip[0].eventType)
819                                 _DrvUSBC_CBFun(USBC_NON_OVER_CURRENT, 0, NULL);
820                             else
821                                 _DrvUSBC_CBFun(USBC_OVER_CURRENT, 0, NULL);
822                             pCurrentChip->usbc_ip[0].eventFlag = 0;
823                         }
824             #endif
825             if(isConnect)
826                 break;
827 
828             MsOS_DelayTask(100);
829         }
830 
831         // removing any delay before USB bus reset
832         //MsOS_DelayTask(1000);
833         //diag_printf("USB is connect\n");
834 
835         while (1)
836         {
837             if (g_pUsbHcd->isBadDevice)
838             {
839                 diag_printf("A bad device found, cnt = %d\n", g_pUsbHcd->badDeviceCnt);
840                 break;
841             }
842 
843             kk = 0;
844             while(kk<HUB_DEBOUNCE_STABLE) // 100, debouncing?
845             {
846                 USBCriticalSectionIn(0);
847                 isConnect = MDrv_UsbDeviceConnect(g_pUsbHcd);
848                 USBCriticalSectionOut(0);
849                 if ( !isConnect )
850                 {
851                     MsOS_DelayTask(600);  //By Jonas! for hub event
852                     diag_printf("USB port disconnect\n");
853                     //if (isExtHubConnect)
854                         hub_poll(); //for disconnect hub event
855                     goto PORT_DISCONNECT;
856                 }
857                 kk++;
858                 MsOS_DelayTask(HUB_DEBOUNCE_STEP); // mimic Linux behavior
859             }
860 #if 0
861             if (!MDrv_UsbDeviceConnect())
862             {
863                 hub_poll(); //for disconnect hub event
864                 break;
865             }
866 #endif
867             // Device is connecting to the port
868             isFirstEvent = FALSE;
869             if (has_hub_events(&PortStatus))
870             {
871                 //diag_printf("hub event\n");
872                 isFirstEvent = TRUE;
873                 hub_poll();
874 
875 #if 0
876                 if ( PortStatus & USB_PORT_STAT_CONNECTION )
877                 {
878                     usb_get_connected_dev_state(&DevState, &DevClass);
879 
880                     if (DevClass == 9)
881                     {
882                         diag_printf("External Hub is connected\n");
883                         isExtHubConnect = TRUE;
884                     }
885                 }
886 #endif
887             }
888 
889             if (isExtHubConnect)
890                 hub_poll();
891 
892             isMSCPlugIn = FALSE;
893             for (us_id=0; us_id<MAX_USTOR; us_id++)
894             {
895                 if (Is_Stor_Dev_Info_Valid(us_id))   // Check if we found a Mass Stoarge device.
896                 {
897                     isMSCPlugIn = TRUE;
898                     if (!Is_Stor_Dev_Init(us_id))    // First time after connected
899                     {
900                         diag_printf("found a Mass Storage device [%x], try to init it\n", us_id);
901 
902                         if (bInit_USB_Disk(us_id))
903                         {
904                             for (ii=0; ii<=Get_Stor_Max_Lun(us_id); ii++)
905                             {
906                                 LunSts[ii] = LunPreSts[ii] = bIsDevValid(us_id, ii);
907                                 if (LunSts[ii])
908                                 {
909                                     diag_printf("LUN %d is init ok\n", ii);
910                                 }
911                             }
912                             Set_Stor_Dev_Init(us_id, TRUE);
913                             diag_printf("MSC plug in\n");
914                         }
915                     }
916                     else
917                     {
918                         vChk_USB_LUNs(us_id);
919 
920                         for (ii=0; ii<=Get_Stor_Max_Lun(us_id); ii++)
921                         {
922                             LunSts[ii] = bIsDevValid(us_id, ii);
923                             if ( LunSts[ii] && (LunSts[ii] != LunPreSts[ii]) )
924                             {
925                                 diag_printf("Chk LUN %d is init ok\n", ii);
926                             }
927                             LunPreSts[ii] = LunSts[ii];
928                         }
929                     }
930                 }
931                 else
932                 {
933                     #if 0
934                     if (isMSCPlugIn)
935                     {
936                         diag_printf("MSC plug out\n");
937                         isMSCPlugIn = FALSE;
938                         if ( _DrvUSB_CBFun != NULL )
939                             _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_STOR);
940                     }
941                     #endif
942                 }
943 
944             }
945 
946 
947 
948         #if USB_HID_SUPPORT
949             if (Is_Any_Hid_Dev_Info_Valid_EX())
950             {
951                 gScanHIDDevices =FALSE;
952                 for (ii=0; ii<MAX_HID_DEV_COUNT; ii++)	//find the port index
953                 {
954                     isHIDPlugIntoSamePort = FALSE;		//a bool value to indicate that the interface enumeration below is for the same port
955 		            isHIDPlugIn_IntfCnt = 0;
956                     for (jj=0; jj<MAX_HID_INTF_COUNT; jj++)	//find the interface index
957                     {
958                         if(isHIDPlugIntoSamePort==FALSE)
959 			    isHIDPlugIntoSamePort = TRUE;	//for multiple interfaces, using same port
960 
961                         if ( Is_Hid_Dev_Info_Valid_EX(ii,jj) &&
962 				  (HID_us[ii][jj]->pusb_dev->bus->hcpriv == cpe_ehci_dev.dev.driver_data) )
963                         {
964                             if (!Is_Hid_Dev_Init_EX(ii,jj))
965                             {
966 	                        if ((Invalid_HidDevs & ((1 << jj)<<(ii*MAX_HID_INTF_COUNT))) != 0)
967                                 {
968 			            continue;;
969 				}
970 
971                                if (usb_get_report_descriptor_EX(ii, jj, Hid_Report_Desc, 0x65) == ENOERR)
972                                 {
973                    	    /* index 3 of id_Report_Desc is for Local Usage type: 1(Unknown), 2(Mouse),4(Joystick),6(Keyboard)
974                    	    *  e.g. of Joystick: 5 1 9 "4" a1 1 85 ...
975                    	    */
976                    	    if(Hid_Report_Desc[3]==Usage_Unkown)
977                                     {
978        			        Invalid_HidDevs |= ((1 << jj)<<(ii * MAX_HID_INTF_COUNT));
979                        		diag_printf("Invalid_HidDevs=%lx\n",Invalid_HidDevs);
980                        		continue;
981                                    }
982 
983                                     //hid_parse_report(Hid_Report_Desc, 0x65);
984                                    usb_hid_parse_report(Hid_Report_Desc, 0x65, &HID_us[ii][jj]->buttons, HID_us[ii][jj]->axis, &HID_us[ii][jj]->keys);
985                                     diag_printf("HID finished\n");
986                                    Set_Hid_Dev_Init_EX(ii, jj, TRUE);
987 
988        			    #if 1  //RCC
989        			        if ((isHIDPlugIn_IntfCnt < MAX_HID_INTF_COUNT)&& (isHIDPlugIntoSamePort == TRUE))
990                                        {
991                                            isHIDPlugIn = TRUE;
992                                            isHIDPlugIn_IntfCnt++;
993                                            diag_printf("HID plug into Port(%d) Intf(%d)\n", ii, jj);
994                                            if ( _DrvUSB_CBFun != NULL )
995                                                _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_HID, NULL);
996                                        }
997                    	#else
998                                         if (!isHIDPlugIn)
999                                         {
1000                                             isHIDPlugIn = TRUE;
1001                                             diag_printf("HID plug in\n");
1002                                             if ( _DrvUSB_CBFun != NULL )
1003                                                 _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_HID, NULL);
1004                                         }
1005        			    #endif
1006                                 //usb_hid_get_int_ex(0, HID_us[0]->IntPipeBuf, 8);
1007                                 //MsOS_StartTimer(g_Hid_Timer_ID);
1008                                 }
1009                             }
1010                         }
1011                     }
1012                 }
1013             }
1014             else
1015             {
1016                 if (isHIDPlugIn)
1017                 {
1018                     diag_printf("HID plug out\n");
1019                     isHIDPlugIn = FALSE;
1020                     if ( _DrvUSB_CBFun != NULL )
1021                         _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_HID, NULL);
1022                 }
1023             }
1024         #endif
1025 
1026         #if USB_CDC_SUPPORT/* Start of CDC check*/
1027     		if (Is_Any_Cdc_Dev_Info_Valid())
1028     		{
1029     			for (ii=0; ii<MAX_CDC_DEV_COUNT; ii++)	//find the port index
1030     			{
1031     				if (Is_Cdc_Dev_Info_Valid(ii) &&
1032     					(CDC_us[ii]->pusb_dev->bus->hcpriv == cpe_ehci_dev.dev.driver_data) )
1033     				{
1034     					if (!Is_Cdc_Dev_Init(ii))
1035     					{
1036     						if( Usb_Cdc_Dev_Init())
1037     						{
1038     							diag_printf("CDC plug into Port(%d)\n", ii);
1039     							isCDCPlugIn = TRUE;
1040     							Set_Cdc_Dev_Init(ii, TRUE);
1041 
1042     						}
1043     						else
1044     							diag_printf("CDC plug into Port(%d),init faile\n", ii);
1045 
1046     						if ( _DrvUSB_CBFun != NULL )
1047     							_DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_CDC, NULL);
1048     					}
1049                         else
1050                             Usb_Cdc_Handle();
1051 
1052     					if (isFirstEvent && (CDC_us[ii]->pusb_dev->parent->parent != NULL))
1053     					{
1054     						diag_printf("External Hub is connected\n");
1055     						isExtHubConnect = TRUE;
1056     					}
1057 
1058     				}
1059     			}
1060     		}
1061     		else
1062     		{
1063     			if (isCDCPlugIn)
1064     			{
1065     				diag_printf("CDC plug out\n");
1066     				isCDCPlugIn = FALSE;
1067     				if ( _DrvUSB_CBFun != NULL )
1068     					_DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_CDC, NULL);
1069     			}
1070     		}
1071         #endif/* END of CDC check*/
1072 
1073 
1074             if ( (isMSCPlugIn == FALSE)
1075                 #if USB_HID_SUPPORT
1076                     && !Is_Any_Hid_Dev_Info_Valid_EX()
1077                 #endif
1078                 #if USB_CDC_SUPPORT
1079 				    && !Is_Any_Cdc_Dev_Info_Valid()
1080                 #endif
1081 				&& isFirstEvent )
1082             {
1083                 if ( PortStatus & USB_PORT_STAT_CONNECTION )
1084                 {
1085                     //if ( usb_get_connected_dev_state(&DevState, &DevClass) && (DevState < USB_STATE_CONFIGURED) )
1086                     if (usb_get_connected_dev_state(&DevState, &DevClass, g_pUsbHcd))
1087                     {
1088                         if (DevState < USB_STATE_CONFIGURED)
1089                         {
1090                             diag_printf("Usb device no responding\n");
1091                         }
1092                         else
1093                         {
1094                             if (DevClass == 0x09)
1095                             {
1096                                 diag_printf("External Hub is connected\n");
1097                                 isExtHubConnect = TRUE;
1098                             }
1099                             else
1100                             {
1101                                 diag_printf("Usb device not supported\n");
1102                                 if ( _DrvUSB_CBFun != NULL )
1103                                     _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_UNKNOW, NULL);
1104                             }
1105                         }
1106                     }
1107                 }
1108 
1109             }
1110 
1111             #if USBC_IP_SUPPORT // USBC IP control
1112             if ((pCurrentChip->usbc_ip[0].portNum == 0) && (pCurrentChip->usbc_ip[0].eventFlag))
1113                     if ( PortStatus & USB_PORT_STAT_CONNECTION)
1114                         if (_DrvUSBC_CBFun)
1115                         {
1116                             if (pCurrentChip->usbc_ip[0].eventType)
1117                                 _DrvUSBC_CBFun(USBC_NON_OVER_CURRENT, 0, NULL);
1118                             else
1119                                 _DrvUSBC_CBFun(USBC_OVER_CURRENT, 0, NULL);
1120                             pCurrentChip->usbc_ip[0].eventFlag = 0;
1121                         }
1122             #endif
1123             #if USB_HID_SUPPORT
1124 	        gScanHIDDevices =TRUE;
1125             #endif
1126             //MsOS_DelayTask(1000);
1127         }
1128 
1129 PORT_DISCONNECT:
1130         diag_printf("No device is connecting\n");
1131         //MsOS_StopTimer(g_Hid_Timer_ID);
1132 
1133         if (has_hub_events(&PortStatus))
1134         {
1135             diag_printf("Has hub event .. @ port disconnect\n");
1136             hub_poll();
1137         }
1138 
1139 #if USB_HID_SUPPORT
1140         for (ii=0; ii<MAX_HID_DEV_COUNT; ii++)
1141         {
1142             for (jj=0; jj<MAX_HID_INTF_COUNT; jj++)		//find the interface index
1143             {
1144                 if (Is_Hid_Dev_Info_Valid_EX(ii,jj))
1145                 {
1146                     if (HID_us[ii][jj]->pusb_dev->bus->hcpriv == cpe_ehci_dev.dev.driver_data)
1147                     {
1148                         diag_printf("Set HID Port(%d) Intf(%d) not init\n", ii, jj);
1149                         Set_Hid_Dev_Init_EX(ii, jj, FALSE);
1150                     }
1151                 }
1152             }
1153         }
1154 #endif
1155 
1156 #if USB_CDC_SUPPORT
1157         /* set each bDevInit of CDC_us back to FALSE */
1158         for (ii=0; ii<MAX_CDC_DEV_COUNT; ii++)
1159         {
1160             if (Is_Cdc_Dev_Info_Valid(ii))
1161             {
1162                 if (CDC_us[ii]->pusb_dev->bus->hcpriv == cpe_ehci_dev.dev.driver_data)
1163                 {
1164                     diag_printf("Set CDC Port(%d) not init\n", ii);
1165                     Set_Cdc_Dev_Init(ii, FALSE);
1166                 }
1167             }
1168         }
1169 #endif
1170 
1171         #if 0
1172         if (isMSCPlugIn)
1173         {
1174             diag_printf("MSC plug out\n");
1175             if ( _DrvUSB_CBFun != NULL )
1176                 _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_STOR);
1177         }
1178         #endif
1179 
1180         #if 0
1181         for (us_id=0; us_id<MAX_USTOR; us_id++)
1182         {
1183             if (Is_Stor_Dev_Info_Valid(us_id))
1184             {
1185                 diag_printf("!!! Remove device:%d begin..", us_id);
1186                 vRemove_DISK_LUNs(us_id);
1187                 //Free memory
1188 	            dissociate_dev(Mass_stor_us[us_id]);
1189 	            usb_stor_release_resources(Mass_stor_us[us_id]);
1190                 Clr_Stor_Dev_Info(us_id);
1191                 diag_printf("OK\n");
1192             }
1193         }
1194         #endif
1195 
1196         #if USB_HID_SUPPORT
1197         if (isHIDPlugIn)
1198         {
1199             diag_printf("HID plug out\n");
1200             if ( _DrvUSB_CBFun != NULL )
1201                 _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_HID, NULL);
1202         }
1203         #endif
1204 
1205         #if USB_CDC_SUPPORT
1206         if (isCDCPlugIn)
1207         {
1208             diag_printf("CDC plug out\n");
1209             if ( _DrvUSB_CBFun != NULL )
1210                 _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_CDC, NULL);
1211         }
1212         #endif
1213         //if get any Hub device, enter Hub handler
1214 
1215         //if get any MSC device, enter MSC handler
1216 
1217         //device is disconnected
1218 
1219         USBCriticalSectionIn(0);
1220         ResetMstarUsb(ehci);
1221         USBCriticalSectionOut(0);
1222 
1223     }
1224 }
1225 //struct usb_hcd *g_phcd;
1226 
InitUSBIntr(struct usb_hcd * hcd)1227 void InitUSBIntr(struct usb_hcd * hcd)
1228 {
1229     struct ehci_hcd  *ehci = hcd_to_ehci (hcd);
1230     U32 temp;
1231 
1232     diag_printf("InitUSBIntr enter\n");
1233     #if 0
1234     mwHost20_USBINTR_Set(HOST20_USBINTR_IntOnAsyncAdvance |
1235                                             HOST20_USBINTR_SystemError |
1236                                             HOST20_USBINTR_USBError |
1237                                             HOST20_USBINTR_CompletionOfTransaction);
1238 
1239     mwHost20_USBSTS_Set(mwHost20_USBSTS_Rd());//clear all pending interrupt
1240     //  diag_printf("usb int status:%x\n",mwHost20_USBSTS_Rd());
1241     mwOTG20_Control_Int_Polarity_Hi();
1242     #else
1243     temp = (HOST20_USBINTR_IntOnAsyncAdvance |
1244                                             HOST20_USBINTR_SystemError |
1245 #ifdef ROOTHUB_INTERRUPT_MODE
1246                                             HOST20_USBINTR_PortChangeDetect |
1247 #endif
1248                                             HOST20_USBINTR_USBError |
1249                                             HOST20_USBINTR_CompletionOfTransaction);
1250     ehci_writel (temp, (U32)&ehci->regs->intr_enable);
1251 
1252     temp = ehci_readb((U32)&ehci->regs->status);
1253     ehci_writel (temp, (U32)&ehci->regs->status); //clear all pending interrupt
1254 
1255     temp = ehci_readb((U32)&ehci->regs->bus_control);
1256     temp|= INT_POLAR;
1257     ehci_writel (temp, (U32)&ehci->regs->bus_control); // set interrupt polarity high
1258     #endif
1259 
1260 #ifndef  MS_NOSAPI
1261     //diag_printf("attach usb int\n");
1262 	MsOS_AttachInterrupt(gIrqUHC, _DrvUSB_OnInterrupt);
1263     //diag_printf("enable usb int\n");
1264 	MsOS_EnableInterrupt(gIrqUHC);
1265     //diag_printf("ok");
1266 #endif
1267 }
1268 
_DrvUSB_OnInterrupt(InterruptNum eIntNum)1269 static void _DrvUSB_OnInterrupt(InterruptNum eIntNum)
1270 {
1271     //MS_U8		OldUSBEchiID;
1272 
1273     //MS_U8 status;
1274     //gIntStatus=mwHost20_USBSTS_Rd();
1275     //diag_printf("USB INT:%x\n",gIntStatus);
1276     //mwHost20_USBSTS_Set(gIntStatus);         //clear usb intr status
1277     MsOS_DisableInterrupt(gIrqUHC);
1278     //MsOS_SetEvent(_s32UsbEventId,gIntStatus);
1279 
1280     //OldUSBEchiID = MDrv_USBGetCurntEhciNum();
1281     //MDrv_USBSelectEhci(1);
1282     g_pUsbHcd->driver->irq(g_pUsbHcd, NULL);
1283     //MDrv_USBSelectEhci(OldUSBEchiID);
1284 
1285     MsOS_EnableInterrupt(gIrqUHC);
1286 }
1287 //extern S32 gs32NonCachedPoolID_MIU0;
1288 #if 0
1289 void TestUsbDisk(void)
1290 {
1291 U8   *buf1,*buf2;
1292 U32  ii,k;
1293 
1294         buf1=(U8*)MsOS_AllocateMemory(0x10000, gs32NonCachedPoolID_MIU0 );//make 64K buffer for testing
1295         buf2=(U8*)MsOS_AllocateMemory(0x10000, gs32NonCachedPoolID_MIU0 );//make 64K buffer for testing
1296 
1297 
1298 
1299         for (ii=0 ; ii< 0x10000 ; ii++)
1300             buf1[ii]=(U8)ii & 0xff;             //make test pattern;
1301 
1302         k=0;
1303        while (1)
1304         {
1305 
1306            for (ii=0 ; ii<0x10000; ii++)
1307               buf2[ii]=0;
1308 
1309            bSCSI_Write_10(0, 0x200, 0x80,buf1);
1310            bSCSI_Read_10(0,0x200,0x80,buf2);
1311 
1312            for (ii=0 ; ii< 0x10000 ; ii++)
1313               if (buf1[ii]!=buf2[ii])
1314                 {
1315                     k=99999999;
1316                     while(1) {}         //miscompare....
1317 
1318                 }
1319            // buf1[ii]=(U8)ii & 0xff;             //make test pattern;
1320            k++;
1321         }
1322 
1323         MsOS_FreeMemory((void *)buf1,  gs32NonCachedPoolID_MIU0);
1324         MsOS_FreeMemory((void *)buf2,  gs32NonCachedPoolID_MIU0);
1325 }
1326 #endif
1327 
USBCriticalSectionIn(MS_U8 Port)1328 void USBCriticalSectionIn(MS_U8 Port)
1329 {
1330     if (Port == 0)
1331         MsOS_ObtainMutex(_s32MutexUSB, MSOS_WAIT_FOREVER);
1332     else if (Port == 2)
1333         MsOS_ObtainMutex(_s32MutexUSB_Port2, MSOS_WAIT_FOREVER);
1334     else if (Port == 1)
1335         MsOS_ObtainMutex(_s32MutexUSB_Port1, MSOS_WAIT_FOREVER);
1336     else if (Port == 3)
1337         MsOS_ObtainMutex(_s32MutexUSB_Port3, MSOS_WAIT_FOREVER);
1338 
1339     lock_usb_core();
1340 }
1341 
USBCriticalSectionOut(MS_U8 Port)1342 void USBCriticalSectionOut(MS_U8 Port)
1343 {
1344     unlock_usb_core();
1345 
1346     if (Port == 0)
1347         MsOS_ReleaseMutex(_s32MutexUSB);
1348     else if (Port == 2)
1349         MsOS_ReleaseMutex(_s32MutexUSB_Port2);
1350     else if (Port == 1)
1351         MsOS_ReleaseMutex(_s32MutexUSB_Port1);
1352     else if (Port == 3)
1353         MsOS_ReleaseMutex(_s32MutexUSB_Port3);
1354 }
1355 
MDrv_GetUsbBlockSize(MS_U8 lun)1356 MS_U32 MDrv_GetUsbBlockSize(MS_U8 lun)
1357 {
1358     MS_U32 uBlkSize = 0;
1359 
1360     if ( (Mass_stor_us[0] != NULL) && (lun <= Mass_stor_us[0]->max_lun) )
1361     {
1362         uBlkSize = Mass_stor_us[0]->Mass_stor_device[lun].u32BlockSize;
1363     }
1364 
1365     return uBlkSize;
1366 }
1367 
MDrv_GetUsbBlockSizeEx(MS_U8 uPort,MS_U8 lun)1368 MS_U32 MDrv_GetUsbBlockSizeEx(MS_U8 uPort, MS_U8 lun)
1369 {
1370     MS_U32 uBlkSize = 0;
1371 
1372     if ( (Mass_stor_us[uPort] != NULL) && (lun <= Mass_stor_us[uPort]->max_lun) )
1373     {
1374         uBlkSize = Mass_stor_us[uPort]->Mass_stor_device[lun].u32BlockSize;
1375     }
1376 
1377     return uBlkSize;
1378 }
1379 
MDrv_GetUsbBlockNum(MS_U8 lun)1380 MS_U32 MDrv_GetUsbBlockNum(MS_U8 lun)
1381 {
1382     MS_U32 uTotalBlks = 0;
1383 
1384     if ( (Mass_stor_us[0] != NULL) && (lun <= Mass_stor_us[0]->max_lun) )
1385     {
1386         uTotalBlks = Mass_stor_us[0]->Mass_stor_device[lun].u32BlockTotalNum;
1387     }
1388 
1389     return uTotalBlks;
1390 }
1391 
MDrv_GetUsbBlockNumEx(MS_U8 uPort,MS_U8 lun)1392 MS_U32 MDrv_GetUsbBlockNumEx(MS_U8 uPort, MS_U8 lun)
1393 {
1394     MS_U32 uTotalBlks = 0;
1395 
1396     if ( (Mass_stor_us[uPort] != NULL) && (lun <= Mass_stor_us[uPort]->max_lun) )
1397     {
1398         uTotalBlks = Mass_stor_us[uPort]->Mass_stor_device[lun].u32BlockTotalNum;
1399     }
1400 
1401     return uTotalBlks;
1402 }
1403 
MDrv_UsbGetMaxLUNCount(void)1404 MS_U8 MDrv_UsbGetMaxLUNCount(void)
1405 {
1406     if (Mass_stor_us[0] != NULL)
1407         return Mass_stor_us[0]->max_lun + 1;
1408     else
1409         return 0;
1410 }
1411 
MDrv_UsbGetMaxLUNCountEx(MS_U8 uPort)1412 MS_U8 MDrv_UsbGetMaxLUNCountEx(MS_U8 uPort)
1413 {
1414     if (Mass_stor_us[uPort] != NULL)
1415         return Mass_stor_us[uPort]->max_lun + 1;
1416     else
1417         return 0;
1418 }
1419 
MDrv_UsbBlockReadToMIU(MS_U8 lun,MS_U32 u32BlockAddr,MS_U32 u32BlockNum,MS_U32 u32MIUAddr)1420 MS_BOOL MDrv_UsbBlockReadToMIU(
1421     MS_U8 lun,
1422     MS_U32 u32BlockAddr,
1423     MS_U32 u32BlockNum,
1424     MS_U32 u32MIUAddr)
1425 {
1426     return bSCSI_Read_10(0, lun, u32BlockAddr, u32BlockNum, (U8*) u32MIUAddr);
1427 }
1428 
MDrv_UsbBlockReadToMIUEx(MS_U8 uPort,MS_U8 lun,MS_U32 u32BlockAddr,MS_U32 u32BlockNum,MS_U32 u32MIUAddr)1429 MS_BOOL MDrv_UsbBlockReadToMIUEx(
1430     MS_U8 uPort,
1431     MS_U8 lun,
1432     MS_U32 u32BlockAddr,
1433     MS_U32 u32BlockNum,
1434     MS_U32 u32MIUAddr)
1435 {
1436     return bSCSI_Read_10(uPort, lun, u32BlockAddr, u32BlockNum, (U8*) u32MIUAddr);
1437 }
1438 
MDrv_UsbBlockWriteFromMIU(MS_U8 lun,MS_U32 u32BlockAddr,MS_U32 u32BlockNum,MS_U32 u32MIUAddr)1439 MS_BOOL MDrv_UsbBlockWriteFromMIU(
1440     MS_U8 lun,
1441     MS_U32 u32BlockAddr,
1442     MS_U32 u32BlockNum,
1443     MS_U32 u32MIUAddr)
1444 {
1445     return bSCSI_Write_10(0, lun, u32BlockAddr, u32BlockNum, (U8*) u32MIUAddr);
1446 }
1447 
MDrv_UsbBlockWriteFromMIUEx(MS_U8 uPort,MS_U8 lun,MS_U32 u32BlockAddr,MS_U32 u32BlockNum,MS_U32 u32MIUAddr)1448 MS_BOOL MDrv_UsbBlockWriteFromMIUEx(
1449     MS_U8 uPort,
1450     MS_U8 lun,
1451     MS_U32 u32BlockAddr,
1452     MS_U32 u32BlockNum,
1453     MS_U32 u32MIUAddr)
1454 {
1455     return bSCSI_Write_10(uPort, lun, u32BlockAddr, u32BlockNum, (U8*) u32MIUAddr);
1456 }
1457 
MDrv_UsbIsLunConnected(MS_U8 uPort,MS_U8 lun)1458 MS_BOOL MDrv_UsbIsLunConnected(MS_U8 uPort, MS_U8 lun)
1459 {
1460     struct LUN_Device* LunDevice;
1461 
1462     if (Mass_stor_us[uPort] == NULL)
1463         return FALSE;
1464 
1465     LunDevice = Mass_stor_us[uPort]->Mass_stor_device;
1466 
1467     if (lun <= Mass_stor_us[uPort]->max_lun)
1468         return (LunDevice[lun].bFSInit);
1469     else
1470         return FALSE;
1471 
1472 }
1473 
1474 // ------------------------------------------------------------------------
1475 
1476 
1477 extern MS_U8 u8HubStackBuffer_Port2[HUB_STACK_SIZE];
1478 
1479 struct s_gVar4UsbPort gVar4UsbPort0 =
1480 {
1481     "USB Hub Task",
1482     0,
1483     { 0*MAX_USTOR, 1*MAX_USTOR},
1484     "cpe_ehci",
1485     "CPE_AMBA EHCI",
1486     {CPE_DEVID_USB, "CPE_EHCI HC"},
1487     u8HubStackBuffer,
1488 };
1489 
1490 struct s_gVar4UsbPort gVar4UsbPort2 =
1491 {
1492     "USB Hub Task 2",
1493     2,
1494     { 2*MAX_USTOR, 3*MAX_USTOR}, // TODO: re-arrange
1495     "cpe_ehci_2",
1496     "CPE_AMBA EHCI 2",
1497     {CPE_DEVID_USB_PORT2, "CPE_EHCI HC 2"},
1498     u8HubStackBuffer_Port2,
1499 };
1500 
1501 #ifndef  MS_NOSAPI
1502 static MS_U8 u8HubStackBuffer_Port1[HUB_STACK_SIZE];
1503 #endif
1504 struct s_gVar4UsbPort gVar4UsbPort1 =
1505 {
1506     "USB Hub Task 1",
1507     1,
1508     { MAX_USTOR, 2*MAX_USTOR},
1509     "cpe_ehci_1",
1510     "CPE_AMBA EHCI 1",
1511     {CPE_DEVID_USB_PORT1, "CPE_EHCI HC 1"},
1512     u8HubStackBuffer_Port1,
1513 };
1514 
1515 #ifndef  MS_NOSAPI
1516 static MS_U8 u8HubStackBuffer_Port3[HUB_STACK_SIZE];
1517 #endif
1518 struct s_gVar4UsbPort gVar4UsbPort3 =
1519 {
1520     "USB Hub Task 3",
1521     3,
1522     { 3*MAX_USTOR, 4*MAX_USTOR},
1523     "cpe_ehci_3",
1524     "CPE_AMBA EHCI 3",
1525     {CPE_DEVID_USB_PORT3, "CPE_EHCI HC 3"},
1526     u8HubStackBuffer_Port3,
1527 };
1528 
1529 /* USB host declaration by chip ID */
1530 struct s_ChipUsbHostDef chipURANUS4 =
1531 {
1532     CHIPID_URANUS4,
1533     "URANUS4",
1534     3,
1535     {
1536     {0, BASE_UTMI0, BASE_UHC0, BASE_USBC0, E_IRQ_UHC, E_IRQ_USBC},
1537     {0, BASE_UTMI1, BASE_UHC1, BASE_USBC1, E_IRQ_UHC1, E_IRQ_USBC1},
1538     {EHCFLAG_DPDM_SWAP, BASE_UTMI2, BASE_UHC2, BASE_USBC2, E_IRQ_UHC2, E_IRQ_USBC2},
1539     },
1540     {&gVar4UsbPort0, &gVar4UsbPort1, &gVar4UsbPort2}
1541 };
1542 
1543 struct s_ChipUsbHostDef chipKRONUS =
1544 {
1545     CHIPID_KRONUS,
1546     "KRONUS",
1547     2,
1548     {
1549     {EHCFLAG_DPDM_SWAP, BASE_UTMI0, BASE_UHC0, BASE_USBC0, E_IRQ_UHC, E_IRQ_USBC},
1550     {EHCFLAG_DPDM_SWAP, BASE_UTMI2, BASE_UHC2, BASE_USBC2, E_IRQ_UHC2, E_IRQ_USBC2},
1551     },
1552     {&gVar4UsbPort0, &gVar4UsbPort1, }
1553 };
1554 
1555 struct s_ChipUsbHostDef chipKAISERIN  =
1556 {
1557     CHIPID_KAISERIN,
1558     "KAISERIN",
1559     4,
1560     {
1561     {0, BASE_UTMI0, BASE_UHC0, BASE_USBC0, E_IRQ_UHC, E_IRQ_USBC},
1562     {0, BASE_UTMI1, BASE_UHC1, BASE_USBC1, E_IRQ_UHC1, E_IRQ_USBC1},
1563     {0, BASE_UTMI2, BASE_UHC2, BASE_USBC2, E_IRQ_UHC2, E_IRQ_USBC2},
1564     {0, BASE_UTMI3, BASE_UHC3, BASE_USBC3, E_IRQ_UHC3, E_IRQ_USBC3},
1565     },
1566     {&gVar4UsbPort0, &gVar4UsbPort1, &gVar4UsbPort2, &gVar4UsbPort3}
1567 };
1568 
1569 // any new chip added here ^^^
1570 //
1571 
_DrvUSB_OnInterrupt_EX(InterruptNum eIntNum)1572 static void _DrvUSB_OnInterrupt_EX(InterruptNum eIntNum)
1573 {
1574     // TODO: check if there is any alternative
1575     struct s_ChipUsbHostDef *pChip = pCurrentChip;
1576     struct usb_hcd *hcd;
1577     MS_U8 p;
1578 
1579     if (pChip == NULL)
1580         return;
1581     MsOS_DisableInterrupt(eIntNum);
1582     for (p = 0; p < pChip->nRootHub; p++)
1583     {
1584         if (eIntNum == pChip->reg[p].uhcIRQ)
1585             break;
1586     }
1587     hcd = pChip->p_roothub[p]->cpe_ehci_dev.dev.driver_data;
1588     hcd->driver->irq(hcd, NULL);
1589     MsOS_EnableInterrupt(eIntNum);
1590 }
1591 
InitUSBIntr_EX(struct usb_hcd * hcd)1592 void InitUSBIntr_EX(struct usb_hcd * hcd)
1593 {
1594     struct ehci_hcd  *ehci = hcd_to_ehci (hcd);
1595     U32 temp;
1596 
1597     //diag_printf("InitUSBIntr_EX enter\n");
1598     temp = (HOST20_USBINTR_IntOnAsyncAdvance |
1599                                             HOST20_USBINTR_SystemError |
1600 #ifdef ROOTHUB_INTERRUPT_MODE
1601                                             HOST20_USBINTR_PortChangeDetect |
1602 #endif
1603                                             HOST20_USBINTR_USBError |
1604                                             HOST20_USBINTR_CompletionOfTransaction);
1605     ehci_writel (temp, (U32)&ehci->regs->intr_enable);
1606 
1607     temp = ehci_readb((U32)&ehci->regs->status);
1608     ehci_writel (temp, (U32)&ehci->regs->status); //clear all pending interrupt
1609     //  diag_printf("usb int status:%x\n",mwHost20_USBSTS_Rd());
1610 
1611     temp = ehci_readb((U32)&ehci->regs->bus_control);
1612     temp|= INT_POLAR;
1613     ehci_writel (temp, (U32)&ehci->regs->bus_control);
1614 
1615 #ifndef  MS_NOSAPI
1616 	MsOS_AttachInterrupt(hcd->irq, _DrvUSB_OnInterrupt_EX);
1617     //diag_printf("enable usb int\n");
1618 	MsOS_EnableInterrupt(hcd->irq);
1619     //diag_printf("ok");
1620 #endif
1621 }
1622 
Usb_host_Init_EX(struct usb_hcd * hcd)1623 MS_BOOL Usb_host_Init_EX(struct usb_hcd *hcd)
1624 {
1625     MS_U8 wValue;
1626     MS_U16 wTimer_ms;
1627     struct ehci_hcd  *ehci = hcd_to_ehci (hcd);
1628     U32 temp, temp2;
1629     const struct device_s *__mptr = hcd->controller;
1630     struct cpe_dev *cdev;
1631 
1632     diag_printf("Usb_host_Init_EX......\thcd %x, port %d\n", hcd, hcd->host_id);
1633 
1634    //<1>.Waiting for the Device connect
1635     temp = ehci_readl((U32)&ehci->regs->command);
1636     temp |= CMD_RESET;
1637     ehci_writel (temp, (U32)&ehci->regs->command); //host controller reset
1638     while(ehci_readb((U32)&ehci->regs->command) & CMD_RESET);
1639       //mbHost20_USBCMD_HCReset_Set();            //host controller reset
1640       //while(mbHost20_USBCMD_HCReset_Rd()>0);
1641 
1642    wValue=0;
1643    wTimer_ms=0;
1644    do
1645    {
1646         wValue = ehci_readb((U32)&ehci->regs->port_status[0]) & PORT_CONNECT;
1647       //wValue=mwHost20_PORTSC_ConnectStatus_Rd();
1648             //diag_printf("wValue:%x\n",wValue);
1649 
1650       if (wValue==0) {
1651          USB_DELAY(1);//10, wait 1 ms
1652          wTimer_ms++;
1653       }
1654       //if (mwHost20_PORTSC_ConnectStatus_Rd()==0) return 0;
1655       if (wTimer_ms>9500)          // Case1:Waiting for 10 sec=10000
1656       {                                           // Case2:Waiting for 100 ms =100
1657          diag_printf("??? Waiting for Peripheral Connecting Fail...\n");
1658          return (0);
1659       }
1660    }while(wValue==0);
1661 
1662     cdev = (struct cpe_dev *)( (char *)__mptr - (char *)offsetof(struct cpe_dev,dev) );
1663 
1664     //if (PortReset(UTMIBaseAddr, UHC_BASE)>0)
1665     if (PortReset(cdev->utmibase, (U32)hcd->regs)>0)
1666         return(2);
1667 
1668     temp = (ehci_readl((U32)&ehci->regs->bus_control) >> 9) & 0x3;
1669     diag_printf("speed: %x\n", temp);
1670 
1671     //diag_printf("speed: %x\n", mwOTG20_Control_HOST_SPD_TYP_Rd());
1672 
1673     temp2 = ehci_readl((U32)&ehci->regs->hcmisc) & 0xfffffff3;
1674     if (temp == 2) // high speed
1675     //if (mwOTG20_Control_HOST_SPD_TYP_Rd()==2)                //high speed
1676     {
1677       UTMI_ORXBYTE_EX(0x09, 0x80, cdev->utmibase);                            //HS rx robust enable
1678       temp2 |= (3 << 2);
1679       //mwHost20_Misc_EOF1Time_Set(3);
1680     }
1681     else                 //full speed, low speed
1682     {
1683       UTMI_ANDXBYTE_EX(0x09, 0x7F, cdev->utmibase);
1684       temp2 |= (2 << 2);
1685       //mwHost20_Misc_EOF1Time_Set(2);
1686     }
1687     ehci_writel (temp2, (U32)&ehci->regs->hcmisc); // misc, EOF1
1688 
1689     temp = ehci_readl((U32)&ehci->regs->command) & 0xfffffff3;
1690     temp |= (HOST20_USBCMD_FrameListSize_256 << 2);
1691     ehci_writel (temp, (U32)&ehci->regs->command);
1692 
1693     temp = (HOST20_USBINTR_IntOnAsyncAdvance |
1694                                             HOST20_USBINTR_SystemError |
1695 #ifdef ROOTHUB_INTERRUPT_MODE
1696                                             HOST20_USBINTR_PortChangeDetect |
1697 #endif
1698                                             HOST20_USBINTR_USBError |
1699                                             HOST20_USBINTR_CompletionOfTransaction);
1700     ehci_writel (temp, (U32)&ehci->regs->intr_enable);
1701     //mwHost20_USBINTR_Set(HOST20_USBINTR_IntOnAsyncAdvance |
1702     //                                        HOST20_USBINTR_SystemError |
1703     //                                        HOST20_USBINTR_USBError |
1704     //                                        HOST20_USBINTR_CompletionOfTransaction);
1705 
1706    return (1);
1707 
1708 }
1709 
1710 extern void ResetMstarUsb(struct ehci_hcd *ehci);
MDrv_UsbDeviceConnect_EX(struct usb_hcd * hcd)1711 MS_BOOL MDrv_UsbDeviceConnect_EX(struct usb_hcd *hcd)
1712 {
1713     struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1714     MS_U8 temp_s;
1715     MS_U32 temp;
1716     static MS_U32 usbStartTime = 0;
1717 
1718     temp_s = ehci_readb((U32)&ehci->regs->port_status[0]);
1719     //temp=mwHost20_PORTSC_ConnectStatus_Rd_Port2();
1720     //diag_printf("ConnectStatus:%02bx\n",temp);
1721     //MsOS_DelayTask(10);
1722     if (temp_s & PORT_CONNECT)
1723     {
1724         return TRUE;
1725     }
1726     else
1727     {
1728 #ifndef MS_NOSAPI
1729         if (MsOS_GetSystemTime()-usbStartTime > 1000 )
1730         {
1731             usbStartTime=MsOS_GetSystemTime();
1732 
1733             temp = ehci_readl((U32)&ehci->regs->bus_control);
1734             temp &= ~0x80;
1735             ehci_writel(temp, (U32)&ehci->regs->bus_control);
1736             //UHC2_ANDXBYTE(0x40, 0x7F);//disable force enter FSmode
1737             ResetMstarUsb(ehci);
1738         }
1739 #endif
1740         hcd->isRootHubPortReset = TRUE;
1741         hcd->isBadDevice = FALSE;
1742         hcd->badDeviceCnt = 0;
1743         return FALSE;
1744     }
1745 }
1746 
1747 extern BOOL has_hub_events_EX(U16 *pPortStatus, struct s_gVar4UsbPort *gVar);
1748 extern void hub_poll_EX(struct s_gVar4UsbPort *gVar);
1749 extern struct cpe_dev cpe_ehci_dev_Port2; // for HID support
1750 extern struct list_head hub_event_list_Port2;
1751 extern struct list_head hub_event_list_Port1;
1752 extern struct list_head hub_event_list_Port3;
UsbTask_EX(MS_U32 argc,VOID * argv)1753 void  UsbTask_EX(MS_U32 argc, VOID *argv)
1754 {
1755     MS_U16	    PortStatus;
1756     MS_BOOL     isExtHubConnect, isFirstEvent, isConnect;
1757     int		    DevState;
1758     MS_U8       DevClass, ii, kk;
1759     MS_U8       LunSts[MAX_USB_STOR_LUNS];
1760     MS_U8       LunPreSts[MAX_USB_STOR_LUNS];
1761     MS_BOOL     isMSCPlugIn;
1762     #if USB_HID_SUPPORT
1763     MS_U8       jj;
1764     MS_BOOL     isHIDPlugIn, isHIDPlugIntoSamePort;
1765     MS_U8       isHIDPlugIn_IntfCnt;
1766     #endif
1767     MS_U8       us_id; // for MSC
1768     struct s_gVar4UsbPort *gVar = (struct s_gVar4UsbPort *) argc;
1769     struct ehci_hcd *ehci = hcd_to_ehci(gVar->p_UsbHcd);
1770 
1771     diag_printf("UsbTask EX ... port %d\n", gVar->portNum);
1772     diag_printf("gVar = %x\n", gVar);
1773     while (1)
1774     {
1775         isExtHubConnect = FALSE;
1776         isMSCPlugIn = FALSE;
1777         #if USB_HID_SUPPORT
1778         isHIDPlugIn = FALSE;
1779         #endif
1780 
1781         // Waiting for USB port connection
1782         while(1)
1783         {
1784             USBCriticalSectionIn(gVar->portNum);
1785             isConnect = MDrv_UsbDeviceConnect_EX(gVar->p_UsbHcd);
1786             USBCriticalSectionOut(gVar->portNum);
1787 
1788             #if USBC_IP_SUPPORT // USBC IP control
1789             if ((pCurrentChip->usbc_ip[gVar->portNum].portNum == gVar->portNum) && (pCurrentChip->usbc_ip[gVar->portNum].eventFlag))
1790                         if (_DrvUSBC_CBFun)
1791                         {
1792                             if (pCurrentChip->usbc_ip[gVar->portNum].eventType)
1793                                 _DrvUSBC_CBFun(USBC_NON_OVER_CURRENT, gVar->portNum, NULL);
1794                             else
1795                                 _DrvUSBC_CBFun(USBC_OVER_CURRENT, gVar->portNum, NULL);
1796                             pCurrentChip->usbc_ip[gVar->portNum].eventFlag = 0;
1797                         }
1798             #endif
1799             if(isConnect)
1800                 break;
1801             MsOS_DelayTask(100);
1802         }
1803 
1804         // removing any delay before USB bus reset
1805         //MsOS_DelayTask(1000);
1806         //diag_printf("USB Port %d is connect\n", gVar->portNum);
1807 
1808         while (1)
1809         {
1810             if (gVar->p_UsbHcd->isBadDevice)
1811             {
1812                 diag_printf("A bad device found on port %d\n", gVar->portNum);
1813                 break;
1814             }
1815 
1816             kk = 0;
1817             while(kk<HUB_DEBOUNCE_STABLE)
1818             {
1819                 USBCriticalSectionIn(gVar->portNum);
1820                 isConnect = MDrv_UsbDeviceConnect_EX(gVar->p_UsbHcd);
1821                 USBCriticalSectionOut(gVar->portNum);
1822                 if ( !isConnect )
1823                     goto PORT_DISCONNECT_EX;
1824                 kk++;
1825                 MsOS_DelayTask(HUB_DEBOUNCE_STEP);
1826             }
1827 
1828             // Device is connecting to the port
1829             isFirstEvent = FALSE;
1830             if (has_hub_events_EX(&PortStatus, gVar))
1831             {
1832                 diag_printf("EX**hub event %d\n", gVar->portNum);
1833                 isFirstEvent = TRUE;
1834                 hub_poll_EX(gVar);
1835             }
1836 
1837             if (isExtHubConnect)
1838                 hub_poll_EX(gVar);
1839 
1840             isMSCPlugIn = FALSE;
1841             for (us_id=gVar->vPortRange.vPortStart; us_id<gVar->vPortRange.vPortEnd; us_id++)
1842             {
1843                 if (Is_Stor_Dev_Info_Valid(us_id))   // Check if we found a Mass Stoarge device.
1844                 {
1845                     isMSCPlugIn = TRUE;
1846                     if (!Is_Stor_Dev_Init(us_id))    // First time after connected
1847                     {
1848                         diag_printf("found a Mass Storage device @ port %d, try to init it\n", gVar->portNum);
1849 
1850                         if (bInit_USB_Disk(us_id))
1851                         {
1852                             for (ii=0; ii<=Get_Stor_Max_Lun(us_id); ii++)
1853                             {
1854                                 LunSts[ii] = LunPreSts[ii] = bIsDevValid(us_id, ii);
1855                                 if (LunSts[ii])
1856                                 {
1857                                     diag_printf("LUN %d is init ok\n", ii);
1858                                 }
1859                             }
1860                             Set_Stor_Dev_Init(us_id, TRUE);
1861                             diag_printf("MSC plug in\n");
1862                         }
1863                     }
1864                     else
1865                     {
1866                         vChk_USB_LUNs(us_id);
1867 
1868                         for (ii=0; ii<=Get_Stor_Max_Lun(us_id); ii++)
1869                         {
1870                             LunSts[ii] = bIsDevValid(us_id, ii);
1871                             if ( LunSts[ii] && (LunSts[ii] != LunPreSts[ii]) )
1872                             {
1873                                 diag_printf("Chk LUN %d is init ok\n", ii);
1874                             }
1875                             LunPreSts[ii] = LunSts[ii];
1876                         }
1877                     }
1878                 }
1879                 else
1880                 {
1881                 }
1882 
1883             }
1884 
1885 
1886 #if USB_HID_SUPPORT
1887             if (Is_Any_Hid_Dev_Info_Valid_EX())
1888             {
1889                 for (ii=0; ii<MAX_HID_DEV_COUNT; ii++)	//find the port index
1890                 {
1891                     isHIDPlugIntoSamePort = FALSE;		//a bool value to indicate that the interface enumeration below is for the same port
1892 		            isHIDPlugIn_IntfCnt = 0;
1893                     for (jj=0; jj<MAX_HID_INTF_COUNT; jj++)	//find the interface index
1894                     {
1895                         if(isHIDPlugIntoSamePort==FALSE)
1896 			                isHIDPlugIntoSamePort = TRUE;	//for multiple interfaces, using same port
1897 
1898                         if ( Is_Hid_Dev_Info_Valid_EX(ii,jj) &&
1899 				  (HID_us[ii][jj]->pusb_dev->bus->hcpriv == cpe_ehci_dev_Port2.dev.driver_data) )
1900                         {
1901                             if (!Is_Hid_Dev_Init_EX(ii,jj))
1902                             {
1903 	                        if ((Invalid_HidDevs & ((1 << jj)<<(ii*MAX_HID_INTF_COUNT))) != 0)
1904                                 {
1905 			            continue;;
1906 				}
1907 
1908                                if (usb_get_report_descriptor_EX(ii, jj, Hid_Report_Desc, 0x65) == ENOERR)
1909                                 {
1910                    	    /* index 3 of id_Report_Desc is for Local Usage type: 1(Unknown), 2(Mouse),4(Joystick),6(Keyboard)
1911                    	    *  e.g. of Joystick: 5 1 9 "4" a1 1 85 ...
1912                    	    */
1913                    	    if(Hid_Report_Desc[3]==Usage_Unkown)
1914                                     {
1915        			        Invalid_HidDevs |= ((1 << jj)<<(ii * MAX_HID_INTF_COUNT));
1916                        		diag_printf("Invalid_HidDevs=%lx\n",Invalid_HidDevs);
1917                        		continue;
1918                                    }
1919 
1920                                     //hid_parse_report(Hid_Report_Desc, 0x65);
1921                                    usb_hid_parse_report(Hid_Report_Desc, 0x65, &HID_us[ii][jj]->buttons, HID_us[ii][jj]->axis, &HID_us[ii][jj]->keys);
1922                                     diag_printf("HID finished\n");
1923                                    Set_Hid_Dev_Init_EX(ii, jj, TRUE);
1924 
1925        			    #if 1  //RCC
1926        			        if ((isHIDPlugIn_IntfCnt < MAX_HID_INTF_COUNT)&& (isHIDPlugIntoSamePort == TRUE))
1927                                        {
1928                                            isHIDPlugIn = TRUE;
1929                                            isHIDPlugIn_IntfCnt++;
1930                                            diag_printf("HID plug into Port(%d) Intf(%d)\n", ii, jj);
1931                                            if ( _DrvUSB_CBFun != NULL )
1932                                                _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_HID, NULL);
1933                                        }
1934                    	    #else
1935                                         if (!isHIDPlugIn)
1936                                         {
1937                                             isHIDPlugIn = TRUE;
1938                                             diag_printf("HID plug in\n");
1939                                             if ( _DrvUSB_CBFun != NULL )
1940                                                 _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_HID, NULL);
1941                                         }
1942        			    #endif
1943                                 //usb_hid_get_int_ex(0, HID_us[0]->IntPipeBuf, 8);
1944                                 //MsOS_StartTimer(g_Hid_Timer_ID);
1945                                 }
1946                             }
1947                         }
1948                     }
1949                 }
1950             }
1951             else
1952             {
1953                 if (isHIDPlugIn)
1954                 {
1955                     diag_printf("HID plug out\n");
1956                     isHIDPlugIn = FALSE;
1957                     if ( _DrvUSB_CBFun != NULL )
1958                         _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_HID, NULL);
1959                 }
1960             }
1961 #endif
1962 
1963 
1964             if ( (isMSCPlugIn == FALSE)
1965                 #if USB_HID_SUPPORT
1966                     && !Is_Any_Hid_Dev_Info_Valid_EX()
1967                 #endif
1968                     && isFirstEvent )
1969             {
1970                 if ( PortStatus & USB_PORT_STAT_CONNECTION )
1971                 {
1972                     //if ( usb_get_connected_dev_state(&DevState, &DevClass) && (DevState < USB_STATE_CONFIGURED) )
1973                     if ( usb_get_connected_dev_state(&DevState, &DevClass, gVar->p_UsbHcd) )
1974                     {
1975                         if (DevState < USB_STATE_CONFIGURED)
1976                         {
1977                             diag_printf("EX> Usb device no responding\n");
1978                         }
1979                         else
1980                         {
1981                             if (DevClass == 0x09)
1982                             {
1983                                 diag_printf("EX> External Hub is connected\n");
1984                                 isExtHubConnect = TRUE;
1985                             }
1986                             else
1987                             {
1988                                 diag_printf("EX> Usb device not supported\n");
1989                                 if ( _DrvUSB_CBFun != NULL )
1990                                     _DrvUSB_CBFun(USB_PLUG_IN, USB_EVENT_DEV_TYPE_UNKNOW, NULL);
1991                             }
1992                         }
1993                     }
1994                 }
1995 
1996             }
1997 
1998             #if USBC_IP_SUPPORT // USBC IP control
1999             if ((pCurrentChip->usbc_ip[gVar->portNum].portNum == gVar->portNum) && (pCurrentChip->usbc_ip[gVar->portNum].eventFlag))
2000                     if ( PortStatus & USB_PORT_STAT_CONNECTION)
2001                         if (_DrvUSBC_CBFun)
2002                         {
2003                             if (pCurrentChip->usbc_ip[gVar->portNum].eventType)
2004                                 _DrvUSBC_CBFun(USBC_NON_OVER_CURRENT, gVar->portNum, NULL);
2005                             else
2006                                 _DrvUSBC_CBFun(USBC_OVER_CURRENT, gVar->portNum, NULL);
2007                             pCurrentChip->usbc_ip[gVar->portNum].eventFlag = 0;
2008                         }
2009             #endif
2010 
2011             //MsOS_DelayTask(1000);
2012         }
2013 
2014 PORT_DISCONNECT_EX:
2015         diag_printf("USB port %d disconnect\n", gVar->portNum);
2016         MsOS_DelayTask(600);  //By Jonas! for hub event!
2017         hub_poll_EX(gVar); //for disconnect hub event
2018 
2019         #if USB_HID_SUPPORT
2020         for (ii=0; ii<MAX_HID_DEV_COUNT; ii++)
2021         {
2022             for (jj=0; jj<MAX_HID_INTF_COUNT; jj++)		//find the interface index
2023             {
2024                 if (Is_Hid_Dev_Info_Valid_EX(ii,jj))
2025                 {
2026                     if (HID_us[ii][jj]->pusb_dev->bus->hcpriv == cpe_ehci_dev_Port2.dev.driver_data)
2027                     {
2028                         diag_printf("Set HID Port(%d) Intf(%d) not init\n", ii, jj);
2029                         Set_Hid_Dev_Init_EX(ii, jj, FALSE);
2030                     }
2031                 }
2032             }
2033         }
2034         #endif
2035 
2036         #if USB_HID_SUPPORT
2037         if (isHIDPlugIn)
2038         {
2039             diag_printf("HID plug out\n");
2040             if ( _DrvUSB_CBFun != NULL )
2041                 _DrvUSB_CBFun(USB_PLUG_OUT, USB_EVENT_DEV_TYPE_HID, NULL);
2042         }
2043         #endif
2044         //if get any Hub device, enter Hub handler
2045 
2046         //if get any MSC device, enter MSC handler
2047 
2048         //device is disconnected
2049 
2050         USBCriticalSectionIn(gVar->portNum);
2051         ResetMstarUsb(ehci);
2052         USBCriticalSectionOut(gVar->portNum);
2053 
2054     }
2055 }
2056 
Create_USB_Mutex(U8 port)2057 S32 Create_USB_Mutex(U8 port)
2058 {
2059     S32 temp = -1;
2060 
2061     if (port == 0)
2062         temp = _s32MutexUSB = MsOS_CreateMutex(E_MSOS_FIFO, "USB_MUTEX", MSOS_PROCESS_SHARED);
2063     else if (port == 2)
2064        temp =  _s32MutexUSB_Port2 = MsOS_CreateMutex(E_MSOS_FIFO, "USB_MUTEX_PORT2", MSOS_PROCESS_SHARED);
2065     else if (port == 1)
2066         temp = _s32MutexUSB_Port1 = MsOS_CreateMutex(E_MSOS_FIFO, "USB_MUTEX_PORT1", MSOS_PROCESS_SHARED);
2067     else if (port == 3)
2068         temp = _s32MutexUSB_Port3 = MsOS_CreateMutex(E_MSOS_FIFO, "USB_MUTEX_PORT3", MSOS_PROCESS_SHARED);
2069     return temp;
2070 }
2071 
Delete_USB_Mutex(U8 port)2072 void Delete_USB_Mutex(U8 port)
2073 {
2074     if (port == 0)
2075         MsOS_DeleteMutex(_s32MutexUSB);
2076     else if (port == 2)
2077        MsOS_DeleteMutex(_s32MutexUSB_Port2);
2078     else if (port == 1)
2079         MsOS_DeleteMutex(_s32MutexUSB_Port1);
2080     else if (port == 3)
2081         MsOS_DeleteMutex(_s32MutexUSB_Port3);
2082 }
2083 
USB_Start_EX(struct s_gVar4UsbPort * gVar)2084 void USB_Start_EX(struct s_gVar4UsbPort *gVar)
2085 {
2086 #ifndef  MS_NOSAPI
2087     MS_U8 *HubStack;
2088     int pid;
2089 #endif
2090 
2091     diag_printf("\nUsb start EX..., gVar = %x\n\n", gVar);
2092 
2093 #ifndef  MS_NOSAPI
2094     HubStack = gVar->u8pHubStackBuffer;
2095 
2096     if (Create_USB_Mutex(gVar->portNum) < 0)
2097     {
2098         GEN_EXCEP;
2099         return;
2100     }
2101 
2102     //Create Task
2103     pid = MsOS_CreateTask((TaskEntry) UsbTask_EX,
2104                          (MS_U32)gVar,
2105                          E_TASK_PRI_HIGH,
2106                          TRUE,
2107                          HubStack,
2108                          HUB_STACK_SIZE,
2109                          gVar->name);
2110     if (pid < 0)
2111     {
2112         GEN_EXCEP;
2113         Delete_USB_Mutex(gVar->portNum);
2114         return;
2115     }
2116 #else
2117     //UsbTask();
2118 #endif //MS_NOSAPI
2119 }
2120 
2121 extern int __init ehci_cpe_dev_init_EX(struct cpe_dev * dev);
2122 extern struct list_head hub_event_list;
2123 extern int __init ehci_hcd_cpe_driver_init_EX(void);
MDrv_USB_Port_Init_EX(MS_U8 u8PortNum)2124 MS_BOOL MDrv_USB_Port_Init_EX(MS_U8 u8PortNum)
2125 {
2126     struct s_ChipUsbHostDef *pChip = pCurrentChip;
2127 
2128     if (pChip->nRootHub <= u8PortNum)
2129     {
2130         diag_printf("Chip %s does not not support port %d\n", pChip->name, u8PortNum);
2131         return FALSE;
2132     }
2133     else
2134     {
2135         diag_printf("Init chip %s, port %d\n", pChip->name, u8PortNum);
2136         // bind chip reg definition with cpe_dev
2137         pChip->p_roothub[u8PortNum]->cpe_ehci_dev.mapbase = pChip->reg[u8PortNum].baseUHC;
2138         pChip->p_roothub[u8PortNum]->cpe_ehci_dev.utmibase = pChip->reg[u8PortNum].baseUTMI;
2139         pChip->p_roothub[u8PortNum]->cpe_ehci_dev.usbcbase = pChip->reg[u8PortNum].baseUSBC;
2140         pChip->p_roothub[u8PortNum]->cpe_ehci_dev.intNum = pChip->reg[u8PortNum].uhcIRQ;
2141         if (u8PortNum == 0)
2142             pChip->p_roothub[u8PortNum]->p_hub_event = &hub_event_list;
2143         else if (u8PortNum == 1)
2144             pChip->p_roothub[u8PortNum]->p_hub_event = &hub_event_list_Port1;
2145         else if (u8PortNum == 2)
2146             pChip->p_roothub[u8PortNum]->p_hub_event = &hub_event_list_Port2;
2147         else if (u8PortNum == 3)
2148             pChip->p_roothub[u8PortNum]->p_hub_event = &hub_event_list_Port3;
2149     }
2150 
2151     // do Usb Host initial
2152     U4_series_usb_init(pChip->reg[u8PortNum].baseUTMI, pChip->reg[u8PortNum].baseUSBC, pChip->reg[u8PortNum].baseUHC, pChip->reg[u8PortNum].iFlag);
2153 
2154     //ehci_hcd_cpe_driver_init_EX(); // register cpe_driver
2155 
2156     ehci_cpe_dev_init_EX(&pChip->p_roothub[u8PortNum]->cpe_ehci_dev); // initial cpe_dev
2157     if (pChip->p_roothub[u8PortNum]->cpe_ehci_dev.dev.driver_data == NULL)
2158     {
2159         diag_printf("port %d hcd not allocated!!!\n", u8PortNum);
2160         return FALSE;
2161     }
2162     pChip->p_roothub[u8PortNum]->p_UsbHcd = pChip->p_roothub[u8PortNum]->cpe_ehci_dev.dev.driver_data; // for MDrv_UsbDeviceConnect()
2163 
2164     USB_Start_EX(pChip->p_roothub[u8PortNum]);
2165 
2166     return TRUE;
2167 }
2168 
2169 extern struct usb_hcd *g_pUsbHcd_Port2;
msc_get_hcd(U8 host_id)2170 struct usb_hcd *msc_get_hcd(U8 host_id)
2171 {
2172     if ( (MDrv_USBGetChipID()== CHIPID_KRONUS) || (MDrv_USBGetChipID()== CHIPID_URANUS4))
2173     {
2174         if (host_id == 0)
2175             return g_pUsbHcd;
2176         else
2177             return g_pUsbHcd_Port2;
2178     }
2179     else
2180     {
2181         return pCurrentChip->p_roothub[host_id]->p_UsbHcd;
2182     }
2183 }
2184 
2185 extern void init_usbc_intr(MS_U8 p);
MDrv_OverCurrentDetect_RegisterCallBack(USBCallback pCallbackFn,MS_U8 port_mask)2186 void MDrv_OverCurrentDetect_RegisterCallBack (USBCallback pCallbackFn, MS_U8 port_mask)
2187 {
2188 #if USBC_IP_SUPPORT // USBC IP control
2189     struct s_ChipUsbHostDef *pChip = pCurrentChip;
2190     MS_U8 p;
2191 
2192     diag_printf("<MDrv_OverCurrentDetect_RegisterCallBack> %p, port_mask(%x)\n", pCallbackFn, port_mask);
2193     _DrvUSBC_CBFun = pCallbackFn;
2194     for (p = 0; p < pChip->nRootHub; p++)
2195         if (port_mask & (1<<p))
2196             init_usbc_intr(p);
2197 #else
2198     diag_printf("<MDrv_OverCurrentDetect_RegisterCallBack> NOT support!!! Please turn on USBC_IP_SUPPORT in drvUSB.h\n");
2199 #endif
2200 }
2201