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";
36 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status);
37 static void mv_u3d_stop_activity(struct mv_u3d *u3d,
38 struct usb_gadget_driver *driver);
40 /* for endpoint 0 operations */
41 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc = {
42 .bLength = USB_DT_ENDPOINT_SIZE,
43 .bDescriptorType = USB_DT_ENDPOINT,
44 .bEndpointAddress = 0,
45 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
46 .wMaxPacketSize = MV_U3D_EP0_MAX_PKT_SIZE,
49 static void mv_u3d_ep0_reset(struct mv_u3d *u3d)
55 for (i = 0; i < 2; i++) {
59 /* ep0 ep context, ep0 in and out share the same ep context */
60 ep->ep_context = &u3d->ep_context[1];
63 /* reset ep state machine */
65 epxcr = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
66 epxcr |= MV_U3D_EPXCR_EP_INIT;
67 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr0);
69 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
70 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr0);
72 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
73 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
74 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
75 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
76 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
77 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr1);
80 epxcr = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
81 epxcr |= MV_U3D_EPXCR_EP_INIT;
82 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr0);
84 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
85 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr0);
87 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
88 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
89 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
90 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
91 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
92 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr1);
95 static void mv_u3d_ep0_stall(struct mv_u3d *u3d)
98 dev_dbg(u3d->dev, "%s\n", __func__);
100 /* set TX and RX to stall */
101 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
102 tmp |= MV_U3D_EPXCR_EP_HALT;
103 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
105 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
106 tmp |= MV_U3D_EPXCR_EP_HALT;
107 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
109 /* update ep0 state */
110 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
111 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
114 static int mv_u3d_process_ep_req(struct mv_u3d *u3d, int index,
115 struct mv_u3d_req *curr_req)
117 struct mv_u3d_trb *curr_trb;
118 int actual, remaining_length = 0;
119 int direction, ep_num;
121 u32 tmp, status, length;
123 direction = index % 2;
126 actual = curr_req->req.length;
128 while (!list_empty(&curr_req->trb_list)) {
129 curr_trb = list_entry(curr_req->trb_list.next,
130 struct mv_u3d_trb, trb_list);
131 if (!curr_trb->trb_hw->ctrl.own) {
132 dev_err(u3d->dev, "%s, TRB own error!\n",
133 u3d->eps[index].name);
137 curr_trb->trb_hw->ctrl.own = 0;
138 if (direction == MV_U3D_EP_DIR_OUT)
139 tmp = ioread32(&u3d->vuc_regs->rxst[ep_num].statuslo);
141 tmp = ioread32(&u3d->vuc_regs->txst[ep_num].statuslo);
143 status = tmp >> MV_U3D_XFERSTATUS_COMPLETE_SHIFT;
144 length = tmp & MV_U3D_XFERSTATUS_TRB_LENGTH_MASK;
146 if (status == MV_U3D_COMPLETE_SUCCESS ||
147 (status == MV_U3D_COMPLETE_SHORT_PACKET &&
148 direction == MV_U3D_EP_DIR_OUT)) {
149 remaining_length += length;
150 actual -= remaining_length;
153 "complete_tr error: ep=%d %s: error = 0x%x\n",
154 index >> 1, direction ? "SEND" : "RECV",
159 list_del_init(&curr_trb->trb_list);
164 curr_req->req.actual = actual;
169 * mv_u3d_done() - retire a request; caller blocked irqs
170 * @status : request status to be set, only works when
171 * request is still in progress.
174 void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status)
175 __releases(&ep->udc->lock)
176 __acquires(&ep->udc->lock)
178 struct mv_u3d *u3d = (struct mv_u3d *)ep->u3d;
180 dev_dbg(u3d->dev, "mv_u3d_done: remove req->queue\n");
181 /* Removed the req from ep queue */
182 list_del_init(&req->queue);
184 /* req.status should be set as -EINPROGRESS in ep_queue() */
185 if (req->req.status == -EINPROGRESS)
186 req->req.status = status;
188 status = req->req.status;
190 /* Free trb for the request */
192 dma_pool_free(u3d->trb_pool,
193 req->trb_head->trb_hw, req->trb_head->trb_dma);
195 dma_unmap_single(ep->u3d->gadget.dev.parent,
196 (dma_addr_t)req->trb_head->trb_dma,
197 req->trb_count * sizeof(struct mv_u3d_trb_hw),
199 kfree(req->trb_head->trb_hw);
201 kfree(req->trb_head);
203 usb_gadget_unmap_request(&u3d->gadget, &req->req, mv_u3d_ep_dir(ep));
205 if (status && (status != -ESHUTDOWN)) {
206 dev_dbg(u3d->dev, "complete %s req %p stat %d len %u/%u",
207 ep->ep.name, &req->req, status,
208 req->req.actual, req->req.length);
211 spin_unlock(&ep->u3d->lock);
213 usb_gadget_giveback_request(&ep->ep, &req->req);
215 spin_lock(&ep->u3d->lock);
218 static int mv_u3d_queue_trb(struct mv_u3d_ep *ep, struct mv_u3d_req *req)
222 struct mv_u3d_ep_context *ep_context;
226 direction = mv_u3d_ep_dir(ep);
228 /* ep0 in and out share the same ep context slot 1*/
230 ep_context = &(u3d->ep_context[1]);
232 ep_context = &(u3d->ep_context[ep->ep_num * 2 + direction]);
234 /* check if the pipe is empty or not */
235 if (!list_empty(&ep->queue)) {
236 dev_err(u3d->dev, "add trb to non-empty queue!\n");
240 ep_context->rsvd0 = cpu_to_le32(1);
241 ep_context->rsvd1 = 0;
243 /* Configure the trb address and set the DCS bit.
244 * Both DCS bit and own bit in trb should be set.
246 ep_context->trb_addr_lo =
247 cpu_to_le32(req->trb_head->trb_dma | DCS_ENABLE);
248 ep_context->trb_addr_hi = 0;
250 /* Ensure that updates to the EP Context will
251 * occure before Ring Bell.
255 /* ring bell the ep */
260 + ((direction == MV_U3D_EP_DIR_OUT) ? 0 : 1);
262 iowrite32(tmp, &u3d->op_regs->doorbell);
267 static struct mv_u3d_trb *mv_u3d_build_trb_one(struct mv_u3d_req *req,
268 unsigned *length, dma_addr_t *dma)
271 unsigned int direction;
272 struct mv_u3d_trb *trb;
273 struct mv_u3d_trb_hw *trb_hw;
276 /* how big will this transfer be? */
277 *length = req->req.length - req->req.actual;
278 BUG_ON(*length > (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
282 trb = kzalloc(sizeof(*trb), GFP_ATOMIC);
287 * Be careful that no _GFP_HIGHMEM is set,
288 * or we can not use dma_to_virt
289 * cannot use GFP_KERNEL in spin lock
291 trb_hw = dma_pool_alloc(u3d->trb_pool, GFP_ATOMIC, dma);
295 "%s, dma_pool_alloc fail\n", __func__);
299 trb->trb_hw = trb_hw;
301 /* initialize buffer page pointers */
302 temp = (u32)(req->req.dma + req->req.actual);
304 trb_hw->buf_addr_lo = cpu_to_le32(temp);
305 trb_hw->buf_addr_hi = 0;
306 trb_hw->trb_len = cpu_to_le32(*length);
307 trb_hw->ctrl.own = 1;
309 if (req->ep->ep_num == 0)
310 trb_hw->ctrl.type = TYPE_DATA;
312 trb_hw->ctrl.type = TYPE_NORMAL;
314 req->req.actual += *length;
316 direction = mv_u3d_ep_dir(req->ep);
317 if (direction == MV_U3D_EP_DIR_IN)
318 trb_hw->ctrl.dir = 1;
320 trb_hw->ctrl.dir = 0;
322 /* Enable interrupt for the last trb of a request */
323 if (!req->req.no_interrupt)
324 trb_hw->ctrl.ioc = 1;
326 trb_hw->ctrl.chain = 0;
332 static int mv_u3d_build_trb_chain(struct mv_u3d_req *req, unsigned *length,
333 struct mv_u3d_trb *trb, int *is_last)
336 unsigned int direction;
339 /* how big will this transfer be? */
340 *length = min(req->req.length - req->req.actual,
341 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
347 /* initialize buffer page pointers */
348 temp = (u32)(req->req.dma + req->req.actual);
350 trb->trb_hw->buf_addr_lo = cpu_to_le32(temp);
351 trb->trb_hw->buf_addr_hi = 0;
352 trb->trb_hw->trb_len = cpu_to_le32(*length);
353 trb->trb_hw->ctrl.own = 1;
355 if (req->ep->ep_num == 0)
356 trb->trb_hw->ctrl.type = TYPE_DATA;
358 trb->trb_hw->ctrl.type = TYPE_NORMAL;
360 req->req.actual += *length;
362 direction = mv_u3d_ep_dir(req->ep);
363 if (direction == MV_U3D_EP_DIR_IN)
364 trb->trb_hw->ctrl.dir = 1;
366 trb->trb_hw->ctrl.dir = 0;
368 /* zlp is needed if req->req.zero is set */
370 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
374 } else if (req->req.length == req->req.actual)
379 /* Enable interrupt for the last trb of a request */
380 if (*is_last && !req->req.no_interrupt)
381 trb->trb_hw->ctrl.ioc = 1;
384 trb->trb_hw->ctrl.chain = 0;
386 trb->trb_hw->ctrl.chain = 1;
387 dev_dbg(u3d->dev, "chain trb\n");
395 /* generate TRB linked list for a request
396 * usb controller only supports continous trb chain,
397 * that trb structure physical address should be continous.
399 static int mv_u3d_req_to_trb(struct mv_u3d_req *req)
403 struct mv_u3d_trb *trb;
404 struct mv_u3d_trb_hw *trb_hw;
412 INIT_LIST_HEAD(&req->trb_list);
414 length = req->req.length - req->req.actual;
415 /* normally the request transfer length is less than 16KB.
416 * we use buil_trb_one() to optimize it.
418 if (length <= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER) {
419 trb = mv_u3d_build_trb_one(req, &count, &dma);
420 list_add_tail(&trb->trb_list, &req->trb_list);
425 trb_num = length / MV_U3D_EP_MAX_LENGTH_TRANSFER;
426 if (length % MV_U3D_EP_MAX_LENGTH_TRANSFER)
429 trb = kcalloc(trb_num, sizeof(*trb), GFP_ATOMIC);
433 trb_hw = kcalloc(trb_num, sizeof(*trb_hw), GFP_ATOMIC);
440 trb->trb_hw = trb_hw;
441 if (mv_u3d_build_trb_chain(req, &count,
444 "%s, mv_u3d_build_trb_chain fail\n",
449 list_add_tail(&trb->trb_list, &req->trb_list);
455 req->trb_head = list_entry(req->trb_list.next,
456 struct mv_u3d_trb, trb_list);
457 req->trb_head->trb_dma = dma_map_single(u3d->gadget.dev.parent,
458 req->trb_head->trb_hw,
459 trb_num * sizeof(*trb_hw),
461 if (dma_mapping_error(u3d->gadget.dev.parent,
462 req->trb_head->trb_dma)) {
463 kfree(req->trb_head->trb_hw);
464 kfree(req->trb_head);
475 mv_u3d_start_queue(struct mv_u3d_ep *ep)
477 struct mv_u3d *u3d = ep->u3d;
478 struct mv_u3d_req *req;
481 if (!list_empty(&ep->req_list) && !ep->processing)
482 req = list_entry(ep->req_list.next, struct mv_u3d_req, list);
488 /* set up dma mapping */
489 ret = usb_gadget_map_request(&u3d->gadget, &req->req,
492 goto break_processing;
494 req->req.status = -EINPROGRESS;
499 ret = mv_u3d_req_to_trb(req);
501 dev_err(u3d->dev, "%s, mv_u3d_req_to_trb fail\n", __func__);
502 goto break_processing;
505 /* and push them to device queue */
506 ret = mv_u3d_queue_trb(ep, req);
508 goto break_processing;
510 /* irq handler advances the queue */
511 list_add_tail(&req->queue, &ep->queue);
520 static int mv_u3d_ep_enable(struct usb_ep *_ep,
521 const struct usb_endpoint_descriptor *desc)
524 struct mv_u3d_ep *ep;
526 unsigned maxburst = 0;
527 u32 epxcr, direction;
529 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT)
532 ep = container_of(_ep, struct mv_u3d_ep, ep);
535 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN)
538 direction = mv_u3d_ep_dir(ep);
539 max = le16_to_cpu(desc->wMaxPacketSize);
543 maxburst = _ep->maxburst;
545 /* Set the max burst size */
546 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
547 case USB_ENDPOINT_XFER_BULK:
550 "max burst should not be greater "
551 "than 16 on bulk ep\n");
553 _ep->maxburst = maxburst;
556 "maxburst: %d on bulk %s\n", maxburst, ep->name);
558 case USB_ENDPOINT_XFER_CONTROL:
559 /* control transfer only supports maxburst as one */
561 _ep->maxburst = maxburst;
563 case USB_ENDPOINT_XFER_INT:
566 "max burst should be 1 on int ep "
567 "if transfer size is not 1024\n");
569 _ep->maxburst = maxburst;
572 case USB_ENDPOINT_XFER_ISOC:
575 "max burst should be 1 on isoc ep "
576 "if transfer size is not 1024\n");
578 _ep->maxburst = maxburst;
585 ep->ep.maxpacket = max;
589 /* Enable the endpoint for Rx or Tx and set the endpoint type */
590 if (direction == MV_U3D_EP_DIR_OUT) {
591 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
592 epxcr |= MV_U3D_EPXCR_EP_INIT;
593 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
595 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
596 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
598 epxcr = ((max << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
599 | ((maxburst - 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
600 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
601 | (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
602 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
604 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
605 epxcr |= MV_U3D_EPXCR_EP_INIT;
606 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
608 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
609 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
611 epxcr = ((max << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
612 | ((maxburst - 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
613 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
614 | (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
615 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
623 static int mv_u3d_ep_disable(struct usb_ep *_ep)
626 struct mv_u3d_ep *ep;
627 u32 epxcr, direction;
633 ep = container_of(_ep, struct mv_u3d_ep, ep);
639 direction = mv_u3d_ep_dir(ep);
641 /* nuke all pending requests (does flush) */
642 spin_lock_irqsave(&u3d->lock, flags);
643 mv_u3d_nuke(ep, -ESHUTDOWN);
644 spin_unlock_irqrestore(&u3d->lock, flags);
646 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
647 if (direction == MV_U3D_EP_DIR_OUT) {
648 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
649 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
650 | USB_ENDPOINT_XFERTYPE_MASK);
651 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
653 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
654 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
655 | USB_ENDPOINT_XFERTYPE_MASK);
656 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
665 static struct usb_request *
666 mv_u3d_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
668 struct mv_u3d_req *req = NULL;
670 req = kzalloc(sizeof *req, gfp_flags);
674 INIT_LIST_HEAD(&req->queue);
679 static void mv_u3d_free_request(struct usb_ep *_ep, struct usb_request *_req)
681 struct mv_u3d_req *req = container_of(_req, struct mv_u3d_req, req);
686 static void mv_u3d_ep_fifo_flush(struct usb_ep *_ep)
690 struct mv_u3d_ep *ep = container_of(_ep, struct mv_u3d_ep, ep);
694 /* if endpoint is not enabled, cannot flush endpoint */
699 direction = mv_u3d_ep_dir(ep);
701 /* ep0 need clear bit after flushing fifo. */
703 if (direction == MV_U3D_EP_DIR_OUT) {
704 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
705 tmp |= MV_U3D_EPXCR_EP_FLUSH;
706 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
708 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
709 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
711 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
712 tmp |= MV_U3D_EPXCR_EP_FLUSH;
713 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
715 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
716 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
721 if (direction == MV_U3D_EP_DIR_OUT) {
722 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
723 tmp |= MV_U3D_EPXCR_EP_FLUSH;
724 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
726 /* Wait until flushing completed */
727 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
728 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0) &
729 MV_U3D_EPXCR_EP_FLUSH) {
731 * EP_FLUSH bit should be cleared to indicate this
732 * operation is complete
736 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
737 direction ? "in" : "out");
743 } else { /* EP_DIR_IN */
744 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
745 tmp |= MV_U3D_EPXCR_EP_FLUSH;
746 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
748 /* Wait until flushing completed */
749 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
750 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0) &
751 MV_U3D_EPXCR_EP_FLUSH) {
753 * EP_FLUSH bit should be cleared to indicate this
754 * operation is complete
758 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
759 direction ? "in" : "out");
768 /* queues (submits) an I/O request to an endpoint */
770 mv_u3d_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
772 struct mv_u3d_ep *ep;
773 struct mv_u3d_req *req;
776 int is_first_req = 0;
778 if (unlikely(!_ep || !_req))
781 ep = container_of(_ep, struct mv_u3d_ep, ep);
784 req = container_of(_req, struct mv_u3d_req, req);
787 && u3d->ep0_state == MV_U3D_STATUS_STAGE
789 dev_dbg(u3d->dev, "ep0 status stage\n");
790 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
794 dev_dbg(u3d->dev, "%s: %s, req: 0x%p\n",
795 __func__, _ep->name, req);
797 /* catch various bogus parameters */
798 if (!req->req.complete || !req->req.buf
799 || !list_empty(&req->queue)) {
801 "%s, bad params, _req: 0x%p,"
802 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
803 "list_empty: 0x%x\n",
805 req->req.complete, req->req.buf,
806 list_empty(&req->queue));
809 if (unlikely(!ep->ep.desc)) {
810 dev_err(u3d->dev, "%s, bad ep\n", __func__);
813 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
814 if (req->req.length > ep->ep.maxpacket)
818 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN) {
820 "bad params of driver/speed\n");
826 /* Software list handles usb request. */
827 spin_lock_irqsave(&ep->req_lock, flags);
828 is_first_req = list_empty(&ep->req_list);
829 list_add_tail(&req->list, &ep->req_list);
830 spin_unlock_irqrestore(&ep->req_lock, flags);
832 dev_dbg(u3d->dev, "list is not empty\n");
836 dev_dbg(u3d->dev, "call mv_u3d_start_queue from usb_ep_queue\n");
837 spin_lock_irqsave(&u3d->lock, flags);
838 mv_u3d_start_queue(ep);
839 spin_unlock_irqrestore(&u3d->lock, flags);
843 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
844 static int mv_u3d_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
846 struct mv_u3d_ep *ep;
847 struct mv_u3d_req *req = NULL, *iter;
849 struct mv_u3d_ep_context *ep_context;
850 struct mv_u3d_req *next_req;
858 ep = container_of(_ep, struct mv_u3d_ep, ep);
861 spin_lock_irqsave(&ep->u3d->lock, flags);
863 /* make sure it's actually queued on this endpoint */
864 list_for_each_entry(iter, &ep->queue, queue) {
865 if (&iter->req != _req)
875 /* The request is in progress, or completed but not dequeued */
876 if (ep->queue.next == &req->queue) {
877 _req->status = -ECONNRESET;
878 mv_u3d_ep_fifo_flush(_ep);
880 /* The request isn't the last request in this ep queue */
881 if (req->queue.next != &ep->queue) {
883 "it is the last request in this ep queue\n");
884 ep_context = ep->ep_context;
885 next_req = list_entry(req->queue.next,
886 struct mv_u3d_req, queue);
888 /* Point first TRB of next request to the EP context. */
889 iowrite32((unsigned long) next_req->trb_head,
890 &ep_context->trb_addr_lo);
892 struct mv_u3d_ep_context *ep_context;
893 ep_context = ep->ep_context;
894 ep_context->trb_addr_lo = 0;
895 ep_context->trb_addr_hi = 0;
901 mv_u3d_done(ep, req, -ECONNRESET);
903 /* remove the req from the ep req list */
904 if (!list_empty(&ep->req_list)) {
905 struct mv_u3d_req *curr_req;
906 curr_req = list_entry(ep->req_list.next,
907 struct mv_u3d_req, list);
908 if (curr_req == req) {
909 list_del_init(&req->list);
915 spin_unlock_irqrestore(&ep->u3d->lock, flags);
920 mv_u3d_ep_set_stall(struct mv_u3d *u3d, u8 ep_num, u8 direction, int stall)
923 struct mv_u3d_ep *ep = u3d->eps;
925 dev_dbg(u3d->dev, "%s\n", __func__);
926 if (direction == MV_U3D_EP_DIR_OUT) {
927 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
929 tmp |= MV_U3D_EPXCR_EP_HALT;
931 tmp &= ~MV_U3D_EPXCR_EP_HALT;
932 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
934 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
936 tmp |= MV_U3D_EPXCR_EP_HALT;
938 tmp &= ~MV_U3D_EPXCR_EP_HALT;
939 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
943 static int mv_u3d_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
945 struct mv_u3d_ep *ep;
950 ep = container_of(_ep, struct mv_u3d_ep, ep);
957 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
958 status = -EOPNOTSUPP;
963 * Attempt to halt IN ep will fail if any transfer requests
966 if (halt && (mv_u3d_ep_dir(ep) == MV_U3D_EP_DIR_IN)
967 && !list_empty(&ep->queue)) {
972 spin_lock_irqsave(&ep->u3d->lock, flags);
973 mv_u3d_ep_set_stall(u3d, ep->ep_num, mv_u3d_ep_dir(ep), halt);
978 spin_unlock_irqrestore(&ep->u3d->lock, flags);
981 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
986 static int mv_u3d_ep_set_halt(struct usb_ep *_ep, int halt)
988 return mv_u3d_ep_set_halt_wedge(_ep, halt, 0);
991 static int mv_u3d_ep_set_wedge(struct usb_ep *_ep)
993 return mv_u3d_ep_set_halt_wedge(_ep, 1, 1);
996 static const struct usb_ep_ops mv_u3d_ep_ops = {
997 .enable = mv_u3d_ep_enable,
998 .disable = mv_u3d_ep_disable,
1000 .alloc_request = mv_u3d_alloc_request,
1001 .free_request = mv_u3d_free_request,
1003 .queue = mv_u3d_ep_queue,
1004 .dequeue = mv_u3d_ep_dequeue,
1006 .set_wedge = mv_u3d_ep_set_wedge,
1007 .set_halt = mv_u3d_ep_set_halt,
1008 .fifo_flush = mv_u3d_ep_fifo_flush,
1011 static void mv_u3d_controller_stop(struct mv_u3d *u3d)
1015 if (!u3d->clock_gating && u3d->vbus_valid_detect)
1016 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID,
1017 &u3d->vuc_regs->intrenable);
1019 iowrite32(0, &u3d->vuc_regs->intrenable);
1020 iowrite32(~0x0, &u3d->vuc_regs->endcomplete);
1021 iowrite32(~0x0, &u3d->vuc_regs->trbunderrun);
1022 iowrite32(~0x0, &u3d->vuc_regs->trbcomplete);
1023 iowrite32(~0x0, &u3d->vuc_regs->linkchange);
1024 iowrite32(0x1, &u3d->vuc_regs->setuplock);
1026 /* Reset the RUN bit in the command register to stop USB */
1027 tmp = ioread32(&u3d->op_regs->usbcmd);
1028 tmp &= ~MV_U3D_CMD_RUN_STOP;
1029 iowrite32(tmp, &u3d->op_regs->usbcmd);
1030 dev_dbg(u3d->dev, "after u3d_stop, USBCMD 0x%x\n",
1031 ioread32(&u3d->op_regs->usbcmd));
1034 static void mv_u3d_controller_start(struct mv_u3d *u3d)
1039 /* enable link LTSSM state machine */
1040 temp = ioread32(&u3d->vuc_regs->ltssm);
1041 temp |= MV_U3D_LTSSM_PHY_INIT_DONE;
1042 iowrite32(temp, &u3d->vuc_regs->ltssm);
1044 /* Enable interrupts */
1045 usbintr = MV_U3D_INTR_ENABLE_LINK_CHG | MV_U3D_INTR_ENABLE_TXDESC_ERR |
1046 MV_U3D_INTR_ENABLE_RXDESC_ERR | MV_U3D_INTR_ENABLE_TX_COMPLETE |
1047 MV_U3D_INTR_ENABLE_RX_COMPLETE | MV_U3D_INTR_ENABLE_SETUP |
1048 (u3d->vbus_valid_detect ? MV_U3D_INTR_ENABLE_VBUS_VALID : 0);
1049 iowrite32(usbintr, &u3d->vuc_regs->intrenable);
1051 /* Enable ctrl ep */
1052 iowrite32(0x1, &u3d->vuc_regs->ctrlepenable);
1054 /* Set the Run bit in the command register */
1055 iowrite32(MV_U3D_CMD_RUN_STOP, &u3d->op_regs->usbcmd);
1056 dev_dbg(u3d->dev, "after u3d_start, USBCMD 0x%x\n",
1057 ioread32(&u3d->op_regs->usbcmd));
1060 static int mv_u3d_controller_reset(struct mv_u3d *u3d)
1065 /* Stop the controller */
1066 tmp = ioread32(&u3d->op_regs->usbcmd);
1067 tmp &= ~MV_U3D_CMD_RUN_STOP;
1068 iowrite32(tmp, &u3d->op_regs->usbcmd);
1070 /* Reset the controller to get default values */
1071 iowrite32(MV_U3D_CMD_CTRL_RESET, &u3d->op_regs->usbcmd);
1073 /* wait for reset to complete */
1074 loops = LOOPS(MV_U3D_RESET_TIMEOUT);
1075 while (ioread32(&u3d->op_regs->usbcmd) & MV_U3D_CMD_CTRL_RESET) {
1078 "Wait for RESET completed TIMEOUT\n");
1085 /* Configure the Endpoint Context Address */
1086 iowrite32(u3d->ep_context_dma, &u3d->op_regs->dcbaapl);
1087 iowrite32(0, &u3d->op_regs->dcbaaph);
1092 static int mv_u3d_enable(struct mv_u3d *u3d)
1094 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1100 if (!u3d->clock_gating) {
1105 dev_dbg(u3d->dev, "enable u3d\n");
1106 clk_enable(u3d->clk);
1107 if (pdata->phy_init) {
1108 retval = pdata->phy_init(u3d->phy_regs);
1111 "init phy error %d\n", retval);
1112 clk_disable(u3d->clk);
1121 static void mv_u3d_disable(struct mv_u3d *u3d)
1123 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1124 if (u3d->clock_gating && u3d->active) {
1125 dev_dbg(u3d->dev, "disable u3d\n");
1126 if (pdata->phy_deinit)
1127 pdata->phy_deinit(u3d->phy_regs);
1128 clk_disable(u3d->clk);
1133 static int mv_u3d_vbus_session(struct usb_gadget *gadget, int is_active)
1136 unsigned long flags;
1139 u3d = container_of(gadget, struct mv_u3d, gadget);
1141 spin_lock_irqsave(&u3d->lock, flags);
1143 u3d->vbus_active = (is_active != 0);
1144 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1145 __func__, u3d->softconnect, u3d->vbus_active);
1147 * 1. external VBUS detect: we can disable/enable clock on demand.
1148 * 2. UDC VBUS detect: we have to enable clock all the time.
1149 * 3. No VBUS detect: we have to enable clock all the time.
1151 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1152 retval = mv_u3d_enable(u3d);
1155 * after clock is disabled, we lost all the register
1156 * context. We have to re-init registers
1158 mv_u3d_controller_reset(u3d);
1159 mv_u3d_ep0_reset(u3d);
1160 mv_u3d_controller_start(u3d);
1162 } else if (u3d->driver && u3d->softconnect) {
1166 /* stop all the transfer in queue*/
1167 mv_u3d_stop_activity(u3d, u3d->driver);
1168 mv_u3d_controller_stop(u3d);
1169 mv_u3d_disable(u3d);
1173 spin_unlock_irqrestore(&u3d->lock, flags);
1177 /* constrain controller's VBUS power usage
1178 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1179 * reporting how much power the device may consume. For example, this
1180 * could affect how quickly batteries are recharged.
1182 * Returns zero on success, else negative errno.
1184 static int mv_u3d_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1186 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1193 static int mv_u3d_pullup(struct usb_gadget *gadget, int is_on)
1195 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1196 unsigned long flags;
1199 spin_lock_irqsave(&u3d->lock, flags);
1201 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1202 __func__, u3d->softconnect, u3d->vbus_active);
1203 u3d->softconnect = (is_on != 0);
1204 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1205 retval = mv_u3d_enable(u3d);
1208 * after clock is disabled, we lost all the register
1209 * context. We have to re-init registers
1211 mv_u3d_controller_reset(u3d);
1212 mv_u3d_ep0_reset(u3d);
1213 mv_u3d_controller_start(u3d);
1215 } else if (u3d->driver && u3d->vbus_active) {
1216 /* stop all the transfer in queue*/
1217 mv_u3d_stop_activity(u3d, u3d->driver);
1218 mv_u3d_controller_stop(u3d);
1219 mv_u3d_disable(u3d);
1222 spin_unlock_irqrestore(&u3d->lock, flags);
1227 static int mv_u3d_start(struct usb_gadget *g,
1228 struct usb_gadget_driver *driver)
1230 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1231 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1232 unsigned long flags;
1237 spin_lock_irqsave(&u3d->lock, flags);
1239 if (!u3d->clock_gating) {
1240 clk_enable(u3d->clk);
1241 if (pdata->phy_init)
1242 pdata->phy_init(u3d->phy_regs);
1245 /* hook up the driver ... */
1246 driver->driver.bus = NULL;
1247 u3d->driver = driver;
1249 u3d->ep0_dir = USB_DIR_OUT;
1251 spin_unlock_irqrestore(&u3d->lock, flags);
1253 u3d->vbus_valid_detect = 1;
1258 static int mv_u3d_stop(struct usb_gadget *g)
1260 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1261 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1262 unsigned long flags;
1264 u3d->vbus_valid_detect = 0;
1265 spin_lock_irqsave(&u3d->lock, flags);
1267 /* enable clock to access controller register */
1268 clk_enable(u3d->clk);
1269 if (pdata->phy_init)
1270 pdata->phy_init(u3d->phy_regs);
1272 mv_u3d_controller_stop(u3d);
1273 /* stop all usb activities */
1274 u3d->gadget.speed = USB_SPEED_UNKNOWN;
1275 mv_u3d_stop_activity(u3d, NULL);
1276 mv_u3d_disable(u3d);
1278 if (pdata->phy_deinit)
1279 pdata->phy_deinit(u3d->phy_regs);
1280 clk_disable(u3d->clk);
1282 spin_unlock_irqrestore(&u3d->lock, flags);
1289 /* device controller usb_gadget_ops structure */
1290 static const struct usb_gadget_ops mv_u3d_ops = {
1291 /* notify controller that VBUS is powered or not */
1292 .vbus_session = mv_u3d_vbus_session,
1294 /* constrain controller's VBUS power usage */
1295 .vbus_draw = mv_u3d_vbus_draw,
1297 .pullup = mv_u3d_pullup,
1298 .udc_start = mv_u3d_start,
1299 .udc_stop = mv_u3d_stop,
1302 static int mv_u3d_eps_init(struct mv_u3d *u3d)
1304 struct mv_u3d_ep *ep;
1308 /* initialize ep0, ep0 in/out use eps[1] */
1311 strncpy(ep->name, "ep0", sizeof(ep->name));
1312 ep->ep.name = ep->name;
1313 ep->ep.ops = &mv_u3d_ep_ops;
1315 usb_ep_set_maxpacket_limit(&ep->ep, MV_U3D_EP0_MAX_PKT_SIZE);
1316 ep->ep.caps.type_control = true;
1317 ep->ep.caps.dir_in = true;
1318 ep->ep.caps.dir_out = true;
1320 ep->ep.desc = &mv_u3d_ep0_desc;
1321 INIT_LIST_HEAD(&ep->queue);
1322 INIT_LIST_HEAD(&ep->req_list);
1323 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1325 /* add ep0 ep_context */
1326 ep->ep_context = &u3d->ep_context[1];
1328 /* initialize other endpoints */
1329 for (i = 2; i < u3d->max_eps * 2; i++) {
1332 snprintf(name, sizeof(name), "ep%din", i >> 1);
1333 ep->direction = MV_U3D_EP_DIR_IN;
1334 ep->ep.caps.dir_in = true;
1336 snprintf(name, sizeof(name), "ep%dout", i >> 1);
1337 ep->direction = MV_U3D_EP_DIR_OUT;
1338 ep->ep.caps.dir_out = true;
1341 strncpy(ep->name, name, sizeof(ep->name));
1342 ep->ep.name = ep->name;
1344 ep->ep.caps.type_iso = true;
1345 ep->ep.caps.type_bulk = true;
1346 ep->ep.caps.type_int = true;
1348 ep->ep.ops = &mv_u3d_ep_ops;
1349 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
1352 INIT_LIST_HEAD(&ep->queue);
1353 list_add_tail(&ep->ep.ep_list, &u3d->gadget.ep_list);
1355 INIT_LIST_HEAD(&ep->req_list);
1356 spin_lock_init(&ep->req_lock);
1357 ep->ep_context = &u3d->ep_context[i];
1363 /* delete all endpoint requests, called with spinlock held */
1364 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status)
1366 /* endpoint fifo flush */
1367 mv_u3d_ep_fifo_flush(&ep->ep);
1369 while (!list_empty(&ep->queue)) {
1370 struct mv_u3d_req *req = NULL;
1371 req = list_entry(ep->queue.next, struct mv_u3d_req, queue);
1372 mv_u3d_done(ep, req, status);
1376 /* stop all USB activities */
1378 void mv_u3d_stop_activity(struct mv_u3d *u3d, struct usb_gadget_driver *driver)
1380 struct mv_u3d_ep *ep;
1382 mv_u3d_nuke(&u3d->eps[1], -ESHUTDOWN);
1384 list_for_each_entry(ep, &u3d->gadget.ep_list, ep.ep_list) {
1385 mv_u3d_nuke(ep, -ESHUTDOWN);
1388 /* report disconnect; the driver is already quiesced */
1390 spin_unlock(&u3d->lock);
1391 driver->disconnect(&u3d->gadget);
1392 spin_lock(&u3d->lock);
1396 static void mv_u3d_irq_process_error(struct mv_u3d *u3d)
1398 /* Increment the error count */
1400 dev_err(u3d->dev, "%s\n", __func__);
1403 static void mv_u3d_irq_process_link_change(struct mv_u3d *u3d)
1407 linkchange = ioread32(&u3d->vuc_regs->linkchange);
1408 iowrite32(linkchange, &u3d->vuc_regs->linkchange);
1410 dev_dbg(u3d->dev, "linkchange: 0x%x\n", linkchange);
1412 if (linkchange & MV_U3D_LINK_CHANGE_LINK_UP) {
1413 dev_dbg(u3d->dev, "link up: ltssm state: 0x%x\n",
1414 ioread32(&u3d->vuc_regs->ltssmstate));
1416 u3d->usb_state = USB_STATE_DEFAULT;
1417 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1418 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
1421 u3d->gadget.speed = USB_SPEED_SUPER;
1424 if (linkchange & MV_U3D_LINK_CHANGE_SUSPEND) {
1425 dev_dbg(u3d->dev, "link suspend\n");
1426 u3d->resume_state = u3d->usb_state;
1427 u3d->usb_state = USB_STATE_SUSPENDED;
1430 if (linkchange & MV_U3D_LINK_CHANGE_RESUME) {
1431 dev_dbg(u3d->dev, "link resume\n");
1432 u3d->usb_state = u3d->resume_state;
1433 u3d->resume_state = 0;
1436 if (linkchange & MV_U3D_LINK_CHANGE_WRESET) {
1437 dev_dbg(u3d->dev, "warm reset\n");
1438 u3d->usb_state = USB_STATE_POWERED;
1441 if (linkchange & MV_U3D_LINK_CHANGE_HRESET) {
1442 dev_dbg(u3d->dev, "hot reset\n");
1443 u3d->usb_state = USB_STATE_DEFAULT;
1446 if (linkchange & MV_U3D_LINK_CHANGE_INACT)
1447 dev_dbg(u3d->dev, "inactive\n");
1449 if (linkchange & MV_U3D_LINK_CHANGE_DISABLE_AFTER_U0)
1450 dev_dbg(u3d->dev, "ss.disabled\n");
1452 if (linkchange & MV_U3D_LINK_CHANGE_VBUS_INVALID) {
1453 dev_dbg(u3d->dev, "vbus invalid\n");
1454 u3d->usb_state = USB_STATE_ATTACHED;
1455 u3d->vbus_valid_detect = 1;
1456 /* if external vbus detect is not supported,
1457 * we handle it here.
1460 spin_unlock(&u3d->lock);
1461 mv_u3d_vbus_session(&u3d->gadget, 0);
1462 spin_lock(&u3d->lock);
1467 static void mv_u3d_ch9setaddress(struct mv_u3d *u3d,
1468 struct usb_ctrlrequest *setup)
1472 if (u3d->usb_state != USB_STATE_DEFAULT) {
1474 "%s, cannot setaddr in this state (%d)\n",
1475 __func__, u3d->usb_state);
1479 u3d->dev_addr = (u8)setup->wValue;
1481 dev_dbg(u3d->dev, "%s: 0x%x\n", __func__, u3d->dev_addr);
1483 if (u3d->dev_addr > 127) {
1485 "%s, u3d address is wrong (out of range)\n", __func__);
1490 /* update usb state */
1491 u3d->usb_state = USB_STATE_ADDRESS;
1493 /* set the new address */
1494 tmp = ioread32(&u3d->vuc_regs->devaddrtiebrkr);
1496 tmp |= (u32)u3d->dev_addr;
1497 iowrite32(tmp, &u3d->vuc_regs->devaddrtiebrkr);
1501 mv_u3d_ep0_stall(u3d);
1504 static int mv_u3d_is_set_configuration(struct usb_ctrlrequest *setup)
1506 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1507 if (setup->bRequest == USB_REQ_SET_CONFIGURATION)
1513 static void mv_u3d_handle_setup_packet(struct mv_u3d *u3d, u8 ep_num,
1514 struct usb_ctrlrequest *setup)
1515 __releases(&u3c->lock)
1516 __acquires(&u3c->lock)
1518 bool delegate = false;
1520 mv_u3d_nuke(&u3d->eps[ep_num * 2 + MV_U3D_EP_DIR_IN], -ESHUTDOWN);
1522 dev_dbg(u3d->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1523 setup->bRequestType, setup->bRequest,
1524 setup->wValue, setup->wIndex, setup->wLength);
1526 /* We process some stardard setup requests here */
1527 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1528 switch (setup->bRequest) {
1529 case USB_REQ_GET_STATUS:
1533 case USB_REQ_SET_ADDRESS:
1534 mv_u3d_ch9setaddress(u3d, setup);
1537 case USB_REQ_CLEAR_FEATURE:
1541 case USB_REQ_SET_FEATURE:
1551 /* delegate USB standard requests to the gadget driver */
1553 /* USB requests handled by gadget */
1554 if (setup->wLength) {
1555 /* DATA phase from gadget, STATUS phase from u3d */
1556 u3d->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1557 ? MV_U3D_EP_DIR_IN : MV_U3D_EP_DIR_OUT;
1558 spin_unlock(&u3d->lock);
1559 if (u3d->driver->setup(&u3d->gadget,
1560 &u3d->local_setup_buff) < 0) {
1561 dev_err(u3d->dev, "setup error!\n");
1562 mv_u3d_ep0_stall(u3d);
1564 spin_lock(&u3d->lock);
1566 /* no DATA phase, STATUS phase from gadget */
1567 u3d->ep0_dir = MV_U3D_EP_DIR_IN;
1568 u3d->ep0_state = MV_U3D_STATUS_STAGE;
1569 spin_unlock(&u3d->lock);
1570 if (u3d->driver->setup(&u3d->gadget,
1571 &u3d->local_setup_buff) < 0)
1572 mv_u3d_ep0_stall(u3d);
1573 spin_lock(&u3d->lock);
1576 if (mv_u3d_is_set_configuration(setup)) {
1577 dev_dbg(u3d->dev, "u3d configured\n");
1578 u3d->usb_state = USB_STATE_CONFIGURED;
1583 static void mv_u3d_get_setup_data(struct mv_u3d *u3d, u8 ep_num, u8 *buffer_ptr)
1585 struct mv_u3d_ep_context *epcontext;
1587 epcontext = &u3d->ep_context[ep_num * 2 + MV_U3D_EP_DIR_IN];
1589 /* Copy the setup packet to local buffer */
1590 memcpy(buffer_ptr, (u8 *) &epcontext->setup_buffer, 8);
1593 static void mv_u3d_irq_process_setup(struct mv_u3d *u3d)
1596 /* Process all Setup packet received interrupts */
1597 tmp = ioread32(&u3d->vuc_regs->setuplock);
1599 for (i = 0; i < u3d->max_eps; i++) {
1600 if (tmp & (1 << i)) {
1601 mv_u3d_get_setup_data(u3d, i,
1602 (u8 *)(&u3d->local_setup_buff));
1603 mv_u3d_handle_setup_packet(u3d, i,
1604 &u3d->local_setup_buff);
1609 iowrite32(tmp, &u3d->vuc_regs->setuplock);
1612 static void mv_u3d_irq_process_tr_complete(struct mv_u3d *u3d)
1615 int i, ep_num = 0, direction = 0;
1616 struct mv_u3d_ep *curr_ep;
1617 struct mv_u3d_req *curr_req, *temp_req;
1620 tmp = ioread32(&u3d->vuc_regs->endcomplete);
1622 dev_dbg(u3d->dev, "tr_complete: ep: 0x%x\n", tmp);
1625 iowrite32(tmp, &u3d->vuc_regs->endcomplete);
1627 for (i = 0; i < u3d->max_eps * 2; i++) {
1631 bit_pos = 1 << (ep_num + 16 * direction);
1633 if (!(bit_pos & tmp))
1637 curr_ep = &u3d->eps[1];
1639 curr_ep = &u3d->eps[i];
1641 /* remove req out of ep request list after completion */
1642 dev_dbg(u3d->dev, "tr comp: check req_list\n");
1643 spin_lock(&curr_ep->req_lock);
1644 if (!list_empty(&curr_ep->req_list)) {
1645 struct mv_u3d_req *req;
1646 req = list_entry(curr_ep->req_list.next,
1647 struct mv_u3d_req, list);
1648 list_del_init(&req->list);
1649 curr_ep->processing = 0;
1651 spin_unlock(&curr_ep->req_lock);
1653 /* process the req queue until an uncomplete request */
1654 list_for_each_entry_safe(curr_req, temp_req,
1655 &curr_ep->queue, queue) {
1656 status = mv_u3d_process_ep_req(u3d, i, curr_req);
1659 /* write back status to req */
1660 curr_req->req.status = status;
1662 /* ep0 request completion */
1664 mv_u3d_done(curr_ep, curr_req, 0);
1667 mv_u3d_done(curr_ep, curr_req, status);
1671 dev_dbg(u3d->dev, "call mv_u3d_start_queue from ep complete\n");
1672 mv_u3d_start_queue(curr_ep);
1676 static irqreturn_t mv_u3d_irq(int irq, void *dev)
1678 struct mv_u3d *u3d = (struct mv_u3d *)dev;
1683 spin_lock(&u3d->lock);
1685 status = ioread32(&u3d->vuc_regs->intrcause);
1686 intr = ioread32(&u3d->vuc_regs->intrenable);
1690 spin_unlock(&u3d->lock);
1691 dev_err(u3d->dev, "irq error!\n");
1695 if (status & MV_U3D_USBINT_VBUS_VALID) {
1696 bridgesetting = ioread32(&u3d->vuc_regs->bridgesetting);
1697 if (bridgesetting & MV_U3D_BRIDGE_SETTING_VBUS_VALID) {
1698 /* write vbus valid bit of bridge setting to clear */
1699 bridgesetting = MV_U3D_BRIDGE_SETTING_VBUS_VALID;
1700 iowrite32(bridgesetting, &u3d->vuc_regs->bridgesetting);
1701 dev_dbg(u3d->dev, "vbus valid\n");
1703 u3d->usb_state = USB_STATE_POWERED;
1704 u3d->vbus_valid_detect = 0;
1705 /* if external vbus detect is not supported,
1706 * we handle it here.
1709 spin_unlock(&u3d->lock);
1710 mv_u3d_vbus_session(&u3d->gadget, 1);
1711 spin_lock(&u3d->lock);
1714 dev_err(u3d->dev, "vbus bit is not set\n");
1717 /* RX data is already in the 16KB FIFO.*/
1718 if (status & MV_U3D_USBINT_UNDER_RUN) {
1719 trbunderrun = ioread32(&u3d->vuc_regs->trbunderrun);
1720 dev_err(u3d->dev, "under run, ep%d\n", trbunderrun);
1721 iowrite32(trbunderrun, &u3d->vuc_regs->trbunderrun);
1722 mv_u3d_irq_process_error(u3d);
1725 if (status & (MV_U3D_USBINT_RXDESC_ERR | MV_U3D_USBINT_TXDESC_ERR)) {
1726 /* write one to clear */
1727 iowrite32(status & (MV_U3D_USBINT_RXDESC_ERR
1728 | MV_U3D_USBINT_TXDESC_ERR),
1729 &u3d->vuc_regs->intrcause);
1730 dev_err(u3d->dev, "desc err 0x%x\n", status);
1731 mv_u3d_irq_process_error(u3d);
1734 if (status & MV_U3D_USBINT_LINK_CHG)
1735 mv_u3d_irq_process_link_change(u3d);
1737 if (status & MV_U3D_USBINT_TX_COMPLETE)
1738 mv_u3d_irq_process_tr_complete(u3d);
1740 if (status & MV_U3D_USBINT_RX_COMPLETE)
1741 mv_u3d_irq_process_tr_complete(u3d);
1743 if (status & MV_U3D_USBINT_SETUP)
1744 mv_u3d_irq_process_setup(u3d);
1746 spin_unlock(&u3d->lock);
1750 static int mv_u3d_remove(struct platform_device *dev)
1752 struct mv_u3d *u3d = platform_get_drvdata(dev);
1754 BUG_ON(u3d == NULL);
1756 usb_del_gadget_udc(&u3d->gadget);
1758 /* free memory allocated in probe */
1759 dma_pool_destroy(u3d->trb_pool);
1761 if (u3d->ep_context)
1762 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1763 u3d->ep_context, u3d->ep_context_dma);
1768 free_irq(u3d->irq, u3d);
1771 iounmap(u3d->cap_regs);
1772 u3d->cap_regs = NULL;
1774 kfree(u3d->status_req);
1783 static int mv_u3d_probe(struct platform_device *dev)
1785 struct mv_u3d *u3d = NULL;
1786 struct mv_usb_platform_data *pdata = dev_get_platdata(&dev->dev);
1791 if (!dev_get_platdata(&dev->dev)) {
1792 dev_err(&dev->dev, "missing platform_data\n");
1797 u3d = kzalloc(sizeof(*u3d), GFP_KERNEL);
1800 goto err_alloc_private;
1803 spin_lock_init(&u3d->lock);
1805 platform_set_drvdata(dev, u3d);
1807 u3d->dev = &dev->dev;
1808 u3d->vbus = pdata->vbus;
1810 u3d->clk = clk_get(&dev->dev, NULL);
1811 if (IS_ERR(u3d->clk)) {
1812 retval = PTR_ERR(u3d->clk);
1816 r = platform_get_resource_byname(dev, IORESOURCE_MEM, "capregs");
1818 dev_err(&dev->dev, "no I/O memory resource defined\n");
1820 goto err_get_cap_regs;
1823 u3d->cap_regs = (struct mv_u3d_cap_regs __iomem *)
1824 ioremap(r->start, resource_size(r));
1825 if (!u3d->cap_regs) {
1826 dev_err(&dev->dev, "failed to map I/O memory\n");
1828 goto err_map_cap_regs;
1830 dev_dbg(&dev->dev, "cap_regs address: 0x%lx/0x%lx\n",
1831 (unsigned long) r->start,
1832 (unsigned long) u3d->cap_regs);
1835 /* we will access controller register, so enable the u3d controller */
1836 retval = clk_enable(u3d->clk);
1838 dev_err(&dev->dev, "clk_enable error %d\n", retval);
1839 goto err_u3d_enable;
1842 if (pdata->phy_init) {
1843 retval = pdata->phy_init(u3d->phy_regs);
1845 dev_err(&dev->dev, "init phy error %d\n", retval);
1846 clk_disable(u3d->clk);
1851 u3d->op_regs = (struct mv_u3d_op_regs __iomem *)(u3d->cap_regs
1852 + MV_U3D_USB3_OP_REGS_OFFSET);
1854 u3d->vuc_regs = (struct mv_u3d_vuc_regs __iomem *)(u3d->cap_regs
1855 + ioread32(&u3d->cap_regs->vuoff));
1860 * some platform will use usb to download image, it may not disconnect
1861 * usb gadget before loading kernel. So first stop u3d here.
1863 mv_u3d_controller_stop(u3d);
1864 iowrite32(0xFFFFFFFF, &u3d->vuc_regs->intrcause);
1866 if (pdata->phy_deinit)
1867 pdata->phy_deinit(u3d->phy_regs);
1868 clk_disable(u3d->clk);
1870 size = u3d->max_eps * sizeof(struct mv_u3d_ep_context) * 2;
1871 size = (size + MV_U3D_EP_CONTEXT_ALIGNMENT - 1)
1872 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT - 1);
1873 u3d->ep_context = dma_alloc_coherent(&dev->dev, size,
1874 &u3d->ep_context_dma, GFP_KERNEL);
1875 if (!u3d->ep_context) {
1876 dev_err(&dev->dev, "allocate ep context memory failed\n");
1878 goto err_alloc_ep_context;
1880 u3d->ep_context_size = size;
1882 /* create TRB dma_pool resource */
1883 u3d->trb_pool = dma_pool_create("u3d_trb",
1885 sizeof(struct mv_u3d_trb_hw),
1886 MV_U3D_TRB_ALIGNMENT,
1887 MV_U3D_DMA_BOUNDARY);
1889 if (!u3d->trb_pool) {
1891 goto err_alloc_trb_pool;
1894 size = u3d->max_eps * sizeof(struct mv_u3d_ep) * 2;
1895 u3d->eps = kzalloc(size, GFP_KERNEL);
1901 /* initialize ep0 status request structure */
1902 u3d->status_req = kzalloc(sizeof(struct mv_u3d_req) + 8, GFP_KERNEL);
1903 if (!u3d->status_req) {
1905 goto err_alloc_status_req;
1907 INIT_LIST_HEAD(&u3d->status_req->queue);
1909 /* allocate a small amount of memory to get valid address */
1910 u3d->status_req->req.buf = (char *)u3d->status_req
1911 + sizeof(struct mv_u3d_req);
1912 u3d->status_req->req.dma = virt_to_phys(u3d->status_req->req.buf);
1914 u3d->resume_state = USB_STATE_NOTATTACHED;
1915 u3d->usb_state = USB_STATE_ATTACHED;
1916 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1917 u3d->remote_wakeup = 0;
1919 r = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1921 dev_err(&dev->dev, "no IRQ resource defined\n");
1925 u3d->irq = r->start;
1927 /* initialize gadget structure */
1928 u3d->gadget.ops = &mv_u3d_ops; /* usb_gadget_ops */
1929 u3d->gadget.ep0 = &u3d->eps[1].ep; /* gadget ep0 */
1930 INIT_LIST_HEAD(&u3d->gadget.ep_list); /* ep_list */
1931 u3d->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
1933 /* the "gadget" abstracts/virtualizes the controller */
1934 u3d->gadget.name = driver_name; /* gadget name */
1936 mv_u3d_eps_init(u3d);
1938 if (request_irq(u3d->irq, mv_u3d_irq,
1939 IRQF_SHARED, driver_name, u3d)) {
1941 dev_err(&dev->dev, "Request irq %d for u3d failed\n",
1944 goto err_request_irq;
1947 /* external vbus detection */
1949 u3d->clock_gating = 1;
1950 dev_err(&dev->dev, "external vbus detection\n");
1953 if (!u3d->clock_gating)
1954 u3d->vbus_active = 1;
1956 /* enable usb3 controller vbus detection */
1957 u3d->vbus_valid_detect = 1;
1959 retval = usb_add_gadget_udc(&dev->dev, &u3d->gadget);
1961 goto err_unregister;
1963 dev_dbg(&dev->dev, "successful probe usb3 device %s clock gating.\n",
1964 u3d->clock_gating ? "with" : "without");
1969 free_irq(u3d->irq, u3d);
1972 kfree(u3d->status_req);
1973 err_alloc_status_req:
1976 dma_pool_destroy(u3d->trb_pool);
1978 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1979 u3d->ep_context, u3d->ep_context_dma);
1980 err_alloc_ep_context:
1983 iounmap(u3d->cap_regs);
1994 #ifdef CONFIG_PM_SLEEP
1995 static int mv_u3d_suspend(struct device *dev)
1997 struct mv_u3d *u3d = dev_get_drvdata(dev);
2000 * only cable is unplugged, usb can suspend.
2001 * So do not care about clock_gating == 1, it is handled by
2004 if (!u3d->clock_gating) {
2005 mv_u3d_controller_stop(u3d);
2007 spin_lock_irq(&u3d->lock);
2008 /* stop all usb activities */
2009 mv_u3d_stop_activity(u3d, u3d->driver);
2010 spin_unlock_irq(&u3d->lock);
2012 mv_u3d_disable(u3d);
2018 static int mv_u3d_resume(struct device *dev)
2020 struct mv_u3d *u3d = dev_get_drvdata(dev);
2023 if (!u3d->clock_gating) {
2024 retval = mv_u3d_enable(u3d);
2028 if (u3d->driver && u3d->softconnect) {
2029 mv_u3d_controller_reset(u3d);
2030 mv_u3d_ep0_reset(u3d);
2031 mv_u3d_controller_start(u3d);
2039 static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops, mv_u3d_suspend, mv_u3d_resume);
2041 static void mv_u3d_shutdown(struct platform_device *dev)
2043 struct mv_u3d *u3d = platform_get_drvdata(dev);
2046 tmp = ioread32(&u3d->op_regs->usbcmd);
2047 tmp &= ~MV_U3D_CMD_RUN_STOP;
2048 iowrite32(tmp, &u3d->op_regs->usbcmd);
2051 static struct platform_driver mv_u3d_driver = {
2052 .probe = mv_u3d_probe,
2053 .remove = mv_u3d_remove,
2054 .shutdown = mv_u3d_shutdown,
2057 .pm = &mv_u3d_pm_ops,
2061 module_platform_driver(mv_u3d_driver);
2062 MODULE_ALIAS("platform:mv-u3d");
2063 MODULE_DESCRIPTION(DRIVER_DESC);
2064 MODULE_AUTHOR("Yu Xu <yuxu@marvell.com>");
2065 MODULE_LICENSE("GPL");