usb: dwc2: gadget: dont warn if endpoint is not enabled
[linux-2.6-block.git] / drivers / usb / dwc2 / gadget.c
CommitLineData
8b9bc460 1/**
dfbc6fa3
AT
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5b7d70c6
BD
4 *
5 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * S3C USB2.0 High-speed / OtG driver
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
8b9bc460 15 */
5b7d70c6
BD
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/debugfs.h>
7ad8096e 24#include <linux/mutex.h>
5b7d70c6
BD
25#include <linux/seq_file.h>
26#include <linux/delay.h>
27#include <linux/io.h>
5a0e3ad6 28#include <linux/slab.h>
e50bf385 29#include <linux/clk.h>
fc9a731e 30#include <linux/regulator/consumer.h>
c50f056c 31#include <linux/of_platform.h>
74084844 32#include <linux/phy/phy.h>
5b7d70c6
BD
33
34#include <linux/usb/ch9.h>
35#include <linux/usb/gadget.h>
b2e587db 36#include <linux/usb/phy.h>
126625e1 37#include <linux/platform_data/s3c-hsotg.h>
5b7d70c6 38
f7c0b143 39#include "core.h"
941fcce4 40#include "hw.h"
5b7d70c6
BD
41
42/* conversion functions */
43static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
44{
45 return container_of(req, struct s3c_hsotg_req, req);
46}
47
48static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
49{
50 return container_of(ep, struct s3c_hsotg_ep, ep);
51}
52
941fcce4 53static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
5b7d70c6 54{
941fcce4 55 return container_of(gadget, struct dwc2_hsotg, gadget);
5b7d70c6
BD
56}
57
58static inline void __orr32(void __iomem *ptr, u32 val)
59{
60 writel(readl(ptr) | val, ptr);
61}
62
63static inline void __bic32(void __iomem *ptr, u32 val)
64{
65 writel(readl(ptr) & ~val, ptr);
66}
67
c6f5c050
MYK
68static inline struct s3c_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
69 u32 ep_index, u32 dir_in)
70{
71 if (dir_in)
72 return hsotg->eps_in[ep_index];
73 else
74 return hsotg->eps_out[ep_index];
75}
76
997f4f81 77/* forward declaration of functions */
941fcce4 78static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
5b7d70c6
BD
79
80/**
81 * using_dma - return the DMA status of the driver.
82 * @hsotg: The driver state.
83 *
84 * Return true if we're using DMA.
85 *
86 * Currently, we have the DMA support code worked into everywhere
87 * that needs it, but the AMBA DMA implementation in the hardware can
88 * only DMA from 32bit aligned addresses. This means that gadgets such
89 * as the CDC Ethernet cannot work as they often pass packets which are
90 * not 32bit aligned.
91 *
92 * Unfortunately the choice to use DMA or not is global to the controller
93 * and seems to be only settable when the controller is being put through
94 * a core reset. This means we either need to fix the gadgets to take
95 * account of DMA alignment, or add bounce buffers (yuerk).
96 *
edd74be8 97 * g_using_dma is set depending on dts flag.
5b7d70c6 98 */
941fcce4 99static inline bool using_dma(struct dwc2_hsotg *hsotg)
5b7d70c6 100{
edd74be8 101 return hsotg->g_using_dma;
5b7d70c6
BD
102}
103
104/**
105 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
106 * @hsotg: The device state
107 * @ints: A bitmask of the interrupts to enable
108 */
941fcce4 109static void s3c_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
5b7d70c6 110{
94cb8fd6 111 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
112 u32 new_gsintmsk;
113
114 new_gsintmsk = gsintmsk | ints;
115
116 if (new_gsintmsk != gsintmsk) {
117 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
94cb8fd6 118 writel(new_gsintmsk, hsotg->regs + GINTMSK);
5b7d70c6
BD
119 }
120}
121
122/**
123 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
124 * @hsotg: The device state
125 * @ints: A bitmask of the interrupts to enable
126 */
941fcce4 127static void s3c_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
5b7d70c6 128{
94cb8fd6 129 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
130 u32 new_gsintmsk;
131
132 new_gsintmsk = gsintmsk & ~ints;
133
134 if (new_gsintmsk != gsintmsk)
94cb8fd6 135 writel(new_gsintmsk, hsotg->regs + GINTMSK);
5b7d70c6
BD
136}
137
138/**
139 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
140 * @hsotg: The device state
141 * @ep: The endpoint index
142 * @dir_in: True if direction is in.
143 * @en: The enable value, true to enable
144 *
145 * Set or clear the mask for an individual endpoint's interrupt
146 * request.
147 */
941fcce4 148static void s3c_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
149 unsigned int ep, unsigned int dir_in,
150 unsigned int en)
151{
152 unsigned long flags;
153 u32 bit = 1 << ep;
154 u32 daint;
155
156 if (!dir_in)
157 bit <<= 16;
158
159 local_irq_save(flags);
94cb8fd6 160 daint = readl(hsotg->regs + DAINTMSK);
5b7d70c6
BD
161 if (en)
162 daint |= bit;
163 else
164 daint &= ~bit;
94cb8fd6 165 writel(daint, hsotg->regs + DAINTMSK);
5b7d70c6
BD
166 local_irq_restore(flags);
167}
168
169/**
170 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
171 * @hsotg: The device instance.
172 */
941fcce4 173static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
5b7d70c6 174{
0f002d20
BD
175 unsigned int ep;
176 unsigned int addr;
1703a6d3 177 int timeout;
0f002d20
BD
178 u32 val;
179
0a176279
GH
180 /* set RX/NPTX FIFO sizes */
181 writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
182 writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
183 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
184 hsotg->regs + GNPTXFSIZ);
0f002d20 185
8b9bc460
LM
186 /*
187 * arange all the rest of the TX FIFOs, as some versions of this
0f002d20
BD
188 * block have overlapping default addresses. This also ensures
189 * that if the settings have been changed, then they are set to
8b9bc460
LM
190 * known values.
191 */
0f002d20
BD
192
193 /* start at the end of the GNPTXFSIZ, rounded up */
0a176279 194 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
0f002d20 195
8b9bc460 196 /*
0a176279 197 * Configure fifos sizes from provided configuration and assign
b203d0a2
RB
198 * them to endpoints dynamically according to maxpacket size value of
199 * given endpoint.
8b9bc460 200 */
0a176279
GH
201 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
202 if (!hsotg->g_tx_fifo_sz[ep])
203 continue;
0f002d20 204 val = addr;
0a176279
GH
205 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
206 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
cff9eb75 207 "insufficient fifo memory");
0a176279 208 addr += hsotg->g_tx_fifo_sz[ep];
0f002d20 209
47a1685f 210 writel(val, hsotg->regs + DPTXFSIZN(ep));
0f002d20 211 }
1703a6d3 212
8b9bc460
LM
213 /*
214 * according to p428 of the design guide, we need to ensure that
215 * all fifos are flushed before continuing
216 */
1703a6d3 217
47a1685f
DN
218 writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
219 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
1703a6d3
BD
220
221 /* wait until the fifos are both flushed */
222 timeout = 100;
223 while (1) {
94cb8fd6 224 val = readl(hsotg->regs + GRSTCTL);
1703a6d3 225
47a1685f 226 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
1703a6d3
BD
227 break;
228
229 if (--timeout == 0) {
230 dev_err(hsotg->dev,
231 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
232 __func__, val);
233 }
234
235 udelay(1);
236 }
237
238 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
5b7d70c6
BD
239}
240
241/**
242 * @ep: USB endpoint to allocate request for.
243 * @flags: Allocation flags
244 *
245 * Allocate a new USB request structure appropriate for the specified endpoint
246 */
0978f8c5
MB
247static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
248 gfp_t flags)
5b7d70c6
BD
249{
250 struct s3c_hsotg_req *req;
251
252 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
253 if (!req)
254 return NULL;
255
256 INIT_LIST_HEAD(&req->queue);
257
5b7d70c6
BD
258 return &req->req;
259}
260
261/**
262 * is_ep_periodic - return true if the endpoint is in periodic mode.
263 * @hs_ep: The endpoint to query.
264 *
265 * Returns true if the endpoint is in periodic mode, meaning it is being
266 * used for an Interrupt or ISO transfer.
267 */
268static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
269{
270 return hs_ep->periodic;
271}
272
273/**
274 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
275 * @hsotg: The device state.
276 * @hs_ep: The endpoint for the request
277 * @hs_req: The request being processed.
278 *
279 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
280 * of a request to ensure the buffer is ready for access by the caller.
8b9bc460 281 */
941fcce4 282static void s3c_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
283 struct s3c_hsotg_ep *hs_ep,
284 struct s3c_hsotg_req *hs_req)
285{
286 struct usb_request *req = &hs_req->req;
5b7d70c6
BD
287
288 /* ignore this if we're not moving any data */
289 if (hs_req->req.length == 0)
290 return;
291
17d966a3 292 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
5b7d70c6
BD
293}
294
295/**
296 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
297 * @hsotg: The controller state.
298 * @hs_ep: The endpoint we're going to write for.
299 * @hs_req: The request to write data for.
300 *
301 * This is called when the TxFIFO has some space in it to hold a new
302 * transmission and we have something to give it. The actual setup of
303 * the data size is done elsewhere, so all we have to do is to actually
304 * write the data.
305 *
306 * The return value is zero if there is more space (or nothing was done)
307 * otherwise -ENOSPC is returned if the FIFO space was used up.
308 *
309 * This routine is only needed for PIO
8b9bc460 310 */
941fcce4 311static int s3c_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
312 struct s3c_hsotg_ep *hs_ep,
313 struct s3c_hsotg_req *hs_req)
314{
315 bool periodic = is_ep_periodic(hs_ep);
94cb8fd6 316 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
5b7d70c6
BD
317 int buf_pos = hs_req->req.actual;
318 int to_write = hs_ep->size_loaded;
319 void *data;
320 int can_write;
321 int pkt_round;
4fca54aa 322 int max_transfer;
5b7d70c6
BD
323
324 to_write -= (buf_pos - hs_ep->last_load);
325
326 /* if there's nothing to write, get out early */
327 if (to_write == 0)
328 return 0;
329
10aebc77 330 if (periodic && !hsotg->dedicated_fifos) {
94cb8fd6 331 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
5b7d70c6
BD
332 int size_left;
333 int size_done;
334
8b9bc460
LM
335 /*
336 * work out how much data was loaded so we can calculate
337 * how much data is left in the fifo.
338 */
5b7d70c6 339
47a1685f 340 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6 341
8b9bc460
LM
342 /*
343 * if shared fifo, we cannot write anything until the
e7a9ff54
BD
344 * previous data has been completely sent.
345 */
346 if (hs_ep->fifo_load != 0) {
47a1685f 347 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
e7a9ff54
BD
348 return -ENOSPC;
349 }
350
5b7d70c6
BD
351 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
352 __func__, size_left,
353 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
354
355 /* how much of the data has moved */
356 size_done = hs_ep->size_loaded - size_left;
357
358 /* how much data is left in the fifo */
359 can_write = hs_ep->fifo_load - size_done;
360 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
361 __func__, can_write);
362
363 can_write = hs_ep->fifo_size - can_write;
364 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
365 __func__, can_write);
366
367 if (can_write <= 0) {
47a1685f 368 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
5b7d70c6
BD
369 return -ENOSPC;
370 }
10aebc77 371 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
94cb8fd6 372 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
10aebc77
BD
373
374 can_write &= 0xffff;
375 can_write *= 4;
5b7d70c6 376 } else {
47a1685f 377 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
5b7d70c6
BD
378 dev_dbg(hsotg->dev,
379 "%s: no queue slots available (0x%08x)\n",
380 __func__, gnptxsts);
381
47a1685f 382 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
5b7d70c6
BD
383 return -ENOSPC;
384 }
385
47a1685f 386 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
679f9b7c 387 can_write *= 4; /* fifo size is in 32bit quantities. */
5b7d70c6
BD
388 }
389
4fca54aa
RB
390 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
391
392 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
393 __func__, gnptxsts, can_write, to_write, max_transfer);
5b7d70c6 394
8b9bc460
LM
395 /*
396 * limit to 512 bytes of data, it seems at least on the non-periodic
5b7d70c6
BD
397 * FIFO, requests of >512 cause the endpoint to get stuck with a
398 * fragment of the end of the transfer in it.
399 */
811f3303 400 if (can_write > 512 && !periodic)
5b7d70c6
BD
401 can_write = 512;
402
8b9bc460
LM
403 /*
404 * limit the write to one max-packet size worth of data, but allow
03e10e5a 405 * the transfer to return that it did not run out of fifo space
8b9bc460
LM
406 * doing it.
407 */
4fca54aa
RB
408 if (to_write > max_transfer) {
409 to_write = max_transfer;
03e10e5a 410
5cb2ff0c
RB
411 /* it's needed only when we do not use dedicated fifos */
412 if (!hsotg->dedicated_fifos)
413 s3c_hsotg_en_gsint(hsotg,
47a1685f
DN
414 periodic ? GINTSTS_PTXFEMP :
415 GINTSTS_NPTXFEMP);
03e10e5a
BD
416 }
417
5b7d70c6
BD
418 /* see if we can write data */
419
420 if (to_write > can_write) {
421 to_write = can_write;
4fca54aa 422 pkt_round = to_write % max_transfer;
5b7d70c6 423
8b9bc460
LM
424 /*
425 * Round the write down to an
5b7d70c6
BD
426 * exact number of packets.
427 *
428 * Note, we do not currently check to see if we can ever
429 * write a full packet or not to the FIFO.
430 */
431
432 if (pkt_round)
433 to_write -= pkt_round;
434
8b9bc460
LM
435 /*
436 * enable correct FIFO interrupt to alert us when there
437 * is more room left.
438 */
5b7d70c6 439
5cb2ff0c
RB
440 /* it's needed only when we do not use dedicated fifos */
441 if (!hsotg->dedicated_fifos)
442 s3c_hsotg_en_gsint(hsotg,
47a1685f
DN
443 periodic ? GINTSTS_PTXFEMP :
444 GINTSTS_NPTXFEMP);
5b7d70c6
BD
445 }
446
447 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
448 to_write, hs_req->req.length, can_write, buf_pos);
449
450 if (to_write <= 0)
451 return -ENOSPC;
452
453 hs_req->req.actual = buf_pos + to_write;
454 hs_ep->total_data += to_write;
455
456 if (periodic)
457 hs_ep->fifo_load += to_write;
458
459 to_write = DIV_ROUND_UP(to_write, 4);
460 data = hs_req->req.buf + buf_pos;
461
1a7ed5be 462 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
5b7d70c6
BD
463
464 return (to_write >= can_write) ? -ENOSPC : 0;
465}
466
467/**
468 * get_ep_limit - get the maximum data legnth for this endpoint
469 * @hs_ep: The endpoint
470 *
471 * Return the maximum data that can be queued in one go on a given endpoint
472 * so that transfers that are too long can be split.
473 */
474static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
475{
476 int index = hs_ep->index;
477 unsigned maxsize;
478 unsigned maxpkt;
479
480 if (index != 0) {
47a1685f
DN
481 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
482 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
5b7d70c6 483 } else {
b05ca580 484 maxsize = 64+64;
66e5c643 485 if (hs_ep->dir_in)
47a1685f 486 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
66e5c643 487 else
5b7d70c6 488 maxpkt = 2;
5b7d70c6
BD
489 }
490
491 /* we made the constant loading easier above by using +1 */
492 maxpkt--;
493 maxsize--;
494
8b9bc460
LM
495 /*
496 * constrain by packet count if maxpkts*pktsize is greater
497 * than the length register size.
498 */
5b7d70c6
BD
499
500 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
501 maxsize = maxpkt * hs_ep->ep.maxpacket;
502
503 return maxsize;
504}
505
506/**
507 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
508 * @hsotg: The controller state.
509 * @hs_ep: The endpoint to process a request for
510 * @hs_req: The request to start.
511 * @continuing: True if we are doing more for the current request.
512 *
513 * Start the given request running by setting the endpoint registers
514 * appropriately, and writing any data to the FIFOs.
515 */
941fcce4 516static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
517 struct s3c_hsotg_ep *hs_ep,
518 struct s3c_hsotg_req *hs_req,
519 bool continuing)
520{
521 struct usb_request *ureq = &hs_req->req;
522 int index = hs_ep->index;
523 int dir_in = hs_ep->dir_in;
524 u32 epctrl_reg;
525 u32 epsize_reg;
526 u32 epsize;
527 u32 ctrl;
528 unsigned length;
529 unsigned packets;
530 unsigned maxreq;
531
532 if (index != 0) {
533 if (hs_ep->req && !continuing) {
534 dev_err(hsotg->dev, "%s: active request\n", __func__);
535 WARN_ON(1);
536 return;
537 } else if (hs_ep->req != hs_req && continuing) {
538 dev_err(hsotg->dev,
539 "%s: continue different req\n", __func__);
540 WARN_ON(1);
541 return;
542 }
543 }
544
94cb8fd6
LM
545 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
546 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
5b7d70c6
BD
547
548 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
549 __func__, readl(hsotg->regs + epctrl_reg), index,
550 hs_ep->dir_in ? "in" : "out");
551
9c39ddc6
AT
552 /* If endpoint is stalled, we will restart request later */
553 ctrl = readl(hsotg->regs + epctrl_reg);
554
47a1685f 555 if (ctrl & DXEPCTL_STALL) {
9c39ddc6
AT
556 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
557 return;
558 }
559
5b7d70c6 560 length = ureq->length - ureq->actual;
71225bee
LM
561 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
562 ureq->length, ureq->actual);
5b7d70c6
BD
563 if (0)
564 dev_dbg(hsotg->dev,
0cc4cf6f 565 "REQ buf %p len %d dma %pad noi=%d zp=%d snok=%d\n",
8b3bc14f 566 ureq->buf, length, &ureq->dma,
5b7d70c6
BD
567 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
568
569 maxreq = get_ep_limit(hs_ep);
570 if (length > maxreq) {
571 int round = maxreq % hs_ep->ep.maxpacket;
572
573 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
574 __func__, length, maxreq, round);
575
576 /* round down to multiple of packets */
577 if (round)
578 maxreq -= round;
579
580 length = maxreq;
581 }
582
583 if (length)
584 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
585 else
586 packets = 1; /* send one packet if length is zero. */
587
4fca54aa
RB
588 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
589 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
590 return;
591 }
592
5b7d70c6 593 if (dir_in && index != 0)
4fca54aa 594 if (hs_ep->isochronous)
47a1685f 595 epsize = DXEPTSIZ_MC(packets);
4fca54aa 596 else
47a1685f 597 epsize = DXEPTSIZ_MC(1);
5b7d70c6
BD
598 else
599 epsize = 0;
600
f71b5e25
MYK
601 /*
602 * zero length packet should be programmed on its own and should not
603 * be counted in DIEPTSIZ.PktCnt with other packets.
604 */
605 if (dir_in && ureq->zero && !continuing) {
606 /* Test if zlp is actually required. */
607 if ((ureq->length >= hs_ep->ep.maxpacket) &&
608 !(ureq->length % hs_ep->ep.maxpacket))
609 hs_ep->sent_zlp = 1;
5b7d70c6
BD
610 }
611
47a1685f
DN
612 epsize |= DXEPTSIZ_PKTCNT(packets);
613 epsize |= DXEPTSIZ_XFERSIZE(length);
5b7d70c6
BD
614
615 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
616 __func__, packets, length, ureq->length, epsize, epsize_reg);
617
618 /* store the request as the current one we're doing */
619 hs_ep->req = hs_req;
620
621 /* write size / packets */
622 writel(epsize, hsotg->regs + epsize_reg);
623
db1d8ba3 624 if (using_dma(hsotg) && !continuing) {
5b7d70c6
BD
625 unsigned int dma_reg;
626
8b9bc460
LM
627 /*
628 * write DMA address to control register, buffer already
629 * synced by s3c_hsotg_ep_queue().
630 */
5b7d70c6 631
94cb8fd6 632 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
5b7d70c6
BD
633 writel(ureq->dma, hsotg->regs + dma_reg);
634
0cc4cf6f 635 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
8b3bc14f 636 __func__, &ureq->dma, dma_reg);
5b7d70c6
BD
637 }
638
47a1685f
DN
639 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
640 ctrl |= DXEPCTL_USBACTEP;
71225bee 641
fe0b94ab 642 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
71225bee
LM
643
644 /* For Setup request do not clear NAK */
fe0b94ab 645 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
47a1685f 646 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
71225bee 647
5b7d70c6
BD
648 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
649 writel(ctrl, hsotg->regs + epctrl_reg);
650
8b9bc460
LM
651 /*
652 * set these, it seems that DMA support increments past the end
5b7d70c6 653 * of the packet buffer so we need to calculate the length from
8b9bc460
LM
654 * this information.
655 */
5b7d70c6
BD
656 hs_ep->size_loaded = length;
657 hs_ep->last_load = ureq->actual;
658
659 if (dir_in && !using_dma(hsotg)) {
660 /* set these anyway, we may need them for non-periodic in */
661 hs_ep->fifo_load = 0;
662
663 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
664 }
665
8b9bc460
LM
666 /*
667 * clear the INTknTXFEmpMsk when we start request, more as a aide
668 * to debugging to see what is going on.
669 */
5b7d70c6 670 if (dir_in)
47a1685f 671 writel(DIEPMSK_INTKNTXFEMPMSK,
94cb8fd6 672 hsotg->regs + DIEPINT(index));
5b7d70c6 673
8b9bc460
LM
674 /*
675 * Note, trying to clear the NAK here causes problems with transmit
676 * on the S3C6400 ending up with the TXFIFO becoming full.
677 */
5b7d70c6
BD
678
679 /* check ep is enabled */
47a1685f 680 if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
1a0ed863 681 dev_dbg(hsotg->dev,
47a1685f 682 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
5b7d70c6
BD
683 index, readl(hsotg->regs + epctrl_reg));
684
47a1685f 685 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
5b7d70c6 686 __func__, readl(hsotg->regs + epctrl_reg));
afcf4169
RB
687
688 /* enable ep interrupts */
689 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
5b7d70c6
BD
690}
691
692/**
693 * s3c_hsotg_map_dma - map the DMA memory being used for the request
694 * @hsotg: The device state.
695 * @hs_ep: The endpoint the request is on.
696 * @req: The request being processed.
697 *
698 * We've been asked to queue a request, so ensure that the memory buffer
699 * is correctly setup for DMA. If we've been passed an extant DMA address
700 * then ensure the buffer has been synced to memory. If our buffer has no
701 * DMA memory, then we map the memory and mark our request to allow us to
702 * cleanup on completion.
8b9bc460 703 */
941fcce4 704static int s3c_hsotg_map_dma(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
705 struct s3c_hsotg_ep *hs_ep,
706 struct usb_request *req)
707{
5b7d70c6 708 struct s3c_hsotg_req *hs_req = our_req(req);
e58ebcd1 709 int ret;
5b7d70c6
BD
710
711 /* if the length is zero, ignore the DMA data */
712 if (hs_req->req.length == 0)
713 return 0;
714
e58ebcd1
FB
715 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
716 if (ret)
717 goto dma_error;
5b7d70c6
BD
718
719 return 0;
720
721dma_error:
722 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
723 __func__, req->buf, req->length);
724
725 return -EIO;
726}
727
728static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
729 gfp_t gfp_flags)
730{
731 struct s3c_hsotg_req *hs_req = our_req(req);
732 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 733 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6
BD
734 bool first;
735
736 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
737 ep->name, req, req->length, req->buf, req->no_interrupt,
738 req->zero, req->short_not_ok);
739
740 /* initialise status of the request */
741 INIT_LIST_HEAD(&hs_req->queue);
742 req->actual = 0;
743 req->status = -EINPROGRESS;
744
745 /* if we're using DMA, sync the buffers as necessary */
746 if (using_dma(hs)) {
747 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
748 if (ret)
749 return ret;
750 }
751
5b7d70c6
BD
752 first = list_empty(&hs_ep->queue);
753 list_add_tail(&hs_req->queue, &hs_ep->queue);
754
755 if (first)
756 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
757
5b7d70c6
BD
758 return 0;
759}
760
5ad1d316
LM
761static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
762 gfp_t gfp_flags)
763{
764 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 765 struct dwc2_hsotg *hs = hs_ep->parent;
5ad1d316
LM
766 unsigned long flags = 0;
767 int ret = 0;
768
769 spin_lock_irqsave(&hs->lock, flags);
770 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
771 spin_unlock_irqrestore(&hs->lock, flags);
772
773 return ret;
774}
775
5b7d70c6
BD
776static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
777 struct usb_request *req)
778{
779 struct s3c_hsotg_req *hs_req = our_req(req);
780
781 kfree(hs_req);
782}
783
784/**
785 * s3c_hsotg_complete_oursetup - setup completion callback
786 * @ep: The endpoint the request was on.
787 * @req: The request completed.
788 *
789 * Called on completion of any requests the driver itself
790 * submitted that need cleaning up.
791 */
792static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
793 struct usb_request *req)
794{
795 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 796 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
797
798 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
799
800 s3c_hsotg_ep_free_request(ep, req);
801}
802
803/**
804 * ep_from_windex - convert control wIndex value to endpoint
805 * @hsotg: The driver state.
806 * @windex: The control request wIndex field (in host order).
807 *
808 * Convert the given wIndex into a pointer to an driver endpoint
809 * structure, or return NULL if it is not a valid endpoint.
8b9bc460 810 */
941fcce4 811static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
812 u32 windex)
813{
c6f5c050 814 struct s3c_hsotg_ep *ep;
5b7d70c6
BD
815 int dir = (windex & USB_DIR_IN) ? 1 : 0;
816 int idx = windex & 0x7F;
817
818 if (windex >= 0x100)
819 return NULL;
820
b3f489b2 821 if (idx > hsotg->num_of_eps)
5b7d70c6
BD
822 return NULL;
823
c6f5c050
MYK
824 ep = index_to_ep(hsotg, idx, dir);
825
5b7d70c6
BD
826 if (idx && ep->dir_in != dir)
827 return NULL;
828
829 return ep;
830}
831
832/**
833 * s3c_hsotg_send_reply - send reply to control request
834 * @hsotg: The device state
835 * @ep: Endpoint 0
836 * @buff: Buffer for request
837 * @length: Length of reply.
838 *
839 * Create a request and queue it on the given endpoint. This is useful as
840 * an internal method of sending replies to certain control requests, etc.
841 */
941fcce4 842static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
843 struct s3c_hsotg_ep *ep,
844 void *buff,
845 int length)
846{
847 struct usb_request *req;
848 int ret;
849
850 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
851
852 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
853 hsotg->ep0_reply = req;
854 if (!req) {
855 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
856 return -ENOMEM;
857 }
858
859 req->buf = hsotg->ep0_buff;
860 req->length = length;
f71b5e25
MYK
861 /*
862 * zero flag is for sending zlp in DATA IN stage. It has no impact on
863 * STATUS stage.
864 */
865 req->zero = 0;
5b7d70c6
BD
866 req->complete = s3c_hsotg_complete_oursetup;
867
868 if (length)
869 memcpy(req->buf, buff, length);
5b7d70c6
BD
870
871 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
872 if (ret) {
873 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
874 return ret;
875 }
876
877 return 0;
878}
879
880/**
881 * s3c_hsotg_process_req_status - process request GET_STATUS
882 * @hsotg: The device state
883 * @ctrl: USB control request
884 */
941fcce4 885static int s3c_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
886 struct usb_ctrlrequest *ctrl)
887{
c6f5c050 888 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
5b7d70c6
BD
889 struct s3c_hsotg_ep *ep;
890 __le16 reply;
891 int ret;
892
893 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
894
895 if (!ep0->dir_in) {
896 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
897 return -EINVAL;
898 }
899
900 switch (ctrl->bRequestType & USB_RECIP_MASK) {
901 case USB_RECIP_DEVICE:
902 reply = cpu_to_le16(0); /* bit 0 => self powered,
903 * bit 1 => remote wakeup */
904 break;
905
906 case USB_RECIP_INTERFACE:
907 /* currently, the data result should be zero */
908 reply = cpu_to_le16(0);
909 break;
910
911 case USB_RECIP_ENDPOINT:
912 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
913 if (!ep)
914 return -ENOENT;
915
916 reply = cpu_to_le16(ep->halted ? 1 : 0);
917 break;
918
919 default:
920 return 0;
921 }
922
923 if (le16_to_cpu(ctrl->wLength) != 2)
924 return -EINVAL;
925
926 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
927 if (ret) {
928 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
929 return ret;
930 }
931
932 return 1;
933}
934
935static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
936
9c39ddc6
AT
937/**
938 * get_ep_head - return the first request on the endpoint
939 * @hs_ep: The controller endpoint to get
940 *
941 * Get the first request on the endpoint.
942 */
943static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
944{
945 if (list_empty(&hs_ep->queue))
946 return NULL;
947
948 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
949}
950
5b7d70c6
BD
951/**
952 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
953 * @hsotg: The device state
954 * @ctrl: USB control request
955 */
941fcce4 956static int s3c_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
957 struct usb_ctrlrequest *ctrl)
958{
c6f5c050 959 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
9c39ddc6
AT
960 struct s3c_hsotg_req *hs_req;
961 bool restart;
5b7d70c6
BD
962 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
963 struct s3c_hsotg_ep *ep;
26ab3d0c 964 int ret;
bd9ef7bf 965 bool halted;
5b7d70c6
BD
966
967 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
968 __func__, set ? "SET" : "CLEAR");
969
970 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
971 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
972 if (!ep) {
973 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
974 __func__, le16_to_cpu(ctrl->wIndex));
975 return -ENOENT;
976 }
977
978 switch (le16_to_cpu(ctrl->wValue)) {
979 case USB_ENDPOINT_HALT:
bd9ef7bf
RB
980 halted = ep->halted;
981
5b7d70c6 982 s3c_hsotg_ep_sethalt(&ep->ep, set);
26ab3d0c
AT
983
984 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
985 if (ret) {
986 dev_err(hsotg->dev,
987 "%s: failed to send reply\n", __func__);
988 return ret;
989 }
9c39ddc6 990
bd9ef7bf
RB
991 /*
992 * we have to complete all requests for ep if it was
993 * halted, and the halt was cleared by CLEAR_FEATURE
994 */
995
996 if (!set && halted) {
9c39ddc6
AT
997 /*
998 * If we have request in progress,
999 * then complete it
1000 */
1001 if (ep->req) {
1002 hs_req = ep->req;
1003 ep->req = NULL;
1004 list_del_init(&hs_req->queue);
304f7e5e
MS
1005 usb_gadget_giveback_request(&ep->ep,
1006 &hs_req->req);
9c39ddc6
AT
1007 }
1008
1009 /* If we have pending request, then start it */
1010 restart = !list_empty(&ep->queue);
1011 if (restart) {
1012 hs_req = get_ep_head(ep);
1013 s3c_hsotg_start_req(hsotg, ep,
1014 hs_req, false);
1015 }
1016 }
1017
5b7d70c6
BD
1018 break;
1019
1020 default:
1021 return -ENOENT;
1022 }
1023 } else
1024 return -ENOENT; /* currently only deal with endpoint */
1025
1026 return 1;
1027}
1028
941fcce4 1029static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
ab93e014 1030
c9f721b2
RB
1031/**
1032 * s3c_hsotg_stall_ep0 - stall ep0
1033 * @hsotg: The device state
1034 *
1035 * Set stall for ep0 as response for setup request.
1036 */
941fcce4 1037static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
e9ebe7c3 1038{
c6f5c050 1039 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
c9f721b2
RB
1040 u32 reg;
1041 u32 ctrl;
1042
1043 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1044 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1045
1046 /*
1047 * DxEPCTL_Stall will be cleared by EP once it has
1048 * taken effect, so no need to clear later.
1049 */
1050
1051 ctrl = readl(hsotg->regs + reg);
47a1685f
DN
1052 ctrl |= DXEPCTL_STALL;
1053 ctrl |= DXEPCTL_CNAK;
c9f721b2
RB
1054 writel(ctrl, hsotg->regs + reg);
1055
1056 dev_dbg(hsotg->dev,
47a1685f 1057 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
c9f721b2
RB
1058 ctrl, reg, readl(hsotg->regs + reg));
1059
1060 /*
1061 * complete won't be called, so we enqueue
1062 * setup request here
1063 */
1064 s3c_hsotg_enqueue_setup(hsotg);
1065}
1066
5b7d70c6
BD
1067/**
1068 * s3c_hsotg_process_control - process a control request
1069 * @hsotg: The device state
1070 * @ctrl: The control request received
1071 *
1072 * The controller has received the SETUP phase of a control request, and
1073 * needs to work out what to do next (and whether to pass it on to the
1074 * gadget driver).
1075 */
941fcce4 1076static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1077 struct usb_ctrlrequest *ctrl)
1078{
c6f5c050 1079 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
5b7d70c6
BD
1080 int ret = 0;
1081 u32 dcfg;
1082
5b7d70c6
BD
1083 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1084 ctrl->bRequest, ctrl->bRequestType,
1085 ctrl->wValue, ctrl->wLength);
1086
fe0b94ab
MYK
1087 if (ctrl->wLength == 0) {
1088 ep0->dir_in = 1;
1089 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1090 } else if (ctrl->bRequestType & USB_DIR_IN) {
5b7d70c6 1091 ep0->dir_in = 1;
fe0b94ab
MYK
1092 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1093 } else {
1094 ep0->dir_in = 0;
1095 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1096 }
5b7d70c6
BD
1097
1098 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1099 switch (ctrl->bRequest) {
1100 case USB_REQ_SET_ADDRESS:
94cb8fd6 1101 dcfg = readl(hsotg->regs + DCFG);
47a1685f 1102 dcfg &= ~DCFG_DEVADDR_MASK;
d5dbd3f7
PZ
1103 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1104 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
94cb8fd6 1105 writel(dcfg, hsotg->regs + DCFG);
5b7d70c6
BD
1106
1107 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1108
1109 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1110 return;
1111
1112 case USB_REQ_GET_STATUS:
1113 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1114 break;
1115
1116 case USB_REQ_CLEAR_FEATURE:
1117 case USB_REQ_SET_FEATURE:
1118 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1119 break;
1120 }
1121 }
1122
1123 /* as a fallback, try delivering it to the driver to deal with */
1124
1125 if (ret == 0 && hsotg->driver) {
93f599f2 1126 spin_unlock(&hsotg->lock);
5b7d70c6 1127 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
93f599f2 1128 spin_lock(&hsotg->lock);
5b7d70c6
BD
1129 if (ret < 0)
1130 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1131 }
1132
8b9bc460
LM
1133 /*
1134 * the request is either unhandlable, or is not formatted correctly
5b7d70c6
BD
1135 * so respond with a STALL for the status stage to indicate failure.
1136 */
1137
c9f721b2
RB
1138 if (ret < 0)
1139 s3c_hsotg_stall_ep0(hsotg);
5b7d70c6
BD
1140}
1141
5b7d70c6
BD
1142/**
1143 * s3c_hsotg_complete_setup - completion of a setup transfer
1144 * @ep: The endpoint the request was on.
1145 * @req: The request completed.
1146 *
1147 * Called on completion of any requests the driver itself submitted for
1148 * EP0 setup packets
1149 */
1150static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1151 struct usb_request *req)
1152{
1153 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 1154 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
1155
1156 if (req->status < 0) {
1157 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1158 return;
1159 }
1160
93f599f2 1161 spin_lock(&hsotg->lock);
5b7d70c6
BD
1162 if (req->actual == 0)
1163 s3c_hsotg_enqueue_setup(hsotg);
1164 else
1165 s3c_hsotg_process_control(hsotg, req->buf);
93f599f2 1166 spin_unlock(&hsotg->lock);
5b7d70c6
BD
1167}
1168
1169/**
1170 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1171 * @hsotg: The device state.
1172 *
1173 * Enqueue a request on EP0 if necessary to received any SETUP packets
1174 * received from the host.
1175 */
941fcce4 1176static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
1177{
1178 struct usb_request *req = hsotg->ctrl_req;
1179 struct s3c_hsotg_req *hs_req = our_req(req);
1180 int ret;
1181
1182 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1183
1184 req->zero = 0;
1185 req->length = 8;
1186 req->buf = hsotg->ctrl_buff;
1187 req->complete = s3c_hsotg_complete_setup;
1188
1189 if (!list_empty(&hs_req->queue)) {
1190 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1191 return;
1192 }
1193
c6f5c050 1194 hsotg->eps_out[0]->dir_in = 0;
fe0b94ab
MYK
1195 hsotg->eps_out[0]->sent_zlp = 0;
1196 hsotg->ep0_state = DWC2_EP0_SETUP;
5b7d70c6 1197
c6f5c050 1198 ret = s3c_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
5b7d70c6
BD
1199 if (ret < 0) {
1200 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
8b9bc460
LM
1201 /*
1202 * Don't think there's much we can do other than watch the
1203 * driver fail.
1204 */
5b7d70c6
BD
1205 }
1206}
1207
fe0b94ab
MYK
1208static void s3c_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1209 struct s3c_hsotg_ep *hs_ep)
1210{
1211 u32 ctrl;
1212 u8 index = hs_ep->index;
1213 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1214 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1215
1216 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", index);
1217
1218 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1219 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1220 epsiz_reg);
1221
1222 ctrl = readl(hsotg->regs + epctl_reg);
1223 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1224 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1225 ctrl |= DXEPCTL_USBACTEP;
1226 writel(ctrl, hsotg->regs + epctl_reg);
1227}
1228
5b7d70c6
BD
1229/**
1230 * s3c_hsotg_complete_request - complete a request given to us
1231 * @hsotg: The device state.
1232 * @hs_ep: The endpoint the request was on.
1233 * @hs_req: The request to complete.
1234 * @result: The result code (0 => Ok, otherwise errno)
1235 *
1236 * The given request has finished, so call the necessary completion
1237 * if it has one and then look to see if we can start a new request
1238 * on the endpoint.
1239 *
1240 * Note, expects the ep to already be locked as appropriate.
8b9bc460 1241 */
941fcce4 1242static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1243 struct s3c_hsotg_ep *hs_ep,
1244 struct s3c_hsotg_req *hs_req,
1245 int result)
1246{
1247 bool restart;
1248
1249 if (!hs_req) {
1250 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1251 return;
1252 }
1253
1254 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1255 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1256
8b9bc460
LM
1257 /*
1258 * only replace the status if we've not already set an error
1259 * from a previous transaction
1260 */
5b7d70c6
BD
1261
1262 if (hs_req->req.status == -EINPROGRESS)
1263 hs_req->req.status = result;
1264
1265 hs_ep->req = NULL;
1266 list_del_init(&hs_req->queue);
1267
1268 if (using_dma(hsotg))
1269 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1270
8b9bc460
LM
1271 /*
1272 * call the complete request with the locks off, just in case the
1273 * request tries to queue more work for this endpoint.
1274 */
5b7d70c6
BD
1275
1276 if (hs_req->req.complete) {
22258f49 1277 spin_unlock(&hsotg->lock);
304f7e5e 1278 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
22258f49 1279 spin_lock(&hsotg->lock);
5b7d70c6
BD
1280 }
1281
8b9bc460
LM
1282 /*
1283 * Look to see if there is anything else to do. Note, the completion
5b7d70c6 1284 * of the previous request may have caused a new request to be started
8b9bc460
LM
1285 * so be careful when doing this.
1286 */
5b7d70c6
BD
1287
1288 if (!hs_ep->req && result >= 0) {
1289 restart = !list_empty(&hs_ep->queue);
1290 if (restart) {
1291 hs_req = get_ep_head(hs_ep);
1292 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1293 }
1294 }
1295}
1296
5b7d70c6
BD
1297/**
1298 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1299 * @hsotg: The device state.
1300 * @ep_idx: The endpoint index for the data
1301 * @size: The size of data in the fifo, in bytes
1302 *
1303 * The FIFO status shows there is data to read from the FIFO for a given
1304 * endpoint, so sort out whether we need to read the data into a request
1305 * that has been made for that endpoint.
1306 */
941fcce4 1307static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
5b7d70c6 1308{
c6f5c050 1309 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
5b7d70c6 1310 struct s3c_hsotg_req *hs_req = hs_ep->req;
94cb8fd6 1311 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
5b7d70c6
BD
1312 int to_read;
1313 int max_req;
1314 int read_ptr;
1315
22258f49 1316
5b7d70c6 1317 if (!hs_req) {
94cb8fd6 1318 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
5b7d70c6
BD
1319 int ptr;
1320
6b448af4 1321 dev_dbg(hsotg->dev,
47a1685f 1322 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
5b7d70c6
BD
1323 __func__, size, ep_idx, epctl);
1324
1325 /* dump the data from the FIFO, we've nothing we can do */
1326 for (ptr = 0; ptr < size; ptr += 4)
1327 (void)readl(fifo);
1328
1329 return;
1330 }
1331
5b7d70c6
BD
1332 to_read = size;
1333 read_ptr = hs_req->req.actual;
1334 max_req = hs_req->req.length - read_ptr;
1335
a33e7136
BD
1336 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1337 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1338
5b7d70c6 1339 if (to_read > max_req) {
8b9bc460
LM
1340 /*
1341 * more data appeared than we where willing
5b7d70c6
BD
1342 * to deal with in this request.
1343 */
1344
1345 /* currently we don't deal this */
1346 WARN_ON_ONCE(1);
1347 }
1348
5b7d70c6
BD
1349 hs_ep->total_data += to_read;
1350 hs_req->req.actual += to_read;
1351 to_read = DIV_ROUND_UP(to_read, 4);
1352
8b9bc460
LM
1353 /*
1354 * note, we might over-write the buffer end by 3 bytes depending on
1355 * alignment of the data.
1356 */
1a7ed5be 1357 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
5b7d70c6
BD
1358}
1359
1360/**
fe0b94ab 1361 * s3c_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
5b7d70c6 1362 * @hsotg: The device instance
fe0b94ab 1363 * @dir_in: If IN zlp
5b7d70c6
BD
1364 *
1365 * Generate a zero-length IN packet request for terminating a SETUP
1366 * transaction.
1367 *
1368 * Note, since we don't write any data to the TxFIFO, then it is
25985edc 1369 * currently believed that we do not need to wait for any space in
5b7d70c6
BD
1370 * the TxFIFO.
1371 */
fe0b94ab 1372static void s3c_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
5b7d70c6 1373{
c6f5c050 1374 /* eps_out[0] is used in both directions */
fe0b94ab
MYK
1375 hsotg->eps_out[0]->dir_in = dir_in;
1376 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
5b7d70c6 1377
fe0b94ab 1378 s3c_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
5b7d70c6
BD
1379}
1380
1381/**
1382 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1383 * @hsotg: The device instance
1384 * @epnum: The endpoint received from
5b7d70c6
BD
1385 *
1386 * The RXFIFO has delivered an OutDone event, which means that the data
1387 * transfer for an OUT endpoint has been completed, either by a short
1388 * packet or by the finish of a transfer.
8b9bc460 1389 */
fe0b94ab 1390static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
5b7d70c6 1391{
94cb8fd6 1392 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
c6f5c050 1393 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
5b7d70c6
BD
1394 struct s3c_hsotg_req *hs_req = hs_ep->req;
1395 struct usb_request *req = &hs_req->req;
47a1685f 1396 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6
BD
1397 int result = 0;
1398
1399 if (!hs_req) {
1400 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1401 return;
1402 }
1403
fe0b94ab
MYK
1404 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1405 dev_dbg(hsotg->dev, "zlp packet received\n");
1406 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1407 s3c_hsotg_enqueue_setup(hsotg);
1408 return;
1409 }
1410
5b7d70c6 1411 if (using_dma(hsotg)) {
5b7d70c6 1412 unsigned size_done;
5b7d70c6 1413
8b9bc460
LM
1414 /*
1415 * Calculate the size of the transfer by checking how much
5b7d70c6
BD
1416 * is left in the endpoint size register and then working it
1417 * out from the amount we loaded for the transfer.
1418 *
1419 * We need to do this as DMA pointers are always 32bit aligned
1420 * so may overshoot/undershoot the transfer.
1421 */
1422
5b7d70c6
BD
1423 size_done = hs_ep->size_loaded - size_left;
1424 size_done += hs_ep->last_load;
1425
1426 req->actual = size_done;
1427 }
1428
a33e7136
BD
1429 /* if there is more request to do, schedule new transfer */
1430 if (req->actual < req->length && size_left == 0) {
1431 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1432 return;
1433 }
1434
5b7d70c6
BD
1435 if (req->actual < req->length && req->short_not_ok) {
1436 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1437 __func__, req->actual, req->length);
1438
8b9bc460
LM
1439 /*
1440 * todo - what should we return here? there's no one else
1441 * even bothering to check the status.
1442 */
5b7d70c6
BD
1443 }
1444
fe0b94ab
MYK
1445 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1446 /* Move to STATUS IN */
1447 s3c_hsotg_ep0_zlp(hsotg, true);
1448 return;
5b7d70c6
BD
1449 }
1450
5ad1d316 1451 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
5b7d70c6
BD
1452}
1453
1454/**
1455 * s3c_hsotg_read_frameno - read current frame number
1456 * @hsotg: The device instance
1457 *
1458 * Return the current frame number
8b9bc460 1459 */
941fcce4 1460static u32 s3c_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
1461{
1462 u32 dsts;
1463
94cb8fd6
LM
1464 dsts = readl(hsotg->regs + DSTS);
1465 dsts &= DSTS_SOFFN_MASK;
1466 dsts >>= DSTS_SOFFN_SHIFT;
5b7d70c6
BD
1467
1468 return dsts;
1469}
1470
1471/**
1472 * s3c_hsotg_handle_rx - RX FIFO has data
1473 * @hsotg: The device instance
1474 *
1475 * The IRQ handler has detected that the RX FIFO has some data in it
1476 * that requires processing, so find out what is in there and do the
1477 * appropriate read.
1478 *
25985edc 1479 * The RXFIFO is a true FIFO, the packets coming out are still in packet
5b7d70c6
BD
1480 * chunks, so if you have x packets received on an endpoint you'll get x
1481 * FIFO events delivered, each with a packet's worth of data in it.
1482 *
1483 * When using DMA, we should not be processing events from the RXFIFO
1484 * as the actual data should be sent to the memory directly and we turn
1485 * on the completion interrupts to get notifications of transfer completion.
1486 */
941fcce4 1487static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
5b7d70c6 1488{
94cb8fd6 1489 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
5b7d70c6
BD
1490 u32 epnum, status, size;
1491
1492 WARN_ON(using_dma(hsotg));
1493
47a1685f
DN
1494 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1495 status = grxstsr & GRXSTS_PKTSTS_MASK;
5b7d70c6 1496
47a1685f
DN
1497 size = grxstsr & GRXSTS_BYTECNT_MASK;
1498 size >>= GRXSTS_BYTECNT_SHIFT;
5b7d70c6
BD
1499
1500 if (1)
1501 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1502 __func__, grxstsr, size, epnum);
1503
47a1685f
DN
1504 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1505 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1506 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
5b7d70c6
BD
1507 break;
1508
47a1685f 1509 case GRXSTS_PKTSTS_OUTDONE:
5b7d70c6
BD
1510 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1511 s3c_hsotg_read_frameno(hsotg));
1512
1513 if (!using_dma(hsotg))
fe0b94ab 1514 s3c_hsotg_handle_outdone(hsotg, epnum);
5b7d70c6
BD
1515 break;
1516
47a1685f 1517 case GRXSTS_PKTSTS_SETUPDONE:
5b7d70c6
BD
1518 dev_dbg(hsotg->dev,
1519 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1520 s3c_hsotg_read_frameno(hsotg),
94cb8fd6 1521 readl(hsotg->regs + DOEPCTL(0)));
fe0b94ab
MYK
1522 /*
1523 * Call s3c_hsotg_handle_outdone here if it was not called from
1524 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1525 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1526 */
1527 if (hsotg->ep0_state == DWC2_EP0_SETUP)
1528 s3c_hsotg_handle_outdone(hsotg, epnum);
5b7d70c6
BD
1529 break;
1530
47a1685f 1531 case GRXSTS_PKTSTS_OUTRX:
5b7d70c6
BD
1532 s3c_hsotg_rx_data(hsotg, epnum, size);
1533 break;
1534
47a1685f 1535 case GRXSTS_PKTSTS_SETUPRX:
5b7d70c6
BD
1536 dev_dbg(hsotg->dev,
1537 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1538 s3c_hsotg_read_frameno(hsotg),
94cb8fd6 1539 readl(hsotg->regs + DOEPCTL(0)));
5b7d70c6 1540
fe0b94ab
MYK
1541 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1542
5b7d70c6
BD
1543 s3c_hsotg_rx_data(hsotg, epnum, size);
1544 break;
1545
1546 default:
1547 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1548 __func__, grxstsr);
1549
1550 s3c_hsotg_dump(hsotg);
1551 break;
1552 }
1553}
1554
1555/**
1556 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1557 * @mps: The maximum packet size in bytes.
8b9bc460 1558 */
5b7d70c6
BD
1559static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1560{
1561 switch (mps) {
1562 case 64:
94cb8fd6 1563 return D0EPCTL_MPS_64;
5b7d70c6 1564 case 32:
94cb8fd6 1565 return D0EPCTL_MPS_32;
5b7d70c6 1566 case 16:
94cb8fd6 1567 return D0EPCTL_MPS_16;
5b7d70c6 1568 case 8:
94cb8fd6 1569 return D0EPCTL_MPS_8;
5b7d70c6
BD
1570 }
1571
1572 /* bad max packet size, warn and return invalid result */
1573 WARN_ON(1);
1574 return (u32)-1;
1575}
1576
1577/**
1578 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1579 * @hsotg: The driver state.
1580 * @ep: The index number of the endpoint
1581 * @mps: The maximum packet size in bytes
1582 *
1583 * Configure the maximum packet size for the given endpoint, updating
1584 * the hardware control registers to reflect this.
1585 */
941fcce4 1586static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
c6f5c050 1587 unsigned int ep, unsigned int mps, unsigned int dir_in)
5b7d70c6 1588{
c6f5c050 1589 struct s3c_hsotg_ep *hs_ep;
5b7d70c6
BD
1590 void __iomem *regs = hsotg->regs;
1591 u32 mpsval;
4fca54aa 1592 u32 mcval;
5b7d70c6
BD
1593 u32 reg;
1594
c6f5c050
MYK
1595 hs_ep = index_to_ep(hsotg, ep, dir_in);
1596 if (!hs_ep)
1597 return;
1598
5b7d70c6
BD
1599 if (ep == 0) {
1600 /* EP0 is a special case */
1601 mpsval = s3c_hsotg_ep0_mps(mps);
1602 if (mpsval > 3)
1603 goto bad_mps;
e9edd199 1604 hs_ep->ep.maxpacket = mps;
4fca54aa 1605 hs_ep->mc = 1;
5b7d70c6 1606 } else {
47a1685f 1607 mpsval = mps & DXEPCTL_MPS_MASK;
e9edd199 1608 if (mpsval > 1024)
5b7d70c6 1609 goto bad_mps;
4fca54aa
RB
1610 mcval = ((mps >> 11) & 0x3) + 1;
1611 hs_ep->mc = mcval;
1612 if (mcval > 3)
1613 goto bad_mps;
e9edd199 1614 hs_ep->ep.maxpacket = mpsval;
5b7d70c6
BD
1615 }
1616
c6f5c050
MYK
1617 if (dir_in) {
1618 reg = readl(regs + DIEPCTL(ep));
1619 reg &= ~DXEPCTL_MPS_MASK;
1620 reg |= mpsval;
1621 writel(reg, regs + DIEPCTL(ep));
1622 } else {
94cb8fd6 1623 reg = readl(regs + DOEPCTL(ep));
47a1685f 1624 reg &= ~DXEPCTL_MPS_MASK;
659ad60c 1625 reg |= mpsval;
94cb8fd6 1626 writel(reg, regs + DOEPCTL(ep));
659ad60c 1627 }
5b7d70c6
BD
1628
1629 return;
1630
1631bad_mps:
1632 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1633}
1634
9c39ddc6
AT
1635/**
1636 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1637 * @hsotg: The driver state
1638 * @idx: The index for the endpoint (0..15)
1639 */
941fcce4 1640static void s3c_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
9c39ddc6
AT
1641{
1642 int timeout;
1643 int val;
1644
47a1685f 1645 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
94cb8fd6 1646 hsotg->regs + GRSTCTL);
9c39ddc6
AT
1647
1648 /* wait until the fifo is flushed */
1649 timeout = 100;
1650
1651 while (1) {
94cb8fd6 1652 val = readl(hsotg->regs + GRSTCTL);
9c39ddc6 1653
47a1685f 1654 if ((val & (GRSTCTL_TXFFLSH)) == 0)
9c39ddc6
AT
1655 break;
1656
1657 if (--timeout == 0) {
1658 dev_err(hsotg->dev,
1659 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1660 __func__, val);
e0cbe595 1661 break;
9c39ddc6
AT
1662 }
1663
1664 udelay(1);
1665 }
1666}
5b7d70c6
BD
1667
1668/**
1669 * s3c_hsotg_trytx - check to see if anything needs transmitting
1670 * @hsotg: The driver state
1671 * @hs_ep: The driver endpoint to check.
1672 *
1673 * Check to see if there is a request that has data to send, and if so
1674 * make an attempt to write data into the FIFO.
1675 */
941fcce4 1676static int s3c_hsotg_trytx(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1677 struct s3c_hsotg_ep *hs_ep)
1678{
1679 struct s3c_hsotg_req *hs_req = hs_ep->req;
1680
afcf4169
RB
1681 if (!hs_ep->dir_in || !hs_req) {
1682 /**
1683 * if request is not enqueued, we disable interrupts
1684 * for endpoints, excepting ep0
1685 */
1686 if (hs_ep->index != 0)
1687 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1688 hs_ep->dir_in, 0);
5b7d70c6 1689 return 0;
afcf4169 1690 }
5b7d70c6
BD
1691
1692 if (hs_req->req.actual < hs_req->req.length) {
1693 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1694 hs_ep->index);
1695 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1696 }
1697
1698 return 0;
1699}
1700
1701/**
1702 * s3c_hsotg_complete_in - complete IN transfer
1703 * @hsotg: The device state.
1704 * @hs_ep: The endpoint that has just completed.
1705 *
1706 * An IN transfer has been completed, update the transfer's state and then
1707 * call the relevant completion routines.
1708 */
941fcce4 1709static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1710 struct s3c_hsotg_ep *hs_ep)
1711{
1712 struct s3c_hsotg_req *hs_req = hs_ep->req;
94cb8fd6 1713 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
5b7d70c6
BD
1714 int size_left, size_done;
1715
1716 if (!hs_req) {
1717 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1718 return;
1719 }
1720
d3ca0259 1721 /* Finish ZLP handling for IN EP0 transactions */
fe0b94ab
MYK
1722 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1723 dev_dbg(hsotg->dev, "zlp packet sent\n");
5ad1d316 1724 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
fe0b94ab 1725 s3c_hsotg_enqueue_setup(hsotg);
d3ca0259
LM
1726 return;
1727 }
1728
8b9bc460
LM
1729 /*
1730 * Calculate the size of the transfer by checking how much is left
5b7d70c6
BD
1731 * in the endpoint size register and then working it out from
1732 * the amount we loaded for the transfer.
1733 *
1734 * We do this even for DMA, as the transfer may have incremented
1735 * past the end of the buffer (DMA transfers are always 32bit
1736 * aligned).
1737 */
1738
47a1685f 1739 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6
BD
1740
1741 size_done = hs_ep->size_loaded - size_left;
1742 size_done += hs_ep->last_load;
1743
1744 if (hs_req->req.actual != size_done)
1745 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1746 __func__, hs_req->req.actual, size_done);
1747
1748 hs_req->req.actual = size_done;
d3ca0259
LM
1749 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1750 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
1751
5b7d70c6
BD
1752 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1753 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1754 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
fe0b94ab
MYK
1755 return;
1756 }
1757
f71b5e25
MYK
1758 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
1759 if (hs_ep->sent_zlp) {
1760 s3c_hsotg_program_zlp(hsotg, hs_ep);
1761 hs_ep->sent_zlp = 0;
1762 /* transfer will be completed on next complete interrupt */
1763 return;
1764 }
1765
fe0b94ab
MYK
1766 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1767 /* Move to STATUS OUT */
1768 s3c_hsotg_ep0_zlp(hsotg, false);
1769 return;
1770 }
1771
1772 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
5b7d70c6
BD
1773}
1774
1775/**
1776 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1777 * @hsotg: The driver state
1778 * @idx: The index for the endpoint (0..15)
1779 * @dir_in: Set if this is an IN endpoint
1780 *
1781 * Process and clear any interrupt pending for an individual endpoint
8b9bc460 1782 */
941fcce4 1783static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
5b7d70c6
BD
1784 int dir_in)
1785{
c6f5c050 1786 struct s3c_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
94cb8fd6
LM
1787 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1788 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1789 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
5b7d70c6 1790 u32 ints;
1479e841 1791 u32 ctrl;
5b7d70c6
BD
1792
1793 ints = readl(hsotg->regs + epint_reg);
1479e841 1794 ctrl = readl(hsotg->regs + epctl_reg);
5b7d70c6 1795
a3395f0d
AT
1796 /* Clear endpoint interrupts */
1797 writel(ints, hsotg->regs + epint_reg);
1798
c6f5c050
MYK
1799 if (!hs_ep) {
1800 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
1801 __func__, idx, dir_in ? "in" : "out");
1802 return;
1803 }
1804
5b7d70c6
BD
1805 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1806 __func__, idx, dir_in ? "in" : "out", ints);
1807
b787d755
MYK
1808 /* Don't process XferCompl interrupt if it is a setup packet */
1809 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
1810 ints &= ~DXEPINT_XFERCOMPL;
1811
47a1685f 1812 if (ints & DXEPINT_XFERCOMPL) {
1479e841 1813 if (hs_ep->isochronous && hs_ep->interval == 1) {
47a1685f
DN
1814 if (ctrl & DXEPCTL_EOFRNUM)
1815 ctrl |= DXEPCTL_SETEVENFR;
1479e841 1816 else
47a1685f 1817 ctrl |= DXEPCTL_SETODDFR;
1479e841
RB
1818 writel(ctrl, hsotg->regs + epctl_reg);
1819 }
1820
5b7d70c6 1821 dev_dbg(hsotg->dev,
47a1685f 1822 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
5b7d70c6
BD
1823 __func__, readl(hsotg->regs + epctl_reg),
1824 readl(hsotg->regs + epsiz_reg));
1825
8b9bc460
LM
1826 /*
1827 * we get OutDone from the FIFO, so we only need to look
1828 * at completing IN requests here
1829 */
5b7d70c6
BD
1830 if (dir_in) {
1831 s3c_hsotg_complete_in(hsotg, hs_ep);
1832
c9a64ea8 1833 if (idx == 0 && !hs_ep->req)
5b7d70c6
BD
1834 s3c_hsotg_enqueue_setup(hsotg);
1835 } else if (using_dma(hsotg)) {
8b9bc460
LM
1836 /*
1837 * We're using DMA, we need to fire an OutDone here
1838 * as we ignore the RXFIFO.
1839 */
5b7d70c6 1840
fe0b94ab 1841 s3c_hsotg_handle_outdone(hsotg, idx);
5b7d70c6 1842 }
5b7d70c6
BD
1843 }
1844
47a1685f 1845 if (ints & DXEPINT_EPDISBLD) {
5b7d70c6 1846 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
5b7d70c6 1847
9c39ddc6
AT
1848 if (dir_in) {
1849 int epctl = readl(hsotg->regs + epctl_reg);
1850
b203d0a2 1851 s3c_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
9c39ddc6 1852
47a1685f
DN
1853 if ((epctl & DXEPCTL_STALL) &&
1854 (epctl & DXEPCTL_EPTYPE_BULK)) {
94cb8fd6 1855 int dctl = readl(hsotg->regs + DCTL);
9c39ddc6 1856
47a1685f 1857 dctl |= DCTL_CGNPINNAK;
94cb8fd6 1858 writel(dctl, hsotg->regs + DCTL);
9c39ddc6
AT
1859 }
1860 }
1861 }
1862
47a1685f 1863 if (ints & DXEPINT_AHBERR)
5b7d70c6 1864 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
5b7d70c6 1865
47a1685f 1866 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
5b7d70c6
BD
1867 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1868
1869 if (using_dma(hsotg) && idx == 0) {
8b9bc460
LM
1870 /*
1871 * this is the notification we've received a
5b7d70c6
BD
1872 * setup packet. In non-DMA mode we'd get this
1873 * from the RXFIFO, instead we need to process
8b9bc460
LM
1874 * the setup here.
1875 */
5b7d70c6
BD
1876
1877 if (dir_in)
1878 WARN_ON_ONCE(1);
1879 else
fe0b94ab 1880 s3c_hsotg_handle_outdone(hsotg, 0);
5b7d70c6 1881 }
5b7d70c6
BD
1882 }
1883
47a1685f 1884 if (ints & DXEPINT_BACK2BACKSETUP)
5b7d70c6 1885 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
5b7d70c6 1886
1479e841 1887 if (dir_in && !hs_ep->isochronous) {
8b9bc460 1888 /* not sure if this is important, but we'll clear it anyway */
47a1685f 1889 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
5b7d70c6
BD
1890 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1891 __func__, idx);
5b7d70c6
BD
1892 }
1893
1894 /* this probably means something bad is happening */
47a1685f 1895 if (ints & DIEPMSK_INTKNEPMISMSK) {
5b7d70c6
BD
1896 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1897 __func__, idx);
5b7d70c6 1898 }
10aebc77
BD
1899
1900 /* FIFO has space or is empty (see GAHBCFG) */
1901 if (hsotg->dedicated_fifos &&
47a1685f 1902 ints & DIEPMSK_TXFIFOEMPTY) {
10aebc77
BD
1903 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1904 __func__, idx);
70fa030f
AT
1905 if (!using_dma(hsotg))
1906 s3c_hsotg_trytx(hsotg, hs_ep);
10aebc77 1907 }
5b7d70c6 1908 }
5b7d70c6
BD
1909}
1910
1911/**
1912 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1913 * @hsotg: The device state.
1914 *
1915 * Handle updating the device settings after the enumeration phase has
1916 * been completed.
8b9bc460 1917 */
941fcce4 1918static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
5b7d70c6 1919{
94cb8fd6 1920 u32 dsts = readl(hsotg->regs + DSTS);
9b2667f1 1921 int ep0_mps = 0, ep_mps = 8;
5b7d70c6 1922
8b9bc460
LM
1923 /*
1924 * This should signal the finish of the enumeration phase
5b7d70c6 1925 * of the USB handshaking, so we should now know what rate
8b9bc460
LM
1926 * we connected at.
1927 */
5b7d70c6
BD
1928
1929 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1930
8b9bc460
LM
1931 /*
1932 * note, since we're limited by the size of transfer on EP0, and
5b7d70c6 1933 * it seems IN transfers must be a even number of packets we do
8b9bc460
LM
1934 * not advertise a 64byte MPS on EP0.
1935 */
5b7d70c6
BD
1936
1937 /* catch both EnumSpd_FS and EnumSpd_FS48 */
47a1685f
DN
1938 switch (dsts & DSTS_ENUMSPD_MASK) {
1939 case DSTS_ENUMSPD_FS:
1940 case DSTS_ENUMSPD_FS48:
5b7d70c6 1941 hsotg->gadget.speed = USB_SPEED_FULL;
5b7d70c6 1942 ep0_mps = EP0_MPS_LIMIT;
295538ff 1943 ep_mps = 1023;
5b7d70c6
BD
1944 break;
1945
47a1685f 1946 case DSTS_ENUMSPD_HS:
5b7d70c6 1947 hsotg->gadget.speed = USB_SPEED_HIGH;
5b7d70c6 1948 ep0_mps = EP0_MPS_LIMIT;
295538ff 1949 ep_mps = 1024;
5b7d70c6
BD
1950 break;
1951
47a1685f 1952 case DSTS_ENUMSPD_LS:
5b7d70c6 1953 hsotg->gadget.speed = USB_SPEED_LOW;
8b9bc460
LM
1954 /*
1955 * note, we don't actually support LS in this driver at the
5b7d70c6
BD
1956 * moment, and the documentation seems to imply that it isn't
1957 * supported by the PHYs on some of the devices.
1958 */
1959 break;
1960 }
e538dfda
MN
1961 dev_info(hsotg->dev, "new device is %s\n",
1962 usb_speed_string(hsotg->gadget.speed));
5b7d70c6 1963
8b9bc460
LM
1964 /*
1965 * we should now know the maximum packet size for an
1966 * endpoint, so set the endpoints to a default value.
1967 */
5b7d70c6
BD
1968
1969 if (ep0_mps) {
1970 int i;
c6f5c050
MYK
1971 /* Initialize ep0 for both in and out directions */
1972 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
1973 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
1974 for (i = 1; i < hsotg->num_of_eps; i++) {
1975 if (hsotg->eps_in[i])
1976 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
1977 if (hsotg->eps_out[i])
1978 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
1979 }
5b7d70c6
BD
1980 }
1981
1982 /* ensure after enumeration our EP0 is active */
1983
1984 s3c_hsotg_enqueue_setup(hsotg);
1985
1986 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
94cb8fd6
LM
1987 readl(hsotg->regs + DIEPCTL0),
1988 readl(hsotg->regs + DOEPCTL0));
5b7d70c6
BD
1989}
1990
1991/**
1992 * kill_all_requests - remove all requests from the endpoint's queue
1993 * @hsotg: The device state.
1994 * @ep: The endpoint the requests may be on.
1995 * @result: The result code to use.
5b7d70c6
BD
1996 *
1997 * Go through the requests on the given endpoint and mark them
1998 * completed with the given result code.
1999 */
941fcce4 2000static void kill_all_requests(struct dwc2_hsotg *hsotg,
5b7d70c6 2001 struct s3c_hsotg_ep *ep,
6b448af4 2002 int result)
5b7d70c6
BD
2003{
2004 struct s3c_hsotg_req *req, *treq;
b203d0a2 2005 unsigned size;
5b7d70c6 2006
6b448af4 2007 ep->req = NULL;
5b7d70c6 2008
6b448af4 2009 list_for_each_entry_safe(req, treq, &ep->queue, queue)
5b7d70c6
BD
2010 s3c_hsotg_complete_request(hsotg, ep, req,
2011 result);
6b448af4 2012
b203d0a2
RB
2013 if (!hsotg->dedicated_fifos)
2014 return;
2015 size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2016 if (size < ep->fifo_size)
2017 s3c_hsotg_txfifo_flush(hsotg, ep->fifo_index);
5b7d70c6
BD
2018}
2019
5b7d70c6 2020/**
5e891342 2021 * s3c_hsotg_disconnect - disconnect service
5b7d70c6
BD
2022 * @hsotg: The device state.
2023 *
5e891342
LM
2024 * The device has been disconnected. Remove all current
2025 * transactions and signal the gadget driver that this
2026 * has happened.
8b9bc460 2027 */
4ace06e8 2028void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
2029{
2030 unsigned ep;
2031
4ace06e8
MS
2032 if (!hsotg->connected)
2033 return;
2034
2035 hsotg->connected = 0;
c6f5c050
MYK
2036
2037 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2038 if (hsotg->eps_in[ep])
2039 kill_all_requests(hsotg, hsotg->eps_in[ep],
2040 -ESHUTDOWN);
2041 if (hsotg->eps_out[ep])
2042 kill_all_requests(hsotg, hsotg->eps_out[ep],
2043 -ESHUTDOWN);
2044 }
5b7d70c6
BD
2045
2046 call_gadget(hsotg, disconnect);
2047}
4ace06e8 2048EXPORT_SYMBOL_GPL(s3c_hsotg_disconnect);
5b7d70c6
BD
2049
2050/**
2051 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2052 * @hsotg: The device state:
2053 * @periodic: True if this is a periodic FIFO interrupt
2054 */
941fcce4 2055static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
5b7d70c6
BD
2056{
2057 struct s3c_hsotg_ep *ep;
2058 int epno, ret;
2059
2060 /* look through for any more data to transmit */
b3f489b2 2061 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
c6f5c050
MYK
2062 ep = index_to_ep(hsotg, epno, 1);
2063
2064 if (!ep)
2065 continue;
5b7d70c6
BD
2066
2067 if (!ep->dir_in)
2068 continue;
2069
2070 if ((periodic && !ep->periodic) ||
2071 (!periodic && ep->periodic))
2072 continue;
2073
2074 ret = s3c_hsotg_trytx(hsotg, ep);
2075 if (ret < 0)
2076 break;
2077 }
2078}
2079
5b7d70c6 2080/* IRQ flags which will trigger a retry around the IRQ loop */
47a1685f
DN
2081#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2082 GINTSTS_PTXFEMP | \
2083 GINTSTS_RXFLVL)
5b7d70c6 2084
308d734e
LM
2085/**
2086 * s3c_hsotg_corereset - issue softreset to the core
2087 * @hsotg: The device state
2088 *
2089 * Issue a soft reset to the core, and await the core finishing it.
8b9bc460 2090 */
941fcce4 2091static int s3c_hsotg_corereset(struct dwc2_hsotg *hsotg)
308d734e
LM
2092{
2093 int timeout;
2094 u32 grstctl;
2095
2096 dev_dbg(hsotg->dev, "resetting core\n");
2097
2098 /* issue soft reset */
47a1685f 2099 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
308d734e 2100
2868fea2 2101 timeout = 10000;
308d734e 2102 do {
94cb8fd6 2103 grstctl = readl(hsotg->regs + GRSTCTL);
47a1685f 2104 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
308d734e 2105
47a1685f 2106 if (grstctl & GRSTCTL_CSFTRST) {
308d734e
LM
2107 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2108 return -EINVAL;
2109 }
2110
2868fea2 2111 timeout = 10000;
308d734e
LM
2112
2113 while (1) {
94cb8fd6 2114 u32 grstctl = readl(hsotg->regs + GRSTCTL);
308d734e
LM
2115
2116 if (timeout-- < 0) {
2117 dev_info(hsotg->dev,
2118 "%s: reset failed, GRSTCTL=%08x\n",
2119 __func__, grstctl);
2120 return -ETIMEDOUT;
2121 }
2122
47a1685f 2123 if (!(grstctl & GRSTCTL_AHBIDLE))
308d734e
LM
2124 continue;
2125
2126 break; /* reset done */
2127 }
2128
2129 dev_dbg(hsotg->dev, "reset successful\n");
2130 return 0;
2131}
2132
8b9bc460
LM
2133/**
2134 * s3c_hsotg_core_init - issue softreset to the core
2135 * @hsotg: The device state
2136 *
2137 * Issue a soft reset to the core, and await the core finishing it.
2138 */
510ffaa4 2139void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
308d734e
LM
2140{
2141 s3c_hsotg_corereset(hsotg);
2142
2143 /*
2144 * we must now enable ep0 ready for host detection and then
2145 * set configuration.
2146 */
2147
2148 /* set the PLL on, remove the HNP/SRP and set the PHY */
47a1685f 2149 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
94cb8fd6 2150 (0x5 << 10), hsotg->regs + GUSBCFG);
308d734e
LM
2151
2152 s3c_hsotg_init_fifo(hsotg);
2153
47a1685f 2154 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
308d734e 2155
47a1685f 2156 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
308d734e
LM
2157
2158 /* Clear any pending OTG interrupts */
94cb8fd6 2159 writel(0xffffffff, hsotg->regs + GOTGINT);
308d734e
LM
2160
2161 /* Clear any pending interrupts */
94cb8fd6 2162 writel(0xffffffff, hsotg->regs + GINTSTS);
308d734e 2163
47a1685f
DN
2164 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2165 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2166 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2167 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2168 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2169 hsotg->regs + GINTMSK);
308d734e
LM
2170
2171 if (using_dma(hsotg))
47a1685f 2172 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
5f05048e 2173 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
94cb8fd6 2174 hsotg->regs + GAHBCFG);
308d734e 2175 else
47a1685f
DN
2176 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2177 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2178 GAHBCFG_GLBL_INTR_EN,
8acc8296 2179 hsotg->regs + GAHBCFG);
308d734e
LM
2180
2181 /*
8acc8296
RB
2182 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2183 * when we have no data to transfer. Otherwise we get being flooded by
2184 * interrupts.
308d734e
LM
2185 */
2186
6ff2e832
MYK
2187 writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
2188 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
47a1685f
DN
2189 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2190 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2191 DIEPMSK_INTKNEPMISMSK,
2192 hsotg->regs + DIEPMSK);
308d734e
LM
2193
2194 /*
2195 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2196 * DMA mode we may need this.
2197 */
47a1685f
DN
2198 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2199 DIEPMSK_TIMEOUTMSK) : 0) |
2200 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2201 DOEPMSK_SETUPMSK,
2202 hsotg->regs + DOEPMSK);
308d734e 2203
94cb8fd6 2204 writel(0, hsotg->regs + DAINTMSK);
308d734e
LM
2205
2206 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
94cb8fd6
LM
2207 readl(hsotg->regs + DIEPCTL0),
2208 readl(hsotg->regs + DOEPCTL0));
308d734e
LM
2209
2210 /* enable in and out endpoint interrupts */
47a1685f 2211 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
308d734e
LM
2212
2213 /*
2214 * Enable the RXFIFO when in slave mode, as this is how we collect
2215 * the data. In DMA mode, we get events from the FIFO but also
2216 * things we cannot process, so do not use it.
2217 */
2218 if (!using_dma(hsotg))
47a1685f 2219 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
308d734e
LM
2220
2221 /* Enable interrupts for EP0 in and out */
2222 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2223 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2224
47a1685f 2225 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
308d734e 2226 udelay(10); /* see openiboot */
47a1685f 2227 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
308d734e 2228
94cb8fd6 2229 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
308d734e
LM
2230
2231 /*
94cb8fd6 2232 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
308d734e
LM
2233 * writing to the EPCTL register..
2234 */
2235
2236 /* set to read 1 8byte packet */
47a1685f
DN
2237 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2238 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
308d734e 2239
c6f5c050 2240 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
47a1685f
DN
2241 DXEPCTL_CNAK | DXEPCTL_EPENA |
2242 DXEPCTL_USBACTEP,
94cb8fd6 2243 hsotg->regs + DOEPCTL0);
308d734e
LM
2244
2245 /* enable, but don't activate EP0in */
c6f5c050 2246 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
47a1685f 2247 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
308d734e
LM
2248
2249 s3c_hsotg_enqueue_setup(hsotg);
2250
2251 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
94cb8fd6
LM
2252 readl(hsotg->regs + DIEPCTL0),
2253 readl(hsotg->regs + DOEPCTL0));
308d734e
LM
2254
2255 /* clear global NAKs */
ad38dc5d 2256 writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK | DCTL_SFTDISCON,
94cb8fd6 2257 hsotg->regs + DCTL);
308d734e
LM
2258
2259 /* must be at-least 3ms to allow bus to see disconnect */
2260 mdelay(3);
2261
ac3c81f3 2262 hsotg->last_rst = jiffies;
ad38dc5d
MS
2263}
2264
941fcce4 2265static void s3c_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
ad38dc5d
MS
2266{
2267 /* set the soft-disconnect bit */
2268 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2269}
ac3c81f3 2270
510ffaa4 2271void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg)
ad38dc5d 2272{
308d734e 2273 /* remove the soft-disconnect and let's go */
47a1685f 2274 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
308d734e
LM
2275}
2276
5b7d70c6
BD
2277/**
2278 * s3c_hsotg_irq - handle device interrupt
2279 * @irq: The IRQ number triggered
2280 * @pw: The pw value when registered the handler.
2281 */
2282static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2283{
941fcce4 2284 struct dwc2_hsotg *hsotg = pw;
5b7d70c6
BD
2285 int retry_count = 8;
2286 u32 gintsts;
2287 u32 gintmsk;
2288
5ad1d316 2289 spin_lock(&hsotg->lock);
5b7d70c6 2290irq_retry:
94cb8fd6
LM
2291 gintsts = readl(hsotg->regs + GINTSTS);
2292 gintmsk = readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
2293
2294 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2295 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2296
2297 gintsts &= gintmsk;
2298
47a1685f
DN
2299 if (gintsts & GINTSTS_ENUMDONE) {
2300 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
a3395f0d
AT
2301
2302 s3c_hsotg_irq_enumdone(hsotg);
4ace06e8 2303 hsotg->connected = 1;
5b7d70c6
BD
2304 }
2305
47a1685f 2306 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
94cb8fd6 2307 u32 daint = readl(hsotg->regs + DAINT);
7e804650
RB
2308 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2309 u32 daint_out, daint_in;
5b7d70c6
BD
2310 int ep;
2311
7e804650 2312 daint &= daintmsk;
47a1685f
DN
2313 daint_out = daint >> DAINT_OUTEP_SHIFT;
2314 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
7e804650 2315
5b7d70c6
BD
2316 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2317
cec87f1d
MYK
2318 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2319 ep++, daint_out >>= 1) {
5b7d70c6
BD
2320 if (daint_out & 1)
2321 s3c_hsotg_epint(hsotg, ep, 0);
2322 }
2323
cec87f1d
MYK
2324 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2325 ep++, daint_in >>= 1) {
5b7d70c6
BD
2326 if (daint_in & 1)
2327 s3c_hsotg_epint(hsotg, ep, 1);
2328 }
5b7d70c6
BD
2329 }
2330
47a1685f 2331 if (gintsts & GINTSTS_USBRST) {
12a1f4dc 2332
94cb8fd6 2333 u32 usb_status = readl(hsotg->regs + GOTGCTL);
12a1f4dc 2334
9599815d 2335 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
5b7d70c6 2336 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
94cb8fd6 2337 readl(hsotg->regs + GNPTXSTS));
5b7d70c6 2338
47a1685f 2339 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
a3395f0d 2340
94cb8fd6 2341 if (usb_status & GOTGCTL_BSESVLD) {
12a1f4dc
LM
2342 if (time_after(jiffies, hsotg->last_rst +
2343 msecs_to_jiffies(200))) {
5b7d70c6 2344
c6f5c050 2345 kill_all_requests(hsotg, hsotg->eps_out[0],
6b448af4 2346 -ECONNRESET);
5b7d70c6 2347
ad38dc5d
MS
2348 s3c_hsotg_core_init_disconnected(hsotg);
2349 s3c_hsotg_core_connect(hsotg);
12a1f4dc
LM
2350 }
2351 }
5b7d70c6
BD
2352 }
2353
2354 /* check both FIFOs */
2355
47a1685f 2356 if (gintsts & GINTSTS_NPTXFEMP) {
5b7d70c6
BD
2357 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2358
8b9bc460
LM
2359 /*
2360 * Disable the interrupt to stop it happening again
5b7d70c6 2361 * unless one of these endpoint routines decides that
8b9bc460
LM
2362 * it needs re-enabling
2363 */
5b7d70c6 2364
47a1685f 2365 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
5b7d70c6 2366 s3c_hsotg_irq_fifoempty(hsotg, false);
5b7d70c6
BD
2367 }
2368
47a1685f 2369 if (gintsts & GINTSTS_PTXFEMP) {
5b7d70c6
BD
2370 dev_dbg(hsotg->dev, "PTxFEmp\n");
2371
94cb8fd6 2372 /* See note in GINTSTS_NPTxFEmp */
5b7d70c6 2373
47a1685f 2374 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
5b7d70c6 2375 s3c_hsotg_irq_fifoempty(hsotg, true);
5b7d70c6
BD
2376 }
2377
47a1685f 2378 if (gintsts & GINTSTS_RXFLVL) {
8b9bc460
LM
2379 /*
2380 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
5b7d70c6 2381 * we need to retry s3c_hsotg_handle_rx if this is still
8b9bc460
LM
2382 * set.
2383 */
5b7d70c6
BD
2384
2385 s3c_hsotg_handle_rx(hsotg);
5b7d70c6
BD
2386 }
2387
47a1685f 2388 if (gintsts & GINTSTS_ERLYSUSP) {
94cb8fd6 2389 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
47a1685f 2390 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
5b7d70c6
BD
2391 }
2392
8b9bc460
LM
2393 /*
2394 * these next two seem to crop-up occasionally causing the core
5b7d70c6 2395 * to shutdown the USB transfer, so try clearing them and logging
8b9bc460
LM
2396 * the occurrence.
2397 */
5b7d70c6 2398
47a1685f 2399 if (gintsts & GINTSTS_GOUTNAKEFF) {
5b7d70c6
BD
2400 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2401
47a1685f 2402 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
a3395f0d
AT
2403
2404 s3c_hsotg_dump(hsotg);
5b7d70c6
BD
2405 }
2406
47a1685f 2407 if (gintsts & GINTSTS_GINNAKEFF) {
5b7d70c6
BD
2408 dev_info(hsotg->dev, "GINNakEff triggered\n");
2409
47a1685f 2410 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
a3395f0d
AT
2411
2412 s3c_hsotg_dump(hsotg);
5b7d70c6
BD
2413 }
2414
8b9bc460
LM
2415 /*
2416 * if we've had fifo events, we should try and go around the
2417 * loop again to see if there's any point in returning yet.
2418 */
5b7d70c6
BD
2419
2420 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2421 goto irq_retry;
2422
5ad1d316
LM
2423 spin_unlock(&hsotg->lock);
2424
5b7d70c6
BD
2425 return IRQ_HANDLED;
2426}
2427
2428/**
2429 * s3c_hsotg_ep_enable - enable the given endpoint
2430 * @ep: The USB endpint to configure
2431 * @desc: The USB endpoint descriptor to configure with.
2432 *
2433 * This is called from the USB gadget code's usb_ep_enable().
8b9bc460 2434 */
5b7d70c6
BD
2435static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2436 const struct usb_endpoint_descriptor *desc)
2437{
2438 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2439 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
2440 unsigned long flags;
2441 int index = hs_ep->index;
2442 u32 epctrl_reg;
2443 u32 epctrl;
2444 u32 mps;
2445 int dir_in;
b203d0a2 2446 int i, val, size;
19c190f9 2447 int ret = 0;
5b7d70c6
BD
2448
2449 dev_dbg(hsotg->dev,
2450 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2451 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2452 desc->wMaxPacketSize, desc->bInterval);
2453
2454 /* not to be called for EP0 */
2455 WARN_ON(index == 0);
2456
2457 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2458 if (dir_in != hs_ep->dir_in) {
2459 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2460 return -EINVAL;
2461 }
2462
29cc8897 2463 mps = usb_endpoint_maxp(desc);
5b7d70c6
BD
2464
2465 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2466
94cb8fd6 2467 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
5b7d70c6
BD
2468 epctrl = readl(hsotg->regs + epctrl_reg);
2469
2470 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2471 __func__, epctrl, epctrl_reg);
2472
22258f49 2473 spin_lock_irqsave(&hsotg->lock, flags);
5b7d70c6 2474
47a1685f
DN
2475 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2476 epctrl |= DXEPCTL_MPS(mps);
5b7d70c6 2477
8b9bc460
LM
2478 /*
2479 * mark the endpoint as active, otherwise the core may ignore
2480 * transactions entirely for this endpoint
2481 */
47a1685f 2482 epctrl |= DXEPCTL_USBACTEP;
5b7d70c6 2483
8b9bc460
LM
2484 /*
2485 * set the NAK status on the endpoint, otherwise we might try and
5b7d70c6
BD
2486 * do something with data that we've yet got a request to process
2487 * since the RXFIFO will take data for an endpoint even if the
2488 * size register hasn't been set.
2489 */
2490
47a1685f 2491 epctrl |= DXEPCTL_SNAK;
5b7d70c6
BD
2492
2493 /* update the endpoint state */
c6f5c050 2494 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
5b7d70c6
BD
2495
2496 /* default, set to non-periodic */
1479e841 2497 hs_ep->isochronous = 0;
5b7d70c6 2498 hs_ep->periodic = 0;
a18ed7b0 2499 hs_ep->halted = 0;
1479e841 2500 hs_ep->interval = desc->bInterval;
5b7d70c6 2501
4fca54aa
RB
2502 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2503 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2504
5b7d70c6
BD
2505 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2506 case USB_ENDPOINT_XFER_ISOC:
47a1685f
DN
2507 epctrl |= DXEPCTL_EPTYPE_ISO;
2508 epctrl |= DXEPCTL_SETEVENFR;
1479e841
RB
2509 hs_ep->isochronous = 1;
2510 if (dir_in)
2511 hs_ep->periodic = 1;
2512 break;
5b7d70c6
BD
2513
2514 case USB_ENDPOINT_XFER_BULK:
47a1685f 2515 epctrl |= DXEPCTL_EPTYPE_BULK;
5b7d70c6
BD
2516 break;
2517
2518 case USB_ENDPOINT_XFER_INT:
b203d0a2 2519 if (dir_in)
5b7d70c6 2520 hs_ep->periodic = 1;
5b7d70c6 2521
47a1685f 2522 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
5b7d70c6
BD
2523 break;
2524
2525 case USB_ENDPOINT_XFER_CONTROL:
47a1685f 2526 epctrl |= DXEPCTL_EPTYPE_CONTROL;
5b7d70c6
BD
2527 break;
2528 }
2529
8b9bc460
LM
2530 /*
2531 * if the hardware has dedicated fifos, we must give each IN EP
10aebc77
BD
2532 * a unique tx-fifo even if it is non-periodic.
2533 */
b203d0a2
RB
2534 if (dir_in && hsotg->dedicated_fifos) {
2535 size = hs_ep->ep.maxpacket*hs_ep->mc;
5f2196bd 2536 for (i = 1; i < hsotg->num_of_eps; ++i) {
b203d0a2
RB
2537 if (hsotg->fifo_map & (1<<i))
2538 continue;
2539 val = readl(hsotg->regs + DPTXFSIZN(i));
2540 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2541 if (val < size)
2542 continue;
2543 hsotg->fifo_map |= 1<<i;
2544
2545 epctrl |= DXEPCTL_TXFNUM(i);
2546 hs_ep->fifo_index = i;
2547 hs_ep->fifo_size = val;
2548 break;
2549 }
5f2196bd
MYK
2550 if (i == hsotg->num_of_eps) {
2551 dev_err(hsotg->dev,
2552 "%s: No suitable fifo found\n", __func__);
b585a48b
SM
2553 ret = -ENOMEM;
2554 goto error;
2555 }
b203d0a2 2556 }
10aebc77 2557
5b7d70c6
BD
2558 /* for non control endpoints, set PID to D0 */
2559 if (index)
47a1685f 2560 epctrl |= DXEPCTL_SETD0PID;
5b7d70c6
BD
2561
2562 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2563 __func__, epctrl);
2564
2565 writel(epctrl, hsotg->regs + epctrl_reg);
2566 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2567 __func__, readl(hsotg->regs + epctrl_reg));
2568
2569 /* enable the endpoint interrupt */
2570 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2571
b585a48b 2572error:
22258f49 2573 spin_unlock_irqrestore(&hsotg->lock, flags);
19c190f9 2574 return ret;
5b7d70c6
BD
2575}
2576
8b9bc460
LM
2577/**
2578 * s3c_hsotg_ep_disable - disable given endpoint
2579 * @ep: The endpoint to disable.
2580 */
5b7d70c6
BD
2581static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2582{
2583 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2584 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
2585 int dir_in = hs_ep->dir_in;
2586 int index = hs_ep->index;
2587 unsigned long flags;
2588 u32 epctrl_reg;
2589 u32 ctrl;
2590
1e011293 2591 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
5b7d70c6 2592
c6f5c050 2593 if (ep == &hsotg->eps_out[0]->ep) {
5b7d70c6
BD
2594 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2595 return -EINVAL;
2596 }
2597
94cb8fd6 2598 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
5b7d70c6 2599
5ad1d316 2600 spin_lock_irqsave(&hsotg->lock, flags);
5b7d70c6 2601
b203d0a2
RB
2602 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2603 hs_ep->fifo_index = 0;
2604 hs_ep->fifo_size = 0;
5b7d70c6
BD
2605
2606 ctrl = readl(hsotg->regs + epctrl_reg);
47a1685f
DN
2607 ctrl &= ~DXEPCTL_EPENA;
2608 ctrl &= ~DXEPCTL_USBACTEP;
2609 ctrl |= DXEPCTL_SNAK;
5b7d70c6
BD
2610
2611 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2612 writel(ctrl, hsotg->regs + epctrl_reg);
2613
2614 /* disable endpoint interrupts */
2615 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2616
1141ea01
MYK
2617 /* terminate all requests with shutdown */
2618 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
2619
22258f49 2620 spin_unlock_irqrestore(&hsotg->lock, flags);
5b7d70c6
BD
2621 return 0;
2622}
2623
2624/**
2625 * on_list - check request is on the given endpoint
2626 * @ep: The endpoint to check.
2627 * @test: The request to test if it is on the endpoint.
8b9bc460 2628 */
5b7d70c6
BD
2629static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2630{
2631 struct s3c_hsotg_req *req, *treq;
2632
2633 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2634 if (req == test)
2635 return true;
2636 }
2637
2638 return false;
2639}
2640
8b9bc460
LM
2641/**
2642 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2643 * @ep: The endpoint to dequeue.
2644 * @req: The request to be removed from a queue.
2645 */
5b7d70c6
BD
2646static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2647{
2648 struct s3c_hsotg_req *hs_req = our_req(req);
2649 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2650 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6
BD
2651 unsigned long flags;
2652
1e011293 2653 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
5b7d70c6 2654
22258f49 2655 spin_lock_irqsave(&hs->lock, flags);
5b7d70c6
BD
2656
2657 if (!on_list(hs_ep, hs_req)) {
22258f49 2658 spin_unlock_irqrestore(&hs->lock, flags);
5b7d70c6
BD
2659 return -EINVAL;
2660 }
2661
2662 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
22258f49 2663 spin_unlock_irqrestore(&hs->lock, flags);
5b7d70c6
BD
2664
2665 return 0;
2666}
2667
8b9bc460
LM
2668/**
2669 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2670 * @ep: The endpoint to set halt.
2671 * @value: Set or unset the halt.
2672 */
5b7d70c6
BD
2673static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2674{
2675 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2676 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6 2677 int index = hs_ep->index;
5b7d70c6
BD
2678 u32 epreg;
2679 u32 epctl;
9c39ddc6 2680 u32 xfertype;
5b7d70c6
BD
2681
2682 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2683
c9f721b2
RB
2684 if (index == 0) {
2685 if (value)
2686 s3c_hsotg_stall_ep0(hs);
2687 else
2688 dev_warn(hs->dev,
2689 "%s: can't clear halt on ep0\n", __func__);
2690 return 0;
2691 }
2692
c6f5c050
MYK
2693 if (hs_ep->dir_in) {
2694 epreg = DIEPCTL(index);
2695 epctl = readl(hs->regs + epreg);
2696
2697 if (value) {
2698 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2699 if (epctl & DXEPCTL_EPENA)
2700 epctl |= DXEPCTL_EPDIS;
2701 } else {
2702 epctl &= ~DXEPCTL_STALL;
2703 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2704 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2705 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2706 epctl |= DXEPCTL_SETD0PID;
2707 }
2708 writel(epctl, hs->regs + epreg);
9c39ddc6 2709 } else {
5b7d70c6 2710
c6f5c050
MYK
2711 epreg = DOEPCTL(index);
2712 epctl = readl(hs->regs + epreg);
5b7d70c6 2713
c6f5c050
MYK
2714 if (value)
2715 epctl |= DXEPCTL_STALL;
2716 else {
2717 epctl &= ~DXEPCTL_STALL;
2718 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2719 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2720 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2721 epctl |= DXEPCTL_SETD0PID;
2722 }
2723 writel(epctl, hs->regs + epreg);
9c39ddc6 2724 }
5b7d70c6 2725
a18ed7b0
RB
2726 hs_ep->halted = value;
2727
5b7d70c6
BD
2728 return 0;
2729}
2730
5ad1d316
LM
2731/**
2732 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2733 * @ep: The endpoint to set halt.
2734 * @value: Set or unset the halt.
2735 */
2736static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2737{
2738 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2739 struct dwc2_hsotg *hs = hs_ep->parent;
5ad1d316
LM
2740 unsigned long flags = 0;
2741 int ret = 0;
2742
2743 spin_lock_irqsave(&hs->lock, flags);
2744 ret = s3c_hsotg_ep_sethalt(ep, value);
2745 spin_unlock_irqrestore(&hs->lock, flags);
2746
2747 return ret;
2748}
2749
5b7d70c6
BD
2750static struct usb_ep_ops s3c_hsotg_ep_ops = {
2751 .enable = s3c_hsotg_ep_enable,
2752 .disable = s3c_hsotg_ep_disable,
2753 .alloc_request = s3c_hsotg_ep_alloc_request,
2754 .free_request = s3c_hsotg_ep_free_request,
5ad1d316 2755 .queue = s3c_hsotg_ep_queue_lock,
5b7d70c6 2756 .dequeue = s3c_hsotg_ep_dequeue,
5ad1d316 2757 .set_halt = s3c_hsotg_ep_sethalt_lock,
25985edc 2758 /* note, don't believe we have any call for the fifo routines */
5b7d70c6
BD
2759};
2760
41188786
LM
2761/**
2762 * s3c_hsotg_phy_enable - enable platform phy dev
8b9bc460 2763 * @hsotg: The driver state
41188786
LM
2764 *
2765 * A wrapper for platform code responsible for controlling
2766 * low-level USB code
2767 */
941fcce4 2768static void s3c_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
41188786
LM
2769{
2770 struct platform_device *pdev = to_platform_device(hsotg->dev);
2771
2772 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
b2e587db 2773
ca2c5ba8 2774 if (hsotg->uphy)
74084844 2775 usb_phy_init(hsotg->uphy);
ca2c5ba8 2776 else if (hsotg->plat && hsotg->plat->phy_init)
41188786 2777 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
ca2c5ba8
KD
2778 else {
2779 phy_init(hsotg->phy);
2780 phy_power_on(hsotg->phy);
2781 }
41188786
LM
2782}
2783
2784/**
2785 * s3c_hsotg_phy_disable - disable platform phy dev
8b9bc460 2786 * @hsotg: The driver state
41188786
LM
2787 *
2788 * A wrapper for platform code responsible for controlling
2789 * low-level USB code
2790 */
941fcce4 2791static void s3c_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
41188786
LM
2792{
2793 struct platform_device *pdev = to_platform_device(hsotg->dev);
2794
ca2c5ba8 2795 if (hsotg->uphy)
74084844 2796 usb_phy_shutdown(hsotg->uphy);
ca2c5ba8 2797 else if (hsotg->plat && hsotg->plat->phy_exit)
41188786 2798 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
ca2c5ba8
KD
2799 else {
2800 phy_power_off(hsotg->phy);
2801 phy_exit(hsotg->phy);
2802 }
41188786
LM
2803}
2804
8b9bc460
LM
2805/**
2806 * s3c_hsotg_init - initalize the usb core
2807 * @hsotg: The driver state
2808 */
941fcce4 2809static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
b3f489b2
LM
2810{
2811 /* unmask subset of endpoint interrupts */
2812
47a1685f
DN
2813 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2814 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2815 hsotg->regs + DIEPMSK);
b3f489b2 2816
47a1685f
DN
2817 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2818 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2819 hsotg->regs + DOEPMSK);
b3f489b2 2820
94cb8fd6 2821 writel(0, hsotg->regs + DAINTMSK);
b3f489b2
LM
2822
2823 /* Be in disconnected state until gadget is registered */
47a1685f 2824 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
b3f489b2
LM
2825
2826 if (0) {
2827 /* post global nak until we're ready */
47a1685f 2828 writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
94cb8fd6 2829 hsotg->regs + DCTL);
b3f489b2
LM
2830 }
2831
2832 /* setup fifos */
2833
2834 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
94cb8fd6
LM
2835 readl(hsotg->regs + GRXFSIZ),
2836 readl(hsotg->regs + GNPTXFSIZ));
b3f489b2
LM
2837
2838 s3c_hsotg_init_fifo(hsotg);
2839
2840 /* set the PLL on, remove the HNP/SRP and set the PHY */
47a1685f 2841 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
94cb8fd6 2842 hsotg->regs + GUSBCFG);
b3f489b2 2843
f5090044
GH
2844 if (using_dma(hsotg))
2845 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
b3f489b2
LM
2846}
2847
8b9bc460
LM
2848/**
2849 * s3c_hsotg_udc_start - prepare the udc for work
2850 * @gadget: The usb gadget state
2851 * @driver: The usb gadget driver
2852 *
2853 * Perform initialization to prepare udc device and driver
2854 * to work.
2855 */
f65f0f10
LM
2856static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2857 struct usb_gadget_driver *driver)
5b7d70c6 2858{
941fcce4 2859 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
5b9451f8 2860 unsigned long flags;
5b7d70c6
BD
2861 int ret;
2862
2863 if (!hsotg) {
a023da33 2864 pr_err("%s: called with no device\n", __func__);
5b7d70c6
BD
2865 return -ENODEV;
2866 }
2867
2868 if (!driver) {
2869 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2870 return -EINVAL;
2871 }
2872
7177aed4 2873 if (driver->max_speed < USB_SPEED_FULL)
5b7d70c6 2874 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
5b7d70c6 2875
f65f0f10 2876 if (!driver->setup) {
5b7d70c6
BD
2877 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2878 return -EINVAL;
2879 }
2880
7ad8096e 2881 mutex_lock(&hsotg->init_mutex);
5b7d70c6
BD
2882 WARN_ON(hsotg->driver);
2883
2884 driver->driver.bus = NULL;
2885 hsotg->driver = driver;
7d7b2292 2886 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
5b7d70c6
BD
2887 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2888
d00b4142
RB
2889 clk_enable(hsotg->clk);
2890
f65f0f10
LM
2891 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2892 hsotg->supplies);
5b7d70c6 2893 if (ret) {
f65f0f10 2894 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
5b7d70c6
BD
2895 goto err;
2896 }
2897
c816c47f 2898 s3c_hsotg_phy_enable(hsotg);
f6c01592
GH
2899 if (!IS_ERR_OR_NULL(hsotg->uphy))
2900 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
c816c47f 2901
5b9451f8
MS
2902 spin_lock_irqsave(&hsotg->lock, flags);
2903 s3c_hsotg_init(hsotg);
2904 s3c_hsotg_core_init_disconnected(hsotg);
dc6e69e6 2905 hsotg->enabled = 0;
5b9451f8
MS
2906 spin_unlock_irqrestore(&hsotg->lock, flags);
2907
5b7d70c6 2908 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
5b9451f8 2909
7ad8096e
MS
2910 mutex_unlock(&hsotg->init_mutex);
2911
5b7d70c6
BD
2912 return 0;
2913
2914err:
7ad8096e 2915 mutex_unlock(&hsotg->init_mutex);
5b7d70c6 2916 hsotg->driver = NULL;
5b7d70c6
BD
2917 return ret;
2918}
2919
8b9bc460
LM
2920/**
2921 * s3c_hsotg_udc_stop - stop the udc
2922 * @gadget: The usb gadget state
2923 * @driver: The usb gadget driver
2924 *
2925 * Stop udc hw block and stay tunned for future transmissions
2926 */
22835b80 2927static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
5b7d70c6 2928{
941fcce4 2929 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
2b19a52c 2930 unsigned long flags = 0;
5b7d70c6
BD
2931 int ep;
2932
2933 if (!hsotg)
2934 return -ENODEV;
2935
7ad8096e
MS
2936 mutex_lock(&hsotg->init_mutex);
2937
5b7d70c6 2938 /* all endpoints should be shutdown */
c6f5c050
MYK
2939 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
2940 if (hsotg->eps_in[ep])
2941 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
2942 if (hsotg->eps_out[ep])
2943 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
2944 }
5b7d70c6 2945
2b19a52c
LM
2946 spin_lock_irqsave(&hsotg->lock, flags);
2947
32805c35 2948 hsotg->driver = NULL;
5b7d70c6 2949 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
dc6e69e6 2950 hsotg->enabled = 0;
5b7d70c6 2951
2b19a52c
LM
2952 spin_unlock_irqrestore(&hsotg->lock, flags);
2953
f6c01592
GH
2954 if (!IS_ERR_OR_NULL(hsotg->uphy))
2955 otg_set_peripheral(hsotg->uphy->otg, NULL);
c816c47f
MS
2956 s3c_hsotg_phy_disable(hsotg);
2957
c8c10253 2958 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
5b7d70c6 2959
d00b4142
RB
2960 clk_disable(hsotg->clk);
2961
7ad8096e
MS
2962 mutex_unlock(&hsotg->init_mutex);
2963
5b7d70c6
BD
2964 return 0;
2965}
5b7d70c6 2966
8b9bc460
LM
2967/**
2968 * s3c_hsotg_gadget_getframe - read the frame number
2969 * @gadget: The usb gadget state
2970 *
2971 * Read the {micro} frame number
2972 */
5b7d70c6
BD
2973static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2974{
2975 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2976}
2977
a188b689
LM
2978/**
2979 * s3c_hsotg_pullup - connect/disconnect the USB PHY
2980 * @gadget: The usb gadget state
2981 * @is_on: Current state of the USB PHY
2982 *
2983 * Connect/Disconnect the USB PHY pullup
2984 */
2985static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
2986{
941fcce4 2987 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
a188b689
LM
2988 unsigned long flags = 0;
2989
d784f1e5 2990 dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
a188b689 2991
7ad8096e 2992 mutex_lock(&hsotg->init_mutex);
a188b689
LM
2993 spin_lock_irqsave(&hsotg->lock, flags);
2994 if (is_on) {
d00b4142 2995 clk_enable(hsotg->clk);
dc6e69e6 2996 hsotg->enabled = 1;
ad38dc5d 2997 s3c_hsotg_core_connect(hsotg);
a188b689 2998 } else {
5b9451f8 2999 s3c_hsotg_core_disconnect(hsotg);
dc6e69e6 3000 hsotg->enabled = 0;
d00b4142 3001 clk_disable(hsotg->clk);
a188b689
LM
3002 }
3003
3004 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3005 spin_unlock_irqrestore(&hsotg->lock, flags);
7ad8096e 3006 mutex_unlock(&hsotg->init_mutex);
a188b689
LM
3007
3008 return 0;
3009}
3010
eeef4587 3011static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
5b7d70c6 3012 .get_frame = s3c_hsotg_gadget_getframe,
f65f0f10
LM
3013 .udc_start = s3c_hsotg_udc_start,
3014 .udc_stop = s3c_hsotg_udc_stop,
a188b689 3015 .pullup = s3c_hsotg_pullup,
5b7d70c6
BD
3016};
3017
3018/**
3019 * s3c_hsotg_initep - initialise a single endpoint
3020 * @hsotg: The device state.
3021 * @hs_ep: The endpoint to be initialised.
3022 * @epnum: The endpoint number
3023 *
3024 * Initialise the given endpoint (as part of the probe and device state
3025 * creation) to give to the gadget driver. Setup the endpoint name, any
3026 * direction information and other state that may be required.
3027 */
941fcce4 3028static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
5b7d70c6 3029 struct s3c_hsotg_ep *hs_ep,
c6f5c050
MYK
3030 int epnum,
3031 bool dir_in)
5b7d70c6 3032{
5b7d70c6
BD
3033 char *dir;
3034
3035 if (epnum == 0)
3036 dir = "";
c6f5c050 3037 else if (dir_in)
5b7d70c6 3038 dir = "in";
c6f5c050
MYK
3039 else
3040 dir = "out";
5b7d70c6 3041
c6f5c050 3042 hs_ep->dir_in = dir_in;
5b7d70c6
BD
3043 hs_ep->index = epnum;
3044
3045 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3046
3047 INIT_LIST_HEAD(&hs_ep->queue);
3048 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3049
5b7d70c6
BD
3050 /* add to the list of endpoints known by the gadget driver */
3051 if (epnum)
3052 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3053
3054 hs_ep->parent = hsotg;
3055 hs_ep->ep.name = hs_ep->name;
e117e742 3056 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
5b7d70c6
BD
3057 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3058
8b9bc460
LM
3059 /*
3060 * if we're using dma, we need to set the next-endpoint pointer
5b7d70c6
BD
3061 * to be something valid.
3062 */
3063
3064 if (using_dma(hsotg)) {
47a1685f 3065 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
c6f5c050
MYK
3066 if (dir_in)
3067 writel(next, hsotg->regs + DIEPCTL(epnum));
3068 else
3069 writel(next, hsotg->regs + DOEPCTL(epnum));
5b7d70c6
BD
3070 }
3071}
3072
b3f489b2
LM
3073/**
3074 * s3c_hsotg_hw_cfg - read HW configuration registers
3075 * @param: The device state
3076 *
3077 * Read the USB core HW configuration registers
3078 */
c6f5c050 3079static int s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
5b7d70c6 3080{
c6f5c050
MYK
3081 u32 cfg;
3082 u32 ep_type;
3083 u32 i;
3084
b3f489b2 3085 /* check hardware configuration */
5b7d70c6 3086
c6f5c050
MYK
3087 cfg = readl(hsotg->regs + GHWCFG2);
3088 hsotg->num_of_eps = (cfg >> 10) & 0xF;
3089 /* Add ep0 */
3090 hsotg->num_of_eps++;
10aebc77 3091
c6f5c050
MYK
3092 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct s3c_hsotg_ep),
3093 GFP_KERNEL);
3094 if (!hsotg->eps_in[0])
3095 return -ENOMEM;
3096 /* Same s3c_hsotg_ep is used in both directions for ep0 */
3097 hsotg->eps_out[0] = hsotg->eps_in[0];
3098
3099 cfg = readl(hsotg->regs + GHWCFG1);
3100 for (i = 1; i < hsotg->num_of_eps; i++, cfg >>= 2) {
3101 ep_type = cfg & 3;
3102 /* Direction in or both */
3103 if (!(ep_type & 2)) {
3104 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
3105 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3106 if (!hsotg->eps_in[i])
3107 return -ENOMEM;
3108 }
3109 /* Direction out or both */
3110 if (!(ep_type & 1)) {
3111 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
3112 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3113 if (!hsotg->eps_out[i])
3114 return -ENOMEM;
3115 }
3116 }
3117
3118 cfg = readl(hsotg->regs + GHWCFG3);
3119 hsotg->fifo_mem = (cfg >> 16);
10aebc77 3120
c6f5c050
MYK
3121 cfg = readl(hsotg->regs + GHWCFG4);
3122 hsotg->dedicated_fifos = (cfg >> 25) & 1;
10aebc77 3123
cff9eb75
MS
3124 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3125 hsotg->num_of_eps,
3126 hsotg->dedicated_fifos ? "dedicated" : "shared",
3127 hsotg->fifo_mem);
c6f5c050 3128 return 0;
5b7d70c6
BD
3129}
3130
8b9bc460
LM
3131/**
3132 * s3c_hsotg_dump - dump state of the udc
3133 * @param: The device state
3134 */
941fcce4 3135static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
5b7d70c6 3136{
83a01804 3137#ifdef DEBUG
5b7d70c6
BD
3138 struct device *dev = hsotg->dev;
3139 void __iomem *regs = hsotg->regs;
3140 u32 val;
3141 int idx;
3142
3143 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
94cb8fd6
LM
3144 readl(regs + DCFG), readl(regs + DCTL),
3145 readl(regs + DIEPMSK));
5b7d70c6
BD
3146
3147 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
94cb8fd6 3148 readl(regs + GAHBCFG), readl(regs + 0x44));
5b7d70c6
BD
3149
3150 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
94cb8fd6 3151 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
5b7d70c6
BD
3152
3153 /* show periodic fifo settings */
3154
364f8e93 3155 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
47a1685f 3156 val = readl(regs + DPTXFSIZN(idx));
5b7d70c6 3157 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
47a1685f
DN
3158 val >> FIFOSIZE_DEPTH_SHIFT,
3159 val & FIFOSIZE_STARTADDR_MASK);
5b7d70c6
BD
3160 }
3161
364f8e93 3162 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
5b7d70c6
BD
3163 dev_info(dev,
3164 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
94cb8fd6
LM
3165 readl(regs + DIEPCTL(idx)),
3166 readl(regs + DIEPTSIZ(idx)),
3167 readl(regs + DIEPDMA(idx)));
5b7d70c6 3168
94cb8fd6 3169 val = readl(regs + DOEPCTL(idx));
5b7d70c6
BD
3170 dev_info(dev,
3171 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
94cb8fd6
LM
3172 idx, readl(regs + DOEPCTL(idx)),
3173 readl(regs + DOEPTSIZ(idx)),
3174 readl(regs + DOEPDMA(idx)));
5b7d70c6
BD
3175
3176 }
3177
3178 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
94cb8fd6 3179 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
83a01804 3180#endif
5b7d70c6
BD
3181}
3182
5b7d70c6
BD
3183/**
3184 * state_show - debugfs: show overall driver and device state.
3185 * @seq: The seq file to write to.
3186 * @v: Unused parameter.
3187 *
3188 * This debugfs entry shows the overall state of the hardware and
3189 * some general information about each of the endpoints available
3190 * to the system.
3191 */
3192static int state_show(struct seq_file *seq, void *v)
3193{
941fcce4 3194 struct dwc2_hsotg *hsotg = seq->private;
5b7d70c6
BD
3195 void __iomem *regs = hsotg->regs;
3196 int idx;
3197
3198 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
94cb8fd6
LM
3199 readl(regs + DCFG),
3200 readl(regs + DCTL),
3201 readl(regs + DSTS));
5b7d70c6
BD
3202
3203 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
94cb8fd6 3204 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
5b7d70c6
BD
3205
3206 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
94cb8fd6
LM
3207 readl(regs + GINTMSK),
3208 readl(regs + GINTSTS));
5b7d70c6
BD
3209
3210 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
94cb8fd6
LM
3211 readl(regs + DAINTMSK),
3212 readl(regs + DAINT));
5b7d70c6
BD
3213
3214 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
94cb8fd6
LM
3215 readl(regs + GNPTXSTS),
3216 readl(regs + GRXSTSR));
5b7d70c6 3217
a023da33 3218 seq_puts(seq, "\nEndpoint status:\n");
5b7d70c6 3219
364f8e93 3220 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
5b7d70c6
BD
3221 u32 in, out;
3222
94cb8fd6
LM
3223 in = readl(regs + DIEPCTL(idx));
3224 out = readl(regs + DOEPCTL(idx));
5b7d70c6
BD
3225
3226 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3227 idx, in, out);
3228
94cb8fd6
LM
3229 in = readl(regs + DIEPTSIZ(idx));
3230 out = readl(regs + DOEPTSIZ(idx));
5b7d70c6
BD
3231
3232 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3233 in, out);
3234
a023da33 3235 seq_puts(seq, "\n");
5b7d70c6
BD
3236 }
3237
3238 return 0;
3239}
3240
3241static int state_open(struct inode *inode, struct file *file)
3242{
3243 return single_open(file, state_show, inode->i_private);
3244}
3245
3246static const struct file_operations state_fops = {
3247 .owner = THIS_MODULE,
3248 .open = state_open,
3249 .read = seq_read,
3250 .llseek = seq_lseek,
3251 .release = single_release,
3252};
3253
3254/**
3255 * fifo_show - debugfs: show the fifo information
3256 * @seq: The seq_file to write data to.
3257 * @v: Unused parameter.
3258 *
3259 * Show the FIFO information for the overall fifo and all the
3260 * periodic transmission FIFOs.
8b9bc460 3261 */
5b7d70c6
BD
3262static int fifo_show(struct seq_file *seq, void *v)
3263{
941fcce4 3264 struct dwc2_hsotg *hsotg = seq->private;
5b7d70c6
BD
3265 void __iomem *regs = hsotg->regs;
3266 u32 val;
3267 int idx;
3268
a023da33 3269 seq_puts(seq, "Non-periodic FIFOs:\n");
94cb8fd6 3270 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
5b7d70c6 3271
94cb8fd6 3272 val = readl(regs + GNPTXFSIZ);
5b7d70c6 3273 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
47a1685f
DN
3274 val >> FIFOSIZE_DEPTH_SHIFT,
3275 val & FIFOSIZE_DEPTH_MASK);
5b7d70c6 3276
a023da33 3277 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
5b7d70c6 3278
364f8e93 3279 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
47a1685f 3280 val = readl(regs + DPTXFSIZN(idx));
5b7d70c6
BD
3281
3282 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
47a1685f
DN
3283 val >> FIFOSIZE_DEPTH_SHIFT,
3284 val & FIFOSIZE_STARTADDR_MASK);
5b7d70c6
BD
3285 }
3286
3287 return 0;
3288}
3289
3290static int fifo_open(struct inode *inode, struct file *file)
3291{
3292 return single_open(file, fifo_show, inode->i_private);
3293}
3294
3295static const struct file_operations fifo_fops = {
3296 .owner = THIS_MODULE,
3297 .open = fifo_open,
3298 .read = seq_read,
3299 .llseek = seq_lseek,
3300 .release = single_release,
3301};
3302
3303
3304static const char *decode_direction(int is_in)
3305{
3306 return is_in ? "in" : "out";
3307}
3308
3309/**
3310 * ep_show - debugfs: show the state of an endpoint.
3311 * @seq: The seq_file to write data to.
3312 * @v: Unused parameter.
3313 *
3314 * This debugfs entry shows the state of the given endpoint (one is
3315 * registered for each available).
8b9bc460 3316 */
5b7d70c6
BD
3317static int ep_show(struct seq_file *seq, void *v)
3318{
3319 struct s3c_hsotg_ep *ep = seq->private;
941fcce4 3320 struct dwc2_hsotg *hsotg = ep->parent;
5b7d70c6
BD
3321 struct s3c_hsotg_req *req;
3322 void __iomem *regs = hsotg->regs;
3323 int index = ep->index;
3324 int show_limit = 15;
3325 unsigned long flags;
3326
3327 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3328 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3329
3330 /* first show the register state */
3331
3332 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
94cb8fd6
LM
3333 readl(regs + DIEPCTL(index)),
3334 readl(regs + DOEPCTL(index)));
5b7d70c6
BD
3335
3336 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
94cb8fd6
LM
3337 readl(regs + DIEPDMA(index)),
3338 readl(regs + DOEPDMA(index)));
5b7d70c6
BD
3339
3340 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
94cb8fd6
LM
3341 readl(regs + DIEPINT(index)),
3342 readl(regs + DOEPINT(index)));
5b7d70c6
BD
3343
3344 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
94cb8fd6
LM
3345 readl(regs + DIEPTSIZ(index)),
3346 readl(regs + DOEPTSIZ(index)));
5b7d70c6 3347
a023da33 3348 seq_puts(seq, "\n");
5b7d70c6
BD
3349 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3350 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3351
3352 seq_printf(seq, "request list (%p,%p):\n",
3353 ep->queue.next, ep->queue.prev);
3354
22258f49 3355 spin_lock_irqsave(&hsotg->lock, flags);
5b7d70c6
BD
3356
3357 list_for_each_entry(req, &ep->queue, queue) {
3358 if (--show_limit < 0) {
a023da33 3359 seq_puts(seq, "not showing more requests...\n");
5b7d70c6
BD
3360 break;
3361 }
3362
3363 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3364 req == ep->req ? '*' : ' ',
3365 req, req->req.length, req->req.buf);
3366 seq_printf(seq, "%d done, res %d\n",
3367 req->req.actual, req->req.status);
3368 }
3369
22258f49 3370 spin_unlock_irqrestore(&hsotg->lock, flags);
5b7d70c6
BD
3371
3372 return 0;
3373}
3374
3375static int ep_open(struct inode *inode, struct file *file)
3376{
3377 return single_open(file, ep_show, inode->i_private);
3378}
3379
3380static const struct file_operations ep_fops = {
3381 .owner = THIS_MODULE,
3382 .open = ep_open,
3383 .read = seq_read,
3384 .llseek = seq_lseek,
3385 .release = single_release,
3386};
3387
3388/**
3389 * s3c_hsotg_create_debug - create debugfs directory and files
3390 * @hsotg: The driver state
3391 *
3392 * Create the debugfs files to allow the user to get information
3393 * about the state of the system. The directory name is created
3394 * with the same name as the device itself, in case we end up
3395 * with multiple blocks in future systems.
8b9bc460 3396 */
941fcce4 3397static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
3398{
3399 struct dentry *root;
3400 unsigned epidx;
3401
3402 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3403 hsotg->debug_root = root;
3404 if (IS_ERR(root)) {
3405 dev_err(hsotg->dev, "cannot create debug root\n");
3406 return;
3407 }
3408
3409 /* create general state file */
3410
3411 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3412 hsotg, &state_fops);
3413
3414 if (IS_ERR(hsotg->debug_file))
3415 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3416
3417 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3418 hsotg, &fifo_fops);
3419
3420 if (IS_ERR(hsotg->debug_fifo))
3421 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3422
c6f5c050 3423 /* Create one file for each out endpoint */
b3f489b2 3424 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
c6f5c050 3425 struct s3c_hsotg_ep *ep;
5b7d70c6 3426
c6f5c050
MYK
3427 ep = hsotg->eps_out[epidx];
3428 if (ep) {
3429 ep->debugfs = debugfs_create_file(ep->name, 0444,
3430 root, ep, &ep_fops);
5b7d70c6 3431
c6f5c050
MYK
3432 if (IS_ERR(ep->debugfs))
3433 dev_err(hsotg->dev, "failed to create %s debug file\n",
3434 ep->name);
3435 }
3436 }
3437 /* Create one file for each in endpoint. EP0 is handled with out eps */
3438 for (epidx = 1; epidx < hsotg->num_of_eps; epidx++) {
3439 struct s3c_hsotg_ep *ep;
3440
3441 ep = hsotg->eps_in[epidx];
3442 if (ep) {
3443 ep->debugfs = debugfs_create_file(ep->name, 0444,
3444 root, ep, &ep_fops);
3445
3446 if (IS_ERR(ep->debugfs))
3447 dev_err(hsotg->dev, "failed to create %s debug file\n",
3448 ep->name);
3449 }
5b7d70c6
BD
3450 }
3451}
3452
3453/**
3454 * s3c_hsotg_delete_debug - cleanup debugfs entries
3455 * @hsotg: The driver state
3456 *
3457 * Cleanup (remove) the debugfs files for use on module exit.
8b9bc460 3458 */
941fcce4 3459static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
3460{
3461 unsigned epidx;
3462
b3f489b2 3463 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
c6f5c050
MYK
3464 if (hsotg->eps_in[epidx])
3465 debugfs_remove(hsotg->eps_in[epidx]->debugfs);
3466 if (hsotg->eps_out[epidx])
3467 debugfs_remove(hsotg->eps_out[epidx]->debugfs);
5b7d70c6
BD
3468 }
3469
3470 debugfs_remove(hsotg->debug_file);
3471 debugfs_remove(hsotg->debug_fifo);
3472 debugfs_remove(hsotg->debug_root);
3473}
3474
edd74be8
GH
3475#ifdef CONFIG_OF
3476static void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg)
3477{
3478 struct device_node *np = hsotg->dev->of_node;
0a176279
GH
3479 u32 len = 0;
3480 u32 i = 0;
edd74be8
GH
3481
3482 /* Enable dma if requested in device tree */
3483 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
0a176279
GH
3484
3485 /*
3486 * Register TX periodic fifo size per endpoint.
3487 * EP0 is excluded since it has no fifo configuration.
3488 */
3489 if (!of_find_property(np, "g-tx-fifo-size", &len))
3490 goto rx_fifo;
3491
3492 len /= sizeof(u32);
3493
3494 /* Read tx fifo sizes other than ep0 */
3495 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3496 &hsotg->g_tx_fifo_sz[1], len))
3497 goto rx_fifo;
3498
3499 /* Add ep0 */
3500 len++;
3501
3502 /* Make remaining TX fifos unavailable */
3503 if (len < MAX_EPS_CHANNELS) {
3504 for (i = len; i < MAX_EPS_CHANNELS; i++)
3505 hsotg->g_tx_fifo_sz[i] = 0;
3506 }
3507
3508rx_fifo:
3509 /* Register RX fifo size */
3510 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3511
3512 /* Register NPTX fifo size */
3513 of_property_read_u32(np, "g-np-tx-fifo-size",
3514 &hsotg->g_np_g_tx_fifo_sz);
edd74be8
GH
3515}
3516#else
3517static inline void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
3518#endif
3519
8b9bc460 3520/**
117777b2
DN
3521 * dwc2_gadget_init - init function for gadget
3522 * @dwc2: The data structure for the DWC2 driver.
3523 * @irq: The IRQ number for the controller.
8b9bc460 3524 */
117777b2 3525int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
5b7d70c6 3526{
117777b2
DN
3527 struct device *dev = hsotg->dev;
3528 struct s3c_hsotg_plat *plat = dev->platform_data;
5b7d70c6
BD
3529 int epnum;
3530 int ret;
fc9a731e 3531 int i;
0a176279 3532 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
5b7d70c6 3533
1b59fc7e
KD
3534 /* Set default UTMI width */
3535 hsotg->phyif = GUSBCFG_PHYIF16;
3536
edd74be8
GH
3537 s3c_hsotg_of_probe(hsotg);
3538
0a176279
GH
3539 /* Initialize to legacy fifo configuration values */
3540 hsotg->g_rx_fifo_sz = 2048;
3541 hsotg->g_np_g_tx_fifo_sz = 1024;
3542 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3543 /* Device tree specific probe */
3544 s3c_hsotg_of_probe(hsotg);
3545 /* Dump fifo information */
3546 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3547 hsotg->g_np_g_tx_fifo_sz);
3548 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3549 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3550 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3551 hsotg->g_tx_fifo_sz[i]);
74084844 3552 /*
135b3c43
YL
3553 * If platform probe couldn't find a generic PHY or an old style
3554 * USB PHY, fall back to pdata
74084844 3555 */
135b3c43
YL
3556 if (IS_ERR_OR_NULL(hsotg->phy) && IS_ERR_OR_NULL(hsotg->uphy)) {
3557 plat = dev_get_platdata(dev);
3558 if (!plat) {
3559 dev_err(dev,
3560 "no platform data or transceiver defined\n");
3561 return -EPROBE_DEFER;
3562 }
3563 hsotg->plat = plat;
3564 } else if (hsotg->phy) {
1b59fc7e
KD
3565 /*
3566 * If using the generic PHY framework, check if the PHY bus
3567 * width is 8-bit and set the phyif appropriately.
3568 */
135b3c43 3569 if (phy_get_bus_width(hsotg->phy) == 8)
1b59fc7e
KD
3570 hsotg->phyif = GUSBCFG_PHYIF8;
3571 }
b2e587db 3572
117777b2 3573 hsotg->clk = devm_clk_get(dev, "otg");
31ee04de 3574 if (IS_ERR(hsotg->clk)) {
8d736d8a 3575 hsotg->clk = NULL;
f415fbd1 3576 dev_dbg(dev, "cannot get otg clock\n");
5b7d70c6
BD
3577 }
3578
d327ab5b 3579 hsotg->gadget.max_speed = USB_SPEED_HIGH;
5b7d70c6
BD
3580 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3581 hsotg->gadget.name = dev_name(dev);
5b7d70c6 3582
5b7d70c6
BD
3583 /* reset the system */
3584
f415fbd1
DN
3585 ret = clk_prepare_enable(hsotg->clk);
3586 if (ret) {
3587 dev_err(dev, "failed to enable otg clk\n");
3588 goto err_clk;
3589 }
3590
31ee04de 3591
fc9a731e
LM
3592 /* regulators */
3593
3594 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3595 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3596
cd76213e 3597 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
fc9a731e
LM
3598 hsotg->supplies);
3599 if (ret) {
3600 dev_err(dev, "failed to request supplies: %d\n", ret);
338edabc 3601 goto err_clk;
fc9a731e
LM
3602 }
3603
3604 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3605 hsotg->supplies);
3606
3607 if (ret) {
941fcce4 3608 dev_err(dev, "failed to enable supplies: %d\n", ret);
c139ec27 3609 goto err_clk;
fc9a731e
LM
3610 }
3611
41188786
LM
3612 /* usb phy enable */
3613 s3c_hsotg_phy_enable(hsotg);
5b7d70c6 3614
5b7d70c6 3615 s3c_hsotg_corereset(hsotg);
c6f5c050
MYK
3616 ret = s3c_hsotg_hw_cfg(hsotg);
3617 if (ret) {
3618 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
3619 goto err_clk;
3620 }
3621
cff9eb75 3622 s3c_hsotg_init(hsotg);
b3f489b2 3623
3f95001d
MYK
3624 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3625 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3626 if (!hsotg->ctrl_buff) {
3627 dev_err(dev, "failed to allocate ctrl request buff\n");
3628 ret = -ENOMEM;
3629 goto err_supplies;
3630 }
3631
3632 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3633 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3634 if (!hsotg->ep0_buff) {
3635 dev_err(dev, "failed to allocate ctrl reply buff\n");
3636 ret = -ENOMEM;
3637 goto err_supplies;
3638 }
3639
db8178c3
DN
3640 ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
3641 dev_name(hsotg->dev), hsotg);
eb3c56c5
MS
3642 if (ret < 0) {
3643 s3c_hsotg_phy_disable(hsotg);
3644 clk_disable_unprepare(hsotg->clk);
3645 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3646 hsotg->supplies);
db8178c3 3647 dev_err(dev, "cannot claim IRQ for gadget\n");
c139ec27 3648 goto err_supplies;
eb3c56c5
MS
3649 }
3650
b3f489b2
LM
3651 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3652
3653 if (hsotg->num_of_eps == 0) {
3654 dev_err(dev, "wrong number of EPs (zero)\n");
dfdda5a0 3655 ret = -EINVAL;
b3f489b2
LM
3656 goto err_supplies;
3657 }
3658
b3f489b2
LM
3659 /* setup endpoint information */
3660
3661 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
c6f5c050 3662 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
b3f489b2
LM
3663
3664 /* allocate EP0 request */
3665
c6f5c050 3666 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
b3f489b2
LM
3667 GFP_KERNEL);
3668 if (!hsotg->ctrl_req) {
3669 dev_err(dev, "failed to allocate ctrl req\n");
dfdda5a0 3670 ret = -ENOMEM;
c6f5c050 3671 goto err_supplies;
b3f489b2 3672 }
5b7d70c6
BD
3673
3674 /* initialise the endpoints now the core has been initialised */
c6f5c050
MYK
3675 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3676 if (hsotg->eps_in[epnum])
3677 s3c_hsotg_initep(hsotg, hsotg->eps_in[epnum],
3678 epnum, 1);
3679 if (hsotg->eps_out[epnum])
3680 s3c_hsotg_initep(hsotg, hsotg->eps_out[epnum],
3681 epnum, 0);
3682 }
5b7d70c6 3683
f65f0f10 3684 /* disable power and clock */
3a8146aa 3685 s3c_hsotg_phy_disable(hsotg);
f65f0f10
LM
3686
3687 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3688 hsotg->supplies);
3689 if (ret) {
117777b2 3690 dev_err(dev, "failed to disable supplies: %d\n", ret);
c6f5c050 3691 goto err_supplies;
f65f0f10
LM
3692 }
3693
117777b2 3694 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
0f91349b 3695 if (ret)
c6f5c050 3696 goto err_supplies;
0f91349b 3697
5b7d70c6
BD
3698 s3c_hsotg_create_debug(hsotg);
3699
3700 s3c_hsotg_dump(hsotg);
3701
5b7d70c6
BD
3702 return 0;
3703
fc9a731e 3704err_supplies:
41188786 3705 s3c_hsotg_phy_disable(hsotg);
31ee04de 3706err_clk:
1d144c67 3707 clk_disable_unprepare(hsotg->clk);
338edabc 3708
5b7d70c6
BD
3709 return ret;
3710}
117777b2 3711EXPORT_SYMBOL_GPL(dwc2_gadget_init);
5b7d70c6 3712
8b9bc460
LM
3713/**
3714 * s3c_hsotg_remove - remove function for hsotg driver
3715 * @pdev: The platform information for the driver
3716 */
117777b2 3717int s3c_hsotg_remove(struct dwc2_hsotg *hsotg)
5b7d70c6 3718{
0f91349b 3719 usb_del_gadget_udc(&hsotg->gadget);
5b7d70c6 3720 s3c_hsotg_delete_debug(hsotg);
04b4a0fc 3721 clk_disable_unprepare(hsotg->clk);
31ee04de 3722
5b7d70c6
BD
3723 return 0;
3724}
117777b2 3725EXPORT_SYMBOL_GPL(s3c_hsotg_remove);
5b7d70c6 3726
117777b2 3727int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
b83e333a 3728{
b83e333a
MS
3729 unsigned long flags;
3730 int ret = 0;
3731
7ad8096e
MS
3732 mutex_lock(&hsotg->init_mutex);
3733
dc6e69e6
MS
3734 if (hsotg->driver) {
3735 int ep;
3736
b83e333a
MS
3737 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3738 hsotg->driver->driver.name);
3739
dc6e69e6
MS
3740 spin_lock_irqsave(&hsotg->lock, flags);
3741 if (hsotg->enabled)
3742 s3c_hsotg_core_disconnect(hsotg);
3743 s3c_hsotg_disconnect(hsotg);
3744 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3745 spin_unlock_irqrestore(&hsotg->lock, flags);
b83e333a 3746
dc6e69e6 3747 s3c_hsotg_phy_disable(hsotg);
b83e333a 3748
c6f5c050
MYK
3749 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3750 if (hsotg->eps_in[ep])
3751 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3752 if (hsotg->eps_out[ep])
3753 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3754 }
b83e333a
MS
3755
3756 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3757 hsotg->supplies);
d00b4142 3758 clk_disable(hsotg->clk);
b83e333a
MS
3759 }
3760
7ad8096e
MS
3761 mutex_unlock(&hsotg->init_mutex);
3762
b83e333a
MS
3763 return ret;
3764}
117777b2 3765EXPORT_SYMBOL_GPL(s3c_hsotg_suspend);
b83e333a 3766
117777b2 3767int s3c_hsotg_resume(struct dwc2_hsotg *hsotg)
b83e333a 3768{
b83e333a
MS
3769 unsigned long flags;
3770 int ret = 0;
3771
7ad8096e
MS
3772 mutex_lock(&hsotg->init_mutex);
3773
b83e333a
MS
3774 if (hsotg->driver) {
3775 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3776 hsotg->driver->driver.name);
d00b4142
RB
3777
3778 clk_enable(hsotg->clk);
b83e333a 3779 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
dc6e69e6 3780 hsotg->supplies);
b83e333a 3781
dc6e69e6 3782 s3c_hsotg_phy_enable(hsotg);
b83e333a 3783
dc6e69e6
MS
3784 spin_lock_irqsave(&hsotg->lock, flags);
3785 s3c_hsotg_core_init_disconnected(hsotg);
3786 if (hsotg->enabled)
3787 s3c_hsotg_core_connect(hsotg);
3788 spin_unlock_irqrestore(&hsotg->lock, flags);
3789 }
7ad8096e 3790 mutex_unlock(&hsotg->init_mutex);
b83e333a
MS
3791
3792 return ret;
3793}
117777b2 3794EXPORT_SYMBOL_GPL(s3c_hsotg_resume);