2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmapool.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/ioport.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/timer.h>
20 #include <linux/list.h>
21 #include <linux/notifier.h>
22 #include <linux/interrupt.h>
23 #include <linux/moduleparam.h>
24 #include <linux/device.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
29 #include <linux/irq.h>
30 #include <linux/platform_device.h>
31 #include <linux/platform_data/mv_usb.h>
32 #include <linux/clk.h>
36 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
38 static const char driver_name
[] = "mv_u3d";
39 static const char driver_desc
[] = DRIVER_DESC
;
41 static void mv_u3d_nuke(struct mv_u3d_ep
*ep
, int status
);
42 static void mv_u3d_stop_activity(struct mv_u3d
*u3d
,
43 struct usb_gadget_driver
*driver
);
45 /* for endpoint 0 operations */
46 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc
= {
47 .bLength
= USB_DT_ENDPOINT_SIZE
,
48 .bDescriptorType
= USB_DT_ENDPOINT
,
49 .bEndpointAddress
= 0,
50 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
51 .wMaxPacketSize
= MV_U3D_EP0_MAX_PKT_SIZE
,
54 static void mv_u3d_ep0_reset(struct mv_u3d
*u3d
)
60 for (i
= 0; i
< 2; i
++) {
64 /* ep0 ep context, ep0 in and out share the same ep context */
65 ep
->ep_context
= &u3d
->ep_context
[1];
68 /* reset ep state machine */
70 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
71 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
72 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
74 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
75 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
77 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
78 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
79 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
80 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
81 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
82 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr1
);
85 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
86 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
87 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
89 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
90 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
92 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
93 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
94 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
95 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
96 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
97 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr1
);
100 static void mv_u3d_ep0_stall(struct mv_u3d
*u3d
)
103 dev_dbg(u3d
->dev
, "%s\n", __func__
);
105 /* set TX and RX to stall */
106 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
107 tmp
|= MV_U3D_EPXCR_EP_HALT
;
108 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
110 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
111 tmp
|= MV_U3D_EPXCR_EP_HALT
;
112 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
114 /* update ep0 state */
115 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
116 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
119 static int mv_u3d_process_ep_req(struct mv_u3d
*u3d
, int index
,
120 struct mv_u3d_req
*curr_req
)
122 struct mv_u3d_trb
*curr_trb
;
123 dma_addr_t cur_deq_lo
;
124 struct mv_u3d_ep_context
*curr_ep_context
;
125 int trb_complete
, actual
, remaining_length
= 0;
126 int direction
, ep_num
;
128 u32 tmp
, status
, length
;
130 curr_ep_context
= &u3d
->ep_context
[index
];
131 direction
= index
% 2;
135 actual
= curr_req
->req
.length
;
137 while (!list_empty(&curr_req
->trb_list
)) {
138 curr_trb
= list_entry(curr_req
->trb_list
.next
,
139 struct mv_u3d_trb
, trb_list
);
140 if (!curr_trb
->trb_hw
->ctrl
.own
) {
141 dev_err(u3d
->dev
, "%s, TRB own error!\n",
142 u3d
->eps
[index
].name
);
146 curr_trb
->trb_hw
->ctrl
.own
= 0;
147 if (direction
== MV_U3D_EP_DIR_OUT
) {
148 tmp
= ioread32(&u3d
->vuc_regs
->rxst
[ep_num
].statuslo
);
150 ioread32(&u3d
->vuc_regs
->rxst
[ep_num
].curdeqlo
);
152 tmp
= ioread32(&u3d
->vuc_regs
->txst
[ep_num
].statuslo
);
154 ioread32(&u3d
->vuc_regs
->txst
[ep_num
].curdeqlo
);
157 status
= tmp
>> MV_U3D_XFERSTATUS_COMPLETE_SHIFT
;
158 length
= tmp
& MV_U3D_XFERSTATUS_TRB_LENGTH_MASK
;
160 if (status
== MV_U3D_COMPLETE_SUCCESS
||
161 (status
== MV_U3D_COMPLETE_SHORT_PACKET
&&
162 direction
== MV_U3D_EP_DIR_OUT
)) {
163 remaining_length
+= length
;
164 actual
-= remaining_length
;
167 "complete_tr error: ep=%d %s: error = 0x%x\n",
168 index
>> 1, direction
? "SEND" : "RECV",
173 list_del_init(&curr_trb
->trb_list
);
178 curr_req
->req
.actual
= actual
;
183 * mv_u3d_done() - retire a request; caller blocked irqs
184 * @status : request status to be set, only works when
185 * request is still in progress.
188 void mv_u3d_done(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
, int status
)
189 __releases(&ep
->udc
->lock
)
190 __acquires(&ep
->udc
->lock
)
192 struct mv_u3d
*u3d
= (struct mv_u3d
*)ep
->u3d
;
194 dev_dbg(u3d
->dev
, "mv_u3d_done: remove req->queue\n");
195 /* Removed the req from ep queue */
196 list_del_init(&req
->queue
);
198 /* req.status should be set as -EINPROGRESS in ep_queue() */
199 if (req
->req
.status
== -EINPROGRESS
)
200 req
->req
.status
= status
;
202 status
= req
->req
.status
;
204 /* Free trb for the request */
206 dma_pool_free(u3d
->trb_pool
,
207 req
->trb_head
->trb_hw
, req
->trb_head
->trb_dma
);
209 dma_unmap_single(ep
->u3d
->gadget
.dev
.parent
,
210 (dma_addr_t
)req
->trb_head
->trb_dma
,
211 req
->trb_count
* sizeof(struct mv_u3d_trb_hw
),
213 kfree(req
->trb_head
->trb_hw
);
215 kfree(req
->trb_head
);
217 usb_gadget_unmap_request(&u3d
->gadget
, &req
->req
, mv_u3d_ep_dir(ep
));
219 if (status
&& (status
!= -ESHUTDOWN
)) {
220 dev_dbg(u3d
->dev
, "complete %s req %p stat %d len %u/%u",
221 ep
->ep
.name
, &req
->req
, status
,
222 req
->req
.actual
, req
->req
.length
);
225 spin_unlock(&ep
->u3d
->lock
);
227 * complete() is from gadget layer,
228 * eg fsg->bulk_in_complete()
230 if (req
->req
.complete
)
231 req
->req
.complete(&ep
->ep
, &req
->req
);
233 spin_lock(&ep
->u3d
->lock
);
236 static int mv_u3d_queue_trb(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
)
240 struct mv_u3d_ep_context
*ep_context
;
244 direction
= mv_u3d_ep_dir(ep
);
246 /* ep0 in and out share the same ep context slot 1*/
248 ep_context
= &(u3d
->ep_context
[1]);
250 ep_context
= &(u3d
->ep_context
[ep
->ep_num
* 2 + direction
]);
252 /* check if the pipe is empty or not */
253 if (!list_empty(&ep
->queue
)) {
254 dev_err(u3d
->dev
, "add trb to non-empty queue!\n");
258 ep_context
->rsvd0
= cpu_to_le32(1);
259 ep_context
->rsvd1
= 0;
261 /* Configure the trb address and set the DCS bit.
262 * Both DCS bit and own bit in trb should be set.
264 ep_context
->trb_addr_lo
=
265 cpu_to_le32(req
->trb_head
->trb_dma
| DCS_ENABLE
);
266 ep_context
->trb_addr_hi
= 0;
268 /* Ensure that updates to the EP Context will
269 * occure before Ring Bell.
273 /* ring bell the ep */
278 + ((direction
== MV_U3D_EP_DIR_OUT
) ? 0 : 1);
280 iowrite32(tmp
, &u3d
->op_regs
->doorbell
);
285 static struct mv_u3d_trb
*mv_u3d_build_trb_one(struct mv_u3d_req
*req
,
286 unsigned *length
, dma_addr_t
*dma
)
289 unsigned int direction
;
290 struct mv_u3d_trb
*trb
;
291 struct mv_u3d_trb_hw
*trb_hw
;
294 /* how big will this transfer be? */
295 *length
= req
->req
.length
- req
->req
.actual
;
296 BUG_ON(*length
> (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
300 trb
= kzalloc(sizeof(*trb
), GFP_ATOMIC
);
302 dev_err(u3d
->dev
, "%s, trb alloc fail\n", __func__
);
307 * Be careful that no _GFP_HIGHMEM is set,
308 * or we can not use dma_to_virt
309 * cannot use GFP_KERNEL in spin lock
311 trb_hw
= dma_pool_alloc(u3d
->trb_pool
, GFP_ATOMIC
, dma
);
314 "%s, dma_pool_alloc fail\n", __func__
);
318 trb
->trb_hw
= trb_hw
;
320 /* initialize buffer page pointers */
321 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
323 trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
324 trb_hw
->buf_addr_hi
= 0;
325 trb_hw
->trb_len
= cpu_to_le32(*length
);
326 trb_hw
->ctrl
.own
= 1;
328 if (req
->ep
->ep_num
== 0)
329 trb_hw
->ctrl
.type
= TYPE_DATA
;
331 trb_hw
->ctrl
.type
= TYPE_NORMAL
;
333 req
->req
.actual
+= *length
;
335 direction
= mv_u3d_ep_dir(req
->ep
);
336 if (direction
== MV_U3D_EP_DIR_IN
)
337 trb_hw
->ctrl
.dir
= 1;
339 trb_hw
->ctrl
.dir
= 0;
341 /* Enable interrupt for the last trb of a request */
342 if (!req
->req
.no_interrupt
)
343 trb_hw
->ctrl
.ioc
= 1;
345 trb_hw
->ctrl
.chain
= 0;
351 static int mv_u3d_build_trb_chain(struct mv_u3d_req
*req
, unsigned *length
,
352 struct mv_u3d_trb
*trb
, int *is_last
)
355 unsigned int direction
;
358 /* how big will this transfer be? */
359 *length
= min(req
->req
.length
- req
->req
.actual
,
360 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
366 /* initialize buffer page pointers */
367 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
369 trb
->trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
370 trb
->trb_hw
->buf_addr_hi
= 0;
371 trb
->trb_hw
->trb_len
= cpu_to_le32(*length
);
372 trb
->trb_hw
->ctrl
.own
= 1;
374 if (req
->ep
->ep_num
== 0)
375 trb
->trb_hw
->ctrl
.type
= TYPE_DATA
;
377 trb
->trb_hw
->ctrl
.type
= TYPE_NORMAL
;
379 req
->req
.actual
+= *length
;
381 direction
= mv_u3d_ep_dir(req
->ep
);
382 if (direction
== MV_U3D_EP_DIR_IN
)
383 trb
->trb_hw
->ctrl
.dir
= 1;
385 trb
->trb_hw
->ctrl
.dir
= 0;
387 /* zlp is needed if req->req.zero is set */
389 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
393 } else if (req
->req
.length
== req
->req
.actual
)
398 /* Enable interrupt for the last trb of a request */
399 if (*is_last
&& !req
->req
.no_interrupt
)
400 trb
->trb_hw
->ctrl
.ioc
= 1;
403 trb
->trb_hw
->ctrl
.chain
= 0;
405 trb
->trb_hw
->ctrl
.chain
= 1;
406 dev_dbg(u3d
->dev
, "chain trb\n");
414 /* generate TRB linked list for a request
415 * usb controller only supports continous trb chain,
416 * that trb structure physical address should be continous.
418 static int mv_u3d_req_to_trb(struct mv_u3d_req
*req
)
422 struct mv_u3d_trb
*trb
;
423 struct mv_u3d_trb_hw
*trb_hw
;
431 INIT_LIST_HEAD(&req
->trb_list
);
433 length
= req
->req
.length
- req
->req
.actual
;
434 /* normally the request transfer length is less than 16KB.
435 * we use buil_trb_one() to optimize it.
437 if (length
<= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
) {
438 trb
= mv_u3d_build_trb_one(req
, &count
, &dma
);
439 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
444 trb_num
= length
/ MV_U3D_EP_MAX_LENGTH_TRANSFER
;
445 if (length
% MV_U3D_EP_MAX_LENGTH_TRANSFER
)
448 trb
= kcalloc(trb_num
, sizeof(*trb
), GFP_ATOMIC
);
451 "%s, trb alloc fail\n", __func__
);
455 trb_hw
= kcalloc(trb_num
, sizeof(*trb_hw
), GFP_ATOMIC
);
458 "%s, trb_hw alloc fail\n", __func__
);
463 trb
->trb_hw
= trb_hw
;
464 if (mv_u3d_build_trb_chain(req
, &count
,
467 "%s, mv_u3d_build_trb_chain fail\n",
472 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
478 req
->trb_head
= list_entry(req
->trb_list
.next
,
479 struct mv_u3d_trb
, trb_list
);
480 req
->trb_head
->trb_dma
= dma_map_single(u3d
->gadget
.dev
.parent
,
481 req
->trb_head
->trb_hw
,
482 trb_num
* sizeof(*trb_hw
),
492 mv_u3d_start_queue(struct mv_u3d_ep
*ep
)
494 struct mv_u3d
*u3d
= ep
->u3d
;
495 struct mv_u3d_req
*req
;
498 if (!list_empty(&ep
->req_list
) && !ep
->processing
)
499 req
= list_entry(ep
->req_list
.next
, struct mv_u3d_req
, list
);
505 /* set up dma mapping */
506 ret
= usb_gadget_map_request(&u3d
->gadget
, &req
->req
,
511 req
->req
.status
= -EINPROGRESS
;
515 /* build trbs and push them to device queue */
516 if (!mv_u3d_req_to_trb(req
)) {
517 ret
= mv_u3d_queue_trb(ep
, req
);
524 dev_err(u3d
->dev
, "%s, mv_u3d_req_to_trb fail\n", __func__
);
528 /* irq handler advances the queue */
530 list_add_tail(&req
->queue
, &ep
->queue
);
535 static int mv_u3d_ep_enable(struct usb_ep
*_ep
,
536 const struct usb_endpoint_descriptor
*desc
)
539 struct mv_u3d_ep
*ep
;
540 struct mv_u3d_ep_context
*ep_context
;
542 unsigned maxburst
= 0;
543 u32 epxcr
, direction
;
545 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
548 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
551 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
)
554 direction
= mv_u3d_ep_dir(ep
);
555 max
= le16_to_cpu(desc
->wMaxPacketSize
);
559 maxburst
= _ep
->maxburst
;
561 /* Get the endpoint context address */
562 ep_context
= (struct mv_u3d_ep_context
*)ep
->ep_context
;
564 /* Set the max burst size */
565 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
566 case USB_ENDPOINT_XFER_BULK
:
569 "max burst should not be greater "
570 "than 16 on bulk ep\n");
572 _ep
->maxburst
= maxburst
;
575 "maxburst: %d on bulk %s\n", maxburst
, ep
->name
);
577 case USB_ENDPOINT_XFER_CONTROL
:
578 /* control transfer only supports maxburst as one */
580 _ep
->maxburst
= maxburst
;
582 case USB_ENDPOINT_XFER_INT
:
585 "max burst should be 1 on int ep "
586 "if transfer size is not 1024\n");
588 _ep
->maxburst
= maxburst
;
591 case USB_ENDPOINT_XFER_ISOC
:
594 "max burst should be 1 on isoc ep "
595 "if transfer size is not 1024\n");
597 _ep
->maxburst
= maxburst
;
604 ep
->ep
.maxpacket
= max
;
608 /* Enable the endpoint for Rx or Tx and set the endpoint type */
609 if (direction
== MV_U3D_EP_DIR_OUT
) {
610 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
611 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
612 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
614 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
615 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
617 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
618 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
619 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
620 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
621 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
623 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
624 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
625 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
627 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
628 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
630 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
631 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
632 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
633 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
634 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
642 static int mv_u3d_ep_disable(struct usb_ep
*_ep
)
645 struct mv_u3d_ep
*ep
;
646 struct mv_u3d_ep_context
*ep_context
;
647 u32 epxcr
, direction
;
653 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
659 /* Get the endpoint context address */
660 ep_context
= ep
->ep_context
;
662 direction
= mv_u3d_ep_dir(ep
);
664 /* nuke all pending requests (does flush) */
665 spin_lock_irqsave(&u3d
->lock
, flags
);
666 mv_u3d_nuke(ep
, -ESHUTDOWN
);
667 spin_unlock_irqrestore(&u3d
->lock
, flags
);
669 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
670 if (direction
== MV_U3D_EP_DIR_OUT
) {
671 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
672 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
673 | USB_ENDPOINT_XFERTYPE_MASK
);
674 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
676 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
677 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
678 | USB_ENDPOINT_XFERTYPE_MASK
);
679 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
688 static struct usb_request
*
689 mv_u3d_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
691 struct mv_u3d_req
*req
= NULL
;
693 req
= kzalloc(sizeof *req
, gfp_flags
);
697 INIT_LIST_HEAD(&req
->queue
);
702 static void mv_u3d_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
704 struct mv_u3d_req
*req
= container_of(_req
, struct mv_u3d_req
, req
);
709 static void mv_u3d_ep_fifo_flush(struct usb_ep
*_ep
)
713 struct mv_u3d_ep
*ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
717 /* if endpoint is not enabled, cannot flush endpoint */
722 direction
= mv_u3d_ep_dir(ep
);
724 /* ep0 need clear bit after flushing fifo. */
726 if (direction
== MV_U3D_EP_DIR_OUT
) {
727 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
728 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
729 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
731 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
732 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
734 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
735 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
736 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
738 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
739 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
744 if (direction
== MV_U3D_EP_DIR_OUT
) {
745 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
746 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
747 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
749 /* Wait until flushing completed */
750 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
751 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
) &
752 MV_U3D_EPXCR_EP_FLUSH
) {
754 * EP_FLUSH bit should be cleared to indicate this
755 * operation is complete
759 "EP FLUSH TIMEOUT for ep%d%s\n", ep
->ep_num
,
760 direction
? "in" : "out");
766 } else { /* EP_DIR_IN */
767 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
768 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
769 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
771 /* Wait until flushing completed */
772 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
773 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
) &
774 MV_U3D_EPXCR_EP_FLUSH
) {
776 * EP_FLUSH bit should be cleared to indicate this
777 * operation is complete
781 "EP FLUSH TIMEOUT for ep%d%s\n", ep
->ep_num
,
782 direction
? "in" : "out");
791 /* queues (submits) an I/O request to an endpoint */
793 mv_u3d_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
795 struct mv_u3d_ep
*ep
;
796 struct mv_u3d_req
*req
;
799 int is_first_req
= 0;
801 if (unlikely(!_ep
|| !_req
))
804 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
807 req
= container_of(_req
, struct mv_u3d_req
, req
);
810 && u3d
->ep0_state
== MV_U3D_STATUS_STAGE
812 dev_dbg(u3d
->dev
, "ep0 status stage\n");
813 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
817 dev_dbg(u3d
->dev
, "%s: %s, req: 0x%p\n",
818 __func__
, _ep
->name
, req
);
820 /* catch various bogus parameters */
821 if (!req
->req
.complete
|| !req
->req
.buf
822 || !list_empty(&req
->queue
)) {
824 "%s, bad params, _req: 0x%p,"
825 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
826 "list_empty: 0x%x\n",
828 req
->req
.complete
, req
->req
.buf
,
829 list_empty(&req
->queue
));
832 if (unlikely(!ep
->ep
.desc
)) {
833 dev_err(u3d
->dev
, "%s, bad ep\n", __func__
);
836 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
837 if (req
->req
.length
> ep
->ep
.maxpacket
)
841 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
843 "bad params of driver/speed\n");
849 /* Software list handles usb request. */
850 spin_lock_irqsave(&ep
->req_lock
, flags
);
851 is_first_req
= list_empty(&ep
->req_list
);
852 list_add_tail(&req
->list
, &ep
->req_list
);
853 spin_unlock_irqrestore(&ep
->req_lock
, flags
);
855 dev_dbg(u3d
->dev
, "list is not empty\n");
859 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from usb_ep_queue\n");
860 spin_lock_irqsave(&u3d
->lock
, flags
);
861 mv_u3d_start_queue(ep
);
862 spin_unlock_irqrestore(&u3d
->lock
, flags
);
866 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
867 static int mv_u3d_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
869 struct mv_u3d_ep
*ep
;
870 struct mv_u3d_req
*req
;
872 struct mv_u3d_ep_context
*ep_context
;
873 struct mv_u3d_req
*next_req
;
881 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
884 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
886 /* make sure it's actually queued on this endpoint */
887 list_for_each_entry(req
, &ep
->queue
, queue
) {
888 if (&req
->req
== _req
)
891 if (&req
->req
!= _req
) {
896 /* The request is in progress, or completed but not dequeued */
897 if (ep
->queue
.next
== &req
->queue
) {
898 _req
->status
= -ECONNRESET
;
899 mv_u3d_ep_fifo_flush(_ep
);
901 /* The request isn't the last request in this ep queue */
902 if (req
->queue
.next
!= &ep
->queue
) {
904 "it is the last request in this ep queue\n");
905 ep_context
= ep
->ep_context
;
906 next_req
= list_entry(req
->queue
.next
,
907 struct mv_u3d_req
, queue
);
909 /* Point first TRB of next request to the EP context. */
910 iowrite32((unsigned long) next_req
->trb_head
,
911 &ep_context
->trb_addr_lo
);
913 struct mv_u3d_ep_context
*ep_context
;
914 ep_context
= ep
->ep_context
;
915 ep_context
->trb_addr_lo
= 0;
916 ep_context
->trb_addr_hi
= 0;
922 mv_u3d_done(ep
, req
, -ECONNRESET
);
924 /* remove the req from the ep req list */
925 if (!list_empty(&ep
->req_list
)) {
926 struct mv_u3d_req
*curr_req
;
927 curr_req
= list_entry(ep
->req_list
.next
,
928 struct mv_u3d_req
, list
);
929 if (curr_req
== req
) {
930 list_del_init(&req
->list
);
936 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
941 mv_u3d_ep_set_stall(struct mv_u3d
*u3d
, u8 ep_num
, u8 direction
, int stall
)
944 struct mv_u3d_ep
*ep
= u3d
->eps
;
946 dev_dbg(u3d
->dev
, "%s\n", __func__
);
947 if (direction
== MV_U3D_EP_DIR_OUT
) {
948 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
950 tmp
|= MV_U3D_EPXCR_EP_HALT
;
952 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
953 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
955 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
957 tmp
|= MV_U3D_EPXCR_EP_HALT
;
959 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
960 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
964 static int mv_u3d_ep_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
966 struct mv_u3d_ep
*ep
;
967 unsigned long flags
= 0;
971 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
978 if (ep
->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
&& (mv_u3d_ep_dir(ep
) == MV_U3D_EP_DIR_IN
)
988 && !list_empty(&ep
->queue
)) {
993 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
994 mv_u3d_ep_set_stall(u3d
, ep
->ep_num
, mv_u3d_ep_dir(ep
), halt
);
999 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
1001 if (ep
->ep_num
== 0)
1002 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1007 static int mv_u3d_ep_set_halt(struct usb_ep
*_ep
, int halt
)
1009 return mv_u3d_ep_set_halt_wedge(_ep
, halt
, 0);
1012 static int mv_u3d_ep_set_wedge(struct usb_ep
*_ep
)
1014 return mv_u3d_ep_set_halt_wedge(_ep
, 1, 1);
1017 static struct usb_ep_ops mv_u3d_ep_ops
= {
1018 .enable
= mv_u3d_ep_enable
,
1019 .disable
= mv_u3d_ep_disable
,
1021 .alloc_request
= mv_u3d_alloc_request
,
1022 .free_request
= mv_u3d_free_request
,
1024 .queue
= mv_u3d_ep_queue
,
1025 .dequeue
= mv_u3d_ep_dequeue
,
1027 .set_wedge
= mv_u3d_ep_set_wedge
,
1028 .set_halt
= mv_u3d_ep_set_halt
,
1029 .fifo_flush
= mv_u3d_ep_fifo_flush
,
1032 static void mv_u3d_controller_stop(struct mv_u3d
*u3d
)
1036 if (!u3d
->clock_gating
&& u3d
->vbus_valid_detect
)
1037 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID
,
1038 &u3d
->vuc_regs
->intrenable
);
1040 iowrite32(0, &u3d
->vuc_regs
->intrenable
);
1041 iowrite32(~0x0, &u3d
->vuc_regs
->endcomplete
);
1042 iowrite32(~0x0, &u3d
->vuc_regs
->trbunderrun
);
1043 iowrite32(~0x0, &u3d
->vuc_regs
->trbcomplete
);
1044 iowrite32(~0x0, &u3d
->vuc_regs
->linkchange
);
1045 iowrite32(0x1, &u3d
->vuc_regs
->setuplock
);
1047 /* Reset the RUN bit in the command register to stop USB */
1048 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1049 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1050 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1051 dev_dbg(u3d
->dev
, "after u3d_stop, USBCMD 0x%x\n",
1052 ioread32(&u3d
->op_regs
->usbcmd
));
1055 static void mv_u3d_controller_start(struct mv_u3d
*u3d
)
1060 /* enable link LTSSM state machine */
1061 temp
= ioread32(&u3d
->vuc_regs
->ltssm
);
1062 temp
|= MV_U3D_LTSSM_PHY_INIT_DONE
;
1063 iowrite32(temp
, &u3d
->vuc_regs
->ltssm
);
1065 /* Enable interrupts */
1066 usbintr
= MV_U3D_INTR_ENABLE_LINK_CHG
| MV_U3D_INTR_ENABLE_TXDESC_ERR
|
1067 MV_U3D_INTR_ENABLE_RXDESC_ERR
| MV_U3D_INTR_ENABLE_TX_COMPLETE
|
1068 MV_U3D_INTR_ENABLE_RX_COMPLETE
| MV_U3D_INTR_ENABLE_SETUP
|
1069 (u3d
->vbus_valid_detect
? MV_U3D_INTR_ENABLE_VBUS_VALID
: 0);
1070 iowrite32(usbintr
, &u3d
->vuc_regs
->intrenable
);
1072 /* Enable ctrl ep */
1073 iowrite32(0x1, &u3d
->vuc_regs
->ctrlepenable
);
1075 /* Set the Run bit in the command register */
1076 iowrite32(MV_U3D_CMD_RUN_STOP
, &u3d
->op_regs
->usbcmd
);
1077 dev_dbg(u3d
->dev
, "after u3d_start, USBCMD 0x%x\n",
1078 ioread32(&u3d
->op_regs
->usbcmd
));
1081 static int mv_u3d_controller_reset(struct mv_u3d
*u3d
)
1086 /* Stop the controller */
1087 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1088 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1089 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1091 /* Reset the controller to get default values */
1092 iowrite32(MV_U3D_CMD_CTRL_RESET
, &u3d
->op_regs
->usbcmd
);
1094 /* wait for reset to complete */
1095 loops
= LOOPS(MV_U3D_RESET_TIMEOUT
);
1096 while (ioread32(&u3d
->op_regs
->usbcmd
) & MV_U3D_CMD_CTRL_RESET
) {
1099 "Wait for RESET completed TIMEOUT\n");
1106 /* Configure the Endpoint Context Address */
1107 iowrite32(u3d
->ep_context_dma
, &u3d
->op_regs
->dcbaapl
);
1108 iowrite32(0, &u3d
->op_regs
->dcbaaph
);
1113 static int mv_u3d_enable(struct mv_u3d
*u3d
)
1115 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1121 if (!u3d
->clock_gating
) {
1126 dev_dbg(u3d
->dev
, "enable u3d\n");
1127 clk_enable(u3d
->clk
);
1128 if (pdata
->phy_init
) {
1129 retval
= pdata
->phy_init(u3d
->phy_regs
);
1132 "init phy error %d\n", retval
);
1133 clk_disable(u3d
->clk
);
1142 static void mv_u3d_disable(struct mv_u3d
*u3d
)
1144 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1145 if (u3d
->clock_gating
&& u3d
->active
) {
1146 dev_dbg(u3d
->dev
, "disable u3d\n");
1147 if (pdata
->phy_deinit
)
1148 pdata
->phy_deinit(u3d
->phy_regs
);
1149 clk_disable(u3d
->clk
);
1154 static int mv_u3d_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1157 unsigned long flags
;
1160 u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1162 spin_lock_irqsave(&u3d
->lock
, flags
);
1164 u3d
->vbus_active
= (is_active
!= 0);
1165 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1166 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1168 * 1. external VBUS detect: we can disable/enable clock on demand.
1169 * 2. UDC VBUS detect: we have to enable clock all the time.
1170 * 3. No VBUS detect: we have to enable clock all the time.
1172 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1173 retval
= mv_u3d_enable(u3d
);
1176 * after clock is disabled, we lost all the register
1177 * context. We have to re-init registers
1179 mv_u3d_controller_reset(u3d
);
1180 mv_u3d_ep0_reset(u3d
);
1181 mv_u3d_controller_start(u3d
);
1183 } else if (u3d
->driver
&& u3d
->softconnect
) {
1187 /* stop all the transfer in queue*/
1188 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1189 mv_u3d_controller_stop(u3d
);
1190 mv_u3d_disable(u3d
);
1194 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1198 /* constrain controller's VBUS power usage
1199 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1200 * reporting how much power the device may consume. For example, this
1201 * could affect how quickly batteries are recharged.
1203 * Returns zero on success, else negative errno.
1205 static int mv_u3d_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1207 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1214 static int mv_u3d_pullup(struct usb_gadget
*gadget
, int is_on
)
1216 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1217 unsigned long flags
;
1220 spin_lock_irqsave(&u3d
->lock
, flags
);
1222 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1223 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1224 u3d
->softconnect
= (is_on
!= 0);
1225 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1226 retval
= mv_u3d_enable(u3d
);
1229 * after clock is disabled, we lost all the register
1230 * context. We have to re-init registers
1232 mv_u3d_controller_reset(u3d
);
1233 mv_u3d_ep0_reset(u3d
);
1234 mv_u3d_controller_start(u3d
);
1236 } else if (u3d
->driver
&& u3d
->vbus_active
) {
1237 /* stop all the transfer in queue*/
1238 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1239 mv_u3d_controller_stop(u3d
);
1240 mv_u3d_disable(u3d
);
1243 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1248 static int mv_u3d_start(struct usb_gadget
*g
,
1249 struct usb_gadget_driver
*driver
)
1251 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1252 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1253 unsigned long flags
;
1258 spin_lock_irqsave(&u3d
->lock
, flags
);
1260 if (!u3d
->clock_gating
) {
1261 clk_enable(u3d
->clk
);
1262 if (pdata
->phy_init
)
1263 pdata
->phy_init(u3d
->phy_regs
);
1266 /* hook up the driver ... */
1267 driver
->driver
.bus
= NULL
;
1268 u3d
->driver
= driver
;
1270 u3d
->ep0_dir
= USB_DIR_OUT
;
1272 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1274 u3d
->vbus_valid_detect
= 1;
1279 static int mv_u3d_stop(struct usb_gadget
*g
,
1280 struct usb_gadget_driver
*driver
)
1282 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1283 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1284 unsigned long flags
;
1286 u3d
->vbus_valid_detect
= 0;
1287 spin_lock_irqsave(&u3d
->lock
, flags
);
1289 /* enable clock to access controller register */
1290 clk_enable(u3d
->clk
);
1291 if (pdata
->phy_init
)
1292 pdata
->phy_init(u3d
->phy_regs
);
1294 mv_u3d_controller_stop(u3d
);
1295 /* stop all usb activities */
1296 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1297 mv_u3d_stop_activity(u3d
, driver
);
1298 mv_u3d_disable(u3d
);
1300 if (pdata
->phy_deinit
)
1301 pdata
->phy_deinit(u3d
->phy_regs
);
1302 clk_disable(u3d
->clk
);
1304 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1311 /* device controller usb_gadget_ops structure */
1312 static const struct usb_gadget_ops mv_u3d_ops
= {
1313 /* notify controller that VBUS is powered or not */
1314 .vbus_session
= mv_u3d_vbus_session
,
1316 /* constrain controller's VBUS power usage */
1317 .vbus_draw
= mv_u3d_vbus_draw
,
1319 .pullup
= mv_u3d_pullup
,
1320 .udc_start
= mv_u3d_start
,
1321 .udc_stop
= mv_u3d_stop
,
1324 static int mv_u3d_eps_init(struct mv_u3d
*u3d
)
1326 struct mv_u3d_ep
*ep
;
1330 /* initialize ep0, ep0 in/out use eps[1] */
1333 strncpy(ep
->name
, "ep0", sizeof(ep
->name
));
1334 ep
->ep
.name
= ep
->name
;
1335 ep
->ep
.ops
= &mv_u3d_ep_ops
;
1337 ep
->ep
.maxpacket
= MV_U3D_EP0_MAX_PKT_SIZE
;
1339 ep
->ep
.desc
= &mv_u3d_ep0_desc
;
1340 INIT_LIST_HEAD(&ep
->queue
);
1341 INIT_LIST_HEAD(&ep
->req_list
);
1342 ep
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
1344 /* add ep0 ep_context */
1345 ep
->ep_context
= &u3d
->ep_context
[1];
1347 /* initialize other endpoints */
1348 for (i
= 2; i
< u3d
->max_eps
* 2; i
++) {
1351 snprintf(name
, sizeof(name
), "ep%din", i
>> 1);
1352 ep
->direction
= MV_U3D_EP_DIR_IN
;
1354 snprintf(name
, sizeof(name
), "ep%dout", i
>> 1);
1355 ep
->direction
= MV_U3D_EP_DIR_OUT
;
1358 strncpy(ep
->name
, name
, sizeof(ep
->name
));
1359 ep
->ep
.name
= ep
->name
;
1361 ep
->ep
.ops
= &mv_u3d_ep_ops
;
1362 ep
->ep
.maxpacket
= (unsigned short) ~0;
1365 INIT_LIST_HEAD(&ep
->queue
);
1366 list_add_tail(&ep
->ep
.ep_list
, &u3d
->gadget
.ep_list
);
1368 INIT_LIST_HEAD(&ep
->req_list
);
1369 spin_lock_init(&ep
->req_lock
);
1370 ep
->ep_context
= &u3d
->ep_context
[i
];
1376 /* delete all endpoint requests, called with spinlock held */
1377 static void mv_u3d_nuke(struct mv_u3d_ep
*ep
, int status
)
1379 /* endpoint fifo flush */
1380 mv_u3d_ep_fifo_flush(&ep
->ep
);
1382 while (!list_empty(&ep
->queue
)) {
1383 struct mv_u3d_req
*req
= NULL
;
1384 req
= list_entry(ep
->queue
.next
, struct mv_u3d_req
, queue
);
1385 mv_u3d_done(ep
, req
, status
);
1389 /* stop all USB activities */
1391 void mv_u3d_stop_activity(struct mv_u3d
*u3d
, struct usb_gadget_driver
*driver
)
1393 struct mv_u3d_ep
*ep
;
1395 mv_u3d_nuke(&u3d
->eps
[1], -ESHUTDOWN
);
1397 list_for_each_entry(ep
, &u3d
->gadget
.ep_list
, ep
.ep_list
) {
1398 mv_u3d_nuke(ep
, -ESHUTDOWN
);
1401 /* report disconnect; the driver is already quiesced */
1403 spin_unlock(&u3d
->lock
);
1404 driver
->disconnect(&u3d
->gadget
);
1405 spin_lock(&u3d
->lock
);
1409 static void mv_u3d_irq_process_error(struct mv_u3d
*u3d
)
1411 /* Increment the error count */
1413 dev_err(u3d
->dev
, "%s\n", __func__
);
1416 static void mv_u3d_irq_process_link_change(struct mv_u3d
*u3d
)
1420 linkchange
= ioread32(&u3d
->vuc_regs
->linkchange
);
1421 iowrite32(linkchange
, &u3d
->vuc_regs
->linkchange
);
1423 dev_dbg(u3d
->dev
, "linkchange: 0x%x\n", linkchange
);
1425 if (linkchange
& MV_U3D_LINK_CHANGE_LINK_UP
) {
1426 dev_dbg(u3d
->dev
, "link up: ltssm state: 0x%x\n",
1427 ioread32(&u3d
->vuc_regs
->ltssmstate
));
1429 u3d
->usb_state
= USB_STATE_DEFAULT
;
1430 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1431 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
1434 u3d
->gadget
.speed
= USB_SPEED_SUPER
;
1437 if (linkchange
& MV_U3D_LINK_CHANGE_SUSPEND
) {
1438 dev_dbg(u3d
->dev
, "link suspend\n");
1439 u3d
->resume_state
= u3d
->usb_state
;
1440 u3d
->usb_state
= USB_STATE_SUSPENDED
;
1443 if (linkchange
& MV_U3D_LINK_CHANGE_RESUME
) {
1444 dev_dbg(u3d
->dev
, "link resume\n");
1445 u3d
->usb_state
= u3d
->resume_state
;
1446 u3d
->resume_state
= 0;
1449 if (linkchange
& MV_U3D_LINK_CHANGE_WRESET
) {
1450 dev_dbg(u3d
->dev
, "warm reset\n");
1451 u3d
->usb_state
= USB_STATE_POWERED
;
1454 if (linkchange
& MV_U3D_LINK_CHANGE_HRESET
) {
1455 dev_dbg(u3d
->dev
, "hot reset\n");
1456 u3d
->usb_state
= USB_STATE_DEFAULT
;
1459 if (linkchange
& MV_U3D_LINK_CHANGE_INACT
)
1460 dev_dbg(u3d
->dev
, "inactive\n");
1462 if (linkchange
& MV_U3D_LINK_CHANGE_DISABLE_AFTER_U0
)
1463 dev_dbg(u3d
->dev
, "ss.disabled\n");
1465 if (linkchange
& MV_U3D_LINK_CHANGE_VBUS_INVALID
) {
1466 dev_dbg(u3d
->dev
, "vbus invalid\n");
1467 u3d
->usb_state
= USB_STATE_ATTACHED
;
1468 u3d
->vbus_valid_detect
= 1;
1469 /* if external vbus detect is not supported,
1470 * we handle it here.
1473 spin_unlock(&u3d
->lock
);
1474 mv_u3d_vbus_session(&u3d
->gadget
, 0);
1475 spin_lock(&u3d
->lock
);
1480 static void mv_u3d_ch9setaddress(struct mv_u3d
*u3d
,
1481 struct usb_ctrlrequest
*setup
)
1485 if (u3d
->usb_state
!= USB_STATE_DEFAULT
) {
1487 "%s, cannot setaddr in this state (%d)\n",
1488 __func__
, u3d
->usb_state
);
1492 u3d
->dev_addr
= (u8
)setup
->wValue
;
1494 dev_dbg(u3d
->dev
, "%s: 0x%x\n", __func__
, u3d
->dev_addr
);
1496 if (u3d
->dev_addr
> 127) {
1498 "%s, u3d address is wrong (out of range)\n", __func__
);
1503 /* update usb state */
1504 u3d
->usb_state
= USB_STATE_ADDRESS
;
1506 /* set the new address */
1507 tmp
= ioread32(&u3d
->vuc_regs
->devaddrtiebrkr
);
1509 tmp
|= (u32
)u3d
->dev_addr
;
1510 iowrite32(tmp
, &u3d
->vuc_regs
->devaddrtiebrkr
);
1514 mv_u3d_ep0_stall(u3d
);
1517 static int mv_u3d_is_set_configuration(struct usb_ctrlrequest
*setup
)
1519 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
1520 if (setup
->bRequest
== USB_REQ_SET_CONFIGURATION
)
1526 static void mv_u3d_handle_setup_packet(struct mv_u3d
*u3d
, u8 ep_num
,
1527 struct usb_ctrlrequest
*setup
)
1528 __releases(&u3c
->lock
)
1529 __acquires(&u3c
->lock
)
1531 bool delegate
= false;
1533 mv_u3d_nuke(&u3d
->eps
[ep_num
* 2 + MV_U3D_EP_DIR_IN
], -ESHUTDOWN
);
1535 dev_dbg(u3d
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1536 setup
->bRequestType
, setup
->bRequest
,
1537 setup
->wValue
, setup
->wIndex
, setup
->wLength
);
1539 /* We process some stardard setup requests here */
1540 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1541 switch (setup
->bRequest
) {
1542 case USB_REQ_GET_STATUS
:
1546 case USB_REQ_SET_ADDRESS
:
1547 mv_u3d_ch9setaddress(u3d
, setup
);
1550 case USB_REQ_CLEAR_FEATURE
:
1554 case USB_REQ_SET_FEATURE
:
1564 /* delegate USB standard requests to the gadget driver */
1565 if (delegate
== true) {
1566 /* USB requests handled by gadget */
1567 if (setup
->wLength
) {
1568 /* DATA phase from gadget, STATUS phase from u3d */
1569 u3d
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1570 ? MV_U3D_EP_DIR_IN
: MV_U3D_EP_DIR_OUT
;
1571 spin_unlock(&u3d
->lock
);
1572 if (u3d
->driver
->setup(&u3d
->gadget
,
1573 &u3d
->local_setup_buff
) < 0) {
1574 dev_err(u3d
->dev
, "setup error!\n");
1575 mv_u3d_ep0_stall(u3d
);
1577 spin_lock(&u3d
->lock
);
1579 /* no DATA phase, STATUS phase from gadget */
1580 u3d
->ep0_dir
= MV_U3D_EP_DIR_IN
;
1581 u3d
->ep0_state
= MV_U3D_STATUS_STAGE
;
1582 spin_unlock(&u3d
->lock
);
1583 if (u3d
->driver
->setup(&u3d
->gadget
,
1584 &u3d
->local_setup_buff
) < 0)
1585 mv_u3d_ep0_stall(u3d
);
1586 spin_lock(&u3d
->lock
);
1589 if (mv_u3d_is_set_configuration(setup
)) {
1590 dev_dbg(u3d
->dev
, "u3d configured\n");
1591 u3d
->usb_state
= USB_STATE_CONFIGURED
;
1596 static void mv_u3d_get_setup_data(struct mv_u3d
*u3d
, u8 ep_num
, u8
*buffer_ptr
)
1598 struct mv_u3d_ep_context
*epcontext
;
1600 epcontext
= &u3d
->ep_context
[ep_num
* 2 + MV_U3D_EP_DIR_IN
];
1602 /* Copy the setup packet to local buffer */
1603 memcpy(buffer_ptr
, (u8
*) &epcontext
->setup_buffer
, 8);
1606 static void mv_u3d_irq_process_setup(struct mv_u3d
*u3d
)
1609 /* Process all Setup packet received interrupts */
1610 tmp
= ioread32(&u3d
->vuc_regs
->setuplock
);
1612 for (i
= 0; i
< u3d
->max_eps
; i
++) {
1613 if (tmp
& (1 << i
)) {
1614 mv_u3d_get_setup_data(u3d
, i
,
1615 (u8
*)(&u3d
->local_setup_buff
));
1616 mv_u3d_handle_setup_packet(u3d
, i
,
1617 &u3d
->local_setup_buff
);
1622 iowrite32(tmp
, &u3d
->vuc_regs
->setuplock
);
1625 static void mv_u3d_irq_process_tr_complete(struct mv_u3d
*u3d
)
1628 int i
, ep_num
= 0, direction
= 0;
1629 struct mv_u3d_ep
*curr_ep
;
1630 struct mv_u3d_req
*curr_req
, *temp_req
;
1633 tmp
= ioread32(&u3d
->vuc_regs
->endcomplete
);
1635 dev_dbg(u3d
->dev
, "tr_complete: ep: 0x%x\n", tmp
);
1638 iowrite32(tmp
, &u3d
->vuc_regs
->endcomplete
);
1640 for (i
= 0; i
< u3d
->max_eps
* 2; i
++) {
1644 bit_pos
= 1 << (ep_num
+ 16 * direction
);
1646 if (!(bit_pos
& tmp
))
1650 curr_ep
= &u3d
->eps
[1];
1652 curr_ep
= &u3d
->eps
[i
];
1654 /* remove req out of ep request list after completion */
1655 dev_dbg(u3d
->dev
, "tr comp: check req_list\n");
1656 spin_lock(&curr_ep
->req_lock
);
1657 if (!list_empty(&curr_ep
->req_list
)) {
1658 struct mv_u3d_req
*req
;
1659 req
= list_entry(curr_ep
->req_list
.next
,
1660 struct mv_u3d_req
, list
);
1661 list_del_init(&req
->list
);
1662 curr_ep
->processing
= 0;
1664 spin_unlock(&curr_ep
->req_lock
);
1666 /* process the req queue until an uncomplete request */
1667 list_for_each_entry_safe(curr_req
, temp_req
,
1668 &curr_ep
->queue
, queue
) {
1669 status
= mv_u3d_process_ep_req(u3d
, i
, curr_req
);
1672 /* write back status to req */
1673 curr_req
->req
.status
= status
;
1675 /* ep0 request completion */
1677 mv_u3d_done(curr_ep
, curr_req
, 0);
1680 mv_u3d_done(curr_ep
, curr_req
, status
);
1684 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from ep complete\n");
1685 mv_u3d_start_queue(curr_ep
);
1689 static irqreturn_t
mv_u3d_irq(int irq
, void *dev
)
1691 struct mv_u3d
*u3d
= (struct mv_u3d
*)dev
;
1696 spin_lock(&u3d
->lock
);
1698 status
= ioread32(&u3d
->vuc_regs
->intrcause
);
1699 intr
= ioread32(&u3d
->vuc_regs
->intrenable
);
1703 spin_unlock(&u3d
->lock
);
1704 dev_err(u3d
->dev
, "irq error!\n");
1708 if (status
& MV_U3D_USBINT_VBUS_VALID
) {
1709 bridgesetting
= ioread32(&u3d
->vuc_regs
->bridgesetting
);
1710 if (bridgesetting
& MV_U3D_BRIDGE_SETTING_VBUS_VALID
) {
1711 /* write vbus valid bit of bridge setting to clear */
1712 bridgesetting
= MV_U3D_BRIDGE_SETTING_VBUS_VALID
;
1713 iowrite32(bridgesetting
, &u3d
->vuc_regs
->bridgesetting
);
1714 dev_dbg(u3d
->dev
, "vbus valid\n");
1716 u3d
->usb_state
= USB_STATE_POWERED
;
1717 u3d
->vbus_valid_detect
= 0;
1718 /* if external vbus detect is not supported,
1719 * we handle it here.
1722 spin_unlock(&u3d
->lock
);
1723 mv_u3d_vbus_session(&u3d
->gadget
, 1);
1724 spin_lock(&u3d
->lock
);
1727 dev_err(u3d
->dev
, "vbus bit is not set\n");
1730 /* RX data is already in the 16KB FIFO.*/
1731 if (status
& MV_U3D_USBINT_UNDER_RUN
) {
1732 trbunderrun
= ioread32(&u3d
->vuc_regs
->trbunderrun
);
1733 dev_err(u3d
->dev
, "under run, ep%d\n", trbunderrun
);
1734 iowrite32(trbunderrun
, &u3d
->vuc_regs
->trbunderrun
);
1735 mv_u3d_irq_process_error(u3d
);
1738 if (status
& (MV_U3D_USBINT_RXDESC_ERR
| MV_U3D_USBINT_TXDESC_ERR
)) {
1739 /* write one to clear */
1740 iowrite32(status
& (MV_U3D_USBINT_RXDESC_ERR
1741 | MV_U3D_USBINT_TXDESC_ERR
),
1742 &u3d
->vuc_regs
->intrcause
);
1743 dev_err(u3d
->dev
, "desc err 0x%x\n", status
);
1744 mv_u3d_irq_process_error(u3d
);
1747 if (status
& MV_U3D_USBINT_LINK_CHG
)
1748 mv_u3d_irq_process_link_change(u3d
);
1750 if (status
& MV_U3D_USBINT_TX_COMPLETE
)
1751 mv_u3d_irq_process_tr_complete(u3d
);
1753 if (status
& MV_U3D_USBINT_RX_COMPLETE
)
1754 mv_u3d_irq_process_tr_complete(u3d
);
1756 if (status
& MV_U3D_USBINT_SETUP
)
1757 mv_u3d_irq_process_setup(u3d
);
1759 spin_unlock(&u3d
->lock
);
1763 static int mv_u3d_remove(struct platform_device
*dev
)
1765 struct mv_u3d
*u3d
= platform_get_drvdata(dev
);
1767 BUG_ON(u3d
== NULL
);
1769 usb_del_gadget_udc(&u3d
->gadget
);
1771 /* free memory allocated in probe */
1773 dma_pool_destroy(u3d
->trb_pool
);
1775 if (u3d
->ep_context
)
1776 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
1777 u3d
->ep_context
, u3d
->ep_context_dma
);
1782 free_irq(u3d
->irq
, u3d
);
1785 iounmap(u3d
->cap_regs
);
1786 u3d
->cap_regs
= NULL
;
1788 kfree(u3d
->status_req
);
1797 static int mv_u3d_probe(struct platform_device
*dev
)
1799 struct mv_u3d
*u3d
= NULL
;
1800 struct mv_usb_platform_data
*pdata
= dev_get_platdata(&dev
->dev
);
1805 if (!dev_get_platdata(&dev
->dev
)) {
1806 dev_err(&dev
->dev
, "missing platform_data\n");
1811 u3d
= kzalloc(sizeof(*u3d
), GFP_KERNEL
);
1813 dev_err(&dev
->dev
, "failed to allocate memory for u3d\n");
1815 goto err_alloc_private
;
1818 spin_lock_init(&u3d
->lock
);
1820 platform_set_drvdata(dev
, u3d
);
1822 u3d
->dev
= &dev
->dev
;
1823 u3d
->vbus
= pdata
->vbus
;
1825 u3d
->clk
= clk_get(&dev
->dev
, NULL
);
1826 if (IS_ERR(u3d
->clk
)) {
1827 retval
= PTR_ERR(u3d
->clk
);
1831 r
= platform_get_resource_byname(dev
, IORESOURCE_MEM
, "capregs");
1833 dev_err(&dev
->dev
, "no I/O memory resource defined\n");
1835 goto err_get_cap_regs
;
1838 u3d
->cap_regs
= (struct mv_u3d_cap_regs __iomem
*)
1839 ioremap(r
->start
, resource_size(r
));
1840 if (!u3d
->cap_regs
) {
1841 dev_err(&dev
->dev
, "failed to map I/O memory\n");
1843 goto err_map_cap_regs
;
1845 dev_dbg(&dev
->dev
, "cap_regs address: 0x%lx/0x%lx\n",
1846 (unsigned long) r
->start
,
1847 (unsigned long) u3d
->cap_regs
);
1850 /* we will access controller register, so enable the u3d controller */
1851 clk_enable(u3d
->clk
);
1853 if (pdata
->phy_init
) {
1854 retval
= pdata
->phy_init(u3d
->phy_regs
);
1856 dev_err(&dev
->dev
, "init phy error %d\n", retval
);
1857 goto err_u3d_enable
;
1861 u3d
->op_regs
= (struct mv_u3d_op_regs __iomem
*)(u3d
->cap_regs
1862 + MV_U3D_USB3_OP_REGS_OFFSET
);
1864 u3d
->vuc_regs
= (struct mv_u3d_vuc_regs __iomem
*)(u3d
->cap_regs
1865 + ioread32(&u3d
->cap_regs
->vuoff
));
1870 * some platform will use usb to download image, it may not disconnect
1871 * usb gadget before loading kernel. So first stop u3d here.
1873 mv_u3d_controller_stop(u3d
);
1874 iowrite32(0xFFFFFFFF, &u3d
->vuc_regs
->intrcause
);
1876 if (pdata
->phy_deinit
)
1877 pdata
->phy_deinit(u3d
->phy_regs
);
1878 clk_disable(u3d
->clk
);
1880 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep_context
) * 2;
1881 size
= (size
+ MV_U3D_EP_CONTEXT_ALIGNMENT
- 1)
1882 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT
- 1);
1883 u3d
->ep_context
= dma_alloc_coherent(&dev
->dev
, size
,
1884 &u3d
->ep_context_dma
, GFP_KERNEL
);
1885 if (!u3d
->ep_context
) {
1886 dev_err(&dev
->dev
, "allocate ep context memory failed\n");
1888 goto err_alloc_ep_context
;
1890 u3d
->ep_context_size
= size
;
1892 /* create TRB dma_pool resource */
1893 u3d
->trb_pool
= dma_pool_create("u3d_trb",
1895 sizeof(struct mv_u3d_trb_hw
),
1896 MV_U3D_TRB_ALIGNMENT
,
1897 MV_U3D_DMA_BOUNDARY
);
1899 if (!u3d
->trb_pool
) {
1901 goto err_alloc_trb_pool
;
1904 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep
) * 2;
1905 u3d
->eps
= kzalloc(size
, GFP_KERNEL
);
1907 dev_err(&dev
->dev
, "allocate ep memory failed\n");
1912 /* initialize ep0 status request structure */
1913 u3d
->status_req
= kzalloc(sizeof(struct mv_u3d_req
) + 8, GFP_KERNEL
);
1914 if (!u3d
->status_req
) {
1915 dev_err(&dev
->dev
, "allocate status_req memory failed\n");
1917 goto err_alloc_status_req
;
1919 INIT_LIST_HEAD(&u3d
->status_req
->queue
);
1921 /* allocate a small amount of memory to get valid address */
1922 u3d
->status_req
->req
.buf
= (char *)u3d
->status_req
1923 + sizeof(struct mv_u3d_req
);
1924 u3d
->status_req
->req
.dma
= virt_to_phys(u3d
->status_req
->req
.buf
);
1926 u3d
->resume_state
= USB_STATE_NOTATTACHED
;
1927 u3d
->usb_state
= USB_STATE_ATTACHED
;
1928 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1929 u3d
->remote_wakeup
= 0;
1931 r
= platform_get_resource(dev
, IORESOURCE_IRQ
, 0);
1933 dev_err(&dev
->dev
, "no IRQ resource defined\n");
1937 u3d
->irq
= r
->start
;
1938 if (request_irq(u3d
->irq
, mv_u3d_irq
,
1939 IRQF_DISABLED
| IRQF_SHARED
, driver_name
, u3d
)) {
1941 dev_err(&dev
->dev
, "Request irq %d for u3d failed\n",
1944 goto err_request_irq
;
1947 /* initialize gadget structure */
1948 u3d
->gadget
.ops
= &mv_u3d_ops
; /* usb_gadget_ops */
1949 u3d
->gadget
.ep0
= &u3d
->eps
[1].ep
; /* gadget ep0 */
1950 INIT_LIST_HEAD(&u3d
->gadget
.ep_list
); /* ep_list */
1951 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
1953 /* the "gadget" abstracts/virtualizes the controller */
1954 u3d
->gadget
.name
= driver_name
; /* gadget name */
1956 mv_u3d_eps_init(u3d
);
1958 /* external vbus detection */
1960 u3d
->clock_gating
= 1;
1961 dev_err(&dev
->dev
, "external vbus detection\n");
1964 if (!u3d
->clock_gating
)
1965 u3d
->vbus_active
= 1;
1967 /* enable usb3 controller vbus detection */
1968 u3d
->vbus_valid_detect
= 1;
1970 retval
= usb_add_gadget_udc(&dev
->dev
, &u3d
->gadget
);
1972 goto err_unregister
;
1974 dev_dbg(&dev
->dev
, "successful probe usb3 device %s clock gating.\n",
1975 u3d
->clock_gating
? "with" : "without");
1980 free_irq(u3d
->irq
, u3d
);
1983 kfree(u3d
->status_req
);
1984 err_alloc_status_req
:
1987 dma_pool_destroy(u3d
->trb_pool
);
1989 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
1990 u3d
->ep_context
, u3d
->ep_context_dma
);
1991 err_alloc_ep_context
:
1992 if (pdata
->phy_deinit
)
1993 pdata
->phy_deinit(u3d
->phy_regs
);
1994 clk_disable(u3d
->clk
);
1996 iounmap(u3d
->cap_regs
);
2007 #ifdef CONFIG_PM_SLEEP
2008 static int mv_u3d_suspend(struct device
*dev
)
2010 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
2013 * only cable is unplugged, usb can suspend.
2014 * So do not care about clock_gating == 1, it is handled by
2017 if (!u3d
->clock_gating
) {
2018 mv_u3d_controller_stop(u3d
);
2020 spin_lock_irq(&u3d
->lock
);
2021 /* stop all usb activities */
2022 mv_u3d_stop_activity(u3d
, u3d
->driver
);
2023 spin_unlock_irq(&u3d
->lock
);
2025 mv_u3d_disable(u3d
);
2031 static int mv_u3d_resume(struct device
*dev
)
2033 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
2036 if (!u3d
->clock_gating
) {
2037 retval
= mv_u3d_enable(u3d
);
2041 if (u3d
->driver
&& u3d
->softconnect
) {
2042 mv_u3d_controller_reset(u3d
);
2043 mv_u3d_ep0_reset(u3d
);
2044 mv_u3d_controller_start(u3d
);
2052 static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops
, mv_u3d_suspend
, mv_u3d_resume
);
2054 static void mv_u3d_shutdown(struct platform_device
*dev
)
2056 struct mv_u3d
*u3d
= platform_get_drvdata(dev
);
2059 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
2060 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
2061 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
2064 static struct platform_driver mv_u3d_driver
= {
2065 .probe
= mv_u3d_probe
,
2066 .remove
= mv_u3d_remove
,
2067 .shutdown
= mv_u3d_shutdown
,
2069 .owner
= THIS_MODULE
,
2071 .pm
= &mv_u3d_pm_ops
,
2075 module_platform_driver(mv_u3d_driver
);
2076 MODULE_ALIAS("platform:mv-u3d");
2077 MODULE_DESCRIPTION(DRIVER_DESC
);
2078 MODULE_AUTHOR("Yu Xu <yuxu@marvell.com>");
2079 MODULE_LICENSE("GPL");