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/err.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/unaligned.h>
41 #define DRIVER_DESC "Marvell PXA USB Device Controller driver"
42 #define DRIVER_VERSION "8 Nov 2010"
44 #define ep_dir(ep) (((ep)->ep_num == 0) ? \
45 ((ep)->udc->ep0_dir) : ((ep)->direction))
47 /* timeout value -- usec */
48 #define RESET_TIMEOUT 10000
49 #define FLUSH_TIMEOUT 10000
50 #define EPSTATUS_TIMEOUT 10000
51 #define PRIME_TIMEOUT 10000
52 #define READSAFE_TIMEOUT 1000
54 #define LOOPS_USEC_SHIFT 1
55 #define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
56 #define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
58 static DECLARE_COMPLETION(release_done
);
60 static const char driver_name
[] = "mv_udc";
61 static const char driver_desc
[] = DRIVER_DESC
;
63 static void nuke(struct mv_ep
*ep
, int status
);
64 static void stop_activity(struct mv_udc
*udc
, struct usb_gadget_driver
*driver
);
66 /* for endpoint 0 operations */
67 static const struct usb_endpoint_descriptor mv_ep0_desc
= {
68 .bLength
= USB_DT_ENDPOINT_SIZE
,
69 .bDescriptorType
= USB_DT_ENDPOINT
,
70 .bEndpointAddress
= 0,
71 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
72 .wMaxPacketSize
= EP0_MAX_PKT_SIZE
,
75 static void ep0_reset(struct mv_udc
*udc
)
82 for (i
= 0; i
< 2; i
++) {
87 ep
->dqh
= &udc
->ep_dqh
[i
];
89 /* configure ep0 endpoint capabilities in dQH */
90 ep
->dqh
->max_packet_length
=
91 (EP0_MAX_PKT_SIZE
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
94 ep
->dqh
->next_dtd_ptr
= EP_QUEUE_HEAD_NEXT_TERMINATE
;
96 epctrlx
= readl(&udc
->op_regs
->epctrlx
[0]);
98 epctrlx
|= EPCTRL_TX_ENABLE
99 | (USB_ENDPOINT_XFER_CONTROL
100 << EPCTRL_TX_EP_TYPE_SHIFT
);
103 epctrlx
|= EPCTRL_RX_ENABLE
104 | (USB_ENDPOINT_XFER_CONTROL
105 << EPCTRL_RX_EP_TYPE_SHIFT
);
108 writel(epctrlx
, &udc
->op_regs
->epctrlx
[0]);
112 /* protocol ep0 stall, will automatically be cleared on new transaction */
113 static void ep0_stall(struct mv_udc
*udc
)
117 /* set TX and RX to stall */
118 epctrlx
= readl(&udc
->op_regs
->epctrlx
[0]);
119 epctrlx
|= EPCTRL_RX_EP_STALL
| EPCTRL_TX_EP_STALL
;
120 writel(epctrlx
, &udc
->op_regs
->epctrlx
[0]);
122 /* update ep0 state */
123 udc
->ep0_state
= WAIT_FOR_SETUP
;
124 udc
->ep0_dir
= EP_DIR_OUT
;
127 static int process_ep_req(struct mv_udc
*udc
, int index
,
128 struct mv_req
*curr_req
)
130 struct mv_dtd
*curr_dtd
;
131 struct mv_dqh
*curr_dqh
;
132 int actual
, remaining_length
;
138 curr_dqh
= &udc
->ep_dqh
[index
];
139 direction
= index
% 2;
141 curr_dtd
= curr_req
->head
;
142 actual
= curr_req
->req
.length
;
144 for (i
= 0; i
< curr_req
->dtd_count
; i
++) {
145 if (curr_dtd
->size_ioc_sts
& DTD_STATUS_ACTIVE
) {
146 dev_dbg(&udc
->dev
->dev
, "%s, dTD not completed\n",
147 udc
->eps
[index
].name
);
151 errors
= curr_dtd
->size_ioc_sts
& DTD_ERROR_MASK
;
154 (curr_dtd
->size_ioc_sts
& DTD_PACKET_SIZE
)
155 >> DTD_LENGTH_BIT_POS
;
156 actual
-= remaining_length
;
158 if (remaining_length
) {
160 dev_dbg(&udc
->dev
->dev
,
161 "TX dTD remains data\n");
168 dev_info(&udc
->dev
->dev
,
169 "complete_tr error: ep=%d %s: error = 0x%x\n",
170 index
>> 1, direction
? "SEND" : "RECV",
172 if (errors
& DTD_STATUS_HALTED
) {
173 /* Clear the errors and Halt condition */
174 curr_dqh
->size_ioc_int_sts
&= ~errors
;
176 } else if (errors
& DTD_STATUS_DATA_BUFF_ERR
) {
178 } else if (errors
& DTD_STATUS_TRANSACTION_ERR
) {
182 if (i
!= curr_req
->dtd_count
- 1)
183 curr_dtd
= (struct mv_dtd
*)curr_dtd
->next_dtd_virt
;
188 if (direction
== EP_DIR_OUT
)
189 bit_pos
= 1 << curr_req
->ep
->ep_num
;
191 bit_pos
= 1 << (16 + curr_req
->ep
->ep_num
);
193 while ((curr_dqh
->curr_dtd_ptr
== curr_dtd
->td_dma
)) {
194 if (curr_dtd
->dtd_next
== EP_QUEUE_HEAD_NEXT_TERMINATE
) {
195 while (readl(&udc
->op_regs
->epstatus
) & bit_pos
)
202 curr_req
->req
.actual
= actual
;
208 * done() - retire a request; caller blocked irqs
209 * @status : request status to be set, only works when
210 * request is still in progress.
212 static void done(struct mv_ep
*ep
, struct mv_req
*req
, int status
)
213 __releases(&ep
->udc
->lock
)
214 __acquires(&ep
->udc
->lock
)
216 struct mv_udc
*udc
= NULL
;
217 unsigned char stopped
= ep
->stopped
;
218 struct mv_dtd
*curr_td
, *next_td
;
221 udc
= (struct mv_udc
*)ep
->udc
;
222 /* Removed the req from fsl_ep->queue */
223 list_del_init(&req
->queue
);
225 /* req.status should be set as -EINPROGRESS in ep_queue() */
226 if (req
->req
.status
== -EINPROGRESS
)
227 req
->req
.status
= status
;
229 status
= req
->req
.status
;
231 /* Free dtd for the request */
233 for (j
= 0; j
< req
->dtd_count
; j
++) {
235 if (j
!= req
->dtd_count
- 1)
236 next_td
= curr_td
->next_dtd_virt
;
237 dma_pool_free(udc
->dtd_pool
, curr_td
, curr_td
->td_dma
);
240 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep_dir(ep
));
242 if (status
&& (status
!= -ESHUTDOWN
))
243 dev_info(&udc
->dev
->dev
, "complete %s req %p stat %d len %u/%u",
244 ep
->ep
.name
, &req
->req
, status
,
245 req
->req
.actual
, req
->req
.length
);
249 spin_unlock(&ep
->udc
->lock
);
251 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
253 spin_lock(&ep
->udc
->lock
);
254 ep
->stopped
= stopped
;
257 static int queue_dtd(struct mv_ep
*ep
, struct mv_req
*req
)
261 u32 bit_pos
, direction
;
262 u32 usbcmd
, epstatus
;
267 direction
= ep_dir(ep
);
268 dqh
= &(udc
->ep_dqh
[ep
->ep_num
* 2 + direction
]);
269 bit_pos
= 1 << (((direction
== EP_DIR_OUT
) ? 0 : 16) + ep
->ep_num
);
271 /* check if the pipe is empty */
272 if (!(list_empty(&ep
->queue
))) {
273 struct mv_req
*lastreq
;
274 lastreq
= list_entry(ep
->queue
.prev
, struct mv_req
, queue
);
275 lastreq
->tail
->dtd_next
=
276 req
->head
->td_dma
& EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
280 if (readl(&udc
->op_regs
->epprime
) & bit_pos
)
283 loops
= LOOPS(READSAFE_TIMEOUT
);
285 /* start with setting the semaphores */
286 usbcmd
= readl(&udc
->op_regs
->usbcmd
);
287 usbcmd
|= USBCMD_ATDTW_TRIPWIRE_SET
;
288 writel(usbcmd
, &udc
->op_regs
->usbcmd
);
290 /* read the endpoint status */
291 epstatus
= readl(&udc
->op_regs
->epstatus
) & bit_pos
;
294 * Reread the ATDTW semaphore bit to check if it is
295 * cleared. When hardware see a hazard, it will clear
296 * the bit or else we remain set to 1 and we can
297 * proceed with priming of endpoint if not already
300 if (readl(&udc
->op_regs
->usbcmd
)
301 & USBCMD_ATDTW_TRIPWIRE_SET
)
306 dev_err(&udc
->dev
->dev
,
307 "Timeout for ATDTW_TRIPWIRE...\n");
314 /* Clear the semaphore */
315 usbcmd
= readl(&udc
->op_regs
->usbcmd
);
316 usbcmd
&= USBCMD_ATDTW_TRIPWIRE_CLEAR
;
317 writel(usbcmd
, &udc
->op_regs
->usbcmd
);
323 /* Write dQH next pointer and terminate bit to 0 */
324 dqh
->next_dtd_ptr
= req
->head
->td_dma
325 & EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
327 /* clear active and halt bit, in case set from a previous error */
328 dqh
->size_ioc_int_sts
&= ~(DTD_STATUS_ACTIVE
| DTD_STATUS_HALTED
);
330 /* Ensure that updates to the QH will occur before priming. */
333 /* Prime the Endpoint */
334 writel(bit_pos
, &udc
->op_regs
->epprime
);
340 static struct mv_dtd
*build_dtd(struct mv_req
*req
, unsigned *length
,
341 dma_addr_t
*dma
, int *is_last
)
348 /* how big will this transfer be? */
349 if (usb_endpoint_xfer_isoc(req
->ep
->ep
.desc
)) {
351 mult
= (dqh
->max_packet_length
>> EP_QUEUE_HEAD_MULT_POS
)
353 *length
= min(req
->req
.length
- req
->req
.actual
,
354 (unsigned)(mult
* req
->ep
->ep
.maxpacket
));
356 *length
= min(req
->req
.length
- req
->req
.actual
,
357 (unsigned)EP_MAX_LENGTH_TRANSFER
);
362 * Be careful that no _GFP_HIGHMEM is set,
363 * or we can not use dma_to_virt
365 dtd
= dma_pool_alloc(udc
->dtd_pool
, GFP_ATOMIC
, dma
);
370 /* initialize buffer page pointers */
371 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
372 dtd
->buff_ptr0
= cpu_to_le32(temp
);
374 dtd
->buff_ptr1
= cpu_to_le32(temp
+ 0x1000);
375 dtd
->buff_ptr2
= cpu_to_le32(temp
+ 0x2000);
376 dtd
->buff_ptr3
= cpu_to_le32(temp
+ 0x3000);
377 dtd
->buff_ptr4
= cpu_to_le32(temp
+ 0x4000);
379 req
->req
.actual
+= *length
;
381 /* zlp is needed if req->req.zero is set */
383 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
387 } else if (req
->req
.length
== req
->req
.actual
)
392 /* Fill in the transfer size; set active bit */
393 temp
= ((*length
<< DTD_LENGTH_BIT_POS
) | DTD_STATUS_ACTIVE
);
395 /* Enable interrupt for the last dtd of a request */
396 if (*is_last
&& !req
->req
.no_interrupt
)
401 dtd
->size_ioc_sts
= temp
;
408 /* generate dTD linked list for a request */
409 static int req_to_dtd(struct mv_req
*req
)
412 int is_last
, is_first
= 1;
413 struct mv_dtd
*dtd
, *last_dtd
= NULL
;
417 dtd
= build_dtd(req
, &count
, &dma
, &is_last
);
425 last_dtd
->dtd_next
= dma
;
426 last_dtd
->next_dtd_virt
= dtd
;
432 /* set terminate bit to 1 for the last dTD */
433 dtd
->dtd_next
= DTD_NEXT_TERMINATE
;
440 static int mv_ep_enable(struct usb_ep
*_ep
,
441 const struct usb_endpoint_descriptor
*desc
)
447 u32 bit_pos
, epctrlx
, direction
;
448 unsigned char zlt
= 0, ios
= 0, mult
= 0;
451 ep
= container_of(_ep
, struct mv_ep
, ep
);
455 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
458 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
461 direction
= ep_dir(ep
);
462 max
= usb_endpoint_maxp(desc
);
465 * disable HW zero length termination select
466 * driver handles zero length packet through req->req.zero
470 bit_pos
= 1 << ((direction
== EP_DIR_OUT
? 0 : 16) + ep
->ep_num
);
472 /* Check if the Endpoint is Primed */
473 if ((readl(&udc
->op_regs
->epprime
) & bit_pos
)
474 || (readl(&udc
->op_regs
->epstatus
) & bit_pos
)) {
475 dev_info(&udc
->dev
->dev
,
476 "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
477 " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
478 (unsigned)ep
->ep_num
, direction
? "SEND" : "RECV",
479 (unsigned)readl(&udc
->op_regs
->epprime
),
480 (unsigned)readl(&udc
->op_regs
->epstatus
),
484 /* Set the max packet length, interrupt on Setup and Mult fields */
485 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
486 case USB_ENDPOINT_XFER_BULK
:
490 case USB_ENDPOINT_XFER_CONTROL
:
492 case USB_ENDPOINT_XFER_INT
:
495 case USB_ENDPOINT_XFER_ISOC
:
496 /* Calculate transactions needed for high bandwidth iso */
497 mult
= usb_endpoint_maxp_mult(desc
);
498 /* 3 transactions at most */
506 spin_lock_irqsave(&udc
->lock
, flags
);
507 /* Get the endpoint queue head address */
509 dqh
->max_packet_length
= (max
<< EP_QUEUE_HEAD_MAX_PKT_LEN_POS
)
510 | (mult
<< EP_QUEUE_HEAD_MULT_POS
)
511 | (zlt
? EP_QUEUE_HEAD_ZLT_SEL
: 0)
512 | (ios
? EP_QUEUE_HEAD_IOS
: 0);
513 dqh
->next_dtd_ptr
= 1;
514 dqh
->size_ioc_int_sts
= 0;
516 ep
->ep
.maxpacket
= max
;
520 /* Enable the endpoint for Rx or Tx and set the endpoint type */
521 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
522 if (direction
== EP_DIR_IN
) {
523 epctrlx
&= ~EPCTRL_TX_ALL_MASK
;
524 epctrlx
|= EPCTRL_TX_ENABLE
| EPCTRL_TX_DATA_TOGGLE_RST
525 | ((desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
526 << EPCTRL_TX_EP_TYPE_SHIFT
);
528 epctrlx
&= ~EPCTRL_RX_ALL_MASK
;
529 epctrlx
|= EPCTRL_RX_ENABLE
| EPCTRL_RX_DATA_TOGGLE_RST
530 | ((desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
531 << EPCTRL_RX_EP_TYPE_SHIFT
);
533 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
536 * Implement Guideline (GL# USB-7) The unused endpoint type must
537 * be programmed to bulk.
539 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
540 if ((epctrlx
& EPCTRL_RX_ENABLE
) == 0) {
541 epctrlx
|= (USB_ENDPOINT_XFER_BULK
542 << EPCTRL_RX_EP_TYPE_SHIFT
);
543 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
546 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
547 if ((epctrlx
& EPCTRL_TX_ENABLE
) == 0) {
548 epctrlx
|= (USB_ENDPOINT_XFER_BULK
549 << EPCTRL_TX_EP_TYPE_SHIFT
);
550 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
553 spin_unlock_irqrestore(&udc
->lock
, flags
);
560 static int mv_ep_disable(struct usb_ep
*_ep
)
565 u32 epctrlx
, direction
;
568 ep
= container_of(_ep
, struct mv_ep
, ep
);
569 if ((_ep
== NULL
) || !ep
->ep
.desc
)
574 /* Get the endpoint queue head address */
577 spin_lock_irqsave(&udc
->lock
, flags
);
579 direction
= ep_dir(ep
);
581 /* Reset the max packet length and the interrupt on Setup */
582 dqh
->max_packet_length
= 0;
584 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
585 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
586 epctrlx
&= ~((direction
== EP_DIR_IN
)
587 ? (EPCTRL_TX_ENABLE
| EPCTRL_TX_TYPE
)
588 : (EPCTRL_RX_ENABLE
| EPCTRL_RX_TYPE
));
589 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
591 /* nuke all pending requests (does flush) */
592 nuke(ep
, -ESHUTDOWN
);
597 spin_unlock_irqrestore(&udc
->lock
, flags
);
602 static struct usb_request
*
603 mv_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
605 struct mv_req
*req
= NULL
;
607 req
= kzalloc(sizeof *req
, gfp_flags
);
611 req
->req
.dma
= DMA_ADDR_INVALID
;
612 INIT_LIST_HEAD(&req
->queue
);
617 static void mv_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
619 struct mv_req
*req
= NULL
;
621 req
= container_of(_req
, struct mv_req
, req
);
627 static void mv_ep_fifo_flush(struct usb_ep
*_ep
)
630 u32 bit_pos
, direction
;
637 ep
= container_of(_ep
, struct mv_ep
, ep
);
642 direction
= ep_dir(ep
);
645 bit_pos
= (1 << 16) | 1;
646 else if (direction
== EP_DIR_OUT
)
647 bit_pos
= 1 << ep
->ep_num
;
649 bit_pos
= 1 << (16 + ep
->ep_num
);
651 loops
= LOOPS(EPSTATUS_TIMEOUT
);
653 unsigned int inter_loops
;
656 dev_err(&udc
->dev
->dev
,
657 "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
658 (unsigned)readl(&udc
->op_regs
->epstatus
),
662 /* Write 1 to the Flush register */
663 writel(bit_pos
, &udc
->op_regs
->epflush
);
665 /* Wait until flushing completed */
666 inter_loops
= LOOPS(FLUSH_TIMEOUT
);
667 while (readl(&udc
->op_regs
->epflush
)) {
669 * ENDPTFLUSH bit should be cleared to indicate this
670 * operation is complete
672 if (inter_loops
== 0) {
673 dev_err(&udc
->dev
->dev
,
674 "TIMEOUT for ENDPTFLUSH=0x%x,"
676 (unsigned)readl(&udc
->op_regs
->epflush
),
684 } while (readl(&udc
->op_regs
->epstatus
) & bit_pos
);
687 /* queues (submits) an I/O request to an endpoint */
689 mv_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
691 struct mv_ep
*ep
= container_of(_ep
, struct mv_ep
, ep
);
692 struct mv_req
*req
= container_of(_req
, struct mv_req
, req
);
693 struct mv_udc
*udc
= ep
->udc
;
697 /* catch various bogus parameters */
698 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
699 || !list_empty(&req
->queue
)) {
700 dev_err(&udc
->dev
->dev
, "%s, bad params", __func__
);
703 if (unlikely(!_ep
|| !ep
->ep
.desc
)) {
704 dev_err(&udc
->dev
->dev
, "%s, bad ep", __func__
);
709 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
714 /* map virtual address to hardware */
715 retval
= usb_gadget_map_request(&udc
->gadget
, _req
, ep_dir(ep
));
719 req
->req
.status
= -EINPROGRESS
;
723 spin_lock_irqsave(&udc
->lock
, flags
);
725 /* build dtds and push them to device queue */
726 if (!req_to_dtd(req
)) {
727 retval
= queue_dtd(ep
, req
);
729 spin_unlock_irqrestore(&udc
->lock
, flags
);
730 dev_err(&udc
->dev
->dev
, "Failed to queue dtd\n");
734 spin_unlock_irqrestore(&udc
->lock
, flags
);
735 dev_err(&udc
->dev
->dev
, "Failed to dma_pool_alloc\n");
740 /* Update ep0 state */
742 udc
->ep0_state
= DATA_STATE_XMIT
;
744 /* irq handler advances the queue */
745 list_add_tail(&req
->queue
, &ep
->queue
);
746 spin_unlock_irqrestore(&udc
->lock
, flags
);
751 usb_gadget_unmap_request(&udc
->gadget
, _req
, ep_dir(ep
));
756 static void mv_prime_ep(struct mv_ep
*ep
, struct mv_req
*req
)
758 struct mv_dqh
*dqh
= ep
->dqh
;
761 /* Write dQH next pointer and terminate bit to 0 */
762 dqh
->next_dtd_ptr
= req
->head
->td_dma
763 & EP_QUEUE_HEAD_NEXT_POINTER_MASK
;
765 /* clear active and halt bit, in case set from a previous error */
766 dqh
->size_ioc_int_sts
&= ~(DTD_STATUS_ACTIVE
| DTD_STATUS_HALTED
);
768 /* Ensure that updates to the QH will occure before priming. */
771 bit_pos
= 1 << (((ep_dir(ep
) == EP_DIR_OUT
) ? 0 : 16) + ep
->ep_num
);
773 /* Prime the Endpoint */
774 writel(bit_pos
, &ep
->udc
->op_regs
->epprime
);
777 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
778 static int mv_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
780 struct mv_ep
*ep
= container_of(_ep
, struct mv_ep
, ep
);
782 struct mv_udc
*udc
= ep
->udc
;
784 int stopped
, ret
= 0;
790 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
791 stopped
= ep
->stopped
;
793 /* Stop the ep before we deal with the queue */
795 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
796 if (ep_dir(ep
) == EP_DIR_IN
)
797 epctrlx
&= ~EPCTRL_TX_ENABLE
;
799 epctrlx
&= ~EPCTRL_RX_ENABLE
;
800 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
802 /* make sure it's actually queued on this endpoint */
803 list_for_each_entry(req
, &ep
->queue
, queue
) {
804 if (&req
->req
== _req
)
807 if (&req
->req
!= _req
) {
812 /* The request is in progress, or completed but not dequeued */
813 if (ep
->queue
.next
== &req
->queue
) {
814 _req
->status
= -ECONNRESET
;
815 mv_ep_fifo_flush(_ep
); /* flush current transfer */
817 /* The request isn't the last request in this ep queue */
818 if (req
->queue
.next
!= &ep
->queue
) {
819 struct mv_req
*next_req
;
821 next_req
= list_entry(req
->queue
.next
,
822 struct mv_req
, queue
);
824 /* Point the QH to the first TD of next request */
825 mv_prime_ep(ep
, next_req
);
830 qh
->next_dtd_ptr
= 1;
831 qh
->size_ioc_int_sts
= 0;
834 /* The request hasn't been processed, patch up the TD chain */
836 struct mv_req
*prev_req
;
838 prev_req
= list_entry(req
->queue
.prev
, struct mv_req
, queue
);
839 writel(readl(&req
->tail
->dtd_next
),
840 &prev_req
->tail
->dtd_next
);
844 done(ep
, req
, -ECONNRESET
);
848 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep
->ep_num
]);
849 if (ep_dir(ep
) == EP_DIR_IN
)
850 epctrlx
|= EPCTRL_TX_ENABLE
;
852 epctrlx
|= EPCTRL_RX_ENABLE
;
853 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep
->ep_num
]);
854 ep
->stopped
= stopped
;
856 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
860 static void ep_set_stall(struct mv_udc
*udc
, u8 ep_num
, u8 direction
, int stall
)
864 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep_num
]);
867 if (direction
== EP_DIR_IN
)
868 epctrlx
|= EPCTRL_TX_EP_STALL
;
870 epctrlx
|= EPCTRL_RX_EP_STALL
;
872 if (direction
== EP_DIR_IN
) {
873 epctrlx
&= ~EPCTRL_TX_EP_STALL
;
874 epctrlx
|= EPCTRL_TX_DATA_TOGGLE_RST
;
876 epctrlx
&= ~EPCTRL_RX_EP_STALL
;
877 epctrlx
|= EPCTRL_RX_DATA_TOGGLE_RST
;
880 writel(epctrlx
, &udc
->op_regs
->epctrlx
[ep_num
]);
883 static int ep_is_stall(struct mv_udc
*udc
, u8 ep_num
, u8 direction
)
887 epctrlx
= readl(&udc
->op_regs
->epctrlx
[ep_num
]);
889 if (direction
== EP_DIR_OUT
)
890 return (epctrlx
& EPCTRL_RX_EP_STALL
) ? 1 : 0;
892 return (epctrlx
& EPCTRL_TX_EP_STALL
) ? 1 : 0;
895 static int mv_ep_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
898 unsigned long flags
= 0;
902 ep
= container_of(_ep
, struct mv_ep
, ep
);
904 if (!_ep
|| !ep
->ep
.desc
) {
909 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
910 status
= -EOPNOTSUPP
;
915 * Attempt to halt IN ep will fail if any transfer requests
918 if (halt
&& (ep_dir(ep
) == EP_DIR_IN
) && !list_empty(&ep
->queue
)) {
923 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
924 ep_set_stall(udc
, ep
->ep_num
, ep_dir(ep
), halt
);
929 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
931 if (ep
->ep_num
== 0) {
932 udc
->ep0_state
= WAIT_FOR_SETUP
;
933 udc
->ep0_dir
= EP_DIR_OUT
;
939 static int mv_ep_set_halt(struct usb_ep
*_ep
, int halt
)
941 return mv_ep_set_halt_wedge(_ep
, halt
, 0);
944 static int mv_ep_set_wedge(struct usb_ep
*_ep
)
946 return mv_ep_set_halt_wedge(_ep
, 1, 1);
949 static const struct usb_ep_ops mv_ep_ops
= {
950 .enable
= mv_ep_enable
,
951 .disable
= mv_ep_disable
,
953 .alloc_request
= mv_alloc_request
,
954 .free_request
= mv_free_request
,
956 .queue
= mv_ep_queue
,
957 .dequeue
= mv_ep_dequeue
,
959 .set_wedge
= mv_ep_set_wedge
,
960 .set_halt
= mv_ep_set_halt
,
961 .fifo_flush
= mv_ep_fifo_flush
, /* flush fifo */
964 static void udc_clock_enable(struct mv_udc
*udc
)
966 clk_prepare_enable(udc
->clk
);
969 static void udc_clock_disable(struct mv_udc
*udc
)
971 clk_disable_unprepare(udc
->clk
);
974 static void udc_stop(struct mv_udc
*udc
)
978 /* Disable interrupts */
979 tmp
= readl(&udc
->op_regs
->usbintr
);
980 tmp
&= ~(USBINTR_INT_EN
| USBINTR_ERR_INT_EN
|
981 USBINTR_PORT_CHANGE_DETECT_EN
| USBINTR_RESET_EN
);
982 writel(tmp
, &udc
->op_regs
->usbintr
);
986 /* Reset the Run the bit in the command register to stop VUSB */
987 tmp
= readl(&udc
->op_regs
->usbcmd
);
988 tmp
&= ~USBCMD_RUN_STOP
;
989 writel(tmp
, &udc
->op_regs
->usbcmd
);
992 static void udc_start(struct mv_udc
*udc
)
996 usbintr
= USBINTR_INT_EN
| USBINTR_ERR_INT_EN
997 | USBINTR_PORT_CHANGE_DETECT_EN
998 | USBINTR_RESET_EN
| USBINTR_DEVICE_SUSPEND
;
999 /* Enable interrupts */
1000 writel(usbintr
, &udc
->op_regs
->usbintr
);
1004 /* Set the Run bit in the command register */
1005 writel(USBCMD_RUN_STOP
, &udc
->op_regs
->usbcmd
);
1008 static int udc_reset(struct mv_udc
*udc
)
1013 /* Stop the controller */
1014 tmp
= readl(&udc
->op_regs
->usbcmd
);
1015 tmp
&= ~USBCMD_RUN_STOP
;
1016 writel(tmp
, &udc
->op_regs
->usbcmd
);
1018 /* Reset the controller to get default values */
1019 writel(USBCMD_CTRL_RESET
, &udc
->op_regs
->usbcmd
);
1021 /* wait for reset to complete */
1022 loops
= LOOPS(RESET_TIMEOUT
);
1023 while (readl(&udc
->op_regs
->usbcmd
) & USBCMD_CTRL_RESET
) {
1025 dev_err(&udc
->dev
->dev
,
1026 "Wait for RESET completed TIMEOUT\n");
1033 /* set controller to device mode */
1034 tmp
= readl(&udc
->op_regs
->usbmode
);
1035 tmp
|= USBMODE_CTRL_MODE_DEVICE
;
1037 /* turn setup lockout off, require setup tripwire in usbcmd */
1038 tmp
|= USBMODE_SETUP_LOCK_OFF
;
1040 writel(tmp
, &udc
->op_regs
->usbmode
);
1042 writel(0x0, &udc
->op_regs
->epsetupstat
);
1044 /* Configure the Endpoint List Address */
1045 writel(udc
->ep_dqh_dma
& USB_EP_LIST_ADDRESS_MASK
,
1046 &udc
->op_regs
->eplistaddr
);
1048 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1049 if (readl(&udc
->cap_regs
->hcsparams
) & HCSPARAMS_PPC
)
1050 portsc
&= (~PORTSCX_W1C_BITS
| ~PORTSCX_PORT_POWER
);
1053 portsc
|= PORTSCX_FORCE_FULL_SPEED_CONNECT
;
1055 portsc
&= (~PORTSCX_FORCE_FULL_SPEED_CONNECT
);
1057 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1059 tmp
= readl(&udc
->op_regs
->epctrlx
[0]);
1060 tmp
&= ~(EPCTRL_TX_EP_STALL
| EPCTRL_RX_EP_STALL
);
1061 writel(tmp
, &udc
->op_regs
->epctrlx
[0]);
1066 static int mv_udc_enable_internal(struct mv_udc
*udc
)
1073 dev_dbg(&udc
->dev
->dev
, "enable udc\n");
1074 udc_clock_enable(udc
);
1075 if (udc
->pdata
->phy_init
) {
1076 retval
= udc
->pdata
->phy_init(udc
->phy_regs
);
1078 dev_err(&udc
->dev
->dev
,
1079 "init phy error %d\n", retval
);
1080 udc_clock_disable(udc
);
1089 static int mv_udc_enable(struct mv_udc
*udc
)
1091 if (udc
->clock_gating
)
1092 return mv_udc_enable_internal(udc
);
1097 static void mv_udc_disable_internal(struct mv_udc
*udc
)
1100 dev_dbg(&udc
->dev
->dev
, "disable udc\n");
1101 if (udc
->pdata
->phy_deinit
)
1102 udc
->pdata
->phy_deinit(udc
->phy_regs
);
1103 udc_clock_disable(udc
);
1108 static void mv_udc_disable(struct mv_udc
*udc
)
1110 if (udc
->clock_gating
)
1111 mv_udc_disable_internal(udc
);
1114 static int mv_udc_get_frame(struct usb_gadget
*gadget
)
1122 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1124 retval
= readl(&udc
->op_regs
->frindex
) & USB_FRINDEX_MASKS
;
1129 /* Tries to wake up the host connected to this gadget */
1130 static int mv_udc_wakeup(struct usb_gadget
*gadget
)
1132 struct mv_udc
*udc
= container_of(gadget
, struct mv_udc
, gadget
);
1135 /* Remote wakeup feature not enabled by host */
1136 if (!udc
->remote_wakeup
)
1139 portsc
= readl(&udc
->op_regs
->portsc
);
1140 /* not suspended? */
1141 if (!(portsc
& PORTSCX_PORT_SUSPEND
))
1143 /* trigger force resume */
1144 portsc
|= PORTSCX_PORT_FORCE_RESUME
;
1145 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1149 static int mv_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1152 unsigned long flags
;
1155 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1156 spin_lock_irqsave(&udc
->lock
, flags
);
1158 udc
->vbus_active
= (is_active
!= 0);
1160 dev_dbg(&udc
->dev
->dev
, "%s: softconnect %d, vbus_active %d\n",
1161 __func__
, udc
->softconnect
, udc
->vbus_active
);
1163 if (udc
->driver
&& udc
->softconnect
&& udc
->vbus_active
) {
1164 retval
= mv_udc_enable(udc
);
1166 /* Clock is disabled, need re-init registers */
1171 } else if (udc
->driver
&& udc
->softconnect
) {
1175 /* stop all the transfer in queue*/
1176 stop_activity(udc
, udc
->driver
);
1178 mv_udc_disable(udc
);
1182 spin_unlock_irqrestore(&udc
->lock
, flags
);
1186 static int mv_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1189 unsigned long flags
;
1192 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1193 spin_lock_irqsave(&udc
->lock
, flags
);
1195 udc
->softconnect
= (is_on
!= 0);
1197 dev_dbg(&udc
->dev
->dev
, "%s: softconnect %d, vbus_active %d\n",
1198 __func__
, udc
->softconnect
, udc
->vbus_active
);
1200 if (udc
->driver
&& udc
->softconnect
&& udc
->vbus_active
) {
1201 retval
= mv_udc_enable(udc
);
1203 /* Clock is disabled, need re-init registers */
1208 } else if (udc
->driver
&& udc
->vbus_active
) {
1209 /* stop all the transfer in queue*/
1210 stop_activity(udc
, udc
->driver
);
1212 mv_udc_disable(udc
);
1215 spin_unlock_irqrestore(&udc
->lock
, flags
);
1219 static int mv_udc_start(struct usb_gadget
*, struct usb_gadget_driver
*);
1220 static int mv_udc_stop(struct usb_gadget
*);
1221 /* device controller usb_gadget_ops structure */
1222 static const struct usb_gadget_ops mv_ops
= {
1224 /* returns the current frame number */
1225 .get_frame
= mv_udc_get_frame
,
1227 /* tries to wake up the host connected to this gadget */
1228 .wakeup
= mv_udc_wakeup
,
1230 /* notify controller that VBUS is powered or not */
1231 .vbus_session
= mv_udc_vbus_session
,
1233 /* D+ pullup, software-controlled connect/disconnect to USB host */
1234 .pullup
= mv_udc_pullup
,
1235 .udc_start
= mv_udc_start
,
1236 .udc_stop
= mv_udc_stop
,
1239 static int eps_init(struct mv_udc
*udc
)
1245 /* initialize ep0 */
1248 strncpy(ep
->name
, "ep0", sizeof(ep
->name
));
1249 ep
->ep
.name
= ep
->name
;
1250 ep
->ep
.ops
= &mv_ep_ops
;
1253 usb_ep_set_maxpacket_limit(&ep
->ep
, EP0_MAX_PKT_SIZE
);
1254 ep
->ep
.caps
.type_control
= true;
1255 ep
->ep
.caps
.dir_in
= true;
1256 ep
->ep
.caps
.dir_out
= true;
1258 ep
->ep
.desc
= &mv_ep0_desc
;
1259 INIT_LIST_HEAD(&ep
->queue
);
1261 ep
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
1263 /* initialize other endpoints */
1264 for (i
= 2; i
< udc
->max_eps
* 2; i
++) {
1267 snprintf(name
, sizeof(name
), "ep%din", i
/ 2);
1268 ep
->direction
= EP_DIR_IN
;
1269 ep
->ep
.caps
.dir_in
= true;
1271 snprintf(name
, sizeof(name
), "ep%dout", i
/ 2);
1272 ep
->direction
= EP_DIR_OUT
;
1273 ep
->ep
.caps
.dir_out
= true;
1276 strncpy(ep
->name
, name
, sizeof(ep
->name
));
1277 ep
->ep
.name
= ep
->name
;
1279 ep
->ep
.caps
.type_iso
= true;
1280 ep
->ep
.caps
.type_bulk
= true;
1281 ep
->ep
.caps
.type_int
= true;
1283 ep
->ep
.ops
= &mv_ep_ops
;
1285 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
1288 INIT_LIST_HEAD(&ep
->queue
);
1289 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1291 ep
->dqh
= &udc
->ep_dqh
[i
];
1297 /* delete all endpoint requests, called with spinlock held */
1298 static void nuke(struct mv_ep
*ep
, int status
)
1300 /* called with spinlock held */
1303 /* endpoint fifo flush */
1304 mv_ep_fifo_flush(&ep
->ep
);
1306 while (!list_empty(&ep
->queue
)) {
1307 struct mv_req
*req
= NULL
;
1308 req
= list_entry(ep
->queue
.next
, struct mv_req
, queue
);
1309 done(ep
, req
, status
);
1313 static void gadget_reset(struct mv_udc
*udc
, struct usb_gadget_driver
*driver
)
1317 nuke(&udc
->eps
[0], -ESHUTDOWN
);
1319 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1320 nuke(ep
, -ESHUTDOWN
);
1323 /* report reset; the driver is already quiesced */
1325 spin_unlock(&udc
->lock
);
1326 usb_gadget_udc_reset(&udc
->gadget
, driver
);
1327 spin_lock(&udc
->lock
);
1330 /* stop all USB activities */
1331 static void stop_activity(struct mv_udc
*udc
, struct usb_gadget_driver
*driver
)
1335 nuke(&udc
->eps
[0], -ESHUTDOWN
);
1337 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1338 nuke(ep
, -ESHUTDOWN
);
1341 /* report disconnect; the driver is already quiesced */
1343 spin_unlock(&udc
->lock
);
1344 driver
->disconnect(&udc
->gadget
);
1345 spin_lock(&udc
->lock
);
1349 static int mv_udc_start(struct usb_gadget
*gadget
,
1350 struct usb_gadget_driver
*driver
)
1354 unsigned long flags
;
1356 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1361 spin_lock_irqsave(&udc
->lock
, flags
);
1363 /* hook up the driver ... */
1364 driver
->driver
.bus
= NULL
;
1365 udc
->driver
= driver
;
1367 udc
->usb_state
= USB_STATE_ATTACHED
;
1368 udc
->ep0_state
= WAIT_FOR_SETUP
;
1369 udc
->ep0_dir
= EP_DIR_OUT
;
1371 spin_unlock_irqrestore(&udc
->lock
, flags
);
1373 if (udc
->transceiver
) {
1374 retval
= otg_set_peripheral(udc
->transceiver
->otg
,
1377 dev_err(&udc
->dev
->dev
,
1378 "unable to register peripheral to otg\n");
1384 /* When boot with cable attached, there will be no vbus irq occurred */
1386 queue_work(udc
->qwork
, &udc
->vbus_work
);
1391 static int mv_udc_stop(struct usb_gadget
*gadget
)
1394 unsigned long flags
;
1396 udc
= container_of(gadget
, struct mv_udc
, gadget
);
1398 spin_lock_irqsave(&udc
->lock
, flags
);
1403 /* stop all usb activities */
1404 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1405 stop_activity(udc
, NULL
);
1406 mv_udc_disable(udc
);
1408 spin_unlock_irqrestore(&udc
->lock
, flags
);
1410 /* unbind gadget driver */
1416 static void mv_set_ptc(struct mv_udc
*udc
, u32 mode
)
1420 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1421 portsc
|= mode
<< 16;
1422 writel(portsc
, &udc
->op_regs
->portsc
[0]);
1425 static void prime_status_complete(struct usb_ep
*ep
, struct usb_request
*_req
)
1427 struct mv_ep
*mvep
= container_of(ep
, struct mv_ep
, ep
);
1428 struct mv_req
*req
= container_of(_req
, struct mv_req
, req
);
1430 unsigned long flags
;
1434 dev_info(&udc
->dev
->dev
, "switch to test mode %d\n", req
->test_mode
);
1436 spin_lock_irqsave(&udc
->lock
, flags
);
1437 if (req
->test_mode
) {
1438 mv_set_ptc(udc
, req
->test_mode
);
1441 spin_unlock_irqrestore(&udc
->lock
, flags
);
1445 udc_prime_status(struct mv_udc
*udc
, u8 direction
, u16 status
, bool empty
)
1452 udc
->ep0_dir
= direction
;
1453 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1455 req
= udc
->status_req
;
1457 /* fill in the reqest structure */
1458 if (empty
== false) {
1459 *((u16
*) req
->req
.buf
) = cpu_to_le16(status
);
1460 req
->req
.length
= 2;
1462 req
->req
.length
= 0;
1465 req
->req
.status
= -EINPROGRESS
;
1466 req
->req
.actual
= 0;
1467 if (udc
->test_mode
) {
1468 req
->req
.complete
= prime_status_complete
;
1469 req
->test_mode
= udc
->test_mode
;
1472 req
->req
.complete
= NULL
;
1475 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1476 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1477 req
->req
.buf
, req
->req
.length
,
1478 ep_dir(ep
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1482 /* prime the data phase */
1483 if (!req_to_dtd(req
)) {
1484 retval
= queue_dtd(ep
, req
);
1486 dev_err(&udc
->dev
->dev
,
1487 "Failed to queue dtd when prime status\n");
1490 } else{ /* no mem */
1492 dev_err(&udc
->dev
->dev
,
1493 "Failed to dma_pool_alloc when prime status\n");
1497 list_add_tail(&req
->queue
, &ep
->queue
);
1501 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep_dir(ep
));
1506 static void mv_udc_testmode(struct mv_udc
*udc
, u16 index
)
1508 if (index
<= TEST_FORCE_EN
) {
1509 udc
->test_mode
= index
;
1510 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1513 dev_err(&udc
->dev
->dev
,
1514 "This test mode(%d) is not supported\n", index
);
1517 static void ch9setaddress(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1519 udc
->dev_addr
= (u8
)setup
->wValue
;
1521 /* update usb state */
1522 udc
->usb_state
= USB_STATE_ADDRESS
;
1524 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1528 static void ch9getstatus(struct mv_udc
*udc
, u8 ep_num
,
1529 struct usb_ctrlrequest
*setup
)
1534 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
1535 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
1538 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1539 status
= 1 << USB_DEVICE_SELF_POWERED
;
1540 status
|= udc
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
1541 } else if ((setup
->bRequestType
& USB_RECIP_MASK
)
1542 == USB_RECIP_INTERFACE
) {
1543 /* get interface status */
1545 } else if ((setup
->bRequestType
& USB_RECIP_MASK
)
1546 == USB_RECIP_ENDPOINT
) {
1547 u8 ep_num
, direction
;
1549 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1550 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1551 ? EP_DIR_IN
: EP_DIR_OUT
;
1552 status
= ep_is_stall(udc
, ep_num
, direction
)
1553 << USB_ENDPOINT_HALT
;
1556 retval
= udc_prime_status(udc
, EP_DIR_IN
, status
, false);
1560 udc
->ep0_state
= DATA_STATE_XMIT
;
1563 static void ch9clearfeature(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1569 if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1570 == ((USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))) {
1571 switch (setup
->wValue
) {
1572 case USB_DEVICE_REMOTE_WAKEUP
:
1573 udc
->remote_wakeup
= 0;
1578 } else if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1579 == ((USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
))) {
1580 switch (setup
->wValue
) {
1581 case USB_ENDPOINT_HALT
:
1582 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1583 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1584 ? EP_DIR_IN
: EP_DIR_OUT
;
1585 if (setup
->wValue
!= 0 || setup
->wLength
!= 0
1586 || ep_num
> udc
->max_eps
)
1588 ep
= &udc
->eps
[ep_num
* 2 + direction
];
1591 spin_unlock(&udc
->lock
);
1592 ep_set_stall(udc
, ep_num
, direction
, 0);
1593 spin_lock(&udc
->lock
);
1601 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1607 static void ch9setfeature(struct mv_udc
*udc
, struct usb_ctrlrequest
*setup
)
1612 if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1613 == ((USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))) {
1614 switch (setup
->wValue
) {
1615 case USB_DEVICE_REMOTE_WAKEUP
:
1616 udc
->remote_wakeup
= 1;
1618 case USB_DEVICE_TEST_MODE
:
1619 if (setup
->wIndex
& 0xFF
1620 || udc
->gadget
.speed
!= USB_SPEED_HIGH
)
1623 if (udc
->usb_state
!= USB_STATE_CONFIGURED
1624 && udc
->usb_state
!= USB_STATE_ADDRESS
1625 && udc
->usb_state
!= USB_STATE_DEFAULT
)
1628 mv_udc_testmode(udc
, (setup
->wIndex
>> 8));
1633 } else if ((setup
->bRequestType
& (USB_TYPE_MASK
| USB_RECIP_MASK
))
1634 == ((USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
))) {
1635 switch (setup
->wValue
) {
1636 case USB_ENDPOINT_HALT
:
1637 ep_num
= setup
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1638 direction
= (setup
->wIndex
& USB_ENDPOINT_DIR_MASK
)
1639 ? EP_DIR_IN
: EP_DIR_OUT
;
1640 if (setup
->wValue
!= 0 || setup
->wLength
!= 0
1641 || ep_num
> udc
->max_eps
)
1643 spin_unlock(&udc
->lock
);
1644 ep_set_stall(udc
, ep_num
, direction
, 1);
1645 spin_lock(&udc
->lock
);
1653 if (udc_prime_status(udc
, EP_DIR_IN
, 0, true))
1659 static void handle_setup_packet(struct mv_udc
*udc
, u8 ep_num
,
1660 struct usb_ctrlrequest
*setup
)
1661 __releases(&ep
->udc
->lock
)
1662 __acquires(&ep
->udc
->lock
)
1664 bool delegate
= false;
1666 nuke(&udc
->eps
[ep_num
* 2 + EP_DIR_OUT
], -ESHUTDOWN
);
1668 dev_dbg(&udc
->dev
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1669 setup
->bRequestType
, setup
->bRequest
,
1670 setup
->wValue
, setup
->wIndex
, setup
->wLength
);
1671 /* We process some standard setup requests here */
1672 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1673 switch (setup
->bRequest
) {
1674 case USB_REQ_GET_STATUS
:
1675 ch9getstatus(udc
, ep_num
, setup
);
1678 case USB_REQ_SET_ADDRESS
:
1679 ch9setaddress(udc
, setup
);
1682 case USB_REQ_CLEAR_FEATURE
:
1683 ch9clearfeature(udc
, setup
);
1686 case USB_REQ_SET_FEATURE
:
1687 ch9setfeature(udc
, setup
);
1696 /* delegate USB standard requests to the gadget driver */
1697 if (delegate
== true) {
1698 /* USB requests handled by gadget */
1699 if (setup
->wLength
) {
1700 /* DATA phase from gadget, STATUS phase from udc */
1701 udc
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1702 ? EP_DIR_IN
: EP_DIR_OUT
;
1703 spin_unlock(&udc
->lock
);
1704 if (udc
->driver
->setup(&udc
->gadget
,
1705 &udc
->local_setup_buff
) < 0)
1707 spin_lock(&udc
->lock
);
1708 udc
->ep0_state
= (setup
->bRequestType
& USB_DIR_IN
)
1709 ? DATA_STATE_XMIT
: DATA_STATE_RECV
;
1711 /* no DATA phase, IN STATUS phase from gadget */
1712 udc
->ep0_dir
= EP_DIR_IN
;
1713 spin_unlock(&udc
->lock
);
1714 if (udc
->driver
->setup(&udc
->gadget
,
1715 &udc
->local_setup_buff
) < 0)
1717 spin_lock(&udc
->lock
);
1718 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1723 /* complete DATA or STATUS phase of ep0 prime status phase if needed */
1724 static void ep0_req_complete(struct mv_udc
*udc
,
1725 struct mv_ep
*ep0
, struct mv_req
*req
)
1729 if (udc
->usb_state
== USB_STATE_ADDRESS
) {
1730 /* set the new address */
1731 new_addr
= (u32
)udc
->dev_addr
;
1732 writel(new_addr
<< USB_DEVICE_ADDRESS_BIT_SHIFT
,
1733 &udc
->op_regs
->deviceaddr
);
1738 switch (udc
->ep0_state
) {
1739 case DATA_STATE_XMIT
:
1740 /* receive status phase */
1741 if (udc_prime_status(udc
, EP_DIR_OUT
, 0, true))
1744 case DATA_STATE_RECV
:
1745 /* send status phase */
1746 if (udc_prime_status(udc
, EP_DIR_IN
, 0 , true))
1749 case WAIT_FOR_OUT_STATUS
:
1750 udc
->ep0_state
= WAIT_FOR_SETUP
;
1752 case WAIT_FOR_SETUP
:
1753 dev_err(&udc
->dev
->dev
, "unexpect ep0 packets\n");
1761 static void get_setup_data(struct mv_udc
*udc
, u8 ep_num
, u8
*buffer_ptr
)
1766 dqh
= &udc
->ep_dqh
[ep_num
* 2 + EP_DIR_OUT
];
1768 /* Clear bit in ENDPTSETUPSTAT */
1769 writel((1 << ep_num
), &udc
->op_regs
->epsetupstat
);
1771 /* while a hazard exists when setup package arrives */
1773 /* Set Setup Tripwire */
1774 temp
= readl(&udc
->op_regs
->usbcmd
);
1775 writel(temp
| USBCMD_SETUP_TRIPWIRE_SET
, &udc
->op_regs
->usbcmd
);
1777 /* Copy the setup packet to local buffer */
1778 memcpy(buffer_ptr
, (u8
*) dqh
->setup_buffer
, 8);
1779 } while (!(readl(&udc
->op_regs
->usbcmd
) & USBCMD_SETUP_TRIPWIRE_SET
));
1781 /* Clear Setup Tripwire */
1782 temp
= readl(&udc
->op_regs
->usbcmd
);
1783 writel(temp
& ~USBCMD_SETUP_TRIPWIRE_SET
, &udc
->op_regs
->usbcmd
);
1786 static void irq_process_tr_complete(struct mv_udc
*udc
)
1789 int i
, ep_num
= 0, direction
= 0;
1790 struct mv_ep
*curr_ep
;
1791 struct mv_req
*curr_req
, *temp_req
;
1795 * We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
1796 * because the setup packets are to be read ASAP
1799 /* Process all Setup packet received interrupts */
1800 tmp
= readl(&udc
->op_regs
->epsetupstat
);
1803 for (i
= 0; i
< udc
->max_eps
; i
++) {
1804 if (tmp
& (1 << i
)) {
1805 get_setup_data(udc
, i
,
1806 (u8
*)(&udc
->local_setup_buff
));
1807 handle_setup_packet(udc
, i
,
1808 &udc
->local_setup_buff
);
1813 /* Don't clear the endpoint setup status register here.
1814 * It is cleared as a setup packet is read out of the buffer
1817 /* Process non-setup transaction complete interrupts */
1818 tmp
= readl(&udc
->op_regs
->epcomplete
);
1823 writel(tmp
, &udc
->op_regs
->epcomplete
);
1825 for (i
= 0; i
< udc
->max_eps
* 2; i
++) {
1829 bit_pos
= 1 << (ep_num
+ 16 * direction
);
1831 if (!(bit_pos
& tmp
))
1835 curr_ep
= &udc
->eps
[0];
1837 curr_ep
= &udc
->eps
[i
];
1838 /* process the req queue until an uncomplete request */
1839 list_for_each_entry_safe(curr_req
, temp_req
,
1840 &curr_ep
->queue
, queue
) {
1841 status
= process_ep_req(udc
, i
, curr_req
);
1845 /* write back status to req */
1846 curr_req
->req
.status
= status
;
1848 /* ep0 request completion */
1850 ep0_req_complete(udc
, curr_ep
, curr_req
);
1853 done(curr_ep
, curr_req
, status
);
1859 static void irq_process_reset(struct mv_udc
*udc
)
1864 udc
->ep0_dir
= EP_DIR_OUT
;
1865 udc
->ep0_state
= WAIT_FOR_SETUP
;
1866 udc
->remote_wakeup
= 0; /* default to 0 on reset */
1868 /* The address bits are past bit 25-31. Set the address */
1869 tmp
= readl(&udc
->op_regs
->deviceaddr
);
1870 tmp
&= ~(USB_DEVICE_ADDRESS_MASK
);
1871 writel(tmp
, &udc
->op_regs
->deviceaddr
);
1873 /* Clear all the setup token semaphores */
1874 tmp
= readl(&udc
->op_regs
->epsetupstat
);
1875 writel(tmp
, &udc
->op_regs
->epsetupstat
);
1877 /* Clear all the endpoint complete status bits */
1878 tmp
= readl(&udc
->op_regs
->epcomplete
);
1879 writel(tmp
, &udc
->op_regs
->epcomplete
);
1881 /* wait until all endptprime bits cleared */
1882 loops
= LOOPS(PRIME_TIMEOUT
);
1883 while (readl(&udc
->op_regs
->epprime
) & 0xFFFFFFFF) {
1885 dev_err(&udc
->dev
->dev
,
1886 "Timeout for ENDPTPRIME = 0x%x\n",
1887 readl(&udc
->op_regs
->epprime
));
1894 /* Write 1s to the Flush register */
1895 writel((u32
)~0, &udc
->op_regs
->epflush
);
1897 if (readl(&udc
->op_regs
->portsc
[0]) & PORTSCX_PORT_RESET
) {
1898 dev_info(&udc
->dev
->dev
, "usb bus reset\n");
1899 udc
->usb_state
= USB_STATE_DEFAULT
;
1900 /* reset all the queues, stop all USB activities */
1901 gadget_reset(udc
, udc
->driver
);
1903 dev_info(&udc
->dev
->dev
, "USB reset portsc 0x%x\n",
1904 readl(&udc
->op_regs
->portsc
));
1912 /* reset all the queues, stop all USB activities */
1913 stop_activity(udc
, udc
->driver
);
1915 /* reset ep0 dQH and endptctrl */
1918 /* enable interrupt and set controller to run state */
1921 udc
->usb_state
= USB_STATE_ATTACHED
;
1925 static void handle_bus_resume(struct mv_udc
*udc
)
1927 udc
->usb_state
= udc
->resume_state
;
1928 udc
->resume_state
= 0;
1930 /* report resume to the driver */
1932 if (udc
->driver
->resume
) {
1933 spin_unlock(&udc
->lock
);
1934 udc
->driver
->resume(&udc
->gadget
);
1935 spin_lock(&udc
->lock
);
1940 static void irq_process_suspend(struct mv_udc
*udc
)
1942 udc
->resume_state
= udc
->usb_state
;
1943 udc
->usb_state
= USB_STATE_SUSPENDED
;
1945 if (udc
->driver
->suspend
) {
1946 spin_unlock(&udc
->lock
);
1947 udc
->driver
->suspend(&udc
->gadget
);
1948 spin_lock(&udc
->lock
);
1952 static void irq_process_port_change(struct mv_udc
*udc
)
1956 portsc
= readl(&udc
->op_regs
->portsc
[0]);
1957 if (!(portsc
& PORTSCX_PORT_RESET
)) {
1959 u32 speed
= portsc
& PORTSCX_PORT_SPEED_MASK
;
1961 case PORTSCX_PORT_SPEED_HIGH
:
1962 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1964 case PORTSCX_PORT_SPEED_FULL
:
1965 udc
->gadget
.speed
= USB_SPEED_FULL
;
1967 case PORTSCX_PORT_SPEED_LOW
:
1968 udc
->gadget
.speed
= USB_SPEED_LOW
;
1971 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1976 if (portsc
& PORTSCX_PORT_SUSPEND
) {
1977 udc
->resume_state
= udc
->usb_state
;
1978 udc
->usb_state
= USB_STATE_SUSPENDED
;
1979 if (udc
->driver
->suspend
) {
1980 spin_unlock(&udc
->lock
);
1981 udc
->driver
->suspend(&udc
->gadget
);
1982 spin_lock(&udc
->lock
);
1986 if (!(portsc
& PORTSCX_PORT_SUSPEND
)
1987 && udc
->usb_state
== USB_STATE_SUSPENDED
) {
1988 handle_bus_resume(udc
);
1991 if (!udc
->resume_state
)
1992 udc
->usb_state
= USB_STATE_DEFAULT
;
1995 static void irq_process_error(struct mv_udc
*udc
)
1997 /* Increment the error count */
2001 static irqreturn_t
mv_udc_irq(int irq
, void *dev
)
2003 struct mv_udc
*udc
= (struct mv_udc
*)dev
;
2006 /* Disable ISR when stopped bit is set */
2010 spin_lock(&udc
->lock
);
2012 status
= readl(&udc
->op_regs
->usbsts
);
2013 intr
= readl(&udc
->op_regs
->usbintr
);
2017 spin_unlock(&udc
->lock
);
2021 /* Clear all the interrupts occurred */
2022 writel(status
, &udc
->op_regs
->usbsts
);
2024 if (status
& USBSTS_ERR
)
2025 irq_process_error(udc
);
2027 if (status
& USBSTS_RESET
)
2028 irq_process_reset(udc
);
2030 if (status
& USBSTS_PORT_CHANGE
)
2031 irq_process_port_change(udc
);
2033 if (status
& USBSTS_INT
)
2034 irq_process_tr_complete(udc
);
2036 if (status
& USBSTS_SUSPEND
)
2037 irq_process_suspend(udc
);
2039 spin_unlock(&udc
->lock
);
2044 static irqreturn_t
mv_udc_vbus_irq(int irq
, void *dev
)
2046 struct mv_udc
*udc
= (struct mv_udc
*)dev
;
2048 /* polling VBUS and init phy may cause too much time*/
2050 queue_work(udc
->qwork
, &udc
->vbus_work
);
2055 static void mv_udc_vbus_work(struct work_struct
*work
)
2060 udc
= container_of(work
, struct mv_udc
, vbus_work
);
2061 if (!udc
->pdata
->vbus
)
2064 vbus
= udc
->pdata
->vbus
->poll();
2065 dev_info(&udc
->dev
->dev
, "vbus is %d\n", vbus
);
2067 if (vbus
== VBUS_HIGH
)
2068 mv_udc_vbus_session(&udc
->gadget
, 1);
2069 else if (vbus
== VBUS_LOW
)
2070 mv_udc_vbus_session(&udc
->gadget
, 0);
2073 /* release device structure */
2074 static void gadget_release(struct device
*_dev
)
2078 udc
= dev_get_drvdata(_dev
);
2080 complete(udc
->done
);
2083 static int mv_udc_remove(struct platform_device
*pdev
)
2087 udc
= platform_get_drvdata(pdev
);
2089 usb_del_gadget_udc(&udc
->gadget
);
2092 flush_workqueue(udc
->qwork
);
2093 destroy_workqueue(udc
->qwork
);
2096 /* free memory allocated in probe */
2097 dma_pool_destroy(udc
->dtd_pool
);
2100 dma_free_coherent(&pdev
->dev
, udc
->ep_dqh_size
,
2101 udc
->ep_dqh
, udc
->ep_dqh_dma
);
2103 mv_udc_disable(udc
);
2105 /* free dev, wait for the release() finished */
2106 wait_for_completion(udc
->done
);
2111 static int mv_udc_probe(struct platform_device
*pdev
)
2113 struct mv_usb_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2119 if (pdata
== NULL
) {
2120 dev_err(&pdev
->dev
, "missing platform_data\n");
2124 udc
= devm_kzalloc(&pdev
->dev
, sizeof(*udc
), GFP_KERNEL
);
2128 udc
->done
= &release_done
;
2129 udc
->pdata
= dev_get_platdata(&pdev
->dev
);
2130 spin_lock_init(&udc
->lock
);
2134 if (pdata
->mode
== MV_USB_MODE_OTG
) {
2135 udc
->transceiver
= devm_usb_get_phy(&pdev
->dev
,
2137 if (IS_ERR(udc
->transceiver
)) {
2138 retval
= PTR_ERR(udc
->transceiver
);
2140 if (retval
== -ENXIO
)
2143 udc
->transceiver
= NULL
;
2144 return -EPROBE_DEFER
;
2148 /* udc only have one sysclk. */
2149 udc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
2150 if (IS_ERR(udc
->clk
))
2151 return PTR_ERR(udc
->clk
);
2153 r
= platform_get_resource_byname(udc
->dev
, IORESOURCE_MEM
, "capregs");
2155 dev_err(&pdev
->dev
, "no I/O memory resource defined\n");
2159 udc
->cap_regs
= (struct mv_cap_regs __iomem
*)
2160 devm_ioremap(&pdev
->dev
, r
->start
, resource_size(r
));
2161 if (udc
->cap_regs
== NULL
) {
2162 dev_err(&pdev
->dev
, "failed to map I/O memory\n");
2166 r
= platform_get_resource_byname(udc
->dev
, IORESOURCE_MEM
, "phyregs");
2168 dev_err(&pdev
->dev
, "no phy I/O memory resource defined\n");
2172 udc
->phy_regs
= devm_ioremap(&pdev
->dev
, r
->start
, resource_size(r
));
2173 if (udc
->phy_regs
== NULL
) {
2174 dev_err(&pdev
->dev
, "failed to map phy I/O memory\n");
2178 /* we will acces controller register, so enable the clk */
2179 retval
= mv_udc_enable_internal(udc
);
2184 (struct mv_op_regs __iomem
*)((unsigned long)udc
->cap_regs
2185 + (readl(&udc
->cap_regs
->caplength_hciversion
)
2187 udc
->max_eps
= readl(&udc
->cap_regs
->dccparams
) & DCCPARAMS_DEN_MASK
;
2190 * some platform will use usb to download image, it may not disconnect
2191 * usb gadget before loading kernel. So first stop udc here.
2194 writel(0xFFFFFFFF, &udc
->op_regs
->usbsts
);
2196 size
= udc
->max_eps
* sizeof(struct mv_dqh
) *2;
2197 size
= (size
+ DQH_ALIGNMENT
- 1) & ~(DQH_ALIGNMENT
- 1);
2198 udc
->ep_dqh
= dma_alloc_coherent(&pdev
->dev
, size
,
2199 &udc
->ep_dqh_dma
, GFP_KERNEL
);
2201 if (udc
->ep_dqh
== NULL
) {
2202 dev_err(&pdev
->dev
, "allocate dQH memory failed\n");
2204 goto err_disable_clock
;
2206 udc
->ep_dqh_size
= size
;
2208 /* create dTD dma_pool resource */
2209 udc
->dtd_pool
= dma_pool_create("mv_dtd",
2211 sizeof(struct mv_dtd
),
2215 if (!udc
->dtd_pool
) {
2220 size
= udc
->max_eps
* sizeof(struct mv_ep
) *2;
2221 udc
->eps
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
2222 if (udc
->eps
== NULL
) {
2224 goto err_destroy_dma
;
2227 /* initialize ep0 status request structure */
2228 udc
->status_req
= devm_kzalloc(&pdev
->dev
, sizeof(struct mv_req
),
2230 if (!udc
->status_req
) {
2232 goto err_destroy_dma
;
2234 INIT_LIST_HEAD(&udc
->status_req
->queue
);
2236 /* allocate a small amount of memory to get valid address */
2237 udc
->status_req
->req
.buf
= kzalloc(8, GFP_KERNEL
);
2238 udc
->status_req
->req
.dma
= DMA_ADDR_INVALID
;
2240 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2241 udc
->usb_state
= USB_STATE_POWERED
;
2242 udc
->ep0_dir
= EP_DIR_OUT
;
2243 udc
->remote_wakeup
= 0;
2245 r
= platform_get_resource(udc
->dev
, IORESOURCE_IRQ
, 0);
2247 dev_err(&pdev
->dev
, "no IRQ resource defined\n");
2249 goto err_destroy_dma
;
2251 udc
->irq
= r
->start
;
2252 if (devm_request_irq(&pdev
->dev
, udc
->irq
, mv_udc_irq
,
2253 IRQF_SHARED
, driver_name
, udc
)) {
2254 dev_err(&pdev
->dev
, "Request irq %d for UDC failed\n",
2257 goto err_destroy_dma
;
2260 /* initialize gadget structure */
2261 udc
->gadget
.ops
= &mv_ops
; /* usb_gadget_ops */
2262 udc
->gadget
.ep0
= &udc
->eps
[0].ep
; /* gadget ep0 */
2263 INIT_LIST_HEAD(&udc
->gadget
.ep_list
); /* ep_list */
2264 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
2265 udc
->gadget
.max_speed
= USB_SPEED_HIGH
; /* support dual speed */
2267 /* the "gadget" abstracts/virtualizes the controller */
2268 udc
->gadget
.name
= driver_name
; /* gadget name */
2272 /* VBUS detect: we can disable/enable clock on demand.*/
2273 if (udc
->transceiver
)
2274 udc
->clock_gating
= 1;
2275 else if (pdata
->vbus
) {
2276 udc
->clock_gating
= 1;
2277 retval
= devm_request_threaded_irq(&pdev
->dev
,
2278 pdata
->vbus
->irq
, NULL
,
2279 mv_udc_vbus_irq
, IRQF_ONESHOT
, "vbus", udc
);
2281 dev_info(&pdev
->dev
,
2282 "Can not request irq for VBUS, "
2283 "disable clock gating\n");
2284 udc
->clock_gating
= 0;
2287 udc
->qwork
= create_singlethread_workqueue("mv_udc_queue");
2289 dev_err(&pdev
->dev
, "cannot create workqueue\n");
2291 goto err_destroy_dma
;
2294 INIT_WORK(&udc
->vbus_work
, mv_udc_vbus_work
);
2298 * When clock gating is supported, we can disable clk and phy.
2299 * If not, it means that VBUS detection is not supported, we
2300 * have to enable vbus active all the time to let controller work.
2302 if (udc
->clock_gating
)
2303 mv_udc_disable_internal(udc
);
2305 udc
->vbus_active
= 1;
2307 retval
= usb_add_gadget_udc_release(&pdev
->dev
, &udc
->gadget
,
2310 goto err_create_workqueue
;
2312 platform_set_drvdata(pdev
, udc
);
2313 dev_info(&pdev
->dev
, "successful probe UDC device %s clock gating.\n",
2314 udc
->clock_gating
? "with" : "without");
2318 err_create_workqueue
:
2319 destroy_workqueue(udc
->qwork
);
2321 dma_pool_destroy(udc
->dtd_pool
);
2323 dma_free_coherent(&pdev
->dev
, udc
->ep_dqh_size
,
2324 udc
->ep_dqh
, udc
->ep_dqh_dma
);
2326 mv_udc_disable_internal(udc
);
2332 static int mv_udc_suspend(struct device
*dev
)
2336 udc
= dev_get_drvdata(dev
);
2338 /* if OTG is enabled, the following will be done in OTG driver*/
2339 if (udc
->transceiver
)
2342 if (udc
->pdata
->vbus
&& udc
->pdata
->vbus
->poll
)
2343 if (udc
->pdata
->vbus
->poll() == VBUS_HIGH
) {
2344 dev_info(&udc
->dev
->dev
, "USB cable is connected!\n");
2349 * only cable is unplugged, udc can suspend.
2350 * So do not care about clock_gating == 1.
2352 if (!udc
->clock_gating
) {
2355 spin_lock_irq(&udc
->lock
);
2356 /* stop all usb activities */
2357 stop_activity(udc
, udc
->driver
);
2358 spin_unlock_irq(&udc
->lock
);
2360 mv_udc_disable_internal(udc
);
2366 static int mv_udc_resume(struct device
*dev
)
2371 udc
= dev_get_drvdata(dev
);
2373 /* if OTG is enabled, the following will be done in OTG driver*/
2374 if (udc
->transceiver
)
2377 if (!udc
->clock_gating
) {
2378 retval
= mv_udc_enable_internal(udc
);
2382 if (udc
->driver
&& udc
->softconnect
) {
2392 static const struct dev_pm_ops mv_udc_pm_ops
= {
2393 .suspend
= mv_udc_suspend
,
2394 .resume
= mv_udc_resume
,
2398 static void mv_udc_shutdown(struct platform_device
*pdev
)
2403 udc
= platform_get_drvdata(pdev
);
2404 /* reset controller mode to IDLE */
2406 mode
= readl(&udc
->op_regs
->usbmode
);
2408 writel(mode
, &udc
->op_regs
->usbmode
);
2409 mv_udc_disable(udc
);
2412 static struct platform_driver udc_driver
= {
2413 .probe
= mv_udc_probe
,
2414 .remove
= mv_udc_remove
,
2415 .shutdown
= mv_udc_shutdown
,
2419 .pm
= &mv_udc_pm_ops
,
2424 module_platform_driver(udc_driver
);
2425 MODULE_ALIAS("platform:mv-udc");
2426 MODULE_DESCRIPTION(DRIVER_DESC
);
2427 MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
2428 MODULE_VERSION(DRIVER_VERSION
);
2429 MODULE_LICENSE("GPL");