blob: aca347b7da578fdcc81a13851d8b9a651e4c357e [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 {
264 link_trb->buffer = TRB_BUFFER(priv_ep->trb_pool_dma);
265 link_trb->control = 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
465/**
466 * cdns3_wa2_descmiss_copy_data copy data from internal requests to
467 * request queued by class driver.
468 * @priv_ep: extended endpoint object
469 * @request: request object
470 */
471static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
472 struct usb_request *request)
473{
474 struct usb_request *descmiss_req;
475 struct cdns3_request *descmiss_priv_req;
476
477 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
478 int chunk_end;
479 int length;
480
481 descmiss_priv_req =
482 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
483 descmiss_req = &descmiss_priv_req->request;
484
485 /* driver can't touch pending request */
486 if (descmiss_priv_req->flags & REQUEST_PENDING)
487 break;
488
489 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
490 length = request->actual + descmiss_req->actual;
491
492 request->status = descmiss_req->status;
493
494 if (length <= request->length) {
495 memcpy(&((u8 *)request->buf)[request->actual],
496 descmiss_req->buf,
497 descmiss_req->actual);
498 request->actual = length;
499 } else {
500 /* It should never occures */
501 request->status = -ENOMEM;
502 }
503
504 list_del_init(&descmiss_priv_req->list);
505
506 kfree(descmiss_req->buf);
507 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
508 --priv_ep->wa2_counter;
509
510 if (!chunk_end)
511 break;
512 }
513}
514
Jason Yane9010322020-04-02 20:38:37 +0800515static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800516 struct cdns3_endpoint *priv_ep,
517 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100518{
519 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
520 priv_req->flags & REQUEST_INTERNAL) {
521 struct usb_request *req;
522
523 req = cdns3_next_request(&priv_ep->deferred_req_list);
524
525 priv_ep->descmis_req = NULL;
526
527 if (!req)
528 return NULL;
529
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100530 /* unmap the gadget request before copying data */
531 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
532 priv_ep->dir);
533
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100534 cdns3_wa2_descmiss_copy_data(priv_ep, req);
535 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
536 req->length != req->actual) {
537 /* wait for next part of transfer */
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100538 /* re-map the gadget request buffer*/
539 usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
540 usb_endpoint_dir_in(priv_ep->endpoint.desc));
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100541 return NULL;
542 }
543
544 if (req->status == -EINPROGRESS)
545 req->status = 0;
546
547 list_del_init(&req->list);
548 cdns3_start_all_request(priv_dev, priv_ep);
549 return req;
550 }
551
552 return &priv_req->request;
553}
554
Jason Yane9010322020-04-02 20:38:37 +0800555static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +0800556 struct cdns3_endpoint *priv_ep,
557 struct cdns3_request *priv_req)
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100558{
559 int deferred = 0;
560
561 /*
562 * If transfer was queued before DESCMISS appear than we
563 * can disable handling of DESCMISS interrupt. Driver assumes that it
564 * can disable special treatment for this endpoint.
565 */
566 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
567 u32 reg;
568
569 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
570 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
571 reg = readl(&priv_dev->regs->ep_sts_en);
572 reg &= ~EP_STS_EN_DESCMISEN;
573 trace_cdns3_wa2(priv_ep, "workaround disabled\n");
574 writel(reg, &priv_dev->regs->ep_sts_en);
575 }
576
577 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
578 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
579 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
580
581 /*
582 * DESCMISS transfer has been finished, so data will be
583 * directly copied from internal allocated usb_request
584 * objects.
585 */
586 if (pending_empty && !descmiss_empty &&
587 !(priv_req->flags & REQUEST_INTERNAL)) {
588 cdns3_wa2_descmiss_copy_data(priv_ep,
589 &priv_req->request);
590
591 trace_cdns3_wa2(priv_ep, "get internal stored data");
592
593 list_add_tail(&priv_req->request.list,
594 &priv_ep->pending_req_list);
595 cdns3_gadget_giveback(priv_ep, priv_req,
596 priv_req->request.status);
597
598 /*
599 * Intentionally driver returns positive value as
600 * correct value. It informs that transfer has
601 * been finished.
602 */
603 return EINPROGRESS;
604 }
605
606 /*
607 * Driver will wait for completion DESCMISS transfer,
608 * before starts new, not DESCMISS transfer.
609 */
610 if (!pending_empty && !descmiss_empty) {
611 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
612 deferred = 1;
613 }
614
615 if (priv_req->flags & REQUEST_INTERNAL)
616 list_add_tail(&priv_req->list,
617 &priv_ep->wa2_descmiss_req_list);
618 }
619
620 return deferred;
621}
622
623static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
624{
625 struct cdns3_request *priv_req;
626
627 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
628 u8 chain;
629
630 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
631 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
632
633 trace_cdns3_wa2(priv_ep, "removes eldest request");
634
635 kfree(priv_req->request.buf);
636 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
637 &priv_req->request);
638 list_del_init(&priv_req->list);
639 --priv_ep->wa2_counter;
640
641 if (!chain)
642 break;
643 }
644}
645
646/**
647 * cdns3_wa2_descmissing_packet - handles descriptor missing event.
Lee Jones4a35aa62020-07-02 15:46:12 +0100648 * @priv_ep: extended gadget object
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100649 *
650 * This function is used only for WA2. For more information see Work around 2
651 * description.
652 */
653static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
654{
655 struct cdns3_request *priv_req;
656 struct usb_request *request;
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100657 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
658
659 /* check for pending transfer */
660 if (!pending_empty) {
661 trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
662 return;
663 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100664
665 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
666 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
667 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
668 }
669
670 trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
671
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100672 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
673 trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100674 cdns3_wa2_remove_old_request(priv_ep);
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100675 }
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100676
677 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
678 GFP_ATOMIC);
679 if (!request)
680 goto err;
681
682 priv_req = to_cdns3_request(request);
683 priv_req->flags |= REQUEST_INTERNAL;
684
685 /* if this field is still assigned it indicate that transfer related
686 * with this request has not been finished yet. Driver in this
687 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
688 * flag to previous one. It will indicate that current request is
689 * part of the previous one.
690 */
691 if (priv_ep->descmis_req)
692 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
693
694 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
695 GFP_ATOMIC);
696 priv_ep->wa2_counter++;
697
698 if (!priv_req->request.buf) {
699 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
700 goto err;
701 }
702
703 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
704 priv_ep->descmis_req = priv_req;
705
706 __cdns3_gadget_ep_queue(&priv_ep->endpoint,
707 &priv_ep->descmis_req->request,
708 GFP_ATOMIC);
709
710 return;
711
712err:
713 dev_err(priv_ep->cdns3_dev->dev,
714 "Failed: No sufficient memory for DESCMIS\n");
715}
716
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100717static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
718{
719 u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
720
721 if (tdl) {
722 u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
723
724 writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
725 &priv_dev->regs->ep_cmd);
726 }
727}
728
729static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
730{
731 u32 ep_sts_reg;
732
733 /* select EP0-out */
734 cdns3_select_ep(priv_dev, 0);
735
736 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
737
738 if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
739 u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
740 struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
741
742 if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
743 outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
744 u8 pending_empty = list_empty(&outq_ep->pending_req_list);
745
746 if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
747 (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
748 !pending_empty) {
749 } else {
750 u32 ep_sts_en_reg;
751 u32 ep_cmd_reg;
752
753 cdns3_select_ep(priv_dev, outq_ep->num |
754 outq_ep->dir);
755 ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
756 ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
757
758 outq_ep->flags |= EP_TDLCHK_EN;
759 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
760 EP_CFG_TDL_CHK);
761
762 cdns3_wa2_enable_detection(priv_dev, outq_ep,
763 ep_sts_en_reg);
764 writel(ep_sts_en_reg,
765 &priv_dev->regs->ep_sts_en);
766 /* reset tdl value to zero */
767 cdns3_wa2_reset_tdl(priv_dev);
768 /*
769 * Memory barrier - Reset tdl before ringing the
770 * doorbell.
771 */
772 wmb();
773 if (EP_CMD_DRDY & ep_cmd_reg) {
774 trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
775
776 } else {
777 trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
778 /*
779 * ring doorbell to generate DESCMIS irq
780 */
781 writel(EP_CMD_DRDY,
782 &priv_dev->regs->ep_cmd);
783 }
784 }
785 }
786 }
787}
788
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100789/**
790 * cdns3_gadget_giveback - call struct usb_request's ->complete callback
791 * @priv_ep: The endpoint to whom the request belongs to
792 * @priv_req: The request we're giving back
793 * @status: completion code for the request
794 *
795 * Must be called with controller's lock held and interrupts disabled. This
796 * function will unmap @req and call its ->complete() callback to notify upper
797 * layers that it has completed.
798 */
799void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
800 struct cdns3_request *priv_req,
801 int status)
802{
803 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
804 struct usb_request *request = &priv_req->request;
805
806 list_del_init(&request->list);
807
808 if (request->status == -EINPROGRESS)
809 request->status = status;
810
811 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
812 priv_ep->dir);
813
814 if ((priv_req->flags & REQUEST_UNALIGNED) &&
815 priv_ep->dir == USB_DIR_OUT && !request->status)
816 memcpy(request->buf, priv_req->aligned_buf->buf,
817 request->length);
818
819 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
820 trace_cdns3_gadget_giveback(priv_req);
821
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +0100822 if (priv_dev->dev_ver < DEV_VER_V2) {
823 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
824 priv_req);
825 if (!request)
826 return;
827 }
828
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100829 if (request->complete) {
830 spin_unlock(&priv_dev->lock);
831 usb_gadget_giveback_request(&priv_ep->endpoint,
832 request);
833 spin_lock(&priv_dev->lock);
834 }
835
836 if (request->buf == priv_dev->zlp_buf)
837 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
838}
839
Jason Yane9010322020-04-02 20:38:37 +0800840static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100841{
842 /* Work around for stale data address in TRB*/
843 if (priv_ep->wa1_set) {
844 trace_cdns3_wa1(priv_ep, "restore cycle bit");
845
846 priv_ep->wa1_set = 0;
847 priv_ep->wa1_trb_index = 0xFFFF;
848 if (priv_ep->wa1_cycle_bit) {
849 priv_ep->wa1_trb->control =
850 priv_ep->wa1_trb->control | 0x1;
851 } else {
852 priv_ep->wa1_trb->control =
853 priv_ep->wa1_trb->control & ~0x1;
854 }
855 }
856}
857
858static void cdns3_free_aligned_request_buf(struct work_struct *work)
859{
860 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
861 aligned_buf_wq);
862 struct cdns3_aligned_buf *buf, *tmp;
863 unsigned long flags;
864
865 spin_lock_irqsave(&priv_dev->lock, flags);
866
867 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
868 if (!buf->in_use) {
869 list_del(&buf->list);
870
871 /*
872 * Re-enable interrupts to free DMA capable memory.
873 * Driver can't free this memory with disabled
874 * interrupts.
875 */
876 spin_unlock_irqrestore(&priv_dev->lock, flags);
877 dma_free_coherent(priv_dev->sysdev, buf->size,
878 buf->buf, buf->dma);
879 kfree(buf);
880 spin_lock_irqsave(&priv_dev->lock, flags);
881 }
882 }
883
884 spin_unlock_irqrestore(&priv_dev->lock, flags);
885}
886
887static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
888{
889 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
890 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
891 struct cdns3_aligned_buf *buf;
892
893 /* check if buffer is aligned to 8. */
894 if (!((uintptr_t)priv_req->request.buf & 0x7))
895 return 0;
896
897 buf = priv_req->aligned_buf;
898
899 if (!buf || priv_req->request.length > buf->size) {
900 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
901 if (!buf)
902 return -ENOMEM;
903
904 buf->size = priv_req->request.length;
905
906 buf->buf = dma_alloc_coherent(priv_dev->sysdev,
907 buf->size,
908 &buf->dma,
909 GFP_ATOMIC);
910 if (!buf->buf) {
911 kfree(buf);
912 return -ENOMEM;
913 }
914
915 if (priv_req->aligned_buf) {
916 trace_cdns3_free_aligned_request(priv_req);
917 priv_req->aligned_buf->in_use = 0;
918 queue_work(system_freezable_wq,
919 &priv_dev->aligned_buf_wq);
920 }
921
922 buf->in_use = 1;
923 priv_req->aligned_buf = buf;
924
925 list_add_tail(&buf->list,
926 &priv_dev->aligned_buf_list);
927 }
928
929 if (priv_ep->dir == USB_DIR_IN) {
930 memcpy(buf->buf, priv_req->request.buf,
931 priv_req->request.length);
932 }
933
934 priv_req->flags |= REQUEST_UNALIGNED;
935 trace_cdns3_prepare_aligned_request(priv_req);
936
937 return 0;
938}
939
940static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
941 struct cdns3_trb *trb)
942{
943 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
944
945 if (!priv_ep->wa1_set) {
946 u32 doorbell;
947
948 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
949
950 if (doorbell) {
951 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
952 priv_ep->wa1_set = 1;
953 priv_ep->wa1_trb = trb;
954 priv_ep->wa1_trb_index = priv_ep->enqueue;
955 trace_cdns3_wa1(priv_ep, "set guard");
956 return 0;
957 }
958 }
959 return 1;
960}
961
962static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
963 struct cdns3_endpoint *priv_ep)
964{
965 int dma_index;
966 u32 doorbell;
967
968 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
969 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
970
971 if (!doorbell || dma_index != priv_ep->wa1_trb_index)
972 cdns3_wa1_restore_cycle_bit(priv_ep);
973}
974
Jayshri Pawar54c4c692019-12-13 06:25:42 +0100975static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
976 struct usb_request *request)
977{
978 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
979 struct cdns3_request *priv_req;
980 struct cdns3_trb *trb;
981 dma_addr_t trb_dma;
982 int address;
983 u32 control;
984 u32 length;
985 u32 tdl;
986 unsigned int sg_idx = priv_ep->stream_sg_idx;
987
988 priv_req = to_cdns3_request(request);
989 address = priv_ep->endpoint.desc->bEndpointAddress;
990
991 priv_ep->flags |= EP_PENDING_REQUEST;
992
993 /* must allocate buffer aligned to 8 */
994 if (priv_req->flags & REQUEST_UNALIGNED)
995 trb_dma = priv_req->aligned_buf->dma;
996 else
997 trb_dma = request->dma;
998
999 /* For stream capable endpoints driver use only single TD. */
1000 trb = priv_ep->trb_pool + priv_ep->enqueue;
1001 priv_req->start_trb = priv_ep->enqueue;
1002 priv_req->end_trb = priv_req->start_trb;
1003 priv_req->trb = trb;
1004
1005 cdns3_select_ep(priv_ep->cdns3_dev, address);
1006
1007 control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
1008 TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
1009
1010 if (!request->num_sgs) {
1011 trb->buffer = TRB_BUFFER(trb_dma);
1012 length = request->length;
1013 } else {
1014 trb->buffer = TRB_BUFFER(request->sg[sg_idx].dma_address);
1015 length = request->sg[sg_idx].length;
1016 }
1017
1018 tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
1019
1020 trb->length = TRB_BURST_LEN(16 /*priv_ep->trb_burst_size*/) |
1021 TRB_LEN(length);
1022
1023 /*
1024 * For DEV_VER_V2 controller version we have enabled
1025 * USB_CONF2_EN_TDL_TRB in DMULT configuration.
1026 * This enables TDL calculation based on TRB, hence setting TDL in TRB.
1027 */
1028 if (priv_dev->dev_ver >= DEV_VER_V2) {
1029 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1030 trb->length |= TRB_TDL_SS_SIZE(tdl);
1031 }
1032 priv_req->flags |= REQUEST_PENDING;
1033
1034 trb->control = control;
1035
1036 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1037
1038 /*
1039 * Memory barrier - Cycle Bit must be set before trb->length and
1040 * trb->buffer fields.
1041 */
1042 wmb();
1043
1044 /* always first element */
1045 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
1046 &priv_dev->regs->ep_traddr);
1047
1048 if (!(priv_ep->flags & EP_STALLED)) {
1049 trace_cdns3_ring(priv_ep);
1050 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1051 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1052
1053 priv_ep->prime_flag = false;
1054
1055 /*
1056 * Controller version DEV_VER_V2 tdl calculation
1057 * is based on TRB
1058 */
1059
1060 if (priv_dev->dev_ver < DEV_VER_V2)
1061 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1062 &priv_dev->regs->ep_cmd);
1063 else if (priv_dev->dev_ver > DEV_VER_V2)
1064 writel(tdl, &priv_dev->regs->ep_tdl);
1065
1066 priv_ep->last_stream_id = priv_req->request.stream_id;
1067 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1068 writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
1069 EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
1070
1071 trace_cdns3_doorbell_epx(priv_ep->name,
1072 readl(&priv_dev->regs->ep_traddr));
1073 }
1074
1075 /* WORKAROUND for transition to L0 */
1076 __cdns3_gadget_wakeup(priv_dev);
1077
1078 return 0;
1079}
1080
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001081/**
1082 * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
1083 * @priv_ep: endpoint object
Lee Jones4a35aa62020-07-02 15:46:12 +01001084 * @request: request object
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001085 *
1086 * Returns zero on success or negative value on failure
1087 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001088static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
1089 struct usb_request *request)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001090{
1091 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1092 struct cdns3_request *priv_req;
1093 struct cdns3_trb *trb;
1094 dma_addr_t trb_dma;
1095 u32 togle_pcs = 1;
1096 int sg_iter = 0;
1097 int num_trb;
1098 int address;
1099 u32 control;
1100 int pcs;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001101 u16 total_tdl = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001102
1103 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1104 num_trb = priv_ep->interval;
1105 else
1106 num_trb = request->num_sgs ? request->num_sgs : 1;
1107
1108 if (num_trb > priv_ep->free_trbs) {
1109 priv_ep->flags |= EP_RING_FULL;
1110 return -ENOBUFS;
1111 }
1112
1113 priv_req = to_cdns3_request(request);
1114 address = priv_ep->endpoint.desc->bEndpointAddress;
1115
1116 priv_ep->flags |= EP_PENDING_REQUEST;
1117
1118 /* must allocate buffer aligned to 8 */
1119 if (priv_req->flags & REQUEST_UNALIGNED)
1120 trb_dma = priv_req->aligned_buf->dma;
1121 else
1122 trb_dma = request->dma;
1123
1124 trb = priv_ep->trb_pool + priv_ep->enqueue;
1125 priv_req->start_trb = priv_ep->enqueue;
1126 priv_req->trb = trb;
1127
1128 cdns3_select_ep(priv_ep->cdns3_dev, address);
1129
1130 /* prepare ring */
1131 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
1132 struct cdns3_trb *link_trb;
1133 int doorbell, dma_index;
1134 u32 ch_bit = 0;
1135
1136 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1137 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1138
1139 /* Driver can't update LINK TRB if it is current processed. */
1140 if (doorbell && dma_index == priv_ep->num_trbs - 1) {
1141 priv_ep->flags |= EP_DEFERRED_DRDY;
1142 return -ENOBUFS;
1143 }
1144
1145 /*updating C bt in Link TRB before starting DMA*/
1146 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
1147 /*
1148 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
1149 * that DMA stuck at the LINK TRB.
1150 * On the other hand, removing TRB_CHAIN for longer TRs for
1151 * epXout cause that DMA stuck after handling LINK TRB.
1152 * To eliminate this strange behavioral driver set TRB_CHAIN
1153 * bit only for TR size > 2.
1154 */
1155 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
1156 TRBS_PER_SEGMENT > 2)
1157 ch_bit = TRB_CHAIN;
1158
1159 link_trb->control = ((priv_ep->pcs) ? TRB_CYCLE : 0) |
1160 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit;
1161 }
1162
1163 if (priv_dev->dev_ver <= DEV_VER_V2)
1164 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
1165
1166 /* set incorrect Cycle Bit for first trb*/
1167 control = priv_ep->pcs ? 0 : TRB_CYCLE;
1168
1169 do {
1170 u32 length;
1171 u16 td_size = 0;
1172
1173 /* fill TRB */
1174 control |= TRB_TYPE(TRB_NORMAL);
1175 trb->buffer = TRB_BUFFER(request->num_sgs == 0
1176 ? trb_dma : request->sg[sg_iter].dma_address);
1177
1178 if (likely(!request->num_sgs))
1179 length = request->length;
1180 else
1181 length = request->sg[sg_iter].length;
1182
1183 if (likely(priv_dev->dev_ver >= DEV_VER_V2))
1184 td_size = DIV_ROUND_UP(length,
1185 priv_ep->endpoint.maxpacket);
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001186 else if (priv_ep->flags & EP_TDLCHK_EN)
1187 total_tdl += DIV_ROUND_UP(length,
1188 priv_ep->endpoint.maxpacket);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001189
1190 trb->length = TRB_BURST_LEN(priv_ep->trb_burst_size) |
1191 TRB_LEN(length);
1192 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1193 trb->length |= TRB_TDL_SS_SIZE(td_size);
1194 else
1195 control |= TRB_TDL_HS_SIZE(td_size);
1196
1197 pcs = priv_ep->pcs ? TRB_CYCLE : 0;
1198
1199 /*
1200 * first trb should be prepared as last to avoid processing
1201 * transfer to early
1202 */
1203 if (sg_iter != 0)
1204 control |= pcs;
1205
1206 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
1207 control |= TRB_IOC | TRB_ISP;
1208 } else {
1209 /* for last element in TD or in SG list */
1210 if (sg_iter == (num_trb - 1) && sg_iter != 0)
1211 control |= pcs | TRB_IOC | TRB_ISP;
1212 }
1213
1214 if (sg_iter)
1215 trb->control = control;
1216 else
1217 priv_req->trb->control = control;
1218
1219 control = 0;
1220 ++sg_iter;
1221 priv_req->end_trb = priv_ep->enqueue;
1222 cdns3_ep_inc_enq(priv_ep);
1223 trb = priv_ep->trb_pool + priv_ep->enqueue;
1224 } while (sg_iter < num_trb);
1225
1226 trb = priv_req->trb;
1227
1228 priv_req->flags |= REQUEST_PENDING;
1229
1230 if (sg_iter == 1)
1231 trb->control |= TRB_IOC | TRB_ISP;
1232
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001233 if (priv_dev->dev_ver < DEV_VER_V2 &&
1234 (priv_ep->flags & EP_TDLCHK_EN)) {
1235 u16 tdl = total_tdl;
1236 u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
1237
1238 if (tdl > EP_CMD_TDL_MAX) {
1239 tdl = EP_CMD_TDL_MAX;
1240 priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
1241 }
1242
1243 if (old_tdl < tdl) {
1244 tdl -= old_tdl;
1245 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1246 &priv_dev->regs->ep_cmd);
1247 }
1248 }
1249
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001250 /*
1251 * Memory barrier - cycle bit must be set before other filds in trb.
1252 */
1253 wmb();
1254
1255 /* give the TD to the consumer*/
1256 if (togle_pcs)
1257 trb->control = trb->control ^ 1;
1258
1259 if (priv_dev->dev_ver <= DEV_VER_V2)
1260 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
1261
1262 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1263
1264 /*
1265 * Memory barrier - Cycle Bit must be set before trb->length and
1266 * trb->buffer fields.
1267 */
1268 wmb();
1269
1270 /*
1271 * For DMULT mode we can set address to transfer ring only once after
1272 * enabling endpoint.
1273 */
1274 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
1275 /*
1276 * Until SW is not ready to handle the OUT transfer the ISO OUT
1277 * Endpoint should be disabled (EP_CFG.ENABLE = 0).
1278 * EP_CFG_ENABLE must be set before updating ep_traddr.
1279 */
1280 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
1281 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
1282 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
1283 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
1284 EP_CFG_ENABLE);
1285 }
1286
1287 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
1288 priv_req->start_trb * TRB_SIZE),
1289 &priv_dev->regs->ep_traddr);
1290
1291 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
1292 }
1293
1294 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
1295 trace_cdns3_ring(priv_ep);
1296 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1297 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1298 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1299 trace_cdns3_doorbell_epx(priv_ep->name,
1300 readl(&priv_dev->regs->ep_traddr));
1301 }
1302
1303 /* WORKAROUND for transition to L0 */
1304 __cdns3_gadget_wakeup(priv_dev);
1305
1306 return 0;
1307}
1308
1309void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1310{
1311 struct cdns3_endpoint *priv_ep;
1312 struct usb_ep *ep;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001313
1314 if (priv_dev->hw_configured_flag)
1315 return;
1316
1317 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001318
1319 cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1320 USB_CONF_U1EN | USB_CONF_U2EN);
1321
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001322 priv_dev->hw_configured_flag = 1;
1323
1324 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1325 if (ep->enabled) {
1326 priv_ep = ep_to_cdns3_ep(ep);
1327 cdns3_start_all_request(priv_dev, priv_ep);
1328 }
1329 }
Peter Chenf4cfe5c2020-07-17 18:13:17 +08001330
1331 cdns3_allow_enable_l1(priv_dev, 1);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001332}
1333
1334/**
1335 * cdns3_request_handled - check whether request has been handled by DMA
1336 *
1337 * @priv_ep: extended endpoint object.
1338 * @priv_req: request object for checking
1339 *
1340 * Endpoint must be selected before invoking this function.
1341 *
1342 * Returns false if request has not been handled by DMA, else returns true.
1343 *
1344 * SR - start ring
1345 * ER - end ring
1346 * DQ = priv_ep->dequeue - dequeue position
1347 * EQ = priv_ep->enqueue - enqueue position
1348 * ST = priv_req->start_trb - index of first TRB in transfer ring
1349 * ET = priv_req->end_trb - index of last TRB in transfer ring
1350 * CI = current_index - index of processed TRB by DMA.
1351 *
1352 * As first step, function checks if cycle bit for priv_req->start_trb is
1353 * correct.
1354 *
1355 * some rules:
1356 * 1. priv_ep->dequeue never exceed current_index.
1357 * 2 priv_ep->enqueue never exceed priv_ep->dequeue
1358 * 3. exception: priv_ep->enqueue == priv_ep->dequeue
1359 * and priv_ep->free_trbs is zero.
1360 * This case indicate that TR is full.
1361 *
1362 * Then We can split recognition into two parts:
1363 * Case 1 - priv_ep->dequeue < current_index
1364 * SR ... EQ ... DQ ... CI ... ER
1365 * SR ... DQ ... CI ... EQ ... ER
1366 *
1367 * Request has been handled by DMA if ST and ET is between DQ and CI.
1368 *
1369 * Case 2 - priv_ep->dequeue > current_index
1370 * This situation take place when CI go through the LINK TRB at the end of
1371 * transfer ring.
1372 * SR ... CI ... EQ ... DQ ... ER
1373 *
1374 * Request has been handled by DMA if ET is less then CI or
1375 * ET is greater or equal DQ.
1376 */
1377static bool cdns3_request_handled(struct cdns3_endpoint *priv_ep,
1378 struct cdns3_request *priv_req)
1379{
1380 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
Colin Ian King1f9f5a82020-02-08 16:18:02 +00001381 struct cdns3_trb *trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001382 int current_index = 0;
1383 int handled = 0;
1384 int doorbell;
1385
1386 current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1387 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1388
1389 trb = &priv_ep->trb_pool[priv_req->start_trb];
1390
1391 if ((trb->control & TRB_CYCLE) != priv_ep->ccs)
1392 goto finish;
1393
1394 if (doorbell == 1 && current_index == priv_ep->dequeue)
1395 goto finish;
1396
1397 /* The corner case for TRBS_PER_SEGMENT equal 2). */
1398 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1399 handled = 1;
1400 goto finish;
1401 }
1402
1403 if (priv_ep->enqueue == priv_ep->dequeue &&
1404 priv_ep->free_trbs == 0) {
1405 handled = 1;
1406 } else if (priv_ep->dequeue < current_index) {
1407 if ((current_index == (priv_ep->num_trbs - 1)) &&
1408 !priv_ep->dequeue)
1409 goto finish;
1410
1411 if (priv_req->end_trb >= priv_ep->dequeue &&
1412 priv_req->end_trb < current_index)
1413 handled = 1;
1414 } else if (priv_ep->dequeue > current_index) {
1415 if (priv_req->end_trb < current_index ||
1416 priv_req->end_trb >= priv_ep->dequeue)
1417 handled = 1;
1418 }
1419
1420finish:
1421 trace_cdns3_request_handled(priv_req, current_index, handled);
1422
1423 return handled;
1424}
1425
1426static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1427 struct cdns3_endpoint *priv_ep)
1428{
1429 struct cdns3_request *priv_req;
1430 struct usb_request *request;
1431 struct cdns3_trb *trb;
1432
1433 while (!list_empty(&priv_ep->pending_req_list)) {
1434 request = cdns3_next_request(&priv_ep->pending_req_list);
1435 priv_req = to_cdns3_request(request);
1436
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01001437 trb = priv_ep->trb_pool + priv_ep->dequeue;
1438
1439 /* Request was dequeued and TRB was changed to TRB_LINK. */
1440 if (TRB_FIELD_TO_TYPE(trb->control) == TRB_LINK) {
1441 trace_cdns3_complete_trb(priv_ep, trb);
1442 cdns3_move_deq_to_next_trb(priv_req);
1443 }
1444
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001445 if (!request->stream_id) {
1446 /* Re-select endpoint. It could be changed by other CPU
1447 * during handling usb_gadget_giveback_request.
1448 */
1449 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001450
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001451 if (!cdns3_request_handled(priv_ep, priv_req))
1452 goto prepare_next_td;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001453
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001454 trb = priv_ep->trb_pool + priv_ep->dequeue;
1455 trace_cdns3_complete_trb(priv_ep, trb);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001456
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001457 if (trb != priv_req->trb)
1458 dev_warn(priv_dev->dev,
1459 "request_trb=0x%p, queue_trb=0x%p\n",
1460 priv_req->trb, trb);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001461
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001462 request->actual = TRB_LEN(le32_to_cpu(trb->length));
1463 cdns3_move_deq_to_next_trb(priv_req);
1464 cdns3_gadget_giveback(priv_ep, priv_req, 0);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001465
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001466 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1467 TRBS_PER_SEGMENT == 2)
1468 break;
1469 } else {
1470 /* Re-select endpoint. It could be changed by other CPU
1471 * during handling usb_gadget_giveback_request.
1472 */
1473 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1474
1475 trb = priv_ep->trb_pool;
1476 trace_cdns3_complete_trb(priv_ep, trb);
1477
1478 if (trb != priv_req->trb)
1479 dev_warn(priv_dev->dev,
1480 "request_trb=0x%p, queue_trb=0x%p\n",
1481 priv_req->trb, trb);
1482
1483 request->actual += TRB_LEN(le32_to_cpu(trb->length));
1484
1485 if (!request->num_sgs ||
1486 (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
1487 priv_ep->stream_sg_idx = 0;
1488 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1489 } else {
1490 priv_ep->stream_sg_idx++;
1491 cdns3_ep_run_stream_transfer(priv_ep, request);
1492 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001493 break;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001494 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001495 }
1496 priv_ep->flags &= ~EP_PENDING_REQUEST;
1497
1498prepare_next_td:
1499 if (!(priv_ep->flags & EP_STALLED) &&
1500 !(priv_ep->flags & EP_STALL_PENDING))
1501 cdns3_start_all_request(priv_dev, priv_ep);
1502}
1503
1504void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1505{
1506 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1507
1508 cdns3_wa1_restore_cycle_bit(priv_ep);
1509
1510 if (rearm) {
1511 trace_cdns3_ring(priv_ep);
1512
1513 /* Cycle Bit must be updated before arming DMA. */
1514 wmb();
1515 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1516
1517 __cdns3_gadget_wakeup(priv_dev);
1518
1519 trace_cdns3_doorbell_epx(priv_ep->name,
1520 readl(&priv_dev->regs->ep_traddr));
1521 }
1522}
1523
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001524static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
1525{
1526 u16 tdl = priv_ep->pending_tdl;
1527 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1528
1529 if (tdl > EP_CMD_TDL_MAX) {
1530 tdl = EP_CMD_TDL_MAX;
1531 priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
1532 } else {
1533 priv_ep->pending_tdl = 0;
1534 }
1535
1536 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
1537}
1538
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001539/**
1540 * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
1541 * @priv_ep: endpoint object
1542 *
1543 * Returns 0
1544 */
1545static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1546{
1547 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1548 u32 ep_sts_reg;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001549 struct usb_request *deferred_request;
1550 struct usb_request *pending_request;
1551 u32 tdl = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001552
1553 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1554
1555 trace_cdns3_epx_irq(priv_dev, priv_ep);
1556
1557 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1558 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1559
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001560 if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
1561 bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
1562
1563 tdl = cdns3_get_tdl(priv_dev);
1564
1565 /*
1566 * Continue the previous transfer:
1567 * There is some racing between ERDY and PRIME. The device send
1568 * ERDY and almost in the same time Host send PRIME. It cause
1569 * that host ignore the ERDY packet and driver has to send it
1570 * again.
1571 */
1572 if (tdl && (dbusy | !EP_STS_BUFFEMPTY(ep_sts_reg) |
1573 EP_STS_HOSTPP(ep_sts_reg))) {
1574 writel(EP_CMD_ERDY |
1575 EP_CMD_ERDY_SID(priv_ep->last_stream_id),
1576 &priv_dev->regs->ep_cmd);
1577 ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
1578 } else {
1579 priv_ep->prime_flag = true;
1580
1581 pending_request = cdns3_next_request(&priv_ep->pending_req_list);
1582 deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
1583
1584 if (deferred_request && !pending_request) {
1585 cdns3_start_all_request(priv_dev, priv_ep);
1586 }
1587 }
1588 }
1589
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001590 if (ep_sts_reg & EP_STS_TRBERR) {
1591 if (priv_ep->flags & EP_STALL_PENDING &&
1592 !(ep_sts_reg & EP_STS_DESCMIS &&
1593 priv_dev->dev_ver < DEV_VER_V2)) {
1594 cdns3_ep_stall_flush(priv_ep);
1595 }
1596
1597 /*
1598 * For isochronous transfer driver completes request on
1599 * IOC or on TRBERR. IOC appears only when device receive
1600 * OUT data packet. If host disable stream or lost some packet
1601 * then the only way to finish all queued transfer is to do it
1602 * on TRBERR event.
1603 */
1604 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1605 !priv_ep->wa1_set) {
1606 if (!priv_ep->dir) {
1607 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1608
1609 ep_cfg &= ~EP_CFG_ENABLE;
1610 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1611 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1612 }
1613 cdns3_transfer_completed(priv_dev, priv_ep);
1614 } else if (!(priv_ep->flags & EP_STALLED) &&
1615 !(priv_ep->flags & EP_STALL_PENDING)) {
1616 if (priv_ep->flags & EP_DEFERRED_DRDY) {
1617 priv_ep->flags &= ~EP_DEFERRED_DRDY;
1618 cdns3_start_all_request(priv_dev, priv_ep);
1619 } else {
1620 cdns3_rearm_transfer(priv_ep,
1621 priv_ep->wa1_set);
1622 }
1623 }
1624 }
1625
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001626 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
1627 (ep_sts_reg & EP_STS_IOT)) {
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001628 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1629 if (ep_sts_reg & EP_STS_ISP)
1630 priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1631 else
1632 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1633 }
1634
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001635 if (!priv_ep->use_streams) {
1636 if ((ep_sts_reg & EP_STS_IOC) ||
1637 (ep_sts_reg & EP_STS_ISP)) {
1638 cdns3_transfer_completed(priv_dev, priv_ep);
1639 } else if ((priv_ep->flags & EP_TDLCHK_EN) &
1640 priv_ep->pending_tdl) {
1641 /* handle IOT with pending tdl */
1642 cdns3_reprogram_tdl(priv_ep);
1643 }
1644 } else if (priv_ep->dir == USB_DIR_OUT) {
1645 priv_ep->ep_sts_pending |= ep_sts_reg;
1646 } else if (ep_sts_reg & EP_STS_IOT) {
1647 cdns3_transfer_completed(priv_dev, priv_ep);
1648 }
1649 }
1650
1651 /*
1652 * MD_EXIT interrupt sets when stream capable endpoint exits
1653 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
1654 */
1655 if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
1656 (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
1657 priv_ep->ep_sts_pending = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001658 cdns3_transfer_completed(priv_dev, priv_ep);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001659 }
1660
1661 /*
1662 * WA2: this condition should only be meet when
1663 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
1664 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001665 * In other cases this interrupt will be disabled.
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01001666 */
1667 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1668 !(priv_ep->flags & EP_STALLED))
1669 cdns3_wa2_descmissing_packet(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001670
1671 return 0;
1672}
1673
1674static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1675{
1676 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect) {
1677 spin_unlock(&priv_dev->lock);
1678 priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
1679 spin_lock(&priv_dev->lock);
1680 }
1681}
1682
1683/**
1684 * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
1685 * @priv_dev: extended gadget object
1686 * @usb_ists: bitmap representation of device's reported interrupts
1687 * (usb_ists register value)
1688 */
1689static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1690 u32 usb_ists)
1691{
1692 int speed = 0;
1693
1694 trace_cdns3_usb_irq(priv_dev, usb_ists);
1695 if (usb_ists & USB_ISTS_L1ENTI) {
1696 /*
1697 * WORKAROUND: CDNS3 controller has issue with hardware resuming
1698 * from L1. To fix it, if any DMA transfer is pending driver
1699 * must starts driving resume signal immediately.
1700 */
1701 if (readl(&priv_dev->regs->drbl))
1702 __cdns3_gadget_wakeup(priv_dev);
1703 }
1704
1705 /* Connection detected */
1706 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1707 speed = cdns3_get_speed(priv_dev);
1708 priv_dev->gadget.speed = speed;
1709 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1710 cdns3_ep0_config(priv_dev);
1711 }
1712
1713 /* Disconnection detected */
1714 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
1715 cdns3_disconnect_gadget(priv_dev);
1716 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1717 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1718 cdns3_hw_reset_eps_config(priv_dev);
1719 }
1720
1721 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1722 if (priv_dev->gadget_driver &&
1723 priv_dev->gadget_driver->suspend) {
1724 spin_unlock(&priv_dev->lock);
1725 priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1726 spin_lock(&priv_dev->lock);
1727 }
1728 }
1729
1730 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1731 if (priv_dev->gadget_driver &&
1732 priv_dev->gadget_driver->resume) {
1733 spin_unlock(&priv_dev->lock);
1734 priv_dev->gadget_driver->resume(&priv_dev->gadget);
1735 spin_lock(&priv_dev->lock);
1736 }
1737 }
1738
1739 /* reset*/
1740 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1741 if (priv_dev->gadget_driver) {
1742 spin_unlock(&priv_dev->lock);
1743 usb_gadget_udc_reset(&priv_dev->gadget,
1744 priv_dev->gadget_driver);
1745 spin_lock(&priv_dev->lock);
1746
1747 /*read again to check the actual speed*/
1748 speed = cdns3_get_speed(priv_dev);
1749 priv_dev->gadget.speed = speed;
1750 cdns3_hw_reset_eps_config(priv_dev);
1751 cdns3_ep0_config(priv_dev);
1752 }
1753 }
1754}
1755
1756/**
1757 * cdns3_device_irq_handler- interrupt handler for device part of controller
1758 *
1759 * @irq: irq number for cdns3 core device
1760 * @data: structure of cdns3
1761 *
1762 * Returns IRQ_HANDLED or IRQ_NONE
1763 */
1764static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1765{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001766 struct cdns3_device *priv_dev = data;
Peter Chenb1234e32020-09-02 17:57:32 +08001767 struct cdns3 *cdns = dev_get_drvdata(priv_dev->dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001768 irqreturn_t ret = IRQ_NONE;
1769 u32 reg;
1770
Peter Chenb1234e32020-09-02 17:57:32 +08001771 if (cdns->in_lpm)
1772 return ret;
1773
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001774 /* check USB device interrupt */
1775 reg = readl(&priv_dev->regs->usb_ists);
1776 if (reg) {
1777 /* After masking interrupts the new interrupts won't be
1778 * reported in usb_ists/ep_ists. In order to not lose some
1779 * of them driver disables only detected interrupts.
1780 * They will be enabled ASAP after clearing source of
1781 * interrupt. This an unusual behavior only applies to
1782 * usb_ists register.
1783 */
1784 reg = ~reg & readl(&priv_dev->regs->usb_ien);
1785 /* mask deferred interrupt. */
1786 writel(reg, &priv_dev->regs->usb_ien);
1787 ret = IRQ_WAKE_THREAD;
1788 }
1789
1790 /* check endpoint interrupt */
1791 reg = readl(&priv_dev->regs->ep_ists);
1792 if (reg) {
1793 writel(0, &priv_dev->regs->ep_ien);
1794 ret = IRQ_WAKE_THREAD;
1795 }
1796
1797 return ret;
1798}
1799
1800/**
1801 * cdns3_device_thread_irq_handler- interrupt handler for device part
1802 * of controller
1803 *
1804 * @irq: irq number for cdns3 core device
1805 * @data: structure of cdns3
1806 *
1807 * Returns IRQ_HANDLED or IRQ_NONE
1808 */
1809static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1810{
Peter Chenaf58e1f2019-12-27 17:10:04 +08001811 struct cdns3_device *priv_dev = data;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001812 irqreturn_t ret = IRQ_NONE;
1813 unsigned long flags;
Peter Chen06825ca2020-06-23 11:10:01 +08001814 unsigned int bit;
Peter Chen8685c462020-06-23 11:10:00 +08001815 unsigned long reg;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001816
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001817 spin_lock_irqsave(&priv_dev->lock, flags);
1818
1819 reg = readl(&priv_dev->regs->usb_ists);
1820 if (reg) {
1821 writel(reg, &priv_dev->regs->usb_ists);
1822 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1823 cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1824 ret = IRQ_HANDLED;
1825 }
1826
1827 reg = readl(&priv_dev->regs->ep_ists);
1828
1829 /* handle default endpoint OUT */
1830 if (reg & EP_ISTS_EP_OUT0) {
1831 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1832 ret = IRQ_HANDLED;
1833 }
1834
1835 /* handle default endpoint IN */
1836 if (reg & EP_ISTS_EP_IN0) {
1837 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1838 ret = IRQ_HANDLED;
1839 }
1840
1841 /* check if interrupt from non default endpoint, if no exit */
1842 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1843 if (!reg)
1844 goto irqend;
1845
Peter Chen8685c462020-06-23 11:10:00 +08001846 for_each_set_bit(bit, &reg,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001847 sizeof(u32) * BITS_PER_BYTE) {
1848 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1849 ret = IRQ_HANDLED;
1850 }
1851
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001852 if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
1853 cdns3_wa2_check_outq_status(priv_dev);
1854
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001855irqend:
1856 writel(~0, &priv_dev->regs->ep_ien);
1857 spin_unlock_irqrestore(&priv_dev->lock, flags);
1858
1859 return ret;
1860}
1861
1862/**
1863 * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
1864 *
1865 * The real reservation will occur during write to EP_CFG register,
1866 * this function is used to check if the 'size' reservation is allowed.
1867 *
1868 * @priv_dev: extended gadget object
1869 * @size: the size (KB) for EP would like to allocate
1870 * @is_in: endpoint direction
1871 *
1872 * Return 0 if the required size can met or negative value on failure
1873 */
1874static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
1875 int size, int is_in)
1876{
1877 int remained;
1878
1879 /* 2KB are reserved for EP0*/
1880 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
1881
1882 if (is_in) {
1883 if (remained < size)
1884 return -EPERM;
1885
1886 priv_dev->onchip_used_size += size;
1887 } else {
1888 int required;
1889
1890 /**
1891 * ALL OUT EPs are shared the same chunk onchip memory, so
1892 * driver checks if it already has assigned enough buffers
1893 */
1894 if (priv_dev->out_mem_is_allocated >= size)
1895 return 0;
1896
1897 required = size - priv_dev->out_mem_is_allocated;
1898
1899 if (required > remained)
1900 return -EPERM;
1901
1902 priv_dev->out_mem_is_allocated += required;
1903 priv_dev->onchip_used_size += required;
1904 }
1905
1906 return 0;
1907}
1908
Jason Yane9010322020-04-02 20:38:37 +08001909static void cdns3_stream_ep_reconfig(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +08001910 struct cdns3_endpoint *priv_ep)
Jayshri Pawar54c4c692019-12-13 06:25:42 +01001911{
1912 if (!priv_ep->use_streams || priv_dev->gadget.speed < USB_SPEED_SUPER)
1913 return;
1914
1915 if (priv_dev->dev_ver >= DEV_VER_V3) {
1916 u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
1917
1918 /*
1919 * Stream capable endpoints are handled by using ep_tdl
1920 * register. Other endpoints use TDL from TRB feature.
1921 */
1922 cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb, mask);
1923 }
1924
1925 /* Enable Stream Bit TDL chk and SID chk */
1926 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_STREAM_EN |
1927 EP_CFG_TDL_CHK | EP_CFG_SID_CHK);
1928}
1929
Jason Yane9010322020-04-02 20:38:37 +08001930static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
kbuild test robote2e77a92020-03-27 09:12:01 +08001931 struct cdns3_endpoint *priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001932{
1933 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
1934
1935 /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
1936 if (priv_dev->dev_ver <= DEV_VER_V2)
1937 writel(USB_CONF_DMULT, &regs->usb_conf);
1938
1939 if (priv_dev->dev_ver == DEV_VER_V2)
1940 writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
1941
1942 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
1943 u32 mask;
1944
1945 if (priv_ep->dir)
1946 mask = BIT(priv_ep->num + 16);
1947 else
1948 mask = BIT(priv_ep->num);
1949
1950 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1951 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
1952 cdns3_set_register_bit(&regs->tdl_beh, mask);
1953 cdns3_set_register_bit(&regs->tdl_beh2, mask);
1954 cdns3_set_register_bit(&regs->dma_adv_td, mask);
1955 }
1956
1957 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
1958 cdns3_set_register_bit(&regs->tdl_from_trb, mask);
1959
1960 cdns3_set_register_bit(&regs->dtrans, mask);
1961 }
1962}
1963
1964/**
1965 * cdns3_ep_config Configure hardware endpoint
1966 * @priv_ep: extended endpoint object
1967 */
1968void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
1969{
1970 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
1971 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1972 u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
1973 u32 max_packet_size = 0;
1974 u8 maxburst = 0;
1975 u32 ep_cfg = 0;
1976 u8 buffering;
1977 u8 mult = 0;
1978 int ret;
1979
1980 buffering = CDNS3_EP_BUF_SIZE - 1;
1981
1982 cdns3_configure_dmult(priv_dev, priv_ep);
1983
1984 switch (priv_ep->type) {
1985 case USB_ENDPOINT_XFER_INT:
1986 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
1987
1988 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
1989 priv_dev->dev_ver > DEV_VER_V2)
1990 ep_cfg |= EP_CFG_TDL_CHK;
1991 break;
1992 case USB_ENDPOINT_XFER_BULK:
1993 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
1994
1995 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
1996 priv_dev->dev_ver > DEV_VER_V2)
1997 ep_cfg |= EP_CFG_TDL_CHK;
1998 break;
1999 default:
2000 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
2001 mult = CDNS3_EP_ISO_HS_MULT - 1;
2002 buffering = mult + 1;
2003 }
2004
2005 switch (priv_dev->gadget.speed) {
2006 case USB_SPEED_FULL:
2007 max_packet_size = is_iso_ep ? 1023 : 64;
2008 break;
2009 case USB_SPEED_HIGH:
2010 max_packet_size = is_iso_ep ? 1024 : 512;
2011 break;
2012 case USB_SPEED_SUPER:
2013 /* It's limitation that driver assumes in driver. */
2014 mult = 0;
2015 max_packet_size = 1024;
2016 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2017 maxburst = CDNS3_EP_ISO_SS_BURST - 1;
2018 buffering = (mult + 1) *
2019 (maxburst + 1);
2020
2021 if (priv_ep->interval > 1)
2022 buffering++;
2023 } else {
2024 maxburst = CDNS3_EP_BUF_SIZE - 1;
2025 }
2026 break;
2027 default:
2028 /* all other speed are not supported */
2029 return;
2030 }
2031
2032 if (max_packet_size == 1024)
2033 priv_ep->trb_burst_size = 128;
2034 else if (max_packet_size >= 512)
2035 priv_ep->trb_burst_size = 64;
2036 else
2037 priv_ep->trb_burst_size = 16;
2038
2039 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2040 !!priv_ep->dir);
2041 if (ret) {
2042 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2043 return;
2044 }
2045
2046 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
2047 EP_CFG_MULT(mult) |
2048 EP_CFG_BUFFERING(buffering) |
2049 EP_CFG_MAXBURST(maxburst);
2050
2051 cdns3_select_ep(priv_dev, bEndpointAddress);
2052 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2053
2054 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
2055 priv_ep->name, ep_cfg);
2056}
2057
2058/* Find correct direction for HW endpoint according to description */
2059static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
2060 struct cdns3_endpoint *priv_ep)
2061{
2062 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
2063 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
2064}
2065
2066static struct
2067cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
2068 struct usb_endpoint_descriptor *desc)
2069{
2070 struct usb_ep *ep;
2071 struct cdns3_endpoint *priv_ep;
2072
2073 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2074 unsigned long num;
2075 int ret;
2076 /* ep name pattern likes epXin or epXout */
2077 char c[2] = {ep->name[2], '\0'};
2078
2079 ret = kstrtoul(c, 10, &num);
2080 if (ret)
2081 return ERR_PTR(ret);
2082
2083 priv_ep = ep_to_cdns3_ep(ep);
2084 if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
2085 if (!(priv_ep->flags & EP_CLAIMED)) {
2086 priv_ep->num = num;
2087 return priv_ep;
2088 }
2089 }
2090 }
2091
2092 return ERR_PTR(-ENOENT);
2093}
2094
2095/*
2096 * Cadence IP has one limitation that all endpoints must be configured
2097 * (Type & MaxPacketSize) before setting configuration through hardware
2098 * register, it means we can't change endpoints configuration after
2099 * set_configuration.
2100 *
2101 * This function set EP_CLAIMED flag which is added when the gadget driver
2102 * uses usb_ep_autoconfig to configure specific endpoint;
2103 * When the udc driver receives set_configurion request,
2104 * it goes through all claimed endpoints, and configure all endpoints
2105 * accordingly.
2106 *
2107 * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
2108 * ep_cfg register which can be changed after set_configuration, and do
2109 * some software operation accordingly.
2110 */
2111static struct
2112usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
2113 struct usb_endpoint_descriptor *desc,
2114 struct usb_ss_ep_comp_descriptor *comp_desc)
2115{
2116 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2117 struct cdns3_endpoint *priv_ep;
2118 unsigned long flags;
2119
2120 priv_ep = cdns3_find_available_ep(priv_dev, desc);
2121 if (IS_ERR(priv_ep)) {
2122 dev_err(priv_dev->dev, "no available ep\n");
2123 return NULL;
2124 }
2125
2126 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
2127
2128 spin_lock_irqsave(&priv_dev->lock, flags);
2129 priv_ep->endpoint.desc = desc;
2130 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
2131 priv_ep->type = usb_endpoint_type(desc);
2132 priv_ep->flags |= EP_CLAIMED;
2133 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2134
2135 spin_unlock_irqrestore(&priv_dev->lock, flags);
2136 return &priv_ep->endpoint;
2137}
2138
2139/**
2140 * cdns3_gadget_ep_alloc_request Allocates request
2141 * @ep: endpoint object associated with request
2142 * @gfp_flags: gfp flags
2143 *
2144 * Returns allocated request address, NULL on allocation error
2145 */
2146struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
2147 gfp_t gfp_flags)
2148{
2149 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2150 struct cdns3_request *priv_req;
2151
2152 priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
2153 if (!priv_req)
2154 return NULL;
2155
2156 priv_req->priv_ep = priv_ep;
2157
2158 trace_cdns3_alloc_request(priv_req);
2159 return &priv_req->request;
2160}
2161
2162/**
2163 * cdns3_gadget_ep_free_request Free memory occupied by request
2164 * @ep: endpoint object associated with request
2165 * @request: request to free memory
2166 */
2167void cdns3_gadget_ep_free_request(struct usb_ep *ep,
2168 struct usb_request *request)
2169{
2170 struct cdns3_request *priv_req = to_cdns3_request(request);
2171
2172 if (priv_req->aligned_buf)
2173 priv_req->aligned_buf->in_use = 0;
2174
2175 trace_cdns3_free_request(priv_req);
2176 kfree(priv_req);
2177}
2178
2179/**
2180 * cdns3_gadget_ep_enable Enable endpoint
2181 * @ep: endpoint object
2182 * @desc: endpoint descriptor
2183 *
2184 * Returns 0 on success, error code elsewhere
2185 */
2186static int cdns3_gadget_ep_enable(struct usb_ep *ep,
2187 const struct usb_endpoint_descriptor *desc)
2188{
2189 struct cdns3_endpoint *priv_ep;
2190 struct cdns3_device *priv_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002191 const struct usb_ss_ep_comp_descriptor *comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002192 u32 reg = EP_STS_EN_TRBERREN;
2193 u32 bEndpointAddress;
2194 unsigned long flags;
2195 int enable = 1;
2196 int ret;
2197 int val;
2198
2199 priv_ep = ep_to_cdns3_ep(ep);
2200 priv_dev = priv_ep->cdns3_dev;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002201 comp_desc = priv_ep->endpoint.comp_desc;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002202
2203 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
2204 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
2205 return -EINVAL;
2206 }
2207
2208 if (!desc->wMaxPacketSize) {
2209 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
2210 return -EINVAL;
2211 }
2212
2213 if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
2214 "%s is already enabled\n", priv_ep->name))
2215 return 0;
2216
2217 spin_lock_irqsave(&priv_dev->lock, flags);
2218
2219 priv_ep->endpoint.desc = desc;
2220 priv_ep->type = usb_endpoint_type(desc);
2221 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2222
2223 if (priv_ep->interval > ISO_MAX_INTERVAL &&
2224 priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2225 dev_err(priv_dev->dev, "Driver is limited to %d period\n",
2226 ISO_MAX_INTERVAL);
2227
2228 ret = -EINVAL;
2229 goto exit;
2230 }
2231
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002232 bEndpointAddress = priv_ep->num | priv_ep->dir;
2233 cdns3_select_ep(priv_dev, bEndpointAddress);
2234
2235 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
2236 /*
2237 * Enable stream support (SS mode) related interrupts
2238 * in EP_STS_EN Register
2239 */
2240 if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2241 reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
2242 EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
2243 EP_STS_EN_STREAMREN;
2244 priv_ep->use_streams = true;
2245 cdns3_stream_ep_reconfig(priv_dev, priv_ep);
2246 priv_dev->using_streams |= true;
2247 }
2248 }
2249
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002250 ret = cdns3_allocate_trb_pool(priv_ep);
2251
2252 if (ret)
2253 goto exit;
2254
2255 bEndpointAddress = priv_ep->num | priv_ep->dir;
2256 cdns3_select_ep(priv_dev, bEndpointAddress);
2257
2258 trace_cdns3_gadget_ep_enable(priv_ep);
2259
2260 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2261
2262 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2263 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2264 1, 1000);
2265
2266 if (unlikely(ret)) {
2267 cdns3_free_trb_pool(priv_ep);
2268 ret = -EINVAL;
2269 goto exit;
2270 }
2271
2272 /* enable interrupt for selected endpoint */
2273 cdns3_set_register_bit(&priv_dev->regs->ep_ien,
2274 BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
2275
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002276 if (priv_dev->dev_ver < DEV_VER_V2)
2277 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
2278
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002279 writel(reg, &priv_dev->regs->ep_sts_en);
2280
2281 /*
2282 * For some versions of controller at some point during ISO OUT traffic
2283 * DMA reads Transfer Ring for the EP which has never got doorbell.
2284 * This issue was detected only on simulation, but to avoid this issue
2285 * driver add protection against it. To fix it driver enable ISO OUT
2286 * endpoint before setting DRBL. This special treatment of ISO OUT
2287 * endpoints are recommended by controller specification.
2288 */
2289 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2290 enable = 0;
2291
2292 if (enable)
2293 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_ENABLE);
2294
2295 ep->desc = desc;
2296 priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002297 EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002298 priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
2299 priv_ep->wa1_set = 0;
2300 priv_ep->enqueue = 0;
2301 priv_ep->dequeue = 0;
2302 reg = readl(&priv_dev->regs->ep_sts);
2303 priv_ep->pcs = !!EP_STS_CCS(reg);
2304 priv_ep->ccs = !!EP_STS_CCS(reg);
2305 /* one TRB is reserved for link TRB used in DMULT mode*/
2306 priv_ep->free_trbs = priv_ep->num_trbs - 1;
2307exit:
2308 spin_unlock_irqrestore(&priv_dev->lock, flags);
2309
2310 return ret;
2311}
2312
2313/**
2314 * cdns3_gadget_ep_disable Disable endpoint
2315 * @ep: endpoint object
2316 *
2317 * Returns 0 on success, error code elsewhere
2318 */
2319static int cdns3_gadget_ep_disable(struct usb_ep *ep)
2320{
2321 struct cdns3_endpoint *priv_ep;
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002322 struct cdns3_request *priv_req;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002323 struct cdns3_device *priv_dev;
2324 struct usb_request *request;
2325 unsigned long flags;
2326 int ret = 0;
2327 u32 ep_cfg;
2328 int val;
2329
2330 if (!ep) {
2331 pr_err("usbss: invalid parameters\n");
2332 return -EINVAL;
2333 }
2334
2335 priv_ep = ep_to_cdns3_ep(ep);
2336 priv_dev = priv_ep->cdns3_dev;
2337
2338 if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
2339 "%s is already disabled\n", priv_ep->name))
2340 return 0;
2341
2342 spin_lock_irqsave(&priv_dev->lock, flags);
2343
2344 trace_cdns3_gadget_ep_disable(priv_ep);
2345
2346 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2347
2348 ep_cfg = readl(&priv_dev->regs->ep_cfg);
2349 ep_cfg &= ~EP_CFG_ENABLE;
2350 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2351
2352 /**
2353 * Driver needs some time before resetting endpoint.
2354 * It need waits for clearing DBUSY bit or for timeout expired.
2355 * 10us is enough time for controller to stop transfer.
2356 */
2357 readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
2358 !(val & EP_STS_DBUSY), 1, 10);
2359 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2360
2361 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2362 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2363 1, 1000);
2364 if (unlikely(ret))
2365 dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
2366 priv_ep->name);
2367
2368 while (!list_empty(&priv_ep->pending_req_list)) {
2369 request = cdns3_next_request(&priv_ep->pending_req_list);
2370
2371 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2372 -ESHUTDOWN);
2373 }
2374
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002375 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
2376 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
2377
2378 kfree(priv_req->request.buf);
2379 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
2380 &priv_req->request);
2381 list_del_init(&priv_req->list);
2382 --priv_ep->wa2_counter;
2383 }
2384
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002385 while (!list_empty(&priv_ep->deferred_req_list)) {
2386 request = cdns3_next_request(&priv_ep->deferred_req_list);
2387
2388 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2389 -ESHUTDOWN);
2390 }
2391
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002392 priv_ep->descmis_req = NULL;
2393
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002394 ep->desc = NULL;
2395 priv_ep->flags &= ~EP_ENABLED;
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002396 priv_ep->use_streams = false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002397
2398 spin_unlock_irqrestore(&priv_dev->lock, flags);
2399
2400 return ret;
2401}
2402
2403/**
2404 * cdns3_gadget_ep_queue Transfer data on endpoint
2405 * @ep: endpoint object
2406 * @request: request object
2407 * @gfp_flags: gfp flags
2408 *
2409 * Returns 0 on success, error code elsewhere
2410 */
2411static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
2412 struct usb_request *request,
2413 gfp_t gfp_flags)
2414{
2415 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2416 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2417 struct cdns3_request *priv_req;
2418 int ret = 0;
2419
2420 request->actual = 0;
2421 request->status = -EINPROGRESS;
2422 priv_req = to_cdns3_request(request);
2423 trace_cdns3_ep_queue(priv_req);
2424
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002425 if (priv_dev->dev_ver < DEV_VER_V2) {
2426 ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
2427 priv_req);
2428
2429 if (ret == EINPROGRESS)
2430 return 0;
2431 }
2432
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002433 ret = cdns3_prepare_aligned_request_buf(priv_req);
2434 if (ret < 0)
2435 return ret;
2436
2437 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
2438 usb_endpoint_dir_in(ep->desc));
2439 if (ret)
2440 return ret;
2441
2442 list_add_tail(&request->list, &priv_ep->deferred_req_list);
2443
2444 /*
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002445 * For stream capable endpoint if prime irq flag is set then only start
2446 * request.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002447 * If hardware endpoint configuration has not been set yet then
2448 * just queue request in deferred list. Transfer will be started in
2449 * cdns3_set_hw_configuration.
2450 */
Jayshri Pawar54c4c692019-12-13 06:25:42 +01002451 if (!request->stream_id) {
2452 if (priv_dev->hw_configured_flag &&
2453 !(priv_ep->flags & EP_STALLED) &&
2454 !(priv_ep->flags & EP_STALL_PENDING))
2455 cdns3_start_all_request(priv_dev, priv_ep);
2456 } else {
2457 if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
2458 cdns3_start_all_request(priv_dev, priv_ep);
2459 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002460
2461 return 0;
2462}
2463
2464static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2465 gfp_t gfp_flags)
2466{
2467 struct usb_request *zlp_request;
2468 struct cdns3_endpoint *priv_ep;
2469 struct cdns3_device *priv_dev;
2470 unsigned long flags;
2471 int ret;
2472
2473 if (!request || !ep)
2474 return -EINVAL;
2475
2476 priv_ep = ep_to_cdns3_ep(ep);
2477 priv_dev = priv_ep->cdns3_dev;
2478
2479 spin_lock_irqsave(&priv_dev->lock, flags);
2480
2481 ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
2482
2483 if (ret == 0 && request->zero && request->length &&
2484 (request->length % ep->maxpacket == 0)) {
2485 struct cdns3_request *priv_req;
2486
2487 zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
2488 zlp_request->buf = priv_dev->zlp_buf;
2489 zlp_request->length = 0;
2490
2491 priv_req = to_cdns3_request(zlp_request);
2492 priv_req->flags |= REQUEST_ZLP;
2493
2494 dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
2495 priv_ep->name);
2496 ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
2497 }
2498
2499 spin_unlock_irqrestore(&priv_dev->lock, flags);
2500 return ret;
2501}
2502
2503/**
2504 * cdns3_gadget_ep_dequeue Remove request from transfer queue
2505 * @ep: endpoint object associated with request
2506 * @request: request object
2507 *
2508 * Returns 0 on success, error code elsewhere
2509 */
2510int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
2511 struct usb_request *request)
2512{
2513 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2514 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2515 struct usb_request *req, *req_temp;
2516 struct cdns3_request *priv_req;
2517 struct cdns3_trb *link_trb;
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002518 u8 req_on_hw_ring = 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002519 unsigned long flags;
2520 int ret = 0;
2521
2522 if (!ep || !request || !ep->desc)
2523 return -EINVAL;
2524
2525 spin_lock_irqsave(&priv_dev->lock, flags);
2526
2527 priv_req = to_cdns3_request(request);
2528
2529 trace_cdns3_ep_dequeue(priv_req);
2530
2531 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2532
2533 list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
2534 list) {
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002535 if (request == req) {
2536 req_on_hw_ring = 1;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002537 goto found;
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002538 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002539 }
2540
2541 list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
2542 list) {
2543 if (request == req)
2544 goto found;
2545 }
2546
2547 goto not_found;
2548
2549found:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002550 link_trb = priv_req->trb;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002551
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002552 /* Update ring only if removed request is on pending_req_list list */
Peter Chen95cd7dc2020-04-30 15:07:13 +08002553 if (req_on_hw_ring && link_trb) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002554 link_trb->buffer = TRB_BUFFER(priv_ep->trb_pool_dma +
Peter Chen8a7c47f2020-02-19 22:14:54 +08002555 ((priv_req->end_trb + 1) * TRB_SIZE));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002556 link_trb->control = (link_trb->control & TRB_CYCLE) |
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002557 TRB_TYPE(TRB_LINK) | TRB_CHAIN;
2558
2559 if (priv_ep->wa1_trb == priv_req->trb)
2560 cdns3_wa1_restore_cycle_bit(priv_ep);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002561 }
2562
Pawel Laszczakf616c3b2019-10-13 10:20:20 +01002563 cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
2564
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002565not_found:
2566 spin_unlock_irqrestore(&priv_dev->lock, flags);
2567 return ret;
2568}
2569
2570/**
2571 * __cdns3_gadget_ep_set_halt Sets stall on selected endpoint
2572 * Should be called after acquiring spin_lock and selecting ep
Lee Jones4a35aa62020-07-02 15:46:12 +01002573 * @priv_ep: endpoint object to set stall on.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002574 */
2575void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
2576{
2577 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2578
2579 trace_cdns3_halt(priv_ep, 1, 0);
2580
2581 if (!(priv_ep->flags & EP_STALLED)) {
2582 u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
2583
2584 if (!(ep_sts_reg & EP_STS_DBUSY))
2585 cdns3_ep_stall_flush(priv_ep);
2586 else
2587 priv_ep->flags |= EP_STALL_PENDING;
2588 }
2589}
2590
2591/**
2592 * __cdns3_gadget_ep_clear_halt Clears stall on selected endpoint
2593 * Should be called after acquiring spin_lock and selecting ep
Lee Jones4a35aa62020-07-02 15:46:12 +01002594 * @priv_ep: endpoint object to clear stall on
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002595 */
2596int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
2597{
2598 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2599 struct usb_request *request;
Peter Chen4bf2dd62020-02-19 22:14:55 +08002600 struct cdns3_request *priv_req;
2601 struct cdns3_trb *trb = NULL;
Colin Ian King04db1d22019-09-02 15:50:35 +01002602 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002603 int val;
2604
2605 trace_cdns3_halt(priv_ep, 0, 0);
2606
Peter Chen4bf2dd62020-02-19 22:14:55 +08002607 request = cdns3_next_request(&priv_ep->pending_req_list);
2608 if (request) {
2609 priv_req = to_cdns3_request(request);
2610 trb = priv_req->trb;
2611 if (trb)
2612 trb->control = trb->control ^ TRB_CYCLE;
2613 }
2614
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002615 writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2616
2617 /* wait for EPRST cleared */
Colin Ian King04db1d22019-09-02 15:50:35 +01002618 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2619 !(val & EP_CMD_EPRST), 1, 100);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002620 if (ret)
2621 return -EINVAL;
2622
2623 priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
2624
Peter Chen4bf2dd62020-02-19 22:14:55 +08002625 if (request) {
2626 if (trb)
2627 trb->control = trb->control ^ TRB_CYCLE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002628 cdns3_rearm_transfer(priv_ep, 1);
Peter Chen4bf2dd62020-02-19 22:14:55 +08002629 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002630
2631 cdns3_start_all_request(priv_dev, priv_ep);
2632 return ret;
2633}
2634
2635/**
2636 * cdns3_gadget_ep_set_halt Sets/clears stall on selected endpoint
2637 * @ep: endpoint object to set/clear stall on
2638 * @value: 1 for set stall, 0 for clear stall
2639 *
2640 * Returns 0 on success, error code elsewhere
2641 */
2642int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2643{
2644 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2645 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2646 unsigned long flags;
2647 int ret = 0;
2648
2649 if (!(priv_ep->flags & EP_ENABLED))
2650 return -EPERM;
2651
2652 spin_lock_irqsave(&priv_dev->lock, flags);
2653
2654 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2655
2656 if (!value) {
2657 priv_ep->flags &= ~EP_WEDGE;
2658 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
2659 } else {
2660 __cdns3_gadget_ep_set_halt(priv_ep);
2661 }
2662
2663 spin_unlock_irqrestore(&priv_dev->lock, flags);
2664
2665 return ret;
2666}
2667
2668extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
2669
2670static const struct usb_ep_ops cdns3_gadget_ep_ops = {
2671 .enable = cdns3_gadget_ep_enable,
2672 .disable = cdns3_gadget_ep_disable,
2673 .alloc_request = cdns3_gadget_ep_alloc_request,
2674 .free_request = cdns3_gadget_ep_free_request,
2675 .queue = cdns3_gadget_ep_queue,
2676 .dequeue = cdns3_gadget_ep_dequeue,
2677 .set_halt = cdns3_gadget_ep_set_halt,
2678 .set_wedge = cdns3_gadget_ep_set_wedge,
2679};
2680
2681/**
2682 * cdns3_gadget_get_frame Returns number of actual ITP frame
2683 * @gadget: gadget object
2684 *
2685 * Returns number of actual ITP frame
2686 */
2687static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
2688{
2689 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2690
2691 return readl(&priv_dev->regs->usb_itpn);
2692}
2693
2694int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
2695{
2696 enum usb_device_speed speed;
2697
2698 speed = cdns3_get_speed(priv_dev);
2699
2700 if (speed >= USB_SPEED_SUPER)
2701 return 0;
2702
2703 /* Start driving resume signaling to indicate remote wakeup. */
2704 writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
2705
2706 return 0;
2707}
2708
2709static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
2710{
2711 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2712 unsigned long flags;
2713 int ret = 0;
2714
2715 spin_lock_irqsave(&priv_dev->lock, flags);
2716 ret = __cdns3_gadget_wakeup(priv_dev);
2717 spin_unlock_irqrestore(&priv_dev->lock, flags);
2718 return ret;
2719}
2720
2721static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
2722 int is_selfpowered)
2723{
2724 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2725 unsigned long flags;
2726
2727 spin_lock_irqsave(&priv_dev->lock, flags);
2728 priv_dev->is_selfpowered = !!is_selfpowered;
2729 spin_unlock_irqrestore(&priv_dev->lock, flags);
2730 return 0;
2731}
2732
2733static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
2734{
2735 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2736
Peter Chen0eeda052020-09-01 10:33:50 +08002737 if (is_on) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002738 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
Peter Chen0eeda052020-09-01 10:33:50 +08002739 } else {
2740 writel(~0, &priv_dev->regs->ep_ists);
2741 writel(~0, &priv_dev->regs->usb_ists);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002742 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
Peter Chen0eeda052020-09-01 10:33:50 +08002743 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002744
2745 return 0;
2746}
2747
2748static void cdns3_gadget_config(struct cdns3_device *priv_dev)
2749{
2750 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2751 u32 reg;
2752
2753 cdns3_ep0_config(priv_dev);
2754
2755 /* enable interrupts for endpoint 0 (in and out) */
2756 writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, &regs->ep_ien);
2757
2758 /*
2759 * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
2760 * revision of controller.
2761 */
2762 if (priv_dev->dev_ver == DEV_VER_TI_V1) {
2763 reg = readl(&regs->dbg_link1);
2764
2765 reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
2766 reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
2767 DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
2768 writel(reg, &regs->dbg_link1);
2769 }
2770
2771 /*
2772 * By default some platforms has set protected access to memory.
2773 * This cause problem with cache, so driver restore non-secure
2774 * access to memory.
2775 */
2776 reg = readl(&regs->dma_axi_ctrl);
2777 reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
2778 DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
2779 writel(reg, &regs->dma_axi_ctrl);
2780
2781 /* enable generic interrupt*/
2782 writel(USB_IEN_INIT, &regs->usb_ien);
2783 writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, &regs->usb_conf);
Peter Chenb5148d92020-09-01 10:33:49 +08002784 /* keep Fast Access bit */
2785 writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002786
2787 cdns3_configure_dmult(priv_dev, NULL);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002788}
2789
2790/**
2791 * cdns3_gadget_udc_start Gadget start
2792 * @gadget: gadget object
2793 * @driver: driver which operates on this gadget
2794 *
2795 * Returns 0 on success, error code elsewhere
2796 */
2797static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
2798 struct usb_gadget_driver *driver)
2799{
2800 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2801 unsigned long flags;
Roger Quadros94e259f2019-10-30 14:16:07 +02002802 enum usb_device_speed max_speed = driver->max_speed;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002803
2804 spin_lock_irqsave(&priv_dev->lock, flags);
2805 priv_dev->gadget_driver = driver;
Roger Quadros94e259f2019-10-30 14:16:07 +02002806
2807 /* limit speed if necessary */
2808 max_speed = min(driver->max_speed, gadget->max_speed);
2809
2810 switch (max_speed) {
2811 case USB_SPEED_FULL:
2812 writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
2813 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2814 break;
2815 case USB_SPEED_HIGH:
2816 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2817 break;
2818 case USB_SPEED_SUPER:
2819 break;
2820 default:
2821 dev_err(priv_dev->dev,
2822 "invalid maximum_speed parameter %d\n",
2823 max_speed);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05002824 fallthrough;
Roger Quadros94e259f2019-10-30 14:16:07 +02002825 case USB_SPEED_UNKNOWN:
2826 /* default to superspeed */
2827 max_speed = USB_SPEED_SUPER;
2828 break;
2829 }
2830
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002831 cdns3_gadget_config(priv_dev);
2832 spin_unlock_irqrestore(&priv_dev->lock, flags);
2833 return 0;
2834}
2835
2836/**
2837 * cdns3_gadget_udc_stop Stops gadget
2838 * @gadget: gadget object
2839 *
2840 * Returns 0
2841 */
2842static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
2843{
2844 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2845 struct cdns3_endpoint *priv_ep;
2846 u32 bEndpointAddress;
2847 struct usb_ep *ep;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002848 int val;
2849
2850 priv_dev->gadget_driver = NULL;
2851
2852 priv_dev->onchip_used_size = 0;
2853 priv_dev->out_mem_is_allocated = 0;
2854 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2855
2856 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2857 priv_ep = ep_to_cdns3_ep(ep);
2858 bEndpointAddress = priv_ep->num | priv_ep->dir;
2859 cdns3_select_ep(priv_dev, bEndpointAddress);
2860 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2861 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2862 !(val & EP_CMD_EPRST), 1, 100);
Sanket Parmarf5c8d292019-10-29 12:24:41 +00002863
2864 priv_ep->flags &= ~EP_CLAIMED;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002865 }
2866
2867 /* disable interrupt for device */
2868 writel(0, &priv_dev->regs->usb_ien);
Peter Chenb5148d92020-09-01 10:33:49 +08002869 writel(0, &priv_dev->regs->usb_pwr);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002870 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2871
Xu Wang8e1a2002019-12-20 07:19:38 +00002872 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002873}
2874
2875static const struct usb_gadget_ops cdns3_gadget_ops = {
2876 .get_frame = cdns3_gadget_get_frame,
2877 .wakeup = cdns3_gadget_wakeup,
2878 .set_selfpowered = cdns3_gadget_set_selfpowered,
2879 .pullup = cdns3_gadget_pullup,
2880 .udc_start = cdns3_gadget_udc_start,
2881 .udc_stop = cdns3_gadget_udc_stop,
2882 .match_ep = cdns3_gadget_match_ep,
2883};
2884
2885static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
2886{
2887 int i;
2888
2889 /* ep0 OUT point to ep0 IN. */
2890 priv_dev->eps[16] = NULL;
2891
2892 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
2893 if (priv_dev->eps[i]) {
2894 cdns3_free_trb_pool(priv_dev->eps[i]);
2895 devm_kfree(priv_dev->dev, priv_dev->eps[i]);
2896 }
2897}
2898
2899/**
2900 * cdns3_init_eps Initializes software endpoints of gadget
Lee Jones4a35aa62020-07-02 15:46:12 +01002901 * @priv_dev: extended gadget object
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002902 *
2903 * Returns 0 on success, error code elsewhere
2904 */
2905static int cdns3_init_eps(struct cdns3_device *priv_dev)
2906{
2907 u32 ep_enabled_reg, iso_ep_reg;
2908 struct cdns3_endpoint *priv_ep;
2909 int ep_dir, ep_number;
2910 u32 ep_mask;
2911 int ret = 0;
2912 int i;
2913
2914 /* Read it from USB_CAP3 to USB_CAP5 */
2915 ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
2916 iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
2917
2918 dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
2919
2920 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
2921 ep_dir = i >> 4; /* i div 16 */
2922 ep_number = i & 0xF; /* i % 16 */
2923 ep_mask = BIT(i);
2924
2925 if (!(ep_enabled_reg & ep_mask))
2926 continue;
2927
2928 if (ep_dir && !ep_number) {
2929 priv_dev->eps[i] = priv_dev->eps[0];
2930 continue;
2931 }
2932
2933 priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
2934 GFP_KERNEL);
Colin Ian King4d2233e2019-09-02 19:43:34 +01002935 if (!priv_ep)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002936 goto err;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002937
2938 /* set parent of endpoint object */
2939 priv_ep->cdns3_dev = priv_dev;
2940 priv_dev->eps[i] = priv_ep;
2941 priv_ep->num = ep_number;
2942 priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
2943
2944 if (!ep_number) {
2945 ret = cdns3_init_ep0(priv_dev, priv_ep);
2946 if (ret) {
2947 dev_err(priv_dev->dev, "Failed to init ep0\n");
2948 goto err;
2949 }
2950 } else {
2951 snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
2952 ep_number, !!ep_dir ? "in" : "out");
2953 priv_ep->endpoint.name = priv_ep->name;
2954
2955 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
2956 CDNS3_EP_MAX_PACKET_LIMIT);
2957 priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
2958 priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
2959 if (ep_dir)
2960 priv_ep->endpoint.caps.dir_in = 1;
2961 else
2962 priv_ep->endpoint.caps.dir_out = 1;
2963
2964 if (iso_ep_reg & ep_mask)
2965 priv_ep->endpoint.caps.type_iso = 1;
2966
2967 priv_ep->endpoint.caps.type_bulk = 1;
2968 priv_ep->endpoint.caps.type_int = 1;
2969
2970 list_add_tail(&priv_ep->endpoint.ep_list,
2971 &priv_dev->gadget.ep_list);
2972 }
2973
2974 priv_ep->flags = 0;
2975
Peter Cheneed6ed62020-03-31 16:10:05 +08002976 dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002977 priv_ep->name,
2978 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
2979 priv_ep->endpoint.caps.type_iso ? "ISO" : "");
2980
2981 INIT_LIST_HEAD(&priv_ep->pending_req_list);
2982 INIT_LIST_HEAD(&priv_ep->deferred_req_list);
Pawel Laszczak6bbf87a2019-08-26 12:19:31 +01002983 INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002984 }
2985
2986 return 0;
2987err:
2988 cdns3_free_all_eps(priv_dev);
2989 return -ENOMEM;
2990}
2991
2992void cdns3_gadget_exit(struct cdns3 *cdns)
2993{
2994 struct cdns3_device *priv_dev;
2995
2996 priv_dev = cdns->gadget_dev;
2997
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01002998
2999 pm_runtime_mark_last_busy(cdns->dev);
3000 pm_runtime_put_autosuspend(cdns->dev);
3001
3002 usb_del_gadget_udc(&priv_dev->gadget);
Peter Chen98df91f2020-09-01 10:35:49 +08003003 devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003004
3005 cdns3_free_all_eps(priv_dev);
3006
3007 while (!list_empty(&priv_dev->aligned_buf_list)) {
3008 struct cdns3_aligned_buf *buf;
3009
3010 buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
3011 dma_free_coherent(priv_dev->sysdev, buf->size,
3012 buf->buf,
3013 buf->dma);
3014
3015 list_del(&buf->list);
3016 kfree(buf);
3017 }
3018
3019 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3020 priv_dev->setup_dma);
3021
3022 kfree(priv_dev->zlp_buf);
3023 kfree(priv_dev);
3024 cdns->gadget_dev = NULL;
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +02003025 cdns3_drd_gadget_off(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003026}
3027
3028static int cdns3_gadget_start(struct cdns3 *cdns)
3029{
3030 struct cdns3_device *priv_dev;
3031 u32 max_speed;
3032 int ret;
3033
3034 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
3035 if (!priv_dev)
3036 return -ENOMEM;
3037
3038 cdns->gadget_dev = priv_dev;
3039 priv_dev->sysdev = cdns->dev;
3040 priv_dev->dev = cdns->dev;
3041 priv_dev->regs = cdns->dev_regs;
3042
3043 device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
3044 &priv_dev->onchip_buffers);
3045
3046 if (priv_dev->onchip_buffers <= 0) {
3047 u32 reg = readl(&priv_dev->regs->usb_cap2);
3048
3049 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
3050 }
3051
3052 if (!priv_dev->onchip_buffers)
3053 priv_dev->onchip_buffers = 256;
3054
3055 max_speed = usb_get_maximum_speed(cdns->dev);
3056
3057 /* Check the maximum_speed parameter */
3058 switch (max_speed) {
3059 case USB_SPEED_FULL:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003060 case USB_SPEED_HIGH:
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003061 case USB_SPEED_SUPER:
3062 break;
3063 default:
3064 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
3065 max_speed);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05003066 fallthrough;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003067 case USB_SPEED_UNKNOWN:
3068 /* default to superspeed */
3069 max_speed = USB_SPEED_SUPER;
3070 break;
3071 }
3072
3073 /* fill gadget fields */
3074 priv_dev->gadget.max_speed = max_speed;
3075 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3076 priv_dev->gadget.ops = &cdns3_gadget_ops;
3077 priv_dev->gadget.name = "usb-ss-gadget";
3078 priv_dev->gadget.sg_supported = 1;
3079 priv_dev->gadget.quirk_avoids_skb_reserve = 1;
Peter Chen77f30ff2020-05-10 13:30:42 +08003080 priv_dev->gadget.irq = cdns->dev_irq;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003081
3082 spin_lock_init(&priv_dev->lock);
3083 INIT_WORK(&priv_dev->pending_status_wq,
3084 cdns3_pending_setup_status_handler);
3085
3086 INIT_WORK(&priv_dev->aligned_buf_wq,
3087 cdns3_free_aligned_request_buf);
3088
3089 /* initialize endpoint container */
3090 INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
3091 INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
3092
3093 ret = cdns3_init_eps(priv_dev);
3094 if (ret) {
3095 dev_err(priv_dev->dev, "Failed to create endpoints\n");
3096 goto err1;
3097 }
3098
3099 /* allocate memory for setup packet buffer */
3100 priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
3101 &priv_dev->setup_dma, GFP_DMA);
3102 if (!priv_dev->setup_buf) {
3103 ret = -ENOMEM;
3104 goto err2;
3105 }
3106
3107 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
3108
3109 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
3110 readl(&priv_dev->regs->usb_cap6));
3111 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
3112 readl(&priv_dev->regs->usb_cap1));
Colin Ian King5d041112019-09-03 13:07:10 +01003113 dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003114 readl(&priv_dev->regs->usb_cap2));
3115
3116 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
3117
3118 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
3119 if (!priv_dev->zlp_buf) {
3120 ret = -ENOMEM;
3121 goto err3;
3122 }
3123
3124 /* add USB gadget device */
3125 ret = usb_add_gadget_udc(priv_dev->dev, &priv_dev->gadget);
3126 if (ret < 0) {
3127 dev_err(priv_dev->dev,
3128 "Failed to register USB device controller\n");
3129 goto err4;
3130 }
3131
3132 return 0;
3133err4:
3134 kfree(priv_dev->zlp_buf);
3135err3:
3136 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3137 priv_dev->setup_dma);
3138err2:
3139 cdns3_free_all_eps(priv_dev);
3140err1:
3141 cdns->gadget_dev = NULL;
3142 return ret;
3143}
3144
3145static int __cdns3_gadget_init(struct cdns3 *cdns)
3146{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003147 int ret = 0;
3148
Pawel Laszczakeb21a742019-10-07 13:03:23 +01003149 /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
3150 ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
3151 if (ret) {
3152 dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
3153 return ret;
3154 }
3155
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +02003156 cdns3_drd_gadget_on(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003157 pm_runtime_get_sync(cdns->dev);
3158
3159 ret = cdns3_gadget_start(cdns);
3160 if (ret)
3161 return ret;
3162
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003163 /*
3164 * Because interrupt line can be shared with other components in
3165 * driver it can't use IRQF_ONESHOT flag here.
3166 */
3167 ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
3168 cdns3_device_irq_handler,
3169 cdns3_device_thread_irq_handler,
Peter Chenaf58e1f2019-12-27 17:10:04 +08003170 IRQF_SHARED, dev_name(cdns->dev),
3171 cdns->gadget_dev);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003172
3173 if (ret)
3174 goto err0;
3175
3176 return 0;
3177err0:
3178 cdns3_gadget_exit(cdns);
3179 return ret;
3180}
3181
3182static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup)
3183{
3184 struct cdns3_device *priv_dev = cdns->gadget_dev;
3185
3186 cdns3_disconnect_gadget(priv_dev);
3187
3188 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3189 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
3190 cdns3_hw_reset_eps_config(priv_dev);
3191
3192 /* disable interrupt for device */
3193 writel(0, &priv_dev->regs->usb_ien);
3194
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003195 return 0;
3196}
3197
3198static int cdns3_gadget_resume(struct cdns3 *cdns, bool hibernated)
3199{
3200 struct cdns3_device *priv_dev = cdns->gadget_dev;
3201
3202 if (!priv_dev->gadget_driver)
3203 return 0;
3204
3205 cdns3_gadget_config(priv_dev);
3206
3207 return 0;
3208}
3209
3210/**
3211 * cdns3_gadget_init - initialize device structure
3212 *
Lee Jones4a35aa62020-07-02 15:46:12 +01003213 * @cdns: cdns3 instance
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01003214 *
3215 * This function initializes the gadget.
3216 */
3217int cdns3_gadget_init(struct cdns3 *cdns)
3218{
3219 struct cdns3_role_driver *rdrv;
3220
3221 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
3222 if (!rdrv)
3223 return -ENOMEM;
3224
3225 rdrv->start = __cdns3_gadget_init;
3226 rdrv->stop = cdns3_gadget_exit;
3227 rdrv->suspend = cdns3_gadget_suspend;
3228 rdrv->resume = cdns3_gadget_resume;
3229 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
3230 rdrv->name = "gadget";
3231 cdns->roles[USB_ROLE_DEVICE] = rdrv;
3232
3233 return 0;
3234}