2 * drivers/usb/gadget/s3c_udc_otg_xfer_dma.c
3 * Samsung S3C on-chip full/high speed USB OTG 2.0 device controllers
5 * Copyright (C) 2009 for Samsung Electronics
7 * BSP Support for Samsung's UDC driver
9 * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
11 * State machine bugfixes:
12 * Marek Szyprowski <m.szyprowski@samsung.com>
15 * Marek Szyprowski <m.szyprowski@samsung.com>
16 * Lukasz Majewski <l.majewski@samsumg.com>
18 * SPDX-License-Identifier: GPL-2.0+
21 static u8 clear_feature_num;
22 int clear_feature_flag;
24 /* Bulk-Only Mass Storage Reset (class-specific request) */
25 #define GET_MAX_LUN_REQUEST 0xFE
26 #define BOT_RESET_REQUEST 0xFF
28 static inline void s3c_udc_ep0_zlp(struct s3c_udc *dev)
32 writel(usb_ctrl_dma_addr, ®->in_endp[EP0_CON].diepdma);
33 writel(DIEPT_SIZ_PKT_CNT(1), ®->in_endp[EP0_CON].dieptsiz);
35 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
36 writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
37 ®->in_endp[EP0_CON].diepctl);
39 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
40 __func__, readl(®->in_endp[EP0_CON].diepctl));
41 dev->ep0state = WAIT_FOR_IN_COMPLETE;
44 void s3c_udc_pre_setup(void)
48 debug_cond(DEBUG_IN_EP,
49 "%s : Prepare Setup packets.\n", __func__);
51 writel(DOEPT_SIZ_PKT_CNT(1) | sizeof(struct usb_ctrlrequest),
52 ®->out_endp[EP0_CON].doeptsiz);
53 writel(usb_ctrl_dma_addr, ®->out_endp[EP0_CON].doepdma);
55 ep_ctrl = readl(®->out_endp[EP0_CON].doepctl);
56 writel(ep_ctrl|DEPCTL_EPENA, ®->out_endp[EP0_CON].doepctl);
58 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
59 __func__, readl(®->in_endp[EP0_CON].diepctl));
60 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
61 __func__, readl(®->out_endp[EP0_CON].doepctl));
65 static inline void s3c_ep0_complete_out(void)
69 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
70 __func__, readl(®->in_endp[EP0_CON].diepctl));
71 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
72 __func__, readl(®->out_endp[EP0_CON].doepctl));
74 debug_cond(DEBUG_IN_EP,
75 "%s : Prepare Complete Out packet.\n", __func__);
77 writel(DOEPT_SIZ_PKT_CNT(1) | sizeof(struct usb_ctrlrequest),
78 ®->out_endp[EP0_CON].doeptsiz);
79 writel(usb_ctrl_dma_addr, ®->out_endp[EP0_CON].doepdma);
81 ep_ctrl = readl(®->out_endp[EP0_CON].doepctl);
82 writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
83 ®->out_endp[EP0_CON].doepctl);
85 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
86 __func__, readl(®->in_endp[EP0_CON].diepctl));
87 debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
88 __func__, readl(®->out_endp[EP0_CON].doepctl));
93 static int setdma_rx(struct s3c_ep *ep, struct s3c_request *req)
97 u32 ep_num = ep_index(ep);
99 buf = req->req.buf + req->req.actual;
100 length = min(req->req.length - req->req.actual,
101 ep_num ? DMA_BUFFER_SIZE : ep->ep.maxpacket);
106 if (ep_num == EP0_CON || length == 0)
109 pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
111 ctrl = readl(®->out_endp[ep_num].doepctl);
113 writel(the_controller->dma_addr[ep_index(ep)+1],
114 ®->out_endp[ep_num].doepdma);
115 writel(DOEPT_SIZ_PKT_CNT(pktcnt) | DOEPT_SIZ_XFER_SIZE(length),
116 ®->out_endp[ep_num].doeptsiz);
117 writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, ®->out_endp[ep_num].doepctl);
119 debug_cond(DEBUG_OUT_EP != 0,
120 "%s: EP%d RX DMA start : DOEPDMA = 0x%x,"
121 "DOEPTSIZ = 0x%x, DOEPCTL = 0x%x\n"
122 "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
124 readl(®->out_endp[ep_num].doepdma),
125 readl(®->out_endp[ep_num].doeptsiz),
126 readl(®->out_endp[ep_num].doepctl),
127 buf, pktcnt, length);
132 int setdma_tx(struct s3c_ep *ep, struct s3c_request *req)
136 u32 ep_num = ep_index(ep);
137 u32 *p = the_controller->dma_buf[ep_index(ep)+1];
139 buf = req->req.buf + req->req.actual;
140 length = req->req.length - req->req.actual;
142 if (ep_num == EP0_CON)
143 length = min(length, (u32)ep_maxpacket(ep));
147 memcpy(p, ep->dma_buf, length);
149 flush_dcache_range((unsigned long) p ,
150 (unsigned long) p + DMA_BUFFER_SIZE);
155 pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
157 /* Flush the endpoint's Tx FIFO */
158 writel(TX_FIFO_NUMBER(ep->fifo_num), ®->grstctl);
159 writel(TX_FIFO_NUMBER(ep->fifo_num) | TX_FIFO_FLUSH, ®->grstctl);
160 while (readl(®->grstctl) & TX_FIFO_FLUSH)
163 writel(the_controller->dma_addr[ep_index(ep)+1],
164 ®->in_endp[ep_num].diepdma);
165 writel(DIEPT_SIZ_PKT_CNT(pktcnt) | DIEPT_SIZ_XFER_SIZE(length),
166 ®->in_endp[ep_num].dieptsiz);
168 ctrl = readl(®->in_endp[ep_num].diepctl);
170 /* Write the FIFO number to be used for this endpoint */
171 ctrl &= DIEPCTL_TX_FIFO_NUM_MASK;
172 ctrl |= DIEPCTL_TX_FIFO_NUM(ep->fifo_num);
174 /* Clear reserved (Next EP) bits */
175 ctrl = (ctrl&~(EP_MASK<<DEPCTL_NEXT_EP_BIT));
177 writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, ®->in_endp[ep_num].diepctl);
179 debug_cond(DEBUG_IN_EP,
180 "%s:EP%d TX DMA start : DIEPDMA0 = 0x%x,"
181 "DIEPTSIZ0 = 0x%x, DIEPCTL0 = 0x%x\n"
182 "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
184 readl(®->in_endp[ep_num].diepdma),
185 readl(®->in_endp[ep_num].dieptsiz),
186 readl(®->in_endp[ep_num].diepctl),
187 buf, pktcnt, length);
192 static void complete_rx(struct s3c_udc *dev, u8 ep_num)
194 struct s3c_ep *ep = &dev->ep[ep_num];
195 struct s3c_request *req = NULL;
196 u32 ep_tsr = 0, xfer_size = 0, is_short = 0;
197 u32 *p = the_controller->dma_buf[ep_index(ep)+1];
199 if (list_empty(&ep->queue)) {
200 debug_cond(DEBUG_OUT_EP != 0,
201 "%s: RX DMA done : NULL REQ on OUT EP-%d\n",
207 req = list_entry(ep->queue.next, struct s3c_request, queue);
208 ep_tsr = readl(®->out_endp[ep_num].doeptsiz);
210 if (ep_num == EP0_CON)
211 xfer_size = (ep_tsr & DOEPT_SIZ_XFER_SIZE_MAX_EP0);
213 xfer_size = (ep_tsr & DOEPT_SIZ_XFER_SIZE_MAX_EP);
215 xfer_size = ep->len - xfer_size;
217 invalidate_dcache_range((unsigned long) p,
218 (unsigned long) p + DMA_BUFFER_SIZE);
220 memcpy(ep->dma_buf, p, ep->len);
222 req->req.actual += min(xfer_size, req->req.length - req->req.actual);
223 is_short = (xfer_size < ep->ep.maxpacket);
225 debug_cond(DEBUG_OUT_EP != 0,
226 "%s: RX DMA done : ep = %d, rx bytes = %d/%d, "
227 "is_short = %d, DOEPTSIZ = 0x%x, remained bytes = %d\n",
228 __func__, ep_num, req->req.actual, req->req.length,
229 is_short, ep_tsr, xfer_size);
231 if (is_short || req->req.actual == req->req.length) {
232 if (ep_num == EP0_CON && dev->ep0state == DATA_STATE_RECV) {
233 debug_cond(DEBUG_OUT_EP != 0, " => Send ZLP\n");
234 s3c_udc_ep0_zlp(dev);
235 /* packet will be completed in complete_tx() */
236 dev->ep0state = WAIT_FOR_IN_COMPLETE;
240 if (!list_empty(&ep->queue)) {
241 req = list_entry(ep->queue.next,
242 struct s3c_request, queue);
243 debug_cond(DEBUG_OUT_EP != 0,
244 "%s: Next Rx request start...\n",
253 static void complete_tx(struct s3c_udc *dev, u8 ep_num)
255 struct s3c_ep *ep = &dev->ep[ep_num];
256 struct s3c_request *req;
257 u32 ep_tsr = 0, xfer_size = 0, is_short = 0;
260 if (dev->ep0state == WAIT_FOR_NULL_COMPLETE) {
261 dev->ep0state = WAIT_FOR_OUT_COMPLETE;
262 s3c_ep0_complete_out();
266 if (list_empty(&ep->queue)) {
267 debug_cond(DEBUG_IN_EP,
268 "%s: TX DMA done : NULL REQ on IN EP-%d\n",
274 req = list_entry(ep->queue.next, struct s3c_request, queue);
276 ep_tsr = readl(®->in_endp[ep_num].dieptsiz);
279 is_short = (xfer_size < ep->ep.maxpacket);
280 req->req.actual += min(xfer_size, req->req.length - req->req.actual);
282 debug_cond(DEBUG_IN_EP,
283 "%s: TX DMA done : ep = %d, tx bytes = %d/%d, "
284 "is_short = %d, DIEPTSIZ = 0x%x, remained bytes = %d\n",
285 __func__, ep_num, req->req.actual, req->req.length,
286 is_short, ep_tsr, xfer_size);
289 if (dev->ep0state == DATA_STATE_XMIT) {
290 debug_cond(DEBUG_IN_EP,
291 "%s: ep_num = %d, ep0stat =="
294 last = write_fifo_ep0(ep, req);
296 dev->ep0state = WAIT_FOR_COMPLETE;
297 } else if (dev->ep0state == WAIT_FOR_IN_COMPLETE) {
298 debug_cond(DEBUG_IN_EP,
299 "%s: ep_num = %d, completing request\n",
302 dev->ep0state = WAIT_FOR_SETUP;
303 } else if (dev->ep0state == WAIT_FOR_COMPLETE) {
304 debug_cond(DEBUG_IN_EP,
305 "%s: ep_num = %d, completing request\n",
308 dev->ep0state = WAIT_FOR_OUT_COMPLETE;
309 s3c_ep0_complete_out();
311 debug_cond(DEBUG_IN_EP,
312 "%s: ep_num = %d, invalid ep state\n",
318 if (req->req.actual == req->req.length)
321 if (!list_empty(&ep->queue)) {
322 req = list_entry(ep->queue.next, struct s3c_request, queue);
323 debug_cond(DEBUG_IN_EP,
324 "%s: Next Tx request start...\n", __func__);
329 static inline void s3c_udc_check_tx_queue(struct s3c_udc *dev, u8 ep_num)
331 struct s3c_ep *ep = &dev->ep[ep_num];
332 struct s3c_request *req;
334 debug_cond(DEBUG_IN_EP,
335 "%s: Check queue, ep_num = %d\n", __func__, ep_num);
337 if (!list_empty(&ep->queue)) {
338 req = list_entry(ep->queue.next, struct s3c_request, queue);
339 debug_cond(DEBUG_IN_EP,
340 "%s: Next Tx request(0x%p) start...\n",
348 debug_cond(DEBUG_IN_EP,
349 "%s: NULL REQ on IN EP-%d\n", __func__, ep_num);
356 static void process_ep_in_intr(struct s3c_udc *dev)
358 u32 ep_intr, ep_intr_status;
361 ep_intr = readl(®->daint);
362 debug_cond(DEBUG_IN_EP,
363 "*** %s: EP In interrupt : DAINT = 0x%x\n", __func__, ep_intr);
365 ep_intr &= DAINT_MASK;
368 if (ep_intr & DAINT_IN_EP_INT(1)) {
369 ep_intr_status = readl(®->in_endp[ep_num].diepint);
370 debug_cond(DEBUG_IN_EP,
371 "\tEP%d-IN : DIEPINT = 0x%x\n",
372 ep_num, ep_intr_status);
374 /* Interrupt Clear */
375 writel(ep_intr_status, ®->in_endp[ep_num].diepint);
377 if (ep_intr_status & TRANSFER_DONE) {
378 complete_tx(dev, ep_num);
382 WAIT_FOR_IN_COMPLETE)
383 dev->ep0state = WAIT_FOR_SETUP;
385 if (dev->ep0state == WAIT_FOR_SETUP)
388 /* continue transfer after
389 set_clear_halt for DMA mode */
390 if (clear_feature_flag == 1) {
391 s3c_udc_check_tx_queue(dev,
393 clear_feature_flag = 0;
403 static void process_ep_out_intr(struct s3c_udc *dev)
405 u32 ep_intr, ep_intr_status;
408 ep_intr = readl(®->daint);
409 debug_cond(DEBUG_OUT_EP != 0,
410 "*** %s: EP OUT interrupt : DAINT = 0x%x\n",
413 ep_intr = (ep_intr >> DAINT_OUT_BIT) & DAINT_MASK;
417 ep_intr_status = readl(®->out_endp[ep_num].doepint);
418 debug_cond(DEBUG_OUT_EP != 0,
419 "\tEP%d-OUT : DOEPINT = 0x%x\n",
420 ep_num, ep_intr_status);
422 /* Interrupt Clear */
423 writel(ep_intr_status, ®->out_endp[ep_num].doepint);
426 if (ep_intr_status & TRANSFER_DONE) {
428 WAIT_FOR_OUT_COMPLETE)
429 complete_rx(dev, ep_num);
431 dev->ep0state = WAIT_FOR_SETUP;
437 CTRL_OUT_EP_SETUP_PHASE_DONE) {
438 debug_cond(DEBUG_OUT_EP != 0,
439 "SETUP packet arrived\n");
443 if (ep_intr_status & TRANSFER_DONE)
444 complete_rx(dev, ep_num);
453 * usb client interrupt handler.
455 static int s3c_udc_irq(int irq, void *_dev)
457 struct s3c_udc *dev = _dev;
459 u32 usb_status, gintmsk;
462 spin_lock_irqsave(&dev->lock, flags);
464 intr_status = readl(®->gintsts);
465 gintmsk = readl(®->gintmsk);
467 debug_cond(DEBUG_ISR,
468 "\n*** %s : GINTSTS=0x%x(on state %s), GINTMSK : 0x%x,"
469 "DAINT : 0x%x, DAINTMSK : 0x%x\n",
470 __func__, intr_status, state_names[dev->ep0state], gintmsk,
471 readl(®->daint), readl(®->daintmsk));
474 spin_unlock_irqrestore(&dev->lock, flags);
478 if (intr_status & INT_ENUMDONE) {
479 debug_cond(DEBUG_ISR, "\tSpeed Detection interrupt\n");
481 writel(INT_ENUMDONE, ®->gintsts);
482 usb_status = (readl(®->dsts) & 0x6);
484 if (usb_status & (USB_FULL_30_60MHZ | USB_FULL_48MHZ)) {
485 debug_cond(DEBUG_ISR,
486 "\t\tFull Speed Detection\n");
487 set_max_pktsize(dev, USB_SPEED_FULL);
490 debug_cond(DEBUG_ISR,
491 "\t\tHigh Speed Detection : 0x%x\n",
493 set_max_pktsize(dev, USB_SPEED_HIGH);
497 if (intr_status & INT_EARLY_SUSPEND) {
498 debug_cond(DEBUG_ISR, "\tEarly suspend interrupt\n");
499 writel(INT_EARLY_SUSPEND, ®->gintsts);
502 if (intr_status & INT_SUSPEND) {
503 usb_status = readl(®->dsts);
504 debug_cond(DEBUG_ISR,
505 "\tSuspend interrupt :(DSTS):0x%x\n", usb_status);
506 writel(INT_SUSPEND, ®->gintsts);
508 if (dev->gadget.speed != USB_SPEED_UNKNOWN
510 if (dev->driver->suspend)
511 dev->driver->suspend(&dev->gadget);
513 /* HACK to let gadget detect disconnected state */
514 if (dev->driver->disconnect) {
515 spin_unlock_irqrestore(&dev->lock, flags);
516 dev->driver->disconnect(&dev->gadget);
517 spin_lock_irqsave(&dev->lock, flags);
522 if (intr_status & INT_RESUME) {
523 debug_cond(DEBUG_ISR, "\tResume interrupt\n");
524 writel(INT_RESUME, ®->gintsts);
526 if (dev->gadget.speed != USB_SPEED_UNKNOWN
528 && dev->driver->resume) {
530 dev->driver->resume(&dev->gadget);
534 if (intr_status & INT_RESET) {
535 usb_status = readl(®->gotgctl);
536 debug_cond(DEBUG_ISR,
537 "\tReset interrupt - (GOTGCTL):0x%x\n", usb_status);
538 writel(INT_RESET, ®->gintsts);
540 if ((usb_status & 0xc0000) == (0x3 << 18)) {
541 if (reset_available) {
542 debug_cond(DEBUG_ISR,
543 "\t\tOTG core got reset (%d)!!\n",
546 dev->ep0state = WAIT_FOR_SETUP;
554 debug_cond(DEBUG_ISR,
555 "\t\tRESET handling skipped\n");
559 if (intr_status & INT_IN_EP)
560 process_ep_in_intr(dev);
562 if (intr_status & INT_OUT_EP)
563 process_ep_out_intr(dev);
565 spin_unlock_irqrestore(&dev->lock, flags);
570 /** Queue one request
571 * Kickstart transfer if needed
573 static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
576 struct s3c_request *req;
582 req = container_of(_req, struct s3c_request, req);
583 if (unlikely(!_req || !_req->complete || !_req->buf
584 || !list_empty(&req->queue))) {
586 debug("%s: bad params\n", __func__);
590 ep = container_of(_ep, struct s3c_ep, ep);
592 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
594 debug("%s: bad ep: %s, %d, %p\n", __func__,
595 ep->ep.name, !ep->desc, _ep);
599 ep_num = ep_index(ep);
601 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
603 debug("%s: bogus device state %p\n", __func__, dev->driver);
607 spin_lock_irqsave(&dev->lock, flags);
609 _req->status = -EINPROGRESS;
612 /* kickstart this i/o queue? */
613 debug("\n*** %s: %s-%s req = %p, len = %d, buf = %p"
614 "Q empty = %d, stopped = %d\n",
615 __func__, _ep->name, ep_is_in(ep) ? "in" : "out",
616 _req, _req->length, _req->buf,
617 list_empty(&ep->queue), ep->stopped);
621 int i, len = _req->length;
626 for (i = 0; i < len; i++) {
627 printf("%02x", ((u8 *)_req->buf)[i]);
635 if (list_empty(&ep->queue) && !ep->stopped) {
639 list_add_tail(&req->queue, &ep->queue);
640 s3c_ep0_kick(dev, ep);
643 } else if (ep_is_in(ep)) {
644 gintsts = readl(®->gintsts);
645 debug_cond(DEBUG_IN_EP,
646 "%s: ep_is_in, S3C_UDC_OTG_GINTSTS=0x%x\n",
651 gintsts = readl(®->gintsts);
652 debug_cond(DEBUG_OUT_EP != 0,
653 "%s:ep_is_out, S3C_UDC_OTG_GINTSTS=0x%x\n",
660 /* pio or dma irq handler advances the queue. */
661 if (likely(req != 0))
662 list_add_tail(&req->queue, &ep->queue);
664 spin_unlock_irqrestore(&dev->lock, flags);
669 /****************************************************************/
670 /* End Point 0 related functions */
671 /****************************************************************/
673 /* return: 0 = still running, 1 = completed, negative = errno */
674 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req)
680 max = ep_maxpacket(ep);
682 debug_cond(DEBUG_EP0 != 0, "%s: max = %d\n", __func__, max);
684 count = setdma_tx(ep, req);
686 /* last packet is usually short (or a zlp) */
687 if (likely(count != max))
690 if (likely(req->req.length != req->req.actual + count)
697 debug_cond(DEBUG_EP0 != 0,
698 "%s: wrote %s %d bytes%s %d left %p\n", __func__,
701 req->req.length - req->req.actual - count, req);
703 /* requests complete when all IN data is in the FIFO */
705 ep->dev->ep0state = WAIT_FOR_SETUP;
712 int s3c_fifo_read(struct s3c_ep *ep, u32 *cp, int max)
716 bytes = sizeof(struct usb_ctrlrequest);
718 invalidate_dcache_range((unsigned long) ep->dev->dma_buf[ep_index(ep)],
719 (unsigned long) ep->dev->dma_buf[ep_index(ep)]
722 debug_cond(DEBUG_EP0 != 0,
723 "%s: bytes=%d, ep_index=%d %p\n", __func__,
724 bytes, ep_index(ep), ep->dev->dma_buf[ep_index(ep)]);
730 * udc_set_address - set the USB address for this device
733 * Called from control endpoint function
734 * after it decodes a set address setup packet.
736 static void udc_set_address(struct s3c_udc *dev, unsigned char address)
738 u32 ctrl = readl(®->dcfg);
739 writel(DEVICE_ADDRESS(address) | ctrl, ®->dcfg);
741 s3c_udc_ep0_zlp(dev);
743 debug_cond(DEBUG_EP0 != 0,
744 "%s: USB OTG 2.0 Device address=%d, DCFG=0x%x\n",
745 __func__, address, readl(®->dcfg));
747 dev->usb_address = address;
750 static inline void s3c_udc_ep0_set_stall(struct s3c_ep *ep)
756 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
758 /* set the disable and stall bits */
759 if (ep_ctrl & DEPCTL_EPENA)
760 ep_ctrl |= DEPCTL_EPDIS;
762 ep_ctrl |= DEPCTL_STALL;
764 writel(ep_ctrl, ®->in_endp[EP0_CON].diepctl);
766 debug_cond(DEBUG_EP0 != 0,
767 "%s: set ep%d stall, DIEPCTL0 = 0x%p\n",
768 __func__, ep_index(ep), ®->in_endp[EP0_CON].diepctl);
770 * The application can only set this bit, and the core clears it,
771 * when a SETUP token is received for this endpoint
773 dev->ep0state = WAIT_FOR_SETUP;
778 static void s3c_ep0_read(struct s3c_udc *dev)
780 struct s3c_request *req;
781 struct s3c_ep *ep = &dev->ep[0];
783 if (!list_empty(&ep->queue)) {
784 req = list_entry(ep->queue.next, struct s3c_request, queue);
787 debug("%s: ---> BUG\n", __func__);
792 debug_cond(DEBUG_EP0 != 0,
793 "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
794 __func__, req, req->req.length, req->req.actual);
796 if (req->req.length == 0) {
797 /* zlp for Set_configuration, Set_interface,
798 * or Bulk-Only mass storge reset */
801 s3c_udc_ep0_zlp(dev);
803 debug_cond(DEBUG_EP0 != 0,
804 "%s: req.length = 0, bRequest = %d\n",
805 __func__, usb_ctrl->bRequest);
815 static int s3c_ep0_write(struct s3c_udc *dev)
817 struct s3c_request *req;
818 struct s3c_ep *ep = &dev->ep[0];
819 int ret, need_zlp = 0;
821 if (list_empty(&ep->queue))
824 req = list_entry(ep->queue.next, struct s3c_request, queue);
827 debug_cond(DEBUG_EP0 != 0, "%s: NULL REQ\n", __func__);
831 debug_cond(DEBUG_EP0 != 0,
832 "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
833 __func__, req, req->req.length, req->req.actual);
835 if (req->req.length - req->req.actual == ep0_fifo_size) {
836 /* Next write will end with the packet size, */
837 /* so we need Zero-length-packet */
841 ret = write_fifo_ep0(ep, req);
843 if ((ret == 1) && !need_zlp) {
845 dev->ep0state = WAIT_FOR_COMPLETE;
846 debug_cond(DEBUG_EP0 != 0,
847 "%s: finished, waiting for status\n", __func__);
850 dev->ep0state = DATA_STATE_XMIT;
851 debug_cond(DEBUG_EP0 != 0,
852 "%s: not finished\n", __func__);
860 int s3c_udc_get_status(struct s3c_udc *dev,
861 struct usb_ctrlrequest *crq)
863 u8 ep_num = crq->wIndex & 0x7F;
865 u32 *p = the_controller->dma_buf[1];
867 debug_cond(DEBUG_SETUP != 0,
868 "%s: *** USB_REQ_GET_STATUS\n", __func__);
869 printf("crq->brequest:0x%x\n", crq->bRequestType & USB_RECIP_MASK);
870 switch (crq->bRequestType & USB_RECIP_MASK) {
871 case USB_RECIP_INTERFACE:
873 debug_cond(DEBUG_SETUP != 0,
874 "\tGET_STATUS:USB_RECIP_INTERFACE, g_stauts = %d\n",
878 case USB_RECIP_DEVICE:
879 g_status = 0x1; /* Self powered */
880 debug_cond(DEBUG_SETUP != 0,
881 "\tGET_STATUS: USB_RECIP_DEVICE, g_stauts = %d\n",
885 case USB_RECIP_ENDPOINT:
886 if (crq->wLength > 2) {
887 debug_cond(DEBUG_SETUP != 0,
888 "\tGET_STATUS:Not support EP or wLength\n");
892 g_status = dev->ep[ep_num].stopped;
893 debug_cond(DEBUG_SETUP != 0,
894 "\tGET_STATUS: USB_RECIP_ENDPOINT, g_stauts = %d\n",
903 memcpy(p, &g_status, sizeof(g_status));
905 flush_dcache_range((unsigned long) p,
906 (unsigned long) p + DMA_BUFFER_SIZE);
908 writel(the_controller->dma_addr[1], ®->in_endp[EP0_CON].diepdma);
909 writel(DIEPT_SIZ_PKT_CNT(1) | DIEPT_SIZ_XFER_SIZE(2),
910 ®->in_endp[EP0_CON].dieptsiz);
912 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
913 writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
914 ®->in_endp[EP0_CON].diepctl);
915 dev->ep0state = WAIT_FOR_NULL_COMPLETE;
920 static void s3c_udc_set_nak(struct s3c_ep *ep)
925 ep_num = ep_index(ep);
926 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
929 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
930 ep_ctrl |= DEPCTL_SNAK;
931 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
932 debug("%s: set NAK, DIEPCTL%d = 0x%x\n",
933 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
935 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
936 ep_ctrl |= DEPCTL_SNAK;
937 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
938 debug("%s: set NAK, DOEPCTL%d = 0x%x\n",
939 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
946 void s3c_udc_ep_set_stall(struct s3c_ep *ep)
951 ep_num = ep_index(ep);
952 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
955 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
957 /* set the disable and stall bits */
958 if (ep_ctrl & DEPCTL_EPENA)
959 ep_ctrl |= DEPCTL_EPDIS;
961 ep_ctrl |= DEPCTL_STALL;
963 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
964 debug("%s: set stall, DIEPCTL%d = 0x%x\n",
965 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
968 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
970 /* set the stall bit */
971 ep_ctrl |= DEPCTL_STALL;
973 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
974 debug("%s: set stall, DOEPCTL%d = 0x%x\n",
975 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
981 void s3c_udc_ep_clear_stall(struct s3c_ep *ep)
986 ep_num = ep_index(ep);
987 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
990 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
992 /* clear stall bit */
993 ep_ctrl &= ~DEPCTL_STALL;
996 * USB Spec 9.4.5: For endpoints using data toggle, regardless
997 * of whether an endpoint has the Halt feature set, a
998 * ClearFeature(ENDPOINT_HALT) request always results in the
999 * data toggle being reinitialized to DATA0.
1001 if (ep->bmAttributes == USB_ENDPOINT_XFER_INT
1002 || ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
1003 ep_ctrl |= DEPCTL_SETD0PID; /* DATA0 */
1006 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
1007 debug("%s: cleared stall, DIEPCTL%d = 0x%x\n",
1008 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
1011 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
1013 /* clear stall bit */
1014 ep_ctrl &= ~DEPCTL_STALL;
1016 if (ep->bmAttributes == USB_ENDPOINT_XFER_INT
1017 || ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
1018 ep_ctrl |= DEPCTL_SETD0PID; /* DATA0 */
1021 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
1022 debug("%s: cleared stall, DOEPCTL%d = 0x%x\n",
1023 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
1029 static int s3c_udc_set_halt(struct usb_ep *_ep, int value)
1032 struct s3c_udc *dev;
1033 unsigned long flags;
1036 ep = container_of(_ep, struct s3c_ep, ep);
1037 ep_num = ep_index(ep);
1039 if (unlikely(!_ep || !ep->desc || ep_num == EP0_CON ||
1040 ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC)) {
1041 debug("%s: %s bad ep or descriptor\n", __func__, ep->ep.name);
1045 /* Attempt to halt IN ep will fail if any transfer requests
1046 * are still queue */
1047 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1048 debug("%s: %s queue not empty, req = %p\n",
1049 __func__, ep->ep.name,
1050 list_entry(ep->queue.next, struct s3c_request, queue));
1056 debug("%s: ep_num = %d, value = %d\n", __func__, ep_num, value);
1058 spin_lock_irqsave(&dev->lock, flags);
1062 s3c_udc_ep_clear_stall(ep);
1065 dev->ep0state = WAIT_FOR_SETUP;
1068 s3c_udc_ep_set_stall(ep);
1071 spin_unlock_irqrestore(&dev->lock, flags);
1076 void s3c_udc_ep_activate(struct s3c_ep *ep)
1079 u32 ep_ctrl = 0, daintmsk = 0;
1081 ep_num = ep_index(ep);
1083 /* Read DEPCTLn register */
1085 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
1086 daintmsk = 1 << ep_num;
1088 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
1089 daintmsk = (1 << ep_num) << DAINT_OUT_BIT;
1092 debug("%s: EPCTRL%d = 0x%x, ep_is_in = %d\n",
1093 __func__, ep_num, ep_ctrl, ep_is_in(ep));
1095 /* If the EP is already active don't change the EP Control
1097 if (!(ep_ctrl & DEPCTL_USBACTEP)) {
1098 ep_ctrl = (ep_ctrl & ~DEPCTL_TYPE_MASK) |
1099 (ep->bmAttributes << DEPCTL_TYPE_BIT);
1100 ep_ctrl = (ep_ctrl & ~DEPCTL_MPS_MASK) |
1101 (ep->ep.maxpacket << DEPCTL_MPS_BIT);
1102 ep_ctrl |= (DEPCTL_SETD0PID | DEPCTL_USBACTEP | DEPCTL_SNAK);
1105 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
1106 debug("%s: USB Ative EP%d, DIEPCTRL%d = 0x%x\n",
1107 __func__, ep_num, ep_num,
1108 readl(®->in_endp[ep_num].diepctl));
1110 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
1111 debug("%s: USB Ative EP%d, DOEPCTRL%d = 0x%x\n",
1112 __func__, ep_num, ep_num,
1113 readl(®->out_endp[ep_num].doepctl));
1117 /* Unmask EP Interrtupt */
1118 writel(readl(®->daintmsk)|daintmsk, ®->daintmsk);
1119 debug("%s: DAINTMSK = 0x%x\n", __func__, readl(®->daintmsk));
1123 static int s3c_udc_clear_feature(struct usb_ep *_ep)
1125 struct s3c_udc *dev;
1129 ep = container_of(_ep, struct s3c_ep, ep);
1130 ep_num = ep_index(ep);
1133 debug_cond(DEBUG_SETUP != 0,
1134 "%s: ep_num = %d, is_in = %d, clear_feature_flag = %d\n",
1135 __func__, ep_num, ep_is_in(ep), clear_feature_flag);
1137 if (usb_ctrl->wLength != 0) {
1138 debug_cond(DEBUG_SETUP != 0,
1139 "\tCLEAR_FEATURE: wLength is not zero.....\n");
1143 switch (usb_ctrl->bRequestType & USB_RECIP_MASK) {
1144 case USB_RECIP_DEVICE:
1145 switch (usb_ctrl->wValue) {
1146 case USB_DEVICE_REMOTE_WAKEUP:
1147 debug_cond(DEBUG_SETUP != 0,
1148 "\tOFF:USB_DEVICE_REMOTE_WAKEUP\n");
1151 case USB_DEVICE_TEST_MODE:
1152 debug_cond(DEBUG_SETUP != 0,
1153 "\tCLEAR_FEATURE: USB_DEVICE_TEST_MODE\n");
1154 /** @todo Add CLEAR_FEATURE for TEST modes. */
1158 s3c_udc_ep0_zlp(dev);
1161 case USB_RECIP_ENDPOINT:
1162 debug_cond(DEBUG_SETUP != 0,
1163 "\tCLEAR_FEATURE:USB_RECIP_ENDPOINT, wValue = %d\n",
1166 if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
1168 s3c_udc_ep0_set_stall(ep);
1172 s3c_udc_ep0_zlp(dev);
1174 s3c_udc_ep_clear_stall(ep);
1175 s3c_udc_ep_activate(ep);
1178 clear_feature_num = ep_num;
1179 clear_feature_flag = 1;
1187 static int s3c_udc_set_feature(struct usb_ep *_ep)
1189 struct s3c_udc *dev;
1193 ep = container_of(_ep, struct s3c_ep, ep);
1194 ep_num = ep_index(ep);
1197 debug_cond(DEBUG_SETUP != 0,
1198 "%s: *** USB_REQ_SET_FEATURE , ep_num = %d\n",
1201 if (usb_ctrl->wLength != 0) {
1202 debug_cond(DEBUG_SETUP != 0,
1203 "\tSET_FEATURE: wLength is not zero.....\n");
1207 switch (usb_ctrl->bRequestType & USB_RECIP_MASK) {
1208 case USB_RECIP_DEVICE:
1209 switch (usb_ctrl->wValue) {
1210 case USB_DEVICE_REMOTE_WAKEUP:
1211 debug_cond(DEBUG_SETUP != 0,
1212 "\tSET_FEATURE:USB_DEVICE_REMOTE_WAKEUP\n");
1214 case USB_DEVICE_B_HNP_ENABLE:
1215 debug_cond(DEBUG_SETUP != 0,
1216 "\tSET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
1219 case USB_DEVICE_A_HNP_SUPPORT:
1220 /* RH port supports HNP */
1221 debug_cond(DEBUG_SETUP != 0,
1222 "\tSET_FEATURE:USB_DEVICE_A_HNP_SUPPORT\n");
1225 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1226 /* other RH port does */
1227 debug_cond(DEBUG_SETUP != 0,
1228 "\tSET: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
1232 s3c_udc_ep0_zlp(dev);
1235 case USB_RECIP_INTERFACE:
1236 debug_cond(DEBUG_SETUP != 0,
1237 "\tSET_FEATURE: USB_RECIP_INTERFACE\n");
1240 case USB_RECIP_ENDPOINT:
1241 debug_cond(DEBUG_SETUP != 0,
1242 "\tSET_FEATURE: USB_RECIP_ENDPOINT\n");
1243 if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
1245 s3c_udc_ep0_set_stall(ep);
1249 s3c_udc_ep_set_stall(ep);
1252 s3c_udc_ep0_zlp(dev);
1260 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1262 void s3c_ep0_setup(struct s3c_udc *dev)
1264 struct s3c_ep *ep = &dev->ep[0];
1268 /* Nuke all previous transfers */
1271 /* read control req from fifo (8 bytes) */
1272 s3c_fifo_read(ep, (u32 *)usb_ctrl, 8);
1274 debug_cond(DEBUG_SETUP != 0,
1275 "%s: bRequestType = 0x%x(%s), bRequest = 0x%x"
1276 "\twLength = 0x%x, wValue = 0x%x, wIndex= 0x%x\n",
1277 __func__, usb_ctrl->bRequestType,
1278 (usb_ctrl->bRequestType & USB_DIR_IN) ? "IN" : "OUT",
1280 usb_ctrl->wLength, usb_ctrl->wValue, usb_ctrl->wIndex);
1284 int i, len = sizeof(*usb_ctrl);
1285 char *p = (char *)usb_ctrl;
1288 for (i = 0; i < len; i++) {
1289 printf("%02x", ((u8 *)p)[i]);
1297 if (usb_ctrl->bRequest == GET_MAX_LUN_REQUEST &&
1298 usb_ctrl->wLength != 1) {
1299 debug_cond(DEBUG_SETUP != 0,
1300 "\t%s:GET_MAX_LUN_REQUEST:invalid",
1302 debug_cond(DEBUG_SETUP != 0,
1303 "wLength = %d, setup returned\n",
1306 s3c_udc_ep0_set_stall(ep);
1307 dev->ep0state = WAIT_FOR_SETUP;
1310 } else if (usb_ctrl->bRequest == BOT_RESET_REQUEST &&
1311 usb_ctrl->wLength != 0) {
1312 /* Bulk-Only *mass storge reset of class-specific request */
1313 debug_cond(DEBUG_SETUP != 0,
1314 "%s:BOT Rest:invalid wLength =%d, setup returned\n",
1315 __func__, usb_ctrl->wLength);
1317 s3c_udc_ep0_set_stall(ep);
1318 dev->ep0state = WAIT_FOR_SETUP;
1323 /* Set direction of EP0 */
1324 if (likely(usb_ctrl->bRequestType & USB_DIR_IN)) {
1325 ep->bEndpointAddress |= USB_DIR_IN;
1327 ep->bEndpointAddress &= ~USB_DIR_IN;
1329 /* cope with automagic for some standard requests. */
1330 dev->req_std = (usb_ctrl->bRequestType & USB_TYPE_MASK)
1331 == USB_TYPE_STANDARD;
1333 dev->req_pending = 1;
1335 /* Handle some SETUP packets ourselves */
1337 switch (usb_ctrl->bRequest) {
1338 case USB_REQ_SET_ADDRESS:
1339 debug_cond(DEBUG_SETUP != 0,
1340 "%s: *** USB_REQ_SET_ADDRESS (%d)\n",
1341 __func__, usb_ctrl->wValue);
1342 if (usb_ctrl->bRequestType
1343 != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1346 udc_set_address(dev, usb_ctrl->wValue);
1349 case USB_REQ_SET_CONFIGURATION:
1350 debug_cond(DEBUG_SETUP != 0,
1351 "=====================================\n");
1352 debug_cond(DEBUG_SETUP != 0,
1353 "%s: USB_REQ_SET_CONFIGURATION (%d)\n",
1354 __func__, usb_ctrl->wValue);
1356 if (usb_ctrl->bRequestType == USB_RECIP_DEVICE)
1357 reset_available = 1;
1361 case USB_REQ_GET_DESCRIPTOR:
1362 debug_cond(DEBUG_SETUP != 0,
1363 "%s: *** USB_REQ_GET_DESCRIPTOR\n",
1367 case USB_REQ_SET_INTERFACE:
1368 debug_cond(DEBUG_SETUP != 0,
1369 "%s: *** USB_REQ_SET_INTERFACE (%d)\n",
1370 __func__, usb_ctrl->wValue);
1372 if (usb_ctrl->bRequestType == USB_RECIP_INTERFACE)
1373 reset_available = 1;
1377 case USB_REQ_GET_CONFIGURATION:
1378 debug_cond(DEBUG_SETUP != 0,
1379 "%s: *** USB_REQ_GET_CONFIGURATION\n",
1383 case USB_REQ_GET_STATUS:
1384 if (!s3c_udc_get_status(dev, usb_ctrl))
1389 case USB_REQ_CLEAR_FEATURE:
1390 ep_num = usb_ctrl->wIndex & 0x7f;
1392 if (!s3c_udc_clear_feature(&dev->ep[ep_num].ep))
1397 case USB_REQ_SET_FEATURE:
1398 ep_num = usb_ctrl->wIndex & 0x7f;
1400 if (!s3c_udc_set_feature(&dev->ep[ep_num].ep))
1406 debug_cond(DEBUG_SETUP != 0,
1407 "%s: *** Default of usb_ctrl->bRequest=0x%x"
1408 "happened.\n", __func__, usb_ctrl->bRequest);
1414 if (likely(dev->driver)) {
1415 /* device-2-host (IN) or no data setup command,
1416 * process immediately */
1417 debug_cond(DEBUG_SETUP != 0,
1418 "%s:usb_ctrlreq will be passed to fsg_setup()\n",
1421 spin_unlock(&dev->lock);
1422 i = dev->driver->setup(&dev->gadget, usb_ctrl);
1423 spin_lock(&dev->lock);
1426 /* setup processing failed, force stall */
1427 s3c_udc_ep0_set_stall(ep);
1428 dev->ep0state = WAIT_FOR_SETUP;
1430 debug_cond(DEBUG_SETUP != 0,
1431 "\tdev->driver->setup failed (%d),"
1433 i, usb_ctrl->bRequest);
1436 } else if (dev->req_pending) {
1437 dev->req_pending = 0;
1438 debug_cond(DEBUG_SETUP != 0,
1439 "\tdev->req_pending...\n");
1442 debug_cond(DEBUG_SETUP != 0,
1443 "\tep0state = %s\n", state_names[dev->ep0state]);
1449 * handle ep0 interrupt
1451 static void s3c_handle_ep0(struct s3c_udc *dev)
1453 if (dev->ep0state == WAIT_FOR_SETUP) {
1454 debug_cond(DEBUG_OUT_EP != 0,
1455 "%s: WAIT_FOR_SETUP\n", __func__);
1459 debug_cond(DEBUG_OUT_EP != 0,
1460 "%s: strange state!!(state = %s)\n",
1461 __func__, state_names[dev->ep0state]);
1465 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep)
1467 debug_cond(DEBUG_EP0 != 0,
1468 "%s: ep_is_in = %d\n", __func__, ep_is_in(ep));
1470 dev->ep0state = DATA_STATE_XMIT;
1474 dev->ep0state = DATA_STATE_RECV;