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 int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state);
75 static int fsl_udc_resume(struct platform_device *pdev);
76 static void fsl_ep_fifo_flush(struct usb_ep *_ep);
79 #define fsl_readl(addr) in_le32(addr)
80 #define fsl_writel(addr, val32) out_le32(val32, addr)
82 #define fsl_readl(addr) readl(addr)
83 #define fsl_writel(addr, val32) writel(addr, val32)
86 /********************************************************************
87 * Internal Used Function
88 ********************************************************************/
89 /*-----------------------------------------------------------------
90 * done() - retire a request; caller blocked irqs
91 * @status : request status to be set, only works when
92 * request is still in progress.
93 *--------------------------------------------------------------*/
94 static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
96 struct fsl_udc *udc = NULL;
97 unsigned char stopped = ep->stopped;
98 struct ep_td_struct *curr_td, *next_td;
101 udc = (struct fsl_udc *)ep->udc;
102 /* Removed the req from fsl_ep->queue */
103 list_del_init(&req->queue);
105 /* req.status should be set as -EINPROGRESS in ep_queue() */
106 if (req->req.status == -EINPROGRESS)
107 req->req.status = status;
109 status = req->req.status;
111 /* Free dtd for the request */
113 for (j = 0; j < req->dtd_count; j++) {
115 if (j != req->dtd_count - 1) {
116 next_td = curr_td->next_td_virt;
118 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
122 dma_unmap_single(ep->udc->gadget.dev.parent,
123 req->req.dma, req->req.length,
127 req->req.dma = DMA_ADDR_INVALID;
130 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
131 req->req.dma, req->req.length,
136 if (status && (status != -ESHUTDOWN))
137 VDBG("complete %s req %p stat %d len %u/%u",
138 ep->ep.name, &req->req, status,
139 req->req.actual, req->req.length);
143 spin_unlock(&ep->udc->lock);
144 /* complete() is from gadget layer,
145 * eg fsg->bulk_in_complete() */
146 if (req->req.complete)
147 req->req.complete(&ep->ep, &req->req);
149 spin_lock(&ep->udc->lock);
150 ep->stopped = stopped;
153 /*-----------------------------------------------------------------
154 * nuke(): delete all requests related to this ep
155 * called with spinlock held
156 *--------------------------------------------------------------*/
157 static void nuke(struct fsl_ep *ep, int status)
162 fsl_ep_fifo_flush(&ep->ep);
164 /* Whether this eq has request linked */
165 while (!list_empty(&ep->queue)) {
166 struct fsl_req *req = NULL;
168 req = list_entry(ep->queue.next, struct fsl_req, queue);
169 done(ep, req, status);
173 /*------------------------------------------------------------------
174 Internal Hardware related function
175 ------------------------------------------------------------------*/
177 static int dr_controller_setup(struct fsl_udc *udc)
179 unsigned int tmp = 0, portctrl = 0, ctrl = 0;
180 unsigned long timeout;
181 #define FSL_UDC_RESET_TIMEOUT 1000
183 /* Stop and reset the usb controller */
184 tmp = fsl_readl(&dr_regs->usbcmd);
185 tmp &= ~USB_CMD_RUN_STOP;
186 fsl_writel(tmp, &dr_regs->usbcmd);
188 tmp = fsl_readl(&dr_regs->usbcmd);
189 tmp |= USB_CMD_CTRL_RESET;
190 fsl_writel(tmp, &dr_regs->usbcmd);
192 /* Wait for reset to complete */
193 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
194 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
195 if (time_after(jiffies, timeout)) {
196 ERR("udc reset timeout!\n");
202 /* Set the controller as device mode */
203 tmp = fsl_readl(&dr_regs->usbmode);
204 tmp |= USB_MODE_CTRL_MODE_DEVICE;
205 /* Disable Setup Lockout */
206 tmp |= USB_MODE_SETUP_LOCK_OFF;
207 fsl_writel(tmp, &dr_regs->usbmode);
209 /* Clear the setup status */
210 fsl_writel(0, &dr_regs->usbsts);
212 tmp = udc->ep_qh_dma;
213 tmp &= USB_EP_LIST_ADDRESS_MASK;
214 fsl_writel(tmp, &dr_regs->endpointlistaddr);
216 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
217 udc->ep_qh, (int)tmp,
218 fsl_readl(&dr_regs->endpointlistaddr));
220 /* Config PHY interface */
221 portctrl = fsl_readl(&dr_regs->portsc1);
222 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
223 switch (udc->phy_mode) {
224 case FSL_USB2_PHY_ULPI:
225 portctrl |= PORTSCX_PTS_ULPI;
227 case FSL_USB2_PHY_UTMI_WIDE:
228 portctrl |= PORTSCX_PTW_16BIT;
230 case FSL_USB2_PHY_UTMI:
231 portctrl |= PORTSCX_PTS_UTMI;
233 case FSL_USB2_PHY_SERIAL:
234 portctrl |= PORTSCX_PTS_FSLS;
239 fsl_writel(portctrl, &dr_regs->portsc1);
241 /* Config control enable i/o output, cpu endian register */
242 ctrl = __raw_readl(&usb_sys_regs->control);
243 ctrl |= USB_CTRL_IOENB;
244 __raw_writel(ctrl, &usb_sys_regs->control);
246 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
247 /* Turn on cache snooping hardware, since some PowerPC platforms
248 * wholly rely on hardware to deal with cache coherent. */
250 /* Setup Snooping for all the 4GB space */
251 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
252 __raw_writel(tmp, &usb_sys_regs->snoop1);
253 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
254 __raw_writel(tmp, &usb_sys_regs->snoop2);
260 /* Enable DR irq and set controller to run state */
261 static void dr_controller_run(struct fsl_udc *udc)
265 /* Enable DR irq reg */
266 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
267 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
268 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
270 fsl_writel(temp, &dr_regs->usbintr);
272 /* Clear stopped bit */
275 /* Set the controller as device mode */
276 temp = fsl_readl(&dr_regs->usbmode);
277 temp |= USB_MODE_CTRL_MODE_DEVICE;
278 fsl_writel(temp, &dr_regs->usbmode);
280 /* Set controller to Run */
281 temp = fsl_readl(&dr_regs->usbcmd);
282 temp |= USB_CMD_RUN_STOP;
283 fsl_writel(temp, &dr_regs->usbcmd);
288 static void dr_controller_stop(struct fsl_udc *udc)
292 /* disable all INTR */
293 fsl_writel(0, &dr_regs->usbintr);
295 /* Set stopped bit for isr */
298 /* disable IO output */
299 /* usb_sys_regs->control = 0; */
301 /* set controller to Stop */
302 tmp = fsl_readl(&dr_regs->usbcmd);
303 tmp &= ~USB_CMD_RUN_STOP;
304 fsl_writel(tmp, &dr_regs->usbcmd);
309 static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
310 unsigned char ep_type)
312 unsigned int tmp_epctrl = 0;
314 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
317 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
318 tmp_epctrl |= EPCTRL_TX_ENABLE;
319 tmp_epctrl |= ((unsigned int)(ep_type)
320 << EPCTRL_TX_EP_TYPE_SHIFT);
323 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
324 tmp_epctrl |= EPCTRL_RX_ENABLE;
325 tmp_epctrl |= ((unsigned int)(ep_type)
326 << EPCTRL_RX_EP_TYPE_SHIFT);
329 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
333 dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
337 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
340 /* set the stall bit */
342 tmp_epctrl |= EPCTRL_TX_EP_STALL;
344 tmp_epctrl |= EPCTRL_RX_EP_STALL;
346 /* clear the stall bit and reset data toggle */
348 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
349 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
351 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
352 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
355 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
358 /* Get stall status of a specific ep
359 Return: 0: not stalled; 1:stalled */
360 static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
364 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
366 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
368 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
371 /********************************************************************
372 Internal Structure Build up functions
373 ********************************************************************/
375 /*------------------------------------------------------------------
376 * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
377 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
379 ------------------------------------------------------------------*/
380 static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
381 unsigned char dir, unsigned char ep_type,
382 unsigned int max_pkt_len,
383 unsigned int zlt, unsigned char mult)
385 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
386 unsigned int tmp = 0;
388 /* set the Endpoint Capabilites in QH */
390 case USB_ENDPOINT_XFER_CONTROL:
391 /* Interrupt On Setup (IOS). for control ep */
392 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
395 case USB_ENDPOINT_XFER_ISOC:
396 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
397 | (mult << EP_QUEUE_HEAD_MULT_POS);
399 case USB_ENDPOINT_XFER_BULK:
400 case USB_ENDPOINT_XFER_INT:
401 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
404 VDBG("error ep type is %d", ep_type);
408 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
409 p_QH->max_pkt_length = cpu_to_le32(tmp);
414 /* Setup qh structure and ep register for ep0. */
415 static void ep0_setup(struct fsl_udc *udc)
417 /* the intialization of an ep includes: fields in QH, Regs,
419 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
420 USB_MAX_CTRL_PAYLOAD, 0, 0);
421 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
422 USB_MAX_CTRL_PAYLOAD, 0, 0);
423 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
424 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
430 /***********************************************************************
431 Endpoint Management Functions
432 ***********************************************************************/
434 /*-------------------------------------------------------------------------
435 * when configurations are set, or when interface settings change
436 * for example the do_set_interface() in gadget layer,
437 * the driver will enable or disable the relevant endpoints
438 * ep0 doesn't use this routine. It is always enabled.
439 -------------------------------------------------------------------------*/
440 static int fsl_ep_enable(struct usb_ep *_ep,
441 const struct usb_endpoint_descriptor *desc)
443 struct fsl_udc *udc = NULL;
444 struct fsl_ep *ep = NULL;
445 unsigned short max = 0;
446 unsigned char mult = 0, zlt;
447 int retval = -EINVAL;
448 unsigned long flags = 0;
450 ep = container_of(_ep, struct fsl_ep, ep);
452 /* catch various bogus parameters */
453 if (!_ep || !desc || ep->desc
454 || (desc->bDescriptorType != USB_DT_ENDPOINT))
459 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
462 max = le16_to_cpu(desc->wMaxPacketSize);
464 /* Disable automatic zlp generation. Driver is reponsible to indicate
465 * explicitly through req->req.zero. This is needed to enable multi-td
469 /* Assume the max packet size from gadget is always correct */
470 switch (desc->bmAttributes & 0x03) {
471 case USB_ENDPOINT_XFER_CONTROL:
472 case USB_ENDPOINT_XFER_BULK:
473 case USB_ENDPOINT_XFER_INT:
474 /* mult = 0. Execute N Transactions as demonstrated by
475 * the USB variable length packet protocol where N is
476 * computed using the Maximum Packet Length (dQH) and
477 * the Total Bytes field (dTD) */
480 case USB_ENDPOINT_XFER_ISOC:
481 /* Calculate transactions needed for high bandwidth iso */
482 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
483 max = max & 0x8ff; /* bit 0~10 */
484 /* 3 transactions at most */
492 spin_lock_irqsave(&udc->lock, flags);
493 ep->ep.maxpacket = max;
497 /* Controller related setup */
498 /* Init EPx Queue Head (Ep Capabilites field in QH
499 * according to max, zlt, mult) */
500 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
501 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
502 ? USB_SEND : USB_RECV),
503 (unsigned char) (desc->bmAttributes
504 & USB_ENDPOINT_XFERTYPE_MASK),
507 /* Init endpoint ctrl register */
508 dr_ep_setup((unsigned char) ep_index(ep),
509 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
510 ? USB_SEND : USB_RECV),
511 (unsigned char) (desc->bmAttributes
512 & USB_ENDPOINT_XFERTYPE_MASK));
514 spin_unlock_irqrestore(&udc->lock, flags);
517 VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
518 ep->desc->bEndpointAddress & 0x0f,
519 (desc->bEndpointAddress & USB_DIR_IN)
520 ? "in" : "out", max);
525 /*---------------------------------------------------------------------
526 * @ep : the ep being unconfigured. May not be ep0
527 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
528 *---------------------------------------------------------------------*/
529 static int fsl_ep_disable(struct usb_ep *_ep)
531 struct fsl_udc *udc = NULL;
532 struct fsl_ep *ep = NULL;
533 unsigned long flags = 0;
537 ep = container_of(_ep, struct fsl_ep, ep);
538 if (!_ep || !ep->desc) {
539 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
543 /* disable ep on controller */
544 ep_num = ep_index(ep);
545 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
547 epctrl &= ~EPCTRL_TX_ENABLE;
549 epctrl &= ~EPCTRL_RX_ENABLE;
550 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
552 udc = (struct fsl_udc *)ep->udc;
553 spin_lock_irqsave(&udc->lock, flags);
555 /* nuke all pending requests (does flush) */
556 nuke(ep, -ESHUTDOWN);
560 spin_unlock_irqrestore(&udc->lock, flags);
562 VDBG("disabled %s OK", _ep->name);
566 /*---------------------------------------------------------------------
567 * allocate a request object used by this endpoint
568 * the main operation is to insert the req->queue to the eq->queue
569 * Returns the request, or null if one could not be allocated
570 *---------------------------------------------------------------------*/
571 static struct usb_request *
572 fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
574 struct fsl_req *req = NULL;
576 req = kzalloc(sizeof *req, gfp_flags);
580 req->req.dma = DMA_ADDR_INVALID;
581 INIT_LIST_HEAD(&req->queue);
586 static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
588 struct fsl_req *req = NULL;
590 req = container_of(_req, struct fsl_req, req);
596 /*-------------------------------------------------------------------------*/
597 static int fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
599 int i = ep_index(ep) * 2 + ep_is_in(ep);
600 u32 temp, bitmask, tmp_stat;
601 struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
603 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
604 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
606 bitmask = ep_is_in(ep)
607 ? (1 << (ep_index(ep) + 16))
608 : (1 << (ep_index(ep)));
610 /* check if the pipe is empty */
611 if (!(list_empty(&ep->queue))) {
612 /* Add td to the end */
613 struct fsl_req *lastreq;
614 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
615 lastreq->tail->next_td_ptr =
616 cpu_to_le32(req->head->td_dma & DTD_ADDR_MASK);
617 /* Read prime bit, if 1 goto done */
618 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
622 /* Set ATDTW bit in USBCMD */
623 temp = fsl_readl(&dr_regs->usbcmd);
624 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
626 /* Read correct status bit */
627 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
629 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
631 /* Write ATDTW bit to 0 */
632 temp = fsl_readl(&dr_regs->usbcmd);
633 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
639 /* Write dQH next pointer and terminate bit to 0 */
640 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
641 dQH->next_dtd_ptr = cpu_to_le32(temp);
643 /* Clear active and halt bit */
644 temp = cpu_to_le32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
645 | EP_QUEUE_HEAD_STATUS_HALT));
646 dQH->size_ioc_int_sts &= temp;
648 /* Prime endpoint by writing 1 to ENDPTPRIME */
650 ? (1 << (ep_index(ep) + 16))
651 : (1 << (ep_index(ep)));
652 fsl_writel(temp, &dr_regs->endpointprime);
657 /* Fill in the dTD structure
658 * @req: request that the transfer belongs to
659 * @length: return actually data length of the dTD
660 * @dma: return dma address of the dTD
661 * @is_last: return flag if it is the last dTD of the request
662 * return: pointer to the built dTD */
663 static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
664 dma_addr_t *dma, int *is_last)
667 struct ep_td_struct *dtd;
669 /* how big will this transfer be? */
670 *length = min(req->req.length - req->req.actual,
671 (unsigned)EP_MAX_LENGTH_TRANSFER);
673 dtd = dma_pool_alloc(udc_controller->td_pool, GFP_KERNEL, dma);
678 /* Clear reserved field */
679 swap_temp = cpu_to_le32(dtd->size_ioc_sts);
680 swap_temp &= ~DTD_RESERVED_FIELDS;
681 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
683 /* Init all of buffer page pointers */
684 swap_temp = (u32) (req->req.dma + req->req.actual);
685 dtd->buff_ptr0 = cpu_to_le32(swap_temp);
686 dtd->buff_ptr1 = cpu_to_le32(swap_temp + 0x1000);
687 dtd->buff_ptr2 = cpu_to_le32(swap_temp + 0x2000);
688 dtd->buff_ptr3 = cpu_to_le32(swap_temp + 0x3000);
689 dtd->buff_ptr4 = cpu_to_le32(swap_temp + 0x4000);
691 req->req.actual += *length;
693 /* zlp is needed if req->req.zero is set */
695 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
699 } else if (req->req.length == req->req.actual)
705 VDBG("multi-dtd request!");
706 /* Fill in the transfer size; set active bit */
707 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
709 /* Enable interrupt for the last dtd of a request */
710 if (*is_last && !req->req.no_interrupt)
711 swap_temp |= DTD_IOC;
713 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
717 VDBG("length = %d address= 0x%x", *length, (int)*dma);
722 /* Generate dtd chain for a request */
723 static int fsl_req_to_dtd(struct fsl_req *req)
728 struct ep_td_struct *last_dtd = NULL, *dtd;
732 dtd = fsl_build_dtd(req, &count, &dma, &is_last);
740 last_dtd->next_td_ptr = cpu_to_le32(dma);
741 last_dtd->next_td_virt = dtd;
748 dtd->next_td_ptr = cpu_to_le32(DTD_NEXT_TERMINATE);
755 /* queues (submits) an I/O request to an endpoint */
757 fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
759 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
760 struct fsl_req *req = container_of(_req, struct fsl_req, req);
765 /* catch various bogus parameters */
766 if (!_req || !req->req.complete || !req->req.buf
767 || !list_empty(&req->queue)) {
768 VDBG("%s, bad params", __func__);
771 if (unlikely(!_ep || !ep->desc)) {
772 VDBG("%s, bad ep", __func__);
775 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
776 if (req->req.length > ep->ep.maxpacket)
782 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
787 /* map virtual address to hardware */
788 if (req->req.dma == DMA_ADDR_INVALID) {
789 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
791 req->req.length, ep_is_in(ep)
796 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
797 req->req.dma, req->req.length,
804 req->req.status = -EINPROGRESS;
808 spin_lock_irqsave(&udc->lock, flags);
810 /* build dtds and push them to device queue */
811 if (!fsl_req_to_dtd(req)) {
812 fsl_queue_td(ep, req);
814 spin_unlock_irqrestore(&udc->lock, flags);
818 /* Update ep0 state */
819 if ((ep_index(ep) == 0))
820 udc->ep0_state = DATA_STATE_XMIT;
822 /* irq handler advances the queue */
824 list_add_tail(&req->queue, &ep->queue);
825 spin_unlock_irqrestore(&udc->lock, flags);
830 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
831 static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
833 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
836 int ep_num, stopped, ret = 0;
842 spin_lock_irqsave(&ep->udc->lock, flags);
843 stopped = ep->stopped;
845 /* Stop the ep before we deal with the queue */
847 ep_num = ep_index(ep);
848 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
850 epctrl &= ~EPCTRL_TX_ENABLE;
852 epctrl &= ~EPCTRL_RX_ENABLE;
853 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
855 /* make sure it's actually queued on this endpoint */
856 list_for_each_entry(req, &ep->queue, queue) {
857 if (&req->req == _req)
860 if (&req->req != _req) {
865 /* The request is in progress, or completed but not dequeued */
866 if (ep->queue.next == &req->queue) {
867 _req->status = -ECONNRESET;
868 fsl_ep_fifo_flush(_ep); /* flush current transfer */
870 /* The request isn't the last request in this ep queue */
871 if (req->queue.next != &ep->queue) {
872 struct ep_queue_head *qh;
873 struct fsl_req *next_req;
876 next_req = list_entry(req->queue.next, struct fsl_req,
879 /* Point the QH to the first TD of next request */
880 fsl_writel((u32) next_req->head, &qh->curr_dtd_ptr);
883 /* The request hasn't been processed, patch up the TD chain */
885 struct fsl_req *prev_req;
887 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
888 fsl_writel(fsl_readl(&req->tail->next_td_ptr),
889 &prev_req->tail->next_td_ptr);
893 done(ep, req, -ECONNRESET);
896 out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
898 epctrl |= EPCTRL_TX_ENABLE;
900 epctrl |= EPCTRL_RX_ENABLE;
901 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
902 ep->stopped = stopped;
904 spin_unlock_irqrestore(&ep->udc->lock, flags);
908 /*-------------------------------------------------------------------------*/
910 /*-----------------------------------------------------------------
911 * modify the endpoint halt feature
912 * @ep: the non-isochronous endpoint being stalled
913 * @value: 1--set halt 0--clear halt
914 * Returns zero, or a negative error code.
915 *----------------------------------------------------------------*/
916 static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
918 struct fsl_ep *ep = NULL;
919 unsigned long flags = 0;
920 int status = -EOPNOTSUPP; /* operation not supported */
921 unsigned char ep_dir = 0, ep_num = 0;
922 struct fsl_udc *udc = NULL;
924 ep = container_of(_ep, struct fsl_ep, ep);
926 if (!_ep || !ep->desc) {
931 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
932 status = -EOPNOTSUPP;
936 /* Attempt to halt IN ep will fail if any transfer requests
938 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
944 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
945 ep_num = (unsigned char)(ep_index(ep));
946 spin_lock_irqsave(&ep->udc->lock, flags);
947 dr_ep_change_stall(ep_num, ep_dir, value);
948 spin_unlock_irqrestore(&ep->udc->lock, flags);
950 if (ep_index(ep) == 0) {
951 udc->ep0_state = WAIT_FOR_SETUP;
955 VDBG(" %s %s halt stat %d", ep->ep.name,
956 value ? "set" : "clear", status);
961 static void fsl_ep_fifo_flush(struct usb_ep *_ep)
966 unsigned long timeout;
967 #define FSL_UDC_FLUSH_TIMEOUT 1000
972 ep = container_of(_ep, struct fsl_ep, ep);
976 ep_num = ep_index(ep);
977 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
980 bits = (1 << 16) | 1;
981 else if (ep_dir == USB_SEND)
982 bits = 1 << (16 + ep_num);
986 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
988 fsl_writel(bits, &dr_regs->endptflush);
990 /* Wait until flush complete */
991 while (fsl_readl(&dr_regs->endptflush)) {
992 if (time_after(jiffies, timeout)) {
993 ERR("ep flush timeout\n");
998 /* See if we need to flush again */
999 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1002 static struct usb_ep_ops fsl_ep_ops = {
1003 .enable = fsl_ep_enable,
1004 .disable = fsl_ep_disable,
1006 .alloc_request = fsl_alloc_request,
1007 .free_request = fsl_free_request,
1009 .queue = fsl_ep_queue,
1010 .dequeue = fsl_ep_dequeue,
1012 .set_halt = fsl_ep_set_halt,
1013 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1016 /*-------------------------------------------------------------------------
1017 Gadget Driver Layer Operations
1018 -------------------------------------------------------------------------*/
1020 /*----------------------------------------------------------------------
1021 * Get the current frame number (from DR frame_index Reg )
1022 *----------------------------------------------------------------------*/
1023 static int fsl_get_frame(struct usb_gadget *gadget)
1025 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1028 /*-----------------------------------------------------------------------
1029 * Tries to wake up the host connected to this gadget
1030 -----------------------------------------------------------------------*/
1031 static int fsl_wakeup(struct usb_gadget *gadget)
1033 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1036 /* Remote wakeup feature not enabled by host */
1037 if (!udc->remote_wakeup)
1040 portsc = fsl_readl(&dr_regs->portsc1);
1041 /* not suspended? */
1042 if (!(portsc & PORTSCX_PORT_SUSPEND))
1044 /* trigger force resume */
1045 portsc |= PORTSCX_PORT_FORCE_RESUME;
1046 fsl_writel(portsc, &dr_regs->portsc1);
1050 static int can_pullup(struct fsl_udc *udc)
1052 return udc->driver && udc->softconnect && udc->vbus_active;
1055 /* Notify controller that VBUS is powered, Called by whatever
1056 detects VBUS sessions */
1057 static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1059 struct fsl_udc *udc;
1060 unsigned long flags;
1062 udc = container_of(gadget, struct fsl_udc, gadget);
1063 spin_lock_irqsave(&udc->lock, flags);
1064 VDBG("VBUS %s", is_active ? "on" : "off");
1065 udc->vbus_active = (is_active != 0);
1066 if (can_pullup(udc))
1067 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1070 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1072 spin_unlock_irqrestore(&udc->lock, flags);
1076 /* constrain controller's VBUS power usage
1077 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1078 * reporting how much power the device may consume. For example, this
1079 * could affect how quickly batteries are recharged.
1081 * Returns zero on success, else negative errno.
1083 static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1085 struct fsl_udc *udc;
1087 udc = container_of(gadget, struct fsl_udc, gadget);
1088 if (udc->transceiver)
1089 return otg_set_power(udc->transceiver, mA);
1093 /* Change Data+ pullup status
1094 * this func is used by usb_gadget_connect/disconnet
1096 static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1098 struct fsl_udc *udc;
1100 udc = container_of(gadget, struct fsl_udc, gadget);
1101 udc->softconnect = (is_on != 0);
1102 if (can_pullup(udc))
1103 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1106 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1112 /* defined in gadget.h */
1113 static struct usb_gadget_ops fsl_gadget_ops = {
1114 .get_frame = fsl_get_frame,
1115 .wakeup = fsl_wakeup,
1116 /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1117 .vbus_session = fsl_vbus_session,
1118 .vbus_draw = fsl_vbus_draw,
1119 .pullup = fsl_pullup,
1122 /* Set protocol stall on ep0, protocol stall will automatically be cleared
1123 on new transaction */
1124 static void ep0stall(struct fsl_udc *udc)
1128 /* must set tx and rx to stall at the same time */
1129 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1130 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1131 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1132 udc->ep0_state = WAIT_FOR_SETUP;
1136 /* Prime a status phase for ep0 */
1137 static int ep0_prime_status(struct fsl_udc *udc, int direction)
1139 struct fsl_req *req = udc->status_req;
1143 if (direction == EP_DIR_IN)
1144 udc->ep0_dir = USB_DIR_IN;
1146 udc->ep0_dir = USB_DIR_OUT;
1149 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1152 req->req.length = 0;
1153 req->req.status = -EINPROGRESS;
1154 req->req.actual = 0;
1155 req->req.complete = NULL;
1158 if (fsl_req_to_dtd(req) == 0)
1159 status = fsl_queue_td(ep, req);
1164 ERR("Can't queue ep0 status request\n");
1165 list_add_tail(&req->queue, &ep->queue);
1170 static inline int udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1172 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1177 nuke(ep, -ESHUTDOWN);
1185 static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1187 /* Save the new address to device struct */
1188 udc->device_address = (u8) value;
1189 /* Update usb state */
1190 udc->usb_state = USB_STATE_ADDRESS;
1192 if (ep0_prime_status(udc, EP_DIR_IN))
1199 static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1200 u16 index, u16 length)
1202 u16 tmp = 0; /* Status, cpu endian */
1204 struct fsl_req *req;
1210 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1211 /* Get device status */
1212 tmp = 1 << USB_DEVICE_SELF_POWERED;
1213 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1214 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1215 /* Get interface status */
1216 /* We don't have interface information in udc driver */
1218 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1219 /* Get endpoint status */
1220 struct fsl_ep *target_ep;
1222 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1224 /* stall if endpoint doesn't exist */
1225 if (!target_ep->desc)
1227 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1228 << USB_ENDPOINT_HALT;
1231 udc->ep0_dir = USB_DIR_IN;
1232 /* Borrow the per device status_req */
1233 req = udc->status_req;
1234 /* Fill in the reqest structure */
1235 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1237 req->req.length = 2;
1238 req->req.status = -EINPROGRESS;
1239 req->req.actual = 0;
1240 req->req.complete = NULL;
1243 /* prime the data phase */
1244 if ((fsl_req_to_dtd(req) == 0))
1245 status = fsl_queue_td(ep, req);
1250 ERR("Can't respond to getstatus request\n");
1253 list_add_tail(&req->queue, &ep->queue);
1254 udc->ep0_state = DATA_STATE_XMIT;
1260 static void setup_received_irq(struct fsl_udc *udc,
1261 struct usb_ctrlrequest *setup)
1263 u16 wValue = le16_to_cpu(setup->wValue);
1264 u16 wIndex = le16_to_cpu(setup->wIndex);
1265 u16 wLength = le16_to_cpu(setup->wLength);
1267 udc_reset_ep_queue(udc, 0);
1269 /* We process some stardard setup requests here */
1270 switch (setup->bRequest) {
1271 case USB_REQ_GET_STATUS:
1272 /* Data+Status phase from udc */
1273 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1274 != (USB_DIR_IN | USB_TYPE_STANDARD))
1276 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1279 case USB_REQ_SET_ADDRESS:
1280 /* Status phase from udc */
1281 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1282 | USB_RECIP_DEVICE))
1284 ch9setaddress(udc, wValue, wIndex, wLength);
1287 case USB_REQ_CLEAR_FEATURE:
1288 case USB_REQ_SET_FEATURE:
1289 /* Status phase from udc */
1291 int rc = -EOPNOTSUPP;
1293 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1294 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
1295 int pipe = get_pipe_by_windex(wIndex);
1298 if (wValue != 0 || wLength != 0 || pipe > udc->max_ep)
1300 ep = get_ep_by_pipe(udc, pipe);
1302 spin_unlock(&udc->lock);
1303 rc = fsl_ep_set_halt(&ep->ep,
1304 (setup->bRequest == USB_REQ_SET_FEATURE)
1306 spin_lock(&udc->lock);
1308 } else if ((setup->bRequestType & (USB_RECIP_MASK
1309 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
1310 | USB_TYPE_STANDARD)) {
1311 /* Note: The driver has not include OTG support yet.
1312 * This will be set when OTG support is added */
1313 if (!gadget_is_otg(&udc->gadget))
1315 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE)
1316 udc->gadget.b_hnp_enable = 1;
1317 else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
1318 udc->gadget.a_hnp_support = 1;
1319 else if (setup->bRequest ==
1320 USB_DEVICE_A_ALT_HNP_SUPPORT)
1321 udc->gadget.a_alt_hnp_support = 1;
1329 if (ep0_prime_status(udc, EP_DIR_IN))
1339 /* Requests handled by gadget */
1341 /* Data phase from gadget, status phase from udc */
1342 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1343 ? USB_DIR_IN : USB_DIR_OUT;
1344 spin_unlock(&udc->lock);
1345 if (udc->driver->setup(&udc->gadget,
1346 &udc->local_setup_buff) < 0)
1348 spin_lock(&udc->lock);
1349 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1350 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1352 /* No data phase, IN status from gadget */
1353 udc->ep0_dir = USB_DIR_IN;
1354 spin_unlock(&udc->lock);
1355 if (udc->driver->setup(&udc->gadget,
1356 &udc->local_setup_buff) < 0)
1358 spin_lock(&udc->lock);
1359 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1363 /* Process request for Data or Status phase of ep0
1364 * prime status phase if needed */
1365 static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1366 struct fsl_req *req)
1368 if (udc->usb_state == USB_STATE_ADDRESS) {
1369 /* Set the new address */
1370 u32 new_address = (u32) udc->device_address;
1371 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1372 &dr_regs->deviceaddr);
1377 switch (udc->ep0_state) {
1378 case DATA_STATE_XMIT:
1379 /* receive status phase */
1380 if (ep0_prime_status(udc, EP_DIR_OUT))
1383 case DATA_STATE_RECV:
1384 /* send status phase */
1385 if (ep0_prime_status(udc, EP_DIR_IN))
1388 case WAIT_FOR_OUT_STATUS:
1389 udc->ep0_state = WAIT_FOR_SETUP;
1391 case WAIT_FOR_SETUP:
1392 ERR("Unexpect ep0 packets\n");
1400 /* Tripwire mechanism to ensure a setup packet payload is extracted without
1401 * being corrupted by another incoming setup packet */
1402 static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1405 struct ep_queue_head *qh;
1407 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1409 /* Clear bit in ENDPTSETUPSTAT */
1410 temp = fsl_readl(&dr_regs->endptsetupstat);
1411 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1413 /* while a hazard exists when setup package arrives */
1415 /* Set Setup Tripwire */
1416 temp = fsl_readl(&dr_regs->usbcmd);
1417 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1419 /* Copy the setup packet to local buffer */
1420 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1421 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1423 /* Clear Setup Tripwire */
1424 temp = fsl_readl(&dr_regs->usbcmd);
1425 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1428 /* process-ep_req(): free the completed Tds for this req */
1429 static int process_ep_req(struct fsl_udc *udc, int pipe,
1430 struct fsl_req *curr_req)
1432 struct ep_td_struct *curr_td;
1433 int td_complete, actual, remaining_length, j, tmp;
1436 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1437 int direction = pipe % 2;
1439 curr_td = curr_req->head;
1441 actual = curr_req->req.length;
1443 for (j = 0; j < curr_req->dtd_count; j++) {
1444 remaining_length = (le32_to_cpu(curr_td->size_ioc_sts)
1446 >> DTD_LENGTH_BIT_POS;
1447 actual -= remaining_length;
1449 if ((errors = le32_to_cpu(curr_td->size_ioc_sts) &
1451 if (errors & DTD_STATUS_HALTED) {
1452 ERR("dTD error %08x QH=%d\n", errors, pipe);
1453 /* Clear the errors and Halt condition */
1454 tmp = le32_to_cpu(curr_qh->size_ioc_int_sts);
1456 curr_qh->size_ioc_int_sts = cpu_to_le32(tmp);
1458 /* FIXME: continue with next queued TD? */
1462 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1463 VDBG("Transfer overflow");
1466 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1471 ERR("Unknown error has occured (0x%x)!\n",
1474 } else if (le32_to_cpu(curr_td->size_ioc_sts)
1475 & DTD_STATUS_ACTIVE) {
1476 VDBG("Request not complete");
1477 status = REQ_UNCOMPLETE;
1479 } else if (remaining_length) {
1481 VDBG("Transmit dTD remaining length not zero");
1490 VDBG("dTD transmitted successful");
1493 if (j != curr_req->dtd_count - 1)
1494 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1500 curr_req->req.actual = actual;
1505 /* Process a DTD completion interrupt */
1506 static void dtd_complete_irq(struct fsl_udc *udc)
1509 int i, ep_num, direction, bit_mask, status;
1510 struct fsl_ep *curr_ep;
1511 struct fsl_req *curr_req, *temp_req;
1513 /* Clear the bits in the register */
1514 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1515 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1520 for (i = 0; i < udc->max_ep * 2; i++) {
1524 bit_mask = 1 << (ep_num + 16 * direction);
1526 if (!(bit_pos & bit_mask))
1529 curr_ep = get_ep_by_pipe(udc, i);
1531 /* If the ep is configured */
1532 if (curr_ep->name == NULL) {
1533 WARNING("Invalid EP?");
1537 /* process the req queue until an uncomplete request */
1538 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1540 status = process_ep_req(udc, i, curr_req);
1542 VDBG("status of process_ep_req= %d, ep = %d",
1544 if (status == REQ_UNCOMPLETE)
1546 /* write back status to req */
1547 curr_req->req.status = status;
1550 ep0_req_complete(udc, curr_ep, curr_req);
1553 done(curr_ep, curr_req, status);
1558 /* Process a port change interrupt */
1559 static void port_change_irq(struct fsl_udc *udc)
1563 /* Bus resetting is finished */
1564 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) {
1566 speed = (fsl_readl(&dr_regs->portsc1)
1567 & PORTSCX_PORT_SPEED_MASK);
1569 case PORTSCX_PORT_SPEED_HIGH:
1570 udc->gadget.speed = USB_SPEED_HIGH;
1572 case PORTSCX_PORT_SPEED_FULL:
1573 udc->gadget.speed = USB_SPEED_FULL;
1575 case PORTSCX_PORT_SPEED_LOW:
1576 udc->gadget.speed = USB_SPEED_LOW;
1579 udc->gadget.speed = USB_SPEED_UNKNOWN;
1584 /* Update USB state */
1585 if (!udc->resume_state)
1586 udc->usb_state = USB_STATE_DEFAULT;
1589 /* Process suspend interrupt */
1590 static void suspend_irq(struct fsl_udc *udc)
1592 udc->resume_state = udc->usb_state;
1593 udc->usb_state = USB_STATE_SUSPENDED;
1595 /* report suspend to the driver, serial.c does not support this */
1596 if (udc->driver->suspend)
1597 udc->driver->suspend(&udc->gadget);
1600 static void bus_resume(struct fsl_udc *udc)
1602 udc->usb_state = udc->resume_state;
1603 udc->resume_state = 0;
1605 /* report resume to the driver, serial.c does not support this */
1606 if (udc->driver->resume)
1607 udc->driver->resume(&udc->gadget);
1610 /* Clear up all ep queues */
1611 static int reset_queues(struct fsl_udc *udc)
1615 for (pipe = 0; pipe < udc->max_pipes; pipe++)
1616 udc_reset_ep_queue(udc, pipe);
1618 /* report disconnect; the driver is already quiesced */
1619 spin_unlock(&udc->lock);
1620 udc->driver->disconnect(&udc->gadget);
1621 spin_lock(&udc->lock);
1626 /* Process reset interrupt */
1627 static void reset_irq(struct fsl_udc *udc)
1630 unsigned long timeout;
1632 /* Clear the device address */
1633 temp = fsl_readl(&dr_regs->deviceaddr);
1634 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
1636 udc->device_address = 0;
1638 /* Clear usb state */
1639 udc->resume_state = 0;
1641 udc->ep0_state = WAIT_FOR_SETUP;
1642 udc->remote_wakeup = 0; /* default to 0 on reset */
1643 udc->gadget.b_hnp_enable = 0;
1644 udc->gadget.a_hnp_support = 0;
1645 udc->gadget.a_alt_hnp_support = 0;
1647 /* Clear all the setup token semaphores */
1648 temp = fsl_readl(&dr_regs->endptsetupstat);
1649 fsl_writel(temp, &dr_regs->endptsetupstat);
1651 /* Clear all the endpoint complete status bits */
1652 temp = fsl_readl(&dr_regs->endptcomplete);
1653 fsl_writel(temp, &dr_regs->endptcomplete);
1655 timeout = jiffies + 100;
1656 while (fsl_readl(&dr_regs->endpointprime)) {
1657 /* Wait until all endptprime bits cleared */
1658 if (time_after(jiffies, timeout)) {
1659 ERR("Timeout for reset\n");
1665 /* Write 1s to the flush register */
1666 fsl_writel(0xffffffff, &dr_regs->endptflush);
1668 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1670 /* Reset all the queues, include XD, dTD, EP queue
1671 * head and TR Queue */
1673 udc->usb_state = USB_STATE_DEFAULT;
1675 VDBG("Controller reset");
1676 /* initialize usb hw reg except for regs for EP, not
1677 * touch usbintr reg */
1678 dr_controller_setup(udc);
1680 /* Reset all internal used Queues */
1685 /* Enable DR IRQ reg, Set Run bit, change udc state */
1686 dr_controller_run(udc);
1687 udc->usb_state = USB_STATE_ATTACHED;
1692 * USB device controller interrupt handler
1694 static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1696 struct fsl_udc *udc = _udc;
1698 irqreturn_t status = IRQ_NONE;
1699 unsigned long flags;
1701 /* Disable ISR for OTG host mode */
1704 spin_lock_irqsave(&udc->lock, flags);
1705 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
1706 /* Clear notification bits */
1707 fsl_writel(irq_src, &dr_regs->usbsts);
1709 /* VDBG("irq_src [0x%8x]", irq_src); */
1711 /* Need to resume? */
1712 if (udc->usb_state == USB_STATE_SUSPENDED)
1713 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
1717 if (irq_src & USB_STS_INT) {
1719 /* Setup package, we only support ep0 as control ep */
1720 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
1721 tripwire_handler(udc, 0,
1722 (u8 *) (&udc->local_setup_buff));
1723 setup_received_irq(udc, &udc->local_setup_buff);
1724 status = IRQ_HANDLED;
1727 /* completion of dtd */
1728 if (fsl_readl(&dr_regs->endptcomplete)) {
1729 dtd_complete_irq(udc);
1730 status = IRQ_HANDLED;
1734 /* SOF (for ISO transfer) */
1735 if (irq_src & USB_STS_SOF) {
1736 status = IRQ_HANDLED;
1740 if (irq_src & USB_STS_PORT_CHANGE) {
1741 port_change_irq(udc);
1742 status = IRQ_HANDLED;
1745 /* Reset Received */
1746 if (irq_src & USB_STS_RESET) {
1748 status = IRQ_HANDLED;
1751 /* Sleep Enable (Suspend) */
1752 if (irq_src & USB_STS_SUSPEND) {
1754 status = IRQ_HANDLED;
1757 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
1758 VDBG("Error IRQ %x", irq_src);
1761 spin_unlock_irqrestore(&udc->lock, flags);
1765 /*----------------------------------------------------------------*
1766 * Hook to gadget drivers
1767 * Called by initialization code of gadget drivers
1768 *----------------------------------------------------------------*/
1769 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1771 int retval = -ENODEV;
1772 unsigned long flags = 0;
1774 if (!udc_controller)
1777 if (!driver || (driver->speed != USB_SPEED_FULL
1778 && driver->speed != USB_SPEED_HIGH)
1779 || !driver->bind || !driver->disconnect
1783 if (udc_controller->driver)
1786 /* lock is needed but whether should use this lock or another */
1787 spin_lock_irqsave(&udc_controller->lock, flags);
1789 driver->driver.bus = NULL;
1790 /* hook up the driver */
1791 udc_controller->driver = driver;
1792 udc_controller->gadget.dev.driver = &driver->driver;
1793 spin_unlock_irqrestore(&udc_controller->lock, flags);
1795 /* bind udc driver to gadget driver */
1796 retval = driver->bind(&udc_controller->gadget);
1798 VDBG("bind to %s --> %d", driver->driver.name, retval);
1799 udc_controller->gadget.dev.driver = NULL;
1800 udc_controller->driver = NULL;
1804 /* Enable DR IRQ reg and Set usbcmd reg Run bit */
1805 dr_controller_run(udc_controller);
1806 udc_controller->usb_state = USB_STATE_ATTACHED;
1807 udc_controller->ep0_state = WAIT_FOR_SETUP;
1808 udc_controller->ep0_dir = 0;
1809 printk(KERN_INFO "%s: bind to driver %s\n",
1810 udc_controller->gadget.name, driver->driver.name);
1814 printk("gadget driver register failed %d\n", retval);
1817 EXPORT_SYMBOL(usb_gadget_register_driver);
1819 /* Disconnect from gadget driver */
1820 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1822 struct fsl_ep *loop_ep;
1823 unsigned long flags;
1825 if (!udc_controller)
1828 if (!driver || driver != udc_controller->driver || !driver->unbind)
1831 if (udc_controller->transceiver)
1832 otg_set_peripheral(udc_controller->transceiver, NULL);
1834 /* stop DR, disable intr */
1835 dr_controller_stop(udc_controller);
1837 /* in fact, no needed */
1838 udc_controller->usb_state = USB_STATE_ATTACHED;
1839 udc_controller->ep0_state = WAIT_FOR_SETUP;
1840 udc_controller->ep0_dir = 0;
1842 /* stand operation */
1843 spin_lock_irqsave(&udc_controller->lock, flags);
1844 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
1845 nuke(&udc_controller->eps[0], -ESHUTDOWN);
1846 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
1848 nuke(loop_ep, -ESHUTDOWN);
1849 spin_unlock_irqrestore(&udc_controller->lock, flags);
1851 /* unbind gadget and unhook driver. */
1852 driver->unbind(&udc_controller->gadget);
1853 udc_controller->gadget.dev.driver = NULL;
1854 udc_controller->driver = NULL;
1856 printk("unregistered gadget driver '%s'\n", driver->driver.name);
1859 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1861 /*-------------------------------------------------------------------------
1862 PROC File System Support
1863 -------------------------------------------------------------------------*/
1864 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1866 #include <linux/seq_file.h>
1868 static const char proc_filename[] = "driver/fsl_usb2_udc";
1870 static int fsl_proc_read(char *page, char **start, off_t off, int count,
1871 int *eof, void *_dev)
1875 unsigned size = count;
1876 unsigned long flags;
1879 struct fsl_ep *ep = NULL;
1880 struct fsl_req *req;
1882 struct fsl_udc *udc = udc_controller;
1886 spin_lock_irqsave(&udc->lock, flags);
1888 /* ------basic driver information ---- */
1889 t = scnprintf(next, size,
1892 "Gadget driver: %s\n\n",
1893 driver_name, DRIVER_VERSION,
1894 udc->driver ? udc->driver->driver.name : "(none)");
1898 /* ------ DR Registers ----- */
1899 tmp_reg = fsl_readl(&dr_regs->usbcmd);
1900 t = scnprintf(next, size,
1904 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
1905 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
1909 tmp_reg = fsl_readl(&dr_regs->usbsts);
1910 t = scnprintf(next, size,
1912 "Dr Suspend: %d" "Reset Received: %d" "System Error: %s"
1913 "USB Error Interrupt: %s\n\n",
1914 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
1915 (tmp_reg & USB_STS_RESET) ? 1 : 0,
1916 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
1917 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
1921 tmp_reg = fsl_readl(&dr_regs->usbintr);
1922 t = scnprintf(next, size,
1923 "USB Intrrupt Enable Reg:\n"
1924 "Sleep Enable: %d" "SOF Received Enable: %d"
1925 "Reset Enable: %d\n"
1926 "System Error Enable: %d"
1927 "Port Change Dectected Enable: %d\n"
1928 "USB Error Intr Enable: %d" "USB Intr Enable: %d\n\n",
1929 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
1930 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
1931 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
1932 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
1933 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
1934 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
1935 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
1939 tmp_reg = fsl_readl(&dr_regs->frindex);
1940 t = scnprintf(next, size,
1941 "USB Frame Index Reg:" "Frame Number is 0x%x\n\n",
1942 (tmp_reg & USB_FRINDEX_MASKS));
1946 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
1947 t = scnprintf(next, size,
1948 "USB Device Address Reg:" "Device Addr is 0x%x\n\n",
1949 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
1953 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
1954 t = scnprintf(next, size,
1955 "USB Endpoint List Address Reg:"
1956 "Device Addr is 0x%x\n\n",
1957 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
1961 tmp_reg = fsl_readl(&dr_regs->portsc1);
1962 t = scnprintf(next, size,
1963 "USB Port Status&Control Reg:\n"
1964 "Port Transceiver Type : %s" "Port Speed: %s \n"
1965 "PHY Low Power Suspend: %s" "Port Reset: %s"
1966 "Port Suspend Mode: %s \n" "Over-current Change: %s"
1967 "Port Enable/Disable Change: %s\n"
1968 "Port Enabled/Disabled: %s"
1969 "Current Connect Status: %s\n\n", ( {
1971 switch (tmp_reg & PORTSCX_PTS_FSLS) {
1972 case PORTSCX_PTS_UTMI:
1974 case PORTSCX_PTS_ULPI:
1976 case PORTSCX_PTS_FSLS:
1977 s = "FS/LS Serial"; break;
1983 switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
1984 case PORTSCX_PORT_SPEED_FULL:
1985 s = "Full Speed"; break;
1986 case PORTSCX_PORT_SPEED_LOW:
1987 s = "Low Speed"; break;
1988 case PORTSCX_PORT_SPEED_HIGH:
1989 s = "High Speed"; break;
1991 s = "Undefined"; break;
1995 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
1996 "Normal PHY mode" : "Low power mode",
1997 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
1999 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
2000 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
2002 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
2004 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
2006 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
2007 "Attached" : "Not-Att");
2011 tmp_reg = fsl_readl(&dr_regs->usbmode);
2012 t = scnprintf(next, size,
2013 "USB Mode Reg:" "Controller Mode is : %s\n\n", ( {
2015 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2016 case USB_MODE_CTRL_MODE_IDLE:
2018 case USB_MODE_CTRL_MODE_DEVICE:
2019 s = "Device Controller"; break;
2020 case USB_MODE_CTRL_MODE_HOST:
2021 s = "Host Controller"; break;
2030 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2031 t = scnprintf(next, size,
2032 "Endpoint Setup Status Reg:" "SETUP on ep 0x%x\n\n",
2033 (tmp_reg & EP_SETUP_STATUS_MASK));
2037 for (i = 0; i < udc->max_ep / 2; i++) {
2038 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2039 t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
2044 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2045 t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n", tmp_reg);
2049 tmp_reg = usb_sys_regs->snoop1;
2050 t = scnprintf(next, size, "\nSnoop1 Reg : = [0x%x]\n\n", tmp_reg);
2054 tmp_reg = usb_sys_regs->control;
2055 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
2060 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2062 t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
2063 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2067 if (list_empty(&ep->queue)) {
2068 t = scnprintf(next, size, "its req queue is empty\n\n");
2072 list_for_each_entry(req, &ep->queue, queue) {
2073 t = scnprintf(next, size,
2074 "req %p actual 0x%x length 0x%x buf %p\n",
2075 &req->req, req->req.actual,
2076 req->req.length, req->req.buf);
2081 /* other gadget->eplist ep */
2082 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2084 t = scnprintf(next, size,
2085 "\nFor %s Maxpkt is 0x%x "
2087 ep->ep.name, ep_maxpacket(ep),
2092 if (list_empty(&ep->queue)) {
2093 t = scnprintf(next, size,
2094 "its req queue is empty\n\n");
2098 list_for_each_entry(req, &ep->queue, queue) {
2099 t = scnprintf(next, size,
2100 "req %p actual 0x%x length"
2102 &req->req, req->req.actual,
2103 req->req.length, req->req.buf);
2106 } /* end for each_entry of ep req */
2107 } /* end for else */
2108 } /* end for if(ep->queue) */
2109 } /* end (ep->desc) */
2111 spin_unlock_irqrestore(&udc->lock, flags);
2114 return count - size;
2117 #define create_proc_file() create_proc_read_entry(proc_filename, \
2118 0, NULL, fsl_proc_read, NULL)
2120 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2122 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2124 #define create_proc_file() do {} while (0)
2125 #define remove_proc_file() do {} while (0)
2127 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2129 /*-------------------------------------------------------------------------*/
2131 /* Release udc structures */
2132 static void fsl_udc_release(struct device *dev)
2134 complete(udc_controller->done);
2135 dma_free_coherent(dev, udc_controller->ep_qh_size,
2136 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2137 kfree(udc_controller);
2140 /******************************************************************
2141 Internal structure setup functions
2142 *******************************************************************/
2143 /*------------------------------------------------------------------
2144 * init resource for globle controller
2145 * Return the udc handle on success or NULL on failure
2146 ------------------------------------------------------------------*/
2147 static int __init struct_udc_setup(struct fsl_udc *udc,
2148 struct platform_device *pdev)
2150 struct fsl_usb2_platform_data *pdata;
2153 pdata = pdev->dev.platform_data;
2154 udc->phy_mode = pdata->phy_mode;
2156 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2158 ERR("malloc fsl_ep failed\n");
2162 /* initialized QHs, take care of alignment */
2163 size = udc->max_ep * sizeof(struct ep_queue_head);
2164 if (size < QH_ALIGNMENT)
2165 size = QH_ALIGNMENT;
2166 else if ((size % QH_ALIGNMENT) != 0) {
2167 size += QH_ALIGNMENT + 1;
2168 size &= ~(QH_ALIGNMENT - 1);
2170 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2171 &udc->ep_qh_dma, GFP_KERNEL);
2173 ERR("malloc QHs for udc failed\n");
2178 udc->ep_qh_size = size;
2180 /* Initialize ep0 status request structure */
2181 /* FIXME: fsl_alloc_request() ignores ep argument */
2182 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2183 struct fsl_req, req);
2184 /* allocate a small amount of memory to get valid address */
2185 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2186 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2188 udc->resume_state = USB_STATE_NOTATTACHED;
2189 udc->usb_state = USB_STATE_POWERED;
2191 udc->remote_wakeup = 0; /* default to 0 on reset */
2192 spin_lock_init(&udc->lock);
2197 /*----------------------------------------------------------------
2198 * Setup the fsl_ep struct for eps
2199 * Link fsl_ep->ep to gadget->ep_list
2200 * ep0out is not used so do nothing here
2201 * ep0in should be taken care
2202 *--------------------------------------------------------------*/
2203 static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2204 char *name, int link)
2206 struct fsl_ep *ep = &udc->eps[index];
2209 strcpy(ep->name, name);
2210 ep->ep.name = ep->name;
2212 ep->ep.ops = &fsl_ep_ops;
2215 /* for ep0: maxP defined in desc
2216 * for other eps, maxP is set by epautoconfig() called by gadget layer
2218 ep->ep.maxpacket = (unsigned short) ~0;
2220 /* the queue lists any req for this ep */
2221 INIT_LIST_HEAD(&ep->queue);
2223 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
2225 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2226 ep->gadget = &udc->gadget;
2227 ep->qh = &udc->ep_qh[index];
2232 /* Driver probe function
2233 * all intialization operations implemented here except enabling usb_intr reg
2234 * board setup should have been done in the platform code
2236 static int __init fsl_udc_probe(struct platform_device *pdev)
2238 struct resource *res;
2243 if (strcmp(pdev->name, driver_name)) {
2244 VDBG("Wrong device");
2248 udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
2249 if (udc_controller == NULL) {
2250 ERR("malloc udc failed\n");
2254 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2256 kfree(udc_controller);
2260 if (!request_mem_region(res->start, res->end - res->start + 1,
2262 ERR("request mem region for %s failed\n", pdev->name);
2263 kfree(udc_controller);
2267 dr_regs = ioremap(res->start, res->end - res->start + 1);
2273 usb_sys_regs = (struct usb_sys_interface *)
2274 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2276 /* Read Device Controller Capability Parameters register */
2277 dccparams = fsl_readl(&dr_regs->dccparams);
2278 if (!(dccparams & DCCPARAMS_DC)) {
2279 ERR("This SOC doesn't support device role\n");
2283 /* Get max device endpoints */
2284 /* DEN is bidirectional ep number, max_ep doubles the number */
2285 udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
2287 udc_controller->irq = platform_get_irq(pdev, 0);
2288 if (!udc_controller->irq) {
2293 ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
2294 driver_name, udc_controller);
2296 ERR("cannot request irq %d err %d\n",
2297 udc_controller->irq, ret);
2301 /* Initialize the udc structure including QH member and other member */
2302 if (struct_udc_setup(udc_controller, pdev)) {
2303 ERR("Can't initialize udc data structure\n");
2308 /* initialize usb hw reg except for regs for EP,
2309 * leave usbintr reg untouched */
2310 dr_controller_setup(udc_controller);
2312 /* Setup gadget structure */
2313 udc_controller->gadget.ops = &fsl_gadget_ops;
2314 udc_controller->gadget.is_dualspeed = 1;
2315 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2316 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2317 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2318 udc_controller->gadget.name = driver_name;
2320 /* Setup gadget.dev and register with kernel */
2321 dev_set_name(&udc_controller->gadget.dev, "gadget");
2322 udc_controller->gadget.dev.release = fsl_udc_release;
2323 udc_controller->gadget.dev.parent = &pdev->dev;
2324 ret = device_register(&udc_controller->gadget.dev);
2328 /* setup QH and epctrl for ep0 */
2329 ep0_setup(udc_controller);
2331 /* setup udc->eps[] for ep0 */
2332 struct_ep_setup(udc_controller, 0, "ep0", 0);
2333 /* for ep0: the desc defined here;
2334 * for other eps, gadget layer called ep_enable with defined desc
2336 udc_controller->eps[0].desc = &fsl_ep0_desc;
2337 udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
2339 /* setup the udc->eps[] for non-control endpoints and link
2340 * to gadget.ep_list */
2341 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
2344 sprintf(name, "ep%dout", i);
2345 struct_ep_setup(udc_controller, i * 2, name, 1);
2346 sprintf(name, "ep%din", i);
2347 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2350 /* use dma_pool for TD management */
2351 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
2352 sizeof(struct ep_td_struct),
2353 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2354 if (udc_controller->td_pool == NULL) {
2362 device_unregister(&udc_controller->gadget.dev);
2364 free_irq(udc_controller->irq, udc_controller);
2368 release_mem_region(res->start, res->end - res->start + 1);
2369 kfree(udc_controller);
2373 /* Driver removal function
2374 * Free resources and finish pending transactions
2376 static int __exit fsl_udc_remove(struct platform_device *pdev)
2378 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2380 DECLARE_COMPLETION(done);
2382 if (!udc_controller)
2384 udc_controller->done = &done;
2386 /* DR has been stopped in usb_gadget_unregister_driver() */
2389 /* Free allocated memory */
2390 kfree(udc_controller->status_req->req.buf);
2391 kfree(udc_controller->status_req);
2392 kfree(udc_controller->eps);
2394 dma_pool_destroy(udc_controller->td_pool);
2395 free_irq(udc_controller->irq, udc_controller);
2397 release_mem_region(res->start, res->end - res->start + 1);
2399 device_unregister(&udc_controller->gadget.dev);
2400 /* free udc --wait for the release() finished */
2401 wait_for_completion(&done);
2406 /*-----------------------------------------------------------------
2407 * Modify Power management attributes
2408 * Used by OTG statemachine to disable gadget temporarily
2409 -----------------------------------------------------------------*/
2410 static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
2412 dr_controller_stop(udc_controller);
2416 /*-----------------------------------------------------------------
2417 * Invoked on USB resume. May be called in_interrupt.
2418 * Here we start the DR controller and enable the irq
2419 *-----------------------------------------------------------------*/
2420 static int fsl_udc_resume(struct platform_device *pdev)
2422 /* Enable DR irq reg and set controller Run */
2423 if (udc_controller->stopped) {
2424 dr_controller_setup(udc_controller);
2425 dr_controller_run(udc_controller);
2427 udc_controller->usb_state = USB_STATE_ATTACHED;
2428 udc_controller->ep0_state = WAIT_FOR_SETUP;
2429 udc_controller->ep0_dir = 0;
2433 /*-------------------------------------------------------------------------
2434 Register entry point for the peripheral controller driver
2435 --------------------------------------------------------------------------*/
2437 static struct platform_driver udc_driver = {
2438 .remove = __exit_p(fsl_udc_remove),
2439 /* these suspend and resume are not usb suspend and resume */
2440 .suspend = fsl_udc_suspend,
2441 .resume = fsl_udc_resume,
2443 .name = (char *)driver_name,
2444 .owner = THIS_MODULE,
2448 static int __init udc_init(void)
2450 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
2451 return platform_driver_probe(&udc_driver, fsl_udc_probe);
2454 module_init(udc_init);
2456 static void __exit udc_exit(void)
2458 platform_driver_unregister(&udc_driver);
2459 printk("%s unregistered\n", driver_desc);
2462 module_exit(udc_exit);
2464 MODULE_DESCRIPTION(DRIVER_DESC);
2465 MODULE_AUTHOR(DRIVER_AUTHOR);
2466 MODULE_LICENSE("GPL");
2467 MODULE_ALIAS("platform:fsl-usb2-udc");