1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
6 #include <linux/module.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/dmapool.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/ioport.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/timer.h>
16 #include <linux/list.h>
17 #include <linux/notifier.h>
18 #include <linux/interrupt.h>
19 #include <linux/moduleparam.h>
20 #include <linux/device.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
25 #include <linux/irq.h>
26 #include <linux/platform_device.h>
27 #include <linux/platform_data/mv_usb.h>
28 #include <linux/clk.h>
32 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
34 static const char driver_name
[] = "mv_u3d";
35 static const char driver_desc
[] = DRIVER_DESC
;
37 static void mv_u3d_nuke(struct mv_u3d_ep
*ep
, int status
);
38 static void mv_u3d_stop_activity(struct mv_u3d
*u3d
,
39 struct usb_gadget_driver
*driver
);
41 /* for endpoint 0 operations */
42 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc
= {
43 .bLength
= USB_DT_ENDPOINT_SIZE
,
44 .bDescriptorType
= USB_DT_ENDPOINT
,
45 .bEndpointAddress
= 0,
46 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
47 .wMaxPacketSize
= MV_U3D_EP0_MAX_PKT_SIZE
,
50 static void mv_u3d_ep0_reset(struct mv_u3d
*u3d
)
56 for (i
= 0; i
< 2; i
++) {
60 /* ep0 ep context, ep0 in and out share the same ep context */
61 ep
->ep_context
= &u3d
->ep_context
[1];
64 /* reset ep state machine */
66 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
67 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
68 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
70 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
71 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
73 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
74 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
75 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
76 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
77 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
78 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxoutcr1
);
81 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
82 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
83 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
85 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
86 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
88 epxcr
= ((MV_U3D_EP0_MAX_PKT_SIZE
89 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
90 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
91 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
92 | MV_U3D_EPXCR_EP_TYPE_CONTROL
);
93 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[0].epxincr1
);
96 static void mv_u3d_ep0_stall(struct mv_u3d
*u3d
)
99 dev_dbg(u3d
->dev
, "%s\n", __func__
);
101 /* set TX and RX to stall */
102 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
103 tmp
|= MV_U3D_EPXCR_EP_HALT
;
104 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
106 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
107 tmp
|= MV_U3D_EPXCR_EP_HALT
;
108 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
110 /* update ep0 state */
111 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
112 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
115 static int mv_u3d_process_ep_req(struct mv_u3d
*u3d
, int index
,
116 struct mv_u3d_req
*curr_req
)
118 struct mv_u3d_trb
*curr_trb
;
119 int actual
, remaining_length
= 0;
120 int direction
, ep_num
;
122 u32 tmp
, status
, length
;
124 direction
= index
% 2;
127 actual
= curr_req
->req
.length
;
129 while (!list_empty(&curr_req
->trb_list
)) {
130 curr_trb
= list_entry(curr_req
->trb_list
.next
,
131 struct mv_u3d_trb
, trb_list
);
132 if (!curr_trb
->trb_hw
->ctrl
.own
) {
133 dev_err(u3d
->dev
, "%s, TRB own error!\n",
134 u3d
->eps
[index
].name
);
138 curr_trb
->trb_hw
->ctrl
.own
= 0;
139 if (direction
== MV_U3D_EP_DIR_OUT
)
140 tmp
= ioread32(&u3d
->vuc_regs
->rxst
[ep_num
].statuslo
);
142 tmp
= ioread32(&u3d
->vuc_regs
->txst
[ep_num
].statuslo
);
144 status
= tmp
>> MV_U3D_XFERSTATUS_COMPLETE_SHIFT
;
145 length
= tmp
& MV_U3D_XFERSTATUS_TRB_LENGTH_MASK
;
147 if (status
== MV_U3D_COMPLETE_SUCCESS
||
148 (status
== MV_U3D_COMPLETE_SHORT_PACKET
&&
149 direction
== MV_U3D_EP_DIR_OUT
)) {
150 remaining_length
+= length
;
151 actual
-= remaining_length
;
154 "complete_tr error: ep=%d %s: error = 0x%x\n",
155 index
>> 1, direction
? "SEND" : "RECV",
160 list_del_init(&curr_trb
->trb_list
);
165 curr_req
->req
.actual
= actual
;
170 * mv_u3d_done() - retire a request; caller blocked irqs
171 * @status : request status to be set, only works when
172 * request is still in progress.
175 void mv_u3d_done(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
, int status
)
176 __releases(&ep
->udc
->lock
)
177 __acquires(&ep
->udc
->lock
)
179 struct mv_u3d
*u3d
= (struct mv_u3d
*)ep
->u3d
;
181 dev_dbg(u3d
->dev
, "mv_u3d_done: remove req->queue\n");
182 /* Removed the req from ep queue */
183 list_del_init(&req
->queue
);
185 /* req.status should be set as -EINPROGRESS in ep_queue() */
186 if (req
->req
.status
== -EINPROGRESS
)
187 req
->req
.status
= status
;
189 status
= req
->req
.status
;
191 /* Free trb for the request */
193 dma_pool_free(u3d
->trb_pool
,
194 req
->trb_head
->trb_hw
, req
->trb_head
->trb_dma
);
196 dma_unmap_single(ep
->u3d
->gadget
.dev
.parent
,
197 (dma_addr_t
)req
->trb_head
->trb_dma
,
198 req
->trb_count
* sizeof(struct mv_u3d_trb_hw
),
200 kfree(req
->trb_head
->trb_hw
);
202 kfree(req
->trb_head
);
204 usb_gadget_unmap_request(&u3d
->gadget
, &req
->req
, mv_u3d_ep_dir(ep
));
206 if (status
&& (status
!= -ESHUTDOWN
)) {
207 dev_dbg(u3d
->dev
, "complete %s req %p stat %d len %u/%u",
208 ep
->ep
.name
, &req
->req
, status
,
209 req
->req
.actual
, req
->req
.length
);
212 spin_unlock(&ep
->u3d
->lock
);
214 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
216 spin_lock(&ep
->u3d
->lock
);
219 static int mv_u3d_queue_trb(struct mv_u3d_ep
*ep
, struct mv_u3d_req
*req
)
223 struct mv_u3d_ep_context
*ep_context
;
227 direction
= mv_u3d_ep_dir(ep
);
229 /* ep0 in and out share the same ep context slot 1*/
231 ep_context
= &(u3d
->ep_context
[1]);
233 ep_context
= &(u3d
->ep_context
[ep
->ep_num
* 2 + direction
]);
235 /* check if the pipe is empty or not */
236 if (!list_empty(&ep
->queue
)) {
237 dev_err(u3d
->dev
, "add trb to non-empty queue!\n");
241 ep_context
->rsvd0
= cpu_to_le32(1);
242 ep_context
->rsvd1
= 0;
244 /* Configure the trb address and set the DCS bit.
245 * Both DCS bit and own bit in trb should be set.
247 ep_context
->trb_addr_lo
=
248 cpu_to_le32(req
->trb_head
->trb_dma
| DCS_ENABLE
);
249 ep_context
->trb_addr_hi
= 0;
251 /* Ensure that updates to the EP Context will
252 * occure before Ring Bell.
256 /* ring bell the ep */
261 + ((direction
== MV_U3D_EP_DIR_OUT
) ? 0 : 1);
263 iowrite32(tmp
, &u3d
->op_regs
->doorbell
);
268 static struct mv_u3d_trb
*mv_u3d_build_trb_one(struct mv_u3d_req
*req
,
269 unsigned *length
, dma_addr_t
*dma
)
272 unsigned int direction
;
273 struct mv_u3d_trb
*trb
;
274 struct mv_u3d_trb_hw
*trb_hw
;
277 /* how big will this transfer be? */
278 *length
= req
->req
.length
- req
->req
.actual
;
279 BUG_ON(*length
> (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
283 trb
= kzalloc(sizeof(*trb
), GFP_ATOMIC
);
288 * Be careful that no _GFP_HIGHMEM is set,
289 * or we can not use dma_to_virt
290 * cannot use GFP_KERNEL in spin lock
292 trb_hw
= dma_pool_alloc(u3d
->trb_pool
, GFP_ATOMIC
, dma
);
296 "%s, dma_pool_alloc fail\n", __func__
);
300 trb
->trb_hw
= trb_hw
;
302 /* initialize buffer page pointers */
303 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
305 trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
306 trb_hw
->buf_addr_hi
= 0;
307 trb_hw
->trb_len
= cpu_to_le32(*length
);
308 trb_hw
->ctrl
.own
= 1;
310 if (req
->ep
->ep_num
== 0)
311 trb_hw
->ctrl
.type
= TYPE_DATA
;
313 trb_hw
->ctrl
.type
= TYPE_NORMAL
;
315 req
->req
.actual
+= *length
;
317 direction
= mv_u3d_ep_dir(req
->ep
);
318 if (direction
== MV_U3D_EP_DIR_IN
)
319 trb_hw
->ctrl
.dir
= 1;
321 trb_hw
->ctrl
.dir
= 0;
323 /* Enable interrupt for the last trb of a request */
324 if (!req
->req
.no_interrupt
)
325 trb_hw
->ctrl
.ioc
= 1;
327 trb_hw
->ctrl
.chain
= 0;
333 static int mv_u3d_build_trb_chain(struct mv_u3d_req
*req
, unsigned *length
,
334 struct mv_u3d_trb
*trb
, int *is_last
)
337 unsigned int direction
;
340 /* how big will this transfer be? */
341 *length
= min(req
->req
.length
- req
->req
.actual
,
342 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
);
348 /* initialize buffer page pointers */
349 temp
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
351 trb
->trb_hw
->buf_addr_lo
= cpu_to_le32(temp
);
352 trb
->trb_hw
->buf_addr_hi
= 0;
353 trb
->trb_hw
->trb_len
= cpu_to_le32(*length
);
354 trb
->trb_hw
->ctrl
.own
= 1;
356 if (req
->ep
->ep_num
== 0)
357 trb
->trb_hw
->ctrl
.type
= TYPE_DATA
;
359 trb
->trb_hw
->ctrl
.type
= TYPE_NORMAL
;
361 req
->req
.actual
+= *length
;
363 direction
= mv_u3d_ep_dir(req
->ep
);
364 if (direction
== MV_U3D_EP_DIR_IN
)
365 trb
->trb_hw
->ctrl
.dir
= 1;
367 trb
->trb_hw
->ctrl
.dir
= 0;
369 /* zlp is needed if req->req.zero is set */
371 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
375 } else if (req
->req
.length
== req
->req
.actual
)
380 /* Enable interrupt for the last trb of a request */
381 if (*is_last
&& !req
->req
.no_interrupt
)
382 trb
->trb_hw
->ctrl
.ioc
= 1;
385 trb
->trb_hw
->ctrl
.chain
= 0;
387 trb
->trb_hw
->ctrl
.chain
= 1;
388 dev_dbg(u3d
->dev
, "chain trb\n");
396 /* generate TRB linked list for a request
397 * usb controller only supports continous trb chain,
398 * that trb structure physical address should be continous.
400 static int mv_u3d_req_to_trb(struct mv_u3d_req
*req
)
404 struct mv_u3d_trb
*trb
;
405 struct mv_u3d_trb_hw
*trb_hw
;
413 INIT_LIST_HEAD(&req
->trb_list
);
415 length
= req
->req
.length
- req
->req
.actual
;
416 /* normally the request transfer length is less than 16KB.
417 * we use buil_trb_one() to optimize it.
419 if (length
<= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER
) {
420 trb
= mv_u3d_build_trb_one(req
, &count
, &dma
);
421 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
426 trb_num
= length
/ MV_U3D_EP_MAX_LENGTH_TRANSFER
;
427 if (length
% MV_U3D_EP_MAX_LENGTH_TRANSFER
)
430 trb
= kcalloc(trb_num
, sizeof(*trb
), GFP_ATOMIC
);
434 trb_hw
= kcalloc(trb_num
, sizeof(*trb_hw
), GFP_ATOMIC
);
441 trb
->trb_hw
= trb_hw
;
442 if (mv_u3d_build_trb_chain(req
, &count
,
445 "%s, mv_u3d_build_trb_chain fail\n",
450 list_add_tail(&trb
->trb_list
, &req
->trb_list
);
456 req
->trb_head
= list_entry(req
->trb_list
.next
,
457 struct mv_u3d_trb
, trb_list
);
458 req
->trb_head
->trb_dma
= dma_map_single(u3d
->gadget
.dev
.parent
,
459 req
->trb_head
->trb_hw
,
460 trb_num
* sizeof(*trb_hw
),
462 if (dma_mapping_error(u3d
->gadget
.dev
.parent
,
463 req
->trb_head
->trb_dma
)) {
464 kfree(req
->trb_head
->trb_hw
);
465 kfree(req
->trb_head
);
476 mv_u3d_start_queue(struct mv_u3d_ep
*ep
)
478 struct mv_u3d
*u3d
= ep
->u3d
;
479 struct mv_u3d_req
*req
;
482 if (!list_empty(&ep
->req_list
) && !ep
->processing
)
483 req
= list_entry(ep
->req_list
.next
, struct mv_u3d_req
, list
);
489 /* set up dma mapping */
490 ret
= usb_gadget_map_request(&u3d
->gadget
, &req
->req
,
493 goto break_processing
;
495 req
->req
.status
= -EINPROGRESS
;
500 ret
= mv_u3d_req_to_trb(req
);
502 dev_err(u3d
->dev
, "%s, mv_u3d_req_to_trb fail\n", __func__
);
503 goto break_processing
;
506 /* and push them to device queue */
507 ret
= mv_u3d_queue_trb(ep
, req
);
509 goto break_processing
;
511 /* irq handler advances the queue */
512 list_add_tail(&req
->queue
, &ep
->queue
);
521 static int mv_u3d_ep_enable(struct usb_ep
*_ep
,
522 const struct usb_endpoint_descriptor
*desc
)
525 struct mv_u3d_ep
*ep
;
527 unsigned maxburst
= 0;
528 u32 epxcr
, direction
;
530 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
533 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
536 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
)
539 direction
= mv_u3d_ep_dir(ep
);
540 max
= le16_to_cpu(desc
->wMaxPacketSize
);
544 maxburst
= _ep
->maxburst
;
546 /* Set the max burst size */
547 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
548 case USB_ENDPOINT_XFER_BULK
:
551 "max burst should not be greater "
552 "than 16 on bulk ep\n");
554 _ep
->maxburst
= maxburst
;
557 "maxburst: %d on bulk %s\n", maxburst
, ep
->name
);
559 case USB_ENDPOINT_XFER_CONTROL
:
560 /* control transfer only supports maxburst as one */
562 _ep
->maxburst
= maxburst
;
564 case USB_ENDPOINT_XFER_INT
:
567 "max burst should be 1 on int ep "
568 "if transfer size is not 1024\n");
570 _ep
->maxburst
= maxburst
;
573 case USB_ENDPOINT_XFER_ISOC
:
576 "max burst should be 1 on isoc ep "
577 "if transfer size is not 1024\n");
579 _ep
->maxburst
= maxburst
;
586 ep
->ep
.maxpacket
= max
;
590 /* Enable the endpoint for Rx or Tx and set the endpoint type */
591 if (direction
== MV_U3D_EP_DIR_OUT
) {
592 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
593 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
594 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
596 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
597 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
599 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
600 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
601 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
602 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
603 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
605 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
606 epxcr
|= MV_U3D_EPXCR_EP_INIT
;
607 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
609 epxcr
&= ~MV_U3D_EPXCR_EP_INIT
;
610 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
612 epxcr
= ((max
<< MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT
)
613 | ((maxburst
- 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT
)
614 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
615 | (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
));
616 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
624 static int mv_u3d_ep_disable(struct usb_ep
*_ep
)
627 struct mv_u3d_ep
*ep
;
628 u32 epxcr
, direction
;
634 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
640 direction
= mv_u3d_ep_dir(ep
);
642 /* nuke all pending requests (does flush) */
643 spin_lock_irqsave(&u3d
->lock
, flags
);
644 mv_u3d_nuke(ep
, -ESHUTDOWN
);
645 spin_unlock_irqrestore(&u3d
->lock
, flags
);
647 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
648 if (direction
== MV_U3D_EP_DIR_OUT
) {
649 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
650 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
651 | USB_ENDPOINT_XFERTYPE_MASK
);
652 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr1
);
654 epxcr
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
655 epxcr
&= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT
)
656 | USB_ENDPOINT_XFERTYPE_MASK
);
657 iowrite32(epxcr
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr1
);
666 static struct usb_request
*
667 mv_u3d_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
669 struct mv_u3d_req
*req
= NULL
;
671 req
= kzalloc(sizeof *req
, gfp_flags
);
675 INIT_LIST_HEAD(&req
->queue
);
680 static void mv_u3d_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
682 struct mv_u3d_req
*req
= container_of(_req
, struct mv_u3d_req
, req
);
687 static void mv_u3d_ep_fifo_flush(struct usb_ep
*_ep
)
691 struct mv_u3d_ep
*ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
695 /* if endpoint is not enabled, cannot flush endpoint */
700 direction
= mv_u3d_ep_dir(ep
);
702 /* ep0 need clear bit after flushing fifo. */
704 if (direction
== MV_U3D_EP_DIR_OUT
) {
705 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
706 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
707 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
709 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
710 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxoutcr0
);
712 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[0].epxincr0
);
713 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
714 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
716 tmp
&= ~MV_U3D_EPXCR_EP_FLUSH
;
717 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[0].epxincr0
);
722 if (direction
== MV_U3D_EP_DIR_OUT
) {
723 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
724 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
725 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
727 /* Wait until flushing completed */
728 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
729 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
) &
730 MV_U3D_EPXCR_EP_FLUSH
) {
732 * EP_FLUSH bit should be cleared to indicate this
733 * operation is complete
737 "EP FLUSH TIMEOUT for ep%d%s\n", ep
->ep_num
,
738 direction
? "in" : "out");
744 } else { /* EP_DIR_IN */
745 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
746 tmp
|= MV_U3D_EPXCR_EP_FLUSH
;
747 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
749 /* Wait until flushing completed */
750 loops
= LOOPS(MV_U3D_FLUSH_TIMEOUT
);
751 while (ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
) &
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");
769 /* queues (submits) an I/O request to an endpoint */
771 mv_u3d_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
773 struct mv_u3d_ep
*ep
;
774 struct mv_u3d_req
*req
;
777 int is_first_req
= 0;
779 if (unlikely(!_ep
|| !_req
))
782 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
785 req
= container_of(_req
, struct mv_u3d_req
, req
);
788 && u3d
->ep0_state
== MV_U3D_STATUS_STAGE
790 dev_dbg(u3d
->dev
, "ep0 status stage\n");
791 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
795 dev_dbg(u3d
->dev
, "%s: %s, req: 0x%p\n",
796 __func__
, _ep
->name
, req
);
798 /* catch various bogus parameters */
799 if (!req
->req
.complete
|| !req
->req
.buf
800 || !list_empty(&req
->queue
)) {
802 "%s, bad params, _req: 0x%p,"
803 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
804 "list_empty: 0x%x\n",
806 req
->req
.complete
, req
->req
.buf
,
807 list_empty(&req
->queue
));
810 if (unlikely(!ep
->ep
.desc
)) {
811 dev_err(u3d
->dev
, "%s, bad ep\n", __func__
);
814 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
815 if (req
->req
.length
> ep
->ep
.maxpacket
)
819 if (!u3d
->driver
|| u3d
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
821 "bad params of driver/speed\n");
827 /* Software list handles usb request. */
828 spin_lock_irqsave(&ep
->req_lock
, flags
);
829 is_first_req
= list_empty(&ep
->req_list
);
830 list_add_tail(&req
->list
, &ep
->req_list
);
831 spin_unlock_irqrestore(&ep
->req_lock
, flags
);
833 dev_dbg(u3d
->dev
, "list is not empty\n");
837 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from usb_ep_queue\n");
838 spin_lock_irqsave(&u3d
->lock
, flags
);
839 mv_u3d_start_queue(ep
);
840 spin_unlock_irqrestore(&u3d
->lock
, flags
);
844 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
845 static int mv_u3d_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
847 struct mv_u3d_ep
*ep
;
848 struct mv_u3d_req
*req
;
850 struct mv_u3d_ep_context
*ep_context
;
851 struct mv_u3d_req
*next_req
;
859 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
862 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
864 /* make sure it's actually queued on this endpoint */
865 list_for_each_entry(req
, &ep
->queue
, queue
) {
866 if (&req
->req
== _req
)
869 if (&req
->req
!= _req
) {
874 /* The request is in progress, or completed but not dequeued */
875 if (ep
->queue
.next
== &req
->queue
) {
876 _req
->status
= -ECONNRESET
;
877 mv_u3d_ep_fifo_flush(_ep
);
879 /* The request isn't the last request in this ep queue */
880 if (req
->queue
.next
!= &ep
->queue
) {
882 "it is the last request in this ep queue\n");
883 ep_context
= ep
->ep_context
;
884 next_req
= list_entry(req
->queue
.next
,
885 struct mv_u3d_req
, queue
);
887 /* Point first TRB of next request to the EP context. */
888 iowrite32((unsigned long) next_req
->trb_head
,
889 &ep_context
->trb_addr_lo
);
891 struct mv_u3d_ep_context
*ep_context
;
892 ep_context
= ep
->ep_context
;
893 ep_context
->trb_addr_lo
= 0;
894 ep_context
->trb_addr_hi
= 0;
900 mv_u3d_done(ep
, req
, -ECONNRESET
);
902 /* remove the req from the ep req list */
903 if (!list_empty(&ep
->req_list
)) {
904 struct mv_u3d_req
*curr_req
;
905 curr_req
= list_entry(ep
->req_list
.next
,
906 struct mv_u3d_req
, list
);
907 if (curr_req
== req
) {
908 list_del_init(&req
->list
);
914 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
919 mv_u3d_ep_set_stall(struct mv_u3d
*u3d
, u8 ep_num
, u8 direction
, int stall
)
922 struct mv_u3d_ep
*ep
= u3d
->eps
;
924 dev_dbg(u3d
->dev
, "%s\n", __func__
);
925 if (direction
== MV_U3D_EP_DIR_OUT
) {
926 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
928 tmp
|= MV_U3D_EPXCR_EP_HALT
;
930 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
931 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxoutcr0
);
933 tmp
= ioread32(&u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
935 tmp
|= MV_U3D_EPXCR_EP_HALT
;
937 tmp
&= ~MV_U3D_EPXCR_EP_HALT
;
938 iowrite32(tmp
, &u3d
->vuc_regs
->epcr
[ep
->ep_num
].epxincr0
);
942 static int mv_u3d_ep_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
944 struct mv_u3d_ep
*ep
;
945 unsigned long flags
= 0;
949 ep
= container_of(_ep
, struct mv_u3d_ep
, ep
);
956 if (ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
957 status
= -EOPNOTSUPP
;
962 * Attempt to halt IN ep will fail if any transfer requests
965 if (halt
&& (mv_u3d_ep_dir(ep
) == MV_U3D_EP_DIR_IN
)
966 && !list_empty(&ep
->queue
)) {
971 spin_lock_irqsave(&ep
->u3d
->lock
, flags
);
972 mv_u3d_ep_set_stall(u3d
, ep
->ep_num
, mv_u3d_ep_dir(ep
), halt
);
977 spin_unlock_irqrestore(&ep
->u3d
->lock
, flags
);
980 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
985 static int mv_u3d_ep_set_halt(struct usb_ep
*_ep
, int halt
)
987 return mv_u3d_ep_set_halt_wedge(_ep
, halt
, 0);
990 static int mv_u3d_ep_set_wedge(struct usb_ep
*_ep
)
992 return mv_u3d_ep_set_halt_wedge(_ep
, 1, 1);
995 static const struct usb_ep_ops mv_u3d_ep_ops
= {
996 .enable
= mv_u3d_ep_enable
,
997 .disable
= mv_u3d_ep_disable
,
999 .alloc_request
= mv_u3d_alloc_request
,
1000 .free_request
= mv_u3d_free_request
,
1002 .queue
= mv_u3d_ep_queue
,
1003 .dequeue
= mv_u3d_ep_dequeue
,
1005 .set_wedge
= mv_u3d_ep_set_wedge
,
1006 .set_halt
= mv_u3d_ep_set_halt
,
1007 .fifo_flush
= mv_u3d_ep_fifo_flush
,
1010 static void mv_u3d_controller_stop(struct mv_u3d
*u3d
)
1014 if (!u3d
->clock_gating
&& u3d
->vbus_valid_detect
)
1015 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID
,
1016 &u3d
->vuc_regs
->intrenable
);
1018 iowrite32(0, &u3d
->vuc_regs
->intrenable
);
1019 iowrite32(~0x0, &u3d
->vuc_regs
->endcomplete
);
1020 iowrite32(~0x0, &u3d
->vuc_regs
->trbunderrun
);
1021 iowrite32(~0x0, &u3d
->vuc_regs
->trbcomplete
);
1022 iowrite32(~0x0, &u3d
->vuc_regs
->linkchange
);
1023 iowrite32(0x1, &u3d
->vuc_regs
->setuplock
);
1025 /* Reset the RUN bit in the command register to stop USB */
1026 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1027 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1028 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1029 dev_dbg(u3d
->dev
, "after u3d_stop, USBCMD 0x%x\n",
1030 ioread32(&u3d
->op_regs
->usbcmd
));
1033 static void mv_u3d_controller_start(struct mv_u3d
*u3d
)
1038 /* enable link LTSSM state machine */
1039 temp
= ioread32(&u3d
->vuc_regs
->ltssm
);
1040 temp
|= MV_U3D_LTSSM_PHY_INIT_DONE
;
1041 iowrite32(temp
, &u3d
->vuc_regs
->ltssm
);
1043 /* Enable interrupts */
1044 usbintr
= MV_U3D_INTR_ENABLE_LINK_CHG
| MV_U3D_INTR_ENABLE_TXDESC_ERR
|
1045 MV_U3D_INTR_ENABLE_RXDESC_ERR
| MV_U3D_INTR_ENABLE_TX_COMPLETE
|
1046 MV_U3D_INTR_ENABLE_RX_COMPLETE
| MV_U3D_INTR_ENABLE_SETUP
|
1047 (u3d
->vbus_valid_detect
? MV_U3D_INTR_ENABLE_VBUS_VALID
: 0);
1048 iowrite32(usbintr
, &u3d
->vuc_regs
->intrenable
);
1050 /* Enable ctrl ep */
1051 iowrite32(0x1, &u3d
->vuc_regs
->ctrlepenable
);
1053 /* Set the Run bit in the command register */
1054 iowrite32(MV_U3D_CMD_RUN_STOP
, &u3d
->op_regs
->usbcmd
);
1055 dev_dbg(u3d
->dev
, "after u3d_start, USBCMD 0x%x\n",
1056 ioread32(&u3d
->op_regs
->usbcmd
));
1059 static int mv_u3d_controller_reset(struct mv_u3d
*u3d
)
1064 /* Stop the controller */
1065 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
1066 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
1067 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
1069 /* Reset the controller to get default values */
1070 iowrite32(MV_U3D_CMD_CTRL_RESET
, &u3d
->op_regs
->usbcmd
);
1072 /* wait for reset to complete */
1073 loops
= LOOPS(MV_U3D_RESET_TIMEOUT
);
1074 while (ioread32(&u3d
->op_regs
->usbcmd
) & MV_U3D_CMD_CTRL_RESET
) {
1077 "Wait for RESET completed TIMEOUT\n");
1084 /* Configure the Endpoint Context Address */
1085 iowrite32(u3d
->ep_context_dma
, &u3d
->op_regs
->dcbaapl
);
1086 iowrite32(0, &u3d
->op_regs
->dcbaaph
);
1091 static int mv_u3d_enable(struct mv_u3d
*u3d
)
1093 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1099 if (!u3d
->clock_gating
) {
1104 dev_dbg(u3d
->dev
, "enable u3d\n");
1105 clk_enable(u3d
->clk
);
1106 if (pdata
->phy_init
) {
1107 retval
= pdata
->phy_init(u3d
->phy_regs
);
1110 "init phy error %d\n", retval
);
1111 clk_disable(u3d
->clk
);
1120 static void mv_u3d_disable(struct mv_u3d
*u3d
)
1122 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1123 if (u3d
->clock_gating
&& u3d
->active
) {
1124 dev_dbg(u3d
->dev
, "disable u3d\n");
1125 if (pdata
->phy_deinit
)
1126 pdata
->phy_deinit(u3d
->phy_regs
);
1127 clk_disable(u3d
->clk
);
1132 static int mv_u3d_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1135 unsigned long flags
;
1138 u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1140 spin_lock_irqsave(&u3d
->lock
, flags
);
1142 u3d
->vbus_active
= (is_active
!= 0);
1143 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1144 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1146 * 1. external VBUS detect: we can disable/enable clock on demand.
1147 * 2. UDC VBUS detect: we have to enable clock all the time.
1148 * 3. No VBUS detect: we have to enable clock all the time.
1150 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1151 retval
= mv_u3d_enable(u3d
);
1154 * after clock is disabled, we lost all the register
1155 * context. We have to re-init registers
1157 mv_u3d_controller_reset(u3d
);
1158 mv_u3d_ep0_reset(u3d
);
1159 mv_u3d_controller_start(u3d
);
1161 } else if (u3d
->driver
&& u3d
->softconnect
) {
1165 /* stop all the transfer in queue*/
1166 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1167 mv_u3d_controller_stop(u3d
);
1168 mv_u3d_disable(u3d
);
1172 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1176 /* constrain controller's VBUS power usage
1177 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1178 * reporting how much power the device may consume. For example, this
1179 * could affect how quickly batteries are recharged.
1181 * Returns zero on success, else negative errno.
1183 static int mv_u3d_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1185 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1192 static int mv_u3d_pullup(struct usb_gadget
*gadget
, int is_on
)
1194 struct mv_u3d
*u3d
= container_of(gadget
, struct mv_u3d
, gadget
);
1195 unsigned long flags
;
1198 spin_lock_irqsave(&u3d
->lock
, flags
);
1200 dev_dbg(u3d
->dev
, "%s: softconnect %d, vbus_active %d\n",
1201 __func__
, u3d
->softconnect
, u3d
->vbus_active
);
1202 u3d
->softconnect
= (is_on
!= 0);
1203 if (u3d
->driver
&& u3d
->softconnect
&& u3d
->vbus_active
) {
1204 retval
= mv_u3d_enable(u3d
);
1207 * after clock is disabled, we lost all the register
1208 * context. We have to re-init registers
1210 mv_u3d_controller_reset(u3d
);
1211 mv_u3d_ep0_reset(u3d
);
1212 mv_u3d_controller_start(u3d
);
1214 } else if (u3d
->driver
&& u3d
->vbus_active
) {
1215 /* stop all the transfer in queue*/
1216 mv_u3d_stop_activity(u3d
, u3d
->driver
);
1217 mv_u3d_controller_stop(u3d
);
1218 mv_u3d_disable(u3d
);
1221 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1226 static int mv_u3d_start(struct usb_gadget
*g
,
1227 struct usb_gadget_driver
*driver
)
1229 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1230 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1231 unsigned long flags
;
1236 spin_lock_irqsave(&u3d
->lock
, flags
);
1238 if (!u3d
->clock_gating
) {
1239 clk_enable(u3d
->clk
);
1240 if (pdata
->phy_init
)
1241 pdata
->phy_init(u3d
->phy_regs
);
1244 /* hook up the driver ... */
1245 driver
->driver
.bus
= NULL
;
1246 u3d
->driver
= driver
;
1248 u3d
->ep0_dir
= USB_DIR_OUT
;
1250 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1252 u3d
->vbus_valid_detect
= 1;
1257 static int mv_u3d_stop(struct usb_gadget
*g
)
1259 struct mv_u3d
*u3d
= container_of(g
, struct mv_u3d
, gadget
);
1260 struct mv_usb_platform_data
*pdata
= dev_get_platdata(u3d
->dev
);
1261 unsigned long flags
;
1263 u3d
->vbus_valid_detect
= 0;
1264 spin_lock_irqsave(&u3d
->lock
, flags
);
1266 /* enable clock to access controller register */
1267 clk_enable(u3d
->clk
);
1268 if (pdata
->phy_init
)
1269 pdata
->phy_init(u3d
->phy_regs
);
1271 mv_u3d_controller_stop(u3d
);
1272 /* stop all usb activities */
1273 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1274 mv_u3d_stop_activity(u3d
, NULL
);
1275 mv_u3d_disable(u3d
);
1277 if (pdata
->phy_deinit
)
1278 pdata
->phy_deinit(u3d
->phy_regs
);
1279 clk_disable(u3d
->clk
);
1281 spin_unlock_irqrestore(&u3d
->lock
, flags
);
1288 /* device controller usb_gadget_ops structure */
1289 static const struct usb_gadget_ops mv_u3d_ops
= {
1290 /* notify controller that VBUS is powered or not */
1291 .vbus_session
= mv_u3d_vbus_session
,
1293 /* constrain controller's VBUS power usage */
1294 .vbus_draw
= mv_u3d_vbus_draw
,
1296 .pullup
= mv_u3d_pullup
,
1297 .udc_start
= mv_u3d_start
,
1298 .udc_stop
= mv_u3d_stop
,
1301 static int mv_u3d_eps_init(struct mv_u3d
*u3d
)
1303 struct mv_u3d_ep
*ep
;
1307 /* initialize ep0, ep0 in/out use eps[1] */
1310 strncpy(ep
->name
, "ep0", sizeof(ep
->name
));
1311 ep
->ep
.name
= ep
->name
;
1312 ep
->ep
.ops
= &mv_u3d_ep_ops
;
1314 usb_ep_set_maxpacket_limit(&ep
->ep
, MV_U3D_EP0_MAX_PKT_SIZE
);
1315 ep
->ep
.caps
.type_control
= true;
1316 ep
->ep
.caps
.dir_in
= true;
1317 ep
->ep
.caps
.dir_out
= true;
1319 ep
->ep
.desc
= &mv_u3d_ep0_desc
;
1320 INIT_LIST_HEAD(&ep
->queue
);
1321 INIT_LIST_HEAD(&ep
->req_list
);
1322 ep
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
1324 /* add ep0 ep_context */
1325 ep
->ep_context
= &u3d
->ep_context
[1];
1327 /* initialize other endpoints */
1328 for (i
= 2; i
< u3d
->max_eps
* 2; i
++) {
1331 snprintf(name
, sizeof(name
), "ep%din", i
>> 1);
1332 ep
->direction
= MV_U3D_EP_DIR_IN
;
1333 ep
->ep
.caps
.dir_in
= true;
1335 snprintf(name
, sizeof(name
), "ep%dout", i
>> 1);
1336 ep
->direction
= MV_U3D_EP_DIR_OUT
;
1337 ep
->ep
.caps
.dir_out
= true;
1340 strncpy(ep
->name
, name
, sizeof(ep
->name
));
1341 ep
->ep
.name
= ep
->name
;
1343 ep
->ep
.caps
.type_iso
= true;
1344 ep
->ep
.caps
.type_bulk
= true;
1345 ep
->ep
.caps
.type_int
= true;
1347 ep
->ep
.ops
= &mv_u3d_ep_ops
;
1348 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
1351 INIT_LIST_HEAD(&ep
->queue
);
1352 list_add_tail(&ep
->ep
.ep_list
, &u3d
->gadget
.ep_list
);
1354 INIT_LIST_HEAD(&ep
->req_list
);
1355 spin_lock_init(&ep
->req_lock
);
1356 ep
->ep_context
= &u3d
->ep_context
[i
];
1362 /* delete all endpoint requests, called with spinlock held */
1363 static void mv_u3d_nuke(struct mv_u3d_ep
*ep
, int status
)
1365 /* endpoint fifo flush */
1366 mv_u3d_ep_fifo_flush(&ep
->ep
);
1368 while (!list_empty(&ep
->queue
)) {
1369 struct mv_u3d_req
*req
= NULL
;
1370 req
= list_entry(ep
->queue
.next
, struct mv_u3d_req
, queue
);
1371 mv_u3d_done(ep
, req
, status
);
1375 /* stop all USB activities */
1377 void mv_u3d_stop_activity(struct mv_u3d
*u3d
, struct usb_gadget_driver
*driver
)
1379 struct mv_u3d_ep
*ep
;
1381 mv_u3d_nuke(&u3d
->eps
[1], -ESHUTDOWN
);
1383 list_for_each_entry(ep
, &u3d
->gadget
.ep_list
, ep
.ep_list
) {
1384 mv_u3d_nuke(ep
, -ESHUTDOWN
);
1387 /* report disconnect; the driver is already quiesced */
1389 spin_unlock(&u3d
->lock
);
1390 driver
->disconnect(&u3d
->gadget
);
1391 spin_lock(&u3d
->lock
);
1395 static void mv_u3d_irq_process_error(struct mv_u3d
*u3d
)
1397 /* Increment the error count */
1399 dev_err(u3d
->dev
, "%s\n", __func__
);
1402 static void mv_u3d_irq_process_link_change(struct mv_u3d
*u3d
)
1406 linkchange
= ioread32(&u3d
->vuc_regs
->linkchange
);
1407 iowrite32(linkchange
, &u3d
->vuc_regs
->linkchange
);
1409 dev_dbg(u3d
->dev
, "linkchange: 0x%x\n", linkchange
);
1411 if (linkchange
& MV_U3D_LINK_CHANGE_LINK_UP
) {
1412 dev_dbg(u3d
->dev
, "link up: ltssm state: 0x%x\n",
1413 ioread32(&u3d
->vuc_regs
->ltssmstate
));
1415 u3d
->usb_state
= USB_STATE_DEFAULT
;
1416 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1417 u3d
->ep0_state
= MV_U3D_WAIT_FOR_SETUP
;
1420 u3d
->gadget
.speed
= USB_SPEED_SUPER
;
1423 if (linkchange
& MV_U3D_LINK_CHANGE_SUSPEND
) {
1424 dev_dbg(u3d
->dev
, "link suspend\n");
1425 u3d
->resume_state
= u3d
->usb_state
;
1426 u3d
->usb_state
= USB_STATE_SUSPENDED
;
1429 if (linkchange
& MV_U3D_LINK_CHANGE_RESUME
) {
1430 dev_dbg(u3d
->dev
, "link resume\n");
1431 u3d
->usb_state
= u3d
->resume_state
;
1432 u3d
->resume_state
= 0;
1435 if (linkchange
& MV_U3D_LINK_CHANGE_WRESET
) {
1436 dev_dbg(u3d
->dev
, "warm reset\n");
1437 u3d
->usb_state
= USB_STATE_POWERED
;
1440 if (linkchange
& MV_U3D_LINK_CHANGE_HRESET
) {
1441 dev_dbg(u3d
->dev
, "hot reset\n");
1442 u3d
->usb_state
= USB_STATE_DEFAULT
;
1445 if (linkchange
& MV_U3D_LINK_CHANGE_INACT
)
1446 dev_dbg(u3d
->dev
, "inactive\n");
1448 if (linkchange
& MV_U3D_LINK_CHANGE_DISABLE_AFTER_U0
)
1449 dev_dbg(u3d
->dev
, "ss.disabled\n");
1451 if (linkchange
& MV_U3D_LINK_CHANGE_VBUS_INVALID
) {
1452 dev_dbg(u3d
->dev
, "vbus invalid\n");
1453 u3d
->usb_state
= USB_STATE_ATTACHED
;
1454 u3d
->vbus_valid_detect
= 1;
1455 /* if external vbus detect is not supported,
1456 * we handle it here.
1459 spin_unlock(&u3d
->lock
);
1460 mv_u3d_vbus_session(&u3d
->gadget
, 0);
1461 spin_lock(&u3d
->lock
);
1466 static void mv_u3d_ch9setaddress(struct mv_u3d
*u3d
,
1467 struct usb_ctrlrequest
*setup
)
1471 if (u3d
->usb_state
!= USB_STATE_DEFAULT
) {
1473 "%s, cannot setaddr in this state (%d)\n",
1474 __func__
, u3d
->usb_state
);
1478 u3d
->dev_addr
= (u8
)setup
->wValue
;
1480 dev_dbg(u3d
->dev
, "%s: 0x%x\n", __func__
, u3d
->dev_addr
);
1482 if (u3d
->dev_addr
> 127) {
1484 "%s, u3d address is wrong (out of range)\n", __func__
);
1489 /* update usb state */
1490 u3d
->usb_state
= USB_STATE_ADDRESS
;
1492 /* set the new address */
1493 tmp
= ioread32(&u3d
->vuc_regs
->devaddrtiebrkr
);
1495 tmp
|= (u32
)u3d
->dev_addr
;
1496 iowrite32(tmp
, &u3d
->vuc_regs
->devaddrtiebrkr
);
1500 mv_u3d_ep0_stall(u3d
);
1503 static int mv_u3d_is_set_configuration(struct usb_ctrlrequest
*setup
)
1505 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
1506 if (setup
->bRequest
== USB_REQ_SET_CONFIGURATION
)
1512 static void mv_u3d_handle_setup_packet(struct mv_u3d
*u3d
, u8 ep_num
,
1513 struct usb_ctrlrequest
*setup
)
1514 __releases(&u3c
->lock
)
1515 __acquires(&u3c
->lock
)
1517 bool delegate
= false;
1519 mv_u3d_nuke(&u3d
->eps
[ep_num
* 2 + MV_U3D_EP_DIR_IN
], -ESHUTDOWN
);
1521 dev_dbg(u3d
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1522 setup
->bRequestType
, setup
->bRequest
,
1523 setup
->wValue
, setup
->wIndex
, setup
->wLength
);
1525 /* We process some stardard setup requests here */
1526 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1527 switch (setup
->bRequest
) {
1528 case USB_REQ_GET_STATUS
:
1532 case USB_REQ_SET_ADDRESS
:
1533 mv_u3d_ch9setaddress(u3d
, setup
);
1536 case USB_REQ_CLEAR_FEATURE
:
1540 case USB_REQ_SET_FEATURE
:
1550 /* delegate USB standard requests to the gadget driver */
1551 if (delegate
== true) {
1552 /* USB requests handled by gadget */
1553 if (setup
->wLength
) {
1554 /* DATA phase from gadget, STATUS phase from u3d */
1555 u3d
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
1556 ? MV_U3D_EP_DIR_IN
: MV_U3D_EP_DIR_OUT
;
1557 spin_unlock(&u3d
->lock
);
1558 if (u3d
->driver
->setup(&u3d
->gadget
,
1559 &u3d
->local_setup_buff
) < 0) {
1560 dev_err(u3d
->dev
, "setup error!\n");
1561 mv_u3d_ep0_stall(u3d
);
1563 spin_lock(&u3d
->lock
);
1565 /* no DATA phase, STATUS phase from gadget */
1566 u3d
->ep0_dir
= MV_U3D_EP_DIR_IN
;
1567 u3d
->ep0_state
= MV_U3D_STATUS_STAGE
;
1568 spin_unlock(&u3d
->lock
);
1569 if (u3d
->driver
->setup(&u3d
->gadget
,
1570 &u3d
->local_setup_buff
) < 0)
1571 mv_u3d_ep0_stall(u3d
);
1572 spin_lock(&u3d
->lock
);
1575 if (mv_u3d_is_set_configuration(setup
)) {
1576 dev_dbg(u3d
->dev
, "u3d configured\n");
1577 u3d
->usb_state
= USB_STATE_CONFIGURED
;
1582 static void mv_u3d_get_setup_data(struct mv_u3d
*u3d
, u8 ep_num
, u8
*buffer_ptr
)
1584 struct mv_u3d_ep_context
*epcontext
;
1586 epcontext
= &u3d
->ep_context
[ep_num
* 2 + MV_U3D_EP_DIR_IN
];
1588 /* Copy the setup packet to local buffer */
1589 memcpy(buffer_ptr
, (u8
*) &epcontext
->setup_buffer
, 8);
1592 static void mv_u3d_irq_process_setup(struct mv_u3d
*u3d
)
1595 /* Process all Setup packet received interrupts */
1596 tmp
= ioread32(&u3d
->vuc_regs
->setuplock
);
1598 for (i
= 0; i
< u3d
->max_eps
; i
++) {
1599 if (tmp
& (1 << i
)) {
1600 mv_u3d_get_setup_data(u3d
, i
,
1601 (u8
*)(&u3d
->local_setup_buff
));
1602 mv_u3d_handle_setup_packet(u3d
, i
,
1603 &u3d
->local_setup_buff
);
1608 iowrite32(tmp
, &u3d
->vuc_regs
->setuplock
);
1611 static void mv_u3d_irq_process_tr_complete(struct mv_u3d
*u3d
)
1614 int i
, ep_num
= 0, direction
= 0;
1615 struct mv_u3d_ep
*curr_ep
;
1616 struct mv_u3d_req
*curr_req
, *temp_req
;
1619 tmp
= ioread32(&u3d
->vuc_regs
->endcomplete
);
1621 dev_dbg(u3d
->dev
, "tr_complete: ep: 0x%x\n", tmp
);
1624 iowrite32(tmp
, &u3d
->vuc_regs
->endcomplete
);
1626 for (i
= 0; i
< u3d
->max_eps
* 2; i
++) {
1630 bit_pos
= 1 << (ep_num
+ 16 * direction
);
1632 if (!(bit_pos
& tmp
))
1636 curr_ep
= &u3d
->eps
[1];
1638 curr_ep
= &u3d
->eps
[i
];
1640 /* remove req out of ep request list after completion */
1641 dev_dbg(u3d
->dev
, "tr comp: check req_list\n");
1642 spin_lock(&curr_ep
->req_lock
);
1643 if (!list_empty(&curr_ep
->req_list
)) {
1644 struct mv_u3d_req
*req
;
1645 req
= list_entry(curr_ep
->req_list
.next
,
1646 struct mv_u3d_req
, list
);
1647 list_del_init(&req
->list
);
1648 curr_ep
->processing
= 0;
1650 spin_unlock(&curr_ep
->req_lock
);
1652 /* process the req queue until an uncomplete request */
1653 list_for_each_entry_safe(curr_req
, temp_req
,
1654 &curr_ep
->queue
, queue
) {
1655 status
= mv_u3d_process_ep_req(u3d
, i
, curr_req
);
1658 /* write back status to req */
1659 curr_req
->req
.status
= status
;
1661 /* ep0 request completion */
1663 mv_u3d_done(curr_ep
, curr_req
, 0);
1666 mv_u3d_done(curr_ep
, curr_req
, status
);
1670 dev_dbg(u3d
->dev
, "call mv_u3d_start_queue from ep complete\n");
1671 mv_u3d_start_queue(curr_ep
);
1675 static irqreturn_t
mv_u3d_irq(int irq
, void *dev
)
1677 struct mv_u3d
*u3d
= (struct mv_u3d
*)dev
;
1682 spin_lock(&u3d
->lock
);
1684 status
= ioread32(&u3d
->vuc_regs
->intrcause
);
1685 intr
= ioread32(&u3d
->vuc_regs
->intrenable
);
1689 spin_unlock(&u3d
->lock
);
1690 dev_err(u3d
->dev
, "irq error!\n");
1694 if (status
& MV_U3D_USBINT_VBUS_VALID
) {
1695 bridgesetting
= ioread32(&u3d
->vuc_regs
->bridgesetting
);
1696 if (bridgesetting
& MV_U3D_BRIDGE_SETTING_VBUS_VALID
) {
1697 /* write vbus valid bit of bridge setting to clear */
1698 bridgesetting
= MV_U3D_BRIDGE_SETTING_VBUS_VALID
;
1699 iowrite32(bridgesetting
, &u3d
->vuc_regs
->bridgesetting
);
1700 dev_dbg(u3d
->dev
, "vbus valid\n");
1702 u3d
->usb_state
= USB_STATE_POWERED
;
1703 u3d
->vbus_valid_detect
= 0;
1704 /* if external vbus detect is not supported,
1705 * we handle it here.
1708 spin_unlock(&u3d
->lock
);
1709 mv_u3d_vbus_session(&u3d
->gadget
, 1);
1710 spin_lock(&u3d
->lock
);
1713 dev_err(u3d
->dev
, "vbus bit is not set\n");
1716 /* RX data is already in the 16KB FIFO.*/
1717 if (status
& MV_U3D_USBINT_UNDER_RUN
) {
1718 trbunderrun
= ioread32(&u3d
->vuc_regs
->trbunderrun
);
1719 dev_err(u3d
->dev
, "under run, ep%d\n", trbunderrun
);
1720 iowrite32(trbunderrun
, &u3d
->vuc_regs
->trbunderrun
);
1721 mv_u3d_irq_process_error(u3d
);
1724 if (status
& (MV_U3D_USBINT_RXDESC_ERR
| MV_U3D_USBINT_TXDESC_ERR
)) {
1725 /* write one to clear */
1726 iowrite32(status
& (MV_U3D_USBINT_RXDESC_ERR
1727 | MV_U3D_USBINT_TXDESC_ERR
),
1728 &u3d
->vuc_regs
->intrcause
);
1729 dev_err(u3d
->dev
, "desc err 0x%x\n", status
);
1730 mv_u3d_irq_process_error(u3d
);
1733 if (status
& MV_U3D_USBINT_LINK_CHG
)
1734 mv_u3d_irq_process_link_change(u3d
);
1736 if (status
& MV_U3D_USBINT_TX_COMPLETE
)
1737 mv_u3d_irq_process_tr_complete(u3d
);
1739 if (status
& MV_U3D_USBINT_RX_COMPLETE
)
1740 mv_u3d_irq_process_tr_complete(u3d
);
1742 if (status
& MV_U3D_USBINT_SETUP
)
1743 mv_u3d_irq_process_setup(u3d
);
1745 spin_unlock(&u3d
->lock
);
1749 static int mv_u3d_remove(struct platform_device
*dev
)
1751 struct mv_u3d
*u3d
= platform_get_drvdata(dev
);
1753 BUG_ON(u3d
== NULL
);
1755 usb_del_gadget_udc(&u3d
->gadget
);
1757 /* free memory allocated in probe */
1758 dma_pool_destroy(u3d
->trb_pool
);
1760 if (u3d
->ep_context
)
1761 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
1762 u3d
->ep_context
, u3d
->ep_context_dma
);
1767 free_irq(u3d
->irq
, u3d
);
1770 iounmap(u3d
->cap_regs
);
1771 u3d
->cap_regs
= NULL
;
1773 kfree(u3d
->status_req
);
1782 static int mv_u3d_probe(struct platform_device
*dev
)
1784 struct mv_u3d
*u3d
= NULL
;
1785 struct mv_usb_platform_data
*pdata
= dev_get_platdata(&dev
->dev
);
1790 if (!dev_get_platdata(&dev
->dev
)) {
1791 dev_err(&dev
->dev
, "missing platform_data\n");
1796 u3d
= kzalloc(sizeof(*u3d
), GFP_KERNEL
);
1799 goto err_alloc_private
;
1802 spin_lock_init(&u3d
->lock
);
1804 platform_set_drvdata(dev
, u3d
);
1806 u3d
->dev
= &dev
->dev
;
1807 u3d
->vbus
= pdata
->vbus
;
1809 u3d
->clk
= clk_get(&dev
->dev
, NULL
);
1810 if (IS_ERR(u3d
->clk
)) {
1811 retval
= PTR_ERR(u3d
->clk
);
1815 r
= platform_get_resource_byname(dev
, IORESOURCE_MEM
, "capregs");
1817 dev_err(&dev
->dev
, "no I/O memory resource defined\n");
1819 goto err_get_cap_regs
;
1822 u3d
->cap_regs
= (struct mv_u3d_cap_regs __iomem
*)
1823 ioremap(r
->start
, resource_size(r
));
1824 if (!u3d
->cap_regs
) {
1825 dev_err(&dev
->dev
, "failed to map I/O memory\n");
1827 goto err_map_cap_regs
;
1829 dev_dbg(&dev
->dev
, "cap_regs address: 0x%lx/0x%lx\n",
1830 (unsigned long) r
->start
,
1831 (unsigned long) u3d
->cap_regs
);
1834 /* we will access controller register, so enable the u3d controller */
1835 retval
= clk_enable(u3d
->clk
);
1837 dev_err(&dev
->dev
, "clk_enable error %d\n", retval
);
1838 goto err_u3d_enable
;
1841 if (pdata
->phy_init
) {
1842 retval
= pdata
->phy_init(u3d
->phy_regs
);
1844 dev_err(&dev
->dev
, "init phy error %d\n", retval
);
1845 clk_disable(u3d
->clk
);
1850 u3d
->op_regs
= (struct mv_u3d_op_regs __iomem
*)(u3d
->cap_regs
1851 + MV_U3D_USB3_OP_REGS_OFFSET
);
1853 u3d
->vuc_regs
= (struct mv_u3d_vuc_regs __iomem
*)(u3d
->cap_regs
1854 + ioread32(&u3d
->cap_regs
->vuoff
));
1859 * some platform will use usb to download image, it may not disconnect
1860 * usb gadget before loading kernel. So first stop u3d here.
1862 mv_u3d_controller_stop(u3d
);
1863 iowrite32(0xFFFFFFFF, &u3d
->vuc_regs
->intrcause
);
1865 if (pdata
->phy_deinit
)
1866 pdata
->phy_deinit(u3d
->phy_regs
);
1867 clk_disable(u3d
->clk
);
1869 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep_context
) * 2;
1870 size
= (size
+ MV_U3D_EP_CONTEXT_ALIGNMENT
- 1)
1871 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT
- 1);
1872 u3d
->ep_context
= dma_alloc_coherent(&dev
->dev
, size
,
1873 &u3d
->ep_context_dma
, GFP_KERNEL
);
1874 if (!u3d
->ep_context
) {
1875 dev_err(&dev
->dev
, "allocate ep context memory failed\n");
1877 goto err_alloc_ep_context
;
1879 u3d
->ep_context_size
= size
;
1881 /* create TRB dma_pool resource */
1882 u3d
->trb_pool
= dma_pool_create("u3d_trb",
1884 sizeof(struct mv_u3d_trb_hw
),
1885 MV_U3D_TRB_ALIGNMENT
,
1886 MV_U3D_DMA_BOUNDARY
);
1888 if (!u3d
->trb_pool
) {
1890 goto err_alloc_trb_pool
;
1893 size
= u3d
->max_eps
* sizeof(struct mv_u3d_ep
) * 2;
1894 u3d
->eps
= kzalloc(size
, GFP_KERNEL
);
1900 /* initialize ep0 status request structure */
1901 u3d
->status_req
= kzalloc(sizeof(struct mv_u3d_req
) + 8, GFP_KERNEL
);
1902 if (!u3d
->status_req
) {
1904 goto err_alloc_status_req
;
1906 INIT_LIST_HEAD(&u3d
->status_req
->queue
);
1908 /* allocate a small amount of memory to get valid address */
1909 u3d
->status_req
->req
.buf
= (char *)u3d
->status_req
1910 + sizeof(struct mv_u3d_req
);
1911 u3d
->status_req
->req
.dma
= virt_to_phys(u3d
->status_req
->req
.buf
);
1913 u3d
->resume_state
= USB_STATE_NOTATTACHED
;
1914 u3d
->usb_state
= USB_STATE_ATTACHED
;
1915 u3d
->ep0_dir
= MV_U3D_EP_DIR_OUT
;
1916 u3d
->remote_wakeup
= 0;
1918 r
= platform_get_resource(dev
, IORESOURCE_IRQ
, 0);
1920 dev_err(&dev
->dev
, "no IRQ resource defined\n");
1924 u3d
->irq
= r
->start
;
1925 if (request_irq(u3d
->irq
, mv_u3d_irq
,
1926 IRQF_SHARED
, driver_name
, u3d
)) {
1928 dev_err(&dev
->dev
, "Request irq %d for u3d failed\n",
1931 goto err_request_irq
;
1934 /* initialize gadget structure */
1935 u3d
->gadget
.ops
= &mv_u3d_ops
; /* usb_gadget_ops */
1936 u3d
->gadget
.ep0
= &u3d
->eps
[1].ep
; /* gadget ep0 */
1937 INIT_LIST_HEAD(&u3d
->gadget
.ep_list
); /* ep_list */
1938 u3d
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
1940 /* the "gadget" abstracts/virtualizes the controller */
1941 u3d
->gadget
.name
= driver_name
; /* gadget name */
1943 mv_u3d_eps_init(u3d
);
1945 /* external vbus detection */
1947 u3d
->clock_gating
= 1;
1948 dev_err(&dev
->dev
, "external vbus detection\n");
1951 if (!u3d
->clock_gating
)
1952 u3d
->vbus_active
= 1;
1954 /* enable usb3 controller vbus detection */
1955 u3d
->vbus_valid_detect
= 1;
1957 retval
= usb_add_gadget_udc(&dev
->dev
, &u3d
->gadget
);
1959 goto err_unregister
;
1961 dev_dbg(&dev
->dev
, "successful probe usb3 device %s clock gating.\n",
1962 u3d
->clock_gating
? "with" : "without");
1967 free_irq(u3d
->irq
, u3d
);
1970 kfree(u3d
->status_req
);
1971 err_alloc_status_req
:
1974 dma_pool_destroy(u3d
->trb_pool
);
1976 dma_free_coherent(&dev
->dev
, u3d
->ep_context_size
,
1977 u3d
->ep_context
, u3d
->ep_context_dma
);
1978 err_alloc_ep_context
:
1981 iounmap(u3d
->cap_regs
);
1992 #ifdef CONFIG_PM_SLEEP
1993 static int mv_u3d_suspend(struct device
*dev
)
1995 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
1998 * only cable is unplugged, usb can suspend.
1999 * So do not care about clock_gating == 1, it is handled by
2002 if (!u3d
->clock_gating
) {
2003 mv_u3d_controller_stop(u3d
);
2005 spin_lock_irq(&u3d
->lock
);
2006 /* stop all usb activities */
2007 mv_u3d_stop_activity(u3d
, u3d
->driver
);
2008 spin_unlock_irq(&u3d
->lock
);
2010 mv_u3d_disable(u3d
);
2016 static int mv_u3d_resume(struct device
*dev
)
2018 struct mv_u3d
*u3d
= dev_get_drvdata(dev
);
2021 if (!u3d
->clock_gating
) {
2022 retval
= mv_u3d_enable(u3d
);
2026 if (u3d
->driver
&& u3d
->softconnect
) {
2027 mv_u3d_controller_reset(u3d
);
2028 mv_u3d_ep0_reset(u3d
);
2029 mv_u3d_controller_start(u3d
);
2037 static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops
, mv_u3d_suspend
, mv_u3d_resume
);
2039 static void mv_u3d_shutdown(struct platform_device
*dev
)
2041 struct mv_u3d
*u3d
= platform_get_drvdata(dev
);
2044 tmp
= ioread32(&u3d
->op_regs
->usbcmd
);
2045 tmp
&= ~MV_U3D_CMD_RUN_STOP
;
2046 iowrite32(tmp
, &u3d
->op_regs
->usbcmd
);
2049 static struct platform_driver mv_u3d_driver
= {
2050 .probe
= mv_u3d_probe
,
2051 .remove
= mv_u3d_remove
,
2052 .shutdown
= mv_u3d_shutdown
,
2055 .pm
= &mv_u3d_pm_ops
,
2059 module_platform_driver(mv_u3d_driver
);
2060 MODULE_ALIAS("platform:mv-u3d");
2061 MODULE_DESCRIPTION(DRIVER_DESC
);
2062 MODULE_AUTHOR("Yu Xu <yuxu@marvell.com>");
2063 MODULE_LICENSE("GPL");