2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmapool.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/ioport.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/timer.h>
19 #include <linux/list.h>
20 #include <linux/notifier.h>
21 #include <linux/interrupt.h>
22 #include <linux/moduleparam.h>
23 #include <linux/device.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
28 #include <linux/irq.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_data/mv_usb.h>
31 #include <linux/clk.h>
35 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
37 static const char driver_name[] = "mv_u3d";
38 static const char driver_desc[] = DRIVER_DESC;
40 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status);
41 static void mv_u3d_stop_activity(struct mv_u3d *u3d,
42 struct usb_gadget_driver *driver);
44 /* for endpoint 0 operations */
45 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc = {
46 .bLength = USB_DT_ENDPOINT_SIZE,
47 .bDescriptorType = USB_DT_ENDPOINT,
48 .bEndpointAddress = 0,
49 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
50 .wMaxPacketSize = MV_U3D_EP0_MAX_PKT_SIZE,
53 static void mv_u3d_ep0_reset(struct mv_u3d *u3d)
59 for (i = 0; i < 2; i++) {
63 /* ep0 ep context, ep0 in and out share the same ep context */
64 ep->ep_context = &u3d->ep_context[1];
67 /* reset ep state machine */
69 epxcr = ioread32(&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_EPXCR_EP_INIT;
74 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr0);
76 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
77 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
78 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
79 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
80 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
81 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr1);
84 epxcr = ioread32(&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_EPXCR_EP_INIT;
89 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr0);
91 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
92 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
93 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
94 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
95 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
96 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr1);
99 static void mv_u3d_ep0_stall(struct mv_u3d *u3d)
102 dev_dbg(u3d->dev, "%s\n", __func__);
104 /* set TX and RX to stall */
105 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
106 tmp |= MV_U3D_EPXCR_EP_HALT;
107 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
109 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
110 tmp |= MV_U3D_EPXCR_EP_HALT;
111 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
113 /* update ep0 state */
114 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
115 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
118 static int mv_u3d_process_ep_req(struct mv_u3d *u3d, int index,
119 struct mv_u3d_req *curr_req)
121 struct mv_u3d_trb *curr_trb;
122 dma_addr_t cur_deq_lo;
123 struct mv_u3d_ep_context *curr_ep_context;
124 int trb_complete, actual, remaining_length = 0;
125 int direction, ep_num;
127 u32 tmp, status, length;
129 curr_ep_context = &u3d->ep_context[index];
130 direction = index % 2;
134 actual = curr_req->req.length;
136 while (!list_empty(&curr_req->trb_list)) {
137 curr_trb = list_entry(curr_req->trb_list.next,
138 struct mv_u3d_trb, trb_list);
139 if (!curr_trb->trb_hw->ctrl.own) {
140 dev_err(u3d->dev, "%s, TRB own error!\n",
141 u3d->eps[index].name);
145 curr_trb->trb_hw->ctrl.own = 0;
146 if (direction == MV_U3D_EP_DIR_OUT) {
147 tmp = ioread32(&u3d->vuc_regs->rxst[ep_num].statuslo);
149 ioread32(&u3d->vuc_regs->rxst[ep_num].curdeqlo);
151 tmp = ioread32(&u3d->vuc_regs->txst[ep_num].statuslo);
153 ioread32(&u3d->vuc_regs->txst[ep_num].curdeqlo);
156 status = tmp >> MV_U3D_XFERSTATUS_COMPLETE_SHIFT;
157 length = tmp & MV_U3D_XFERSTATUS_TRB_LENGTH_MASK;
159 if (status == MV_U3D_COMPLETE_SUCCESS ||
160 (status == MV_U3D_COMPLETE_SHORT_PACKET &&
161 direction == MV_U3D_EP_DIR_OUT)) {
162 remaining_length += length;
163 actual -= remaining_length;
166 "complete_tr error: ep=%d %s: error = 0x%x\n",
167 index >> 1, direction ? "SEND" : "RECV",
172 list_del_init(&curr_trb->trb_list);
177 curr_req->req.actual = actual;
182 * mv_u3d_done() - retire a request; caller blocked irqs
183 * @status : request status to be set, only works when
184 * request is still in progress.
187 void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status)
188 __releases(&ep->udc->lock)
189 __acquires(&ep->udc->lock)
191 struct mv_u3d *u3d = (struct mv_u3d *)ep->u3d;
193 dev_dbg(u3d->dev, "mv_u3d_done: remove req->queue\n");
194 /* Removed the req from ep queue */
195 list_del_init(&req->queue);
197 /* req.status should be set as -EINPROGRESS in ep_queue() */
198 if (req->req.status == -EINPROGRESS)
199 req->req.status = status;
201 status = req->req.status;
203 /* Free trb for the request */
205 dma_pool_free(u3d->trb_pool,
206 req->trb_head->trb_hw, req->trb_head->trb_dma);
208 dma_unmap_single(ep->u3d->gadget.dev.parent,
209 (dma_addr_t)req->trb_head->trb_dma,
210 req->trb_count * sizeof(struct mv_u3d_trb_hw),
212 kfree(req->trb_head->trb_hw);
214 kfree(req->trb_head);
216 usb_gadget_unmap_request(&u3d->gadget, &req->req, mv_u3d_ep_dir(ep));
218 if (status && (status != -ESHUTDOWN)) {
219 dev_dbg(u3d->dev, "complete %s req %p stat %d len %u/%u",
220 ep->ep.name, &req->req, status,
221 req->req.actual, req->req.length);
224 spin_unlock(&ep->u3d->lock);
226 * complete() is from gadget layer,
227 * eg fsg->bulk_in_complete()
229 if (req->req.complete)
230 req->req.complete(&ep->ep, &req->req);
232 spin_lock(&ep->u3d->lock);
235 static int mv_u3d_queue_trb(struct mv_u3d_ep *ep, struct mv_u3d_req *req)
239 struct mv_u3d_ep_context *ep_context;
243 direction = mv_u3d_ep_dir(ep);
245 /* ep0 in and out share the same ep context slot 1*/
247 ep_context = &(u3d->ep_context[1]);
249 ep_context = &(u3d->ep_context[ep->ep_num * 2 + direction]);
251 /* check if the pipe is empty or not */
252 if (!list_empty(&ep->queue)) {
253 dev_err(u3d->dev, "add trb to non-empty queue!\n");
257 ep_context->rsvd0 = cpu_to_le32(1);
258 ep_context->rsvd1 = 0;
260 /* Configure the trb address and set the DCS bit.
261 * Both DCS bit and own bit in trb should be set.
263 ep_context->trb_addr_lo =
264 cpu_to_le32(req->trb_head->trb_dma | DCS_ENABLE);
265 ep_context->trb_addr_hi = 0;
267 /* Ensure that updates to the EP Context will
268 * occure before Ring Bell.
272 /* ring bell the ep */
277 + ((direction == MV_U3D_EP_DIR_OUT) ? 0 : 1);
279 iowrite32(tmp, &u3d->op_regs->doorbell);
284 static struct mv_u3d_trb *mv_u3d_build_trb_one(struct mv_u3d_req *req,
285 unsigned *length, dma_addr_t *dma)
288 unsigned int direction;
289 struct mv_u3d_trb *trb;
290 struct mv_u3d_trb_hw *trb_hw;
293 /* how big will this transfer be? */
294 *length = req->req.length - req->req.actual;
295 BUG_ON(*length > (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
299 trb = kzalloc(sizeof(*trb), GFP_ATOMIC);
304 * Be careful that no _GFP_HIGHMEM is set,
305 * or we can not use dma_to_virt
306 * cannot use GFP_KERNEL in spin lock
308 trb_hw = dma_pool_alloc(u3d->trb_pool, GFP_ATOMIC, dma);
312 "%s, dma_pool_alloc fail\n", __func__);
316 trb->trb_hw = trb_hw;
318 /* initialize buffer page pointers */
319 temp = (u32)(req->req.dma + req->req.actual);
321 trb_hw->buf_addr_lo = cpu_to_le32(temp);
322 trb_hw->buf_addr_hi = 0;
323 trb_hw->trb_len = cpu_to_le32(*length);
324 trb_hw->ctrl.own = 1;
326 if (req->ep->ep_num == 0)
327 trb_hw->ctrl.type = TYPE_DATA;
329 trb_hw->ctrl.type = TYPE_NORMAL;
331 req->req.actual += *length;
333 direction = mv_u3d_ep_dir(req->ep);
334 if (direction == MV_U3D_EP_DIR_IN)
335 trb_hw->ctrl.dir = 1;
337 trb_hw->ctrl.dir = 0;
339 /* Enable interrupt for the last trb of a request */
340 if (!req->req.no_interrupt)
341 trb_hw->ctrl.ioc = 1;
343 trb_hw->ctrl.chain = 0;
349 static int mv_u3d_build_trb_chain(struct mv_u3d_req *req, unsigned *length,
350 struct mv_u3d_trb *trb, int *is_last)
353 unsigned int direction;
356 /* how big will this transfer be? */
357 *length = min(req->req.length - req->req.actual,
358 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
364 /* initialize buffer page pointers */
365 temp = (u32)(req->req.dma + req->req.actual);
367 trb->trb_hw->buf_addr_lo = cpu_to_le32(temp);
368 trb->trb_hw->buf_addr_hi = 0;
369 trb->trb_hw->trb_len = cpu_to_le32(*length);
370 trb->trb_hw->ctrl.own = 1;
372 if (req->ep->ep_num == 0)
373 trb->trb_hw->ctrl.type = TYPE_DATA;
375 trb->trb_hw->ctrl.type = TYPE_NORMAL;
377 req->req.actual += *length;
379 direction = mv_u3d_ep_dir(req->ep);
380 if (direction == MV_U3D_EP_DIR_IN)
381 trb->trb_hw->ctrl.dir = 1;
383 trb->trb_hw->ctrl.dir = 0;
385 /* zlp is needed if req->req.zero is set */
387 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
391 } else if (req->req.length == req->req.actual)
396 /* Enable interrupt for the last trb of a request */
397 if (*is_last && !req->req.no_interrupt)
398 trb->trb_hw->ctrl.ioc = 1;
401 trb->trb_hw->ctrl.chain = 0;
403 trb->trb_hw->ctrl.chain = 1;
404 dev_dbg(u3d->dev, "chain trb\n");
412 /* generate TRB linked list for a request
413 * usb controller only supports continous trb chain,
414 * that trb structure physical address should be continous.
416 static int mv_u3d_req_to_trb(struct mv_u3d_req *req)
420 struct mv_u3d_trb *trb;
421 struct mv_u3d_trb_hw *trb_hw;
429 INIT_LIST_HEAD(&req->trb_list);
431 length = req->req.length - req->req.actual;
432 /* normally the request transfer length is less than 16KB.
433 * we use buil_trb_one() to optimize it.
435 if (length <= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER) {
436 trb = mv_u3d_build_trb_one(req, &count, &dma);
437 list_add_tail(&trb->trb_list, &req->trb_list);
442 trb_num = length / MV_U3D_EP_MAX_LENGTH_TRANSFER;
443 if (length % MV_U3D_EP_MAX_LENGTH_TRANSFER)
446 trb = kcalloc(trb_num, sizeof(*trb), GFP_ATOMIC);
450 trb_hw = kcalloc(trb_num, sizeof(*trb_hw), GFP_ATOMIC);
457 trb->trb_hw = trb_hw;
458 if (mv_u3d_build_trb_chain(req, &count,
461 "%s, mv_u3d_build_trb_chain fail\n",
466 list_add_tail(&trb->trb_list, &req->trb_list);
472 req->trb_head = list_entry(req->trb_list.next,
473 struct mv_u3d_trb, trb_list);
474 req->trb_head->trb_dma = dma_map_single(u3d->gadget.dev.parent,
475 req->trb_head->trb_hw,
476 trb_num * sizeof(*trb_hw),
486 mv_u3d_start_queue(struct mv_u3d_ep *ep)
488 struct mv_u3d *u3d = ep->u3d;
489 struct mv_u3d_req *req;
492 if (!list_empty(&ep->req_list) && !ep->processing)
493 req = list_entry(ep->req_list.next, struct mv_u3d_req, list);
499 /* set up dma mapping */
500 ret = usb_gadget_map_request(&u3d->gadget, &req->req,
505 req->req.status = -EINPROGRESS;
509 /* build trbs and push them to device queue */
510 if (!mv_u3d_req_to_trb(req)) {
511 ret = mv_u3d_queue_trb(ep, req);
518 dev_err(u3d->dev, "%s, mv_u3d_req_to_trb fail\n", __func__);
522 /* irq handler advances the queue */
524 list_add_tail(&req->queue, &ep->queue);
529 static int mv_u3d_ep_enable(struct usb_ep *_ep,
530 const struct usb_endpoint_descriptor *desc)
533 struct mv_u3d_ep *ep;
534 struct mv_u3d_ep_context *ep_context;
536 unsigned maxburst = 0;
537 u32 epxcr, direction;
539 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT)
542 ep = container_of(_ep, struct mv_u3d_ep, ep);
545 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN)
548 direction = mv_u3d_ep_dir(ep);
549 max = le16_to_cpu(desc->wMaxPacketSize);
553 maxburst = _ep->maxburst;
555 /* Get the endpoint context address */
556 ep_context = (struct mv_u3d_ep_context *)ep->ep_context;
558 /* Set the max burst size */
559 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
560 case USB_ENDPOINT_XFER_BULK:
563 "max burst should not be greater "
564 "than 16 on bulk ep\n");
566 _ep->maxburst = maxburst;
569 "maxburst: %d on bulk %s\n", maxburst, ep->name);
571 case USB_ENDPOINT_XFER_CONTROL:
572 /* control transfer only supports maxburst as one */
574 _ep->maxburst = maxburst;
576 case USB_ENDPOINT_XFER_INT:
579 "max burst should be 1 on int ep "
580 "if transfer size is not 1024\n");
582 _ep->maxburst = maxburst;
585 case USB_ENDPOINT_XFER_ISOC:
588 "max burst should be 1 on isoc ep "
589 "if transfer size is not 1024\n");
591 _ep->maxburst = maxburst;
598 ep->ep.maxpacket = max;
602 /* Enable the endpoint for Rx or Tx and set the endpoint type */
603 if (direction == MV_U3D_EP_DIR_OUT) {
604 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
605 epxcr |= MV_U3D_EPXCR_EP_INIT;
606 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
608 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
609 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
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].epxoutcr1);
617 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
618 epxcr |= MV_U3D_EPXCR_EP_INIT;
619 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
621 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
622 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
624 epxcr = ((max << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
625 | ((maxburst - 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
626 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
627 | (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
628 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
636 static int mv_u3d_ep_disable(struct usb_ep *_ep)
639 struct mv_u3d_ep *ep;
640 struct mv_u3d_ep_context *ep_context;
641 u32 epxcr, direction;
647 ep = container_of(_ep, struct mv_u3d_ep, ep);
653 /* Get the endpoint context address */
654 ep_context = ep->ep_context;
656 direction = mv_u3d_ep_dir(ep);
658 /* nuke all pending requests (does flush) */
659 spin_lock_irqsave(&u3d->lock, flags);
660 mv_u3d_nuke(ep, -ESHUTDOWN);
661 spin_unlock_irqrestore(&u3d->lock, flags);
663 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
664 if (direction == MV_U3D_EP_DIR_OUT) {
665 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
666 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
667 | USB_ENDPOINT_XFERTYPE_MASK);
668 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
670 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
671 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
672 | USB_ENDPOINT_XFERTYPE_MASK);
673 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
682 static struct usb_request *
683 mv_u3d_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
685 struct mv_u3d_req *req = NULL;
687 req = kzalloc(sizeof *req, gfp_flags);
691 INIT_LIST_HEAD(&req->queue);
696 static void mv_u3d_free_request(struct usb_ep *_ep, struct usb_request *_req)
698 struct mv_u3d_req *req = container_of(_req, struct mv_u3d_req, req);
703 static void mv_u3d_ep_fifo_flush(struct usb_ep *_ep)
707 struct mv_u3d_ep *ep = container_of(_ep, struct mv_u3d_ep, ep);
711 /* if endpoint is not enabled, cannot flush endpoint */
716 direction = mv_u3d_ep_dir(ep);
718 /* ep0 need clear bit after flushing fifo. */
720 if (direction == MV_U3D_EP_DIR_OUT) {
721 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
722 tmp |= MV_U3D_EPXCR_EP_FLUSH;
723 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
725 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
726 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
728 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
729 tmp |= MV_U3D_EPXCR_EP_FLUSH;
730 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
732 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
733 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
738 if (direction == MV_U3D_EP_DIR_OUT) {
739 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
740 tmp |= MV_U3D_EPXCR_EP_FLUSH;
741 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
743 /* Wait until flushing completed */
744 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
745 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0) &
746 MV_U3D_EPXCR_EP_FLUSH) {
748 * EP_FLUSH bit should be cleared to indicate this
749 * operation is complete
753 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
754 direction ? "in" : "out");
760 } else { /* EP_DIR_IN */
761 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
762 tmp |= MV_U3D_EPXCR_EP_FLUSH;
763 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
765 /* Wait until flushing completed */
766 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
767 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0) &
768 MV_U3D_EPXCR_EP_FLUSH) {
770 * EP_FLUSH bit should be cleared to indicate this
771 * operation is complete
775 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
776 direction ? "in" : "out");
785 /* queues (submits) an I/O request to an endpoint */
787 mv_u3d_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
789 struct mv_u3d_ep *ep;
790 struct mv_u3d_req *req;
793 int is_first_req = 0;
795 if (unlikely(!_ep || !_req))
798 ep = container_of(_ep, struct mv_u3d_ep, ep);
801 req = container_of(_req, struct mv_u3d_req, req);
804 && u3d->ep0_state == MV_U3D_STATUS_STAGE
806 dev_dbg(u3d->dev, "ep0 status stage\n");
807 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
811 dev_dbg(u3d->dev, "%s: %s, req: 0x%p\n",
812 __func__, _ep->name, req);
814 /* catch various bogus parameters */
815 if (!req->req.complete || !req->req.buf
816 || !list_empty(&req->queue)) {
818 "%s, bad params, _req: 0x%p,"
819 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
820 "list_empty: 0x%x\n",
822 req->req.complete, req->req.buf,
823 list_empty(&req->queue));
826 if (unlikely(!ep->ep.desc)) {
827 dev_err(u3d->dev, "%s, bad ep\n", __func__);
830 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
831 if (req->req.length > ep->ep.maxpacket)
835 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN) {
837 "bad params of driver/speed\n");
843 /* Software list handles usb request. */
844 spin_lock_irqsave(&ep->req_lock, flags);
845 is_first_req = list_empty(&ep->req_list);
846 list_add_tail(&req->list, &ep->req_list);
847 spin_unlock_irqrestore(&ep->req_lock, flags);
849 dev_dbg(u3d->dev, "list is not empty\n");
853 dev_dbg(u3d->dev, "call mv_u3d_start_queue from usb_ep_queue\n");
854 spin_lock_irqsave(&u3d->lock, flags);
855 mv_u3d_start_queue(ep);
856 spin_unlock_irqrestore(&u3d->lock, flags);
860 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
861 static int mv_u3d_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
863 struct mv_u3d_ep *ep;
864 struct mv_u3d_req *req;
866 struct mv_u3d_ep_context *ep_context;
867 struct mv_u3d_req *next_req;
875 ep = container_of(_ep, struct mv_u3d_ep, ep);
878 spin_lock_irqsave(&ep->u3d->lock, flags);
880 /* make sure it's actually queued on this endpoint */
881 list_for_each_entry(req, &ep->queue, queue) {
882 if (&req->req == _req)
885 if (&req->req != _req) {
890 /* The request is in progress, or completed but not dequeued */
891 if (ep->queue.next == &req->queue) {
892 _req->status = -ECONNRESET;
893 mv_u3d_ep_fifo_flush(_ep);
895 /* The request isn't the last request in this ep queue */
896 if (req->queue.next != &ep->queue) {
898 "it is the last request in this ep queue\n");
899 ep_context = ep->ep_context;
900 next_req = list_entry(req->queue.next,
901 struct mv_u3d_req, queue);
903 /* Point first TRB of next request to the EP context. */
904 iowrite32((unsigned long) next_req->trb_head,
905 &ep_context->trb_addr_lo);
907 struct mv_u3d_ep_context *ep_context;
908 ep_context = ep->ep_context;
909 ep_context->trb_addr_lo = 0;
910 ep_context->trb_addr_hi = 0;
916 mv_u3d_done(ep, req, -ECONNRESET);
918 /* remove the req from the ep req list */
919 if (!list_empty(&ep->req_list)) {
920 struct mv_u3d_req *curr_req;
921 curr_req = list_entry(ep->req_list.next,
922 struct mv_u3d_req, list);
923 if (curr_req == req) {
924 list_del_init(&req->list);
930 spin_unlock_irqrestore(&ep->u3d->lock, flags);
935 mv_u3d_ep_set_stall(struct mv_u3d *u3d, u8 ep_num, u8 direction, int stall)
938 struct mv_u3d_ep *ep = u3d->eps;
940 dev_dbg(u3d->dev, "%s\n", __func__);
941 if (direction == MV_U3D_EP_DIR_OUT) {
942 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
944 tmp |= MV_U3D_EPXCR_EP_HALT;
946 tmp &= ~MV_U3D_EPXCR_EP_HALT;
947 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
949 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
951 tmp |= MV_U3D_EPXCR_EP_HALT;
953 tmp &= ~MV_U3D_EPXCR_EP_HALT;
954 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
958 static int mv_u3d_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
960 struct mv_u3d_ep *ep;
961 unsigned long flags = 0;
965 ep = container_of(_ep, struct mv_u3d_ep, ep);
972 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
973 status = -EOPNOTSUPP;
978 * Attempt to halt IN ep will fail if any transfer requests
981 if (halt && (mv_u3d_ep_dir(ep) == MV_U3D_EP_DIR_IN)
982 && !list_empty(&ep->queue)) {
987 spin_lock_irqsave(&ep->u3d->lock, flags);
988 mv_u3d_ep_set_stall(u3d, ep->ep_num, mv_u3d_ep_dir(ep), halt);
993 spin_unlock_irqrestore(&ep->u3d->lock, flags);
996 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1001 static int mv_u3d_ep_set_halt(struct usb_ep *_ep, int halt)
1003 return mv_u3d_ep_set_halt_wedge(_ep, halt, 0);
1006 static int mv_u3d_ep_set_wedge(struct usb_ep *_ep)
1008 return mv_u3d_ep_set_halt_wedge(_ep, 1, 1);
1011 static struct usb_ep_ops mv_u3d_ep_ops = {
1012 .enable = mv_u3d_ep_enable,
1013 .disable = mv_u3d_ep_disable,
1015 .alloc_request = mv_u3d_alloc_request,
1016 .free_request = mv_u3d_free_request,
1018 .queue = mv_u3d_ep_queue,
1019 .dequeue = mv_u3d_ep_dequeue,
1021 .set_wedge = mv_u3d_ep_set_wedge,
1022 .set_halt = mv_u3d_ep_set_halt,
1023 .fifo_flush = mv_u3d_ep_fifo_flush,
1026 static void mv_u3d_controller_stop(struct mv_u3d *u3d)
1030 if (!u3d->clock_gating && u3d->vbus_valid_detect)
1031 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID,
1032 &u3d->vuc_regs->intrenable);
1034 iowrite32(0, &u3d->vuc_regs->intrenable);
1035 iowrite32(~0x0, &u3d->vuc_regs->endcomplete);
1036 iowrite32(~0x0, &u3d->vuc_regs->trbunderrun);
1037 iowrite32(~0x0, &u3d->vuc_regs->trbcomplete);
1038 iowrite32(~0x0, &u3d->vuc_regs->linkchange);
1039 iowrite32(0x1, &u3d->vuc_regs->setuplock);
1041 /* Reset the RUN bit in the command register to stop USB */
1042 tmp = ioread32(&u3d->op_regs->usbcmd);
1043 tmp &= ~MV_U3D_CMD_RUN_STOP;
1044 iowrite32(tmp, &u3d->op_regs->usbcmd);
1045 dev_dbg(u3d->dev, "after u3d_stop, USBCMD 0x%x\n",
1046 ioread32(&u3d->op_regs->usbcmd));
1049 static void mv_u3d_controller_start(struct mv_u3d *u3d)
1054 /* enable link LTSSM state machine */
1055 temp = ioread32(&u3d->vuc_regs->ltssm);
1056 temp |= MV_U3D_LTSSM_PHY_INIT_DONE;
1057 iowrite32(temp, &u3d->vuc_regs->ltssm);
1059 /* Enable interrupts */
1060 usbintr = MV_U3D_INTR_ENABLE_LINK_CHG | MV_U3D_INTR_ENABLE_TXDESC_ERR |
1061 MV_U3D_INTR_ENABLE_RXDESC_ERR | MV_U3D_INTR_ENABLE_TX_COMPLETE |
1062 MV_U3D_INTR_ENABLE_RX_COMPLETE | MV_U3D_INTR_ENABLE_SETUP |
1063 (u3d->vbus_valid_detect ? MV_U3D_INTR_ENABLE_VBUS_VALID : 0);
1064 iowrite32(usbintr, &u3d->vuc_regs->intrenable);
1066 /* Enable ctrl ep */
1067 iowrite32(0x1, &u3d->vuc_regs->ctrlepenable);
1069 /* Set the Run bit in the command register */
1070 iowrite32(MV_U3D_CMD_RUN_STOP, &u3d->op_regs->usbcmd);
1071 dev_dbg(u3d->dev, "after u3d_start, USBCMD 0x%x\n",
1072 ioread32(&u3d->op_regs->usbcmd));
1075 static int mv_u3d_controller_reset(struct mv_u3d *u3d)
1080 /* Stop the controller */
1081 tmp = ioread32(&u3d->op_regs->usbcmd);
1082 tmp &= ~MV_U3D_CMD_RUN_STOP;
1083 iowrite32(tmp, &u3d->op_regs->usbcmd);
1085 /* Reset the controller to get default values */
1086 iowrite32(MV_U3D_CMD_CTRL_RESET, &u3d->op_regs->usbcmd);
1088 /* wait for reset to complete */
1089 loops = LOOPS(MV_U3D_RESET_TIMEOUT);
1090 while (ioread32(&u3d->op_regs->usbcmd) & MV_U3D_CMD_CTRL_RESET) {
1093 "Wait for RESET completed TIMEOUT\n");
1100 /* Configure the Endpoint Context Address */
1101 iowrite32(u3d->ep_context_dma, &u3d->op_regs->dcbaapl);
1102 iowrite32(0, &u3d->op_regs->dcbaaph);
1107 static int mv_u3d_enable(struct mv_u3d *u3d)
1109 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1115 if (!u3d->clock_gating) {
1120 dev_dbg(u3d->dev, "enable u3d\n");
1121 clk_enable(u3d->clk);
1122 if (pdata->phy_init) {
1123 retval = pdata->phy_init(u3d->phy_regs);
1126 "init phy error %d\n", retval);
1127 clk_disable(u3d->clk);
1136 static void mv_u3d_disable(struct mv_u3d *u3d)
1138 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1139 if (u3d->clock_gating && u3d->active) {
1140 dev_dbg(u3d->dev, "disable u3d\n");
1141 if (pdata->phy_deinit)
1142 pdata->phy_deinit(u3d->phy_regs);
1143 clk_disable(u3d->clk);
1148 static int mv_u3d_vbus_session(struct usb_gadget *gadget, int is_active)
1151 unsigned long flags;
1154 u3d = container_of(gadget, struct mv_u3d, gadget);
1156 spin_lock_irqsave(&u3d->lock, flags);
1158 u3d->vbus_active = (is_active != 0);
1159 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1160 __func__, u3d->softconnect, u3d->vbus_active);
1162 * 1. external VBUS detect: we can disable/enable clock on demand.
1163 * 2. UDC VBUS detect: we have to enable clock all the time.
1164 * 3. No VBUS detect: we have to enable clock all the time.
1166 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1167 retval = mv_u3d_enable(u3d);
1170 * after clock is disabled, we lost all the register
1171 * context. We have to re-init registers
1173 mv_u3d_controller_reset(u3d);
1174 mv_u3d_ep0_reset(u3d);
1175 mv_u3d_controller_start(u3d);
1177 } else if (u3d->driver && u3d->softconnect) {
1181 /* stop all the transfer in queue*/
1182 mv_u3d_stop_activity(u3d, u3d->driver);
1183 mv_u3d_controller_stop(u3d);
1184 mv_u3d_disable(u3d);
1188 spin_unlock_irqrestore(&u3d->lock, flags);
1192 /* constrain controller's VBUS power usage
1193 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1194 * reporting how much power the device may consume. For example, this
1195 * could affect how quickly batteries are recharged.
1197 * Returns zero on success, else negative errno.
1199 static int mv_u3d_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1201 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1208 static int mv_u3d_pullup(struct usb_gadget *gadget, int is_on)
1210 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1211 unsigned long flags;
1214 spin_lock_irqsave(&u3d->lock, flags);
1216 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1217 __func__, u3d->softconnect, u3d->vbus_active);
1218 u3d->softconnect = (is_on != 0);
1219 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1220 retval = mv_u3d_enable(u3d);
1223 * after clock is disabled, we lost all the register
1224 * context. We have to re-init registers
1226 mv_u3d_controller_reset(u3d);
1227 mv_u3d_ep0_reset(u3d);
1228 mv_u3d_controller_start(u3d);
1230 } else if (u3d->driver && u3d->vbus_active) {
1231 /* stop all the transfer in queue*/
1232 mv_u3d_stop_activity(u3d, u3d->driver);
1233 mv_u3d_controller_stop(u3d);
1234 mv_u3d_disable(u3d);
1237 spin_unlock_irqrestore(&u3d->lock, flags);
1242 static int mv_u3d_start(struct usb_gadget *g,
1243 struct usb_gadget_driver *driver)
1245 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1246 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1247 unsigned long flags;
1252 spin_lock_irqsave(&u3d->lock, flags);
1254 if (!u3d->clock_gating) {
1255 clk_enable(u3d->clk);
1256 if (pdata->phy_init)
1257 pdata->phy_init(u3d->phy_regs);
1260 /* hook up the driver ... */
1261 driver->driver.bus = NULL;
1262 u3d->driver = driver;
1264 u3d->ep0_dir = USB_DIR_OUT;
1266 spin_unlock_irqrestore(&u3d->lock, flags);
1268 u3d->vbus_valid_detect = 1;
1273 static int mv_u3d_stop(struct usb_gadget *g,
1274 struct usb_gadget_driver *driver)
1276 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1277 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1278 unsigned long flags;
1280 u3d->vbus_valid_detect = 0;
1281 spin_lock_irqsave(&u3d->lock, flags);
1283 /* enable clock to access controller register */
1284 clk_enable(u3d->clk);
1285 if (pdata->phy_init)
1286 pdata->phy_init(u3d->phy_regs);
1288 mv_u3d_controller_stop(u3d);
1289 /* stop all usb activities */
1290 u3d->gadget.speed = USB_SPEED_UNKNOWN;
1291 mv_u3d_stop_activity(u3d, driver);
1292 mv_u3d_disable(u3d);
1294 if (pdata->phy_deinit)
1295 pdata->phy_deinit(u3d->phy_regs);
1296 clk_disable(u3d->clk);
1298 spin_unlock_irqrestore(&u3d->lock, flags);
1305 /* device controller usb_gadget_ops structure */
1306 static const struct usb_gadget_ops mv_u3d_ops = {
1307 /* notify controller that VBUS is powered or not */
1308 .vbus_session = mv_u3d_vbus_session,
1310 /* constrain controller's VBUS power usage */
1311 .vbus_draw = mv_u3d_vbus_draw,
1313 .pullup = mv_u3d_pullup,
1314 .udc_start = mv_u3d_start,
1315 .udc_stop = mv_u3d_stop,
1318 static int mv_u3d_eps_init(struct mv_u3d *u3d)
1320 struct mv_u3d_ep *ep;
1324 /* initialize ep0, ep0 in/out use eps[1] */
1327 strncpy(ep->name, "ep0", sizeof(ep->name));
1328 ep->ep.name = ep->name;
1329 ep->ep.ops = &mv_u3d_ep_ops;
1331 usb_ep_set_maxpacket_limit(&ep->ep, MV_U3D_EP0_MAX_PKT_SIZE);
1333 ep->ep.desc = &mv_u3d_ep0_desc;
1334 INIT_LIST_HEAD(&ep->queue);
1335 INIT_LIST_HEAD(&ep->req_list);
1336 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1338 /* add ep0 ep_context */
1339 ep->ep_context = &u3d->ep_context[1];
1341 /* initialize other endpoints */
1342 for (i = 2; i < u3d->max_eps * 2; i++) {
1345 snprintf(name, sizeof(name), "ep%din", i >> 1);
1346 ep->direction = MV_U3D_EP_DIR_IN;
1348 snprintf(name, sizeof(name), "ep%dout", i >> 1);
1349 ep->direction = MV_U3D_EP_DIR_OUT;
1352 strncpy(ep->name, name, sizeof(ep->name));
1353 ep->ep.name = ep->name;
1355 ep->ep.ops = &mv_u3d_ep_ops;
1356 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
1359 INIT_LIST_HEAD(&ep->queue);
1360 list_add_tail(&ep->ep.ep_list, &u3d->gadget.ep_list);
1362 INIT_LIST_HEAD(&ep->req_list);
1363 spin_lock_init(&ep->req_lock);
1364 ep->ep_context = &u3d->ep_context[i];
1370 /* delete all endpoint requests, called with spinlock held */
1371 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status)
1373 /* endpoint fifo flush */
1374 mv_u3d_ep_fifo_flush(&ep->ep);
1376 while (!list_empty(&ep->queue)) {
1377 struct mv_u3d_req *req = NULL;
1378 req = list_entry(ep->queue.next, struct mv_u3d_req, queue);
1379 mv_u3d_done(ep, req, status);
1383 /* stop all USB activities */
1385 void mv_u3d_stop_activity(struct mv_u3d *u3d, struct usb_gadget_driver *driver)
1387 struct mv_u3d_ep *ep;
1389 mv_u3d_nuke(&u3d->eps[1], -ESHUTDOWN);
1391 list_for_each_entry(ep, &u3d->gadget.ep_list, ep.ep_list) {
1392 mv_u3d_nuke(ep, -ESHUTDOWN);
1395 /* report disconnect; the driver is already quiesced */
1397 spin_unlock(&u3d->lock);
1398 driver->disconnect(&u3d->gadget);
1399 spin_lock(&u3d->lock);
1403 static void mv_u3d_irq_process_error(struct mv_u3d *u3d)
1405 /* Increment the error count */
1407 dev_err(u3d->dev, "%s\n", __func__);
1410 static void mv_u3d_irq_process_link_change(struct mv_u3d *u3d)
1414 linkchange = ioread32(&u3d->vuc_regs->linkchange);
1415 iowrite32(linkchange, &u3d->vuc_regs->linkchange);
1417 dev_dbg(u3d->dev, "linkchange: 0x%x\n", linkchange);
1419 if (linkchange & MV_U3D_LINK_CHANGE_LINK_UP) {
1420 dev_dbg(u3d->dev, "link up: ltssm state: 0x%x\n",
1421 ioread32(&u3d->vuc_regs->ltssmstate));
1423 u3d->usb_state = USB_STATE_DEFAULT;
1424 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1425 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
1428 u3d->gadget.speed = USB_SPEED_SUPER;
1431 if (linkchange & MV_U3D_LINK_CHANGE_SUSPEND) {
1432 dev_dbg(u3d->dev, "link suspend\n");
1433 u3d->resume_state = u3d->usb_state;
1434 u3d->usb_state = USB_STATE_SUSPENDED;
1437 if (linkchange & MV_U3D_LINK_CHANGE_RESUME) {
1438 dev_dbg(u3d->dev, "link resume\n");
1439 u3d->usb_state = u3d->resume_state;
1440 u3d->resume_state = 0;
1443 if (linkchange & MV_U3D_LINK_CHANGE_WRESET) {
1444 dev_dbg(u3d->dev, "warm reset\n");
1445 u3d->usb_state = USB_STATE_POWERED;
1448 if (linkchange & MV_U3D_LINK_CHANGE_HRESET) {
1449 dev_dbg(u3d->dev, "hot reset\n");
1450 u3d->usb_state = USB_STATE_DEFAULT;
1453 if (linkchange & MV_U3D_LINK_CHANGE_INACT)
1454 dev_dbg(u3d->dev, "inactive\n");
1456 if (linkchange & MV_U3D_LINK_CHANGE_DISABLE_AFTER_U0)
1457 dev_dbg(u3d->dev, "ss.disabled\n");
1459 if (linkchange & MV_U3D_LINK_CHANGE_VBUS_INVALID) {
1460 dev_dbg(u3d->dev, "vbus invalid\n");
1461 u3d->usb_state = USB_STATE_ATTACHED;
1462 u3d->vbus_valid_detect = 1;
1463 /* if external vbus detect is not supported,
1464 * we handle it here.
1467 spin_unlock(&u3d->lock);
1468 mv_u3d_vbus_session(&u3d->gadget, 0);
1469 spin_lock(&u3d->lock);
1474 static void mv_u3d_ch9setaddress(struct mv_u3d *u3d,
1475 struct usb_ctrlrequest *setup)
1479 if (u3d->usb_state != USB_STATE_DEFAULT) {
1481 "%s, cannot setaddr in this state (%d)\n",
1482 __func__, u3d->usb_state);
1486 u3d->dev_addr = (u8)setup->wValue;
1488 dev_dbg(u3d->dev, "%s: 0x%x\n", __func__, u3d->dev_addr);
1490 if (u3d->dev_addr > 127) {
1492 "%s, u3d address is wrong (out of range)\n", __func__);
1497 /* update usb state */
1498 u3d->usb_state = USB_STATE_ADDRESS;
1500 /* set the new address */
1501 tmp = ioread32(&u3d->vuc_regs->devaddrtiebrkr);
1503 tmp |= (u32)u3d->dev_addr;
1504 iowrite32(tmp, &u3d->vuc_regs->devaddrtiebrkr);
1508 mv_u3d_ep0_stall(u3d);
1511 static int mv_u3d_is_set_configuration(struct usb_ctrlrequest *setup)
1513 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1514 if (setup->bRequest == USB_REQ_SET_CONFIGURATION)
1520 static void mv_u3d_handle_setup_packet(struct mv_u3d *u3d, u8 ep_num,
1521 struct usb_ctrlrequest *setup)
1522 __releases(&u3c->lock)
1523 __acquires(&u3c->lock)
1525 bool delegate = false;
1527 mv_u3d_nuke(&u3d->eps[ep_num * 2 + MV_U3D_EP_DIR_IN], -ESHUTDOWN);
1529 dev_dbg(u3d->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1530 setup->bRequestType, setup->bRequest,
1531 setup->wValue, setup->wIndex, setup->wLength);
1533 /* We process some stardard setup requests here */
1534 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1535 switch (setup->bRequest) {
1536 case USB_REQ_GET_STATUS:
1540 case USB_REQ_SET_ADDRESS:
1541 mv_u3d_ch9setaddress(u3d, setup);
1544 case USB_REQ_CLEAR_FEATURE:
1548 case USB_REQ_SET_FEATURE:
1558 /* delegate USB standard requests to the gadget driver */
1559 if (delegate == true) {
1560 /* USB requests handled by gadget */
1561 if (setup->wLength) {
1562 /* DATA phase from gadget, STATUS phase from u3d */
1563 u3d->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1564 ? MV_U3D_EP_DIR_IN : MV_U3D_EP_DIR_OUT;
1565 spin_unlock(&u3d->lock);
1566 if (u3d->driver->setup(&u3d->gadget,
1567 &u3d->local_setup_buff) < 0) {
1568 dev_err(u3d->dev, "setup error!\n");
1569 mv_u3d_ep0_stall(u3d);
1571 spin_lock(&u3d->lock);
1573 /* no DATA phase, STATUS phase from gadget */
1574 u3d->ep0_dir = MV_U3D_EP_DIR_IN;
1575 u3d->ep0_state = MV_U3D_STATUS_STAGE;
1576 spin_unlock(&u3d->lock);
1577 if (u3d->driver->setup(&u3d->gadget,
1578 &u3d->local_setup_buff) < 0)
1579 mv_u3d_ep0_stall(u3d);
1580 spin_lock(&u3d->lock);
1583 if (mv_u3d_is_set_configuration(setup)) {
1584 dev_dbg(u3d->dev, "u3d configured\n");
1585 u3d->usb_state = USB_STATE_CONFIGURED;
1590 static void mv_u3d_get_setup_data(struct mv_u3d *u3d, u8 ep_num, u8 *buffer_ptr)
1592 struct mv_u3d_ep_context *epcontext;
1594 epcontext = &u3d->ep_context[ep_num * 2 + MV_U3D_EP_DIR_IN];
1596 /* Copy the setup packet to local buffer */
1597 memcpy(buffer_ptr, (u8 *) &epcontext->setup_buffer, 8);
1600 static void mv_u3d_irq_process_setup(struct mv_u3d *u3d)
1603 /* Process all Setup packet received interrupts */
1604 tmp = ioread32(&u3d->vuc_regs->setuplock);
1606 for (i = 0; i < u3d->max_eps; i++) {
1607 if (tmp & (1 << i)) {
1608 mv_u3d_get_setup_data(u3d, i,
1609 (u8 *)(&u3d->local_setup_buff));
1610 mv_u3d_handle_setup_packet(u3d, i,
1611 &u3d->local_setup_buff);
1616 iowrite32(tmp, &u3d->vuc_regs->setuplock);
1619 static void mv_u3d_irq_process_tr_complete(struct mv_u3d *u3d)
1622 int i, ep_num = 0, direction = 0;
1623 struct mv_u3d_ep *curr_ep;
1624 struct mv_u3d_req *curr_req, *temp_req;
1627 tmp = ioread32(&u3d->vuc_regs->endcomplete);
1629 dev_dbg(u3d->dev, "tr_complete: ep: 0x%x\n", tmp);
1632 iowrite32(tmp, &u3d->vuc_regs->endcomplete);
1634 for (i = 0; i < u3d->max_eps * 2; i++) {
1638 bit_pos = 1 << (ep_num + 16 * direction);
1640 if (!(bit_pos & tmp))
1644 curr_ep = &u3d->eps[1];
1646 curr_ep = &u3d->eps[i];
1648 /* remove req out of ep request list after completion */
1649 dev_dbg(u3d->dev, "tr comp: check req_list\n");
1650 spin_lock(&curr_ep->req_lock);
1651 if (!list_empty(&curr_ep->req_list)) {
1652 struct mv_u3d_req *req;
1653 req = list_entry(curr_ep->req_list.next,
1654 struct mv_u3d_req, list);
1655 list_del_init(&req->list);
1656 curr_ep->processing = 0;
1658 spin_unlock(&curr_ep->req_lock);
1660 /* process the req queue until an uncomplete request */
1661 list_for_each_entry_safe(curr_req, temp_req,
1662 &curr_ep->queue, queue) {
1663 status = mv_u3d_process_ep_req(u3d, i, curr_req);
1666 /* write back status to req */
1667 curr_req->req.status = status;
1669 /* ep0 request completion */
1671 mv_u3d_done(curr_ep, curr_req, 0);
1674 mv_u3d_done(curr_ep, curr_req, status);
1678 dev_dbg(u3d->dev, "call mv_u3d_start_queue from ep complete\n");
1679 mv_u3d_start_queue(curr_ep);
1683 static irqreturn_t mv_u3d_irq(int irq, void *dev)
1685 struct mv_u3d *u3d = (struct mv_u3d *)dev;
1690 spin_lock(&u3d->lock);
1692 status = ioread32(&u3d->vuc_regs->intrcause);
1693 intr = ioread32(&u3d->vuc_regs->intrenable);
1697 spin_unlock(&u3d->lock);
1698 dev_err(u3d->dev, "irq error!\n");
1702 if (status & MV_U3D_USBINT_VBUS_VALID) {
1703 bridgesetting = ioread32(&u3d->vuc_regs->bridgesetting);
1704 if (bridgesetting & MV_U3D_BRIDGE_SETTING_VBUS_VALID) {
1705 /* write vbus valid bit of bridge setting to clear */
1706 bridgesetting = MV_U3D_BRIDGE_SETTING_VBUS_VALID;
1707 iowrite32(bridgesetting, &u3d->vuc_regs->bridgesetting);
1708 dev_dbg(u3d->dev, "vbus valid\n");
1710 u3d->usb_state = USB_STATE_POWERED;
1711 u3d->vbus_valid_detect = 0;
1712 /* if external vbus detect is not supported,
1713 * we handle it here.
1716 spin_unlock(&u3d->lock);
1717 mv_u3d_vbus_session(&u3d->gadget, 1);
1718 spin_lock(&u3d->lock);
1721 dev_err(u3d->dev, "vbus bit is not set\n");
1724 /* RX data is already in the 16KB FIFO.*/
1725 if (status & MV_U3D_USBINT_UNDER_RUN) {
1726 trbunderrun = ioread32(&u3d->vuc_regs->trbunderrun);
1727 dev_err(u3d->dev, "under run, ep%d\n", trbunderrun);
1728 iowrite32(trbunderrun, &u3d->vuc_regs->trbunderrun);
1729 mv_u3d_irq_process_error(u3d);
1732 if (status & (MV_U3D_USBINT_RXDESC_ERR | MV_U3D_USBINT_TXDESC_ERR)) {
1733 /* write one to clear */
1734 iowrite32(status & (MV_U3D_USBINT_RXDESC_ERR
1735 | MV_U3D_USBINT_TXDESC_ERR),
1736 &u3d->vuc_regs->intrcause);
1737 dev_err(u3d->dev, "desc err 0x%x\n", status);
1738 mv_u3d_irq_process_error(u3d);
1741 if (status & MV_U3D_USBINT_LINK_CHG)
1742 mv_u3d_irq_process_link_change(u3d);
1744 if (status & MV_U3D_USBINT_TX_COMPLETE)
1745 mv_u3d_irq_process_tr_complete(u3d);
1747 if (status & MV_U3D_USBINT_RX_COMPLETE)
1748 mv_u3d_irq_process_tr_complete(u3d);
1750 if (status & MV_U3D_USBINT_SETUP)
1751 mv_u3d_irq_process_setup(u3d);
1753 spin_unlock(&u3d->lock);
1757 static int mv_u3d_remove(struct platform_device *dev)
1759 struct mv_u3d *u3d = platform_get_drvdata(dev);
1761 BUG_ON(u3d == NULL);
1763 usb_del_gadget_udc(&u3d->gadget);
1765 /* free memory allocated in probe */
1767 dma_pool_destroy(u3d->trb_pool);
1769 if (u3d->ep_context)
1770 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1771 u3d->ep_context, u3d->ep_context_dma);
1776 free_irq(u3d->irq, u3d);
1779 iounmap(u3d->cap_regs);
1780 u3d->cap_regs = NULL;
1782 kfree(u3d->status_req);
1791 static int mv_u3d_probe(struct platform_device *dev)
1793 struct mv_u3d *u3d = NULL;
1794 struct mv_usb_platform_data *pdata = dev_get_platdata(&dev->dev);
1799 if (!dev_get_platdata(&dev->dev)) {
1800 dev_err(&dev->dev, "missing platform_data\n");
1805 u3d = kzalloc(sizeof(*u3d), GFP_KERNEL);
1808 goto err_alloc_private;
1811 spin_lock_init(&u3d->lock);
1813 platform_set_drvdata(dev, u3d);
1815 u3d->dev = &dev->dev;
1816 u3d->vbus = pdata->vbus;
1818 u3d->clk = clk_get(&dev->dev, NULL);
1819 if (IS_ERR(u3d->clk)) {
1820 retval = PTR_ERR(u3d->clk);
1824 r = platform_get_resource_byname(dev, IORESOURCE_MEM, "capregs");
1826 dev_err(&dev->dev, "no I/O memory resource defined\n");
1828 goto err_get_cap_regs;
1831 u3d->cap_regs = (struct mv_u3d_cap_regs __iomem *)
1832 ioremap(r->start, resource_size(r));
1833 if (!u3d->cap_regs) {
1834 dev_err(&dev->dev, "failed to map I/O memory\n");
1836 goto err_map_cap_regs;
1838 dev_dbg(&dev->dev, "cap_regs address: 0x%lx/0x%lx\n",
1839 (unsigned long) r->start,
1840 (unsigned long) u3d->cap_regs);
1843 /* we will access controller register, so enable the u3d controller */
1844 clk_enable(u3d->clk);
1846 if (pdata->phy_init) {
1847 retval = pdata->phy_init(u3d->phy_regs);
1849 dev_err(&dev->dev, "init phy error %d\n", retval);
1850 goto err_u3d_enable;
1854 u3d->op_regs = (struct mv_u3d_op_regs __iomem *)(u3d->cap_regs
1855 + MV_U3D_USB3_OP_REGS_OFFSET);
1857 u3d->vuc_regs = (struct mv_u3d_vuc_regs __iomem *)(u3d->cap_regs
1858 + ioread32(&u3d->cap_regs->vuoff));
1863 * some platform will use usb to download image, it may not disconnect
1864 * usb gadget before loading kernel. So first stop u3d here.
1866 mv_u3d_controller_stop(u3d);
1867 iowrite32(0xFFFFFFFF, &u3d->vuc_regs->intrcause);
1869 if (pdata->phy_deinit)
1870 pdata->phy_deinit(u3d->phy_regs);
1871 clk_disable(u3d->clk);
1873 size = u3d->max_eps * sizeof(struct mv_u3d_ep_context) * 2;
1874 size = (size + MV_U3D_EP_CONTEXT_ALIGNMENT - 1)
1875 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT - 1);
1876 u3d->ep_context = dma_alloc_coherent(&dev->dev, size,
1877 &u3d->ep_context_dma, GFP_KERNEL);
1878 if (!u3d->ep_context) {
1879 dev_err(&dev->dev, "allocate ep context memory failed\n");
1881 goto err_alloc_ep_context;
1883 u3d->ep_context_size = size;
1885 /* create TRB dma_pool resource */
1886 u3d->trb_pool = dma_pool_create("u3d_trb",
1888 sizeof(struct mv_u3d_trb_hw),
1889 MV_U3D_TRB_ALIGNMENT,
1890 MV_U3D_DMA_BOUNDARY);
1892 if (!u3d->trb_pool) {
1894 goto err_alloc_trb_pool;
1897 size = u3d->max_eps * sizeof(struct mv_u3d_ep) * 2;
1898 u3d->eps = kzalloc(size, GFP_KERNEL);
1904 /* initialize ep0 status request structure */
1905 u3d->status_req = kzalloc(sizeof(struct mv_u3d_req) + 8, GFP_KERNEL);
1906 if (!u3d->status_req) {
1908 goto err_alloc_status_req;
1910 INIT_LIST_HEAD(&u3d->status_req->queue);
1912 /* allocate a small amount of memory to get valid address */
1913 u3d->status_req->req.buf = (char *)u3d->status_req
1914 + sizeof(struct mv_u3d_req);
1915 u3d->status_req->req.dma = virt_to_phys(u3d->status_req->req.buf);
1917 u3d->resume_state = USB_STATE_NOTATTACHED;
1918 u3d->usb_state = USB_STATE_ATTACHED;
1919 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1920 u3d->remote_wakeup = 0;
1922 r = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1924 dev_err(&dev->dev, "no IRQ resource defined\n");
1928 u3d->irq = r->start;
1929 if (request_irq(u3d->irq, mv_u3d_irq,
1930 IRQF_SHARED, driver_name, u3d)) {
1932 dev_err(&dev->dev, "Request irq %d for u3d failed\n",
1935 goto err_request_irq;
1938 /* initialize gadget structure */
1939 u3d->gadget.ops = &mv_u3d_ops; /* usb_gadget_ops */
1940 u3d->gadget.ep0 = &u3d->eps[1].ep; /* gadget ep0 */
1941 INIT_LIST_HEAD(&u3d->gadget.ep_list); /* ep_list */
1942 u3d->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
1944 /* the "gadget" abstracts/virtualizes the controller */
1945 u3d->gadget.name = driver_name; /* gadget name */
1947 mv_u3d_eps_init(u3d);
1949 /* external vbus detection */
1951 u3d->clock_gating = 1;
1952 dev_err(&dev->dev, "external vbus detection\n");
1955 if (!u3d->clock_gating)
1956 u3d->vbus_active = 1;
1958 /* enable usb3 controller vbus detection */
1959 u3d->vbus_valid_detect = 1;
1961 retval = usb_add_gadget_udc(&dev->dev, &u3d->gadget);
1963 goto err_unregister;
1965 dev_dbg(&dev->dev, "successful probe usb3 device %s clock gating.\n",
1966 u3d->clock_gating ? "with" : "without");
1971 free_irq(u3d->irq, u3d);
1974 kfree(u3d->status_req);
1975 err_alloc_status_req:
1978 dma_pool_destroy(u3d->trb_pool);
1980 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1981 u3d->ep_context, u3d->ep_context_dma);
1982 err_alloc_ep_context:
1983 if (pdata->phy_deinit)
1984 pdata->phy_deinit(u3d->phy_regs);
1985 clk_disable(u3d->clk);
1987 iounmap(u3d->cap_regs);
1998 #ifdef CONFIG_PM_SLEEP
1999 static int mv_u3d_suspend(struct device *dev)
2001 struct mv_u3d *u3d = dev_get_drvdata(dev);
2004 * only cable is unplugged, usb can suspend.
2005 * So do not care about clock_gating == 1, it is handled by
2008 if (!u3d->clock_gating) {
2009 mv_u3d_controller_stop(u3d);
2011 spin_lock_irq(&u3d->lock);
2012 /* stop all usb activities */
2013 mv_u3d_stop_activity(u3d, u3d->driver);
2014 spin_unlock_irq(&u3d->lock);
2016 mv_u3d_disable(u3d);
2022 static int mv_u3d_resume(struct device *dev)
2024 struct mv_u3d *u3d = dev_get_drvdata(dev);
2027 if (!u3d->clock_gating) {
2028 retval = mv_u3d_enable(u3d);
2032 if (u3d->driver && u3d->softconnect) {
2033 mv_u3d_controller_reset(u3d);
2034 mv_u3d_ep0_reset(u3d);
2035 mv_u3d_controller_start(u3d);
2043 static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops, mv_u3d_suspend, mv_u3d_resume);
2045 static void mv_u3d_shutdown(struct platform_device *dev)
2047 struct mv_u3d *u3d = platform_get_drvdata(dev);
2050 tmp = ioread32(&u3d->op_regs->usbcmd);
2051 tmp &= ~MV_U3D_CMD_RUN_STOP;
2052 iowrite32(tmp, &u3d->op_regs->usbcmd);
2055 static struct platform_driver mv_u3d_driver = {
2056 .probe = mv_u3d_probe,
2057 .remove = mv_u3d_remove,
2058 .shutdown = mv_u3d_shutdown,
2060 .owner = THIS_MODULE,
2062 .pm = &mv_u3d_pm_ops,
2066 module_platform_driver(mv_u3d_driver);
2067 MODULE_ALIAS("platform:mv-u3d");
2068 MODULE_DESCRIPTION(DRIVER_DESC);
2070 MODULE_LICENSE("GPL");