xref: /openbmc/linux/drivers/usb/cdns3/cdnsp-gadget.c (revision aa82f94e869edd72f4fadb08c6ffca8927e4934e)
13d829045SPawel Laszczak // SPDX-License-Identifier: GPL-2.0
23d829045SPawel Laszczak /*
33d829045SPawel Laszczak  * Cadence CDNSP DRD Driver.
43d829045SPawel Laszczak  *
53d829045SPawel Laszczak  * Copyright (C) 2020 Cadence.
63d829045SPawel Laszczak  *
73d829045SPawel Laszczak  * Author: Pawel Laszczak <pawell@cadence.com>
83d829045SPawel Laszczak  *
93d829045SPawel Laszczak  */
103d829045SPawel Laszczak 
113d829045SPawel Laszczak #include <linux/moduleparam.h>
123d829045SPawel Laszczak #include <linux/dma-mapping.h>
133d829045SPawel Laszczak #include <linux/module.h>
143d829045SPawel Laszczak #include <linux/iopoll.h>
153d829045SPawel Laszczak #include <linux/delay.h>
163d829045SPawel Laszczak #include <linux/log2.h>
173d829045SPawel Laszczak #include <linux/slab.h>
183d829045SPawel Laszczak #include <linux/pci.h>
193d829045SPawel Laszczak #include <linux/irq.h>
203d829045SPawel Laszczak #include <linux/dmi.h>
213d829045SPawel Laszczak 
223d829045SPawel Laszczak #include "core.h"
233d829045SPawel Laszczak #include "gadget-export.h"
243d829045SPawel Laszczak #include "drd.h"
253d829045SPawel Laszczak #include "cdnsp-gadget.h"
26118b2a32SPawel Laszczak #include "cdnsp-trace.h"
273d829045SPawel Laszczak 
283d829045SPawel Laszczak unsigned int cdnsp_port_speed(unsigned int port_status)
293d829045SPawel Laszczak {
303d829045SPawel Laszczak 	/*Detect gadget speed based on PORTSC register*/
313d829045SPawel Laszczak 	if (DEV_SUPERSPEEDPLUS(port_status))
323d829045SPawel Laszczak 		return USB_SPEED_SUPER_PLUS;
333d829045SPawel Laszczak 	else if (DEV_SUPERSPEED(port_status))
343d829045SPawel Laszczak 		return USB_SPEED_SUPER;
353d829045SPawel Laszczak 	else if (DEV_HIGHSPEED(port_status))
363d829045SPawel Laszczak 		return USB_SPEED_HIGH;
373d829045SPawel Laszczak 	else if (DEV_FULLSPEED(port_status))
383d829045SPawel Laszczak 		return USB_SPEED_FULL;
393d829045SPawel Laszczak 
403d829045SPawel Laszczak 	/* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
413d829045SPawel Laszczak 	return USB_SPEED_UNKNOWN;
423d829045SPawel Laszczak }
433d829045SPawel Laszczak 
443d829045SPawel Laszczak /*
453d829045SPawel Laszczak  * Given a port state, this function returns a value that would result in the
463d829045SPawel Laszczak  * port being in the same state, if the value was written to the port status
473d829045SPawel Laszczak  * control register.
483d829045SPawel Laszczak  * Save Read Only (RO) bits and save read/write bits where
493d829045SPawel Laszczak  * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
503d829045SPawel Laszczak  * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
513d829045SPawel Laszczak  */
523d829045SPawel Laszczak u32 cdnsp_port_state_to_neutral(u32 state)
533d829045SPawel Laszczak {
543d829045SPawel Laszczak 	/* Save read-only status and port state. */
553d829045SPawel Laszczak 	return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
563d829045SPawel Laszczak }
573d829045SPawel Laszczak 
583d829045SPawel Laszczak /**
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  */
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;
843d829045SPawel Laszczak 	};
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 
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 
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 
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 
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 
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  */
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  */
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  */
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  */
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  */
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
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
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 
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:
3813d829045SPawel Laszczak 		ret = cdnsp_queue_isoc_tx_prepare(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  */
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 
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. */
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 
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 
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 
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 	if (value) {
6043d829045SPawel Laszczak 		ret = cdnsp_cmd_stop_ep(pdev, pep);
6053d829045SPawel Laszczak 		if (ret)
6063d829045SPawel Laszczak 			return ret;
6073d829045SPawel Laszczak 
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 		/*
6173d829045SPawel Laszczak 		 * In device mode driver can call reset endpoint command
6183d829045SPawel Laszczak 		 * from any endpoint state.
6193d829045SPawel Laszczak 		 */
6203d829045SPawel Laszczak 		cdnsp_queue_reset_ep(pdev, pep->idx);
6213d829045SPawel Laszczak 		cdnsp_ring_cmd_db(pdev);
6223d829045SPawel Laszczak 		ret = cdnsp_wait_for_cmd_compl(pdev);
623118b2a32SPawel Laszczak 		trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
624118b2a32SPawel Laszczak 
6253d829045SPawel Laszczak 		if (ret)
6263d829045SPawel Laszczak 			return ret;
6273d829045SPawel Laszczak 
6283d829045SPawel Laszczak 		pep->ep_state &= ~EP_HALTED;
6293d829045SPawel Laszczak 
6303d829045SPawel Laszczak 		if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
6313d829045SPawel Laszczak 			cdnsp_ring_doorbell_for_active_rings(pdev, pep);
6323d829045SPawel Laszczak 
6333d829045SPawel Laszczak 		pep->ep_state &= ~EP_WEDGE;
6343d829045SPawel Laszczak 	}
6353d829045SPawel Laszczak 
6363d829045SPawel Laszczak 	return 0;
6373d829045SPawel Laszczak }
6383d829045SPawel Laszczak 
6393d829045SPawel Laszczak static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
6403d829045SPawel Laszczak 					  struct cdnsp_ep *pep)
6413d829045SPawel Laszczak {
6423d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
6433d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
6443d829045SPawel Laszczak 	int ret = 0;
6453d829045SPawel Laszczak 	u32 ep_sts;
6463d829045SPawel Laszczak 	int i;
6473d829045SPawel Laszczak 
6483d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
6493d829045SPawel Laszczak 
6503d829045SPawel Laszczak 	/* Don't issue the command if there's no endpoints to update. */
6513d829045SPawel Laszczak 	if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
6523d829045SPawel Laszczak 		return 0;
6533d829045SPawel Laszczak 
6543d829045SPawel Laszczak 	ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
6553d829045SPawel Laszczak 	ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
6563d829045SPawel Laszczak 	ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
6573d829045SPawel Laszczak 
6583d829045SPawel Laszczak 	/* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
6593d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
6603d829045SPawel Laszczak 	for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
6613d829045SPawel Laszczak 		__le32 le32 = cpu_to_le32(BIT(i));
6623d829045SPawel Laszczak 
6633d829045SPawel Laszczak 		if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
6643d829045SPawel Laszczak 		    (ctrl_ctx->add_flags & le32) || i == 1) {
6653d829045SPawel Laszczak 			slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
6663d829045SPawel Laszczak 			slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
6673d829045SPawel Laszczak 			break;
6683d829045SPawel Laszczak 		}
6693d829045SPawel Laszczak 	}
6703d829045SPawel Laszczak 
6713d829045SPawel Laszczak 	ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
6723d829045SPawel Laszczak 
6733d829045SPawel Laszczak 	if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
6743d829045SPawel Laszczak 	     ep_sts == EP_STATE_DISABLED) ||
6753d829045SPawel Laszczak 	    (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
6763d829045SPawel Laszczak 		ret = cdnsp_configure_endpoint(pdev);
6773d829045SPawel Laszczak 
678118b2a32SPawel Laszczak 	trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
679118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
680118b2a32SPawel Laszczak 
6813d829045SPawel Laszczak 	cdnsp_zero_in_ctx(pdev);
6823d829045SPawel Laszczak 
6833d829045SPawel Laszczak 	return ret;
6843d829045SPawel Laszczak }
6853d829045SPawel Laszczak 
6863d829045SPawel Laszczak /*
6873d829045SPawel Laszczak  * This submits a Reset Device Command, which will set the device state to 0,
6883d829045SPawel Laszczak  * set the device address to 0, and disable all the endpoints except the default
6893d829045SPawel Laszczak  * control endpoint. The USB core should come back and call
6903d829045SPawel Laszczak  * cdnsp_setup_device(), and then re-set up the configuration.
6913d829045SPawel Laszczak  */
6923d829045SPawel Laszczak int cdnsp_reset_device(struct cdnsp_device *pdev)
6933d829045SPawel Laszczak {
6943d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
6953d829045SPawel Laszczak 	int slot_state;
6963d829045SPawel Laszczak 	int ret, i;
6973d829045SPawel Laszczak 
6983d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
6993d829045SPawel Laszczak 	slot_ctx->dev_info = 0;
7003d829045SPawel Laszczak 	pdev->device_address = 0;
7013d829045SPawel Laszczak 
7023d829045SPawel Laszczak 	/* If device is not setup, there is no point in resetting it. */
7033d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
7043d829045SPawel Laszczak 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
705118b2a32SPawel Laszczak 	trace_cdnsp_reset_device(slot_ctx);
7063d829045SPawel Laszczak 
7073d829045SPawel Laszczak 	if (slot_state <= SLOT_STATE_DEFAULT &&
7083d829045SPawel Laszczak 	    pdev->eps[0].ep_state & EP_HALTED) {
7093d829045SPawel Laszczak 		cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
7103d829045SPawel Laszczak 	}
7113d829045SPawel Laszczak 
7123d829045SPawel Laszczak 	/*
7133d829045SPawel Laszczak 	 * During Reset Device command controller shall transition the
7143d829045SPawel Laszczak 	 * endpoint ep0 to the Running State.
7153d829045SPawel Laszczak 	 */
7163d829045SPawel Laszczak 	pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
7173d829045SPawel Laszczak 	pdev->eps[0].ep_state |= EP_ENABLED;
7183d829045SPawel Laszczak 
7193d829045SPawel Laszczak 	if (slot_state <= SLOT_STATE_DEFAULT)
7203d829045SPawel Laszczak 		return 0;
7213d829045SPawel Laszczak 
7223d829045SPawel Laszczak 	cdnsp_queue_reset_device(pdev);
7233d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
7243d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
7253d829045SPawel Laszczak 
7263d829045SPawel Laszczak 	/*
7273d829045SPawel Laszczak 	 * After Reset Device command all not default endpoints
7283d829045SPawel Laszczak 	 * are in Disabled state.
7293d829045SPawel Laszczak 	 */
7303d829045SPawel Laszczak 	for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
73110076de3SPawel Laszczak 		pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
7323d829045SPawel Laszczak 
733118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
734118b2a32SPawel Laszczak 
7353d829045SPawel Laszczak 	if (ret)
7363d829045SPawel Laszczak 		dev_err(pdev->dev, "Reset device failed with error code %d",
7373d829045SPawel Laszczak 			ret);
7383d829045SPawel Laszczak 
7393d829045SPawel Laszczak 	return ret;
7403d829045SPawel Laszczak }
7413d829045SPawel Laszczak 
7423d829045SPawel Laszczak /*
7433d829045SPawel Laszczak  * Sets the MaxPStreams field and the Linear Stream Array field.
7443d829045SPawel Laszczak  * Sets the dequeue pointer to the stream context array.
7453d829045SPawel Laszczak  */
7463d829045SPawel Laszczak static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
7473d829045SPawel Laszczak 					     struct cdnsp_ep_ctx *ep_ctx,
7483d829045SPawel Laszczak 					     struct cdnsp_stream_info *stream_info)
7493d829045SPawel Laszczak {
7503d829045SPawel Laszczak 	u32 max_primary_streams;
7513d829045SPawel Laszczak 
7523d829045SPawel Laszczak 	/* MaxPStreams is the number of stream context array entries, not the
7533d829045SPawel Laszczak 	 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
7543d829045SPawel Laszczak 	 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
7553d829045SPawel Laszczak 	 */
7563d829045SPawel Laszczak 	max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
7573d829045SPawel Laszczak 	ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
7583d829045SPawel Laszczak 	ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
7593d829045SPawel Laszczak 				       | EP_HAS_LSA);
7603d829045SPawel Laszczak 	ep_ctx->deq  = cpu_to_le64(stream_info->ctx_array_dma);
7613d829045SPawel Laszczak }
7623d829045SPawel Laszczak 
7633d829045SPawel Laszczak /*
7643d829045SPawel Laszczak  * The drivers use this function to prepare a bulk endpoints to use streams.
7653d829045SPawel Laszczak  *
7663d829045SPawel Laszczak  * Don't allow the call to succeed if endpoint only supports one stream
7673d829045SPawel Laszczak  * (which means it doesn't support streams at all).
7683d829045SPawel Laszczak  */
7693d829045SPawel Laszczak int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
7703d829045SPawel Laszczak {
7713d829045SPawel Laszczak 	unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
7723d829045SPawel Laszczak 	unsigned int num_stream_ctxs;
7733d829045SPawel Laszczak 	int ret;
7743d829045SPawel Laszczak 
7753d829045SPawel Laszczak 	if (num_streams ==  0)
7763d829045SPawel Laszczak 		return 0;
7773d829045SPawel Laszczak 
7783d829045SPawel Laszczak 	if (num_streams > STREAM_NUM_STREAMS)
7793d829045SPawel Laszczak 		return -EINVAL;
7803d829045SPawel Laszczak 
7813d829045SPawel Laszczak 	/*
7823d829045SPawel Laszczak 	 * Add two to the number of streams requested to account for
7833d829045SPawel Laszczak 	 * stream 0 that is reserved for controller usage and one additional
7843d829045SPawel Laszczak 	 * for TASK SET FULL response.
7853d829045SPawel Laszczak 	 */
7863d829045SPawel Laszczak 	num_streams += 2;
7873d829045SPawel Laszczak 
7883d829045SPawel Laszczak 	/* The stream context array size must be a power of two */
7893d829045SPawel Laszczak 	num_stream_ctxs = roundup_pow_of_two(num_streams);
7903d829045SPawel Laszczak 
791118b2a32SPawel Laszczak 	trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
792118b2a32SPawel Laszczak 
7933d829045SPawel Laszczak 	ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
7943d829045SPawel Laszczak 	if (ret)
7953d829045SPawel Laszczak 		return ret;
7963d829045SPawel Laszczak 
7973d829045SPawel Laszczak 	cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
7983d829045SPawel Laszczak 
7993d829045SPawel Laszczak 	pep->ep_state |= EP_HAS_STREAMS;
8003d829045SPawel Laszczak 	pep->stream_info.td_count = 0;
8013d829045SPawel Laszczak 	pep->stream_info.first_prime_det = 0;
8023d829045SPawel Laszczak 
8033d829045SPawel Laszczak 	/* Subtract 1 for stream 0, which drivers can't use. */
8043d829045SPawel Laszczak 	return num_streams - 1;
8053d829045SPawel Laszczak }
8063d829045SPawel Laszczak 
8073d829045SPawel Laszczak int cdnsp_disable_slot(struct cdnsp_device *pdev)
8083d829045SPawel Laszczak {
8093d829045SPawel Laszczak 	int ret;
8103d829045SPawel Laszczak 
8113d829045SPawel Laszczak 	cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
8123d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8133d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8143d829045SPawel Laszczak 
8153d829045SPawel Laszczak 	pdev->slot_id = 0;
8163d829045SPawel Laszczak 	pdev->active_port = NULL;
8173d829045SPawel Laszczak 
818118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
819118b2a32SPawel Laszczak 
8203d829045SPawel Laszczak 	memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
8213d829045SPawel Laszczak 	memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
8223d829045SPawel Laszczak 
8233d829045SPawel Laszczak 	return ret;
8243d829045SPawel Laszczak }
8253d829045SPawel Laszczak 
8263d829045SPawel Laszczak int cdnsp_enable_slot(struct cdnsp_device *pdev)
8273d829045SPawel Laszczak {
8283d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
8293d829045SPawel Laszczak 	int slot_state;
8303d829045SPawel Laszczak 	int ret;
8313d829045SPawel Laszczak 
8323d829045SPawel Laszczak 	/* If device is not setup, there is no point in resetting it */
8333d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
8343d829045SPawel Laszczak 	slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
8353d829045SPawel Laszczak 
8363d829045SPawel Laszczak 	if (slot_state != SLOT_STATE_DISABLED)
8373d829045SPawel Laszczak 		return 0;
8383d829045SPawel Laszczak 
8393d829045SPawel Laszczak 	cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
8403d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8413d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8423d829045SPawel Laszczak 	if (ret)
843118b2a32SPawel Laszczak 		goto show_trace;
8443d829045SPawel Laszczak 
8453d829045SPawel Laszczak 	pdev->slot_id = 1;
8463d829045SPawel Laszczak 
847118b2a32SPawel Laszczak show_trace:
848118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
849118b2a32SPawel Laszczak 
850118b2a32SPawel Laszczak 	return ret;
8513d829045SPawel Laszczak }
8523d829045SPawel Laszczak 
8533d829045SPawel Laszczak /*
8543d829045SPawel Laszczak  * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
8553d829045SPawel Laszczak  * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
8563d829045SPawel Laszczak  */
8573d829045SPawel Laszczak int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
8583d829045SPawel Laszczak {
8593d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
8603d829045SPawel Laszczak 	struct cdnsp_slot_ctx *slot_ctx;
8613d829045SPawel Laszczak 	int dev_state = 0;
8623d829045SPawel Laszczak 	int ret;
8633d829045SPawel Laszczak 
864118b2a32SPawel Laszczak 	if (!pdev->slot_id) {
865118b2a32SPawel Laszczak 		trace_cdnsp_slot_id("incorrect");
8663d829045SPawel Laszczak 		return -EINVAL;
867118b2a32SPawel Laszczak 	}
8683d829045SPawel Laszczak 
8693d829045SPawel Laszczak 	if (!pdev->active_port->port_num)
8703d829045SPawel Laszczak 		return -EINVAL;
8713d829045SPawel Laszczak 
8723d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
8733d829045SPawel Laszczak 	dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
8743d829045SPawel Laszczak 
875118b2a32SPawel Laszczak 	if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
876118b2a32SPawel Laszczak 		trace_cdnsp_slot_already_in_default(slot_ctx);
8773d829045SPawel Laszczak 		return 0;
878118b2a32SPawel Laszczak 	}
8793d829045SPawel Laszczak 
8803d829045SPawel Laszczak 	slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
8813d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
8823d829045SPawel Laszczak 
8833d829045SPawel Laszczak 	if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
8843d829045SPawel Laszczak 		ret = cdnsp_setup_addressable_priv_dev(pdev);
8853d829045SPawel Laszczak 		if (ret)
8863d829045SPawel Laszczak 			return ret;
8873d829045SPawel Laszczak 	}
8883d829045SPawel Laszczak 
8893d829045SPawel Laszczak 	cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
8903d829045SPawel Laszczak 
8913d829045SPawel Laszczak 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
8923d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
8933d829045SPawel Laszczak 
894118b2a32SPawel Laszczak 	trace_cdnsp_setup_device_slot(slot_ctx);
895118b2a32SPawel Laszczak 
8963d829045SPawel Laszczak 	cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
8973d829045SPawel Laszczak 	cdnsp_ring_cmd_db(pdev);
8983d829045SPawel Laszczak 	ret = cdnsp_wait_for_cmd_compl(pdev);
8993d829045SPawel Laszczak 
900118b2a32SPawel Laszczak 	trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
901118b2a32SPawel Laszczak 
9023d829045SPawel Laszczak 	/* Zero the input context control for later use. */
9033d829045SPawel Laszczak 	ctrl_ctx->add_flags = 0;
9043d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
9053d829045SPawel Laszczak 
9063d829045SPawel Laszczak 	return ret;
9073d829045SPawel Laszczak }
9083d829045SPawel Laszczak 
9093d829045SPawel Laszczak void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
9103d829045SPawel Laszczak 				 struct usb_request *req,
9113d829045SPawel Laszczak 				 int enable)
9123d829045SPawel Laszczak {
9133d829045SPawel Laszczak 	if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
9143d829045SPawel Laszczak 		return;
9153d829045SPawel Laszczak 
916118b2a32SPawel Laszczak 	trace_cdnsp_lpm(enable);
917118b2a32SPawel Laszczak 
9183d829045SPawel Laszczak 	if (enable)
9193d829045SPawel Laszczak 		writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
9203d829045SPawel Laszczak 		       &pdev->active_port->regs->portpmsc);
9213d829045SPawel Laszczak 	else
9223d829045SPawel Laszczak 		writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
9233d829045SPawel Laszczak }
9243d829045SPawel Laszczak 
9253d829045SPawel Laszczak static int cdnsp_get_frame(struct cdnsp_device *pdev)
9263d829045SPawel Laszczak {
9273d829045SPawel Laszczak 	return readl(&pdev->run_regs->microframe_index) >> 3;
9283d829045SPawel Laszczak }
9293d829045SPawel Laszczak 
9303d829045SPawel Laszczak static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
9313d829045SPawel Laszczak 				  const struct usb_endpoint_descriptor *desc)
9323d829045SPawel Laszczak {
9333d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
9343d829045SPawel Laszczak 	struct cdnsp_device *pdev;
9353d829045SPawel Laszczak 	struct cdnsp_ep *pep;
9363d829045SPawel Laszczak 	unsigned long flags;
9373d829045SPawel Laszczak 	u32 added_ctxs;
9383d829045SPawel Laszczak 	int ret;
9393d829045SPawel Laszczak 
9403d829045SPawel Laszczak 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
9413d829045SPawel Laszczak 	    !desc->wMaxPacketSize)
9423d829045SPawel Laszczak 		return -EINVAL;
9433d829045SPawel Laszczak 
9443d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
9453d829045SPawel Laszczak 	pdev = pep->pdev;
94610076de3SPawel Laszczak 	pep->ep_state &= ~EP_UNCONFIGURED;
9473d829045SPawel Laszczak 
9483d829045SPawel Laszczak 	if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
9493d829045SPawel Laszczak 			  "%s is already enabled\n", pep->name))
9503d829045SPawel Laszczak 		return 0;
9513d829045SPawel Laszczak 
9523d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
9533d829045SPawel Laszczak 
9543d829045SPawel Laszczak 	added_ctxs = cdnsp_get_endpoint_flag(desc);
9553d829045SPawel Laszczak 	if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
9563d829045SPawel Laszczak 		dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
9573d829045SPawel Laszczak 		ret = -EINVAL;
9583d829045SPawel Laszczak 		goto unlock;
9593d829045SPawel Laszczak 	}
9603d829045SPawel Laszczak 
9613d829045SPawel Laszczak 	pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
9623d829045SPawel Laszczak 
9633d829045SPawel Laszczak 	if (pdev->gadget.speed == USB_SPEED_FULL) {
9643d829045SPawel Laszczak 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
9653d829045SPawel Laszczak 			pep->interval = desc->bInterval << 3;
9663d829045SPawel Laszczak 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
9673d829045SPawel Laszczak 			pep->interval = BIT(desc->bInterval - 1) << 3;
9683d829045SPawel Laszczak 	}
9693d829045SPawel Laszczak 
9703d829045SPawel Laszczak 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
9713d829045SPawel Laszczak 		if (pep->interval > BIT(12)) {
9723d829045SPawel Laszczak 			dev_err(pdev->dev, "bInterval %d not supported\n",
9733d829045SPawel Laszczak 				desc->bInterval);
9743d829045SPawel Laszczak 			ret = -EINVAL;
9753d829045SPawel Laszczak 			goto unlock;
9763d829045SPawel Laszczak 		}
9773d829045SPawel Laszczak 		cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
9783d829045SPawel Laszczak 	}
9793d829045SPawel Laszczak 
9803d829045SPawel Laszczak 	ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
9813d829045SPawel Laszczak 	if (ret)
9823d829045SPawel Laszczak 		goto unlock;
9833d829045SPawel Laszczak 
9843d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
9853d829045SPawel Laszczak 	ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
9863d829045SPawel Laszczak 	ctrl_ctx->drop_flags = 0;
9873d829045SPawel Laszczak 
9883d829045SPawel Laszczak 	ret = cdnsp_update_eps_configuration(pdev, pep);
9893d829045SPawel Laszczak 	if (ret) {
9903d829045SPawel Laszczak 		cdnsp_free_endpoint_rings(pdev, pep);
9913d829045SPawel Laszczak 		goto unlock;
9923d829045SPawel Laszczak 	}
9933d829045SPawel Laszczak 
9943d829045SPawel Laszczak 	pep->ep_state |= EP_ENABLED;
9953d829045SPawel Laszczak 	pep->ep_state &= ~EP_STOPPED;
9963d829045SPawel Laszczak 
9973d829045SPawel Laszczak unlock:
998118b2a32SPawel Laszczak 	trace_cdnsp_ep_enable_end(pep, 0);
9993d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
10003d829045SPawel Laszczak 
10013d829045SPawel Laszczak 	return ret;
10023d829045SPawel Laszczak }
10033d829045SPawel Laszczak 
10043d829045SPawel Laszczak static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
10053d829045SPawel Laszczak {
10063d829045SPawel Laszczak 	struct cdnsp_input_control_ctx *ctrl_ctx;
10073d829045SPawel Laszczak 	struct cdnsp_request *preq;
10083d829045SPawel Laszczak 	struct cdnsp_device *pdev;
10093d829045SPawel Laszczak 	struct cdnsp_ep *pep;
10103d829045SPawel Laszczak 	unsigned long flags;
10113d829045SPawel Laszczak 	u32 drop_flag;
10123d829045SPawel Laszczak 	int ret = 0;
10133d829045SPawel Laszczak 
10143d829045SPawel Laszczak 	if (!ep)
10153d829045SPawel Laszczak 		return -EINVAL;
10163d829045SPawel Laszczak 
10173d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
10183d829045SPawel Laszczak 	pdev = pep->pdev;
10193d829045SPawel Laszczak 
10203d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
10213d829045SPawel Laszczak 
10223d829045SPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED)) {
10233d829045SPawel Laszczak 		dev_err(pdev->dev, "%s is already disabled\n", pep->name);
10243d829045SPawel Laszczak 		ret = -EINVAL;
10253d829045SPawel Laszczak 		goto finish;
10263d829045SPawel Laszczak 	}
10273d829045SPawel Laszczak 
10283d829045SPawel Laszczak 	pep->ep_state |= EP_DIS_IN_RROGRESS;
102910076de3SPawel Laszczak 
103010076de3SPawel Laszczak 	/* Endpoint was unconfigured by Reset Device command. */
103110076de3SPawel Laszczak 	if (!(pep->ep_state & EP_UNCONFIGURED)) {
103210076de3SPawel Laszczak 		cdnsp_cmd_stop_ep(pdev, pep);
10333d829045SPawel Laszczak 		cdnsp_cmd_flush_ep(pdev, pep);
103410076de3SPawel Laszczak 	}
10353d829045SPawel Laszczak 
10363d829045SPawel Laszczak 	/* Remove all queued USB requests. */
10373d829045SPawel Laszczak 	while (!list_empty(&pep->pending_list)) {
10383d829045SPawel Laszczak 		preq = next_request(&pep->pending_list);
10393d829045SPawel Laszczak 		cdnsp_ep_dequeue(pep, preq);
10403d829045SPawel Laszczak 	}
10413d829045SPawel Laszczak 
10423d829045SPawel Laszczak 	cdnsp_invalidate_ep_events(pdev, pep);
10433d829045SPawel Laszczak 
10443d829045SPawel Laszczak 	pep->ep_state &= ~EP_DIS_IN_RROGRESS;
10453d829045SPawel Laszczak 	drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
10463d829045SPawel Laszczak 	ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
10473d829045SPawel Laszczak 	ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
10483d829045SPawel Laszczak 	ctrl_ctx->add_flags = 0;
10493d829045SPawel Laszczak 
10503d829045SPawel Laszczak 	cdnsp_endpoint_zero(pdev, pep);
10513d829045SPawel Laszczak 
105210076de3SPawel Laszczak 	if (!(pep->ep_state & EP_UNCONFIGURED))
10533d829045SPawel Laszczak 		ret = cdnsp_update_eps_configuration(pdev, pep);
105410076de3SPawel Laszczak 
10553d829045SPawel Laszczak 	cdnsp_free_endpoint_rings(pdev, pep);
10563d829045SPawel Laszczak 
105710076de3SPawel Laszczak 	pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
10583d829045SPawel Laszczak 	pep->ep_state |= EP_STOPPED;
10593d829045SPawel Laszczak 
10603d829045SPawel Laszczak finish:
1061118b2a32SPawel Laszczak 	trace_cdnsp_ep_disable_end(pep, 0);
10623d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
10633d829045SPawel Laszczak 
10643d829045SPawel Laszczak 	return ret;
10653d829045SPawel Laszczak }
10663d829045SPawel Laszczak 
10673d829045SPawel Laszczak static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
10683d829045SPawel Laszczak 							 gfp_t gfp_flags)
10693d829045SPawel Laszczak {
10703d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
10713d829045SPawel Laszczak 	struct cdnsp_request *preq;
10723d829045SPawel Laszczak 
10733d829045SPawel Laszczak 	preq = kzalloc(sizeof(*preq), gfp_flags);
10743d829045SPawel Laszczak 	if (!preq)
10753d829045SPawel Laszczak 		return NULL;
10763d829045SPawel Laszczak 
10773d829045SPawel Laszczak 	preq->epnum = pep->number;
10783d829045SPawel Laszczak 	preq->pep = pep;
10793d829045SPawel Laszczak 
1080118b2a32SPawel Laszczak 	trace_cdnsp_alloc_request(preq);
1081118b2a32SPawel Laszczak 
10823d829045SPawel Laszczak 	return &preq->request;
10833d829045SPawel Laszczak }
10843d829045SPawel Laszczak 
10853d829045SPawel Laszczak static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
10863d829045SPawel Laszczak 					 struct usb_request *request)
10873d829045SPawel Laszczak {
10883d829045SPawel Laszczak 	struct cdnsp_request *preq = to_cdnsp_request(request);
10893d829045SPawel Laszczak 
1090118b2a32SPawel Laszczak 	trace_cdnsp_free_request(preq);
10913d829045SPawel Laszczak 	kfree(preq);
10923d829045SPawel Laszczak }
10933d829045SPawel Laszczak 
10943d829045SPawel Laszczak static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
10953d829045SPawel Laszczak 				 struct usb_request *request,
10963d829045SPawel Laszczak 				 gfp_t gfp_flags)
10973d829045SPawel Laszczak {
10983d829045SPawel Laszczak 	struct cdnsp_request *preq;
10993d829045SPawel Laszczak 	struct cdnsp_device *pdev;
11003d829045SPawel Laszczak 	struct cdnsp_ep *pep;
11013d829045SPawel Laszczak 	unsigned long flags;
11023d829045SPawel Laszczak 	int ret;
11033d829045SPawel Laszczak 
11043d829045SPawel Laszczak 	if (!request || !ep)
11053d829045SPawel Laszczak 		return -EINVAL;
11063d829045SPawel Laszczak 
11073d829045SPawel Laszczak 	pep = to_cdnsp_ep(ep);
11083d829045SPawel Laszczak 	pdev = pep->pdev;
11093d829045SPawel Laszczak 
11103d829045SPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED)) {
11113d829045SPawel Laszczak 		dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
11123d829045SPawel Laszczak 			pep->name);
11133d829045SPawel Laszczak 		return -EINVAL;
11143d829045SPawel Laszczak 	}
11153d829045SPawel Laszczak 
11163d829045SPawel Laszczak 	preq = to_cdnsp_request(request);
11173d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11183d829045SPawel Laszczak 	ret = cdnsp_ep_enqueue(pep, preq);
11193d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11203d829045SPawel Laszczak 
11213d829045SPawel Laszczak 	return ret;
11223d829045SPawel Laszczak }
11233d829045SPawel Laszczak 
11243d829045SPawel Laszczak static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
11253d829045SPawel Laszczak 				   struct usb_request *request)
11263d829045SPawel Laszczak {
11273d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11283d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
11293d829045SPawel Laszczak 	unsigned long flags;
11303d829045SPawel Laszczak 	int ret;
11313d829045SPawel Laszczak 
11323d829045SPawel Laszczak 	if (!pep->endpoint.desc) {
11333d829045SPawel Laszczak 		dev_err(pdev->dev,
11343d829045SPawel Laszczak 			"%s: can't dequeue to disabled endpoint\n",
11353d829045SPawel Laszczak 			pep->name);
11363d829045SPawel Laszczak 		return -ESHUTDOWN;
11373d829045SPawel Laszczak 	}
11383d829045SPawel Laszczak 
1139cf97d7afSPawel Laszczak 	/* Requests has been dequeued during disabling endpoint. */
1140cf97d7afSPawel Laszczak 	if (!(pep->ep_state & EP_ENABLED))
1141cf97d7afSPawel Laszczak 		return 0;
1142cf97d7afSPawel Laszczak 
11433d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11443d829045SPawel Laszczak 	ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
11453d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11463d829045SPawel Laszczak 
11473d829045SPawel Laszczak 	return ret;
11483d829045SPawel Laszczak }
11493d829045SPawel Laszczak 
11503d829045SPawel Laszczak static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
11513d829045SPawel Laszczak {
11523d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11533d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
11543d829045SPawel Laszczak 	struct cdnsp_request *preq;
115518538a50SJohan Hovold 	unsigned long flags;
11563d829045SPawel Laszczak 	int ret;
11573d829045SPawel Laszczak 
11583d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11593d829045SPawel Laszczak 
11603d829045SPawel Laszczak 	preq = next_request(&pep->pending_list);
11613d829045SPawel Laszczak 	if (value) {
11623d829045SPawel Laszczak 		if (preq) {
1163118b2a32SPawel Laszczak 			trace_cdnsp_ep_busy_try_halt_again(pep, 0);
11643d829045SPawel Laszczak 			ret = -EAGAIN;
11653d829045SPawel Laszczak 			goto done;
11663d829045SPawel Laszczak 		}
11673d829045SPawel Laszczak 	}
11683d829045SPawel Laszczak 
11693d829045SPawel Laszczak 	ret = cdnsp_halt_endpoint(pdev, pep, value);
11703d829045SPawel Laszczak 
11713d829045SPawel Laszczak done:
11723d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11733d829045SPawel Laszczak 	return ret;
11743d829045SPawel Laszczak }
11753d829045SPawel Laszczak 
11763d829045SPawel Laszczak static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
11773d829045SPawel Laszczak {
11783d829045SPawel Laszczak 	struct cdnsp_ep *pep = to_cdnsp_ep(ep);
11793d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
118018538a50SJohan Hovold 	unsigned long flags;
11813d829045SPawel Laszczak 	int ret;
11823d829045SPawel Laszczak 
11833d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
11843d829045SPawel Laszczak 	pep->ep_state |= EP_WEDGE;
11853d829045SPawel Laszczak 	ret = cdnsp_halt_endpoint(pdev, pep, 1);
11863d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
11873d829045SPawel Laszczak 
11883d829045SPawel Laszczak 	return ret;
11893d829045SPawel Laszczak }
11903d829045SPawel Laszczak 
11913d829045SPawel Laszczak static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
11923d829045SPawel Laszczak 	.enable		= cdnsp_gadget_ep_enable,
11933d829045SPawel Laszczak 	.disable	= cdnsp_gadget_ep_disable,
11943d829045SPawel Laszczak 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
11953d829045SPawel Laszczak 	.free_request	= cdnsp_gadget_ep_free_request,
11963d829045SPawel Laszczak 	.queue		= cdnsp_gadget_ep_queue,
11973d829045SPawel Laszczak 	.dequeue	= cdnsp_gadget_ep_dequeue,
11983d829045SPawel Laszczak 	.set_halt	= cdnsp_gadget_ep_set_halt,
11993d829045SPawel Laszczak 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
12003d829045SPawel Laszczak };
12013d829045SPawel Laszczak 
12023d829045SPawel Laszczak static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
12033d829045SPawel Laszczak 	.enable		= cdnsp_gadget_ep_enable,
12043d829045SPawel Laszczak 	.disable	= cdnsp_gadget_ep_disable,
12053d829045SPawel Laszczak 	.alloc_request	= cdnsp_gadget_ep_alloc_request,
12063d829045SPawel Laszczak 	.free_request	= cdnsp_gadget_ep_free_request,
12073d829045SPawel Laszczak 	.queue		= cdnsp_gadget_ep_queue,
12083d829045SPawel Laszczak 	.dequeue	= cdnsp_gadget_ep_dequeue,
12093d829045SPawel Laszczak 	.set_halt	= cdnsp_gadget_ep_set_halt,
12103d829045SPawel Laszczak 	.set_wedge	= cdnsp_gadget_ep_set_wedge,
12113d829045SPawel Laszczak };
12123d829045SPawel Laszczak 
12133d829045SPawel Laszczak void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
12143d829045SPawel Laszczak 			   struct cdnsp_request *preq,
12153d829045SPawel Laszczak 			   int status)
12163d829045SPawel Laszczak {
12173d829045SPawel Laszczak 	struct cdnsp_device *pdev = pep->pdev;
12183d829045SPawel Laszczak 
12193d829045SPawel Laszczak 	list_del(&preq->list);
12203d829045SPawel Laszczak 
12213d829045SPawel Laszczak 	if (preq->request.status == -EINPROGRESS)
12223d829045SPawel Laszczak 		preq->request.status = status;
12233d829045SPawel Laszczak 
12243d829045SPawel Laszczak 	usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
12253d829045SPawel Laszczak 					preq->direction);
12263d829045SPawel Laszczak 
1227118b2a32SPawel Laszczak 	trace_cdnsp_request_giveback(preq);
1228118b2a32SPawel Laszczak 
12293d829045SPawel Laszczak 	if (preq != &pdev->ep0_preq) {
12303d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
12313d829045SPawel Laszczak 		usb_gadget_giveback_request(&pep->endpoint, &preq->request);
12323d829045SPawel Laszczak 		spin_lock(&pdev->lock);
12333d829045SPawel Laszczak 	}
12343d829045SPawel Laszczak }
12353d829045SPawel Laszczak 
12363d829045SPawel Laszczak static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
12373d829045SPawel Laszczak 	.bLength =		USB_DT_ENDPOINT_SIZE,
12383d829045SPawel Laszczak 	.bDescriptorType =	USB_DT_ENDPOINT,
12393d829045SPawel Laszczak 	.bmAttributes =		USB_ENDPOINT_XFER_CONTROL,
12403d829045SPawel Laszczak };
12413d829045SPawel Laszczak 
12423d829045SPawel Laszczak static int cdnsp_run(struct cdnsp_device *pdev,
12433d829045SPawel Laszczak 		     enum usb_device_speed speed)
12443d829045SPawel Laszczak {
12453d829045SPawel Laszczak 	u32 fs_speed = 0;
12463d829045SPawel Laszczak 	u64 temp_64;
12473d829045SPawel Laszczak 	u32 temp;
12483d829045SPawel Laszczak 	int ret;
12493d829045SPawel Laszczak 
12503d829045SPawel Laszczak 	temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
12513d829045SPawel Laszczak 	temp_64 &= ~ERST_PTR_MASK;
12523d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_control);
12533d829045SPawel Laszczak 	temp &= ~IMOD_INTERVAL_MASK;
12543d829045SPawel Laszczak 	temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
12553d829045SPawel Laszczak 	writel(temp, &pdev->ir_set->irq_control);
12563d829045SPawel Laszczak 
12573d829045SPawel Laszczak 	temp = readl(&pdev->port3x_regs->mode_addr);
12583d829045SPawel Laszczak 
12593d829045SPawel Laszczak 	switch (speed) {
12603d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
12613d829045SPawel Laszczak 		temp |= CFG_3XPORT_SSP_SUPPORT;
12623d829045SPawel Laszczak 		break;
12633d829045SPawel Laszczak 	case USB_SPEED_SUPER:
12643d829045SPawel Laszczak 		temp &= ~CFG_3XPORT_SSP_SUPPORT;
12653d829045SPawel Laszczak 		break;
12663d829045SPawel Laszczak 	case USB_SPEED_HIGH:
12673d829045SPawel Laszczak 		break;
12683d829045SPawel Laszczak 	case USB_SPEED_FULL:
12693d829045SPawel Laszczak 		fs_speed = PORT_REG6_FORCE_FS;
12703d829045SPawel Laszczak 		break;
12713d829045SPawel Laszczak 	default:
12723d829045SPawel Laszczak 		dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
12733d829045SPawel Laszczak 			speed);
12743d829045SPawel Laszczak 		fallthrough;
12753d829045SPawel Laszczak 	case USB_SPEED_UNKNOWN:
12763d829045SPawel Laszczak 		/* Default to superspeed. */
12773d829045SPawel Laszczak 		speed = USB_SPEED_SUPER;
12783d829045SPawel Laszczak 		break;
12793d829045SPawel Laszczak 	}
12803d829045SPawel Laszczak 
12813d829045SPawel Laszczak 	if (speed >= USB_SPEED_SUPER) {
12823d829045SPawel Laszczak 		writel(temp, &pdev->port3x_regs->mode_addr);
12833d829045SPawel Laszczak 		cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
12843d829045SPawel Laszczak 				     XDEV_RXDETECT);
12853d829045SPawel Laszczak 	} else {
12863d829045SPawel Laszczak 		cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
12873d829045SPawel Laszczak 	}
12883d829045SPawel Laszczak 
12893d829045SPawel Laszczak 	cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
12903d829045SPawel Laszczak 			     XDEV_RXDETECT);
12913d829045SPawel Laszczak 
12923d829045SPawel Laszczak 	cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
12933d829045SPawel Laszczak 
12943d829045SPawel Laszczak 	writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
12953d829045SPawel Laszczak 
12963d829045SPawel Laszczak 	ret = cdnsp_start(pdev);
12973d829045SPawel Laszczak 	if (ret) {
12983d829045SPawel Laszczak 		ret = -ENODEV;
12993d829045SPawel Laszczak 		goto err;
13003d829045SPawel Laszczak 	}
13013d829045SPawel Laszczak 
13023d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->command);
13033d829045SPawel Laszczak 	temp |= (CMD_INTE);
13043d829045SPawel Laszczak 	writel(temp, &pdev->op_regs->command);
13053d829045SPawel Laszczak 
13063d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
13073d829045SPawel Laszczak 	writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
13083d829045SPawel Laszczak 
1309118b2a32SPawel Laszczak 	trace_cdnsp_init("Controller ready to work");
13103d829045SPawel Laszczak 	return 0;
13113d829045SPawel Laszczak err:
13123d829045SPawel Laszczak 	cdnsp_halt(pdev);
13133d829045SPawel Laszczak 	return ret;
13143d829045SPawel Laszczak }
13153d829045SPawel Laszczak 
13163d829045SPawel Laszczak static int cdnsp_gadget_udc_start(struct usb_gadget *g,
13173d829045SPawel Laszczak 				  struct usb_gadget_driver *driver)
13183d829045SPawel Laszczak {
13193d829045SPawel Laszczak 	enum usb_device_speed max_speed = driver->max_speed;
13203d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
13213d829045SPawel Laszczak 	unsigned long flags;
13223d829045SPawel Laszczak 	int ret;
13233d829045SPawel Laszczak 
13243d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
13253d829045SPawel Laszczak 	pdev->gadget_driver = driver;
13263d829045SPawel Laszczak 
13273d829045SPawel Laszczak 	/* limit speed if necessary */
13283d829045SPawel Laszczak 	max_speed = min(driver->max_speed, g->max_speed);
13293d829045SPawel Laszczak 	ret = cdnsp_run(pdev, max_speed);
13303d829045SPawel Laszczak 
13313d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
13323d829045SPawel Laszczak 
13333d829045SPawel Laszczak 	return ret;
13343d829045SPawel Laszczak }
13353d829045SPawel Laszczak 
13363d829045SPawel Laszczak /*
13373d829045SPawel Laszczak  * Update Event Ring Dequeue Pointer:
13383d829045SPawel Laszczak  * - When all events have finished
13393d829045SPawel Laszczak  * - To avoid "Event Ring Full Error" condition
13403d829045SPawel Laszczak  */
13413d829045SPawel Laszczak void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
13423d829045SPawel Laszczak 			       union cdnsp_trb *event_ring_deq,
13433d829045SPawel Laszczak 			       u8 clear_ehb)
13443d829045SPawel Laszczak {
13453d829045SPawel Laszczak 	u64 temp_64;
13463d829045SPawel Laszczak 	dma_addr_t deq;
13473d829045SPawel Laszczak 
13483d829045SPawel Laszczak 	temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
13493d829045SPawel Laszczak 
13503d829045SPawel Laszczak 	/* If necessary, update the HW's version of the event ring deq ptr. */
13513d829045SPawel Laszczak 	if (event_ring_deq != pdev->event_ring->dequeue) {
13523d829045SPawel Laszczak 		deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
13533d829045SPawel Laszczak 					    pdev->event_ring->dequeue);
13543d829045SPawel Laszczak 		temp_64 &= ERST_PTR_MASK;
13553d829045SPawel Laszczak 		temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
13563d829045SPawel Laszczak 	}
13573d829045SPawel Laszczak 
13583d829045SPawel Laszczak 	/* Clear the event handler busy flag (RW1C). */
13593d829045SPawel Laszczak 	if (clear_ehb)
13603d829045SPawel Laszczak 		temp_64 |= ERST_EHB;
13613d829045SPawel Laszczak 	else
13623d829045SPawel Laszczak 		temp_64 &= ~ERST_EHB;
13633d829045SPawel Laszczak 
13643d829045SPawel Laszczak 	cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
13653d829045SPawel Laszczak }
13663d829045SPawel Laszczak 
13673d829045SPawel Laszczak static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
13683d829045SPawel Laszczak {
13693d829045SPawel Laszczak 	struct cdnsp_segment *seg;
13703d829045SPawel Laszczak 	u64 val_64;
13713d829045SPawel Laszczak 	int i;
13723d829045SPawel Laszczak 
13733d829045SPawel Laszczak 	cdnsp_initialize_ring_info(pdev->cmd_ring);
13743d829045SPawel Laszczak 
13753d829045SPawel Laszczak 	seg = pdev->cmd_ring->first_seg;
13763d829045SPawel Laszczak 	for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
13773d829045SPawel Laszczak 		memset(seg->trbs, 0,
13783d829045SPawel Laszczak 		       sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
13793d829045SPawel Laszczak 		seg = seg->next;
13803d829045SPawel Laszczak 	}
13813d829045SPawel Laszczak 
13823d829045SPawel Laszczak 	/* Set the address in the Command Ring Control register. */
13833d829045SPawel Laszczak 	val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
13843d829045SPawel Laszczak 	val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
13853d829045SPawel Laszczak 		 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
13863d829045SPawel Laszczak 		 pdev->cmd_ring->cycle_state;
13873d829045SPawel Laszczak 	cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
13883d829045SPawel Laszczak }
13893d829045SPawel Laszczak 
13903d829045SPawel Laszczak static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
13913d829045SPawel Laszczak {
13923d829045SPawel Laszczak 	struct cdnsp_segment *event_deq_seg;
13933d829045SPawel Laszczak 	union cdnsp_trb *event_ring_deq;
13943d829045SPawel Laszczak 	union cdnsp_trb *event;
13953d829045SPawel Laszczak 	u32 cycle_bit;
13963d829045SPawel Laszczak 
13973d829045SPawel Laszczak 	event_ring_deq = pdev->event_ring->dequeue;
13983d829045SPawel Laszczak 	event_deq_seg = pdev->event_ring->deq_seg;
13993d829045SPawel Laszczak 	event = pdev->event_ring->dequeue;
14003d829045SPawel Laszczak 
14013d829045SPawel Laszczak 	/* Update ring dequeue pointer. */
14023d829045SPawel Laszczak 	while (1) {
14033d829045SPawel Laszczak 		cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
14043d829045SPawel Laszczak 
14053d829045SPawel Laszczak 		/* Does the controller or driver own the TRB? */
14063d829045SPawel Laszczak 		if (cycle_bit != pdev->event_ring->cycle_state)
14073d829045SPawel Laszczak 			break;
14083d829045SPawel Laszczak 
14093d829045SPawel Laszczak 		cdnsp_inc_deq(pdev, pdev->event_ring);
14103d829045SPawel Laszczak 
14113d829045SPawel Laszczak 		if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
14123d829045SPawel Laszczak 			event++;
14133d829045SPawel Laszczak 			continue;
14143d829045SPawel Laszczak 		}
14153d829045SPawel Laszczak 
14163d829045SPawel Laszczak 		if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
14173d829045SPawel Laszczak 					   event))
14183d829045SPawel Laszczak 			cycle_bit ^= 1;
14193d829045SPawel Laszczak 
14203d829045SPawel Laszczak 		event_deq_seg = event_deq_seg->next;
14213d829045SPawel Laszczak 		event = event_deq_seg->trbs;
14223d829045SPawel Laszczak 	}
14233d829045SPawel Laszczak 
14243d829045SPawel Laszczak 	cdnsp_update_erst_dequeue(pdev,  event_ring_deq, 1);
14253d829045SPawel Laszczak }
14263d829045SPawel Laszczak 
14273d829045SPawel Laszczak static void cdnsp_stop(struct cdnsp_device *pdev)
14283d829045SPawel Laszczak {
14293d829045SPawel Laszczak 	u32 temp;
14303d829045SPawel Laszczak 
14313d829045SPawel Laszczak 	cdnsp_cmd_flush_ep(pdev, &pdev->eps[0]);
14323d829045SPawel Laszczak 
14333d829045SPawel Laszczak 	/* Remove internally queued request for ep0. */
14343d829045SPawel Laszczak 	if (!list_empty(&pdev->eps[0].pending_list)) {
14353d829045SPawel Laszczak 		struct cdnsp_request *req;
14363d829045SPawel Laszczak 
14373d829045SPawel Laszczak 		req = next_request(&pdev->eps[0].pending_list);
14383d829045SPawel Laszczak 		if (req == &pdev->ep0_preq)
14393d829045SPawel Laszczak 			cdnsp_ep_dequeue(&pdev->eps[0], req);
14403d829045SPawel Laszczak 	}
14413d829045SPawel Laszczak 
14423d829045SPawel Laszczak 	cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
14433d829045SPawel Laszczak 	cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
14443d829045SPawel Laszczak 	cdnsp_disable_slot(pdev);
14453d829045SPawel Laszczak 	cdnsp_halt(pdev);
14463d829045SPawel Laszczak 
14473d829045SPawel Laszczak 	temp = readl(&pdev->op_regs->status);
14483d829045SPawel Laszczak 	writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
14493d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
14503d829045SPawel Laszczak 	writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
14513d829045SPawel Laszczak 
14523d829045SPawel Laszczak 	cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
14533d829045SPawel Laszczak 	cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
14543d829045SPawel Laszczak 
14553d829045SPawel Laszczak 	/* Clear interrupt line */
14563d829045SPawel Laszczak 	temp = readl(&pdev->ir_set->irq_pending);
14573d829045SPawel Laszczak 	temp |= IMAN_IP;
14583d829045SPawel Laszczak 	writel(temp, &pdev->ir_set->irq_pending);
14593d829045SPawel Laszczak 
14603d829045SPawel Laszczak 	cdnsp_consume_all_events(pdev);
14613d829045SPawel Laszczak 	cdnsp_clear_cmd_ring(pdev);
1462118b2a32SPawel Laszczak 
1463118b2a32SPawel Laszczak 	trace_cdnsp_exit("Controller stopped.");
14643d829045SPawel Laszczak }
14653d829045SPawel Laszczak 
14663d829045SPawel Laszczak /*
14673d829045SPawel Laszczak  * Stop controller.
14683d829045SPawel Laszczak  * This function is called by the gadget core when the driver is removed.
14693d829045SPawel Laszczak  * Disable slot, disable IRQs, and quiesce the controller.
14703d829045SPawel Laszczak  */
14713d829045SPawel Laszczak static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
14723d829045SPawel Laszczak {
14733d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
14743d829045SPawel Laszczak 	unsigned long flags;
14753d829045SPawel Laszczak 
14763d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
14773d829045SPawel Laszczak 	cdnsp_stop(pdev);
14783d829045SPawel Laszczak 	pdev->gadget_driver = NULL;
14793d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
14803d829045SPawel Laszczak 
14813d829045SPawel Laszczak 	return 0;
14823d829045SPawel Laszczak }
14833d829045SPawel Laszczak 
14843d829045SPawel Laszczak static int cdnsp_gadget_get_frame(struct usb_gadget *g)
14853d829045SPawel Laszczak {
14863d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
14873d829045SPawel Laszczak 
14883d829045SPawel Laszczak 	return cdnsp_get_frame(pdev);
14893d829045SPawel Laszczak }
14903d829045SPawel Laszczak 
14913d829045SPawel Laszczak static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
14923d829045SPawel Laszczak {
14933d829045SPawel Laszczak 	struct cdnsp_port_regs __iomem *port_regs;
14943d829045SPawel Laszczak 	u32 portpm, portsc;
14953d829045SPawel Laszczak 
14963d829045SPawel Laszczak 	port_regs = pdev->active_port->regs;
14973d829045SPawel Laszczak 	portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
14983d829045SPawel Laszczak 
14993d829045SPawel Laszczak 	/* Remote wakeup feature is not enabled by host. */
15003d829045SPawel Laszczak 	if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
15013d829045SPawel Laszczak 		portpm = readl(&port_regs->portpmsc);
15023d829045SPawel Laszczak 
15033d829045SPawel Laszczak 		if (!(portpm & PORT_RWE))
15043d829045SPawel Laszczak 			return;
15053d829045SPawel Laszczak 	}
15063d829045SPawel Laszczak 
15073d829045SPawel Laszczak 	if (portsc == XDEV_U3 && !pdev->may_wakeup)
15083d829045SPawel Laszczak 		return;
15093d829045SPawel Laszczak 
15103d829045SPawel Laszczak 	cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
15113d829045SPawel Laszczak 
15123d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
15133d829045SPawel Laszczak }
15143d829045SPawel Laszczak 
15153d829045SPawel Laszczak static int cdnsp_gadget_wakeup(struct usb_gadget *g)
15163d829045SPawel Laszczak {
15173d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
15183d829045SPawel Laszczak 	unsigned long flags;
15193d829045SPawel Laszczak 
15203d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
15213d829045SPawel Laszczak 	__cdnsp_gadget_wakeup(pdev);
15223d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
15233d829045SPawel Laszczak 
15243d829045SPawel Laszczak 	return 0;
15253d829045SPawel Laszczak }
15263d829045SPawel Laszczak 
15273d829045SPawel Laszczak static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
15283d829045SPawel Laszczak 					int is_selfpowered)
15293d829045SPawel Laszczak {
15303d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(g);
15313d829045SPawel Laszczak 	unsigned long flags;
15323d829045SPawel Laszczak 
15333d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
15343d829045SPawel Laszczak 	g->is_selfpowered = !!is_selfpowered;
15353d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
15363d829045SPawel Laszczak 
15373d829045SPawel Laszczak 	return 0;
15383d829045SPawel Laszczak }
15393d829045SPawel Laszczak 
15403d829045SPawel Laszczak static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
15413d829045SPawel Laszczak {
15423d829045SPawel Laszczak 	struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
15433d829045SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(pdev->dev);
15443d829045SPawel Laszczak 
1545118b2a32SPawel Laszczak 	trace_cdnsp_pullup(is_on);
1546118b2a32SPawel Laszczak 
15473d829045SPawel Laszczak 	if (!is_on) {
15483d829045SPawel Laszczak 		cdnsp_reset_device(pdev);
15493d829045SPawel Laszczak 		cdns_clear_vbus(cdns);
15503d829045SPawel Laszczak 	} else {
15513d829045SPawel Laszczak 		cdns_set_vbus(cdns);
15523d829045SPawel Laszczak 	}
15533d829045SPawel Laszczak 	return 0;
15543d829045SPawel Laszczak }
15553d829045SPawel Laszczak 
15567650778eSZou Wei static const struct usb_gadget_ops cdnsp_gadget_ops = {
15573d829045SPawel Laszczak 	.get_frame		= cdnsp_gadget_get_frame,
15583d829045SPawel Laszczak 	.wakeup			= cdnsp_gadget_wakeup,
15593d829045SPawel Laszczak 	.set_selfpowered	= cdnsp_gadget_set_selfpowered,
15603d829045SPawel Laszczak 	.pullup			= cdnsp_gadget_pullup,
15613d829045SPawel Laszczak 	.udc_start		= cdnsp_gadget_udc_start,
15623d829045SPawel Laszczak 	.udc_stop		= cdnsp_gadget_udc_stop,
15633d829045SPawel Laszczak };
15643d829045SPawel Laszczak 
15653d829045SPawel Laszczak static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
15663d829045SPawel Laszczak 				   struct cdnsp_ep *pep)
15673d829045SPawel Laszczak {
15683d829045SPawel Laszczak 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
15693d829045SPawel Laszczak 	int endpoints;
15703d829045SPawel Laszczak 
15713d829045SPawel Laszczak 	reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
15723d829045SPawel Laszczak 
15733d829045SPawel Laszczak 	if (!pep->direction) {
15743d829045SPawel Laszczak 		pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
15753d829045SPawel Laszczak 		pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
15763d829045SPawel Laszczak 		pep->buffering = (pep->buffering + 1) / 2;
15773d829045SPawel Laszczak 		pep->buffering_period = (pep->buffering_period + 1) / 2;
15783d829045SPawel Laszczak 		return;
15793d829045SPawel Laszczak 	}
15803d829045SPawel Laszczak 
158116e36101SPawel Laszczak 	endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
15823d829045SPawel Laszczak 
15833d829045SPawel Laszczak 	/* Set to XBUF_TX_TAG_MASK_0 register. */
15843d829045SPawel Laszczak 	reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
15853d829045SPawel Laszczak 	/* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
15863d829045SPawel Laszczak 	reg += pep->number * sizeof(u32) * 2;
15873d829045SPawel Laszczak 
15883d829045SPawel Laszczak 	pep->buffering = (readl(reg) + 1) / 2;
15893d829045SPawel Laszczak 	pep->buffering_period = pep->buffering;
15903d829045SPawel Laszczak }
15913d829045SPawel Laszczak 
15923d829045SPawel Laszczak static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
15933d829045SPawel Laszczak {
15943d829045SPawel Laszczak 	int max_streams = HCC_MAX_PSA(pdev->hcc_params);
15953d829045SPawel Laszczak 	struct cdnsp_ep *pep;
15963d829045SPawel Laszczak 	int i;
15973d829045SPawel Laszczak 
15983d829045SPawel Laszczak 	INIT_LIST_HEAD(&pdev->gadget.ep_list);
15993d829045SPawel Laszczak 
16003d829045SPawel Laszczak 	if (max_streams < STREAM_LOG_STREAMS) {
16013d829045SPawel Laszczak 		dev_err(pdev->dev, "Stream size %d not supported\n",
16023d829045SPawel Laszczak 			max_streams);
16033d829045SPawel Laszczak 		return -EINVAL;
16043d829045SPawel Laszczak 	}
16053d829045SPawel Laszczak 
16063d829045SPawel Laszczak 	max_streams = STREAM_LOG_STREAMS;
16073d829045SPawel Laszczak 
16083d829045SPawel Laszczak 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
16093d829045SPawel Laszczak 		bool direction = !(i & 1); /* Start from OUT endpoint. */
16103d829045SPawel Laszczak 		u8 epnum = ((i + 1) >> 1);
16113d829045SPawel Laszczak 
16123d829045SPawel Laszczak 		if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
16133d829045SPawel Laszczak 			continue;
16143d829045SPawel Laszczak 
16153d829045SPawel Laszczak 		pep = &pdev->eps[i];
16163d829045SPawel Laszczak 		pep->pdev = pdev;
16173d829045SPawel Laszczak 		pep->number = epnum;
16183d829045SPawel Laszczak 		pep->direction = direction; /* 0 for OUT, 1 for IN. */
16193d829045SPawel Laszczak 
16203d829045SPawel Laszczak 		/*
16213d829045SPawel Laszczak 		 * Ep0 is bidirectional, so ep0in and ep0out are represented by
16223d829045SPawel Laszczak 		 * pdev->eps[0]
16233d829045SPawel Laszczak 		 */
16243d829045SPawel Laszczak 		if (epnum == 0) {
16253d829045SPawel Laszczak 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
16263d829045SPawel Laszczak 				 epnum, "BiDir");
16273d829045SPawel Laszczak 
16283d829045SPawel Laszczak 			pep->idx = 0;
16293d829045SPawel Laszczak 			usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
16303d829045SPawel Laszczak 			pep->endpoint.maxburst = 1;
16313d829045SPawel Laszczak 			pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
16323d829045SPawel Laszczak 			pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
16333d829045SPawel Laszczak 			pep->endpoint.comp_desc = NULL;
16343d829045SPawel Laszczak 			pep->endpoint.caps.type_control = true;
16353d829045SPawel Laszczak 			pep->endpoint.caps.dir_in = true;
16363d829045SPawel Laszczak 			pep->endpoint.caps.dir_out = true;
16373d829045SPawel Laszczak 
16383d829045SPawel Laszczak 			pdev->ep0_preq.epnum = pep->number;
16393d829045SPawel Laszczak 			pdev->ep0_preq.pep = pep;
16403d829045SPawel Laszczak 			pdev->gadget.ep0 = &pep->endpoint;
16413d829045SPawel Laszczak 		} else {
16423d829045SPawel Laszczak 			snprintf(pep->name, sizeof(pep->name), "ep%d%s",
16433d829045SPawel Laszczak 				 epnum, (pep->direction) ? "in" : "out");
16443d829045SPawel Laszczak 
16453d829045SPawel Laszczak 			pep->idx =  (epnum * 2 + (direction ? 1 : 0)) - 1;
16463d829045SPawel Laszczak 			usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
16473d829045SPawel Laszczak 
16483d829045SPawel Laszczak 			pep->endpoint.max_streams = max_streams;
16493d829045SPawel Laszczak 			pep->endpoint.ops = &cdnsp_gadget_ep_ops;
16503d829045SPawel Laszczak 			list_add_tail(&pep->endpoint.ep_list,
16513d829045SPawel Laszczak 				      &pdev->gadget.ep_list);
16523d829045SPawel Laszczak 
16533d829045SPawel Laszczak 			pep->endpoint.caps.type_iso = true;
16543d829045SPawel Laszczak 			pep->endpoint.caps.type_bulk = true;
16553d829045SPawel Laszczak 			pep->endpoint.caps.type_int = true;
16563d829045SPawel Laszczak 
16573d829045SPawel Laszczak 			pep->endpoint.caps.dir_in = direction;
16583d829045SPawel Laszczak 			pep->endpoint.caps.dir_out = !direction;
16593d829045SPawel Laszczak 		}
16603d829045SPawel Laszczak 
16613d829045SPawel Laszczak 		pep->endpoint.name = pep->name;
16623d829045SPawel Laszczak 		pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
16633d829045SPawel Laszczak 		pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
16643d829045SPawel Laszczak 		cdnsp_get_ep_buffering(pdev, pep);
16653d829045SPawel Laszczak 
16663d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
16673d829045SPawel Laszczak 			"CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
16683d829045SPawel Laszczak 			"SupDir IN: %s, OUT: %s\n",
16693d829045SPawel Laszczak 			pep->name, 1024,
16703d829045SPawel Laszczak 			(pep->endpoint.caps.type_control) ? "yes" : "no",
16713d829045SPawel Laszczak 			(pep->endpoint.caps.type_int) ? "yes" : "no",
16723d829045SPawel Laszczak 			(pep->endpoint.caps.type_bulk) ? "yes" : "no",
16733d829045SPawel Laszczak 			(pep->endpoint.caps.type_iso) ? "yes" : "no",
16743d829045SPawel Laszczak 			(pep->endpoint.caps.dir_in) ? "yes" : "no",
16753d829045SPawel Laszczak 			(pep->endpoint.caps.dir_out) ? "yes" : "no");
16763d829045SPawel Laszczak 
16773d829045SPawel Laszczak 		INIT_LIST_HEAD(&pep->pending_list);
16783d829045SPawel Laszczak 	}
16793d829045SPawel Laszczak 
16803d829045SPawel Laszczak 	return 0;
16813d829045SPawel Laszczak }
16823d829045SPawel Laszczak 
16833d829045SPawel Laszczak static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
16843d829045SPawel Laszczak {
16853d829045SPawel Laszczak 	struct cdnsp_ep *pep;
16863d829045SPawel Laszczak 	int i;
16873d829045SPawel Laszczak 
16883d829045SPawel Laszczak 	for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
16893d829045SPawel Laszczak 		pep = &pdev->eps[i];
16903d829045SPawel Laszczak 		if (pep->number != 0 && pep->out_ctx)
16913d829045SPawel Laszczak 			list_del(&pep->endpoint.ep_list);
16923d829045SPawel Laszczak 	}
16933d829045SPawel Laszczak }
16943d829045SPawel Laszczak 
16953d829045SPawel Laszczak void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
16963d829045SPawel Laszczak {
16973d829045SPawel Laszczak 	pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
16983d829045SPawel Laszczak 
16993d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
17003d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17013d829045SPawel Laszczak 		pdev->gadget_driver->disconnect(&pdev->gadget);
17023d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17033d829045SPawel Laszczak 	}
17043d829045SPawel Laszczak 
17053d829045SPawel Laszczak 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
17063d829045SPawel Laszczak 	usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
17073d829045SPawel Laszczak 
17083d829045SPawel Laszczak 	pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
17093d829045SPawel Laszczak }
17103d829045SPawel Laszczak 
17113d829045SPawel Laszczak void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
17123d829045SPawel Laszczak {
17133d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
17143d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17153d829045SPawel Laszczak 		pdev->gadget_driver->suspend(&pdev->gadget);
17163d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17173d829045SPawel Laszczak 	}
17183d829045SPawel Laszczak }
17193d829045SPawel Laszczak 
17203d829045SPawel Laszczak void cdnsp_resume_gadget(struct cdnsp_device *pdev)
17213d829045SPawel Laszczak {
17223d829045SPawel Laszczak 	if (pdev->gadget_driver && pdev->gadget_driver->resume) {
17233d829045SPawel Laszczak 		spin_unlock(&pdev->lock);
17243d829045SPawel Laszczak 		pdev->gadget_driver->resume(&pdev->gadget);
17253d829045SPawel Laszczak 		spin_lock(&pdev->lock);
17263d829045SPawel Laszczak 	}
17273d829045SPawel Laszczak }
17283d829045SPawel Laszczak 
17293d829045SPawel Laszczak void cdnsp_irq_reset(struct cdnsp_device *pdev)
17303d829045SPawel Laszczak {
17313d829045SPawel Laszczak 	struct cdnsp_port_regs __iomem *port_regs;
17323d829045SPawel Laszczak 
17333d829045SPawel Laszczak 	cdnsp_reset_device(pdev);
17343d829045SPawel Laszczak 
17353d829045SPawel Laszczak 	port_regs = pdev->active_port->regs;
17363d829045SPawel Laszczak 	pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
17373d829045SPawel Laszczak 
17383d829045SPawel Laszczak 	spin_unlock(&pdev->lock);
17393d829045SPawel Laszczak 	usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
17403d829045SPawel Laszczak 	spin_lock(&pdev->lock);
17413d829045SPawel Laszczak 
17423d829045SPawel Laszczak 	switch (pdev->gadget.speed) {
17433d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
17443d829045SPawel Laszczak 	case USB_SPEED_SUPER:
17453d829045SPawel Laszczak 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
17463d829045SPawel Laszczak 		pdev->gadget.ep0->maxpacket = 512;
17473d829045SPawel Laszczak 		break;
17483d829045SPawel Laszczak 	case USB_SPEED_HIGH:
17493d829045SPawel Laszczak 	case USB_SPEED_FULL:
17503d829045SPawel Laszczak 		cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
17513d829045SPawel Laszczak 		pdev->gadget.ep0->maxpacket = 64;
17523d829045SPawel Laszczak 		break;
17533d829045SPawel Laszczak 	default:
17543d829045SPawel Laszczak 		/* Low speed is not supported. */
17553d829045SPawel Laszczak 		dev_err(pdev->dev, "Unknown device speed\n");
17563d829045SPawel Laszczak 		break;
17573d829045SPawel Laszczak 	}
17583d829045SPawel Laszczak 
17593d829045SPawel Laszczak 	cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
17603d829045SPawel Laszczak 	cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
17613d829045SPawel Laszczak 	usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
17623d829045SPawel Laszczak }
17633d829045SPawel Laszczak 
17643d829045SPawel Laszczak static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
17653d829045SPawel Laszczak {
17663d829045SPawel Laszczak 	void __iomem *reg = &pdev->cap_regs->hc_capbase;
17673d829045SPawel Laszczak 
17683d829045SPawel Laszczak 	reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
176916e36101SPawel Laszczak 	pdev->rev_cap  = reg;
17703d829045SPawel Laszczak 
17713d829045SPawel Laszczak 	dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
177216e36101SPawel Laszczak 		 readl(&pdev->rev_cap->ctrl_revision),
177316e36101SPawel Laszczak 		 readl(&pdev->rev_cap->rtl_revision),
177416e36101SPawel Laszczak 		 readl(&pdev->rev_cap->ep_supported),
177516e36101SPawel Laszczak 		 readl(&pdev->rev_cap->rx_buff_size),
177616e36101SPawel Laszczak 		 readl(&pdev->rev_cap->tx_buff_size));
17773d829045SPawel Laszczak }
17783d829045SPawel Laszczak 
17793d829045SPawel Laszczak static int cdnsp_gen_setup(struct cdnsp_device *pdev)
17803d829045SPawel Laszczak {
17813d829045SPawel Laszczak 	int ret;
17823d829045SPawel Laszczak 	u32 reg;
17833d829045SPawel Laszczak 
17843d829045SPawel Laszczak 	pdev->cap_regs = pdev->regs;
17853d829045SPawel Laszczak 	pdev->op_regs = pdev->regs +
17863d829045SPawel Laszczak 		HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
17873d829045SPawel Laszczak 	pdev->run_regs = pdev->regs +
17883d829045SPawel Laszczak 		(readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
17893d829045SPawel Laszczak 
17903d829045SPawel Laszczak 	/* Cache read-only capability registers */
17913d829045SPawel Laszczak 	pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
17923d829045SPawel Laszczak 	pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
17933d829045SPawel Laszczak 	pdev->hci_version = HC_VERSION(pdev->hcc_params);
17943d829045SPawel Laszczak 	pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
17953d829045SPawel Laszczak 
17963d829045SPawel Laszczak 	cdnsp_get_rev_cap(pdev);
17973d829045SPawel Laszczak 
17983d829045SPawel Laszczak 	/* Make sure the Device Controller is halted. */
17993d829045SPawel Laszczak 	ret = cdnsp_halt(pdev);
18003d829045SPawel Laszczak 	if (ret)
18013d829045SPawel Laszczak 		return ret;
18023d829045SPawel Laszczak 
18033d829045SPawel Laszczak 	/* Reset the internal controller memory state and registers. */
18043d829045SPawel Laszczak 	ret = cdnsp_reset(pdev);
18053d829045SPawel Laszczak 	if (ret)
18063d829045SPawel Laszczak 		return ret;
18073d829045SPawel Laszczak 
18083d829045SPawel Laszczak 	/*
18093d829045SPawel Laszczak 	 * Set dma_mask and coherent_dma_mask to 64-bits,
18103d829045SPawel Laszczak 	 * if controller supports 64-bit addressing.
18113d829045SPawel Laszczak 	 */
18123d829045SPawel Laszczak 	if (HCC_64BIT_ADDR(pdev->hcc_params) &&
18133d829045SPawel Laszczak 	    !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
18143d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
18153d829045SPawel Laszczak 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
18163d829045SPawel Laszczak 	} else {
18173d829045SPawel Laszczak 		/*
18183d829045SPawel Laszczak 		 * This is to avoid error in cases where a 32-bit USB
18193d829045SPawel Laszczak 		 * controller is used on a 64-bit capable system.
18203d829045SPawel Laszczak 		 */
18213d829045SPawel Laszczak 		ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
18223d829045SPawel Laszczak 		if (ret)
18233d829045SPawel Laszczak 			return ret;
18243d829045SPawel Laszczak 
18253d829045SPawel Laszczak 		dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
18263d829045SPawel Laszczak 		dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
18273d829045SPawel Laszczak 	}
18283d829045SPawel Laszczak 
18293d829045SPawel Laszczak 	spin_lock_init(&pdev->lock);
18303d829045SPawel Laszczak 
1831dc68ba6cSPawel Laszczak 	ret = cdnsp_mem_init(pdev);
18323d829045SPawel Laszczak 	if (ret)
18333d829045SPawel Laszczak 		return ret;
18343d829045SPawel Laszczak 
18353d829045SPawel Laszczak 	/*
18363d829045SPawel Laszczak 	 * Software workaround for U1: after transition
18373d829045SPawel Laszczak 	 * to U1 the controller starts gating clock, and in some cases,
18383d829045SPawel Laszczak 	 * it causes that controller stack.
18393d829045SPawel Laszczak 	 */
18403d829045SPawel Laszczak 	reg = readl(&pdev->port3x_regs->mode_2);
18413d829045SPawel Laszczak 	reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
18423d829045SPawel Laszczak 	writel(reg, &pdev->port3x_regs->mode_2);
18433d829045SPawel Laszczak 
18443d829045SPawel Laszczak 	return 0;
18453d829045SPawel Laszczak }
18463d829045SPawel Laszczak 
18473d829045SPawel Laszczak static int __cdnsp_gadget_init(struct cdns *cdns)
18483d829045SPawel Laszczak {
18493d829045SPawel Laszczak 	struct cdnsp_device *pdev;
18503d829045SPawel Laszczak 	u32 max_speed;
18513d829045SPawel Laszczak 	int ret = -ENOMEM;
18523d829045SPawel Laszczak 
18533d829045SPawel Laszczak 	cdns_drd_gadget_on(cdns);
18543d829045SPawel Laszczak 
18553d829045SPawel Laszczak 	pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
18563d829045SPawel Laszczak 	if (!pdev)
18573d829045SPawel Laszczak 		return -ENOMEM;
18583d829045SPawel Laszczak 
18593d829045SPawel Laszczak 	pm_runtime_get_sync(cdns->dev);
18603d829045SPawel Laszczak 
18613d829045SPawel Laszczak 	cdns->gadget_dev = pdev;
18623d829045SPawel Laszczak 	pdev->dev = cdns->dev;
18633d829045SPawel Laszczak 	pdev->regs = cdns->dev_regs;
18643d829045SPawel Laszczak 	max_speed = usb_get_maximum_speed(cdns->dev);
18653d829045SPawel Laszczak 
18663d829045SPawel Laszczak 	switch (max_speed) {
18673d829045SPawel Laszczak 	case USB_SPEED_FULL:
18683d829045SPawel Laszczak 	case USB_SPEED_HIGH:
18693d829045SPawel Laszczak 	case USB_SPEED_SUPER:
18703d829045SPawel Laszczak 	case USB_SPEED_SUPER_PLUS:
18713d829045SPawel Laszczak 		break;
18723d829045SPawel Laszczak 	default:
18733d829045SPawel Laszczak 		dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
18743d829045SPawel Laszczak 		fallthrough;
18753d829045SPawel Laszczak 	case USB_SPEED_UNKNOWN:
18763d829045SPawel Laszczak 		/* Default to SSP */
18773d829045SPawel Laszczak 		max_speed = USB_SPEED_SUPER_PLUS;
18783d829045SPawel Laszczak 		break;
18793d829045SPawel Laszczak 	}
18803d829045SPawel Laszczak 
18813d829045SPawel Laszczak 	pdev->gadget.ops = &cdnsp_gadget_ops;
18823d829045SPawel Laszczak 	pdev->gadget.name = "cdnsp-gadget";
18833d829045SPawel Laszczak 	pdev->gadget.speed = USB_SPEED_UNKNOWN;
18843d829045SPawel Laszczak 	pdev->gadget.sg_supported = 1;
1885*aa82f94eSPawel Laszczak 	pdev->gadget.max_speed = max_speed;
18863d829045SPawel Laszczak 	pdev->gadget.lpm_capable = 1;
18873d829045SPawel Laszczak 
18883d829045SPawel Laszczak 	pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
18893d829045SPawel Laszczak 	if (!pdev->setup_buf)
18903d829045SPawel Laszczak 		goto free_pdev;
18913d829045SPawel Laszczak 
18923d829045SPawel Laszczak 	/*
18933d829045SPawel Laszczak 	 * Controller supports not aligned buffer but it should improve
18943d829045SPawel Laszczak 	 * performance.
18953d829045SPawel Laszczak 	 */
18963d829045SPawel Laszczak 	pdev->gadget.quirk_ep_out_aligned_size = true;
18973d829045SPawel Laszczak 
18983d829045SPawel Laszczak 	ret = cdnsp_gen_setup(pdev);
18993d829045SPawel Laszczak 	if (ret) {
19003d829045SPawel Laszczak 		dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
19013d829045SPawel Laszczak 		goto free_setup;
19023d829045SPawel Laszczak 	}
19033d829045SPawel Laszczak 
19043d829045SPawel Laszczak 	ret = cdnsp_gadget_init_endpoints(pdev);
19053d829045SPawel Laszczak 	if (ret) {
19063d829045SPawel Laszczak 		dev_err(pdev->dev, "failed to initialize endpoints\n");
19073d829045SPawel Laszczak 		goto halt_pdev;
19083d829045SPawel Laszczak 	}
19093d829045SPawel Laszczak 
19103d829045SPawel Laszczak 	ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
19113d829045SPawel Laszczak 	if (ret) {
19123d829045SPawel Laszczak 		dev_err(pdev->dev, "failed to register udc\n");
19133d829045SPawel Laszczak 		goto free_endpoints;
19143d829045SPawel Laszczak 	}
19153d829045SPawel Laszczak 
19163d829045SPawel Laszczak 	ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
19173d829045SPawel Laszczak 					cdnsp_irq_handler,
19183d829045SPawel Laszczak 					cdnsp_thread_irq_handler, IRQF_SHARED,
19193d829045SPawel Laszczak 					dev_name(pdev->dev), pdev);
19203d829045SPawel Laszczak 	if (ret)
19213d829045SPawel Laszczak 		goto del_gadget;
19223d829045SPawel Laszczak 
19233d829045SPawel Laszczak 	return 0;
19243d829045SPawel Laszczak 
19253d829045SPawel Laszczak del_gadget:
19263d829045SPawel Laszczak 	usb_del_gadget_udc(&pdev->gadget);
19273d829045SPawel Laszczak free_endpoints:
19283d829045SPawel Laszczak 	cdnsp_gadget_free_endpoints(pdev);
19293d829045SPawel Laszczak halt_pdev:
19303d829045SPawel Laszczak 	cdnsp_halt(pdev);
19313d829045SPawel Laszczak 	cdnsp_reset(pdev);
19323d829045SPawel Laszczak 	cdnsp_mem_cleanup(pdev);
19333d829045SPawel Laszczak free_setup:
19343d829045SPawel Laszczak 	kfree(pdev->setup_buf);
19353d829045SPawel Laszczak free_pdev:
19363d829045SPawel Laszczak 	kfree(pdev);
19373d829045SPawel Laszczak 
19383d829045SPawel Laszczak 	return ret;
19393d829045SPawel Laszczak }
19403d829045SPawel Laszczak 
19413d829045SPawel Laszczak static void cdnsp_gadget_exit(struct cdns *cdns)
19423d829045SPawel Laszczak {
19433d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19443d829045SPawel Laszczak 
19453d829045SPawel Laszczak 	devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
19463d829045SPawel Laszczak 	pm_runtime_mark_last_busy(cdns->dev);
19473d829045SPawel Laszczak 	pm_runtime_put_autosuspend(cdns->dev);
19483d829045SPawel Laszczak 	usb_del_gadget_udc(&pdev->gadget);
19493d829045SPawel Laszczak 	cdnsp_gadget_free_endpoints(pdev);
19503d829045SPawel Laszczak 	cdnsp_mem_cleanup(pdev);
19513d829045SPawel Laszczak 	kfree(pdev);
19523d829045SPawel Laszczak 	cdns->gadget_dev = NULL;
19533d829045SPawel Laszczak 	cdns_drd_gadget_off(cdns);
19543d829045SPawel Laszczak }
19553d829045SPawel Laszczak 
19563d829045SPawel Laszczak static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
19573d829045SPawel Laszczak {
19583d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19593d829045SPawel Laszczak 	unsigned long flags;
19603d829045SPawel Laszczak 
19613d829045SPawel Laszczak 	if (pdev->link_state == XDEV_U3)
19623d829045SPawel Laszczak 		return 0;
19633d829045SPawel Laszczak 
19643d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
19653d829045SPawel Laszczak 	cdnsp_disconnect_gadget(pdev);
19663d829045SPawel Laszczak 	cdnsp_stop(pdev);
19673d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
19683d829045SPawel Laszczak 
19693d829045SPawel Laszczak 	return 0;
19703d829045SPawel Laszczak }
19713d829045SPawel Laszczak 
19723d829045SPawel Laszczak static int cdnsp_gadget_resume(struct cdns *cdns, bool hibernated)
19733d829045SPawel Laszczak {
19743d829045SPawel Laszczak 	struct cdnsp_device *pdev = cdns->gadget_dev;
19753d829045SPawel Laszczak 	enum usb_device_speed max_speed;
19763d829045SPawel Laszczak 	unsigned long flags;
19773d829045SPawel Laszczak 	int ret;
19783d829045SPawel Laszczak 
19793d829045SPawel Laszczak 	if (!pdev->gadget_driver)
19803d829045SPawel Laszczak 		return 0;
19813d829045SPawel Laszczak 
19823d829045SPawel Laszczak 	spin_lock_irqsave(&pdev->lock, flags);
19833d829045SPawel Laszczak 	max_speed = pdev->gadget_driver->max_speed;
19843d829045SPawel Laszczak 
19853d829045SPawel Laszczak 	/* Limit speed if necessary. */
19863d829045SPawel Laszczak 	max_speed = min(max_speed, pdev->gadget.max_speed);
19873d829045SPawel Laszczak 
19883d829045SPawel Laszczak 	ret = cdnsp_run(pdev, max_speed);
19893d829045SPawel Laszczak 
19903d829045SPawel Laszczak 	if (pdev->link_state == XDEV_U3)
19913d829045SPawel Laszczak 		__cdnsp_gadget_wakeup(pdev);
19923d829045SPawel Laszczak 
19933d829045SPawel Laszczak 	spin_unlock_irqrestore(&pdev->lock, flags);
19943d829045SPawel Laszczak 
19953d829045SPawel Laszczak 	return ret;
19963d829045SPawel Laszczak }
19973d829045SPawel Laszczak 
19983d829045SPawel Laszczak /**
19993d829045SPawel Laszczak  * cdnsp_gadget_init - initialize device structure
20003d829045SPawel Laszczak  * @cdns: cdnsp instance
20013d829045SPawel Laszczak  *
20023d829045SPawel Laszczak  * This function initializes the gadget.
20033d829045SPawel Laszczak  */
20043d829045SPawel Laszczak int cdnsp_gadget_init(struct cdns *cdns)
20053d829045SPawel Laszczak {
20063d829045SPawel Laszczak 	struct cdns_role_driver *rdrv;
20073d829045SPawel Laszczak 
20083d829045SPawel Laszczak 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
20093d829045SPawel Laszczak 	if (!rdrv)
20103d829045SPawel Laszczak 		return -ENOMEM;
20113d829045SPawel Laszczak 
20123d829045SPawel Laszczak 	rdrv->start	= __cdnsp_gadget_init;
20133d829045SPawel Laszczak 	rdrv->stop	= cdnsp_gadget_exit;
20143d829045SPawel Laszczak 	rdrv->suspend	= cdnsp_gadget_suspend;
20153d829045SPawel Laszczak 	rdrv->resume	= cdnsp_gadget_resume;
20163d829045SPawel Laszczak 	rdrv->state	= CDNS_ROLE_STATE_INACTIVE;
20173d829045SPawel Laszczak 	rdrv->name	= "gadget";
20183d829045SPawel Laszczak 	cdns->roles[USB_ROLE_DEVICE] = rdrv;
20193d829045SPawel Laszczak 
20203d829045SPawel Laszczak 	return 0;
20213d829045SPawel Laszczak }
2022