1 // SPDX-License-Identifier: GPL-2.0
3 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
5 * Copyright (C) 2000-2002 Lineo
6 * by Stuart Lynne, Tom Rushworth, and Bruce Balden
7 * Copyright (C) 2002 Toshiba Corporation
8 * Copyright (C) 2003 MontaVista Software (source@mvista.com)
12 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
14 * - Endpoint numbering is fixed: ep{1,2,3}-bulk
15 * - Gadget drivers can choose ep maxpacket (8/16/32/64)
16 * - Gadget drivers can choose direction (IN, OUT)
17 * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
20 // #define VERBOSE /* extra debug messages (success too) */
21 // #define USB_TRACE /* packet-level success messages */
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/delay.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/errno.h>
30 #include <linux/timer.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/device.h>
36 #include <linux/usb/ch9.h>
37 #include <linux/usb/gadget.h>
38 #include <linux/prefetch.h>
40 #include <asm/byteorder.h>
43 #include <asm/unaligned.h>
48 #define DRIVER_DESC "TC86C001 USB Device Controller"
49 #define DRIVER_VERSION "30-Oct 2003"
51 static const char driver_name
[] = "goku_udc";
52 static const char driver_desc
[] = DRIVER_DESC
;
54 MODULE_AUTHOR("source@mvista.com");
55 MODULE_DESCRIPTION(DRIVER_DESC
);
56 MODULE_LICENSE("GPL");
60 * IN dma behaves ok under testing, though the IN-dma abort paths don't
61 * seem to behave quite as expected. Used by default.
63 * OUT dma documents design problems handling the common "short packet"
64 * transfer termination policy; it couldn't be enabled by default, even
65 * if the OUT-dma abort problems had a resolution.
67 static unsigned use_dma
= 1;
70 //#include <linux/moduleparam.h>
71 /* "modprobe goku_udc use_dma=1" etc
73 * 1 to use IN dma only (normal operation)
74 * 2 to use IN and OUT dma
76 module_param(use_dma
, uint
, S_IRUGO
);
79 /*-------------------------------------------------------------------------*/
81 static void nuke(struct goku_ep
*, int status
);
84 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
86 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
91 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
99 ep
= container_of(_ep
, struct goku_ep
, ep
);
101 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
104 if (ep
== &dev
->ep
[0])
106 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
108 if (ep
->num
!= usb_endpoint_num(desc
))
111 switch (usb_endpoint_type(desc
)) {
112 case USB_ENDPOINT_XFER_BULK
:
113 case USB_ENDPOINT_XFER_INT
:
119 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
120 != EPxSTATUS_EP_INVALID
)
123 /* enabling the no-toggle interrupt mode would need an api hook */
125 max
= get_unaligned_le16(&desc
->wMaxPacketSize
);
128 mode
++; /* fall through */
130 mode
++; /* fall through */
132 mode
++; /* fall through */
139 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
141 /* ep1/ep2 dma direction is chosen early; it works in the other
142 * direction, with pio. be cautious with out-dma.
144 ep
->is_in
= usb_endpoint_dir_in(desc
);
147 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
149 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
151 DBG(dev
, "%s out-dma hides short packets\n",
155 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
157 /* ep1 and ep2 can do double buffering and/or dma */
159 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
162 /* double buffer except (for now) with pio in */
163 tmp
= ((ep
->dma
|| !ep
->is_in
)
164 ? 0x10 /* double buffered */
165 : 0x11 /* single buffer */
167 tmp
|= readl(®s
->EPxSingle
);
168 writel(tmp
, ®s
->EPxSingle
);
170 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
171 tmp
|= readl(®s
->EPxBCS
);
172 writel(tmp
, ®s
->EPxBCS
);
174 writel(mode
, ep
->reg_mode
);
175 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
176 ep
->ep
.maxpacket
= max
;
179 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
181 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
182 ep
->is_in
? "IN" : "OUT",
183 ep
->dma
? "dma" : "pio",
189 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
191 struct goku_udc
*dev
= ep
->dev
;
194 command(regs
, COMMAND_INVALID
, ep
->num
);
196 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
197 dev
->int_enable
&= ~(INT_MSTWREND
199 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
200 dev
->int_enable
&= ~INT_MSTRDEND
;
201 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
203 dev
->int_enable
&= ~INT_EP0
;
204 writel(dev
->int_enable
, ®s
->int_enable
);
205 readl(®s
->int_enable
);
207 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
210 tmp
= readl(&r
->EPxSingle
);
211 tmp
&= ~(0x11 << ep
->num
);
212 writel(tmp
, &r
->EPxSingle
);
214 tmp
= readl(&r
->EPxBCS
);
215 tmp
&= ~(0x11 << ep
->num
);
216 writel(tmp
, &r
->EPxBCS
);
218 /* reset dma in case we're still using it */
222 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
223 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
224 master
&= ~MST_W_BITS
;
225 master
|= MST_WR_RESET
;
227 master
&= ~MST_R_BITS
;
228 master
|= MST_RD_RESET
;
230 writel(master
, ®s
->dma_master
);
234 usb_ep_set_maxpacket_limit(&ep
->ep
, MAX_FIFO_SIZE
);
241 static int goku_ep_disable(struct usb_ep
*_ep
)
244 struct goku_udc
*dev
;
247 ep
= container_of(_ep
, struct goku_ep
, ep
);
248 if (!_ep
|| !ep
->ep
.desc
)
251 if (dev
->ep0state
== EP0_SUSPEND
)
254 VDBG(dev
, "disable %s\n", _ep
->name
);
256 spin_lock_irqsave(&dev
->lock
, flags
);
257 nuke(ep
, -ESHUTDOWN
);
258 ep_reset(dev
->regs
, ep
);
259 spin_unlock_irqrestore(&dev
->lock
, flags
);
264 /*-------------------------------------------------------------------------*/
266 static struct usb_request
*
267 goku_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
269 struct goku_request
*req
;
273 req
= kzalloc(sizeof *req
, gfp_flags
);
277 INIT_LIST_HEAD(&req
->queue
);
282 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
284 struct goku_request
*req
;
289 req
= container_of(_req
, struct goku_request
, req
);
290 WARN_ON(!list_empty(&req
->queue
));
294 /*-------------------------------------------------------------------------*/
297 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
299 struct goku_udc
*dev
;
300 unsigned stopped
= ep
->stopped
;
302 list_del_init(&req
->queue
);
304 if (likely(req
->req
.status
== -EINPROGRESS
))
305 req
->req
.status
= status
;
307 status
= req
->req
.status
;
312 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
315 if (status
&& status
!= -ESHUTDOWN
)
317 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
318 ep
->ep
.name
, &req
->req
, status
,
319 req
->req
.actual
, req
->req
.length
);
321 /* don't modify queue heads during completion callback */
323 spin_unlock(&dev
->lock
);
324 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
325 spin_lock(&dev
->lock
);
326 ep
->stopped
= stopped
;
329 /*-------------------------------------------------------------------------*/
332 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
334 unsigned length
, count
;
336 length
= min(req
->req
.length
- req
->req
.actual
, max
);
337 req
->req
.actual
+= length
;
340 while (likely(count
--))
341 writel(*buf
++, fifo
);
345 // return: 0 = still running, 1 = completed, negative = errno
346 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
348 struct goku_udc
*dev
= ep
->dev
;
354 tmp
= readl(&dev
->regs
->DataSet
);
355 buf
= req
->req
.buf
+ req
->req
.actual
;
359 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
362 /* NOTE: just single-buffered PIO-IN for now. */
363 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
366 /* clear our "packet available" irq */
368 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
370 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
372 /* last packet often short (sometimes a zlp, especially on ep0) */
373 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
374 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
376 dev
->ep
[0].stopped
= 1;
377 dev
->ep0state
= EP0_STATUS
;
381 if (likely(req
->req
.length
!= req
->req
.actual
)
387 #if 0 /* printk seemed to trash is_last...*/
389 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
390 ep
->ep
.name
, count
, is_last
? "/last" : "",
391 req
->req
.length
- req
->req
.actual
, req
);
394 /* requests complete when all IN data is in the FIFO,
395 * or sometimes later, if a zlp was needed.
405 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
407 struct goku_udc_regs __iomem
*regs
;
410 unsigned bufferspace
, is_short
, dbuff
;
412 regs
= ep
->dev
->regs
;
414 buf
= req
->req
.buf
+ req
->req
.actual
;
417 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
420 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
422 /* ack dataset irq matching the status we'll handle */
424 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
426 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
427 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
428 bufferspace
= req
->req
.length
- req
->req
.actual
;
430 /* usually do nothing without an OUT packet */
431 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
432 if (unlikely(set
== 0))
434 /* use ep1/ep2 double-buffering for OUT */
435 if (!(size
& PACKET_ACTIVE
))
436 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
437 if (!(size
& PACKET_ACTIVE
)) /* "can't happen" */
439 size
&= DATASIZE
; /* EPxSizeH == 0 */
441 /* ep0out no-out-data case for set_config, etc */
445 /* read all bytes from this packet */
446 req
->req
.actual
+= size
;
447 is_short
= (size
< ep
->ep
.maxpacket
);
449 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
450 ep
->ep
.name
, size
, is_short
? "/S" : "",
451 req
, req
->req
.actual
, req
->req
.length
);
453 while (likely(size
-- != 0)) {
454 u8 byte
= (u8
) readl(ep
->reg_fifo
);
456 if (unlikely(bufferspace
== 0)) {
457 /* this happens when the driver's buffer
458 * is smaller than what the host sent.
459 * discard the extra data in this packet.
461 if (req
->req
.status
!= -EOVERFLOW
)
462 DBG(ep
->dev
, "%s overflow %u\n",
464 req
->req
.status
= -EOVERFLOW
;
472 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
473 if (unlikely(ep
->num
== 0)) {
474 /* non-control endpoints now usable? */
475 if (ep
->dev
->req_config
)
476 writel(ep
->dev
->configured
477 ? USBSTATE_CONFIGURED
480 /* ep0out status stage */
481 writel(~(1<<0), ®s
->EOP
);
483 ep
->dev
->ep0state
= EP0_STATUS
;
487 /* empty the second buffer asap */
488 if (dbuff
&& !list_empty(&ep
->queue
)) {
489 req
= list_entry(ep
->queue
.next
,
490 struct goku_request
, queue
);
500 pio_irq_enable(struct goku_udc
*dev
,
501 struct goku_udc_regs __iomem
*regs
, int epnum
)
503 dev
->int_enable
|= INT_EPxDATASET (epnum
);
504 writel(dev
->int_enable
, ®s
->int_enable
);
505 /* write may still be posted */
509 pio_irq_disable(struct goku_udc
*dev
,
510 struct goku_udc_regs __iomem
*regs
, int epnum
)
512 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
513 writel(dev
->int_enable
, ®s
->int_enable
);
514 /* write may still be posted */
518 pio_advance(struct goku_ep
*ep
)
520 struct goku_request
*req
;
522 if (unlikely(list_empty (&ep
->queue
)))
524 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
525 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
529 /*-------------------------------------------------------------------------*/
531 // return: 0 = q running, 1 = q stopped, negative = errno
532 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
534 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
536 u32 start
= req
->req
.dma
;
537 u32 end
= start
+ req
->req
.length
- 1;
539 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
541 /* re-init the bits affecting IN dma; careful with zlps */
542 if (likely(ep
->is_in
)) {
543 if (unlikely(master
& MST_RD_ENA
)) {
544 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
548 writel(end
, ®s
->in_dma_end
);
549 writel(start
, ®s
->in_dma_start
);
551 master
&= ~MST_R_BITS
;
552 if (unlikely(req
->req
.length
== 0))
553 master
= MST_RD_ENA
| MST_RD_EOPB
;
554 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
556 master
= MST_RD_ENA
| MST_EOPB_ENA
;
558 master
= MST_RD_ENA
| MST_EOPB_DIS
;
560 ep
->dev
->int_enable
|= INT_MSTRDEND
;
562 /* Goku DMA-OUT merges short packets, which plays poorly with
563 * protocols where short packets mark the transfer boundaries.
564 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
565 * ending transfers after 3 SOFs; we don't turn it on.
568 if (unlikely(master
& MST_WR_ENA
)) {
569 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
573 writel(end
, ®s
->out_dma_end
);
574 writel(start
, ®s
->out_dma_start
);
576 master
&= ~MST_W_BITS
;
577 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
579 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
582 writel(master
, ®s
->dma_master
);
583 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
587 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
589 struct goku_request
*req
;
590 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
593 master
= readl(®s
->dma_master
);
595 if (unlikely(list_empty(&ep
->queue
))) {
598 dev
->int_enable
&= ~INT_MSTRDEND
;
600 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
601 writel(dev
->int_enable
, ®s
->int_enable
);
604 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
606 /* normal hw dma completion (not abort) */
607 if (likely(ep
->is_in
)) {
608 if (unlikely(master
& MST_RD_ENA
))
610 req
->req
.actual
= readl(®s
->in_dma_current
);
612 if (unlikely(master
& MST_WR_ENA
))
615 /* hardware merges short packets, and also hides packet
616 * overruns. a partial packet MAY be in the fifo here.
618 req
->req
.actual
= readl(®s
->out_dma_current
);
620 req
->req
.actual
-= req
->req
.dma
;
624 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
625 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
626 req
->req
.actual
, req
->req
.length
, req
);
629 if (list_empty(&ep
->queue
))
631 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
632 (void) start_dma(ep
, req
);
635 static void abort_dma(struct goku_ep
*ep
, int status
)
637 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
638 struct goku_request
*req
;
641 /* NAK future host requests, hoping the implicit delay lets the
642 * dma engine finish reading (or writing) its latest packet and
643 * empty the dma buffer (up to 16 bytes).
645 * This avoids needing to clean up a partial packet in the fifo;
646 * we can't do that for IN without side effects to HALT and TOGGLE.
648 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
649 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
650 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
652 /* FIXME using these resets isn't usably documented. this may
653 * not work unless it's followed by disabling the endpoint.
655 * FIXME the OUT reset path doesn't even behave consistently.
658 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
660 curr
= readl(®s
->in_dma_current
);
662 writel(curr
, ®s
->in_dma_end
);
663 writel(curr
, ®s
->in_dma_start
);
665 master
&= ~MST_R_BITS
;
666 master
|= MST_RD_RESET
;
667 writel(master
, ®s
->dma_master
);
669 if (readl(®s
->dma_master
) & MST_RD_ENA
)
670 DBG(ep
->dev
, "IN dma active after reset!\n");
673 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
675 curr
= readl(®s
->out_dma_current
);
677 writel(curr
, ®s
->out_dma_end
);
678 writel(curr
, ®s
->out_dma_start
);
680 master
&= ~MST_W_BITS
;
681 master
|= MST_WR_RESET
;
682 writel(master
, ®s
->dma_master
);
684 if (readl(®s
->dma_master
) & MST_WR_ENA
)
685 DBG(ep
->dev
, "OUT dma active after reset!\n");
687 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
688 req
->req
.status
= status
;
690 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __func__
, ep
->ep
.name
,
691 ep
->is_in
? "IN" : "OUT",
692 req
->req
.actual
, req
->req
.length
);
694 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
699 /* dma already completed; no abort needed */
700 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
701 req
->req
.actual
= req
->req
.length
;
705 /*-------------------------------------------------------------------------*/
708 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
710 struct goku_request
*req
;
712 struct goku_udc
*dev
;
716 /* always require a cpu-view buffer so pio works */
717 req
= container_of(_req
, struct goku_request
, req
);
718 if (unlikely(!_req
|| !_req
->complete
719 || !_req
->buf
|| !list_empty(&req
->queue
)))
721 ep
= container_of(_ep
, struct goku_ep
, ep
);
722 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->num
!= 0)))
725 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
728 /* can't touch registers when suspended */
729 if (dev
->ep0state
== EP0_SUSPEND
)
732 /* set up dma mapping in case the caller didn't */
734 status
= usb_gadget_map_request(&dev
->gadget
, &req
->req
,
741 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
742 _ep
->name
, _req
, _req
->length
, _req
->buf
);
745 spin_lock_irqsave(&dev
->lock
, flags
);
747 _req
->status
= -EINPROGRESS
;
750 /* for ep0 IN without premature status, zlp is required and
751 * writing EOP starts the status stage (OUT).
753 if (unlikely(ep
->num
== 0 && ep
->is_in
))
756 /* kickstart this i/o queue? */
758 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
759 /* dma: done after dma completion IRQ (or error)
760 * pio: done after last fifo operation
763 status
= start_dma(ep
, req
);
765 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
767 if (unlikely(status
!= 0)) {
773 } /* else pio or dma irq handler advances the queue. */
775 if (likely(req
!= NULL
))
776 list_add_tail(&req
->queue
, &ep
->queue
);
778 if (likely(!list_empty(&ep
->queue
))
779 && likely(ep
->num
!= 0)
781 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
782 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
784 spin_unlock_irqrestore(&dev
->lock
, flags
);
786 /* pci writes may still be posted */
790 /* dequeue ALL requests */
791 static void nuke(struct goku_ep
*ep
, int status
)
793 struct goku_request
*req
;
796 if (list_empty(&ep
->queue
))
799 abort_dma(ep
, status
);
800 while (!list_empty(&ep
->queue
)) {
801 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
802 done(ep
, req
, status
);
806 /* dequeue JUST ONE request */
807 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
809 struct goku_request
*req
;
811 struct goku_udc
*dev
;
814 ep
= container_of(_ep
, struct goku_ep
, ep
);
815 if (!_ep
|| !_req
|| (!ep
->ep
.desc
&& ep
->num
!= 0))
821 /* we can't touch (dma) registers when suspended */
822 if (dev
->ep0state
== EP0_SUSPEND
)
825 VDBG(dev
, "%s %s %s %s %p\n", __func__
, _ep
->name
,
826 ep
->is_in
? "IN" : "OUT",
827 ep
->dma
? "dma" : "pio",
830 spin_lock_irqsave(&dev
->lock
, flags
);
832 /* make sure it's actually queued on this endpoint */
833 list_for_each_entry (req
, &ep
->queue
, queue
) {
834 if (&req
->req
== _req
)
837 if (&req
->req
!= _req
) {
838 spin_unlock_irqrestore (&dev
->lock
, flags
);
842 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
843 abort_dma(ep
, -ECONNRESET
);
844 done(ep
, req
, -ECONNRESET
);
845 dma_advance(dev
, ep
);
846 } else if (!list_empty(&req
->queue
))
847 done(ep
, req
, -ECONNRESET
);
850 spin_unlock_irqrestore(&dev
->lock
, flags
);
852 return req
? 0 : -EOPNOTSUPP
;
855 /*-------------------------------------------------------------------------*/
857 static void goku_clear_halt(struct goku_ep
*ep
)
859 // assert (ep->num !=0)
860 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
861 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
862 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
866 struct goku_request
*req
;
868 if (list_empty(&ep
->queue
))
870 req
= list_entry(ep
->queue
.next
, struct goku_request
,
872 (void) start_dma(ep
, req
);
878 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
886 ep
= container_of (_ep
, struct goku_ep
, ep
);
890 ep
->dev
->ep0state
= EP0_STALL
;
891 ep
->dev
->ep
[0].stopped
= 1;
895 /* don't change EPxSTATUS_EP_INVALID to READY */
896 } else if (!ep
->ep
.desc
) {
897 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
901 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
902 if (!list_empty(&ep
->queue
))
904 else if (ep
->is_in
&& value
905 /* data in (either) packet buffer? */
906 && (readl(&ep
->dev
->regs
->DataSet
)
907 & DATASET_AB(ep
->num
)))
913 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
914 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
915 readl(ep
->reg_status
);
917 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
921 static int goku_fifo_status(struct usb_ep
*_ep
)
924 struct goku_udc_regs __iomem
*regs
;
929 ep
= container_of(_ep
, struct goku_ep
, ep
);
931 /* size is only reported sanely for OUT */
935 /* ignores 16-byte dma buffer; SizeH == 0 */
936 regs
= ep
->dev
->regs
;
937 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
938 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
939 VDBG(ep
->dev
, "%s %s %u\n", __func__
, ep
->ep
.name
, size
);
943 static void goku_fifo_flush(struct usb_ep
*_ep
)
946 struct goku_udc_regs __iomem
*regs
;
951 ep
= container_of(_ep
, struct goku_ep
, ep
);
952 VDBG(ep
->dev
, "%s %s\n", __func__
, ep
->ep
.name
);
954 /* don't change EPxSTATUS_EP_INVALID to READY */
955 if (!ep
->ep
.desc
&& ep
->num
!= 0) {
956 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
960 regs
= ep
->dev
->regs
;
961 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
964 /* Non-desirable behavior: FIFO_CLEAR also clears the
965 * endpoint halt feature. For OUT, we _could_ just read
966 * the bytes out (PIO, if !ep->dma); for in, no choice.
969 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
972 static const struct usb_ep_ops goku_ep_ops
= {
973 .enable
= goku_ep_enable
,
974 .disable
= goku_ep_disable
,
976 .alloc_request
= goku_alloc_request
,
977 .free_request
= goku_free_request
,
980 .dequeue
= goku_dequeue
,
982 .set_halt
= goku_set_halt
,
983 .fifo_status
= goku_fifo_status
,
984 .fifo_flush
= goku_fifo_flush
,
987 /*-------------------------------------------------------------------------*/
989 static int goku_get_frame(struct usb_gadget
*_gadget
)
994 static struct usb_ep
*goku_match_ep(struct usb_gadget
*g
,
995 struct usb_endpoint_descriptor
*desc
,
996 struct usb_ss_ep_comp_descriptor
*ep_comp
)
998 struct goku_udc
*dev
= to_goku_udc(g
);
1001 switch (usb_endpoint_type(desc
)) {
1002 case USB_ENDPOINT_XFER_INT
:
1003 /* single buffering is enough */
1004 ep
= &dev
->ep
[3].ep
;
1005 if (usb_gadget_ep_match_desc(g
, ep
, desc
, ep_comp
))
1008 case USB_ENDPOINT_XFER_BULK
:
1009 if (usb_endpoint_dir_in(desc
)) {
1010 /* DMA may be available */
1011 ep
= &dev
->ep
[2].ep
;
1012 if (usb_gadget_ep_match_desc(g
, ep
, desc
, ep_comp
))
1023 static int goku_udc_start(struct usb_gadget
*g
,
1024 struct usb_gadget_driver
*driver
);
1025 static int goku_udc_stop(struct usb_gadget
*g
);
1027 static const struct usb_gadget_ops goku_ops
= {
1028 .get_frame
= goku_get_frame
,
1029 .udc_start
= goku_udc_start
,
1030 .udc_stop
= goku_udc_stop
,
1031 .match_ep
= goku_match_ep
,
1036 /*-------------------------------------------------------------------------*/
1038 static inline const char *dmastr(void)
1041 return "(dma disabled)";
1042 else if (use_dma
== 2)
1043 return "(dma IN and OUT)";
1048 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1050 static const char proc_node_name
[] = "driver/udc";
1052 #define FOURBITS "%s%s%s%s"
1053 #define EIGHTBITS FOURBITS FOURBITS
1055 static void dump_intmask(struct seq_file
*m
, const char *label
, u32 mask
)
1057 /* int_status is the same format ... */
1058 seq_printf(m
, "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1060 (mask
& INT_PWRDETECT
) ? " power" : "",
1061 (mask
& INT_SYSERROR
) ? " sys" : "",
1062 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1063 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1065 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1066 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1067 (mask
& INT_ERR
) ? " err" : "",
1068 (mask
& INT_SOF
) ? " sof" : "",
1070 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1071 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1072 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1073 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1075 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1076 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1077 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1078 (mask
& INT_STATUS
) ? " ep0status" : "",
1080 (mask
& INT_SETUP
) ? " setup" : "",
1081 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1082 (mask
& INT_USBRESET
) ? " reset" : "",
1083 (mask
& INT_SUSPEND
) ? " suspend" : "");
1086 static const char *udc_ep_state(enum ep0state state
)
1089 case EP0_DISCONNECT
:
1090 return "ep0_disconnect";
1098 return "ep0_status";
1102 return "ep0_suspend";
1108 static const char *udc_ep_status(u32 status
)
1110 switch (status
& EPxSTATUS_EP_MASK
) {
1111 case EPxSTATUS_EP_READY
:
1113 case EPxSTATUS_EP_DATAIN
:
1115 case EPxSTATUS_EP_FULL
:
1117 case EPxSTATUS_EP_TX_ERR
: /* host will retry */
1119 case EPxSTATUS_EP_RX_ERR
:
1121 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1123 case EPxSTATUS_EP_STALL
:
1125 case EPxSTATUS_EP_INVALID
: /* these "can't happen" */
1132 static int udc_proc_read(struct seq_file
*m
, void *v
)
1134 struct goku_udc
*dev
= m
->private;
1135 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1136 unsigned long flags
;
1137 int i
, is_usb_connected
;
1140 local_irq_save(flags
);
1142 /* basic device status */
1143 tmp
= readl(®s
->power_detect
);
1144 is_usb_connected
= tmp
& PW_DETECT
;
1147 "%s version: %s %s\n"
1148 "Gadget driver: %s\n"
1151 pci_name(dev
->pdev
), driver_desc
,
1152 driver_name
, DRIVER_VERSION
, dmastr(),
1153 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1155 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1157 udc_ep_state(dev
->ep0state
));
1159 dump_intmask(m
, "int_status", readl(®s
->int_status
));
1160 dump_intmask(m
, "int_enable", readl(®s
->int_enable
));
1162 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1165 /* registers for (active) device and ep0 */
1166 seq_printf(m
, "\nirqs %lu\ndataset %02x single.bcs %02x.%02x state %x addr %u\n",
1167 dev
->irqs
, readl(®s
->DataSet
),
1168 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1169 readl(®s
->UsbState
),
1170 readl(®s
->address
));
1171 if (seq_has_overflowed(m
))
1174 tmp
= readl(®s
->dma_master
);
1175 seq_printf(m
, "dma %03X =" EIGHTBITS
"%s %s\n",
1177 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1178 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1179 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1180 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1182 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1183 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1184 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1185 (tmp
& MST_RD_ENA
) ? " IN" : "",
1187 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1188 (tmp
& MST_CONNECTION
) ? "ep1in/ep2out" : "ep1out/ep2in");
1189 if (seq_has_overflowed(m
))
1192 /* dump endpoint queues */
1193 for (i
= 0; i
< 4; i
++) {
1194 struct goku_ep
*ep
= &dev
->ep
[i
];
1195 struct goku_request
*req
;
1197 if (i
&& !ep
->ep
.desc
)
1200 tmp
= readl(ep
->reg_status
);
1201 seq_printf(m
, "%s %s max %u %s, irqs %lu, status %02x (%s) " FOURBITS
"\n",
1203 ep
->is_in
? "in" : "out",
1205 ep
->dma
? "dma" : "pio",
1207 tmp
, udc_ep_status(tmp
),
1208 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1209 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1210 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1211 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : "");
1212 if (seq_has_overflowed(m
))
1215 if (list_empty(&ep
->queue
)) {
1216 seq_puts(m
, "\t(nothing queued)\n");
1217 if (seq_has_overflowed(m
))
1221 list_for_each_entry(req
, &ep
->queue
, queue
) {
1222 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1223 if (i
== UDC_MSTRD_ENDPOINT
)
1224 tmp
= readl(®s
->in_dma_current
);
1226 tmp
= readl(®s
->out_dma_current
);
1227 tmp
-= req
->req
.dma
;
1230 tmp
= req
->req
.actual
;
1232 seq_printf(m
, "\treq %p len %u/%u buf %p\n",
1233 &req
->req
, tmp
, req
->req
.length
,
1235 if (seq_has_overflowed(m
))
1241 local_irq_restore(flags
);
1246 * seq_file wrappers for procfile show routines.
1248 static int udc_proc_open(struct inode
*inode
, struct file
*file
)
1250 return single_open(file
, udc_proc_read
, PDE_DATA(file_inode(file
)));
1253 static const struct file_operations udc_proc_fops
= {
1254 .open
= udc_proc_open
,
1256 .llseek
= seq_lseek
,
1257 .release
= single_release
,
1260 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1262 /*-------------------------------------------------------------------------*/
1264 static void udc_reinit (struct goku_udc
*dev
)
1266 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1270 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1271 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1272 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1273 dev
->ep0state
= EP0_DISCONNECT
;
1276 for (i
= 0; i
< 4; i
++) {
1277 struct goku_ep
*ep
= &dev
->ep
[i
];
1280 ep
->ep
.name
= names
[i
];
1281 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1282 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1283 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1285 ep
->ep
.ops
= &goku_ep_ops
;
1286 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1288 INIT_LIST_HEAD (&ep
->queue
);
1293 ep
->ep
.caps
.type_control
= true;
1295 ep
->ep
.caps
.type_bulk
= true;
1297 ep
->ep
.caps
.dir_in
= true;
1298 ep
->ep
.caps
.dir_out
= true;
1301 dev
->ep
[0].reg_mode
= NULL
;
1302 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, MAX_EP0_SIZE
);
1303 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1306 static void udc_reset(struct goku_udc
*dev
)
1308 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1310 writel(0, ®s
->power_detect
);
1311 writel(0, ®s
->int_enable
);
1312 readl(®s
->int_enable
);
1313 dev
->int_enable
= 0;
1315 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1316 * don't let INT_PWRDETECT sequence begin
1319 writel(PW_RESETB
, ®s
->power_detect
);
1320 readl(®s
->int_enable
);
1323 static void ep0_start(struct goku_udc
*dev
)
1325 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1328 VDBG(dev
, "%s\n", __func__
);
1332 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1334 /* hw handles set_address, set_feature, get_status; maybe more */
1335 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1336 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1337 | G_REQMODE_GET_DESC
1338 | G_REQMODE_CLEAR_FEAT
1341 for (i
= 0; i
< 4; i
++)
1342 dev
->ep
[i
].irqs
= 0;
1344 /* can't modify descriptors after writing UsbReady */
1345 for (i
= 0; i
< DESC_LEN
; i
++)
1346 writel(0, ®s
->descriptors
[i
]);
1347 writel(0, ®s
->UsbReady
);
1349 /* expect ep0 requests when the host drops reset */
1350 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1351 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1352 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1353 readl(®s
->int_enable
);
1354 dev
->gadget
.speed
= USB_SPEED_FULL
;
1355 dev
->ep0state
= EP0_IDLE
;
1358 static void udc_enable(struct goku_udc
*dev
)
1360 /* start enumeration now, or after power detect irq */
1361 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1364 DBG(dev
, "%s\n", __func__
);
1365 dev
->int_enable
= INT_PWRDETECT
;
1366 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1370 /*-------------------------------------------------------------------------*/
1372 /* keeping it simple:
1373 * - one bus driver, initted first;
1374 * - one function driver, initted second
1377 /* when a driver is successfully registered, it will receive
1378 * control requests including set_configuration(), which enables
1379 * non-control requests. then usb traffic follows until a
1380 * disconnect is reported. then a host may connect again, or
1381 * the driver might get unbound.
1383 static int goku_udc_start(struct usb_gadget
*g
,
1384 struct usb_gadget_driver
*driver
)
1386 struct goku_udc
*dev
= to_goku_udc(g
);
1388 /* hook up the driver */
1389 driver
->driver
.bus
= NULL
;
1390 dev
->driver
= driver
;
1393 * then enable host detection and ep0; and we're ready
1394 * for set_configuration as well as eventual disconnect.
1401 static void stop_activity(struct goku_udc
*dev
)
1405 DBG (dev
, "%s\n", __func__
);
1407 /* disconnect gadget driver after quiesceing hw and the driver */
1409 for (i
= 0; i
< 4; i
++)
1410 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1416 static int goku_udc_stop(struct usb_gadget
*g
)
1418 struct goku_udc
*dev
= to_goku_udc(g
);
1419 unsigned long flags
;
1421 spin_lock_irqsave(&dev
->lock
, flags
);
1424 spin_unlock_irqrestore(&dev
->lock
, flags
);
1429 /*-------------------------------------------------------------------------*/
1431 static void ep0_setup(struct goku_udc
*dev
)
1433 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1434 struct usb_ctrlrequest ctrl
;
1437 /* read SETUP packet and enter DATA stage */
1438 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1439 ctrl
.bRequest
= readl(®s
->bRequest
);
1440 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1441 | readl(®s
->wValueL
));
1442 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1443 | readl(®s
->wIndexL
));
1444 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1445 | readl(®s
->wLengthL
));
1446 writel(0, ®s
->SetupRecv
);
1448 nuke(&dev
->ep
[0], 0);
1449 dev
->ep
[0].stopped
= 0;
1450 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1451 dev
->ep
[0].is_in
= 1;
1452 dev
->ep0state
= EP0_IN
;
1453 /* detect early status stages */
1454 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1456 dev
->ep
[0].is_in
= 0;
1457 dev
->ep0state
= EP0_OUT
;
1459 /* NOTE: CLEAR_FEATURE is done in software so that we can
1460 * synchronize transfer restarts after bulk IN stalls. data
1461 * won't even enter the fifo until the halt is cleared.
1463 switch (ctrl
.bRequest
) {
1464 case USB_REQ_CLEAR_FEATURE
:
1465 switch (ctrl
.bRequestType
) {
1466 case USB_RECIP_ENDPOINT
:
1467 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1468 /* active endpoint */
1470 (!dev
->ep
[tmp
].ep
.desc
&& tmp
!= 0))
1472 if (ctrl
.wIndex
& cpu_to_le16(
1474 if (!dev
->ep
[tmp
].is_in
)
1477 if (dev
->ep
[tmp
].is_in
)
1480 if (ctrl
.wValue
!= cpu_to_le16(
1484 goku_clear_halt(&dev
->ep
[tmp
]);
1486 /* start ep0out status stage */
1487 writel(~(1<<0), ®s
->EOP
);
1488 dev
->ep
[0].stopped
= 1;
1489 dev
->ep0state
= EP0_STATUS
;
1491 case USB_RECIP_DEVICE
:
1492 /* device remote wakeup: always clear */
1493 if (ctrl
.wValue
!= cpu_to_le16(1))
1495 VDBG(dev
, "clear dev remote wakeup\n");
1497 case USB_RECIP_INTERFACE
:
1499 default: /* pass to gadget driver */
1509 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1510 ctrl
.bRequestType
, ctrl
.bRequest
,
1511 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1512 le16_to_cpu(ctrl
.wLength
));
1515 /* hw wants to know when we're configured (or not) */
1516 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1517 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1518 if (unlikely(dev
->req_config
))
1519 dev
->configured
= (ctrl
.wValue
!= cpu_to_le16(0));
1521 /* delegate everything to the gadget driver.
1522 * it may respond after this irq handler returns.
1524 spin_unlock (&dev
->lock
);
1525 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1526 spin_lock (&dev
->lock
);
1527 if (unlikely(tmp
< 0)) {
1530 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1531 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1533 command(regs
, COMMAND_STALL
, 0);
1534 dev
->ep
[0].stopped
= 1;
1535 dev
->ep0state
= EP0_STALL
;
1538 /* expect at least one data or status stage irq */
1541 #define ACK(irqbit) { \
1543 writel(~irqbit, ®s->int_status); \
1547 static irqreturn_t
goku_irq(int irq
, void *_dev
)
1549 struct goku_udc
*dev
= _dev
;
1550 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1552 u32 stat
, handled
= 0;
1553 unsigned i
, rescans
= 5;
1555 spin_lock(&dev
->lock
);
1558 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1563 /* device-wide irqs */
1564 if (unlikely(stat
& INT_DEVWIDE
)) {
1565 if (stat
& INT_SYSERROR
) {
1566 ERROR(dev
, "system error\n");
1570 // FIXME have a neater way to prevent re-enumeration
1574 if (stat
& INT_PWRDETECT
) {
1575 writel(~stat
, ®s
->int_status
);
1576 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1577 VDBG(dev
, "connect\n");
1580 DBG(dev
, "disconnect\n");
1581 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1583 dev
->ep0state
= EP0_DISCONNECT
;
1584 dev
->int_enable
= INT_DEVWIDE
;
1585 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1591 if (stat
& INT_SUSPEND
) {
1593 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1594 switch (dev
->ep0state
) {
1595 case EP0_DISCONNECT
:
1601 DBG(dev
, "USB suspend\n");
1602 dev
->ep0state
= EP0_SUSPEND
;
1603 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1605 && dev
->driver
->suspend
) {
1606 spin_unlock(&dev
->lock
);
1607 dev
->driver
->suspend(&dev
->gadget
);
1608 spin_lock(&dev
->lock
);
1611 if (dev
->ep0state
!= EP0_SUSPEND
) {
1612 DBG(dev
, "bogus USB resume %d\n",
1616 DBG(dev
, "USB resume\n");
1617 dev
->ep0state
= EP0_IDLE
;
1618 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1620 && dev
->driver
->resume
) {
1621 spin_unlock(&dev
->lock
);
1622 dev
->driver
->resume(&dev
->gadget
);
1623 spin_lock(&dev
->lock
);
1628 if (stat
& INT_USBRESET
) { /* hub reset done */
1630 INFO(dev
, "USB reset done, gadget %s\n",
1631 dev
->driver
->driver
.name
);
1633 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1636 /* progress ep0 setup, data, or status stages.
1637 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1639 if (stat
& INT_SETUP
) {
1644 if (stat
& INT_STATUSNAK
) {
1645 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1646 if (dev
->ep0state
== EP0_IN
) {
1650 writel(~(1<<0), ®s
->EOP
);
1651 dev
->ep0state
= EP0_STATUS
;
1654 if (stat
& INT_ENDPOINT0
) {
1661 /* dma completion */
1662 if (stat
& INT_MSTRDEND
) { /* IN */
1664 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1666 dma_advance(dev
, ep
);
1668 if (stat
& INT_MSTWREND
) { /* OUT */
1670 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1672 dma_advance(dev
, ep
);
1674 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1675 ACK(INT_MSTWRTMOUT
);
1676 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1678 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1679 // reset dma? then dma_advance()
1683 for (i
= 1; i
< 4; i
++) {
1684 u32 tmp
= INT_EPxDATASET(i
);
1690 if (list_empty (&ep
->queue
))
1691 pio_irq_disable(dev
, regs
, i
);
1701 (void)readl(®s
->int_enable
);
1702 spin_unlock(&dev
->lock
);
1704 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1705 readl(®s
->int_status
), dev
->int_enable
);
1706 return IRQ_RETVAL(handled
);
1711 /*-------------------------------------------------------------------------*/
1713 static void gadget_release(struct device
*_dev
)
1715 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1720 /* tear down the binding between this driver and the pci device */
1722 static void goku_remove(struct pci_dev
*pdev
)
1724 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1726 DBG(dev
, "%s\n", __func__
);
1728 usb_del_gadget_udc(&dev
->gadget
);
1730 BUG_ON(dev
->driver
);
1732 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1733 remove_proc_entry(proc_node_name
, NULL
);
1738 free_irq(pdev
->irq
, dev
);
1741 if (dev
->got_region
)
1742 release_mem_region(pci_resource_start (pdev
, 0),
1743 pci_resource_len (pdev
, 0));
1745 pci_disable_device(pdev
);
1749 INFO(dev
, "unbind\n");
1752 /* wrap this driver around the specified pci device, but
1753 * don't respond over USB until a gadget driver binds to us.
1756 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1758 struct goku_udc
*dev
= NULL
;
1759 unsigned long resource
, len
;
1760 void __iomem
*base
= NULL
;
1764 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1769 /* alloc, and start init */
1770 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
1776 spin_lock_init(&dev
->lock
);
1778 dev
->gadget
.ops
= &goku_ops
;
1779 dev
->gadget
.max_speed
= USB_SPEED_FULL
;
1781 /* the "gadget" abstracts/virtualizes the controller */
1782 dev
->gadget
.name
= driver_name
;
1784 /* now all the pci goodies ... */
1785 retval
= pci_enable_device(pdev
);
1787 DBG(dev
, "can't enable, %d\n", retval
);
1792 resource
= pci_resource_start(pdev
, 0);
1793 len
= pci_resource_len(pdev
, 0);
1794 if (!request_mem_region(resource
, len
, driver_name
)) {
1795 DBG(dev
, "controller already in use\n");
1799 dev
->got_region
= 1;
1801 base
= ioremap_nocache(resource
, len
);
1803 DBG(dev
, "can't map memory\n");
1807 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1809 pci_set_drvdata(pdev
, dev
);
1810 INFO(dev
, "%s\n", driver_desc
);
1811 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1812 INFO(dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
1814 /* init to known state, then setup irqs */
1817 if (request_irq(pdev
->irq
, goku_irq
, IRQF_SHARED
,
1818 driver_name
, dev
) != 0) {
1819 DBG(dev
, "request interrupt %d failed\n", pdev
->irq
);
1825 pci_set_master(pdev
);
1828 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1829 proc_create_data(proc_node_name
, 0, NULL
, &udc_proc_fops
, dev
);
1832 retval
= usb_add_gadget_udc_release(&pdev
->dev
, &dev
->gadget
,
1842 /* gadget_release is not registered yet, kfree explicitly */
1848 /*-------------------------------------------------------------------------*/
1850 static const struct pci_device_id pci_ids
[] = { {
1851 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
1853 .vendor
= 0x102f, /* Toshiba */
1854 .device
= 0x0107, /* this UDC */
1855 .subvendor
= PCI_ANY_ID
,
1856 .subdevice
= PCI_ANY_ID
,
1858 }, { /* end: all zeroes */ }
1860 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1862 static struct pci_driver goku_pci_driver
= {
1863 .name
= (char *) driver_name
,
1864 .id_table
= pci_ids
,
1866 .probe
= goku_probe
,
1867 .remove
= goku_remove
,
1869 /* FIXME add power management support */
1872 module_pci_driver(goku_pci_driver
);