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