2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
3 * Author: Chao Xie <chao.xie@marvell.com>
4 * Neil Zhang <zhangwm@marvell.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmapool.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/ioport.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/timer.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/moduleparam.h>
27 #include <linux/device.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/otg.h>
33 #include <linux/irq.h>
34 #include <linux/platform_device.h>
35 #include <linux/clk.h>
36 #include <linux/platform_data/mv_usb.h>
37 #include <asm/system.h>
38 #include <asm/unaligned.h>
42 #define DRIVER_DESC "Marvell PXA USB Device Controller driver"
43 #define DRIVER_VERSION "8 Nov 2010"
45 #define ep_dir(ep) (((ep)->ep_num == 0) ? \
46 ((ep)->udc->ep0_dir) : ((ep)->direction))
48 /* timeout value -- usec */
49 #define RESET_TIMEOUT 10000
50 #define FLUSH_TIMEOUT 10000
51 #define EPSTATUS_TIMEOUT 10000
52 #define PRIME_TIMEOUT 10000
53 #define READSAFE_TIMEOUT 1000
54 #define DTD_TIMEOUT 1000
56 #define LOOPS_USEC_SHIFT 4
57 #define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
58 #define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
60 static DECLARE_COMPLETION(release_done
);
62 static const char driver_name
[] = "mv_udc";
63 static const char driver_desc
[] = DRIVER_DESC
;
65 /* controller device global variable */
66 static struct mv_udc
*the_controller
;
69 static void nuke(struct mv_ep
*ep
, int status
);
70 static void stop_activity(struct mv_udc
*udc
, struct usb_gadget_driver
*driver
);
72 /* for endpoint 0 operations */
73 static const struct usb_endpoint_descriptor mv_ep0_desc
= {
74 .bLength
= USB_DT_ENDPOINT_SIZE
,
75 .bDescriptorType
= USB_DT_ENDPOINT
,
76 .bEndpointAddress
= 0,
77 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
78 .wMaxPacketSize
= EP0_MAX_PKT_SIZE
,
81 static void ep0_reset(struct mv_udc
*udc
)
88 for (i
= 0; i
< 2; i
++) {
93 ep
->dqh
= &udc
->ep_dqh
[i
];
95 /* configure ep0 endpoint capabilities in dQH */
96 ep
->dqh
->max_packet_length
=
97 (EP0_MAX_PKT_SIZE
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
100 ep
->dqh
->next_dtd_ptr
= EP_QUEUE_HEAD_NEXT_TERMINATE
;
102 epctrlx
= readl(&udc
->op_regs
->epctrlx
[0]);
104 epctrlx
|= EPCTRL_TX_ENABLE
105 | (USB_ENDPOINT_XFER_CONTROL
106 << EPCTRL_TX_EP_TYPE_SHIFT
);
109 epctrlx
|= EPCTRL_RX_ENABLE
110 | (USB_ENDPOINT_XFER_CONTROL
111 << EPCTRL_RX_EP_TYPE_SHIFT
);
114 writel(epctrlx
, &udc
->op_regs
->epctrlx
[0]);
118 /* protocol ep0 stall, will automatically be cleared on new transaction */
119 static void ep0_stall(struct mv_udc
*udc
)
123 /* set TX and RX to stall */
124 epctrlx
= readl(&udc
->op_regs
->epctrlx
[0]);
125 epctrlx
|= EPCTRL_RX_EP_STALL
| EPCTRL_TX_EP_STALL
;
126 writel(epctrlx
, &udc
->op_regs
->epctrlx
[0]);
128 /* update ep0 state */
129 udc
->ep0_state
= WAIT_FOR_SETUP
;
130 udc
->ep0_dir
= EP_DIR_OUT
;
133 static int process_ep_req(struct mv_udc
*udc
, int index
,
134 struct mv_req
*curr_req
)
136 struct mv_dtd
*curr_dtd
;
137 struct mv_dqh
*curr_dqh
;
138 int td_complete
, actual
, remaining_length
;
144 curr_dqh
= &udc
->ep_dqh
[index
];
145 direction
= index
% 2;
147 curr_dtd
= curr_req
->head
;
149 actual
= curr_req
->req
.length
;
151 for (i
= 0; i
< curr_req
->dtd_count
; i
++) {
152 if (curr_dtd
->size_ioc_sts
& DTD_STATUS_ACTIVE
) {
153 dev_dbg(&udc
->dev
->dev
, "%s, dTD not completed\n",
154 udc
->eps
[index
].name
);
158 errors
= curr_dtd
->size_ioc_sts
& DTD_ERROR_MASK
;
161 (curr_dtd
->size_ioc_sts
& DTD_PACKET_SIZE
)
162 >> DTD_LENGTH_BIT_POS
;
163 actual
-= remaining_length
;
165 if (remaining_length
) {
167 dev_dbg(&udc
->dev
->dev
,
168 "TX dTD remains data\n");
175 dev_info(&udc
->dev
->dev
,
176 "complete_tr error: ep=%d %s: error = 0x%x\n",
177 index
>> 1, direction
? "SEND" : "RECV",
179 if (errors
& DTD_STATUS_HALTED
) {
180 /* Clear the errors and Halt condition */
181 curr_dqh
->size_ioc_int_sts
&= ~errors
;
183 } else if (errors
& DTD_STATUS_DATA_BUFF_ERR
) {
185 } else if (errors
& DTD_STATUS_TRANSACTION_ERR
) {
189 if (i
!= curr_req
->dtd_count
- 1)
190 curr_dtd
= (struct mv_dtd
*)curr_dtd
->next_dtd_virt
;
195 if (direction
== EP_DIR_OUT
)
196 bit_pos
= 1 << curr_req
->ep
->ep_num
;
198 bit_pos
= 1 << (16 + curr_req
->ep
->ep_num
);
200 while ((curr_dqh
->curr_dtd_ptr
== curr_dtd
->td_dma
)) {
201 if (curr_dtd
->dtd_next
== EP_QUEUE_HEAD_NEXT_TERMINATE
) {
202 while (readl(&udc
->op_regs
->epstatus
) & bit_pos
)
209 curr_req
->req
.actual
= actual
;
215 * done() - retire a request; caller blocked irqs
216 * @status : request status to be set, only works when
217 * request is still in progress.
219 static void done(struct mv_ep
*ep
, struct mv_req
*req
, int status
)
221 struct mv_udc
*udc
= NULL
;
222 unsigned char stopped
= ep
->stopped
;
223 struct mv_dtd
*curr_td
, *next_td
;
226 udc
= (struct mv_udc
*)ep
->udc
;
227 /* Removed the req from fsl_ep->queue */
228 list_del_init(&req
->queue
);
230 /* req.status should be set as -EINPROGRESS in ep_queue() */
231 if (req
->req
.status
== -EINPROGRESS
)
232 req
->req
.status
= status
;
234 status
= req
->req
.status
;
236 /* Free dtd for the request */
238 for (j
= 0; j
< req
->dtd_count
; j
++) {
240 if (j
!= req
->dtd_count
- 1)
241 next_td
= curr_td
->next_dtd_virt
;
242 dma_pool_free(udc
->dtd_pool
, curr_td
, curr_td
->td_dma
);
246 dma_unmap_single(ep
->udc
->gadget
.dev
.parent
,
247 req
->req
.dma
, req
->req
.length
,
248 ((ep_dir(ep
) == EP_DIR_IN
) ?
249 DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
250 req
->req
.dma
= DMA_ADDR_INVALID
;
253 dma_sync_single_for_cpu(ep
->udc
->gadget
.dev
.parent
,
254 req
->req
.dma
, req
->req
.length
,
255 ((ep_dir(ep
) == EP_DIR_IN
) ?
256 DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
258 if (status
&& (status
!= -ESHUTDOWN
))
259 dev_info(&udc
->dev
->dev
, "complete %s req %p stat %d len %u/%u",
260 ep
->ep
.name
, &req
->req
, status
,
261 req
->req
.actual
, req
->req
.length
);
265 spin_unlock(&ep
->udc
->lock
);
267 * complete() is from gadget layer,
268 * eg fsg->bulk_in_complete()
270 if (req
->req
.complete
)
271 req
->req
.complete(&ep
->ep
, &req
->req
);
273 spin_lock(&ep
->udc
->lock
);
274 ep
->stopped
= stopped
;
277 static int queue_dtd(struct mv_ep
*ep
, struct mv_req
*req
)
279 u32 tmp
, epstatus
, bit_pos
, direction
;
283 int readsafe
, retval
= 0;
286 direction
= ep_dir(ep
);
287 dqh
= &(udc
->ep_dqh
[ep
->ep_num
* 2 + direction
]);
288 bit_pos
= 1 << (((direction
== EP_DIR_OUT
) ? 0 : 16) + ep
->ep_num
);
290 /* check if the pipe is empty */
291 if (!(list_empty(&ep
->queue
))) {
292 struct mv_req
*lastreq
;
293 lastreq
= list_entry(ep
->queue
.prev
, struct mv_req
, queue
);
294 lastreq
->tail
->dtd_next
=
295 req
->head
->td_dma
& EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
296 if (readl(&udc
->op_regs
->epprime
) & bit_pos
) {
297 loops
= LOOPS(PRIME_TIMEOUT
);
298 while (readl(&udc
->op_regs
->epprime
) & bit_pos
) {
306 if (readl(&udc
->op_regs
->epstatus
) & bit_pos
)
310 loops
= LOOPS(READSAFE_TIMEOUT
);
311 while (readsafe
== 0) {
316 /* start with setting the semaphores */
317 tmp
= readl(&udc
->op_regs
->usbcmd
);
318 tmp
|= USBCMD_ATDTW_TRIPWIRE_SET
;
319 writel(tmp
, &udc
->op_regs
->usbcmd
);
321 /* read the endpoint status */
322 epstatus
= readl(&udc
->op_regs
->epstatus
) & bit_pos
;
325 * Reread the ATDTW semaphore bit to check if it is
326 * cleared. When hardware see a hazard, it will clear
327 * the bit or else we remain set to 1 and we can
328 * proceed with priming of endpoint if not already
331 if (readl(&udc
->op_regs
->usbcmd
)
332 & USBCMD_ATDTW_TRIPWIRE_SET
) {
339 /* Clear the semaphore */
340 tmp
= readl(&udc
->op_regs
->usbcmd
);
341 tmp
&= USBCMD_ATDTW_TRIPWIRE_CLEAR
;
342 writel(tmp
, &udc
->op_regs
->usbcmd
);
344 /* If endpoint is not active, we activate it now. */
346 if (direction
== EP_DIR_IN
) {
347 struct mv_dtd
*curr_dtd
= dma_to_virt(
348 &udc
->dev
->dev
, dqh
->curr_dtd_ptr
);
350 loops
= LOOPS(DTD_TIMEOUT
);
351 while (curr_dtd
->size_ioc_sts
352 & DTD_STATUS_ACTIVE
) {
361 /* No other transfers on the queue */
363 /* Write dQH next pointer and terminate bit to 0 */
364 dqh
->next_dtd_ptr
= req
->head
->td_dma
365 & EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
366 dqh
->size_ioc_int_sts
= 0;
369 * Ensure that updates to the QH will
370 * occur before priming.
374 /* Prime the Endpoint */
375 writel(bit_pos
, &udc
->op_regs
->epprime
);
378 /* Write dQH next pointer and terminate bit to 0 */
379 dqh
->next_dtd_ptr
= req
->head
->td_dma
380 & EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
381 dqh
->size_ioc_int_sts
= 0;
383 /* Ensure that updates to the QH will occur before priming. */
386 /* Prime the Endpoint */
387 writel(bit_pos
, &udc
->op_regs
->epprime
);
389 if (direction
== EP_DIR_IN
) {
390 /* FIXME add status check after prime the IN ep */
392 u32 curr_dtd_ptr
= dqh
->curr_dtd_ptr
;
394 loops
= LOOPS(DTD_TIMEOUT
);
396 while ((curr_dtd_ptr
!= req
->head
->td_dma
)) {
397 curr_dtd_ptr
= dqh
->curr_dtd_ptr
;
399 dev_err(&udc
->dev
->dev
,
400 "failed to prime %s\n",
408 if (loops
== (LOOPS(DTD_TIMEOUT
) >> 2)) {
411 dev_info(&udc
->dev
->dev
,
414 &udc
->op_regs
->epprime
);
424 static struct mv_dtd
*build_dtd(struct mv_req
*req
, unsigned *length
,
425 dma_addr_t
*dma
, int *is_last
)
431 /* how big will this transfer be? */
432 *length
= min(req
->req
.length
- req
->req
.actual
,
433 (unsigned)EP_MAX_LENGTH_TRANSFER
);
438 * Be careful that no _GFP_HIGHMEM is set,
439 * or we can not use dma_to_virt
441 dtd
= dma_pool_alloc(udc
->dtd_pool
, GFP_KERNEL
, dma
);
446 /* initialize buffer page pointers */
447 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
448 dtd
->buff_ptr0
= cpu_to_le32(temp
);
450 dtd
->buff_ptr1
= cpu_to_le32(temp
+ 0x1000);
451 dtd
->buff_ptr2
= cpu_to_le32(temp
+ 0x2000);
452 dtd
->buff_ptr3
= cpu_to_le32(temp
+ 0x3000);
453 dtd
->buff_ptr4
= cpu_to_le32(temp
+ 0x4000);
455 req
->req
.actual
+= *length
;
457 /* zlp is needed if req->req.zero is set */
459 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
463 } else if (req
->req
.length
== req
->req
.actual
)
468 /* Fill in the transfer size; set active bit */
469 temp
= ((*length
<< DTD_LENGTH_BIT_POS
) | DTD_STATUS_ACTIVE
);
471 /* Enable interrupt for the last dtd of a request */
472 if (*is_last
&& !req
->req
.no_interrupt
)
475 dtd
->size_ioc_sts
= temp
;
482 /* generate dTD linked list for a request */
483 static int req_to_dtd(struct mv_req
*req
)
486 int is_last
, is_first
= 1;
487 struct mv_dtd
*dtd
, *last_dtd
= NULL
;
494 dtd
= build_dtd(req
, &count
, &dma
, &is_last
);
502 last_dtd
->dtd_next
= dma
;
503 last_dtd
->next_dtd_virt
= dtd
;
509 /* set terminate bit to 1 for the last dTD */
510 dtd
->dtd_next
= DTD_NEXT_TERMINATE
;
517 static int mv_ep_enable(struct usb_ep
*_ep
,
518 const struct usb_endpoint_descriptor
*desc
)
524 u32 bit_pos
, epctrlx
, direction
;
525 unsigned char zlt
= 0, ios
= 0, mult
= 0;
528 ep
= container_of(_ep
, struct mv_ep
, ep
);
531 if (!_ep
|| !desc
|| ep
->desc
532 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
535 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
538 direction
= ep_dir(ep
);
539 max
= usb_endpoint_maxp(desc
);
542 * disable HW zero length termination select
543 * driver handles zero length packet through req->req.zero
547 bit_pos
= 1 << ((direction
== EP_DIR_OUT
? 0 : 16) + ep
->ep_num
);
549 /* Check if the Endpoint is Primed */
550 if ((readl(&udc
->op_regs
->epprime
) & bit_pos
)
551 || (readl(&udc
->op_regs
->epstatus
) & bit_pos
)) {
552 dev_info(&udc
->dev
->dev
,
553 "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
554 " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
555 (unsigned)ep
->ep_num
, direction
? "SEND" : "RECV",
556 (unsigned)readl(&udc
->op_regs
->epprime
),
557 (unsigned)readl(&udc
->op_regs
->epstatus
),
561 /* Set the max packet length, interrupt on Setup and Mult fields */
562 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
563 case USB_ENDPOINT_XFER_BULK
:
567 case USB_ENDPOINT_XFER_CONTROL
:
569 case USB_ENDPOINT_XFER_INT
:
572 case USB_ENDPOINT_XFER_ISOC
:
573 /* Calculate transactions needed for high bandwidth iso */
574 mult
= (unsigned char)(1 + ((max
>> 11) & 0x03));
575 max
= max
& 0x7ff; /* bit 0~10 */
576 /* 3 transactions at most */
584 spin_lock_irqsave(&udc
->lock
, flags
);
585 /* Get the endpoint queue head address */
587 dqh
->max_packet_length
= (max
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
588 | (mult
<< EP_QUEUE_HEAD_MULT_POS
)
589 | (zlt
? EP_QUEUE_HEAD_ZLT_SEL
: 0)
590 | (ios
? EP_QUEUE_HEAD_IOS
: 0);
591 dqh
->next_dtd_ptr
= 1;
592 dqh
->size_ioc_int_sts
= 0;
594 ep
->ep
.maxpacket
= max
;
598 /* Enable the endpoint for Rx or Tx and set the endpoint type */
599 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
600 if (direction
== EP_DIR_IN
) {
601 epctrlx
&= ~EPCTRL_TX_ALL_MASK
;
602 epctrlx
|= EPCTRL_TX_ENABLE
| EPCTRL_TX_DATA_TOGGLE_RST
603 | ((desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
604 << EPCTRL_TX_EP_TYPE_SHIFT
);
606 epctrlx
&= ~EPCTRL_RX_ALL_MASK
;
607 epctrlx
|= EPCTRL_RX_ENABLE
| EPCTRL_RX_DATA_TOGGLE_RST
608 | ((desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
609 << EPCTRL_RX_EP_TYPE_SHIFT
);
611 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
614 * Implement Guideline (GL# USB-7) The unused endpoint type must
615 * be programmed to bulk.
617 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
618 if ((epctrlx
& EPCTRL_RX_ENABLE
) == 0) {
619 epctrlx
|= (USB_ENDPOINT_XFER_BULK
620 << EPCTRL_RX_EP_TYPE_SHIFT
);
621 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
624 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
625 if ((epctrlx
& EPCTRL_TX_ENABLE
) == 0) {
626 epctrlx
|= (USB_ENDPOINT_XFER_BULK
627 << EPCTRL_TX_EP_TYPE_SHIFT
);
628 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
631 spin_unlock_irqrestore(&udc
->lock
, flags
);
638 static int mv_ep_disable(struct usb_ep
*_ep
)
643 u32 bit_pos
, epctrlx
, direction
;
646 ep
= container_of(_ep
, struct mv_ep
, ep
);
647 if ((_ep
== NULL
) || !ep
->desc
)
652 /* Get the endpoint queue head address */
655 spin_lock_irqsave(&udc
->lock
, flags
);
657 direction
= ep_dir(ep
);
658 bit_pos
= 1 << ((direction
== EP_DIR_OUT
? 0 : 16) + ep
->ep_num
);
660 /* Reset the max packet length and the interrupt on Setup */
661 dqh
->max_packet_length
= 0;
663 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
664 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
665 epctrlx
&= ~((direction
== EP_DIR_IN
)
666 ? (EPCTRL_TX_ENABLE
| EPCTRL_TX_TYPE
)
667 : (EPCTRL_RX_ENABLE
| EPCTRL_RX_TYPE
));
668 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
670 /* nuke all pending requests (does flush) */
671 nuke(ep
, -ESHUTDOWN
);
676 spin_unlock_irqrestore(&udc
->lock
, flags
);
681 static struct usb_request
*
682 mv_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
684 struct mv_req
*req
= NULL
;
686 req
= kzalloc(sizeof *req
, gfp_flags
);
690 req
->req
.dma
= DMA_ADDR_INVALID
;
691 INIT_LIST_HEAD(&req
->queue
);
696 static void mv_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
698 struct mv_req
*req
= NULL
;
700 req
= container_of(_req
, struct mv_req
, req
);
706 static void mv_ep_fifo_flush(struct usb_ep
*_ep
)
709 u32 bit_pos
, direction
;
716 ep
= container_of(_ep
, struct mv_ep
, ep
);
721 direction
= ep_dir(ep
);
724 bit_pos
= (1 << 16) | 1;
725 else if (direction
== EP_DIR_OUT
)
726 bit_pos
= 1 << ep
->ep_num
;
728 bit_pos
= 1 << (16 + ep
->ep_num
);
730 loops
= LOOPS(EPSTATUS_TIMEOUT
);
732 unsigned int inter_loops
;
735 dev_err(&udc
->dev
->dev
,
736 "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
737 (unsigned)readl(&udc
->op_regs
->epstatus
),
741 /* Write 1 to the Flush register */
742 writel(bit_pos
, &udc
->op_regs
->epflush
);
744 /* Wait until flushing completed */
745 inter_loops
= LOOPS(FLUSH_TIMEOUT
);
746 while (readl(&udc
->op_regs
->epflush
)) {
748 * ENDPTFLUSH bit should be cleared to indicate this
749 * operation is complete
751 if (inter_loops
== 0) {
752 dev_err(&udc
->dev
->dev
,
753 "TIMEOUT for ENDPTFLUSH=0x%x,"
755 (unsigned)readl(&udc
->op_regs
->epflush
),
763 } while (readl(&udc
->op_regs
->epstatus
) & bit_pos
);
766 /* queues (submits) an I/O request to an endpoint */
768 mv_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
770 struct mv_ep
*ep
= container_of(_ep
, struct mv_ep
, ep
);
771 struct mv_req
*req
= container_of(_req
, struct mv_req
, req
);
772 struct mv_udc
*udc
= ep
->udc
;
775 /* catch various bogus parameters */
776 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
777 || !list_empty(&req
->queue
)) {
778 dev_err(&udc
->dev
->dev
, "%s, bad params", __func__
);
781 if (unlikely(!_ep
|| !ep
->desc
)) {
782 dev_err(&udc
->dev
->dev
, "%s, bad ep", __func__
);
785 if (ep
->desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
786 if (req
->req
.length
> ep
->ep
.maxpacket
)
791 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
796 /* map virtual address to hardware */
797 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
798 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
800 req
->req
.length
, ep_dir(ep
)
805 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
806 req
->req
.dma
, req
->req
.length
,
813 req
->req
.status
= -EINPROGRESS
;
817 spin_lock_irqsave(&udc
->lock
, flags
);
819 /* build dtds and push them to device queue */
820 if (!req_to_dtd(req
)) {
822 retval
= queue_dtd(ep
, req
);
824 spin_unlock_irqrestore(&udc
->lock
, flags
);
828 spin_unlock_irqrestore(&udc
->lock
, flags
);
832 /* Update ep0 state */
834 udc
->ep0_state
= DATA_STATE_XMIT
;
836 /* irq handler advances the queue */
838 list_add_tail(&req
->queue
, &ep
->queue
);
839 spin_unlock_irqrestore(&udc
->lock
, flags
);
844 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
845 static int mv_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
847 struct mv_ep
*ep
= container_of(_ep
, struct mv_ep
, ep
);
849 struct mv_udc
*udc
= ep
->udc
;
851 int stopped
, ret
= 0;
857 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
858 stopped
= ep
->stopped
;
860 /* Stop the ep before we deal with the queue */
862 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
863 if (ep_dir(ep
) == EP_DIR_IN
)
864 epctrlx
&= ~EPCTRL_TX_ENABLE
;
866 epctrlx
&= ~EPCTRL_RX_ENABLE
;
867 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
869 /* make sure it's actually queued on this endpoint */
870 list_for_each_entry(req
, &ep
->queue
, queue
) {
871 if (&req
->req
== _req
)
874 if (&req
->req
!= _req
) {
879 /* The request is in progress, or completed but not dequeued */
880 if (ep
->queue
.next
== &req
->queue
) {
881 _req
->status
= -ECONNRESET
;
882 mv_ep_fifo_flush(_ep
); /* flush current transfer */
884 /* The request isn't the last request in this ep queue */
885 if (req
->queue
.next
!= &ep
->queue
) {
887 struct mv_req
*next_req
;
890 next_req
= list_entry(req
->queue
.next
, struct mv_req
,
893 /* Point the QH to the first TD of next request */
894 writel((u32
) next_req
->head
, &qh
->curr_dtd_ptr
);
899 qh
->next_dtd_ptr
= 1;
900 qh
->size_ioc_int_sts
= 0;
903 /* The request hasn't been processed, patch up the TD chain */
905 struct mv_req
*prev_req
;
907 prev_req
= list_entry(req
->queue
.prev
, struct mv_req
, queue
);
908 writel(readl(&req
->tail
->dtd_next
),
909 &prev_req
->tail
->dtd_next
);
913 done(ep
, req
, -ECONNRESET
);
917 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
918 if (ep_dir(ep
) == EP_DIR_IN
)
919 epctrlx
|= EPCTRL_TX_ENABLE
;
921 epctrlx
|= EPCTRL_RX_ENABLE
;
922 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
923 ep
->stopped
= stopped
;
925 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
929 static void ep_set_stall(struct mv_udc
*udc
, u8 ep_num
, u8 direction
, int stall
)
933 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep_num
]);
936 if (direction
== EP_DIR_IN
)
937 epctrlx
|= EPCTRL_TX_EP_STALL
;
939 epctrlx
|= EPCTRL_RX_EP_STALL
;
941 if (direction
== EP_DIR_IN
) {
942 epctrlx
&= ~EPCTRL_TX_EP_STALL
;
943 epctrlx
|= EPCTRL_TX_DATA_TOGGLE_RST
;
945 epctrlx
&= ~EPCTRL_RX_EP_STALL
;
946 epctrlx
|= EPCTRL_RX_DATA_TOGGLE_RST
;
949 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep_num
]);
952 static int ep_is_stall(struct mv_udc
*udc
, u8 ep_num
, u8 direction
)
956 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep_num
]);
958 if (direction
== EP_DIR_OUT
)
959 return (epctrlx
& EPCTRL_RX_EP_STALL
) ? 1 : 0;
961 return (epctrlx
& EPCTRL_TX_EP_STALL
) ? 1 : 0;
964 static int mv_ep_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
967 unsigned long flags
= 0;
971 ep
= container_of(_ep
, struct mv_ep
, ep
);
973 if (!_ep
|| !ep
->desc
) {
978 if (ep
->desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
979 status
= -EOPNOTSUPP
;
984 * Attempt to halt IN ep will fail if any transfer requests
987 if (halt
&& (ep_dir(ep
) == EP_DIR_IN
) && !list_empty(&ep
->queue
)) {
992 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
993 ep_set_stall(udc
, ep
->ep_num
, ep_dir(ep
), halt
);
998 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1000 if (ep
->ep_num
== 0) {
1001 udc
->ep0_state
= WAIT_FOR_SETUP
;
1002 udc
->ep0_dir
= EP_DIR_OUT
;
1008 static int mv_ep_set_halt(struct usb_ep
*_ep
, int halt
)
1010 return mv_ep_set_halt_wedge(_ep
, halt
, 0);
1013 static int mv_ep_set_wedge(struct usb_ep
*_ep
)
1015 return mv_ep_set_halt_wedge(_ep
, 1, 1);
1018 static struct usb_ep_ops mv_ep_ops
= {
1019 .enable
= mv_ep_enable
,
1020 .disable
= mv_ep_disable
,
1022 .alloc_request
= mv_alloc_request
,
1023 .free_request
= mv_free_request
,
1025 .queue
= mv_ep_queue
,
1026 .dequeue
= mv_ep_dequeue
,
1028 .set_wedge
= mv_ep_set_wedge
,
1029 .set_halt
= mv_ep_set_halt
,
1030 .fifo_flush
= mv_ep_fifo_flush
, /* flush fifo */
1033 static void udc_clock_enable(struct mv_udc
*udc
)
1037 for (i
= 0; i
< udc
->clknum
; i
++)
1038 clk_enable(udc
->clk
[i
]);
1041 static void udc_clock_disable(struct mv_udc
*udc
)
1045 for (i
= 0; i
< udc
->clknum
; i
++)
1046 clk_disable(udc
->clk
[i
]);
1049 static void udc_stop(struct mv_udc
*udc
)
1053 /* Disable interrupts */
1054 tmp
= readl(&udc
->op_regs
->usbintr
);
1055 tmp
&= ~(USBINTR_INT_EN
| USBINTR_ERR_INT_EN
|
1056 USBINTR_PORT_CHANGE_DETECT_EN
| USBINTR_RESET_EN
);
1057 writel(tmp
, &udc
->op_regs
->usbintr
);
1059 /* Reset the Run the bit in the command register to stop VUSB */
1060 tmp
= readl(&udc
->op_regs
->usbcmd
);
1061 tmp
&= ~USBCMD_RUN_STOP
;
1062 writel(tmp
, &udc
->op_regs
->usbcmd
);
1065 static void udc_start(struct mv_udc
*udc
)
1069 usbintr
= USBINTR_INT_EN
| USBINTR_ERR_INT_EN
1070 | USBINTR_PORT_CHANGE_DETECT_EN
1071 | USBINTR_RESET_EN
| USBINTR_DEVICE_SUSPEND
;
1072 /* Enable interrupts */
1073 writel(usbintr
, &udc
->op_regs
->usbintr
);
1075 /* Set the Run bit in the command register */
1076 writel(USBCMD_RUN_STOP
, &udc
->op_regs
->usbcmd
);
1079 static int udc_reset(struct mv_udc
*udc
)
1084 /* Stop the controller */
1085 tmp
= readl(&udc
->op_regs
->usbcmd
);
1086 tmp
&= ~USBCMD_RUN_STOP
;
1087 writel(tmp
, &udc
->op_regs
->usbcmd
);
1089 /* Reset the controller to get default values */
1090 writel(USBCMD_CTRL_RESET
, &udc
->op_regs
->usbcmd
);
1092 /* wait for reset to complete */
1093 loops
= LOOPS(RESET_TIMEOUT
);
1094 while (readl(&udc
->op_regs
->usbcmd
) & USBCMD_CTRL_RESET
) {
1096 dev_err(&udc
->dev
->dev
,
1097 "Wait for RESET completed TIMEOUT\n");
1104 /* set controller to device mode */
1105 tmp
= readl(&udc
->op_regs
->usbmode
);
1106 tmp
|= USBMODE_CTRL_MODE_DEVICE
;
1108 /* turn setup lockout off, require setup tripwire in usbcmd */
1109 tmp
|= USBMODE_SETUP_LOCK_OFF
| USBMODE_STREAM_DISABLE
;
1111 writel(tmp
, &udc
->op_regs
->usbmode
);
1113 writel(0x0, &udc
->op_regs
->epsetupstat
);
1115 /* Configure the Endpoint List Address */
1116 writel(udc
->ep_dqh_dma
& USB_EP_LIST_ADDRESS_MASK
,
1117 &udc
->op_regs
->eplistaddr
);
1119 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1120 if (readl(&udc
->cap_regs
->hcsparams
) & HCSPARAMS_PPC
)
1121 portsc
&= (~PORTSCX_W1C_BITS
| ~PORTSCX_PORT_POWER
);
1124 portsc
|= PORTSCX_FORCE_FULL_SPEED_CONNECT
;
1126 portsc
&= (~PORTSCX_FORCE_FULL_SPEED_CONNECT
);
1128 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1130 tmp
= readl(&udc
->op_regs
->epctrlx
[0]);
1131 tmp
&= ~(EPCTRL_TX_EP_STALL
| EPCTRL_RX_EP_STALL
);
1132 writel(tmp
, &udc
->op_regs
->epctrlx
[0]);
1137 static int mv_udc_enable(struct mv_udc
*udc
)
1141 if (udc
->clock_gating
== 0 || udc
->active
)
1144 dev_dbg(&udc
->dev
->dev
, "enable udc\n");
1145 udc_clock_enable(udc
);
1146 if (udc
->pdata
->phy_init
) {
1147 retval
= udc
->pdata
->phy_init(udc
->phy_regs
);
1149 dev_err(&udc
->dev
->dev
,
1150 "init phy error %d\n", retval
);
1151 udc_clock_disable(udc
);
1160 static void mv_udc_disable(struct mv_udc
*udc
)
1162 if (udc
->clock_gating
&& udc
->active
) {
1163 dev_dbg(&udc
->dev
->dev
, "disable udc\n");
1164 if (udc
->pdata
->phy_deinit
)
1165 udc
->pdata
->phy_deinit(udc
->phy_regs
);
1166 udc_clock_disable(udc
);
1171 static int mv_udc_get_frame(struct usb_gadget
*gadget
)
1179 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1181 retval
= readl(udc
->op_regs
->frindex
) & USB_FRINDEX_MASKS
;
1186 /* Tries to wake up the host connected to this gadget */
1187 static int mv_udc_wakeup(struct usb_gadget
*gadget
)
1189 struct mv_udc
*udc
= container_of(gadget
, struct mv_udc
, gadget
);
1192 /* Remote wakeup feature not enabled by host */
1193 if (!udc
->remote_wakeup
)
1196 portsc
= readl(&udc
->op_regs
->portsc
);
1197 /* not suspended? */
1198 if (!(portsc
& PORTSCX_PORT_SUSPEND
))
1200 /* trigger force resume */
1201 portsc
|= PORTSCX_PORT_FORCE_RESUME
;
1202 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1206 static int mv_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1209 unsigned long flags
;
1212 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1213 spin_lock_irqsave(&udc
->lock
, flags
);
1215 dev_dbg(&udc
->dev
->dev
, "%s: softconnect %d, vbus_active %d\n",
1216 __func__
, udc
->softconnect
, udc
->vbus_active
);
1218 udc
->vbus_active
= (is_active
!= 0);
1219 if (udc
->driver
&& udc
->softconnect
&& udc
->vbus_active
) {
1220 retval
= mv_udc_enable(udc
);
1222 /* Clock is disabled, need re-init registers */
1227 } else if (udc
->driver
&& udc
->softconnect
) {
1228 /* stop all the transfer in queue*/
1229 stop_activity(udc
, udc
->driver
);
1231 mv_udc_disable(udc
);
1234 spin_unlock_irqrestore(&udc
->lock
, flags
);
1238 static int mv_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1241 unsigned long flags
;
1244 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1245 spin_lock_irqsave(&udc
->lock
, flags
);
1247 dev_dbg(&udc
->dev
->dev
, "%s: softconnect %d, vbus_active %d\n",
1248 __func__
, udc
->softconnect
, udc
->vbus_active
);
1250 udc
->softconnect
= (is_on
!= 0);
1251 if (udc
->driver
&& udc
->softconnect
&& udc
->vbus_active
) {
1252 retval
= mv_udc_enable(udc
);
1254 /* Clock is disabled, need re-init registers */
1259 } else if (udc
->driver
&& udc
->vbus_active
) {
1260 /* stop all the transfer in queue*/
1261 stop_activity(udc
, udc
->driver
);
1263 mv_udc_disable(udc
);
1266 spin_unlock_irqrestore(&udc
->lock
, flags
);
1270 static int mv_udc_start(struct usb_gadget_driver
*driver
,
1271 int (*bind
)(struct usb_gadget
*));
1272 static int mv_udc_stop(struct usb_gadget_driver
*driver
);
1273 /* device controller usb_gadget_ops structure */
1274 static const struct usb_gadget_ops mv_ops
= {
1276 /* returns the current frame number */
1277 .get_frame
= mv_udc_get_frame
,
1279 /* tries to wake up the host connected to this gadget */
1280 .wakeup
= mv_udc_wakeup
,
1282 /* notify controller that VBUS is powered or not */
1283 .vbus_session
= mv_udc_vbus_session
,
1285 /* D+ pullup, software-controlled connect/disconnect to USB host */
1286 .pullup
= mv_udc_pullup
,
1287 .start
= mv_udc_start
,
1288 .stop
= mv_udc_stop
,
1291 static int eps_init(struct mv_udc
*udc
)
1297 /* initialize ep0 */
1300 strncpy(ep
->name
, "ep0", sizeof(ep
->name
));
1301 ep
->ep
.name
= ep
->name
;
1302 ep
->ep
.ops
= &mv_ep_ops
;
1305 ep
->ep
.maxpacket
= EP0_MAX_PKT_SIZE
;
1307 ep
->desc
= &mv_ep0_desc
;
1308 INIT_LIST_HEAD(&ep
->queue
);
1310 ep
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
1312 /* initialize other endpoints */
1313 for (i
= 2; i
< udc
->max_eps
* 2; i
++) {
1316 snprintf(name
, sizeof(name
), "ep%din", i
/ 2);
1317 ep
->direction
= EP_DIR_IN
;
1319 snprintf(name
, sizeof(name
), "ep%dout", i
/ 2);
1320 ep
->direction
= EP_DIR_OUT
;
1323 strncpy(ep
->name
, name
, sizeof(ep
->name
));
1324 ep
->ep
.name
= ep
->name
;
1326 ep
->ep
.ops
= &mv_ep_ops
;
1328 ep
->ep
.maxpacket
= (unsigned short) ~0;
1331 INIT_LIST_HEAD(&ep
->queue
);
1332 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1334 ep
->dqh
= &udc
->ep_dqh
[i
];
1340 /* delete all endpoint requests, called with spinlock held */
1341 static void nuke(struct mv_ep
*ep
, int status
)
1343 /* called with spinlock held */
1346 /* endpoint fifo flush */
1347 mv_ep_fifo_flush(&ep
->ep
);
1349 while (!list_empty(&ep
->queue
)) {
1350 struct mv_req
*req
= NULL
;
1351 req
= list_entry(ep
->queue
.next
, struct mv_req
, queue
);
1352 done(ep
, req
, status
);
1356 /* stop all USB activities */
1357 static void stop_activity(struct mv_udc
*udc
, struct usb_gadget_driver
*driver
)
1361 nuke(&udc
->eps
[0], -ESHUTDOWN
);
1363 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1364 nuke(ep
, -ESHUTDOWN
);
1367 /* report disconnect; the driver is already quiesced */
1369 spin_unlock(&udc
->lock
);
1370 driver
->disconnect(&udc
->gadget
);
1371 spin_lock(&udc
->lock
);
1375 static int mv_udc_start(struct usb_gadget_driver
*driver
,
1376 int (*bind
)(struct usb_gadget
*))
1378 struct mv_udc
*udc
= the_controller
;
1380 unsigned long flags
;
1388 spin_lock_irqsave(&udc
->lock
, flags
);
1390 /* hook up the driver ... */
1391 driver
->driver
.bus
= NULL
;
1392 udc
->driver
= driver
;
1393 udc
->gadget
.dev
.driver
= &driver
->driver
;
1395 udc
->usb_state
= USB_STATE_ATTACHED
;
1396 udc
->ep0_state
= WAIT_FOR_SETUP
;
1397 udc
->ep0_dir
= EP_DIR_OUT
;
1399 spin_unlock_irqrestore(&udc
->lock
, flags
);
1401 retval
= bind(&udc
->gadget
);
1403 dev_err(&udc
->dev
->dev
, "bind to driver %s --> %d\n",
1404 driver
->driver
.name
, retval
);
1406 udc
->gadget
.dev
.driver
= NULL
;
1410 /* pullup is always on */
1411 mv_udc_pullup(&udc
->gadget
, 1);
1413 /* When boot with cable attached, there will be no vbus irq occurred */
1415 queue_work(udc
->qwork
, &udc
->vbus_work
);
1420 static int mv_udc_stop(struct usb_gadget_driver
*driver
)
1422 struct mv_udc
*udc
= the_controller
;
1423 unsigned long flags
;
1428 spin_lock_irqsave(&udc
->lock
, flags
);
1433 /* stop all usb activities */
1434 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1435 stop_activity(udc
, driver
);
1436 mv_udc_disable(udc
);
1438 spin_unlock_irqrestore(&udc
->lock
, flags
);
1440 /* unbind gadget driver */
1441 driver
->unbind(&udc
->gadget
);
1442 udc
->gadget
.dev
.driver
= NULL
;
1448 static void mv_set_ptc(struct mv_udc
*udc
, u32 mode
)
1452 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1453 portsc
|= mode
<< 16;
1454 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1457 static void prime_status_complete(struct usb_ep
*ep
, struct usb_request
*_req
)
1459 struct mv_udc
*udc
= the_controller
;
1460 struct mv_req
*req
= container_of(_req
, struct mv_req
, req
);
1461 unsigned long flags
;
1463 dev_info(&udc
->dev
->dev
, "switch to test mode %d\n", req
->test_mode
);
1465 spin_lock_irqsave(&udc
->lock
, flags
);
1466 if (req
->test_mode
) {
1467 mv_set_ptc(udc
, req
->test_mode
);
1470 spin_unlock_irqrestore(&udc
->lock
, flags
);
1474 udc_prime_status(struct mv_udc
*udc
, u8 direction
, u16 status
, bool empty
)
1481 udc
->ep0_dir
= direction
;
1482 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1484 req
= udc
->status_req
;
1486 /* fill in the reqest structure */
1487 if (empty
== false) {
1488 *((u16
*) req
->req
.buf
) = cpu_to_le16(status
);
1489 req
->req
.length
= 2;
1491 req
->req
.length
= 0;
1494 req
->req
.status
= -EINPROGRESS
;
1495 req
->req
.actual
= 0;
1496 if (udc
->test_mode
) {
1497 req
->req
.complete
= prime_status_complete
;
1498 req
->test_mode
= udc
->test_mode
;
1501 req
->req
.complete
= NULL
;
1504 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1505 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1506 req
->req
.buf
, req
->req
.length
,
1507 ep_dir(ep
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1511 /* prime the data phase */
1512 if (!req_to_dtd(req
))
1513 retval
= queue_dtd(ep
, req
);
1520 dev_err(&udc
->dev
->dev
, "response error on GET_STATUS request\n");
1524 list_add_tail(&req
->queue
, &ep
->queue
);
1531 static void mv_udc_testmode(struct mv_udc
*udc
, u16 index
)
1533 if (index
<= TEST_FORCE_EN
) {
1534 udc
->test_mode
= index
;
1535 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1538 dev_err(&udc
->dev
->dev
,
1539 "This test mode(%d) is not supported\n", index
);
1542 static void ch9setaddress(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1544 udc
->dev_addr
= (u8
)setup
->wValue
;
1546 /* update usb state */
1547 udc
->usb_state
= USB_STATE_ADDRESS
;
1549 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1553 static void ch9getstatus(struct mv_udc
*udc
, u8 ep_num
,
1554 struct usb_ctrlrequest
*setup
)
1559 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
1560 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
1563 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1564 status
= 1 << USB_DEVICE_SELF_POWERED
;
1565 status
|= udc
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
1566 } else if ((setup
->bRequestType
& USB_RECIP_MASK
)
1567 == USB_RECIP_INTERFACE
) {
1568 /* get interface status */
1570 } else if ((setup
->bRequestType
& USB_RECIP_MASK
)
1571 == USB_RECIP_ENDPOINT
) {
1572 u8 ep_num
, direction
;
1574 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1575 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1576 ? EP_DIR_IN
: EP_DIR_OUT
;
1577 status
= ep_is_stall(udc
, ep_num
, direction
)
1578 << USB_ENDPOINT_HALT
;
1581 retval
= udc_prime_status(udc
, EP_DIR_IN
, status
, false);
1585 udc
->ep0_state
= DATA_STATE_XMIT
;
1588 static void ch9clearfeature(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1594 if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1595 == ((USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))) {
1596 switch (setup
->wValue
) {
1597 case USB_DEVICE_REMOTE_WAKEUP
:
1598 udc
->remote_wakeup
= 0;
1603 } else if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1604 == ((USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
))) {
1605 switch (setup
->wValue
) {
1606 case USB_ENDPOINT_HALT
:
1607 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1608 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1609 ? EP_DIR_IN
: EP_DIR_OUT
;
1610 if (setup
->wValue
!= 0 || setup
->wLength
!= 0
1611 || ep_num
> udc
->max_eps
)
1613 ep
= &udc
->eps
[ep_num
* 2 + direction
];
1616 spin_unlock(&udc
->lock
);
1617 ep_set_stall(udc
, ep_num
, direction
, 0);
1618 spin_lock(&udc
->lock
);
1626 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1632 static void ch9setfeature(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1637 if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1638 == ((USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))) {
1639 switch (setup
->wValue
) {
1640 case USB_DEVICE_REMOTE_WAKEUP
:
1641 udc
->remote_wakeup
= 1;
1643 case USB_DEVICE_TEST_MODE
:
1644 if (setup
->wIndex
& 0xFF
1645 || udc
->gadget
.speed
!= USB_SPEED_HIGH
)
1648 if (udc
->usb_state
!= USB_STATE_CONFIGURED
1649 && udc
->usb_state
!= USB_STATE_ADDRESS
1650 && udc
->usb_state
!= USB_STATE_DEFAULT
)
1653 mv_udc_testmode(udc
, (setup
->wIndex
>> 8));
1658 } else if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1659 == ((USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
))) {
1660 switch (setup
->wValue
) {
1661 case USB_ENDPOINT_HALT
:
1662 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1663 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1664 ? EP_DIR_IN
: EP_DIR_OUT
;
1665 if (setup
->wValue
!= 0 || setup
->wLength
!= 0
1666 || ep_num
> udc
->max_eps
)
1668 spin_unlock(&udc
->lock
);
1669 ep_set_stall(udc
, ep_num
, direction
, 1);
1670 spin_lock(&udc
->lock
);
1678 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1684 static void handle_setup_packet(struct mv_udc
*udc
, u8 ep_num
,
1685 struct usb_ctrlrequest
*setup
)
1687 bool delegate
= false;
1689 nuke(&udc
->eps
[ep_num
* 2 + EP_DIR_OUT
], -ESHUTDOWN
);
1691 dev_dbg(&udc
->dev
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1692 setup
->bRequestType
, setup
->bRequest
,
1693 setup
->wValue
, setup
->wIndex
, setup
->wLength
);
1694 /* We process some stardard setup requests here */
1695 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1696 switch (setup
->bRequest
) {
1697 case USB_REQ_GET_STATUS
:
1698 ch9getstatus(udc
, ep_num
, setup
);
1701 case USB_REQ_SET_ADDRESS
:
1702 ch9setaddress(udc
, setup
);
1705 case USB_REQ_CLEAR_FEATURE
:
1706 ch9clearfeature(udc
, setup
);
1709 case USB_REQ_SET_FEATURE
:
1710 ch9setfeature(udc
, setup
);
1719 /* delegate USB standard requests to the gadget driver */
1720 if (delegate
== true) {
1721 /* USB requests handled by gadget */
1722 if (setup
->wLength
) {
1723 /* DATA phase from gadget, STATUS phase from udc */
1724 udc
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1725 ? EP_DIR_IN
: EP_DIR_OUT
;
1726 spin_unlock(&udc
->lock
);
1727 if (udc
->driver
->setup(&udc
->gadget
,
1728 &udc
->local_setup_buff
) < 0)
1730 spin_lock(&udc
->lock
);
1731 udc
->ep0_state
= (setup
->bRequestType
& USB_DIR_IN
)
1732 ? DATA_STATE_XMIT
: DATA_STATE_RECV
;
1734 /* no DATA phase, IN STATUS phase from gadget */
1735 udc
->ep0_dir
= EP_DIR_IN
;
1736 spin_unlock(&udc
->lock
);
1737 if (udc
->driver
->setup(&udc
->gadget
,
1738 &udc
->local_setup_buff
) < 0)
1740 spin_lock(&udc
->lock
);
1741 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1746 /* complete DATA or STATUS phase of ep0 prime status phase if needed */
1747 static void ep0_req_complete(struct mv_udc
*udc
,
1748 struct mv_ep
*ep0
, struct mv_req
*req
)
1752 if (udc
->usb_state
== USB_STATE_ADDRESS
) {
1753 /* set the new address */
1754 new_addr
= (u32
)udc
->dev_addr
;
1755 writel(new_addr
<< USB_DEVICE_ADDRESS_BIT_SHIFT
,
1756 &udc
->op_regs
->deviceaddr
);
1761 switch (udc
->ep0_state
) {
1762 case DATA_STATE_XMIT
:
1763 /* receive status phase */
1764 if (udc_prime_status(udc
, EP_DIR_OUT
, 0, true))
1767 case DATA_STATE_RECV
:
1768 /* send status phase */
1769 if (udc_prime_status(udc
, EP_DIR_IN
, 0 , true))
1772 case WAIT_FOR_OUT_STATUS
:
1773 udc
->ep0_state
= WAIT_FOR_SETUP
;
1775 case WAIT_FOR_SETUP
:
1776 dev_err(&udc
->dev
->dev
, "unexpect ep0 packets\n");
1784 static void get_setup_data(struct mv_udc
*udc
, u8 ep_num
, u8
*buffer_ptr
)
1789 dqh
= &udc
->ep_dqh
[ep_num
* 2 + EP_DIR_OUT
];
1791 /* Clear bit in ENDPTSETUPSTAT */
1792 writel((1 << ep_num
), &udc
->op_regs
->epsetupstat
);
1794 /* while a hazard exists when setup package arrives */
1796 /* Set Setup Tripwire */
1797 temp
= readl(&udc
->op_regs
->usbcmd
);
1798 writel(temp
| USBCMD_SETUP_TRIPWIRE_SET
, &udc
->op_regs
->usbcmd
);
1800 /* Copy the setup packet to local buffer */
1801 memcpy(buffer_ptr
, (u8
*) dqh
->setup_buffer
, 8);
1802 } while (!(readl(&udc
->op_regs
->usbcmd
) & USBCMD_SETUP_TRIPWIRE_SET
));
1804 /* Clear Setup Tripwire */
1805 temp
= readl(&udc
->op_regs
->usbcmd
);
1806 writel(temp
& ~USBCMD_SETUP_TRIPWIRE_SET
, &udc
->op_regs
->usbcmd
);
1809 static void irq_process_tr_complete(struct mv_udc
*udc
)
1812 int i
, ep_num
= 0, direction
= 0;
1813 struct mv_ep
*curr_ep
;
1814 struct mv_req
*curr_req
, *temp_req
;
1818 * We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
1819 * because the setup packets are to be read ASAP
1822 /* Process all Setup packet received interrupts */
1823 tmp
= readl(&udc
->op_regs
->epsetupstat
);
1826 for (i
= 0; i
< udc
->max_eps
; i
++) {
1827 if (tmp
& (1 << i
)) {
1828 get_setup_data(udc
, i
,
1829 (u8
*)(&udc
->local_setup_buff
));
1830 handle_setup_packet(udc
, i
,
1831 &udc
->local_setup_buff
);
1836 /* Don't clear the endpoint setup status register here.
1837 * It is cleared as a setup packet is read out of the buffer
1840 /* Process non-setup transaction complete interrupts */
1841 tmp
= readl(&udc
->op_regs
->epcomplete
);
1846 writel(tmp
, &udc
->op_regs
->epcomplete
);
1848 for (i
= 0; i
< udc
->max_eps
* 2; i
++) {
1852 bit_pos
= 1 << (ep_num
+ 16 * direction
);
1854 if (!(bit_pos
& tmp
))
1858 curr_ep
= &udc
->eps
[0];
1860 curr_ep
= &udc
->eps
[i
];
1861 /* process the req queue until an uncomplete request */
1862 list_for_each_entry_safe(curr_req
, temp_req
,
1863 &curr_ep
->queue
, queue
) {
1864 status
= process_ep_req(udc
, i
, curr_req
);
1868 /* write back status to req */
1869 curr_req
->req
.status
= status
;
1871 /* ep0 request completion */
1873 ep0_req_complete(udc
, curr_ep
, curr_req
);
1876 done(curr_ep
, curr_req
, status
);
1882 void irq_process_reset(struct mv_udc
*udc
)
1887 udc
->ep0_dir
= EP_DIR_OUT
;
1888 udc
->ep0_state
= WAIT_FOR_SETUP
;
1889 udc
->remote_wakeup
= 0; /* default to 0 on reset */
1891 /* The address bits are past bit 25-31. Set the address */
1892 tmp
= readl(&udc
->op_regs
->deviceaddr
);
1893 tmp
&= ~(USB_DEVICE_ADDRESS_MASK
);
1894 writel(tmp
, &udc
->op_regs
->deviceaddr
);
1896 /* Clear all the setup token semaphores */
1897 tmp
= readl(&udc
->op_regs
->epsetupstat
);
1898 writel(tmp
, &udc
->op_regs
->epsetupstat
);
1900 /* Clear all the endpoint complete status bits */
1901 tmp
= readl(&udc
->op_regs
->epcomplete
);
1902 writel(tmp
, &udc
->op_regs
->epcomplete
);
1904 /* wait until all endptprime bits cleared */
1905 loops
= LOOPS(PRIME_TIMEOUT
);
1906 while (readl(&udc
->op_regs
->epprime
) & 0xFFFFFFFF) {
1908 dev_err(&udc
->dev
->dev
,
1909 "Timeout for ENDPTPRIME = 0x%x\n",
1910 readl(&udc
->op_regs
->epprime
));
1917 /* Write 1s to the Flush register */
1918 writel((u32
)~0, &udc
->op_regs
->epflush
);
1920 if (readl(&udc
->op_regs
->portsc
[0]) & PORTSCX_PORT_RESET
) {
1921 dev_info(&udc
->dev
->dev
, "usb bus reset\n");
1922 udc
->usb_state
= USB_STATE_DEFAULT
;
1923 /* reset all the queues, stop all USB activities */
1924 stop_activity(udc
, udc
->driver
);
1926 dev_info(&udc
->dev
->dev
, "USB reset portsc 0x%x\n",
1927 readl(&udc
->op_regs
->portsc
));
1935 /* reset all the queues, stop all USB activities */
1936 stop_activity(udc
, udc
->driver
);
1938 /* reset ep0 dQH and endptctrl */
1941 /* enable interrupt and set controller to run state */
1944 udc
->usb_state
= USB_STATE_ATTACHED
;
1948 static void handle_bus_resume(struct mv_udc
*udc
)
1950 udc
->usb_state
= udc
->resume_state
;
1951 udc
->resume_state
= 0;
1953 /* report resume to the driver */
1955 if (udc
->driver
->resume
) {
1956 spin_unlock(&udc
->lock
);
1957 udc
->driver
->resume(&udc
->gadget
);
1958 spin_lock(&udc
->lock
);
1963 static void irq_process_suspend(struct mv_udc
*udc
)
1965 udc
->resume_state
= udc
->usb_state
;
1966 udc
->usb_state
= USB_STATE_SUSPENDED
;
1968 if (udc
->driver
->suspend
) {
1969 spin_unlock(&udc
->lock
);
1970 udc
->driver
->suspend(&udc
->gadget
);
1971 spin_lock(&udc
->lock
);
1975 static void irq_process_port_change(struct mv_udc
*udc
)
1979 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1980 if (!(portsc
& PORTSCX_PORT_RESET
)) {
1982 u32 speed
= portsc
& PORTSCX_PORT_SPEED_MASK
;
1984 case PORTSCX_PORT_SPEED_HIGH
:
1985 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1987 case PORTSCX_PORT_SPEED_FULL
:
1988 udc
->gadget
.speed
= USB_SPEED_FULL
;
1990 case PORTSCX_PORT_SPEED_LOW
:
1991 udc
->gadget
.speed
= USB_SPEED_LOW
;
1994 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1999 if (portsc
& PORTSCX_PORT_SUSPEND
) {
2000 udc
->resume_state
= udc
->usb_state
;
2001 udc
->usb_state
= USB_STATE_SUSPENDED
;
2002 if (udc
->driver
->suspend
) {
2003 spin_unlock(&udc
->lock
);
2004 udc
->driver
->suspend(&udc
->gadget
);
2005 spin_lock(&udc
->lock
);
2009 if (!(portsc
& PORTSCX_PORT_SUSPEND
)
2010 && udc
->usb_state
== USB_STATE_SUSPENDED
) {
2011 handle_bus_resume(udc
);
2014 if (!udc
->resume_state
)
2015 udc
->usb_state
= USB_STATE_DEFAULT
;
2018 static void irq_process_error(struct mv_udc
*udc
)
2020 /* Increment the error count */
2024 static irqreturn_t
mv_udc_irq(int irq
, void *dev
)
2026 struct mv_udc
*udc
= (struct mv_udc
*)dev
;
2029 spin_lock(&udc
->lock
);
2031 status
= readl(&udc
->op_regs
->usbsts
);
2032 intr
= readl(&udc
->op_regs
->usbintr
);
2036 spin_unlock(&udc
->lock
);
2040 /* Clear all the interrupts occurred */
2041 writel(status
, &udc
->op_regs
->usbsts
);
2043 if (status
& USBSTS_ERR
)
2044 irq_process_error(udc
);
2046 if (status
& USBSTS_RESET
)
2047 irq_process_reset(udc
);
2049 if (status
& USBSTS_PORT_CHANGE
)
2050 irq_process_port_change(udc
);
2052 if (status
& USBSTS_INT
)
2053 irq_process_tr_complete(udc
);
2055 if (status
& USBSTS_SUSPEND
)
2056 irq_process_suspend(udc
);
2058 spin_unlock(&udc
->lock
);
2063 static irqreturn_t
mv_udc_vbus_irq(int irq
, void *dev
)
2065 struct mv_udc
*udc
= (struct mv_udc
*)dev
;
2067 /* polling VBUS and init phy may cause too much time*/
2069 queue_work(udc
->qwork
, &udc
->vbus_work
);
2074 static void mv_udc_vbus_work(struct work_struct
*work
)
2079 udc
= container_of(work
, struct mv_udc
, vbus_work
);
2080 if (!udc
->pdata
->vbus
)
2083 vbus
= udc
->pdata
->vbus
->poll();
2084 dev_info(&udc
->dev
->dev
, "vbus is %d\n", vbus
);
2086 if (vbus
== VBUS_HIGH
)
2087 mv_udc_vbus_session(&udc
->gadget
, 1);
2088 else if (vbus
== VBUS_LOW
)
2089 mv_udc_vbus_session(&udc
->gadget
, 0);
2092 /* release device structure */
2093 static void gadget_release(struct device
*_dev
)
2095 struct mv_udc
*udc
= the_controller
;
2097 complete(udc
->done
);
2100 static int __devexit
mv_udc_remove(struct platform_device
*dev
)
2102 struct mv_udc
*udc
= the_controller
;
2105 usb_del_gadget_udc(&udc
->gadget
);
2108 flush_workqueue(udc
->qwork
);
2109 destroy_workqueue(udc
->qwork
);
2112 if (udc
->pdata
&& udc
->pdata
->vbus
&& udc
->clock_gating
)
2113 free_irq(udc
->pdata
->vbus
->irq
, &dev
->dev
);
2115 /* free memory allocated in probe */
2117 dma_pool_destroy(udc
->dtd_pool
);
2120 dma_free_coherent(&dev
->dev
, udc
->ep_dqh_size
,
2121 udc
->ep_dqh
, udc
->ep_dqh_dma
);
2126 free_irq(udc
->irq
, &dev
->dev
);
2128 mv_udc_disable(udc
);
2131 iounmap(udc
->cap_regs
);
2132 udc
->cap_regs
= NULL
;
2135 iounmap((void *)udc
->phy_regs
);
2138 if (udc
->status_req
) {
2139 kfree(udc
->status_req
->req
.buf
);
2140 kfree(udc
->status_req
);
2143 for (clk_i
= 0; clk_i
<= udc
->clknum
; clk_i
++)
2144 clk_put(udc
->clk
[clk_i
]);
2146 device_unregister(&udc
->gadget
.dev
);
2148 /* free dev, wait for the release() finished */
2149 wait_for_completion(udc
->done
);
2152 the_controller
= NULL
;
2157 static int __devinit
mv_udc_probe(struct platform_device
*dev
)
2159 struct mv_usb_platform_data
*pdata
= dev
->dev
.platform_data
;
2166 if (pdata
== NULL
) {
2167 dev_err(&dev
->dev
, "missing platform_data\n");
2171 size
= sizeof(*udc
) + sizeof(struct clk
*) * pdata
->clknum
;
2172 udc
= kzalloc(size
, GFP_KERNEL
);
2174 dev_err(&dev
->dev
, "failed to allocate memory for udc\n");
2178 the_controller
= udc
;
2179 udc
->done
= &release_done
;
2180 udc
->pdata
= dev
->dev
.platform_data
;
2181 spin_lock_init(&udc
->lock
);
2185 udc
->clknum
= pdata
->clknum
;
2186 for (clk_i
= 0; clk_i
< udc
->clknum
; clk_i
++) {
2187 udc
->clk
[clk_i
] = clk_get(&dev
->dev
, pdata
->clkname
[clk_i
]);
2188 if (IS_ERR(udc
->clk
[clk_i
])) {
2189 retval
= PTR_ERR(udc
->clk
[clk_i
]);
2194 r
= platform_get_resource_byname(udc
->dev
, IORESOURCE_MEM
, "capregs");
2196 dev_err(&dev
->dev
, "no I/O memory resource defined\n");
2201 udc
->cap_regs
= (struct mv_cap_regs __iomem
*)
2202 ioremap(r
->start
, resource_size(r
));
2203 if (udc
->cap_regs
== NULL
) {
2204 dev_err(&dev
->dev
, "failed to map I/O memory\n");
2209 r
= platform_get_resource_byname(udc
->dev
, IORESOURCE_MEM
, "phyregs");
2211 dev_err(&dev
->dev
, "no phy I/O memory resource defined\n");
2213 goto err_iounmap_capreg
;
2216 udc
->phy_regs
= (unsigned int)ioremap(r
->start
, resource_size(r
));
2217 if (udc
->phy_regs
== 0) {
2218 dev_err(&dev
->dev
, "failed to map phy I/O memory\n");
2220 goto err_iounmap_capreg
;
2223 /* we will acces controller register, so enable the clk */
2224 udc_clock_enable(udc
);
2225 if (pdata
->phy_init
) {
2226 retval
= pdata
->phy_init(udc
->phy_regs
);
2228 dev_err(&dev
->dev
, "phy init error %d\n", retval
);
2229 goto err_iounmap_phyreg
;
2233 udc
->op_regs
= (struct mv_op_regs __iomem
*)((u32
)udc
->cap_regs
2234 + (readl(&udc
->cap_regs
->caplength_hciversion
)
2236 udc
->max_eps
= readl(&udc
->cap_regs
->dccparams
) & DCCPARAMS_DEN_MASK
;
2239 * some platform will use usb to download image, it may not disconnect
2240 * usb gadget before loading kernel. So first stop udc here.
2243 writel(0xFFFFFFFF, &udc
->op_regs
->usbsts
);
2245 size
= udc
->max_eps
* sizeof(struct mv_dqh
) *2;
2246 size
= (size
+ DQH_ALIGNMENT
- 1) & ~(DQH_ALIGNMENT
- 1);
2247 udc
->ep_dqh
= dma_alloc_coherent(&dev
->dev
, size
,
2248 &udc
->ep_dqh_dma
, GFP_KERNEL
);
2250 if (udc
->ep_dqh
== NULL
) {
2251 dev_err(&dev
->dev
, "allocate dQH memory failed\n");
2253 goto err_disable_clock
;
2255 udc
->ep_dqh_size
= size
;
2257 /* create dTD dma_pool resource */
2258 udc
->dtd_pool
= dma_pool_create("mv_dtd",
2260 sizeof(struct mv_dtd
),
2264 if (!udc
->dtd_pool
) {
2269 size
= udc
->max_eps
* sizeof(struct mv_ep
) *2;
2270 udc
->eps
= kzalloc(size
, GFP_KERNEL
);
2271 if (udc
->eps
== NULL
) {
2272 dev_err(&dev
->dev
, "allocate ep memory failed\n");
2274 goto err_destroy_dma
;
2277 /* initialize ep0 status request structure */
2278 udc
->status_req
= kzalloc(sizeof(struct mv_req
), GFP_KERNEL
);
2279 if (!udc
->status_req
) {
2280 dev_err(&dev
->dev
, "allocate status_req memory failed\n");
2284 INIT_LIST_HEAD(&udc
->status_req
->queue
);
2286 /* allocate a small amount of memory to get valid address */
2287 udc
->status_req
->req
.buf
= kzalloc(8, GFP_KERNEL
);
2288 udc
->status_req
->req
.dma
= DMA_ADDR_INVALID
;
2290 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2291 udc
->usb_state
= USB_STATE_POWERED
;
2292 udc
->ep0_dir
= EP_DIR_OUT
;
2293 udc
->remote_wakeup
= 0;
2295 r
= platform_get_resource(udc
->dev
, IORESOURCE_IRQ
, 0);
2297 dev_err(&dev
->dev
, "no IRQ resource defined\n");
2299 goto err_free_status_req
;
2301 udc
->irq
= r
->start
;
2302 if (request_irq(udc
->irq
, mv_udc_irq
,
2303 IRQF_SHARED
, driver_name
, udc
)) {
2304 dev_err(&dev
->dev
, "Request irq %d for UDC failed\n",
2307 goto err_free_status_req
;
2310 /* initialize gadget structure */
2311 udc
->gadget
.ops
= &mv_ops
; /* usb_gadget_ops */
2312 udc
->gadget
.ep0
= &udc
->eps
[0].ep
; /* gadget ep0 */
2313 INIT_LIST_HEAD(&udc
->gadget
.ep_list
); /* ep_list */
2314 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
2315 udc
->gadget
.is_dualspeed
= 1; /* support dual speed */
2317 /* the "gadget" abstracts/virtualizes the controller */
2318 dev_set_name(&udc
->gadget
.dev
, "gadget");
2319 udc
->gadget
.dev
.parent
= &dev
->dev
;
2320 udc
->gadget
.dev
.dma_mask
= dev
->dev
.dma_mask
;
2321 udc
->gadget
.dev
.release
= gadget_release
;
2322 udc
->gadget
.name
= driver_name
; /* gadget name */
2324 retval
= device_register(&udc
->gadget
.dev
);
2330 /* VBUS detect: we can disable/enable clock on demand.*/
2332 udc
->clock_gating
= 1;
2333 retval
= request_threaded_irq(pdata
->vbus
->irq
, NULL
,
2334 mv_udc_vbus_irq
, IRQF_ONESHOT
, "vbus", udc
);
2337 "Can not request irq for VBUS, "
2338 "disable clock gating\n");
2339 udc
->clock_gating
= 0;
2342 udc
->qwork
= create_singlethread_workqueue("mv_udc_queue");
2344 dev_err(&dev
->dev
, "cannot create workqueue\n");
2346 goto err_unregister
;
2349 INIT_WORK(&udc
->vbus_work
, mv_udc_vbus_work
);
2353 * When clock gating is supported, we can disable clk and phy.
2354 * If not, it means that VBUS detection is not supported, we
2355 * have to enable vbus active all the time to let controller work.
2357 if (udc
->clock_gating
) {
2358 if (udc
->pdata
->phy_deinit
)
2359 udc
->pdata
->phy_deinit(udc
->phy_regs
);
2360 udc_clock_disable(udc
);
2362 udc
->vbus_active
= 1;
2364 retval
= usb_add_gadget_udc(&dev
->dev
, &udc
->gadget
);
2366 goto err_unregister
;
2368 dev_info(&dev
->dev
, "successful probe UDC device %s clock gating.\n",
2369 udc
->clock_gating
? "with" : "without");
2374 if (udc
->pdata
&& udc
->pdata
->vbus
&& udc
->clock_gating
)
2375 free_irq(pdata
->vbus
->irq
, &dev
->dev
);
2376 device_unregister(&udc
->gadget
.dev
);
2378 free_irq(udc
->irq
, &dev
->dev
);
2379 err_free_status_req
:
2380 kfree(udc
->status_req
->req
.buf
);
2381 kfree(udc
->status_req
);
2385 dma_pool_destroy(udc
->dtd_pool
);
2387 dma_free_coherent(&dev
->dev
, udc
->ep_dqh_size
,
2388 udc
->ep_dqh
, udc
->ep_dqh_dma
);
2390 if (udc
->pdata
->phy_deinit
)
2391 udc
->pdata
->phy_deinit(udc
->phy_regs
);
2392 udc_clock_disable(udc
);
2394 iounmap((void *)udc
->phy_regs
);
2396 iounmap(udc
->cap_regs
);
2398 for (clk_i
--; clk_i
>= 0; clk_i
--)
2399 clk_put(udc
->clk
[clk_i
]);
2400 the_controller
= NULL
;
2406 static int mv_udc_suspend(struct device
*_dev
)
2408 struct mv_udc
*udc
= the_controller
;
2415 static int mv_udc_resume(struct device
*_dev
)
2417 struct mv_udc
*udc
= the_controller
;
2420 if (udc
->pdata
->phy_init
) {
2421 retval
= udc
->pdata
->phy_init(udc
->phy_regs
);
2423 dev_err(&udc
->dev
->dev
,
2424 "init phy error %d when resume back\n",
2437 static const struct dev_pm_ops mv_udc_pm_ops
= {
2438 .suspend
= mv_udc_suspend
,
2439 .resume
= mv_udc_resume
,
2443 static void mv_udc_shutdown(struct platform_device
*dev
)
2445 struct mv_udc
*udc
= the_controller
;
2448 /* reset controller mode to IDLE */
2449 mode
= readl(&udc
->op_regs
->usbmode
);
2451 writel(mode
, &udc
->op_regs
->usbmode
);
2454 static struct platform_driver udc_driver
= {
2455 .probe
= mv_udc_probe
,
2456 .remove
= __exit_p(mv_udc_remove
),
2457 .shutdown
= mv_udc_shutdown
,
2459 .owner
= THIS_MODULE
,
2462 .pm
= &mv_udc_pm_ops
,
2466 MODULE_ALIAS("platform:pxa-u2o");
2468 MODULE_DESCRIPTION(DRIVER_DESC
);
2469 MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
2470 MODULE_VERSION(DRIVER_VERSION
);
2471 MODULE_LICENSE("GPL");
2474 static int __init
init(void)
2476 return platform_driver_register(&udc_driver
);
2481 static void __exit
cleanup(void)
2483 platform_driver_unregister(&udc_driver
);
2485 module_exit(cleanup
);