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;
101 length = min(req->req.length - req->req.actual, (int)ep->ep.maxpacket);
109 pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
112 ctrl = readl(®->out_endp[ep_num].doepctl);
114 writel(the_controller->dma_addr[ep_index(ep)+1],
115 ®->out_endp[ep_num].doepdma);
116 writel(DOEPT_SIZ_PKT_CNT(pktcnt) | DOEPT_SIZ_XFER_SIZE(length),
117 ®->out_endp[ep_num].doeptsiz);
118 writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, ®->out_endp[ep_num].doepctl);
120 debug_cond(DEBUG_OUT_EP != 0,
121 "%s: EP%d RX DMA start : DOEPDMA = 0x%x,"
122 "DOEPTSIZ = 0x%x, DOEPCTL = 0x%x\n"
123 "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
125 readl(®->out_endp[ep_num].doepdma),
126 readl(®->out_endp[ep_num].doeptsiz),
127 readl(®->out_endp[ep_num].doepctl),
128 buf, pktcnt, length);
133 int setdma_tx(struct s3c_ep *ep, struct s3c_request *req)
137 u32 ep_num = ep_index(ep);
138 u32 *p = the_controller->dma_buf[ep_index(ep)+1];
140 buf = req->req.buf + req->req.actual;
141 length = req->req.length - req->req.actual;
143 if (ep_num == EP0_CON)
144 length = min(length, (u32)ep_maxpacket(ep));
148 memcpy(p, ep->dma_buf, length);
150 flush_dcache_range((unsigned long) p ,
151 (unsigned long) p + DMA_BUFFER_SIZE);
156 pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
158 /* Flush the endpoint's Tx FIFO */
159 writel(TX_FIFO_NUMBER(ep->fifo_num), ®->grstctl);
160 writel(TX_FIFO_NUMBER(ep->fifo_num) | TX_FIFO_FLUSH, ®->grstctl);
161 while (readl(®->grstctl) & TX_FIFO_FLUSH)
164 writel(the_controller->dma_addr[ep_index(ep)+1],
165 ®->in_endp[ep_num].diepdma);
166 writel(DIEPT_SIZ_PKT_CNT(pktcnt) | DIEPT_SIZ_XFER_SIZE(length),
167 ®->in_endp[ep_num].dieptsiz);
169 ctrl = readl(®->in_endp[ep_num].diepctl);
171 /* Write the FIFO number to be used for this endpoint */
172 ctrl &= DIEPCTL_TX_FIFO_NUM_MASK;
173 ctrl |= DIEPCTL_TX_FIFO_NUM(ep->fifo_num);
175 /* Clear reserved (Next EP) bits */
176 ctrl = (ctrl&~(EP_MASK<<DEPCTL_NEXT_EP_BIT));
178 writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, ®->in_endp[ep_num].diepctl);
180 debug_cond(DEBUG_IN_EP,
181 "%s:EP%d TX DMA start : DIEPDMA0 = 0x%x,"
182 "DIEPTSIZ0 = 0x%x, DIEPCTL0 = 0x%x\n"
183 "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
185 readl(®->in_endp[ep_num].diepdma),
186 readl(®->in_endp[ep_num].dieptsiz),
187 readl(®->in_endp[ep_num].diepctl),
188 buf, pktcnt, length);
193 static void complete_rx(struct s3c_udc *dev, u8 ep_num)
195 struct s3c_ep *ep = &dev->ep[ep_num];
196 struct s3c_request *req = NULL;
197 u32 ep_tsr = 0, xfer_size = 0, is_short = 0;
198 u32 *p = the_controller->dma_buf[ep_index(ep)+1];
200 if (list_empty(&ep->queue)) {
201 debug_cond(DEBUG_OUT_EP != 0,
202 "%s: RX DMA done : NULL REQ on OUT EP-%d\n",
208 req = list_entry(ep->queue.next, struct s3c_request, queue);
209 ep_tsr = readl(®->out_endp[ep_num].doeptsiz);
211 if (ep_num == EP0_CON)
212 xfer_size = (ep_tsr & DOEPT_SIZ_XFER_SIZE_MAX_EP0);
214 xfer_size = (ep_tsr & DOEPT_SIZ_XFER_SIZE_MAX_EP);
216 xfer_size = ep->len - xfer_size;
218 invalidate_dcache_range((unsigned long) p,
219 (unsigned long) p + DMA_BUFFER_SIZE);
221 memcpy(ep->dma_buf, p, ep->len);
223 req->req.actual += min(xfer_size, req->req.length - req->req.actual);
224 is_short = (xfer_size < ep->ep.maxpacket);
226 debug_cond(DEBUG_OUT_EP != 0,
227 "%s: RX DMA done : ep = %d, rx bytes = %d/%d, "
228 "is_short = %d, DOEPTSIZ = 0x%x, remained bytes = %d\n",
229 __func__, ep_num, req->req.actual, req->req.length,
230 is_short, ep_tsr, xfer_size);
232 if (is_short || req->req.actual == req->req.length) {
233 if (ep_num == EP0_CON && dev->ep0state == DATA_STATE_RECV) {
234 debug_cond(DEBUG_OUT_EP != 0, " => Send ZLP\n");
235 s3c_udc_ep0_zlp(dev);
236 /* packet will be completed in complete_tx() */
237 dev->ep0state = WAIT_FOR_IN_COMPLETE;
241 if (!list_empty(&ep->queue)) {
242 req = list_entry(ep->queue.next,
243 struct s3c_request, queue);
244 debug_cond(DEBUG_OUT_EP != 0,
245 "%s: Next Rx request start...\n",
254 static void complete_tx(struct s3c_udc *dev, u8 ep_num)
256 struct s3c_ep *ep = &dev->ep[ep_num];
257 struct s3c_request *req;
258 u32 ep_tsr = 0, xfer_size = 0, is_short = 0;
261 if (dev->ep0state == WAIT_FOR_NULL_COMPLETE) {
262 dev->ep0state = WAIT_FOR_OUT_COMPLETE;
263 s3c_ep0_complete_out();
267 if (list_empty(&ep->queue)) {
268 debug_cond(DEBUG_IN_EP,
269 "%s: TX DMA done : NULL REQ on IN EP-%d\n",
275 req = list_entry(ep->queue.next, struct s3c_request, queue);
277 ep_tsr = readl(®->in_endp[ep_num].dieptsiz);
280 is_short = (xfer_size < ep->ep.maxpacket);
281 req->req.actual += min(xfer_size, req->req.length - req->req.actual);
283 debug_cond(DEBUG_IN_EP,
284 "%s: TX DMA done : ep = %d, tx bytes = %d/%d, "
285 "is_short = %d, DIEPTSIZ = 0x%x, remained bytes = %d\n",
286 __func__, ep_num, req->req.actual, req->req.length,
287 is_short, ep_tsr, xfer_size);
290 if (dev->ep0state == DATA_STATE_XMIT) {
291 debug_cond(DEBUG_IN_EP,
292 "%s: ep_num = %d, ep0stat =="
295 last = write_fifo_ep0(ep, req);
297 dev->ep0state = WAIT_FOR_COMPLETE;
298 } else if (dev->ep0state == WAIT_FOR_IN_COMPLETE) {
299 debug_cond(DEBUG_IN_EP,
300 "%s: ep_num = %d, completing request\n",
303 dev->ep0state = WAIT_FOR_SETUP;
304 } else if (dev->ep0state == WAIT_FOR_COMPLETE) {
305 debug_cond(DEBUG_IN_EP,
306 "%s: ep_num = %d, completing request\n",
309 dev->ep0state = WAIT_FOR_OUT_COMPLETE;
310 s3c_ep0_complete_out();
312 debug_cond(DEBUG_IN_EP,
313 "%s: ep_num = %d, invalid ep state\n",
319 if (req->req.actual == req->req.length)
322 if (!list_empty(&ep->queue)) {
323 req = list_entry(ep->queue.next, struct s3c_request, queue);
324 debug_cond(DEBUG_IN_EP,
325 "%s: Next Tx request start...\n", __func__);
330 static inline void s3c_udc_check_tx_queue(struct s3c_udc *dev, u8 ep_num)
332 struct s3c_ep *ep = &dev->ep[ep_num];
333 struct s3c_request *req;
335 debug_cond(DEBUG_IN_EP,
336 "%s: Check queue, ep_num = %d\n", __func__, ep_num);
338 if (!list_empty(&ep->queue)) {
339 req = list_entry(ep->queue.next, struct s3c_request, queue);
340 debug_cond(DEBUG_IN_EP,
341 "%s: Next Tx request(0x%p) start...\n",
349 debug_cond(DEBUG_IN_EP,
350 "%s: NULL REQ on IN EP-%d\n", __func__, ep_num);
357 static void process_ep_in_intr(struct s3c_udc *dev)
359 u32 ep_intr, ep_intr_status;
362 ep_intr = readl(®->daint);
363 debug_cond(DEBUG_IN_EP,
364 "*** %s: EP In interrupt : DAINT = 0x%x\n", __func__, ep_intr);
366 ep_intr &= DAINT_MASK;
369 if (ep_intr & DAINT_IN_EP_INT(1)) {
370 ep_intr_status = readl(®->in_endp[ep_num].diepint);
371 debug_cond(DEBUG_IN_EP,
372 "\tEP%d-IN : DIEPINT = 0x%x\n",
373 ep_num, ep_intr_status);
375 /* Interrupt Clear */
376 writel(ep_intr_status, ®->in_endp[ep_num].diepint);
378 if (ep_intr_status & TRANSFER_DONE) {
379 complete_tx(dev, ep_num);
383 WAIT_FOR_IN_COMPLETE)
384 dev->ep0state = WAIT_FOR_SETUP;
386 if (dev->ep0state == WAIT_FOR_SETUP)
389 /* continue transfer after
390 set_clear_halt for DMA mode */
391 if (clear_feature_flag == 1) {
392 s3c_udc_check_tx_queue(dev,
394 clear_feature_flag = 0;
404 static void process_ep_out_intr(struct s3c_udc *dev)
406 u32 ep_intr, ep_intr_status;
409 ep_intr = readl(®->daint);
410 debug_cond(DEBUG_OUT_EP != 0,
411 "*** %s: EP OUT interrupt : DAINT = 0x%x\n",
414 ep_intr = (ep_intr >> DAINT_OUT_BIT) & DAINT_MASK;
418 ep_intr_status = readl(®->out_endp[ep_num].doepint);
419 debug_cond(DEBUG_OUT_EP != 0,
420 "\tEP%d-OUT : DOEPINT = 0x%x\n",
421 ep_num, ep_intr_status);
423 /* Interrupt Clear */
424 writel(ep_intr_status, ®->out_endp[ep_num].doepint);
427 if (ep_intr_status & TRANSFER_DONE) {
429 WAIT_FOR_OUT_COMPLETE)
430 complete_rx(dev, ep_num);
432 dev->ep0state = WAIT_FOR_SETUP;
438 CTRL_OUT_EP_SETUP_PHASE_DONE) {
439 debug_cond(DEBUG_OUT_EP != 0,
440 "SETUP packet arrived\n");
444 if (ep_intr_status & TRANSFER_DONE)
445 complete_rx(dev, ep_num);
454 * usb client interrupt handler.
456 static int s3c_udc_irq(int irq, void *_dev)
458 struct s3c_udc *dev = _dev;
460 u32 usb_status, gintmsk;
463 spin_lock_irqsave(&dev->lock, flags);
465 intr_status = readl(®->gintsts);
466 gintmsk = readl(®->gintmsk);
468 debug_cond(DEBUG_ISR,
469 "\n*** %s : GINTSTS=0x%x(on state %s), GINTMSK : 0x%x,"
470 "DAINT : 0x%x, DAINTMSK : 0x%x\n",
471 __func__, intr_status, state_names[dev->ep0state], gintmsk,
472 readl(®->daint), readl(®->daintmsk));
475 spin_unlock_irqrestore(&dev->lock, flags);
479 if (intr_status & INT_ENUMDONE) {
480 debug_cond(DEBUG_ISR, "\tSpeed Detection interrupt\n");
482 writel(INT_ENUMDONE, ®->gintsts);
483 usb_status = (readl(®->dsts) & 0x6);
485 if (usb_status & (USB_FULL_30_60MHZ | USB_FULL_48MHZ)) {
486 debug_cond(DEBUG_ISR,
487 "\t\tFull Speed Detection\n");
488 set_max_pktsize(dev, USB_SPEED_FULL);
491 debug_cond(DEBUG_ISR,
492 "\t\tHigh Speed Detection : 0x%x\n",
494 set_max_pktsize(dev, USB_SPEED_HIGH);
498 if (intr_status & INT_EARLY_SUSPEND) {
499 debug_cond(DEBUG_ISR, "\tEarly suspend interrupt\n");
500 writel(INT_EARLY_SUSPEND, ®->gintsts);
503 if (intr_status & INT_SUSPEND) {
504 usb_status = readl(®->dsts);
505 debug_cond(DEBUG_ISR,
506 "\tSuspend interrupt :(DSTS):0x%x\n", usb_status);
507 writel(INT_SUSPEND, ®->gintsts);
509 if (dev->gadget.speed != USB_SPEED_UNKNOWN
511 if (dev->driver->suspend)
512 dev->driver->suspend(&dev->gadget);
514 /* HACK to let gadget detect disconnected state */
515 if (dev->driver->disconnect) {
516 spin_unlock_irqrestore(&dev->lock, flags);
517 dev->driver->disconnect(&dev->gadget);
518 spin_lock_irqsave(&dev->lock, flags);
523 if (intr_status & INT_RESUME) {
524 debug_cond(DEBUG_ISR, "\tResume interrupt\n");
525 writel(INT_RESUME, ®->gintsts);
527 if (dev->gadget.speed != USB_SPEED_UNKNOWN
529 && dev->driver->resume) {
531 dev->driver->resume(&dev->gadget);
535 if (intr_status & INT_RESET) {
536 usb_status = readl(®->gotgctl);
537 debug_cond(DEBUG_ISR,
538 "\tReset interrupt - (GOTGCTL):0x%x\n", usb_status);
539 writel(INT_RESET, ®->gintsts);
541 if ((usb_status & 0xc0000) == (0x3 << 18)) {
542 if (reset_available) {
543 debug_cond(DEBUG_ISR,
544 "\t\tOTG core got reset (%d)!!\n",
547 dev->ep0state = WAIT_FOR_SETUP;
555 debug_cond(DEBUG_ISR,
556 "\t\tRESET handling skipped\n");
560 if (intr_status & INT_IN_EP)
561 process_ep_in_intr(dev);
563 if (intr_status & INT_OUT_EP)
564 process_ep_out_intr(dev);
566 spin_unlock_irqrestore(&dev->lock, flags);
571 /** Queue one request
572 * Kickstart transfer if needed
574 static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
577 struct s3c_request *req;
583 req = container_of(_req, struct s3c_request, req);
584 if (unlikely(!_req || !_req->complete || !_req->buf
585 || !list_empty(&req->queue))) {
587 debug("%s: bad params\n", __func__);
591 ep = container_of(_ep, struct s3c_ep, ep);
593 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
595 debug("%s: bad ep: %s, %d, %p\n", __func__,
596 ep->ep.name, !ep->desc, _ep);
600 ep_num = ep_index(ep);
602 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
604 debug("%s: bogus device state %p\n", __func__, dev->driver);
608 spin_lock_irqsave(&dev->lock, flags);
610 _req->status = -EINPROGRESS;
613 /* kickstart this i/o queue? */
614 debug("\n*** %s: %s-%s req = %p, len = %d, buf = %p"
615 "Q empty = %d, stopped = %d\n",
616 __func__, _ep->name, ep_is_in(ep) ? "in" : "out",
617 _req, _req->length, _req->buf,
618 list_empty(&ep->queue), ep->stopped);
622 int i, len = _req->length;
627 for (i = 0; i < len; i++) {
628 printf("%02x", ((u8 *)_req->buf)[i]);
636 if (list_empty(&ep->queue) && !ep->stopped) {
640 list_add_tail(&req->queue, &ep->queue);
641 s3c_ep0_kick(dev, ep);
644 } else if (ep_is_in(ep)) {
645 gintsts = readl(®->gintsts);
646 debug_cond(DEBUG_IN_EP,
647 "%s: ep_is_in, S3C_UDC_OTG_GINTSTS=0x%x\n",
652 gintsts = readl(®->gintsts);
653 debug_cond(DEBUG_OUT_EP != 0,
654 "%s:ep_is_out, S3C_UDC_OTG_GINTSTS=0x%x\n",
661 /* pio or dma irq handler advances the queue. */
662 if (likely(req != 0))
663 list_add_tail(&req->queue, &ep->queue);
665 spin_unlock_irqrestore(&dev->lock, flags);
670 /****************************************************************/
671 /* End Point 0 related functions */
672 /****************************************************************/
674 /* return: 0 = still running, 1 = completed, negative = errno */
675 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req)
681 max = ep_maxpacket(ep);
683 debug_cond(DEBUG_EP0 != 0, "%s: max = %d\n", __func__, max);
685 count = setdma_tx(ep, req);
687 /* last packet is usually short (or a zlp) */
688 if (likely(count != max))
691 if (likely(req->req.length != req->req.actual + count)
698 debug_cond(DEBUG_EP0 != 0,
699 "%s: wrote %s %d bytes%s %d left %p\n", __func__,
702 req->req.length - req->req.actual - count, req);
704 /* requests complete when all IN data is in the FIFO */
706 ep->dev->ep0state = WAIT_FOR_SETUP;
713 int s3c_fifo_read(struct s3c_ep *ep, u32 *cp, int max)
717 bytes = sizeof(struct usb_ctrlrequest);
719 invalidate_dcache_range((unsigned long) ep->dev->dma_buf[ep_index(ep)],
720 (unsigned long) ep->dev->dma_buf[ep_index(ep)]
723 debug_cond(DEBUG_EP0 != 0,
724 "%s: bytes=%d, ep_index=%d %p\n", __func__,
725 bytes, ep_index(ep), ep->dev->dma_buf[ep_index(ep)]);
731 * udc_set_address - set the USB address for this device
734 * Called from control endpoint function
735 * after it decodes a set address setup packet.
737 static void udc_set_address(struct s3c_udc *dev, unsigned char address)
739 u32 ctrl = readl(®->dcfg);
740 writel(DEVICE_ADDRESS(address) | ctrl, ®->dcfg);
742 s3c_udc_ep0_zlp(dev);
744 debug_cond(DEBUG_EP0 != 0,
745 "%s: USB OTG 2.0 Device address=%d, DCFG=0x%x\n",
746 __func__, address, readl(®->dcfg));
748 dev->usb_address = address;
751 static inline void s3c_udc_ep0_set_stall(struct s3c_ep *ep)
757 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
759 /* set the disable and stall bits */
760 if (ep_ctrl & DEPCTL_EPENA)
761 ep_ctrl |= DEPCTL_EPDIS;
763 ep_ctrl |= DEPCTL_STALL;
765 writel(ep_ctrl, ®->in_endp[EP0_CON].diepctl);
767 debug_cond(DEBUG_EP0 != 0,
768 "%s: set ep%d stall, DIEPCTL0 = 0x%p\n",
769 __func__, ep_index(ep), ®->in_endp[EP0_CON].diepctl);
771 * The application can only set this bit, and the core clears it,
772 * when a SETUP token is received for this endpoint
774 dev->ep0state = WAIT_FOR_SETUP;
779 static void s3c_ep0_read(struct s3c_udc *dev)
781 struct s3c_request *req;
782 struct s3c_ep *ep = &dev->ep[0];
784 if (!list_empty(&ep->queue)) {
785 req = list_entry(ep->queue.next, struct s3c_request, queue);
788 debug("%s: ---> BUG\n", __func__);
793 debug_cond(DEBUG_EP0 != 0,
794 "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
795 __func__, req, req->req.length, req->req.actual);
797 if (req->req.length == 0) {
798 /* zlp for Set_configuration, Set_interface,
799 * or Bulk-Only mass storge reset */
802 s3c_udc_ep0_zlp(dev);
804 debug_cond(DEBUG_EP0 != 0,
805 "%s: req.length = 0, bRequest = %d\n",
806 __func__, usb_ctrl->bRequest);
816 static int s3c_ep0_write(struct s3c_udc *dev)
818 struct s3c_request *req;
819 struct s3c_ep *ep = &dev->ep[0];
820 int ret, need_zlp = 0;
822 if (list_empty(&ep->queue))
825 req = list_entry(ep->queue.next, struct s3c_request, queue);
828 debug_cond(DEBUG_EP0 != 0, "%s: NULL REQ\n", __func__);
832 debug_cond(DEBUG_EP0 != 0,
833 "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
834 __func__, req, req->req.length, req->req.actual);
836 if (req->req.length - req->req.actual == ep0_fifo_size) {
837 /* Next write will end with the packet size, */
838 /* so we need Zero-length-packet */
842 ret = write_fifo_ep0(ep, req);
844 if ((ret == 1) && !need_zlp) {
846 dev->ep0state = WAIT_FOR_COMPLETE;
847 debug_cond(DEBUG_EP0 != 0,
848 "%s: finished, waiting for status\n", __func__);
851 dev->ep0state = DATA_STATE_XMIT;
852 debug_cond(DEBUG_EP0 != 0,
853 "%s: not finished\n", __func__);
861 int s3c_udc_get_status(struct s3c_udc *dev,
862 struct usb_ctrlrequest *crq)
864 u8 ep_num = crq->wIndex & 0x7F;
866 u32 *p = the_controller->dma_buf[1];
868 debug_cond(DEBUG_SETUP != 0,
869 "%s: *** USB_REQ_GET_STATUS\n", __func__);
870 printf("crq->brequest:0x%x\n", crq->bRequestType & USB_RECIP_MASK);
871 switch (crq->bRequestType & USB_RECIP_MASK) {
872 case USB_RECIP_INTERFACE:
874 debug_cond(DEBUG_SETUP != 0,
875 "\tGET_STATUS:USB_RECIP_INTERFACE, g_stauts = %d\n",
879 case USB_RECIP_DEVICE:
880 g_status = 0x1; /* Self powered */
881 debug_cond(DEBUG_SETUP != 0,
882 "\tGET_STATUS: USB_RECIP_DEVICE, g_stauts = %d\n",
886 case USB_RECIP_ENDPOINT:
887 if (crq->wLength > 2) {
888 debug_cond(DEBUG_SETUP != 0,
889 "\tGET_STATUS:Not support EP or wLength\n");
893 g_status = dev->ep[ep_num].stopped;
894 debug_cond(DEBUG_SETUP != 0,
895 "\tGET_STATUS: USB_RECIP_ENDPOINT, g_stauts = %d\n",
904 memcpy(p, &g_status, sizeof(g_status));
906 flush_dcache_range((unsigned long) p,
907 (unsigned long) p + DMA_BUFFER_SIZE);
909 writel(the_controller->dma_addr[1], ®->in_endp[EP0_CON].diepdma);
910 writel(DIEPT_SIZ_PKT_CNT(1) | DIEPT_SIZ_XFER_SIZE(2),
911 ®->in_endp[EP0_CON].dieptsiz);
913 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
914 writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
915 ®->in_endp[EP0_CON].diepctl);
916 dev->ep0state = WAIT_FOR_NULL_COMPLETE;
921 static void s3c_udc_set_nak(struct s3c_ep *ep)
926 ep_num = ep_index(ep);
927 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
930 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
931 ep_ctrl |= DEPCTL_SNAK;
932 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
933 debug("%s: set NAK, DIEPCTL%d = 0x%x\n",
934 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
936 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
937 ep_ctrl |= DEPCTL_SNAK;
938 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
939 debug("%s: set NAK, DOEPCTL%d = 0x%x\n",
940 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
947 void s3c_udc_ep_set_stall(struct s3c_ep *ep)
952 ep_num = ep_index(ep);
953 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
956 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
958 /* set the disable and stall bits */
959 if (ep_ctrl & DEPCTL_EPENA)
960 ep_ctrl |= DEPCTL_EPDIS;
962 ep_ctrl |= DEPCTL_STALL;
964 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
965 debug("%s: set stall, DIEPCTL%d = 0x%x\n",
966 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
969 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
971 /* set the stall bit */
972 ep_ctrl |= DEPCTL_STALL;
974 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
975 debug("%s: set stall, DOEPCTL%d = 0x%x\n",
976 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
982 void s3c_udc_ep_clear_stall(struct s3c_ep *ep)
987 ep_num = ep_index(ep);
988 debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
991 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
993 /* clear stall bit */
994 ep_ctrl &= ~DEPCTL_STALL;
997 * USB Spec 9.4.5: For endpoints using data toggle, regardless
998 * of whether an endpoint has the Halt feature set, a
999 * ClearFeature(ENDPOINT_HALT) request always results in the
1000 * data toggle being reinitialized to DATA0.
1002 if (ep->bmAttributes == USB_ENDPOINT_XFER_INT
1003 || ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
1004 ep_ctrl |= DEPCTL_SETD0PID; /* DATA0 */
1007 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
1008 debug("%s: cleared stall, DIEPCTL%d = 0x%x\n",
1009 __func__, ep_num, readl(®->in_endp[ep_num].diepctl));
1012 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
1014 /* clear stall bit */
1015 ep_ctrl &= ~DEPCTL_STALL;
1017 if (ep->bmAttributes == USB_ENDPOINT_XFER_INT
1018 || ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
1019 ep_ctrl |= DEPCTL_SETD0PID; /* DATA0 */
1022 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
1023 debug("%s: cleared stall, DOEPCTL%d = 0x%x\n",
1024 __func__, ep_num, readl(®->out_endp[ep_num].doepctl));
1030 static int s3c_udc_set_halt(struct usb_ep *_ep, int value)
1033 struct s3c_udc *dev;
1034 unsigned long flags;
1037 ep = container_of(_ep, struct s3c_ep, ep);
1038 ep_num = ep_index(ep);
1040 if (unlikely(!_ep || !ep->desc || ep_num == EP0_CON ||
1041 ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC)) {
1042 debug("%s: %s bad ep or descriptor\n", __func__, ep->ep.name);
1046 /* Attempt to halt IN ep will fail if any transfer requests
1047 * are still queue */
1048 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1049 debug("%s: %s queue not empty, req = %p\n",
1050 __func__, ep->ep.name,
1051 list_entry(ep->queue.next, struct s3c_request, queue));
1057 debug("%s: ep_num = %d, value = %d\n", __func__, ep_num, value);
1059 spin_lock_irqsave(&dev->lock, flags);
1063 s3c_udc_ep_clear_stall(ep);
1066 dev->ep0state = WAIT_FOR_SETUP;
1069 s3c_udc_ep_set_stall(ep);
1072 spin_unlock_irqrestore(&dev->lock, flags);
1077 void s3c_udc_ep_activate(struct s3c_ep *ep)
1080 u32 ep_ctrl = 0, daintmsk = 0;
1082 ep_num = ep_index(ep);
1084 /* Read DEPCTLn register */
1086 ep_ctrl = readl(®->in_endp[ep_num].diepctl);
1087 daintmsk = 1 << ep_num;
1089 ep_ctrl = readl(®->out_endp[ep_num].doepctl);
1090 daintmsk = (1 << ep_num) << DAINT_OUT_BIT;
1093 debug("%s: EPCTRL%d = 0x%x, ep_is_in = %d\n",
1094 __func__, ep_num, ep_ctrl, ep_is_in(ep));
1096 /* If the EP is already active don't change the EP Control
1098 if (!(ep_ctrl & DEPCTL_USBACTEP)) {
1099 ep_ctrl = (ep_ctrl & ~DEPCTL_TYPE_MASK) |
1100 (ep->bmAttributes << DEPCTL_TYPE_BIT);
1101 ep_ctrl = (ep_ctrl & ~DEPCTL_MPS_MASK) |
1102 (ep->ep.maxpacket << DEPCTL_MPS_BIT);
1103 ep_ctrl |= (DEPCTL_SETD0PID | DEPCTL_USBACTEP | DEPCTL_SNAK);
1106 writel(ep_ctrl, ®->in_endp[ep_num].diepctl);
1107 debug("%s: USB Ative EP%d, DIEPCTRL%d = 0x%x\n",
1108 __func__, ep_num, ep_num,
1109 readl(®->in_endp[ep_num].diepctl));
1111 writel(ep_ctrl, ®->out_endp[ep_num].doepctl);
1112 debug("%s: USB Ative EP%d, DOEPCTRL%d = 0x%x\n",
1113 __func__, ep_num, ep_num,
1114 readl(®->out_endp[ep_num].doepctl));
1118 /* Unmask EP Interrtupt */
1119 writel(readl(®->daintmsk)|daintmsk, ®->daintmsk);
1120 debug("%s: DAINTMSK = 0x%x\n", __func__, readl(®->daintmsk));
1124 static int s3c_udc_clear_feature(struct usb_ep *_ep)
1126 struct s3c_udc *dev;
1130 ep = container_of(_ep, struct s3c_ep, ep);
1131 ep_num = ep_index(ep);
1134 debug_cond(DEBUG_SETUP != 0,
1135 "%s: ep_num = %d, is_in = %d, clear_feature_flag = %d\n",
1136 __func__, ep_num, ep_is_in(ep), clear_feature_flag);
1138 if (usb_ctrl->wLength != 0) {
1139 debug_cond(DEBUG_SETUP != 0,
1140 "\tCLEAR_FEATURE: wLength is not zero.....\n");
1144 switch (usb_ctrl->bRequestType & USB_RECIP_MASK) {
1145 case USB_RECIP_DEVICE:
1146 switch (usb_ctrl->wValue) {
1147 case USB_DEVICE_REMOTE_WAKEUP:
1148 debug_cond(DEBUG_SETUP != 0,
1149 "\tOFF:USB_DEVICE_REMOTE_WAKEUP\n");
1152 case USB_DEVICE_TEST_MODE:
1153 debug_cond(DEBUG_SETUP != 0,
1154 "\tCLEAR_FEATURE: USB_DEVICE_TEST_MODE\n");
1155 /** @todo Add CLEAR_FEATURE for TEST modes. */
1159 s3c_udc_ep0_zlp(dev);
1162 case USB_RECIP_ENDPOINT:
1163 debug_cond(DEBUG_SETUP != 0,
1164 "\tCLEAR_FEATURE:USB_RECIP_ENDPOINT, wValue = %d\n",
1167 if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
1169 s3c_udc_ep0_set_stall(ep);
1173 s3c_udc_ep0_zlp(dev);
1175 s3c_udc_ep_clear_stall(ep);
1176 s3c_udc_ep_activate(ep);
1179 clear_feature_num = ep_num;
1180 clear_feature_flag = 1;
1188 static int s3c_udc_set_feature(struct usb_ep *_ep)
1190 struct s3c_udc *dev;
1194 ep = container_of(_ep, struct s3c_ep, ep);
1195 ep_num = ep_index(ep);
1198 debug_cond(DEBUG_SETUP != 0,
1199 "%s: *** USB_REQ_SET_FEATURE , ep_num = %d\n",
1202 if (usb_ctrl->wLength != 0) {
1203 debug_cond(DEBUG_SETUP != 0,
1204 "\tSET_FEATURE: wLength is not zero.....\n");
1208 switch (usb_ctrl->bRequestType & USB_RECIP_MASK) {
1209 case USB_RECIP_DEVICE:
1210 switch (usb_ctrl->wValue) {
1211 case USB_DEVICE_REMOTE_WAKEUP:
1212 debug_cond(DEBUG_SETUP != 0,
1213 "\tSET_FEATURE:USB_DEVICE_REMOTE_WAKEUP\n");
1215 case USB_DEVICE_B_HNP_ENABLE:
1216 debug_cond(DEBUG_SETUP != 0,
1217 "\tSET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
1220 case USB_DEVICE_A_HNP_SUPPORT:
1221 /* RH port supports HNP */
1222 debug_cond(DEBUG_SETUP != 0,
1223 "\tSET_FEATURE:USB_DEVICE_A_HNP_SUPPORT\n");
1226 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1227 /* other RH port does */
1228 debug_cond(DEBUG_SETUP != 0,
1229 "\tSET: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
1233 s3c_udc_ep0_zlp(dev);
1236 case USB_RECIP_INTERFACE:
1237 debug_cond(DEBUG_SETUP != 0,
1238 "\tSET_FEATURE: USB_RECIP_INTERFACE\n");
1241 case USB_RECIP_ENDPOINT:
1242 debug_cond(DEBUG_SETUP != 0,
1243 "\tSET_FEATURE: USB_RECIP_ENDPOINT\n");
1244 if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
1246 s3c_udc_ep0_set_stall(ep);
1250 s3c_udc_ep_set_stall(ep);
1253 s3c_udc_ep0_zlp(dev);
1261 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1263 void s3c_ep0_setup(struct s3c_udc *dev)
1265 struct s3c_ep *ep = &dev->ep[0];
1269 /* Nuke all previous transfers */
1272 /* read control req from fifo (8 bytes) */
1273 s3c_fifo_read(ep, (u32 *)usb_ctrl, 8);
1275 debug_cond(DEBUG_SETUP != 0,
1276 "%s: bRequestType = 0x%x(%s), bRequest = 0x%x"
1277 "\twLength = 0x%x, wValue = 0x%x, wIndex= 0x%x\n",
1278 __func__, usb_ctrl->bRequestType,
1279 (usb_ctrl->bRequestType & USB_DIR_IN) ? "IN" : "OUT",
1281 usb_ctrl->wLength, usb_ctrl->wValue, usb_ctrl->wIndex);
1285 int i, len = sizeof(*usb_ctrl);
1286 char *p = (char *)usb_ctrl;
1289 for (i = 0; i < len; i++) {
1290 printf("%02x", ((u8 *)p)[i]);
1298 if (usb_ctrl->bRequest == GET_MAX_LUN_REQUEST &&
1299 usb_ctrl->wLength != 1) {
1300 debug_cond(DEBUG_SETUP != 0,
1301 "\t%s:GET_MAX_LUN_REQUEST:invalid",
1303 debug_cond(DEBUG_SETUP != 0,
1304 "wLength = %d, setup returned\n",
1307 s3c_udc_ep0_set_stall(ep);
1308 dev->ep0state = WAIT_FOR_SETUP;
1311 } else if (usb_ctrl->bRequest == BOT_RESET_REQUEST &&
1312 usb_ctrl->wLength != 0) {
1313 /* Bulk-Only *mass storge reset of class-specific request */
1314 debug_cond(DEBUG_SETUP != 0,
1315 "%s:BOT Rest:invalid wLength =%d, setup returned\n",
1316 __func__, usb_ctrl->wLength);
1318 s3c_udc_ep0_set_stall(ep);
1319 dev->ep0state = WAIT_FOR_SETUP;
1324 /* Set direction of EP0 */
1325 if (likely(usb_ctrl->bRequestType & USB_DIR_IN)) {
1326 ep->bEndpointAddress |= USB_DIR_IN;
1328 ep->bEndpointAddress &= ~USB_DIR_IN;
1330 /* cope with automagic for some standard requests. */
1331 dev->req_std = (usb_ctrl->bRequestType & USB_TYPE_MASK)
1332 == USB_TYPE_STANDARD;
1334 dev->req_pending = 1;
1336 /* Handle some SETUP packets ourselves */
1338 switch (usb_ctrl->bRequest) {
1339 case USB_REQ_SET_ADDRESS:
1340 debug_cond(DEBUG_SETUP != 0,
1341 "%s: *** USB_REQ_SET_ADDRESS (%d)\n",
1342 __func__, usb_ctrl->wValue);
1343 if (usb_ctrl->bRequestType
1344 != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1347 udc_set_address(dev, usb_ctrl->wValue);
1350 case USB_REQ_SET_CONFIGURATION:
1351 debug_cond(DEBUG_SETUP != 0,
1352 "=====================================\n");
1353 debug_cond(DEBUG_SETUP != 0,
1354 "%s: USB_REQ_SET_CONFIGURATION (%d)\n",
1355 __func__, usb_ctrl->wValue);
1357 if (usb_ctrl->bRequestType == USB_RECIP_DEVICE)
1358 reset_available = 1;
1362 case USB_REQ_GET_DESCRIPTOR:
1363 debug_cond(DEBUG_SETUP != 0,
1364 "%s: *** USB_REQ_GET_DESCRIPTOR\n",
1368 case USB_REQ_SET_INTERFACE:
1369 debug_cond(DEBUG_SETUP != 0,
1370 "%s: *** USB_REQ_SET_INTERFACE (%d)\n",
1371 __func__, usb_ctrl->wValue);
1373 if (usb_ctrl->bRequestType == USB_RECIP_INTERFACE)
1374 reset_available = 1;
1378 case USB_REQ_GET_CONFIGURATION:
1379 debug_cond(DEBUG_SETUP != 0,
1380 "%s: *** USB_REQ_GET_CONFIGURATION\n",
1384 case USB_REQ_GET_STATUS:
1385 if (!s3c_udc_get_status(dev, usb_ctrl))
1390 case USB_REQ_CLEAR_FEATURE:
1391 ep_num = usb_ctrl->wIndex & 0x7f;
1393 if (!s3c_udc_clear_feature(&dev->ep[ep_num].ep))
1398 case USB_REQ_SET_FEATURE:
1399 ep_num = usb_ctrl->wIndex & 0x7f;
1401 if (!s3c_udc_set_feature(&dev->ep[ep_num].ep))
1407 debug_cond(DEBUG_SETUP != 0,
1408 "%s: *** Default of usb_ctrl->bRequest=0x%x"
1409 "happened.\n", __func__, usb_ctrl->bRequest);
1415 if (likely(dev->driver)) {
1416 /* device-2-host (IN) or no data setup command,
1417 * process immediately */
1418 debug_cond(DEBUG_SETUP != 0,
1419 "%s:usb_ctrlreq will be passed to fsg_setup()\n",
1422 spin_unlock(&dev->lock);
1423 i = dev->driver->setup(&dev->gadget, usb_ctrl);
1424 spin_lock(&dev->lock);
1427 /* setup processing failed, force stall */
1428 s3c_udc_ep0_set_stall(ep);
1429 dev->ep0state = WAIT_FOR_SETUP;
1431 debug_cond(DEBUG_SETUP != 0,
1432 "\tdev->driver->setup failed (%d),"
1434 i, usb_ctrl->bRequest);
1437 } else if (dev->req_pending) {
1438 dev->req_pending = 0;
1439 debug_cond(DEBUG_SETUP != 0,
1440 "\tdev->req_pending...\n");
1443 debug_cond(DEBUG_SETUP != 0,
1444 "\tep0state = %s\n", state_names[dev->ep0state]);
1450 * handle ep0 interrupt
1452 static void s3c_handle_ep0(struct s3c_udc *dev)
1454 if (dev->ep0state == WAIT_FOR_SETUP) {
1455 debug_cond(DEBUG_OUT_EP != 0,
1456 "%s: WAIT_FOR_SETUP\n", __func__);
1460 debug_cond(DEBUG_OUT_EP != 0,
1461 "%s: strange state!!(state = %s)\n",
1462 __func__, state_names[dev->ep0state]);
1466 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep)
1468 debug_cond(DEBUG_EP0 != 0,
1469 "%s: ep_is_in = %d\n", __func__, ep_is_in(ep));
1471 dev->ep0state = DATA_STATE_XMIT;
1475 dev->ep0state = DATA_STATE_RECV;