2 /* ==========================================================================
3 * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
8 * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
9 * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
10 * otherwise expressly agreed to in writing between Synopsys and you.
12 * The Software IS NOT an item of Licensed Software or Licensed Product under
13 * any End User Software License Agreement or Agreement for Licensed Product
14 * with Synopsys or any supplement thereto. You are permitted to use and
15 * redistribute this Software in source and binary forms, with or without
16 * modification, provided that redistributions of source code must retain this
17 * notice. You may not view, use, disclose, copy or distribute this file or
18 * any information contained herein except pursuant to this license grant from
19 * Synopsys. If you do not agree with this notice, including the disclaimer
20 * below, then you are not authorized to use the Software.
22 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33 * ========================================================================== */
34 #ifndef DWC_DEVICE_ONLY
39 * This file contains the implementation of the HCD. In Linux, the HCD
40 * implements the hc_driver API.
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/init.h>
46 #include <linux/device.h>
47 #include <linux/errno.h>
48 #include <linux/list.h>
49 #include <linux/interrupt.h>
50 #include <linux/string.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/version.h>
57 #include <linux/usb.h>
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
59 #include <../drivers/usb/core/hcd.h>
61 #include <linux/usb/hcd.h>
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
66 #define USB_URB_EP_LINKING 1
68 #define USB_URB_EP_LINKING 0
71 #include "dwc_otg_hcd_if.h"
72 #include "dwc_otg_dbg.h"
73 #include "dwc_otg_driver.h"
74 #include "dwc_otg_hcd.h"
77 #define __virt_to_bus __virt_to_phys
78 #define __bus_to_virt __phys_to_virt
79 #define __pfn_to_bus(x) __pfn_to_phys(x)
80 #define __bus_to_pfn(x) __phys_to_pfn(x)
83 extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
86 * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
87 * qualified with its direction (possible 32 endpoints per device).
89 #define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
90 ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
92 static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
94 extern bool fiq_enable;
96 /** @name Linux HC Driver API Functions */
98 /* manage i/o requests, device state */
99 static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
100 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
101 struct usb_host_endpoint *ep,
103 struct urb *urb, gfp_t mem_flags);
105 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
106 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
107 static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
109 #else /* kernels at or post 2.6.30 */
110 static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
111 struct urb *urb, int status);
112 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
114 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
115 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
116 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
118 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
119 extern int hcd_start(struct usb_hcd *hcd);
120 extern void hcd_stop(struct usb_hcd *hcd);
121 static int get_frame_number(struct usb_hcd *hcd);
122 extern int hub_status_data(struct usb_hcd *hcd, char *buf);
123 extern int hub_control(struct usb_hcd *hcd,
125 u16 wValue, u16 wIndex, char *buf, u16 wLength);
127 struct wrapper_priv_data {
128 dwc_otg_hcd_t *dwc_otg_hcd;
133 static struct hc_driver dwc_otg_hc_driver = {
135 .description = dwc_otg_hcd_name,
136 .product_desc = "DWC OTG Controller",
137 .hcd_priv_size = sizeof(struct wrapper_priv_data),
139 .irq = dwc_otg_hcd_irq,
141 .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
149 .urb_enqueue = dwc_otg_urb_enqueue,
150 .urb_dequeue = dwc_otg_urb_dequeue,
151 .endpoint_disable = endpoint_disable,
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
153 .endpoint_reset = endpoint_reset,
155 .get_frame_number = get_frame_number,
157 .hub_status_data = hub_status_data,
158 .hub_control = hub_control,
163 /** Gets the dwc_otg_hcd from a struct usb_hcd */
164 static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
166 struct wrapper_priv_data *p;
167 p = (struct wrapper_priv_data *)(hcd->hcd_priv);
168 return p->dwc_otg_hcd;
171 /** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
172 static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
174 return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
177 /** Gets the usb_host_endpoint associated with an URB. */
178 inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
180 struct usb_device *dev = urb->dev;
181 int ep_num = usb_pipeendpoint(urb->pipe);
183 if (usb_pipein(urb->pipe))
184 return dev->ep_in[ep_num];
186 return dev->ep_out[ep_num];
189 static int _disconnect(dwc_otg_hcd_t * hcd)
191 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
193 usb_hcd->self.is_b_host = 0;
197 static int _start(dwc_otg_hcd_t * hcd)
199 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
201 usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
207 static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
208 uint32_t * port_addr)
210 struct urb *urb = (struct urb *)urb_handle;
212 #if 1 //GRAYG - temporary
213 if (NULL == urb_handle)
214 DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
215 if (NULL == urb->dev)
216 DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
217 if (NULL == port_addr)
218 DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
221 if (NULL == urb->dev->tt->hub) {
222 DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
224 //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
225 *hub_addr = 0; //GRAYG
226 // we probably shouldn't have a transaction translator if
227 // there's no associated hub?
229 bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
230 if (urb->dev->tt->hub == bus->root_hub)
233 *hub_addr = urb->dev->tt->hub->devnum;
235 *port_addr = urb->dev->ttport;
238 *port_addr = urb->dev->ttport;
243 static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
245 struct urb *urb = (struct urb *)urb_handle;
246 return urb->dev->speed;
249 static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
251 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
252 return usb_hcd->self.b_hnp_enable;
255 static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
258 hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
259 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
260 hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
262 hcd_to_bus(hcd)->bandwidth_int_reqs++;
266 static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
269 hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
270 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
271 hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
273 hcd_to_bus(hcd)->bandwidth_int_reqs--;
278 * Sets the final status of an URB and returns it to the device driver. Any
279 * required cleanup of the URB is performed. The HCD lock should be held on
282 static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
283 dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
285 struct urb *urb = (struct urb *)urb_handle;
286 urb_tq_entry_t *new_entry;
288 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
289 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
290 __func__, urb, usb_pipedevice(urb->pipe),
291 usb_pipeendpoint(urb->pipe),
292 usb_pipein(urb->pipe) ? "IN" : "OUT", status);
293 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
295 for (i = 0; i < urb->number_of_packets; i++) {
296 DWC_PRINTF(" ISO Desc %d status: %d\n",
297 i, urb->iso_frame_desc[i].status);
301 new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
302 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
303 /* Convert status value. */
305 case -DWC_E_PROTOCOL:
308 case -DWC_E_IN_PROGRESS:
309 status = -EINPROGRESS;
320 case -DWC_E_OVERFLOW:
323 case -DWC_E_SHUTDOWN:
328 DWC_PRINTF("Uknown urb status %d\n", status);
333 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
336 urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
337 urb->actual_length = 0;
338 for (i = 0; i < urb->number_of_packets; ++i) {
339 urb->iso_frame_desc[i].actual_length =
340 dwc_otg_hcd_urb_get_iso_desc_actual_length
342 urb->actual_length += urb->iso_frame_desc[i].actual_length;
343 urb->iso_frame_desc[i].status =
344 dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
348 urb->status = status;
351 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
352 (urb->actual_length < urb->transfer_buffer_length)) {
353 urb->status = -EREMOTEIO;
357 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
358 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
359 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
361 free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
362 dwc_otg_hcd_get_ep_bandwidth(hcd,
367 DWC_FREE(dwc_otg_urb);
369 DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
370 urb->status = -EPROTO;
371 /* don't schedule the tasklet -
372 * directly return the packet here with error. */
373 #if USB_URB_EP_LINKING
374 usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
377 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
379 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
382 new_entry->urb = urb;
383 #if USB_URB_EP_LINKING
384 rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
386 usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
390 DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
392 DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
398 static struct dwc_otg_hcd_function_ops hcd_fops = {
400 .disconnect = _disconnect,
401 .hub_info = _hub_info,
403 .complete = _complete,
404 .get_b_hnp_enable = _get_b_hnp_enable,
409 static int simfiq_irq = -1;
411 void local_fiq_enable(void)
414 enable_irq(simfiq_irq);
417 void local_fiq_disable(void)
420 disable_irq(simfiq_irq);
423 irqreturn_t fiq_irq_handler(int irq, void *dev_id)
425 dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *)dev_id;
428 dwc_otg_fiq_fsm(dwc_otg_hcd->fiq_state, dwc_otg_hcd->core_if->core_params->host_channels);
430 dwc_otg_fiq_nop(dwc_otg_hcd->fiq_state);
436 static struct fiq_handler fh = {
442 static void hcd_init_fiq(void *cookie)
444 dwc_otg_device_t *otg_dev = cookie;
445 dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
453 if (claim_fiq(&fh)) {
454 DWC_ERROR("Can't claim FIQ");
457 DWC_WARN("FIQ on core %d", smp_processor_id());
458 DWC_WARN("FIQ ASM at %px length %d", &_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
459 set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
460 memset(®s,0,sizeof(regs));
462 regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
463 if (fiq_fsm_enable) {
464 regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
465 //regs.ARM_r10 = dwc_otg_hcd->dma;
466 regs.ARM_fp = (long) dwc_otg_fiq_fsm;
468 regs.ARM_fp = (long) dwc_otg_fiq_nop;
471 regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
473 // __show_regs(®s);
477 dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
478 //Set the mphi periph to the required registers
479 dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
480 if (otg_dev->os_dep.use_swirq) {
481 dwc_otg_hcd->fiq_state->mphi_regs.swirq_set =
482 otg_dev->os_dep.mphi_base + 0x1f0;
483 dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr =
484 otg_dev->os_dep.mphi_base + 0x1f4;
485 DWC_WARN("Fake MPHI regs_base at %px",
486 dwc_otg_hcd->fiq_state->mphi_regs.base);
488 dwc_otg_hcd->fiq_state->mphi_regs.ctrl =
489 otg_dev->os_dep.mphi_base + 0x4c;
490 dwc_otg_hcd->fiq_state->mphi_regs.outdda
491 = otg_dev->os_dep.mphi_base + 0x28;
492 dwc_otg_hcd->fiq_state->mphi_regs.outddb
493 = otg_dev->os_dep.mphi_base + 0x2c;
494 dwc_otg_hcd->fiq_state->mphi_regs.intstat
495 = otg_dev->os_dep.mphi_base + 0x50;
496 DWC_WARN("MPHI regs_base at %px",
497 dwc_otg_hcd->fiq_state->mphi_regs.base);
499 //Enable mphi peripheral
500 writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
502 if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
503 DWC_WARN("MPHI periph has been enabled");
505 DWC_WARN("MPHI periph has NOT been enabled");
508 // Enable FIQ interrupt from USB peripheral
510 irq = otg_dev->os_dep.fiq_num;
513 DWC_ERROR("Can't get SIM-FIQ irq");
517 retval = request_irq(irq, fiq_irq_handler, 0, "dwc_otg_sim-fiq", dwc_otg_hcd);
520 DWC_ERROR("Unable to request SIM-FIQ irq\n");
526 #ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
527 irq = otg_dev->os_dep.fiq_num;
529 irq = INTERRUPT_VC_USB;
532 DWC_ERROR("Can't get FIQ irq");
536 * We could take an interrupt immediately after enabling the FIQ.
537 * Ensure coherency of hcd->fiq_state.
547 * Initializes the HCD. This function allocates memory for and initializes the
548 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
549 * USB bus with the core and calls the hc_driver->start() function. It returns
550 * a negative error on failure.
552 int hcd_init(dwc_bus_dev_t *_dev)
554 struct usb_hcd *hcd = NULL;
555 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
556 dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
560 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
562 /* Set device flags indicating whether the HCD supports DMA. */
563 if (dwc_otg_is_dma_enable(otg_dev->core_if))
564 dmamask = DMA_BIT_MASK(32);
568 #if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
569 dma_set_mask(&_dev->dev, dmamask);
570 dma_set_coherent_mask(&_dev->dev, dmamask);
571 #elif defined(PCI_INTERFACE)
572 pci_set_dma_mask(_dev, dmamask);
573 pci_set_consistent_dma_mask(_dev, dmamask);
577 * Allocate memory for the base HCD plus the DWC OTG HCD.
578 * Initialize the base HCD.
580 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
581 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
583 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
585 // hcd->uses_new_polling = 1;
593 hcd->regs = otg_dev->os_dep.base;
596 /* Initialize the DWC OTG HCD. */
597 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
601 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
603 otg_dev->hcd = dwc_otg_hcd;
604 otg_dev->hcd->otg_dev = otg_dev;
607 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if))
611 hcd_init_fiq(otg_dev);
613 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
618 if (num_online_cpus() > 1) {
620 * bcm2709: can run the FIQ on a separate core to IRQs.
621 * Ensure driver state is visible to other cores before setting up the FIQ.
624 smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
626 smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
631 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
632 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
633 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
634 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
636 /* Don't support SG list at this point */
637 hcd->self.sg_tablesize = 0;
640 * Finish generic HCD initialization and start the HCD. This function
641 * allocates the DMA buffer pool, registers the USB bus, requests the
642 * IRQ line, and calls hcd_start method.
644 retval = usb_add_hcd(hcd, otg_dev->os_dep.irq_num, IRQF_SHARED);
649 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
660 * Frees memory and resources associated with the HCD and deregisters the bus.
662 void hcd_remove(dwc_bus_dev_t *_dev)
664 dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
665 dwc_otg_hcd_t *dwc_otg_hcd;
668 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
671 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
675 dwc_otg_hcd = otg_dev->hcd;
678 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
682 hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
686 "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
691 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
692 dwc_otg_hcd_remove(dwc_otg_hcd);
696 /* =========================================================================
697 * Linux HC Driver Functions
698 * ========================================================================= */
700 /** Initializes the DWC_otg controller and its root hub and prepares it for host
701 * mode operation. Activates the root port. Returns 0 on success and a negative
702 * error code on failure. */
703 int hcd_start(struct usb_hcd *hcd)
705 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
708 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
709 bus = hcd_to_bus(hcd);
711 hcd->state = HC_STATE_RUNNING;
712 if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
716 /* Initialize and connect root hub if one is not already attached */
718 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
719 /* Inform the HUB driver to resume. */
720 usb_hcd_resume_root_hub(hcd);
727 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
730 void hcd_stop(struct usb_hcd *hcd)
732 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
734 dwc_otg_hcd_stop(dwc_otg_hcd);
737 /** Returns the current frame number. */
738 static int get_frame_number(struct usb_hcd *hcd)
741 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
742 hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
743 if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
744 return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
746 return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
750 static void dump_urb_info(struct urb *urb, char *fn_name)
752 DWC_PRINTF("%s, urb %p\n", fn_name, urb);
753 DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
754 DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
755 (usb_pipein(urb->pipe) ? "IN" : "OUT"));
756 DWC_PRINTF(" Endpoint type: %s\n", ( {
758 switch (usb_pipetype(urb->pipe)) {
760 pipetype = "CONTROL"; break; case PIPE_BULK:
761 pipetype = "BULK"; break; case PIPE_INTERRUPT:
762 pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
763 pipetype = "ISOCHRONOUS"; break; default:
764 pipetype = "UNKNOWN"; break;};
767 DWC_PRINTF(" Speed: %s\n", ( {
768 char *speed; switch (urb->dev->speed) {
770 speed = "HIGH"; break; case USB_SPEED_FULL:
771 speed = "FULL"; break; case USB_SPEED_LOW:
772 speed = "LOW"; break; default:
773 speed = "UNKNOWN"; break;};
776 DWC_PRINTF(" Max packet size: %d\n",
777 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
778 DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
779 DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
780 urb->transfer_buffer, (void *)urb->transfer_dma);
781 DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
782 urb->setup_packet, (void *)urb->setup_dma);
783 DWC_PRINTF(" Interval: %d\n", urb->interval);
784 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
786 for (i = 0; i < urb->number_of_packets; i++) {
787 DWC_PRINTF(" ISO Desc %d:\n", i);
788 DWC_PRINTF(" offset: %d, length %d\n",
789 urb->iso_frame_desc[i].offset,
790 urb->iso_frame_desc[i].length);
796 /** Starts processing a USB transfer request specified by a USB Request Block
797 * (URB). mem_flags indicates the type of memory allocation to use while
798 * processing this URB. */
799 static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
800 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
801 struct usb_host_endpoint *ep,
803 struct urb *urb, gfp_t mem_flags)
806 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
807 struct usb_host_endpoint *ep = urb->ep;
809 dwc_irqflags_t irqflags;
810 void **ref_ep_hcpriv = &ep->hcpriv;
811 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
812 dwc_otg_hcd_urb_t *dwc_otg_urb;
814 int alloc_bandwidth = 0;
820 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
821 dump_urb_info(urb, "dwc_otg_urb_enqueue");
824 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
825 || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
826 if (!dwc_otg_hcd_is_bandwidth_allocated
827 (dwc_otg_hcd, ref_ep_hcpriv)) {
832 switch (usb_pipetype(urb->pipe)) {
834 ep_type = USB_ENDPOINT_XFER_CONTROL;
836 case PIPE_ISOCHRONOUS:
837 ep_type = USB_ENDPOINT_XFER_ISOC;
840 ep_type = USB_ENDPOINT_XFER_BULK;
843 ep_type = USB_ENDPOINT_XFER_INT;
846 DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
849 /* # of packets is often 0 - do we really need to call this then? */
850 dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
851 urb->number_of_packets,
852 mem_flags == GFP_ATOMIC ? 1 : 0);
854 if(dwc_otg_urb == NULL)
857 if (!dwc_otg_urb && urb->number_of_packets)
860 dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
861 usb_pipeendpoint(urb->pipe), ep_type,
862 usb_pipein(urb->pipe),
863 usb_maxpacket(urb->dev, urb->pipe,
864 !(usb_pipein(urb->pipe))));
866 buf = urb->transfer_buffer;
867 if (hcd_uses_dma(hcd) && !buf && urb->transfer_buffer_length) {
869 * Calculate virtual address from physical address,
870 * because some class driver may not fill transfer_buffer.
871 * In Buffer DMA mode virual address is used,
872 * when handling non DWORD aligned buffers.
874 buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
875 dev_warn_once(&urb->dev->dev,
876 "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
877 &urb->transfer_dma, buf);
880 if (!buf && urb->transfer_buffer_length) {
881 DWC_FREE(dwc_otg_urb);
882 DWC_ERROR("transfer_buffer is NULL in PIO mode or both "
883 "transfer_buffer and transfer_dma are NULL in DMA mode\n");
887 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
888 flags |= URB_GIVEBACK_ASAP;
889 if (urb->transfer_flags & URB_ZERO_PACKET)
890 flags |= URB_SEND_ZERO_PACKET;
892 dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
894 urb->transfer_buffer_length,
896 urb->setup_dma, flags, urb->interval);
898 for (i = 0; i < urb->number_of_packets; ++i) {
899 dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
901 iso_frame_desc[i].offset,
903 iso_frame_desc[i].length);
906 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
907 urb->hcpriv = dwc_otg_urb;
908 #if USB_URB_EP_LINKING
909 retval = usb_hcd_link_urb_to_ep(hcd, urb);
913 retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
914 /*(dwc_otg_qh_t **)*/
917 if (alloc_bandwidth) {
918 allocate_bus_bandwidth(hcd,
919 dwc_otg_hcd_get_ep_bandwidth(
920 dwc_otg_hcd, *ref_ep_hcpriv),
924 DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
925 #if USB_URB_EP_LINKING
926 usb_hcd_unlink_urb_from_ep(hcd, urb);
928 DWC_FREE(dwc_otg_urb);
930 if (retval == -DWC_E_NO_DEVICE)
934 #if USB_URB_EP_LINKING
937 DWC_FREE(dwc_otg_urb);
941 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
945 /** Aborts/cancels a USB transfer request. Always returns 0 to indicate
947 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
948 static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
950 static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
953 dwc_irqflags_t flags;
954 dwc_otg_hcd_t *dwc_otg_hcd;
957 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
959 dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
962 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
963 dump_urb_info(urb, "dwc_otg_urb_dequeue");
967 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
968 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
970 if(urb->hcpriv != NULL) {
971 dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
972 (dwc_otg_hcd_urb_t *)urb->hcpriv);
974 DWC_FREE(urb->hcpriv);
980 /* Higher layer software sets URB status. */
981 #if USB_URB_EP_LINKING
982 usb_hcd_unlink_urb_from_ep(hcd, urb);
984 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
987 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
988 usb_hcd_giveback_urb(hcd, urb);
990 usb_hcd_giveback_urb(hcd, urb, status);
992 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
993 DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
994 DWC_PRINTF(" 1urb->status = %d\n", urb->status);
996 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
998 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
999 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
1006 /* Frees resources in the DWC_otg controller related to a given endpoint. Also
1007 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
1008 * must already be dequeued. */
1009 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1011 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1013 DWC_DEBUGPL(DBG_HCD,
1014 "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
1015 "endpoint=%d\n", ep->desc.bEndpointAddress,
1016 dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
1017 dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
1021 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
1022 /* Resets endpoint specific parameter values, in current version used to reset
1023 * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
1024 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1026 dwc_irqflags_t flags;
1027 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1029 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n",
1030 ep->desc.bEndpointAddress);
1032 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1034 dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
1036 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1040 /** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
1041 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
1044 * This function is called by the USB core when an interrupt occurs */
1045 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
1047 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1048 int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
1050 S3C2410X_CLEAR_EINTPEND();
1052 return IRQ_RETVAL(retval);
1055 /** Creates Status Change bitmap for the root hub and root port. The bitmap is
1056 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
1057 * is the status change indicator for the single root port. Returns 1 if either
1058 * change indicator is 1, otherwise returns 0. */
1059 int hub_status_data(struct usb_hcd *hcd, char *buf)
1061 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1064 buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
1066 return (buf[0] != 0);
1069 /** Handles hub class-specific requests. */
1070 int hub_control(struct usb_hcd *hcd,
1071 u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
1075 retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
1076 typeReq, wValue, wIndex, buf, wLength);
1079 case -DWC_E_INVALID:
1087 #endif /* DWC_DEVICE_ONLY */