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