2 * Copyright (C) 2004-2007,2011 Freescale Semiconductor, Inc.
5 * Author: Li Yang <leoli@freescale.com>
6 * Jiang Bo <tanya.jiang@freescale.com>
9 * Freescale high-speed USB SOC DR module device controller driver.
10 * This can be found on MPC8349E/MPC8313E/MPC5121E cpus.
11 * The driver is previously named as mpc_udc. Based on bare board
12 * code from Dave Liu and Shlomi Gridish.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/ioport.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/interrupt.h>
31 #include <linux/proc_fs.h>
33 #include <linux/moduleparam.h>
34 #include <linux/device.h>
35 #include <linux/usb/ch9.h>
36 #include <linux/usb/gadget.h>
37 #include <linux/usb/otg.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/platform_device.h>
40 #include <linux/fsl_devices.h>
41 #include <linux/dmapool.h>
42 #include <linux/delay.h>
44 #include <asm/byteorder.h>
46 #include <asm/system.h>
47 #include <asm/unaligned.h>
50 #include "fsl_usb2_udc.h"
52 #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
53 #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
54 #define DRIVER_VERSION "Apr 20, 2007"
56 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
58 static const char driver_name[] = "fsl-usb2-udc";
59 static const char driver_desc[] = DRIVER_DESC;
61 static struct usb_dr_device *dr_regs;
62 #ifndef CONFIG_ARCH_MXC
63 static struct usb_sys_interface *usb_sys_regs;
66 /* it is initialized in probe() */
67 static struct fsl_udc *udc_controller = NULL;
69 static const struct usb_endpoint_descriptor
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = 0,
74 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
75 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
78 static void fsl_ep_fifo_flush(struct usb_ep *_ep);
82 * On some SoCs, the USB controller registers can be big or little endian,
83 * depending on the version of the chip. In order to be able to run the
84 * same kernel binary on 2 different versions of an SoC, the BE/LE decision
85 * must be made at run time. _fsl_readl and fsl_writel are pointers to the
86 * BE or LE readl() and writel() functions, and fsl_readl() and fsl_writel()
87 * call through those pointers. Platform code for SoCs that have BE USB
88 * registers should set pdata->big_endian_mmio flag.
90 * This also applies to controller-to-cpu accessors for the USB descriptors,
91 * since their endianness is also SoC dependant. Platform code for SoCs that
92 * have BE USB descriptors should set pdata->big_endian_desc flag.
94 static u32 _fsl_readl_be(const unsigned __iomem *p)
99 static u32 _fsl_readl_le(const unsigned __iomem *p)
104 static void _fsl_writel_be(u32 v, unsigned __iomem *p)
109 static void _fsl_writel_le(u32 v, unsigned __iomem *p)
114 static u32 (*_fsl_readl)(const unsigned __iomem *p);
115 static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
117 #define fsl_readl(p) (*_fsl_readl)((p))
118 #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
120 static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata)
122 if (pdata->big_endian_mmio) {
123 _fsl_readl = _fsl_readl_be;
124 _fsl_writel = _fsl_writel_be;
126 _fsl_readl = _fsl_readl_le;
127 _fsl_writel = _fsl_writel_le;
131 static inline u32 cpu_to_hc32(const u32 x)
133 return udc_controller->pdata->big_endian_desc
134 ? (__force u32)cpu_to_be32(x)
135 : (__force u32)cpu_to_le32(x);
138 static inline u32 hc32_to_cpu(const u32 x)
140 return udc_controller->pdata->big_endian_desc
141 ? be32_to_cpu((__force __be32)x)
142 : le32_to_cpu((__force __le32)x);
144 #else /* !CONFIG_PPC32 */
145 static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata) {}
147 #define fsl_readl(addr) readl(addr)
148 #define fsl_writel(val32, addr) writel(val32, addr)
149 #define cpu_to_hc32(x) cpu_to_le32(x)
150 #define hc32_to_cpu(x) le32_to_cpu(x)
151 #endif /* CONFIG_PPC32 */
153 /********************************************************************
154 * Internal Used Function
155 ********************************************************************/
156 /*-----------------------------------------------------------------
157 * done() - retire a request; caller blocked irqs
158 * @status : request status to be set, only works when
159 * request is still in progress.
160 *--------------------------------------------------------------*/
161 static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
163 struct fsl_udc *udc = NULL;
164 unsigned char stopped = ep->stopped;
165 struct ep_td_struct *curr_td, *next_td;
168 udc = (struct fsl_udc *)ep->udc;
169 /* Removed the req from fsl_ep->queue */
170 list_del_init(&req->queue);
172 /* req.status should be set as -EINPROGRESS in ep_queue() */
173 if (req->req.status == -EINPROGRESS)
174 req->req.status = status;
176 status = req->req.status;
178 /* Free dtd for the request */
180 for (j = 0; j < req->dtd_count; j++) {
182 if (j != req->dtd_count - 1) {
183 next_td = curr_td->next_td_virt;
185 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
189 dma_unmap_single(ep->udc->gadget.dev.parent,
190 req->req.dma, req->req.length,
194 req->req.dma = DMA_ADDR_INVALID;
197 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
198 req->req.dma, req->req.length,
203 if (status && (status != -ESHUTDOWN))
204 VDBG("complete %s req %p stat %d len %u/%u",
205 ep->ep.name, &req->req, status,
206 req->req.actual, req->req.length);
210 spin_unlock(&ep->udc->lock);
211 /* complete() is from gadget layer,
212 * eg fsg->bulk_in_complete() */
213 if (req->req.complete)
214 req->req.complete(&ep->ep, &req->req);
216 spin_lock(&ep->udc->lock);
217 ep->stopped = stopped;
220 /*-----------------------------------------------------------------
221 * nuke(): delete all requests related to this ep
222 * called with spinlock held
223 *--------------------------------------------------------------*/
224 static void nuke(struct fsl_ep *ep, int status)
229 fsl_ep_fifo_flush(&ep->ep);
231 /* Whether this eq has request linked */
232 while (!list_empty(&ep->queue)) {
233 struct fsl_req *req = NULL;
235 req = list_entry(ep->queue.next, struct fsl_req, queue);
236 done(ep, req, status);
240 /*------------------------------------------------------------------
241 Internal Hardware related function
242 ------------------------------------------------------------------*/
244 static int dr_controller_setup(struct fsl_udc *udc)
246 unsigned int tmp, portctrl, ep_num;
247 unsigned int max_no_of_ep;
248 #ifndef CONFIG_ARCH_MXC
251 unsigned long timeout;
252 #define FSL_UDC_RESET_TIMEOUT 1000
254 /* Config PHY interface */
255 portctrl = fsl_readl(&dr_regs->portsc1);
256 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
257 switch (udc->phy_mode) {
258 case FSL_USB2_PHY_ULPI:
259 portctrl |= PORTSCX_PTS_ULPI;
261 case FSL_USB2_PHY_UTMI_WIDE:
262 portctrl |= PORTSCX_PTW_16BIT;
264 case FSL_USB2_PHY_UTMI:
265 portctrl |= PORTSCX_PTS_UTMI;
267 case FSL_USB2_PHY_SERIAL:
268 portctrl |= PORTSCX_PTS_FSLS;
273 fsl_writel(portctrl, &dr_regs->portsc1);
275 /* Stop and reset the usb controller */
276 tmp = fsl_readl(&dr_regs->usbcmd);
277 tmp &= ~USB_CMD_RUN_STOP;
278 fsl_writel(tmp, &dr_regs->usbcmd);
280 tmp = fsl_readl(&dr_regs->usbcmd);
281 tmp |= USB_CMD_CTRL_RESET;
282 fsl_writel(tmp, &dr_regs->usbcmd);
284 /* Wait for reset to complete */
285 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
286 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
287 if (time_after(jiffies, timeout)) {
288 ERR("udc reset timeout!\n");
294 /* Set the controller as device mode */
295 tmp = fsl_readl(&dr_regs->usbmode);
296 tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
297 tmp |= USB_MODE_CTRL_MODE_DEVICE;
298 /* Disable Setup Lockout */
299 tmp |= USB_MODE_SETUP_LOCK_OFF;
302 fsl_writel(tmp, &dr_regs->usbmode);
304 /* Clear the setup status */
305 fsl_writel(0, &dr_regs->usbsts);
307 tmp = udc->ep_qh_dma;
308 tmp &= USB_EP_LIST_ADDRESS_MASK;
309 fsl_writel(tmp, &dr_regs->endpointlistaddr);
311 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
312 udc->ep_qh, (int)tmp,
313 fsl_readl(&dr_regs->endpointlistaddr));
315 max_no_of_ep = (0x0000001F & fsl_readl(&dr_regs->dccparams));
316 for (ep_num = 1; ep_num < max_no_of_ep; ep_num++) {
317 tmp = fsl_readl(&dr_regs->endptctrl[ep_num]);
318 tmp &= ~(EPCTRL_TX_TYPE | EPCTRL_RX_TYPE);
319 tmp |= (EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT)
320 | (EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT);
321 fsl_writel(tmp, &dr_regs->endptctrl[ep_num]);
323 /* Config control enable i/o output, cpu endian register */
324 #ifndef CONFIG_ARCH_MXC
325 if (udc->pdata->have_sysif_regs) {
326 ctrl = __raw_readl(&usb_sys_regs->control);
327 ctrl |= USB_CTRL_IOENB;
328 __raw_writel(ctrl, &usb_sys_regs->control);
332 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
333 /* Turn on cache snooping hardware, since some PowerPC platforms
334 * wholly rely on hardware to deal with cache coherent. */
336 if (udc->pdata->have_sysif_regs) {
337 /* Setup Snooping for all the 4GB space */
338 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
339 __raw_writel(tmp, &usb_sys_regs->snoop1);
340 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
341 __raw_writel(tmp, &usb_sys_regs->snoop2);
348 /* Enable DR irq and set controller to run state */
349 static void dr_controller_run(struct fsl_udc *udc)
353 /* Enable DR irq reg */
354 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
355 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
356 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
358 fsl_writel(temp, &dr_regs->usbintr);
360 /* Clear stopped bit */
363 /* Set the controller as device mode */
364 temp = fsl_readl(&dr_regs->usbmode);
365 temp |= USB_MODE_CTRL_MODE_DEVICE;
366 fsl_writel(temp, &dr_regs->usbmode);
368 /* Set controller to Run */
369 temp = fsl_readl(&dr_regs->usbcmd);
370 temp |= USB_CMD_RUN_STOP;
371 fsl_writel(temp, &dr_regs->usbcmd);
374 static void dr_controller_stop(struct fsl_udc *udc)
378 pr_debug("%s\n", __func__);
380 /* if we're in OTG mode, and the Host is currently using the port,
381 * stop now and don't rip the controller out from under the
384 if (udc->gadget.is_otg) {
385 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
386 pr_debug("udc: Leaving early\n");
391 /* disable all INTR */
392 fsl_writel(0, &dr_regs->usbintr);
394 /* Set stopped bit for isr */
397 /* disable IO output */
398 /* usb_sys_regs->control = 0; */
400 /* set controller to Stop */
401 tmp = fsl_readl(&dr_regs->usbcmd);
402 tmp &= ~USB_CMD_RUN_STOP;
403 fsl_writel(tmp, &dr_regs->usbcmd);
406 static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
407 unsigned char ep_type)
409 unsigned int tmp_epctrl = 0;
411 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
414 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
415 tmp_epctrl |= EPCTRL_TX_ENABLE;
416 tmp_epctrl &= ~EPCTRL_TX_TYPE;
417 tmp_epctrl |= ((unsigned int)(ep_type)
418 << EPCTRL_TX_EP_TYPE_SHIFT);
421 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
422 tmp_epctrl |= EPCTRL_RX_ENABLE;
423 tmp_epctrl &= ~EPCTRL_RX_TYPE;
424 tmp_epctrl |= ((unsigned int)(ep_type)
425 << EPCTRL_RX_EP_TYPE_SHIFT);
428 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
432 dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
436 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
439 /* set the stall bit */
441 tmp_epctrl |= EPCTRL_TX_EP_STALL;
443 tmp_epctrl |= EPCTRL_RX_EP_STALL;
445 /* clear the stall bit and reset data toggle */
447 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
448 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
450 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
451 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
454 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
457 /* Get stall status of a specific ep
458 Return: 0: not stalled; 1:stalled */
459 static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
463 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
465 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
467 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
470 /********************************************************************
471 Internal Structure Build up functions
472 ********************************************************************/
474 /*------------------------------------------------------------------
475 * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
476 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
478 ------------------------------------------------------------------*/
479 static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
480 unsigned char dir, unsigned char ep_type,
481 unsigned int max_pkt_len,
482 unsigned int zlt, unsigned char mult)
484 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
485 unsigned int tmp = 0;
487 /* set the Endpoint Capabilites in QH */
489 case USB_ENDPOINT_XFER_CONTROL:
490 /* Interrupt On Setup (IOS). for control ep */
491 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
494 case USB_ENDPOINT_XFER_ISOC:
495 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
496 | (mult << EP_QUEUE_HEAD_MULT_POS);
498 case USB_ENDPOINT_XFER_BULK:
499 case USB_ENDPOINT_XFER_INT:
500 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
503 VDBG("error ep type is %d", ep_type);
507 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
509 p_QH->max_pkt_length = cpu_to_hc32(tmp);
510 p_QH->next_dtd_ptr = 1;
511 p_QH->size_ioc_int_sts = 0;
514 /* Setup qh structure and ep register for ep0. */
515 static void ep0_setup(struct fsl_udc *udc)
517 /* the intialization of an ep includes: fields in QH, Regs,
519 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
520 USB_MAX_CTRL_PAYLOAD, 0, 0);
521 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
522 USB_MAX_CTRL_PAYLOAD, 0, 0);
523 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
524 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
530 /***********************************************************************
531 Endpoint Management Functions
532 ***********************************************************************/
534 /*-------------------------------------------------------------------------
535 * when configurations are set, or when interface settings change
536 * for example the do_set_interface() in gadget layer,
537 * the driver will enable or disable the relevant endpoints
538 * ep0 doesn't use this routine. It is always enabled.
539 -------------------------------------------------------------------------*/
540 static int fsl_ep_enable(struct usb_ep *_ep,
541 const struct usb_endpoint_descriptor *desc)
543 struct fsl_udc *udc = NULL;
544 struct fsl_ep *ep = NULL;
545 unsigned short max = 0;
546 unsigned char mult = 0, zlt;
547 int retval = -EINVAL;
548 unsigned long flags = 0;
550 ep = container_of(_ep, struct fsl_ep, ep);
552 /* catch various bogus parameters */
553 if (!_ep || !desc || ep->desc
554 || (desc->bDescriptorType != USB_DT_ENDPOINT))
559 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
562 max = usb_endpoint_maxp(desc);
564 /* Disable automatic zlp generation. Driver is responsible to indicate
565 * explicitly through req->req.zero. This is needed to enable multi-td
569 /* Assume the max packet size from gadget is always correct */
570 switch (desc->bmAttributes & 0x03) {
571 case USB_ENDPOINT_XFER_CONTROL:
572 case USB_ENDPOINT_XFER_BULK:
573 case USB_ENDPOINT_XFER_INT:
574 /* mult = 0. Execute N Transactions as demonstrated by
575 * the USB variable length packet protocol where N is
576 * computed using the Maximum Packet Length (dQH) and
577 * the Total Bytes field (dTD) */
580 case USB_ENDPOINT_XFER_ISOC:
581 /* Calculate transactions needed for high bandwidth iso */
582 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
583 max = max & 0x7ff; /* bit 0~10 */
584 /* 3 transactions at most */
592 spin_lock_irqsave(&udc->lock, flags);
593 ep->ep.maxpacket = max;
597 /* Controller related setup */
598 /* Init EPx Queue Head (Ep Capabilites field in QH
599 * according to max, zlt, mult) */
600 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
601 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
602 ? USB_SEND : USB_RECV),
603 (unsigned char) (desc->bmAttributes
604 & USB_ENDPOINT_XFERTYPE_MASK),
607 /* Init endpoint ctrl register */
608 dr_ep_setup((unsigned char) ep_index(ep),
609 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
610 ? USB_SEND : USB_RECV),
611 (unsigned char) (desc->bmAttributes
612 & USB_ENDPOINT_XFERTYPE_MASK));
614 spin_unlock_irqrestore(&udc->lock, flags);
617 VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
618 ep->desc->bEndpointAddress & 0x0f,
619 (desc->bEndpointAddress & USB_DIR_IN)
620 ? "in" : "out", max);
625 /*---------------------------------------------------------------------
626 * @ep : the ep being unconfigured. May not be ep0
627 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
628 *---------------------------------------------------------------------*/
629 static int fsl_ep_disable(struct usb_ep *_ep)
631 struct fsl_udc *udc = NULL;
632 struct fsl_ep *ep = NULL;
633 unsigned long flags = 0;
637 ep = container_of(_ep, struct fsl_ep, ep);
638 if (!_ep || !ep->desc) {
639 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
643 /* disable ep on controller */
644 ep_num = ep_index(ep);
645 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
647 epctrl &= ~(EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE);
648 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT;
650 epctrl &= ~(EPCTRL_RX_ENABLE | EPCTRL_TX_TYPE);
651 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT;
653 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
655 udc = (struct fsl_udc *)ep->udc;
656 spin_lock_irqsave(&udc->lock, flags);
658 /* nuke all pending requests (does flush) */
659 nuke(ep, -ESHUTDOWN);
664 spin_unlock_irqrestore(&udc->lock, flags);
666 VDBG("disabled %s OK", _ep->name);
670 /*---------------------------------------------------------------------
671 * allocate a request object used by this endpoint
672 * the main operation is to insert the req->queue to the eq->queue
673 * Returns the request, or null if one could not be allocated
674 *---------------------------------------------------------------------*/
675 static struct usb_request *
676 fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
678 struct fsl_req *req = NULL;
680 req = kzalloc(sizeof *req, gfp_flags);
684 req->req.dma = DMA_ADDR_INVALID;
685 INIT_LIST_HEAD(&req->queue);
690 static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
692 struct fsl_req *req = NULL;
694 req = container_of(_req, struct fsl_req, req);
700 /* Actually add a dTD chain to an empty dQH and let go */
701 static void fsl_prime_ep(struct fsl_ep *ep, struct ep_td_struct *td)
703 struct ep_queue_head *qh = get_qh_by_ep(ep);
705 /* Write dQH next pointer and terminate bit to 0 */
706 qh->next_dtd_ptr = cpu_to_hc32(td->td_dma
707 & EP_QUEUE_HEAD_NEXT_POINTER_MASK);
709 /* Clear active and halt bit */
710 qh->size_ioc_int_sts &= cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
711 | EP_QUEUE_HEAD_STATUS_HALT));
713 /* Ensure that updates to the QH will occur before priming. */
716 /* Prime endpoint by writing correct bit to ENDPTPRIME */
717 fsl_writel(ep_is_in(ep) ? (1 << (ep_index(ep) + 16))
718 : (1 << (ep_index(ep))), &dr_regs->endpointprime);
721 /* Add dTD chain to the dQH of an EP */
722 static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
724 u32 temp, bitmask, tmp_stat;
726 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
727 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
729 bitmask = ep_is_in(ep)
730 ? (1 << (ep_index(ep) + 16))
731 : (1 << (ep_index(ep)));
733 /* check if the pipe is empty */
734 if (!(list_empty(&ep->queue))) {
735 /* Add td to the end */
736 struct fsl_req *lastreq;
737 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
738 lastreq->tail->next_td_ptr =
739 cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
740 /* Read prime bit, if 1 goto done */
741 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
745 /* Set ATDTW bit in USBCMD */
746 temp = fsl_readl(&dr_regs->usbcmd);
747 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
749 /* Read correct status bit */
750 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
752 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
754 /* Write ATDTW bit to 0 */
755 temp = fsl_readl(&dr_regs->usbcmd);
756 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
762 fsl_prime_ep(ep, req->head);
765 /* Fill in the dTD structure
766 * @req: request that the transfer belongs to
767 * @length: return actually data length of the dTD
768 * @dma: return dma address of the dTD
769 * @is_last: return flag if it is the last dTD of the request
770 * return: pointer to the built dTD */
771 static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
772 dma_addr_t *dma, int *is_last)
775 struct ep_td_struct *dtd;
777 /* how big will this transfer be? */
778 *length = min(req->req.length - req->req.actual,
779 (unsigned)EP_MAX_LENGTH_TRANSFER);
781 dtd = dma_pool_alloc(udc_controller->td_pool, GFP_KERNEL, dma);
786 /* Clear reserved field */
787 swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
788 swap_temp &= ~DTD_RESERVED_FIELDS;
789 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
791 /* Init all of buffer page pointers */
792 swap_temp = (u32) (req->req.dma + req->req.actual);
793 dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
794 dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
795 dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
796 dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
797 dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
799 req->req.actual += *length;
801 /* zlp is needed if req->req.zero is set */
803 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
807 } else if (req->req.length == req->req.actual)
813 VDBG("multi-dtd request!");
814 /* Fill in the transfer size; set active bit */
815 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
817 /* Enable interrupt for the last dtd of a request */
818 if (*is_last && !req->req.no_interrupt)
819 swap_temp |= DTD_IOC;
821 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
825 VDBG("length = %d address= 0x%x", *length, (int)*dma);
830 /* Generate dtd chain for a request */
831 static int fsl_req_to_dtd(struct fsl_req *req)
836 struct ep_td_struct *last_dtd = NULL, *dtd;
840 dtd = fsl_build_dtd(req, &count, &dma, &is_last);
848 last_dtd->next_td_ptr = cpu_to_hc32(dma);
849 last_dtd->next_td_virt = dtd;
856 dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
863 /* queues (submits) an I/O request to an endpoint */
865 fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
867 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
868 struct fsl_req *req = container_of(_req, struct fsl_req, req);
872 /* catch various bogus parameters */
873 if (!_req || !req->req.complete || !req->req.buf
874 || !list_empty(&req->queue)) {
875 VDBG("%s, bad params", __func__);
878 if (unlikely(!_ep || !ep->desc)) {
879 VDBG("%s, bad ep", __func__);
882 if (usb_endpoint_xfer_isoc(ep->desc)) {
883 if (req->req.length > ep->ep.maxpacket)
888 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
893 /* map virtual address to hardware */
894 if (req->req.dma == DMA_ADDR_INVALID) {
895 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
897 req->req.length, ep_is_in(ep)
902 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
903 req->req.dma, req->req.length,
910 req->req.status = -EINPROGRESS;
914 spin_lock_irqsave(&udc->lock, flags);
916 /* build dtds and push them to device queue */
917 if (!fsl_req_to_dtd(req)) {
918 fsl_queue_td(ep, req);
920 spin_unlock_irqrestore(&udc->lock, flags);
924 /* Update ep0 state */
925 if ((ep_index(ep) == 0))
926 udc->ep0_state = DATA_STATE_XMIT;
928 /* irq handler advances the queue */
930 list_add_tail(&req->queue, &ep->queue);
931 spin_unlock_irqrestore(&udc->lock, flags);
936 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
937 static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
939 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
942 int ep_num, stopped, ret = 0;
948 spin_lock_irqsave(&ep->udc->lock, flags);
949 stopped = ep->stopped;
951 /* Stop the ep before we deal with the queue */
953 ep_num = ep_index(ep);
954 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
956 epctrl &= ~EPCTRL_TX_ENABLE;
958 epctrl &= ~EPCTRL_RX_ENABLE;
959 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
961 /* make sure it's actually queued on this endpoint */
962 list_for_each_entry(req, &ep->queue, queue) {
963 if (&req->req == _req)
966 if (&req->req != _req) {
971 /* The request is in progress, or completed but not dequeued */
972 if (ep->queue.next == &req->queue) {
973 _req->status = -ECONNRESET;
974 fsl_ep_fifo_flush(_ep); /* flush current transfer */
976 /* The request isn't the last request in this ep queue */
977 if (req->queue.next != &ep->queue) {
978 struct fsl_req *next_req;
980 next_req = list_entry(req->queue.next, struct fsl_req,
983 /* prime with dTD of next request */
984 fsl_prime_ep(ep, next_req->head);
986 /* The request hasn't been processed, patch up the TD chain */
988 struct fsl_req *prev_req;
990 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
991 prev_req->tail->next_td_ptr = req->tail->next_td_ptr;
994 done(ep, req, -ECONNRESET);
997 out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
999 epctrl |= EPCTRL_TX_ENABLE;
1001 epctrl |= EPCTRL_RX_ENABLE;
1002 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
1003 ep->stopped = stopped;
1005 spin_unlock_irqrestore(&ep->udc->lock, flags);
1009 /*-------------------------------------------------------------------------*/
1011 /*-----------------------------------------------------------------
1012 * modify the endpoint halt feature
1013 * @ep: the non-isochronous endpoint being stalled
1014 * @value: 1--set halt 0--clear halt
1015 * Returns zero, or a negative error code.
1016 *----------------------------------------------------------------*/
1017 static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
1019 struct fsl_ep *ep = NULL;
1020 unsigned long flags = 0;
1021 int status = -EOPNOTSUPP; /* operation not supported */
1022 unsigned char ep_dir = 0, ep_num = 0;
1023 struct fsl_udc *udc = NULL;
1025 ep = container_of(_ep, struct fsl_ep, ep);
1027 if (!_ep || !ep->desc) {
1032 if (usb_endpoint_xfer_isoc(ep->desc)) {
1033 status = -EOPNOTSUPP;
1037 /* Attempt to halt IN ep will fail if any transfer requests
1038 * are still queue */
1039 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1045 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1046 ep_num = (unsigned char)(ep_index(ep));
1047 spin_lock_irqsave(&ep->udc->lock, flags);
1048 dr_ep_change_stall(ep_num, ep_dir, value);
1049 spin_unlock_irqrestore(&ep->udc->lock, flags);
1051 if (ep_index(ep) == 0) {
1052 udc->ep0_state = WAIT_FOR_SETUP;
1056 VDBG(" %s %s halt stat %d", ep->ep.name,
1057 value ? "set" : "clear", status);
1062 static int fsl_ep_fifo_status(struct usb_ep *_ep)
1065 struct fsl_udc *udc;
1068 struct ep_queue_head *qh;
1070 ep = container_of(_ep, struct fsl_ep, ep);
1071 if (!_ep || (!ep->desc && ep_index(ep) != 0))
1074 udc = (struct fsl_udc *)ep->udc;
1076 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1079 qh = get_qh_by_ep(ep);
1081 bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
1082 (1 << (ep_index(ep)));
1084 if (fsl_readl(&dr_regs->endptstatus) & bitmask)
1085 size = (qh->size_ioc_int_sts & DTD_PACKET_SIZE)
1086 >> DTD_LENGTH_BIT_POS;
1088 pr_debug("%s %u\n", __func__, size);
1092 static void fsl_ep_fifo_flush(struct usb_ep *_ep)
1097 unsigned long timeout;
1098 #define FSL_UDC_FLUSH_TIMEOUT 1000
1103 ep = container_of(_ep, struct fsl_ep, ep);
1107 ep_num = ep_index(ep);
1108 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1111 bits = (1 << 16) | 1;
1112 else if (ep_dir == USB_SEND)
1113 bits = 1 << (16 + ep_num);
1117 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
1119 fsl_writel(bits, &dr_regs->endptflush);
1121 /* Wait until flush complete */
1122 while (fsl_readl(&dr_regs->endptflush)) {
1123 if (time_after(jiffies, timeout)) {
1124 ERR("ep flush timeout\n");
1129 /* See if we need to flush again */
1130 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1133 static struct usb_ep_ops fsl_ep_ops = {
1134 .enable = fsl_ep_enable,
1135 .disable = fsl_ep_disable,
1137 .alloc_request = fsl_alloc_request,
1138 .free_request = fsl_free_request,
1140 .queue = fsl_ep_queue,
1141 .dequeue = fsl_ep_dequeue,
1143 .set_halt = fsl_ep_set_halt,
1144 .fifo_status = fsl_ep_fifo_status,
1145 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1148 /*-------------------------------------------------------------------------
1149 Gadget Driver Layer Operations
1150 -------------------------------------------------------------------------*/
1152 /*----------------------------------------------------------------------
1153 * Get the current frame number (from DR frame_index Reg )
1154 *----------------------------------------------------------------------*/
1155 static int fsl_get_frame(struct usb_gadget *gadget)
1157 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1160 /*-----------------------------------------------------------------------
1161 * Tries to wake up the host connected to this gadget
1162 -----------------------------------------------------------------------*/
1163 static int fsl_wakeup(struct usb_gadget *gadget)
1165 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1168 /* Remote wakeup feature not enabled by host */
1169 if (!udc->remote_wakeup)
1172 portsc = fsl_readl(&dr_regs->portsc1);
1173 /* not suspended? */
1174 if (!(portsc & PORTSCX_PORT_SUSPEND))
1176 /* trigger force resume */
1177 portsc |= PORTSCX_PORT_FORCE_RESUME;
1178 fsl_writel(portsc, &dr_regs->portsc1);
1182 static int can_pullup(struct fsl_udc *udc)
1184 return udc->driver && udc->softconnect && udc->vbus_active;
1187 /* Notify controller that VBUS is powered, Called by whatever
1188 detects VBUS sessions */
1189 static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1191 struct fsl_udc *udc;
1192 unsigned long flags;
1194 udc = container_of(gadget, struct fsl_udc, gadget);
1195 spin_lock_irqsave(&udc->lock, flags);
1196 VDBG("VBUS %s", is_active ? "on" : "off");
1197 udc->vbus_active = (is_active != 0);
1198 if (can_pullup(udc))
1199 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1202 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1204 spin_unlock_irqrestore(&udc->lock, flags);
1208 /* constrain controller's VBUS power usage
1209 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1210 * reporting how much power the device may consume. For example, this
1211 * could affect how quickly batteries are recharged.
1213 * Returns zero on success, else negative errno.
1215 static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1217 struct fsl_udc *udc;
1219 udc = container_of(gadget, struct fsl_udc, gadget);
1220 if (udc->transceiver)
1221 return otg_set_power(udc->transceiver, mA);
1225 /* Change Data+ pullup status
1226 * this func is used by usb_gadget_connect/disconnet
1228 static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1230 struct fsl_udc *udc;
1232 udc = container_of(gadget, struct fsl_udc, gadget);
1233 udc->softconnect = (is_on != 0);
1234 if (can_pullup(udc))
1235 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1238 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1244 static int fsl_start(struct usb_gadget_driver *driver,
1245 int (*bind)(struct usb_gadget *));
1246 static int fsl_stop(struct usb_gadget_driver *driver);
1247 /* defined in gadget.h */
1248 static struct usb_gadget_ops fsl_gadget_ops = {
1249 .get_frame = fsl_get_frame,
1250 .wakeup = fsl_wakeup,
1251 /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1252 .vbus_session = fsl_vbus_session,
1253 .vbus_draw = fsl_vbus_draw,
1254 .pullup = fsl_pullup,
1259 /* Set protocol stall on ep0, protocol stall will automatically be cleared
1260 on new transaction */
1261 static void ep0stall(struct fsl_udc *udc)
1265 /* must set tx and rx to stall at the same time */
1266 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1267 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1268 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1269 udc->ep0_state = WAIT_FOR_SETUP;
1273 /* Prime a status phase for ep0 */
1274 static int ep0_prime_status(struct fsl_udc *udc, int direction)
1276 struct fsl_req *req = udc->status_req;
1279 if (direction == EP_DIR_IN)
1280 udc->ep0_dir = USB_DIR_IN;
1282 udc->ep0_dir = USB_DIR_OUT;
1285 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1288 req->req.length = 0;
1289 req->req.status = -EINPROGRESS;
1290 req->req.actual = 0;
1291 req->req.complete = NULL;
1294 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1295 req->req.buf, req->req.length,
1296 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1299 if (fsl_req_to_dtd(req) == 0)
1300 fsl_queue_td(ep, req);
1304 list_add_tail(&req->queue, &ep->queue);
1309 static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1311 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1314 nuke(ep, -ESHUTDOWN);
1320 static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1322 /* Save the new address to device struct */
1323 udc->device_address = (u8) value;
1324 /* Update usb state */
1325 udc->usb_state = USB_STATE_ADDRESS;
1327 if (ep0_prime_status(udc, EP_DIR_IN))
1334 static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1335 u16 index, u16 length)
1337 u16 tmp = 0; /* Status, cpu endian */
1338 struct fsl_req *req;
1343 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1344 /* Get device status */
1345 tmp = 1 << USB_DEVICE_SELF_POWERED;
1346 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1347 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1348 /* Get interface status */
1349 /* We don't have interface information in udc driver */
1351 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1352 /* Get endpoint status */
1353 struct fsl_ep *target_ep;
1355 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1357 /* stall if endpoint doesn't exist */
1358 if (!target_ep->desc)
1360 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1361 << USB_ENDPOINT_HALT;
1364 udc->ep0_dir = USB_DIR_IN;
1365 /* Borrow the per device status_req */
1366 req = udc->status_req;
1367 /* Fill in the reqest structure */
1368 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1371 req->req.length = 2;
1372 req->req.status = -EINPROGRESS;
1373 req->req.actual = 0;
1374 req->req.complete = NULL;
1377 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1378 req->req.buf, req->req.length,
1379 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1382 /* prime the data phase */
1383 if ((fsl_req_to_dtd(req) == 0))
1384 fsl_queue_td(ep, req);
1388 list_add_tail(&req->queue, &ep->queue);
1389 udc->ep0_state = DATA_STATE_XMIT;
1395 static void setup_received_irq(struct fsl_udc *udc,
1396 struct usb_ctrlrequest *setup)
1398 u16 wValue = le16_to_cpu(setup->wValue);
1399 u16 wIndex = le16_to_cpu(setup->wIndex);
1400 u16 wLength = le16_to_cpu(setup->wLength);
1402 udc_reset_ep_queue(udc, 0);
1404 /* We process some stardard setup requests here */
1405 switch (setup->bRequest) {
1406 case USB_REQ_GET_STATUS:
1407 /* Data+Status phase from udc */
1408 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1409 != (USB_DIR_IN | USB_TYPE_STANDARD))
1411 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1414 case USB_REQ_SET_ADDRESS:
1415 /* Status phase from udc */
1416 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1417 | USB_RECIP_DEVICE))
1419 ch9setaddress(udc, wValue, wIndex, wLength);
1422 case USB_REQ_CLEAR_FEATURE:
1423 case USB_REQ_SET_FEATURE:
1424 /* Status phase from udc */
1426 int rc = -EOPNOTSUPP;
1429 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1430 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
1431 int pipe = get_pipe_by_windex(wIndex);
1434 if (wValue != 0 || wLength != 0 || pipe > udc->max_ep)
1436 ep = get_ep_by_pipe(udc, pipe);
1438 spin_unlock(&udc->lock);
1439 rc = fsl_ep_set_halt(&ep->ep,
1440 (setup->bRequest == USB_REQ_SET_FEATURE)
1442 spin_lock(&udc->lock);
1444 } else if ((setup->bRequestType & (USB_RECIP_MASK
1445 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
1446 | USB_TYPE_STANDARD)) {
1447 /* Note: The driver has not include OTG support yet.
1448 * This will be set when OTG support is added */
1449 if (wValue == USB_DEVICE_TEST_MODE)
1451 else if (gadget_is_otg(&udc->gadget)) {
1452 if (setup->bRequest ==
1453 USB_DEVICE_B_HNP_ENABLE)
1454 udc->gadget.b_hnp_enable = 1;
1455 else if (setup->bRequest ==
1456 USB_DEVICE_A_HNP_SUPPORT)
1457 udc->gadget.a_hnp_support = 1;
1458 else if (setup->bRequest ==
1459 USB_DEVICE_A_ALT_HNP_SUPPORT)
1460 udc->gadget.a_alt_hnp_support = 1;
1467 if (ep0_prime_status(udc, EP_DIR_IN))
1474 tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
1475 fsl_writel(tmp, &dr_regs->portsc1);
1476 printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
1486 /* Requests handled by gadget */
1488 /* Data phase from gadget, status phase from udc */
1489 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1490 ? USB_DIR_IN : USB_DIR_OUT;
1491 spin_unlock(&udc->lock);
1492 if (udc->driver->setup(&udc->gadget,
1493 &udc->local_setup_buff) < 0)
1495 spin_lock(&udc->lock);
1496 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1497 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1499 /* No data phase, IN status from gadget */
1500 udc->ep0_dir = USB_DIR_IN;
1501 spin_unlock(&udc->lock);
1502 if (udc->driver->setup(&udc->gadget,
1503 &udc->local_setup_buff) < 0)
1505 spin_lock(&udc->lock);
1506 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1510 /* Process request for Data or Status phase of ep0
1511 * prime status phase if needed */
1512 static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1513 struct fsl_req *req)
1515 if (udc->usb_state == USB_STATE_ADDRESS) {
1516 /* Set the new address */
1517 u32 new_address = (u32) udc->device_address;
1518 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1519 &dr_regs->deviceaddr);
1524 switch (udc->ep0_state) {
1525 case DATA_STATE_XMIT:
1526 /* receive status phase */
1527 if (ep0_prime_status(udc, EP_DIR_OUT))
1530 case DATA_STATE_RECV:
1531 /* send status phase */
1532 if (ep0_prime_status(udc, EP_DIR_IN))
1535 case WAIT_FOR_OUT_STATUS:
1536 udc->ep0_state = WAIT_FOR_SETUP;
1538 case WAIT_FOR_SETUP:
1539 ERR("Unexpect ep0 packets\n");
1547 /* Tripwire mechanism to ensure a setup packet payload is extracted without
1548 * being corrupted by another incoming setup packet */
1549 static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1552 struct ep_queue_head *qh;
1553 struct fsl_usb2_platform_data *pdata = udc->pdata;
1555 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1557 /* Clear bit in ENDPTSETUPSTAT */
1558 temp = fsl_readl(&dr_regs->endptsetupstat);
1559 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1561 /* while a hazard exists when setup package arrives */
1563 /* Set Setup Tripwire */
1564 temp = fsl_readl(&dr_regs->usbcmd);
1565 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1567 /* Copy the setup packet to local buffer */
1568 if (pdata->le_setup_buf) {
1569 u32 *p = (u32 *)buffer_ptr;
1570 u32 *s = (u32 *)qh->setup_buffer;
1572 /* Convert little endian setup buffer to CPU endian */
1573 *p++ = le32_to_cpu(*s++);
1574 *p = le32_to_cpu(*s);
1576 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1578 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1580 /* Clear Setup Tripwire */
1581 temp = fsl_readl(&dr_regs->usbcmd);
1582 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1585 /* process-ep_req(): free the completed Tds for this req */
1586 static int process_ep_req(struct fsl_udc *udc, int pipe,
1587 struct fsl_req *curr_req)
1589 struct ep_td_struct *curr_td;
1590 int td_complete, actual, remaining_length, j, tmp;
1593 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1594 int direction = pipe % 2;
1596 curr_td = curr_req->head;
1598 actual = curr_req->req.length;
1600 for (j = 0; j < curr_req->dtd_count; j++) {
1601 remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
1603 >> DTD_LENGTH_BIT_POS;
1604 actual -= remaining_length;
1606 errors = hc32_to_cpu(curr_td->size_ioc_sts);
1607 if (errors & DTD_ERROR_MASK) {
1608 if (errors & DTD_STATUS_HALTED) {
1609 ERR("dTD error %08x QH=%d\n", errors, pipe);
1610 /* Clear the errors and Halt condition */
1611 tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
1613 curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
1615 /* FIXME: continue with next queued TD? */
1619 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1620 VDBG("Transfer overflow");
1623 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1628 ERR("Unknown error has occurred (0x%x)!\n",
1631 } else if (hc32_to_cpu(curr_td->size_ioc_sts)
1632 & DTD_STATUS_ACTIVE) {
1633 VDBG("Request not complete");
1634 status = REQ_UNCOMPLETE;
1636 } else if (remaining_length) {
1638 VDBG("Transmit dTD remaining length not zero");
1647 VDBG("dTD transmitted successful");
1650 if (j != curr_req->dtd_count - 1)
1651 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1657 curr_req->req.actual = actual;
1662 /* Process a DTD completion interrupt */
1663 static void dtd_complete_irq(struct fsl_udc *udc)
1666 int i, ep_num, direction, bit_mask, status;
1667 struct fsl_ep *curr_ep;
1668 struct fsl_req *curr_req, *temp_req;
1670 /* Clear the bits in the register */
1671 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1672 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1677 for (i = 0; i < udc->max_ep * 2; i++) {
1681 bit_mask = 1 << (ep_num + 16 * direction);
1683 if (!(bit_pos & bit_mask))
1686 curr_ep = get_ep_by_pipe(udc, i);
1688 /* If the ep is configured */
1689 if (curr_ep->name == NULL) {
1690 WARNING("Invalid EP?");
1694 /* process the req queue until an uncomplete request */
1695 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1697 status = process_ep_req(udc, i, curr_req);
1699 VDBG("status of process_ep_req= %d, ep = %d",
1701 if (status == REQ_UNCOMPLETE)
1703 /* write back status to req */
1704 curr_req->req.status = status;
1707 ep0_req_complete(udc, curr_ep, curr_req);
1710 done(curr_ep, curr_req, status);
1715 static inline enum usb_device_speed portscx_device_speed(u32 reg)
1717 switch (reg & PORTSCX_PORT_SPEED_MASK) {
1718 case PORTSCX_PORT_SPEED_HIGH:
1719 return USB_SPEED_HIGH;
1720 case PORTSCX_PORT_SPEED_FULL:
1721 return USB_SPEED_FULL;
1722 case PORTSCX_PORT_SPEED_LOW:
1723 return USB_SPEED_LOW;
1725 return USB_SPEED_UNKNOWN;
1729 /* Process a port change interrupt */
1730 static void port_change_irq(struct fsl_udc *udc)
1735 /* Bus resetting is finished */
1736 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
1739 portscx_device_speed(fsl_readl(&dr_regs->portsc1));
1741 /* Update USB state */
1742 if (!udc->resume_state)
1743 udc->usb_state = USB_STATE_DEFAULT;
1746 /* Process suspend interrupt */
1747 static void suspend_irq(struct fsl_udc *udc)
1749 udc->resume_state = udc->usb_state;
1750 udc->usb_state = USB_STATE_SUSPENDED;
1752 /* report suspend to the driver, serial.c does not support this */
1753 if (udc->driver->suspend)
1754 udc->driver->suspend(&udc->gadget);
1757 static void bus_resume(struct fsl_udc *udc)
1759 udc->usb_state = udc->resume_state;
1760 udc->resume_state = 0;
1762 /* report resume to the driver, serial.c does not support this */
1763 if (udc->driver->resume)
1764 udc->driver->resume(&udc->gadget);
1767 /* Clear up all ep queues */
1768 static int reset_queues(struct fsl_udc *udc)
1772 for (pipe = 0; pipe < udc->max_pipes; pipe++)
1773 udc_reset_ep_queue(udc, pipe);
1775 /* report disconnect; the driver is already quiesced */
1776 spin_unlock(&udc->lock);
1777 udc->driver->disconnect(&udc->gadget);
1778 spin_lock(&udc->lock);
1783 /* Process reset interrupt */
1784 static void reset_irq(struct fsl_udc *udc)
1787 unsigned long timeout;
1789 /* Clear the device address */
1790 temp = fsl_readl(&dr_regs->deviceaddr);
1791 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
1793 udc->device_address = 0;
1795 /* Clear usb state */
1796 udc->resume_state = 0;
1798 udc->ep0_state = WAIT_FOR_SETUP;
1799 udc->remote_wakeup = 0; /* default to 0 on reset */
1800 udc->gadget.b_hnp_enable = 0;
1801 udc->gadget.a_hnp_support = 0;
1802 udc->gadget.a_alt_hnp_support = 0;
1804 /* Clear all the setup token semaphores */
1805 temp = fsl_readl(&dr_regs->endptsetupstat);
1806 fsl_writel(temp, &dr_regs->endptsetupstat);
1808 /* Clear all the endpoint complete status bits */
1809 temp = fsl_readl(&dr_regs->endptcomplete);
1810 fsl_writel(temp, &dr_regs->endptcomplete);
1812 timeout = jiffies + 100;
1813 while (fsl_readl(&dr_regs->endpointprime)) {
1814 /* Wait until all endptprime bits cleared */
1815 if (time_after(jiffies, timeout)) {
1816 ERR("Timeout for reset\n");
1822 /* Write 1s to the flush register */
1823 fsl_writel(0xffffffff, &dr_regs->endptflush);
1825 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1827 /* Bus is reseting */
1829 /* Reset all the queues, include XD, dTD, EP queue
1830 * head and TR Queue */
1832 udc->usb_state = USB_STATE_DEFAULT;
1834 VDBG("Controller reset");
1835 /* initialize usb hw reg except for regs for EP, not
1836 * touch usbintr reg */
1837 dr_controller_setup(udc);
1839 /* Reset all internal used Queues */
1844 /* Enable DR IRQ reg, Set Run bit, change udc state */
1845 dr_controller_run(udc);
1846 udc->usb_state = USB_STATE_ATTACHED;
1851 * USB device controller interrupt handler
1853 static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1855 struct fsl_udc *udc = _udc;
1857 irqreturn_t status = IRQ_NONE;
1858 unsigned long flags;
1860 /* Disable ISR for OTG host mode */
1863 spin_lock_irqsave(&udc->lock, flags);
1864 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
1865 /* Clear notification bits */
1866 fsl_writel(irq_src, &dr_regs->usbsts);
1868 /* VDBG("irq_src [0x%8x]", irq_src); */
1870 /* Need to resume? */
1871 if (udc->usb_state == USB_STATE_SUSPENDED)
1872 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
1876 if (irq_src & USB_STS_INT) {
1878 /* Setup package, we only support ep0 as control ep */
1879 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
1880 tripwire_handler(udc, 0,
1881 (u8 *) (&udc->local_setup_buff));
1882 setup_received_irq(udc, &udc->local_setup_buff);
1883 status = IRQ_HANDLED;
1886 /* completion of dtd */
1887 if (fsl_readl(&dr_regs->endptcomplete)) {
1888 dtd_complete_irq(udc);
1889 status = IRQ_HANDLED;
1893 /* SOF (for ISO transfer) */
1894 if (irq_src & USB_STS_SOF) {
1895 status = IRQ_HANDLED;
1899 if (irq_src & USB_STS_PORT_CHANGE) {
1900 port_change_irq(udc);
1901 status = IRQ_HANDLED;
1904 /* Reset Received */
1905 if (irq_src & USB_STS_RESET) {
1908 status = IRQ_HANDLED;
1911 /* Sleep Enable (Suspend) */
1912 if (irq_src & USB_STS_SUSPEND) {
1914 status = IRQ_HANDLED;
1917 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
1918 VDBG("Error IRQ %x", irq_src);
1921 spin_unlock_irqrestore(&udc->lock, flags);
1925 /*----------------------------------------------------------------*
1926 * Hook to gadget drivers
1927 * Called by initialization code of gadget drivers
1928 *----------------------------------------------------------------*/
1929 static int fsl_start(struct usb_gadget_driver *driver,
1930 int (*bind)(struct usb_gadget *))
1932 int retval = -ENODEV;
1933 unsigned long flags = 0;
1935 if (!udc_controller)
1938 if (!driver || driver->max_speed < USB_SPEED_FULL
1939 || !bind || !driver->disconnect || !driver->setup)
1942 if (udc_controller->driver)
1945 /* lock is needed but whether should use this lock or another */
1946 spin_lock_irqsave(&udc_controller->lock, flags);
1948 driver->driver.bus = NULL;
1949 /* hook up the driver */
1950 udc_controller->driver = driver;
1951 udc_controller->gadget.dev.driver = &driver->driver;
1952 spin_unlock_irqrestore(&udc_controller->lock, flags);
1954 /* bind udc driver to gadget driver */
1955 retval = bind(&udc_controller->gadget);
1957 VDBG("bind to %s --> %d", driver->driver.name, retval);
1958 udc_controller->gadget.dev.driver = NULL;
1959 udc_controller->driver = NULL;
1963 if (udc_controller->transceiver) {
1964 /* Suspend the controller until OTG enable it */
1965 udc_controller->stopped = 1;
1966 printk(KERN_INFO "Suspend udc for OTG auto detect\n");
1968 /* connect to bus through transceiver */
1969 if (udc_controller->transceiver) {
1970 retval = otg_set_peripheral(udc_controller->transceiver,
1971 &udc_controller->gadget);
1973 ERR("can't bind to transceiver\n");
1974 driver->unbind(&udc_controller->gadget);
1975 udc_controller->gadget.dev.driver = 0;
1976 udc_controller->driver = 0;
1981 /* Enable DR IRQ reg and set USBCMD reg Run bit */
1982 dr_controller_run(udc_controller);
1983 udc_controller->usb_state = USB_STATE_ATTACHED;
1984 udc_controller->ep0_state = WAIT_FOR_SETUP;
1985 udc_controller->ep0_dir = 0;
1987 printk(KERN_INFO "%s: bind to driver %s\n",
1988 udc_controller->gadget.name, driver->driver.name);
1992 printk(KERN_WARNING "gadget driver register failed %d\n",
1997 /* Disconnect from gadget driver */
1998 static int fsl_stop(struct usb_gadget_driver *driver)
2000 struct fsl_ep *loop_ep;
2001 unsigned long flags;
2003 if (!udc_controller)
2006 if (!driver || driver != udc_controller->driver || !driver->unbind)
2009 if (udc_controller->transceiver)
2010 otg_set_peripheral(udc_controller->transceiver, NULL);
2012 /* stop DR, disable intr */
2013 dr_controller_stop(udc_controller);
2015 /* in fact, no needed */
2016 udc_controller->usb_state = USB_STATE_ATTACHED;
2017 udc_controller->ep0_state = WAIT_FOR_SETUP;
2018 udc_controller->ep0_dir = 0;
2020 /* stand operation */
2021 spin_lock_irqsave(&udc_controller->lock, flags);
2022 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2023 nuke(&udc_controller->eps[0], -ESHUTDOWN);
2024 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
2026 nuke(loop_ep, -ESHUTDOWN);
2027 spin_unlock_irqrestore(&udc_controller->lock, flags);
2029 /* report disconnect; the controller is already quiesced */
2030 driver->disconnect(&udc_controller->gadget);
2032 /* unbind gadget and unhook driver. */
2033 driver->unbind(&udc_controller->gadget);
2034 udc_controller->gadget.dev.driver = NULL;
2035 udc_controller->driver = NULL;
2037 printk(KERN_WARNING "unregistered gadget driver '%s'\n",
2038 driver->driver.name);
2042 /*-------------------------------------------------------------------------
2043 PROC File System Support
2044 -------------------------------------------------------------------------*/
2045 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2047 #include <linux/seq_file.h>
2049 static const char proc_filename[] = "driver/fsl_usb2_udc";
2051 static int fsl_proc_read(char *page, char **start, off_t off, int count,
2052 int *eof, void *_dev)
2056 unsigned size = count;
2057 unsigned long flags;
2060 struct fsl_ep *ep = NULL;
2061 struct fsl_req *req;
2063 struct fsl_udc *udc = udc_controller;
2067 spin_lock_irqsave(&udc->lock, flags);
2069 /* ------basic driver information ---- */
2070 t = scnprintf(next, size,
2073 "Gadget driver: %s\n\n",
2074 driver_name, DRIVER_VERSION,
2075 udc->driver ? udc->driver->driver.name : "(none)");
2079 /* ------ DR Registers ----- */
2080 tmp_reg = fsl_readl(&dr_regs->usbcmd);
2081 t = scnprintf(next, size,
2085 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
2086 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
2090 tmp_reg = fsl_readl(&dr_regs->usbsts);
2091 t = scnprintf(next, size,
2093 "Dr Suspend: %d Reset Received: %d System Error: %s "
2094 "USB Error Interrupt: %s\n\n",
2095 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
2096 (tmp_reg & USB_STS_RESET) ? 1 : 0,
2097 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
2098 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
2102 tmp_reg = fsl_readl(&dr_regs->usbintr);
2103 t = scnprintf(next, size,
2104 "USB Intrrupt Enable Reg:\n"
2105 "Sleep Enable: %d SOF Received Enable: %d "
2106 "Reset Enable: %d\n"
2107 "System Error Enable: %d "
2108 "Port Change Dectected Enable: %d\n"
2109 "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
2110 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
2111 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
2112 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
2113 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
2114 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
2115 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
2116 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
2120 tmp_reg = fsl_readl(&dr_regs->frindex);
2121 t = scnprintf(next, size,
2122 "USB Frame Index Reg: Frame Number is 0x%x\n\n",
2123 (tmp_reg & USB_FRINDEX_MASKS));
2127 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
2128 t = scnprintf(next, size,
2129 "USB Device Address Reg: Device Addr is 0x%x\n\n",
2130 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
2134 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
2135 t = scnprintf(next, size,
2136 "USB Endpoint List Address Reg: "
2137 "Device Addr is 0x%x\n\n",
2138 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
2142 tmp_reg = fsl_readl(&dr_regs->portsc1);
2143 t = scnprintf(next, size,
2144 "USB Port Status&Control Reg:\n"
2145 "Port Transceiver Type : %s Port Speed: %s\n"
2146 "PHY Low Power Suspend: %s Port Reset: %s "
2147 "Port Suspend Mode: %s\n"
2148 "Over-current Change: %s "
2149 "Port Enable/Disable Change: %s\n"
2150 "Port Enabled/Disabled: %s "
2151 "Current Connect Status: %s\n\n", ( {
2153 switch (tmp_reg & PORTSCX_PTS_FSLS) {
2154 case PORTSCX_PTS_UTMI:
2156 case PORTSCX_PTS_ULPI:
2158 case PORTSCX_PTS_FSLS:
2159 s = "FS/LS Serial"; break;
2164 usb_speed_string(portscx_device_speed(tmp_reg)),
2165 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
2166 "Normal PHY mode" : "Low power mode",
2167 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
2169 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
2170 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
2172 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
2174 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
2176 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
2177 "Attached" : "Not-Att");
2181 tmp_reg = fsl_readl(&dr_regs->usbmode);
2182 t = scnprintf(next, size,
2183 "USB Mode Reg: Controller Mode is: %s\n\n", ( {
2185 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2186 case USB_MODE_CTRL_MODE_IDLE:
2188 case USB_MODE_CTRL_MODE_DEVICE:
2189 s = "Device Controller"; break;
2190 case USB_MODE_CTRL_MODE_HOST:
2191 s = "Host Controller"; break;
2200 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2201 t = scnprintf(next, size,
2202 "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
2203 (tmp_reg & EP_SETUP_STATUS_MASK));
2207 for (i = 0; i < udc->max_ep / 2; i++) {
2208 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2209 t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
2214 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2215 t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
2219 #ifndef CONFIG_ARCH_MXC
2220 if (udc->pdata->have_sysif_regs) {
2221 tmp_reg = usb_sys_regs->snoop1;
2222 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2226 tmp_reg = usb_sys_regs->control;
2227 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
2234 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2236 t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
2237 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2241 if (list_empty(&ep->queue)) {
2242 t = scnprintf(next, size, "its req queue is empty\n\n");
2246 list_for_each_entry(req, &ep->queue, queue) {
2247 t = scnprintf(next, size,
2248 "req %p actual 0x%x length 0x%x buf %p\n",
2249 &req->req, req->req.actual,
2250 req->req.length, req->req.buf);
2255 /* other gadget->eplist ep */
2256 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2258 t = scnprintf(next, size,
2259 "\nFor %s Maxpkt is 0x%x "
2261 ep->ep.name, ep_maxpacket(ep),
2266 if (list_empty(&ep->queue)) {
2267 t = scnprintf(next, size,
2268 "its req queue is empty\n\n");
2272 list_for_each_entry(req, &ep->queue, queue) {
2273 t = scnprintf(next, size,
2274 "req %p actual 0x%x length "
2276 &req->req, req->req.actual,
2277 req->req.length, req->req.buf);
2280 } /* end for each_entry of ep req */
2281 } /* end for else */
2282 } /* end for if(ep->queue) */
2283 } /* end (ep->desc) */
2285 spin_unlock_irqrestore(&udc->lock, flags);
2288 return count - size;
2291 #define create_proc_file() create_proc_read_entry(proc_filename, \
2292 0, NULL, fsl_proc_read, NULL)
2294 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2296 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2298 #define create_proc_file() do {} while (0)
2299 #define remove_proc_file() do {} while (0)
2301 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2303 /*-------------------------------------------------------------------------*/
2305 /* Release udc structures */
2306 static void fsl_udc_release(struct device *dev)
2308 complete(udc_controller->done);
2309 dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
2310 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2311 kfree(udc_controller);
2314 /******************************************************************
2315 Internal structure setup functions
2316 *******************************************************************/
2317 /*------------------------------------------------------------------
2318 * init resource for globle controller
2319 * Return the udc handle on success or NULL on failure
2320 ------------------------------------------------------------------*/
2321 static int __init struct_udc_setup(struct fsl_udc *udc,
2322 struct platform_device *pdev)
2324 struct fsl_usb2_platform_data *pdata;
2327 pdata = pdev->dev.platform_data;
2328 udc->phy_mode = pdata->phy_mode;
2330 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2332 ERR("malloc fsl_ep failed\n");
2336 /* initialized QHs, take care of alignment */
2337 size = udc->max_ep * sizeof(struct ep_queue_head);
2338 if (size < QH_ALIGNMENT)
2339 size = QH_ALIGNMENT;
2340 else if ((size % QH_ALIGNMENT) != 0) {
2341 size += QH_ALIGNMENT + 1;
2342 size &= ~(QH_ALIGNMENT - 1);
2344 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2345 &udc->ep_qh_dma, GFP_KERNEL);
2347 ERR("malloc QHs for udc failed\n");
2352 udc->ep_qh_size = size;
2354 /* Initialize ep0 status request structure */
2355 /* FIXME: fsl_alloc_request() ignores ep argument */
2356 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2357 struct fsl_req, req);
2358 /* allocate a small amount of memory to get valid address */
2359 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2361 udc->resume_state = USB_STATE_NOTATTACHED;
2362 udc->usb_state = USB_STATE_POWERED;
2364 udc->remote_wakeup = 0; /* default to 0 on reset */
2369 /*----------------------------------------------------------------
2370 * Setup the fsl_ep struct for eps
2371 * Link fsl_ep->ep to gadget->ep_list
2372 * ep0out is not used so do nothing here
2373 * ep0in should be taken care
2374 *--------------------------------------------------------------*/
2375 static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2376 char *name, int link)
2378 struct fsl_ep *ep = &udc->eps[index];
2381 strcpy(ep->name, name);
2382 ep->ep.name = ep->name;
2384 ep->ep.ops = &fsl_ep_ops;
2387 /* for ep0: maxP defined in desc
2388 * for other eps, maxP is set by epautoconfig() called by gadget layer
2390 ep->ep.maxpacket = (unsigned short) ~0;
2392 /* the queue lists any req for this ep */
2393 INIT_LIST_HEAD(&ep->queue);
2395 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
2397 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2398 ep->gadget = &udc->gadget;
2399 ep->qh = &udc->ep_qh[index];
2404 /* Driver probe function
2405 * all intialization operations implemented here except enabling usb_intr reg
2406 * board setup should have been done in the platform code
2408 static int __init fsl_udc_probe(struct platform_device *pdev)
2410 struct fsl_usb2_platform_data *pdata;
2411 struct resource *res;
2416 if (strcmp(pdev->name, driver_name)) {
2417 VDBG("Wrong device");
2421 udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
2422 if (udc_controller == NULL) {
2423 ERR("malloc udc failed\n");
2427 pdata = pdev->dev.platform_data;
2428 udc_controller->pdata = pdata;
2429 spin_lock_init(&udc_controller->lock);
2430 udc_controller->stopped = 1;
2432 #ifdef CONFIG_USB_OTG
2433 if (pdata->operating_mode == FSL_USB2_DR_OTG) {
2434 udc_controller->transceiver = otg_get_transceiver();
2435 if (!udc_controller->transceiver) {
2436 ERR("Can't find OTG driver!\n");
2443 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2449 if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
2450 if (!request_mem_region(res->start, resource_size(res),
2452 ERR("request mem region for %s failed\n", pdev->name);
2458 dr_regs = ioremap(res->start, resource_size(res));
2461 goto err_release_mem_region;
2464 pdata->regs = (void *)dr_regs;
2467 * do platform specific init: check the clock, grab/config pins, etc.
2469 if (pdata->init && pdata->init(pdev)) {
2471 goto err_iounmap_noclk;
2474 /* Set accessors only after pdata->init() ! */
2475 fsl_set_accessors(pdata);
2477 #ifndef CONFIG_ARCH_MXC
2478 if (pdata->have_sysif_regs)
2479 usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
2482 /* Initialize USB clocks */
2483 ret = fsl_udc_clk_init(pdev);
2485 goto err_iounmap_noclk;
2487 /* Read Device Controller Capability Parameters register */
2488 dccparams = fsl_readl(&dr_regs->dccparams);
2489 if (!(dccparams & DCCPARAMS_DC)) {
2490 ERR("This SOC doesn't support device role\n");
2494 /* Get max device endpoints */
2495 /* DEN is bidirectional ep number, max_ep doubles the number */
2496 udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
2498 udc_controller->irq = platform_get_irq(pdev, 0);
2499 if (!udc_controller->irq) {
2504 ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
2505 driver_name, udc_controller);
2507 ERR("cannot request irq %d err %d\n",
2508 udc_controller->irq, ret);
2512 /* Initialize the udc structure including QH member and other member */
2513 if (struct_udc_setup(udc_controller, pdev)) {
2514 ERR("Can't initialize udc data structure\n");
2519 if (!udc_controller->transceiver) {
2520 /* initialize usb hw reg except for regs for EP,
2521 * leave usbintr reg untouched */
2522 dr_controller_setup(udc_controller);
2525 fsl_udc_clk_finalize(pdev);
2527 /* Setup gadget structure */
2528 udc_controller->gadget.ops = &fsl_gadget_ops;
2529 udc_controller->gadget.max_speed = USB_SPEED_HIGH;
2530 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2531 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2532 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2533 udc_controller->gadget.name = driver_name;
2535 /* Setup gadget.dev and register with kernel */
2536 dev_set_name(&udc_controller->gadget.dev, "gadget");
2537 udc_controller->gadget.dev.release = fsl_udc_release;
2538 udc_controller->gadget.dev.parent = &pdev->dev;
2539 ret = device_register(&udc_controller->gadget.dev);
2543 if (udc_controller->transceiver)
2544 udc_controller->gadget.is_otg = 1;
2546 /* setup QH and epctrl for ep0 */
2547 ep0_setup(udc_controller);
2549 /* setup udc->eps[] for ep0 */
2550 struct_ep_setup(udc_controller, 0, "ep0", 0);
2551 /* for ep0: the desc defined here;
2552 * for other eps, gadget layer called ep_enable with defined desc
2554 udc_controller->eps[0].desc = &fsl_ep0_desc;
2555 udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
2557 /* setup the udc->eps[] for non-control endpoints and link
2558 * to gadget.ep_list */
2559 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
2562 sprintf(name, "ep%dout", i);
2563 struct_ep_setup(udc_controller, i * 2, name, 1);
2564 sprintf(name, "ep%din", i);
2565 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2568 /* use dma_pool for TD management */
2569 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
2570 sizeof(struct ep_td_struct),
2571 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2572 if (udc_controller->td_pool == NULL) {
2574 goto err_unregister;
2577 ret = usb_add_gadget_udc(&pdev->dev, &udc_controller->gadget);
2585 dma_pool_destroy(udc_controller->td_pool);
2587 device_unregister(&udc_controller->gadget.dev);
2589 free_irq(udc_controller->irq, udc_controller);
2593 fsl_udc_clk_release();
2596 err_release_mem_region:
2597 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2598 release_mem_region(res->start, resource_size(res));
2600 kfree(udc_controller);
2601 udc_controller = NULL;
2605 /* Driver removal function
2606 * Free resources and finish pending transactions
2608 static int __exit fsl_udc_remove(struct platform_device *pdev)
2610 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2611 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
2613 DECLARE_COMPLETION(done);
2615 if (!udc_controller)
2618 usb_del_gadget_udc(&udc_controller->gadget);
2619 udc_controller->done = &done;
2621 fsl_udc_clk_release();
2623 /* DR has been stopped in usb_gadget_unregister_driver() */
2626 /* Free allocated memory */
2627 kfree(udc_controller->status_req->req.buf);
2628 kfree(udc_controller->status_req);
2629 kfree(udc_controller->eps);
2631 dma_pool_destroy(udc_controller->td_pool);
2632 free_irq(udc_controller->irq, udc_controller);
2634 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2635 release_mem_region(res->start, resource_size(res));
2637 device_unregister(&udc_controller->gadget.dev);
2638 /* free udc --wait for the release() finished */
2639 wait_for_completion(&done);
2642 * do platform specific un-initialization:
2643 * release iomux pins, etc.
2651 /*-----------------------------------------------------------------
2652 * Modify Power management attributes
2653 * Used by OTG statemachine to disable gadget temporarily
2654 -----------------------------------------------------------------*/
2655 static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
2657 dr_controller_stop(udc_controller);
2661 /*-----------------------------------------------------------------
2662 * Invoked on USB resume. May be called in_interrupt.
2663 * Here we start the DR controller and enable the irq
2664 *-----------------------------------------------------------------*/
2665 static int fsl_udc_resume(struct platform_device *pdev)
2667 /* Enable DR irq reg and set controller Run */
2668 if (udc_controller->stopped) {
2669 dr_controller_setup(udc_controller);
2670 dr_controller_run(udc_controller);
2672 udc_controller->usb_state = USB_STATE_ATTACHED;
2673 udc_controller->ep0_state = WAIT_FOR_SETUP;
2674 udc_controller->ep0_dir = 0;
2678 static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
2680 struct fsl_udc *udc = udc_controller;
2683 mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
2685 pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
2688 * If the controller is already stopped, then this must be a
2689 * PM suspend. Remember this fact, so that we will leave the
2690 * controller stopped at PM resume time.
2693 pr_debug("gadget already stopped, leaving early\n");
2694 udc->already_stopped = 1;
2698 if (mode != USB_MODE_CTRL_MODE_DEVICE) {
2699 pr_debug("gadget not in device mode, leaving early\n");
2703 /* stop the controller */
2704 usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
2705 fsl_writel(usbcmd, &dr_regs->usbcmd);
2709 pr_info("USB Gadget suspended\n");
2714 static int fsl_udc_otg_resume(struct device *dev)
2716 pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
2717 udc_controller->stopped, udc_controller->already_stopped);
2720 * If the controller was stopped at suspend time, then
2721 * don't resume it now.
2723 if (udc_controller->already_stopped) {
2724 udc_controller->already_stopped = 0;
2725 pr_debug("gadget was already stopped, leaving early\n");
2729 pr_info("USB Gadget resume\n");
2731 return fsl_udc_resume(NULL);
2734 /*-------------------------------------------------------------------------
2735 Register entry point for the peripheral controller driver
2736 --------------------------------------------------------------------------*/
2738 static struct platform_driver udc_driver = {
2739 .remove = __exit_p(fsl_udc_remove),
2740 /* these suspend and resume are not usb suspend and resume */
2741 .suspend = fsl_udc_suspend,
2742 .resume = fsl_udc_resume,
2744 .name = (char *)driver_name,
2745 .owner = THIS_MODULE,
2746 /* udc suspend/resume called from OTG driver */
2747 .suspend = fsl_udc_otg_suspend,
2748 .resume = fsl_udc_otg_resume,
2752 static int __init udc_init(void)
2754 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
2755 return platform_driver_probe(&udc_driver, fsl_udc_probe);
2758 module_init(udc_init);
2760 static void __exit udc_exit(void)
2762 platform_driver_unregister(&udc_driver);
2763 printk(KERN_WARNING "%s unregistered\n", driver_desc);
2766 module_exit(udc_exit);
2768 MODULE_DESCRIPTION(DRIVER_DESC);
2769 MODULE_AUTHOR(DRIVER_AUTHOR);
2770 MODULE_LICENSE("GPL");
2771 MODULE_ALIAS("platform:fsl-usb2-udc");