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