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>
33 #include <asm/system.h>
34 #include <asm/unaligned.h>
35 #include <asm/byteorder.h>
39 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
41 static const char driver_name
[] = "mv_u3d";
42 static const char driver_desc
[] = DRIVER_DESC
;
44 static void mv_u3d_nuke(struct mv_u3d_ep
*ep
, int status
);
45 static void mv_u3d_stop_activity(struct mv_u3d
*u3d
,
46 struct usb_gadget_driver
*driver
);
48 /* for endpoint 0 operations */
49 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc
= {
50 .bLength
= USB_DT_ENDPOINT_SIZE
,
51 .bDescriptorType
= USB_DT_ENDPOINT
,
52 .bEndpointAddress
= 0,
53 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
54 .wMaxPacketSize
= MV_U3D_EP0_MAX_PKT_SIZE
,
57 static void mv_u3d_ep0_reset(struct mv_u3d
*u3d
)
63 for (i
= 0; i
< 2; i
++) {
67 /* ep0 ep context, ep0 in and out share the same ep context */
68 ep
->ep_context
= &u3d
->ep_context
[1];
71 /* reset ep state machine */
73 epxcr
= ioread32(&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_EPXCR_EP_INIT
;
78 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
80 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
81 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
82 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
83 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
84 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
85 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr1
);
88 epxcr
= ioread32(&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_EPXCR_EP_INIT
;
93 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
95 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
96 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
97 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
98 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
99 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
100 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr1
);
103 static void mv_u3d_ep0_stall(struct mv_u3d
*u3d
)
106 dev_dbg(u3d
->dev
, "%s\n", __func__
);
108 /* set TX and RX to stall */
109 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
110 tmp
|= MV_U3D_EPXCR_EP_HALT
;
111 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
113 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
114 tmp
|= MV_U3D_EPXCR_EP_HALT
;
115 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
117 /* update ep0 state */
118 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
119 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
122 static int mv_u3d_process_ep_req(struct mv_u3d
*u3d
, int index
,
123 struct mv_u3d_req
*curr_req
)
125 struct mv_u3d_trb
*curr_trb
;
126 dma_addr_t cur_deq_lo
;
127 struct mv_u3d_ep_context
*curr_ep_context
;
128 int trb_complete
, actual
, remaining_length
;
129 int direction
, ep_num
;
131 u32 tmp
, status
, length
;
133 curr_ep_context
= &u3d
->ep_context
[index
];
134 direction
= index
% 2;
138 actual
= curr_req
->req
.length
;
140 while (!list_empty(&curr_req
->trb_list
)) {
141 curr_trb
= list_entry(curr_req
->trb_list
.next
,
142 struct mv_u3d_trb
, trb_list
);
143 if (!curr_trb
->trb_hw
->ctrl
.own
) {
144 dev_err(u3d
->dev
, "%s, TRB own error!\n",
145 u3d
->eps
[index
].name
);
149 curr_trb
->trb_hw
->ctrl
.own
= 0;
150 if (direction
== MV_U3D_EP_DIR_OUT
) {
151 tmp
= ioread32(&u3d
->vuc_regs
->rxst
[ep_num
].statuslo
);
153 ioread32(&u3d
->vuc_regs
->rxst
[ep_num
].curdeqlo
);
155 tmp
= ioread32(&u3d
->vuc_regs
->txst
[ep_num
].statuslo
);
157 ioread32(&u3d
->vuc_regs
->txst
[ep_num
].curdeqlo
);
160 status
= tmp
>> MV_U3D_XFERSTATUS_COMPLETE_SHIFT
;
161 length
= tmp
& MV_U3D_XFERSTATUS_TRB_LENGTH_MASK
;
163 if (status
== MV_U3D_COMPLETE_SUCCESS
||
164 (status
== MV_U3D_COMPLETE_SHORT_PACKET
&&
165 direction
== MV_U3D_EP_DIR_OUT
)) {
166 remaining_length
+= length
;
167 actual
-= remaining_length
;
170 "complete_tr error: ep=%d %s: error = 0x%x\n",
171 index
>> 1, direction
? "SEND" : "RECV",
176 list_del_init(&curr_trb
->trb_list
);
181 curr_req
->req
.actual
= actual
;
186 * mv_u3d_done() - retire a request; caller blocked irqs
187 * @status : request status to be set, only works when
188 * request is still in progress.
191 void mv_u3d_done(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
, int status
)
193 struct mv_u3d
*u3d
= (struct mv_u3d
*)ep
->u3d
;
195 dev_dbg(u3d
->dev
, "mv_u3d_done: remove req->queue\n");
196 /* Removed the req from ep queue */
197 list_del_init(&req
->queue
);
199 /* req.status should be set as -EINPROGRESS in ep_queue() */
200 if (req
->req
.status
== -EINPROGRESS
)
201 req
->req
.status
= status
;
203 status
= req
->req
.status
;
205 /* Free trb for the request */
207 dma_pool_free(u3d
->trb_pool
,
208 req
->trb_head
->trb_hw
, req
->trb_head
->trb_dma
);
210 dma_unmap_single(ep
->u3d
->gadget
.dev
.parent
,
211 (dma_addr_t
)req
->trb_head
->trb_dma
,
212 req
->trb_count
* sizeof(struct mv_u3d_trb_hw
),
214 kfree(req
->trb_head
->trb_hw
);
216 kfree(req
->trb_head
);
218 usb_gadget_unmap_request(&u3d
->gadget
, &req
->req
, mv_u3d_ep_dir(ep
));
220 if (status
&& (status
!= -ESHUTDOWN
)) {
221 dev_dbg(u3d
->dev
, "complete %s req %p stat %d len %u/%u",
222 ep
->ep
.name
, &req
->req
, status
,
223 req
->req
.actual
, req
->req
.length
);
226 spin_unlock(&ep
->u3d
->lock
);
228 * complete() is from gadget layer,
229 * eg fsg->bulk_in_complete()
231 if (req
->req
.complete
)
232 req
->req
.complete(&ep
->ep
, &req
->req
);
234 spin_lock(&ep
->u3d
->lock
);
237 static int mv_u3d_queue_trb(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
)
241 struct mv_u3d_ep_context
*ep_context
;
245 direction
= mv_u3d_ep_dir(ep
);
247 /* ep0 in and out share the same ep context slot 1*/
249 ep_context
= &(u3d
->ep_context
[1]);
251 ep_context
= &(u3d
->ep_context
[ep
->ep_num
* 2 + direction
]);
253 /* check if the pipe is empty or not */
254 if (!list_empty(&ep
->queue
)) {
255 dev_err(u3d
->dev
, "add trb to non-empty queue!\n");
259 ep_context
->rsvd0
= cpu_to_le32(1);
260 ep_context
->rsvd1
= 0;
262 /* Configure the trb address and set the DCS bit.
263 * Both DCS bit and own bit in trb should be set.
265 ep_context
->trb_addr_lo
=
266 cpu_to_le32(req
->trb_head
->trb_dma
| DCS_ENABLE
);
267 ep_context
->trb_addr_hi
= 0;
269 /* Ensure that updates to the EP Context will
270 * occure before Ring Bell.
274 /* ring bell the ep */
279 + ((direction
== MV_U3D_EP_DIR_OUT
) ? 0 : 1);
281 iowrite32(tmp
, &u3d
->op_regs
->doorbell
);
286 static struct mv_u3d_trb
*mv_u3d_build_trb_one(struct mv_u3d_req
*req
,
287 unsigned *length
, dma_addr_t
*dma
)
290 unsigned int direction
;
291 struct mv_u3d_trb
*trb
;
292 struct mv_u3d_trb_hw
*trb_hw
;
295 /* how big will this transfer be? */
296 *length
= req
->req
.length
- req
->req
.actual
;
297 BUG_ON(*length
> (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
301 trb
= kzalloc(sizeof(*trb
), GFP_ATOMIC
);
303 dev_err(u3d
->dev
, "%s, trb alloc fail\n", __func__
);
308 * Be careful that no _GFP_HIGHMEM is set,
309 * or we can not use dma_to_virt
310 * cannot use GFP_KERNEL in spin lock
312 trb_hw
= dma_pool_alloc(u3d
->trb_pool
, GFP_ATOMIC
, dma
);
315 "%s, dma_pool_alloc fail\n", __func__
);
319 trb
->trb_hw
= trb_hw
;
321 /* initialize buffer page pointers */
322 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
324 trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
325 trb_hw
->buf_addr_hi
= 0;
326 trb_hw
->trb_len
= cpu_to_le32(*length
);
327 trb_hw
->ctrl
.own
= 1;
329 if (req
->ep
->ep_num
== 0)
330 trb_hw
->ctrl
.type
= TYPE_DATA
;
332 trb_hw
->ctrl
.type
= TYPE_NORMAL
;
334 req
->req
.actual
+= *length
;
336 direction
= mv_u3d_ep_dir(req
->ep
);
337 if (direction
== MV_U3D_EP_DIR_IN
)
338 trb_hw
->ctrl
.dir
= 1;
340 trb_hw
->ctrl
.dir
= 0;
342 /* Enable interrupt for the last trb of a request */
343 if (!req
->req
.no_interrupt
)
344 trb_hw
->ctrl
.ioc
= 1;
346 trb_hw
->ctrl
.chain
= 0;
352 static int mv_u3d_build_trb_chain(struct mv_u3d_req
*req
, unsigned *length
,
353 struct mv_u3d_trb
*trb
, int *is_last
)
356 unsigned int direction
;
359 /* how big will this transfer be? */
360 *length
= min(req
->req
.length
- req
->req
.actual
,
361 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
367 /* initialize buffer page pointers */
368 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
370 trb
->trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
371 trb
->trb_hw
->buf_addr_hi
= 0;
372 trb
->trb_hw
->trb_len
= cpu_to_le32(*length
);
373 trb
->trb_hw
->ctrl
.own
= 1;
375 if (req
->ep
->ep_num
== 0)
376 trb
->trb_hw
->ctrl
.type
= TYPE_DATA
;
378 trb
->trb_hw
->ctrl
.type
= TYPE_NORMAL
;
380 req
->req
.actual
+= *length
;
382 direction
= mv_u3d_ep_dir(req
->ep
);
383 if (direction
== MV_U3D_EP_DIR_IN
)
384 trb
->trb_hw
->ctrl
.dir
= 1;
386 trb
->trb_hw
->ctrl
.dir
= 0;
388 /* zlp is needed if req->req.zero is set */
390 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
394 } else if (req
->req
.length
== req
->req
.actual
)
399 /* Enable interrupt for the last trb of a request */
400 if (*is_last
&& !req
->req
.no_interrupt
)
401 trb
->trb_hw
->ctrl
.ioc
= 1;
404 trb
->trb_hw
->ctrl
.chain
= 0;
406 trb
->trb_hw
->ctrl
.chain
= 1;
407 dev_dbg(u3d
->dev
, "chain trb\n");
415 /* generate TRB linked list for a request
416 * usb controller only supports continous trb chain,
417 * that trb structure physical address should be continous.
419 static int mv_u3d_req_to_trb(struct mv_u3d_req
*req
)
423 struct mv_u3d_trb
*trb
;
424 struct mv_u3d_trb_hw
*trb_hw
;
432 INIT_LIST_HEAD(&req
->trb_list
);
434 length
= req
->req
.length
- req
->req
.actual
;
435 /* normally the request transfer length is less than 16KB.
436 * we use buil_trb_one() to optimize it.
438 if (length
<= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
) {
439 trb
= mv_u3d_build_trb_one(req
, &count
, &dma
);
440 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
445 trb_num
= length
/ MV_U3D_EP_MAX_LENGTH_TRANSFER
;
446 if (length
% MV_U3D_EP_MAX_LENGTH_TRANSFER
)
449 trb
= kcalloc(trb_num
, sizeof(*trb
), GFP_ATOMIC
);
452 "%s, trb alloc fail\n", __func__
);
456 trb_hw
= kcalloc(trb_num
, sizeof(*trb_hw
), GFP_ATOMIC
);
459 "%s, trb_hw alloc fail\n", __func__
);
464 trb
->trb_hw
= trb_hw
;
465 if (mv_u3d_build_trb_chain(req
, &count
,
468 "%s, mv_u3d_build_trb_chain fail\n",
473 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
479 req
->trb_head
= list_entry(req
->trb_list
.next
,
480 struct mv_u3d_trb
, trb_list
);
481 req
->trb_head
->trb_dma
= dma_map_single(u3d
->gadget
.dev
.parent
,
482 req
->trb_head
->trb_hw
,
483 trb_num
* sizeof(*trb_hw
),
493 mv_u3d_start_queue(struct mv_u3d_ep
*ep
)
495 struct mv_u3d
*u3d
= ep
->u3d
;
496 struct mv_u3d_req
*req
;
499 if (!list_empty(&ep
->req_list
) && !ep
->processing
)
500 req
= list_entry(ep
->req_list
.next
, struct mv_u3d_req
, list
);
506 /* set up dma mapping */
507 ret
= usb_gadget_map_request(&u3d
->gadget
, &req
->req
,
512 req
->req
.status
= -EINPROGRESS
;
516 /* build trbs and push them to device queue */
517 if (!mv_u3d_req_to_trb(req
)) {
518 ret
= mv_u3d_queue_trb(ep
, req
);
525 dev_err(u3d
->dev
, "%s, mv_u3d_req_to_trb fail\n", __func__
);
529 /* irq handler advances the queue */
531 list_add_tail(&req
->queue
, &ep
->queue
);
536 static int mv_u3d_ep_enable(struct usb_ep
*_ep
,
537 const struct usb_endpoint_descriptor
*desc
)
540 struct mv_u3d_ep
*ep
;
541 struct mv_u3d_ep_context
*ep_context
;
543 unsigned maxburst
= 0;
544 u32 epxcr
, direction
;
546 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
549 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
552 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
)
555 direction
= mv_u3d_ep_dir(ep
);
556 max
= le16_to_cpu(desc
->wMaxPacketSize
);
560 maxburst
= _ep
->maxburst
;
562 /* Get the endpoint context address */
563 ep_context
= (struct mv_u3d_ep_context
*)ep
->ep_context
;
565 /* Set the max burst size */
566 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
567 case USB_ENDPOINT_XFER_BULK
:
570 "max burst should not be greater "
571 "than 16 on bulk ep\n");
573 _ep
->maxburst
= maxburst
;
576 "maxburst: %d on bulk %s\n", maxburst
, ep
->name
);
578 case USB_ENDPOINT_XFER_CONTROL
:
579 /* control transfer only supports maxburst as one */
581 _ep
->maxburst
= maxburst
;
583 case USB_ENDPOINT_XFER_INT
:
586 "max burst should be 1 on int ep "
587 "if transfer size is not 1024\n");
589 _ep
->maxburst
= maxburst
;
592 case USB_ENDPOINT_XFER_ISOC
:
595 "max burst should be 1 on isoc ep "
596 "if transfer size is not 1024\n");
598 _ep
->maxburst
= maxburst
;
605 ep
->ep
.maxpacket
= max
;
609 /* Enable the endpoint for Rx or Tx and set the endpoint type */
610 if (direction
== MV_U3D_EP_DIR_OUT
) {
611 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
612 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
613 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
615 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
616 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
618 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
619 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
620 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
621 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
622 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
624 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
625 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
626 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
628 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
629 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
631 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
632 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
633 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
634 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
635 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
643 static int mv_u3d_ep_disable(struct usb_ep
*_ep
)
646 struct mv_u3d_ep
*ep
;
647 struct mv_u3d_ep_context
*ep_context
;
648 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 mv_u3d_nuke(ep
, -ESHUTDOWN
);
667 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
668 if (direction
== MV_U3D_EP_DIR_OUT
) {
669 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
670 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
671 | USB_ENDPOINT_XFERTYPE_MASK
);
672 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
674 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
675 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
676 | USB_ENDPOINT_XFERTYPE_MASK
);
677 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
686 static struct usb_request
*
687 mv_u3d_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
689 struct mv_u3d_req
*req
= NULL
;
691 req
= kzalloc(sizeof *req
, gfp_flags
);
695 INIT_LIST_HEAD(&req
->queue
);
700 static void mv_u3d_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
702 struct mv_u3d_req
*req
= container_of(_req
, struct mv_u3d_req
, req
);
707 static void mv_u3d_ep_fifo_flush(struct usb_ep
*_ep
)
711 struct mv_u3d_ep
*ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
715 /* if endpoint is not enabled, cannot flush endpoint */
720 direction
= mv_u3d_ep_dir(ep
);
722 /* ep0 need clear bit after flushing fifo. */
724 if (direction
== MV_U3D_EP_DIR_OUT
) {
725 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
726 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
727 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
729 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
730 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
732 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
733 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
734 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
736 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
737 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
742 if (direction
== MV_U3D_EP_DIR_OUT
) {
743 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
744 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
745 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
747 /* Wait until flushing completed */
748 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
749 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
) &
750 MV_U3D_EPXCR_EP_FLUSH
) {
752 * EP_FLUSH bit should be cleared to indicate this
753 * operation is complete
757 "EP FLUSH TIMEOUT for ep%d%s\n", ep
->ep_num
,
758 direction
? "in" : "out");
764 } else { /* EP_DIR_IN */
765 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
766 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
767 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
769 /* Wait until flushing completed */
770 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
771 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
) &
772 MV_U3D_EPXCR_EP_FLUSH
) {
774 * EP_FLUSH bit should be cleared to indicate this
775 * operation is complete
779 "EP FLUSH TIMEOUT for ep%d%s\n", ep
->ep_num
,
780 direction
? "in" : "out");
789 /* queues (submits) an I/O request to an endpoint */
791 mv_u3d_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
793 struct mv_u3d_ep
*ep
;
794 struct mv_u3d_req
*req
;
797 int is_first_req
= 0;
799 if (unlikely(!_ep
|| !_req
))
802 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
805 req
= container_of(_req
, struct mv_u3d_req
, req
);
808 && u3d
->ep0_state
== MV_U3D_STATUS_STAGE
810 dev_dbg(u3d
->dev
, "ep0 status stage\n");
811 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
815 dev_dbg(u3d
->dev
, "%s: %s, req: 0x%x\n",
816 __func__
, _ep
->name
, (u32
)req
);
818 /* catch various bogus parameters */
819 if (!req
->req
.complete
|| !req
->req
.buf
820 || !list_empty(&req
->queue
)) {
822 "%s, bad params, _req: 0x%x,"
823 "req->req.complete: 0x%x, req->req.buf: 0x%x,"
824 "list_empty: 0x%x\n",
826 (u32
)req
->req
.complete
, (u32
)req
->req
.buf
,
827 (u32
)list_empty(&req
->queue
));
830 if (unlikely(!ep
->ep
.desc
)) {
831 dev_err(u3d
->dev
, "%s, bad ep\n", __func__
);
834 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
835 if (req
->req
.length
> ep
->ep
.maxpacket
)
839 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
841 "bad params of driver/speed\n");
847 /* Software list handles usb request. */
848 spin_lock_irqsave(&ep
->req_lock
, flags
);
849 is_first_req
= list_empty(&ep
->req_list
);
850 list_add_tail(&req
->list
, &ep
->req_list
);
851 spin_unlock_irqrestore(&ep
->req_lock
, flags
);
853 dev_dbg(u3d
->dev
, "list is not empty\n");
857 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from usb_ep_queue\n");
858 spin_lock_irqsave(&u3d
->lock
, flags
);
859 mv_u3d_start_queue(ep
);
860 spin_unlock_irqrestore(&u3d
->lock
, flags
);
864 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
865 static int mv_u3d_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
867 struct mv_u3d_ep
*ep
;
868 struct mv_u3d_req
*req
;
870 struct mv_u3d_ep_context
*ep_context
;
871 struct mv_u3d_req
*next_req
;
879 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
882 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
884 /* make sure it's actually queued on this endpoint */
885 list_for_each_entry(req
, &ep
->queue
, queue
) {
886 if (&req
->req
== _req
)
889 if (&req
->req
!= _req
) {
894 /* The request is in progress, or completed but not dequeued */
895 if (ep
->queue
.next
== &req
->queue
) {
896 _req
->status
= -ECONNRESET
;
897 mv_u3d_ep_fifo_flush(_ep
);
899 /* The request isn't the last request in this ep queue */
900 if (req
->queue
.next
!= &ep
->queue
) {
902 "it is the last request in this ep queue\n");
903 ep_context
= ep
->ep_context
;
904 next_req
= list_entry(req
->queue
.next
,
905 struct mv_u3d_req
, queue
);
907 /* Point first TRB of next request to the EP context. */
908 iowrite32((u32
) next_req
->trb_head
,
909 &ep_context
->trb_addr_lo
);
911 struct mv_u3d_ep_context
*ep_context
;
912 ep_context
= ep
->ep_context
;
913 ep_context
->trb_addr_lo
= 0;
914 ep_context
->trb_addr_hi
= 0;
920 mv_u3d_done(ep
, req
, -ECONNRESET
);
922 /* remove the req from the ep req list */
923 if (!list_empty(&ep
->req_list
)) {
924 struct mv_u3d_req
*curr_req
;
925 curr_req
= list_entry(ep
->req_list
.next
,
926 struct mv_u3d_req
, list
);
927 if (curr_req
== req
) {
928 list_del_init(&req
->list
);
934 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
939 mv_u3d_ep_set_stall(struct mv_u3d
*u3d
, u8 ep_num
, u8 direction
, int stall
)
942 struct mv_u3d_ep
*ep
= u3d
->eps
;
944 dev_dbg(u3d
->dev
, "%s\n", __func__
);
945 if (direction
== MV_U3D_EP_DIR_OUT
) {
946 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
948 tmp
|= MV_U3D_EPXCR_EP_HALT
;
950 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
951 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
953 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
955 tmp
|= MV_U3D_EPXCR_EP_HALT
;
957 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
958 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
962 static int mv_u3d_ep_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
964 struct mv_u3d_ep
*ep
;
965 unsigned long flags
= 0;
969 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
976 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
977 status
= -EOPNOTSUPP
;
982 * Attempt to halt IN ep will fail if any transfer requests
985 if (halt
&& (mv_u3d_ep_dir(ep
) == MV_U3D_EP_DIR_IN
)
986 && !list_empty(&ep
->queue
)) {
991 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
992 mv_u3d_ep_set_stall(u3d
, ep
->ep_num
, mv_u3d_ep_dir(ep
), halt
);
997 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
1000 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1005 static int mv_u3d_ep_set_halt(struct usb_ep
*_ep
, int halt
)
1007 return mv_u3d_ep_set_halt_wedge(_ep
, halt
, 0);
1010 static int mv_u3d_ep_set_wedge(struct usb_ep
*_ep
)
1012 return mv_u3d_ep_set_halt_wedge(_ep
, 1, 1);
1015 static struct usb_ep_ops mv_u3d_ep_ops
= {
1016 .enable
= mv_u3d_ep_enable
,
1017 .disable
= mv_u3d_ep_disable
,
1019 .alloc_request
= mv_u3d_alloc_request
,
1020 .free_request
= mv_u3d_free_request
,
1022 .queue
= mv_u3d_ep_queue
,
1023 .dequeue
= mv_u3d_ep_dequeue
,
1025 .set_wedge
= mv_u3d_ep_set_wedge
,
1026 .set_halt
= mv_u3d_ep_set_halt
,
1027 .fifo_flush
= mv_u3d_ep_fifo_flush
,
1030 static void mv_u3d_controller_stop(struct mv_u3d
*u3d
)
1034 if (!u3d
->clock_gating
&& u3d
->vbus_valid_detect
)
1035 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID
,
1036 &u3d
->vuc_regs
->intrenable
);
1038 iowrite32(0, &u3d
->vuc_regs
->intrenable
);
1039 iowrite32(~0x0, &u3d
->vuc_regs
->endcomplete
);
1040 iowrite32(~0x0, &u3d
->vuc_regs
->trbunderrun
);
1041 iowrite32(~0x0, &u3d
->vuc_regs
->trbcomplete
);
1042 iowrite32(~0x0, &u3d
->vuc_regs
->linkchange
);
1043 iowrite32(0x1, &u3d
->vuc_regs
->setuplock
);
1045 /* Reset the RUN bit in the command register to stop USB */
1046 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1047 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1048 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1049 dev_dbg(u3d
->dev
, "after u3d_stop, USBCMD 0x%x\n",
1050 ioread32(&u3d
->op_regs
->usbcmd
));
1053 static void mv_u3d_controller_start(struct mv_u3d
*u3d
)
1058 /* enable link LTSSM state machine */
1059 temp
= ioread32(&u3d
->vuc_regs
->ltssm
);
1060 temp
|= MV_U3D_LTSSM_PHY_INIT_DONE
;
1061 iowrite32(temp
, &u3d
->vuc_regs
->ltssm
);
1063 /* Enable interrupts */
1064 usbintr
= MV_U3D_INTR_ENABLE_LINK_CHG
| MV_U3D_INTR_ENABLE_TXDESC_ERR
|
1065 MV_U3D_INTR_ENABLE_RXDESC_ERR
| MV_U3D_INTR_ENABLE_TX_COMPLETE
|
1066 MV_U3D_INTR_ENABLE_RX_COMPLETE
| MV_U3D_INTR_ENABLE_SETUP
|
1067 (u3d
->vbus_valid_detect
? MV_U3D_INTR_ENABLE_VBUS_VALID
: 0);
1068 iowrite32(usbintr
, &u3d
->vuc_regs
->intrenable
);
1070 /* Enable ctrl ep */
1071 iowrite32(0x1, &u3d
->vuc_regs
->ctrlepenable
);
1073 /* Set the Run bit in the command register */
1074 iowrite32(MV_U3D_CMD_RUN_STOP
, &u3d
->op_regs
->usbcmd
);
1075 dev_dbg(u3d
->dev
, "after u3d_start, USBCMD 0x%x\n",
1076 ioread32(&u3d
->op_regs
->usbcmd
));
1079 static int mv_u3d_controller_reset(struct mv_u3d
*u3d
)
1084 /* Stop the controller */
1085 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1086 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1087 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1089 /* Reset the controller to get default values */
1090 iowrite32(MV_U3D_CMD_CTRL_RESET
, &u3d
->op_regs
->usbcmd
);
1092 /* wait for reset to complete */
1093 loops
= LOOPS(MV_U3D_RESET_TIMEOUT
);
1094 while (ioread32(&u3d
->op_regs
->usbcmd
) & MV_U3D_CMD_CTRL_RESET
) {
1097 "Wait for RESET completed TIMEOUT\n");
1104 /* Configure the Endpoint Context Address */
1105 iowrite32(u3d
->ep_context_dma
, &u3d
->op_regs
->dcbaapl
);
1106 iowrite32(0, &u3d
->op_regs
->dcbaaph
);
1111 static int mv_u3d_enable(struct mv_u3d
*u3d
)
1113 struct mv_usb_platform_data
*pdata
= u3d
->dev
->platform_data
;
1119 if (!u3d
->clock_gating
) {
1124 dev_dbg(u3d
->dev
, "enable u3d\n");
1125 clk_enable(u3d
->clk
);
1126 if (pdata
->phy_init
) {
1127 retval
= pdata
->phy_init(u3d
->phy_regs
);
1130 "init phy error %d\n", retval
);
1131 clk_disable(u3d
->clk
);
1140 static void mv_u3d_disable(struct mv_u3d
*u3d
)
1142 struct mv_usb_platform_data
*pdata
= u3d
->dev
->platform_data
;
1143 if (u3d
->clock_gating
&& u3d
->active
) {
1144 dev_dbg(u3d
->dev
, "disable u3d\n");
1145 if (pdata
->phy_deinit
)
1146 pdata
->phy_deinit(u3d
->phy_regs
);
1147 clk_disable(u3d
->clk
);
1152 static int mv_u3d_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1155 unsigned long flags
;
1158 u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1160 spin_lock_irqsave(&u3d
->lock
, flags
);
1162 u3d
->vbus_active
= (is_active
!= 0);
1163 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1164 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1166 * 1. external VBUS detect: we can disable/enable clock on demand.
1167 * 2. UDC VBUS detect: we have to enable clock all the time.
1168 * 3. No VBUS detect: we have to enable clock all the time.
1170 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1171 retval
= mv_u3d_enable(u3d
);
1174 * after clock is disabled, we lost all the register
1175 * context. We have to re-init registers
1177 mv_u3d_controller_reset(u3d
);
1178 mv_u3d_ep0_reset(u3d
);
1179 mv_u3d_controller_start(u3d
);
1181 } else if (u3d
->driver
&& u3d
->softconnect
) {
1185 /* stop all the transfer in queue*/
1186 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1187 mv_u3d_controller_stop(u3d
);
1188 mv_u3d_disable(u3d
);
1192 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1196 /* constrain controller's VBUS power usage
1197 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1198 * reporting how much power the device may consume. For example, this
1199 * could affect how quickly batteries are recharged.
1201 * Returns zero on success, else negative errno.
1203 static int mv_u3d_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1205 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1212 static int mv_u3d_pullup(struct usb_gadget
*gadget
, int is_on
)
1214 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1215 unsigned long flags
;
1218 spin_lock_irqsave(&u3d
->lock
, flags
);
1220 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1221 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1222 u3d
->softconnect
= (is_on
!= 0);
1223 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1224 retval
= mv_u3d_enable(u3d
);
1227 * after clock is disabled, we lost all the register
1228 * context. We have to re-init registers
1230 mv_u3d_controller_reset(u3d
);
1231 mv_u3d_ep0_reset(u3d
);
1232 mv_u3d_controller_start(u3d
);
1234 } else if (u3d
->driver
&& u3d
->vbus_active
) {
1235 /* stop all the transfer in queue*/
1236 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1237 mv_u3d_controller_stop(u3d
);
1238 mv_u3d_disable(u3d
);
1241 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1246 static int mv_u3d_start(struct usb_gadget
*g
,
1247 struct usb_gadget_driver
*driver
)
1249 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1250 struct mv_usb_platform_data
*pdata
= u3d
->dev
->platform_data
;
1251 unsigned long flags
;
1256 spin_lock_irqsave(&u3d
->lock
, flags
);
1258 if (!u3d
->clock_gating
) {
1259 clk_enable(u3d
->clk
);
1260 if (pdata
->phy_init
)
1261 pdata
->phy_init(u3d
->phy_regs
);
1264 /* hook up the driver ... */
1265 driver
->driver
.bus
= NULL
;
1266 u3d
->driver
= driver
;
1267 u3d
->gadget
.dev
.driver
= &driver
->driver
;
1269 u3d
->ep0_dir
= USB_DIR_OUT
;
1271 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1273 u3d
->vbus_valid_detect
= 1;
1278 static int mv_u3d_stop(struct usb_gadget
*g
,
1279 struct usb_gadget_driver
*driver
)
1281 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1282 struct mv_usb_platform_data
*pdata
= u3d
->dev
->platform_data
;
1283 unsigned long flags
;
1285 u3d
->vbus_valid_detect
= 0;
1286 spin_lock_irqsave(&u3d
->lock
, flags
);
1288 /* enable clock to access controller register */
1289 clk_enable(u3d
->clk
);
1290 if (pdata
->phy_init
)
1291 pdata
->phy_init(u3d
->phy_regs
);
1293 mv_u3d_controller_stop(u3d
);
1294 /* stop all usb activities */
1295 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1296 mv_u3d_stop_activity(u3d
, driver
);
1297 mv_u3d_disable(u3d
);
1299 if (pdata
->phy_deinit
)
1300 pdata
->phy_deinit(u3d
->phy_regs
);
1301 clk_disable(u3d
->clk
);
1303 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1305 u3d
->gadget
.dev
.driver
= NULL
;
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
)
1529 bool delegate
= false;
1531 mv_u3d_nuke(&u3d
->eps
[ep_num
* 2 + MV_U3D_EP_DIR_IN
], -ESHUTDOWN
);
1533 dev_dbg(u3d
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1534 setup
->bRequestType
, setup
->bRequest
,
1535 setup
->wValue
, setup
->wIndex
, setup
->wLength
);
1537 /* We process some stardard setup requests here */
1538 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1539 switch (setup
->bRequest
) {
1540 case USB_REQ_GET_STATUS
:
1544 case USB_REQ_SET_ADDRESS
:
1545 mv_u3d_ch9setaddress(u3d
, setup
);
1548 case USB_REQ_CLEAR_FEATURE
:
1552 case USB_REQ_SET_FEATURE
:
1562 /* delegate USB standard requests to the gadget driver */
1563 if (delegate
== true) {
1564 /* USB requests handled by gadget */
1565 if (setup
->wLength
) {
1566 /* DATA phase from gadget, STATUS phase from u3d */
1567 u3d
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1568 ? MV_U3D_EP_DIR_IN
: MV_U3D_EP_DIR_OUT
;
1569 spin_unlock(&u3d
->lock
);
1570 if (u3d
->driver
->setup(&u3d
->gadget
,
1571 &u3d
->local_setup_buff
) < 0) {
1572 dev_err(u3d
->dev
, "setup error!\n");
1573 mv_u3d_ep0_stall(u3d
);
1575 spin_lock(&u3d
->lock
);
1577 /* no DATA phase, STATUS phase from gadget */
1578 u3d
->ep0_dir
= MV_U3D_EP_DIR_IN
;
1579 u3d
->ep0_state
= MV_U3D_STATUS_STAGE
;
1580 spin_unlock(&u3d
->lock
);
1581 if (u3d
->driver
->setup(&u3d
->gadget
,
1582 &u3d
->local_setup_buff
) < 0)
1583 mv_u3d_ep0_stall(u3d
);
1584 spin_lock(&u3d
->lock
);
1587 if (mv_u3d_is_set_configuration(setup
)) {
1588 dev_dbg(u3d
->dev
, "u3d configured\n");
1589 u3d
->usb_state
= USB_STATE_CONFIGURED
;
1594 static void mv_u3d_get_setup_data(struct mv_u3d
*u3d
, u8 ep_num
, u8
*buffer_ptr
)
1596 struct mv_u3d_ep_context
*epcontext
;
1598 epcontext
= &u3d
->ep_context
[ep_num
* 2 + MV_U3D_EP_DIR_IN
];
1600 /* Copy the setup packet to local buffer */
1601 memcpy(buffer_ptr
, (u8
*) &epcontext
->setup_buffer
, 8);
1604 static void mv_u3d_irq_process_setup(struct mv_u3d
*u3d
)
1607 /* Process all Setup packet received interrupts */
1608 tmp
= ioread32(&u3d
->vuc_regs
->setuplock
);
1610 for (i
= 0; i
< u3d
->max_eps
; i
++) {
1611 if (tmp
& (1 << i
)) {
1612 mv_u3d_get_setup_data(u3d
, i
,
1613 (u8
*)(&u3d
->local_setup_buff
));
1614 mv_u3d_handle_setup_packet(u3d
, i
,
1615 &u3d
->local_setup_buff
);
1620 iowrite32(tmp
, &u3d
->vuc_regs
->setuplock
);
1623 static void mv_u3d_irq_process_tr_complete(struct mv_u3d
*u3d
)
1626 int i
, ep_num
= 0, direction
= 0;
1627 struct mv_u3d_ep
*curr_ep
;
1628 struct mv_u3d_req
*curr_req
, *temp_req
;
1631 tmp
= ioread32(&u3d
->vuc_regs
->endcomplete
);
1633 dev_dbg(u3d
->dev
, "tr_complete: ep: 0x%x\n", tmp
);
1636 iowrite32(tmp
, &u3d
->vuc_regs
->endcomplete
);
1638 for (i
= 0; i
< u3d
->max_eps
* 2; i
++) {
1642 bit_pos
= 1 << (ep_num
+ 16 * direction
);
1644 if (!(bit_pos
& tmp
))
1648 curr_ep
= &u3d
->eps
[1];
1650 curr_ep
= &u3d
->eps
[i
];
1652 /* remove req out of ep request list after completion */
1653 dev_dbg(u3d
->dev
, "tr comp: check req_list\n");
1654 spin_lock(&curr_ep
->req_lock
);
1655 if (!list_empty(&curr_ep
->req_list
)) {
1656 struct mv_u3d_req
*req
;
1657 req
= list_entry(curr_ep
->req_list
.next
,
1658 struct mv_u3d_req
, list
);
1659 list_del_init(&req
->list
);
1660 curr_ep
->processing
= 0;
1662 spin_unlock(&curr_ep
->req_lock
);
1664 /* process the req queue until an uncomplete request */
1665 list_for_each_entry_safe(curr_req
, temp_req
,
1666 &curr_ep
->queue
, queue
) {
1667 status
= mv_u3d_process_ep_req(u3d
, i
, curr_req
);
1670 /* write back status to req */
1671 curr_req
->req
.status
= status
;
1673 /* ep0 request completion */
1675 mv_u3d_done(curr_ep
, curr_req
, 0);
1678 mv_u3d_done(curr_ep
, curr_req
, status
);
1682 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from ep complete\n");
1683 mv_u3d_start_queue(curr_ep
);
1687 static irqreturn_t
mv_u3d_irq(int irq
, void *dev
)
1689 struct mv_u3d
*u3d
= (struct mv_u3d
*)dev
;
1694 spin_lock(&u3d
->lock
);
1696 status
= ioread32(&u3d
->vuc_regs
->intrcause
);
1697 intr
= ioread32(&u3d
->vuc_regs
->intrenable
);
1701 spin_unlock(&u3d
->lock
);
1702 dev_err(u3d
->dev
, "irq error!\n");
1706 if (status
& MV_U3D_USBINT_VBUS_VALID
) {
1707 bridgesetting
= ioread32(&u3d
->vuc_regs
->bridgesetting
);
1708 if (bridgesetting
& MV_U3D_BRIDGE_SETTING_VBUS_VALID
) {
1709 /* write vbus valid bit of bridge setting to clear */
1710 bridgesetting
= MV_U3D_BRIDGE_SETTING_VBUS_VALID
;
1711 iowrite32(bridgesetting
, &u3d
->vuc_regs
->bridgesetting
);
1712 dev_dbg(u3d
->dev
, "vbus valid\n");
1714 u3d
->usb_state
= USB_STATE_POWERED
;
1715 u3d
->vbus_valid_detect
= 0;
1716 /* if external vbus detect is not supported,
1717 * we handle it here.
1720 spin_unlock(&u3d
->lock
);
1721 mv_u3d_vbus_session(&u3d
->gadget
, 1);
1722 spin_lock(&u3d
->lock
);
1725 dev_err(u3d
->dev
, "vbus bit is not set\n");
1728 /* RX data is already in the 16KB FIFO.*/
1729 if (status
& MV_U3D_USBINT_UNDER_RUN
) {
1730 trbunderrun
= ioread32(&u3d
->vuc_regs
->trbunderrun
);
1731 dev_err(u3d
->dev
, "under run, ep%d\n", trbunderrun
);
1732 iowrite32(trbunderrun
, &u3d
->vuc_regs
->trbunderrun
);
1733 mv_u3d_irq_process_error(u3d
);
1736 if (status
& (MV_U3D_USBINT_RXDESC_ERR
| MV_U3D_USBINT_TXDESC_ERR
)) {
1737 /* write one to clear */
1738 iowrite32(status
& (MV_U3D_USBINT_RXDESC_ERR
1739 | MV_U3D_USBINT_TXDESC_ERR
),
1740 &u3d
->vuc_regs
->intrcause
);
1741 dev_err(u3d
->dev
, "desc err 0x%x\n", status
);
1742 mv_u3d_irq_process_error(u3d
);
1745 if (status
& MV_U3D_USBINT_LINK_CHG
)
1746 mv_u3d_irq_process_link_change(u3d
);
1748 if (status
& MV_U3D_USBINT_TX_COMPLETE
)
1749 mv_u3d_irq_process_tr_complete(u3d
);
1751 if (status
& MV_U3D_USBINT_RX_COMPLETE
)
1752 mv_u3d_irq_process_tr_complete(u3d
);
1754 if (status
& MV_U3D_USBINT_SETUP
)
1755 mv_u3d_irq_process_setup(u3d
);
1757 spin_unlock(&u3d
->lock
);
1761 static void mv_u3d_gadget_release(struct device
*dev
)
1763 dev_dbg(dev
, "%s\n", __func__
);
1766 static __devexit
int mv_u3d_remove(struct platform_device
*dev
)
1768 struct mv_u3d
*u3d
= platform_get_drvdata(dev
);
1770 BUG_ON(u3d
== NULL
);
1772 usb_del_gadget_udc(&u3d
->gadget
);
1774 /* free memory allocated in probe */
1776 dma_pool_destroy(u3d
->trb_pool
);
1778 if (u3d
->ep_context
)
1779 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
1780 u3d
->ep_context
, u3d
->ep_context_dma
);
1785 free_irq(u3d
->irq
, &dev
->dev
);
1788 iounmap(u3d
->cap_regs
);
1789 u3d
->cap_regs
= NULL
;
1791 kfree(u3d
->status_req
);
1795 device_unregister(&u3d
->gadget
.dev
);
1797 platform_set_drvdata(dev
, NULL
);
1804 static int mv_u3d_probe(struct platform_device
*dev
)
1806 struct mv_u3d
*u3d
= NULL
;
1807 struct mv_usb_platform_data
*pdata
= dev
->dev
.platform_data
;
1812 if (!dev
->dev
.platform_data
) {
1813 dev_err(&dev
->dev
, "missing platform_data\n");
1818 u3d
= kzalloc(sizeof(*u3d
), GFP_KERNEL
);
1820 dev_err(&dev
->dev
, "failed to allocate memory for u3d\n");
1822 goto err_alloc_private
;
1825 spin_lock_init(&u3d
->lock
);
1827 platform_set_drvdata(dev
, u3d
);
1829 u3d
->dev
= &dev
->dev
;
1830 u3d
->vbus
= pdata
->vbus
;
1832 u3d
->clk
= clk_get(&dev
->dev
, pdata
->clkname
[0]);
1833 if (IS_ERR(u3d
->clk
)) {
1834 retval
= PTR_ERR(u3d
->clk
);
1838 r
= platform_get_resource_byname(dev
, IORESOURCE_MEM
, "capregs");
1840 dev_err(&dev
->dev
, "no I/O memory resource defined\n");
1842 goto err_get_cap_regs
;
1845 u3d
->cap_regs
= (struct mv_u3d_cap_regs __iomem
*)
1846 ioremap(r
->start
, resource_size(r
));
1847 if (!u3d
->cap_regs
) {
1848 dev_err(&dev
->dev
, "failed to map I/O memory\n");
1850 goto err_map_cap_regs
;
1852 dev_dbg(&dev
->dev
, "cap_regs address: 0x%x/0x%x\n",
1853 (unsigned int)r
->start
, (unsigned int)u3d
->cap_regs
);
1856 /* we will access controller register, so enable the u3d controller */
1857 clk_enable(u3d
->clk
);
1859 if (pdata
->phy_init
) {
1860 retval
= pdata
->phy_init(u3d
->phy_regs
);
1862 dev_err(&dev
->dev
, "init phy error %d\n", retval
);
1863 goto err_u3d_enable
;
1867 u3d
->op_regs
= (struct mv_u3d_op_regs __iomem
*)((u32
)u3d
->cap_regs
1868 + MV_U3D_USB3_OP_REGS_OFFSET
);
1870 u3d
->vuc_regs
= (struct mv_u3d_vuc_regs __iomem
*)((u32
)u3d
->cap_regs
1871 + ioread32(&u3d
->cap_regs
->vuoff
));
1876 * some platform will use usb to download image, it may not disconnect
1877 * usb gadget before loading kernel. So first stop u3d here.
1879 mv_u3d_controller_stop(u3d
);
1880 iowrite32(0xFFFFFFFF, &u3d
->vuc_regs
->intrcause
);
1882 if (pdata
->phy_deinit
)
1883 pdata
->phy_deinit(u3d
->phy_regs
);
1884 clk_disable(u3d
->clk
);
1886 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep_context
) * 2;
1887 size
= (size
+ MV_U3D_EP_CONTEXT_ALIGNMENT
- 1)
1888 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT
- 1);
1889 u3d
->ep_context
= dma_alloc_coherent(&dev
->dev
, size
,
1890 &u3d
->ep_context_dma
, GFP_KERNEL
);
1891 if (!u3d
->ep_context
) {
1892 dev_err(&dev
->dev
, "allocate ep context memory failed\n");
1894 goto err_alloc_ep_context
;
1896 u3d
->ep_context_size
= size
;
1898 /* create TRB dma_pool resource */
1899 u3d
->trb_pool
= dma_pool_create("u3d_trb",
1901 sizeof(struct mv_u3d_trb_hw
),
1902 MV_U3D_TRB_ALIGNMENT
,
1903 MV_U3D_DMA_BOUNDARY
);
1905 if (!u3d
->trb_pool
) {
1907 goto err_alloc_trb_pool
;
1910 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep
) * 2;
1911 u3d
->eps
= kzalloc(size
, GFP_KERNEL
);
1913 dev_err(&dev
->dev
, "allocate ep memory failed\n");
1918 /* initialize ep0 status request structure */
1919 u3d
->status_req
= kzalloc(sizeof(struct mv_u3d_req
) + 8, GFP_KERNEL
);
1920 if (!u3d
->status_req
) {
1921 dev_err(&dev
->dev
, "allocate status_req memory failed\n");
1923 goto err_alloc_status_req
;
1925 INIT_LIST_HEAD(&u3d
->status_req
->queue
);
1927 /* allocate a small amount of memory to get valid address */
1928 u3d
->status_req
->req
.buf
= (char *)u3d
->status_req
1929 + sizeof(struct mv_u3d_req
);
1930 u3d
->status_req
->req
.dma
= virt_to_phys(u3d
->status_req
->req
.buf
);
1932 u3d
->resume_state
= USB_STATE_NOTATTACHED
;
1933 u3d
->usb_state
= USB_STATE_ATTACHED
;
1934 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1935 u3d
->remote_wakeup
= 0;
1937 r
= platform_get_resource(dev
, IORESOURCE_IRQ
, 0);
1939 dev_err(&dev
->dev
, "no IRQ resource defined\n");
1943 u3d
->irq
= r
->start
;
1944 if (request_irq(u3d
->irq
, mv_u3d_irq
,
1945 IRQF_DISABLED
| IRQF_SHARED
, driver_name
, u3d
)) {
1947 dev_err(&dev
->dev
, "Request irq %d for u3d failed\n",
1950 goto err_request_irq
;
1953 /* initialize gadget structure */
1954 u3d
->gadget
.ops
= &mv_u3d_ops
; /* usb_gadget_ops */
1955 u3d
->gadget
.ep0
= &u3d
->eps
[1].ep
; /* gadget ep0 */
1956 INIT_LIST_HEAD(&u3d
->gadget
.ep_list
); /* ep_list */
1957 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
1959 /* the "gadget" abstracts/virtualizes the controller */
1960 dev_set_name(&u3d
->gadget
.dev
, "gadget");
1961 u3d
->gadget
.dev
.parent
= &dev
->dev
;
1962 u3d
->gadget
.dev
.dma_mask
= dev
->dev
.dma_mask
;
1963 u3d
->gadget
.dev
.release
= mv_u3d_gadget_release
;
1964 u3d
->gadget
.name
= driver_name
; /* gadget name */
1966 retval
= device_register(&u3d
->gadget
.dev
);
1968 goto err_register_gadget_device
;
1970 mv_u3d_eps_init(u3d
);
1972 /* external vbus detection */
1974 u3d
->clock_gating
= 1;
1975 dev_err(&dev
->dev
, "external vbus detection\n");
1978 if (!u3d
->clock_gating
)
1979 u3d
->vbus_active
= 1;
1981 /* enable usb3 controller vbus detection */
1982 u3d
->vbus_valid_detect
= 1;
1984 retval
= usb_add_gadget_udc(&dev
->dev
, &u3d
->gadget
);
1986 goto err_unregister
;
1988 dev_dbg(&dev
->dev
, "successful probe usb3 device %s clock gating.\n",
1989 u3d
->clock_gating
? "with" : "without");
1994 device_unregister(&u3d
->gadget
.dev
);
1995 err_register_gadget_device
:
1996 free_irq(u3d
->irq
, &dev
->dev
);
1999 kfree(u3d
->status_req
);
2000 err_alloc_status_req
:
2003 dma_pool_destroy(u3d
->trb_pool
);
2005 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
2006 u3d
->ep_context
, u3d
->ep_context_dma
);
2007 err_alloc_ep_context
:
2008 if (pdata
->phy_deinit
)
2009 pdata
->phy_deinit(u3d
->phy_regs
);
2010 clk_disable(u3d
->clk
);
2012 iounmap(u3d
->cap_regs
);
2017 platform_set_drvdata(dev
, NULL
);
2025 static int mv_u3d_suspend(struct device
*dev
)
2027 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
2030 * only cable is unplugged, usb can suspend.
2031 * So do not care about clock_gating == 1, it is handled by
2034 if (!u3d
->clock_gating
) {
2035 mv_u3d_controller_stop(u3d
);
2037 spin_lock_irq(&u3d
->lock
);
2038 /* stop all usb activities */
2039 mv_u3d_stop_activity(u3d
, u3d
->driver
);
2040 spin_unlock_irq(&u3d
->lock
);
2042 mv_u3d_disable(u3d
);
2048 static int mv_u3d_resume(struct device
*dev
)
2050 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
2053 if (!u3d
->clock_gating
) {
2054 retval
= mv_u3d_enable(u3d
);
2058 if (u3d
->driver
&& u3d
->softconnect
) {
2059 mv_u3d_controller_reset(u3d
);
2060 mv_u3d_ep0_reset(u3d
);
2061 mv_u3d_controller_start(u3d
);
2068 SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops
, mv_u3d_suspend
, mv_u3d_resume
);
2071 static void mv_u3d_shutdown(struct platform_device
*dev
)
2073 struct mv_u3d
*u3d
= dev_get_drvdata(&dev
->dev
);
2076 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
2077 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
2078 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
2081 static struct platform_driver mv_u3d_driver
= {
2082 .probe
= mv_u3d_probe
,
2083 .remove
= __exit_p(mv_u3d_remove
),
2084 .shutdown
= mv_u3d_shutdown
,
2086 .owner
= THIS_MODULE
,
2089 .pm
= &mv_u3d_pm_ops
,
2094 module_platform_driver(mv_u3d_driver
);
2095 MODULE_ALIAS("platform:mv-u3d");
2096 MODULE_DESCRIPTION(DRIVER_DESC
);
2097 MODULE_AUTHOR("Yu Xu <yuxu@marvell.com>");
2098 MODULE_LICENSE("GPL");