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