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