2 * Copyright 2004-2012 Freescale Semiconductor, Inc. All Rights Reserved.
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/ioport.h>
19 #include <linux/types.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/workqueue.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/timer.h>
27 #include <linux/list.h>
28 #include <linux/interrupt.h>
29 #include <linux/proc_fs.h>
31 #include <linux/jiffies.h>
32 #include <linux/moduleparam.h>
33 #include <linux/device.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
36 #include <linux/usb/otg.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/platform_device.h>
39 #include <linux/fsl_devices.h>
40 #include <linux/dmapool.h>
43 #include <asm/processor.h>
44 #include <asm/byteorder.h>
46 #include <asm/system.h>
47 #include <asm/mach-types.h>
48 #include <asm/unaligned.h>
50 #include <asm/cacheflush.h>
52 #include "arcotg_udc.h"
53 #include <mach/arc_otg.h>
54 #include <mach/iram.h>
56 #define DRIVER_DESC "ARC USBOTG Device Controller driver"
57 #define DRIVER_AUTHOR "Freescale Semiconductor"
58 #define DRIVER_VERSION "1 August 2005"
60 #ifdef CONFIG_PPC_MPC512x
61 #define BIG_ENDIAN_DESC
64 #ifdef BIG_ENDIAN_DESC
65 #define cpu_to_hc32(x) (x)
66 #define hc32_to_cpu(x) (x)
68 #define cpu_to_hc32(x) cpu_to_le32((x))
69 #define hc32_to_cpu(x) le32_to_cpu((x))
71 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
72 DEFINE_MUTEX(udc_resume_mutex);
73 extern void usb_debounce_id_vbus(void);
74 static const char driver_name[] = "fsl-usb2-udc";
75 static const char driver_desc[] = DRIVER_DESC;
77 volatile static struct usb_dr_device *dr_regs;
78 volatile static struct usb_sys_interface *usb_sys_regs;
80 #include "imx_usb_charger.c" /* support for usb charger detect */
82 /* it is initialized in probe() */
83 static struct fsl_udc *udc_controller;
85 #ifdef POSTPONE_FREE_LAST_DTD
86 static struct ep_td_struct *last_free_td;
88 static const struct usb_endpoint_descriptor
90 .bLength = USB_DT_ENDPOINT_SIZE,
91 .bDescriptorType = USB_DT_ENDPOINT,
92 .bEndpointAddress = 0,
93 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
94 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
96 static const size_t g_iram_size = IRAM_TD_PPH_SIZE;
97 static unsigned long g_iram_base;
98 static __iomem void *g_iram_addr;
100 typedef int (*dev_sus)(struct device *dev, pm_message_t state);
101 typedef int (*dev_res) (struct device *dev);
102 static int udc_suspend(struct fsl_udc *udc);
103 static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state);
104 static int fsl_udc_resume(struct platform_device *pdev);
105 static void fsl_ep_fifo_flush(struct usb_ep *_ep);
106 static void gadget_wait_line_to_se0(void);
108 #ifdef CONFIG_USB_OTG
109 /* Get platform resource from OTG driver */
110 extern struct resource *otg_get_resources(void);
113 extern void fsl_platform_set_test_mode(struct fsl_usb2_platform_data *pdata, enum usb_test_mode mode);
115 #ifdef CONFIG_WORKAROUND_ARCUSB_REG_RW
116 static void safe_writel(u32 val32, volatile u32 *addr)
118 __asm__ ("swp %0, %0, [%1]" : : "r"(val32), "r"(addr));
123 #define fsl_readl(addr) in_le32((addr))
124 #define fsl_writel(addr, val32) out_le32((val32), (addr))
125 #elif defined (CONFIG_WORKAROUND_ARCUSB_REG_RW)
126 #define fsl_readl(addr) readl((addr))
127 #define fsl_writel(val32, addr) safe_writel(val32, addr)
129 #define fsl_readl(addr) readl((addr))
130 #define fsl_writel(addr, val32) writel((addr), (val32))
133 /********************************************************************
134 * Internal Used Function
135 ********************************************************************/
138 static void dump_ep_queue(struct fsl_ep *ep)
142 struct ep_td_struct *dtd;
144 if (list_empty(&ep->queue)) {
145 pr_debug("udc: empty\n");
149 ep_index = ep_index(ep) * 2 + ep_is_in(ep);
150 pr_debug("udc: ep=0x%p index=%d\n", ep, ep_index);
152 list_for_each_entry(req, &ep->queue, queue) {
153 pr_debug("udc: req=0x%p dTD count=%d\n", req, req->dtd_count);
154 pr_debug("udc: dTD head=0x%p tail=0x%p\n", req->head,
160 if (le32_to_cpu(dtd->next_td_ptr) & DTD_NEXT_TERMINATE)
161 break; /* end of dTD list */
163 dtd = dtd->next_td_virt;
168 static inline void dump_ep_queue(struct fsl_ep *ep)
173 #if (defined CONFIG_ARCH_MX35 || defined CONFIG_ARCH_MX25)
175 * The Phy at MX35 and MX25 have bugs, it must disable, and re-eable phy
176 * if the phy clock is disabled before
178 static void reset_phy(void)
181 phyctrl = fsl_readl(&dr_regs->phyctrl1);
182 phyctrl &= ~PHY_CTRL0_USBEN;
183 fsl_writel(phyctrl, &dr_regs->phyctrl1);
185 phyctrl = fsl_readl(&dr_regs->phyctrl1);
186 phyctrl |= PHY_CTRL0_USBEN;
187 fsl_writel(phyctrl, &dr_regs->phyctrl1);
190 static void reset_phy(void){; }
192 /*-----------------------------------------------------------------
193 * done() - retire a request; caller blocked irqs
194 * @status : request status to be set, only works when
195 * request is still in progress.
196 *--------------------------------------------------------------*/
197 static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
199 struct fsl_udc *udc = NULL;
200 unsigned char stopped = ep->stopped;
201 struct ep_td_struct *curr_td, *next_td;
204 udc = (struct fsl_udc *)ep->udc;
205 /* Removed the req from fsl_ep->queue */
206 list_del_init(&req->queue);
208 /* req.status should be set as -EINPROGRESS in ep_queue() */
209 if (req->req.status == -EINPROGRESS)
210 req->req.status = status;
212 status = req->req.status;
214 /* Free dtd for the request */
216 for (j = 0; j < req->dtd_count; j++) {
218 if (j != req->dtd_count - 1) {
219 next_td = curr_td->next_td_virt;
220 #ifdef POSTPONE_FREE_LAST_DTD
221 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
223 if (last_free_td != NULL)
224 dma_pool_free(udc->td_pool, last_free_td,
225 last_free_td->td_dma);
226 last_free_td = curr_td;
231 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
235 if (USE_MSC_WR(req->req.length)) {
237 memmove(req->req.buf, req->req.buf + 1, MSC_BULK_CB_WRAP_LEN);
241 dma_unmap_single(ep->udc->gadget.dev.parent,
242 req->req.dma, req->req.length,
246 req->req.dma = DMA_ADDR_INVALID;
249 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
250 req->req.dma, req->req.length,
255 if (status && (status != -ESHUTDOWN))
256 VDBG("complete %s req %p stat %d len %u/%u",
257 ep->ep.name, &req->req, status,
258 req->req.actual, req->req.length);
262 spin_unlock(&ep->udc->lock);
263 /* complete() is from gadget layer,
264 * eg fsg->bulk_in_complete() */
265 if (req->req.complete)
266 req->req.complete(&ep->ep, &req->req);
268 spin_lock(&ep->udc->lock);
269 ep->stopped = stopped;
272 /*-----------------------------------------------------------------
273 * nuke(): delete all requests related to this ep
274 * called with spinlock held
275 *--------------------------------------------------------------*/
276 static void nuke(struct fsl_ep *ep, int status)
280 * At udc stop mode, the clock is already off
281 * So flush fifo, should be done at clock on mode.
283 if (!ep->udc->stopped)
284 fsl_ep_fifo_flush(&ep->ep);
286 /* Whether this eq has request linked */
287 while (!list_empty(&ep->queue)) {
288 struct fsl_req *req = NULL;
290 req = list_entry(ep->queue.next, struct fsl_req, queue);
291 done(ep, req, status);
296 /*------------------------------------------------------------------
297 Internal Hardware related function
298 ------------------------------------------------------------------*/
299 static void dr_discharge_line(struct fsl_usb2_platform_data *pdata, bool enable)
301 /* enable/disable pulldown dp and dm */
302 if (pdata->dr_discharge_line) {
303 pdata->dr_discharge_line(enable);
305 * Some platforms, like mx6x, are very slow change line state
306 * to SE0 for dp and dm.
307 * So, we need to discharge dp and dm, otherwise there is a wakeup interrupt
308 * after we enable the wakeup function.
311 gadget_wait_line_to_se0();
317 dr_wake_up_enable(struct fsl_udc *udc, bool enable)
319 struct fsl_usb2_platform_data *pdata;
322 if (pdata && pdata->wake_up_enable)
323 pdata->wake_up_enable(pdata, enable);
326 static inline void dr_clk_gate(bool on)
328 struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
330 if (!pdata || !pdata->usb_clock_for_pm)
332 pdata->usb_clock_for_pm(on);
337 static void dr_phy_low_power_mode(struct fsl_udc *udc, bool enable)
339 struct fsl_usb2_platform_data *pdata = udc->pdata;
342 spin_lock_irqsave(&pdata->lock, flags);
344 if (pdata && pdata->phy_lowpower_suspend) {
345 pdata->phy_lowpower_suspend(pdata, enable);
348 portsc = fsl_readl(&dr_regs->portsc1);
349 portsc |= PORTSCX_PHY_LOW_POWER_SPD;
350 fsl_writel(portsc, &dr_regs->portsc1);
352 portsc = fsl_readl(&dr_regs->portsc1);
353 portsc &= ~PORTSCX_PHY_LOW_POWER_SPD;
354 fsl_writel(portsc, &dr_regs->portsc1);
357 pdata->lowpower = enable;
358 spin_unlock_irqrestore(&pdata->lock, flags);
361 static int dr_controller_setup(struct fsl_udc *udc)
363 unsigned int tmp = 0, portctrl = 0;
364 unsigned int __attribute((unused)) ctrl = 0;
365 unsigned long timeout;
366 struct fsl_usb2_platform_data *pdata;
368 #define FSL_UDC_RESET_TIMEOUT 1000
370 /* before here, make sure dr_regs has been initialized */
375 /* Stop and reset the usb controller */
376 tmp = fsl_readl(&dr_regs->usbcmd);
377 tmp &= ~USB_CMD_RUN_STOP;
378 fsl_writel(tmp, &dr_regs->usbcmd);
380 tmp = fsl_readl(&dr_regs->usbcmd);
381 tmp |= USB_CMD_CTRL_RESET;
382 fsl_writel(tmp, &dr_regs->usbcmd);
384 /* Wait for reset to complete */
385 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
386 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
387 if (time_after(jiffies, timeout)) {
388 ERR("udc reset timeout! \n");
394 /* Set the controller as device mode */
395 tmp = fsl_readl(&dr_regs->usbmode);
396 tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
397 tmp |= USB_MODE_CTRL_MODE_DEVICE;
398 /* Disable Setup Lockout */
399 tmp |= USB_MODE_SETUP_LOCK_OFF;
402 fsl_writel(tmp, &dr_regs->usbmode);
404 dr_discharge_line(pdata, true);
406 fsl_platform_set_device_mode(pdata);
408 /* Clear the setup status */
409 fsl_writel(0xffffffff, &dr_regs->usbsts);
411 tmp = udc->ep_qh_dma;
412 tmp &= USB_EP_LIST_ADDRESS_MASK;
413 fsl_writel(tmp, &dr_regs->endpointlistaddr);
415 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
416 (int)udc->ep_qh, (int)tmp,
417 fsl_readl(&dr_regs->endpointlistaddr));
419 /* Config PHY interface */
420 portctrl = fsl_readl(&dr_regs->portsc1);
421 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
422 switch (udc->phy_mode) {
423 case FSL_USB2_PHY_ULPI:
424 portctrl |= PORTSCX_PTS_ULPI;
426 case FSL_USB2_PHY_UTMI_WIDE:
427 portctrl |= PORTSCX_PTW_16BIT;
429 case FSL_USB2_PHY_UTMI:
430 portctrl |= PORTSCX_PTS_UTMI;
432 case FSL_USB2_PHY_SERIAL:
433 portctrl |= PORTSCX_PTS_FSLS;
438 fsl_writel(portctrl, &dr_regs->portsc1);
440 if (pdata->change_ahb_burst) {
441 /* if usb should not work in default INCRx mode */
442 tmp = fsl_readl(&dr_regs->sbuscfg);
443 tmp = (tmp & ~0x07) | pdata->ahb_burst_mode;
444 fsl_writel(tmp, &dr_regs->sbuscfg);
447 if (pdata->have_sysif_regs) {
448 /* Config control enable i/o output, cpu endian register */
449 ctrl = __raw_readl(&usb_sys_regs->control);
450 ctrl |= USB_CTRL_IOENB;
451 __raw_writel(ctrl, &usb_sys_regs->control);
454 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
455 /* Turn on cache snooping hardware, since some PowerPC platforms
456 * wholly rely on hardware to deal with cache coherent. */
458 if (pdata->have_sysif_regs) {
459 /* Setup Snooping for all the 4GB space */
460 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
461 __raw_writel(tmp, &usb_sys_regs->snoop1);
462 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
463 __raw_writel(tmp, &usb_sys_regs->snoop2);
470 /* Enable DR irq and set controller to run state */
471 static void dr_controller_run(struct fsl_udc *udc)
475 udc_controller->usb_state = USB_STATE_ATTACHED;
476 udc_controller->ep0_dir = 0;
478 fsl_platform_pullup_enable(udc->pdata);
480 /* Enable DR irq reg */
481 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
482 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
483 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
485 fsl_writel(temp, &dr_regs->usbintr);
488 temp = fsl_readl(&dr_regs->otgsc);
489 temp |= OTGSC_B_SESSION_VALID_IRQ_EN;
490 fsl_writel(temp, &dr_regs->otgsc);
492 /* If vbus not on and used low power mode */
493 if (!(temp & OTGSC_B_SESSION_VALID)) {
494 /* Set stopped before low power mode */
495 udc->vbus_active = false;
498 dr_wake_up_enable(udc, true);
499 /* enter lower power mode */
500 dr_phy_low_power_mode(udc, true);
501 printk(KERN_DEBUG "%s: udc enter low power mode \n", __func__);
503 #ifdef CONFIG_ARCH_MX37
505 add some delay for USB timing issue. USB may be
506 recognize as FS device
507 during USB gadget remote wake up function
511 /* Clear stopped bit */
514 /* disable pulldown dp and dm */
515 dr_discharge_line(udc->pdata, false);
516 udc->vbus_active = true;
517 /* notify vbus is connected */
518 imx_usb_vbus_connect(&udc->charger);
519 printk(KERN_DEBUG "%s: udc out low power mode\n", __func__);
525 static void dr_controller_stop(struct fsl_udc *udc)
529 pr_debug("%s\n", __func__);
531 /* if we're in OTG mode, and the Host is currently using the port,
532 * stop now and don't rip the controller out from under the
535 if (udc->gadget.is_otg) {
536 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
537 pr_debug("udc: Leaving early\n");
542 /* disable all INTR */
543 fsl_writel(0, &dr_regs->usbintr);
545 /* disable wake up */
546 dr_wake_up_enable(udc, false);
547 /* disable BSV irq */
548 tmp = fsl_readl(&dr_regs->otgsc);
549 tmp &= ~OTGSC_B_SESSION_VALID_IRQ_EN;
550 fsl_writel(tmp, &dr_regs->otgsc);
552 /* Set stopped bit for isr */
555 /* disable IO output */
556 /* usb_sys_regs->control = 0; */
558 fsl_platform_pullup_disable(udc->pdata);
560 /* set controller to Stop */
561 tmp = fsl_readl(&dr_regs->usbcmd);
562 tmp &= ~USB_CMD_RUN_STOP;
563 fsl_writel(tmp, &dr_regs->usbcmd);
568 void dr_ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_type)
570 unsigned int tmp_epctrl = 0;
572 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
575 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
576 tmp_epctrl |= EPCTRL_TX_ENABLE;
577 tmp_epctrl |= ((unsigned int)(ep_type)
578 << EPCTRL_TX_EP_TYPE_SHIFT);
581 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
582 tmp_epctrl |= EPCTRL_RX_ENABLE;
583 tmp_epctrl |= ((unsigned int)(ep_type)
584 << EPCTRL_RX_EP_TYPE_SHIFT);
587 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
591 dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
595 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
598 /* set the stall bit */
600 tmp_epctrl |= EPCTRL_TX_EP_STALL;
602 tmp_epctrl |= EPCTRL_RX_EP_STALL;
604 /* clear the stall bit and reset data toggle */
606 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
607 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
609 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
610 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
613 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
616 /* Get stall status of a specific ep
617 Return: 0: not stalled; 1:stalled */
618 static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
622 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
624 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
626 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
629 /********************************************************************
630 Internal Structure Build up functions
631 ********************************************************************/
633 /*------------------------------------------------------------------
634 * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
635 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
637 ------------------------------------------------------------------*/
638 static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
639 unsigned char dir, unsigned char ep_type,
640 unsigned int max_pkt_len,
641 unsigned int zlt, unsigned char mult)
643 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
644 unsigned int tmp = 0;
646 /* set the Endpoint Capabilites in QH */
648 case USB_ENDPOINT_XFER_CONTROL:
649 /* Interrupt On Setup (IOS). for control ep */
650 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
653 case USB_ENDPOINT_XFER_ISOC:
654 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
655 | (mult << EP_QUEUE_HEAD_MULT_POS);
657 case USB_ENDPOINT_XFER_BULK:
658 case USB_ENDPOINT_XFER_INT:
659 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
662 VDBG("error ep type is %d", ep_type);
666 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
667 p_QH->max_pkt_length = cpu_to_hc32(tmp);
672 /* Setup qh structure and ep register for ep0. */
673 static void ep0_setup(struct fsl_udc *udc)
675 /* the intialization of an ep includes: fields in QH, Regs,
677 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
678 USB_MAX_CTRL_PAYLOAD, 0, 0);
679 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
680 USB_MAX_CTRL_PAYLOAD, 0, 0);
681 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
682 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
688 /***********************************************************************
689 Endpoint Management Functions
690 ***********************************************************************/
692 /*-------------------------------------------------------------------------
693 * when configurations are set, or when interface settings change
694 * for example the do_set_interface() in gadget layer,
695 * the driver will enable or disable the relevant endpoints
696 * ep0 doesn't use this routine. It is always enabled.
697 -------------------------------------------------------------------------*/
698 static int fsl_ep_enable(struct usb_ep *_ep,
699 const struct usb_endpoint_descriptor *desc)
701 struct fsl_udc *udc = NULL;
702 struct fsl_ep *ep = NULL;
703 unsigned short max = 0;
704 unsigned char mult = 0, zlt;
705 int retval = -EINVAL;
706 unsigned long flags = 0;
708 ep = container_of(_ep, struct fsl_ep, ep);
710 pr_debug("udc: %s ep.name=%s\n", __func__, ep->ep.name);
711 /* catch various bogus parameters */
712 if (!_ep || !desc || ep->desc
713 || (desc->bDescriptorType != USB_DT_ENDPOINT))
718 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
721 max = le16_to_cpu(desc->wMaxPacketSize);
723 /* Disable automatic zlp generation. Driver is reponsible to indicate
724 * explicitly through req->req.zero. This is needed to enable multi-td
728 /* Assume the max packet size from gadget is always correct */
729 switch (desc->bmAttributes & 0x03) {
730 case USB_ENDPOINT_XFER_CONTROL:
731 case USB_ENDPOINT_XFER_BULK:
732 case USB_ENDPOINT_XFER_INT:
733 /* mult = 0. Execute N Transactions as demonstrated by
734 * the USB variable length packet protocol where N is
735 * computed using the Maximum Packet Length (dQH) and
736 * the Total Bytes field (dTD) */
739 case USB_ENDPOINT_XFER_ISOC:
740 /* Calculate transactions needed for high bandwidth iso */
741 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
742 max = max & 0x7ff; /* bit 0~10 */
743 /* 3 transactions at most */
751 spin_lock_irqsave(&udc->lock, flags);
752 ep->ep.maxpacket = max;
756 /* Controller related setup */
757 /* Init EPx Queue Head (Ep Capabilites field in QH
758 * according to max, zlt, mult) */
759 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
760 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
761 ? USB_SEND : USB_RECV),
762 (unsigned char) (desc->bmAttributes
763 & USB_ENDPOINT_XFERTYPE_MASK),
766 /* Init endpoint ctrl register */
767 dr_ep_setup((unsigned char) ep_index(ep),
768 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
769 ? USB_SEND : USB_RECV),
770 (unsigned char) (desc->bmAttributes
771 & USB_ENDPOINT_XFERTYPE_MASK));
773 spin_unlock_irqrestore(&udc->lock, flags);
776 VDBG("enabled %s (ep%d%s) maxpacket %d", ep->ep.name,
777 ep->desc->bEndpointAddress & 0x0f,
778 (desc->bEndpointAddress & USB_DIR_IN)
779 ? "in" : "out", max);
784 /*---------------------------------------------------------------------
785 * @ep : the ep being unconfigured. May not be ep0
786 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
787 *---------------------------------------------------------------------*/
788 static int fsl_ep_disable(struct usb_ep *_ep)
790 struct fsl_udc *udc = NULL;
791 struct fsl_ep *ep = NULL;
792 unsigned long flags = 0;
796 ep = container_of(_ep, struct fsl_ep, ep);
797 if (!_ep || !ep->desc) {
798 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
802 /* disable ep on controller */
803 ep_num = ep_index(ep);
804 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
806 epctrl &= ~EPCTRL_TX_ENABLE;
808 epctrl &= ~EPCTRL_RX_ENABLE;
809 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
811 udc = (struct fsl_udc *)ep->udc;
812 spin_lock_irqsave(&udc->lock, flags);
813 /* nuke all pending requests (does flush) */
814 nuke(ep, -ESHUTDOWN);
818 spin_unlock_irqrestore(&udc->lock, flags);
820 VDBG("disabled %s OK", _ep->name);
824 /*---------------------------------------------------------------------
825 * allocate a request object used by this endpoint
826 * the main operation is to insert the req->queue to the eq->queue
827 * Returns the request, or null if one could not be allocated
828 *---------------------------------------------------------------------*/
829 static struct usb_request *
830 fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
832 struct fsl_req *req = NULL;
834 req = kzalloc(sizeof *req, gfp_flags);
838 req->req.dma = DMA_ADDR_INVALID;
839 pr_debug("udc: req=0x%p set req.dma=0x%x\n", req, req->req.dma);
840 INIT_LIST_HEAD(&req->queue);
845 static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
847 struct fsl_req *req = NULL;
849 req = container_of(_req, struct fsl_req, req);
855 static void update_qh(struct fsl_req *req)
857 struct fsl_ep *ep = req->ep;
858 int i = ep_index(ep) * 2 + ep_is_in(ep);
860 struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
862 /* Write dQH next pointer and terminate bit to 0 */
863 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
864 if (NEED_IRAM(req->ep)) {
865 /* set next dtd stop bit,ensure only one dtd in this list */
866 req->cur->next_td_ptr |= cpu_to_hc32(DTD_NEXT_TERMINATE);
867 temp = req->cur->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
869 dQH->next_dtd_ptr = cpu_to_hc32(temp);
870 /* Clear active and halt bit */
871 temp = cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
872 | EP_QUEUE_HEAD_STATUS_HALT));
873 dQH->size_ioc_int_sts &= temp;
875 #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
878 /* Prime endpoint by writing 1 to ENDPTPRIME */
880 ? (1 << (ep_index(ep) + 16))
881 : (1 << (ep_index(ep)));
882 fsl_writel(temp, &dr_regs->endpointprime);
885 /*-------------------------------------------------------------------------*/
886 static int fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
888 u32 temp, bitmask, tmp_stat;
890 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
891 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
893 bitmask = ep_is_in(ep)
894 ? (1 << (ep_index(ep) + 16))
895 : (1 << (ep_index(ep)));
899 * - the request is empty, and
900 * - the request is not the status request for ep0
902 if (!(list_empty(&ep->queue)) &&
903 !((ep_index(ep) == 0) && (req->req.length == 0))) {
904 /* Add td to the end */
905 struct fsl_req *lastreq;
906 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
908 /* only one dtd in dqh */
909 lastreq->tail->next_td_ptr =
910 cpu_to_hc32(req->head->td_dma | DTD_NEXT_TERMINATE);
913 lastreq->tail->next_td_ptr =
914 cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
916 #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
919 /* Read prime bit, if 1 goto done */
920 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
923 /* Set ATDTW bit in USBCMD */
924 temp = fsl_readl(&dr_regs->usbcmd);
925 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
927 /* Read correct status bit */
928 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
930 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
932 /* Write ATDTW bit to 0 */
933 temp = fsl_readl(&dr_regs->usbcmd);
934 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
944 /* Fill in the dTD structure
945 * @req: request that the transfer belongs to
946 * @length: return actually data length of the dTD
947 * @dma: return dma address of the dTD
948 * @is_last: return flag if it is the last dTD of the request
949 * return: pointer to the built dTD */
950 static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
951 dma_addr_t *dma, int *is_last)
954 struct ep_td_struct *dtd;
956 /* how big will this transfer be? */
957 *length = min(req->req.length - req->req.actual,
958 (unsigned)EP_MAX_LENGTH_TRANSFER);
959 if (NEED_IRAM(req->ep))
960 *length = min(*length, g_iram_size);
961 #ifdef CONFIG_FSL_UTP
962 dtd = dma_pool_alloc_nonbufferable(udc_controller->td_pool, GFP_ATOMIC, dma);
964 dtd = dma_pool_alloc(udc_controller->td_pool, GFP_ATOMIC, dma);
970 /* Clear reserved field */
971 swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
972 swap_temp &= ~DTD_RESERVED_FIELDS;
973 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
975 /* Init all of buffer page pointers */
976 swap_temp = (u32) (req->req.dma + req->req.actual);
977 if (NEED_IRAM(req->ep))
978 swap_temp = (u32) (req->req.dma);
979 dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
980 dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
981 dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
982 dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
983 dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
985 req->req.actual += *length;
987 /* zlp is needed if req->req.zero is set */
989 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
993 } else if (req->req.length == req->req.actual)
999 VDBG("multi-dtd request!\n");
1000 /* Fill in the transfer size; set active bit */
1001 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
1003 /* Enable interrupt for the last dtd of a request */
1004 if (*is_last && !req->req.no_interrupt)
1005 swap_temp |= DTD_IOC;
1006 if (NEED_IRAM(req->ep))
1007 swap_temp |= DTD_IOC;
1009 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
1013 VDBG("length = %d address= 0x%x", *length, (int)*dma);
1018 /* Generate dtd chain for a request */
1019 static int fsl_req_to_dtd(struct fsl_req *req)
1024 struct ep_td_struct *last_dtd = NULL, *dtd;
1027 if (NEED_IRAM(req->ep)) {
1028 req->oridma = req->req.dma;
1029 /* here, replace user buffer to iram buffer */
1030 if (ep_is_in(req->ep)) {
1031 req->req.dma = req->ep->udc->iram_buffer[1];
1032 if ((list_empty(&req->ep->queue))) {
1033 /* copy data only when no bulk in transfer is
1035 memcpy((char *)req->ep->udc->iram_buffer_v[1],
1036 req->req.buf, min(req->req.length,
1040 req->req.dma = req->ep->udc->iram_buffer[0];
1044 if (USE_MSC_WR(req->req.length))
1048 dtd = fsl_build_dtd(req, &count, &dma, &is_last);
1056 last_dtd->next_td_ptr = cpu_to_hc32(dma);
1057 last_dtd->next_td_virt = dtd;
1064 dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
1065 req->cur = req->head;
1071 /* queues (submits) an I/O request to an endpoint */
1073 fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
1075 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
1076 struct fsl_req *req = container_of(_req, struct fsl_req, req);
1077 struct fsl_udc *udc;
1078 unsigned long flags;
1082 if (!_ep || !ep->desc) {
1083 VDBG("%s, bad ep\n", __func__);
1088 spin_lock_irqsave(&udc->lock, flags);
1090 /* catch various bogus parameters */
1091 if (!_req || !req->req.buf || (ep_index(ep)
1092 && !list_empty(&req->queue))) {
1093 VDBG("%s, bad params\n", __func__);
1094 spin_unlock_irqrestore(&udc->lock, flags);
1097 if (usb_endpoint_xfer_isoc(ep->desc)) {
1098 if (req->req.length > ep->ep.maxpacket) {
1099 spin_unlock_irqrestore(&udc->lock, flags);
1105 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1106 spin_unlock_irqrestore(&udc->lock, flags);
1111 /* map virtual address to hardware */
1112 if (req->req.dma == DMA_ADDR_INVALID) {
1113 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1115 req->req.length, ep_is_in(ep)
1120 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
1121 req->req.dma, req->req.length,
1128 req->req.status = -EINPROGRESS;
1129 req->req.actual = 0;
1131 if (NEED_IRAM(ep)) {
1133 req->buffer_offset = 0;
1136 /* build dtds and push them to device queue */
1137 if (!fsl_req_to_dtd(req)) {
1138 fsl_queue_td(ep, req);
1140 spin_unlock_irqrestore(&udc->lock, flags);
1144 /* irq handler advances the queue */
1146 list_add_tail(&req->queue, &ep->queue);
1148 spin_unlock_irqrestore(&udc->lock, flags);
1153 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1154 static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1156 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
1157 struct fsl_req *req;
1158 unsigned long flags;
1159 int ep_num, stopped, ret = 0;
1160 struct fsl_udc *udc = NULL;
1166 spin_lock_irqsave(&ep->udc->lock, flags);
1167 stopped = ep->stopped;
1170 /* Stop the ep before we deal with the queue */
1172 ep_num = ep_index(ep);
1173 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
1175 epctrl &= ~EPCTRL_TX_ENABLE;
1177 epctrl &= ~EPCTRL_RX_ENABLE;
1178 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
1180 /* make sure it's actually queued on this endpoint */
1181 list_for_each_entry(req, &ep->queue, queue) {
1182 if (&req->req == _req)
1185 if (&req->req != _req) {
1190 /* The request is in progress, or completed but not dequeued */
1191 if (ep->queue.next == &req->queue) {
1192 _req->status = -ECONNRESET;
1193 fsl_ep_fifo_flush(_ep); /* flush current transfer */
1195 /* The request isn't the last request in this ep queue */
1196 if (req->queue.next != &ep->queue) {
1197 struct ep_queue_head *qh;
1198 struct fsl_req *next_req;
1201 next_req = list_entry(req->queue.next, struct fsl_req,
1204 /* prime with dTD of next request */
1205 update_qh(next_req);
1208 /* The request hasn't been processed, patch up the TD chain */
1210 struct fsl_req *prev_req;
1212 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
1213 prev_req->tail->next_td_ptr = req->tail->next_td_ptr;
1214 #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
1219 done(ep, req, -ECONNRESET);
1222 out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
1224 epctrl |= EPCTRL_TX_ENABLE;
1226 epctrl |= EPCTRL_RX_ENABLE;
1227 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
1228 ep->stopped = stopped;
1230 spin_unlock_irqrestore(&ep->udc->lock, flags);
1234 /*-------------------------------------------------------------------------*/
1236 /*-----------------------------------------------------------------
1237 * modify the endpoint halt feature
1238 * @ep: the non-isochronous endpoint being stalled
1239 * @value: 1--set halt 0--clear halt
1240 * Returns zero, or a negative error code.
1241 *----------------------------------------------------------------*/
1242 static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
1244 struct fsl_ep *ep = NULL;
1245 unsigned long flags = 0;
1246 int status = -EOPNOTSUPP; /* operation not supported */
1247 unsigned char ep_dir = 0, ep_num = 0;
1248 struct fsl_udc *udc = NULL;
1250 ep = container_of(_ep, struct fsl_ep, ep);
1252 if (!_ep || !ep->desc) {
1256 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1257 status = -ESHUTDOWN;
1261 if (usb_endpoint_xfer_isoc(ep->desc)) {
1262 status = -EOPNOTSUPP;
1266 /* Attempt to halt IN ep will fail if any transfer requests
1267 * are still queue */
1268 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1274 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1275 ep_num = (unsigned char)(ep_index(ep));
1276 spin_lock_irqsave(&ep->udc->lock, flags);
1277 dr_ep_change_stall(ep_num, ep_dir, value);
1278 spin_unlock_irqrestore(&ep->udc->lock, flags);
1280 if (ep_index(ep) == 0) {
1284 VDBG(" %s %s halt stat %d", ep->ep.name,
1285 value ? "set" : "clear", status);
1290 static int arcotg_fifo_status(struct usb_ep *_ep)
1293 struct fsl_udc *udc;
1296 struct ep_queue_head *d_qh;
1298 ep = container_of(_ep, struct fsl_ep, ep);
1299 if (!_ep || (!ep->desc && ep_index(ep) != 0))
1302 udc = (struct fsl_udc *)ep->udc;
1304 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1307 d_qh = &ep->udc->ep_qh[ep_index(ep) * 2 + ep_is_in(ep)];
1309 bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
1310 (1 << (ep_index(ep)));
1312 if (fsl_readl(&dr_regs->endptstatus) & bitmask)
1313 size = (d_qh->size_ioc_int_sts & DTD_PACKET_SIZE)
1314 >> DTD_LENGTH_BIT_POS;
1316 pr_debug("%s %u\n", __func__, size);
1320 static void fsl_ep_fifo_flush(struct usb_ep *_ep)
1325 unsigned long timeout;
1326 #define FSL_UDC_FLUSH_TIMEOUT 1000
1331 ep = container_of(_ep, struct fsl_ep, ep);
1335 ep_num = ep_index(ep);
1336 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1339 bits = (1 << 16) | 1;
1340 else if (ep_dir == USB_SEND)
1341 bits = 1 << (16 + ep_num);
1345 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
1347 fsl_writel(bits, &dr_regs->endptflush);
1349 /* Wait until flush complete */
1350 while (fsl_readl(&dr_regs->endptflush)) {
1351 if (time_after(jiffies, timeout)) {
1352 ERR("ep flush timeout\n");
1357 /* See if we need to flush again */
1358 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1361 static struct usb_ep_ops fsl_ep_ops = {
1362 .enable = fsl_ep_enable,
1363 .disable = fsl_ep_disable,
1365 .alloc_request = fsl_alloc_request,
1366 .free_request = fsl_free_request,
1368 .queue = fsl_ep_queue,
1369 .dequeue = fsl_ep_dequeue,
1371 .set_halt = fsl_ep_set_halt,
1372 .fifo_status = arcotg_fifo_status,
1373 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1376 /*-------------------------------------------------------------------------
1377 Gadget Driver Layer Operations
1378 -------------------------------------------------------------------------*/
1380 /*----------------------------------------------------------------------
1381 * Get the current frame number (from DR frame_index Reg )
1382 *----------------------------------------------------------------------*/
1383 static int fsl_get_frame(struct usb_gadget *gadget)
1385 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1388 /*-----------------------------------------------------------------------
1389 * Tries to wake up the host connected to this gadget
1390 -----------------------------------------------------------------------*/
1391 static int fsl_wakeup(struct usb_gadget *gadget)
1393 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1396 /* Remote wakeup feature not enabled by host */
1397 if (!udc->remote_wakeup)
1400 portsc = fsl_readl(&dr_regs->portsc1);
1401 /* not suspended? */
1402 if (!(portsc & PORTSCX_PORT_SUSPEND))
1404 /* trigger force resume */
1405 portsc |= PORTSCX_PORT_FORCE_RESUME;
1406 fsl_writel(portsc, &dr_regs->portsc1);
1410 static int can_pullup(struct fsl_udc *udc)
1412 return udc->driver && udc->softconnect && udc->vbus_active;
1415 /* Notify controller that VBUS is powered, Called by whatever
1416 detects VBUS sessions */
1417 static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1419 struct fsl_udc *udc;
1420 unsigned long flags;
1422 udc = container_of(gadget, struct fsl_udc, gadget);
1423 spin_lock_irqsave(&udc->lock, flags);
1424 VDBG("VBUS %s\n", is_active ? "on" : "off");
1425 udc->vbus_active = (is_active != 0);
1426 if (can_pullup(udc))
1427 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1430 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1432 spin_unlock_irqrestore(&udc->lock, flags);
1436 /* constrain controller's VBUS power usage
1437 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1438 * reporting how much power the device may consume. For example, this
1439 * could affect how quickly batteries are recharged.
1441 * Returns zero on success, else negative errno.
1443 static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1445 struct fsl_udc *udc;
1446 struct fsl_usb2_platform_data *pdata;
1448 udc = container_of(gadget, struct fsl_udc, gadget);
1449 if (udc->transceiver)
1450 return otg_set_power(udc->transceiver, mA);
1452 if (pdata->xcvr_ops && pdata->xcvr_ops->set_vbus_draw) {
1453 pdata->xcvr_ops->set_vbus_draw(pdata->xcvr_ops, pdata, mA);
1459 /* Change Data+ pullup status
1460 * this func is used by usb_gadget_connect/disconnet
1462 static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1464 struct fsl_udc *udc;
1466 udc = container_of(gadget, struct fsl_udc, gadget);
1467 udc->softconnect = (is_on != 0);
1468 if (can_pullup(udc))
1469 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1472 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1479 * The USB PHY/Charger driver can't visit usb_gadget directly, so
1480 * supply a wrapped function for usb charger visiting.
1482 static void usb_charger_pullup_dp(bool enable)
1484 fsl_pullup(&udc_controller->gadget, (int)enable);
1487 /* defined in gadget.h */
1488 static struct usb_gadget_ops fsl_gadget_ops = {
1489 .get_frame = fsl_get_frame,
1490 .wakeup = fsl_wakeup,
1491 /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1492 .vbus_session = fsl_vbus_session,
1493 .vbus_draw = fsl_vbus_draw,
1494 .pullup = fsl_pullup,
1497 /* Set protocol stall on ep0, protocol stall will automatically be cleared
1498 on new transaction */
1499 static void ep0stall(struct fsl_udc *udc)
1503 /* must set tx and rx to stall at the same time */
1504 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1505 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1506 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1510 /* Prime a status phase for ep0 */
1511 static int ep0_prime_status(struct fsl_udc *udc, int direction)
1513 struct fsl_req *req = udc->status_req;
1517 if (direction == EP_DIR_IN)
1518 udc->ep0_dir = USB_DIR_IN;
1520 udc->ep0_dir = USB_DIR_OUT;
1525 req->req.length = 0;
1526 req->req.status = -EINPROGRESS;
1528 status = fsl_ep_queue(&ep->ep, &req->req, GFP_ATOMIC);
1532 static inline int udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1534 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1539 nuke(ep, -ESHUTDOWN);
1547 static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1549 /* Save the new address to device struct */
1550 udc->device_address = (u8) value;
1551 /* Update usb state */
1552 udc->usb_state = USB_STATE_ADDRESS;
1554 if (ep0_prime_status(udc, EP_DIR_IN))
1561 static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1562 u16 index, u16 length)
1564 u16 tmp = 0; /* Status, cpu endian */
1566 struct fsl_req *req;
1572 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1573 /* Get device status */
1574 tmp = 1 << USB_DEVICE_SELF_POWERED;
1575 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1576 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1577 /* Get interface status */
1578 /* We don't have interface information in udc driver */
1580 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1581 /* Get endpoint status */
1582 struct fsl_ep *target_ep;
1584 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1586 /* stall if endpoint doesn't exist */
1587 if (!target_ep->desc)
1589 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1590 << USB_ENDPOINT_HALT;
1593 udc->ep0_dir = USB_DIR_IN;
1594 /* Borrow the per device data_req */
1595 /* status_req had been used to prime status */
1596 req = udc->data_req;
1597 /* Fill in the reqest structure */
1598 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1600 req->req.length = 2;
1602 status = fsl_ep_queue(&ep->ep, &req->req, GFP_ATOMIC);
1604 udc_reset_ep_queue(udc, 0);
1605 ERR("Can't respond to getstatus request \n");
1609 if (ep0_prime_status(udc, EP_DIR_OUT))
1617 static void setup_received_irq(struct fsl_udc *udc,
1618 struct usb_ctrlrequest *setup)
1620 u16 wValue = le16_to_cpu(setup->wValue);
1621 u16 wIndex = le16_to_cpu(setup->wIndex);
1622 u16 wLength = le16_to_cpu(setup->wLength);
1623 struct usb_gadget *gadget = &(udc->gadget);
1625 udc_reset_ep_queue(udc, 0);
1627 /* We process some stardard setup requests here */
1628 switch (setup->bRequest) {
1629 case USB_REQ_GET_STATUS:
1630 /* Data+Status phase from udc */
1631 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1632 != (USB_DIR_IN | USB_TYPE_STANDARD))
1634 spin_unlock(&udc->lock);
1635 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1636 spin_lock(&udc->lock);
1639 case USB_REQ_SET_ADDRESS:
1640 /* Status phase from udc */
1641 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1642 | USB_RECIP_DEVICE))
1644 spin_unlock(&udc->lock);
1645 ch9setaddress(udc, wValue, wIndex, wLength);
1646 spin_lock(&udc->lock);
1648 case USB_REQ_SET_CONFIGURATION:
1649 spin_unlock(&udc->lock);
1650 fsl_vbus_draw(gadget, mA);
1651 spin_lock(&udc->lock);
1653 case USB_REQ_CLEAR_FEATURE:
1654 case USB_REQ_SET_FEATURE:
1655 /* Status phase from udc */
1657 int rc = -EOPNOTSUPP;
1660 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1661 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
1662 int pipe = get_pipe_by_windex(wIndex);
1665 if (wValue != 0 || wLength != 0 || pipe >= (udc->max_ep / 2))
1667 ep = get_ep_by_pipe(udc, pipe);
1669 spin_unlock(&udc->lock);
1670 rc = fsl_ep_set_halt(&ep->ep,
1671 (setup->bRequest == USB_REQ_SET_FEATURE)
1673 spin_lock(&udc->lock);
1675 } else if ((setup->bRequestType & (USB_RECIP_MASK
1676 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
1677 | USB_TYPE_STANDARD)) {
1678 /* Note: The driver has not include OTG support yet.
1679 * This will be set when OTG support is added */
1680 if (setup->wValue == USB_DEVICE_TEST_MODE)
1681 ptc = setup->wIndex >> 8;
1682 else if (setup->wValue == USB_DEVICE_REMOTE_WAKEUP) {
1683 if (setup->bRequest == USB_REQ_SET_FEATURE)
1684 udc->remote_wakeup = 1;
1686 udc->remote_wakeup = 0;
1688 else if (gadget_is_otg(&udc->gadget)) {
1689 if (setup->bRequest ==
1690 USB_DEVICE_B_HNP_ENABLE)
1691 udc->gadget.b_hnp_enable = 1;
1692 else if (setup->bRequest ==
1693 USB_DEVICE_A_HNP_SUPPORT)
1694 udc->gadget.a_hnp_support = 1;
1695 else if (setup->bRequest ==
1696 USB_DEVICE_A_ALT_HNP_SUPPORT)
1697 udc->gadget.a_alt_hnp_support = 1;
1704 spin_unlock(&udc->lock);
1705 if (ep0_prime_status(udc, EP_DIR_IN))
1707 spin_lock(&udc->lock);
1713 fsl_platform_set_test_mode(udc->pdata, ptc);
1714 tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
1715 fsl_writel(tmp, &dr_regs->portsc1);
1716 printk(KERN_INFO "udc: switch to test mode 0x%x.\n", ptc);
1726 /* Requests handled by gadget */
1728 /* Data phase from gadget, status phase from udc */
1729 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1730 ? USB_DIR_IN : USB_DIR_OUT;
1731 spin_unlock(&udc->lock);
1732 if (udc->driver->setup(&udc->gadget,
1733 &udc->local_setup_buff) < 0) {
1734 /* cancel all requests on ep0 */
1735 udc_reset_ep_queue(udc, 0);
1738 /* prime the status phase */
1739 int dir = EP_DIR_IN;
1740 if (setup->bRequestType & USB_DIR_IN)
1742 if (ep0_prime_status(udc, dir))
1746 /* No data phase, IN status from gadget */
1747 udc->ep0_dir = USB_DIR_IN;
1748 spin_unlock(&udc->lock);
1749 if (udc->driver->setup(&udc->gadget,
1750 &udc->local_setup_buff) < 0)
1753 spin_lock(&udc->lock);
1756 /* Process request for Data or Status phase of ep0
1757 * prime status phase if needed */
1758 static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1759 struct fsl_req *req)
1761 if (udc->usb_state == USB_STATE_ADDRESS) {
1762 /* Set the new address */
1763 u32 new_address = (u32) udc->device_address;
1764 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1765 &dr_regs->deviceaddr);
1771 /* Tripwire mechanism to ensure a setup packet payload is extracted without
1772 * being corrupted by another incoming setup packet */
1773 static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1776 struct ep_queue_head *qh;
1777 struct fsl_usb2_platform_data *pdata = udc->pdata;
1779 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1781 /* Clear bit in ENDPTSETUPSTAT */
1782 temp = fsl_readl(&dr_regs->endptsetupstat);
1783 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1785 /* while a hazard exists when setup package arrives */
1787 /* Set Setup Tripwire */
1788 temp = fsl_readl(&dr_regs->usbcmd);
1789 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1791 /* Copy the setup packet to local buffer */
1792 if (pdata->le_setup_buf) {
1793 u32 *p = (u32 *)buffer_ptr;
1794 u32 *s = (u32 *)qh->setup_buffer;
1796 /* Convert little endian setup buffer to CPU endian */
1797 *p++ = le32_to_cpu(*s++);
1798 *p = le32_to_cpu(*s);
1800 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1802 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1804 /* Clear Setup Tripwire */
1805 temp = fsl_readl(&dr_regs->usbcmd);
1806 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1809 static void iram_process_ep_complete(struct fsl_req *curr_req,
1814 int in = ep_is_in(curr_req->ep);
1817 buf = (char *)udc_controller->iram_buffer_v[1];
1819 buf = (char *)udc_controller->iram_buffer_v[0];
1821 if (curr_req->cur->next_td_ptr == cpu_to_hc32(DTD_NEXT_TERMINATE)
1822 || (cur_transfer < g_iram_size)
1823 || (curr_req->req.length == curr_req->req.actual))
1824 curr_req->last_one = 1;
1826 if (curr_req->last_one) {
1827 /* the last transfer */
1829 memcpy(curr_req->req.buf + curr_req->buffer_offset, buf,
1832 if (curr_req->tail->next_td_ptr !=
1833 cpu_to_hc32(DTD_NEXT_TERMINATE)) {
1834 /* have next request,queue it */
1835 struct fsl_req *next_req;
1837 list_entry(curr_req->queue.next,
1838 struct fsl_req, queue);
1840 memcpy(buf, next_req->req.buf,
1841 min(g_iram_size, next_req->req.length));
1842 update_qh(next_req);
1844 curr_req->req.dma = curr_req->oridma;
1846 /* queue next dtd */
1847 /* because had next dtd, so should finish */
1848 /* tranferring g_iram_size data */
1849 curr_req->buffer_offset += g_iram_size;
1850 /* pervious set stop bit,now clear it */
1851 curr_req->cur->next_td_ptr &= ~cpu_to_hc32(DTD_NEXT_TERMINATE);
1852 curr_req->cur = curr_req->cur->next_td_virt;
1855 min(curr_req->req.length - curr_req->buffer_offset,
1857 memcpy(buf, curr_req->req.buf + curr_req->buffer_offset,
1860 memcpy(curr_req->req.buf + curr_req->buffer_offset -
1861 g_iram_size, buf, g_iram_size);
1863 update_qh(curr_req);
1867 /* process-ep_req(): free the completed Tds for this req */
1868 static int process_ep_req(struct fsl_udc *udc, int pipe,
1869 struct fsl_req *curr_req)
1871 struct ep_td_struct *curr_td;
1872 int td_complete, actual, remaining_length, j, tmp;
1875 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1876 int direction = pipe % 2;
1877 int total = 0, real_len;
1879 curr_td = curr_req->head;
1881 actual = curr_req->req.length;
1882 real_len = curr_req->req.length;
1884 for (j = 0; j < curr_req->dtd_count; j++) {
1885 remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
1887 >> DTD_LENGTH_BIT_POS;
1888 if (NEED_IRAM(curr_req->ep)) {
1889 if (real_len >= g_iram_size) {
1890 actual = g_iram_size;
1891 real_len -= g_iram_size;
1892 } else { /* the last packet */
1894 curr_req->last_one = 1;
1897 actual -= remaining_length;
1900 errors = hc32_to_cpu(curr_td->size_ioc_sts) & DTD_ERROR_MASK;
1902 if (errors & DTD_STATUS_HALTED) {
1903 ERR("dTD error %08x QH=%d\n", errors, pipe);
1904 /* Clear the errors and Halt condition */
1905 tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
1907 curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
1909 /* FIXME: continue with next queued TD? */
1913 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1914 VDBG("Transfer overflow");
1917 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1922 ERR("Unknown error has occured (0x%x)!\r\n",
1925 } else if (hc32_to_cpu(curr_td->size_ioc_sts)
1926 & DTD_STATUS_ACTIVE) {
1927 VDBG("Request not complete");
1928 status = REQ_UNCOMPLETE;
1930 } else if (remaining_length) {
1932 VDBG("Transmit dTD remaining length not zero");
1941 VDBG("dTD transmitted successful ");
1943 if (NEED_IRAM(curr_req->ep))
1945 next_td_ptr & cpu_to_hc32(DTD_NEXT_TERMINATE))
1947 if (j != curr_req->dtd_count - 1)
1948 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1953 curr_req->req.actual = total;
1954 if (NEED_IRAM(curr_req->ep))
1955 iram_process_ep_complete(curr_req, actual);
1959 /* Process a DTD completion interrupt */
1960 static void dtd_complete_irq(struct fsl_udc *udc)
1963 int i, ep_num, direction, bit_mask, status;
1964 struct fsl_ep *curr_ep;
1965 struct fsl_req *curr_req, *temp_req;
1967 /* Clear the bits in the register */
1968 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1969 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1974 for (i = 0; i < udc->max_ep; i++) {
1978 bit_mask = 1 << (ep_num + 16 * direction);
1980 if (!(bit_pos & bit_mask))
1983 curr_ep = get_ep_by_pipe(udc, i);
1985 /* If the ep is configured */
1986 if (curr_ep->name == NULL) {
1987 INFO("Invalid EP?");
1991 /* process the req queue until an uncomplete request */
1992 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1994 status = process_ep_req(udc, i, curr_req);
1996 VDBG("status of process_ep_req= %d, ep = %d",
1998 if (status == REQ_UNCOMPLETE)
2000 /* write back status to req */
2001 curr_req->req.status = status;
2004 ep0_req_complete(udc, curr_ep, curr_req);
2007 if (NEED_IRAM(curr_ep)) {
2008 if (curr_req->last_one)
2009 done(curr_ep, curr_req, status);
2010 /* only check the 1th req */
2013 done(curr_ep, curr_req, status);
2016 dump_ep_queue(curr_ep);
2020 static void fsl_udc_speed_update(struct fsl_udc *udc)
2025 /* Wait for port reset finished */
2026 while ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)
2030 speed = (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SPEED_MASK);
2032 case PORTSCX_PORT_SPEED_HIGH:
2033 udc->gadget.speed = USB_SPEED_HIGH;
2035 case PORTSCX_PORT_SPEED_FULL:
2036 udc->gadget.speed = USB_SPEED_FULL;
2038 case PORTSCX_PORT_SPEED_LOW:
2039 udc->gadget.speed = USB_SPEED_LOW;
2042 udc->gadget.speed = USB_SPEED_UNKNOWN;
2047 /* Process a port change interrupt */
2048 static void port_change_irq(struct fsl_udc *udc)
2053 /* Update port speed */
2054 fsl_udc_speed_update(udc);
2056 /* Update USB state */
2057 if (!udc->resume_state)
2058 udc->usb_state = USB_STATE_DEFAULT;
2061 /* Process suspend interrupt */
2062 static void suspend_irq(struct fsl_udc *udc)
2064 pr_debug("%s begins\n", __func__);
2066 udc->resume_state = udc->usb_state;
2067 udc->usb_state = USB_STATE_SUSPENDED;
2069 /* report suspend to the driver, serial.c does not support this */
2070 if (udc->driver->suspend)
2071 udc->driver->suspend(&udc->gadget);
2073 pr_debug("%s ends\n", __func__);
2076 static void bus_resume(struct fsl_udc *udc)
2078 udc->usb_state = udc->resume_state;
2079 udc->resume_state = 0;
2081 /* report resume to the driver, serial.c does not support this */
2082 if (udc->driver->resume)
2083 udc->driver->resume(&udc->gadget);
2086 /* Clear up all ep queues */
2087 static int reset_queues(struct fsl_udc *udc)
2091 for (pipe = 0; pipe < udc->max_pipes; pipe++)
2092 udc_reset_ep_queue(udc, pipe);
2094 spin_unlock(&udc->lock);
2095 /* report disconnect; the driver is already quiesced */
2096 udc->driver->disconnect(&udc->gadget);
2097 spin_lock(&udc->lock);
2102 /* Process reset interrupt */
2103 static void reset_irq(struct fsl_udc *udc)
2107 /* Clear the device address */
2108 temp = fsl_readl(&dr_regs->deviceaddr);
2109 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
2111 udc->device_address = 0;
2113 /* Clear usb state */
2114 udc->resume_state = 0;
2116 udc->remote_wakeup = 0; /* default to 0 on reset */
2117 udc->gadget.b_hnp_enable = 0;
2118 udc->gadget.a_hnp_support = 0;
2119 udc->gadget.a_alt_hnp_support = 0;
2121 /* Clear all the setup token semaphores */
2122 temp = fsl_readl(&dr_regs->endptsetupstat);
2123 fsl_writel(temp, &dr_regs->endptsetupstat);
2125 /* Clear all the endpoint complete status bits */
2126 temp = fsl_readl(&dr_regs->endptcomplete);
2127 fsl_writel(temp, &dr_regs->endptcomplete);
2129 /* Write 1s to the flush register */
2130 fsl_writel(0xffffffff, &dr_regs->endptflush);
2132 /* Bus is reseting */
2134 /* Reset all the queues, include XD, dTD, EP queue
2135 * head and TR Queue */
2137 udc->usb_state = USB_STATE_DEFAULT;
2140 #define FSL_DP_CHANGE_TIMEOUT (msecs_to_jiffies(1000)) /* 1000 ms */
2141 static void gadget_wait_line_to_se0(void)
2143 unsigned long timeout;
2144 timeout = jiffies + FSL_DP_CHANGE_TIMEOUT;
2145 /* Wait for DP to SE0 */
2146 while (!((fsl_readl(&dr_regs->portsc1) &
2147 (u32)((1 << 10) | (1 << 11))) == PORTSCX_LINE_STATUS_SE0)) {
2148 if (time_after(jiffies, timeout)) {
2149 pr_warning(KERN_ERR "wait dp to SE0 timeout, please check"
2150 " your hardware design!\n");
2157 #define FSL_WAIT_CLASS_DRIVER_TIMEOUT (msecs_to_jiffies(3000)) /* 3s */
2158 static void gadget_wait_class_driver_finish(void)
2160 unsigned long timeout;
2161 struct fsl_udc *udc = udc_controller;
2164 timeout = jiffies + FSL_WAIT_CLASS_DRIVER_TIMEOUT;
2165 /* for non-control endpoints */
2166 while (i < (int)(udc_controller->max_ep)) {
2167 ep = &udc->eps[i++];
2168 if (ep->stopped == 0) {
2169 if (time_after(timeout, jiffies)) {
2174 pr_warning(KERN_WARNING "We have waited 3s, but the class driver"
2175 " has still not finishes!\n");
2182 static void fsl_gadget_disconnect_event(struct work_struct *work)
2184 struct fsl_udc *udc = udc_controller;
2185 unsigned long flags;
2186 struct fsl_usb2_platform_data *pdata;
2191 /* notify vbus is disconnected */
2192 imx_usb_vbus_disconnect(&udc->charger);
2193 /* wait line to se0 */
2194 dr_discharge_line(pdata, true);
2196 * Wait class drivers finish, an well-behaviour class driver should
2197 * call ep_disable when it is notified to be disconnected.
2199 gadget_wait_class_driver_finish();
2201 spin_lock_irqsave(&udc->lock, flags);
2203 /* here we need to enable the B_SESSION_IRQ
2204 * to enable the following device attach
2206 tmp = fsl_readl(&dr_regs->otgsc);
2207 if (!(tmp & (OTGSC_B_SESSION_VALID_IRQ_EN)))
2208 fsl_writel(tmp | (OTGSC_B_SESSION_VALID_IRQ_EN),
2211 spin_unlock_irqrestore(&udc->lock, flags);
2212 /* enable wake up */
2213 dr_wake_up_enable(udc, true);
2214 /* close USB PHY clock */
2215 dr_phy_low_power_mode(udc, true);
2216 /* close dr controller clock */
2218 printk(KERN_DEBUG "%s: udc enter low power mode\n", __func__);
2221 /* if wakup udc, return true; else return false*/
2222 bool try_wake_up_udc(struct fsl_udc *udc)
2224 struct fsl_usb2_platform_data *pdata;
2229 /* check if Vbus change irq */
2230 irq_src = fsl_readl(&dr_regs->otgsc) & (~OTGSC_ID_CHANGE_IRQ_STS);
2231 if (irq_src & OTGSC_B_SESSION_VALID_IRQ_STS) {
2233 /* Only handle device interrupt event
2234 * For mx53 loco board, the debug ID value is 0 and
2235 * DO NOT support OTG function
2237 if (!machine_is_mx53_loco())
2238 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID))
2241 fsl_writel(irq_src, &dr_regs->otgsc);
2243 tmp = fsl_readl(&dr_regs->usbcmd);
2244 /* check BSV bit to see if fall or rise */
2245 if (irq_src & OTGSC_B_SESSION_VALID) {
2246 if (udc->suspended) /*let the system pm resume the udc */
2248 udc->vbus_active = true;
2250 /* disable pulldown dp and dm */
2251 dr_discharge_line(pdata, false);
2252 /* notify vbus is connected */
2253 imx_usb_vbus_connect(&udc->charger);
2254 printk(KERN_DEBUG "%s: udc out low power mode\n", __func__);
2256 udc->vbus_active = false;
2257 fsl_pullup(&udc_controller->gadget, false); /* usbcmd.rs=0 */
2258 /* here we need disable B_SESSION_IRQ, after
2259 * schedule_work finished, it need to be enabled again.
2260 * Doing like this can avoid conflicting between rapid
2263 tmp = fsl_readl(&dr_regs->otgsc);
2264 if (tmp & (OTGSC_B_SESSION_VALID_IRQ_EN))
2266 (~OTGSC_B_SESSION_VALID_IRQ_EN),
2268 /* update port status */
2269 fsl_udc_speed_update(udc);
2270 spin_unlock(&udc->lock);
2272 udc->driver->disconnect(&udc->gadget);
2273 spin_lock(&udc->lock);
2274 schedule_work(&udc->gadget_disconnect_schedule);
2282 * USB device controller interrupt handler
2284 static irqreturn_t fsl_udc_irq(int irq, void *_udc)
2286 struct fsl_udc *udc = _udc;
2288 irqreturn_t status = IRQ_NONE;
2289 unsigned long flags;
2290 struct fsl_usb2_platform_data *pdata = udc->pdata;
2292 if (pdata->irq_delay)
2295 spin_lock_irqsave(&udc->lock, flags);
2297 #ifdef CONFIG_USB_OTG
2298 /* if no gadget register in this driver, we need do noting */
2299 if (udc->transceiver->gadget == NULL) {
2302 /* only handle device interrupt event */
2303 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
2307 if (try_wake_up_udc(udc) == false) {
2310 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
2311 /* Clear notification bits */
2312 fsl_writel(irq_src, &dr_regs->usbsts);
2314 /* only handle enabled interrupt */
2318 VDBG("0x%x\n", irq_src);
2320 /* Need to resume? */
2321 if (udc->usb_state == USB_STATE_SUSPENDED)
2322 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
2326 if (irq_src & USB_STS_INT) {
2328 /* Setup package, we only support ep0 as control ep */
2329 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
2330 tripwire_handler(udc, 0,
2331 (u8 *) (&udc->local_setup_buff));
2332 setup_received_irq(udc, &udc->local_setup_buff);
2333 status = IRQ_HANDLED;
2336 /* completion of dtd */
2337 if (fsl_readl(&dr_regs->endptcomplete)) {
2338 dtd_complete_irq(udc);
2339 status = IRQ_HANDLED;
2343 /* SOF (for ISO transfer) */
2344 if (irq_src & USB_STS_SOF) {
2345 status = IRQ_HANDLED;
2349 if (irq_src & USB_STS_PORT_CHANGE) {
2350 port_change_irq(udc);
2351 status = IRQ_HANDLED;
2354 /* Reset Received */
2355 if (irq_src & USB_STS_RESET) {
2358 status = IRQ_HANDLED;
2361 /* Sleep Enable (Suspend) */
2362 if (irq_src & USB_STS_SUSPEND) {
2363 VDBG("suspend int");
2364 if (!(udc->usb_state == USB_STATE_SUSPENDED))
2366 status = IRQ_HANDLED;
2369 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
2370 VDBG("Error IRQ %x ", irq_src);
2374 spin_unlock_irqrestore(&udc->lock, flags);
2378 /*----------------------------------------------------------------*
2379 * Hook to gadget drivers
2380 * Called by initialization code of gadget drivers
2381 *----------------------------------------------------------------*/
2382 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2383 int (*bind)(struct usb_gadget *))
2385 int retval = -ENODEV;
2386 unsigned long flags = 0;
2388 if (!udc_controller)
2391 if (!bind || !driver || (driver->speed != USB_SPEED_FULL
2392 && driver->speed != USB_SPEED_HIGH)
2393 || !driver->disconnect
2397 if (udc_controller->driver)
2400 /* lock is needed but whether should use this lock or another */
2401 spin_lock_irqsave(&udc_controller->lock, flags);
2403 driver->driver.bus = 0;
2404 udc_controller->pdata->port_enables = 1;
2405 /* hook up the driver */
2406 udc_controller->driver = driver;
2407 udc_controller->gadget.dev.driver = &driver->driver;
2408 spin_unlock_irqrestore(&udc_controller->lock, flags);
2410 /* It doesn't need to switch usb from low power mode to normal mode
2413 if (!udc_controller->transceiver)
2414 dr_phy_low_power_mode(udc_controller, false);
2416 /* bind udc driver to gadget driver */
2417 retval = bind(&udc_controller->gadget);
2419 VDBG("bind to %s --> %d", driver->driver.name, retval);
2420 udc_controller->gadget.dev.driver = 0;
2421 udc_controller->driver = 0;
2426 if (udc_controller->transceiver) {
2427 printk(KERN_INFO "Suspend udc for OTG auto detect\n");
2428 udc_controller->suspended = 1;
2429 dr_wake_up_enable(udc_controller, true);
2431 /* export udc suspend/resume call to OTG */
2432 udc_controller->gadget.dev.driver->suspend = (dev_sus)fsl_udc_suspend;
2433 udc_controller->gadget.dev.driver->resume = (dev_res)fsl_udc_resume;
2435 /* connect to bus through transceiver */
2436 retval = otg_set_peripheral(udc_controller->transceiver,
2437 &udc_controller->gadget);
2439 ERR("can't bind to transceiver\n");
2440 driver->unbind(&udc_controller->gadget);
2441 udc_controller->gadget.dev.driver = 0;
2442 udc_controller->driver = 0;
2446 /* Enable DR IRQ reg and Set usbcmd reg Run bit */
2447 dr_controller_run(udc_controller);
2448 if (udc_controller->stopped)
2451 printk(KERN_INFO "%s: bind to driver %s \n",
2452 udc_controller->gadget.name, driver->driver.name);
2456 printk(KERN_DEBUG "retval %d \n", retval);
2457 udc_controller->pdata->port_enables = 0;
2461 EXPORT_SYMBOL(usb_gadget_probe_driver);
2463 /* Disconnect from gadget driver */
2464 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2466 struct fsl_ep *loop_ep;
2467 unsigned long flags;
2469 if (!udc_controller)
2472 if (!driver || driver != udc_controller->driver || !driver->unbind)
2475 if (udc_controller->stopped)
2478 if (udc_controller->transceiver)
2479 (void)otg_set_peripheral(udc_controller->transceiver, 0);
2481 /* stop DR, disable intr */
2482 dr_controller_stop(udc_controller);
2484 udc_controller->pdata->port_enables = 0;
2485 /* in fact, no needed */
2486 udc_controller->usb_state = USB_STATE_ATTACHED;
2487 udc_controller->ep0_dir = 0;
2489 /* stand operation */
2490 spin_lock_irqsave(&udc_controller->lock, flags);
2491 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2492 nuke(&udc_controller->eps[0], -ESHUTDOWN);
2493 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
2495 nuke(loop_ep, -ESHUTDOWN);
2496 spin_unlock_irqrestore(&udc_controller->lock, flags);
2498 /* disconnect gadget before unbinding */
2499 driver->disconnect(&udc_controller->gadget);
2501 /* unbind gadget and unhook driver. */
2502 driver->unbind(&udc_controller->gadget);
2503 udc_controller->gadget.dev.driver = 0;
2504 udc_controller->driver = 0;
2506 if (udc_controller->gadget.is_otg) {
2507 dr_wake_up_enable(udc_controller, true);
2510 dr_phy_low_power_mode(udc_controller, true);
2512 printk(KERN_INFO "unregistered gadget driver '%s'\r\n",
2513 driver->driver.name);
2516 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2518 /*-------------------------------------------------------------------------
2519 PROC File System Support
2520 -------------------------------------------------------------------------*/
2521 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2523 #include <linux/seq_file.h>
2525 static const char proc_filename[] = "driver/fsl_usb2_udc";
2527 static int fsl_proc_read(char *page, char **start, off_t off, int count,
2528 int *eof, void *_dev)
2532 unsigned size = count;
2533 unsigned long flags;
2536 struct fsl_ep *ep = NULL;
2537 struct fsl_req *req;
2538 struct fsl_usb2_platform_data *pdata;
2540 struct fsl_udc *udc = udc_controller;
2546 spin_lock_irqsave(&udc->lock, flags);
2548 /* ------basic driver infomation ---- */
2549 t = scnprintf(next, size,
2552 "Gadget driver: %s\n\n",
2553 driver_name, DRIVER_VERSION,
2554 udc->driver ? udc->driver->driver.name : "(none)");
2558 /* ------ DR Registers ----- */
2559 tmp_reg = fsl_readl(&dr_regs->usbcmd);
2560 t = scnprintf(next, size,
2564 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
2565 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
2569 tmp_reg = fsl_readl(&dr_regs->usbsts);
2570 t = scnprintf(next, size,
2572 "Dr Suspend: %d" "Reset Received: %d" "System Error: %s"
2573 "USB Error Interrupt: %s\n\n",
2574 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
2575 (tmp_reg & USB_STS_RESET) ? 1 : 0,
2576 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
2577 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
2581 tmp_reg = fsl_readl(&dr_regs->usbintr);
2582 t = scnprintf(next, size,
2583 "USB Intrrupt Enable Reg:\n"
2584 "Sleep Enable: %d" "SOF Received Enable: %d"
2585 "Reset Enable: %d\n"
2586 "System Error Enable: %d"
2587 "Port Change Dectected Enable: %d\n"
2588 "USB Error Intr Enable: %d" "USB Intr Enable: %d\n\n",
2589 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
2590 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
2591 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
2592 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
2593 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
2594 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
2595 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
2599 tmp_reg = fsl_readl(&dr_regs->frindex);
2600 t = scnprintf(next, size,
2601 "USB Frame Index Reg:" "Frame Number is 0x%x\n\n",
2602 (tmp_reg & USB_FRINDEX_MASKS));
2606 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
2607 t = scnprintf(next, size,
2608 "USB Device Address Reg:" "Device Addr is 0x%x\n\n",
2609 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
2613 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
2614 t = scnprintf(next, size,
2615 "USB Endpoint List Address Reg:"
2616 "Device Addr is 0x%x\n\n",
2617 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
2621 tmp_reg = fsl_readl(&dr_regs->portsc1);
2622 t = scnprintf(next, size,
2623 "USB Port Status&Control Reg:\n"
2624 "Port Transceiver Type : %s" "Port Speed: %s \n"
2625 "PHY Low Power Suspend: %s" "Port Reset: %s"
2626 "Port Suspend Mode: %s \n" "Over-current Change: %s"
2627 "Port Enable/Disable Change: %s\n"
2628 "Port Enabled/Disabled: %s"
2629 "Current Connect Status: %s\n\n", ({
2631 switch (tmp_reg & PORTSCX_PTS_FSLS) {
2632 case PORTSCX_PTS_UTMI:
2634 case PORTSCX_PTS_ULPI:
2636 case PORTSCX_PTS_FSLS:
2637 s = "FS/LS Serial"; break;
2643 switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
2644 case PORTSCX_PORT_SPEED_FULL:
2645 s = "Full Speed"; break;
2646 case PORTSCX_PORT_SPEED_LOW:
2647 s = "Low Speed"; break;
2648 case PORTSCX_PORT_SPEED_HIGH:
2649 s = "High Speed"; break;
2651 s = "Undefined"; break;
2655 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
2656 "Normal PHY mode" : "Low power mode",
2657 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
2659 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
2660 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
2662 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
2664 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
2666 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
2667 "Attached" : "Not-Att");
2671 tmp_reg = fsl_readl(&dr_regs->usbmode);
2672 t = scnprintf(next, size,
2673 "USB Mode Reg:" "Controller Mode is : %s\n\n", ({
2675 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2676 case USB_MODE_CTRL_MODE_IDLE:
2678 case USB_MODE_CTRL_MODE_DEVICE:
2679 s = "Device Controller"; break;
2680 case USB_MODE_CTRL_MODE_HOST:
2681 s = "Host Controller"; break;
2690 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2691 t = scnprintf(next, size,
2692 "Endpoint Setup Status Reg:" "SETUP on ep 0x%x\n\n",
2693 (tmp_reg & EP_SETUP_STATUS_MASK));
2697 for (i = 0; i < udc->max_ep / 2; i++) {
2698 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2699 t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
2704 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2705 t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n", tmp_reg);
2709 if (pdata->have_sysif_regs) {
2710 tmp_reg = usb_sys_regs->snoop1;
2711 t = scnprintf(next, size, "\nSnoop1 Reg = [0x%x]\n\n", tmp_reg);
2715 tmp_reg = usb_sys_regs->control;
2716 t = scnprintf(next, size, "General Control Reg = [0x%x]\n\n",
2722 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2724 t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
2725 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2729 if (list_empty(&ep->queue)) {
2730 t = scnprintf(next, size, "its req queue is empty\n\n");
2734 list_for_each_entry(req, &ep->queue, queue) {
2735 t = scnprintf(next, size,
2736 "req %p actual 0x%x length 0x%x buf %p\n",
2737 &req->req, req->req.actual,
2738 req->req.length, req->req.buf);
2743 /* other gadget->eplist ep */
2744 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2746 t = scnprintf(next, size,
2747 "\nFor %s Maxpkt is 0x%x "
2749 ep->ep.name, ep_maxpacket(ep),
2754 if (list_empty(&ep->queue)) {
2755 t = scnprintf(next, size,
2756 "its req queue is empty\n\n");
2760 list_for_each_entry(req, &ep->queue, queue) {
2761 t = scnprintf(next, size,
2762 "req %p actual 0x%x length"
2764 &req->req, req->req.actual,
2765 req->req.length, req->req.buf);
2768 } /* end for each_entry of ep req */
2769 } /* end for else */
2770 } /* end for if(ep->queue) */
2771 } /* end (ep->desc) */
2773 spin_unlock_irqrestore(&udc->lock, flags);
2777 return count - size;
2780 #define create_proc_file() create_proc_read_entry(proc_filename, \
2781 0, NULL, fsl_proc_read, NULL)
2783 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2785 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2787 #define create_proc_file() do {} while (0)
2788 #define remove_proc_file() do {} while (0)
2790 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2792 /* sys entries for device control */
2793 static ssize_t fsl_udc_remote_wakeup_show(struct device *dev,
2794 struct device_attribute *attr, char *buf)
2796 return sprintf(buf, "%d\n", udc_controller->remote_wakeup);
2799 static ssize_t fsl_udc_remote_wakeup_store(struct device *dev, struct device_attribute *attr,
2800 const char *buf, size_t count)
2803 unsigned long flags;
2804 struct fsl_udc *udc = udc_controller;
2806 spin_lock_irqsave(&udc->lock, flags);
2807 if (udc_controller->stopped) {
2808 pr_warning(KERN_ERR "Controller has already stopped, Quit.\n"
2809 "Please make sure usb is online, and controller"
2810 " is not stopped.\n");
2811 spin_unlock_irqrestore(&udc->lock, flags);
2815 if (sscanf(buf, "%d", &value) != 1) {
2816 printk(KERN_ERR "Only numeric 0/1 is valid\n");
2817 spin_unlock_irqrestore(&udc->lock, flags);
2820 if (value == 0 || value == 1)
2821 udc_controller->remote_wakeup = value;
2823 printk(KERN_ERR "Only numeric 0/1 is valid\n");
2824 spin_unlock_irqrestore(&udc->lock, flags);
2828 spin_unlock_irqrestore(&udc->lock, flags);
2833 static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUGO, fsl_udc_remote_wakeup_show,
2834 fsl_udc_remote_wakeup_store);
2836 static ssize_t fsl_udc_start_remote_wakeup_store(struct device *dev, struct device_attribute *attr,
2837 const char *buf, size_t count)
2840 unsigned long flags;
2841 struct fsl_udc *udc = udc_controller;
2843 spin_lock_irqsave(&udc->lock, flags);
2844 if (udc_controller->stopped) {
2845 pr_warning(KERN_ERR "Controller has already stopped, Quit.\n"
2846 "Please make sure usb is online, and controller"
2847 " is not stopped.\n");
2848 spin_unlock_irqrestore(&udc->lock, flags);
2852 if (sscanf(buf, "%d", &value) != 1) {
2853 printk(KERN_ERR "Only numeric 1 is valid\n");
2854 spin_unlock_irqrestore(&udc->lock, flags);
2859 printk(KERN_ERR "Only numeric 1 is valid\n");
2860 spin_unlock_irqrestore(&udc->lock, flags);
2864 if (udc_controller->remote_wakeup == 1) {
2866 printk(KERN_INFO "Send Resume signal\n");
2867 temp = fsl_readl(&dr_regs->portsc1);
2868 temp |= PORTSCX_PORT_FORCE_RESUME;
2869 fsl_writel(temp, &dr_regs->portsc1);
2871 printk(KERN_WARNING "udc remote wakeup has not enabled\n");
2873 spin_unlock_irqrestore(&udc->lock, flags);
2878 static DEVICE_ATTR(start_remote_wakeup, S_IWUGO, NULL,
2879 fsl_udc_start_remote_wakeup_store);
2881 static struct attribute *fsl_udc_attrs[] = {
2882 &dev_attr_remote_wakeup.attr,
2883 &dev_attr_start_remote_wakeup.attr,
2887 static const struct attribute_group fsl_udc_attr_group = {
2888 .attrs = fsl_udc_attrs,
2891 static const struct attribute_group *fsl_udc_attr_groups[] = {
2892 &fsl_udc_attr_group,
2895 /*-------------------------------------------------------------------------*/
2897 /* Release udc structures */
2898 static void fsl_udc_release(struct device *dev)
2900 complete(udc_controller->done);
2901 dma_free_coherent(dev, udc_controller->ep_qh_size,
2902 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2903 kfree(udc_controller);
2906 /******************************************************************
2907 Internal structure setup functions
2908 *******************************************************************/
2909 /*------------------------------------------------------------------
2910 * init resource for globle controller
2911 * Return the udc handle on success or NULL on failure
2912 ------------------------------------------------------------------*/
2913 static int __init struct_udc_setup(struct fsl_udc *udc,
2914 struct platform_device *pdev)
2916 struct fsl_usb2_platform_data *pdata;
2919 pdata = pdev->dev.platform_data;
2920 udc->phy_mode = pdata->phy_mode;
2922 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2924 ERR("malloc fsl_ep failed\n");
2928 /* initialized QHs, take care of alignment */
2929 size = udc->max_ep * sizeof(struct ep_queue_head);
2930 if (size < QH_ALIGNMENT)
2931 size = QH_ALIGNMENT;
2932 else if ((size % QH_ALIGNMENT) != 0) {
2933 size += QH_ALIGNMENT + 1;
2934 size &= ~(QH_ALIGNMENT - 1);
2936 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2937 &udc->ep_qh_dma, GFP_KERNEL);
2939 ERR("malloc QHs for udc failed\n");
2944 udc->ep_qh_size = size;
2946 /* Initialize ep0 status request structure */
2947 /* FIXME: fsl_alloc_request() ignores ep argument */
2948 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2949 struct fsl_req, req);
2950 /* allocate a small amount of memory to get valid address */
2951 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2952 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2953 /* Initialize ep0 data request structure */
2954 udc->data_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2955 struct fsl_req, req);
2956 udc->data_req->req.buf = kmalloc(8, GFP_KERNEL);
2957 udc->data_req->req.dma = virt_to_phys(udc->data_req->req.buf);
2959 udc->resume_state = USB_STATE_NOTATTACHED;
2960 udc->usb_state = USB_STATE_POWERED;
2962 udc->remote_wakeup = 0; /* default to 0 on reset */
2963 spin_lock_init(&udc->lock);
2968 /*----------------------------------------------------------------
2969 * Setup the fsl_ep struct for eps
2970 * Link fsl_ep->ep to gadget->ep_list
2971 * ep0out is not used so do nothing here
2972 * ep0in should be taken care
2973 *--------------------------------------------------------------*/
2974 static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2975 char *name, int link)
2977 struct fsl_ep *ep = &udc->eps[index];
2980 strcpy(ep->name, name);
2981 ep->ep.name = ep->name;
2983 ep->ep.ops = &fsl_ep_ops;
2985 * For ep0, the endpoint is enabled after controller initialization
2986 * For non-ep0, the endpoint is stopped default, and will be enabled
2987 * by class driver when needed.
2994 /* for ep0: maxP defined in desc
2995 * for other eps, maxP is set by epautoconfig() called by gadget layer
2997 ep->ep.maxpacket = (unsigned short) ~0;
2999 /* the queue lists any req for this ep */
3000 INIT_LIST_HEAD(&ep->queue);
3002 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
3004 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3005 ep->gadget = &udc->gadget;
3006 ep->qh = &udc->ep_qh[index];
3011 /* Driver probe function
3012 * all intialization operations implemented here except enabling usb_intr reg
3013 * board setup should have been done in the platform code
3015 static int __devinit fsl_udc_probe(struct platform_device *pdev)
3017 struct resource *res;
3018 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
3023 udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
3024 if (udc_controller == NULL) {
3025 ERR("malloc udc failed\n");
3028 udc_controller->pdata = pdata;
3030 #ifdef CONFIG_USB_OTG
3031 /* Memory and interrupt resources will be passed from OTG */
3032 udc_controller->transceiver = otg_get_transceiver();
3033 if (!udc_controller->transceiver) {
3034 printk(KERN_ERR "Can't find OTG driver!\n");
3038 udc_controller->gadget.is_otg = 1;
3040 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3046 #ifndef CONFIG_USB_OTG
3047 if (!request_mem_region(res->start, resource_size(res),
3049 ERR("request mem region for %s failed \n", pdev->name);
3054 dr_regs = ioremap(res->start, resource_size(res));
3059 pdata->regs = (void *)dr_regs;
3061 * do platform specific init: check the clock, grab/config pins, etc.
3063 if (pdata->init && pdata->init(pdev)) {
3068 spin_lock_init(&pdata->lock);
3070 /* Due to mx35/mx25's phy's bug */
3073 if (pdata->have_sysif_regs)
3074 usb_sys_regs = (struct usb_sys_interface *)
3075 ((u32)dr_regs + USB_DR_SYS_OFFSET);
3077 /* Read Device Controller Capability Parameters register */
3078 dccparams = fsl_readl(&dr_regs->dccparams);
3079 if (!(dccparams & DCCPARAMS_DC)) {
3080 ERR("This SOC doesn't support device role\n");
3084 /* Get max device endpoints */
3085 /* DEN is bidirectional ep number, max_ep doubles the number */
3086 udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
3088 udc_controller->irq = platform_get_irq(pdev, 0);
3089 if (!udc_controller->irq) {
3094 ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
3095 driver_name, udc_controller);
3097 ERR("cannot request irq %d err %d \n",
3098 udc_controller->irq, ret);
3102 /* Initialize the udc structure including QH member and other member */
3103 if (struct_udc_setup(udc_controller, pdev)) {
3104 ERR("Can't initialize udc data structure\n");
3109 if (!udc_controller->transceiver) {
3110 /* initialize usb hw reg except for regs for EP,
3111 * leave usbintr reg untouched */
3112 dr_controller_setup(udc_controller);
3115 /* Setup gadget structure */
3116 udc_controller->gadget.ops = &fsl_gadget_ops;
3117 udc_controller->gadget.is_dualspeed = 1;
3118 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
3119 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
3120 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
3121 udc_controller->gadget.name = driver_name;
3123 /* Setup gadget.dev and register with kernel */
3124 dev_set_name(&udc_controller->gadget.dev, "gadget");
3125 udc_controller->gadget.dev.release = fsl_udc_release;
3126 udc_controller->gadget.dev.parent = &pdev->dev;
3127 udc_controller->gadget.dev.groups = fsl_udc_attr_groups;
3128 ret = device_register(&udc_controller->gadget.dev);
3132 /* setup QH and epctrl for ep0 */
3133 ep0_setup(udc_controller);
3135 /* setup udc->eps[] for ep0 */
3136 struct_ep_setup(udc_controller, 0, "ep0", 0);
3137 /* for ep0: the desc defined here;
3138 * for other eps, gadget layer called ep_enable with defined desc
3140 udc_controller->eps[0].desc = &fsl_ep0_desc;
3141 udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
3143 /* setup the udc->eps[] for non-control endpoints and link
3144 * to gadget.ep_list */
3145 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
3148 sprintf(name, "ep%dout", i);
3149 struct_ep_setup(udc_controller, i * 2, name, 1);
3150 sprintf(name, "ep%din", i);
3151 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
3154 /* use dma_pool for TD management */
3155 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
3156 sizeof(struct ep_td_struct),
3157 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
3158 if (udc_controller->td_pool == NULL) {
3163 g_iram_addr = iram_alloc(USB_IRAM_SIZE, &g_iram_base);
3164 for (i = 0; i < IRAM_PPH_NTD; i++) {
3165 udc_controller->iram_buffer[i] =
3166 g_iram_base + i * g_iram_size;
3167 udc_controller->iram_buffer_v[i] =
3168 g_iram_addr + i * g_iram_size;
3172 INIT_WORK(&udc_controller->gadget_disconnect_schedule, fsl_gadget_disconnect_event);
3173 #ifdef POSTPONE_FREE_LAST_DTD
3174 last_free_td = NULL;
3177 /* disable all INTR */
3178 #ifndef CONFIG_USB_OTG
3179 fsl_writel(0, &dr_regs->usbintr);
3180 dr_wake_up_enable(udc_controller, false);
3182 dr_wake_up_enable(udc_controller, true);
3186 * As mx25/mx35 does not implement clk_gate, should not let phy to low
3187 * power mode due to IC bug
3189 #if !(defined CONFIG_ARCH_MX35 || defined CONFIG_ARCH_MX25)
3191 dr_phy_low_power_mode(udc_controller, true);
3194 udc_controller->stopped = 1;
3196 /* let the gadget register function open the clk */
3201 /* create usb charger */
3202 #ifdef CONFIG_IMX_USB_CHARGER
3203 udc_controller->charger.dev = &pdev->dev;
3204 udc_controller->charger.dp_pullup = usb_charger_pullup_dp;
3205 udc_controller->charger.enable = true;
3206 if (pdata->charger_base_addr)
3207 udc_controller->charger.charger_base_addr = pdata->charger_base_addr;
3208 if (imx_usb_create_charger(&udc_controller->charger, "imx_usb_charger"))
3209 dev_err(&pdev->dev, "Can't create usb charger\n");
3211 udc_controller->charger.dp_pullup = usb_charger_pullup_dp;
3212 udc_controller->charger.enable = false;
3218 device_unregister(&udc_controller->gadget.dev);
3220 free_irq(udc_controller->irq, udc_controller);
3223 pdata->exit(pdata->pdev);
3225 iounmap((u8 __iomem *)dr_regs);
3227 if (!udc_controller->transceiver)
3228 release_mem_region(res->start, resource_size(res));
3230 kfree(udc_controller);
3231 udc_controller = NULL;
3235 /* Driver removal function
3236 * Free resources and finish pending transactions
3238 static int __exit fsl_udc_remove(struct platform_device *pdev)
3240 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
3242 DECLARE_COMPLETION(done);
3244 if (!udc_controller)
3246 udc_controller->done = &done;
3247 /* open USB PHY clock */
3248 if (udc_controller->stopped)
3251 /* DR has been stopped in usb_gadget_unregister_driver() */
3254 /* Free allocated memory */
3256 iram_free(g_iram_base, IRAM_PPH_NTD * g_iram_size);
3257 kfree(udc_controller->status_req->req.buf);
3258 kfree(udc_controller->status_req);
3259 kfree(udc_controller->data_req->req.buf);
3260 kfree(udc_controller->data_req);
3261 kfree(udc_controller->eps);
3262 #ifdef POSTPONE_FREE_LAST_DTD
3263 if (last_free_td != NULL)
3264 dma_pool_free(udc_controller->td_pool, last_free_td,
3265 last_free_td->td_dma);
3267 dma_pool_destroy(udc_controller->td_pool);
3268 free_irq(udc_controller->irq, udc_controller);
3269 iounmap((u8 __iomem *)dr_regs);
3271 #ifdef CONFIG_IMX_USB_CHARGER
3272 imx_usb_remove_charger(&udc_controller->charger);
3275 #ifndef CONFIG_USB_OTG
3277 struct resource *res;
3278 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3279 release_mem_region(res->start, resource_size(res));
3283 device_unregister(&udc_controller->gadget.dev);
3284 /* free udc --wait for the release() finished */
3285 wait_for_completion(&done);
3287 * do platform specific un-initialization:
3288 * release iomux pins, etc.
3291 pdata->exit(pdata->pdev);
3293 if (udc_controller->stopped)
3299 static bool udc_can_wakeup_system(void)
3301 struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
3303 if (pdata->operating_mode == FSL_USB2_DR_OTG)
3304 if (device_may_wakeup(udc_controller->transceiver->dev))
3309 if (device_may_wakeup(udc_controller->gadget.dev.parent))
3315 static int udc_suspend(struct fsl_udc *udc)
3317 struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
3321 * When it is the PM suspend routine and the device has no
3322 * abilities to wakeup system, it should not set wakeup enable.
3323 * Otherwise, the system will wakeup even the user only wants to
3326 if (pdata->pmflags == 0) {
3327 if (!udc_can_wakeup_system()) {
3328 dr_wake_up_enable(udc, false);
3330 if (pdata->platform_phy_power_on)
3331 pdata->platform_phy_power_on();
3332 dr_wake_up_enable(udc, true);
3337 * If the controller is already stopped, then this must be a
3338 * PM suspend. Remember this fact, so that we will leave the
3339 * controller stopped at PM resume time.
3341 if (udc->suspended) {
3342 printk(KERN_DEBUG "gadget already suspended, leaving early\n");
3346 mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
3347 usbcmd = fsl_readl(&dr_regs->usbcmd);
3348 if (mode != USB_MODE_CTRL_MODE_DEVICE) {
3349 printk(KERN_DEBUG "gadget not in device mode, leaving early\n");
3354 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_A_BUS_VALID)) {
3355 /* stop the controller */
3356 usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
3357 fsl_writel(usbcmd, &dr_regs->usbcmd);
3361 dr_phy_low_power_mode(udc, true);
3363 if (udc->suspended > 1) {
3365 "It's the case usb device is on otg port\
3366 and the gadget driver"
3367 "is loaded during boots up\n"
3368 "So, do not increase suspended counter Or\
3370 "please debug it !!!\n"
3380 /*-----------------------------------------------------------------
3381 * Modify Power management attributes
3382 * Used by OTG statemachine to disable gadget temporarily
3383 -----------------------------------------------------------------*/
3384 static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
3387 printk(KERN_DEBUG "udc suspend begins\n");
3388 if (get_gadget_data(&udc_controller->gadget) == NULL) {
3389 /* if no gadget is binded, quit */
3393 if (udc_controller->stopped)
3395 if (((!(udc_controller->gadget.is_otg)) ||
3396 (fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) &&
3397 (udc_controller->usb_state > USB_STATE_POWERED) &&
3398 (udc_controller->usb_state < USB_STATE_SUSPENDED)) {
3399 return -EBUSY;/* keep the clk on */
3401 ret = udc_suspend(udc_controller);
3404 printk(KERN_DEBUG "USB Gadget suspend ends\n");
3408 /*-----------------------------------------------------------------
3409 * Invoked on USB resume. May be called in_interrupt.
3410 * Here we start the DR controller and enable the irq
3411 *-----------------------------------------------------------------*/
3412 static int fsl_udc_resume(struct platform_device *pdev)
3414 struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
3415 struct fsl_usb2_wakeup_platform_data *wake_up_pdata = pdata->wakeup_pdata;
3416 printk(KERN_DEBUG "USB Gadget resume begins\n");
3418 printk(KERN_DEBUG "%s, Wait for wakeup thread finishes\n", __func__);
3419 wait_event_interruptible(wake_up_pdata->wq, !wake_up_pdata->usb_wakeup_is_pending);
3422 if (get_gadget_data(&udc_controller->gadget) == NULL) {
3423 /* if no gadget is binded, quit */
3426 mutex_lock(&udc_resume_mutex);
3428 pr_debug("%s(): stopped %d suspended %d\n", __func__,
3429 udc_controller->stopped, udc_controller->suspended);
3430 /* Do noop if the udc is already at resume state */
3431 if (udc_controller->suspended == 0) {
3433 if (udc_controller->stopped)
3435 usb_debounce_id_vbus();
3436 if (fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID) {
3437 temp = fsl_readl(&dr_regs->otgsc);
3438 /* if b_session_irq_en is cleared by otg */
3439 if (!(temp & OTGSC_B_SESSION_VALID_IRQ_EN)) {
3440 temp |= OTGSC_B_SESSION_VALID_IRQ_EN;
3441 fsl_writel(temp, &dr_regs->otgsc);
3444 if (udc_controller->stopped)
3446 mutex_unlock(&udc_resume_mutex);
3449 /* prevent the quirk interrupts from resuming */
3450 disable_irq_nosync(udc_controller->irq);
3453 * If the controller was stopped at suspend time, then
3454 * don't resume it now.
3457 if (udc_controller->suspended > 1) {
3458 printk(KERN_DEBUG "gadget was already stopped, leaving early\n");
3459 if (udc_controller->stopped) {
3466 * To fix suspend issue connected to usb charger,if stopped is 0
3467 * suspended is 1,clock on and out of low power mode to avoid
3468 * next system suspend no clock to cause system hang.
3470 if (udc_controller->suspended && !udc_controller->stopped) {
3472 dr_wake_up_enable(udc_controller, false);
3473 dr_phy_low_power_mode(udc_controller, false);
3475 /* Enable DR irq reg and set controller Run */
3476 if (udc_controller->stopped) {
3477 /* the clock is already on at usb wakeup routine */
3478 if (pdata->lowpower)
3480 dr_wake_up_enable(udc_controller, false);
3481 dr_phy_low_power_mode(udc_controller, false);
3482 usb_debounce_id_vbus();
3483 /* if in host mode, we need to do nothing */
3484 if ((fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID) == 0) {
3485 dr_phy_low_power_mode(udc_controller, true);
3486 dr_wake_up_enable(udc_controller, true);
3489 dr_controller_setup(udc_controller);
3490 dr_controller_run(udc_controller);
3493 /* if udc is resume by otg id change and no device
3494 * connecting to the otg, otg will enter low power mode*/
3495 if (udc_controller->stopped) {
3497 * If it is PM resume routine, the udc is at low power mode,
3498 * and the udc has no abilities to wakeup system, it should
3499 * set the abilities to wakeup itself. Otherwise, the usb
3500 * subsystem will not leave from low power mode.
3502 if (!udc_can_wakeup_system() &&
3503 (pdata->pmflags == 0)) {
3504 dr_wake_up_enable(udc_controller, true);
3510 --udc_controller->suspended;
3511 enable_irq(udc_controller->irq);
3512 mutex_unlock(&udc_resume_mutex);
3513 printk(KERN_DEBUG "USB Gadget resume ends\n");
3517 /*-------------------------------------------------------------------------
3518 Register entry point for the peripheral controller driver
3519 --------------------------------------------------------------------------*/
3521 static struct platform_driver udc_driver = {
3522 .remove = __exit_p(fsl_udc_remove),
3523 /* these suspend and resume are not usb suspend and resume */
3524 .suspend = fsl_udc_suspend,
3525 .resume = fsl_udc_resume,
3526 .probe = fsl_udc_probe,
3528 .name = driver_name,
3529 .owner = THIS_MODULE,
3533 static int __init udc_init(void)
3535 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
3536 return platform_driver_register(&udc_driver);
3538 #ifdef CONFIG_MXS_VBUS_CURRENT_DRAW
3539 fs_initcall(udc_init);
3541 module_init(udc_init);
3543 static void __exit udc_exit(void)
3545 platform_driver_unregister(&udc_driver);
3546 printk(KERN_INFO "%s unregistered \n", driver_desc);
3549 module_exit(udc_exit);
3551 MODULE_DESCRIPTION(DRIVER_DESC);
3552 MODULE_AUTHOR(DRIVER_AUTHOR);
3553 MODULE_LICENSE("GPL");