xref: /openbmc/linux/drivers/usb/cdns3/cdnsp-gadget.c (revision 55b7acbd15b15e75c6df468c72177a6b32e648cf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence CDNSP DRD Driver.
4  *
5  * Copyright (C) 2020 Cadence.
6  *
7  * Author: Pawel Laszczak <pawell@cadence.com>
8  *
9  */
10 
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/iopoll.h>
15 #include <linux/delay.h>
16 #include <linux/log2.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/irq.h>
20 #include <linux/dmi.h>
21 
22 #include "core.h"
23 #include "gadget-export.h"
24 #include "drd.h"
25 #include "cdnsp-gadget.h"
26 #include "cdnsp-trace.h"
27 
cdnsp_port_speed(unsigned int port_status)28 unsigned int cdnsp_port_speed(unsigned int port_status)
29 {
30 	/*Detect gadget speed based on PORTSC register*/
31 	if (DEV_SUPERSPEEDPLUS(port_status) ||
32 	    DEV_SSP_GEN1x2(port_status) || DEV_SSP_GEN2x2(port_status))
33 		return USB_SPEED_SUPER_PLUS;
34 	else if (DEV_SUPERSPEED(port_status))
35 		return USB_SPEED_SUPER;
36 	else if (DEV_HIGHSPEED(port_status))
37 		return USB_SPEED_HIGH;
38 	else if (DEV_FULLSPEED(port_status))
39 		return USB_SPEED_FULL;
40 
41 	/* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
42 	return USB_SPEED_UNKNOWN;
43 }
44 
45 /*
46  * Given a port state, this function returns a value that would result in the
47  * port being in the same state, if the value was written to the port status
48  * control register.
49  * Save Read Only (RO) bits and save read/write bits where
50  * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
51  * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
52  */
cdnsp_port_state_to_neutral(u32 state)53 u32 cdnsp_port_state_to_neutral(u32 state)
54 {
55 	/* Save read-only status and port state. */
56 	return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
57 }
58 
59 /**
60  * cdnsp_find_next_ext_cap - Find the offset of the extended capabilities
61  *                           with capability ID id.
62  * @base: PCI MMIO registers base address.
63  * @start: Address at which to start looking, (0 or HCC_PARAMS to start at
64  *         beginning of list)
65  * @id: Extended capability ID to search for.
66  *
67  * Returns the offset of the next matching extended capability structure.
68  * Some capabilities can occur several times,
69  * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all.
70  */
cdnsp_find_next_ext_cap(void __iomem * base,u32 start,int id)71 int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id)
72 {
73 	u32 offset = start;
74 	u32 next;
75 	u32 val;
76 
77 	if (!start || start == HCC_PARAMS_OFFSET) {
78 		val = readl(base + HCC_PARAMS_OFFSET);
79 		if (val == ~0)
80 			return 0;
81 
82 		offset = HCC_EXT_CAPS(val) << 2;
83 		if (!offset)
84 			return 0;
85 	}
86 
87 	do {
88 		val = readl(base + offset);
89 		if (val == ~0)
90 			return 0;
91 
92 		if (EXT_CAPS_ID(val) == id && offset != start)
93 			return offset;
94 
95 		next = EXT_CAPS_NEXT(val);
96 		offset += next << 2;
97 	} while (next);
98 
99 	return 0;
100 }
101 
cdnsp_set_link_state(struct cdnsp_device * pdev,__le32 __iomem * port_regs,u32 link_state)102 void cdnsp_set_link_state(struct cdnsp_device *pdev,
103 			  __le32 __iomem *port_regs,
104 			  u32 link_state)
105 {
106 	int port_num = 0xFF;
107 	u32 temp;
108 
109 	temp = readl(port_regs);
110 	temp = cdnsp_port_state_to_neutral(temp);
111 	temp |= PORT_WKCONN_E | PORT_WKDISC_E;
112 	writel(temp, port_regs);
113 
114 	temp &= ~PORT_PLS_MASK;
115 	temp |= PORT_LINK_STROBE | link_state;
116 
117 	if (pdev->active_port)
118 		port_num = pdev->active_port->port_num;
119 
120 	trace_cdnsp_handle_port_status(port_num, readl(port_regs));
121 	writel(temp, port_regs);
122 	trace_cdnsp_link_state_changed(port_num, readl(port_regs));
123 }
124 
cdnsp_disable_port(struct cdnsp_device * pdev,__le32 __iomem * port_regs)125 static void cdnsp_disable_port(struct cdnsp_device *pdev,
126 			       __le32 __iomem *port_regs)
127 {
128 	u32 temp = cdnsp_port_state_to_neutral(readl(port_regs));
129 
130 	writel(temp | PORT_PED, port_regs);
131 }
132 
cdnsp_clear_port_change_bit(struct cdnsp_device * pdev,__le32 __iomem * port_regs)133 static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev,
134 					__le32 __iomem *port_regs)
135 {
136 	u32 portsc = readl(port_regs);
137 
138 	writel(cdnsp_port_state_to_neutral(portsc) |
139 	       (portsc & PORT_CHANGE_BITS), port_regs);
140 }
141 
cdnsp_set_apb_timeout_value(struct cdnsp_device * pdev)142 static void cdnsp_set_apb_timeout_value(struct cdnsp_device *pdev)
143 {
144 	struct cdns *cdns = dev_get_drvdata(pdev->dev);
145 	__le32 __iomem *reg;
146 	void __iomem *base;
147 	u32 offset = 0;
148 	u32 val;
149 
150 	if (!cdns->override_apb_timeout)
151 		return;
152 
153 	base = &pdev->cap_regs->hc_capbase;
154 	offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
155 	reg = base + offset + REG_CHICKEN_BITS_3_OFFSET;
156 
157 	val  = le32_to_cpu(readl(reg));
158 	val = CHICKEN_APB_TIMEOUT_SET(val, cdns->override_apb_timeout);
159 	writel(cpu_to_le32(val), reg);
160 }
161 
cdnsp_set_chicken_bits_2(struct cdnsp_device * pdev,u32 bit)162 static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
163 {
164 	__le32 __iomem *reg;
165 	void __iomem *base;
166 	u32 offset = 0;
167 
168 	base = &pdev->cap_regs->hc_capbase;
169 	offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
170 	reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
171 
172 	bit = readl(reg) | bit;
173 	writel(bit, reg);
174 }
175 
cdnsp_clear_chicken_bits_2(struct cdnsp_device * pdev,u32 bit)176 static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
177 {
178 	__le32 __iomem *reg;
179 	void __iomem *base;
180 	u32 offset = 0;
181 
182 	base = &pdev->cap_regs->hc_capbase;
183 	offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
184 	reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
185 
186 	bit = readl(reg) & ~bit;
187 	writel(bit, reg);
188 }
189 
190 /*
191  * Disable interrupts and begin the controller halting process.
192  */
cdnsp_quiesce(struct cdnsp_device * pdev)193 static void cdnsp_quiesce(struct cdnsp_device *pdev)
194 {
195 	u32 halted;
196 	u32 mask;
197 	u32 cmd;
198 
199 	mask = ~(u32)(CDNSP_IRQS);
200 
201 	halted = readl(&pdev->op_regs->status) & STS_HALT;
202 	if (!halted)
203 		mask &= ~(CMD_R_S | CMD_DEVEN);
204 
205 	cmd = readl(&pdev->op_regs->command);
206 	cmd &= mask;
207 	writel(cmd, &pdev->op_regs->command);
208 }
209 
210 /*
211  * Force controller into halt state.
212  *
213  * Disable any IRQs and clear the run/stop bit.
214  * Controller will complete any current and actively pipelined transactions, and
215  * should halt within 16 ms of the run/stop bit being cleared.
216  * Read controller Halted bit in the status register to see when the
217  * controller is finished.
218  */
cdnsp_halt(struct cdnsp_device * pdev)219 int cdnsp_halt(struct cdnsp_device *pdev)
220 {
221 	int ret;
222 	u32 val;
223 
224 	cdnsp_quiesce(pdev);
225 
226 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val,
227 					val & STS_HALT, 1,
228 					CDNSP_MAX_HALT_USEC);
229 	if (ret) {
230 		dev_err(pdev->dev, "ERROR: Device halt failed\n");
231 		return ret;
232 	}
233 
234 	pdev->cdnsp_state |= CDNSP_STATE_HALTED;
235 
236 	return 0;
237 }
238 
239 /*
240  * device controller died, register read returns 0xffffffff, or command never
241  * ends.
242  */
cdnsp_died(struct cdnsp_device * pdev)243 void cdnsp_died(struct cdnsp_device *pdev)
244 {
245 	dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n");
246 	pdev->cdnsp_state |= CDNSP_STATE_DYING;
247 	cdnsp_halt(pdev);
248 }
249 
250 /*
251  * Set the run bit and wait for the device to be running.
252  */
cdnsp_start(struct cdnsp_device * pdev)253 static int cdnsp_start(struct cdnsp_device *pdev)
254 {
255 	u32 temp;
256 	int ret;
257 
258 	temp = readl(&pdev->op_regs->command);
259 	temp |= (CMD_R_S | CMD_DEVEN);
260 	writel(temp, &pdev->op_regs->command);
261 
262 	pdev->cdnsp_state = 0;
263 
264 	/*
265 	 * Wait for the STS_HALT Status bit to be 0 to indicate the device is
266 	 * running.
267 	 */
268 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
269 					!(temp & STS_HALT), 1,
270 					CDNSP_MAX_HALT_USEC);
271 	if (ret) {
272 		pdev->cdnsp_state = CDNSP_STATE_DYING;
273 		dev_err(pdev->dev, "ERROR: Controller run failed\n");
274 	}
275 
276 	return ret;
277 }
278 
279 /*
280  * Reset a halted controller.
281  *
282  * This resets pipelines, timers, counters, state machines, etc.
283  * Transactions will be terminated immediately, and operational registers
284  * will be set to their defaults.
285  */
cdnsp_reset(struct cdnsp_device * pdev)286 int cdnsp_reset(struct cdnsp_device *pdev)
287 {
288 	u32 command;
289 	u32 temp;
290 	int ret;
291 
292 	temp = readl(&pdev->op_regs->status);
293 
294 	if (temp == ~(u32)0) {
295 		dev_err(pdev->dev, "Device not accessible, reset failed.\n");
296 		return -ENODEV;
297 	}
298 
299 	if ((temp & STS_HALT) == 0) {
300 		dev_err(pdev->dev, "Controller not halted, aborting reset.\n");
301 		return -EINVAL;
302 	}
303 
304 	command = readl(&pdev->op_regs->command);
305 	command |= CMD_RESET;
306 	writel(command, &pdev->op_regs->command);
307 
308 	ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp,
309 					!(temp & CMD_RESET), 1,
310 					10 * 1000);
311 	if (ret) {
312 		dev_err(pdev->dev, "ERROR: Controller reset failed\n");
313 		return ret;
314 	}
315 
316 	/*
317 	 * CDNSP cannot write any doorbells or operational registers other
318 	 * than status until the "Controller Not Ready" flag is cleared.
319 	 */
320 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
321 					!(temp & STS_CNR), 1,
322 					10 * 1000);
323 
324 	if (ret) {
325 		dev_err(pdev->dev, "ERROR: Controller not ready to work\n");
326 		return ret;
327 	}
328 
329 	dev_dbg(pdev->dev, "Controller ready to work");
330 
331 	return ret;
332 }
333 
334 /*
335  * cdnsp_get_endpoint_index - Find the index for an endpoint given its
336  * descriptor.Use the return value to right shift 1 for the bitmask.
337  *
338  * Index = (epnum * 2) + direction - 1,
339  * where direction = 0 for OUT, 1 for IN.
340  * For control endpoints, the IN index is used (OUT index is unused), so
341  * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
342  */
343 static unsigned int
cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor * desc)344 	cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc)
345 {
346 	unsigned int index = (unsigned int)usb_endpoint_num(desc);
347 
348 	if (usb_endpoint_xfer_control(desc))
349 		return index * 2;
350 
351 	return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
352 }
353 
354 /*
355  * Find the flag for this endpoint (for use in the control context). Use the
356  * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
357  * bit 1, etc.
358  */
359 static unsigned int
cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor * desc)360 	cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc)
361 {
362 	return 1 << (cdnsp_get_endpoint_index(desc) + 1);
363 }
364 
cdnsp_ep_enqueue(struct cdnsp_ep * pep,struct cdnsp_request * preq)365 int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
366 {
367 	struct cdnsp_device *pdev = pep->pdev;
368 	struct usb_request *request;
369 	int ret;
370 
371 	if (preq->epnum == 0 && !list_empty(&pep->pending_list)) {
372 		trace_cdnsp_request_enqueue_busy(preq);
373 		return -EBUSY;
374 	}
375 
376 	request = &preq->request;
377 	request->actual = 0;
378 	request->status = -EINPROGRESS;
379 	preq->direction = pep->direction;
380 	preq->epnum = pep->number;
381 	preq->td.drbl = 0;
382 
383 	ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction);
384 	if (ret) {
385 		trace_cdnsp_request_enqueue_error(preq);
386 		return ret;
387 	}
388 
389 	list_add_tail(&preq->list, &pep->pending_list);
390 
391 	trace_cdnsp_request_enqueue(preq);
392 
393 	switch (usb_endpoint_type(pep->endpoint.desc)) {
394 	case USB_ENDPOINT_XFER_CONTROL:
395 		ret = cdnsp_queue_ctrl_tx(pdev, preq);
396 		break;
397 	case USB_ENDPOINT_XFER_BULK:
398 	case USB_ENDPOINT_XFER_INT:
399 		ret = cdnsp_queue_bulk_tx(pdev, preq);
400 		break;
401 	case USB_ENDPOINT_XFER_ISOC:
402 		ret = cdnsp_queue_isoc_tx(pdev, preq);
403 	}
404 
405 	if (ret)
406 		goto unmap;
407 
408 	return 0;
409 
410 unmap:
411 	usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
412 					pep->direction);
413 	list_del(&preq->list);
414 	trace_cdnsp_request_enqueue_error(preq);
415 
416 	return ret;
417 }
418 
419 /*
420  * Remove the request's TD from the endpoint ring. This may cause the
421  * controller to stop USB transfers, potentially stopping in the middle of a
422  * TRB buffer. The controller should pick up where it left off in the TD,
423  * unless a Set Transfer Ring Dequeue Pointer is issued.
424  *
425  * The TRBs that make up the buffers for the canceled request will be "removed"
426  * from the ring. Since the ring is a contiguous structure, they can't be
427  * physically removed. Instead, there are two options:
428  *
429  *  1) If the controller is in the middle of processing the request to be
430  *     canceled, we simply move the ring's dequeue pointer past those TRBs
431  *     using the Set Transfer Ring Dequeue Pointer command. This will be
432  *     the common case, when drivers timeout on the last submitted request
433  *     and attempt to cancel.
434  *
435  *  2) If the controller is in the middle of a different TD, we turn the TRBs
436  *     into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained.
437  *     The controller will need to invalidate the any TRBs it has cached after
438  *     the stop endpoint command.
439  *
440  *  3) The TD may have completed by the time the Stop Endpoint Command
441  *     completes, so software needs to handle that case too.
442  *
443  */
cdnsp_ep_dequeue(struct cdnsp_ep * pep,struct cdnsp_request * preq)444 int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
445 {
446 	struct cdnsp_device *pdev = pep->pdev;
447 	int ret_stop = 0;
448 	int ret_rem;
449 
450 	trace_cdnsp_request_dequeue(preq);
451 
452 	if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING)
453 		ret_stop = cdnsp_cmd_stop_ep(pdev, pep);
454 
455 	ret_rem = cdnsp_remove_request(pdev, preq, pep);
456 
457 	return ret_rem ? ret_rem : ret_stop;
458 }
459 
cdnsp_zero_in_ctx(struct cdnsp_device * pdev)460 static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev)
461 {
462 	struct cdnsp_input_control_ctx *ctrl_ctx;
463 	struct cdnsp_slot_ctx *slot_ctx;
464 	struct cdnsp_ep_ctx *ep_ctx;
465 	int i;
466 
467 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
468 
469 	/*
470 	 * When a device's add flag and drop flag are zero, any subsequent
471 	 * configure endpoint command will leave that endpoint's state
472 	 * untouched. Make sure we don't leave any old state in the input
473 	 * endpoint contexts.
474 	 */
475 	ctrl_ctx->drop_flags = 0;
476 	ctrl_ctx->add_flags = 0;
477 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
478 	slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
479 
480 	/* Endpoint 0 is always valid */
481 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
482 	for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) {
483 		ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i);
484 		ep_ctx->ep_info = 0;
485 		ep_ctx->ep_info2 = 0;
486 		ep_ctx->deq = 0;
487 		ep_ctx->tx_info = 0;
488 	}
489 }
490 
491 /* Issue a configure endpoint command and wait for it to finish. */
cdnsp_configure_endpoint(struct cdnsp_device * pdev)492 static int cdnsp_configure_endpoint(struct cdnsp_device *pdev)
493 {
494 	int ret;
495 
496 	cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma);
497 	cdnsp_ring_cmd_db(pdev);
498 	ret = cdnsp_wait_for_cmd_compl(pdev);
499 	if (ret) {
500 		dev_err(pdev->dev,
501 			"ERR: unexpected command completion code 0x%x.\n", ret);
502 		return -EINVAL;
503 	}
504 
505 	return ret;
506 }
507 
cdnsp_invalidate_ep_events(struct cdnsp_device * pdev,struct cdnsp_ep * pep)508 static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev,
509 				       struct cdnsp_ep *pep)
510 {
511 	struct cdnsp_segment *segment;
512 	union cdnsp_trb *event;
513 	u32 cycle_state;
514 	u32  data;
515 
516 	event = pdev->event_ring->dequeue;
517 	segment = pdev->event_ring->deq_seg;
518 	cycle_state = pdev->event_ring->cycle_state;
519 
520 	while (1) {
521 		data = le32_to_cpu(event->trans_event.flags);
522 
523 		/* Check the owner of the TRB. */
524 		if ((data & TRB_CYCLE) != cycle_state)
525 			break;
526 
527 		if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER &&
528 		    TRB_TO_EP_ID(data) == (pep->idx + 1)) {
529 			data |= TRB_EVENT_INVALIDATE;
530 			event->trans_event.flags = cpu_to_le32(data);
531 		}
532 
533 		if (cdnsp_last_trb_on_seg(segment, event)) {
534 			cycle_state ^= 1;
535 			segment = pdev->event_ring->deq_seg->next;
536 			event = segment->trbs;
537 		} else {
538 			event++;
539 		}
540 	}
541 }
542 
cdnsp_wait_for_cmd_compl(struct cdnsp_device * pdev)543 int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev)
544 {
545 	struct cdnsp_segment *event_deq_seg;
546 	union cdnsp_trb *cmd_trb;
547 	dma_addr_t cmd_deq_dma;
548 	union cdnsp_trb *event;
549 	u32 cycle_state;
550 	u32 retry = 10;
551 	int ret, val;
552 	u64 cmd_dma;
553 	u32  flags;
554 
555 	cmd_trb = pdev->cmd.command_trb;
556 	pdev->cmd.status = 0;
557 
558 	trace_cdnsp_cmd_wait_for_compl(pdev->cmd_ring, &cmd_trb->generic);
559 
560 	ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val,
561 					!CMD_RING_BUSY(val), 1,
562 					CDNSP_CMD_TIMEOUT);
563 	if (ret) {
564 		dev_err(pdev->dev, "ERR: Timeout while waiting for command\n");
565 		trace_cdnsp_cmd_timeout(pdev->cmd_ring, &cmd_trb->generic);
566 		pdev->cdnsp_state = CDNSP_STATE_DYING;
567 		return -ETIMEDOUT;
568 	}
569 
570 	event = pdev->event_ring->dequeue;
571 	event_deq_seg = pdev->event_ring->deq_seg;
572 	cycle_state = pdev->event_ring->cycle_state;
573 
574 	cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb);
575 	if (!cmd_deq_dma)
576 		return -EINVAL;
577 
578 	while (1) {
579 		flags = le32_to_cpu(event->event_cmd.flags);
580 
581 		/* Check the owner of the TRB. */
582 		if ((flags & TRB_CYCLE) != cycle_state) {
583 			/*
584 			 * Give some extra time to get chance controller
585 			 * to finish command before returning error code.
586 			 * Checking CMD_RING_BUSY is not sufficient because
587 			 * this bit is cleared to '0' when the Command
588 			 * Descriptor has been executed by controller
589 			 * and not when command completion event has
590 			 * be added to event ring.
591 			 */
592 			if (retry--) {
593 				udelay(20);
594 				continue;
595 			}
596 
597 			return -EINVAL;
598 		}
599 
600 		cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb);
601 
602 		/*
603 		 * Check whether the completion event is for last queued
604 		 * command.
605 		 */
606 		if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION ||
607 		    cmd_dma != (u64)cmd_deq_dma) {
608 			if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
609 				event++;
610 				continue;
611 			}
612 
613 			if (cdnsp_last_trb_on_ring(pdev->event_ring,
614 						   event_deq_seg, event))
615 				cycle_state ^= 1;
616 
617 			event_deq_seg = event_deq_seg->next;
618 			event = event_deq_seg->trbs;
619 			continue;
620 		}
621 
622 		trace_cdnsp_handle_command(pdev->cmd_ring, &cmd_trb->generic);
623 
624 		pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
625 		if (pdev->cmd.status == COMP_SUCCESS)
626 			return 0;
627 
628 		return -pdev->cmd.status;
629 	}
630 }
631 
cdnsp_halt_endpoint(struct cdnsp_device * pdev,struct cdnsp_ep * pep,int value)632 int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
633 			struct cdnsp_ep *pep,
634 			int value)
635 {
636 	int ret;
637 
638 	trace_cdnsp_ep_halt(value ? "Set" : "Clear");
639 
640 	ret = cdnsp_cmd_stop_ep(pdev, pep);
641 	if (ret)
642 		return ret;
643 
644 	if (value) {
645 		if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) {
646 			cdnsp_queue_halt_endpoint(pdev, pep->idx);
647 			cdnsp_ring_cmd_db(pdev);
648 			ret = cdnsp_wait_for_cmd_compl(pdev);
649 		}
650 
651 		pep->ep_state |= EP_HALTED;
652 	} else {
653 		cdnsp_queue_reset_ep(pdev, pep->idx);
654 		cdnsp_ring_cmd_db(pdev);
655 		ret = cdnsp_wait_for_cmd_compl(pdev);
656 		trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
657 
658 		if (ret)
659 			return ret;
660 
661 		pep->ep_state &= ~EP_HALTED;
662 
663 		if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
664 			cdnsp_ring_doorbell_for_active_rings(pdev, pep);
665 
666 		pep->ep_state &= ~EP_WEDGE;
667 	}
668 
669 	return 0;
670 }
671 
cdnsp_update_eps_configuration(struct cdnsp_device * pdev,struct cdnsp_ep * pep)672 static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
673 					  struct cdnsp_ep *pep)
674 {
675 	struct cdnsp_input_control_ctx *ctrl_ctx;
676 	struct cdnsp_slot_ctx *slot_ctx;
677 	int ret = 0;
678 	u32 ep_sts;
679 	int i;
680 
681 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
682 
683 	/* Don't issue the command if there's no endpoints to update. */
684 	if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
685 		return 0;
686 
687 	ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
688 	ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
689 	ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
690 
691 	/* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
692 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
693 	for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
694 		__le32 le32 = cpu_to_le32(BIT(i));
695 
696 		if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
697 		    (ctrl_ctx->add_flags & le32) || i == 1) {
698 			slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
699 			slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
700 			break;
701 		}
702 	}
703 
704 	ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
705 
706 	if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
707 	     ep_sts == EP_STATE_DISABLED) ||
708 	    (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
709 		ret = cdnsp_configure_endpoint(pdev);
710 
711 	trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
712 	trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
713 
714 	cdnsp_zero_in_ctx(pdev);
715 
716 	return ret;
717 }
718 
719 /*
720  * This submits a Reset Device Command, which will set the device state to 0,
721  * set the device address to 0, and disable all the endpoints except the default
722  * control endpoint. The USB core should come back and call
723  * cdnsp_setup_device(), and then re-set up the configuration.
724  */
cdnsp_reset_device(struct cdnsp_device * pdev)725 int cdnsp_reset_device(struct cdnsp_device *pdev)
726 {
727 	struct cdnsp_slot_ctx *slot_ctx;
728 	int slot_state;
729 	int ret, i;
730 
731 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
732 	slot_ctx->dev_info = 0;
733 	pdev->device_address = 0;
734 
735 	/* If device is not setup, there is no point in resetting it. */
736 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
737 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
738 	trace_cdnsp_reset_device(slot_ctx);
739 
740 	if (slot_state <= SLOT_STATE_DEFAULT &&
741 	    pdev->eps[0].ep_state & EP_HALTED) {
742 		cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
743 	}
744 
745 	/*
746 	 * During Reset Device command controller shall transition the
747 	 * endpoint ep0 to the Running State.
748 	 */
749 	pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
750 	pdev->eps[0].ep_state |= EP_ENABLED;
751 
752 	if (slot_state <= SLOT_STATE_DEFAULT)
753 		return 0;
754 
755 	cdnsp_queue_reset_device(pdev);
756 	cdnsp_ring_cmd_db(pdev);
757 	ret = cdnsp_wait_for_cmd_compl(pdev);
758 
759 	/*
760 	 * After Reset Device command all not default endpoints
761 	 * are in Disabled state.
762 	 */
763 	for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
764 		pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
765 
766 	trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
767 
768 	if (ret)
769 		dev_err(pdev->dev, "Reset device failed with error code %d",
770 			ret);
771 
772 	return ret;
773 }
774 
775 /*
776  * Sets the MaxPStreams field and the Linear Stream Array field.
777  * Sets the dequeue pointer to the stream context array.
778  */
cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device * pdev,struct cdnsp_ep_ctx * ep_ctx,struct cdnsp_stream_info * stream_info)779 static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
780 					     struct cdnsp_ep_ctx *ep_ctx,
781 					     struct cdnsp_stream_info *stream_info)
782 {
783 	u32 max_primary_streams;
784 
785 	/* MaxPStreams is the number of stream context array entries, not the
786 	 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
787 	 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
788 	 */
789 	max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
790 	ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
791 	ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
792 				       | EP_HAS_LSA);
793 	ep_ctx->deq  = cpu_to_le64(stream_info->ctx_array_dma);
794 }
795 
796 /*
797  * The drivers use this function to prepare a bulk endpoints to use streams.
798  *
799  * Don't allow the call to succeed if endpoint only supports one stream
800  * (which means it doesn't support streams at all).
801  */
cdnsp_alloc_streams(struct cdnsp_device * pdev,struct cdnsp_ep * pep)802 int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
803 {
804 	unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
805 	unsigned int num_stream_ctxs;
806 	int ret;
807 
808 	if (num_streams ==  0)
809 		return 0;
810 
811 	if (num_streams > STREAM_NUM_STREAMS)
812 		return -EINVAL;
813 
814 	/*
815 	 * Add two to the number of streams requested to account for
816 	 * stream 0 that is reserved for controller usage and one additional
817 	 * for TASK SET FULL response.
818 	 */
819 	num_streams += 2;
820 
821 	/* The stream context array size must be a power of two */
822 	num_stream_ctxs = roundup_pow_of_two(num_streams);
823 
824 	trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
825 
826 	ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
827 	if (ret)
828 		return ret;
829 
830 	cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
831 
832 	pep->ep_state |= EP_HAS_STREAMS;
833 	pep->stream_info.td_count = 0;
834 	pep->stream_info.first_prime_det = 0;
835 
836 	/* Subtract 1 for stream 0, which drivers can't use. */
837 	return num_streams - 1;
838 }
839 
cdnsp_disable_slot(struct cdnsp_device * pdev)840 int cdnsp_disable_slot(struct cdnsp_device *pdev)
841 {
842 	int ret;
843 
844 	cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
845 	cdnsp_ring_cmd_db(pdev);
846 	ret = cdnsp_wait_for_cmd_compl(pdev);
847 
848 	pdev->slot_id = 0;
849 	pdev->active_port = NULL;
850 
851 	trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
852 
853 	memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
854 	memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
855 
856 	return ret;
857 }
858 
cdnsp_enable_slot(struct cdnsp_device * pdev)859 int cdnsp_enable_slot(struct cdnsp_device *pdev)
860 {
861 	struct cdnsp_slot_ctx *slot_ctx;
862 	int slot_state;
863 	int ret;
864 
865 	/* If device is not setup, there is no point in resetting it */
866 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
867 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
868 
869 	if (slot_state != SLOT_STATE_DISABLED)
870 		return 0;
871 
872 	cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
873 	cdnsp_ring_cmd_db(pdev);
874 	ret = cdnsp_wait_for_cmd_compl(pdev);
875 	if (ret)
876 		goto show_trace;
877 
878 	pdev->slot_id = 1;
879 
880 show_trace:
881 	trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
882 
883 	return ret;
884 }
885 
886 /*
887  * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
888  * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
889  */
cdnsp_setup_device(struct cdnsp_device * pdev,enum cdnsp_setup_dev setup)890 int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
891 {
892 	struct cdnsp_input_control_ctx *ctrl_ctx;
893 	struct cdnsp_slot_ctx *slot_ctx;
894 	int dev_state = 0;
895 	int ret;
896 
897 	if (!pdev->slot_id) {
898 		trace_cdnsp_slot_id("incorrect");
899 		return -EINVAL;
900 	}
901 
902 	if (!pdev->active_port->port_num)
903 		return -EINVAL;
904 
905 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
906 	dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
907 
908 	if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
909 		trace_cdnsp_slot_already_in_default(slot_ctx);
910 		return 0;
911 	}
912 
913 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
914 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
915 
916 	if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
917 		ret = cdnsp_setup_addressable_priv_dev(pdev);
918 		if (ret)
919 			return ret;
920 	}
921 
922 	cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
923 
924 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
925 	ctrl_ctx->drop_flags = 0;
926 
927 	trace_cdnsp_setup_device_slot(slot_ctx);
928 
929 	cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
930 	cdnsp_ring_cmd_db(pdev);
931 	ret = cdnsp_wait_for_cmd_compl(pdev);
932 
933 	trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
934 
935 	/* Zero the input context control for later use. */
936 	ctrl_ctx->add_flags = 0;
937 	ctrl_ctx->drop_flags = 0;
938 
939 	return ret;
940 }
941 
cdnsp_set_usb2_hardware_lpm(struct cdnsp_device * pdev,struct usb_request * req,int enable)942 void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
943 				 struct usb_request *req,
944 				 int enable)
945 {
946 	if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
947 		return;
948 
949 	trace_cdnsp_lpm(enable);
950 
951 	if (enable)
952 		writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
953 		       &pdev->active_port->regs->portpmsc);
954 	else
955 		writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
956 }
957 
cdnsp_get_frame(struct cdnsp_device * pdev)958 static int cdnsp_get_frame(struct cdnsp_device *pdev)
959 {
960 	return readl(&pdev->run_regs->microframe_index) >> 3;
961 }
962 
cdnsp_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)963 static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
964 				  const struct usb_endpoint_descriptor *desc)
965 {
966 	struct cdnsp_input_control_ctx *ctrl_ctx;
967 	struct cdnsp_device *pdev;
968 	struct cdnsp_ep *pep;
969 	unsigned long flags;
970 	u32 added_ctxs;
971 	int ret;
972 
973 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
974 	    !desc->wMaxPacketSize)
975 		return -EINVAL;
976 
977 	pep = to_cdnsp_ep(ep);
978 	pdev = pep->pdev;
979 	pep->ep_state &= ~EP_UNCONFIGURED;
980 
981 	if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
982 			  "%s is already enabled\n", pep->name))
983 		return 0;
984 
985 	spin_lock_irqsave(&pdev->lock, flags);
986 
987 	added_ctxs = cdnsp_get_endpoint_flag(desc);
988 	if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
989 		dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
990 		ret = -EINVAL;
991 		goto unlock;
992 	}
993 
994 	pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
995 
996 	if (pdev->gadget.speed == USB_SPEED_FULL) {
997 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
998 			pep->interval = desc->bInterval << 3;
999 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
1000 			pep->interval = BIT(desc->bInterval - 1) << 3;
1001 	}
1002 
1003 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
1004 		if (pep->interval > BIT(12)) {
1005 			dev_err(pdev->dev, "bInterval %d not supported\n",
1006 				desc->bInterval);
1007 			ret = -EINVAL;
1008 			goto unlock;
1009 		}
1010 		cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1011 	}
1012 
1013 	ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
1014 	if (ret)
1015 		goto unlock;
1016 
1017 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1018 	ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
1019 	ctrl_ctx->drop_flags = 0;
1020 
1021 	ret = cdnsp_update_eps_configuration(pdev, pep);
1022 	if (ret) {
1023 		cdnsp_free_endpoint_rings(pdev, pep);
1024 		goto unlock;
1025 	}
1026 
1027 	pep->ep_state |= EP_ENABLED;
1028 	pep->ep_state &= ~EP_STOPPED;
1029 
1030 unlock:
1031 	trace_cdnsp_ep_enable_end(pep, 0);
1032 	spin_unlock_irqrestore(&pdev->lock, flags);
1033 
1034 	return ret;
1035 }
1036 
cdnsp_gadget_ep_disable(struct usb_ep * ep)1037 static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
1038 {
1039 	struct cdnsp_input_control_ctx *ctrl_ctx;
1040 	struct cdnsp_request *preq;
1041 	struct cdnsp_device *pdev;
1042 	struct cdnsp_ep *pep;
1043 	unsigned long flags;
1044 	u32 drop_flag;
1045 	int ret = 0;
1046 
1047 	if (!ep)
1048 		return -EINVAL;
1049 
1050 	pep = to_cdnsp_ep(ep);
1051 	pdev = pep->pdev;
1052 
1053 	spin_lock_irqsave(&pdev->lock, flags);
1054 
1055 	if (!(pep->ep_state & EP_ENABLED)) {
1056 		dev_err(pdev->dev, "%s is already disabled\n", pep->name);
1057 		ret = -EINVAL;
1058 		goto finish;
1059 	}
1060 
1061 	pep->ep_state |= EP_DIS_IN_RROGRESS;
1062 
1063 	/* Endpoint was unconfigured by Reset Device command. */
1064 	if (!(pep->ep_state & EP_UNCONFIGURED))
1065 		cdnsp_cmd_stop_ep(pdev, pep);
1066 
1067 	/* Remove all queued USB requests. */
1068 	while (!list_empty(&pep->pending_list)) {
1069 		preq = next_request(&pep->pending_list);
1070 		cdnsp_ep_dequeue(pep, preq);
1071 	}
1072 
1073 	cdnsp_invalidate_ep_events(pdev, pep);
1074 
1075 	pep->ep_state &= ~EP_DIS_IN_RROGRESS;
1076 	drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
1077 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1078 	ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
1079 	ctrl_ctx->add_flags = 0;
1080 
1081 	cdnsp_endpoint_zero(pdev, pep);
1082 
1083 	if (!(pep->ep_state & EP_UNCONFIGURED))
1084 		ret = cdnsp_update_eps_configuration(pdev, pep);
1085 
1086 	cdnsp_free_endpoint_rings(pdev, pep);
1087 
1088 	pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
1089 	pep->ep_state |= EP_STOPPED;
1090 
1091 finish:
1092 	trace_cdnsp_ep_disable_end(pep, 0);
1093 	spin_unlock_irqrestore(&pdev->lock, flags);
1094 
1095 	return ret;
1096 }
1097 
cdnsp_gadget_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1098 static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
1099 							 gfp_t gfp_flags)
1100 {
1101 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1102 	struct cdnsp_request *preq;
1103 
1104 	preq = kzalloc(sizeof(*preq), gfp_flags);
1105 	if (!preq)
1106 		return NULL;
1107 
1108 	preq->epnum = pep->number;
1109 	preq->pep = pep;
1110 
1111 	trace_cdnsp_alloc_request(preq);
1112 
1113 	return &preq->request;
1114 }
1115 
cdnsp_gadget_ep_free_request(struct usb_ep * ep,struct usb_request * request)1116 static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
1117 					 struct usb_request *request)
1118 {
1119 	struct cdnsp_request *preq = to_cdnsp_request(request);
1120 
1121 	trace_cdnsp_free_request(preq);
1122 	kfree(preq);
1123 }
1124 
cdnsp_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)1125 static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
1126 				 struct usb_request *request,
1127 				 gfp_t gfp_flags)
1128 {
1129 	struct cdnsp_request *preq;
1130 	struct cdnsp_device *pdev;
1131 	struct cdnsp_ep *pep;
1132 	unsigned long flags;
1133 	int ret;
1134 
1135 	if (!request || !ep)
1136 		return -EINVAL;
1137 
1138 	pep = to_cdnsp_ep(ep);
1139 	pdev = pep->pdev;
1140 
1141 	if (!(pep->ep_state & EP_ENABLED)) {
1142 		dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
1143 			pep->name);
1144 		return -EINVAL;
1145 	}
1146 
1147 	preq = to_cdnsp_request(request);
1148 	spin_lock_irqsave(&pdev->lock, flags);
1149 	ret = cdnsp_ep_enqueue(pep, preq);
1150 	spin_unlock_irqrestore(&pdev->lock, flags);
1151 
1152 	return ret;
1153 }
1154 
cdnsp_gadget_ep_dequeue(struct usb_ep * ep,struct usb_request * request)1155 static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
1156 				   struct usb_request *request)
1157 {
1158 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1159 	struct cdnsp_device *pdev = pep->pdev;
1160 	unsigned long flags;
1161 	int ret;
1162 
1163 	if (request->status != -EINPROGRESS)
1164 		return 0;
1165 
1166 	if (!pep->endpoint.desc) {
1167 		dev_err(pdev->dev,
1168 			"%s: can't dequeue to disabled endpoint\n",
1169 			pep->name);
1170 		return -ESHUTDOWN;
1171 	}
1172 
1173 	/* Requests has been dequeued during disabling endpoint. */
1174 	if (!(pep->ep_state & EP_ENABLED))
1175 		return 0;
1176 
1177 	spin_lock_irqsave(&pdev->lock, flags);
1178 	ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
1179 	spin_unlock_irqrestore(&pdev->lock, flags);
1180 
1181 	return ret;
1182 }
1183 
cdnsp_gadget_ep_set_halt(struct usb_ep * ep,int value)1184 static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
1185 {
1186 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1187 	struct cdnsp_device *pdev = pep->pdev;
1188 	struct cdnsp_request *preq;
1189 	unsigned long flags;
1190 	int ret;
1191 
1192 	spin_lock_irqsave(&pdev->lock, flags);
1193 
1194 	preq = next_request(&pep->pending_list);
1195 	if (value) {
1196 		if (preq) {
1197 			trace_cdnsp_ep_busy_try_halt_again(pep, 0);
1198 			ret = -EAGAIN;
1199 			goto done;
1200 		}
1201 	}
1202 
1203 	ret = cdnsp_halt_endpoint(pdev, pep, value);
1204 
1205 done:
1206 	spin_unlock_irqrestore(&pdev->lock, flags);
1207 	return ret;
1208 }
1209 
cdnsp_gadget_ep_set_wedge(struct usb_ep * ep)1210 static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
1211 {
1212 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1213 	struct cdnsp_device *pdev = pep->pdev;
1214 	unsigned long flags;
1215 	int ret;
1216 
1217 	spin_lock_irqsave(&pdev->lock, flags);
1218 	pep->ep_state |= EP_WEDGE;
1219 	ret = cdnsp_halt_endpoint(pdev, pep, 1);
1220 	spin_unlock_irqrestore(&pdev->lock, flags);
1221 
1222 	return ret;
1223 }
1224 
1225 static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
1226 	.enable		= cdnsp_gadget_ep_enable,
1227 	.disable	= cdnsp_gadget_ep_disable,
1228 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
1229 	.free_request	= cdnsp_gadget_ep_free_request,
1230 	.queue		= cdnsp_gadget_ep_queue,
1231 	.dequeue	= cdnsp_gadget_ep_dequeue,
1232 	.set_halt	= cdnsp_gadget_ep_set_halt,
1233 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
1234 };
1235 
1236 static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
1237 	.enable		= cdnsp_gadget_ep_enable,
1238 	.disable	= cdnsp_gadget_ep_disable,
1239 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
1240 	.free_request	= cdnsp_gadget_ep_free_request,
1241 	.queue		= cdnsp_gadget_ep_queue,
1242 	.dequeue	= cdnsp_gadget_ep_dequeue,
1243 	.set_halt	= cdnsp_gadget_ep_set_halt,
1244 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
1245 };
1246 
cdnsp_gadget_giveback(struct cdnsp_ep * pep,struct cdnsp_request * preq,int status)1247 void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
1248 			   struct cdnsp_request *preq,
1249 			   int status)
1250 {
1251 	struct cdnsp_device *pdev = pep->pdev;
1252 
1253 	list_del(&preq->list);
1254 
1255 	if (preq->request.status == -EINPROGRESS)
1256 		preq->request.status = status;
1257 
1258 	usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
1259 					preq->direction);
1260 
1261 	trace_cdnsp_request_giveback(preq);
1262 
1263 	if (preq != &pdev->ep0_preq) {
1264 		spin_unlock(&pdev->lock);
1265 		usb_gadget_giveback_request(&pep->endpoint, &preq->request);
1266 		spin_lock(&pdev->lock);
1267 	}
1268 }
1269 
1270 static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
1271 	.bLength =		USB_DT_ENDPOINT_SIZE,
1272 	.bDescriptorType =	USB_DT_ENDPOINT,
1273 	.bmAttributes =		USB_ENDPOINT_XFER_CONTROL,
1274 };
1275 
cdnsp_run(struct cdnsp_device * pdev,enum usb_device_speed speed)1276 static int cdnsp_run(struct cdnsp_device *pdev,
1277 		     enum usb_device_speed speed)
1278 {
1279 	u32 fs_speed = 0;
1280 	u32 temp;
1281 	int ret;
1282 
1283 	temp = readl(&pdev->ir_set->irq_control);
1284 	temp &= ~IMOD_INTERVAL_MASK;
1285 	temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
1286 	writel(temp, &pdev->ir_set->irq_control);
1287 
1288 	temp = readl(&pdev->port3x_regs->mode_addr);
1289 
1290 	switch (speed) {
1291 	case USB_SPEED_SUPER_PLUS:
1292 		temp |= CFG_3XPORT_SSP_SUPPORT;
1293 		break;
1294 	case USB_SPEED_SUPER:
1295 		temp &= ~CFG_3XPORT_SSP_SUPPORT;
1296 		break;
1297 	case USB_SPEED_HIGH:
1298 		break;
1299 	case USB_SPEED_FULL:
1300 		fs_speed = PORT_REG6_FORCE_FS;
1301 		break;
1302 	default:
1303 		dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
1304 			speed);
1305 		fallthrough;
1306 	case USB_SPEED_UNKNOWN:
1307 		/* Default to superspeed. */
1308 		speed = USB_SPEED_SUPER;
1309 		break;
1310 	}
1311 
1312 	if (speed >= USB_SPEED_SUPER) {
1313 		writel(temp, &pdev->port3x_regs->mode_addr);
1314 		cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
1315 				     XDEV_RXDETECT);
1316 	} else {
1317 		cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1318 	}
1319 
1320 	cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
1321 			     XDEV_RXDETECT);
1322 
1323 	cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1324 
1325 	writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
1326 
1327 	ret = cdnsp_start(pdev);
1328 	if (ret) {
1329 		ret = -ENODEV;
1330 		goto err;
1331 	}
1332 
1333 	temp = readl(&pdev->op_regs->command);
1334 	temp |= (CMD_INTE);
1335 	writel(temp, &pdev->op_regs->command);
1336 
1337 	temp = readl(&pdev->ir_set->irq_pending);
1338 	writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
1339 
1340 	trace_cdnsp_init("Controller ready to work");
1341 	return 0;
1342 err:
1343 	cdnsp_halt(pdev);
1344 	return ret;
1345 }
1346 
cdnsp_gadget_udc_start(struct usb_gadget * g,struct usb_gadget_driver * driver)1347 static int cdnsp_gadget_udc_start(struct usb_gadget *g,
1348 				  struct usb_gadget_driver *driver)
1349 {
1350 	enum usb_device_speed max_speed = driver->max_speed;
1351 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1352 	unsigned long flags;
1353 	int ret;
1354 
1355 	spin_lock_irqsave(&pdev->lock, flags);
1356 	pdev->gadget_driver = driver;
1357 
1358 	/* limit speed if necessary */
1359 	max_speed = min(driver->max_speed, g->max_speed);
1360 	ret = cdnsp_run(pdev, max_speed);
1361 
1362 	spin_unlock_irqrestore(&pdev->lock, flags);
1363 
1364 	return ret;
1365 }
1366 
1367 /*
1368  * Update Event Ring Dequeue Pointer:
1369  * - When all events have finished
1370  * - To avoid "Event Ring Full Error" condition
1371  */
cdnsp_update_erst_dequeue(struct cdnsp_device * pdev,union cdnsp_trb * event_ring_deq,u8 clear_ehb)1372 void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
1373 			       union cdnsp_trb *event_ring_deq,
1374 			       u8 clear_ehb)
1375 {
1376 	u64 temp_64;
1377 	dma_addr_t deq;
1378 
1379 	temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
1380 
1381 	/* If necessary, update the HW's version of the event ring deq ptr. */
1382 	if (event_ring_deq != pdev->event_ring->dequeue) {
1383 		deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
1384 					    pdev->event_ring->dequeue);
1385 		temp_64 &= ERST_PTR_MASK;
1386 		temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
1387 	}
1388 
1389 	/* Clear the event handler busy flag (RW1C). */
1390 	if (clear_ehb)
1391 		temp_64 |= ERST_EHB;
1392 	else
1393 		temp_64 &= ~ERST_EHB;
1394 
1395 	cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
1396 }
1397 
cdnsp_clear_cmd_ring(struct cdnsp_device * pdev)1398 static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
1399 {
1400 	struct cdnsp_segment *seg;
1401 	u64 val_64;
1402 	int i;
1403 
1404 	cdnsp_initialize_ring_info(pdev->cmd_ring);
1405 
1406 	seg = pdev->cmd_ring->first_seg;
1407 	for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
1408 		memset(seg->trbs, 0,
1409 		       sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
1410 		seg = seg->next;
1411 	}
1412 
1413 	/* Set the address in the Command Ring Control register. */
1414 	val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
1415 	val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
1416 		 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
1417 		 pdev->cmd_ring->cycle_state;
1418 	cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
1419 }
1420 
cdnsp_consume_all_events(struct cdnsp_device * pdev)1421 static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
1422 {
1423 	struct cdnsp_segment *event_deq_seg;
1424 	union cdnsp_trb *event_ring_deq;
1425 	union cdnsp_trb *event;
1426 	u32 cycle_bit;
1427 
1428 	event_ring_deq = pdev->event_ring->dequeue;
1429 	event_deq_seg = pdev->event_ring->deq_seg;
1430 	event = pdev->event_ring->dequeue;
1431 
1432 	/* Update ring dequeue pointer. */
1433 	while (1) {
1434 		cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
1435 
1436 		/* Does the controller or driver own the TRB? */
1437 		if (cycle_bit != pdev->event_ring->cycle_state)
1438 			break;
1439 
1440 		cdnsp_inc_deq(pdev, pdev->event_ring);
1441 
1442 		if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
1443 			event++;
1444 			continue;
1445 		}
1446 
1447 		if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
1448 					   event))
1449 			cycle_bit ^= 1;
1450 
1451 		event_deq_seg = event_deq_seg->next;
1452 		event = event_deq_seg->trbs;
1453 	}
1454 
1455 	cdnsp_update_erst_dequeue(pdev,  event_ring_deq, 1);
1456 }
1457 
cdnsp_stop(struct cdnsp_device * pdev)1458 static void cdnsp_stop(struct cdnsp_device *pdev)
1459 {
1460 	u32 temp;
1461 
1462 	/* Remove internally queued request for ep0. */
1463 	if (!list_empty(&pdev->eps[0].pending_list)) {
1464 		struct cdnsp_request *req;
1465 
1466 		req = next_request(&pdev->eps[0].pending_list);
1467 		if (req == &pdev->ep0_preq)
1468 			cdnsp_ep_dequeue(&pdev->eps[0], req);
1469 	}
1470 
1471 	cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
1472 	cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1473 	cdnsp_disable_slot(pdev);
1474 	cdnsp_halt(pdev);
1475 
1476 	temp = readl(&pdev->op_regs->status);
1477 	writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
1478 	temp = readl(&pdev->ir_set->irq_pending);
1479 	writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
1480 
1481 	cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
1482 	cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
1483 
1484 	/* Clear interrupt line */
1485 	temp = readl(&pdev->ir_set->irq_pending);
1486 	temp |= IMAN_IP;
1487 	writel(temp, &pdev->ir_set->irq_pending);
1488 
1489 	cdnsp_consume_all_events(pdev);
1490 	cdnsp_clear_cmd_ring(pdev);
1491 
1492 	trace_cdnsp_exit("Controller stopped.");
1493 }
1494 
1495 /*
1496  * Stop controller.
1497  * This function is called by the gadget core when the driver is removed.
1498  * Disable slot, disable IRQs, and quiesce the controller.
1499  */
cdnsp_gadget_udc_stop(struct usb_gadget * g)1500 static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
1501 {
1502 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1503 	unsigned long flags;
1504 
1505 	spin_lock_irqsave(&pdev->lock, flags);
1506 	cdnsp_stop(pdev);
1507 	pdev->gadget_driver = NULL;
1508 	spin_unlock_irqrestore(&pdev->lock, flags);
1509 
1510 	return 0;
1511 }
1512 
cdnsp_gadget_get_frame(struct usb_gadget * g)1513 static int cdnsp_gadget_get_frame(struct usb_gadget *g)
1514 {
1515 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1516 
1517 	return cdnsp_get_frame(pdev);
1518 }
1519 
__cdnsp_gadget_wakeup(struct cdnsp_device * pdev)1520 static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
1521 {
1522 	struct cdnsp_port_regs __iomem *port_regs;
1523 	u32 portpm, portsc;
1524 
1525 	port_regs = pdev->active_port->regs;
1526 	portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
1527 
1528 	/* Remote wakeup feature is not enabled by host. */
1529 	if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
1530 		portpm = readl(&port_regs->portpmsc);
1531 
1532 		if (!(portpm & PORT_RWE))
1533 			return;
1534 	}
1535 
1536 	if (portsc == XDEV_U3 && !pdev->may_wakeup)
1537 		return;
1538 
1539 	cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
1540 
1541 	pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
1542 }
1543 
cdnsp_gadget_wakeup(struct usb_gadget * g)1544 static int cdnsp_gadget_wakeup(struct usb_gadget *g)
1545 {
1546 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1547 	unsigned long flags;
1548 
1549 	spin_lock_irqsave(&pdev->lock, flags);
1550 	__cdnsp_gadget_wakeup(pdev);
1551 	spin_unlock_irqrestore(&pdev->lock, flags);
1552 
1553 	return 0;
1554 }
1555 
cdnsp_gadget_set_selfpowered(struct usb_gadget * g,int is_selfpowered)1556 static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
1557 					int is_selfpowered)
1558 {
1559 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1560 	unsigned long flags;
1561 
1562 	spin_lock_irqsave(&pdev->lock, flags);
1563 	g->is_selfpowered = !!is_selfpowered;
1564 	spin_unlock_irqrestore(&pdev->lock, flags);
1565 
1566 	return 0;
1567 }
1568 
cdnsp_gadget_pullup(struct usb_gadget * gadget,int is_on)1569 static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
1570 {
1571 	struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
1572 	struct cdns *cdns = dev_get_drvdata(pdev->dev);
1573 	unsigned long flags;
1574 
1575 	trace_cdnsp_pullup(is_on);
1576 
1577 	/*
1578 	 * Disable events handling while controller is being
1579 	 * enabled/disabled.
1580 	 */
1581 	disable_irq(cdns->dev_irq);
1582 	spin_lock_irqsave(&pdev->lock, flags);
1583 
1584 	if (!is_on) {
1585 		cdnsp_reset_device(pdev);
1586 		cdns_clear_vbus(cdns);
1587 	} else {
1588 		cdns_set_vbus(cdns);
1589 	}
1590 
1591 	spin_unlock_irqrestore(&pdev->lock, flags);
1592 	enable_irq(cdns->dev_irq);
1593 
1594 	return 0;
1595 }
1596 
1597 static const struct usb_gadget_ops cdnsp_gadget_ops = {
1598 	.get_frame		= cdnsp_gadget_get_frame,
1599 	.wakeup			= cdnsp_gadget_wakeup,
1600 	.set_selfpowered	= cdnsp_gadget_set_selfpowered,
1601 	.pullup			= cdnsp_gadget_pullup,
1602 	.udc_start		= cdnsp_gadget_udc_start,
1603 	.udc_stop		= cdnsp_gadget_udc_stop,
1604 };
1605 
cdnsp_get_ep_buffering(struct cdnsp_device * pdev,struct cdnsp_ep * pep)1606 static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
1607 				   struct cdnsp_ep *pep)
1608 {
1609 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
1610 	int endpoints;
1611 
1612 	reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
1613 
1614 	if (!pep->direction) {
1615 		pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
1616 		pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
1617 		pep->buffering = (pep->buffering + 1) / 2;
1618 		pep->buffering_period = (pep->buffering_period + 1) / 2;
1619 		return;
1620 	}
1621 
1622 	endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
1623 
1624 	/* Set to XBUF_TX_TAG_MASK_0 register. */
1625 	reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
1626 	/* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
1627 	reg += pep->number * sizeof(u32) * 2;
1628 
1629 	pep->buffering = (readl(reg) + 1) / 2;
1630 	pep->buffering_period = pep->buffering;
1631 }
1632 
cdnsp_gadget_init_endpoints(struct cdnsp_device * pdev)1633 static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
1634 {
1635 	int max_streams = HCC_MAX_PSA(pdev->hcc_params);
1636 	struct cdnsp_ep *pep;
1637 	int i;
1638 
1639 	INIT_LIST_HEAD(&pdev->gadget.ep_list);
1640 
1641 	if (max_streams < STREAM_LOG_STREAMS) {
1642 		dev_err(pdev->dev, "Stream size %d not supported\n",
1643 			max_streams);
1644 		return -EINVAL;
1645 	}
1646 
1647 	max_streams = STREAM_LOG_STREAMS;
1648 
1649 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1650 		bool direction = !(i & 1); /* Start from OUT endpoint. */
1651 		u8 epnum = ((i + 1) >> 1);
1652 
1653 		if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
1654 			continue;
1655 
1656 		pep = &pdev->eps[i];
1657 		pep->pdev = pdev;
1658 		pep->number = epnum;
1659 		pep->direction = direction; /* 0 for OUT, 1 for IN. */
1660 
1661 		/*
1662 		 * Ep0 is bidirectional, so ep0in and ep0out are represented by
1663 		 * pdev->eps[0]
1664 		 */
1665 		if (epnum == 0) {
1666 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1667 				 epnum, "BiDir");
1668 
1669 			pep->idx = 0;
1670 			usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
1671 			pep->endpoint.maxburst = 1;
1672 			pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
1673 			pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
1674 			pep->endpoint.comp_desc = NULL;
1675 			pep->endpoint.caps.type_control = true;
1676 			pep->endpoint.caps.dir_in = true;
1677 			pep->endpoint.caps.dir_out = true;
1678 
1679 			pdev->ep0_preq.epnum = pep->number;
1680 			pdev->ep0_preq.pep = pep;
1681 			pdev->gadget.ep0 = &pep->endpoint;
1682 		} else {
1683 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1684 				 epnum, (pep->direction) ? "in" : "out");
1685 
1686 			pep->idx =  (epnum * 2 + (direction ? 1 : 0)) - 1;
1687 			usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
1688 
1689 			pep->endpoint.max_streams = max_streams;
1690 			pep->endpoint.ops = &cdnsp_gadget_ep_ops;
1691 			list_add_tail(&pep->endpoint.ep_list,
1692 				      &pdev->gadget.ep_list);
1693 
1694 			pep->endpoint.caps.type_iso = true;
1695 			pep->endpoint.caps.type_bulk = true;
1696 			pep->endpoint.caps.type_int = true;
1697 
1698 			pep->endpoint.caps.dir_in = direction;
1699 			pep->endpoint.caps.dir_out = !direction;
1700 		}
1701 
1702 		pep->endpoint.name = pep->name;
1703 		pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
1704 		pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
1705 		cdnsp_get_ep_buffering(pdev, pep);
1706 
1707 		dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
1708 			"CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
1709 			"SupDir IN: %s, OUT: %s\n",
1710 			pep->name, 1024,
1711 			(pep->endpoint.caps.type_control) ? "yes" : "no",
1712 			(pep->endpoint.caps.type_int) ? "yes" : "no",
1713 			(pep->endpoint.caps.type_bulk) ? "yes" : "no",
1714 			(pep->endpoint.caps.type_iso) ? "yes" : "no",
1715 			(pep->endpoint.caps.dir_in) ? "yes" : "no",
1716 			(pep->endpoint.caps.dir_out) ? "yes" : "no");
1717 
1718 		INIT_LIST_HEAD(&pep->pending_list);
1719 	}
1720 
1721 	return 0;
1722 }
1723 
cdnsp_gadget_free_endpoints(struct cdnsp_device * pdev)1724 static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
1725 {
1726 	struct cdnsp_ep *pep;
1727 	int i;
1728 
1729 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1730 		pep = &pdev->eps[i];
1731 		if (pep->number != 0 && pep->out_ctx)
1732 			list_del(&pep->endpoint.ep_list);
1733 	}
1734 }
1735 
cdnsp_disconnect_gadget(struct cdnsp_device * pdev)1736 void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
1737 {
1738 	pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
1739 
1740 	if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
1741 		spin_unlock(&pdev->lock);
1742 		pdev->gadget_driver->disconnect(&pdev->gadget);
1743 		spin_lock(&pdev->lock);
1744 	}
1745 
1746 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
1747 	usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
1748 
1749 	pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
1750 }
1751 
cdnsp_suspend_gadget(struct cdnsp_device * pdev)1752 void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
1753 {
1754 	if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
1755 		spin_unlock(&pdev->lock);
1756 		pdev->gadget_driver->suspend(&pdev->gadget);
1757 		spin_lock(&pdev->lock);
1758 	}
1759 }
1760 
cdnsp_resume_gadget(struct cdnsp_device * pdev)1761 void cdnsp_resume_gadget(struct cdnsp_device *pdev)
1762 {
1763 	if (pdev->gadget_driver && pdev->gadget_driver->resume) {
1764 		spin_unlock(&pdev->lock);
1765 		pdev->gadget_driver->resume(&pdev->gadget);
1766 		spin_lock(&pdev->lock);
1767 	}
1768 }
1769 
cdnsp_irq_reset(struct cdnsp_device * pdev)1770 void cdnsp_irq_reset(struct cdnsp_device *pdev)
1771 {
1772 	struct cdnsp_port_regs __iomem *port_regs;
1773 
1774 	cdnsp_reset_device(pdev);
1775 
1776 	port_regs = pdev->active_port->regs;
1777 	pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
1778 
1779 	spin_unlock(&pdev->lock);
1780 	usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
1781 	spin_lock(&pdev->lock);
1782 
1783 	switch (pdev->gadget.speed) {
1784 	case USB_SPEED_SUPER_PLUS:
1785 	case USB_SPEED_SUPER:
1786 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1787 		pdev->gadget.ep0->maxpacket = 512;
1788 		break;
1789 	case USB_SPEED_HIGH:
1790 	case USB_SPEED_FULL:
1791 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1792 		pdev->gadget.ep0->maxpacket = 64;
1793 		break;
1794 	default:
1795 		/* Low speed is not supported. */
1796 		dev_err(pdev->dev, "Unknown device speed\n");
1797 		break;
1798 	}
1799 
1800 	cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1801 	cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
1802 	usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
1803 }
1804 
cdnsp_get_rev_cap(struct cdnsp_device * pdev)1805 static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
1806 {
1807 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
1808 
1809 	reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
1810 	pdev->rev_cap  = reg;
1811 
1812 	pdev->rtl_revision = readl(&pdev->rev_cap->rtl_revision);
1813 
1814 	dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
1815 		 readl(&pdev->rev_cap->ctrl_revision),
1816 		 readl(&pdev->rev_cap->rtl_revision),
1817 		 readl(&pdev->rev_cap->ep_supported),
1818 		 readl(&pdev->rev_cap->rx_buff_size),
1819 		 readl(&pdev->rev_cap->tx_buff_size));
1820 }
1821 
cdnsp_gen_setup(struct cdnsp_device * pdev)1822 static int cdnsp_gen_setup(struct cdnsp_device *pdev)
1823 {
1824 	int ret;
1825 	u32 reg;
1826 
1827 	pdev->cap_regs = pdev->regs;
1828 	pdev->op_regs = pdev->regs +
1829 		HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
1830 	pdev->run_regs = pdev->regs +
1831 		(readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
1832 
1833 	/* Cache read-only capability registers */
1834 	pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
1835 	pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
1836 	pdev->hci_version = HC_VERSION(pdev->hcc_params);
1837 	pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
1838 
1839 	/*
1840 	 * Override the APB timeout value to give the controller more time for
1841 	 * enabling UTMI clock and synchronizing APB and UTMI clock domains.
1842 	 * This fix is platform specific and is required to fixes issue with
1843 	 * reading incorrect value from PORTSC register after resuming
1844 	 * from L1 state.
1845 	 */
1846 	cdnsp_set_apb_timeout_value(pdev);
1847 
1848 	cdnsp_get_rev_cap(pdev);
1849 
1850 	/* Make sure the Device Controller is halted. */
1851 	ret = cdnsp_halt(pdev);
1852 	if (ret)
1853 		return ret;
1854 
1855 	/* Reset the internal controller memory state and registers. */
1856 	ret = cdnsp_reset(pdev);
1857 	if (ret)
1858 		return ret;
1859 
1860 	/*
1861 	 * Set dma_mask and coherent_dma_mask to 64-bits,
1862 	 * if controller supports 64-bit addressing.
1863 	 */
1864 	if (HCC_64BIT_ADDR(pdev->hcc_params) &&
1865 	    !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
1866 		dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
1867 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
1868 	} else {
1869 		/*
1870 		 * This is to avoid error in cases where a 32-bit USB
1871 		 * controller is used on a 64-bit capable system.
1872 		 */
1873 		ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
1874 		if (ret)
1875 			return ret;
1876 
1877 		dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
1878 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
1879 	}
1880 
1881 	spin_lock_init(&pdev->lock);
1882 
1883 	ret = cdnsp_mem_init(pdev);
1884 	if (ret)
1885 		return ret;
1886 
1887 	/*
1888 	 * Software workaround for U1: after transition
1889 	 * to U1 the controller starts gating clock, and in some cases,
1890 	 * it causes that controller stack.
1891 	 */
1892 	reg = readl(&pdev->port3x_regs->mode_2);
1893 	reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
1894 	writel(reg, &pdev->port3x_regs->mode_2);
1895 
1896 	return 0;
1897 }
1898 
__cdnsp_gadget_init(struct cdns * cdns)1899 static int __cdnsp_gadget_init(struct cdns *cdns)
1900 {
1901 	struct cdnsp_device *pdev;
1902 	u32 max_speed;
1903 	int ret = -ENOMEM;
1904 
1905 	cdns_drd_gadget_on(cdns);
1906 
1907 	pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
1908 	if (!pdev)
1909 		return -ENOMEM;
1910 
1911 	pm_runtime_get_sync(cdns->dev);
1912 
1913 	cdns->gadget_dev = pdev;
1914 	pdev->dev = cdns->dev;
1915 	pdev->regs = cdns->dev_regs;
1916 	max_speed = usb_get_maximum_speed(cdns->dev);
1917 
1918 	switch (max_speed) {
1919 	case USB_SPEED_FULL:
1920 	case USB_SPEED_HIGH:
1921 	case USB_SPEED_SUPER:
1922 	case USB_SPEED_SUPER_PLUS:
1923 		break;
1924 	default:
1925 		dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
1926 		fallthrough;
1927 	case USB_SPEED_UNKNOWN:
1928 		/* Default to SSP */
1929 		max_speed = USB_SPEED_SUPER_PLUS;
1930 		break;
1931 	}
1932 
1933 	pdev->gadget.ops = &cdnsp_gadget_ops;
1934 	pdev->gadget.name = "cdnsp-gadget";
1935 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
1936 	pdev->gadget.sg_supported = 1;
1937 	pdev->gadget.max_speed = max_speed;
1938 	pdev->gadget.lpm_capable = 1;
1939 
1940 	pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
1941 	if (!pdev->setup_buf)
1942 		goto free_pdev;
1943 
1944 	/*
1945 	 * Controller supports not aligned buffer but it should improve
1946 	 * performance.
1947 	 */
1948 	pdev->gadget.quirk_ep_out_aligned_size = true;
1949 
1950 	ret = cdnsp_gen_setup(pdev);
1951 	if (ret) {
1952 		dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
1953 		goto free_setup;
1954 	}
1955 
1956 	ret = cdnsp_gadget_init_endpoints(pdev);
1957 	if (ret) {
1958 		dev_err(pdev->dev, "failed to initialize endpoints\n");
1959 		goto halt_pdev;
1960 	}
1961 
1962 	ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
1963 	if (ret) {
1964 		dev_err(pdev->dev, "failed to register udc\n");
1965 		goto free_endpoints;
1966 	}
1967 
1968 	ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
1969 					cdnsp_irq_handler,
1970 					cdnsp_thread_irq_handler, IRQF_SHARED,
1971 					dev_name(pdev->dev), pdev);
1972 	if (ret)
1973 		goto del_gadget;
1974 
1975 	return 0;
1976 
1977 del_gadget:
1978 	usb_del_gadget_udc(&pdev->gadget);
1979 free_endpoints:
1980 	cdnsp_gadget_free_endpoints(pdev);
1981 halt_pdev:
1982 	cdnsp_halt(pdev);
1983 	cdnsp_reset(pdev);
1984 	cdnsp_mem_cleanup(pdev);
1985 free_setup:
1986 	kfree(pdev->setup_buf);
1987 free_pdev:
1988 	kfree(pdev);
1989 
1990 	return ret;
1991 }
1992 
cdnsp_gadget_exit(struct cdns * cdns)1993 static void cdnsp_gadget_exit(struct cdns *cdns)
1994 {
1995 	struct cdnsp_device *pdev = cdns->gadget_dev;
1996 
1997 	devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
1998 	pm_runtime_mark_last_busy(cdns->dev);
1999 	pm_runtime_put_autosuspend(cdns->dev);
2000 	usb_del_gadget_udc(&pdev->gadget);
2001 	cdnsp_gadget_free_endpoints(pdev);
2002 	cdnsp_mem_cleanup(pdev);
2003 	kfree(pdev);
2004 	cdns->gadget_dev = NULL;
2005 	cdns_drd_gadget_off(cdns);
2006 }
2007 
cdnsp_gadget_suspend(struct cdns * cdns,bool do_wakeup)2008 static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
2009 {
2010 	struct cdnsp_device *pdev = cdns->gadget_dev;
2011 	unsigned long flags;
2012 
2013 	if (pdev->link_state == XDEV_U3)
2014 		return 0;
2015 
2016 	spin_lock_irqsave(&pdev->lock, flags);
2017 	cdnsp_disconnect_gadget(pdev);
2018 	cdnsp_stop(pdev);
2019 	spin_unlock_irqrestore(&pdev->lock, flags);
2020 
2021 	return 0;
2022 }
2023 
cdnsp_gadget_resume(struct cdns * cdns,bool hibernated)2024 static int cdnsp_gadget_resume(struct cdns *cdns, bool hibernated)
2025 {
2026 	struct cdnsp_device *pdev = cdns->gadget_dev;
2027 	enum usb_device_speed max_speed;
2028 	unsigned long flags;
2029 	int ret;
2030 
2031 	if (!pdev->gadget_driver)
2032 		return 0;
2033 
2034 	spin_lock_irqsave(&pdev->lock, flags);
2035 	max_speed = pdev->gadget_driver->max_speed;
2036 
2037 	/* Limit speed if necessary. */
2038 	max_speed = min(max_speed, pdev->gadget.max_speed);
2039 
2040 	ret = cdnsp_run(pdev, max_speed);
2041 
2042 	if (pdev->link_state == XDEV_U3)
2043 		__cdnsp_gadget_wakeup(pdev);
2044 
2045 	spin_unlock_irqrestore(&pdev->lock, flags);
2046 
2047 	return ret;
2048 }
2049 
2050 /**
2051  * cdnsp_gadget_init - initialize device structure
2052  * @cdns: cdnsp instance
2053  *
2054  * This function initializes the gadget.
2055  */
cdnsp_gadget_init(struct cdns * cdns)2056 int cdnsp_gadget_init(struct cdns *cdns)
2057 {
2058 	struct cdns_role_driver *rdrv;
2059 
2060 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
2061 	if (!rdrv)
2062 		return -ENOMEM;
2063 
2064 	rdrv->start	= __cdnsp_gadget_init;
2065 	rdrv->stop	= cdnsp_gadget_exit;
2066 	rdrv->suspend	= cdnsp_gadget_suspend;
2067 	rdrv->resume	= cdnsp_gadget_resume;
2068 	rdrv->state	= CDNS_ROLE_STATE_INACTIVE;
2069 	rdrv->name	= "gadget";
2070 	cdns->roles[USB_ROLE_DEVICE] = rdrv;
2071 
2072 	return 0;
2073 }
2074