xref: /openbmc/linux/drivers/usb/cdns3/cdnsp-gadget.c (revision 3b414d1b0107fa51ad6063de9752d4b2a8063980)
13d829045SPawel Laszczak // SPDX-License-Identifier: GPL-2.0
23d829045SPawel Laszczak /*
33d829045SPawel Laszczak  * Cadence CDNSP DRD Driver.
43d829045SPawel Laszczak  *
53d829045SPawel Laszczak  * Copyright (C) 2020 Cadence.
63d829045SPawel Laszczak  *
73d829045SPawel Laszczak  * Author: Pawel Laszczak <pawell@cadence.com>
83d829045SPawel Laszczak  *
93d829045SPawel Laszczak  */
103d829045SPawel Laszczak 
113d829045SPawel Laszczak #include <linux/moduleparam.h>
123d829045SPawel Laszczak #include <linux/dma-mapping.h>
133d829045SPawel Laszczak #include <linux/module.h>
143d829045SPawel Laszczak #include <linux/iopoll.h>
153d829045SPawel Laszczak #include <linux/delay.h>
163d829045SPawel Laszczak #include <linux/log2.h>
173d829045SPawel Laszczak #include <linux/slab.h>
183d829045SPawel Laszczak #include <linux/pci.h>
193d829045SPawel Laszczak #include <linux/irq.h>
203d829045SPawel Laszczak #include <linux/dmi.h>
213d829045SPawel Laszczak 
223d829045SPawel Laszczak #include "core.h"
233d829045SPawel Laszczak #include "gadget-export.h"
243d829045SPawel Laszczak #include "drd.h"
253d829045SPawel Laszczak #include "cdnsp-gadget.h"
26118b2a32SPawel Laszczak #include "cdnsp-trace.h"
273d829045SPawel Laszczak 
283d829045SPawel Laszczak unsigned int cdnsp_port_speed(unsigned int port_status)
293d829045SPawel Laszczak {
303d829045SPawel Laszczak 	/*Detect gadget speed based on PORTSC register*/
313d829045SPawel Laszczak 	if (DEV_SUPERSPEEDPLUS(port_status))
323d829045SPawel Laszczak 		return USB_SPEED_SUPER_PLUS;
333d829045SPawel Laszczak 	else if (DEV_SUPERSPEED(port_status))
343d829045SPawel Laszczak 		return USB_SPEED_SUPER;
353d829045SPawel Laszczak 	else if (DEV_HIGHSPEED(port_status))
363d829045SPawel Laszczak 		return USB_SPEED_HIGH;
373d829045SPawel Laszczak 	else if (DEV_FULLSPEED(port_status))
383d829045SPawel Laszczak 		return USB_SPEED_FULL;
393d829045SPawel Laszczak 
403d829045SPawel Laszczak 	/* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
413d829045SPawel Laszczak 	return USB_SPEED_UNKNOWN;
423d829045SPawel Laszczak }
433d829045SPawel Laszczak 
443d829045SPawel Laszczak /*
453d829045SPawel Laszczak  * Given a port state, this function returns a value that would result in the
463d829045SPawel Laszczak  * port being in the same state, if the value was written to the port status
473d829045SPawel Laszczak  * control register.
483d829045SPawel Laszczak  * Save Read Only (RO) bits and save read/write bits where
493d829045SPawel Laszczak  * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
503d829045SPawel Laszczak  * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
513d829045SPawel Laszczak  */
523d829045SPawel Laszczak u32 cdnsp_port_state_to_neutral(u32 state)
533d829045SPawel Laszczak {
543d829045SPawel Laszczak 	/* Save read-only status and port state. */
553d829045SPawel Laszczak 	return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
563d829045SPawel Laszczak }
573d829045SPawel Laszczak 
583d829045SPawel Laszczak /**
593d829045SPawel Laszczak  * Find the offset of the extended capabilities with capability ID id.
603d829045SPawel Laszczak  * @base: PCI MMIO registers base address.
613d829045SPawel Laszczak  * @start: Address at which to start looking, (0 or HCC_PARAMS to start at
623d829045SPawel Laszczak  *         beginning of list)
633d829045SPawel Laszczak  * @id: Extended capability ID to search for.
643d829045SPawel Laszczak  *
653d829045SPawel Laszczak  * Returns the offset of the next matching extended capability structure.
663d829045SPawel Laszczak  * Some capabilities can occur several times,
673d829045SPawel Laszczak  * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all.
683d829045SPawel Laszczak  */
693d829045SPawel Laszczak int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id)
703d829045SPawel Laszczak {
713d829045SPawel Laszczak 	u32 offset = start;
723d829045SPawel Laszczak 	u32 next;
733d829045SPawel Laszczak 	u32 val;
743d829045SPawel Laszczak 
753d829045SPawel Laszczak 	if (!start || start == HCC_PARAMS_OFFSET) {
763d829045SPawel Laszczak 		val = readl(base + HCC_PARAMS_OFFSET);
773d829045SPawel Laszczak 		if (val == ~0)
783d829045SPawel Laszczak 			return 0;
793d829045SPawel Laszczak 
803d829045SPawel Laszczak 		offset = HCC_EXT_CAPS(val) << 2;
813d829045SPawel Laszczak 		if (!offset)
823d829045SPawel Laszczak 			return 0;
833d829045SPawel Laszczak 	};
843d829045SPawel Laszczak 
853d829045SPawel Laszczak 	do {
863d829045SPawel Laszczak 		val = readl(base + offset);
873d829045SPawel Laszczak 		if (val == ~0)
883d829045SPawel Laszczak 			return 0;
893d829045SPawel Laszczak 
903d829045SPawel Laszczak 		if (EXT_CAPS_ID(val) == id && offset != start)
913d829045SPawel Laszczak 			return offset;
923d829045SPawel Laszczak 
933d829045SPawel Laszczak 		next = EXT_CAPS_NEXT(val);
943d829045SPawel Laszczak 		offset += next << 2;
953d829045SPawel Laszczak 	} while (next);
963d829045SPawel Laszczak 
973d829045SPawel Laszczak 	return 0;
983d829045SPawel Laszczak }
993d829045SPawel Laszczak 
1003d829045SPawel Laszczak void cdnsp_set_link_state(struct cdnsp_device *pdev,
1013d829045SPawel Laszczak 			  __le32 __iomem *port_regs,
1023d829045SPawel Laszczak 			  u32 link_state)
1033d829045SPawel Laszczak {
104118b2a32SPawel Laszczak 	int port_num = 0xFF;
1053d829045SPawel Laszczak 	u32 temp;
1063d829045SPawel Laszczak 
1073d829045SPawel Laszczak 	temp = readl(port_regs);
1083d829045SPawel Laszczak 	temp = cdnsp_port_state_to_neutral(temp);
1093d829045SPawel Laszczak 	temp |= PORT_WKCONN_E | PORT_WKDISC_E;
1103d829045SPawel Laszczak 	writel(temp, port_regs);
1113d829045SPawel Laszczak 
1123d829045SPawel Laszczak 	temp &= ~PORT_PLS_MASK;
1133d829045SPawel Laszczak 	temp |= PORT_LINK_STROBE | link_state;
1143d829045SPawel Laszczak 
115118b2a32SPawel Laszczak 	if (pdev->active_port)
116118b2a32SPawel Laszczak 		port_num = pdev->active_port->port_num;
117118b2a32SPawel Laszczak 
118118b2a32SPawel Laszczak 	trace_cdnsp_handle_port_status(port_num, readl(port_regs));
1193d829045SPawel Laszczak 	writel(temp, port_regs);
120118b2a32SPawel Laszczak 	trace_cdnsp_link_state_changed(port_num, readl(port_regs));
1213d829045SPawel Laszczak }
1223d829045SPawel Laszczak 
1233d829045SPawel Laszczak static void cdnsp_disable_port(struct cdnsp_device *pdev,
1243d829045SPawel Laszczak 			       __le32 __iomem *port_regs)
1253d829045SPawel Laszczak {
1263d829045SPawel Laszczak 	u32 temp = cdnsp_port_state_to_neutral(readl(port_regs));
1273d829045SPawel Laszczak 
1283d829045SPawel Laszczak 	writel(temp | PORT_PED, port_regs);
1293d829045SPawel Laszczak }
1303d829045SPawel Laszczak 
1313d829045SPawel Laszczak static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev,
1323d829045SPawel Laszczak 					__le32 __iomem *port_regs)
1333d829045SPawel Laszczak {
1343d829045SPawel Laszczak 	u32 portsc = readl(port_regs);
1353d829045SPawel Laszczak 
1363d829045SPawel Laszczak 	writel(cdnsp_port_state_to_neutral(portsc) |
1373d829045SPawel Laszczak 	       (portsc & PORT_CHANGE_BITS), port_regs);
1383d829045SPawel Laszczak }
1393d829045SPawel Laszczak 
1403d829045SPawel Laszczak static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
1413d829045SPawel Laszczak {
1423d829045SPawel Laszczak 	__le32 __iomem *reg;
1433d829045SPawel Laszczak 	void __iomem *base;
1443d829045SPawel Laszczak 	u32 offset = 0;
1453d829045SPawel Laszczak 
1463d829045SPawel Laszczak 	base = &pdev->cap_regs->hc_capbase;
1473d829045SPawel Laszczak 	offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
1483d829045SPawel Laszczak 	reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
1493d829045SPawel Laszczak 
1503d829045SPawel Laszczak 	bit = readl(reg) | bit;
1513d829045SPawel Laszczak 	writel(bit, reg);
1523d829045SPawel Laszczak }
1533d829045SPawel Laszczak 
1543d829045SPawel Laszczak static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
1553d829045SPawel Laszczak {
1563d829045SPawel Laszczak 	__le32 __iomem *reg;
1573d829045SPawel Laszczak 	void __iomem *base;
1583d829045SPawel Laszczak 	u32 offset = 0;
1593d829045SPawel Laszczak 
1603d829045SPawel Laszczak 	base = &pdev->cap_regs->hc_capbase;
1613d829045SPawel Laszczak 	offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
1623d829045SPawel Laszczak 	reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
1633d829045SPawel Laszczak 
1643d829045SPawel Laszczak 	bit = readl(reg) & ~bit;
1653d829045SPawel Laszczak 	writel(bit, reg);
1663d829045SPawel Laszczak }
1673d829045SPawel Laszczak 
1683d829045SPawel Laszczak /*
1693d829045SPawel Laszczak  * Disable interrupts and begin the controller halting process.
1703d829045SPawel Laszczak  */
1713d829045SPawel Laszczak static void cdnsp_quiesce(struct cdnsp_device *pdev)
1723d829045SPawel Laszczak {
1733d829045SPawel Laszczak 	u32 halted;
1743d829045SPawel Laszczak 	u32 mask;
1753d829045SPawel Laszczak 	u32 cmd;
1763d829045SPawel Laszczak 
1773d829045SPawel Laszczak 	mask = ~(u32)(CDNSP_IRQS);
1783d829045SPawel Laszczak 
1793d829045SPawel Laszczak 	halted = readl(&pdev->op_regs->status) & STS_HALT;
1803d829045SPawel Laszczak 	if (!halted)
1813d829045SPawel Laszczak 		mask &= ~(CMD_R_S | CMD_DEVEN);
1823d829045SPawel Laszczak 
1833d829045SPawel Laszczak 	cmd = readl(&pdev->op_regs->command);
1843d829045SPawel Laszczak 	cmd &= mask;
1853d829045SPawel Laszczak 	writel(cmd, &pdev->op_regs->command);
1863d829045SPawel Laszczak }
1873d829045SPawel Laszczak 
1883d829045SPawel Laszczak /*
1893d829045SPawel Laszczak  * Force controller into halt state.
1903d829045SPawel Laszczak  *
1913d829045SPawel Laszczak  * Disable any IRQs and clear the run/stop bit.
1923d829045SPawel Laszczak  * Controller will complete any current and actively pipelined transactions, and
1933d829045SPawel Laszczak  * should halt within 16 ms of the run/stop bit being cleared.
1943d829045SPawel Laszczak  * Read controller Halted bit in the status register to see when the
1953d829045SPawel Laszczak  * controller is finished.
1963d829045SPawel Laszczak  */
1973d829045SPawel Laszczak int cdnsp_halt(struct cdnsp_device *pdev)
1983d829045SPawel Laszczak {
1993d829045SPawel Laszczak 	int ret;
2003d829045SPawel Laszczak 	u32 val;
2013d829045SPawel Laszczak 
2023d829045SPawel Laszczak 	cdnsp_quiesce(pdev);
2033d829045SPawel Laszczak 
2043d829045SPawel Laszczak 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val,
2053d829045SPawel Laszczak 					val & STS_HALT, 1,
2063d829045SPawel Laszczak 					CDNSP_MAX_HALT_USEC);
2073d829045SPawel Laszczak 	if (ret) {
2083d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Device halt failed\n");
2093d829045SPawel Laszczak 		return ret;
2103d829045SPawel Laszczak 	}
2113d829045SPawel Laszczak 
2123d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_STATE_HALTED;
2133d829045SPawel Laszczak 
2143d829045SPawel Laszczak 	return 0;
2153d829045SPawel Laszczak }
2163d829045SPawel Laszczak 
2173d829045SPawel Laszczak /*
2183d829045SPawel Laszczak  * device controller died, register read returns 0xffffffff, or command never
2193d829045SPawel Laszczak  * ends.
2203d829045SPawel Laszczak  */
2213d829045SPawel Laszczak void cdnsp_died(struct cdnsp_device *pdev)
2223d829045SPawel Laszczak {
2233d829045SPawel Laszczak 	dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n");
2243d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_STATE_DYING;
2253d829045SPawel Laszczak 	cdnsp_halt(pdev);
2263d829045SPawel Laszczak }
2273d829045SPawel Laszczak 
2283d829045SPawel Laszczak /*
2293d829045SPawel Laszczak  * Set the run bit and wait for the device to be running.
2303d829045SPawel Laszczak  */
2313d829045SPawel Laszczak static int cdnsp_start(struct cdnsp_device *pdev)
2323d829045SPawel Laszczak {
2333d829045SPawel Laszczak 	u32 temp;
2343d829045SPawel Laszczak 	int ret;
2353d829045SPawel Laszczak 
2363d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->command);
2373d829045SPawel Laszczak 	temp |= (CMD_R_S | CMD_DEVEN);
2383d829045SPawel Laszczak 	writel(temp, &pdev->op_regs->command);
2393d829045SPawel Laszczak 
2403d829045SPawel Laszczak 	pdev->cdnsp_state = 0;
2413d829045SPawel Laszczak 
2423d829045SPawel Laszczak 	/*
2433d829045SPawel Laszczak 	 * Wait for the STS_HALT Status bit to be 0 to indicate the device is
2443d829045SPawel Laszczak 	 * running.
2453d829045SPawel Laszczak 	 */
2463d829045SPawel Laszczak 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
2473d829045SPawel Laszczak 					!(temp & STS_HALT), 1,
2483d829045SPawel Laszczak 					CDNSP_MAX_HALT_USEC);
2493d829045SPawel Laszczak 	if (ret) {
2503d829045SPawel Laszczak 		pdev->cdnsp_state = CDNSP_STATE_DYING;
2513d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Controller run failed\n");
2523d829045SPawel Laszczak 	}
2533d829045SPawel Laszczak 
2543d829045SPawel Laszczak 	return ret;
2553d829045SPawel Laszczak }
2563d829045SPawel Laszczak 
2573d829045SPawel Laszczak /*
2583d829045SPawel Laszczak  * Reset a halted controller.
2593d829045SPawel Laszczak  *
2603d829045SPawel Laszczak  * This resets pipelines, timers, counters, state machines, etc.
2613d829045SPawel Laszczak  * Transactions will be terminated immediately, and operational registers
2623d829045SPawel Laszczak  * will be set to their defaults.
2633d829045SPawel Laszczak  */
2643d829045SPawel Laszczak int cdnsp_reset(struct cdnsp_device *pdev)
2653d829045SPawel Laszczak {
2663d829045SPawel Laszczak 	u32 command;
2673d829045SPawel Laszczak 	u32 temp;
2683d829045SPawel Laszczak 	int ret;
2693d829045SPawel Laszczak 
2703d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->status);
2713d829045SPawel Laszczak 
2723d829045SPawel Laszczak 	if (temp == ~(u32)0) {
2733d829045SPawel Laszczak 		dev_err(pdev->dev, "Device not accessible, reset failed.\n");
2743d829045SPawel Laszczak 		return -ENODEV;
2753d829045SPawel Laszczak 	}
2763d829045SPawel Laszczak 
2773d829045SPawel Laszczak 	if ((temp & STS_HALT) == 0) {
2783d829045SPawel Laszczak 		dev_err(pdev->dev, "Controller not halted, aborting reset.\n");
2793d829045SPawel Laszczak 		return -EINVAL;
2803d829045SPawel Laszczak 	}
2813d829045SPawel Laszczak 
2823d829045SPawel Laszczak 	command = readl(&pdev->op_regs->command);
2833d829045SPawel Laszczak 	command |= CMD_RESET;
2843d829045SPawel Laszczak 	writel(command, &pdev->op_regs->command);
2853d829045SPawel Laszczak 
2863d829045SPawel Laszczak 	ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp,
2873d829045SPawel Laszczak 					!(temp & CMD_RESET), 1,
2883d829045SPawel Laszczak 					10 * 1000);
2893d829045SPawel Laszczak 	if (ret) {
2903d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Controller reset failed\n");
2913d829045SPawel Laszczak 		return ret;
2923d829045SPawel Laszczak 	}
2933d829045SPawel Laszczak 
2943d829045SPawel Laszczak 	/*
2953d829045SPawel Laszczak 	 * CDNSP cannot write any doorbells or operational registers other
2963d829045SPawel Laszczak 	 * than status until the "Controller Not Ready" flag is cleared.
2973d829045SPawel Laszczak 	 */
2983d829045SPawel Laszczak 	ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
2993d829045SPawel Laszczak 					!(temp & STS_CNR), 1,
3003d829045SPawel Laszczak 					10 * 1000);
3013d829045SPawel Laszczak 
3023d829045SPawel Laszczak 	if (ret) {
3033d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Controller not ready to work\n");
3043d829045SPawel Laszczak 		return ret;
3053d829045SPawel Laszczak 	}
3063d829045SPawel Laszczak 
3073d829045SPawel Laszczak 	dev_dbg(pdev->dev, "Controller ready to work");
3083d829045SPawel Laszczak 
3093d829045SPawel Laszczak 	return ret;
3103d829045SPawel Laszczak }
3113d829045SPawel Laszczak 
3123d829045SPawel Laszczak /*
3133d829045SPawel Laszczak  * cdnsp_get_endpoint_index - Find the index for an endpoint given its
3143d829045SPawel Laszczak  * descriptor.Use the return value to right shift 1 for the bitmask.
3153d829045SPawel Laszczak  *
3163d829045SPawel Laszczak  * Index = (epnum * 2) + direction - 1,
3173d829045SPawel Laszczak  * where direction = 0 for OUT, 1 for IN.
3183d829045SPawel Laszczak  * For control endpoints, the IN index is used (OUT index is unused), so
3193d829045SPawel Laszczak  * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
3203d829045SPawel Laszczak  */
3213d829045SPawel Laszczak static unsigned int
3223d829045SPawel Laszczak 	cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc)
3233d829045SPawel Laszczak {
3243d829045SPawel Laszczak 	unsigned int index = (unsigned int)usb_endpoint_num(desc);
3253d829045SPawel Laszczak 
3263d829045SPawel Laszczak 	if (usb_endpoint_xfer_control(desc))
3273d829045SPawel Laszczak 		return index * 2;
3283d829045SPawel Laszczak 
3293d829045SPawel Laszczak 	return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
3303d829045SPawel Laszczak }
3313d829045SPawel Laszczak 
3323d829045SPawel Laszczak /*
3333d829045SPawel Laszczak  * Find the flag for this endpoint (for use in the control context). Use the
3343d829045SPawel Laszczak  * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
3353d829045SPawel Laszczak  * bit 1, etc.
3363d829045SPawel Laszczak  */
3373d829045SPawel Laszczak static unsigned int
3383d829045SPawel Laszczak 	cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc)
3393d829045SPawel Laszczak {
3403d829045SPawel Laszczak 	return 1 << (cdnsp_get_endpoint_index(desc) + 1);
3413d829045SPawel Laszczak }
3423d829045SPawel Laszczak 
3433d829045SPawel Laszczak int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
3443d829045SPawel Laszczak {
3453d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
3463d829045SPawel Laszczak 	struct usb_request *request;
3473d829045SPawel Laszczak 	int ret;
3483d829045SPawel Laszczak 
349118b2a32SPawel Laszczak 	if (preq->epnum == 0 && !list_empty(&pep->pending_list)) {
350118b2a32SPawel Laszczak 		trace_cdnsp_request_enqueue_busy(preq);
3513d829045SPawel Laszczak 		return -EBUSY;
352118b2a32SPawel Laszczak 	}
3533d829045SPawel Laszczak 
3543d829045SPawel Laszczak 	request = &preq->request;
3553d829045SPawel Laszczak 	request->actual = 0;
3563d829045SPawel Laszczak 	request->status = -EINPROGRESS;
3573d829045SPawel Laszczak 	preq->direction = pep->direction;
3583d829045SPawel Laszczak 	preq->epnum = pep->number;
3593d829045SPawel Laszczak 	preq->td.drbl = 0;
3603d829045SPawel Laszczak 
3613d829045SPawel Laszczak 	ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction);
362118b2a32SPawel Laszczak 	if (ret) {
363118b2a32SPawel Laszczak 		trace_cdnsp_request_enqueue_error(preq);
3643d829045SPawel Laszczak 		return ret;
365118b2a32SPawel Laszczak 	}
3663d829045SPawel Laszczak 
3673d829045SPawel Laszczak 	list_add_tail(&preq->list, &pep->pending_list);
3683d829045SPawel Laszczak 
369118b2a32SPawel Laszczak 	trace_cdnsp_request_enqueue(preq);
370118b2a32SPawel Laszczak 
3713d829045SPawel Laszczak 	switch (usb_endpoint_type(pep->endpoint.desc)) {
3723d829045SPawel Laszczak 	case USB_ENDPOINT_XFER_CONTROL:
3733d829045SPawel Laszczak 		ret = cdnsp_queue_ctrl_tx(pdev, preq);
3743d829045SPawel Laszczak 		break;
3753d829045SPawel Laszczak 	case USB_ENDPOINT_XFER_BULK:
3763d829045SPawel Laszczak 	case USB_ENDPOINT_XFER_INT:
3773d829045SPawel Laszczak 		ret = cdnsp_queue_bulk_tx(pdev, preq);
3783d829045SPawel Laszczak 		break;
3793d829045SPawel Laszczak 	case USB_ENDPOINT_XFER_ISOC:
3803d829045SPawel Laszczak 		ret = cdnsp_queue_isoc_tx_prepare(pdev, preq);
3813d829045SPawel Laszczak 	}
3823d829045SPawel Laszczak 
3833d829045SPawel Laszczak 	if (ret)
3843d829045SPawel Laszczak 		goto unmap;
3853d829045SPawel Laszczak 
3863d829045SPawel Laszczak 	return 0;
3873d829045SPawel Laszczak 
3883d829045SPawel Laszczak unmap:
3893d829045SPawel Laszczak 	usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
3903d829045SPawel Laszczak 					pep->direction);
3913d829045SPawel Laszczak 	list_del(&preq->list);
392118b2a32SPawel Laszczak 	trace_cdnsp_request_enqueue_error(preq);
3933d829045SPawel Laszczak 
3943d829045SPawel Laszczak 	return ret;
3953d829045SPawel Laszczak }
3963d829045SPawel Laszczak 
3973d829045SPawel Laszczak /*
3983d829045SPawel Laszczak  * Remove the request's TD from the endpoint ring. This may cause the
3993d829045SPawel Laszczak  * controller to stop USB transfers, potentially stopping in the middle of a
4003d829045SPawel Laszczak  * TRB buffer. The controller should pick up where it left off in the TD,
4013d829045SPawel Laszczak  * unless a Set Transfer Ring Dequeue Pointer is issued.
4023d829045SPawel Laszczak  *
4033d829045SPawel Laszczak  * The TRBs that make up the buffers for the canceled request will be "removed"
4043d829045SPawel Laszczak  * from the ring. Since the ring is a contiguous structure, they can't be
4053d829045SPawel Laszczak  * physically removed. Instead, there are two options:
4063d829045SPawel Laszczak  *
4073d829045SPawel Laszczak  *  1) If the controller is in the middle of processing the request to be
4083d829045SPawel Laszczak  *     canceled, we simply move the ring's dequeue pointer past those TRBs
4093d829045SPawel Laszczak  *     using the Set Transfer Ring Dequeue Pointer command. This will be
4103d829045SPawel Laszczak  *     the common case, when drivers timeout on the last submitted request
4113d829045SPawel Laszczak  *     and attempt to cancel.
4123d829045SPawel Laszczak  *
4133d829045SPawel Laszczak  *  2) If the controller is in the middle of a different TD, we turn the TRBs
4143d829045SPawel Laszczak  *     into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained.
4153d829045SPawel Laszczak  *     The controller will need to invalidate the any TRBs it has cached after
4163d829045SPawel Laszczak  *     the stop endpoint command.
4173d829045SPawel Laszczak  *
4183d829045SPawel Laszczak  *  3) The TD may have completed by the time the Stop Endpoint Command
4193d829045SPawel Laszczak  *     completes, so software needs to handle that case too.
4203d829045SPawel Laszczak  *
4213d829045SPawel Laszczak  */
4223d829045SPawel Laszczak int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
4233d829045SPawel Laszczak {
4243d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
425*3b414d1bSPawel Laszczak 	int ret_stop = 0;
426*3b414d1bSPawel Laszczak 	int ret_rem;
4273d829045SPawel Laszczak 
428118b2a32SPawel Laszczak 	trace_cdnsp_request_dequeue(preq);
429118b2a32SPawel Laszczak 
430*3b414d1bSPawel Laszczak 	if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING)
431*3b414d1bSPawel Laszczak 		ret_stop = cdnsp_cmd_stop_ep(pdev, pep);
4323d829045SPawel Laszczak 
433*3b414d1bSPawel Laszczak 	ret_rem = cdnsp_remove_request(pdev, preq, pep);
434*3b414d1bSPawel Laszczak 
435*3b414d1bSPawel Laszczak 	return ret_rem ? ret_rem : ret_stop;
4363d829045SPawel Laszczak }
4373d829045SPawel Laszczak 
4383d829045SPawel Laszczak static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev)
4393d829045SPawel Laszczak {
4403d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
4413d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
4423d829045SPawel Laszczak 	struct cdnsp_ep_ctx *ep_ctx;
4433d829045SPawel Laszczak 	int i;
4443d829045SPawel Laszczak 
4453d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
4463d829045SPawel Laszczak 
4473d829045SPawel Laszczak 	/*
4483d829045SPawel Laszczak 	 * When a device's add flag and drop flag are zero, any subsequent
4493d829045SPawel Laszczak 	 * configure endpoint command will leave that endpoint's state
4503d829045SPawel Laszczak 	 * untouched. Make sure we don't leave any old state in the input
4513d829045SPawel Laszczak 	 * endpoint contexts.
4523d829045SPawel Laszczak 	 */
4533d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
4543d829045SPawel Laszczak 	ctrl_ctx->add_flags = 0;
4553d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
4563d829045SPawel Laszczak 	slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
4573d829045SPawel Laszczak 
4583d829045SPawel Laszczak 	/* Endpoint 0 is always valid */
4593d829045SPawel Laszczak 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
4603d829045SPawel Laszczak 	for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) {
4613d829045SPawel Laszczak 		ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i);
4623d829045SPawel Laszczak 		ep_ctx->ep_info = 0;
4633d829045SPawel Laszczak 		ep_ctx->ep_info2 = 0;
4643d829045SPawel Laszczak 		ep_ctx->deq = 0;
4653d829045SPawel Laszczak 		ep_ctx->tx_info = 0;
4663d829045SPawel Laszczak 	}
4673d829045SPawel Laszczak }
4683d829045SPawel Laszczak 
4693d829045SPawel Laszczak /* Issue a configure endpoint command and wait for it to finish. */
4703d829045SPawel Laszczak static int cdnsp_configure_endpoint(struct cdnsp_device *pdev)
4713d829045SPawel Laszczak {
4723d829045SPawel Laszczak 	int ret;
4733d829045SPawel Laszczak 
4743d829045SPawel Laszczak 	cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma);
4753d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
4763d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
4773d829045SPawel Laszczak 	if (ret) {
4783d829045SPawel Laszczak 		dev_err(pdev->dev,
4793d829045SPawel Laszczak 			"ERR: unexpected command completion code 0x%x.\n", ret);
4803d829045SPawel Laszczak 		return -EINVAL;
4813d829045SPawel Laszczak 	}
4823d829045SPawel Laszczak 
4833d829045SPawel Laszczak 	return ret;
4843d829045SPawel Laszczak }
4853d829045SPawel Laszczak 
4863d829045SPawel Laszczak static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev,
4873d829045SPawel Laszczak 				       struct cdnsp_ep *pep)
4883d829045SPawel Laszczak {
4893d829045SPawel Laszczak 	struct cdnsp_segment *segment;
4903d829045SPawel Laszczak 	union cdnsp_trb *event;
4913d829045SPawel Laszczak 	u32 cycle_state;
49216e36101SPawel Laszczak 	u32  data;
4933d829045SPawel Laszczak 
4943d829045SPawel Laszczak 	event = pdev->event_ring->dequeue;
4953d829045SPawel Laszczak 	segment = pdev->event_ring->deq_seg;
4963d829045SPawel Laszczak 	cycle_state = pdev->event_ring->cycle_state;
4973d829045SPawel Laszczak 
4983d829045SPawel Laszczak 	while (1) {
4993d829045SPawel Laszczak 		data = le32_to_cpu(event->trans_event.flags);
5003d829045SPawel Laszczak 
5013d829045SPawel Laszczak 		/* Check the owner of the TRB. */
5023d829045SPawel Laszczak 		if ((data & TRB_CYCLE) != cycle_state)
5033d829045SPawel Laszczak 			break;
5043d829045SPawel Laszczak 
5053d829045SPawel Laszczak 		if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER &&
5063d829045SPawel Laszczak 		    TRB_TO_EP_ID(data) == (pep->idx + 1)) {
5073d829045SPawel Laszczak 			data |= TRB_EVENT_INVALIDATE;
5083d829045SPawel Laszczak 			event->trans_event.flags = cpu_to_le32(data);
5093d829045SPawel Laszczak 		}
5103d829045SPawel Laszczak 
5113d829045SPawel Laszczak 		if (cdnsp_last_trb_on_seg(segment, event)) {
5123d829045SPawel Laszczak 			cycle_state ^= 1;
5133d829045SPawel Laszczak 			segment = pdev->event_ring->deq_seg->next;
5143d829045SPawel Laszczak 			event = segment->trbs;
5153d829045SPawel Laszczak 		} else {
5163d829045SPawel Laszczak 			event++;
5173d829045SPawel Laszczak 		}
5183d829045SPawel Laszczak 	}
5193d829045SPawel Laszczak }
5203d829045SPawel Laszczak 
5213d829045SPawel Laszczak int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev)
5223d829045SPawel Laszczak {
5233d829045SPawel Laszczak 	struct cdnsp_segment *event_deq_seg;
5243d829045SPawel Laszczak 	union cdnsp_trb *cmd_trb;
5253d829045SPawel Laszczak 	dma_addr_t cmd_deq_dma;
5263d829045SPawel Laszczak 	union cdnsp_trb *event;
5273d829045SPawel Laszczak 	u32 cycle_state;
5283d829045SPawel Laszczak 	int ret, val;
5293d829045SPawel Laszczak 	u64 cmd_dma;
53016e36101SPawel Laszczak 	u32  flags;
5313d829045SPawel Laszczak 
5323d829045SPawel Laszczak 	cmd_trb = pdev->cmd.command_trb;
5333d829045SPawel Laszczak 	pdev->cmd.status = 0;
5343d829045SPawel Laszczak 
535118b2a32SPawel Laszczak 	trace_cdnsp_cmd_wait_for_compl(pdev->cmd_ring, &cmd_trb->generic);
536118b2a32SPawel Laszczak 
5373d829045SPawel Laszczak 	ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val,
5383d829045SPawel Laszczak 					!CMD_RING_BUSY(val), 1,
5393d829045SPawel Laszczak 					CDNSP_CMD_TIMEOUT);
5403d829045SPawel Laszczak 	if (ret) {
5413d829045SPawel Laszczak 		dev_err(pdev->dev, "ERR: Timeout while waiting for command\n");
542118b2a32SPawel Laszczak 		trace_cdnsp_cmd_timeout(pdev->cmd_ring, &cmd_trb->generic);
5433d829045SPawel Laszczak 		pdev->cdnsp_state = CDNSP_STATE_DYING;
5443d829045SPawel Laszczak 		return -ETIMEDOUT;
5453d829045SPawel Laszczak 	}
5463d829045SPawel Laszczak 
5473d829045SPawel Laszczak 	event = pdev->event_ring->dequeue;
5483d829045SPawel Laszczak 	event_deq_seg = pdev->event_ring->deq_seg;
5493d829045SPawel Laszczak 	cycle_state = pdev->event_ring->cycle_state;
5503d829045SPawel Laszczak 
5513d829045SPawel Laszczak 	cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb);
5523d829045SPawel Laszczak 	if (!cmd_deq_dma)
5533d829045SPawel Laszczak 		return -EINVAL;
5543d829045SPawel Laszczak 
5553d829045SPawel Laszczak 	while (1) {
5563d829045SPawel Laszczak 		flags = le32_to_cpu(event->event_cmd.flags);
5573d829045SPawel Laszczak 
5583d829045SPawel Laszczak 		/* Check the owner of the TRB. */
5593d829045SPawel Laszczak 		if ((flags & TRB_CYCLE) != cycle_state)
5603d829045SPawel Laszczak 			return -EINVAL;
5613d829045SPawel Laszczak 
5623d829045SPawel Laszczak 		cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb);
5633d829045SPawel Laszczak 
5643d829045SPawel Laszczak 		/*
5653d829045SPawel Laszczak 		 * Check whether the completion event is for last queued
5663d829045SPawel Laszczak 		 * command.
5673d829045SPawel Laszczak 		 */
5683d829045SPawel Laszczak 		if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION ||
5693d829045SPawel Laszczak 		    cmd_dma != (u64)cmd_deq_dma) {
5703d829045SPawel Laszczak 			if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
5713d829045SPawel Laszczak 				event++;
5723d829045SPawel Laszczak 				continue;
5733d829045SPawel Laszczak 			}
5743d829045SPawel Laszczak 
5753d829045SPawel Laszczak 			if (cdnsp_last_trb_on_ring(pdev->event_ring,
5763d829045SPawel Laszczak 						   event_deq_seg, event))
5773d829045SPawel Laszczak 				cycle_state ^= 1;
5783d829045SPawel Laszczak 
5793d829045SPawel Laszczak 			event_deq_seg = event_deq_seg->next;
5803d829045SPawel Laszczak 			event = event_deq_seg->trbs;
5813d829045SPawel Laszczak 			continue;
5823d829045SPawel Laszczak 		}
5833d829045SPawel Laszczak 
584118b2a32SPawel Laszczak 		trace_cdnsp_handle_command(pdev->cmd_ring, &cmd_trb->generic);
585118b2a32SPawel Laszczak 
5863d829045SPawel Laszczak 		pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
5873d829045SPawel Laszczak 		if (pdev->cmd.status == COMP_SUCCESS)
5883d829045SPawel Laszczak 			return 0;
5893d829045SPawel Laszczak 
5903d829045SPawel Laszczak 		return -pdev->cmd.status;
5913d829045SPawel Laszczak 	}
5923d829045SPawel Laszczak }
5933d829045SPawel Laszczak 
5943d829045SPawel Laszczak int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
5953d829045SPawel Laszczak 			struct cdnsp_ep *pep,
5963d829045SPawel Laszczak 			int value)
5973d829045SPawel Laszczak {
5983d829045SPawel Laszczak 	int ret;
5993d829045SPawel Laszczak 
600118b2a32SPawel Laszczak 	trace_cdnsp_ep_halt(value ? "Set" : "Clear");
601118b2a32SPawel Laszczak 
6023d829045SPawel Laszczak 	if (value) {
6033d829045SPawel Laszczak 		ret = cdnsp_cmd_stop_ep(pdev, pep);
6043d829045SPawel Laszczak 		if (ret)
6053d829045SPawel Laszczak 			return ret;
6063d829045SPawel Laszczak 
6073d829045SPawel Laszczak 		if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) {
6083d829045SPawel Laszczak 			cdnsp_queue_halt_endpoint(pdev, pep->idx);
6093d829045SPawel Laszczak 			cdnsp_ring_cmd_db(pdev);
6103d829045SPawel Laszczak 			ret = cdnsp_wait_for_cmd_compl(pdev);
6113d829045SPawel Laszczak 		}
6123d829045SPawel Laszczak 
6133d829045SPawel Laszczak 		pep->ep_state |= EP_HALTED;
6143d829045SPawel Laszczak 	} else {
6153d829045SPawel Laszczak 		/*
6163d829045SPawel Laszczak 		 * In device mode driver can call reset endpoint command
6173d829045SPawel Laszczak 		 * from any endpoint state.
6183d829045SPawel Laszczak 		 */
6193d829045SPawel Laszczak 		cdnsp_queue_reset_ep(pdev, pep->idx);
6203d829045SPawel Laszczak 		cdnsp_ring_cmd_db(pdev);
6213d829045SPawel Laszczak 		ret = cdnsp_wait_for_cmd_compl(pdev);
622118b2a32SPawel Laszczak 		trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
623118b2a32SPawel Laszczak 
6243d829045SPawel Laszczak 		if (ret)
6253d829045SPawel Laszczak 			return ret;
6263d829045SPawel Laszczak 
6273d829045SPawel Laszczak 		pep->ep_state &= ~EP_HALTED;
6283d829045SPawel Laszczak 
6293d829045SPawel Laszczak 		if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
6303d829045SPawel Laszczak 			cdnsp_ring_doorbell_for_active_rings(pdev, pep);
6313d829045SPawel Laszczak 
6323d829045SPawel Laszczak 		pep->ep_state &= ~EP_WEDGE;
6333d829045SPawel Laszczak 	}
6343d829045SPawel Laszczak 
6353d829045SPawel Laszczak 	return 0;
6363d829045SPawel Laszczak }
6373d829045SPawel Laszczak 
6383d829045SPawel Laszczak static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
6393d829045SPawel Laszczak 					  struct cdnsp_ep *pep)
6403d829045SPawel Laszczak {
6413d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
6423d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
6433d829045SPawel Laszczak 	int ret = 0;
6443d829045SPawel Laszczak 	u32 ep_sts;
6453d829045SPawel Laszczak 	int i;
6463d829045SPawel Laszczak 
6473d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
6483d829045SPawel Laszczak 
6493d829045SPawel Laszczak 	/* Don't issue the command if there's no endpoints to update. */
6503d829045SPawel Laszczak 	if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
6513d829045SPawel Laszczak 		return 0;
6523d829045SPawel Laszczak 
6533d829045SPawel Laszczak 	ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
6543d829045SPawel Laszczak 	ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
6553d829045SPawel Laszczak 	ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
6563d829045SPawel Laszczak 
6573d829045SPawel Laszczak 	/* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
6583d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
6593d829045SPawel Laszczak 	for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
6603d829045SPawel Laszczak 		__le32 le32 = cpu_to_le32(BIT(i));
6613d829045SPawel Laszczak 
6623d829045SPawel Laszczak 		if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
6633d829045SPawel Laszczak 		    (ctrl_ctx->add_flags & le32) || i == 1) {
6643d829045SPawel Laszczak 			slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
6653d829045SPawel Laszczak 			slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
6663d829045SPawel Laszczak 			break;
6673d829045SPawel Laszczak 		}
6683d829045SPawel Laszczak 	}
6693d829045SPawel Laszczak 
6703d829045SPawel Laszczak 	ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
6713d829045SPawel Laszczak 
6723d829045SPawel Laszczak 	if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
6733d829045SPawel Laszczak 	     ep_sts == EP_STATE_DISABLED) ||
6743d829045SPawel Laszczak 	    (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
6753d829045SPawel Laszczak 		ret = cdnsp_configure_endpoint(pdev);
6763d829045SPawel Laszczak 
677118b2a32SPawel Laszczak 	trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
678118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
679118b2a32SPawel Laszczak 
6803d829045SPawel Laszczak 	cdnsp_zero_in_ctx(pdev);
6813d829045SPawel Laszczak 
6823d829045SPawel Laszczak 	return ret;
6833d829045SPawel Laszczak }
6843d829045SPawel Laszczak 
6853d829045SPawel Laszczak /*
6863d829045SPawel Laszczak  * This submits a Reset Device Command, which will set the device state to 0,
6873d829045SPawel Laszczak  * set the device address to 0, and disable all the endpoints except the default
6883d829045SPawel Laszczak  * control endpoint. The USB core should come back and call
6893d829045SPawel Laszczak  * cdnsp_setup_device(), and then re-set up the configuration.
6903d829045SPawel Laszczak  */
6913d829045SPawel Laszczak int cdnsp_reset_device(struct cdnsp_device *pdev)
6923d829045SPawel Laszczak {
6933d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
6943d829045SPawel Laszczak 	int slot_state;
6953d829045SPawel Laszczak 	int ret, i;
6963d829045SPawel Laszczak 
6973d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
6983d829045SPawel Laszczak 	slot_ctx->dev_info = 0;
6993d829045SPawel Laszczak 	pdev->device_address = 0;
7003d829045SPawel Laszczak 
7013d829045SPawel Laszczak 	/* If device is not setup, there is no point in resetting it. */
7023d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
7033d829045SPawel Laszczak 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
704118b2a32SPawel Laszczak 	trace_cdnsp_reset_device(slot_ctx);
7053d829045SPawel Laszczak 
7063d829045SPawel Laszczak 	if (slot_state <= SLOT_STATE_DEFAULT &&
7073d829045SPawel Laszczak 	    pdev->eps[0].ep_state & EP_HALTED) {
7083d829045SPawel Laszczak 		cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
7093d829045SPawel Laszczak 	}
7103d829045SPawel Laszczak 
7113d829045SPawel Laszczak 	/*
7123d829045SPawel Laszczak 	 * During Reset Device command controller shall transition the
7133d829045SPawel Laszczak 	 * endpoint ep0 to the Running State.
7143d829045SPawel Laszczak 	 */
7153d829045SPawel Laszczak 	pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
7163d829045SPawel Laszczak 	pdev->eps[0].ep_state |= EP_ENABLED;
7173d829045SPawel Laszczak 
7183d829045SPawel Laszczak 	if (slot_state <= SLOT_STATE_DEFAULT)
7193d829045SPawel Laszczak 		return 0;
7203d829045SPawel Laszczak 
7213d829045SPawel Laszczak 	cdnsp_queue_reset_device(pdev);
7223d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
7233d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
7243d829045SPawel Laszczak 
7253d829045SPawel Laszczak 	/*
7263d829045SPawel Laszczak 	 * After Reset Device command all not default endpoints
7273d829045SPawel Laszczak 	 * are in Disabled state.
7283d829045SPawel Laszczak 	 */
7293d829045SPawel Laszczak 	for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
73010076de3SPawel Laszczak 		pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
7313d829045SPawel Laszczak 
732118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
733118b2a32SPawel Laszczak 
7343d829045SPawel Laszczak 	if (ret)
7353d829045SPawel Laszczak 		dev_err(pdev->dev, "Reset device failed with error code %d",
7363d829045SPawel Laszczak 			ret);
7373d829045SPawel Laszczak 
7383d829045SPawel Laszczak 	return ret;
7393d829045SPawel Laszczak }
7403d829045SPawel Laszczak 
7413d829045SPawel Laszczak /*
7423d829045SPawel Laszczak  * Sets the MaxPStreams field and the Linear Stream Array field.
7433d829045SPawel Laszczak  * Sets the dequeue pointer to the stream context array.
7443d829045SPawel Laszczak  */
7453d829045SPawel Laszczak static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
7463d829045SPawel Laszczak 					     struct cdnsp_ep_ctx *ep_ctx,
7473d829045SPawel Laszczak 					     struct cdnsp_stream_info *stream_info)
7483d829045SPawel Laszczak {
7493d829045SPawel Laszczak 	u32 max_primary_streams;
7503d829045SPawel Laszczak 
7513d829045SPawel Laszczak 	/* MaxPStreams is the number of stream context array entries, not the
7523d829045SPawel Laszczak 	 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
7533d829045SPawel Laszczak 	 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
7543d829045SPawel Laszczak 	 */
7553d829045SPawel Laszczak 	max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
7563d829045SPawel Laszczak 	ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
7573d829045SPawel Laszczak 	ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
7583d829045SPawel Laszczak 				       | EP_HAS_LSA);
7593d829045SPawel Laszczak 	ep_ctx->deq  = cpu_to_le64(stream_info->ctx_array_dma);
7603d829045SPawel Laszczak }
7613d829045SPawel Laszczak 
7623d829045SPawel Laszczak /*
7633d829045SPawel Laszczak  * The drivers use this function to prepare a bulk endpoints to use streams.
7643d829045SPawel Laszczak  *
7653d829045SPawel Laszczak  * Don't allow the call to succeed if endpoint only supports one stream
7663d829045SPawel Laszczak  * (which means it doesn't support streams at all).
7673d829045SPawel Laszczak  */
7683d829045SPawel Laszczak int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
7693d829045SPawel Laszczak {
7703d829045SPawel Laszczak 	unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
7713d829045SPawel Laszczak 	unsigned int num_stream_ctxs;
7723d829045SPawel Laszczak 	int ret;
7733d829045SPawel Laszczak 
7743d829045SPawel Laszczak 	if (num_streams ==  0)
7753d829045SPawel Laszczak 		return 0;
7763d829045SPawel Laszczak 
7773d829045SPawel Laszczak 	if (num_streams > STREAM_NUM_STREAMS)
7783d829045SPawel Laszczak 		return -EINVAL;
7793d829045SPawel Laszczak 
7803d829045SPawel Laszczak 	/*
7813d829045SPawel Laszczak 	 * Add two to the number of streams requested to account for
7823d829045SPawel Laszczak 	 * stream 0 that is reserved for controller usage and one additional
7833d829045SPawel Laszczak 	 * for TASK SET FULL response.
7843d829045SPawel Laszczak 	 */
7853d829045SPawel Laszczak 	num_streams += 2;
7863d829045SPawel Laszczak 
7873d829045SPawel Laszczak 	/* The stream context array size must be a power of two */
7883d829045SPawel Laszczak 	num_stream_ctxs = roundup_pow_of_two(num_streams);
7893d829045SPawel Laszczak 
790118b2a32SPawel Laszczak 	trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
791118b2a32SPawel Laszczak 
7923d829045SPawel Laszczak 	ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
7933d829045SPawel Laszczak 	if (ret)
7943d829045SPawel Laszczak 		return ret;
7953d829045SPawel Laszczak 
7963d829045SPawel Laszczak 	cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
7973d829045SPawel Laszczak 
7983d829045SPawel Laszczak 	pep->ep_state |= EP_HAS_STREAMS;
7993d829045SPawel Laszczak 	pep->stream_info.td_count = 0;
8003d829045SPawel Laszczak 	pep->stream_info.first_prime_det = 0;
8013d829045SPawel Laszczak 
8023d829045SPawel Laszczak 	/* Subtract 1 for stream 0, which drivers can't use. */
8033d829045SPawel Laszczak 	return num_streams - 1;
8043d829045SPawel Laszczak }
8053d829045SPawel Laszczak 
8063d829045SPawel Laszczak int cdnsp_disable_slot(struct cdnsp_device *pdev)
8073d829045SPawel Laszczak {
8083d829045SPawel Laszczak 	int ret;
8093d829045SPawel Laszczak 
8103d829045SPawel Laszczak 	cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
8113d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8123d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8133d829045SPawel Laszczak 
8143d829045SPawel Laszczak 	pdev->slot_id = 0;
8153d829045SPawel Laszczak 	pdev->active_port = NULL;
8163d829045SPawel Laszczak 
817118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
818118b2a32SPawel Laszczak 
8193d829045SPawel Laszczak 	memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
8203d829045SPawel Laszczak 	memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
8213d829045SPawel Laszczak 
8223d829045SPawel Laszczak 	return ret;
8233d829045SPawel Laszczak }
8243d829045SPawel Laszczak 
8253d829045SPawel Laszczak int cdnsp_enable_slot(struct cdnsp_device *pdev)
8263d829045SPawel Laszczak {
8273d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
8283d829045SPawel Laszczak 	int slot_state;
8293d829045SPawel Laszczak 	int ret;
8303d829045SPawel Laszczak 
8313d829045SPawel Laszczak 	/* If device is not setup, there is no point in resetting it */
8323d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
8333d829045SPawel Laszczak 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
8343d829045SPawel Laszczak 
8353d829045SPawel Laszczak 	if (slot_state != SLOT_STATE_DISABLED)
8363d829045SPawel Laszczak 		return 0;
8373d829045SPawel Laszczak 
8383d829045SPawel Laszczak 	cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
8393d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8403d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8413d829045SPawel Laszczak 	if (ret)
842118b2a32SPawel Laszczak 		goto show_trace;
8433d829045SPawel Laszczak 
8443d829045SPawel Laszczak 	pdev->slot_id = 1;
8453d829045SPawel Laszczak 
846118b2a32SPawel Laszczak show_trace:
847118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
848118b2a32SPawel Laszczak 
849118b2a32SPawel Laszczak 	return ret;
8503d829045SPawel Laszczak }
8513d829045SPawel Laszczak 
8523d829045SPawel Laszczak /*
8533d829045SPawel Laszczak  * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
8543d829045SPawel Laszczak  * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
8553d829045SPawel Laszczak  */
8563d829045SPawel Laszczak int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
8573d829045SPawel Laszczak {
8583d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
8593d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
8603d829045SPawel Laszczak 	int dev_state = 0;
8613d829045SPawel Laszczak 	int ret;
8623d829045SPawel Laszczak 
863118b2a32SPawel Laszczak 	if (!pdev->slot_id) {
864118b2a32SPawel Laszczak 		trace_cdnsp_slot_id("incorrect");
8653d829045SPawel Laszczak 		return -EINVAL;
866118b2a32SPawel Laszczak 	}
8673d829045SPawel Laszczak 
8683d829045SPawel Laszczak 	if (!pdev->active_port->port_num)
8693d829045SPawel Laszczak 		return -EINVAL;
8703d829045SPawel Laszczak 
8713d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
8723d829045SPawel Laszczak 	dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
8733d829045SPawel Laszczak 
874118b2a32SPawel Laszczak 	if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
875118b2a32SPawel Laszczak 		trace_cdnsp_slot_already_in_default(slot_ctx);
8763d829045SPawel Laszczak 		return 0;
877118b2a32SPawel Laszczak 	}
8783d829045SPawel Laszczak 
8793d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
8803d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
8813d829045SPawel Laszczak 
8823d829045SPawel Laszczak 	if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
8833d829045SPawel Laszczak 		ret = cdnsp_setup_addressable_priv_dev(pdev);
8843d829045SPawel Laszczak 		if (ret)
8853d829045SPawel Laszczak 			return ret;
8863d829045SPawel Laszczak 	}
8873d829045SPawel Laszczak 
8883d829045SPawel Laszczak 	cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
8893d829045SPawel Laszczak 
8903d829045SPawel Laszczak 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
8913d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
8923d829045SPawel Laszczak 
893118b2a32SPawel Laszczak 	trace_cdnsp_setup_device_slot(slot_ctx);
894118b2a32SPawel Laszczak 
8953d829045SPawel Laszczak 	cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
8963d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8973d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8983d829045SPawel Laszczak 
899118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
900118b2a32SPawel Laszczak 
9013d829045SPawel Laszczak 	/* Zero the input context control for later use. */
9023d829045SPawel Laszczak 	ctrl_ctx->add_flags = 0;
9033d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
9043d829045SPawel Laszczak 
9053d829045SPawel Laszczak 	return ret;
9063d829045SPawel Laszczak }
9073d829045SPawel Laszczak 
9083d829045SPawel Laszczak void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
9093d829045SPawel Laszczak 				 struct usb_request *req,
9103d829045SPawel Laszczak 				 int enable)
9113d829045SPawel Laszczak {
9123d829045SPawel Laszczak 	if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
9133d829045SPawel Laszczak 		return;
9143d829045SPawel Laszczak 
915118b2a32SPawel Laszczak 	trace_cdnsp_lpm(enable);
916118b2a32SPawel Laszczak 
9173d829045SPawel Laszczak 	if (enable)
9183d829045SPawel Laszczak 		writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
9193d829045SPawel Laszczak 		       &pdev->active_port->regs->portpmsc);
9203d829045SPawel Laszczak 	else
9213d829045SPawel Laszczak 		writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
9223d829045SPawel Laszczak }
9233d829045SPawel Laszczak 
9243d829045SPawel Laszczak static int cdnsp_get_frame(struct cdnsp_device *pdev)
9253d829045SPawel Laszczak {
9263d829045SPawel Laszczak 	return readl(&pdev->run_regs->microframe_index) >> 3;
9273d829045SPawel Laszczak }
9283d829045SPawel Laszczak 
9293d829045SPawel Laszczak static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
9303d829045SPawel Laszczak 				  const struct usb_endpoint_descriptor *desc)
9313d829045SPawel Laszczak {
9323d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
9333d829045SPawel Laszczak 	struct cdnsp_device *pdev;
9343d829045SPawel Laszczak 	struct cdnsp_ep *pep;
9353d829045SPawel Laszczak 	unsigned long flags;
9363d829045SPawel Laszczak 	u32 added_ctxs;
9373d829045SPawel Laszczak 	int ret;
9383d829045SPawel Laszczak 
9393d829045SPawel Laszczak 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
9403d829045SPawel Laszczak 	    !desc->wMaxPacketSize)
9413d829045SPawel Laszczak 		return -EINVAL;
9423d829045SPawel Laszczak 
9433d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
9443d829045SPawel Laszczak 	pdev = pep->pdev;
94510076de3SPawel Laszczak 	pep->ep_state &= ~EP_UNCONFIGURED;
9463d829045SPawel Laszczak 
9473d829045SPawel Laszczak 	if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
9483d829045SPawel Laszczak 			  "%s is already enabled\n", pep->name))
9493d829045SPawel Laszczak 		return 0;
9503d829045SPawel Laszczak 
9513d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
9523d829045SPawel Laszczak 
9533d829045SPawel Laszczak 	added_ctxs = cdnsp_get_endpoint_flag(desc);
9543d829045SPawel Laszczak 	if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
9553d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
9563d829045SPawel Laszczak 		ret = -EINVAL;
9573d829045SPawel Laszczak 		goto unlock;
9583d829045SPawel Laszczak 	}
9593d829045SPawel Laszczak 
9603d829045SPawel Laszczak 	pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
9613d829045SPawel Laszczak 
9623d829045SPawel Laszczak 	if (pdev->gadget.speed == USB_SPEED_FULL) {
9633d829045SPawel Laszczak 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
9643d829045SPawel Laszczak 			pep->interval = desc->bInterval << 3;
9653d829045SPawel Laszczak 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
9663d829045SPawel Laszczak 			pep->interval = BIT(desc->bInterval - 1) << 3;
9673d829045SPawel Laszczak 	}
9683d829045SPawel Laszczak 
9693d829045SPawel Laszczak 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
9703d829045SPawel Laszczak 		if (pep->interval > BIT(12)) {
9713d829045SPawel Laszczak 			dev_err(pdev->dev, "bInterval %d not supported\n",
9723d829045SPawel Laszczak 				desc->bInterval);
9733d829045SPawel Laszczak 			ret = -EINVAL;
9743d829045SPawel Laszczak 			goto unlock;
9753d829045SPawel Laszczak 		}
9763d829045SPawel Laszczak 		cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
9773d829045SPawel Laszczak 	}
9783d829045SPawel Laszczak 
9793d829045SPawel Laszczak 	ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
9803d829045SPawel Laszczak 	if (ret)
9813d829045SPawel Laszczak 		goto unlock;
9823d829045SPawel Laszczak 
9833d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
9843d829045SPawel Laszczak 	ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
9853d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
9863d829045SPawel Laszczak 
9873d829045SPawel Laszczak 	ret = cdnsp_update_eps_configuration(pdev, pep);
9883d829045SPawel Laszczak 	if (ret) {
9893d829045SPawel Laszczak 		cdnsp_free_endpoint_rings(pdev, pep);
9903d829045SPawel Laszczak 		goto unlock;
9913d829045SPawel Laszczak 	}
9923d829045SPawel Laszczak 
9933d829045SPawel Laszczak 	pep->ep_state |= EP_ENABLED;
9943d829045SPawel Laszczak 	pep->ep_state &= ~EP_STOPPED;
9953d829045SPawel Laszczak 
9963d829045SPawel Laszczak unlock:
997118b2a32SPawel Laszczak 	trace_cdnsp_ep_enable_end(pep, 0);
9983d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
9993d829045SPawel Laszczak 
10003d829045SPawel Laszczak 	return ret;
10013d829045SPawel Laszczak }
10023d829045SPawel Laszczak 
10033d829045SPawel Laszczak static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
10043d829045SPawel Laszczak {
10053d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
10063d829045SPawel Laszczak 	struct cdnsp_request *preq;
10073d829045SPawel Laszczak 	struct cdnsp_device *pdev;
10083d829045SPawel Laszczak 	struct cdnsp_ep *pep;
10093d829045SPawel Laszczak 	unsigned long flags;
10103d829045SPawel Laszczak 	u32 drop_flag;
10113d829045SPawel Laszczak 	int ret = 0;
10123d829045SPawel Laszczak 
10133d829045SPawel Laszczak 	if (!ep)
10143d829045SPawel Laszczak 		return -EINVAL;
10153d829045SPawel Laszczak 
10163d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
10173d829045SPawel Laszczak 	pdev = pep->pdev;
10183d829045SPawel Laszczak 
10193d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
10203d829045SPawel Laszczak 
10213d829045SPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED)) {
10223d829045SPawel Laszczak 		dev_err(pdev->dev, "%s is already disabled\n", pep->name);
10233d829045SPawel Laszczak 		ret = -EINVAL;
10243d829045SPawel Laszczak 		goto finish;
10253d829045SPawel Laszczak 	}
10263d829045SPawel Laszczak 
10273d829045SPawel Laszczak 	pep->ep_state |= EP_DIS_IN_RROGRESS;
102810076de3SPawel Laszczak 
102910076de3SPawel Laszczak 	/* Endpoint was unconfigured by Reset Device command. */
103010076de3SPawel Laszczak 	if (!(pep->ep_state & EP_UNCONFIGURED)) {
103110076de3SPawel Laszczak 		cdnsp_cmd_stop_ep(pdev, pep);
10323d829045SPawel Laszczak 		cdnsp_cmd_flush_ep(pdev, pep);
103310076de3SPawel Laszczak 	}
10343d829045SPawel Laszczak 
10353d829045SPawel Laszczak 	/* Remove all queued USB requests. */
10363d829045SPawel Laszczak 	while (!list_empty(&pep->pending_list)) {
10373d829045SPawel Laszczak 		preq = next_request(&pep->pending_list);
10383d829045SPawel Laszczak 		cdnsp_ep_dequeue(pep, preq);
10393d829045SPawel Laszczak 	}
10403d829045SPawel Laszczak 
10413d829045SPawel Laszczak 	cdnsp_invalidate_ep_events(pdev, pep);
10423d829045SPawel Laszczak 
10433d829045SPawel Laszczak 	pep->ep_state &= ~EP_DIS_IN_RROGRESS;
10443d829045SPawel Laszczak 	drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
10453d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
10463d829045SPawel Laszczak 	ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
10473d829045SPawel Laszczak 	ctrl_ctx->add_flags = 0;
10483d829045SPawel Laszczak 
10493d829045SPawel Laszczak 	cdnsp_endpoint_zero(pdev, pep);
10503d829045SPawel Laszczak 
105110076de3SPawel Laszczak 	if (!(pep->ep_state & EP_UNCONFIGURED))
10523d829045SPawel Laszczak 		ret = cdnsp_update_eps_configuration(pdev, pep);
105310076de3SPawel Laszczak 
10543d829045SPawel Laszczak 	cdnsp_free_endpoint_rings(pdev, pep);
10553d829045SPawel Laszczak 
105610076de3SPawel Laszczak 	pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
10573d829045SPawel Laszczak 	pep->ep_state |= EP_STOPPED;
10583d829045SPawel Laszczak 
10593d829045SPawel Laszczak finish:
1060118b2a32SPawel Laszczak 	trace_cdnsp_ep_disable_end(pep, 0);
10613d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
10623d829045SPawel Laszczak 
10633d829045SPawel Laszczak 	return ret;
10643d829045SPawel Laszczak }
10653d829045SPawel Laszczak 
10663d829045SPawel Laszczak static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
10673d829045SPawel Laszczak 							 gfp_t gfp_flags)
10683d829045SPawel Laszczak {
10693d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
10703d829045SPawel Laszczak 	struct cdnsp_request *preq;
10713d829045SPawel Laszczak 
10723d829045SPawel Laszczak 	preq = kzalloc(sizeof(*preq), gfp_flags);
10733d829045SPawel Laszczak 	if (!preq)
10743d829045SPawel Laszczak 		return NULL;
10753d829045SPawel Laszczak 
10763d829045SPawel Laszczak 	preq->epnum = pep->number;
10773d829045SPawel Laszczak 	preq->pep = pep;
10783d829045SPawel Laszczak 
1079118b2a32SPawel Laszczak 	trace_cdnsp_alloc_request(preq);
1080118b2a32SPawel Laszczak 
10813d829045SPawel Laszczak 	return &preq->request;
10823d829045SPawel Laszczak }
10833d829045SPawel Laszczak 
10843d829045SPawel Laszczak static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
10853d829045SPawel Laszczak 					 struct usb_request *request)
10863d829045SPawel Laszczak {
10873d829045SPawel Laszczak 	struct cdnsp_request *preq = to_cdnsp_request(request);
10883d829045SPawel Laszczak 
1089118b2a32SPawel Laszczak 	trace_cdnsp_free_request(preq);
10903d829045SPawel Laszczak 	kfree(preq);
10913d829045SPawel Laszczak }
10923d829045SPawel Laszczak 
10933d829045SPawel Laszczak static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
10943d829045SPawel Laszczak 				 struct usb_request *request,
10953d829045SPawel Laszczak 				 gfp_t gfp_flags)
10963d829045SPawel Laszczak {
10973d829045SPawel Laszczak 	struct cdnsp_request *preq;
10983d829045SPawel Laszczak 	struct cdnsp_device *pdev;
10993d829045SPawel Laszczak 	struct cdnsp_ep *pep;
11003d829045SPawel Laszczak 	unsigned long flags;
11013d829045SPawel Laszczak 	int ret;
11023d829045SPawel Laszczak 
11033d829045SPawel Laszczak 	if (!request || !ep)
11043d829045SPawel Laszczak 		return -EINVAL;
11053d829045SPawel Laszczak 
11063d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
11073d829045SPawel Laszczak 	pdev = pep->pdev;
11083d829045SPawel Laszczak 
11093d829045SPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED)) {
11103d829045SPawel Laszczak 		dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
11113d829045SPawel Laszczak 			pep->name);
11123d829045SPawel Laszczak 		return -EINVAL;
11133d829045SPawel Laszczak 	}
11143d829045SPawel Laszczak 
11153d829045SPawel Laszczak 	preq = to_cdnsp_request(request);
11163d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11173d829045SPawel Laszczak 	ret = cdnsp_ep_enqueue(pep, preq);
11183d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11193d829045SPawel Laszczak 
11203d829045SPawel Laszczak 	return ret;
11213d829045SPawel Laszczak }
11223d829045SPawel Laszczak 
11233d829045SPawel Laszczak static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
11243d829045SPawel Laszczak 				   struct usb_request *request)
11253d829045SPawel Laszczak {
11263d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11273d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
11283d829045SPawel Laszczak 	unsigned long flags;
11293d829045SPawel Laszczak 	int ret;
11303d829045SPawel Laszczak 
11313d829045SPawel Laszczak 	if (!pep->endpoint.desc) {
11323d829045SPawel Laszczak 		dev_err(pdev->dev,
11333d829045SPawel Laszczak 			"%s: can't dequeue to disabled endpoint\n",
11343d829045SPawel Laszczak 			pep->name);
11353d829045SPawel Laszczak 		return -ESHUTDOWN;
11363d829045SPawel Laszczak 	}
11373d829045SPawel Laszczak 
1138cf97d7afSPawel Laszczak 	/* Requests has been dequeued during disabling endpoint. */
1139cf97d7afSPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED))
1140cf97d7afSPawel Laszczak 		return 0;
1141cf97d7afSPawel Laszczak 
11423d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11433d829045SPawel Laszczak 	ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
11443d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11453d829045SPawel Laszczak 
11463d829045SPawel Laszczak 	return ret;
11473d829045SPawel Laszczak }
11483d829045SPawel Laszczak 
11493d829045SPawel Laszczak static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
11503d829045SPawel Laszczak {
11513d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11523d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
11533d829045SPawel Laszczak 	struct cdnsp_request *preq;
11543d829045SPawel Laszczak 	unsigned long flags = 0;
11553d829045SPawel Laszczak 	int ret;
11563d829045SPawel Laszczak 
11573d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11583d829045SPawel Laszczak 
11593d829045SPawel Laszczak 	preq = next_request(&pep->pending_list);
11603d829045SPawel Laszczak 	if (value) {
11613d829045SPawel Laszczak 		if (preq) {
1162118b2a32SPawel Laszczak 			trace_cdnsp_ep_busy_try_halt_again(pep, 0);
11633d829045SPawel Laszczak 			ret = -EAGAIN;
11643d829045SPawel Laszczak 			goto done;
11653d829045SPawel Laszczak 		}
11663d829045SPawel Laszczak 	}
11673d829045SPawel Laszczak 
11683d829045SPawel Laszczak 	ret = cdnsp_halt_endpoint(pdev, pep, value);
11693d829045SPawel Laszczak 
11703d829045SPawel Laszczak done:
11713d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11723d829045SPawel Laszczak 	return ret;
11733d829045SPawel Laszczak }
11743d829045SPawel Laszczak 
11753d829045SPawel Laszczak static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
11763d829045SPawel Laszczak {
11773d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11783d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
11793d829045SPawel Laszczak 	unsigned long flags = 0;
11803d829045SPawel Laszczak 	int ret;
11813d829045SPawel Laszczak 
11823d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11833d829045SPawel Laszczak 	pep->ep_state |= EP_WEDGE;
11843d829045SPawel Laszczak 	ret = cdnsp_halt_endpoint(pdev, pep, 1);
11853d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11863d829045SPawel Laszczak 
11873d829045SPawel Laszczak 	return ret;
11883d829045SPawel Laszczak }
11893d829045SPawel Laszczak 
11903d829045SPawel Laszczak static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
11913d829045SPawel Laszczak 	.enable		= cdnsp_gadget_ep_enable,
11923d829045SPawel Laszczak 	.disable	= cdnsp_gadget_ep_disable,
11933d829045SPawel Laszczak 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
11943d829045SPawel Laszczak 	.free_request	= cdnsp_gadget_ep_free_request,
11953d829045SPawel Laszczak 	.queue		= cdnsp_gadget_ep_queue,
11963d829045SPawel Laszczak 	.dequeue	= cdnsp_gadget_ep_dequeue,
11973d829045SPawel Laszczak 	.set_halt	= cdnsp_gadget_ep_set_halt,
11983d829045SPawel Laszczak 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
11993d829045SPawel Laszczak };
12003d829045SPawel Laszczak 
12013d829045SPawel Laszczak static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
12023d829045SPawel Laszczak 	.enable		= cdnsp_gadget_ep_enable,
12033d829045SPawel Laszczak 	.disable	= cdnsp_gadget_ep_disable,
12043d829045SPawel Laszczak 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
12053d829045SPawel Laszczak 	.free_request	= cdnsp_gadget_ep_free_request,
12063d829045SPawel Laszczak 	.queue		= cdnsp_gadget_ep_queue,
12073d829045SPawel Laszczak 	.dequeue	= cdnsp_gadget_ep_dequeue,
12083d829045SPawel Laszczak 	.set_halt	= cdnsp_gadget_ep_set_halt,
12093d829045SPawel Laszczak 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
12103d829045SPawel Laszczak };
12113d829045SPawel Laszczak 
12123d829045SPawel Laszczak void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
12133d829045SPawel Laszczak 			   struct cdnsp_request *preq,
12143d829045SPawel Laszczak 			   int status)
12153d829045SPawel Laszczak {
12163d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
12173d829045SPawel Laszczak 
12183d829045SPawel Laszczak 	list_del(&preq->list);
12193d829045SPawel Laszczak 
12203d829045SPawel Laszczak 	if (preq->request.status == -EINPROGRESS)
12213d829045SPawel Laszczak 		preq->request.status = status;
12223d829045SPawel Laszczak 
12233d829045SPawel Laszczak 	usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
12243d829045SPawel Laszczak 					preq->direction);
12253d829045SPawel Laszczak 
1226118b2a32SPawel Laszczak 	trace_cdnsp_request_giveback(preq);
1227118b2a32SPawel Laszczak 
12283d829045SPawel Laszczak 	if (preq != &pdev->ep0_preq) {
12293d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
12303d829045SPawel Laszczak 		usb_gadget_giveback_request(&pep->endpoint, &preq->request);
12313d829045SPawel Laszczak 		spin_lock(&pdev->lock);
12323d829045SPawel Laszczak 	}
12333d829045SPawel Laszczak }
12343d829045SPawel Laszczak 
12353d829045SPawel Laszczak static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
12363d829045SPawel Laszczak 	.bLength =		USB_DT_ENDPOINT_SIZE,
12373d829045SPawel Laszczak 	.bDescriptorType =	USB_DT_ENDPOINT,
12383d829045SPawel Laszczak 	.bmAttributes =		USB_ENDPOINT_XFER_CONTROL,
12393d829045SPawel Laszczak };
12403d829045SPawel Laszczak 
12413d829045SPawel Laszczak static int cdnsp_run(struct cdnsp_device *pdev,
12423d829045SPawel Laszczak 		     enum usb_device_speed speed)
12433d829045SPawel Laszczak {
12443d829045SPawel Laszczak 	u32 fs_speed = 0;
12453d829045SPawel Laszczak 	u64 temp_64;
12463d829045SPawel Laszczak 	u32 temp;
12473d829045SPawel Laszczak 	int ret;
12483d829045SPawel Laszczak 
12493d829045SPawel Laszczak 	temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
12503d829045SPawel Laszczak 	temp_64 &= ~ERST_PTR_MASK;
12513d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_control);
12523d829045SPawel Laszczak 	temp &= ~IMOD_INTERVAL_MASK;
12533d829045SPawel Laszczak 	temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
12543d829045SPawel Laszczak 	writel(temp, &pdev->ir_set->irq_control);
12553d829045SPawel Laszczak 
12563d829045SPawel Laszczak 	temp = readl(&pdev->port3x_regs->mode_addr);
12573d829045SPawel Laszczak 
12583d829045SPawel Laszczak 	switch (speed) {
12593d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
12603d829045SPawel Laszczak 		temp |= CFG_3XPORT_SSP_SUPPORT;
12613d829045SPawel Laszczak 		break;
12623d829045SPawel Laszczak 	case USB_SPEED_SUPER:
12633d829045SPawel Laszczak 		temp &= ~CFG_3XPORT_SSP_SUPPORT;
12643d829045SPawel Laszczak 		break;
12653d829045SPawel Laszczak 	case USB_SPEED_HIGH:
12663d829045SPawel Laszczak 		break;
12673d829045SPawel Laszczak 	case USB_SPEED_FULL:
12683d829045SPawel Laszczak 		fs_speed = PORT_REG6_FORCE_FS;
12693d829045SPawel Laszczak 		break;
12703d829045SPawel Laszczak 	default:
12713d829045SPawel Laszczak 		dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
12723d829045SPawel Laszczak 			speed);
12733d829045SPawel Laszczak 		fallthrough;
12743d829045SPawel Laszczak 	case USB_SPEED_UNKNOWN:
12753d829045SPawel Laszczak 		/* Default to superspeed. */
12763d829045SPawel Laszczak 		speed = USB_SPEED_SUPER;
12773d829045SPawel Laszczak 		break;
12783d829045SPawel Laszczak 	}
12793d829045SPawel Laszczak 
12803d829045SPawel Laszczak 	if (speed >= USB_SPEED_SUPER) {
12813d829045SPawel Laszczak 		writel(temp, &pdev->port3x_regs->mode_addr);
12823d829045SPawel Laszczak 		cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
12833d829045SPawel Laszczak 				     XDEV_RXDETECT);
12843d829045SPawel Laszczak 	} else {
12853d829045SPawel Laszczak 		cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
12863d829045SPawel Laszczak 	}
12873d829045SPawel Laszczak 
12883d829045SPawel Laszczak 	cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
12893d829045SPawel Laszczak 			     XDEV_RXDETECT);
12903d829045SPawel Laszczak 
12913d829045SPawel Laszczak 	cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
12923d829045SPawel Laszczak 
12933d829045SPawel Laszczak 	writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
12943d829045SPawel Laszczak 
12953d829045SPawel Laszczak 	ret = cdnsp_start(pdev);
12963d829045SPawel Laszczak 	if (ret) {
12973d829045SPawel Laszczak 		ret = -ENODEV;
12983d829045SPawel Laszczak 		goto err;
12993d829045SPawel Laszczak 	}
13003d829045SPawel Laszczak 
13013d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->command);
13023d829045SPawel Laszczak 	temp |= (CMD_INTE);
13033d829045SPawel Laszczak 	writel(temp, &pdev->op_regs->command);
13043d829045SPawel Laszczak 
13053d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
13063d829045SPawel Laszczak 	writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
13073d829045SPawel Laszczak 
1308118b2a32SPawel Laszczak 	trace_cdnsp_init("Controller ready to work");
13093d829045SPawel Laszczak 	return 0;
13103d829045SPawel Laszczak err:
13113d829045SPawel Laszczak 	cdnsp_halt(pdev);
13123d829045SPawel Laszczak 	return ret;
13133d829045SPawel Laszczak }
13143d829045SPawel Laszczak 
13153d829045SPawel Laszczak static int cdnsp_gadget_udc_start(struct usb_gadget *g,
13163d829045SPawel Laszczak 				  struct usb_gadget_driver *driver)
13173d829045SPawel Laszczak {
13183d829045SPawel Laszczak 	enum usb_device_speed max_speed = driver->max_speed;
13193d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
13203d829045SPawel Laszczak 	unsigned long flags;
13213d829045SPawel Laszczak 	int ret;
13223d829045SPawel Laszczak 
13233d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
13243d829045SPawel Laszczak 	pdev->gadget_driver = driver;
13253d829045SPawel Laszczak 
13263d829045SPawel Laszczak 	/* limit speed if necessary */
13273d829045SPawel Laszczak 	max_speed = min(driver->max_speed, g->max_speed);
13283d829045SPawel Laszczak 	ret = cdnsp_run(pdev, max_speed);
13293d829045SPawel Laszczak 
13303d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
13313d829045SPawel Laszczak 
13323d829045SPawel Laszczak 	return ret;
13333d829045SPawel Laszczak }
13343d829045SPawel Laszczak 
13353d829045SPawel Laszczak /*
13363d829045SPawel Laszczak  * Update Event Ring Dequeue Pointer:
13373d829045SPawel Laszczak  * - When all events have finished
13383d829045SPawel Laszczak  * - To avoid "Event Ring Full Error" condition
13393d829045SPawel Laszczak  */
13403d829045SPawel Laszczak void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
13413d829045SPawel Laszczak 			       union cdnsp_trb *event_ring_deq,
13423d829045SPawel Laszczak 			       u8 clear_ehb)
13433d829045SPawel Laszczak {
13443d829045SPawel Laszczak 	u64 temp_64;
13453d829045SPawel Laszczak 	dma_addr_t deq;
13463d829045SPawel Laszczak 
13473d829045SPawel Laszczak 	temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
13483d829045SPawel Laszczak 
13493d829045SPawel Laszczak 	/* If necessary, update the HW's version of the event ring deq ptr. */
13503d829045SPawel Laszczak 	if (event_ring_deq != pdev->event_ring->dequeue) {
13513d829045SPawel Laszczak 		deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
13523d829045SPawel Laszczak 					    pdev->event_ring->dequeue);
13533d829045SPawel Laszczak 		temp_64 &= ERST_PTR_MASK;
13543d829045SPawel Laszczak 		temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
13553d829045SPawel Laszczak 	}
13563d829045SPawel Laszczak 
13573d829045SPawel Laszczak 	/* Clear the event handler busy flag (RW1C). */
13583d829045SPawel Laszczak 	if (clear_ehb)
13593d829045SPawel Laszczak 		temp_64 |= ERST_EHB;
13603d829045SPawel Laszczak 	else
13613d829045SPawel Laszczak 		temp_64 &= ~ERST_EHB;
13623d829045SPawel Laszczak 
13633d829045SPawel Laszczak 	cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
13643d829045SPawel Laszczak }
13653d829045SPawel Laszczak 
13663d829045SPawel Laszczak static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
13673d829045SPawel Laszczak {
13683d829045SPawel Laszczak 	struct cdnsp_segment *seg;
13693d829045SPawel Laszczak 	u64 val_64;
13703d829045SPawel Laszczak 	int i;
13713d829045SPawel Laszczak 
13723d829045SPawel Laszczak 	cdnsp_initialize_ring_info(pdev->cmd_ring);
13733d829045SPawel Laszczak 
13743d829045SPawel Laszczak 	seg = pdev->cmd_ring->first_seg;
13753d829045SPawel Laszczak 	for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
13763d829045SPawel Laszczak 		memset(seg->trbs, 0,
13773d829045SPawel Laszczak 		       sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
13783d829045SPawel Laszczak 		seg = seg->next;
13793d829045SPawel Laszczak 	}
13803d829045SPawel Laszczak 
13813d829045SPawel Laszczak 	/* Set the address in the Command Ring Control register. */
13823d829045SPawel Laszczak 	val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
13833d829045SPawel Laszczak 	val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
13843d829045SPawel Laszczak 		 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
13853d829045SPawel Laszczak 		 pdev->cmd_ring->cycle_state;
13863d829045SPawel Laszczak 	cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
13873d829045SPawel Laszczak }
13883d829045SPawel Laszczak 
13893d829045SPawel Laszczak static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
13903d829045SPawel Laszczak {
13913d829045SPawel Laszczak 	struct cdnsp_segment *event_deq_seg;
13923d829045SPawel Laszczak 	union cdnsp_trb *event_ring_deq;
13933d829045SPawel Laszczak 	union cdnsp_trb *event;
13943d829045SPawel Laszczak 	u32 cycle_bit;
13953d829045SPawel Laszczak 
13963d829045SPawel Laszczak 	event_ring_deq = pdev->event_ring->dequeue;
13973d829045SPawel Laszczak 	event_deq_seg = pdev->event_ring->deq_seg;
13983d829045SPawel Laszczak 	event = pdev->event_ring->dequeue;
13993d829045SPawel Laszczak 
14003d829045SPawel Laszczak 	/* Update ring dequeue pointer. */
14013d829045SPawel Laszczak 	while (1) {
14023d829045SPawel Laszczak 		cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
14033d829045SPawel Laszczak 
14043d829045SPawel Laszczak 		/* Does the controller or driver own the TRB? */
14053d829045SPawel Laszczak 		if (cycle_bit != pdev->event_ring->cycle_state)
14063d829045SPawel Laszczak 			break;
14073d829045SPawel Laszczak 
14083d829045SPawel Laszczak 		cdnsp_inc_deq(pdev, pdev->event_ring);
14093d829045SPawel Laszczak 
14103d829045SPawel Laszczak 		if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
14113d829045SPawel Laszczak 			event++;
14123d829045SPawel Laszczak 			continue;
14133d829045SPawel Laszczak 		}
14143d829045SPawel Laszczak 
14153d829045SPawel Laszczak 		if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
14163d829045SPawel Laszczak 					   event))
14173d829045SPawel Laszczak 			cycle_bit ^= 1;
14183d829045SPawel Laszczak 
14193d829045SPawel Laszczak 		event_deq_seg = event_deq_seg->next;
14203d829045SPawel Laszczak 		event = event_deq_seg->trbs;
14213d829045SPawel Laszczak 	}
14223d829045SPawel Laszczak 
14233d829045SPawel Laszczak 	cdnsp_update_erst_dequeue(pdev,  event_ring_deq, 1);
14243d829045SPawel Laszczak }
14253d829045SPawel Laszczak 
14263d829045SPawel Laszczak static void cdnsp_stop(struct cdnsp_device *pdev)
14273d829045SPawel Laszczak {
14283d829045SPawel Laszczak 	u32 temp;
14293d829045SPawel Laszczak 
14303d829045SPawel Laszczak 	cdnsp_cmd_flush_ep(pdev, &pdev->eps[0]);
14313d829045SPawel Laszczak 
14323d829045SPawel Laszczak 	/* Remove internally queued request for ep0. */
14333d829045SPawel Laszczak 	if (!list_empty(&pdev->eps[0].pending_list)) {
14343d829045SPawel Laszczak 		struct cdnsp_request *req;
14353d829045SPawel Laszczak 
14363d829045SPawel Laszczak 		req = next_request(&pdev->eps[0].pending_list);
14373d829045SPawel Laszczak 		if (req == &pdev->ep0_preq)
14383d829045SPawel Laszczak 			cdnsp_ep_dequeue(&pdev->eps[0], req);
14393d829045SPawel Laszczak 	}
14403d829045SPawel Laszczak 
14413d829045SPawel Laszczak 	cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
14423d829045SPawel Laszczak 	cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
14433d829045SPawel Laszczak 	cdnsp_disable_slot(pdev);
14443d829045SPawel Laszczak 	cdnsp_halt(pdev);
14453d829045SPawel Laszczak 
14463d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->status);
14473d829045SPawel Laszczak 	writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
14483d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
14493d829045SPawel Laszczak 	writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
14503d829045SPawel Laszczak 
14513d829045SPawel Laszczak 	cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
14523d829045SPawel Laszczak 	cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
14533d829045SPawel Laszczak 
14543d829045SPawel Laszczak 	/* Clear interrupt line */
14553d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
14563d829045SPawel Laszczak 	temp |= IMAN_IP;
14573d829045SPawel Laszczak 	writel(temp, &pdev->ir_set->irq_pending);
14583d829045SPawel Laszczak 
14593d829045SPawel Laszczak 	cdnsp_consume_all_events(pdev);
14603d829045SPawel Laszczak 	cdnsp_clear_cmd_ring(pdev);
1461118b2a32SPawel Laszczak 
1462118b2a32SPawel Laszczak 	trace_cdnsp_exit("Controller stopped.");
14633d829045SPawel Laszczak }
14643d829045SPawel Laszczak 
14653d829045SPawel Laszczak /*
14663d829045SPawel Laszczak  * Stop controller.
14673d829045SPawel Laszczak  * This function is called by the gadget core when the driver is removed.
14683d829045SPawel Laszczak  * Disable slot, disable IRQs, and quiesce the controller.
14693d829045SPawel Laszczak  */
14703d829045SPawel Laszczak static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
14713d829045SPawel Laszczak {
14723d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
14733d829045SPawel Laszczak 	unsigned long flags;
14743d829045SPawel Laszczak 
14753d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
14763d829045SPawel Laszczak 	cdnsp_stop(pdev);
14773d829045SPawel Laszczak 	pdev->gadget_driver = NULL;
14783d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
14793d829045SPawel Laszczak 
14803d829045SPawel Laszczak 	return 0;
14813d829045SPawel Laszczak }
14823d829045SPawel Laszczak 
14833d829045SPawel Laszczak static int cdnsp_gadget_get_frame(struct usb_gadget *g)
14843d829045SPawel Laszczak {
14853d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
14863d829045SPawel Laszczak 
14873d829045SPawel Laszczak 	return cdnsp_get_frame(pdev);
14883d829045SPawel Laszczak }
14893d829045SPawel Laszczak 
14903d829045SPawel Laszczak static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
14913d829045SPawel Laszczak {
14923d829045SPawel Laszczak 	struct cdnsp_port_regs __iomem *port_regs;
14933d829045SPawel Laszczak 	u32 portpm, portsc;
14943d829045SPawel Laszczak 
14953d829045SPawel Laszczak 	port_regs = pdev->active_port->regs;
14963d829045SPawel Laszczak 	portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
14973d829045SPawel Laszczak 
14983d829045SPawel Laszczak 	/* Remote wakeup feature is not enabled by host. */
14993d829045SPawel Laszczak 	if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
15003d829045SPawel Laszczak 		portpm = readl(&port_regs->portpmsc);
15013d829045SPawel Laszczak 
15023d829045SPawel Laszczak 		if (!(portpm & PORT_RWE))
15033d829045SPawel Laszczak 			return;
15043d829045SPawel Laszczak 	}
15053d829045SPawel Laszczak 
15063d829045SPawel Laszczak 	if (portsc == XDEV_U3 && !pdev->may_wakeup)
15073d829045SPawel Laszczak 		return;
15083d829045SPawel Laszczak 
15093d829045SPawel Laszczak 	cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
15103d829045SPawel Laszczak 
15113d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
15123d829045SPawel Laszczak }
15133d829045SPawel Laszczak 
15143d829045SPawel Laszczak static int cdnsp_gadget_wakeup(struct usb_gadget *g)
15153d829045SPawel Laszczak {
15163d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
15173d829045SPawel Laszczak 	unsigned long flags;
15183d829045SPawel Laszczak 
15193d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
15203d829045SPawel Laszczak 	__cdnsp_gadget_wakeup(pdev);
15213d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
15223d829045SPawel Laszczak 
15233d829045SPawel Laszczak 	return 0;
15243d829045SPawel Laszczak }
15253d829045SPawel Laszczak 
15263d829045SPawel Laszczak static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
15273d829045SPawel Laszczak 					int is_selfpowered)
15283d829045SPawel Laszczak {
15293d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
15303d829045SPawel Laszczak 	unsigned long flags;
15313d829045SPawel Laszczak 
15323d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
15333d829045SPawel Laszczak 	g->is_selfpowered = !!is_selfpowered;
15343d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
15353d829045SPawel Laszczak 
15363d829045SPawel Laszczak 	return 0;
15373d829045SPawel Laszczak }
15383d829045SPawel Laszczak 
15393d829045SPawel Laszczak static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
15403d829045SPawel Laszczak {
15413d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
15423d829045SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(pdev->dev);
15433d829045SPawel Laszczak 
1544118b2a32SPawel Laszczak 	trace_cdnsp_pullup(is_on);
1545118b2a32SPawel Laszczak 
15463d829045SPawel Laszczak 	if (!is_on) {
15473d829045SPawel Laszczak 		cdnsp_reset_device(pdev);
15483d829045SPawel Laszczak 		cdns_clear_vbus(cdns);
15493d829045SPawel Laszczak 	} else {
15503d829045SPawel Laszczak 		cdns_set_vbus(cdns);
15513d829045SPawel Laszczak 	}
15523d829045SPawel Laszczak 	return 0;
15533d829045SPawel Laszczak }
15543d829045SPawel Laszczak 
15557650778eSZou Wei static const struct usb_gadget_ops cdnsp_gadget_ops = {
15563d829045SPawel Laszczak 	.get_frame		= cdnsp_gadget_get_frame,
15573d829045SPawel Laszczak 	.wakeup			= cdnsp_gadget_wakeup,
15583d829045SPawel Laszczak 	.set_selfpowered	= cdnsp_gadget_set_selfpowered,
15593d829045SPawel Laszczak 	.pullup			= cdnsp_gadget_pullup,
15603d829045SPawel Laszczak 	.udc_start		= cdnsp_gadget_udc_start,
15613d829045SPawel Laszczak 	.udc_stop		= cdnsp_gadget_udc_stop,
15623d829045SPawel Laszczak };
15633d829045SPawel Laszczak 
15643d829045SPawel Laszczak static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
15653d829045SPawel Laszczak 				   struct cdnsp_ep *pep)
15663d829045SPawel Laszczak {
15673d829045SPawel Laszczak 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
15683d829045SPawel Laszczak 	int endpoints;
15693d829045SPawel Laszczak 
15703d829045SPawel Laszczak 	reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
15713d829045SPawel Laszczak 
15723d829045SPawel Laszczak 	if (!pep->direction) {
15733d829045SPawel Laszczak 		pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
15743d829045SPawel Laszczak 		pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
15753d829045SPawel Laszczak 		pep->buffering = (pep->buffering + 1) / 2;
15763d829045SPawel Laszczak 		pep->buffering_period = (pep->buffering_period + 1) / 2;
15773d829045SPawel Laszczak 		return;
15783d829045SPawel Laszczak 	}
15793d829045SPawel Laszczak 
158016e36101SPawel Laszczak 	endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
15813d829045SPawel Laszczak 
15823d829045SPawel Laszczak 	/* Set to XBUF_TX_TAG_MASK_0 register. */
15833d829045SPawel Laszczak 	reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
15843d829045SPawel Laszczak 	/* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
15853d829045SPawel Laszczak 	reg += pep->number * sizeof(u32) * 2;
15863d829045SPawel Laszczak 
15873d829045SPawel Laszczak 	pep->buffering = (readl(reg) + 1) / 2;
15883d829045SPawel Laszczak 	pep->buffering_period = pep->buffering;
15893d829045SPawel Laszczak }
15903d829045SPawel Laszczak 
15913d829045SPawel Laszczak static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
15923d829045SPawel Laszczak {
15933d829045SPawel Laszczak 	int max_streams = HCC_MAX_PSA(pdev->hcc_params);
15943d829045SPawel Laszczak 	struct cdnsp_ep *pep;
15953d829045SPawel Laszczak 	int i;
15963d829045SPawel Laszczak 
15973d829045SPawel Laszczak 	INIT_LIST_HEAD(&pdev->gadget.ep_list);
15983d829045SPawel Laszczak 
15993d829045SPawel Laszczak 	if (max_streams < STREAM_LOG_STREAMS) {
16003d829045SPawel Laszczak 		dev_err(pdev->dev, "Stream size %d not supported\n",
16013d829045SPawel Laszczak 			max_streams);
16023d829045SPawel Laszczak 		return -EINVAL;
16033d829045SPawel Laszczak 	}
16043d829045SPawel Laszczak 
16053d829045SPawel Laszczak 	max_streams = STREAM_LOG_STREAMS;
16063d829045SPawel Laszczak 
16073d829045SPawel Laszczak 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
16083d829045SPawel Laszczak 		bool direction = !(i & 1); /* Start from OUT endpoint. */
16093d829045SPawel Laszczak 		u8 epnum = ((i + 1) >> 1);
16103d829045SPawel Laszczak 
16113d829045SPawel Laszczak 		if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
16123d829045SPawel Laszczak 			continue;
16133d829045SPawel Laszczak 
16143d829045SPawel Laszczak 		pep = &pdev->eps[i];
16153d829045SPawel Laszczak 		pep->pdev = pdev;
16163d829045SPawel Laszczak 		pep->number = epnum;
16173d829045SPawel Laszczak 		pep->direction = direction; /* 0 for OUT, 1 for IN. */
16183d829045SPawel Laszczak 
16193d829045SPawel Laszczak 		/*
16203d829045SPawel Laszczak 		 * Ep0 is bidirectional, so ep0in and ep0out are represented by
16213d829045SPawel Laszczak 		 * pdev->eps[0]
16223d829045SPawel Laszczak 		 */
16233d829045SPawel Laszczak 		if (epnum == 0) {
16243d829045SPawel Laszczak 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
16253d829045SPawel Laszczak 				 epnum, "BiDir");
16263d829045SPawel Laszczak 
16273d829045SPawel Laszczak 			pep->idx = 0;
16283d829045SPawel Laszczak 			usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
16293d829045SPawel Laszczak 			pep->endpoint.maxburst = 1;
16303d829045SPawel Laszczak 			pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
16313d829045SPawel Laszczak 			pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
16323d829045SPawel Laszczak 			pep->endpoint.comp_desc = NULL;
16333d829045SPawel Laszczak 			pep->endpoint.caps.type_control = true;
16343d829045SPawel Laszczak 			pep->endpoint.caps.dir_in = true;
16353d829045SPawel Laszczak 			pep->endpoint.caps.dir_out = true;
16363d829045SPawel Laszczak 
16373d829045SPawel Laszczak 			pdev->ep0_preq.epnum = pep->number;
16383d829045SPawel Laszczak 			pdev->ep0_preq.pep = pep;
16393d829045SPawel Laszczak 			pdev->gadget.ep0 = &pep->endpoint;
16403d829045SPawel Laszczak 		} else {
16413d829045SPawel Laszczak 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
16423d829045SPawel Laszczak 				 epnum, (pep->direction) ? "in" : "out");
16433d829045SPawel Laszczak 
16443d829045SPawel Laszczak 			pep->idx =  (epnum * 2 + (direction ? 1 : 0)) - 1;
16453d829045SPawel Laszczak 			usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
16463d829045SPawel Laszczak 
16473d829045SPawel Laszczak 			pep->endpoint.max_streams = max_streams;
16483d829045SPawel Laszczak 			pep->endpoint.ops = &cdnsp_gadget_ep_ops;
16493d829045SPawel Laszczak 			list_add_tail(&pep->endpoint.ep_list,
16503d829045SPawel Laszczak 				      &pdev->gadget.ep_list);
16513d829045SPawel Laszczak 
16523d829045SPawel Laszczak 			pep->endpoint.caps.type_iso = true;
16533d829045SPawel Laszczak 			pep->endpoint.caps.type_bulk = true;
16543d829045SPawel Laszczak 			pep->endpoint.caps.type_int = true;
16553d829045SPawel Laszczak 
16563d829045SPawel Laszczak 			pep->endpoint.caps.dir_in = direction;
16573d829045SPawel Laszczak 			pep->endpoint.caps.dir_out = !direction;
16583d829045SPawel Laszczak 		}
16593d829045SPawel Laszczak 
16603d829045SPawel Laszczak 		pep->endpoint.name = pep->name;
16613d829045SPawel Laszczak 		pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
16623d829045SPawel Laszczak 		pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
16633d829045SPawel Laszczak 		cdnsp_get_ep_buffering(pdev, pep);
16643d829045SPawel Laszczak 
16653d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
16663d829045SPawel Laszczak 			"CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
16673d829045SPawel Laszczak 			"SupDir IN: %s, OUT: %s\n",
16683d829045SPawel Laszczak 			pep->name, 1024,
16693d829045SPawel Laszczak 			(pep->endpoint.caps.type_control) ? "yes" : "no",
16703d829045SPawel Laszczak 			(pep->endpoint.caps.type_int) ? "yes" : "no",
16713d829045SPawel Laszczak 			(pep->endpoint.caps.type_bulk) ? "yes" : "no",
16723d829045SPawel Laszczak 			(pep->endpoint.caps.type_iso) ? "yes" : "no",
16733d829045SPawel Laszczak 			(pep->endpoint.caps.dir_in) ? "yes" : "no",
16743d829045SPawel Laszczak 			(pep->endpoint.caps.dir_out) ? "yes" : "no");
16753d829045SPawel Laszczak 
16763d829045SPawel Laszczak 		INIT_LIST_HEAD(&pep->pending_list);
16773d829045SPawel Laszczak 	}
16783d829045SPawel Laszczak 
16793d829045SPawel Laszczak 	return 0;
16803d829045SPawel Laszczak }
16813d829045SPawel Laszczak 
16823d829045SPawel Laszczak static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
16833d829045SPawel Laszczak {
16843d829045SPawel Laszczak 	struct cdnsp_ep *pep;
16853d829045SPawel Laszczak 	int i;
16863d829045SPawel Laszczak 
16873d829045SPawel Laszczak 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
16883d829045SPawel Laszczak 		pep = &pdev->eps[i];
16893d829045SPawel Laszczak 		if (pep->number != 0 && pep->out_ctx)
16903d829045SPawel Laszczak 			list_del(&pep->endpoint.ep_list);
16913d829045SPawel Laszczak 	}
16923d829045SPawel Laszczak }
16933d829045SPawel Laszczak 
16943d829045SPawel Laszczak void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
16953d829045SPawel Laszczak {
16963d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
16973d829045SPawel Laszczak 
16983d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
16993d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17003d829045SPawel Laszczak 		pdev->gadget_driver->disconnect(&pdev->gadget);
17013d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17023d829045SPawel Laszczak 	}
17033d829045SPawel Laszczak 
17043d829045SPawel Laszczak 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
17053d829045SPawel Laszczak 	usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
17063d829045SPawel Laszczak 
17073d829045SPawel Laszczak 	pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
17083d829045SPawel Laszczak }
17093d829045SPawel Laszczak 
17103d829045SPawel Laszczak void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
17113d829045SPawel Laszczak {
17123d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
17133d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17143d829045SPawel Laszczak 		pdev->gadget_driver->suspend(&pdev->gadget);
17153d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17163d829045SPawel Laszczak 	}
17173d829045SPawel Laszczak }
17183d829045SPawel Laszczak 
17193d829045SPawel Laszczak void cdnsp_resume_gadget(struct cdnsp_device *pdev)
17203d829045SPawel Laszczak {
17213d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->resume) {
17223d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17233d829045SPawel Laszczak 		pdev->gadget_driver->resume(&pdev->gadget);
17243d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17253d829045SPawel Laszczak 	}
17263d829045SPawel Laszczak }
17273d829045SPawel Laszczak 
17283d829045SPawel Laszczak void cdnsp_irq_reset(struct cdnsp_device *pdev)
17293d829045SPawel Laszczak {
17303d829045SPawel Laszczak 	struct cdnsp_port_regs __iomem *port_regs;
17313d829045SPawel Laszczak 
17323d829045SPawel Laszczak 	cdnsp_reset_device(pdev);
17333d829045SPawel Laszczak 
17343d829045SPawel Laszczak 	port_regs = pdev->active_port->regs;
17353d829045SPawel Laszczak 	pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
17363d829045SPawel Laszczak 
17373d829045SPawel Laszczak 	spin_unlock(&pdev->lock);
17383d829045SPawel Laszczak 	usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
17393d829045SPawel Laszczak 	spin_lock(&pdev->lock);
17403d829045SPawel Laszczak 
17413d829045SPawel Laszczak 	switch (pdev->gadget.speed) {
17423d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
17433d829045SPawel Laszczak 	case USB_SPEED_SUPER:
17443d829045SPawel Laszczak 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
17453d829045SPawel Laszczak 		pdev->gadget.ep0->maxpacket = 512;
17463d829045SPawel Laszczak 		break;
17473d829045SPawel Laszczak 	case USB_SPEED_HIGH:
17483d829045SPawel Laszczak 	case USB_SPEED_FULL:
17493d829045SPawel Laszczak 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
17503d829045SPawel Laszczak 		pdev->gadget.ep0->maxpacket = 64;
17513d829045SPawel Laszczak 		break;
17523d829045SPawel Laszczak 	default:
17533d829045SPawel Laszczak 		/* Low speed is not supported. */
17543d829045SPawel Laszczak 		dev_err(pdev->dev, "Unknown device speed\n");
17553d829045SPawel Laszczak 		break;
17563d829045SPawel Laszczak 	}
17573d829045SPawel Laszczak 
17583d829045SPawel Laszczak 	cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
17593d829045SPawel Laszczak 	cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
17603d829045SPawel Laszczak 	usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
17613d829045SPawel Laszczak }
17623d829045SPawel Laszczak 
17633d829045SPawel Laszczak static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
17643d829045SPawel Laszczak {
17653d829045SPawel Laszczak 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
17663d829045SPawel Laszczak 
17673d829045SPawel Laszczak 	reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
176816e36101SPawel Laszczak 	pdev->rev_cap  = reg;
17693d829045SPawel Laszczak 
17703d829045SPawel Laszczak 	dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
177116e36101SPawel Laszczak 		 readl(&pdev->rev_cap->ctrl_revision),
177216e36101SPawel Laszczak 		 readl(&pdev->rev_cap->rtl_revision),
177316e36101SPawel Laszczak 		 readl(&pdev->rev_cap->ep_supported),
177416e36101SPawel Laszczak 		 readl(&pdev->rev_cap->rx_buff_size),
177516e36101SPawel Laszczak 		 readl(&pdev->rev_cap->tx_buff_size));
17763d829045SPawel Laszczak }
17773d829045SPawel Laszczak 
17783d829045SPawel Laszczak static int cdnsp_gen_setup(struct cdnsp_device *pdev)
17793d829045SPawel Laszczak {
17803d829045SPawel Laszczak 	int ret;
17813d829045SPawel Laszczak 	u32 reg;
17823d829045SPawel Laszczak 
17833d829045SPawel Laszczak 	pdev->cap_regs = pdev->regs;
17843d829045SPawel Laszczak 	pdev->op_regs = pdev->regs +
17853d829045SPawel Laszczak 		HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
17863d829045SPawel Laszczak 	pdev->run_regs = pdev->regs +
17873d829045SPawel Laszczak 		(readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
17883d829045SPawel Laszczak 
17893d829045SPawel Laszczak 	/* Cache read-only capability registers */
17903d829045SPawel Laszczak 	pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
17913d829045SPawel Laszczak 	pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
17923d829045SPawel Laszczak 	pdev->hci_version = HC_VERSION(pdev->hcc_params);
17933d829045SPawel Laszczak 	pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
17943d829045SPawel Laszczak 
17953d829045SPawel Laszczak 	cdnsp_get_rev_cap(pdev);
17963d829045SPawel Laszczak 
17973d829045SPawel Laszczak 	/* Make sure the Device Controller is halted. */
17983d829045SPawel Laszczak 	ret = cdnsp_halt(pdev);
17993d829045SPawel Laszczak 	if (ret)
18003d829045SPawel Laszczak 		return ret;
18013d829045SPawel Laszczak 
18023d829045SPawel Laszczak 	/* Reset the internal controller memory state and registers. */
18033d829045SPawel Laszczak 	ret = cdnsp_reset(pdev);
18043d829045SPawel Laszczak 	if (ret)
18053d829045SPawel Laszczak 		return ret;
18063d829045SPawel Laszczak 
18073d829045SPawel Laszczak 	/*
18083d829045SPawel Laszczak 	 * Set dma_mask and coherent_dma_mask to 64-bits,
18093d829045SPawel Laszczak 	 * if controller supports 64-bit addressing.
18103d829045SPawel Laszczak 	 */
18113d829045SPawel Laszczak 	if (HCC_64BIT_ADDR(pdev->hcc_params) &&
18123d829045SPawel Laszczak 	    !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
18133d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
18143d829045SPawel Laszczak 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
18153d829045SPawel Laszczak 	} else {
18163d829045SPawel Laszczak 		/*
18173d829045SPawel Laszczak 		 * This is to avoid error in cases where a 32-bit USB
18183d829045SPawel Laszczak 		 * controller is used on a 64-bit capable system.
18193d829045SPawel Laszczak 		 */
18203d829045SPawel Laszczak 		ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
18213d829045SPawel Laszczak 		if (ret)
18223d829045SPawel Laszczak 			return ret;
18233d829045SPawel Laszczak 
18243d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
18253d829045SPawel Laszczak 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
18263d829045SPawel Laszczak 	}
18273d829045SPawel Laszczak 
18283d829045SPawel Laszczak 	spin_lock_init(&pdev->lock);
18293d829045SPawel Laszczak 
1830dc68ba6cSPawel Laszczak 	ret = cdnsp_mem_init(pdev);
18313d829045SPawel Laszczak 	if (ret)
18323d829045SPawel Laszczak 		return ret;
18333d829045SPawel Laszczak 
18343d829045SPawel Laszczak 	/*
18353d829045SPawel Laszczak 	 * Software workaround for U1: after transition
18363d829045SPawel Laszczak 	 * to U1 the controller starts gating clock, and in some cases,
18373d829045SPawel Laszczak 	 * it causes that controller stack.
18383d829045SPawel Laszczak 	 */
18393d829045SPawel Laszczak 	reg = readl(&pdev->port3x_regs->mode_2);
18403d829045SPawel Laszczak 	reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
18413d829045SPawel Laszczak 	writel(reg, &pdev->port3x_regs->mode_2);
18423d829045SPawel Laszczak 
18433d829045SPawel Laszczak 	return 0;
18443d829045SPawel Laszczak }
18453d829045SPawel Laszczak 
18463d829045SPawel Laszczak static int __cdnsp_gadget_init(struct cdns *cdns)
18473d829045SPawel Laszczak {
18483d829045SPawel Laszczak 	struct cdnsp_device *pdev;
18493d829045SPawel Laszczak 	u32 max_speed;
18503d829045SPawel Laszczak 	int ret = -ENOMEM;
18513d829045SPawel Laszczak 
18523d829045SPawel Laszczak 	cdns_drd_gadget_on(cdns);
18533d829045SPawel Laszczak 
18543d829045SPawel Laszczak 	pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
18553d829045SPawel Laszczak 	if (!pdev)
18563d829045SPawel Laszczak 		return -ENOMEM;
18573d829045SPawel Laszczak 
18583d829045SPawel Laszczak 	pm_runtime_get_sync(cdns->dev);
18593d829045SPawel Laszczak 
18603d829045SPawel Laszczak 	cdns->gadget_dev = pdev;
18613d829045SPawel Laszczak 	pdev->dev = cdns->dev;
18623d829045SPawel Laszczak 	pdev->regs = cdns->dev_regs;
18633d829045SPawel Laszczak 	max_speed = usb_get_maximum_speed(cdns->dev);
18643d829045SPawel Laszczak 
18653d829045SPawel Laszczak 	switch (max_speed) {
18663d829045SPawel Laszczak 	case USB_SPEED_FULL:
18673d829045SPawel Laszczak 	case USB_SPEED_HIGH:
18683d829045SPawel Laszczak 	case USB_SPEED_SUPER:
18693d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
18703d829045SPawel Laszczak 		break;
18713d829045SPawel Laszczak 	default:
18723d829045SPawel Laszczak 		dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
18733d829045SPawel Laszczak 		fallthrough;
18743d829045SPawel Laszczak 	case USB_SPEED_UNKNOWN:
18753d829045SPawel Laszczak 		/* Default to SSP */
18763d829045SPawel Laszczak 		max_speed = USB_SPEED_SUPER_PLUS;
18773d829045SPawel Laszczak 		break;
18783d829045SPawel Laszczak 	}
18793d829045SPawel Laszczak 
18803d829045SPawel Laszczak 	pdev->gadget.ops = &cdnsp_gadget_ops;
18813d829045SPawel Laszczak 	pdev->gadget.name = "cdnsp-gadget";
18823d829045SPawel Laszczak 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
18833d829045SPawel Laszczak 	pdev->gadget.sg_supported = 1;
18843d829045SPawel Laszczak 	pdev->gadget.max_speed = USB_SPEED_SUPER_PLUS;
18853d829045SPawel Laszczak 	pdev->gadget.lpm_capable = 1;
18863d829045SPawel Laszczak 
18873d829045SPawel Laszczak 	pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
18883d829045SPawel Laszczak 	if (!pdev->setup_buf)
18893d829045SPawel Laszczak 		goto free_pdev;
18903d829045SPawel Laszczak 
18913d829045SPawel Laszczak 	/*
18923d829045SPawel Laszczak 	 * Controller supports not aligned buffer but it should improve
18933d829045SPawel Laszczak 	 * performance.
18943d829045SPawel Laszczak 	 */
18953d829045SPawel Laszczak 	pdev->gadget.quirk_ep_out_aligned_size = true;
18963d829045SPawel Laszczak 
18973d829045SPawel Laszczak 	ret = cdnsp_gen_setup(pdev);
18983d829045SPawel Laszczak 	if (ret) {
18993d829045SPawel Laszczak 		dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
19003d829045SPawel Laszczak 		goto free_setup;
19013d829045SPawel Laszczak 	}
19023d829045SPawel Laszczak 
19033d829045SPawel Laszczak 	ret = cdnsp_gadget_init_endpoints(pdev);
19043d829045SPawel Laszczak 	if (ret) {
19053d829045SPawel Laszczak 		dev_err(pdev->dev, "failed to initialize endpoints\n");
19063d829045SPawel Laszczak 		goto halt_pdev;
19073d829045SPawel Laszczak 	}
19083d829045SPawel Laszczak 
19093d829045SPawel Laszczak 	ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
19103d829045SPawel Laszczak 	if (ret) {
19113d829045SPawel Laszczak 		dev_err(pdev->dev, "failed to register udc\n");
19123d829045SPawel Laszczak 		goto free_endpoints;
19133d829045SPawel Laszczak 	}
19143d829045SPawel Laszczak 
19153d829045SPawel Laszczak 	ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
19163d829045SPawel Laszczak 					cdnsp_irq_handler,
19173d829045SPawel Laszczak 					cdnsp_thread_irq_handler, IRQF_SHARED,
19183d829045SPawel Laszczak 					dev_name(pdev->dev), pdev);
19193d829045SPawel Laszczak 	if (ret)
19203d829045SPawel Laszczak 		goto del_gadget;
19213d829045SPawel Laszczak 
19223d829045SPawel Laszczak 	return 0;
19233d829045SPawel Laszczak 
19243d829045SPawel Laszczak del_gadget:
19253d829045SPawel Laszczak 	usb_del_gadget_udc(&pdev->gadget);
19263d829045SPawel Laszczak free_endpoints:
19273d829045SPawel Laszczak 	cdnsp_gadget_free_endpoints(pdev);
19283d829045SPawel Laszczak halt_pdev:
19293d829045SPawel Laszczak 	cdnsp_halt(pdev);
19303d829045SPawel Laszczak 	cdnsp_reset(pdev);
19313d829045SPawel Laszczak 	cdnsp_mem_cleanup(pdev);
19323d829045SPawel Laszczak free_setup:
19333d829045SPawel Laszczak 	kfree(pdev->setup_buf);
19343d829045SPawel Laszczak free_pdev:
19353d829045SPawel Laszczak 	kfree(pdev);
19363d829045SPawel Laszczak 
19373d829045SPawel Laszczak 	return ret;
19383d829045SPawel Laszczak }
19393d829045SPawel Laszczak 
19403d829045SPawel Laszczak static void cdnsp_gadget_exit(struct cdns *cdns)
19413d829045SPawel Laszczak {
19423d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19433d829045SPawel Laszczak 
19443d829045SPawel Laszczak 	devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
19453d829045SPawel Laszczak 	pm_runtime_mark_last_busy(cdns->dev);
19463d829045SPawel Laszczak 	pm_runtime_put_autosuspend(cdns->dev);
19473d829045SPawel Laszczak 	usb_del_gadget_udc(&pdev->gadget);
19483d829045SPawel Laszczak 	cdnsp_gadget_free_endpoints(pdev);
19493d829045SPawel Laszczak 	cdnsp_mem_cleanup(pdev);
19503d829045SPawel Laszczak 	kfree(pdev);
19513d829045SPawel Laszczak 	cdns->gadget_dev = NULL;
19523d829045SPawel Laszczak 	cdns_drd_gadget_off(cdns);
19533d829045SPawel Laszczak }
19543d829045SPawel Laszczak 
19553d829045SPawel Laszczak static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
19563d829045SPawel Laszczak {
19573d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19583d829045SPawel Laszczak 	unsigned long flags;
19593d829045SPawel Laszczak 
19603d829045SPawel Laszczak 	if (pdev->link_state == XDEV_U3)
19613d829045SPawel Laszczak 		return 0;
19623d829045SPawel Laszczak 
19633d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
19643d829045SPawel Laszczak 	cdnsp_disconnect_gadget(pdev);
19653d829045SPawel Laszczak 	cdnsp_stop(pdev);
19663d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
19673d829045SPawel Laszczak 
19683d829045SPawel Laszczak 	return 0;
19693d829045SPawel Laszczak }
19703d829045SPawel Laszczak 
19713d829045SPawel Laszczak static int cdnsp_gadget_resume(struct cdns *cdns, bool hibernated)
19723d829045SPawel Laszczak {
19733d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19743d829045SPawel Laszczak 	enum usb_device_speed max_speed;
19753d829045SPawel Laszczak 	unsigned long flags;
19763d829045SPawel Laszczak 	int ret;
19773d829045SPawel Laszczak 
19783d829045SPawel Laszczak 	if (!pdev->gadget_driver)
19793d829045SPawel Laszczak 		return 0;
19803d829045SPawel Laszczak 
19813d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
19823d829045SPawel Laszczak 	max_speed = pdev->gadget_driver->max_speed;
19833d829045SPawel Laszczak 
19843d829045SPawel Laszczak 	/* Limit speed if necessary. */
19853d829045SPawel Laszczak 	max_speed = min(max_speed, pdev->gadget.max_speed);
19863d829045SPawel Laszczak 
19873d829045SPawel Laszczak 	ret = cdnsp_run(pdev, max_speed);
19883d829045SPawel Laszczak 
19893d829045SPawel Laszczak 	if (pdev->link_state == XDEV_U3)
19903d829045SPawel Laszczak 		__cdnsp_gadget_wakeup(pdev);
19913d829045SPawel Laszczak 
19923d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
19933d829045SPawel Laszczak 
19943d829045SPawel Laszczak 	return ret;
19953d829045SPawel Laszczak }
19963d829045SPawel Laszczak 
19973d829045SPawel Laszczak /**
19983d829045SPawel Laszczak  * cdnsp_gadget_init - initialize device structure
19993d829045SPawel Laszczak  * @cdns: cdnsp instance
20003d829045SPawel Laszczak  *
20013d829045SPawel Laszczak  * This function initializes the gadget.
20023d829045SPawel Laszczak  */
20033d829045SPawel Laszczak int cdnsp_gadget_init(struct cdns *cdns)
20043d829045SPawel Laszczak {
20053d829045SPawel Laszczak 	struct cdns_role_driver *rdrv;
20063d829045SPawel Laszczak 
20073d829045SPawel Laszczak 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
20083d829045SPawel Laszczak 	if (!rdrv)
20093d829045SPawel Laszczak 		return -ENOMEM;
20103d829045SPawel Laszczak 
20113d829045SPawel Laszczak 	rdrv->start	= __cdnsp_gadget_init;
20123d829045SPawel Laszczak 	rdrv->stop	= cdnsp_gadget_exit;
20133d829045SPawel Laszczak 	rdrv->suspend	= cdnsp_gadget_suspend;
20143d829045SPawel Laszczak 	rdrv->resume	= cdnsp_gadget_resume;
20153d829045SPawel Laszczak 	rdrv->state	= CDNS_ROLE_STATE_INACTIVE;
20163d829045SPawel Laszczak 	rdrv->name	= "gadget";
20173d829045SPawel Laszczak 	cdns->roles[USB_ROLE_DEVICE] = rdrv;
20183d829045SPawel Laszczak 
20193d829045SPawel Laszczak 	return 0;
20203d829045SPawel Laszczak }
2021