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