blob: 692acf7b9b148d340dfab5664b39e42a4546a82f [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>
62#include <linux/iopoll.h>
63
64#include "core.h"
65#include "gadget-export.h"
66#include "gadget.h"
67#include "trace.h"
68#include "drd.h"
69
70static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
71 struct usb_request *request,
72 gfp_t gfp_flags);
73
Jayshri Pawar54c4c692019-12-13 06:25:42 +010074static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
75 struct usb_request *request);
76
77static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
78 struct usb_request *request);
79
80/**
81 * cdns3_clear_register_bit - clear bit in given register.
82 * @ptr: address of device controller register to be read and changed
83 * @mask: bits requested to clar
84 */
Jason Yane9010322020-04-02 20:38:37 +080085static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask)
Jayshri Pawar54c4c692019-12-13 06:25:42 +010086{
87 mask = readl(ptr) & ~mask;
88 writel(mask, ptr);
89}
90
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010091/**
92 * cdns3_set_register_bit - set bit in given register.
93 * @ptr: address of device controller register to be read and changed
94 * @mask: bits requested to set
95 */
96void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
97{
98 mask = readl(ptr) | mask;
99 writel(mask, ptr);
100}
101
102/**
103 * cdns3_ep_addr_to_index - Macro converts endpoint address to
104 * index of endpoint object in cdns3_device.eps[] container
105 * @ep_addr: endpoint address for which endpoint object is required
106 *
107 */
108u8 cdns3_ep_addr_to_index(u8 ep_addr)
109{
110 return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
111}
112
113static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
114 struct cdns3_endpoint *priv_ep)
115{
116 int dma_index;
117
118 dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
119
120 return dma_index / TRB_SIZE;
121}
122
123/**
124 * cdns3_next_request - returns next request from list
125 * @list: list containing requests
126 *
127 * Returns request or NULL if no requests in list
128 */
129struct usb_request *cdns3_next_request(struct list_head *list)
130{
131 return list_first_entry_or_null(list, struct usb_request, list);
132}
133
134/**
135 * cdns3_next_align_buf - returns next buffer from list
136 * @list: list containing buffers
137 *
138 * Returns buffer or NULL if no buffers in list
139 */
Jason Yane9010322020-04-02 20:38:37 +0800140static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100141{
142 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
143}
144
145/**
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100146 * cdns3_next_priv_request - returns next request from list
147 * @list: list containing requests
148 *
149 * Returns request or NULL if no requests in list
150 */
Jason Yane9010322020-04-02 20:38:37 +0800151static struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100152{
153 return list_first_entry_or_null(list, struct cdns3_request, list);
154}
155
156/**
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100157 * select_ep - selects endpoint
158 * @priv_dev: extended gadget object
159 * @ep: endpoint address
160 */
161void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
162{
163 if (priv_dev->selected_ep == ep)
164 return;
165
166 priv_dev->selected_ep = ep;
167 writel(ep, &priv_dev->regs->ep_sel);
168}
169
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100170/**
171 * cdns3_get_tdl - gets current tdl for selected endpoint.
172 * @priv_dev: extended gadget object
173 *
174 * Before calling this function the appropriate endpoint must
175 * be selected by means of cdns3_select_ep function.
176 */
177static int cdns3_get_tdl(struct cdns3_device *priv_dev)
178{
179 if (priv_dev->dev_ver < DEV_VER_V3)
180 return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
181 else
182 return readl(&priv_dev->regs->ep_tdl);
183}
184
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100185dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
186 struct cdns3_trb *trb)
187{
188 u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
189
190 return priv_ep->trb_pool_dma + offset;
191}
192
Jason Yane9010322020-04-02 20:38:37 +0800193static int cdns3_ring_size(struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100194{
195 switch (priv_ep->type) {
196 case USB_ENDPOINT_XFER_ISOC:
197 return TRB_ISO_RING_SIZE;
198 case USB_ENDPOINT_XFER_CONTROL:
199 return TRB_CTRL_RING_SIZE;
200 default:
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100201 if (priv_ep->use_streams)
202 return TRB_STREAM_RING_SIZE;
203 else
204 return TRB_RING_SIZE;
205 }
206}
207
208static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
209{
210 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
211
212 if (priv_ep->trb_pool) {
213 dma_free_coherent(priv_dev->sysdev,
214 cdns3_ring_size(priv_ep),
215 priv_ep->trb_pool, priv_ep->trb_pool_dma);
216 priv_ep->trb_pool = NULL;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100217 }
218}
219
220/**
221 * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint
222 * @priv_ep: endpoint object
223 *
224 * Function will return 0 on success or -ENOMEM on allocation error
225 */
226int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
227{
228 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
229 int ring_size = cdns3_ring_size(priv_ep);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100230 int num_trbs = ring_size / TRB_SIZE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100231 struct cdns3_trb *link_trb;
232
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100233 if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size)
234 cdns3_free_trb_pool(priv_ep);
235
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100236 if (!priv_ep->trb_pool) {
237 priv_ep->trb_pool = dma_alloc_coherent(priv_dev->sysdev,
238 ring_size,
239 &priv_ep->trb_pool_dma,
240 GFP_DMA32 | GFP_ATOMIC);
241 if (!priv_ep->trb_pool)
242 return -ENOMEM;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100243
244 priv_ep->alloc_ring_size = ring_size;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100245 }
246
Peter Chen95f5acf2020-07-22 11:06:19 +0800247 memset(priv_ep->trb_pool, 0, ring_size);
248
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100249 priv_ep->num_trbs = num_trbs;
250
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100251 if (!priv_ep->num)
252 return 0;
253
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100254 /* Initialize the last TRB as Link TRB */
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100255 link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100256
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100257 if (priv_ep->use_streams) {
258 /*
259 * For stream capable endpoints driver use single correct TRB.
260 * The last trb has zeroed cycle bit
261 */
262 link_trb->control = 0;
263 } else {
Peter Chen8dafb3c2020-08-21 11:14:37 +0800264 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma));
265 link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100266 }
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100267 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100268}
269
270/**
271 * cdns3_ep_stall_flush - Stalls and flushes selected endpoint
272 * @priv_ep: endpoint object
273 *
274 * Endpoint must be selected before call to this function
275 */
276static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
277{
278 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
279 int val;
280
281 trace_cdns3_halt(priv_ep, 1, 1);
282
283 writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
284 &priv_dev->regs->ep_cmd);
285
286 /* wait for DFLUSH cleared */
287 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
288 !(val & EP_CMD_DFLUSH), 1, 1000);
289 priv_ep->flags |= EP_STALLED;
290 priv_ep->flags &= ~EP_STALL_PENDING;
291}
292
293/**
294 * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller.
295 * @priv_dev: extended gadget object
296 */
297void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
298{
299 writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
300
301 cdns3_allow_enable_l1(priv_dev, 0);
302 priv_dev->hw_configured_flag = 0;
303 priv_dev->onchip_used_size = 0;
304 priv_dev->out_mem_is_allocated = 0;
305 priv_dev->wait_for_setup = 0;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100306 priv_dev->using_streams = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100307}
308
309/**
310 * cdns3_ep_inc_trb - increment a trb index.
311 * @index: Pointer to the TRB index to increment.
312 * @cs: Cycle state
313 * @trb_in_seg: number of TRBs in segment
314 *
315 * The index should never point to the link TRB. After incrementing,
316 * if it is point to the link TRB, wrap around to the beginning and revert
317 * cycle state bit The
318 * link TRB is always at the last TRB entry.
319 */
320static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
321{
322 (*index)++;
323 if (*index == (trb_in_seg - 1)) {
324 *index = 0;
325 *cs ^= 1;
326 }
327}
328
329/**
330 * cdns3_ep_inc_enq - increment endpoint's enqueue pointer
331 * @priv_ep: The endpoint whose enqueue pointer we're incrementing
332 */
333static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
334{
335 priv_ep->free_trbs--;
336 cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
337}
338
339/**
340 * cdns3_ep_inc_deq - increment endpoint's dequeue pointer
341 * @priv_ep: The endpoint whose dequeue pointer we're incrementing
342 */
343static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
344{
345 priv_ep->free_trbs++;
346 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
347}
348
Jason Yane9010322020-04-02 20:38:37 +0800349static void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100350{
351 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
352 int current_trb = priv_req->start_trb;
353
354 while (current_trb != priv_req->end_trb) {
355 cdns3_ep_inc_deq(priv_ep);
356 current_trb = priv_ep->dequeue;
357 }
358
359 cdns3_ep_inc_deq(priv_ep);
360}
361
362/**
363 * cdns3_allow_enable_l1 - enable/disable permits to transition to L1.
364 * @priv_dev: Extended gadget object
365 * @enable: Enable/disable permit to transition to L1.
366 *
367 * If bit USB_CONF_L1EN is set and device receive Extended Token packet,
368 * then controller answer with ACK handshake.
369 * If bit USB_CONF_L1DS is set and device receive Extended Token packet,
370 * then controller answer with NYET handshake.
371 */
372void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
373{
374 if (enable)
375 writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
376 else
377 writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
378}
379
380enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
381{
382 u32 reg;
383
384 reg = readl(&priv_dev->regs->usb_sts);
385
386 if (DEV_SUPERSPEED(reg))
387 return USB_SPEED_SUPER;
388 else if (DEV_HIGHSPEED(reg))
389 return USB_SPEED_HIGH;
390 else if (DEV_FULLSPEED(reg))
391 return USB_SPEED_FULL;
392 else if (DEV_LOWSPEED(reg))
393 return USB_SPEED_LOW;
394 return USB_SPEED_UNKNOWN;
395}
396
397/**
398 * cdns3_start_all_request - add to ring all request not started
399 * @priv_dev: Extended gadget object
400 * @priv_ep: The endpoint for whom request will be started.
401 *
402 * Returns return ENOMEM if transfer ring i not enough TRBs to start
403 * all requests.
404 */
405static int cdns3_start_all_request(struct cdns3_device *priv_dev,
406 struct cdns3_endpoint *priv_ep)
407{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100408 struct usb_request *request;
409 int ret = 0;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100410 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
411
412 /*
413 * If the last pending transfer is INTERNAL
414 * OR streams are enabled for this endpoint
415 * do NOT start new transfer till the last one is pending
416 */
417 if (!pending_empty) {
418 struct cdns3_request *priv_req;
419
420 request = cdns3_next_request(&priv_ep->pending_req_list);
421 priv_req = to_cdns3_request(request);
422 if ((priv_req->flags & REQUEST_INTERNAL) ||
423 (priv_ep->flags & EP_TDLCHK_EN) ||
424 priv_ep->use_streams) {
Nicolas Boichatb3a5ce82020-06-27 15:03:04 +0800425 dev_dbg(priv_dev->dev, "Blocking external request\n");
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100426 return ret;
427 }
428 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100429
430 while (!list_empty(&priv_ep->deferred_req_list)) {
431 request = cdns3_next_request(&priv_ep->deferred_req_list);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100432
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100433 if (!priv_ep->use_streams) {
434 ret = cdns3_ep_run_transfer(priv_ep, request);
435 } else {
436 priv_ep->stream_sg_idx = 0;
437 ret = cdns3_ep_run_stream_transfer(priv_ep, request);
438 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100439 if (ret)
440 return ret;
441
442 list_del(&request->list);
443 list_add_tail(&request->list,
444 &priv_ep->pending_req_list);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100445 if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN))
446 break;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100447 }
448
449 priv_ep->flags &= ~EP_RING_FULL;
450 return ret;
451}
452
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100453/*
454 * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set
455 * driver try to detect whether endpoint need additional internal
456 * buffer for unblocking on-chip FIFO buffer. This flag will be cleared
457 * if before first DESCMISS interrupt the DMA will be armed.
458 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100459#define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100460 if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
461 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
462 (reg) |= EP_STS_EN_DESCMISEN; \
463 } } while (0)
464
Peter Chen141e70f2020-09-10 17:11:28 +0800465static void __cdns3_descmiss_copy_data(struct usb_request *request,
466 struct usb_request *descmiss_req)
467{
468 int length = request->actual + descmiss_req->actual;
469 struct scatterlist *s = request->sg;
470
471 if (!s) {
472 if (length <= request->length) {
473 memcpy(&((u8 *)request->buf)[request->actual],
474 descmiss_req->buf,
475 descmiss_req->actual);
476 request->actual = length;
477 } else {
478 /* It should never occures */
479 request->status = -ENOMEM;
480 }
481 } else {
482 if (length <= sg_dma_len(s)) {
483 void *p = phys_to_virt(sg_dma_address(s));
484
485 memcpy(&((u8 *)p)[request->actual],
486 descmiss_req->buf,
487 descmiss_req->actual);
488 request->actual = length;
489 } else {
490 request->status = -ENOMEM;
491 }
492 }
493}
494
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100495/**
496 * cdns3_wa2_descmiss_copy_data copy data from internal requests to
497 * request queued by class driver.
498 * @priv_ep: extended endpoint object
499 * @request: request object
500 */
501static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
502 struct usb_request *request)
503{
504 struct usb_request *descmiss_req;
505 struct cdns3_request *descmiss_priv_req;
506
507 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
508 int chunk_end;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100509
510 descmiss_priv_req =
511 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
512 descmiss_req = &descmiss_priv_req->request;
513
514 /* driver can't touch pending request */
515 if (descmiss_priv_req->flags & REQUEST_PENDING)
516 break;
517
518 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100519 request->status = descmiss_req->status;
Peter Chen141e70f2020-09-10 17:11:28 +0800520 __cdns3_descmiss_copy_data(request, descmiss_req);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100521 list_del_init(&descmiss_priv_req->list);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100522 kfree(descmiss_req->buf);
523 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
524 --priv_ep->wa2_counter;
525
526 if (!chunk_end)
527 break;
528 }
529}
530
Jason Yane9010322020-04-02 20:38:37 +0800531static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800532 struct cdns3_endpoint *priv_ep,
533 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100534{
535 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
536 priv_req->flags & REQUEST_INTERNAL) {
537 struct usb_request *req;
538
539 req = cdns3_next_request(&priv_ep->deferred_req_list);
540
541 priv_ep->descmis_req = NULL;
542
543 if (!req)
544 return NULL;
545
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100546 /* unmap the gadget request before copying data */
547 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
548 priv_ep->dir);
549
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100550 cdns3_wa2_descmiss_copy_data(priv_ep, req);
551 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
552 req->length != req->actual) {
553 /* wait for next part of transfer */
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100554 /* re-map the gadget request buffer*/
555 usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
556 usb_endpoint_dir_in(priv_ep->endpoint.desc));
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100557 return NULL;
558 }
559
560 if (req->status == -EINPROGRESS)
561 req->status = 0;
562
563 list_del_init(&req->list);
564 cdns3_start_all_request(priv_dev, priv_ep);
565 return req;
566 }
567
568 return &priv_req->request;
569}
570
Jason Yane9010322020-04-02 20:38:37 +0800571static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800572 struct cdns3_endpoint *priv_ep,
573 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100574{
575 int deferred = 0;
576
577 /*
578 * If transfer was queued before DESCMISS appear than we
579 * can disable handling of DESCMISS interrupt. Driver assumes that it
580 * can disable special treatment for this endpoint.
581 */
582 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
583 u32 reg;
584
585 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
586 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
587 reg = readl(&priv_dev->regs->ep_sts_en);
588 reg &= ~EP_STS_EN_DESCMISEN;
589 trace_cdns3_wa2(priv_ep, "workaround disabled\n");
590 writel(reg, &priv_dev->regs->ep_sts_en);
591 }
592
593 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
594 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
595 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
596
597 /*
598 * DESCMISS transfer has been finished, so data will be
599 * directly copied from internal allocated usb_request
600 * objects.
601 */
602 if (pending_empty && !descmiss_empty &&
603 !(priv_req->flags & REQUEST_INTERNAL)) {
604 cdns3_wa2_descmiss_copy_data(priv_ep,
605 &priv_req->request);
606
607 trace_cdns3_wa2(priv_ep, "get internal stored data");
608
609 list_add_tail(&priv_req->request.list,
610 &priv_ep->pending_req_list);
611 cdns3_gadget_giveback(priv_ep, priv_req,
612 priv_req->request.status);
613
614 /*
615 * Intentionally driver returns positive value as
616 * correct value. It informs that transfer has
617 * been finished.
618 */
619 return EINPROGRESS;
620 }
621
622 /*
623 * Driver will wait for completion DESCMISS transfer,
624 * before starts new, not DESCMISS transfer.
625 */
626 if (!pending_empty && !descmiss_empty) {
627 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
628 deferred = 1;
629 }
630
631 if (priv_req->flags & REQUEST_INTERNAL)
632 list_add_tail(&priv_req->list,
633 &priv_ep->wa2_descmiss_req_list);
634 }
635
636 return deferred;
637}
638
639static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
640{
641 struct cdns3_request *priv_req;
642
643 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
644 u8 chain;
645
646 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
647 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
648
649 trace_cdns3_wa2(priv_ep, "removes eldest request");
650
651 kfree(priv_req->request.buf);
652 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
653 &priv_req->request);
654 list_del_init(&priv_req->list);
655 --priv_ep->wa2_counter;
656
657 if (!chain)
658 break;
659 }
660}
661
662/**
663 * cdns3_wa2_descmissing_packet - handles descriptor missing event.
Lee Jones4a35aa62020-07-02 15:46:12 +0100664 * @priv_ep: extended gadget object
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100665 *
666 * This function is used only for WA2. For more information see Work around 2
667 * description.
668 */
669static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
670{
671 struct cdns3_request *priv_req;
672 struct usb_request *request;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100673 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
674
675 /* check for pending transfer */
676 if (!pending_empty) {
677 trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
678 return;
679 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100680
681 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
682 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
683 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
684 }
685
686 trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
687
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100688 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
689 trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100690 cdns3_wa2_remove_old_request(priv_ep);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100691 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100692
693 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
694 GFP_ATOMIC);
695 if (!request)
696 goto err;
697
698 priv_req = to_cdns3_request(request);
699 priv_req->flags |= REQUEST_INTERNAL;
700
701 /* if this field is still assigned it indicate that transfer related
702 * with this request has not been finished yet. Driver in this
703 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
704 * flag to previous one. It will indicate that current request is
705 * part of the previous one.
706 */
707 if (priv_ep->descmis_req)
708 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
709
710 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
711 GFP_ATOMIC);
712 priv_ep->wa2_counter++;
713
714 if (!priv_req->request.buf) {
715 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
716 goto err;
717 }
718
719 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
720 priv_ep->descmis_req = priv_req;
721
722 __cdns3_gadget_ep_queue(&priv_ep->endpoint,
723 &priv_ep->descmis_req->request,
724 GFP_ATOMIC);
725
726 return;
727
728err:
729 dev_err(priv_ep->cdns3_dev->dev,
730 "Failed: No sufficient memory for DESCMIS\n");
731}
732
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100733static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
734{
735 u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
736
737 if (tdl) {
738 u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
739
740 writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
741 &priv_dev->regs->ep_cmd);
742 }
743}
744
745static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
746{
747 u32 ep_sts_reg;
748
749 /* select EP0-out */
750 cdns3_select_ep(priv_dev, 0);
751
752 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
753
754 if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
755 u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
756 struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
757
758 if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
759 outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
760 u8 pending_empty = list_empty(&outq_ep->pending_req_list);
761
762 if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
763 (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
764 !pending_empty) {
765 } else {
766 u32 ep_sts_en_reg;
767 u32 ep_cmd_reg;
768
769 cdns3_select_ep(priv_dev, outq_ep->num |
770 outq_ep->dir);
771 ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
772 ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
773
774 outq_ep->flags |= EP_TDLCHK_EN;
775 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
776 EP_CFG_TDL_CHK);
777
778 cdns3_wa2_enable_detection(priv_dev, outq_ep,
779 ep_sts_en_reg);
780 writel(ep_sts_en_reg,
781 &priv_dev->regs->ep_sts_en);
782 /* reset tdl value to zero */
783 cdns3_wa2_reset_tdl(priv_dev);
784 /*
785 * Memory barrier - Reset tdl before ringing the
786 * doorbell.
787 */
788 wmb();
789 if (EP_CMD_DRDY & ep_cmd_reg) {
790 trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
791
792 } else {
793 trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
794 /*
795 * ring doorbell to generate DESCMIS irq
796 */
797 writel(EP_CMD_DRDY,
798 &priv_dev->regs->ep_cmd);
799 }
800 }
801 }
802 }
803}
804
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100805/**
806 * cdns3_gadget_giveback - call struct usb_request's ->complete callback
807 * @priv_ep: The endpoint to whom the request belongs to
808 * @priv_req: The request we're giving back
809 * @status: completion code for the request
810 *
811 * Must be called with controller's lock held and interrupts disabled. This
812 * function will unmap @req and call its ->complete() callback to notify upper
813 * layers that it has completed.
814 */
815void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
816 struct cdns3_request *priv_req,
817 int status)
818{
819 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
820 struct usb_request *request = &priv_req->request;
821
822 list_del_init(&request->list);
823
824 if (request->status == -EINPROGRESS)
825 request->status = status;
826
827 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
828 priv_ep->dir);
829
830 if ((priv_req->flags & REQUEST_UNALIGNED) &&
831 priv_ep->dir == USB_DIR_OUT && !request->status)
832 memcpy(request->buf, priv_req->aligned_buf->buf,
833 request->length);
834
835 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
Peter Chen249f0a22020-09-10 17:11:27 +0800836 /* All TRBs have finished, clear the counter */
837 priv_req->finished_trb = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100838 trace_cdns3_gadget_giveback(priv_req);
839
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100840 if (priv_dev->dev_ver < DEV_VER_V2) {
841 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
842 priv_req);
843 if (!request)
844 return;
845 }
846
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100847 if (request->complete) {
848 spin_unlock(&priv_dev->lock);
849 usb_gadget_giveback_request(&priv_ep->endpoint,
850 request);
851 spin_lock(&priv_dev->lock);
852 }
853
854 if (request->buf == priv_dev->zlp_buf)
855 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
856}
857
Jason Yane9010322020-04-02 20:38:37 +0800858static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100859{
860 /* Work around for stale data address in TRB*/
861 if (priv_ep->wa1_set) {
862 trace_cdns3_wa1(priv_ep, "restore cycle bit");
863
864 priv_ep->wa1_set = 0;
865 priv_ep->wa1_trb_index = 0xFFFF;
866 if (priv_ep->wa1_cycle_bit) {
867 priv_ep->wa1_trb->control =
Peter Chen8dafb3c2020-08-21 11:14:37 +0800868 priv_ep->wa1_trb->control | cpu_to_le32(0x1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100869 } else {
870 priv_ep->wa1_trb->control =
Peter Chen8dafb3c2020-08-21 11:14:37 +0800871 priv_ep->wa1_trb->control & cpu_to_le32(~0x1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100872 }
873 }
874}
875
876static void cdns3_free_aligned_request_buf(struct work_struct *work)
877{
878 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
879 aligned_buf_wq);
880 struct cdns3_aligned_buf *buf, *tmp;
881 unsigned long flags;
882
883 spin_lock_irqsave(&priv_dev->lock, flags);
884
885 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
886 if (!buf->in_use) {
887 list_del(&buf->list);
888
889 /*
890 * Re-enable interrupts to free DMA capable memory.
891 * Driver can't free this memory with disabled
892 * interrupts.
893 */
894 spin_unlock_irqrestore(&priv_dev->lock, flags);
895 dma_free_coherent(priv_dev->sysdev, buf->size,
896 buf->buf, buf->dma);
897 kfree(buf);
898 spin_lock_irqsave(&priv_dev->lock, flags);
899 }
900 }
901
902 spin_unlock_irqrestore(&priv_dev->lock, flags);
903}
904
905static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
906{
907 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
908 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
909 struct cdns3_aligned_buf *buf;
910
911 /* check if buffer is aligned to 8. */
912 if (!((uintptr_t)priv_req->request.buf & 0x7))
913 return 0;
914
915 buf = priv_req->aligned_buf;
916
917 if (!buf || priv_req->request.length > buf->size) {
918 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
919 if (!buf)
920 return -ENOMEM;
921
922 buf->size = priv_req->request.length;
923
924 buf->buf = dma_alloc_coherent(priv_dev->sysdev,
925 buf->size,
926 &buf->dma,
927 GFP_ATOMIC);
928 if (!buf->buf) {
929 kfree(buf);
930 return -ENOMEM;
931 }
932
933 if (priv_req->aligned_buf) {
934 trace_cdns3_free_aligned_request(priv_req);
935 priv_req->aligned_buf->in_use = 0;
936 queue_work(system_freezable_wq,
937 &priv_dev->aligned_buf_wq);
938 }
939
940 buf->in_use = 1;
941 priv_req->aligned_buf = buf;
942
943 list_add_tail(&buf->list,
944 &priv_dev->aligned_buf_list);
945 }
946
947 if (priv_ep->dir == USB_DIR_IN) {
948 memcpy(buf->buf, priv_req->request.buf,
949 priv_req->request.length);
950 }
951
952 priv_req->flags |= REQUEST_UNALIGNED;
953 trace_cdns3_prepare_aligned_request(priv_req);
954
955 return 0;
956}
957
958static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
959 struct cdns3_trb *trb)
960{
961 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
962
963 if (!priv_ep->wa1_set) {
964 u32 doorbell;
965
966 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
967
968 if (doorbell) {
969 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
970 priv_ep->wa1_set = 1;
971 priv_ep->wa1_trb = trb;
972 priv_ep->wa1_trb_index = priv_ep->enqueue;
973 trace_cdns3_wa1(priv_ep, "set guard");
974 return 0;
975 }
976 }
977 return 1;
978}
979
980static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
981 struct cdns3_endpoint *priv_ep)
982{
983 int dma_index;
984 u32 doorbell;
985
986 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
987 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
988
989 if (!doorbell || dma_index != priv_ep->wa1_trb_index)
990 cdns3_wa1_restore_cycle_bit(priv_ep);
991}
992
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100993static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
994 struct usb_request *request)
995{
996 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
997 struct cdns3_request *priv_req;
998 struct cdns3_trb *trb;
999 dma_addr_t trb_dma;
1000 int address;
1001 u32 control;
1002 u32 length;
1003 u32 tdl;
1004 unsigned int sg_idx = priv_ep->stream_sg_idx;
1005
1006 priv_req = to_cdns3_request(request);
1007 address = priv_ep->endpoint.desc->bEndpointAddress;
1008
1009 priv_ep->flags |= EP_PENDING_REQUEST;
1010
1011 /* must allocate buffer aligned to 8 */
1012 if (priv_req->flags & REQUEST_UNALIGNED)
1013 trb_dma = priv_req->aligned_buf->dma;
1014 else
1015 trb_dma = request->dma;
1016
1017 /* For stream capable endpoints driver use only single TD. */
1018 trb = priv_ep->trb_pool + priv_ep->enqueue;
1019 priv_req->start_trb = priv_ep->enqueue;
1020 priv_req->end_trb = priv_req->start_trb;
1021 priv_req->trb = trb;
1022
1023 cdns3_select_ep(priv_ep->cdns3_dev, address);
1024
1025 control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
1026 TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
1027
1028 if (!request->num_sgs) {
Peter Chen8dafb3c2020-08-21 11:14:37 +08001029 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001030 length = request->length;
1031 } else {
Peter Chen8dafb3c2020-08-21 11:14:37 +08001032 trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001033 length = request->sg[sg_idx].length;
1034 }
1035
1036 tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
1037
Peter Chen8dafb3c2020-08-21 11:14:37 +08001038 trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001039
1040 /*
1041 * For DEV_VER_V2 controller version we have enabled
1042 * USB_CONF2_EN_TDL_TRB in DMULT configuration.
1043 * This enables TDL calculation based on TRB, hence setting TDL in TRB.
1044 */
1045 if (priv_dev->dev_ver >= DEV_VER_V2) {
1046 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001047 trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl));
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001048 }
1049 priv_req->flags |= REQUEST_PENDING;
1050
Peter Chen8dafb3c2020-08-21 11:14:37 +08001051 trb->control = cpu_to_le32(control);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001052
1053 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1054
1055 /*
1056 * Memory barrier - Cycle Bit must be set before trb->length and
1057 * trb->buffer fields.
1058 */
1059 wmb();
1060
1061 /* always first element */
1062 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
1063 &priv_dev->regs->ep_traddr);
1064
1065 if (!(priv_ep->flags & EP_STALLED)) {
1066 trace_cdns3_ring(priv_ep);
1067 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1068 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1069
1070 priv_ep->prime_flag = false;
1071
1072 /*
1073 * Controller version DEV_VER_V2 tdl calculation
1074 * is based on TRB
1075 */
1076
1077 if (priv_dev->dev_ver < DEV_VER_V2)
1078 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1079 &priv_dev->regs->ep_cmd);
1080 else if (priv_dev->dev_ver > DEV_VER_V2)
1081 writel(tdl, &priv_dev->regs->ep_tdl);
1082
1083 priv_ep->last_stream_id = priv_req->request.stream_id;
1084 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1085 writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
1086 EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
1087
1088 trace_cdns3_doorbell_epx(priv_ep->name,
1089 readl(&priv_dev->regs->ep_traddr));
1090 }
1091
1092 /* WORKAROUND for transition to L0 */
1093 __cdns3_gadget_wakeup(priv_dev);
1094
1095 return 0;
1096}
1097
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001098/**
1099 * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
1100 * @priv_ep: endpoint object
Lee Jones4a35aa62020-07-02 15:46:12 +01001101 * @request: request object
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001102 *
1103 * Returns zero on success or negative value on failure
1104 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001105static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
1106 struct usb_request *request)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001107{
1108 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1109 struct cdns3_request *priv_req;
1110 struct cdns3_trb *trb;
Peter Chen4e218882020-09-10 17:11:24 +08001111 struct cdns3_trb *link_trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001112 dma_addr_t trb_dma;
1113 u32 togle_pcs = 1;
1114 int sg_iter = 0;
1115 int num_trb;
1116 int address;
1117 u32 control;
1118 int pcs;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001119 u16 total_tdl = 0;
Peter Chenabc6b572020-09-10 17:11:23 +08001120 struct scatterlist *s = NULL;
1121 bool sg_supported = !!(request->num_mapped_sgs);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001122
1123 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1124 num_trb = priv_ep->interval;
1125 else
Peter Chenabc6b572020-09-10 17:11:23 +08001126 num_trb = sg_supported ? request->num_mapped_sgs : 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001127
1128 if (num_trb > priv_ep->free_trbs) {
1129 priv_ep->flags |= EP_RING_FULL;
1130 return -ENOBUFS;
1131 }
1132
1133 priv_req = to_cdns3_request(request);
1134 address = priv_ep->endpoint.desc->bEndpointAddress;
1135
1136 priv_ep->flags |= EP_PENDING_REQUEST;
1137
1138 /* must allocate buffer aligned to 8 */
1139 if (priv_req->flags & REQUEST_UNALIGNED)
1140 trb_dma = priv_req->aligned_buf->dma;
1141 else
1142 trb_dma = request->dma;
1143
1144 trb = priv_ep->trb_pool + priv_ep->enqueue;
1145 priv_req->start_trb = priv_ep->enqueue;
1146 priv_req->trb = trb;
1147
1148 cdns3_select_ep(priv_ep->cdns3_dev, address);
1149
1150 /* prepare ring */
1151 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001152 int doorbell, dma_index;
1153 u32 ch_bit = 0;
1154
1155 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1156 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1157
1158 /* Driver can't update LINK TRB if it is current processed. */
1159 if (doorbell && dma_index == priv_ep->num_trbs - 1) {
1160 priv_ep->flags |= EP_DEFERRED_DRDY;
1161 return -ENOBUFS;
1162 }
1163
1164 /*updating C bt in Link TRB before starting DMA*/
1165 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
1166 /*
1167 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
1168 * that DMA stuck at the LINK TRB.
1169 * On the other hand, removing TRB_CHAIN for longer TRs for
1170 * epXout cause that DMA stuck after handling LINK TRB.
1171 * To eliminate this strange behavioral driver set TRB_CHAIN
1172 * bit only for TR size > 2.
1173 */
1174 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
1175 TRBS_PER_SEGMENT > 2)
1176 ch_bit = TRB_CHAIN;
1177
Peter Chen8dafb3c2020-08-21 11:14:37 +08001178 link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) |
1179 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001180 }
1181
1182 if (priv_dev->dev_ver <= DEV_VER_V2)
1183 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
1184
Peter Chenabc6b572020-09-10 17:11:23 +08001185 if (sg_supported)
1186 s = request->sg;
1187
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001188 /* set incorrect Cycle Bit for first trb*/
1189 control = priv_ep->pcs ? 0 : TRB_CYCLE;
1190
1191 do {
1192 u32 length;
1193 u16 td_size = 0;
1194
1195 /* fill TRB */
1196 control |= TRB_TYPE(TRB_NORMAL);
Peter Chenabc6b572020-09-10 17:11:23 +08001197 if (sg_supported) {
1198 trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s)));
1199 length = sg_dma_len(s);
1200 } else {
1201 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001202 length = request->length;
Peter Chenabc6b572020-09-10 17:11:23 +08001203 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001204
1205 if (likely(priv_dev->dev_ver >= DEV_VER_V2))
1206 td_size = DIV_ROUND_UP(length,
1207 priv_ep->endpoint.maxpacket);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001208 else if (priv_ep->flags & EP_TDLCHK_EN)
1209 total_tdl += DIV_ROUND_UP(length,
1210 priv_ep->endpoint.maxpacket);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001211
Peter Chen8dafb3c2020-08-21 11:14:37 +08001212 trb->length = cpu_to_le32(TRB_BURST_LEN(priv_ep->trb_burst_size) |
1213 TRB_LEN(length));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001214 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001215 trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(td_size));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001216 else
1217 control |= TRB_TDL_HS_SIZE(td_size);
1218
1219 pcs = priv_ep->pcs ? TRB_CYCLE : 0;
1220
1221 /*
1222 * first trb should be prepared as last to avoid processing
1223 * transfer to early
1224 */
1225 if (sg_iter != 0)
1226 control |= pcs;
1227
1228 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
1229 control |= TRB_IOC | TRB_ISP;
1230 } else {
1231 /* for last element in TD or in SG list */
1232 if (sg_iter == (num_trb - 1) && sg_iter != 0)
1233 control |= pcs | TRB_IOC | TRB_ISP;
1234 }
1235
1236 if (sg_iter)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001237 trb->control = cpu_to_le32(control);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001238 else
Peter Chen8dafb3c2020-08-21 11:14:37 +08001239 priv_req->trb->control = cpu_to_le32(control);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001240
Peter Chen87e1dcd2020-09-10 17:11:26 +08001241 if (sg_supported) {
1242 trb->control |= TRB_ISP;
1243 /* Don't set chain bit for last TRB */
1244 if (sg_iter < num_trb - 1)
1245 trb->control |= TRB_CHAIN;
1246
Peter Chenabc6b572020-09-10 17:11:23 +08001247 s = sg_next(s);
Peter Chen87e1dcd2020-09-10 17:11:26 +08001248 }
Peter Chenabc6b572020-09-10 17:11:23 +08001249
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001250 control = 0;
1251 ++sg_iter;
1252 priv_req->end_trb = priv_ep->enqueue;
1253 cdns3_ep_inc_enq(priv_ep);
1254 trb = priv_ep->trb_pool + priv_ep->enqueue;
1255 } while (sg_iter < num_trb);
1256
1257 trb = priv_req->trb;
1258
1259 priv_req->flags |= REQUEST_PENDING;
Peter Chen249f0a22020-09-10 17:11:27 +08001260 priv_req->num_of_trb = num_trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001261
1262 if (sg_iter == 1)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001263 trb->control |= cpu_to_le32(TRB_IOC | TRB_ISP);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001264
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001265 if (priv_dev->dev_ver < DEV_VER_V2 &&
1266 (priv_ep->flags & EP_TDLCHK_EN)) {
1267 u16 tdl = total_tdl;
1268 u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
1269
1270 if (tdl > EP_CMD_TDL_MAX) {
1271 tdl = EP_CMD_TDL_MAX;
1272 priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
1273 }
1274
1275 if (old_tdl < tdl) {
1276 tdl -= old_tdl;
1277 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1278 &priv_dev->regs->ep_cmd);
1279 }
1280 }
1281
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001282 /*
1283 * Memory barrier - cycle bit must be set before other filds in trb.
1284 */
1285 wmb();
1286
1287 /* give the TD to the consumer*/
1288 if (togle_pcs)
Peter Chen8dafb3c2020-08-21 11:14:37 +08001289 trb->control = trb->control ^ cpu_to_le32(1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001290
1291 if (priv_dev->dev_ver <= DEV_VER_V2)
1292 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
1293
Peter Chen4e218882020-09-10 17:11:24 +08001294 if (num_trb > 1) {
1295 int i = 0;
1296
1297 while (i < num_trb) {
1298 trace_cdns3_prepare_trb(priv_ep, trb + i);
1299 if (trb + i == link_trb) {
1300 trb = priv_ep->trb_pool;
1301 num_trb = num_trb - i;
1302 i = 0;
1303 } else {
1304 i++;
1305 }
1306 }
1307 } else {
1308 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1309 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001310
1311 /*
1312 * Memory barrier - Cycle Bit must be set before trb->length and
1313 * trb->buffer fields.
1314 */
1315 wmb();
1316
1317 /*
1318 * For DMULT mode we can set address to transfer ring only once after
1319 * enabling endpoint.
1320 */
1321 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
1322 /*
1323 * Until SW is not ready to handle the OUT transfer the ISO OUT
1324 * Endpoint should be disabled (EP_CFG.ENABLE = 0).
1325 * EP_CFG_ENABLE must be set before updating ep_traddr.
1326 */
1327 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
1328 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
1329 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
1330 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
1331 EP_CFG_ENABLE);
1332 }
1333
1334 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
1335 priv_req->start_trb * TRB_SIZE),
1336 &priv_dev->regs->ep_traddr);
1337
1338 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
1339 }
1340
1341 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
1342 trace_cdns3_ring(priv_ep);
1343 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1344 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1345 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1346 trace_cdns3_doorbell_epx(priv_ep->name,
1347 readl(&priv_dev->regs->ep_traddr));
1348 }
1349
1350 /* WORKAROUND for transition to L0 */
1351 __cdns3_gadget_wakeup(priv_dev);
1352
1353 return 0;
1354}
1355
1356void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1357{
1358 struct cdns3_endpoint *priv_ep;
1359 struct usb_ep *ep;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001360
1361 if (priv_dev->hw_configured_flag)
1362 return;
1363
1364 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001365
1366 cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1367 USB_CONF_U1EN | USB_CONF_U2EN);
1368
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001369 priv_dev->hw_configured_flag = 1;
1370
1371 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1372 if (ep->enabled) {
1373 priv_ep = ep_to_cdns3_ep(ep);
1374 cdns3_start_all_request(priv_dev, priv_ep);
1375 }
1376 }
Peter Chenf4cfe5c2020-07-17 18:13:17 +08001377
1378 cdns3_allow_enable_l1(priv_dev, 1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001379}
1380
1381/**
Peter Chen249f0a22020-09-10 17:11:27 +08001382 * cdns3_trb_handled - check whether trb has been handled by DMA
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001383 *
1384 * @priv_ep: extended endpoint object.
1385 * @priv_req: request object for checking
1386 *
1387 * Endpoint must be selected before invoking this function.
1388 *
1389 * Returns false if request has not been handled by DMA, else returns true.
1390 *
1391 * SR - start ring
1392 * ER - end ring
1393 * DQ = priv_ep->dequeue - dequeue position
1394 * EQ = priv_ep->enqueue - enqueue position
1395 * ST = priv_req->start_trb - index of first TRB in transfer ring
1396 * ET = priv_req->end_trb - index of last TRB in transfer ring
1397 * CI = current_index - index of processed TRB by DMA.
1398 *
Peter Chen249f0a22020-09-10 17:11:27 +08001399 * As first step, we check if the TRB between the ST and ET.
1400 * Then, we check if cycle bit for index priv_ep->dequeue
1401 * is correct.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001402 *
1403 * some rules:
Peter Chen249f0a22020-09-10 17:11:27 +08001404 * 1. priv_ep->dequeue never equals to current_index.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001405 * 2 priv_ep->enqueue never exceed priv_ep->dequeue
1406 * 3. exception: priv_ep->enqueue == priv_ep->dequeue
1407 * and priv_ep->free_trbs is zero.
1408 * This case indicate that TR is full.
1409 *
Peter Chen249f0a22020-09-10 17:11:27 +08001410 * At below two cases, the request have been handled.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001411 * Case 1 - priv_ep->dequeue < current_index
1412 * SR ... EQ ... DQ ... CI ... ER
1413 * SR ... DQ ... CI ... EQ ... ER
1414 *
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001415 * Case 2 - priv_ep->dequeue > current_index
Peter Chen249f0a22020-09-10 17:11:27 +08001416 * This situation takes place when CI go through the LINK TRB at the end of
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001417 * transfer ring.
1418 * SR ... CI ... EQ ... DQ ... ER
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001419 */
Peter Chen249f0a22020-09-10 17:11:27 +08001420static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001421 struct cdns3_request *priv_req)
1422{
1423 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
Colin Ian King1f9f5a82020-02-08 16:18:02 +00001424 struct cdns3_trb *trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001425 int current_index = 0;
1426 int handled = 0;
1427 int doorbell;
1428
1429 current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1430 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1431
Peter Chen249f0a22020-09-10 17:11:27 +08001432 /* current trb doesn't belong to this request */
1433 if (priv_req->start_trb < priv_req->end_trb) {
1434 if (priv_ep->dequeue > priv_req->end_trb)
1435 goto finish;
1436
1437 if (priv_ep->dequeue < priv_req->start_trb)
1438 goto finish;
1439 }
1440
1441 if ((priv_req->start_trb > priv_req->end_trb) &&
1442 (priv_ep->dequeue > priv_req->end_trb) &&
1443 (priv_ep->dequeue < priv_req->start_trb))
1444 goto finish;
1445
1446 if ((priv_req->start_trb == priv_req->end_trb) &&
1447 (priv_ep->dequeue != priv_req->end_trb))
1448 goto finish;
1449
1450 trb = &priv_ep->trb_pool[priv_ep->dequeue];
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001451
Peter Chen8dafb3c2020-08-21 11:14:37 +08001452 if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001453 goto finish;
1454
1455 if (doorbell == 1 && current_index == priv_ep->dequeue)
1456 goto finish;
1457
1458 /* The corner case for TRBS_PER_SEGMENT equal 2). */
1459 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1460 handled = 1;
1461 goto finish;
1462 }
1463
1464 if (priv_ep->enqueue == priv_ep->dequeue &&
1465 priv_ep->free_trbs == 0) {
1466 handled = 1;
1467 } else if (priv_ep->dequeue < current_index) {
1468 if ((current_index == (priv_ep->num_trbs - 1)) &&
1469 !priv_ep->dequeue)
1470 goto finish;
1471
Peter Chen249f0a22020-09-10 17:11:27 +08001472 handled = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001473 } else if (priv_ep->dequeue > current_index) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001474 handled = 1;
1475 }
1476
1477finish:
1478 trace_cdns3_request_handled(priv_req, current_index, handled);
1479
1480 return handled;
1481}
1482
1483static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1484 struct cdns3_endpoint *priv_ep)
1485{
1486 struct cdns3_request *priv_req;
1487 struct usb_request *request;
1488 struct cdns3_trb *trb;
Peter Chen249f0a22020-09-10 17:11:27 +08001489 bool request_handled = false;
1490 bool transfer_end = false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001491
1492 while (!list_empty(&priv_ep->pending_req_list)) {
1493 request = cdns3_next_request(&priv_ep->pending_req_list);
1494 priv_req = to_cdns3_request(request);
1495
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01001496 trb = priv_ep->trb_pool + priv_ep->dequeue;
1497
1498 /* Request was dequeued and TRB was changed to TRB_LINK. */
Peter Chen8dafb3c2020-08-21 11:14:37 +08001499 if (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) {
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01001500 trace_cdns3_complete_trb(priv_ep, trb);
1501 cdns3_move_deq_to_next_trb(priv_req);
1502 }
1503
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001504 if (!request->stream_id) {
1505 /* Re-select endpoint. It could be changed by other CPU
1506 * during handling usb_gadget_giveback_request.
1507 */
1508 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001509
Peter Chen249f0a22020-09-10 17:11:27 +08001510 while (cdns3_trb_handled(priv_ep, priv_req)) {
1511 priv_req->finished_trb++;
1512 if (priv_req->finished_trb >= priv_req->num_of_trb)
1513 request_handled = true;
1514
1515 trb = priv_ep->trb_pool + priv_ep->dequeue;
1516 trace_cdns3_complete_trb(priv_ep, trb);
1517
1518 if (!transfer_end)
1519 request->actual +=
1520 TRB_LEN(le32_to_cpu(trb->length));
1521
1522 if (priv_req->num_of_trb > 1 &&
1523 le32_to_cpu(trb->control) & TRB_SMM)
1524 transfer_end = true;
1525
1526 cdns3_ep_inc_deq(priv_ep);
1527 }
1528
1529 if (request_handled) {
1530 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1531 request_handled = false;
1532 transfer_end = false;
1533 } else {
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001534 goto prepare_next_td;
Peter Chen249f0a22020-09-10 17:11:27 +08001535 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001536
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001537 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1538 TRBS_PER_SEGMENT == 2)
1539 break;
1540 } else {
1541 /* Re-select endpoint. It could be changed by other CPU
1542 * during handling usb_gadget_giveback_request.
1543 */
1544 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1545
1546 trb = priv_ep->trb_pool;
1547 trace_cdns3_complete_trb(priv_ep, trb);
1548
1549 if (trb != priv_req->trb)
1550 dev_warn(priv_dev->dev,
1551 "request_trb=0x%p, queue_trb=0x%p\n",
1552 priv_req->trb, trb);
1553
1554 request->actual += TRB_LEN(le32_to_cpu(trb->length));
1555
1556 if (!request->num_sgs ||
1557 (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
1558 priv_ep->stream_sg_idx = 0;
1559 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1560 } else {
1561 priv_ep->stream_sg_idx++;
1562 cdns3_ep_run_stream_transfer(priv_ep, request);
1563 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001564 break;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001565 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001566 }
1567 priv_ep->flags &= ~EP_PENDING_REQUEST;
1568
1569prepare_next_td:
1570 if (!(priv_ep->flags & EP_STALLED) &&
1571 !(priv_ep->flags & EP_STALL_PENDING))
1572 cdns3_start_all_request(priv_dev, priv_ep);
1573}
1574
1575void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1576{
1577 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1578
1579 cdns3_wa1_restore_cycle_bit(priv_ep);
1580
1581 if (rearm) {
1582 trace_cdns3_ring(priv_ep);
1583
1584 /* Cycle Bit must be updated before arming DMA. */
1585 wmb();
1586 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1587
1588 __cdns3_gadget_wakeup(priv_dev);
1589
1590 trace_cdns3_doorbell_epx(priv_ep->name,
1591 readl(&priv_dev->regs->ep_traddr));
1592 }
1593}
1594
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001595static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
1596{
1597 u16 tdl = priv_ep->pending_tdl;
1598 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1599
1600 if (tdl > EP_CMD_TDL_MAX) {
1601 tdl = EP_CMD_TDL_MAX;
1602 priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
1603 } else {
1604 priv_ep->pending_tdl = 0;
1605 }
1606
1607 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
1608}
1609
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001610/**
1611 * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
1612 * @priv_ep: endpoint object
1613 *
1614 * Returns 0
1615 */
1616static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1617{
1618 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1619 u32 ep_sts_reg;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001620 struct usb_request *deferred_request;
1621 struct usb_request *pending_request;
1622 u32 tdl = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001623
1624 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1625
1626 trace_cdns3_epx_irq(priv_dev, priv_ep);
1627
1628 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1629 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1630
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001631 if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
1632 bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
1633
1634 tdl = cdns3_get_tdl(priv_dev);
1635
1636 /*
1637 * Continue the previous transfer:
1638 * There is some racing between ERDY and PRIME. The device send
1639 * ERDY and almost in the same time Host send PRIME. It cause
1640 * that host ignore the ERDY packet and driver has to send it
1641 * again.
1642 */
Peter Chen8dafb3c2020-08-21 11:14:37 +08001643 if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) ||
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001644 EP_STS_HOSTPP(ep_sts_reg))) {
1645 writel(EP_CMD_ERDY |
1646 EP_CMD_ERDY_SID(priv_ep->last_stream_id),
1647 &priv_dev->regs->ep_cmd);
1648 ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
1649 } else {
1650 priv_ep->prime_flag = true;
1651
1652 pending_request = cdns3_next_request(&priv_ep->pending_req_list);
1653 deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
1654
1655 if (deferred_request && !pending_request) {
1656 cdns3_start_all_request(priv_dev, priv_ep);
1657 }
1658 }
1659 }
1660
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001661 if (ep_sts_reg & EP_STS_TRBERR) {
1662 if (priv_ep->flags & EP_STALL_PENDING &&
1663 !(ep_sts_reg & EP_STS_DESCMIS &&
1664 priv_dev->dev_ver < DEV_VER_V2)) {
1665 cdns3_ep_stall_flush(priv_ep);
1666 }
1667
1668 /*
1669 * For isochronous transfer driver completes request on
1670 * IOC or on TRBERR. IOC appears only when device receive
1671 * OUT data packet. If host disable stream or lost some packet
1672 * then the only way to finish all queued transfer is to do it
1673 * on TRBERR event.
1674 */
1675 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1676 !priv_ep->wa1_set) {
1677 if (!priv_ep->dir) {
1678 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1679
1680 ep_cfg &= ~EP_CFG_ENABLE;
1681 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1682 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1683 }
1684 cdns3_transfer_completed(priv_dev, priv_ep);
1685 } else if (!(priv_ep->flags & EP_STALLED) &&
1686 !(priv_ep->flags & EP_STALL_PENDING)) {
1687 if (priv_ep->flags & EP_DEFERRED_DRDY) {
1688 priv_ep->flags &= ~EP_DEFERRED_DRDY;
1689 cdns3_start_all_request(priv_dev, priv_ep);
1690 } else {
1691 cdns3_rearm_transfer(priv_ep,
1692 priv_ep->wa1_set);
1693 }
1694 }
1695 }
1696
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001697 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
1698 (ep_sts_reg & EP_STS_IOT)) {
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001699 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1700 if (ep_sts_reg & EP_STS_ISP)
1701 priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1702 else
1703 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1704 }
1705
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001706 if (!priv_ep->use_streams) {
1707 if ((ep_sts_reg & EP_STS_IOC) ||
1708 (ep_sts_reg & EP_STS_ISP)) {
1709 cdns3_transfer_completed(priv_dev, priv_ep);
1710 } else if ((priv_ep->flags & EP_TDLCHK_EN) &
1711 priv_ep->pending_tdl) {
1712 /* handle IOT with pending tdl */
1713 cdns3_reprogram_tdl(priv_ep);
1714 }
1715 } else if (priv_ep->dir == USB_DIR_OUT) {
1716 priv_ep->ep_sts_pending |= ep_sts_reg;
1717 } else if (ep_sts_reg & EP_STS_IOT) {
1718 cdns3_transfer_completed(priv_dev, priv_ep);
1719 }
1720 }
1721
1722 /*
1723 * MD_EXIT interrupt sets when stream capable endpoint exits
1724 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
1725 */
1726 if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
1727 (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
1728 priv_ep->ep_sts_pending = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001729 cdns3_transfer_completed(priv_dev, priv_ep);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001730 }
1731
1732 /*
1733 * WA2: this condition should only be meet when
1734 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
1735 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001736 * In other cases this interrupt will be disabled.
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001737 */
1738 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1739 !(priv_ep->flags & EP_STALLED))
1740 cdns3_wa2_descmissing_packet(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001741
1742 return 0;
1743}
1744
1745static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1746{
1747 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect) {
1748 spin_unlock(&priv_dev->lock);
1749 priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
1750 spin_lock(&priv_dev->lock);
1751 }
1752}
1753
1754/**
1755 * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
1756 * @priv_dev: extended gadget object
1757 * @usb_ists: bitmap representation of device's reported interrupts
1758 * (usb_ists register value)
1759 */
1760static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1761 u32 usb_ists)
1762{
1763 int speed = 0;
1764
1765 trace_cdns3_usb_irq(priv_dev, usb_ists);
1766 if (usb_ists & USB_ISTS_L1ENTI) {
1767 /*
1768 * WORKAROUND: CDNS3 controller has issue with hardware resuming
1769 * from L1. To fix it, if any DMA transfer is pending driver
1770 * must starts driving resume signal immediately.
1771 */
1772 if (readl(&priv_dev->regs->drbl))
1773 __cdns3_gadget_wakeup(priv_dev);
1774 }
1775
1776 /* Connection detected */
1777 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1778 speed = cdns3_get_speed(priv_dev);
1779 priv_dev->gadget.speed = speed;
1780 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1781 cdns3_ep0_config(priv_dev);
1782 }
1783
1784 /* Disconnection detected */
1785 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
1786 cdns3_disconnect_gadget(priv_dev);
1787 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1788 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1789 cdns3_hw_reset_eps_config(priv_dev);
1790 }
1791
1792 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1793 if (priv_dev->gadget_driver &&
1794 priv_dev->gadget_driver->suspend) {
1795 spin_unlock(&priv_dev->lock);
1796 priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1797 spin_lock(&priv_dev->lock);
1798 }
1799 }
1800
1801 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1802 if (priv_dev->gadget_driver &&
1803 priv_dev->gadget_driver->resume) {
1804 spin_unlock(&priv_dev->lock);
1805 priv_dev->gadget_driver->resume(&priv_dev->gadget);
1806 spin_lock(&priv_dev->lock);
1807 }
1808 }
1809
1810 /* reset*/
1811 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1812 if (priv_dev->gadget_driver) {
1813 spin_unlock(&priv_dev->lock);
1814 usb_gadget_udc_reset(&priv_dev->gadget,
1815 priv_dev->gadget_driver);
1816 spin_lock(&priv_dev->lock);
1817
1818 /*read again to check the actual speed*/
1819 speed = cdns3_get_speed(priv_dev);
1820 priv_dev->gadget.speed = speed;
1821 cdns3_hw_reset_eps_config(priv_dev);
1822 cdns3_ep0_config(priv_dev);
1823 }
1824 }
1825}
1826
1827/**
1828 * cdns3_device_irq_handler- interrupt handler for device part of controller
1829 *
1830 * @irq: irq number for cdns3 core device
1831 * @data: structure of cdns3
1832 *
1833 * Returns IRQ_HANDLED or IRQ_NONE
1834 */
1835static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1836{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001837 struct cdns3_device *priv_dev = data;
Peter Chenb1234e32020-09-02 17:57:32 +08001838 struct cdns3 *cdns = dev_get_drvdata(priv_dev->dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001839 irqreturn_t ret = IRQ_NONE;
1840 u32 reg;
1841
Peter Chenb1234e32020-09-02 17:57:32 +08001842 if (cdns->in_lpm)
1843 return ret;
1844
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001845 /* check USB device interrupt */
1846 reg = readl(&priv_dev->regs->usb_ists);
1847 if (reg) {
1848 /* After masking interrupts the new interrupts won't be
1849 * reported in usb_ists/ep_ists. In order to not lose some
1850 * of them driver disables only detected interrupts.
1851 * They will be enabled ASAP after clearing source of
1852 * interrupt. This an unusual behavior only applies to
1853 * usb_ists register.
1854 */
1855 reg = ~reg & readl(&priv_dev->regs->usb_ien);
1856 /* mask deferred interrupt. */
1857 writel(reg, &priv_dev->regs->usb_ien);
1858 ret = IRQ_WAKE_THREAD;
1859 }
1860
1861 /* check endpoint interrupt */
1862 reg = readl(&priv_dev->regs->ep_ists);
1863 if (reg) {
1864 writel(0, &priv_dev->regs->ep_ien);
1865 ret = IRQ_WAKE_THREAD;
1866 }
1867
1868 return ret;
1869}
1870
1871/**
1872 * cdns3_device_thread_irq_handler- interrupt handler for device part
1873 * of controller
1874 *
1875 * @irq: irq number for cdns3 core device
1876 * @data: structure of cdns3
1877 *
1878 * Returns IRQ_HANDLED or IRQ_NONE
1879 */
1880static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1881{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001882 struct cdns3_device *priv_dev = data;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001883 irqreturn_t ret = IRQ_NONE;
1884 unsigned long flags;
Peter Chen06825ca2020-06-23 11:10:01 +08001885 unsigned int bit;
Peter Chen8685c462020-06-23 11:10:00 +08001886 unsigned long reg;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001887
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001888 spin_lock_irqsave(&priv_dev->lock, flags);
1889
1890 reg = readl(&priv_dev->regs->usb_ists);
1891 if (reg) {
1892 writel(reg, &priv_dev->regs->usb_ists);
1893 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1894 cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1895 ret = IRQ_HANDLED;
1896 }
1897
1898 reg = readl(&priv_dev->regs->ep_ists);
1899
1900 /* handle default endpoint OUT */
1901 if (reg & EP_ISTS_EP_OUT0) {
1902 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1903 ret = IRQ_HANDLED;
1904 }
1905
1906 /* handle default endpoint IN */
1907 if (reg & EP_ISTS_EP_IN0) {
1908 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1909 ret = IRQ_HANDLED;
1910 }
1911
1912 /* check if interrupt from non default endpoint, if no exit */
1913 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1914 if (!reg)
1915 goto irqend;
1916
Peter Chen8685c462020-06-23 11:10:00 +08001917 for_each_set_bit(bit, &reg,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001918 sizeof(u32) * BITS_PER_BYTE) {
1919 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1920 ret = IRQ_HANDLED;
1921 }
1922
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001923 if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
1924 cdns3_wa2_check_outq_status(priv_dev);
1925
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001926irqend:
1927 writel(~0, &priv_dev->regs->ep_ien);
1928 spin_unlock_irqrestore(&priv_dev->lock, flags);
1929
1930 return ret;
1931}
1932
1933/**
1934 * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
1935 *
1936 * The real reservation will occur during write to EP_CFG register,
1937 * this function is used to check if the 'size' reservation is allowed.
1938 *
1939 * @priv_dev: extended gadget object
1940 * @size: the size (KB) for EP would like to allocate
1941 * @is_in: endpoint direction
1942 *
1943 * Return 0 if the required size can met or negative value on failure
1944 */
1945static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
1946 int size, int is_in)
1947{
1948 int remained;
1949
1950 /* 2KB are reserved for EP0*/
1951 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
1952
1953 if (is_in) {
1954 if (remained < size)
1955 return -EPERM;
1956
1957 priv_dev->onchip_used_size += size;
1958 } else {
1959 int required;
1960
1961 /**
1962 * ALL OUT EPs are shared the same chunk onchip memory, so
1963 * driver checks if it already has assigned enough buffers
1964 */
1965 if (priv_dev->out_mem_is_allocated >= size)
1966 return 0;
1967
1968 required = size - priv_dev->out_mem_is_allocated;
1969
1970 if (required > remained)
1971 return -EPERM;
1972
1973 priv_dev->out_mem_is_allocated += required;
1974 priv_dev->onchip_used_size += required;
1975 }
1976
1977 return 0;
1978}
1979
Jason Yane9010322020-04-02 20:38:37 +08001980static void cdns3_stream_ep_reconfig(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +08001981 struct cdns3_endpoint *priv_ep)
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001982{
1983 if (!priv_ep->use_streams || priv_dev->gadget.speed < USB_SPEED_SUPER)
1984 return;
1985
1986 if (priv_dev->dev_ver >= DEV_VER_V3) {
1987 u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
1988
1989 /*
1990 * Stream capable endpoints are handled by using ep_tdl
1991 * register. Other endpoints use TDL from TRB feature.
1992 */
1993 cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb, mask);
1994 }
1995
1996 /* Enable Stream Bit TDL chk and SID chk */
1997 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_STREAM_EN |
1998 EP_CFG_TDL_CHK | EP_CFG_SID_CHK);
1999}
2000
Jason Yane9010322020-04-02 20:38:37 +08002001static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +08002002 struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002003{
2004 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2005
2006 /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
2007 if (priv_dev->dev_ver <= DEV_VER_V2)
2008 writel(USB_CONF_DMULT, &regs->usb_conf);
2009
2010 if (priv_dev->dev_ver == DEV_VER_V2)
2011 writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
2012
2013 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
2014 u32 mask;
2015
2016 if (priv_ep->dir)
2017 mask = BIT(priv_ep->num + 16);
2018 else
2019 mask = BIT(priv_ep->num);
2020
2021 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
2022 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
2023 cdns3_set_register_bit(&regs->tdl_beh, mask);
2024 cdns3_set_register_bit(&regs->tdl_beh2, mask);
2025 cdns3_set_register_bit(&regs->dma_adv_td, mask);
2026 }
2027
2028 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2029 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
2030
2031 cdns3_set_register_bit(&regs->dtrans, mask);
2032 }
2033}
2034
2035/**
2036 * cdns3_ep_config Configure hardware endpoint
2037 * @priv_ep: extended endpoint object
2038 */
2039void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
2040{
2041 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
2042 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2043 u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
2044 u32 max_packet_size = 0;
2045 u8 maxburst = 0;
2046 u32 ep_cfg = 0;
2047 u8 buffering;
2048 u8 mult = 0;
2049 int ret;
2050
2051 buffering = CDNS3_EP_BUF_SIZE - 1;
2052
2053 cdns3_configure_dmult(priv_dev, priv_ep);
2054
2055 switch (priv_ep->type) {
2056 case USB_ENDPOINT_XFER_INT:
2057 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
2058
2059 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
2060 priv_dev->dev_ver > DEV_VER_V2)
2061 ep_cfg |= EP_CFG_TDL_CHK;
2062 break;
2063 case USB_ENDPOINT_XFER_BULK:
2064 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
2065
2066 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
2067 priv_dev->dev_ver > DEV_VER_V2)
2068 ep_cfg |= EP_CFG_TDL_CHK;
2069 break;
2070 default:
2071 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
2072 mult = CDNS3_EP_ISO_HS_MULT - 1;
2073 buffering = mult + 1;
2074 }
2075
2076 switch (priv_dev->gadget.speed) {
2077 case USB_SPEED_FULL:
2078 max_packet_size = is_iso_ep ? 1023 : 64;
2079 break;
2080 case USB_SPEED_HIGH:
2081 max_packet_size = is_iso_ep ? 1024 : 512;
2082 break;
2083 case USB_SPEED_SUPER:
2084 /* It's limitation that driver assumes in driver. */
2085 mult = 0;
2086 max_packet_size = 1024;
2087 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2088 maxburst = CDNS3_EP_ISO_SS_BURST - 1;
2089 buffering = (mult + 1) *
2090 (maxburst + 1);
2091
2092 if (priv_ep->interval > 1)
2093 buffering++;
2094 } else {
2095 maxburst = CDNS3_EP_BUF_SIZE - 1;
2096 }
2097 break;
2098 default:
2099 /* all other speed are not supported */
2100 return;
2101 }
2102
2103 if (max_packet_size == 1024)
2104 priv_ep->trb_burst_size = 128;
2105 else if (max_packet_size >= 512)
2106 priv_ep->trb_burst_size = 64;
2107 else
2108 priv_ep->trb_burst_size = 16;
2109
2110 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2111 !!priv_ep->dir);
2112 if (ret) {
2113 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2114 return;
2115 }
2116
2117 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
2118 EP_CFG_MULT(mult) |
2119 EP_CFG_BUFFERING(buffering) |
2120 EP_CFG_MAXBURST(maxburst);
2121
2122 cdns3_select_ep(priv_dev, bEndpointAddress);
2123 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2124
2125 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
2126 priv_ep->name, ep_cfg);
2127}
2128
2129/* Find correct direction for HW endpoint according to description */
2130static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
2131 struct cdns3_endpoint *priv_ep)
2132{
2133 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
2134 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
2135}
2136
2137static struct
2138cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
2139 struct usb_endpoint_descriptor *desc)
2140{
2141 struct usb_ep *ep;
2142 struct cdns3_endpoint *priv_ep;
2143
2144 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2145 unsigned long num;
2146 int ret;
2147 /* ep name pattern likes epXin or epXout */
2148 char c[2] = {ep->name[2], '\0'};
2149
2150 ret = kstrtoul(c, 10, &num);
2151 if (ret)
2152 return ERR_PTR(ret);
2153
2154 priv_ep = ep_to_cdns3_ep(ep);
2155 if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
2156 if (!(priv_ep->flags & EP_CLAIMED)) {
2157 priv_ep->num = num;
2158 return priv_ep;
2159 }
2160 }
2161 }
2162
2163 return ERR_PTR(-ENOENT);
2164}
2165
2166/*
2167 * Cadence IP has one limitation that all endpoints must be configured
2168 * (Type & MaxPacketSize) before setting configuration through hardware
2169 * register, it means we can't change endpoints configuration after
2170 * set_configuration.
2171 *
2172 * This function set EP_CLAIMED flag which is added when the gadget driver
2173 * uses usb_ep_autoconfig to configure specific endpoint;
2174 * When the udc driver receives set_configurion request,
2175 * it goes through all claimed endpoints, and configure all endpoints
2176 * accordingly.
2177 *
2178 * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
2179 * ep_cfg register which can be changed after set_configuration, and do
2180 * some software operation accordingly.
2181 */
2182static struct
2183usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
2184 struct usb_endpoint_descriptor *desc,
2185 struct usb_ss_ep_comp_descriptor *comp_desc)
2186{
2187 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2188 struct cdns3_endpoint *priv_ep;
2189 unsigned long flags;
2190
2191 priv_ep = cdns3_find_available_ep(priv_dev, desc);
2192 if (IS_ERR(priv_ep)) {
2193 dev_err(priv_dev->dev, "no available ep\n");
2194 return NULL;
2195 }
2196
2197 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
2198
2199 spin_lock_irqsave(&priv_dev->lock, flags);
2200 priv_ep->endpoint.desc = desc;
2201 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
2202 priv_ep->type = usb_endpoint_type(desc);
2203 priv_ep->flags |= EP_CLAIMED;
2204 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2205
2206 spin_unlock_irqrestore(&priv_dev->lock, flags);
2207 return &priv_ep->endpoint;
2208}
2209
2210/**
2211 * cdns3_gadget_ep_alloc_request Allocates request
2212 * @ep: endpoint object associated with request
2213 * @gfp_flags: gfp flags
2214 *
2215 * Returns allocated request address, NULL on allocation error
2216 */
2217struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
2218 gfp_t gfp_flags)
2219{
2220 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2221 struct cdns3_request *priv_req;
2222
2223 priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
2224 if (!priv_req)
2225 return NULL;
2226
2227 priv_req->priv_ep = priv_ep;
2228
2229 trace_cdns3_alloc_request(priv_req);
2230 return &priv_req->request;
2231}
2232
2233/**
2234 * cdns3_gadget_ep_free_request Free memory occupied by request
2235 * @ep: endpoint object associated with request
2236 * @request: request to free memory
2237 */
2238void cdns3_gadget_ep_free_request(struct usb_ep *ep,
2239 struct usb_request *request)
2240{
2241 struct cdns3_request *priv_req = to_cdns3_request(request);
2242
2243 if (priv_req->aligned_buf)
2244 priv_req->aligned_buf->in_use = 0;
2245
2246 trace_cdns3_free_request(priv_req);
2247 kfree(priv_req);
2248}
2249
2250/**
2251 * cdns3_gadget_ep_enable Enable endpoint
2252 * @ep: endpoint object
2253 * @desc: endpoint descriptor
2254 *
2255 * Returns 0 on success, error code elsewhere
2256 */
2257static int cdns3_gadget_ep_enable(struct usb_ep *ep,
2258 const struct usb_endpoint_descriptor *desc)
2259{
2260 struct cdns3_endpoint *priv_ep;
2261 struct cdns3_device *priv_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002262 const struct usb_ss_ep_comp_descriptor *comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002263 u32 reg = EP_STS_EN_TRBERREN;
2264 u32 bEndpointAddress;
2265 unsigned long flags;
2266 int enable = 1;
2267 int ret;
2268 int val;
2269
2270 priv_ep = ep_to_cdns3_ep(ep);
2271 priv_dev = priv_ep->cdns3_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002272 comp_desc = priv_ep->endpoint.comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002273
2274 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
2275 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
2276 return -EINVAL;
2277 }
2278
2279 if (!desc->wMaxPacketSize) {
2280 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
2281 return -EINVAL;
2282 }
2283
2284 if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
2285 "%s is already enabled\n", priv_ep->name))
2286 return 0;
2287
2288 spin_lock_irqsave(&priv_dev->lock, flags);
2289
2290 priv_ep->endpoint.desc = desc;
2291 priv_ep->type = usb_endpoint_type(desc);
2292 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2293
2294 if (priv_ep->interval > ISO_MAX_INTERVAL &&
2295 priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2296 dev_err(priv_dev->dev, "Driver is limited to %d period\n",
2297 ISO_MAX_INTERVAL);
2298
2299 ret = -EINVAL;
2300 goto exit;
2301 }
2302
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002303 bEndpointAddress = priv_ep->num | priv_ep->dir;
2304 cdns3_select_ep(priv_dev, bEndpointAddress);
2305
2306 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
2307 /*
2308 * Enable stream support (SS mode) related interrupts
2309 * in EP_STS_EN Register
2310 */
2311 if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2312 reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
2313 EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
2314 EP_STS_EN_STREAMREN;
2315 priv_ep->use_streams = true;
2316 cdns3_stream_ep_reconfig(priv_dev, priv_ep);
2317 priv_dev->using_streams |= true;
2318 }
2319 }
2320
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002321 ret = cdns3_allocate_trb_pool(priv_ep);
2322
2323 if (ret)
2324 goto exit;
2325
2326 bEndpointAddress = priv_ep->num | priv_ep->dir;
2327 cdns3_select_ep(priv_dev, bEndpointAddress);
2328
2329 trace_cdns3_gadget_ep_enable(priv_ep);
2330
2331 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2332
2333 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2334 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2335 1, 1000);
2336
2337 if (unlikely(ret)) {
2338 cdns3_free_trb_pool(priv_ep);
2339 ret = -EINVAL;
2340 goto exit;
2341 }
2342
2343 /* enable interrupt for selected endpoint */
2344 cdns3_set_register_bit(&priv_dev->regs->ep_ien,
2345 BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
2346
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002347 if (priv_dev->dev_ver < DEV_VER_V2)
2348 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
2349
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002350 writel(reg, &priv_dev->regs->ep_sts_en);
2351
2352 /*
2353 * For some versions of controller at some point during ISO OUT traffic
2354 * DMA reads Transfer Ring for the EP which has never got doorbell.
2355 * This issue was detected only on simulation, but to avoid this issue
2356 * driver add protection against it. To fix it driver enable ISO OUT
2357 * endpoint before setting DRBL. This special treatment of ISO OUT
2358 * endpoints are recommended by controller specification.
2359 */
2360 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2361 enable = 0;
2362
2363 if (enable)
2364 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_ENABLE);
2365
2366 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 Laszczak7733f6c2019-08-26 12:19:30 +01003072void cdns3_gadget_exit(struct cdns3 *cdns)
3073{
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);
3101
3102 kfree(priv_dev->zlp_buf);
Peter Chen6b777892020-08-21 10:55:47 +08003103 usb_put_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003104 cdns->gadget_dev = NULL;
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +02003105 cdns3_drd_gadget_off(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003106}
3107
3108static int cdns3_gadget_start(struct cdns3 *cdns)
3109{
3110 struct cdns3_device *priv_dev;
3111 u32 max_speed;
3112 int ret;
3113
3114 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
3115 if (!priv_dev)
3116 return -ENOMEM;
3117
Peter Chen6b777892020-08-21 10:55:47 +08003118 usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
3119 cdns3_gadget_release);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003120 cdns->gadget_dev = priv_dev;
3121 priv_dev->sysdev = cdns->dev;
3122 priv_dev->dev = cdns->dev;
3123 priv_dev->regs = cdns->dev_regs;
3124
3125 device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
3126 &priv_dev->onchip_buffers);
3127
3128 if (priv_dev->onchip_buffers <= 0) {
3129 u32 reg = readl(&priv_dev->regs->usb_cap2);
3130
3131 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
3132 }
3133
3134 if (!priv_dev->onchip_buffers)
3135 priv_dev->onchip_buffers = 256;
3136
3137 max_speed = usb_get_maximum_speed(cdns->dev);
3138
3139 /* Check the maximum_speed parameter */
3140 switch (max_speed) {
3141 case USB_SPEED_FULL:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003142 case USB_SPEED_HIGH:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003143 case USB_SPEED_SUPER:
3144 break;
3145 default:
3146 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
3147 max_speed);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05003148 fallthrough;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003149 case USB_SPEED_UNKNOWN:
3150 /* default to superspeed */
3151 max_speed = USB_SPEED_SUPER;
3152 break;
3153 }
3154
3155 /* fill gadget fields */
3156 priv_dev->gadget.max_speed = max_speed;
3157 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3158 priv_dev->gadget.ops = &cdns3_gadget_ops;
3159 priv_dev->gadget.name = "usb-ss-gadget";
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003160 priv_dev->gadget.quirk_avoids_skb_reserve = 1;
Peter Chen77f30ff2020-05-10 13:30:42 +08003161 priv_dev->gadget.irq = cdns->dev_irq;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003162
3163 spin_lock_init(&priv_dev->lock);
3164 INIT_WORK(&priv_dev->pending_status_wq,
3165 cdns3_pending_setup_status_handler);
3166
3167 INIT_WORK(&priv_dev->aligned_buf_wq,
3168 cdns3_free_aligned_request_buf);
3169
3170 /* initialize endpoint container */
3171 INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
3172 INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
3173
3174 ret = cdns3_init_eps(priv_dev);
3175 if (ret) {
3176 dev_err(priv_dev->dev, "Failed to create endpoints\n");
3177 goto err1;
3178 }
3179
3180 /* allocate memory for setup packet buffer */
3181 priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
3182 &priv_dev->setup_dma, GFP_DMA);
3183 if (!priv_dev->setup_buf) {
3184 ret = -ENOMEM;
3185 goto err2;
3186 }
3187
3188 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
3189
3190 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
3191 readl(&priv_dev->regs->usb_cap6));
3192 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
3193 readl(&priv_dev->regs->usb_cap1));
Colin Ian King5d041112019-09-03 13:07:10 +01003194 dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003195 readl(&priv_dev->regs->usb_cap2));
3196
3197 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
Peter Chend6be7c92020-09-10 17:11:29 +08003198 if (priv_dev->dev_ver >= DEV_VER_V2)
3199 priv_dev->gadget.sg_supported = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003200
3201 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
3202 if (!priv_dev->zlp_buf) {
3203 ret = -ENOMEM;
3204 goto err3;
3205 }
3206
3207 /* add USB gadget device */
Peter Chen6b777892020-08-21 10:55:47 +08003208 ret = usb_add_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003209 if (ret < 0) {
Peter Chen6b777892020-08-21 10:55:47 +08003210 dev_err(priv_dev->dev, "Failed to add gadget\n");
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003211 goto err4;
3212 }
3213
3214 return 0;
3215err4:
3216 kfree(priv_dev->zlp_buf);
3217err3:
3218 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3219 priv_dev->setup_dma);
3220err2:
3221 cdns3_free_all_eps(priv_dev);
3222err1:
Peter Chen6b777892020-08-21 10:55:47 +08003223 usb_put_gadget(&priv_dev->gadget);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003224 cdns->gadget_dev = NULL;
3225 return ret;
3226}
3227
3228static int __cdns3_gadget_init(struct cdns3 *cdns)
3229{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003230 int ret = 0;
3231
Pawel Laszczakeb21a742019-10-07 13:03:23 +01003232 /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
3233 ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
3234 if (ret) {
3235 dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
3236 return ret;
3237 }
3238
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +02003239 cdns3_drd_gadget_on(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003240 pm_runtime_get_sync(cdns->dev);
3241
3242 ret = cdns3_gadget_start(cdns);
3243 if (ret)
3244 return ret;
3245
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003246 /*
3247 * Because interrupt line can be shared with other components in
3248 * driver it can't use IRQF_ONESHOT flag here.
3249 */
3250 ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
3251 cdns3_device_irq_handler,
3252 cdns3_device_thread_irq_handler,
Peter Chenaf58e1f2019-12-27 17:10:04 +08003253 IRQF_SHARED, dev_name(cdns->dev),
3254 cdns->gadget_dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003255
3256 if (ret)
3257 goto err0;
3258
3259 return 0;
3260err0:
3261 cdns3_gadget_exit(cdns);
3262 return ret;
3263}
3264
3265static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup)
3266{
3267 struct cdns3_device *priv_dev = cdns->gadget_dev;
3268
3269 cdns3_disconnect_gadget(priv_dev);
3270
3271 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3272 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
3273 cdns3_hw_reset_eps_config(priv_dev);
3274
3275 /* disable interrupt for device */
3276 writel(0, &priv_dev->regs->usb_ien);
3277
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003278 return 0;
3279}
3280
3281static int cdns3_gadget_resume(struct cdns3 *cdns, bool hibernated)
3282{
3283 struct cdns3_device *priv_dev = cdns->gadget_dev;
3284
3285 if (!priv_dev->gadget_driver)
3286 return 0;
3287
3288 cdns3_gadget_config(priv_dev);
3289
3290 return 0;
3291}
3292
3293/**
3294 * cdns3_gadget_init - initialize device structure
3295 *
Lee Jones4a35aa62020-07-02 15:46:12 +01003296 * @cdns: cdns3 instance
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003297 *
3298 * This function initializes the gadget.
3299 */
3300int cdns3_gadget_init(struct cdns3 *cdns)
3301{
3302 struct cdns3_role_driver *rdrv;
3303
3304 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
3305 if (!rdrv)
3306 return -ENOMEM;
3307
3308 rdrv->start = __cdns3_gadget_init;
3309 rdrv->stop = cdns3_gadget_exit;
3310 rdrv->suspend = cdns3_gadget_suspend;
3311 rdrv->resume = cdns3_gadget_resume;
3312 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
3313 rdrv->name = "gadget";
3314 cdns->roles[USB_ROLE_DEVICE] = rdrv;
3315
3316 return 0;
3317}