• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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  *
31  * 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  *
57  */
58 
59 #include <linux/dma-mapping.h>
60 #include <linux/usb/gadget.h>
61 #include <linux/module.h>
62 #include <linux/dmapool.h>
63 #include <linux/iopoll.h>
64 
65 #include "core.h"
66 #include "gadget-export.h"
67 #include "cdns3-gadget.h"
68 #include "cdns3-trace.h"
69 #include "drd.h"
70 
71 static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
72 				   struct usb_request *request,
73 				   gfp_t gfp_flags);
74 
75 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
76 				 struct usb_request *request);
77 
78 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
79 					struct usb_request *request);
80 
81 /**
82  * cdns3_clear_register_bit - clear bit in given register.
83  * @ptr: address of device controller register to be read and changed
84  * @mask: bits requested to clar
85  */
cdns3_clear_register_bit(void __iomem * ptr,u32 mask)86 static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask)
87 {
88 	mask = readl(ptr) & ~mask;
89 	writel(mask, ptr);
90 }
91 
92 /**
93  * cdns3_set_register_bit - set bit in given register.
94  * @ptr: address of device controller register to be read and changed
95  * @mask: bits requested to set
96  */
cdns3_set_register_bit(void __iomem * ptr,u32 mask)97 void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
98 {
99 	mask = readl(ptr) | mask;
100 	writel(mask, ptr);
101 }
102 
103 /**
104  * cdns3_ep_addr_to_index - Macro converts endpoint address to
105  * index of endpoint object in cdns3_device.eps[] container
106  * @ep_addr: endpoint address for which endpoint object is required
107  *
108  */
cdns3_ep_addr_to_index(u8 ep_addr)109 u8 cdns3_ep_addr_to_index(u8 ep_addr)
110 {
111 	return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
112 }
113 
cdns3_get_dma_pos(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)114 static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
115 			     struct cdns3_endpoint *priv_ep)
116 {
117 	int dma_index;
118 
119 	dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
120 
121 	return dma_index / TRB_SIZE;
122 }
123 
124 /**
125  * cdns3_next_request - returns next request from list
126  * @list: list containing requests
127  *
128  * Returns request or NULL if no requests in list
129  */
cdns3_next_request(struct list_head * list)130 struct usb_request *cdns3_next_request(struct list_head *list)
131 {
132 	return list_first_entry_or_null(list, struct usb_request, list);
133 }
134 
135 /**
136  * cdns3_next_align_buf - returns next buffer from list
137  * @list: list containing buffers
138  *
139  * Returns buffer or NULL if no buffers in list
140  */
cdns3_next_align_buf(struct list_head * list)141 static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
142 {
143 	return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
144 }
145 
146 /**
147  * cdns3_next_priv_request - returns next request from list
148  * @list: list containing requests
149  *
150  * Returns request or NULL if no requests in list
151  */
cdns3_next_priv_request(struct list_head * list)152 static struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
153 {
154 	return list_first_entry_or_null(list, struct cdns3_request, list);
155 }
156 
157 /**
158  * cdns3_select_ep - selects endpoint
159  * @priv_dev:  extended gadget object
160  * @ep: endpoint address
161  */
cdns3_select_ep(struct cdns3_device * priv_dev,u32 ep)162 void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
163 {
164 	if (priv_dev->selected_ep == ep)
165 		return;
166 
167 	priv_dev->selected_ep = ep;
168 	writel(ep, &priv_dev->regs->ep_sel);
169 }
170 
171 /**
172  * cdns3_get_tdl - gets current tdl for selected endpoint.
173  * @priv_dev:  extended gadget object
174  *
175  * Before calling this function the appropriate endpoint must
176  * be selected by means of cdns3_select_ep function.
177  */
cdns3_get_tdl(struct cdns3_device * priv_dev)178 static int cdns3_get_tdl(struct cdns3_device *priv_dev)
179 {
180 	if (priv_dev->dev_ver < DEV_VER_V3)
181 		return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
182 	else
183 		return readl(&priv_dev->regs->ep_tdl);
184 }
185 
cdns3_trb_virt_to_dma(struct cdns3_endpoint * priv_ep,struct cdns3_trb * trb)186 dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
187 				 struct cdns3_trb *trb)
188 {
189 	u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
190 
191 	return priv_ep->trb_pool_dma + offset;
192 }
193 
cdns3_free_trb_pool(struct cdns3_endpoint * priv_ep)194 static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
195 {
196 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
197 
198 	if (priv_ep->trb_pool) {
199 		dma_pool_free(priv_dev->eps_dma_pool,
200 			      priv_ep->trb_pool, priv_ep->trb_pool_dma);
201 		priv_ep->trb_pool = NULL;
202 	}
203 }
204 
205 /**
206  * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint
207  * @priv_ep:  endpoint object
208  *
209  * Function will return 0 on success or -ENOMEM on allocation error
210  */
cdns3_allocate_trb_pool(struct cdns3_endpoint * priv_ep)211 int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
212 {
213 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
214 	int ring_size = TRB_RING_SIZE;
215 	int num_trbs = ring_size / TRB_SIZE;
216 	struct cdns3_trb *link_trb;
217 
218 	if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size)
219 		cdns3_free_trb_pool(priv_ep);
220 
221 	if (!priv_ep->trb_pool) {
222 		priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
223 						   GFP_ATOMIC,
224 						   &priv_ep->trb_pool_dma);
225 
226 		if (!priv_ep->trb_pool)
227 			return -ENOMEM;
228 
229 		priv_ep->alloc_ring_size = ring_size;
230 	}
231 
232 	memset(priv_ep->trb_pool, 0, ring_size);
233 
234 	priv_ep->num_trbs = num_trbs;
235 
236 	if (!priv_ep->num)
237 		return 0;
238 
239 	/* Initialize the last TRB as Link TRB */
240 	link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
241 
242 	if (priv_ep->use_streams) {
243 		/*
244 		 * For stream capable endpoints driver use single correct TRB.
245 		 * The last trb has zeroed cycle bit
246 		 */
247 		link_trb->control = 0;
248 	} else {
249 		link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma));
250 		link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE);
251 	}
252 	return 0;
253 }
254 
255 /**
256  * cdns3_ep_stall_flush - Stalls and flushes selected endpoint
257  * @priv_ep: endpoint object
258  *
259  * Endpoint must be selected before call to this function
260  */
cdns3_ep_stall_flush(struct cdns3_endpoint * priv_ep)261 static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
262 {
263 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
264 	int val;
265 
266 	trace_cdns3_halt(priv_ep, 1, 1);
267 
268 	writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
269 	       &priv_dev->regs->ep_cmd);
270 
271 	/* wait for DFLUSH cleared */
272 	readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
273 				  !(val & EP_CMD_DFLUSH), 1, 1000);
274 	priv_ep->flags |= EP_STALLED;
275 	priv_ep->flags &= ~EP_STALL_PENDING;
276 }
277 
278 /**
279  * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller.
280  * @priv_dev: extended gadget object
281  */
cdns3_hw_reset_eps_config(struct cdns3_device * priv_dev)282 void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
283 {
284 	int i;
285 
286 	writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
287 
288 	cdns3_allow_enable_l1(priv_dev, 0);
289 	priv_dev->hw_configured_flag = 0;
290 	priv_dev->onchip_used_size = 0;
291 	priv_dev->out_mem_is_allocated = 0;
292 	priv_dev->wait_for_setup = 0;
293 	priv_dev->using_streams = 0;
294 
295 	for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
296 		if (priv_dev->eps[i])
297 			priv_dev->eps[i]->flags &= ~EP_CONFIGURED;
298 }
299 
300 /**
301  * cdns3_ep_inc_trb - increment a trb index.
302  * @index: Pointer to the TRB index to increment.
303  * @cs: Cycle state
304  * @trb_in_seg: number of TRBs in segment
305  *
306  * The index should never point to the link TRB. After incrementing,
307  * if it is point to the link TRB, wrap around to the beginning and revert
308  * cycle state bit The
309  * link TRB is always at the last TRB entry.
310  */
cdns3_ep_inc_trb(int * index,u8 * cs,int trb_in_seg)311 static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
312 {
313 	(*index)++;
314 	if (*index == (trb_in_seg - 1)) {
315 		*index = 0;
316 		*cs ^=  1;
317 	}
318 }
319 
320 /**
321  * cdns3_ep_inc_enq - increment endpoint's enqueue pointer
322  * @priv_ep: The endpoint whose enqueue pointer we're incrementing
323  */
cdns3_ep_inc_enq(struct cdns3_endpoint * priv_ep)324 static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
325 {
326 	priv_ep->free_trbs--;
327 	cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
328 }
329 
330 /**
331  * cdns3_ep_inc_deq - increment endpoint's dequeue pointer
332  * @priv_ep: The endpoint whose dequeue pointer we're incrementing
333  */
cdns3_ep_inc_deq(struct cdns3_endpoint * priv_ep)334 static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
335 {
336 	priv_ep->free_trbs++;
337 	cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
338 }
339 
340 /**
341  * cdns3_allow_enable_l1 - enable/disable permits to transition to L1.
342  * @priv_dev: Extended gadget object
343  * @enable: Enable/disable permit to transition to L1.
344  *
345  * If bit USB_CONF_L1EN is set and device receive Extended Token packet,
346  * then controller answer with ACK handshake.
347  * If bit USB_CONF_L1DS is set and device receive Extended Token packet,
348  * then controller answer with NYET handshake.
349  */
cdns3_allow_enable_l1(struct cdns3_device * priv_dev,int enable)350 void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
351 {
352 	if (enable)
353 		writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
354 	else
355 		writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
356 }
357 
cdns3_get_speed(struct cdns3_device * priv_dev)358 enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
359 {
360 	u32 reg;
361 
362 	reg = readl(&priv_dev->regs->usb_sts);
363 
364 	if (DEV_SUPERSPEED(reg))
365 		return USB_SPEED_SUPER;
366 	else if (DEV_HIGHSPEED(reg))
367 		return USB_SPEED_HIGH;
368 	else if (DEV_FULLSPEED(reg))
369 		return USB_SPEED_FULL;
370 	else if (DEV_LOWSPEED(reg))
371 		return USB_SPEED_LOW;
372 	return USB_SPEED_UNKNOWN;
373 }
374 
375 /**
376  * cdns3_start_all_request - add to ring all request not started
377  * @priv_dev: Extended gadget object
378  * @priv_ep: The endpoint for whom request will be started.
379  *
380  * Returns return ENOMEM if transfer ring i not enough TRBs to start
381  *         all requests.
382  */
cdns3_start_all_request(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)383 static int cdns3_start_all_request(struct cdns3_device *priv_dev,
384 				   struct cdns3_endpoint *priv_ep)
385 {
386 	struct usb_request *request;
387 	int ret = 0;
388 	u8 pending_empty = list_empty(&priv_ep->pending_req_list);
389 
390 	/*
391 	 * If the last pending transfer is INTERNAL
392 	 * OR streams are enabled for this endpoint
393 	 * do NOT start new transfer till the last one is pending
394 	 */
395 	if (!pending_empty) {
396 		struct cdns3_request *priv_req;
397 
398 		request = cdns3_next_request(&priv_ep->pending_req_list);
399 		priv_req = to_cdns3_request(request);
400 		if ((priv_req->flags & REQUEST_INTERNAL) ||
401 		    (priv_ep->flags & EP_TDLCHK_EN) ||
402 			priv_ep->use_streams) {
403 			dev_dbg(priv_dev->dev, "Blocking external request\n");
404 			return ret;
405 		}
406 	}
407 
408 	while (!list_empty(&priv_ep->deferred_req_list)) {
409 		request = cdns3_next_request(&priv_ep->deferred_req_list);
410 
411 		if (!priv_ep->use_streams) {
412 			ret = cdns3_ep_run_transfer(priv_ep, request);
413 		} else {
414 			priv_ep->stream_sg_idx = 0;
415 			ret = cdns3_ep_run_stream_transfer(priv_ep, request);
416 		}
417 		if (ret)
418 			return ret;
419 
420 		list_move_tail(&request->list, &priv_ep->pending_req_list);
421 		if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN))
422 			break;
423 	}
424 
425 	priv_ep->flags &= ~EP_RING_FULL;
426 	return ret;
427 }
428 
429 /*
430  * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set
431  * driver try to detect whether endpoint need additional internal
432  * buffer for unblocking on-chip FIFO buffer. This flag will be cleared
433  * if before first DESCMISS interrupt the DMA will be armed.
434  */
435 #define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \
436 	if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
437 		priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
438 		(reg) |= EP_STS_EN_DESCMISEN; \
439 	} } while (0)
440 
__cdns3_descmiss_copy_data(struct usb_request * request,struct usb_request * descmiss_req)441 static void __cdns3_descmiss_copy_data(struct usb_request *request,
442 	struct usb_request *descmiss_req)
443 {
444 	int length = request->actual + descmiss_req->actual;
445 	struct scatterlist *s = request->sg;
446 
447 	if (!s) {
448 		if (length <= request->length) {
449 			memcpy(&((u8 *)request->buf)[request->actual],
450 			       descmiss_req->buf,
451 			       descmiss_req->actual);
452 			request->actual = length;
453 		} else {
454 			/* It should never occures */
455 			request->status = -ENOMEM;
456 		}
457 	} else {
458 		if (length <= sg_dma_len(s)) {
459 			void *p = phys_to_virt(sg_dma_address(s));
460 
461 			memcpy(&((u8 *)p)[request->actual],
462 				descmiss_req->buf,
463 				descmiss_req->actual);
464 			request->actual = length;
465 		} else {
466 			request->status = -ENOMEM;
467 		}
468 	}
469 }
470 
471 /**
472  * cdns3_wa2_descmiss_copy_data - copy data from internal requests to
473  * request queued by class driver.
474  * @priv_ep: extended endpoint object
475  * @request: request object
476  */
cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint * priv_ep,struct usb_request * request)477 static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
478 					 struct usb_request *request)
479 {
480 	struct usb_request *descmiss_req;
481 	struct cdns3_request *descmiss_priv_req;
482 
483 	while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
484 		int chunk_end;
485 
486 		descmiss_priv_req =
487 			cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
488 		descmiss_req = &descmiss_priv_req->request;
489 
490 		/* driver can't touch pending request */
491 		if (descmiss_priv_req->flags & REQUEST_PENDING)
492 			break;
493 
494 		chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
495 		request->status = descmiss_req->status;
496 		__cdns3_descmiss_copy_data(request, descmiss_req);
497 		list_del_init(&descmiss_priv_req->list);
498 		kfree(descmiss_req->buf);
499 		cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
500 		--priv_ep->wa2_counter;
501 
502 		if (!chunk_end)
503 			break;
504 	}
505 }
506 
cdns3_wa2_gadget_giveback(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep,struct cdns3_request * priv_req)507 static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
508 						     struct cdns3_endpoint *priv_ep,
509 						     struct cdns3_request *priv_req)
510 {
511 	if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
512 	    priv_req->flags & REQUEST_INTERNAL) {
513 		struct usb_request *req;
514 
515 		req = cdns3_next_request(&priv_ep->deferred_req_list);
516 
517 		priv_ep->descmis_req = NULL;
518 
519 		if (!req)
520 			return NULL;
521 
522 		/* unmap the gadget request before copying data */
523 		usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
524 						priv_ep->dir);
525 
526 		cdns3_wa2_descmiss_copy_data(priv_ep, req);
527 		if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
528 		    req->length != req->actual) {
529 			/* wait for next part of transfer */
530 			/* re-map the gadget request buffer*/
531 			usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
532 				usb_endpoint_dir_in(priv_ep->endpoint.desc));
533 			return NULL;
534 		}
535 
536 		if (req->status == -EINPROGRESS)
537 			req->status = 0;
538 
539 		list_del_init(&req->list);
540 		cdns3_start_all_request(priv_dev, priv_ep);
541 		return req;
542 	}
543 
544 	return &priv_req->request;
545 }
546 
cdns3_wa2_gadget_ep_queue(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep,struct cdns3_request * priv_req)547 static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
548 				     struct cdns3_endpoint *priv_ep,
549 				     struct cdns3_request *priv_req)
550 {
551 	int deferred = 0;
552 
553 	/*
554 	 * If transfer was queued before DESCMISS appear than we
555 	 * can disable handling of DESCMISS interrupt. Driver assumes that it
556 	 * can disable special treatment for this endpoint.
557 	 */
558 	if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
559 		u32 reg;
560 
561 		cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
562 		priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
563 		reg = readl(&priv_dev->regs->ep_sts_en);
564 		reg &= ~EP_STS_EN_DESCMISEN;
565 		trace_cdns3_wa2(priv_ep, "workaround disabled\n");
566 		writel(reg, &priv_dev->regs->ep_sts_en);
567 	}
568 
569 	if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
570 		u8 pending_empty = list_empty(&priv_ep->pending_req_list);
571 		u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
572 
573 		/*
574 		 *  DESCMISS transfer has been finished, so data will be
575 		 *  directly copied from internal allocated usb_request
576 		 *  objects.
577 		 */
578 		if (pending_empty && !descmiss_empty &&
579 		    !(priv_req->flags & REQUEST_INTERNAL)) {
580 			cdns3_wa2_descmiss_copy_data(priv_ep,
581 						     &priv_req->request);
582 
583 			trace_cdns3_wa2(priv_ep, "get internal stored data");
584 
585 			list_add_tail(&priv_req->request.list,
586 				      &priv_ep->pending_req_list);
587 			cdns3_gadget_giveback(priv_ep, priv_req,
588 					      priv_req->request.status);
589 
590 			/*
591 			 * Intentionally driver returns positive value as
592 			 * correct value. It informs that transfer has
593 			 * been finished.
594 			 */
595 			return EINPROGRESS;
596 		}
597 
598 		/*
599 		 * Driver will wait for completion DESCMISS transfer,
600 		 * before starts new, not DESCMISS transfer.
601 		 */
602 		if (!pending_empty && !descmiss_empty) {
603 			trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
604 			deferred = 1;
605 		}
606 
607 		if (priv_req->flags & REQUEST_INTERNAL)
608 			list_add_tail(&priv_req->list,
609 				      &priv_ep->wa2_descmiss_req_list);
610 	}
611 
612 	return deferred;
613 }
614 
cdns3_wa2_remove_old_request(struct cdns3_endpoint * priv_ep)615 static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
616 {
617 	struct cdns3_request *priv_req;
618 
619 	while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
620 		u8 chain;
621 
622 		priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
623 		chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
624 
625 		trace_cdns3_wa2(priv_ep, "removes eldest request");
626 
627 		kfree(priv_req->request.buf);
628 		list_del_init(&priv_req->list);
629 		cdns3_gadget_ep_free_request(&priv_ep->endpoint,
630 					     &priv_req->request);
631 		--priv_ep->wa2_counter;
632 
633 		if (!chain)
634 			break;
635 	}
636 }
637 
638 /**
639  * cdns3_wa2_descmissing_packet - handles descriptor missing event.
640  * @priv_ep: extended gadget object
641  *
642  * This function is used only for WA2. For more information see Work around 2
643  * description.
644  */
cdns3_wa2_descmissing_packet(struct cdns3_endpoint * priv_ep)645 static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
646 {
647 	struct cdns3_request *priv_req;
648 	struct usb_request *request;
649 	u8 pending_empty = list_empty(&priv_ep->pending_req_list);
650 
651 	/* check for pending transfer */
652 	if (!pending_empty) {
653 		trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
654 		return;
655 	}
656 
657 	if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
658 		priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
659 		priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
660 	}
661 
662 	trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
663 
664 	if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
665 		trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
666 		cdns3_wa2_remove_old_request(priv_ep);
667 	}
668 
669 	request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
670 						GFP_ATOMIC);
671 	if (!request)
672 		goto err;
673 
674 	priv_req = to_cdns3_request(request);
675 	priv_req->flags |= REQUEST_INTERNAL;
676 
677 	/* if this field is still assigned it indicate that transfer related
678 	 * with this request has not been finished yet. Driver in this
679 	 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
680 	 * flag to previous one. It will indicate that current request is
681 	 * part of the previous one.
682 	 */
683 	if (priv_ep->descmis_req)
684 		priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
685 
686 	priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
687 					GFP_ATOMIC);
688 	priv_ep->wa2_counter++;
689 
690 	if (!priv_req->request.buf) {
691 		cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
692 		goto err;
693 	}
694 
695 	priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
696 	priv_ep->descmis_req = priv_req;
697 
698 	__cdns3_gadget_ep_queue(&priv_ep->endpoint,
699 				&priv_ep->descmis_req->request,
700 				GFP_ATOMIC);
701 
702 	return;
703 
704 err:
705 	dev_err(priv_ep->cdns3_dev->dev,
706 		"Failed: No sufficient memory for DESCMIS\n");
707 }
708 
cdns3_wa2_reset_tdl(struct cdns3_device * priv_dev)709 static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
710 {
711 	u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
712 
713 	if (tdl) {
714 		u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
715 
716 		writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
717 		       &priv_dev->regs->ep_cmd);
718 	}
719 }
720 
cdns3_wa2_check_outq_status(struct cdns3_device * priv_dev)721 static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
722 {
723 	u32 ep_sts_reg;
724 
725 	/* select EP0-out */
726 	cdns3_select_ep(priv_dev, 0);
727 
728 	ep_sts_reg = readl(&priv_dev->regs->ep_sts);
729 
730 	if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
731 		u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
732 		struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
733 
734 		if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
735 		    outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
736 			u8 pending_empty = list_empty(&outq_ep->pending_req_list);
737 
738 			if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
739 			    (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
740 			    !pending_empty) {
741 			} else {
742 				u32 ep_sts_en_reg;
743 				u32 ep_cmd_reg;
744 
745 				cdns3_select_ep(priv_dev, outq_ep->num |
746 						outq_ep->dir);
747 				ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
748 				ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
749 
750 				outq_ep->flags |= EP_TDLCHK_EN;
751 				cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
752 						       EP_CFG_TDL_CHK);
753 
754 				cdns3_wa2_enable_detection(priv_dev, outq_ep,
755 							   ep_sts_en_reg);
756 				writel(ep_sts_en_reg,
757 				       &priv_dev->regs->ep_sts_en);
758 				/* reset tdl value to zero */
759 				cdns3_wa2_reset_tdl(priv_dev);
760 				/*
761 				 * Memory barrier - Reset tdl before ringing the
762 				 * doorbell.
763 				 */
764 				wmb();
765 				if (EP_CMD_DRDY & ep_cmd_reg) {
766 					trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
767 
768 				} else {
769 					trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
770 					/*
771 					 * ring doorbell to generate DESCMIS irq
772 					 */
773 					writel(EP_CMD_DRDY,
774 					       &priv_dev->regs->ep_cmd);
775 				}
776 			}
777 		}
778 	}
779 }
780 
781 /**
782  * cdns3_gadget_giveback - call struct usb_request's ->complete callback
783  * @priv_ep: The endpoint to whom the request belongs to
784  * @priv_req: The request we're giving back
785  * @status: completion code for the request
786  *
787  * Must be called with controller's lock held and interrupts disabled. This
788  * function will unmap @req and call its ->complete() callback to notify upper
789  * layers that it has completed.
790  */
cdns3_gadget_giveback(struct cdns3_endpoint * priv_ep,struct cdns3_request * priv_req,int status)791 void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
792 			   struct cdns3_request *priv_req,
793 			   int status)
794 {
795 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
796 	struct usb_request *request = &priv_req->request;
797 
798 	list_del_init(&request->list);
799 
800 	if (request->status == -EINPROGRESS)
801 		request->status = status;
802 
803 	usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
804 					priv_ep->dir);
805 
806 	if ((priv_req->flags & REQUEST_UNALIGNED) &&
807 	    priv_ep->dir == USB_DIR_OUT && !request->status) {
808 		/* Make DMA buffer CPU accessible */
809 		dma_sync_single_for_cpu(priv_dev->sysdev,
810 			priv_req->aligned_buf->dma,
811 			priv_req->aligned_buf->size,
812 			priv_req->aligned_buf->dir);
813 		memcpy(request->buf, priv_req->aligned_buf->buf,
814 		       request->length);
815 	}
816 
817 	priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
818 	/* All TRBs have finished, clear the counter */
819 	priv_req->finished_trb = 0;
820 	trace_cdns3_gadget_giveback(priv_req);
821 
822 	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 
829 	/*
830 	 * zlp request is appended by driver, needn't call usb_gadget_giveback_request() to notify
831 	 * gadget composite driver.
832 	 */
833 	if (request->complete && request->buf != priv_dev->zlp_buf) {
834 		spin_unlock(&priv_dev->lock);
835 		usb_gadget_giveback_request(&priv_ep->endpoint,
836 					    request);
837 		spin_lock(&priv_dev->lock);
838 	}
839 
840 	if (request->buf == priv_dev->zlp_buf)
841 		cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
842 }
843 
cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint * priv_ep)844 static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
845 {
846 	/* Work around for stale data address in TRB*/
847 	if (priv_ep->wa1_set) {
848 		trace_cdns3_wa1(priv_ep, "restore cycle bit");
849 
850 		priv_ep->wa1_set = 0;
851 		priv_ep->wa1_trb_index = 0xFFFF;
852 		if (priv_ep->wa1_cycle_bit) {
853 			priv_ep->wa1_trb->control =
854 				priv_ep->wa1_trb->control | cpu_to_le32(0x1);
855 		} else {
856 			priv_ep->wa1_trb->control =
857 				priv_ep->wa1_trb->control & cpu_to_le32(~0x1);
858 		}
859 	}
860 }
861 
cdns3_free_aligned_request_buf(struct work_struct * work)862 static void cdns3_free_aligned_request_buf(struct work_struct *work)
863 {
864 	struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
865 					aligned_buf_wq);
866 	struct cdns3_aligned_buf *buf, *tmp;
867 	unsigned long flags;
868 
869 	spin_lock_irqsave(&priv_dev->lock, flags);
870 
871 	list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
872 		if (!buf->in_use) {
873 			list_del(&buf->list);
874 
875 			/*
876 			 * Re-enable interrupts to free DMA capable memory.
877 			 * Driver can't free this memory with disabled
878 			 * interrupts.
879 			 */
880 			spin_unlock_irqrestore(&priv_dev->lock, flags);
881 			dma_free_noncoherent(priv_dev->sysdev, buf->size,
882 					  buf->buf, buf->dma, buf->dir);
883 			kfree(buf);
884 			spin_lock_irqsave(&priv_dev->lock, flags);
885 		}
886 	}
887 
888 	spin_unlock_irqrestore(&priv_dev->lock, flags);
889 }
890 
cdns3_prepare_aligned_request_buf(struct cdns3_request * priv_req)891 static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
892 {
893 	struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
894 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
895 	struct cdns3_aligned_buf *buf;
896 
897 	/* check if buffer is aligned to 8. */
898 	if (!((uintptr_t)priv_req->request.buf & 0x7))
899 		return 0;
900 
901 	buf = priv_req->aligned_buf;
902 
903 	if (!buf || priv_req->request.length > buf->size) {
904 		buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
905 		if (!buf)
906 			return -ENOMEM;
907 
908 		buf->size = priv_req->request.length;
909 		buf->dir = usb_endpoint_dir_in(priv_ep->endpoint.desc) ?
910 			DMA_TO_DEVICE : DMA_FROM_DEVICE;
911 
912 		buf->buf = dma_alloc_noncoherent(priv_dev->sysdev,
913 					      buf->size,
914 					      &buf->dma,
915 					      buf->dir,
916 					      GFP_ATOMIC);
917 		if (!buf->buf) {
918 			kfree(buf);
919 			return -ENOMEM;
920 		}
921 
922 		if (priv_req->aligned_buf) {
923 			trace_cdns3_free_aligned_request(priv_req);
924 			priv_req->aligned_buf->in_use = 0;
925 			queue_work(system_freezable_wq,
926 				   &priv_dev->aligned_buf_wq);
927 		}
928 
929 		buf->in_use = 1;
930 		priv_req->aligned_buf = buf;
931 
932 		list_add_tail(&buf->list,
933 			      &priv_dev->aligned_buf_list);
934 	}
935 
936 	if (priv_ep->dir == USB_DIR_IN) {
937 		/* Make DMA buffer CPU accessible */
938 		dma_sync_single_for_cpu(priv_dev->sysdev,
939 			buf->dma, buf->size, buf->dir);
940 		memcpy(buf->buf, priv_req->request.buf,
941 		       priv_req->request.length);
942 	}
943 
944 	/* Transfer DMA buffer ownership back to device */
945 	dma_sync_single_for_device(priv_dev->sysdev,
946 			buf->dma, buf->size, buf->dir);
947 
948 	priv_req->flags |= REQUEST_UNALIGNED;
949 	trace_cdns3_prepare_aligned_request(priv_req);
950 
951 	return 0;
952 }
953 
cdns3_wa1_update_guard(struct cdns3_endpoint * priv_ep,struct cdns3_trb * trb)954 static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
955 				  struct cdns3_trb *trb)
956 {
957 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
958 
959 	if (!priv_ep->wa1_set) {
960 		u32 doorbell;
961 
962 		doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
963 
964 		if (doorbell) {
965 			priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
966 			priv_ep->wa1_set = 1;
967 			priv_ep->wa1_trb = trb;
968 			priv_ep->wa1_trb_index = priv_ep->enqueue;
969 			trace_cdns3_wa1(priv_ep, "set guard");
970 			return 0;
971 		}
972 	}
973 	return 1;
974 }
975 
cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)976 static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
977 					     struct cdns3_endpoint *priv_ep)
978 {
979 	int dma_index;
980 	u32 doorbell;
981 
982 	doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
983 	dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
984 
985 	if (!doorbell || dma_index != priv_ep->wa1_trb_index)
986 		cdns3_wa1_restore_cycle_bit(priv_ep);
987 }
988 
cdns3_ep_run_stream_transfer(struct cdns3_endpoint * priv_ep,struct usb_request * request)989 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
990 					struct usb_request *request)
991 {
992 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
993 	struct cdns3_request *priv_req;
994 	struct cdns3_trb *trb;
995 	dma_addr_t trb_dma;
996 	int address;
997 	u32 control;
998 	u32 length;
999 	u32 tdl;
1000 	unsigned int sg_idx = priv_ep->stream_sg_idx;
1001 
1002 	priv_req = to_cdns3_request(request);
1003 	address = priv_ep->endpoint.desc->bEndpointAddress;
1004 
1005 	priv_ep->flags |= EP_PENDING_REQUEST;
1006 
1007 	/* must allocate buffer aligned to 8 */
1008 	if (priv_req->flags & REQUEST_UNALIGNED)
1009 		trb_dma = priv_req->aligned_buf->dma;
1010 	else
1011 		trb_dma = request->dma;
1012 
1013 	/*  For stream capable endpoints driver use only single TD. */
1014 	trb = priv_ep->trb_pool + priv_ep->enqueue;
1015 	priv_req->start_trb = priv_ep->enqueue;
1016 	priv_req->end_trb = priv_req->start_trb;
1017 	priv_req->trb = trb;
1018 
1019 	cdns3_select_ep(priv_ep->cdns3_dev, address);
1020 
1021 	control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
1022 		  TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
1023 
1024 	if (!request->num_sgs) {
1025 		trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
1026 		length = request->length;
1027 	} else {
1028 		trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address));
1029 		length = request->sg[sg_idx].length;
1030 	}
1031 
1032 	tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
1033 
1034 	trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length));
1035 
1036 	/*
1037 	 * For DEV_VER_V2 controller version we have enabled
1038 	 * USB_CONF2_EN_TDL_TRB in DMULT configuration.
1039 	 * This enables TDL calculation based on TRB, hence setting TDL in TRB.
1040 	 */
1041 	if (priv_dev->dev_ver >= DEV_VER_V2) {
1042 		if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1043 			trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl));
1044 	}
1045 	priv_req->flags |= REQUEST_PENDING;
1046 
1047 	trb->control = cpu_to_le32(control);
1048 
1049 	trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1050 
1051 	/*
1052 	 * Memory barrier - Cycle Bit must be set before trb->length  and
1053 	 * trb->buffer fields.
1054 	 */
1055 	wmb();
1056 
1057 	/* always first element */
1058 	writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
1059 	       &priv_dev->regs->ep_traddr);
1060 
1061 	if (!(priv_ep->flags & EP_STALLED)) {
1062 		trace_cdns3_ring(priv_ep);
1063 		/*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1064 		writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1065 
1066 		priv_ep->prime_flag = false;
1067 
1068 		/*
1069 		 * Controller version DEV_VER_V2 tdl calculation
1070 		 * is based on TRB
1071 		 */
1072 
1073 		if (priv_dev->dev_ver < DEV_VER_V2)
1074 			writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1075 			       &priv_dev->regs->ep_cmd);
1076 		else if (priv_dev->dev_ver > DEV_VER_V2)
1077 			writel(tdl, &priv_dev->regs->ep_tdl);
1078 
1079 		priv_ep->last_stream_id = priv_req->request.stream_id;
1080 		writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1081 		writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
1082 		       EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
1083 
1084 		trace_cdns3_doorbell_epx(priv_ep->name,
1085 					 readl(&priv_dev->regs->ep_traddr));
1086 	}
1087 
1088 	/* WORKAROUND for transition to L0 */
1089 	__cdns3_gadget_wakeup(priv_dev);
1090 
1091 	return 0;
1092 }
1093 
cdns3_rearm_drdy_if_needed(struct cdns3_endpoint * priv_ep)1094 static void cdns3_rearm_drdy_if_needed(struct cdns3_endpoint *priv_ep)
1095 {
1096 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1097 
1098 	if (priv_dev->dev_ver < DEV_VER_V3)
1099 		return;
1100 
1101 	if (readl(&priv_dev->regs->ep_sts) & EP_STS_TRBERR) {
1102 		writel(EP_STS_TRBERR, &priv_dev->regs->ep_sts);
1103 		writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1104 	}
1105 }
1106 
1107 /**
1108  * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
1109  * @priv_ep: endpoint object
1110  * @request: request object
1111  *
1112  * Returns zero on success or negative value on failure
1113  */
cdns3_ep_run_transfer(struct cdns3_endpoint * priv_ep,struct usb_request * request)1114 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
1115 				 struct usb_request *request)
1116 {
1117 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1118 	struct cdns3_request *priv_req;
1119 	struct cdns3_trb *trb;
1120 	struct cdns3_trb *link_trb = NULL;
1121 	dma_addr_t trb_dma;
1122 	u32 togle_pcs = 1;
1123 	int sg_iter = 0;
1124 	int num_trb_req;
1125 	int trb_burst;
1126 	int num_trb;
1127 	int address;
1128 	u32 control;
1129 	int pcs;
1130 	u16 total_tdl = 0;
1131 	struct scatterlist *s = NULL;
1132 	bool sg_supported = !!(request->num_mapped_sgs);
1133 
1134 	num_trb_req = sg_supported ? request->num_mapped_sgs : 1;
1135 
1136 	/* ISO transfer require each SOF have a TD, each TD include some TRBs */
1137 	if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1138 		num_trb = priv_ep->interval * num_trb_req;
1139 	else
1140 		num_trb = num_trb_req;
1141 
1142 	priv_req = to_cdns3_request(request);
1143 	address = priv_ep->endpoint.desc->bEndpointAddress;
1144 
1145 	priv_ep->flags |= EP_PENDING_REQUEST;
1146 
1147 	/* must allocate buffer aligned to 8 */
1148 	if (priv_req->flags & REQUEST_UNALIGNED)
1149 		trb_dma = priv_req->aligned_buf->dma;
1150 	else
1151 		trb_dma = request->dma;
1152 
1153 	trb = priv_ep->trb_pool + priv_ep->enqueue;
1154 	priv_req->start_trb = priv_ep->enqueue;
1155 	priv_req->trb = trb;
1156 
1157 	cdns3_select_ep(priv_ep->cdns3_dev, address);
1158 
1159 	/* prepare ring */
1160 	if ((priv_ep->enqueue + num_trb)  >= (priv_ep->num_trbs - 1)) {
1161 		int doorbell, dma_index;
1162 		u32 ch_bit = 0;
1163 
1164 		doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1165 		dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1166 
1167 		/* Driver can't update LINK TRB if it is current processed. */
1168 		if (doorbell && dma_index == priv_ep->num_trbs - 1) {
1169 			priv_ep->flags |= EP_DEFERRED_DRDY;
1170 			return -ENOBUFS;
1171 		}
1172 
1173 		/*updating C bt in  Link TRB before starting DMA*/
1174 		link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
1175 		/*
1176 		 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
1177 		 * that DMA stuck at the LINK TRB.
1178 		 * On the other hand, removing TRB_CHAIN for longer TRs for
1179 		 * epXout cause that DMA stuck after handling LINK TRB.
1180 		 * To eliminate this strange behavioral driver set TRB_CHAIN
1181 		 * bit only for TR size > 2.
1182 		 */
1183 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
1184 		    TRBS_PER_SEGMENT > 2)
1185 			ch_bit = TRB_CHAIN;
1186 
1187 		link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) |
1188 				    TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit);
1189 
1190 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
1191 			/*
1192 			 * ISO require LINK TRB must be first one of TD.
1193 			 * Fill LINK TRBs for left trb space to simply software process logic.
1194 			 */
1195 			while (priv_ep->enqueue) {
1196 				*trb = *link_trb;
1197 				trace_cdns3_prepare_trb(priv_ep, trb);
1198 
1199 				cdns3_ep_inc_enq(priv_ep);
1200 				trb = priv_ep->trb_pool + priv_ep->enqueue;
1201 				priv_req->trb = trb;
1202 			}
1203 		}
1204 	}
1205 
1206 	if (num_trb > priv_ep->free_trbs) {
1207 		priv_ep->flags |= EP_RING_FULL;
1208 		return -ENOBUFS;
1209 	}
1210 
1211 	if (priv_dev->dev_ver <= DEV_VER_V2)
1212 		togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
1213 
1214 	/* set incorrect Cycle Bit for first trb*/
1215 	control = priv_ep->pcs ? 0 : TRB_CYCLE;
1216 	trb->length = 0;
1217 	if (priv_dev->dev_ver >= DEV_VER_V2) {
1218 		u16 td_size;
1219 
1220 		td_size = DIV_ROUND_UP(request->length,
1221 				       priv_ep->endpoint.maxpacket);
1222 		if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1223 			trb->length = cpu_to_le32(TRB_TDL_SS_SIZE(td_size));
1224 		else
1225 			control |= TRB_TDL_HS_SIZE(td_size);
1226 	}
1227 
1228 	do {
1229 		u32 length;
1230 
1231 		if (!(sg_iter % num_trb_req) && sg_supported)
1232 			s = request->sg;
1233 
1234 		/* fill TRB */
1235 		control |= TRB_TYPE(TRB_NORMAL);
1236 		if (sg_supported) {
1237 			trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s)));
1238 			length = sg_dma_len(s);
1239 		} else {
1240 			trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
1241 			length = request->length;
1242 		}
1243 
1244 		if (priv_ep->flags & EP_TDLCHK_EN)
1245 			total_tdl += DIV_ROUND_UP(length,
1246 					       priv_ep->endpoint.maxpacket);
1247 
1248 		trb_burst = priv_ep->trb_burst_size;
1249 
1250 		/*
1251 		 * Supposed DMA cross 4k bounder problem should be fixed at DEV_VER_V2, but still
1252 		 * met problem when do ISO transfer if sg enabled.
1253 		 *
1254 		 * Data pattern likes below when sg enabled, package size is 1k and mult is 2
1255 		 *       [UVC Header(8B) ] [data(3k - 8)] ...
1256 		 *
1257 		 * The received data at offset 0xd000 will get 0xc000 data, len 0x70. Error happen
1258 		 * as below pattern:
1259 		 *	0xd000: wrong
1260 		 *	0xe000: wrong
1261 		 *	0xf000: correct
1262 		 *	0x10000: wrong
1263 		 *	0x11000: wrong
1264 		 *	0x12000: correct
1265 		 *	...
1266 		 *
1267 		 * But it is still unclear about why error have not happen below 0xd000, it should
1268 		 * cross 4k bounder. But anyway, the below code can fix this problem.
1269 		 *
1270 		 * To avoid DMA cross 4k bounder at ISO transfer, reduce burst len according to 16.
1271 		 */
1272 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_dev->dev_ver <= DEV_VER_V2)
1273 			if (ALIGN_DOWN(trb->buffer, SZ_4K) !=
1274 			    ALIGN_DOWN(trb->buffer + length, SZ_4K))
1275 				trb_burst = 16;
1276 
1277 		trb->length |= cpu_to_le32(TRB_BURST_LEN(trb_burst) |
1278 					TRB_LEN(length));
1279 		pcs = priv_ep->pcs ? TRB_CYCLE : 0;
1280 
1281 		/*
1282 		 * first trb should be prepared as last to avoid processing
1283 		 *  transfer to early
1284 		 */
1285 		if (sg_iter != 0)
1286 			control |= pcs;
1287 
1288 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC  && !priv_ep->dir) {
1289 			control |= TRB_IOC | TRB_ISP;
1290 		} else {
1291 			/* for last element in TD or in SG list */
1292 			if (sg_iter == (num_trb - 1) && sg_iter != 0)
1293 				control |= pcs | TRB_IOC | TRB_ISP;
1294 		}
1295 
1296 		if (sg_iter)
1297 			trb->control = cpu_to_le32(control);
1298 		else
1299 			priv_req->trb->control = cpu_to_le32(control);
1300 
1301 		if (sg_supported) {
1302 			trb->control |= cpu_to_le32(TRB_ISP);
1303 			/* Don't set chain bit for last TRB */
1304 			if ((sg_iter % num_trb_req) < num_trb_req - 1)
1305 				trb->control |= cpu_to_le32(TRB_CHAIN);
1306 
1307 			s = sg_next(s);
1308 		}
1309 
1310 		control = 0;
1311 		++sg_iter;
1312 		priv_req->end_trb = priv_ep->enqueue;
1313 		cdns3_ep_inc_enq(priv_ep);
1314 		trb = priv_ep->trb_pool + priv_ep->enqueue;
1315 		trb->length = 0;
1316 	} while (sg_iter < num_trb);
1317 
1318 	trb = priv_req->trb;
1319 
1320 	priv_req->flags |= REQUEST_PENDING;
1321 	priv_req->num_of_trb = num_trb;
1322 
1323 	if (sg_iter == 1)
1324 		trb->control |= cpu_to_le32(TRB_IOC | TRB_ISP);
1325 
1326 	if (priv_dev->dev_ver < DEV_VER_V2 &&
1327 	    (priv_ep->flags & EP_TDLCHK_EN)) {
1328 		u16 tdl = total_tdl;
1329 		u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
1330 
1331 		if (tdl > EP_CMD_TDL_MAX) {
1332 			tdl = EP_CMD_TDL_MAX;
1333 			priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
1334 		}
1335 
1336 		if (old_tdl < tdl) {
1337 			tdl -= old_tdl;
1338 			writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1339 			       &priv_dev->regs->ep_cmd);
1340 		}
1341 	}
1342 
1343 	/*
1344 	 * Memory barrier - cycle bit must be set before other filds in trb.
1345 	 */
1346 	wmb();
1347 
1348 	/* give the TD to the consumer*/
1349 	if (togle_pcs)
1350 		trb->control = trb->control ^ cpu_to_le32(1);
1351 
1352 	if (priv_dev->dev_ver <= DEV_VER_V2)
1353 		cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
1354 
1355 	if (num_trb > 1) {
1356 		int i = 0;
1357 
1358 		while (i < num_trb) {
1359 			trace_cdns3_prepare_trb(priv_ep, trb + i);
1360 			if (trb + i == link_trb) {
1361 				trb = priv_ep->trb_pool;
1362 				num_trb = num_trb - i;
1363 				i = 0;
1364 			} else {
1365 				i++;
1366 			}
1367 		}
1368 	} else {
1369 		trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1370 	}
1371 
1372 	/*
1373 	 * Memory barrier - Cycle Bit must be set before trb->length  and
1374 	 * trb->buffer fields.
1375 	 */
1376 	wmb();
1377 
1378 	/*
1379 	 * For DMULT mode we can set address to transfer ring only once after
1380 	 * enabling endpoint.
1381 	 */
1382 	if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
1383 		/*
1384 		 * Until SW is not ready to handle the OUT transfer the ISO OUT
1385 		 * Endpoint should be disabled (EP_CFG.ENABLE = 0).
1386 		 * EP_CFG_ENABLE must be set before updating ep_traddr.
1387 		 */
1388 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC  && !priv_ep->dir &&
1389 		    !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
1390 			priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
1391 			cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
1392 					       EP_CFG_ENABLE);
1393 		}
1394 
1395 		writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
1396 					priv_req->start_trb * TRB_SIZE),
1397 					&priv_dev->regs->ep_traddr);
1398 
1399 		priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
1400 	}
1401 
1402 	if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
1403 		trace_cdns3_ring(priv_ep);
1404 		/*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1405 		writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1406 		writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1407 		cdns3_rearm_drdy_if_needed(priv_ep);
1408 		trace_cdns3_doorbell_epx(priv_ep->name,
1409 					 readl(&priv_dev->regs->ep_traddr));
1410 	}
1411 
1412 	/* WORKAROUND for transition to L0 */
1413 	__cdns3_gadget_wakeup(priv_dev);
1414 
1415 	return 0;
1416 }
1417 
cdns3_set_hw_configuration(struct cdns3_device * priv_dev)1418 void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1419 {
1420 	struct cdns3_endpoint *priv_ep;
1421 	struct usb_ep *ep;
1422 
1423 	if (priv_dev->hw_configured_flag)
1424 		return;
1425 
1426 	writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
1427 
1428 	cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1429 			       USB_CONF_U1EN | USB_CONF_U2EN);
1430 
1431 	priv_dev->hw_configured_flag = 1;
1432 
1433 	list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1434 		if (ep->enabled) {
1435 			priv_ep = ep_to_cdns3_ep(ep);
1436 			cdns3_start_all_request(priv_dev, priv_ep);
1437 		}
1438 	}
1439 
1440 	cdns3_allow_enable_l1(priv_dev, 1);
1441 }
1442 
1443 /**
1444  * cdns3_trb_handled - check whether trb has been handled by DMA
1445  *
1446  * @priv_ep: extended endpoint object.
1447  * @priv_req: request object for checking
1448  *
1449  * Endpoint must be selected before invoking this function.
1450  *
1451  * Returns false if request has not been handled by DMA, else returns true.
1452  *
1453  * SR - start ring
1454  * ER -  end ring
1455  * DQ = priv_ep->dequeue - dequeue position
1456  * EQ = priv_ep->enqueue -  enqueue position
1457  * ST = priv_req->start_trb - index of first TRB in transfer ring
1458  * ET = priv_req->end_trb - index of last TRB in transfer ring
1459  * CI = current_index - index of processed TRB by DMA.
1460  *
1461  * As first step, we check if the TRB between the ST and ET.
1462  * Then, we check if cycle bit for index priv_ep->dequeue
1463  * is correct.
1464  *
1465  * some rules:
1466  * 1. priv_ep->dequeue never equals to current_index.
1467  * 2  priv_ep->enqueue never exceed priv_ep->dequeue
1468  * 3. exception: priv_ep->enqueue == priv_ep->dequeue
1469  *    and priv_ep->free_trbs is zero.
1470  *    This case indicate that TR is full.
1471  *
1472  * At below two cases, the request have been handled.
1473  * Case 1 - priv_ep->dequeue < current_index
1474  *      SR ... EQ ... DQ ... CI ... ER
1475  *      SR ... DQ ... CI ... EQ ... ER
1476  *
1477  * Case 2 - priv_ep->dequeue > current_index
1478  * This situation takes place when CI go through the LINK TRB at the end of
1479  * transfer ring.
1480  *      SR ... CI ... EQ ... DQ ... ER
1481  */
cdns3_trb_handled(struct cdns3_endpoint * priv_ep,struct cdns3_request * priv_req)1482 static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep,
1483 				  struct cdns3_request *priv_req)
1484 {
1485 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1486 	struct cdns3_trb *trb;
1487 	int current_index = 0;
1488 	int handled = 0;
1489 	int doorbell;
1490 
1491 	current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1492 	doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1493 
1494 	/* current trb doesn't belong to this request */
1495 	if (priv_req->start_trb < priv_req->end_trb) {
1496 		if (priv_ep->dequeue > priv_req->end_trb)
1497 			goto finish;
1498 
1499 		if (priv_ep->dequeue < priv_req->start_trb)
1500 			goto finish;
1501 	}
1502 
1503 	if ((priv_req->start_trb > priv_req->end_trb) &&
1504 		(priv_ep->dequeue > priv_req->end_trb) &&
1505 		(priv_ep->dequeue < priv_req->start_trb))
1506 		goto finish;
1507 
1508 	if ((priv_req->start_trb == priv_req->end_trb) &&
1509 		(priv_ep->dequeue != priv_req->end_trb))
1510 		goto finish;
1511 
1512 	trb = &priv_ep->trb_pool[priv_ep->dequeue];
1513 
1514 	if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs)
1515 		goto finish;
1516 
1517 	if (doorbell == 1 && current_index == priv_ep->dequeue)
1518 		goto finish;
1519 
1520 	/* The corner case for TRBS_PER_SEGMENT equal 2). */
1521 	if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1522 		handled = 1;
1523 		goto finish;
1524 	}
1525 
1526 	if (priv_ep->enqueue == priv_ep->dequeue &&
1527 	    priv_ep->free_trbs == 0) {
1528 		handled = 1;
1529 	} else if (priv_ep->dequeue < current_index) {
1530 		if ((current_index == (priv_ep->num_trbs - 1)) &&
1531 		    !priv_ep->dequeue)
1532 			goto finish;
1533 
1534 		handled = 1;
1535 	} else if (priv_ep->dequeue  > current_index) {
1536 			handled = 1;
1537 	}
1538 
1539 finish:
1540 	trace_cdns3_request_handled(priv_req, current_index, handled);
1541 
1542 	return handled;
1543 }
1544 
cdns3_transfer_completed(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)1545 static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1546 				     struct cdns3_endpoint *priv_ep)
1547 {
1548 	struct cdns3_request *priv_req;
1549 	struct usb_request *request;
1550 	struct cdns3_trb *trb;
1551 	bool request_handled = false;
1552 	bool transfer_end = false;
1553 
1554 	while (!list_empty(&priv_ep->pending_req_list)) {
1555 		request = cdns3_next_request(&priv_ep->pending_req_list);
1556 		priv_req = to_cdns3_request(request);
1557 
1558 		trb = priv_ep->trb_pool + priv_ep->dequeue;
1559 
1560 		/* The TRB was changed as link TRB, and the request was handled at ep_dequeue */
1561 		while (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) {
1562 
1563 			/* ISO ep_traddr may stop at LINK TRB */
1564 			if (priv_ep->dequeue == cdns3_get_dma_pos(priv_dev, priv_ep) &&
1565 			    priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1566 				break;
1567 
1568 			trace_cdns3_complete_trb(priv_ep, trb);
1569 			cdns3_ep_inc_deq(priv_ep);
1570 			trb = priv_ep->trb_pool + priv_ep->dequeue;
1571 		}
1572 
1573 		if (!request->stream_id) {
1574 			/* Re-select endpoint. It could be changed by other CPU
1575 			 * during handling usb_gadget_giveback_request.
1576 			 */
1577 			cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1578 
1579 			while (cdns3_trb_handled(priv_ep, priv_req)) {
1580 				priv_req->finished_trb++;
1581 				if (priv_req->finished_trb >= priv_req->num_of_trb)
1582 					request_handled = true;
1583 
1584 				trb = priv_ep->trb_pool + priv_ep->dequeue;
1585 				trace_cdns3_complete_trb(priv_ep, trb);
1586 
1587 				if (!transfer_end)
1588 					request->actual +=
1589 						TRB_LEN(le32_to_cpu(trb->length));
1590 
1591 				if (priv_req->num_of_trb > 1 &&
1592 					le32_to_cpu(trb->control) & TRB_SMM &&
1593 					le32_to_cpu(trb->control) & TRB_CHAIN)
1594 					transfer_end = true;
1595 
1596 				cdns3_ep_inc_deq(priv_ep);
1597 			}
1598 
1599 			if (request_handled) {
1600 				/* TRBs are duplicated by priv_ep->interval time for ISO IN */
1601 				if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_ep->dir)
1602 					request->actual /= priv_ep->interval;
1603 
1604 				cdns3_gadget_giveback(priv_ep, priv_req, 0);
1605 				request_handled = false;
1606 				transfer_end = false;
1607 			} else {
1608 				goto prepare_next_td;
1609 			}
1610 
1611 			if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1612 			    TRBS_PER_SEGMENT == 2)
1613 				break;
1614 		} else {
1615 			/* Re-select endpoint. It could be changed by other CPU
1616 			 * during handling usb_gadget_giveback_request.
1617 			 */
1618 			cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1619 
1620 			trb = priv_ep->trb_pool;
1621 			trace_cdns3_complete_trb(priv_ep, trb);
1622 
1623 			if (trb != priv_req->trb)
1624 				dev_warn(priv_dev->dev,
1625 					 "request_trb=0x%p, queue_trb=0x%p\n",
1626 					 priv_req->trb, trb);
1627 
1628 			request->actual += TRB_LEN(le32_to_cpu(trb->length));
1629 
1630 			if (!request->num_sgs ||
1631 			    (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
1632 				priv_ep->stream_sg_idx = 0;
1633 				cdns3_gadget_giveback(priv_ep, priv_req, 0);
1634 			} else {
1635 				priv_ep->stream_sg_idx++;
1636 				cdns3_ep_run_stream_transfer(priv_ep, request);
1637 			}
1638 			break;
1639 		}
1640 	}
1641 	priv_ep->flags &= ~EP_PENDING_REQUEST;
1642 
1643 prepare_next_td:
1644 	if (!(priv_ep->flags & EP_STALLED) &&
1645 	    !(priv_ep->flags & EP_STALL_PENDING))
1646 		cdns3_start_all_request(priv_dev, priv_ep);
1647 }
1648 
cdns3_rearm_transfer(struct cdns3_endpoint * priv_ep,u8 rearm)1649 void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1650 {
1651 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1652 
1653 	cdns3_wa1_restore_cycle_bit(priv_ep);
1654 
1655 	if (rearm) {
1656 		trace_cdns3_ring(priv_ep);
1657 
1658 		/* Cycle Bit must be updated before arming DMA. */
1659 		wmb();
1660 		writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1661 
1662 		__cdns3_gadget_wakeup(priv_dev);
1663 
1664 		trace_cdns3_doorbell_epx(priv_ep->name,
1665 					 readl(&priv_dev->regs->ep_traddr));
1666 	}
1667 }
1668 
cdns3_reprogram_tdl(struct cdns3_endpoint * priv_ep)1669 static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
1670 {
1671 	u16 tdl = priv_ep->pending_tdl;
1672 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1673 
1674 	if (tdl > EP_CMD_TDL_MAX) {
1675 		tdl = EP_CMD_TDL_MAX;
1676 		priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
1677 	} else {
1678 		priv_ep->pending_tdl = 0;
1679 	}
1680 
1681 	writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
1682 }
1683 
1684 /**
1685  * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
1686  * @priv_ep: endpoint object
1687  *
1688  * Returns 0
1689  */
cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint * priv_ep)1690 static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1691 {
1692 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1693 	u32 ep_sts_reg;
1694 	struct usb_request *deferred_request;
1695 	struct usb_request *pending_request;
1696 	u32 tdl = 0;
1697 
1698 	cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1699 
1700 	trace_cdns3_epx_irq(priv_dev, priv_ep);
1701 
1702 	ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1703 	writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1704 
1705 	if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
1706 		bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
1707 
1708 		tdl = cdns3_get_tdl(priv_dev);
1709 
1710 		/*
1711 		 * Continue the previous transfer:
1712 		 * There is some racing between ERDY and PRIME. The device send
1713 		 * ERDY and almost in the same time Host send PRIME. It cause
1714 		 * that host ignore the ERDY packet and driver has to send it
1715 		 * again.
1716 		 */
1717 		if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) ||
1718 		    EP_STS_HOSTPP(ep_sts_reg))) {
1719 			writel(EP_CMD_ERDY |
1720 			       EP_CMD_ERDY_SID(priv_ep->last_stream_id),
1721 			       &priv_dev->regs->ep_cmd);
1722 			ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
1723 		} else {
1724 			priv_ep->prime_flag = true;
1725 
1726 			pending_request = cdns3_next_request(&priv_ep->pending_req_list);
1727 			deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
1728 
1729 			if (deferred_request && !pending_request) {
1730 				cdns3_start_all_request(priv_dev, priv_ep);
1731 			}
1732 		}
1733 	}
1734 
1735 	if (ep_sts_reg & EP_STS_TRBERR) {
1736 		if (priv_ep->flags & EP_STALL_PENDING &&
1737 		    !(ep_sts_reg & EP_STS_DESCMIS &&
1738 		    priv_dev->dev_ver < DEV_VER_V2)) {
1739 			cdns3_ep_stall_flush(priv_ep);
1740 		}
1741 
1742 		/*
1743 		 * For isochronous transfer driver completes request on
1744 		 * IOC or on TRBERR. IOC appears only when device receive
1745 		 * OUT data packet. If host disable stream or lost some packet
1746 		 * then the only way to finish all queued transfer is to do it
1747 		 * on TRBERR event.
1748 		 */
1749 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1750 		    !priv_ep->wa1_set) {
1751 			if (!priv_ep->dir) {
1752 				u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1753 
1754 				ep_cfg &= ~EP_CFG_ENABLE;
1755 				writel(ep_cfg, &priv_dev->regs->ep_cfg);
1756 				priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1757 				priv_ep->flags |= EP_UPDATE_EP_TRBADDR;
1758 			}
1759 			cdns3_transfer_completed(priv_dev, priv_ep);
1760 		} else if (!(priv_ep->flags & EP_STALLED) &&
1761 			  !(priv_ep->flags & EP_STALL_PENDING)) {
1762 			if (priv_ep->flags & EP_DEFERRED_DRDY) {
1763 				priv_ep->flags &= ~EP_DEFERRED_DRDY;
1764 				cdns3_start_all_request(priv_dev, priv_ep);
1765 			} else {
1766 				cdns3_rearm_transfer(priv_ep,
1767 						     priv_ep->wa1_set);
1768 			}
1769 		}
1770 	}
1771 
1772 	if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
1773 	    (ep_sts_reg & EP_STS_IOT)) {
1774 		if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1775 			if (ep_sts_reg & EP_STS_ISP)
1776 				priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1777 			else
1778 				priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1779 		}
1780 
1781 		if (!priv_ep->use_streams) {
1782 			if ((ep_sts_reg & EP_STS_IOC) ||
1783 			    (ep_sts_reg & EP_STS_ISP)) {
1784 				cdns3_transfer_completed(priv_dev, priv_ep);
1785 			} else if ((priv_ep->flags & EP_TDLCHK_EN) &
1786 				   priv_ep->pending_tdl) {
1787 				/* handle IOT with pending tdl */
1788 				cdns3_reprogram_tdl(priv_ep);
1789 			}
1790 		} else if (priv_ep->dir == USB_DIR_OUT) {
1791 			priv_ep->ep_sts_pending |= ep_sts_reg;
1792 		} else if (ep_sts_reg & EP_STS_IOT) {
1793 			cdns3_transfer_completed(priv_dev, priv_ep);
1794 		}
1795 	}
1796 
1797 	/*
1798 	 * MD_EXIT interrupt sets when stream capable endpoint exits
1799 	 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
1800 	 */
1801 	if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
1802 	    (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
1803 		priv_ep->ep_sts_pending = 0;
1804 		cdns3_transfer_completed(priv_dev, priv_ep);
1805 	}
1806 
1807 	/*
1808 	 * WA2: this condition should only be meet when
1809 	 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
1810 	 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
1811 	 * In other cases this interrupt will be disabled.
1812 	 */
1813 	if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1814 	    !(priv_ep->flags & EP_STALLED))
1815 		cdns3_wa2_descmissing_packet(priv_ep);
1816 
1817 	return 0;
1818 }
1819 
cdns3_disconnect_gadget(struct cdns3_device * priv_dev)1820 static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1821 {
1822 	if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect)
1823 		priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
1824 }
1825 
1826 /**
1827  * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
1828  * @priv_dev: extended gadget object
1829  * @usb_ists: bitmap representation of device's reported interrupts
1830  * (usb_ists register value)
1831  */
cdns3_check_usb_interrupt_proceed(struct cdns3_device * priv_dev,u32 usb_ists)1832 static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1833 					      u32 usb_ists)
1834 __must_hold(&priv_dev->lock)
1835 {
1836 	int speed = 0;
1837 
1838 	trace_cdns3_usb_irq(priv_dev, usb_ists);
1839 	if (usb_ists & USB_ISTS_L1ENTI) {
1840 		/*
1841 		 * WORKAROUND: CDNS3 controller has issue with hardware resuming
1842 		 * from L1. To fix it, if any DMA transfer is pending driver
1843 		 * must starts driving resume signal immediately.
1844 		 */
1845 		if (readl(&priv_dev->regs->drbl))
1846 			__cdns3_gadget_wakeup(priv_dev);
1847 	}
1848 
1849 	/* Connection detected */
1850 	if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1851 		speed = cdns3_get_speed(priv_dev);
1852 		priv_dev->gadget.speed = speed;
1853 		usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1854 		cdns3_ep0_config(priv_dev);
1855 	}
1856 
1857 	/* Disconnection detected */
1858 	if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
1859 		spin_unlock(&priv_dev->lock);
1860 		cdns3_disconnect_gadget(priv_dev);
1861 		spin_lock(&priv_dev->lock);
1862 		priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1863 		usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1864 		cdns3_hw_reset_eps_config(priv_dev);
1865 	}
1866 
1867 	if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1868 		if (priv_dev->gadget_driver &&
1869 		    priv_dev->gadget_driver->suspend) {
1870 			spin_unlock(&priv_dev->lock);
1871 			priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1872 			spin_lock(&priv_dev->lock);
1873 		}
1874 	}
1875 
1876 	if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1877 		if (priv_dev->gadget_driver &&
1878 		    priv_dev->gadget_driver->resume) {
1879 			spin_unlock(&priv_dev->lock);
1880 			priv_dev->gadget_driver->resume(&priv_dev->gadget);
1881 			spin_lock(&priv_dev->lock);
1882 		}
1883 	}
1884 
1885 	/* reset*/
1886 	if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1887 		if (priv_dev->gadget_driver) {
1888 			spin_unlock(&priv_dev->lock);
1889 			usb_gadget_udc_reset(&priv_dev->gadget,
1890 					     priv_dev->gadget_driver);
1891 			spin_lock(&priv_dev->lock);
1892 
1893 			/*read again to check the actual speed*/
1894 			speed = cdns3_get_speed(priv_dev);
1895 			priv_dev->gadget.speed = speed;
1896 			cdns3_hw_reset_eps_config(priv_dev);
1897 			cdns3_ep0_config(priv_dev);
1898 		}
1899 	}
1900 }
1901 
1902 /**
1903  * cdns3_device_irq_handler - interrupt handler for device part of controller
1904  *
1905  * @irq: irq number for cdns3 core device
1906  * @data: structure of cdns3
1907  *
1908  * Returns IRQ_HANDLED or IRQ_NONE
1909  */
cdns3_device_irq_handler(int irq,void * data)1910 static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1911 {
1912 	struct cdns3_device *priv_dev = data;
1913 	struct cdns *cdns = dev_get_drvdata(priv_dev->dev);
1914 	irqreturn_t ret = IRQ_NONE;
1915 	u32 reg;
1916 
1917 	if (cdns->in_lpm)
1918 		return ret;
1919 
1920 	/* check USB device interrupt */
1921 	reg = readl(&priv_dev->regs->usb_ists);
1922 	if (reg) {
1923 		/* After masking interrupts the new interrupts won't be
1924 		 * reported in usb_ists/ep_ists. In order to not lose some
1925 		 * of them driver disables only detected interrupts.
1926 		 * They will be enabled ASAP after clearing source of
1927 		 * interrupt. This an unusual behavior only applies to
1928 		 * usb_ists register.
1929 		 */
1930 		reg = ~reg & readl(&priv_dev->regs->usb_ien);
1931 		/* mask deferred interrupt. */
1932 		writel(reg, &priv_dev->regs->usb_ien);
1933 		ret = IRQ_WAKE_THREAD;
1934 	}
1935 
1936 	/* check endpoint interrupt */
1937 	reg = readl(&priv_dev->regs->ep_ists);
1938 	if (reg) {
1939 		writel(0, &priv_dev->regs->ep_ien);
1940 		ret = IRQ_WAKE_THREAD;
1941 	}
1942 
1943 	return ret;
1944 }
1945 
1946 /**
1947  * cdns3_device_thread_irq_handler - interrupt handler for device part
1948  * of controller
1949  *
1950  * @irq: irq number for cdns3 core device
1951  * @data: structure of cdns3
1952  *
1953  * Returns IRQ_HANDLED or IRQ_NONE
1954  */
cdns3_device_thread_irq_handler(int irq,void * data)1955 static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1956 {
1957 	struct cdns3_device *priv_dev = data;
1958 	irqreturn_t ret = IRQ_NONE;
1959 	unsigned long flags;
1960 	unsigned int bit;
1961 	unsigned long reg;
1962 
1963 	spin_lock_irqsave(&priv_dev->lock, flags);
1964 
1965 	reg = readl(&priv_dev->regs->usb_ists);
1966 	if (reg) {
1967 		writel(reg, &priv_dev->regs->usb_ists);
1968 		writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1969 		cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1970 		ret = IRQ_HANDLED;
1971 	}
1972 
1973 	reg = readl(&priv_dev->regs->ep_ists);
1974 
1975 	/* handle default endpoint OUT */
1976 	if (reg & EP_ISTS_EP_OUT0) {
1977 		cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1978 		ret = IRQ_HANDLED;
1979 	}
1980 
1981 	/* handle default endpoint IN */
1982 	if (reg & EP_ISTS_EP_IN0) {
1983 		cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1984 		ret = IRQ_HANDLED;
1985 	}
1986 
1987 	/* check if interrupt from non default endpoint, if no exit */
1988 	reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1989 	if (!reg)
1990 		goto irqend;
1991 
1992 	for_each_set_bit(bit, &reg,
1993 			 sizeof(u32) * BITS_PER_BYTE) {
1994 		cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1995 		ret = IRQ_HANDLED;
1996 	}
1997 
1998 	if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
1999 		cdns3_wa2_check_outq_status(priv_dev);
2000 
2001 irqend:
2002 	writel(~0, &priv_dev->regs->ep_ien);
2003 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2004 
2005 	return ret;
2006 }
2007 
2008 /**
2009  * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
2010  *
2011  * The real reservation will occur during write to EP_CFG register,
2012  * this function is used to check if the 'size' reservation is allowed.
2013  *
2014  * @priv_dev: extended gadget object
2015  * @size: the size (KB) for EP would like to allocate
2016  * @is_in: endpoint direction
2017  *
2018  * Return 0 if the required size can met or negative value on failure
2019  */
cdns3_ep_onchip_buffer_reserve(struct cdns3_device * priv_dev,int size,int is_in)2020 static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
2021 					  int size, int is_in)
2022 {
2023 	int remained;
2024 
2025 	/* 2KB are reserved for EP0*/
2026 	remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
2027 
2028 	if (is_in) {
2029 		if (remained < size)
2030 			return -EPERM;
2031 
2032 		priv_dev->onchip_used_size += size;
2033 	} else {
2034 		int required;
2035 
2036 		/**
2037 		 *  ALL OUT EPs are shared the same chunk onchip memory, so
2038 		 * driver checks if it already has assigned enough buffers
2039 		 */
2040 		if (priv_dev->out_mem_is_allocated >= size)
2041 			return 0;
2042 
2043 		required = size - priv_dev->out_mem_is_allocated;
2044 
2045 		if (required > remained)
2046 			return -EPERM;
2047 
2048 		priv_dev->out_mem_is_allocated += required;
2049 		priv_dev->onchip_used_size += required;
2050 	}
2051 
2052 	return 0;
2053 }
2054 
cdns3_configure_dmult(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)2055 static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
2056 				  struct cdns3_endpoint *priv_ep)
2057 {
2058 	struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2059 
2060 	/* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
2061 	if (priv_dev->dev_ver <= DEV_VER_V2)
2062 		writel(USB_CONF_DMULT, &regs->usb_conf);
2063 
2064 	if (priv_dev->dev_ver == DEV_VER_V2)
2065 		writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
2066 
2067 	if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
2068 		u32 mask;
2069 
2070 		if (priv_ep->dir)
2071 			mask = BIT(priv_ep->num + 16);
2072 		else
2073 			mask = BIT(priv_ep->num);
2074 
2075 		if (priv_ep->type != USB_ENDPOINT_XFER_ISOC  && !priv_ep->dir) {
2076 			cdns3_set_register_bit(&regs->tdl_from_trb, mask);
2077 			cdns3_set_register_bit(&regs->tdl_beh, mask);
2078 			cdns3_set_register_bit(&regs->tdl_beh2, mask);
2079 			cdns3_set_register_bit(&regs->dma_adv_td, mask);
2080 		}
2081 
2082 		if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2083 			cdns3_set_register_bit(&regs->tdl_from_trb, mask);
2084 
2085 		cdns3_set_register_bit(&regs->dtrans, mask);
2086 	}
2087 }
2088 
2089 /**
2090  * cdns3_ep_config - Configure hardware endpoint
2091  * @priv_ep: extended endpoint object
2092  * @enable: set EP_CFG_ENABLE bit in ep_cfg register.
2093  */
cdns3_ep_config(struct cdns3_endpoint * priv_ep,bool enable)2094 int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
2095 {
2096 	bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
2097 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2098 	u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
2099 	u32 max_packet_size = priv_ep->wMaxPacketSize;
2100 	u8 maxburst = priv_ep->bMaxBurst;
2101 	u32 ep_cfg = 0;
2102 	u8 buffering;
2103 	int ret;
2104 
2105 	buffering = priv_dev->ep_buf_size - 1;
2106 
2107 	cdns3_configure_dmult(priv_dev, priv_ep);
2108 
2109 	switch (priv_ep->type) {
2110 	case USB_ENDPOINT_XFER_INT:
2111 		ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
2112 
2113 		if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
2114 			ep_cfg |= EP_CFG_TDL_CHK;
2115 		break;
2116 	case USB_ENDPOINT_XFER_BULK:
2117 		ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
2118 
2119 		if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
2120 			ep_cfg |= EP_CFG_TDL_CHK;
2121 		break;
2122 	default:
2123 		ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
2124 		buffering = (priv_ep->bMaxBurst + 1) * (priv_ep->mult + 1) - 1;
2125 	}
2126 
2127 	switch (priv_dev->gadget.speed) {
2128 	case USB_SPEED_FULL:
2129 		max_packet_size = is_iso_ep ? 1023 : 64;
2130 		break;
2131 	case USB_SPEED_HIGH:
2132 		max_packet_size = is_iso_ep ? 1024 : 512;
2133 		break;
2134 	case USB_SPEED_SUPER:
2135 		if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
2136 			max_packet_size = 1024;
2137 			maxburst = priv_dev->ep_buf_size - 1;
2138 		}
2139 		break;
2140 	default:
2141 		/* all other speed are not supported */
2142 		return -EINVAL;
2143 	}
2144 
2145 	if (max_packet_size == 1024)
2146 		priv_ep->trb_burst_size = 128;
2147 	else if (max_packet_size >= 512)
2148 		priv_ep->trb_burst_size = 64;
2149 	else
2150 		priv_ep->trb_burst_size = 16;
2151 
2152 	/*
2153 	 * In versions preceding DEV_VER_V2, for example, iMX8QM, there exit the bugs
2154 	 * in the DMA. These bugs occur when the trb_burst_size exceeds 16 and the
2155 	 * address is not aligned to 128 Bytes (which is a product of the 64-bit AXI
2156 	 * and AXI maximum burst length of 16 or 0xF+1, dma_axi_ctrl0[3:0]). This
2157 	 * results in data corruption when it crosses the 4K border. The corruption
2158 	 * specifically occurs from the position (4K - (address & 0x7F)) to 4K.
2159 	 *
2160 	 * So force trb_burst_size to 16 at such platform.
2161 	 */
2162 	if (priv_dev->dev_ver < DEV_VER_V2)
2163 		priv_ep->trb_burst_size = 16;
2164 
2165 	buffering = min_t(u8, buffering, EP_CFG_BUFFERING_MAX);
2166 	maxburst = min_t(u8, maxburst, EP_CFG_MAXBURST_MAX);
2167 
2168 	/* onchip buffer is only allocated before configuration */
2169 	if (!priv_dev->hw_configured_flag) {
2170 		ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2171 						     !!priv_ep->dir);
2172 		if (ret) {
2173 			dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2174 			return ret;
2175 		}
2176 	}
2177 
2178 	if (enable)
2179 		ep_cfg |= EP_CFG_ENABLE;
2180 
2181 	if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2182 		if (priv_dev->dev_ver >= DEV_VER_V3) {
2183 			u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
2184 
2185 			/*
2186 			 * Stream capable endpoints are handled by using ep_tdl
2187 			 * register. Other endpoints use TDL from TRB feature.
2188 			 */
2189 			cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
2190 						 mask);
2191 		}
2192 
2193 		/*  Enable Stream Bit TDL chk and SID chk */
2194 		ep_cfg |=  EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
2195 	}
2196 
2197 	ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
2198 		  EP_CFG_MULT(priv_ep->mult) |			/* must match EP setting */
2199 		  EP_CFG_BUFFERING(buffering) |
2200 		  EP_CFG_MAXBURST(maxburst);
2201 
2202 	cdns3_select_ep(priv_dev, bEndpointAddress);
2203 	writel(ep_cfg, &priv_dev->regs->ep_cfg);
2204 	priv_ep->flags |= EP_CONFIGURED;
2205 
2206 	dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
2207 		priv_ep->name, ep_cfg);
2208 
2209 	return 0;
2210 }
2211 
2212 /* Find correct direction for HW endpoint according to description */
cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor * desc,struct cdns3_endpoint * priv_ep)2213 static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
2214 				   struct cdns3_endpoint *priv_ep)
2215 {
2216 	return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
2217 	       (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
2218 }
2219 
2220 static struct
cdns3_find_available_ep(struct cdns3_device * priv_dev,struct usb_endpoint_descriptor * desc)2221 cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
2222 					struct usb_endpoint_descriptor *desc)
2223 {
2224 	struct usb_ep *ep;
2225 	struct cdns3_endpoint *priv_ep;
2226 
2227 	list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2228 		unsigned long num;
2229 		int ret;
2230 		/* ep name pattern likes epXin or epXout */
2231 		char c[2] = {ep->name[2], '\0'};
2232 
2233 		ret = kstrtoul(c, 10, &num);
2234 		if (ret)
2235 			return ERR_PTR(ret);
2236 
2237 		priv_ep = ep_to_cdns3_ep(ep);
2238 		if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
2239 			if (!(priv_ep->flags & EP_CLAIMED)) {
2240 				priv_ep->num  = num;
2241 				return priv_ep;
2242 			}
2243 		}
2244 	}
2245 
2246 	return ERR_PTR(-ENOENT);
2247 }
2248 
2249 /*
2250  *  Cadence IP has one limitation that all endpoints must be configured
2251  * (Type & MaxPacketSize) before setting configuration through hardware
2252  * register, it means we can't change endpoints configuration after
2253  * set_configuration.
2254  *
2255  * This function set EP_CLAIMED flag which is added when the gadget driver
2256  * uses usb_ep_autoconfig to configure specific endpoint;
2257  * When the udc driver receives set_configurion request,
2258  * it goes through all claimed endpoints, and configure all endpoints
2259  * accordingly.
2260  *
2261  * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
2262  * ep_cfg register which can be changed after set_configuration, and do
2263  * some software operation accordingly.
2264  */
2265 static struct
cdns3_gadget_match_ep(struct usb_gadget * gadget,struct usb_endpoint_descriptor * desc,struct usb_ss_ep_comp_descriptor * comp_desc)2266 usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
2267 			      struct usb_endpoint_descriptor *desc,
2268 			      struct usb_ss_ep_comp_descriptor *comp_desc)
2269 {
2270 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2271 	struct cdns3_endpoint *priv_ep;
2272 	unsigned long flags;
2273 
2274 	priv_ep = cdns3_find_available_ep(priv_dev, desc);
2275 	if (IS_ERR(priv_ep)) {
2276 		dev_err(priv_dev->dev, "no available ep\n");
2277 		return NULL;
2278 	}
2279 
2280 	dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
2281 
2282 	spin_lock_irqsave(&priv_dev->lock, flags);
2283 	priv_ep->endpoint.desc = desc;
2284 	priv_ep->dir  = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
2285 	priv_ep->type = usb_endpoint_type(desc);
2286 	priv_ep->flags |= EP_CLAIMED;
2287 	priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2288 	priv_ep->wMaxPacketSize =  usb_endpoint_maxp(desc);
2289 	priv_ep->mult = USB_EP_MAXP_MULT(priv_ep->wMaxPacketSize);
2290 	priv_ep->wMaxPacketSize &= USB_ENDPOINT_MAXP_MASK;
2291 	if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && comp_desc) {
2292 		priv_ep->mult =  USB_SS_MULT(comp_desc->bmAttributes) - 1;
2293 		priv_ep->bMaxBurst = comp_desc->bMaxBurst;
2294 	}
2295 
2296 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2297 	return &priv_ep->endpoint;
2298 }
2299 
2300 /**
2301  * cdns3_gadget_ep_alloc_request - Allocates request
2302  * @ep: endpoint object associated with request
2303  * @gfp_flags: gfp flags
2304  *
2305  * Returns allocated request address, NULL on allocation error
2306  */
cdns3_gadget_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)2307 struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
2308 						  gfp_t gfp_flags)
2309 {
2310 	struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2311 	struct cdns3_request *priv_req;
2312 
2313 	priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
2314 	if (!priv_req)
2315 		return NULL;
2316 
2317 	priv_req->priv_ep = priv_ep;
2318 
2319 	trace_cdns3_alloc_request(priv_req);
2320 	return &priv_req->request;
2321 }
2322 
2323 /**
2324  * cdns3_gadget_ep_free_request - Free memory occupied by request
2325  * @ep: endpoint object associated with request
2326  * @request: request to free memory
2327  */
cdns3_gadget_ep_free_request(struct usb_ep * ep,struct usb_request * request)2328 void cdns3_gadget_ep_free_request(struct usb_ep *ep,
2329 				  struct usb_request *request)
2330 {
2331 	struct cdns3_request *priv_req = to_cdns3_request(request);
2332 
2333 	if (priv_req->aligned_buf)
2334 		priv_req->aligned_buf->in_use = 0;
2335 
2336 	trace_cdns3_free_request(priv_req);
2337 	kfree(priv_req);
2338 }
2339 
2340 /**
2341  * cdns3_gadget_ep_enable - Enable endpoint
2342  * @ep: endpoint object
2343  * @desc: endpoint descriptor
2344  *
2345  * Returns 0 on success, error code elsewhere
2346  */
cdns3_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)2347 static int cdns3_gadget_ep_enable(struct usb_ep *ep,
2348 				  const struct usb_endpoint_descriptor *desc)
2349 {
2350 	struct cdns3_endpoint *priv_ep;
2351 	struct cdns3_device *priv_dev;
2352 	const struct usb_ss_ep_comp_descriptor *comp_desc;
2353 	u32 reg = EP_STS_EN_TRBERREN;
2354 	u32 bEndpointAddress;
2355 	unsigned long flags;
2356 	int enable = 1;
2357 	int ret = 0;
2358 	int val;
2359 
2360 	if (!ep) {
2361 		pr_debug("usbss: ep not configured?\n");
2362 		return -EINVAL;
2363 	}
2364 
2365 	priv_ep = ep_to_cdns3_ep(ep);
2366 	priv_dev = priv_ep->cdns3_dev;
2367 	comp_desc = priv_ep->endpoint.comp_desc;
2368 
2369 	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
2370 		dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
2371 		return -EINVAL;
2372 	}
2373 
2374 	if (!desc->wMaxPacketSize) {
2375 		dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
2376 		return -EINVAL;
2377 	}
2378 
2379 	if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
2380 			  "%s is already enabled\n", priv_ep->name))
2381 		return 0;
2382 
2383 	spin_lock_irqsave(&priv_dev->lock, flags);
2384 
2385 	priv_ep->endpoint.desc = desc;
2386 	priv_ep->type = usb_endpoint_type(desc);
2387 	priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2388 
2389 	if (priv_ep->interval > ISO_MAX_INTERVAL &&
2390 	    priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2391 		dev_err(priv_dev->dev, "Driver is limited to %d period\n",
2392 			ISO_MAX_INTERVAL);
2393 
2394 		ret =  -EINVAL;
2395 		goto exit;
2396 	}
2397 
2398 	bEndpointAddress = priv_ep->num | priv_ep->dir;
2399 	cdns3_select_ep(priv_dev, bEndpointAddress);
2400 
2401 	/*
2402 	 * For some versions of controller at some point during ISO OUT traffic
2403 	 * DMA reads Transfer Ring for the EP which has never got doorbell.
2404 	 * This issue was detected only on simulation, but to avoid this issue
2405 	 * driver add protection against it. To fix it driver enable ISO OUT
2406 	 * endpoint before setting DRBL. This special treatment of ISO OUT
2407 	 * endpoints are recommended by controller specification.
2408 	 */
2409 	if (priv_ep->type == USB_ENDPOINT_XFER_ISOC  && !priv_ep->dir)
2410 		enable = 0;
2411 
2412 	if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
2413 		/*
2414 		 * Enable stream support (SS mode) related interrupts
2415 		 * in EP_STS_EN Register
2416 		 */
2417 		if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2418 			reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
2419 				EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
2420 				EP_STS_EN_STREAMREN;
2421 			priv_ep->use_streams = true;
2422 			ret = cdns3_ep_config(priv_ep, enable);
2423 			priv_dev->using_streams |= true;
2424 		}
2425 	} else {
2426 		ret = cdns3_ep_config(priv_ep, enable);
2427 	}
2428 
2429 	if (ret)
2430 		goto exit;
2431 
2432 	ret = cdns3_allocate_trb_pool(priv_ep);
2433 	if (ret)
2434 		goto exit;
2435 
2436 	bEndpointAddress = priv_ep->num | priv_ep->dir;
2437 	cdns3_select_ep(priv_dev, bEndpointAddress);
2438 
2439 	trace_cdns3_gadget_ep_enable(priv_ep);
2440 
2441 	writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2442 
2443 	ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2444 					!(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2445 					1, 1000);
2446 
2447 	if (unlikely(ret)) {
2448 		cdns3_free_trb_pool(priv_ep);
2449 		ret =  -EINVAL;
2450 		goto exit;
2451 	}
2452 
2453 	/* enable interrupt for selected endpoint */
2454 	cdns3_set_register_bit(&priv_dev->regs->ep_ien,
2455 			       BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
2456 
2457 	if (priv_dev->dev_ver < DEV_VER_V2)
2458 		cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
2459 
2460 	writel(reg, &priv_dev->regs->ep_sts_en);
2461 
2462 	ep->desc = desc;
2463 	priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
2464 			    EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
2465 	priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
2466 	priv_ep->wa1_set = 0;
2467 	priv_ep->enqueue = 0;
2468 	priv_ep->dequeue = 0;
2469 	reg = readl(&priv_dev->regs->ep_sts);
2470 	priv_ep->pcs = !!EP_STS_CCS(reg);
2471 	priv_ep->ccs = !!EP_STS_CCS(reg);
2472 	/* one TRB is reserved for link TRB used in DMULT mode*/
2473 	priv_ep->free_trbs = priv_ep->num_trbs - 1;
2474 exit:
2475 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2476 
2477 	return ret;
2478 }
2479 
2480 /**
2481  * cdns3_gadget_ep_disable - Disable endpoint
2482  * @ep: endpoint object
2483  *
2484  * Returns 0 on success, error code elsewhere
2485  */
cdns3_gadget_ep_disable(struct usb_ep * ep)2486 static int cdns3_gadget_ep_disable(struct usb_ep *ep)
2487 {
2488 	struct cdns3_endpoint *priv_ep;
2489 	struct cdns3_request *priv_req;
2490 	struct cdns3_device *priv_dev;
2491 	struct usb_request *request;
2492 	unsigned long flags;
2493 	int ret = 0;
2494 	u32 ep_cfg;
2495 	int val;
2496 
2497 	if (!ep) {
2498 		pr_err("usbss: invalid parameters\n");
2499 		return -EINVAL;
2500 	}
2501 
2502 	priv_ep = ep_to_cdns3_ep(ep);
2503 	priv_dev = priv_ep->cdns3_dev;
2504 
2505 	if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
2506 			  "%s is already disabled\n", priv_ep->name))
2507 		return 0;
2508 
2509 	spin_lock_irqsave(&priv_dev->lock, flags);
2510 
2511 	trace_cdns3_gadget_ep_disable(priv_ep);
2512 
2513 	cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2514 
2515 	ep_cfg = readl(&priv_dev->regs->ep_cfg);
2516 	ep_cfg &= ~EP_CFG_ENABLE;
2517 	writel(ep_cfg, &priv_dev->regs->ep_cfg);
2518 
2519 	/**
2520 	 * Driver needs some time before resetting endpoint.
2521 	 * It need waits for clearing DBUSY bit or for timeout expired.
2522 	 * 10us is enough time for controller to stop transfer.
2523 	 */
2524 	readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
2525 				  !(val & EP_STS_DBUSY), 1, 10);
2526 	writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2527 
2528 	readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2529 				  !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2530 				  1, 1000);
2531 	if (unlikely(ret))
2532 		dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
2533 			priv_ep->name);
2534 
2535 	while (!list_empty(&priv_ep->pending_req_list)) {
2536 		request = cdns3_next_request(&priv_ep->pending_req_list);
2537 
2538 		cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2539 				      -ESHUTDOWN);
2540 	}
2541 
2542 	while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
2543 		priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
2544 		list_del_init(&priv_req->list);
2545 
2546 		kfree(priv_req->request.buf);
2547 		cdns3_gadget_ep_free_request(&priv_ep->endpoint,
2548 					     &priv_req->request);
2549 		--priv_ep->wa2_counter;
2550 	}
2551 
2552 	while (!list_empty(&priv_ep->deferred_req_list)) {
2553 		request = cdns3_next_request(&priv_ep->deferred_req_list);
2554 
2555 		cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2556 				      -ESHUTDOWN);
2557 	}
2558 
2559 	priv_ep->descmis_req = NULL;
2560 
2561 	ep->desc = NULL;
2562 	priv_ep->flags &= ~EP_ENABLED;
2563 	priv_ep->use_streams = false;
2564 
2565 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2566 
2567 	return ret;
2568 }
2569 
2570 /**
2571  * __cdns3_gadget_ep_queue - Transfer data on endpoint
2572  * @ep: endpoint object
2573  * @request: request object
2574  * @gfp_flags: gfp flags
2575  *
2576  * Returns 0 on success, error code elsewhere
2577  */
__cdns3_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)2578 static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
2579 				   struct usb_request *request,
2580 				   gfp_t gfp_flags)
2581 {
2582 	struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2583 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2584 	struct cdns3_request *priv_req;
2585 	int ret = 0;
2586 
2587 	request->actual = 0;
2588 	request->status = -EINPROGRESS;
2589 	priv_req = to_cdns3_request(request);
2590 	trace_cdns3_ep_queue(priv_req);
2591 
2592 	if (priv_dev->dev_ver < DEV_VER_V2) {
2593 		ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
2594 						priv_req);
2595 
2596 		if (ret == EINPROGRESS)
2597 			return 0;
2598 	}
2599 
2600 	ret = cdns3_prepare_aligned_request_buf(priv_req);
2601 	if (ret < 0)
2602 		return ret;
2603 
2604 	ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
2605 					    usb_endpoint_dir_in(ep->desc));
2606 	if (ret)
2607 		return ret;
2608 
2609 	list_add_tail(&request->list, &priv_ep->deferred_req_list);
2610 
2611 	/*
2612 	 * For stream capable endpoint if prime irq flag is set then only start
2613 	 * request.
2614 	 * If hardware endpoint configuration has not been set yet then
2615 	 * just queue request in deferred list. Transfer will be started in
2616 	 * cdns3_set_hw_configuration.
2617 	 */
2618 	if (!request->stream_id) {
2619 		if (priv_dev->hw_configured_flag &&
2620 		    !(priv_ep->flags & EP_STALLED) &&
2621 		    !(priv_ep->flags & EP_STALL_PENDING))
2622 			cdns3_start_all_request(priv_dev, priv_ep);
2623 	} else {
2624 		if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
2625 			cdns3_start_all_request(priv_dev, priv_ep);
2626 	}
2627 
2628 	return 0;
2629 }
2630 
cdns3_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)2631 static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2632 				 gfp_t gfp_flags)
2633 {
2634 	struct usb_request *zlp_request;
2635 	struct cdns3_endpoint *priv_ep;
2636 	struct cdns3_device *priv_dev;
2637 	unsigned long flags;
2638 	int ret;
2639 
2640 	if (!request || !ep)
2641 		return -EINVAL;
2642 
2643 	priv_ep = ep_to_cdns3_ep(ep);
2644 	priv_dev = priv_ep->cdns3_dev;
2645 
2646 	spin_lock_irqsave(&priv_dev->lock, flags);
2647 
2648 	ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
2649 
2650 	if (ret == 0 && request->zero && request->length &&
2651 	    (request->length % ep->maxpacket == 0)) {
2652 		struct cdns3_request *priv_req;
2653 
2654 		zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
2655 		zlp_request->buf = priv_dev->zlp_buf;
2656 		zlp_request->length = 0;
2657 
2658 		priv_req = to_cdns3_request(zlp_request);
2659 		priv_req->flags |= REQUEST_ZLP;
2660 
2661 		dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
2662 			priv_ep->name);
2663 		ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
2664 	}
2665 
2666 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2667 	return ret;
2668 }
2669 
2670 /**
2671  * cdns3_gadget_ep_dequeue - Remove request from transfer queue
2672  * @ep: endpoint object associated with request
2673  * @request: request object
2674  *
2675  * Returns 0 on success, error code elsewhere
2676  */
cdns3_gadget_ep_dequeue(struct usb_ep * ep,struct usb_request * request)2677 int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
2678 			    struct usb_request *request)
2679 {
2680 	struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2681 	struct cdns3_device *priv_dev;
2682 	struct usb_request *req, *req_temp;
2683 	struct cdns3_request *priv_req;
2684 	struct cdns3_trb *link_trb;
2685 	u8 req_on_hw_ring = 0;
2686 	unsigned long flags;
2687 	int ret = 0;
2688 	int val;
2689 
2690 	if (!ep || !request || !ep->desc)
2691 		return -EINVAL;
2692 
2693 	priv_dev = priv_ep->cdns3_dev;
2694 
2695 	spin_lock_irqsave(&priv_dev->lock, flags);
2696 
2697 	priv_req = to_cdns3_request(request);
2698 
2699 	trace_cdns3_ep_dequeue(priv_req);
2700 
2701 	cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2702 
2703 	list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
2704 				 list) {
2705 		if (request == req) {
2706 			req_on_hw_ring = 1;
2707 			goto found;
2708 		}
2709 	}
2710 
2711 	list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
2712 				 list) {
2713 		if (request == req)
2714 			goto found;
2715 	}
2716 
2717 	goto not_found;
2718 
2719 found:
2720 	link_trb = priv_req->trb;
2721 
2722 	/* Update ring only if removed request is on pending_req_list list */
2723 	if (req_on_hw_ring && link_trb) {
2724 		/* Stop DMA */
2725 		writel(EP_CMD_DFLUSH, &priv_dev->regs->ep_cmd);
2726 
2727 		/* wait for DFLUSH cleared */
2728 		readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2729 					  !(val & EP_CMD_DFLUSH), 1, 1000);
2730 
2731 		link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma +
2732 			((priv_req->end_trb + 1) * TRB_SIZE)));
2733 		link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) |
2734 				    TRB_TYPE(TRB_LINK) | TRB_CHAIN);
2735 
2736 		if (priv_ep->wa1_trb == priv_req->trb)
2737 			cdns3_wa1_restore_cycle_bit(priv_ep);
2738 	}
2739 
2740 	cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
2741 
2742 	req = cdns3_next_request(&priv_ep->pending_req_list);
2743 	if (req)
2744 		cdns3_rearm_transfer(priv_ep, 1);
2745 
2746 not_found:
2747 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2748 	return ret;
2749 }
2750 
2751 /**
2752  * __cdns3_gadget_ep_set_halt - Sets stall on selected endpoint
2753  * Should be called after acquiring spin_lock and selecting ep
2754  * @priv_ep: endpoint object to set stall on.
2755  */
__cdns3_gadget_ep_set_halt(struct cdns3_endpoint * priv_ep)2756 void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
2757 {
2758 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2759 
2760 	trace_cdns3_halt(priv_ep, 1, 0);
2761 
2762 	if (!(priv_ep->flags & EP_STALLED)) {
2763 		u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
2764 
2765 		if (!(ep_sts_reg & EP_STS_DBUSY))
2766 			cdns3_ep_stall_flush(priv_ep);
2767 		else
2768 			priv_ep->flags |= EP_STALL_PENDING;
2769 	}
2770 }
2771 
2772 /**
2773  * __cdns3_gadget_ep_clear_halt - Clears stall on selected endpoint
2774  * Should be called after acquiring spin_lock and selecting ep
2775  * @priv_ep: endpoint object to clear stall on
2776  */
__cdns3_gadget_ep_clear_halt(struct cdns3_endpoint * priv_ep)2777 int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
2778 {
2779 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2780 	struct usb_request *request;
2781 	struct cdns3_request *priv_req;
2782 	struct cdns3_trb *trb = NULL;
2783 	struct cdns3_trb trb_tmp;
2784 	int ret;
2785 	int val;
2786 
2787 	trace_cdns3_halt(priv_ep, 0, 0);
2788 
2789 	request = cdns3_next_request(&priv_ep->pending_req_list);
2790 	if (request) {
2791 		priv_req = to_cdns3_request(request);
2792 		trb = priv_req->trb;
2793 		if (trb) {
2794 			trb_tmp = *trb;
2795 			trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
2796 		}
2797 	}
2798 
2799 	writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2800 
2801 	/* wait for EPRST cleared */
2802 	ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2803 					!(val & EP_CMD_EPRST), 1, 100);
2804 	if (ret)
2805 		return -EINVAL;
2806 
2807 	priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
2808 
2809 	if (request) {
2810 		if (trb)
2811 			*trb = trb_tmp;
2812 
2813 		cdns3_rearm_transfer(priv_ep, 1);
2814 	}
2815 
2816 	cdns3_start_all_request(priv_dev, priv_ep);
2817 	return ret;
2818 }
2819 
2820 /**
2821  * cdns3_gadget_ep_set_halt - Sets/clears stall on selected endpoint
2822  * @ep: endpoint object to set/clear stall on
2823  * @value: 1 for set stall, 0 for clear stall
2824  *
2825  * Returns 0 on success, error code elsewhere
2826  */
cdns3_gadget_ep_set_halt(struct usb_ep * ep,int value)2827 int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2828 {
2829 	struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2830 	struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2831 	unsigned long flags;
2832 	int ret = 0;
2833 
2834 	if (!(priv_ep->flags & EP_ENABLED))
2835 		return -EPERM;
2836 
2837 	spin_lock_irqsave(&priv_dev->lock, flags);
2838 
2839 	cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2840 
2841 	if (!value) {
2842 		priv_ep->flags &= ~EP_WEDGE;
2843 		ret = __cdns3_gadget_ep_clear_halt(priv_ep);
2844 	} else {
2845 		__cdns3_gadget_ep_set_halt(priv_ep);
2846 	}
2847 
2848 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2849 
2850 	return ret;
2851 }
2852 
2853 extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
2854 
2855 static const struct usb_ep_ops cdns3_gadget_ep_ops = {
2856 	.enable = cdns3_gadget_ep_enable,
2857 	.disable = cdns3_gadget_ep_disable,
2858 	.alloc_request = cdns3_gadget_ep_alloc_request,
2859 	.free_request = cdns3_gadget_ep_free_request,
2860 	.queue = cdns3_gadget_ep_queue,
2861 	.dequeue = cdns3_gadget_ep_dequeue,
2862 	.set_halt = cdns3_gadget_ep_set_halt,
2863 	.set_wedge = cdns3_gadget_ep_set_wedge,
2864 };
2865 
2866 /**
2867  * cdns3_gadget_get_frame - Returns number of actual ITP frame
2868  * @gadget: gadget object
2869  *
2870  * Returns number of actual ITP frame
2871  */
cdns3_gadget_get_frame(struct usb_gadget * gadget)2872 static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
2873 {
2874 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2875 
2876 	return readl(&priv_dev->regs->usb_itpn);
2877 }
2878 
__cdns3_gadget_wakeup(struct cdns3_device * priv_dev)2879 int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
2880 {
2881 	enum usb_device_speed speed;
2882 
2883 	speed = cdns3_get_speed(priv_dev);
2884 
2885 	if (speed >= USB_SPEED_SUPER)
2886 		return 0;
2887 
2888 	/* Start driving resume signaling to indicate remote wakeup. */
2889 	writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
2890 
2891 	return 0;
2892 }
2893 
cdns3_gadget_wakeup(struct usb_gadget * gadget)2894 static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
2895 {
2896 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2897 	unsigned long flags;
2898 	int ret = 0;
2899 
2900 	spin_lock_irqsave(&priv_dev->lock, flags);
2901 	ret = __cdns3_gadget_wakeup(priv_dev);
2902 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2903 	return ret;
2904 }
2905 
cdns3_gadget_set_selfpowered(struct usb_gadget * gadget,int is_selfpowered)2906 static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
2907 					int is_selfpowered)
2908 {
2909 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2910 	unsigned long flags;
2911 
2912 	spin_lock_irqsave(&priv_dev->lock, flags);
2913 	priv_dev->is_selfpowered = !!is_selfpowered;
2914 	spin_unlock_irqrestore(&priv_dev->lock, flags);
2915 	return 0;
2916 }
2917 
cdns3_gadget_pullup(struct usb_gadget * gadget,int is_on)2918 static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
2919 {
2920 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2921 
2922 	if (is_on) {
2923 		writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
2924 	} else {
2925 		writel(~0, &priv_dev->regs->ep_ists);
2926 		writel(~0, &priv_dev->regs->usb_ists);
2927 		writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2928 	}
2929 
2930 	return 0;
2931 }
2932 
cdns3_gadget_config(struct cdns3_device * priv_dev)2933 static void cdns3_gadget_config(struct cdns3_device *priv_dev)
2934 {
2935 	struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2936 	u32 reg;
2937 
2938 	cdns3_ep0_config(priv_dev);
2939 
2940 	/* enable interrupts for endpoint 0 (in and out) */
2941 	writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, &regs->ep_ien);
2942 
2943 	/*
2944 	 * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
2945 	 * revision of controller.
2946 	 */
2947 	if (priv_dev->dev_ver == DEV_VER_TI_V1) {
2948 		reg = readl(&regs->dbg_link1);
2949 
2950 		reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
2951 		reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
2952 		       DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
2953 		writel(reg, &regs->dbg_link1);
2954 	}
2955 
2956 	/*
2957 	 * By default some platforms has set protected access to memory.
2958 	 * This cause problem with cache, so driver restore non-secure
2959 	 * access to memory.
2960 	 */
2961 	reg = readl(&regs->dma_axi_ctrl);
2962 	reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
2963 	       DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
2964 	writel(reg, &regs->dma_axi_ctrl);
2965 
2966 	/* enable generic interrupt*/
2967 	writel(USB_IEN_INIT, &regs->usb_ien);
2968 	writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, &regs->usb_conf);
2969 	/*  keep Fast Access bit */
2970 	writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
2971 
2972 	cdns3_configure_dmult(priv_dev, NULL);
2973 }
2974 
2975 /**
2976  * cdns3_gadget_udc_start - Gadget start
2977  * @gadget: gadget object
2978  * @driver: driver which operates on this gadget
2979  *
2980  * Returns 0 on success, error code elsewhere
2981  */
cdns3_gadget_udc_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)2982 static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
2983 				  struct usb_gadget_driver *driver)
2984 {
2985 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2986 	unsigned long flags;
2987 	enum usb_device_speed max_speed = driver->max_speed;
2988 
2989 	spin_lock_irqsave(&priv_dev->lock, flags);
2990 	priv_dev->gadget_driver = driver;
2991 
2992 	/* limit speed if necessary */
2993 	max_speed = min(driver->max_speed, gadget->max_speed);
2994 
2995 	switch (max_speed) {
2996 	case USB_SPEED_FULL:
2997 		writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
2998 		writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2999 		break;
3000 	case USB_SPEED_HIGH:
3001 		writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
3002 		break;
3003 	case USB_SPEED_SUPER:
3004 		break;
3005 	default:
3006 		dev_err(priv_dev->dev,
3007 			"invalid maximum_speed parameter %d\n",
3008 			max_speed);
3009 		fallthrough;
3010 	case USB_SPEED_UNKNOWN:
3011 		/* default to superspeed */
3012 		max_speed = USB_SPEED_SUPER;
3013 		break;
3014 	}
3015 
3016 	cdns3_gadget_config(priv_dev);
3017 	spin_unlock_irqrestore(&priv_dev->lock, flags);
3018 	return 0;
3019 }
3020 
3021 /**
3022  * cdns3_gadget_udc_stop - Stops gadget
3023  * @gadget: gadget object
3024  *
3025  * Returns 0
3026  */
cdns3_gadget_udc_stop(struct usb_gadget * gadget)3027 static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
3028 {
3029 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
3030 	struct cdns3_endpoint *priv_ep;
3031 	u32 bEndpointAddress;
3032 	struct usb_ep *ep;
3033 	int val;
3034 
3035 	priv_dev->gadget_driver = NULL;
3036 
3037 	priv_dev->onchip_used_size = 0;
3038 	priv_dev->out_mem_is_allocated = 0;
3039 	priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3040 
3041 	list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
3042 		priv_ep = ep_to_cdns3_ep(ep);
3043 		bEndpointAddress = priv_ep->num | priv_ep->dir;
3044 		cdns3_select_ep(priv_dev, bEndpointAddress);
3045 		writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
3046 		readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
3047 					  !(val & EP_CMD_EPRST), 1, 100);
3048 
3049 		priv_ep->flags &= ~EP_CLAIMED;
3050 	}
3051 
3052 	/* disable interrupt for device */
3053 	writel(0, &priv_dev->regs->usb_ien);
3054 	writel(0, &priv_dev->regs->usb_pwr);
3055 	writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
3056 
3057 	return 0;
3058 }
3059 
3060 /**
3061  * cdns3_gadget_check_config - ensure cdns3 can support the USB configuration
3062  * @gadget: pointer to the USB gadget
3063  *
3064  * Used to record the maximum number of endpoints being used in a USB composite
3065  * device. (across all configurations)  This is to be used in the calculation
3066  * of the TXFIFO sizes when resizing internal memory for individual endpoints.
3067  * It will help ensured that the resizing logic reserves enough space for at
3068  * least one max packet.
3069  */
cdns3_gadget_check_config(struct usb_gadget * gadget)3070 static int cdns3_gadget_check_config(struct usb_gadget *gadget)
3071 {
3072 	struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
3073 	struct cdns3_endpoint *priv_ep;
3074 	struct usb_ep *ep;
3075 	int n_in = 0;
3076 	int iso = 0;
3077 	int out = 1;
3078 	int total;
3079 	int n;
3080 
3081 	list_for_each_entry(ep, &gadget->ep_list, ep_list) {
3082 		priv_ep = ep_to_cdns3_ep(ep);
3083 		if (!(priv_ep->flags & EP_CLAIMED))
3084 			continue;
3085 
3086 		n = (priv_ep->mult + 1) * (priv_ep->bMaxBurst + 1);
3087 		if (ep->address & USB_DIR_IN) {
3088 			/*
3089 			 * ISO transfer: DMA start move data when get ISO, only transfer
3090 			 * data as min(TD size, iso). No benefit for allocate bigger
3091 			 * internal memory than 'iso'.
3092 			 */
3093 			if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
3094 				iso += n;
3095 			else
3096 				n_in++;
3097 		} else {
3098 			if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
3099 				out = max_t(int, out, n);
3100 		}
3101 	}
3102 
3103 	/* 2KB are reserved for EP0, 1KB for out*/
3104 	total = 2 + n_in + out + iso;
3105 
3106 	if (total > priv_dev->onchip_buffers)
3107 		return -ENOMEM;
3108 
3109 	priv_dev->ep_buf_size = (priv_dev->onchip_buffers - 2 - iso) / (n_in + out);
3110 
3111 	return 0;
3112 }
3113 
3114 static const struct usb_gadget_ops cdns3_gadget_ops = {
3115 	.get_frame = cdns3_gadget_get_frame,
3116 	.wakeup = cdns3_gadget_wakeup,
3117 	.set_selfpowered = cdns3_gadget_set_selfpowered,
3118 	.pullup = cdns3_gadget_pullup,
3119 	.udc_start = cdns3_gadget_udc_start,
3120 	.udc_stop = cdns3_gadget_udc_stop,
3121 	.match_ep = cdns3_gadget_match_ep,
3122 	.check_config = cdns3_gadget_check_config,
3123 };
3124 
cdns3_free_all_eps(struct cdns3_device * priv_dev)3125 static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
3126 {
3127 	int i;
3128 
3129 	/* ep0 OUT point to ep0 IN. */
3130 	priv_dev->eps[16] = NULL;
3131 
3132 	for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
3133 		if (priv_dev->eps[i]) {
3134 			cdns3_free_trb_pool(priv_dev->eps[i]);
3135 			devm_kfree(priv_dev->dev, priv_dev->eps[i]);
3136 		}
3137 }
3138 
3139 /**
3140  * cdns3_init_eps - Initializes software endpoints of gadget
3141  * @priv_dev: extended gadget object
3142  *
3143  * Returns 0 on success, error code elsewhere
3144  */
cdns3_init_eps(struct cdns3_device * priv_dev)3145 static int cdns3_init_eps(struct cdns3_device *priv_dev)
3146 {
3147 	u32 ep_enabled_reg, iso_ep_reg;
3148 	struct cdns3_endpoint *priv_ep;
3149 	int ep_dir, ep_number;
3150 	u32 ep_mask;
3151 	int ret = 0;
3152 	int i;
3153 
3154 	/* Read it from USB_CAP3 to USB_CAP5 */
3155 	ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
3156 	iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
3157 
3158 	dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
3159 
3160 	for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
3161 		ep_dir = i >> 4;	/* i div 16 */
3162 		ep_number = i & 0xF;	/* i % 16 */
3163 		ep_mask = BIT(i);
3164 
3165 		if (!(ep_enabled_reg & ep_mask))
3166 			continue;
3167 
3168 		if (ep_dir && !ep_number) {
3169 			priv_dev->eps[i] = priv_dev->eps[0];
3170 			continue;
3171 		}
3172 
3173 		priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
3174 				       GFP_KERNEL);
3175 		if (!priv_ep)
3176 			goto err;
3177 
3178 		/* set parent of endpoint object */
3179 		priv_ep->cdns3_dev = priv_dev;
3180 		priv_dev->eps[i] = priv_ep;
3181 		priv_ep->num = ep_number;
3182 		priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
3183 
3184 		if (!ep_number) {
3185 			ret = cdns3_init_ep0(priv_dev, priv_ep);
3186 			if (ret) {
3187 				dev_err(priv_dev->dev, "Failed to init ep0\n");
3188 				goto err;
3189 			}
3190 		} else {
3191 			snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
3192 				 ep_number, !!ep_dir ? "in" : "out");
3193 			priv_ep->endpoint.name = priv_ep->name;
3194 
3195 			usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
3196 						   CDNS3_EP_MAX_PACKET_LIMIT);
3197 			priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
3198 			priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
3199 			if (ep_dir)
3200 				priv_ep->endpoint.caps.dir_in = 1;
3201 			else
3202 				priv_ep->endpoint.caps.dir_out = 1;
3203 
3204 			if (iso_ep_reg & ep_mask)
3205 				priv_ep->endpoint.caps.type_iso = 1;
3206 
3207 			priv_ep->endpoint.caps.type_bulk = 1;
3208 			priv_ep->endpoint.caps.type_int = 1;
3209 
3210 			list_add_tail(&priv_ep->endpoint.ep_list,
3211 				      &priv_dev->gadget.ep_list);
3212 		}
3213 
3214 		priv_ep->flags = 0;
3215 
3216 		dev_dbg(priv_dev->dev, "Initialized  %s support: %s %s\n",
3217 			 priv_ep->name,
3218 			 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
3219 			 priv_ep->endpoint.caps.type_iso ? "ISO" : "");
3220 
3221 		INIT_LIST_HEAD(&priv_ep->pending_req_list);
3222 		INIT_LIST_HEAD(&priv_ep->deferred_req_list);
3223 		INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
3224 	}
3225 
3226 	return 0;
3227 err:
3228 	cdns3_free_all_eps(priv_dev);
3229 	return -ENOMEM;
3230 }
3231 
cdns3_gadget_release(struct device * dev)3232 static void cdns3_gadget_release(struct device *dev)
3233 {
3234 	struct cdns3_device *priv_dev = container_of(dev,
3235 			struct cdns3_device, gadget.dev);
3236 
3237 	kfree(priv_dev);
3238 }
3239 
cdns3_gadget_exit(struct cdns * cdns)3240 static void cdns3_gadget_exit(struct cdns *cdns)
3241 {
3242 	struct cdns3_device *priv_dev;
3243 
3244 	priv_dev = cdns->gadget_dev;
3245 
3246 
3247 	pm_runtime_mark_last_busy(cdns->dev);
3248 	pm_runtime_put_autosuspend(cdns->dev);
3249 
3250 	usb_del_gadget(&priv_dev->gadget);
3251 	devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
3252 
3253 	cdns3_free_all_eps(priv_dev);
3254 
3255 	while (!list_empty(&priv_dev->aligned_buf_list)) {
3256 		struct cdns3_aligned_buf *buf;
3257 
3258 		buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
3259 		dma_free_noncoherent(priv_dev->sysdev, buf->size,
3260 				  buf->buf,
3261 				  buf->dma,
3262 				  buf->dir);
3263 
3264 		list_del(&buf->list);
3265 		kfree(buf);
3266 	}
3267 
3268 	dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3269 			  priv_dev->setup_dma);
3270 	dma_pool_destroy(priv_dev->eps_dma_pool);
3271 
3272 	kfree(priv_dev->zlp_buf);
3273 	usb_put_gadget(&priv_dev->gadget);
3274 	cdns->gadget_dev = NULL;
3275 	cdns_drd_gadget_off(cdns);
3276 }
3277 
cdns3_gadget_start(struct cdns * cdns)3278 static int cdns3_gadget_start(struct cdns *cdns)
3279 {
3280 	struct cdns3_device *priv_dev;
3281 	u32 max_speed;
3282 	int ret;
3283 
3284 	priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
3285 	if (!priv_dev)
3286 		return -ENOMEM;
3287 
3288 	usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
3289 			cdns3_gadget_release);
3290 	cdns->gadget_dev = priv_dev;
3291 	priv_dev->sysdev = cdns->dev;
3292 	priv_dev->dev = cdns->dev;
3293 	priv_dev->regs = cdns->dev_regs;
3294 
3295 	device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
3296 				 &priv_dev->onchip_buffers);
3297 
3298 	if (priv_dev->onchip_buffers <=  0) {
3299 		u32 reg = readl(&priv_dev->regs->usb_cap2);
3300 
3301 		priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
3302 	}
3303 
3304 	if (!priv_dev->onchip_buffers)
3305 		priv_dev->onchip_buffers = 256;
3306 
3307 	max_speed = usb_get_maximum_speed(cdns->dev);
3308 
3309 	/* Check the maximum_speed parameter */
3310 	switch (max_speed) {
3311 	case USB_SPEED_FULL:
3312 	case USB_SPEED_HIGH:
3313 	case USB_SPEED_SUPER:
3314 		break;
3315 	default:
3316 		dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
3317 			max_speed);
3318 		fallthrough;
3319 	case USB_SPEED_UNKNOWN:
3320 		/* default to superspeed */
3321 		max_speed = USB_SPEED_SUPER;
3322 		break;
3323 	}
3324 
3325 	/* fill gadget fields */
3326 	priv_dev->gadget.max_speed = max_speed;
3327 	priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3328 	priv_dev->gadget.ops = &cdns3_gadget_ops;
3329 	priv_dev->gadget.name = "usb-ss-gadget";
3330 	priv_dev->gadget.quirk_avoids_skb_reserve = 1;
3331 	priv_dev->gadget.irq = cdns->dev_irq;
3332 
3333 	spin_lock_init(&priv_dev->lock);
3334 	INIT_WORK(&priv_dev->pending_status_wq,
3335 		  cdns3_pending_setup_status_handler);
3336 
3337 	INIT_WORK(&priv_dev->aligned_buf_wq,
3338 		  cdns3_free_aligned_request_buf);
3339 
3340 	/* initialize endpoint container */
3341 	INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
3342 	INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
3343 	priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
3344 						 priv_dev->sysdev,
3345 						 TRB_RING_SIZE, 8, 0);
3346 	if (!priv_dev->eps_dma_pool) {
3347 		dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
3348 		ret = -ENOMEM;
3349 		goto err1;
3350 	}
3351 
3352 	ret = cdns3_init_eps(priv_dev);
3353 	if (ret) {
3354 		dev_err(priv_dev->dev, "Failed to create endpoints\n");
3355 		goto err1;
3356 	}
3357 
3358 	/* allocate memory for setup packet buffer */
3359 	priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
3360 						 &priv_dev->setup_dma, GFP_DMA);
3361 	if (!priv_dev->setup_buf) {
3362 		ret = -ENOMEM;
3363 		goto err2;
3364 	}
3365 
3366 	priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
3367 
3368 	dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
3369 		readl(&priv_dev->regs->usb_cap6));
3370 	dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
3371 		readl(&priv_dev->regs->usb_cap1));
3372 	dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
3373 		readl(&priv_dev->regs->usb_cap2));
3374 
3375 	priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
3376 	if (priv_dev->dev_ver >= DEV_VER_V2)
3377 		priv_dev->gadget.sg_supported = 1;
3378 
3379 	priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
3380 	if (!priv_dev->zlp_buf) {
3381 		ret = -ENOMEM;
3382 		goto err3;
3383 	}
3384 
3385 	/* add USB gadget device */
3386 	ret = usb_add_gadget(&priv_dev->gadget);
3387 	if (ret < 0) {
3388 		dev_err(priv_dev->dev, "Failed to add gadget\n");
3389 		goto err4;
3390 	}
3391 
3392 	return 0;
3393 err4:
3394 	kfree(priv_dev->zlp_buf);
3395 err3:
3396 	dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3397 			  priv_dev->setup_dma);
3398 err2:
3399 	cdns3_free_all_eps(priv_dev);
3400 err1:
3401 	dma_pool_destroy(priv_dev->eps_dma_pool);
3402 
3403 	usb_put_gadget(&priv_dev->gadget);
3404 	cdns->gadget_dev = NULL;
3405 	return ret;
3406 }
3407 
__cdns3_gadget_init(struct cdns * cdns)3408 static int __cdns3_gadget_init(struct cdns *cdns)
3409 {
3410 	int ret = 0;
3411 
3412 	/* Ensure 32-bit DMA Mask in case we switched back from Host mode */
3413 	ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
3414 	if (ret) {
3415 		dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
3416 		return ret;
3417 	}
3418 
3419 	cdns_drd_gadget_on(cdns);
3420 	pm_runtime_get_sync(cdns->dev);
3421 
3422 	ret = cdns3_gadget_start(cdns);
3423 	if (ret) {
3424 		pm_runtime_put_sync(cdns->dev);
3425 		return ret;
3426 	}
3427 
3428 	/*
3429 	 * Because interrupt line can be shared with other components in
3430 	 * driver it can't use IRQF_ONESHOT flag here.
3431 	 */
3432 	ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
3433 					cdns3_device_irq_handler,
3434 					cdns3_device_thread_irq_handler,
3435 					IRQF_SHARED, dev_name(cdns->dev),
3436 					cdns->gadget_dev);
3437 
3438 	if (ret)
3439 		goto err0;
3440 
3441 	return 0;
3442 err0:
3443 	cdns3_gadget_exit(cdns);
3444 	return ret;
3445 }
3446 
cdns3_gadget_suspend(struct cdns * cdns,bool do_wakeup)3447 static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup)
3448 __must_hold(&cdns->lock)
3449 {
3450 	struct cdns3_device *priv_dev = cdns->gadget_dev;
3451 
3452 	spin_unlock(&cdns->lock);
3453 	cdns3_disconnect_gadget(priv_dev);
3454 	spin_lock(&cdns->lock);
3455 
3456 	priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3457 	usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
3458 	cdns3_hw_reset_eps_config(priv_dev);
3459 
3460 	/* disable interrupt for device */
3461 	writel(0, &priv_dev->regs->usb_ien);
3462 
3463 	return 0;
3464 }
3465 
cdns3_gadget_resume(struct cdns * cdns,bool hibernated)3466 static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
3467 {
3468 	struct cdns3_device *priv_dev = cdns->gadget_dev;
3469 
3470 	if (!priv_dev->gadget_driver)
3471 		return 0;
3472 
3473 	cdns3_gadget_config(priv_dev);
3474 	if (hibernated)
3475 		writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
3476 
3477 	return 0;
3478 }
3479 
3480 /**
3481  * cdns3_gadget_init - initialize device structure
3482  *
3483  * @cdns: cdns instance
3484  *
3485  * This function initializes the gadget.
3486  */
cdns3_gadget_init(struct cdns * cdns)3487 int cdns3_gadget_init(struct cdns *cdns)
3488 {
3489 	struct cdns_role_driver *rdrv;
3490 
3491 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
3492 	if (!rdrv)
3493 		return -ENOMEM;
3494 
3495 	rdrv->start	= __cdns3_gadget_init;
3496 	rdrv->stop	= cdns3_gadget_exit;
3497 	rdrv->suspend	= cdns3_gadget_suspend;
3498 	rdrv->resume	= cdns3_gadget_resume;
3499 	rdrv->state	= CDNS_ROLE_STATE_INACTIVE;
3500 	rdrv->name	= "gadget";
3501 	cdns->roles[USB_ROLE_DEVICE] = rdrv;
3502 
3503 	return 0;
3504 }
3505