xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/usbhost/drvKernel.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 #include <MsCommon.h>
80 #include <cyg/hal/hal_if.h>
81 #include "include/drvKernel.h"
82 #include "include/drvTimer.h"
83 #include "include/drvPorts.h"
84 #include "include/drvCPE_AMBA.h"
85 
86 extern int usb_core_init(void);
87 extern struct bus_type usb_bus_type;
88 
89 //int CPU_Int_State = INT_DISABLED;
90 
91 extern void init_cache_memory(void);
92 
93 S32      g_USBWaitFlg;
94 S32      g_USBWaitP2Flg;
95 
init_OS_Resource(void)96 void init_OS_Resource(void)
97 {
98 
99 	g_USBWaitFlg = MsOS_CreateEventGroup("USB_Event");
100 
101     //   g_SCSISem = MsOS_CreateSemaphore(1, E_MSOS_FIFO, "USB_Sem");
102 //      _s32MutexUSB = MsOS_CreateMutex(E_MSOS_FIFO, "USB Mutex");
103 
104 
105 }
106 
free_OS_Resource(void)107 void free_OS_Resource(void)
108 {
109 	BOOL err;
110 
111 	err = MsOS_DeleteEventGroup(g_USBWaitFlg);
112 	USB_ASSERT((err == TRUE ),"Delete Bulk Flag Fail...\n");
113 
114 //	err = MsOS_DeleteSemaphore(g_SCSISem);
115 //        err=MsOS_DeleteMutex(_s32MutexUSB);
116 //	USB_ASSERT((err == TRUE ),"Delete USB mutex Fail...\n");
117 }
118 
init_OS_Port2_Resource(void)119 void init_OS_Port2_Resource(void)
120 {
121 
122 	g_USBWaitP2Flg = MsOS_CreateEventGroup("USB_Event");
123 
124     //   g_SCSISem = MsOS_CreateSemaphore(1, E_MSOS_FIFO, "USB_Sem");
125 //      _s32MutexUSB = MsOS_CreateMutex(E_MSOS_FIFO, "USB Mutex");
126 
127 
128 }
129 
free_OS_Port2_Resource(void)130 void free_OS_Port2_Resource(void)
131 {
132 	BOOL err;
133 
134 	err = MsOS_DeleteEventGroup(g_USBWaitP2Flg);
135 	USB_ASSERT((err == TRUE ),"Delete Port2 Bulk Flag Fail...\n");
136 
137 //	err = MsOS_DeleteSemaphore(g_SCSISem);
138 //        err=MsOS_DeleteMutex(_s32MutexUSB);
139 //	USB_ASSERT((err == TRUE ),"Delete USB mutex Fail...\n");
140 }
141 
exit_sys(void)142 void exit_sys(void)
143 {
144 	free_OS_Resource();
145         free_OS_Port2_Resource();
146 }
147 
busyloop_delay(U32 count)148 void busyloop_delay(U32 count)
149 {
150    U32 i,j;
151 
152    for (i=0; i<255; i++)
153       for (j=0; j<count; j++) {
154     }
155 }
156 
157 #if 0
158 //The following code has serious bug.  Jonas_20110519
159 int find_next_zero_bit_le (void *p, int size, int offset)
160 {
161   int i,j;
162   int iOffset,jOffset;
163   char *tmp = (char *)p;
164 
165   iOffset = offset%8;
166   jOffset = offset/8;
167 
168   // Skip offset bits
169   for(i=iOffset;i<8;i++)
170     tmp[i] >>= 1;
171 
172   for(j=jOffset;j<size/8;j++)
173   {
174     for(i=iOffset;i<8;i++)
175     {
176       if ( ( tmp[j] & 0x01 ) == 0 )
177       {
178         return j*8+i;
179       }
180       tmp[j] >>= 1;
181     }
182   }
183   // not found return (maximum size) back
184   return size;
185 }
186 
187 #else
188 
189 #define BITOP_WORD(nr)		((nr) / BITS_PER_LONG)
190 
find_next_zero_bit(const U32 * addr,U32 size,U32 offset)191 U32 find_next_zero_bit(const U32 *addr, U32 size, U32 offset)
192 {
193 	const U32 *p = addr + BITOP_WORD(offset);
194 	U32 result = offset & ~(BITS_PER_LONG-1);
195 	U32 tmp;
196 
197 	if (offset >= size)
198 		return size;
199 	size -= result;
200 	offset %= BITS_PER_LONG;
201 	if (offset) {
202 		tmp = *(p++);
203 		tmp |= ~0UL >> (BITS_PER_LONG - offset);
204 		if (size < BITS_PER_LONG)
205 			goto found_first;
206 		if (~tmp)
207 			goto found_middle;
208 		size -= BITS_PER_LONG;
209 		result += BITS_PER_LONG;
210 	}
211 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
212 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
213 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
214 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
215 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
216 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
217 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
218 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
219 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
220 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
221 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
222 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
223 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
224 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
225 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
226 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
227 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
228 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
229 	while (size & ~(BITS_PER_LONG-1)) {
230 		if (~(tmp = *(p++)))
231 			goto found_middle;
232 		result += BITS_PER_LONG;
233 		size -= BITS_PER_LONG;
234 	}
235 	if (!size)
236 		return result;
237 	tmp = *p;
238 
239 found_first:
240 	tmp |= ~0UL << size;
241 	if (tmp == ~0UL)
242 		return result + size;
243 found_middle:
244 	return result + ffz(tmp);
245 }
246 
247 
find_next_zero_bit_le(U32 * addr,U32 size,U32 offset)248 U32 find_next_zero_bit_le(U32 *addr, U32 size, U32 offset)
249 {
250 	return find_next_zero_bit(addr, size, offset);
251 }
252 
253 #endif
254 
udelay(U32 us)255 void udelay(U32 us)
256 {
257    HAL_DELAY_US(us);
258 }
259 
init_sys(void)260 void init_sys(void)
261 {
262     #if 1
263 	init_cache_memory();
264 	INIT_LIST_HEAD(&usb_bus_type.devices_list);
265 	INIT_LIST_HEAD(&usb_bus_type.drivers_list);
266 	INIT_LIST_HEAD(&CPE_AMBA_bus_type.devices_list);
267 	INIT_LIST_HEAD(&CPE_AMBA_bus_type.drivers_list);
268     #endif
269 
270 	sti();
271 
272     #if  1
273 	// Init timer and init software timer link list
274 	init_timers();
275     #endif
276 
277     #if 1
278 	// Init USB core (including hub)
279 	usb_core_init();
280     #endif
281 }
282 
wait_ms(int x)283 void wait_ms(int x)
284 {
285     if (x==0) return;
286     MsOS_DelayTask(x);
287 }
288 
init_OS_Resource_EX(S32 * USBWaitFlg)289 void init_OS_Resource_EX(S32 *USBWaitFlg)
290 {
291     *USBWaitFlg = MsOS_CreateEventGroup("USB_Event");
292 }
293 
294