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