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