blob: 0b892a2b9cc81317a9186f525dfaea42ba664c37 [file] [log] [blame]
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBSS DRD Driver - gadget side.
4 *
5 * Copyright (C) 2018-2019 Cadence Design Systems.
6 * Copyright (C) 2017-2018 NXP
7 *
8 * Authors: Pawel Jez <pjez@cadence.com>,
9 * Pawel Laszczak <pawell@cadence.com>
10 * Peter Chen <peter.chen@nxp.com>
11 */
12
13/*
14 * Work around 1:
15 * At some situations, the controller may get stale data address in TRB
16 * at below sequences:
17 * 1. Controller read TRB includes data address
18 * 2. Software updates TRBs includes data address and Cycle bit
19 * 3. Controller read TRB which includes Cycle bit
20 * 4. DMA run with stale data address
21 *
22 * To fix this problem, driver needs to make the first TRB in TD as invalid.
23 * After preparing all TRBs driver needs to check the position of DMA and
24 * if the DMA point to the first just added TRB and doorbell is 1,
25 * then driver must defer making this TRB as valid. This TRB will be make
26 * as valid during adding next TRB only if DMA is stopped or at TRBERR
27 * interrupt.
28 *
29 * Issue has been fixed in DEV_VER_V3 version of controller.
30 *
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +010031 * Work around 2:
32 * Controller for OUT endpoints has shared on-chip buffers for all incoming
33 * packets, including ep0out. It's FIFO buffer, so packets must be handle by DMA
34 * in correct order. If the first packet in the buffer will not be handled,
35 * then the following packets directed for other endpoints and functions
36 * will be blocked.
37 * Additionally the packets directed to one endpoint can block entire on-chip
38 * buffers. In this case transfer to other endpoints also will blocked.
39 *
40 * To resolve this issue after raising the descriptor missing interrupt
41 * driver prepares internal usb_request object and use it to arm DMA transfer.
42 *
43 * The problematic situation was observed in case when endpoint has been enabled
44 * but no usb_request were queued. Driver try detects such endpoints and will
45 * use this workaround only for these endpoint.
46 *
47 * Driver use limited number of buffer. This number can be set by macro
48 * CDNS3_WA2_NUM_BUFFERS.
49 *
50 * Such blocking situation was observed on ACM gadget. For this function
51 * host send OUT data packet but ACM function is not prepared for this packet.
52 * It's cause that buffer placed in on chip memory block transfer to other
53 * endpoints.
54 *
55 * Issue has been fixed in DEV_VER_V2 version of controller.
56 *
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010057 */
58
59#include <linux/dma-mapping.h>
60#include <linux/usb/gadget.h>
61#include <linux/module.h>
Sanket Parmarb9b1eae2021-03-09 06:19:39 +010062#include <linux/dmapool.h>
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010063#include <linux/iopoll.h>
64
65#include "core.h"
66#include "gadget-export.h"
Pawel Laszczak64b558f2020-12-07 11:32:26 +010067#include "cdns3-gadget.h"
68#include "cdns3-trace.h"
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010069#include "drd.h"
70
71static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
72 struct usb_request *request,
73 gfp_t gfp_flags);
74
Jayshri Pawar54c4c692019-12-13 06:25:42 +010075static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
76 struct usb_request *request);
77
78static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
79 struct usb_request *request);
80
81/**
82 * cdns3_clear_register_bit - clear bit in given register.
83 * @ptr: address of device controller register to be read and changed
84 * @mask: bits requested to clar
85 */
Jason Yane9010322020-04-02 20:38:37 +080086static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask)
Jayshri Pawar54c4c692019-12-13 06:25:42 +010087{
88 mask = readl(ptr) & ~mask;
89 writel(mask, ptr);
90}
91
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010092/**
93 * cdns3_set_register_bit - set bit in given register.
94 * @ptr: address of device controller register to be read and changed
95 * @mask: bits requested to set
96 */
97void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
98{
99 mask = readl(ptr) | mask;
100 writel(mask, ptr);
101}
102
103/**
104 * cdns3_ep_addr_to_index - Macro converts endpoint address to
105 * index of endpoint object in cdns3_device.eps[] container
106 * @ep_addr: endpoint address for which endpoint object is required
107 *
108 */
109u8 cdns3_ep_addr_to_index(u8 ep_addr)
110{
111 return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
112}
113
114static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
115 struct cdns3_endpoint *priv_ep)
116{
117 int dma_index;
118
119 dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
120
121 return dma_index / TRB_SIZE;
122}
123
124/**
125 * cdns3_next_request - returns next request from list
126 * @list: list containing requests
127 *
128 * Returns request or NULL if no requests in list
129 */
130struct usb_request *cdns3_next_request(struct list_head *list)
131{
132 return list_first_entry_or_null(list, struct usb_request, list);
133}
134
135/**
136 * cdns3_next_align_buf - returns next buffer from list
137 * @list: list containing buffers
138 *
139 * Returns buffer or NULL if no buffers in list
140 */
Jason Yane9010322020-04-02 20:38:37 +0800141static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100142{
143 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
144}
145
146/**
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100147 * cdns3_next_priv_request - returns next request from list
148 * @list: list containing requests
149 *
150 * Returns request or NULL if no requests in list
151 */
Jason Yane9010322020-04-02 20:38:37 +0800152static struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100153{
154 return list_first_entry_or_null(list, struct cdns3_request, list);
155}
156
157/**
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100158 * select_ep - selects endpoint
159 * @priv_dev: extended gadget object
160 * @ep: endpoint address
161 */
162void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
163{
164 if (priv_dev->selected_ep == ep)
165 return;
166
167 priv_dev->selected_ep = ep;
168 writel(ep, &priv_dev->regs->ep_sel);
169}
170
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100171/**
172 * cdns3_get_tdl - gets current tdl for selected endpoint.
173 * @priv_dev: extended gadget object
174 *
175 * Before calling this function the appropriate endpoint must
176 * be selected by means of cdns3_select_ep function.
177 */
178static int cdns3_get_tdl(struct cdns3_device *priv_dev)
179{
180 if (priv_dev->dev_ver < DEV_VER_V3)
181 return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
182 else
183 return readl(&priv_dev->regs->ep_tdl);
184}
185
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100186dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
187 struct cdns3_trb *trb)
188{
189 u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
190
191 return priv_ep->trb_pool_dma + offset;
192}
193
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100194static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
195{
196 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
197
198 if (priv_ep->trb_pool) {
Sanket Parmarb9b1eae2021-03-09 06:19:39 +0100199 dma_pool_free(priv_dev->eps_dma_pool,
200 priv_ep->trb_pool, priv_ep->trb_pool_dma);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100201 priv_ep->trb_pool = NULL;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100202 }
203}
204
205/**
206 * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint
207 * @priv_ep: endpoint object
208 *
209 * Function will return 0 on success or -ENOMEM on allocation error
210 */
211int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
212{
213 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
Sanket Parmarb9b1eae2021-03-09 06:19:39 +0100214 int ring_size = TRB_RING_SIZE;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100215 int num_trbs = ring_size / TRB_SIZE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100216 struct cdns3_trb *link_trb;
217
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100218 if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size)
219 cdns3_free_trb_pool(priv_ep);
220
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100221 if (!priv_ep->trb_pool) {
Sanket Parmarb9b1eae2021-03-09 06:19:39 +0100222 priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
223 GFP_DMA32 | GFP_ATOMIC,
224 &priv_ep->trb_pool_dma);
225
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100226 if (!priv_ep->trb_pool)
227 return -ENOMEM;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100228
229 priv_ep->alloc_ring_size = ring_size;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100230 }
231
Peter Chen95f5acf2020-07-22 11:06:19 +0800232 memset(priv_ep->trb_pool, 0, ring_size);
233
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100234 priv_ep->num_trbs = num_trbs;
235
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100236 if (!priv_ep->num)
237 return 0;
238
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100239 /* Initialize the last TRB as Link TRB */
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100240 link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100241
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100242 if (priv_ep->use_streams) {
243 /*
244 * For stream capable endpoints driver use single correct TRB.
245 * The last trb has zeroed cycle bit
246 */
247 link_trb->control = 0;
248 } else {
Peter Chen8dafb3c2020-08-21 11:14:37 +0800249 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma));
250 link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100251 }
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100252 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100253}
254
255/**
256 * cdns3_ep_stall_flush - Stalls and flushes selected endpoint
257 * @priv_ep: endpoint object
258 *
259 * Endpoint must be selected before call to this function
260 */
261static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
262{
263 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
264 int val;
265
266 trace_cdns3_halt(priv_ep, 1, 1);
267
268 writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
269 &priv_dev->regs->ep_cmd);
270
271 /* wait for DFLUSH cleared */
272 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
273 !(val & EP_CMD_DFLUSH), 1, 1000);
274 priv_ep->flags |= EP_STALLED;
275 priv_ep->flags &= ~EP_STALL_PENDING;
276}
277
278/**
279 * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller.
280 * @priv_dev: extended gadget object
281 */
282void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
283{
Pawel Laszczak52d39672020-10-22 08:55:05 +0800284 int i;
285
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100286 writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
287
288 cdns3_allow_enable_l1(priv_dev, 0);
289 priv_dev->hw_configured_flag = 0;
290 priv_dev->onchip_used_size = 0;
291 priv_dev->out_mem_is_allocated = 0;
292 priv_dev->wait_for_setup = 0;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100293 priv_dev->using_streams = 0;
Pawel Laszczak52d39672020-10-22 08:55:05 +0800294
295 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
296 if (priv_dev->eps[i])
297 priv_dev->eps[i]->flags &= ~EP_CONFIGURED;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100298}
299
300/**
301 * cdns3_ep_inc_trb - increment a trb index.
302 * @index: Pointer to the TRB index to increment.
303 * @cs: Cycle state
304 * @trb_in_seg: number of TRBs in segment
305 *
306 * The index should never point to the link TRB. After incrementing,
307 * if it is point to the link TRB, wrap around to the beginning and revert
308 * cycle state bit The
309 * link TRB is always at the last TRB entry.
310 */
311static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
312{
313 (*index)++;
314 if (*index == (trb_in_seg - 1)) {
315 *index = 0;
316 *cs ^= 1;
317 }
318}
319
320/**
321 * cdns3_ep_inc_enq - increment endpoint's enqueue pointer
322 * @priv_ep: The endpoint whose enqueue pointer we're incrementing
323 */
324static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
325{
326 priv_ep->free_trbs--;
327 cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
328}
329
330/**
331 * cdns3_ep_inc_deq - increment endpoint's dequeue pointer
332 * @priv_ep: The endpoint whose dequeue pointer we're incrementing
333 */
334static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
335{
336 priv_ep->free_trbs++;
337 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
338}
339
Jason Yane9010322020-04-02 20:38:37 +0800340static void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100341{
342 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
343 int current_trb = priv_req->start_trb;
344
345 while (current_trb != priv_req->end_trb) {
346 cdns3_ep_inc_deq(priv_ep);
347 current_trb = priv_ep->dequeue;
348 }
349
350 cdns3_ep_inc_deq(priv_ep);
351}
352
353/**
354 * cdns3_allow_enable_l1 - enable/disable permits to transition to L1.
355 * @priv_dev: Extended gadget object
356 * @enable: Enable/disable permit to transition to L1.
357 *
358 * If bit USB_CONF_L1EN is set and device receive Extended Token packet,
359 * then controller answer with ACK handshake.
360 * If bit USB_CONF_L1DS is set and device receive Extended Token packet,
361 * then controller answer with NYET handshake.
362 */
363void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
364{
365 if (enable)
366 writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
367 else
368 writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
369}
370
371enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
372{
373 u32 reg;
374
375 reg = readl(&priv_dev->regs->usb_sts);
376
377 if (DEV_SUPERSPEED(reg))
378 return USB_SPEED_SUPER;
379 else if (DEV_HIGHSPEED(reg))
380 return USB_SPEED_HIGH;
381 else if (DEV_FULLSPEED(reg))
382 return USB_SPEED_FULL;
383 else if (DEV_LOWSPEED(reg))
384 return USB_SPEED_LOW;
385 return USB_SPEED_UNKNOWN;
386}
387
388/**
389 * cdns3_start_all_request - add to ring all request not started
390 * @priv_dev: Extended gadget object
391 * @priv_ep: The endpoint for whom request will be started.
392 *
393 * Returns return ENOMEM if transfer ring i not enough TRBs to start
394 * all requests.
395 */
396static int cdns3_start_all_request(struct cdns3_device *priv_dev,
397 struct cdns3_endpoint *priv_ep)
398{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100399 struct usb_request *request;
400 int ret = 0;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100401 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
402
403 /*
404 * If the last pending transfer is INTERNAL
405 * OR streams are enabled for this endpoint
406 * do NOT start new transfer till the last one is pending
407 */
408 if (!pending_empty) {
409 struct cdns3_request *priv_req;
410
411 request = cdns3_next_request(&priv_ep->pending_req_list);
412 priv_req = to_cdns3_request(request);
413 if ((priv_req->flags & REQUEST_INTERNAL) ||
414 (priv_ep->flags & EP_TDLCHK_EN) ||
415 priv_ep->use_streams) {
Nicolas Boichatb3a5ce82020-06-27 15:03:04 +0800416 dev_dbg(priv_dev->dev, "Blocking external request\n");
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100417 return ret;
418 }
419 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100420
421 while (!list_empty(&priv_ep->deferred_req_list)) {
422 request = cdns3_next_request(&priv_ep->deferred_req_list);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100423
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100424 if (!priv_ep->use_streams) {
425 ret = cdns3_ep_run_transfer(priv_ep, request);
426 } else {
427 priv_ep->stream_sg_idx = 0;
428 ret = cdns3_ep_run_stream_transfer(priv_ep, request);
429 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100430 if (ret)
431 return ret;
432
433 list_del(&request->list);
434 list_add_tail(&request->list,
435 &priv_ep->pending_req_list);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100436 if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN))
437 break;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100438 }
439
440 priv_ep->flags &= ~EP_RING_FULL;
441 return ret;
442}
443
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100444/*
445 * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set
446 * driver try to detect whether endpoint need additional internal
447 * buffer for unblocking on-chip FIFO buffer. This flag will be cleared
448 * if before first DESCMISS interrupt the DMA will be armed.
449 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100450#define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100451 if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
452 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
453 (reg) |= EP_STS_EN_DESCMISEN; \
454 } } while (0)
455
Peter Chen141e70f2020-09-10 17:11:28 +0800456static void __cdns3_descmiss_copy_data(struct usb_request *request,
457 struct usb_request *descmiss_req)
458{
459 int length = request->actual + descmiss_req->actual;
460 struct scatterlist *s = request->sg;
461
462 if (!s) {
463 if (length <= request->length) {
464 memcpy(&((u8 *)request->buf)[request->actual],
465 descmiss_req->buf,
466 descmiss_req->actual);
467 request->actual = length;
468 } else {
469 /* It should never occures */
470 request->status = -ENOMEM;
471 }
472 } else {
473 if (length <= sg_dma_len(s)) {
474 void *p = phys_to_virt(sg_dma_address(s));
475
476 memcpy(&((u8 *)p)[request->actual],
477 descmiss_req->buf,
478 descmiss_req->actual);
479 request->actual = length;
480 } else {
481 request->status = -ENOMEM;
482 }
483 }
484}
485
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100486/**
487 * cdns3_wa2_descmiss_copy_data copy data from internal requests to
488 * request queued by class driver.
489 * @priv_ep: extended endpoint object
490 * @request: request object
491 */
492static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
493 struct usb_request *request)
494{
495 struct usb_request *descmiss_req;
496 struct cdns3_request *descmiss_priv_req;
497
498 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
499 int chunk_end;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100500
501 descmiss_priv_req =
502 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
503 descmiss_req = &descmiss_priv_req->request;
504
505 /* driver can't touch pending request */
506 if (descmiss_priv_req->flags & REQUEST_PENDING)
507 break;
508
509 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100510 request->status = descmiss_req->status;
Peter Chen141e70f2020-09-10 17:11:28 +0800511 __cdns3_descmiss_copy_data(request, descmiss_req);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100512 list_del_init(&descmiss_priv_req->list);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100513 kfree(descmiss_req->buf);
514 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
515 --priv_ep->wa2_counter;
516
517 if (!chunk_end)
518 break;
519 }
520}
521
Jason Yane9010322020-04-02 20:38:37 +0800522static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800523 struct cdns3_endpoint *priv_ep,
524 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100525{
526 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
527 priv_req->flags & REQUEST_INTERNAL) {
528 struct usb_request *req;
529
530 req = cdns3_next_request(&priv_ep->deferred_req_list);
531
532 priv_ep->descmis_req = NULL;
533
534 if (!req)
535 return NULL;
536
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100537 /* unmap the gadget request before copying data */
538 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
539 priv_ep->dir);
540
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100541 cdns3_wa2_descmiss_copy_data(priv_ep, req);
542 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
543 req->length != req->actual) {
544 /* wait for next part of transfer */
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100545 /* re-map the gadget request buffer*/
546 usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
547 usb_endpoint_dir_in(priv_ep->endpoint.desc));
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100548 return NULL;
549 }
550
551 if (req->status == -EINPROGRESS)
552 req->status = 0;
553
554 list_del_init(&req->list);
555 cdns3_start_all_request(priv_dev, priv_ep);
556 return req;
557 }
558
559 return &priv_req->request;
560}
561
Jason Yane9010322020-04-02 20:38:37 +0800562static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800563 struct cdns3_endpoint *priv_ep,
564 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100565{
566 int deferred = 0;
567
568 /*
569 * If transfer was queued before DESCMISS appear than we
570 * can disable handling of DESCMISS interrupt. Driver assumes that it
571 * can disable special treatment for this endpoint.
572 */
573 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
574 u32 reg;
575
576 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
577 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
578 reg = readl(&priv_dev->regs->ep_sts_en);
579 reg &= ~EP_STS_EN_DESCMISEN;
580 trace_cdns3_wa2(priv_ep, "workaround disabled\n");
581 writel(reg, &priv_dev->regs->ep_sts_en);
582 }
583
584 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
585 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
586 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
587
588 /*
589 * DESCMISS transfer has been finished, so data will be
590 * directly copied from internal allocated usb_request
591 * objects.
592 */
593 if (pending_empty && !descmiss_empty &&
594 !(priv_req->flags & REQUEST_INTERNAL)) {
595 cdns3_wa2_descmiss_copy_data(priv_ep,
596 &priv_req->request);
597
598 trace_cdns3_wa2(priv_ep, "get internal stored data");
599
600 list_add_tail(&priv_req->request.list,
601 &priv_ep->pending_req_list);
602 cdns3_gadget_giveback(priv_ep, priv_req,
603 priv_req->request.status);
604
605 /*
606 * Intentionally driver returns positive value as
607 * correct value. It informs that transfer has
608 * been finished.
609 */
610 return EINPROGRESS;
611 }
612
613 /*
614 * Driver will wait for completion DESCMISS transfer,
615 * before starts new, not DESCMISS transfer.
616 */
617 if (!pending_empty && !descmiss_empty) {
618 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
619 deferred = 1;
620 }
621
622 if (priv_req->flags & REQUEST_INTERNAL)
623 list_add_tail(&priv_req->list,
624 &priv_ep->wa2_descmiss_req_list);
625 }
626
627 return deferred;
628}
629
630static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
631{
632 struct cdns3_request *priv_req;
633
634 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
635 u8 chain;
636
637 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
638 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
639
640 trace_cdns3_wa2(priv_ep, "removes eldest request");
641
642 kfree(priv_req->request.buf);
643 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
644 &priv_req->request);
645 list_del_init(&priv_req->list);
646 --priv_ep->wa2_counter;
647
648 if (!chain)
649 break;
650 }
651}
652
653/**
654 * cdns3_wa2_descmissing_packet - handles descriptor missing event.
Lee Jones4a35aa62020-07-02 15:46:12 +0100655 * @priv_ep: extended gadget object
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100656 *
657 * This function is used only for WA2. For more information see Work around 2
658 * description.
659 */
660static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
661{
662 struct cdns3_request *priv_req;
663 struct usb_request *request;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100664 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
665
666 /* check for pending transfer */
667 if (!pending_empty) {
668 trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
669 return;
670 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100671
672 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
673 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
674 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
675 }
676
677 trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
678
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100679 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
680 trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100681 cdns3_wa2_remove_old_request(priv_ep);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100682 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100683
684 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
685 GFP_ATOMIC);
686 if (!request)
687 goto err;
688
689 priv_req = to_cdns3_request(request);
690 priv_req->flags |= REQUEST_INTERNAL;
691
692 /* if this field is still assigned it indicate that transfer related
693 * with this request has not been finished yet. Driver in this
694 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
695 * flag to previous one. It will indicate that current request is
696 * part of the previous one.
697 */
698 if (priv_ep->descmis_req)
699 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
700
701 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
702 GFP_ATOMIC);
703 priv_ep->wa2_counter++;
704
705 if (!priv_req->request.buf) {
706 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
707 goto err;
708 }
709
710 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
711 priv_ep->descmis_req = priv_req;
712
713 __cdns3_gadget_ep_queue(&priv_ep->endpoint,
714 &priv_ep->descmis_req->request,
715 GFP_ATOMIC);
716
717 return;
718
719err:
720 dev_err(priv_ep->cdns3_dev->dev,
721 "Failed: No sufficient memory for DESCMIS\n");
722}
723
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100724static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
725{
726 u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
727
728 if (tdl) {
729 u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
730
731 writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
732 &priv_dev->regs->ep_cmd);
733 }
734}
735
736static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
737{
738 u32 ep_sts_reg;
739
740 /* select EP0-out */
741 cdns3_select_ep(priv_dev, 0);
742
743 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
744
745 if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
746 u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
747 struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
748
749 if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
750 outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
751 u8 pending_empty = list_empty(&outq_ep->pending_req_list);
752
753 if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
754 (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
755 !pending_empty) {
756 } else {
757 u32 ep_sts_en_reg;
758 u32 ep_cmd_reg;
759
760 cdns3_select_ep(priv_dev, outq_ep->num |
761 outq_ep->dir);
762 ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
763 ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
764
765 outq_ep->flags |= EP_TDLCHK_EN;
766 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
767 EP_CFG_TDL_CHK);
768
769 cdns3_wa2_enable_detection(priv_dev, outq_ep,
770 ep_sts_en_reg);
771 writel(ep_sts_en_reg,
772 &priv_dev->regs->ep_sts_en);
773 /* reset tdl value to zero */
774 cdns3_wa2_reset_tdl(priv_dev);
775 /*
776 * Memory barrier - Reset tdl before ringing the
777 * doorbell.
778 */
779 wmb();
780 if (EP_CMD_DRDY & ep_cmd_reg) {
781 trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
782
783 } else {
784 trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
785 /*
786 * ring doorbell to generate DESCMIS irq
787 */
788 writel(EP_CMD_DRDY,
789 &priv_dev->regs->ep_cmd);
790 }
791 }
792 }
793 }
794}
795
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100796/**
797 * cdns3_gadget_giveback - call struct usb_request's ->complete callback
798 * @priv_ep: The endpoint to whom the request belongs to
799 * @priv_req: The request we're giving back
800 * @status: completion code for the request
801 *
802 * Must be called with controller's lock held and interrupts disabled. This
803 * function will unmap @req and call its ->complete() callback to notify upper
804 * layers that it has completed.
805 */
806void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
807 struct cdns3_request *priv_req,
808 int status)
809{
810 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
811 struct usb_request *request = &priv_req->request;
812
813 list_del_init(&request->list);
814
815 if (request->status == -EINPROGRESS)
816 request->status = status;
817
818 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
819 priv_ep->dir);
820
821 if ((priv_req->flags & REQUEST_UNALIGNED) &&
822 priv_ep->dir == USB_DIR_OUT && !request->status)
823 memcpy(request->buf, priv_req->aligned_buf->buf,
824 request->length);
825
826 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
Peter Chen249f0a22020-09-10 17:11:27 +0800827 /* All TRBs have finished, clear the counter */
828 priv_req->finished_trb = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100829 trace_cdns3_gadget_giveback(priv_req);
830
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100831 if (priv_dev->dev_ver < DEV_VER_V2) {
832 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
833 priv_req);
834 if (!request)
835 return;
836 }
837
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100838 if (request->complete) {
839 spin_unlock(&priv_dev->lock);
840 usb_gadget_giveback_request(&priv_ep->endpoint,
841 request);
842 spin_lock(&priv_dev->lock);
843 }
844
845 if (request->buf == priv_dev->zlp_buf)
846 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
847}
848
Jason Yane9010322020-04-02 20:38:37 +0800849static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100850{
851 /* Work around for stale data address in TRB*/
852 if (priv_ep->wa1_set) {
853 trace_cdns3_wa1(priv_ep, "restore cycle bit");
854
855 priv_ep->wa1_set = 0;
856 priv_ep->wa1_trb_index = 0xFFFF;
857 if (priv_ep->wa1_cycle_bit) {
858 priv_ep->wa1_trb->control =
Peter Chen8dafb3c2020-08-21 11:14:37 +0800859 priv_ep->wa1_trb->control | cpu_to_le32(0x1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100860 } else {
861 priv_ep->wa1_trb->control =
Peter Chen8dafb3c2020-08-21 11:14:37 +0800862 priv_ep->wa1_trb->control & cpu_to_le32(~0x1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100863 }
864 }
865}
866
867static void cdns3_free_aligned_request_buf(struct work_struct *work)
868{
869 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
870 aligned_buf_wq);
871 struct cdns3_aligned_buf *buf, *tmp;
872 unsigned long flags;
873
874 spin_lock_irqsave(&priv_dev->lock, flags);
875
876 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
877 if (!buf->in_use) {
878 list_del(&buf->list);
879
880 /*
881 * Re-enable interrupts to free DMA capable memory.
882 * Driver can't free this memory with disabled
883 * interrupts.
884 */
885 spin_unlock_irqrestore(&priv_dev->lock, flags);
886 dma_free_coherent(priv_dev->sysdev, buf->size,
887 buf->buf, buf->dma);
888 kfree(buf);
889 spin_lock_irqsave(&priv_dev->lock, flags);
890 }
891 }
892
893 spin_unlock_irqrestore(&priv_dev->lock, flags);
894}
895
896static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
897{
898 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
899 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
900 struct cdns3_aligned_buf *buf;
901
902 /* check if buffer is aligned to 8. */
903 if (!((uintptr_t)priv_req->request.buf & 0x7))
904 return 0;
905
906 buf = priv_req->aligned_buf;
907
908 if (!buf || priv_req->request.length > buf->size) {
909 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
910 if (!buf)
911 return -ENOMEM;
912
913 buf->size = priv_req->request.length;
914
915 buf->buf = dma_alloc_coherent(priv_dev->sysdev,
916 buf->size,
917 &buf->dma,
918 GFP_ATOMIC);
919 if (!buf->buf) {
920 kfree(buf);
921 return -ENOMEM;
922 }
923
924 if (priv_req->aligned_buf) {
925 trace_cdns3_free_aligned_request(priv_req);
926 priv_req->aligned_buf->in_use = 0;
927 queue_work(system_freezable_wq,
928 &priv_dev->aligned_buf_wq);
929 }
930
931 buf->in_use = 1;
932 priv_req->aligned_buf = buf;
933
934 list_add_tail(&buf->list,
935 &priv_dev->aligned_buf_list);
936 }
937
938 if (priv_ep->dir == USB_DIR_IN) {
939 memcpy(buf->buf, priv_req->request.buf,
940 priv_req->request.length);
941 }
942
943 priv_req->flags |= REQUEST_UNALIGNED;
944 trace_cdns3_prepare_aligned_request(priv_req);
945
946 return 0;
947}
948
949static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
950 struct cdns3_trb *trb)
951{
952 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
953
954 if (!priv_ep->wa1_set) {
955 u32 doorbell;
956
957 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
958
959 if (doorbell) {
960 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
961 priv_ep->wa1_set = 1;
962 priv_ep->wa1_trb = trb;
963 priv_ep->wa1_trb_index = priv_ep->enqueue;
964 trace_cdns3_wa1(priv_ep, "set guard");
965 return 0;
966 }
967 }
968 return 1;
969}
970
971static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
972 struct cdns3_endpoint *priv_ep)
973{
974 int dma_index;
975 u32 doorbell;
976
977 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
978 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
979
980 if (!doorbell || dma_index != priv_ep->wa1_trb_index)
981 cdns3_wa1_restore_cycle_bit(priv_ep);
982}
983
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100984static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
985 struct usb_request *request)
986{
987 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
988 struct cdns3_request *priv_req;
989 struct cdns3_trb *trb;
990 dma_addr_t trb_dma;
991 int address;
992 u32 control;
993 u32 length;
994 u32 tdl;
995 unsigned int sg_idx = priv_ep->stream_sg_idx;
996
997 priv_req = to_cdns3_request(request);
998 address = priv_ep->endpoint.desc->bEndpointAddress;
999
1000 priv_ep->flags |= EP_PENDING_REQUEST;
1001
1002 /* must allocate buffer aligned to 8 */
1003 if (priv_req->flags & REQUEST_UNALIGNED)
1004 trb_dma = priv_req->aligned_buf->dma;
1005 else
1006 trb_dma = request->dma;
1007
1008 /* For stream capable endpoints driver use only single TD. */
1009 trb = priv_ep->trb_pool + priv_ep->enqueue;
1010 priv_req->start_trb = priv_ep->enqueue;
1011 priv_req->end_trb = priv_req->start_trb;
1012 priv_req->trb = trb;
1013
1014 cdns3_select_ep(priv_ep->cdns3_dev, address);
1015
1016 control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
1017 TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
1018
1019 if (!request->num_sgs) {
Peter Chen8dafb3c2020-08-21 11:14:37 +08001020 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001021 length = request->length;
1022 } else {
Peter Chen8dafb3c2020-08-21 11:14:37 +08001023 trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001024 length = request->sg[sg_idx].length;
1025 }
1026
1027 tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
1028
Peter Chen8dafb3c2020-08-21 11:14:37 +08001029 trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001030
1031 /*
1032 * For DEV_VER_V2 controller version we have enabled
1033 * USB_CONF2_EN_TDL_TRB in DMULT configuration.
1034 * This enables TDL calculation based on TRB, hence setting TDL in TRB.
1035 */
1036 if (priv_dev->dev_ver >= DEV_VER_V2) {
1037 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001038 trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001039 }
1040 priv_req->flags |= REQUEST_PENDING;
1041
Peter Chen8dafb3c2020-08-21 11:14:37 +08001042 trb->control = cpu_to_le32(control);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001043
1044 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1045
1046 /*
1047 * Memory barrier - Cycle Bit must be set before trb->length and
1048 * trb->buffer fields.
1049 */
1050 wmb();
1051
1052 /* always first element */
1053 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
1054 &priv_dev->regs->ep_traddr);
1055
1056 if (!(priv_ep->flags & EP_STALLED)) {
1057 trace_cdns3_ring(priv_ep);
1058 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1059 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1060
1061 priv_ep->prime_flag = false;
1062
1063 /*
1064 * Controller version DEV_VER_V2 tdl calculation
1065 * is based on TRB
1066 */
1067
1068 if (priv_dev->dev_ver < DEV_VER_V2)
1069 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1070 &priv_dev->regs->ep_cmd);
1071 else if (priv_dev->dev_ver > DEV_VER_V2)
1072 writel(tdl, &priv_dev->regs->ep_tdl);
1073
1074 priv_ep->last_stream_id = priv_req->request.stream_id;
1075 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1076 writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
1077 EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
1078
1079 trace_cdns3_doorbell_epx(priv_ep->name,
1080 readl(&priv_dev->regs->ep_traddr));
1081 }
1082
1083 /* WORKAROUND for transition to L0 */
1084 __cdns3_gadget_wakeup(priv_dev);
1085
1086 return 0;
1087}
1088
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001089/**
1090 * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
1091 * @priv_ep: endpoint object
Lee Jones4a35aa62020-07-02 15:46:12 +01001092 * @request: request object
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001093 *
1094 * Returns zero on success or negative value on failure
1095 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001096static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
1097 struct usb_request *request)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001098{
1099 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1100 struct cdns3_request *priv_req;
1101 struct cdns3_trb *trb;
Peter Chen78e91582020-11-03 19:23:27 +08001102 struct cdns3_trb *link_trb = NULL;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001103 dma_addr_t trb_dma;
1104 u32 togle_pcs = 1;
1105 int sg_iter = 0;
1106 int num_trb;
1107 int address;
1108 u32 control;
1109 int pcs;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001110 u16 total_tdl = 0;
Peter Chenabc6b572020-09-10 17:11:23 +08001111 struct scatterlist *s = NULL;
1112 bool sg_supported = !!(request->num_mapped_sgs);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001113
1114 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1115 num_trb = priv_ep->interval;
1116 else
Peter Chenabc6b572020-09-10 17:11:23 +08001117 num_trb = sg_supported ? request->num_mapped_sgs : 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001118
1119 if (num_trb > priv_ep->free_trbs) {
1120 priv_ep->flags |= EP_RING_FULL;
1121 return -ENOBUFS;
1122 }
1123
1124 priv_req = to_cdns3_request(request);
1125 address = priv_ep->endpoint.desc->bEndpointAddress;
1126
1127 priv_ep->flags |= EP_PENDING_REQUEST;
1128
1129 /* must allocate buffer aligned to 8 */
1130 if (priv_req->flags & REQUEST_UNALIGNED)
1131 trb_dma = priv_req->aligned_buf->dma;
1132 else
1133 trb_dma = request->dma;
1134
1135 trb = priv_ep->trb_pool + priv_ep->enqueue;
1136 priv_req->start_trb = priv_ep->enqueue;
1137 priv_req->trb = trb;
1138
1139 cdns3_select_ep(priv_ep->cdns3_dev, address);
1140
1141 /* prepare ring */
1142 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001143 int doorbell, dma_index;
1144 u32 ch_bit = 0;
1145
1146 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1147 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1148
1149 /* Driver can't update LINK TRB if it is current processed. */
1150 if (doorbell && dma_index == priv_ep->num_trbs - 1) {
1151 priv_ep->flags |= EP_DEFERRED_DRDY;
1152 return -ENOBUFS;
1153 }
1154
1155 /*updating C bt in Link TRB before starting DMA*/
1156 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
1157 /*
1158 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
1159 * that DMA stuck at the LINK TRB.
1160 * On the other hand, removing TRB_CHAIN for longer TRs for
1161 * epXout cause that DMA stuck after handling LINK TRB.
1162 * To eliminate this strange behavioral driver set TRB_CHAIN
1163 * bit only for TR size > 2.
1164 */
1165 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
1166 TRBS_PER_SEGMENT > 2)
1167 ch_bit = TRB_CHAIN;
1168
Peter Chen8dafb3c2020-08-21 11:14:37 +08001169 link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) |
1170 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001171 }
1172
1173 if (priv_dev->dev_ver <= DEV_VER_V2)
1174 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
1175
Peter Chenabc6b572020-09-10 17:11:23 +08001176 if (sg_supported)
1177 s = request->sg;
1178
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001179 /* set incorrect Cycle Bit for first trb*/
1180 control = priv_ep->pcs ? 0 : TRB_CYCLE;
Peter Chen40252dd2020-11-03 22:16:00 +08001181 trb->length = 0;
1182 if (priv_dev->dev_ver >= DEV_VER_V2) {
1183 u16 td_size;
1184
1185 td_size = DIV_ROUND_UP(request->length,
1186 priv_ep->endpoint.maxpacket);
1187 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
Pawel Laszczakfba87012020-12-14 12:04:33 +01001188 trb->length = cpu_to_le32(TRB_TDL_SS_SIZE(td_size));
Peter Chen40252dd2020-11-03 22:16:00 +08001189 else
1190 control |= TRB_TDL_HS_SIZE(td_size);
1191 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001192
1193 do {
1194 u32 length;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001195
1196 /* fill TRB */
1197 control |= TRB_TYPE(TRB_NORMAL);
Peter Chenabc6b572020-09-10 17:11:23 +08001198 if (sg_supported) {
1199 trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s)));
1200 length = sg_dma_len(s);
1201 } else {
1202 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001203 length = request->length;
Peter Chenabc6b572020-09-10 17:11:23 +08001204 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001205
Peter Chen40252dd2020-11-03 22:16:00 +08001206 if (priv_ep->flags & EP_TDLCHK_EN)
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001207 total_tdl += DIV_ROUND_UP(length,
1208 priv_ep->endpoint.maxpacket);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001209
Peter Chen40252dd2020-11-03 22:16:00 +08001210 trb->length |= cpu_to_le32(TRB_BURST_LEN(priv_ep->trb_burst_size) |
Peter Chen8dafb3c2020-08-21 11:14:37 +08001211 TRB_LEN(length));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001212 pcs = priv_ep->pcs ? TRB_CYCLE : 0;
1213
1214 /*
1215 * first trb should be prepared as last to avoid processing
1216 * transfer to early
1217 */
1218 if (sg_iter != 0)
1219 control |= pcs;
1220
1221 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
1222 control |= TRB_IOC | TRB_ISP;
1223 } else {
1224 /* for last element in TD or in SG list */
1225 if (sg_iter == (num_trb - 1) && sg_iter != 0)
1226 control |= pcs | TRB_IOC | TRB_ISP;
1227 }
1228
1229 if (sg_iter)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001230 trb->control = cpu_to_le32(control);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001231 else
Peter Chen8dafb3c2020-08-21 11:14:37 +08001232 priv_req->trb->control = cpu_to_le32(control);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001233
Peter Chen87e1dcd2020-09-10 17:11:26 +08001234 if (sg_supported) {
Pawel Laszczakfba87012020-12-14 12:04:33 +01001235 trb->control |= cpu_to_le32(TRB_ISP);
Peter Chen87e1dcd2020-09-10 17:11:26 +08001236 /* Don't set chain bit for last TRB */
1237 if (sg_iter < num_trb - 1)
Pawel Laszczakfba87012020-12-14 12:04:33 +01001238 trb->control |= cpu_to_le32(TRB_CHAIN);
Peter Chen87e1dcd2020-09-10 17:11:26 +08001239
Peter Chenabc6b572020-09-10 17:11:23 +08001240 s = sg_next(s);
Peter Chen87e1dcd2020-09-10 17:11:26 +08001241 }
Peter Chenabc6b572020-09-10 17:11:23 +08001242
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001243 control = 0;
1244 ++sg_iter;
1245 priv_req->end_trb = priv_ep->enqueue;
1246 cdns3_ep_inc_enq(priv_ep);
1247 trb = priv_ep->trb_pool + priv_ep->enqueue;
Peter Chen24fdaee2020-11-25 20:35:23 +08001248 trb->length = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001249 } while (sg_iter < num_trb);
1250
1251 trb = priv_req->trb;
1252
1253 priv_req->flags |= REQUEST_PENDING;
Peter Chen249f0a22020-09-10 17:11:27 +08001254 priv_req->num_of_trb = num_trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001255
1256 if (sg_iter == 1)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001257 trb->control |= cpu_to_le32(TRB_IOC | TRB_ISP);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001258
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001259 if (priv_dev->dev_ver < DEV_VER_V2 &&
1260 (priv_ep->flags & EP_TDLCHK_EN)) {
1261 u16 tdl = total_tdl;
1262 u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
1263
1264 if (tdl > EP_CMD_TDL_MAX) {
1265 tdl = EP_CMD_TDL_MAX;
1266 priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
1267 }
1268
1269 if (old_tdl < tdl) {
1270 tdl -= old_tdl;
1271 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1272 &priv_dev->regs->ep_cmd);
1273 }
1274 }
1275
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001276 /*
1277 * Memory barrier - cycle bit must be set before other filds in trb.
1278 */
1279 wmb();
1280
1281 /* give the TD to the consumer*/
1282 if (togle_pcs)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001283 trb->control = trb->control ^ cpu_to_le32(1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001284
1285 if (priv_dev->dev_ver <= DEV_VER_V2)
1286 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
1287
Peter Chen4e218882020-09-10 17:11:24 +08001288 if (num_trb > 1) {
1289 int i = 0;
1290
1291 while (i < num_trb) {
1292 trace_cdns3_prepare_trb(priv_ep, trb + i);
1293 if (trb + i == link_trb) {
1294 trb = priv_ep->trb_pool;
1295 num_trb = num_trb - i;
1296 i = 0;
1297 } else {
1298 i++;
1299 }
1300 }
1301 } else {
1302 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1303 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001304
1305 /*
1306 * Memory barrier - Cycle Bit must be set before trb->length and
1307 * trb->buffer fields.
1308 */
1309 wmb();
1310
1311 /*
1312 * For DMULT mode we can set address to transfer ring only once after
1313 * enabling endpoint.
1314 */
1315 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
1316 /*
1317 * Until SW is not ready to handle the OUT transfer the ISO OUT
1318 * Endpoint should be disabled (EP_CFG.ENABLE = 0).
1319 * EP_CFG_ENABLE must be set before updating ep_traddr.
1320 */
1321 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
1322 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
1323 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
1324 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
1325 EP_CFG_ENABLE);
1326 }
1327
1328 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
1329 priv_req->start_trb * TRB_SIZE),
1330 &priv_dev->regs->ep_traddr);
1331
1332 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
1333 }
1334
1335 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
1336 trace_cdns3_ring(priv_ep);
1337 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1338 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1339 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1340 trace_cdns3_doorbell_epx(priv_ep->name,
1341 readl(&priv_dev->regs->ep_traddr));
1342 }
1343
1344 /* WORKAROUND for transition to L0 */
1345 __cdns3_gadget_wakeup(priv_dev);
1346
1347 return 0;
1348}
1349
1350void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1351{
1352 struct cdns3_endpoint *priv_ep;
1353 struct usb_ep *ep;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001354
1355 if (priv_dev->hw_configured_flag)
1356 return;
1357
1358 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001359
1360 cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1361 USB_CONF_U1EN | USB_CONF_U2EN);
1362
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001363 priv_dev->hw_configured_flag = 1;
1364
1365 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1366 if (ep->enabled) {
1367 priv_ep = ep_to_cdns3_ep(ep);
1368 cdns3_start_all_request(priv_dev, priv_ep);
1369 }
1370 }
Peter Chenf4cfe5c2020-07-17 18:13:17 +08001371
1372 cdns3_allow_enable_l1(priv_dev, 1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001373}
1374
1375/**
Peter Chen249f0a22020-09-10 17:11:27 +08001376 * cdns3_trb_handled - check whether trb has been handled by DMA
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001377 *
1378 * @priv_ep: extended endpoint object.
1379 * @priv_req: request object for checking
1380 *
1381 * Endpoint must be selected before invoking this function.
1382 *
1383 * Returns false if request has not been handled by DMA, else returns true.
1384 *
1385 * SR - start ring
1386 * ER - end ring
1387 * DQ = priv_ep->dequeue - dequeue position
1388 * EQ = priv_ep->enqueue - enqueue position
1389 * ST = priv_req->start_trb - index of first TRB in transfer ring
1390 * ET = priv_req->end_trb - index of last TRB in transfer ring
1391 * CI = current_index - index of processed TRB by DMA.
1392 *
Peter Chen249f0a22020-09-10 17:11:27 +08001393 * As first step, we check if the TRB between the ST and ET.
1394 * Then, we check if cycle bit for index priv_ep->dequeue
1395 * is correct.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001396 *
1397 * some rules:
Peter Chen249f0a22020-09-10 17:11:27 +08001398 * 1. priv_ep->dequeue never equals to current_index.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001399 * 2 priv_ep->enqueue never exceed priv_ep->dequeue
1400 * 3. exception: priv_ep->enqueue == priv_ep->dequeue
1401 * and priv_ep->free_trbs is zero.
1402 * This case indicate that TR is full.
1403 *
Peter Chen249f0a22020-09-10 17:11:27 +08001404 * At below two cases, the request have been handled.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001405 * Case 1 - priv_ep->dequeue < current_index
1406 * SR ... EQ ... DQ ... CI ... ER
1407 * SR ... DQ ... CI ... EQ ... ER
1408 *
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001409 * Case 2 - priv_ep->dequeue > current_index
Peter Chen249f0a22020-09-10 17:11:27 +08001410 * This situation takes place when CI go through the LINK TRB at the end of
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001411 * transfer ring.
1412 * SR ... CI ... EQ ... DQ ... ER
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001413 */
Peter Chen249f0a22020-09-10 17:11:27 +08001414static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001415 struct cdns3_request *priv_req)
1416{
1417 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
Colin Ian King1f9f5a82020-02-08 16:18:02 +00001418 struct cdns3_trb *trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001419 int current_index = 0;
1420 int handled = 0;
1421 int doorbell;
1422
1423 current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1424 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1425
Peter Chen249f0a22020-09-10 17:11:27 +08001426 /* current trb doesn't belong to this request */
1427 if (priv_req->start_trb < priv_req->end_trb) {
1428 if (priv_ep->dequeue > priv_req->end_trb)
1429 goto finish;
1430
1431 if (priv_ep->dequeue < priv_req->start_trb)
1432 goto finish;
1433 }
1434
1435 if ((priv_req->start_trb > priv_req->end_trb) &&
1436 (priv_ep->dequeue > priv_req->end_trb) &&
1437 (priv_ep->dequeue < priv_req->start_trb))
1438 goto finish;
1439
1440 if ((priv_req->start_trb == priv_req->end_trb) &&
1441 (priv_ep->dequeue != priv_req->end_trb))
1442 goto finish;
1443
1444 trb = &priv_ep->trb_pool[priv_ep->dequeue];
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001445
Peter Chen8dafb3c2020-08-21 11:14:37 +08001446 if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001447 goto finish;
1448
1449 if (doorbell == 1 && current_index == priv_ep->dequeue)
1450 goto finish;
1451
1452 /* The corner case for TRBS_PER_SEGMENT equal 2). */
1453 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1454 handled = 1;
1455 goto finish;
1456 }
1457
1458 if (priv_ep->enqueue == priv_ep->dequeue &&
1459 priv_ep->free_trbs == 0) {
1460 handled = 1;
1461 } else if (priv_ep->dequeue < current_index) {
1462 if ((current_index == (priv_ep->num_trbs - 1)) &&
1463 !priv_ep->dequeue)
1464 goto finish;
1465
Peter Chen249f0a22020-09-10 17:11:27 +08001466 handled = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001467 } else if (priv_ep->dequeue > current_index) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001468 handled = 1;
1469 }
1470
1471finish:
1472 trace_cdns3_request_handled(priv_req, current_index, handled);
1473
1474 return handled;
1475}
1476
1477static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1478 struct cdns3_endpoint *priv_ep)
1479{
1480 struct cdns3_request *priv_req;
1481 struct usb_request *request;
1482 struct cdns3_trb *trb;
Peter Chen249f0a22020-09-10 17:11:27 +08001483 bool request_handled = false;
1484 bool transfer_end = false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001485
1486 while (!list_empty(&priv_ep->pending_req_list)) {
1487 request = cdns3_next_request(&priv_ep->pending_req_list);
1488 priv_req = to_cdns3_request(request);
1489
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01001490 trb = priv_ep->trb_pool + priv_ep->dequeue;
1491
1492 /* Request was dequeued and TRB was changed to TRB_LINK. */
Peter Chen8dafb3c2020-08-21 11:14:37 +08001493 if (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) {
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01001494 trace_cdns3_complete_trb(priv_ep, trb);
1495 cdns3_move_deq_to_next_trb(priv_req);
1496 }
1497
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001498 if (!request->stream_id) {
1499 /* Re-select endpoint. It could be changed by other CPU
1500 * during handling usb_gadget_giveback_request.
1501 */
1502 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001503
Peter Chen249f0a22020-09-10 17:11:27 +08001504 while (cdns3_trb_handled(priv_ep, priv_req)) {
1505 priv_req->finished_trb++;
1506 if (priv_req->finished_trb >= priv_req->num_of_trb)
1507 request_handled = true;
1508
1509 trb = priv_ep->trb_pool + priv_ep->dequeue;
1510 trace_cdns3_complete_trb(priv_ep, trb);
1511
1512 if (!transfer_end)
1513 request->actual +=
1514 TRB_LEN(le32_to_cpu(trb->length));
1515
1516 if (priv_req->num_of_trb > 1 &&
1517 le32_to_cpu(trb->control) & TRB_SMM)
1518 transfer_end = true;
1519
1520 cdns3_ep_inc_deq(priv_ep);
1521 }
1522
1523 if (request_handled) {
1524 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1525 request_handled = false;
1526 transfer_end = false;
1527 } else {
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001528 goto prepare_next_td;
Peter Chen249f0a22020-09-10 17:11:27 +08001529 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001530
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001531 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1532 TRBS_PER_SEGMENT == 2)
1533 break;
1534 } else {
1535 /* Re-select endpoint. It could be changed by other CPU
1536 * during handling usb_gadget_giveback_request.
1537 */
1538 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1539
1540 trb = priv_ep->trb_pool;
1541 trace_cdns3_complete_trb(priv_ep, trb);
1542
1543 if (trb != priv_req->trb)
1544 dev_warn(priv_dev->dev,
1545 "request_trb=0x%p, queue_trb=0x%p\n",
1546 priv_req->trb, trb);
1547
1548 request->actual += TRB_LEN(le32_to_cpu(trb->length));
1549
1550 if (!request->num_sgs ||
1551 (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
1552 priv_ep->stream_sg_idx = 0;
1553 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1554 } else {
1555 priv_ep->stream_sg_idx++;
1556 cdns3_ep_run_stream_transfer(priv_ep, request);
1557 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001558 break;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001559 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001560 }
1561 priv_ep->flags &= ~EP_PENDING_REQUEST;
1562
1563prepare_next_td:
1564 if (!(priv_ep->flags & EP_STALLED) &&
1565 !(priv_ep->flags & EP_STALL_PENDING))
1566 cdns3_start_all_request(priv_dev, priv_ep);
1567}
1568
1569void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1570{
1571 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1572
1573 cdns3_wa1_restore_cycle_bit(priv_ep);
1574
1575 if (rearm) {
1576 trace_cdns3_ring(priv_ep);
1577
1578 /* Cycle Bit must be updated before arming DMA. */
1579 wmb();
1580 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1581
1582 __cdns3_gadget_wakeup(priv_dev);
1583
1584 trace_cdns3_doorbell_epx(priv_ep->name,
1585 readl(&priv_dev->regs->ep_traddr));
1586 }
1587}
1588
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001589static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
1590{
1591 u16 tdl = priv_ep->pending_tdl;
1592 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1593
1594 if (tdl > EP_CMD_TDL_MAX) {
1595 tdl = EP_CMD_TDL_MAX;
1596 priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
1597 } else {
1598 priv_ep->pending_tdl = 0;
1599 }
1600
1601 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
1602}
1603
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001604/**
1605 * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
1606 * @priv_ep: endpoint object
1607 *
1608 * Returns 0
1609 */
1610static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1611{
1612 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1613 u32 ep_sts_reg;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001614 struct usb_request *deferred_request;
1615 struct usb_request *pending_request;
1616 u32 tdl = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001617
1618 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1619
1620 trace_cdns3_epx_irq(priv_dev, priv_ep);
1621
1622 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1623 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1624
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001625 if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
1626 bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
1627
1628 tdl = cdns3_get_tdl(priv_dev);
1629
1630 /*
1631 * Continue the previous transfer:
1632 * There is some racing between ERDY and PRIME. The device send
1633 * ERDY and almost in the same time Host send PRIME. It cause
1634 * that host ignore the ERDY packet and driver has to send it
1635 * again.
1636 */
Peter Chen8dafb3c2020-08-21 11:14:37 +08001637 if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) ||
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001638 EP_STS_HOSTPP(ep_sts_reg))) {
1639 writel(EP_CMD_ERDY |
1640 EP_CMD_ERDY_SID(priv_ep->last_stream_id),
1641 &priv_dev->regs->ep_cmd);
1642 ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
1643 } else {
1644 priv_ep->prime_flag = true;
1645
1646 pending_request = cdns3_next_request(&priv_ep->pending_req_list);
1647 deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
1648
1649 if (deferred_request && !pending_request) {
1650 cdns3_start_all_request(priv_dev, priv_ep);
1651 }
1652 }
1653 }
1654
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001655 if (ep_sts_reg & EP_STS_TRBERR) {
1656 if (priv_ep->flags & EP_STALL_PENDING &&
1657 !(ep_sts_reg & EP_STS_DESCMIS &&
1658 priv_dev->dev_ver < DEV_VER_V2)) {
1659 cdns3_ep_stall_flush(priv_ep);
1660 }
1661
1662 /*
1663 * For isochronous transfer driver completes request on
1664 * IOC or on TRBERR. IOC appears only when device receive
1665 * OUT data packet. If host disable stream or lost some packet
1666 * then the only way to finish all queued transfer is to do it
1667 * on TRBERR event.
1668 */
1669 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1670 !priv_ep->wa1_set) {
1671 if (!priv_ep->dir) {
1672 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1673
1674 ep_cfg &= ~EP_CFG_ENABLE;
1675 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1676 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1677 }
1678 cdns3_transfer_completed(priv_dev, priv_ep);
1679 } else if (!(priv_ep->flags & EP_STALLED) &&
1680 !(priv_ep->flags & EP_STALL_PENDING)) {
1681 if (priv_ep->flags & EP_DEFERRED_DRDY) {
1682 priv_ep->flags &= ~EP_DEFERRED_DRDY;
1683 cdns3_start_all_request(priv_dev, priv_ep);
1684 } else {
1685 cdns3_rearm_transfer(priv_ep,
1686 priv_ep->wa1_set);
1687 }
1688 }
1689 }
1690
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001691 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
1692 (ep_sts_reg & EP_STS_IOT)) {
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001693 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1694 if (ep_sts_reg & EP_STS_ISP)
1695 priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1696 else
1697 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1698 }
1699
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001700 if (!priv_ep->use_streams) {
1701 if ((ep_sts_reg & EP_STS_IOC) ||
1702 (ep_sts_reg & EP_STS_ISP)) {
1703 cdns3_transfer_completed(priv_dev, priv_ep);
1704 } else if ((priv_ep->flags & EP_TDLCHK_EN) &
1705 priv_ep->pending_tdl) {
1706 /* handle IOT with pending tdl */
1707 cdns3_reprogram_tdl(priv_ep);
1708 }
1709 } else if (priv_ep->dir == USB_DIR_OUT) {
1710 priv_ep->ep_sts_pending |= ep_sts_reg;
1711 } else if (ep_sts_reg & EP_STS_IOT) {
1712 cdns3_transfer_completed(priv_dev, priv_ep);
1713 }
1714 }
1715
1716 /*
1717 * MD_EXIT interrupt sets when stream capable endpoint exits
1718 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
1719 */
1720 if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
1721 (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
1722 priv_ep->ep_sts_pending = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001723 cdns3_transfer_completed(priv_dev, priv_ep);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001724 }
1725
1726 /*
1727 * WA2: this condition should only be meet when
1728 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
1729 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001730 * In other cases this interrupt will be disabled.
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001731 */
1732 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1733 !(priv_ep->flags & EP_STALLED))
1734 cdns3_wa2_descmissing_packet(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001735
1736 return 0;
1737}
1738
1739static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1740{
Peter Chene11d2bf2020-10-29 17:55:18 +08001741 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001742 priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001743}
1744
1745/**
1746 * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
1747 * @priv_dev: extended gadget object
1748 * @usb_ists: bitmap representation of device's reported interrupts
1749 * (usb_ists register value)
1750 */
1751static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1752 u32 usb_ists)
Peter Chene11d2bf2020-10-29 17:55:18 +08001753__must_hold(&priv_dev->lock)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001754{
1755 int speed = 0;
1756
1757 trace_cdns3_usb_irq(priv_dev, usb_ists);
1758 if (usb_ists & USB_ISTS_L1ENTI) {
1759 /*
1760 * WORKAROUND: CDNS3 controller has issue with hardware resuming
1761 * from L1. To fix it, if any DMA transfer is pending driver
1762 * must starts driving resume signal immediately.
1763 */
1764 if (readl(&priv_dev->regs->drbl))
1765 __cdns3_gadget_wakeup(priv_dev);
1766 }
1767
1768 /* Connection detected */
1769 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1770 speed = cdns3_get_speed(priv_dev);
1771 priv_dev->gadget.speed = speed;
1772 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1773 cdns3_ep0_config(priv_dev);
1774 }
1775
1776 /* Disconnection detected */
1777 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
Peter Chene11d2bf2020-10-29 17:55:18 +08001778 spin_unlock(&priv_dev->lock);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001779 cdns3_disconnect_gadget(priv_dev);
Peter Chene11d2bf2020-10-29 17:55:18 +08001780 spin_lock(&priv_dev->lock);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001781 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1782 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1783 cdns3_hw_reset_eps_config(priv_dev);
1784 }
1785
1786 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1787 if (priv_dev->gadget_driver &&
1788 priv_dev->gadget_driver->suspend) {
1789 spin_unlock(&priv_dev->lock);
1790 priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1791 spin_lock(&priv_dev->lock);
1792 }
1793 }
1794
1795 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1796 if (priv_dev->gadget_driver &&
1797 priv_dev->gadget_driver->resume) {
1798 spin_unlock(&priv_dev->lock);
1799 priv_dev->gadget_driver->resume(&priv_dev->gadget);
1800 spin_lock(&priv_dev->lock);
1801 }
1802 }
1803
1804 /* reset*/
1805 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1806 if (priv_dev->gadget_driver) {
1807 spin_unlock(&priv_dev->lock);
1808 usb_gadget_udc_reset(&priv_dev->gadget,
1809 priv_dev->gadget_driver);
1810 spin_lock(&priv_dev->lock);
1811
1812 /*read again to check the actual speed*/
1813 speed = cdns3_get_speed(priv_dev);
1814 priv_dev->gadget.speed = speed;
1815 cdns3_hw_reset_eps_config(priv_dev);
1816 cdns3_ep0_config(priv_dev);
1817 }
1818 }
1819}
1820
1821/**
1822 * cdns3_device_irq_handler- interrupt handler for device part of controller
1823 *
1824 * @irq: irq number for cdns3 core device
1825 * @data: structure of cdns3
1826 *
1827 * Returns IRQ_HANDLED or IRQ_NONE
1828 */
1829static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1830{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001831 struct cdns3_device *priv_dev = data;
Pawel Laszczak0b490042020-12-07 11:32:21 +01001832 struct cdns *cdns = dev_get_drvdata(priv_dev->dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001833 irqreturn_t ret = IRQ_NONE;
1834 u32 reg;
1835
Peter Chenb1234e32020-09-02 17:57:32 +08001836 if (cdns->in_lpm)
1837 return ret;
1838
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001839 /* check USB device interrupt */
1840 reg = readl(&priv_dev->regs->usb_ists);
1841 if (reg) {
1842 /* After masking interrupts the new interrupts won't be
1843 * reported in usb_ists/ep_ists. In order to not lose some
1844 * of them driver disables only detected interrupts.
1845 * They will be enabled ASAP after clearing source of
1846 * interrupt. This an unusual behavior only applies to
1847 * usb_ists register.
1848 */
1849 reg = ~reg & readl(&priv_dev->regs->usb_ien);
1850 /* mask deferred interrupt. */
1851 writel(reg, &priv_dev->regs->usb_ien);
1852 ret = IRQ_WAKE_THREAD;
1853 }
1854
1855 /* check endpoint interrupt */
1856 reg = readl(&priv_dev->regs->ep_ists);
1857 if (reg) {
1858 writel(0, &priv_dev->regs->ep_ien);
1859 ret = IRQ_WAKE_THREAD;
1860 }
1861
1862 return ret;
1863}
1864
1865/**
1866 * cdns3_device_thread_irq_handler- interrupt handler for device part
1867 * of controller
1868 *
1869 * @irq: irq number for cdns3 core device
1870 * @data: structure of cdns3
1871 *
1872 * Returns IRQ_HANDLED or IRQ_NONE
1873 */
1874static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1875{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001876 struct cdns3_device *priv_dev = data;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001877 irqreturn_t ret = IRQ_NONE;
1878 unsigned long flags;
Peter Chen06825ca2020-06-23 11:10:01 +08001879 unsigned int bit;
Peter Chen8685c462020-06-23 11:10:00 +08001880 unsigned long reg;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001881
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001882 spin_lock_irqsave(&priv_dev->lock, flags);
1883
1884 reg = readl(&priv_dev->regs->usb_ists);
1885 if (reg) {
1886 writel(reg, &priv_dev->regs->usb_ists);
1887 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1888 cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1889 ret = IRQ_HANDLED;
1890 }
1891
1892 reg = readl(&priv_dev->regs->ep_ists);
1893
1894 /* handle default endpoint OUT */
1895 if (reg & EP_ISTS_EP_OUT0) {
1896 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1897 ret = IRQ_HANDLED;
1898 }
1899
1900 /* handle default endpoint IN */
1901 if (reg & EP_ISTS_EP_IN0) {
1902 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1903 ret = IRQ_HANDLED;
1904 }
1905
1906 /* check if interrupt from non default endpoint, if no exit */
1907 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1908 if (!reg)
1909 goto irqend;
1910
Peter Chen8685c462020-06-23 11:10:00 +08001911 for_each_set_bit(bit, &reg,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001912 sizeof(u32) * BITS_PER_BYTE) {
1913 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1914 ret = IRQ_HANDLED;
1915 }
1916
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001917 if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
1918 cdns3_wa2_check_outq_status(priv_dev);
1919
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001920irqend:
1921 writel(~0, &priv_dev->regs->ep_ien);
1922 spin_unlock_irqrestore(&priv_dev->lock, flags);
1923
1924 return ret;
1925}
1926
1927/**
1928 * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
1929 *
1930 * The real reservation will occur during write to EP_CFG register,
1931 * this function is used to check if the 'size' reservation is allowed.
1932 *
1933 * @priv_dev: extended gadget object
1934 * @size: the size (KB) for EP would like to allocate
1935 * @is_in: endpoint direction
1936 *
1937 * Return 0 if the required size can met or negative value on failure
1938 */
1939static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
1940 int size, int is_in)
1941{
1942 int remained;
1943
1944 /* 2KB are reserved for EP0*/
1945 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
1946
1947 if (is_in) {
1948 if (remained < size)
1949 return -EPERM;
1950
1951 priv_dev->onchip_used_size += size;
1952 } else {
1953 int required;
1954
1955 /**
1956 * ALL OUT EPs are shared the same chunk onchip memory, so
1957 * driver checks if it already has assigned enough buffers
1958 */
1959 if (priv_dev->out_mem_is_allocated >= size)
1960 return 0;
1961
1962 required = size - priv_dev->out_mem_is_allocated;
1963
1964 if (required > remained)
1965 return -EPERM;
1966
1967 priv_dev->out_mem_is_allocated += required;
1968 priv_dev->onchip_used_size += required;
1969 }
1970
1971 return 0;
1972}
1973
Jason Yane9010322020-04-02 20:38:37 +08001974static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +08001975 struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001976{
1977 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
1978
1979 /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
1980 if (priv_dev->dev_ver <= DEV_VER_V2)
1981 writel(USB_CONF_DMULT, &regs->usb_conf);
1982
1983 if (priv_dev->dev_ver == DEV_VER_V2)
1984 writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
1985
1986 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
1987 u32 mask;
1988
1989 if (priv_ep->dir)
1990 mask = BIT(priv_ep->num + 16);
1991 else
1992 mask = BIT(priv_ep->num);
1993
1994 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1995 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
1996 cdns3_set_register_bit(&regs->tdl_beh, mask);
1997 cdns3_set_register_bit(&regs->tdl_beh2, mask);
1998 cdns3_set_register_bit(&regs->dma_adv_td, mask);
1999 }
2000
2001 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2002 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
2003
2004 cdns3_set_register_bit(&regs->dtrans, mask);
2005 }
2006}
2007
2008/**
2009 * cdns3_ep_config Configure hardware endpoint
2010 * @priv_ep: extended endpoint object
Pawel Laszczak52d39672020-10-22 08:55:05 +08002011 * @enable: set EP_CFG_ENABLE bit in ep_cfg register.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002012 */
Pawel Laszczak52d39672020-10-22 08:55:05 +08002013int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002014{
2015 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
2016 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2017 u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
2018 u32 max_packet_size = 0;
2019 u8 maxburst = 0;
2020 u32 ep_cfg = 0;
2021 u8 buffering;
2022 u8 mult = 0;
2023 int ret;
2024
2025 buffering = CDNS3_EP_BUF_SIZE - 1;
2026
2027 cdns3_configure_dmult(priv_dev, priv_ep);
2028
2029 switch (priv_ep->type) {
2030 case USB_ENDPOINT_XFER_INT:
2031 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
2032
2033 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
2034 priv_dev->dev_ver > DEV_VER_V2)
2035 ep_cfg |= EP_CFG_TDL_CHK;
2036 break;
2037 case USB_ENDPOINT_XFER_BULK:
2038 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
2039
2040 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
2041 priv_dev->dev_ver > DEV_VER_V2)
2042 ep_cfg |= EP_CFG_TDL_CHK;
2043 break;
2044 default:
2045 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
2046 mult = CDNS3_EP_ISO_HS_MULT - 1;
2047 buffering = mult + 1;
2048 }
2049
2050 switch (priv_dev->gadget.speed) {
2051 case USB_SPEED_FULL:
2052 max_packet_size = is_iso_ep ? 1023 : 64;
2053 break;
2054 case USB_SPEED_HIGH:
2055 max_packet_size = is_iso_ep ? 1024 : 512;
2056 break;
2057 case USB_SPEED_SUPER:
2058 /* It's limitation that driver assumes in driver. */
2059 mult = 0;
2060 max_packet_size = 1024;
2061 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2062 maxburst = CDNS3_EP_ISO_SS_BURST - 1;
2063 buffering = (mult + 1) *
2064 (maxburst + 1);
2065
2066 if (priv_ep->interval > 1)
2067 buffering++;
2068 } else {
2069 maxburst = CDNS3_EP_BUF_SIZE - 1;
2070 }
2071 break;
2072 default:
2073 /* all other speed are not supported */
Pawel Laszczak52d39672020-10-22 08:55:05 +08002074 return -EINVAL;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002075 }
2076
2077 if (max_packet_size == 1024)
2078 priv_ep->trb_burst_size = 128;
2079 else if (max_packet_size >= 512)
2080 priv_ep->trb_burst_size = 64;
2081 else
2082 priv_ep->trb_burst_size = 16;
2083
Pawel Laszczak52d39672020-10-22 08:55:05 +08002084 /* onchip buffer is only allocated before configuration */
2085 if (!priv_dev->hw_configured_flag) {
2086 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2087 !!priv_ep->dir);
2088 if (ret) {
2089 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2090 return ret;
2091 }
2092 }
2093
2094 if (enable)
2095 ep_cfg |= EP_CFG_ENABLE;
2096
2097 if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2098 if (priv_dev->dev_ver >= DEV_VER_V3) {
2099 u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
2100
2101 /*
2102 * Stream capable endpoints are handled by using ep_tdl
2103 * register. Other endpoints use TDL from TRB feature.
2104 */
2105 cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
2106 mask);
2107 }
2108
2109 /* Enable Stream Bit TDL chk and SID chk */
2110 ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002111 }
2112
2113 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
2114 EP_CFG_MULT(mult) |
2115 EP_CFG_BUFFERING(buffering) |
2116 EP_CFG_MAXBURST(maxburst);
2117
2118 cdns3_select_ep(priv_dev, bEndpointAddress);
2119 writel(ep_cfg, &priv_dev->regs->ep_cfg);
Pawel Laszczak52d39672020-10-22 08:55:05 +08002120 priv_ep->flags |= EP_CONFIGURED;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002121
2122 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
2123 priv_ep->name, ep_cfg);
Pawel Laszczak52d39672020-10-22 08:55:05 +08002124
2125 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002126}
2127
2128/* Find correct direction for HW endpoint according to description */
2129static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
2130 struct cdns3_endpoint *priv_ep)
2131{
2132 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
2133 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
2134}
2135
2136static struct
2137cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
2138 struct usb_endpoint_descriptor *desc)
2139{
2140 struct usb_ep *ep;
2141 struct cdns3_endpoint *priv_ep;
2142
2143 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2144 unsigned long num;
2145 int ret;
2146 /* ep name pattern likes epXin or epXout */
2147 char c[2] = {ep->name[2], '\0'};
2148
2149 ret = kstrtoul(c, 10, &num);
2150 if (ret)
2151 return ERR_PTR(ret);
2152
2153 priv_ep = ep_to_cdns3_ep(ep);
2154 if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
2155 if (!(priv_ep->flags & EP_CLAIMED)) {
2156 priv_ep->num = num;
2157 return priv_ep;
2158 }
2159 }
2160 }
2161
2162 return ERR_PTR(-ENOENT);
2163}
2164
2165/*
2166 * Cadence IP has one limitation that all endpoints must be configured
2167 * (Type & MaxPacketSize) before setting configuration through hardware
2168 * register, it means we can't change endpoints configuration after
2169 * set_configuration.
2170 *
2171 * This function set EP_CLAIMED flag which is added when the gadget driver
2172 * uses usb_ep_autoconfig to configure specific endpoint;
2173 * When the udc driver receives set_configurion request,
2174 * it goes through all claimed endpoints, and configure all endpoints
2175 * accordingly.
2176 *
2177 * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
2178 * ep_cfg register which can be changed after set_configuration, and do
2179 * some software operation accordingly.
2180 */
2181static struct
2182usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
2183 struct usb_endpoint_descriptor *desc,
2184 struct usb_ss_ep_comp_descriptor *comp_desc)
2185{
2186 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2187 struct cdns3_endpoint *priv_ep;
2188 unsigned long flags;
2189
2190 priv_ep = cdns3_find_available_ep(priv_dev, desc);
2191 if (IS_ERR(priv_ep)) {
2192 dev_err(priv_dev->dev, "no available ep\n");
2193 return NULL;
2194 }
2195
2196 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
2197
2198 spin_lock_irqsave(&priv_dev->lock, flags);
2199 priv_ep->endpoint.desc = desc;
2200 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
2201 priv_ep->type = usb_endpoint_type(desc);
2202 priv_ep->flags |= EP_CLAIMED;
2203 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2204
2205 spin_unlock_irqrestore(&priv_dev->lock, flags);
2206 return &priv_ep->endpoint;
2207}
2208
2209/**
2210 * cdns3_gadget_ep_alloc_request Allocates request
2211 * @ep: endpoint object associated with request
2212 * @gfp_flags: gfp flags
2213 *
2214 * Returns allocated request address, NULL on allocation error
2215 */
2216struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
2217 gfp_t gfp_flags)
2218{
2219 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2220 struct cdns3_request *priv_req;
2221
2222 priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
2223 if (!priv_req)
2224 return NULL;
2225
2226 priv_req->priv_ep = priv_ep;
2227
2228 trace_cdns3_alloc_request(priv_req);
2229 return &priv_req->request;
2230}
2231
2232/**
2233 * cdns3_gadget_ep_free_request Free memory occupied by request
2234 * @ep: endpoint object associated with request
2235 * @request: request to free memory
2236 */
2237void cdns3_gadget_ep_free_request(struct usb_ep *ep,
2238 struct usb_request *request)
2239{
2240 struct cdns3_request *priv_req = to_cdns3_request(request);
2241
2242 if (priv_req->aligned_buf)
2243 priv_req->aligned_buf->in_use = 0;
2244
2245 trace_cdns3_free_request(priv_req);
2246 kfree(priv_req);
2247}
2248
2249/**
2250 * cdns3_gadget_ep_enable Enable endpoint
2251 * @ep: endpoint object
2252 * @desc: endpoint descriptor
2253 *
2254 * Returns 0 on success, error code elsewhere
2255 */
2256static int cdns3_gadget_ep_enable(struct usb_ep *ep,
2257 const struct usb_endpoint_descriptor *desc)
2258{
2259 struct cdns3_endpoint *priv_ep;
2260 struct cdns3_device *priv_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002261 const struct usb_ss_ep_comp_descriptor *comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002262 u32 reg = EP_STS_EN_TRBERREN;
2263 u32 bEndpointAddress;
2264 unsigned long flags;
2265 int enable = 1;
Pawel Laszczak52d39672020-10-22 08:55:05 +08002266 int ret = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002267 int val;
2268
2269 priv_ep = ep_to_cdns3_ep(ep);
2270 priv_dev = priv_ep->cdns3_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002271 comp_desc = priv_ep->endpoint.comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002272
2273 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
2274 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
2275 return -EINVAL;
2276 }
2277
2278 if (!desc->wMaxPacketSize) {
2279 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
2280 return -EINVAL;
2281 }
2282
2283 if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
2284 "%s is already enabled\n", priv_ep->name))
2285 return 0;
2286
2287 spin_lock_irqsave(&priv_dev->lock, flags);
2288
2289 priv_ep->endpoint.desc = desc;
2290 priv_ep->type = usb_endpoint_type(desc);
2291 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2292
2293 if (priv_ep->interval > ISO_MAX_INTERVAL &&
2294 priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2295 dev_err(priv_dev->dev, "Driver is limited to %d period\n",
2296 ISO_MAX_INTERVAL);
2297
2298 ret = -EINVAL;
2299 goto exit;
2300 }
2301
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002302 bEndpointAddress = priv_ep->num | priv_ep->dir;
2303 cdns3_select_ep(priv_dev, bEndpointAddress);
2304
Pawel Laszczak52d39672020-10-22 08:55:05 +08002305 /*
2306 * For some versions of controller at some point during ISO OUT traffic
2307 * DMA reads Transfer Ring for the EP which has never got doorbell.
2308 * This issue was detected only on simulation, but to avoid this issue
2309 * driver add protection against it. To fix it driver enable ISO OUT
2310 * endpoint before setting DRBL. This special treatment of ISO OUT
2311 * endpoints are recommended by controller specification.
2312 */
2313 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2314 enable = 0;
2315
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002316 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
2317 /*
2318 * Enable stream support (SS mode) related interrupts
2319 * in EP_STS_EN Register
2320 */
2321 if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2322 reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
2323 EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
2324 EP_STS_EN_STREAMREN;
2325 priv_ep->use_streams = true;
Pawel Laszczak52d39672020-10-22 08:55:05 +08002326 ret = cdns3_ep_config(priv_ep, enable);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002327 priv_dev->using_streams |= true;
2328 }
Pawel Laszczak52d39672020-10-22 08:55:05 +08002329 } else {
2330 ret = cdns3_ep_config(priv_ep, enable);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002331 }
2332
Pawel Laszczak52d39672020-10-22 08:55:05 +08002333 if (ret)
2334 goto exit;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002335
Pawel Laszczak52d39672020-10-22 08:55:05 +08002336 ret = cdns3_allocate_trb_pool(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002337 if (ret)
2338 goto exit;
2339
2340 bEndpointAddress = priv_ep->num | priv_ep->dir;
2341 cdns3_select_ep(priv_dev, bEndpointAddress);
2342
2343 trace_cdns3_gadget_ep_enable(priv_ep);
2344
2345 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2346
2347 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2348 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2349 1, 1000);
2350
2351 if (unlikely(ret)) {
2352 cdns3_free_trb_pool(priv_ep);
2353 ret = -EINVAL;
2354 goto exit;
2355 }
2356
2357 /* enable interrupt for selected endpoint */
2358 cdns3_set_register_bit(&priv_dev->regs->ep_ien,
2359 BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
2360
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002361 if (priv_dev->dev_ver < DEV_VER_V2)
2362 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
2363
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002364 writel(reg, &priv_dev->regs->ep_sts_en);
2365
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002366 ep->desc = desc;
2367 priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002368 EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002369 priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
2370 priv_ep->wa1_set = 0;
2371 priv_ep->enqueue = 0;
2372 priv_ep->dequeue = 0;
2373 reg = readl(&priv_dev->regs->ep_sts);
2374 priv_ep->pcs = !!EP_STS_CCS(reg);
2375 priv_ep->ccs = !!EP_STS_CCS(reg);
2376 /* one TRB is reserved for link TRB used in DMULT mode*/
2377 priv_ep->free_trbs = priv_ep->num_trbs - 1;
2378exit:
2379 spin_unlock_irqrestore(&priv_dev->lock, flags);
2380
2381 return ret;
2382}
2383
2384/**
2385 * cdns3_gadget_ep_disable Disable endpoint
2386 * @ep: endpoint object
2387 *
2388 * Returns 0 on success, error code elsewhere
2389 */
2390static int cdns3_gadget_ep_disable(struct usb_ep *ep)
2391{
2392 struct cdns3_endpoint *priv_ep;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002393 struct cdns3_request *priv_req;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002394 struct cdns3_device *priv_dev;
2395 struct usb_request *request;
2396 unsigned long flags;
2397 int ret = 0;
2398 u32 ep_cfg;
2399 int val;
2400
2401 if (!ep) {
2402 pr_err("usbss: invalid parameters\n");
2403 return -EINVAL;
2404 }
2405
2406 priv_ep = ep_to_cdns3_ep(ep);
2407 priv_dev = priv_ep->cdns3_dev;
2408
2409 if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
2410 "%s is already disabled\n", priv_ep->name))
2411 return 0;
2412
2413 spin_lock_irqsave(&priv_dev->lock, flags);
2414
2415 trace_cdns3_gadget_ep_disable(priv_ep);
2416
2417 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2418
2419 ep_cfg = readl(&priv_dev->regs->ep_cfg);
2420 ep_cfg &= ~EP_CFG_ENABLE;
2421 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2422
2423 /**
2424 * Driver needs some time before resetting endpoint.
2425 * It need waits for clearing DBUSY bit or for timeout expired.
2426 * 10us is enough time for controller to stop transfer.
2427 */
2428 readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
2429 !(val & EP_STS_DBUSY), 1, 10);
2430 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2431
2432 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2433 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2434 1, 1000);
2435 if (unlikely(ret))
2436 dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
2437 priv_ep->name);
2438
2439 while (!list_empty(&priv_ep->pending_req_list)) {
2440 request = cdns3_next_request(&priv_ep->pending_req_list);
2441
2442 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2443 -ESHUTDOWN);
2444 }
2445
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002446 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
2447 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
2448
2449 kfree(priv_req->request.buf);
2450 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
2451 &priv_req->request);
2452 list_del_init(&priv_req->list);
2453 --priv_ep->wa2_counter;
2454 }
2455
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002456 while (!list_empty(&priv_ep->deferred_req_list)) {
2457 request = cdns3_next_request(&priv_ep->deferred_req_list);
2458
2459 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2460 -ESHUTDOWN);
2461 }
2462
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002463 priv_ep->descmis_req = NULL;
2464
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002465 ep->desc = NULL;
2466 priv_ep->flags &= ~EP_ENABLED;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002467 priv_ep->use_streams = false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002468
2469 spin_unlock_irqrestore(&priv_dev->lock, flags);
2470
2471 return ret;
2472}
2473
2474/**
2475 * cdns3_gadget_ep_queue Transfer data on endpoint
2476 * @ep: endpoint object
2477 * @request: request object
2478 * @gfp_flags: gfp flags
2479 *
2480 * Returns 0 on success, error code elsewhere
2481 */
2482static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
2483 struct usb_request *request,
2484 gfp_t gfp_flags)
2485{
2486 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2487 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2488 struct cdns3_request *priv_req;
2489 int ret = 0;
2490
2491 request->actual = 0;
2492 request->status = -EINPROGRESS;
2493 priv_req = to_cdns3_request(request);
2494 trace_cdns3_ep_queue(priv_req);
2495
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002496 if (priv_dev->dev_ver < DEV_VER_V2) {
2497 ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
2498 priv_req);
2499
2500 if (ret == EINPROGRESS)
2501 return 0;
2502 }
2503
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002504 ret = cdns3_prepare_aligned_request_buf(priv_req);
2505 if (ret < 0)
2506 return ret;
2507
2508 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
2509 usb_endpoint_dir_in(ep->desc));
2510 if (ret)
2511 return ret;
2512
2513 list_add_tail(&request->list, &priv_ep->deferred_req_list);
2514
2515 /*
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002516 * For stream capable endpoint if prime irq flag is set then only start
2517 * request.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002518 * If hardware endpoint configuration has not been set yet then
2519 * just queue request in deferred list. Transfer will be started in
2520 * cdns3_set_hw_configuration.
2521 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002522 if (!request->stream_id) {
2523 if (priv_dev->hw_configured_flag &&
2524 !(priv_ep->flags & EP_STALLED) &&
2525 !(priv_ep->flags & EP_STALL_PENDING))
2526 cdns3_start_all_request(priv_dev, priv_ep);
2527 } else {
2528 if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
2529 cdns3_start_all_request(priv_dev, priv_ep);
2530 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002531
2532 return 0;
2533}
2534
2535static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2536 gfp_t gfp_flags)
2537{
2538 struct usb_request *zlp_request;
2539 struct cdns3_endpoint *priv_ep;
2540 struct cdns3_device *priv_dev;
2541 unsigned long flags;
2542 int ret;
2543
2544 if (!request || !ep)
2545 return -EINVAL;
2546
2547 priv_ep = ep_to_cdns3_ep(ep);
2548 priv_dev = priv_ep->cdns3_dev;
2549
2550 spin_lock_irqsave(&priv_dev->lock, flags);
2551
2552 ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
2553
2554 if (ret == 0 && request->zero && request->length &&
2555 (request->length % ep->maxpacket == 0)) {
2556 struct cdns3_request *priv_req;
2557
2558 zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
2559 zlp_request->buf = priv_dev->zlp_buf;
2560 zlp_request->length = 0;
2561
2562 priv_req = to_cdns3_request(zlp_request);
2563 priv_req->flags |= REQUEST_ZLP;
2564
2565 dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
2566 priv_ep->name);
2567 ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
2568 }
2569
2570 spin_unlock_irqrestore(&priv_dev->lock, flags);
2571 return ret;
2572}
2573
2574/**
2575 * cdns3_gadget_ep_dequeue Remove request from transfer queue
2576 * @ep: endpoint object associated with request
2577 * @request: request object
2578 *
2579 * Returns 0 on success, error code elsewhere
2580 */
2581int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
2582 struct usb_request *request)
2583{
2584 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2585 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2586 struct usb_request *req, *req_temp;
2587 struct cdns3_request *priv_req;
2588 struct cdns3_trb *link_trb;
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002589 u8 req_on_hw_ring = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002590 unsigned long flags;
2591 int ret = 0;
2592
2593 if (!ep || !request || !ep->desc)
2594 return -EINVAL;
2595
2596 spin_lock_irqsave(&priv_dev->lock, flags);
2597
2598 priv_req = to_cdns3_request(request);
2599
2600 trace_cdns3_ep_dequeue(priv_req);
2601
2602 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2603
2604 list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
2605 list) {
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002606 if (request == req) {
2607 req_on_hw_ring = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002608 goto found;
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002609 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002610 }
2611
2612 list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
2613 list) {
2614 if (request == req)
2615 goto found;
2616 }
2617
2618 goto not_found;
2619
2620found:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002621 link_trb = priv_req->trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002622
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002623 /* Update ring only if removed request is on pending_req_list list */
Peter Chen95cd7dc2020-04-30 15:07:13 +08002624 if (req_on_hw_ring && link_trb) {
Peter Chen8dafb3c2020-08-21 11:14:37 +08002625 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma +
2626 ((priv_req->end_trb + 1) * TRB_SIZE)));
2627 link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) |
2628 TRB_TYPE(TRB_LINK) | TRB_CHAIN);
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002629
2630 if (priv_ep->wa1_trb == priv_req->trb)
2631 cdns3_wa1_restore_cycle_bit(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002632 }
2633
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002634 cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
2635
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002636not_found:
2637 spin_unlock_irqrestore(&priv_dev->lock, flags);
2638 return ret;
2639}
2640
2641/**
2642 * __cdns3_gadget_ep_set_halt Sets stall on selected endpoint
2643 * Should be called after acquiring spin_lock and selecting ep
Lee Jones4a35aa62020-07-02 15:46:12 +01002644 * @priv_ep: endpoint object to set stall on.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002645 */
2646void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
2647{
2648 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2649
2650 trace_cdns3_halt(priv_ep, 1, 0);
2651
2652 if (!(priv_ep->flags & EP_STALLED)) {
2653 u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
2654
2655 if (!(ep_sts_reg & EP_STS_DBUSY))
2656 cdns3_ep_stall_flush(priv_ep);
2657 else
2658 priv_ep->flags |= EP_STALL_PENDING;
2659 }
2660}
2661
2662/**
2663 * __cdns3_gadget_ep_clear_halt Clears stall on selected endpoint
2664 * Should be called after acquiring spin_lock and selecting ep
Lee Jones4a35aa62020-07-02 15:46:12 +01002665 * @priv_ep: endpoint object to clear stall on
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002666 */
2667int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
2668{
2669 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2670 struct usb_request *request;
Peter Chen4bf2dd62020-02-19 22:14:55 +08002671 struct cdns3_request *priv_req;
2672 struct cdns3_trb *trb = NULL;
Colin Ian King04db1d22019-09-02 15:50:35 +01002673 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002674 int val;
2675
2676 trace_cdns3_halt(priv_ep, 0, 0);
2677
Peter Chen4bf2dd62020-02-19 22:14:55 +08002678 request = cdns3_next_request(&priv_ep->pending_req_list);
2679 if (request) {
2680 priv_req = to_cdns3_request(request);
2681 trb = priv_req->trb;
2682 if (trb)
Peter Chen8dafb3c2020-08-21 11:14:37 +08002683 trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
Peter Chen4bf2dd62020-02-19 22:14:55 +08002684 }
2685
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002686 writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2687
2688 /* wait for EPRST cleared */
Colin Ian King04db1d22019-09-02 15:50:35 +01002689 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2690 !(val & EP_CMD_EPRST), 1, 100);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002691 if (ret)
2692 return -EINVAL;
2693
2694 priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
2695
Peter Chen4bf2dd62020-02-19 22:14:55 +08002696 if (request) {
2697 if (trb)
Peter Chen8dafb3c2020-08-21 11:14:37 +08002698 trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
2699
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002700 cdns3_rearm_transfer(priv_ep, 1);
Peter Chen4bf2dd62020-02-19 22:14:55 +08002701 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002702
2703 cdns3_start_all_request(priv_dev, priv_ep);
2704 return ret;
2705}
2706
2707/**
2708 * cdns3_gadget_ep_set_halt Sets/clears stall on selected endpoint
2709 * @ep: endpoint object to set/clear stall on
2710 * @value: 1 for set stall, 0 for clear stall
2711 *
2712 * Returns 0 on success, error code elsewhere
2713 */
2714int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2715{
2716 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2717 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2718 unsigned long flags;
2719 int ret = 0;
2720
2721 if (!(priv_ep->flags & EP_ENABLED))
2722 return -EPERM;
2723
2724 spin_lock_irqsave(&priv_dev->lock, flags);
2725
2726 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2727
2728 if (!value) {
2729 priv_ep->flags &= ~EP_WEDGE;
2730 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
2731 } else {
2732 __cdns3_gadget_ep_set_halt(priv_ep);
2733 }
2734
2735 spin_unlock_irqrestore(&priv_dev->lock, flags);
2736
2737 return ret;
2738}
2739
2740extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
2741
2742static const struct usb_ep_ops cdns3_gadget_ep_ops = {
2743 .enable = cdns3_gadget_ep_enable,
2744 .disable = cdns3_gadget_ep_disable,
2745 .alloc_request = cdns3_gadget_ep_alloc_request,
2746 .free_request = cdns3_gadget_ep_free_request,
2747 .queue = cdns3_gadget_ep_queue,
2748 .dequeue = cdns3_gadget_ep_dequeue,
2749 .set_halt = cdns3_gadget_ep_set_halt,
2750 .set_wedge = cdns3_gadget_ep_set_wedge,
2751};
2752
2753/**
2754 * cdns3_gadget_get_frame Returns number of actual ITP frame
2755 * @gadget: gadget object
2756 *
2757 * Returns number of actual ITP frame
2758 */
2759static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
2760{
2761 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2762
2763 return readl(&priv_dev->regs->usb_itpn);
2764}
2765
2766int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
2767{
2768 enum usb_device_speed speed;
2769
2770 speed = cdns3_get_speed(priv_dev);
2771
2772 if (speed >= USB_SPEED_SUPER)
2773 return 0;
2774
2775 /* Start driving resume signaling to indicate remote wakeup. */
2776 writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
2777
2778 return 0;
2779}
2780
2781static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
2782{
2783 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2784 unsigned long flags;
2785 int ret = 0;
2786
2787 spin_lock_irqsave(&priv_dev->lock, flags);
2788 ret = __cdns3_gadget_wakeup(priv_dev);
2789 spin_unlock_irqrestore(&priv_dev->lock, flags);
2790 return ret;
2791}
2792
2793static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
2794 int is_selfpowered)
2795{
2796 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2797 unsigned long flags;
2798
2799 spin_lock_irqsave(&priv_dev->lock, flags);
2800 priv_dev->is_selfpowered = !!is_selfpowered;
2801 spin_unlock_irqrestore(&priv_dev->lock, flags);
2802 return 0;
2803}
2804
2805static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
2806{
2807 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2808
Peter Chen0eeda052020-09-01 10:33:50 +08002809 if (is_on) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002810 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
Peter Chen0eeda052020-09-01 10:33:50 +08002811 } else {
2812 writel(~0, &priv_dev->regs->ep_ists);
2813 writel(~0, &priv_dev->regs->usb_ists);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002814 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
Peter Chen0eeda052020-09-01 10:33:50 +08002815 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002816
2817 return 0;
2818}
2819
2820static void cdns3_gadget_config(struct cdns3_device *priv_dev)
2821{
2822 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2823 u32 reg;
2824
2825 cdns3_ep0_config(priv_dev);
2826
2827 /* enable interrupts for endpoint 0 (in and out) */
2828 writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, &regs->ep_ien);
2829
2830 /*
2831 * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
2832 * revision of controller.
2833 */
2834 if (priv_dev->dev_ver == DEV_VER_TI_V1) {
2835 reg = readl(&regs->dbg_link1);
2836
2837 reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
2838 reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
2839 DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
2840 writel(reg, &regs->dbg_link1);
2841 }
2842
2843 /*
2844 * By default some platforms has set protected access to memory.
2845 * This cause problem with cache, so driver restore non-secure
2846 * access to memory.
2847 */
2848 reg = readl(&regs->dma_axi_ctrl);
2849 reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
2850 DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
2851 writel(reg, &regs->dma_axi_ctrl);
2852
2853 /* enable generic interrupt*/
2854 writel(USB_IEN_INIT, &regs->usb_ien);
2855 writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, &regs->usb_conf);
Peter Chenb5148d92020-09-01 10:33:49 +08002856 /* keep Fast Access bit */
2857 writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002858
2859 cdns3_configure_dmult(priv_dev, NULL);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002860}
2861
2862/**
2863 * cdns3_gadget_udc_start Gadget start
2864 * @gadget: gadget object
2865 * @driver: driver which operates on this gadget
2866 *
2867 * Returns 0 on success, error code elsewhere
2868 */
2869static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
2870 struct usb_gadget_driver *driver)
2871{
2872 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2873 unsigned long flags;
Roger Quadros94e259f2019-10-30 14:16:07 +02002874 enum usb_device_speed max_speed = driver->max_speed;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002875
2876 spin_lock_irqsave(&priv_dev->lock, flags);
2877 priv_dev->gadget_driver = driver;
Roger Quadros94e259f2019-10-30 14:16:07 +02002878
2879 /* limit speed if necessary */
2880 max_speed = min(driver->max_speed, gadget->max_speed);
2881
2882 switch (max_speed) {
2883 case USB_SPEED_FULL:
2884 writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
2885 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2886 break;
2887 case USB_SPEED_HIGH:
2888 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2889 break;
2890 case USB_SPEED_SUPER:
2891 break;
2892 default:
2893 dev_err(priv_dev->dev,
2894 "invalid maximum_speed parameter %d\n",
2895 max_speed);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05002896 fallthrough;
Roger Quadros94e259f2019-10-30 14:16:07 +02002897 case USB_SPEED_UNKNOWN:
2898 /* default to superspeed */
2899 max_speed = USB_SPEED_SUPER;
2900 break;
2901 }
2902
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002903 cdns3_gadget_config(priv_dev);
2904 spin_unlock_irqrestore(&priv_dev->lock, flags);
2905 return 0;
2906}
2907
2908/**
2909 * cdns3_gadget_udc_stop Stops gadget
2910 * @gadget: gadget object
2911 *
2912 * Returns 0
2913 */
2914static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
2915{
2916 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2917 struct cdns3_endpoint *priv_ep;
2918 u32 bEndpointAddress;
2919 struct usb_ep *ep;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002920 int val;
2921
2922 priv_dev->gadget_driver = NULL;
2923
2924 priv_dev->onchip_used_size = 0;
2925 priv_dev->out_mem_is_allocated = 0;
2926 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2927
2928 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2929 priv_ep = ep_to_cdns3_ep(ep);
2930 bEndpointAddress = priv_ep->num | priv_ep->dir;
2931 cdns3_select_ep(priv_dev, bEndpointAddress);
2932 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2933 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2934 !(val & EP_CMD_EPRST), 1, 100);
Sanket Parmarf5c8d292019-10-29 12:24:41 +00002935
2936 priv_ep->flags &= ~EP_CLAIMED;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002937 }
2938
2939 /* disable interrupt for device */
2940 writel(0, &priv_dev->regs->usb_ien);
Peter Chenb5148d92020-09-01 10:33:49 +08002941 writel(0, &priv_dev->regs->usb_pwr);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002942 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2943
Xu Wang8e1a2002019-12-20 07:19:38 +00002944 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002945}
2946
2947static const struct usb_gadget_ops cdns3_gadget_ops = {
2948 .get_frame = cdns3_gadget_get_frame,
2949 .wakeup = cdns3_gadget_wakeup,
2950 .set_selfpowered = cdns3_gadget_set_selfpowered,
2951 .pullup = cdns3_gadget_pullup,
2952 .udc_start = cdns3_gadget_udc_start,
2953 .udc_stop = cdns3_gadget_udc_stop,
2954 .match_ep = cdns3_gadget_match_ep,
2955};
2956
2957static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
2958{
2959 int i;
2960
2961 /* ep0 OUT point to ep0 IN. */
2962 priv_dev->eps[16] = NULL;
2963
2964 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
2965 if (priv_dev->eps[i]) {
2966 cdns3_free_trb_pool(priv_dev->eps[i]);
2967 devm_kfree(priv_dev->dev, priv_dev->eps[i]);
2968 }
2969}
2970
2971/**
2972 * cdns3_init_eps Initializes software endpoints of gadget
Lee Jones4a35aa62020-07-02 15:46:12 +01002973 * @priv_dev: extended gadget object
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002974 *
2975 * Returns 0 on success, error code elsewhere
2976 */
2977static int cdns3_init_eps(struct cdns3_device *priv_dev)
2978{
2979 u32 ep_enabled_reg, iso_ep_reg;
2980 struct cdns3_endpoint *priv_ep;
2981 int ep_dir, ep_number;
2982 u32 ep_mask;
2983 int ret = 0;
2984 int i;
2985
2986 /* Read it from USB_CAP3 to USB_CAP5 */
2987 ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
2988 iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
2989
2990 dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
2991
2992 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
2993 ep_dir = i >> 4; /* i div 16 */
2994 ep_number = i & 0xF; /* i % 16 */
2995 ep_mask = BIT(i);
2996
2997 if (!(ep_enabled_reg & ep_mask))
2998 continue;
2999
3000 if (ep_dir && !ep_number) {
3001 priv_dev->eps[i] = priv_dev->eps[0];
3002 continue;
3003 }
3004
3005 priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
3006 GFP_KERNEL);
Colin Ian King4d2233e2019-09-02 19:43:34 +01003007 if (!priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003008 goto err;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003009
3010 /* set parent of endpoint object */
3011 priv_ep->cdns3_dev = priv_dev;
3012 priv_dev->eps[i] = priv_ep;
3013 priv_ep->num = ep_number;
3014 priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
3015
3016 if (!ep_number) {
3017 ret = cdns3_init_ep0(priv_dev, priv_ep);
3018 if (ret) {
3019 dev_err(priv_dev->dev, "Failed to init ep0\n");
3020 goto err;
3021 }
3022 } else {
3023 snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
3024 ep_number, !!ep_dir ? "in" : "out");
3025 priv_ep->endpoint.name = priv_ep->name;
3026
3027 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
3028 CDNS3_EP_MAX_PACKET_LIMIT);
3029 priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
3030 priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
3031 if (ep_dir)
3032 priv_ep->endpoint.caps.dir_in = 1;
3033 else
3034 priv_ep->endpoint.caps.dir_out = 1;
3035
3036 if (iso_ep_reg & ep_mask)
3037 priv_ep->endpoint.caps.type_iso = 1;
3038
3039 priv_ep->endpoint.caps.type_bulk = 1;
3040 priv_ep->endpoint.caps.type_int = 1;
3041
3042 list_add_tail(&priv_ep->endpoint.ep_list,
3043 &priv_dev->gadget.ep_list);
3044 }
3045
3046 priv_ep->flags = 0;
3047
Peter Cheneed6ed62020-03-31 16:10:05 +08003048 dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003049 priv_ep->name,
3050 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
3051 priv_ep->endpoint.caps.type_iso ? "ISO" : "");
3052
3053 INIT_LIST_HEAD(&priv_ep->pending_req_list);
3054 INIT_LIST_HEAD(&priv_ep->deferred_req_list);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01003055 INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003056 }
3057
3058 return 0;
3059err:
3060 cdns3_free_all_eps(priv_dev);
3061 return -ENOMEM;
3062}
3063
Peter Chen6b777892020-08-21 10:55:47 +08003064static void cdns3_gadget_release(struct device *dev)
3065{
3066 struct cdns3_device *priv_dev = container_of(dev,
3067 struct cdns3_device, gadget.dev);
3068
3069 kfree(priv_dev);
3070}
3071
Pawel Laszczak0b490042020-12-07 11:32:21 +01003072static void cdns3_gadget_exit(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003073{
3074 struct cdns3_device *priv_dev;
3075
3076 priv_dev = cdns->gadget_dev;
3077
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003078
3079 pm_runtime_mark_last_busy(cdns->dev);
3080 pm_runtime_put_autosuspend(cdns->dev);
3081
Peter Chen6b777892020-08-21 10:55:47 +08003082 usb_del_gadget(&priv_dev->gadget);
Peter Chen98df91f2020-09-01 10:35:49 +08003083 devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003084
3085 cdns3_free_all_eps(priv_dev);
3086
3087 while (!list_empty(&priv_dev->aligned_buf_list)) {
3088 struct cdns3_aligned_buf *buf;
3089
3090 buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
3091 dma_free_coherent(priv_dev->sysdev, buf->size,
3092 buf->buf,
3093 buf->dma);
3094
3095 list_del(&buf->list);
3096 kfree(buf);
3097 }
3098
3099 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3100 priv_dev->setup_dma);
Sanket Parmarb9b1eae2021-03-09 06:19:39 +01003101 dma_pool_destroy(priv_dev->eps_dma_pool);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003102
3103 kfree(priv_dev->zlp_buf);
Peter Chen6b777892020-08-21 10:55:47 +08003104 usb_put_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003105 cdns->gadget_dev = NULL;
Pawel Laszczak0b490042020-12-07 11:32:21 +01003106 cdns_drd_gadget_off(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003107}
3108
Pawel Laszczak0b490042020-12-07 11:32:21 +01003109static int cdns3_gadget_start(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003110{
3111 struct cdns3_device *priv_dev;
3112 u32 max_speed;
3113 int ret;
3114
3115 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
3116 if (!priv_dev)
3117 return -ENOMEM;
3118
Peter Chen6b777892020-08-21 10:55:47 +08003119 usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
3120 cdns3_gadget_release);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003121 cdns->gadget_dev = priv_dev;
3122 priv_dev->sysdev = cdns->dev;
3123 priv_dev->dev = cdns->dev;
3124 priv_dev->regs = cdns->dev_regs;
3125
3126 device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
3127 &priv_dev->onchip_buffers);
3128
3129 if (priv_dev->onchip_buffers <= 0) {
3130 u32 reg = readl(&priv_dev->regs->usb_cap2);
3131
3132 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
3133 }
3134
3135 if (!priv_dev->onchip_buffers)
3136 priv_dev->onchip_buffers = 256;
3137
3138 max_speed = usb_get_maximum_speed(cdns->dev);
3139
3140 /* Check the maximum_speed parameter */
3141 switch (max_speed) {
3142 case USB_SPEED_FULL:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003143 case USB_SPEED_HIGH:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003144 case USB_SPEED_SUPER:
3145 break;
3146 default:
3147 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
3148 max_speed);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05003149 fallthrough;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003150 case USB_SPEED_UNKNOWN:
3151 /* default to superspeed */
3152 max_speed = USB_SPEED_SUPER;
3153 break;
3154 }
3155
3156 /* fill gadget fields */
3157 priv_dev->gadget.max_speed = max_speed;
3158 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3159 priv_dev->gadget.ops = &cdns3_gadget_ops;
3160 priv_dev->gadget.name = "usb-ss-gadget";
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003161 priv_dev->gadget.quirk_avoids_skb_reserve = 1;
Peter Chen77f30ff2020-05-10 13:30:42 +08003162 priv_dev->gadget.irq = cdns->dev_irq;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003163
3164 spin_lock_init(&priv_dev->lock);
3165 INIT_WORK(&priv_dev->pending_status_wq,
3166 cdns3_pending_setup_status_handler);
3167
3168 INIT_WORK(&priv_dev->aligned_buf_wq,
3169 cdns3_free_aligned_request_buf);
3170
3171 /* initialize endpoint container */
3172 INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
3173 INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
Sanket Parmarb9b1eae2021-03-09 06:19:39 +01003174 priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
3175 priv_dev->sysdev,
3176 TRB_RING_SIZE, 8, 0);
3177 if (!priv_dev->eps_dma_pool) {
3178 dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
3179 ret = -ENOMEM;
3180 goto err1;
3181 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003182
3183 ret = cdns3_init_eps(priv_dev);
3184 if (ret) {
3185 dev_err(priv_dev->dev, "Failed to create endpoints\n");
3186 goto err1;
3187 }
3188
3189 /* allocate memory for setup packet buffer */
3190 priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
3191 &priv_dev->setup_dma, GFP_DMA);
3192 if (!priv_dev->setup_buf) {
3193 ret = -ENOMEM;
3194 goto err2;
3195 }
3196
3197 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
3198
3199 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
3200 readl(&priv_dev->regs->usb_cap6));
3201 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
3202 readl(&priv_dev->regs->usb_cap1));
Colin Ian King5d041112019-09-03 13:07:10 +01003203 dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003204 readl(&priv_dev->regs->usb_cap2));
3205
3206 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
Peter Chend6be7c92020-09-10 17:11:29 +08003207 if (priv_dev->dev_ver >= DEV_VER_V2)
3208 priv_dev->gadget.sg_supported = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003209
3210 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
3211 if (!priv_dev->zlp_buf) {
3212 ret = -ENOMEM;
3213 goto err3;
3214 }
3215
3216 /* add USB gadget device */
Peter Chen6b777892020-08-21 10:55:47 +08003217 ret = usb_add_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003218 if (ret < 0) {
Peter Chen6b777892020-08-21 10:55:47 +08003219 dev_err(priv_dev->dev, "Failed to add gadget\n");
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003220 goto err4;
3221 }
3222
3223 return 0;
3224err4:
3225 kfree(priv_dev->zlp_buf);
3226err3:
3227 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3228 priv_dev->setup_dma);
3229err2:
3230 cdns3_free_all_eps(priv_dev);
3231err1:
Sanket Parmarb9b1eae2021-03-09 06:19:39 +01003232 dma_pool_destroy(priv_dev->eps_dma_pool);
3233
Peter Chen6b777892020-08-21 10:55:47 +08003234 usb_put_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003235 cdns->gadget_dev = NULL;
3236 return ret;
3237}
3238
Pawel Laszczak0b490042020-12-07 11:32:21 +01003239static int __cdns3_gadget_init(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003240{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003241 int ret = 0;
3242
Pawel Laszczakeb21a742019-10-07 13:03:23 +01003243 /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
3244 ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
3245 if (ret) {
3246 dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
3247 return ret;
3248 }
3249
Pawel Laszczak0b490042020-12-07 11:32:21 +01003250 cdns_drd_gadget_on(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003251 pm_runtime_get_sync(cdns->dev);
3252
3253 ret = cdns3_gadget_start(cdns);
3254 if (ret)
3255 return ret;
3256
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003257 /*
3258 * Because interrupt line can be shared with other components in
3259 * driver it can't use IRQF_ONESHOT flag here.
3260 */
3261 ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
3262 cdns3_device_irq_handler,
3263 cdns3_device_thread_irq_handler,
Peter Chenaf58e1f2019-12-27 17:10:04 +08003264 IRQF_SHARED, dev_name(cdns->dev),
3265 cdns->gadget_dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003266
3267 if (ret)
3268 goto err0;
3269
3270 return 0;
3271err0:
3272 cdns3_gadget_exit(cdns);
3273 return ret;
3274}
3275
Pawel Laszczak0b490042020-12-07 11:32:21 +01003276static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup)
Peter Chene11d2bf2020-10-29 17:55:18 +08003277__must_hold(&cdns->lock)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003278{
3279 struct cdns3_device *priv_dev = cdns->gadget_dev;
3280
Peter Chene11d2bf2020-10-29 17:55:18 +08003281 spin_unlock(&cdns->lock);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003282 cdns3_disconnect_gadget(priv_dev);
Peter Chene11d2bf2020-10-29 17:55:18 +08003283 spin_lock(&cdns->lock);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003284
3285 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3286 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
3287 cdns3_hw_reset_eps_config(priv_dev);
3288
3289 /* disable interrupt for device */
3290 writel(0, &priv_dev->regs->usb_ien);
3291
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003292 return 0;
3293}
3294
Pawel Laszczak0b490042020-12-07 11:32:21 +01003295static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003296{
3297 struct cdns3_device *priv_dev = cdns->gadget_dev;
3298
3299 if (!priv_dev->gadget_driver)
3300 return 0;
3301
3302 cdns3_gadget_config(priv_dev);
Frank Li2cf25812021-02-18 16:51:08 -06003303 if (hibernated)
3304 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003305
3306 return 0;
3307}
3308
3309/**
3310 * cdns3_gadget_init - initialize device structure
3311 *
Pawel Laszczak0b490042020-12-07 11:32:21 +01003312 * @cdns: cdns instance
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003313 *
3314 * This function initializes the gadget.
3315 */
Pawel Laszczak0b490042020-12-07 11:32:21 +01003316int cdns3_gadget_init(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003317{
Pawel Laszczak0b490042020-12-07 11:32:21 +01003318 struct cdns_role_driver *rdrv;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003319
3320 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
3321 if (!rdrv)
3322 return -ENOMEM;
3323
3324 rdrv->start = __cdns3_gadget_init;
3325 rdrv->stop = cdns3_gadget_exit;
3326 rdrv->suspend = cdns3_gadget_suspend;
3327 rdrv->resume = cdns3_gadget_resume;
Pawel Laszczak0b490042020-12-07 11:32:21 +01003328 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003329 rdrv->name = "gadget";
3330 cdns->roles[USB_ROLE_DEVICE] = rdrv;
3331
3332 return 0;
3333}