[lantiq]
[openwrt.git] / target / linux / lantiq / patches / 700-dwc_otg.patch
1 --- a/drivers/usb/Kconfig
2 +++ b/drivers/usb/Kconfig
3 @@ -111,6 +111,8 @@
4  
5  source "drivers/usb/host/Kconfig"
6  
7 +source "drivers/usb/dwc_otg/Kconfig"
8 +
9  source "drivers/usb/musb/Kconfig"
10  
11  source "drivers/usb/class/Kconfig"
12 --- a/drivers/usb/Makefile
13 +++ b/drivers/usb/Makefile
14 @@ -27,6 +27,8 @@
15  
16  obj-$(CONFIG_USB_WUSB)         += wusbcore/
17  
18 +obj-$(CONFIG_DWC_OTG)           += dwc_otg/
19 +
20  obj-$(CONFIG_USB_ACM)          += class/
21  obj-$(CONFIG_USB_PRINTER)      += class/
22  obj-$(CONFIG_USB_WDM)          += class/
23 --- /dev/null
24 +++ b/drivers/usb/dwc_otg/Kconfig
25 @@ -0,0 +1,37 @@
26 +config DWC_OTG
27 +        tristate "Synopsis DWC_OTG support"
28 +        depends on USB
29 +        help
30 +          This driver supports Synopsis DWC_OTG IP core
31 +                 embebbed on many SOCs (ralink, infineon, etc)
32 +
33 +choice
34 +        prompt "USB Operation Mode"
35 +        depends on DWC_OTG
36 +        default DWC_OTG_HOST_ONLY
37 +
38 +config DWC_OTG_HOST_ONLY
39 +        bool "HOST ONLY MODE"
40 +        depends on DWC_OTG
41 +
42 +config DWC_OTG_DEVICE_ONLY
43 +        bool "DEVICE ONLY MODE"
44 +        depends on DWC_OTG
45 +endchoice
46 +
47 +choice
48 +        prompt "Platform"
49 +        depends on DWC_OTG
50 +        default DWC_OTG_LANTIQ
51 +
52 +config DWC_OTG_LANTIQ
53 +        bool "Lantiq"
54 +        depends on LANTIQ
55 +        help
56 +          Danube USB Host Controller
57 +                 platform support
58 +endchoice
59 +
60 +config DWC_OTG_DEBUG
61 +        bool "Enable debug mode"
62 +        depends on DWC_OTG
63 --- /dev/null
64 +++ b/drivers/usb/dwc_otg/Makefile
65 @@ -0,0 +1,39 @@
66 +#
67 +# Makefile for DWC_otg Highspeed USB controller driver
68 +#
69 +
70 +ifeq ($(CONFIG_DWC_OTG_DEBUG),y)
71 +EXTRA_CFLAGS   += -DDEBUG
72 +endif
73 +
74 +# Use one of the following flags to compile the software in host-only or
75 +# device-only mode based on the configuration selected by the user
76 +ifeq ($(CONFIG_DWC_OTG_HOST_ONLY),y)
77 +       EXTRA_CFLAGS   += -DDWC_OTG_HOST_ONLY -DDWC_HOST_ONLY
78 +       EXTRA_CFLAGS   += -DDWC_OTG_EN_ISOC -DDWC_EN_ISOC
79 +else ifeq ($(CONFIG_DWC_OTG_DEVICE_ONLY),y)
80 +       EXTRA_CFLAGS   += -DDWC_OTG_DEVICE_ONLY
81 +else
82 +       EXTRA_CFLAGS   += -DDWC_OTG_MODE
83 +endif
84 +
85 +#      EXTRA_CFLAGS += -DDWC_HS_ELECT_TST
86 +#      EXTRA_CFLAGS    += -DDWC_OTG_EXT_CHG_PUMP
87 +
88 +ifeq ($(CONFIG_DWC_OTG_LANTIQ),y)
89 +     EXTRA_CFLAGS += -Dlinux -D__LINUX__ -DDWC_OTG_IFX -DDWC_OTG_HOST_ONLY -DDWC_HOST_ONLY  -D__KERNEL__ 
90 +endif
91 +ifeq ($(CONFIG_DWC_OTG_LANTIQ),m)
92 +     EXTRA_CFLAGS += -Dlinux -D__LINUX__ -DDWC_OTG_IFX -DDWC_HOST_ONLY -DMODULE -D__KERNEL__  -DDEBUG
93 +endif
94 +
95 +obj-$(CONFIG_DWC_OTG)  := dwc_otg.o
96 +dwc_otg-objs    := dwc_otg_hcd.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o
97 +#dwc_otg-objs  += dwc_otg_pcd.o dwc_otg_pcd_intr.o 
98 +dwc_otg-objs    += dwc_otg_attr.o 
99 +dwc_otg-objs    += dwc_otg_cil.o dwc_otg_cil_intr.o
100 +dwc_otg-objs   += dwc_otg_ifx.o
101 +dwc_otg-objs    += dwc_otg_driver.o
102 +
103 +#obj-$(CONFIG_DWC_OTG_IFX)     := dwc_otg_ifx.o
104 +#dwc_otg_ifx-objs              := dwc_otg_ifx.o
105 --- /dev/null
106 +++ b/drivers/usb/dwc_otg/dwc_otg_attr.c
107 @@ -0,0 +1,802 @@
108 +/* ==========================================================================
109 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_attr.c $
110 + * $Revision: 1.1.1.1 $
111 + * $Date: 2009-04-17 06:15:34 $
112 + * $Change: 537387 $
113 + *
114 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
115 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
116 + * otherwise expressly agreed to in writing between Synopsys and you.
117 + * 
118 + * The Software IS NOT an item of Licensed Software or Licensed Product under
119 + * any End User Software License Agreement or Agreement for Licensed Product
120 + * with Synopsys or any supplement thereto. You are permitted to use and
121 + * redistribute this Software in source and binary forms, with or without
122 + * modification, provided that redistributions of source code must retain this
123 + * notice. You may not view, use, disclose, copy or distribute this file or
124 + * any information contained herein except pursuant to this license grant from
125 + * Synopsys. If you do not agree with this notice, including the disclaimer
126 + * below, then you are not authorized to use the Software.
127 + * 
128 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
129 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
130 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
131 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
132 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
133 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
134 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
135 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
136 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
137 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
138 + * DAMAGE.
139 + * ========================================================================== */
140 +
141 +/** @file 
142 + *
143 + * The diagnostic interface will provide access to the controller for
144 + * bringing up the hardware and testing.  The Linux driver attributes
145 + * feature will be used to provide the Linux Diagnostic
146 + * Interface. These attributes are accessed through sysfs.
147 + */
148 +
149 +/** @page "Linux Module Attributes" 
150 + *
151 + * The Linux module attributes feature is used to provide the Linux
152 + * Diagnostic Interface.  These attributes are accessed through sysfs.
153 + * The diagnostic interface will provide access to the controller for
154 + * bringing up the hardware and testing.
155 +
156 +
157 + The following table shows the attributes.
158 + <table>
159 + <tr>
160 + <td><b> Name</b></td>
161 + <td><b> Description</b></td>
162 + <td><b> Access</b></td>
163 + </tr>
164
165 + <tr>
166 + <td> mode </td>
167 + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
168 + <td> Read</td>
169 + </tr>
170
171 + <tr>
172 + <td> hnpcapable </td>
173 + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
174 + Read returns the current value.</td>
175 + <td> Read/Write</td>
176 + </tr>
177
178 + <tr>
179 + <td> srpcapable </td>
180 + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
181 + Read returns the current value.</td>
182 + <td> Read/Write</td>
183 + </tr>
184
185 + <tr>
186 + <td> hnp </td>
187 + <td> Initiates the Host Negotiation Protocol.  Read returns the status.</td>
188 + <td> Read/Write</td>
189 + </tr>
190
191 + <tr>
192 + <td> srp </td>
193 + <td> Initiates the Session Request Protocol.  Read returns the status.</td>
194 + <td> Read/Write</td>
195 + </tr>
196
197 + <tr>
198 + <td> buspower </td>
199 + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
200 + <td> Read/Write</td>
201 + </tr>
202
203 + <tr>
204 + <td> bussuspend </td>
205 + <td> Suspends the USB bus.</td>
206 + <td> Read/Write</td>
207 + </tr>
208
209 + <tr>
210 + <td> busconnected </td>
211 + <td> Gets the connection status of the bus</td>
212 + <td> Read</td>
213 + </tr>
214
215 + <tr>
216 + <td> gotgctl </td>
217 + <td> Gets or sets the Core Control Status Register.</td>
218 + <td> Read/Write</td>
219 + </tr>
220
221 + <tr>
222 + <td> gusbcfg </td>
223 + <td> Gets or sets the Core USB Configuration Register</td>
224 + <td> Read/Write</td>
225 + </tr>
226
227 + <tr>
228 + <td> grxfsiz </td>
229 + <td> Gets or sets the Receive FIFO Size Register</td>
230 + <td> Read/Write</td>
231 + </tr>
232
233 + <tr>
234 + <td> gnptxfsiz </td>
235 + <td> Gets or sets the non-periodic Transmit Size Register</td>
236 + <td> Read/Write</td>
237 + </tr>
238
239 + <tr>
240 + <td> gpvndctl </td>
241 + <td> Gets or sets the PHY Vendor Control Register</td>
242 + <td> Read/Write</td>
243 + </tr>
244
245 + <tr>
246 + <td> ggpio </td>
247 + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
248 + or sets the upper 16 bits.</td>
249 + <td> Read/Write</td>
250 + </tr>
251
252 + <tr>
253 + <td> guid </td>
254 + <td> Gets or sets the value of the User ID Register</td>
255 + <td> Read/Write</td>
256 + </tr>
257
258 + <tr>
259 + <td> gsnpsid </td>
260 + <td> Gets the value of the Synopsys ID Regester</td>
261 + <td> Read</td>
262 + </tr>
263
264 + <tr>
265 + <td> devspeed </td>
266 + <td> Gets or sets the device speed setting in the DCFG register</td>
267 + <td> Read/Write</td>
268 + </tr>
269
270 + <tr>
271 + <td> enumspeed </td>
272 + <td> Gets the device enumeration Speed.</td>
273 + <td> Read</td>
274 + </tr>
275
276 + <tr>
277 + <td> hptxfsiz </td>
278 + <td> Gets the value of the Host Periodic Transmit FIFO</td>
279 + <td> Read</td>
280 + </tr>
281
282 + <tr>
283 + <td> hprt0 </td>
284 + <td> Gets or sets the value in the Host Port Control and Status Register</td>
285 + <td> Read/Write</td>
286 + </tr>
287
288 + <tr>
289 + <td> regoffset </td>
290 + <td> Sets the register offset for the next Register Access</td>
291 + <td> Read/Write</td>
292 + </tr>
293
294 + <tr>
295 + <td> regvalue </td>
296 + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
297 + <td> Read/Write</td>
298 + </tr>
299
300 + <tr>
301 + <td> remote_wakeup </td>
302 + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
303 + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
304 + Wakeup signalling bit in the Device Control Register is set for 1
305 + milli-second.</td>
306 + <td> Read/Write</td>
307 + </tr>
308
309 + <tr>
310 + <td> regdump </td>
311 + <td> Dumps the contents of core registers.</td>
312 + <td> Read</td>
313 + </tr>
314
315 + <tr>
316 + <td> hcddump </td>
317 + <td> Dumps the current HCD state.</td>
318 + <td> Read</td>
319 + </tr>
320
321 + <tr>
322 + <td> hcd_frrem </td>
323 + <td> Shows the average value of the Frame Remaining
324 + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
325 + occurs. This can be used to determine the average interrupt latency. Also
326 + shows the average Frame Remaining value for start_transfer and the "a" and
327 + "b" sample points. The "a" and "b" sample points may be used during debugging
328 + bto determine how long it takes to execute a section of the HCD code.</td>
329 + <td> Read</td>
330 + </tr>
331
332 + <tr>
333 + <td> rd_reg_test </td>
334 + <td> Displays the time required to read the GNPTXFSIZ register many times
335 + (the output shows the number of times the register is read).
336 + <td> Read</td>
337 + </tr>
338
339 + <tr>
340 + <td> wr_reg_test </td>
341 + <td> Displays the time required to write the GNPTXFSIZ register many times
342 + (the output shows the number of times the register is written).
343 + <td> Read</td>
344 + </tr>
345
346 + </table>
347
348 + Example usage:
349 + To get the current mode:
350 + cat /sys/devices/lm0/mode
351
352 + To power down the USB:
353 + echo 0 > /sys/devices/lm0/buspower
354 + */
355 +#include <linux/kernel.h>
356 +#include <linux/module.h>
357 +#include <linux/moduleparam.h>
358 +#include <linux/init.h>
359 +#include <linux/device.h>
360 +#include <linux/errno.h>
361 +#include <linux/types.h>
362 +#include <linux/stat.h>  /* permission constants */
363 +
364 +#include <asm/io.h>
365 +
366 +#include "dwc_otg_plat.h"
367 +#include "dwc_otg_attr.h"
368 +#include "dwc_otg_driver.h"
369 +// #include "dwc_otg_pcd.h"
370 +#include "dwc_otg_hcd.h"
371 +
372 +// 20070316, winder added.
373 +#ifndef SZ_256K
374 +#define SZ_256K                         0x00040000
375 +#endif
376 +
377 +/*
378 + * MACROs for defining sysfs attribute
379 + */
380 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
381 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
382 +{ \
383 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);\
384 +       uint32_t val; \
385 +       val = dwc_read_reg32 (_addr_); \
386 +       val = (val & (_mask_)) >> _shift_; \
387 +       return sprintf (buf, "%s = 0x%x\n", _string_, val); \
388 +}
389 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
390 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, const char *buf, size_t count) \
391 +{ \
392 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);\
393 +       uint32_t set = simple_strtoul(buf, NULL, 16); \
394 +       uint32_t clear = set; \
395 +       clear = ((~clear) << _shift_) & _mask_; \
396 +       set = (set << _shift_) & _mask_; \
397 +       dev_dbg(_dev, "Storing Address=0x%08x Set=0x%08x Clear=0x%08x\n", (uint32_t)_addr_, set, clear); \
398 +       dwc_modify_reg32(_addr_, clear, set); \
399 +       return count; \
400 +}
401 +
402 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
403 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
404 +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
405 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
406 +
407 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
408 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_addr_,_mask_,_shift_,_string_) \
409 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
410 +
411 +/*
412 + * MACROs for defining sysfs attribute for 32-bit registers
413 + */
414 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_addr_,_string_) \
415 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
416 +{ \
417 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);\
418 +       uint32_t val; \
419 +       val = dwc_read_reg32 (_addr_); \
420 +       return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
421 +}
422 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_addr_,_string_) \
423 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, const char *buf, size_t count) \
424 +{ \
425 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);\
426 +       uint32_t val = simple_strtoul(buf, NULL, 16); \
427 +       dev_dbg(_dev, "Storing Address=0x%08x Val=0x%08x\n", (uint32_t)_addr_, val); \
428 +       dwc_write_reg32(_addr_, val); \
429 +       return count; \
430 +}
431 +
432 +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
433 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_addr_,_string_) \
434 +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_addr_,_string_) \
435 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
436 +
437 +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
438 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_addr_,_string_) \
439 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
440 +
441 +
442 +/** @name Functions for Show/Store of Attributes */
443 +/**@{*/
444 +
445 +/**
446 + * Show the register offset of the Register Access.
447 + */
448 +static ssize_t regoffset_show( struct device *_dev, struct device_attribute *attr, char *buf) 
449 +{
450 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
451 +       return snprintf(buf, sizeof("0xFFFFFFFF\n")+1,"0x%08x\n", otg_dev->reg_offset);
452 +}
453 +
454 +/**
455 + * Set the register offset for the next Register Access        Read/Write
456 + */
457 +static ssize_t regoffset_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
458 +                                size_t count ) 
459 +{
460 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
461 +       uint32_t offset = simple_strtoul(buf, NULL, 16);
462 +       //dev_dbg(_dev, "Offset=0x%08x\n", offset);
463 +       if (offset < SZ_256K ) {
464 +               otg_dev->reg_offset = offset;
465 +       }
466 +       else {
467 +               dev_err( _dev, "invalid offset\n" );
468 +       }
469 +
470 +       return count;
471 +}
472 +DEVICE_ATTR(regoffset, S_IRUGO|S_IWUSR, regoffset_show, regoffset_store);
473 +
474 +/**
475 + * Show the value of the register at the offset in the reg_offset
476 + * attribute.
477 + */
478 +static ssize_t regvalue_show( struct device *_dev, struct device_attribute *attr, char *buf) 
479 +{
480 +       dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
481 +       uint32_t val;
482 +       volatile uint32_t *addr;
483 +        
484 +       if (otg_dev->reg_offset != 0xFFFFFFFF &&  0 != otg_dev->base) {
485 +               /* Calculate the address */
486 +               addr = (uint32_t*)(otg_dev->reg_offset + 
487 +                                  (uint8_t*)otg_dev->base);
488 +               //dev_dbg(_dev, "@0x%08x\n", (unsigned)addr); 
489 +               val = dwc_read_reg32( addr );             
490 +               return snprintf(buf, sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n")+1,
491 +                               "Reg@0x%06x = 0x%08x\n", 
492 +                               otg_dev->reg_offset, val);
493 +       }
494 +       else {
495 +               dev_err(_dev, "Invalid offset (0x%0x)\n", 
496 +                       otg_dev->reg_offset);
497 +               return sprintf(buf, "invalid offset\n" );
498 +       }
499 +}
500 +
501 +/**
502 + * Store the value in the register at the offset in the reg_offset
503 + * attribute.
504 + * 
505 + */
506 +static ssize_t regvalue_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
507 +                               size_t count ) 
508 +{
509 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
510 +       volatile uint32_t * addr;
511 +       uint32_t val = simple_strtoul(buf, NULL, 16);
512 +       //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
513 +       if (otg_dev->reg_offset != 0xFFFFFFFF && 0 != otg_dev->base) {
514 +               /* Calculate the address */
515 +               addr = (uint32_t*)(otg_dev->reg_offset + 
516 +                                  (uint8_t*)otg_dev->base);
517 +               //dev_dbg(_dev, "@0x%08x\n", (unsigned)addr); 
518 +               dwc_write_reg32( addr, val );
519 +       }
520 +       else {
521 +               dev_err(_dev, "Invalid Register Offset (0x%08x)\n", 
522 +                       otg_dev->reg_offset);
523 +       }
524 +       return count;
525 +}
526 +DEVICE_ATTR(regvalue,  S_IRUGO|S_IWUSR, regvalue_show, regvalue_store);
527 +
528 +/*
529 + * Attributes
530 + */
531 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<20),20,"Mode");
532 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable,&(otg_dev->core_if->core_global_regs->gusbcfg),(1<<9),9,"Mode");
533 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable,&(otg_dev->core_if->core_global_regs->gusbcfg),(1<<8),8,"Mode");
534 +
535 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
536 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
537 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected,otg_dev->core_if->host_if->hprt0,0x01,0,"Bus Connected");
538 +
539 +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl,&(otg_dev->core_if->core_global_regs->gotgctl),"GOTGCTL");
540 +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,&(otg_dev->core_if->core_global_regs->gusbcfg),"GUSBCFG");
541 +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,&(otg_dev->core_if->core_global_regs->grxfsiz),"GRXFSIZ");
542 +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,&(otg_dev->core_if->core_global_regs->gnptxfsiz),"GNPTXFSIZ");
543 +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,&(otg_dev->core_if->core_global_regs->gpvndctl),"GPVNDCTL");
544 +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,&(otg_dev->core_if->core_global_regs->ggpio),"GGPIO");
545 +DWC_OTG_DEVICE_ATTR_REG32_RW(guid,&(otg_dev->core_if->core_global_regs->guid),"GUID");
546 +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,&(otg_dev->core_if->core_global_regs->gsnpsid),"GSNPSID");
547 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed,&(otg_dev->core_if->dev_if->dev_global_regs->dcfg),0x3,0,"Device Speed");
548 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed,&(otg_dev->core_if->dev_if->dev_global_regs->dsts),0x6,1,"Device Enumeration Speed");
549 +
550 +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,&(otg_dev->core_if->core_global_regs->hptxfsiz),"HPTXFSIZ");
551 +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0,otg_dev->core_if->host_if->hprt0,"HPRT0");
552 +
553 +
554 +/**
555 + * @todo Add code to initiate the HNP.
556 + */
557 +/**
558 + * Show the HNP status bit
559 + */
560 +static ssize_t hnp_show( struct device *_dev, struct device_attribute *attr, char *buf) 
561 +{
562 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
563 +       gotgctl_data_t val;
564 +       val.d32 = dwc_read_reg32 (&(otg_dev->core_if->core_global_regs->gotgctl));
565 +       return sprintf (buf, "HstNegScs = 0x%x\n", val.b.hstnegscs);
566 +}
567 +
568 +/**
569 + * Set the HNP Request bit
570 + */
571 +static ssize_t hnp_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
572 +                         size_t count ) 
573 +{
574 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
575 +       uint32_t in = simple_strtoul(buf, NULL, 16);
576 +       uint32_t *addr = (uint32_t *)&(otg_dev->core_if->core_global_regs->gotgctl);
577 +       gotgctl_data_t mem;
578 +       mem.d32 = dwc_read_reg32(addr);
579 +       mem.b.hnpreq = in;
580 +       dev_dbg(_dev, "Storing Address=0x%08x Data=0x%08x\n", (uint32_t)addr, mem.d32);
581 +       dwc_write_reg32(addr, mem.d32);
582 +       return count;
583 +}
584 +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
585 +
586 +/**
587 + * @todo Add code to initiate the SRP.
588 + */
589 +/**
590 + * Show the SRP status bit
591 + */
592 +static ssize_t srp_show( struct device *_dev, struct device_attribute *attr, char *buf) 
593 +{
594 +#ifndef DWC_HOST_ONLY
595 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
596 +       gotgctl_data_t val;
597 +       val.d32 = dwc_read_reg32 (&(otg_dev->core_if->core_global_regs->gotgctl));
598 +       return sprintf (buf, "SesReqScs = 0x%x\n", val.b.sesreqscs);
599 +#else
600 +       return sprintf(buf, "Host Only Mode!\n");
601 +#endif
602 +}
603 +
604 +/**
605 + * Set the SRP Request bit
606 + */
607 +static ssize_t srp_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
608 +                         size_t count ) 
609 +{
610 +#ifndef DWC_HOST_ONLY
611 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
612 +       dwc_otg_pcd_initiate_srp(otg_dev->pcd);
613 +#endif
614 +       return count;
615 +}
616 +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
617 +
618 +/**
619 + * @todo Need to do more for power on/off?
620 + */
621 +/**
622 + * Show the Bus Power status
623 + */
624 +static ssize_t buspower_show( struct device *_dev, struct device_attribute *attr, char *buf) 
625 +{
626 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
627 +       hprt0_data_t val;
628 +       val.d32 = dwc_read_reg32 (otg_dev->core_if->host_if->hprt0);
629 +       return sprintf (buf, "Bus Power = 0x%x\n", val.b.prtpwr);
630 +}
631 +
632 +
633 +/**
634 + * Set the Bus Power status
635 + */
636 +static ssize_t buspower_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
637 +                               size_t count ) 
638 +{
639 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
640 +       uint32_t on = simple_strtoul(buf, NULL, 16);
641 +       uint32_t *addr = (uint32_t *)otg_dev->core_if->host_if->hprt0;
642 +       hprt0_data_t mem;
643 +
644 +       mem.d32 = dwc_read_reg32(addr);
645 +       mem.b.prtpwr = on;
646 +
647 +       //dev_dbg(_dev, "Storing Address=0x%08x Data=0x%08x\n", (uint32_t)addr, mem.d32);
648 +       dwc_write_reg32(addr, mem.d32);
649 +
650 +       return count;
651 +}
652 +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
653 +
654 +/**
655 + * @todo Need to do more for suspend?
656 + */
657 +/**
658 + * Show the Bus Suspend status
659 + */
660 +static ssize_t bussuspend_show( struct device *_dev, struct device_attribute *attr, char *buf) 
661 +{
662 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
663 +       hprt0_data_t val;
664 +       val.d32 = dwc_read_reg32 (otg_dev->core_if->host_if->hprt0);
665 +       return sprintf (buf, "Bus Suspend = 0x%x\n", val.b.prtsusp);
666 +}
667 +
668 +/**
669 + * Set the Bus Suspend status
670 + */
671 +static ssize_t bussuspend_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
672 +                                 size_t count ) 
673 +{
674 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
675 +       uint32_t in = simple_strtoul(buf, NULL, 16);
676 +       uint32_t *addr = (uint32_t *)otg_dev->core_if->host_if->hprt0;
677 +       hprt0_data_t mem;
678 +       mem.d32 = dwc_read_reg32(addr);
679 +       mem.b.prtsusp = in;
680 +       dev_dbg(_dev, "Storing Address=0x%08x Data=0x%08x\n", (uint32_t)addr, mem.d32);
681 +       dwc_write_reg32(addr, mem.d32);
682 +       return count;
683 +}
684 +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
685 +
686 +/**
687 + * Show the status of Remote Wakeup.
688 + */
689 +static ssize_t remote_wakeup_show( struct device *_dev, struct device_attribute *attr, char *buf) 
690 +{
691 +#ifndef DWC_HOST_ONLY
692 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
693 +       dctl_data_t val;
694 +       val.d32 = dwc_read_reg32( &otg_dev->core_if->dev_if->dev_global_regs->dctl);
695 +       return sprintf( buf, "Remote Wakeup = %d Enabled = %d\n", 
696 +                        val.b.rmtwkupsig, otg_dev->pcd->remote_wakeup_enable);
697 +#else
698 +       return sprintf(buf, "Host Only Mode!\n");
699 +#endif
700 +}
701 +
702 +/**
703 + * Initiate a remote wakeup of the host.  The Device control register
704 + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
705 + * flag is set.
706 + * 
707 + */
708 +static ssize_t remote_wakeup_store( struct device *_dev, struct device_attribute *attr, const char *buf, 
709 +                                    size_t count ) 
710 +{
711 +#ifndef DWC_HOST_ONLY
712 +        uint32_t val = simple_strtoul(buf, NULL, 16);        
713 +       dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
714 +       if (val&1) {
715 +               dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
716 +       }
717 +       else {
718 +               dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
719 +       }
720 +#endif
721 +       return count;
722 +}
723 +DEVICE_ATTR(remote_wakeup,  S_IRUGO|S_IWUSR, remote_wakeup_show, 
724 +            remote_wakeup_store);
725 +
726 +/**
727 + * Dump global registers and either host or device registers (depending on the
728 + * current mode of the core).
729 + */
730 +static ssize_t regdump_show( struct device *_dev, struct device_attribute *attr, char *buf) 
731 +{
732 +#ifdef DEBUG
733 +       dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
734 +       printk("%s otg_dev=0x%p\n", __FUNCTION__, otg_dev);
735 +
736 +        dwc_otg_dump_global_registers( otg_dev->core_if);
737 +        if (dwc_otg_is_host_mode(otg_dev->core_if)) {
738 +                dwc_otg_dump_host_registers( otg_dev->core_if);
739 +        } else {
740 +                dwc_otg_dump_dev_registers( otg_dev->core_if);
741 +        }
742 +#endif
743 +
744 +       return sprintf( buf, "Register Dump\n" );
745 +}
746 +
747 +DEVICE_ATTR(regdump, S_IRUGO|S_IWUSR, regdump_show, 0);
748 +
749 +/**
750 + * Dump the current hcd state.
751 + */
752 +static ssize_t hcddump_show( struct device *_dev, struct device_attribute *attr, char *buf) 
753 +{
754 +#ifndef DWC_DEVICE_ONLY
755 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
756 +       dwc_otg_hcd_dump_state(otg_dev->hcd);
757 +#endif
758 +       return sprintf( buf, "HCD Dump\n" );
759 +}
760 +
761 +DEVICE_ATTR(hcddump, S_IRUGO|S_IWUSR, hcddump_show, 0);
762 +
763 +/**
764 + * Dump the average frame remaining at SOF. This can be used to
765 + * determine average interrupt latency. Frame remaining is also shown for
766 + * start transfer and two additional sample points.
767 + */
768 +static ssize_t hcd_frrem_show( struct device *_dev, struct device_attribute *attr, char *buf) 
769 +{
770 +#ifndef DWC_DEVICE_ONLY
771 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
772 +       dwc_otg_hcd_dump_frrem(otg_dev->hcd);
773 +#endif
774 +       return sprintf( buf, "HCD Dump Frame Remaining\n" );
775 +}
776 +
777 +DEVICE_ATTR(hcd_frrem, S_IRUGO|S_IWUSR, hcd_frrem_show, 0);
778 +
779 +/**
780 + * Displays the time required to read the GNPTXFSIZ register many times (the
781 + * output shows the number of times the register is read).
782 + */
783 +#define RW_REG_COUNT 10000000
784 +#define MSEC_PER_JIFFIE 1000/HZ        
785 +static ssize_t rd_reg_test_show( struct device *_dev, struct device_attribute *attr, char *buf) 
786 +{
787 +       int i;
788 +       int time;
789 +       int start_jiffies;
790 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
791 +
792 +       printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
793 +              HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
794 +       start_jiffies = jiffies;
795 +       for (i = 0; i < RW_REG_COUNT; i++) {
796 +               dwc_read_reg32(&otg_dev->core_if->core_global_regs->gnptxfsiz);
797 +       }
798 +       time = jiffies - start_jiffies;
799 +       return sprintf( buf, "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
800 +                       RW_REG_COUNT, time * MSEC_PER_JIFFIE, time );
801 +}
802 +
803 +DEVICE_ATTR(rd_reg_test, S_IRUGO|S_IWUSR, rd_reg_test_show, 0);
804 +
805 +/**
806 + * Displays the time required to write the GNPTXFSIZ register many times (the
807 + * output shows the number of times the register is written).
808 + */
809 +static ssize_t wr_reg_test_show( struct device *_dev, struct device_attribute *attr, char *buf) 
810 +{
811 +       int i;
812 +       int time;
813 +       int start_jiffies;
814 +        dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
815 +       uint32_t reg_val;
816 +
817 +       printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
818 +              HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
819 +       reg_val = dwc_read_reg32(&otg_dev->core_if->core_global_regs->gnptxfsiz);
820 +       start_jiffies = jiffies;
821 +       for (i = 0; i < RW_REG_COUNT; i++) {
822 +               dwc_write_reg32(&otg_dev->core_if->core_global_regs->gnptxfsiz, reg_val);
823 +       }
824 +       time = jiffies - start_jiffies;
825 +       return sprintf( buf, "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
826 +                       RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
827 +}
828 +
829 +DEVICE_ATTR(wr_reg_test, S_IRUGO|S_IWUSR, wr_reg_test_show, 0);
830 +/**@}*/
831 +
832 +/**
833 + * Create the device files
834 + */
835 +void dwc_otg_attr_create (struct device *_dev)
836 +{
837 +    int retval;
838 +    
839 +    retval = device_create_file(_dev, &dev_attr_regoffset);
840 +    retval += device_create_file(_dev, &dev_attr_regvalue);
841 +    retval += device_create_file(_dev, &dev_attr_mode);
842 +    retval += device_create_file(_dev, &dev_attr_hnpcapable);
843 +    retval += device_create_file(_dev, &dev_attr_srpcapable);
844 +    retval += device_create_file(_dev, &dev_attr_hnp);
845 +    retval += device_create_file(_dev, &dev_attr_srp);
846 +    retval += device_create_file(_dev, &dev_attr_buspower);
847 +    retval += device_create_file(_dev, &dev_attr_bussuspend);
848 +    retval += device_create_file(_dev, &dev_attr_busconnected);
849 +    retval += device_create_file(_dev, &dev_attr_gotgctl);
850 +    retval += device_create_file(_dev, &dev_attr_gusbcfg);
851 +    retval += device_create_file(_dev, &dev_attr_grxfsiz);
852 +    retval += device_create_file(_dev, &dev_attr_gnptxfsiz);
853 +    retval += device_create_file(_dev, &dev_attr_gpvndctl);
854 +    retval += device_create_file(_dev, &dev_attr_ggpio);
855 +    retval += device_create_file(_dev, &dev_attr_guid);
856 +    retval += device_create_file(_dev, &dev_attr_gsnpsid);
857 +    retval += device_create_file(_dev, &dev_attr_devspeed);
858 +    retval += device_create_file(_dev, &dev_attr_enumspeed);
859 +    retval += device_create_file(_dev, &dev_attr_hptxfsiz);
860 +    retval += device_create_file(_dev, &dev_attr_hprt0);
861 +    retval += device_create_file(_dev, &dev_attr_remote_wakeup);
862 +    retval += device_create_file(_dev, &dev_attr_regdump);
863 +    retval += device_create_file(_dev, &dev_attr_hcddump);
864 +    retval += device_create_file(_dev, &dev_attr_hcd_frrem);
865 +    retval += device_create_file(_dev, &dev_attr_rd_reg_test);
866 +    retval += device_create_file(_dev, &dev_attr_wr_reg_test);
867 +
868 +    if(retval != 0)
869 +    {
870 +        DWC_PRINT("cannot create sysfs device files.\n");
871 +        // DWC_PRINT("killing own sysfs device files!\n");
872 +        dwc_otg_attr_remove(_dev);
873 +    }
874 +}
875 +
876 +/**
877 + * Remove the device files
878 + */
879 +void dwc_otg_attr_remove (struct device *_dev)
880 +{
881 +       device_remove_file(_dev, &dev_attr_regoffset);
882 +       device_remove_file(_dev, &dev_attr_regvalue);
883 +       device_remove_file(_dev, &dev_attr_mode);
884 +       device_remove_file(_dev, &dev_attr_hnpcapable);
885 +       device_remove_file(_dev, &dev_attr_srpcapable);
886 +       device_remove_file(_dev, &dev_attr_hnp);
887 +       device_remove_file(_dev, &dev_attr_srp);
888 +       device_remove_file(_dev, &dev_attr_buspower);
889 +       device_remove_file(_dev, &dev_attr_bussuspend);
890 +       device_remove_file(_dev, &dev_attr_busconnected);
891 +       device_remove_file(_dev, &dev_attr_gotgctl);
892 +       device_remove_file(_dev, &dev_attr_gusbcfg);
893 +       device_remove_file(_dev, &dev_attr_grxfsiz);
894 +       device_remove_file(_dev, &dev_attr_gnptxfsiz);
895 +       device_remove_file(_dev, &dev_attr_gpvndctl);
896 +       device_remove_file(_dev, &dev_attr_ggpio);
897 +       device_remove_file(_dev, &dev_attr_guid);
898 +       device_remove_file(_dev, &dev_attr_gsnpsid);
899 +       device_remove_file(_dev, &dev_attr_devspeed);
900 +       device_remove_file(_dev, &dev_attr_enumspeed);
901 +       device_remove_file(_dev, &dev_attr_hptxfsiz);
902 +       device_remove_file(_dev, &dev_attr_hprt0);      
903 +       device_remove_file(_dev, &dev_attr_remote_wakeup);      
904 +       device_remove_file(_dev, &dev_attr_regdump);
905 +       device_remove_file(_dev, &dev_attr_hcddump);
906 +       device_remove_file(_dev, &dev_attr_hcd_frrem);
907 +       device_remove_file(_dev, &dev_attr_rd_reg_test);
908 +       device_remove_file(_dev, &dev_attr_wr_reg_test);
909 +}
910 --- /dev/null
911 +++ b/drivers/usb/dwc_otg/dwc_otg_attr.h
912 @@ -0,0 +1,67 @@
913 +/* ==========================================================================
914 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_attr.h $
915 + * $Revision: 1.1.1.1 $
916 + * $Date: 2009-04-17 06:15:34 $
917 + * $Change: 510275 $
918 + *
919 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
920 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
921 + * otherwise expressly agreed to in writing between Synopsys and you.
922 + * 
923 + * The Software IS NOT an item of Licensed Software or Licensed Product under
924 + * any End User Software License Agreement or Agreement for Licensed Product
925 + * with Synopsys or any supplement thereto. You are permitted to use and
926 + * redistribute this Software in source and binary forms, with or without
927 + * modification, provided that redistributions of source code must retain this
928 + * notice. You may not view, use, disclose, copy or distribute this file or
929 + * any information contained herein except pursuant to this license grant from
930 + * Synopsys. If you do not agree with this notice, including the disclaimer
931 + * below, then you are not authorized to use the Software.
932 + * 
933 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
934 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
935 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
936 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
937 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
938 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
939 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
940 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
941 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
942 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
943 + * DAMAGE.
944 + * ========================================================================== */
945 +
946 +#if !defined(__DWC_OTG_ATTR_H__)
947 +#define __DWC_OTG_ATTR_H__
948 +
949 +/** @file
950 + * This file contains the interface to the Linux device attributes.
951 + */
952 +extern struct device_attribute dev_attr_regoffset;
953 +extern struct device_attribute dev_attr_regvalue;
954 +
955 +extern struct device_attribute dev_attr_mode;
956 +extern struct device_attribute dev_attr_hnpcapable;
957 +extern struct device_attribute dev_attr_srpcapable;
958 +extern struct device_attribute dev_attr_hnp;
959 +extern struct device_attribute dev_attr_srp;
960 +extern struct device_attribute dev_attr_buspower;
961 +extern struct device_attribute dev_attr_bussuspend;
962 +extern struct device_attribute dev_attr_busconnected;
963 +extern struct device_attribute dev_attr_gotgctl;
964 +extern struct device_attribute dev_attr_gusbcfg;
965 +extern struct device_attribute dev_attr_grxfsiz;
966 +extern struct device_attribute dev_attr_gnptxfsiz;
967 +extern struct device_attribute dev_attr_gpvndctl;
968 +extern struct device_attribute dev_attr_ggpio;
969 +extern struct device_attribute dev_attr_guid;
970 +extern struct device_attribute dev_attr_gsnpsid;
971 +extern struct device_attribute dev_attr_devspeed;
972 +extern struct device_attribute dev_attr_enumspeed;
973 +extern struct device_attribute dev_attr_hptxfsiz;
974 +extern struct device_attribute dev_attr_hprt0;
975 +
976 +void dwc_otg_attr_create (struct device *_dev);
977 +void dwc_otg_attr_remove (struct device *_dev);
978 +
979 +#endif
980 --- /dev/null
981 +++ b/drivers/usb/dwc_otg/dwc_otg_cil.c
982 @@ -0,0 +1,3025 @@
983 +/* ==========================================================================
984 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_cil.c $
985 + * $Revision: 1.1.1.1 $
986 + * $Date: 2009-04-17 06:15:34 $
987 + * $Change: 631780 $
988 + *
989 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
990 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
991 + * otherwise expressly agreed to in writing between Synopsys and you.
992 + * 
993 + * The Software IS NOT an item of Licensed Software or Licensed Product under
994 + * any End User Software License Agreement or Agreement for Licensed Product
995 + * with Synopsys or any supplement thereto. You are permitted to use and
996 + * redistribute this Software in source and binary forms, with or without
997 + * modification, provided that redistributions of source code must retain this
998 + * notice. You may not view, use, disclose, copy or distribute this file or
999 + * any information contained herein except pursuant to this license grant from
1000 + * Synopsys. If you do not agree with this notice, including the disclaimer
1001 + * below, then you are not authorized to use the Software.
1002 + * 
1003 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
1004 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1005 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1006 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
1007 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1008 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1009 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1010 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1011 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1012 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1013 + * DAMAGE.
1014 + * ========================================================================== */
1015 +
1016 +/** @file 
1017 + *
1018 + * The Core Interface Layer provides basic services for accessing and
1019 + * managing the DWC_otg hardware. These services are used by both the
1020 + * Host Controller Driver and the Peripheral Controller Driver.
1021 + *
1022 + * The CIL manages the memory map for the core so that the HCD and PCD
1023 + * don't have to do this separately. It also handles basic tasks like
1024 + * reading/writing the registers and data FIFOs in the controller.
1025 + * Some of the data access functions provide encapsulation of several
1026 + * operations required to perform a task, such as writing multiple
1027 + * registers to start a transfer. Finally, the CIL performs basic
1028 + * services that are not specific to either the host or device modes
1029 + * of operation. These services include management of the OTG Host
1030 + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
1031 + * Diagnostic API is also provided to allow testing of the controller
1032 + * hardware.
1033 + *
1034 + * The Core Interface Layer has the following requirements:
1035 + * - Provides basic controller operations.
1036 + * - Minimal use of OS services.  
1037 + * - The OS services used will be abstracted by using inline functions
1038 + *   or macros.
1039 + *
1040 + */
1041 +#include <asm/unaligned.h>
1042 +
1043 +#ifdef DEBUG
1044 +#include <linux/jiffies.h>
1045 +#endif
1046 +
1047 +#include "dwc_otg_plat.h"
1048 +
1049 +#include "dwc_otg_regs.h"
1050 +#include "dwc_otg_cil.h"
1051 +
1052 +/** 
1053 + * This function is called to initialize the DWC_otg CSR data
1054 + * structures.  The register addresses in the device and host
1055 + * structures are initialized from the base address supplied by the
1056 + * caller.  The calling function must make the OS calls to get the
1057 + * base address of the DWC_otg controller registers.  The core_params
1058 + * argument holds the parameters that specify how the core should be
1059 + * configured.
1060 + *
1061 + * @param[in] _reg_base_addr Base address of DWC_otg core registers
1062 + * @param[in] _core_params Pointer to the core configuration parameters 
1063 + *
1064 + */
1065 +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t *_reg_base_addr,
1066 +                                    dwc_otg_core_params_t *_core_params)
1067 +{
1068 +    dwc_otg_core_if_t *core_if = 0;
1069 +    dwc_otg_dev_if_t *dev_if = 0;
1070 +    dwc_otg_host_if_t *host_if = 0;
1071 +    uint8_t *reg_base = (uint8_t *)_reg_base_addr;
1072 +    int i = 0;
1073 +
1074 +    DWC_DEBUGPL(DBG_CILV, "%s(%p,%p)\n", __func__, _reg_base_addr, _core_params);
1075 +   
1076 +    core_if = kmalloc( sizeof(dwc_otg_core_if_t), GFP_KERNEL);
1077 +    if (core_if == 0) {
1078 +        DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_core_if_t failed\n");
1079 +        return 0;
1080 +    }
1081 +    memset(core_if, 0, sizeof(dwc_otg_core_if_t));
1082 +        
1083 +    core_if->core_params = _core_params;
1084 +    core_if->core_global_regs = (dwc_otg_core_global_regs_t *)reg_base;
1085 +    /*
1086 +     * Allocate the Device Mode structures.
1087 +     */
1088 +    dev_if = kmalloc( sizeof(dwc_otg_dev_if_t), GFP_KERNEL);
1089 +    if (dev_if == 0) {
1090 +        DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
1091 +        kfree( core_if );
1092 +        return 0;
1093 +    }
1094 +
1095 +    dev_if->dev_global_regs = 
1096 +        (dwc_otg_device_global_regs_t *)(reg_base + DWC_DEV_GLOBAL_REG_OFFSET);
1097 +        
1098 +    for (i=0; i<MAX_EPS_CHANNELS; i++) {
1099 +        dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
1100 +            (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
1101 +            (i * DWC_EP_REG_OFFSET));
1102 +                
1103 +        dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *) 
1104 +            (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
1105 +            (i * DWC_EP_REG_OFFSET));
1106 +        DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n", 
1107 +                            i, &dev_if->in_ep_regs[i]->diepctl);
1108 +        DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n", 
1109 +                            i, &dev_if->out_ep_regs[i]->doepctl);
1110 +    }
1111 +    dev_if->speed = 0; // unknown
1112 +    //dev_if->num_eps = MAX_EPS_CHANNELS;
1113 +    //dev_if->num_perio_eps = 0;
1114 +        
1115 +    core_if->dev_if = dev_if;
1116 +    /*
1117 +    * Allocate the Host Mode structures.
1118 +    */
1119 +    host_if = kmalloc( sizeof(dwc_otg_host_if_t), GFP_KERNEL);
1120 +    if (host_if == 0) {
1121 +        DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_host_if_t failed\n");
1122 +        kfree( dev_if );
1123 +        kfree( core_if );
1124 +        return 0;
1125 +    }
1126 +
1127 +    host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
1128 +        (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
1129 +    host_if->hprt0 = (uint32_t*)(reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
1130 +    for (i=0; i<MAX_EPS_CHANNELS; i++) {
1131 +        host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
1132 +            (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET + 
1133 +            (i * DWC_OTG_CHAN_REGS_OFFSET));
1134 +        DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n", 
1135 +                            i, &host_if->hc_regs[i]->hcchar);
1136 +    }
1137 +    host_if->num_host_channels = MAX_EPS_CHANNELS;
1138 +    core_if->host_if = host_if;
1139 +
1140 +    for (i=0; i<MAX_EPS_CHANNELS; i++) {
1141 +        core_if->data_fifo[i] = 
1142 +            (uint32_t *)(reg_base + DWC_OTG_DATA_FIFO_OFFSET + 
1143 +            (i * DWC_OTG_DATA_FIFO_SIZE)); 
1144 +        DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08x\n", 
1145 +            i, (unsigned)core_if->data_fifo[i]);
1146 +    } // for loop.
1147 +        
1148 +    core_if->pcgcctl = (uint32_t*)(reg_base + DWC_OTG_PCGCCTL_OFFSET);
1149 +
1150 +    /*
1151 +     * Store the contents of the hardware configuration registers here for
1152 +     * easy access later.
1153 +     */
1154 +    core_if->hwcfg1.d32 = dwc_read_reg32(&core_if->core_global_regs->ghwcfg1);
1155 +    core_if->hwcfg2.d32 = dwc_read_reg32(&core_if->core_global_regs->ghwcfg2);
1156 +    core_if->hwcfg3.d32 = dwc_read_reg32(&core_if->core_global_regs->ghwcfg3);
1157 +    core_if->hwcfg4.d32 = dwc_read_reg32(&core_if->core_global_regs->ghwcfg4);
1158 +
1159 +    DWC_DEBUGPL(DBG_CILV,"hwcfg1=%08x\n",core_if->hwcfg1.d32);
1160 +    DWC_DEBUGPL(DBG_CILV,"hwcfg2=%08x\n",core_if->hwcfg2.d32);
1161 +    DWC_DEBUGPL(DBG_CILV,"hwcfg3=%08x\n",core_if->hwcfg3.d32);
1162 +    DWC_DEBUGPL(DBG_CILV,"hwcfg4=%08x\n",core_if->hwcfg4.d32);
1163 +        
1164 +
1165 +    DWC_DEBUGPL(DBG_CILV,"op_mode=%0x\n",core_if->hwcfg2.b.op_mode);
1166 +    DWC_DEBUGPL(DBG_CILV,"arch=%0x\n",core_if->hwcfg2.b.architecture);
1167 +    DWC_DEBUGPL(DBG_CILV,"num_dev_ep=%d\n",core_if->hwcfg2.b.num_dev_ep);
1168 +    DWC_DEBUGPL(DBG_CILV,"num_host_chan=%d\n",core_if->hwcfg2.b.num_host_chan);
1169 +    DWC_DEBUGPL(DBG_CILV,"nonperio_tx_q_depth=0x%0x\n",core_if->hwcfg2.b.nonperio_tx_q_depth);
1170 +    DWC_DEBUGPL(DBG_CILV,"host_perio_tx_q_depth=0x%0x\n",core_if->hwcfg2.b.host_perio_tx_q_depth);
1171 +    DWC_DEBUGPL(DBG_CILV,"dev_token_q_depth=0x%0x\n",core_if->hwcfg2.b.dev_token_q_depth);
1172 +
1173 +    DWC_DEBUGPL(DBG_CILV,"Total FIFO SZ=%d\n", core_if->hwcfg3.b.dfifo_depth);
1174 +    DWC_DEBUGPL(DBG_CILV,"xfer_size_cntr_width=%0x\n", core_if->hwcfg3.b.xfer_size_cntr_width);
1175 +
1176 +    /*
1177 +     * Set the SRP sucess bit for FS-I2c
1178 +     */
1179 +    core_if->srp_success = 0;
1180 +    core_if->srp_timer_started = 0;
1181 +       
1182 +    return core_if;
1183 +}
1184 +/**
1185 + * This function frees the structures allocated by dwc_otg_cil_init().
1186 + * 
1187 + * @param[in] _core_if The core interface pointer returned from
1188 + * dwc_otg_cil_init().
1189 + *
1190 + */
1191 +void dwc_otg_cil_remove( dwc_otg_core_if_t *_core_if )
1192 +{
1193 +        /* Disable all interrupts */
1194 +        dwc_modify_reg32( &_core_if->core_global_regs->gahbcfg, 1, 0);
1195 +        dwc_write_reg32( &_core_if->core_global_regs->gintmsk, 0);
1196 +
1197 +        if ( _core_if->dev_if ) {
1198 +                kfree( _core_if->dev_if );
1199 +        }
1200 +        if ( _core_if->host_if ) {
1201 +                kfree( _core_if->host_if );
1202 +        }
1203 +        kfree( _core_if );
1204 +}
1205 +
1206 +/**
1207 + * This function enables the controller's Global Interrupt in the AHB Config
1208 + * register.
1209 + *
1210 + * @param[in] _core_if Programming view of DWC_otg controller.
1211 + */
1212 +extern void dwc_otg_enable_global_interrupts( dwc_otg_core_if_t *_core_if )
1213 +{
1214 +        gahbcfg_data_t ahbcfg = { .d32 = 0};
1215 +        ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
1216 +        dwc_modify_reg32(&_core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
1217 +}
1218 +/**
1219 + * This function disables the controller's Global Interrupt in the AHB Config
1220 + * register.
1221 + *
1222 + * @param[in] _core_if Programming view of DWC_otg controller.
1223 + */
1224 +extern void dwc_otg_disable_global_interrupts( dwc_otg_core_if_t *_core_if )
1225 +{
1226 +        gahbcfg_data_t ahbcfg = { .d32 = 0};
1227 +        ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
1228 +        dwc_modify_reg32(&_core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
1229 +}
1230 +
1231 +/**
1232 + * This function initializes the commmon interrupts, used in both
1233 + * device and host modes.
1234 + *
1235 + * @param[in] _core_if Programming view of the DWC_otg controller
1236 + *
1237 + */
1238 +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t *_core_if)
1239 +{
1240 +        dwc_otg_core_global_regs_t *global_regs = 
1241 +                _core_if->core_global_regs;
1242 +        gintmsk_data_t intr_mask = { .d32 = 0};
1243 +        /* Clear any pending OTG Interrupts */
1244 +        dwc_write_reg32( &global_regs->gotgint, 0xFFFFFFFF); 
1245 +        /* Clear any pending interrupts */
1246 +        dwc_write_reg32( &global_regs->gintsts, 0xFFFFFFFF); 
1247 +        /* 
1248 +         * Enable the interrupts in the GINTMSK. 
1249 +         */
1250 +        intr_mask.b.modemismatch = 1;
1251 +        intr_mask.b.otgintr = 1;
1252 +        if (!_core_if->dma_enable) {
1253 +                intr_mask.b.rxstsqlvl = 1;
1254 +        }
1255 +        intr_mask.b.conidstschng = 1;
1256 +        intr_mask.b.wkupintr = 1;
1257 +        intr_mask.b.disconnect = 1;
1258 +        intr_mask.b.usbsuspend = 1;
1259 +       intr_mask.b.sessreqintr = 1;
1260 +        dwc_write_reg32( &global_regs->gintmsk, intr_mask.d32);
1261 +}
1262 +
1263 +/**
1264 + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
1265 + * type.
1266 + */
1267 +static void init_fslspclksel(dwc_otg_core_if_t *_core_if)
1268 +{
1269 +       uint32_t        val;
1270 +       hcfg_data_t     hcfg;
1271 +
1272 +       if (((_core_if->hwcfg2.b.hs_phy_type == 2) &&
1273 +            (_core_if->hwcfg2.b.fs_phy_type == 1) &&
1274 +            (_core_if->core_params->ulpi_fs_ls)) ||
1275 +           (_core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS))
1276 +       {
1277 +               /* Full speed PHY */
1278 +               val = DWC_HCFG_48_MHZ;
1279 +       } else {
1280 +               /* High speed PHY running at full speed or high speed */
1281 +               val = DWC_HCFG_30_60_MHZ;
1282 +       }
1283 +
1284 +       DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
1285 +       hcfg.d32 = dwc_read_reg32(&_core_if->host_if->host_global_regs->hcfg);
1286 +       hcfg.b.fslspclksel = val;
1287 +       dwc_write_reg32(&_core_if->host_if->host_global_regs->hcfg, hcfg.d32);
1288 +}
1289 +
1290 +/**
1291 + * Initializes the DevSpd field of the DCFG register depending on the PHY type
1292 + * and the enumeration speed of the device.
1293 + */
1294 +static void init_devspd(dwc_otg_core_if_t *_core_if)
1295 +{
1296 +       uint32_t        val;
1297 +       dcfg_data_t     dcfg;
1298 +
1299 +       if (((_core_if->hwcfg2.b.hs_phy_type == 2) &&
1300 +            (_core_if->hwcfg2.b.fs_phy_type == 1) &&
1301 +            (_core_if->core_params->ulpi_fs_ls)) ||
1302 +            (_core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) 
1303 +       {
1304 +               /* Full speed PHY */
1305 +               val = 0x3;
1306 +       } else if (_core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
1307 +               /* High speed PHY running at full speed */
1308 +               val = 0x1;
1309 +       } else {
1310 +               /* High speed PHY running at high speed */
1311 +               val = 0x0;
1312 +       }
1313 +
1314 +       DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
1315 +       dcfg.d32 = dwc_read_reg32(&_core_if->dev_if->dev_global_regs->dcfg);
1316 +       dcfg.b.devspd = val;
1317 +       dwc_write_reg32(&_core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
1318 +}
1319 +
1320 +/**
1321 + * This function calculates the number of IN EPS
1322 + * using GHWCFG1 and GHWCFG2 registers values
1323 + *
1324 + * @param _pcd the pcd structure.
1325 + */
1326 +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * _core_if)
1327 +{
1328 +       uint32_t num_in_eps = 0;
1329 +       uint32_t num_eps = _core_if->hwcfg2.b.num_dev_ep;
1330 +       uint32_t hwcfg1 = _core_if->hwcfg1.d32 >> 2;
1331 +       uint32_t num_tx_fifos = _core_if->hwcfg4.b.num_in_eps;
1332 +       int i;
1333 +       for (i = 0; i < num_eps; ++i) {
1334 +               if (!(hwcfg1 & 0x1))
1335 +                       num_in_eps++;
1336 +               hwcfg1 >>= 2;
1337 +       }
1338 +       if (_core_if->hwcfg4.b.ded_fifo_en) {
1339 +               num_in_eps = (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
1340 +       }
1341 +       return num_in_eps;
1342 +}
1343 +
1344 +
1345 +/**
1346 + * This function calculates the number of OUT EPS
1347 + * using GHWCFG1 and GHWCFG2 registers values
1348 + *
1349 + * @param _pcd the pcd structure.
1350 + */
1351 +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * _core_if)
1352 +{
1353 +       uint32_t num_out_eps = 0;
1354 +       uint32_t num_eps = _core_if->hwcfg2.b.num_dev_ep;
1355 +       uint32_t hwcfg1 = _core_if->hwcfg1.d32 >> 2;
1356 +       int i;
1357 +       for (i = 0; i < num_eps; ++i) {
1358 +               if (!(hwcfg1 & 0x2))
1359 +                       num_out_eps++;
1360 +               hwcfg1 >>= 2;
1361 +       }
1362 +       return num_out_eps;
1363 +}
1364 +/**
1365 + * This function initializes the DWC_otg controller registers and
1366 + * prepares the core for device mode or host mode operation.
1367 + *
1368 + * @param _core_if Programming view of the DWC_otg controller
1369 + *
1370 + */
1371 +void dwc_otg_core_init(dwc_otg_core_if_t *_core_if) 
1372 +{
1373 +       dwc_otg_core_global_regs_t * global_regs = _core_if->core_global_regs;
1374 +    dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
1375 +    int i = 0;
1376 +    gahbcfg_data_t ahbcfg = { .d32 = 0};
1377 +    gusbcfg_data_t usbcfg = { .d32 = 0 };
1378 +    gi2cctl_data_t i2cctl = {.d32 = 0};
1379 +
1380 +    DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p)\n",_core_if);
1381 +
1382 +    /* Common Initialization */
1383 +
1384 +    usbcfg.d32 = dwc_read_reg32(&global_regs->gusbcfg);
1385 +       DWC_DEBUGPL(DBG_CIL, "USB config register: 0x%08x\n", usbcfg.d32);
1386 +
1387 +    /* Program the ULPI External VBUS bit if needed */
1388 +    //usbcfg.b.ulpi_ext_vbus_drv = 1;
1389 +    //usbcfg.b.ulpi_ext_vbus_drv = 0;
1390 +    usbcfg.b.ulpi_ext_vbus_drv = 
1391 +        (_core_if->core_params->phy_ulpi_ext_vbus == DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
1392 +
1393 +    /* Set external TS Dline pulsing */
1394 +    usbcfg.b.term_sel_dl_pulse = (_core_if->core_params->ts_dline == 1) ? 1 : 0;
1395 +    dwc_write_reg32 (&global_regs->gusbcfg, usbcfg.d32);
1396 +
1397 +    /* Reset the Controller */
1398 +    dwc_otg_core_reset( _core_if );
1399 +
1400 +    /* Initialize parameters from Hardware configuration registers. */
1401 +#if 0
1402 +    dev_if->num_eps = _core_if->hwcfg2.b.num_dev_ep;
1403 +    dev_if->num_perio_eps = _core_if->hwcfg4.b.num_dev_perio_in_ep;
1404 +#else
1405 +       dev_if->num_in_eps = calc_num_in_eps(_core_if);
1406 +       dev_if->num_out_eps = calc_num_out_eps(_core_if);
1407 +#endif        
1408 +       DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
1409 +                      _core_if->hwcfg4.b.num_dev_perio_in_ep);
1410 +       DWC_DEBUGPL(DBG_CIL, "Is power optimization enabled?  %s\n",
1411 +                    _core_if->hwcfg4.b.power_optimiz ? "Yes" : "No");
1412 +       DWC_DEBUGPL(DBG_CIL, "vbus_valid filter enabled?  %s\n",
1413 +                    _core_if->hwcfg4.b.vbus_valid_filt_en ? "Yes" : "No");
1414 +       DWC_DEBUGPL(DBG_CIL, "iddig filter enabled?  %s\n",
1415 +                    _core_if->hwcfg4.b.iddig_filt_en ? "Yes" : "No");
1416 +
1417 +    DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",_core_if->hwcfg4.b.num_dev_perio_in_ep);
1418 +    for (i=0; i < _core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
1419 +        dev_if->perio_tx_fifo_size[i] =
1420 +                   dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i]) >> 16;
1421 +               DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n", i,
1422 +                            dev_if->perio_tx_fifo_size[i]);
1423 +       }
1424 +       for (i = 0; i < _core_if->hwcfg4.b.num_in_eps; i++) {
1425 +               dev_if->tx_fifo_size[i] =
1426 +                   dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i]) >> 16;
1427 +               DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n", i,
1428 +                            dev_if->perio_tx_fifo_size[i]);
1429 +       }
1430 +        
1431 +    _core_if->total_fifo_size = _core_if->hwcfg3.b.dfifo_depth;
1432 +       _core_if->rx_fifo_size = dwc_read_reg32(&global_regs->grxfsiz);
1433 +       _core_if->nperio_tx_fifo_size = dwc_read_reg32(&global_regs->gnptxfsiz) >> 16;
1434 +        
1435 +    DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", _core_if->total_fifo_size);
1436 +    DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", _core_if->rx_fifo_size);
1437 +    DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n", _core_if->nperio_tx_fifo_size);
1438 +
1439 +    /* This programming sequence needs to happen in FS mode before any other
1440 +    * programming occurs */
1441 +    if ((_core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
1442 +        (_core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
1443 +        /* If FS mode with FS PHY */
1444 +
1445 +        /* core_init() is now called on every switch so only call the
1446 +         * following for the first time through. */
1447 +        if (!_core_if->phy_init_done) {
1448 +            _core_if->phy_init_done = 1;
1449 +            DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
1450 +            usbcfg.d32 = dwc_read_reg32(&global_regs->gusbcfg);
1451 +            usbcfg.b.physel = 1;
1452 +            dwc_write_reg32 (&global_regs->gusbcfg, usbcfg.d32);
1453 +
1454 +            /* Reset after a PHY select */
1455 +            dwc_otg_core_reset( _core_if );
1456 +        }
1457 +
1458 +        /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.  Also
1459 +         * do this on HNP Dev/Host mode switches (done in dev_init and
1460 +         * host_init). */
1461 +        if (dwc_otg_is_host_mode(_core_if)) {
1462 +                       DWC_DEBUGPL(DBG_CIL, "host mode\n");
1463 +            init_fslspclksel(_core_if);
1464 +               } else {
1465 +                       DWC_DEBUGPL(DBG_CIL, "device mode\n");
1466 +            init_devspd(_core_if);
1467 +        }
1468 +
1469 +        if (_core_if->core_params->i2c_enable) {
1470 +            DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
1471 +            /* Program GUSBCFG.OtgUtmifsSel to I2C */
1472 +            usbcfg.d32 = dwc_read_reg32(&global_regs->gusbcfg);
1473 +            usbcfg.b.otgutmifssel = 1;
1474 +            dwc_write_reg32 (&global_regs->gusbcfg, usbcfg.d32);
1475 +                               
1476 +            /* Program GI2CCTL.I2CEn */
1477 +            i2cctl.d32 = dwc_read_reg32(&global_regs->gi2cctl);
1478 +            i2cctl.b.i2cdevaddr = 1;
1479 +            i2cctl.b.i2cen = 0;
1480 +            dwc_write_reg32 (&global_regs->gi2cctl, i2cctl.d32);
1481 +            i2cctl.b.i2cen = 1;
1482 +            dwc_write_reg32 (&global_regs->gi2cctl, i2cctl.d32);
1483 +        }
1484 +
1485 +    } /* endif speed == DWC_SPEED_PARAM_FULL */
1486 +       else {
1487 +        /* High speed PHY. */
1488 +        if (!_core_if->phy_init_done) {
1489 +            _core_if->phy_init_done = 1;
1490 +                       DWC_DEBUGPL(DBG_CIL, "High spped PHY\n");
1491 +            /* HS PHY parameters.  These parameters are preserved
1492 +             * during soft reset so only program the first time.  Do
1493 +             * a soft reset immediately after setting phyif.  */
1494 +            usbcfg.b.ulpi_utmi_sel = _core_if->core_params->phy_type;
1495 +            if (usbcfg.b.ulpi_utmi_sel == 2) { // winder
1496 +                               DWC_DEBUGPL(DBG_CIL, "ULPI\n");
1497 +                /* ULPI interface */
1498 +                usbcfg.b.phyif = 0;
1499 +                usbcfg.b.ddrsel = _core_if->core_params->phy_ulpi_ddr;
1500 +            } else {
1501 +                /* UTMI+ interface */
1502 +                if (_core_if->core_params->phy_utmi_width == 16) {
1503 +                    usbcfg.b.phyif = 1;
1504 +                                       DWC_DEBUGPL(DBG_CIL, "UTMI+ 16\n");
1505 +                               } else {
1506 +                                       DWC_DEBUGPL(DBG_CIL, "UTMI+ 8\n");
1507 +                    usbcfg.b.phyif = 0;
1508 +                }
1509 +            }
1510 +            dwc_write_reg32( &global_regs->gusbcfg, usbcfg.d32);
1511 +
1512 +            /* Reset after setting the PHY parameters */
1513 +            dwc_otg_core_reset( _core_if );
1514 +        }
1515 +    }
1516 +
1517 +    if ((_core_if->hwcfg2.b.hs_phy_type == 2) &&
1518 +        (_core_if->hwcfg2.b.fs_phy_type == 1) &&
1519 +        (_core_if->core_params->ulpi_fs_ls)) 
1520 +    {
1521 +        DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
1522 +        usbcfg.d32 = dwc_read_reg32(&global_regs->gusbcfg);
1523 +        usbcfg.b.ulpi_fsls = 1;
1524 +        usbcfg.b.ulpi_clk_sus_m = 1;
1525 +        dwc_write_reg32(&global_regs->gusbcfg, usbcfg.d32);
1526 +       } else {
1527 +               DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS=0\n");
1528 +        usbcfg.d32 = dwc_read_reg32(&global_regs->gusbcfg);
1529 +        usbcfg.b.ulpi_fsls = 0;
1530 +        usbcfg.b.ulpi_clk_sus_m = 0;
1531 +        dwc_write_reg32(&global_regs->gusbcfg, usbcfg.d32);
1532 +    }
1533 +
1534 +    /* Program the GAHBCFG Register.*/
1535 +    switch (_core_if->hwcfg2.b.architecture){
1536 +
1537 +        case DWC_SLAVE_ONLY_ARCH:
1538 +            DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
1539 +            ahbcfg.b.nptxfemplvl_txfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
1540 +            ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
1541 +            _core_if->dma_enable = 0;
1542 +            break;
1543 +
1544 +        case DWC_EXT_DMA_ARCH:
1545 +            DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
1546 +            ahbcfg.b.hburstlen = _core_if->core_params->dma_burst_size; 
1547 +            _core_if->dma_enable = (_core_if->core_params->dma_enable != 0);
1548 +            break;
1549 +
1550 +        case DWC_INT_DMA_ARCH:
1551 +            DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
1552 +            //ahbcfg.b.hburstlen = DWC_GAHBCFG_INT_DMA_BURST_INCR;
1553 +            ahbcfg.b.hburstlen = DWC_GAHBCFG_INT_DMA_BURST_INCR4;
1554 +            _core_if->dma_enable = (_core_if->core_params->dma_enable != 0);
1555 +            break;
1556 +    }
1557 +    ahbcfg.b.dmaenable = _core_if->dma_enable;
1558 +    dwc_write_reg32(&global_regs->gahbcfg, ahbcfg.d32);
1559 +       _core_if->en_multiple_tx_fifo = _core_if->hwcfg4.b.ded_fifo_en;
1560 +
1561 +    /* 
1562 +     * Program the GUSBCFG register. 
1563 +     */
1564 +    usbcfg.d32 = dwc_read_reg32( &global_regs->gusbcfg );
1565 +
1566 +    switch (_core_if->hwcfg2.b.op_mode) {
1567 +        case DWC_MODE_HNP_SRP_CAPABLE:
1568 +            usbcfg.b.hnpcap = (_core_if->core_params->otg_cap ==
1569 +            DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
1570 +            usbcfg.b.srpcap = (_core_if->core_params->otg_cap !=
1571 +            DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
1572 +            break;
1573 +
1574 +        case DWC_MODE_SRP_ONLY_CAPABLE:
1575 +            usbcfg.b.hnpcap = 0;
1576 +            usbcfg.b.srpcap = (_core_if->core_params->otg_cap !=
1577 +            DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
1578 +            break;
1579 +
1580 +        case DWC_MODE_NO_HNP_SRP_CAPABLE:
1581 +            usbcfg.b.hnpcap = 0;
1582 +            usbcfg.b.srpcap = 0;
1583 +            break;
1584 +
1585 +        case DWC_MODE_SRP_CAPABLE_DEVICE:
1586 +            usbcfg.b.hnpcap = 0;
1587 +            usbcfg.b.srpcap = (_core_if->core_params->otg_cap !=
1588 +            DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
1589 +            break;
1590 +
1591 +        case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
1592 +            usbcfg.b.hnpcap = 0;
1593 +            usbcfg.b.srpcap = 0;
1594 +            break;
1595 +
1596 +        case DWC_MODE_SRP_CAPABLE_HOST:
1597 +            usbcfg.b.hnpcap = 0;
1598 +            usbcfg.b.srpcap = (_core_if->core_params->otg_cap !=
1599 +            DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
1600 +            break;
1601 +
1602 +        case DWC_MODE_NO_SRP_CAPABLE_HOST:
1603 +            usbcfg.b.hnpcap = 0;
1604 +            usbcfg.b.srpcap = 0;
1605 +            break;
1606 +    }
1607 +
1608 +    dwc_write_reg32( &global_regs->gusbcfg, usbcfg.d32);
1609 +        
1610 +    /* Enable common interrupts */
1611 +    dwc_otg_enable_common_interrupts( _core_if );
1612 +
1613 +    /* Do device or host intialization based on mode during PCD
1614 +     * and HCD initialization  */
1615 +    if (dwc_otg_is_host_mode( _core_if )) {
1616 +        DWC_DEBUGPL(DBG_ANY, "Host Mode\n" );
1617 +        _core_if->op_state = A_HOST;
1618 +    } else {
1619 +        DWC_DEBUGPL(DBG_ANY, "Device Mode\n" );
1620 +        _core_if->op_state = B_PERIPHERAL;
1621 +#ifdef DWC_DEVICE_ONLY
1622 +        dwc_otg_core_dev_init( _core_if );
1623 +#endif
1624 +    }
1625 +}
1626 +
1627 +
1628 +/** 
1629 + * This function enables the Device mode interrupts.
1630 + *
1631 + * @param _core_if Programming view of DWC_otg controller
1632 + */
1633 +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t *_core_if)
1634 +{
1635 +        gintmsk_data_t intr_mask = { .d32 = 0};
1636 +       dwc_otg_core_global_regs_t * global_regs = _core_if->core_global_regs;
1637 +
1638 +        DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
1639 +
1640 +        /* Disable all interrupts. */
1641 +        dwc_write_reg32( &global_regs->gintmsk, 0);
1642 +
1643 +        /* Clear any pending interrupts */
1644 +        dwc_write_reg32( &global_regs->gintsts, 0xFFFFFFFF); 
1645 +
1646 +        /* Enable the common interrupts */
1647 +        dwc_otg_enable_common_interrupts( _core_if );
1648 +
1649 +        /* Enable interrupts */
1650 +        intr_mask.b.usbreset = 1;
1651 +        intr_mask.b.enumdone = 1;
1652 +        //intr_mask.b.epmismatch = 1;
1653 +        intr_mask.b.inepintr = 1;
1654 +        intr_mask.b.outepintr = 1;
1655 +        intr_mask.b.erlysuspend = 1;
1656 +       if (_core_if->en_multiple_tx_fifo == 0) {
1657 +               intr_mask.b.epmismatch = 1;
1658 +       }
1659 +
1660 +        /** @todo NGS: Should this be a module parameter? */
1661 +        intr_mask.b.isooutdrop = 1;
1662 +        intr_mask.b.eopframe = 1;
1663 +        intr_mask.b.incomplisoin = 1;
1664 +        intr_mask.b.incomplisoout = 1;
1665 +
1666 +        dwc_modify_reg32( &global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
1667 +
1668 +        DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__, 
1669 +                    dwc_read_reg32( &global_regs->gintmsk));
1670 +}
1671 +
1672 +/**
1673 + * This function initializes the DWC_otg controller registers for
1674 + * device mode.
1675 + * 
1676 + * @param _core_if Programming view of DWC_otg controller
1677 + *
1678 + */
1679 +void dwc_otg_core_dev_init(dwc_otg_core_if_t *_core_if)
1680 +{
1681 +        dwc_otg_core_global_regs_t *global_regs = 
1682 +                _core_if->core_global_regs;
1683 +        dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
1684 +        dwc_otg_core_params_t *params = _core_if->core_params;
1685 +        dcfg_data_t dcfg = {.d32 = 0};
1686 +        grstctl_t resetctl = { .d32=0 };
1687 +        int i;
1688 +        uint32_t rx_fifo_size;
1689 +        fifosize_data_t nptxfifosize;
1690 +       fifosize_data_t txfifosize;
1691 +       dthrctl_data_t dthrctl;
1692 +
1693 +        fifosize_data_t ptxfifosize;
1694 +        
1695 +        /* Restart the Phy Clock */
1696 +        dwc_write_reg32(_core_if->pcgcctl, 0);
1697 +        
1698 +        /* Device configuration register */
1699 +       init_devspd(_core_if);
1700 +       dcfg.d32 = dwc_read_reg32( &dev_if->dev_global_regs->dcfg);
1701 +        dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
1702 +        dwc_write_reg32( &dev_if->dev_global_regs->dcfg, dcfg.d32 );
1703 +
1704 +        /* Configure data FIFO sizes */
1705 +        if ( _core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo ) {
1706 +                
1707 +                DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n", _core_if->total_fifo_size);
1708 +                DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n", params->dev_rx_fifo_size);
1709 +                DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n", params->dev_nperio_tx_fifo_size);
1710 +
1711 +               /* Rx FIFO */
1712 +                DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n", 
1713 +                            dwc_read_reg32(&global_regs->grxfsiz));
1714 +                rx_fifo_size = params->dev_rx_fifo_size;
1715 +                dwc_write_reg32( &global_regs->grxfsiz, rx_fifo_size );
1716 +                DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n", 
1717 +                            dwc_read_reg32(&global_regs->grxfsiz));
1718 +
1719 +               /** Set Periodic Tx FIFO Mask all bits 0 */
1720 +           _core_if->p_tx_msk = 0;
1721 +
1722 +               /** Set Tx FIFO Mask all bits 0 */
1723 +           _core_if->tx_msk = 0;
1724 +               if (_core_if->en_multiple_tx_fifo == 0) {
1725 +               /* Non-periodic Tx FIFO */
1726 +                DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n", 
1727 +                            dwc_read_reg32(&global_regs->gnptxfsiz));
1728 +                nptxfifosize.b.depth  = params->dev_nperio_tx_fifo_size;
1729 +                nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
1730 +                dwc_write_reg32( &global_regs->gnptxfsiz, nptxfifosize.d32 );
1731 +                DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n", 
1732 +                            dwc_read_reg32(&global_regs->gnptxfsiz));
1733 +
1734 +
1735 +                /**@todo NGS: Fix Periodic FIFO Sizing! */
1736 +               /*
1737 +                * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
1738 +                * Indexes of the FIFO size module parameters in the
1739 +                * dev_perio_tx_fifo_size array and the FIFO size registers in
1740 +                * the dptxfsiz array run from 0 to 14.
1741 +                */
1742 +                /** @todo Finish debug of this */   
1743 +                   ptxfifosize.b.startaddr =
1744 +                   nptxfifosize.b.startaddr + nptxfifosize.b.depth;
1745 +                       for (i = 0; i < _core_if->hwcfg4.b.num_dev_perio_in_ep;i++) {
1746 +                       ptxfifosize.b.depth = params->dev_perio_tx_fifo_size[i];
1747 +                               DWC_DEBUGPL(DBG_CIL,"initial dptxfsiz_dieptxf[%d]=%08x\n",
1748 +                                    i,dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i]));
1749 +                               dwc_write_reg32(&global_regs->dptxfsiz_dieptxf[i],ptxfifosize.d32);
1750 +                               DWC_DEBUGPL(DBG_CIL,"new dptxfsiz_dieptxf[%d]=%08x\n",
1751 +                                    i,dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i]));
1752 +                        ptxfifosize.b.startaddr += ptxfifosize.b.depth;
1753 +                }
1754 +               } else {
1755 +
1756 +                   /*
1757 +                    * Tx FIFOs These FIFOs are numbered from 1 to 15.
1758 +                    * Indexes of the FIFO size module parameters in the
1759 +                    * dev_tx_fifo_size array and the FIFO size registers in
1760 +                    * the dptxfsiz_dieptxf array run from 0 to 14.
1761 +                    */
1762 +
1763 +                   /* Non-periodic Tx FIFO */
1764 +                   DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
1765 +                               dwc_read_reg32(&global_regs->gnptxfsiz));
1766 +                       nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
1767 +                       nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
1768 +                       dwc_write_reg32(&global_regs->gnptxfsiz, nptxfifosize.d32);
1769 +                       DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
1770 +                                     dwc_read_reg32(&global_regs->gnptxfsiz));
1771 +                       txfifosize.b.startaddr = nptxfifosize.b.startaddr + nptxfifosize.b.depth;
1772 +                       for (i = 1;i < _core_if->hwcfg4.b.num_dev_perio_in_ep;i++) {
1773 +                               txfifosize.b.depth = params->dev_tx_fifo_size[i];
1774 +                               DWC_DEBUGPL(DBG_CIL,"initial dptxfsiz_dieptxf[%d]=%08x\n",
1775 +                                     i,dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i]));
1776 +                               dwc_write_reg32(&global_regs->dptxfsiz_dieptxf[i - 1],txfifosize.d32);
1777 +                               DWC_DEBUGPL(DBG_CIL,"new dptxfsiz_dieptxf[%d]=%08x\n",
1778 +                                     i,dwc_read_reg32(&global_regs->dptxfsiz_dieptxf[i-1]));
1779 +                               txfifosize.b.startaddr += txfifosize.b.depth;
1780 +        }
1781 +               }
1782 +       }
1783 +        /* Flush the FIFOs */
1784 +        dwc_otg_flush_tx_fifo(_core_if, 0x10); /* all Tx FIFOs */
1785 +        dwc_otg_flush_rx_fifo(_core_if);
1786 +
1787 +       /* Flush the Learning Queue. */
1788 +       resetctl.b.intknqflsh = 1;
1789 +        dwc_write_reg32( &_core_if->core_global_regs->grstctl, resetctl.d32);
1790 +
1791 +        /* Clear all pending Device Interrupts */
1792 +        dwc_write_reg32( &dev_if->dev_global_regs->diepmsk, 0 );
1793 +        dwc_write_reg32( &dev_if->dev_global_regs->doepmsk, 0 );
1794 +        dwc_write_reg32( &dev_if->dev_global_regs->daint, 0xFFFFFFFF );
1795 +        dwc_write_reg32( &dev_if->dev_global_regs->daintmsk, 0 );
1796 +
1797 +       for (i = 0; i <= dev_if->num_in_eps; i++) {
1798 +               depctl_data_t depctl;
1799 +               depctl.d32 = dwc_read_reg32(&dev_if->in_ep_regs[i]->diepctl);
1800 +               if (depctl.b.epena) {
1801 +                       depctl.d32 = 0;
1802 +                       depctl.b.epdis = 1;
1803 +                       depctl.b.snak = 1;
1804 +               } else {
1805 +                       depctl.d32 = 0;
1806 +               }
1807 +               dwc_write_reg32( &dev_if->in_ep_regs[i]->diepctl, depctl.d32);
1808 +
1809 +               dwc_write_reg32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
1810 +               dwc_write_reg32(&dev_if->in_ep_regs[i]->diepdma, 0);
1811 +               dwc_write_reg32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
1812 +       }
1813 +       for (i = 0; i <= dev_if->num_out_eps; i++) {
1814 +               depctl_data_t depctl;
1815 +               depctl.d32 = dwc_read_reg32(&dev_if->out_ep_regs[i]->doepctl);
1816 +               if (depctl.b.epena) {
1817 +                       depctl.d32 = 0;
1818 +                       depctl.b.epdis = 1;
1819 +                       depctl.b.snak = 1;
1820 +               } else {
1821 +                       depctl.d32 = 0;
1822 +               }
1823 +               dwc_write_reg32( &dev_if->out_ep_regs[i]->doepctl, depctl.d32);
1824 +
1825 +                //dwc_write_reg32( &dev_if->in_ep_regs[i]->dieptsiz, 0);
1826 +                dwc_write_reg32( &dev_if->out_ep_regs[i]->doeptsiz, 0);
1827 +                //dwc_write_reg32( &dev_if->in_ep_regs[i]->diepdma, 0);
1828 +                dwc_write_reg32( &dev_if->out_ep_regs[i]->doepdma, 0);
1829 +                //dwc_write_reg32( &dev_if->in_ep_regs[i]->diepint, 0xFF);
1830 +                dwc_write_reg32( &dev_if->out_ep_regs[i]->doepint, 0xFF);
1831 +        }
1832 +        
1833 +       if (_core_if->en_multiple_tx_fifo && _core_if->dma_enable) {
1834 +               dev_if->non_iso_tx_thr_en = _core_if->core_params->thr_ctl & 0x1;
1835 +               dev_if->iso_tx_thr_en = (_core_if->core_params->thr_ctl >> 1) & 0x1;
1836 +               dev_if->rx_thr_en = (_core_if->core_params->thr_ctl >> 2) & 0x1;
1837 +               dev_if->rx_thr_length = _core_if->core_params->rx_thr_length;
1838 +               dev_if->tx_thr_length = _core_if->core_params->tx_thr_length;
1839 +               dthrctl.d32 = 0;
1840 +               dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
1841 +               dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
1842 +               dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
1843 +               dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
1844 +               dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
1845 +               dwc_write_reg32(&dev_if->dev_global_regs->dtknqr3_dthrctl,dthrctl.d32);
1846 +               DWC_DEBUGPL(DBG_CIL, "Non ISO Tx Thr - %d\nISO Tx Thr - %d\n"
1847 +                                       "Rx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
1848 +                                       dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
1849 +                                       dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
1850 +                                       dthrctl.b.rx_thr_len);
1851 +       }
1852 +        dwc_otg_enable_device_interrupts( _core_if );        
1853 +       {
1854 +               diepmsk_data_t msk = {.d32 = 0};
1855 +               msk.b.txfifoundrn = 1;
1856 +               dwc_modify_reg32(&dev_if->dev_global_regs->diepmsk, msk.d32,msk.d32);
1857 +}
1858 +}
1859 +
1860 +/** 
1861 + * This function enables the Host mode interrupts.
1862 + *
1863 + * @param _core_if Programming view of DWC_otg controller
1864 + */
1865 +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t *_core_if)
1866 +{
1867 +        dwc_otg_core_global_regs_t *global_regs = _core_if->core_global_regs;
1868 +       gintmsk_data_t intr_mask = {.d32 = 0};
1869 +
1870 +        DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
1871 +
1872 +        /* Disable all interrupts. */
1873 +        dwc_write_reg32(&global_regs->gintmsk, 0);
1874 +
1875 +        /* Clear any pending interrupts. */
1876 +        dwc_write_reg32(&global_regs->gintsts, 0xFFFFFFFF); 
1877 +
1878 +        /* Enable the common interrupts */
1879 +        dwc_otg_enable_common_interrupts(_core_if);
1880 +
1881 +       /*
1882 +        * Enable host mode interrupts without disturbing common
1883 +        * interrupts.
1884 +        */
1885 +       intr_mask.b.sofintr = 1;
1886 +       intr_mask.b.portintr = 1;
1887 +       intr_mask.b.hcintr = 1;
1888 +
1889 +        //dwc_modify_reg32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
1890 +        //dwc_modify_reg32(&global_regs->gintmsk, 0, intr_mask.d32);
1891 +       dwc_modify_reg32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
1892 +}
1893 +
1894 +/** 
1895 + * This function disables the Host Mode interrupts.
1896 + *
1897 + * @param _core_if Programming view of DWC_otg controller
1898 + */
1899 +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t *_core_if)
1900 +{
1901 +        dwc_otg_core_global_regs_t *global_regs =
1902 +               _core_if->core_global_regs;
1903 +       gintmsk_data_t intr_mask = {.d32 = 0};
1904 +
1905 +        DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
1906 +         
1907 +       /*
1908 +        * Disable host mode interrupts without disturbing common
1909 +        * interrupts.
1910 +        */
1911 +       intr_mask.b.sofintr = 1;
1912 +       intr_mask.b.portintr = 1;
1913 +       intr_mask.b.hcintr = 1;
1914 +        intr_mask.b.ptxfempty = 1;
1915 +       intr_mask.b.nptxfempty = 1;
1916 +        
1917 +        dwc_modify_reg32(&global_regs->gintmsk, intr_mask.d32, 0);
1918 +}
1919 +
1920 +#if 1
1921 +/* currently not used, keep it here as if needed later */
1922 +static int phy_read(dwc_otg_core_if_t * _core_if, int addr)
1923 +{
1924 +       u32 val;
1925 +       int timeout = 10;
1926 +
1927 +       dwc_write_reg32(&_core_if->core_global_regs->gpvndctl,
1928 +                       0x02000000 | (addr << 16));
1929 +       val = dwc_read_reg32(&_core_if->core_global_regs->gpvndctl);
1930 +       while (((val & 0x08000000) == 0) && (timeout--)) {
1931 +               udelay(1000);
1932 +               val = dwc_read_reg32(&_core_if->core_global_regs->gpvndctl);
1933 +       }
1934 +       val = dwc_read_reg32(&_core_if->core_global_regs->gpvndctl);
1935 +       printk("%s: addr=%02x regval=%02x\n", __func__, addr, val & 0x000000ff);
1936 +
1937 +       return 0;
1938 +}
1939 +#endif
1940 +
1941 +/**
1942 + * This function initializes the DWC_otg controller registers for
1943 + * host mode.
1944 + *
1945 + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
1946 + * request queues. Host channels are reset to ensure that they are ready for
1947 + * performing transfers.
1948 + *
1949 + * @param _core_if Programming view of DWC_otg controller
1950 + *
1951 + */
1952 +void dwc_otg_core_host_init(dwc_otg_core_if_t *_core_if)
1953 +{
1954 +        dwc_otg_core_global_regs_t *global_regs = _core_if->core_global_regs;
1955 +       dwc_otg_host_if_t       *host_if = _core_if->host_if;
1956 +        dwc_otg_core_params_t  *params = _core_if->core_params;
1957 +       hprt0_data_t            hprt0 = {.d32 = 0};
1958 +        fifosize_data_t        nptxfifosize;
1959 +        fifosize_data_t        ptxfifosize;
1960 +       int                     i;
1961 +       hcchar_data_t           hcchar;
1962 +       hcfg_data_t             hcfg;
1963 +       dwc_otg_hc_regs_t       *hc_regs;
1964 +       int                     num_channels;
1965 +       gotgctl_data_t  gotgctl = {.d32 = 0};
1966 +
1967 +       DWC_DEBUGPL(DBG_CILV,"%s(%p)\n", __func__, _core_if);
1968 +
1969 +        /* Restart the Phy Clock */
1970 +        dwc_write_reg32(_core_if->pcgcctl, 0);
1971 +        
1972 +       /* Initialize Host Configuration Register */
1973 +       init_fslspclksel(_core_if);
1974 +       if (_core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
1975 +               hcfg.d32 = dwc_read_reg32(&host_if->host_global_regs->hcfg);
1976 +               hcfg.b.fslssupp = 1;
1977 +               dwc_write_reg32(&host_if->host_global_regs->hcfg, hcfg.d32);
1978 +       }
1979 +
1980 +       /* Configure data FIFO sizes */
1981 +       if (_core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
1982 +                DWC_DEBUGPL(DBG_CIL,"Total FIFO Size=%d\n", _core_if->total_fifo_size);
1983 +                DWC_DEBUGPL(DBG_CIL,"Rx FIFO Size=%d\n", params->host_rx_fifo_size);
1984 +                DWC_DEBUGPL(DBG_CIL,"NP Tx FIFO Size=%d\n", params->host_nperio_tx_fifo_size);
1985 +                DWC_DEBUGPL(DBG_CIL,"P Tx FIFO Size=%d\n", params->host_perio_tx_fifo_size);
1986 +
1987 +               /* Rx FIFO */
1988 +               DWC_DEBUGPL(DBG_CIL,"initial grxfsiz=%08x\n", dwc_read_reg32(&global_regs->grxfsiz));
1989 +               dwc_write_reg32(&global_regs->grxfsiz, params->host_rx_fifo_size);
1990 +               DWC_DEBUGPL(DBG_CIL,"new grxfsiz=%08x\n", dwc_read_reg32(&global_regs->grxfsiz));
1991 +
1992 +               /* Non-periodic Tx FIFO */
1993 +                DWC_DEBUGPL(DBG_CIL,"initial gnptxfsiz=%08x\n", dwc_read_reg32(&global_regs->gnptxfsiz));
1994 +                nptxfifosize.b.depth  = params->host_nperio_tx_fifo_size;
1995 +                nptxfifosize.b.startaddr = params->host_rx_fifo_size;
1996 +                dwc_write_reg32(&global_regs->gnptxfsiz, nptxfifosize.d32);
1997 +                DWC_DEBUGPL(DBG_CIL,"new gnptxfsiz=%08x\n", dwc_read_reg32(&global_regs->gnptxfsiz));
1998 +               
1999 +               /* Periodic Tx FIFO */
2000 +                DWC_DEBUGPL(DBG_CIL,"initial hptxfsiz=%08x\n", dwc_read_reg32(&global_regs->hptxfsiz));
2001 +                ptxfifosize.b.depth  = params->host_perio_tx_fifo_size;
2002 +                ptxfifosize.b.startaddr = nptxfifosize.b.startaddr + nptxfifosize.b.depth;
2003 +                dwc_write_reg32(&global_regs->hptxfsiz, ptxfifosize.d32);
2004 +                DWC_DEBUGPL(DBG_CIL,"new hptxfsiz=%08x\n", dwc_read_reg32(&global_regs->hptxfsiz));
2005 +       }
2006 +
2007 +        /* Clear Host Set HNP Enable in the OTG Control Register */
2008 +        gotgctl.b.hstsethnpen = 1;
2009 +        dwc_modify_reg32( &global_regs->gotgctl, gotgctl.d32, 0);
2010 +
2011 +       /* Make sure the FIFOs are flushed. */
2012 +       dwc_otg_flush_tx_fifo(_core_if, 0x10 /* all Tx FIFOs */);
2013 +       dwc_otg_flush_rx_fifo(_core_if);
2014 +
2015 +       /* Flush out any leftover queued requests. */
2016 +       num_channels = _core_if->core_params->host_channels;
2017 +       for (i = 0; i < num_channels; i++) {
2018 +               hc_regs = _core_if->host_if->hc_regs[i];
2019 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2020 +               hcchar.b.chen = 0;
2021 +               hcchar.b.chdis = 1;
2022 +               hcchar.b.epdir = 0;
2023 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2024 +       }
2025 +              
2026 +       /* Halt all channels to put them into a known state. */
2027 +       for (i = 0; i < num_channels; i++) {
2028 +               int count = 0;
2029 +               hc_regs = _core_if->host_if->hc_regs[i];
2030 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2031 +               hcchar.b.chen = 1;
2032 +               hcchar.b.chdis = 1;
2033 +               hcchar.b.epdir = 0;
2034 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2035 +               DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d\n", __func__, i);
2036 +               do {
2037 +                       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2038 +                       if (++count > 200) {
2039 +                               DWC_ERROR("%s: Unable to clear halt on channel %d\n",
2040 +                                         __func__, i);
2041 +                               break;
2042 +                       }
2043 +                       udelay(100);
2044 +               } while (hcchar.b.chen);
2045 +       }
2046 +
2047 +       /* Turn on the vbus power. */
2048 +        DWC_PRINT("Init: Port Power? op_state=%d\n", _core_if->op_state);
2049 +        if (_core_if->op_state == A_HOST){   
2050 +                hprt0.d32 = dwc_otg_read_hprt0(_core_if);
2051 +                DWC_PRINT("Init: Power Port (%d)\n", hprt0.b.prtpwr);
2052 +                if (hprt0.b.prtpwr == 0 ) {
2053 +                        hprt0.b.prtpwr = 1;
2054 +                        dwc_write_reg32(host_if->hprt0, hprt0.d32);
2055 +                }  
2056 +        }
2057 +
2058 +        dwc_otg_enable_host_interrupts( _core_if );
2059 +}
2060 +
2061 +/**
2062 + * Prepares a host channel for transferring packets to/from a specific
2063 + * endpoint. The HCCHARn register is set up with the characteristics specified
2064 + * in _hc. Host channel interrupts that may need to be serviced while this
2065 + * transfer is in progress are enabled.
2066 + *
2067 + * @param _core_if Programming view of DWC_otg controller
2068 + * @param _hc Information needed to initialize the host channel
2069 + */
2070 +void dwc_otg_hc_init(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2071 +{
2072 +       uint32_t intr_enable;
2073 +       hcintmsk_data_t hc_intr_mask;
2074 +       gintmsk_data_t gintmsk = {.d32 = 0};
2075 +       hcchar_data_t hcchar;
2076 +       hcsplt_data_t hcsplt;
2077 +
2078 +       uint8_t hc_num = _hc->hc_num;
2079 +       dwc_otg_host_if_t *host_if = _core_if->host_if;
2080 +       dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
2081 +
2082 +       /* Clear old interrupt conditions for this host channel. */
2083 +       hc_intr_mask.d32 = 0xFFFFFFFF;
2084 +       hc_intr_mask.b.reserved = 0;
2085 +       dwc_write_reg32(&hc_regs->hcint, hc_intr_mask.d32);
2086 +
2087 +       /* Enable channel interrupts required for this transfer. */
2088 +       hc_intr_mask.d32 = 0;
2089 +       hc_intr_mask.b.chhltd = 1;
2090 +       if (_core_if->dma_enable) {
2091 +               hc_intr_mask.b.ahberr = 1;
2092 +               if (_hc->error_state && !_hc->do_split &&
2093 +                   _hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
2094 +                       hc_intr_mask.b.ack = 1;
2095 +                       if (_hc->ep_is_in) {
2096 +                               hc_intr_mask.b.datatglerr = 1;
2097 +                               if (_hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
2098 +                                       hc_intr_mask.b.nak = 1;
2099 +                               }
2100 +                       }
2101 +               }
2102 +       } else {
2103 +               switch (_hc->ep_type) {
2104 +               case DWC_OTG_EP_TYPE_CONTROL:
2105 +               case DWC_OTG_EP_TYPE_BULK:
2106 +                       hc_intr_mask.b.xfercompl = 1;
2107 +                       hc_intr_mask.b.stall = 1;
2108 +                       hc_intr_mask.b.xacterr = 1;
2109 +                       hc_intr_mask.b.datatglerr = 1;
2110 +                       if (_hc->ep_is_in) {
2111 +                               hc_intr_mask.b.bblerr = 1;
2112 +                       } else {
2113 +                               hc_intr_mask.b.nak = 1;
2114 +                               hc_intr_mask.b.nyet = 1;
2115 +                               if (_hc->do_ping) {
2116 +                                       hc_intr_mask.b.ack = 1;
2117 +                               }
2118 +                       }
2119 +
2120 +                       if (_hc->do_split) {
2121 +                               hc_intr_mask.b.nak = 1;
2122 +                               if (_hc->complete_split) {
2123 +                                       hc_intr_mask.b.nyet = 1;
2124 +                               }
2125 +                               else {
2126 +                                       hc_intr_mask.b.ack = 1;
2127 +                               }
2128 +                       }
2129 +
2130 +                       if (_hc->error_state) {
2131 +                               hc_intr_mask.b.ack = 1;
2132 +                       }
2133 +                       break;
2134 +               case DWC_OTG_EP_TYPE_INTR:
2135 +                       hc_intr_mask.b.xfercompl = 1;
2136 +                       hc_intr_mask.b.nak = 1;
2137 +                       hc_intr_mask.b.stall = 1;
2138 +                       hc_intr_mask.b.xacterr = 1;
2139 +                       hc_intr_mask.b.datatglerr = 1;
2140 +                       hc_intr_mask.b.frmovrun = 1;
2141 +
2142 +                       if (_hc->ep_is_in) {
2143 +                               hc_intr_mask.b.bblerr = 1;
2144 +                       }
2145 +                       if (_hc->error_state) {
2146 +                               hc_intr_mask.b.ack = 1;
2147 +                       }
2148 +                       if (_hc->do_split) {
2149 +                               if (_hc->complete_split) {
2150 +                                       hc_intr_mask.b.nyet = 1;
2151 +                               }
2152 +                               else {
2153 +                                       hc_intr_mask.b.ack = 1;
2154 +                               }
2155 +                       }
2156 +                       break;
2157 +               case DWC_OTG_EP_TYPE_ISOC:
2158 +                       hc_intr_mask.b.xfercompl = 1;
2159 +                       hc_intr_mask.b.frmovrun = 1;
2160 +                       hc_intr_mask.b.ack = 1;
2161 +
2162 +                       if (_hc->ep_is_in) {
2163 +                               hc_intr_mask.b.xacterr = 1;
2164 +                               hc_intr_mask.b.bblerr = 1;
2165 +                       }
2166 +                       break;
2167 +               }
2168 +       }
2169 +       dwc_write_reg32(&hc_regs->hcintmsk, hc_intr_mask.d32);
2170 +
2171 +       /* Enable the top level host channel interrupt. */
2172 +       intr_enable = (1 << hc_num);
2173 +       dwc_modify_reg32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
2174 +
2175 +       /* Make sure host channel interrupts are enabled. */
2176 +       gintmsk.b.hcintr = 1;
2177 +       dwc_modify_reg32(&_core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
2178 +       
2179 +       /*
2180 +        * Program the HCCHARn register with the endpoint characteristics for
2181 +        * the current transfer.
2182 +        */
2183 +       hcchar.d32 = 0;
2184 +       hcchar.b.devaddr = _hc->dev_addr;
2185 +       hcchar.b.epnum = _hc->ep_num;
2186 +       hcchar.b.epdir = _hc->ep_is_in;
2187 +       hcchar.b.lspddev = (_hc->speed == DWC_OTG_EP_SPEED_LOW);
2188 +       hcchar.b.eptype = _hc->ep_type;
2189 +       hcchar.b.mps = _hc->max_packet;
2190 +
2191 +       dwc_write_reg32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
2192 +
2193 +       DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, _hc->hc_num);
2194 +       DWC_DEBUGPL(DBG_HCDV, "  Dev Addr: %d\n", hcchar.b.devaddr);
2195 +       DWC_DEBUGPL(DBG_HCDV, "  Ep Num: %d\n", hcchar.b.epnum);
2196 +       DWC_DEBUGPL(DBG_HCDV, "  Is In: %d\n", hcchar.b.epdir);
2197 +       DWC_DEBUGPL(DBG_HCDV, "  Is Low Speed: %d\n", hcchar.b.lspddev);
2198 +       DWC_DEBUGPL(DBG_HCDV, "  Ep Type: %d\n", hcchar.b.eptype);
2199 +       DWC_DEBUGPL(DBG_HCDV, "  Max Pkt: %d\n", hcchar.b.mps);
2200 +       DWC_DEBUGPL(DBG_HCDV, "  Multi Cnt: %d\n", hcchar.b.multicnt);
2201 +
2202 +       /*
2203 +        * Program the HCSPLIT register for SPLITs
2204 +        */
2205 +       hcsplt.d32 = 0;
2206 +       if (_hc->do_split) {
2207 +               DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n", _hc->hc_num,
2208 +                          _hc->complete_split ? "CSPLIT" : "SSPLIT");
2209 +               hcsplt.b.compsplt = _hc->complete_split;
2210 +               hcsplt.b.xactpos = _hc->xact_pos;
2211 +               hcsplt.b.hubaddr = _hc->hub_addr;
2212 +               hcsplt.b.prtaddr = _hc->port_addr;
2213 +               DWC_DEBUGPL(DBG_HCDV, "   comp split %d\n", _hc->complete_split);
2214 +               DWC_DEBUGPL(DBG_HCDV, "   xact pos %d\n", _hc->xact_pos);
2215 +               DWC_DEBUGPL(DBG_HCDV, "   hub addr %d\n", _hc->hub_addr);
2216 +               DWC_DEBUGPL(DBG_HCDV, "   port addr %d\n", _hc->port_addr);
2217 +               DWC_DEBUGPL(DBG_HCDV, "   is_in %d\n", _hc->ep_is_in);
2218 +               DWC_DEBUGPL(DBG_HCDV, "   Max Pkt: %d\n", hcchar.b.mps);
2219 +               DWC_DEBUGPL(DBG_HCDV, "   xferlen: %d\n", _hc->xfer_len);               
2220 +       }
2221 +       dwc_write_reg32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
2222 +
2223 +}
2224 +
2225 +/**
2226 + * Attempts to halt a host channel. This function should only be called in
2227 + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
2228 + * normal circumstances in DMA mode, the controller halts the channel when the
2229 + * transfer is complete or a condition occurs that requires application
2230 + * intervention.
2231 + *
2232 + * In slave mode, checks for a free request queue entry, then sets the Channel
2233 + * Enable and Channel Disable bits of the Host Channel Characteristics
2234 + * register of the specified channel to intiate the halt. If there is no free
2235 + * request queue entry, sets only the Channel Disable bit of the HCCHARn
2236 + * register to flush requests for this channel. In the latter case, sets a
2237 + * flag to indicate that the host channel needs to be halted when a request
2238 + * queue slot is open.
2239 + *
2240 + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
2241 + * HCCHARn register. The controller ensures there is space in the request
2242 + * queue before submitting the halt request.
2243 + *
2244 + * Some time may elapse before the core flushes any posted requests for this
2245 + * host channel and halts. The Channel Halted interrupt handler completes the
2246 + * deactivation of the host channel.
2247 + *
2248 + * @param _core_if Controller register interface.
2249 + * @param _hc Host channel to halt.
2250 + * @param _halt_status Reason for halting the channel.
2251 + */
2252 +void dwc_otg_hc_halt(dwc_otg_core_if_t *_core_if,
2253 +                    dwc_hc_t *_hc,
2254 +                    dwc_otg_halt_status_e _halt_status)
2255 +{
2256 +       gnptxsts_data_t                 nptxsts;
2257 +       hptxsts_data_t                  hptxsts;
2258 +       hcchar_data_t                   hcchar;
2259 +       dwc_otg_hc_regs_t               *hc_regs;
2260 +       dwc_otg_core_global_regs_t      *global_regs;
2261 +       dwc_otg_host_global_regs_t      *host_global_regs;
2262 +
2263 +       hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2264 +       global_regs = _core_if->core_global_regs;
2265 +       host_global_regs = _core_if->host_if->host_global_regs;
2266 +
2267 +       WARN_ON(_halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS);
2268 +
2269 +       if (_halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
2270 +           _halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
2271 +               /*
2272 +                * Disable all channel interrupts except Ch Halted. The QTD
2273 +                * and QH state associated with this transfer has been cleared
2274 +                * (in the case of URB_DEQUEUE), so the channel needs to be
2275 +                * shut down carefully to prevent crashes.
2276 +                */
2277 +               hcintmsk_data_t hcintmsk;
2278 +               hcintmsk.d32 = 0;
2279 +               hcintmsk.b.chhltd = 1;
2280 +               dwc_write_reg32(&hc_regs->hcintmsk, hcintmsk.d32);
2281 +
2282 +               /*
2283 +                * Make sure no other interrupts besides halt are currently
2284 +                * pending. Handling another interrupt could cause a crash due
2285 +                * to the QTD and QH state.
2286 +                */
2287 +               dwc_write_reg32(&hc_regs->hcint, ~hcintmsk.d32);
2288 +
2289 +               /*
2290 +                * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
2291 +                * even if the channel was already halted for some other
2292 +                * reason.
2293 +                */
2294 +               _hc->halt_status = _halt_status;
2295 +
2296 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2297 +               if (hcchar.b.chen == 0) {
2298 +                       /*
2299 +                        * The channel is either already halted or it hasn't
2300 +                        * started yet. In DMA mode, the transfer may halt if
2301 +                        * it finishes normally or a condition occurs that
2302 +                        * requires driver intervention. Don't want to halt
2303 +                        * the channel again. In either Slave or DMA mode,
2304 +                        * it's possible that the transfer has been assigned
2305 +                        * to a channel, but not started yet when an URB is
2306 +                        * dequeued. Don't want to halt a channel that hasn't
2307 +                        * started yet.
2308 +                        */
2309 +                       return;
2310 +               }
2311 +       }
2312 +
2313 +       if (_hc->halt_pending) {
2314 +               /*
2315 +                * A halt has already been issued for this channel. This might
2316 +                * happen when a transfer is aborted by a higher level in
2317 +                * the stack.
2318 +                */
2319 +#ifdef DEBUG
2320 +               DWC_PRINT("*** %s: Channel %d, _hc->halt_pending already set ***\n",
2321 +                         __func__, _hc->hc_num);
2322 +
2323 +/*             dwc_otg_dump_global_registers(_core_if); */
2324 +/*             dwc_otg_dump_host_registers(_core_if); */
2325 +#endif         
2326 +               return;
2327 +       }
2328 +
2329 +        hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2330 +       hcchar.b.chen = 1;
2331 +       hcchar.b.chdis = 1;
2332 +
2333 +       if (!_core_if->dma_enable) {
2334 +               /* Check for space in the request queue to issue the halt. */
2335 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
2336 +                   _hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
2337 +                       nptxsts.d32 = dwc_read_reg32(&global_regs->gnptxsts);
2338 +                       if (nptxsts.b.nptxqspcavail == 0) {
2339 +                               hcchar.b.chen = 0;
2340 +                       }
2341 +               } else {
2342 +                       hptxsts.d32 = dwc_read_reg32(&host_global_regs->hptxsts);
2343 +                       if ((hptxsts.b.ptxqspcavail == 0) || (_core_if->queuing_high_bandwidth)) {
2344 +                               hcchar.b.chen = 0;
2345 +                       }
2346 +               }
2347 +       }
2348 +
2349 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2350 +
2351 +       _hc->halt_status = _halt_status;
2352 +
2353 +       if (hcchar.b.chen) {
2354 +               _hc->halt_pending = 1;
2355 +               _hc->halt_on_queue = 0;
2356 +       } else {
2357 +               _hc->halt_on_queue = 1;
2358 +       }
2359 +
2360 +       DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, _hc->hc_num);
2361 +       DWC_DEBUGPL(DBG_HCDV, "  hcchar: 0x%08x\n", hcchar.d32);
2362 +       DWC_DEBUGPL(DBG_HCDV, "  halt_pending: %d\n", _hc->halt_pending);
2363 +       DWC_DEBUGPL(DBG_HCDV, "  halt_on_queue: %d\n", _hc->halt_on_queue);
2364 +       DWC_DEBUGPL(DBG_HCDV, "  halt_status: %d\n", _hc->halt_status);
2365 +
2366 +       return;
2367 +}
2368 +
2369 +/**
2370 + * Clears the transfer state for a host channel. This function is normally
2371 + * called after a transfer is done and the host channel is being released.
2372 + *
2373 + * @param _core_if Programming view of DWC_otg controller.
2374 + * @param _hc Identifies the host channel to clean up.
2375 + */
2376 +void dwc_otg_hc_cleanup(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2377 +{
2378 +       dwc_otg_hc_regs_t *hc_regs;
2379 +
2380 +       _hc->xfer_started = 0;
2381 +
2382 +       /*
2383 +        * Clear channel interrupt enables and any unhandled channel interrupt
2384 +        * conditions.
2385 +        */
2386 +       hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2387 +       dwc_write_reg32(&hc_regs->hcintmsk, 0);
2388 +       dwc_write_reg32(&hc_regs->hcint, 0xFFFFFFFF);
2389 +
2390 +#ifdef DEBUG
2391 +       del_timer(&_core_if->hc_xfer_timer[_hc->hc_num]);
2392 +       {
2393 +               hcchar_data_t hcchar;
2394 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2395 +               if (hcchar.b.chdis) {
2396 +                       DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
2397 +                                __func__, _hc->hc_num, hcchar.d32);
2398 +               }
2399 +       }
2400 +#endif 
2401 +}
2402 +
2403 +/**
2404 + * Sets the channel property that indicates in which frame a periodic transfer
2405 + * should occur. This is always set to the _next_ frame. This function has no
2406 + * effect on non-periodic transfers.
2407 + *
2408 + * @param _core_if Programming view of DWC_otg controller.
2409 + * @param _hc Identifies the host channel to set up and its properties.
2410 + * @param _hcchar Current value of the HCCHAR register for the specified host
2411 + * channel.
2412 + */
2413 +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t *_core_if,
2414 +                                        dwc_hc_t *_hc,
2415 +                                        hcchar_data_t *_hcchar)
2416 +{
2417 +       if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
2418 +           _hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
2419 +               hfnum_data_t    hfnum;
2420 +               hfnum.d32 = dwc_read_reg32(&_core_if->host_if->host_global_regs->hfnum);
2421 +               /* 1 if _next_ frame is odd, 0 if it's even */
2422 +               _hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
2423 +#ifdef DEBUG
2424 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR && _hc->do_split && !_hc->complete_split) {
2425 +                       switch (hfnum.b.frnum & 0x7) {
2426 +                       case 7:
2427 +                               _core_if->hfnum_7_samples++;
2428 +                               _core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
2429 +                               break;
2430 +                       case 0:
2431 +                               _core_if->hfnum_0_samples++;
2432 +                               _core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
2433 +                               break;
2434 +                       default:
2435 +                               _core_if->hfnum_other_samples++;
2436 +                               _core_if->hfnum_other_frrem_accum += hfnum.b.frrem;
2437 +                               break;
2438 +                       }
2439 +               }
2440 +#endif         
2441 +       }
2442 +}
2443 +
2444 +#ifdef DEBUG
2445 +static void hc_xfer_timeout(unsigned long _ptr)
2446 +{
2447 +       hc_xfer_info_t *xfer_info = (hc_xfer_info_t *)_ptr;
2448 +       int hc_num = xfer_info->hc->hc_num;
2449 +       DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
2450 +       DWC_WARN("  start_hcchar_val 0x%08x\n", xfer_info->core_if->start_hcchar_val[hc_num]);
2451 +}
2452 +#endif
2453 +
2454 +/*
2455 + * This function does the setup for a data transfer for a host channel and
2456 + * starts the transfer. May be called in either Slave mode or DMA mode. In
2457 + * Slave mode, the caller must ensure that there is sufficient space in the
2458 + * request queue and Tx Data FIFO.
2459 + *
2460 + * For an OUT transfer in Slave mode, it loads a data packet into the
2461 + * appropriate FIFO. If necessary, additional data packets will be loaded in
2462 + * the Host ISR.
2463 + *
2464 + * For an IN transfer in Slave mode, a data packet is requested. The data
2465 + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
2466 + * additional data packets are requested in the Host ISR.
2467 + *
2468 + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
2469 + * register along with a packet count of 1 and the channel is enabled. This
2470 + * causes a single PING transaction to occur. Other fields in HCTSIZ are
2471 + * simply set to 0 since no data transfer occurs in this case.
2472 + *
2473 + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
2474 + * all the information required to perform the subsequent data transfer. In
2475 + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
2476 + * controller performs the entire PING protocol, then starts the data
2477 + * transfer.
2478 + *
2479 + * @param _core_if Programming view of DWC_otg controller.
2480 + * @param _hc Information needed to initialize the host channel. The xfer_len
2481 + * value may be reduced to accommodate the max widths of the XferSize and
2482 + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
2483 + * to reflect the final xfer_len value.
2484 + */
2485 +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2486 +{
2487 +       hcchar_data_t hcchar;
2488 +       hctsiz_data_t hctsiz;
2489 +       uint16_t num_packets;
2490 +       uint32_t max_hc_xfer_size = _core_if->core_params->max_transfer_size;
2491 +       uint16_t max_hc_pkt_count = _core_if->core_params->max_packet_count;
2492 +       dwc_otg_hc_regs_t *hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2493 +
2494 +       hctsiz.d32 = 0;
2495 +
2496 +       if (_hc->do_ping) {
2497 +               if (!_core_if->dma_enable) {
2498 +                       dwc_otg_hc_do_ping(_core_if, _hc);
2499 +                       _hc->xfer_started = 1;
2500 +                       return;
2501 +               } else {
2502 +                       hctsiz.b.dopng = 1;
2503 +               }
2504 +       }
2505 +
2506 +       if (_hc->do_split) {
2507 +               num_packets = 1;
2508 +
2509 +               if (_hc->complete_split && !_hc->ep_is_in) {
2510 +                       /* For CSPLIT OUT Transfer, set the size to 0 so the
2511 +                        * core doesn't expect any data written to the FIFO */
2512 +                       _hc->xfer_len = 0;
2513 +               } else if (_hc->ep_is_in || (_hc->xfer_len > _hc->max_packet)) {
2514 +                       _hc->xfer_len = _hc->max_packet;
2515 +               } else if (!_hc->ep_is_in && (_hc->xfer_len > 188)) {
2516 +                       _hc->xfer_len = 188;
2517 +               }
2518 +
2519 +               hctsiz.b.xfersize = _hc->xfer_len;
2520 +       } else {
2521 +               /*
2522 +                * Ensure that the transfer length and packet count will fit
2523 +                * in the widths allocated for them in the HCTSIZn register.
2524 +                */
2525 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
2526 +                   _hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
2527 +                       /*
2528 +                        * Make sure the transfer size is no larger than one
2529 +                        * (micro)frame's worth of data. (A check was done
2530 +                        * when the periodic transfer was accepted to ensure
2531 +                        * that a (micro)frame's worth of data can be
2532 +                        * programmed into a channel.)
2533 +                        */
2534 +                       uint32_t max_periodic_len = _hc->multi_count * _hc->max_packet;
2535 +                       if (_hc->xfer_len > max_periodic_len) {
2536 +                               _hc->xfer_len = max_periodic_len;
2537 +                       } else {
2538 +                       }
2539 +               } else if (_hc->xfer_len > max_hc_xfer_size) {
2540 +                       /* Make sure that xfer_len is a multiple of max packet size. */
2541 +                       _hc->xfer_len = max_hc_xfer_size - _hc->max_packet + 1;
2542 +               }
2543 +
2544 +               if (_hc->xfer_len > 0) {
2545 +                       num_packets = (_hc->xfer_len + _hc->max_packet - 1) / _hc->max_packet;
2546 +                       if (num_packets > max_hc_pkt_count) {
2547 +                               num_packets = max_hc_pkt_count;
2548 +                               _hc->xfer_len = num_packets * _hc->max_packet;
2549 +                       }
2550 +               } else {
2551 +                       /* Need 1 packet for transfer length of 0. */
2552 +                       num_packets = 1;
2553 +               }
2554 +
2555 +               if (_hc->ep_is_in) {
2556 +                       /* Always program an integral # of max packets for IN transfers. */
2557 +                       _hc->xfer_len = num_packets * _hc->max_packet;
2558 +               }
2559 +
2560 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
2561 +                   _hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
2562 +                       /*
2563 +                        * Make sure that the multi_count field matches the
2564 +                        * actual transfer length.
2565 +                        */
2566 +                       _hc->multi_count = num_packets;
2567 +
2568 +               }
2569 +
2570 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
2571 +                       /* Set up the initial PID for the transfer. */
2572 +                       if (_hc->speed == DWC_OTG_EP_SPEED_HIGH) {
2573 +                               if (_hc->ep_is_in) {
2574 +                                       if (_hc->multi_count == 1) {
2575 +                                               _hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
2576 +                                       } else if (_hc->multi_count == 2) {
2577 +                                               _hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
2578 +                                       } else {
2579 +                                               _hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
2580 +                                       }
2581 +                               } else {
2582 +                                       if (_hc->multi_count == 1) {
2583 +                                               _hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
2584 +                                       } else {
2585 +                                               _hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
2586 +                                       }
2587 +                               }
2588 +                       } else {
2589 +                               _hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
2590 +                       }
2591 +               }
2592 +
2593 +               hctsiz.b.xfersize = _hc->xfer_len;
2594 +       }
2595 +
2596 +       _hc->start_pkt_count = num_packets;
2597 +       hctsiz.b.pktcnt = num_packets;
2598 +       hctsiz.b.pid = _hc->data_pid_start;
2599 +       dwc_write_reg32(&hc_regs->hctsiz, hctsiz.d32);
2600 +
2601 +       DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, _hc->hc_num);
2602 +       DWC_DEBUGPL(DBG_HCDV, "  Xfer Size: %d\n", hctsiz.b.xfersize);
2603 +       DWC_DEBUGPL(DBG_HCDV, "  Num Pkts: %d\n", hctsiz.b.pktcnt);
2604 +       DWC_DEBUGPL(DBG_HCDV, "  Start PID: %d\n", hctsiz.b.pid);
2605 +
2606 +       if (_core_if->dma_enable) {
2607 +#ifdef DEBUG
2608 +if(((uint32_t)_hc->xfer_buff)%4)
2609 +printk("dwc_otg_hc_start_transfer _hc->xfer_buff not 4 byte alignment\n");
2610 +#endif
2611 +               dwc_write_reg32(&hc_regs->hcdma, (uint32_t)_hc->xfer_buff);
2612 +       }
2613 +
2614 +       /* Start the split */
2615 +       if (_hc->do_split) {
2616 +               hcsplt_data_t hcsplt;
2617 +               hcsplt.d32 = dwc_read_reg32 (&hc_regs->hcsplt);
2618 +               hcsplt.b.spltena = 1;
2619 +               dwc_write_reg32(&hc_regs->hcsplt, hcsplt.d32);
2620 +       }
2621 +
2622 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2623 +       hcchar.b.multicnt = _hc->multi_count;
2624 +       hc_set_even_odd_frame(_core_if, _hc, &hcchar);
2625 +#ifdef DEBUG
2626 +       _core_if->start_hcchar_val[_hc->hc_num] = hcchar.d32;
2627 +       if (hcchar.b.chdis) {
2628 +               DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
2629 +                        __func__, _hc->hc_num, hcchar.d32);
2630 +       }
2631 +#endif 
2632 +
2633 +       /* Set host channel enable after all other setup is complete. */
2634 +       hcchar.b.chen = 1;
2635 +       hcchar.b.chdis = 0;
2636 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2637 +
2638 +       _hc->xfer_started = 1;
2639 +       _hc->requests++;
2640 +
2641 +       if (!_core_if->dma_enable && !_hc->ep_is_in && _hc->xfer_len > 0) {
2642 +               /* Load OUT packet into the appropriate Tx FIFO. */
2643 +               dwc_otg_hc_write_packet(_core_if, _hc);
2644 +       }
2645 +
2646 +#ifdef DEBUG
2647 +       /* Start a timer for this transfer. */
2648 +       _core_if->hc_xfer_timer[_hc->hc_num].function = hc_xfer_timeout;
2649 +       _core_if->hc_xfer_info[_hc->hc_num].core_if = _core_if;
2650 +       _core_if->hc_xfer_info[_hc->hc_num].hc = _hc;
2651 +       _core_if->hc_xfer_timer[_hc->hc_num].data = (unsigned long)(&_core_if->hc_xfer_info[_hc->hc_num]);
2652 +       _core_if->hc_xfer_timer[_hc->hc_num].expires = jiffies + (HZ*10);
2653 +       add_timer(&_core_if->hc_xfer_timer[_hc->hc_num]);
2654 +#endif
2655 +}
2656 +
2657 +/**
2658 + * This function continues a data transfer that was started by previous call
2659 + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
2660 + * sufficient space in the request queue and Tx Data FIFO. This function
2661 + * should only be called in Slave mode. In DMA mode, the controller acts
2662 + * autonomously to complete transfers programmed to a host channel.
2663 + *
2664 + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
2665 + * if there is any data remaining to be queued. For an IN transfer, another
2666 + * data packet is always requested. For the SETUP phase of a control transfer,
2667 + * this function does nothing.
2668 + *
2669 + * @return 1 if a new request is queued, 0 if no more requests are required
2670 + * for this transfer.
2671 + */
2672 +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2673 +{
2674 +       DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, _hc->hc_num);
2675 +
2676 +       if (_hc->do_split) {
2677 +               /* SPLITs always queue just once per channel */
2678 +               return 0;
2679 +       } else if (_hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
2680 +               /* SETUPs are queued only once since they can't be NAKed. */
2681 +               return 0;
2682 +       } else if (_hc->ep_is_in) {
2683 +               /*
2684 +                * Always queue another request for other IN transfers. If
2685 +                * back-to-back INs are issued and NAKs are received for both,
2686 +                * the driver may still be processing the first NAK when the
2687 +                * second NAK is received. When the interrupt handler clears
2688 +                * the NAK interrupt for the first NAK, the second NAK will
2689 +                * not be seen. So we can't depend on the NAK interrupt
2690 +                * handler to requeue a NAKed request. Instead, IN requests
2691 +                * are issued each time this function is called. When the
2692 +                * transfer completes, the extra requests for the channel will
2693 +                * be flushed.
2694 +                */
2695 +               hcchar_data_t hcchar;
2696 +               dwc_otg_hc_regs_t *hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2697 +
2698 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2699 +               hc_set_even_odd_frame(_core_if, _hc, &hcchar);
2700 +               hcchar.b.chen = 1;
2701 +               hcchar.b.chdis = 0;
2702 +               DWC_DEBUGPL(DBG_HCDV, "  IN xfer: hcchar = 0x%08x\n", hcchar.d32);
2703 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2704 +               _hc->requests++;
2705 +               return 1;
2706 +       } else {
2707 +               /* OUT transfers. */
2708 +               if (_hc->xfer_count < _hc->xfer_len) {
2709 +                       if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
2710 +                           _hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
2711 +                               hcchar_data_t hcchar;
2712 +                               dwc_otg_hc_regs_t *hc_regs;
2713 +                               hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2714 +                               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2715 +                               hc_set_even_odd_frame(_core_if, _hc, &hcchar);
2716 +                       }
2717 +
2718 +                       /* Load OUT packet into the appropriate Tx FIFO. */
2719 +                       dwc_otg_hc_write_packet(_core_if, _hc);
2720 +                       _hc->requests++;
2721 +                       return 1;
2722 +               } else {
2723 +                       return 0;
2724 +               }
2725 +       }
2726 +}
2727 +
2728 +/**
2729 + * Starts a PING transfer. This function should only be called in Slave mode.
2730 + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
2731 + */
2732 +void dwc_otg_hc_do_ping(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2733 +{
2734 +       hcchar_data_t hcchar;
2735 +       hctsiz_data_t hctsiz;
2736 +       dwc_otg_hc_regs_t *hc_regs = _core_if->host_if->hc_regs[_hc->hc_num];
2737 +
2738 +       DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, _hc->hc_num);
2739 +
2740 +       hctsiz.d32 = 0;
2741 +       hctsiz.b.dopng = 1;
2742 +       hctsiz.b.pktcnt = 1;
2743 +       dwc_write_reg32(&hc_regs->hctsiz, hctsiz.d32);
2744 +
2745 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
2746 +       hcchar.b.chen = 1;
2747 +       hcchar.b.chdis = 0;
2748 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
2749 +}
2750 +
2751 +/*
2752 + * This function writes a packet into the Tx FIFO associated with the Host
2753 + * Channel. For a channel associated with a non-periodic EP, the non-periodic
2754 + * Tx FIFO is written. For a channel associated with a periodic EP, the
2755 + * periodic Tx FIFO is written. This function should only be called in Slave
2756 + * mode.
2757 + *
2758 + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
2759 + * then number of bytes written to the Tx FIFO.
2760 + */
2761 +void dwc_otg_hc_write_packet(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
2762 +{
2763 +       uint32_t i;
2764 +       uint32_t remaining_count;
2765 +       uint32_t byte_count;
2766 +       uint32_t dword_count;
2767 +
2768 +       uint32_t *data_buff = (uint32_t *)(_hc->xfer_buff);
2769 +       uint32_t *data_fifo = _core_if->data_fifo[_hc->hc_num];
2770 +
2771 +       remaining_count = _hc->xfer_len - _hc->xfer_count;
2772 +       if (remaining_count > _hc->max_packet) {
2773 +               byte_count = _hc->max_packet;
2774 +       } else {
2775 +               byte_count = remaining_count;
2776 +       }
2777 +
2778 +       dword_count = (byte_count + 3) / 4;
2779 +
2780 +       if ((((unsigned long)data_buff) & 0x3) == 0) {
2781 +               /* xfer_buff is DWORD aligned. */
2782 +               for (i = 0; i < dword_count; i++, data_buff++) {
2783 +                       dwc_write_reg32(data_fifo, *data_buff);
2784 +               }
2785 +       } else {
2786 +               /* xfer_buff is not DWORD aligned. */
2787 +               for (i = 0; i < dword_count; i++, data_buff++) {
2788 +                       dwc_write_reg32(data_fifo, get_unaligned(data_buff));
2789 +               }
2790 +       }
2791 +
2792 +       _hc->xfer_count += byte_count;
2793 +       _hc->xfer_buff += byte_count;
2794 +}
2795 +
2796 +/**
2797 + * Gets the current USB frame number. This is the frame number from the last 
2798 + * SOF packet.  
2799 + */
2800 +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t *_core_if)
2801 +{
2802 +       dsts_data_t dsts;
2803 +       dsts.d32 = dwc_read_reg32(&_core_if->dev_if->dev_global_regs->dsts);
2804 +
2805 +       /* read current frame/microfreme number from DSTS register */
2806 +       return dsts.b.soffn;
2807 +}
2808 +
2809 +/**
2810 + * This function reads a setup packet from the Rx FIFO into the destination 
2811 + * buffer.  This function is called from the Rx Status Queue Level (RxStsQLvl)
2812 + * Interrupt routine when a SETUP packet has been received in Slave mode.
2813 + *
2814 + * @param _core_if Programming view of DWC_otg controller.
2815 + * @param _dest Destination buffer for packet data.
2816 + */
2817 +void dwc_otg_read_setup_packet(dwc_otg_core_if_t *_core_if, uint32_t *_dest)
2818 +{
2819 +       /* Get the 8 bytes of a setup transaction data */
2820 +
2821 +       /* Pop 2 DWORDS off the receive data FIFO into memory */
2822 +       _dest[0] = dwc_read_reg32(_core_if->data_fifo[0]);
2823 +       _dest[1] = dwc_read_reg32(_core_if->data_fifo[0]);
2824 +    //_dest[0] = dwc_read_datafifo32(_core_if->data_fifo[0]);
2825 +       //_dest[1] = dwc_read_datafifo32(_core_if->data_fifo[0]);
2826 +}
2827 +
2828 +
2829 +/**
2830 + * This function enables EP0 OUT to receive SETUP packets and configures EP0 
2831 + * IN for transmitting packets.  It is normally called when the
2832 + * "Enumeration Done" interrupt occurs.
2833 + *
2834 + * @param _core_if Programming view of DWC_otg controller.
2835 + * @param _ep The EP0 data.
2836 + */
2837 +void dwc_otg_ep0_activate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
2838 +{
2839 +        dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
2840 +       dsts_data_t dsts;
2841 +       depctl_data_t diepctl;
2842 +       depctl_data_t doepctl;
2843 +       dctl_data_t dctl ={.d32=0};        
2844 +
2845 +       /* Read the Device Status and Endpoint 0 Control registers */
2846 +       dsts.d32 = dwc_read_reg32(&dev_if->dev_global_regs->dsts);
2847 +       diepctl.d32 = dwc_read_reg32(&dev_if->in_ep_regs[0]->diepctl);
2848 +       doepctl.d32 = dwc_read_reg32(&dev_if->out_ep_regs[0]->doepctl);
2849 +
2850 +       /* Set the MPS of the IN EP based on the enumeration speed */
2851 +       switch (dsts.b.enumspd) {
2852 +       case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
2853 +       case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
2854 +       case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
2855 +               diepctl.b.mps = DWC_DEP0CTL_MPS_64;
2856 +               break;
2857 +       case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
2858 +               diepctl.b.mps = DWC_DEP0CTL_MPS_8;
2859 +               break;
2860 +       }
2861 +
2862 +       dwc_write_reg32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
2863 +
2864 +       /* Enable OUT EP for receive */
2865 +       doepctl.b.epena = 1;
2866 +       dwc_write_reg32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
2867 +
2868 +#ifdef VERBOSE
2869 +        DWC_DEBUGPL(DBG_PCDV,"doepctl0=%0x\n", 
2870 +                    dwc_read_reg32(&dev_if->out_ep_regs[0]->doepctl));
2871 +        DWC_DEBUGPL(DBG_PCDV,"diepctl0=%0x\n", 
2872 +                    dwc_read_reg32(&dev_if->in_ep_regs[0]->diepctl));        
2873 +#endif
2874 +        dctl.b.cgnpinnak = 1;
2875 +        dwc_modify_reg32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
2876 +        DWC_DEBUGPL(DBG_PCDV,"dctl=%0x\n", 
2877 +                    dwc_read_reg32(&dev_if->dev_global_regs->dctl));
2878 +}
2879 +
2880 +/**
2881 + * This function activates an EP.  The Device EP control register for
2882 + * the EP is configured as defined in the ep structure.  Note: This
2883 + * function is not used for EP0.
2884 + *
2885 + * @param _core_if Programming view of DWC_otg controller.
2886 + * @param _ep The EP to activate.
2887 + */
2888 +void dwc_otg_ep_activate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
2889 +{
2890 +        dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
2891 +       depctl_data_t depctl;
2892 +       volatile uint32_t *addr;
2893 +        daint_data_t daintmsk = {.d32=0};
2894 +
2895 +        DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, _ep->num, 
2896 +                    (_ep->is_in?"IN":"OUT"));
2897 +        
2898 +       /* Read DEPCTLn register */
2899 +       if (_ep->is_in == 1) {
2900 +               addr = &dev_if->in_ep_regs[_ep->num]->diepctl;
2901 +                daintmsk.ep.in = 1<<_ep->num;
2902 +        } else {
2903 +               addr = &dev_if->out_ep_regs[_ep->num]->doepctl;
2904 +                daintmsk.ep.out = 1<<_ep->num;
2905 +       }
2906 +        
2907 +        /* If the EP is already active don't change the EP Control
2908 +         * register. */
2909 +        depctl.d32 = dwc_read_reg32(addr);
2910 +       if (!depctl.b.usbactep) {
2911 +                depctl.b.mps = _ep->maxpacket;
2912 +                depctl.b.eptype = _ep->type;
2913 +                depctl.b.txfnum = _ep->tx_fifo_num;
2914 +                
2915 +                if (_ep->type == DWC_OTG_EP_TYPE_ISOC) {
2916 +                       depctl.b.setd0pid = 1;  // ???
2917 +                } else {
2918 +                        depctl.b.setd0pid = 1;
2919 +                }
2920 +                depctl.b.usbactep = 1;
2921 +
2922 +                dwc_write_reg32(addr, depctl.d32);
2923 +                DWC_DEBUGPL(DBG_PCDV,"DEPCTL=%08x\n", dwc_read_reg32(addr));
2924 +        }
2925 +        
2926 +
2927 +        /* Enable the Interrupt for this EP */
2928 +        dwc_modify_reg32(&dev_if->dev_global_regs->daintmsk,
2929 +                         0, daintmsk.d32);
2930 +        DWC_DEBUGPL(DBG_PCDV,"DAINTMSK=%0x\n", 
2931 +                    dwc_read_reg32(&dev_if->dev_global_regs->daintmsk));
2932 +       _ep->stall_clear_flag = 0;
2933 +       return;
2934 +}
2935 +
2936 +/**
2937 + * This function deactivates an EP.  This is done by clearing the USB Active 
2938 + * EP bit in the Device EP control register.  Note: This function is not used 
2939 + * for EP0. EP0 cannot be deactivated.
2940 + *
2941 + * @param _core_if Programming view of DWC_otg controller.
2942 + * @param _ep The EP to deactivate.
2943 + */
2944 +void dwc_otg_ep_deactivate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
2945 +{
2946 +       depctl_data_t depctl ={.d32 = 0};
2947 +       volatile uint32_t *addr;
2948 +        daint_data_t daintmsk = {.d32=0};
2949 +        
2950 +       /* Read DEPCTLn register */
2951 +       if (_ep->is_in == 1) {
2952 +               addr = &_core_if->dev_if->in_ep_regs[_ep->num]->diepctl;
2953 +                daintmsk.ep.in = 1<<_ep->num;
2954 +       } else {
2955 +               addr = &_core_if->dev_if->out_ep_regs[_ep->num]->doepctl;
2956 +                daintmsk.ep.out = 1<<_ep->num;
2957 +       }
2958 +
2959 +       depctl.b.usbactep = 0;
2960 +       dwc_write_reg32(addr, depctl.d32);
2961 +
2962 +        /* Disable the Interrupt for this EP */
2963 +        dwc_modify_reg32(&_core_if->dev_if->dev_global_regs->daintmsk,
2964 +                         daintmsk.d32, 0);
2965 +
2966 +       return;
2967 +}
2968 +
2969 +/**
2970 + * This function does the setup for a data transfer for an EP and
2971 + * starts the transfer.  For an IN transfer, the packets will be
2972 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
2973 + * the packets are unloaded from the Rx FIFO in the ISR.  the ISR.
2974 + *
2975 + * @param _core_if Programming view of DWC_otg controller.
2976 + * @param _ep The EP to start the transfer on.
2977 + */
2978 +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
2979 +{
2980 +        /** @todo Refactor this funciton to check the transfer size
2981 +         * count value does not execed the number bits in the Transfer
2982 +         * count register. */
2983 +       depctl_data_t depctl;
2984 +       deptsiz_data_t deptsiz;
2985 +        gintmsk_data_t intr_mask = { .d32 = 0};
2986 +
2987 +#ifdef CHECK_PACKET_COUNTER_WIDTH
2988 +        const uint32_t MAX_XFER_SIZE = 
2989 +                _core_if->core_params->max_transfer_size;
2990 +        const uint32_t MAX_PKT_COUNT = 
2991 +                _core_if->core_params->max_packet_count;
2992 +        uint32_t num_packets;
2993 +        uint32_t transfer_len;
2994 +        dwc_otg_dev_out_ep_regs_t *out_regs = 
2995 +                _core_if->dev_if->out_ep_regs[_ep->num];
2996 +        dwc_otg_dev_in_ep_regs_t *in_regs = 
2997 +                _core_if->dev_if->in_ep_regs[_ep->num];
2998 +        gnptxsts_data_t txstatus;
2999 +
3000 +        int lvl = SET_DEBUG_LEVEL(DBG_PCD);
3001 +
3002 +        
3003 +        DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
3004 +                    "xfer_buff=%p start_xfer_buff=%p\n",
3005 +                    _ep->num, (_ep->is_in?"IN":"OUT"), _ep->xfer_len, 
3006 +                    _ep->xfer_count, _ep->xfer_buff, _ep->start_xfer_buff);
3007 +
3008 +        transfer_len = _ep->xfer_len - _ep->xfer_count;
3009 +        if (transfer_len > MAX_XFER_SIZE) {
3010 +                transfer_len = MAX_XFER_SIZE;
3011 +        }
3012 +        if (transfer_len == 0) {
3013 +                num_packets = 1;
3014 +                /* OUT EP to recieve Zero-length packet set transfer
3015 +                 * size to maxpacket size. */
3016 +                if (!_ep->is_in) {
3017 +                        transfer_len = _ep->maxpacket;                
3018 +                }
3019 +        } else {
3020 +                num_packets = 
3021 +                        (transfer_len + _ep->maxpacket - 1) / _ep->maxpacket;
3022 +                if (num_packets > MAX_PKT_COUNT) {
3023 +                        num_packets = MAX_PKT_COUNT;
3024 +                }
3025 +        }
3026 +        DWC_DEBUGPL(DBG_PCD, "transfer_len=%d #pckt=%d\n", transfer_len, 
3027 +                    num_packets);
3028 +
3029 +        deptsiz.b.xfersize = transfer_len;
3030 +        deptsiz.b.pktcnt = num_packets;
3031 +
3032 +       /* IN endpoint */
3033 +       if (_ep->is_in == 1) {
3034 +               depctl.d32 = dwc_read_reg32(&in_regs->diepctl);
3035 +        } else {/* OUT endpoint */
3036 +                depctl.d32 = dwc_read_reg32(&out_regs->doepctl);
3037 +        }
3038 +        
3039 +        /* EP enable, IN data in FIFO */
3040 +        depctl.b.cnak = 1;
3041 +        depctl.b.epena = 1;
3042 +       /* IN endpoint */
3043 +       if (_ep->is_in == 1) {
3044 +                txstatus.d32 = 
3045 +                        dwc_read_reg32(&_core_if->core_global_regs->gnptxsts);
3046 +                if (txstatus.b.nptxqspcavail == 0) {
3047 +                        DWC_DEBUGPL(DBG_ANY, "TX Queue Full (0x%0x)\n", 
3048 +                                    txstatus.d32);
3049 +                        return;
3050 +                }
3051 +                dwc_write_reg32(&in_regs->dieptsiz, deptsiz.d32);
3052 +               dwc_write_reg32(&in_regs->diepctl, depctl.d32);
3053 +                /** 
3054 +                 * Enable the Non-Periodic Tx FIFO empty interrupt, the
3055 +                 * data will be written into the fifo by the ISR.
3056 +                 */ 
3057 +                if (_core_if->dma_enable) {
3058 +                       dwc_write_reg32(&in_regs->diepdma, (uint32_t) _ep->xfer_buff);
3059 +               } else {
3060 +                       if (_core_if->en_multiple_tx_fifo == 0) {
3061 +                        intr_mask.b.nptxfempty = 1;
3062 +                        dwc_modify_reg32( &_core_if->core_global_regs->gintsts,
3063 +                                          intr_mask.d32, 0);
3064 +                        dwc_modify_reg32( &_core_if->core_global_regs->gintmsk,
3065 +                                          intr_mask.d32, intr_mask.d32);
3066 +                       } else {
3067 +                           /* Enable the Tx FIFO Empty Interrupt for this EP */
3068 +                           if (_ep->xfer_len > 0 &&
3069 +                                        _ep->type != DWC_OTG_EP_TYPE_ISOC) {
3070 +                                       uint32_t fifoemptymsk = 0;
3071 +                                       fifoemptymsk = (0x1 << _ep->num);
3072 +                                       dwc_modify_reg32(&_core_if->dev_if->dev_global_regs->
3073 +                                                       dtknqr4_fifoemptymsk,0, fifoemptymsk);
3074 +                }
3075 +                       }
3076 +               }
3077 +       } else {            /* OUT endpoint */
3078 +               dwc_write_reg32(&out_regs->doeptsiz, deptsiz.d32);
3079 +               dwc_write_reg32(&out_regs->doepctl, depctl.d32);
3080 +                if (_core_if->dma_enable) {
3081 +                       dwc_write_reg32(&out_regs->doepdma,(uint32_t) _ep->xfer_buff);
3082 +               }
3083 +        }
3084 +        DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n", 
3085 +                    dwc_read_reg32(&out_regs->doepctl),
3086 +                    dwc_read_reg32(&out_regs->doeptsiz));
3087 +        DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n", 
3088 +                    dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daintmsk),
3089 +                    dwc_read_reg32(&_core_if->core_global_regs->gintmsk));        
3090 +
3091 +        SET_DEBUG_LEVEL(lvl);
3092 +#endif
3093 +        DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
3094 +        
3095 +        DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
3096 +                    "xfer_buff=%p start_xfer_buff=%p\n",
3097 +                    _ep->num, (_ep->is_in?"IN":"OUT"), _ep->xfer_len, 
3098 +                    _ep->xfer_count, _ep->xfer_buff, _ep->start_xfer_buff);
3099 +
3100 +       /* IN endpoint */
3101 +       if (_ep->is_in == 1) {
3102 +               dwc_otg_dev_in_ep_regs_t * in_regs = _core_if->dev_if->in_ep_regs[_ep->num];
3103 +               gnptxsts_data_t gtxstatus;
3104 +               gtxstatus.d32 = dwc_read_reg32(&_core_if->core_global_regs->gnptxsts);
3105 +               if (_core_if->en_multiple_tx_fifo == 0 &&
3106 +                       gtxstatus.b.nptxqspcavail == 0) {
3107 +#ifdef DEBUG
3108 +                        DWC_PRINT("TX Queue Full (0x%0x)\n", gtxstatus.d32);
3109 +#endif
3110 +                        //return;
3111 +                        MDELAY(100); //james
3112 +                }
3113 +                
3114 +               depctl.d32 = dwc_read_reg32(&(in_regs->diepctl));
3115 +               deptsiz.d32 = dwc_read_reg32(&(in_regs->dieptsiz));
3116 +
3117 +                /* Zero Length Packet? */
3118 +                if (_ep->xfer_len == 0) {
3119 +                        deptsiz.b.xfersize = 0;
3120 +                        deptsiz.b.pktcnt = 1;
3121 +                } else {
3122 +                        
3123 +                        /* Program the transfer size and packet count
3124 +                         *  as follows: xfersize = N * maxpacket +
3125 +                         *  short_packet pktcnt = N + (short_packet
3126 +                         *  exist ? 1 : 0)  
3127 +                         */
3128 +                        deptsiz.b.xfersize = _ep->xfer_len;
3129 +                       deptsiz.b.pktcnt = (_ep->xfer_len - 1 + _ep->maxpacket) / _ep->maxpacket;
3130 +               }
3131 +
3132 +                dwc_write_reg32(&in_regs->dieptsiz, deptsiz.d32);
3133 +
3134 +               /* Write the DMA register */
3135 +               if (_core_if->dma_enable) {
3136 +#if 1 // winder
3137 +                       dma_cache_wback_inv((unsigned long) _ep->xfer_buff, _ep->xfer_len); // winder
3138 +                       dwc_write_reg32 (&(in_regs->diepdma), 
3139 +                                        CPHYSADDR((uint32_t)_ep->xfer_buff)); // winder
3140 +#else
3141 +                        dwc_write_reg32 (&(in_regs->diepdma),
3142 +                                        (uint32_t)_ep->dma_addr);
3143 +#endif
3144 +               } else {
3145 +                       if (_ep->type != DWC_OTG_EP_TYPE_ISOC) {
3146 +                       /** 
3147 +                        * Enable the Non-Periodic Tx FIFO empty interrupt,
3148 +                                * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
3149 +                        * the data will be written into the fifo by the ISR.
3150 +                        */ 
3151 +                           if (_core_if->en_multiple_tx_fifo == 0) {
3152 +                        intr_mask.b.nptxfempty = 1;
3153 +                        dwc_modify_reg32( &_core_if->core_global_regs->gintsts,
3154 +                                          intr_mask.d32, 0);
3155 +                        dwc_modify_reg32( &_core_if->core_global_regs->gintmsk,
3156 +                                          intr_mask.d32, intr_mask.d32);
3157 +                               } else {
3158 +                                   /* Enable the Tx FIFO Empty Interrupt for this EP */
3159 +                                   if (_ep->xfer_len > 0) {
3160 +                                               uint32_t fifoemptymsk = 0;
3161 +                                               fifoemptymsk = 1 << _ep->num;
3162 +                                               dwc_modify_reg32(&_core_if->dev_if->dev_global_regs->
3163 +                                                                 dtknqr4_fifoemptymsk,0,fifoemptymsk);
3164 +                                       }
3165 +                               }
3166 +                       }
3167 +                }
3168 +                
3169 +               /* EP enable, IN data in FIFO */
3170 +               depctl.b.cnak = 1;
3171 +               depctl.b.epena = 1;
3172 +               dwc_write_reg32(&in_regs->diepctl, depctl.d32);
3173 +
3174 +               if (_core_if->dma_enable) {
3175 +               depctl.d32 = dwc_read_reg32 (&_core_if->dev_if->in_ep_regs[0]->diepctl);
3176 +               depctl.b.nextep = _ep->num;
3177 +               dwc_write_reg32 (&_core_if->dev_if->in_ep_regs[0]->diepctl, depctl.d32);
3178 +
3179 +               }
3180 +       } else {
3181 +                /* OUT endpoint */
3182 +           dwc_otg_dev_out_ep_regs_t * out_regs = _core_if->dev_if->out_ep_regs[_ep->num];
3183 +
3184 +               depctl.d32 = dwc_read_reg32(&(out_regs->doepctl));
3185 +               deptsiz.d32 = dwc_read_reg32(&(out_regs->doeptsiz));
3186 +
3187 +               /* Program the transfer size and packet count as follows:
3188 +                 * 
3189 +                *  pktcnt = N                                         
3190 +                *  xfersize = N * maxpacket
3191 +                 */
3192 +                if (_ep->xfer_len == 0) {
3193 +                        /* Zero Length Packet */
3194 +                        deptsiz.b.xfersize = _ep->maxpacket;
3195 +                        deptsiz.b.pktcnt = 1;
3196 +                } else {
3197 +                       deptsiz.b.pktcnt = (_ep->xfer_len + (_ep->maxpacket - 1)) / _ep->maxpacket;
3198 +                        deptsiz.b.xfersize = deptsiz.b.pktcnt * _ep->maxpacket;
3199 +                }
3200 +               dwc_write_reg32(&out_regs->doeptsiz, deptsiz.d32);
3201 +
3202 +                DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
3203 +                             _ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
3204 +
3205 +               if (_core_if->dma_enable) {
3206 +#if 1 // winder
3207 +                       dwc_write_reg32 (&(out_regs->doepdma), 
3208 +                                        CPHYSADDR((uint32_t)_ep->xfer_buff)); // winder
3209 +#else
3210 +                       dwc_write_reg32 (&(out_regs->doepdma), 
3211 +                                        (uint32_t)_ep->dma_addr);
3212 +#endif
3213 +               }
3214 +
3215 +               if (_ep->type == DWC_OTG_EP_TYPE_ISOC) {
3216 +                        /** @todo NGS: dpid is read-only. Use setd0pid
3217 +                         * or setd1pid. */
3218 +                       if (_ep->even_odd_frame) {
3219 +                               depctl.b.setd1pid = 1;
3220 +                       } else {
3221 +                               depctl.b.setd0pid = 1;
3222 +                       }
3223 +               }
3224 +
3225 +               /* EP enable */
3226 +               depctl.b.cnak = 1;
3227 +               depctl.b.epena = 1;
3228 +
3229 +               dwc_write_reg32(&out_regs->doepctl, depctl.d32);
3230 +
3231 +                DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n", 
3232 +                            dwc_read_reg32(&out_regs->doepctl),
3233 +                            dwc_read_reg32(&out_regs->doeptsiz));
3234 +                DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n", 
3235 +                            dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daintmsk),
3236 +                            dwc_read_reg32(&_core_if->core_global_regs->gintmsk));        
3237 +       }
3238 +}
3239 +
3240 +
3241 +/**
3242 + * This function does the setup for a data transfer for EP0 and starts
3243 + * the transfer.  For an IN transfer, the packets will be loaded into
3244 + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
3245 + * unloaded from the Rx FIFO in the ISR.
3246 + *
3247 + * @param _core_if Programming view of DWC_otg controller.
3248 + * @param _ep The EP0 data.
3249 + */
3250 +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
3251 +{
3252 +       volatile depctl_data_t depctl;
3253 +       volatile deptsiz0_data_t deptsiz;
3254 +        gintmsk_data_t intr_mask = { .d32 = 0};
3255 +
3256 +        DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
3257 +                    "xfer_buff=%p start_xfer_buff=%p total_len=%d\n",
3258 +                    _ep->num, (_ep->is_in?"IN":"OUT"), _ep->xfer_len, 
3259 +                    _ep->xfer_count, _ep->xfer_buff, _ep->start_xfer_buff,
3260 +                    _ep->total_len);
3261 +        _ep->total_len = _ep->xfer_len;
3262 +
3263 +       /* IN endpoint */
3264 +       if (_ep->is_in == 1) {
3265 +               dwc_otg_dev_in_ep_regs_t * in_regs = _core_if->dev_if->in_ep_regs[0];
3266 +               gnptxsts_data_t gtxstatus;
3267 +               gtxstatus.d32 = dwc_read_reg32(&_core_if->core_global_regs->gnptxsts);
3268 +               if (_core_if->en_multiple_tx_fifo == 0 &&
3269 +                       gtxstatus.b.nptxqspcavail == 0) {
3270 +#ifdef DEBUG
3271 +                        deptsiz.d32 = dwc_read_reg32(&in_regs->dieptsiz);
3272 +                        DWC_DEBUGPL(DBG_PCD,"DIEPCTL0=%0x\n", 
3273 +                                    dwc_read_reg32(&in_regs->diepctl));
3274 +                        DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n", 
3275 +                                    deptsiz.d32, deptsiz.b.xfersize,deptsiz.b.pktcnt);
3276 +                       DWC_PRINT("TX Queue or FIFO Full (0x%0x)\n", gtxstatus.d32);
3277 +#endif /*  */
3278 +                                               printk("TX Queue or FIFO Full!!!!\n"); // test-only
3279 +                        //return;
3280 +                        MDELAY(100); //james
3281 +                }
3282 +
3283 +                depctl.d32 = dwc_read_reg32(&in_regs->diepctl);
3284 +               deptsiz.d32 = dwc_read_reg32(&in_regs->dieptsiz);
3285 +
3286 +                /* Zero Length Packet? */
3287 +                if (_ep->xfer_len == 0) {
3288 +                        deptsiz.b.xfersize = 0;
3289 +                        deptsiz.b.pktcnt = 1;
3290 +                } else {
3291 +                        /* Program the transfer size and packet count
3292 +                         *  as follows: xfersize = N * maxpacket +
3293 +                         *  short_packet pktcnt = N + (short_packet
3294 +                         *  exist ? 1 : 0)  
3295 +                         */
3296 +                       if (_ep->xfer_len > _ep->maxpacket) {
3297 +                               _ep->xfer_len = _ep->maxpacket;
3298 +                               deptsiz.b.xfersize = _ep->maxpacket;
3299 +                       }
3300 +                       else {
3301 +                               deptsiz.b.xfersize = _ep->xfer_len;
3302 +                       }
3303 +                        deptsiz.b.pktcnt = 1;
3304 +
3305 +               }
3306 +                dwc_write_reg32(&in_regs->dieptsiz, deptsiz.d32);
3307 +                DWC_DEBUGPL(DBG_PCDV, "IN len=%d  xfersize=%d pktcnt=%d [%08x]\n",
3308 +                            _ep->xfer_len, deptsiz.b.xfersize,deptsiz.b.pktcnt, deptsiz.d32);
3309 +
3310 +               /* Write the DMA register */
3311 +               if (_core_if->dma_enable) {
3312 +                       dwc_write_reg32(&(in_regs->diepdma), (uint32_t) _ep->dma_addr);
3313 +               }
3314 +
3315 +               /* EP enable, IN data in FIFO */
3316 +               depctl.b.cnak = 1;
3317 +               depctl.b.epena = 1;
3318 +               dwc_write_reg32(&in_regs->diepctl, depctl.d32);
3319 +
3320 +                /** 
3321 +                 * Enable the Non-Periodic Tx FIFO empty interrupt, the
3322 +                 * data will be written into the fifo by the ISR.
3323 +                 */ 
3324 +                if (!_core_if->dma_enable) {
3325 +                       if (_core_if->en_multiple_tx_fifo == 0) {
3326 +                        intr_mask.b.nptxfempty = 1;
3327 +                               dwc_modify_reg32(&_core_if->core_global_regs->gintsts, intr_mask.d32, 0);
3328 +                               dwc_modify_reg32(&_core_if->core_global_regs->gintmsk, intr_mask.d32,
3329 +                                                 intr_mask.d32);
3330 +                       } else {
3331 +                           /* Enable the Tx FIFO Empty Interrupt for this EP */
3332 +                           if (_ep->xfer_len > 0) {
3333 +                                       uint32_t fifoemptymsk = 0;
3334 +                                       fifoemptymsk |= 1 << _ep->num;
3335 +                                       dwc_modify_reg32(&_core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
3336 +                                                0, fifoemptymsk);
3337 +                }
3338 +                
3339 +                       }
3340 +               }
3341 +       } else {
3342 +           /* OUT endpoint */
3343 +           dwc_otg_dev_out_ep_regs_t * out_regs = _core_if->dev_if->out_ep_regs[_ep->num];
3344 +
3345 +               depctl.d32 = dwc_read_reg32(&out_regs->doepctl);
3346 +               deptsiz.d32 = dwc_read_reg32(&out_regs->doeptsiz);
3347 +
3348 +               /* Program the transfer size and packet count as follows:
3349 +                *  xfersize = N * (maxpacket + 4 - (maxpacket % 4))
3350 +                *  pktcnt = N                                          */
3351 +                if (_ep->xfer_len == 0) {
3352 +                        /* Zero Length Packet */
3353 +                        deptsiz.b.xfersize = _ep->maxpacket;
3354 +                        deptsiz.b.pktcnt = 1;
3355 +                } else {
3356 +                       deptsiz.b.pktcnt = (_ep->xfer_len + (_ep->maxpacket - 1)) / _ep->maxpacket;
3357 +                        deptsiz.b.xfersize = deptsiz.b.pktcnt * _ep->maxpacket;
3358 +                }
3359 +                
3360 +               dwc_write_reg32(&out_regs->doeptsiz, deptsiz.d32);
3361 +                DWC_DEBUGPL(DBG_PCDV, "len=%d  xfersize=%d pktcnt=%d\n",
3362 +                            _ep->xfer_len, deptsiz.b.xfersize,deptsiz.b.pktcnt);
3363 +
3364 +               if (_core_if->dma_enable) {
3365 +                       dwc_write_reg32(&(out_regs->doepdma), (uint32_t) _ep->dma_addr);
3366 +               }
3367 +
3368 +               /* EP enable */
3369 +               depctl.b.cnak = 1;
3370 +               depctl.b.epena = 1;
3371 +               dwc_write_reg32 (&(out_regs->doepctl), depctl.d32);
3372 +       }
3373 +}
3374 +
3375 +/**
3376 + * This function continues control IN transfers started by
3377 + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
3378 + * single packet.  NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
3379 + * bit for the packet count.
3380 + *
3381 + * @param _core_if Programming view of DWC_otg controller.
3382 + * @param _ep The EP0 data.
3383 + */
3384 +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
3385 +{
3386 +       depctl_data_t depctl;
3387 +       deptsiz0_data_t deptsiz;
3388 +        gintmsk_data_t intr_mask = { .d32 = 0};
3389 +
3390 +       if (_ep->is_in == 1) {
3391 +               dwc_otg_dev_in_ep_regs_t *in_regs = 
3392 +                       _core_if->dev_if->in_ep_regs[0];
3393 +                gnptxsts_data_t tx_status = {.d32 = 0};
3394 +
3395 +                tx_status.d32 = dwc_read_reg32( &_core_if->core_global_regs->gnptxsts );
3396 +                /** @todo Should there be check for room in the Tx
3397 +                 * Status Queue.  If not remove the code above this comment. */
3398 +
3399 +                depctl.d32 = dwc_read_reg32(&in_regs->diepctl);
3400 +               deptsiz.d32 = dwc_read_reg32(&in_regs->dieptsiz);
3401 +
3402 +                /* Program the transfer size and packet count
3403 +                 *  as follows: xfersize = N * maxpacket +
3404 +                 *  short_packet pktcnt = N + (short_packet
3405 +                 *  exist ? 1 : 0)  
3406 +                 */
3407 +                deptsiz.b.xfersize = (_ep->total_len - _ep->xfer_count) > _ep->maxpacket ? _ep->maxpacket : 
3408 +                        (_ep->total_len - _ep->xfer_count);
3409 +                deptsiz.b.pktcnt = 1;
3410 +               _ep->xfer_len += deptsiz.b.xfersize;
3411 +
3412 +                dwc_write_reg32(&in_regs->dieptsiz, deptsiz.d32);
3413 +                DWC_DEBUGPL(DBG_PCDV, "IN len=%d  xfersize=%d pktcnt=%d [%08x]\n",
3414 +                            _ep->xfer_len, 
3415 +                            deptsiz.b.xfersize, deptsiz.b.pktcnt, deptsiz.d32);
3416 +
3417 +               /* Write the DMA register */
3418 +               if (_core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
3419 +                       dwc_write_reg32 (&(in_regs->diepdma), 
3420 +                                        CPHYSADDR((uint32_t)_ep->dma_addr)); // winder
3421 +               }
3422 +
3423 +               /* EP enable, IN data in FIFO */
3424 +               depctl.b.cnak = 1;
3425 +               depctl.b.epena = 1;
3426 +               dwc_write_reg32(&in_regs->diepctl, depctl.d32);
3427 +
3428 +                /** 
3429 +                 * Enable the Non-Periodic Tx FIFO empty interrupt, the
3430 +                 * data will be written into the fifo by the ISR.
3431 +                 */ 
3432 +                if (!_core_if->dma_enable) {
3433 +                        /* First clear it from GINTSTS */
3434 +                        intr_mask.b.nptxfempty = 1;
3435 +                        dwc_write_reg32( &_core_if->core_global_regs->gintsts,
3436 +                                         intr_mask.d32 );
3437 +
3438 +                        dwc_modify_reg32( &_core_if->core_global_regs->gintmsk,
3439 +                                          intr_mask.d32, intr_mask.d32);
3440 +                }
3441 +                
3442 +       } 
3443 +
3444 +}
3445 +
3446 +#ifdef DEBUG
3447 +void dump_msg(const u8 *buf, unsigned int length)
3448 +{
3449 +       unsigned int    start, num, i;
3450 +       char            line[52], *p;
3451 +
3452 +       if (length >= 512)
3453 +               return;
3454 +       start = 0;
3455 +       while (length > 0) {
3456 +               num = min(length, 16u);
3457 +               p = line;
3458 +               for (i = 0; i < num; ++i) {
3459 +                       if (i == 8)
3460 +                               *p++ = ' ';
3461 +                       sprintf(p, " %02x", buf[i]);
3462 +                       p += 3;
3463 +               }
3464 +               *p = 0;
3465 +               DWC_PRINT( "%6x: %s\n", start, line);
3466 +               buf += num;
3467 +               start += num;
3468 +               length -= num;
3469 +       }
3470 +}
3471 +#else
3472 +static inline void dump_msg(const u8 *buf, unsigned int length)
3473 +{
3474 +}
3475 +#endif
3476 +
3477 +/**
3478 + * This function writes a packet into the Tx FIFO associated with the
3479 + * EP.  For non-periodic EPs the non-periodic Tx FIFO is written.  For
3480 + * periodic EPs the periodic Tx FIFO associated with the EP is written
3481 + * with all packets for the next micro-frame.
3482 + *
3483 + * @param _core_if Programming view of DWC_otg controller.
3484 + * @param _ep The EP to write packet for.
3485 + * @param _dma Indicates if DMA is being used.
3486 + */
3487 +void dwc_otg_ep_write_packet(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep, int _dma)
3488 +{
3489 +       /**
3490 +        * The buffer is padded to DWORD on a per packet basis in
3491 +        * slave/dma mode if the MPS is not DWORD aligned.  The last
3492 +        * packet, if short, is also padded to a multiple of DWORD.
3493 +        *
3494 +        * ep->xfer_buff always starts DWORD aligned in memory and is a 
3495 +        * multiple of DWORD in length
3496 +        *
3497 +        * ep->xfer_len can be any number of bytes
3498 +        *
3499 +        * ep->xfer_count is a multiple of ep->maxpacket until the last 
3500 +        *  packet
3501 +        *
3502 +        * FIFO access is DWORD */
3503 +
3504 +       uint32_t i;
3505 +       uint32_t byte_count;
3506 +       uint32_t dword_count;
3507 +       uint32_t *fifo;
3508 +        uint32_t *data_buff = (uint32_t *)_ep->xfer_buff;
3509 +        
3510 +        //DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, _core_if, _ep);
3511 +        if (_ep->xfer_count >= _ep->xfer_len) {
3512 +                DWC_WARN("%s() No data for EP%d!!!\n", __func__, _ep->num);
3513 +                return;                
3514 +        }
3515 +
3516 +       /* Find the byte length of the packet either short packet or MPS */
3517 +       if ((_ep->xfer_len - _ep->xfer_count) < _ep->maxpacket) {
3518 +               byte_count = _ep->xfer_len - _ep->xfer_count;
3519 +       }
3520 +       else {
3521 +               byte_count = _ep->maxpacket;
3522 +       }
3523 +
3524 +       /* Find the DWORD length, padded by extra bytes as neccessary if MPS
3525 +        * is not a multiple of DWORD */
3526 +       dword_count =  (byte_count + 3) / 4;
3527 +
3528 +#ifdef VERBOSE
3529 +        dump_msg(_ep->xfer_buff, byte_count);        
3530 +#endif
3531 +        if (_ep->type == DWC_OTG_EP_TYPE_ISOC) {
3532 +                /**@todo NGS Where are the Periodic Tx FIFO addresses
3533 +                 * intialized?  What should this be? */
3534 +                fifo = _core_if->data_fifo[_ep->tx_fifo_num];
3535 +        } else {
3536 +                fifo = _core_if->data_fifo[_ep->num];
3537 +        }
3538 +        
3539 +        DWC_DEBUGPL((DBG_PCDV|DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
3540 +                    fifo, data_buff, *data_buff, byte_count);
3541 +        
3542 +
3543 +       if (!_dma) {
3544 +               for (i=0; i<dword_count; i++, data_buff++) {
3545 +                       dwc_write_reg32( fifo, *data_buff );
3546 +               }
3547 +       }
3548 +
3549 +       _ep->xfer_count += byte_count;
3550 +        _ep->xfer_buff += byte_count;
3551 +#if 1 // winder, why do we need this??
3552 +       _ep->dma_addr += byte_count;
3553 +#endif
3554 +}
3555 +
3556 +/** 
3557 + * Set the EP STALL.
3558 + *
3559 + * @param _core_if Programming view of DWC_otg controller.
3560 + * @param _ep The EP to set the stall on.
3561 + */
3562 +void dwc_otg_ep_set_stall(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
3563 +{
3564 +       depctl_data_t depctl;
3565 +       volatile uint32_t *depctl_addr;
3566 +
3567 +        DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, _ep->num, 
3568 +                 (_ep->is_in?"IN":"OUT"));
3569 +
3570 +       if (_ep->is_in == 1) {
3571 +               depctl_addr = &(_core_if->dev_if->in_ep_regs[_ep->num]->diepctl);
3572 +               depctl.d32 = dwc_read_reg32(depctl_addr);
3573 +
3574 +               /* set the disable and stall bits */
3575 +               if (depctl.b.epena) {
3576 +                        depctl.b.epdis = 1;
3577 +                }
3578 +               depctl.b.stall = 1;
3579 +               dwc_write_reg32(depctl_addr, depctl.d32);
3580 +
3581 +       } else {
3582 +               depctl_addr = &(_core_if->dev_if->out_ep_regs[_ep->num]->doepctl);
3583 +               depctl.d32 = dwc_read_reg32(depctl_addr);
3584 +
3585 +               /* set the stall bit */
3586 +               depctl.b.stall = 1;
3587 +               dwc_write_reg32(depctl_addr, depctl.d32);
3588 +       }
3589 +        DWC_DEBUGPL(DBG_PCD,"DEPCTL=%0x\n",dwc_read_reg32(depctl_addr));
3590 +       return;
3591 +}
3592 +
3593 +/** 
3594 + * Clear the EP STALL.
3595 + *
3596 + * @param _core_if Programming view of DWC_otg controller.
3597 + * @param _ep The EP to clear stall from.
3598 + */
3599 +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep)
3600 +{
3601 +       depctl_data_t depctl;
3602 +       volatile uint32_t *depctl_addr;
3603 +
3604 +        DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, _ep->num, 
3605 +                    (_ep->is_in?"IN":"OUT"));
3606 +
3607 +       if (_ep->is_in == 1) {
3608 +               depctl_addr = &(_core_if->dev_if->in_ep_regs[_ep->num]->diepctl);
3609 +       } else {
3610 +               depctl_addr = &(_core_if->dev_if->out_ep_regs[_ep->num]->doepctl);
3611 +       }
3612 +
3613 +       depctl.d32 = dwc_read_reg32(depctl_addr);
3614 +
3615 +       /* clear the stall bits */
3616 +       depctl.b.stall = 0;
3617 +
3618 +        /* 
3619 +         * USB Spec 9.4.5: For endpoints using data toggle, regardless
3620 +         * of whether an endpoint has the Halt feature set, a
3621 +         * ClearFeature(ENDPOINT_HALT) request always results in the
3622 +         * data toggle being reinitialized to DATA0.
3623 +         */
3624 +        if (_ep->type == DWC_OTG_EP_TYPE_INTR || 
3625 +            _ep->type == DWC_OTG_EP_TYPE_BULK) {
3626 +                depctl.b.setd0pid = 1; /* DATA0 */
3627 +        }
3628 +        
3629 +       dwc_write_reg32(depctl_addr, depctl.d32);
3630 +        DWC_DEBUGPL(DBG_PCD,"DEPCTL=%0x\n",dwc_read_reg32(depctl_addr));
3631 +       return;
3632 +}
3633 +
3634 +/** 
3635 + * This function reads a packet from the Rx FIFO into the destination
3636 + * buffer.  To read SETUP data use dwc_otg_read_setup_packet.
3637 + *
3638 + * @param _core_if Programming view of DWC_otg controller.
3639 + * @param _dest   Destination buffer for the packet.
3640 + * @param _bytes  Number of bytes to copy to the destination.
3641 + */
3642 +void dwc_otg_read_packet(dwc_otg_core_if_t *_core_if,
3643 +                        uint8_t *_dest, 
3644 +                        uint16_t _bytes)
3645 +{
3646 +       int i;
3647 +       int word_count = (_bytes + 3) / 4;
3648 +
3649 +       volatile uint32_t *fifo = _core_if->data_fifo[0];
3650 +       uint32_t *data_buff = (uint32_t *)_dest;
3651 +
3652 +       /**
3653 +        * @todo Account for the case where _dest is not dword aligned. This
3654 +        * requires reading data from the FIFO into a uint32_t temp buffer,
3655 +        * then moving it into the data buffer.
3656 +        */
3657 +
3658 +        DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__, 
3659 +                    _core_if, _dest, _bytes);
3660 +
3661 +       for (i=0; i<word_count; i++, data_buff++) {
3662 +               *data_buff = dwc_read_reg32(fifo);
3663 +       }
3664 +
3665 +       return;
3666 +}
3667 +
3668 +
3669 +#ifdef DEBUG
3670 +/**
3671 + * This functions reads the device registers and prints them
3672 + *
3673 + * @param _core_if Programming view of DWC_otg controller.
3674 + */
3675 +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t *_core_if)
3676 +{
3677 +       int i;
3678 +       volatile uint32_t *addr;
3679 +
3680 +       DWC_PRINT("Device Global Registers\n");
3681 +       addr=&_core_if->dev_if->dev_global_regs->dcfg;
3682 +       DWC_PRINT("DCFG      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3683 +       addr=&_core_if->dev_if->dev_global_regs->dctl;
3684 +       DWC_PRINT("DCTL      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3685 +       addr=&_core_if->dev_if->dev_global_regs->dsts;
3686 +       DWC_PRINT("DSTS      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3687 +       addr=&_core_if->dev_if->dev_global_regs->diepmsk;
3688 +       DWC_PRINT("DIEPMSK   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3689 +       addr=&_core_if->dev_if->dev_global_regs->doepmsk;
3690 +       DWC_PRINT("DOEPMSK   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3691 +       addr=&_core_if->dev_if->dev_global_regs->daint;
3692 +       DWC_PRINT("DAINT     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3693 +       addr=&_core_if->dev_if->dev_global_regs->dtknqr1;
3694 +       DWC_PRINT("DTKNQR1   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3695 +        if (_core_if->hwcfg2.b.dev_token_q_depth > 6) {
3696 +                addr=&_core_if->dev_if->dev_global_regs->dtknqr2;
3697 +                DWC_PRINT("DTKNQR2   @0x%08X : 0x%08X\n",
3698 +                          (uint32_t)addr,dwc_read_reg32(addr));
3699 +        }
3700 +        
3701 +       addr=&_core_if->dev_if->dev_global_regs->dvbusdis;
3702 +       DWC_PRINT("DVBUSID   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3703 +
3704 +       addr=&_core_if->dev_if->dev_global_regs->dvbuspulse;
3705 +       DWC_PRINT("DVBUSPULSE   @0x%08X : 0x%08X\n",
3706 +                  (uint32_t)addr,dwc_read_reg32(addr));
3707 +
3708 +        if (_core_if->hwcfg2.b.dev_token_q_depth > 14) {
3709 +               addr = &_core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
3710 +                DWC_PRINT("DTKNQR3   @0x%08X : 0x%08X\n",
3711 +                          (uint32_t)addr, dwc_read_reg32(addr));
3712 +        }
3713 +
3714 +        if (_core_if->hwcfg2.b.dev_token_q_depth > 22) {
3715 +               addr = &_core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
3716 +               DWC_PRINT("DTKNQR4       @0x%08X : 0x%08X\n", (uint32_t) addr,
3717 +                          dwc_read_reg32(addr));
3718 +       }
3719 +       for (i = 0; i <= _core_if->dev_if->num_in_eps; i++) {
3720 +               DWC_PRINT("Device IN EP %d Registers\n", i);
3721 +               addr=&_core_if->dev_if->in_ep_regs[i]->diepctl;
3722 +               DWC_PRINT("DIEPCTL   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3723 +               addr=&_core_if->dev_if->in_ep_regs[i]->diepint;
3724 +               DWC_PRINT("DIEPINT   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3725 +               addr=&_core_if->dev_if->in_ep_regs[i]->dieptsiz;
3726 +               DWC_PRINT("DIETSIZ   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3727 +               addr=&_core_if->dev_if->in_ep_regs[i]->diepdma;
3728 +               DWC_PRINT("DIEPDMA   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3729 +               
3730 +addr = &_core_if->dev_if->in_ep_regs[i]->dtxfsts;
3731 +               DWC_PRINT("DTXFSTS       @0x%08X : 0x%08X\n", (uint32_t) addr,
3732 +                          dwc_read_reg32(addr));
3733 +       }
3734 +       for (i = 0; i <= _core_if->dev_if->num_out_eps; i++) {
3735 +               DWC_PRINT("Device OUT EP %d Registers\n", i);
3736 +               addr=&_core_if->dev_if->out_ep_regs[i]->doepctl;
3737 +               DWC_PRINT("DOEPCTL   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3738 +               addr=&_core_if->dev_if->out_ep_regs[i]->doepfn;
3739 +               DWC_PRINT("DOEPFN    @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3740 +               addr=&_core_if->dev_if->out_ep_regs[i]->doepint;
3741 +               DWC_PRINT("DOEPINT   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3742 +               addr=&_core_if->dev_if->out_ep_regs[i]->doeptsiz;
3743 +               DWC_PRINT("DOETSIZ   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3744 +               addr=&_core_if->dev_if->out_ep_regs[i]->doepdma;
3745 +               DWC_PRINT("DOEPDMA   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3746 +       }
3747 +       return;
3748 +}
3749 +
3750 +/**
3751 + * This function reads the host registers and prints them
3752 + *
3753 + * @param _core_if Programming view of DWC_otg controller.
3754 + */
3755 +void dwc_otg_dump_host_registers(dwc_otg_core_if_t *_core_if)
3756 +{
3757 +       int i;
3758 +       volatile uint32_t *addr;
3759 +
3760 +       DWC_PRINT("Host Global Registers\n");
3761 +       addr=&_core_if->host_if->host_global_regs->hcfg;
3762 +       DWC_PRINT("HCFG      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3763 +       addr=&_core_if->host_if->host_global_regs->hfir;
3764 +       DWC_PRINT("HFIR      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3765 +       addr=&_core_if->host_if->host_global_regs->hfnum;
3766 +       DWC_PRINT("HFNUM     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3767 +       addr=&_core_if->host_if->host_global_regs->hptxsts;
3768 +       DWC_PRINT("HPTXSTS   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3769 +       addr=&_core_if->host_if->host_global_regs->haint;
3770 +       DWC_PRINT("HAINT     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3771 +       addr=&_core_if->host_if->host_global_regs->haintmsk;
3772 +       DWC_PRINT("HAINTMSK  @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3773 +       addr=_core_if->host_if->hprt0;
3774 +       DWC_PRINT("HPRT0     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3775 +
3776 +       for (i=0; i<_core_if->core_params->host_channels; i++) {
3777 +               DWC_PRINT("Host Channel %d Specific Registers\n", i);
3778 +               addr=&_core_if->host_if->hc_regs[i]->hcchar;
3779 +               DWC_PRINT("HCCHAR    @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3780 +               addr=&_core_if->host_if->hc_regs[i]->hcsplt;
3781 +               DWC_PRINT("HCSPLT    @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3782 +               addr=&_core_if->host_if->hc_regs[i]->hcint;
3783 +               DWC_PRINT("HCINT     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3784 +               addr=&_core_if->host_if->hc_regs[i]->hcintmsk;
3785 +               DWC_PRINT("HCINTMSK  @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3786 +               addr=&_core_if->host_if->hc_regs[i]->hctsiz;
3787 +               DWC_PRINT("HCTSIZ    @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3788 +               addr=&_core_if->host_if->hc_regs[i]->hcdma;
3789 +               DWC_PRINT("HCDMA     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3790 +
3791 +       }
3792 +       return;
3793 +}
3794 +
3795 +/**
3796 + * This function reads the core global registers and prints them
3797 + *
3798 + * @param _core_if Programming view of DWC_otg controller.
3799 + */
3800 +void dwc_otg_dump_global_registers(dwc_otg_core_if_t *_core_if)
3801 +{
3802 +       int i;
3803 +       volatile uint32_t *addr;
3804 +
3805 +       DWC_PRINT("Core Global Registers\n");
3806 +       addr=&_core_if->core_global_regs->gotgctl;
3807 +       DWC_PRINT("GOTGCTL   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3808 +       addr=&_core_if->core_global_regs->gotgint;
3809 +       DWC_PRINT("GOTGINT   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3810 +       addr=&_core_if->core_global_regs->gahbcfg;
3811 +       DWC_PRINT("GAHBCFG   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3812 +       addr=&_core_if->core_global_regs->gusbcfg;
3813 +       DWC_PRINT("GUSBCFG   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3814 +       addr=&_core_if->core_global_regs->grstctl;
3815 +       DWC_PRINT("GRSTCTL   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3816 +       addr=&_core_if->core_global_regs->gintsts;
3817 +       DWC_PRINT("GINTSTS   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3818 +       addr=&_core_if->core_global_regs->gintmsk;
3819 +       DWC_PRINT("GINTMSK   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3820 +       addr=&_core_if->core_global_regs->grxstsr;
3821 +       DWC_PRINT("GRXSTSR   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3822 +       //addr=&_core_if->core_global_regs->grxstsp;
3823 +       //DWC_PRINT("GRXSTSP   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3824 +       addr=&_core_if->core_global_regs->grxfsiz;
3825 +       DWC_PRINT("GRXFSIZ   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3826 +       addr=&_core_if->core_global_regs->gnptxfsiz;
3827 +       DWC_PRINT("GNPTXFSIZ @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3828 +       addr=&_core_if->core_global_regs->gnptxsts;
3829 +       DWC_PRINT("GNPTXSTS  @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3830 +       addr=&_core_if->core_global_regs->gi2cctl;
3831 +       DWC_PRINT("GI2CCTL   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3832 +       addr=&_core_if->core_global_regs->gpvndctl;
3833 +       DWC_PRINT("GPVNDCTL  @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3834 +       addr=&_core_if->core_global_regs->ggpio;
3835 +       DWC_PRINT("GGPIO     @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3836 +       addr=&_core_if->core_global_regs->guid;
3837 +       DWC_PRINT("GUID      @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3838 +       addr=&_core_if->core_global_regs->gsnpsid;
3839 +       DWC_PRINT("GSNPSID   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3840 +       addr=&_core_if->core_global_regs->ghwcfg1;
3841 +       DWC_PRINT("GHWCFG1   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3842 +       addr=&_core_if->core_global_regs->ghwcfg2;
3843 +       DWC_PRINT("GHWCFG2   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3844 +       addr=&_core_if->core_global_regs->ghwcfg3;
3845 +       DWC_PRINT("GHWCFG3   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3846 +       addr=&_core_if->core_global_regs->ghwcfg4;
3847 +       DWC_PRINT("GHWCFG4   @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3848 +       addr=&_core_if->core_global_regs->hptxfsiz;
3849 +       DWC_PRINT("HPTXFSIZ  @0x%08X : 0x%08X\n",(uint32_t)addr,dwc_read_reg32(addr));
3850 +
3851 +        for (i=0; i<_core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
3852 +               addr=&_core_if->core_global_regs->dptxfsiz_dieptxf[i];
3853 +               DWC_PRINT("DPTXFSIZ[%d] @0x%08X : 0x%08X\n",i,(uint32_t)addr,dwc_read_reg32(addr));
3854 +       }
3855 +
3856 +}
3857 +#endif
3858 +
3859 +/**
3860 + * Flush a Tx FIFO.
3861 + *
3862 + * @param _core_if Programming view of DWC_otg controller.
3863 + * @param _num Tx FIFO to flush.
3864 + */
3865 +extern void dwc_otg_flush_tx_fifo( dwc_otg_core_if_t *_core_if, 
3866 +                                   const int _num ) 
3867 +{
3868 +        dwc_otg_core_global_regs_t *global_regs = _core_if->core_global_regs;
3869 +        volatile grstctl_t greset = { .d32 = 0};
3870 +        int count = 0;
3871 +        
3872 +        DWC_DEBUGPL((DBG_CIL|DBG_PCDV), "Flush Tx FIFO %d\n", _num);
3873 +
3874 +        greset.b.txfflsh = 1;
3875 +        greset.b.txfnum = _num;
3876 +        dwc_write_reg32( &global_regs->grstctl, greset.d32 );
3877 +        
3878 +        do {
3879 +                greset.d32 = dwc_read_reg32( &global_regs->grstctl);
3880 +                if (++count > 10000){
3881 +                        DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
3882 +                                  __func__, greset.d32,
3883 +                                  dwc_read_reg32( &global_regs->gnptxsts));
3884 +                        break;
3885 +                }
3886 +
3887 +               udelay(1);
3888 +        } while (greset.b.txfflsh == 1);
3889 +        /* Wait for 3 PHY Clocks*/
3890 +        UDELAY(1);
3891 +}
3892 +
3893 +/**
3894 + * Flush Rx FIFO.
3895 + *
3896 + * @param _core_if Programming view of DWC_otg controller.
3897 + */
3898 +extern void dwc_otg_flush_rx_fifo( dwc_otg_core_if_t *_core_if ) 
3899 +{
3900 +        dwc_otg_core_global_regs_t *global_regs = _core_if->core_global_regs;
3901 +        volatile grstctl_t greset = { .d32 = 0};
3902 +        int count = 0;
3903 +        
3904 +        DWC_DEBUGPL((DBG_CIL|DBG_PCDV), "%s\n", __func__);
3905 +        /*
3906 +         * 
3907 +         */
3908 +        greset.b.rxfflsh = 1;
3909 +        dwc_write_reg32( &global_regs->grstctl, greset.d32 );
3910 +        
3911 +        do {
3912 +                greset.d32 = dwc_read_reg32( &global_regs->grstctl);
3913 +                if (++count > 10000){
3914 +                        DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__, 
3915 +                                 greset.d32);
3916 +                        break;
3917 +                }
3918 +        } while (greset.b.rxfflsh == 1);        
3919 +        /* Wait for 3 PHY Clocks*/
3920 +        UDELAY(1);
3921 +}
3922 +
3923 +/**
3924 + * Do core a soft reset of the core.  Be careful with this because it
3925 + * resets all the internal state machines of the core.
3926 + */
3927 +
3928 +void dwc_otg_core_reset(dwc_otg_core_if_t *_core_if)
3929 +{
3930 +       dwc_otg_core_global_regs_t *global_regs = _core_if->core_global_regs;
3931 +       volatile grstctl_t greset = { .d32 = 0};
3932 +       int count = 0;
3933 +
3934 +       DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
3935 +       /* Wait for AHB master IDLE state. */
3936 +       do {
3937 +               UDELAY(10);
3938 +               greset.d32 = dwc_read_reg32( &global_regs->grstctl);
3939 +               if (++count > 100000){
3940 +                       DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x %x\n", __func__, 
3941 +                       greset.d32, greset.b.ahbidle);
3942 +                       return;
3943 +               }
3944 +       } while (greset.b.ahbidle == 0);
3945 +        
3946 +// winder add.
3947 +#if 1
3948 +       /* Note: Actually, I don't exactly why we need to put delay here. */
3949 +       MDELAY(100);
3950 +#endif
3951 +       /* Core Soft Reset */
3952 +       count = 0;
3953 +       greset.b.csftrst = 1;
3954 +       dwc_write_reg32( &global_regs->grstctl, greset.d32 );
3955 +// winder add.
3956 +#if 1
3957 +       /* Note: Actually, I don't exactly why we need to put delay here. */
3958 +       MDELAY(100);
3959 +#endif
3960 +       do {
3961 +               greset.d32 = dwc_read_reg32( &global_regs->grstctl);
3962 +               if (++count > 10000){
3963 +                       DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n", __func__, 
3964 +                               greset.d32);
3965 +                       break;
3966 +               }
3967 +               udelay(1);
3968 +       } while (greset.b.csftrst == 1);        
3969 +       /* Wait for 3 PHY Clocks*/
3970 +       //DWC_PRINT("100ms\n");
3971 +       MDELAY(100);
3972 +}
3973 +
3974 +
3975 +
3976 +/**
3977 + * Register HCD callbacks.  The callbacks are used to start and stop
3978 + * the HCD for interrupt processing.
3979 + *
3980 + * @param _core_if Programming view of DWC_otg controller.
3981 + * @param _cb the HCD callback structure.
3982 + * @param _p pointer to be passed to callback function (usb_hcd*).
3983 + */
3984 +extern void dwc_otg_cil_register_hcd_callbacks( dwc_otg_core_if_t *_core_if,
3985 +                                                dwc_otg_cil_callbacks_t *_cb,
3986 +                                                void *_p)
3987 +{
3988 +        _core_if->hcd_cb = _cb;        
3989 +        _cb->p = _p;        
3990 +}
3991 +
3992 +/**
3993 + * Register PCD callbacks.  The callbacks are used to start and stop
3994 + * the PCD for interrupt processing.
3995 + *
3996 + * @param _core_if Programming view of DWC_otg controller.
3997 + * @param _cb the PCD callback structure.
3998 + * @param _p pointer to be passed to callback function (pcd*).
3999 + */
4000 +extern void dwc_otg_cil_register_pcd_callbacks( dwc_otg_core_if_t *_core_if,
4001 +                                                dwc_otg_cil_callbacks_t *_cb,
4002 +                                                void *_p)
4003 +{
4004 +        _core_if->pcd_cb = _cb;
4005 +        _cb->p = _p;
4006 +}
4007 +
4008 --- /dev/null
4009 +++ b/drivers/usb/dwc_otg/dwc_otg_cil.h
4010 @@ -0,0 +1,911 @@
4011 +/* ==========================================================================
4012 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_cil.h $
4013 + * $Revision: 1.1.1.1 $
4014 + * $Date: 2009-04-17 06:15:34 $
4015 + * $Change: 631780 $
4016 + *
4017 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
4018 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
4019 + * otherwise expressly agreed to in writing between Synopsys and you.
4020 + * 
4021 + * The Software IS NOT an item of Licensed Software or Licensed Product under
4022 + * any End User Software License Agreement or Agreement for Licensed Product
4023 + * with Synopsys or any supplement thereto. You are permitted to use and
4024 + * redistribute this Software in source and binary forms, with or without
4025 + * modification, provided that redistributions of source code must retain this
4026 + * notice. You may not view, use, disclose, copy or distribute this file or
4027 + * any information contained herein except pursuant to this license grant from
4028 + * Synopsys. If you do not agree with this notice, including the disclaimer
4029 + * below, then you are not authorized to use the Software.
4030 + * 
4031 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
4032 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4033 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4034 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
4035 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
4036 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
4037 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
4038 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4039 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4040 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
4041 + * DAMAGE.
4042 + * ========================================================================== */
4043 +
4044 +#if !defined(__DWC_CIL_H__)
4045 +#define __DWC_CIL_H__
4046 +
4047 +#include "dwc_otg_plat.h"
4048 +
4049 +#include "dwc_otg_regs.h"
4050 +#ifdef DEBUG
4051 +#include "linux/timer.h"
4052 +#endif
4053 +
4054 +/* the OTG capabilities. */
4055 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
4056 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
4057 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
4058 +/* the maximum speed of operation in host and device mode. */
4059 +#define DWC_SPEED_PARAM_HIGH 0
4060 +#define DWC_SPEED_PARAM_FULL 1
4061 +/* the PHY clock rate in low power mode when connected to a
4062 + * Low Speed device in host mode. */
4063 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
4064 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
4065 +/* the type of PHY interface to use. */
4066 +#define DWC_PHY_TYPE_PARAM_FS 0
4067 +#define DWC_PHY_TYPE_PARAM_UTMI 1
4068 +#define DWC_PHY_TYPE_PARAM_ULPI 2
4069 +/* whether to use the internal or external supply to 
4070 + * drive the vbus with a ULPI phy. */
4071 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
4072 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
4073 +/* EP type. */
4074 +
4075 +/**
4076 + * @file
4077 + * This file contains the interface to the Core Interface Layer.
4078 + */
4079 +
4080 +/**
4081 + * The <code>dwc_ep</code> structure represents the state of a single
4082 + * endpoint when acting in device mode. It contains the data items
4083 + * needed for an endpoint to be activated and transfer packets.
4084 + */
4085 +typedef struct dwc_ep {
4086 +        /** EP number used for register address lookup */
4087 +        uint8_t  num;
4088 +        /** EP direction 0 = OUT */
4089 +        unsigned is_in : 1;           
4090 +        /** EP active. */
4091 +        unsigned active : 1;
4092 +
4093 +       /** Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic Tx FIFO
4094 +               If dedicated Tx FIFOs are enabled for all IN Eps - Tx FIFO # FOR IN EPs*/
4095 +        unsigned tx_fifo_num : 4;  
4096 +        /** EP type: 0 - Control, 1 - ISOC,  2 - BULK,  3 - INTR */
4097 +        unsigned type : 2;      
4098 +#define DWC_OTG_EP_TYPE_CONTROL    0
4099 +#define DWC_OTG_EP_TYPE_ISOC       1
4100 +#define DWC_OTG_EP_TYPE_BULK       2
4101 +#define DWC_OTG_EP_TYPE_INTR       3
4102 +
4103 +        /** DATA start PID for INTR and BULK EP */
4104 +        unsigned data_pid_start : 1;  
4105 +        /** Frame (even/odd) for ISOC EP */
4106 +        unsigned even_odd_frame : 1;  
4107 +        /** Max Packet bytes */
4108 +        unsigned maxpacket : 11;
4109 +
4110 +        /** @name Transfer state */
4111 +       /** @{ */
4112 +
4113 +       /**
4114 +        * Pointer to the beginning of the transfer buffer -- do not modify
4115 +        * during transfer.
4116 +        */
4117 +       
4118 +       uint32_t dma_addr;
4119 +
4120 +       uint8_t *start_xfer_buff;
4121 +        /** pointer to the transfer buffer */
4122 +        uint8_t *xfer_buff;          
4123 +        /** Number of bytes to transfer */
4124 +        unsigned xfer_len : 19;       
4125 +        /** Number of bytes transferred. */
4126 +        unsigned xfer_count : 19;
4127 +        /** Sent ZLP */
4128 +        unsigned sent_zlp : 1;
4129 +        /** Total len for control transfer */
4130 +        unsigned total_len : 19;
4131 +
4132 +               /** stall clear flag */
4133 +               unsigned stall_clear_flag : 1;
4134 +
4135 +       /** @} */
4136 +} dwc_ep_t;
4137 +
4138 +/*
4139 + * Reasons for halting a host channel.
4140 + */
4141 +typedef enum dwc_otg_halt_status {
4142 +       DWC_OTG_HC_XFER_NO_HALT_STATUS,
4143 +       DWC_OTG_HC_XFER_COMPLETE,
4144 +       DWC_OTG_HC_XFER_URB_COMPLETE,
4145 +       DWC_OTG_HC_XFER_ACK,
4146 +       DWC_OTG_HC_XFER_NAK,
4147 +       DWC_OTG_HC_XFER_NYET,
4148 +       DWC_OTG_HC_XFER_STALL,
4149 +       DWC_OTG_HC_XFER_XACT_ERR,
4150 +       DWC_OTG_HC_XFER_FRAME_OVERRUN,
4151 +       DWC_OTG_HC_XFER_BABBLE_ERR,
4152 +       DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
4153 +       DWC_OTG_HC_XFER_AHB_ERR,
4154 +       DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
4155 +       DWC_OTG_HC_XFER_URB_DEQUEUE
4156 +} dwc_otg_halt_status_e;
4157 +       
4158 +/**
4159 + * Host channel descriptor. This structure represents the state of a single
4160 + * host channel when acting in host mode. It contains the data items needed to
4161 + * transfer packets to an endpoint via a host channel.
4162 + */
4163 +typedef struct dwc_hc {
4164 +       /** Host channel number used for register address lookup */
4165 +       uint8_t  hc_num;
4166 +
4167 +       /** Device to access */
4168 +       unsigned dev_addr : 7;
4169 +
4170 +       /** EP to access */
4171 +       unsigned ep_num : 4;
4172 +
4173 +       /** EP direction. 0: OUT, 1: IN */
4174 +       unsigned ep_is_in : 1;
4175 +
4176 +       /**
4177 +        * EP speed.
4178 +        * One of the following values:
4179 +        *      - DWC_OTG_EP_SPEED_LOW
4180 +        *      - DWC_OTG_EP_SPEED_FULL
4181 +        *      - DWC_OTG_EP_SPEED_HIGH
4182 +        */
4183 +       unsigned speed : 2;
4184 +#define DWC_OTG_EP_SPEED_LOW   0
4185 +#define DWC_OTG_EP_SPEED_FULL  1
4186 +#define DWC_OTG_EP_SPEED_HIGH  2       
4187 +
4188 +       /**
4189 +        * Endpoint type.
4190 +        * One of the following values:
4191 +        *      - DWC_OTG_EP_TYPE_CONTROL: 0
4192 +        *      - DWC_OTG_EP_TYPE_ISOC: 1
4193 +        *      - DWC_OTG_EP_TYPE_BULK: 2
4194 +        *      - DWC_OTG_EP_TYPE_INTR: 3
4195 +        */
4196 +       unsigned ep_type : 2;
4197 +
4198 +       /** Max packet size in bytes */
4199 +       unsigned max_packet : 11;
4200 +
4201 +       /**
4202 +        * PID for initial transaction.
4203 +        * 0: DATA0,<br>
4204 +        * 1: DATA2,<br>
4205 +        * 2: DATA1,<br>
4206 +        * 3: MDATA (non-Control EP),
4207 +        *    SETUP (Control EP)
4208 +        */
4209 +       unsigned data_pid_start : 2;
4210 +#define DWC_OTG_HC_PID_DATA0 0
4211 +#define DWC_OTG_HC_PID_DATA2 1
4212 +#define DWC_OTG_HC_PID_DATA1 2
4213 +#define DWC_OTG_HC_PID_MDATA 3
4214 +#define DWC_OTG_HC_PID_SETUP 3
4215 +
4216 +       /** Number of periodic transactions per (micro)frame */
4217 +       unsigned multi_count: 2;
4218 +
4219 +       /** @name Transfer State */
4220 +       /** @{ */
4221 +
4222 +       /** Pointer to the current transfer buffer position. */
4223 +       uint8_t *xfer_buff;
4224 +       /** Total number of bytes to transfer. */
4225 +       uint32_t xfer_len;
4226 +       /** Number of bytes transferred so far. */
4227 +       uint32_t xfer_count;
4228 +       /** Packet count at start of transfer.*/
4229 +       uint16_t start_pkt_count;
4230 +
4231 +       /**
4232 +        * Flag to indicate whether the transfer has been started. Set to 1 if
4233 +        * it has been started, 0 otherwise.
4234 +        */
4235 +       uint8_t xfer_started;
4236 +
4237 +       /**
4238 +        * Set to 1 to indicate that a PING request should be issued on this
4239 +        * channel. If 0, process normally.
4240 +        */
4241 +       uint8_t do_ping;
4242 +
4243 +       /**
4244 +        * Set to 1 to indicate that the error count for this transaction is
4245 +        * non-zero. Set to 0 if the error count is 0.
4246 +        */
4247 +       uint8_t error_state;
4248 +
4249 +       /**
4250 +        * Set to 1 to indicate that this channel should be halted the next
4251 +        * time a request is queued for the channel. This is necessary in
4252 +        * slave mode if no request queue space is available when an attempt
4253 +        * is made to halt the channel.
4254 +        */
4255 +       uint8_t halt_on_queue;
4256 +
4257 +       /**
4258 +        * Set to 1 if the host channel has been halted, but the core is not
4259 +        * finished flushing queued requests. Otherwise 0.
4260 +        */
4261 +       uint8_t halt_pending;
4262 +
4263 +       /**
4264 +        * Reason for halting the host channel.
4265 +        */
4266 +       dwc_otg_halt_status_e   halt_status;
4267 +
4268 +       /*
4269 +        * Split settings for the host channel
4270 +        */
4271 +       uint8_t do_split;          /**< Enable split for the channel */
4272 +       uint8_t complete_split;    /**< Enable complete split */
4273 +       uint8_t hub_addr;          /**< Address of high speed hub */
4274 +
4275 +       uint8_t port_addr;         /**< Port of the low/full speed device */
4276 +       /** Split transaction position 
4277 +        * One of the following values:
4278 +        *    - DWC_HCSPLIT_XACTPOS_MID 
4279 +        *    - DWC_HCSPLIT_XACTPOS_BEGIN
4280 +        *    - DWC_HCSPLIT_XACTPOS_END
4281 +        *    - DWC_HCSPLIT_XACTPOS_ALL */
4282 +       uint8_t xact_pos;
4283 +
4284 +       /** Set when the host channel does a short read. */
4285 +       uint8_t short_read;
4286 +
4287 +       /**
4288 +        * Number of requests issued for this channel since it was assigned to
4289 +        * the current transfer (not counting PINGs).
4290 +        */
4291 +       uint8_t requests;
4292 +
4293 +       /**
4294 +        * Queue Head for the transfer being processed by this channel.
4295 +        */
4296 +       struct dwc_otg_qh *qh;
4297 +
4298 +       /** @} */
4299 +
4300 +       /** Entry in list of host channels. */
4301 +       struct list_head        hc_list_entry;
4302 +} dwc_hc_t;
4303 +
4304 +/**
4305 + * The following parameters may be specified when starting the module. These
4306 + * parameters define how the DWC_otg controller should be configured.
4307 + * Parameter values are passed to the CIL initialization function
4308 + * dwc_otg_cil_init.
4309 + */
4310 +
4311 +typedef struct dwc_otg_core_params 
4312 +{
4313 +       int32_t opt;
4314 +//#define dwc_param_opt_default 1
4315 +        /**
4316 +        * Specifies the OTG capabilities. The driver will automatically
4317 +        * detect the value for this parameter if none is specified.
4318 +         * 0 - HNP and SRP capable (default)
4319 +         * 1 - SRP Only capable
4320 +         * 2 - No HNP/SRP capable
4321 +         */
4322 +        int32_t otg_cap;
4323 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
4324 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
4325 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
4326 +//#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
4327 +       /**
4328 +         * Specifies whether to use slave or DMA mode for accessing the data
4329 +         * FIFOs. The driver will automatically detect the value for this
4330 +         * parameter if none is specified.
4331 +         * 0 - Slave
4332 +         * 1 - DMA (default, if available)
4333 +         */
4334 +       int32_t dma_enable;
4335 +//#define dwc_param_dma_enable_default 1
4336 +       /** The DMA Burst size (applicable only for External DMA
4337 +         * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
4338 +         */
4339 +        int32_t dma_burst_size;  /* Translate this to GAHBCFG values */
4340 +//#define dwc_param_dma_burst_size_default 32
4341 +       /**
4342 +        * Specifies the maximum speed of operation in host and device mode.
4343 +        * The actual speed depends on the speed of the attached device and
4344 +        * the value of phy_type. The actual speed depends on the speed of the
4345 +        * attached device.
4346 +        * 0 - High Speed (default)
4347 +        * 1 - Full Speed
4348 +        */
4349 +        int32_t speed;
4350 +//#define dwc_param_speed_default 0
4351 +#define DWC_SPEED_PARAM_HIGH 0
4352 +#define DWC_SPEED_PARAM_FULL 1
4353 +
4354 +       /** Specifies whether low power mode is supported when attached 
4355 +        *  to a Full Speed or Low Speed device in host mode.
4356 +        * 0 - Don't support low power mode (default)
4357 +        * 1 - Support low power mode
4358 +        */
4359 +       int32_t host_support_fs_ls_low_power;
4360 +//#define dwc_param_host_support_fs_ls_low_power_default 0
4361 +       /** Specifies the PHY clock rate in low power mode when connected to a
4362 +        * Low Speed device in host mode. This parameter is applicable only if
4363 +        * HOST_SUPPORT_FS_LS_LOW_POWER is enabled.  If PHY_TYPE is set to FS
4364 +        * then defaults to 6 MHZ otherwise 48 MHZ.
4365 +        *
4366 +        * 0 - 48 MHz
4367 +        * 1 - 6 MHz
4368 +        */
4369 +       int32_t host_ls_low_power_phy_clk;
4370 +//#define dwc_param_host_ls_low_power_phy_clk_default 0
4371 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
4372 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
4373 +       /**
4374 +        * 0 - Use cC FIFO size parameters
4375 +        * 1 - Allow dynamic FIFO sizing (default)
4376 +        */
4377 +       int32_t enable_dynamic_fifo;
4378 +//#define dwc_param_enable_dynamic_fifo_default 1
4379 +       /** Total number of 4-byte words in the data FIFO memory. This 
4380 +        * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic 
4381 +        * Tx FIFOs.
4382 +        * 32 to 32768 (default 8192)
4383 +        * Note: The total FIFO memory depth in the FPGA configuration is 8192.
4384 +        */
4385 +       int32_t data_fifo_size;
4386 +//#define dwc_param_data_fifo_size_default 8192
4387 +       /** Number of 4-byte words in the Rx FIFO in device mode when dynamic 
4388 +        * FIFO sizing is enabled.
4389 +        * 16 to 32768 (default 1064)
4390 +        */
4391 +       int32_t dev_rx_fifo_size;
4392 +//#define dwc_param_dev_rx_fifo_size_default 1064
4393 +       /** Number of 4-byte words in the non-periodic Tx FIFO in device mode 
4394 +        * when dynamic FIFO sizing is enabled.
4395 +        * 16 to 32768 (default 1024)
4396 +        */
4397 +       int32_t dev_nperio_tx_fifo_size;
4398 +//#define dwc_param_dev_nperio_tx_fifo_size_default 1024
4399 +       /** Number of 4-byte words in each of the periodic Tx FIFOs in device
4400 +        * mode when dynamic FIFO sizing is enabled.
4401 +        * 4 to 768 (default 256)
4402 +        */
4403 +       uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
4404 +//#define dwc_param_dev_perio_tx_fifo_size_default 256
4405 +       /** Number of 4-byte words in the Rx FIFO in host mode when dynamic 
4406 +        * FIFO sizing is enabled.
4407 +        * 16 to 32768 (default 1024)  
4408 +        */
4409 +       int32_t host_rx_fifo_size;
4410 +//#define dwc_param_host_rx_fifo_size_default 1024
4411 +        /** Number of 4-byte words in the non-periodic Tx FIFO in host mode 
4412 +        * when Dynamic FIFO sizing is enabled in the core. 
4413 +        * 16 to 32768 (default 1024)
4414 +        */
4415 +       int32_t host_nperio_tx_fifo_size;
4416 +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
4417 +       /** Number of 4-byte words in the host periodic Tx FIFO when dynamic 
4418 +        * FIFO sizing is enabled. 
4419 +        * 16 to 32768 (default 1024)
4420 +        */
4421 +       int32_t host_perio_tx_fifo_size;
4422 +//#define dwc_param_host_perio_tx_fifo_size_default 1024
4423 +       /** The maximum transfer size supported in bytes.  
4424 +        * 2047 to 65,535  (default 65,535)
4425 +        */
4426 +       int32_t max_transfer_size;
4427 +//#define dwc_param_max_transfer_size_default 65535
4428 +       /** The maximum number of packets in a transfer.  
4429 +        * 15 to 511  (default 511)
4430 +        */
4431 +       int32_t max_packet_count;
4432 +//#define dwc_param_max_packet_count_default 511
4433 +       /** The number of host channel registers to use.  
4434 +        * 1 to 16 (default 12) 
4435 +        * Note: The FPGA configuration supports a maximum of 12 host channels.
4436 +        */
4437 +       int32_t host_channels;
4438 +//#define dwc_param_host_channels_default 12
4439 +       /** The number of endpoints in addition to EP0 available for device 
4440 +        * mode operations. 
4441 +        * 1 to 15 (default 6 IN and OUT) 
4442 +        * Note: The FPGA configuration supports a maximum of 6 IN and OUT 
4443 +        * endpoints in addition to EP0.
4444 +        */
4445 +       int32_t dev_endpoints;
4446 +//#define dwc_param_dev_endpoints_default 6
4447 +        /** 
4448 +         * Specifies the type of PHY interface to use. By default, the driver
4449 +         * will automatically detect the phy_type.
4450 +         * 
4451 +         * 0 - Full Speed PHY
4452 +         * 1 - UTMI+ (default)
4453 +         * 2 - ULPI
4454 +         */
4455 +       int32_t phy_type; 
4456 +#define DWC_PHY_TYPE_PARAM_FS 0
4457 +#define DWC_PHY_TYPE_PARAM_UTMI 1
4458 +#define DWC_PHY_TYPE_PARAM_ULPI 2
4459 +//#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
4460 +       /**
4461 +         * Specifies the UTMI+ Data Width.  This parameter is
4462 +         * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
4463 +         * PHY_TYPE, this parameter indicates the data width between
4464 +         * the MAC and the ULPI Wrapper.) Also, this parameter is
4465 +         * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
4466 +         * to "8 and 16 bits", meaning that the core has been
4467 +         * configured to work at either data path width. 
4468 +         *
4469 +         * 8 or 16 bits (default 16)
4470 +         */
4471 +        int32_t phy_utmi_width;
4472 +//#define dwc_param_phy_utmi_width_default 16
4473 +        /**
4474 +         * Specifies whether the ULPI operates at double or single
4475 +         * data rate. This parameter is only applicable if PHY_TYPE is
4476 +         * ULPI.
4477 +         * 
4478 +         * 0 - single data rate ULPI interface with 8 bit wide data
4479 +         * bus (default)
4480 +         * 1 - double data rate ULPI interface with 4 bit wide data
4481 +         * bus
4482 +         */
4483 +        int32_t phy_ulpi_ddr;
4484 +//#define dwc_param_phy_ulpi_ddr_default 0
4485 +       /**
4486 +        * Specifies whether to use the internal or external supply to 
4487 +        * drive the vbus with a ULPI phy.
4488 +        */
4489 +       int32_t phy_ulpi_ext_vbus;
4490 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
4491 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
4492 +//#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
4493 +        /**
4494 +        * Specifies whether to use the I2Cinterface for full speed PHY. This
4495 +        * parameter is only applicable if PHY_TYPE is FS.
4496 +         * 0 - No (default)
4497 +         * 1 - Yes
4498 +         */
4499 +        int32_t i2c_enable;
4500 +//#define dwc_param_i2c_enable_default 0
4501 +
4502 +        int32_t ulpi_fs_ls;
4503 +//#define dwc_param_ulpi_fs_ls_default 0
4504 +
4505 +       int32_t ts_dline;
4506 +//#define dwc_param_ts_dline_default 0
4507 +
4508 +       /**
4509 +        * Specifies whether dedicated transmit FIFOs are
4510 +        * enabled for non periodic IN endpoints in device mode
4511 +        * 0 - No
4512 +        * 1 - Yes
4513 +        */
4514 +        int32_t en_multiple_tx_fifo;
4515 +#define dwc_param_en_multiple_tx_fifo_default 1
4516 +
4517 +       /** Number of 4-byte words in each of the Tx FIFOs in device
4518 +        * mode when dynamic FIFO sizing is enabled.
4519 +        * 4 to 768 (default 256)
4520 +        */
4521 +       uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
4522 +#define dwc_param_dev_tx_fifo_size_default 256
4523 +
4524 +       /** Thresholding enable flag-
4525 +        * bit 0 - enable non-ISO Tx thresholding
4526 +        * bit 1 - enable ISO Tx thresholding
4527 +        * bit 2 - enable Rx thresholding
4528 +        */
4529 +       uint32_t thr_ctl;
4530 +#define dwc_param_thr_ctl_default 0
4531 +
4532 +       /** Thresholding length for Tx
4533 +        *      FIFOs in 32 bit DWORDs
4534 +        */
4535 +       uint32_t tx_thr_length;
4536 +#define dwc_param_tx_thr_length_default 64
4537 +
4538 +       /** Thresholding length for Rx
4539 +        *      FIFOs in 32 bit DWORDs
4540 +        */
4541 +       uint32_t rx_thr_length;
4542 +#define dwc_param_rx_thr_length_default 64
4543 +} dwc_otg_core_params_t;
4544 +
4545 +#ifdef DEBUG
4546 +struct dwc_otg_core_if;
4547 +typedef        struct hc_xfer_info
4548 +{
4549 +       struct dwc_otg_core_if  *core_if;
4550 +       dwc_hc_t                *hc;
4551 +} hc_xfer_info_t;
4552 +#endif
4553 +
4554 +/**
4555 + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
4556 + * the DWC_otg controller acting in either host or device mode. It
4557 + * represents the programming view of the controller as a whole.
4558 + */
4559 +typedef struct dwc_otg_core_if 
4560 +{
4561 +    /** Parameters that define how the core should be configured.*/
4562 +    dwc_otg_core_params_t      *core_params;
4563 +
4564 +    /** Core Global registers starting at offset 000h. */
4565 +    dwc_otg_core_global_regs_t *core_global_regs;
4566 +
4567 +    /** Device-specific information */
4568 +    dwc_otg_dev_if_t           *dev_if;
4569 +    /** Host-specific information */
4570 +    dwc_otg_host_if_t          *host_if;
4571 +
4572 +    /*
4573 +     * Set to 1 if the core PHY interface bits in USBCFG have been
4574 +     * initialized.
4575 +     */
4576 +    uint8_t phy_init_done;
4577 +
4578 +    /*
4579 +     * SRP Success flag, set by srp success interrupt in FS I2C mode
4580 +     */
4581 +    uint8_t srp_success;
4582 +    uint8_t srp_timer_started;
4583 +
4584 +    /* Common configuration information */
4585 +    /** Power and Clock Gating Control Register */
4586 +    volatile uint32_t *pcgcctl;
4587 +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
4588 +
4589 +    /** Push/pop addresses for endpoints or host channels.*/
4590 +    uint32_t *data_fifo[MAX_EPS_CHANNELS];
4591 +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
4592 +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
4593 +
4594 +    /** Total RAM for FIFOs (Bytes) */
4595 +    uint16_t total_fifo_size;
4596 +    /** Size of Rx FIFO (Bytes) */
4597 +    uint16_t rx_fifo_size;
4598 +    /** Size of Non-periodic Tx FIFO (Bytes) */
4599 +    uint16_t nperio_tx_fifo_size;
4600 +        
4601 +    /** 1 if DMA is enabled, 0 otherwise. */
4602 +    uint8_t    dma_enable;
4603 +
4604 +       /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
4605 +       uint8_t en_multiple_tx_fifo;
4606 +
4607 +    /** Set to 1 if multiple packets of a high-bandwidth transfer is in
4608 +     * process of being queued */
4609 +    uint8_t queuing_high_bandwidth;
4610 +
4611 +    /** Hardware Configuration -- stored here for convenience.*/
4612 +    hwcfg1_data_t hwcfg1;
4613 +    hwcfg2_data_t hwcfg2;
4614 +    hwcfg3_data_t hwcfg3;
4615 +    hwcfg4_data_t hwcfg4;
4616 +
4617 +    /** The operational State, during transations
4618 +     * (a_host>>a_peripherial and b_device=>b_host) this may not
4619 +     * match the core but allows the software to determine
4620 +     * transitions.
4621 +     */
4622 +    uint8_t op_state;
4623 +        
4624 +    /**
4625 +     * Set to 1 if the HCD needs to be restarted on a session request
4626 +     * interrupt. This is required if no connector ID status change has
4627 +     * occurred since the HCD was last disconnected.
4628 +     */
4629 +    uint8_t restart_hcd_on_session_req;
4630 +
4631 +    /** HCD callbacks */
4632 +    /** A-Device is a_host */
4633 +#define A_HOST                 (1)
4634 +    /** A-Device is a_suspend */
4635 +#define A_SUSPEND      (2)
4636 +    /** A-Device is a_peripherial */
4637 +#define A_PERIPHERAL   (3)
4638 +    /** B-Device is operating as a Peripheral. */
4639 +#define B_PERIPHERAL   (4)
4640 +    /** B-Device is operating as a Host. */
4641 +#define B_HOST                 (5)        
4642 +
4643 +    /** HCD callbacks */
4644 +    struct dwc_otg_cil_callbacks *hcd_cb;
4645 +    /** PCD callbacks */
4646 +    struct dwc_otg_cil_callbacks *pcd_cb;
4647 +
4648 +       /** Device mode Periodic Tx FIFO Mask */
4649 +       uint32_t p_tx_msk;
4650 +       /** Device mode Periodic Tx FIFO Mask */
4651 +       uint32_t tx_msk;
4652 +
4653 +#ifdef DEBUG
4654 +    uint32_t           start_hcchar_val[MAX_EPS_CHANNELS];
4655 +
4656 +    hc_xfer_info_t             hc_xfer_info[MAX_EPS_CHANNELS];
4657 +    struct timer_list  hc_xfer_timer[MAX_EPS_CHANNELS];
4658 +
4659 +#if 1 // winder
4660 +    uint32_t           hfnum_7_samples;
4661 +    uint32_t           hfnum_7_frrem_accum;
4662 +    uint32_t           hfnum_0_samples;
4663 +    uint32_t           hfnum_0_frrem_accum;
4664 +    uint32_t           hfnum_other_samples;
4665 +    uint32_t           hfnum_other_frrem_accum;
4666 +#else
4667 +    uint32_t           hfnum_7_samples;
4668 +    uint64_t           hfnum_7_frrem_accum;
4669 +    uint32_t           hfnum_0_samples;
4670 +    uint64_t           hfnum_0_frrem_accum;
4671 +    uint32_t           hfnum_other_samples;
4672 +    uint64_t           hfnum_other_frrem_accum;
4673 +#endif
4674 +       resource_size_t phys_addr;              /* Added to support PLB DMA : phys-virt mapping */
4675 +#endif 
4676 +
4677 +} dwc_otg_core_if_t;
4678 +
4679 +/*
4680 + * The following functions support initialization of the CIL driver component
4681 + * and the DWC_otg controller.
4682 + */
4683 +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t *_reg_base_addr,
4684 +                                           dwc_otg_core_params_t *_core_params);
4685 +extern void dwc_otg_cil_remove(dwc_otg_core_if_t *_core_if);
4686 +extern void dwc_otg_core_init(dwc_otg_core_if_t *_core_if);
4687 +extern void dwc_otg_core_host_init(dwc_otg_core_if_t *_core_if);
4688 +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t *_core_if);
4689 +extern void dwc_otg_enable_global_interrupts( dwc_otg_core_if_t *_core_if );
4690 +extern void dwc_otg_disable_global_interrupts( dwc_otg_core_if_t *_core_if );
4691 +
4692 +/** @name Device CIL Functions
4693 + * The following functions support managing the DWC_otg controller in device
4694 + * mode.
4695 + */
4696 +/**@{*/
4697 +extern void dwc_otg_wakeup(dwc_otg_core_if_t *_core_if);
4698 +extern void dwc_otg_read_setup_packet (dwc_otg_core_if_t *_core_if, uint32_t *_dest);
4699 +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t *_core_if);
4700 +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4701 +extern void dwc_otg_ep_activate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4702 +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4703 +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4704 +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4705 +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4706 +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep, int _dma);
4707 +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4708 +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t *_core_if, dwc_ep_t *_ep);
4709 +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t *_core_if);
4710 +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t *_core_if);
4711 +/**@}*/
4712 +
4713 +/** @name Host CIL Functions
4714 + * The following functions support managing the DWC_otg controller in host
4715 + * mode.
4716 + */
4717 +/**@{*/
4718 +extern void dwc_otg_hc_init(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4719 +extern void dwc_otg_hc_halt(dwc_otg_core_if_t *_core_if,
4720 +                           dwc_hc_t *_hc,
4721 +                           dwc_otg_halt_status_e _halt_status);
4722 +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4723 +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4724 +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4725 +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4726 +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc);
4727 +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t *_core_if);
4728 +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t *_core_if);
4729 +
4730 +/**
4731 + * This function Reads HPRT0 in preparation to modify.  It keeps the
4732 + * WC bits 0 so that if they are read as 1, they won't clear when you
4733 + * write it back 
4734 + */
4735 +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t *_core_if) 
4736 +{
4737 +        hprt0_data_t hprt0;
4738 +        hprt0.d32 = dwc_read_reg32(_core_if->host_if->hprt0);
4739 +        hprt0.b.prtena = 0;
4740 +        hprt0.b.prtconndet = 0;
4741 +        hprt0.b.prtenchng = 0;
4742 +        hprt0.b.prtovrcurrchng = 0;
4743 +        return hprt0.d32;
4744 +}
4745 +
4746 +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t *_core_if);
4747 +/**@}*/
4748 +
4749 +/** @name Common CIL Functions
4750 + * The following functions support managing the DWC_otg controller in either
4751 + * device or host mode.
4752 + */
4753 +/**@{*/
4754 +
4755 +extern void dwc_otg_read_packet(dwc_otg_core_if_t *core_if,
4756 +                               uint8_t *dest, 
4757 +                               uint16_t bytes);
4758 +
4759 +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t *_core_if);
4760 +
4761 +extern void dwc_otg_flush_tx_fifo( dwc_otg_core_if_t *_core_if, 
4762 +                                   const int _num );
4763 +extern void dwc_otg_flush_rx_fifo( dwc_otg_core_if_t *_core_if );
4764 +extern void dwc_otg_core_reset( dwc_otg_core_if_t *_core_if );
4765 +
4766 +#define NP_TXFIFO_EMPTY -1
4767 +#define MAX_NP_TXREQUEST_Q_SLOTS 8
4768 +/**
4769 + * This function returns the endpoint number of the request at
4770 + * the top of non-periodic TX FIFO, or -1 if the request FIFO is
4771 + * empty.
4772 + */
4773 +static inline int dwc_otg_top_nptxfifo_epnum(dwc_otg_core_if_t *_core_if) {
4774 +       gnptxsts_data_t txstatus = {.d32 = 0};
4775 +
4776 +       txstatus.d32 = dwc_read_reg32(&_core_if->core_global_regs->gnptxsts);
4777 +       return (txstatus.b.nptxqspcavail == MAX_NP_TXREQUEST_Q_SLOTS ?
4778 +               -1 : txstatus.b.nptxqtop_chnep);
4779 +}
4780 +/**
4781 + * This function returns the Core Interrupt register.
4782 + */
4783 +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t *_core_if) {
4784 +       return (dwc_read_reg32(&_core_if->core_global_regs->gintsts) &
4785 +                dwc_read_reg32(&_core_if->core_global_regs->gintmsk));
4786 +}
4787 +
4788 +/**
4789 + * This function returns the OTG Interrupt register.
4790 + */
4791 +static inline uint32_t dwc_otg_read_otg_intr (dwc_otg_core_if_t *_core_if) {
4792 +       return (dwc_read_reg32 (&_core_if->core_global_regs->gotgint));
4793 +}
4794 +
4795 +/**
4796 + * This function reads the Device All Endpoints Interrupt register and
4797 + * returns the IN endpoint interrupt bits.
4798 + */
4799 +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *_core_if) {
4800 +        uint32_t v;
4801 +        v = dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daint) &
4802 +                dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daintmsk);
4803 +        return (v & 0xffff);
4804 +        
4805 +}
4806 +
4807 +/**
4808 + * This function reads the Device All Endpoints Interrupt register and
4809 + * returns the OUT endpoint interrupt bits.
4810 + */
4811 +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *_core_if) {
4812 +        uint32_t v;
4813 +        v = dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daint) &
4814 +                dwc_read_reg32(&_core_if->dev_if->dev_global_regs->daintmsk);
4815 +        return ((v & 0xffff0000) >> 16);
4816 +}
4817 +
4818 +/**
4819 + * This function returns the Device IN EP Interrupt register
4820 + */
4821 +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t *_core_if,
4822 +                                                   dwc_ep_t *_ep)
4823 +{
4824 +        dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
4825 +       uint32_t v, msk, emp;
4826 +       msk = dwc_read_reg32(&dev_if->dev_global_regs->diepmsk);
4827 +       emp = dwc_read_reg32(&dev_if->dev_global_regs->dtknqr4_fifoemptymsk);
4828 +       msk |= ((emp >> _ep->num) & 0x1) << 7;
4829 +       v = dwc_read_reg32(&dev_if->in_ep_regs[_ep->num]->diepint) & msk;
4830 +/*
4831 +       dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
4832 +        uint32_t v;
4833 +        v = dwc_read_reg32(&dev_if->in_ep_regs[_ep->num]->diepint) &
4834 +                dwc_read_reg32(&dev_if->dev_global_regs->diepmsk);
4835 +*/
4836 +        return v;        
4837 +}
4838 +/**
4839 + * This function returns the Device OUT EP Interrupt register
4840 + */
4841 +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *_core_if, 
4842 +                                                    dwc_ep_t *_ep)
4843 +{
4844 +        dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
4845 +        uint32_t v;
4846 +        v = dwc_read_reg32( &dev_if->out_ep_regs[_ep->num]->doepint) &
4847 +                       dwc_read_reg32(&dev_if->dev_global_regs->doepmsk);
4848 +        return v;        
4849 +}
4850 +
4851 +/**
4852 + * This function returns the Host All Channel Interrupt register
4853 + */
4854 +static inline uint32_t dwc_otg_read_host_all_channels_intr (dwc_otg_core_if_t *_core_if)
4855 +{
4856 +       return (dwc_read_reg32 (&_core_if->host_if->host_global_regs->haint));
4857 +}
4858 +
4859 +static inline uint32_t dwc_otg_read_host_channel_intr (dwc_otg_core_if_t *_core_if, dwc_hc_t *_hc)
4860 +{
4861 +       return (dwc_read_reg32 (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
4862 +}
4863 +
4864 +
4865 +/**
4866 + * This function returns the mode of the operation, host or device.
4867 + *
4868 + * @return 0 - Device Mode, 1 - Host Mode 
4869 + */
4870 +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t *_core_if) {
4871 +        return (dwc_read_reg32( &_core_if->core_global_regs->gintsts ) & 0x1);
4872 +}
4873 +
4874 +static inline uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t *_core_if) 
4875 +{
4876 +        return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
4877 +}
4878 +static inline uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t *_core_if) 
4879 +{
4880 +        return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
4881 +}
4882 +
4883 +extern int32_t dwc_otg_handle_common_intr( dwc_otg_core_if_t *_core_if );
4884 +
4885 +
4886 +/**@}*/
4887 +
4888 +/**
4889 + * DWC_otg CIL callback structure.  This structure allows the HCD and
4890 + * PCD to register functions used for starting and stopping the PCD
4891 + * and HCD for role change on for a DRD.
4892 + */
4893 +typedef struct dwc_otg_cil_callbacks 
4894 +{
4895 +        /** Start function for role change */
4896 +        int (*start) (void *_p);
4897 +        /** Stop Function for role change */
4898 +        int (*stop) (void *_p);
4899 +        /** Disconnect Function for role change */
4900 +        int (*disconnect) (void *_p);
4901 +        /** Resume/Remote wakeup Function */
4902 +        int (*resume_wakeup) (void *_p);
4903 +        /** Suspend function */
4904 +        int (*suspend) (void *_p);
4905 +        /** Session Start (SRP) */
4906 +        int (*session_start) (void *_p);
4907 +        /** Pointer passed to start() and stop() */
4908 +        void *p;
4909 +} dwc_otg_cil_callbacks_t;
4910 +
4911 +
4912 +
4913 +extern void dwc_otg_cil_register_pcd_callbacks( dwc_otg_core_if_t *_core_if,
4914 +                                                dwc_otg_cil_callbacks_t *_cb,
4915 +                                                void *_p);
4916 +extern void dwc_otg_cil_register_hcd_callbacks( dwc_otg_core_if_t *_core_if,
4917 +                                                dwc_otg_cil_callbacks_t *_cb,
4918 +                                                void *_p);
4919 +
4920 +
4921 +#endif
4922 --- /dev/null
4923 +++ b/drivers/usb/dwc_otg/dwc_otg_cil_ifx.h
4924 @@ -0,0 +1,58 @@
4925 +/******************************************************************************
4926 +**
4927 +** FILE NAME    : dwc_otg_cil_ifx.h
4928 +** PROJECT      : Twinpass/Danube
4929 +** MODULES      : DWC OTG USB
4930 +**
4931 +** DATE         : 07 Sep. 2007
4932 +** AUTHOR       : Sung Winder
4933 +** DESCRIPTION  : Default param value.
4934 +** COPYRIGHT    :       Copyright (c) 2007
4935 +**                      Infineon Technologies AG
4936 +**                      2F, No.2, Li-Hsin Rd., Hsinchu Science Park,
4937 +**                      Hsin-chu City, 300 Taiwan.
4938 +**
4939 +**    This program is free software; you can redistribute it and/or modify
4940 +**    it under the terms of the GNU General Public License as published by
4941 +**    the Free Software Foundation; either version 2 of the License, or
4942 +**    (at your option) any later version.
4943 +**
4944 +** HISTORY
4945 +** $Date          $Author         $Comment
4946 +** 12 April 2007   Sung Winder     Initiate Version
4947 +*******************************************************************************/
4948 +#if !defined(__DWC_OTG_CIL_IFX_H__)
4949 +#define __DWC_OTG_CIL_IFX_H__
4950 +
4951 +/* ================ Default param value ================== */
4952 +#define dwc_param_opt_default 1
4953 +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE
4954 +#define dwc_param_dma_enable_default 1
4955 +#define dwc_param_dma_burst_size_default 32
4956 +#define dwc_param_speed_default DWC_SPEED_PARAM_HIGH
4957 +#define dwc_param_host_support_fs_ls_low_power_default 0
4958 +#define dwc_param_host_ls_low_power_phy_clk_default DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ
4959 +#define dwc_param_enable_dynamic_fifo_default 1
4960 +#define dwc_param_data_fifo_size_default 2048
4961 +#define dwc_param_dev_rx_fifo_size_default 1024
4962 +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
4963 +#define dwc_param_dev_perio_tx_fifo_size_default 768
4964 +#define dwc_param_host_rx_fifo_size_default 640
4965 +#define dwc_param_host_nperio_tx_fifo_size_default 640
4966 +#define dwc_param_host_perio_tx_fifo_size_default 768
4967 +#define dwc_param_max_transfer_size_default 65535
4968 +#define dwc_param_max_packet_count_default 511
4969 +#define dwc_param_host_channels_default 16
4970 +#define dwc_param_dev_endpoints_default 6
4971 +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
4972 +#define dwc_param_phy_utmi_width_default 16
4973 +#define dwc_param_phy_ulpi_ddr_default 0
4974 +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
4975 +#define dwc_param_i2c_enable_default 0
4976 +#define dwc_param_ulpi_fs_ls_default 0
4977 +#define dwc_param_ts_dline_default 0
4978 +
4979 +/* ======================================================= */
4980 +
4981 +#endif // __DWC_OTG_CIL_IFX_H__
4982 +
4983 --- /dev/null
4984 +++ b/drivers/usb/dwc_otg/dwc_otg_cil_intr.c
4985 @@ -0,0 +1,708 @@
4986 +/* ==========================================================================
4987 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_cil_intr.c $
4988 + * $Revision: 1.1.1.1 $
4989 + * $Date: 2009-04-17 06:15:34 $
4990 + * $Change: 553126 $
4991 + *
4992 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
4993 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
4994 + * otherwise expressly agreed to in writing between Synopsys and you.
4995 + * 
4996 + * The Software IS NOT an item of Licensed Software or Licensed Product under
4997 + * any End User Software License Agreement or Agreement for Licensed Product
4998 + * with Synopsys or any supplement thereto. You are permitted to use and
4999 + * redistribute this Software in source and binary forms, with or without
5000 + * modification, provided that redistributions of source code must retain this
5001 + * notice. You may not view, use, disclose, copy or distribute this file or
5002 + * any information contained herein except pursuant to this license grant from
5003 + * Synopsys. If you do not agree with this notice, including the disclaimer
5004 + * below, then you are not authorized to use the Software.
5005 + * 
5006 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
5007 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5008 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5009 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
5010 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
5011 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
5012 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
5013 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5014 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5015 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5016 + * DAMAGE.
5017 + * ========================================================================== */
5018 +
5019 +/** @file 
5020 + *
5021 + * The Core Interface Layer provides basic services for accessing and
5022 + * managing the DWC_otg hardware. These services are used by both the
5023 + * Host Controller Driver and the Peripheral Controller Driver.
5024 + *
5025 + * This file contains the Common Interrupt handlers.
5026 + */
5027 +#include "dwc_otg_plat.h"
5028 +#include "dwc_otg_regs.h"
5029 +#include "dwc_otg_cil.h"
5030 +
5031 +#ifdef DEBUG
5032 +inline const char *op_state_str( dwc_otg_core_if_t *_core_if ) 
5033 +{
5034 +        return (_core_if->op_state==A_HOST?"a_host":
5035 +                (_core_if->op_state==A_SUSPEND?"a_suspend":
5036 +                 (_core_if->op_state==A_PERIPHERAL?"a_peripheral":
5037 +                  (_core_if->op_state==B_PERIPHERAL?"b_peripheral":
5038 +                   (_core_if->op_state==B_HOST?"b_host":
5039 +                    "unknown")))));
5040 +}
5041 +#endif
5042 +
5043 +/** This function will log a debug message 
5044 + *
5045 + * @param _core_if Programming view of DWC_otg controller.
5046 + */
5047 +int32_t dwc_otg_handle_mode_mismatch_intr (dwc_otg_core_if_t *_core_if)
5048 +{
5049 +       gintsts_data_t gintsts;
5050 +       DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n", 
5051 +                dwc_otg_mode(_core_if) ? "Host" : "Device");
5052 +
5053 +       /* Clear interrupt */
5054 +       gintsts.d32 = 0;
5055 +       gintsts.b.modemismatch = 1;     
5056 +       dwc_write_reg32 (&_core_if->core_global_regs->gintsts, gintsts.d32);
5057 +       return 1;
5058 +}
5059 +
5060 +/** Start the HCD.  Helper function for using the HCD callbacks.
5061 + *
5062 + * @param _core_if Programming view of DWC_otg controller.
5063 + */
5064 +static inline void hcd_start( dwc_otg_core_if_t *_core_if ) 
5065 +{        
5066 +        if (_core_if->hcd_cb && _core_if->hcd_cb->start) {
5067 +                _core_if->hcd_cb->start( _core_if->hcd_cb->p );
5068 +        }
5069 +}
5070 +/** Stop the HCD.  Helper function for using the HCD callbacks. 
5071 + *
5072 + * @param _core_if Programming view of DWC_otg controller.
5073 + */
5074 +static inline void hcd_stop( dwc_otg_core_if_t *_core_if ) 
5075 +{        
5076 +        if (_core_if->hcd_cb && _core_if->hcd_cb->stop) {
5077 +                _core_if->hcd_cb->stop( _core_if->hcd_cb->p );
5078 +        }
5079 +}
5080 +/** Disconnect the HCD.  Helper function for using the HCD callbacks.
5081 + *
5082 + * @param _core_if Programming view of DWC_otg controller.
5083 + */
5084 +static inline void hcd_disconnect( dwc_otg_core_if_t *_core_if ) 
5085 +{
5086 +        if (_core_if->hcd_cb && _core_if->hcd_cb->disconnect) {
5087 +                _core_if->hcd_cb->disconnect( _core_if->hcd_cb->p );
5088 +        }
5089 +}
5090 +/** Inform the HCD the a New Session has begun.  Helper function for
5091 + * using the HCD callbacks.
5092 + *
5093 + * @param _core_if Programming view of DWC_otg controller.
5094 + */
5095 +static inline void hcd_session_start( dwc_otg_core_if_t *_core_if ) 
5096 +{
5097 +        if (_core_if->hcd_cb && _core_if->hcd_cb->session_start) {
5098 +                _core_if->hcd_cb->session_start( _core_if->hcd_cb->p );
5099 +        }
5100 +}
5101 +
5102 +/** Start the PCD.  Helper function for using the PCD callbacks.
5103 + *
5104 + * @param _core_if Programming view of DWC_otg controller.
5105 + */
5106 +static inline void pcd_start( dwc_otg_core_if_t *_core_if ) 
5107 +{
5108 +        if (_core_if->pcd_cb && _core_if->pcd_cb->start ) {
5109 +                _core_if->pcd_cb->start( _core_if->pcd_cb->p );
5110 +        }
5111 +}
5112 +/** Stop the PCD.  Helper function for using the PCD callbacks. 
5113 + *
5114 + * @param _core_if Programming view of DWC_otg controller.
5115 + */
5116 +static inline void pcd_stop( dwc_otg_core_if_t *_core_if ) 
5117 +{
5118 +        if (_core_if->pcd_cb && _core_if->pcd_cb->stop ) {
5119 +                _core_if->pcd_cb->stop( _core_if->pcd_cb->p );
5120 +        }
5121 +}
5122 +/** Suspend the PCD.  Helper function for using the PCD callbacks. 
5123 + *
5124 + * @param _core_if Programming view of DWC_otg controller.
5125 + */
5126 +static inline void pcd_suspend( dwc_otg_core_if_t *_core_if ) 
5127 +{
5128 +        if (_core_if->pcd_cb && _core_if->pcd_cb->suspend ) {
5129 +                _core_if->pcd_cb->suspend( _core_if->pcd_cb->p );
5130 +        }
5131 +}
5132 +/** Resume the PCD.  Helper function for using the PCD callbacks. 
5133 + *
5134 + * @param _core_if Programming view of DWC_otg controller.
5135 + */
5136 +static inline void pcd_resume( dwc_otg_core_if_t *_core_if ) 
5137 +{
5138 +        if (_core_if->pcd_cb && _core_if->pcd_cb->resume_wakeup ) {
5139 +                _core_if->pcd_cb->resume_wakeup( _core_if->pcd_cb->p );
5140 +        }
5141 +}
5142 +
5143 +/**
5144 + * This function handles the OTG Interrupts. It reads the OTG
5145 + * Interrupt Register (GOTGINT) to determine what interrupt has
5146 + * occurred.
5147 + *
5148 + * @param _core_if Programming view of DWC_otg controller.
5149 + */
5150 +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t *_core_if)
5151 +{
5152 +        dwc_otg_core_global_regs_t *global_regs = 
5153 +                _core_if->core_global_regs;
5154 +       gotgint_data_t gotgint;
5155 +        gotgctl_data_t gotgctl;
5156 +       gintmsk_data_t gintmsk;
5157 +
5158 +       gotgint.d32 = dwc_read_reg32( &global_regs->gotgint);
5159 +        gotgctl.d32 = dwc_read_reg32( &global_regs->gotgctl);
5160 +       DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
5161 +                    op_state_str(_core_if));
5162 +        //DWC_DEBUGPL(DBG_CIL, "gotgctl=%08x\n", gotgctl.d32 );
5163 +
5164 +       if (gotgint.b.sesenddet) {
5165 +               DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
5166 +                           "Session End Detected++ (%s)\n",
5167 +                            op_state_str(_core_if));
5168 +                gotgctl.d32 = dwc_read_reg32( &global_regs->gotgctl);
5169 +
5170 +                if (_core_if->op_state == B_HOST) {
5171 +                        pcd_start( _core_if );
5172 +                        _core_if->op_state = B_PERIPHERAL;
5173 +                } else {
5174 +                        /* If not B_HOST and Device HNP still set. HNP
5175 +                         * Did not succeed!*/
5176 +                        if (gotgctl.b.devhnpen) {
5177 +                                DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
5178 +                                DWC_ERROR( "Device Not Connected/Responding!\n" );
5179 +                        }
5180 +
5181 +                        /* If Session End Detected the B-Cable has
5182 +                         * been disconnected. */
5183 +                        /* Reset PCD and Gadget driver to a
5184 +                         * clean state. */
5185 +                        pcd_stop(_core_if);
5186 +                }
5187 +                gotgctl.d32 = 0;
5188 +                gotgctl.b.devhnpen = 1;
5189 +                dwc_modify_reg32( &global_regs->gotgctl, 
5190 +                                  gotgctl.d32, 0);
5191 +        }
5192 +       if (gotgint.b.sesreqsucstschng) {
5193 +               DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
5194 +                           "Session Reqeust Success Status Change++\n");
5195 +                gotgctl.d32 = dwc_read_reg32( &global_regs->gotgctl);
5196 +                if (gotgctl.b.sesreqscs) {
5197 +                       if ((_core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) && 
5198 +                           (_core_if->core_params->i2c_enable)) {
5199 +                               _core_if->srp_success = 1;
5200 +                       }
5201 +                       else {
5202 +                               pcd_resume( _core_if );
5203 +                               /* Clear Session Request */
5204 +                               gotgctl.d32 = 0;
5205 +                               gotgctl.b.sesreq = 1;
5206 +                               dwc_modify_reg32( &global_regs->gotgctl, 
5207 +                                                 gotgctl.d32, 0);
5208 +                       }
5209 +                }
5210 +       }
5211 +       if (gotgint.b.hstnegsucstschng) {
5212 +                /* Print statements during the HNP interrupt handling
5213 +                 * can cause it to fail.*/
5214 +                gotgctl.d32 = dwc_read_reg32(&global_regs->gotgctl);
5215 +                if (gotgctl.b.hstnegscs) {
5216 +                        if (dwc_otg_is_host_mode(_core_if) ) {
5217 +                                _core_if->op_state = B_HOST;
5218 +                               /*
5219 +                                * Need to disable SOF interrupt immediately.
5220 +                                * When switching from device to host, the PCD
5221 +                                * interrupt handler won't handle the
5222 +                                * interrupt if host mode is already set. The
5223 +                                * HCD interrupt handler won't get called if
5224 +                                * the HCD state is HALT. This means that the
5225 +                                * interrupt does not get handled and Linux
5226 +                                * complains loudly.
5227 +                                */
5228 +                               gintmsk.d32 = 0;
5229 +                               gintmsk.b.sofintr = 1;
5230 +                               dwc_modify_reg32(&global_regs->gintmsk,
5231 +                                                gintmsk.d32, 0);
5232 +                                pcd_stop(_core_if);
5233 +                                /*
5234 +                                 * Initialize the Core for Host mode.
5235 +                                 */
5236 +                                hcd_start( _core_if );
5237 +                                _core_if->op_state = B_HOST;
5238 +                        }
5239 +                } else {
5240 +                        gotgctl.d32 = 0;
5241 +                        gotgctl.b.hnpreq = 1;
5242 +                        gotgctl.b.devhnpen = 1;
5243 +                        dwc_modify_reg32( &global_regs->gotgctl, 
5244 +                                          gotgctl.d32, 0);
5245 +                        DWC_DEBUGPL( DBG_ANY, "HNP Failed\n");
5246 +                        DWC_ERROR( "Device Not Connected/Responding\n" );
5247 +                }
5248 +       }
5249 +       if (gotgint.b.hstnegdet) {
5250 +                /* The disconnect interrupt is set at the same time as
5251 +                * Host Negotiation Detected.  During the mode
5252 +                * switch all interrupts are cleared so the disconnect
5253 +                * interrupt handler will not get executed.
5254 +                 */
5255 +               DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
5256 +                           "Host Negotiation Detected++ (%s)\n", 
5257 +                            (dwc_otg_is_host_mode(_core_if)?"Host":"Device"));
5258 +                if (dwc_otg_is_device_mode(_core_if)){
5259 +                        DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",_core_if->op_state);
5260 +                        hcd_disconnect( _core_if );
5261 +                        pcd_start( _core_if );
5262 +                        _core_if->op_state = A_PERIPHERAL;
5263 +                } else {
5264 +                       /*
5265 +                        * Need to disable SOF interrupt immediately. When
5266 +                        * switching from device to host, the PCD interrupt
5267 +                        * handler won't handle the interrupt if host mode is
5268 +                        * already set. The HCD interrupt handler won't get
5269 +                        * called if the HCD state is HALT. This means that
5270 +                        * the interrupt does not get handled and Linux
5271 +                        * complains loudly.
5272 +                        */
5273 +                       gintmsk.d32 = 0;
5274 +                       gintmsk.b.sofintr = 1;
5275 +                       dwc_modify_reg32(&global_regs->gintmsk,
5276 +                                        gintmsk.d32, 0);
5277 +                        pcd_stop( _core_if );
5278 +                        hcd_start( _core_if );
5279 +                        _core_if->op_state = A_HOST;
5280 +                }
5281 +       }
5282 +       if (gotgint.b.adevtoutchng) {
5283 +               DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
5284 +                           "A-Device Timeout Change++\n");
5285 +       }
5286 +       if (gotgint.b.debdone) {
5287 +               DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
5288 +                           "Debounce Done++\n");
5289 +       }
5290 +
5291 +       /* Clear GOTGINT */
5292 +       dwc_write_reg32 (&_core_if->core_global_regs->gotgint, gotgint.d32);
5293 +
5294 +       return 1;
5295 +}
5296 +
5297 +/**
5298 + * This function handles the Connector ID Status Change Interrupt.  It
5299 + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
5300 + * is a Device to Host Mode transition or a Host Mode to Device
5301 + * Transition.  
5302 + *
5303 + * This only occurs when the cable is connected/removed from the PHY
5304 + * connector.
5305 + *
5306 + * @param _core_if Programming view of DWC_otg controller.
5307 + */
5308 +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t *_core_if)
5309 +{
5310 +        uint32_t count = 0;
5311 +        
5312 +       gintsts_data_t gintsts = { .d32 = 0 };
5313 +       gintmsk_data_t gintmsk = { .d32 = 0 };
5314 +        gotgctl_data_t gotgctl = { .d32 = 0 }; 
5315 +
5316 +       /*
5317 +        * Need to disable SOF interrupt immediately. If switching from device
5318 +        * to host, the PCD interrupt handler won't handle the interrupt if
5319 +        * host mode is already set. The HCD interrupt handler won't get
5320 +        * called if the HCD state is HALT. This means that the interrupt does
5321 +        * not get handled and Linux complains loudly.
5322 +        */
5323 +       gintmsk.b.sofintr = 1;
5324 +       dwc_modify_reg32(&_core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
5325 +
5326 +       DWC_DEBUGPL(DBG_CIL, " ++Connector ID Status Change Interrupt++  (%s)\n",
5327 +                    (dwc_otg_is_host_mode(_core_if)?"Host":"Device"));
5328 +        gotgctl.d32 = dwc_read_reg32(&_core_if->core_global_regs->gotgctl);
5329 +       DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
5330 +       DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
5331 +        
5332 +        /* B-Device connector (Device Mode) */
5333 +        if (gotgctl.b.conidsts) {
5334 +                /* Wait for switch to device mode. */
5335 +                while (!dwc_otg_is_device_mode(_core_if) ){
5336 +                        DWC_PRINT("Waiting for Peripheral Mode, Mode=%s\n",
5337 +                                  (dwc_otg_is_host_mode(_core_if)?"Host":"Peripheral"));
5338 +                        MDELAY(100);
5339 +                        if (++count > 10000) *(uint32_t*)NULL=0;
5340 +                }
5341 +                _core_if->op_state = B_PERIPHERAL;
5342 +               dwc_otg_core_init(_core_if);
5343 +               dwc_otg_enable_global_interrupts(_core_if);
5344 +                pcd_start( _core_if );
5345 +        } else {
5346 +                /* A-Device connector (Host Mode) */
5347 +                while (!dwc_otg_is_host_mode(_core_if) ) {
5348 +                        DWC_PRINT("Waiting for Host Mode, Mode=%s\n",
5349 +                                  (dwc_otg_is_host_mode(_core_if)?"Host":"Peripheral"));
5350 +                        MDELAY(100);
5351 +                        if (++count > 10000) *(uint32_t*)NULL=0;
5352 +                }
5353 +                _core_if->op_state = A_HOST;
5354 +                /*
5355 +                 * Initialize the Core for Host mode.
5356 +                 */
5357 +               dwc_otg_core_init(_core_if);
5358 +               dwc_otg_enable_global_interrupts(_core_if);
5359 +                hcd_start( _core_if );
5360 +        }
5361 +
5362 +       /* Set flag and clear interrupt */
5363 +       gintsts.b.conidstschng = 1;
5364 +       dwc_write_reg32 (&_core_if->core_global_regs->gintsts, gintsts.d32);
5365 +
5366 +       return 1;
5367 +}
5368 +
5369 +/** 
5370 + * This interrupt indicates that a device is initiating the Session
5371 + * Request Protocol to request the host to turn on bus power so a new
5372 + * session can begin. The handler responds by turning on bus power. If
5373 + * the DWC_otg controller is in low power mode, the handler brings the
5374 + * controller out of low power mode before turning on bus power. 
5375 + *
5376 + * @param _core_if Programming view of DWC_otg controller.
5377 + */
5378 +int32_t dwc_otg_handle_session_req_intr( dwc_otg_core_if_t *_core_if )
5379 +{
5380 +#ifndef DWC_HOST_ONLY // winder
5381 +    hprt0_data_t hprt0;
5382 +#endif
5383 +    gintsts_data_t gintsts;
5384 +
5385 +#ifndef DWC_HOST_ONLY
5386 +    DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");   
5387 +
5388 +    if (dwc_otg_is_device_mode(_core_if) ) {
5389 +        DWC_PRINT("SRP: Device mode\n");
5390 +    } else {
5391 +        DWC_PRINT("SRP: Host mode\n");
5392 +
5393 +        /* Turn on the port power bit. */
5394 +        hprt0.d32 = dwc_otg_read_hprt0( _core_if );
5395 +        hprt0.b.prtpwr = 1;
5396 +        dwc_write_reg32(_core_if->host_if->hprt0, hprt0.d32);
5397 +
5398 +        /* Start the Connection timer. So a message can be displayed
5399 +        * if connect does not occur within 10 seconds. */ 
5400 +        hcd_session_start( _core_if );
5401 +    }
5402 +#endif
5403 +
5404 +    /* Clear interrupt */
5405 +    gintsts.d32 = 0;
5406 +    gintsts.b.sessreqintr = 1;
5407 +    dwc_write_reg32 (&_core_if->core_global_regs->gintsts, gintsts.d32);
5408 +
5409 +    return 1;
5410 +}
5411 +
5412 +/** 
5413 + * This interrupt indicates that the DWC_otg controller has detected a
5414 + * resume or remote wakeup sequence. If the DWC_otg controller is in
5415 + * low power mode, the handler must brings the controller out of low
5416 + * power mode. The controller automatically begins resume
5417 + * signaling. The handler schedules a time to stop resume signaling.
5418 + */
5419 +int32_t dwc_otg_handle_wakeup_detected_intr( dwc_otg_core_if_t *_core_if )
5420 +{
5421 +       gintsts_data_t gintsts;
5422 +
5423 +       DWC_DEBUGPL(DBG_ANY, "++Resume and Remote Wakeup Detected Interrupt++\n");
5424 +
5425 +        if (dwc_otg_is_device_mode(_core_if) ) { 
5426 +                dctl_data_t dctl = {.d32=0};
5427 +                DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", 
5428 +                            dwc_read_reg32( &_core_if->dev_if->dev_global_regs->dsts));
5429 +#ifdef PARTIAL_POWER_DOWN
5430 +                if (_core_if->hwcfg4.b.power_optimiz) {
5431 +                        pcgcctl_data_t power = {.d32=0};
5432 +
5433 +                        power.d32 = dwc_read_reg32( _core_if->pcgcctl );
5434 +                        DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n", power.d32);
5435 +
5436 +                        power.b.stoppclk = 0;
5437 +                        dwc_write_reg32( _core_if->pcgcctl, power.d32);
5438 +
5439 +                        power.b.pwrclmp = 0;
5440 +                        dwc_write_reg32( _core_if->pcgcctl, power.d32);
5441 +
5442 +                        power.b.rstpdwnmodule = 0;
5443 +                        dwc_write_reg32( _core_if->pcgcctl, power.d32);
5444 +                }
5445 +#endif
5446 +                /* Clear the Remote Wakeup Signalling */
5447 +                dctl.b.rmtwkupsig = 1;
5448 +                dwc_modify_reg32( &_core_if->dev_if->dev_global_regs->dctl, 
5449 +                                  dctl.d32, 0 );
5450 +
5451 +                if (_core_if->pcd_cb && _core_if->pcd_cb->resume_wakeup) {
5452 +                        _core_if->pcd_cb->resume_wakeup( _core_if->pcd_cb->p );
5453 +                }
5454 +        
5455 +        } else {
5456 +                /*
5457 +                * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
5458 +                * so that OPT tests pass with all PHYs).
5459 +                */
5460 +                hprt0_data_t hprt0 = {.d32=0};
5461 +                pcgcctl_data_t pcgcctl = {.d32=0};
5462 +                /* Restart the Phy Clock */
5463 +                pcgcctl.b.stoppclk = 1;
5464 +                dwc_modify_reg32(_core_if->pcgcctl, pcgcctl.d32, 0);
5465 +                UDELAY(10);
5466 +                
5467 +                /* Now wait for 70 ms. */
5468 +                hprt0.d32 = dwc_otg_read_hprt0( _core_if );
5469 +                DWC_DEBUGPL(DBG_ANY,"Resume: HPRT0=%0x\n", hprt0.d32);
5470 +                MDELAY(70);
5471 +                hprt0.b.prtres = 0; /* Resume */
5472 +                dwc_write_reg32(_core_if->host_if->hprt0, hprt0.d32);                
5473 +                DWC_DEBUGPL(DBG_ANY,"Clear Resume: HPRT0=%0x\n", dwc_read_reg32(_core_if->host_if->hprt0));
5474 +        }        
5475 +
5476 +       /* Clear interrupt */
5477 +       gintsts.d32 = 0;
5478 +       gintsts.b.wkupintr = 1;
5479 +       dwc_write_reg32 (&_core_if->core_global_regs->gintsts, gintsts.d32);
5480 +
5481 +       return 1;
5482 +}
5483 +
5484 +/** 
5485 + * This interrupt indicates that a device has been disconnected from
5486 + * the root port. 
5487 + */
5488 +int32_t dwc_otg_handle_disconnect_intr( dwc_otg_core_if_t *_core_if)
5489 +{
5490 +       gintsts_data_t gintsts;
5491 +
5492 +       DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n", 
5493 +                    (dwc_otg_is_host_mode(_core_if)?"Host":"Device"), 
5494 +                    op_state_str(_core_if));
5495 +
5496 +/** @todo Consolidate this if statement. */
5497 +#ifndef DWC_HOST_ONLY
5498 +        if (_core_if->op_state == B_HOST) {
5499 +                /* If in device mode Disconnect and stop the HCD, then
5500 +                 * start the PCD. */
5501 +                hcd_disconnect( _core_if );
5502 +                pcd_start( _core_if );
5503 +                _core_if->op_state = B_PERIPHERAL;
5504 +        } else if (dwc_otg_is_device_mode(_core_if)) {
5505 +                gotgctl_data_t gotgctl = { .d32 = 0 }; 
5506 +                gotgctl.d32 = dwc_read_reg32(&_core_if->core_global_regs->gotgctl);
5507 +                if (gotgctl.b.hstsethnpen==1) {
5508 +                        /* Do nothing, if HNP in process the OTG
5509 +                         * interrupt "Host Negotiation Detected"
5510 +                         * interrupt will do the mode switch.
5511 +                         */
5512 +                } else if (gotgctl.b.devhnpen == 0) {
5513 +                        /* If in device mode Disconnect and stop the HCD, then
5514 +                         * start the PCD. */
5515 +                        hcd_disconnect( _core_if );
5516 +                        pcd_start( _core_if );
5517 +                        _core_if->op_state = B_PERIPHERAL;
5518 +                } else {
5519 +                        DWC_DEBUGPL(DBG_ANY,"!a_peripheral && !devhnpen\n");
5520 +                }
5521 +        } else {
5522 +                if (_core_if->op_state == A_HOST) {
5523 +                        /* A-Cable still connected but device disconnected. */
5524 +                        hcd_disconnect( _core_if );
5525 +                }
5526 +        }
5527 +#endif
5528 +/* Without OTG, we should use the disconnect function!? winder added.*/
5529 +#if 1 // NO OTG, so host only!!
5530 +        hcd_disconnect( _core_if );
5531 +#endif
5532 +   
5533 +       gintsts.d32 = 0;
5534 +       gintsts.b.disconnect = 1;
5535 +       dwc_write_reg32 (&_core_if->core_global_regs->gintsts, gintsts.d32);
5536 +       return 1;
5537 +}
5538 +/**
5539 + * This interrupt indicates that SUSPEND state has been detected on
5540 + * the USB.
5541 + * 
5542 + * For HNP the USB Suspend interrupt signals the change from
5543 + * "a_peripheral" to "a_host".
5544 + *
5545 + * When power management is enabled the core will be put in low power
5546 + * mode.
5547 + */
5548 +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t *_core_if )
5549 +{
5550 +        dsts_data_t dsts;
5551 +        gintsts_data_t gintsts;
5552 +
5553 +         //805141:<IFTW-fchang>.removed DWC_DEBUGPL(DBG_ANY,"USB SUSPEND\n");
5554 +
5555 +        if (dwc_otg_is_device_mode( _core_if ) ) {             
5556 +                /* Check the Device status register to determine if the Suspend
5557 +                 * state is active. */
5558 +                dsts.d32 = dwc_read_reg32( &_core_if->dev_if->dev_global_regs->dsts);
5559 +                DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
5560 +                DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
5561 +                            "HWCFG4.power Optimize=%d\n", 
5562 +                            dsts.b.suspsts, _core_if->hwcfg4.b.power_optimiz);
5563 +
5564 +
5565 +#ifdef PARTIAL_POWER_DOWN
5566 +/** @todo Add a module parameter for power management. */
5567 +        
5568 +                if (dsts.b.suspsts && _core_if->hwcfg4.b.power_optimiz) {
5569 +                        pcgcctl_data_t power = {.d32=0};
5570 +                        DWC_DEBUGPL(DBG_CIL, "suspend\n");
5571 +
5572 +                        power.b.pwrclmp = 1;
5573 +                        dwc_write_reg32( _core_if->pcgcctl, power.d32);
5574 +
5575 +                        power.b.rstpdwnmodule = 1;
5576 +                        dwc_modify_reg32( _core_if->pcgcctl, 0, power.d32);
5577 +
5578 +                        power.b.stoppclk = 1;
5579 +                        dwc_modify_reg32( _core_if->pcgcctl, 0, power.d32);
5580 +                
5581 +                } else {
5582 +                        DWC_DEBUGPL(DBG_ANY,"disconnect?\n");
5583 +                }
5584 +#endif
5585 +                /* PCD callback for suspend. */
5586 +                pcd_suspend(_core_if);
5587 +        } else {
5588 +                if (_core_if->op_state == A_PERIPHERAL) {
5589 +                        DWC_DEBUGPL(DBG_ANY,"a_peripheral->a_host\n");
5590 +                        /* Clear the a_peripheral flag, back to a_host. */
5591 +                        pcd_stop( _core_if );
5592 +                        hcd_start( _core_if );
5593 +                        _core_if->op_state = A_HOST;
5594 +                }                
5595 +        }
5596 +        
5597 +       /* Clear interrupt */
5598 +       gintsts.d32 = 0;
5599 +       gintsts.b.usbsuspend = 1;
5600 +       dwc_write_reg32( &_core_if->core_global_regs->gintsts, gintsts.d32);
5601 +
5602 +        return 1;
5603 +}
5604 +
5605 +
5606 +/**
5607 + * This function returns the Core Interrupt register.
5608 + */
5609 +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t *_core_if) 
5610 +{
5611 +        gintsts_data_t gintsts;
5612 +        gintmsk_data_t gintmsk;
5613 +        gintmsk_data_t gintmsk_common = {.d32=0};
5614 +       gintmsk_common.b.wkupintr = 1;
5615 +       gintmsk_common.b.sessreqintr = 1;
5616 +       gintmsk_common.b.conidstschng = 1;
5617 +       gintmsk_common.b.otgintr = 1;
5618 +       gintmsk_common.b.modemismatch = 1;
5619 +        gintmsk_common.b.disconnect = 1;
5620 +        gintmsk_common.b.usbsuspend = 1;
5621 +        /** @todo: The port interrupt occurs while in device 
5622 +         * mode. Added code to CIL to clear the interrupt for now! 
5623 +         */
5624 +        gintmsk_common.b.portintr = 1;
5625 +
5626 +        gintsts.d32 = dwc_read_reg32(&_core_if->core_global_regs->gintsts);
5627 +        gintmsk.d32 = dwc_read_reg32(&_core_if->core_global_regs->gintmsk);
5628 +#ifdef DEBUG
5629 +        /* if any common interrupts set */
5630 +        if (gintsts.d32 & gintmsk_common.d32) {
5631 +                DWC_DEBUGPL(DBG_ANY, "gintsts=%08x  gintmsk=%08x\n", 
5632 +                            gintsts.d32, gintmsk.d32);
5633 +        }
5634 +#endif        
5635 +        
5636 +        return ((gintsts.d32 & gintmsk.d32 ) & gintmsk_common.d32);
5637 +
5638 +}
5639 +
5640 +/**
5641 + * Common interrupt handler.
5642 + *
5643 + * The common interrupts are those that occur in both Host and Device mode. 
5644 + * This handler handles the following interrupts:
5645 + * - Mode Mismatch Interrupt
5646 + * - Disconnect Interrupt
5647 + * - OTG Interrupt
5648 + * - Connector ID Status Change Interrupt
5649 + * - Session Request Interrupt.
5650 + * - Resume / Remote Wakeup Detected Interrupt.
5651 + * 
5652 + */
5653 +extern int32_t dwc_otg_handle_common_intr( dwc_otg_core_if_t *_core_if )
5654 +{
5655 +       int retval = 0;
5656 +        gintsts_data_t gintsts;
5657 +
5658 +        gintsts.d32 = dwc_otg_read_common_intr(_core_if);
5659 +
5660 +        if (gintsts.b.modemismatch) {
5661 +                retval |= dwc_otg_handle_mode_mismatch_intr( _core_if );
5662 +        }
5663 +        if (gintsts.b.otgintr) {
5664 +                retval |= dwc_otg_handle_otg_intr( _core_if );
5665 +        }
5666 +        if (gintsts.b.conidstschng) {
5667 +                retval |= dwc_otg_handle_conn_id_status_change_intr( _core_if );
5668 +        }
5669 +        if (gintsts.b.disconnect) {
5670 +                retval |= dwc_otg_handle_disconnect_intr( _core_if );
5671 +        }
5672 +        if (gintsts.b.sessreqintr) {
5673 +                retval |= dwc_otg_handle_session_req_intr( _core_if );
5674 +        }
5675 +        if (gintsts.b.wkupintr) {
5676 +                retval |= dwc_otg_handle_wakeup_detected_intr( _core_if );
5677 +        }
5678 +        if (gintsts.b.usbsuspend) {
5679 +                retval |= dwc_otg_handle_usb_suspend_intr( _core_if );
5680 +        }
5681 +        if (gintsts.b.portintr && dwc_otg_is_device_mode(_core_if)) {
5682 +                /* The port interrupt occurs while in device mode with HPRT0
5683 +                 * Port Enable/Disable.
5684 +                 */
5685 +                gintsts.d32 = 0;
5686 +                gintsts.b.portintr = 1;
5687 +                dwc_write_reg32(&_core_if->core_global_regs->gintsts, 
5688 +                                gintsts.d32);
5689 +                retval |= 1;
5690 +                
5691 +        }
5692 +        return retval;
5693 +}
5694 --- /dev/null
5695 +++ b/drivers/usb/dwc_otg/dwc_otg_driver.c
5696 @@ -0,0 +1,1269 @@
5697 +/* ==========================================================================
5698 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_driver.c $
5699 + * $Revision: 1.1.1.1 $
5700 + * $Date: 2009-04-17 06:15:34 $
5701 + * $Change: 631780 $
5702 + *
5703 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
5704 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
5705 + * otherwise expressly agreed to in writing between Synopsys and you.
5706 + * 
5707 + * The Software IS NOT an item of Licensed Software or Licensed Product under
5708 + * any End User Software License Agreement or Agreement for Licensed Product
5709 + * with Synopsys or any supplement thereto. You are permitted to use and
5710 + * redistribute this Software in source and binary forms, with or without
5711 + * modification, provided that redistributions of source code must retain this
5712 + * notice. You may not view, use, disclose, copy or distribute this file or
5713 + * any information contained herein except pursuant to this license grant from
5714 + * Synopsys. If you do not agree with this notice, including the disclaimer
5715 + * below, then you are not authorized to use the Software.
5716 + * 
5717 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
5718 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5719 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5720 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
5721 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
5722 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
5723 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
5724 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5725 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5726 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5727 + * DAMAGE.
5728 + * ========================================================================== */
5729 +
5730 +/** @file
5731 + * The dwc_otg_driver module provides the initialization and cleanup entry
5732 + * points for the DWC_otg driver. This module will be dynamically installed
5733 + * after Linux is booted using the insmod command. When the module is
5734 + * installed, the dwc_otg_init function is called. When the module is
5735 + * removed (using rmmod), the dwc_otg_cleanup function is called.
5736 + * 
5737 + * This module also defines a data structure for the dwc_otg_driver, which is
5738 + * used in conjunction with the standard ARM lm_device structure. These
5739 + * structures allow the OTG driver to comply with the standard Linux driver
5740 + * model in which devices and drivers are registered with a bus driver. This
5741 + * has the benefit that Linux can expose attributes of the driver and device
5742 + * in its special sysfs file system. Users can then read or write files in
5743 + * this file system to perform diagnostics on the driver components or the
5744 + * device.
5745 + */
5746 +
5747 +#include <linux/kernel.h>
5748 +#include <linux/module.h>
5749 +#include <linux/moduleparam.h>
5750 +#include <linux/init.h>
5751 +#include <linux/gpio.h>
5752 +
5753 +#include <linux/device.h>
5754 +#include <linux/platform_device.h>
5755 +
5756 +#include <linux/errno.h>
5757 +#include <linux/types.h>
5758 +#include <linux/stat.h>  /* permission constants */
5759 +#include <linux/irq.h>
5760 +#include <asm/io.h>
5761 +
5762 +#include "dwc_otg_plat.h"
5763 +#include "dwc_otg_attr.h"
5764 +#include "dwc_otg_driver.h"
5765 +#include "dwc_otg_cil.h"
5766 +#include "dwc_otg_cil_ifx.h"
5767 +
5768 +// #include "dwc_otg_pcd.h" // device
5769 +#include "dwc_otg_hcd.h"   // host
5770 +
5771 +#include "dwc_otg_ifx.h" // for Infineon platform specific.
5772 +
5773 +#define        DWC_DRIVER_VERSION      "2.60a 22-NOV-2006"
5774 +#define        DWC_DRIVER_DESC         "HS OTG USB Controller driver"
5775 +
5776 +const char dwc_driver_name[] = "dwc_otg";
5777 +
5778 +static unsigned long dwc_iomem_base = IFX_USB_IOMEM_BASE;
5779 +int dwc_irq = LQ_USB_INT;
5780 +//int dwc_irq = 54;
5781 +//int dwc_irq = IFXMIPS_USB_OC_INT;
5782 +
5783 +extern int ifx_usb_hc_init(unsigned long base_addr, int irq);
5784 +extern void ifx_usb_hc_remove(void);
5785 +
5786 +/*-------------------------------------------------------------------------*/
5787 +/* Encapsulate the module parameter settings */
5788 +
5789 +static dwc_otg_core_params_t dwc_otg_module_params = {
5790 +        .opt = -1,
5791 +        .otg_cap = -1,
5792 +        .dma_enable = -1,
5793 +       .dma_burst_size = -1,
5794 +       .speed = -1,
5795 +       .host_support_fs_ls_low_power = -1,
5796 +       .host_ls_low_power_phy_clk = -1,
5797 +       .enable_dynamic_fifo = -1,
5798 +       .data_fifo_size = -1,
5799 +       .dev_rx_fifo_size = -1,
5800 +       .dev_nperio_tx_fifo_size = -1,
5801 +       .dev_perio_tx_fifo_size = /* dev_perio_tx_fifo_size_1 */ {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},  /* 15 */
5802 +       .host_rx_fifo_size = -1,
5803 +       .host_nperio_tx_fifo_size = -1,
5804 +       .host_perio_tx_fifo_size = -1,
5805 +       .max_transfer_size = -1,
5806 +       .max_packet_count = -1,
5807 +       .host_channels = -1,
5808 +       .dev_endpoints = -1,
5809 +       .phy_type = -1,
5810 +        .phy_utmi_width = -1,
5811 +        .phy_ulpi_ddr = -1,
5812 +        .phy_ulpi_ext_vbus = -1,
5813 +       .i2c_enable = -1,
5814 +       .ulpi_fs_ls = -1,
5815 +       .ts_dline = -1,
5816 +       .en_multiple_tx_fifo = -1,
5817 +       .dev_tx_fifo_size = { /* dev_tx_fifo_size */
5818 +     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
5819 +       }, /* 15 */
5820 +       .thr_ctl = -1,
5821 +       .tx_thr_length = -1,
5822 +       .rx_thr_length = -1,
5823 +};
5824 +
5825 +/**
5826 + * This function shows the Driver Version.
5827 + */
5828 +static ssize_t version_show(struct device_driver *dev, char *buf)
5829 +{
5830 +        return snprintf(buf, sizeof(DWC_DRIVER_VERSION)+2,"%s\n", 
5831 +                        DWC_DRIVER_VERSION);
5832 +}
5833 +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
5834 +
5835 +/**
5836 + * Global Debug Level Mask.
5837 + */
5838 +uint32_t g_dbg_lvl = 0xff; /* OFF */
5839 +
5840 +/**
5841 + * This function shows the driver Debug Level.
5842 + */
5843 +static ssize_t dbg_level_show(struct device_driver *_drv, char *_buf)
5844 +{
5845 +        return sprintf(_buf, "0x%0x\n", g_dbg_lvl);
5846 +}
5847 +/**
5848 + * This function stores the driver Debug Level.
5849 + */
5850 +static ssize_t dbg_level_store(struct device_driver *_drv, const char *_buf, 
5851 +                               size_t _count)
5852 +{
5853 +       g_dbg_lvl = simple_strtoul(_buf, NULL, 16);
5854 +        return _count;
5855 +}
5856 +static DRIVER_ATTR(debuglevel, S_IRUGO|S_IWUSR, dbg_level_show, dbg_level_store);
5857 +
5858 +/**
5859 + * This function is called during module intialization to verify that
5860 + * the module parameters are in a valid state.
5861 + */
5862 +static int check_parameters(dwc_otg_core_if_t *core_if)
5863 +{
5864 +       int i;
5865 +       int retval = 0;
5866 +
5867 +/* Checks if the parameter is outside of its valid range of values */
5868 +#define DWC_OTG_PARAM_TEST(_param_,_low_,_high_) \
5869 +       ((dwc_otg_module_params._param_ < (_low_)) || \
5870 +         (dwc_otg_module_params._param_ > (_high_)))
5871 +
5872 +/* If the parameter has been set by the user, check that the parameter value is
5873 + * within the value range of values.  If not, report a module error. */
5874 +#define DWC_OTG_PARAM_ERR(_param_,_low_,_high_,_string_) \
5875 +        do { \
5876 +               if (dwc_otg_module_params._param_ != -1) { \
5877 +                       if (DWC_OTG_PARAM_TEST(_param_,(_low_),(_high_))) { \
5878 +                               DWC_ERROR("`%d' invalid for parameter `%s'\n", \
5879 +                                         dwc_otg_module_params._param_, _string_); \
5880 +                               dwc_otg_module_params._param_ = dwc_param_##_param_##_default; \
5881 +                               retval ++; \
5882 +                       } \
5883 +               } \
5884 +       } while (0)
5885 +
5886 +       DWC_OTG_PARAM_ERR(opt,0,1,"opt");
5887 +       DWC_OTG_PARAM_ERR(otg_cap,0,2,"otg_cap");
5888 +        DWC_OTG_PARAM_ERR(dma_enable,0,1,"dma_enable");
5889 +       DWC_OTG_PARAM_ERR(speed,0,1,"speed");
5890 +       DWC_OTG_PARAM_ERR(host_support_fs_ls_low_power,0,1,"host_support_fs_ls_low_power");
5891 +       DWC_OTG_PARAM_ERR(host_ls_low_power_phy_clk,0,1,"host_ls_low_power_phy_clk");
5892 +       DWC_OTG_PARAM_ERR(enable_dynamic_fifo,0,1,"enable_dynamic_fifo");
5893 +       DWC_OTG_PARAM_ERR(data_fifo_size,32,32768,"data_fifo_size");
5894 +       DWC_OTG_PARAM_ERR(dev_rx_fifo_size,16,32768,"dev_rx_fifo_size");
5895 +       DWC_OTG_PARAM_ERR(dev_nperio_tx_fifo_size,16,32768,"dev_nperio_tx_fifo_size");
5896 +       DWC_OTG_PARAM_ERR(host_rx_fifo_size,16,32768,"host_rx_fifo_size");
5897 +       DWC_OTG_PARAM_ERR(host_nperio_tx_fifo_size,16,32768,"host_nperio_tx_fifo_size");
5898 +       DWC_OTG_PARAM_ERR(host_perio_tx_fifo_size,16,32768,"host_perio_tx_fifo_size");
5899 +       DWC_OTG_PARAM_ERR(max_transfer_size,2047,524288,"max_transfer_size");
5900 +       DWC_OTG_PARAM_ERR(max_packet_count,15,511,"max_packet_count");
5901 +       DWC_OTG_PARAM_ERR(host_channels,1,16,"host_channels");
5902 +       DWC_OTG_PARAM_ERR(dev_endpoints,1,15,"dev_endpoints");
5903 +       DWC_OTG_PARAM_ERR(phy_type,0,2,"phy_type");
5904 +        DWC_OTG_PARAM_ERR(phy_ulpi_ddr,0,1,"phy_ulpi_ddr");
5905 +        DWC_OTG_PARAM_ERR(phy_ulpi_ext_vbus,0,1,"phy_ulpi_ext_vbus");
5906 +       DWC_OTG_PARAM_ERR(i2c_enable,0,1,"i2c_enable");
5907 +       DWC_OTG_PARAM_ERR(ulpi_fs_ls,0,1,"ulpi_fs_ls");
5908 +       DWC_OTG_PARAM_ERR(ts_dline,0,1,"ts_dline");
5909 +
5910 +       if (dwc_otg_module_params.dma_burst_size != -1) {
5911 +               if (DWC_OTG_PARAM_TEST(dma_burst_size,1,1) &&
5912 +                   DWC_OTG_PARAM_TEST(dma_burst_size,4,4) &&
5913 +                   DWC_OTG_PARAM_TEST(dma_burst_size,8,8) &&
5914 +                   DWC_OTG_PARAM_TEST(dma_burst_size,16,16) &&
5915 +                   DWC_OTG_PARAM_TEST(dma_burst_size,32,32) &&
5916 +                   DWC_OTG_PARAM_TEST(dma_burst_size,64,64) &&
5917 +                   DWC_OTG_PARAM_TEST(dma_burst_size,128,128) &&
5918 +                   DWC_OTG_PARAM_TEST(dma_burst_size,256,256))
5919 +               {
5920 +                       DWC_ERROR("`%d' invalid for parameter `dma_burst_size'\n", 
5921 +                                 dwc_otg_module_params.dma_burst_size);
5922 +                       dwc_otg_module_params.dma_burst_size = 32;
5923 +                       retval ++;
5924 +               }
5925 +       }
5926 +
5927 +       if (dwc_otg_module_params.phy_utmi_width != -1) {
5928 +               if (DWC_OTG_PARAM_TEST(phy_utmi_width,8,8) &&
5929 +                   DWC_OTG_PARAM_TEST(phy_utmi_width,16,16)) 
5930 +               {
5931 +                       DWC_ERROR("`%d' invalid for parameter `phy_utmi_width'\n", 
5932 +                                 dwc_otg_module_params.phy_utmi_width);
5933 +                       //dwc_otg_module_params.phy_utmi_width = 16;
5934 +                       dwc_otg_module_params.phy_utmi_width = 8;
5935 +                       retval ++;
5936 +               }
5937 +       }
5938 +
5939 +       for (i=0; i<15; i++) {
5940 +               /** @todo should be like above */
5941 +               //DWC_OTG_PARAM_ERR(dev_perio_tx_fifo_size[i],4,768,"dev_perio_tx_fifo_size");
5942 +               if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
5943 +                       if (DWC_OTG_PARAM_TEST(dev_perio_tx_fifo_size[i],4,768)) {
5944 +                               DWC_ERROR("`%d' invalid for parameter `%s_%d'\n",
5945 +                                         dwc_otg_module_params.dev_perio_tx_fifo_size[i], "dev_perio_tx_fifo_size", i);
5946 +                               dwc_otg_module_params.dev_perio_tx_fifo_size[i] = dwc_param_dev_perio_tx_fifo_size_default;
5947 +                               retval ++;
5948 +                       }
5949 +               }
5950 +       }
5951 +
5952 +       DWC_OTG_PARAM_ERR(en_multiple_tx_fifo, 0, 1, "en_multiple_tx_fifo");
5953 +       for (i = 0; i < 15; i++) {
5954 +               /** @todo should be like above */
5955 +                   //DWC_OTG_PARAM_ERR(dev_tx_fifo_size[i],4,768,"dev_tx_fifo_size");
5956 +                   if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
5957 +                       if (DWC_OTG_PARAM_TEST(dev_tx_fifo_size[i], 4, 768)) {
5958 +                               DWC_ERROR("`%d' invalid for parameter `%s_%d'\n",
5959 +                                       dwc_otg_module_params.dev_tx_fifo_size[i],
5960 +                                    "dev_tx_fifo_size", i);
5961 +                               dwc_otg_module_params.dev_tx_fifo_size[i] =
5962 +                                   dwc_param_dev_tx_fifo_size_default;
5963 +                               retval++;
5964 +                       }
5965 +               }
5966 +       }
5967 +       DWC_OTG_PARAM_ERR(thr_ctl, 0, 7, "thr_ctl");
5968 +       DWC_OTG_PARAM_ERR(tx_thr_length, 8, 128, "tx_thr_length");
5969 +       DWC_OTG_PARAM_ERR(rx_thr_length, 8, 128, "rx_thr_length");
5970 +
5971 +       /* At this point, all module parameters that have been set by the user
5972 +        * are valid, and those that have not are left unset.  Now set their
5973 +        * default values and/or check the parameters against the hardware
5974 +        * configurations of the OTG core. */
5975 +
5976 +
5977 +
5978 +/* This sets the parameter to the default value if it has not been set by the
5979 + * user */
5980 +#define DWC_OTG_PARAM_SET_DEFAULT(_param_) \
5981 +       ({ \
5982 +               int changed = 1; \
5983 +               if (dwc_otg_module_params._param_ == -1) { \
5984 +                       changed = 0; \
5985 +                       dwc_otg_module_params._param_ = dwc_param_##_param_##_default; \
5986 +               } \
5987 +               changed; \
5988 +       })
5989 +
5990 +/* This checks the macro agains the hardware configuration to see if it is
5991 + * valid.  It is possible that the default value could be invalid.  In this
5992 + * case, it will report a module error if the user touched the parameter.
5993 + * Otherwise it will adjust the value without any error. */
5994 +#define DWC_OTG_PARAM_CHECK_VALID(_param_,_str_,_is_valid_,_set_valid_) \
5995 +       ({ \
5996 +               int changed = DWC_OTG_PARAM_SET_DEFAULT(_param_); \
5997 +               int error = 0; \
5998 +               if (!(_is_valid_)) { \
5999 +                       if (changed) { \
6000 +                               DWC_ERROR("`%d' invalid for parameter `%s'.  Check HW configuration.\n", dwc_otg_module_params._param_,_str_); \
6001 +                               error = 1; \
6002 +                       } \
6003 +                       dwc_otg_module_params._param_ = (_set_valid_); \
6004 +               } \
6005 +               error; \
6006 +       })
6007 +
6008 +       /* OTG Cap */
6009 +       retval += DWC_OTG_PARAM_CHECK_VALID(otg_cap,"otg_cap",
6010 +                  ({
6011 +                         int valid;
6012 +                         valid = 1;
6013 +                         switch (dwc_otg_module_params.otg_cap) {
6014 +                         case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
6015 +                                 if (core_if->hwcfg2.b.op_mode != DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG) valid = 0;
6016 +                                 break;
6017 +                         case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
6018 +                                 if ((core_if->hwcfg2.b.op_mode != DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG) &&
6019 +                                     (core_if->hwcfg2.b.op_mode != DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG) &&
6020 +                                     (core_if->hwcfg2.b.op_mode != DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) &&
6021 +                                     (core_if->hwcfg2.b.op_mode != DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST))
6022 +                                 {
6023 +                                         valid = 0;
6024 +                                 }
6025 +                                 break;
6026 +                         case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
6027 +                                 /* always valid */
6028 +                                 break;
6029 +                         } 
6030 +                         valid;
6031 +                 }),
6032 +                  (((core_if->hwcfg2.b.op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG) ||
6033 +                   (core_if->hwcfg2.b.op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG) ||
6034 +                   (core_if->hwcfg2.b.op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
6035 +                   (core_if->hwcfg2.b.op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
6036 +                  DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
6037 +                  DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE));
6038 +       
6039 +       retval += DWC_OTG_PARAM_CHECK_VALID(dma_enable,"dma_enable",
6040 +                                           ((dwc_otg_module_params.dma_enable == 1) && (core_if->hwcfg2.b.architecture == 0)) ? 0 : 1, 
6041 +                                           0);
6042 +
6043 +       retval += DWC_OTG_PARAM_CHECK_VALID(opt,"opt",
6044 +                                           1,
6045 +                                           0);
6046 +
6047 +       DWC_OTG_PARAM_SET_DEFAULT(dma_burst_size);
6048 +
6049 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_support_fs_ls_low_power,
6050 +                                           "host_support_fs_ls_low_power",
6051 +                                           1, 0);
6052 +
6053 +       retval += DWC_OTG_PARAM_CHECK_VALID(enable_dynamic_fifo,
6054 +                                 "enable_dynamic_fifo",
6055 +                                 ((dwc_otg_module_params.enable_dynamic_fifo == 0) ||
6056 +                                  (core_if->hwcfg2.b.dynamic_fifo == 1)), 0);
6057 +
6058 +
6059 +       retval += DWC_OTG_PARAM_CHECK_VALID(data_fifo_size,
6060 +                                 "data_fifo_size",
6061 +                                 (dwc_otg_module_params.data_fifo_size <= core_if->hwcfg3.b.dfifo_depth),
6062 +                                 core_if->hwcfg3.b.dfifo_depth);
6063 +
6064 +       retval += DWC_OTG_PARAM_CHECK_VALID(dev_rx_fifo_size,
6065 +                                 "dev_rx_fifo_size",
6066 +                                 (dwc_otg_module_params.dev_rx_fifo_size <= dwc_read_reg32(&core_if->core_global_regs->grxfsiz)),
6067 +                                 dwc_read_reg32(&core_if->core_global_regs->grxfsiz));
6068 +
6069 +       retval += DWC_OTG_PARAM_CHECK_VALID(dev_nperio_tx_fifo_size,
6070 +                                 "dev_nperio_tx_fifo_size",
6071 +                                 (dwc_otg_module_params.dev_nperio_tx_fifo_size <= (dwc_read_reg32(&core_if->core_global_regs->gnptxfsiz) >> 16)),
6072 +                                 (dwc_read_reg32(&core_if->core_global_regs->gnptxfsiz) >> 16));
6073 +
6074 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_rx_fifo_size,
6075 +                                           "host_rx_fifo_size",
6076 +                                           (dwc_otg_module_params.host_rx_fifo_size <= dwc_read_reg32(&core_if->core_global_regs->grxfsiz)),
6077 +                                           dwc_read_reg32(&core_if->core_global_regs->grxfsiz));
6078 +
6079 +
6080 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_nperio_tx_fifo_size,
6081 +                                 "host_nperio_tx_fifo_size",
6082 +                                 (dwc_otg_module_params.host_nperio_tx_fifo_size <= (dwc_read_reg32(&core_if->core_global_regs->gnptxfsiz) >> 16)),
6083 +                                 (dwc_read_reg32(&core_if->core_global_regs->gnptxfsiz) >> 16));
6084 +
6085 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_perio_tx_fifo_size,
6086 +                                           "host_perio_tx_fifo_size",
6087 +                                           (dwc_otg_module_params.host_perio_tx_fifo_size <= ((dwc_read_reg32(&core_if->core_global_regs->hptxfsiz) >> 16))),
6088 +                                           ((dwc_read_reg32(&core_if->core_global_regs->hptxfsiz) >> 16)));
6089 +
6090 +       retval += DWC_OTG_PARAM_CHECK_VALID(max_transfer_size,
6091 +                                 "max_transfer_size",
6092 +                                 (dwc_otg_module_params.max_transfer_size < (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))),
6093 +                                 ((1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11)) - 1));
6094 +
6095 +       retval += DWC_OTG_PARAM_CHECK_VALID(max_packet_count,
6096 +                                 "max_packet_count",
6097 +                                 (dwc_otg_module_params.max_packet_count < (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))),
6098 +                                 ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1));
6099 +
6100 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_channels,
6101 +                                 "host_channels",
6102 +                                 (dwc_otg_module_params.host_channels <= (core_if->hwcfg2.b.num_host_chan + 1)),
6103 +                                 (core_if->hwcfg2.b.num_host_chan + 1));
6104 +
6105 +       retval += DWC_OTG_PARAM_CHECK_VALID(dev_endpoints,
6106 +                                 "dev_endpoints",
6107 +                                 (dwc_otg_module_params.dev_endpoints <= (core_if->hwcfg2.b.num_dev_ep)),
6108 +                                 core_if->hwcfg2.b.num_dev_ep);
6109 +
6110 +/*
6111 + * Define the following to disable the FS PHY Hardware checking.  This is for
6112 + * internal testing only.
6113 + *
6114 + * #define NO_FS_PHY_HW_CHECKS 
6115 + */
6116 +
6117 +#ifdef NO_FS_PHY_HW_CHECKS
6118 +       retval += DWC_OTG_PARAM_CHECK_VALID(phy_type,
6119 +                                           "phy_type", 1, 0);
6120 +#else
6121 +       retval += DWC_OTG_PARAM_CHECK_VALID(phy_type,
6122 +                                           "phy_type",
6123 +                                           ({
6124 +                                                   int valid = 0;
6125 +                                                   if ((dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_UTMI) &&
6126 +                                                       ((core_if->hwcfg2.b.hs_phy_type == 1) || 
6127 +                                                        (core_if->hwcfg2.b.hs_phy_type == 3)))
6128 +                                                   {
6129 +                                                           valid = 1;
6130 +                                                   }
6131 +                                                   else if ((dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_ULPI) &&
6132 +                                                            ((core_if->hwcfg2.b.hs_phy_type == 2) || 
6133 +                                                             (core_if->hwcfg2.b.hs_phy_type == 3)))
6134 +                                                   {
6135 +                                                           valid = 1;
6136 +                                                   }
6137 +                                                   else if ((dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_FS) &&
6138 +                                                            (core_if->hwcfg2.b.fs_phy_type == 1))
6139 +                                                   {
6140 +                                                           valid = 1;
6141 +                                                   }
6142 +                                                   valid;
6143 +                                           }),
6144 +                                           ({
6145 +                                                   int set = DWC_PHY_TYPE_PARAM_FS;
6146 +                                                   if (core_if->hwcfg2.b.hs_phy_type) { 
6147 +                                                           if ((core_if->hwcfg2.b.hs_phy_type == 3) || 
6148 +                                                               (core_if->hwcfg2.b.hs_phy_type == 1)) {
6149 +                                                                   set = DWC_PHY_TYPE_PARAM_UTMI;
6150 +                                                           }
6151 +                                                           else {
6152 +                                                                   set = DWC_PHY_TYPE_PARAM_ULPI;
6153 +                                                           }
6154 +                                                   }
6155 +                                                   set;
6156 +                                           }));
6157 +#endif
6158 +
6159 +       retval += DWC_OTG_PARAM_CHECK_VALID(speed,"speed",
6160 +                                           (dwc_otg_module_params.speed == 0) && (dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_FS) ? 0 : 1,
6161 +                                           dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
6162 +
6163 +       retval += DWC_OTG_PARAM_CHECK_VALID(host_ls_low_power_phy_clk,
6164 +                                           "host_ls_low_power_phy_clk",
6165 +                                           ((dwc_otg_module_params.host_ls_low_power_phy_clk == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ) && (dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_FS) ? 0 : 1),
6166 +                                           ((dwc_otg_module_params.phy_type == DWC_PHY_TYPE_PARAM_FS) ? DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ : DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ));
6167 +
6168 +        DWC_OTG_PARAM_SET_DEFAULT(phy_ulpi_ddr);
6169 +        DWC_OTG_PARAM_SET_DEFAULT(phy_ulpi_ext_vbus);
6170 +        DWC_OTG_PARAM_SET_DEFAULT(phy_utmi_width);
6171 +        DWC_OTG_PARAM_SET_DEFAULT(ulpi_fs_ls);
6172 +        DWC_OTG_PARAM_SET_DEFAULT(ts_dline);
6173 +
6174 +#ifdef NO_FS_PHY_HW_CHECKS
6175 +       retval += DWC_OTG_PARAM_CHECK_VALID(i2c_enable,
6176 +                                           "i2c_enable", 1, 0);
6177 +#else
6178 +       retval += DWC_OTG_PARAM_CHECK_VALID(i2c_enable,
6179 +                                           "i2c_enable",
6180 +                                           (dwc_otg_module_params.i2c_enable == 1) && (core_if->hwcfg3.b.i2c == 0) ? 0 : 1,
6181 +                                           0);
6182 +#endif
6183 +
6184 +       for (i=0; i<16; i++) {
6185 +
6186 +               int changed = 1;
6187 +               int error = 0;
6188 +
6189 +               if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] == -1) {
6190 +                       changed = 0;
6191 +                       dwc_otg_module_params.dev_perio_tx_fifo_size[i] = dwc_param_dev_perio_tx_fifo_size_default;
6192 +               }
6193 +               if (!(dwc_otg_module_params.dev_perio_tx_fifo_size[i] <= (dwc_read_reg32(&core_if->core_global_regs->dptxfsiz_dieptxf[i])))) {
6194 +                       if (changed) {
6195 +                               DWC_ERROR("`%d' invalid for parameter `dev_perio_fifo_size_%d'.  Check HW configuration.\n", dwc_otg_module_params.dev_perio_tx_fifo_size[i],i);
6196 +                               error = 1;
6197 +                       }
6198 +                       dwc_otg_module_params.dev_perio_tx_fifo_size[i] = dwc_read_reg32(&core_if->core_global_regs->dptxfsiz_dieptxf[i]);
6199 +               }
6200 +               retval += error;
6201 +       }
6202 +
6203 +       retval += DWC_OTG_PARAM_CHECK_VALID(en_multiple_tx_fifo,
6204 +                               "en_multiple_tx_fifo",
6205 +                               ((dwc_otg_module_params.en_multiple_tx_fifo == 1) &&
6206 +                               (core_if->hwcfg4.b.ded_fifo_en == 0)) ? 0 : 1, 0);
6207 +
6208 +       for (i = 0; i < 16; i++) {
6209 +               int changed = 1;
6210 +               int error = 0;
6211 +               if (dwc_otg_module_params.dev_tx_fifo_size[i] == -1) {
6212 +                       changed = 0;
6213 +                       dwc_otg_module_params.dev_tx_fifo_size[i] =
6214 +                           dwc_param_dev_tx_fifo_size_default;
6215 +               }
6216 +               if (!(dwc_otg_module_params.dev_tx_fifo_size[i] <=
6217 +                    (dwc_read_reg32(&core_if->core_global_regs->dptxfsiz_dieptxf[i])))) {
6218 +                       if (changed) {
6219 +                               DWC_ERROR("%d' invalid for parameter `dev_perio_fifo_size_%d'."
6220 +                                       "Check HW configuration.\n",dwc_otg_module_params.dev_tx_fifo_size[i],i);
6221 +                               error = 1;
6222 +                       }
6223 +                       dwc_otg_module_params.dev_tx_fifo_size[i] =
6224 +                           dwc_read_reg32(&core_if->core_global_regs->dptxfsiz_dieptxf[i]);
6225 +               }
6226 +               retval += error;
6227 +       }
6228 +       DWC_OTG_PARAM_SET_DEFAULT(thr_ctl);
6229 +       DWC_OTG_PARAM_SET_DEFAULT(tx_thr_length);
6230 +       DWC_OTG_PARAM_SET_DEFAULT(rx_thr_length);
6231 +       return retval;
6232 +} // check_parameters 
6233 +
6234 +
6235 +/** 
6236 + * This function is the top level interrupt handler for the Common
6237 + * (Device and host modes) interrupts.
6238 + */
6239 +static irqreturn_t dwc_otg_common_irq(int _irq, void *_dev)
6240 +{
6241 +       dwc_otg_device_t *otg_dev = _dev;
6242 +       int32_t retval = IRQ_NONE;
6243 +
6244 +       retval = dwc_otg_handle_common_intr( otg_dev->core_if );
6245 +
6246 +       mask_and_ack_ifx_irq (_irq);
6247 +    
6248 +       return IRQ_RETVAL(retval);
6249 +}
6250 +
6251 +
6252 +/**
6253 + * This function is called when a DWC_OTG device is unregistered with the
6254 + * dwc_otg_driver. This happens, for example, when the rmmod command is
6255 + * executed. The device may or may not be electrically present. If it is
6256 + * present, the driver stops device processing. Any resources used on behalf
6257 + * of this device are freed.
6258 + *
6259 + * @return
6260 + */
6261 +static int
6262 +dwc_otg_driver_remove(struct platform_device *_dev)
6263 +{
6264 +    //dwc_otg_device_t *otg_dev = dev_get_drvdata(&_dev->dev);
6265 +    dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
6266 +
6267 +    DWC_DEBUGPL(DBG_ANY, "%s(%p)\n", __func__, _dev);
6268 +
6269 +    if (otg_dev == NULL) {
6270 +        /* Memory allocation for the dwc_otg_device failed. */
6271 +        return 0;
6272 +    }
6273 +
6274 +    /*
6275 +    * Free the IRQ 
6276 +    */
6277 +    if (otg_dev->common_irq_installed) {
6278 +        free_irq( otg_dev->irq, otg_dev );
6279 +    }
6280 +
6281 +#ifndef DWC_DEVICE_ONLY
6282 +    if (otg_dev->hcd != NULL) {
6283 +        dwc_otg_hcd_remove(&_dev->dev);
6284 +    }
6285 +#endif
6286 +       printk("after removehcd\n");
6287 +
6288 +// Note: Integrate HOST and DEVICE(Gadget) is not planned yet.
6289 +#ifndef DWC_HOST_ONLY
6290 +    if (otg_dev->pcd != NULL) {
6291 +        dwc_otg_pcd_remove(otg_dev);
6292 +    }
6293 +#endif
6294 +    if (otg_dev->core_if != NULL) {
6295 +        dwc_otg_cil_remove( otg_dev->core_if );
6296 +    }
6297 +       printk("after removecil\n");
6298 +
6299 +    /*
6300 +     * Remove the device attributes
6301 +     */
6302 +    dwc_otg_attr_remove(&_dev->dev);
6303 +       printk("after removeattr\n");
6304 +
6305 +    /*
6306 +     * Return the memory.
6307 +     */
6308 +    if (otg_dev->base != NULL) {
6309 +        iounmap(otg_dev->base);
6310 +    }
6311 +       if (otg_dev->phys_addr != 0) {
6312 +               release_mem_region(otg_dev->phys_addr, otg_dev->base_len);
6313 +       }
6314 +    kfree(otg_dev);
6315 +        
6316 +    /*
6317 +     * Clear the drvdata pointer.
6318 +     */
6319 +       //dev_set_drvdata(&_dev->dev, 0);
6320 +    platform_set_drvdata(_dev, 0);
6321 +    return 0;
6322 +}
6323 +
6324 +/**
6325 + * This function is called when an DWC_OTG device is bound to a
6326 + * dwc_otg_driver. It creates the driver components required to
6327 + * control the device (CIL, HCD, and PCD) and it initializes the
6328 + * device. The driver components are stored in a dwc_otg_device
6329 + * structure. A reference to the dwc_otg_device is saved in the
6330 + * lm_device. This allows the driver to access the dwc_otg_device
6331 + * structure on subsequent calls to driver methods for this device.
6332 + *
6333 + * @return
6334 + */
6335 +static int __devinit
6336 +dwc_otg_driver_probe(struct platform_device *_dev)
6337 +{
6338 +    int retval = 0;
6339 +    dwc_otg_device_t *dwc_otg_device;
6340 +    int32_t    snpsid;
6341 +       struct resource *res;
6342 +       gusbcfg_data_t usbcfg = {.d32 = 0};
6343 +
6344 +       // GPIOs
6345 +       gpio_request(_dev->dev.platform_data, "USB_POWER");
6346 +       gpio_direction_output(_dev->dev.platform_data, 1);
6347 +
6348 +       dev_dbg(&_dev->dev, "dwc_otg_driver_probe (%p)\n", _dev);
6349 +
6350 +    dwc_otg_device = kmalloc(sizeof(dwc_otg_device_t), GFP_KERNEL);
6351 +    if (dwc_otg_device == 0) {
6352 +        dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
6353 +        retval = -ENOMEM;
6354 +        goto fail;
6355 +    }
6356 +    memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
6357 +    dwc_otg_device->reg_offset = 0xFFFFFFFF;
6358 +
6359 +    /*
6360 +     * Retrieve the memory and IRQ resources.
6361 +     */
6362 +       dwc_otg_device->irq = platform_get_irq(_dev, 0);
6363 +       if (dwc_otg_device->irq == 0) {
6364 +               dev_err(&_dev->dev, "no device irq\n");
6365 +               retval = -ENODEV;
6366 +               goto fail;
6367 +       }
6368 +       dev_dbg(&_dev->dev, "OTG - device irq: %d\n", dwc_otg_device->irq);
6369 +       res = platform_get_resource(_dev, IORESOURCE_MEM, 0);
6370 +       if (res == NULL) {
6371 +               dev_err(&_dev->dev, "no CSR address\n");
6372 +               retval = -ENODEV;
6373 +               goto fail;
6374 +       }
6375 +       dev_dbg(&_dev->dev, "OTG - ioresource_mem start0x%08x: end:0x%08x\n",
6376 +               (unsigned)res->start, (unsigned)res->end);
6377 +       dwc_otg_device->phys_addr = res->start;
6378 +       dwc_otg_device->base_len = res->end - res->start + 1;
6379 +       if (request_mem_region(dwc_otg_device->phys_addr, dwc_otg_device->base_len,
6380 +           dwc_driver_name) == NULL) {
6381 +               dev_err(&_dev->dev, "request_mem_region failed\n");
6382 +               retval = -EBUSY;
6383 +               goto fail;
6384 +       }
6385 +
6386 +       /*
6387 +     * Map the DWC_otg Core memory into virtual address space.
6388 +     */
6389 +    dwc_otg_device->base = ioremap_nocache(dwc_otg_device->phys_addr, dwc_otg_device->base_len);
6390 +    if (dwc_otg_device->base == NULL)    {
6391 +        dev_err(&_dev->dev, "ioremap() failed\n");
6392 +        retval = -ENOMEM;
6393 +        goto fail;
6394 +    }
6395 +    dev_dbg(&_dev->dev, "mapped base=0x%08x\n", (unsigned)dwc_otg_device->base);
6396 +
6397 +    /*
6398 +     * Attempt to ensure this device is really a DWC_otg Controller.
6399 +     * Read and verify the SNPSID register contents. The value should be
6400 +     * 0x45F42XXX, which corresponds to "OT2", as in "OTG version 2.XX".
6401 +     */
6402 +    snpsid = dwc_read_reg32((uint32_t *)((uint8_t *)dwc_otg_device->base + 0x40));
6403 +    if ((snpsid & 0xFFFFF000) != 0x4F542000) {
6404 +        dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n", snpsid);
6405 +        retval = -EINVAL;
6406 +        goto fail;
6407 +    }
6408 +
6409 +    /*
6410 +     * Initialize driver data to point to the global DWC_otg
6411 +     * Device structure.
6412 +     */
6413 +    platform_set_drvdata(_dev, dwc_otg_device);
6414 +    dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
6415 +    dwc_otg_device->core_if = dwc_otg_cil_init( dwc_otg_device->base, &dwc_otg_module_params);
6416 +    if (dwc_otg_device->core_if == 0) {
6417 +        dev_err(&_dev->dev, "CIL initialization failed!\n");
6418 +        retval = -ENOMEM;
6419 +        goto fail;
6420 +    }
6421 +       
6422 +    /*
6423 +     * Validate parameter values.
6424 +     */
6425 +    if (check_parameters(dwc_otg_device->core_if) != 0) {
6426 +        retval = -EINVAL;
6427 +        goto fail;
6428 +    }
6429 +
6430 +       /* Added for PLB DMA phys virt mapping */
6431 +       //dwc_otg_device->core_if->phys_addr = dwc_otg_device->phys_addr;
6432 +    /*
6433 +     * Create Device Attributes in sysfs
6434 +     */  
6435 +    dwc_otg_attr_create (&_dev->dev);
6436 +
6437 +    /*
6438 +     * Disable the global interrupt until all the interrupt
6439 +     * handlers are installed.
6440 +     */
6441 +    dwc_otg_disable_global_interrupts( dwc_otg_device->core_if );
6442 +    /*
6443 +     * Install the interrupt handler for the common interrupts before
6444 +     * enabling common interrupts in core_init below.
6445 +     */
6446 +    DWC_DEBUGPL( DBG_CIL, "registering (common) handler for irq%d\n", dwc_otg_device->irq);
6447 +
6448 +    retval = request_irq((unsigned int)dwc_otg_device->irq, dwc_otg_common_irq,
6449 +        //SA_INTERRUPT|SA_SHIRQ, "dwc_otg", (void *)dwc_otg_device );
6450 +        IRQF_SHARED, "dwc_otg", (void *)dwc_otg_device );
6451 +        //IRQF_DISABLED, "dwc_otg", (void *)dwc_otg_device );
6452 +    if (retval != 0) {
6453 +        DWC_ERROR("request of irq%d failed retval: %d\n", dwc_otg_device->irq, retval);
6454 +        retval = -EBUSY;
6455 +        goto fail;
6456 +    } else {
6457 +        dwc_otg_device->common_irq_installed = 1;
6458 +    }
6459 +
6460 +    /*
6461 +     * Initialize the DWC_otg core.
6462 +     */
6463 +    dwc_otg_core_init( dwc_otg_device->core_if );
6464 +
6465 +
6466 +#ifndef DWC_HOST_ONLY  // otg device mode. (gadget.)
6467 +    /*
6468 +     * Initialize the PCD
6469 +     */
6470 +    retval = dwc_otg_pcd_init(dwc_otg_device);
6471 +    if (retval != 0) {
6472 +        DWC_ERROR("dwc_otg_pcd_init failed\n");
6473 +        dwc_otg_device->pcd = NULL;
6474 +        goto fail;
6475 +    }
6476 +#endif // DWC_HOST_ONLY
6477 +
6478 +#ifndef DWC_DEVICE_ONLY // otg host mode. (HCD)
6479 +    /*
6480 +     * Initialize the HCD
6481 +     */
6482 +#if 1  /*fscz*/
6483 +       /* force_host_mode */
6484 +       usbcfg.d32 = dwc_read_reg32(&dwc_otg_device->core_if->core_global_regs ->gusbcfg);
6485 +       usbcfg.b.force_host_mode = 1;
6486 +       dwc_write_reg32(&dwc_otg_device->core_if->core_global_regs ->gusbcfg, usbcfg.d32);
6487 +#endif
6488 +    retval = dwc_otg_hcd_init(&_dev->dev, dwc_otg_device);
6489 +    if (retval != 0) {
6490 +        DWC_ERROR("dwc_otg_hcd_init failed\n");
6491 +        dwc_otg_device->hcd = NULL;
6492 +        goto fail;
6493 +    }
6494 +#endif // DWC_DEVICE_ONLY
6495 +
6496 +    /*
6497 +     * Enable the global interrupt after all the interrupt
6498 +     * handlers are installed.
6499 +     */
6500 +    dwc_otg_enable_global_interrupts( dwc_otg_device->core_if );
6501 +#if 0  /*fscz*/
6502 +       usbcfg.d32 = dwc_read_reg32(&dwc_otg_device->core_if->core_global_regs ->gusbcfg);
6503 +       usbcfg.b.force_host_mode = 0;
6504 +       dwc_write_reg32(&dwc_otg_device->core_if->core_global_regs ->gusbcfg, usbcfg.d32);
6505 +#endif
6506 +
6507 +
6508 +    return 0;
6509 +
6510 +fail:
6511 +    dwc_otg_driver_remove(_dev);
6512 +    return retval;
6513 +}
6514 +
6515 +/** 
6516 + * This structure defines the methods to be called by a bus driver
6517 + * during the lifecycle of a device on that bus. Both drivers and
6518 + * devices are registered with a bus driver. The bus driver matches
6519 + * devices to drivers based on information in the device and driver
6520 + * structures.
6521 + *
6522 + * The probe function is called when the bus driver matches a device
6523 + * to this driver. The remove function is called when a device is
6524 + * unregistered with the bus driver.
6525 + */
6526 +struct platform_driver dwc_otg_driver = {
6527 +       .probe  = dwc_otg_driver_probe,
6528 +       .remove = dwc_otg_driver_remove,
6529 +//     .suspend = dwc_otg_driver_suspend,
6530 +//     .resume = dwc_otg_driver_resume,
6531 +       .driver = {
6532 +               .name = dwc_driver_name,
6533 +               .owner = THIS_MODULE,
6534 +       },
6535 +};
6536 +EXPORT_SYMBOL(dwc_otg_driver);
6537 +
6538 +/**
6539 + * This function is called when the dwc_otg_driver is installed with the
6540 + * insmod command. It registers the dwc_otg_driver structure with the
6541 + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
6542 + * to be called. In addition, the bus driver will automatically expose
6543 + * attributes defined for the device and driver in the special sysfs file
6544 + * system.
6545 + *
6546 + * @return
6547 + */
6548 +static int __init dwc_otg_init(void) 
6549 +{
6550 +    int retval = 0;
6551 +
6552 +    printk(KERN_INFO "%s: version %s\n", dwc_driver_name, DWC_DRIVER_VERSION);
6553 +
6554 +       // ifxmips setup
6555 +    retval = ifx_usb_hc_init(dwc_iomem_base, dwc_irq);
6556 +    if (retval < 0)
6557 +    {
6558 +        printk(KERN_ERR "%s retval=%d\n", __func__, retval);
6559 +        return retval;
6560 +    }
6561 +    dwc_otg_power_on(); // ifx only!!
6562 +
6563 +
6564 +    retval = platform_driver_register(&dwc_otg_driver);
6565 +
6566 +    if (retval < 0) {
6567 +        printk(KERN_ERR "%s retval=%d\n", __func__, retval);
6568 +        goto error1;
6569 +    }
6570 +
6571 +    retval = driver_create_file(&dwc_otg_driver.driver, &driver_attr_version);
6572 +    if (retval < 0)
6573 +    {
6574 +        printk(KERN_ERR "%s retval=%d\n", __func__, retval);
6575 +        goto error2;
6576 +    }
6577 +    retval = driver_create_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
6578 +    if (retval < 0)
6579 +    {
6580 +        printk(KERN_ERR "%s retval=%d\n", __func__, retval);
6581 +        goto error3;
6582 +    }
6583 +    return retval;
6584 +
6585 +
6586 +error3:
6587 +    driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
6588 +error2:
6589 +    driver_unregister(&dwc_otg_driver.driver);
6590 +error1:
6591 +    ifx_usb_hc_remove();
6592 +    return retval;
6593 +}
6594 +module_init(dwc_otg_init);
6595 +
6596 +/** 
6597 + * This function is called when the driver is removed from the kernel
6598 + * with the rmmod command. The driver unregisters itself with its bus
6599 + * driver.
6600 + *
6601 + */
6602 +static void __exit dwc_otg_cleanup(void)
6603 +{
6604 +    printk(KERN_DEBUG "dwc_otg_cleanup()\n");
6605 +
6606 +    driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
6607 +    driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
6608 +
6609 +    platform_driver_unregister(&dwc_otg_driver);
6610 +    ifx_usb_hc_remove();
6611 +
6612 +    printk(KERN_INFO "%s module removed\n", dwc_driver_name);
6613 +}
6614 +module_exit(dwc_otg_cleanup);
6615 +
6616 +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
6617 +MODULE_AUTHOR("Synopsys Inc.");
6618 +MODULE_LICENSE("GPL");
6619 +
6620 +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
6621 +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
6622 +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
6623 +MODULE_PARM_DESC(opt, "OPT Mode");
6624 +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
6625 +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
6626 +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int, 0444);
6627 +MODULE_PARM_DESC(dma_burst_size, "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
6628 +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
6629 +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
6630 +module_param_named(host_support_fs_ls_low_power, dwc_otg_module_params.host_support_fs_ls_low_power, int, 0444);
6631 +MODULE_PARM_DESC(host_support_fs_ls_low_power, "Support Low Power w/FS or LS 0=Support 1=Don't Support");
6632 +module_param_named(host_ls_low_power_phy_clk, dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
6633 +MODULE_PARM_DESC(host_ls_low_power_phy_clk, "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
6634 +module_param_named(enable_dynamic_fifo, dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
6635 +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
6636 +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int, 0444);
6637 +MODULE_PARM_DESC(data_fifo_size, "Total number of words in the data FIFO memory 32-32768");
6638 +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size, int, 0444);
6639 +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
6640 +module_param_named(dev_nperio_tx_fifo_size, dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
6641 +MODULE_PARM_DESC(dev_nperio_tx_fifo_size, "Number of words in the non-periodic Tx FIFO 16-32768");
6642 +module_param_named(dev_perio_tx_fifo_size_1, dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
6643 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1, "Number of words in the periodic Tx FIFO 4-768");
6644 +module_param_named(dev_perio_tx_fifo_size_2, dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
6645 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2, "Number of words in the periodic Tx FIFO 4-768");
6646 +module_param_named(dev_perio_tx_fifo_size_3, dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
6647 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3, "Number of words in the periodic Tx FIFO 4-768");
6648 +module_param_named(dev_perio_tx_fifo_size_4, dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
6649 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4, "Number of words in the periodic Tx FIFO 4-768");
6650 +module_param_named(dev_perio_tx_fifo_size_5, dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
6651 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5, "Number of words in the periodic Tx FIFO 4-768");
6652 +module_param_named(dev_perio_tx_fifo_size_6, dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
6653 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6, "Number of words in the periodic Tx FIFO 4-768");
6654 +module_param_named(dev_perio_tx_fifo_size_7, dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
6655 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7, "Number of words in the periodic Tx FIFO 4-768");
6656 +module_param_named(dev_perio_tx_fifo_size_8, dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
6657 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8, "Number of words in the periodic Tx FIFO 4-768");
6658 +module_param_named(dev_perio_tx_fifo_size_9, dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
6659 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9, "Number of words in the periodic Tx FIFO 4-768");
6660 +module_param_named(dev_perio_tx_fifo_size_10, dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
6661 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10, "Number of words in the periodic Tx FIFO 4-768");
6662 +module_param_named(dev_perio_tx_fifo_size_11, dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
6663 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11, "Number of words in the periodic Tx FIFO 4-768");
6664 +module_param_named(dev_perio_tx_fifo_size_12, dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
6665 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12, "Number of words in the periodic Tx FIFO 4-768");
6666 +module_param_named(dev_perio_tx_fifo_size_13, dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
6667 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13, "Number of words in the periodic Tx FIFO 4-768");
6668 +module_param_named(dev_perio_tx_fifo_size_14, dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
6669 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14, "Number of words in the periodic Tx FIFO 4-768");
6670 +module_param_named(dev_perio_tx_fifo_size_15, dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
6671 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15, "Number of words in the periodic Tx FIFO 4-768");
6672 +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size, int, 0444);
6673 +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
6674 +module_param_named(host_nperio_tx_fifo_size, dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
6675 +MODULE_PARM_DESC(host_nperio_tx_fifo_size, "Number of words in the non-periodic Tx FIFO 16-32768");
6676 +module_param_named(host_perio_tx_fifo_size, dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
6677 +MODULE_PARM_DESC(host_perio_tx_fifo_size, "Number of words in the host periodic Tx FIFO 16-32768");
6678 +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size, int, 0444);
6679 +/** @todo Set the max to 512K, modify checks */
6680 +MODULE_PARM_DESC(max_transfer_size, "The maximum transfer size supported in bytes 2047-65535");
6681 +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count, int, 0444);
6682 +MODULE_PARM_DESC(max_packet_count, "The maximum number of packets in a transfer 15-511");
6683 +module_param_named(host_channels, dwc_otg_module_params.host_channels, int, 0444);
6684 +MODULE_PARM_DESC(host_channels, "The number of host channel registers to use 1-16");
6685 +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int, 0444);
6686 +MODULE_PARM_DESC(dev_endpoints, "The number of endpoints in addition to EP0 available for device mode 1-15");
6687 +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
6688 +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
6689 +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int, 0444);
6690 +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
6691 +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
6692 +MODULE_PARM_DESC(phy_ulpi_ddr, "ULPI at double or single data rate 0=Single 1=Double");
6693 +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus, int, 0444);
6694 +MODULE_PARM_DESC(phy_ulpi_ext_vbus, "ULPI PHY using internal or external vbus 0=Internal");
6695 +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
6696 +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
6697 +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
6698 +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
6699 +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
6700 +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
6701 +module_param_named(debug, g_dbg_lvl, int, 0444);
6702 +MODULE_PARM_DESC(debug, "0");
6703 +module_param_named(en_multiple_tx_fifo,
6704 +                    dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
6705 +MODULE_PARM_DESC(en_multiple_tx_fifo,
6706 +                 "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
6707 +module_param_named(dev_tx_fifo_size_1,
6708 +                   dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
6709 +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
6710 +module_param_named(dev_tx_fifo_size_2,
6711 +                   dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
6712 +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
6713 +module_param_named(dev_tx_fifo_size_3,
6714 +                   dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
6715 +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
6716 +module_param_named(dev_tx_fifo_size_4,
6717 +                   dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
6718 +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
6719 +module_param_named(dev_tx_fifo_size_5,
6720 +                   dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
6721 +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
6722 +module_param_named(dev_tx_fifo_size_6,
6723 +                   dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
6724 +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
6725 +module_param_named(dev_tx_fifo_size_7,
6726 +                   dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
6727 +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
6728 +module_param_named(dev_tx_fifo_size_8,
6729 +                   dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
6730 +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
6731 +module_param_named(dev_tx_fifo_size_9,
6732 +                   dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
6733 +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
6734 +module_param_named(dev_tx_fifo_size_10,
6735 +                   dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
6736 +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
6737 +module_param_named(dev_tx_fifo_size_11,
6738 +                   dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
6739 +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
6740 +module_param_named(dev_tx_fifo_size_12,
6741 +                   dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
6742 +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
6743 +module_param_named(dev_tx_fifo_size_13,
6744 +                   dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
6745 +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
6746 +module_param_named(dev_tx_fifo_size_14,
6747 +                   dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
6748 +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
6749 +module_param_named(dev_tx_fifo_size_15,
6750 +                   dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
6751 +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
6752 +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
6753 +MODULE_PARM_DESC(thr_ctl, "Thresholding enable flag bit"
6754 +               "0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
6755 +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int, 0444);
6756 +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
6757 +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int, 0444);
6758 +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
6759 +module_param_named (iomem_base, dwc_iomem_base, ulong, 0444);
6760 +MODULE_PARM_DESC (dwc_iomem_base, "The base address of the DWC_OTG register.");
6761 +module_param_named (irq, dwc_irq, int, 0444);
6762 +MODULE_PARM_DESC (dwc_irq, "The interrupt number");
6763 +
6764 +/** @page "Module Parameters"
6765 + *
6766 + * The following parameters may be specified when starting the module.
6767 + * These parameters define how the DWC_otg controller should be
6768 + * configured.  Parameter values are passed to the CIL initialization
6769 + * function dwc_otg_cil_init
6770 + *
6771 + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
6772 + *
6773
6774 + <table>
6775 + <tr><td>Parameter Name</td><td>Meaning</td></tr> 
6776
6777 + <tr>
6778 + <td>otg_cap</td>
6779 + <td>Specifies the OTG capabilities. The driver will automatically detect the
6780 + value for this parameter if none is specified.
6781 + - 0: HNP and SRP capable (default, if available)
6782 + - 1: SRP Only capable
6783 + - 2: No HNP/SRP capable
6784 + </td></tr>
6785
6786 + <tr>
6787 + <td>dma_enable</td>
6788 + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
6789 + The driver will automatically detect the value for this parameter if none is
6790 + specified.
6791 + - 0: Slave
6792 + - 1: DMA (default, if available)
6793 + </td></tr>
6794
6795 + <tr>
6796 + <td>dma_burst_size</td>
6797 + <td>The DMA Burst size (applicable only for External DMA Mode).
6798 + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
6799 + </td></tr>
6800
6801 + <tr>
6802 + <td>speed</td>
6803 + <td>Specifies the maximum speed of operation in host and device mode. The
6804 + actual speed depends on the speed of the attached device and the value of
6805 + phy_type.
6806 + - 0: High Speed (default)
6807 + - 1: Full Speed
6808 + </td></tr>
6809
6810 + <tr>
6811 + <td>host_support_fs_ls_low_power</td>
6812 + <td>Specifies whether low power mode is supported when attached to a Full
6813 + Speed or Low Speed device in host mode.
6814 + - 0: Don't support low power mode (default)
6815 + - 1: Support low power mode
6816 + </td></tr>
6817
6818 + <tr>
6819 + <td>host_ls_low_power_phy_clk</td>
6820 + <td>Specifies the PHY clock rate in low power mode when connected to a Low
6821 + Speed device in host mode. This parameter is applicable only if
6822 + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
6823 + - 0: 48 MHz (default)
6824 + - 1: 6 MHz
6825 + </td></tr>
6826
6827 + <tr>
6828 + <td>enable_dynamic_fifo</td>
6829 + <td> Specifies whether FIFOs may be resized by the driver software.
6830 + - 0: Use cC FIFO size parameters
6831 + - 1: Allow dynamic FIFO sizing (default)
6832 + </td></tr>
6833
6834 + <tr>
6835 + <td>data_fifo_size</td>
6836 + <td>Total number of 4-byte words in the data FIFO memory. This memory
6837 + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
6838 + - Values: 32 to 32768 (default 8192)
6839 +
6840 + Note: The total FIFO memory depth in the FPGA configuration is 8192.
6841 + </td></tr>
6842
6843 + <tr>
6844 + <td>dev_rx_fifo_size</td>
6845 + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
6846 + FIFO sizing is enabled.
6847 + - Values: 16 to 32768 (default 1064)
6848 + </td></tr>
6849
6850 + <tr>
6851 + <td>dev_nperio_tx_fifo_size</td>
6852 + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
6853 + dynamic FIFO sizing is enabled.
6854 + - Values: 16 to 32768 (default 1024)
6855 + </td></tr>
6856
6857 + <tr>
6858 + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
6859 + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
6860 + when dynamic FIFO sizing is enabled.
6861 + - Values: 4 to 768 (default 256)
6862 + </td></tr>
6863
6864 + <tr>
6865 + <td>host_rx_fifo_size</td>
6866 + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
6867 + sizing is enabled.
6868 + - Values: 16 to 32768 (default 1024)
6869 + </td></tr>
6870
6871 + <tr>
6872 + <td>host_nperio_tx_fifo_size</td>
6873 + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
6874 + dynamic FIFO sizing is enabled in the core.
6875 + - Values: 16 to 32768 (default 1024)
6876 + </td></tr>
6877
6878 + <tr>
6879 + <td>host_perio_tx_fifo_size</td>
6880 + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
6881 + sizing is enabled.
6882 + - Values: 16 to 32768 (default 1024)
6883 + </td></tr>
6884
6885 + <tr>
6886 + <td>max_transfer_size</td>
6887 + <td>The maximum transfer size supported in bytes.
6888 + - Values: 2047 to 65,535 (default 65,535)
6889 + </td></tr>
6890
6891 + <tr>
6892 + <td>max_packet_count</td>
6893 + <td>The maximum number of packets in a transfer.
6894 + - Values: 15 to 511 (default 511)
6895 + </td></tr>
6896
6897 + <tr>
6898 + <td>host_channels</td>
6899 + <td>The number of host channel registers to use.
6900 + - Values: 1 to 16 (default 12)
6901 +
6902 + Note: The FPGA configuration supports a maximum of 12 host channels.
6903 + </td></tr>
6904
6905 + <tr>
6906 + <td>dev_endpoints</td>
6907 + <td>The number of endpoints in addition to EP0 available for device mode
6908 + operations.
6909 + - Values: 1 to 15 (default 6 IN and OUT)
6910 +
6911 + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
6912 + addition to EP0.
6913 + </td></tr>
6914
6915 + <tr>
6916 + <td>phy_type</td>
6917 + <td>Specifies the type of PHY interface to use. By default, the driver will
6918 + automatically detect the phy_type.
6919 + - 0: Full Speed
6920 + - 1: UTMI+ (default, if available)
6921 + - 2: ULPI
6922 + </td></tr>
6923
6924 + <tr>
6925 + <td>phy_utmi_width</td>
6926 + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
6927 + phy_type of UTMI+. Also, this parameter is applicable only if the
6928 + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
6929 + core has been configured to work at either data path width.
6930 + - Values: 8 or 16 bits (default 16)
6931 + </td></tr>
6932
6933 + <tr>
6934 + <td>phy_ulpi_ddr</td>
6935 + <td>Specifies whether the ULPI operates at double or single data rate. This
6936 + parameter is only applicable if phy_type is ULPI.
6937 + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
6938 + - 1: double data rate ULPI interface with 4 bit wide data bus
6939 + </td></tr>
6940 +
6941 + <tr>
6942 + <td>i2c_enable</td>
6943 + <td>Specifies whether to use the I2C interface for full speed PHY. This
6944 + parameter is only applicable if PHY_TYPE is FS.
6945 + - 0: Disabled (default)
6946 + - 1: Enabled
6947 + </td></tr>
6948 +
6949 + <tr>
6950 + <td>otg_en_multiple_tx_fifo</td>
6951 + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
6952 + The driver will automatically detect the value for this parameter if none is
6953 + specified.
6954 + - 0: Disabled
6955 + - 1: Enabled (default, if available)
6956 + </td></tr>
6957 +
6958 + <tr>
6959 + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
6960 + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
6961 + when dynamic FIFO sizing is enabled.
6962 + - Values: 4 to 768 (default 256)
6963 + </td></tr>
6964 +
6965 +*/
6966 --- /dev/null
6967 +++ b/drivers/usb/dwc_otg/dwc_otg_driver.h
6968 @@ -0,0 +1,84 @@
6969 +/* ==========================================================================
6970 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_driver.h $
6971 + * $Revision: 1.1.1.1 $
6972 + * $Date: 2009-04-17 06:15:34 $
6973 + * $Change: 510275 $
6974 + *
6975 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
6976 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
6977 + * otherwise expressly agreed to in writing between Synopsys and you.
6978 + * 
6979 + * The Software IS NOT an item of Licensed Software or Licensed Product under
6980 + * any End User Software License Agreement or Agreement for Licensed Product
6981 + * with Synopsys or any supplement thereto. You are permitted to use and
6982 + * redistribute this Software in source and binary forms, with or without
6983 + * modification, provided that redistributions of source code must retain this
6984 + * notice. You may not view, use, disclose, copy or distribute this file or
6985 + * any information contained herein except pursuant to this license grant from
6986 + * Synopsys. If you do not agree with this notice, including the disclaimer
6987 + * below, then you are not authorized to use the Software.
6988 + * 
6989 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
6990 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6991 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6992 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
6993 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6994 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6995 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
6996 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6997 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6998 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
6999 + * DAMAGE.
7000 + * ========================================================================== */
7001 +
7002 +#if !defined(__DWC_OTG_DRIVER_H__)
7003 +#define __DWC_OTG_DRIVER_H__
7004 +
7005 +/** @file
7006 + * This file contains the interface to the Linux driver.
7007 + */
7008 +#include "dwc_otg_cil.h"
7009 +
7010 +/* Type declarations */
7011 +struct dwc_otg_pcd;
7012 +struct dwc_otg_hcd;
7013 +
7014 +/**
7015 + * This structure is a wrapper that encapsulates the driver components used to
7016 + * manage a single DWC_otg controller.
7017 + */
7018 +typedef struct dwc_otg_device
7019 +{
7020 +    /** Base address returned from ioremap() */
7021 +    void *base;
7022 +    
7023 +    /** Pointer to the core interface structure. */
7024 +    dwc_otg_core_if_t *core_if;
7025 +
7026 +    /** Register offset for Diagnostic API.*/
7027 +    uint32_t reg_offset;
7028 +
7029 +    /** Pointer to the PCD structure. */
7030 +    struct dwc_otg_pcd *pcd;
7031 +
7032 +    /** Pointer to the HCD structure. */
7033 +    struct dwc_otg_hcd *hcd;
7034 +
7035 +    /** Flag to indicate whether the common IRQ handler is installed. */
7036 +    uint8_t common_irq_installed;
7037 +
7038 +    /** Interrupt request number. */
7039 +       unsigned int irq;
7040 +
7041 +    /** Physical address of Control and Status registers, used by
7042 +     *  release_mem_region().
7043 +     */
7044 +       resource_size_t phys_addr;
7045 +
7046 +    /** Length of memory region, used by release_mem_region(). */
7047 +       unsigned long base_len;
7048 +} dwc_otg_device_t;
7049 +
7050 +//#define dev_dbg(fake, format, arg...) printk(KERN_CRIT __FILE__ ":%d: " format "\n" , __LINE__, ## arg)
7051 +
7052 +#endif
7053 --- /dev/null
7054 +++ b/drivers/usb/dwc_otg/dwc_otg_hcd.c
7055 @@ -0,0 +1,2870 @@
7056 +/* ==========================================================================
7057 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_hcd.c $
7058 + * $Revision: 1.1.1.1 $
7059 + * $Date: 2009-04-17 06:15:34 $
7060 + * $Change: 631780 $
7061 + *
7062 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
7063 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
7064 + * otherwise expressly agreed to in writing between Synopsys and you.
7065 + * 
7066 + * The Software IS NOT an item of Licensed Software or Licensed Product under
7067 + * any End User Software License Agreement or Agreement for Licensed Product
7068 + * with Synopsys or any supplement thereto. You are permitted to use and
7069 + * redistribute this Software in source and binary forms, with or without
7070 + * modification, provided that redistributions of source code must retain this
7071 + * notice. You may not view, use, disclose, copy or distribute this file or
7072 + * any information contained herein except pursuant to this license grant from
7073 + * Synopsys. If you do not agree with this notice, including the disclaimer
7074 + * below, then you are not authorized to use the Software.
7075 + * 
7076 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
7077 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7078 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7079 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
7080 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7081 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
7082 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
7083 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
7084 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
7085 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
7086 + * DAMAGE.
7087 + * ========================================================================== */
7088 +#ifndef DWC_DEVICE_ONLY
7089 +
7090 +/**
7091 + * @file
7092 + *
7093 + * This file contains the implementation of the HCD. In Linux, the HCD
7094 + * implements the hc_driver API.
7095 + */
7096 +#include <linux/kernel.h>
7097 +#include <linux/module.h>
7098 +#include <linux/moduleparam.h>
7099 +#include <linux/init.h>
7100 +
7101 +#include <linux/device.h>
7102 +
7103 +#include <linux/errno.h>
7104 +#include <linux/list.h>
7105 +#include <linux/interrupt.h>
7106 +#include <linux/string.h>
7107 +
7108 +#include <linux/dma-mapping.h>
7109 +
7110 +#include "dwc_otg_driver.h"
7111 +#include "dwc_otg_hcd.h"
7112 +#include "dwc_otg_regs.h"
7113 +
7114 +#include <asm/irq.h>
7115 +#include "dwc_otg_ifx.h" // for Infineon platform specific.
7116 +extern atomic_t release_later;
7117 +
7118 +static u64 dma_mask = DMA_BIT_MASK(32);
7119 +
7120 +static const char dwc_otg_hcd_name [] = "dwc_otg_hcd";
7121 +static const struct hc_driver dwc_otg_hc_driver = 
7122 +{
7123 +       .description =          dwc_otg_hcd_name,
7124 +       .product_desc =         "DWC OTG Controller",
7125 +       .hcd_priv_size =        sizeof(dwc_otg_hcd_t),
7126 +       .irq =                  dwc_otg_hcd_irq,
7127 +       .flags =                HCD_MEMORY | HCD_USB2,
7128 +       //.reset =
7129 +       .start =                dwc_otg_hcd_start,
7130 +       //.suspend =            
7131 +       //.resume =             
7132 +       .stop =                 dwc_otg_hcd_stop,
7133 +       .urb_enqueue =          dwc_otg_hcd_urb_enqueue,
7134 +       .urb_dequeue =          dwc_otg_hcd_urb_dequeue,
7135 +       .endpoint_disable =     dwc_otg_hcd_endpoint_disable,
7136 +       .get_frame_number =     dwc_otg_hcd_get_frame_number,
7137 +       .hub_status_data =      dwc_otg_hcd_hub_status_data,
7138 +       .hub_control =          dwc_otg_hcd_hub_control,
7139 +       //.hub_suspend =        
7140 +       //.hub_resume =         
7141 +};
7142 +
7143 +
7144 +/**
7145 + * Work queue function for starting the HCD when A-Cable is connected.
7146 + * The dwc_otg_hcd_start() must be called in a process context.
7147 + */
7148 +static void hcd_start_func(struct work_struct *work)
7149 +{
7150 +       struct dwc_otg_hcd *priv =
7151 +               container_of(work, struct dwc_otg_hcd, start_work);
7152 +       struct usb_hcd *usb_hcd = (struct usb_hcd *)priv->_p;
7153 +       DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, usb_hcd);
7154 +       if (usb_hcd) {
7155 +               dwc_otg_hcd_start(usb_hcd);
7156 +       }
7157 +}
7158 +
7159 +
7160 +/**
7161 + * HCD Callback function for starting the HCD when A-Cable is
7162 + * connected.
7163 + *
7164 + * @param _p void pointer to the <code>struct usb_hcd</code>
7165 + */
7166 +static int32_t dwc_otg_hcd_start_cb(void *_p)
7167 +{
7168 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(_p);
7169 +       dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
7170 +       hprt0_data_t hprt0;
7171 +       if (core_if->op_state == B_HOST) {
7172 +               /* 
7173 +                * Reset the port.  During a HNP mode switch the reset
7174 +                * needs to occur within 1ms and have a duration of at
7175 +                * least 50ms. 
7176 +                */
7177 +               hprt0.d32 = dwc_otg_read_hprt0 (core_if);
7178 +               hprt0.b.prtrst = 1;
7179 +               dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
7180 +               ((struct usb_hcd *)_p)->self.is_b_host = 1;
7181 +       } else {
7182 +               ((struct usb_hcd *)_p)->self.is_b_host = 0;
7183 +       }
7184 +       /* Need to start the HCD in a non-interrupt context. */
7185 +       INIT_WORK(&dwc_otg_hcd->start_work, hcd_start_func);
7186 +       dwc_otg_hcd->_p = _p;
7187 +       schedule_work(&dwc_otg_hcd->start_work);
7188 +       return 1;
7189 +}
7190 +
7191 +
7192 +/**
7193 + * HCD Callback function for stopping the HCD.
7194 + *
7195 + * @param _p void pointer to the <code>struct usb_hcd</code>
7196 + */
7197 +static int32_t dwc_otg_hcd_stop_cb( void *_p )
7198 +{
7199 +       struct usb_hcd *usb_hcd = (struct usb_hcd *)_p;
7200 +       DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, _p);
7201 +       dwc_otg_hcd_stop( usb_hcd );
7202 +       return 1;
7203 +}
7204 +static void del_xfer_timers(dwc_otg_hcd_t *_hcd)
7205 +{
7206 +#ifdef DEBUG
7207 +       int i;
7208 +       int num_channels = _hcd->core_if->core_params->host_channels;
7209 +       for (i = 0; i < num_channels; i++) {
7210 +               del_timer(&_hcd->core_if->hc_xfer_timer[i]);
7211 +       }
7212 +#endif /*  */
7213 +}
7214 +
7215 +static void del_timers(dwc_otg_hcd_t *_hcd)
7216 +{
7217 +       del_xfer_timers(_hcd);
7218 +       del_timer(&_hcd->conn_timer);
7219 +}
7220 +
7221 +/**
7222 + * Processes all the URBs in a single list of QHs. Completes them with
7223 + * -ETIMEDOUT and frees the QTD.
7224 + */
7225 +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * _hcd,
7226 +               struct list_head *_qh_list)
7227 +{
7228 +       struct list_head        *qh_item;
7229 +       dwc_otg_qh_t            *qh;
7230 +       struct list_head        *qtd_item;
7231 +       dwc_otg_qtd_t           *qtd;
7232 +
7233 +       list_for_each(qh_item, _qh_list) {
7234 +               qh = list_entry(qh_item, dwc_otg_qh_t, qh_list_entry);
7235 +               for (qtd_item = qh->qtd_list.next; qtd_item != &qh->qtd_list;
7236 +                               qtd_item = qh->qtd_list.next) {
7237 +                       qtd = list_entry(qtd_item, dwc_otg_qtd_t, qtd_list_entry);
7238 +                       if (qtd->urb != NULL) {
7239 +                               dwc_otg_hcd_complete_urb(_hcd, qtd->urb,-ETIMEDOUT);
7240 +                       }
7241 +                       dwc_otg_hcd_qtd_remove_and_free(qtd);
7242 +               }
7243 +       }
7244 +}
7245 +
7246 +/**
7247 + * Responds with an error status of ETIMEDOUT to all URBs in the non-periodic
7248 + * and periodic schedules. The QTD associated with each URB is removed from
7249 + * the schedule and freed. This function may be called when a disconnect is
7250 + * detected or when the HCD is being stopped.
7251 + */
7252 +static void kill_all_urbs(dwc_otg_hcd_t *_hcd)
7253 +{
7254 +       kill_urbs_in_qh_list(_hcd, &_hcd->non_periodic_sched_deferred);
7255 +       kill_urbs_in_qh_list(_hcd, &_hcd->non_periodic_sched_inactive);
7256 +       kill_urbs_in_qh_list(_hcd, &_hcd->non_periodic_sched_active);
7257 +       kill_urbs_in_qh_list(_hcd, &_hcd->periodic_sched_inactive);
7258 +       kill_urbs_in_qh_list(_hcd, &_hcd->periodic_sched_ready);
7259 +       kill_urbs_in_qh_list(_hcd, &_hcd->periodic_sched_assigned);
7260 +       kill_urbs_in_qh_list(_hcd, &_hcd->periodic_sched_queued);
7261 +}
7262 +
7263 +/**
7264 + * HCD Callback function for disconnect of the HCD.
7265 + *
7266 + * @param _p void pointer to the <code>struct usb_hcd</code>
7267 + */
7268 +static int32_t dwc_otg_hcd_disconnect_cb( void *_p )
7269 +{
7270 +       gintsts_data_t  intr;
7271 +       dwc_otg_hcd_t   *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_p);
7272 +
7273 +       DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, _p);
7274 +
7275 +       /* 
7276 +        * Set status flags for the hub driver.
7277 +        */
7278 +       dwc_otg_hcd->flags.b.port_connect_status_change = 1;
7279 +       dwc_otg_hcd->flags.b.port_connect_status = 0;
7280 +
7281 +       /*
7282 +        * Shutdown any transfers in process by clearing the Tx FIFO Empty
7283 +        * interrupt mask and status bits and disabling subsequent host
7284 +        * channel interrupts.
7285 +        */
7286 +       intr.d32 = 0;
7287 +       intr.b.nptxfempty = 1;
7288 +       intr.b.ptxfempty = 1;
7289 +       intr.b.hcintr = 1;
7290 +       dwc_modify_reg32 (&dwc_otg_hcd->core_if->core_global_regs->gintmsk, intr.d32, 0);
7291 +       dwc_modify_reg32 (&dwc_otg_hcd->core_if->core_global_regs->gintsts, intr.d32, 0);
7292 +
7293 +       del_timers(dwc_otg_hcd);
7294 +
7295 +       /*
7296 +        * Turn off the vbus power only if the core has transitioned to device
7297 +        * mode. If still in host mode, need to keep power on to detect a
7298 +        * reconnection.
7299 +        */
7300 +       if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
7301 +               if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {        
7302 +                       hprt0_data_t hprt0 = { .d32=0 };
7303 +                       DWC_PRINT("Disconnect: PortPower off\n");
7304 +                       hprt0.b.prtpwr = 0;
7305 +                       dwc_write_reg32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0.d32);
7306 +               }
7307 +
7308 +               dwc_otg_disable_host_interrupts( dwc_otg_hcd->core_if );
7309 +       }
7310 +
7311 +       /* Respond with an error status to all URBs in the schedule. */
7312 +       kill_all_urbs(dwc_otg_hcd);
7313 +
7314 +       if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
7315 +               /* Clean up any host channels that were in use. */
7316 +               int                     num_channels;
7317 +               int                     i;
7318 +               dwc_hc_t                *channel;
7319 +               dwc_otg_hc_regs_t       *hc_regs;
7320 +               hcchar_data_t           hcchar;
7321 +
7322 +               num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
7323 +
7324 +               if (!dwc_otg_hcd->core_if->dma_enable) {
7325 +                       /* Flush out any channel requests in slave mode. */
7326 +                       for (i = 0; i < num_channels; i++) {
7327 +                               channel = dwc_otg_hcd->hc_ptr_array[i];
7328 +                               if (list_empty(&channel->hc_list_entry)) {
7329 +                                       hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[i];
7330 +                                       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
7331 +                                       if (hcchar.b.chen) {
7332 +                                               hcchar.b.chen = 0;
7333 +                                               hcchar.b.chdis = 1;
7334 +                                               hcchar.b.epdir = 0;
7335 +                                               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
7336 +                                       }
7337 +                               }
7338 +                       }
7339 +               }
7340 +
7341 +               for (i = 0; i < num_channels; i++) {
7342 +                       channel = dwc_otg_hcd->hc_ptr_array[i];
7343 +                       if (list_empty(&channel->hc_list_entry)) {
7344 +                               hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[i];
7345 +                               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
7346 +                               if (hcchar.b.chen) {
7347 +                                       /* Halt the channel. */
7348 +                                       hcchar.b.chdis = 1;
7349 +                                       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
7350 +                               }
7351 +
7352 +                               dwc_otg_hc_cleanup(dwc_otg_hcd->core_if, channel);
7353 +                               list_add_tail(&channel->hc_list_entry,
7354 +                                               &dwc_otg_hcd->free_hc_list);
7355 +                       }
7356 +               }
7357 +       }
7358 +
7359 +       /* A disconnect will end the session so the B-Device is no
7360 +        * longer a B-host. */
7361 +       ((struct usb_hcd *)_p)->self.is_b_host = 0;
7362 +
7363 +       return 1;
7364 +}
7365 +
7366 +/**
7367 + * Connection timeout function.  An OTG host is required to display a
7368 + * message if the device does not connect within 10 seconds.
7369 + */
7370 +void dwc_otg_hcd_connect_timeout( unsigned long _ptr )
7371 +{
7372 +       DWC_DEBUGPL(DBG_HCDV, "%s(%x)\n", __func__, (int)_ptr);
7373 +       DWC_PRINT( "Connect Timeout\n");
7374 +       DWC_ERROR( "Device Not Connected/Responding\n" );
7375 +}
7376 +
7377 +/**
7378 + * Start the connection timer.  An OTG host is required to display a
7379 + * message if the device does not connect within 10 seconds.  The
7380 + * timer is deleted if a port connect interrupt occurs before the
7381 + * timer expires.
7382 + */
7383 +static void dwc_otg_hcd_start_connect_timer( dwc_otg_hcd_t *_hcd)
7384 +{
7385 +       init_timer( &_hcd->conn_timer );
7386 +       _hcd->conn_timer.function = dwc_otg_hcd_connect_timeout;
7387 +       _hcd->conn_timer.data = (unsigned long)0;
7388 +       _hcd->conn_timer.expires = jiffies + (HZ*10);
7389 +       add_timer( &_hcd->conn_timer );
7390 +}
7391 +
7392 +/**
7393 + * HCD Callback function for disconnect of the HCD.
7394 + *
7395 + * @param _p void pointer to the <code>struct usb_hcd</code>
7396 + */
7397 +static int32_t dwc_otg_hcd_session_start_cb( void *_p )
7398 +{
7399 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_p);
7400 +       DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, _p);
7401 +       dwc_otg_hcd_start_connect_timer( dwc_otg_hcd );
7402 +       return 1;
7403 +}
7404 +
7405 +/**
7406 + * HCD Callback structure for handling mode switching.
7407 + */
7408 +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
7409 +       .start = dwc_otg_hcd_start_cb,
7410 +       .stop = dwc_otg_hcd_stop_cb,
7411 +       .disconnect = dwc_otg_hcd_disconnect_cb,
7412 +       .session_start = dwc_otg_hcd_session_start_cb,
7413 +       .p = 0,
7414 +};
7415 +
7416 +
7417 +/**
7418 + * Reset tasklet function
7419 + */
7420 +static void reset_tasklet_func (unsigned long data)
7421 +{
7422 +       dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t*)data;
7423 +       dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
7424 +       hprt0_data_t hprt0;
7425 +
7426 +       DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
7427 +
7428 +       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
7429 +       hprt0.b.prtrst = 1;
7430 +       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
7431 +       mdelay (60);
7432 +
7433 +       hprt0.b.prtrst = 0;
7434 +       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
7435 +       dwc_otg_hcd->flags.b.port_reset_change = 1;     
7436 +
7437 +       return;
7438 +}
7439 +
7440 +static struct tasklet_struct reset_tasklet = { 
7441 +       .next = NULL,
7442 +       .state = 0,
7443 +       .count = ATOMIC_INIT(0),
7444 +       .func = reset_tasklet_func,
7445 +       .data = 0,
7446 +};
7447 +
7448 +/**
7449 + * Initializes the HCD. This function allocates memory for and initializes the
7450 + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
7451 + * USB bus with the core and calls the hc_driver->start() function. It returns
7452 + * a negative error on failure.
7453 + */
7454 +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
7455 +
7456 +int  __devinit  dwc_otg_hcd_init(struct device *_dev, dwc_otg_device_t * dwc_otg_device)
7457 +{
7458 +       struct usb_hcd *hcd = NULL;
7459 +       dwc_otg_hcd_t *dwc_otg_hcd = NULL;
7460 +       dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
7461 +
7462 +       int             num_channels;
7463 +       int             i;
7464 +       dwc_hc_t        *channel;
7465 +
7466 +       int retval = 0;
7467 +
7468 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
7469 +
7470 +       /*
7471 +        * Allocate memory for the base HCD plus the DWC OTG HCD.
7472 +        * Initialize the base HCD.
7473 +        */
7474 +       hcd = usb_create_hcd(&dwc_otg_hc_driver, _dev, dev_name(_dev));
7475 +       if (hcd == NULL) {
7476 +               retval = -ENOMEM;
7477 +               goto error1;
7478 +       }
7479 +       dev_set_drvdata(_dev, dwc_otg_device); /* fscz restore */
7480 +       hcd->regs = otg_dev->base;
7481 +       hcd->rsrc_start = (int)otg_dev->base;
7482 +
7483 +       hcd->self.otg_port = 1;  
7484 +
7485 +       /* Initialize the DWC OTG HCD. */
7486 +       dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
7487 +       dwc_otg_hcd->core_if = otg_dev->core_if;
7488 +       otg_dev->hcd = dwc_otg_hcd;
7489 +
7490 +       /* Register the HCD CIL Callbacks */
7491 +       dwc_otg_cil_register_hcd_callbacks(otg_dev->core_if, 
7492 +                       &hcd_cil_callbacks, hcd);
7493 +
7494 +       /* Initialize the non-periodic schedule. */
7495 +       INIT_LIST_HEAD(&dwc_otg_hcd->non_periodic_sched_inactive);
7496 +       INIT_LIST_HEAD(&dwc_otg_hcd->non_periodic_sched_active);
7497 +       INIT_LIST_HEAD(&dwc_otg_hcd->non_periodic_sched_deferred);
7498 +
7499 +       /* Initialize the periodic schedule. */
7500 +       INIT_LIST_HEAD(&dwc_otg_hcd->periodic_sched_inactive);
7501 +       INIT_LIST_HEAD(&dwc_otg_hcd->periodic_sched_ready);
7502 +       INIT_LIST_HEAD(&dwc_otg_hcd->periodic_sched_assigned);
7503 +       INIT_LIST_HEAD(&dwc_otg_hcd->periodic_sched_queued);
7504 +
7505 +       /*
7506 +        * Create a host channel descriptor for each host channel implemented
7507 +        * in the controller. Initialize the channel descriptor array.
7508 +        */
7509 +       INIT_LIST_HEAD(&dwc_otg_hcd->free_hc_list);
7510 +       num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
7511 +       for (i = 0; i < num_channels; i++) {
7512 +               channel = kmalloc(sizeof(dwc_hc_t), GFP_KERNEL);
7513 +               if (channel == NULL) {
7514 +                       retval = -ENOMEM;
7515 +                       DWC_ERROR("%s: host channel allocation failed\n", __func__);
7516 +                       goto error2;
7517 +               }
7518 +               memset(channel, 0, sizeof(dwc_hc_t));
7519 +               channel->hc_num = i;
7520 +               dwc_otg_hcd->hc_ptr_array[i] = channel;
7521 +#ifdef DEBUG
7522 +               init_timer(&dwc_otg_hcd->core_if->hc_xfer_timer[i]);
7523 +#endif         
7524 +
7525 +               DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i, channel);
7526 +       }
7527 +
7528 +       /* Initialize the Connection timeout timer. */
7529 +       init_timer( &dwc_otg_hcd->conn_timer );
7530 +
7531 +       /* Initialize reset tasklet. */
7532 +       reset_tasklet.data = (unsigned long) dwc_otg_hcd;
7533 +       dwc_otg_hcd->reset_tasklet = &reset_tasklet;
7534 +
7535 +       /* Set device flags indicating whether the HCD supports DMA. */
7536 +       if (otg_dev->core_if->dma_enable) {
7537 +               DWC_PRINT("Using DMA mode\n");
7538 +               //_dev->dma_mask = (void *)~0;
7539 +               //_dev->coherent_dma_mask = ~0;
7540 +               _dev->dma_mask = &dma_mask;
7541 +               _dev->coherent_dma_mask = DMA_BIT_MASK(32);
7542 +       } else {
7543 +               DWC_PRINT("Using Slave mode\n");
7544 +               _dev->dma_mask = (void *)0;
7545 +               _dev->coherent_dma_mask = 0;
7546 +       }
7547 +
7548 +       init_hcd_usecs(dwc_otg_hcd);
7549 +       /*
7550 +        * Finish generic HCD initialization and start the HCD. This function
7551 +        * allocates the DMA buffer pool, registers the USB bus, requests the
7552 +        * IRQ line, and calls dwc_otg_hcd_start method.
7553 +        */
7554 +       retval = usb_add_hcd(hcd, otg_dev->irq, IRQF_SHARED);
7555 +       if (retval < 0) {
7556 +               goto error2;
7557 +       }
7558 +
7559 +       /*
7560 +        * Allocate space for storing data on status transactions. Normally no
7561 +        * data is sent, but this space acts as a bit bucket. This must be
7562 +        * done after usb_add_hcd since that function allocates the DMA buffer
7563 +        * pool.
7564 +        */
7565 +       if (otg_dev->core_if->dma_enable) {
7566 +               dwc_otg_hcd->status_buf =
7567 +                       dma_alloc_coherent(_dev,
7568 +                                       DWC_OTG_HCD_STATUS_BUF_SIZE,
7569 +                                       &dwc_otg_hcd->status_buf_dma,
7570 +                                       GFP_KERNEL | GFP_DMA);
7571 +       } else {
7572 +               dwc_otg_hcd->status_buf = kmalloc(DWC_OTG_HCD_STATUS_BUF_SIZE,
7573 +                               GFP_KERNEL);
7574 +       }
7575 +       if (dwc_otg_hcd->status_buf == NULL) {
7576 +               retval = -ENOMEM;
7577 +               DWC_ERROR("%s: status_buf allocation failed\n", __func__);
7578 +               goto error3;
7579 +       }
7580 +
7581 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Initialized HCD, bus=%s, usbbus=%d\n", 
7582 +                       dev_name(_dev), hcd->self.busnum);
7583 +
7584 +       return 0;
7585 +
7586 +       /* Error conditions */
7587 +error3:
7588 +       usb_remove_hcd(hcd);
7589 +error2:
7590 +       dwc_otg_hcd_free(hcd);
7591 +       usb_put_hcd(hcd);
7592 +error1:
7593 +       return retval;
7594 +}
7595 +
7596 +/**
7597 + * Removes the HCD.
7598 + * Frees memory and resources associated with the HCD and deregisters the bus.
7599 + */
7600 +void dwc_otg_hcd_remove(struct device *_dev)
7601 +{
7602 +       dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev);
7603 +       dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
7604 +       struct usb_hcd *hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
7605 +
7606 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE\n");
7607 +
7608 +       /* Turn off all interrupts */
7609 +       dwc_write_reg32 (&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0);
7610 +       dwc_modify_reg32 (&dwc_otg_hcd->core_if->core_global_regs->gahbcfg, 1, 0);
7611 +
7612 +       usb_remove_hcd(hcd);
7613 +
7614 +       dwc_otg_hcd_free(hcd);
7615 +
7616 +       usb_put_hcd(hcd);
7617 +
7618 +       return;
7619 +}
7620 +
7621 +
7622 +/* =========================================================================
7623 + *  Linux HC Driver Functions
7624 + * ========================================================================= */
7625 +
7626 +/**
7627 + * Initializes dynamic portions of the DWC_otg HCD state.
7628 + */
7629 +static void hcd_reinit(dwc_otg_hcd_t *_hcd)
7630 +{
7631 +       struct list_head        *item;
7632 +       int                     num_channels;
7633 +       int                     i;
7634 +       dwc_hc_t                *channel;
7635 +
7636 +       _hcd->flags.d32 = 0;
7637 +
7638 +       _hcd->non_periodic_qh_ptr = &_hcd->non_periodic_sched_active;
7639 +       _hcd->available_host_channels = _hcd->core_if->core_params->host_channels;
7640 +
7641 +       /*
7642 +        * Put all channels in the free channel list and clean up channel
7643 +        * states.
7644 +        */
7645 +       item = _hcd->free_hc_list.next;
7646 +       while (item != &_hcd->free_hc_list) {
7647 +               list_del(item);
7648 +               item = _hcd->free_hc_list.next;
7649 +       }
7650 +       num_channels = _hcd->core_if->core_params->host_channels;
7651 +       for (i = 0; i < num_channels; i++) {
7652 +               channel = _hcd->hc_ptr_array[i];
7653 +               list_add_tail(&channel->hc_list_entry, &_hcd->free_hc_list);
7654 +               dwc_otg_hc_cleanup(_hcd->core_if, channel);
7655 +       }
7656 +
7657 +       /* Initialize the DWC core for host mode operation. */
7658 +       dwc_otg_core_host_init(_hcd->core_if);
7659 +}
7660 +
7661 +/** Initializes the DWC_otg controller and its root hub and prepares it for host
7662 + * mode operation. Activates the root port. Returns 0 on success and a negative
7663 + * error code on failure. */
7664 +int dwc_otg_hcd_start(struct usb_hcd *_hcd)
7665 +{
7666 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
7667 +       dwc_otg_core_if_t * core_if = dwc_otg_hcd->core_if;
7668 +       struct usb_bus *bus;
7669 +
7670 +       //      int retval;
7671 +
7672 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
7673 +
7674 +       bus = hcd_to_bus(_hcd);
7675 +
7676 +       /* Initialize the bus state.  If the core is in Device Mode
7677 +        * HALT the USB bus and return. */
7678 +       if (dwc_otg_is_device_mode (core_if)) {
7679 +               _hcd->state = HC_STATE_HALT;
7680 +               return 0;
7681 +       }
7682 +       _hcd->state = HC_STATE_RUNNING;
7683 +
7684 +       /* Initialize and connect root hub if one is not already attached */
7685 +       if (bus->root_hub) {
7686 +               DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
7687 +               /* Inform the HUB driver to resume. */
7688 +               usb_hcd_resume_root_hub(_hcd);
7689 +       }
7690 +       else {
7691 +#if 0
7692 +               struct usb_device *udev;
7693 +               udev = usb_alloc_dev(NULL, bus, 0);
7694 +               if (!udev) {
7695 +                       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Error udev alloc\n");
7696 +                       return -ENODEV;
7697 +               }
7698 +               udev->speed = USB_SPEED_HIGH;
7699 +               /* Not needed - VJ
7700 +                  if ((retval = usb_hcd_register_root_hub(udev, _hcd)) != 0) {
7701 +                  DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Error registering %d\n", retval);
7702 +                  return -ENODEV;
7703 +                  }
7704 +                  */
7705 +#else
7706 +               DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Error udev alloc\n");
7707 +#endif
7708 +       }
7709 +
7710 +       hcd_reinit(dwc_otg_hcd);
7711 +
7712 +       return 0;
7713 +}
7714 +
7715 +static void qh_list_free(dwc_otg_hcd_t *_hcd, struct list_head *_qh_list)
7716 +{
7717 +       struct list_head        *item;
7718 +       dwc_otg_qh_t            *qh;
7719 +
7720 +       if (_qh_list->next == NULL) {
7721 +               /* The list hasn't been initialized yet. */
7722 +               return;
7723 +       }
7724 +
7725 +       /* Ensure there are no QTDs or URBs left. */
7726 +       kill_urbs_in_qh_list(_hcd, _qh_list);
7727 +
7728 +       for (item = _qh_list->next; item != _qh_list; item = _qh_list->next) {
7729 +               qh = list_entry(item, dwc_otg_qh_t, qh_list_entry);
7730 +               dwc_otg_hcd_qh_remove_and_free(_hcd, qh);
7731 +       }
7732 +}
7733 +
7734 +/**
7735 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
7736 + * stopped.
7737 + */
7738 +void dwc_otg_hcd_stop(struct usb_hcd *_hcd)
7739 +{
7740 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
7741 +       hprt0_data_t hprt0 = { .d32=0 };
7742 +
7743 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
7744 +
7745 +       /* Turn off all host-specific interrupts. */
7746 +       dwc_otg_disable_host_interrupts( dwc_otg_hcd->core_if );
7747 +
7748 +       /*
7749 +        * The root hub should be disconnected before this function is called.
7750 +        * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
7751 +        * and the QH lists (via ..._hcd_endpoint_disable).
7752 +        */
7753 +
7754 +       /* Turn off the vbus power */
7755 +       DWC_PRINT("PortPower off\n");
7756 +       hprt0.b.prtpwr = 0;
7757 +       dwc_write_reg32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0.d32);
7758 +
7759 +       return;
7760 +}
7761 +
7762 +
7763 +/** Returns the current frame number. */
7764 +int dwc_otg_hcd_get_frame_number(struct usb_hcd *_hcd)
7765 +{
7766 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(_hcd);
7767 +       hfnum_data_t hfnum;
7768 +
7769 +       hfnum.d32 = dwc_read_reg32(&dwc_otg_hcd->core_if->
7770 +                       host_if->host_global_regs->hfnum);
7771 +
7772 +#ifdef DEBUG_SOF
7773 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n", hfnum.b.frnum);
7774 +#endif 
7775 +       return hfnum.b.frnum;
7776 +}
7777 +
7778 +/**
7779 + * Frees secondary storage associated with the dwc_otg_hcd structure contained
7780 + * in the struct usb_hcd field.
7781 + */
7782 +void dwc_otg_hcd_free(struct usb_hcd *_hcd)
7783 +{
7784 +       dwc_otg_hcd_t   *dwc_otg_hcd = hcd_to_dwc_otg_hcd(_hcd);
7785 +       int             i;
7786 +
7787 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
7788 +
7789 +       del_timers(dwc_otg_hcd);
7790 +
7791 +       /* Free memory for QH/QTD lists */
7792 +       qh_list_free(dwc_otg_hcd,       &dwc_otg_hcd->non_periodic_sched_inactive);
7793 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_deferred);
7794 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
7795 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
7796 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
7797 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
7798 +       qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
7799 +
7800 +       /* Free memory for the host channels. */
7801 +       for (i = 0; i < MAX_EPS_CHANNELS; i++) {
7802 +               dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
7803 +               if (hc != NULL) {
7804 +                       DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n", i, hc);
7805 +                       kfree(hc);
7806 +               }
7807 +       }
7808 +
7809 +       if (dwc_otg_hcd->core_if->dma_enable) {
7810 +               if (dwc_otg_hcd->status_buf_dma) {
7811 +                       dma_free_coherent(_hcd->self.controller,
7812 +                                       DWC_OTG_HCD_STATUS_BUF_SIZE,
7813 +                                       dwc_otg_hcd->status_buf,
7814 +                                       dwc_otg_hcd->status_buf_dma);
7815 +               }
7816 +       } else if (dwc_otg_hcd->status_buf != NULL) {
7817 +               kfree(dwc_otg_hcd->status_buf);
7818 +       }
7819 +
7820 +       return;
7821 +}
7822 +
7823 +
7824 +#ifdef DEBUG
7825 +static void dump_urb_info(struct urb *_urb, char* _fn_name)
7826 +{
7827 +       DWC_PRINT("%s, urb %p\n", _fn_name, _urb);
7828 +       DWC_PRINT("  Device address: %d\n", usb_pipedevice(_urb->pipe));
7829 +       DWC_PRINT("  Endpoint: %d, %s\n", usb_pipeendpoint(_urb->pipe),
7830 +                       (usb_pipein(_urb->pipe) ? "IN" : "OUT"));
7831 +       DWC_PRINT("  Endpoint type: %s\n",
7832 +                       ({char *pipetype;
7833 +                        switch (usb_pipetype(_urb->pipe)) {
7834 +                        case PIPE_CONTROL: pipetype = "CONTROL"; break;
7835 +                        case PIPE_BULK: pipetype = "BULK"; break;
7836 +                        case PIPE_INTERRUPT: pipetype = "INTERRUPT"; break;
7837 +                        case PIPE_ISOCHRONOUS: pipetype = "ISOCHRONOUS"; break;
7838 +                        default: pipetype = "UNKNOWN"; break;
7839 +                        }; pipetype;}));
7840 +       DWC_PRINT("  Speed: %s\n",
7841 +                       ({char *speed;
7842 +                        switch (_urb->dev->speed) {
7843 +                        case USB_SPEED_HIGH: speed = "HIGH"; break;
7844 +                        case USB_SPEED_FULL: speed = "FULL"; break;
7845 +                        case USB_SPEED_LOW: speed = "LOW"; break;
7846 +                        default: speed = "UNKNOWN"; break;
7847 +                        }; speed;}));
7848 +       DWC_PRINT("  Max packet size: %d\n",
7849 +                       usb_maxpacket(_urb->dev, _urb->pipe, usb_pipeout(_urb->pipe)));
7850 +       DWC_PRINT("  Data buffer length: %d\n", _urb->transfer_buffer_length);
7851 +       DWC_PRINT("  Transfer buffer: %p, Transfer DMA: %p\n",
7852 +                       _urb->transfer_buffer, (void *)_urb->transfer_dma);
7853 +       DWC_PRINT("  Setup buffer: %p, Setup DMA: %p\n",
7854 +                       _urb->setup_packet, (void *)_urb->setup_dma);
7855 +       DWC_PRINT("  Interval: %d\n", _urb->interval);
7856 +       if (usb_pipetype(_urb->pipe) == PIPE_ISOCHRONOUS) {
7857 +               int i;
7858 +               for (i = 0; i < _urb->number_of_packets;  i++) {
7859 +                       DWC_PRINT("  ISO Desc %d:\n", i);
7860 +                       DWC_PRINT("    offset: %d, length %d\n",
7861 +                                       _urb->iso_frame_desc[i].offset,
7862 +                                       _urb->iso_frame_desc[i].length);
7863 +               }
7864 +       }
7865 +}
7866 +
7867 +static void dump_channel_info(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *qh)
7868 +{
7869 +       if (qh->channel != NULL) {
7870 +               dwc_hc_t *hc = qh->channel;
7871 +               struct list_head *item;
7872 +               dwc_otg_qh_t *qh_item;
7873 +               int num_channels = _hcd->core_if->core_params->host_channels;
7874 +               int i;
7875 +
7876 +               dwc_otg_hc_regs_t *hc_regs;
7877 +               hcchar_data_t   hcchar;
7878 +               hcsplt_data_t   hcsplt;
7879 +               hctsiz_data_t   hctsiz;
7880 +               uint32_t        hcdma;
7881 +
7882 +               hc_regs = _hcd->core_if->host_if->hc_regs[hc->hc_num];
7883 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
7884 +               hcsplt.d32 = dwc_read_reg32(&hc_regs->hcsplt);
7885 +               hctsiz.d32 = dwc_read_reg32(&hc_regs->hctsiz);
7886 +               hcdma = dwc_read_reg32(&hc_regs->hcdma);
7887 +
7888 +               DWC_PRINT("  Assigned to channel %p:\n", hc);
7889 +               DWC_PRINT("    hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
7890 +               DWC_PRINT("    hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
7891 +               DWC_PRINT("    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
7892 +                               hc->dev_addr, hc->ep_num, hc->ep_is_in);
7893 +               DWC_PRINT("    ep_type: %d\n", hc->ep_type);
7894 +               DWC_PRINT("    max_packet: %d\n", hc->max_packet);
7895 +               DWC_PRINT("    data_pid_start: %d\n", hc->data_pid_start);
7896 +               DWC_PRINT("    xfer_started: %d\n", hc->xfer_started);
7897 +               DWC_PRINT("    halt_status: %d\n", hc->halt_status);
7898 +               DWC_PRINT("    xfer_buff: %p\n", hc->xfer_buff);
7899 +               DWC_PRINT("    xfer_len: %d\n", hc->xfer_len);
7900 +               DWC_PRINT("    qh: %p\n", hc->qh);
7901 +               DWC_PRINT("  NP inactive sched:\n");
7902 +               list_for_each(item, &_hcd->non_periodic_sched_inactive) {
7903 +                       qh_item = list_entry(item, dwc_otg_qh_t, qh_list_entry);
7904 +                       DWC_PRINT("    %p\n", qh_item);
7905 +               } DWC_PRINT("  NP active sched:\n");
7906 +               list_for_each(item, &_hcd->non_periodic_sched_deferred) {
7907 +                       qh_item = list_entry(item, dwc_otg_qh_t, qh_list_entry);
7908 +                       DWC_PRINT("    %p\n", qh_item);
7909 +               } DWC_PRINT("  NP deferred sched:\n");
7910 +               list_for_each(item, &_hcd->non_periodic_sched_active) {
7911 +                       qh_item = list_entry(item, dwc_otg_qh_t, qh_list_entry);
7912 +                       DWC_PRINT("    %p\n", qh_item);
7913 +               } DWC_PRINT("  Channels: \n");
7914 +               for (i = 0; i < num_channels; i++) {
7915 +                       dwc_hc_t *hc = _hcd->hc_ptr_array[i];
7916 +                       DWC_PRINT("    %2d: %p\n", i, hc);
7917 +               }
7918 +       }
7919 +}
7920 +#endif // DEBUG
7921 +
7922 +/** Starts processing a USB transfer request specified by a USB Request Block
7923 + * (URB). mem_flags indicates the type of memory allocation to use while
7924 + * processing this URB. */
7925 +int dwc_otg_hcd_urb_enqueue(struct usb_hcd *_hcd, 
7926 +               struct urb *_urb, 
7927 +               gfp_t _mem_flags)
7928 +{
7929 +       unsigned long flags;
7930 +       int retval;
7931 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
7932 +       dwc_otg_qtd_t *qtd;
7933 +
7934 +       local_irq_save(flags);
7935 +       retval = usb_hcd_link_urb_to_ep(_hcd, _urb);
7936 +       if (retval) {
7937 +               local_irq_restore(flags);
7938 +               return retval;
7939 +       }
7940 +#ifdef DEBUG
7941 +       if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
7942 +               dump_urb_info(_urb, "dwc_otg_hcd_urb_enqueue");
7943 +       }
7944 +#endif // DEBUG
7945 +       if (!dwc_otg_hcd->flags.b.port_connect_status) {
7946 +               /* No longer connected. */
7947 +               local_irq_restore(flags);
7948 +               return -ENODEV;
7949 +       }
7950 +
7951 +       qtd = dwc_otg_hcd_qtd_create (_urb);
7952 +       if (qtd == NULL) {
7953 +               local_irq_restore(flags);
7954 +               DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
7955 +               return -ENOMEM;
7956 +       }
7957 +
7958 +       retval = dwc_otg_hcd_qtd_add (qtd, dwc_otg_hcd);
7959 +       if (retval < 0) {
7960 +               DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
7961 +                               "Error status %d\n", retval);
7962 +               dwc_otg_hcd_qtd_free(qtd);
7963 +       }
7964 +
7965 +       local_irq_restore (flags);
7966 +       return retval;
7967 +}
7968 +
7969 +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
7970 + * success.  */
7971 +int dwc_otg_hcd_urb_dequeue(struct usb_hcd *_hcd, struct urb *_urb, int _status)
7972 +{
7973 +       unsigned long flags;
7974 +       dwc_otg_hcd_t *dwc_otg_hcd;
7975 +       dwc_otg_qtd_t *urb_qtd;
7976 +       dwc_otg_qh_t *qh;
7977 +       int retval;
7978 +       //struct usb_host_endpoint *_ep = NULL;
7979 +
7980 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
7981 +
7982 +       local_irq_save(flags);
7983 +
7984 +       retval = usb_hcd_check_unlink_urb(_hcd, _urb, _status);
7985 +       if (retval) {
7986 +               local_irq_restore(flags);
7987 +               return retval;
7988 +       }
7989 +
7990 +       dwc_otg_hcd = hcd_to_dwc_otg_hcd(_hcd);
7991 +       urb_qtd = (dwc_otg_qtd_t *)_urb->hcpriv;
7992 +       if (urb_qtd == NULL) {
7993 +               printk("urb_qtd is NULL for _urb %08x\n",(unsigned)_urb);
7994 +               goto done;
7995 +       }
7996 +       qh = (dwc_otg_qh_t *) urb_qtd->qtd_qh_ptr;
7997 +       if (qh == NULL) {
7998 +               goto done;
7999 +       }
8000 +
8001 +#ifdef DEBUG
8002 +       if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
8003 +               dump_urb_info(_urb, "dwc_otg_hcd_urb_dequeue");
8004 +               if (urb_qtd == qh->qtd_in_process) {
8005 +                       dump_channel_info(dwc_otg_hcd, qh);
8006 +               }
8007 +       }
8008 +#endif // DEBUG
8009 +
8010 +       if (urb_qtd == qh->qtd_in_process) {
8011 +               /* The QTD is in process (it has been assigned to a channel). */
8012 +
8013 +               if (dwc_otg_hcd->flags.b.port_connect_status) {
8014 +                       /*
8015 +                        * If still connected (i.e. in host mode), halt the
8016 +                        * channel so it can be used for other transfers. If
8017 +                        * no longer connected, the host registers can't be
8018 +                        * written to halt the channel since the core is in
8019 +                        * device mode.
8020 +                        */
8021 +                       dwc_otg_hc_halt(dwc_otg_hcd->core_if, qh->channel,
8022 +                                       DWC_OTG_HC_XFER_URB_DEQUEUE);
8023 +               }
8024 +       }
8025 +
8026 +       /*
8027 +        * Free the QTD and clean up the associated QH. Leave the QH in the
8028 +        * schedule if it has any remaining QTDs.
8029 +        */
8030 +       dwc_otg_hcd_qtd_remove_and_free(urb_qtd);
8031 +       if (urb_qtd == qh->qtd_in_process) {
8032 +               dwc_otg_hcd_qh_deactivate(dwc_otg_hcd, qh, 0);
8033 +               qh->channel = NULL;
8034 +               qh->qtd_in_process = NULL;
8035 +       } else if (list_empty(&qh->qtd_list)) {
8036 +               dwc_otg_hcd_qh_remove(dwc_otg_hcd, qh);
8037 +       }
8038 +
8039 +done:
8040 +       local_irq_restore(flags);
8041 +       _urb->hcpriv = NULL;
8042 +
8043 +       /* Higher layer software sets URB status. */
8044 +       usb_hcd_unlink_urb_from_ep(_hcd, _urb);
8045 +       usb_hcd_giveback_urb(_hcd, _urb, _status);
8046 +       if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
8047 +               DWC_PRINT("Called usb_hcd_giveback_urb()\n");
8048 +               DWC_PRINT("  urb->status = %d\n", _urb->status);
8049 +       }
8050 +
8051 +       return 0;
8052 +}
8053 +
8054 +
8055 +/** Frees resources in the DWC_otg controller related to a given endpoint. Also
8056 + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
8057 + * must already be dequeued. */
8058 +void dwc_otg_hcd_endpoint_disable(struct usb_hcd *_hcd,
8059 +               struct usb_host_endpoint *_ep)
8060 +
8061 +{
8062 +       dwc_otg_qh_t *qh;
8063 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(_hcd);
8064 +
8065 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
8066 +                       "endpoint=%d\n", _ep->desc.bEndpointAddress,
8067 +                       dwc_ep_addr_to_endpoint(_ep->desc.bEndpointAddress));
8068 +
8069 +       qh = (dwc_otg_qh_t *)(_ep->hcpriv);
8070 +       if (qh != NULL) {
8071 +#ifdef DEBUG
8072 +               /** Check that the QTD list is really empty */
8073 +               if (!list_empty(&qh->qtd_list)) {
8074 +                       DWC_WARN("DWC OTG HCD EP DISABLE:"
8075 +                                       " QTD List for this endpoint is not empty\n");
8076 +               }
8077 +#endif // DEBUG
8078 +
8079 +               dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd, qh);
8080 +               _ep->hcpriv = NULL;
8081 +       }
8082 +
8083 +       return;
8084 +}
8085 +extern int dwc_irq;
8086 +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
8087 + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
8088 + * interrupt.
8089 + *
8090 + * This function is called by the USB core when an interrupt occurs */
8091 +irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *_hcd)
8092 +{
8093 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
8094 +
8095 +       mask_and_ack_ifx_irq (dwc_irq);
8096 +       return IRQ_RETVAL(dwc_otg_hcd_handle_intr(dwc_otg_hcd));
8097 +}
8098 +
8099 +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
8100 + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
8101 + * is the status change indicator for the single root port. Returns 1 if either
8102 + * change indicator is 1, otherwise returns 0. */
8103 +int dwc_otg_hcd_hub_status_data(struct usb_hcd *_hcd, char *_buf)
8104 +{
8105 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
8106 +
8107 +       _buf[0] = 0;
8108 +       _buf[0] |= (dwc_otg_hcd->flags.b.port_connect_status_change ||
8109 +                       dwc_otg_hcd->flags.b.port_reset_change ||
8110 +                       dwc_otg_hcd->flags.b.port_enable_change ||
8111 +                       dwc_otg_hcd->flags.b.port_suspend_change ||
8112 +                       dwc_otg_hcd->flags.b.port_over_current_change) << 1;
8113 +
8114 +#ifdef DEBUG
8115 +       if (_buf[0]) {
8116 +               DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
8117 +                               " Root port status changed\n");
8118 +               DWC_DEBUGPL(DBG_HCDV, "  port_connect_status_change: %d\n",
8119 +                               dwc_otg_hcd->flags.b.port_connect_status_change);
8120 +               DWC_DEBUGPL(DBG_HCDV, "  port_reset_change: %d\n",
8121 +                               dwc_otg_hcd->flags.b.port_reset_change);
8122 +               DWC_DEBUGPL(DBG_HCDV, "  port_enable_change: %d\n",
8123 +                               dwc_otg_hcd->flags.b.port_enable_change);
8124 +               DWC_DEBUGPL(DBG_HCDV, "  port_suspend_change: %d\n",
8125 +                               dwc_otg_hcd->flags.b.port_suspend_change);
8126 +               DWC_DEBUGPL(DBG_HCDV, "  port_over_current_change: %d\n",
8127 +                               dwc_otg_hcd->flags.b.port_over_current_change);
8128 +       }
8129 +#endif // DEBUG
8130 +       return (_buf[0] != 0);
8131 +}
8132 +
8133 +#ifdef DWC_HS_ELECT_TST
8134 +/*
8135 + * Quick and dirty hack to implement the HS Electrical Test
8136 + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
8137 + *
8138 + * This code was copied from our userspace app "hset". It sends a
8139 + * Get Device Descriptor control sequence in two parts, first the
8140 + * Setup packet by itself, followed some time later by the In and
8141 + * Ack packets. Rather than trying to figure out how to add this
8142 + * functionality to the normal driver code, we just hijack the
8143 + * hardware, using these two function to drive the hardware
8144 + * directly.
8145 + */
8146 +
8147 +dwc_otg_core_global_regs_t *global_regs;
8148 +dwc_otg_host_global_regs_t *hc_global_regs;
8149 +dwc_otg_hc_regs_t *hc_regs;
8150 +uint32_t *data_fifo;
8151 +
8152 +static void do_setup(void)
8153 +{
8154 +       gintsts_data_t gintsts;
8155 +       hctsiz_data_t hctsiz;
8156 +       hcchar_data_t hcchar;
8157 +       haint_data_t haint;
8158 +       hcint_data_t hcint;
8159 +
8160 +       /* Enable HAINTs */
8161 +       dwc_write_reg32(&hc_global_regs->haintmsk, 0x0001);
8162 +
8163 +       /* Enable HCINTs */
8164 +       dwc_write_reg32(&hc_regs->hcintmsk, 0x04a3);
8165 +
8166 +       /* Read GINTSTS */
8167 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8168 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8169 +
8170 +       /* Read HAINT */
8171 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8172 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8173 +
8174 +       /* Read HCINT */
8175 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8176 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8177 +
8178 +       /* Read HCCHAR */
8179 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8180 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8181 +
8182 +       /* Clear HCINT */
8183 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8184 +
8185 +       /* Clear HAINT */
8186 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8187 +
8188 +       /* Clear GINTSTS */
8189 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8190 +
8191 +       /* Read GINTSTS */
8192 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8193 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8194 +
8195 +       /*
8196 +        * Send Setup packet (Get Device Descriptor)
8197 +        */
8198 +
8199 +       /* Make sure channel is disabled */
8200 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8201 +       if (hcchar.b.chen) {
8202 +               //fprintf(stderr, "Channel already enabled 1, HCCHAR = %08x\n", hcchar.d32);
8203 +               hcchar.b.chdis = 1;
8204 +               //              hcchar.b.chen = 1;
8205 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8206 +               //sleep(1);
8207 +               MDELAY(1000);
8208 +
8209 +               /* Read GINTSTS */
8210 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8211 +               //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8212 +
8213 +               /* Read HAINT */
8214 +               haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8215 +               //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8216 +
8217 +               /* Read HCINT */
8218 +               hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8219 +               //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8220 +
8221 +               /* Read HCCHAR */
8222 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8223 +               //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8224 +
8225 +               /* Clear HCINT */
8226 +               dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8227 +
8228 +               /* Clear HAINT */
8229 +               dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8230 +
8231 +               /* Clear GINTSTS */
8232 +               dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8233 +
8234 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8235 +               //if (hcchar.b.chen) {
8236 +               //      fprintf(stderr, "** Channel _still_ enabled 1, HCCHAR = %08x **\n", hcchar.d32);
8237 +               //}
8238 +       }
8239 +
8240 +       /* Set HCTSIZ */
8241 +       hctsiz.d32 = 0;
8242 +       hctsiz.b.xfersize = 8;
8243 +       hctsiz.b.pktcnt = 1;
8244 +       hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
8245 +       dwc_write_reg32(&hc_regs->hctsiz, hctsiz.d32);
8246 +
8247 +       /* Set HCCHAR */
8248 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8249 +       hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
8250 +       hcchar.b.epdir = 0;
8251 +       hcchar.b.epnum = 0;
8252 +       hcchar.b.mps = 8;
8253 +       hcchar.b.chen = 1;
8254 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8255 +
8256 +       /* Fill FIFO with Setup data for Get Device Descriptor */
8257 +       data_fifo = (uint32_t *)((char *)global_regs + 0x1000);
8258 +       dwc_write_reg32(data_fifo++, 0x01000680);
8259 +       dwc_write_reg32(data_fifo++, 0x00080000);
8260 +
8261 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8262 +       //fprintf(stderr, "Waiting for HCINTR intr 1, GINTSTS = %08x\n", gintsts.d32);
8263 +
8264 +       /* Wait for host channel interrupt */
8265 +       do {
8266 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8267 +       } while (gintsts.b.hcintr == 0);
8268 +
8269 +       //fprintf(stderr, "Got HCINTR intr 1, GINTSTS = %08x\n", gintsts.d32);
8270 +
8271 +       /* Disable HCINTs */
8272 +       dwc_write_reg32(&hc_regs->hcintmsk, 0x0000);
8273 +
8274 +       /* Disable HAINTs */
8275 +       dwc_write_reg32(&hc_global_regs->haintmsk, 0x0000);
8276 +
8277 +       /* Read HAINT */
8278 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8279 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8280 +
8281 +       /* Read HCINT */
8282 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8283 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8284 +
8285 +       /* Read HCCHAR */
8286 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8287 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8288 +
8289 +       /* Clear HCINT */
8290 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8291 +
8292 +       /* Clear HAINT */
8293 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8294 +
8295 +       /* Clear GINTSTS */
8296 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8297 +
8298 +       /* Read GINTSTS */
8299 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8300 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8301 +}
8302 +
8303 +static void do_in_ack(void)
8304 +{
8305 +       gintsts_data_t gintsts;
8306 +       hctsiz_data_t hctsiz;
8307 +       hcchar_data_t hcchar;
8308 +       haint_data_t haint;
8309 +       hcint_data_t hcint;
8310 +       host_grxsts_data_t grxsts;
8311 +
8312 +       /* Enable HAINTs */
8313 +       dwc_write_reg32(&hc_global_regs->haintmsk, 0x0001);
8314 +
8315 +       /* Enable HCINTs */
8316 +       dwc_write_reg32(&hc_regs->hcintmsk, 0x04a3);
8317 +
8318 +       /* Read GINTSTS */
8319 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8320 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8321 +
8322 +       /* Read HAINT */
8323 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8324 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8325 +
8326 +       /* Read HCINT */
8327 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8328 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8329 +
8330 +       /* Read HCCHAR */
8331 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8332 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8333 +
8334 +       /* Clear HCINT */
8335 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8336 +
8337 +       /* Clear HAINT */
8338 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8339 +
8340 +       /* Clear GINTSTS */
8341 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8342 +
8343 +       /* Read GINTSTS */
8344 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8345 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8346 +
8347 +       /*
8348 +        * Receive Control In packet
8349 +        */
8350 +
8351 +       /* Make sure channel is disabled */
8352 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8353 +       if (hcchar.b.chen) {
8354 +               //fprintf(stderr, "Channel already enabled 2, HCCHAR = %08x\n", hcchar.d32);
8355 +               hcchar.b.chdis = 1;
8356 +               hcchar.b.chen = 1;
8357 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8358 +               //sleep(1);
8359 +               MDELAY(1000);
8360 +
8361 +               /* Read GINTSTS */
8362 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8363 +               //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8364 +
8365 +               /* Read HAINT */
8366 +               haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8367 +               //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8368 +
8369 +               /* Read HCINT */
8370 +               hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8371 +               //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8372 +
8373 +               /* Read HCCHAR */
8374 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8375 +               //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8376 +
8377 +               /* Clear HCINT */
8378 +               dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8379 +
8380 +               /* Clear HAINT */
8381 +               dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8382 +
8383 +               /* Clear GINTSTS */
8384 +               dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8385 +
8386 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8387 +               //if (hcchar.b.chen) {
8388 +               //      fprintf(stderr, "** Channel _still_ enabled 2, HCCHAR = %08x **\n", hcchar.d32);
8389 +               //}
8390 +       }
8391 +
8392 +       /* Set HCTSIZ */
8393 +       hctsiz.d32 = 0;
8394 +       hctsiz.b.xfersize = 8;
8395 +       hctsiz.b.pktcnt = 1;
8396 +       hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
8397 +       dwc_write_reg32(&hc_regs->hctsiz, hctsiz.d32);
8398 +
8399 +       /* Set HCCHAR */
8400 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8401 +       hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
8402 +       hcchar.b.epdir = 1;
8403 +       hcchar.b.epnum = 0;
8404 +       hcchar.b.mps = 8;
8405 +       hcchar.b.chen = 1;
8406 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8407 +
8408 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8409 +       //fprintf(stderr, "Waiting for RXSTSQLVL intr 1, GINTSTS = %08x\n", gintsts.d32);
8410 +
8411 +       /* Wait for receive status queue interrupt */
8412 +       do {
8413 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8414 +       } while (gintsts.b.rxstsqlvl == 0);
8415 +
8416 +       //fprintf(stderr, "Got RXSTSQLVL intr 1, GINTSTS = %08x\n", gintsts.d32);
8417 +
8418 +       /* Read RXSTS */
8419 +       grxsts.d32 = dwc_read_reg32(&global_regs->grxstsp);
8420 +       //fprintf(stderr, "GRXSTS: %08x\n", grxsts.d32);
8421 +
8422 +       /* Clear RXSTSQLVL in GINTSTS */
8423 +       gintsts.d32 = 0;
8424 +       gintsts.b.rxstsqlvl = 1;
8425 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8426 +
8427 +       switch (grxsts.b.pktsts) {
8428 +               case DWC_GRXSTS_PKTSTS_IN:
8429 +                       /* Read the data into the host buffer */
8430 +                       if (grxsts.b.bcnt > 0) {
8431 +                               int i;
8432 +                               int word_count = (grxsts.b.bcnt + 3) / 4;
8433 +
8434 +                               data_fifo = (uint32_t *)((char *)global_regs + 0x1000);
8435 +
8436 +                               for (i = 0; i < word_count; i++) {
8437 +                                       (void)dwc_read_reg32(data_fifo++);
8438 +                               }
8439 +                       }
8440 +
8441 +                       //fprintf(stderr, "Received %u bytes\n", (unsigned)grxsts.b.bcnt);
8442 +                       break;
8443 +
8444 +               default:
8445 +                       //fprintf(stderr, "** Unexpected GRXSTS packet status 1 **\n");
8446 +                       break;
8447 +       }
8448 +
8449 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8450 +       //fprintf(stderr, "Waiting for RXSTSQLVL intr 2, GINTSTS = %08x\n", gintsts.d32);
8451 +
8452 +       /* Wait for receive status queue interrupt */
8453 +       do {
8454 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8455 +       } while (gintsts.b.rxstsqlvl == 0);
8456 +
8457 +       //fprintf(stderr, "Got RXSTSQLVL intr 2, GINTSTS = %08x\n", gintsts.d32);
8458 +
8459 +       /* Read RXSTS */
8460 +       grxsts.d32 = dwc_read_reg32(&global_regs->grxstsp);
8461 +       //fprintf(stderr, "GRXSTS: %08x\n", grxsts.d32);
8462 +
8463 +       /* Clear RXSTSQLVL in GINTSTS */
8464 +       gintsts.d32 = 0;
8465 +       gintsts.b.rxstsqlvl = 1;
8466 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8467 +
8468 +       switch (grxsts.b.pktsts) {
8469 +               case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
8470 +                       break;
8471 +
8472 +               default:
8473 +                       //fprintf(stderr, "** Unexpected GRXSTS packet status 2 **\n");
8474 +                       break;
8475 +       }
8476 +
8477 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8478 +       //fprintf(stderr, "Waiting for HCINTR intr 2, GINTSTS = %08x\n", gintsts.d32);
8479 +
8480 +       /* Wait for host channel interrupt */
8481 +       do {
8482 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8483 +       } while (gintsts.b.hcintr == 0);
8484 +
8485 +       //fprintf(stderr, "Got HCINTR intr 2, GINTSTS = %08x\n", gintsts.d32);
8486 +
8487 +       /* Read HAINT */
8488 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8489 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8490 +
8491 +       /* Read HCINT */
8492 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8493 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8494 +
8495 +       /* Read HCCHAR */
8496 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8497 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8498 +
8499 +       /* Clear HCINT */
8500 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8501 +
8502 +       /* Clear HAINT */
8503 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8504 +
8505 +       /* Clear GINTSTS */
8506 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8507 +
8508 +       /* Read GINTSTS */
8509 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8510 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8511 +
8512 +       //      usleep(100000);
8513 +       //      mdelay(100);
8514 +       MDELAY(1);
8515 +
8516 +       /*
8517 +        * Send handshake packet
8518 +        */
8519 +
8520 +       /* Read HAINT */
8521 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8522 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8523 +
8524 +       /* Read HCINT */
8525 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8526 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8527 +
8528 +       /* Read HCCHAR */
8529 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8530 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8531 +
8532 +       /* Clear HCINT */
8533 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8534 +
8535 +       /* Clear HAINT */
8536 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8537 +
8538 +       /* Clear GINTSTS */
8539 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8540 +
8541 +       /* Read GINTSTS */
8542 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8543 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8544 +
8545 +       /* Make sure channel is disabled */
8546 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8547 +       if (hcchar.b.chen) {
8548 +               //fprintf(stderr, "Channel already enabled 3, HCCHAR = %08x\n", hcchar.d32);
8549 +               hcchar.b.chdis = 1;
8550 +               hcchar.b.chen = 1;
8551 +               dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8552 +               //sleep(1);
8553 +               MDELAY(1000);
8554 +
8555 +               /* Read GINTSTS */
8556 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8557 +               //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8558 +
8559 +               /* Read HAINT */
8560 +               haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8561 +               //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8562 +
8563 +               /* Read HCINT */
8564 +               hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8565 +               //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8566 +
8567 +               /* Read HCCHAR */
8568 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8569 +               //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8570 +
8571 +               /* Clear HCINT */
8572 +               dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8573 +
8574 +               /* Clear HAINT */
8575 +               dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8576 +
8577 +               /* Clear GINTSTS */
8578 +               dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8579 +
8580 +               hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8581 +               //if (hcchar.b.chen) {
8582 +               //      fprintf(stderr, "** Channel _still_ enabled 3, HCCHAR = %08x **\n", hcchar.d32);
8583 +               //}
8584 +       }
8585 +
8586 +       /* Set HCTSIZ */
8587 +       hctsiz.d32 = 0;
8588 +       hctsiz.b.xfersize = 0;
8589 +       hctsiz.b.pktcnt = 1;
8590 +       hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
8591 +       dwc_write_reg32(&hc_regs->hctsiz, hctsiz.d32);
8592 +
8593 +       /* Set HCCHAR */
8594 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8595 +       hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
8596 +       hcchar.b.epdir = 0;
8597 +       hcchar.b.epnum = 0;
8598 +       hcchar.b.mps = 8;
8599 +       hcchar.b.chen = 1;
8600 +       dwc_write_reg32(&hc_regs->hcchar, hcchar.d32);
8601 +
8602 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8603 +       //fprintf(stderr, "Waiting for HCINTR intr 3, GINTSTS = %08x\n", gintsts.d32);
8604 +
8605 +       /* Wait for host channel interrupt */
8606 +       do {
8607 +               gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8608 +       } while (gintsts.b.hcintr == 0);
8609 +
8610 +       //fprintf(stderr, "Got HCINTR intr 3, GINTSTS = %08x\n", gintsts.d32);
8611 +
8612 +       /* Disable HCINTs */
8613 +       dwc_write_reg32(&hc_regs->hcintmsk, 0x0000);
8614 +
8615 +       /* Disable HAINTs */
8616 +       dwc_write_reg32(&hc_global_regs->haintmsk, 0x0000);
8617 +
8618 +       /* Read HAINT */
8619 +       haint.d32 = dwc_read_reg32(&hc_global_regs->haint);
8620 +       //fprintf(stderr, "HAINT: %08x\n", haint.d32);
8621 +
8622 +       /* Read HCINT */
8623 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
8624 +       //fprintf(stderr, "HCINT: %08x\n", hcint.d32);
8625 +
8626 +       /* Read HCCHAR */
8627 +       hcchar.d32 = dwc_read_reg32(&hc_regs->hcchar);
8628 +       //fprintf(stderr, "HCCHAR: %08x\n", hcchar.d32);
8629 +
8630 +       /* Clear HCINT */
8631 +       dwc_write_reg32(&hc_regs->hcint, hcint.d32);
8632 +
8633 +       /* Clear HAINT */
8634 +       dwc_write_reg32(&hc_global_regs->haint, haint.d32);
8635 +
8636 +       /* Clear GINTSTS */
8637 +       dwc_write_reg32(&global_regs->gintsts, gintsts.d32);
8638 +
8639 +       /* Read GINTSTS */
8640 +       gintsts.d32 = dwc_read_reg32(&global_regs->gintsts);
8641 +       //fprintf(stderr, "GINTSTS: %08x\n", gintsts.d32);
8642 +}
8643 +#endif /* DWC_HS_ELECT_TST */
8644 +
8645 +/** Handles hub class-specific requests.*/
8646 +int dwc_otg_hcd_hub_control(struct usb_hcd *_hcd, 
8647 +               u16 _typeReq, 
8648 +               u16 _wValue, 
8649 +               u16 _wIndex, 
8650 +               char *_buf, 
8651 +               u16 _wLength)
8652 +{
8653 +       int retval = 0;
8654 +
8655 +       dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (_hcd);
8656 +       dwc_otg_core_if_t *core_if = hcd_to_dwc_otg_hcd (_hcd)->core_if;
8657 +       struct usb_hub_descriptor *desc;
8658 +       hprt0_data_t hprt0 = {.d32 = 0};
8659 +
8660 +       uint32_t port_status;
8661 +
8662 +       switch (_typeReq) {
8663 +               case ClearHubFeature:
8664 +                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8665 +                                       "ClearHubFeature 0x%x\n", _wValue);
8666 +                       switch (_wValue) {
8667 +                               case C_HUB_LOCAL_POWER:
8668 +                               case C_HUB_OVER_CURRENT:
8669 +                                       /* Nothing required here */
8670 +                                       break;
8671 +                               default:
8672 +                                       retval = -EINVAL;
8673 +                                       DWC_ERROR ("DWC OTG HCD - "
8674 +                                                       "ClearHubFeature request %xh unknown\n", _wValue);
8675 +                       }
8676 +                       break;
8677 +               case ClearPortFeature:
8678 +                       if (!_wIndex || _wIndex > 1)
8679 +                               goto error;
8680 +
8681 +                       switch (_wValue) {
8682 +                               case USB_PORT_FEAT_ENABLE:
8683 +                                       DWC_DEBUGPL (DBG_ANY, "DWC OTG HCD HUB CONTROL - "
8684 +                                                       "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
8685 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8686 +                                       hprt0.b.prtena = 1;
8687 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8688 +                                       break;
8689 +                               case USB_PORT_FEAT_SUSPEND:
8690 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8691 +                                                       "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
8692 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8693 +                                       hprt0.b.prtres = 1;
8694 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8695 +                                       /* Clear Resume bit */
8696 +                                       mdelay (100);
8697 +                                       hprt0.b.prtres = 0;
8698 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8699 +                                       break;
8700 +                               case USB_PORT_FEAT_POWER:
8701 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8702 +                                                       "ClearPortFeature USB_PORT_FEAT_POWER\n");
8703 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8704 +                                       hprt0.b.prtpwr = 0;
8705 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8706 +                                       break;
8707 +                               case USB_PORT_FEAT_INDICATOR:
8708 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8709 +                                                       "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
8710 +                                       /* Port inidicator not supported */
8711 +                                       break;
8712 +                               case USB_PORT_FEAT_C_CONNECTION:
8713 +                                       /* Clears drivers internal connect status change
8714 +                                        * flag */
8715 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8716 +                                                       "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
8717 +                                       dwc_otg_hcd->flags.b.port_connect_status_change = 0;
8718 +                                       break;
8719 +                               case USB_PORT_FEAT_C_RESET:
8720 +                                       /* Clears the driver's internal Port Reset Change
8721 +                                        * flag */
8722 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8723 +                                                       "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
8724 +                                       dwc_otg_hcd->flags.b.port_reset_change = 0;
8725 +                                       break;
8726 +                               case USB_PORT_FEAT_C_ENABLE:
8727 +                                       /* Clears the driver's internal Port
8728 +                                        * Enable/Disable Change flag */
8729 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8730 +                                                       "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
8731 +                                       dwc_otg_hcd->flags.b.port_enable_change = 0;
8732 +                                       break;
8733 +                               case USB_PORT_FEAT_C_SUSPEND:
8734 +                                       /* Clears the driver's internal Port Suspend
8735 +                                        * Change flag, which is set when resume signaling on
8736 +                                        * the host port is complete */
8737 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8738 +                                                       "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
8739 +                                       dwc_otg_hcd->flags.b.port_suspend_change = 0;
8740 +                                       break;
8741 +                               case USB_PORT_FEAT_C_OVER_CURRENT:
8742 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8743 +                                                       "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
8744 +                                       dwc_otg_hcd->flags.b.port_over_current_change = 0;
8745 +                                       break;
8746 +                               default:
8747 +                                       retval = -EINVAL;
8748 +                                       DWC_ERROR ("DWC OTG HCD - "
8749 +                                                       "ClearPortFeature request %xh "
8750 +                                                       "unknown or unsupported\n", _wValue);
8751 +                       }
8752 +                       break;
8753 +               case GetHubDescriptor:
8754 +                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8755 +                                       "GetHubDescriptor\n");
8756 +                       desc = (struct usb_hub_descriptor *)_buf;
8757 +                       desc->bDescLength = 9;
8758 +                       desc->bDescriptorType = 0x29;
8759 +                       desc->bNbrPorts = 1;
8760 +                       desc->wHubCharacteristics = 0x08;
8761 +                       desc->bPwrOn2PwrGood = 1;
8762 +                       desc->bHubContrCurrent = 0;
8763 +                       desc->bitmap[0] = 0;
8764 +                       desc->bitmap[1] = 0xff;
8765 +                       break;
8766 +               case GetHubStatus:
8767 +                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8768 +                                       "GetHubStatus\n");
8769 +                       memset (_buf, 0, 4);
8770 +                       break;
8771 +               case GetPortStatus:
8772 +                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8773 +                                       "GetPortStatus\n");
8774 +
8775 +                       if (!_wIndex || _wIndex > 1)
8776 +                               goto error;
8777 +
8778 +                       port_status = 0;
8779 +
8780 +                       if (dwc_otg_hcd->flags.b.port_connect_status_change)
8781 +                               port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
8782 +
8783 +                       if (dwc_otg_hcd->flags.b.port_enable_change)
8784 +                               port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
8785 +
8786 +                       if (dwc_otg_hcd->flags.b.port_suspend_change)
8787 +                               port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
8788 +
8789 +                       if (dwc_otg_hcd->flags.b.port_reset_change)
8790 +                               port_status |= (1 << USB_PORT_FEAT_C_RESET);
8791 +
8792 +                       if (dwc_otg_hcd->flags.b.port_over_current_change) {
8793 +                               DWC_ERROR("Device Not Supported\n");
8794 +                               port_status |= (1 << USB_PORT_FEAT_C_OVER_CURRENT);
8795 +                       }
8796 +
8797 +                       if (!dwc_otg_hcd->flags.b.port_connect_status) {
8798 +                               printk("DISCONNECTED PORT\n");
8799 +                               /*
8800 +                                * The port is disconnected, which means the core is
8801 +                                * either in device mode or it soon will be. Just
8802 +                                * return 0's for the remainder of the port status
8803 +                                * since the port register can't be read if the core
8804 +                                * is in device mode.
8805 +                                */
8806 +#if 1 // winder.
8807 +                               *((u32 *) _buf) = cpu_to_le32(port_status);
8808 +#else
8809 +                               *((__le32 *) _buf) = cpu_to_le32(port_status);
8810 +#endif
8811 +                               break;
8812 +                       }
8813 +
8814 +                       hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0);
8815 +                       DWC_DEBUGPL(DBG_HCDV, "  HPRT0: 0x%08x\n", hprt0.d32);
8816 +
8817 +                       if (hprt0.b.prtconnsts) 
8818 +                               port_status |= (1 << USB_PORT_FEAT_CONNECTION);
8819 +
8820 +                       if (hprt0.b.prtena)
8821 +                               port_status |= (1 << USB_PORT_FEAT_ENABLE);
8822 +
8823 +                       if (hprt0.b.prtsusp)
8824 +                               port_status |= (1 << USB_PORT_FEAT_SUSPEND);
8825 +
8826 +                       if (hprt0.b.prtovrcurract)
8827 +                               port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
8828 +
8829 +                       if (hprt0.b.prtrst)
8830 +                               port_status |= (1 << USB_PORT_FEAT_RESET);
8831 +
8832 +                       if (hprt0.b.prtpwr)
8833 +                               port_status |= (1 << USB_PORT_FEAT_POWER);
8834 +
8835 +                       if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
8836 +                               port_status |= USB_PORT_STAT_HIGH_SPEED;
8837 +
8838 +                       else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
8839 +                               port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
8840 +
8841 +                       if (hprt0.b.prttstctl)
8842 +                               port_status |= (1 << USB_PORT_FEAT_TEST);
8843 +
8844 +                       /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
8845 +#if 1 // winder.
8846 +                       *((u32 *) _buf) = cpu_to_le32(port_status);
8847 +#else
8848 +                       *((__le32 *) _buf) = cpu_to_le32(port_status);
8849 +#endif
8850 +
8851 +                       break;
8852 +               case SetHubFeature:
8853 +                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8854 +                                       "SetHubFeature\n");
8855 +                       /* No HUB features supported */
8856 +                       break;
8857 +               case SetPortFeature:
8858 +                       if (_wValue != USB_PORT_FEAT_TEST && (!_wIndex || _wIndex > 1))
8859 +                               goto error;
8860 +
8861 +                       if (!dwc_otg_hcd->flags.b.port_connect_status) {
8862 +                               /*
8863 +                                * The port is disconnected, which means the core is
8864 +                                * either in device mode or it soon will be. Just
8865 +                                * return without doing anything since the port
8866 +                                * register can't be written if the core is in device
8867 +                                * mode.
8868 +                                */
8869 +                               break;
8870 +                       }
8871 +
8872 +                       switch (_wValue) {
8873 +                               case USB_PORT_FEAT_SUSPEND:
8874 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8875 +                                                       "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
8876 +                                       if (_hcd->self.otg_port == _wIndex
8877 +                                                       && _hcd->self.b_hnp_enable) {
8878 +                                               gotgctl_data_t  gotgctl = {.d32=0};
8879 +                                               gotgctl.b.hstsethnpen = 1;
8880 +                                               dwc_modify_reg32(&core_if->core_global_regs->
8881 +                                                               gotgctl, 0, gotgctl.d32);
8882 +                                               core_if->op_state = A_SUSPEND;
8883 +                                       }
8884 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8885 +                                       hprt0.b.prtsusp = 1;
8886 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8887 +                                       //DWC_PRINT( "SUSPEND: HPRT0=%0x\n", hprt0.d32);       
8888 +                                       /* Suspend the Phy Clock */
8889 +                                       {
8890 +                                               pcgcctl_data_t pcgcctl = {.d32=0};
8891 +                                               pcgcctl.b.stoppclk = 1;
8892 +                                               dwc_write_reg32(core_if->pcgcctl, pcgcctl.d32);
8893 +                                       }
8894 +
8895 +                                       /* For HNP the bus must be suspended for at least 200ms.*/
8896 +                                       if (_hcd->self.b_hnp_enable) {
8897 +                                               mdelay(200);
8898 +                                               //DWC_PRINT( "SUSPEND: wait complete! (%d)\n", _hcd->state);
8899 +                                       }
8900 +                                       break;
8901 +                               case USB_PORT_FEAT_POWER:
8902 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8903 +                                                       "SetPortFeature - USB_PORT_FEAT_POWER\n");
8904 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8905 +                                       hprt0.b.prtpwr = 1;
8906 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8907 +                                       break;
8908 +                               case USB_PORT_FEAT_RESET:
8909 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8910 +                                                       "SetPortFeature - USB_PORT_FEAT_RESET\n");
8911 +                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8912 +                                       /* TODO: Is this for OTG protocol??
8913 +                                        *       We shoudl remove OTG totally for Danube system.
8914 +                                        *       But, in the future, maybe we need this.
8915 +                                        */
8916 +#if 1 // winder 
8917 +                                       hprt0.b.prtrst = 1;
8918 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8919 +#else
8920 +                                       /* When B-Host the Port reset bit is set in
8921 +                                        * the Start HCD Callback function, so that
8922 +                                        * the reset is started within 1ms of the HNP
8923 +                                        * success interrupt. */
8924 +                                       if (!_hcd->self.is_b_host) {
8925 +                                               hprt0.b.prtrst = 1;
8926 +                                               dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8927 +                                       }
8928 +#endif
8929 +                                       /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
8930 +                                       MDELAY (60);
8931 +                                       hprt0.b.prtrst = 0;
8932 +                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8933 +                                       break;
8934 +
8935 +#ifdef DWC_HS_ELECT_TST
8936 +                               case USB_PORT_FEAT_TEST:
8937 +                                       {
8938 +                                               uint32_t t;
8939 +                                               gintmsk_data_t gintmsk;
8940 +
8941 +                                               t = (_wIndex >> 8); /* MSB wIndex USB */
8942 +                                               DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
8943 +                                                               "SetPortFeature - USB_PORT_FEAT_TEST %d\n", t);
8944 +                                               printk("USB_PORT_FEAT_TEST %d\n", t);
8945 +                                               if (t < 6) {
8946 +                                                       hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8947 +                                                       hprt0.b.prttstctl = t;
8948 +                                                       dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8949 +                                               } else {
8950 +                                                       /* Setup global vars with reg addresses (quick and
8951 +                                                        * dirty hack, should be cleaned up)
8952 +                                                        */
8953 +                                                       global_regs = core_if->core_global_regs;
8954 +                                                       hc_global_regs = core_if->host_if->host_global_regs;
8955 +                                                       hc_regs = (dwc_otg_hc_regs_t *)((char *)global_regs + 0x500);
8956 +                                                       data_fifo = (uint32_t *)((char *)global_regs + 0x1000);
8957 +
8958 +                                                       if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
8959 +                                                               /* Save current interrupt mask */
8960 +                                                               gintmsk.d32 = dwc_read_reg32(&global_regs->gintmsk);
8961 +
8962 +                                                               /* Disable all interrupts while we muck with
8963 +                                                                * the hardware directly
8964 +                                                                */
8965 +                                                               dwc_write_reg32(&global_regs->gintmsk, 0);
8966 +
8967 +                                                               /* 15 second delay per the test spec */
8968 +                                                               mdelay(15000);
8969 +
8970 +                                                               /* Drive suspend on the root port */
8971 +                                                               hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8972 +                                                               hprt0.b.prtsusp = 1;
8973 +                                                               hprt0.b.prtres = 0;
8974 +                                                               dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8975 +
8976 +                                                               /* 15 second delay per the test spec */
8977 +                                                               mdelay(15000);
8978 +
8979 +                                                               /* Drive resume on the root port */
8980 +                                                               hprt0.d32 = dwc_otg_read_hprt0 (core_if);
8981 +                                                               hprt0.b.prtsusp = 0;
8982 +                                                               hprt0.b.prtres = 1;
8983 +                                                               dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8984 +                                                               mdelay(100);
8985 +
8986 +                                                               /* Clear the resume bit */
8987 +                                                               hprt0.b.prtres = 0;
8988 +                                                               dwc_write_reg32(core_if->host_if->hprt0, hprt0.d32);
8989 +
8990 +                                                               /* Restore interrupts */
8991 +                                                               dwc_write_reg32(&global_regs->gintmsk, gintmsk.d32);
8992 +                                                       } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
8993 +                                                               /* Save current interrupt mask */
8994 +                                                               gintmsk.d32 = dwc_read_reg32(&global_regs->gintmsk);
8995 +
8996 +                                                               /* Disable all interrupts while we muck with
8997 +                                                                * the hardware directly
8998 +                                                                */
8999 +                                                               dwc_write_reg32(&global_regs->gintmsk, 0);
9000 +
9001 +                                                               /* 15 second delay per the test spec */
9002 +                                                               mdelay(15000);
9003 +
9004 +                                                               /* Send the Setup packet */
9005 +                                                               do_setup();
9006 +
9007 +                                                               /* 15 second delay so nothing else happens for awhile */
9008 +                                                               mdelay(15000);
9009 +
9010 +                                                               /* Restore interrupts */
9011 +                                                               dwc_write_reg32(&global_regs->gintmsk, gintmsk.d32);
9012 +                                                       } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
9013 +                                                               /* Save current interrupt mask */
9014 +                                                               gintmsk.d32 = dwc_read_reg32(&global_regs->gintmsk);
9015 +
9016 +                                                               /* Disable all interrupts while we muck with
9017 +                                                                * the hardware directly
9018 +                                                                */
9019 +                                                               dwc_write_reg32(&global_regs->gintmsk, 0);
9020 +
9021 +                                                               /* Send the Setup packet */
9022 +                                                               do_setup();
9023 +
9024 +                                                               /* 15 second delay so nothing else happens for awhile */
9025 +                                                               mdelay(15000);
9026 +
9027 +                                                               /* Send the In and Ack packets */
9028 +                                                               do_in_ack();
9029 +
9030 +                                                               /* 15 second delay so nothing else happens for awhile */
9031 +                                                               mdelay(15000);
9032 +
9033 +                                                               /* Restore interrupts */
9034 +                                                               dwc_write_reg32(&global_regs->gintmsk, gintmsk.d32);
9035 +                                                       }
9036 +                                               }
9037 +                                               break;
9038 +                                       }
9039 +#endif /* DWC_HS_ELECT_TST */
9040 +
9041 +                               case USB_PORT_FEAT_INDICATOR:
9042 +                                       DWC_DEBUGPL (DBG_HCD, "DWC OTG HCD HUB CONTROL - "
9043 +                                                       "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
9044 +                                       /* Not supported */
9045 +                                       break;
9046 +                               default:
9047 +                                       retval = -EINVAL;
9048 +                                       DWC_ERROR ("DWC OTG HCD - "
9049 +                                                       "SetPortFeature request %xh "
9050 +                                                       "unknown or unsupported\n", _wValue);
9051 +                                       break;
9052 +                       }
9053 +                       break;
9054 +               default:
9055 +error:
9056 +                       retval = -EINVAL;
9057 +                       DWC_WARN ("DWC OTG HCD - "
9058 +                                       "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n", 
9059 +                                       _typeReq, _wIndex, _wValue);
9060 +                       break;
9061 +       }
9062 +
9063 +       return retval;
9064 +}
9065 +
9066 +
9067 +/**
9068 + * Assigns transactions from a QTD to a free host channel and initializes the
9069 + * host channel to perform the transactions. The host channel is removed from
9070 + * the free list.
9071 + *
9072 + * @param _hcd The HCD state structure.
9073 + * @param _qh Transactions from the first QTD for this QH are selected and
9074 + * assigned to a free host channel.
9075 + */
9076 +static void assign_and_init_hc(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
9077 +{
9078 +       dwc_hc_t        *hc;
9079 +       dwc_otg_qtd_t   *qtd;
9080 +       struct urb      *urb;
9081 +
9082 +       DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p)\n", __func__, _hcd, _qh);
9083 +
9084 +       hc = list_entry(_hcd->free_hc_list.next, dwc_hc_t, hc_list_entry);
9085 +
9086 +       /* Remove the host channel from the free list. */
9087 +       list_del_init(&hc->hc_list_entry);
9088 +
9089 +       qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry);
9090 +       urb = qtd->urb;
9091 +       _qh->channel = hc;
9092 +       _qh->qtd_in_process = qtd;
9093 +
9094 +       /*
9095 +        * Use usb_pipedevice to determine device address. This address is
9096 +        * 0 before the SET_ADDRESS command and the correct address afterward.
9097 +        */
9098 +       hc->dev_addr = usb_pipedevice(urb->pipe);
9099 +       hc->ep_num = usb_pipeendpoint(urb->pipe);
9100 +
9101 +       if (urb->dev->speed == USB_SPEED_LOW) {
9102 +               hc->speed = DWC_OTG_EP_SPEED_LOW;
9103 +       } else if (urb->dev->speed == USB_SPEED_FULL) {
9104 +               hc->speed = DWC_OTG_EP_SPEED_FULL;
9105 +       } else {
9106 +               hc->speed = DWC_OTG_EP_SPEED_HIGH;
9107 +       }
9108 +       hc->max_packet = dwc_max_packet(_qh->maxp);
9109 +
9110 +       hc->xfer_started = 0;
9111 +       hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
9112 +       hc->error_state = (qtd->error_count > 0);
9113 +       hc->halt_on_queue = 0;
9114 +       hc->halt_pending = 0;
9115 +       hc->requests = 0;
9116 +
9117 +       /*
9118 +        * The following values may be modified in the transfer type section
9119 +        * below. The xfer_len value may be reduced when the transfer is
9120 +        * started to accommodate the max widths of the XferSize and PktCnt
9121 +        * fields in the HCTSIZn register.
9122 +        */
9123 +       hc->do_ping = _qh->ping_state;
9124 +       hc->ep_is_in = (usb_pipein(urb->pipe) != 0);
9125 +       hc->data_pid_start = _qh->data_toggle;
9126 +       hc->multi_count = 1;
9127 +
9128 +       if (_hcd->core_if->dma_enable) {
9129 +               hc->xfer_buff = (uint8_t *)(u32)urb->transfer_dma + urb->actual_length;
9130 +       } else {
9131 +               hc->xfer_buff = (uint8_t *)urb->transfer_buffer + urb->actual_length;
9132 +       }
9133 +       hc->xfer_len = urb->transfer_buffer_length - urb->actual_length;
9134 +       hc->xfer_count = 0;
9135 +
9136 +       /*
9137 +        * Set the split attributes
9138 +        */
9139 +       hc->do_split = 0;
9140 +       if (_qh->do_split) {
9141 +               hc->do_split = 1;
9142 +               hc->xact_pos = qtd->isoc_split_pos;
9143 +               hc->complete_split = qtd->complete_split;
9144 +               hc->hub_addr = urb->dev->tt->hub->devnum;
9145 +               hc->port_addr = urb->dev->ttport;
9146 +       }
9147 +
9148 +       switch (usb_pipetype(urb->pipe)) {
9149 +               case PIPE_CONTROL:
9150 +                       hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
9151 +                       switch (qtd->control_phase) {
9152 +                               case DWC_OTG_CONTROL_SETUP:
9153 +                                       DWC_DEBUGPL(DBG_HCDV, "  Control setup transaction\n");
9154 +                                       hc->do_ping = 0;
9155 +                                       hc->ep_is_in = 0;
9156 +                                       hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
9157 +                                       if (_hcd->core_if->dma_enable) {
9158 +                                               hc->xfer_buff = (uint8_t *)(u32)urb->setup_dma;
9159 +                                       } else {
9160 +                                               hc->xfer_buff = (uint8_t *)urb->setup_packet;
9161 +                                       }
9162 +                                       hc->xfer_len = 8;
9163 +                                       break;
9164 +                               case DWC_OTG_CONTROL_DATA:
9165 +                                       DWC_DEBUGPL(DBG_HCDV, "  Control data transaction\n");
9166 +                                       hc->data_pid_start = qtd->data_toggle;
9167 +                                       break;
9168 +                               case DWC_OTG_CONTROL_STATUS:
9169 +                                       /*
9170 +                                        * Direction is opposite of data direction or IN if no
9171 +                                        * data.
9172 +                                        */
9173 +                                       DWC_DEBUGPL(DBG_HCDV, "  Control status transaction\n");
9174 +                                       if (urb->transfer_buffer_length == 0) {
9175 +                                               hc->ep_is_in = 1;
9176 +                                       } else {
9177 +                                               hc->ep_is_in = (usb_pipein(urb->pipe) != USB_DIR_IN);
9178 +                                       }
9179 +                                       if (hc->ep_is_in) {
9180 +                                               hc->do_ping = 0;
9181 +                                       }
9182 +                                       hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
9183 +                                       hc->xfer_len = 0;
9184 +                                       if (_hcd->core_if->dma_enable) {
9185 +                                               hc->xfer_buff = (uint8_t *)_hcd->status_buf_dma;
9186 +                                       } else {
9187 +                                               hc->xfer_buff = (uint8_t *)_hcd->status_buf;
9188 +                                       }
9189 +                                       break;
9190 +                       }
9191 +                       break;
9192 +               case PIPE_BULK:
9193 +                       hc->ep_type = DWC_OTG_EP_TYPE_BULK;
9194 +                       break;
9195 +               case PIPE_INTERRUPT:
9196 +                       hc->ep_type = DWC_OTG_EP_TYPE_INTR;
9197 +                       break;
9198 +               case PIPE_ISOCHRONOUS:
9199 +                       {
9200 +                               struct usb_iso_packet_descriptor *frame_desc;
9201 +                               frame_desc = &urb->iso_frame_desc[qtd->isoc_frame_index];
9202 +                               hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
9203 +                               if (_hcd->core_if->dma_enable) {
9204 +                                       hc->xfer_buff = (uint8_t *)(u32)urb->transfer_dma;
9205 +                               } else {
9206 +                                       hc->xfer_buff = (uint8_t *)urb->transfer_buffer;
9207 +                               }
9208 +                               hc->xfer_buff += frame_desc->offset + qtd->isoc_split_offset;
9209 +                               hc->xfer_len = frame_desc->length - qtd->isoc_split_offset;
9210 +
9211 +                               if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
9212 +                                       if (hc->xfer_len <= 188) {
9213 +                                               hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
9214 +                                       }
9215 +                                       else {
9216 +                                               hc->xact_pos = DWC_HCSPLIT_XACTPOS_BEGIN;
9217 +                                       }
9218 +                               }
9219 +                       }
9220 +                       break;
9221 +       }
9222 +
9223 +       if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
9224 +                       hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
9225 +               /*
9226 +                * This value may be modified when the transfer is started to
9227 +                * reflect the actual transfer length.
9228 +                */
9229 +               hc->multi_count = dwc_hb_mult(_qh->maxp);
9230 +       }
9231 +
9232 +       dwc_otg_hc_init(_hcd->core_if, hc);
9233 +       hc->qh = _qh;
9234 +}
9235 +#define DEBUG_HOST_CHANNELS
9236 +#ifdef DEBUG_HOST_CHANNELS
9237 +static int last_sel_trans_num_per_scheduled = 0;
9238 +module_param(last_sel_trans_num_per_scheduled, int, 0444);
9239 +
9240 +static int last_sel_trans_num_nonper_scheduled = 0;
9241 +module_param(last_sel_trans_num_nonper_scheduled, int, 0444);
9242 +
9243 +static int last_sel_trans_num_avail_hc_at_start = 0;
9244 +module_param(last_sel_trans_num_avail_hc_at_start, int, 0444);
9245 +
9246 +static int last_sel_trans_num_avail_hc_at_end = 0;
9247 +module_param(last_sel_trans_num_avail_hc_at_end, int, 0444);
9248 +#endif /* DEBUG_HOST_CHANNELS */
9249 +
9250 +/**
9251 + * This function selects transactions from the HCD transfer schedule and
9252 + * assigns them to available host channels. It is called from HCD interrupt
9253 + * handler functions.
9254 + *
9255 + * @param _hcd The HCD state structure.
9256 + *
9257 + * @return The types of new transactions that were assigned to host channels.
9258 + */
9259 +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t *_hcd)
9260 +{
9261 +       struct list_head                *qh_ptr;
9262 +       dwc_otg_qh_t                    *qh;
9263 +       int                             num_channels;
9264 +       unsigned long flags;
9265 +       dwc_otg_transaction_type_e      ret_val = DWC_OTG_TRANSACTION_NONE;
9266 +
9267 +#ifdef DEBUG_SOF
9268 +       DWC_DEBUGPL(DBG_HCD, "  Select Transactions\n");
9269 +#endif /*  */
9270 +
9271 +#ifdef DEBUG_HOST_CHANNELS
9272 +       last_sel_trans_num_per_scheduled = 0;
9273 +       last_sel_trans_num_nonper_scheduled = 0;
9274 +       last_sel_trans_num_avail_hc_at_start = _hcd->available_host_channels;
9275 +#endif /* DEBUG_HOST_CHANNELS */
9276 +
9277 +       /* Process entries in the periodic ready list. */
9278 +       num_channels = _hcd->core_if->core_params->host_channels;
9279 +       qh_ptr = _hcd->periodic_sched_ready.next;
9280 +       while (qh_ptr != &_hcd->periodic_sched_ready
9281 +                       && !list_empty(&_hcd->free_hc_list)) {
9282 +
9283 +               // Make sure we leave one channel for non periodic transactions.
9284 +               local_irq_save(flags);
9285 +               if (_hcd->available_host_channels <= 1) {
9286 +                       local_irq_restore(flags);
9287 +                       break;
9288 +               }
9289 +               _hcd->available_host_channels--;
9290 +               local_irq_restore(flags);
9291 +#ifdef DEBUG_HOST_CHANNELS
9292 +               last_sel_trans_num_per_scheduled++;
9293 +#endif /* DEBUG_HOST_CHANNELS */
9294 +
9295 +               qh = list_entry(qh_ptr, dwc_otg_qh_t, qh_list_entry);
9296 +               assign_and_init_hc(_hcd, qh);
9297 +
9298 +               /*
9299 +                * Move the QH from the periodic ready schedule to the
9300 +                * periodic assigned schedule.
9301 +                */
9302 +               qh_ptr = qh_ptr->next;
9303 +               local_irq_save(flags);
9304 +               list_move(&qh->qh_list_entry, &_hcd->periodic_sched_assigned);
9305 +               local_irq_restore(flags);
9306 +               ret_val = DWC_OTG_TRANSACTION_PERIODIC;
9307 +       }
9308 +
9309 +       /*
9310 +        * Process entries in the deferred portion of the non-periodic list.
9311 +        * A NAK put them here and, at the right time, they need to be
9312 +        * placed on the sched_inactive list.
9313 +        */
9314 +       qh_ptr = _hcd->non_periodic_sched_deferred.next;
9315 +       while (qh_ptr != &_hcd->non_periodic_sched_deferred) {
9316 +               uint16_t frame_number =
9317 +                       dwc_otg_hcd_get_frame_number(dwc_otg_hcd_to_hcd(_hcd));
9318 +               qh = list_entry(qh_ptr, dwc_otg_qh_t, qh_list_entry);
9319 +               qh_ptr = qh_ptr->next;
9320 +
9321 +               if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
9322 +                       // NAK did this
9323 +                       /*
9324 +                        * Move the QH from the non periodic deferred schedule to
9325 +                        * the non periodic inactive schedule.
9326 +                        */
9327 +                       local_irq_save(flags);
9328 +                       list_move(&qh->qh_list_entry,
9329 +                                       &_hcd->non_periodic_sched_inactive);
9330 +                       local_irq_restore(flags);
9331 +               }
9332 +       }
9333 +
9334 +       /*
9335 +        * Process entries in the inactive portion of the non-periodic
9336 +        * schedule. Some free host channels may not be used if they are
9337 +        * reserved for periodic transfers.
9338 +        */
9339 +       qh_ptr = _hcd->non_periodic_sched_inactive.next;
9340 +       num_channels = _hcd->core_if->core_params->host_channels;
9341 +       while (qh_ptr != &_hcd->non_periodic_sched_inactive
9342 +                       && !list_empty(&_hcd->free_hc_list)) {
9343 +
9344 +               local_irq_save(flags);
9345 +               if (_hcd->available_host_channels < 1) {
9346 +                       local_irq_restore(flags);
9347 +                       break;
9348 +               }
9349 +               _hcd->available_host_channels--;
9350 +               local_irq_restore(flags);
9351 +#ifdef DEBUG_HOST_CHANNELS
9352 +               last_sel_trans_num_nonper_scheduled++;
9353 +#endif /* DEBUG_HOST_CHANNELS */
9354 +
9355 +               qh = list_entry(qh_ptr, dwc_otg_qh_t, qh_list_entry);
9356 +               assign_and_init_hc(_hcd, qh);
9357 +
9358 +               /*
9359 +                * Move the QH from the non-periodic inactive schedule to the
9360 +                * non-periodic active schedule.
9361 +                */
9362 +               qh_ptr = qh_ptr->next;
9363 +               local_irq_save(flags);
9364 +               list_move(&qh->qh_list_entry, &_hcd->non_periodic_sched_active);
9365 +               local_irq_restore(flags);
9366 +
9367 +               if (ret_val == DWC_OTG_TRANSACTION_NONE) {
9368 +                       ret_val = DWC_OTG_TRANSACTION_NON_PERIODIC;
9369 +               } else {
9370 +                       ret_val = DWC_OTG_TRANSACTION_ALL;
9371 +               }
9372 +
9373 +       }
9374 +#ifdef DEBUG_HOST_CHANNELS
9375 +       last_sel_trans_num_avail_hc_at_end = _hcd->available_host_channels;
9376 +#endif /* DEBUG_HOST_CHANNELS */
9377 +
9378 +       return ret_val;
9379 +}
9380 +
9381 +/**
9382 + * Attempts to queue a single transaction request for a host channel
9383 + * associated with either a periodic or non-periodic transfer. This function
9384 + * assumes that there is space available in the appropriate request queue. For
9385 + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
9386 + * is available in the appropriate Tx FIFO.
9387 + *
9388 + * @param _hcd The HCD state structure.
9389 + * @param _hc Host channel descriptor associated with either a periodic or
9390 + * non-periodic transfer.
9391 + * @param _fifo_dwords_avail Number of DWORDs available in the periodic Tx
9392 + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
9393 + * transfers.
9394 + *
9395 + * @return 1 if a request is queued and more requests may be needed to
9396 + * complete the transfer, 0 if no more requests are required for this
9397 + * transfer, -1 if there is insufficient space in the Tx FIFO.
9398 + */
9399 +static int queue_transaction(dwc_otg_hcd_t *_hcd,
9400 +               dwc_hc_t *_hc,
9401 +               uint16_t _fifo_dwords_avail)
9402 +{
9403 +       int retval;
9404 +
9405 +       if (_hcd->core_if->dma_enable) {
9406 +               if (!_hc->xfer_started) {
9407 +                       dwc_otg_hc_start_transfer(_hcd->core_if, _hc);
9408 +                       _hc->qh->ping_state = 0;
9409 +               }
9410 +               retval = 0;
9411 +       } else  if (_hc->halt_pending) {
9412 +               /* Don't queue a request if the channel has been halted. */
9413 +               retval = 0;
9414 +       } else if (_hc->halt_on_queue) {
9415 +               dwc_otg_hc_halt(_hcd->core_if, _hc, _hc->halt_status);
9416 +               retval = 0;
9417 +       } else if (_hc->do_ping) {
9418 +               if (!_hc->xfer_started) {
9419 +                       dwc_otg_hc_start_transfer(_hcd->core_if, _hc);
9420 +               }
9421 +               retval = 0;
9422 +       } else if (!_hc->ep_is_in ||
9423 +                       _hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
9424 +               if ((_fifo_dwords_avail * 4) >= _hc->max_packet) {
9425 +                       if (!_hc->xfer_started) {
9426 +                               dwc_otg_hc_start_transfer(_hcd->core_if, _hc);
9427 +                               retval = 1;
9428 +                       } else {
9429 +                               retval = dwc_otg_hc_continue_transfer(_hcd->core_if, _hc);
9430 +                       }
9431 +               } else {
9432 +                       retval = -1;
9433 +               }
9434 +       } else {                
9435 +               if (!_hc->xfer_started) {
9436 +                       dwc_otg_hc_start_transfer(_hcd->core_if, _hc);
9437 +                       retval = 1;
9438 +               } else {
9439 +                       retval = dwc_otg_hc_continue_transfer(_hcd->core_if, _hc);
9440 +               }
9441 +       }
9442 +
9443 +       return retval;
9444 +}
9445 +
9446 +/**
9447 + * Processes active non-periodic channels and queues transactions for these
9448 + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
9449 + * FIFO Empty interrupt is enabled if there are more transactions to queue as
9450 + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
9451 + * FIFO Empty interrupt is disabled.
9452 + */
9453 +static void process_non_periodic_channels(dwc_otg_hcd_t *_hcd)
9454 +{
9455 +       gnptxsts_data_t         tx_status;
9456 +       struct list_head        *orig_qh_ptr;
9457 +       dwc_otg_qh_t            *qh;
9458 +       int                     status;
9459 +       int                     no_queue_space = 0;
9460 +       int                     no_fifo_space = 0;
9461 +       int                     more_to_do = 0;
9462 +
9463 +       dwc_otg_core_global_regs_t *global_regs = _hcd->core_if->core_global_regs;
9464 +
9465 +       DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
9466 +#ifdef DEBUG   
9467 +       tx_status.d32 = dwc_read_reg32(&global_regs->gnptxsts);
9468 +       DWC_DEBUGPL(DBG_HCDV, "  NP Tx Req Queue Space Avail (before queue): %d\n",
9469 +                       tx_status.b.nptxqspcavail);
9470 +       DWC_DEBUGPL(DBG_HCDV, "  NP Tx FIFO Space Avail (before queue): %d\n",
9471 +                       tx_status.b.nptxfspcavail);
9472 +#endif
9473 +       /*
9474 +        * Keep track of the starting point. Skip over the start-of-list
9475 +        * entry.
9476 +        */
9477 +       if (_hcd->non_periodic_qh_ptr == &_hcd->non_periodic_sched_active) {
9478 +               _hcd->non_periodic_qh_ptr = _hcd->non_periodic_qh_ptr->next;
9479 +       }
9480 +       orig_qh_ptr = _hcd->non_periodic_qh_ptr;
9481 +
9482 +       /*
9483 +        * Process once through the active list or until no more space is
9484 +        * available in the request queue or the Tx FIFO.
9485 +        */
9486 +       do {
9487 +               tx_status.d32 = dwc_read_reg32(&global_regs->gnptxsts);
9488 +               if (!_hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
9489 +                       no_queue_space = 1;
9490 +                       break;
9491 +               }
9492 +
9493 +               qh = list_entry(_hcd->non_periodic_qh_ptr, dwc_otg_qh_t, qh_list_entry);
9494 +               status = queue_transaction(_hcd, qh->channel, tx_status.b.nptxfspcavail);
9495 +
9496 +               if (status > 0) {
9497 +                       more_to_do = 1;
9498 +               } else if (status < 0) {
9499 +                       no_fifo_space = 1;
9500 +                       break;
9501 +               }
9502 +
9503 +               /* Advance to next QH, skipping start-of-list entry. */
9504 +               _hcd->non_periodic_qh_ptr = _hcd->non_periodic_qh_ptr->next;
9505 +               if (_hcd->non_periodic_qh_ptr == &_hcd->non_periodic_sched_active) {
9506 +                       _hcd->non_periodic_qh_ptr = _hcd->non_periodic_qh_ptr->next;
9507 +               }
9508 +
9509 +       } while (_hcd->non_periodic_qh_ptr != orig_qh_ptr);
9510 +
9511 +       if (!_hcd->core_if->dma_enable) {
9512 +               gintmsk_data_t intr_mask = {.d32 = 0};
9513 +               intr_mask.b.nptxfempty = 1;
9514 +
9515 +#ifdef DEBUG   
9516 +               tx_status.d32 = dwc_read_reg32(&global_regs->gnptxsts);
9517 +               DWC_DEBUGPL(DBG_HCDV, "  NP Tx Req Queue Space Avail (after queue): %d\n",
9518 +                               tx_status.b.nptxqspcavail);
9519 +               DWC_DEBUGPL(DBG_HCDV, "  NP Tx FIFO Space Avail (after queue): %d\n",
9520 +                               tx_status.b.nptxfspcavail);
9521 +#endif
9522 +               if (more_to_do || no_queue_space || no_fifo_space) {
9523 +                       /*
9524 +                        * May need to queue more transactions as the request
9525 +                        * queue or Tx FIFO empties. Enable the non-periodic
9526 +                        * Tx FIFO empty interrupt. (Always use the half-empty
9527 +                        * level to ensure that new requests are loaded as
9528 +                        * soon as possible.)
9529 +                        */
9530 +                       dwc_modify_reg32(&global_regs->gintmsk, 0, intr_mask.d32);
9531 +               } else {
9532 +                       /*
9533 +                        * Disable the Tx FIFO empty interrupt since there are
9534 +                        * no more transactions that need to be queued right
9535 +                        * now. This function is called from interrupt
9536 +                        * handlers to queue more transactions as transfer
9537 +                        * states change.
9538 +                        */
9539 +                       dwc_modify_reg32(&global_regs->gintmsk, intr_mask.d32, 0);
9540 +               }
9541 +       }
9542 +}
9543 +
9544 +/**
9545 + * Processes periodic channels for the next frame and queues transactions for
9546 + * these channels to the DWC_otg controller. After queueing transactions, the
9547 + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
9548 + * to queue as Periodic Tx FIFO or request queue space becomes available.
9549 + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
9550 + */
9551 +static void process_periodic_channels(dwc_otg_hcd_t *_hcd)
9552 +{
9553 +       hptxsts_data_t          tx_status;
9554 +       struct list_head        *qh_ptr;
9555 +       dwc_otg_qh_t            *qh;
9556 +       int                     status;
9557 +       int                     no_queue_space = 0;
9558 +       int                     no_fifo_space = 0;
9559 +
9560 +       dwc_otg_host_global_regs_t *host_regs;
9561 +       host_regs = _hcd->core_if->host_if->host_global_regs;
9562 +
9563 +       DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
9564 +#ifdef DEBUG   
9565 +       tx_status.d32 = dwc_read_reg32(&host_regs->hptxsts);
9566 +       DWC_DEBUGPL(DBG_HCDV, "  P Tx Req Queue Space Avail (before queue): %d\n",
9567 +                       tx_status.b.ptxqspcavail);
9568 +       DWC_DEBUGPL(DBG_HCDV, "  P Tx FIFO Space Avail (before queue): %d\n",
9569 +                       tx_status.b.ptxfspcavail);
9570 +#endif
9571 +
9572 +       qh_ptr = _hcd->periodic_sched_assigned.next;
9573 +       while (qh_ptr != &_hcd->periodic_sched_assigned) {
9574 +               tx_status.d32 = dwc_read_reg32(&host_regs->hptxsts);
9575 +               if (tx_status.b.ptxqspcavail == 0) {
9576 +                       no_queue_space = 1;
9577 +                       break;
9578 +               }
9579 +
9580 +               qh = list_entry(qh_ptr, dwc_otg_qh_t, qh_list_entry);
9581 +
9582 +               /*
9583 +                * Set a flag if we're queuing high-bandwidth in slave mode.
9584 +                * The flag prevents any halts to get into the request queue in
9585 +                * the middle of multiple high-bandwidth packets getting queued.
9586 +                */
9587 +               if ((!_hcd->core_if->dma_enable) && 
9588 +                               (qh->channel->multi_count > 1)) 
9589 +               {
9590 +                       _hcd->core_if->queuing_high_bandwidth = 1;
9591 +               }
9592 +
9593 +               status = queue_transaction(_hcd, qh->channel, tx_status.b.ptxfspcavail);
9594 +               if (status < 0) {
9595 +                       no_fifo_space = 1;
9596 +                       break;
9597 +               }
9598 +
9599 +               /*
9600 +                * In Slave mode, stay on the current transfer until there is
9601 +                * nothing more to do or the high-bandwidth request count is
9602 +                * reached. In DMA mode, only need to queue one request. The
9603 +                * controller automatically handles multiple packets for
9604 +                * high-bandwidth transfers.
9605 +                */
9606 +               if (_hcd->core_if->dma_enable ||
9607 +                               (status == 0 ||
9608 +                                qh->channel->requests == qh->channel->multi_count)) {
9609 +                       qh_ptr = qh_ptr->next;
9610 +                       /*
9611 +                        * Move the QH from the periodic assigned schedule to
9612 +                        * the periodic queued schedule.
9613 +                        */
9614 +                       list_move(&qh->qh_list_entry, &_hcd->periodic_sched_queued);
9615 +
9616 +                       /* done queuing high bandwidth */
9617 +                       _hcd->core_if->queuing_high_bandwidth = 0;
9618 +               }
9619 +       }
9620 +
9621 +       if (!_hcd->core_if->dma_enable) {
9622 +               dwc_otg_core_global_regs_t *global_regs;
9623 +               gintmsk_data_t intr_mask = {.d32 = 0};
9624 +
9625 +               global_regs = _hcd->core_if->core_global_regs;
9626 +               intr_mask.b.ptxfempty = 1;
9627 +#ifdef DEBUG   
9628 +               tx_status.d32 = dwc_read_reg32(&host_regs->hptxsts);
9629 +               DWC_DEBUGPL(DBG_HCDV, "  P Tx Req Queue Space Avail (after queue): %d\n",
9630 +                               tx_status.b.ptxqspcavail);
9631 +               DWC_DEBUGPL(DBG_HCDV, "  P Tx FIFO Space Avail (after queue): %d\n",
9632 +                               tx_status.b.ptxfspcavail);
9633 +#endif
9634 +               if (!(list_empty(&_hcd->periodic_sched_assigned)) ||
9635 +                               no_queue_space || no_fifo_space) {
9636 +                       /*
9637 +                        * May need to queue more transactions as the request
9638 +                        * queue or Tx FIFO empties. Enable the periodic Tx
9639 +                        * FIFO empty interrupt. (Always use the half-empty
9640 +                        * level to ensure that new requests are loaded as
9641 +                        * soon as possible.)
9642 +                        */
9643 +                       dwc_modify_reg32(&global_regs->gintmsk, 0, intr_mask.d32);
9644 +               } else {
9645 +                       /*
9646 +                        * Disable the Tx FIFO empty interrupt since there are
9647 +                        * no more transactions that need to be queued right
9648 +                        * now. This function is called from interrupt
9649 +                        * handlers to queue more transactions as transfer
9650 +                        * states change.
9651 +                        */
9652 +                       dwc_modify_reg32(&global_regs->gintmsk, intr_mask.d32, 0);
9653 +               }
9654 +       }               
9655 +}
9656 +
9657 +/**
9658 + * This function processes the currently active host channels and queues
9659 + * transactions for these channels to the DWC_otg controller. It is called
9660 + * from HCD interrupt handler functions.
9661 + *
9662 + * @param _hcd The HCD state structure.
9663 + * @param _tr_type The type(s) of transactions to queue (non-periodic,
9664 + * periodic, or both).
9665 + */
9666 +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t *_hcd,
9667 +               dwc_otg_transaction_type_e _tr_type)
9668 +{
9669 +#ifdef DEBUG_SOF
9670 +       DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
9671 +#endif
9672 +       /* Process host channels associated with periodic transfers. */
9673 +       if ((_tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
9674 +                               _tr_type == DWC_OTG_TRANSACTION_ALL) &&
9675 +                       !list_empty(&_hcd->periodic_sched_assigned)) {
9676 +
9677 +               process_periodic_channels(_hcd);
9678 +       }
9679 +
9680 +       /* Process host channels associated with non-periodic transfers. */
9681 +       if ((_tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
9682 +                               _tr_type == DWC_OTG_TRANSACTION_ALL)) {
9683 +               if (!list_empty(&_hcd->non_periodic_sched_active)) {
9684 +                       process_non_periodic_channels(_hcd);
9685 +               } else {
9686 +                       /*
9687 +                        * Ensure NP Tx FIFO empty interrupt is disabled when
9688 +                        * there are no non-periodic transfers to process.
9689 +                        */
9690 +                       gintmsk_data_t gintmsk = {.d32 = 0};
9691 +                       gintmsk.b.nptxfempty = 1;
9692 +                       dwc_modify_reg32(&_hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
9693 +               }
9694 +       }
9695 +}
9696 +
9697 +/**
9698 + * Sets the final status of an URB and returns it to the device driver. Any
9699 + * required cleanup of the URB is performed.
9700 + */
9701 +void dwc_otg_hcd_complete_urb(dwc_otg_hcd_t * _hcd, struct urb *_urb,
9702 +               int _status)
9703 +       __releases(_hcd->lock)
9704 +__acquires(_hcd->lock)
9705 +{
9706 +#ifdef DEBUG
9707 +       if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
9708 +               DWC_PRINT("%s: urb %p, device %d, ep %d %s, status=%d\n",
9709 +                               __func__, _urb, usb_pipedevice(_urb->pipe),
9710 +                               usb_pipeendpoint(_urb->pipe),
9711 +                               usb_pipein(_urb->pipe) ? "IN" : "OUT", _status);
9712 +               if (usb_pipetype(_urb->pipe) == PIPE_ISOCHRONOUS) {
9713 +                       int i;
9714 +                       for (i = 0; i < _urb->number_of_packets; i++) {
9715 +                               DWC_PRINT("  ISO Desc %d status: %d\n",
9716 +                                               i, _urb->iso_frame_desc[i].status);
9717 +                       }
9718 +               }
9719 +       }
9720 +#endif
9721 +
9722 +       _urb->status = _status;
9723 +       _urb->hcpriv = NULL;
9724 +       usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(_hcd), _urb);
9725 +       spin_unlock(&_hcd->lock);
9726 +       usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(_hcd), _urb, _status);
9727 +       spin_lock(&_hcd->lock);
9728 +}
9729 +
9730 +/*
9731 + * Returns the Queue Head for an URB.
9732 + */
9733 +dwc_otg_qh_t *dwc_urb_to_qh(struct urb *_urb)
9734 +{
9735 +       struct usb_host_endpoint *ep = dwc_urb_to_endpoint(_urb);
9736 +       return (dwc_otg_qh_t *)ep->hcpriv;
9737 +}
9738 +
9739 +#ifdef DEBUG
9740 +void dwc_print_setup_data (uint8_t *setup)
9741 +{
9742 +       int i;
9743 +       if (CHK_DEBUG_LEVEL(DBG_HCD)){
9744 +               DWC_PRINT("Setup Data = MSB ");
9745 +               for (i=7; i>=0; i--) DWC_PRINT ("%02x ", setup[i]);
9746 +               DWC_PRINT("\n");
9747 +               DWC_PRINT("  bmRequestType Tranfer = %s\n", (setup[0]&0x80) ? "Device-to-Host" : "Host-to-Device");
9748 +               DWC_PRINT("  bmRequestType Type = ");
9749 +               switch ((setup[0]&0x60) >> 5) {
9750 +                       case 0: DWC_PRINT("Standard\n"); break;
9751 +                       case 1: DWC_PRINT("Class\n"); break;
9752 +                       case 2: DWC_PRINT("Vendor\n"); break;
9753 +                       case 3: DWC_PRINT("Reserved\n"); break;
9754 +               }
9755 +               DWC_PRINT("  bmRequestType Recipient = ");
9756 +               switch (setup[0]&0x1f) {
9757 +                       case 0: DWC_PRINT("Device\n"); break;
9758 +                       case 1: DWC_PRINT("Interface\n"); break;
9759 +                       case 2: DWC_PRINT("Endpoint\n"); break;
9760 +                       case 3: DWC_PRINT("Other\n"); break;
9761 +                       default: DWC_PRINT("Reserved\n"); break;
9762 +               }
9763 +               DWC_PRINT("  bRequest = 0x%0x\n", setup[1]);
9764 +               DWC_PRINT("  wValue = 0x%0x\n", *((uint16_t *)&setup[2]));
9765 +               DWC_PRINT("  wIndex = 0x%0x\n", *((uint16_t *)&setup[4]));
9766 +               DWC_PRINT("  wLength = 0x%0x\n\n", *((uint16_t *)&setup[6]));
9767 +       }
9768 +}
9769 +#endif
9770 +
9771 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t *_hcd) {
9772 +#ifdef DEBUG
9773 +#if 0
9774 +       DWC_PRINT("Frame remaining at SOF:\n");
9775 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9776 +                       _hcd->frrem_samples, _hcd->frrem_accum,
9777 +                       (_hcd->frrem_samples > 0) ?
9778 +                       _hcd->frrem_accum/_hcd->frrem_samples : 0);
9779 +
9780 +       DWC_PRINT("\n");
9781 +       DWC_PRINT("Frame remaining at start_transfer (uframe 7):\n");
9782 +       DWC_PRINT("  samples %u, accum %u, avg %u\n",
9783 +                       _hcd->core_if->hfnum_7_samples, _hcd->core_if->hfnum_7_frrem_accum,
9784 +                       (_hcd->core_if->hfnum_7_samples > 0) ?
9785 +                       _hcd->core_if->hfnum_7_frrem_accum/_hcd->core_if->hfnum_7_samples : 0);
9786 +       DWC_PRINT("Frame remaining at start_transfer (uframe 0):\n");
9787 +       DWC_PRINT("  samples %u, accum %u, avg %u\n",
9788 +                       _hcd->core_if->hfnum_0_samples, _hcd->core_if->hfnum_0_frrem_accum,
9789 +                       (_hcd->core_if->hfnum_0_samples > 0) ?
9790 +                       _hcd->core_if->hfnum_0_frrem_accum/_hcd->core_if->hfnum_0_samples : 0);
9791 +       DWC_PRINT("Frame remaining at start_transfer (uframe 1-6):\n");
9792 +       DWC_PRINT("  samples %u, accum %u, avg %u\n",
9793 +                       _hcd->core_if->hfnum_other_samples, _hcd->core_if->hfnum_other_frrem_accum,
9794 +                       (_hcd->core_if->hfnum_other_samples > 0) ?
9795 +                       _hcd->core_if->hfnum_other_frrem_accum/_hcd->core_if->hfnum_other_samples : 0);
9796 +
9797 +       DWC_PRINT("\n");
9798 +       DWC_PRINT("Frame remaining at sample point A (uframe 7):\n");
9799 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9800 +                       _hcd->hfnum_7_samples_a, _hcd->hfnum_7_frrem_accum_a,
9801 +                       (_hcd->hfnum_7_samples_a > 0) ?
9802 +                       _hcd->hfnum_7_frrem_accum_a/_hcd->hfnum_7_samples_a : 0);
9803 +       DWC_PRINT("Frame remaining at sample point A (uframe 0):\n");
9804 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9805 +                       _hcd->hfnum_0_samples_a, _hcd->hfnum_0_frrem_accum_a,
9806 +                       (_hcd->hfnum_0_samples_a > 0) ?
9807 +                       _hcd->hfnum_0_frrem_accum_a/_hcd->hfnum_0_samples_a : 0);
9808 +       DWC_PRINT("Frame remaining at sample point A (uframe 1-6):\n");
9809 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9810 +                       _hcd->hfnum_other_samples_a, _hcd->hfnum_other_frrem_accum_a,
9811 +                       (_hcd->hfnum_other_samples_a > 0) ?
9812 +                       _hcd->hfnum_other_frrem_accum_a/_hcd->hfnum_other_samples_a : 0);
9813 +
9814 +       DWC_PRINT("\n");
9815 +       DWC_PRINT("Frame remaining at sample point B (uframe 7):\n");
9816 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9817 +                       _hcd->hfnum_7_samples_b, _hcd->hfnum_7_frrem_accum_b,
9818 +                       (_hcd->hfnum_7_samples_b > 0) ?
9819 +                       _hcd->hfnum_7_frrem_accum_b/_hcd->hfnum_7_samples_b : 0);
9820 +       DWC_PRINT("Frame remaining at sample point B (uframe 0):\n");
9821 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9822 +                       _hcd->hfnum_0_samples_b, _hcd->hfnum_0_frrem_accum_b,
9823 +                       (_hcd->hfnum_0_samples_b > 0) ?
9824 +                       _hcd->hfnum_0_frrem_accum_b/_hcd->hfnum_0_samples_b : 0);
9825 +       DWC_PRINT("Frame remaining at sample point B (uframe 1-6):\n");
9826 +       DWC_PRINT("  samples %u, accum %llu, avg %llu\n",
9827 +                       _hcd->hfnum_other_samples_b, _hcd->hfnum_other_frrem_accum_b,
9828 +                       (_hcd->hfnum_other_samples_b > 0) ?
9829 +                       _hcd->hfnum_other_frrem_accum_b/_hcd->hfnum_other_samples_b : 0);
9830 +#endif
9831 +#endif 
9832 +}
9833 +
9834 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t *_hcd)
9835 +{
9836 +#ifdef DEBUG
9837 +       int num_channels;
9838 +       int i;
9839 +       gnptxsts_data_t np_tx_status;
9840 +       hptxsts_data_t p_tx_status;
9841 +
9842 +       num_channels = _hcd->core_if->core_params->host_channels;
9843 +       DWC_PRINT("\n");
9844 +       DWC_PRINT("************************************************************\n");
9845 +       DWC_PRINT("HCD State:\n");
9846 +       DWC_PRINT("  Num channels: %d\n", num_channels);
9847 +       for (i = 0; i < num_channels; i++) {
9848 +               dwc_hc_t *hc = _hcd->hc_ptr_array[i];
9849 +               DWC_PRINT("  Channel %d:\n", i);
9850 +               DWC_PRINT("    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
9851 +                               hc->dev_addr, hc->ep_num, hc->ep_is_in);
9852 +               DWC_PRINT("    speed: %d\n", hc->speed);
9853 +               DWC_PRINT("    ep_type: %d\n", hc->ep_type);
9854 +               DWC_PRINT("    max_packet: %d\n", hc->max_packet);
9855 +               DWC_PRINT("    data_pid_start: %d\n", hc->data_pid_start);
9856 +               DWC_PRINT("    multi_count: %d\n", hc->multi_count);
9857 +               DWC_PRINT("    xfer_started: %d\n", hc->xfer_started);
9858 +               DWC_PRINT("    xfer_buff: %p\n", hc->xfer_buff);
9859 +               DWC_PRINT("    xfer_len: %d\n", hc->xfer_len);
9860 +               DWC_PRINT("    xfer_count: %d\n", hc->xfer_count);
9861 +               DWC_PRINT("    halt_on_queue: %d\n", hc->halt_on_queue);
9862 +               DWC_PRINT("    halt_pending: %d\n", hc->halt_pending);
9863 +               DWC_PRINT("    halt_status: %d\n", hc->halt_status);
9864 +               DWC_PRINT("    do_split: %d\n", hc->do_split);
9865 +               DWC_PRINT("    complete_split: %d\n", hc->complete_split);
9866 +               DWC_PRINT("    hub_addr: %d\n", hc->hub_addr);
9867 +               DWC_PRINT("    port_addr: %d\n", hc->port_addr);
9868 +               DWC_PRINT("    xact_pos: %d\n", hc->xact_pos);
9869 +               DWC_PRINT("    requests: %d\n", hc->requests);
9870 +               DWC_PRINT("    qh: %p\n", hc->qh);
9871 +               if (hc->xfer_started) {
9872 +                       hfnum_data_t hfnum;
9873 +                       hcchar_data_t hcchar;
9874 +                       hctsiz_data_t hctsiz;
9875 +                       hcint_data_t hcint;
9876 +                       hcintmsk_data_t hcintmsk;
9877 +                       hfnum.d32 = dwc_read_reg32(&_hcd->core_if->host_if->host_global_regs->hfnum);
9878 +                       hcchar.d32 = dwc_read_reg32(&_hcd->core_if->host_if->hc_regs[i]->hcchar);
9879 +                       hctsiz.d32 = dwc_read_reg32(&_hcd->core_if->host_if->hc_regs[i]->hctsiz);
9880 +                       hcint.d32 = dwc_read_reg32(&_hcd->core_if->host_if->hc_regs[i]->hcint);
9881 +                       hcintmsk.d32 = dwc_read_reg32(&_hcd->core_if->host_if->hc_regs[i]->hcintmsk);
9882 +                       DWC_PRINT("    hfnum: 0x%08x\n", hfnum.d32);
9883 +                       DWC_PRINT("    hcchar: 0x%08x\n", hcchar.d32);
9884 +                       DWC_PRINT("    hctsiz: 0x%08x\n", hctsiz.d32);
9885 +                       DWC_PRINT("    hcint: 0x%08x\n", hcint.d32);
9886 +                       DWC_PRINT("    hcintmsk: 0x%08x\n", hcintmsk.d32);
9887 +               }
9888 +               if (hc->xfer_started && (hc->qh != NULL) && (hc->qh->qtd_in_process != NULL)) {
9889 +                       dwc_otg_qtd_t *qtd;
9890 +                       struct urb *urb;
9891 +                       qtd = hc->qh->qtd_in_process;
9892 +                       urb = qtd->urb;
9893 +                       DWC_PRINT("    URB Info:\n");
9894 +                       DWC_PRINT("      qtd: %p, urb: %p\n", qtd, urb);
9895 +                       if (urb != NULL) {
9896 +                               DWC_PRINT("      Dev: %d, EP: %d %s\n",
9897 +                                               usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe),
9898 +                                               usb_pipein(urb->pipe) ? "IN" : "OUT");
9899 +                               DWC_PRINT("      Max packet size: %d\n",
9900 +                                               usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
9901 +                               DWC_PRINT("      transfer_buffer: %p\n", urb->transfer_buffer);
9902 +                               DWC_PRINT("      transfer_dma: %p\n", (void *)urb->transfer_dma);
9903 +                               DWC_PRINT("      transfer_buffer_length: %d\n", urb->transfer_buffer_length);
9904 +                               DWC_PRINT("      actual_length: %d\n", urb->actual_length);
9905 +                       }
9906 +               }
9907 +       }
9908 +       //DWC_PRINT("  non_periodic_channels: %d\n", _hcd->non_periodic_channels);
9909 +       //DWC_PRINT("  periodic_channels: %d\n", _hcd->periodic_channels);
9910 +       DWC_PRINT("  available_channels: %d\n", _hcd->available_host_channels);
9911 +       DWC_PRINT("  periodic_usecs: %d\n", _hcd->periodic_usecs);
9912 +       np_tx_status.d32 = dwc_read_reg32(&_hcd->core_if->core_global_regs->gnptxsts);
9913 +       DWC_PRINT("  NP Tx Req Queue Space Avail: %d\n", np_tx_status.b.nptxqspcavail);
9914 +       DWC_PRINT("  NP Tx FIFO Space Avail: %d\n", np_tx_status.b.nptxfspcavail);
9915 +       p_tx_status.d32 = dwc_read_reg32(&_hcd->core_if->host_if->host_global_regs->hptxsts);
9916 +       DWC_PRINT("  P Tx Req Queue Space Avail: %d\n", p_tx_status.b.ptxqspcavail);
9917 +       DWC_PRINT("  P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
9918 +       dwc_otg_hcd_dump_frrem(_hcd);
9919 +       dwc_otg_dump_global_registers(_hcd->core_if);
9920 +       dwc_otg_dump_host_registers(_hcd->core_if);
9921 +       DWC_PRINT("************************************************************\n");
9922 +       DWC_PRINT("\n");
9923 +#endif
9924 +}
9925 +#endif /* DWC_DEVICE_ONLY */
9926 --- /dev/null
9927 +++ b/drivers/usb/dwc_otg/dwc_otg_hcd.h
9928 @@ -0,0 +1,676 @@
9929 +/* ==========================================================================
9930 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_hcd.h $
9931 + * $Revision: 1.1.1.1 $
9932 + * $Date: 2009-04-17 06:15:34 $
9933 + * $Change: 537387 $
9934 + *
9935 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
9936 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
9937 + * otherwise expressly agreed to in writing between Synopsys and you.
9938 + * 
9939 + * The Software IS NOT an item of Licensed Software or Licensed Product under
9940 + * any End User Software License Agreement or Agreement for Licensed Product
9941 + * with Synopsys or any supplement thereto. You are permitted to use and
9942 + * redistribute this Software in source and binary forms, with or without
9943 + * modification, provided that redistributions of source code must retain this
9944 + * notice. You may not view, use, disclose, copy or distribute this file or
9945 + * any information contained herein except pursuant to this license grant from
9946 + * Synopsys. If you do not agree with this notice, including the disclaimer
9947 + * below, then you are not authorized to use the Software.
9948 + * 
9949 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
9950 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9951 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9952 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
9953 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
9954 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
9955 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
9956 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9957 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9958 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
9959 + * DAMAGE.
9960 + * ========================================================================== */
9961 +#ifndef DWC_DEVICE_ONLY
9962 +#if !defined(__DWC_HCD_H__)
9963 +#define __DWC_HCD_H__
9964 +
9965 +#include <linux/list.h>
9966 +#include <linux/usb.h>
9967 +#include <linux/usb/hcd.h>
9968 +
9969 +struct lm_device;
9970 +struct dwc_otg_device;
9971 +
9972 +#include "dwc_otg_cil.h"
9973 +//#include "dwc_otg_ifx.h" // winder
9974 +
9975 +
9976 +/**
9977 + * @file
9978 + *
9979 + * This file contains the structures, constants, and interfaces for
9980 + * the Host Contoller Driver (HCD).
9981 + *
9982 + * The Host Controller Driver (HCD) is responsible for translating requests
9983 + * from the USB Driver into the appropriate actions on the DWC_otg controller.
9984 + * It isolates the USBD from the specifics of the controller by providing an
9985 + * API to the USBD.
9986 + */
9987 +
9988 +/**
9989 + * Phases for control transfers.
9990 + */
9991 +typedef enum dwc_otg_control_phase {
9992 +       DWC_OTG_CONTROL_SETUP,
9993 +       DWC_OTG_CONTROL_DATA,
9994 +       DWC_OTG_CONTROL_STATUS
9995 +} dwc_otg_control_phase_e;
9996 +
9997 +/** Transaction types. */
9998 +typedef enum dwc_otg_transaction_type {
9999 +       DWC_OTG_TRANSACTION_NONE,
10000 +       DWC_OTG_TRANSACTION_PERIODIC,
10001 +       DWC_OTG_TRANSACTION_NON_PERIODIC,
10002 +       DWC_OTG_TRANSACTION_ALL
10003 +} dwc_otg_transaction_type_e;
10004 +
10005 +/**
10006 + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
10007 + * interrupt, or isochronous transfer. A single QTD is created for each URB
10008 + * (of one of these types) submitted to the HCD. The transfer associated with
10009 + * a QTD may require one or multiple transactions.
10010 + *
10011 + * A QTD is linked to a Queue Head, which is entered in either the
10012 + * non-periodic or periodic schedule for execution. When a QTD is chosen for
10013 + * execution, some or all of its transactions may be executed. After
10014 + * execution, the state of the QTD is updated. The QTD may be retired if all
10015 + * its transactions are complete or if an error occurred. Otherwise, it
10016 + * remains in the schedule so more transactions can be executed later.
10017 + */
10018 +struct dwc_otg_qh;
10019 +typedef struct dwc_otg_qtd {
10020 +       /**
10021 +        * Determines the PID of the next data packet for the data phase of
10022 +        * control transfers. Ignored for other transfer types.<br>
10023 +        * One of the following values:
10024 +        *      - DWC_OTG_HC_PID_DATA0
10025 +        *      - DWC_OTG_HC_PID_DATA1
10026 +        */
10027 +       uint8_t                 data_toggle;
10028 +
10029 +       /** Current phase for control transfers (Setup, Data, or Status). */
10030 +       dwc_otg_control_phase_e control_phase;
10031 +
10032 +       /** Keep track of the current split type
10033 +        * for FS/LS endpoints on a HS Hub */
10034 +       uint8_t                 complete_split;
10035 +
10036 +       /** How many bytes transferred during SSPLIT OUT */
10037 +       uint32_t                ssplit_out_xfer_count;
10038 +
10039 +       /**
10040 +        * Holds the number of bus errors that have occurred for a transaction
10041 +        * within this transfer.
10042 +        */
10043 +       uint8_t                 error_count;
10044 +
10045 +       /**
10046 +        * Index of the next frame descriptor for an isochronous transfer. A
10047 +        * frame descriptor describes the buffer position and length of the
10048 +        * data to be transferred in the next scheduled (micro)frame of an
10049 +        * isochronous transfer. It also holds status for that transaction.
10050 +        * The frame index starts at 0.
10051 +        */
10052 +       int                     isoc_frame_index;
10053 +
10054 +       /** Position of the ISOC split on full/low speed */
10055 +       uint8_t                 isoc_split_pos;
10056 +
10057 +       /** Position of the ISOC split in the buffer for the current frame */
10058 +       uint16_t                isoc_split_offset;
10059 +
10060 +       /** URB for this transfer */
10061 +       struct urb              *urb;
10062 +
10063 +       /** This list of QTDs */
10064 +       struct list_head        qtd_list_entry;
10065 +
10066 +       /* Field to track the qh pointer */
10067 +       struct dwc_otg_qh *qtd_qh_ptr;
10068 +} dwc_otg_qtd_t;
10069 +
10070 +/**
10071 + * A Queue Head (QH) holds the static characteristics of an endpoint and
10072 + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
10073 + * be entered in either the non-periodic or periodic schedule.
10074 + */
10075 +typedef struct dwc_otg_qh {
10076 +       /**
10077 +        * Endpoint type.
10078 +        * One of the following values:
10079 +        *      - USB_ENDPOINT_XFER_CONTROL
10080 +        *      - USB_ENDPOINT_XFER_ISOC
10081 +        *      - USB_ENDPOINT_XFER_BULK
10082 +        *      - USB_ENDPOINT_XFER_INT
10083 +        */
10084 +       uint8_t                 ep_type;
10085 +       uint8_t                 ep_is_in;
10086 +
10087 +       /** wMaxPacketSize Field of Endpoint Descriptor. */
10088 +       uint16_t                maxp;
10089 +
10090 +       /**
10091 +        * Determines the PID of the next data packet for non-control
10092 +        * transfers. Ignored for control transfers.<br>
10093 +        * One of the following values:
10094 +        *      - DWC_OTG_HC_PID_DATA0
10095 +        *      - DWC_OTG_HC_PID_DATA1
10096 +        */
10097 +       uint8_t                 data_toggle;
10098 +
10099 +       /** Ping state if 1. */
10100 +       uint8_t                 ping_state;
10101 +
10102 +       /**
10103 +        * List of QTDs for this QH.
10104 +        */
10105 +       struct list_head        qtd_list;
10106 +
10107 +       /** Host channel currently processing transfers for this QH. */
10108 +       dwc_hc_t                *channel;
10109 +
10110 +       /** QTD currently assigned to a host channel for this QH. */
10111 +       dwc_otg_qtd_t           *qtd_in_process;
10112 +
10113 +       /** Full/low speed endpoint on high-speed hub requires split. */
10114 +       uint8_t                 do_split;
10115 +
10116 +       /** @name Periodic schedule information */
10117 +       /** @{ */
10118 +
10119 +       /** Bandwidth in microseconds per (micro)frame. */
10120 +       uint8_t                 usecs;
10121 +
10122 +       /** Interval between transfers in (micro)frames. */
10123 +       uint16_t                interval;
10124 +
10125 +       /**
10126 +        * (micro)frame to initialize a periodic transfer. The transfer
10127 +        * executes in the following (micro)frame.
10128 +        */
10129 +       uint16_t                sched_frame;
10130 +
10131 +       /** (micro)frame at which last start split was initialized. */
10132 +       uint16_t                start_split_frame;
10133 +
10134 +       /** @} */
10135 +
10136 +       uint16_t speed;
10137 +       uint16_t frame_usecs[8];
10138 +       /** Entry for QH in either the periodic or non-periodic schedule. */
10139 +       struct list_head        qh_list_entry;
10140 +} dwc_otg_qh_t;
10141 +
10142 +/**
10143 + * This structure holds the state of the HCD, including the non-periodic and
10144 + * periodic schedules.
10145 + */
10146 +typedef struct dwc_otg_hcd {
10147 +       spinlock_t              lock;
10148 +
10149 +       /** DWC OTG Core Interface Layer */
10150 +       dwc_otg_core_if_t       *core_if;
10151 +
10152 +       /** Internal DWC HCD Flags */   
10153 +       volatile union dwc_otg_hcd_internal_flags {
10154 +               uint32_t d32;
10155 +               struct {
10156 +                       unsigned port_connect_status_change : 1;
10157 +                       unsigned port_connect_status : 1;
10158 +                       unsigned port_reset_change : 1;
10159 +                       unsigned port_enable_change : 1;
10160 +                       unsigned port_suspend_change : 1;
10161 +                       unsigned port_over_current_change : 1;
10162 +                       unsigned reserved : 27;
10163 +               } b;
10164 +       } flags;
10165 +
10166 +       /**
10167 +        * Inactive items in the non-periodic schedule. This is a list of
10168 +        * Queue Heads. Transfers associated with these Queue Heads are not
10169 +        * currently assigned to a host channel.
10170 +        */
10171 +       struct list_head        non_periodic_sched_inactive;
10172 +
10173 +       /**
10174 +        * Deferred items in the non-periodic schedule. This is a list of
10175 +        * Queue Heads. Transfers associated with these Queue Heads are not
10176 +        * currently assigned to a host channel.
10177 +        * When we get an NAK, the QH goes here.
10178 +        */
10179 +       struct list_head        non_periodic_sched_deferred;
10180 +
10181 +       /**
10182 +        * Active items in the non-periodic schedule. This is a list of
10183 +        * Queue Heads. Transfers associated with these Queue Heads are
10184 +        * currently assigned to a host channel.
10185 +        */
10186 +       struct list_head        non_periodic_sched_active;
10187 +
10188 +       /**
10189 +        * Pointer to the next Queue Head to process in the active
10190 +        * non-periodic schedule.
10191 +        */
10192 +       struct list_head        *non_periodic_qh_ptr;
10193 +
10194 +       /**
10195 +        * Inactive items in the periodic schedule. This is a list of QHs for
10196 +        * periodic transfers that are _not_ scheduled for the next frame.
10197 +        * Each QH in the list has an interval counter that determines when it
10198 +        * needs to be scheduled for execution. This scheduling mechanism
10199 +        * allows only a simple calculation for periodic bandwidth used (i.e.
10200 +        * must assume that all periodic transfers may need to execute in the
10201 +        * same frame). However, it greatly simplifies scheduling and should
10202 +        * be sufficient for the vast majority of OTG hosts, which need to
10203 +        * connect to a small number of peripherals at one time.
10204 +        *
10205 +        * Items move from this list to periodic_sched_ready when the QH
10206 +        * interval counter is 0 at SOF.
10207 +        */
10208 +       struct list_head        periodic_sched_inactive;
10209 +
10210 +       /**
10211 +        * List of periodic QHs that are ready for execution in the next
10212 +        * frame, but have not yet been assigned to host channels.
10213 +        *
10214 +        * Items move from this list to periodic_sched_assigned as host
10215 +        * channels become available during the current frame.
10216 +        */
10217 +       struct list_head        periodic_sched_ready;
10218 +
10219 +       /**
10220 +        * List of periodic QHs to be executed in the next frame that are
10221 +        * assigned to host channels.
10222 +        *
10223 +        * Items move from this list to periodic_sched_queued as the
10224 +        * transactions for the QH are queued to the DWC_otg controller.
10225 +        */
10226 +       struct list_head        periodic_sched_assigned;
10227 +
10228 +       /**
10229 +        * List of periodic QHs that have been queued for execution.
10230 +        *
10231 +        * Items move from this list to either periodic_sched_inactive or
10232 +        * periodic_sched_ready when the channel associated with the transfer
10233 +        * is released. If the interval for the QH is 1, the item moves to
10234 +        * periodic_sched_ready because it must be rescheduled for the next
10235 +        * frame. Otherwise, the item moves to periodic_sched_inactive.
10236 +        */
10237 +       struct list_head        periodic_sched_queued;
10238 +
10239 +       /**
10240 +        * Total bandwidth claimed so far for periodic transfers. This value
10241 +        * is in microseconds per (micro)frame. The assumption is that all
10242 +        * periodic transfers may occur in the same (micro)frame.
10243 +        */
10244 +       uint16_t                periodic_usecs;
10245 +
10246 +       /**
10247 +        * Total bandwidth claimed so far for all periodic transfers
10248 +        * in a frame.
10249 +        * This will include a mixture of HS and FS transfers.
10250 +        * Units are microseconds per (micro)frame.
10251 +        * We have a budget per frame and have to schedule
10252 +        * transactions accordingly.
10253 +        * Watch out for the fact that things are actually scheduled for the
10254 +        * "next frame".
10255 +        */
10256 +       uint16_t                frame_usecs[8];
10257 +
10258 +       /**
10259 +        * Frame number read from the core at SOF. The value ranges from 0 to
10260 +        * DWC_HFNUM_MAX_FRNUM.
10261 +        */
10262 +       uint16_t                frame_number;
10263 +
10264 +       /**
10265 +        * Free host channels in the controller. This is a list of
10266 +        * dwc_hc_t items.
10267 +        */
10268 +       struct list_head        free_hc_list;
10269 +
10270 +       /**
10271 +        * Number of available host channels.
10272 +        */
10273 +       int                     available_host_channels;
10274 +
10275 +       /**
10276 +        * Array of pointers to the host channel descriptors. Allows accessing
10277 +        * a host channel descriptor given the host channel number. This is
10278 +        * useful in interrupt handlers.
10279 +        */
10280 +       dwc_hc_t                *hc_ptr_array[MAX_EPS_CHANNELS];
10281 +
10282 +       /**
10283 +        * Buffer to use for any data received during the status phase of a
10284 +        * control transfer. Normally no data is transferred during the status
10285 +        * phase. This buffer is used as a bit bucket. 
10286 +        */
10287 +       uint8_t                 *status_buf;
10288 +
10289 +       /**
10290 +        * DMA address for status_buf.
10291 +        */
10292 +       dma_addr_t              status_buf_dma;
10293 +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64 
10294 +
10295 +       /**
10296 +        * Structure to allow starting the HCD in a non-interrupt context
10297 +        * during an OTG role change.
10298 +        */
10299 +       struct work_struct      start_work;
10300 +       struct usb_hcd          *_p;
10301 +
10302 +       /**
10303 +        * Connection timer. An OTG host must display a message if the device
10304 +        * does not connect. Started when the VBus power is turned on via
10305 +        * sysfs attribute "buspower".
10306 +        */
10307 +        struct timer_list      conn_timer;
10308 +
10309 +       /* Tasket to do a reset */
10310 +       struct tasklet_struct   *reset_tasklet;
10311 +
10312 +#ifdef DEBUG
10313 +       uint32_t                frrem_samples;
10314 +       uint64_t                frrem_accum;
10315 +
10316 +       uint32_t                hfnum_7_samples_a;
10317 +       uint64_t                hfnum_7_frrem_accum_a;
10318 +       uint32_t                hfnum_0_samples_a;
10319 +       uint64_t                hfnum_0_frrem_accum_a;
10320 +       uint32_t                hfnum_other_samples_a;
10321 +       uint64_t                hfnum_other_frrem_accum_a;
10322 +
10323 +       uint32_t                hfnum_7_samples_b;
10324 +       uint64_t                hfnum_7_frrem_accum_b;
10325 +       uint32_t                hfnum_0_samples_b;
10326 +       uint64_t                hfnum_0_frrem_accum_b;
10327 +       uint32_t                hfnum_other_samples_b;
10328 +       uint64_t                hfnum_other_frrem_accum_b;
10329 +#endif
10330 +
10331 +} dwc_otg_hcd_t;
10332 +
10333 +/** Gets the dwc_otg_hcd from a struct usb_hcd */
10334 +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
10335 +{
10336 +       return (dwc_otg_hcd_t *)(hcd->hcd_priv);
10337 +}
10338 +
10339 +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
10340 +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t *dwc_otg_hcd)
10341 +{
10342 +       return container_of((void *)dwc_otg_hcd, struct usb_hcd, hcd_priv);
10343 +}
10344 +
10345 +/** @name HCD Create/Destroy Functions */
10346 +/** @{ */
10347 +extern int  __devinit dwc_otg_hcd_init(struct device *_dev, dwc_otg_device_t * dwc_otg_device);
10348 +extern void dwc_otg_hcd_remove(struct device *_dev);
10349 +/** @} */
10350 +
10351 +/** @name Linux HC Driver API Functions */
10352 +/** @{ */
10353 +
10354 +extern int dwc_otg_hcd_start(struct usb_hcd *hcd);
10355 +extern void dwc_otg_hcd_stop(struct usb_hcd *hcd);
10356 +extern int dwc_otg_hcd_get_frame_number(struct usb_hcd *hcd);
10357 +extern void dwc_otg_hcd_free(struct usb_hcd *hcd);
10358 +
10359 +extern int dwc_otg_hcd_urb_enqueue(struct usb_hcd *hcd, 
10360 +                                  struct urb *urb, 
10361 +                                  gfp_t mem_flags);
10362 +extern int dwc_otg_hcd_urb_dequeue(struct usb_hcd *hcd, 
10363 +                                  struct urb *urb,
10364 +                                  int status);
10365 +extern irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
10366 +
10367 +extern void dwc_otg_hcd_endpoint_disable(struct usb_hcd *hcd,
10368 +                                        struct usb_host_endpoint *ep);
10369 +
10370 +extern int dwc_otg_hcd_hub_status_data(struct usb_hcd *hcd, 
10371 +                                      char *buf);
10372 +extern int dwc_otg_hcd_hub_control(struct usb_hcd *hcd, 
10373 +                                  u16 typeReq, 
10374 +                                  u16 wValue, 
10375 +                                  u16 wIndex, 
10376 +                                  char *buf, 
10377 +                                  u16 wLength);
10378 +
10379 +/** @} */
10380 +
10381 +/** @name Transaction Execution Functions */
10382 +/** @{ */
10383 +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t *_hcd);
10384 +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t *_hcd,
10385 +                                          dwc_otg_transaction_type_e _tr_type);
10386 +extern void dwc_otg_hcd_complete_urb(dwc_otg_hcd_t *_hcd, struct urb *_urb,
10387 +                                    int _status);
10388 +/** @} */
10389 +
10390 +/** @name Interrupt Handler Functions */
10391 +/** @{ */
10392 +extern int32_t dwc_otg_hcd_handle_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10393 +extern int32_t dwc_otg_hcd_handle_sof_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10394 +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10395 +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10396 +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10397 +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *_dwc_otg_hcd);
10398 +extern int32_t dwc_otg_hcd_handle_port_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10399 +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10400 +extern int32_t dwc_otg_hcd_handle_disconnect_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10401 +extern int32_t dwc_otg_hcd_handle_hc_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10402 +extern int32_t dwc_otg_hcd_handle_hc_n_intr (dwc_otg_hcd_t *_dwc_otg_hcd, uint32_t _num);
10403 +extern int32_t dwc_otg_hcd_handle_session_req_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10404 +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
10405 +/** @} */
10406 +
10407 +
10408 +/** @name Schedule Queue Functions */
10409 +/** @{ */
10410 +
10411 +/* Implemented in dwc_otg_hcd_queue.c */
10412 +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create (dwc_otg_hcd_t *_hcd, struct urb *_urb);
10413 +extern void dwc_otg_hcd_qh_init (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, struct urb *_urb);
10414 +extern void dwc_otg_hcd_qh_free (dwc_otg_qh_t *_qh);
10415 +extern int dwc_otg_hcd_qh_add (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh);
10416 +extern void dwc_otg_hcd_qh_remove (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh);
10417 +extern void dwc_otg_hcd_qh_deactivate (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, int sched_csplit);
10418 +extern int dwc_otg_hcd_qh_deferr (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, int delay);
10419 +
10420 +/** Remove and free a QH */
10421 +static inline void dwc_otg_hcd_qh_remove_and_free (dwc_otg_hcd_t *_hcd,
10422 +                                                  dwc_otg_qh_t *_qh)
10423 +{
10424 +       dwc_otg_hcd_qh_remove (_hcd, _qh);
10425 +       dwc_otg_hcd_qh_free (_qh);
10426 +}
10427 +
10428 +/** Allocates memory for a QH structure.
10429 + * @return Returns the memory allocate or NULL on error. */
10430 +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc (void)
10431 +{
10432 +#ifdef _SC_BUILD_ 
10433 +    return (dwc_otg_qh_t *) kmalloc (sizeof(dwc_otg_qh_t), GFP_ATOMIC);
10434 +#else
10435 +       return (dwc_otg_qh_t *) kmalloc (sizeof(dwc_otg_qh_t), GFP_KERNEL);
10436 +#endif 
10437 +}
10438 +
10439 +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create (struct urb *urb);
10440 +extern void dwc_otg_hcd_qtd_init (dwc_otg_qtd_t *qtd, struct urb *urb);
10441 +extern int dwc_otg_hcd_qtd_add (dwc_otg_qtd_t *qtd, dwc_otg_hcd_t *dwc_otg_hcd);
10442 +
10443 +/** Allocates memory for a QTD structure.
10444 + * @return Returns the memory allocate or NULL on error. */
10445 +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc (void)
10446 +{
10447 +#ifdef _SC_BUILD_    
10448 +    return (dwc_otg_qtd_t *) kmalloc (sizeof(dwc_otg_qtd_t), GFP_ATOMIC);
10449 +#else
10450 +       return (dwc_otg_qtd_t *) kmalloc (sizeof(dwc_otg_qtd_t), GFP_KERNEL);
10451 +#endif 
10452 +}
10453 +
10454 +/** Frees the memory for a QTD structure.  QTD should already be removed from
10455 + * list.
10456 + * @param[in] _qtd QTD to free.*/
10457 +static inline void dwc_otg_hcd_qtd_free (dwc_otg_qtd_t *_qtd)
10458 +{
10459 +       kfree (_qtd);
10460 +}
10461 +
10462 +/** Removes a QTD from list.
10463 + * @param[in] _qtd QTD to remove from list. */
10464 +static inline void dwc_otg_hcd_qtd_remove (dwc_otg_qtd_t *_qtd)
10465 +{
10466 +       unsigned long flags;
10467 +       local_irq_save (flags);
10468 +       list_del (&_qtd->qtd_list_entry);
10469 +       local_irq_restore (flags);
10470 +}
10471 +
10472 +/** Remove and free a QTD */
10473 +static inline void dwc_otg_hcd_qtd_remove_and_free (dwc_otg_qtd_t *_qtd)
10474 +{
10475 +       dwc_otg_hcd_qtd_remove (_qtd);
10476 +       dwc_otg_hcd_qtd_free (_qtd);
10477 +}
10478 +
10479 +/** @} */
10480 +
10481 +
10482 +/** @name Internal Functions */
10483 +/** @{ */
10484 +dwc_otg_qh_t *dwc_urb_to_qh(struct urb *_urb);
10485 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t *_hcd);
10486 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t *_hcd);
10487 +/** @} */
10488 +
10489 +
10490 +/** Gets the usb_host_endpoint associated with an URB. */
10491 +static inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *_urb)
10492 +{
10493 +       struct usb_device *dev = _urb->dev;
10494 +       int ep_num = usb_pipeendpoint(_urb->pipe);
10495 +    if (usb_pipein(_urb->pipe))
10496 +        return dev->ep_in[ep_num];
10497 +    else
10498 +        return dev->ep_out[ep_num];
10499 +}
10500 +
10501 +/**
10502 + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
10503 + * qualified with its direction (possible 32 endpoints per device).
10504 + */
10505 +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) \
10506 +       ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
10507 +                                                     ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
10508 +
10509 +/** Gets the QH that contains the list_head */
10510 +#define dwc_list_to_qh(_list_head_ptr_) (container_of(_list_head_ptr_,dwc_otg_qh_t,qh_list_entry))
10511 +
10512 +/** Gets the QTD that contains the list_head */
10513 +#define dwc_list_to_qtd(_list_head_ptr_) (container_of(_list_head_ptr_,dwc_otg_qtd_t,qtd_list_entry))
10514 +
10515 +/** Check if QH is non-periodic  */
10516 +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == USB_ENDPOINT_XFER_BULK) || \
10517 +                                     (_qh_ptr_->ep_type == USB_ENDPOINT_XFER_CONTROL))
10518 +
10519 +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
10520 +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
10521 +
10522 +/** Packet size for any kind of endpoint descriptor */
10523 +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
10524 +
10525 +/**
10526 + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
10527 + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
10528 + * frame number when the max frame number is reached.
10529 + */
10530 +static inline int dwc_frame_num_le(uint16_t _frame1, uint16_t _frame2)
10531 +{
10532 +       return ((_frame2 - _frame1) & DWC_HFNUM_MAX_FRNUM) <=
10533 +               (DWC_HFNUM_MAX_FRNUM >> 1);
10534 +}
10535 +
10536 +/**
10537 + * Returns true if _frame1 is greater than _frame2. The comparison is done
10538 + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
10539 + * number when the max frame number is reached.
10540 + */
10541 +static inline int dwc_frame_num_gt(uint16_t _frame1, uint16_t _frame2)
10542 +{
10543 +       return (_frame1 != _frame2) &&
10544 +               (((_frame1 - _frame2) & DWC_HFNUM_MAX_FRNUM) <
10545 +                (DWC_HFNUM_MAX_FRNUM >> 1));
10546 +}
10547 +
10548 +/**
10549 + * Increments _frame by the amount specified by _inc. The addition is done
10550 + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
10551 + */
10552 +static inline uint16_t dwc_frame_num_inc(uint16_t _frame, uint16_t _inc)
10553 +{
10554 +       return (_frame + _inc) & DWC_HFNUM_MAX_FRNUM;
10555 +}
10556 +
10557 +static inline uint16_t dwc_full_frame_num (uint16_t _frame)
10558 +{
10559 +       return ((_frame) & DWC_HFNUM_MAX_FRNUM) >> 3;
10560 +}
10561 +
10562 +static inline uint16_t dwc_micro_frame_num (uint16_t _frame)
10563 +{
10564 +       return (_frame) & 0x7;
10565 +}
10566 +
10567 +#ifdef DEBUG
10568 +/**
10569 + * Macro to sample the remaining PHY clocks left in the current frame. This
10570 + * may be used during debugging to determine the average time it takes to
10571 + * execute sections of code. There are two possible sample points, "a" and
10572 + * "b", so the _letter argument must be one of these values.
10573 + *
10574 + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
10575 + * example, "cat /sys/devices/lm0/hcd_frrem".
10576 + */
10577 +#define dwc_sample_frrem(_hcd, _qh, _letter) \
10578 +{ \
10579 +       hfnum_data_t hfnum; \
10580 +       dwc_otg_qtd_t *qtd; \
10581 +       qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
10582 +       if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
10583 +               hfnum.d32 = dwc_read_reg32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
10584 +               switch (hfnum.b.frnum & 0x7) { \
10585 +               case 7: \
10586 +                       _hcd->hfnum_7_samples_##_letter++; \
10587 +                       _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
10588 +                       break; \
10589 +               case 0: \
10590 +                       _hcd->hfnum_0_samples_##_letter++; \
10591 +                       _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
10592 +                       break; \
10593 +               default: \
10594 +                       _hcd->hfnum_other_samples_##_letter++; \
10595 +                       _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
10596 +                       break; \
10597 +               } \
10598 +       } \
10599 +}
10600 +#else // DEBUG
10601 +#define dwc_sample_frrem(_hcd, _qh, _letter) 
10602 +#endif // DEBUG                
10603 +#endif // __DWC_HCD_H__
10604 +#endif /* DWC_DEVICE_ONLY */
10605 --- /dev/null
10606 +++ b/drivers/usb/dwc_otg/dwc_otg_hcd_intr.c
10607 @@ -0,0 +1,1841 @@
10608 +/* ==========================================================================
10609 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_hcd_intr.c $
10610 + * $Revision: 1.1.1.1 $
10611 + * $Date: 2009-04-17 06:15:34 $
10612 + * $Change: 553126 $
10613 + *
10614 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
10615 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
10616 + * otherwise expressly agreed to in writing between Synopsys and you.
10617 + * 
10618 + * The Software IS NOT an item of Licensed Software or Licensed Product under
10619 + * any End User Software License Agreement or Agreement for Licensed Product
10620 + * with Synopsys or any supplement thereto. You are permitted to use and
10621 + * redistribute this Software in source and binary forms, with or without
10622 + * modification, provided that redistributions of source code must retain this
10623 + * notice. You may not view, use, disclose, copy or distribute this file or
10624 + * any information contained herein except pursuant to this license grant from
10625 + * Synopsys. If you do not agree with this notice, including the disclaimer
10626 + * below, then you are not authorized to use the Software.
10627 + * 
10628 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
10629 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10630 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10631 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
10632 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
10633 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
10634 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
10635 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
10636 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
10637 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10638 + * DAMAGE.
10639 + * ========================================================================== */
10640 +#ifndef DWC_DEVICE_ONLY
10641 +
10642 +#include "dwc_otg_driver.h"
10643 +#include "dwc_otg_hcd.h"
10644 +#include "dwc_otg_regs.h"
10645 +
10646 +const int erratum_usb09_patched = 0;
10647 +const int deferral_on = 1;
10648 +const int nak_deferral_delay = 8;
10649 +const int nyet_deferral_delay = 1;
10650 +/** @file 
10651 + * This file contains the implementation of the HCD Interrupt handlers. 
10652 + */
10653 +
10654 +/** This function handles interrupts for the HCD. */
10655 +int32_t dwc_otg_hcd_handle_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
10656 +{
10657 +       int retval = 0;
10658 +
10659 +        dwc_otg_core_if_t *core_if = _dwc_otg_hcd->core_if;
10660 +        gintsts_data_t gintsts;
10661 +#ifdef DEBUG
10662 +        dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
10663 +#endif
10664 +
10665 +       /* Check if HOST Mode */
10666 +        if (dwc_otg_is_host_mode(core_if)) {
10667 +               gintsts.d32 = dwc_otg_read_core_intr(core_if);
10668 +               if (!gintsts.d32) {
10669 +                       return 0;
10670 +               }
10671 +
10672 +#ifdef DEBUG
10673 +               /* Don't print debug message in the interrupt handler on SOF */
10674 +#  ifndef DEBUG_SOF
10675 +               if (gintsts.d32 != DWC_SOF_INTR_MASK)
10676 +#  endif
10677 +                       DWC_DEBUGPL (DBG_HCD, "\n");
10678 +#endif
10679 +
10680 +#ifdef DEBUG
10681 +#  ifndef DEBUG_SOF
10682 +               if (gintsts.d32 != DWC_SOF_INTR_MASK)
10683 +#  endif
10684 +                       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n", gintsts.d32);
10685 +#endif
10686 +
10687 +                if (gintsts.b.sofintr) {
10688 +                       retval |= dwc_otg_hcd_handle_sof_intr (_dwc_otg_hcd);
10689 +                }
10690 +                if (gintsts.b.rxstsqlvl) {
10691 +                       retval |= dwc_otg_hcd_handle_rx_status_q_level_intr (_dwc_otg_hcd);
10692 +                }
10693 +                if (gintsts.b.nptxfempty) {
10694 +                       retval |= dwc_otg_hcd_handle_np_tx_fifo_empty_intr (_dwc_otg_hcd);
10695 +               }
10696 +                if (gintsts.b.i2cintr) {
10697 +                       /** @todo Implement i2cintr handler. */
10698 +                }
10699 +               if (gintsts.b.portintr) {
10700 +                       retval |= dwc_otg_hcd_handle_port_intr (_dwc_otg_hcd);
10701 +               }
10702 +               if (gintsts.b.hcintr) {
10703 +                       retval |= dwc_otg_hcd_handle_hc_intr (_dwc_otg_hcd);
10704 +               }
10705 +               if (gintsts.b.ptxfempty) {
10706 +                       retval |= dwc_otg_hcd_handle_perio_tx_fifo_empty_intr (_dwc_otg_hcd);
10707 +               }
10708 +#ifdef DEBUG
10709 +#  ifndef DEBUG_SOF
10710 +               if (gintsts.d32 != DWC_SOF_INTR_MASK)
10711 +#  endif
10712 +               {
10713 +                       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Finished Servicing Interrupts\n");
10714 +                       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
10715 +                                   dwc_read_reg32(&global_regs->gintsts));
10716 +                       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
10717 +                                   dwc_read_reg32(&global_regs->gintmsk));                
10718 +               }
10719 +#endif
10720 +
10721 +#ifdef DEBUG
10722 +#  ifndef DEBUG_SOF
10723 +       if (gintsts.d32 != DWC_SOF_INTR_MASK)
10724 +#  endif
10725 +               DWC_DEBUGPL (DBG_HCD, "\n");
10726 +#endif
10727 +
10728 +       }
10729 +
10730 +       return retval;
10731 +}
10732 +
10733 +#ifdef DWC_TRACK_MISSED_SOFS
10734 +#warning Compiling code to track missed SOFs
10735 +#define FRAME_NUM_ARRAY_SIZE 1000
10736 +/**
10737 + * This function is for debug only.
10738 + */
10739 +static inline void track_missed_sofs(uint16_t _curr_frame_number) {
10740 +       static uint16_t         frame_num_array[FRAME_NUM_ARRAY_SIZE];
10741 +       static uint16_t         last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
10742 +       static int              frame_num_idx = 0;
10743 +       static uint16_t         last_frame_num = DWC_HFNUM_MAX_FRNUM;
10744 +       static int              dumped_frame_num_array = 0;
10745 +       
10746 +       if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
10747 +               if ((((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) != _curr_frame_number)) {
10748 +                       frame_num_array[frame_num_idx] = _curr_frame_number;
10749 +                       last_frame_num_array[frame_num_idx++] = last_frame_num;
10750 +               }
10751 +       } else if (!dumped_frame_num_array) {
10752 +               int i;
10753 +               printk(KERN_EMERG USB_DWC "Frame     Last Frame\n");
10754 +               printk(KERN_EMERG USB_DWC "-----     ----------\n");
10755 +               for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
10756 +                       printk(KERN_EMERG USB_DWC "0x%04x    0x%04x\n",
10757 +                              frame_num_array[i], last_frame_num_array[i]);
10758 +               }
10759 +               dumped_frame_num_array = 1;
10760 +       }
10761 +       last_frame_num = _curr_frame_number;
10762 +}
10763 +#endif 
10764 +
10765 +/**
10766 + * Handles the start-of-frame interrupt in host mode. Non-periodic
10767 + * transactions may be queued to the DWC_otg controller for the current
10768 + * (micro)frame. Periodic transactions may be queued to the controller for the
10769 + * next (micro)frame.
10770 + */
10771 +int32_t dwc_otg_hcd_handle_sof_intr (dwc_otg_hcd_t *_hcd)
10772 +{
10773 +       hfnum_data_t            hfnum;
10774 +       struct list_head        *qh_entry;
10775 +       dwc_otg_qh_t            *qh;
10776 +       dwc_otg_transaction_type_e tr_type;
10777 +       gintsts_data_t gintsts = {.d32 = 0};
10778 +
10779 +       hfnum.d32 = dwc_read_reg32(&_hcd->core_if->host_if->host_global_regs->hfnum);
10780 +
10781 +#ifdef DEBUG_SOF
10782 +       DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
10783 +#endif
10784 +
10785 +       _hcd->frame_number = hfnum.b.frnum;
10786 +
10787 +#ifdef DEBUG
10788 +       _hcd->frrem_accum += hfnum.b.frrem;
10789 +       _hcd->frrem_samples++;
10790 +#endif
10791 +
10792 +#ifdef DWC_TRACK_MISSED_SOFS
10793 +       track_missed_sofs(_hcd->frame_number);
10794 +#endif 
10795 +
10796 +       /* Determine whether any periodic QHs should be executed. */
10797 +       qh_entry = _hcd->periodic_sched_inactive.next;
10798 +       while (qh_entry != &_hcd->periodic_sched_inactive) {
10799 +               qh = list_entry(qh_entry, dwc_otg_qh_t, qh_list_entry);
10800 +               qh_entry = qh_entry->next;
10801 +               if (dwc_frame_num_le(qh->sched_frame, _hcd->frame_number)) {
10802 +                       /* 
10803 +                        * Move QH to the ready list to be executed next
10804 +                        * (micro)frame.
10805 +                        */
10806 +                       list_move(&qh->qh_list_entry, &_hcd->periodic_sched_ready);
10807 +               }
10808 +       }
10809 +
10810 +       tr_type = dwc_otg_hcd_select_transactions(_hcd);
10811 +       if (tr_type != DWC_OTG_TRANSACTION_NONE) {
10812 +               dwc_otg_hcd_queue_transactions(_hcd, tr_type);
10813 +       }
10814 +
10815 +       /* Clear interrupt */
10816 +       gintsts.b.sofintr = 1;
10817 +       dwc_write_reg32(&_hcd->core_if->core_global_regs->gintsts, gintsts.d32);
10818 +
10819 +       return 1;
10820 +}
10821 +
10822 +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
10823 + * least one packet in the Rx FIFO.  The packets are moved from the FIFO to
10824 + * memory if the DWC_otg controller is operating in Slave mode. */
10825 +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
10826 +{
10827 +       host_grxsts_data_t grxsts;
10828 +       dwc_hc_t *hc = NULL;
10829 +
10830 +       DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
10831 +
10832 +       grxsts.d32 = dwc_read_reg32(&_dwc_otg_hcd->core_if->core_global_regs->grxstsp);
10833 +
10834 +       hc = _dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
10835 +
10836 +       /* Packet Status */
10837 +       DWC_DEBUGPL(DBG_HCDV, "    Ch num = %d\n", grxsts.b.chnum);
10838 +       DWC_DEBUGPL(DBG_HCDV, "    Count = %d\n", grxsts.b.bcnt);
10839 +       DWC_DEBUGPL(DBG_HCDV, "    DPID = %d, hc.dpid = %d\n", grxsts.b.dpid, hc->data_pid_start);
10840 +       DWC_DEBUGPL(DBG_HCDV, "    PStatus = %d\n", grxsts.b.pktsts);
10841 +       
10842 +       switch (grxsts.b.pktsts) {
10843 +       case DWC_GRXSTS_PKTSTS_IN:
10844 +               /* Read the data into the host buffer. */
10845 +               if (grxsts.b.bcnt > 0) {
10846 +                       dwc_otg_read_packet(_dwc_otg_hcd->core_if, 
10847 +                                           hc->xfer_buff, 
10848 +                                           grxsts.b.bcnt);
10849 +
10850 +                       /* Update the HC fields for the next packet received. */
10851 +                       hc->xfer_count += grxsts.b.bcnt;
10852 +                       hc->xfer_buff += grxsts.b.bcnt;
10853 +               }
10854 +               
10855 +       case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
10856 +       case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
10857 +       case DWC_GRXSTS_PKTSTS_CH_HALTED:
10858 +               /* Handled in interrupt, just ignore data */
10859 +               break;
10860 +       default:
10861 +               DWC_ERROR ("RX_STS_Q Interrupt: Unknown status %d\n", grxsts.b.pktsts);
10862 +               break;
10863 +       }
10864 +       
10865 +       return 1;
10866 +}
10867 +
10868 +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
10869 + * data packets may be written to the FIFO for OUT transfers. More requests
10870 + * may be written to the non-periodic request queue for IN transfers. This
10871 + * interrupt is enabled only in Slave mode. */
10872 +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
10873 +{
10874 +       DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
10875 +       dwc_otg_hcd_queue_transactions(_dwc_otg_hcd,
10876 +                                      DWC_OTG_TRANSACTION_NON_PERIODIC);
10877 +       return 1;
10878 +}
10879 +
10880 +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
10881 + * packets may be written to the FIFO for OUT transfers. More requests may be
10882 + * written to the periodic request queue for IN transfers. This interrupt is
10883 + * enabled only in Slave mode. */
10884 +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
10885 +{
10886 +       DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");  
10887 +       dwc_otg_hcd_queue_transactions(_dwc_otg_hcd,
10888 +                                      DWC_OTG_TRANSACTION_PERIODIC);
10889 +       return 1;
10890 +}
10891 +
10892 +/** There are multiple conditions that can cause a port interrupt. This function
10893 + * determines which interrupt conditions have occurred and handles them
10894 + * appropriately. */
10895 +int32_t dwc_otg_hcd_handle_port_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
10896 +{
10897 +       int retval = 0;
10898 +       hprt0_data_t hprt0;
10899 +       hprt0_data_t hprt0_modify;
10900 +
10901 +       hprt0.d32 = dwc_read_reg32(_dwc_otg_hcd->core_if->host_if->hprt0);
10902 +       hprt0_modify.d32 = dwc_read_reg32(_dwc_otg_hcd->core_if->host_if->hprt0);
10903 +
10904 +       /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
10905 +        * GINTSTS */
10906 +
10907 +       hprt0_modify.b.prtena = 0;
10908 +       hprt0_modify.b.prtconndet = 0; 
10909 +       hprt0_modify.b.prtenchng = 0;
10910 +       hprt0_modify.b.prtovrcurrchng = 0; 
10911 +
10912 +       /* Port Connect Detected 
10913 +        * Set flag and clear if detected */
10914 +       if (hprt0.b.prtconndet) {
10915 +               DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
10916 +                           "Port Connect Detected--\n", hprt0.d32);
10917 +               _dwc_otg_hcd->flags.b.port_connect_status_change = 1;
10918 +               _dwc_otg_hcd->flags.b.port_connect_status = 1;
10919 +               hprt0_modify.b.prtconndet = 1;
10920 +
10921 +                /* B-Device has connected, Delete the connection timer.  */
10922 +                del_timer( &_dwc_otg_hcd->conn_timer );
10923 +
10924 +               /* The Hub driver asserts a reset when it sees port connect
10925 +                * status change flag */
10926 +               retval |= 1;
10927 +       }
10928 +
10929 +       /* Port Enable Changed
10930 +        * Clear if detected - Set internal flag if disabled */
10931 +       if (hprt0.b.prtenchng) {
10932 +               DWC_DEBUGPL(DBG_HCD, "  --Port Interrupt HPRT0=0x%08x "
10933 +                           "Port Enable Changed--\n", hprt0.d32);
10934 +               hprt0_modify.b.prtenchng = 1;
10935 +               if (hprt0.b.prtena == 1) {
10936 +                       int do_reset = 0;
10937 +                       dwc_otg_core_params_t *params = _dwc_otg_hcd->core_if->core_params;
10938 +                       dwc_otg_core_global_regs_t *global_regs = _dwc_otg_hcd->core_if->core_global_regs;
10939 +                       dwc_otg_host_if_t *host_if = _dwc_otg_hcd->core_if->host_if;
10940 +
10941 +                       /* Check if we need to adjust the PHY clock speed for
10942 +                        * low power and adjust it */
10943 +                       if (params->host_support_fs_ls_low_power)
10944 +                       {
10945 +                               gusbcfg_data_t usbcfg;
10946 +
10947 +                               usbcfg.d32 = dwc_read_reg32 (&global_regs->gusbcfg);
10948 +
10949 +                               if ((hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED) ||
10950 +                                   (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED))
10951 +                               {
10952 +                                       /* 
10953 +                                        * Low power 
10954 +                                        */
10955 +                                       hcfg_data_t hcfg;
10956 +                                       if (usbcfg.b.phylpwrclksel == 0) {
10957 +                                               /* Set PHY low power clock select for FS/LS devices */
10958 +                                               usbcfg.b.phylpwrclksel = 1;
10959 +                                               dwc_write_reg32(&global_regs->gusbcfg, usbcfg.d32);
10960 +                                               do_reset = 1;
10961 +                                       }
10962 +
10963 +                                       hcfg.d32 = dwc_read_reg32(&host_if->host_global_regs->hcfg);
10964 +
10965 +                                       if ((hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED) && 
10966 +                                           (params->host_ls_low_power_phy_clk ==
10967 +                                            DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ))
10968 +                                       {
10969 +                                               /* 6 MHZ */
10970 +                                               DWC_DEBUGPL(DBG_CIL, "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
10971 +                                               if (hcfg.b.fslspclksel != DWC_HCFG_6_MHZ) {
10972 +                                                       hcfg.b.fslspclksel = DWC_HCFG_6_MHZ;
10973 +                                                       dwc_write_reg32(&host_if->host_global_regs->hcfg,
10974 +                                                                       hcfg.d32);
10975 +                                                       do_reset = 1;
10976 +                                               }
10977 +                                       }
10978 +                                       else {
10979 +                                               /* 48 MHZ */
10980 +                                               DWC_DEBUGPL(DBG_CIL, "FS_PHY programming HCFG to 48 MHz ()\n");
10981 +                                               if (hcfg.b.fslspclksel != DWC_HCFG_48_MHZ) {
10982 +                                                       hcfg.b.fslspclksel = DWC_HCFG_48_MHZ;
10983 +                                                       dwc_write_reg32(&host_if->host_global_regs->hcfg,
10984 +                                                                       hcfg.d32);
10985 +                                                       do_reset = 1;
10986 +                                               }
10987 +                                       }
10988 +                               }
10989 +                               else {
10990 +                                       /* 
10991 +                                        * Not low power 
10992 +                                        */
10993 +                                       if (usbcfg.b.phylpwrclksel == 1) {
10994 +                                               usbcfg.b.phylpwrclksel = 0;
10995 +                                               dwc_write_reg32(&global_regs->gusbcfg, usbcfg.d32);
10996 +                                               do_reset = 1;
10997 +                                       }
10998 +                               }
10999 +
11000 +                               if (do_reset) {
11001 +                                       tasklet_schedule(_dwc_otg_hcd->reset_tasklet);
11002 +                               }
11003 +                       }
11004 +                       
11005 +                       if (!do_reset) {
11006 +                               /* Port has been enabled set the reset change flag */
11007 +                               _dwc_otg_hcd->flags.b.port_reset_change = 1;
11008 +                       }
11009 +
11010 +               } else {
11011 +                       _dwc_otg_hcd->flags.b.port_enable_change = 1;
11012 +               }
11013 +               retval |= 1;
11014 +       }
11015 +
11016 +       /** Overcurrent Change Interrupt */
11017 +       if (hprt0.b.prtovrcurrchng) {
11018 +               DWC_DEBUGPL(DBG_HCD, "  --Port Interrupt HPRT0=0x%08x "
11019 +                           "Port Overcurrent Changed--\n", hprt0.d32);
11020 +               _dwc_otg_hcd->flags.b.port_over_current_change = 1;
11021 +               hprt0_modify.b.prtovrcurrchng = 1; 
11022 +               retval |= 1;
11023 +       }
11024 +
11025 +       /* Clear Port Interrupts */
11026 +       dwc_write_reg32(_dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
11027 +
11028 +       return retval;
11029 +}
11030 +
11031 +
11032 +/** This interrupt indicates that one or more host channels has a pending
11033 + * interrupt. There are multiple conditions that can cause each host channel
11034 + * interrupt. This function determines which conditions have occurred for each
11035 + * host channel interrupt and handles them appropriately. */
11036 +int32_t dwc_otg_hcd_handle_hc_intr (dwc_otg_hcd_t *_dwc_otg_hcd)
11037 +{
11038 +       int i;
11039 +       int retval = 0;
11040 +       haint_data_t haint;
11041 +
11042 +       /* Clear appropriate bits in HCINTn to clear the interrupt bit in
11043 +        * GINTSTS */
11044 +
11045 +       haint.d32 = dwc_otg_read_host_all_channels_intr(_dwc_otg_hcd->core_if);
11046 +
11047 +       for (i=0; i<_dwc_otg_hcd->core_if->core_params->host_channels; i++) {
11048 +               if (haint.b2.chint & (1 << i)) {
11049 +                       retval |= dwc_otg_hcd_handle_hc_n_intr (_dwc_otg_hcd, i);
11050 +               }
11051 +       }
11052 +
11053 +       return retval;
11054 +}
11055 +
11056 +/* Macro used to clear one channel interrupt */
11057 +#define clear_hc_int(_hc_regs_,_intr_) \
11058 +do { \
11059 +       hcint_data_t hcint_clear = {.d32 = 0}; \
11060 +       hcint_clear.b._intr_ = 1; \
11061 +       dwc_write_reg32(&((_hc_regs_)->hcint), hcint_clear.d32); \
11062 +} while (0)
11063 +
11064 +/*
11065 + * Macro used to disable one channel interrupt. Channel interrupts are
11066 + * disabled when the channel is halted or released by the interrupt handler.
11067 + * There is no need to handle further interrupts of that type until the
11068 + * channel is re-assigned. In fact, subsequent handling may cause crashes
11069 + * because the channel structures are cleaned up when the channel is released.
11070 + */
11071 +#define disable_hc_int(_hc_regs_,_intr_) \
11072 +do { \
11073 +       hcintmsk_data_t hcintmsk = {.d32 = 0}; \
11074 +       hcintmsk.b._intr_ = 1; \
11075 +       dwc_modify_reg32(&((_hc_regs_)->hcintmsk), hcintmsk.d32, 0); \
11076 +} while (0)
11077 +
11078 +/**
11079 + * Gets the actual length of a transfer after the transfer halts. _halt_status
11080 + * holds the reason for the halt.
11081 + *
11082 + * For IN transfers where _halt_status is DWC_OTG_HC_XFER_COMPLETE, 
11083 + * *_short_read is set to 1 upon return if less than the requested
11084 + * number of bytes were transferred. Otherwise, *_short_read is set to 0 upon
11085 + * return. _short_read may also be NULL on entry, in which case it remains
11086 + * unchanged.
11087 + */
11088 +static uint32_t get_actual_xfer_length(dwc_hc_t *_hc,
11089 +                                      dwc_otg_hc_regs_t *_hc_regs,
11090 +                                      dwc_otg_qtd_t *_qtd,
11091 +                                      dwc_otg_halt_status_e _halt_status,
11092 +                                      int *_short_read)
11093 +{
11094 +       hctsiz_data_t   hctsiz;
11095 +       uint32_t        length;
11096 +
11097 +       if (_short_read != NULL) {
11098 +               *_short_read = 0;
11099 +       }
11100 +       hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
11101 +
11102 +       if (_halt_status == DWC_OTG_HC_XFER_COMPLETE) {
11103 +               if (_hc->ep_is_in) {
11104 +                       length = _hc->xfer_len - hctsiz.b.xfersize;
11105 +                       if (_short_read != NULL) {
11106 +                               *_short_read = (hctsiz.b.xfersize != 0);
11107 +                       }
11108 +               } else if (_hc->qh->do_split) {
11109 +                       length = _qtd->ssplit_out_xfer_count;
11110 +               } else {
11111 +                       length = _hc->xfer_len;
11112 +               }
11113 +       } else {
11114 +               /*
11115 +                * Must use the hctsiz.pktcnt field to determine how much data
11116 +                * has been transferred. This field reflects the number of
11117 +                * packets that have been transferred via the USB. This is
11118 +                * always an integral number of packets if the transfer was
11119 +                * halted before its normal completion. (Can't use the
11120 +                * hctsiz.xfersize field because that reflects the number of
11121 +                * bytes transferred via the AHB, not the USB).
11122 +                */
11123 +               length = (_hc->start_pkt_count - hctsiz.b.pktcnt) * _hc->max_packet;
11124 +       }
11125 +
11126 +       return length;
11127 +}
11128 +
11129 +/**
11130 + * Updates the state of the URB after a Transfer Complete interrupt on the
11131 + * host channel. Updates the actual_length field of the URB based on the
11132 + * number of bytes transferred via the host channel. Sets the URB status
11133 + * if the data transfer is finished. 
11134 + *
11135 + * @return 1 if the data transfer specified by the URB is completely finished,
11136 + * 0 otherwise.
11137 + */
11138 +static int update_urb_state_xfer_comp(dwc_hc_t *_hc,
11139 +                                     dwc_otg_hc_regs_t * _hc_regs, struct urb *_urb,
11140 +                                     dwc_otg_qtd_t * _qtd, int *status)
11141 +{
11142 +       int             xfer_done = 0;
11143 +       int             short_read = 0;
11144 +
11145 +       _urb->actual_length += get_actual_xfer_length(_hc, _hc_regs, _qtd,
11146 +                                                     DWC_OTG_HC_XFER_COMPLETE,
11147 +                                                     &short_read);
11148 +
11149 +       if (short_read || (_urb->actual_length == _urb->transfer_buffer_length)) {
11150 +               xfer_done = 1;
11151 +               if (short_read && (_urb->transfer_flags & URB_SHORT_NOT_OK)) {
11152 +                       *status = -EREMOTEIO;
11153 +               } else {
11154 +                       *status = 0;
11155 +               }
11156 +       }
11157 +
11158 +#ifdef DEBUG
11159 +       {
11160 +               hctsiz_data_t   hctsiz;
11161 +               hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
11162 +               DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
11163 +                           __func__, (_hc->ep_is_in ? "IN" : "OUT"), _hc->hc_num);
11164 +               DWC_DEBUGPL(DBG_HCDV, "  hc->xfer_len %d\n", _hc->xfer_len);
11165 +               DWC_DEBUGPL(DBG_HCDV, "  hctsiz.xfersize %d\n", hctsiz.b.xfersize);
11166 +               DWC_DEBUGPL(DBG_HCDV, "  urb->transfer_buffer_length %d\n",
11167 +                           _urb->transfer_buffer_length);
11168 +               DWC_DEBUGPL(DBG_HCDV, "  urb->actual_length %d\n", _urb->actual_length);
11169 +               DWC_DEBUGPL(DBG_HCDV, "  short_read %d, xfer_done %d\n",
11170 +                           short_read, xfer_done);
11171 +       }
11172 +#endif
11173 +
11174 +       return xfer_done;
11175 +}
11176 +
11177 +/*
11178 + * Save the starting data toggle for the next transfer. The data toggle is
11179 + * saved in the QH for non-control transfers and it's saved in the QTD for
11180 + * control transfers.
11181 + */
11182 +static void save_data_toggle(dwc_hc_t *_hc,
11183 +                            dwc_otg_hc_regs_t *_hc_regs,
11184 +                            dwc_otg_qtd_t *_qtd)
11185 +{
11186 +       hctsiz_data_t hctsiz;
11187 +       hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
11188 +
11189 +       if (_hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
11190 +               dwc_otg_qh_t *qh = _hc->qh;
11191 +               if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
11192 +                       qh->data_toggle = DWC_OTG_HC_PID_DATA0;
11193 +               } else {
11194 +                       qh->data_toggle = DWC_OTG_HC_PID_DATA1;
11195 +               }
11196 +       } else {
11197 +               if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
11198 +                       _qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
11199 +               } else {
11200 +                       _qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
11201 +               }
11202 +       }
11203 +}
11204 +
11205 +/**
11206 + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
11207 + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
11208 + * still linked to the QH, the QH is added to the end of the inactive
11209 + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
11210 + * schedule if no more QTDs are linked to the QH.
11211 + */
11212 +static void deactivate_qh(dwc_otg_hcd_t *_hcd,
11213 +                         dwc_otg_qh_t *_qh,
11214 +                         int free_qtd)
11215 +{
11216 +       int continue_split = 0;
11217 +       dwc_otg_qtd_t *qtd;
11218 +
11219 +       DWC_DEBUGPL(DBG_HCDV, "  %s(%p,%p,%d)\n", __func__, _hcd, _qh, free_qtd);
11220 +
11221 +       qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry);
11222 +
11223 +       if (qtd->complete_split) {
11224 +               continue_split = 1;
11225 +       } 
11226 +       else if ((qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID) ||
11227 +                (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END))
11228 +       {
11229 +               continue_split = 1;
11230 +       }
11231 +
11232 +       if (free_qtd) {
11233 +               /*
11234 +                * Note that this was previously a call to
11235 +                * dwc_otg_hcd_qtd_remove_and_free(qtd), which frees the qtd.
11236 +                * However, that call frees the qtd memory, and we continue in the
11237 +                * interrupt logic to access it many more times, including writing
11238 +                * to it.  With slub debugging on, it is clear that we were writing
11239 +                * to memory we had freed.
11240 +                * Call this instead, and now I have moved the freeing of the memory to
11241 +                * the end of processing this interrupt.
11242 +                */
11243 +               //dwc_otg_hcd_qtd_remove_and_free(qtd);
11244 +               dwc_otg_hcd_qtd_remove(qtd);
11245 +               
11246 +               continue_split = 0;
11247 +       }
11248 +
11249 +       _qh->channel = NULL;
11250 +       _qh->qtd_in_process = NULL;
11251 +       dwc_otg_hcd_qh_deactivate(_hcd, _qh, continue_split);
11252 +}
11253 +
11254 +/**
11255 + * Updates the state of an Isochronous URB when the transfer is stopped for
11256 + * any reason. The fields of the current entry in the frame descriptor array
11257 + * are set based on the transfer state and the input _halt_status. Completes
11258 + * the Isochronous URB if all the URB frames have been completed.
11259 + *
11260 + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
11261 + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
11262 + */
11263 +static dwc_otg_halt_status_e
11264 +update_isoc_urb_state(dwc_otg_hcd_t *_hcd,
11265 +                     dwc_hc_t *_hc,
11266 +                     dwc_otg_hc_regs_t *_hc_regs,
11267 +                     dwc_otg_qtd_t *_qtd,
11268 +                     dwc_otg_halt_status_e _halt_status)
11269 +{
11270 +       struct urb *urb = _qtd->urb;
11271 +       dwc_otg_halt_status_e ret_val = _halt_status;
11272 +       struct usb_iso_packet_descriptor *frame_desc;
11273 +
11274 +       frame_desc = &urb->iso_frame_desc[_qtd->isoc_frame_index];
11275 +       switch (_halt_status) {
11276 +       case DWC_OTG_HC_XFER_COMPLETE:
11277 +               frame_desc->status = 0;
11278 +               frame_desc->actual_length =
11279 +                       get_actual_xfer_length(_hc, _hc_regs, _qtd,
11280 +                                              _halt_status, NULL);
11281 +               break;
11282 +       case DWC_OTG_HC_XFER_FRAME_OVERRUN:
11283 +               urb->error_count++;
11284 +               if (_hc->ep_is_in) {
11285 +                       frame_desc->status = -ENOSR;
11286 +               } else {
11287 +                       frame_desc->status = -ECOMM;
11288 +               }
11289 +               frame_desc->actual_length = 0;
11290 +               break;
11291 +       case DWC_OTG_HC_XFER_BABBLE_ERR:
11292 +               urb->error_count++;
11293 +               frame_desc->status = -EOVERFLOW;
11294 +               /* Don't need to update actual_length in this case. */
11295 +               break;
11296 +       case DWC_OTG_HC_XFER_XACT_ERR:
11297 +               urb->error_count++;
11298 +               frame_desc->status = -EPROTO;
11299 +               frame_desc->actual_length =
11300 +                       get_actual_xfer_length(_hc, _hc_regs, _qtd,
11301 +                                              _halt_status, NULL);
11302 +       default:
11303 +               DWC_ERROR("%s: Unhandled _halt_status (%d)\n", __func__,
11304 +                         _halt_status);
11305 +               BUG();
11306 +               break;
11307 +       }
11308 +
11309 +       if (++_qtd->isoc_frame_index == urb->number_of_packets) {
11310 +               /*
11311 +                * urb->status is not used for isoc transfers. 
11312 +                * The individual frame_desc statuses are used instead.
11313 +                */
11314 +               dwc_otg_hcd_complete_urb(_hcd, urb, 0);
11315 +               ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
11316 +       } else {
11317 +               ret_val = DWC_OTG_HC_XFER_COMPLETE;
11318 +       }
11319 +
11320 +       return ret_val;
11321 +}
11322 +
11323 +/**
11324 + * Releases a host channel for use by other transfers. Attempts to select and
11325 + * queue more transactions since at least one host channel is available.
11326 + *
11327 + * @param _hcd The HCD state structure.
11328 + * @param _hc The host channel to release.
11329 + * @param _qtd The QTD associated with the host channel. This QTD may be freed
11330 + * if the transfer is complete or an error has occurred.
11331 + * @param _halt_status Reason the channel is being released. This status
11332 + * determines the actions taken by this function.
11333 + */
11334 +static void release_channel(dwc_otg_hcd_t *_hcd,
11335 +                           dwc_hc_t *_hc,
11336 +                           dwc_otg_qtd_t *_qtd,
11337 +                           dwc_otg_halt_status_e _halt_status,
11338 +                               int *must_free)
11339 +{
11340 +       dwc_otg_transaction_type_e tr_type;
11341 +       int free_qtd;
11342 +       dwc_otg_qh_t * _qh;
11343 +       int deact = 1;
11344 +       int retry_delay = 1;
11345 +       unsigned long flags;
11346 +
11347 +       DWC_DEBUGPL(DBG_HCDV, "  %s: channel %d, halt_status %d\n", __func__,
11348 +                     _hc->hc_num, _halt_status);
11349 +
11350 +       switch (_halt_status) {
11351 +       case DWC_OTG_HC_XFER_NYET:
11352 +       case DWC_OTG_HC_XFER_NAK:
11353 +               if (_halt_status == DWC_OTG_HC_XFER_NYET) {
11354 +                       retry_delay = nyet_deferral_delay;
11355 +               } else {
11356 +                       retry_delay = nak_deferral_delay;
11357 +               }
11358 +               free_qtd = 0;
11359 +               if (deferral_on && _hc->do_split) {
11360 +                       _qh = _hc->qh;
11361 +                       if (_qh) {
11362 +                               deact = dwc_otg_hcd_qh_deferr(_hcd, _qh , retry_delay);
11363 +                       }
11364 +               }
11365 +               break;
11366 +       case DWC_OTG_HC_XFER_URB_COMPLETE:
11367 +               free_qtd = 1;
11368 +               break;
11369 +       case DWC_OTG_HC_XFER_AHB_ERR:
11370 +       case DWC_OTG_HC_XFER_STALL:
11371 +       case DWC_OTG_HC_XFER_BABBLE_ERR:
11372 +               free_qtd = 1;
11373 +               break;
11374 +       case DWC_OTG_HC_XFER_XACT_ERR:
11375 +               if (_qtd->error_count >= 3) {
11376 +                       DWC_DEBUGPL(DBG_HCDV, "  Complete URB with transaction error\n");
11377 +                       free_qtd = 1;
11378 +                       //_qtd->urb->status = -EPROTO;
11379 +                       dwc_otg_hcd_complete_urb(_hcd, _qtd->urb, -EPROTO);
11380 +               } else {
11381 +                       free_qtd = 0;
11382 +               }
11383 +               break;
11384 +       case DWC_OTG_HC_XFER_URB_DEQUEUE:
11385 +               /*
11386 +                * The QTD has already been removed and the QH has been
11387 +                * deactivated. Don't want to do anything except release the
11388 +                * host channel and try to queue more transfers.
11389 +                */
11390 +               goto cleanup;
11391 +       case DWC_OTG_HC_XFER_NO_HALT_STATUS:
11392 +               DWC_ERROR("%s: No halt_status, channel %d\n", __func__, _hc->hc_num);
11393 +               free_qtd = 0;
11394 +               break;
11395 +       default:
11396 +               free_qtd = 0;
11397 +               break;
11398 +       }
11399 +       if (free_qtd) {
11400 +               /* Only change must_free to true (do not set to zero here -- it is
11401 +                * pre-initialized to zero).
11402 +                */
11403 +               *must_free = 1;
11404 +       }
11405 +       if (deact) {
11406 +       deactivate_qh(_hcd, _hc->qh, free_qtd);
11407 +       }
11408 + cleanup:
11409 +       /*
11410 +        * Release the host channel for use by other transfers. The cleanup
11411 +        * function clears the channel interrupt enables and conditions, so
11412 +        * there's no need to clear the Channel Halted interrupt separately.
11413 +        */
11414 +       dwc_otg_hc_cleanup(_hcd->core_if, _hc);
11415 +       list_add_tail(&_hc->hc_list_entry, &_hcd->free_hc_list);
11416 +
11417 +       local_irq_save(flags);
11418 +       _hcd->available_host_channels++;
11419 +       local_irq_restore(flags);
11420 +       /* Try to queue more transfers now that there's a free channel, */
11421 +       /* unless erratum_usb09_patched is set */
11422 +       if (!erratum_usb09_patched) {
11423 +       tr_type = dwc_otg_hcd_select_transactions(_hcd);
11424 +       if (tr_type != DWC_OTG_TRANSACTION_NONE) {
11425 +               dwc_otg_hcd_queue_transactions(_hcd, tr_type);
11426 +               }
11427 +       }
11428 +}
11429 +
11430 +/**
11431 + * Halts a host channel. If the channel cannot be halted immediately because
11432 + * the request queue is full, this function ensures that the FIFO empty
11433 + * interrupt for the appropriate queue is enabled so that the halt request can
11434 + * be queued when there is space in the request queue.
11435 + *
11436 + * This function may also be called in DMA mode. In that case, the channel is
11437 + * simply released since the core always halts the channel automatically in
11438 + * DMA mode.
11439 + */
11440 +static void halt_channel(dwc_otg_hcd_t *_hcd,
11441 +                        dwc_hc_t *_hc,
11442 +                        dwc_otg_qtd_t *_qtd,
11443 +                        dwc_otg_halt_status_e _halt_status, int *must_free)
11444 +{
11445 +       if (_hcd->core_if->dma_enable) {
11446 +               release_channel(_hcd, _hc, _qtd, _halt_status, must_free);
11447 +               return;
11448 +       }
11449 +
11450 +       /* Slave mode processing... */
11451 +       dwc_otg_hc_halt(_hcd->core_if, _hc, _halt_status);
11452 +
11453 +       if (_hc->halt_on_queue) {
11454 +               gintmsk_data_t gintmsk = {.d32 = 0};
11455 +               dwc_otg_core_global_regs_t *global_regs;
11456 +               global_regs = _hcd->core_if->core_global_regs;
11457 +
11458 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
11459 +                   _hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
11460 +                       /*
11461 +                        * Make sure the Non-periodic Tx FIFO empty interrupt
11462 +                        * is enabled so that the non-periodic schedule will
11463 +                        * be processed.
11464 +                        */
11465 +                       gintmsk.b.nptxfempty = 1;
11466 +                       dwc_modify_reg32(&global_regs->gintmsk, 0, gintmsk.d32);
11467 +               } else {
11468 +                       /*
11469 +                        * Move the QH from the periodic queued schedule to
11470 +                        * the periodic assigned schedule. This allows the
11471 +                        * halt to be queued when the periodic schedule is
11472 +                        * processed.
11473 +                        */
11474 +                       list_move(&_hc->qh->qh_list_entry,
11475 +                                 &_hcd->periodic_sched_assigned);
11476 +
11477 +                       /*
11478 +                        * Make sure the Periodic Tx FIFO Empty interrupt is
11479 +                        * enabled so that the periodic schedule will be
11480 +                        * processed.
11481 +                        */
11482 +                       gintmsk.b.ptxfempty = 1;
11483 +                       dwc_modify_reg32(&global_regs->gintmsk, 0, gintmsk.d32);
11484 +               }
11485 +       }
11486 +}
11487 +
11488 +/**
11489 + * Performs common cleanup for non-periodic transfers after a Transfer
11490 + * Complete interrupt. This function should be called after any endpoint type
11491 + * specific handling is finished to release the host channel.
11492 + */
11493 +static void complete_non_periodic_xfer(dwc_otg_hcd_t *_hcd,
11494 +                                      dwc_hc_t *_hc,
11495 +                                      dwc_otg_hc_regs_t *_hc_regs,
11496 +                                      dwc_otg_qtd_t *_qtd,
11497 +                                      dwc_otg_halt_status_e _halt_status, int *must_free)
11498 +{
11499 +       hcint_data_t hcint;
11500 +
11501 +       _qtd->error_count = 0;
11502 +
11503 +       hcint.d32 = dwc_read_reg32(&_hc_regs->hcint);
11504 +       if (hcint.b.nyet) {
11505 +               /*
11506 +                * Got a NYET on the last transaction of the transfer. This
11507 +                * means that the endpoint should be in the PING state at the
11508 +                * beginning of the next transfer.
11509 +                */
11510 +               _hc->qh->ping_state = 1;
11511 +               clear_hc_int(_hc_regs,nyet);
11512 +       }
11513 +
11514 +       /*
11515 +        * Always halt and release the host channel to make it available for
11516 +        * more transfers. There may still be more phases for a control
11517 +        * transfer or more data packets for a bulk transfer at this point,
11518 +        * but the host channel is still halted. A channel will be reassigned
11519 +        * to the transfer when the non-periodic schedule is processed after
11520 +        * the channel is released. This allows transactions to be queued
11521 +        * properly via dwc_otg_hcd_queue_transactions, which also enables the
11522 +        * Tx FIFO Empty interrupt if necessary.
11523 +        */
11524 +       if (_hc->ep_is_in) {
11525 +               /*
11526 +                * IN transfers in Slave mode require an explicit disable to
11527 +                * halt the channel. (In DMA mode, this call simply releases
11528 +                * the channel.)
11529 +                */
11530 +           halt_channel(_hcd, _hc, _qtd, _halt_status, must_free);
11531 +       } else {
11532 +               /*
11533 +                * The channel is automatically disabled by the core for OUT
11534 +                * transfers in Slave mode.
11535 +                */
11536 +           release_channel(_hcd, _hc, _qtd, _halt_status, must_free);
11537 +       }
11538 +}
11539 +
11540 +/**
11541 + * Performs common cleanup for periodic transfers after a Transfer Complete
11542 + * interrupt. This function should be called after any endpoint type specific
11543 + * handling is finished to release the host channel.
11544 + */
11545 +static void complete_periodic_xfer(dwc_otg_hcd_t *_hcd,
11546 +                                  dwc_hc_t *_hc,
11547 +                                  dwc_otg_hc_regs_t *_hc_regs,
11548 +                                  dwc_otg_qtd_t *_qtd,
11549 +                                  dwc_otg_halt_status_e _halt_status, int *must_free)
11550 +{
11551 +       hctsiz_data_t hctsiz;
11552 +       _qtd->error_count = 0;
11553 +               
11554 +       hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
11555 +       if (!_hc->ep_is_in || hctsiz.b.pktcnt == 0) {
11556 +               /* Core halts channel in these cases. */
11557 +           release_channel(_hcd, _hc, _qtd, _halt_status, must_free);
11558 +       } else {
11559 +               /* Flush any outstanding requests from the Tx queue. */
11560 +           halt_channel(_hcd, _hc, _qtd, _halt_status, must_free);
11561 +       }
11562 +}
11563 +
11564 +/**
11565 + * Handles a host channel Transfer Complete interrupt. This handler may be
11566 + * called in either DMA mode or Slave mode.
11567 + */
11568 +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t *_hcd,
11569 +                                      dwc_hc_t *_hc,
11570 +                                      dwc_otg_hc_regs_t *_hc_regs,
11571 +                                      dwc_otg_qtd_t *_qtd, int *must_free)
11572 +{
11573 +       int                     urb_xfer_done;
11574 +       dwc_otg_halt_status_e   halt_status = DWC_OTG_HC_XFER_COMPLETE;
11575 +       struct urb              *urb = _qtd->urb;
11576 +       int                     pipe_type = usb_pipetype(urb->pipe);
11577 +       int status = -EINPROGRESS;
11578 +
11579 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11580 +                   "Transfer Complete--\n", _hc->hc_num);
11581 +
11582 +       /* 
11583 +        * Handle xfer complete on CSPLIT.
11584 +        */
11585 +       if (_hc->qh->do_split) {
11586 +               _qtd->complete_split = 0;
11587 +       }
11588 +
11589 +       /* Update the QTD and URB states. */
11590 +       switch (pipe_type) {
11591 +       case PIPE_CONTROL:
11592 +               switch (_qtd->control_phase) {
11593 +               case DWC_OTG_CONTROL_SETUP:
11594 +                       if (urb->transfer_buffer_length > 0) {
11595 +                               _qtd->control_phase = DWC_OTG_CONTROL_DATA;
11596 +                       } else {
11597 +                               _qtd->control_phase = DWC_OTG_CONTROL_STATUS;
11598 +                       }
11599 +                       DWC_DEBUGPL(DBG_HCDV, "  Control setup transaction done\n");
11600 +                       halt_status = DWC_OTG_HC_XFER_COMPLETE;
11601 +                       break;
11602 +               case DWC_OTG_CONTROL_DATA: {
11603 +                       urb_xfer_done = update_urb_state_xfer_comp(_hc, _hc_regs,urb, _qtd, &status);
11604 +                       if (urb_xfer_done) {
11605 +                               _qtd->control_phase = DWC_OTG_CONTROL_STATUS;
11606 +                               DWC_DEBUGPL(DBG_HCDV, "  Control data transfer done\n");
11607 +                       } else {
11608 +                               save_data_toggle(_hc, _hc_regs, _qtd);
11609 +                       }
11610 +                       halt_status = DWC_OTG_HC_XFER_COMPLETE;
11611 +                       break;
11612 +               }
11613 +               case DWC_OTG_CONTROL_STATUS:
11614 +                       DWC_DEBUGPL(DBG_HCDV, "  Control transfer complete\n");
11615 +                       if (status == -EINPROGRESS) {
11616 +                               status = 0;
11617 +                       }
11618 +                       dwc_otg_hcd_complete_urb(_hcd, urb, status);
11619 +                       halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
11620 +                       break;
11621 +               }
11622 +
11623 +               complete_non_periodic_xfer(_hcd, _hc, _hc_regs, _qtd,
11624 +                                            halt_status, must_free);
11625 +               break;
11626 +       case PIPE_BULK:
11627 +               DWC_DEBUGPL(DBG_HCDV, "  Bulk transfer complete\n");
11628 +               urb_xfer_done = update_urb_state_xfer_comp(_hc, _hc_regs, urb, _qtd, &status);
11629 +               if (urb_xfer_done) {
11630 +                       dwc_otg_hcd_complete_urb(_hcd, urb, status);
11631 +                       halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
11632 +               } else {
11633 +                       halt_status = DWC_OTG_HC_XFER_COMPLETE;
11634 +               }
11635 +                       
11636 +               save_data_toggle(_hc, _hc_regs, _qtd);
11637 +               complete_non_periodic_xfer(_hcd, _hc, _hc_regs, _qtd,halt_status, must_free);
11638 +               break;
11639 +       case PIPE_INTERRUPT:
11640 +               DWC_DEBUGPL(DBG_HCDV, "  Interrupt transfer complete\n");
11641 +               update_urb_state_xfer_comp(_hc, _hc_regs, urb, _qtd, &status);
11642 +
11643 +               /*
11644 +                * Interrupt URB is done on the first transfer complete
11645 +                * interrupt.
11646 +                */
11647 +           dwc_otg_hcd_complete_urb(_hcd, urb, status);
11648 +               save_data_toggle(_hc, _hc_regs, _qtd);
11649 +               complete_periodic_xfer(_hcd, _hc, _hc_regs, _qtd,
11650 +                                       DWC_OTG_HC_XFER_URB_COMPLETE, must_free);
11651 +               break;
11652 +       case PIPE_ISOCHRONOUS:
11653 +               DWC_DEBUGPL(DBG_HCDV,  "  Isochronous transfer complete\n");
11654 +               if (_qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL)
11655 +               {
11656 +                       halt_status = update_isoc_urb_state(_hcd, _hc, _hc_regs, _qtd,
11657 +                                                           DWC_OTG_HC_XFER_COMPLETE);
11658 +               }
11659 +               complete_periodic_xfer(_hcd, _hc, _hc_regs, _qtd, halt_status, must_free);
11660 +               break;
11661 +       }
11662 +
11663 +        disable_hc_int(_hc_regs,xfercompl);
11664 +
11665 +       return 1;
11666 +}
11667 +
11668 +/**
11669 + * Handles a host channel STALL interrupt. This handler may be called in
11670 + * either DMA mode or Slave mode.
11671 + */
11672 +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t *_hcd,
11673 +                                   dwc_hc_t *_hc,
11674 +                                   dwc_otg_hc_regs_t *_hc_regs,
11675 +                                   dwc_otg_qtd_t *_qtd, int *must_free)
11676 +{
11677 +       struct urb *urb = _qtd->urb;
11678 +       int pipe_type = usb_pipetype(urb->pipe);
11679 +
11680 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11681 +                   "STALL Received--\n", _hc->hc_num);
11682 +
11683 +       if (pipe_type == PIPE_CONTROL) {
11684 +               dwc_otg_hcd_complete_urb(_hcd, _qtd->urb, -EPIPE);
11685 +       }
11686 +
11687 +       if (pipe_type == PIPE_BULK || pipe_type == PIPE_INTERRUPT) {
11688 +               dwc_otg_hcd_complete_urb(_hcd, _qtd->urb, -EPIPE);
11689 +               /*
11690 +                * USB protocol requires resetting the data toggle for bulk
11691 +                * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
11692 +                * setup command is issued to the endpoint. Anticipate the
11693 +                * CLEAR_FEATURE command since a STALL has occurred and reset
11694 +                * the data toggle now.
11695 +                */
11696 +               _hc->qh->data_toggle = 0;
11697 +       }
11698 +
11699 +       halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_STALL, must_free);
11700 +       disable_hc_int(_hc_regs,stall);
11701 +
11702 +       return 1;
11703 +}
11704 +
11705 +/*
11706 + * Updates the state of the URB when a transfer has been stopped due to an
11707 + * abnormal condition before the transfer completes. Modifies the
11708 + * actual_length field of the URB to reflect the number of bytes that have
11709 + * actually been transferred via the host channel.
11710 + */
11711 +static void update_urb_state_xfer_intr(dwc_hc_t *_hc,
11712 +                                      dwc_otg_hc_regs_t *_hc_regs,
11713 +                                      struct urb *_urb,
11714 +                                      dwc_otg_qtd_t *_qtd,
11715 +                                      dwc_otg_halt_status_e _halt_status)
11716 +{
11717 +       uint32_t bytes_transferred = get_actual_xfer_length(_hc, _hc_regs, _qtd,
11718 +                                                           _halt_status, NULL);
11719 +       _urb->actual_length += bytes_transferred;
11720 +
11721 +#ifdef DEBUG
11722 +       {
11723 +               hctsiz_data_t   hctsiz;
11724 +               hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
11725 +               DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
11726 +                           __func__, (_hc->ep_is_in ? "IN" : "OUT"), _hc->hc_num);
11727 +               DWC_DEBUGPL(DBG_HCDV, "  _hc->start_pkt_count %d\n", _hc->start_pkt_count);
11728 +               DWC_DEBUGPL(DBG_HCDV, "  hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
11729 +               DWC_DEBUGPL(DBG_HCDV, "  _hc->max_packet %d\n", _hc->max_packet);
11730 +               DWC_DEBUGPL(DBG_HCDV, "  bytes_transferred %d\n", bytes_transferred);
11731 +               DWC_DEBUGPL(DBG_HCDV, "  _urb->actual_length %d\n", _urb->actual_length);
11732 +               DWC_DEBUGPL(DBG_HCDV, "  _urb->transfer_buffer_length %d\n",
11733 +                           _urb->transfer_buffer_length);
11734 +       }
11735 +#endif 
11736 +}
11737 +
11738 +/**
11739 + * Handles a host channel NAK interrupt. This handler may be called in either
11740 + * DMA mode or Slave mode.
11741 + */
11742 +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t *_hcd,
11743 +                                 dwc_hc_t *_hc,
11744 +                                 dwc_otg_hc_regs_t *_hc_regs,
11745 +                                 dwc_otg_qtd_t *_qtd, int *must_free)
11746 +{
11747 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11748 +                   "NAK Received--\n", _hc->hc_num);
11749 +
11750 +       /*
11751 +        * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
11752 +        * interrupt.  Re-start the SSPLIT transfer.
11753 +        */
11754 +       if (_hc->do_split) {
11755 +               if (_hc->complete_split) {
11756 +                       _qtd->error_count = 0;
11757 +               }
11758 +               _qtd->complete_split = 0;
11759 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_NAK, must_free);
11760 +               goto handle_nak_done;
11761 +       }
11762 +
11763 +       switch (usb_pipetype(_qtd->urb->pipe)) {
11764 +       case PIPE_CONTROL:
11765 +       case PIPE_BULK:
11766 +               if (_hcd->core_if->dma_enable && _hc->ep_is_in) {
11767 +                       /*
11768 +                        * NAK interrupts are enabled on bulk/control IN
11769 +                        * transfers in DMA mode for the sole purpose of
11770 +                        * resetting the error count after a transaction error
11771 +                        * occurs. The core will continue transferring data.
11772 +                        */
11773 +                       _qtd->error_count = 0;
11774 +                       goto handle_nak_done;
11775 +               }
11776 +
11777 +               /*
11778 +                * NAK interrupts normally occur during OUT transfers in DMA
11779 +                * or Slave mode. For IN transfers, more requests will be
11780 +                * queued as request queue space is available.
11781 +                */
11782 +               _qtd->error_count = 0;
11783 +
11784 +               if (!_hc->qh->ping_state) {
11785 +                       update_urb_state_xfer_intr(_hc, _hc_regs, _qtd->urb,
11786 +                                                  _qtd, DWC_OTG_HC_XFER_NAK);
11787 +                       save_data_toggle(_hc, _hc_regs, _qtd);
11788 +                       if (_qtd->urb->dev->speed == USB_SPEED_HIGH) {
11789 +                               _hc->qh->ping_state = 1;
11790 +                       }
11791 +               }
11792 +
11793 +               /*
11794 +                * Halt the channel so the transfer can be re-started from
11795 +                * the appropriate point or the PING protocol will
11796 +                * start/continue. 
11797 +                */
11798 +           halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_NAK, must_free);
11799 +               break;
11800 +       case PIPE_INTERRUPT:
11801 +               _qtd->error_count = 0;
11802 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_NAK, must_free);
11803 +               break;
11804 +       case PIPE_ISOCHRONOUS:
11805 +               /* Should never get called for isochronous transfers. */
11806 +               BUG();
11807 +               break;
11808 +       }
11809 +
11810 + handle_nak_done:
11811 +       disable_hc_int(_hc_regs,nak);
11812 +
11813 +       return 1;
11814 +}
11815 +
11816 +/**
11817 + * Handles a host channel ACK interrupt. This interrupt is enabled when
11818 + * performing the PING protocol in Slave mode, when errors occur during
11819 + * either Slave mode or DMA mode, and during Start Split transactions.
11820 + */
11821 +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t *_hcd,
11822 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
11823 +{
11824 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11825 +                   "ACK Received--\n", _hc->hc_num);
11826 +
11827 +       if (_hc->do_split) {
11828 +               /*
11829 +                * Handle ACK on SSPLIT.
11830 +                * ACK should not occur in CSPLIT.
11831 +                */
11832 +               if ((!_hc->ep_is_in) && (_hc->data_pid_start != DWC_OTG_HC_PID_SETUP)) {
11833 +                       _qtd->ssplit_out_xfer_count = _hc->xfer_len;
11834 +               }
11835 +               if (!(_hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !_hc->ep_is_in)) {
11836 +                       /* Don't need complete for isochronous out transfers. */
11837 +                       _qtd->complete_split = 1;
11838 +               }
11839 +
11840 +               /* ISOC OUT */
11841 +               if ((_hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && !_hc->ep_is_in) {
11842 +                       switch (_hc->xact_pos) {
11843 +                       case DWC_HCSPLIT_XACTPOS_ALL:
11844 +                               break;
11845 +                       case DWC_HCSPLIT_XACTPOS_END:
11846 +                               _qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
11847 +                               _qtd->isoc_split_offset = 0;
11848 +                               break;
11849 +                       case DWC_HCSPLIT_XACTPOS_BEGIN:
11850 +                       case DWC_HCSPLIT_XACTPOS_MID: 
11851 +                               /*
11852 +                                * For BEGIN or MID, calculate the length for
11853 +                                * the next microframe to determine the correct
11854 +                                * SSPLIT token, either MID or END.
11855 +                                */
11856 +                               do {
11857 +                                       struct usb_iso_packet_descriptor *frame_desc;
11858 +
11859 +                                       frame_desc = &_qtd->urb->iso_frame_desc[_qtd->isoc_frame_index];
11860 +                                       _qtd->isoc_split_offset += 188;
11861 +
11862 +                                       if ((frame_desc->length - _qtd->isoc_split_offset) <= 188) {
11863 +                                               _qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_END;
11864 +                                       }
11865 +                                       else {
11866 +                                               _qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_MID;
11867 +                                       }
11868 +                                       
11869 +                               } while(0);
11870 +                               break;
11871 +                       }
11872 +               } else {
11873 +                       halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_ACK, must_free);
11874 +               }
11875 +       } else {
11876 +               _qtd->error_count = 0;
11877 +
11878 +               if (_hc->qh->ping_state) {
11879 +                       _hc->qh->ping_state = 0;
11880 +                       /*
11881 +                        * Halt the channel so the transfer can be re-started
11882 +                        * from the appropriate point. This only happens in
11883 +                        * Slave mode. In DMA mode, the ping_state is cleared
11884 +                        * when the transfer is started because the core
11885 +                        * automatically executes the PING, then the transfer.
11886 +                        */
11887 +                   halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_ACK, must_free);
11888 +               } else {
11889 +                   halt_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
11890 +               }
11891 +       }
11892 +
11893 +       /*
11894 +        * If the ACK occurred when _not_ in the PING state, let the channel
11895 +        * continue transferring data after clearing the error count.
11896 +        */
11897 +
11898 +       disable_hc_int(_hc_regs,ack);
11899 +
11900 +       return 1;
11901 +}
11902 +
11903 +/**
11904 + * Handles a host channel NYET interrupt. This interrupt should only occur on
11905 + * Bulk and Control OUT endpoints and for complete split transactions. If a
11906 + * NYET occurs at the same time as a Transfer Complete interrupt, it is
11907 + * handled in the xfercomp interrupt handler, not here. This handler may be
11908 + * called in either DMA mode or Slave mode.
11909 + */
11910 +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t *_hcd,
11911 +                                  dwc_hc_t *_hc,
11912 +                                  dwc_otg_hc_regs_t *_hc_regs,
11913 +                                  dwc_otg_qtd_t *_qtd, int *must_free)
11914 +{
11915 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11916 +                   "NYET Received--\n", _hc->hc_num);
11917 +
11918 +       /*
11919 +        * NYET on CSPLIT
11920 +        * re-do the CSPLIT immediately on non-periodic
11921 +        */
11922 +       if ((_hc->do_split) && (_hc->complete_split)) {
11923 +               if ((_hc->ep_type == DWC_OTG_EP_TYPE_INTR) || 
11924 +                   (_hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
11925 +                       int frnum = dwc_otg_hcd_get_frame_number(dwc_otg_hcd_to_hcd(_hcd));
11926 +
11927 +                       if (dwc_full_frame_num(frnum) !=
11928 +                           dwc_full_frame_num(_hc->qh->sched_frame)) {
11929 +                               /*
11930 +                                * No longer in the same full speed frame.
11931 +                                * Treat this as a transaction error.
11932 +                                */
11933 +#if 0
11934 +                               /** @todo Fix system performance so this can
11935 +                                * be treated as an error. Right now complete
11936 +                                * splits cannot be scheduled precisely enough
11937 +                                * due to other system activity, so this error
11938 +                                * occurs regularly in Slave mode.
11939 +                                */
11940 +                               _qtd->error_count++;
11941 +#endif                         
11942 +                               _qtd->complete_split = 0;
11943 +                               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_XACT_ERR, must_free);
11944 +                               /** @todo add support for isoc release */
11945 +                               goto handle_nyet_done;
11946 +                       }
11947 +               }
11948 +
11949 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_NYET, must_free);
11950 +               goto handle_nyet_done;
11951 +       }
11952 +
11953 +       _hc->qh->ping_state = 1;
11954 +       _qtd->error_count = 0;
11955 +
11956 +       update_urb_state_xfer_intr(_hc, _hc_regs, _qtd->urb, _qtd,
11957 +                                  DWC_OTG_HC_XFER_NYET);
11958 +       save_data_toggle(_hc, _hc_regs, _qtd);
11959 +
11960 +       /*
11961 +        * Halt the channel and re-start the transfer so the PING
11962 +        * protocol will start.
11963 +        */
11964 +    halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_NYET, must_free);
11965 +
11966 +handle_nyet_done:
11967 +       disable_hc_int(_hc_regs,nyet);
11968 +       clear_hc_int(_hc_regs, nyet);
11969 +       return 1;
11970 +}
11971 +
11972 +/**
11973 + * Handles a host channel babble interrupt. This handler may be called in
11974 + * either DMA mode or Slave mode.
11975 + */
11976 +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t *_hcd,
11977 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
11978 +{
11979 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
11980 +                   "Babble Error--\n", _hc->hc_num);
11981 +       if (_hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
11982 +               dwc_otg_hcd_complete_urb(_hcd, _qtd->urb, -EOVERFLOW);
11983 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_BABBLE_ERR, must_free);
11984 +       } else {
11985 +               dwc_otg_halt_status_e halt_status;
11986 +               halt_status = update_isoc_urb_state(_hcd, _hc, _hc_regs, _qtd,
11987 +                                                   DWC_OTG_HC_XFER_BABBLE_ERR);
11988 +               halt_channel(_hcd, _hc, _qtd, halt_status, must_free);
11989 +       }
11990 +       disable_hc_int(_hc_regs,bblerr);
11991 +       return 1;
11992 +}
11993 +
11994 +/**
11995 + * Handles a host channel AHB error interrupt. This handler is only called in
11996 + * DMA mode.
11997 + */
11998 +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t *_hcd,
11999 +                                    dwc_hc_t *_hc,
12000 +                                    dwc_otg_hc_regs_t *_hc_regs,
12001 +                                    dwc_otg_qtd_t *_qtd)
12002 +{
12003 +       hcchar_data_t   hcchar;
12004 +       hcsplt_data_t   hcsplt;
12005 +       hctsiz_data_t   hctsiz;
12006 +       uint32_t        hcdma;
12007 +       struct urb      *urb = _qtd->urb;
12008 +
12009 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
12010 +                   "AHB Error--\n", _hc->hc_num);
12011 +
12012 +       hcchar.d32 = dwc_read_reg32(&_hc_regs->hcchar);
12013 +       hcsplt.d32 = dwc_read_reg32(&_hc_regs->hcsplt);
12014 +       hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
12015 +       hcdma = dwc_read_reg32(&_hc_regs->hcdma);
12016 +
12017 +       DWC_ERROR("AHB ERROR, Channel %d\n", _hc->hc_num);
12018 +       DWC_ERROR("  hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
12019 +       DWC_ERROR("  hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
12020 +               DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
12021 +       DWC_ERROR("  Device address: %d\n", usb_pipedevice(urb->pipe));
12022 +       DWC_ERROR("  Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
12023 +                   (usb_pipein(urb->pipe) ? "IN" : "OUT"));
12024 +       DWC_ERROR("  Endpoint type: %s\n",
12025 +                   ({char *pipetype;
12026 +                   switch (usb_pipetype(urb->pipe)) {
12027 +                   case PIPE_CONTROL: pipetype = "CONTROL"; break;
12028 +                   case PIPE_BULK: pipetype = "BULK"; break;
12029 +                   case PIPE_INTERRUPT: pipetype = "INTERRUPT"; break;
12030 +                   case PIPE_ISOCHRONOUS: pipetype = "ISOCHRONOUS"; break;
12031 +                   default: pipetype = "UNKNOWN"; break;
12032 +                   }; pipetype;}));
12033 +       DWC_ERROR("  Speed: %s\n",
12034 +                   ({char *speed;
12035 +                   switch (urb->dev->speed) {
12036 +                   case USB_SPEED_HIGH: speed = "HIGH"; break;
12037 +                   case USB_SPEED_FULL: speed = "FULL"; break;
12038 +                   case USB_SPEED_LOW: speed = "LOW"; break;
12039 +                   default: speed = "UNKNOWN"; break;
12040 +                   }; speed;}));
12041 +       DWC_ERROR("  Max packet size: %d\n",
12042 +                   usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
12043 +       DWC_ERROR("  Data buffer length: %d\n", urb->transfer_buffer_length);
12044 +       DWC_ERROR("  Transfer buffer: %p, Transfer DMA: %p\n",
12045 +                 urb->transfer_buffer, (void *)(u32)urb->transfer_dma);
12046 +       DWC_ERROR("  Setup buffer: %p, Setup DMA: %p\n", 
12047 +                 urb->setup_packet, (void *)(u32)urb->setup_dma);
12048 +       DWC_ERROR("  Interval: %d\n", urb->interval);
12049 +
12050 +       dwc_otg_hcd_complete_urb(_hcd, urb, -EIO);
12051 +
12052 +       /*
12053 +        * Force a channel halt. Don't call halt_channel because that won't
12054 +        * write to the HCCHARn register in DMA mode to force the halt.
12055 +        */
12056 +       dwc_otg_hc_halt(_hcd->core_if, _hc, DWC_OTG_HC_XFER_AHB_ERR);
12057 +
12058 +       disable_hc_int(_hc_regs,ahberr);
12059 +       return 1;
12060 +}
12061 +
12062 +/**
12063 + * Handles a host channel transaction error interrupt. This handler may be
12064 + * called in either DMA mode or Slave mode.
12065 + */
12066 +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t *_hcd,
12067 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12068 +{
12069 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
12070 +                   "Transaction Error--\n", _hc->hc_num);
12071 +
12072 +       switch (usb_pipetype(_qtd->urb->pipe)) {
12073 +       case PIPE_CONTROL:
12074 +       case PIPE_BULK:
12075 +               _qtd->error_count++;
12076 +               if (!_hc->qh->ping_state) {
12077 +                       update_urb_state_xfer_intr(_hc, _hc_regs, _qtd->urb,
12078 +                                                  _qtd, DWC_OTG_HC_XFER_XACT_ERR);
12079 +                       save_data_toggle(_hc, _hc_regs, _qtd);
12080 +                       if (!_hc->ep_is_in && _qtd->urb->dev->speed == USB_SPEED_HIGH) {
12081 +                               _hc->qh->ping_state = 1;
12082 +                       }
12083 +               }
12084 +
12085 +               /*
12086 +                * Halt the channel so the transfer can be re-started from
12087 +                * the appropriate point or the PING protocol will start.
12088 +                */
12089 +           halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_XACT_ERR, must_free);
12090 +               break;
12091 +       case PIPE_INTERRUPT:
12092 +               _qtd->error_count++;
12093 +               if ((_hc->do_split) && (_hc->complete_split)) {
12094 +                       _qtd->complete_split = 0;
12095 +               }
12096 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_XACT_ERR, must_free);
12097 +               break;
12098 +       case PIPE_ISOCHRONOUS:
12099 +               {
12100 +                       dwc_otg_halt_status_e halt_status;
12101 +                       halt_status = update_isoc_urb_state(_hcd, _hc, _hc_regs, _qtd,
12102 +                                                           DWC_OTG_HC_XFER_XACT_ERR);
12103 +                                                           
12104 +                       halt_channel(_hcd, _hc, _qtd, halt_status, must_free);
12105 +               }
12106 +               break;
12107 +       }
12108 +               
12109 +
12110 +       disable_hc_int(_hc_regs,xacterr);
12111 +
12112 +       return 1;
12113 +}
12114 +
12115 +/**
12116 + * Handles a host channel frame overrun interrupt. This handler may be called
12117 + * in either DMA mode or Slave mode.
12118 + */
12119 +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t *_hcd,
12120 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12121 +{
12122 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
12123 +                   "Frame Overrun--\n", _hc->hc_num);
12124 +
12125 +       switch (usb_pipetype(_qtd->urb->pipe)) {
12126 +       case PIPE_CONTROL:
12127 +       case PIPE_BULK:
12128 +               break;
12129 +       case PIPE_INTERRUPT:
12130 +               halt_channel(_hcd, _hc, _qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN, must_free);
12131 +               break;
12132 +       case PIPE_ISOCHRONOUS:
12133 +               {
12134 +                       dwc_otg_halt_status_e halt_status;
12135 +                       halt_status = update_isoc_urb_state(_hcd, _hc, _hc_regs, _qtd,
12136 +                                                           DWC_OTG_HC_XFER_FRAME_OVERRUN);
12137 +                                                           
12138 +                       halt_channel(_hcd, _hc, _qtd, halt_status, must_free);
12139 +               }
12140 +               break;
12141 +       }
12142 +
12143 +       disable_hc_int(_hc_regs,frmovrun);
12144 +
12145 +       return 1;
12146 +}
12147 +
12148 +/**
12149 + * Handles a host channel data toggle error interrupt. This handler may be
12150 + * called in either DMA mode or Slave mode.
12151 + */
12152 +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t *_hcd,
12153 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12154 +{
12155 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
12156 +                   "Data Toggle Error--\n", _hc->hc_num);
12157 +
12158 +       if (_hc->ep_is_in) {
12159 +               _qtd->error_count = 0;
12160 +       } else {
12161 +               DWC_ERROR("Data Toggle Error on OUT transfer,"
12162 +                         "channel %d\n", _hc->hc_num);
12163 +       }
12164 +
12165 +       disable_hc_int(_hc_regs,datatglerr);
12166 +
12167 +       return 1;
12168 +}
12169 +
12170 +#ifdef DEBUG
12171 +/**
12172 + * This function is for debug only. It checks that a valid halt status is set
12173 + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
12174 + * taken and a warning is issued.
12175 + * @return 1 if halt status is ok, 0 otherwise.
12176 + */
12177 +static inline int halt_status_ok(dwc_otg_hcd_t *_hcd,
12178 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12179 +{
12180 +       hcchar_data_t hcchar;
12181 +       hctsiz_data_t hctsiz;
12182 +       hcint_data_t hcint;
12183 +       hcintmsk_data_t hcintmsk;
12184 +       hcsplt_data_t hcsplt;
12185 +
12186 +       if (_hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
12187 +               /*
12188 +                * This code is here only as a check. This condition should
12189 +                * never happen. Ignore the halt if it does occur.
12190 +                */
12191 +               hcchar.d32 = dwc_read_reg32(&_hc_regs->hcchar);
12192 +               hctsiz.d32 = dwc_read_reg32(&_hc_regs->hctsiz);
12193 +               hcint.d32 = dwc_read_reg32(&_hc_regs->hcint);
12194 +               hcintmsk.d32 = dwc_read_reg32(&_hc_regs->hcintmsk);
12195 +               hcsplt.d32 = dwc_read_reg32(&_hc_regs->hcsplt);
12196 +               DWC_WARN("%s: _hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
12197 +                        "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
12198 +                        "hcint 0x%08x, hcintmsk 0x%08x, "
12199 +                        "hcsplt 0x%08x, qtd->complete_split %d\n",
12200 +                        __func__, _hc->hc_num, hcchar.d32, hctsiz.d32,
12201 +                        hcint.d32, hcintmsk.d32,
12202 +                        hcsplt.d32, _qtd->complete_split);
12203 +
12204 +               DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
12205 +                        __func__, _hc->hc_num);
12206 +               DWC_WARN("\n");
12207 +               clear_hc_int(_hc_regs,chhltd);
12208 +               return 0;
12209 +       }
12210 +
12211 +       /*
12212 +        * This code is here only as a check. hcchar.chdis should
12213 +        * never be set when the halt interrupt occurs. Halt the
12214 +        * channel again if it does occur.
12215 +        */
12216 +       hcchar.d32 = dwc_read_reg32(&_hc_regs->hcchar);
12217 +       if (hcchar.b.chdis) {
12218 +               DWC_WARN("%s: hcchar.chdis set unexpectedly, "
12219 +                        "hcchar 0x%08x, trying to halt again\n",
12220 +                        __func__, hcchar.d32);
12221 +               clear_hc_int(_hc_regs,chhltd);
12222 +               _hc->halt_pending = 0;
12223 +               halt_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
12224 +               return 0;
12225 +       }
12226 +
12227 +       return 1;
12228 +}
12229 +#endif
12230 +
12231 +/**
12232 + * Handles a host Channel Halted interrupt in DMA mode. This handler
12233 + * determines the reason the channel halted and proceeds accordingly.
12234 + */
12235 +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t *_hcd,
12236 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12237 +{
12238 +       hcint_data_t hcint;
12239 +       hcintmsk_data_t hcintmsk;
12240 +
12241 +       if (_hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
12242 +           _hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
12243 +               /*
12244 +                * Just release the channel. A dequeue can happen on a
12245 +                * transfer timeout. In the case of an AHB Error, the channel
12246 +                * was forced to halt because there's no way to gracefully
12247 +                * recover.
12248 +                */
12249 +           release_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
12250 +               return;
12251 +       }
12252 +
12253 +       /* Read the HCINTn register to determine the cause for the halt. */
12254 +       hcint.d32 = dwc_read_reg32(&_hc_regs->hcint);
12255 +       hcintmsk.d32 = dwc_read_reg32(&_hc_regs->hcintmsk);
12256 +
12257 +       if (hcint.b.xfercomp) {
12258 +               /** @todo This is here because of a possible hardware bug.  Spec
12259 +                * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
12260 +                * interrupt w/ACK bit set should occur, but I only see the
12261 +                * XFERCOMP bit, even with it masked out.  This is a workaround
12262 +                * for that behavior.  Should fix this when hardware is fixed.
12263 +                */
12264 +               if ((_hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!_hc->ep_is_in)) {
12265 +                       handle_hc_ack_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12266 +               }
12267 +               handle_hc_xfercomp_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12268 +       } else if (hcint.b.stall) {
12269 +               handle_hc_stall_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12270 +       } else if (hcint.b.xacterr) {
12271 +               /*
12272 +                * Must handle xacterr before nak or ack. Could get a xacterr
12273 +                * at the same time as either of these on a BULK/CONTROL OUT
12274 +                * that started with a PING. The xacterr takes precedence.
12275 +                */
12276 +           handle_hc_xacterr_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12277 +       } else if (hcint.b.nyet) {
12278 +               /*
12279 +                * Must handle nyet before nak or ack. Could get a nyet at the
12280 +                * same time as either of those on a BULK/CONTROL OUT that
12281 +                * started with a PING. The nyet takes precedence.
12282 +                */
12283 +           handle_hc_nyet_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12284 +       } else if (hcint.b.bblerr) {
12285 +               handle_hc_babble_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12286 +       } else if (hcint.b.frmovrun) {
12287 +               handle_hc_frmovrun_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12288 +       } else if (hcint.b.datatglerr) {
12289 +               handle_hc_datatglerr_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12290 +               _hc->qh->data_toggle = 0;
12291 +               halt_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
12292 +       } else if (hcint.b.nak && !hcintmsk.b.nak) {
12293 +               /*
12294 +                * If nak is not masked, it's because a non-split IN transfer
12295 +                * is in an error state. In that case, the nak is handled by
12296 +                * the nak interrupt handler, not here. Handle nak here for
12297 +                * BULK/CONTROL OUT transfers, which halt on a NAK to allow
12298 +                * rewinding the buffer pointer.
12299 +                */
12300 +           handle_hc_nak_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12301 +       } else if (hcint.b.ack && !hcintmsk.b.ack) {
12302 +               /*
12303 +                * If ack is not masked, it's because a non-split IN transfer
12304 +                * is in an error state. In that case, the ack is handled by
12305 +                * the ack interrupt handler, not here. Handle ack here for
12306 +                * split transfers. Start splits halt on ACK.
12307 +                */
12308 +           handle_hc_ack_intr(_hcd, _hc, _hc_regs, _qtd, must_free);
12309 +       } else {
12310 +               if (_hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
12311 +                   _hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
12312 +                       /*
12313 +                        * A periodic transfer halted with no other channel
12314 +                        * interrupts set. Assume it was halted by the core
12315 +                        * because it could not be completed in its scheduled
12316 +                        * (micro)frame.
12317 +                        */
12318 +#ifdef DEBUG
12319 +                       DWC_PRINT("%s: Halt channel %d (assume incomplete periodic transfer)\n",
12320 +                                 __func__, _hc->hc_num);
12321 +#endif /*  */
12322 +                   halt_channel(_hcd, _hc, _qtd,
12323 +                                        DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE, must_free);
12324 +               } else {
12325 +#ifdef DEBUG
12326 +                       DWC_ERROR("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
12327 +                            "for halting is unknown, nyet %d, hcint 0x%08x, intsts 0x%08x\n",
12328 +                            __func__, _hc->hc_num, hcint.b.nyet, hcint.d32,
12329 +                                dwc_read_reg32(&_hcd->core_if->core_global_regs->gintsts));
12330 +#endif                 
12331 +                       halt_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
12332 +               }
12333 +       }
12334 +}
12335 +
12336 +/**
12337 + * Handles a host channel Channel Halted interrupt.
12338 + *
12339 + * In slave mode, this handler is called only when the driver specifically
12340 + * requests a halt. This occurs during handling other host channel interrupts
12341 + * (e.g. nak, xacterr, stall, nyet, etc.).
12342 + *
12343 + * In DMA mode, this is the interrupt that occurs when the core has finished
12344 + * processing a transfer on a channel. Other host channel interrupts (except
12345 + * ahberr) are disabled in DMA mode.
12346 + */
12347 +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t *_hcd,
12348 +       dwc_hc_t * _hc, dwc_otg_hc_regs_t * _hc_regs, dwc_otg_qtd_t * _qtd, int *must_free)
12349 +{
12350 +       DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
12351 +                   "Channel Halted--\n", _hc->hc_num);
12352 +
12353 +       if (_hcd->core_if->dma_enable) {
12354 +               handle_hc_chhltd_intr_dma(_hcd, _hc, _hc_regs, _qtd, must_free);
12355 +       } else {
12356 +#ifdef DEBUG
12357 +               if (!halt_status_ok(_hcd, _hc, _hc_regs, _qtd, must_free)) {
12358 +                       return 1;
12359 +               }
12360 +#endif /*  */
12361 +           release_channel(_hcd, _hc, _qtd, _hc->halt_status, must_free);
12362 +       }
12363 +
12364 +       return 1;
12365 +}
12366 +
12367 +/** Handles interrupt for a specific Host Channel */
12368 +int32_t dwc_otg_hcd_handle_hc_n_intr (dwc_otg_hcd_t *_dwc_otg_hcd, uint32_t _num)
12369 +{
12370 +       int must_free = 0;
12371 +       int retval = 0;
12372 +       hcint_data_t hcint;
12373 +       hcintmsk_data_t hcintmsk;
12374 +       dwc_hc_t *hc;
12375 +       dwc_otg_hc_regs_t *hc_regs;
12376 +       dwc_otg_qtd_t *qtd;
12377 +       
12378 +       DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", _num);
12379 +
12380 +       hc = _dwc_otg_hcd->hc_ptr_array[_num];
12381 +       hc_regs = _dwc_otg_hcd->core_if->host_if->hc_regs[_num];
12382 +       qtd = list_entry(hc->qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry);
12383 +
12384 +       hcint.d32 = dwc_read_reg32(&hc_regs->hcint);
12385 +       hcintmsk.d32 = dwc_read_reg32(&hc_regs->hcintmsk);
12386 +       DWC_DEBUGPL(DBG_HCDV, "  hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
12387 +                   hcint.d32, hcintmsk.d32, (hcint.d32 & hcintmsk.d32));
12388 +       hcint.d32 = hcint.d32 & hcintmsk.d32;
12389 +
12390 +       if (!_dwc_otg_hcd->core_if->dma_enable) {
12391 +               if ((hcint.b.chhltd) && (hcint.d32 != 0x2)) {
12392 +                       hcint.b.chhltd = 0;
12393 +               }
12394 +       }
12395 +
12396 +       if (hcint.b.xfercomp) {
12397 +               retval |= handle_hc_xfercomp_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12398 +               /*
12399 +                * If NYET occurred at same time as Xfer Complete, the NYET is
12400 +                * handled by the Xfer Complete interrupt handler. Don't want
12401 +                * to call the NYET interrupt handler in this case.
12402 +                */
12403 +               hcint.b.nyet = 0;
12404 +       }
12405 +       if (hcint.b.chhltd) {
12406 +               retval |= handle_hc_chhltd_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12407 +       }
12408 +       if (hcint.b.ahberr) {
12409 +               retval |= handle_hc_ahberr_intr(_dwc_otg_hcd, hc, hc_regs, qtd);
12410 +       }
12411 +       if (hcint.b.stall) {
12412 +               retval |= handle_hc_stall_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12413 +       }
12414 +       if (hcint.b.nak) {
12415 +               retval |= handle_hc_nak_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12416 +       }
12417 +       if (hcint.b.ack) {
12418 +               retval |= handle_hc_ack_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12419 +       }
12420 +       if (hcint.b.nyet) {
12421 +               retval |= handle_hc_nyet_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12422 +       }
12423 +       if (hcint.b.xacterr) {
12424 +               retval |= handle_hc_xacterr_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12425 +       }
12426 +       if (hcint.b.bblerr) {
12427 +               retval |= handle_hc_babble_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12428 +       }
12429 +       if (hcint.b.frmovrun) {
12430 +               retval |= handle_hc_frmovrun_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12431 +       }
12432 +       if (hcint.b.datatglerr) {
12433 +               retval |= handle_hc_datatglerr_intr(_dwc_otg_hcd, hc, hc_regs, qtd, &must_free);
12434 +       }
12435 +
12436 +       /*
12437 +        * Logic to free the qtd here, at the end of the hc intr
12438 +        * processing, if the handling of this interrupt determined
12439 +        * that it needs to be freed.
12440 +        */
12441 +       if (must_free) {
12442 +               /* Free the qtd here now that we are done using it. */
12443 +               dwc_otg_hcd_qtd_free(qtd);
12444 +       }
12445 +       return retval;
12446 +}
12447 +
12448 +#endif /* DWC_DEVICE_ONLY */
12449 --- /dev/null
12450 +++ b/drivers/usb/dwc_otg/dwc_otg_hcd_queue.c
12451 @@ -0,0 +1,794 @@
12452 +/* ==========================================================================
12453 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_hcd_queue.c $
12454 + * $Revision: 1.1.1.1 $
12455 + * $Date: 2009-04-17 06:15:34 $
12456 + * $Change: 537387 $
12457 + *
12458 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
12459 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
12460 + * otherwise expressly agreed to in writing between Synopsys and you.
12461 + * 
12462 + * The Software IS NOT an item of Licensed Software or Licensed Product under
12463 + * any End User Software License Agreement or Agreement for Licensed Product
12464 + * with Synopsys or any supplement thereto. You are permitted to use and
12465 + * redistribute this Software in source and binary forms, with or without
12466 + * modification, provided that redistributions of source code must retain this
12467 + * notice. You may not view, use, disclose, copy or distribute this file or
12468 + * any information contained herein except pursuant to this license grant from
12469 + * Synopsys. If you do not agree with this notice, including the disclaimer
12470 + * below, then you are not authorized to use the Software.
12471 + * 
12472 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
12473 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12474 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12475 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
12476 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
12477 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12478 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
12479 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12480 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
12481 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
12482 + * DAMAGE.
12483 + * ========================================================================== */
12484 +#ifndef DWC_DEVICE_ONLY
12485 +
12486 +/**
12487 + * @file
12488 + *
12489 + * This file contains the functions to manage Queue Heads and Queue
12490 + * Transfer Descriptors.
12491 + */
12492 +#include <linux/kernel.h>
12493 +#include <linux/module.h>
12494 +#include <linux/moduleparam.h>
12495 +#include <linux/init.h>
12496 +#include <linux/device.h>
12497 +#include <linux/errno.h>
12498 +#include <linux/list.h>
12499 +#include <linux/interrupt.h>
12500 +#include <linux/string.h>
12501 +
12502 +#include "dwc_otg_driver.h"
12503 +#include "dwc_otg_hcd.h"
12504 +#include "dwc_otg_regs.h"
12505 +
12506 +/**
12507 + * This function allocates and initializes a QH.
12508 + *
12509 + * @param _hcd The HCD state structure for the DWC OTG controller.
12510 + * @param[in] _urb Holds the information about the device/endpoint that we need
12511 + * to initialize the QH.
12512 + *
12513 + * @return Returns pointer to the newly allocated QH, or NULL on error. */
12514 +dwc_otg_qh_t *dwc_otg_hcd_qh_create (dwc_otg_hcd_t *_hcd, struct urb *_urb)
12515 +{
12516 +       dwc_otg_qh_t *qh;
12517 +
12518 +       /* Allocate memory */
12519 +       /** @todo add memflags argument */
12520 +       qh = dwc_otg_hcd_qh_alloc ();
12521 +       if (qh == NULL) {
12522 +               return NULL;
12523 +       }
12524 +
12525 +       dwc_otg_hcd_qh_init (_hcd, qh, _urb);
12526 +       return qh;
12527 +}
12528 +
12529 +/** Free each QTD in the QH's QTD-list then free the QH.  QH should already be
12530 + * removed from a list.  QTD list should already be empty if called from URB
12531 + * Dequeue.
12532 + *
12533 + * @param[in] _qh The QH to free.
12534 + */
12535 +void dwc_otg_hcd_qh_free (dwc_otg_qh_t *_qh)
12536 +{
12537 +       dwc_otg_qtd_t *qtd;
12538 +       struct list_head *pos;
12539 +       unsigned long flags;
12540 +
12541 +       /* Free each QTD in the QTD list */
12542 +       local_irq_save (flags);
12543 +       for (pos = _qh->qtd_list.next;
12544 +            pos != &_qh->qtd_list;
12545 +            pos = _qh->qtd_list.next)
12546 +       {
12547 +               list_del (pos);
12548 +               qtd = dwc_list_to_qtd (pos);
12549 +               dwc_otg_hcd_qtd_free (qtd);
12550 +       }
12551 +       local_irq_restore (flags);
12552 +
12553 +       kfree (_qh);
12554 +       return;
12555 +}
12556 +
12557 +/** Initializes a QH structure.
12558 + *
12559 + * @param[in] _hcd The HCD state structure for the DWC OTG controller.
12560 + * @param[in] _qh The QH to init.
12561 + * @param[in] _urb Holds the information about the device/endpoint that we need
12562 + * to initialize the QH. */
12563 +#define SCHEDULE_SLOP 10
12564 +void dwc_otg_hcd_qh_init(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, struct urb *_urb)
12565 +{
12566 +       memset (_qh, 0, sizeof (dwc_otg_qh_t));
12567 +
12568 +       /* Initialize QH */
12569 +       switch (usb_pipetype(_urb->pipe)) {
12570 +       case PIPE_CONTROL:
12571 +               _qh->ep_type = USB_ENDPOINT_XFER_CONTROL;
12572 +               break;
12573 +       case PIPE_BULK:
12574 +               _qh->ep_type = USB_ENDPOINT_XFER_BULK;
12575 +               break;
12576 +       case PIPE_ISOCHRONOUS:
12577 +               _qh->ep_type = USB_ENDPOINT_XFER_ISOC;
12578 +               break;
12579 +       case PIPE_INTERRUPT: 
12580 +               _qh->ep_type = USB_ENDPOINT_XFER_INT;
12581 +               break;
12582 +       }
12583 +
12584 +       _qh->ep_is_in = usb_pipein(_urb->pipe) ? 1 : 0;
12585 +
12586 +       _qh->data_toggle = DWC_OTG_HC_PID_DATA0;
12587 +       _qh->maxp = usb_maxpacket(_urb->dev, _urb->pipe, !(usb_pipein(_urb->pipe)));
12588 +       INIT_LIST_HEAD(&_qh->qtd_list);
12589 +       INIT_LIST_HEAD(&_qh->qh_list_entry);
12590 +       _qh->channel = NULL;
12591 +
12592 +       /* FS/LS Enpoint on HS Hub 
12593 +        * NOT virtual root hub */
12594 +       _qh->do_split = 0;
12595 +       _qh->speed = _urb->dev->speed;
12596 +       if (((_urb->dev->speed == USB_SPEED_LOW) || 
12597 +            (_urb->dev->speed == USB_SPEED_FULL)) &&
12598 +               (_urb->dev->tt) && (_urb->dev->tt->hub) && (_urb->dev->tt->hub->devnum != 1)) {
12599 +               DWC_DEBUGPL(DBG_HCD, "QH init: EP %d: TT found at hub addr %d, for port %d\n", 
12600 +                          usb_pipeendpoint(_urb->pipe), _urb->dev->tt->hub->devnum, 
12601 +                          _urb->dev->ttport);
12602 +               _qh->do_split = 1;
12603 +       }
12604 +
12605 +       if (_qh->ep_type == USB_ENDPOINT_XFER_INT ||
12606 +           _qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
12607 +               /* Compute scheduling parameters once and save them. */
12608 +               hprt0_data_t hprt;
12609 +
12610 +               /** @todo Account for split transfers in the bus time. */
12611 +               int bytecount = dwc_hb_mult(_qh->maxp) * dwc_max_packet(_qh->maxp);
12612 +               _qh->usecs = NS_TO_US(usb_calc_bus_time(_urb->dev->speed,
12613 +                                              usb_pipein(_urb->pipe),
12614 +                                       (_qh->ep_type == USB_ENDPOINT_XFER_ISOC),bytecount));
12615 +
12616 +               /* Start in a slightly future (micro)frame. */
12617 +               _qh->sched_frame = dwc_frame_num_inc(_hcd->frame_number, SCHEDULE_SLOP);
12618 +               _qh->interval = _urb->interval;
12619 +#if 0
12620 +               /* Increase interrupt polling rate for debugging. */
12621 +               if (_qh->ep_type == USB_ENDPOINT_XFER_INT) {
12622 +                       _qh->interval = 8;
12623 +               }
12624 +#endif         
12625 +               hprt.d32 = dwc_read_reg32(_hcd->core_if->host_if->hprt0);
12626 +               if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) && 
12627 +                   ((_urb->dev->speed == USB_SPEED_LOW) || 
12628 +                    (_urb->dev->speed == USB_SPEED_FULL)))
12629 +               {
12630 +                       _qh->interval *= 8;
12631 +                       _qh->sched_frame |= 0x7;
12632 +                       _qh->start_split_frame = _qh->sched_frame;
12633 +               }
12634 +       }
12635 +
12636 +       DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
12637 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - qh = %p\n", _qh);
12638 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Device Address = %d\n",
12639 +                   _urb->dev->devnum);
12640 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Endpoint %d, %s\n",
12641 +                   usb_pipeendpoint(_urb->pipe),
12642 +                   usb_pipein(_urb->pipe) == USB_DIR_IN ? "IN" : "OUT");
12643 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Speed = %s\n", 
12644 +                   ({ char *speed; switch (_urb->dev->speed) {
12645 +                   case USB_SPEED_LOW: speed = "low";  break;
12646 +                   case USB_SPEED_FULL: speed = "full";        break;
12647 +                   case USB_SPEED_HIGH: speed = "high";        break;
12648 +                   default: speed = "?";       break;
12649 +                   }; speed;}));
12650 +       DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Type = %s\n",
12651 +                   ({ char *type; switch (_qh->ep_type) {
12652 +                   case USB_ENDPOINT_XFER_ISOC: type = "isochronous";  break;
12653 +                   case USB_ENDPOINT_XFER_INT: type = "interrupt";     break;
12654 +                   case USB_ENDPOINT_XFER_CONTROL: type = "control";   break;
12655 +                   case USB_ENDPOINT_XFER_BULK: type = "bulk"; break;
12656 +                   default: type = "?";        break;
12657 +                   }; type;}));
12658 +#ifdef DEBUG
12659 +       if (_qh->ep_type == USB_ENDPOINT_XFER_INT) {
12660 +               DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
12661 +                           _qh->usecs);
12662 +               DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
12663 +                           _qh->interval);
12664 +       }
12665 +#endif 
12666 +       
12667 +       return;
12668 +}
12669 +
12670 +/**
12671 + * Microframe scheduler
12672 + * track the total use in hcd->frame_usecs
12673 + * keep each qh use in qh->frame_usecs
12674 + * when surrendering the qh then donate the time back
12675 + */
12676 +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
12677 +
12678 +/*
12679 + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
12680 + */
12681 +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
12682 +{
12683 +       int i;
12684 +       for (i=0; i<8; i++) {
12685 +               _hcd->frame_usecs[i] = max_uframe_usecs[i];
12686 +       }
12687 +       return 0;
12688 +}
12689 +
12690 +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
12691 +{
12692 +       int i;
12693 +       unsigned short utime;
12694 +       int t_left;
12695 +       int ret;
12696 +       int done;
12697 +
12698 +       ret = -1;
12699 +       utime = _qh->usecs;
12700 +       t_left = utime;
12701 +       i = 0;
12702 +       done = 0;
12703 +       while (done == 0) {
12704 +               /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
12705 +               if (utime <= _hcd->frame_usecs[i]) {
12706 +                       _hcd->frame_usecs[i] -= utime;
12707 +                       _qh->frame_usecs[i] += utime;
12708 +                       t_left -= utime;
12709 +                       ret = i;
12710 +                       done = 1;
12711 +                       return ret;
12712 +               } else {
12713 +                       i++;
12714 +                       if (i == 8) {
12715 +                               done = 1;
12716 +                               ret = -1;
12717 +                       }
12718 +               }
12719 +       }
12720 +       return ret;
12721 +}
12722 +
12723 +/*
12724 + * use this for FS apps that can span multiple uframes
12725 + */
12726 +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
12727 +{
12728 +       int i;
12729 +       int j;
12730 +       unsigned short utime;
12731 +       int t_left;
12732 +       int ret;
12733 +       int done;
12734 +       unsigned short xtime;
12735 +
12736 +       ret = -1;
12737 +       utime = _qh->usecs;
12738 +       t_left = utime;
12739 +       i = 0;
12740 +       done = 0;
12741 +loop:
12742 +       while (done == 0) {
12743 +               if(_hcd->frame_usecs[i] <= 0) {
12744 +                       i++;
12745 +                       if (i == 8) {
12746 +                               done = 1;
12747 +                               ret = -1;
12748 +                       }
12749 +                       goto loop;
12750 +               }
12751 +
12752 +               /*
12753 +                * we need n consequtive slots
12754 +                * so use j as a start slot j plus j+1 must be enough time (for now)
12755 +                */
12756 +               xtime= _hcd->frame_usecs[i];
12757 +               for (j = i+1 ; j < 8 ; j++ ) {
12758 +                       /*
12759 +                        * if we add this frame remaining time to xtime we may
12760 +                        * be OK, if not we need to test j for a complete frame
12761 +                        */
12762 +                       if ((xtime+_hcd->frame_usecs[j]) < utime) {
12763 +                               if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
12764 +                                       j = 8;
12765 +                                       ret = -1;
12766 +                                       continue;
12767 +                               }
12768 +                       }
12769 +                       if (xtime >= utime) {
12770 +                               ret = i;
12771 +                               j = 8;  /* stop loop with a good value ret */
12772 +                               continue;
12773 +                       }
12774 +                       /* add the frame time to x time */
12775 +                       xtime += _hcd->frame_usecs[j];
12776 +                       /* we must have a fully available next frame or break */
12777 +                       if ((xtime < utime)
12778 +                           && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
12779 +                               ret = -1;
12780 +                               j = 8;  /* stop loop with a bad value ret */
12781 +                               continue;
12782 +                       }
12783 +               }
12784 +               if (ret >= 0) {
12785 +                       t_left = utime;
12786 +                       for (j = i; (t_left>0) && (j < 8); j++ ) {
12787 +                               t_left -= _hcd->frame_usecs[j];
12788 +                               if ( t_left <= 0 ) {
12789 +                                       _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
12790 +                                       _hcd->frame_usecs[j]= -t_left;
12791 +                                       ret = i;
12792 +                                       done = 1;
12793 +                               } else {
12794 +                                       _qh->frame_usecs[j] += _hcd->frame_usecs[j];
12795 +                                       _hcd->frame_usecs[j] = 0;
12796 +                               }
12797 +                       }
12798 +               } else {
12799 +                       i++;
12800 +                       if (i == 8) {
12801 +                               done = 1;
12802 +                               ret = -1;
12803 +                       }
12804 +               }
12805 +       }
12806 +       return ret;
12807 +}
12808 +
12809 +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
12810 +{
12811 +       int ret;
12812 +       ret = -1;
12813 +
12814 +       if (_qh->speed == USB_SPEED_HIGH) {
12815 +               /* if this is a hs transaction we need a full frame */
12816 +               ret = find_single_uframe(_hcd, _qh);
12817 +       } else {
12818 +               /* if this is a fs transaction we may need a sequence of frames */
12819 +               ret = find_multi_uframe(_hcd, _qh);
12820 +       }
12821 +       return ret;
12822 +}
12823 +                       
12824 +/**
12825 + * Checks that the max transfer size allowed in a host channel is large enough
12826 + * to handle the maximum data transfer in a single (micro)frame for a periodic
12827 + * transfer.
12828 + *
12829 + * @param _hcd The HCD state structure for the DWC OTG controller.
12830 + * @param _qh QH for a periodic endpoint.
12831 + *
12832 + * @return 0 if successful, negative error code otherwise.
12833 + */
12834 +static int check_max_xfer_size(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
12835 +{
12836 +       int             status;
12837 +       uint32_t        max_xfer_size;
12838 +       uint32_t        max_channel_xfer_size;
12839 +
12840 +       status = 0;
12841 +
12842 +       max_xfer_size = dwc_max_packet(_qh->maxp) * dwc_hb_mult(_qh->maxp);
12843 +       max_channel_xfer_size = _hcd->core_if->core_params->max_transfer_size;
12844 +
12845 +       if (max_xfer_size > max_channel_xfer_size) {
12846 +               DWC_NOTICE("%s: Periodic xfer length %d > "
12847 +                           "max xfer length for channel %d\n",
12848 +                           __func__, max_xfer_size, max_channel_xfer_size);
12849 +               status = -ENOSPC;
12850 +       }
12851 +
12852 +       return status;
12853 +}
12854 +
12855 +/**
12856 + * Schedules an interrupt or isochronous transfer in the periodic schedule.
12857 + *
12858 + * @param _hcd The HCD state structure for the DWC OTG controller.
12859 + * @param _qh QH for the periodic transfer. The QH should already contain the
12860 + * scheduling information.
12861 + *
12862 + * @return 0 if successful, negative error code otherwise.
12863 + */
12864 +static int schedule_periodic(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
12865 +{
12866 +       int status = 0;
12867 +
12868 +       int frame;
12869 +       status = find_uframe(_hcd, _qh);
12870 +       frame = -1;
12871 +       if (status == 0) {
12872 +               frame = 7;
12873 +       } else {
12874 +               if (status > 0 )
12875 +                       frame = status-1;
12876 +       }
12877 +
12878 +       /* Set the new frame up */
12879 +       if (frame > -1) {
12880 +               _qh->sched_frame &= ~0x7;
12881 +               _qh->sched_frame |= (frame & 7);
12882 +       }
12883 +
12884 +       if (status != -1 )
12885 +               status = 0;
12886 +       if (status) {
12887 +               DWC_NOTICE("%s: Insufficient periodic bandwidth for "
12888 +                          "periodic transfer.\n", __func__);
12889 +               return status;
12890 +       }
12891 +
12892 +       status = check_max_xfer_size(_hcd, _qh);
12893 +       if (status) {
12894 +               DWC_NOTICE("%s: Channel max transfer size too small "
12895 +                           "for periodic transfer.\n", __func__);
12896 +               return status;
12897 +       }
12898 +
12899 +       /* Always start in the inactive schedule. */
12900 +       list_add_tail(&_qh->qh_list_entry, &_hcd->periodic_sched_inactive);
12901 +
12902 +
12903 +       /* Update claimed usecs per (micro)frame. */
12904 +       _hcd->periodic_usecs += _qh->usecs;
12905 +
12906 +       /* Update average periodic bandwidth claimed and # periodic reqs for usbfs. */
12907 +       hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_allocated += _qh->usecs / _qh->interval;
12908 +       if (_qh->ep_type == USB_ENDPOINT_XFER_INT) {
12909 +               hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_int_reqs++;
12910 +               DWC_DEBUGPL(DBG_HCD, "Scheduled intr: qh %p, usecs %d, period %d\n",
12911 +                           _qh, _qh->usecs, _qh->interval);
12912 +       } else {
12913 +               hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_isoc_reqs++;
12914 +               DWC_DEBUGPL(DBG_HCD, "Scheduled isoc: qh %p, usecs %d, period %d\n",
12915 +                           _qh, _qh->usecs, _qh->interval);
12916 +       }
12917 +               
12918 +       return status;
12919 +}
12920 +
12921 +/**
12922 + * This function adds a QH to either the non periodic or periodic schedule if
12923 + * it is not already in the schedule. If the QH is already in the schedule, no
12924 + * action is taken.
12925 + *
12926 + * @return 0 if successful, negative error code otherwise.
12927 + */
12928 +int dwc_otg_hcd_qh_add (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
12929 +{
12930 +       unsigned long flags;
12931 +       int status = 0;
12932 +
12933 +       local_irq_save(flags);
12934 +
12935 +       if (!list_empty(&_qh->qh_list_entry)) {
12936 +               /* QH already in a schedule. */
12937 +               goto done;
12938 +       }
12939 +
12940 +       /* Add the new QH to the appropriate schedule */
12941 +       if (dwc_qh_is_non_per(_qh)) {
12942 +               /* Always start in the inactive schedule. */
12943 +               list_add_tail(&_qh->qh_list_entry, &_hcd->non_periodic_sched_inactive);
12944 +       } else {
12945 +               status = schedule_periodic(_hcd, _qh);
12946 +       }
12947 +
12948 + done:
12949 +       local_irq_restore(flags);
12950 +
12951 +       return status;
12952 +}
12953 +
12954 +/**
12955 + * This function adds a QH to the non periodic deferred schedule.
12956 + *
12957 + * @return 0 if successful, negative error code otherwise.
12958 + */
12959 +int dwc_otg_hcd_qh_add_deferred(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
12960 +{
12961 +       unsigned long flags;
12962 +       local_irq_save(flags);
12963 +       if (!list_empty(&_qh->qh_list_entry)) {
12964 +               /* QH already in a schedule. */
12965 +               goto done;
12966 +       }
12967 +
12968 +       /* Add the new QH to the non periodic deferred schedule */
12969 +       if (dwc_qh_is_non_per(_qh)) {
12970 +               list_add_tail(&_qh->qh_list_entry,
12971 +                             &_hcd->non_periodic_sched_deferred);
12972 +       }
12973 +done:
12974 +       local_irq_restore(flags);
12975 +       return 0;
12976 +}
12977 +
12978 +/**
12979 + * Removes an interrupt or isochronous transfer from the periodic schedule.
12980 + *
12981 + * @param _hcd The HCD state structure for the DWC OTG controller.
12982 + * @param _qh QH for the periodic transfer.
12983 + */
12984 +static void deschedule_periodic(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
12985 +{
12986 +       int i;
12987 +       list_del_init(&_qh->qh_list_entry);
12988 +
12989 +
12990 +       /* Update claimed usecs per (micro)frame. */
12991 +       _hcd->periodic_usecs -= _qh->usecs;
12992 +
12993 +       for (i = 0; i < 8; i++) {
12994 +               _hcd->frame_usecs[i] += _qh->frame_usecs[i];
12995 +               _qh->frame_usecs[i] = 0;
12996 +       }
12997 +       /* Update average periodic bandwidth claimed and # periodic reqs for usbfs. */
12998 +       hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_allocated -= _qh->usecs / _qh->interval;
12999 +
13000 +       if (_qh->ep_type == USB_ENDPOINT_XFER_INT) {
13001 +               hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_int_reqs--;
13002 +               DWC_DEBUGPL(DBG_HCD, "Descheduled intr: qh %p, usecs %d, period %d\n",
13003 +                           _qh, _qh->usecs, _qh->interval);
13004 +       } else {
13005 +               hcd_to_bus(dwc_otg_hcd_to_hcd(_hcd))->bandwidth_isoc_reqs--;
13006 +               DWC_DEBUGPL(DBG_HCD, "Descheduled isoc: qh %p, usecs %d, period %d\n",
13007 +                           _qh, _qh->usecs, _qh->interval);
13008 +       }
13009 +}
13010 +
13011 +/** 
13012 + * Removes a QH from either the non-periodic or periodic schedule.  Memory is
13013 + * not freed.
13014 + *
13015 + * @param[in] _hcd The HCD state structure.
13016 + * @param[in] _qh QH to remove from schedule. */
13017 +void dwc_otg_hcd_qh_remove (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh)
13018 +{
13019 +       unsigned long flags;
13020 +
13021 +       local_irq_save(flags);
13022 +
13023 +       if (list_empty(&_qh->qh_list_entry)) {
13024 +               /* QH is not in a schedule. */
13025 +               goto done;
13026 +       }
13027 +
13028 +       if (dwc_qh_is_non_per(_qh)) {
13029 +               if (_hcd->non_periodic_qh_ptr == &_qh->qh_list_entry) {
13030 +                       _hcd->non_periodic_qh_ptr = _hcd->non_periodic_qh_ptr->next;
13031 +               }
13032 +               list_del_init(&_qh->qh_list_entry);
13033 +       } else {
13034 +               deschedule_periodic(_hcd, _qh);
13035 +       }
13036 +
13037 + done:
13038 +       local_irq_restore(flags);
13039 +}
13040 +
13041 +/**
13042 + * Defers a QH. For non-periodic QHs, removes the QH from the active
13043 + * non-periodic schedule. The QH is added to the deferred non-periodic
13044 + * schedule if any QTDs are still attached to the QH.
13045 + */
13046 +int dwc_otg_hcd_qh_deferr(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh, int delay)
13047 +{
13048 +        int deact = 1;
13049 +       unsigned long flags;
13050 +       local_irq_save(flags);
13051 +       if (dwc_qh_is_non_per(_qh)) {
13052 +               _qh->sched_frame =
13053 +                 dwc_frame_num_inc(_hcd->frame_number,
13054 +                                   delay);
13055 +               _qh->channel = NULL;
13056 +               _qh->qtd_in_process = NULL;
13057 +               deact = 0;
13058 +               dwc_otg_hcd_qh_remove(_hcd, _qh);
13059 +               if (!list_empty(&_qh->qtd_list)) {
13060 +                       /* Add back to deferred non-periodic schedule. */
13061 +                       dwc_otg_hcd_qh_add_deferred(_hcd, _qh);
13062 +               }
13063 +       }
13064 +       local_irq_restore(flags);
13065 +       return deact;
13066 +}
13067 +
13068 +/**
13069 + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
13070 + * non-periodic schedule. The QH is added to the inactive non-periodic
13071 + * schedule if any QTDs are still attached to the QH.
13072 + *
13073 + * For periodic QHs, the QH is removed from the periodic queued schedule. If
13074 + * there are any QTDs still attached to the QH, the QH is added to either the
13075 + * periodic inactive schedule or the periodic ready schedule and its next
13076 + * scheduled frame is calculated. The QH is placed in the ready schedule if
13077 + * the scheduled frame has been reached already. Otherwise it's placed in the
13078 + * inactive schedule. If there are no QTDs attached to the QH, the QH is
13079 + * completely removed from the periodic schedule.
13080 + */
13081 +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, int sched_next_periodic_split)
13082 +{
13083 +       unsigned long flags;
13084 +       local_irq_save(flags);
13085 +
13086 +       if (dwc_qh_is_non_per(_qh)) {
13087 +               dwc_otg_hcd_qh_remove(_hcd, _qh);
13088 +               if (!list_empty(&_qh->qtd_list)) {
13089 +                       /* Add back to inactive non-periodic schedule. */
13090 +                       dwc_otg_hcd_qh_add(_hcd, _qh);
13091 +               }
13092 +       } else {
13093 +               uint16_t frame_number = dwc_otg_hcd_get_frame_number(dwc_otg_hcd_to_hcd(_hcd));
13094 +
13095 +               if (_qh->do_split) {
13096 +                       /* Schedule the next continuing periodic split transfer */
13097 +                       if (sched_next_periodic_split) {
13098 +
13099 +                               _qh->sched_frame = frame_number;
13100 +                               if (dwc_frame_num_le(frame_number,
13101 +                                                    dwc_frame_num_inc(_qh->start_split_frame, 1))) {
13102 +                                       /*
13103 +                                        * Allow one frame to elapse after start
13104 +                                        * split microframe before scheduling
13105 +                                        * complete split, but DONT if we are
13106 +                                        * doing the next start split in the
13107 +                                        * same frame for an ISOC out.
13108 +                                        */
13109 +                                       if ((_qh->ep_type != USB_ENDPOINT_XFER_ISOC) || (_qh->ep_is_in != 0)) {
13110 +                                               _qh->sched_frame = dwc_frame_num_inc(_qh->sched_frame, 1);
13111 +                                       }
13112 +                               }
13113 +                       } else {
13114 +                               _qh->sched_frame = dwc_frame_num_inc(_qh->start_split_frame,
13115 +                                                                    _qh->interval);
13116 +                               if (dwc_frame_num_le(_qh->sched_frame, frame_number)) {
13117 +                                       _qh->sched_frame = frame_number;
13118 +                               }
13119 +                               _qh->sched_frame |= 0x7;
13120 +                               _qh->start_split_frame = _qh->sched_frame;
13121 +                       }
13122 +               } else {
13123 +                       _qh->sched_frame = dwc_frame_num_inc(_qh->sched_frame, _qh->interval);
13124 +                       if (dwc_frame_num_le(_qh->sched_frame, frame_number)) {
13125 +                               _qh->sched_frame = frame_number;
13126 +                       }
13127 +               }
13128 +
13129 +               if (list_empty(&_qh->qtd_list)) {
13130 +                       dwc_otg_hcd_qh_remove(_hcd, _qh);
13131 +               } else {
13132 +                       /*
13133 +                        * Remove from periodic_sched_queued and move to
13134 +                        * appropriate queue.
13135 +                        */
13136 +                       if (dwc_frame_num_le(_qh->sched_frame, frame_number)) {
13137 +                               list_move(&_qh->qh_list_entry,
13138 +                                         &_hcd->periodic_sched_ready);
13139 +                       } else {
13140 +                               list_move(&_qh->qh_list_entry,
13141 +                                         &_hcd->periodic_sched_inactive);
13142 +                       }
13143 +               }
13144 +       }
13145 +
13146 +       local_irq_restore(flags);
13147 +}
13148 +
13149 +/** 
13150 + * This function allocates and initializes a QTD. 
13151 + *
13152 + * @param[in] _urb The URB to create a QTD from.  Each URB-QTD pair will end up
13153 + * pointing to each other so each pair should have a unique correlation.
13154 + *
13155 + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
13156 +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create (struct urb *_urb)
13157 +{
13158 +       dwc_otg_qtd_t *qtd;
13159 +
13160 +       qtd = dwc_otg_hcd_qtd_alloc ();
13161 +       if (qtd == NULL) {
13162 +               return NULL;
13163 +       }
13164 +
13165 +       dwc_otg_hcd_qtd_init (qtd, _urb);
13166 +       return qtd;
13167 +}
13168 +
13169 +/** 
13170 + * Initializes a QTD structure.
13171 + *
13172 + * @param[in] _qtd The QTD to initialize.
13173 + * @param[in] _urb The URB to use for initialization.  */
13174 +void dwc_otg_hcd_qtd_init (dwc_otg_qtd_t *_qtd, struct urb *_urb)
13175 +{
13176 +       memset (_qtd, 0, sizeof (dwc_otg_qtd_t));
13177 +       _qtd->urb = _urb;
13178 +       if (usb_pipecontrol(_urb->pipe)) {
13179 +               /*
13180 +                * The only time the QTD data toggle is used is on the data
13181 +                * phase of control transfers. This phase always starts with
13182 +                * DATA1.
13183 +                */
13184 +               _qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
13185 +               _qtd->control_phase = DWC_OTG_CONTROL_SETUP;
13186 +       }
13187 +
13188 +       /* start split */
13189 +       _qtd->complete_split = 0;
13190 +       _qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
13191 +       _qtd->isoc_split_offset = 0;
13192 +
13193 +       /* Store the qtd ptr in the urb to reference what QTD. */
13194 +       _urb->hcpriv = _qtd;
13195 +       return;
13196 +}
13197 +
13198 +/**
13199 + * This function adds a QTD to the QTD-list of a QH.  It will find the correct
13200 + * QH to place the QTD into.  If it does not find a QH, then it will create a
13201 + * new QH. If the QH to which the QTD is added is not currently scheduled, it
13202 + * is placed into the proper schedule based on its EP type.
13203 + *
13204 + * @param[in] _qtd The QTD to add
13205 + * @param[in] _dwc_otg_hcd The DWC HCD structure
13206 + *
13207 + * @return 0 if successful, negative error code otherwise.
13208 + */
13209 +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * _qtd,  dwc_otg_hcd_t * _dwc_otg_hcd)
13210 +{
13211 +       struct usb_host_endpoint *ep;
13212 +       dwc_otg_qh_t *qh;
13213 +       unsigned long flags;
13214 +       int retval = 0;
13215 +       struct urb *urb = _qtd->urb;
13216 +
13217 +       local_irq_save(flags);
13218 +
13219 +       /*
13220 +        * Get the QH which holds the QTD-list to insert to. Create QH if it
13221 +        * doesn't exist.
13222 +        */
13223 +       ep = dwc_urb_to_endpoint(urb);
13224 +       qh = (dwc_otg_qh_t *)ep->hcpriv;
13225 +       if (qh == NULL) {
13226 +               qh = dwc_otg_hcd_qh_create (_dwc_otg_hcd, urb);
13227 +               if (qh == NULL) {
13228 +                       retval = -1;
13229 +                       goto done;
13230 +               }
13231 +               ep->hcpriv = qh;
13232 +       }
13233 +
13234 +       _qtd->qtd_qh_ptr = qh;
13235 +       retval = dwc_otg_hcd_qh_add(_dwc_otg_hcd, qh);
13236 +       if (retval == 0) {
13237 +               list_add_tail(&_qtd->qtd_list_entry, &qh->qtd_list);
13238 +       }
13239 +
13240 + done:
13241 +       local_irq_restore(flags);
13242 +       return retval;
13243 +}
13244 +
13245 +#endif /* DWC_DEVICE_ONLY */
13246 --- /dev/null
13247 +++ b/drivers/usb/dwc_otg/dwc_otg_ifx.c
13248 @@ -0,0 +1,105 @@
13249 +/******************************************************************************
13250 +**
13251 +** FILE NAME    : dwc_otg_ifx.c
13252 +** PROJECT      : Twinpass/Danube
13253 +** MODULES      : DWC OTG USB
13254 +**
13255 +** DATE         : 12 Auguest 2007
13256 +** AUTHOR       : Sung Winder
13257 +** DESCRIPTION  : Platform specific initialization.
13258 +** COPYRIGHT    :       Copyright (c) 2007
13259 +**                      Infineon Technologies AG
13260 +**                      2F, No.2, Li-Hsin Rd., Hsinchu Science Park,
13261 +**                      Hsin-chu City, 300 Taiwan.
13262 +**
13263 +**    This program is free software; you can redistribute it and/or modify
13264 +**    it under the terms of the GNU General Public License as published by
13265 +**    the Free Software Foundation; either version 2 of the License, or
13266 +**    (at your option) any later version.
13267 +**
13268 +** HISTORY
13269 +** $Date             $Author         $Comment
13270 +** 12 Auguest 2007   Sung Winder     Initiate Version
13271 +*******************************************************************************/
13272 +#include "dwc_otg_ifx.h"
13273 +
13274 +#include <linux/platform_device.h>
13275 +#include <linux/kernel.h>
13276 +#include <linux/ioport.h>
13277 +#include <linux/gpio.h>
13278 +
13279 +#include <asm/io.h>
13280 +//#include <asm/mach-ifxmips/ifxmips.h>
13281 +#include <xway.h>
13282 +
13283 +#define IFXMIPS_GPIO_BASE_ADDR  (0xBE100B00)
13284 +
13285 +#define IFXMIPS_GPIO_P0_OUT     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0010))
13286 +#define IFXMIPS_GPIO_P1_OUT     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0040))
13287 +#define IFXMIPS_GPIO_P0_IN      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0014))
13288 +#define IFXMIPS_GPIO_P1_IN      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0044))
13289 +#define IFXMIPS_GPIO_P0_DIR     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0018))
13290 +#define IFXMIPS_GPIO_P1_DIR     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0048))
13291 +#define IFXMIPS_GPIO_P0_ALTSEL0     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x001C))
13292 +#define IFXMIPS_GPIO_P1_ALTSEL0     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x004C))
13293 +#define IFXMIPS_GPIO_P0_ALTSEL1     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0020))
13294 +#define IFXMIPS_GPIO_P1_ALTSEL1     ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0050))
13295 +#define IFXMIPS_GPIO_P0_OD      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0024))
13296 +#define IFXMIPS_GPIO_P1_OD      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0054))
13297 +#define IFXMIPS_GPIO_P0_STOFF       ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0028))
13298 +#define IFXMIPS_GPIO_P1_STOFF       ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0058))
13299 +#define IFXMIPS_GPIO_P0_PUDSEL      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x002C))
13300 +#define IFXMIPS_GPIO_P1_PUDSEL      ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x005C))
13301 +#define IFXMIPS_GPIO_P0_PUDEN       ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0030))
13302 +#define IFXMIPS_GPIO_P1_PUDEN       ((u32 *)(IFXMIPS_GPIO_BASE_ADDR + 0x0060))
13303 +
13304 +
13305 +extern void lq_enable_irq(unsigned int irq_nr);
13306 +#define writel lq_w32
13307 +#define readl lq_r32
13308 +void dwc_otg_power_on (void)
13309 +{
13310 +       // clear power
13311 +       writel(readl(DANUBE_PMU_PWDCR) | 0x41, DANUBE_PMU_PWDCR);
13312 +       // set clock gating
13313 +       writel(readl(DANUBE_CGU_IFCCR) | 0x30, DANUBE_CGU_IFCCR);
13314 +       // set power
13315 +       writel(readl(DANUBE_PMU_PWDCR) & ~0x1, DANUBE_PMU_PWDCR);
13316 +       writel(readl(DANUBE_PMU_PWDCR) & ~0x40, DANUBE_PMU_PWDCR);
13317 +       writel(readl(DANUBE_PMU_PWDCR) & ~0x8000, DANUBE_PMU_PWDCR);
13318 +
13319 +#if 1//defined (DWC_HOST_ONLY)
13320 +       // make the hardware be a host controller (default)
13321 +       //clear_bit (DANUBE_USBCFG_HDSEL_BIT, (volatile unsigned long *)DANUBE_RCU_UBSCFG);
13322 +       writel(readl(DANUBE_RCU_UBSCFG) & ~(1<<DANUBE_USBCFG_HDSEL_BIT), DANUBE_RCU_UBSCFG);
13323 +
13324 +       //#elif defined (DWC_DEVICE_ONLY)
13325 +       /* set the controller to the device mode */
13326 +       //    set_bit (DANUBE_USBCFG_HDSEL_BIT, (volatile unsigned long *)DANUBE_RCU_UBSCFG);
13327 +#else
13328 +#error  "For Danube/Twinpass, it should be HOST or Device Only."
13329 +#endif
13330 +
13331 +       // set the HC's byte-order to big-endian
13332 +       //set_bit (DANUBE_USBCFG_HOST_END_BIT, (volatile unsigned long *)DANUBE_RCU_UBSCFG);
13333 +       writel(readl(DANUBE_RCU_UBSCFG) | (1<<DANUBE_USBCFG_HOST_END_BIT), DANUBE_RCU_UBSCFG);
13334 +       //clear_bit (DANUBE_USBCFG_SLV_END_BIT, (volatile unsigned long *)DANUBE_RCU_UBSCFG);
13335 +       writel(readl(DANUBE_RCU_UBSCFG) & ~(1<<DANUBE_USBCFG_SLV_END_BIT), DANUBE_RCU_UBSCFG);
13336 +       //writel(0x400, DANUBE_RCU_UBSCFG);
13337 +
13338 +       // PHY configurations.
13339 +       writel (0x14014, (volatile unsigned long *)0xbe10103c);
13340 +}
13341 +
13342 +static void release_platform_dev(struct device * dev)
13343 +{
13344 +}
13345 +
13346 +int ifx_usb_hc_init(unsigned long base_addr, int irq)
13347 +{
13348 +       return 0;
13349 +}
13350 +
13351 +void ifx_usb_hc_remove(void)
13352 +{
13353 +}
13354 --- /dev/null
13355 +++ b/drivers/usb/dwc_otg/dwc_otg_ifx.h
13356 @@ -0,0 +1,79 @@
13357 +/******************************************************************************
13358 +**
13359 +** FILE NAME    : dwc_otg_ifx.h
13360 +** PROJECT      : Twinpass/Danube
13361 +** MODULES      : DWC OTG USB
13362 +**
13363 +** DATE         : 12 April 2007
13364 +** AUTHOR       : Sung Winder
13365 +** DESCRIPTION  : Platform specific initialization.
13366 +** COPYRIGHT    :       Copyright (c) 2007
13367 +**                      Infineon Technologies AG
13368 +**                      2F, No.2, Li-Hsin Rd., Hsinchu Science Park,
13369 +**                      Hsin-chu City, 300 Taiwan.
13370 +**
13371 +**    This program is free software; you can redistribute it and/or modify
13372 +**    it under the terms of the GNU General Public License as published by
13373 +**    the Free Software Foundation; either version 2 of the License, or
13374 +**    (at your option) any later version.
13375 +**
13376 +** HISTORY
13377 +** $Date          $Author         $Comment
13378 +** 12 April 2007   Sung Winder     Initiate Version
13379 +*******************************************************************************/
13380 +#if !defined(__DWC_OTG_IFX_H__)
13381 +#define __DWC_OTG_IFX_H__
13382 +
13383 +#include <irq.h>
13384 +
13385 +// 20070316, winder added.
13386 +#ifndef SZ_256K
13387 +#define SZ_256K                         0x00040000
13388 +#endif
13389 +
13390 +extern void dwc_otg_power_on (void);
13391 +
13392 +/* FIXME: The current Linux-2.6 do not have these header files, but anyway, we need these. */
13393 +// #include <asm/danube/danube.h>
13394 +// #include <asm/ifx/irq.h>
13395 +
13396 +/* winder, I used the Danube parameter as default. *
13397 + * We could change this through module param.      */
13398 +#define IFX_USB_IOMEM_BASE 0x1e101000
13399 +#define IFX_USB_IOMEM_SIZE SZ_256K
13400 +#define IFX_USB_IRQ LQ_USB_INT
13401 +
13402 +/**
13403 + * This function is called to set correct clock gating and power.
13404 + * For Twinpass/Danube board.
13405 + */
13406 +#ifndef DANUBE_RCU_BASE_ADDR
13407 +#define DANUBE_RCU_BASE_ADDR            (0xBF203000)
13408 +#endif
13409 +
13410 +#ifndef DANUBE_CGU
13411 +#define DANUBE_CGU                          (0xBF103000)
13412 +#endif
13413 +#ifndef DANUBE_CGU_IFCCR
13414 +/***CGU Interface Clock Control Register***/
13415 +#define DANUBE_CGU_IFCCR                        ((volatile u32*)(DANUBE_CGU+ 0x0018))
13416 +#endif
13417 +
13418 +#ifndef DANUBE_PMU
13419 +#define DANUBE_PMU                              (KSEG1+0x1F102000)
13420 +#endif
13421 +#ifndef DANUBE_PMU_PWDCR
13422 +/* PMU Power down Control Register */
13423 +#define DANUBE_PMU_PWDCR                        ((volatile u32*)(DANUBE_PMU+0x001C))
13424 +#endif
13425 +
13426 +
13427 +#define DANUBE_RCU_UBSCFG  ((volatile u32*)(DANUBE_RCU_BASE_ADDR + 0x18))
13428 +#define DANUBE_USBCFG_HDSEL_BIT    11  // 0:host, 1:device
13429 +#define DANUBE_USBCFG_HOST_END_BIT 10  // 0:little_end, 1:big_end
13430 +#define DANUBE_USBCFG_SLV_END_BIT  9   // 0:little_end, 1:big_end
13431 +
13432 +extern void lq_mask_and_ack_irq (unsigned int irq_nr);
13433 +#define mask_and_ack_ifx_irq lq_mask_and_ack_irq
13434 +
13435 +#endif //__DWC_OTG_IFX_H__
13436 --- /dev/null
13437 +++ b/drivers/usb/dwc_otg/dwc_otg_plat.h
13438 @@ -0,0 +1,269 @@
13439 +/* ==========================================================================
13440 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/platform/dwc_otg_plat.h $
13441 + * $Revision: 1.1.1.1 $
13442 + * $Date: 2009-04-17 06:15:34 $
13443 + * $Change: 510301 $
13444 + *
13445 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
13446 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
13447 + * otherwise expressly agreed to in writing between Synopsys and you.
13448 + * 
13449 + * The Software IS NOT an item of Licensed Software or Licensed Product under
13450 + * any End User Software License Agreement or Agreement for Licensed Product
13451 + * with Synopsys or any supplement thereto. You are permitted to use and
13452 + * redistribute this Software in source and binary forms, with or without
13453 + * modification, provided that redistributions of source code must retain this
13454 + * notice. You may not view, use, disclose, copy or distribute this file or
13455 + * any information contained herein except pursuant to this license grant from
13456 + * Synopsys. If you do not agree with this notice, including the disclaimer
13457 + * below, then you are not authorized to use the Software.
13458 + * 
13459 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
13460 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13461 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13462 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
13463 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13464 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
13465 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
13466 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13467 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13468 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
13469 + * DAMAGE.
13470 + * ========================================================================== */
13471 +
13472 +#if !defined(__DWC_OTG_PLAT_H__)
13473 +#define __DWC_OTG_PLAT_H__
13474 +
13475 +#include <linux/types.h>
13476 +#include <linux/slab.h>
13477 +#include <linux/list.h>
13478 +#include <linux/delay.h>
13479 +#include <asm/io.h>
13480 +
13481 +/**
13482 + * @file 
13483 + *
13484 + * This file contains the Platform Specific constants, interfaces
13485 + * (functions and macros) for Linux.
13486 + *
13487 + */
13488 +/*#if !defined(__LINUX__)
13489 +#error "The contents of this file is Linux specific!!!"
13490 +#endif
13491 +*/
13492 +#include <xway.h>
13493 +#define writel lq_w32
13494 +#define readl lq_r32
13495 +
13496 +/**
13497 + * Reads the content of a register.
13498 + *
13499 + * @param _reg address of register to read.
13500 + * @return contents of the register.
13501 + *
13502 +
13503 + * Usage:<br>
13504 + * <code>uint32_t dev_ctl = dwc_read_reg32(&dev_regs->dctl);</code> 
13505 + */
13506 +static __inline__ uint32_t dwc_read_reg32( volatile uint32_t *_reg) 
13507 +{
13508 +        return readl(_reg);
13509 +};
13510 +
13511 +/** 
13512 + * Writes a register with a 32 bit value.
13513 + *
13514 + * @param _reg address of register to read.
13515 + * @param _value to write to _reg.
13516 + *
13517 + * Usage:<br>
13518 + * <code>dwc_write_reg32(&dev_regs->dctl, 0); </code>
13519 + */
13520 +static __inline__ void dwc_write_reg32( volatile uint32_t *_reg, const uint32_t _value) 
13521 +{
13522 +        writel( _value, _reg );
13523 +};
13524 +
13525 +/**  
13526 + * This function modifies bit values in a register.  Using the
13527 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13528 + *
13529 + * @param _reg address of register to read.
13530 + * @param _clear_mask bit mask to be cleared.
13531 + * @param _set_mask bit mask to be set.
13532 + *
13533 + * Usage:<br> 
13534 + * <code> // Clear the SOF Interrupt Mask bit and <br>
13535 + * // set the OTG Interrupt mask bit, leaving all others as they were.
13536 + *    dwc_modify_reg32(&dev_regs->gintmsk, DWC_SOF_INT, DWC_OTG_INT);</code>
13537 + */
13538 +static __inline__
13539 + void dwc_modify_reg32( volatile uint32_t *_reg, const uint32_t _clear_mask, const uint32_t _set_mask) 
13540 +{
13541 +        writel( (readl(_reg) & ~_clear_mask) | _set_mask, _reg );  
13542 +};
13543 +
13544 +
13545 +/**
13546 + * Wrapper for the OS micro-second delay function.
13547 + * @param[in] _usecs Microseconds of delay
13548 + */
13549 +static __inline__ void UDELAY( const uint32_t _usecs ) 
13550 +{
13551 +        udelay( _usecs );
13552 +}
13553 +
13554 +/**
13555 + * Wrapper for the OS milli-second delay function.
13556 + * @param[in] _msecs milliseconds of delay
13557 + */
13558 +static __inline__ void MDELAY( const uint32_t _msecs ) 
13559 +{
13560 +        mdelay( _msecs );
13561 +}
13562 +
13563 +/**
13564 + * Wrapper for the Linux spin_lock.  On the ARM (Integrator)
13565 + * spin_lock() is a nop.
13566 + *
13567 + * @param _lock Pointer to the spinlock.
13568 + */
13569 +static __inline__ void SPIN_LOCK( spinlock_t *_lock )  
13570 +{
13571 +        spin_lock(_lock);
13572 +}
13573 +
13574 +/**
13575 + * Wrapper for the Linux spin_unlock.  On the ARM (Integrator)
13576 + * spin_lock() is a nop.
13577 + *
13578 + * @param _lock Pointer to the spinlock.
13579 + */
13580 +static __inline__ void SPIN_UNLOCK( spinlock_t *_lock )     
13581 +{ 
13582 +        spin_unlock(_lock);
13583 +}
13584 +
13585 +/**
13586 + * Wrapper (macro) for the Linux spin_lock_irqsave.  On the ARM
13587 + * (Integrator) spin_lock() is a nop.
13588 + *
13589 + * @param _l Pointer to the spinlock.
13590 + * @param _f unsigned long for irq flags storage.
13591 + */
13592 +#define SPIN_LOCK_IRQSAVE( _l, _f )  { \
13593 +       spin_lock_irqsave(_l,_f); \
13594 +       }
13595 +
13596 +/**
13597 + * Wrapper (macro) for the Linux spin_unlock_irqrestore.  On the ARM
13598 + * (Integrator) spin_lock() is a nop.
13599 + *
13600 + * @param _l Pointer to the spinlock.
13601 + * @param _f unsigned long for irq flags storage.
13602 + */
13603 +#define SPIN_UNLOCK_IRQRESTORE( _l,_f ) {\
13604 +       spin_unlock_irqrestore(_l,_f);  \
13605 +       }
13606 +
13607 +
13608 +/*
13609 + * Debugging support vanishes in non-debug builds.  
13610 + */
13611 +
13612 +
13613 +/**
13614 + * The Debug Level bit-mask variable.
13615 + */
13616 +extern uint32_t g_dbg_lvl;
13617 +/**
13618 + * Set the Debug Level variable.
13619 + */
13620 +static inline uint32_t SET_DEBUG_LEVEL( const uint32_t _new )
13621 +{
13622 +        uint32_t old = g_dbg_lvl;
13623 +        g_dbg_lvl = _new;
13624 +        return old;
13625 +}
13626 +
13627 +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
13628 +#define DBG_CIL                (0x2)
13629 +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
13630 + * messages */
13631 +#define DBG_CILV       (0x20)
13632 +/**  When debug level has the DBG_PCD bit set, display PCD (Device) debug
13633 + *  messages */
13634 +#define DBG_PCD                (0x4)   
13635 +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
13636 + * messages */
13637 +#define DBG_PCDV       (0x40)  
13638 +/** When debug level has the DBG_HCD bit set, display Host debug messages */
13639 +#define DBG_HCD                (0x8)   
13640 +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
13641 + * messages */
13642 +#define DBG_HCDV       (0x80)
13643 +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
13644 + *  mode. */
13645 +#define DBG_HCD_URB    (0x800)
13646 +
13647 +/** When debug level has any bit set, display debug messages */
13648 +#define DBG_ANY                (0xFF)
13649 +
13650 +/** All debug messages off */
13651 +#define DBG_OFF                0
13652 +
13653 +/** Prefix string for DWC_DEBUG print macros. */
13654 +#define USB_DWC "DWC_otg: "
13655 +
13656 +/** 
13657 + * Print a debug message when the Global debug level variable contains
13658 + * the bit defined in <code>lvl</code>.
13659 + *
13660 + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
13661 + * @param[in] x - like printf
13662 + *
13663 + *    Example:<p>
13664 + * <code>
13665 + *      DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
13666 + * </code>
13667 + * <br>
13668 + * results in:<br> 
13669 + * <code>
13670 + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
13671 + * </code>
13672 + */
13673 +#ifdef DEBUG
13674 +
13675 +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)printk( KERN_DEBUG USB_DWC x ); }while(0)
13676 +# define DWC_DEBUGP(x...)      DWC_DEBUGPL(DBG_ANY, x )
13677 +
13678 +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
13679 +
13680 +#else
13681 +
13682 +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
13683 +# define DWC_DEBUGP(x...)
13684 +
13685 +# define CHK_DEBUG_LEVEL(level) (0)
13686 +
13687 +#endif /*DEBUG*/
13688 +
13689 +/**
13690 + * Print an Error message.
13691 + */
13692 +#define DWC_ERROR(x...) printk( KERN_ERR USB_DWC x )
13693 +/**
13694 + * Print a Warning message.
13695 + */
13696 +#define DWC_WARN(x...) printk( KERN_WARNING USB_DWC x )
13697 +/**
13698 + * Print a notice (normal but significant message).
13699 + */
13700 +#define DWC_NOTICE(x...) printk( KERN_NOTICE USB_DWC x )
13701 +/**
13702 + *  Basic message printing.
13703 + */
13704 +#define DWC_PRINT(x...) printk( KERN_INFO USB_DWC x )
13705 +
13706 +#endif
13707 +
13708 --- /dev/null
13709 +++ b/drivers/usb/dwc_otg/dwc_otg_regs.h
13710 @@ -0,0 +1,1797 @@
13711 +/* ==========================================================================
13712 + * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_regs.h $
13713 + * $Revision: 1.1.1.1 $
13714 + * $Date: 2009-04-17 06:15:34 $
13715 + * $Change: 631780 $
13716 + *
13717 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
13718 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
13719 + * otherwise expressly agreed to in writing between Synopsys and you.
13720 + * 
13721 + * The Software IS NOT an item of Licensed Software or Licensed Product under
13722 + * any End User Software License Agreement or Agreement for Licensed Product
13723 + * with Synopsys or any supplement thereto. You are permitted to use and
13724 + * redistribute this Software in source and binary forms, with or without
13725 + * modification, provided that redistributions of source code must retain this
13726 + * notice. You may not view, use, disclose, copy or distribute this file or
13727 + * any information contained herein except pursuant to this license grant from
13728 + * Synopsys. If you do not agree with this notice, including the disclaimer
13729 + * below, then you are not authorized to use the Software.
13730 + * 
13731 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
13732 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13733 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13734 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
13735 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13736 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
13737 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
13738 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13739 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13740 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
13741 + * DAMAGE.
13742 + * ========================================================================== */
13743 +
13744 +#ifndef __DWC_OTG_REGS_H__
13745 +#define __DWC_OTG_REGS_H__
13746 +
13747 +/**
13748 + * @file
13749 + *
13750 + * This file contains the data structures for accessing the DWC_otg core registers.
13751 + *
13752 + * The application interfaces with the HS OTG core by reading from and
13753 + * writing to the Control and Status Register (CSR) space through the
13754 + * AHB Slave interface. These registers are 32 bits wide, and the
13755 + * addresses are 32-bit-block aligned.
13756 + * CSRs are classified as follows:
13757 + * - Core Global Registers
13758 + * - Device Mode Registers
13759 + * - Device Global Registers
13760 + * - Device Endpoint Specific Registers
13761 + * - Host Mode Registers
13762 + * - Host Global Registers
13763 + * - Host Port CSRs
13764 + * - Host Channel Specific Registers
13765 + *
13766 + * Only the Core Global registers can be accessed in both Device and
13767 + * Host modes. When the HS OTG core is operating in one mode, either
13768 + * Device or Host, the application must not access registers from the
13769 + * other mode. When the core switches from one mode to another, the
13770 + * registers in the new mode of operation must be reprogrammed as they
13771 + * would be after a power-on reset.
13772 + */
13773 +
13774 +/****************************************************************************/
13775 +/** DWC_otg Core registers .  
13776 + * The dwc_otg_core_global_regs structure defines the size
13777 + * and relative field offsets for the Core Global registers.
13778 + */
13779 +typedef struct dwc_otg_core_global_regs 
13780 +{
13781 +        /** OTG Control and Status Register.  <i>Offset: 000h</i> */
13782 +        volatile uint32_t gotgctl; 
13783 +        /** OTG Interrupt Register.  <i>Offset: 004h</i> */
13784 +        volatile uint32_t gotgint; 
13785 +        /**Core AHB Configuration Register.  <i>Offset: 008h</i> */
13786 +        volatile uint32_t gahbcfg; 
13787 +#define DWC_GLBINTRMASK        0x0001
13788 +#define DWC_DMAENABLE          0x0020
13789 +#define DWC_NPTXEMPTYLVL_EMPTY         0x0080
13790 +#define DWC_NPTXEMPTYLVL_HALFEMPTY     0x0000
13791 +#define DWC_PTXEMPTYLVL_EMPTY  0x0100
13792 +#define DWC_PTXEMPTYLVL_HALFEMPTY      0x0000
13793 +
13794 +
13795 +        /**Core USB Configuration Register.  <i>Offset: 00Ch</i> */
13796 +        volatile uint32_t gusbcfg; 
13797 +        /**Core Reset Register.  <i>Offset: 010h</i> */
13798 +        volatile uint32_t grstctl; 
13799 +        /**Core Interrupt Register.  <i>Offset: 014h</i> */
13800 +        volatile uint32_t gintsts; 
13801 +        /**Core Interrupt Mask Register.  <i>Offset: 018h</i> */
13802 +        volatile uint32_t gintmsk; 
13803 +        /**Receive Status Queue Read Register (Read Only).  <i>Offset: 01Ch</i> */
13804 +        volatile uint32_t grxstsr; 
13805 +        /**Receive Status Queue Read & POP Register (Read Only).  <i>Offset: 020h</i>*/
13806 +        volatile uint32_t grxstsp; 
13807 +        /**Receive FIFO Size Register.  <i>Offset: 024h</i> */
13808 +        volatile uint32_t grxfsiz; 
13809 +        /**Non Periodic Transmit FIFO Size Register.  <i>Offset: 028h</i> */
13810 +        volatile uint32_t gnptxfsiz; 
13811 +        /**Non Periodic Transmit FIFO/Queue Status Register (Read
13812 +         * Only). <i>Offset: 02Ch</i> */
13813 +        volatile uint32_t gnptxsts; 
13814 +        /**I2C Access Register.  <i>Offset: 030h</i> */
13815 +        volatile uint32_t gi2cctl; 
13816 +        /**PHY Vendor Control Register.  <i>Offset: 034h</i> */
13817 +        volatile uint32_t gpvndctl;
13818 +        /**General Purpose Input/Output Register.  <i>Offset: 038h</i> */
13819 +        volatile uint32_t ggpio; 
13820 +        /**User ID Register.  <i>Offset: 03Ch</i> */
13821 +        volatile uint32_t guid; 
13822 +        /**Synopsys ID Register (Read Only).  <i>Offset: 040h</i> */
13823 +        volatile uint32_t gsnpsid;
13824 +        /**User HW Config1 Register (Read Only).  <i>Offset: 044h</i> */
13825 +        volatile uint32_t ghwcfg1; 
13826 +        /**User HW Config2 Register (Read Only).  <i>Offset: 048h</i> */
13827 +        volatile uint32_t ghwcfg2;
13828 +#define DWC_SLAVE_ONLY_ARCH 0
13829 +#define DWC_EXT_DMA_ARCH 1
13830 +#define DWC_INT_DMA_ARCH 2
13831 +
13832 +#define DWC_MODE_HNP_SRP_CAPABLE       0
13833 +#define DWC_MODE_SRP_ONLY_CAPABLE      1
13834 +#define DWC_MODE_NO_HNP_SRP_CAPABLE    2
13835 +#define DWC_MODE_SRP_CAPABLE_DEVICE    3
13836 +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE  4
13837 +#define DWC_MODE_SRP_CAPABLE_HOST      5
13838 +#define DWC_MODE_NO_SRP_CAPABLE_HOST   6
13839 +
13840 +        /**User HW Config3 Register (Read Only).  <i>Offset: 04Ch</i> */
13841 +        volatile uint32_t ghwcfg3;
13842 +        /**User HW Config4 Register (Read Only).  <i>Offset: 050h</i>*/
13843 +        volatile uint32_t ghwcfg4;
13844 +        /** Reserved  <i>Offset: 054h-0FFh</i> */
13845 +        uint32_t reserved[43];
13846 +        /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
13847 +        volatile uint32_t hptxfsiz;
13848 +       /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
13849 +               otherwise Device Transmit FIFO#n Register.
13850 +         * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
13851 +        //volatile uint32_t dptxfsiz[15];
13852 +       volatile uint32_t dptxfsiz_dieptxf[15];
13853 +} dwc_otg_core_global_regs_t;
13854 +
13855 +/**
13856 + * This union represents the bit fields of the Core OTG Control
13857 + * and Status Register (GOTGCTL).  Set the bits using the bit 
13858 + * fields then write the <i>d32</i> value to the register.
13859 + */
13860 +typedef union gotgctl_data
13861 +{
13862 +        /** raw register data */
13863 +        uint32_t d32;
13864 +        /** register bits */
13865 +        struct 
13866 +        {
13867 +               unsigned reserved31_21 : 11;
13868 +               unsigned currmod : 1;
13869 +               unsigned bsesvld : 1;
13870 +               unsigned asesvld : 1;
13871 +               unsigned reserved17 : 1;
13872 +               unsigned conidsts : 1;
13873 +               unsigned reserved15_12 : 4;
13874 +               unsigned devhnpen : 1;
13875 +               unsigned hstsethnpen : 1;
13876 +               unsigned hnpreq : 1;
13877 +               unsigned hstnegscs : 1;
13878 +               unsigned reserved7_2 : 6;
13879 +        unsigned sesreq : 1;
13880 +        unsigned sesreqscs : 1;
13881 +        } b;
13882 +} gotgctl_data_t;
13883 +
13884 +/**
13885 + * This union represents the bit fields of the Core OTG Interrupt Register
13886 + * (GOTGINT).  Set/clear the bits using the bit fields then write the <i>d32</i>
13887 + * value to the register.
13888 + */
13889 +typedef union gotgint_data
13890 +{
13891 +        /** raw register data */
13892 +        uint32_t d32;
13893 +        /** register bits */
13894 +        struct 
13895 +        {
13896 +               /** Current Mode */
13897 +               unsigned reserved31_20 : 12;
13898 +               /** Debounce Done */
13899 +               unsigned debdone : 1;
13900 +               /** A-Device Timeout Change */
13901 +               unsigned adevtoutchng : 1;
13902 +               /** Host Negotiation Detected */
13903 +               unsigned hstnegdet : 1;
13904 +               unsigned reserver16_10 : 7;
13905 +               /** Host Negotiation Success Status Change */
13906 +               unsigned hstnegsucstschng : 1;
13907 +               /** Session Request Success Status Change */
13908 +               unsigned sesreqsucstschng : 1;
13909 +               unsigned reserved3_7 : 5;
13910 +               /** Session End Detected */
13911 +               unsigned sesenddet : 1;
13912 +               /** Current Mode */
13913 +               unsigned reserved1_0 : 2;
13914 +        } b;
13915 +} gotgint_data_t;
13916 +
13917 +
13918 +/**
13919 + * This union represents the bit fields of the Core AHB Configuration
13920 + * Register (GAHBCFG).  Set/clear the bits using the bit fields then
13921 + * write the <i>d32</i> value to the register.
13922 + */
13923 +typedef union gahbcfg_data
13924 +{
13925 +        /** raw register data */
13926 +        uint32_t d32;
13927 +        /** register bits */
13928 +        struct 
13929 +        {
13930 +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY          1
13931 +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY      0
13932 +                unsigned reserved9_31 : 23;
13933 +                unsigned ptxfemplvl : 1;
13934 +                unsigned nptxfemplvl_txfemplvl : 1;
13935 +#define DWC_GAHBCFG_DMAENABLE                  1
13936 +                unsigned reserved : 1;
13937 +                unsigned dmaenable : 1;
13938 +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE       0
13939 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR                 1
13940 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4        3
13941 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8        5
13942 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16       7
13943 +                unsigned hburstlen : 4;
13944 +                unsigned glblintrmsk : 1;
13945 +#define DWC_GAHBCFG_GLBINT_ENABLE              1
13946 +
13947 +        } b;
13948 +} gahbcfg_data_t;
13949 +
13950 +/**
13951 + * This union represents the bit fields of the Core USB Configuration
13952 + * Register (GUSBCFG).  Set the bits using the bit fields then write
13953 + * the <i>d32</i> value to the register.
13954 + */
13955 +typedef union gusbcfg_data
13956 +{
13957 +        /** raw register data */
13958 +        uint32_t d32;
13959 +        /** register bits */
13960 +        struct 
13961 +        {
13962 +       unsigned corrupt_tx_packet: 1;          /*fscz*/
13963 +               unsigned force_device_mode: 1;
13964 +               unsigned force_host_mode: 1;
13965 +               unsigned reserved23_28 : 6;
13966 +               unsigned term_sel_dl_pulse : 1;
13967 +                unsigned ulpi_int_vbus_indicator : 1;
13968 +                unsigned ulpi_ext_vbus_drv : 1;
13969 +               unsigned ulpi_clk_sus_m : 1;
13970 +               unsigned ulpi_auto_res : 1;
13971 +               unsigned ulpi_fsls : 1;
13972 +                unsigned otgutmifssel : 1;
13973 +                unsigned phylpwrclksel : 1;
13974 +                unsigned nptxfrwnden : 1;
13975 +                unsigned usbtrdtim : 4;
13976 +                unsigned hnpcap : 1;
13977 +                unsigned srpcap : 1;
13978 +                unsigned ddrsel : 1;
13979 +                unsigned physel : 1;
13980 +                unsigned fsintf : 1;
13981 +                unsigned ulpi_utmi_sel : 1;
13982 +                unsigned phyif : 1;
13983 +                unsigned toutcal : 3;
13984 +        } b;
13985 +} gusbcfg_data_t;
13986 +
13987 +/**
13988 + * This union represents the bit fields of the Core Reset Register
13989 + * (GRSTCTL).  Set/clear the bits using the bit fields then write the
13990 + * <i>d32</i> value to the register.
13991 + */
13992 +typedef union grstctl_data
13993 +{
13994 +        /** raw register data */
13995 +        uint32_t d32;
13996 +        /** register bits */
13997 +        struct 
13998 +        {
13999 +                /** AHB Master Idle.  Indicates the AHB Master State
14000 +                 * Machine is in IDLE condition. */
14001 +                unsigned ahbidle : 1;                
14002 +                /** DMA Request Signal.  Indicated DMA request is in
14003 +                 * probress.  Used for debug purpose. */
14004 +                unsigned dmareq : 1;
14005 +                /** Reserved */
14006 +                       unsigned reserved29_11 : 19;
14007 +                /** TxFIFO Number (TxFNum) (Device and Host).
14008 +                 * 
14009 +                 * This is the FIFO number which needs to be flushed,
14010 +                 * using the TxFIFO Flush bit. This field should not
14011 +                 * be changed until the TxFIFO Flush bit is cleared by
14012 +                 * the core.
14013 +                 *   - 0x0 : Non Periodic TxFIFO Flush
14014 +                 *   - 0x1 : Periodic TxFIFO #1 Flush in device mode
14015 +                 *     or Periodic TxFIFO in host mode
14016 +                 *   - 0x2 : Periodic TxFIFO #2 Flush in device mode.
14017 +                 *   - ...
14018 +                 *   - 0xF : Periodic TxFIFO #15 Flush in device mode
14019 +                 *   - 0x10: Flush all the Transmit NonPeriodic and
14020 +                 *     Transmit Periodic FIFOs in the core
14021 +                 */
14022 +                unsigned txfnum : 5;
14023 +                /** TxFIFO Flush (TxFFlsh) (Device and Host).  
14024 +                 *
14025 +                 * This bit is used to selectively flush a single or
14026 +                 * all transmit FIFOs.  The application must first
14027 +                 * ensure that the core is not in the middle of a
14028 +                 * transaction.  <p>The application should write into
14029 +                 * this bit, only after making sure that neither the
14030 +                 * DMA engine is writing into the TxFIFO nor the MAC
14031 +                 * is reading the data out of the FIFO.  <p>The
14032 +                 * application should wait until the core clears this
14033 +                 * bit, before performing any operations. This bit
14034 +                 * will takes 8 clocks (slowest of PHY or AHB clock)
14035 +                 * to clear.
14036 +                 */
14037 +                unsigned txfflsh : 1;
14038 +                /** RxFIFO Flush (RxFFlsh) (Device and Host)
14039 +                 *
14040 +                 * The application can flush the entire Receive FIFO
14041 +                 * using this bit.  <p>The application must first
14042 +                 * ensure that the core is not in the middle of a
14043 +                 * transaction.  <p>The application should write into
14044 +                 * this bit, only after making sure that neither the
14045 +                 * DMA engine is reading from the RxFIFO nor the MAC
14046 +                 * is writing the data in to the FIFO.  <p>The
14047 +                 * application should wait until the bit is cleared
14048 +                 * before performing any other operations. This bit
14049 +                 * will takes 8 clocks (slowest of PHY or AHB clock)
14050 +                 * to clear.
14051 +                 */
14052 +                unsigned rxfflsh : 1;
14053 +                /** In Token Sequence Learning Queue Flush
14054 +                 * (INTknQFlsh) (Device Only)
14055 +                 */
14056 +                unsigned intknqflsh : 1;
14057 +                /** Host Frame Counter Reset (Host Only)<br>
14058 +                 * 
14059 +                 * The application can reset the (micro)frame number
14060 +                 * counter inside the core, using this bit. When the
14061 +                 * (micro)frame counter is reset, the subsequent SOF
14062 +                 * sent out by the core, will have a (micro)frame
14063 +                 * number of 0.
14064 +                 */
14065 +                unsigned hstfrm : 1;
14066 +                /** Hclk Soft Reset
14067 +                *
14068 +                * The application uses this bit to reset the control logic in
14069 +                * the AHB clock domain. Only AHB clock domain pipelines are
14070 +                * reset.
14071 +                */
14072 +                unsigned hsftrst : 1;
14073 +                /** Core Soft Reset (CSftRst) (Device and Host)
14074 +                 *
14075 +                 * The application can flush the control logic in the
14076 +                 * entire core using this bit. This bit resets the
14077 +                 * pipelines in the AHB Clock domain as well as the
14078 +                 * PHY Clock domain.
14079 +                 *
14080 +                 * The state machines are reset to an IDLE state, the
14081 +                 * control bits in the CSRs are cleared, all the
14082 +                 * transmit FIFOs and the receive FIFO are flushed.
14083 +                 *
14084 +                 * The status mask bits that control the generation of
14085 +                 * the interrupt, are cleared, to clear the
14086 +                 * interrupt. The interrupt status bits are not
14087 +                 * cleared, so the application can get the status of
14088 +                 * any events that occurred in the core after it has
14089 +                 * set this bit.
14090 +                 *
14091 +                 * Any transactions on the AHB are terminated as soon
14092 +                 * as possible following the protocol. Any
14093 +                 * transactions on the USB are terminated immediately.
14094 +                 *
14095 +                 * The configuration settings in the CSRs are
14096 +                 * unchanged, so the software doesn't have to
14097 +                 * reprogram these registers (Device
14098 +                 * Configuration/Host Configuration/Core System
14099 +                 * Configuration/Core PHY Configuration).
14100 +                 *
14101 +                 * The application can write to this bit, any time it
14102 +                 * wants to reset the core. This is a self clearing
14103 +                 * bit and the core clears this bit after all the
14104 +                 * necessary logic is reset in the core, which may
14105 +                 * take several clocks, depending on the current state
14106 +                 * of the core.
14107 +                 */
14108 +                unsigned csftrst : 1;
14109 +        } b;
14110 +} grstctl_t;
14111 +
14112 +
14113 +/**
14114 + * This union represents the bit fields of the Core Interrupt Mask
14115 + * Register (GINTMSK).  Set/clear the bits using the bit fields then
14116 + * write the <i>d32</i> value to the register.
14117 + */
14118 +typedef union gintmsk_data
14119 +{
14120 +        /** raw register data */
14121 +        uint32_t d32;
14122 +        /** register bits */
14123 +        struct 
14124 +        {
14125 +                unsigned wkupintr : 1;
14126 +                unsigned sessreqintr : 1;
14127 +                unsigned disconnect : 1;
14128 +                unsigned conidstschng : 1;
14129 +                unsigned reserved27 : 1;
14130 +                unsigned ptxfempty : 1;
14131 +                unsigned hcintr : 1;
14132 +                unsigned portintr : 1;
14133 +                unsigned reserved22_23 : 2;
14134 +                unsigned incomplisoout : 1;
14135 +                unsigned incomplisoin : 1;
14136 +                unsigned outepintr : 1;
14137 +                unsigned inepintr : 1;
14138 +                unsigned epmismatch : 1;
14139 +                unsigned reserved16 : 1;
14140 +                unsigned eopframe : 1;
14141 +                unsigned isooutdrop : 1;
14142 +                unsigned enumdone : 1;
14143 +                unsigned usbreset : 1;
14144 +                unsigned usbsuspend : 1;
14145 +                unsigned erlysuspend : 1;
14146 +                unsigned i2cintr : 1;
14147 +                unsigned reserved8 : 1;
14148 +                unsigned goutnakeff : 1;
14149 +                unsigned ginnakeff : 1;
14150 +                unsigned nptxfempty : 1;
14151 +                unsigned rxstsqlvl : 1;
14152 +                unsigned sofintr : 1;
14153 +                unsigned otgintr : 1;
14154 +                unsigned modemismatch : 1;
14155 +                unsigned reserved0 : 1;
14156 +        } b;
14157 +} gintmsk_data_t;
14158 +/**
14159 + * This union represents the bit fields of the Core Interrupt Register
14160 + * (GINTSTS).  Set/clear the bits using the bit fields then write the
14161 + * <i>d32</i> value to the register.
14162 + */
14163 +typedef union gintsts_data
14164 +{
14165 +        /** raw register data */
14166 +        uint32_t d32;
14167 +#define DWC_SOF_INTR_MASK 0x0008
14168 +        /** register bits */
14169 +        struct 
14170 +        {
14171 +#define DWC_HOST_MODE 1
14172 +                unsigned wkupintr : 1;
14173 +                unsigned sessreqintr : 1;
14174 +                unsigned disconnect : 1;
14175 +                unsigned conidstschng : 1;
14176 +                unsigned reserved27 : 1;
14177 +                unsigned ptxfempty : 1;
14178 +                unsigned hcintr : 1;
14179 +                unsigned portintr : 1;
14180 +                unsigned reserved22_23 : 2;
14181 +                unsigned incomplisoout : 1;
14182 +                unsigned incomplisoin : 1;
14183 +                unsigned outepintr : 1;
14184 +                unsigned inepint: 1;
14185 +                unsigned epmismatch : 1;
14186 +                unsigned intokenrx : 1;
14187 +                unsigned eopframe : 1;
14188 +                unsigned isooutdrop : 1;
14189 +                unsigned enumdone : 1;
14190 +                unsigned usbreset : 1;
14191 +                unsigned usbsuspend : 1;
14192 +                unsigned erlysuspend : 1;
14193 +                unsigned i2cintr : 1;
14194 +                unsigned reserved8 : 1;
14195 +                unsigned goutnakeff : 1;
14196 +                unsigned ginnakeff : 1;
14197 +                unsigned nptxfempty : 1;
14198 +                unsigned rxstsqlvl : 1;
14199 +                unsigned sofintr : 1;
14200 +                unsigned otgintr : 1;
14201 +                unsigned modemismatch : 1;
14202 +                unsigned curmode : 1;
14203 +        } b;
14204 +} gintsts_data_t;
14205 +
14206 +
14207 +/**
14208 + * This union represents the bit fields in the Device Receive Status Read and 
14209 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i> 
14210 + * element then read out the bits using the <i>b</i>it elements.
14211 + */
14212 +typedef union device_grxsts_data {
14213 +        /** raw register data */
14214 +        uint32_t d32;
14215 +        /** register bits */
14216 +       struct {
14217 +         unsigned reserved : 7;
14218 +         unsigned fn : 4;
14219 +#define DWC_STS_DATA_UPDT      0x2               // OUT Data Packet
14220 +#define DWC_STS_XFER_COMP      0x3               // OUT Data Transfer Complete
14221 +
14222 +#define DWC_DSTS_GOUT_NAK      0x1               // Global OUT NAK
14223 +#define DWC_DSTS_SETUP_COMP    0x4               // Setup Phase Complete
14224 +#define DWC_DSTS_SETUP_UPDT    0x6               // SETUP Packet
14225 +         unsigned pktsts : 4;
14226 +         unsigned dpid : 2;
14227 +         unsigned bcnt : 11;
14228 +         unsigned epnum : 4;
14229 +        } b;
14230 +} device_grxsts_data_t;
14231 +
14232 +/**
14233 + * This union represents the bit fields in the Host Receive Status Read and 
14234 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i> 
14235 + * element then read out the bits using the <i>b</i>it elements.
14236 + */
14237 +typedef union host_grxsts_data {
14238 +        /** raw register data */
14239 +        uint32_t d32;
14240 +        /** register bits */
14241 +       struct {
14242 +         unsigned reserved31_21 : 11;
14243 +#define DWC_GRXSTS_PKTSTS_IN              0x2
14244 +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP    0x3
14245 +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
14246 +#define DWC_GRXSTS_PKTSTS_CH_HALTED       0x7
14247 +         unsigned pktsts : 4;
14248 +         unsigned dpid : 2;
14249 +         unsigned bcnt : 11;
14250 +         unsigned chnum : 4;
14251 +        } b;
14252 +} host_grxsts_data_t;
14253 +
14254 +/**
14255 + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
14256 + * GNPTXFSIZ, DPTXFSIZn). Read the register into the <i>d32</i> element then
14257 + * read out the bits using the <i>b</i>it elements.
14258 + */
14259 +typedef union fifosize_data {
14260 +        /** raw register data */
14261 +        uint32_t d32;
14262 +        /** register bits */
14263 +       struct {
14264 +               unsigned depth : 16;
14265 +               unsigned startaddr : 16;
14266 +        } b;
14267 +} fifosize_data_t;
14268 +
14269 +/**
14270 + * This union represents the bit fields in the Non-Periodic Transmit
14271 + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
14272 + * <i>d32</i> element then read out the bits using the <i>b</i>it
14273 + * elements.
14274 + */
14275 +typedef union gnptxsts_data {
14276 +        /** raw register data */
14277 +        uint32_t d32;
14278 +        /** register bits */
14279 +       struct {
14280 +                unsigned reserved : 1;
14281 +                /** Top of the Non-Periodic Transmit Request Queue 
14282 +                 *  - bits 30:27 - Channel/EP Number
14283 +                 *  - bits 26:25 - Token Type 
14284 +                 *  - bit 24 - Terminate (Last entry for the selected
14285 +                 *    channel/EP)
14286 +                 *    - 2'b00 - IN/OUT
14287 +                 *    - 2'b01 - Zero Length OUT
14288 +                 *    - 2'b10 - PING/Complete Split
14289 +                 *    - 2'b11 - Channel Halt
14290 +
14291 +                 */
14292 +                unsigned nptxqtop_chnep : 4;
14293 +                unsigned nptxqtop_token : 2;
14294 +                unsigned nptxqtop_terminate : 1;
14295 +               unsigned nptxqspcavail : 8;
14296 +               unsigned nptxfspcavail : 16;
14297 +        } b;
14298 +} gnptxsts_data_t;
14299 +
14300 +/**
14301 + * This union represents the bit fields in the Transmit
14302 + * FIFO Status Register (DTXFSTS). Read the register into the
14303 + * <i>d32</i> element then read out the bits using the <i>b</i>it
14304 + * elements.
14305 + */
14306 +typedef union dtxfsts_data     /* fscz */       //*
14307 +{
14308 +       /** raw register data */
14309 +       uint32_t d32;
14310 +       /** register bits */
14311 +       struct {
14312 +               unsigned reserved : 16;
14313 +               unsigned txfspcavail : 16;
14314 +       } b;
14315 +} dtxfsts_data_t;
14316 +
14317 +/**
14318 + * This union represents the bit fields in the I2C Control Register
14319 + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
14320 + * bits using the <i>b</i>it elements.
14321 + */
14322 +typedef union gi2cctl_data {
14323 +        /** raw register data */
14324 +        uint32_t d32;
14325 +        /** register bits */
14326 +       struct {
14327 +               unsigned bsydne : 1;
14328 +               unsigned rw : 1;
14329 +               unsigned reserved : 2;
14330 +               unsigned i2cdevaddr : 2;
14331 +               unsigned i2csuspctl : 1;
14332 +               unsigned ack : 1;
14333 +               unsigned i2cen : 1;
14334 +               unsigned addr : 7;
14335 +               unsigned regaddr : 8;
14336 +               unsigned rwdata : 8;
14337 +        } b;
14338 +} gi2cctl_data_t;
14339 +
14340 +/**
14341 + * This union represents the bit fields in the User HW Config1
14342 + * Register.  Read the register into the <i>d32</i> element then read
14343 + * out the bits using the <i>b</i>it elements.
14344 + */
14345 +typedef union hwcfg1_data {
14346 +        /** raw register data */
14347 +        uint32_t d32;
14348 +        /** register bits */
14349 +        struct {
14350 +                unsigned ep_dir15 : 2;
14351 +                unsigned ep_dir14 : 2;
14352 +                unsigned ep_dir13 : 2;
14353 +                unsigned ep_dir12 : 2;
14354 +                unsigned ep_dir11 : 2;
14355 +                unsigned ep_dir10 : 2;
14356 +                unsigned ep_dir9 : 2;
14357 +                unsigned ep_dir8 : 2;
14358 +                unsigned ep_dir7 : 2;
14359 +                unsigned ep_dir6 : 2;
14360 +                unsigned ep_dir5 : 2;
14361 +                unsigned ep_dir4 : 2;
14362 +                unsigned ep_dir3 : 2;
14363 +                unsigned ep_dir2 : 2;
14364 +                unsigned ep_dir1 : 2;
14365 +                unsigned ep_dir0 : 2;
14366 +        } b;
14367 +} hwcfg1_data_t;
14368 +
14369 +/**
14370 + * This union represents the bit fields in the User HW Config2
14371 + * Register.  Read the register into the <i>d32</i> element then read
14372 + * out the bits using the <i>b</i>it elements.
14373 + */
14374 +typedef union hwcfg2_data
14375 +{
14376 +        /** raw register data */
14377 +        uint32_t d32;
14378 +        /** register bits */
14379 +        struct {
14380 +                /* GHWCFG2 */
14381 +                unsigned reserved31 : 1;
14382 +                unsigned dev_token_q_depth : 5;
14383 +                unsigned host_perio_tx_q_depth : 2;
14384 +                unsigned nonperio_tx_q_depth : 2;
14385 +                unsigned rx_status_q_depth : 2;
14386 +                unsigned dynamic_fifo : 1;
14387 +                unsigned perio_ep_supported : 1;
14388 +                unsigned num_host_chan : 4;
14389 +                unsigned num_dev_ep : 4;
14390 +                unsigned fs_phy_type : 2;
14391 +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
14392 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
14393 +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
14394 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
14395 +                unsigned hs_phy_type : 2;
14396 +                unsigned point2point : 1;
14397 +                unsigned architecture : 2;
14398 +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
14399 +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
14400 +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
14401 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
14402 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
14403 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
14404 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
14405 +                unsigned op_mode : 3;
14406 +        } b;
14407 +} hwcfg2_data_t;
14408 +
14409 +/**
14410 + * This union represents the bit fields in the User HW Config3
14411 + * Register.  Read the register into the <i>d32</i> element then read
14412 + * out the bits using the <i>b</i>it elements.
14413 + */
14414 +typedef union hwcfg3_data
14415 +{
14416 +        /** raw register data */
14417 +        uint32_t d32;
14418 +        /** register bits */
14419 +        struct {
14420 +                /* GHWCFG3 */
14421 +                unsigned dfifo_depth : 16;
14422 +                unsigned reserved15_13 : 3;
14423 +                unsigned ahb_phy_clock_synch : 1;
14424 +                unsigned synch_reset_type : 1;
14425 +                unsigned optional_features : 1;
14426 +                unsigned vendor_ctrl_if : 1;
14427 +                unsigned i2c : 1;
14428 +                unsigned otg_func : 1;
14429 +                unsigned packet_size_cntr_width : 3;
14430 +                unsigned xfer_size_cntr_width : 4;
14431 +        } b;
14432 +} hwcfg3_data_t;
14433 +
14434 +/**
14435 + * This union represents the bit fields in the User HW Config4
14436 + * Register.  Read the register into the <i>d32</i> element then read
14437 + * out the bits using the <i>b</i>it elements.
14438 + */
14439 +typedef union hwcfg4_data
14440 +{
14441 +        /** raw register data */
14442 +        uint32_t d32;
14443 +        /** register bits */
14444 +        struct {
14445 +unsigned reserved31_30 : 2;    /* fscz */
14446 +               unsigned num_in_eps : 4;
14447 +               unsigned ded_fifo_en : 1;
14448 +
14449 +                unsigned session_end_filt_en : 1;                
14450 +                unsigned b_valid_filt_en : 1;                
14451 +                unsigned a_valid_filt_en : 1;
14452 +                unsigned vbus_valid_filt_en : 1;
14453 +                unsigned iddig_filt_en : 1;
14454 +                unsigned num_dev_mode_ctrl_ep : 4;
14455 +                unsigned utmi_phy_data_width : 2;
14456 +                unsigned min_ahb_freq : 9;
14457 +                unsigned power_optimiz : 1;
14458 +                unsigned num_dev_perio_in_ep : 4;
14459 +        } b;
14460 +} hwcfg4_data_t;
14461 +
14462 +////////////////////////////////////////////
14463 +// Device Registers
14464 +/**
14465 + * Device Global Registers. <i>Offsets 800h-BFFh</i>
14466 + *
14467 + * The following structures define the size and relative field offsets
14468 + * for the Device Mode Registers.
14469 + *
14470 + * <i>These registers are visible only in Device mode and must not be
14471 + * accessed in Host mode, as the results are unknown.</i>
14472 + */
14473 +typedef struct dwc_otg_dev_global_regs 
14474 +{
14475 +        /** Device Configuration Register. <i>Offset 800h</i> */
14476 +        volatile uint32_t dcfg; 
14477 +        /** Device Control Register. <i>Offset: 804h</i> */
14478 +        volatile uint32_t dctl; 
14479 +        /** Device Status Register (Read Only). <i>Offset: 808h</i> */
14480 +        volatile uint32_t dsts; 
14481 +        /** Reserved. <i>Offset: 80Ch</i> */
14482 +        uint32_t unused;       
14483 +        /** Device IN Endpoint Common Interrupt Mask
14484 +         * Register. <i>Offset: 810h</i> */
14485 +        volatile uint32_t diepmsk; 
14486 +        /** Device OUT Endpoint Common Interrupt Mask
14487 +         * Register. <i>Offset: 814h</i> */
14488 +        volatile uint32_t doepmsk;     
14489 +        /** Device All Endpoints Interrupt Register.  <i>Offset: 818h</i> */
14490 +        volatile uint32_t daint;       
14491 +        /** Device All Endpoints Interrupt Mask Register.  <i>Offset:
14492 +         * 81Ch</i> */
14493 +        volatile uint32_t daintmsk; 
14494 +        /** Device IN Token Queue Read Register-1 (Read Only).
14495 +         * <i>Offset: 820h</i> */
14496 +        volatile uint32_t dtknqr1;     
14497 +        /** Device IN Token Queue Read Register-2 (Read Only).
14498 +         * <i>Offset: 824h</i> */ 
14499 +        volatile uint32_t dtknqr2;     
14500 +        /** Device VBUS  discharge Register.  <i>Offset: 828h</i> */
14501 +        volatile uint32_t dvbusdis;    
14502 +        /** Device VBUS Pulse Register.  <i>Offset: 82Ch</i> */
14503 +        volatile uint32_t dvbuspulse;
14504 +        /** Device IN Token Queue Read Register-3 (Read Only).
14505 +        *  Device Thresholding control register (Read/Write)
14506 +        * <i>Offset: 830h</i> */
14507 +           volatile uint32_t dtknqr3_dthrctl;
14508 +       /** Device IN Token Queue Read Register-4 (Read Only). /
14509 +        *  Device IN EPs empty Inr. Mask Register (Read/Write)
14510 +         * <i>Offset: 834h</i> */ 
14511 +           volatile uint32_t dtknqr4_fifoemptymsk;
14512 +} dwc_otg_device_global_regs_t; 
14513 +
14514 +/**
14515 + * This union represents the bit fields in the Device Configuration
14516 + * Register.  Read the register into the <i>d32</i> member then
14517 + * set/clear the bits using the <i>b</i>it elements.  Write the
14518 + * <i>d32</i> member to the dcfg register.
14519 + */
14520 +typedef union dcfg_data
14521 +{
14522 +        /** raw register data */
14523 +        uint32_t d32;
14524 +        /** register bits */
14525 +        struct {
14526 +                unsigned reserved31_23 : 9;
14527 +                /** In Endpoint Mis-match count */
14528 +                unsigned epmscnt : 5;
14529 +                unsigned reserved13_17 : 5;
14530 +                /** Periodic Frame Interval */
14531 +#define DWC_DCFG_FRAME_INTERVAL_80 0
14532 +#define DWC_DCFG_FRAME_INTERVAL_85 1
14533 +#define DWC_DCFG_FRAME_INTERVAL_90 2
14534 +#define DWC_DCFG_FRAME_INTERVAL_95 3
14535 +                unsigned perfrint : 2;
14536 +                /** Device Addresses */
14537 +                unsigned devaddr : 7;
14538 +                unsigned reserved3 : 1;
14539 +                /** Non Zero Length Status OUT Handshake */
14540 +#define DWC_DCFG_SEND_STALL 1
14541 +                unsigned nzstsouthshk : 1;
14542 +                /** Device Speed */
14543 +                unsigned devspd : 2;
14544 +        } b;
14545 +} dcfg_data_t;
14546 +
14547 +/**
14548 + * This union represents the bit fields in the Device Control
14549 + * Register.  Read the register into the <i>d32</i> member then
14550 + * set/clear the bits using the <i>b</i>it elements.
14551 + */
14552 +typedef union dctl_data
14553 +{
14554 +       /** raw register data */
14555 +       uint32_t d32;
14556 +       /** register bits */
14557 +       struct {
14558 +               unsigned reserved : 20;
14559 +               /** Power-On Programming Done */
14560 +               unsigned pwronprgdone : 1;
14561 +               /** Clear Global OUT NAK */
14562 +               unsigned cgoutnak : 1;
14563 +               /** Set Global OUT NAK */
14564 +               unsigned sgoutnak : 1;
14565 +               /** Clear Global Non-Periodic IN NAK */
14566 +               unsigned cgnpinnak : 1;
14567 +               /** Set Global Non-Periodic IN NAK */
14568 +               unsigned sgnpinnak : 1;
14569 +               /** Test Control */
14570 +               unsigned tstctl : 3;
14571 +               /** Global OUT NAK Status */
14572 +               unsigned goutnaksts : 1;
14573 +               /** Global Non-Periodic IN NAK Status */
14574 +               unsigned gnpinnaksts : 1;
14575 +               /** Soft Disconnect */
14576 +               unsigned sftdiscon : 1;
14577 +               /** Remote Wakeup */
14578 +               unsigned rmtwkupsig : 1;
14579 +       } b;
14580 +} dctl_data_t;
14581 +
14582 +/**
14583 + * This union represents the bit fields in the Device Status
14584 + * Register.  Read the register into the <i>d32</i> member then
14585 + * set/clear the bits using the <i>b</i>it elements.
14586 + */
14587 +typedef union dsts_data
14588 +{
14589 +       /** raw register data */
14590 +       uint32_t d32;
14591 +       /** register bits */
14592 +       struct {
14593 +               unsigned reserved22_31 : 10;
14594 +               /** Frame or Microframe Number of the received SOF */
14595 +               unsigned soffn : 14;
14596 +               unsigned reserved4_7: 4;
14597 +               /** Erratic Error */
14598 +               unsigned errticerr : 1;
14599 +               /** Enumerated Speed */
14600 +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
14601 +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
14602 +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ           2
14603 +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ          3
14604 +               unsigned enumspd : 2;
14605 +               /** Suspend Status */
14606 +               unsigned suspsts : 1;
14607 +        } b;
14608 +} dsts_data_t;
14609 +
14610 +
14611 +/**
14612 + * This union represents the bit fields in the Device IN EP Interrupt
14613 + * Register and the Device IN EP Common Mask Register.
14614 + *
14615 + * - Read the register into the <i>d32</i> member then set/clear the
14616 + *   bits using the <i>b</i>it elements.
14617 + */
14618 +typedef union diepint_data
14619 +{
14620 +       /** raw register data */
14621 +       uint32_t d32;
14622 +       /** register bits */
14623 +       struct {
14624 +               unsigned reserved07_31 : 23;
14625 +               unsigned txfifoundrn : 1;
14626 +               /** IN Endpoint HAK Effective mask */
14627 +        unsigned emptyintr : 1;
14628 +               /** IN Endpoint NAK Effective mask */
14629 +               unsigned inepnakeff : 1;
14630 +               /** IN Token Received with EP mismatch mask */
14631 +               unsigned intknepmis : 1;
14632 +               /** IN Token received with TxF Empty mask */
14633 +               unsigned intktxfemp : 1;
14634 +               /** TimeOUT Handshake mask (non-ISOC EPs) */
14635 +               unsigned timeout : 1;
14636 +               /** AHB Error mask */
14637 +               unsigned ahberr : 1;
14638 +               /** Endpoint disable mask */
14639 +               unsigned epdisabled : 1;
14640 +               /** Transfer complete mask */
14641 +               unsigned xfercompl : 1;
14642 +        } b;
14643 +} diepint_data_t;
14644 +/**
14645 + * This union represents the bit fields in the Device IN EP Common
14646 + * Interrupt Mask Register.
14647 + */
14648 +typedef union diepint_data diepmsk_data_t;
14649 +
14650 +/**
14651 + * This union represents the bit fields in the Device OUT EP Interrupt
14652 + * Registerand Device OUT EP Common Interrupt Mask Register.
14653 + *
14654 + * - Read the register into the <i>d32</i> member then set/clear the
14655 + *   bits using the <i>b</i>it elements.
14656 + */
14657 +typedef union doepint_data
14658 +{
14659 +       /** raw register data */
14660 +       uint32_t d32;
14661 +       /** register bits */
14662 +       struct {
14663 +               unsigned reserved04_31 : 27;
14664 +               /** OUT Token Received when Endpoint Disabled */
14665 +               unsigned outtknepdis : 1;
14666 +               /** Setup Phase Done (contorl EPs) */
14667 +               unsigned setup : 1;
14668 +               /** AHB Error */
14669 +               unsigned ahberr : 1;
14670 +               /** Endpoint disable  */
14671 +               unsigned epdisabled : 1;
14672 +               /** Transfer complete */
14673 +               unsigned xfercompl : 1;
14674 +        } b;
14675 +} doepint_data_t;
14676 +/**
14677 + * This union represents the bit fields in the Device OUT EP Common
14678 + * Interrupt Mask Register.
14679 + */
14680 +typedef union doepint_data doepmsk_data_t;
14681 +
14682 +
14683 +/**
14684 + * This union represents the bit fields in the Device All EP Interrupt
14685 + * and Mask Registers.
14686 + * - Read the register into the <i>d32</i> member then set/clear the
14687 + *   bits using the <i>b</i>it elements.
14688 + */
14689 +typedef union daint_data
14690 +{
14691 +       /** raw register data */
14692 +       uint32_t d32;
14693 +       /** register bits */
14694 +       struct {
14695 +               /** OUT Endpoint bits */
14696 +               unsigned out : 16;
14697 +               /** IN Endpoint bits */
14698 +               unsigned in : 16;
14699 +        } ep;
14700 +       struct {
14701 +               /** OUT Endpoint bits */
14702 +               unsigned outep15 : 1;
14703 +               unsigned outep14 : 1;
14704 +               unsigned outep13 : 1;
14705 +               unsigned outep12 : 1;
14706 +               unsigned outep11 : 1;
14707 +               unsigned outep10 : 1;
14708 +               unsigned outep9  : 1;
14709 +               unsigned outep8  : 1;
14710 +               unsigned outep7  : 1;
14711 +               unsigned outep6  : 1;
14712 +               unsigned outep5  : 1;
14713 +               unsigned outep4  : 1;
14714 +               unsigned outep3  : 1;
14715 +               unsigned outep2  : 1;
14716 +               unsigned outep1  : 1;
14717 +               unsigned outep0  : 1;
14718 +               /** IN Endpoint bits */
14719 +               unsigned inep15 : 1;
14720 +               unsigned inep14 : 1;
14721 +               unsigned inep13 : 1;
14722 +               unsigned inep12 : 1;
14723 +               unsigned inep11 : 1;
14724 +               unsigned inep10 : 1;
14725 +               unsigned inep9  : 1;
14726 +               unsigned inep8  : 1;
14727 +               unsigned inep7  : 1;
14728 +               unsigned inep6  : 1;
14729 +               unsigned inep5  : 1;
14730 +               unsigned inep4  : 1;
14731 +               unsigned inep3  : 1;
14732 +               unsigned inep2  : 1;
14733 +               unsigned inep1  : 1;
14734 +               unsigned inep0  : 1;
14735 +        } b;
14736 +} daint_data_t;
14737 +
14738 +/**
14739 + * This union represents the bit fields in the Device IN Token Queue
14740 + * Read Registers.
14741 + * - Read the register into the <i>d32</i> member.
14742 + * - READ-ONLY Register
14743 + */
14744 +typedef union dtknq1_data
14745 +{
14746 +        /** raw register data */
14747 +        uint32_t d32;
14748 +        /** register bits */
14749 +        struct {
14750 +                /** EP Numbers of IN Tokens 0 ... 4 */
14751 +                unsigned epnums0_5 : 24;
14752 +                /** write pointer has wrapped. */
14753 +                unsigned wrap_bit : 1;
14754 +                /** Reserved */
14755 +                unsigned reserved05_06 : 2;
14756 +                /** In Token Queue Write Pointer */
14757 +                unsigned intknwptr : 5;
14758 +        }b;
14759 +} dtknq1_data_t;
14760 +
14761 +/**
14762 + * This union represents Threshold control Register
14763 + * - Read and write the register into the <i>d32</i> member.
14764 + * - READ-WRITABLE Register
14765 + */
14766 +typedef union dthrctl_data                     //* /*fscz */
14767 +{
14768 +    /** raw register data */
14769 +    uint32_t d32;
14770 +    /** register bits */
14771 +    struct {
14772 +        /** Reserved */
14773 +        unsigned reserved26_31 : 6;
14774 +        /** Rx Thr. Length */
14775 +        unsigned rx_thr_len : 9;
14776 +        /** Rx Thr. Enable */
14777 +        unsigned rx_thr_en : 1;
14778 +        /** Reserved */
14779 +        unsigned reserved11_15 : 5;
14780 +        /** Tx Thr. Length */
14781 +        unsigned tx_thr_len : 9;
14782 +        /** ISO Tx Thr. Enable */
14783 +        unsigned iso_thr_en : 1;
14784 +        /** non ISO Tx Thr. Enable */
14785 +        unsigned non_iso_thr_en : 1;
14786 +
14787 +    }b;
14788 +} dthrctl_data_t;
14789 +
14790 +/**
14791 + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
14792 + * 900h-AFCh</i>
14793 + *
14794 + * There will be one set of endpoint registers per logical endpoint
14795 + * implemented.
14796 + *
14797 + * <i>These registers are visible only in Device mode and must not be
14798 + * accessed in Host mode, as the results are unknown.</i>
14799 + */
14800 +typedef struct dwc_otg_dev_in_ep_regs 
14801 +{
14802 +        /** Device IN Endpoint Control Register. <i>Offset:900h +
14803 +         * (ep_num * 20h) + 00h</i> */
14804 +        volatile uint32_t diepctl;
14805 +        /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
14806 +        uint32_t reserved04;    
14807 +        /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
14808 +         * (ep_num * 20h) + 08h</i> */
14809 +        volatile uint32_t diepint; 
14810 +        /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
14811 +        uint32_t reserved0C;    
14812 +        /** Device IN Endpoint Transfer Size
14813 +         * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
14814 +        volatile uint32_t dieptsiz; 
14815 +        /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
14816 +         * (ep_num * 20h) + 14h</i> */
14817 +        volatile uint32_t diepdma; 
14818 +        /** Reserved. <i>Offset:900h + (ep_num * 20h) + 18h - 900h +
14819 +         * (ep_num * 20h) + 1Ch</i>*/
14820 +           volatile uint32_t dtxfsts;
14821 +       /** Reserved. <i>Offset:900h + (ep_num * 20h) + 1Ch - 900h +
14822 +            * (ep_num * 20h) + 1Ch</i>*/
14823 +       uint32_t reserved18;
14824 +} dwc_otg_dev_in_ep_regs_t;
14825 +
14826 +/**
14827 + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
14828 + * B00h-CFCh</i>
14829 + *
14830 + * There will be one set of endpoint registers per logical endpoint
14831 + * implemented.
14832 + *
14833 + * <i>These registers are visible only in Device mode and must not be
14834 + * accessed in Host mode, as the results are unknown.</i>
14835 + */
14836 +typedef struct dwc_otg_dev_out_ep_regs 
14837 +{
14838 +        /** Device OUT Endpoint Control Register. <i>Offset:B00h +
14839 +         * (ep_num * 20h) + 00h</i> */
14840 +        volatile uint32_t doepctl; 
14841 +        /** Device OUT Endpoint Frame number Register.  <i>Offset:
14842 +         * B00h + (ep_num * 20h) + 04h</i> */ 
14843 +        volatile uint32_t doepfn; 
14844 +        /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
14845 +         * (ep_num * 20h) + 08h</i> */
14846 +        volatile uint32_t doepint; 
14847 +        /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
14848 +        uint32_t reserved0C;    
14849 +        /** Device OUT Endpoint Transfer Size Register. <i>Offset:
14850 +         * B00h + (ep_num * 20h) + 10h</i> */
14851 +        volatile uint32_t doeptsiz; 
14852 +        /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
14853 +         * + (ep_num * 20h) + 14h</i> */
14854 +        volatile uint32_t doepdma; 
14855 +        /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 18h - B00h +
14856 +         * (ep_num * 20h) + 1Ch</i> */
14857 +        uint32_t unused[2];     
14858 +} dwc_otg_dev_out_ep_regs_t;
14859 +
14860 +/**
14861 + * This union represents the bit fields in the Device EP Control
14862 + * Register.  Read the register into the <i>d32</i> member then
14863 + * set/clear the bits using the <i>b</i>it elements.
14864 + */
14865 +typedef union depctl_data
14866 +{
14867 +        /** raw register data */
14868 +        uint32_t d32;
14869 +        /** register bits */
14870 +        struct {
14871 +               /** Endpoint Enable */
14872 +               unsigned epena : 1;
14873 +               /** Endpoint Disable */
14874 +               unsigned epdis : 1;
14875 +                /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
14876 +                 * Writing to this field sets the Endpoint DPID (DPID)
14877 +                 * field in this register to DATA1 Set Odd
14878 +                 * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
14879 +                 * Writing to this field sets the Even/Odd
14880 +                 * (micro)frame (EO_FrNum) field to odd (micro) frame.
14881 +                 */
14882 +                unsigned setd1pid : 1;
14883 +                /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
14884 +                 * Writing to this field sets the Endpoint DPID (DPID)
14885 +                 * field in this register to DATA0. Set Even
14886 +                 * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
14887 +                 * Writing to this field sets the Even/Odd
14888 +                 * (micro)frame (EO_FrNum) field to even (micro)
14889 +                 * frame.
14890 +                 */
14891 +                unsigned setd0pid : 1;
14892 +               /** Set NAK */
14893 +               unsigned snak : 1;
14894 +               /** Clear NAK */
14895 +               unsigned cnak : 1;
14896 +               /** Tx Fifo Number 
14897 +                * IN EPn/IN EP0
14898 +                * OUT EPn/OUT EP0 - reserved */
14899 +               unsigned txfnum : 4;
14900 +               /** Stall Handshake */
14901 +               unsigned stall : 1;
14902 +               /** Snoop Mode 
14903 +                * OUT EPn/OUT EP0
14904 +                * IN EPn/IN EP0 - reserved */
14905 +               unsigned snp : 1;
14906 +               /** Endpoint Type 
14907 +                *  2'b00: Control
14908 +                *  2'b01: Isochronous
14909 +                *  2'b10: Bulk
14910 +                *  2'b11: Interrupt */
14911 +               unsigned eptype : 2;
14912 +               /** NAK Status */
14913 +               unsigned naksts : 1;
14914 +               /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
14915 +                 * This field contains the PID of the packet going to
14916 +                 * be received or transmitted on this endpoint. The
14917 +                 * application should program the PID of the first
14918 +                 * packet going to be received or transmitted on this
14919 +                 * endpoint , after the endpoint is
14920 +                 * activated. Application use the SetD1PID and
14921 +                 * SetD0PID fields of this register to program either
14922 +                 * D0 or D1 PID.
14923 +                 * 
14924 +                 * The encoding for this field is
14925 +                 *   - 0: D0
14926 +                 *   - 1: D1
14927 +                 */
14928 +               unsigned dpid : 1;
14929 +               /** USB Active Endpoint */
14930 +               unsigned usbactep : 1;
14931 +               /** Next Endpoint 
14932 +                * IN EPn/IN EP0 
14933 +                * OUT EPn/OUT EP0 - reserved */
14934 +               unsigned nextep : 4;
14935 +               /** Maximum Packet Size 
14936 +                * IN/OUT EPn
14937 +                * IN/OUT EP0 - 2 bits
14938 +                *   2'b00: 64 Bytes
14939 +                *   2'b01: 32
14940 +                *   2'b10: 16
14941 +                *   2'b11: 8 */
14942 +#define DWC_DEP0CTL_MPS_64   0
14943 +#define DWC_DEP0CTL_MPS_32   1
14944 +#define DWC_DEP0CTL_MPS_16   2
14945 +#define DWC_DEP0CTL_MPS_8    3
14946 +               unsigned mps : 11;
14947 +        } b;
14948 +} depctl_data_t;
14949 +
14950 +/**
14951 + * This union represents the bit fields in the Device EP Transfer
14952 + * Size Register.  Read the register into the <i>d32</i> member then
14953 + * set/clear the bits using the <i>b</i>it elements.
14954 + */
14955 +typedef union deptsiz_data
14956 +{
14957 +        /** raw register data */
14958 +        uint32_t d32;
14959 +        /** register bits */
14960 +        struct {
14961 +               unsigned reserved : 1;
14962 +               /** Multi Count - Periodic IN endpoints */
14963 +               unsigned mc : 2;
14964 +               /** Packet Count */
14965 +               unsigned pktcnt : 10;
14966 +               /** Transfer size */
14967 +               unsigned xfersize : 19;
14968 +        } b;
14969 +} deptsiz_data_t;
14970 +
14971 +/**
14972 + * This union represents the bit fields in the Device EP 0 Transfer
14973 + * Size Register.  Read the register into the <i>d32</i> member then
14974 + * set/clear the bits using the <i>b</i>it elements.
14975 + */
14976 +typedef union deptsiz0_data
14977 +{
14978 +        /** raw register data */
14979 +        uint32_t d32;
14980 +        /** register bits */
14981 +        struct {
14982 +                unsigned reserved31 : 1;
14983 +                /**Setup Packet Count (DOEPTSIZ0 Only) */
14984 +                unsigned supcnt : 2;
14985 +                /** Reserved */
14986 +               unsigned reserved28_20 : 9;
14987 +               /** Packet Count */
14988 +               unsigned pktcnt : 1;
14989 +                /** Reserved */
14990 +               unsigned reserved18_7 : 12;
14991 +               /** Transfer size */
14992 +               unsigned xfersize : 7;
14993 +        } b;
14994 +} deptsiz0_data_t;
14995 +
14996 +
14997 +/** Maximum number of Periodic FIFOs */
14998 +#define MAX_PERIO_FIFOS 15
14999 +/** Maximum number of TX FIFOs */
15000 +#define MAX_TX_FIFOS 15
15001 +/** Maximum number of Endpoints/HostChannels */
15002 +#define MAX_EPS_CHANNELS 16
15003 +//#define MAX_EPS_CHANNELS 4
15004 +
15005 +/**
15006 + * The dwc_otg_dev_if structure contains information needed to manage
15007 + * the DWC_otg controller acting in device mode. It represents the
15008 + * programming view of the device-specific aspects of the controller.
15009 + */
15010 +typedef struct dwc_otg_dev_if {
15011 +        /** Pointer to device Global registers.
15012 +         * Device Global Registers starting at offset 800h
15013 +         */
15014 +        dwc_otg_device_global_regs_t *dev_global_regs; 
15015 +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
15016 +
15017 +        /** 
15018 +         * Device Logical IN Endpoint-Specific Registers 900h-AFCh 
15019 +         */
15020 +        dwc_otg_dev_in_ep_regs_t     *in_ep_regs[MAX_EPS_CHANNELS];
15021 +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
15022 +#define DWC_EP_REG_OFFSET 0x20
15023 +
15024 +        /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
15025 +        dwc_otg_dev_out_ep_regs_t    *out_ep_regs[MAX_EPS_CHANNELS];
15026 +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
15027 +
15028 +        /* Device configuration information*/
15029 +        uint8_t  speed;              /**< Device Speed  0: Unknown, 1: LS, 2:FS, 3: HS */
15030 +        //uint8_t  num_eps;            /**< Number of EPs  range: 0-16 (includes EP0) */
15031 +        //uint8_t  num_perio_eps;      /**< # of Periodic EP range: 0-15 */
15032 +       /*fscz */
15033 +    uint8_t  num_in_eps;         /**< Number # of Tx EP range: 0-15 exept ep0 */
15034 +    uint8_t  num_out_eps;        /**< Number # of Rx EP range: 0-15 exept ep 0*/
15035 +
15036 +        /** Size of periodic FIFOs (Bytes) */
15037 +        uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];  
15038 +
15039 +       /** Size of Tx FIFOs (Bytes) */
15040 +       uint16_t tx_fifo_size[MAX_TX_FIFOS];
15041 +
15042 +       /** Thresholding enable flags and length varaiables **/
15043 +       uint16_t rx_thr_en;
15044 +       uint16_t iso_tx_thr_en;
15045 +       uint16_t non_iso_tx_thr_en;
15046 +
15047 +       uint16_t rx_thr_length;
15048 +       uint16_t tx_thr_length;
15049 +} dwc_otg_dev_if_t;
15050 +
15051 +/**
15052 + * This union represents the bit fields in the Power and Clock Gating Control
15053 + * Register. Read the register into the <i>d32</i> member then set/clear the
15054 + * bits using the <i>b</i>it elements.
15055 + */
15056 +typedef union pcgcctl_data     
15057 +{
15058 +       /** raw register data */
15059 +       uint32_t d32;
15060 +
15061 +       /** register bits */
15062 +       struct {
15063 +               unsigned reserved31_05 : 27;
15064 +               /** PHY Suspended */
15065 +               unsigned physuspended : 1;
15066 +               /** Reset Power Down Modules */
15067 +               unsigned rstpdwnmodule : 1;
15068 +               /** Power Clamp */
15069 +               unsigned pwrclmp : 1;
15070 +               /** Gate Hclk */
15071 +               unsigned gatehclk : 1;
15072 +               /** Stop Pclk */
15073 +               unsigned stoppclk : 1;
15074 +       } b;
15075 +} pcgcctl_data_t;
15076 +
15077 +/////////////////////////////////////////////////
15078 +// Host Mode Register Structures
15079 +//
15080 +/**
15081 + * The Host Global Registers structure defines the size and relative
15082 + * field offsets for the Host Mode Global Registers.  Host Global
15083 + * Registers offsets 400h-7FFh.
15084 +*/
15085 +typedef struct dwc_otg_host_global_regs 
15086 +{
15087 +        /** Host Configuration Register.   <i>Offset: 400h</i> */
15088 +        volatile uint32_t hcfg;       
15089 +        /** Host Frame Interval Register.   <i>Offset: 404h</i> */
15090 +        volatile uint32_t hfir;       
15091 +        /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
15092 +        volatile uint32_t hfnum; 
15093 +        /** Reserved.   <i>Offset: 40Ch</i> */
15094 +        uint32_t reserved40C;
15095 +        /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
15096 +        volatile uint32_t hptxsts;    
15097 +        /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
15098 +        volatile uint32_t haint;      
15099 +        /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
15100 +        volatile uint32_t haintmsk;   
15101 +} dwc_otg_host_global_regs_t;
15102 +
15103 +/**
15104 + * This union represents the bit fields in the Host Configuration Register.
15105 + * Read the register into the <i>d32</i> member then set/clear the bits using
15106 + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
15107 + */
15108 +typedef union hcfg_data
15109 +{
15110 +        /** raw register data */
15111 +        uint32_t d32;
15112 +
15113 +        /** register bits */
15114 +        struct {
15115 +                /** Reserved */
15116 +               //unsigned reserved31_03 : 29;
15117 +               /** FS/LS Only Support */
15118 +               unsigned fslssupp : 1;
15119 +               /** FS/LS Phy Clock Select */
15120 +#define DWC_HCFG_30_60_MHZ 0
15121 +#define DWC_HCFG_48_MHZ    1
15122 +#define DWC_HCFG_6_MHZ     2
15123 +               unsigned fslspclksel : 2;
15124 +        } b;
15125 +} hcfg_data_t;
15126 +
15127 +/**
15128 + * This union represents the bit fields in the Host Frame Remaing/Number
15129 + * Register.  
15130 + */
15131 +typedef union hfir_data
15132 +{
15133 +        /** raw register data */
15134 +        uint32_t d32;
15135 +
15136 +        /** register bits */
15137 +        struct {
15138 +               unsigned reserved : 16;
15139 +               unsigned frint : 16;
15140 +        } b;
15141 +} hfir_data_t;
15142 +
15143 +/**
15144 + * This union represents the bit fields in the Host Frame Remaing/Number
15145 + * Register.  
15146 + */
15147 +typedef union hfnum_data
15148 +{
15149 +        /** raw register data */
15150 +        uint32_t d32;
15151 +
15152 +        /** register bits */
15153 +        struct {
15154 +               unsigned frrem : 16;
15155 +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
15156 +               unsigned frnum : 16;
15157 +        } b;
15158 +} hfnum_data_t;
15159 +
15160 +typedef union hptxsts_data
15161 +{
15162 +       /** raw register data */
15163 +       uint32_t d32;
15164 +
15165 +       /** register bits */
15166 +       struct {
15167 +               /** Top of the Periodic Transmit Request Queue
15168 +                *  - bit 24 - Terminate (last entry for the selected channel)
15169 +                *  - bits 26:25 - Token Type
15170 +                *    - 2'b00 - Zero length
15171 +                *    - 2'b01 - Ping
15172 +                *    - 2'b10 - Disable
15173 +                *  - bits 30:27 - Channel Number
15174 +                *  - bit 31 - Odd/even microframe
15175 +                */
15176 +               unsigned ptxqtop_odd : 1;
15177 +               unsigned ptxqtop_chnum : 4;
15178 +               unsigned ptxqtop_token : 2;
15179 +               unsigned ptxqtop_terminate : 1;
15180 +               unsigned ptxqspcavail : 8;
15181 +               unsigned ptxfspcavail : 16;
15182 +       } b;
15183 +} hptxsts_data_t;
15184 +
15185 +/**
15186 + * This union represents the bit fields in the Host Port Control and Status
15187 + * Register. Read the register into the <i>d32</i> member then set/clear the
15188 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
15189 + * hprt0 register.
15190 + */
15191 +typedef union hprt0_data
15192 +{
15193 +        /** raw register data */
15194 +        uint32_t d32;
15195 +        /** register bits */
15196 +        struct {
15197 +               unsigned reserved19_31 : 13;
15198 +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
15199 +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
15200 +#define DWC_HPRT0_PRTSPD_LOW_SPEED  2
15201 +               unsigned prtspd : 2;
15202 +               unsigned prttstctl : 4;
15203 +               unsigned prtpwr : 1;
15204 +               unsigned prtlnsts : 2;
15205 +               unsigned reserved9 : 1;
15206 +               unsigned prtrst : 1;
15207 +               unsigned prtsusp : 1;
15208 +               unsigned prtres : 1;
15209 +               unsigned prtovrcurrchng : 1;
15210 +               unsigned prtovrcurract : 1;
15211 +               unsigned prtenchng : 1;
15212 +               unsigned prtena : 1;
15213 +               unsigned prtconndet : 1;
15214 +               unsigned prtconnsts : 1;
15215 +        } b;
15216 +} hprt0_data_t;
15217 +
15218 +/**
15219 + * This union represents the bit fields in the Host All Interrupt 
15220 + * Register.  
15221 + */
15222 +typedef union haint_data
15223 +{
15224 +        /** raw register data */
15225 +        uint32_t d32;
15226 +        /** register bits */
15227 +        struct {
15228 +               unsigned reserved : 16;
15229 +               unsigned ch15 : 1;
15230 +               unsigned ch14 : 1;
15231 +               unsigned ch13 : 1;
15232 +               unsigned ch12 : 1;
15233 +               unsigned ch11 : 1;
15234 +               unsigned ch10 : 1;
15235 +               unsigned ch9 : 1;
15236 +               unsigned ch8 : 1;
15237 +               unsigned ch7 : 1;
15238 +               unsigned ch6 : 1;
15239 +               unsigned ch5 : 1;
15240 +               unsigned ch4 : 1;
15241 +               unsigned ch3 : 1;
15242 +               unsigned ch2 : 1;
15243 +               unsigned ch1 : 1;
15244 +               unsigned ch0 : 1;
15245 +       } b;
15246 +        struct {
15247 +               unsigned reserved : 16;
15248 +               unsigned chint : 16;
15249 +       } b2;
15250 +} haint_data_t;
15251 +
15252 +/**
15253 + * This union represents the bit fields in the Host All Interrupt 
15254 + * Register.  
15255 + */
15256 +typedef union haintmsk_data
15257 +{
15258 +        /** raw register data */
15259 +        uint32_t d32;
15260 +        /** register bits */
15261 +        struct {
15262 +               unsigned reserved : 16;
15263 +               unsigned ch15 : 1;
15264 +               unsigned ch14 : 1;
15265 +               unsigned ch13 : 1;
15266 +               unsigned ch12 : 1;
15267 +               unsigned ch11 : 1;
15268 +               unsigned ch10 : 1;
15269 +               unsigned ch9 : 1;
15270 +               unsigned ch8 : 1;
15271 +               unsigned ch7 : 1;
15272 +               unsigned ch6 : 1;
15273 +               unsigned ch5 : 1;
15274 +               unsigned ch4 : 1;
15275 +               unsigned ch3 : 1;
15276 +               unsigned ch2 : 1;
15277 +               unsigned ch1 : 1;
15278 +               unsigned ch0 : 1;
15279 +       } b;
15280 +        struct {
15281 +               unsigned reserved : 16;
15282 +               unsigned chint : 16;
15283 +       } b2;
15284 +} haintmsk_data_t;
15285 +
15286 +/** 
15287 + * Host Channel Specific Registers. <i>500h-5FCh</i>
15288 + */
15289 +typedef struct dwc_otg_hc_regs 
15290 +{
15291 +        /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
15292 +        volatile uint32_t hcchar;     
15293 +        /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
15294 +        volatile uint32_t hcsplt;     
15295 +        /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
15296 +        volatile uint32_t hcint;
15297 +        /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
15298 +        volatile uint32_t hcintmsk;
15299 +        /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
15300 +        volatile uint32_t hctsiz;
15301 +        /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
15302 +        volatile uint32_t hcdma;
15303 +        /** Reserved.  <i>Offset: 500h + (chan_num * 20h) + 18h - 500h + (chan_num * 20h) + 1Ch</i> */
15304 +        uint32_t reserved[2];
15305 +} dwc_otg_hc_regs_t;
15306 +
15307 +/**
15308 + * This union represents the bit fields in the Host Channel Characteristics
15309 + * Register. Read the register into the <i>d32</i> member then set/clear the
15310 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
15311 + * hcchar register.
15312 + */
15313 +typedef union hcchar_data
15314 +{
15315 +        /** raw register data */
15316 +        uint32_t d32;
15317 +
15318 +        /** register bits */
15319 +        struct {
15320 +               /** Channel enable */
15321 +               unsigned chen : 1;
15322 +               /** Channel disable */
15323 +               unsigned chdis : 1;
15324 +               /**
15325 +                * Frame to transmit periodic transaction.
15326 +                * 0: even, 1: odd
15327 +                */
15328 +               unsigned oddfrm : 1;
15329 +               /** Device address */
15330 +               unsigned devaddr : 7;
15331 +               /** Packets per frame for periodic transfers. 0 is reserved. */
15332 +               unsigned multicnt : 2;
15333 +               /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
15334 +               unsigned eptype : 2;
15335 +               /** 0: Full/high speed device, 1: Low speed device */
15336 +               unsigned lspddev : 1;
15337 +               unsigned reserved : 1;
15338 +               /** 0: OUT, 1: IN */
15339 +               unsigned epdir : 1;
15340 +               /** Endpoint number */
15341 +               unsigned epnum : 4;
15342 +               /** Maximum packet size in bytes */
15343 +               unsigned mps : 11;
15344 +        } b;
15345 +} hcchar_data_t;
15346 +
15347 +typedef union hcsplt_data
15348 +{
15349 +        /** raw register data */
15350 +        uint32_t d32;
15351 +
15352 +        /** register bits */
15353 +        struct {
15354 +               /** Split Enble */
15355 +               unsigned spltena : 1;
15356 +               /** Reserved */
15357 +               unsigned reserved : 14;
15358 +               /** Do Complete Split */
15359 +               unsigned compsplt : 1;
15360 +               /** Transaction Position */
15361 +#define DWC_HCSPLIT_XACTPOS_MID 0
15362 +#define DWC_HCSPLIT_XACTPOS_END 1
15363 +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
15364 +#define DWC_HCSPLIT_XACTPOS_ALL 3
15365 +               unsigned xactpos : 2;
15366 +               /** Hub Address */
15367 +               unsigned hubaddr : 7;
15368 +               /** Port Address */
15369 +               unsigned prtaddr : 7;
15370 +       } b;
15371 +} hcsplt_data_t;
15372 +
15373 +
15374 +/**
15375 + * This union represents the bit fields in the Host All Interrupt 
15376 + * Register.  
15377 + */
15378 +typedef union hcint_data
15379 +{
15380 +        /** raw register data */
15381 +        uint32_t d32;
15382 +        /** register bits */
15383 +        struct {
15384 +               /** Reserved */
15385 +               unsigned reserved : 21;
15386 +               /** Data Toggle Error */
15387 +               unsigned datatglerr : 1;
15388 +               /** Frame Overrun */
15389 +               unsigned frmovrun : 1;
15390 +               /** Babble Error */
15391 +               unsigned bblerr : 1;
15392 +               /** Transaction Err */
15393 +               unsigned xacterr : 1;
15394 +               /** NYET Response Received */
15395 +               unsigned nyet : 1;
15396 +               /** ACK Response Received */
15397 +               unsigned ack : 1;
15398 +               /** NAK Response Received */
15399 +               unsigned nak : 1;
15400 +               /** STALL Response Received */
15401 +               unsigned stall : 1;
15402 +               /** AHB Error */
15403 +               unsigned ahberr : 1;
15404 +               /** Channel Halted */
15405 +               unsigned chhltd : 1;
15406 +               /** Transfer Complete */
15407 +               unsigned xfercomp : 1;
15408 +       } b;
15409 +} hcint_data_t;
15410 +
15411 +/**
15412 + * This union represents the bit fields in the Host Channel Transfer Size
15413 + * Register. Read the register into the <i>d32</i> member then set/clear the
15414 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
15415 + * hcchar register.
15416 + */
15417 +typedef union hctsiz_data
15418 +{
15419 +        /** raw register data */
15420 +        uint32_t d32;
15421 +
15422 +        /** register bits */
15423 +        struct {
15424 +               /** Do PING protocol when 1 */
15425 +               unsigned dopng : 1;
15426 +               /**
15427 +                * Packet ID for next data packet
15428 +                * 0: DATA0
15429 +                * 1: DATA2
15430 +                * 2: DATA1
15431 +                * 3: MDATA (non-Control), SETUP (Control)
15432 +                */
15433 +#define DWC_HCTSIZ_DATA0 0
15434 +#define DWC_HCTSIZ_DATA1 2
15435 +#define DWC_HCTSIZ_DATA2 1
15436 +#define DWC_HCTSIZ_MDATA 3
15437 +#define DWC_HCTSIZ_SETUP 3             
15438 +               unsigned pid : 2;
15439 +               /** Data packets to transfer */
15440 +               unsigned pktcnt : 10;
15441 +               /** Total transfer size in bytes */
15442 +               unsigned xfersize : 19;
15443 +        } b;
15444 +} hctsiz_data_t;
15445 +
15446 +/**
15447 + * This union represents the bit fields in the Host Channel Interrupt Mask
15448 + * Register. Read the register into the <i>d32</i> member then set/clear the
15449 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
15450 + * hcintmsk register.
15451 + */
15452 +typedef union hcintmsk_data
15453 +{
15454 +        /** raw register data */
15455 +        uint32_t d32;
15456 +
15457 +        /** register bits */
15458 +        struct {
15459 +               unsigned reserved : 21;
15460 +               unsigned datatglerr : 1;
15461 +               unsigned frmovrun : 1;
15462 +               unsigned bblerr : 1;
15463 +               unsigned xacterr : 1;
15464 +               unsigned nyet : 1;
15465 +               unsigned ack : 1;
15466 +               unsigned nak : 1;
15467 +               unsigned stall : 1;
15468 +               unsigned ahberr : 1;
15469 +               unsigned chhltd : 1;
15470 +               unsigned xfercompl : 1;
15471 +        } b;
15472 +} hcintmsk_data_t;
15473 +
15474 +/** OTG Host Interface Structure.
15475 + *
15476 + * The OTG Host Interface Structure structure contains information
15477 + * needed to manage the DWC_otg controller acting in host mode. It
15478 + * represents the programming view of the host-specific aspects of the
15479 + * controller.
15480 + */
15481 +typedef struct dwc_otg_host_if {
15482 +        /** Host Global Registers starting at offset 400h.*/
15483 +        dwc_otg_host_global_regs_t *host_global_regs;
15484 +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400 
15485 +
15486 +        /** Host Port 0 Control and Status Register */
15487 +        volatile uint32_t *hprt0;
15488 +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
15489 +        
15490 +
15491 +        /** Host Channel Specific Registers at offsets 500h-5FCh. */
15492 +        dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
15493 +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
15494 +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
15495 +
15496 +
15497 +        /* Host configuration information */
15498 +        /** Number of Host Channels (range: 1-16) */
15499 +        uint8_t  num_host_channels;    
15500 +        /** Periodic EPs supported (0: no, 1: yes) */
15501 +        uint8_t  perio_eps_supported;
15502 +        /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
15503 +        uint16_t perio_tx_fifo_size;   
15504 +  
15505 +} dwc_otg_host_if_t;
15506 +
15507 +#endif
15508 --- a/arch/mips/lantiq/xway/Makefile
15509 +++ b/arch/mips/lantiq/xway/Makefile
15510 @@ -4,3 +4,4 @@
15511  obj-$(CONFIG_LANTIQ_MACH_EASY50712) += mach-easy50712.o
15512  obj-$(CONFIG_LANTIQ_MACH_EASY4010) += mach-easy4010.o
15513  obj-$(CONFIG_LANTIQ_MACH_ARV45XX) += mach-arv45xx.o
15514 +obj-y += dev-dwc_otg.o
15515 --- /dev/null
15516 +++ b/arch/mips/lantiq/xway/dev-dwc_otg.c
15517 @@ -0,0 +1,68 @@
15518 +/*
15519 + * This program is free software; you can redistribute it and/or modify
15520 + * it under the terms of the GNU General Public License as published by
15521 + * the Free Software Foundation; either version 2 of the License, or
15522 + * (at your option) any later version.
15523 + *
15524 + * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
15525 + */
15526 +
15527 +#include <linux/init.h>
15528 +#include <linux/module.h>
15529 +#include <linux/types.h>
15530 +#include <linux/string.h>
15531 +#include <linux/mtd/physmap.h>
15532 +#include <linux/kernel.h>
15533 +#include <linux/reboot.h>
15534 +#include <linux/platform_device.h>
15535 +#include <linux/leds.h>
15536 +#include <linux/etherdevice.h>
15537 +#include <linux/reboot.h>
15538 +#include <linux/time.h>
15539 +#include <linux/io.h>
15540 +#include <linux/gpio.h>
15541 +#include <linux/leds.h>
15542 +
15543 +#include <asm/bootinfo.h>
15544 +#include <asm/irq.h>
15545 +
15546 +#include <xway.h>
15547 +#include <xway_irq.h>
15548 +#include <lantiq_platform.h>
15549 +
15550 +#define LQ_USB_IOMEM_BASE 0x1e101000
15551 +#define LQ_USB_IOMEM_SIZE 0x00040000
15552 +
15553 +static struct resource resources[] =
15554 +{
15555 +       [0] = {
15556 +               .name    = "dwc_otg_membase",
15557 +               .start   = LQ_USB_IOMEM_BASE,
15558 +               .end       = LQ_USB_IOMEM_BASE + LQ_USB_IOMEM_SIZE - 1,
15559 +               .flags   = IORESOURCE_MEM,
15560 +       },
15561 +       [1] = {
15562 +               .name    = "dwc_otg_irq",
15563 +               .start   = LQ_USB_INT,
15564 +               .flags   = IORESOURCE_IRQ,
15565 +       },
15566 +};
15567 +
15568 +static u64 dwc_dmamask = (u32)0x1fffffff;
15569 +
15570 +static struct platform_device platform_dev = {
15571 +       .name = "dwc_otg",
15572 +       .dev = {
15573 +               .dma_mask      = &dwc_dmamask,
15574 +       },
15575 +       .resource               = resources,
15576 +       .num_resources          = ARRAY_SIZE(resources),
15577 +};
15578 +
15579 +int __init
15580 +xway_register_dwc(int pin)
15581 +{
15582 +       lq_enable_irq(resources[1].start);
15583 +       platform_dev.dev.platform_data = (void*) pin;
15584 +       return platform_device_register(&platform_dev);
15585 +}
15586 --- /dev/null
15587 +++ b/arch/mips/lantiq/xway/dev-dwc_otg.h
15588 @@ -0,0 +1,17 @@
15589 +/*
15590 + * This program is free software; you can redistribute it and/or modify
15591 + * it under the terms of the GNU General Public License as published by
15592 + * the Free Software Foundation; either version 2 of the License, or
15593 + * (at your option) any later version.
15594 + *
15595 + * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
15596 + */
15597 +
15598 +#ifndef _LQ_DEV_DWC_H__
15599 +#define _LQ_DEV_DWC_H__
15600 +
15601 +#include <lantiq_platform.h>
15602 +
15603 +extern void __init xway_register_dwc(int pin);
15604 +
15605 +#endif
15606 --- a/arch/mips/lantiq/xway/mach-arv45xx.c
15607 +++ b/arch/mips/lantiq/xway/mach-arv45xx.c
15608 @@ -23,6 +23,7 @@
15609  #include <lantiq_platform.h>
15610  
15611  #include "devices.h"
15612 +#include "dev-dwc_otg.h"
15613  
15614  #define ARV452_LATCH_SWITCH            (1 << 10)
15615  #define ARV752DPW22_LATCH_DEFAULT      (2)
15616 @@ -195,6 +196,7 @@
15617         lq_register_pci(&lq_pci_data);
15618         lq_register_wdt();
15619         arv45xx_register_ethernet();
15620 +       xway_register_dwc(14);
15621  }
15622  
15623  MIPS_MACHINE(LANTIQ_MACH_ARV4518,
15624 @@ -214,6 +216,7 @@
15625         lq_register_pci(&lq_pci_data);
15626         lq_register_wdt();
15627         arv45xx_register_ethernet();
15628 +       xway_register_dwc(28);
15629  }
15630  
15631  MIPS_MACHINE(LANTIQ_MACH_ARV452,