blob: 03bc479d04e0d63cb90c8009c3dd52e952c69bb7 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/**
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
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.
15 */
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/mutex.h>
24#include <linux/seq_file.h>
25#include <linux/delay.h>
26#include <linux/io.h>
27#include <linux/slab.h>
28#include <linux/of_platform.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32#include <linux/usb/phy.h>
33
34#include "core.h"
35#include "hw.h"
36
37/* conversion functions */
38static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
39{
40 return container_of(req, struct dwc2_hsotg_req, req);
41}
42
43static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
44{
45 return container_of(ep, struct dwc2_hsotg_ep, ep);
46}
47
48static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
49{
50 return container_of(gadget, struct dwc2_hsotg, gadget);
51}
52
53static inline void __orr32(void __iomem *ptr, u32 val)
54{
55 dwc2_writel(dwc2_readl(ptr) | val, ptr);
56}
57
58static inline void __bic32(void __iomem *ptr, u32 val)
59{
60 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
61}
62
63static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
64 u32 ep_index, u32 dir_in)
65{
66 if (dir_in)
67 return hsotg->eps_in[ep_index];
68 else
69 return hsotg->eps_out[ep_index];
70}
71
72/* forward declaration of functions */
73static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
74
75/**
76 * using_dma - return the DMA status of the driver.
77 * @hsotg: The driver state.
78 *
79 * Return true if we're using DMA.
80 *
81 * Currently, we have the DMA support code worked into everywhere
82 * that needs it, but the AMBA DMA implementation in the hardware can
83 * only DMA from 32bit aligned addresses. This means that gadgets such
84 * as the CDC Ethernet cannot work as they often pass packets which are
85 * not 32bit aligned.
86 *
87 * Unfortunately the choice to use DMA or not is global to the controller
88 * and seems to be only settable when the controller is being put through
89 * a core reset. This means we either need to fix the gadgets to take
90 * account of DMA alignment, or add bounce buffers (yuerk).
91 *
92 * g_using_dma is set depending on dts flag.
93 */
94static inline bool using_dma(struct dwc2_hsotg *hsotg)
95{
96 return hsotg->params.g_dma;
97}
98
99/*
100 * using_desc_dma - return the descriptor DMA status of the driver.
101 * @hsotg: The driver state.
102 *
103 * Return true if we're using descriptor DMA.
104 */
105static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
106{
107 return hsotg->params.g_dma_desc;
108}
109
110/**
111 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
112 * @hs_ep: The endpoint
113 * @increment: The value to increment by
114 *
115 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
116 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
117 */
118static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
119{
120 hs_ep->target_frame += hs_ep->interval;
121 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
122 hs_ep->frame_overrun = 1;
123 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
124 } else {
125 hs_ep->frame_overrun = 0;
126 }
127}
128
129/**
130 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
131 * @hsotg: The device state
132 * @ints: A bitmask of the interrupts to enable
133 */
134static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
135{
136 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
137 u32 new_gsintmsk;
138
139 new_gsintmsk = gsintmsk | ints;
140
141 if (new_gsintmsk != gsintmsk) {
142 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
143 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
144 }
145}
146
147/**
148 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
149 * @hsotg: The device state
150 * @ints: A bitmask of the interrupts to enable
151 */
152static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
153{
154 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
155 u32 new_gsintmsk;
156
157 new_gsintmsk = gsintmsk & ~ints;
158
159 if (new_gsintmsk != gsintmsk)
160 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
161}
162
163/**
164 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
165 * @hsotg: The device state
166 * @ep: The endpoint index
167 * @dir_in: True if direction is in.
168 * @en: The enable value, true to enable
169 *
170 * Set or clear the mask for an individual endpoint's interrupt
171 * request.
172 */
173static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
174 unsigned int ep, unsigned int dir_in,
175 unsigned int en)
176{
177 unsigned long flags;
178 u32 bit = 1 << ep;
179 u32 daint;
180
181 if (!dir_in)
182 bit <<= 16;
183
184 local_irq_save(flags);
185 daint = dwc2_readl(hsotg->regs + DAINTMSK);
186 if (en)
187 daint |= bit;
188 else
189 daint &= ~bit;
190 dwc2_writel(daint, hsotg->regs + DAINTMSK);
191 local_irq_restore(flags);
192}
193
194/**
195 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
196 */
197int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
198{
199 if (hsotg->hw_params.en_multiple_tx_fifo)
200 /* In dedicated FIFO mode we need count of IN EPs */
201 return (dwc2_readl(hsotg->regs + GHWCFG4) &
202 GHWCFG4_NUM_IN_EPS_MASK) >> GHWCFG4_NUM_IN_EPS_SHIFT;
203 else
204 /* In shared FIFO mode we need count of Periodic IN EPs */
205 return hsotg->hw_params.num_dev_perio_in_ep;
206}
207
208/**
209 * dwc2_hsotg_ep_info_size - return Endpoint Info Control block size in DWORDs
210 */
211static int dwc2_hsotg_ep_info_size(struct dwc2_hsotg *hsotg)
212{
213 int val = 0;
214 int i;
215 u32 ep_dirs;
216
217 /*
218 * Don't need additional space for ep info control registers in
219 * slave mode.
220 */
221 if (!using_dma(hsotg)) {
222 dev_dbg(hsotg->dev, "Buffer DMA ep info size 0\n");
223 return 0;
224 }
225
226 /*
227 * Buffer DMA mode - 1 location per endpoit
228 * Descriptor DMA mode - 4 locations per endpoint
229 */
230 ep_dirs = hsotg->hw_params.dev_ep_dirs;
231
232 for (i = 0; i <= hsotg->hw_params.num_dev_ep; i++) {
233 val += ep_dirs & 3 ? 1 : 2;
234 ep_dirs >>= 2;
235 }
236
237 if (using_desc_dma(hsotg))
238 val = val * 4;
239
240 return val;
241}
242
243/**
244 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
245 * device mode TX FIFOs
246 */
247int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
248{
249 int ep_info_size;
250 int addr;
251 int tx_addr_max;
252 u32 np_tx_fifo_size;
253
254 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
255 hsotg->params.g_np_tx_fifo_size);
256
257 /* Get Endpoint Info Control block size in DWORDs. */
258 ep_info_size = dwc2_hsotg_ep_info_size(hsotg);
259 tx_addr_max = hsotg->hw_params.total_fifo_size - ep_info_size;
260
261 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
262 if (tx_addr_max <= addr)
263 return 0;
264
265 return tx_addr_max - addr;
266}
267
268/**
269 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
270 * TX FIFOs
271 */
272int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
273{
274 int tx_fifo_count;
275 int tx_fifo_depth;
276
277 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
278
279 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
280
281 if (!tx_fifo_count)
282 return tx_fifo_depth;
283 else
284 return tx_fifo_depth / tx_fifo_count;
285}
286
287/**
288 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
289 * @hsotg: The device instance.
290 */
291static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
292{
293 unsigned int ep;
294 unsigned int addr;
295 int timeout;
296 u32 val;
297 u32 *txfsz = hsotg->params.g_tx_fifo_size;
298
299 /* Reset fifo map if not correctly cleared during previous session */
300 WARN_ON(hsotg->fifo_map);
301 hsotg->fifo_map = 0;
302
303 /* set RX/NPTX FIFO sizes */
304 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
305 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
306 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
307 hsotg->regs + GNPTXFSIZ);
308
309 /*
310 * arange all the rest of the TX FIFOs, as some versions of this
311 * block have overlapping default addresses. This also ensures
312 * that if the settings have been changed, then they are set to
313 * known values.
314 */
315
316 /* start at the end of the GNPTXFSIZ, rounded up */
317 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
318
319 /*
320 * Configure fifos sizes from provided configuration and assign
321 * them to endpoints dynamically according to maxpacket size value of
322 * given endpoint.
323 */
324 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
325 if (!txfsz[ep])
326 continue;
327 val = addr;
328 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
329 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
330 "insufficient fifo memory");
331 addr += txfsz[ep];
332
333 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
334 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
335 }
336
337 dwc2_writel(hsotg->hw_params.total_fifo_size |
338 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
339 hsotg->regs + GDFIFOCFG);
340 /*
341 * according to p428 of the design guide, we need to ensure that
342 * all fifos are flushed before continuing
343 */
344
345 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
346 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
347
348 /* wait until the fifos are both flushed */
349 timeout = 100;
350 while (1) {
351 val = dwc2_readl(hsotg->regs + GRSTCTL);
352
353 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
354 break;
355
356 if (--timeout == 0) {
357 dev_err(hsotg->dev,
358 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
359 __func__, val);
360 break;
361 }
362
363 udelay(1);
364 }
365
366 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
367}
368
369/**
370 * @ep: USB endpoint to allocate request for.
371 * @flags: Allocation flags
372 *
373 * Allocate a new USB request structure appropriate for the specified endpoint
374 */
375static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
376 gfp_t flags)
377{
378 struct dwc2_hsotg_req *req;
379
380 req = kzalloc(sizeof(*req), flags);
381 if (!req)
382 return NULL;
383
384 INIT_LIST_HEAD(&req->queue);
385
386 return &req->req;
387}
388
389/**
390 * is_ep_periodic - return true if the endpoint is in periodic mode.
391 * @hs_ep: The endpoint to query.
392 *
393 * Returns true if the endpoint is in periodic mode, meaning it is being
394 * used for an Interrupt or ISO transfer.
395 */
396static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
397{
398 return hs_ep->periodic;
399}
400
401/**
402 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
403 * @hsotg: The device state.
404 * @hs_ep: The endpoint for the request
405 * @hs_req: The request being processed.
406 *
407 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
408 * of a request to ensure the buffer is ready for access by the caller.
409 */
410static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
411 struct dwc2_hsotg_ep *hs_ep,
412 struct dwc2_hsotg_req *hs_req)
413{
414 struct usb_request *req = &hs_req->req;
415
416 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
417}
418
419/*
420 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
421 * for Control endpoint
422 * @hsotg: The device state.
423 *
424 * This function will allocate 4 descriptor chains for EP 0: 2 for
425 * Setup stage, per one for IN and OUT data/status transactions.
426 */
427static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
428{
429 hsotg->setup_desc[0] =
430 dmam_alloc_coherent(hsotg->dev,
431 sizeof(struct dwc2_dma_desc),
432 &hsotg->setup_desc_dma[0],
433 GFP_KERNEL);
434 if (!hsotg->setup_desc[0])
435 goto fail;
436
437 hsotg->setup_desc[1] =
438 dmam_alloc_coherent(hsotg->dev,
439 sizeof(struct dwc2_dma_desc),
440 &hsotg->setup_desc_dma[1],
441 GFP_KERNEL);
442 if (!hsotg->setup_desc[1])
443 goto fail;
444
445 hsotg->ctrl_in_desc =
446 dmam_alloc_coherent(hsotg->dev,
447 sizeof(struct dwc2_dma_desc),
448 &hsotg->ctrl_in_desc_dma,
449 GFP_KERNEL);
450 if (!hsotg->ctrl_in_desc)
451 goto fail;
452
453 hsotg->ctrl_out_desc =
454 dmam_alloc_coherent(hsotg->dev,
455 sizeof(struct dwc2_dma_desc),
456 &hsotg->ctrl_out_desc_dma,
457 GFP_KERNEL);
458 if (!hsotg->ctrl_out_desc)
459 goto fail;
460
461 return 0;
462
463fail:
464 return -ENOMEM;
465}
466
467/**
468 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
469 * @hsotg: The controller state.
470 * @hs_ep: The endpoint we're going to write for.
471 * @hs_req: The request to write data for.
472 *
473 * This is called when the TxFIFO has some space in it to hold a new
474 * transmission and we have something to give it. The actual setup of
475 * the data size is done elsewhere, so all we have to do is to actually
476 * write the data.
477 *
478 * The return value is zero if there is more space (or nothing was done)
479 * otherwise -ENOSPC is returned if the FIFO space was used up.
480 *
481 * This routine is only needed for PIO
482 */
483static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
484 struct dwc2_hsotg_ep *hs_ep,
485 struct dwc2_hsotg_req *hs_req)
486{
487 bool periodic = is_ep_periodic(hs_ep);
488 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
489 int buf_pos = hs_req->req.actual;
490 int to_write = hs_ep->size_loaded;
491 void *data;
492 int can_write;
493 int pkt_round;
494 int max_transfer;
495
496 to_write -= (buf_pos - hs_ep->last_load);
497
498 /* if there's nothing to write, get out early */
499 if (to_write == 0)
500 return 0;
501
502 if (periodic && !hsotg->dedicated_fifos) {
503 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
504 int size_left;
505 int size_done;
506
507 /*
508 * work out how much data was loaded so we can calculate
509 * how much data is left in the fifo.
510 */
511
512 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
513
514 /*
515 * if shared fifo, we cannot write anything until the
516 * previous data has been completely sent.
517 */
518 if (hs_ep->fifo_load != 0) {
519 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
520 return -ENOSPC;
521 }
522
523 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
524 __func__, size_left,
525 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
526
527 /* how much of the data has moved */
528 size_done = hs_ep->size_loaded - size_left;
529
530 /* how much data is left in the fifo */
531 can_write = hs_ep->fifo_load - size_done;
532 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
533 __func__, can_write);
534
535 can_write = hs_ep->fifo_size - can_write;
536 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
537 __func__, can_write);
538
539 if (can_write <= 0) {
540 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
541 return -ENOSPC;
542 }
543 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
544 can_write = dwc2_readl(hsotg->regs +
545 DTXFSTS(hs_ep->fifo_index));
546
547 can_write &= 0xffff;
548 can_write *= 4;
549 } else {
550 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
551 dev_dbg(hsotg->dev,
552 "%s: no queue slots available (0x%08x)\n",
553 __func__, gnptxsts);
554
555 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
556 return -ENOSPC;
557 }
558
559 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
560 can_write *= 4; /* fifo size is in 32bit quantities. */
561 }
562
563 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
564
565 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
566 __func__, gnptxsts, can_write, to_write, max_transfer);
567
568 /*
569 * limit to 512 bytes of data, it seems at least on the non-periodic
570 * FIFO, requests of >512 cause the endpoint to get stuck with a
571 * fragment of the end of the transfer in it.
572 */
573 if (can_write > 512 && !periodic)
574 can_write = 512;
575
576 /*
577 * limit the write to one max-packet size worth of data, but allow
578 * the transfer to return that it did not run out of fifo space
579 * doing it.
580 */
581 if (to_write > max_transfer) {
582 to_write = max_transfer;
583
584 /* it's needed only when we do not use dedicated fifos */
585 if (!hsotg->dedicated_fifos)
586 dwc2_hsotg_en_gsint(hsotg,
587 periodic ? GINTSTS_PTXFEMP :
588 GINTSTS_NPTXFEMP);
589 }
590
591 /* see if we can write data */
592
593 if (to_write > can_write) {
594 to_write = can_write;
595 pkt_round = to_write % max_transfer;
596
597 /*
598 * Round the write down to an
599 * exact number of packets.
600 *
601 * Note, we do not currently check to see if we can ever
602 * write a full packet or not to the FIFO.
603 */
604
605 if (pkt_round)
606 to_write -= pkt_round;
607
608 /*
609 * enable correct FIFO interrupt to alert us when there
610 * is more room left.
611 */
612
613 /* it's needed only when we do not use dedicated fifos */
614 if (!hsotg->dedicated_fifos)
615 dwc2_hsotg_en_gsint(hsotg,
616 periodic ? GINTSTS_PTXFEMP :
617 GINTSTS_NPTXFEMP);
618 }
619
620 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
621 to_write, hs_req->req.length, can_write, buf_pos);
622
623 if (to_write <= 0)
624 return -ENOSPC;
625
626 hs_req->req.actual = buf_pos + to_write;
627 hs_ep->total_data += to_write;
628
629 if (periodic)
630 hs_ep->fifo_load += to_write;
631
632 to_write = DIV_ROUND_UP(to_write, 4);
633 data = hs_req->req.buf + buf_pos;
634
635 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
636
637 return (to_write >= can_write) ? -ENOSPC : 0;
638}
639
640/**
641 * get_ep_limit - get the maximum data legnth for this endpoint
642 * @hs_ep: The endpoint
643 *
644 * Return the maximum data that can be queued in one go on a given endpoint
645 * so that transfers that are too long can be split.
646 */
647static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
648{
649 int index = hs_ep->index;
650 unsigned int maxsize;
651 unsigned int maxpkt;
652
653 if (index != 0) {
654 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
655 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
656 } else {
657 maxsize = 64 + 64;
658 if (hs_ep->dir_in)
659 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
660 else
661 maxpkt = 2;
662 }
663
664 /* we made the constant loading easier above by using +1 */
665 maxpkt--;
666 maxsize--;
667
668 /*
669 * constrain by packet count if maxpkts*pktsize is greater
670 * than the length register size.
671 */
672
673 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
674 maxsize = maxpkt * hs_ep->ep.maxpacket;
675
676 return maxsize;
677}
678
679/**
680 * dwc2_hsotg_read_frameno - read current frame number
681 * @hsotg: The device instance
682 *
683 * Return the current frame number
684 */
685static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
686{
687 u32 dsts;
688
689 dsts = dwc2_readl(hsotg->regs + DSTS);
690 dsts &= DSTS_SOFFN_MASK;
691 dsts >>= DSTS_SOFFN_SHIFT;
692
693 return dsts;
694}
695
696/**
697 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
698 * DMA descriptor chain prepared for specific endpoint
699 * @hs_ep: The endpoint
700 *
701 * Return the maximum data that can be queued in one go on a given endpoint
702 * depending on its descriptor chain capacity so that transfers that
703 * are too long can be split.
704 */
705static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
706{
707 int is_isoc = hs_ep->isochronous;
708 unsigned int maxsize;
709
710 if (is_isoc)
711 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
712 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
713 else
714 maxsize = DEV_DMA_NBYTES_LIMIT;
715
716 /* Above size of one descriptor was chosen, multiple it */
717 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
718
719 return maxsize;
720}
721
722/*
723 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
724 * @hs_ep: The endpoint
725 * @mask: RX/TX bytes mask to be defined
726 *
727 * Returns maximum data payload for one descriptor after analyzing endpoint
728 * characteristics.
729 * DMA descriptor transfer bytes limit depends on EP type:
730 * Control out - MPS,
731 * Isochronous - descriptor rx/tx bytes bitfield limit,
732 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
733 * have concatenations from various descriptors within one packet.
734 *
735 * Selects corresponding mask for RX/TX bytes as well.
736 */
737static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
738{
739 u32 mps = hs_ep->ep.maxpacket;
740 int dir_in = hs_ep->dir_in;
741 u32 desc_size = 0;
742
743 if (!hs_ep->index && !dir_in) {
744 desc_size = mps;
745 *mask = DEV_DMA_NBYTES_MASK;
746 } else if (hs_ep->isochronous) {
747 if (dir_in) {
748 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
749 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
750 } else {
751 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
752 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
753 }
754 } else {
755 desc_size = DEV_DMA_NBYTES_LIMIT;
756 *mask = DEV_DMA_NBYTES_MASK;
757
758 /* Round down desc_size to be mps multiple */
759 desc_size -= desc_size % mps;
760 }
761
762 return desc_size;
763}
764
765/*
766 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
767 * @hs_ep: The endpoint
768 * @dma_buff: DMA address to use
769 * @len: Length of the transfer
770 *
771 * This function will iterate over descriptor chain and fill its entries
772 * with corresponding information based on transfer data.
773 */
774static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
775 dma_addr_t dma_buff,
776 unsigned int len)
777{
778 struct dwc2_hsotg *hsotg = hs_ep->parent;
779 int dir_in = hs_ep->dir_in;
780 struct dwc2_dma_desc *desc = hs_ep->desc_list;
781 u32 mps = hs_ep->ep.maxpacket;
782 u32 maxsize = 0;
783 u32 offset = 0;
784 u32 mask = 0;
785 int i;
786
787 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
788
789 hs_ep->desc_count = (len / maxsize) +
790 ((len % maxsize) ? 1 : 0);
791 if (len == 0)
792 hs_ep->desc_count = 1;
793
794 for (i = 0; i < hs_ep->desc_count; ++i) {
795 desc->status = 0;
796 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
797 << DEV_DMA_BUFF_STS_SHIFT);
798
799 if (len > maxsize) {
800 if (!hs_ep->index && !dir_in)
801 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
802
803 desc->status |= (maxsize <<
804 DEV_DMA_NBYTES_SHIFT & mask);
805 desc->buf = dma_buff + offset;
806
807 len -= maxsize;
808 offset += maxsize;
809 } else {
810 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
811
812 if (dir_in)
813 desc->status |= (len % mps) ? DEV_DMA_SHORT :
814 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
815 if (len > maxsize)
816 dev_err(hsotg->dev, "wrong len %d\n", len);
817
818 desc->status |=
819 len << DEV_DMA_NBYTES_SHIFT & mask;
820 desc->buf = dma_buff + offset;
821 }
822
823 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
824 desc->status |= (DEV_DMA_BUFF_STS_HREADY
825 << DEV_DMA_BUFF_STS_SHIFT);
826 desc++;
827 }
828}
829
830/*
831 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
832 * @hs_ep: The isochronous endpoint.
833 * @dma_buff: usb requests dma buffer.
834 * @len: usb request transfer length.
835 *
836 * Finds out index of first free entry either in the bottom or up half of
837 * descriptor chain depend on which is under SW control and not processed
838 * by HW. Then fills that descriptor with the data of the arrived usb request,
839 * frame info, sets Last and IOC bits increments next_desc. If filled
840 * descriptor is not the first one, removes L bit from the previous descriptor
841 * status.
842 */
843static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
844 dma_addr_t dma_buff, unsigned int len)
845{
846 struct dwc2_dma_desc *desc;
847 struct dwc2_hsotg *hsotg = hs_ep->parent;
848 u32 index;
849 u32 maxsize = 0;
850 u32 mask = 0;
851 u8 pid = 0;
852
853 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
854 if (len > maxsize) {
855 dev_err(hsotg->dev, "wrong len %d\n", len);
856 return -EINVAL;
857 }
858
859 /*
860 * If SW has already filled half of chain, then return and wait for
861 * the other chain to be processed by HW.
862 */
863 if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
864 return -EBUSY;
865
866 /* Increment frame number by interval for IN */
867 if (hs_ep->dir_in)
868 dwc2_gadget_incr_frame_num(hs_ep);
869
870 index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
871 hs_ep->next_desc;
872
873 /* Sanity check of calculated index */
874 if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
875 (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
876 dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
877 return -EINVAL;
878 }
879
880 desc = &hs_ep->desc_list[index];
881
882 /* Clear L bit of previous desc if more than one entries in the chain */
883 if (hs_ep->next_desc)
884 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
885
886 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
887 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
888
889 desc->status = 0;
890 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
891
892 desc->buf = dma_buff;
893 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
894 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
895
896 if (hs_ep->dir_in) {
897 if (len)
898 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
899 else
900 pid = 1;
901 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
902 DEV_DMA_ISOC_PID_MASK) |
903 ((len % hs_ep->ep.maxpacket) ?
904 DEV_DMA_SHORT : 0) |
905 ((hs_ep->target_frame <<
906 DEV_DMA_ISOC_FRNUM_SHIFT) &
907 DEV_DMA_ISOC_FRNUM_MASK);
908 }
909
910 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
911 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
912
913 /* Update index of last configured entry in the chain */
914 hs_ep->next_desc++;
915
916 return 0;
917}
918
919/*
920 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
921 * @hs_ep: The isochronous endpoint.
922 *
923 * Prepare first descriptor chain for isochronous endpoints. Afterwards
924 * write DMA address to HW and enable the endpoint.
925 *
926 * Switch between descriptor chains via isoc_chain_num to give SW opportunity
927 * to prepare second descriptor chain while first one is being processed by HW.
928 */
929static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
930{
931 struct dwc2_hsotg *hsotg = hs_ep->parent;
932 struct dwc2_hsotg_req *hs_req, *treq;
933 int index = hs_ep->index;
934 int ret;
935 u32 dma_reg;
936 u32 depctl;
937 u32 ctrl;
938
939 if (list_empty(&hs_ep->queue)) {
940 hs_ep->target_frame = TARGET_FRAME_INITIAL;
941 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
942 return;
943 }
944
945 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
946 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
947 hs_req->req.length);
948 if (ret) {
949 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
950 break;
951 }
952 }
953
954 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
955 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
956
957 /* write descriptor chain address to control register */
958 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
959
960 ctrl = dwc2_readl(hsotg->regs + depctl);
961 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
962 dwc2_writel(ctrl, hsotg->regs + depctl);
963
964 /* Switch ISOC descriptor chain number being processed by SW*/
965 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
966 hs_ep->next_desc = 0;
967}
968
969/**
970 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
971 * @hsotg: The controller state.
972 * @hs_ep: The endpoint to process a request for
973 * @hs_req: The request to start.
974 * @continuing: True if we are doing more for the current request.
975 *
976 * Start the given request running by setting the endpoint registers
977 * appropriately, and writing any data to the FIFOs.
978 */
979static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
980 struct dwc2_hsotg_ep *hs_ep,
981 struct dwc2_hsotg_req *hs_req,
982 bool continuing)
983{
984 struct usb_request *ureq = &hs_req->req;
985 int index = hs_ep->index;
986 int dir_in = hs_ep->dir_in;
987 u32 epctrl_reg;
988 u32 epsize_reg;
989 u32 epsize;
990 u32 ctrl;
991 unsigned int length;
992 unsigned int packets;
993 unsigned int maxreq;
994 unsigned int dma_reg;
995
996 if (index != 0) {
997 if (hs_ep->req && !continuing) {
998 dev_err(hsotg->dev, "%s: active request\n", __func__);
999 WARN_ON(1);
1000 return;
1001 } else if (hs_ep->req != hs_req && continuing) {
1002 dev_err(hsotg->dev,
1003 "%s: continue different req\n", __func__);
1004 WARN_ON(1);
1005 return;
1006 }
1007 }
1008
1009 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
1010 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
1011 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1012
1013 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
1014 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
1015 hs_ep->dir_in ? "in" : "out");
1016
1017 /* If endpoint is stalled, we will restart request later */
1018 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
1019
1020 if (index && ctrl & DXEPCTL_STALL) {
1021 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1022 return;
1023 }
1024
1025 length = ureq->length - ureq->actual;
1026 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1027 ureq->length, ureq->actual);
1028
1029 if (!using_desc_dma(hsotg))
1030 maxreq = get_ep_limit(hs_ep);
1031 else
1032 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1033
1034 if (length > maxreq) {
1035 int round = maxreq % hs_ep->ep.maxpacket;
1036
1037 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1038 __func__, length, maxreq, round);
1039
1040 /* round down to multiple of packets */
1041 if (round)
1042 maxreq -= round;
1043
1044 length = maxreq;
1045 }
1046
1047 if (length)
1048 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1049 else
1050 packets = 1; /* send one packet if length is zero. */
1051
1052 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1053 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1054 return;
1055 }
1056
1057 if (dir_in && index != 0)
1058 if (hs_ep->isochronous)
1059 epsize = DXEPTSIZ_MC(packets);
1060 else
1061 epsize = DXEPTSIZ_MC(1);
1062 else
1063 epsize = 0;
1064
1065 /*
1066 * zero length packet should be programmed on its own and should not
1067 * be counted in DIEPTSIZ.PktCnt with other packets.
1068 */
1069 if (dir_in && ureq->zero && !continuing) {
1070 /* Test if zlp is actually required. */
1071 if ((ureq->length >= hs_ep->ep.maxpacket) &&
1072 !(ureq->length % hs_ep->ep.maxpacket))
1073 hs_ep->send_zlp = 1;
1074 }
1075
1076 epsize |= DXEPTSIZ_PKTCNT(packets);
1077 epsize |= DXEPTSIZ_XFERSIZE(length);
1078
1079 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1080 __func__, packets, length, ureq->length, epsize, epsize_reg);
1081
1082 /* store the request as the current one we're doing */
1083 hs_ep->req = hs_req;
1084
1085 if (using_desc_dma(hsotg)) {
1086 u32 offset = 0;
1087 u32 mps = hs_ep->ep.maxpacket;
1088
1089 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1090 if (!dir_in) {
1091 if (!index)
1092 length = mps;
1093 else if (length % mps)
1094 length += (mps - (length % mps));
1095 }
1096
1097 /*
1098 * If more data to send, adjust DMA for EP0 out data stage.
1099 * ureq->dma stays unchanged, hence increment it by already
1100 * passed passed data count before starting new transaction.
1101 */
1102 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1103 continuing)
1104 offset = ureq->actual;
1105
1106 /* Fill DDMA chain entries */
1107 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1108 length);
1109
1110 /* write descriptor chain address to control register */
1111 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1112
1113 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1114 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1115 } else {
1116 /* write size / packets */
1117 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1118
1119 if (using_dma(hsotg) && !continuing && (length != 0)) {
1120 /*
1121 * write DMA address to control register, buffer
1122 * already synced by dwc2_hsotg_ep_queue().
1123 */
1124
1125 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1126
1127 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1128 __func__, &ureq->dma, dma_reg);
1129 }
1130 }
1131
1132 if (hs_ep->isochronous && hs_ep->interval == 1) {
1133 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1134 dwc2_gadget_incr_frame_num(hs_ep);
1135
1136 if (hs_ep->target_frame & 0x1)
1137 ctrl |= DXEPCTL_SETODDFR;
1138 else
1139 ctrl |= DXEPCTL_SETEVENFR;
1140 }
1141
1142 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1143
1144 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
1145
1146 /* For Setup request do not clear NAK */
1147 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
1148 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1149
1150 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
1151 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
1152
1153 /*
1154 * set these, it seems that DMA support increments past the end
1155 * of the packet buffer so we need to calculate the length from
1156 * this information.
1157 */
1158 hs_ep->size_loaded = length;
1159 hs_ep->last_load = ureq->actual;
1160
1161 if (dir_in && !using_dma(hsotg)) {
1162 /* set these anyway, we may need them for non-periodic in */
1163 hs_ep->fifo_load = 0;
1164
1165 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1166 }
1167
1168 /*
1169 * Note, trying to clear the NAK here causes problems with transmit
1170 * on the S3C6400 ending up with the TXFIFO becoming full.
1171 */
1172
1173 /* check ep is enabled */
1174 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
1175 dev_dbg(hsotg->dev,
1176 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
1177 index, dwc2_readl(hsotg->regs + epctrl_reg));
1178
1179 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
1180 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
1181
1182 /* enable ep interrupts */
1183 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
1184}
1185
1186/**
1187 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
1188 * @hsotg: The device state.
1189 * @hs_ep: The endpoint the request is on.
1190 * @req: The request being processed.
1191 *
1192 * We've been asked to queue a request, so ensure that the memory buffer
1193 * is correctly setup for DMA. If we've been passed an extant DMA address
1194 * then ensure the buffer has been synced to memory. If our buffer has no
1195 * DMA memory, then we map the memory and mark our request to allow us to
1196 * cleanup on completion.
1197 */
1198static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
1199 struct dwc2_hsotg_ep *hs_ep,
1200 struct usb_request *req)
1201{
1202 int ret;
1203
1204 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1205 if (ret)
1206 goto dma_error;
1207
1208 return 0;
1209
1210dma_error:
1211 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1212 __func__, req->buf, req->length);
1213
1214 return -EIO;
1215}
1216
1217static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
1218 struct dwc2_hsotg_ep *hs_ep,
1219 struct dwc2_hsotg_req *hs_req)
1220{
1221 void *req_buf = hs_req->req.buf;
1222
1223 /* If dma is not being used or buffer is aligned */
1224 if (!using_dma(hsotg) || !((long)req_buf & 3))
1225 return 0;
1226
1227 WARN_ON(hs_req->saved_req_buf);
1228
1229 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
1230 hs_ep->ep.name, req_buf, hs_req->req.length);
1231
1232 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1233 if (!hs_req->req.buf) {
1234 hs_req->req.buf = req_buf;
1235 dev_err(hsotg->dev,
1236 "%s: unable to allocate memory for bounce buffer\n",
1237 __func__);
1238 return -ENOMEM;
1239 }
1240
1241 /* Save actual buffer */
1242 hs_req->saved_req_buf = req_buf;
1243
1244 if (hs_ep->dir_in)
1245 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1246 return 0;
1247}
1248
1249static void
1250dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1251 struct dwc2_hsotg_ep *hs_ep,
1252 struct dwc2_hsotg_req *hs_req)
1253{
1254 /* If dma is not being used or buffer was aligned */
1255 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1256 return;
1257
1258 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1259 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1260
1261 /* Copy data from bounce buffer on successful out transfer */
1262 if (!hs_ep->dir_in && !hs_req->req.status)
1263 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
1264 hs_req->req.actual);
1265
1266 /* Free bounce buffer */
1267 kfree(hs_req->req.buf);
1268
1269 hs_req->req.buf = hs_req->saved_req_buf;
1270 hs_req->saved_req_buf = NULL;
1271}
1272
1273/**
1274 * dwc2_gadget_target_frame_elapsed - Checks target frame
1275 * @hs_ep: The driver endpoint to check
1276 *
1277 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1278 * corresponding transfer.
1279 */
1280static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1281{
1282 struct dwc2_hsotg *hsotg = hs_ep->parent;
1283 u32 target_frame = hs_ep->target_frame;
1284 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1285 bool frame_overrun = hs_ep->frame_overrun;
1286
1287 if (!frame_overrun && current_frame >= target_frame)
1288 return true;
1289
1290 if (frame_overrun && current_frame >= target_frame &&
1291 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1292 return true;
1293
1294 return false;
1295}
1296
1297/*
1298 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1299 * @hsotg: The driver state
1300 * @hs_ep: the ep descriptor chain is for
1301 *
1302 * Called to update EP0 structure's pointers depend on stage of
1303 * control transfer.
1304 */
1305static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1306 struct dwc2_hsotg_ep *hs_ep)
1307{
1308 switch (hsotg->ep0_state) {
1309 case DWC2_EP0_SETUP:
1310 case DWC2_EP0_STATUS_OUT:
1311 hs_ep->desc_list = hsotg->setup_desc[0];
1312 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1313 break;
1314 case DWC2_EP0_DATA_IN:
1315 case DWC2_EP0_STATUS_IN:
1316 hs_ep->desc_list = hsotg->ctrl_in_desc;
1317 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1318 break;
1319 case DWC2_EP0_DATA_OUT:
1320 hs_ep->desc_list = hsotg->ctrl_out_desc;
1321 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1322 break;
1323 default:
1324 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1325 hsotg->ep0_state);
1326 return -EINVAL;
1327 }
1328
1329 return 0;
1330}
1331
1332static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
1333 gfp_t gfp_flags)
1334{
1335 struct dwc2_hsotg_req *hs_req = our_req(req);
1336 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1337 struct dwc2_hsotg *hs = hs_ep->parent;
1338 bool first;
1339 int ret;
1340
1341 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1342 ep->name, req, req->length, req->buf, req->no_interrupt,
1343 req->zero, req->short_not_ok);
1344
1345 /* Prevent new request submission when controller is suspended */
1346 if (hs->lx_state == DWC2_L2) {
1347 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
1348 __func__);
1349 return -EAGAIN;
1350 }
1351
1352 /* initialise status of the request */
1353 INIT_LIST_HEAD(&hs_req->queue);
1354 req->actual = 0;
1355 req->status = -EINPROGRESS;
1356
1357 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
1358 if (ret)
1359 return ret;
1360
1361 /* if we're using DMA, sync the buffers as necessary */
1362 if (using_dma(hs)) {
1363 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
1364 if (ret)
1365 return ret;
1366 }
1367 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1368 if (using_desc_dma(hs) && !hs_ep->index) {
1369 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1370 if (ret)
1371 return ret;
1372 }
1373
1374 first = list_empty(&hs_ep->queue);
1375 list_add_tail(&hs_req->queue, &hs_ep->queue);
1376
1377 /*
1378 * Handle DDMA isochronous transfers separately - just add new entry
1379 * to the half of descriptor chain that is not processed by HW.
1380 * Transfer will be started once SW gets either one of NAK or
1381 * OutTknEpDis interrupts.
1382 */
1383 if (using_desc_dma(hs) && hs_ep->isochronous &&
1384 hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1385 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1386 hs_req->req.length);
1387 if (ret)
1388 dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
1389
1390 return 0;
1391 }
1392
1393 if (first) {
1394 if (!hs_ep->isochronous) {
1395 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1396 return 0;
1397 }
1398
1399 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1400 dwc2_gadget_incr_frame_num(hs_ep);
1401
1402 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1403 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1404 }
1405 return 0;
1406}
1407
1408static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
1409 gfp_t gfp_flags)
1410{
1411 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1412 struct dwc2_hsotg *hs = hs_ep->parent;
1413 unsigned long flags = 0;
1414 int ret = 0;
1415
1416 spin_lock_irqsave(&hs->lock, flags);
1417 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
1418 spin_unlock_irqrestore(&hs->lock, flags);
1419
1420 return ret;
1421}
1422
1423static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
1424 struct usb_request *req)
1425{
1426 struct dwc2_hsotg_req *hs_req = our_req(req);
1427
1428 kfree(hs_req);
1429}
1430
1431/**
1432 * dwc2_hsotg_complete_oursetup - setup completion callback
1433 * @ep: The endpoint the request was on.
1434 * @req: The request completed.
1435 *
1436 * Called on completion of any requests the driver itself
1437 * submitted that need cleaning up.
1438 */
1439static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
1440 struct usb_request *req)
1441{
1442 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1443 struct dwc2_hsotg *hsotg = hs_ep->parent;
1444
1445 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1446
1447 dwc2_hsotg_ep_free_request(ep, req);
1448}
1449
1450/**
1451 * ep_from_windex - convert control wIndex value to endpoint
1452 * @hsotg: The driver state.
1453 * @windex: The control request wIndex field (in host order).
1454 *
1455 * Convert the given wIndex into a pointer to an driver endpoint
1456 * structure, or return NULL if it is not a valid endpoint.
1457 */
1458static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
1459 u32 windex)
1460{
1461 struct dwc2_hsotg_ep *ep;
1462 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1463 int idx = windex & 0x7F;
1464
1465 if (windex >= 0x100)
1466 return NULL;
1467
1468 if (idx > hsotg->num_of_eps)
1469 return NULL;
1470
1471 ep = index_to_ep(hsotg, idx, dir);
1472
1473 if (idx && ep->dir_in != dir)
1474 return NULL;
1475
1476 return ep;
1477}
1478
1479/**
1480 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
1481 * @hsotg: The driver state.
1482 * @testmode: requested usb test mode
1483 * Enable usb Test Mode requested by the Host.
1484 */
1485int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
1486{
1487 int dctl = dwc2_readl(hsotg->regs + DCTL);
1488
1489 dctl &= ~DCTL_TSTCTL_MASK;
1490 switch (testmode) {
1491 case TEST_J:
1492 case TEST_K:
1493 case TEST_SE0_NAK:
1494 case TEST_PACKET:
1495 case TEST_FORCE_EN:
1496 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1497 break;
1498 default:
1499 return -EINVAL;
1500 }
1501 dwc2_writel(dctl, hsotg->regs + DCTL);
1502 return 0;
1503}
1504
1505/**
1506 * dwc2_hsotg_send_reply - send reply to control request
1507 * @hsotg: The device state
1508 * @ep: Endpoint 0
1509 * @buff: Buffer for request
1510 * @length: Length of reply.
1511 *
1512 * Create a request and queue it on the given endpoint. This is useful as
1513 * an internal method of sending replies to certain control requests, etc.
1514 */
1515static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1516 struct dwc2_hsotg_ep *ep,
1517 void *buff,
1518 int length)
1519{
1520 struct usb_request *req;
1521 int ret;
1522
1523 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1524
1525 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1526 hsotg->ep0_reply = req;
1527 if (!req) {
1528 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1529 return -ENOMEM;
1530 }
1531
1532 req->buf = hsotg->ep0_buff;
1533 req->length = length;
1534 /*
1535 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1536 * STATUS stage.
1537 */
1538 req->zero = 0;
1539 req->complete = dwc2_hsotg_complete_oursetup;
1540
1541 if (length)
1542 memcpy(req->buf, buff, length);
1543
1544 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1545 if (ret) {
1546 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1547 return ret;
1548 }
1549
1550 return 0;
1551}
1552
1553/**
1554 * dwc2_hsotg_process_req_status - process request GET_STATUS
1555 * @hsotg: The device state
1556 * @ctrl: USB control request
1557 */
1558static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
1559 struct usb_ctrlrequest *ctrl)
1560{
1561 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1562 struct dwc2_hsotg_ep *ep;
1563 __le16 reply;
1564 int ret;
1565
1566 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1567
1568 if (!ep0->dir_in) {
1569 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1570 return -EINVAL;
1571 }
1572
1573 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1574 case USB_RECIP_DEVICE:
1575 /*
1576 * bit 0 => self powered
1577 * bit 1 => remote wakeup
1578 */
1579 reply = cpu_to_le16(0);
1580 break;
1581
1582 case USB_RECIP_INTERFACE:
1583 /* currently, the data result should be zero */
1584 reply = cpu_to_le16(0);
1585 break;
1586
1587 case USB_RECIP_ENDPOINT:
1588 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1589 if (!ep)
1590 return -ENOENT;
1591
1592 reply = cpu_to_le16(ep->halted ? 1 : 0);
1593 break;
1594
1595 default:
1596 return 0;
1597 }
1598
1599 if (le16_to_cpu(ctrl->wLength) != 2)
1600 return -EINVAL;
1601
1602 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
1603 if (ret) {
1604 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1605 return ret;
1606 }
1607
1608 return 1;
1609}
1610
1611static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
1612
1613/**
1614 * get_ep_head - return the first request on the endpoint
1615 * @hs_ep: The controller endpoint to get
1616 *
1617 * Get the first request on the endpoint.
1618 */
1619static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
1620{
1621 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1622 queue);
1623}
1624
1625/**
1626 * dwc2_gadget_start_next_request - Starts next request from ep queue
1627 * @hs_ep: Endpoint structure
1628 *
1629 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1630 * in its handler. Hence we need to unmask it here to be able to do
1631 * resynchronization.
1632 */
1633static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1634{
1635 u32 mask;
1636 struct dwc2_hsotg *hsotg = hs_ep->parent;
1637 int dir_in = hs_ep->dir_in;
1638 struct dwc2_hsotg_req *hs_req;
1639 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1640
1641 if (!list_empty(&hs_ep->queue)) {
1642 hs_req = get_ep_head(hs_ep);
1643 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1644 return;
1645 }
1646 if (!hs_ep->isochronous)
1647 return;
1648
1649 if (dir_in) {
1650 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1651 __func__);
1652 } else {
1653 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1654 __func__);
1655 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1656 mask |= DOEPMSK_OUTTKNEPDISMSK;
1657 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1658 }
1659}
1660
1661/**
1662 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
1663 * @hsotg: The device state
1664 * @ctrl: USB control request
1665 */
1666static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
1667 struct usb_ctrlrequest *ctrl)
1668{
1669 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1670 struct dwc2_hsotg_req *hs_req;
1671 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1672 struct dwc2_hsotg_ep *ep;
1673 int ret;
1674 bool halted;
1675 u32 recip;
1676 u32 wValue;
1677 u32 wIndex;
1678
1679 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1680 __func__, set ? "SET" : "CLEAR");
1681
1682 wValue = le16_to_cpu(ctrl->wValue);
1683 wIndex = le16_to_cpu(ctrl->wIndex);
1684 recip = ctrl->bRequestType & USB_RECIP_MASK;
1685
1686 switch (recip) {
1687 case USB_RECIP_DEVICE:
1688 switch (wValue) {
1689 case USB_DEVICE_TEST_MODE:
1690 if ((wIndex & 0xff) != 0)
1691 return -EINVAL;
1692 if (!set)
1693 return -EINVAL;
1694
1695 hsotg->test_mode = wIndex >> 8;
1696 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1697 if (ret) {
1698 dev_err(hsotg->dev,
1699 "%s: failed to send reply\n", __func__);
1700 return ret;
1701 }
1702 break;
1703 default:
1704 return -ENOENT;
1705 }
1706 break;
1707
1708 case USB_RECIP_ENDPOINT:
1709 ep = ep_from_windex(hsotg, wIndex);
1710 if (!ep) {
1711 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1712 __func__, wIndex);
1713 return -ENOENT;
1714 }
1715
1716 switch (wValue) {
1717 case USB_ENDPOINT_HALT:
1718 halted = ep->halted;
1719
1720 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
1721
1722 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1723 if (ret) {
1724 dev_err(hsotg->dev,
1725 "%s: failed to send reply\n", __func__);
1726 return ret;
1727 }
1728
1729 /*
1730 * we have to complete all requests for ep if it was
1731 * halted, and the halt was cleared by CLEAR_FEATURE
1732 */
1733
1734 if (!set && halted) {
1735 /*
1736 * If we have request in progress,
1737 * then complete it
1738 */
1739 if (ep->req) {
1740 hs_req = ep->req;
1741 ep->req = NULL;
1742 list_del_init(&hs_req->queue);
1743 if (hs_req->req.complete) {
1744 spin_unlock(&hsotg->lock);
1745 usb_gadget_giveback_request(
1746 &ep->ep, &hs_req->req);
1747 spin_lock(&hsotg->lock);
1748 }
1749 }
1750
1751 /* If we have pending request, then start it */
1752 if (!ep->req)
1753 dwc2_gadget_start_next_request(ep);
1754 }
1755
1756 break;
1757
1758 default:
1759 return -ENOENT;
1760 }
1761 break;
1762 default:
1763 return -ENOENT;
1764 }
1765 return 1;
1766}
1767
1768static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
1769
1770/**
1771 * dwc2_hsotg_stall_ep0 - stall ep0
1772 * @hsotg: The device state
1773 *
1774 * Set stall for ep0 as response for setup request.
1775 */
1776static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
1777{
1778 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1779 u32 reg;
1780 u32 ctrl;
1781
1782 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1783 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1784
1785 /*
1786 * DxEPCTL_Stall will be cleared by EP once it has
1787 * taken effect, so no need to clear later.
1788 */
1789
1790 ctrl = dwc2_readl(hsotg->regs + reg);
1791 ctrl |= DXEPCTL_STALL;
1792 ctrl |= DXEPCTL_CNAK;
1793 dwc2_writel(ctrl, hsotg->regs + reg);
1794
1795 dev_dbg(hsotg->dev,
1796 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
1797 ctrl, reg, dwc2_readl(hsotg->regs + reg));
1798
1799 /*
1800 * complete won't be called, so we enqueue
1801 * setup request here
1802 */
1803 dwc2_hsotg_enqueue_setup(hsotg);
1804}
1805
1806/**
1807 * dwc2_hsotg_process_control - process a control request
1808 * @hsotg: The device state
1809 * @ctrl: The control request received
1810 *
1811 * The controller has received the SETUP phase of a control request, and
1812 * needs to work out what to do next (and whether to pass it on to the
1813 * gadget driver).
1814 */
1815static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
1816 struct usb_ctrlrequest *ctrl)
1817{
1818 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1819 int ret = 0;
1820 u32 dcfg;
1821
1822 dev_dbg(hsotg->dev,
1823 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1824 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1825 ctrl->wIndex, ctrl->wLength);
1826
1827 if (ctrl->wLength == 0) {
1828 ep0->dir_in = 1;
1829 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1830 } else if (ctrl->bRequestType & USB_DIR_IN) {
1831 ep0->dir_in = 1;
1832 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1833 } else {
1834 ep0->dir_in = 0;
1835 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1836 }
1837
1838 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1839 switch (ctrl->bRequest) {
1840 case USB_REQ_SET_ADDRESS:
1841 hsotg->connected = 1;
1842 dcfg = dwc2_readl(hsotg->regs + DCFG);
1843 dcfg &= ~DCFG_DEVADDR_MASK;
1844 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1845 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
1846 dwc2_writel(dcfg, hsotg->regs + DCFG);
1847
1848 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1849
1850 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1851 return;
1852
1853 case USB_REQ_GET_STATUS:
1854 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
1855 break;
1856
1857 case USB_REQ_CLEAR_FEATURE:
1858 case USB_REQ_SET_FEATURE:
1859 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
1860 break;
1861 }
1862 }
1863
1864 /* as a fallback, try delivering it to the driver to deal with */
1865
1866 if (ret == 0 && hsotg->driver) {
1867 spin_unlock(&hsotg->lock);
1868 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1869 spin_lock(&hsotg->lock);
1870 if (ret < 0)
1871 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1872 }
1873
1874 /*
1875 * the request is either unhandlable, or is not formatted correctly
1876 * so respond with a STALL for the status stage to indicate failure.
1877 */
1878
1879 if (ret < 0)
1880 dwc2_hsotg_stall_ep0(hsotg);
1881}
1882
1883/**
1884 * dwc2_hsotg_complete_setup - completion of a setup transfer
1885 * @ep: The endpoint the request was on.
1886 * @req: The request completed.
1887 *
1888 * Called on completion of any requests the driver itself submitted for
1889 * EP0 setup packets
1890 */
1891static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
1892 struct usb_request *req)
1893{
1894 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1895 struct dwc2_hsotg *hsotg = hs_ep->parent;
1896
1897 if (req->status < 0) {
1898 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1899 return;
1900 }
1901
1902 spin_lock(&hsotg->lock);
1903 if (req->actual == 0)
1904 dwc2_hsotg_enqueue_setup(hsotg);
1905 else
1906 dwc2_hsotg_process_control(hsotg, req->buf);
1907 spin_unlock(&hsotg->lock);
1908}
1909
1910/**
1911 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
1912 * @hsotg: The device state.
1913 *
1914 * Enqueue a request on EP0 if necessary to received any SETUP packets
1915 * received from the host.
1916 */
1917static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
1918{
1919 struct usb_request *req = hsotg->ctrl_req;
1920 struct dwc2_hsotg_req *hs_req = our_req(req);
1921 int ret;
1922
1923 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1924
1925 req->zero = 0;
1926 req->length = 8;
1927 req->buf = hsotg->ctrl_buff;
1928 req->complete = dwc2_hsotg_complete_setup;
1929
1930 if (!list_empty(&hs_req->queue)) {
1931 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1932 return;
1933 }
1934
1935 hsotg->eps_out[0]->dir_in = 0;
1936 hsotg->eps_out[0]->send_zlp = 0;
1937 hsotg->ep0_state = DWC2_EP0_SETUP;
1938
1939 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
1940 if (ret < 0) {
1941 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1942 /*
1943 * Don't think there's much we can do other than watch the
1944 * driver fail.
1945 */
1946 }
1947}
1948
1949static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1950 struct dwc2_hsotg_ep *hs_ep)
1951{
1952 u32 ctrl;
1953 u8 index = hs_ep->index;
1954 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1955 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1956
1957 if (hs_ep->dir_in)
1958 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1959 index);
1960 else
1961 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1962 index);
1963 if (using_desc_dma(hsotg)) {
1964 /* Not specific buffer needed for ep0 ZLP */
1965 dma_addr_t dma = hs_ep->desc_list_dma;
1966
1967 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1968 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1969 } else {
1970 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1971 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1972 epsiz_reg);
1973 }
1974
1975 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1976 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1977 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1978 ctrl |= DXEPCTL_USBACTEP;
1979 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1980}
1981
1982/**
1983 * dwc2_hsotg_complete_request - complete a request given to us
1984 * @hsotg: The device state.
1985 * @hs_ep: The endpoint the request was on.
1986 * @hs_req: The request to complete.
1987 * @result: The result code (0 => Ok, otherwise errno)
1988 *
1989 * The given request has finished, so call the necessary completion
1990 * if it has one and then look to see if we can start a new request
1991 * on the endpoint.
1992 *
1993 * Note, expects the ep to already be locked as appropriate.
1994 */
1995static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1996 struct dwc2_hsotg_ep *hs_ep,
1997 struct dwc2_hsotg_req *hs_req,
1998 int result)
1999{
2000 if (!hs_req) {
2001 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2002 return;
2003 }
2004
2005 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2006 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2007
2008 /*
2009 * only replace the status if we've not already set an error
2010 * from a previous transaction
2011 */
2012
2013 if (hs_req->req.status == -EINPROGRESS)
2014 hs_req->req.status = result;
2015
2016 if (using_dma(hsotg))
2017 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2018
2019 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
2020
2021 hs_ep->req = NULL;
2022 list_del_init(&hs_req->queue);
2023
2024 /*
2025 * call the complete request with the locks off, just in case the
2026 * request tries to queue more work for this endpoint.
2027 */
2028
2029 if (hs_req->req.complete) {
2030 spin_unlock(&hsotg->lock);
2031 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
2032 spin_lock(&hsotg->lock);
2033 }
2034
2035 /* In DDMA don't need to proceed to starting of next ISOC request */
2036 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2037 return;
2038
2039 /*
2040 * Look to see if there is anything else to do. Note, the completion
2041 * of the previous request may have caused a new request to be started
2042 * so be careful when doing this.
2043 */
2044
2045 if (!hs_ep->req && result >= 0)
2046 dwc2_gadget_start_next_request(hs_ep);
2047}
2048
2049/*
2050 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2051 * @hs_ep: The endpoint the request was on.
2052 *
2053 * Get first request from the ep queue, determine descriptor on which complete
2054 * happened. SW based on isoc_chain_num discovers which half of the descriptor
2055 * chain is currently in use by HW, adjusts dma_address and calculates index
2056 * of completed descriptor based on the value of DEPDMA register. Update actual
2057 * length of request, giveback to gadget.
2058 */
2059static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2060{
2061 struct dwc2_hsotg *hsotg = hs_ep->parent;
2062 struct dwc2_hsotg_req *hs_req;
2063 struct usb_request *ureq;
2064 int index;
2065 dma_addr_t dma_addr;
2066 u32 dma_reg;
2067 u32 depdma;
2068 u32 desc_sts;
2069 u32 mask;
2070
2071 hs_req = get_ep_head(hs_ep);
2072 if (!hs_req) {
2073 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2074 return;
2075 }
2076 ureq = &hs_req->req;
2077
2078 dma_addr = hs_ep->desc_list_dma;
2079
2080 /*
2081 * If lower half of descriptor chain is currently use by SW,
2082 * that means higher half is being processed by HW, so shift
2083 * DMA address to higher half of descriptor chain.
2084 */
2085 if (!hs_ep->isoc_chain_num)
2086 dma_addr += sizeof(struct dwc2_dma_desc) *
2087 (MAX_DMA_DESC_NUM_GENERIC / 2);
2088
2089 dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
2090 depdma = dwc2_readl(hsotg->regs + dma_reg);
2091
2092 index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
2093 desc_sts = hs_ep->desc_list[index].status;
2094
2095 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2096 DEV_DMA_ISOC_RX_NBYTES_MASK;
2097 ureq->actual = ureq->length -
2098 ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
2099
2100 /* Adjust actual length for ISOC Out if length is not align of 4 */
2101 if (!hs_ep->dir_in && ureq->length & 0x3)
2102 ureq->actual += 4 - (ureq->length & 0x3);
2103
2104 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2105}
2106
2107/*
2108 * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
2109 * @hs_ep: The isochronous endpoint to be re-enabled.
2110 *
2111 * If ep has been disabled due to last descriptor servicing (IN endpoint) or
2112 * BNA (OUT endpoint) check the status of other half of descriptor chain that
2113 * was under SW control till HW was busy and restart the endpoint if needed.
2114 */
2115static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
2116{
2117 struct dwc2_hsotg *hsotg = hs_ep->parent;
2118 u32 depctl;
2119 u32 dma_reg;
2120 u32 ctrl;
2121 u32 dma_addr = hs_ep->desc_list_dma;
2122 unsigned char index = hs_ep->index;
2123
2124 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
2125 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2126
2127 ctrl = dwc2_readl(hsotg->regs + depctl);
2128
2129 /*
2130 * EP was disabled if HW has processed last descriptor or BNA was set.
2131 * So restart ep if SW has prepared new descriptor chain in ep_queue
2132 * routine while HW was busy.
2133 */
2134 if (!(ctrl & DXEPCTL_EPENA)) {
2135 if (!hs_ep->next_desc) {
2136 dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
2137 __func__);
2138 return;
2139 }
2140
2141 dma_addr += sizeof(struct dwc2_dma_desc) *
2142 (MAX_DMA_DESC_NUM_GENERIC / 2) *
2143 hs_ep->isoc_chain_num;
2144 dwc2_writel(dma_addr, hsotg->regs + dma_reg);
2145
2146 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
2147 dwc2_writel(ctrl, hsotg->regs + depctl);
2148
2149 /* Switch ISOC descriptor chain number being processed by SW*/
2150 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
2151 hs_ep->next_desc = 0;
2152
2153 dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
2154 __func__);
2155 }
2156}
2157
2158/**
2159 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
2160 * @hsotg: The device state.
2161 * @ep_idx: The endpoint index for the data
2162 * @size: The size of data in the fifo, in bytes
2163 *
2164 * The FIFO status shows there is data to read from the FIFO for a given
2165 * endpoint, so sort out whether we need to read the data into a request
2166 * that has been made for that endpoint.
2167 */
2168static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
2169{
2170 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2171 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2172 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
2173 int to_read;
2174 int max_req;
2175 int read_ptr;
2176
2177 if (!hs_req) {
2178 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
2179 int ptr;
2180
2181 dev_dbg(hsotg->dev,
2182 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
2183 __func__, size, ep_idx, epctl);
2184
2185 /* dump the data from the FIFO, we've nothing we can do */
2186 for (ptr = 0; ptr < size; ptr += 4)
2187 (void)dwc2_readl(fifo);
2188
2189 return;
2190 }
2191
2192 to_read = size;
2193 read_ptr = hs_req->req.actual;
2194 max_req = hs_req->req.length - read_ptr;
2195
2196 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2197 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2198
2199 if (to_read > max_req) {
2200 /*
2201 * more data appeared than we where willing
2202 * to deal with in this request.
2203 */
2204
2205 /* currently we don't deal this */
2206 WARN_ON_ONCE(1);
2207 }
2208
2209 hs_ep->total_data += to_read;
2210 hs_req->req.actual += to_read;
2211 to_read = DIV_ROUND_UP(to_read, 4);
2212
2213 /*
2214 * note, we might over-write the buffer end by 3 bytes depending on
2215 * alignment of the data.
2216 */
2217 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
2218}
2219
2220/**
2221 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
2222 * @hsotg: The device instance
2223 * @dir_in: If IN zlp
2224 *
2225 * Generate a zero-length IN packet request for terminating a SETUP
2226 * transaction.
2227 *
2228 * Note, since we don't write any data to the TxFIFO, then it is
2229 * currently believed that we do not need to wait for any space in
2230 * the TxFIFO.
2231 */
2232static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
2233{
2234 /* eps_out[0] is used in both directions */
2235 hsotg->eps_out[0]->dir_in = dir_in;
2236 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
2237
2238 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
2239}
2240
2241static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
2242 u32 epctl_reg)
2243{
2244 u32 ctrl;
2245
2246 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2247 if (ctrl & DXEPCTL_EOFRNUM)
2248 ctrl |= DXEPCTL_SETEVENFR;
2249 else
2250 ctrl |= DXEPCTL_SETODDFR;
2251 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2252}
2253
2254/*
2255 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2256 * @hs_ep - The endpoint on which transfer went
2257 *
2258 * Iterate over endpoints descriptor chain and get info on bytes remained
2259 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2260 */
2261static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2262{
2263 struct dwc2_hsotg *hsotg = hs_ep->parent;
2264 unsigned int bytes_rem = 0;
2265 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2266 int i;
2267 u32 status;
2268
2269 if (!desc)
2270 return -EINVAL;
2271
2272 for (i = 0; i < hs_ep->desc_count; ++i) {
2273 status = desc->status;
2274 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2275
2276 if (status & DEV_DMA_STS_MASK)
2277 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2278 i, status & DEV_DMA_STS_MASK);
2279 desc++;
2280 }
2281
2282 return bytes_rem;
2283}
2284
2285/**
2286 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
2287 * @hsotg: The device instance
2288 * @epnum: The endpoint received from
2289 *
2290 * The RXFIFO has delivered an OutDone event, which means that the data
2291 * transfer for an OUT endpoint has been completed, either by a short
2292 * packet or by the finish of a transfer.
2293 */
2294static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
2295{
2296 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
2297 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2298 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2299 struct usb_request *req = &hs_req->req;
2300 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2301 int result = 0;
2302
2303 if (!hs_req) {
2304 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2305 return;
2306 }
2307
2308 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2309 dev_dbg(hsotg->dev, "zlp packet received\n");
2310 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2311 dwc2_hsotg_enqueue_setup(hsotg);
2312 return;
2313 }
2314
2315 if (using_desc_dma(hsotg))
2316 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2317
2318 if (using_dma(hsotg)) {
2319 unsigned int size_done;
2320
2321 /*
2322 * Calculate the size of the transfer by checking how much
2323 * is left in the endpoint size register and then working it
2324 * out from the amount we loaded for the transfer.
2325 *
2326 * We need to do this as DMA pointers are always 32bit aligned
2327 * so may overshoot/undershoot the transfer.
2328 */
2329
2330 size_done = hs_ep->size_loaded - size_left;
2331 size_done += hs_ep->last_load;
2332
2333 req->actual = size_done;
2334 }
2335
2336 /* if there is more request to do, schedule new transfer */
2337 if (req->actual < req->length && size_left == 0) {
2338 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2339 return;
2340 }
2341
2342 if (req->actual < req->length && req->short_not_ok) {
2343 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2344 __func__, req->actual, req->length);
2345
2346 /*
2347 * todo - what should we return here? there's no one else
2348 * even bothering to check the status.
2349 */
2350 }
2351
2352 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2353 if (!using_desc_dma(hsotg) && epnum == 0 &&
2354 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2355 /* Move to STATUS IN */
2356 dwc2_hsotg_ep0_zlp(hsotg, true);
2357 return;
2358 }
2359
2360 /*
2361 * Slave mode OUT transfers do not go through XferComplete so
2362 * adjust the ISOC parity here.
2363 */
2364 if (!using_dma(hsotg)) {
2365 if (hs_ep->isochronous && hs_ep->interval == 1)
2366 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
2367 else if (hs_ep->isochronous && hs_ep->interval > 1)
2368 dwc2_gadget_incr_frame_num(hs_ep);
2369 }
2370
2371 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
2372}
2373
2374/**
2375 * dwc2_hsotg_handle_rx - RX FIFO has data
2376 * @hsotg: The device instance
2377 *
2378 * The IRQ handler has detected that the RX FIFO has some data in it
2379 * that requires processing, so find out what is in there and do the
2380 * appropriate read.
2381 *
2382 * The RXFIFO is a true FIFO, the packets coming out are still in packet
2383 * chunks, so if you have x packets received on an endpoint you'll get x
2384 * FIFO events delivered, each with a packet's worth of data in it.
2385 *
2386 * When using DMA, we should not be processing events from the RXFIFO
2387 * as the actual data should be sent to the memory directly and we turn
2388 * on the completion interrupts to get notifications of transfer completion.
2389 */
2390static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
2391{
2392 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
2393 u32 epnum, status, size;
2394
2395 WARN_ON(using_dma(hsotg));
2396
2397 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2398 status = grxstsr & GRXSTS_PKTSTS_MASK;
2399
2400 size = grxstsr & GRXSTS_BYTECNT_MASK;
2401 size >>= GRXSTS_BYTECNT_SHIFT;
2402
2403 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
2404 __func__, grxstsr, size, epnum);
2405
2406 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2407 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2408 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
2409 break;
2410
2411 case GRXSTS_PKTSTS_OUTDONE:
2412 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
2413 dwc2_hsotg_read_frameno(hsotg));
2414
2415 if (!using_dma(hsotg))
2416 dwc2_hsotg_handle_outdone(hsotg, epnum);
2417 break;
2418
2419 case GRXSTS_PKTSTS_SETUPDONE:
2420 dev_dbg(hsotg->dev,
2421 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
2422 dwc2_hsotg_read_frameno(hsotg),
2423 dwc2_readl(hsotg->regs + DOEPCTL(0)));
2424 /*
2425 * Call dwc2_hsotg_handle_outdone here if it was not called from
2426 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2427 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2428 */
2429 if (hsotg->ep0_state == DWC2_EP0_SETUP)
2430 dwc2_hsotg_handle_outdone(hsotg, epnum);
2431 break;
2432
2433 case GRXSTS_PKTSTS_OUTRX:
2434 dwc2_hsotg_rx_data(hsotg, epnum, size);
2435 break;
2436
2437 case GRXSTS_PKTSTS_SETUPRX:
2438 dev_dbg(hsotg->dev,
2439 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
2440 dwc2_hsotg_read_frameno(hsotg),
2441 dwc2_readl(hsotg->regs + DOEPCTL(0)));
2442
2443 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2444
2445 dwc2_hsotg_rx_data(hsotg, epnum, size);
2446 break;
2447
2448 default:
2449 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2450 __func__, grxstsr);
2451
2452 dwc2_hsotg_dump(hsotg);
2453 break;
2454 }
2455}
2456
2457/**
2458 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
2459 * @mps: The maximum packet size in bytes.
2460 */
2461static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
2462{
2463 switch (mps) {
2464 case 64:
2465 return D0EPCTL_MPS_64;
2466 case 32:
2467 return D0EPCTL_MPS_32;
2468 case 16:
2469 return D0EPCTL_MPS_16;
2470 case 8:
2471 return D0EPCTL_MPS_8;
2472 }
2473
2474 /* bad max packet size, warn and return invalid result */
2475 WARN_ON(1);
2476 return (u32)-1;
2477}
2478
2479/**
2480 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
2481 * @hsotg: The driver state.
2482 * @ep: The index number of the endpoint
2483 * @mps: The maximum packet size in bytes
2484 * @mc: The multicount value
2485 *
2486 * Configure the maximum packet size for the given endpoint, updating
2487 * the hardware control registers to reflect this.
2488 */
2489static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
2490 unsigned int ep, unsigned int mps,
2491 unsigned int mc, unsigned int dir_in)
2492{
2493 struct dwc2_hsotg_ep *hs_ep;
2494 void __iomem *regs = hsotg->regs;
2495 u32 reg;
2496
2497 hs_ep = index_to_ep(hsotg, ep, dir_in);
2498 if (!hs_ep)
2499 return;
2500
2501 if (ep == 0) {
2502 u32 mps_bytes = mps;
2503
2504 /* EP0 is a special case */
2505 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2506 if (mps > 3)
2507 goto bad_mps;
2508 hs_ep->ep.maxpacket = mps_bytes;
2509 hs_ep->mc = 1;
2510 } else {
2511 if (mps > 1024)
2512 goto bad_mps;
2513 hs_ep->mc = mc;
2514 if (mc > 3)
2515 goto bad_mps;
2516 hs_ep->ep.maxpacket = mps;
2517 }
2518
2519 if (dir_in) {
2520 reg = dwc2_readl(regs + DIEPCTL(ep));
2521 reg &= ~DXEPCTL_MPS_MASK;
2522 reg |= mps;
2523 dwc2_writel(reg, regs + DIEPCTL(ep));
2524 } else {
2525 reg = dwc2_readl(regs + DOEPCTL(ep));
2526 reg &= ~DXEPCTL_MPS_MASK;
2527 reg |= mps;
2528 dwc2_writel(reg, regs + DOEPCTL(ep));
2529 }
2530
2531 return;
2532
2533bad_mps:
2534 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2535}
2536
2537/**
2538 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
2539 * @hsotg: The driver state
2540 * @idx: The index for the endpoint (0..15)
2541 */
2542static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
2543{
2544 int timeout;
2545 int val;
2546
2547 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2548 hsotg->regs + GRSTCTL);
2549
2550 /* wait until the fifo is flushed */
2551 timeout = 100;
2552
2553 while (1) {
2554 val = dwc2_readl(hsotg->regs + GRSTCTL);
2555
2556 if ((val & (GRSTCTL_TXFFLSH)) == 0)
2557 break;
2558
2559 if (--timeout == 0) {
2560 dev_err(hsotg->dev,
2561 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
2562 __func__, val);
2563 break;
2564 }
2565
2566 udelay(1);
2567 }
2568}
2569
2570/**
2571 * dwc2_hsotg_trytx - check to see if anything needs transmitting
2572 * @hsotg: The driver state
2573 * @hs_ep: The driver endpoint to check.
2574 *
2575 * Check to see if there is a request that has data to send, and if so
2576 * make an attempt to write data into the FIFO.
2577 */
2578static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
2579 struct dwc2_hsotg_ep *hs_ep)
2580{
2581 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2582
2583 if (!hs_ep->dir_in || !hs_req) {
2584 /**
2585 * if request is not enqueued, we disable interrupts
2586 * for endpoints, excepting ep0
2587 */
2588 if (hs_ep->index != 0)
2589 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
2590 hs_ep->dir_in, 0);
2591 return 0;
2592 }
2593
2594 if (hs_req->req.actual < hs_req->req.length) {
2595 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2596 hs_ep->index);
2597 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
2598 }
2599
2600 return 0;
2601}
2602
2603/**
2604 * dwc2_hsotg_complete_in - complete IN transfer
2605 * @hsotg: The device state.
2606 * @hs_ep: The endpoint that has just completed.
2607 *
2608 * An IN transfer has been completed, update the transfer's state and then
2609 * call the relevant completion routines.
2610 */
2611static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
2612 struct dwc2_hsotg_ep *hs_ep)
2613{
2614 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2615 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
2616 int size_left, size_done;
2617
2618 if (!hs_req) {
2619 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2620 return;
2621 }
2622
2623 /* Finish ZLP handling for IN EP0 transactions */
2624 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2625 dev_dbg(hsotg->dev, "zlp packet sent\n");
2626
2627 /*
2628 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2629 * changed to IN. Change back to complete OUT transfer request
2630 */
2631 hs_ep->dir_in = 0;
2632
2633 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2634 if (hsotg->test_mode) {
2635 int ret;
2636
2637 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
2638 if (ret < 0) {
2639 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
2640 hsotg->test_mode);
2641 dwc2_hsotg_stall_ep0(hsotg);
2642 return;
2643 }
2644 }
2645 dwc2_hsotg_enqueue_setup(hsotg);
2646 return;
2647 }
2648
2649 /*
2650 * Calculate the size of the transfer by checking how much is left
2651 * in the endpoint size register and then working it out from
2652 * the amount we loaded for the transfer.
2653 *
2654 * We do this even for DMA, as the transfer may have incremented
2655 * past the end of the buffer (DMA transfers are always 32bit
2656 * aligned).
2657 */
2658 if (using_desc_dma(hsotg)) {
2659 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2660 if (size_left < 0)
2661 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2662 size_left);
2663 } else {
2664 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2665 }
2666
2667 size_done = hs_ep->size_loaded - size_left;
2668 size_done += hs_ep->last_load;
2669
2670 if (hs_req->req.actual != size_done)
2671 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2672 __func__, hs_req->req.actual, size_done);
2673
2674 hs_req->req.actual = size_done;
2675 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2676 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
2677
2678 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2679 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
2680 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2681 return;
2682 }
2683
2684 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
2685 if (hs_ep->send_zlp) {
2686 dwc2_hsotg_program_zlp(hsotg, hs_ep);
2687 hs_ep->send_zlp = 0;
2688 /* transfer will be completed on next complete interrupt */
2689 return;
2690 }
2691
2692 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2693 /* Move to STATUS OUT */
2694 dwc2_hsotg_ep0_zlp(hsotg, false);
2695 return;
2696 }
2697
2698 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2699}
2700
2701/**
2702 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2703 * @hsotg: The device state.
2704 * @idx: Index of ep.
2705 * @dir_in: Endpoint direction 1-in 0-out.
2706 *
2707 * Reads for endpoint with given index and direction, by masking
2708 * epint_reg with coresponding mask.
2709 */
2710static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2711 unsigned int idx, int dir_in)
2712{
2713 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2714 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2715 u32 ints;
2716 u32 mask;
2717 u32 diepempmsk;
2718
2719 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2720 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2721 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2722 mask |= DXEPINT_SETUP_RCVD;
2723
2724 ints = dwc2_readl(hsotg->regs + epint_reg);
2725 ints &= mask;
2726 return ints;
2727}
2728
2729/**
2730 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2731 * @hs_ep: The endpoint on which interrupt is asserted.
2732 *
2733 * This interrupt indicates that the endpoint has been disabled per the
2734 * application's request.
2735 *
2736 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2737 * in case of ISOC completes current request.
2738 *
2739 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2740 * request starts it.
2741 */
2742static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2743{
2744 struct dwc2_hsotg *hsotg = hs_ep->parent;
2745 struct dwc2_hsotg_req *hs_req;
2746 unsigned char idx = hs_ep->index;
2747 int dir_in = hs_ep->dir_in;
2748 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2749 int dctl = dwc2_readl(hsotg->regs + DCTL);
2750
2751 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2752
2753 if (dir_in) {
2754 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2755
2756 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2757
2758 if (hs_ep->isochronous) {
2759 dwc2_hsotg_complete_in(hsotg, hs_ep);
2760 return;
2761 }
2762
2763 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2764 int dctl = dwc2_readl(hsotg->regs + DCTL);
2765
2766 dctl |= DCTL_CGNPINNAK;
2767 dwc2_writel(dctl, hsotg->regs + DCTL);
2768 }
2769 return;
2770 }
2771
2772 if (dctl & DCTL_GOUTNAKSTS) {
2773 dctl |= DCTL_CGOUTNAK;
2774 dwc2_writel(dctl, hsotg->regs + DCTL);
2775 }
2776
2777 if (!hs_ep->isochronous)
2778 return;
2779
2780 if (list_empty(&hs_ep->queue)) {
2781 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2782 __func__, hs_ep);
2783 return;
2784 }
2785
2786 do {
2787 hs_req = get_ep_head(hs_ep);
2788 if (hs_req)
2789 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2790 -ENODATA);
2791 dwc2_gadget_incr_frame_num(hs_ep);
2792 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2793
2794 dwc2_gadget_start_next_request(hs_ep);
2795}
2796
2797/**
2798 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2799 * @hs_ep: The endpoint on which interrupt is asserted.
2800 *
2801 * This is starting point for ISOC-OUT transfer, synchronization done with
2802 * first out token received from host while corresponding EP is disabled.
2803 *
2804 * Device does not know initial frame in which out token will come. For this
2805 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2806 * getting this interrupt SW starts calculation for next transfer frame.
2807 */
2808static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2809{
2810 struct dwc2_hsotg *hsotg = ep->parent;
2811 int dir_in = ep->dir_in;
2812 u32 doepmsk;
2813 u32 tmp;
2814
2815 if (dir_in || !ep->isochronous)
2816 return;
2817
2818 /*
2819 * Store frame in which irq was asserted here, as
2820 * it can change while completing request below.
2821 */
2822 tmp = dwc2_hsotg_read_frameno(hsotg);
2823
2824 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2825
2826 if (using_desc_dma(hsotg)) {
2827 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2828 /* Start first ISO Out */
2829 ep->target_frame = tmp;
2830 dwc2_gadget_start_isoc_ddma(ep);
2831 }
2832 return;
2833 }
2834
2835 if (ep->interval > 1 &&
2836 ep->target_frame == TARGET_FRAME_INITIAL) {
2837 u32 dsts;
2838 u32 ctrl;
2839
2840 dsts = dwc2_readl(hsotg->regs + DSTS);
2841 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2842 dwc2_gadget_incr_frame_num(ep);
2843
2844 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2845 if (ep->target_frame & 0x1)
2846 ctrl |= DXEPCTL_SETODDFR;
2847 else
2848 ctrl |= DXEPCTL_SETEVENFR;
2849
2850 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2851 }
2852
2853 dwc2_gadget_start_next_request(ep);
2854 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2855 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2856 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2857}
2858
2859/**
2860 * dwc2_gadget_handle_nak - handle NAK interrupt
2861 * @hs_ep: The endpoint on which interrupt is asserted.
2862 *
2863 * This is starting point for ISOC-IN transfer, synchronization done with
2864 * first IN token received from host while corresponding EP is disabled.
2865 *
2866 * Device does not know when first one token will arrive from host. On first
2867 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2868 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2869 * sent in response to that as there was no data in FIFO. SW is basing on this
2870 * interrupt to obtain frame in which token has come and then based on the
2871 * interval calculates next frame for transfer.
2872 */
2873static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2874{
2875 struct dwc2_hsotg *hsotg = hs_ep->parent;
2876 int dir_in = hs_ep->dir_in;
2877
2878 if (!dir_in || !hs_ep->isochronous)
2879 return;
2880
2881 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2882 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2883
2884 if (using_desc_dma(hsotg)) {
2885 dwc2_gadget_start_isoc_ddma(hs_ep);
2886 return;
2887 }
2888
2889 if (hs_ep->interval > 1) {
2890 u32 ctrl = dwc2_readl(hsotg->regs +
2891 DIEPCTL(hs_ep->index));
2892 if (hs_ep->target_frame & 0x1)
2893 ctrl |= DXEPCTL_SETODDFR;
2894 else
2895 ctrl |= DXEPCTL_SETEVENFR;
2896
2897 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2898 }
2899
2900 dwc2_hsotg_complete_request(hsotg, hs_ep,
2901 get_ep_head(hs_ep), 0);
2902 }
2903
2904 dwc2_gadget_incr_frame_num(hs_ep);
2905}
2906
2907/**
2908 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
2909 * @hsotg: The driver state
2910 * @idx: The index for the endpoint (0..15)
2911 * @dir_in: Set if this is an IN endpoint
2912 *
2913 * Process and clear any interrupt pending for an individual endpoint
2914 */
2915static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
2916 int dir_in)
2917{
2918 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
2919 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2920 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2921 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
2922 u32 ints;
2923 u32 ctrl;
2924
2925 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
2926 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2927
2928 /* Clear endpoint interrupts */
2929 dwc2_writel(ints, hsotg->regs + epint_reg);
2930
2931 if (!hs_ep) {
2932 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2933 __func__, idx, dir_in ? "in" : "out");
2934 return;
2935 }
2936
2937 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2938 __func__, idx, dir_in ? "in" : "out", ints);
2939
2940 /* Don't process XferCompl interrupt if it is a setup packet */
2941 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2942 ints &= ~DXEPINT_XFERCOMPL;
2943
2944 /*
2945 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2946 * stage and xfercomplete was generated without SETUP phase done
2947 * interrupt. SW should parse received setup packet only after host's
2948 * exit from setup phase of control transfer.
2949 */
2950 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2951 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2952 ints &= ~DXEPINT_XFERCOMPL;
2953
2954 if (ints & DXEPINT_XFERCOMPL) {
2955 dev_dbg(hsotg->dev,
2956 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
2957 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2958 dwc2_readl(hsotg->regs + epsiz_reg));
2959
2960 /* In DDMA handle isochronous requests separately */
2961 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
2962 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
2963 /* Try to start next isoc request */
2964 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2965 } else if (dir_in) {
2966 /*
2967 * We get OutDone from the FIFO, so we only
2968 * need to look at completing IN requests here
2969 * if operating slave mode
2970 */
2971 if (hs_ep->isochronous && hs_ep->interval > 1)
2972 dwc2_gadget_incr_frame_num(hs_ep);
2973
2974 dwc2_hsotg_complete_in(hsotg, hs_ep);
2975 if (ints & DXEPINT_NAKINTRPT)
2976 ints &= ~DXEPINT_NAKINTRPT;
2977
2978 if (idx == 0 && !hs_ep->req)
2979 dwc2_hsotg_enqueue_setup(hsotg);
2980 } else if (using_dma(hsotg)) {
2981 /*
2982 * We're using DMA, we need to fire an OutDone here
2983 * as we ignore the RXFIFO.
2984 */
2985 if (hs_ep->isochronous && hs_ep->interval > 1)
2986 dwc2_gadget_incr_frame_num(hs_ep);
2987
2988 dwc2_hsotg_handle_outdone(hsotg, idx);
2989 }
2990 }
2991
2992 if (ints & DXEPINT_EPDISBLD)
2993 dwc2_gadget_handle_ep_disabled(hs_ep);
2994
2995 if (ints & DXEPINT_OUTTKNEPDIS)
2996 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2997
2998 if (ints & DXEPINT_NAKINTRPT)
2999 dwc2_gadget_handle_nak(hs_ep);
3000
3001 if (ints & DXEPINT_AHBERR)
3002 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
3003
3004 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
3005 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3006
3007 if (using_dma(hsotg) && idx == 0) {
3008 /*
3009 * this is the notification we've received a
3010 * setup packet. In non-DMA mode we'd get this
3011 * from the RXFIFO, instead we need to process
3012 * the setup here.
3013 */
3014
3015 if (dir_in)
3016 WARN_ON_ONCE(1);
3017 else
3018 dwc2_hsotg_handle_outdone(hsotg, 0);
3019 }
3020 }
3021
3022 if (ints & DXEPINT_STSPHSERCVD) {
3023 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3024
3025 /* Move to STATUS IN for DDMA */
3026 if (using_desc_dma(hsotg))
3027 dwc2_hsotg_ep0_zlp(hsotg, true);
3028 }
3029
3030 if (ints & DXEPINT_BACK2BACKSETUP)
3031 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
3032
3033 if (ints & DXEPINT_BNAINTR) {
3034 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
3035
3036 /*
3037 * Try to start next isoc request, if any.
3038 * Sometimes the endpoint remains enabled after BNA interrupt
3039 * assertion, which is not expected, hence we can enter here
3040 * couple of times.
3041 */
3042 if (hs_ep->isochronous)
3043 dwc2_gadget_start_next_isoc_ddma(hs_ep);
3044 }
3045
3046 if (dir_in && !hs_ep->isochronous) {
3047 /* not sure if this is important, but we'll clear it anyway */
3048 if (ints & DXEPINT_INTKNTXFEMP) {
3049 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3050 __func__, idx);
3051 }
3052
3053 /* this probably means something bad is happening */
3054 if (ints & DXEPINT_INTKNEPMIS) {
3055 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3056 __func__, idx);
3057 }
3058
3059 /* FIFO has space or is empty (see GAHBCFG) */
3060 if (hsotg->dedicated_fifos &&
3061 ints & DXEPINT_TXFEMP) {
3062 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3063 __func__, idx);
3064 if (!using_dma(hsotg))
3065 dwc2_hsotg_trytx(hsotg, hs_ep);
3066 }
3067 }
3068}
3069
3070/**
3071 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
3072 * @hsotg: The device state.
3073 *
3074 * Handle updating the device settings after the enumeration phase has
3075 * been completed.
3076 */
3077static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
3078{
3079 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
3080 int ep0_mps = 0, ep_mps = 8;
3081
3082 /*
3083 * This should signal the finish of the enumeration phase
3084 * of the USB handshaking, so we should now know what rate
3085 * we connected at.
3086 */
3087
3088 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3089
3090 /*
3091 * note, since we're limited by the size of transfer on EP0, and
3092 * it seems IN transfers must be a even number of packets we do
3093 * not advertise a 64byte MPS on EP0.
3094 */
3095
3096 /* catch both EnumSpd_FS and EnumSpd_FS48 */
3097 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
3098 case DSTS_ENUMSPD_FS:
3099 case DSTS_ENUMSPD_FS48:
3100 hsotg->gadget.speed = USB_SPEED_FULL;
3101 ep0_mps = EP0_MPS_LIMIT;
3102 ep_mps = 1023;
3103 break;
3104
3105 case DSTS_ENUMSPD_HS:
3106 hsotg->gadget.speed = USB_SPEED_HIGH;
3107 ep0_mps = EP0_MPS_LIMIT;
3108 ep_mps = 1024;
3109 break;
3110
3111 case DSTS_ENUMSPD_LS:
3112 hsotg->gadget.speed = USB_SPEED_LOW;
3113 ep0_mps = 8;
3114 ep_mps = 8;
3115 /*
3116 * note, we don't actually support LS in this driver at the
3117 * moment, and the documentation seems to imply that it isn't
3118 * supported by the PHYs on some of the devices.
3119 */
3120 break;
3121 }
3122 dev_info(hsotg->dev, "new device is %s\n",
3123 usb_speed_string(hsotg->gadget.speed));
3124
3125 /*
3126 * we should now know the maximum packet size for an
3127 * endpoint, so set the endpoints to a default value.
3128 */
3129
3130 if (ep0_mps) {
3131 int i;
3132 /* Initialize ep0 for both in and out directions */
3133 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3134 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
3135 for (i = 1; i < hsotg->num_of_eps; i++) {
3136 if (hsotg->eps_in[i])
3137 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3138 0, 1);
3139 if (hsotg->eps_out[i])
3140 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3141 0, 0);
3142 }
3143 }
3144
3145 /* ensure after enumeration our EP0 is active */
3146
3147 dwc2_hsotg_enqueue_setup(hsotg);
3148
3149 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3150 dwc2_readl(hsotg->regs + DIEPCTL0),
3151 dwc2_readl(hsotg->regs + DOEPCTL0));
3152}
3153
3154/**
3155 * kill_all_requests - remove all requests from the endpoint's queue
3156 * @hsotg: The device state.
3157 * @ep: The endpoint the requests may be on.
3158 * @result: The result code to use.
3159 *
3160 * Go through the requests on the given endpoint and mark them
3161 * completed with the given result code.
3162 */
3163static void kill_all_requests(struct dwc2_hsotg *hsotg,
3164 struct dwc2_hsotg_ep *ep,
3165 int result)
3166{
3167 struct dwc2_hsotg_req *req, *treq;
3168 unsigned int size;
3169
3170 ep->req = NULL;
3171
3172 list_for_each_entry_safe(req, treq, &ep->queue, queue)
3173 dwc2_hsotg_complete_request(hsotg, ep, req,
3174 result);
3175
3176 if (!hsotg->dedicated_fifos)
3177 return;
3178 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
3179 if (size < ep->fifo_size)
3180 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
3181}
3182
3183/**
3184 * dwc2_hsotg_disconnect - disconnect service
3185 * @hsotg: The device state.
3186 *
3187 * The device has been disconnected. Remove all current
3188 * transactions and signal the gadget driver that this
3189 * has happened.
3190 */
3191void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
3192{
3193 unsigned int ep;
3194
3195 if (!hsotg->connected)
3196 return;
3197
3198 hsotg->connected = 0;
3199 hsotg->test_mode = 0;
3200
3201 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3202 if (hsotg->eps_in[ep])
3203 kill_all_requests(hsotg, hsotg->eps_in[ep],
3204 -ESHUTDOWN);
3205 if (hsotg->eps_out[ep])
3206 kill_all_requests(hsotg, hsotg->eps_out[ep],
3207 -ESHUTDOWN);
3208 }
3209
3210 call_gadget(hsotg, disconnect);
3211 hsotg->lx_state = DWC2_L3;
3212}
3213
3214/**
3215 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
3216 * @hsotg: The device state:
3217 * @periodic: True if this is a periodic FIFO interrupt
3218 */
3219static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
3220{
3221 struct dwc2_hsotg_ep *ep;
3222 int epno, ret;
3223
3224 /* look through for any more data to transmit */
3225 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
3226 ep = index_to_ep(hsotg, epno, 1);
3227
3228 if (!ep)
3229 continue;
3230
3231 if (!ep->dir_in)
3232 continue;
3233
3234 if ((periodic && !ep->periodic) ||
3235 (!periodic && ep->periodic))
3236 continue;
3237
3238 ret = dwc2_hsotg_trytx(hsotg, ep);
3239 if (ret < 0)
3240 break;
3241 }
3242}
3243
3244/* IRQ flags which will trigger a retry around the IRQ loop */
3245#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3246 GINTSTS_PTXFEMP | \
3247 GINTSTS_RXFLVL)
3248
3249/**
3250 * dwc2_hsotg_core_init - issue softreset to the core
3251 * @hsotg: The device state
3252 *
3253 * Issue a soft reset to the core, and await the core finishing it.
3254 */
3255void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
3256 bool is_usb_reset)
3257{
3258 u32 intmsk;
3259 u32 val;
3260 u32 usbcfg;
3261 u32 dcfg = 0;
3262
3263 /* Kill any ep0 requests as controller will be reinitialized */
3264 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3265
3266 if (!is_usb_reset)
3267 if (dwc2_core_reset(hsotg, true))
3268 return;
3269
3270 /*
3271 * we must now enable ep0 ready for host detection and then
3272 * set configuration.
3273 */
3274
3275 /* keep other bits untouched (so e.g. forced modes are not lost) */
3276 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3277 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3278 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
3279
3280 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
3281 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3282 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
3283 /* FS/LS Dedicated Transceiver Interface */
3284 usbcfg |= GUSBCFG_PHYSEL;
3285 } else {
3286 /* set the PLL on, remove the HNP/SRP and set the PHY */
3287 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3288 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3289 (val << GUSBCFG_USBTRDTIM_SHIFT);
3290 }
3291 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
3292
3293 dwc2_hsotg_init_fifo(hsotg);
3294
3295 if (!is_usb_reset)
3296 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3297
3298 dcfg |= DCFG_EPMISCNT(1);
3299
3300 switch (hsotg->params.speed) {
3301 case DWC2_SPEED_PARAM_LOW:
3302 dcfg |= DCFG_DEVSPD_LS;
3303 break;
3304 case DWC2_SPEED_PARAM_FULL:
3305 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3306 dcfg |= DCFG_DEVSPD_FS48;
3307 else
3308 dcfg |= DCFG_DEVSPD_FS;
3309 break;
3310 default:
3311 dcfg |= DCFG_DEVSPD_HS;
3312 }
3313
3314 dwc2_writel(dcfg, hsotg->regs + DCFG);
3315
3316 /* Clear any pending OTG interrupts */
3317 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
3318
3319 /* Clear any pending interrupts */
3320 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
3321 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
3322 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
3323 GINTSTS_USBRST | GINTSTS_RESETDET |
3324 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
3325 GINTSTS_USBSUSP | GINTSTS_WKUPINT;
3326
3327 if (!using_desc_dma(hsotg))
3328 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
3329
3330 if (!hsotg->params.external_id_pin_ctl)
3331 intmsk |= GINTSTS_CONIDSTSCHNG;
3332
3333 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
3334
3335 if (using_dma(hsotg)) {
3336 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3337 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
3338 hsotg->regs + GAHBCFG);
3339
3340 /* Set DDMA mode support in the core if needed */
3341 if (using_desc_dma(hsotg))
3342 __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
3343
3344 } else {
3345 dwc2_writel(((hsotg->dedicated_fifos) ?
3346 (GAHBCFG_NP_TXF_EMP_LVL |
3347 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3348 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
3349 }
3350
3351 /*
3352 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3353 * when we have no data to transfer. Otherwise we get being flooded by
3354 * interrupts.
3355 */
3356
3357 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
3358 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
3359 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
3360 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
3361 hsotg->regs + DIEPMSK);
3362
3363 /*
3364 * don't need XferCompl, we get that from RXFIFO in slave mode. In
3365 * DMA mode we may need this and StsPhseRcvd.
3366 */
3367 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3368 DOEPMSK_STSPHSERCVDMSK) : 0) |
3369 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
3370 DOEPMSK_SETUPMSK,
3371 hsotg->regs + DOEPMSK);
3372
3373 /* Enable BNA interrupt for DDMA */
3374 if (using_desc_dma(hsotg))
3375 __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
3376
3377 dwc2_writel(0, hsotg->regs + DAINTMSK);
3378
3379 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3380 dwc2_readl(hsotg->regs + DIEPCTL0),
3381 dwc2_readl(hsotg->regs + DOEPCTL0));
3382
3383 /* enable in and out endpoint interrupts */
3384 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
3385
3386 /*
3387 * Enable the RXFIFO when in slave mode, as this is how we collect
3388 * the data. In DMA mode, we get events from the FIFO but also
3389 * things we cannot process, so do not use it.
3390 */
3391 if (!using_dma(hsotg))
3392 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
3393
3394 /* Enable interrupts for EP0 in and out */
3395 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3396 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
3397
3398 if (!is_usb_reset) {
3399 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3400 udelay(10); /* see openiboot */
3401 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3402 }
3403
3404 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
3405
3406 /*
3407 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
3408 * writing to the EPCTL register..
3409 */
3410
3411 /* set to read 1 8byte packet */
3412 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3413 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
3414
3415 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3416 DXEPCTL_CNAK | DXEPCTL_EPENA |
3417 DXEPCTL_USBACTEP,
3418 hsotg->regs + DOEPCTL0);
3419
3420 /* enable, but don't activate EP0in */
3421 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3422 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
3423
3424 /* clear global NAKs */
3425 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3426 if (!is_usb_reset)
3427 val |= DCTL_SFTDISCON;
3428 __orr32(hsotg->regs + DCTL, val);
3429
3430 /* must be at-least 3ms to allow bus to see disconnect */
3431 mdelay(3);
3432
3433 hsotg->lx_state = DWC2_L0;
3434
3435 dwc2_hsotg_enqueue_setup(hsotg);
3436
3437 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3438 dwc2_readl(hsotg->regs + DIEPCTL0),
3439 dwc2_readl(hsotg->regs + DOEPCTL0));
3440}
3441
3442static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
3443{
3444 /* set the soft-disconnect bit */
3445 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3446}
3447
3448void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
3449{
3450 /* remove the soft-disconnect and let's go */
3451 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3452}
3453
3454/**
3455 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3456 * @hsotg: The device state:
3457 *
3458 * This interrupt indicates one of the following conditions occurred while
3459 * transmitting an ISOC transaction.
3460 * - Corrupted IN Token for ISOC EP.
3461 * - Packet not complete in FIFO.
3462 *
3463 * The following actions will be taken:
3464 * - Determine the EP
3465 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3466 */
3467static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3468{
3469 struct dwc2_hsotg_ep *hs_ep;
3470 u32 epctrl;
3471 u32 idx;
3472
3473 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3474
3475 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3476 hs_ep = hsotg->eps_in[idx];
3477 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
3478 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3479 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3480 epctrl |= DXEPCTL_SNAK;
3481 epctrl |= DXEPCTL_EPDIS;
3482 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3483 }
3484 }
3485
3486 /* Clear interrupt */
3487 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3488}
3489
3490/**
3491 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3492 * @hsotg: The device state:
3493 *
3494 * This interrupt indicates one of the following conditions occurred while
3495 * transmitting an ISOC transaction.
3496 * - Corrupted OUT Token for ISOC EP.
3497 * - Packet not complete in FIFO.
3498 *
3499 * The following actions will be taken:
3500 * - Determine the EP
3501 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3502 */
3503static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3504{
3505 u32 gintsts;
3506 u32 gintmsk;
3507 u32 epctrl;
3508 struct dwc2_hsotg_ep *hs_ep;
3509 int idx;
3510
3511 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3512
3513 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3514 hs_ep = hsotg->eps_out[idx];
3515 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3516 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3517 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3518 /* Unmask GOUTNAKEFF interrupt */
3519 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3520 gintmsk |= GINTSTS_GOUTNAKEFF;
3521 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3522
3523 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3524 if (!(gintsts & GINTSTS_GOUTNAKEFF))
3525 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3526 }
3527 }
3528
3529 /* Clear interrupt */
3530 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3531}
3532
3533/**
3534 * dwc2_hsotg_irq - handle device interrupt
3535 * @irq: The IRQ number triggered
3536 * @pw: The pw value when registered the handler.
3537 */
3538static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
3539{
3540 struct dwc2_hsotg *hsotg = pw;
3541 int retry_count = 8;
3542 u32 gintsts;
3543 u32 gintmsk;
3544
3545 if (!dwc2_is_device_mode(hsotg))
3546 return IRQ_NONE;
3547
3548 spin_lock(&hsotg->lock);
3549irq_retry:
3550 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3551 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3552
3553 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3554 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3555
3556 gintsts &= gintmsk;
3557
3558 if (gintsts & GINTSTS_RESETDET) {
3559 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3560
3561 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3562
3563 /* This event must be used only if controller is suspended */
3564 if (hsotg->lx_state == DWC2_L2) {
3565 dwc2_exit_hibernation(hsotg, true);
3566 hsotg->lx_state = DWC2_L0;
3567 }
3568 }
3569
3570 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
3571 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3572 u32 connected = hsotg->connected;
3573
3574 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3575 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3576 dwc2_readl(hsotg->regs + GNPTXSTS));
3577
3578 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3579
3580 /* Report disconnection if it is not already done. */
3581 dwc2_hsotg_disconnect(hsotg);
3582
3583 /* Reset device address to zero */
3584 __bic32(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
3585
3586 if (usb_status & GOTGCTL_BSESVLD && connected)
3587 dwc2_hsotg_core_init_disconnected(hsotg, true);
3588 }
3589
3590 if (gintsts & GINTSTS_ENUMDONE) {
3591 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
3592
3593 dwc2_hsotg_irq_enumdone(hsotg);
3594 }
3595
3596 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
3597 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3598 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3599 u32 daint_out, daint_in;
3600 int ep;
3601
3602 daint &= daintmsk;
3603 daint_out = daint >> DAINT_OUTEP_SHIFT;
3604 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
3605
3606 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3607
3608 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3609 ep++, daint_out >>= 1) {
3610 if (daint_out & 1)
3611 dwc2_hsotg_epint(hsotg, ep, 0);
3612 }
3613
3614 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3615 ep++, daint_in >>= 1) {
3616 if (daint_in & 1)
3617 dwc2_hsotg_epint(hsotg, ep, 1);
3618 }
3619 }
3620
3621 /* check both FIFOs */
3622
3623 if (gintsts & GINTSTS_NPTXFEMP) {
3624 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3625
3626 /*
3627 * Disable the interrupt to stop it happening again
3628 * unless one of these endpoint routines decides that
3629 * it needs re-enabling
3630 */
3631
3632 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3633 dwc2_hsotg_irq_fifoempty(hsotg, false);
3634 }
3635
3636 if (gintsts & GINTSTS_PTXFEMP) {
3637 dev_dbg(hsotg->dev, "PTxFEmp\n");
3638
3639 /* See note in GINTSTS_NPTxFEmp */
3640
3641 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3642 dwc2_hsotg_irq_fifoempty(hsotg, true);
3643 }
3644
3645 if (gintsts & GINTSTS_RXFLVL) {
3646 /*
3647 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
3648 * we need to retry dwc2_hsotg_handle_rx if this is still
3649 * set.
3650 */
3651
3652 dwc2_hsotg_handle_rx(hsotg);
3653 }
3654
3655 if (gintsts & GINTSTS_ERLYSUSP) {
3656 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
3657 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
3658 }
3659
3660 /*
3661 * these next two seem to crop-up occasionally causing the core
3662 * to shutdown the USB transfer, so try clearing them and logging
3663 * the occurrence.
3664 */
3665
3666 if (gintsts & GINTSTS_GOUTNAKEFF) {
3667 u8 idx;
3668 u32 epctrl;
3669 u32 gintmsk;
3670 struct dwc2_hsotg_ep *hs_ep;
3671
3672 /* Mask this interrupt */
3673 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3674 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3675 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3676
3677 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3678 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3679 hs_ep = hsotg->eps_out[idx];
3680 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3681
3682 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3683 epctrl |= DXEPCTL_SNAK;
3684 epctrl |= DXEPCTL_EPDIS;
3685 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3686 }
3687 }
3688
3689 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
3690 }
3691
3692 if (gintsts & GINTSTS_GINNAKEFF) {
3693 dev_info(hsotg->dev, "GINNakEff triggered\n");
3694
3695 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3696
3697 dwc2_hsotg_dump(hsotg);
3698 }
3699
3700 if (gintsts & GINTSTS_INCOMPL_SOIN)
3701 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
3702
3703 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3704 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
3705
3706 /*
3707 * if we've had fifo events, we should try and go around the
3708 * loop again to see if there's any point in returning yet.
3709 */
3710
3711 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
3712 goto irq_retry;
3713
3714 spin_unlock(&hsotg->lock);
3715
3716 return IRQ_HANDLED;
3717}
3718
3719static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3720 u32 bit, u32 timeout)
3721{
3722 u32 i;
3723
3724 for (i = 0; i < timeout; i++) {
3725 if (dwc2_readl(hs_otg->regs + reg) & bit)
3726 return 0;
3727 udelay(1);
3728 }
3729
3730 return -ETIMEDOUT;
3731}
3732
3733static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3734 struct dwc2_hsotg_ep *hs_ep)
3735{
3736 u32 epctrl_reg;
3737 u32 epint_reg;
3738
3739 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3740 DOEPCTL(hs_ep->index);
3741 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3742 DOEPINT(hs_ep->index);
3743
3744 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3745 hs_ep->name);
3746
3747 if (hs_ep->dir_in) {
3748 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3749 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3750 /* Wait for Nak effect */
3751 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3752 DXEPINT_INEPNAKEFF, 100))
3753 dev_warn(hsotg->dev,
3754 "%s: timeout DIEPINT.NAKEFF\n",
3755 __func__);
3756 } else {
3757 __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
3758 /* Wait for Nak effect */
3759 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3760 GINTSTS_GINNAKEFF, 100))
3761 dev_warn(hsotg->dev,
3762 "%s: timeout GINTSTS.GINNAKEFF\n",
3763 __func__);
3764 }
3765 } else {
3766 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3767 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3768
3769 /* Wait for global nak to take effect */
3770 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3771 GINTSTS_GOUTNAKEFF, 100))
3772 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3773 __func__);
3774 }
3775
3776 /* Disable ep */
3777 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3778
3779 /* Wait for ep to be disabled */
3780 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3781 dev_warn(hsotg->dev,
3782 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3783
3784 /* Clear EPDISBLD interrupt */
3785 __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
3786
3787 if (hs_ep->dir_in) {
3788 unsigned short fifo_index;
3789
3790 if (hsotg->dedicated_fifos || hs_ep->periodic)
3791 fifo_index = hs_ep->fifo_index;
3792 else
3793 fifo_index = 0;
3794
3795 /* Flush TX FIFO */
3796 dwc2_flush_tx_fifo(hsotg, fifo_index);
3797
3798 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3799 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3800 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3801
3802 } else {
3803 /* Remove global NAKs */
3804 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
3805 }
3806}
3807
3808/**
3809 * dwc2_hsotg_ep_enable - enable the given endpoint
3810 * @ep: The USB endpint to configure
3811 * @desc: The USB endpoint descriptor to configure with.
3812 *
3813 * This is called from the USB gadget code's usb_ep_enable().
3814 */
3815static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
3816 const struct usb_endpoint_descriptor *desc)
3817{
3818 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
3819 struct dwc2_hsotg *hsotg = hs_ep->parent;
3820 unsigned long flags;
3821 unsigned int index = hs_ep->index;
3822 u32 epctrl_reg;
3823 u32 epctrl;
3824 u32 mps;
3825 u32 mc;
3826 u32 mask;
3827 unsigned int dir_in;
3828 unsigned int i, val, size;
3829 int ret = 0;
3830
3831 dev_dbg(hsotg->dev,
3832 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3833 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3834 desc->wMaxPacketSize, desc->bInterval);
3835
3836 /* not to be called for EP0 */
3837 if (index == 0) {
3838 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3839 return -EINVAL;
3840 }
3841
3842 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3843 if (dir_in != hs_ep->dir_in) {
3844 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3845 return -EINVAL;
3846 }
3847
3848 mps = usb_endpoint_maxp(desc);
3849 mc = usb_endpoint_maxp_mult(desc);
3850
3851 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
3852
3853 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
3854 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
3855
3856 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3857 __func__, epctrl, epctrl_reg);
3858
3859 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3860 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3861 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
3862 MAX_DMA_DESC_NUM_GENERIC *
3863 sizeof(struct dwc2_dma_desc),
3864 &hs_ep->desc_list_dma, GFP_ATOMIC);
3865 if (!hs_ep->desc_list) {
3866 ret = -ENOMEM;
3867 goto error2;
3868 }
3869 }
3870
3871 spin_lock_irqsave(&hsotg->lock, flags);
3872
3873 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3874 epctrl |= DXEPCTL_MPS(mps);
3875
3876 /*
3877 * mark the endpoint as active, otherwise the core may ignore
3878 * transactions entirely for this endpoint
3879 */
3880 epctrl |= DXEPCTL_USBACTEP;
3881
3882 /* update the endpoint state */
3883 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
3884
3885 /* default, set to non-periodic */
3886 hs_ep->isochronous = 0;
3887 hs_ep->periodic = 0;
3888 hs_ep->halted = 0;
3889 hs_ep->interval = desc->bInterval;
3890
3891 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3892 case USB_ENDPOINT_XFER_ISOC:
3893 epctrl |= DXEPCTL_EPTYPE_ISO;
3894 epctrl |= DXEPCTL_SETEVENFR;
3895 hs_ep->isochronous = 1;
3896 hs_ep->interval = 1 << (desc->bInterval - 1);
3897 hs_ep->target_frame = TARGET_FRAME_INITIAL;
3898 hs_ep->isoc_chain_num = 0;
3899 hs_ep->next_desc = 0;
3900 if (dir_in) {
3901 hs_ep->periodic = 1;
3902 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3903 mask |= DIEPMSK_NAKMSK;
3904 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3905 } else {
3906 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3907 mask |= DOEPMSK_OUTTKNEPDISMSK;
3908 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3909 }
3910 break;
3911
3912 case USB_ENDPOINT_XFER_BULK:
3913 epctrl |= DXEPCTL_EPTYPE_BULK;
3914 break;
3915
3916 case USB_ENDPOINT_XFER_INT:
3917 if (dir_in)
3918 hs_ep->periodic = 1;
3919
3920 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3921 hs_ep->interval = 1 << (desc->bInterval - 1);
3922
3923 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
3924 break;
3925
3926 case USB_ENDPOINT_XFER_CONTROL:
3927 epctrl |= DXEPCTL_EPTYPE_CONTROL;
3928 break;
3929 }
3930
3931 /*
3932 * if the hardware has dedicated fifos, we must give each IN EP
3933 * a unique tx-fifo even if it is non-periodic.
3934 */
3935 if (dir_in && hsotg->dedicated_fifos) {
3936 unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
3937 u32 fifo_index = 0;
3938 u32 fifo_size = UINT_MAX;
3939
3940 size = hs_ep->ep.maxpacket * hs_ep->mc;
3941 for (i = 1; i <= fifo_count; ++i) {
3942 if (hsotg->fifo_map & (1 << i))
3943 continue;
3944 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
3945 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
3946 if (val < size)
3947 continue;
3948 /* Search for smallest acceptable fifo */
3949 if (val < fifo_size) {
3950 fifo_size = val;
3951 fifo_index = i;
3952 }
3953 }
3954 if (!fifo_index) {
3955 dev_err(hsotg->dev,
3956 "%s: No suitable fifo found\n", __func__);
3957 ret = -ENOMEM;
3958 goto error1;
3959 }
3960 hsotg->fifo_map |= 1 << fifo_index;
3961 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3962 hs_ep->fifo_index = fifo_index;
3963 hs_ep->fifo_size = fifo_size;
3964 }
3965
3966 /* for non control endpoints, set PID to D0 */
3967 if (index && !hs_ep->isochronous)
3968 epctrl |= DXEPCTL_SETD0PID;
3969
3970 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3971 __func__, epctrl);
3972
3973 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
3974 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
3975 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
3976
3977 /* enable the endpoint interrupt */
3978 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
3979
3980error1:
3981 spin_unlock_irqrestore(&hsotg->lock, flags);
3982
3983error2:
3984 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3985 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3986 sizeof(struct dwc2_dma_desc),
3987 hs_ep->desc_list, hs_ep->desc_list_dma);
3988 hs_ep->desc_list = NULL;
3989 }
3990
3991 return ret;
3992}
3993
3994/**
3995 * dwc2_hsotg_ep_disable - disable given endpoint
3996 * @ep: The endpoint to disable.
3997 */
3998static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
3999{
4000 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4001 struct dwc2_hsotg *hsotg = hs_ep->parent;
4002 int dir_in = hs_ep->dir_in;
4003 int index = hs_ep->index;
4004 unsigned long flags;
4005 u32 epctrl_reg;
4006 u32 ctrl;
4007
4008 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
4009
4010 if (ep == &hsotg->eps_out[0]->ep) {
4011 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4012 return -EINVAL;
4013 }
4014
4015 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
4016
4017 spin_lock_irqsave(&hsotg->lock, flags);
4018
4019 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
4020
4021 if (ctrl & DXEPCTL_EPENA)
4022 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4023
4024 ctrl &= ~DXEPCTL_EPENA;
4025 ctrl &= ~DXEPCTL_USBACTEP;
4026 ctrl |= DXEPCTL_SNAK;
4027
4028 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
4029 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
4030
4031 /* disable endpoint interrupts */
4032 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
4033
4034 /* terminate all requests with shutdown */
4035 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4036
4037 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4038 hs_ep->fifo_index = 0;
4039 hs_ep->fifo_size = 0;
4040
4041 spin_unlock_irqrestore(&hsotg->lock, flags);
4042 return 0;
4043}
4044
4045/**
4046 * on_list - check request is on the given endpoint
4047 * @ep: The endpoint to check.
4048 * @test: The request to test if it is on the endpoint.
4049 */
4050static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
4051{
4052 struct dwc2_hsotg_req *req, *treq;
4053
4054 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4055 if (req == test)
4056 return true;
4057 }
4058
4059 return false;
4060}
4061
4062/**
4063 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
4064 * @ep: The endpoint to dequeue.
4065 * @req: The request to be removed from a queue.
4066 */
4067static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
4068{
4069 struct dwc2_hsotg_req *hs_req = our_req(req);
4070 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4071 struct dwc2_hsotg *hs = hs_ep->parent;
4072 unsigned long flags;
4073
4074 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
4075
4076 spin_lock_irqsave(&hs->lock, flags);
4077
4078 if (!on_list(hs_ep, hs_req)) {
4079 spin_unlock_irqrestore(&hs->lock, flags);
4080 return -EINVAL;
4081 }
4082
4083 /* Dequeue already started request */
4084 if (req == &hs_ep->req->req)
4085 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4086
4087 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
4088 spin_unlock_irqrestore(&hs->lock, flags);
4089
4090 return 0;
4091}
4092
4093/**
4094 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
4095 * @ep: The endpoint to set halt.
4096 * @value: Set or unset the halt.
4097 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4098 * the endpoint is busy processing requests.
4099 *
4100 * We need to stall the endpoint immediately if request comes from set_feature
4101 * protocol command handler.
4102 */
4103static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
4104{
4105 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4106 struct dwc2_hsotg *hs = hs_ep->parent;
4107 int index = hs_ep->index;
4108 u32 epreg;
4109 u32 epctl;
4110 u32 xfertype;
4111
4112 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4113
4114 if (index == 0) {
4115 if (value)
4116 dwc2_hsotg_stall_ep0(hs);
4117 else
4118 dev_warn(hs->dev,
4119 "%s: can't clear halt on ep0\n", __func__);
4120 return 0;
4121 }
4122
4123 if (hs_ep->isochronous) {
4124 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4125 return -EINVAL;
4126 }
4127
4128 if (!now && value && !list_empty(&hs_ep->queue)) {
4129 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4130 ep->name);
4131 return -EAGAIN;
4132 }
4133
4134 if (hs_ep->dir_in) {
4135 epreg = DIEPCTL(index);
4136 epctl = dwc2_readl(hs->regs + epreg);
4137
4138 if (value) {
4139 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
4140 if (epctl & DXEPCTL_EPENA)
4141 epctl |= DXEPCTL_EPDIS;
4142 } else {
4143 epctl &= ~DXEPCTL_STALL;
4144 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4145 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4146 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4147 epctl |= DXEPCTL_SETD0PID;
4148 }
4149 dwc2_writel(epctl, hs->regs + epreg);
4150 } else {
4151 epreg = DOEPCTL(index);
4152 epctl = dwc2_readl(hs->regs + epreg);
4153
4154 if (value) {
4155 epctl |= DXEPCTL_STALL;
4156 } else {
4157 epctl &= ~DXEPCTL_STALL;
4158 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4159 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4160 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4161 epctl |= DXEPCTL_SETD0PID;
4162 }
4163 dwc2_writel(epctl, hs->regs + epreg);
4164 }
4165
4166 hs_ep->halted = value;
4167
4168 return 0;
4169}
4170
4171/**
4172 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
4173 * @ep: The endpoint to set halt.
4174 * @value: Set or unset the halt.
4175 */
4176static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
4177{
4178 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4179 struct dwc2_hsotg *hs = hs_ep->parent;
4180 unsigned long flags = 0;
4181 int ret = 0;
4182
4183 spin_lock_irqsave(&hs->lock, flags);
4184 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
4185 spin_unlock_irqrestore(&hs->lock, flags);
4186
4187 return ret;
4188}
4189
4190static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
4191 .enable = dwc2_hsotg_ep_enable,
4192 .disable = dwc2_hsotg_ep_disable,
4193 .alloc_request = dwc2_hsotg_ep_alloc_request,
4194 .free_request = dwc2_hsotg_ep_free_request,
4195 .queue = dwc2_hsotg_ep_queue_lock,
4196 .dequeue = dwc2_hsotg_ep_dequeue,
4197 .set_halt = dwc2_hsotg_ep_sethalt_lock,
4198 /* note, don't believe we have any call for the fifo routines */
4199};
4200
4201/**
4202 * dwc2_hsotg_init - initialize the usb core
4203 * @hsotg: The driver state
4204 */
4205static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
4206{
4207 u32 trdtim;
4208 u32 usbcfg;
4209 /* unmask subset of endpoint interrupts */
4210
4211 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4212 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4213 hsotg->regs + DIEPMSK);
4214
4215 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4216 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4217 hsotg->regs + DOEPMSK);
4218
4219 dwc2_writel(0, hsotg->regs + DAINTMSK);
4220
4221 /* Be in disconnected state until gadget is registered */
4222 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
4223
4224 /* setup fifos */
4225
4226 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
4227 dwc2_readl(hsotg->regs + GRXFSIZ),
4228 dwc2_readl(hsotg->regs + GNPTXFSIZ));
4229
4230 dwc2_hsotg_init_fifo(hsotg);
4231
4232 /* keep other bits untouched (so e.g. forced modes are not lost) */
4233 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4234 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
4235 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
4236
4237 /* set the PLL on, remove the HNP/SRP and set the PHY */
4238 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
4239 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4240 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4241 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
4242
4243 if (using_dma(hsotg))
4244 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
4245}
4246
4247/**
4248 * dwc2_hsotg_udc_start - prepare the udc for work
4249 * @gadget: The usb gadget state
4250 * @driver: The usb gadget driver
4251 *
4252 * Perform initialization to prepare udc device and driver
4253 * to work.
4254 */
4255static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
4256 struct usb_gadget_driver *driver)
4257{
4258 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4259 unsigned long flags;
4260 int ret;
4261
4262 if (!hsotg) {
4263 pr_err("%s: called with no device\n", __func__);
4264 return -ENODEV;
4265 }
4266
4267 if (!driver) {
4268 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4269 return -EINVAL;
4270 }
4271
4272 if (driver->max_speed < USB_SPEED_FULL)
4273 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
4274
4275 if (!driver->setup) {
4276 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4277 return -EINVAL;
4278 }
4279
4280 WARN_ON(hsotg->driver);
4281
4282 driver->driver.bus = NULL;
4283 hsotg->driver = driver;
4284 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
4285 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4286
4287 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4288 ret = dwc2_lowlevel_hw_enable(hsotg);
4289 if (ret)
4290 goto err;
4291 }
4292
4293 if (!IS_ERR_OR_NULL(hsotg->uphy))
4294 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
4295
4296 spin_lock_irqsave(&hsotg->lock, flags);
4297 if (dwc2_hw_is_device(hsotg)) {
4298 dwc2_hsotg_init(hsotg);
4299 dwc2_hsotg_core_init_disconnected(hsotg, false);
4300 }
4301
4302 hsotg->enabled = 0;
4303 spin_unlock_irqrestore(&hsotg->lock, flags);
4304
4305 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
4306
4307 return 0;
4308
4309err:
4310 hsotg->driver = NULL;
4311 return ret;
4312}
4313
4314/**
4315 * dwc2_hsotg_udc_stop - stop the udc
4316 * @gadget: The usb gadget state
4317 * @driver: The usb gadget driver
4318 *
4319 * Stop udc hw block and stay tunned for future transmissions
4320 */
4321static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
4322{
4323 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4324 unsigned long flags = 0;
4325 int ep;
4326
4327 if (!hsotg)
4328 return -ENODEV;
4329
4330 /* all endpoints should be shutdown */
4331 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4332 if (hsotg->eps_in[ep])
4333 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
4334 if (hsotg->eps_out[ep])
4335 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
4336 }
4337
4338 spin_lock_irqsave(&hsotg->lock, flags);
4339
4340 hsotg->driver = NULL;
4341 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4342 hsotg->enabled = 0;
4343
4344 spin_unlock_irqrestore(&hsotg->lock, flags);
4345
4346 if (!IS_ERR_OR_NULL(hsotg->uphy))
4347 otg_set_peripheral(hsotg->uphy->otg, NULL);
4348
4349 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4350 dwc2_lowlevel_hw_disable(hsotg);
4351
4352 return 0;
4353}
4354
4355/**
4356 * dwc2_hsotg_gadget_getframe - read the frame number
4357 * @gadget: The usb gadget state
4358 *
4359 * Read the {micro} frame number
4360 */
4361static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
4362{
4363 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
4364}
4365
4366/**
4367 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
4368 * @gadget: The usb gadget state
4369 * @is_on: Current state of the USB PHY
4370 *
4371 * Connect/Disconnect the USB PHY pullup
4372 */
4373static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
4374{
4375 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4376 unsigned long flags = 0;
4377
4378 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
4379 hsotg->op_state);
4380
4381 /* Don't modify pullup state while in host mode */
4382 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4383 hsotg->enabled = is_on;
4384 return 0;
4385 }
4386
4387 spin_lock_irqsave(&hsotg->lock, flags);
4388 if (is_on) {
4389 hsotg->enabled = 1;
4390 dwc2_hsotg_core_init_disconnected(hsotg, false);
4391 dwc2_hsotg_core_connect(hsotg);
4392 } else {
4393 dwc2_hsotg_core_disconnect(hsotg);
4394 dwc2_hsotg_disconnect(hsotg);
4395 hsotg->enabled = 0;
4396 }
4397
4398 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4399 spin_unlock_irqrestore(&hsotg->lock, flags);
4400
4401 return 0;
4402}
4403
4404static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
4405{
4406 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4407 unsigned long flags;
4408
4409 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4410 spin_lock_irqsave(&hsotg->lock, flags);
4411
4412 /*
4413 * If controller is hibernated, it must exit from hibernation
4414 * before being initialized / de-initialized
4415 */
4416 if (hsotg->lx_state == DWC2_L2)
4417 dwc2_exit_hibernation(hsotg, false);
4418
4419 if (is_active) {
4420 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
4421
4422 dwc2_hsotg_core_init_disconnected(hsotg, false);
4423 if (hsotg->enabled)
4424 dwc2_hsotg_core_connect(hsotg);
4425 } else {
4426 dwc2_hsotg_core_disconnect(hsotg);
4427 dwc2_hsotg_disconnect(hsotg);
4428 }
4429
4430 spin_unlock_irqrestore(&hsotg->lock, flags);
4431 return 0;
4432}
4433
4434/**
4435 * dwc2_hsotg_vbus_draw - report bMaxPower field
4436 * @gadget: The usb gadget state
4437 * @mA: Amount of current
4438 *
4439 * Report how much power the device may consume to the phy.
4440 */
4441static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
4442{
4443 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4444
4445 if (IS_ERR_OR_NULL(hsotg->uphy))
4446 return -ENOTSUPP;
4447 return usb_phy_set_power(hsotg->uphy, mA);
4448}
4449
4450static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4451 .get_frame = dwc2_hsotg_gadget_getframe,
4452 .udc_start = dwc2_hsotg_udc_start,
4453 .udc_stop = dwc2_hsotg_udc_stop,
4454 .pullup = dwc2_hsotg_pullup,
4455 .vbus_session = dwc2_hsotg_vbus_session,
4456 .vbus_draw = dwc2_hsotg_vbus_draw,
4457};
4458
4459/**
4460 * dwc2_hsotg_initep - initialise a single endpoint
4461 * @hsotg: The device state.
4462 * @hs_ep: The endpoint to be initialised.
4463 * @epnum: The endpoint number
4464 *
4465 * Initialise the given endpoint (as part of the probe and device state
4466 * creation) to give to the gadget driver. Setup the endpoint name, any
4467 * direction information and other state that may be required.
4468 */
4469static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
4470 struct dwc2_hsotg_ep *hs_ep,
4471 int epnum,
4472 bool dir_in)
4473{
4474 char *dir;
4475
4476 if (epnum == 0)
4477 dir = "";
4478 else if (dir_in)
4479 dir = "in";
4480 else
4481 dir = "out";
4482
4483 hs_ep->dir_in = dir_in;
4484 hs_ep->index = epnum;
4485
4486 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4487
4488 INIT_LIST_HEAD(&hs_ep->queue);
4489 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4490
4491 /* add to the list of endpoints known by the gadget driver */
4492 if (epnum)
4493 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4494
4495 hs_ep->parent = hsotg;
4496 hs_ep->ep.name = hs_ep->name;
4497
4498 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4499 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4500 else
4501 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4502 epnum ? 1024 : EP0_MPS_LIMIT);
4503 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
4504
4505 if (epnum == 0) {
4506 hs_ep->ep.caps.type_control = true;
4507 } else {
4508 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4509 hs_ep->ep.caps.type_iso = true;
4510 hs_ep->ep.caps.type_bulk = true;
4511 }
4512 hs_ep->ep.caps.type_int = true;
4513 }
4514
4515 if (dir_in)
4516 hs_ep->ep.caps.dir_in = true;
4517 else
4518 hs_ep->ep.caps.dir_out = true;
4519
4520 /*
4521 * if we're using dma, we need to set the next-endpoint pointer
4522 * to be something valid.
4523 */
4524
4525 if (using_dma(hsotg)) {
4526 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
4527
4528 if (dir_in)
4529 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
4530 else
4531 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
4532 }
4533}
4534
4535/**
4536 * dwc2_hsotg_hw_cfg - read HW configuration registers
4537 * @param: The device state
4538 *
4539 * Read the USB core HW configuration registers
4540 */
4541static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
4542{
4543 u32 cfg;
4544 u32 ep_type;
4545 u32 i;
4546
4547 /* check hardware configuration */
4548
4549 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4550
4551 /* Add ep0 */
4552 hsotg->num_of_eps++;
4553
4554 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4555 sizeof(struct dwc2_hsotg_ep),
4556 GFP_KERNEL);
4557 if (!hsotg->eps_in[0])
4558 return -ENOMEM;
4559 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
4560 hsotg->eps_out[0] = hsotg->eps_in[0];
4561
4562 cfg = hsotg->hw_params.dev_ep_dirs;
4563 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
4564 ep_type = cfg & 3;
4565 /* Direction in or both */
4566 if (!(ep_type & 2)) {
4567 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
4568 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
4569 if (!hsotg->eps_in[i])
4570 return -ENOMEM;
4571 }
4572 /* Direction out or both */
4573 if (!(ep_type & 1)) {
4574 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
4575 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
4576 if (!hsotg->eps_out[i])
4577 return -ENOMEM;
4578 }
4579 }
4580
4581 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4582 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
4583
4584 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4585 hsotg->num_of_eps,
4586 hsotg->dedicated_fifos ? "dedicated" : "shared",
4587 hsotg->fifo_mem);
4588 return 0;
4589}
4590
4591/**
4592 * dwc2_hsotg_dump - dump state of the udc
4593 * @param: The device state
4594 */
4595static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
4596{
4597#ifdef DEBUG
4598 struct device *dev = hsotg->dev;
4599 void __iomem *regs = hsotg->regs;
4600 u32 val;
4601 int idx;
4602
4603 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
4604 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4605 dwc2_readl(regs + DIEPMSK));
4606
4607 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
4608 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
4609
4610 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
4611 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
4612
4613 /* show periodic fifo settings */
4614
4615 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
4616 val = dwc2_readl(regs + DPTXFSIZN(idx));
4617 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
4618 val >> FIFOSIZE_DEPTH_SHIFT,
4619 val & FIFOSIZE_STARTADDR_MASK);
4620 }
4621
4622 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
4623 dev_info(dev,
4624 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
4625 dwc2_readl(regs + DIEPCTL(idx)),
4626 dwc2_readl(regs + DIEPTSIZ(idx)),
4627 dwc2_readl(regs + DIEPDMA(idx)));
4628
4629 val = dwc2_readl(regs + DOEPCTL(idx));
4630 dev_info(dev,
4631 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
4632 idx, dwc2_readl(regs + DOEPCTL(idx)),
4633 dwc2_readl(regs + DOEPTSIZ(idx)),
4634 dwc2_readl(regs + DOEPDMA(idx)));
4635 }
4636
4637 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
4638 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
4639#endif
4640}
4641
4642/**
4643 * dwc2_gadget_init - init function for gadget
4644 * @dwc2: The data structure for the DWC2 driver.
4645 * @irq: The IRQ number for the controller.
4646 */
4647int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
4648{
4649 struct device *dev = hsotg->dev;
4650 int epnum;
4651 int ret;
4652
4653 /* Dump fifo information */
4654 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
4655 hsotg->params.g_np_tx_fifo_size);
4656 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
4657
4658 hsotg->gadget.max_speed = USB_SPEED_HIGH;
4659 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
4660 hsotg->gadget.name = dev_name(dev);
4661 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4662 hsotg->gadget.is_otg = 1;
4663 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4664 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
4665
4666 ret = dwc2_hsotg_hw_cfg(hsotg);
4667 if (ret) {
4668 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
4669 return ret;
4670 }
4671
4672 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4673 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
4674 if (!hsotg->ctrl_buff)
4675 return -ENOMEM;
4676
4677 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4678 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
4679 if (!hsotg->ep0_buff)
4680 return -ENOMEM;
4681
4682 if (using_desc_dma(hsotg)) {
4683 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4684 if (ret < 0)
4685 return ret;
4686 }
4687
4688 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
4689 dev_name(hsotg->dev), hsotg);
4690 if (ret < 0) {
4691 dev_err(dev, "cannot claim IRQ for gadget\n");
4692 return ret;
4693 }
4694
4695 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4696
4697 if (hsotg->num_of_eps == 0) {
4698 dev_err(dev, "wrong number of EPs (zero)\n");
4699 return -EINVAL;
4700 }
4701
4702 /* setup endpoint information */
4703
4704 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
4705 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
4706
4707 /* allocate EP0 request */
4708
4709 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
4710 GFP_KERNEL);
4711 if (!hsotg->ctrl_req) {
4712 dev_err(dev, "failed to allocate ctrl req\n");
4713 return -ENOMEM;
4714 }
4715
4716 /* initialise the endpoints now the core has been initialised */
4717 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4718 if (hsotg->eps_in[epnum])
4719 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
4720 epnum, 1);
4721 if (hsotg->eps_out[epnum])
4722 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
4723 epnum, 0);
4724 }
4725
4726 dwc2_hsotg_dump(hsotg);
4727
4728 return 0;
4729}
4730
4731/**
4732 * dwc2_hsotg_remove - remove function for hsotg driver
4733 * @pdev: The platform information for the driver
4734 */
4735int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
4736{
4737 usb_del_gadget_udc(&hsotg->gadget);
4738 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
4739
4740 return 0;
4741}
4742
4743int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
4744{
4745 unsigned long flags;
4746
4747 if (hsotg->lx_state != DWC2_L0)
4748 return 0;
4749
4750 if (hsotg->driver) {
4751 int ep;
4752
4753 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4754 hsotg->driver->driver.name);
4755
4756 spin_lock_irqsave(&hsotg->lock, flags);
4757 if (hsotg->enabled)
4758 dwc2_hsotg_core_disconnect(hsotg);
4759 dwc2_hsotg_disconnect(hsotg);
4760 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4761 spin_unlock_irqrestore(&hsotg->lock, flags);
4762
4763 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4764 if (hsotg->eps_in[ep])
4765 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
4766 if (hsotg->eps_out[ep])
4767 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
4768 }
4769 }
4770
4771 return 0;
4772}
4773
4774int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
4775{
4776 unsigned long flags;
4777
4778 if (hsotg->lx_state == DWC2_L2)
4779 return 0;
4780
4781 if (hsotg->driver) {
4782 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4783 hsotg->driver->driver.name);
4784
4785 spin_lock_irqsave(&hsotg->lock, flags);
4786 dwc2_hsotg_core_init_disconnected(hsotg, false);
4787 if (hsotg->enabled)
4788 dwc2_hsotg_core_connect(hsotg);
4789 spin_unlock_irqrestore(&hsotg->lock, flags);
4790 }
4791
4792 return 0;
4793}
4794
4795/**
4796 * dwc2_backup_device_registers() - Backup controller device registers.
4797 * When suspending usb bus, registers needs to be backuped
4798 * if controller power is disabled once suspended.
4799 *
4800 * @hsotg: Programming view of the DWC_otg controller
4801 */
4802int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4803{
4804 struct dwc2_dregs_backup *dr;
4805 int i;
4806
4807 dev_dbg(hsotg->dev, "%s\n", __func__);
4808
4809 /* Backup dev regs */
4810 dr = &hsotg->dr_backup;
4811
4812 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4813 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4814 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4815 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4816 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4817
4818 for (i = 0; i < hsotg->num_of_eps; i++) {
4819 /* Backup IN EPs */
4820 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4821
4822 /* Ensure DATA PID is correctly configured */
4823 if (dr->diepctl[i] & DXEPCTL_DPID)
4824 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4825 else
4826 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4827
4828 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4829 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4830
4831 /* Backup OUT EPs */
4832 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4833
4834 /* Ensure DATA PID is correctly configured */
4835 if (dr->doepctl[i] & DXEPCTL_DPID)
4836 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4837 else
4838 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4839
4840 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4841 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4842 }
4843 dr->valid = true;
4844 return 0;
4845}
4846
4847/**
4848 * dwc2_restore_device_registers() - Restore controller device registers.
4849 * When resuming usb bus, device registers needs to be restored
4850 * if controller power were disabled.
4851 *
4852 * @hsotg: Programming view of the DWC_otg controller
4853 */
4854int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4855{
4856 struct dwc2_dregs_backup *dr;
4857 u32 dctl;
4858 int i;
4859
4860 dev_dbg(hsotg->dev, "%s\n", __func__);
4861
4862 /* Restore dev regs */
4863 dr = &hsotg->dr_backup;
4864 if (!dr->valid) {
4865 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4866 __func__);
4867 return -EINVAL;
4868 }
4869 dr->valid = false;
4870
4871 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4872 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4873 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4874 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4875 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4876
4877 for (i = 0; i < hsotg->num_of_eps; i++) {
4878 /* Restore IN EPs */
4879 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4880 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4881 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4882
4883 /* Restore OUT EPs */
4884 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4885 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4886 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4887 }
4888
4889 /* Set the Power-On Programming done bit */
4890 dctl = dwc2_readl(hsotg->regs + DCTL);
4891 dctl |= DCTL_PWRONPRGDONE;
4892 dwc2_writel(dctl, hsotg->regs + DCTL);
4893
4894 return 0;
4895}