blob: 70ac47a341ac25a5f5aae5915341664639f0cd43 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
Googleraf606d22022-10-26 21:40:12 -07003 * 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
Googleraf606d22022-10-26 21:40:12 -070012 */
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>
Googler9398cc32022-12-02 17:21:52 +080030#include <linux/usb/composite.h>
31
Googleraf606d22022-10-26 21:40:12 -070032
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
Googler9398cc32022-12-02 17:21:52 +080052static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Googleraf606d22022-10-26 21:40:12 -070053{
Googler9398cc32022-12-02 17:21:52 +080054 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
Googleraf606d22022-10-26 21:40:12 -070055}
56
Googler9398cc32022-12-02 17:21:52 +080057static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Googleraf606d22022-10-26 21:40:12 -070058{
Googler9398cc32022-12-02 17:21:52 +080059 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
Googleraf606d22022-10-26 21:40:12 -070060}
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{
Googler9398cc32022-12-02 17:21:52 +080095 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;
Googleraf606d22022-10-26 21:40:12 -0700107}
108
109/**
110 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
111 * @hs_ep: The endpoint
Googleraf606d22022-10-26 21:40:12 -0700112 *
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;
Googlerb48fa912023-03-17 12:40:29 +0530119 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Googler9398cc32022-12-02 17:21:52 +0800120 hs_ep->frame_overrun = true;
Googlerb48fa912023-03-17 12:40:29 +0530121 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
Googleraf606d22022-10-26 21:40:12 -0700122 } else {
Googler9398cc32022-12-02 17:21:52 +0800123 hs_ep->frame_overrun = false;
Googleraf606d22022-10-26 21:40:12 -0700124 }
125}
126
127/**
Googler9398cc32022-12-02 17:21:52 +0800128 * 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{
Googler9398cc32022-12-02 17:21:52 +0800139 if (hs_ep->target_frame)
140 hs_ep->target_frame -= 1;
141 else
Googlerb48fa912023-03-17 12:40:29 +0530142 hs_ep->target_frame = DSTS_SOFFN_LIMIT;
Googler9398cc32022-12-02 17:21:52 +0800143}
144
145/**
Googleraf606d22022-10-26 21:40:12 -0700146 * 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{
Googler9398cc32022-12-02 17:21:52 +0800152 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700153 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);
Googler9398cc32022-12-02 17:21:52 +0800159 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700160 }
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{
Googler9398cc32022-12-02 17:21:52 +0800170 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700171 u32 new_gsintmsk;
172
173 new_gsintmsk = gsintmsk & ~ints;
174
175 if (new_gsintmsk != gsintmsk)
Googler9398cc32022-12-02 17:21:52 +0800176 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700177}
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,
Googler9398cc32022-12-02 17:21:52 +0800190 unsigned int ep, unsigned int dir_in,
Googleraf606d22022-10-26 21:40:12 -0700191 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);
Googler9398cc32022-12-02 17:21:52 +0800201 daint = dwc2_readl(hsotg, DAINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700202 if (en)
203 daint |= bit;
204 else
205 daint &= ~bit;
Googler9398cc32022-12-02 17:21:52 +0800206 dwc2_writel(hsotg, daint, DAINTMSK);
Googleraf606d22022-10-26 21:40:12 -0700207 local_irq_restore(flags);
208}
209
210/**
Googler9398cc32022-12-02 17:21:52 +0800211 * 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/**
251 * 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/**
272 * 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/**
Googleraf606d22022-10-26 21:40:12 -0700293 * 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;
Googler9398cc32022-12-02 17:21:52 +0800301
Googleraf606d22022-10-26 21:40:12 -0700302 u32 val;
Googler9398cc32022-12-02 17:21:52 +0800303 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Googleraf606d22022-10-26 21:40:12 -0700304
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 */
Googler9398cc32022-12-02 17:21:52 +0800310 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);
Googleraf606d22022-10-26 21:40:12 -0700315
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 */
Googler9398cc32022-12-02 17:21:52 +0800324 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Googleraf606d22022-10-26 21:40:12 -0700325
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++) {
Googler9398cc32022-12-02 17:21:52 +0800332 if (!txfsz[ep])
Googleraf606d22022-10-26 21:40:12 -0700333 continue;
334 val = addr;
Googler9398cc32022-12-02 17:21:52 +0800335 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
336 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
Googleraf606d22022-10-26 21:40:12 -0700337 "insufficient fifo memory");
Googler9398cc32022-12-02 17:21:52 +0800338 addr += txfsz[ep];
Googleraf606d22022-10-26 21:40:12 -0700339
Googler9398cc32022-12-02 17:21:52 +0800340 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
341 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
Googleraf606d22022-10-26 21:40:12 -0700342 }
343
Googler9398cc32022-12-02 17:21:52 +0800344 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
345 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
346 GDFIFOCFG);
Googleraf606d22022-10-26 21:40:12 -0700347 /*
348 * according to p428 of the design guide, we need to ensure that
349 * all fifos are flushed before continuing
350 */
351
Googler9398cc32022-12-02 17:21:52 +0800352 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
353 GRSTCTL_RXFFLSH, GRSTCTL);
Googleraf606d22022-10-26 21:40:12 -0700354
355 /* wait until the fifos are both flushed */
356 timeout = 100;
357 while (1) {
Googler9398cc32022-12-02 17:21:52 +0800358 val = dwc2_readl(hsotg, GRSTCTL);
Googleraf606d22022-10-26 21:40:12 -0700359
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/**
Googler9398cc32022-12-02 17:21:52 +0800377 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Googleraf606d22022-10-26 21:40:12 -0700378 * @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,
Googler9398cc32022-12-02 17:21:52 +0800384 gfp_t flags)
Googleraf606d22022-10-26 21:40:12 -0700385{
386 struct dwc2_hsotg_req *req;
387
Googler9398cc32022-12-02 17:21:52 +0800388 req = kzalloc(sizeof(*req), flags);
Googleraf606d22022-10-26 21:40:12 -0700389 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,
Googler9398cc32022-12-02 17:21:52 +0800419 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -0700420 struct dwc2_hsotg_req *hs_req)
421{
422 struct usb_request *req = &hs_req->req;
423
Googlerb48fa912023-03-17 12:40:29 +0530424 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Googler38bda472022-08-19 10:07:08 -0700425}
Googler4f18c0c2022-09-20 17:23:36 +0800426
Googler9398cc32022-12-02 17:21:52 +0800427/*
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
Googleraf606d22022-10-26 21:40:12 -0700475/**
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,
Googler9398cc32022-12-02 17:21:52 +0800492 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -0700493 struct dwc2_hsotg_req *hs_req)
494{
495 bool periodic = is_ep_periodic(hs_ep);
Googler9398cc32022-12-02 17:21:52 +0800496 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
Googleraf606d22022-10-26 21:40:12 -0700497 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) {
Googler9398cc32022-12-02 17:21:52 +0800511 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Googleraf606d22022-10-26 21:40:12 -0700512 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) {
Googler9398cc32022-12-02 17:21:52 +0800552 can_write = dwc2_readl(hsotg,
553 DTXFSTS(hs_ep->fifo_index));
Googleraf606d22022-10-26 21:40:12 -0700554
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",
Googler9398cc32022-12-02 17:21:52 +0800574 __func__, gnptxsts, can_write, to_write, max_transfer);
Googleraf606d22022-10-26 21:40:12 -0700575
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,
Googler9398cc32022-12-02 17:21:52 +0800595 periodic ? GINTSTS_PTXFEMP :
Googleraf606d22022-10-26 21:40:12 -0700596 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,
Googler9398cc32022-12-02 17:21:52 +0800624 periodic ? GINTSTS_PTXFEMP :
Googleraf606d22022-10-26 21:40:12 -0700625 GINTSTS_NPTXFEMP);
626 }
627
628 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
Googler9398cc32022-12-02 17:21:52 +0800629 to_write, hs_req->req.length, can_write, buf_pos);
Googleraf606d22022-10-26 21:40:12 -0700630
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
Googler9398cc32022-12-02 17:21:52 +0800643 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
Googleraf606d22022-10-26 21:40:12 -0700644
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 */
Googler9398cc32022-12-02 17:21:52 +0800655static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Googleraf606d22022-10-26 21:40:12 -0700656{
657 int index = hs_ep->index;
Googler9398cc32022-12-02 17:21:52 +0800658 unsigned int maxsize;
659 unsigned int maxpkt;
Googleraf606d22022-10-26 21:40:12 -0700660
661 if (index != 0) {
662 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
663 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
664 } else {
Googler9398cc32022-12-02 17:21:52 +0800665 maxsize = 64 + 64;
Googleraf606d22022-10-26 21:40:12 -0700666 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/**
Googler9398cc32022-12-02 17:21:52 +0800688 * dwc2_hsotg_read_frameno - read current frame number
689 * @hsotg: The device instance
690 *
691 * Return the current frame number
692 */
Googleraf606d22022-10-26 21:40:12 -0700693static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
694{
695 u32 dsts;
696
Googler9398cc32022-12-02 17:21:52 +0800697 dsts = dwc2_readl(hsotg, DSTS);
Googleraf606d22022-10-26 21:40:12 -0700698 dsts &= DSTS_SOFFN_MASK;
699 dsts >>= DSTS_SOFFN_SHIFT;
700
701 return dsts;
702}
703
704/**
Googler9398cc32022-12-02 17:21:52 +0800705 * 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{
715 const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
716 int is_isoc = hs_ep->isochronous;
717 unsigned int maxsize;
718 u32 mps = hs_ep->ep.maxpacket;
719 int dir_in = hs_ep->dir_in;
720
721 if (is_isoc)
722 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;
725 else
726 maxsize = DEV_DMA_NBYTES_LIMIT * MAX_DMA_DESC_NUM_GENERIC;
727
728 /* 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
733 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.
748 * Interrupt OUT - if mps not multiple of 4 then a single packet corresponds
749 * to a single descriptor.
750 *
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{
755 const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
756 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
779 /* 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
786 return desc_size;
787}
788
789static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
790 struct dwc2_dma_desc **desc,
791 dma_addr_t dma_buff,
792 unsigned int len,
793 bool true_last)
794{
795 int dir_in = hs_ep->dir_in;
796 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) {
810 (*desc)->status = 0;
811 (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY
812 << DEV_DMA_BUFF_STS_SHIFT);
813
814 if (len > maxsize) {
815 if (!hs_ep->index && !dir_in)
816 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
817
818 (*desc)->status |=
819 maxsize << DEV_DMA_NBYTES_SHIFT & mask;
820 (*desc)->buf = dma_buff + offset;
821
822 len -= maxsize;
823 offset += maxsize;
824 } else {
825 if (true_last)
826 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
827
828 if (dir_in)
829 (*desc)->status |= (len % mps) ? DEV_DMA_SHORT :
830 ((hs_ep->send_zlp && true_last) ?
831 DEV_DMA_SHORT : 0);
832
833 (*desc)->status |=
834 len << DEV_DMA_NBYTES_SHIFT & mask;
835 (*desc)->buf = dma_buff + offset;
836 }
837
838 (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK;
839 (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY
840 << DEV_DMA_BUFF_STS_SHIFT);
841 (*desc)++;
842 }
843}
844
845/*
846 * 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/*
887 * 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++;
956 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_HS_ISOC)
957 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 */
988 for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) {
989 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) {
997 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,
1004 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
Googler9398cc32022-12-02 17:21:52 +08001021/**
Googleraf606d22022-10-26 21:40:12 -07001022 * 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,
Googler9398cc32022-12-02 17:21:52 +08001032 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -07001033 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;
Googler9398cc32022-12-02 17:21:52 +08001043 unsigned int length;
1044 unsigned int packets;
1045 unsigned int maxreq;
1046 unsigned int dma_reg;
Googleraf606d22022-10-26 21:40:12 -07001047
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
Googler9398cc32022-12-02 17:21:52 +08001061 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Googleraf606d22022-10-26 21:40:12 -07001062 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",
Googler9398cc32022-12-02 17:21:52 +08001066 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Googleraf606d22022-10-26 21:40:12 -07001067 hs_ep->dir_in ? "in" : "out");
1068
1069 /* If endpoint is stalled, we will restart request later */
Googler9398cc32022-12-02 17:21:52 +08001070 ctrl = dwc2_readl(hsotg, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07001071
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
Googler9398cc32022-12-02 17:21:52 +08001081 if (!using_desc_dma(hsotg))
1082 maxreq = get_ep_limit(hs_ep);
1083 else
1084 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1085
Googleraf606d22022-10-26 21:40:12 -07001086 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
Googleraf606d22022-10-26 21:40:12 -07001104 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) &&
Googler9398cc32022-12-02 17:21:52 +08001119 !(ureq->length % hs_ep->ep.maxpacket))
Googleraf606d22022-10-26 21:40:12 -07001120 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
Googler9398cc32022-12-02 17:21:52 +08001132 if (using_desc_dma(hsotg)) {
1133 u32 offset = 0;
1134 u32 mps = hs_ep->ep.maxpacket;
Googleraf606d22022-10-26 21:40:12 -07001135
Googler9398cc32022-12-02 17:21:52 +08001136 /* 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 }
Googleraf606d22022-10-26 21:40:12 -07001143
Googler9398cc32022-12-02 17:21:52 +08001144 if (continuing)
1145 offset = ureq->actual;
Googleraf606d22022-10-26 21:40:12 -07001146
Googler9398cc32022-12-02 17:21:52 +08001147 /* Fill DDMA chain entries */
1148 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1149 length);
Googleraf606d22022-10-26 21:40:12 -07001150
Googler9398cc32022-12-02 17:21:52 +08001151 /* 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 }
Googleraf606d22022-10-26 21:40:12 -07001171 }
1172
Googlerb48fa912023-03-17 12:40:29 +05301173 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;
Googleraf606d22022-10-26 21:40:12 -07001181 }
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);
Googler9398cc32022-12-02 17:21:52 +08001192 dwc2_writel(hsotg, ctrl, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07001193
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 */
Googler9398cc32022-12-02 17:21:52 +08001215 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Googleraf606d22022-10-26 21:40:12 -07001216 dev_dbg(hsotg->dev,
Googler9398cc32022-12-02 17:21:52 +08001217 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
1218 index, dwc2_readl(hsotg, epctrl_reg));
Googleraf606d22022-10-26 21:40:12 -07001219
1220 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08001221 __func__, dwc2_readl(hsotg, epctrl_reg));
Googleraf606d22022-10-26 21:40:12 -07001222
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,
Googler9398cc32022-12-02 17:21:52 +08001240 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -07001241 struct usb_request *req)
1242{
Googleraf606d22022-10-26 21:40:12 -07001243 int ret;
1244
Googleraf606d22022-10-26 21:40:12 -07001245 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1246 if (ret)
1247 goto dma_error;
1248
1249 return 0;
1250
1251dma_error:
1252 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1253 __func__, req->buf, req->length);
1254
1255 return -EIO;
1256}
1257
1258static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001259 struct dwc2_hsotg_ep *hs_ep,
1260 struct dwc2_hsotg_req *hs_req)
Googleraf606d22022-10-26 21:40:12 -07001261{
1262 void *req_buf = hs_req->req.buf;
1263
1264 /* If dma is not being used or buffer is aligned */
1265 if (!using_dma(hsotg) || !((long)req_buf & 3))
1266 return 0;
1267
1268 WARN_ON(hs_req->saved_req_buf);
1269
1270 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
Googler9398cc32022-12-02 17:21:52 +08001271 hs_ep->ep.name, req_buf, hs_req->req.length);
Googleraf606d22022-10-26 21:40:12 -07001272
1273 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1274 if (!hs_req->req.buf) {
1275 hs_req->req.buf = req_buf;
1276 dev_err(hsotg->dev,
1277 "%s: unable to allocate memory for bounce buffer\n",
1278 __func__);
1279 return -ENOMEM;
1280 }
1281
1282 /* Save actual buffer */
1283 hs_req->saved_req_buf = req_buf;
1284
1285 if (hs_ep->dir_in)
1286 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1287 return 0;
1288}
1289
Googler9398cc32022-12-02 17:21:52 +08001290static void
1291dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1292 struct dwc2_hsotg_ep *hs_ep,
1293 struct dwc2_hsotg_req *hs_req)
Googleraf606d22022-10-26 21:40:12 -07001294{
1295 /* If dma is not being used or buffer was aligned */
1296 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1297 return;
1298
1299 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1300 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1301
1302 /* Copy data from bounce buffer on successful out transfer */
1303 if (!hs_ep->dir_in && !hs_req->req.status)
1304 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
Googler9398cc32022-12-02 17:21:52 +08001305 hs_req->req.actual);
Googleraf606d22022-10-26 21:40:12 -07001306
1307 /* Free bounce buffer */
1308 kfree(hs_req->req.buf);
1309
1310 hs_req->req.buf = hs_req->saved_req_buf;
1311 hs_req->saved_req_buf = NULL;
1312}
1313
1314/**
1315 * dwc2_gadget_target_frame_elapsed - Checks target frame
1316 * @hs_ep: The driver endpoint to check
1317 *
1318 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1319 * corresponding transfer.
1320 */
1321static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1322{
1323 struct dwc2_hsotg *hsotg = hs_ep->parent;
1324 u32 target_frame = hs_ep->target_frame;
Googler9398cc32022-12-02 17:21:52 +08001325 u32 current_frame = hsotg->frame_number;
Googleraf606d22022-10-26 21:40:12 -07001326 bool frame_overrun = hs_ep->frame_overrun;
1327
1328 if (!frame_overrun && current_frame >= target_frame)
1329 return true;
1330
1331 if (frame_overrun && current_frame >= target_frame &&
Googlerb48fa912023-03-17 12:40:29 +05301332 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
Googleraf606d22022-10-26 21:40:12 -07001333 return true;
1334
1335 return false;
1336}
1337
Googler9398cc32022-12-02 17:21:52 +08001338/*
1339 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1340 * @hsotg: The driver state
1341 * @hs_ep: the ep descriptor chain is for
1342 *
1343 * Called to update EP0 structure's pointers depend on stage of
1344 * control transfer.
1345 */
1346static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1347 struct dwc2_hsotg_ep *hs_ep)
1348{
1349 switch (hsotg->ep0_state) {
1350 case DWC2_EP0_SETUP:
1351 case DWC2_EP0_STATUS_OUT:
1352 hs_ep->desc_list = hsotg->setup_desc[0];
1353 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1354 break;
1355 case DWC2_EP0_DATA_IN:
1356 case DWC2_EP0_STATUS_IN:
1357 hs_ep->desc_list = hsotg->ctrl_in_desc;
1358 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1359 break;
1360 case DWC2_EP0_DATA_OUT:
1361 hs_ep->desc_list = hsotg->ctrl_out_desc;
1362 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1363 break;
1364 default:
1365 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1366 hsotg->ep0_state);
1367 return -EINVAL;
1368 }
1369
1370 return 0;
1371}
1372
Googleraf606d22022-10-26 21:40:12 -07001373static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Googler9398cc32022-12-02 17:21:52 +08001374 gfp_t gfp_flags)
Googleraf606d22022-10-26 21:40:12 -07001375{
1376 struct dwc2_hsotg_req *hs_req = our_req(req);
1377 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1378 struct dwc2_hsotg *hs = hs_ep->parent;
1379 bool first;
1380 int ret;
Googler9398cc32022-12-02 17:21:52 +08001381 u32 maxsize = 0;
1382 u32 mask = 0;
1383
Googleraf606d22022-10-26 21:40:12 -07001384
1385 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1386 ep->name, req, req->length, req->buf, req->no_interrupt,
1387 req->zero, req->short_not_ok);
1388
1389 /* Prevent new request submission when controller is suspended */
Googler9398cc32022-12-02 17:21:52 +08001390 if (hs->lx_state != DWC2_L0) {
1391 dev_dbg(hs->dev, "%s: submit request only in active state\n",
1392 __func__);
Googleraf606d22022-10-26 21:40:12 -07001393 return -EAGAIN;
1394 }
1395
1396 /* initialise status of the request */
1397 INIT_LIST_HEAD(&hs_req->queue);
1398 req->actual = 0;
1399 req->status = -EINPROGRESS;
1400
Googler9398cc32022-12-02 17:21:52 +08001401 /* Don't queue ISOC request if length greater than mps*mc */
1402 if (hs_ep->isochronous &&
1403 req->length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1404 dev_err(hs->dev, "req length > maxpacket*mc\n");
1405 return -EINVAL;
1406 }
1407
1408 /* In DDMA mode for ISOC's don't queue request if length greater
1409 * than descriptor limits.
1410 */
1411 if (using_desc_dma(hs) && hs_ep->isochronous) {
1412 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1413 if (hs_ep->dir_in && req->length > maxsize) {
1414 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1415 req->length, maxsize);
1416 return -EINVAL;
1417 }
1418
1419 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1420 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1421 req->length, hs_ep->ep.maxpacket);
1422 return -EINVAL;
1423 }
1424 }
1425
Googleraf606d22022-10-26 21:40:12 -07001426 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
1427 if (ret)
1428 return ret;
1429
1430 /* if we're using DMA, sync the buffers as necessary */
1431 if (using_dma(hs)) {
1432 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
1433 if (ret)
1434 return ret;
1435 }
Googler9398cc32022-12-02 17:21:52 +08001436 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1437 if (using_desc_dma(hs) && !hs_ep->index) {
1438 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1439 if (ret)
1440 return ret;
1441 }
Googleraf606d22022-10-26 21:40:12 -07001442
1443 first = list_empty(&hs_ep->queue);
1444 list_add_tail(&hs_req->queue, &hs_ep->queue);
1445
Googler9398cc32022-12-02 17:21:52 +08001446 /*
1447 * Handle DDMA isochronous transfers separately - just add new entry
1448 * to the descriptor chain.
1449 * Transfer will be started once SW gets either one of NAK or
1450 * OutTknEpDis interrupts.
1451 */
1452 if (using_desc_dma(hs) && hs_ep->isochronous) {
1453 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1454 dma_addr_t dma_addr = hs_req->req.dma;
1455
1456 if (hs_req->req.num_sgs) {
1457 WARN_ON(hs_req->req.num_sgs > 1);
1458 dma_addr = sg_dma_address(hs_req->req.sg);
1459 }
1460 dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
1461 hs_req->req.length);
1462 }
1463 return 0;
1464 }
1465
1466 /* Change EP direction if status phase request is after data out */
1467 if (!hs_ep->index && !req->length && !hs_ep->dir_in &&
1468 hs->ep0_state == DWC2_EP0_DATA_OUT)
1469 hs_ep->dir_in = 1;
1470
Googleraf606d22022-10-26 21:40:12 -07001471 if (first) {
1472 if (!hs_ep->isochronous) {
1473 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1474 return 0;
1475 }
1476
Googler9398cc32022-12-02 17:21:52 +08001477 /* Update current frame number value. */
1478 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1479 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Googleraf606d22022-10-26 21:40:12 -07001480 dwc2_gadget_incr_frame_num(hs_ep);
Googler9398cc32022-12-02 17:21:52 +08001481 /* Update current frame number value once more as it
1482 * changes here.
1483 */
1484 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1485 }
Googleraf606d22022-10-26 21:40:12 -07001486
1487 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1488 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1489 }
1490 return 0;
1491}
1492
1493static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Googler9398cc32022-12-02 17:21:52 +08001494 gfp_t gfp_flags)
Googleraf606d22022-10-26 21:40:12 -07001495{
1496 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1497 struct dwc2_hsotg *hs = hs_ep->parent;
1498 unsigned long flags = 0;
1499 int ret = 0;
1500
1501 spin_lock_irqsave(&hs->lock, flags);
1502 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
1503 spin_unlock_irqrestore(&hs->lock, flags);
1504
1505 return ret;
1506}
1507
1508static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Googler9398cc32022-12-02 17:21:52 +08001509 struct usb_request *req)
Googleraf606d22022-10-26 21:40:12 -07001510{
1511 struct dwc2_hsotg_req *hs_req = our_req(req);
1512
1513 kfree(hs_req);
1514}
1515
1516/**
1517 * dwc2_hsotg_complete_oursetup - setup completion callback
1518 * @ep: The endpoint the request was on.
1519 * @req: The request completed.
1520 *
1521 * Called on completion of any requests the driver itself
1522 * submitted that need cleaning up.
1523 */
1524static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Googler9398cc32022-12-02 17:21:52 +08001525 struct usb_request *req)
Googleraf606d22022-10-26 21:40:12 -07001526{
1527 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1528 struct dwc2_hsotg *hsotg = hs_ep->parent;
1529
1530 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1531
1532 dwc2_hsotg_ep_free_request(ep, req);
1533}
1534
1535/**
1536 * ep_from_windex - convert control wIndex value to endpoint
1537 * @hsotg: The driver state.
1538 * @windex: The control request wIndex field (in host order).
1539 *
1540 * Convert the given wIndex into a pointer to an driver endpoint
1541 * structure, or return NULL if it is not a valid endpoint.
1542 */
1543static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001544 u32 windex)
Googleraf606d22022-10-26 21:40:12 -07001545{
Googlerb48fa912023-03-17 12:40:29 +05301546 struct dwc2_hsotg_ep *ep;
Googleraf606d22022-10-26 21:40:12 -07001547 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
Googlerb48fa912023-03-17 12:40:29 +05301556 ep = index_to_ep(hsotg, idx, dir);
1557
1558 if (idx && ep->dir_in != dir)
1559 return NULL;
1560
1561 return ep;
Googleraf606d22022-10-26 21:40:12 -07001562}
1563
1564/**
1565 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
1566 * @hsotg: The driver state.
1567 * @testmode: requested usb test mode
1568 * Enable usb Test Mode requested by the Host.
1569 */
1570int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
1571{
Googler9398cc32022-12-02 17:21:52 +08001572 int dctl = dwc2_readl(hsotg, DCTL);
Googleraf606d22022-10-26 21:40:12 -07001573
1574 dctl &= ~DCTL_TSTCTL_MASK;
1575 switch (testmode) {
1576 case TEST_J:
1577 case TEST_K:
1578 case TEST_SE0_NAK:
1579 case TEST_PACKET:
1580 case TEST_FORCE_EN:
1581 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1582 break;
1583 default:
1584 return -EINVAL;
1585 }
Googler9398cc32022-12-02 17:21:52 +08001586 dwc2_writel(hsotg, dctl, DCTL);
Googleraf606d22022-10-26 21:40:12 -07001587 return 0;
1588}
1589
1590/**
1591 * dwc2_hsotg_send_reply - send reply to control request
1592 * @hsotg: The device state
1593 * @ep: Endpoint 0
1594 * @buff: Buffer for request
1595 * @length: Length of reply.
1596 *
1597 * Create a request and queue it on the given endpoint. This is useful as
1598 * an internal method of sending replies to certain control requests, etc.
1599 */
1600static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001601 struct dwc2_hsotg_ep *ep,
Googleraf606d22022-10-26 21:40:12 -07001602 void *buff,
1603 int length)
1604{
1605 struct usb_request *req;
1606 int ret;
1607
1608 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1609
1610 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1611 hsotg->ep0_reply = req;
1612 if (!req) {
1613 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1614 return -ENOMEM;
1615 }
1616
1617 req->buf = hsotg->ep0_buff;
1618 req->length = length;
1619 /*
1620 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1621 * STATUS stage.
1622 */
1623 req->zero = 0;
1624 req->complete = dwc2_hsotg_complete_oursetup;
1625
1626 if (length)
1627 memcpy(req->buf, buff, length);
1628
1629 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1630 if (ret) {
1631 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1632 return ret;
1633 }
1634
1635 return 0;
1636}
1637
1638/**
1639 * dwc2_hsotg_process_req_status - process request GET_STATUS
1640 * @hsotg: The device state
1641 * @ctrl: USB control request
1642 */
1643static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001644 struct usb_ctrlrequest *ctrl)
Googleraf606d22022-10-26 21:40:12 -07001645{
1646 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1647 struct dwc2_hsotg_ep *ep;
1648 __le16 reply;
Googler9398cc32022-12-02 17:21:52 +08001649 u16 status;
Googleraf606d22022-10-26 21:40:12 -07001650 int ret;
1651
1652 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1653
1654 if (!ep0->dir_in) {
1655 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1656 return -EINVAL;
1657 }
1658
1659 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1660 case USB_RECIP_DEVICE:
Googler9398cc32022-12-02 17:21:52 +08001661 status = 1 << USB_DEVICE_SELF_POWERED;
1662 status |= hsotg->remote_wakeup_allowed <<
1663 USB_DEVICE_REMOTE_WAKEUP;
1664 reply = cpu_to_le16(status);
Googleraf606d22022-10-26 21:40:12 -07001665 break;
1666
1667 case USB_RECIP_INTERFACE:
1668 /* currently, the data result should be zero */
1669 reply = cpu_to_le16(0);
1670 break;
1671
1672 case USB_RECIP_ENDPOINT:
1673 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1674 if (!ep)
1675 return -ENOENT;
1676
1677 reply = cpu_to_le16(ep->halted ? 1 : 0);
1678 break;
1679
1680 default:
1681 return 0;
1682 }
1683
1684 if (le16_to_cpu(ctrl->wLength) != 2)
1685 return -EINVAL;
1686
1687 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
1688 if (ret) {
1689 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1690 return ret;
1691 }
1692
1693 return 1;
1694}
1695
1696static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
1697
1698/**
1699 * get_ep_head - return the first request on the endpoint
1700 * @hs_ep: The controller endpoint to get
1701 *
1702 * Get the first request on the endpoint.
1703 */
1704static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
1705{
Googler9398cc32022-12-02 17:21:52 +08001706 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1707 queue);
Googleraf606d22022-10-26 21:40:12 -07001708}
1709
1710/**
1711 * dwc2_gadget_start_next_request - Starts next request from ep queue
1712 * @hs_ep: Endpoint structure
1713 *
1714 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1715 * in its handler. Hence we need to unmask it here to be able to do
1716 * resynchronization.
1717 */
1718static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1719{
Googlerb48fa912023-03-17 12:40:29 +05301720 u32 mask;
Googleraf606d22022-10-26 21:40:12 -07001721 struct dwc2_hsotg *hsotg = hs_ep->parent;
1722 int dir_in = hs_ep->dir_in;
1723 struct dwc2_hsotg_req *hs_req;
Googlerb48fa912023-03-17 12:40:29 +05301724 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
Googleraf606d22022-10-26 21:40:12 -07001725
1726 if (!list_empty(&hs_ep->queue)) {
1727 hs_req = get_ep_head(hs_ep);
1728 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1729 return;
1730 }
1731 if (!hs_ep->isochronous)
1732 return;
1733
1734 if (dir_in) {
1735 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1736 __func__);
1737 } else {
1738 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1739 __func__);
Googlerb48fa912023-03-17 12:40:29 +05301740 mask = dwc2_readl(hsotg, epmsk_reg);
1741 mask |= DOEPMSK_OUTTKNEPDISMSK;
1742 dwc2_writel(hsotg, mask, epmsk_reg);
Googleraf606d22022-10-26 21:40:12 -07001743 }
1744}
1745
1746/**
1747 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
1748 * @hsotg: The device state
1749 * @ctrl: USB control request
1750 */
1751static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001752 struct usb_ctrlrequest *ctrl)
Googleraf606d22022-10-26 21:40:12 -07001753{
1754 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1755 struct dwc2_hsotg_req *hs_req;
1756 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1757 struct dwc2_hsotg_ep *ep;
1758 int ret;
1759 bool halted;
1760 u32 recip;
1761 u32 wValue;
1762 u32 wIndex;
1763
1764 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1765 __func__, set ? "SET" : "CLEAR");
1766
1767 wValue = le16_to_cpu(ctrl->wValue);
1768 wIndex = le16_to_cpu(ctrl->wIndex);
1769 recip = ctrl->bRequestType & USB_RECIP_MASK;
1770
1771 switch (recip) {
1772 case USB_RECIP_DEVICE:
1773 switch (wValue) {
Googler9398cc32022-12-02 17:21:52 +08001774 case USB_DEVICE_REMOTE_WAKEUP:
1775 if (set)
1776 hsotg->remote_wakeup_allowed = 1;
1777 else
1778 hsotg->remote_wakeup_allowed = 0;
1779 break;
1780
Googleraf606d22022-10-26 21:40:12 -07001781 case USB_DEVICE_TEST_MODE:
1782 if ((wIndex & 0xff) != 0)
1783 return -EINVAL;
1784 if (!set)
1785 return -EINVAL;
1786
1787 hsotg->test_mode = wIndex >> 8;
Googleraf606d22022-10-26 21:40:12 -07001788 break;
1789 default:
1790 return -ENOENT;
1791 }
Googler9398cc32022-12-02 17:21:52 +08001792
1793 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1794 if (ret) {
1795 dev_err(hsotg->dev,
1796 "%s: failed to send reply\n", __func__);
1797 return ret;
1798 }
Googleraf606d22022-10-26 21:40:12 -07001799 break;
1800
1801 case USB_RECIP_ENDPOINT:
1802 ep = ep_from_windex(hsotg, wIndex);
1803 if (!ep) {
1804 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1805 __func__, wIndex);
1806 return -ENOENT;
1807 }
1808
1809 switch (wValue) {
1810 case USB_ENDPOINT_HALT:
1811 halted = ep->halted;
1812
1813 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
1814
1815 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1816 if (ret) {
1817 dev_err(hsotg->dev,
1818 "%s: failed to send reply\n", __func__);
1819 return ret;
1820 }
1821
1822 /*
1823 * we have to complete all requests for ep if it was
1824 * halted, and the halt was cleared by CLEAR_FEATURE
1825 */
1826
1827 if (!set && halted) {
1828 /*
1829 * If we have request in progress,
1830 * then complete it
1831 */
1832 if (ep->req) {
1833 hs_req = ep->req;
1834 ep->req = NULL;
1835 list_del_init(&hs_req->queue);
1836 if (hs_req->req.complete) {
1837 spin_unlock(&hsotg->lock);
1838 usb_gadget_giveback_request(
1839 &ep->ep, &hs_req->req);
1840 spin_lock(&hsotg->lock);
1841 }
1842 }
1843
1844 /* If we have pending request, then start it */
Googler9398cc32022-12-02 17:21:52 +08001845 if (!ep->req)
Googleraf606d22022-10-26 21:40:12 -07001846 dwc2_gadget_start_next_request(ep);
Googleraf606d22022-10-26 21:40:12 -07001847 }
1848
1849 break;
1850
1851 default:
1852 return -ENOENT;
1853 }
1854 break;
1855 default:
1856 return -ENOENT;
1857 }
1858 return 1;
1859}
1860
1861static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
1862
1863/**
1864 * dwc2_hsotg_stall_ep0 - stall ep0
1865 * @hsotg: The device state
1866 *
1867 * Set stall for ep0 as response for setup request.
1868 */
1869static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
1870{
1871 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1872 u32 reg;
1873 u32 ctrl;
1874
1875 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1876 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1877
1878 /*
1879 * DxEPCTL_Stall will be cleared by EP once it has
1880 * taken effect, so no need to clear later.
1881 */
1882
Googler9398cc32022-12-02 17:21:52 +08001883 ctrl = dwc2_readl(hsotg, reg);
Googleraf606d22022-10-26 21:40:12 -07001884 ctrl |= DXEPCTL_STALL;
1885 ctrl |= DXEPCTL_CNAK;
Googler9398cc32022-12-02 17:21:52 +08001886 dwc2_writel(hsotg, ctrl, reg);
Googleraf606d22022-10-26 21:40:12 -07001887
1888 dev_dbg(hsotg->dev,
1889 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Googler9398cc32022-12-02 17:21:52 +08001890 ctrl, reg, dwc2_readl(hsotg, reg));
Googleraf606d22022-10-26 21:40:12 -07001891
1892 /*
1893 * complete won't be called, so we enqueue
1894 * setup request here
1895 */
1896 dwc2_hsotg_enqueue_setup(hsotg);
1897}
1898
1899/**
1900 * dwc2_hsotg_process_control - process a control request
1901 * @hsotg: The device state
1902 * @ctrl: The control request received
1903 *
1904 * The controller has received the SETUP phase of a control request, and
1905 * needs to work out what to do next (and whether to pass it on to the
1906 * gadget driver).
1907 */
1908static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08001909 struct usb_ctrlrequest *ctrl)
Googleraf606d22022-10-26 21:40:12 -07001910{
1911 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1912 int ret = 0;
1913 u32 dcfg;
1914
1915 dev_dbg(hsotg->dev,
1916 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1917 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1918 ctrl->wIndex, ctrl->wLength);
1919
1920 if (ctrl->wLength == 0) {
1921 ep0->dir_in = 1;
1922 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1923 } else if (ctrl->bRequestType & USB_DIR_IN) {
1924 ep0->dir_in = 1;
1925 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1926 } else {
1927 ep0->dir_in = 0;
1928 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1929 }
1930
1931 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1932 switch (ctrl->bRequest) {
1933 case USB_REQ_SET_ADDRESS:
1934 hsotg->connected = 1;
Googler9398cc32022-12-02 17:21:52 +08001935 dcfg = dwc2_readl(hsotg, DCFG);
Googleraf606d22022-10-26 21:40:12 -07001936 dcfg &= ~DCFG_DEVADDR_MASK;
1937 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1938 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Googler9398cc32022-12-02 17:21:52 +08001939 dwc2_writel(hsotg, dcfg, DCFG);
Googleraf606d22022-10-26 21:40:12 -07001940
1941 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1942
1943 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1944 return;
1945
1946 case USB_REQ_GET_STATUS:
1947 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
1948 break;
1949
1950 case USB_REQ_CLEAR_FEATURE:
1951 case USB_REQ_SET_FEATURE:
1952 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
1953 break;
1954 }
1955 }
1956
1957 /* as a fallback, try delivering it to the driver to deal with */
1958
1959 if (ret == 0 && hsotg->driver) {
1960 spin_unlock(&hsotg->lock);
1961 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1962 spin_lock(&hsotg->lock);
1963 if (ret < 0)
1964 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1965 }
1966
Googler9398cc32022-12-02 17:21:52 +08001967 hsotg->delayed_status = false;
1968 if (ret == USB_GADGET_DELAYED_STATUS)
1969 hsotg->delayed_status = true;
1970
Googleraf606d22022-10-26 21:40:12 -07001971 /*
1972 * the request is either unhandlable, or is not formatted correctly
1973 * so respond with a STALL for the status stage to indicate failure.
1974 */
1975
1976 if (ret < 0)
1977 dwc2_hsotg_stall_ep0(hsotg);
1978}
1979
1980/**
1981 * dwc2_hsotg_complete_setup - completion of a setup transfer
1982 * @ep: The endpoint the request was on.
1983 * @req: The request completed.
1984 *
1985 * Called on completion of any requests the driver itself submitted for
1986 * EP0 setup packets
1987 */
1988static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Googler9398cc32022-12-02 17:21:52 +08001989 struct usb_request *req)
Googleraf606d22022-10-26 21:40:12 -07001990{
1991 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1992 struct dwc2_hsotg *hsotg = hs_ep->parent;
1993
1994 if (req->status < 0) {
1995 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1996 return;
1997 }
1998
1999 spin_lock(&hsotg->lock);
2000 if (req->actual == 0)
2001 dwc2_hsotg_enqueue_setup(hsotg);
2002 else
2003 dwc2_hsotg_process_control(hsotg, req->buf);
2004 spin_unlock(&hsotg->lock);
2005}
2006
2007/**
2008 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
2009 * @hsotg: The device state.
2010 *
2011 * Enqueue a request on EP0 if necessary to received any SETUP packets
2012 * received from the host.
2013 */
2014static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
2015{
2016 struct usb_request *req = hsotg->ctrl_req;
2017 struct dwc2_hsotg_req *hs_req = our_req(req);
2018 int ret;
2019
2020 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
2021
2022 req->zero = 0;
2023 req->length = 8;
2024 req->buf = hsotg->ctrl_buff;
2025 req->complete = dwc2_hsotg_complete_setup;
2026
2027 if (!list_empty(&hs_req->queue)) {
2028 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
2029 return;
2030 }
2031
2032 hsotg->eps_out[0]->dir_in = 0;
2033 hsotg->eps_out[0]->send_zlp = 0;
2034 hsotg->ep0_state = DWC2_EP0_SETUP;
2035
2036 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
2037 if (ret < 0) {
2038 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
2039 /*
2040 * Don't think there's much we can do other than watch the
2041 * driver fail.
2042 */
2043 }
2044}
2045
2046static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08002047 struct dwc2_hsotg_ep *hs_ep)
Googleraf606d22022-10-26 21:40:12 -07002048{
2049 u32 ctrl;
2050 u8 index = hs_ep->index;
2051 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2052 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
2053
2054 if (hs_ep->dir_in)
2055 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Googler9398cc32022-12-02 17:21:52 +08002056 index);
Googleraf606d22022-10-26 21:40:12 -07002057 else
2058 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Googler9398cc32022-12-02 17:21:52 +08002059 index);
2060 if (using_desc_dma(hsotg)) {
2061 /* Not specific buffer needed for ep0 ZLP */
2062 dma_addr_t dma = hs_ep->desc_list_dma;
Googleraf606d22022-10-26 21:40:12 -07002063
Googler9398cc32022-12-02 17:21:52 +08002064 if (!index)
2065 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
Googleraf606d22022-10-26 21:40:12 -07002066
Googler9398cc32022-12-02 17:21:52 +08002067 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
2068 } else {
2069 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2070 DXEPTSIZ_XFERSIZE(0),
2071 epsiz_reg);
2072 }
2073
2074 ctrl = dwc2_readl(hsotg, epctl_reg);
Googleraf606d22022-10-26 21:40:12 -07002075 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
2076 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
2077 ctrl |= DXEPCTL_USBACTEP;
Googler9398cc32022-12-02 17:21:52 +08002078 dwc2_writel(hsotg, ctrl, epctl_reg);
Googleraf606d22022-10-26 21:40:12 -07002079}
2080
2081/**
2082 * dwc2_hsotg_complete_request - complete a request given to us
2083 * @hsotg: The device state.
2084 * @hs_ep: The endpoint the request was on.
2085 * @hs_req: The request to complete.
2086 * @result: The result code (0 => Ok, otherwise errno)
2087 *
2088 * The given request has finished, so call the necessary completion
2089 * if it has one and then look to see if we can start a new request
2090 * on the endpoint.
2091 *
2092 * Note, expects the ep to already be locked as appropriate.
2093 */
2094static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08002095 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -07002096 struct dwc2_hsotg_req *hs_req,
2097 int result)
2098{
Googleraf606d22022-10-26 21:40:12 -07002099 if (!hs_req) {
2100 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2101 return;
2102 }
2103
2104 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2105 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2106
2107 /*
2108 * only replace the status if we've not already set an error
2109 * from a previous transaction
2110 */
2111
2112 if (hs_req->req.status == -EINPROGRESS)
2113 hs_req->req.status = result;
2114
2115 if (using_dma(hsotg))
2116 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2117
2118 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
2119
2120 hs_ep->req = NULL;
2121 list_del_init(&hs_req->queue);
2122
2123 /*
2124 * call the complete request with the locks off, just in case the
2125 * request tries to queue more work for this endpoint.
2126 */
2127
2128 if (hs_req->req.complete) {
2129 spin_unlock(&hsotg->lock);
2130 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
2131 spin_lock(&hsotg->lock);
2132 }
2133
Googler9398cc32022-12-02 17:21:52 +08002134 /* In DDMA don't need to proceed to starting of next ISOC request */
2135 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2136 return;
2137
Googleraf606d22022-10-26 21:40:12 -07002138 /*
2139 * Look to see if there is anything else to do. Note, the completion
2140 * of the previous request may have caused a new request to be started
2141 * so be careful when doing this.
2142 */
2143
Googler9398cc32022-12-02 17:21:52 +08002144 if (!hs_ep->req && result >= 0)
Googleraf606d22022-10-26 21:40:12 -07002145 dwc2_gadget_start_next_request(hs_ep);
Googler9398cc32022-12-02 17:21:52 +08002146}
2147
2148/*
2149 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2150 * @hs_ep: The endpoint the request was on.
2151 *
2152 * Get first request from the ep queue, determine descriptor on which complete
2153 * happened. SW discovers which descriptor currently in use by HW, adjusts
2154 * dma_address and calculates index of completed descriptor based on the value
2155 * of DEPDMA register. Update actual length of request, giveback to gadget.
2156 */
2157static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2158{
2159 struct dwc2_hsotg *hsotg = hs_ep->parent;
2160 struct dwc2_hsotg_req *hs_req;
2161 struct usb_request *ureq;
2162 u32 desc_sts;
2163 u32 mask;
2164
2165 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2166
2167 /* Process only descriptors with buffer status set to DMA done */
2168 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2169 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2170
2171 hs_req = get_ep_head(hs_ep);
2172 if (!hs_req) {
2173 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2174 return;
2175 }
2176 ureq = &hs_req->req;
2177
2178 /* Check completion status */
2179 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2180 DEV_DMA_STS_SUCC) {
2181 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2182 DEV_DMA_ISOC_RX_NBYTES_MASK;
2183 ureq->actual = ureq->length - ((desc_sts & mask) >>
2184 DEV_DMA_ISOC_NBYTES_SHIFT);
2185
2186 /* Adjust actual len for ISOC Out if len is
2187 * not align of 4
2188 */
2189 if (!hs_ep->dir_in && ureq->length & 0x3)
2190 ureq->actual += 4 - (ureq->length & 0x3);
2191
2192 /* Set actual frame number for completed transfers */
2193 ureq->frame_number =
2194 (desc_sts & DEV_DMA_ISOC_FRNUM_MASK) >>
2195 DEV_DMA_ISOC_FRNUM_SHIFT;
2196 }
2197
2198 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2199
2200 hs_ep->compl_desc++;
2201 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
2202 hs_ep->compl_desc = 0;
2203 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Googleraf606d22022-10-26 21:40:12 -07002204 }
2205}
2206
Googler9398cc32022-12-02 17:21:52 +08002207/*
2208 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2209 * @hs_ep: The isochronous endpoint.
2210 *
2211 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2212 * interrupt. Reset target frame and next_desc to allow to start
2213 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2214 * interrupt for OUT direction.
2215 */
2216static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
2217{
2218 struct dwc2_hsotg *hsotg = hs_ep->parent;
2219
2220 if (!hs_ep->dir_in)
2221 dwc2_flush_rx_fifo(hsotg);
2222 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
2223
2224 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2225 hs_ep->next_desc = 0;
2226 hs_ep->compl_desc = 0;
2227}
2228
Googleraf606d22022-10-26 21:40:12 -07002229/**
2230 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
2231 * @hsotg: The device state.
2232 * @ep_idx: The endpoint index for the data
2233 * @size: The size of data in the fifo, in bytes
2234 *
2235 * The FIFO status shows there is data to read from the FIFO for a given
2236 * endpoint, so sort out whether we need to read the data into a request
2237 * that has been made for that endpoint.
2238 */
2239static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
2240{
2241 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2242 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Googleraf606d22022-10-26 21:40:12 -07002243 int to_read;
2244 int max_req;
2245 int read_ptr;
2246
Googleraf606d22022-10-26 21:40:12 -07002247 if (!hs_req) {
Googler9398cc32022-12-02 17:21:52 +08002248 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Googleraf606d22022-10-26 21:40:12 -07002249 int ptr;
2250
2251 dev_dbg(hsotg->dev,
Googler9398cc32022-12-02 17:21:52 +08002252 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Googleraf606d22022-10-26 21:40:12 -07002253 __func__, size, ep_idx, epctl);
2254
2255 /* dump the data from the FIFO, we've nothing we can do */
2256 for (ptr = 0; ptr < size; ptr += 4)
Googler9398cc32022-12-02 17:21:52 +08002257 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Googleraf606d22022-10-26 21:40:12 -07002258
2259 return;
2260 }
2261
2262 to_read = size;
2263 read_ptr = hs_req->req.actual;
2264 max_req = hs_req->req.length - read_ptr;
2265
2266 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2267 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2268
2269 if (to_read > max_req) {
2270 /*
2271 * more data appeared than we where willing
2272 * to deal with in this request.
2273 */
2274
2275 /* currently we don't deal this */
2276 WARN_ON_ONCE(1);
2277 }
2278
2279 hs_ep->total_data += to_read;
2280 hs_req->req.actual += to_read;
2281 to_read = DIV_ROUND_UP(to_read, 4);
2282
2283 /*
2284 * note, we might over-write the buffer end by 3 bytes depending on
2285 * alignment of the data.
2286 */
Googler9398cc32022-12-02 17:21:52 +08002287 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2288 hs_req->req.buf + read_ptr, to_read);
Googleraf606d22022-10-26 21:40:12 -07002289}
2290
2291/**
2292 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
2293 * @hsotg: The device instance
2294 * @dir_in: If IN zlp
2295 *
2296 * Generate a zero-length IN packet request for terminating a SETUP
2297 * transaction.
2298 *
2299 * Note, since we don't write any data to the TxFIFO, then it is
2300 * currently believed that we do not need to wait for any space in
2301 * the TxFIFO.
2302 */
2303static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
2304{
2305 /* eps_out[0] is used in both directions */
2306 hsotg->eps_out[0]->dir_in = dir_in;
2307 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
2308
2309 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
2310}
2311
Googlerb48fa912023-03-17 12:40:29 +05302312static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
2313 u32 epctl_reg)
2314{
2315 u32 ctrl;
2316
2317 ctrl = dwc2_readl(hsotg, epctl_reg);
2318 if (ctrl & DXEPCTL_EOFRNUM)
2319 ctrl |= DXEPCTL_SETEVENFR;
2320 else
2321 ctrl |= DXEPCTL_SETODDFR;
2322 dwc2_writel(hsotg, ctrl, epctl_reg);
2323}
2324
Googler9398cc32022-12-02 17:21:52 +08002325/*
2326 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2327 * @hs_ep - The endpoint on which transfer went
2328 *
2329 * Iterate over endpoints descriptor chain and get info on bytes remained
2330 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2331 */
2332static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
Googleraf606d22022-10-26 21:40:12 -07002333{
Googler9398cc32022-12-02 17:21:52 +08002334 const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
2335 struct dwc2_hsotg *hsotg = hs_ep->parent;
2336 unsigned int bytes_rem = 0;
2337 unsigned int bytes_rem_correction = 0;
2338 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2339 int i;
2340 u32 status;
2341 u32 mps = hs_ep->ep.maxpacket;
2342 int dir_in = hs_ep->dir_in;
Googleraf606d22022-10-26 21:40:12 -07002343
Googler9398cc32022-12-02 17:21:52 +08002344 if (!desc)
2345 return -EINVAL;
2346
2347 /* Interrupt OUT EP with mps not multiple of 4 */
2348 if (hs_ep->index)
2349 if (usb_endpoint_xfer_int(ep_desc) && !dir_in && (mps % 4))
2350 bytes_rem_correction = 4 - (mps % 4);
2351
2352 for (i = 0; i < hs_ep->desc_count; ++i) {
2353 status = desc->status;
2354 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2355 bytes_rem -= bytes_rem_correction;
2356
2357 if (status & DEV_DMA_STS_MASK)
2358 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2359 i, status & DEV_DMA_STS_MASK);
2360
2361 if (status & DEV_DMA_L)
2362 break;
2363
2364 desc++;
2365 }
2366
2367 return bytes_rem;
Googleraf606d22022-10-26 21:40:12 -07002368}
2369
2370/**
2371 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
2372 * @hsotg: The device instance
2373 * @epnum: The endpoint received from
2374 *
2375 * The RXFIFO has delivered an OutDone event, which means that the data
2376 * transfer for an OUT endpoint has been completed, either by a short
2377 * packet or by the finish of a transfer.
2378 */
2379static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
2380{
Googler9398cc32022-12-02 17:21:52 +08002381 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Googleraf606d22022-10-26 21:40:12 -07002382 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2383 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2384 struct usb_request *req = &hs_req->req;
Googler9398cc32022-12-02 17:21:52 +08002385 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Googleraf606d22022-10-26 21:40:12 -07002386 int result = 0;
2387
2388 if (!hs_req) {
2389 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2390 return;
2391 }
2392
2393 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2394 dev_dbg(hsotg->dev, "zlp packet received\n");
2395 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2396 dwc2_hsotg_enqueue_setup(hsotg);
2397 return;
2398 }
2399
Googler9398cc32022-12-02 17:21:52 +08002400 if (using_desc_dma(hsotg))
2401 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2402
Googleraf606d22022-10-26 21:40:12 -07002403 if (using_dma(hsotg)) {
Googler9398cc32022-12-02 17:21:52 +08002404 unsigned int size_done;
Googleraf606d22022-10-26 21:40:12 -07002405
2406 /*
2407 * Calculate the size of the transfer by checking how much
2408 * is left in the endpoint size register and then working it
2409 * out from the amount we loaded for the transfer.
2410 *
2411 * We need to do this as DMA pointers are always 32bit aligned
2412 * so may overshoot/undershoot the transfer.
2413 */
2414
2415 size_done = hs_ep->size_loaded - size_left;
2416 size_done += hs_ep->last_load;
2417
2418 req->actual = size_done;
2419 }
2420
2421 /* if there is more request to do, schedule new transfer */
2422 if (req->actual < req->length && size_left == 0) {
2423 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2424 return;
2425 }
2426
2427 if (req->actual < req->length && req->short_not_ok) {
2428 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2429 __func__, req->actual, req->length);
2430
2431 /*
2432 * todo - what should we return here? there's no one else
2433 * even bothering to check the status.
2434 */
2435 }
2436
Googler9398cc32022-12-02 17:21:52 +08002437 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2438 if (!using_desc_dma(hsotg) && epnum == 0 &&
2439 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Googleraf606d22022-10-26 21:40:12 -07002440 /* Move to STATUS IN */
Googler9398cc32022-12-02 17:21:52 +08002441 if (!hsotg->delayed_status)
2442 dwc2_hsotg_ep0_zlp(hsotg, true);
Googleraf606d22022-10-26 21:40:12 -07002443 }
2444
Googlerb48fa912023-03-17 12:40:29 +05302445 /*
2446 * Slave mode OUT transfers do not go through XferComplete so
2447 * adjust the ISOC parity here.
2448 */
2449 if (!using_dma(hsotg)) {
2450 if (hs_ep->isochronous && hs_ep->interval == 1)
2451 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
2452 else if (hs_ep->isochronous && hs_ep->interval > 1)
2453 dwc2_gadget_incr_frame_num(hs_ep);
Googleraf606d22022-10-26 21:40:12 -07002454 }
2455
Googlerb48fa912023-03-17 12:40:29 +05302456 /* Set actual frame number for completed transfers */
2457 if (!using_desc_dma(hsotg) && hs_ep->isochronous)
2458 req->frame_number = hsotg->frame_number;
2459
Googleraf606d22022-10-26 21:40:12 -07002460 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
2461}
2462
2463/**
2464 * dwc2_hsotg_handle_rx - RX FIFO has data
2465 * @hsotg: The device instance
2466 *
2467 * The IRQ handler has detected that the RX FIFO has some data in it
2468 * that requires processing, so find out what is in there and do the
2469 * appropriate read.
2470 *
2471 * The RXFIFO is a true FIFO, the packets coming out are still in packet
2472 * chunks, so if you have x packets received on an endpoint you'll get x
2473 * FIFO events delivered, each with a packet's worth of data in it.
2474 *
2475 * When using DMA, we should not be processing events from the RXFIFO
2476 * as the actual data should be sent to the memory directly and we turn
2477 * on the completion interrupts to get notifications of transfer completion.
2478 */
2479static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
2480{
Googler9398cc32022-12-02 17:21:52 +08002481 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Googleraf606d22022-10-26 21:40:12 -07002482 u32 epnum, status, size;
2483
2484 WARN_ON(using_dma(hsotg));
2485
2486 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2487 status = grxstsr & GRXSTS_PKTSTS_MASK;
2488
2489 size = grxstsr & GRXSTS_BYTECNT_MASK;
2490 size >>= GRXSTS_BYTECNT_SHIFT;
2491
2492 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Googler9398cc32022-12-02 17:21:52 +08002493 __func__, grxstsr, size, epnum);
Googleraf606d22022-10-26 21:40:12 -07002494
2495 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2496 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2497 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
2498 break;
2499
2500 case GRXSTS_PKTSTS_OUTDONE:
2501 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
2502 dwc2_hsotg_read_frameno(hsotg));
2503
2504 if (!using_dma(hsotg))
2505 dwc2_hsotg_handle_outdone(hsotg, epnum);
2506 break;
2507
2508 case GRXSTS_PKTSTS_SETUPDONE:
2509 dev_dbg(hsotg->dev,
2510 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
2511 dwc2_hsotg_read_frameno(hsotg),
Googler9398cc32022-12-02 17:21:52 +08002512 dwc2_readl(hsotg, DOEPCTL(0)));
Googleraf606d22022-10-26 21:40:12 -07002513 /*
2514 * Call dwc2_hsotg_handle_outdone here if it was not called from
2515 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2516 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2517 */
2518 if (hsotg->ep0_state == DWC2_EP0_SETUP)
2519 dwc2_hsotg_handle_outdone(hsotg, epnum);
2520 break;
2521
2522 case GRXSTS_PKTSTS_OUTRX:
2523 dwc2_hsotg_rx_data(hsotg, epnum, size);
2524 break;
2525
2526 case GRXSTS_PKTSTS_SETUPRX:
2527 dev_dbg(hsotg->dev,
2528 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
2529 dwc2_hsotg_read_frameno(hsotg),
Googler9398cc32022-12-02 17:21:52 +08002530 dwc2_readl(hsotg, DOEPCTL(0)));
Googleraf606d22022-10-26 21:40:12 -07002531
2532 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2533
2534 dwc2_hsotg_rx_data(hsotg, epnum, size);
2535 break;
2536
2537 default:
2538 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2539 __func__, grxstsr);
2540
2541 dwc2_hsotg_dump(hsotg);
2542 break;
2543 }
2544}
2545
2546/**
2547 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
2548 * @mps: The maximum packet size in bytes.
2549 */
2550static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
2551{
2552 switch (mps) {
2553 case 64:
2554 return D0EPCTL_MPS_64;
2555 case 32:
2556 return D0EPCTL_MPS_32;
2557 case 16:
2558 return D0EPCTL_MPS_16;
2559 case 8:
2560 return D0EPCTL_MPS_8;
2561 }
2562
2563 /* bad max packet size, warn and return invalid result */
2564 WARN_ON(1);
2565 return (u32)-1;
2566}
2567
2568/**
2569 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
2570 * @hsotg: The driver state.
2571 * @ep: The index number of the endpoint
2572 * @mps: The maximum packet size in bytes
Googler9398cc32022-12-02 17:21:52 +08002573 * @mc: The multicount value
2574 * @dir_in: True if direction is in.
Googleraf606d22022-10-26 21:40:12 -07002575 *
2576 * Configure the maximum packet size for the given endpoint, updating
2577 * the hardware control registers to reflect this.
2578 */
2579static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08002580 unsigned int ep, unsigned int mps,
2581 unsigned int mc, unsigned int dir_in)
Googleraf606d22022-10-26 21:40:12 -07002582{
2583 struct dwc2_hsotg_ep *hs_ep;
Googleraf606d22022-10-26 21:40:12 -07002584 u32 reg;
2585
2586 hs_ep = index_to_ep(hsotg, ep, dir_in);
2587 if (!hs_ep)
2588 return;
2589
2590 if (ep == 0) {
Googler9398cc32022-12-02 17:21:52 +08002591 u32 mps_bytes = mps;
2592
Googleraf606d22022-10-26 21:40:12 -07002593 /* EP0 is a special case */
Googler9398cc32022-12-02 17:21:52 +08002594 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2595 if (mps > 3)
Googler012a81c2022-09-15 14:55:24 +08002596 goto bad_mps;
Googler9398cc32022-12-02 17:21:52 +08002597 hs_ep->ep.maxpacket = mps_bytes;
Googler9726be62022-12-14 05:53:31 +00002598 hs_ep->mc = 1;
2599 } else {
Googler9398cc32022-12-02 17:21:52 +08002600 if (mps > 1024)
Googler9726be62022-12-14 05:53:31 +00002601 goto bad_mps;
Googler9398cc32022-12-02 17:21:52 +08002602 hs_ep->mc = mc;
2603 if (mc > 3)
Googler9726be62022-12-14 05:53:31 +00002604 goto bad_mps;
Googler9398cc32022-12-02 17:21:52 +08002605 hs_ep->ep.maxpacket = mps;
Googleraf606d22022-10-26 21:40:12 -07002606 }
2607
2608 if (dir_in) {
Googler9398cc32022-12-02 17:21:52 +08002609 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Googleraf606d22022-10-26 21:40:12 -07002610 reg &= ~DXEPCTL_MPS_MASK;
Googler9398cc32022-12-02 17:21:52 +08002611 reg |= mps;
2612 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Googleraf606d22022-10-26 21:40:12 -07002613 } else {
Googler9398cc32022-12-02 17:21:52 +08002614 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Googleraf606d22022-10-26 21:40:12 -07002615 reg &= ~DXEPCTL_MPS_MASK;
Googler9398cc32022-12-02 17:21:52 +08002616 reg |= mps;
2617 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Googleraf606d22022-10-26 21:40:12 -07002618 }
2619
2620 return;
2621
2622bad_mps:
2623 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2624}
2625
2626/**
2627 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
2628 * @hsotg: The driver state
2629 * @idx: The index for the endpoint (0..15)
2630 */
2631static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
2632{
Googler9398cc32022-12-02 17:21:52 +08002633 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2634 GRSTCTL);
Googleraf606d22022-10-26 21:40:12 -07002635
2636 /* wait until the fifo is flushed */
Googler9398cc32022-12-02 17:21:52 +08002637 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2638 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2639 __func__);
Googleraf606d22022-10-26 21:40:12 -07002640}
2641
2642/**
2643 * dwc2_hsotg_trytx - check to see if anything needs transmitting
2644 * @hsotg: The driver state
2645 * @hs_ep: The driver endpoint to check.
2646 *
2647 * Check to see if there is a request that has data to send, and if so
2648 * make an attempt to write data into the FIFO.
2649 */
2650static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08002651 struct dwc2_hsotg_ep *hs_ep)
Googleraf606d22022-10-26 21:40:12 -07002652{
2653 struct dwc2_hsotg_req *hs_req = hs_ep->req;
2654
2655 if (!hs_ep->dir_in || !hs_req) {
2656 /**
2657 * if request is not enqueued, we disable interrupts
2658 * for endpoints, excepting ep0
2659 */
2660 if (hs_ep->index != 0)
2661 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Googler9398cc32022-12-02 17:21:52 +08002662 hs_ep->dir_in, 0);
Googleraf606d22022-10-26 21:40:12 -07002663 return 0;
2664 }
2665
2666 if (hs_req->req.actual < hs_req->req.length) {
2667 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2668 hs_ep->index);
2669 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
2670 }
2671
2672 return 0;
2673}
2674
2675/**
2676 * dwc2_hsotg_complete_in - complete IN transfer
2677 * @hsotg: The device state.
2678 * @hs_ep: The endpoint that has just completed.
2679 *
2680 * An IN transfer has been completed, update the transfer's state and then
2681 * call the relevant completion routines.
2682 */
2683static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08002684 struct dwc2_hsotg_ep *hs_ep)
Googleraf606d22022-10-26 21:40:12 -07002685{
2686 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Googler9398cc32022-12-02 17:21:52 +08002687 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Googleraf606d22022-10-26 21:40:12 -07002688 int size_left, size_done;
2689
2690 if (!hs_req) {
2691 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2692 return;
2693 }
2694
2695 /* Finish ZLP handling for IN EP0 transactions */
2696 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2697 dev_dbg(hsotg->dev, "zlp packet sent\n");
Googler9398cc32022-12-02 17:21:52 +08002698
2699 /*
2700 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2701 * changed to IN. Change back to complete OUT transfer request
2702 */
2703 hs_ep->dir_in = 0;
2704
Googleraf606d22022-10-26 21:40:12 -07002705 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2706 if (hsotg->test_mode) {
2707 int ret;
2708
2709 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
2710 if (ret < 0) {
2711 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
Googler9398cc32022-12-02 17:21:52 +08002712 hsotg->test_mode);
Googleraf606d22022-10-26 21:40:12 -07002713 dwc2_hsotg_stall_ep0(hsotg);
2714 return;
2715 }
2716 }
2717 dwc2_hsotg_enqueue_setup(hsotg);
2718 return;
2719 }
2720
2721 /*
2722 * Calculate the size of the transfer by checking how much is left
2723 * in the endpoint size register and then working it out from
2724 * the amount we loaded for the transfer.
2725 *
2726 * We do this even for DMA, as the transfer may have incremented
2727 * past the end of the buffer (DMA transfers are always 32bit
2728 * aligned).
2729 */
Googler9398cc32022-12-02 17:21:52 +08002730 if (using_desc_dma(hsotg)) {
2731 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2732 if (size_left < 0)
2733 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2734 size_left);
2735 } else {
2736 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2737 }
Googleraf606d22022-10-26 21:40:12 -07002738
2739 size_done = hs_ep->size_loaded - size_left;
2740 size_done += hs_ep->last_load;
2741
2742 if (hs_req->req.actual != size_done)
2743 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2744 __func__, hs_req->req.actual, size_done);
2745
2746 hs_req->req.actual = size_done;
2747 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2748 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
2749
2750 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2751 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
2752 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2753 return;
2754 }
2755
Googlerb48fa912023-03-17 12:40:29 +05302756 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Googleraf606d22022-10-26 21:40:12 -07002757 if (hs_ep->send_zlp) {
Googlerb48fa912023-03-17 12:40:29 +05302758 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Googleraf606d22022-10-26 21:40:12 -07002759 hs_ep->send_zlp = 0;
Googlerb48fa912023-03-17 12:40:29 +05302760 /* transfer will be completed on next complete interrupt */
2761 return;
Googleraf606d22022-10-26 21:40:12 -07002762 }
2763
2764 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2765 /* Move to STATUS OUT */
2766 dwc2_hsotg_ep0_zlp(hsotg, false);
2767 return;
2768 }
2769
2770 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2771}
2772
2773/**
2774 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2775 * @hsotg: The device state.
2776 * @idx: Index of ep.
2777 * @dir_in: Endpoint direction 1-in 0-out.
2778 *
2779 * Reads for endpoint with given index and direction, by masking
2780 * epint_reg with coresponding mask.
2781 */
2782static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2783 unsigned int idx, int dir_in)
2784{
2785 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2786 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2787 u32 ints;
2788 u32 mask;
2789 u32 diepempmsk;
2790
Googler9398cc32022-12-02 17:21:52 +08002791 mask = dwc2_readl(hsotg, epmsk_reg);
2792 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Googleraf606d22022-10-26 21:40:12 -07002793 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2794 mask |= DXEPINT_SETUP_RCVD;
2795
Googler9398cc32022-12-02 17:21:52 +08002796 ints = dwc2_readl(hsotg, epint_reg);
Googleraf606d22022-10-26 21:40:12 -07002797 ints &= mask;
2798 return ints;
2799}
2800
2801/**
2802 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2803 * @hs_ep: The endpoint on which interrupt is asserted.
2804 *
2805 * This interrupt indicates that the endpoint has been disabled per the
2806 * application's request.
2807 *
2808 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2809 * in case of ISOC completes current request.
2810 *
2811 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2812 * request starts it.
2813 */
2814static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2815{
2816 struct dwc2_hsotg *hsotg = hs_ep->parent;
2817 struct dwc2_hsotg_req *hs_req;
2818 unsigned char idx = hs_ep->index;
2819 int dir_in = hs_ep->dir_in;
2820 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Googler9398cc32022-12-02 17:21:52 +08002821 int dctl = dwc2_readl(hsotg, DCTL);
Googleraf606d22022-10-26 21:40:12 -07002822
2823 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2824
2825 if (dir_in) {
Googler9398cc32022-12-02 17:21:52 +08002826 int epctl = dwc2_readl(hsotg, epctl_reg);
Googleraf606d22022-10-26 21:40:12 -07002827
2828 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2829
Googlerb48fa912023-03-17 12:40:29 +05302830 if (hs_ep->isochronous) {
2831 dwc2_hsotg_complete_in(hsotg, hs_ep);
2832 return;
2833 }
2834
Googleraf606d22022-10-26 21:40:12 -07002835 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Googler9398cc32022-12-02 17:21:52 +08002836 int dctl = dwc2_readl(hsotg, DCTL);
Googleraf606d22022-10-26 21:40:12 -07002837
2838 dctl |= DCTL_CGNPINNAK;
Googler9398cc32022-12-02 17:21:52 +08002839 dwc2_writel(hsotg, dctl, DCTL);
Googleraf606d22022-10-26 21:40:12 -07002840 }
Googlerb48fa912023-03-17 12:40:29 +05302841 return;
2842 }
Googleraf606d22022-10-26 21:40:12 -07002843
Googlerb48fa912023-03-17 12:40:29 +05302844 if (dctl & DCTL_GOUTNAKSTS) {
2845 dctl |= DCTL_CGOUTNAK;
2846 dwc2_writel(hsotg, dctl, DCTL);
Googleraf606d22022-10-26 21:40:12 -07002847 }
2848
2849 if (!hs_ep->isochronous)
2850 return;
2851
2852 if (list_empty(&hs_ep->queue)) {
2853 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2854 __func__, hs_ep);
2855 return;
2856 }
2857
2858 do {
2859 hs_req = get_ep_head(hs_ep);
Googlerb48fa912023-03-17 12:40:29 +05302860 if (hs_req)
Googleraf606d22022-10-26 21:40:12 -07002861 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2862 -ENODATA);
2863 dwc2_gadget_incr_frame_num(hs_ep);
Googler9398cc32022-12-02 17:21:52 +08002864 /* Update current frame number value. */
2865 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Googleraf606d22022-10-26 21:40:12 -07002866 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
Googlerb48fa912023-03-17 12:40:29 +05302867
2868 dwc2_gadget_start_next_request(hs_ep);
Googleraf606d22022-10-26 21:40:12 -07002869}
2870
2871/**
2872 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Googler9398cc32022-12-02 17:21:52 +08002873 * @ep: The endpoint on which interrupt is asserted.
Googleraf606d22022-10-26 21:40:12 -07002874 *
2875 * This is starting point for ISOC-OUT transfer, synchronization done with
2876 * first out token received from host while corresponding EP is disabled.
2877 *
2878 * Device does not know initial frame in which out token will come. For this
2879 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2880 * getting this interrupt SW starts calculation for next transfer frame.
2881 */
2882static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2883{
2884 struct dwc2_hsotg *hsotg = ep->parent;
2885 int dir_in = ep->dir_in;
Googlerb48fa912023-03-17 12:40:29 +05302886 u32 doepmsk;
Googleraf606d22022-10-26 21:40:12 -07002887
2888 if (dir_in || !ep->isochronous)
2889 return;
2890
Googler9398cc32022-12-02 17:21:52 +08002891 if (using_desc_dma(hsotg)) {
2892 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2893 /* Start first ISO Out */
2894 ep->target_frame = hsotg->frame_number;
2895 dwc2_gadget_start_isoc_ddma(ep);
2896 }
2897 return;
Googler38bda472022-08-19 10:07:08 -07002898 }
2899
Googlerb48fa912023-03-17 12:40:29 +05302900 if (ep->interval > 1 &&
2901 ep->target_frame == TARGET_FRAME_INITIAL) {
Googler9398cc32022-12-02 17:21:52 +08002902 u32 ctrl;
2903
2904 ep->target_frame = hsotg->frame_number;
Googler9398cc32022-12-02 17:21:52 +08002905 dwc2_gadget_incr_frame_num(ep);
Googlerb48fa912023-03-17 12:40:29 +05302906
2907 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
2908 if (ep->target_frame & 0x1)
2909 ctrl |= DXEPCTL_SETODDFR;
2910 else
2911 ctrl |= DXEPCTL_SETEVENFR;
2912
2913 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Googler9398cc32022-12-02 17:21:52 +08002914 }
2915
Googlerb48fa912023-03-17 12:40:29 +05302916 dwc2_gadget_start_next_request(ep);
2917 doepmsk = dwc2_readl(hsotg, DOEPMSK);
2918 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2919 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07002920}
2921
2922/**
Googler9398cc32022-12-02 17:21:52 +08002923 * dwc2_gadget_handle_nak - handle NAK interrupt
2924 * @hs_ep: The endpoint on which interrupt is asserted.
2925 *
2926 * This is starting point for ISOC-IN transfer, synchronization done with
2927 * first IN token received from host while corresponding EP is disabled.
2928 *
2929 * Device does not know when first one token will arrive from host. On first
2930 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2931 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2932 * sent in response to that as there was no data in FIFO. SW is basing on this
2933 * interrupt to obtain frame in which token has come and then based on the
2934 * interval calculates next frame for transfer.
2935 */
Googleraf606d22022-10-26 21:40:12 -07002936static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2937{
2938 struct dwc2_hsotg *hsotg = hs_ep->parent;
2939 int dir_in = hs_ep->dir_in;
2940
2941 if (!dir_in || !hs_ep->isochronous)
2942 return;
2943
2944 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Googler9398cc32022-12-02 17:21:52 +08002945
2946 if (using_desc_dma(hsotg)) {
2947 hs_ep->target_frame = hsotg->frame_number;
2948 dwc2_gadget_incr_frame_num(hs_ep);
2949
2950 /* In service interval mode target_frame must
2951 * be set to last (u)frame of the service interval.
2952 */
2953 if (hsotg->params.service_interval) {
2954 /* Set target_frame to the first (u)frame of
2955 * the service interval
2956 */
2957 hs_ep->target_frame &= ~hs_ep->interval + 1;
2958
2959 /* Set target_frame to the last (u)frame of
2960 * the service interval
2961 */
2962 dwc2_gadget_incr_frame_num(hs_ep);
2963 dwc2_gadget_dec_frame_num_by_one(hs_ep);
2964 }
2965
2966 dwc2_gadget_start_isoc_ddma(hs_ep);
2967 return;
2968 }
2969
2970 hs_ep->target_frame = hsotg->frame_number;
Googleraf606d22022-10-26 21:40:12 -07002971 if (hs_ep->interval > 1) {
Googler9398cc32022-12-02 17:21:52 +08002972 u32 ctrl = dwc2_readl(hsotg,
Googleraf606d22022-10-26 21:40:12 -07002973 DIEPCTL(hs_ep->index));
2974 if (hs_ep->target_frame & 0x1)
2975 ctrl |= DXEPCTL_SETODDFR;
2976 else
2977 ctrl |= DXEPCTL_SETEVENFR;
2978
Googler9398cc32022-12-02 17:21:52 +08002979 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Googler38bda472022-08-19 10:07:08 -07002980 }
Googlerb48fa912023-03-17 12:40:29 +05302981
2982 dwc2_hsotg_complete_request(hsotg, hs_ep,
2983 get_ep_head(hs_ep), 0);
Googler38bda472022-08-19 10:07:08 -07002984 }
2985
Googlerb48fa912023-03-17 12:40:29 +05302986 if (!using_desc_dma(hsotg))
Googler9398cc32022-12-02 17:21:52 +08002987 dwc2_gadget_incr_frame_num(hs_ep);
Googleraf606d22022-10-26 21:40:12 -07002988}
2989
2990/**
2991 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
2992 * @hsotg: The driver state
2993 * @idx: The index for the endpoint (0..15)
2994 * @dir_in: Set if this is an IN endpoint
2995 *
2996 * Process and clear any interrupt pending for an individual endpoint
2997 */
2998static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Googler9398cc32022-12-02 17:21:52 +08002999 int dir_in)
Googleraf606d22022-10-26 21:40:12 -07003000{
3001 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
3002 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
3003 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
3004 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
3005 u32 ints;
3006 u32 ctrl;
3007
3008 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Googler9398cc32022-12-02 17:21:52 +08003009 ctrl = dwc2_readl(hsotg, epctl_reg);
Googleraf606d22022-10-26 21:40:12 -07003010
3011 /* Clear endpoint interrupts */
Googler9398cc32022-12-02 17:21:52 +08003012 dwc2_writel(hsotg, ints, epint_reg);
Googleraf606d22022-10-26 21:40:12 -07003013
3014 if (!hs_ep) {
3015 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
Googler9398cc32022-12-02 17:21:52 +08003016 __func__, idx, dir_in ? "in" : "out");
Googleraf606d22022-10-26 21:40:12 -07003017 return;
3018 }
3019
3020 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
3021 __func__, idx, dir_in ? "in" : "out", ints);
3022
3023 /* Don't process XferCompl interrupt if it is a setup packet */
3024 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
3025 ints &= ~DXEPINT_XFERCOMPL;
3026
Googler9398cc32022-12-02 17:21:52 +08003027 /*
3028 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
3029 * stage and xfercomplete was generated without SETUP phase done
3030 * interrupt. SW should parse received setup packet only after host's
3031 * exit from setup phase of control transfer.
3032 */
3033 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
3034 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
3035 ints &= ~DXEPINT_XFERCOMPL;
Googleraf606d22022-10-26 21:40:12 -07003036
3037 if (ints & DXEPINT_XFERCOMPL) {
3038 dev_dbg(hsotg->dev,
3039 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Googler9398cc32022-12-02 17:21:52 +08003040 __func__, dwc2_readl(hsotg, epctl_reg),
3041 dwc2_readl(hsotg, epsiz_reg));
Googleraf606d22022-10-26 21:40:12 -07003042
Googler9398cc32022-12-02 17:21:52 +08003043 /* In DDMA handle isochronous requests separately */
3044 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Googlerb48fa912023-03-17 12:40:29 +05303045 /* XferCompl set along with BNA */
3046 if (!(ints & DXEPINT_BNAINTR))
3047 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Googler9398cc32022-12-02 17:21:52 +08003048 } else if (dir_in) {
3049 /*
3050 * We get OutDone from the FIFO, so we only
3051 * need to look at completing IN requests here
3052 * if operating slave mode
3053 */
Googlerb48fa912023-03-17 12:40:29 +05303054 if (hs_ep->isochronous && hs_ep->interval > 1)
3055 dwc2_gadget_incr_frame_num(hs_ep);
3056
3057 dwc2_hsotg_complete_in(hsotg, hs_ep);
3058 if (ints & DXEPINT_NAKINTRPT)
3059 ints &= ~DXEPINT_NAKINTRPT;
Googleraf606d22022-10-26 21:40:12 -07003060
3061 if (idx == 0 && !hs_ep->req)
3062 dwc2_hsotg_enqueue_setup(hsotg);
3063 } else if (using_dma(hsotg)) {
3064 /*
3065 * We're using DMA, we need to fire an OutDone here
3066 * as we ignore the RXFIFO.
3067 */
Googlerb48fa912023-03-17 12:40:29 +05303068 if (hs_ep->isochronous && hs_ep->interval > 1)
3069 dwc2_gadget_incr_frame_num(hs_ep);
3070
3071 dwc2_hsotg_handle_outdone(hsotg, idx);
Googleraf606d22022-10-26 21:40:12 -07003072 }
3073 }
3074
3075 if (ints & DXEPINT_EPDISBLD)
3076 dwc2_gadget_handle_ep_disabled(hs_ep);
3077
3078 if (ints & DXEPINT_OUTTKNEPDIS)
3079 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
3080
3081 if (ints & DXEPINT_NAKINTRPT)
3082 dwc2_gadget_handle_nak(hs_ep);
3083
3084 if (ints & DXEPINT_AHBERR)
3085 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
3086
3087 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
3088 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3089
3090 if (using_dma(hsotg) && idx == 0) {
3091 /*
3092 * this is the notification we've received a
3093 * setup packet. In non-DMA mode we'd get this
3094 * from the RXFIFO, instead we need to process
3095 * the setup here.
3096 */
3097
3098 if (dir_in)
3099 WARN_ON_ONCE(1);
3100 else
3101 dwc2_hsotg_handle_outdone(hsotg, 0);
3102 }
3103 }
3104
Googler9398cc32022-12-02 17:21:52 +08003105 if (ints & DXEPINT_STSPHSERCVD) {
3106 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3107
3108 /* Safety check EP0 state when STSPHSERCVD asserted */
3109 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
3110 /* Move to STATUS IN for DDMA */
3111 if (using_desc_dma(hsotg)) {
3112 if (!hsotg->delayed_status)
3113 dwc2_hsotg_ep0_zlp(hsotg, true);
3114 else
3115 /* In case of 3 stage Control Write with delayed
3116 * status, when Status IN transfer started
3117 * before STSPHSERCVD asserted, NAKSTS bit not
3118 * cleared by CNAK in dwc2_hsotg_start_req()
3119 * function. Clear now NAKSTS to allow complete
3120 * transfer.
3121 */
3122 dwc2_set_bit(hsotg, DIEPCTL(0),
3123 DXEPCTL_CNAK);
3124 }
3125 }
3126
3127 }
3128
Googleraf606d22022-10-26 21:40:12 -07003129 if (ints & DXEPINT_BACK2BACKSETUP)
3130 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
3131
Googler9398cc32022-12-02 17:21:52 +08003132 if (ints & DXEPINT_BNAINTR) {
3133 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
3134 if (hs_ep->isochronous)
3135 dwc2_gadget_handle_isoc_bna(hs_ep);
3136 }
3137
Googleraf606d22022-10-26 21:40:12 -07003138 if (dir_in && !hs_ep->isochronous) {
3139 /* not sure if this is important, but we'll clear it anyway */
3140 if (ints & DXEPINT_INTKNTXFEMP) {
3141 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3142 __func__, idx);
3143 }
3144
3145 /* this probably means something bad is happening */
3146 if (ints & DXEPINT_INTKNEPMIS) {
3147 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3148 __func__, idx);
3149 }
3150
3151 /* FIFO has space or is empty (see GAHBCFG) */
3152 if (hsotg->dedicated_fifos &&
3153 ints & DXEPINT_TXFEMP) {
3154 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3155 __func__, idx);
3156 if (!using_dma(hsotg))
3157 dwc2_hsotg_trytx(hsotg, hs_ep);
3158 }
3159 }
3160}
3161
3162/**
3163 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
3164 * @hsotg: The device state.
3165 *
3166 * Handle updating the device settings after the enumeration phase has
3167 * been completed.
3168 */
3169static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
3170{
Googler9398cc32022-12-02 17:21:52 +08003171 u32 dsts = dwc2_readl(hsotg, DSTS);
Googleraf606d22022-10-26 21:40:12 -07003172 int ep0_mps = 0, ep_mps = 8;
3173
3174 /*
3175 * This should signal the finish of the enumeration phase
3176 * of the USB handshaking, so we should now know what rate
3177 * we connected at.
3178 */
3179
3180 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3181
3182 /*
3183 * note, since we're limited by the size of transfer on EP0, and
3184 * it seems IN transfers must be a even number of packets we do
3185 * not advertise a 64byte MPS on EP0.
3186 */
3187
3188 /* catch both EnumSpd_FS and EnumSpd_FS48 */
3189 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
3190 case DSTS_ENUMSPD_FS:
3191 case DSTS_ENUMSPD_FS48:
3192 hsotg->gadget.speed = USB_SPEED_FULL;
3193 ep0_mps = EP0_MPS_LIMIT;
3194 ep_mps = 1023;
3195 break;
3196
3197 case DSTS_ENUMSPD_HS:
3198 hsotg->gadget.speed = USB_SPEED_HIGH;
3199 ep0_mps = EP0_MPS_LIMIT;
3200 ep_mps = 1024;
3201 break;
3202
3203 case DSTS_ENUMSPD_LS:
3204 hsotg->gadget.speed = USB_SPEED_LOW;
Googler9398cc32022-12-02 17:21:52 +08003205 ep0_mps = 8;
3206 ep_mps = 8;
Googleraf606d22022-10-26 21:40:12 -07003207 /*
3208 * note, we don't actually support LS in this driver at the
3209 * moment, and the documentation seems to imply that it isn't
3210 * supported by the PHYs on some of the devices.
3211 */
3212 break;
3213 }
3214 dev_info(hsotg->dev, "new device is %s\n",
3215 usb_speed_string(hsotg->gadget.speed));
3216
3217 /*
3218 * we should now know the maximum packet size for an
3219 * endpoint, so set the endpoints to a default value.
3220 */
3221
3222 if (ep0_mps) {
3223 int i;
3224 /* Initialize ep0 for both in and out directions */
Googler9398cc32022-12-02 17:21:52 +08003225 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3226 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Googleraf606d22022-10-26 21:40:12 -07003227 for (i = 1; i < hsotg->num_of_eps; i++) {
3228 if (hsotg->eps_in[i])
Googler9398cc32022-12-02 17:21:52 +08003229 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3230 0, 1);
Googleraf606d22022-10-26 21:40:12 -07003231 if (hsotg->eps_out[i])
Googler9398cc32022-12-02 17:21:52 +08003232 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3233 0, 0);
Googleraf606d22022-10-26 21:40:12 -07003234 }
3235 }
3236
3237 /* ensure after enumeration our EP0 is active */
3238
3239 dwc2_hsotg_enqueue_setup(hsotg);
3240
3241 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08003242 dwc2_readl(hsotg, DIEPCTL0),
3243 dwc2_readl(hsotg, DOEPCTL0));
Googleraf606d22022-10-26 21:40:12 -07003244}
3245
3246/**
3247 * kill_all_requests - remove all requests from the endpoint's queue
3248 * @hsotg: The device state.
3249 * @ep: The endpoint the requests may be on.
3250 * @result: The result code to use.
3251 *
3252 * Go through the requests on the given endpoint and mark them
3253 * completed with the given result code.
3254 */
3255static void kill_all_requests(struct dwc2_hsotg *hsotg,
3256 struct dwc2_hsotg_ep *ep,
3257 int result)
3258{
Googler9398cc32022-12-02 17:21:52 +08003259 unsigned int size;
Googleraf606d22022-10-26 21:40:12 -07003260
3261 ep->req = NULL;
3262
Googler9398cc32022-12-02 17:21:52 +08003263 while (!list_empty(&ep->queue)) {
3264 struct dwc2_hsotg_req *req = get_ep_head(ep);
3265
3266 dwc2_hsotg_complete_request(hsotg, ep, req, result);
3267 }
Googleraf606d22022-10-26 21:40:12 -07003268
3269 if (!hsotg->dedicated_fifos)
3270 return;
Googler9398cc32022-12-02 17:21:52 +08003271 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Googleraf606d22022-10-26 21:40:12 -07003272 if (size < ep->fifo_size)
3273 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
3274}
3275
3276/**
3277 * dwc2_hsotg_disconnect - disconnect service
3278 * @hsotg: The device state.
3279 *
3280 * The device has been disconnected. Remove all current
3281 * transactions and signal the gadget driver that this
3282 * has happened.
3283 */
3284void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
3285{
Googler9398cc32022-12-02 17:21:52 +08003286 unsigned int ep;
Googleraf606d22022-10-26 21:40:12 -07003287
3288 if (!hsotg->connected)
3289 return;
3290
3291 hsotg->connected = 0;
3292 hsotg->test_mode = 0;
3293
Googler9398cc32022-12-02 17:21:52 +08003294 /* all endpoints should be shutdown */
Googleraf606d22022-10-26 21:40:12 -07003295 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3296 if (hsotg->eps_in[ep])
3297 kill_all_requests(hsotg, hsotg->eps_in[ep],
Googler9398cc32022-12-02 17:21:52 +08003298 -ESHUTDOWN);
Googleraf606d22022-10-26 21:40:12 -07003299 if (hsotg->eps_out[ep])
3300 kill_all_requests(hsotg, hsotg->eps_out[ep],
Googler9398cc32022-12-02 17:21:52 +08003301 -ESHUTDOWN);
Googleraf606d22022-10-26 21:40:12 -07003302 }
3303
3304 call_gadget(hsotg, disconnect);
3305 hsotg->lx_state = DWC2_L3;
Googler9398cc32022-12-02 17:21:52 +08003306
3307 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Googleraf606d22022-10-26 21:40:12 -07003308}
3309
3310/**
3311 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
3312 * @hsotg: The device state:
3313 * @periodic: True if this is a periodic FIFO interrupt
3314 */
3315static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
3316{
3317 struct dwc2_hsotg_ep *ep;
3318 int epno, ret;
3319
3320 /* look through for any more data to transmit */
3321 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
3322 ep = index_to_ep(hsotg, epno, 1);
3323
3324 if (!ep)
3325 continue;
3326
3327 if (!ep->dir_in)
3328 continue;
3329
3330 if ((periodic && !ep->periodic) ||
3331 (!periodic && ep->periodic))
3332 continue;
3333
3334 ret = dwc2_hsotg_trytx(hsotg, ep);
3335 if (ret < 0)
3336 break;
3337 }
3338}
3339
3340/* IRQ flags which will trigger a retry around the IRQ loop */
3341#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3342 GINTSTS_PTXFEMP | \
3343 GINTSTS_RXFLVL)
3344
Googler9398cc32022-12-02 17:21:52 +08003345static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
Googleraf606d22022-10-26 21:40:12 -07003346/**
3347 * dwc2_hsotg_core_init - issue softreset to the core
3348 * @hsotg: The device state
Googler9398cc32022-12-02 17:21:52 +08003349 * @is_usb_reset: Usb resetting flag
Googleraf606d22022-10-26 21:40:12 -07003350 *
3351 * Issue a soft reset to the core, and await the core finishing it.
3352 */
3353void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08003354 bool is_usb_reset)
Googleraf606d22022-10-26 21:40:12 -07003355{
3356 u32 intmsk;
3357 u32 val;
3358 u32 usbcfg;
Googler9398cc32022-12-02 17:21:52 +08003359 u32 dcfg = 0;
3360 int ep;
Googleraf606d22022-10-26 21:40:12 -07003361
3362 /* Kill any ep0 requests as controller will be reinitialized */
3363 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3364
Googler9398cc32022-12-02 17:21:52 +08003365 if (!is_usb_reset) {
3366 if (dwc2_core_reset(hsotg, true))
Googleraf606d22022-10-26 21:40:12 -07003367 return;
Googler9398cc32022-12-02 17:21:52 +08003368 } else {
3369 /* all endpoints should be shutdown */
3370 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3371 if (hsotg->eps_in[ep])
3372 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3373 if (hsotg->eps_out[ep])
3374 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3375 }
3376 }
Googleraf606d22022-10-26 21:40:12 -07003377
3378 /*
3379 * we must now enable ep0 ready for host detection and then
3380 * set configuration.
3381 */
3382
3383 /* keep other bits untouched (so e.g. forced modes are not lost) */
Googler9398cc32022-12-02 17:21:52 +08003384 usbcfg = dwc2_readl(hsotg, GUSBCFG);
3385 usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
3386 usbcfg |= GUSBCFG_TOUTCAL(7);
Googleraf606d22022-10-26 21:40:12 -07003387
Googler9398cc32022-12-02 17:21:52 +08003388 /* remove the HNP/SRP and set the PHY */
3389 usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP);
3390 dwc2_writel(hsotg, usbcfg, GUSBCFG);
3391
3392 dwc2_phy_init(hsotg, true);
Googleraf606d22022-10-26 21:40:12 -07003393
3394 dwc2_hsotg_init_fifo(hsotg);
3395
3396 if (!is_usb_reset)
Googler9398cc32022-12-02 17:21:52 +08003397 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Googleraf606d22022-10-26 21:40:12 -07003398
Googler9398cc32022-12-02 17:21:52 +08003399 dcfg |= DCFG_EPMISCNT(1);
3400
3401 switch (hsotg->params.speed) {
3402 case DWC2_SPEED_PARAM_LOW:
3403 dcfg |= DCFG_DEVSPD_LS;
3404 break;
3405 case DWC2_SPEED_PARAM_FULL:
3406 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3407 dcfg |= DCFG_DEVSPD_FS48;
3408 else
3409 dcfg |= DCFG_DEVSPD_FS;
3410 break;
3411 default:
3412 dcfg |= DCFG_DEVSPD_HS;
3413 }
3414
3415 if (hsotg->params.ipg_isoc_en)
3416 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3417
3418 dwc2_writel(hsotg, dcfg, DCFG);
Googleraf606d22022-10-26 21:40:12 -07003419
3420 /* Clear any pending OTG interrupts */
Googler9398cc32022-12-02 17:21:52 +08003421 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Googleraf606d22022-10-26 21:40:12 -07003422
3423 /* Clear any pending interrupts */
Googler9398cc32022-12-02 17:21:52 +08003424 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003425 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
3426 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
3427 GINTSTS_USBRST | GINTSTS_RESETDET |
3428 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
3429 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
Googler9398cc32022-12-02 17:21:52 +08003430 GINTSTS_LPMTRANRCVD;
Googleraf606d22022-10-26 21:40:12 -07003431
Googler9398cc32022-12-02 17:21:52 +08003432 if (!using_desc_dma(hsotg))
3433 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
3434
3435 if (!hsotg->params.external_id_pin_ctl)
Googleraf606d22022-10-26 21:40:12 -07003436 intmsk |= GINTSTS_CONIDSTSCHNG;
3437
Googler9398cc32022-12-02 17:21:52 +08003438 dwc2_writel(hsotg, intmsk, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003439
Googler9398cc32022-12-02 17:21:52 +08003440 if (using_dma(hsotg)) {
3441 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3442 hsotg->params.ahbcfg,
3443 GAHBCFG);
3444
3445 /* Set DDMA mode support in the core if needed */
3446 if (using_desc_dma(hsotg))
3447 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
3448
3449 } else {
3450 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Googleraf606d22022-10-26 21:40:12 -07003451 (GAHBCFG_NP_TXF_EMP_LVL |
3452 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Googler9398cc32022-12-02 17:21:52 +08003453 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
3454 }
Googleraf606d22022-10-26 21:40:12 -07003455
3456 /*
3457 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3458 * when we have no data to transfer. Otherwise we get being flooded by
3459 * interrupts.
3460 */
3461
Googler9398cc32022-12-02 17:21:52 +08003462 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Googleraf606d22022-10-26 21:40:12 -07003463 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
3464 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
3465 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Googler9398cc32022-12-02 17:21:52 +08003466 DIEPMSK);
Googleraf606d22022-10-26 21:40:12 -07003467
3468 /*
3469 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Googler9398cc32022-12-02 17:21:52 +08003470 * DMA mode we may need this and StsPhseRcvd.
Googleraf606d22022-10-26 21:40:12 -07003471 */
Googler9398cc32022-12-02 17:21:52 +08003472 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3473 DOEPMSK_STSPHSERCVDMSK) : 0) |
Googleraf606d22022-10-26 21:40:12 -07003474 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Googler9398cc32022-12-02 17:21:52 +08003475 DOEPMSK_SETUPMSK,
3476 DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07003477
Googler9398cc32022-12-02 17:21:52 +08003478 /* Enable BNA interrupt for DDMA */
3479 if (using_desc_dma(hsotg)) {
3480 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3481 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
3482 }
3483
3484 /* Enable Service Interval mode if supported */
3485 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3486 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3487
3488 dwc2_writel(hsotg, 0, DAINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003489
3490 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08003491 dwc2_readl(hsotg, DIEPCTL0),
3492 dwc2_readl(hsotg, DOEPCTL0));
Googleraf606d22022-10-26 21:40:12 -07003493
3494 /* enable in and out endpoint interrupts */
3495 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
3496
3497 /*
3498 * Enable the RXFIFO when in slave mode, as this is how we collect
3499 * the data. In DMA mode, we get events from the FIFO but also
3500 * things we cannot process, so do not use it.
3501 */
3502 if (!using_dma(hsotg))
3503 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
3504
3505 /* Enable interrupts for EP0 in and out */
3506 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3507 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
3508
3509 if (!is_usb_reset) {
Googler9398cc32022-12-02 17:21:52 +08003510 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Googleraf606d22022-10-26 21:40:12 -07003511 udelay(10); /* see openiboot */
Googler9398cc32022-12-02 17:21:52 +08003512 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Googleraf606d22022-10-26 21:40:12 -07003513 }
3514
Googler9398cc32022-12-02 17:21:52 +08003515 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Googleraf606d22022-10-26 21:40:12 -07003516
3517 /*
3518 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
3519 * writing to the EPCTL register..
3520 */
3521
3522 /* set to read 1 8byte packet */
Googler9398cc32022-12-02 17:21:52 +08003523 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3524 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Googleraf606d22022-10-26 21:40:12 -07003525
Googler9398cc32022-12-02 17:21:52 +08003526 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Googleraf606d22022-10-26 21:40:12 -07003527 DXEPCTL_CNAK | DXEPCTL_EPENA |
3528 DXEPCTL_USBACTEP,
Googler9398cc32022-12-02 17:21:52 +08003529 DOEPCTL0);
Googleraf606d22022-10-26 21:40:12 -07003530
3531 /* enable, but don't activate EP0in */
Googler9398cc32022-12-02 17:21:52 +08003532 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3533 DXEPCTL_USBACTEP, DIEPCTL0);
Googleraf606d22022-10-26 21:40:12 -07003534
3535 /* clear global NAKs */
3536 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3537 if (!is_usb_reset)
3538 val |= DCTL_SFTDISCON;
Googler9398cc32022-12-02 17:21:52 +08003539 dwc2_set_bit(hsotg, DCTL, val);
3540
3541 /* configure the core to support LPM */
3542 dwc2_gadget_init_lpm(hsotg);
3543
3544 /* program GREFCLK register if needed */
3545 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3546 dwc2_gadget_program_ref_clk(hsotg);
Googleraf606d22022-10-26 21:40:12 -07003547
3548 /* must be at-least 3ms to allow bus to see disconnect */
3549 mdelay(3);
3550
3551 hsotg->lx_state = DWC2_L0;
3552
3553 dwc2_hsotg_enqueue_setup(hsotg);
3554
3555 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08003556 dwc2_readl(hsotg, DIEPCTL0),
3557 dwc2_readl(hsotg, DOEPCTL0));
Googleraf606d22022-10-26 21:40:12 -07003558}
3559
3560static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
3561{
3562 /* set the soft-disconnect bit */
Googler9398cc32022-12-02 17:21:52 +08003563 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Googleraf606d22022-10-26 21:40:12 -07003564}
3565
3566void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
3567{
3568 /* remove the soft-disconnect and let's go */
Googler9398cc32022-12-02 17:21:52 +08003569 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Googleraf606d22022-10-26 21:40:12 -07003570}
3571
3572/**
3573 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3574 * @hsotg: The device state:
3575 *
3576 * This interrupt indicates one of the following conditions occurred while
3577 * transmitting an ISOC transaction.
3578 * - Corrupted IN Token for ISOC EP.
3579 * - Packet not complete in FIFO.
3580 *
3581 * The following actions will be taken:
3582 * - Determine the EP
3583 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3584 */
3585static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3586{
3587 struct dwc2_hsotg_ep *hs_ep;
3588 u32 epctrl;
Googler9398cc32022-12-02 17:21:52 +08003589 u32 daintmsk;
Googleraf606d22022-10-26 21:40:12 -07003590 u32 idx;
3591
3592 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3593
Googler9398cc32022-12-02 17:21:52 +08003594 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3595
3596 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Googleraf606d22022-10-26 21:40:12 -07003597 hs_ep = hsotg->eps_in[idx];
Googler9398cc32022-12-02 17:21:52 +08003598 /* Proceed only unmasked ISOC EPs */
3599 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
3600 continue;
3601
3602 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
3603 if ((epctrl & DXEPCTL_EPENA) &&
Googleraf606d22022-10-26 21:40:12 -07003604 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3605 epctrl |= DXEPCTL_SNAK;
3606 epctrl |= DXEPCTL_EPDIS;
Googler9398cc32022-12-02 17:21:52 +08003607 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Googleraf606d22022-10-26 21:40:12 -07003608 }
3609 }
3610
3611 /* Clear interrupt */
Googler9398cc32022-12-02 17:21:52 +08003612 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003613}
3614
3615/**
3616 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3617 * @hsotg: The device state:
3618 *
3619 * This interrupt indicates one of the following conditions occurred while
3620 * transmitting an ISOC transaction.
3621 * - Corrupted OUT Token for ISOC EP.
3622 * - Packet not complete in FIFO.
3623 *
3624 * The following actions will be taken:
3625 * - Determine the EP
3626 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3627 */
3628static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3629{
3630 u32 gintsts;
3631 u32 gintmsk;
Googler9398cc32022-12-02 17:21:52 +08003632 u32 daintmsk;
Googleraf606d22022-10-26 21:40:12 -07003633 u32 epctrl;
3634 struct dwc2_hsotg_ep *hs_ep;
3635 int idx;
3636
3637 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3638
Googler9398cc32022-12-02 17:21:52 +08003639 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3640 daintmsk >>= DAINT_OUTEP_SHIFT;
3641
3642 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Googleraf606d22022-10-26 21:40:12 -07003643 hs_ep = hsotg->eps_out[idx];
Googler9398cc32022-12-02 17:21:52 +08003644 /* Proceed only unmasked ISOC EPs */
3645 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
3646 continue;
3647
3648 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
3649 if ((epctrl & DXEPCTL_EPENA) &&
Googleraf606d22022-10-26 21:40:12 -07003650 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3651 /* Unmask GOUTNAKEFF interrupt */
Googler9398cc32022-12-02 17:21:52 +08003652 gintmsk = dwc2_readl(hsotg, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003653 gintmsk |= GINTSTS_GOUTNAKEFF;
Googler9398cc32022-12-02 17:21:52 +08003654 dwc2_writel(hsotg, gintmsk, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003655
Googler9398cc32022-12-02 17:21:52 +08003656 gintsts = dwc2_readl(hsotg, GINTSTS);
3657 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
3658 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
3659 break;
3660 }
Googleraf606d22022-10-26 21:40:12 -07003661 }
3662 }
3663
3664 /* Clear interrupt */
Googler9398cc32022-12-02 17:21:52 +08003665 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003666}
3667
3668/**
3669 * dwc2_hsotg_irq - handle device interrupt
3670 * @irq: The IRQ number triggered
3671 * @pw: The pw value when registered the handler.
3672 */
3673static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
3674{
3675 struct dwc2_hsotg *hsotg = pw;
3676 int retry_count = 8;
3677 u32 gintsts;
3678 u32 gintmsk;
3679
3680 if (!dwc2_is_device_mode(hsotg))
3681 return IRQ_NONE;
3682
3683 spin_lock(&hsotg->lock);
3684irq_retry:
Googler9398cc32022-12-02 17:21:52 +08003685 gintsts = dwc2_readl(hsotg, GINTSTS);
3686 gintmsk = dwc2_readl(hsotg, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003687
3688 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3689 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3690
3691 gintsts &= gintmsk;
3692
3693 if (gintsts & GINTSTS_RESETDET) {
3694 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3695
Googler9398cc32022-12-02 17:21:52 +08003696 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003697
3698 /* This event must be used only if controller is suspended */
3699 if (hsotg->lx_state == DWC2_L2) {
Googler9398cc32022-12-02 17:21:52 +08003700 dwc2_exit_partial_power_down(hsotg, true);
Googleraf606d22022-10-26 21:40:12 -07003701 hsotg->lx_state = DWC2_L0;
3702 }
3703 }
3704
3705 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Googler9398cc32022-12-02 17:21:52 +08003706 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Googleraf606d22022-10-26 21:40:12 -07003707 u32 connected = hsotg->connected;
3708
3709 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3710 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Googler9398cc32022-12-02 17:21:52 +08003711 dwc2_readl(hsotg, GNPTXSTS));
Googleraf606d22022-10-26 21:40:12 -07003712
Googler9398cc32022-12-02 17:21:52 +08003713 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003714
3715 /* Report disconnection if it is not already done. */
3716 dwc2_hsotg_disconnect(hsotg);
3717
Googler9398cc32022-12-02 17:21:52 +08003718 /* Reset device address to zero */
3719 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
3720
Googleraf606d22022-10-26 21:40:12 -07003721 if (usb_status & GOTGCTL_BSESVLD && connected)
3722 dwc2_hsotg_core_init_disconnected(hsotg, true);
3723 }
3724
3725 if (gintsts & GINTSTS_ENUMDONE) {
Googler9398cc32022-12-02 17:21:52 +08003726 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003727
3728 dwc2_hsotg_irq_enumdone(hsotg);
3729 }
3730
3731 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Googler9398cc32022-12-02 17:21:52 +08003732 u32 daint = dwc2_readl(hsotg, DAINT);
3733 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003734 u32 daint_out, daint_in;
3735 int ep;
3736
3737 daint &= daintmsk;
3738 daint_out = daint >> DAINT_OUTEP_SHIFT;
3739 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
3740
3741 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3742
3743 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3744 ep++, daint_out >>= 1) {
3745 if (daint_out & 1)
3746 dwc2_hsotg_epint(hsotg, ep, 0);
3747 }
3748
3749 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3750 ep++, daint_in >>= 1) {
3751 if (daint_in & 1)
3752 dwc2_hsotg_epint(hsotg, ep, 1);
3753 }
3754 }
3755
3756 /* check both FIFOs */
3757
3758 if (gintsts & GINTSTS_NPTXFEMP) {
3759 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3760
3761 /*
3762 * Disable the interrupt to stop it happening again
3763 * unless one of these endpoint routines decides that
3764 * it needs re-enabling
3765 */
3766
3767 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3768 dwc2_hsotg_irq_fifoempty(hsotg, false);
3769 }
3770
3771 if (gintsts & GINTSTS_PTXFEMP) {
3772 dev_dbg(hsotg->dev, "PTxFEmp\n");
3773
3774 /* See note in GINTSTS_NPTxFEmp */
3775
3776 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3777 dwc2_hsotg_irq_fifoempty(hsotg, true);
3778 }
3779
3780 if (gintsts & GINTSTS_RXFLVL) {
3781 /*
3782 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
3783 * we need to retry dwc2_hsotg_handle_rx if this is still
3784 * set.
3785 */
3786
3787 dwc2_hsotg_handle_rx(hsotg);
3788 }
3789
3790 if (gintsts & GINTSTS_ERLYSUSP) {
3791 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Googler9398cc32022-12-02 17:21:52 +08003792 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Googleraf606d22022-10-26 21:40:12 -07003793 }
3794
3795 /*
3796 * these next two seem to crop-up occasionally causing the core
3797 * to shutdown the USB transfer, so try clearing them and logging
3798 * the occurrence.
3799 */
3800
3801 if (gintsts & GINTSTS_GOUTNAKEFF) {
3802 u8 idx;
3803 u32 epctrl;
3804 u32 gintmsk;
Googler9398cc32022-12-02 17:21:52 +08003805 u32 daintmsk;
Googleraf606d22022-10-26 21:40:12 -07003806 struct dwc2_hsotg_ep *hs_ep;
3807
Googler9398cc32022-12-02 17:21:52 +08003808 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3809 daintmsk >>= DAINT_OUTEP_SHIFT;
Googleraf606d22022-10-26 21:40:12 -07003810 /* Mask this interrupt */
Googler9398cc32022-12-02 17:21:52 +08003811 gintmsk = dwc2_readl(hsotg, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003812 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Googler9398cc32022-12-02 17:21:52 +08003813 dwc2_writel(hsotg, gintmsk, GINTMSK);
Googleraf606d22022-10-26 21:40:12 -07003814
3815 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Googler9398cc32022-12-02 17:21:52 +08003816 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Googleraf606d22022-10-26 21:40:12 -07003817 hs_ep = hsotg->eps_out[idx];
Googler9398cc32022-12-02 17:21:52 +08003818 /* Proceed only unmasked ISOC EPs */
3819 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
3820 continue;
Googleraf606d22022-10-26 21:40:12 -07003821
Googler9398cc32022-12-02 17:21:52 +08003822 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
3823
3824 if (epctrl & DXEPCTL_EPENA) {
Googleraf606d22022-10-26 21:40:12 -07003825 epctrl |= DXEPCTL_SNAK;
3826 epctrl |= DXEPCTL_EPDIS;
Googler9398cc32022-12-02 17:21:52 +08003827 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Googleraf606d22022-10-26 21:40:12 -07003828 }
3829 }
3830
3831 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
3832 }
3833
3834 if (gintsts & GINTSTS_GINNAKEFF) {
3835 dev_info(hsotg->dev, "GINNakEff triggered\n");
3836
Googler9398cc32022-12-02 17:21:52 +08003837 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Googleraf606d22022-10-26 21:40:12 -07003838
3839 dwc2_hsotg_dump(hsotg);
3840 }
3841
3842 if (gintsts & GINTSTS_INCOMPL_SOIN)
3843 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
3844
3845 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3846 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
3847
3848 /*
3849 * if we've had fifo events, we should try and go around the
3850 * loop again to see if there's any point in returning yet.
3851 */
3852
3853 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
Googler9398cc32022-12-02 17:21:52 +08003854 goto irq_retry;
3855
3856 /* Check WKUP_ALERT interrupt*/
3857 if (hsotg->params.service_interval)
3858 dwc2_gadget_wkup_alert_handler(hsotg);
Googleraf606d22022-10-26 21:40:12 -07003859
3860 spin_unlock(&hsotg->lock);
3861
3862 return IRQ_HANDLED;
3863}
3864
Googler9398cc32022-12-02 17:21:52 +08003865static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3866 struct dwc2_hsotg_ep *hs_ep)
3867{
3868 u32 epctrl_reg;
3869 u32 epint_reg;
3870
3871 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3872 DOEPCTL(hs_ep->index);
3873 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3874 DOEPINT(hs_ep->index);
3875
3876 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3877 hs_ep->name);
3878
3879 if (hs_ep->dir_in) {
3880 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3881 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
3882 /* Wait for Nak effect */
3883 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3884 DXEPINT_INEPNAKEFF, 100))
3885 dev_warn(hsotg->dev,
3886 "%s: timeout DIEPINT.NAKEFF\n",
3887 __func__);
3888 } else {
3889 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
3890 /* Wait for Nak effect */
3891 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3892 GINTSTS_GINNAKEFF, 100))
3893 dev_warn(hsotg->dev,
3894 "%s: timeout GINTSTS.GINNAKEFF\n",
3895 __func__);
3896 }
3897 } else {
3898 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3899 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
3900
3901 /* Wait for global nak to take effect */
3902 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3903 GINTSTS_GOUTNAKEFF, 100))
3904 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3905 __func__);
3906 }
3907
3908 /* Disable ep */
3909 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3910
3911 /* Wait for ep to be disabled */
3912 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3913 dev_warn(hsotg->dev,
3914 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3915
3916 /* Clear EPDISBLD interrupt */
3917 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
3918
3919 if (hs_ep->dir_in) {
3920 unsigned short fifo_index;
3921
3922 if (hsotg->dedicated_fifos || hs_ep->periodic)
3923 fifo_index = hs_ep->fifo_index;
3924 else
3925 fifo_index = 0;
3926
3927 /* Flush TX FIFO */
3928 dwc2_flush_tx_fifo(hsotg, fifo_index);
3929
3930 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3931 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3932 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
3933
3934 } else {
3935 /* Remove global NAKs */
3936 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
3937 }
3938}
3939
Googleraf606d22022-10-26 21:40:12 -07003940/**
3941 * dwc2_hsotg_ep_enable - enable the given endpoint
3942 * @ep: The USB endpint to configure
3943 * @desc: The USB endpoint descriptor to configure with.
3944 *
3945 * This is called from the USB gadget code's usb_ep_enable().
3946 */
3947static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Googler9398cc32022-12-02 17:21:52 +08003948 const struct usb_endpoint_descriptor *desc)
Googleraf606d22022-10-26 21:40:12 -07003949{
3950 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
3951 struct dwc2_hsotg *hsotg = hs_ep->parent;
3952 unsigned long flags;
3953 unsigned int index = hs_ep->index;
3954 u32 epctrl_reg;
3955 u32 epctrl;
3956 u32 mps;
Googler9398cc32022-12-02 17:21:52 +08003957 u32 mc;
Googleraf606d22022-10-26 21:40:12 -07003958 u32 mask;
3959 unsigned int dir_in;
3960 unsigned int i, val, size;
3961 int ret = 0;
Googler9398cc32022-12-02 17:21:52 +08003962 unsigned char ep_type;
3963 int desc_num;
Googleraf606d22022-10-26 21:40:12 -07003964
3965 dev_dbg(hsotg->dev,
3966 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3967 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3968 desc->wMaxPacketSize, desc->bInterval);
3969
3970 /* not to be called for EP0 */
3971 if (index == 0) {
3972 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3973 return -EINVAL;
3974 }
3975
3976 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3977 if (dir_in != hs_ep->dir_in) {
3978 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3979 return -EINVAL;
3980 }
3981
Googler9398cc32022-12-02 17:21:52 +08003982 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Googleraf606d22022-10-26 21:40:12 -07003983 mps = usb_endpoint_maxp(desc);
Googler9398cc32022-12-02 17:21:52 +08003984 mc = usb_endpoint_maxp_mult(desc);
3985
3986 /* ISOC IN in DDMA supported bInterval up to 10 */
3987 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3988 dir_in && desc->bInterval > 10) {
3989 dev_err(hsotg->dev,
3990 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3991 return -EINVAL;
3992 }
3993
3994 /* High bandwidth ISOC OUT in DDMA not supported */
3995 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3996 !dir_in && mc > 1) {
3997 dev_err(hsotg->dev,
3998 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3999 return -EINVAL;
4000 }
Googleraf606d22022-10-26 21:40:12 -07004001
4002 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
4003
4004 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Googler9398cc32022-12-02 17:21:52 +08004005 epctrl = dwc2_readl(hsotg, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07004006
4007 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
4008 __func__, epctrl, epctrl_reg);
4009
Googler9398cc32022-12-02 17:21:52 +08004010 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC)
4011 desc_num = MAX_DMA_DESC_NUM_HS_ISOC;
4012 else
4013 desc_num = MAX_DMA_DESC_NUM_GENERIC;
4014
4015 /* Allocate DMA descriptor chain for non-ctrl endpoints */
4016 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
4017 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
4018 desc_num * sizeof(struct dwc2_dma_desc),
4019 &hs_ep->desc_list_dma, GFP_ATOMIC);
4020 if (!hs_ep->desc_list) {
4021 ret = -ENOMEM;
4022 goto error2;
4023 }
4024 }
4025
Googleraf606d22022-10-26 21:40:12 -07004026 spin_lock_irqsave(&hsotg->lock, flags);
4027
4028 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
4029 epctrl |= DXEPCTL_MPS(mps);
4030
4031 /*
4032 * mark the endpoint as active, otherwise the core may ignore
4033 * transactions entirely for this endpoint
4034 */
4035 epctrl |= DXEPCTL_USBACTEP;
4036
4037 /* update the endpoint state */
Googler9398cc32022-12-02 17:21:52 +08004038 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Googleraf606d22022-10-26 21:40:12 -07004039
4040 /* default, set to non-periodic */
4041 hs_ep->isochronous = 0;
4042 hs_ep->periodic = 0;
4043 hs_ep->halted = 0;
4044 hs_ep->interval = desc->bInterval;
4045
Googler9398cc32022-12-02 17:21:52 +08004046 switch (ep_type) {
Googleraf606d22022-10-26 21:40:12 -07004047 case USB_ENDPOINT_XFER_ISOC:
4048 epctrl |= DXEPCTL_EPTYPE_ISO;
4049 epctrl |= DXEPCTL_SETEVENFR;
4050 hs_ep->isochronous = 1;
4051 hs_ep->interval = 1 << (desc->bInterval - 1);
4052 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Googler9398cc32022-12-02 17:21:52 +08004053 hs_ep->next_desc = 0;
4054 hs_ep->compl_desc = 0;
Googleraf606d22022-10-26 21:40:12 -07004055 if (dir_in) {
4056 hs_ep->periodic = 1;
Googler9398cc32022-12-02 17:21:52 +08004057 mask = dwc2_readl(hsotg, DIEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004058 mask |= DIEPMSK_NAKMSK;
Googler9398cc32022-12-02 17:21:52 +08004059 dwc2_writel(hsotg, mask, DIEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004060 } else {
Googler9398cc32022-12-02 17:21:52 +08004061 mask = dwc2_readl(hsotg, DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004062 mask |= DOEPMSK_OUTTKNEPDISMSK;
Googler9398cc32022-12-02 17:21:52 +08004063 dwc2_writel(hsotg, mask, DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004064 }
4065 break;
4066
4067 case USB_ENDPOINT_XFER_BULK:
4068 epctrl |= DXEPCTL_EPTYPE_BULK;
4069 break;
4070
4071 case USB_ENDPOINT_XFER_INT:
4072 if (dir_in)
4073 hs_ep->periodic = 1;
4074
4075 if (hsotg->gadget.speed == USB_SPEED_HIGH)
4076 hs_ep->interval = 1 << (desc->bInterval - 1);
4077
4078 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
4079 break;
4080
4081 case USB_ENDPOINT_XFER_CONTROL:
4082 epctrl |= DXEPCTL_EPTYPE_CONTROL;
4083 break;
4084 }
4085
4086 /*
4087 * if the hardware has dedicated fifos, we must give each IN EP
4088 * a unique tx-fifo even if it is non-periodic.
4089 */
4090 if (dir_in && hsotg->dedicated_fifos) {
Googler9398cc32022-12-02 17:21:52 +08004091 unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
Googleraf606d22022-10-26 21:40:12 -07004092 u32 fifo_index = 0;
4093 u32 fifo_size = UINT_MAX;
Googler9398cc32022-12-02 17:21:52 +08004094
4095 size = hs_ep->ep.maxpacket * hs_ep->mc;
4096 for (i = 1; i <= fifo_count; ++i) {
4097 if (hsotg->fifo_map & (1 << i))
Googleraf606d22022-10-26 21:40:12 -07004098 continue;
Googler9398cc32022-12-02 17:21:52 +08004099 val = dwc2_readl(hsotg, DPTXFSIZN(i));
4100 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Googleraf606d22022-10-26 21:40:12 -07004101 if (val < size)
4102 continue;
4103 /* Search for smallest acceptable fifo */
4104 if (val < fifo_size) {
4105 fifo_size = val;
4106 fifo_index = i;
4107 }
4108 }
4109 if (!fifo_index) {
4110 dev_err(hsotg->dev,
4111 "%s: No suitable fifo found\n", __func__);
4112 ret = -ENOMEM;
Googler9398cc32022-12-02 17:21:52 +08004113 goto error1;
Googleraf606d22022-10-26 21:40:12 -07004114 }
Googler9398cc32022-12-02 17:21:52 +08004115 epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT);
Googleraf606d22022-10-26 21:40:12 -07004116 hsotg->fifo_map |= 1 << fifo_index;
4117 epctrl |= DXEPCTL_TXFNUM(fifo_index);
4118 hs_ep->fifo_index = fifo_index;
4119 hs_ep->fifo_size = fifo_size;
4120 }
4121
4122 /* for non control endpoints, set PID to D0 */
4123 if (index && !hs_ep->isochronous)
4124 epctrl |= DXEPCTL_SETD0PID;
4125
Googler9398cc32022-12-02 17:21:52 +08004126 /* WA for Full speed ISOC IN in DDMA mode.
4127 * By Clear NAK status of EP, core will send ZLP
4128 * to IN token and assert NAK interrupt relying
4129 * on TxFIFO status only
4130 */
4131
4132 if (hsotg->gadget.speed == USB_SPEED_FULL &&
4133 hs_ep->isochronous && dir_in) {
4134 /* The WA applies only to core versions from 2.72a
4135 * to 4.00a (including both). Also for FS_IOT_1.00a
4136 * and HS_IOT_1.00a.
4137 */
4138 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
4139
4140 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
4141 gsnpsid <= DWC2_CORE_REV_4_00a) ||
4142 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
4143 gsnpsid == DWC2_HS_IOT_REV_1_00a)
4144 epctrl |= DXEPCTL_CNAK;
4145 }
4146
Googleraf606d22022-10-26 21:40:12 -07004147 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4148 __func__, epctrl);
4149
Googler9398cc32022-12-02 17:21:52 +08004150 dwc2_writel(hsotg, epctrl, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07004151 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004152 __func__, dwc2_readl(hsotg, epctrl_reg));
Googleraf606d22022-10-26 21:40:12 -07004153
4154 /* enable the endpoint interrupt */
4155 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
4156
Googler9398cc32022-12-02 17:21:52 +08004157error1:
Googleraf606d22022-10-26 21:40:12 -07004158 spin_unlock_irqrestore(&hsotg->lock, flags);
Googler9398cc32022-12-02 17:21:52 +08004159
4160error2:
4161 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
4162 dmam_free_coherent(hsotg->dev, desc_num *
4163 sizeof(struct dwc2_dma_desc),
4164 hs_ep->desc_list, hs_ep->desc_list_dma);
4165 hs_ep->desc_list = NULL;
4166 }
4167
Googleraf606d22022-10-26 21:40:12 -07004168 return ret;
4169}
4170
4171/**
4172 * dwc2_hsotg_ep_disable - disable given endpoint
4173 * @ep: The endpoint to disable.
4174 */
4175static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
4176{
4177 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4178 struct dwc2_hsotg *hsotg = hs_ep->parent;
4179 int dir_in = hs_ep->dir_in;
4180 int index = hs_ep->index;
Googleraf606d22022-10-26 21:40:12 -07004181 u32 epctrl_reg;
4182 u32 ctrl;
4183
4184 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
4185
4186 if (ep == &hsotg->eps_out[0]->ep) {
4187 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4188 return -EINVAL;
4189 }
4190
Googler9398cc32022-12-02 17:21:52 +08004191 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4192 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4193 return -EINVAL;
4194 }
4195
Googleraf606d22022-10-26 21:40:12 -07004196 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
4197
Googler9398cc32022-12-02 17:21:52 +08004198 ctrl = dwc2_readl(hsotg, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07004199
Googler9398cc32022-12-02 17:21:52 +08004200 if (ctrl & DXEPCTL_EPENA)
4201 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4202
Googleraf606d22022-10-26 21:40:12 -07004203 ctrl &= ~DXEPCTL_EPENA;
4204 ctrl &= ~DXEPCTL_USBACTEP;
4205 ctrl |= DXEPCTL_SNAK;
4206
4207 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Googler9398cc32022-12-02 17:21:52 +08004208 dwc2_writel(hsotg, ctrl, epctrl_reg);
Googleraf606d22022-10-26 21:40:12 -07004209
4210 /* disable endpoint interrupts */
4211 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
4212
4213 /* terminate all requests with shutdown */
4214 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4215
4216 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4217 hs_ep->fifo_index = 0;
4218 hs_ep->fifo_size = 0;
4219
Googler9726be62022-12-14 05:53:31 +00004220 return 0;
Googler0109c452022-10-13 17:50:39 +08004221}
4222
Googler9398cc32022-12-02 17:21:52 +08004223static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
4224{
4225 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4226 struct dwc2_hsotg *hsotg = hs_ep->parent;
4227 unsigned long flags;
4228 int ret;
4229
4230 spin_lock_irqsave(&hsotg->lock, flags);
4231 ret = dwc2_hsotg_ep_disable(ep);
4232 spin_unlock_irqrestore(&hsotg->lock, flags);
4233 return ret;
4234}
4235
Googleraf606d22022-10-26 21:40:12 -07004236/**
4237 * on_list - check request is on the given endpoint
4238 * @ep: The endpoint to check.
4239 * @test: The request to test if it is on the endpoint.
4240 */
4241static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
4242{
4243 struct dwc2_hsotg_req *req, *treq;
4244
4245 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4246 if (req == test)
4247 return true;
4248 }
4249
4250 return false;
4251}
4252
Googleraf606d22022-10-26 21:40:12 -07004253/**
4254 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
4255 * @ep: The endpoint to dequeue.
4256 * @req: The request to be removed from a queue.
4257 */
4258static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
4259{
4260 struct dwc2_hsotg_req *hs_req = our_req(req);
4261 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4262 struct dwc2_hsotg *hs = hs_ep->parent;
4263 unsigned long flags;
4264
4265 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
4266
4267 spin_lock_irqsave(&hs->lock, flags);
4268
4269 if (!on_list(hs_ep, hs_req)) {
4270 spin_unlock_irqrestore(&hs->lock, flags);
4271 return -EINVAL;
4272 }
4273
4274 /* Dequeue already started request */
4275 if (req == &hs_ep->req->req)
4276 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4277
4278 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
4279 spin_unlock_irqrestore(&hs->lock, flags);
4280
4281 return 0;
4282}
4283
4284/**
4285 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
4286 * @ep: The endpoint to set halt.
4287 * @value: Set or unset the halt.
4288 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4289 * the endpoint is busy processing requests.
4290 *
4291 * We need to stall the endpoint immediately if request comes from set_feature
4292 * protocol command handler.
4293 */
4294static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
4295{
4296 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4297 struct dwc2_hsotg *hs = hs_ep->parent;
4298 int index = hs_ep->index;
4299 u32 epreg;
4300 u32 epctl;
4301 u32 xfertype;
4302
4303 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4304
4305 if (index == 0) {
4306 if (value)
4307 dwc2_hsotg_stall_ep0(hs);
4308 else
4309 dev_warn(hs->dev,
4310 "%s: can't clear halt on ep0\n", __func__);
4311 return 0;
4312 }
4313
4314 if (hs_ep->isochronous) {
4315 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4316 return -EINVAL;
4317 }
4318
4319 if (!now && value && !list_empty(&hs_ep->queue)) {
4320 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4321 ep->name);
4322 return -EAGAIN;
4323 }
4324
4325 if (hs_ep->dir_in) {
4326 epreg = DIEPCTL(index);
Googler9398cc32022-12-02 17:21:52 +08004327 epctl = dwc2_readl(hs, epreg);
Googleraf606d22022-10-26 21:40:12 -07004328
4329 if (value) {
4330 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
4331 if (epctl & DXEPCTL_EPENA)
4332 epctl |= DXEPCTL_EPDIS;
4333 } else {
4334 epctl &= ~DXEPCTL_STALL;
4335 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4336 if (xfertype == DXEPCTL_EPTYPE_BULK ||
Googler9398cc32022-12-02 17:21:52 +08004337 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4338 epctl |= DXEPCTL_SETD0PID;
Googleraf606d22022-10-26 21:40:12 -07004339 }
Googler9398cc32022-12-02 17:21:52 +08004340 dwc2_writel(hs, epctl, epreg);
Googleraf606d22022-10-26 21:40:12 -07004341 } else {
Googler9726be62022-12-14 05:53:31 +00004342 epreg = DOEPCTL(index);
Googler9398cc32022-12-02 17:21:52 +08004343 epctl = dwc2_readl(hs, epreg);
Googler9726be62022-12-14 05:53:31 +00004344
Googler9398cc32022-12-02 17:21:52 +08004345 if (value) {
Googleraf606d22022-10-26 21:40:12 -07004346 epctl |= DXEPCTL_STALL;
Googler9398cc32022-12-02 17:21:52 +08004347 } else {
Googleraf606d22022-10-26 21:40:12 -07004348 epctl &= ~DXEPCTL_STALL;
4349 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4350 if (xfertype == DXEPCTL_EPTYPE_BULK ||
Googler9398cc32022-12-02 17:21:52 +08004351 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4352 epctl |= DXEPCTL_SETD0PID;
Googleraf606d22022-10-26 21:40:12 -07004353 }
Googler9398cc32022-12-02 17:21:52 +08004354 dwc2_writel(hs, epctl, epreg);
Googleraf606d22022-10-26 21:40:12 -07004355 }
4356
4357 hs_ep->halted = value;
4358
4359 return 0;
4360}
4361
4362/**
4363 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
4364 * @ep: The endpoint to set halt.
4365 * @value: Set or unset the halt.
4366 */
4367static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
4368{
4369 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4370 struct dwc2_hsotg *hs = hs_ep->parent;
4371 unsigned long flags = 0;
4372 int ret = 0;
4373
4374 spin_lock_irqsave(&hs->lock, flags);
4375 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
4376 spin_unlock_irqrestore(&hs->lock, flags);
4377
4378 return ret;
4379}
4380
Googler9398cc32022-12-02 17:21:52 +08004381static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Googleraf606d22022-10-26 21:40:12 -07004382 .enable = dwc2_hsotg_ep_enable,
Googler9398cc32022-12-02 17:21:52 +08004383 .disable = dwc2_hsotg_ep_disable_lock,
Googleraf606d22022-10-26 21:40:12 -07004384 .alloc_request = dwc2_hsotg_ep_alloc_request,
4385 .free_request = dwc2_hsotg_ep_free_request,
4386 .queue = dwc2_hsotg_ep_queue_lock,
4387 .dequeue = dwc2_hsotg_ep_dequeue,
4388 .set_halt = dwc2_hsotg_ep_sethalt_lock,
4389 /* note, don't believe we have any call for the fifo routines */
4390};
4391
4392/**
Googler9398cc32022-12-02 17:21:52 +08004393 * dwc2_hsotg_init - initialize the usb core
Googleraf606d22022-10-26 21:40:12 -07004394 * @hsotg: The driver state
4395 */
4396static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
4397{
Googleraf606d22022-10-26 21:40:12 -07004398 /* unmask subset of endpoint interrupts */
4399
Googler9398cc32022-12-02 17:21:52 +08004400 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Googleraf606d22022-10-26 21:40:12 -07004401 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Googler9398cc32022-12-02 17:21:52 +08004402 DIEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004403
Googler9398cc32022-12-02 17:21:52 +08004404 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Googleraf606d22022-10-26 21:40:12 -07004405 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Googler9398cc32022-12-02 17:21:52 +08004406 DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07004407
Googler9398cc32022-12-02 17:21:52 +08004408 dwc2_writel(hsotg, 0, DAINTMSK);
Googleraf606d22022-10-26 21:40:12 -07004409
4410 /* Be in disconnected state until gadget is registered */
Googler9398cc32022-12-02 17:21:52 +08004411 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Googleraf606d22022-10-26 21:40:12 -07004412
4413 /* setup fifos */
4414
4415 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004416 dwc2_readl(hsotg, GRXFSIZ),
4417 dwc2_readl(hsotg, GNPTXFSIZ));
Googleraf606d22022-10-26 21:40:12 -07004418
4419 dwc2_hsotg_init_fifo(hsotg);
4420
Googleraf606d22022-10-26 21:40:12 -07004421 if (using_dma(hsotg))
Googler9398cc32022-12-02 17:21:52 +08004422 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Googleraf606d22022-10-26 21:40:12 -07004423}
4424
4425/**
4426 * dwc2_hsotg_udc_start - prepare the udc for work
4427 * @gadget: The usb gadget state
4428 * @driver: The usb gadget driver
4429 *
4430 * Perform initialization to prepare udc device and driver
4431 * to work.
4432 */
4433static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Googler9398cc32022-12-02 17:21:52 +08004434 struct usb_gadget_driver *driver)
Googleraf606d22022-10-26 21:40:12 -07004435{
4436 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4437 unsigned long flags;
4438 int ret;
4439
4440 if (!hsotg) {
4441 pr_err("%s: called with no device\n", __func__);
4442 return -ENODEV;
4443 }
4444
4445 if (!driver) {
4446 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4447 return -EINVAL;
4448 }
4449
4450 if (driver->max_speed < USB_SPEED_FULL)
4451 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
4452
4453 if (!driver->setup) {
4454 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4455 return -EINVAL;
4456 }
4457
4458 WARN_ON(hsotg->driver);
4459
Googlerb48fa912023-03-17 12:40:29 +05304460 driver->driver.bus = NULL;
Googleraf606d22022-10-26 21:40:12 -07004461 hsotg->driver = driver;
4462 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
4463 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4464
4465 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4466 ret = dwc2_lowlevel_hw_enable(hsotg);
4467 if (ret)
4468 goto err;
4469 }
4470
4471 if (!IS_ERR_OR_NULL(hsotg->uphy))
4472 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
4473
4474 spin_lock_irqsave(&hsotg->lock, flags);
4475 if (dwc2_hw_is_device(hsotg)) {
4476 dwc2_hsotg_init(hsotg);
4477 dwc2_hsotg_core_init_disconnected(hsotg, false);
4478 }
4479
4480 hsotg->enabled = 0;
4481 spin_unlock_irqrestore(&hsotg->lock, flags);
4482
Googler9398cc32022-12-02 17:21:52 +08004483 gadget->sg_supported = using_desc_dma(hsotg);
Googleraf606d22022-10-26 21:40:12 -07004484 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
4485
4486 return 0;
4487
4488err:
4489 hsotg->driver = NULL;
4490 return ret;
4491}
4492
4493/**
4494 * dwc2_hsotg_udc_stop - stop the udc
4495 * @gadget: The usb gadget state
Googleraf606d22022-10-26 21:40:12 -07004496 *
4497 * Stop udc hw block and stay tunned for future transmissions
4498 */
4499static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
4500{
4501 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4502 unsigned long flags = 0;
4503 int ep;
4504
4505 if (!hsotg)
4506 return -ENODEV;
4507
4508 /* all endpoints should be shutdown */
4509 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4510 if (hsotg->eps_in[ep])
Googler9398cc32022-12-02 17:21:52 +08004511 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Googleraf606d22022-10-26 21:40:12 -07004512 if (hsotg->eps_out[ep])
Googler9398cc32022-12-02 17:21:52 +08004513 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Googleraf606d22022-10-26 21:40:12 -07004514 }
4515
4516 spin_lock_irqsave(&hsotg->lock, flags);
4517
4518 hsotg->driver = NULL;
4519 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4520 hsotg->enabled = 0;
4521
4522 spin_unlock_irqrestore(&hsotg->lock, flags);
4523
4524 if (!IS_ERR_OR_NULL(hsotg->uphy))
4525 otg_set_peripheral(hsotg->uphy->otg, NULL);
4526
4527 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4528 dwc2_lowlevel_hw_disable(hsotg);
4529
4530 return 0;
4531}
4532
4533/**
4534 * dwc2_hsotg_gadget_getframe - read the frame number
4535 * @gadget: The usb gadget state
4536 *
4537 * Read the {micro} frame number
4538 */
4539static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
4540{
4541 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
4542}
4543
4544/**
4545 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
4546 * @gadget: The usb gadget state
4547 * @is_on: Current state of the USB PHY
4548 *
4549 * Connect/Disconnect the USB PHY pullup
4550 */
4551static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
4552{
4553 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4554 unsigned long flags = 0;
4555
4556 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
Googler9398cc32022-12-02 17:21:52 +08004557 hsotg->op_state);
Googleraf606d22022-10-26 21:40:12 -07004558
4559 /* Don't modify pullup state while in host mode */
4560 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4561 hsotg->enabled = is_on;
4562 return 0;
4563 }
4564
4565 spin_lock_irqsave(&hsotg->lock, flags);
4566 if (is_on) {
4567 hsotg->enabled = 1;
4568 dwc2_hsotg_core_init_disconnected(hsotg, false);
Googler9398cc32022-12-02 17:21:52 +08004569 /* Enable ACG feature in device mode,if supported */
4570 dwc2_enable_acg(hsotg);
Googleraf606d22022-10-26 21:40:12 -07004571 dwc2_hsotg_core_connect(hsotg);
4572 } else {
4573 dwc2_hsotg_core_disconnect(hsotg);
4574 dwc2_hsotg_disconnect(hsotg);
4575 hsotg->enabled = 0;
4576 }
4577
4578 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4579 spin_unlock_irqrestore(&hsotg->lock, flags);
4580
4581 return 0;
4582}
4583
4584static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
4585{
4586 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4587 unsigned long flags;
4588
4589 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4590 spin_lock_irqsave(&hsotg->lock, flags);
4591
4592 /*
Googler9398cc32022-12-02 17:21:52 +08004593 * If controller is hibernated, it must exit from power_down
Googleraf606d22022-10-26 21:40:12 -07004594 * before being initialized / de-initialized
4595 */
4596 if (hsotg->lx_state == DWC2_L2)
Googler9398cc32022-12-02 17:21:52 +08004597 dwc2_exit_partial_power_down(hsotg, false);
Googleraf606d22022-10-26 21:40:12 -07004598
4599 if (is_active) {
4600 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
4601
4602 dwc2_hsotg_core_init_disconnected(hsotg, false);
Googler9398cc32022-12-02 17:21:52 +08004603 if (hsotg->enabled) {
4604 /* Enable ACG feature in device mode,if supported */
4605 dwc2_enable_acg(hsotg);
Googleraf606d22022-10-26 21:40:12 -07004606 dwc2_hsotg_core_connect(hsotg);
Googler9398cc32022-12-02 17:21:52 +08004607 }
Googleraf606d22022-10-26 21:40:12 -07004608 } else {
4609 dwc2_hsotg_core_disconnect(hsotg);
4610 dwc2_hsotg_disconnect(hsotg);
4611 }
4612
4613 spin_unlock_irqrestore(&hsotg->lock, flags);
4614 return 0;
4615}
4616
4617/**
4618 * dwc2_hsotg_vbus_draw - report bMaxPower field
4619 * @gadget: The usb gadget state
4620 * @mA: Amount of current
4621 *
4622 * Report how much power the device may consume to the phy.
4623 */
Googler9398cc32022-12-02 17:21:52 +08004624static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Googleraf606d22022-10-26 21:40:12 -07004625{
4626 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4627
4628 if (IS_ERR_OR_NULL(hsotg->uphy))
4629 return -ENOTSUPP;
4630 return usb_phy_set_power(hsotg->uphy, mA);
4631}
4632
4633static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4634 .get_frame = dwc2_hsotg_gadget_getframe,
4635 .udc_start = dwc2_hsotg_udc_start,
4636 .udc_stop = dwc2_hsotg_udc_stop,
4637 .pullup = dwc2_hsotg_pullup,
4638 .vbus_session = dwc2_hsotg_vbus_session,
4639 .vbus_draw = dwc2_hsotg_vbus_draw,
4640};
4641
4642/**
4643 * dwc2_hsotg_initep - initialise a single endpoint
4644 * @hsotg: The device state.
4645 * @hs_ep: The endpoint to be initialised.
4646 * @epnum: The endpoint number
Googler9398cc32022-12-02 17:21:52 +08004647 * @dir_in: True if direction is in.
Googleraf606d22022-10-26 21:40:12 -07004648 *
4649 * Initialise the given endpoint (as part of the probe and device state
4650 * creation) to give to the gadget driver. Setup the endpoint name, any
4651 * direction information and other state that may be required.
4652 */
4653static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
Googler9398cc32022-12-02 17:21:52 +08004654 struct dwc2_hsotg_ep *hs_ep,
Googleraf606d22022-10-26 21:40:12 -07004655 int epnum,
4656 bool dir_in)
4657{
4658 char *dir;
4659
4660 if (epnum == 0)
4661 dir = "";
4662 else if (dir_in)
4663 dir = "in";
4664 else
4665 dir = "out";
4666
4667 hs_ep->dir_in = dir_in;
4668 hs_ep->index = epnum;
4669
4670 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4671
4672 INIT_LIST_HEAD(&hs_ep->queue);
4673 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4674
4675 /* add to the list of endpoints known by the gadget driver */
4676 if (epnum)
4677 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4678
4679 hs_ep->parent = hsotg;
4680 hs_ep->ep.name = hs_ep->name;
Googler9398cc32022-12-02 17:21:52 +08004681
4682 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4683 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4684 else
4685 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4686 epnum ? 1024 : EP0_MPS_LIMIT);
Googleraf606d22022-10-26 21:40:12 -07004687 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
4688
4689 if (epnum == 0) {
4690 hs_ep->ep.caps.type_control = true;
4691 } else {
Googler9398cc32022-12-02 17:21:52 +08004692 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4693 hs_ep->ep.caps.type_iso = true;
4694 hs_ep->ep.caps.type_bulk = true;
4695 }
Googleraf606d22022-10-26 21:40:12 -07004696 hs_ep->ep.caps.type_int = true;
4697 }
4698
4699 if (dir_in)
4700 hs_ep->ep.caps.dir_in = true;
4701 else
4702 hs_ep->ep.caps.dir_out = true;
4703
4704 /*
4705 * if we're using dma, we need to set the next-endpoint pointer
4706 * to be something valid.
4707 */
4708
4709 if (using_dma(hsotg)) {
4710 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Googler9398cc32022-12-02 17:21:52 +08004711
Googleraf606d22022-10-26 21:40:12 -07004712 if (dir_in)
Googler9398cc32022-12-02 17:21:52 +08004713 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Googleraf606d22022-10-26 21:40:12 -07004714 else
Googler9398cc32022-12-02 17:21:52 +08004715 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Googleraf606d22022-10-26 21:40:12 -07004716 }
4717}
4718
4719/**
4720 * dwc2_hsotg_hw_cfg - read HW configuration registers
Googler9398cc32022-12-02 17:21:52 +08004721 * @hsotg: Programming view of the DWC_otg controller
Googleraf606d22022-10-26 21:40:12 -07004722 *
4723 * Read the USB core HW configuration registers
4724 */
4725static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
4726{
4727 u32 cfg;
4728 u32 ep_type;
4729 u32 i;
4730
4731 /* check hardware configuration */
4732
4733 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4734
4735 /* Add ep0 */
4736 hsotg->num_of_eps++;
4737
Googler9398cc32022-12-02 17:21:52 +08004738 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4739 sizeof(struct dwc2_hsotg_ep),
4740 GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07004741 if (!hsotg->eps_in[0])
4742 return -ENOMEM;
4743 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
4744 hsotg->eps_out[0] = hsotg->eps_in[0];
4745
4746 cfg = hsotg->hw_params.dev_ep_dirs;
4747 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
4748 ep_type = cfg & 3;
4749 /* Direction in or both */
4750 if (!(ep_type & 2)) {
4751 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
4752 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
4753 if (!hsotg->eps_in[i])
4754 return -ENOMEM;
4755 }
4756 /* Direction out or both */
4757 if (!(ep_type & 1)) {
4758 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
4759 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
4760 if (!hsotg->eps_out[i])
4761 return -ENOMEM;
4762 }
4763 }
4764
4765 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4766 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
4767
4768 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4769 hsotg->num_of_eps,
4770 hsotg->dedicated_fifos ? "dedicated" : "shared",
4771 hsotg->fifo_mem);
4772 return 0;
4773}
4774
4775/**
4776 * dwc2_hsotg_dump - dump state of the udc
Googler9398cc32022-12-02 17:21:52 +08004777 * @hsotg: Programming view of the DWC_otg controller
4778 *
Googleraf606d22022-10-26 21:40:12 -07004779 */
4780static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
4781{
4782#ifdef DEBUG
4783 struct device *dev = hsotg->dev;
Googleraf606d22022-10-26 21:40:12 -07004784 u32 val;
4785 int idx;
4786
4787 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004788 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4789 dwc2_readl(hsotg, DIEPMSK));
Googleraf606d22022-10-26 21:40:12 -07004790
4791 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004792 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Googleraf606d22022-10-26 21:40:12 -07004793
4794 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004795 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Googleraf606d22022-10-26 21:40:12 -07004796
4797 /* show periodic fifo settings */
4798
4799 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Googler9398cc32022-12-02 17:21:52 +08004800 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Googleraf606d22022-10-26 21:40:12 -07004801 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
4802 val >> FIFOSIZE_DEPTH_SHIFT,
4803 val & FIFOSIZE_STARTADDR_MASK);
4804 }
4805
4806 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
4807 dev_info(dev,
4808 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Googler9398cc32022-12-02 17:21:52 +08004809 dwc2_readl(hsotg, DIEPCTL(idx)),
4810 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4811 dwc2_readl(hsotg, DIEPDMA(idx)));
Googleraf606d22022-10-26 21:40:12 -07004812
Googler9398cc32022-12-02 17:21:52 +08004813 val = dwc2_readl(hsotg, DOEPCTL(idx));
Googleraf606d22022-10-26 21:40:12 -07004814 dev_info(dev,
4815 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004816 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4817 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4818 dwc2_readl(hsotg, DOEPDMA(idx)));
Googleraf606d22022-10-26 21:40:12 -07004819 }
4820
4821 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Googler9398cc32022-12-02 17:21:52 +08004822 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Googleraf606d22022-10-26 21:40:12 -07004823#endif
4824}
4825
Googleraf606d22022-10-26 21:40:12 -07004826/**
4827 * dwc2_gadget_init - init function for gadget
Googler9398cc32022-12-02 17:21:52 +08004828 * @hsotg: Programming view of the DWC_otg controller
4829 *
Googleraf606d22022-10-26 21:40:12 -07004830 */
Googler9398cc32022-12-02 17:21:52 +08004831int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Googleraf606d22022-10-26 21:40:12 -07004832{
4833 struct device *dev = hsotg->dev;
4834 int epnum;
4835 int ret;
Googleraf606d22022-10-26 21:40:12 -07004836
4837 /* Dump fifo information */
4838 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
Googler9398cc32022-12-02 17:21:52 +08004839 hsotg->params.g_np_tx_fifo_size);
4840 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Googleraf606d22022-10-26 21:40:12 -07004841
4842 hsotg->gadget.max_speed = USB_SPEED_HIGH;
4843 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
4844 hsotg->gadget.name = dev_name(dev);
Googler9398cc32022-12-02 17:21:52 +08004845 hsotg->remote_wakeup_allowed = 0;
4846
4847 if (hsotg->params.lpm)
4848 hsotg->gadget.lpm_capable = true;
4849
Googleraf606d22022-10-26 21:40:12 -07004850 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4851 hsotg->gadget.is_otg = 1;
4852 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4853 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
4854
4855 ret = dwc2_hsotg_hw_cfg(hsotg);
4856 if (ret) {
4857 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
4858 return ret;
4859 }
4860
4861 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4862 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
4863 if (!hsotg->ctrl_buff)
4864 return -ENOMEM;
4865
4866 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4867 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
4868 if (!hsotg->ep0_buff)
4869 return -ENOMEM;
4870
Googler9398cc32022-12-02 17:21:52 +08004871 if (using_desc_dma(hsotg)) {
4872 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4873 if (ret < 0)
4874 return ret;
4875 }
4876
4877 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4878 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Googleraf606d22022-10-26 21:40:12 -07004879 if (ret < 0) {
4880 dev_err(dev, "cannot claim IRQ for gadget\n");
4881 return ret;
4882 }
4883
4884 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4885
4886 if (hsotg->num_of_eps == 0) {
4887 dev_err(dev, "wrong number of EPs (zero)\n");
4888 return -EINVAL;
4889 }
4890
4891 /* setup endpoint information */
4892
4893 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
4894 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
4895
4896 /* allocate EP0 request */
4897
4898 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
4899 GFP_KERNEL);
4900 if (!hsotg->ctrl_req) {
4901 dev_err(dev, "failed to allocate ctrl req\n");
4902 return -ENOMEM;
4903 }
4904
4905 /* initialise the endpoints now the core has been initialised */
4906 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4907 if (hsotg->eps_in[epnum])
4908 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Googler9398cc32022-12-02 17:21:52 +08004909 epnum, 1);
Googleraf606d22022-10-26 21:40:12 -07004910 if (hsotg->eps_out[epnum])
4911 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Googler9398cc32022-12-02 17:21:52 +08004912 epnum, 0);
Googleraf606d22022-10-26 21:40:12 -07004913 }
4914
4915 dwc2_hsotg_dump(hsotg);
4916
4917 return 0;
4918}
4919
4920/**
4921 * dwc2_hsotg_remove - remove function for hsotg driver
Googler9398cc32022-12-02 17:21:52 +08004922 * @hsotg: Programming view of the DWC_otg controller
4923 *
Googleraf606d22022-10-26 21:40:12 -07004924 */
4925int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
4926{
4927 usb_del_gadget_udc(&hsotg->gadget);
4928 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
4929
4930 return 0;
4931}
4932
4933int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
4934{
4935 unsigned long flags;
4936
4937 if (hsotg->lx_state != DWC2_L0)
4938 return 0;
4939
4940 if (hsotg->driver) {
4941 int ep;
4942
4943 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4944 hsotg->driver->driver.name);
4945
4946 spin_lock_irqsave(&hsotg->lock, flags);
4947 if (hsotg->enabled)
4948 dwc2_hsotg_core_disconnect(hsotg);
4949 dwc2_hsotg_disconnect(hsotg);
4950 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4951 spin_unlock_irqrestore(&hsotg->lock, flags);
4952
Googlerb48fa912023-03-17 12:40:29 +05304953 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
Googleraf606d22022-10-26 21:40:12 -07004954 if (hsotg->eps_in[ep])
Googler9398cc32022-12-02 17:21:52 +08004955 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Googleraf606d22022-10-26 21:40:12 -07004956 if (hsotg->eps_out[ep])
Googler9398cc32022-12-02 17:21:52 +08004957 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Googleraf606d22022-10-26 21:40:12 -07004958 }
4959 }
4960
4961 return 0;
4962}
4963
4964int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
4965{
4966 unsigned long flags;
4967
4968 if (hsotg->lx_state == DWC2_L2)
4969 return 0;
4970
4971 if (hsotg->driver) {
4972 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4973 hsotg->driver->driver.name);
4974
4975 spin_lock_irqsave(&hsotg->lock, flags);
4976 dwc2_hsotg_core_init_disconnected(hsotg, false);
Googler9398cc32022-12-02 17:21:52 +08004977 if (hsotg->enabled) {
4978 /* Enable ACG feature in device mode,if supported */
4979 dwc2_enable_acg(hsotg);
Googleraf606d22022-10-26 21:40:12 -07004980 dwc2_hsotg_core_connect(hsotg);
Googler9398cc32022-12-02 17:21:52 +08004981 }
Googleraf606d22022-10-26 21:40:12 -07004982 spin_unlock_irqrestore(&hsotg->lock, flags);
4983 }
4984
4985 return 0;
4986}
4987
4988/**
4989 * dwc2_backup_device_registers() - Backup controller device registers.
4990 * When suspending usb bus, registers needs to be backuped
4991 * if controller power is disabled once suspended.
4992 *
4993 * @hsotg: Programming view of the DWC_otg controller
4994 */
4995int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4996{
4997 struct dwc2_dregs_backup *dr;
4998 int i;
4999
5000 dev_dbg(hsotg->dev, "%s\n", __func__);
5001
5002 /* Backup dev regs */
5003 dr = &hsotg->dr_backup;
5004
Googler9398cc32022-12-02 17:21:52 +08005005 dr->dcfg = dwc2_readl(hsotg, DCFG);
5006 dr->dctl = dwc2_readl(hsotg, DCTL);
5007 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
5008 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
5009 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07005010
5011 for (i = 0; i < hsotg->num_of_eps; i++) {
5012 /* Backup IN EPs */
Googler9398cc32022-12-02 17:21:52 +08005013 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
Googleraf606d22022-10-26 21:40:12 -07005014
5015 /* Ensure DATA PID is correctly configured */
5016 if (dr->diepctl[i] & DXEPCTL_DPID)
5017 dr->diepctl[i] |= DXEPCTL_SETD1PID;
5018 else
5019 dr->diepctl[i] |= DXEPCTL_SETD0PID;
5020
Googler9398cc32022-12-02 17:21:52 +08005021 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
5022 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
Googleraf606d22022-10-26 21:40:12 -07005023
5024 /* Backup OUT EPs */
Googler9398cc32022-12-02 17:21:52 +08005025 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
Googleraf606d22022-10-26 21:40:12 -07005026
5027 /* Ensure DATA PID is correctly configured */
5028 if (dr->doepctl[i] & DXEPCTL_DPID)
5029 dr->doepctl[i] |= DXEPCTL_SETD1PID;
5030 else
5031 dr->doepctl[i] |= DXEPCTL_SETD0PID;
5032
Googler9398cc32022-12-02 17:21:52 +08005033 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
5034 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
5035 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
Googleraf606d22022-10-26 21:40:12 -07005036 }
5037 dr->valid = true;
5038 return 0;
5039}
5040
5041/**
5042 * dwc2_restore_device_registers() - Restore controller device registers.
5043 * When resuming usb bus, device registers needs to be restored
5044 * if controller power were disabled.
5045 *
5046 * @hsotg: Programming view of the DWC_otg controller
Googler9398cc32022-12-02 17:21:52 +08005047 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
5048 *
5049 * Return: 0 if successful, negative error code otherwise
Googleraf606d22022-10-26 21:40:12 -07005050 */
Googler9398cc32022-12-02 17:21:52 +08005051int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
Googleraf606d22022-10-26 21:40:12 -07005052{
5053 struct dwc2_dregs_backup *dr;
Googleraf606d22022-10-26 21:40:12 -07005054 int i;
5055
5056 dev_dbg(hsotg->dev, "%s\n", __func__);
5057
5058 /* Restore dev regs */
5059 dr = &hsotg->dr_backup;
5060 if (!dr->valid) {
5061 dev_err(hsotg->dev, "%s: no device registers to restore\n",
5062 __func__);
5063 return -EINVAL;
5064 }
5065 dr->valid = false;
5066
Googler9398cc32022-12-02 17:21:52 +08005067 if (!remote_wakeup)
5068 dwc2_writel(hsotg, dr->dctl, DCTL);
5069
5070 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
5071 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
5072 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
Googleraf606d22022-10-26 21:40:12 -07005073
5074 for (i = 0; i < hsotg->num_of_eps; i++) {
5075 /* Restore IN EPs */
Googler9398cc32022-12-02 17:21:52 +08005076 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
5077 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
5078 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
5079 /** WA for enabled EPx's IN in DDMA mode. On entering to
5080 * hibernation wrong value read and saved from DIEPDMAx,
5081 * as result BNA interrupt asserted on hibernation exit
5082 * by restoring from saved area.
5083 */
5084 if (hsotg->params.g_dma_desc &&
5085 (dr->diepctl[i] & DXEPCTL_EPENA))
5086 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
5087 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
5088 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Googleraf606d22022-10-26 21:40:12 -07005089 /* Restore OUT EPs */
Googler9398cc32022-12-02 17:21:52 +08005090 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
5091 /* WA for enabled EPx's OUT in DDMA mode. On entering to
5092 * hibernation wrong value read and saved from DOEPDMAx,
5093 * as result BNA interrupt asserted on hibernation exit
5094 * by restoring from saved area.
5095 */
5096 if (hsotg->params.g_dma_desc &&
5097 (dr->doepctl[i] & DXEPCTL_EPENA))
5098 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
5099 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
5100 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
Googleraf606d22022-10-26 21:40:12 -07005101 }
5102
Googleraf606d22022-10-26 21:40:12 -07005103 return 0;
5104}
Googler9398cc32022-12-02 17:21:52 +08005105
5106/**
5107 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
5108 *
5109 * @hsotg: Programming view of DWC_otg controller
5110 *
5111 */
5112void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
5113{
5114 u32 val;
5115
5116 if (!hsotg->params.lpm)
5117 return;
5118
5119 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
5120 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
5121 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
5122 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
5123 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
5124 val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL;
5125 val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC;
5126 dwc2_writel(hsotg, val, GLPMCFG);
5127 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
5128
5129 /* Unmask WKUP_ALERT Interrupt */
5130 if (hsotg->params.service_interval)
5131 dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
5132}
5133
5134/**
5135 * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode
5136 *
5137 * @hsotg: Programming view of DWC_otg controller
5138 *
5139 */
5140void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
5141{
5142 u32 val = 0;
5143
5144 val |= GREFCLK_REF_CLK_MODE;
5145 val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
5146 val |= hsotg->params.sof_cnt_wkup_alert <<
5147 GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT;
5148
5149 dwc2_writel(hsotg, val, GREFCLK);
5150 dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
5151}
5152
5153/**
5154 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
5155 *
5156 * @hsotg: Programming view of the DWC_otg controller
5157 *
5158 * Return non-zero if failed to enter to hibernation.
5159 */
5160int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5161{
5162 u32 gpwrdn;
5163 int ret = 0;
5164
5165 /* Change to L2(suspend) state */
5166 hsotg->lx_state = DWC2_L2;
5167 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5168 ret = dwc2_backup_global_registers(hsotg);
5169 if (ret) {
5170 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5171 __func__);
5172 return ret;
5173 }
5174 ret = dwc2_backup_device_registers(hsotg);
5175 if (ret) {
5176 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5177 __func__);
5178 return ret;
5179 }
5180
5181 gpwrdn = GPWRDN_PWRDNRSTN;
5182 gpwrdn |= GPWRDN_PMUACTV;
5183 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5184 udelay(10);
5185
5186 /* Set flag to indicate that we are in hibernation */
5187 hsotg->hibernated = 1;
5188
5189 /* Enable interrupts from wake up logic */
5190 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5191 gpwrdn |= GPWRDN_PMUINTSEL;
5192 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5193 udelay(10);
5194
5195 /* Unmask device mode interrupts in GPWRDN */
5196 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5197 gpwrdn |= GPWRDN_RST_DET_MSK;
5198 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5199 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5200 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5201 udelay(10);
5202
5203 /* Enable Power Down Clamp */
5204 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5205 gpwrdn |= GPWRDN_PWRDNCLMP;
5206 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5207 udelay(10);
5208
5209 /* Switch off VDD */
5210 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5211 gpwrdn |= GPWRDN_PWRDNSWTCH;
5212 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5213 udelay(10);
5214
5215 /* Save gpwrdn register for further usage if stschng interrupt */
5216 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
5217 dev_dbg(hsotg->dev, "Hibernation completed\n");
5218
5219 return ret;
5220}
5221
5222/**
5223 * dwc2_gadget_exit_hibernation()
5224 * This function is for exiting from Device mode hibernation by host initiated
5225 * resume/reset and device initiated remote-wakeup.
5226 *
5227 * @hsotg: Programming view of the DWC_otg controller
5228 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5229 * @reset: indicates whether resume is initiated by Reset.
5230 *
5231 * Return non-zero if failed to exit from hibernation.
5232 */
5233int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5234 int rem_wakeup, int reset)
5235{
5236 u32 pcgcctl;
5237 u32 gpwrdn;
5238 u32 dctl;
5239 int ret = 0;
5240 struct dwc2_gregs_backup *gr;
5241 struct dwc2_dregs_backup *dr;
5242
5243 gr = &hsotg->gr_backup;
5244 dr = &hsotg->dr_backup;
5245
5246 if (!hsotg->hibernated) {
5247 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5248 return 1;
5249 }
5250 dev_dbg(hsotg->dev,
5251 "%s: called with rem_wakeup = %d reset = %d\n",
5252 __func__, rem_wakeup, reset);
5253
5254 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5255
5256 if (!reset) {
5257 /* Clear all pending interupts */
5258 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5259 }
5260
5261 /* De-assert Restore */
5262 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5263 gpwrdn &= ~GPWRDN_RESTORE;
5264 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5265 udelay(10);
5266
5267 if (!rem_wakeup) {
5268 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5269 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5270 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5271 }
5272
5273 /* Restore GUSBCFG, DCFG and DCTL */
5274 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5275 dwc2_writel(hsotg, dr->dcfg, DCFG);
5276 dwc2_writel(hsotg, dr->dctl, DCTL);
5277
5278 /* De-assert Wakeup Logic */
5279 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5280 gpwrdn &= ~GPWRDN_PMUACTV;
5281 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5282
5283 if (rem_wakeup) {
5284 udelay(10);
5285 /* Start Remote Wakeup Signaling */
5286 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
5287 } else {
5288 udelay(50);
5289 /* Set Device programming done bit */
5290 dctl = dwc2_readl(hsotg, DCTL);
5291 dctl |= DCTL_PWRONPRGDONE;
5292 dwc2_writel(hsotg, dctl, DCTL);
5293 }
5294 /* Wait for interrupts which must be cleared */
5295 mdelay(2);
5296 /* Clear all pending interupts */
5297 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5298
5299 /* Restore global registers */
5300 ret = dwc2_restore_global_registers(hsotg);
5301 if (ret) {
5302 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5303 __func__);
5304 return ret;
5305 }
5306
5307 /* Restore device registers */
5308 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5309 if (ret) {
5310 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5311 __func__);
5312 return ret;
5313 }
5314
5315 if (rem_wakeup) {
5316 mdelay(10);
5317 dctl = dwc2_readl(hsotg, DCTL);
5318 dctl &= ~DCTL_RMTWKUPSIG;
5319 dwc2_writel(hsotg, dctl, DCTL);
5320 }
5321
5322 hsotg->hibernated = 0;
5323 hsotg->lx_state = DWC2_L0;
5324 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5325
5326 return ret;
5327}