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