2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmapool.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/ioport.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/timer.h>
20 #include <linux/list.h>
21 #include <linux/notifier.h>
22 #include <linux/interrupt.h>
23 #include <linux/moduleparam.h>
24 #include <linux/device.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
29 #include <linux/irq.h>
30 #include <linux/platform_device.h>
31 #include <linux/platform_data/mv_usb.h>
32 #include <linux/clk.h>
36 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
38 static const char driver_name[] = "mv_u3d";
39 static const char driver_desc[] = DRIVER_DESC;
41 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status);
42 static void mv_u3d_stop_activity(struct mv_u3d *u3d,
43 struct usb_gadget_driver *driver);
45 /* for endpoint 0 operations */
46 static const struct usb_endpoint_descriptor mv_u3d_ep0_desc = {
47 .bLength = USB_DT_ENDPOINT_SIZE,
48 .bDescriptorType = USB_DT_ENDPOINT,
49 .bEndpointAddress = 0,
50 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
51 .wMaxPacketSize = MV_U3D_EP0_MAX_PKT_SIZE,
54 static void mv_u3d_ep0_reset(struct mv_u3d *u3d)
60 for (i = 0; i < 2; i++) {
64 /* ep0 ep context, ep0 in and out share the same ep context */
65 ep->ep_context = &u3d->ep_context[1];
68 /* reset ep state machine */
70 epxcr = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
71 epxcr |= MV_U3D_EPXCR_EP_INIT;
72 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr0);
74 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
75 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr0);
77 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
78 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
79 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
80 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
81 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
82 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxoutcr1);
85 epxcr = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
86 epxcr |= MV_U3D_EPXCR_EP_INIT;
87 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr0);
89 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
90 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr0);
92 epxcr = ((MV_U3D_EP0_MAX_PKT_SIZE
93 << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
94 | (1 << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
95 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
96 | MV_U3D_EPXCR_EP_TYPE_CONTROL);
97 iowrite32(epxcr, &u3d->vuc_regs->epcr[0].epxincr1);
100 static void mv_u3d_ep0_stall(struct mv_u3d *u3d)
103 dev_dbg(u3d->dev, "%s\n", __func__);
105 /* set TX and RX to stall */
106 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
107 tmp |= MV_U3D_EPXCR_EP_HALT;
108 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
110 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
111 tmp |= MV_U3D_EPXCR_EP_HALT;
112 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
114 /* update ep0 state */
115 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
116 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
119 static int mv_u3d_process_ep_req(struct mv_u3d *u3d, int index,
120 struct mv_u3d_req *curr_req)
122 struct mv_u3d_trb *curr_trb;
123 dma_addr_t cur_deq_lo;
124 struct mv_u3d_ep_context *curr_ep_context;
125 int trb_complete, actual, remaining_length = 0;
126 int direction, ep_num;
128 u32 tmp, status, length;
130 curr_ep_context = &u3d->ep_context[index];
131 direction = index % 2;
135 actual = curr_req->req.length;
137 while (!list_empty(&curr_req->trb_list)) {
138 curr_trb = list_entry(curr_req->trb_list.next,
139 struct mv_u3d_trb, trb_list);
140 if (!curr_trb->trb_hw->ctrl.own) {
141 dev_err(u3d->dev, "%s, TRB own error!\n",
142 u3d->eps[index].name);
146 curr_trb->trb_hw->ctrl.own = 0;
147 if (direction == MV_U3D_EP_DIR_OUT) {
148 tmp = ioread32(&u3d->vuc_regs->rxst[ep_num].statuslo);
150 ioread32(&u3d->vuc_regs->rxst[ep_num].curdeqlo);
152 tmp = ioread32(&u3d->vuc_regs->txst[ep_num].statuslo);
154 ioread32(&u3d->vuc_regs->txst[ep_num].curdeqlo);
157 status = tmp >> MV_U3D_XFERSTATUS_COMPLETE_SHIFT;
158 length = tmp & MV_U3D_XFERSTATUS_TRB_LENGTH_MASK;
160 if (status == MV_U3D_COMPLETE_SUCCESS ||
161 (status == MV_U3D_COMPLETE_SHORT_PACKET &&
162 direction == MV_U3D_EP_DIR_OUT)) {
163 remaining_length += length;
164 actual -= remaining_length;
167 "complete_tr error: ep=%d %s: error = 0x%x\n",
168 index >> 1, direction ? "SEND" : "RECV",
173 list_del_init(&curr_trb->trb_list);
178 curr_req->req.actual = actual;
183 * mv_u3d_done() - retire a request; caller blocked irqs
184 * @status : request status to be set, only works when
185 * request is still in progress.
188 void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status)
189 __releases(&ep->udc->lock)
190 __acquires(&ep->udc->lock)
192 struct mv_u3d *u3d = (struct mv_u3d *)ep->u3d;
194 dev_dbg(u3d->dev, "mv_u3d_done: remove req->queue\n");
195 /* Removed the req from ep queue */
196 list_del_init(&req->queue);
198 /* req.status should be set as -EINPROGRESS in ep_queue() */
199 if (req->req.status == -EINPROGRESS)
200 req->req.status = status;
202 status = req->req.status;
204 /* Free trb for the request */
206 dma_pool_free(u3d->trb_pool,
207 req->trb_head->trb_hw, req->trb_head->trb_dma);
209 dma_unmap_single(ep->u3d->gadget.dev.parent,
210 (dma_addr_t)req->trb_head->trb_dma,
211 req->trb_count * sizeof(struct mv_u3d_trb_hw),
213 kfree(req->trb_head->trb_hw);
215 kfree(req->trb_head);
217 usb_gadget_unmap_request(&u3d->gadget, &req->req, mv_u3d_ep_dir(ep));
219 if (status && (status != -ESHUTDOWN)) {
220 dev_dbg(u3d->dev, "complete %s req %p stat %d len %u/%u",
221 ep->ep.name, &req->req, status,
222 req->req.actual, req->req.length);
225 spin_unlock(&ep->u3d->lock);
227 * complete() is from gadget layer,
228 * eg fsg->bulk_in_complete()
230 if (req->req.complete)
231 req->req.complete(&ep->ep, &req->req);
233 spin_lock(&ep->u3d->lock);
236 static int mv_u3d_queue_trb(struct mv_u3d_ep *ep, struct mv_u3d_req *req)
240 struct mv_u3d_ep_context *ep_context;
244 direction = mv_u3d_ep_dir(ep);
246 /* ep0 in and out share the same ep context slot 1*/
248 ep_context = &(u3d->ep_context[1]);
250 ep_context = &(u3d->ep_context[ep->ep_num * 2 + direction]);
252 /* check if the pipe is empty or not */
253 if (!list_empty(&ep->queue)) {
254 dev_err(u3d->dev, "add trb to non-empty queue!\n");
258 ep_context->rsvd0 = cpu_to_le32(1);
259 ep_context->rsvd1 = 0;
261 /* Configure the trb address and set the DCS bit.
262 * Both DCS bit and own bit in trb should be set.
264 ep_context->trb_addr_lo =
265 cpu_to_le32(req->trb_head->trb_dma | DCS_ENABLE);
266 ep_context->trb_addr_hi = 0;
268 /* Ensure that updates to the EP Context will
269 * occure before Ring Bell.
273 /* ring bell the ep */
278 + ((direction == MV_U3D_EP_DIR_OUT) ? 0 : 1);
280 iowrite32(tmp, &u3d->op_regs->doorbell);
285 static struct mv_u3d_trb *mv_u3d_build_trb_one(struct mv_u3d_req *req,
286 unsigned *length, dma_addr_t *dma)
289 unsigned int direction;
290 struct mv_u3d_trb *trb;
291 struct mv_u3d_trb_hw *trb_hw;
294 /* how big will this transfer be? */
295 *length = req->req.length - req->req.actual;
296 BUG_ON(*length > (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
300 trb = kzalloc(sizeof(*trb), GFP_ATOMIC);
302 dev_err(u3d->dev, "%s, trb alloc fail\n", __func__);
307 * Be careful that no _GFP_HIGHMEM is set,
308 * or we can not use dma_to_virt
309 * cannot use GFP_KERNEL in spin lock
311 trb_hw = dma_pool_alloc(u3d->trb_pool, GFP_ATOMIC, dma);
315 "%s, dma_pool_alloc fail\n", __func__);
319 trb->trb_hw = trb_hw;
321 /* initialize buffer page pointers */
322 temp = (u32)(req->req.dma + req->req.actual);
324 trb_hw->buf_addr_lo = cpu_to_le32(temp);
325 trb_hw->buf_addr_hi = 0;
326 trb_hw->trb_len = cpu_to_le32(*length);
327 trb_hw->ctrl.own = 1;
329 if (req->ep->ep_num == 0)
330 trb_hw->ctrl.type = TYPE_DATA;
332 trb_hw->ctrl.type = TYPE_NORMAL;
334 req->req.actual += *length;
336 direction = mv_u3d_ep_dir(req->ep);
337 if (direction == MV_U3D_EP_DIR_IN)
338 trb_hw->ctrl.dir = 1;
340 trb_hw->ctrl.dir = 0;
342 /* Enable interrupt for the last trb of a request */
343 if (!req->req.no_interrupt)
344 trb_hw->ctrl.ioc = 1;
346 trb_hw->ctrl.chain = 0;
352 static int mv_u3d_build_trb_chain(struct mv_u3d_req *req, unsigned *length,
353 struct mv_u3d_trb *trb, int *is_last)
356 unsigned int direction;
359 /* how big will this transfer be? */
360 *length = min(req->req.length - req->req.actual,
361 (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER);
367 /* initialize buffer page pointers */
368 temp = (u32)(req->req.dma + req->req.actual);
370 trb->trb_hw->buf_addr_lo = cpu_to_le32(temp);
371 trb->trb_hw->buf_addr_hi = 0;
372 trb->trb_hw->trb_len = cpu_to_le32(*length);
373 trb->trb_hw->ctrl.own = 1;
375 if (req->ep->ep_num == 0)
376 trb->trb_hw->ctrl.type = TYPE_DATA;
378 trb->trb_hw->ctrl.type = TYPE_NORMAL;
380 req->req.actual += *length;
382 direction = mv_u3d_ep_dir(req->ep);
383 if (direction == MV_U3D_EP_DIR_IN)
384 trb->trb_hw->ctrl.dir = 1;
386 trb->trb_hw->ctrl.dir = 0;
388 /* zlp is needed if req->req.zero is set */
390 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
394 } else if (req->req.length == req->req.actual)
399 /* Enable interrupt for the last trb of a request */
400 if (*is_last && !req->req.no_interrupt)
401 trb->trb_hw->ctrl.ioc = 1;
404 trb->trb_hw->ctrl.chain = 0;
406 trb->trb_hw->ctrl.chain = 1;
407 dev_dbg(u3d->dev, "chain trb\n");
415 /* generate TRB linked list for a request
416 * usb controller only supports continous trb chain,
417 * that trb structure physical address should be continous.
419 static int mv_u3d_req_to_trb(struct mv_u3d_req *req)
423 struct mv_u3d_trb *trb;
424 struct mv_u3d_trb_hw *trb_hw;
432 INIT_LIST_HEAD(&req->trb_list);
434 length = req->req.length - req->req.actual;
435 /* normally the request transfer length is less than 16KB.
436 * we use buil_trb_one() to optimize it.
438 if (length <= (unsigned)MV_U3D_EP_MAX_LENGTH_TRANSFER) {
439 trb = mv_u3d_build_trb_one(req, &count, &dma);
440 list_add_tail(&trb->trb_list, &req->trb_list);
445 trb_num = length / MV_U3D_EP_MAX_LENGTH_TRANSFER;
446 if (length % MV_U3D_EP_MAX_LENGTH_TRANSFER)
449 trb = kcalloc(trb_num, sizeof(*trb), GFP_ATOMIC);
452 "%s, trb alloc fail\n", __func__);
456 trb_hw = kcalloc(trb_num, sizeof(*trb_hw), GFP_ATOMIC);
460 "%s, trb_hw alloc fail\n", __func__);
465 trb->trb_hw = trb_hw;
466 if (mv_u3d_build_trb_chain(req, &count,
469 "%s, mv_u3d_build_trb_chain fail\n",
474 list_add_tail(&trb->trb_list, &req->trb_list);
480 req->trb_head = list_entry(req->trb_list.next,
481 struct mv_u3d_trb, trb_list);
482 req->trb_head->trb_dma = dma_map_single(u3d->gadget.dev.parent,
483 req->trb_head->trb_hw,
484 trb_num * sizeof(*trb_hw),
494 mv_u3d_start_queue(struct mv_u3d_ep *ep)
496 struct mv_u3d *u3d = ep->u3d;
497 struct mv_u3d_req *req;
500 if (!list_empty(&ep->req_list) && !ep->processing)
501 req = list_entry(ep->req_list.next, struct mv_u3d_req, list);
507 /* set up dma mapping */
508 ret = usb_gadget_map_request(&u3d->gadget, &req->req,
513 req->req.status = -EINPROGRESS;
517 /* build trbs and push them to device queue */
518 if (!mv_u3d_req_to_trb(req)) {
519 ret = mv_u3d_queue_trb(ep, req);
526 dev_err(u3d->dev, "%s, mv_u3d_req_to_trb fail\n", __func__);
530 /* irq handler advances the queue */
532 list_add_tail(&req->queue, &ep->queue);
537 static int mv_u3d_ep_enable(struct usb_ep *_ep,
538 const struct usb_endpoint_descriptor *desc)
541 struct mv_u3d_ep *ep;
542 struct mv_u3d_ep_context *ep_context;
544 unsigned maxburst = 0;
545 u32 epxcr, direction;
547 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT)
550 ep = container_of(_ep, struct mv_u3d_ep, ep);
553 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN)
556 direction = mv_u3d_ep_dir(ep);
557 max = le16_to_cpu(desc->wMaxPacketSize);
561 maxburst = _ep->maxburst;
563 /* Get the endpoint context address */
564 ep_context = (struct mv_u3d_ep_context *)ep->ep_context;
566 /* Set the max burst size */
567 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
568 case USB_ENDPOINT_XFER_BULK:
571 "max burst should not be greater "
572 "than 16 on bulk ep\n");
574 _ep->maxburst = maxburst;
577 "maxburst: %d on bulk %s\n", maxburst, ep->name);
579 case USB_ENDPOINT_XFER_CONTROL:
580 /* control transfer only supports maxburst as one */
582 _ep->maxburst = maxburst;
584 case USB_ENDPOINT_XFER_INT:
587 "max burst should be 1 on int ep "
588 "if transfer size is not 1024\n");
590 _ep->maxburst = maxburst;
593 case USB_ENDPOINT_XFER_ISOC:
596 "max burst should be 1 on isoc ep "
597 "if transfer size is not 1024\n");
599 _ep->maxburst = maxburst;
606 ep->ep.maxpacket = max;
610 /* Enable the endpoint for Rx or Tx and set the endpoint type */
611 if (direction == MV_U3D_EP_DIR_OUT) {
612 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
613 epxcr |= MV_U3D_EPXCR_EP_INIT;
614 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
616 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
617 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
619 epxcr = ((max << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
620 | ((maxburst - 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
621 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
622 | (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
623 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
625 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
626 epxcr |= MV_U3D_EPXCR_EP_INIT;
627 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
629 epxcr &= ~MV_U3D_EPXCR_EP_INIT;
630 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
632 epxcr = ((max << MV_U3D_EPXCR_MAX_PACKET_SIZE_SHIFT)
633 | ((maxburst - 1) << MV_U3D_EPXCR_MAX_BURST_SIZE_SHIFT)
634 | (1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
635 | (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
636 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
644 static int mv_u3d_ep_disable(struct usb_ep *_ep)
647 struct mv_u3d_ep *ep;
648 struct mv_u3d_ep_context *ep_context;
649 u32 epxcr, direction;
655 ep = container_of(_ep, struct mv_u3d_ep, ep);
661 /* Get the endpoint context address */
662 ep_context = ep->ep_context;
664 direction = mv_u3d_ep_dir(ep);
666 /* nuke all pending requests (does flush) */
667 spin_lock_irqsave(&u3d->lock, flags);
668 mv_u3d_nuke(ep, -ESHUTDOWN);
669 spin_unlock_irqrestore(&u3d->lock, flags);
671 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
672 if (direction == MV_U3D_EP_DIR_OUT) {
673 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
674 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
675 | USB_ENDPOINT_XFERTYPE_MASK);
676 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr1);
678 epxcr = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
679 epxcr &= ~((1 << MV_U3D_EPXCR_EP_ENABLE_SHIFT)
680 | USB_ENDPOINT_XFERTYPE_MASK);
681 iowrite32(epxcr, &u3d->vuc_regs->epcr[ep->ep_num].epxincr1);
690 static struct usb_request *
691 mv_u3d_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
693 struct mv_u3d_req *req = NULL;
695 req = kzalloc(sizeof *req, gfp_flags);
699 INIT_LIST_HEAD(&req->queue);
704 static void mv_u3d_free_request(struct usb_ep *_ep, struct usb_request *_req)
706 struct mv_u3d_req *req = container_of(_req, struct mv_u3d_req, req);
711 static void mv_u3d_ep_fifo_flush(struct usb_ep *_ep)
715 struct mv_u3d_ep *ep = container_of(_ep, struct mv_u3d_ep, ep);
719 /* if endpoint is not enabled, cannot flush endpoint */
724 direction = mv_u3d_ep_dir(ep);
726 /* ep0 need clear bit after flushing fifo. */
728 if (direction == MV_U3D_EP_DIR_OUT) {
729 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxoutcr0);
730 tmp |= MV_U3D_EPXCR_EP_FLUSH;
731 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
733 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
734 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxoutcr0);
736 tmp = ioread32(&u3d->vuc_regs->epcr[0].epxincr0);
737 tmp |= MV_U3D_EPXCR_EP_FLUSH;
738 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
740 tmp &= ~MV_U3D_EPXCR_EP_FLUSH;
741 iowrite32(tmp, &u3d->vuc_regs->epcr[0].epxincr0);
746 if (direction == MV_U3D_EP_DIR_OUT) {
747 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
748 tmp |= MV_U3D_EPXCR_EP_FLUSH;
749 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
751 /* Wait until flushing completed */
752 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
753 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0) &
754 MV_U3D_EPXCR_EP_FLUSH) {
756 * EP_FLUSH bit should be cleared to indicate this
757 * operation is complete
761 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
762 direction ? "in" : "out");
768 } else { /* EP_DIR_IN */
769 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
770 tmp |= MV_U3D_EPXCR_EP_FLUSH;
771 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
773 /* Wait until flushing completed */
774 loops = LOOPS(MV_U3D_FLUSH_TIMEOUT);
775 while (ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0) &
776 MV_U3D_EPXCR_EP_FLUSH) {
778 * EP_FLUSH bit should be cleared to indicate this
779 * operation is complete
783 "EP FLUSH TIMEOUT for ep%d%s\n", ep->ep_num,
784 direction ? "in" : "out");
793 /* queues (submits) an I/O request to an endpoint */
795 mv_u3d_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
797 struct mv_u3d_ep *ep;
798 struct mv_u3d_req *req;
801 int is_first_req = 0;
803 if (unlikely(!_ep || !_req))
806 ep = container_of(_ep, struct mv_u3d_ep, ep);
809 req = container_of(_req, struct mv_u3d_req, req);
812 && u3d->ep0_state == MV_U3D_STATUS_STAGE
814 dev_dbg(u3d->dev, "ep0 status stage\n");
815 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
819 dev_dbg(u3d->dev, "%s: %s, req: 0x%p\n",
820 __func__, _ep->name, req);
822 /* catch various bogus parameters */
823 if (!req->req.complete || !req->req.buf
824 || !list_empty(&req->queue)) {
826 "%s, bad params, _req: 0x%p,"
827 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
828 "list_empty: 0x%x\n",
830 req->req.complete, req->req.buf,
831 list_empty(&req->queue));
834 if (unlikely(!ep->ep.desc)) {
835 dev_err(u3d->dev, "%s, bad ep\n", __func__);
838 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
839 if (req->req.length > ep->ep.maxpacket)
843 if (!u3d->driver || u3d->gadget.speed == USB_SPEED_UNKNOWN) {
845 "bad params of driver/speed\n");
851 /* Software list handles usb request. */
852 spin_lock_irqsave(&ep->req_lock, flags);
853 is_first_req = list_empty(&ep->req_list);
854 list_add_tail(&req->list, &ep->req_list);
855 spin_unlock_irqrestore(&ep->req_lock, flags);
857 dev_dbg(u3d->dev, "list is not empty\n");
861 dev_dbg(u3d->dev, "call mv_u3d_start_queue from usb_ep_queue\n");
862 spin_lock_irqsave(&u3d->lock, flags);
863 mv_u3d_start_queue(ep);
864 spin_unlock_irqrestore(&u3d->lock, flags);
868 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
869 static int mv_u3d_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
871 struct mv_u3d_ep *ep;
872 struct mv_u3d_req *req;
874 struct mv_u3d_ep_context *ep_context;
875 struct mv_u3d_req *next_req;
883 ep = container_of(_ep, struct mv_u3d_ep, ep);
886 spin_lock_irqsave(&ep->u3d->lock, flags);
888 /* make sure it's actually queued on this endpoint */
889 list_for_each_entry(req, &ep->queue, queue) {
890 if (&req->req == _req)
893 if (&req->req != _req) {
898 /* The request is in progress, or completed but not dequeued */
899 if (ep->queue.next == &req->queue) {
900 _req->status = -ECONNRESET;
901 mv_u3d_ep_fifo_flush(_ep);
903 /* The request isn't the last request in this ep queue */
904 if (req->queue.next != &ep->queue) {
906 "it is the last request in this ep queue\n");
907 ep_context = ep->ep_context;
908 next_req = list_entry(req->queue.next,
909 struct mv_u3d_req, queue);
911 /* Point first TRB of next request to the EP context. */
912 iowrite32((unsigned long) next_req->trb_head,
913 &ep_context->trb_addr_lo);
915 struct mv_u3d_ep_context *ep_context;
916 ep_context = ep->ep_context;
917 ep_context->trb_addr_lo = 0;
918 ep_context->trb_addr_hi = 0;
924 mv_u3d_done(ep, req, -ECONNRESET);
926 /* remove the req from the ep req list */
927 if (!list_empty(&ep->req_list)) {
928 struct mv_u3d_req *curr_req;
929 curr_req = list_entry(ep->req_list.next,
930 struct mv_u3d_req, list);
931 if (curr_req == req) {
932 list_del_init(&req->list);
938 spin_unlock_irqrestore(&ep->u3d->lock, flags);
943 mv_u3d_ep_set_stall(struct mv_u3d *u3d, u8 ep_num, u8 direction, int stall)
946 struct mv_u3d_ep *ep = u3d->eps;
948 dev_dbg(u3d->dev, "%s\n", __func__);
949 if (direction == MV_U3D_EP_DIR_OUT) {
950 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
952 tmp |= MV_U3D_EPXCR_EP_HALT;
954 tmp &= ~MV_U3D_EPXCR_EP_HALT;
955 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxoutcr0);
957 tmp = ioread32(&u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
959 tmp |= MV_U3D_EPXCR_EP_HALT;
961 tmp &= ~MV_U3D_EPXCR_EP_HALT;
962 iowrite32(tmp, &u3d->vuc_regs->epcr[ep->ep_num].epxincr0);
966 static int mv_u3d_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
968 struct mv_u3d_ep *ep;
969 unsigned long flags = 0;
973 ep = container_of(_ep, struct mv_u3d_ep, ep);
980 if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
981 status = -EOPNOTSUPP;
986 * Attempt to halt IN ep will fail if any transfer requests
989 if (halt && (mv_u3d_ep_dir(ep) == MV_U3D_EP_DIR_IN)
990 && !list_empty(&ep->queue)) {
995 spin_lock_irqsave(&ep->u3d->lock, flags);
996 mv_u3d_ep_set_stall(u3d, ep->ep_num, mv_u3d_ep_dir(ep), halt);
1001 spin_unlock_irqrestore(&ep->u3d->lock, flags);
1003 if (ep->ep_num == 0)
1004 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1009 static int mv_u3d_ep_set_halt(struct usb_ep *_ep, int halt)
1011 return mv_u3d_ep_set_halt_wedge(_ep, halt, 0);
1014 static int mv_u3d_ep_set_wedge(struct usb_ep *_ep)
1016 return mv_u3d_ep_set_halt_wedge(_ep, 1, 1);
1019 static struct usb_ep_ops mv_u3d_ep_ops = {
1020 .enable = mv_u3d_ep_enable,
1021 .disable = mv_u3d_ep_disable,
1023 .alloc_request = mv_u3d_alloc_request,
1024 .free_request = mv_u3d_free_request,
1026 .queue = mv_u3d_ep_queue,
1027 .dequeue = mv_u3d_ep_dequeue,
1029 .set_wedge = mv_u3d_ep_set_wedge,
1030 .set_halt = mv_u3d_ep_set_halt,
1031 .fifo_flush = mv_u3d_ep_fifo_flush,
1034 static void mv_u3d_controller_stop(struct mv_u3d *u3d)
1038 if (!u3d->clock_gating && u3d->vbus_valid_detect)
1039 iowrite32(MV_U3D_INTR_ENABLE_VBUS_VALID,
1040 &u3d->vuc_regs->intrenable);
1042 iowrite32(0, &u3d->vuc_regs->intrenable);
1043 iowrite32(~0x0, &u3d->vuc_regs->endcomplete);
1044 iowrite32(~0x0, &u3d->vuc_regs->trbunderrun);
1045 iowrite32(~0x0, &u3d->vuc_regs->trbcomplete);
1046 iowrite32(~0x0, &u3d->vuc_regs->linkchange);
1047 iowrite32(0x1, &u3d->vuc_regs->setuplock);
1049 /* Reset the RUN bit in the command register to stop USB */
1050 tmp = ioread32(&u3d->op_regs->usbcmd);
1051 tmp &= ~MV_U3D_CMD_RUN_STOP;
1052 iowrite32(tmp, &u3d->op_regs->usbcmd);
1053 dev_dbg(u3d->dev, "after u3d_stop, USBCMD 0x%x\n",
1054 ioread32(&u3d->op_regs->usbcmd));
1057 static void mv_u3d_controller_start(struct mv_u3d *u3d)
1062 /* enable link LTSSM state machine */
1063 temp = ioread32(&u3d->vuc_regs->ltssm);
1064 temp |= MV_U3D_LTSSM_PHY_INIT_DONE;
1065 iowrite32(temp, &u3d->vuc_regs->ltssm);
1067 /* Enable interrupts */
1068 usbintr = MV_U3D_INTR_ENABLE_LINK_CHG | MV_U3D_INTR_ENABLE_TXDESC_ERR |
1069 MV_U3D_INTR_ENABLE_RXDESC_ERR | MV_U3D_INTR_ENABLE_TX_COMPLETE |
1070 MV_U3D_INTR_ENABLE_RX_COMPLETE | MV_U3D_INTR_ENABLE_SETUP |
1071 (u3d->vbus_valid_detect ? MV_U3D_INTR_ENABLE_VBUS_VALID : 0);
1072 iowrite32(usbintr, &u3d->vuc_regs->intrenable);
1074 /* Enable ctrl ep */
1075 iowrite32(0x1, &u3d->vuc_regs->ctrlepenable);
1077 /* Set the Run bit in the command register */
1078 iowrite32(MV_U3D_CMD_RUN_STOP, &u3d->op_regs->usbcmd);
1079 dev_dbg(u3d->dev, "after u3d_start, USBCMD 0x%x\n",
1080 ioread32(&u3d->op_regs->usbcmd));
1083 static int mv_u3d_controller_reset(struct mv_u3d *u3d)
1088 /* Stop the controller */
1089 tmp = ioread32(&u3d->op_regs->usbcmd);
1090 tmp &= ~MV_U3D_CMD_RUN_STOP;
1091 iowrite32(tmp, &u3d->op_regs->usbcmd);
1093 /* Reset the controller to get default values */
1094 iowrite32(MV_U3D_CMD_CTRL_RESET, &u3d->op_regs->usbcmd);
1096 /* wait for reset to complete */
1097 loops = LOOPS(MV_U3D_RESET_TIMEOUT);
1098 while (ioread32(&u3d->op_regs->usbcmd) & MV_U3D_CMD_CTRL_RESET) {
1101 "Wait for RESET completed TIMEOUT\n");
1108 /* Configure the Endpoint Context Address */
1109 iowrite32(u3d->ep_context_dma, &u3d->op_regs->dcbaapl);
1110 iowrite32(0, &u3d->op_regs->dcbaaph);
1115 static int mv_u3d_enable(struct mv_u3d *u3d)
1117 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1123 if (!u3d->clock_gating) {
1128 dev_dbg(u3d->dev, "enable u3d\n");
1129 clk_enable(u3d->clk);
1130 if (pdata->phy_init) {
1131 retval = pdata->phy_init(u3d->phy_regs);
1134 "init phy error %d\n", retval);
1135 clk_disable(u3d->clk);
1144 static void mv_u3d_disable(struct mv_u3d *u3d)
1146 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1147 if (u3d->clock_gating && u3d->active) {
1148 dev_dbg(u3d->dev, "disable u3d\n");
1149 if (pdata->phy_deinit)
1150 pdata->phy_deinit(u3d->phy_regs);
1151 clk_disable(u3d->clk);
1156 static int mv_u3d_vbus_session(struct usb_gadget *gadget, int is_active)
1159 unsigned long flags;
1162 u3d = container_of(gadget, struct mv_u3d, gadget);
1164 spin_lock_irqsave(&u3d->lock, flags);
1166 u3d->vbus_active = (is_active != 0);
1167 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1168 __func__, u3d->softconnect, u3d->vbus_active);
1170 * 1. external VBUS detect: we can disable/enable clock on demand.
1171 * 2. UDC VBUS detect: we have to enable clock all the time.
1172 * 3. No VBUS detect: we have to enable clock all the time.
1174 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1175 retval = mv_u3d_enable(u3d);
1178 * after clock is disabled, we lost all the register
1179 * context. We have to re-init registers
1181 mv_u3d_controller_reset(u3d);
1182 mv_u3d_ep0_reset(u3d);
1183 mv_u3d_controller_start(u3d);
1185 } else if (u3d->driver && u3d->softconnect) {
1189 /* stop all the transfer in queue*/
1190 mv_u3d_stop_activity(u3d, u3d->driver);
1191 mv_u3d_controller_stop(u3d);
1192 mv_u3d_disable(u3d);
1196 spin_unlock_irqrestore(&u3d->lock, flags);
1200 /* constrain controller's VBUS power usage
1201 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1202 * reporting how much power the device may consume. For example, this
1203 * could affect how quickly batteries are recharged.
1205 * Returns zero on success, else negative errno.
1207 static int mv_u3d_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1209 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1216 static int mv_u3d_pullup(struct usb_gadget *gadget, int is_on)
1218 struct mv_u3d *u3d = container_of(gadget, struct mv_u3d, gadget);
1219 unsigned long flags;
1222 spin_lock_irqsave(&u3d->lock, flags);
1224 dev_dbg(u3d->dev, "%s: softconnect %d, vbus_active %d\n",
1225 __func__, u3d->softconnect, u3d->vbus_active);
1226 u3d->softconnect = (is_on != 0);
1227 if (u3d->driver && u3d->softconnect && u3d->vbus_active) {
1228 retval = mv_u3d_enable(u3d);
1231 * after clock is disabled, we lost all the register
1232 * context. We have to re-init registers
1234 mv_u3d_controller_reset(u3d);
1235 mv_u3d_ep0_reset(u3d);
1236 mv_u3d_controller_start(u3d);
1238 } else if (u3d->driver && u3d->vbus_active) {
1239 /* stop all the transfer in queue*/
1240 mv_u3d_stop_activity(u3d, u3d->driver);
1241 mv_u3d_controller_stop(u3d);
1242 mv_u3d_disable(u3d);
1245 spin_unlock_irqrestore(&u3d->lock, flags);
1250 static int mv_u3d_start(struct usb_gadget *g,
1251 struct usb_gadget_driver *driver)
1253 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1254 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1255 unsigned long flags;
1260 spin_lock_irqsave(&u3d->lock, flags);
1262 if (!u3d->clock_gating) {
1263 clk_enable(u3d->clk);
1264 if (pdata->phy_init)
1265 pdata->phy_init(u3d->phy_regs);
1268 /* hook up the driver ... */
1269 driver->driver.bus = NULL;
1270 u3d->driver = driver;
1272 u3d->ep0_dir = USB_DIR_OUT;
1274 spin_unlock_irqrestore(&u3d->lock, flags);
1276 u3d->vbus_valid_detect = 1;
1281 static int mv_u3d_stop(struct usb_gadget *g,
1282 struct usb_gadget_driver *driver)
1284 struct mv_u3d *u3d = container_of(g, struct mv_u3d, gadget);
1285 struct mv_usb_platform_data *pdata = dev_get_platdata(u3d->dev);
1286 unsigned long flags;
1288 u3d->vbus_valid_detect = 0;
1289 spin_lock_irqsave(&u3d->lock, flags);
1291 /* enable clock to access controller register */
1292 clk_enable(u3d->clk);
1293 if (pdata->phy_init)
1294 pdata->phy_init(u3d->phy_regs);
1296 mv_u3d_controller_stop(u3d);
1297 /* stop all usb activities */
1298 u3d->gadget.speed = USB_SPEED_UNKNOWN;
1299 mv_u3d_stop_activity(u3d, driver);
1300 mv_u3d_disable(u3d);
1302 if (pdata->phy_deinit)
1303 pdata->phy_deinit(u3d->phy_regs);
1304 clk_disable(u3d->clk);
1306 spin_unlock_irqrestore(&u3d->lock, flags);
1313 /* device controller usb_gadget_ops structure */
1314 static const struct usb_gadget_ops mv_u3d_ops = {
1315 /* notify controller that VBUS is powered or not */
1316 .vbus_session = mv_u3d_vbus_session,
1318 /* constrain controller's VBUS power usage */
1319 .vbus_draw = mv_u3d_vbus_draw,
1321 .pullup = mv_u3d_pullup,
1322 .udc_start = mv_u3d_start,
1323 .udc_stop = mv_u3d_stop,
1326 static int mv_u3d_eps_init(struct mv_u3d *u3d)
1328 struct mv_u3d_ep *ep;
1332 /* initialize ep0, ep0 in/out use eps[1] */
1335 strncpy(ep->name, "ep0", sizeof(ep->name));
1336 ep->ep.name = ep->name;
1337 ep->ep.ops = &mv_u3d_ep_ops;
1339 ep->ep.maxpacket = MV_U3D_EP0_MAX_PKT_SIZE;
1341 ep->ep.desc = &mv_u3d_ep0_desc;
1342 INIT_LIST_HEAD(&ep->queue);
1343 INIT_LIST_HEAD(&ep->req_list);
1344 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1346 /* add ep0 ep_context */
1347 ep->ep_context = &u3d->ep_context[1];
1349 /* initialize other endpoints */
1350 for (i = 2; i < u3d->max_eps * 2; i++) {
1353 snprintf(name, sizeof(name), "ep%din", i >> 1);
1354 ep->direction = MV_U3D_EP_DIR_IN;
1356 snprintf(name, sizeof(name), "ep%dout", i >> 1);
1357 ep->direction = MV_U3D_EP_DIR_OUT;
1360 strncpy(ep->name, name, sizeof(ep->name));
1361 ep->ep.name = ep->name;
1363 ep->ep.ops = &mv_u3d_ep_ops;
1364 ep->ep.maxpacket = (unsigned short) ~0;
1367 INIT_LIST_HEAD(&ep->queue);
1368 list_add_tail(&ep->ep.ep_list, &u3d->gadget.ep_list);
1370 INIT_LIST_HEAD(&ep->req_list);
1371 spin_lock_init(&ep->req_lock);
1372 ep->ep_context = &u3d->ep_context[i];
1378 /* delete all endpoint requests, called with spinlock held */
1379 static void mv_u3d_nuke(struct mv_u3d_ep *ep, int status)
1381 /* endpoint fifo flush */
1382 mv_u3d_ep_fifo_flush(&ep->ep);
1384 while (!list_empty(&ep->queue)) {
1385 struct mv_u3d_req *req = NULL;
1386 req = list_entry(ep->queue.next, struct mv_u3d_req, queue);
1387 mv_u3d_done(ep, req, status);
1391 /* stop all USB activities */
1393 void mv_u3d_stop_activity(struct mv_u3d *u3d, struct usb_gadget_driver *driver)
1395 struct mv_u3d_ep *ep;
1397 mv_u3d_nuke(&u3d->eps[1], -ESHUTDOWN);
1399 list_for_each_entry(ep, &u3d->gadget.ep_list, ep.ep_list) {
1400 mv_u3d_nuke(ep, -ESHUTDOWN);
1403 /* report disconnect; the driver is already quiesced */
1405 spin_unlock(&u3d->lock);
1406 driver->disconnect(&u3d->gadget);
1407 spin_lock(&u3d->lock);
1411 static void mv_u3d_irq_process_error(struct mv_u3d *u3d)
1413 /* Increment the error count */
1415 dev_err(u3d->dev, "%s\n", __func__);
1418 static void mv_u3d_irq_process_link_change(struct mv_u3d *u3d)
1422 linkchange = ioread32(&u3d->vuc_regs->linkchange);
1423 iowrite32(linkchange, &u3d->vuc_regs->linkchange);
1425 dev_dbg(u3d->dev, "linkchange: 0x%x\n", linkchange);
1427 if (linkchange & MV_U3D_LINK_CHANGE_LINK_UP) {
1428 dev_dbg(u3d->dev, "link up: ltssm state: 0x%x\n",
1429 ioread32(&u3d->vuc_regs->ltssmstate));
1431 u3d->usb_state = USB_STATE_DEFAULT;
1432 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1433 u3d->ep0_state = MV_U3D_WAIT_FOR_SETUP;
1436 u3d->gadget.speed = USB_SPEED_SUPER;
1439 if (linkchange & MV_U3D_LINK_CHANGE_SUSPEND) {
1440 dev_dbg(u3d->dev, "link suspend\n");
1441 u3d->resume_state = u3d->usb_state;
1442 u3d->usb_state = USB_STATE_SUSPENDED;
1445 if (linkchange & MV_U3D_LINK_CHANGE_RESUME) {
1446 dev_dbg(u3d->dev, "link resume\n");
1447 u3d->usb_state = u3d->resume_state;
1448 u3d->resume_state = 0;
1451 if (linkchange & MV_U3D_LINK_CHANGE_WRESET) {
1452 dev_dbg(u3d->dev, "warm reset\n");
1453 u3d->usb_state = USB_STATE_POWERED;
1456 if (linkchange & MV_U3D_LINK_CHANGE_HRESET) {
1457 dev_dbg(u3d->dev, "hot reset\n");
1458 u3d->usb_state = USB_STATE_DEFAULT;
1461 if (linkchange & MV_U3D_LINK_CHANGE_INACT)
1462 dev_dbg(u3d->dev, "inactive\n");
1464 if (linkchange & MV_U3D_LINK_CHANGE_DISABLE_AFTER_U0)
1465 dev_dbg(u3d->dev, "ss.disabled\n");
1467 if (linkchange & MV_U3D_LINK_CHANGE_VBUS_INVALID) {
1468 dev_dbg(u3d->dev, "vbus invalid\n");
1469 u3d->usb_state = USB_STATE_ATTACHED;
1470 u3d->vbus_valid_detect = 1;
1471 /* if external vbus detect is not supported,
1472 * we handle it here.
1475 spin_unlock(&u3d->lock);
1476 mv_u3d_vbus_session(&u3d->gadget, 0);
1477 spin_lock(&u3d->lock);
1482 static void mv_u3d_ch9setaddress(struct mv_u3d *u3d,
1483 struct usb_ctrlrequest *setup)
1487 if (u3d->usb_state != USB_STATE_DEFAULT) {
1489 "%s, cannot setaddr in this state (%d)\n",
1490 __func__, u3d->usb_state);
1494 u3d->dev_addr = (u8)setup->wValue;
1496 dev_dbg(u3d->dev, "%s: 0x%x\n", __func__, u3d->dev_addr);
1498 if (u3d->dev_addr > 127) {
1500 "%s, u3d address is wrong (out of range)\n", __func__);
1505 /* update usb state */
1506 u3d->usb_state = USB_STATE_ADDRESS;
1508 /* set the new address */
1509 tmp = ioread32(&u3d->vuc_regs->devaddrtiebrkr);
1511 tmp |= (u32)u3d->dev_addr;
1512 iowrite32(tmp, &u3d->vuc_regs->devaddrtiebrkr);
1516 mv_u3d_ep0_stall(u3d);
1519 static int mv_u3d_is_set_configuration(struct usb_ctrlrequest *setup)
1521 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1522 if (setup->bRequest == USB_REQ_SET_CONFIGURATION)
1528 static void mv_u3d_handle_setup_packet(struct mv_u3d *u3d, u8 ep_num,
1529 struct usb_ctrlrequest *setup)
1530 __releases(&u3c->lock)
1531 __acquires(&u3c->lock)
1533 bool delegate = false;
1535 mv_u3d_nuke(&u3d->eps[ep_num * 2 + MV_U3D_EP_DIR_IN], -ESHUTDOWN);
1537 dev_dbg(u3d->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1538 setup->bRequestType, setup->bRequest,
1539 setup->wValue, setup->wIndex, setup->wLength);
1541 /* We process some stardard setup requests here */
1542 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1543 switch (setup->bRequest) {
1544 case USB_REQ_GET_STATUS:
1548 case USB_REQ_SET_ADDRESS:
1549 mv_u3d_ch9setaddress(u3d, setup);
1552 case USB_REQ_CLEAR_FEATURE:
1556 case USB_REQ_SET_FEATURE:
1566 /* delegate USB standard requests to the gadget driver */
1567 if (delegate == true) {
1568 /* USB requests handled by gadget */
1569 if (setup->wLength) {
1570 /* DATA phase from gadget, STATUS phase from u3d */
1571 u3d->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1572 ? MV_U3D_EP_DIR_IN : MV_U3D_EP_DIR_OUT;
1573 spin_unlock(&u3d->lock);
1574 if (u3d->driver->setup(&u3d->gadget,
1575 &u3d->local_setup_buff) < 0) {
1576 dev_err(u3d->dev, "setup error!\n");
1577 mv_u3d_ep0_stall(u3d);
1579 spin_lock(&u3d->lock);
1581 /* no DATA phase, STATUS phase from gadget */
1582 u3d->ep0_dir = MV_U3D_EP_DIR_IN;
1583 u3d->ep0_state = MV_U3D_STATUS_STAGE;
1584 spin_unlock(&u3d->lock);
1585 if (u3d->driver->setup(&u3d->gadget,
1586 &u3d->local_setup_buff) < 0)
1587 mv_u3d_ep0_stall(u3d);
1588 spin_lock(&u3d->lock);
1591 if (mv_u3d_is_set_configuration(setup)) {
1592 dev_dbg(u3d->dev, "u3d configured\n");
1593 u3d->usb_state = USB_STATE_CONFIGURED;
1598 static void mv_u3d_get_setup_data(struct mv_u3d *u3d, u8 ep_num, u8 *buffer_ptr)
1600 struct mv_u3d_ep_context *epcontext;
1602 epcontext = &u3d->ep_context[ep_num * 2 + MV_U3D_EP_DIR_IN];
1604 /* Copy the setup packet to local buffer */
1605 memcpy(buffer_ptr, (u8 *) &epcontext->setup_buffer, 8);
1608 static void mv_u3d_irq_process_setup(struct mv_u3d *u3d)
1611 /* Process all Setup packet received interrupts */
1612 tmp = ioread32(&u3d->vuc_regs->setuplock);
1614 for (i = 0; i < u3d->max_eps; i++) {
1615 if (tmp & (1 << i)) {
1616 mv_u3d_get_setup_data(u3d, i,
1617 (u8 *)(&u3d->local_setup_buff));
1618 mv_u3d_handle_setup_packet(u3d, i,
1619 &u3d->local_setup_buff);
1624 iowrite32(tmp, &u3d->vuc_regs->setuplock);
1627 static void mv_u3d_irq_process_tr_complete(struct mv_u3d *u3d)
1630 int i, ep_num = 0, direction = 0;
1631 struct mv_u3d_ep *curr_ep;
1632 struct mv_u3d_req *curr_req, *temp_req;
1635 tmp = ioread32(&u3d->vuc_regs->endcomplete);
1637 dev_dbg(u3d->dev, "tr_complete: ep: 0x%x\n", tmp);
1640 iowrite32(tmp, &u3d->vuc_regs->endcomplete);
1642 for (i = 0; i < u3d->max_eps * 2; i++) {
1646 bit_pos = 1 << (ep_num + 16 * direction);
1648 if (!(bit_pos & tmp))
1652 curr_ep = &u3d->eps[1];
1654 curr_ep = &u3d->eps[i];
1656 /* remove req out of ep request list after completion */
1657 dev_dbg(u3d->dev, "tr comp: check req_list\n");
1658 spin_lock(&curr_ep->req_lock);
1659 if (!list_empty(&curr_ep->req_list)) {
1660 struct mv_u3d_req *req;
1661 req = list_entry(curr_ep->req_list.next,
1662 struct mv_u3d_req, list);
1663 list_del_init(&req->list);
1664 curr_ep->processing = 0;
1666 spin_unlock(&curr_ep->req_lock);
1668 /* process the req queue until an uncomplete request */
1669 list_for_each_entry_safe(curr_req, temp_req,
1670 &curr_ep->queue, queue) {
1671 status = mv_u3d_process_ep_req(u3d, i, curr_req);
1674 /* write back status to req */
1675 curr_req->req.status = status;
1677 /* ep0 request completion */
1679 mv_u3d_done(curr_ep, curr_req, 0);
1682 mv_u3d_done(curr_ep, curr_req, status);
1686 dev_dbg(u3d->dev, "call mv_u3d_start_queue from ep complete\n");
1687 mv_u3d_start_queue(curr_ep);
1691 static irqreturn_t mv_u3d_irq(int irq, void *dev)
1693 struct mv_u3d *u3d = (struct mv_u3d *)dev;
1698 spin_lock(&u3d->lock);
1700 status = ioread32(&u3d->vuc_regs->intrcause);
1701 intr = ioread32(&u3d->vuc_regs->intrenable);
1705 spin_unlock(&u3d->lock);
1706 dev_err(u3d->dev, "irq error!\n");
1710 if (status & MV_U3D_USBINT_VBUS_VALID) {
1711 bridgesetting = ioread32(&u3d->vuc_regs->bridgesetting);
1712 if (bridgesetting & MV_U3D_BRIDGE_SETTING_VBUS_VALID) {
1713 /* write vbus valid bit of bridge setting to clear */
1714 bridgesetting = MV_U3D_BRIDGE_SETTING_VBUS_VALID;
1715 iowrite32(bridgesetting, &u3d->vuc_regs->bridgesetting);
1716 dev_dbg(u3d->dev, "vbus valid\n");
1718 u3d->usb_state = USB_STATE_POWERED;
1719 u3d->vbus_valid_detect = 0;
1720 /* if external vbus detect is not supported,
1721 * we handle it here.
1724 spin_unlock(&u3d->lock);
1725 mv_u3d_vbus_session(&u3d->gadget, 1);
1726 spin_lock(&u3d->lock);
1729 dev_err(u3d->dev, "vbus bit is not set\n");
1732 /* RX data is already in the 16KB FIFO.*/
1733 if (status & MV_U3D_USBINT_UNDER_RUN) {
1734 trbunderrun = ioread32(&u3d->vuc_regs->trbunderrun);
1735 dev_err(u3d->dev, "under run, ep%d\n", trbunderrun);
1736 iowrite32(trbunderrun, &u3d->vuc_regs->trbunderrun);
1737 mv_u3d_irq_process_error(u3d);
1740 if (status & (MV_U3D_USBINT_RXDESC_ERR | MV_U3D_USBINT_TXDESC_ERR)) {
1741 /* write one to clear */
1742 iowrite32(status & (MV_U3D_USBINT_RXDESC_ERR
1743 | MV_U3D_USBINT_TXDESC_ERR),
1744 &u3d->vuc_regs->intrcause);
1745 dev_err(u3d->dev, "desc err 0x%x\n", status);
1746 mv_u3d_irq_process_error(u3d);
1749 if (status & MV_U3D_USBINT_LINK_CHG)
1750 mv_u3d_irq_process_link_change(u3d);
1752 if (status & MV_U3D_USBINT_TX_COMPLETE)
1753 mv_u3d_irq_process_tr_complete(u3d);
1755 if (status & MV_U3D_USBINT_RX_COMPLETE)
1756 mv_u3d_irq_process_tr_complete(u3d);
1758 if (status & MV_U3D_USBINT_SETUP)
1759 mv_u3d_irq_process_setup(u3d);
1761 spin_unlock(&u3d->lock);
1765 static int mv_u3d_remove(struct platform_device *dev)
1767 struct mv_u3d *u3d = platform_get_drvdata(dev);
1769 BUG_ON(u3d == NULL);
1771 usb_del_gadget_udc(&u3d->gadget);
1773 /* free memory allocated in probe */
1775 dma_pool_destroy(u3d->trb_pool);
1777 if (u3d->ep_context)
1778 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1779 u3d->ep_context, u3d->ep_context_dma);
1784 free_irq(u3d->irq, u3d);
1787 iounmap(u3d->cap_regs);
1788 u3d->cap_regs = NULL;
1790 kfree(u3d->status_req);
1799 static int mv_u3d_probe(struct platform_device *dev)
1801 struct mv_u3d *u3d = NULL;
1802 struct mv_usb_platform_data *pdata = dev_get_platdata(&dev->dev);
1807 if (!dev_get_platdata(&dev->dev)) {
1808 dev_err(&dev->dev, "missing platform_data\n");
1813 u3d = kzalloc(sizeof(*u3d), GFP_KERNEL);
1815 dev_err(&dev->dev, "failed to allocate memory for u3d\n");
1817 goto err_alloc_private;
1820 spin_lock_init(&u3d->lock);
1822 platform_set_drvdata(dev, u3d);
1824 u3d->dev = &dev->dev;
1825 u3d->vbus = pdata->vbus;
1827 u3d->clk = clk_get(&dev->dev, NULL);
1828 if (IS_ERR(u3d->clk)) {
1829 retval = PTR_ERR(u3d->clk);
1833 r = platform_get_resource_byname(dev, IORESOURCE_MEM, "capregs");
1835 dev_err(&dev->dev, "no I/O memory resource defined\n");
1837 goto err_get_cap_regs;
1840 u3d->cap_regs = (struct mv_u3d_cap_regs __iomem *)
1841 ioremap(r->start, resource_size(r));
1842 if (!u3d->cap_regs) {
1843 dev_err(&dev->dev, "failed to map I/O memory\n");
1845 goto err_map_cap_regs;
1847 dev_dbg(&dev->dev, "cap_regs address: 0x%lx/0x%lx\n",
1848 (unsigned long) r->start,
1849 (unsigned long) u3d->cap_regs);
1852 /* we will access controller register, so enable the u3d controller */
1853 clk_enable(u3d->clk);
1855 if (pdata->phy_init) {
1856 retval = pdata->phy_init(u3d->phy_regs);
1858 dev_err(&dev->dev, "init phy error %d\n", retval);
1859 goto err_u3d_enable;
1863 u3d->op_regs = (struct mv_u3d_op_regs __iomem *)(u3d->cap_regs
1864 + MV_U3D_USB3_OP_REGS_OFFSET);
1866 u3d->vuc_regs = (struct mv_u3d_vuc_regs __iomem *)(u3d->cap_regs
1867 + ioread32(&u3d->cap_regs->vuoff));
1872 * some platform will use usb to download image, it may not disconnect
1873 * usb gadget before loading kernel. So first stop u3d here.
1875 mv_u3d_controller_stop(u3d);
1876 iowrite32(0xFFFFFFFF, &u3d->vuc_regs->intrcause);
1878 if (pdata->phy_deinit)
1879 pdata->phy_deinit(u3d->phy_regs);
1880 clk_disable(u3d->clk);
1882 size = u3d->max_eps * sizeof(struct mv_u3d_ep_context) * 2;
1883 size = (size + MV_U3D_EP_CONTEXT_ALIGNMENT - 1)
1884 & ~(MV_U3D_EP_CONTEXT_ALIGNMENT - 1);
1885 u3d->ep_context = dma_alloc_coherent(&dev->dev, size,
1886 &u3d->ep_context_dma, GFP_KERNEL);
1887 if (!u3d->ep_context) {
1888 dev_err(&dev->dev, "allocate ep context memory failed\n");
1890 goto err_alloc_ep_context;
1892 u3d->ep_context_size = size;
1894 /* create TRB dma_pool resource */
1895 u3d->trb_pool = dma_pool_create("u3d_trb",
1897 sizeof(struct mv_u3d_trb_hw),
1898 MV_U3D_TRB_ALIGNMENT,
1899 MV_U3D_DMA_BOUNDARY);
1901 if (!u3d->trb_pool) {
1903 goto err_alloc_trb_pool;
1906 size = u3d->max_eps * sizeof(struct mv_u3d_ep) * 2;
1907 u3d->eps = kzalloc(size, GFP_KERNEL);
1909 dev_err(&dev->dev, "allocate ep memory failed\n");
1914 /* initialize ep0 status request structure */
1915 u3d->status_req = kzalloc(sizeof(struct mv_u3d_req) + 8, GFP_KERNEL);
1916 if (!u3d->status_req) {
1917 dev_err(&dev->dev, "allocate status_req memory failed\n");
1919 goto err_alloc_status_req;
1921 INIT_LIST_HEAD(&u3d->status_req->queue);
1923 /* allocate a small amount of memory to get valid address */
1924 u3d->status_req->req.buf = (char *)u3d->status_req
1925 + sizeof(struct mv_u3d_req);
1926 u3d->status_req->req.dma = virt_to_phys(u3d->status_req->req.buf);
1928 u3d->resume_state = USB_STATE_NOTATTACHED;
1929 u3d->usb_state = USB_STATE_ATTACHED;
1930 u3d->ep0_dir = MV_U3D_EP_DIR_OUT;
1931 u3d->remote_wakeup = 0;
1933 r = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1935 dev_err(&dev->dev, "no IRQ resource defined\n");
1939 u3d->irq = r->start;
1940 if (request_irq(u3d->irq, mv_u3d_irq,
1941 IRQF_SHARED, driver_name, u3d)) {
1943 dev_err(&dev->dev, "Request irq %d for u3d failed\n",
1946 goto err_request_irq;
1949 /* initialize gadget structure */
1950 u3d->gadget.ops = &mv_u3d_ops; /* usb_gadget_ops */
1951 u3d->gadget.ep0 = &u3d->eps[1].ep; /* gadget ep0 */
1952 INIT_LIST_HEAD(&u3d->gadget.ep_list); /* ep_list */
1953 u3d->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
1955 /* the "gadget" abstracts/virtualizes the controller */
1956 u3d->gadget.name = driver_name; /* gadget name */
1958 mv_u3d_eps_init(u3d);
1960 /* external vbus detection */
1962 u3d->clock_gating = 1;
1963 dev_err(&dev->dev, "external vbus detection\n");
1966 if (!u3d->clock_gating)
1967 u3d->vbus_active = 1;
1969 /* enable usb3 controller vbus detection */
1970 u3d->vbus_valid_detect = 1;
1972 retval = usb_add_gadget_udc(&dev->dev, &u3d->gadget);
1974 goto err_unregister;
1976 dev_dbg(&dev->dev, "successful probe usb3 device %s clock gating.\n",
1977 u3d->clock_gating ? "with" : "without");
1982 free_irq(u3d->irq, u3d);
1985 kfree(u3d->status_req);
1986 err_alloc_status_req:
1989 dma_pool_destroy(u3d->trb_pool);
1991 dma_free_coherent(&dev->dev, u3d->ep_context_size,
1992 u3d->ep_context, u3d->ep_context_dma);
1993 err_alloc_ep_context:
1994 if (pdata->phy_deinit)
1995 pdata->phy_deinit(u3d->phy_regs);
1996 clk_disable(u3d->clk);
1998 iounmap(u3d->cap_regs);
2009 #ifdef CONFIG_PM_SLEEP
2010 static int mv_u3d_suspend(struct device *dev)
2012 struct mv_u3d *u3d = dev_get_drvdata(dev);
2015 * only cable is unplugged, usb can suspend.
2016 * So do not care about clock_gating == 1, it is handled by
2019 if (!u3d->clock_gating) {
2020 mv_u3d_controller_stop(u3d);
2022 spin_lock_irq(&u3d->lock);
2023 /* stop all usb activities */
2024 mv_u3d_stop_activity(u3d, u3d->driver);
2025 spin_unlock_irq(&u3d->lock);
2027 mv_u3d_disable(u3d);
2033 static int mv_u3d_resume(struct device *dev)
2035 struct mv_u3d *u3d = dev_get_drvdata(dev);
2038 if (!u3d->clock_gating) {
2039 retval = mv_u3d_enable(u3d);
2043 if (u3d->driver && u3d->softconnect) {
2044 mv_u3d_controller_reset(u3d);
2045 mv_u3d_ep0_reset(u3d);
2046 mv_u3d_controller_start(u3d);
2054 static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops, mv_u3d_suspend, mv_u3d_resume);
2056 static void mv_u3d_shutdown(struct platform_device *dev)
2058 struct mv_u3d *u3d = platform_get_drvdata(dev);
2061 tmp = ioread32(&u3d->op_regs->usbcmd);
2062 tmp &= ~MV_U3D_CMD_RUN_STOP;
2063 iowrite32(tmp, &u3d->op_regs->usbcmd);
2066 static struct platform_driver mv_u3d_driver = {
2067 .probe = mv_u3d_probe,
2068 .remove = mv_u3d_remove,
2069 .shutdown = mv_u3d_shutdown,
2071 .owner = THIS_MODULE,
2073 .pm = &mv_u3d_pm_ops,
2077 module_platform_driver(mv_u3d_driver);
2078 MODULE_ALIAS("platform:mv-u3d");
2079 MODULE_DESCRIPTION(DRIVER_DESC);
2080 MODULE_AUTHOR("Yu Xu <yuxu@marvell.com>");
2081 MODULE_LICENSE("GPL");