2 * Copyright (C) 2004-2007,2011-2012 Freescale Semiconductor, Inc.
5 * Author: Li Yang <leoli@freescale.com>
6 * Jiang Bo <tanya.jiang@freescale.com>
9 * Freescale high-speed USB SOC DR module device controller driver.
10 * This can be found on MPC8349E/MPC8313E/MPC5121E cpus.
11 * The driver is previously named as mpc_udc. Based on bare board
12 * code from Dave Liu and Shlomi Gridish.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/ioport.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
32 #include <linux/proc_fs.h>
34 #include <linux/moduleparam.h>
35 #include <linux/device.h>
36 #include <linux/usb/ch9.h>
37 #include <linux/usb/gadget.h>
38 #include <linux/usb/otg.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/platform_device.h>
41 #include <linux/fsl_devices.h>
42 #include <linux/dmapool.h>
43 #include <linux/delay.h>
44 #include <linux/of_device.h>
46 #include <asm/byteorder.h>
48 #include <asm/unaligned.h>
51 #include "fsl_usb2_udc.h"
53 #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
54 #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
55 #define DRIVER_VERSION "Apr 20, 2007"
57 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
59 static const char driver_name
[] = "fsl-usb2-udc";
60 static const char driver_desc
[] = DRIVER_DESC
;
62 static struct usb_dr_device __iomem
*dr_regs
;
64 static struct usb_sys_interface __iomem
*usb_sys_regs
;
66 /* it is initialized in probe() */
67 static struct fsl_udc
*udc_controller
= NULL
;
69 static const struct usb_endpoint_descriptor
71 .bLength
= USB_DT_ENDPOINT_SIZE
,
72 .bDescriptorType
= USB_DT_ENDPOINT
,
73 .bEndpointAddress
= 0,
74 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
75 .wMaxPacketSize
= USB_MAX_CTRL_PAYLOAD
,
78 static void fsl_ep_fifo_flush(struct usb_ep
*_ep
);
82 * On some SoCs, the USB controller registers can be big or little endian,
83 * depending on the version of the chip. In order to be able to run the
84 * same kernel binary on 2 different versions of an SoC, the BE/LE decision
85 * must be made at run time. _fsl_readl and fsl_writel are pointers to the
86 * BE or LE readl() and writel() functions, and fsl_readl() and fsl_writel()
87 * call through those pointers. Platform code for SoCs that have BE USB
88 * registers should set pdata->big_endian_mmio flag.
90 * This also applies to controller-to-cpu accessors for the USB descriptors,
91 * since their endianness is also SoC dependant. Platform code for SoCs that
92 * have BE USB descriptors should set pdata->big_endian_desc flag.
94 static u32
_fsl_readl_be(const unsigned __iomem
*p
)
99 static u32
_fsl_readl_le(const unsigned __iomem
*p
)
104 static void _fsl_writel_be(u32 v
, unsigned __iomem
*p
)
109 static void _fsl_writel_le(u32 v
, unsigned __iomem
*p
)
114 static u32 (*_fsl_readl
)(const unsigned __iomem
*p
);
115 static void (*_fsl_writel
)(u32 v
, unsigned __iomem
*p
);
117 #define fsl_readl(p) (*_fsl_readl)((p))
118 #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
120 static inline void fsl_set_accessors(struct fsl_usb2_platform_data
*pdata
)
122 if (pdata
->big_endian_mmio
) {
123 _fsl_readl
= _fsl_readl_be
;
124 _fsl_writel
= _fsl_writel_be
;
126 _fsl_readl
= _fsl_readl_le
;
127 _fsl_writel
= _fsl_writel_le
;
131 static inline u32
cpu_to_hc32(const u32 x
)
133 return udc_controller
->pdata
->big_endian_desc
134 ? (__force u32
)cpu_to_be32(x
)
135 : (__force u32
)cpu_to_le32(x
);
138 static inline u32
hc32_to_cpu(const u32 x
)
140 return udc_controller
->pdata
->big_endian_desc
141 ? be32_to_cpu((__force __be32
)x
)
142 : le32_to_cpu((__force __le32
)x
);
144 #else /* !CONFIG_PPC32 */
145 static inline void fsl_set_accessors(struct fsl_usb2_platform_data
*pdata
) {}
147 #define fsl_readl(addr) readl(addr)
148 #define fsl_writel(val32, addr) writel(val32, addr)
149 #define cpu_to_hc32(x) cpu_to_le32(x)
150 #define hc32_to_cpu(x) le32_to_cpu(x)
151 #endif /* CONFIG_PPC32 */
153 /********************************************************************
154 * Internal Used Function
155 ********************************************************************/
156 /*-----------------------------------------------------------------
157 * done() - retire a request; caller blocked irqs
158 * @status : request status to be set, only works when
159 * request is still in progress.
160 *--------------------------------------------------------------*/
161 static void done(struct fsl_ep
*ep
, struct fsl_req
*req
, int status
)
162 __releases(ep
->udc
->lock
)
163 __acquires(ep
->udc
->lock
)
165 struct fsl_udc
*udc
= NULL
;
166 unsigned char stopped
= ep
->stopped
;
167 struct ep_td_struct
*curr_td
, *next_td
;
170 udc
= (struct fsl_udc
*)ep
->udc
;
171 /* Removed the req from fsl_ep->queue */
172 list_del_init(&req
->queue
);
174 /* req.status should be set as -EINPROGRESS in ep_queue() */
175 if (req
->req
.status
== -EINPROGRESS
)
176 req
->req
.status
= status
;
178 status
= req
->req
.status
;
180 /* Free dtd for the request */
182 for (j
= 0; j
< req
->dtd_count
; j
++) {
184 if (j
!= req
->dtd_count
- 1) {
185 next_td
= curr_td
->next_td_virt
;
187 dma_pool_free(udc
->td_pool
, curr_td
, curr_td
->td_dma
);
190 usb_gadget_unmap_request(&ep
->udc
->gadget
, &req
->req
, ep_is_in(ep
));
192 if (status
&& (status
!= -ESHUTDOWN
))
193 VDBG("complete %s req %p stat %d len %u/%u",
194 ep
->ep
.name
, &req
->req
, status
,
195 req
->req
.actual
, req
->req
.length
);
199 spin_unlock(&ep
->udc
->lock
);
201 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
203 spin_lock(&ep
->udc
->lock
);
204 ep
->stopped
= stopped
;
207 /*-----------------------------------------------------------------
208 * nuke(): delete all requests related to this ep
209 * called with spinlock held
210 *--------------------------------------------------------------*/
211 static void nuke(struct fsl_ep
*ep
, int status
)
216 fsl_ep_fifo_flush(&ep
->ep
);
218 /* Whether this eq has request linked */
219 while (!list_empty(&ep
->queue
)) {
220 struct fsl_req
*req
= NULL
;
222 req
= list_entry(ep
->queue
.next
, struct fsl_req
, queue
);
223 done(ep
, req
, status
);
227 /*------------------------------------------------------------------
228 Internal Hardware related function
229 ------------------------------------------------------------------*/
231 static int dr_controller_setup(struct fsl_udc
*udc
)
233 unsigned int tmp
, portctrl
, ep_num
;
234 unsigned int max_no_of_ep
;
236 unsigned long timeout
;
238 #define FSL_UDC_RESET_TIMEOUT 1000
240 /* Config PHY interface */
241 portctrl
= fsl_readl(&dr_regs
->portsc1
);
242 portctrl
&= ~(PORTSCX_PHY_TYPE_SEL
| PORTSCX_PORT_WIDTH
);
243 switch (udc
->phy_mode
) {
244 case FSL_USB2_PHY_ULPI
:
245 if (udc
->pdata
->have_sysif_regs
) {
246 if (udc
->pdata
->controller_ver
) {
247 /* controller version 1.6 or above */
248 ctrl
= __raw_readl(&usb_sys_regs
->control
);
249 ctrl
&= ~USB_CTRL_UTMI_PHY_EN
;
250 ctrl
|= USB_CTRL_USB_EN
;
251 __raw_writel(ctrl
, &usb_sys_regs
->control
);
254 portctrl
|= PORTSCX_PTS_ULPI
;
256 case FSL_USB2_PHY_UTMI_WIDE
:
257 portctrl
|= PORTSCX_PTW_16BIT
;
259 case FSL_USB2_PHY_UTMI
:
260 if (udc
->pdata
->have_sysif_regs
) {
261 if (udc
->pdata
->controller_ver
) {
262 /* controller version 1.6 or above */
263 ctrl
= __raw_readl(&usb_sys_regs
->control
);
264 ctrl
|= (USB_CTRL_UTMI_PHY_EN
|
266 __raw_writel(ctrl
, &usb_sys_regs
->control
);
267 mdelay(FSL_UTMI_PHY_DLY
); /* Delay for UTMI
268 PHY CLK to become stable - 10ms*/
271 portctrl
|= PORTSCX_PTS_UTMI
;
273 case FSL_USB2_PHY_SERIAL
:
274 portctrl
|= PORTSCX_PTS_FSLS
;
279 fsl_writel(portctrl
, &dr_regs
->portsc1
);
281 /* Stop and reset the usb controller */
282 tmp
= fsl_readl(&dr_regs
->usbcmd
);
283 tmp
&= ~USB_CMD_RUN_STOP
;
284 fsl_writel(tmp
, &dr_regs
->usbcmd
);
286 tmp
= fsl_readl(&dr_regs
->usbcmd
);
287 tmp
|= USB_CMD_CTRL_RESET
;
288 fsl_writel(tmp
, &dr_regs
->usbcmd
);
290 /* Wait for reset to complete */
291 timeout
= jiffies
+ FSL_UDC_RESET_TIMEOUT
;
292 while (fsl_readl(&dr_regs
->usbcmd
) & USB_CMD_CTRL_RESET
) {
293 if (time_after(jiffies
, timeout
)) {
294 ERR("udc reset timeout!\n");
300 /* Set the controller as device mode */
301 tmp
= fsl_readl(&dr_regs
->usbmode
);
302 tmp
&= ~USB_MODE_CTRL_MODE_MASK
; /* clear mode bits */
303 tmp
|= USB_MODE_CTRL_MODE_DEVICE
;
304 /* Disable Setup Lockout */
305 tmp
|= USB_MODE_SETUP_LOCK_OFF
;
308 fsl_writel(tmp
, &dr_regs
->usbmode
);
310 /* Clear the setup status */
311 fsl_writel(0, &dr_regs
->usbsts
);
313 tmp
= udc
->ep_qh_dma
;
314 tmp
&= USB_EP_LIST_ADDRESS_MASK
;
315 fsl_writel(tmp
, &dr_regs
->endpointlistaddr
);
317 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
318 udc
->ep_qh
, (int)tmp
,
319 fsl_readl(&dr_regs
->endpointlistaddr
));
321 max_no_of_ep
= (0x0000001F & fsl_readl(&dr_regs
->dccparams
));
322 for (ep_num
= 1; ep_num
< max_no_of_ep
; ep_num
++) {
323 tmp
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
324 tmp
&= ~(EPCTRL_TX_TYPE
| EPCTRL_RX_TYPE
);
325 tmp
|= (EPCTRL_EP_TYPE_BULK
<< EPCTRL_TX_EP_TYPE_SHIFT
)
326 | (EPCTRL_EP_TYPE_BULK
<< EPCTRL_RX_EP_TYPE_SHIFT
);
327 fsl_writel(tmp
, &dr_regs
->endptctrl
[ep_num
]);
329 /* Config control enable i/o output, cpu endian register */
330 #ifndef CONFIG_ARCH_MXC
331 if (udc
->pdata
->have_sysif_regs
) {
332 ctrl
= __raw_readl(&usb_sys_regs
->control
);
333 ctrl
|= USB_CTRL_IOENB
;
334 __raw_writel(ctrl
, &usb_sys_regs
->control
);
338 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
339 /* Turn on cache snooping hardware, since some PowerPC platforms
340 * wholly rely on hardware to deal with cache coherent. */
342 if (udc
->pdata
->have_sysif_regs
) {
343 /* Setup Snooping for all the 4GB space */
344 tmp
= SNOOP_SIZE_2GB
; /* starts from 0x0, size 2G */
345 __raw_writel(tmp
, &usb_sys_regs
->snoop1
);
346 tmp
|= 0x80000000; /* starts from 0x8000000, size 2G */
347 __raw_writel(tmp
, &usb_sys_regs
->snoop2
);
354 /* Enable DR irq and set controller to run state */
355 static void dr_controller_run(struct fsl_udc
*udc
)
359 /* Enable DR irq reg */
360 temp
= USB_INTR_INT_EN
| USB_INTR_ERR_INT_EN
361 | USB_INTR_PTC_DETECT_EN
| USB_INTR_RESET_EN
362 | USB_INTR_DEVICE_SUSPEND
| USB_INTR_SYS_ERR_EN
;
364 fsl_writel(temp
, &dr_regs
->usbintr
);
366 /* Clear stopped bit */
369 /* Set the controller as device mode */
370 temp
= fsl_readl(&dr_regs
->usbmode
);
371 temp
|= USB_MODE_CTRL_MODE_DEVICE
;
372 fsl_writel(temp
, &dr_regs
->usbmode
);
374 /* Set controller to Run */
375 temp
= fsl_readl(&dr_regs
->usbcmd
);
376 temp
|= USB_CMD_RUN_STOP
;
377 fsl_writel(temp
, &dr_regs
->usbcmd
);
380 static void dr_controller_stop(struct fsl_udc
*udc
)
384 pr_debug("%s\n", __func__
);
386 /* if we're in OTG mode, and the Host is currently using the port,
387 * stop now and don't rip the controller out from under the
390 if (udc
->gadget
.is_otg
) {
391 if (!(fsl_readl(&dr_regs
->otgsc
) & OTGSC_STS_USB_ID
)) {
392 pr_debug("udc: Leaving early\n");
397 /* disable all INTR */
398 fsl_writel(0, &dr_regs
->usbintr
);
400 /* Set stopped bit for isr */
403 /* disable IO output */
404 /* usb_sys_regs->control = 0; */
406 /* set controller to Stop */
407 tmp
= fsl_readl(&dr_regs
->usbcmd
);
408 tmp
&= ~USB_CMD_RUN_STOP
;
409 fsl_writel(tmp
, &dr_regs
->usbcmd
);
412 static void dr_ep_setup(unsigned char ep_num
, unsigned char dir
,
413 unsigned char ep_type
)
415 unsigned int tmp_epctrl
= 0;
417 tmp_epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
420 tmp_epctrl
|= EPCTRL_TX_DATA_TOGGLE_RST
;
421 tmp_epctrl
|= EPCTRL_TX_ENABLE
;
422 tmp_epctrl
&= ~EPCTRL_TX_TYPE
;
423 tmp_epctrl
|= ((unsigned int)(ep_type
)
424 << EPCTRL_TX_EP_TYPE_SHIFT
);
427 tmp_epctrl
|= EPCTRL_RX_DATA_TOGGLE_RST
;
428 tmp_epctrl
|= EPCTRL_RX_ENABLE
;
429 tmp_epctrl
&= ~EPCTRL_RX_TYPE
;
430 tmp_epctrl
|= ((unsigned int)(ep_type
)
431 << EPCTRL_RX_EP_TYPE_SHIFT
);
434 fsl_writel(tmp_epctrl
, &dr_regs
->endptctrl
[ep_num
]);
438 dr_ep_change_stall(unsigned char ep_num
, unsigned char dir
, int value
)
442 tmp_epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
445 /* set the stall bit */
447 tmp_epctrl
|= EPCTRL_TX_EP_STALL
;
449 tmp_epctrl
|= EPCTRL_RX_EP_STALL
;
451 /* clear the stall bit and reset data toggle */
453 tmp_epctrl
&= ~EPCTRL_TX_EP_STALL
;
454 tmp_epctrl
|= EPCTRL_TX_DATA_TOGGLE_RST
;
456 tmp_epctrl
&= ~EPCTRL_RX_EP_STALL
;
457 tmp_epctrl
|= EPCTRL_RX_DATA_TOGGLE_RST
;
460 fsl_writel(tmp_epctrl
, &dr_regs
->endptctrl
[ep_num
]);
463 /* Get stall status of a specific ep
464 Return: 0: not stalled; 1:stalled */
465 static int dr_ep_get_stall(unsigned char ep_num
, unsigned char dir
)
469 epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
471 return (epctrl
& EPCTRL_TX_EP_STALL
) ? 1 : 0;
473 return (epctrl
& EPCTRL_RX_EP_STALL
) ? 1 : 0;
476 /********************************************************************
477 Internal Structure Build up functions
478 ********************************************************************/
480 /*------------------------------------------------------------------
481 * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
482 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
484 ------------------------------------------------------------------*/
485 static void struct_ep_qh_setup(struct fsl_udc
*udc
, unsigned char ep_num
,
486 unsigned char dir
, unsigned char ep_type
,
487 unsigned int max_pkt_len
,
488 unsigned int zlt
, unsigned char mult
)
490 struct ep_queue_head
*p_QH
= &udc
->ep_qh
[2 * ep_num
+ dir
];
491 unsigned int tmp
= 0;
493 /* set the Endpoint Capabilites in QH */
495 case USB_ENDPOINT_XFER_CONTROL
:
496 /* Interrupt On Setup (IOS). for control ep */
497 tmp
= (max_pkt_len
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
500 case USB_ENDPOINT_XFER_ISOC
:
501 tmp
= (max_pkt_len
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
502 | (mult
<< EP_QUEUE_HEAD_MULT_POS
);
504 case USB_ENDPOINT_XFER_BULK
:
505 case USB_ENDPOINT_XFER_INT
:
506 tmp
= max_pkt_len
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
;
509 VDBG("error ep type is %d", ep_type
);
513 tmp
|= EP_QUEUE_HEAD_ZLT_SEL
;
515 p_QH
->max_pkt_length
= cpu_to_hc32(tmp
);
516 p_QH
->next_dtd_ptr
= 1;
517 p_QH
->size_ioc_int_sts
= 0;
520 /* Setup qh structure and ep register for ep0. */
521 static void ep0_setup(struct fsl_udc
*udc
)
523 /* the intialization of an ep includes: fields in QH, Regs,
525 struct_ep_qh_setup(udc
, 0, USB_RECV
, USB_ENDPOINT_XFER_CONTROL
,
526 USB_MAX_CTRL_PAYLOAD
, 0, 0);
527 struct_ep_qh_setup(udc
, 0, USB_SEND
, USB_ENDPOINT_XFER_CONTROL
,
528 USB_MAX_CTRL_PAYLOAD
, 0, 0);
529 dr_ep_setup(0, USB_RECV
, USB_ENDPOINT_XFER_CONTROL
);
530 dr_ep_setup(0, USB_SEND
, USB_ENDPOINT_XFER_CONTROL
);
536 /***********************************************************************
537 Endpoint Management Functions
538 ***********************************************************************/
540 /*-------------------------------------------------------------------------
541 * when configurations are set, or when interface settings change
542 * for example the do_set_interface() in gadget layer,
543 * the driver will enable or disable the relevant endpoints
544 * ep0 doesn't use this routine. It is always enabled.
545 -------------------------------------------------------------------------*/
546 static int fsl_ep_enable(struct usb_ep
*_ep
,
547 const struct usb_endpoint_descriptor
*desc
)
549 struct fsl_udc
*udc
= NULL
;
550 struct fsl_ep
*ep
= NULL
;
551 unsigned short max
= 0;
552 unsigned char mult
= 0, zlt
;
553 int retval
= -EINVAL
;
554 unsigned long flags
= 0;
556 ep
= container_of(_ep
, struct fsl_ep
, ep
);
558 /* catch various bogus parameters */
560 || (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
565 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
568 max
= usb_endpoint_maxp(desc
);
570 /* Disable automatic zlp generation. Driver is responsible to indicate
571 * explicitly through req->req.zero. This is needed to enable multi-td
575 /* Assume the max packet size from gadget is always correct */
576 switch (desc
->bmAttributes
& 0x03) {
577 case USB_ENDPOINT_XFER_CONTROL
:
578 case USB_ENDPOINT_XFER_BULK
:
579 case USB_ENDPOINT_XFER_INT
:
580 /* mult = 0. Execute N Transactions as demonstrated by
581 * the USB variable length packet protocol where N is
582 * computed using the Maximum Packet Length (dQH) and
583 * the Total Bytes field (dTD) */
586 case USB_ENDPOINT_XFER_ISOC
:
587 /* Calculate transactions needed for high bandwidth iso */
588 mult
= (unsigned char)(1 + ((max
>> 11) & 0x03));
589 max
= max
& 0x7ff; /* bit 0~10 */
590 /* 3 transactions at most */
598 spin_lock_irqsave(&udc
->lock
, flags
);
599 ep
->ep
.maxpacket
= max
;
603 /* Controller related setup */
604 /* Init EPx Queue Head (Ep Capabilites field in QH
605 * according to max, zlt, mult) */
606 struct_ep_qh_setup(udc
, (unsigned char) ep_index(ep
),
607 (unsigned char) ((desc
->bEndpointAddress
& USB_DIR_IN
)
608 ? USB_SEND
: USB_RECV
),
609 (unsigned char) (desc
->bmAttributes
610 & USB_ENDPOINT_XFERTYPE_MASK
),
613 /* Init endpoint ctrl register */
614 dr_ep_setup((unsigned char) ep_index(ep
),
615 (unsigned char) ((desc
->bEndpointAddress
& USB_DIR_IN
)
616 ? USB_SEND
: USB_RECV
),
617 (unsigned char) (desc
->bmAttributes
618 & USB_ENDPOINT_XFERTYPE_MASK
));
620 spin_unlock_irqrestore(&udc
->lock
, flags
);
623 VDBG("enabled %s (ep%d%s) maxpacket %d",ep
->ep
.name
,
624 ep
->ep
.desc
->bEndpointAddress
& 0x0f,
625 (desc
->bEndpointAddress
& USB_DIR_IN
)
626 ? "in" : "out", max
);
631 /*---------------------------------------------------------------------
632 * @ep : the ep being unconfigured. May not be ep0
633 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
634 *---------------------------------------------------------------------*/
635 static int fsl_ep_disable(struct usb_ep
*_ep
)
637 struct fsl_udc
*udc
= NULL
;
638 struct fsl_ep
*ep
= NULL
;
639 unsigned long flags
= 0;
643 ep
= container_of(_ep
, struct fsl_ep
, ep
);
644 if (!_ep
|| !ep
->ep
.desc
) {
645 VDBG("%s not enabled", _ep
? ep
->ep
.name
: NULL
);
649 /* disable ep on controller */
650 ep_num
= ep_index(ep
);
651 epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
653 epctrl
&= ~(EPCTRL_TX_ENABLE
| EPCTRL_TX_TYPE
);
654 epctrl
|= EPCTRL_EP_TYPE_BULK
<< EPCTRL_TX_EP_TYPE_SHIFT
;
656 epctrl
&= ~(EPCTRL_RX_ENABLE
| EPCTRL_TX_TYPE
);
657 epctrl
|= EPCTRL_EP_TYPE_BULK
<< EPCTRL_RX_EP_TYPE_SHIFT
;
659 fsl_writel(epctrl
, &dr_regs
->endptctrl
[ep_num
]);
661 udc
= (struct fsl_udc
*)ep
->udc
;
662 spin_lock_irqsave(&udc
->lock
, flags
);
664 /* nuke all pending requests (does flush) */
665 nuke(ep
, -ESHUTDOWN
);
669 spin_unlock_irqrestore(&udc
->lock
, flags
);
671 VDBG("disabled %s OK", _ep
->name
);
675 /*---------------------------------------------------------------------
676 * allocate a request object used by this endpoint
677 * the main operation is to insert the req->queue to the eq->queue
678 * Returns the request, or null if one could not be allocated
679 *---------------------------------------------------------------------*/
680 static struct usb_request
*
681 fsl_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
683 struct fsl_req
*req
= NULL
;
685 req
= kzalloc(sizeof *req
, gfp_flags
);
689 req
->req
.dma
= DMA_ADDR_INVALID
;
690 INIT_LIST_HEAD(&req
->queue
);
695 static void fsl_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
697 struct fsl_req
*req
= NULL
;
699 req
= container_of(_req
, struct fsl_req
, req
);
705 /* Actually add a dTD chain to an empty dQH and let go */
706 static void fsl_prime_ep(struct fsl_ep
*ep
, struct ep_td_struct
*td
)
708 struct ep_queue_head
*qh
= get_qh_by_ep(ep
);
710 /* Write dQH next pointer and terminate bit to 0 */
711 qh
->next_dtd_ptr
= cpu_to_hc32(td
->td_dma
712 & EP_QUEUE_HEAD_NEXT_POINTER_MASK
);
714 /* Clear active and halt bit */
715 qh
->size_ioc_int_sts
&= cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
716 | EP_QUEUE_HEAD_STATUS_HALT
));
718 /* Ensure that updates to the QH will occur before priming. */
721 /* Prime endpoint by writing correct bit to ENDPTPRIME */
722 fsl_writel(ep_is_in(ep
) ? (1 << (ep_index(ep
) + 16))
723 : (1 << (ep_index(ep
))), &dr_regs
->endpointprime
);
726 /* Add dTD chain to the dQH of an EP */
727 static void fsl_queue_td(struct fsl_ep
*ep
, struct fsl_req
*req
)
729 u32 temp
, bitmask
, tmp_stat
;
731 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
732 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
734 bitmask
= ep_is_in(ep
)
735 ? (1 << (ep_index(ep
) + 16))
736 : (1 << (ep_index(ep
)));
738 /* check if the pipe is empty */
739 if (!(list_empty(&ep
->queue
)) && !(ep_index(ep
) == 0)) {
740 /* Add td to the end */
741 struct fsl_req
*lastreq
;
742 lastreq
= list_entry(ep
->queue
.prev
, struct fsl_req
, queue
);
743 lastreq
->tail
->next_td_ptr
=
744 cpu_to_hc32(req
->head
->td_dma
& DTD_ADDR_MASK
);
745 /* Ensure dTD's next dtd pointer to be updated */
747 /* Read prime bit, if 1 goto done */
748 if (fsl_readl(&dr_regs
->endpointprime
) & bitmask
)
752 /* Set ATDTW bit in USBCMD */
753 temp
= fsl_readl(&dr_regs
->usbcmd
);
754 fsl_writel(temp
| USB_CMD_ATDTW
, &dr_regs
->usbcmd
);
756 /* Read correct status bit */
757 tmp_stat
= fsl_readl(&dr_regs
->endptstatus
) & bitmask
;
759 } while (!(fsl_readl(&dr_regs
->usbcmd
) & USB_CMD_ATDTW
));
761 /* Write ATDTW bit to 0 */
762 temp
= fsl_readl(&dr_regs
->usbcmd
);
763 fsl_writel(temp
& ~USB_CMD_ATDTW
, &dr_regs
->usbcmd
);
769 fsl_prime_ep(ep
, req
->head
);
772 /* Fill in the dTD structure
773 * @req: request that the transfer belongs to
774 * @length: return actually data length of the dTD
775 * @dma: return dma address of the dTD
776 * @is_last: return flag if it is the last dTD of the request
777 * return: pointer to the built dTD */
778 static struct ep_td_struct
*fsl_build_dtd(struct fsl_req
*req
, unsigned *length
,
779 dma_addr_t
*dma
, int *is_last
, gfp_t gfp_flags
)
782 struct ep_td_struct
*dtd
;
784 /* how big will this transfer be? */
785 *length
= min(req
->req
.length
- req
->req
.actual
,
786 (unsigned)EP_MAX_LENGTH_TRANSFER
);
788 dtd
= dma_pool_alloc(udc_controller
->td_pool
, gfp_flags
, dma
);
793 /* Clear reserved field */
794 swap_temp
= hc32_to_cpu(dtd
->size_ioc_sts
);
795 swap_temp
&= ~DTD_RESERVED_FIELDS
;
796 dtd
->size_ioc_sts
= cpu_to_hc32(swap_temp
);
798 /* Init all of buffer page pointers */
799 swap_temp
= (u32
) (req
->req
.dma
+ req
->req
.actual
);
800 dtd
->buff_ptr0
= cpu_to_hc32(swap_temp
);
801 dtd
->buff_ptr1
= cpu_to_hc32(swap_temp
+ 0x1000);
802 dtd
->buff_ptr2
= cpu_to_hc32(swap_temp
+ 0x2000);
803 dtd
->buff_ptr3
= cpu_to_hc32(swap_temp
+ 0x3000);
804 dtd
->buff_ptr4
= cpu_to_hc32(swap_temp
+ 0x4000);
806 req
->req
.actual
+= *length
;
808 /* zlp is needed if req->req.zero is set */
810 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
814 } else if (req
->req
.length
== req
->req
.actual
)
820 VDBG("multi-dtd request!");
821 /* Fill in the transfer size; set active bit */
822 swap_temp
= ((*length
<< DTD_LENGTH_BIT_POS
) | DTD_STATUS_ACTIVE
);
824 /* Enable interrupt for the last dtd of a request */
825 if (*is_last
&& !req
->req
.no_interrupt
)
826 swap_temp
|= DTD_IOC
;
828 dtd
->size_ioc_sts
= cpu_to_hc32(swap_temp
);
832 VDBG("length = %d address= 0x%x", *length
, (int)*dma
);
837 /* Generate dtd chain for a request */
838 static int fsl_req_to_dtd(struct fsl_req
*req
, gfp_t gfp_flags
)
843 struct ep_td_struct
*last_dtd
= NULL
, *dtd
;
847 dtd
= fsl_build_dtd(req
, &count
, &dma
, &is_last
, gfp_flags
);
855 last_dtd
->next_td_ptr
= cpu_to_hc32(dma
);
856 last_dtd
->next_td_virt
= dtd
;
863 dtd
->next_td_ptr
= cpu_to_hc32(DTD_NEXT_TERMINATE
);
870 /* queues (submits) an I/O request to an endpoint */
872 fsl_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
874 struct fsl_ep
*ep
= container_of(_ep
, struct fsl_ep
, ep
);
875 struct fsl_req
*req
= container_of(_req
, struct fsl_req
, req
);
880 /* catch various bogus parameters */
881 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
882 || !list_empty(&req
->queue
)) {
883 VDBG("%s, bad params", __func__
);
886 if (unlikely(!_ep
|| !ep
->ep
.desc
)) {
887 VDBG("%s, bad ep", __func__
);
890 if (usb_endpoint_xfer_isoc(ep
->ep
.desc
)) {
891 if (req
->req
.length
> ep
->ep
.maxpacket
)
896 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
901 ret
= usb_gadget_map_request(&ep
->udc
->gadget
, &req
->req
, ep_is_in(ep
));
905 req
->req
.status
= -EINPROGRESS
;
909 /* build dtds and push them to device queue */
910 if (!fsl_req_to_dtd(req
, gfp_flags
)) {
911 spin_lock_irqsave(&udc
->lock
, flags
);
912 fsl_queue_td(ep
, req
);
917 /* irq handler advances the queue */
919 list_add_tail(&req
->queue
, &ep
->queue
);
920 spin_unlock_irqrestore(&udc
->lock
, flags
);
925 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
926 static int fsl_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
928 struct fsl_ep
*ep
= container_of(_ep
, struct fsl_ep
, ep
);
931 int ep_num
, stopped
, ret
= 0;
937 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
938 stopped
= ep
->stopped
;
940 /* Stop the ep before we deal with the queue */
942 ep_num
= ep_index(ep
);
943 epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
945 epctrl
&= ~EPCTRL_TX_ENABLE
;
947 epctrl
&= ~EPCTRL_RX_ENABLE
;
948 fsl_writel(epctrl
, &dr_regs
->endptctrl
[ep_num
]);
950 /* make sure it's actually queued on this endpoint */
951 list_for_each_entry(req
, &ep
->queue
, queue
) {
952 if (&req
->req
== _req
)
955 if (&req
->req
!= _req
) {
960 /* The request is in progress, or completed but not dequeued */
961 if (ep
->queue
.next
== &req
->queue
) {
962 _req
->status
= -ECONNRESET
;
963 fsl_ep_fifo_flush(_ep
); /* flush current transfer */
965 /* The request isn't the last request in this ep queue */
966 if (req
->queue
.next
!= &ep
->queue
) {
967 struct fsl_req
*next_req
;
969 next_req
= list_entry(req
->queue
.next
, struct fsl_req
,
972 /* prime with dTD of next request */
973 fsl_prime_ep(ep
, next_req
->head
);
975 /* The request hasn't been processed, patch up the TD chain */
977 struct fsl_req
*prev_req
;
979 prev_req
= list_entry(req
->queue
.prev
, struct fsl_req
, queue
);
980 prev_req
->tail
->next_td_ptr
= req
->tail
->next_td_ptr
;
983 done(ep
, req
, -ECONNRESET
);
986 out
: epctrl
= fsl_readl(&dr_regs
->endptctrl
[ep_num
]);
988 epctrl
|= EPCTRL_TX_ENABLE
;
990 epctrl
|= EPCTRL_RX_ENABLE
;
991 fsl_writel(epctrl
, &dr_regs
->endptctrl
[ep_num
]);
992 ep
->stopped
= stopped
;
994 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
998 /*-------------------------------------------------------------------------*/
1000 /*-----------------------------------------------------------------
1001 * modify the endpoint halt feature
1002 * @ep: the non-isochronous endpoint being stalled
1003 * @value: 1--set halt 0--clear halt
1004 * Returns zero, or a negative error code.
1005 *----------------------------------------------------------------*/
1006 static int fsl_ep_set_halt(struct usb_ep
*_ep
, int value
)
1008 struct fsl_ep
*ep
= NULL
;
1009 unsigned long flags
= 0;
1010 int status
= -EOPNOTSUPP
; /* operation not supported */
1011 unsigned char ep_dir
= 0, ep_num
= 0;
1012 struct fsl_udc
*udc
= NULL
;
1014 ep
= container_of(_ep
, struct fsl_ep
, ep
);
1016 if (!_ep
|| !ep
->ep
.desc
) {
1021 if (usb_endpoint_xfer_isoc(ep
->ep
.desc
)) {
1022 status
= -EOPNOTSUPP
;
1026 /* Attempt to halt IN ep will fail if any transfer requests
1027 * are still queue */
1028 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1034 ep_dir
= ep_is_in(ep
) ? USB_SEND
: USB_RECV
;
1035 ep_num
= (unsigned char)(ep_index(ep
));
1036 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1037 dr_ep_change_stall(ep_num
, ep_dir
, value
);
1038 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1040 if (ep_index(ep
) == 0) {
1041 udc
->ep0_state
= WAIT_FOR_SETUP
;
1045 VDBG(" %s %s halt stat %d", ep
->ep
.name
,
1046 value
? "set" : "clear", status
);
1051 static int fsl_ep_fifo_status(struct usb_ep
*_ep
)
1054 struct fsl_udc
*udc
;
1057 struct ep_queue_head
*qh
;
1059 ep
= container_of(_ep
, struct fsl_ep
, ep
);
1060 if (!_ep
|| (!ep
->ep
.desc
&& ep_index(ep
) != 0))
1063 udc
= (struct fsl_udc
*)ep
->udc
;
1065 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1068 qh
= get_qh_by_ep(ep
);
1070 bitmask
= (ep_is_in(ep
)) ? (1 << (ep_index(ep
) + 16)) :
1071 (1 << (ep_index(ep
)));
1073 if (fsl_readl(&dr_regs
->endptstatus
) & bitmask
)
1074 size
= (qh
->size_ioc_int_sts
& DTD_PACKET_SIZE
)
1075 >> DTD_LENGTH_BIT_POS
;
1077 pr_debug("%s %u\n", __func__
, size
);
1081 static void fsl_ep_fifo_flush(struct usb_ep
*_ep
)
1086 unsigned long timeout
;
1087 #define FSL_UDC_FLUSH_TIMEOUT 1000
1092 ep
= container_of(_ep
, struct fsl_ep
, ep
);
1096 ep_num
= ep_index(ep
);
1097 ep_dir
= ep_is_in(ep
) ? USB_SEND
: USB_RECV
;
1100 bits
= (1 << 16) | 1;
1101 else if (ep_dir
== USB_SEND
)
1102 bits
= 1 << (16 + ep_num
);
1106 timeout
= jiffies
+ FSL_UDC_FLUSH_TIMEOUT
;
1108 fsl_writel(bits
, &dr_regs
->endptflush
);
1110 /* Wait until flush complete */
1111 while (fsl_readl(&dr_regs
->endptflush
)) {
1112 if (time_after(jiffies
, timeout
)) {
1113 ERR("ep flush timeout\n");
1118 /* See if we need to flush again */
1119 } while (fsl_readl(&dr_regs
->endptstatus
) & bits
);
1122 static struct usb_ep_ops fsl_ep_ops
= {
1123 .enable
= fsl_ep_enable
,
1124 .disable
= fsl_ep_disable
,
1126 .alloc_request
= fsl_alloc_request
,
1127 .free_request
= fsl_free_request
,
1129 .queue
= fsl_ep_queue
,
1130 .dequeue
= fsl_ep_dequeue
,
1132 .set_halt
= fsl_ep_set_halt
,
1133 .fifo_status
= fsl_ep_fifo_status
,
1134 .fifo_flush
= fsl_ep_fifo_flush
, /* flush fifo */
1137 /*-------------------------------------------------------------------------
1138 Gadget Driver Layer Operations
1139 -------------------------------------------------------------------------*/
1141 /*----------------------------------------------------------------------
1142 * Get the current frame number (from DR frame_index Reg )
1143 *----------------------------------------------------------------------*/
1144 static int fsl_get_frame(struct usb_gadget
*gadget
)
1146 return (int)(fsl_readl(&dr_regs
->frindex
) & USB_FRINDEX_MASKS
);
1149 /*-----------------------------------------------------------------------
1150 * Tries to wake up the host connected to this gadget
1151 -----------------------------------------------------------------------*/
1152 static int fsl_wakeup(struct usb_gadget
*gadget
)
1154 struct fsl_udc
*udc
= container_of(gadget
, struct fsl_udc
, gadget
);
1157 /* Remote wakeup feature not enabled by host */
1158 if (!udc
->remote_wakeup
)
1161 portsc
= fsl_readl(&dr_regs
->portsc1
);
1162 /* not suspended? */
1163 if (!(portsc
& PORTSCX_PORT_SUSPEND
))
1165 /* trigger force resume */
1166 portsc
|= PORTSCX_PORT_FORCE_RESUME
;
1167 fsl_writel(portsc
, &dr_regs
->portsc1
);
1171 static int can_pullup(struct fsl_udc
*udc
)
1173 return udc
->driver
&& udc
->softconnect
&& udc
->vbus_active
;
1176 /* Notify controller that VBUS is powered, Called by whatever
1177 detects VBUS sessions */
1178 static int fsl_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1180 struct fsl_udc
*udc
;
1181 unsigned long flags
;
1183 udc
= container_of(gadget
, struct fsl_udc
, gadget
);
1184 spin_lock_irqsave(&udc
->lock
, flags
);
1185 VDBG("VBUS %s", is_active
? "on" : "off");
1186 udc
->vbus_active
= (is_active
!= 0);
1187 if (can_pullup(udc
))
1188 fsl_writel((fsl_readl(&dr_regs
->usbcmd
) | USB_CMD_RUN_STOP
),
1191 fsl_writel((fsl_readl(&dr_regs
->usbcmd
) & ~USB_CMD_RUN_STOP
),
1193 spin_unlock_irqrestore(&udc
->lock
, flags
);
1197 /* constrain controller's VBUS power usage
1198 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1199 * reporting how much power the device may consume. For example, this
1200 * could affect how quickly batteries are recharged.
1202 * Returns zero on success, else negative errno.
1204 static int fsl_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1206 struct fsl_udc
*udc
;
1208 udc
= container_of(gadget
, struct fsl_udc
, gadget
);
1209 if (!IS_ERR_OR_NULL(udc
->transceiver
))
1210 return usb_phy_set_power(udc
->transceiver
, mA
);
1214 /* Change Data+ pullup status
1215 * this func is used by usb_gadget_connect/disconnet
1217 static int fsl_pullup(struct usb_gadget
*gadget
, int is_on
)
1219 struct fsl_udc
*udc
;
1221 udc
= container_of(gadget
, struct fsl_udc
, gadget
);
1223 if (!udc
->vbus_active
)
1226 udc
->softconnect
= (is_on
!= 0);
1227 if (can_pullup(udc
))
1228 fsl_writel((fsl_readl(&dr_regs
->usbcmd
) | USB_CMD_RUN_STOP
),
1231 fsl_writel((fsl_readl(&dr_regs
->usbcmd
) & ~USB_CMD_RUN_STOP
),
1237 static int fsl_udc_start(struct usb_gadget
*g
,
1238 struct usb_gadget_driver
*driver
);
1239 static int fsl_udc_stop(struct usb_gadget
*g
);
1241 static const struct usb_gadget_ops fsl_gadget_ops
= {
1242 .get_frame
= fsl_get_frame
,
1243 .wakeup
= fsl_wakeup
,
1244 /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1245 .vbus_session
= fsl_vbus_session
,
1246 .vbus_draw
= fsl_vbus_draw
,
1247 .pullup
= fsl_pullup
,
1248 .udc_start
= fsl_udc_start
,
1249 .udc_stop
= fsl_udc_stop
,
1253 * Empty complete function used by this driver to fill in the req->complete
1254 * field when creating a request since the complete field is mandatory.
1256 static void fsl_noop_complete(struct usb_ep
*ep
, struct usb_request
*req
) { }
1258 /* Set protocol stall on ep0, protocol stall will automatically be cleared
1259 on new transaction */
1260 static void ep0stall(struct fsl_udc
*udc
)
1264 /* must set tx and rx to stall at the same time */
1265 tmp
= fsl_readl(&dr_regs
->endptctrl
[0]);
1266 tmp
|= EPCTRL_TX_EP_STALL
| EPCTRL_RX_EP_STALL
;
1267 fsl_writel(tmp
, &dr_regs
->endptctrl
[0]);
1268 udc
->ep0_state
= WAIT_FOR_SETUP
;
1272 /* Prime a status phase for ep0 */
1273 static int ep0_prime_status(struct fsl_udc
*udc
, int direction
)
1275 struct fsl_req
*req
= udc
->status_req
;
1279 if (direction
== EP_DIR_IN
)
1280 udc
->ep0_dir
= USB_DIR_IN
;
1282 udc
->ep0_dir
= USB_DIR_OUT
;
1285 if (udc
->ep0_state
!= DATA_STATE_XMIT
)
1286 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1289 req
->req
.length
= 0;
1290 req
->req
.status
= -EINPROGRESS
;
1291 req
->req
.actual
= 0;
1292 req
->req
.complete
= fsl_noop_complete
;
1295 ret
= usb_gadget_map_request(&ep
->udc
->gadget
, &req
->req
, ep_is_in(ep
));
1299 if (fsl_req_to_dtd(req
, GFP_ATOMIC
) == 0)
1300 fsl_queue_td(ep
, req
);
1304 list_add_tail(&req
->queue
, &ep
->queue
);
1309 static void udc_reset_ep_queue(struct fsl_udc
*udc
, u8 pipe
)
1311 struct fsl_ep
*ep
= get_ep_by_pipe(udc
, pipe
);
1314 nuke(ep
, -ESHUTDOWN
);
1320 static void ch9setaddress(struct fsl_udc
*udc
, u16 value
, u16 index
, u16 length
)
1322 /* Save the new address to device struct */
1323 udc
->device_address
= (u8
) value
;
1324 /* Update usb state */
1325 udc
->usb_state
= USB_STATE_ADDRESS
;
1327 if (ep0_prime_status(udc
, EP_DIR_IN
))
1334 static void ch9getstatus(struct fsl_udc
*udc
, u8 request_type
, u16 value
,
1335 u16 index
, u16 length
)
1337 u16 tmp
= 0; /* Status, cpu endian */
1338 struct fsl_req
*req
;
1344 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1345 /* Get device status */
1346 tmp
= udc
->gadget
.is_selfpowered
;
1347 tmp
|= udc
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
1348 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1349 /* Get interface status */
1350 /* We don't have interface information in udc driver */
1352 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1353 /* Get endpoint status */
1354 struct fsl_ep
*target_ep
;
1356 target_ep
= get_ep_by_pipe(udc
, get_pipe_by_windex(index
));
1358 /* stall if endpoint doesn't exist */
1359 if (!target_ep
->ep
.desc
)
1361 tmp
= dr_ep_get_stall(ep_index(target_ep
), ep_is_in(target_ep
))
1362 << USB_ENDPOINT_HALT
;
1365 udc
->ep0_dir
= USB_DIR_IN
;
1366 /* Borrow the per device status_req */
1367 req
= udc
->status_req
;
1368 /* Fill in the reqest structure */
1369 *((u16
*) req
->req
.buf
) = cpu_to_le16(tmp
);
1372 req
->req
.length
= 2;
1373 req
->req
.status
= -EINPROGRESS
;
1374 req
->req
.actual
= 0;
1375 req
->req
.complete
= fsl_noop_complete
;
1378 ret
= usb_gadget_map_request(&ep
->udc
->gadget
, &req
->req
, ep_is_in(ep
));
1382 /* prime the data phase */
1383 if ((fsl_req_to_dtd(req
, GFP_ATOMIC
) == 0))
1384 fsl_queue_td(ep
, req
);
1388 list_add_tail(&req
->queue
, &ep
->queue
);
1389 udc
->ep0_state
= DATA_STATE_XMIT
;
1390 if (ep0_prime_status(udc
, EP_DIR_OUT
))
1398 static void setup_received_irq(struct fsl_udc
*udc
,
1399 struct usb_ctrlrequest
*setup
)
1400 __releases(udc
->lock
)
1401 __acquires(udc
->lock
)
1403 u16 wValue
= le16_to_cpu(setup
->wValue
);
1404 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
1405 u16 wLength
= le16_to_cpu(setup
->wLength
);
1407 udc_reset_ep_queue(udc
, 0);
1409 /* We process some stardard setup requests here */
1410 switch (setup
->bRequest
) {
1411 case USB_REQ_GET_STATUS
:
1412 /* Data+Status phase from udc */
1413 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
1414 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
1416 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
, wLength
);
1419 case USB_REQ_SET_ADDRESS
:
1420 /* Status phase from udc */
1421 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
1422 | USB_RECIP_DEVICE
))
1424 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
1427 case USB_REQ_CLEAR_FEATURE
:
1428 case USB_REQ_SET_FEATURE
:
1429 /* Status phase from udc */
1431 int rc
= -EOPNOTSUPP
;
1434 if ((setup
->bRequestType
& (USB_RECIP_MASK
| USB_TYPE_MASK
))
1435 == (USB_RECIP_ENDPOINT
| USB_TYPE_STANDARD
)) {
1436 int pipe
= get_pipe_by_windex(wIndex
);
1439 if (wValue
!= 0 || wLength
!= 0 || pipe
>= udc
->max_ep
)
1441 ep
= get_ep_by_pipe(udc
, pipe
);
1443 spin_unlock(&udc
->lock
);
1444 rc
= fsl_ep_set_halt(&ep
->ep
,
1445 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
1447 spin_lock(&udc
->lock
);
1449 } else if ((setup
->bRequestType
& (USB_RECIP_MASK
1450 | USB_TYPE_MASK
)) == (USB_RECIP_DEVICE
1451 | USB_TYPE_STANDARD
)) {
1452 /* Note: The driver has not include OTG support yet.
1453 * This will be set when OTG support is added */
1454 if (wValue
== USB_DEVICE_TEST_MODE
)
1456 else if (gadget_is_otg(&udc
->gadget
)) {
1457 if (setup
->bRequest
==
1458 USB_DEVICE_B_HNP_ENABLE
)
1459 udc
->gadget
.b_hnp_enable
= 1;
1460 else if (setup
->bRequest
==
1461 USB_DEVICE_A_HNP_SUPPORT
)
1462 udc
->gadget
.a_hnp_support
= 1;
1463 else if (setup
->bRequest
==
1464 USB_DEVICE_A_ALT_HNP_SUPPORT
)
1465 udc
->gadget
.a_alt_hnp_support
= 1;
1472 if (ep0_prime_status(udc
, EP_DIR_IN
))
1479 tmp
= fsl_readl(&dr_regs
->portsc1
) | (ptc
<< 16);
1480 fsl_writel(tmp
, &dr_regs
->portsc1
);
1481 printk(KERN_INFO
"udc: switch to test mode %d.\n", ptc
);
1491 /* Requests handled by gadget */
1493 /* Data phase from gadget, status phase from udc */
1494 udc
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1495 ? USB_DIR_IN
: USB_DIR_OUT
;
1496 spin_unlock(&udc
->lock
);
1497 if (udc
->driver
->setup(&udc
->gadget
,
1498 &udc
->local_setup_buff
) < 0)
1500 spin_lock(&udc
->lock
);
1501 udc
->ep0_state
= (setup
->bRequestType
& USB_DIR_IN
)
1502 ? DATA_STATE_XMIT
: DATA_STATE_RECV
;
1504 * If the data stage is IN, send status prime immediately.
1505 * See 2.0 Spec chapter 8.5.3.3 for detail.
1507 if (udc
->ep0_state
== DATA_STATE_XMIT
)
1508 if (ep0_prime_status(udc
, EP_DIR_OUT
))
1512 /* No data phase, IN status from gadget */
1513 udc
->ep0_dir
= USB_DIR_IN
;
1514 spin_unlock(&udc
->lock
);
1515 if (udc
->driver
->setup(&udc
->gadget
,
1516 &udc
->local_setup_buff
) < 0)
1518 spin_lock(&udc
->lock
);
1519 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1523 /* Process request for Data or Status phase of ep0
1524 * prime status phase if needed */
1525 static void ep0_req_complete(struct fsl_udc
*udc
, struct fsl_ep
*ep0
,
1526 struct fsl_req
*req
)
1528 if (udc
->usb_state
== USB_STATE_ADDRESS
) {
1529 /* Set the new address */
1530 u32 new_address
= (u32
) udc
->device_address
;
1531 fsl_writel(new_address
<< USB_DEVICE_ADDRESS_BIT_POS
,
1532 &dr_regs
->deviceaddr
);
1537 switch (udc
->ep0_state
) {
1538 case DATA_STATE_XMIT
:
1539 /* already primed at setup_received_irq */
1540 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1542 case DATA_STATE_RECV
:
1543 /* send status phase */
1544 if (ep0_prime_status(udc
, EP_DIR_IN
))
1547 case WAIT_FOR_OUT_STATUS
:
1548 udc
->ep0_state
= WAIT_FOR_SETUP
;
1550 case WAIT_FOR_SETUP
:
1551 ERR("Unexpect ep0 packets\n");
1559 /* Tripwire mechanism to ensure a setup packet payload is extracted without
1560 * being corrupted by another incoming setup packet */
1561 static void tripwire_handler(struct fsl_udc
*udc
, u8 ep_num
, u8
*buffer_ptr
)
1564 struct ep_queue_head
*qh
;
1565 struct fsl_usb2_platform_data
*pdata
= udc
->pdata
;
1567 qh
= &udc
->ep_qh
[ep_num
* 2 + EP_DIR_OUT
];
1569 /* Clear bit in ENDPTSETUPSTAT */
1570 temp
= fsl_readl(&dr_regs
->endptsetupstat
);
1571 fsl_writel(temp
| (1 << ep_num
), &dr_regs
->endptsetupstat
);
1573 /* while a hazard exists when setup package arrives */
1575 /* Set Setup Tripwire */
1576 temp
= fsl_readl(&dr_regs
->usbcmd
);
1577 fsl_writel(temp
| USB_CMD_SUTW
, &dr_regs
->usbcmd
);
1579 /* Copy the setup packet to local buffer */
1580 if (pdata
->le_setup_buf
) {
1581 u32
*p
= (u32
*)buffer_ptr
;
1582 u32
*s
= (u32
*)qh
->setup_buffer
;
1584 /* Convert little endian setup buffer to CPU endian */
1585 *p
++ = le32_to_cpu(*s
++);
1586 *p
= le32_to_cpu(*s
);
1588 memcpy(buffer_ptr
, (u8
*) qh
->setup_buffer
, 8);
1590 } while (!(fsl_readl(&dr_regs
->usbcmd
) & USB_CMD_SUTW
));
1592 /* Clear Setup Tripwire */
1593 temp
= fsl_readl(&dr_regs
->usbcmd
);
1594 fsl_writel(temp
& ~USB_CMD_SUTW
, &dr_regs
->usbcmd
);
1597 /* process-ep_req(): free the completed Tds for this req */
1598 static int process_ep_req(struct fsl_udc
*udc
, int pipe
,
1599 struct fsl_req
*curr_req
)
1601 struct ep_td_struct
*curr_td
;
1602 int td_complete
, actual
, remaining_length
, j
, tmp
;
1605 struct ep_queue_head
*curr_qh
= &udc
->ep_qh
[pipe
];
1606 int direction
= pipe
% 2;
1608 curr_td
= curr_req
->head
;
1610 actual
= curr_req
->req
.length
;
1612 for (j
= 0; j
< curr_req
->dtd_count
; j
++) {
1613 remaining_length
= (hc32_to_cpu(curr_td
->size_ioc_sts
)
1615 >> DTD_LENGTH_BIT_POS
;
1616 actual
-= remaining_length
;
1618 errors
= hc32_to_cpu(curr_td
->size_ioc_sts
);
1619 if (errors
& DTD_ERROR_MASK
) {
1620 if (errors
& DTD_STATUS_HALTED
) {
1621 ERR("dTD error %08x QH=%d\n", errors
, pipe
);
1622 /* Clear the errors and Halt condition */
1623 tmp
= hc32_to_cpu(curr_qh
->size_ioc_int_sts
);
1625 curr_qh
->size_ioc_int_sts
= cpu_to_hc32(tmp
);
1627 /* FIXME: continue with next queued TD? */
1631 if (errors
& DTD_STATUS_DATA_BUFF_ERR
) {
1632 VDBG("Transfer overflow");
1635 } else if (errors
& DTD_STATUS_TRANSACTION_ERR
) {
1640 ERR("Unknown error has occurred (0x%x)!\n",
1643 } else if (hc32_to_cpu(curr_td
->size_ioc_sts
)
1644 & DTD_STATUS_ACTIVE
) {
1645 VDBG("Request not complete");
1646 status
= REQ_UNCOMPLETE
;
1648 } else if (remaining_length
) {
1650 VDBG("Transmit dTD remaining length not zero");
1659 VDBG("dTD transmitted successful");
1662 if (j
!= curr_req
->dtd_count
- 1)
1663 curr_td
= (struct ep_td_struct
*)curr_td
->next_td_virt
;
1669 curr_req
->req
.actual
= actual
;
1674 /* Process a DTD completion interrupt */
1675 static void dtd_complete_irq(struct fsl_udc
*udc
)
1678 int i
, ep_num
, direction
, bit_mask
, status
;
1679 struct fsl_ep
*curr_ep
;
1680 struct fsl_req
*curr_req
, *temp_req
;
1682 /* Clear the bits in the register */
1683 bit_pos
= fsl_readl(&dr_regs
->endptcomplete
);
1684 fsl_writel(bit_pos
, &dr_regs
->endptcomplete
);
1689 for (i
= 0; i
< udc
->max_ep
; i
++) {
1693 bit_mask
= 1 << (ep_num
+ 16 * direction
);
1695 if (!(bit_pos
& bit_mask
))
1698 curr_ep
= get_ep_by_pipe(udc
, i
);
1700 /* If the ep is configured */
1701 if (!curr_ep
->ep
.name
) {
1702 WARNING("Invalid EP?");
1706 /* process the req queue until an uncomplete request */
1707 list_for_each_entry_safe(curr_req
, temp_req
, &curr_ep
->queue
,
1709 status
= process_ep_req(udc
, i
, curr_req
);
1711 VDBG("status of process_ep_req= %d, ep = %d",
1713 if (status
== REQ_UNCOMPLETE
)
1715 /* write back status to req */
1716 curr_req
->req
.status
= status
;
1719 ep0_req_complete(udc
, curr_ep
, curr_req
);
1722 done(curr_ep
, curr_req
, status
);
1727 static inline enum usb_device_speed
portscx_device_speed(u32 reg
)
1729 switch (reg
& PORTSCX_PORT_SPEED_MASK
) {
1730 case PORTSCX_PORT_SPEED_HIGH
:
1731 return USB_SPEED_HIGH
;
1732 case PORTSCX_PORT_SPEED_FULL
:
1733 return USB_SPEED_FULL
;
1734 case PORTSCX_PORT_SPEED_LOW
:
1735 return USB_SPEED_LOW
;
1737 return USB_SPEED_UNKNOWN
;
1741 /* Process a port change interrupt */
1742 static void port_change_irq(struct fsl_udc
*udc
)
1747 /* Bus resetting is finished */
1748 if (!(fsl_readl(&dr_regs
->portsc1
) & PORTSCX_PORT_RESET
))
1751 portscx_device_speed(fsl_readl(&dr_regs
->portsc1
));
1753 /* Update USB state */
1754 if (!udc
->resume_state
)
1755 udc
->usb_state
= USB_STATE_DEFAULT
;
1758 /* Process suspend interrupt */
1759 static void suspend_irq(struct fsl_udc
*udc
)
1761 udc
->resume_state
= udc
->usb_state
;
1762 udc
->usb_state
= USB_STATE_SUSPENDED
;
1764 /* report suspend to the driver, serial.c does not support this */
1765 if (udc
->driver
->suspend
)
1766 udc
->driver
->suspend(&udc
->gadget
);
1769 static void bus_resume(struct fsl_udc
*udc
)
1771 udc
->usb_state
= udc
->resume_state
;
1772 udc
->resume_state
= 0;
1774 /* report resume to the driver, serial.c does not support this */
1775 if (udc
->driver
->resume
)
1776 udc
->driver
->resume(&udc
->gadget
);
1779 /* Clear up all ep queues */
1780 static int reset_queues(struct fsl_udc
*udc
, bool bus_reset
)
1784 for (pipe
= 0; pipe
< udc
->max_pipes
; pipe
++)
1785 udc_reset_ep_queue(udc
, pipe
);
1787 /* report disconnect; the driver is already quiesced */
1788 spin_unlock(&udc
->lock
);
1790 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1792 udc
->driver
->disconnect(&udc
->gadget
);
1793 spin_lock(&udc
->lock
);
1798 /* Process reset interrupt */
1799 static void reset_irq(struct fsl_udc
*udc
)
1802 unsigned long timeout
;
1804 /* Clear the device address */
1805 temp
= fsl_readl(&dr_regs
->deviceaddr
);
1806 fsl_writel(temp
& ~USB_DEVICE_ADDRESS_MASK
, &dr_regs
->deviceaddr
);
1808 udc
->device_address
= 0;
1810 /* Clear usb state */
1811 udc
->resume_state
= 0;
1813 udc
->ep0_state
= WAIT_FOR_SETUP
;
1814 udc
->remote_wakeup
= 0; /* default to 0 on reset */
1815 udc
->gadget
.b_hnp_enable
= 0;
1816 udc
->gadget
.a_hnp_support
= 0;
1817 udc
->gadget
.a_alt_hnp_support
= 0;
1819 /* Clear all the setup token semaphores */
1820 temp
= fsl_readl(&dr_regs
->endptsetupstat
);
1821 fsl_writel(temp
, &dr_regs
->endptsetupstat
);
1823 /* Clear all the endpoint complete status bits */
1824 temp
= fsl_readl(&dr_regs
->endptcomplete
);
1825 fsl_writel(temp
, &dr_regs
->endptcomplete
);
1827 timeout
= jiffies
+ 100;
1828 while (fsl_readl(&dr_regs
->endpointprime
)) {
1829 /* Wait until all endptprime bits cleared */
1830 if (time_after(jiffies
, timeout
)) {
1831 ERR("Timeout for reset\n");
1837 /* Write 1s to the flush register */
1838 fsl_writel(0xffffffff, &dr_regs
->endptflush
);
1840 if (fsl_readl(&dr_regs
->portsc1
) & PORTSCX_PORT_RESET
) {
1842 /* Bus is reseting */
1844 /* Reset all the queues, include XD, dTD, EP queue
1845 * head and TR Queue */
1846 reset_queues(udc
, true);
1847 udc
->usb_state
= USB_STATE_DEFAULT
;
1849 VDBG("Controller reset");
1850 /* initialize usb hw reg except for regs for EP, not
1851 * touch usbintr reg */
1852 dr_controller_setup(udc
);
1854 /* Reset all internal used Queues */
1855 reset_queues(udc
, false);
1859 /* Enable DR IRQ reg, Set Run bit, change udc state */
1860 dr_controller_run(udc
);
1861 udc
->usb_state
= USB_STATE_ATTACHED
;
1866 * USB device controller interrupt handler
1868 static irqreturn_t
fsl_udc_irq(int irq
, void *_udc
)
1870 struct fsl_udc
*udc
= _udc
;
1872 irqreturn_t status
= IRQ_NONE
;
1873 unsigned long flags
;
1875 /* Disable ISR for OTG host mode */
1878 spin_lock_irqsave(&udc
->lock
, flags
);
1879 irq_src
= fsl_readl(&dr_regs
->usbsts
) & fsl_readl(&dr_regs
->usbintr
);
1880 /* Clear notification bits */
1881 fsl_writel(irq_src
, &dr_regs
->usbsts
);
1883 /* VDBG("irq_src [0x%8x]", irq_src); */
1885 /* Need to resume? */
1886 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
1887 if ((fsl_readl(&dr_regs
->portsc1
) & PORTSCX_PORT_SUSPEND
) == 0)
1891 if (irq_src
& USB_STS_INT
) {
1893 /* Setup package, we only support ep0 as control ep */
1894 if (fsl_readl(&dr_regs
->endptsetupstat
) & EP_SETUP_STATUS_EP0
) {
1895 tripwire_handler(udc
, 0,
1896 (u8
*) (&udc
->local_setup_buff
));
1897 setup_received_irq(udc
, &udc
->local_setup_buff
);
1898 status
= IRQ_HANDLED
;
1901 /* completion of dtd */
1902 if (fsl_readl(&dr_regs
->endptcomplete
)) {
1903 dtd_complete_irq(udc
);
1904 status
= IRQ_HANDLED
;
1908 /* SOF (for ISO transfer) */
1909 if (irq_src
& USB_STS_SOF
) {
1910 status
= IRQ_HANDLED
;
1914 if (irq_src
& USB_STS_PORT_CHANGE
) {
1915 port_change_irq(udc
);
1916 status
= IRQ_HANDLED
;
1919 /* Reset Received */
1920 if (irq_src
& USB_STS_RESET
) {
1923 status
= IRQ_HANDLED
;
1926 /* Sleep Enable (Suspend) */
1927 if (irq_src
& USB_STS_SUSPEND
) {
1929 status
= IRQ_HANDLED
;
1932 if (irq_src
& (USB_STS_ERR
| USB_STS_SYS_ERR
)) {
1933 VDBG("Error IRQ %x", irq_src
);
1936 spin_unlock_irqrestore(&udc
->lock
, flags
);
1940 /*----------------------------------------------------------------*
1941 * Hook to gadget drivers
1942 * Called by initialization code of gadget drivers
1943 *----------------------------------------------------------------*/
1944 static int fsl_udc_start(struct usb_gadget
*g
,
1945 struct usb_gadget_driver
*driver
)
1948 unsigned long flags
= 0;
1950 /* lock is needed but whether should use this lock or another */
1951 spin_lock_irqsave(&udc_controller
->lock
, flags
);
1953 driver
->driver
.bus
= NULL
;
1954 /* hook up the driver */
1955 udc_controller
->driver
= driver
;
1956 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
1957 g
->is_selfpowered
= 1;
1959 if (!IS_ERR_OR_NULL(udc_controller
->transceiver
)) {
1960 /* Suspend the controller until OTG enable it */
1961 udc_controller
->stopped
= 1;
1962 printk(KERN_INFO
"Suspend udc for OTG auto detect\n");
1964 /* connect to bus through transceiver */
1965 if (!IS_ERR_OR_NULL(udc_controller
->transceiver
)) {
1966 retval
= otg_set_peripheral(
1967 udc_controller
->transceiver
->otg
,
1968 &udc_controller
->gadget
);
1970 ERR("can't bind to transceiver\n");
1971 udc_controller
->driver
= NULL
;
1976 /* Enable DR IRQ reg and set USBCMD reg Run bit */
1977 dr_controller_run(udc_controller
);
1978 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
1979 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
1980 udc_controller
->ep0_dir
= 0;
1986 /* Disconnect from gadget driver */
1987 static int fsl_udc_stop(struct usb_gadget
*g
)
1989 struct fsl_ep
*loop_ep
;
1990 unsigned long flags
;
1992 if (!IS_ERR_OR_NULL(udc_controller
->transceiver
))
1993 otg_set_peripheral(udc_controller
->transceiver
->otg
, NULL
);
1995 /* stop DR, disable intr */
1996 dr_controller_stop(udc_controller
);
1998 /* in fact, no needed */
1999 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2000 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2001 udc_controller
->ep0_dir
= 0;
2003 /* stand operation */
2004 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2005 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2006 nuke(&udc_controller
->eps
[0], -ESHUTDOWN
);
2007 list_for_each_entry(loop_ep
, &udc_controller
->gadget
.ep_list
,
2009 nuke(loop_ep
, -ESHUTDOWN
);
2010 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2012 udc_controller
->driver
= NULL
;
2017 /*-------------------------------------------------------------------------
2018 PROC File System Support
2019 -------------------------------------------------------------------------*/
2020 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2022 #include <linux/seq_file.h>
2024 static const char proc_filename
[] = "driver/fsl_usb2_udc";
2026 static int fsl_proc_read(struct seq_file
*m
, void *v
)
2028 unsigned long flags
;
2031 struct fsl_ep
*ep
= NULL
;
2032 struct fsl_req
*req
;
2034 struct fsl_udc
*udc
= udc_controller
;
2036 spin_lock_irqsave(&udc
->lock
, flags
);
2038 /* ------basic driver information ---- */
2042 "Gadget driver: %s\n\n",
2043 driver_name
, DRIVER_VERSION
,
2044 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
2046 /* ------ DR Registers ----- */
2047 tmp_reg
= fsl_readl(&dr_regs
->usbcmd
);
2052 (tmp_reg
& USB_CMD_SUTW
) ? 1 : 0,
2053 (tmp_reg
& USB_CMD_RUN_STOP
) ? "Run" : "Stop");
2055 tmp_reg
= fsl_readl(&dr_regs
->usbsts
);
2058 "Dr Suspend: %d Reset Received: %d System Error: %s "
2059 "USB Error Interrupt: %s\n\n",
2060 (tmp_reg
& USB_STS_SUSPEND
) ? 1 : 0,
2061 (tmp_reg
& USB_STS_RESET
) ? 1 : 0,
2062 (tmp_reg
& USB_STS_SYS_ERR
) ? "Err" : "Normal",
2063 (tmp_reg
& USB_STS_ERR
) ? "Err detected" : "No err");
2065 tmp_reg
= fsl_readl(&dr_regs
->usbintr
);
2067 "USB Interrupt Enable Reg:\n"
2068 "Sleep Enable: %d SOF Received Enable: %d "
2069 "Reset Enable: %d\n"
2070 "System Error Enable: %d "
2071 "Port Change Dectected Enable: %d\n"
2072 "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
2073 (tmp_reg
& USB_INTR_DEVICE_SUSPEND
) ? 1 : 0,
2074 (tmp_reg
& USB_INTR_SOF_EN
) ? 1 : 0,
2075 (tmp_reg
& USB_INTR_RESET_EN
) ? 1 : 0,
2076 (tmp_reg
& USB_INTR_SYS_ERR_EN
) ? 1 : 0,
2077 (tmp_reg
& USB_INTR_PTC_DETECT_EN
) ? 1 : 0,
2078 (tmp_reg
& USB_INTR_ERR_INT_EN
) ? 1 : 0,
2079 (tmp_reg
& USB_INTR_INT_EN
) ? 1 : 0);
2081 tmp_reg
= fsl_readl(&dr_regs
->frindex
);
2083 "USB Frame Index Reg: Frame Number is 0x%x\n\n",
2084 (tmp_reg
& USB_FRINDEX_MASKS
));
2086 tmp_reg
= fsl_readl(&dr_regs
->deviceaddr
);
2088 "USB Device Address Reg: Device Addr is 0x%x\n\n",
2089 (tmp_reg
& USB_DEVICE_ADDRESS_MASK
));
2091 tmp_reg
= fsl_readl(&dr_regs
->endpointlistaddr
);
2093 "USB Endpoint List Address Reg: "
2094 "Device Addr is 0x%x\n\n",
2095 (tmp_reg
& USB_EP_LIST_ADDRESS_MASK
));
2097 tmp_reg
= fsl_readl(&dr_regs
->portsc1
);
2099 "USB Port Status&Control Reg:\n"
2100 "Port Transceiver Type : %s Port Speed: %s\n"
2101 "PHY Low Power Suspend: %s Port Reset: %s "
2102 "Port Suspend Mode: %s\n"
2103 "Over-current Change: %s "
2104 "Port Enable/Disable Change: %s\n"
2105 "Port Enabled/Disabled: %s "
2106 "Current Connect Status: %s\n\n", ( {
2108 switch (tmp_reg
& PORTSCX_PTS_FSLS
) {
2109 case PORTSCX_PTS_UTMI
:
2111 case PORTSCX_PTS_ULPI
:
2113 case PORTSCX_PTS_FSLS
:
2114 s
= "FS/LS Serial"; break;
2119 usb_speed_string(portscx_device_speed(tmp_reg
)),
2120 (tmp_reg
& PORTSCX_PHY_LOW_POWER_SPD
) ?
2121 "Normal PHY mode" : "Low power mode",
2122 (tmp_reg
& PORTSCX_PORT_RESET
) ? "In Reset" :
2124 (tmp_reg
& PORTSCX_PORT_SUSPEND
) ? "In " : "Not in",
2125 (tmp_reg
& PORTSCX_OVER_CURRENT_CHG
) ? "Dected" :
2127 (tmp_reg
& PORTSCX_PORT_EN_DIS_CHANGE
) ? "Disable" :
2129 (tmp_reg
& PORTSCX_PORT_ENABLE
) ? "Enable" :
2131 (tmp_reg
& PORTSCX_CURRENT_CONNECT_STATUS
) ?
2132 "Attached" : "Not-Att");
2134 tmp_reg
= fsl_readl(&dr_regs
->usbmode
);
2136 "USB Mode Reg: Controller Mode is: %s\n\n", ( {
2138 switch (tmp_reg
& USB_MODE_CTRL_MODE_HOST
) {
2139 case USB_MODE_CTRL_MODE_IDLE
:
2141 case USB_MODE_CTRL_MODE_DEVICE
:
2142 s
= "Device Controller"; break;
2143 case USB_MODE_CTRL_MODE_HOST
:
2144 s
= "Host Controller"; break;
2151 tmp_reg
= fsl_readl(&dr_regs
->endptsetupstat
);
2153 "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
2154 (tmp_reg
& EP_SETUP_STATUS_MASK
));
2156 for (i
= 0; i
< udc
->max_ep
/ 2; i
++) {
2157 tmp_reg
= fsl_readl(&dr_regs
->endptctrl
[i
]);
2158 seq_printf(m
, "EP Ctrl Reg [0x%x]: = [0x%x]\n", i
, tmp_reg
);
2160 tmp_reg
= fsl_readl(&dr_regs
->endpointprime
);
2161 seq_printf(m
, "EP Prime Reg = [0x%x]\n\n", tmp_reg
);
2163 #ifndef CONFIG_ARCH_MXC
2164 if (udc
->pdata
->have_sysif_regs
) {
2165 tmp_reg
= usb_sys_regs
->snoop1
;
2166 seq_printf(m
, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg
);
2168 tmp_reg
= usb_sys_regs
->control
;
2169 seq_printf(m
, "General Control Reg : = [0x%x]\n\n", tmp_reg
);
2173 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2175 seq_printf(m
, "For %s Maxpkt is 0x%x index is 0x%x\n",
2176 ep
->ep
.name
, ep_maxpacket(ep
), ep_index(ep
));
2178 if (list_empty(&ep
->queue
)) {
2179 seq_puts(m
, "its req queue is empty\n\n");
2181 list_for_each_entry(req
, &ep
->queue
, queue
) {
2183 "req %p actual 0x%x length 0x%x buf %p\n",
2184 &req
->req
, req
->req
.actual
,
2185 req
->req
.length
, req
->req
.buf
);
2188 /* other gadget->eplist ep */
2189 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
2192 "\nFor %s Maxpkt is 0x%x "
2194 ep
->ep
.name
, ep_maxpacket(ep
),
2197 if (list_empty(&ep
->queue
)) {
2198 seq_puts(m
, "its req queue is empty\n\n");
2200 list_for_each_entry(req
, &ep
->queue
, queue
) {
2202 "req %p actual 0x%x length "
2204 &req
->req
, req
->req
.actual
,
2205 req
->req
.length
, req
->req
.buf
);
2206 } /* end for each_entry of ep req */
2207 } /* end for else */
2208 } /* end for if(ep->queue) */
2209 } /* end (ep->desc) */
2211 spin_unlock_irqrestore(&udc
->lock
, flags
);
2216 * seq_file wrappers for procfile show routines.
2218 static int fsl_proc_open(struct inode
*inode
, struct file
*file
)
2220 return single_open(file
, fsl_proc_read
, NULL
);
2223 static const struct file_operations fsl_proc_fops
= {
2224 .open
= fsl_proc_open
,
2226 .llseek
= seq_lseek
,
2227 .release
= single_release
,
2230 #define create_proc_file() proc_create(proc_filename, 0, NULL, &fsl_proc_fops)
2231 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2233 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2235 #define create_proc_file() do {} while (0)
2236 #define remove_proc_file() do {} while (0)
2238 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2240 /*-------------------------------------------------------------------------*/
2242 /* Release udc structures */
2243 static void fsl_udc_release(struct device
*dev
)
2245 complete(udc_controller
->done
);
2246 dma_free_coherent(dev
->parent
, udc_controller
->ep_qh_size
,
2247 udc_controller
->ep_qh
, udc_controller
->ep_qh_dma
);
2248 kfree(udc_controller
);
2251 /******************************************************************
2252 Internal structure setup functions
2253 *******************************************************************/
2254 /*------------------------------------------------------------------
2255 * init resource for globle controller
2256 * Return the udc handle on success or NULL on failure
2257 ------------------------------------------------------------------*/
2258 static int struct_udc_setup(struct fsl_udc
*udc
,
2259 struct platform_device
*pdev
)
2261 struct fsl_usb2_platform_data
*pdata
;
2264 pdata
= dev_get_platdata(&pdev
->dev
);
2265 udc
->phy_mode
= pdata
->phy_mode
;
2267 udc
->eps
= kzalloc(sizeof(struct fsl_ep
) * udc
->max_ep
, GFP_KERNEL
);
2271 /* initialized QHs, take care of alignment */
2272 size
= udc
->max_ep
* sizeof(struct ep_queue_head
);
2273 if (size
< QH_ALIGNMENT
)
2274 size
= QH_ALIGNMENT
;
2275 else if ((size
% QH_ALIGNMENT
) != 0) {
2276 size
+= QH_ALIGNMENT
+ 1;
2277 size
&= ~(QH_ALIGNMENT
- 1);
2279 udc
->ep_qh
= dma_alloc_coherent(&pdev
->dev
, size
,
2280 &udc
->ep_qh_dma
, GFP_KERNEL
);
2282 ERR("malloc QHs for udc failed\n");
2287 udc
->ep_qh_size
= size
;
2289 /* Initialize ep0 status request structure */
2290 /* FIXME: fsl_alloc_request() ignores ep argument */
2291 udc
->status_req
= container_of(fsl_alloc_request(NULL
, GFP_KERNEL
),
2292 struct fsl_req
, req
);
2293 /* allocate a small amount of memory to get valid address */
2294 udc
->status_req
->req
.buf
= kmalloc(8, GFP_KERNEL
);
2296 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2297 udc
->usb_state
= USB_STATE_POWERED
;
2299 udc
->remote_wakeup
= 0; /* default to 0 on reset */
2304 /*----------------------------------------------------------------
2305 * Setup the fsl_ep struct for eps
2306 * Link fsl_ep->ep to gadget->ep_list
2307 * ep0out is not used so do nothing here
2308 * ep0in should be taken care
2309 *--------------------------------------------------------------*/
2310 static int struct_ep_setup(struct fsl_udc
*udc
, unsigned char index
,
2311 char *name
, int link
)
2313 struct fsl_ep
*ep
= &udc
->eps
[index
];
2316 strcpy(ep
->name
, name
);
2317 ep
->ep
.name
= ep
->name
;
2319 ep
->ep
.ops
= &fsl_ep_ops
;
2323 ep
->ep
.caps
.type_control
= true;
2325 ep
->ep
.caps
.type_iso
= true;
2326 ep
->ep
.caps
.type_bulk
= true;
2327 ep
->ep
.caps
.type_int
= true;
2331 ep
->ep
.caps
.dir_in
= true;
2333 ep
->ep
.caps
.dir_out
= true;
2335 /* for ep0: maxP defined in desc
2336 * for other eps, maxP is set by epautoconfig() called by gadget layer
2338 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
2340 /* the queue lists any req for this ep */
2341 INIT_LIST_HEAD(&ep
->queue
);
2343 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
2345 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2346 ep
->gadget
= &udc
->gadget
;
2347 ep
->qh
= &udc
->ep_qh
[index
];
2352 /* Driver probe function
2353 * all intialization operations implemented here except enabling usb_intr reg
2354 * board setup should have been done in the platform code
2356 static int fsl_udc_probe(struct platform_device
*pdev
)
2358 struct fsl_usb2_platform_data
*pdata
;
2359 struct resource
*res
;
2364 udc_controller
= kzalloc(sizeof(struct fsl_udc
), GFP_KERNEL
);
2365 if (udc_controller
== NULL
)
2368 pdata
= dev_get_platdata(&pdev
->dev
);
2369 udc_controller
->pdata
= pdata
;
2370 spin_lock_init(&udc_controller
->lock
);
2371 udc_controller
->stopped
= 1;
2373 #ifdef CONFIG_USB_OTG
2374 if (pdata
->operating_mode
== FSL_USB2_DR_OTG
) {
2375 udc_controller
->transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
2376 if (IS_ERR_OR_NULL(udc_controller
->transceiver
)) {
2377 ERR("Can't find OTG driver!\n");
2384 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2390 if (pdata
->operating_mode
== FSL_USB2_DR_DEVICE
) {
2391 if (!request_mem_region(res
->start
, resource_size(res
),
2393 ERR("request mem region for %s failed\n", pdev
->name
);
2399 dr_regs
= ioremap(res
->start
, resource_size(res
));
2402 goto err_release_mem_region
;
2405 pdata
->regs
= (void __iomem
*)dr_regs
;
2408 * do platform specific init: check the clock, grab/config pins, etc.
2410 if (pdata
->init
&& pdata
->init(pdev
)) {
2412 goto err_iounmap_noclk
;
2415 /* Set accessors only after pdata->init() ! */
2416 fsl_set_accessors(pdata
);
2418 #ifndef CONFIG_ARCH_MXC
2419 if (pdata
->have_sysif_regs
)
2420 usb_sys_regs
= (void *)dr_regs
+ USB_DR_SYS_OFFSET
;
2423 /* Initialize USB clocks */
2424 ret
= fsl_udc_clk_init(pdev
);
2426 goto err_iounmap_noclk
;
2428 /* Read Device Controller Capability Parameters register */
2429 dccparams
= fsl_readl(&dr_regs
->dccparams
);
2430 if (!(dccparams
& DCCPARAMS_DC
)) {
2431 ERR("This SOC doesn't support device role\n");
2435 /* Get max device endpoints */
2436 /* DEN is bidirectional ep number, max_ep doubles the number */
2437 udc_controller
->max_ep
= (dccparams
& DCCPARAMS_DEN_MASK
) * 2;
2439 udc_controller
->irq
= platform_get_irq(pdev
, 0);
2440 if (!udc_controller
->irq
) {
2445 ret
= request_irq(udc_controller
->irq
, fsl_udc_irq
, IRQF_SHARED
,
2446 driver_name
, udc_controller
);
2448 ERR("cannot request irq %d err %d\n",
2449 udc_controller
->irq
, ret
);
2453 /* Initialize the udc structure including QH member and other member */
2454 if (struct_udc_setup(udc_controller
, pdev
)) {
2455 ERR("Can't initialize udc data structure\n");
2460 if (IS_ERR_OR_NULL(udc_controller
->transceiver
)) {
2461 /* initialize usb hw reg except for regs for EP,
2462 * leave usbintr reg untouched */
2463 dr_controller_setup(udc_controller
);
2466 ret
= fsl_udc_clk_finalize(pdev
);
2470 /* Setup gadget structure */
2471 udc_controller
->gadget
.ops
= &fsl_gadget_ops
;
2472 udc_controller
->gadget
.max_speed
= USB_SPEED_HIGH
;
2473 udc_controller
->gadget
.ep0
= &udc_controller
->eps
[0].ep
;
2474 INIT_LIST_HEAD(&udc_controller
->gadget
.ep_list
);
2475 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2476 udc_controller
->gadget
.name
= driver_name
;
2478 /* Setup gadget.dev and register with kernel */
2479 dev_set_name(&udc_controller
->gadget
.dev
, "gadget");
2480 udc_controller
->gadget
.dev
.of_node
= pdev
->dev
.of_node
;
2482 if (!IS_ERR_OR_NULL(udc_controller
->transceiver
))
2483 udc_controller
->gadget
.is_otg
= 1;
2485 /* setup QH and epctrl for ep0 */
2486 ep0_setup(udc_controller
);
2488 /* setup udc->eps[] for ep0 */
2489 struct_ep_setup(udc_controller
, 0, "ep0", 0);
2490 /* for ep0: the desc defined here;
2491 * for other eps, gadget layer called ep_enable with defined desc
2493 udc_controller
->eps
[0].ep
.desc
= &fsl_ep0_desc
;
2494 usb_ep_set_maxpacket_limit(&udc_controller
->eps
[0].ep
,
2495 USB_MAX_CTRL_PAYLOAD
);
2497 /* setup the udc->eps[] for non-control endpoints and link
2498 * to gadget.ep_list */
2499 for (i
= 1; i
< (int)(udc_controller
->max_ep
/ 2); i
++) {
2502 sprintf(name
, "ep%dout", i
);
2503 struct_ep_setup(udc_controller
, i
* 2, name
, 1);
2504 sprintf(name
, "ep%din", i
);
2505 struct_ep_setup(udc_controller
, i
* 2 + 1, name
, 1);
2508 /* use dma_pool for TD management */
2509 udc_controller
->td_pool
= dma_pool_create("udc_td", &pdev
->dev
,
2510 sizeof(struct ep_td_struct
),
2511 DTD_ALIGNMENT
, UDC_DMA_BOUNDARY
);
2512 if (udc_controller
->td_pool
== NULL
) {
2517 ret
= usb_add_gadget_udc_release(&pdev
->dev
, &udc_controller
->gadget
,
2526 dma_pool_destroy(udc_controller
->td_pool
);
2528 free_irq(udc_controller
->irq
, udc_controller
);
2532 fsl_udc_clk_release();
2535 err_release_mem_region
:
2536 if (pdata
->operating_mode
== FSL_USB2_DR_DEVICE
)
2537 release_mem_region(res
->start
, resource_size(res
));
2539 kfree(udc_controller
);
2540 udc_controller
= NULL
;
2544 /* Driver removal function
2545 * Free resources and finish pending transactions
2547 static int fsl_udc_remove(struct platform_device
*pdev
)
2549 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2550 struct fsl_usb2_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2552 DECLARE_COMPLETION_ONSTACK(done
);
2554 if (!udc_controller
)
2557 udc_controller
->done
= &done
;
2558 usb_del_gadget_udc(&udc_controller
->gadget
);
2560 fsl_udc_clk_release();
2562 /* DR has been stopped in usb_gadget_unregister_driver() */
2565 /* Free allocated memory */
2566 kfree(udc_controller
->status_req
->req
.buf
);
2567 kfree(udc_controller
->status_req
);
2568 kfree(udc_controller
->eps
);
2570 dma_pool_destroy(udc_controller
->td_pool
);
2571 free_irq(udc_controller
->irq
, udc_controller
);
2573 if (pdata
->operating_mode
== FSL_USB2_DR_DEVICE
)
2574 release_mem_region(res
->start
, resource_size(res
));
2576 /* free udc --wait for the release() finished */
2577 wait_for_completion(&done
);
2580 * do platform specific un-initialization:
2581 * release iomux pins, etc.
2589 /*-----------------------------------------------------------------
2590 * Modify Power management attributes
2591 * Used by OTG statemachine to disable gadget temporarily
2592 -----------------------------------------------------------------*/
2593 static int fsl_udc_suspend(struct platform_device
*pdev
, pm_message_t state
)
2595 dr_controller_stop(udc_controller
);
2599 /*-----------------------------------------------------------------
2600 * Invoked on USB resume. May be called in_interrupt.
2601 * Here we start the DR controller and enable the irq
2602 *-----------------------------------------------------------------*/
2603 static int fsl_udc_resume(struct platform_device
*pdev
)
2605 /* Enable DR irq reg and set controller Run */
2606 if (udc_controller
->stopped
) {
2607 dr_controller_setup(udc_controller
);
2608 dr_controller_run(udc_controller
);
2610 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2611 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2612 udc_controller
->ep0_dir
= 0;
2616 static int fsl_udc_otg_suspend(struct device
*dev
, pm_message_t state
)
2618 struct fsl_udc
*udc
= udc_controller
;
2621 mode
= fsl_readl(&dr_regs
->usbmode
) & USB_MODE_CTRL_MODE_MASK
;
2623 pr_debug("%s(): mode 0x%x stopped %d\n", __func__
, mode
, udc
->stopped
);
2626 * If the controller is already stopped, then this must be a
2627 * PM suspend. Remember this fact, so that we will leave the
2628 * controller stopped at PM resume time.
2631 pr_debug("gadget already stopped, leaving early\n");
2632 udc
->already_stopped
= 1;
2636 if (mode
!= USB_MODE_CTRL_MODE_DEVICE
) {
2637 pr_debug("gadget not in device mode, leaving early\n");
2641 /* stop the controller */
2642 usbcmd
= fsl_readl(&dr_regs
->usbcmd
) & ~USB_CMD_RUN_STOP
;
2643 fsl_writel(usbcmd
, &dr_regs
->usbcmd
);
2647 pr_info("USB Gadget suspended\n");
2652 static int fsl_udc_otg_resume(struct device
*dev
)
2654 pr_debug("%s(): stopped %d already_stopped %d\n", __func__
,
2655 udc_controller
->stopped
, udc_controller
->already_stopped
);
2658 * If the controller was stopped at suspend time, then
2659 * don't resume it now.
2661 if (udc_controller
->already_stopped
) {
2662 udc_controller
->already_stopped
= 0;
2663 pr_debug("gadget was already stopped, leaving early\n");
2667 pr_info("USB Gadget resume\n");
2669 return fsl_udc_resume(NULL
);
2671 /*-------------------------------------------------------------------------
2672 Register entry point for the peripheral controller driver
2673 --------------------------------------------------------------------------*/
2674 static const struct platform_device_id fsl_udc_devtype
[] = {
2676 .name
= "imx-udc-mx27",
2678 .name
= "imx-udc-mx51",
2683 MODULE_DEVICE_TABLE(platform
, fsl_udc_devtype
);
2684 static struct platform_driver udc_driver
= {
2685 .remove
= fsl_udc_remove
,
2686 /* Just for FSL i.mx SoC currently */
2687 .id_table
= fsl_udc_devtype
,
2688 /* these suspend and resume are not usb suspend and resume */
2689 .suspend
= fsl_udc_suspend
,
2690 .resume
= fsl_udc_resume
,
2692 .name
= driver_name
,
2693 /* udc suspend/resume called from OTG driver */
2694 .suspend
= fsl_udc_otg_suspend
,
2695 .resume
= fsl_udc_otg_resume
,
2699 module_platform_driver_probe(udc_driver
, fsl_udc_probe
);
2701 MODULE_DESCRIPTION(DRIVER_DESC
);
2702 MODULE_AUTHOR(DRIVER_AUTHOR
);
2703 MODULE_LICENSE("GPL");
2704 MODULE_ALIAS("platform:fsl-usb2-udc");