2 * Copyright (C) 2004-2007 Freescale Semicondutor, Inc. All rights reserved.
4 * Author: Li Yang <leoli@freescale.com>
5 * Jiang Bo <tanya.jiang@freescale.com>
8 * Freescale high-speed USB SOC DR module device controller driver.
9 * This can be found on MPC8349E/MPC8313E cpus.
10 * The driver is previously named as mpc_udc. Based on bare board
11 * code from Dave Liu and Shlomi Gridish.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/ioport.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/proc_fs.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>
42 #include <asm/byteorder.h>
44 #include <asm/system.h>
45 #include <asm/unaligned.h>
48 #include "fsl_usb2_udc.h"
50 #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
51 #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
52 #define DRIVER_VERSION "Apr 20, 2007"
54 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
56 static const char driver_name[] = "fsl-usb2-udc";
57 static const char driver_desc[] = DRIVER_DESC;
59 static struct usb_dr_device *dr_regs;
60 static struct usb_sys_interface *usb_sys_regs;
62 /* it is initialized in probe() */
63 static struct fsl_udc *udc_controller = NULL;
65 static const struct usb_endpoint_descriptor
67 .bLength = USB_DT_ENDPOINT_SIZE,
68 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = 0,
70 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
71 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
74 static void fsl_ep_fifo_flush(struct usb_ep *_ep);
77 #define fsl_readl(addr) in_le32(addr)
78 #define fsl_writel(val32, addr) out_le32(addr, val32)
80 #define fsl_readl(addr) readl(addr)
81 #define fsl_writel(val32, addr) writel(val32, addr)
84 /********************************************************************
85 * Internal Used Function
86 ********************************************************************/
87 /*-----------------------------------------------------------------
88 * done() - retire a request; caller blocked irqs
89 * @status : request status to be set, only works when
90 * request is still in progress.
91 *--------------------------------------------------------------*/
92 static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
94 struct fsl_udc *udc = NULL;
95 unsigned char stopped = ep->stopped;
96 struct ep_td_struct *curr_td, *next_td;
99 udc = (struct fsl_udc *)ep->udc;
100 /* Removed the req from fsl_ep->queue */
101 list_del_init(&req->queue);
103 /* req.status should be set as -EINPROGRESS in ep_queue() */
104 if (req->req.status == -EINPROGRESS)
105 req->req.status = status;
107 status = req->req.status;
109 /* Free dtd for the request */
111 for (j = 0; j < req->dtd_count; j++) {
113 if (j != req->dtd_count - 1) {
114 next_td = curr_td->next_td_virt;
116 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
120 dma_unmap_single(ep->udc->gadget.dev.parent,
121 req->req.dma, req->req.length,
125 req->req.dma = DMA_ADDR_INVALID;
128 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
129 req->req.dma, req->req.length,
134 if (status && (status != -ESHUTDOWN))
135 VDBG("complete %s req %p stat %d len %u/%u",
136 ep->ep.name, &req->req, status,
137 req->req.actual, req->req.length);
141 spin_unlock(&ep->udc->lock);
142 /* complete() is from gadget layer,
143 * eg fsg->bulk_in_complete() */
144 if (req->req.complete)
145 req->req.complete(&ep->ep, &req->req);
147 spin_lock(&ep->udc->lock);
148 ep->stopped = stopped;
151 /*-----------------------------------------------------------------
152 * nuke(): delete all requests related to this ep
153 * called with spinlock held
154 *--------------------------------------------------------------*/
155 static void nuke(struct fsl_ep *ep, int status)
160 fsl_ep_fifo_flush(&ep->ep);
162 /* Whether this eq has request linked */
163 while (!list_empty(&ep->queue)) {
164 struct fsl_req *req = NULL;
166 req = list_entry(ep->queue.next, struct fsl_req, queue);
167 done(ep, req, status);
171 /*------------------------------------------------------------------
172 Internal Hardware related function
173 ------------------------------------------------------------------*/
175 static int dr_controller_setup(struct fsl_udc *udc)
177 unsigned int tmp = 0, portctrl = 0, ctrl = 0;
178 unsigned long timeout;
179 #define FSL_UDC_RESET_TIMEOUT 1000
181 /* Stop and reset the usb controller */
182 tmp = fsl_readl(&dr_regs->usbcmd);
183 tmp &= ~USB_CMD_RUN_STOP;
184 fsl_writel(tmp, &dr_regs->usbcmd);
186 tmp = fsl_readl(&dr_regs->usbcmd);
187 tmp |= USB_CMD_CTRL_RESET;
188 fsl_writel(tmp, &dr_regs->usbcmd);
190 /* Wait for reset to complete */
191 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
192 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
193 if (time_after(jiffies, timeout)) {
194 ERR("udc reset timeout!\n");
200 /* Set the controller as device mode */
201 tmp = fsl_readl(&dr_regs->usbmode);
202 tmp |= USB_MODE_CTRL_MODE_DEVICE;
203 /* Disable Setup Lockout */
204 tmp |= USB_MODE_SETUP_LOCK_OFF;
205 fsl_writel(tmp, &dr_regs->usbmode);
207 /* Clear the setup status */
208 fsl_writel(0, &dr_regs->usbsts);
210 tmp = udc->ep_qh_dma;
211 tmp &= USB_EP_LIST_ADDRESS_MASK;
212 fsl_writel(tmp, &dr_regs->endpointlistaddr);
214 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
215 udc->ep_qh, (int)tmp,
216 fsl_readl(&dr_regs->endpointlistaddr));
218 /* Config PHY interface */
219 portctrl = fsl_readl(&dr_regs->portsc1);
220 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
221 switch (udc->phy_mode) {
222 case FSL_USB2_PHY_ULPI:
223 portctrl |= PORTSCX_PTS_ULPI;
225 case FSL_USB2_PHY_UTMI_WIDE:
226 portctrl |= PORTSCX_PTW_16BIT;
228 case FSL_USB2_PHY_UTMI:
229 portctrl |= PORTSCX_PTS_UTMI;
231 case FSL_USB2_PHY_SERIAL:
232 portctrl |= PORTSCX_PTS_FSLS;
237 fsl_writel(portctrl, &dr_regs->portsc1);
239 /* Config control enable i/o output, cpu endian register */
240 ctrl = __raw_readl(&usb_sys_regs->control);
241 ctrl |= USB_CTRL_IOENB;
242 __raw_writel(ctrl, &usb_sys_regs->control);
244 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
245 /* Turn on cache snooping hardware, since some PowerPC platforms
246 * wholly rely on hardware to deal with cache coherent. */
248 /* Setup Snooping for all the 4GB space */
249 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
250 __raw_writel(tmp, &usb_sys_regs->snoop1);
251 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
252 __raw_writel(tmp, &usb_sys_regs->snoop2);
258 /* Enable DR irq and set controller to run state */
259 static void dr_controller_run(struct fsl_udc *udc)
263 /* Enable DR irq reg */
264 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
265 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
266 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
268 fsl_writel(temp, &dr_regs->usbintr);
270 /* Clear stopped bit */
273 /* Set the controller as device mode */
274 temp = fsl_readl(&dr_regs->usbmode);
275 temp |= USB_MODE_CTRL_MODE_DEVICE;
276 fsl_writel(temp, &dr_regs->usbmode);
278 /* Set controller to Run */
279 temp = fsl_readl(&dr_regs->usbcmd);
280 temp |= USB_CMD_RUN_STOP;
281 fsl_writel(temp, &dr_regs->usbcmd);
286 static void dr_controller_stop(struct fsl_udc *udc)
290 /* disable all INTR */
291 fsl_writel(0, &dr_regs->usbintr);
293 /* Set stopped bit for isr */
296 /* disable IO output */
297 /* usb_sys_regs->control = 0; */
299 /* set controller to Stop */
300 tmp = fsl_readl(&dr_regs->usbcmd);
301 tmp &= ~USB_CMD_RUN_STOP;
302 fsl_writel(tmp, &dr_regs->usbcmd);
307 static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
308 unsigned char ep_type)
310 unsigned int tmp_epctrl = 0;
312 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
315 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
316 tmp_epctrl |= EPCTRL_TX_ENABLE;
317 tmp_epctrl |= ((unsigned int)(ep_type)
318 << EPCTRL_TX_EP_TYPE_SHIFT);
321 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
322 tmp_epctrl |= EPCTRL_RX_ENABLE;
323 tmp_epctrl |= ((unsigned int)(ep_type)
324 << EPCTRL_RX_EP_TYPE_SHIFT);
327 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
331 dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
335 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
338 /* set the stall bit */
340 tmp_epctrl |= EPCTRL_TX_EP_STALL;
342 tmp_epctrl |= EPCTRL_RX_EP_STALL;
344 /* clear the stall bit and reset data toggle */
346 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
347 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
349 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
350 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
353 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
356 /* Get stall status of a specific ep
357 Return: 0: not stalled; 1:stalled */
358 static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
362 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
364 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
366 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
369 /********************************************************************
370 Internal Structure Build up functions
371 ********************************************************************/
373 /*------------------------------------------------------------------
374 * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
375 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
377 ------------------------------------------------------------------*/
378 static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
379 unsigned char dir, unsigned char ep_type,
380 unsigned int max_pkt_len,
381 unsigned int zlt, unsigned char mult)
383 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
384 unsigned int tmp = 0;
386 /* set the Endpoint Capabilites in QH */
388 case USB_ENDPOINT_XFER_CONTROL:
389 /* Interrupt On Setup (IOS). for control ep */
390 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
393 case USB_ENDPOINT_XFER_ISOC:
394 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
395 | (mult << EP_QUEUE_HEAD_MULT_POS);
397 case USB_ENDPOINT_XFER_BULK:
398 case USB_ENDPOINT_XFER_INT:
399 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
402 VDBG("error ep type is %d", ep_type);
406 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
407 p_QH->max_pkt_length = cpu_to_le32(tmp);
412 /* Setup qh structure and ep register for ep0. */
413 static void ep0_setup(struct fsl_udc *udc)
415 /* the intialization of an ep includes: fields in QH, Regs,
417 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
418 USB_MAX_CTRL_PAYLOAD, 0, 0);
419 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
420 USB_MAX_CTRL_PAYLOAD, 0, 0);
421 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
422 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
428 /***********************************************************************
429 Endpoint Management Functions
430 ***********************************************************************/
432 /*-------------------------------------------------------------------------
433 * when configurations are set, or when interface settings change
434 * for example the do_set_interface() in gadget layer,
435 * the driver will enable or disable the relevant endpoints
436 * ep0 doesn't use this routine. It is always enabled.
437 -------------------------------------------------------------------------*/
438 static int fsl_ep_enable(struct usb_ep *_ep,
439 const struct usb_endpoint_descriptor *desc)
441 struct fsl_udc *udc = NULL;
442 struct fsl_ep *ep = NULL;
443 unsigned short max = 0;
444 unsigned char mult = 0, zlt;
445 int retval = -EINVAL;
446 unsigned long flags = 0;
448 ep = container_of(_ep, struct fsl_ep, ep);
450 /* catch various bogus parameters */
451 if (!_ep || !desc || ep->desc
452 || (desc->bDescriptorType != USB_DT_ENDPOINT))
457 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
460 max = le16_to_cpu(desc->wMaxPacketSize);
462 /* Disable automatic zlp generation. Driver is reponsible to indicate
463 * explicitly through req->req.zero. This is needed to enable multi-td
467 /* Assume the max packet size from gadget is always correct */
468 switch (desc->bmAttributes & 0x03) {
469 case USB_ENDPOINT_XFER_CONTROL:
470 case USB_ENDPOINT_XFER_BULK:
471 case USB_ENDPOINT_XFER_INT:
472 /* mult = 0. Execute N Transactions as demonstrated by
473 * the USB variable length packet protocol where N is
474 * computed using the Maximum Packet Length (dQH) and
475 * the Total Bytes field (dTD) */
478 case USB_ENDPOINT_XFER_ISOC:
479 /* Calculate transactions needed for high bandwidth iso */
480 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
481 max = max & 0x8ff; /* bit 0~10 */
482 /* 3 transactions at most */
490 spin_lock_irqsave(&udc->lock, flags);
491 ep->ep.maxpacket = max;
495 /* Controller related setup */
496 /* Init EPx Queue Head (Ep Capabilites field in QH
497 * according to max, zlt, mult) */
498 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
499 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
500 ? USB_SEND : USB_RECV),
501 (unsigned char) (desc->bmAttributes
502 & USB_ENDPOINT_XFERTYPE_MASK),
505 /* Init endpoint ctrl register */
506 dr_ep_setup((unsigned char) ep_index(ep),
507 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
508 ? USB_SEND : USB_RECV),
509 (unsigned char) (desc->bmAttributes
510 & USB_ENDPOINT_XFERTYPE_MASK));
512 spin_unlock_irqrestore(&udc->lock, flags);
515 VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
516 ep->desc->bEndpointAddress & 0x0f,
517 (desc->bEndpointAddress & USB_DIR_IN)
518 ? "in" : "out", max);
523 /*---------------------------------------------------------------------
524 * @ep : the ep being unconfigured. May not be ep0
525 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
526 *---------------------------------------------------------------------*/
527 static int fsl_ep_disable(struct usb_ep *_ep)
529 struct fsl_udc *udc = NULL;
530 struct fsl_ep *ep = NULL;
531 unsigned long flags = 0;
535 ep = container_of(_ep, struct fsl_ep, ep);
536 if (!_ep || !ep->desc) {
537 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
541 /* disable ep on controller */
542 ep_num = ep_index(ep);
543 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
545 epctrl &= ~EPCTRL_TX_ENABLE;
547 epctrl &= ~EPCTRL_RX_ENABLE;
548 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
550 udc = (struct fsl_udc *)ep->udc;
551 spin_lock_irqsave(&udc->lock, flags);
553 /* nuke all pending requests (does flush) */
554 nuke(ep, -ESHUTDOWN);
558 spin_unlock_irqrestore(&udc->lock, flags);
560 VDBG("disabled %s OK", _ep->name);
564 /*---------------------------------------------------------------------
565 * allocate a request object used by this endpoint
566 * the main operation is to insert the req->queue to the eq->queue
567 * Returns the request, or null if one could not be allocated
568 *---------------------------------------------------------------------*/
569 static struct usb_request *
570 fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
572 struct fsl_req *req = NULL;
574 req = kzalloc(sizeof *req, gfp_flags);
578 req->req.dma = DMA_ADDR_INVALID;
579 INIT_LIST_HEAD(&req->queue);
584 static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
586 struct fsl_req *req = NULL;
588 req = container_of(_req, struct fsl_req, req);
594 /*-------------------------------------------------------------------------*/
595 static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
597 int i = ep_index(ep) * 2 + ep_is_in(ep);
598 u32 temp, bitmask, tmp_stat;
599 struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
601 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
602 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
604 bitmask = ep_is_in(ep)
605 ? (1 << (ep_index(ep) + 16))
606 : (1 << (ep_index(ep)));
608 /* check if the pipe is empty */
609 if (!(list_empty(&ep->queue))) {
610 /* Add td to the end */
611 struct fsl_req *lastreq;
612 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
613 lastreq->tail->next_td_ptr =
614 cpu_to_le32(req->head->td_dma & DTD_ADDR_MASK);
615 /* Read prime bit, if 1 goto done */
616 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
620 /* Set ATDTW bit in USBCMD */
621 temp = fsl_readl(&dr_regs->usbcmd);
622 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
624 /* Read correct status bit */
625 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
627 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
629 /* Write ATDTW bit to 0 */
630 temp = fsl_readl(&dr_regs->usbcmd);
631 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
637 /* Write dQH next pointer and terminate bit to 0 */
638 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
639 dQH->next_dtd_ptr = cpu_to_le32(temp);
641 /* Clear active and halt bit */
642 temp = cpu_to_le32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
643 | EP_QUEUE_HEAD_STATUS_HALT));
644 dQH->size_ioc_int_sts &= temp;
646 /* Prime endpoint by writing 1 to ENDPTPRIME */
648 ? (1 << (ep_index(ep) + 16))
649 : (1 << (ep_index(ep)));
650 fsl_writel(temp, &dr_regs->endpointprime);
655 /* Fill in the dTD structure
656 * @req: request that the transfer belongs to
657 * @length: return actually data length of the dTD
658 * @dma: return dma address of the dTD
659 * @is_last: return flag if it is the last dTD of the request
660 * return: pointer to the built dTD */
661 static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
662 dma_addr_t *dma, int *is_last)
665 struct ep_td_struct *dtd;
667 /* how big will this transfer be? */
668 *length = min(req->req.length - req->req.actual,
669 (unsigned)EP_MAX_LENGTH_TRANSFER);
671 dtd = dma_pool_alloc(udc_controller->td_pool, GFP_KERNEL, dma);
676 /* Clear reserved field */
677 swap_temp = cpu_to_le32(dtd->size_ioc_sts);
678 swap_temp &= ~DTD_RESERVED_FIELDS;
679 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
681 /* Init all of buffer page pointers */
682 swap_temp = (u32) (req->req.dma + req->req.actual);
683 dtd->buff_ptr0 = cpu_to_le32(swap_temp);
684 dtd->buff_ptr1 = cpu_to_le32(swap_temp + 0x1000);
685 dtd->buff_ptr2 = cpu_to_le32(swap_temp + 0x2000);
686 dtd->buff_ptr3 = cpu_to_le32(swap_temp + 0x3000);
687 dtd->buff_ptr4 = cpu_to_le32(swap_temp + 0x4000);
689 req->req.actual += *length;
691 /* zlp is needed if req->req.zero is set */
693 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
697 } else if (req->req.length == req->req.actual)
703 VDBG("multi-dtd request!");
704 /* Fill in the transfer size; set active bit */
705 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
707 /* Enable interrupt for the last dtd of a request */
708 if (*is_last && !req->req.no_interrupt)
709 swap_temp |= DTD_IOC;
711 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
715 VDBG("length = %d address= 0x%x", *length, (int)*dma);
720 /* Generate dtd chain for a request */
721 static int fsl_req_to_dtd(struct fsl_req *req)
726 struct ep_td_struct *last_dtd = NULL, *dtd;
730 dtd = fsl_build_dtd(req, &count, &dma, &is_last);
738 last_dtd->next_td_ptr = cpu_to_le32(dma);
739 last_dtd->next_td_virt = dtd;
746 dtd->next_td_ptr = cpu_to_le32(DTD_NEXT_TERMINATE);
753 /* queues (submits) an I/O request to an endpoint */
755 fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
757 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
758 struct fsl_req *req = container_of(_req, struct fsl_req, req);
763 /* catch various bogus parameters */
764 if (!_req || !req->req.complete || !req->req.buf
765 || !list_empty(&req->queue)) {
766 VDBG("%s, bad params", __func__);
769 if (unlikely(!_ep || !ep->desc)) {
770 VDBG("%s, bad ep", __func__);
773 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
774 if (req->req.length > ep->ep.maxpacket)
780 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
785 /* map virtual address to hardware */
786 if (req->req.dma == DMA_ADDR_INVALID) {
787 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
789 req->req.length, ep_is_in(ep)
794 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
795 req->req.dma, req->req.length,
802 req->req.status = -EINPROGRESS;
806 spin_lock_irqsave(&udc->lock, flags);
808 /* build dtds and push them to device queue */
809 if (!fsl_req_to_dtd(req)) {
810 fsl_queue_td(ep, req);
812 spin_unlock_irqrestore(&udc->lock, flags);
816 /* Update ep0 state */
817 if ((ep_index(ep) == 0))
818 udc->ep0_state = DATA_STATE_XMIT;
820 /* irq handler advances the queue */
822 list_add_tail(&req->queue, &ep->queue);
823 spin_unlock_irqrestore(&udc->lock, flags);
828 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
829 static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
831 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
834 int ep_num, stopped, ret = 0;
840 spin_lock_irqsave(&ep->udc->lock, flags);
841 stopped = ep->stopped;
843 /* Stop the ep before we deal with the queue */
845 ep_num = ep_index(ep);
846 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
848 epctrl &= ~EPCTRL_TX_ENABLE;
850 epctrl &= ~EPCTRL_RX_ENABLE;
851 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
853 /* make sure it's actually queued on this endpoint */
854 list_for_each_entry(req, &ep->queue, queue) {
855 if (&req->req == _req)
858 if (&req->req != _req) {
863 /* The request is in progress, or completed but not dequeued */
864 if (ep->queue.next == &req->queue) {
865 _req->status = -ECONNRESET;
866 fsl_ep_fifo_flush(_ep); /* flush current transfer */
868 /* The request isn't the last request in this ep queue */
869 if (req->queue.next != &ep->queue) {
870 struct ep_queue_head *qh;
871 struct fsl_req *next_req;
874 next_req = list_entry(req->queue.next, struct fsl_req,
877 /* Point the QH to the first TD of next request */
878 fsl_writel((u32) next_req->head, &qh->curr_dtd_ptr);
881 /* The request hasn't been processed, patch up the TD chain */
883 struct fsl_req *prev_req;
885 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
886 fsl_writel(fsl_readl(&req->tail->next_td_ptr),
887 &prev_req->tail->next_td_ptr);
891 done(ep, req, -ECONNRESET);
894 out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
896 epctrl |= EPCTRL_TX_ENABLE;
898 epctrl |= EPCTRL_RX_ENABLE;
899 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
900 ep->stopped = stopped;
902 spin_unlock_irqrestore(&ep->udc->lock, flags);
906 /*-------------------------------------------------------------------------*/
908 /*-----------------------------------------------------------------
909 * modify the endpoint halt feature
910 * @ep: the non-isochronous endpoint being stalled
911 * @value: 1--set halt 0--clear halt
912 * Returns zero, or a negative error code.
913 *----------------------------------------------------------------*/
914 static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
916 struct fsl_ep *ep = NULL;
917 unsigned long flags = 0;
918 int status = -EOPNOTSUPP; /* operation not supported */
919 unsigned char ep_dir = 0, ep_num = 0;
920 struct fsl_udc *udc = NULL;
922 ep = container_of(_ep, struct fsl_ep, ep);
924 if (!_ep || !ep->desc) {
929 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
930 status = -EOPNOTSUPP;
934 /* Attempt to halt IN ep will fail if any transfer requests
936 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
942 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
943 ep_num = (unsigned char)(ep_index(ep));
944 spin_lock_irqsave(&ep->udc->lock, flags);
945 dr_ep_change_stall(ep_num, ep_dir, value);
946 spin_unlock_irqrestore(&ep->udc->lock, flags);
948 if (ep_index(ep) == 0) {
949 udc->ep0_state = WAIT_FOR_SETUP;
953 VDBG(" %s %s halt stat %d", ep->ep.name,
954 value ? "set" : "clear", status);
959 static void fsl_ep_fifo_flush(struct usb_ep *_ep)
964 unsigned long timeout;
965 #define FSL_UDC_FLUSH_TIMEOUT 1000
970 ep = container_of(_ep, struct fsl_ep, ep);
974 ep_num = ep_index(ep);
975 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
978 bits = (1 << 16) | 1;
979 else if (ep_dir == USB_SEND)
980 bits = 1 << (16 + ep_num);
984 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
986 fsl_writel(bits, &dr_regs->endptflush);
988 /* Wait until flush complete */
989 while (fsl_readl(&dr_regs->endptflush)) {
990 if (time_after(jiffies, timeout)) {
991 ERR("ep flush timeout\n");
996 /* See if we need to flush again */
997 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1000 static struct usb_ep_ops fsl_ep_ops = {
1001 .enable = fsl_ep_enable,
1002 .disable = fsl_ep_disable,
1004 .alloc_request = fsl_alloc_request,
1005 .free_request = fsl_free_request,
1007 .queue = fsl_ep_queue,
1008 .dequeue = fsl_ep_dequeue,
1010 .set_halt = fsl_ep_set_halt,
1011 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1014 /*-------------------------------------------------------------------------
1015 Gadget Driver Layer Operations
1016 -------------------------------------------------------------------------*/
1018 /*----------------------------------------------------------------------
1019 * Get the current frame number (from DR frame_index Reg )
1020 *----------------------------------------------------------------------*/
1021 static int fsl_get_frame(struct usb_gadget *gadget)
1023 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1026 /*-----------------------------------------------------------------------
1027 * Tries to wake up the host connected to this gadget
1028 -----------------------------------------------------------------------*/
1029 static int fsl_wakeup(struct usb_gadget *gadget)
1031 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1034 /* Remote wakeup feature not enabled by host */
1035 if (!udc->remote_wakeup)
1038 portsc = fsl_readl(&dr_regs->portsc1);
1039 /* not suspended? */
1040 if (!(portsc & PORTSCX_PORT_SUSPEND))
1042 /* trigger force resume */
1043 portsc |= PORTSCX_PORT_FORCE_RESUME;
1044 fsl_writel(portsc, &dr_regs->portsc1);
1048 static int can_pullup(struct fsl_udc *udc)
1050 return udc->driver && udc->softconnect && udc->vbus_active;
1053 /* Notify controller that VBUS is powered, Called by whatever
1054 detects VBUS sessions */
1055 static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1057 struct fsl_udc *udc;
1058 unsigned long flags;
1060 udc = container_of(gadget, struct fsl_udc, gadget);
1061 spin_lock_irqsave(&udc->lock, flags);
1062 VDBG("VBUS %s", is_active ? "on" : "off");
1063 udc->vbus_active = (is_active != 0);
1064 if (can_pullup(udc))
1065 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1068 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1070 spin_unlock_irqrestore(&udc->lock, flags);
1074 /* constrain controller's VBUS power usage
1075 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1076 * reporting how much power the device may consume. For example, this
1077 * could affect how quickly batteries are recharged.
1079 * Returns zero on success, else negative errno.
1081 static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1083 struct fsl_udc *udc;
1085 udc = container_of(gadget, struct fsl_udc, gadget);
1086 if (udc->transceiver)
1087 return otg_set_power(udc->transceiver, mA);
1091 /* Change Data+ pullup status
1092 * this func is used by usb_gadget_connect/disconnet
1094 static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1096 struct fsl_udc *udc;
1098 udc = container_of(gadget, struct fsl_udc, gadget);
1099 udc->softconnect = (is_on != 0);
1100 if (can_pullup(udc))
1101 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1104 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1110 /* defined in gadget.h */
1111 static struct usb_gadget_ops fsl_gadget_ops = {
1112 .get_frame = fsl_get_frame,
1113 .wakeup = fsl_wakeup,
1114 /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1115 .vbus_session = fsl_vbus_session,
1116 .vbus_draw = fsl_vbus_draw,
1117 .pullup = fsl_pullup,
1120 /* Set protocol stall on ep0, protocol stall will automatically be cleared
1121 on new transaction */
1122 static void ep0stall(struct fsl_udc *udc)
1126 /* must set tx and rx to stall at the same time */
1127 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1128 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1129 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1130 udc->ep0_state = WAIT_FOR_SETUP;
1134 /* Prime a status phase for ep0 */
1135 static int ep0_prime_status(struct fsl_udc *udc, int direction)
1137 struct fsl_req *req = udc->status_req;
1140 if (direction == EP_DIR_IN)
1141 udc->ep0_dir = USB_DIR_IN;
1143 udc->ep0_dir = USB_DIR_OUT;
1146 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1149 req->req.length = 0;
1150 req->req.status = -EINPROGRESS;
1151 req->req.actual = 0;
1152 req->req.complete = NULL;
1155 if (fsl_req_to_dtd(req) == 0)
1156 fsl_queue_td(ep, req);
1160 list_add_tail(&req->queue, &ep->queue);
1165 static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1167 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1170 nuke(ep, -ESHUTDOWN);
1176 static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1178 /* Save the new address to device struct */
1179 udc->device_address = (u8) value;
1180 /* Update usb state */
1181 udc->usb_state = USB_STATE_ADDRESS;
1183 if (ep0_prime_status(udc, EP_DIR_IN))
1190 static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1191 u16 index, u16 length)
1193 u16 tmp = 0; /* Status, cpu endian */
1194 struct fsl_req *req;
1199 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1200 /* Get device status */
1201 tmp = 1 << USB_DEVICE_SELF_POWERED;
1202 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1203 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1204 /* Get interface status */
1205 /* We don't have interface information in udc driver */
1207 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1208 /* Get endpoint status */
1209 struct fsl_ep *target_ep;
1211 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1213 /* stall if endpoint doesn't exist */
1214 if (!target_ep->desc)
1216 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1217 << USB_ENDPOINT_HALT;
1220 udc->ep0_dir = USB_DIR_IN;
1221 /* Borrow the per device status_req */
1222 req = udc->status_req;
1223 /* Fill in the reqest structure */
1224 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1226 req->req.length = 2;
1227 req->req.status = -EINPROGRESS;
1228 req->req.actual = 0;
1229 req->req.complete = NULL;
1232 /* prime the data phase */
1233 if ((fsl_req_to_dtd(req) == 0))
1234 fsl_queue_td(ep, req);
1238 list_add_tail(&req->queue, &ep->queue);
1239 udc->ep0_state = DATA_STATE_XMIT;
1245 static void setup_received_irq(struct fsl_udc *udc,
1246 struct usb_ctrlrequest *setup)
1248 u16 wValue = le16_to_cpu(setup->wValue);
1249 u16 wIndex = le16_to_cpu(setup->wIndex);
1250 u16 wLength = le16_to_cpu(setup->wLength);
1252 udc_reset_ep_queue(udc, 0);
1254 /* We process some stardard setup requests here */
1255 switch (setup->bRequest) {
1256 case USB_REQ_GET_STATUS:
1257 /* Data+Status phase from udc */
1258 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1259 != (USB_DIR_IN | USB_TYPE_STANDARD))
1261 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1264 case USB_REQ_SET_ADDRESS:
1265 /* Status phase from udc */
1266 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1267 | USB_RECIP_DEVICE))
1269 ch9setaddress(udc, wValue, wIndex, wLength);
1272 case USB_REQ_CLEAR_FEATURE:
1273 case USB_REQ_SET_FEATURE:
1274 /* Status phase from udc */
1276 int rc = -EOPNOTSUPP;
1278 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1279 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
1280 int pipe = get_pipe_by_windex(wIndex);
1283 if (wValue != 0 || wLength != 0 || pipe > udc->max_ep)
1285 ep = get_ep_by_pipe(udc, pipe);
1287 spin_unlock(&udc->lock);
1288 rc = fsl_ep_set_halt(&ep->ep,
1289 (setup->bRequest == USB_REQ_SET_FEATURE)
1291 spin_lock(&udc->lock);
1293 } else if ((setup->bRequestType & (USB_RECIP_MASK
1294 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
1295 | USB_TYPE_STANDARD)) {
1296 /* Note: The driver has not include OTG support yet.
1297 * This will be set when OTG support is added */
1298 if (!gadget_is_otg(&udc->gadget))
1300 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE)
1301 udc->gadget.b_hnp_enable = 1;
1302 else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
1303 udc->gadget.a_hnp_support = 1;
1304 else if (setup->bRequest ==
1305 USB_DEVICE_A_ALT_HNP_SUPPORT)
1306 udc->gadget.a_alt_hnp_support = 1;
1314 if (ep0_prime_status(udc, EP_DIR_IN))
1324 /* Requests handled by gadget */
1326 /* Data phase from gadget, status phase from udc */
1327 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1328 ? USB_DIR_IN : USB_DIR_OUT;
1329 spin_unlock(&udc->lock);
1330 if (udc->driver->setup(&udc->gadget,
1331 &udc->local_setup_buff) < 0)
1333 spin_lock(&udc->lock);
1334 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1335 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1337 /* No data phase, IN status from gadget */
1338 udc->ep0_dir = USB_DIR_IN;
1339 spin_unlock(&udc->lock);
1340 if (udc->driver->setup(&udc->gadget,
1341 &udc->local_setup_buff) < 0)
1343 spin_lock(&udc->lock);
1344 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1348 /* Process request for Data or Status phase of ep0
1349 * prime status phase if needed */
1350 static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1351 struct fsl_req *req)
1353 if (udc->usb_state == USB_STATE_ADDRESS) {
1354 /* Set the new address */
1355 u32 new_address = (u32) udc->device_address;
1356 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1357 &dr_regs->deviceaddr);
1362 switch (udc->ep0_state) {
1363 case DATA_STATE_XMIT:
1364 /* receive status phase */
1365 if (ep0_prime_status(udc, EP_DIR_OUT))
1368 case DATA_STATE_RECV:
1369 /* send status phase */
1370 if (ep0_prime_status(udc, EP_DIR_IN))
1373 case WAIT_FOR_OUT_STATUS:
1374 udc->ep0_state = WAIT_FOR_SETUP;
1376 case WAIT_FOR_SETUP:
1377 ERR("Unexpect ep0 packets\n");
1385 /* Tripwire mechanism to ensure a setup packet payload is extracted without
1386 * being corrupted by another incoming setup packet */
1387 static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1390 struct ep_queue_head *qh;
1392 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1394 /* Clear bit in ENDPTSETUPSTAT */
1395 temp = fsl_readl(&dr_regs->endptsetupstat);
1396 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1398 /* while a hazard exists when setup package arrives */
1400 /* Set Setup Tripwire */
1401 temp = fsl_readl(&dr_regs->usbcmd);
1402 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1404 /* Copy the setup packet to local buffer */
1405 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1406 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1408 /* Clear Setup Tripwire */
1409 temp = fsl_readl(&dr_regs->usbcmd);
1410 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1413 /* process-ep_req(): free the completed Tds for this req */
1414 static int process_ep_req(struct fsl_udc *udc, int pipe,
1415 struct fsl_req *curr_req)
1417 struct ep_td_struct *curr_td;
1418 int td_complete, actual, remaining_length, j, tmp;
1421 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1422 int direction = pipe % 2;
1424 curr_td = curr_req->head;
1426 actual = curr_req->req.length;
1428 for (j = 0; j < curr_req->dtd_count; j++) {
1429 remaining_length = (le32_to_cpu(curr_td->size_ioc_sts)
1431 >> DTD_LENGTH_BIT_POS;
1432 actual -= remaining_length;
1434 if ((errors = le32_to_cpu(curr_td->size_ioc_sts) &
1436 if (errors & DTD_STATUS_HALTED) {
1437 ERR("dTD error %08x QH=%d\n", errors, pipe);
1438 /* Clear the errors and Halt condition */
1439 tmp = le32_to_cpu(curr_qh->size_ioc_int_sts);
1441 curr_qh->size_ioc_int_sts = cpu_to_le32(tmp);
1443 /* FIXME: continue with next queued TD? */
1447 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1448 VDBG("Transfer overflow");
1451 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1456 ERR("Unknown error has occured (0x%x)!\n",
1459 } else if (le32_to_cpu(curr_td->size_ioc_sts)
1460 & DTD_STATUS_ACTIVE) {
1461 VDBG("Request not complete");
1462 status = REQ_UNCOMPLETE;
1464 } else if (remaining_length) {
1466 VDBG("Transmit dTD remaining length not zero");
1475 VDBG("dTD transmitted successful");
1478 if (j != curr_req->dtd_count - 1)
1479 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1485 curr_req->req.actual = actual;
1490 /* Process a DTD completion interrupt */
1491 static void dtd_complete_irq(struct fsl_udc *udc)
1494 int i, ep_num, direction, bit_mask, status;
1495 struct fsl_ep *curr_ep;
1496 struct fsl_req *curr_req, *temp_req;
1498 /* Clear the bits in the register */
1499 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1500 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1505 for (i = 0; i < udc->max_ep * 2; i++) {
1509 bit_mask = 1 << (ep_num + 16 * direction);
1511 if (!(bit_pos & bit_mask))
1514 curr_ep = get_ep_by_pipe(udc, i);
1516 /* If the ep is configured */
1517 if (curr_ep->name == NULL) {
1518 WARNING("Invalid EP?");
1522 /* process the req queue until an uncomplete request */
1523 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1525 status = process_ep_req(udc, i, curr_req);
1527 VDBG("status of process_ep_req= %d, ep = %d",
1529 if (status == REQ_UNCOMPLETE)
1531 /* write back status to req */
1532 curr_req->req.status = status;
1535 ep0_req_complete(udc, curr_ep, curr_req);
1538 done(curr_ep, curr_req, status);
1543 /* Process a port change interrupt */
1544 static void port_change_irq(struct fsl_udc *udc)
1548 /* Bus resetting is finished */
1549 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) {
1551 speed = (fsl_readl(&dr_regs->portsc1)
1552 & PORTSCX_PORT_SPEED_MASK);
1554 case PORTSCX_PORT_SPEED_HIGH:
1555 udc->gadget.speed = USB_SPEED_HIGH;
1557 case PORTSCX_PORT_SPEED_FULL:
1558 udc->gadget.speed = USB_SPEED_FULL;
1560 case PORTSCX_PORT_SPEED_LOW:
1561 udc->gadget.speed = USB_SPEED_LOW;
1564 udc->gadget.speed = USB_SPEED_UNKNOWN;
1569 /* Update USB state */
1570 if (!udc->resume_state)
1571 udc->usb_state = USB_STATE_DEFAULT;
1574 /* Process suspend interrupt */
1575 static void suspend_irq(struct fsl_udc *udc)
1577 udc->resume_state = udc->usb_state;
1578 udc->usb_state = USB_STATE_SUSPENDED;
1580 /* report suspend to the driver, serial.c does not support this */
1581 if (udc->driver->suspend)
1582 udc->driver->suspend(&udc->gadget);
1585 static void bus_resume(struct fsl_udc *udc)
1587 udc->usb_state = udc->resume_state;
1588 udc->resume_state = 0;
1590 /* report resume to the driver, serial.c does not support this */
1591 if (udc->driver->resume)
1592 udc->driver->resume(&udc->gadget);
1595 /* Clear up all ep queues */
1596 static int reset_queues(struct fsl_udc *udc)
1600 for (pipe = 0; pipe < udc->max_pipes; pipe++)
1601 udc_reset_ep_queue(udc, pipe);
1603 /* report disconnect; the driver is already quiesced */
1604 spin_unlock(&udc->lock);
1605 udc->driver->disconnect(&udc->gadget);
1606 spin_lock(&udc->lock);
1611 /* Process reset interrupt */
1612 static void reset_irq(struct fsl_udc *udc)
1615 unsigned long timeout;
1617 /* Clear the device address */
1618 temp = fsl_readl(&dr_regs->deviceaddr);
1619 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
1621 udc->device_address = 0;
1623 /* Clear usb state */
1624 udc->resume_state = 0;
1626 udc->ep0_state = WAIT_FOR_SETUP;
1627 udc->remote_wakeup = 0; /* default to 0 on reset */
1628 udc->gadget.b_hnp_enable = 0;
1629 udc->gadget.a_hnp_support = 0;
1630 udc->gadget.a_alt_hnp_support = 0;
1632 /* Clear all the setup token semaphores */
1633 temp = fsl_readl(&dr_regs->endptsetupstat);
1634 fsl_writel(temp, &dr_regs->endptsetupstat);
1636 /* Clear all the endpoint complete status bits */
1637 temp = fsl_readl(&dr_regs->endptcomplete);
1638 fsl_writel(temp, &dr_regs->endptcomplete);
1640 timeout = jiffies + 100;
1641 while (fsl_readl(&dr_regs->endpointprime)) {
1642 /* Wait until all endptprime bits cleared */
1643 if (time_after(jiffies, timeout)) {
1644 ERR("Timeout for reset\n");
1650 /* Write 1s to the flush register */
1651 fsl_writel(0xffffffff, &dr_regs->endptflush);
1653 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1655 /* Reset all the queues, include XD, dTD, EP queue
1656 * head and TR Queue */
1658 udc->usb_state = USB_STATE_DEFAULT;
1660 VDBG("Controller reset");
1661 /* initialize usb hw reg except for regs for EP, not
1662 * touch usbintr reg */
1663 dr_controller_setup(udc);
1665 /* Reset all internal used Queues */
1670 /* Enable DR IRQ reg, Set Run bit, change udc state */
1671 dr_controller_run(udc);
1672 udc->usb_state = USB_STATE_ATTACHED;
1677 * USB device controller interrupt handler
1679 static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1681 struct fsl_udc *udc = _udc;
1683 irqreturn_t status = IRQ_NONE;
1684 unsigned long flags;
1686 /* Disable ISR for OTG host mode */
1689 spin_lock_irqsave(&udc->lock, flags);
1690 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
1691 /* Clear notification bits */
1692 fsl_writel(irq_src, &dr_regs->usbsts);
1694 /* VDBG("irq_src [0x%8x]", irq_src); */
1696 /* Need to resume? */
1697 if (udc->usb_state == USB_STATE_SUSPENDED)
1698 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
1702 if (irq_src & USB_STS_INT) {
1704 /* Setup package, we only support ep0 as control ep */
1705 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
1706 tripwire_handler(udc, 0,
1707 (u8 *) (&udc->local_setup_buff));
1708 setup_received_irq(udc, &udc->local_setup_buff);
1709 status = IRQ_HANDLED;
1712 /* completion of dtd */
1713 if (fsl_readl(&dr_regs->endptcomplete)) {
1714 dtd_complete_irq(udc);
1715 status = IRQ_HANDLED;
1719 /* SOF (for ISO transfer) */
1720 if (irq_src & USB_STS_SOF) {
1721 status = IRQ_HANDLED;
1725 if (irq_src & USB_STS_PORT_CHANGE) {
1726 port_change_irq(udc);
1727 status = IRQ_HANDLED;
1730 /* Reset Received */
1731 if (irq_src & USB_STS_RESET) {
1733 status = IRQ_HANDLED;
1736 /* Sleep Enable (Suspend) */
1737 if (irq_src & USB_STS_SUSPEND) {
1739 status = IRQ_HANDLED;
1742 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
1743 VDBG("Error IRQ %x", irq_src);
1746 spin_unlock_irqrestore(&udc->lock, flags);
1750 /*----------------------------------------------------------------*
1751 * Hook to gadget drivers
1752 * Called by initialization code of gadget drivers
1753 *----------------------------------------------------------------*/
1754 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1756 int retval = -ENODEV;
1757 unsigned long flags = 0;
1759 if (!udc_controller)
1762 if (!driver || (driver->speed != USB_SPEED_FULL
1763 && driver->speed != USB_SPEED_HIGH)
1764 || !driver->bind || !driver->disconnect
1768 if (udc_controller->driver)
1771 /* lock is needed but whether should use this lock or another */
1772 spin_lock_irqsave(&udc_controller->lock, flags);
1774 driver->driver.bus = NULL;
1775 /* hook up the driver */
1776 udc_controller->driver = driver;
1777 udc_controller->gadget.dev.driver = &driver->driver;
1778 spin_unlock_irqrestore(&udc_controller->lock, flags);
1780 /* bind udc driver to gadget driver */
1781 retval = driver->bind(&udc_controller->gadget);
1783 VDBG("bind to %s --> %d", driver->driver.name, retval);
1784 udc_controller->gadget.dev.driver = NULL;
1785 udc_controller->driver = NULL;
1789 /* Enable DR IRQ reg and Set usbcmd reg Run bit */
1790 dr_controller_run(udc_controller);
1791 udc_controller->usb_state = USB_STATE_ATTACHED;
1792 udc_controller->ep0_state = WAIT_FOR_SETUP;
1793 udc_controller->ep0_dir = 0;
1794 printk(KERN_INFO "%s: bind to driver %s\n",
1795 udc_controller->gadget.name, driver->driver.name);
1799 printk("gadget driver register failed %d\n", retval);
1802 EXPORT_SYMBOL(usb_gadget_register_driver);
1804 /* Disconnect from gadget driver */
1805 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1807 struct fsl_ep *loop_ep;
1808 unsigned long flags;
1810 if (!udc_controller)
1813 if (!driver || driver != udc_controller->driver || !driver->unbind)
1816 if (udc_controller->transceiver)
1817 otg_set_peripheral(udc_controller->transceiver, NULL);
1819 /* stop DR, disable intr */
1820 dr_controller_stop(udc_controller);
1822 /* in fact, no needed */
1823 udc_controller->usb_state = USB_STATE_ATTACHED;
1824 udc_controller->ep0_state = WAIT_FOR_SETUP;
1825 udc_controller->ep0_dir = 0;
1827 /* stand operation */
1828 spin_lock_irqsave(&udc_controller->lock, flags);
1829 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
1830 nuke(&udc_controller->eps[0], -ESHUTDOWN);
1831 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
1833 nuke(loop_ep, -ESHUTDOWN);
1834 spin_unlock_irqrestore(&udc_controller->lock, flags);
1836 /* unbind gadget and unhook driver. */
1837 driver->unbind(&udc_controller->gadget);
1838 udc_controller->gadget.dev.driver = NULL;
1839 udc_controller->driver = NULL;
1841 printk("unregistered gadget driver '%s'\n", driver->driver.name);
1844 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1846 /*-------------------------------------------------------------------------
1847 PROC File System Support
1848 -------------------------------------------------------------------------*/
1849 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1851 #include <linux/seq_file.h>
1853 static const char proc_filename[] = "driver/fsl_usb2_udc";
1855 static int fsl_proc_read(char *page, char **start, off_t off, int count,
1856 int *eof, void *_dev)
1860 unsigned size = count;
1861 unsigned long flags;
1864 struct fsl_ep *ep = NULL;
1865 struct fsl_req *req;
1867 struct fsl_udc *udc = udc_controller;
1871 spin_lock_irqsave(&udc->lock, flags);
1873 /* ------basic driver information ---- */
1874 t = scnprintf(next, size,
1877 "Gadget driver: %s\n\n",
1878 driver_name, DRIVER_VERSION,
1879 udc->driver ? udc->driver->driver.name : "(none)");
1883 /* ------ DR Registers ----- */
1884 tmp_reg = fsl_readl(&dr_regs->usbcmd);
1885 t = scnprintf(next, size,
1889 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
1890 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
1894 tmp_reg = fsl_readl(&dr_regs->usbsts);
1895 t = scnprintf(next, size,
1897 "Dr Suspend: %d Reset Received: %d System Error: %s "
1898 "USB Error Interrupt: %s\n\n",
1899 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
1900 (tmp_reg & USB_STS_RESET) ? 1 : 0,
1901 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
1902 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
1906 tmp_reg = fsl_readl(&dr_regs->usbintr);
1907 t = scnprintf(next, size,
1908 "USB Intrrupt Enable Reg:\n"
1909 "Sleep Enable: %d SOF Received Enable: %d "
1910 "Reset Enable: %d\n"
1911 "System Error Enable: %d "
1912 "Port Change Dectected Enable: %d\n"
1913 "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
1914 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
1915 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
1916 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
1917 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
1918 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
1919 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
1920 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
1924 tmp_reg = fsl_readl(&dr_regs->frindex);
1925 t = scnprintf(next, size,
1926 "USB Frame Index Reg: Frame Number is 0x%x\n\n",
1927 (tmp_reg & USB_FRINDEX_MASKS));
1931 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
1932 t = scnprintf(next, size,
1933 "USB Device Address Reg: Device Addr is 0x%x\n\n",
1934 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
1938 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
1939 t = scnprintf(next, size,
1940 "USB Endpoint List Address Reg: "
1941 "Device Addr is 0x%x\n\n",
1942 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
1946 tmp_reg = fsl_readl(&dr_regs->portsc1);
1947 t = scnprintf(next, size,
1948 "USB Port Status&Control Reg:\n"
1949 "Port Transceiver Type : %s Port Speed: %s\n"
1950 "PHY Low Power Suspend: %s Port Reset: %s "
1951 "Port Suspend Mode: %s\n"
1952 "Over-current Change: %s "
1953 "Port Enable/Disable Change: %s\n"
1954 "Port Enabled/Disabled: %s "
1955 "Current Connect Status: %s\n\n", ( {
1957 switch (tmp_reg & PORTSCX_PTS_FSLS) {
1958 case PORTSCX_PTS_UTMI:
1960 case PORTSCX_PTS_ULPI:
1962 case PORTSCX_PTS_FSLS:
1963 s = "FS/LS Serial"; break;
1969 switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
1970 case PORTSCX_PORT_SPEED_FULL:
1971 s = "Full Speed"; break;
1972 case PORTSCX_PORT_SPEED_LOW:
1973 s = "Low Speed"; break;
1974 case PORTSCX_PORT_SPEED_HIGH:
1975 s = "High Speed"; break;
1977 s = "Undefined"; break;
1981 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
1982 "Normal PHY mode" : "Low power mode",
1983 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
1985 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
1986 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
1988 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
1990 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
1992 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
1993 "Attached" : "Not-Att");
1997 tmp_reg = fsl_readl(&dr_regs->usbmode);
1998 t = scnprintf(next, size,
1999 "USB Mode Reg: Controller Mode is: %s\n\n", ( {
2001 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2002 case USB_MODE_CTRL_MODE_IDLE:
2004 case USB_MODE_CTRL_MODE_DEVICE:
2005 s = "Device Controller"; break;
2006 case USB_MODE_CTRL_MODE_HOST:
2007 s = "Host Controller"; break;
2016 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2017 t = scnprintf(next, size,
2018 "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
2019 (tmp_reg & EP_SETUP_STATUS_MASK));
2023 for (i = 0; i < udc->max_ep / 2; i++) {
2024 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2025 t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
2030 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2031 t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
2035 tmp_reg = usb_sys_regs->snoop1;
2036 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2040 tmp_reg = usb_sys_regs->control;
2041 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
2046 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2048 t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
2049 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2053 if (list_empty(&ep->queue)) {
2054 t = scnprintf(next, size, "its req queue is empty\n\n");
2058 list_for_each_entry(req, &ep->queue, queue) {
2059 t = scnprintf(next, size,
2060 "req %p actual 0x%x length 0x%x buf %p\n",
2061 &req->req, req->req.actual,
2062 req->req.length, req->req.buf);
2067 /* other gadget->eplist ep */
2068 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2070 t = scnprintf(next, size,
2071 "\nFor %s Maxpkt is 0x%x "
2073 ep->ep.name, ep_maxpacket(ep),
2078 if (list_empty(&ep->queue)) {
2079 t = scnprintf(next, size,
2080 "its req queue is empty\n\n");
2084 list_for_each_entry(req, &ep->queue, queue) {
2085 t = scnprintf(next, size,
2086 "req %p actual 0x%x length "
2088 &req->req, req->req.actual,
2089 req->req.length, req->req.buf);
2092 } /* end for each_entry of ep req */
2093 } /* end for else */
2094 } /* end for if(ep->queue) */
2095 } /* end (ep->desc) */
2097 spin_unlock_irqrestore(&udc->lock, flags);
2100 return count - size;
2103 #define create_proc_file() create_proc_read_entry(proc_filename, \
2104 0, NULL, fsl_proc_read, NULL)
2106 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2108 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2110 #define create_proc_file() do {} while (0)
2111 #define remove_proc_file() do {} while (0)
2113 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2115 /*-------------------------------------------------------------------------*/
2117 /* Release udc structures */
2118 static void fsl_udc_release(struct device *dev)
2120 complete(udc_controller->done);
2121 dma_free_coherent(dev, udc_controller->ep_qh_size,
2122 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2123 kfree(udc_controller);
2126 /******************************************************************
2127 Internal structure setup functions
2128 *******************************************************************/
2129 /*------------------------------------------------------------------
2130 * init resource for globle controller
2131 * Return the udc handle on success or NULL on failure
2132 ------------------------------------------------------------------*/
2133 static int __init struct_udc_setup(struct fsl_udc *udc,
2134 struct platform_device *pdev)
2136 struct fsl_usb2_platform_data *pdata;
2139 pdata = pdev->dev.platform_data;
2140 udc->phy_mode = pdata->phy_mode;
2142 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2144 ERR("malloc fsl_ep failed\n");
2148 /* initialized QHs, take care of alignment */
2149 size = udc->max_ep * sizeof(struct ep_queue_head);
2150 if (size < QH_ALIGNMENT)
2151 size = QH_ALIGNMENT;
2152 else if ((size % QH_ALIGNMENT) != 0) {
2153 size += QH_ALIGNMENT + 1;
2154 size &= ~(QH_ALIGNMENT - 1);
2156 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2157 &udc->ep_qh_dma, GFP_KERNEL);
2159 ERR("malloc QHs for udc failed\n");
2164 udc->ep_qh_size = size;
2166 /* Initialize ep0 status request structure */
2167 /* FIXME: fsl_alloc_request() ignores ep argument */
2168 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2169 struct fsl_req, req);
2170 /* allocate a small amount of memory to get valid address */
2171 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2172 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2174 udc->resume_state = USB_STATE_NOTATTACHED;
2175 udc->usb_state = USB_STATE_POWERED;
2177 udc->remote_wakeup = 0; /* default to 0 on reset */
2182 /*----------------------------------------------------------------
2183 * Setup the fsl_ep struct for eps
2184 * Link fsl_ep->ep to gadget->ep_list
2185 * ep0out is not used so do nothing here
2186 * ep0in should be taken care
2187 *--------------------------------------------------------------*/
2188 static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2189 char *name, int link)
2191 struct fsl_ep *ep = &udc->eps[index];
2194 strcpy(ep->name, name);
2195 ep->ep.name = ep->name;
2197 ep->ep.ops = &fsl_ep_ops;
2200 /* for ep0: maxP defined in desc
2201 * for other eps, maxP is set by epautoconfig() called by gadget layer
2203 ep->ep.maxpacket = (unsigned short) ~0;
2205 /* the queue lists any req for this ep */
2206 INIT_LIST_HEAD(&ep->queue);
2208 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
2210 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2211 ep->gadget = &udc->gadget;
2212 ep->qh = &udc->ep_qh[index];
2217 /* Driver probe function
2218 * all intialization operations implemented here except enabling usb_intr reg
2219 * board setup should have been done in the platform code
2221 static int __init fsl_udc_probe(struct platform_device *pdev)
2223 struct resource *res;
2228 if (strcmp(pdev->name, driver_name)) {
2229 VDBG("Wrong device");
2233 udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
2234 if (udc_controller == NULL) {
2235 ERR("malloc udc failed\n");
2239 spin_lock_init(&udc_controller->lock);
2240 udc_controller->stopped = 1;
2242 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2244 kfree(udc_controller);
2248 if (!request_mem_region(res->start, res->end - res->start + 1,
2250 ERR("request mem region for %s failed\n", pdev->name);
2251 kfree(udc_controller);
2255 dr_regs = ioremap(res->start, res->end - res->start + 1);
2261 usb_sys_regs = (struct usb_sys_interface *)
2262 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2264 /* Read Device Controller Capability Parameters register */
2265 dccparams = fsl_readl(&dr_regs->dccparams);
2266 if (!(dccparams & DCCPARAMS_DC)) {
2267 ERR("This SOC doesn't support device role\n");
2271 /* Get max device endpoints */
2272 /* DEN is bidirectional ep number, max_ep doubles the number */
2273 udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
2275 udc_controller->irq = platform_get_irq(pdev, 0);
2276 if (!udc_controller->irq) {
2281 ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
2282 driver_name, udc_controller);
2284 ERR("cannot request irq %d err %d\n",
2285 udc_controller->irq, ret);
2289 /* Initialize the udc structure including QH member and other member */
2290 if (struct_udc_setup(udc_controller, pdev)) {
2291 ERR("Can't initialize udc data structure\n");
2296 /* initialize usb hw reg except for regs for EP,
2297 * leave usbintr reg untouched */
2298 dr_controller_setup(udc_controller);
2300 /* Setup gadget structure */
2301 udc_controller->gadget.ops = &fsl_gadget_ops;
2302 udc_controller->gadget.is_dualspeed = 1;
2303 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2304 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2305 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2306 udc_controller->gadget.name = driver_name;
2308 /* Setup gadget.dev and register with kernel */
2309 dev_set_name(&udc_controller->gadget.dev, "gadget");
2310 udc_controller->gadget.dev.release = fsl_udc_release;
2311 udc_controller->gadget.dev.parent = &pdev->dev;
2312 ret = device_register(&udc_controller->gadget.dev);
2316 /* setup QH and epctrl for ep0 */
2317 ep0_setup(udc_controller);
2319 /* setup udc->eps[] for ep0 */
2320 struct_ep_setup(udc_controller, 0, "ep0", 0);
2321 /* for ep0: the desc defined here;
2322 * for other eps, gadget layer called ep_enable with defined desc
2324 udc_controller->eps[0].desc = &fsl_ep0_desc;
2325 udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
2327 /* setup the udc->eps[] for non-control endpoints and link
2328 * to gadget.ep_list */
2329 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
2332 sprintf(name, "ep%dout", i);
2333 struct_ep_setup(udc_controller, i * 2, name, 1);
2334 sprintf(name, "ep%din", i);
2335 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2338 /* use dma_pool for TD management */
2339 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
2340 sizeof(struct ep_td_struct),
2341 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2342 if (udc_controller->td_pool == NULL) {
2350 device_unregister(&udc_controller->gadget.dev);
2352 free_irq(udc_controller->irq, udc_controller);
2356 release_mem_region(res->start, res->end - res->start + 1);
2357 kfree(udc_controller);
2361 /* Driver removal function
2362 * Free resources and finish pending transactions
2364 static int __exit fsl_udc_remove(struct platform_device *pdev)
2366 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2368 DECLARE_COMPLETION(done);
2370 if (!udc_controller)
2372 udc_controller->done = &done;
2374 /* DR has been stopped in usb_gadget_unregister_driver() */
2377 /* Free allocated memory */
2378 kfree(udc_controller->status_req->req.buf);
2379 kfree(udc_controller->status_req);
2380 kfree(udc_controller->eps);
2382 dma_pool_destroy(udc_controller->td_pool);
2383 free_irq(udc_controller->irq, udc_controller);
2385 release_mem_region(res->start, res->end - res->start + 1);
2387 device_unregister(&udc_controller->gadget.dev);
2388 /* free udc --wait for the release() finished */
2389 wait_for_completion(&done);
2394 /*-----------------------------------------------------------------
2395 * Modify Power management attributes
2396 * Used by OTG statemachine to disable gadget temporarily
2397 -----------------------------------------------------------------*/
2398 static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
2400 dr_controller_stop(udc_controller);
2404 /*-----------------------------------------------------------------
2405 * Invoked on USB resume. May be called in_interrupt.
2406 * Here we start the DR controller and enable the irq
2407 *-----------------------------------------------------------------*/
2408 static int fsl_udc_resume(struct platform_device *pdev)
2410 /* Enable DR irq reg and set controller Run */
2411 if (udc_controller->stopped) {
2412 dr_controller_setup(udc_controller);
2413 dr_controller_run(udc_controller);
2415 udc_controller->usb_state = USB_STATE_ATTACHED;
2416 udc_controller->ep0_state = WAIT_FOR_SETUP;
2417 udc_controller->ep0_dir = 0;
2421 /*-------------------------------------------------------------------------
2422 Register entry point for the peripheral controller driver
2423 --------------------------------------------------------------------------*/
2425 static struct platform_driver udc_driver = {
2426 .remove = __exit_p(fsl_udc_remove),
2427 /* these suspend and resume are not usb suspend and resume */
2428 .suspend = fsl_udc_suspend,
2429 .resume = fsl_udc_resume,
2431 .name = (char *)driver_name,
2432 .owner = THIS_MODULE,
2436 static int __init udc_init(void)
2438 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
2439 return platform_driver_probe(&udc_driver, fsl_udc_probe);
2442 module_init(udc_init);
2444 static void __exit udc_exit(void)
2446 platform_driver_unregister(&udc_driver);
2447 printk("%s unregistered\n", driver_desc);
2450 module_exit(udc_exit);
2452 MODULE_DESCRIPTION(DRIVER_DESC);
2453 MODULE_AUTHOR(DRIVER_AUTHOR);
2454 MODULE_LICENSE("GPL");
2455 MODULE_ALIAS("platform:fsl-usb2-udc");