2 * linux/drivers/usb/gadget/s3c-hsotg.c
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Copyright 2008 Openmoko, Inc.
8 * Copyright 2008 Simtec Electronics
9 * Ben Dooks <ben@simtec.co.uk>
10 * http://armlinux.simtec.co.uk/
12 * S3C USB2.0 High-speed / OtG driver
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/clk.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/of_platform.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
36 #include <linux/usb/phy.h>
37 #include <linux/platform_data/s3c-hsotg.h>
41 #include "s3c-hsotg.h"
43 static const char * const s3c_hsotg_supply_names[] = {
44 "vusb_d", /* digital USB supply, 1.2V */
45 "vusb_a", /* analog USB supply, 1.1V */
51 * Unfortunately there seems to be a limit of the amount of data that can
52 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
53 * packets (which practically means 1 packet and 63 bytes of data) when the
56 * This means if we are wanting to move >127 bytes of data, we need to
57 * split the transactions up, but just doing one packet at a time does
58 * not work (this may be an implicit DATA0 PID on first packet of the
59 * transaction) and doing 2 packets is outside the controller's limits.
61 * If we try to lower the MPS size for EP0, then no transfers work properly
62 * for EP0, and the system will fail basic enumeration. As no cause for this
63 * has currently been found, we cannot support any large IN transfers for
66 #define EP0_MPS_LIMIT 64
72 * struct s3c_hsotg_ep - driver endpoint definition.
73 * @ep: The gadget layer representation of the endpoint.
74 * @name: The driver generated name for the endpoint.
75 * @queue: Queue of requests for this endpoint.
76 * @parent: Reference back to the parent device structure.
77 * @req: The current request that the endpoint is processing. This is
78 * used to indicate an request has been loaded onto the endpoint
79 * and has yet to be completed (maybe due to data move, or simply
80 * awaiting an ack from the core all the data has been completed).
81 * @debugfs: File entry for debugfs file for this endpoint.
82 * @lock: State lock to protect contents of endpoint.
83 * @dir_in: Set to true if this endpoint is of the IN direction, which
84 * means that it is sending data to the Host.
85 * @index: The index for the endpoint registers.
86 * @mc: Multi Count - number of transactions per microframe
87 * @interval - Interval for periodic endpoints
88 * @name: The name array passed to the USB core.
89 * @halted: Set if the endpoint has been halted.
90 * @periodic: Set if this is a periodic ep, such as Interrupt
91 * @isochronous: Set if this is a isochronous ep
92 * @sent_zlp: Set if we've sent a zero-length packet.
93 * @total_data: The total number of data bytes done.
94 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
95 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
96 * @last_load: The offset of data for the last start of request.
97 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
99 * This is the driver's state for each registered enpoint, allowing it
100 * to keep track of transactions that need doing. Each endpoint has a
101 * lock to protect the state, to try and avoid using an overall lock
102 * for the host controller as much as possible.
104 * For periodic IN endpoints, we have fifo_size and fifo_load to try
105 * and keep track of the amount of data in the periodic FIFO for each
106 * of these as we don't have a status register that tells us how much
107 * is in each of them. (note, this may actually be useless information
108 * as in shared-fifo mode periodic in acts like a single-frame packet
109 * buffer than a fifo)
111 struct s3c_hsotg_ep {
113 struct list_head queue;
114 struct s3c_hsotg *parent;
115 struct s3c_hsotg_req *req;
116 struct dentry *debugfs;
119 unsigned long total_data;
120 unsigned int size_loaded;
121 unsigned int last_load;
122 unsigned int fifo_load;
123 unsigned short fifo_size;
125 unsigned char dir_in;
128 unsigned char interval;
130 unsigned int halted:1;
131 unsigned int periodic:1;
132 unsigned int isochronous:1;
133 unsigned int sent_zlp:1;
139 * struct s3c_hsotg - driver state.
140 * @dev: The parent device supplied to the probe function
141 * @driver: USB gadget driver
142 * @phy: The otg phy transceiver structure for phy control.
143 * @plat: The platform specific configuration data. This can be removed once
144 * all SoCs support usb transceiver.
145 * @regs: The memory area mapped for accessing registers.
146 * @irq: The IRQ number we are using
147 * @supplies: Definition of USB power supplies
148 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
149 * @num_of_eps: Number of available EPs (excluding EP0)
150 * @debug_root: root directrory for debugfs.
151 * @debug_file: main status file for debugfs.
152 * @debug_fifo: FIFO status file for debugfs.
153 * @ep0_reply: Request used for ep0 reply.
154 * @ep0_buff: Buffer for EP0 reply data, if needed.
155 * @ctrl_buff: Buffer for EP0 control requests.
156 * @ctrl_req: Request for EP0 control packets.
157 * @setup: NAK management for EP0 SETUP
158 * @last_rst: Time of last reset
159 * @eps: The endpoints being supplied to the gadget framework
163 struct usb_gadget_driver *driver;
165 struct s3c_hsotg_plat *plat;
173 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
175 unsigned int dedicated_fifos:1;
176 unsigned char num_of_eps;
178 struct dentry *debug_root;
179 struct dentry *debug_file;
180 struct dentry *debug_fifo;
182 struct usb_request *ep0_reply;
183 struct usb_request *ctrl_req;
187 struct usb_gadget gadget;
189 unsigned long last_rst;
190 struct s3c_hsotg_ep *eps;
194 * struct s3c_hsotg_req - data transfer request
195 * @req: The USB gadget request
196 * @queue: The list of requests for the endpoint this is queued for.
197 * @in_progress: Has already had size/packets written to core
198 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
200 struct s3c_hsotg_req {
201 struct usb_request req;
202 struct list_head queue;
203 unsigned char in_progress;
204 unsigned char mapped;
207 /* conversion functions */
208 static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
210 return container_of(req, struct s3c_hsotg_req, req);
213 static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
215 return container_of(ep, struct s3c_hsotg_ep, ep);
218 static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
220 return container_of(gadget, struct s3c_hsotg, gadget);
223 static inline void __orr32(void __iomem *ptr, u32 val)
225 writel(readl(ptr) | val, ptr);
228 static inline void __bic32(void __iomem *ptr, u32 val)
230 writel(readl(ptr) & ~val, ptr);
233 /* forward decleration of functions */
234 static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
237 * using_dma - return the DMA status of the driver.
238 * @hsotg: The driver state.
240 * Return true if we're using DMA.
242 * Currently, we have the DMA support code worked into everywhere
243 * that needs it, but the AMBA DMA implementation in the hardware can
244 * only DMA from 32bit aligned addresses. This means that gadgets such
245 * as the CDC Ethernet cannot work as they often pass packets which are
248 * Unfortunately the choice to use DMA or not is global to the controller
249 * and seems to be only settable when the controller is being put through
250 * a core reset. This means we either need to fix the gadgets to take
251 * account of DMA alignment, or add bounce buffers (yuerk).
253 * Until this issue is sorted out, we always return 'false'.
255 static inline bool using_dma(struct s3c_hsotg *hsotg)
257 return false; /* support is not complete */
261 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
262 * @hsotg: The device state
263 * @ints: A bitmask of the interrupts to enable
265 static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
267 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
270 new_gsintmsk = gsintmsk | ints;
272 if (new_gsintmsk != gsintmsk) {
273 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
274 writel(new_gsintmsk, hsotg->regs + GINTMSK);
279 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
280 * @hsotg: The device state
281 * @ints: A bitmask of the interrupts to enable
283 static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
285 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
288 new_gsintmsk = gsintmsk & ~ints;
290 if (new_gsintmsk != gsintmsk)
291 writel(new_gsintmsk, hsotg->regs + GINTMSK);
295 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
296 * @hsotg: The device state
297 * @ep: The endpoint index
298 * @dir_in: True if direction is in.
299 * @en: The enable value, true to enable
301 * Set or clear the mask for an individual endpoint's interrupt
304 static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
305 unsigned int ep, unsigned int dir_in,
315 local_irq_save(flags);
316 daint = readl(hsotg->regs + DAINTMSK);
321 writel(daint, hsotg->regs + DAINTMSK);
322 local_irq_restore(flags);
326 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
327 * @hsotg: The device instance.
329 static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
337 /* set FIFO sizes to 2048/1024 */
339 writel(2048, hsotg->regs + GRXFSIZ);
340 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
341 GNPTXFSIZ_NPTxFDep(1024),
342 hsotg->regs + GNPTXFSIZ);
345 * arange all the rest of the TX FIFOs, as some versions of this
346 * block have overlapping default addresses. This also ensures
347 * that if the settings have been changed, then they are set to
351 /* start at the end of the GNPTXFSIZ, rounded up */
356 * currently we allocate TX FIFOs for all possible endpoints,
357 * and assume that they are all the same size.
360 for (ep = 1; ep <= 15; ep++) {
362 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
365 writel(val, hsotg->regs + DPTXFSIZn(ep));
369 * according to p428 of the design guide, we need to ensure that
370 * all fifos are flushed before continuing
373 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
374 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
376 /* wait until the fifos are both flushed */
379 val = readl(hsotg->regs + GRSTCTL);
381 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
384 if (--timeout == 0) {
386 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
393 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
397 * @ep: USB endpoint to allocate request for.
398 * @flags: Allocation flags
400 * Allocate a new USB request structure appropriate for the specified endpoint
402 static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
405 struct s3c_hsotg_req *req;
407 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
411 INIT_LIST_HEAD(&req->queue);
417 * is_ep_periodic - return true if the endpoint is in periodic mode.
418 * @hs_ep: The endpoint to query.
420 * Returns true if the endpoint is in periodic mode, meaning it is being
421 * used for an Interrupt or ISO transfer.
423 static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
425 return hs_ep->periodic;
429 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
430 * @hsotg: The device state.
431 * @hs_ep: The endpoint for the request
432 * @hs_req: The request being processed.
434 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
435 * of a request to ensure the buffer is ready for access by the caller.
437 static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
438 struct s3c_hsotg_ep *hs_ep,
439 struct s3c_hsotg_req *hs_req)
441 struct usb_request *req = &hs_req->req;
443 /* ignore this if we're not moving any data */
444 if (hs_req->req.length == 0)
447 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
451 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
452 * @hsotg: The controller state.
453 * @hs_ep: The endpoint we're going to write for.
454 * @hs_req: The request to write data for.
456 * This is called when the TxFIFO has some space in it to hold a new
457 * transmission and we have something to give it. The actual setup of
458 * the data size is done elsewhere, so all we have to do is to actually
461 * The return value is zero if there is more space (or nothing was done)
462 * otherwise -ENOSPC is returned if the FIFO space was used up.
464 * This routine is only needed for PIO
466 static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
467 struct s3c_hsotg_ep *hs_ep,
468 struct s3c_hsotg_req *hs_req)
470 bool periodic = is_ep_periodic(hs_ep);
471 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
472 int buf_pos = hs_req->req.actual;
473 int to_write = hs_ep->size_loaded;
479 to_write -= (buf_pos - hs_ep->last_load);
481 /* if there's nothing to write, get out early */
485 if (periodic && !hsotg->dedicated_fifos) {
486 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
491 * work out how much data was loaded so we can calculate
492 * how much data is left in the fifo.
495 size_left = DxEPTSIZ_XferSize_GET(epsize);
498 * if shared fifo, we cannot write anything until the
499 * previous data has been completely sent.
501 if (hs_ep->fifo_load != 0) {
502 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
506 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
508 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
510 /* how much of the data has moved */
511 size_done = hs_ep->size_loaded - size_left;
513 /* how much data is left in the fifo */
514 can_write = hs_ep->fifo_load - size_done;
515 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
516 __func__, can_write);
518 can_write = hs_ep->fifo_size - can_write;
519 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
520 __func__, can_write);
522 if (can_write <= 0) {
523 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
526 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
527 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
532 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
534 "%s: no queue slots available (0x%08x)\n",
537 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
541 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
542 can_write *= 4; /* fifo size is in 32bit quantities. */
545 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
547 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
548 __func__, gnptxsts, can_write, to_write, max_transfer);
551 * limit to 512 bytes of data, it seems at least on the non-periodic
552 * FIFO, requests of >512 cause the endpoint to get stuck with a
553 * fragment of the end of the transfer in it.
555 if (can_write > 512 && !periodic)
559 * limit the write to one max-packet size worth of data, but allow
560 * the transfer to return that it did not run out of fifo space
563 if (to_write > max_transfer) {
564 to_write = max_transfer;
566 /* it's needed only when we do not use dedicated fifos */
567 if (!hsotg->dedicated_fifos)
568 s3c_hsotg_en_gsint(hsotg,
569 periodic ? GINTSTS_PTxFEmp :
573 /* see if we can write data */
575 if (to_write > can_write) {
576 to_write = can_write;
577 pkt_round = to_write % max_transfer;
580 * Round the write down to an
581 * exact number of packets.
583 * Note, we do not currently check to see if we can ever
584 * write a full packet or not to the FIFO.
588 to_write -= pkt_round;
591 * enable correct FIFO interrupt to alert us when there
595 /* it's needed only when we do not use dedicated fifos */
596 if (!hsotg->dedicated_fifos)
597 s3c_hsotg_en_gsint(hsotg,
598 periodic ? GINTSTS_PTxFEmp :
602 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
603 to_write, hs_req->req.length, can_write, buf_pos);
608 hs_req->req.actual = buf_pos + to_write;
609 hs_ep->total_data += to_write;
612 hs_ep->fifo_load += to_write;
614 to_write = DIV_ROUND_UP(to_write, 4);
615 data = hs_req->req.buf + buf_pos;
617 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
619 return (to_write >= can_write) ? -ENOSPC : 0;
623 * get_ep_limit - get the maximum data legnth for this endpoint
624 * @hs_ep: The endpoint
626 * Return the maximum data that can be queued in one go on a given endpoint
627 * so that transfers that are too long can be split.
629 static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
631 int index = hs_ep->index;
636 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
637 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
641 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
646 /* we made the constant loading easier above by using +1 */
651 * constrain by packet count if maxpkts*pktsize is greater
652 * than the length register size.
655 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
656 maxsize = maxpkt * hs_ep->ep.maxpacket;
662 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
663 * @hsotg: The controller state.
664 * @hs_ep: The endpoint to process a request for
665 * @hs_req: The request to start.
666 * @continuing: True if we are doing more for the current request.
668 * Start the given request running by setting the endpoint registers
669 * appropriately, and writing any data to the FIFOs.
671 static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
672 struct s3c_hsotg_ep *hs_ep,
673 struct s3c_hsotg_req *hs_req,
676 struct usb_request *ureq = &hs_req->req;
677 int index = hs_ep->index;
678 int dir_in = hs_ep->dir_in;
688 if (hs_ep->req && !continuing) {
689 dev_err(hsotg->dev, "%s: active request\n", __func__);
692 } else if (hs_ep->req != hs_req && continuing) {
694 "%s: continue different req\n", __func__);
700 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
701 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
703 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
704 __func__, readl(hsotg->regs + epctrl_reg), index,
705 hs_ep->dir_in ? "in" : "out");
707 /* If endpoint is stalled, we will restart request later */
708 ctrl = readl(hsotg->regs + epctrl_reg);
710 if (ctrl & DxEPCTL_Stall) {
711 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
715 length = ureq->length - ureq->actual;
716 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
717 ureq->length, ureq->actual);
720 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
721 ureq->buf, length, ureq->dma,
722 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
724 maxreq = get_ep_limit(hs_ep);
725 if (length > maxreq) {
726 int round = maxreq % hs_ep->ep.maxpacket;
728 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
729 __func__, length, maxreq, round);
731 /* round down to multiple of packets */
739 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
741 packets = 1; /* send one packet if length is zero. */
743 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
744 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
748 if (dir_in && index != 0)
749 if (hs_ep->isochronous)
750 epsize = DxEPTSIZ_MC(packets);
752 epsize = DxEPTSIZ_MC(1);
756 if (index != 0 && ureq->zero) {
758 * test for the packets being exactly right for the
762 if (length == (packets * hs_ep->ep.maxpacket))
766 epsize |= DxEPTSIZ_PktCnt(packets);
767 epsize |= DxEPTSIZ_XferSize(length);
769 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
770 __func__, packets, length, ureq->length, epsize, epsize_reg);
772 /* store the request as the current one we're doing */
775 /* write size / packets */
776 writel(epsize, hsotg->regs + epsize_reg);
778 if (using_dma(hsotg) && !continuing) {
779 unsigned int dma_reg;
782 * write DMA address to control register, buffer already
783 * synced by s3c_hsotg_ep_queue().
786 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
787 writel(ureq->dma, hsotg->regs + dma_reg);
789 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
790 __func__, ureq->dma, dma_reg);
793 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
794 ctrl |= DxEPCTL_USBActEp;
796 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
798 /* For Setup request do not clear NAK */
799 if (hsotg->setup && index == 0)
802 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
805 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
806 writel(ctrl, hsotg->regs + epctrl_reg);
809 * set these, it seems that DMA support increments past the end
810 * of the packet buffer so we need to calculate the length from
813 hs_ep->size_loaded = length;
814 hs_ep->last_load = ureq->actual;
816 if (dir_in && !using_dma(hsotg)) {
817 /* set these anyway, we may need them for non-periodic in */
818 hs_ep->fifo_load = 0;
820 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
824 * clear the INTknTXFEmpMsk when we start request, more as a aide
825 * to debugging to see what is going on.
828 writel(DIEPMSK_INTknTXFEmpMsk,
829 hsotg->regs + DIEPINT(index));
832 * Note, trying to clear the NAK here causes problems with transmit
833 * on the S3C6400 ending up with the TXFIFO becoming full.
836 /* check ep is enabled */
837 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
839 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
840 index, readl(hsotg->regs + epctrl_reg));
842 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
843 __func__, readl(hsotg->regs + epctrl_reg));
845 /* enable ep interrupts */
846 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
850 * s3c_hsotg_map_dma - map the DMA memory being used for the request
851 * @hsotg: The device state.
852 * @hs_ep: The endpoint the request is on.
853 * @req: The request being processed.
855 * We've been asked to queue a request, so ensure that the memory buffer
856 * is correctly setup for DMA. If we've been passed an extant DMA address
857 * then ensure the buffer has been synced to memory. If our buffer has no
858 * DMA memory, then we map the memory and mark our request to allow us to
859 * cleanup on completion.
861 static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
862 struct s3c_hsotg_ep *hs_ep,
863 struct usb_request *req)
865 struct s3c_hsotg_req *hs_req = our_req(req);
868 /* if the length is zero, ignore the DMA data */
869 if (hs_req->req.length == 0)
872 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
879 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
880 __func__, req->buf, req->length);
885 static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
888 struct s3c_hsotg_req *hs_req = our_req(req);
889 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
890 struct s3c_hsotg *hs = hs_ep->parent;
893 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
894 ep->name, req, req->length, req->buf, req->no_interrupt,
895 req->zero, req->short_not_ok);
897 /* initialise status of the request */
898 INIT_LIST_HEAD(&hs_req->queue);
900 req->status = -EINPROGRESS;
902 /* if we're using DMA, sync the buffers as necessary */
904 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
909 first = list_empty(&hs_ep->queue);
910 list_add_tail(&hs_req->queue, &hs_ep->queue);
913 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
918 static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
921 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
922 struct s3c_hsotg *hs = hs_ep->parent;
923 unsigned long flags = 0;
926 spin_lock_irqsave(&hs->lock, flags);
927 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
928 spin_unlock_irqrestore(&hs->lock, flags);
933 static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
934 struct usb_request *req)
936 struct s3c_hsotg_req *hs_req = our_req(req);
942 * s3c_hsotg_complete_oursetup - setup completion callback
943 * @ep: The endpoint the request was on.
944 * @req: The request completed.
946 * Called on completion of any requests the driver itself
947 * submitted that need cleaning up.
949 static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
950 struct usb_request *req)
952 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
953 struct s3c_hsotg *hsotg = hs_ep->parent;
955 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
957 s3c_hsotg_ep_free_request(ep, req);
961 * ep_from_windex - convert control wIndex value to endpoint
962 * @hsotg: The driver state.
963 * @windex: The control request wIndex field (in host order).
965 * Convert the given wIndex into a pointer to an driver endpoint
966 * structure, or return NULL if it is not a valid endpoint.
968 static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
971 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
972 int dir = (windex & USB_DIR_IN) ? 1 : 0;
973 int idx = windex & 0x7F;
978 if (idx > hsotg->num_of_eps)
981 if (idx && ep->dir_in != dir)
988 * s3c_hsotg_send_reply - send reply to control request
989 * @hsotg: The device state
991 * @buff: Buffer for request
992 * @length: Length of reply.
994 * Create a request and queue it on the given endpoint. This is useful as
995 * an internal method of sending replies to certain control requests, etc.
997 static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
998 struct s3c_hsotg_ep *ep,
1002 struct usb_request *req;
1005 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1007 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1008 hsotg->ep0_reply = req;
1010 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1014 req->buf = hsotg->ep0_buff;
1015 req->length = length;
1016 req->zero = 1; /* always do zero-length final transfer */
1017 req->complete = s3c_hsotg_complete_oursetup;
1020 memcpy(req->buf, buff, length);
1024 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1026 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1034 * s3c_hsotg_process_req_status - process request GET_STATUS
1035 * @hsotg: The device state
1036 * @ctrl: USB control request
1038 static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1039 struct usb_ctrlrequest *ctrl)
1041 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1042 struct s3c_hsotg_ep *ep;
1046 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1049 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1053 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1054 case USB_RECIP_DEVICE:
1055 reply = cpu_to_le16(0); /* bit 0 => self powered,
1056 * bit 1 => remote wakeup */
1059 case USB_RECIP_INTERFACE:
1060 /* currently, the data result should be zero */
1061 reply = cpu_to_le16(0);
1064 case USB_RECIP_ENDPOINT:
1065 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1069 reply = cpu_to_le16(ep->halted ? 1 : 0);
1076 if (le16_to_cpu(ctrl->wLength) != 2)
1079 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1081 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1088 static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1091 * get_ep_head - return the first request on the endpoint
1092 * @hs_ep: The controller endpoint to get
1094 * Get the first request on the endpoint.
1096 static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1098 if (list_empty(&hs_ep->queue))
1101 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1105 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1106 * @hsotg: The device state
1107 * @ctrl: USB control request
1109 static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1110 struct usb_ctrlrequest *ctrl)
1112 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1113 struct s3c_hsotg_req *hs_req;
1115 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1116 struct s3c_hsotg_ep *ep;
1120 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1121 __func__, set ? "SET" : "CLEAR");
1123 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1124 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1126 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1127 __func__, le16_to_cpu(ctrl->wIndex));
1131 switch (le16_to_cpu(ctrl->wValue)) {
1132 case USB_ENDPOINT_HALT:
1133 halted = ep->halted;
1135 s3c_hsotg_ep_sethalt(&ep->ep, set);
1137 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1140 "%s: failed to send reply\n", __func__);
1145 * we have to complete all requests for ep if it was
1146 * halted, and the halt was cleared by CLEAR_FEATURE
1149 if (!set && halted) {
1151 * If we have request in progress,
1157 list_del_init(&hs_req->queue);
1158 hs_req->req.complete(&ep->ep,
1162 /* If we have pending request, then start it */
1163 restart = !list_empty(&ep->queue);
1165 hs_req = get_ep_head(ep);
1166 s3c_hsotg_start_req(hsotg, ep,
1177 return -ENOENT; /* currently only deal with endpoint */
1182 static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1183 static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
1186 * s3c_hsotg_process_control - process a control request
1187 * @hsotg: The device state
1188 * @ctrl: The control request received
1190 * The controller has received the SETUP phase of a control request, and
1191 * needs to work out what to do next (and whether to pass it on to the
1194 static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1195 struct usb_ctrlrequest *ctrl)
1197 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1203 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1204 ctrl->bRequest, ctrl->bRequestType,
1205 ctrl->wValue, ctrl->wLength);
1208 * record the direction of the request, for later use when enquing
1212 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1213 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1216 * if we've no data with this request, then the last part of the
1217 * transaction is going to implicitly be IN.
1219 if (ctrl->wLength == 0)
1222 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1223 switch (ctrl->bRequest) {
1224 case USB_REQ_SET_ADDRESS:
1225 s3c_hsotg_disconnect(hsotg);
1226 dcfg = readl(hsotg->regs + DCFG);
1227 dcfg &= ~DCFG_DevAddr_MASK;
1228 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1229 writel(dcfg, hsotg->regs + DCFG);
1231 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1233 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1236 case USB_REQ_GET_STATUS:
1237 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1240 case USB_REQ_CLEAR_FEATURE:
1241 case USB_REQ_SET_FEATURE:
1242 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1247 /* as a fallback, try delivering it to the driver to deal with */
1249 if (ret == 0 && hsotg->driver) {
1250 spin_unlock(&hsotg->lock);
1251 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1252 spin_lock(&hsotg->lock);
1254 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1258 * the request is either unhandlable, or is not formatted correctly
1259 * so respond with a STALL for the status stage to indicate failure.
1266 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1267 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1270 * DxEPCTL_Stall will be cleared by EP once it has
1271 * taken effect, so no need to clear later.
1274 ctrl = readl(hsotg->regs + reg);
1275 ctrl |= DxEPCTL_Stall;
1276 ctrl |= DxEPCTL_CNAK;
1277 writel(ctrl, hsotg->regs + reg);
1280 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1281 ctrl, reg, readl(hsotg->regs + reg));
1284 * don't believe we need to anything more to get the EP
1285 * to reply with a STALL packet
1289 * complete won't be called, so we enqueue
1290 * setup request here
1292 s3c_hsotg_enqueue_setup(hsotg);
1297 * s3c_hsotg_complete_setup - completion of a setup transfer
1298 * @ep: The endpoint the request was on.
1299 * @req: The request completed.
1301 * Called on completion of any requests the driver itself submitted for
1304 static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1305 struct usb_request *req)
1307 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1308 struct s3c_hsotg *hsotg = hs_ep->parent;
1310 if (req->status < 0) {
1311 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1315 spin_lock(&hsotg->lock);
1316 if (req->actual == 0)
1317 s3c_hsotg_enqueue_setup(hsotg);
1319 s3c_hsotg_process_control(hsotg, req->buf);
1320 spin_unlock(&hsotg->lock);
1324 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1325 * @hsotg: The device state.
1327 * Enqueue a request on EP0 if necessary to received any SETUP packets
1328 * received from the host.
1330 static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1332 struct usb_request *req = hsotg->ctrl_req;
1333 struct s3c_hsotg_req *hs_req = our_req(req);
1336 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1340 req->buf = hsotg->ctrl_buff;
1341 req->complete = s3c_hsotg_complete_setup;
1343 if (!list_empty(&hs_req->queue)) {
1344 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1348 hsotg->eps[0].dir_in = 0;
1350 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1352 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1354 * Don't think there's much we can do other than watch the
1361 * s3c_hsotg_complete_request - complete a request given to us
1362 * @hsotg: The device state.
1363 * @hs_ep: The endpoint the request was on.
1364 * @hs_req: The request to complete.
1365 * @result: The result code (0 => Ok, otherwise errno)
1367 * The given request has finished, so call the necessary completion
1368 * if it has one and then look to see if we can start a new request
1371 * Note, expects the ep to already be locked as appropriate.
1373 static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1374 struct s3c_hsotg_ep *hs_ep,
1375 struct s3c_hsotg_req *hs_req,
1381 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1385 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1386 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1389 * only replace the status if we've not already set an error
1390 * from a previous transaction
1393 if (hs_req->req.status == -EINPROGRESS)
1394 hs_req->req.status = result;
1397 list_del_init(&hs_req->queue);
1399 if (using_dma(hsotg))
1400 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1403 * call the complete request with the locks off, just in case the
1404 * request tries to queue more work for this endpoint.
1407 if (hs_req->req.complete) {
1408 spin_unlock(&hsotg->lock);
1409 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1410 spin_lock(&hsotg->lock);
1414 * Look to see if there is anything else to do. Note, the completion
1415 * of the previous request may have caused a new request to be started
1416 * so be careful when doing this.
1419 if (!hs_ep->req && result >= 0) {
1420 restart = !list_empty(&hs_ep->queue);
1422 hs_req = get_ep_head(hs_ep);
1423 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1429 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1430 * @hsotg: The device state.
1431 * @ep_idx: The endpoint index for the data
1432 * @size: The size of data in the fifo, in bytes
1434 * The FIFO status shows there is data to read from the FIFO for a given
1435 * endpoint, so sort out whether we need to read the data into a request
1436 * that has been made for that endpoint.
1438 static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1440 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1441 struct s3c_hsotg_req *hs_req = hs_ep->req;
1442 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
1449 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
1452 dev_warn(hsotg->dev,
1453 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1454 __func__, size, ep_idx, epctl);
1456 /* dump the data from the FIFO, we've nothing we can do */
1457 for (ptr = 0; ptr < size; ptr += 4)
1464 read_ptr = hs_req->req.actual;
1465 max_req = hs_req->req.length - read_ptr;
1467 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1468 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1470 if (to_read > max_req) {
1472 * more data appeared than we where willing
1473 * to deal with in this request.
1476 /* currently we don't deal this */
1480 hs_ep->total_data += to_read;
1481 hs_req->req.actual += to_read;
1482 to_read = DIV_ROUND_UP(to_read, 4);
1485 * note, we might over-write the buffer end by 3 bytes depending on
1486 * alignment of the data.
1488 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1492 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1493 * @hsotg: The device instance
1494 * @req: The request currently on this endpoint
1496 * Generate a zero-length IN packet request for terminating a SETUP
1499 * Note, since we don't write any data to the TxFIFO, then it is
1500 * currently believed that we do not need to wait for any space in
1503 static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1504 struct s3c_hsotg_req *req)
1509 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1513 if (req->req.length == 0) {
1514 hsotg->eps[0].sent_zlp = 1;
1515 s3c_hsotg_enqueue_setup(hsotg);
1519 hsotg->eps[0].dir_in = 1;
1520 hsotg->eps[0].sent_zlp = 1;
1522 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1524 /* issue a zero-sized packet to terminate this */
1525 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1526 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
1528 ctrl = readl(hsotg->regs + DIEPCTL0);
1529 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
1530 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
1531 ctrl |= DxEPCTL_USBActEp;
1532 writel(ctrl, hsotg->regs + DIEPCTL0);
1536 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1537 * @hsotg: The device instance
1538 * @epnum: The endpoint received from
1539 * @was_setup: Set if processing a SetupDone event.
1541 * The RXFIFO has delivered an OutDone event, which means that the data
1542 * transfer for an OUT endpoint has been completed, either by a short
1543 * packet or by the finish of a transfer.
1545 static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1546 int epnum, bool was_setup)
1548 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
1549 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1550 struct s3c_hsotg_req *hs_req = hs_ep->req;
1551 struct usb_request *req = &hs_req->req;
1552 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
1556 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1560 if (using_dma(hsotg)) {
1564 * Calculate the size of the transfer by checking how much
1565 * is left in the endpoint size register and then working it
1566 * out from the amount we loaded for the transfer.
1568 * We need to do this as DMA pointers are always 32bit aligned
1569 * so may overshoot/undershoot the transfer.
1572 size_done = hs_ep->size_loaded - size_left;
1573 size_done += hs_ep->last_load;
1575 req->actual = size_done;
1578 /* if there is more request to do, schedule new transfer */
1579 if (req->actual < req->length && size_left == 0) {
1580 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1582 } else if (epnum == 0) {
1584 * After was_setup = 1 =>
1585 * set CNAK for non Setup requests
1587 hsotg->setup = was_setup ? 0 : 1;
1590 if (req->actual < req->length && req->short_not_ok) {
1591 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1592 __func__, req->actual, req->length);
1595 * todo - what should we return here? there's no one else
1596 * even bothering to check the status.
1602 * Condition req->complete != s3c_hsotg_complete_setup says:
1603 * send ZLP when we have an asynchronous request from gadget
1605 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1606 s3c_hsotg_send_zlp(hsotg, hs_req);
1609 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1613 * s3c_hsotg_read_frameno - read current frame number
1614 * @hsotg: The device instance
1616 * Return the current frame number
1618 static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1622 dsts = readl(hsotg->regs + DSTS);
1623 dsts &= DSTS_SOFFN_MASK;
1624 dsts >>= DSTS_SOFFN_SHIFT;
1630 * s3c_hsotg_handle_rx - RX FIFO has data
1631 * @hsotg: The device instance
1633 * The IRQ handler has detected that the RX FIFO has some data in it
1634 * that requires processing, so find out what is in there and do the
1637 * The RXFIFO is a true FIFO, the packets coming out are still in packet
1638 * chunks, so if you have x packets received on an endpoint you'll get x
1639 * FIFO events delivered, each with a packet's worth of data in it.
1641 * When using DMA, we should not be processing events from the RXFIFO
1642 * as the actual data should be sent to the memory directly and we turn
1643 * on the completion interrupts to get notifications of transfer completion.
1645 static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
1647 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
1648 u32 epnum, status, size;
1650 WARN_ON(using_dma(hsotg));
1652 epnum = grxstsr & GRXSTS_EPNum_MASK;
1653 status = grxstsr & GRXSTS_PktSts_MASK;
1655 size = grxstsr & GRXSTS_ByteCnt_MASK;
1656 size >>= GRXSTS_ByteCnt_SHIFT;
1659 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1660 __func__, grxstsr, size, epnum);
1662 #define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
1664 switch (status >> GRXSTS_PktSts_SHIFT) {
1665 case __status(GRXSTS_PktSts_GlobalOutNAK):
1666 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1669 case __status(GRXSTS_PktSts_OutDone):
1670 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1671 s3c_hsotg_read_frameno(hsotg));
1673 if (!using_dma(hsotg))
1674 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1677 case __status(GRXSTS_PktSts_SetupDone):
1679 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1680 s3c_hsotg_read_frameno(hsotg),
1681 readl(hsotg->regs + DOEPCTL(0)));
1683 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1686 case __status(GRXSTS_PktSts_OutRX):
1687 s3c_hsotg_rx_data(hsotg, epnum, size);
1690 case __status(GRXSTS_PktSts_SetupRX):
1692 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1693 s3c_hsotg_read_frameno(hsotg),
1694 readl(hsotg->regs + DOEPCTL(0)));
1696 s3c_hsotg_rx_data(hsotg, epnum, size);
1700 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1703 s3c_hsotg_dump(hsotg);
1709 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1710 * @mps: The maximum packet size in bytes.
1712 static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1716 return D0EPCTL_MPS_64;
1718 return D0EPCTL_MPS_32;
1720 return D0EPCTL_MPS_16;
1722 return D0EPCTL_MPS_8;
1725 /* bad max packet size, warn and return invalid result */
1731 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1732 * @hsotg: The driver state.
1733 * @ep: The index number of the endpoint
1734 * @mps: The maximum packet size in bytes
1736 * Configure the maximum packet size for the given endpoint, updating
1737 * the hardware control registers to reflect this.
1739 static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1740 unsigned int ep, unsigned int mps)
1742 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1743 void __iomem *regs = hsotg->regs;
1749 /* EP0 is a special case */
1750 mpsval = s3c_hsotg_ep0_mps(mps);
1753 hs_ep->ep.maxpacket = mps;
1756 mpsval = mps & DxEPCTL_MPS_MASK;
1759 mcval = ((mps >> 11) & 0x3) + 1;
1763 hs_ep->ep.maxpacket = mpsval;
1767 * update both the in and out endpoint controldir_ registers, even
1768 * if one of the directions may not be in use.
1771 reg = readl(regs + DIEPCTL(ep));
1772 reg &= ~DxEPCTL_MPS_MASK;
1774 writel(reg, regs + DIEPCTL(ep));
1777 reg = readl(regs + DOEPCTL(ep));
1778 reg &= ~DxEPCTL_MPS_MASK;
1780 writel(reg, regs + DOEPCTL(ep));
1786 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1790 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1791 * @hsotg: The driver state
1792 * @idx: The index for the endpoint (0..15)
1794 static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1799 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1800 hsotg->regs + GRSTCTL);
1802 /* wait until the fifo is flushed */
1806 val = readl(hsotg->regs + GRSTCTL);
1808 if ((val & (GRSTCTL_TxFFlsh)) == 0)
1811 if (--timeout == 0) {
1813 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1822 * s3c_hsotg_trytx - check to see if anything needs transmitting
1823 * @hsotg: The driver state
1824 * @hs_ep: The driver endpoint to check.
1826 * Check to see if there is a request that has data to send, and if so
1827 * make an attempt to write data into the FIFO.
1829 static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1830 struct s3c_hsotg_ep *hs_ep)
1832 struct s3c_hsotg_req *hs_req = hs_ep->req;
1834 if (!hs_ep->dir_in || !hs_req) {
1836 * if request is not enqueued, we disable interrupts
1837 * for endpoints, excepting ep0
1839 if (hs_ep->index != 0)
1840 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1845 if (hs_req->req.actual < hs_req->req.length) {
1846 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1848 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1855 * s3c_hsotg_complete_in - complete IN transfer
1856 * @hsotg: The device state.
1857 * @hs_ep: The endpoint that has just completed.
1859 * An IN transfer has been completed, update the transfer's state and then
1860 * call the relevant completion routines.
1862 static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1863 struct s3c_hsotg_ep *hs_ep)
1865 struct s3c_hsotg_req *hs_req = hs_ep->req;
1866 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
1867 int size_left, size_done;
1870 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1874 /* Finish ZLP handling for IN EP0 transactions */
1875 if (hsotg->eps[0].sent_zlp) {
1876 dev_dbg(hsotg->dev, "zlp packet received\n");
1877 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1882 * Calculate the size of the transfer by checking how much is left
1883 * in the endpoint size register and then working it out from
1884 * the amount we loaded for the transfer.
1886 * We do this even for DMA, as the transfer may have incremented
1887 * past the end of the buffer (DMA transfers are always 32bit
1891 size_left = DxEPTSIZ_XferSize_GET(epsize);
1893 size_done = hs_ep->size_loaded - size_left;
1894 size_done += hs_ep->last_load;
1896 if (hs_req->req.actual != size_done)
1897 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1898 __func__, hs_req->req.actual, size_done);
1900 hs_req->req.actual = size_done;
1901 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1902 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
1905 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1906 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1907 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1908 * inform the host that no more data is available.
1909 * The state of req.zero member is checked to be sure that the value to
1910 * send is smaller than wValue expected from host.
1911 * Check req.length to NOT send another ZLP when the current one is
1912 * under completion (the one for which this completion has been called).
1914 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1915 hs_req->req.length == hs_req->req.actual &&
1916 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1918 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1919 s3c_hsotg_send_zlp(hsotg, hs_req);
1924 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1925 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1926 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1928 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1932 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1933 * @hsotg: The driver state
1934 * @idx: The index for the endpoint (0..15)
1935 * @dir_in: Set if this is an IN endpoint
1937 * Process and clear any interrupt pending for an individual endpoint
1939 static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1942 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1943 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1944 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1945 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
1949 ints = readl(hsotg->regs + epint_reg);
1950 ctrl = readl(hsotg->regs + epctl_reg);
1952 /* Clear endpoint interrupts */
1953 writel(ints, hsotg->regs + epint_reg);
1955 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1956 __func__, idx, dir_in ? "in" : "out", ints);
1958 if (ints & DxEPINT_XferCompl) {
1959 if (hs_ep->isochronous && hs_ep->interval == 1) {
1960 if (ctrl & DxEPCTL_EOFrNum)
1961 ctrl |= DxEPCTL_SetEvenFr;
1963 ctrl |= DxEPCTL_SetOddFr;
1964 writel(ctrl, hsotg->regs + epctl_reg);
1968 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1969 __func__, readl(hsotg->regs + epctl_reg),
1970 readl(hsotg->regs + epsiz_reg));
1973 * we get OutDone from the FIFO, so we only need to look
1974 * at completing IN requests here
1977 s3c_hsotg_complete_in(hsotg, hs_ep);
1979 if (idx == 0 && !hs_ep->req)
1980 s3c_hsotg_enqueue_setup(hsotg);
1981 } else if (using_dma(hsotg)) {
1983 * We're using DMA, we need to fire an OutDone here
1984 * as we ignore the RXFIFO.
1987 s3c_hsotg_handle_outdone(hsotg, idx, false);
1991 if (ints & DxEPINT_EPDisbld) {
1992 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
1995 int epctl = readl(hsotg->regs + epctl_reg);
1997 s3c_hsotg_txfifo_flush(hsotg, idx);
1999 if ((epctl & DxEPCTL_Stall) &&
2000 (epctl & DxEPCTL_EPType_Bulk)) {
2001 int dctl = readl(hsotg->regs + DCTL);
2003 dctl |= DCTL_CGNPInNAK;
2004 writel(dctl, hsotg->regs + DCTL);
2009 if (ints & DxEPINT_AHBErr)
2010 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
2012 if (ints & DxEPINT_Setup) { /* Setup or Timeout */
2013 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2015 if (using_dma(hsotg) && idx == 0) {
2017 * this is the notification we've received a
2018 * setup packet. In non-DMA mode we'd get this
2019 * from the RXFIFO, instead we need to process
2026 s3c_hsotg_handle_outdone(hsotg, 0, true);
2030 if (ints & DxEPINT_Back2BackSetup)
2031 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
2033 if (dir_in && !hs_ep->isochronous) {
2034 /* not sure if this is important, but we'll clear it anyway */
2035 if (ints & DIEPMSK_INTknTXFEmpMsk) {
2036 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2040 /* this probably means something bad is happening */
2041 if (ints & DIEPMSK_INTknEPMisMsk) {
2042 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2046 /* FIFO has space or is empty (see GAHBCFG) */
2047 if (hsotg->dedicated_fifos &&
2048 ints & DIEPMSK_TxFIFOEmpty) {
2049 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2051 if (!using_dma(hsotg))
2052 s3c_hsotg_trytx(hsotg, hs_ep);
2058 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2059 * @hsotg: The device state.
2061 * Handle updating the device settings after the enumeration phase has
2064 static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2066 u32 dsts = readl(hsotg->regs + DSTS);
2067 int ep0_mps = 0, ep_mps;
2070 * This should signal the finish of the enumeration phase
2071 * of the USB handshaking, so we should now know what rate
2075 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2078 * note, since we're limited by the size of transfer on EP0, and
2079 * it seems IN transfers must be a even number of packets we do
2080 * not advertise a 64byte MPS on EP0.
2083 /* catch both EnumSpd_FS and EnumSpd_FS48 */
2084 switch (dsts & DSTS_EnumSpd_MASK) {
2085 case DSTS_EnumSpd_FS:
2086 case DSTS_EnumSpd_FS48:
2087 hsotg->gadget.speed = USB_SPEED_FULL;
2088 ep0_mps = EP0_MPS_LIMIT;
2092 case DSTS_EnumSpd_HS:
2093 hsotg->gadget.speed = USB_SPEED_HIGH;
2094 ep0_mps = EP0_MPS_LIMIT;
2098 case DSTS_EnumSpd_LS:
2099 hsotg->gadget.speed = USB_SPEED_LOW;
2101 * note, we don't actually support LS in this driver at the
2102 * moment, and the documentation seems to imply that it isn't
2103 * supported by the PHYs on some of the devices.
2107 dev_info(hsotg->dev, "new device is %s\n",
2108 usb_speed_string(hsotg->gadget.speed));
2111 * we should now know the maximum packet size for an
2112 * endpoint, so set the endpoints to a default value.
2117 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
2118 for (i = 1; i < hsotg->num_of_eps; i++)
2119 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2122 /* ensure after enumeration our EP0 is active */
2124 s3c_hsotg_enqueue_setup(hsotg);
2126 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2127 readl(hsotg->regs + DIEPCTL0),
2128 readl(hsotg->regs + DOEPCTL0));
2132 * kill_all_requests - remove all requests from the endpoint's queue
2133 * @hsotg: The device state.
2134 * @ep: The endpoint the requests may be on.
2135 * @result: The result code to use.
2136 * @force: Force removal of any current requests
2138 * Go through the requests on the given endpoint and mark them
2139 * completed with the given result code.
2141 static void kill_all_requests(struct s3c_hsotg *hsotg,
2142 struct s3c_hsotg_ep *ep,
2143 int result, bool force)
2145 struct s3c_hsotg_req *req, *treq;
2147 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2149 * currently, we can't do much about an already
2150 * running request on an in endpoint
2153 if (ep->req == req && ep->dir_in && !force)
2156 s3c_hsotg_complete_request(hsotg, ep, req,
2161 #define call_gadget(_hs, _entry) \
2163 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
2164 (_hs)->driver && (_hs)->driver->_entry) { \
2165 spin_unlock(&_hs->lock); \
2166 (_hs)->driver->_entry(&(_hs)->gadget); \
2167 spin_lock(&_hs->lock); \
2172 * s3c_hsotg_disconnect - disconnect service
2173 * @hsotg: The device state.
2175 * The device has been disconnected. Remove all current
2176 * transactions and signal the gadget driver that this
2179 static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
2183 for (ep = 0; ep < hsotg->num_of_eps; ep++)
2184 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2186 call_gadget(hsotg, disconnect);
2190 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2191 * @hsotg: The device state:
2192 * @periodic: True if this is a periodic FIFO interrupt
2194 static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2196 struct s3c_hsotg_ep *ep;
2199 /* look through for any more data to transmit */
2201 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
2202 ep = &hsotg->eps[epno];
2207 if ((periodic && !ep->periodic) ||
2208 (!periodic && ep->periodic))
2211 ret = s3c_hsotg_trytx(hsotg, ep);
2217 /* IRQ flags which will trigger a retry around the IRQ loop */
2218 #define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2223 * s3c_hsotg_corereset - issue softreset to the core
2224 * @hsotg: The device state
2226 * Issue a soft reset to the core, and await the core finishing it.
2228 static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2233 dev_dbg(hsotg->dev, "resetting core\n");
2235 /* issue soft reset */
2236 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
2240 grstctl = readl(hsotg->regs + GRSTCTL);
2241 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
2243 if (grstctl & GRSTCTL_CSftRst) {
2244 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2251 u32 grstctl = readl(hsotg->regs + GRSTCTL);
2253 if (timeout-- < 0) {
2254 dev_info(hsotg->dev,
2255 "%s: reset failed, GRSTCTL=%08x\n",
2260 if (!(grstctl & GRSTCTL_AHBIdle))
2263 break; /* reset done */
2266 dev_dbg(hsotg->dev, "reset successful\n");
2271 * s3c_hsotg_core_init - issue softreset to the core
2272 * @hsotg: The device state
2274 * Issue a soft reset to the core, and await the core finishing it.
2276 static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2278 s3c_hsotg_corereset(hsotg);
2281 * we must now enable ep0 ready for host detection and then
2282 * set configuration.
2285 /* set the PLL on, remove the HNP/SRP and set the PHY */
2286 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) |
2287 (0x5 << 10), hsotg->regs + GUSBCFG);
2289 s3c_hsotg_init_fifo(hsotg);
2291 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
2293 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
2295 /* Clear any pending OTG interrupts */
2296 writel(0xffffffff, hsotg->regs + GOTGINT);
2298 /* Clear any pending interrupts */
2299 writel(0xffffffff, hsotg->regs + GINTSTS);
2301 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2302 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2303 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2304 GINTSTS_EnumDone | GINTSTS_OTGInt |
2305 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2306 hsotg->regs + GINTMSK);
2308 if (using_dma(hsotg))
2309 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2310 GAHBCFG_HBstLen_Incr4,
2311 hsotg->regs + GAHBCFG);
2313 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NPTxFEmpLvl |
2314 GAHBCFG_PTxFEmpLvl) : 0) |
2316 hsotg->regs + GAHBCFG);
2319 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2320 * when we have no data to transfer. Otherwise we get being flooded by
2324 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty |
2325 DIEPMSK_INTknTXFEmpMsk : 0) |
2326 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2327 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2328 DIEPMSK_INTknEPMisMsk,
2329 hsotg->regs + DIEPMSK);
2332 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2333 * DMA mode we may need this.
2335 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2336 DIEPMSK_TimeOUTMsk) : 0) |
2337 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2339 hsotg->regs + DOEPMSK);
2341 writel(0, hsotg->regs + DAINTMSK);
2343 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2344 readl(hsotg->regs + DIEPCTL0),
2345 readl(hsotg->regs + DOEPCTL0));
2347 /* enable in and out endpoint interrupts */
2348 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
2351 * Enable the RXFIFO when in slave mode, as this is how we collect
2352 * the data. In DMA mode, we get events from the FIFO but also
2353 * things we cannot process, so do not use it.
2355 if (!using_dma(hsotg))
2356 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
2358 /* Enable interrupts for EP0 in and out */
2359 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2360 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2362 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
2363 udelay(10); /* see openiboot */
2364 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
2366 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
2369 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
2370 * writing to the EPCTL register..
2373 /* set to read 1 8byte packet */
2374 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2375 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2377 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2378 DxEPCTL_CNAK | DxEPCTL_EPEna |
2380 hsotg->regs + DOEPCTL0);
2382 /* enable, but don't activate EP0in */
2383 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2384 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
2386 s3c_hsotg_enqueue_setup(hsotg);
2388 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2389 readl(hsotg->regs + DIEPCTL0),
2390 readl(hsotg->regs + DOEPCTL0));
2392 /* clear global NAKs */
2393 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2394 hsotg->regs + DCTL);
2396 /* must be at-least 3ms to allow bus to see disconnect */
2399 /* remove the soft-disconnect and let's go */
2400 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
2404 * s3c_hsotg_irq - handle device interrupt
2405 * @irq: The IRQ number triggered
2406 * @pw: The pw value when registered the handler.
2408 static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2410 struct s3c_hsotg *hsotg = pw;
2411 int retry_count = 8;
2415 spin_lock(&hsotg->lock);
2417 gintsts = readl(hsotg->regs + GINTSTS);
2418 gintmsk = readl(hsotg->regs + GINTMSK);
2420 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2421 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2425 if (gintsts & GINTSTS_OTGInt) {
2426 u32 otgint = readl(hsotg->regs + GOTGINT);
2428 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2430 writel(otgint, hsotg->regs + GOTGINT);
2433 if (gintsts & GINTSTS_SessReqInt) {
2434 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2435 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
2438 if (gintsts & GINTSTS_EnumDone) {
2439 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
2441 s3c_hsotg_irq_enumdone(hsotg);
2444 if (gintsts & GINTSTS_ConIDStsChng) {
2445 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2446 readl(hsotg->regs + DSTS),
2447 readl(hsotg->regs + GOTGCTL));
2449 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
2452 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2453 u32 daint = readl(hsotg->regs + DAINT);
2454 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2455 u32 daint_out, daint_in;
2459 daint_out = daint >> DAINT_OutEP_SHIFT;
2460 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
2462 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2464 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2466 s3c_hsotg_epint(hsotg, ep, 0);
2469 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2471 s3c_hsotg_epint(hsotg, ep, 1);
2475 if (gintsts & GINTSTS_USBRst) {
2477 u32 usb_status = readl(hsotg->regs + GOTGCTL);
2479 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2480 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2481 readl(hsotg->regs + GNPTXSTS));
2483 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
2485 if (usb_status & GOTGCTL_BSESVLD) {
2486 if (time_after(jiffies, hsotg->last_rst +
2487 msecs_to_jiffies(200))) {
2489 kill_all_requests(hsotg, &hsotg->eps[0],
2492 s3c_hsotg_core_init(hsotg);
2493 hsotg->last_rst = jiffies;
2498 /* check both FIFOs */
2500 if (gintsts & GINTSTS_NPTxFEmp) {
2501 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2504 * Disable the interrupt to stop it happening again
2505 * unless one of these endpoint routines decides that
2506 * it needs re-enabling
2509 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
2510 s3c_hsotg_irq_fifoempty(hsotg, false);
2513 if (gintsts & GINTSTS_PTxFEmp) {
2514 dev_dbg(hsotg->dev, "PTxFEmp\n");
2516 /* See note in GINTSTS_NPTxFEmp */
2518 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
2519 s3c_hsotg_irq_fifoempty(hsotg, true);
2522 if (gintsts & GINTSTS_RxFLvl) {
2524 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
2525 * we need to retry s3c_hsotg_handle_rx if this is still
2529 s3c_hsotg_handle_rx(hsotg);
2532 if (gintsts & GINTSTS_ModeMis) {
2533 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2534 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
2537 if (gintsts & GINTSTS_USBSusp) {
2538 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2539 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
2541 call_gadget(hsotg, suspend);
2544 if (gintsts & GINTSTS_WkUpInt) {
2545 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2546 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
2548 call_gadget(hsotg, resume);
2551 if (gintsts & GINTSTS_ErlySusp) {
2552 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2553 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
2557 * these next two seem to crop-up occasionally causing the core
2558 * to shutdown the USB transfer, so try clearing them and logging
2562 if (gintsts & GINTSTS_GOUTNakEff) {
2563 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2565 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
2567 s3c_hsotg_dump(hsotg);
2570 if (gintsts & GINTSTS_GINNakEff) {
2571 dev_info(hsotg->dev, "GINNakEff triggered\n");
2573 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
2575 s3c_hsotg_dump(hsotg);
2579 * if we've had fifo events, we should try and go around the
2580 * loop again to see if there's any point in returning yet.
2583 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2586 spin_unlock(&hsotg->lock);
2592 * s3c_hsotg_ep_enable - enable the given endpoint
2593 * @ep: The USB endpint to configure
2594 * @desc: The USB endpoint descriptor to configure with.
2596 * This is called from the USB gadget code's usb_ep_enable().
2598 static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2599 const struct usb_endpoint_descriptor *desc)
2601 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2602 struct s3c_hsotg *hsotg = hs_ep->parent;
2603 unsigned long flags;
2604 int index = hs_ep->index;
2612 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2613 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2614 desc->wMaxPacketSize, desc->bInterval);
2616 /* not to be called for EP0 */
2617 WARN_ON(index == 0);
2619 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2620 if (dir_in != hs_ep->dir_in) {
2621 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2625 mps = usb_endpoint_maxp(desc);
2627 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2629 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
2630 epctrl = readl(hsotg->regs + epctrl_reg);
2632 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2633 __func__, epctrl, epctrl_reg);
2635 spin_lock_irqsave(&hsotg->lock, flags);
2637 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2638 epctrl |= DxEPCTL_MPS(mps);
2641 * mark the endpoint as active, otherwise the core may ignore
2642 * transactions entirely for this endpoint
2644 epctrl |= DxEPCTL_USBActEp;
2647 * set the NAK status on the endpoint, otherwise we might try and
2648 * do something with data that we've yet got a request to process
2649 * since the RXFIFO will take data for an endpoint even if the
2650 * size register hasn't been set.
2653 epctrl |= DxEPCTL_SNAK;
2655 /* update the endpoint state */
2656 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
2658 /* default, set to non-periodic */
2659 hs_ep->isochronous = 0;
2660 hs_ep->periodic = 0;
2662 hs_ep->interval = desc->bInterval;
2664 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2665 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2667 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2668 case USB_ENDPOINT_XFER_ISOC:
2669 epctrl |= DxEPCTL_EPType_Iso;
2670 epctrl |= DxEPCTL_SetEvenFr;
2671 hs_ep->isochronous = 1;
2673 hs_ep->periodic = 1;
2676 case USB_ENDPOINT_XFER_BULK:
2677 epctrl |= DxEPCTL_EPType_Bulk;
2680 case USB_ENDPOINT_XFER_INT:
2683 * Allocate our TxFNum by simply using the index
2684 * of the endpoint for the moment. We could do
2685 * something better if the host indicates how
2686 * many FIFOs we are expecting to use.
2689 hs_ep->periodic = 1;
2690 epctrl |= DxEPCTL_TxFNum(index);
2693 epctrl |= DxEPCTL_EPType_Intterupt;
2696 case USB_ENDPOINT_XFER_CONTROL:
2697 epctrl |= DxEPCTL_EPType_Control;
2702 * if the hardware has dedicated fifos, we must give each IN EP
2703 * a unique tx-fifo even if it is non-periodic.
2705 if (dir_in && hsotg->dedicated_fifos)
2706 epctrl |= DxEPCTL_TxFNum(index);
2708 /* for non control endpoints, set PID to D0 */
2710 epctrl |= DxEPCTL_SetD0PID;
2712 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2715 writel(epctrl, hsotg->regs + epctrl_reg);
2716 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2717 __func__, readl(hsotg->regs + epctrl_reg));
2719 /* enable the endpoint interrupt */
2720 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2722 spin_unlock_irqrestore(&hsotg->lock, flags);
2727 * s3c_hsotg_ep_disable - disable given endpoint
2728 * @ep: The endpoint to disable.
2730 static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2732 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2733 struct s3c_hsotg *hsotg = hs_ep->parent;
2734 int dir_in = hs_ep->dir_in;
2735 int index = hs_ep->index;
2736 unsigned long flags;
2740 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2742 if (ep == &hsotg->eps[0].ep) {
2743 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2747 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
2749 spin_lock_irqsave(&hsotg->lock, flags);
2750 /* terminate all requests with shutdown */
2751 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2754 ctrl = readl(hsotg->regs + epctrl_reg);
2755 ctrl &= ~DxEPCTL_EPEna;
2756 ctrl &= ~DxEPCTL_USBActEp;
2757 ctrl |= DxEPCTL_SNAK;
2759 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2760 writel(ctrl, hsotg->regs + epctrl_reg);
2762 /* disable endpoint interrupts */
2763 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2765 spin_unlock_irqrestore(&hsotg->lock, flags);
2770 * on_list - check request is on the given endpoint
2771 * @ep: The endpoint to check.
2772 * @test: The request to test if it is on the endpoint.
2774 static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2776 struct s3c_hsotg_req *req, *treq;
2778 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2787 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2788 * @ep: The endpoint to dequeue.
2789 * @req: The request to be removed from a queue.
2791 static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2793 struct s3c_hsotg_req *hs_req = our_req(req);
2794 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2795 struct s3c_hsotg *hs = hs_ep->parent;
2796 unsigned long flags;
2798 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2800 spin_lock_irqsave(&hs->lock, flags);
2802 if (!on_list(hs_ep, hs_req)) {
2803 spin_unlock_irqrestore(&hs->lock, flags);
2807 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2808 spin_unlock_irqrestore(&hs->lock, flags);
2814 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2815 * @ep: The endpoint to set halt.
2816 * @value: Set or unset the halt.
2818 static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2820 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2821 struct s3c_hsotg *hs = hs_ep->parent;
2822 int index = hs_ep->index;
2827 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2829 /* write both IN and OUT control registers */
2831 epreg = DIEPCTL(index);
2832 epctl = readl(hs->regs + epreg);
2835 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2836 if (epctl & DxEPCTL_EPEna)
2837 epctl |= DxEPCTL_EPDis;
2839 epctl &= ~DxEPCTL_Stall;
2840 xfertype = epctl & DxEPCTL_EPType_MASK;
2841 if (xfertype == DxEPCTL_EPType_Bulk ||
2842 xfertype == DxEPCTL_EPType_Intterupt)
2843 epctl |= DxEPCTL_SetD0PID;
2846 writel(epctl, hs->regs + epreg);
2848 epreg = DOEPCTL(index);
2849 epctl = readl(hs->regs + epreg);
2852 epctl |= DxEPCTL_Stall;
2854 epctl &= ~DxEPCTL_Stall;
2855 xfertype = epctl & DxEPCTL_EPType_MASK;
2856 if (xfertype == DxEPCTL_EPType_Bulk ||
2857 xfertype == DxEPCTL_EPType_Intterupt)
2858 epctl |= DxEPCTL_SetD0PID;
2861 writel(epctl, hs->regs + epreg);
2863 hs_ep->halted = value;
2869 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2870 * @ep: The endpoint to set halt.
2871 * @value: Set or unset the halt.
2873 static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2875 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2876 struct s3c_hsotg *hs = hs_ep->parent;
2877 unsigned long flags = 0;
2880 spin_lock_irqsave(&hs->lock, flags);
2881 ret = s3c_hsotg_ep_sethalt(ep, value);
2882 spin_unlock_irqrestore(&hs->lock, flags);
2887 static struct usb_ep_ops s3c_hsotg_ep_ops = {
2888 .enable = s3c_hsotg_ep_enable,
2889 .disable = s3c_hsotg_ep_disable,
2890 .alloc_request = s3c_hsotg_ep_alloc_request,
2891 .free_request = s3c_hsotg_ep_free_request,
2892 .queue = s3c_hsotg_ep_queue_lock,
2893 .dequeue = s3c_hsotg_ep_dequeue,
2894 .set_halt = s3c_hsotg_ep_sethalt_lock,
2895 /* note, don't believe we have any call for the fifo routines */
2899 * s3c_hsotg_phy_enable - enable platform phy dev
2900 * @hsotg: The driver state
2902 * A wrapper for platform code responsible for controlling
2903 * low-level USB code
2905 static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2907 struct platform_device *pdev = to_platform_device(hsotg->dev);
2909 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2912 usb_phy_init(hsotg->phy);
2913 else if (hsotg->plat->phy_init)
2914 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2918 * s3c_hsotg_phy_disable - disable platform phy dev
2919 * @hsotg: The driver state
2921 * A wrapper for platform code responsible for controlling
2922 * low-level USB code
2924 static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2926 struct platform_device *pdev = to_platform_device(hsotg->dev);
2929 usb_phy_shutdown(hsotg->phy);
2930 else if (hsotg->plat->phy_exit)
2931 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2935 * s3c_hsotg_init - initalize the usb core
2936 * @hsotg: The driver state
2938 static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2940 /* unmask subset of endpoint interrupts */
2942 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2943 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2944 hsotg->regs + DIEPMSK);
2946 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2947 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2948 hsotg->regs + DOEPMSK);
2950 writel(0, hsotg->regs + DAINTMSK);
2952 /* Be in disconnected state until gadget is registered */
2953 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
2956 /* post global nak until we're ready */
2957 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2958 hsotg->regs + DCTL);
2963 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2964 readl(hsotg->regs + GRXFSIZ),
2965 readl(hsotg->regs + GNPTXFSIZ));
2967 s3c_hsotg_init_fifo(hsotg);
2969 /* set the PLL on, remove the HNP/SRP and set the PHY */
2970 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2971 hsotg->regs + GUSBCFG);
2973 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2974 hsotg->regs + GAHBCFG);
2978 * s3c_hsotg_udc_start - prepare the udc for work
2979 * @gadget: The usb gadget state
2980 * @driver: The usb gadget driver
2982 * Perform initialization to prepare udc device and driver
2985 static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2986 struct usb_gadget_driver *driver)
2988 struct s3c_hsotg *hsotg = to_hsotg(gadget);
2992 pr_err("%s: called with no device\n", __func__);
2997 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3001 if (driver->max_speed < USB_SPEED_FULL)
3002 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
3004 if (!driver->setup) {
3005 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3009 WARN_ON(hsotg->driver);
3011 driver->driver.bus = NULL;
3012 hsotg->driver = driver;
3013 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
3014 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3016 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3019 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3023 hsotg->last_rst = jiffies;
3024 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
3028 hsotg->driver = NULL;
3033 * s3c_hsotg_udc_stop - stop the udc
3034 * @gadget: The usb gadget state
3035 * @driver: The usb gadget driver
3037 * Stop udc hw block and stay tunned for future transmissions
3039 static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
3040 struct usb_gadget_driver *driver)
3042 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3043 unsigned long flags = 0;
3049 /* all endpoints should be shutdown */
3050 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3051 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3053 spin_lock_irqsave(&hsotg->lock, flags);
3055 s3c_hsotg_phy_disable(hsotg);
3058 hsotg->driver = NULL;
3060 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3062 spin_unlock_irqrestore(&hsotg->lock, flags);
3064 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3070 * s3c_hsotg_gadget_getframe - read the frame number
3071 * @gadget: The usb gadget state
3073 * Read the {micro} frame number
3075 static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3077 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3081 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3082 * @gadget: The usb gadget state
3083 * @is_on: Current state of the USB PHY
3085 * Connect/Disconnect the USB PHY pullup
3087 static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3089 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3090 unsigned long flags = 0;
3092 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3094 spin_lock_irqsave(&hsotg->lock, flags);
3096 s3c_hsotg_phy_enable(hsotg);
3097 s3c_hsotg_core_init(hsotg);
3099 s3c_hsotg_disconnect(hsotg);
3100 s3c_hsotg_phy_disable(hsotg);
3103 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3104 spin_unlock_irqrestore(&hsotg->lock, flags);
3109 static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
3110 .get_frame = s3c_hsotg_gadget_getframe,
3111 .udc_start = s3c_hsotg_udc_start,
3112 .udc_stop = s3c_hsotg_udc_stop,
3113 .pullup = s3c_hsotg_pullup,
3117 * s3c_hsotg_initep - initialise a single endpoint
3118 * @hsotg: The device state.
3119 * @hs_ep: The endpoint to be initialised.
3120 * @epnum: The endpoint number
3122 * Initialise the given endpoint (as part of the probe and device state
3123 * creation) to give to the gadget driver. Setup the endpoint name, any
3124 * direction information and other state that may be required.
3126 static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
3127 struct s3c_hsotg_ep *hs_ep,
3135 else if ((epnum % 2) == 0) {
3142 hs_ep->index = epnum;
3144 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3146 INIT_LIST_HEAD(&hs_ep->queue);
3147 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3149 /* add to the list of endpoints known by the gadget driver */
3151 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3153 hs_ep->parent = hsotg;
3154 hs_ep->ep.name = hs_ep->name;
3155 hs_ep->ep.maxpacket = epnum ? 1024 : EP0_MPS_LIMIT;
3156 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3159 * Read the FIFO size for the Periodic TX FIFO, even if we're
3160 * an OUT endpoint, we may as well do this if in future the
3161 * code is changed to make each endpoint's direction changeable.
3164 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3165 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
3168 * if we're using dma, we need to set the next-endpoint pointer
3169 * to be something valid.
3172 if (using_dma(hsotg)) {
3173 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3174 writel(next, hsotg->regs + DIEPCTL(epnum));
3175 writel(next, hsotg->regs + DOEPCTL(epnum));
3180 * s3c_hsotg_hw_cfg - read HW configuration registers
3181 * @param: The device state
3183 * Read the USB core HW configuration registers
3185 static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
3188 /* check hardware configuration */
3190 cfg2 = readl(hsotg->regs + 0x48);
3191 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3193 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3195 cfg4 = readl(hsotg->regs + 0x50);
3196 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3198 dev_info(hsotg->dev, "%s fifos\n",
3199 hsotg->dedicated_fifos ? "dedicated" : "shared");
3203 * s3c_hsotg_dump - dump state of the udc
3204 * @param: The device state
3206 static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3209 struct device *dev = hsotg->dev;
3210 void __iomem *regs = hsotg->regs;
3214 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
3215 readl(regs + DCFG), readl(regs + DCTL),
3216 readl(regs + DIEPMSK));
3218 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
3219 readl(regs + GAHBCFG), readl(regs + 0x44));
3221 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
3222 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
3224 /* show periodic fifo settings */
3226 for (idx = 1; idx <= 15; idx++) {
3227 val = readl(regs + DPTXFSIZn(idx));
3228 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
3229 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3230 val & DPTXFSIZn_DPTxFStAddr_MASK);
3233 for (idx = 0; idx < 15; idx++) {
3235 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
3236 readl(regs + DIEPCTL(idx)),
3237 readl(regs + DIEPTSIZ(idx)),
3238 readl(regs + DIEPDMA(idx)));
3240 val = readl(regs + DOEPCTL(idx));
3242 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
3243 idx, readl(regs + DOEPCTL(idx)),
3244 readl(regs + DOEPTSIZ(idx)),
3245 readl(regs + DOEPDMA(idx)));
3249 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
3250 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
3255 * state_show - debugfs: show overall driver and device state.
3256 * @seq: The seq file to write to.
3257 * @v: Unused parameter.
3259 * This debugfs entry shows the overall state of the hardware and
3260 * some general information about each of the endpoints available
3263 static int state_show(struct seq_file *seq, void *v)
3265 struct s3c_hsotg *hsotg = seq->private;
3266 void __iomem *regs = hsotg->regs;
3269 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3272 readl(regs + DSTS));
3274 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3275 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
3277 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3278 readl(regs + GINTMSK),
3279 readl(regs + GINTSTS));
3281 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3282 readl(regs + DAINTMSK),
3283 readl(regs + DAINT));
3285 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3286 readl(regs + GNPTXSTS),
3287 readl(regs + GRXSTSR));
3289 seq_puts(seq, "\nEndpoint status:\n");
3291 for (idx = 0; idx < 15; idx++) {
3294 in = readl(regs + DIEPCTL(idx));
3295 out = readl(regs + DOEPCTL(idx));
3297 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3300 in = readl(regs + DIEPTSIZ(idx));
3301 out = readl(regs + DOEPTSIZ(idx));
3303 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3306 seq_puts(seq, "\n");
3312 static int state_open(struct inode *inode, struct file *file)
3314 return single_open(file, state_show, inode->i_private);
3317 static const struct file_operations state_fops = {
3318 .owner = THIS_MODULE,
3321 .llseek = seq_lseek,
3322 .release = single_release,
3326 * fifo_show - debugfs: show the fifo information
3327 * @seq: The seq_file to write data to.
3328 * @v: Unused parameter.
3330 * Show the FIFO information for the overall fifo and all the
3331 * periodic transmission FIFOs.
3333 static int fifo_show(struct seq_file *seq, void *v)
3335 struct s3c_hsotg *hsotg = seq->private;
3336 void __iomem *regs = hsotg->regs;
3340 seq_puts(seq, "Non-periodic FIFOs:\n");
3341 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
3343 val = readl(regs + GNPTXFSIZ);
3344 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3345 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3346 val & GNPTXFSIZ_NPTxFStAddr_MASK);
3348 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
3350 for (idx = 1; idx <= 15; idx++) {
3351 val = readl(regs + DPTXFSIZn(idx));
3353 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3354 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3355 val & DPTXFSIZn_DPTxFStAddr_MASK);
3361 static int fifo_open(struct inode *inode, struct file *file)
3363 return single_open(file, fifo_show, inode->i_private);
3366 static const struct file_operations fifo_fops = {
3367 .owner = THIS_MODULE,
3370 .llseek = seq_lseek,
3371 .release = single_release,
3375 static const char *decode_direction(int is_in)
3377 return is_in ? "in" : "out";
3381 * ep_show - debugfs: show the state of an endpoint.
3382 * @seq: The seq_file to write data to.
3383 * @v: Unused parameter.
3385 * This debugfs entry shows the state of the given endpoint (one is
3386 * registered for each available).
3388 static int ep_show(struct seq_file *seq, void *v)
3390 struct s3c_hsotg_ep *ep = seq->private;
3391 struct s3c_hsotg *hsotg = ep->parent;
3392 struct s3c_hsotg_req *req;
3393 void __iomem *regs = hsotg->regs;
3394 int index = ep->index;
3395 int show_limit = 15;
3396 unsigned long flags;
3398 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3399 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3401 /* first show the register state */
3403 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3404 readl(regs + DIEPCTL(index)),
3405 readl(regs + DOEPCTL(index)));
3407 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3408 readl(regs + DIEPDMA(index)),
3409 readl(regs + DOEPDMA(index)));
3411 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3412 readl(regs + DIEPINT(index)),
3413 readl(regs + DOEPINT(index)));
3415 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3416 readl(regs + DIEPTSIZ(index)),
3417 readl(regs + DOEPTSIZ(index)));
3419 seq_puts(seq, "\n");
3420 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3421 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3423 seq_printf(seq, "request list (%p,%p):\n",
3424 ep->queue.next, ep->queue.prev);
3426 spin_lock_irqsave(&hsotg->lock, flags);
3428 list_for_each_entry(req, &ep->queue, queue) {
3429 if (--show_limit < 0) {
3430 seq_puts(seq, "not showing more requests...\n");
3434 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3435 req == ep->req ? '*' : ' ',
3436 req, req->req.length, req->req.buf);
3437 seq_printf(seq, "%d done, res %d\n",
3438 req->req.actual, req->req.status);
3441 spin_unlock_irqrestore(&hsotg->lock, flags);
3446 static int ep_open(struct inode *inode, struct file *file)
3448 return single_open(file, ep_show, inode->i_private);
3451 static const struct file_operations ep_fops = {
3452 .owner = THIS_MODULE,
3455 .llseek = seq_lseek,
3456 .release = single_release,
3460 * s3c_hsotg_create_debug - create debugfs directory and files
3461 * @hsotg: The driver state
3463 * Create the debugfs files to allow the user to get information
3464 * about the state of the system. The directory name is created
3465 * with the same name as the device itself, in case we end up
3466 * with multiple blocks in future systems.
3468 static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3470 struct dentry *root;
3473 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3474 hsotg->debug_root = root;
3476 dev_err(hsotg->dev, "cannot create debug root\n");
3480 /* create general state file */
3482 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3483 hsotg, &state_fops);
3485 if (IS_ERR(hsotg->debug_file))
3486 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3488 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3491 if (IS_ERR(hsotg->debug_fifo))
3492 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3494 /* create one file for each endpoint */
3496 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
3497 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3499 ep->debugfs = debugfs_create_file(ep->name, 0444,
3500 root, ep, &ep_fops);
3502 if (IS_ERR(ep->debugfs))
3503 dev_err(hsotg->dev, "failed to create %s debug file\n",
3509 * s3c_hsotg_delete_debug - cleanup debugfs entries
3510 * @hsotg: The driver state
3512 * Cleanup (remove) the debugfs files for use on module exit.
3514 static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3518 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
3519 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3520 debugfs_remove(ep->debugfs);
3523 debugfs_remove(hsotg->debug_file);
3524 debugfs_remove(hsotg->debug_fifo);
3525 debugfs_remove(hsotg->debug_root);
3529 * s3c_hsotg_probe - probe function for hsotg driver
3530 * @pdev: The platform information for the driver
3533 static int s3c_hsotg_probe(struct platform_device *pdev)
3535 struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
3536 struct usb_phy *phy;
3537 struct device *dev = &pdev->dev;
3538 struct s3c_hsotg_ep *eps;
3539 struct s3c_hsotg *hsotg;
3540 struct resource *res;
3545 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
3547 dev_err(dev, "cannot get memory\n");
3551 phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3553 /* Fallback for pdata */
3554 plat = dev_get_platdata(&pdev->dev);
3556 dev_err(&pdev->dev, "no platform data or transceiver defined\n");
3557 return -EPROBE_DEFER;
3567 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
3568 if (IS_ERR(hsotg->clk)) {
3569 dev_err(dev, "cannot get otg clock\n");
3570 return PTR_ERR(hsotg->clk);
3573 platform_set_drvdata(pdev, hsotg);
3575 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3577 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3578 if (IS_ERR(hsotg->regs)) {
3579 ret = PTR_ERR(hsotg->regs);
3583 ret = platform_get_irq(pdev, 0);
3585 dev_err(dev, "cannot find IRQ\n");
3589 spin_lock_init(&hsotg->lock);
3593 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3594 dev_name(dev), hsotg);
3596 dev_err(dev, "cannot claim IRQ\n");
3600 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3602 hsotg->gadget.max_speed = USB_SPEED_HIGH;
3603 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3604 hsotg->gadget.name = dev_name(dev);
3606 /* reset the system */
3608 clk_prepare_enable(hsotg->clk);
3612 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3613 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3615 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3618 dev_err(dev, "failed to request supplies: %d\n", ret);
3622 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3626 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3630 /* usb phy enable */
3631 s3c_hsotg_phy_enable(hsotg);
3633 s3c_hsotg_corereset(hsotg);
3634 s3c_hsotg_init(hsotg);
3635 s3c_hsotg_hw_cfg(hsotg);
3637 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3639 if (hsotg->num_of_eps == 0) {
3640 dev_err(dev, "wrong number of EPs (zero)\n");
3645 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3648 dev_err(dev, "cannot get memory\n");
3655 /* setup endpoint information */
3657 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3658 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3660 /* allocate EP0 request */
3662 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3664 if (!hsotg->ctrl_req) {
3665 dev_err(dev, "failed to allocate ctrl req\n");
3670 /* initialise the endpoints now the core has been initialised */
3671 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
3672 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3674 /* disable power and clock */
3676 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3679 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3683 s3c_hsotg_phy_disable(hsotg);
3685 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3689 s3c_hsotg_create_debug(hsotg);
3691 s3c_hsotg_dump(hsotg);
3698 s3c_hsotg_phy_disable(hsotg);
3700 clk_disable_unprepare(hsotg->clk);
3706 * s3c_hsotg_remove - remove function for hsotg driver
3707 * @pdev: The platform information for the driver
3709 static int s3c_hsotg_remove(struct platform_device *pdev)
3711 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3713 usb_del_gadget_udc(&hsotg->gadget);
3715 s3c_hsotg_delete_debug(hsotg);
3717 if (hsotg->driver) {
3718 /* should have been done already by driver model core */
3719 usb_gadget_unregister_driver(hsotg->driver);
3722 s3c_hsotg_phy_disable(hsotg);
3723 clk_disable_unprepare(hsotg->clk);
3729 #define s3c_hsotg_suspend NULL
3730 #define s3c_hsotg_resume NULL
3734 static const struct of_device_id s3c_hsotg_of_ids[] = {
3735 { .compatible = "samsung,s3c6400-hsotg", },
3738 MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
3741 static struct platform_driver s3c_hsotg_driver = {
3743 .name = "s3c-hsotg",
3744 .owner = THIS_MODULE,
3745 .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
3747 .probe = s3c_hsotg_probe,
3748 .remove = s3c_hsotg_remove,
3749 .suspend = s3c_hsotg_suspend,
3750 .resume = s3c_hsotg_resume,
3753 module_platform_driver(s3c_hsotg_driver);
3755 MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3756 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3757 MODULE_LICENSE("GPL");
3758 MODULE_ALIAS("platform:s3c-hsotg");