xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/usbhost/drvEhciHcd_2.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 "include/drvConfig.h"
80 
81 #include <MsCommon.h>
82 #include "include/drvCompiler.h"
83 #include "include/drvPorts.h"
84 #include "include/drvErrno.h"
85 #include "include/drvIO.h"
86 #include "include/drvPCIMEM.h"
87 #include "include/drvList.h"
88 #include "include/drvTimer.h"
89 #include "include/drvKernel.h"
90 // USB related implemented header files
91 #include "include/drvUSB.h"
92 #include "drvHCD.h"
93 #include "include/drvCPE_EHCI.h"
94 #include "drvEHCI.h"
95 #include "drvUSBHwCtl.h"
96 #include "drvUsbHostConfig.h"
97 #include "include/drvCPE_AMBA.h"
98 #include "drvHUB.h"
99 
100 #define EHCI_DEBUG
101 #ifdef EHCI_DEBUG
102 #define EHCI_Dbg		diag_printf
103 #else
104 #define EHCI_Dbg(x,...)
105 #endif
106 
107 #define EHCI_ERR		diag_printf
108 
109 struct cpe_dev cpe_ehci_dev_Port2;
110 struct cpe_driver ehci_hcd_cpe_driver_Port2;
111 struct hc_driver ehci_cpe_hc_driver_Port2;
112 struct usb_hcd *g_pUsbHcd_Port2 = NULL;
113 
114 extern void cpe_ehci_start_hc(struct cpe_dev *dev);
115 extern void usb_hcd_cpe_ehci_remove (struct usb_hcd *hcd, struct cpe_dev *dev);
116 extern void cpe_ehci_stop_hc(struct cpe_dev *dev);
117 extern void ehci_irq (struct usb_hcd *hcd, struct pt_regs *regs);
118 extern int ehci_hc_reset (struct usb_hcd *hcd);
119 extern  int ehci_start (struct usb_hcd *hcd);
120 extern int ehci_suspend (struct usb_hcd *hcd, U32 state);
121 extern int ehci_resume (struct usb_hcd *hcd);
122 extern void ehci_stop (struct usb_hcd *hcd);
123 extern  struct usb_hcd *ehci_hcd_alloc (void);
124 extern  void ehci_hcd_free (struct usb_hcd *hcd);
125 extern  int ehci_urb_enqueue (struct usb_hcd  *hcd, struct urb  *urb, int    mem_flags);
126 extern  int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb);
127 extern  void ehci_endpoint_disable (struct usb_hcd *hcd, struct hcd_dev *dev, int ep);
128 extern int ehci_get_frame (struct usb_hcd *hcd);
129 extern int ehci_hub_status_data (struct usb_hcd *hcd, char *buf);
130 extern  int ehci_hub_control (struct usb_hcd  *hcd, U16 typeReq, U16 wValue, U16 wIndex, char *buf, U16 wLength);
131 
132 extern struct list_head hub_event_list_Port2;
133 extern void init_OS_Resource_EX(S32 *USBWaitFlg);
134 extern int ehci_hcd_cpe_ehci_drv_remove(struct device_s *_dev);
135 extern int ehci_hcd_cpe_ehci_drv_suspend(struct device_s *_dev, U32 state, U32 level);
136 extern int ehci_hcd_cpe_ehci_drv_resume(struct device_s *_dev, U32 level);
137 extern struct bus_type CPE_AMBA_bus_type_Port2;
138 
139 
usb_hcd_cpe_ehci_probe_Port2(struct hc_driver * driver,struct usb_hcd ** hcd_out,struct cpe_dev * dev)140 int usb_hcd_cpe_ehci_probe_Port2(struct hc_driver *driver,
141         struct usb_hcd **hcd_out,
142         struct cpe_dev *dev)
143 {
144 	int retval;
145 	struct usb_hcd *hcd = 0;
146 	//Basic initialization for FPFA version IP
147 //	mwPeri20_Control_ChipEnable_Set();
148 //    #ifdef CYGPKG_USB_HALF_SPEED_MODE
149 //	mwPeri20_Control_HALFSPEEDEnable_Set();
150  //  #endif
151 
152 //#ifdef FORCE_FULL_SPEED
153 	// Force HC to full speed
154 	//mwOTG20_Control_ForceFullSpeed_Set();
155 	//mwOTG20_Control_ForceHighSpeed_Clr();
156 //#endif
157 //#ifdef FORCE_HIGH_SPEED
158 	// Force HC to high speed
159 	//mwOTG20_Control_ForceFullSpeed_Clr();
160 	//mwOTG20_Control_ForceHighSpeed_Set();
161 //#endif
162 
163 
164        diag_printf("usb_hcd_cpe_ehci_probe port2\n");
165 	cpe_ehci_start_hc(dev);
166 
167 	/*
168 	* driver->hcd_alloc() will allocate a memory space of (struct ehci_hcd) and then return the reference of ehci_hcd.hcd back
169 	* Therefore, driver can access the member of ehci_hcd later.
170 	* driver->hcd_alloc is a function point of ehci_hcd_alloc()
171 	*/
172 
173 	hcd = driver->hcd_alloc();
174        diag_printf("hcd: %lx\n", (U32)hcd);
175 	if (hcd == NULL){
176 		dbg ("hcd_alloc failed%s"," ");
177 		retval = -ENOMEM;
178 		goto err1;
179 	}
180 
181 	hcd->driver = (struct hc_driver *) driver;
182 	//hcd->description = driver->description;
183 
184 	memcpy((char*)hcd,(char*)driver,24);
185 
186 	hcd->irq  = gIrqUHC2; //CPE_USB_EHCI1_IRQ;
187 	hcd->regs = (U32*) gBaseUHC2;
188         hcd->host_id = 2; // new
189         hcd->phub_event = &hub_event_list_Port2;
190 	hcd->pdev = (struct pci_dev  *)(1111); //Fake PCI device
191 	hcd->self.controller = &dev->dev;
192 	hcd->controller = hcd->self.controller;
193 
194 	retval = hcd_buffer_create (hcd);
195 	if (retval != 0) {
196 		dbg ("pool alloc fail%s","\n");
197 		goto err1;
198 	}
199 
200 	/* till now HC has been in an indeterminate state ... */
201 	if (driver->reset && (retval = driver->reset (hcd)) < 0) {
202 		dev_err (hcd->controller, "can't reset%s","\n");
203 		goto err2;
204 	}
205 	// Initialize IRQ and ISR Trap, enable interrupt later (at the end)
206 #if 0
207 	fLib_CloseInt(hcd->irq);
208 	fLib_SetIntTrig(hcd->irq,LEVEL,L_ACTIVE);
209 	retval = fLib_ReqInt (hcd->irq, usb_hcd_cpe_ehci_hcim_irq, hcd);
210 	if ( retval == FALSE )
211 	{
212 		err("Register ISR fail%s","");
213 		retval = -EINVAL;
214 		goto err1;
215 	}
216 #ifdef CONFIG_OTG
217 #ifndef CONFIG_FARADAY_FOTG200
218 	// Enable CPE EHCI HC's Interrupt
219 	fLib_EnableInt(hcd->irq);
220 #else
221 	// Enable EHCI interrupt after intilizing OTG controller driver
222 #endif
223 #else
224 	// Do OTG support ==> Enable CPE EHCI HC's Interrupt now
225 	fLib_EnableInt(hcd->irq);
226 #endif
227 #endif
228 	//mwOTG20_Interrupt_Mask_HOST_Clr();//yuwen mark
229 
230 #if 0
231 	if (retval != TRUE) {
232 		dbg("request_irq failed%s"," ");
233 		retval = -EBUSY;
234 		goto err2;
235 	}
236 #endif
237 
238 	usb_info ("%s (CPE_AMBA) at 0x%p, irq %d\n",
239 		hcd->description, hcd->regs, hcd->irq);
240 	usb_bus_init (&hcd->self);
241 	hcd->self.op = &usb_hcd_operations;
242 	hcd->self.hcpriv = (void *) hcd;
243 	hcd->self.bus_name = "cpe_ehci_2";
244 	//hcd->product_desc = "CPE_AMBA EHCI";
245 	memcpy(hcd->product_desc, "CPE_AMBA EHCI 2", sizeof(hcd->product_desc));
246 
247         hcd->pre_sts = 0; // new
248         hcd->pre_temp = 0; // new
249         init_OS_Resource_EX(&hcd->USBWaitFlg);
250 	INIT_LIST_HEAD (&hcd->dev_list);
251 	usb_register_bus (&hcd->self);    //For USB2, marked???
252 
253 	if ((retval = driver->start (hcd)) < 0)
254 	{
255 		usb_hcd_cpe_ehci_remove(hcd, dev);
256 		return retval;
257 	}
258 
259 	*hcd_out = hcd;
260 
261 	return retval;
262 
263 err2:
264 	hcd_buffer_destroy (hcd);
265 	if (hcd)
266 	driver->hcd_free(hcd);
267 err1:
268 	cpe_ehci_stop_hc(dev);
269 	//release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
270 	return retval;
271 }
cpe_ehci_hcd_init_Port2(void)272 static void cpe_ehci_hcd_init_Port2(void)
273 {
274 	memcpy( ehci_cpe_hc_driver_Port2.description ,  "ehci_hcd_2", sizeof("ehci_hcd_2")+1);
275 
276 	/*
277 	* generic hardware linkage
278 	*/
279 	ehci_cpe_hc_driver_Port2.irq =      ehci_irq;
280 	ehci_cpe_hc_driver_Port2.flags =    HCD_MEMORY | HCD_USB2;
281 
282 	/*
283 	* basic lifecycle operations
284 	*/
285 	ehci_cpe_hc_driver_Port2.reset =    ehci_hc_reset;
286 	ehci_cpe_hc_driver_Port2.start =    ehci_start;
287 	ehci_cpe_hc_driver_Port2.suspend =  ehci_suspend;
288 	ehci_cpe_hc_driver_Port2.resume  =  ehci_resume;
289 	ehci_cpe_hc_driver_Port2.stop    =  ehci_stop;
290 
291 	/*
292 	* memory lifecycle (except per-request)
293 	*/
294 	ehci_cpe_hc_driver_Port2.hcd_alloc =  ehci_hcd_alloc;
295 	ehci_cpe_hc_driver_Port2.hcd_free  =  ehci_hcd_free;
296 
297 	/*
298 	* managing i/o requests and associated device resources
299 	*/
300 	ehci_cpe_hc_driver_Port2.urb_enqueue      = ehci_urb_enqueue;
301 	ehci_cpe_hc_driver_Port2.urb_dequeue      = ehci_urb_dequeue;
302 	ehci_cpe_hc_driver_Port2.endpoint_disable =  ehci_endpoint_disable;
303 
304 	/*
305 	* scheduling support
306 	*/
307 	ehci_cpe_hc_driver_Port2.get_frame_number =  ehci_get_frame;
308 
309 	/*
310 	* root hub support
311 	*/
312 	ehci_cpe_hc_driver_Port2.hub_status_data =  ehci_hub_status_data;
313 	ehci_cpe_hc_driver_Port2.hub_control     =  ehci_hub_control;
314 }
315 
316 extern void InitUSBIntr_Port2(struct usb_hcd * hcd);
ehci_hcd_cpe_ehci_drv_probe_Port2(struct device_s * _dev)317 static int ehci_hcd_cpe_ehci_drv_probe_Port2(struct device_s *_dev)
318 {
319     struct cpe_dev *dev;
320     struct usb_hcd *hcd = NULL;
321     int ret;
322 
323     const struct device_s *__mptr = _dev;
324     dev = (struct cpe_dev *)( (char *)__mptr - (char *)offsetof(struct cpe_dev,dev) );
325 
326     EHCI_Dbg("ehci_hcd_cpe_ehci_drv_probe_Port2 enter\n");
327     if (usb_disabled())
328         return -ENODEV;
329 
330     cpe_ehci_hcd_init_Port2();
331 
332     ret = usb_hcd_cpe_ehci_probe_Port2(&ehci_cpe_hc_driver_Port2, &hcd, dev);
333 
334     if (ret == 0)
335     {
336         dev->dev.driver_data = hcd;
337         g_pUsbHcd_Port2 = hcd;
338         InitUSBIntr_Port2(hcd);					//add USB interrupt entry . yuwen
339     }
340 
341     return ret;
342 }
ehci_hcd_cpe_ehci_init_Port2(void)343 static int __init ehci_hcd_cpe_ehci_init_Port2(void)
344 {
345 	pr_debug ("ehci_hcd: block sizes: qh %d qtd %d itd %d sitd %d\n",
346 				sizeof (struct ehci_qh), sizeof (struct ehci_qtd),
347 				sizeof (struct ehci_itd), sizeof (struct ehci_sitd));
348 	strncpy(ehci_hcd_cpe_driver_Port2.drv.name, "cpe_ehci_2", sizeof("cpe_ehci_2"));
349 	ehci_hcd_cpe_driver_Port2.drv.bus     = &CPE_AMBA_bus_type_Port2;
350 	ehci_hcd_cpe_driver_Port2.drv.probe   = ehci_hcd_cpe_ehci_drv_probe_Port2;
351 	ehci_hcd_cpe_driver_Port2.drv.remove  = ehci_hcd_cpe_ehci_drv_remove;
352 	ehci_hcd_cpe_driver_Port2.drv.suspend = ehci_hcd_cpe_ehci_drv_suspend;
353 	ehci_hcd_cpe_driver_Port2.drv.resume  = ehci_hcd_cpe_ehci_drv_resume;
354 	ehci_hcd_cpe_driver_Port2.devid       = CPE_DEVID_USB_PORT2;
355 	return driver_register(&ehci_hcd_cpe_driver_Port2.drv);
356 }
357 
358 
ehci_cpe_dev_init_Port2(void)359 int __init ehci_cpe_dev_init_Port2(void)
360 {
361 	// Cannot remove: Init list heads of device and driver lists of CPE EHCI device
362 	EHCI_Dbg("ehci_cpe_dev_init_Port2 enter\n");
363 	device_initialize(&cpe_ehci_dev_Port2.dev);
364 #ifdef DEBUG
365 	//dbg (DRIVER_INFO " Init CPE EHCI Host Controller%s"," ");
366 #endif
367 	// Init CPE EHCI device data structure
368 	cpe_ehci_dev_Port2.dev.parent =  NULL; //parent should be CPE AMBA
369 	strncpy(cpe_ehci_dev_Port2.dev.name,"CPE_EHCI HC 2", sizeof("CPE_EHCI HC 2"));
370 	strncpy(cpe_ehci_dev_Port2.dev.bus_id,"AHB0_EHCI_2", sizeof("AHB0_EHCI_2"));
371 	cpe_ehci_dev_Port2.dev.bus  = &CPE_AMBA_bus_type_Port2;
372 	cpe_ehci_dev_Port2.devid    = CPE_DEVID_USB_PORT2;
373 	cpe_ehci_dev_Port2.mapbase  = gBaseUHC2;
374 	cpe_ehci_dev_Port2.utmibase  = gBaseUTMI2;
375         cpe_ehci_dev_Port2.usbcbase  = gBaseUSBC2;
376 	cpe_ehci_dev_Port2.dma_mask = 0x00; //Unsupport DMA
377 	//register cpe_ehci devie to CPE AMBA bus
378 	device_add(&cpe_ehci_dev_Port2.dev);
379 
380 	return ehci_hcd_cpe_ehci_init_Port2();
381 }
382 
383