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