185d5e707SKishon Vijay Abraham I /** 285d5e707SKishon Vijay Abraham I * core.c - DesignWare USB3 DRD Controller Core file 385d5e707SKishon Vijay Abraham I * 430c31d58SKishon Vijay Abraham I * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com 585d5e707SKishon Vijay Abraham I * 685d5e707SKishon Vijay Abraham I * Authors: Felipe Balbi <balbi@ti.com>, 785d5e707SKishon Vijay Abraham I * Sebastian Andrzej Siewior <bigeasy@linutronix.de> 885d5e707SKishon Vijay Abraham I * 930c31d58SKishon Vijay Abraham I * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported 1030c31d58SKishon Vijay Abraham I * to uboot. 1185d5e707SKishon Vijay Abraham I * 1230c31d58SKishon Vijay Abraham I * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA 1385d5e707SKishon Vijay Abraham I * 1430c31d58SKishon Vijay Abraham I * SPDX-License-Identifier: GPL-2.0 1585d5e707SKishon Vijay Abraham I */ 1685d5e707SKishon Vijay Abraham I 1771744d0dSKishon Vijay Abraham I #include <common.h> 1871744d0dSKishon Vijay Abraham I #include <malloc.h> 198e1906a8SKishon Vijay Abraham I #include <dwc3-uboot.h> 2071744d0dSKishon Vijay Abraham I #include <asm/dma-mapping.h> 2185d5e707SKishon Vijay Abraham I #include <linux/ioport.h> 2285d5e707SKishon Vijay Abraham I 2385d5e707SKishon Vijay Abraham I #include <linux/usb/ch9.h> 2485d5e707SKishon Vijay Abraham I #include <linux/usb/gadget.h> 2585d5e707SKishon Vijay Abraham I 2685d5e707SKishon Vijay Abraham I #include "core.h" 2785d5e707SKishon Vijay Abraham I #include "gadget.h" 2885d5e707SKishon Vijay Abraham I #include "io.h" 2985d5e707SKishon Vijay Abraham I 3071744d0dSKishon Vijay Abraham I #include "linux-compat.h" 3185d5e707SKishon Vijay Abraham I 32793d347fSKishon Vijay Abraham I static LIST_HEAD(dwc3_list); 3385d5e707SKishon Vijay Abraham I /* -------------------------------------------------------------------------- */ 3485d5e707SKishon Vijay Abraham I 3585d5e707SKishon Vijay Abraham I void dwc3_set_mode(struct dwc3 *dwc, u32 mode) 3685d5e707SKishon Vijay Abraham I { 3785d5e707SKishon Vijay Abraham I u32 reg; 3885d5e707SKishon Vijay Abraham I 3985d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 4085d5e707SKishon Vijay Abraham I reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); 4185d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_PRTCAPDIR(mode); 4285d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 4385d5e707SKishon Vijay Abraham I } 4485d5e707SKishon Vijay Abraham I 4585d5e707SKishon Vijay Abraham I /** 4685d5e707SKishon Vijay Abraham I * dwc3_core_soft_reset - Issues core soft reset and PHY reset 4785d5e707SKishon Vijay Abraham I * @dwc: pointer to our context structure 4885d5e707SKishon Vijay Abraham I */ 4985d5e707SKishon Vijay Abraham I static int dwc3_core_soft_reset(struct dwc3 *dwc) 5085d5e707SKishon Vijay Abraham I { 5185d5e707SKishon Vijay Abraham I u32 reg; 5285d5e707SKishon Vijay Abraham I 5385d5e707SKishon Vijay Abraham I /* Before Resetting PHY, put Core in Reset */ 5485d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 5585d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_CORESOFTRESET; 5685d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 5785d5e707SKishon Vijay Abraham I 5885d5e707SKishon Vijay Abraham I /* Assert USB3 PHY reset */ 5985d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 6085d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; 6185d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 6285d5e707SKishon Vijay Abraham I 6385d5e707SKishon Vijay Abraham I /* Assert USB2 PHY reset */ 6485d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 6585d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; 6685d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 6785d5e707SKishon Vijay Abraham I 6885d5e707SKishon Vijay Abraham I mdelay(100); 6985d5e707SKishon Vijay Abraham I 7085d5e707SKishon Vijay Abraham I /* Clear USB3 PHY reset */ 7185d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 7285d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; 7385d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 7485d5e707SKishon Vijay Abraham I 7585d5e707SKishon Vijay Abraham I /* Clear USB2 PHY reset */ 7685d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 7785d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; 7885d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 7985d5e707SKishon Vijay Abraham I 8085d5e707SKishon Vijay Abraham I mdelay(100); 8185d5e707SKishon Vijay Abraham I 8285d5e707SKishon Vijay Abraham I /* After PHYs are stable we can take Core out of reset state */ 8385d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 8485d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_CORESOFTRESET; 8585d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 8685d5e707SKishon Vijay Abraham I 8785d5e707SKishon Vijay Abraham I return 0; 8885d5e707SKishon Vijay Abraham I } 8985d5e707SKishon Vijay Abraham I 9085d5e707SKishon Vijay Abraham I /** 9185d5e707SKishon Vijay Abraham I * dwc3_free_one_event_buffer - Frees one event buffer 9285d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 9385d5e707SKishon Vijay Abraham I * @evt: Pointer to event buffer to be freed 9485d5e707SKishon Vijay Abraham I */ 9585d5e707SKishon Vijay Abraham I static void dwc3_free_one_event_buffer(struct dwc3 *dwc, 9685d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt) 9785d5e707SKishon Vijay Abraham I { 9871744d0dSKishon Vijay Abraham I dma_free_coherent(evt->buf); 9985d5e707SKishon Vijay Abraham I } 10085d5e707SKishon Vijay Abraham I 10185d5e707SKishon Vijay Abraham I /** 10285d5e707SKishon Vijay Abraham I * dwc3_alloc_one_event_buffer - Allocates one event buffer structure 10385d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 10485d5e707SKishon Vijay Abraham I * @length: size of the event buffer 10585d5e707SKishon Vijay Abraham I * 10685d5e707SKishon Vijay Abraham I * Returns a pointer to the allocated event buffer structure on success 10785d5e707SKishon Vijay Abraham I * otherwise ERR_PTR(errno). 10885d5e707SKishon Vijay Abraham I */ 10985d5e707SKishon Vijay Abraham I static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, 11085d5e707SKishon Vijay Abraham I unsigned length) 11185d5e707SKishon Vijay Abraham I { 11285d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 11385d5e707SKishon Vijay Abraham I 11485d5e707SKishon Vijay Abraham I evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); 11585d5e707SKishon Vijay Abraham I if (!evt) 11685d5e707SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 11785d5e707SKishon Vijay Abraham I 11885d5e707SKishon Vijay Abraham I evt->dwc = dwc; 11985d5e707SKishon Vijay Abraham I evt->length = length; 12071744d0dSKishon Vijay Abraham I evt->buf = dma_alloc_coherent(length, 12171744d0dSKishon Vijay Abraham I (unsigned long *)&evt->dma); 12285d5e707SKishon Vijay Abraham I if (!evt->buf) 12385d5e707SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 12485d5e707SKishon Vijay Abraham I 12585d5e707SKishon Vijay Abraham I return evt; 12685d5e707SKishon Vijay Abraham I } 12785d5e707SKishon Vijay Abraham I 12885d5e707SKishon Vijay Abraham I /** 12985d5e707SKishon Vijay Abraham I * dwc3_free_event_buffers - frees all allocated event buffers 13085d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 13185d5e707SKishon Vijay Abraham I */ 13285d5e707SKishon Vijay Abraham I static void dwc3_free_event_buffers(struct dwc3 *dwc) 13385d5e707SKishon Vijay Abraham I { 13485d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 13585d5e707SKishon Vijay Abraham I int i; 13685d5e707SKishon Vijay Abraham I 13785d5e707SKishon Vijay Abraham I for (i = 0; i < dwc->num_event_buffers; i++) { 13885d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[i]; 13985d5e707SKishon Vijay Abraham I if (evt) 14085d5e707SKishon Vijay Abraham I dwc3_free_one_event_buffer(dwc, evt); 14185d5e707SKishon Vijay Abraham I } 14285d5e707SKishon Vijay Abraham I } 14385d5e707SKishon Vijay Abraham I 14485d5e707SKishon Vijay Abraham I /** 14585d5e707SKishon Vijay Abraham I * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length 14685d5e707SKishon Vijay Abraham I * @dwc: pointer to our controller context structure 14785d5e707SKishon Vijay Abraham I * @length: size of event buffer 14885d5e707SKishon Vijay Abraham I * 14985d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. In the error case, dwc 15085d5e707SKishon Vijay Abraham I * may contain some buffers allocated but not all which were requested. 15185d5e707SKishon Vijay Abraham I */ 15285d5e707SKishon Vijay Abraham I static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) 15385d5e707SKishon Vijay Abraham I { 15485d5e707SKishon Vijay Abraham I int num; 15585d5e707SKishon Vijay Abraham I int i; 15685d5e707SKishon Vijay Abraham I 15785d5e707SKishon Vijay Abraham I num = DWC3_NUM_INT(dwc->hwparams.hwparams1); 15885d5e707SKishon Vijay Abraham I dwc->num_event_buffers = num; 15985d5e707SKishon Vijay Abraham I 160*526a50f8SKishon Vijay Abraham I dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE, 161*526a50f8SKishon Vijay Abraham I sizeof(*dwc->ev_buffs) * num); 16285d5e707SKishon Vijay Abraham I if (!dwc->ev_buffs) 16385d5e707SKishon Vijay Abraham I return -ENOMEM; 16485d5e707SKishon Vijay Abraham I 16585d5e707SKishon Vijay Abraham I for (i = 0; i < num; i++) { 16685d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 16785d5e707SKishon Vijay Abraham I 16885d5e707SKishon Vijay Abraham I evt = dwc3_alloc_one_event_buffer(dwc, length); 16985d5e707SKishon Vijay Abraham I if (IS_ERR(evt)) { 17085d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "can't allocate event buffer\n"); 17185d5e707SKishon Vijay Abraham I return PTR_ERR(evt); 17285d5e707SKishon Vijay Abraham I } 17385d5e707SKishon Vijay Abraham I dwc->ev_buffs[i] = evt; 17485d5e707SKishon Vijay Abraham I } 17585d5e707SKishon Vijay Abraham I 17685d5e707SKishon Vijay Abraham I return 0; 17785d5e707SKishon Vijay Abraham I } 17885d5e707SKishon Vijay Abraham I 17985d5e707SKishon Vijay Abraham I /** 18085d5e707SKishon Vijay Abraham I * dwc3_event_buffers_setup - setup our allocated event buffers 18185d5e707SKishon Vijay Abraham I * @dwc: pointer to our controller context structure 18285d5e707SKishon Vijay Abraham I * 18385d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. 18485d5e707SKishon Vijay Abraham I */ 18585d5e707SKishon Vijay Abraham I static int dwc3_event_buffers_setup(struct dwc3 *dwc) 18685d5e707SKishon Vijay Abraham I { 18785d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 18885d5e707SKishon Vijay Abraham I int n; 18985d5e707SKishon Vijay Abraham I 19085d5e707SKishon Vijay Abraham I for (n = 0; n < dwc->num_event_buffers; n++) { 19185d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[n]; 19285d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", 19385d5e707SKishon Vijay Abraham I evt->buf, (unsigned long long) evt->dma, 19485d5e707SKishon Vijay Abraham I evt->length); 19585d5e707SKishon Vijay Abraham I 19685d5e707SKishon Vijay Abraham I evt->lpos = 0; 19785d5e707SKishon Vijay Abraham I 19885d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 19985d5e707SKishon Vijay Abraham I lower_32_bits(evt->dma)); 20085d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 20185d5e707SKishon Vijay Abraham I upper_32_bits(evt->dma)); 20285d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 20385d5e707SKishon Vijay Abraham I DWC3_GEVNTSIZ_SIZE(evt->length)); 20485d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); 20585d5e707SKishon Vijay Abraham I } 20685d5e707SKishon Vijay Abraham I 20785d5e707SKishon Vijay Abraham I return 0; 20885d5e707SKishon Vijay Abraham I } 20985d5e707SKishon Vijay Abraham I 21085d5e707SKishon Vijay Abraham I static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) 21185d5e707SKishon Vijay Abraham I { 21285d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 21385d5e707SKishon Vijay Abraham I int n; 21485d5e707SKishon Vijay Abraham I 21585d5e707SKishon Vijay Abraham I for (n = 0; n < dwc->num_event_buffers; n++) { 21685d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[n]; 21785d5e707SKishon Vijay Abraham I 21885d5e707SKishon Vijay Abraham I evt->lpos = 0; 21985d5e707SKishon Vijay Abraham I 22085d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); 22185d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); 22285d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK 22385d5e707SKishon Vijay Abraham I | DWC3_GEVNTSIZ_SIZE(0)); 22485d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); 22585d5e707SKishon Vijay Abraham I } 22685d5e707SKishon Vijay Abraham I } 22785d5e707SKishon Vijay Abraham I 22885d5e707SKishon Vijay Abraham I static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) 22985d5e707SKishon Vijay Abraham I { 23085d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 23185d5e707SKishon Vijay Abraham I return 0; 23285d5e707SKishon Vijay Abraham I 23385d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 23485d5e707SKishon Vijay Abraham I return 0; 23585d5e707SKishon Vijay Abraham I 23685d5e707SKishon Vijay Abraham I dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, 23785d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); 23885d5e707SKishon Vijay Abraham I if (!dwc->scratchbuf) 23985d5e707SKishon Vijay Abraham I return -ENOMEM; 24085d5e707SKishon Vijay Abraham I 24185d5e707SKishon Vijay Abraham I return 0; 24285d5e707SKishon Vijay Abraham I } 24385d5e707SKishon Vijay Abraham I 24485d5e707SKishon Vijay Abraham I static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) 24585d5e707SKishon Vijay Abraham I { 24685d5e707SKishon Vijay Abraham I dma_addr_t scratch_addr; 24785d5e707SKishon Vijay Abraham I u32 param; 24885d5e707SKishon Vijay Abraham I int ret; 24985d5e707SKishon Vijay Abraham I 25085d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 25185d5e707SKishon Vijay Abraham I return 0; 25285d5e707SKishon Vijay Abraham I 25385d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 25485d5e707SKishon Vijay Abraham I return 0; 25585d5e707SKishon Vijay Abraham I 25671744d0dSKishon Vijay Abraham I scratch_addr = dma_map_single(dwc->scratchbuf, 25785d5e707SKishon Vijay Abraham I dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE, 25885d5e707SKishon Vijay Abraham I DMA_BIDIRECTIONAL); 25985d5e707SKishon Vijay Abraham I if (dma_mapping_error(dwc->dev, scratch_addr)) { 26085d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to map scratch buffer\n"); 26185d5e707SKishon Vijay Abraham I ret = -EFAULT; 26285d5e707SKishon Vijay Abraham I goto err0; 26385d5e707SKishon Vijay Abraham I } 26485d5e707SKishon Vijay Abraham I 26585d5e707SKishon Vijay Abraham I dwc->scratch_addr = scratch_addr; 26685d5e707SKishon Vijay Abraham I 26785d5e707SKishon Vijay Abraham I param = lower_32_bits(scratch_addr); 26885d5e707SKishon Vijay Abraham I 26985d5e707SKishon Vijay Abraham I ret = dwc3_send_gadget_generic_command(dwc, 27085d5e707SKishon Vijay Abraham I DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param); 27185d5e707SKishon Vijay Abraham I if (ret < 0) 27285d5e707SKishon Vijay Abraham I goto err1; 27385d5e707SKishon Vijay Abraham I 27485d5e707SKishon Vijay Abraham I param = upper_32_bits(scratch_addr); 27585d5e707SKishon Vijay Abraham I 27685d5e707SKishon Vijay Abraham I ret = dwc3_send_gadget_generic_command(dwc, 27785d5e707SKishon Vijay Abraham I DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param); 27885d5e707SKishon Vijay Abraham I if (ret < 0) 27985d5e707SKishon Vijay Abraham I goto err1; 28085d5e707SKishon Vijay Abraham I 28185d5e707SKishon Vijay Abraham I return 0; 28285d5e707SKishon Vijay Abraham I 28385d5e707SKishon Vijay Abraham I err1: 28471744d0dSKishon Vijay Abraham I dma_unmap_single((void *)dwc->scratch_addr, dwc->nr_scratch * 28585d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); 28685d5e707SKishon Vijay Abraham I 28785d5e707SKishon Vijay Abraham I err0: 28885d5e707SKishon Vijay Abraham I return ret; 28985d5e707SKishon Vijay Abraham I } 29085d5e707SKishon Vijay Abraham I 29185d5e707SKishon Vijay Abraham I static void dwc3_free_scratch_buffers(struct dwc3 *dwc) 29285d5e707SKishon Vijay Abraham I { 29385d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 29485d5e707SKishon Vijay Abraham I return; 29585d5e707SKishon Vijay Abraham I 29685d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 29785d5e707SKishon Vijay Abraham I return; 29885d5e707SKishon Vijay Abraham I 29971744d0dSKishon Vijay Abraham I dma_unmap_single((void *)dwc->scratch_addr, dwc->nr_scratch * 30085d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); 30185d5e707SKishon Vijay Abraham I kfree(dwc->scratchbuf); 30285d5e707SKishon Vijay Abraham I } 30385d5e707SKishon Vijay Abraham I 30485d5e707SKishon Vijay Abraham I static void dwc3_core_num_eps(struct dwc3 *dwc) 30585d5e707SKishon Vijay Abraham I { 30685d5e707SKishon Vijay Abraham I struct dwc3_hwparams *parms = &dwc->hwparams; 30785d5e707SKishon Vijay Abraham I 30885d5e707SKishon Vijay Abraham I dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); 30985d5e707SKishon Vijay Abraham I dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; 31085d5e707SKishon Vijay Abraham I 31185d5e707SKishon Vijay Abraham I dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n", 31285d5e707SKishon Vijay Abraham I dwc->num_in_eps, dwc->num_out_eps); 31385d5e707SKishon Vijay Abraham I } 31485d5e707SKishon Vijay Abraham I 31585d5e707SKishon Vijay Abraham I static void dwc3_cache_hwparams(struct dwc3 *dwc) 31685d5e707SKishon Vijay Abraham I { 31785d5e707SKishon Vijay Abraham I struct dwc3_hwparams *parms = &dwc->hwparams; 31885d5e707SKishon Vijay Abraham I 31985d5e707SKishon Vijay Abraham I parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); 32085d5e707SKishon Vijay Abraham I parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); 32185d5e707SKishon Vijay Abraham I parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); 32285d5e707SKishon Vijay Abraham I parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); 32385d5e707SKishon Vijay Abraham I parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); 32485d5e707SKishon Vijay Abraham I parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); 32585d5e707SKishon Vijay Abraham I parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); 32685d5e707SKishon Vijay Abraham I parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); 32785d5e707SKishon Vijay Abraham I parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); 32885d5e707SKishon Vijay Abraham I } 32985d5e707SKishon Vijay Abraham I 33085d5e707SKishon Vijay Abraham I /** 33185d5e707SKishon Vijay Abraham I * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core 33285d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 33385d5e707SKishon Vijay Abraham I */ 33485d5e707SKishon Vijay Abraham I static void dwc3_phy_setup(struct dwc3 *dwc) 33585d5e707SKishon Vijay Abraham I { 33685d5e707SKishon Vijay Abraham I u32 reg; 33785d5e707SKishon Vijay Abraham I 33885d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 33985d5e707SKishon Vijay Abraham I 34085d5e707SKishon Vijay Abraham I /* 34185d5e707SKishon Vijay Abraham I * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY 34285d5e707SKishon Vijay Abraham I * to '0' during coreConsultant configuration. So default value 34385d5e707SKishon Vijay Abraham I * will be '0' when the core is reset. Application needs to set it 34485d5e707SKishon Vijay Abraham I * to '1' after the core initialization is completed. 34585d5e707SKishon Vijay Abraham I */ 34685d5e707SKishon Vijay Abraham I if (dwc->revision > DWC3_REVISION_194A) 34785d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_SUSPHY; 34885d5e707SKishon Vijay Abraham I 34985d5e707SKishon Vijay Abraham I if (dwc->u2ss_inp3_quirk) 35085d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK; 35185d5e707SKishon Vijay Abraham I 35285d5e707SKishon Vijay Abraham I if (dwc->req_p1p2p3_quirk) 35385d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_REQP1P2P3; 35485d5e707SKishon Vijay Abraham I 35585d5e707SKishon Vijay Abraham I if (dwc->del_p1p2p3_quirk) 35685d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN; 35785d5e707SKishon Vijay Abraham I 35885d5e707SKishon Vijay Abraham I if (dwc->del_phy_power_chg_quirk) 35985d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE; 36085d5e707SKishon Vijay Abraham I 36185d5e707SKishon Vijay Abraham I if (dwc->lfps_filter_quirk) 36285d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_LFPSFILT; 36385d5e707SKishon Vijay Abraham I 36485d5e707SKishon Vijay Abraham I if (dwc->rx_detect_poll_quirk) 36585d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL; 36685d5e707SKishon Vijay Abraham I 36785d5e707SKishon Vijay Abraham I if (dwc->tx_de_emphasis_quirk) 36885d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis); 36985d5e707SKishon Vijay Abraham I 37085d5e707SKishon Vijay Abraham I if (dwc->dis_u3_susphy_quirk) 37185d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; 37285d5e707SKishon Vijay Abraham I 37385d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 37485d5e707SKishon Vijay Abraham I 37585d5e707SKishon Vijay Abraham I mdelay(100); 37685d5e707SKishon Vijay Abraham I 37785d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 37885d5e707SKishon Vijay Abraham I 37985d5e707SKishon Vijay Abraham I /* 38085d5e707SKishon Vijay Abraham I * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to 38185d5e707SKishon Vijay Abraham I * '0' during coreConsultant configuration. So default value will 38285d5e707SKishon Vijay Abraham I * be '0' when the core is reset. Application needs to set it to 38385d5e707SKishon Vijay Abraham I * '1' after the core initialization is completed. 38485d5e707SKishon Vijay Abraham I */ 38585d5e707SKishon Vijay Abraham I if (dwc->revision > DWC3_REVISION_194A) 38685d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB2PHYCFG_SUSPHY; 38785d5e707SKishon Vijay Abraham I 38885d5e707SKishon Vijay Abraham I if (dwc->dis_u2_susphy_quirk) 38985d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 39085d5e707SKishon Vijay Abraham I 39185d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 39285d5e707SKishon Vijay Abraham I 39385d5e707SKishon Vijay Abraham I mdelay(100); 39485d5e707SKishon Vijay Abraham I } 39585d5e707SKishon Vijay Abraham I 39685d5e707SKishon Vijay Abraham I /** 39785d5e707SKishon Vijay Abraham I * dwc3_core_init - Low-level initialization of DWC3 Core 39885d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 39985d5e707SKishon Vijay Abraham I * 40085d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. 40185d5e707SKishon Vijay Abraham I */ 40285d5e707SKishon Vijay Abraham I static int dwc3_core_init(struct dwc3 *dwc) 40385d5e707SKishon Vijay Abraham I { 40485d5e707SKishon Vijay Abraham I unsigned long timeout; 40585d5e707SKishon Vijay Abraham I u32 hwparams4 = dwc->hwparams.hwparams4; 40685d5e707SKishon Vijay Abraham I u32 reg; 40785d5e707SKishon Vijay Abraham I int ret; 40885d5e707SKishon Vijay Abraham I 40985d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); 41085d5e707SKishon Vijay Abraham I /* This should read as U3 followed by revision number */ 41185d5e707SKishon Vijay Abraham I if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { 41285d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); 41385d5e707SKishon Vijay Abraham I ret = -ENODEV; 41485d5e707SKishon Vijay Abraham I goto err0; 41585d5e707SKishon Vijay Abraham I } 41685d5e707SKishon Vijay Abraham I dwc->revision = reg; 41785d5e707SKishon Vijay Abraham I 41885d5e707SKishon Vijay Abraham I /* Handle USB2.0-only core configuration */ 41985d5e707SKishon Vijay Abraham I if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) == 42085d5e707SKishon Vijay Abraham I DWC3_GHWPARAMS3_SSPHY_IFC_DIS) { 42185d5e707SKishon Vijay Abraham I if (dwc->maximum_speed == USB_SPEED_SUPER) 42285d5e707SKishon Vijay Abraham I dwc->maximum_speed = USB_SPEED_HIGH; 42385d5e707SKishon Vijay Abraham I } 42485d5e707SKishon Vijay Abraham I 42585d5e707SKishon Vijay Abraham I /* issue device SoftReset too */ 42671744d0dSKishon Vijay Abraham I timeout = 5000; 42785d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); 42871744d0dSKishon Vijay Abraham I while (timeout--) { 42985d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_DCTL); 43085d5e707SKishon Vijay Abraham I if (!(reg & DWC3_DCTL_CSFTRST)) 43185d5e707SKishon Vijay Abraham I break; 43271744d0dSKishon Vijay Abraham I }; 43385d5e707SKishon Vijay Abraham I 43471744d0dSKishon Vijay Abraham I if (!timeout) { 43585d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "Reset Timed Out\n"); 43685d5e707SKishon Vijay Abraham I ret = -ETIMEDOUT; 43785d5e707SKishon Vijay Abraham I goto err0; 43885d5e707SKishon Vijay Abraham I } 43985d5e707SKishon Vijay Abraham I 44085d5e707SKishon Vijay Abraham I ret = dwc3_core_soft_reset(dwc); 44185d5e707SKishon Vijay Abraham I if (ret) 44285d5e707SKishon Vijay Abraham I goto err0; 44385d5e707SKishon Vijay Abraham I 44485d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 44585d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_SCALEDOWN_MASK; 44685d5e707SKishon Vijay Abraham I 44785d5e707SKishon Vijay Abraham I switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { 44885d5e707SKishon Vijay Abraham I case DWC3_GHWPARAMS1_EN_PWROPT_CLK: 44985d5e707SKishon Vijay Abraham I /** 45085d5e707SKishon Vijay Abraham I * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an 45185d5e707SKishon Vijay Abraham I * issue which would cause xHCI compliance tests to fail. 45285d5e707SKishon Vijay Abraham I * 45385d5e707SKishon Vijay Abraham I * Because of that we cannot enable clock gating on such 45485d5e707SKishon Vijay Abraham I * configurations. 45585d5e707SKishon Vijay Abraham I * 45685d5e707SKishon Vijay Abraham I * Refers to: 45785d5e707SKishon Vijay Abraham I * 45885d5e707SKishon Vijay Abraham I * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based 45985d5e707SKishon Vijay Abraham I * SOF/ITP Mode Used 46085d5e707SKishon Vijay Abraham I */ 46185d5e707SKishon Vijay Abraham I if ((dwc->dr_mode == USB_DR_MODE_HOST || 46285d5e707SKishon Vijay Abraham I dwc->dr_mode == USB_DR_MODE_OTG) && 46385d5e707SKishon Vijay Abraham I (dwc->revision >= DWC3_REVISION_210A && 46485d5e707SKishon Vijay Abraham I dwc->revision <= DWC3_REVISION_250A)) 46585d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC; 46685d5e707SKishon Vijay Abraham I else 46785d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_DSBLCLKGTNG; 46885d5e707SKishon Vijay Abraham I break; 46985d5e707SKishon Vijay Abraham I case DWC3_GHWPARAMS1_EN_PWROPT_HIB: 47085d5e707SKishon Vijay Abraham I /* enable hibernation here */ 47185d5e707SKishon Vijay Abraham I dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4); 47285d5e707SKishon Vijay Abraham I 47385d5e707SKishon Vijay Abraham I /* 47485d5e707SKishon Vijay Abraham I * REVISIT Enabling this bit so that host-mode hibernation 47585d5e707SKishon Vijay Abraham I * will work. Device-mode hibernation is not yet implemented. 47685d5e707SKishon Vijay Abraham I */ 47785d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_GBLHIBERNATIONEN; 47885d5e707SKishon Vijay Abraham I break; 47985d5e707SKishon Vijay Abraham I default: 48085d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "No power optimization available\n"); 48185d5e707SKishon Vijay Abraham I } 48285d5e707SKishon Vijay Abraham I 48385d5e707SKishon Vijay Abraham I /* check if current dwc3 is on simulation board */ 48485d5e707SKishon Vijay Abraham I if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) { 48585d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "it is on FPGA board\n"); 48685d5e707SKishon Vijay Abraham I dwc->is_fpga = true; 48785d5e707SKishon Vijay Abraham I } 48885d5e707SKishon Vijay Abraham I 48971744d0dSKishon Vijay Abraham I if(dwc->disable_scramble_quirk && !dwc->is_fpga) 49071744d0dSKishon Vijay Abraham I WARN(true, 49185d5e707SKishon Vijay Abraham I "disable_scramble cannot be used on non-FPGA builds\n"); 49285d5e707SKishon Vijay Abraham I 49385d5e707SKishon Vijay Abraham I if (dwc->disable_scramble_quirk && dwc->is_fpga) 49485d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_DISSCRAMBLE; 49585d5e707SKishon Vijay Abraham I else 49685d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_DISSCRAMBLE; 49785d5e707SKishon Vijay Abraham I 49885d5e707SKishon Vijay Abraham I if (dwc->u2exit_lfps_quirk) 49985d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_U2EXIT_LFPS; 50085d5e707SKishon Vijay Abraham I 50185d5e707SKishon Vijay Abraham I /* 50285d5e707SKishon Vijay Abraham I * WORKAROUND: DWC3 revisions <1.90a have a bug 50385d5e707SKishon Vijay Abraham I * where the device can fail to connect at SuperSpeed 50485d5e707SKishon Vijay Abraham I * and falls back to high-speed mode which causes 50585d5e707SKishon Vijay Abraham I * the device to enter a Connect/Disconnect loop 50685d5e707SKishon Vijay Abraham I */ 50785d5e707SKishon Vijay Abraham I if (dwc->revision < DWC3_REVISION_190A) 50885d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_U2RSTECN; 50985d5e707SKishon Vijay Abraham I 51085d5e707SKishon Vijay Abraham I dwc3_core_num_eps(dwc); 51185d5e707SKishon Vijay Abraham I 51285d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 51385d5e707SKishon Vijay Abraham I 51485d5e707SKishon Vijay Abraham I dwc3_phy_setup(dwc); 51585d5e707SKishon Vijay Abraham I 51685d5e707SKishon Vijay Abraham I ret = dwc3_alloc_scratch_buffers(dwc); 51785d5e707SKishon Vijay Abraham I if (ret) 51871744d0dSKishon Vijay Abraham I goto err0; 51985d5e707SKishon Vijay Abraham I 52085d5e707SKishon Vijay Abraham I ret = dwc3_setup_scratch_buffers(dwc); 52185d5e707SKishon Vijay Abraham I if (ret) 52271744d0dSKishon Vijay Abraham I goto err1; 52385d5e707SKishon Vijay Abraham I 52485d5e707SKishon Vijay Abraham I return 0; 52585d5e707SKishon Vijay Abraham I 52685d5e707SKishon Vijay Abraham I err1: 52771744d0dSKishon Vijay Abraham I dwc3_free_scratch_buffers(dwc); 52885d5e707SKishon Vijay Abraham I 52985d5e707SKishon Vijay Abraham I err0: 53085d5e707SKishon Vijay Abraham I return ret; 53185d5e707SKishon Vijay Abraham I } 53285d5e707SKishon Vijay Abraham I 53385d5e707SKishon Vijay Abraham I static void dwc3_core_exit(struct dwc3 *dwc) 53485d5e707SKishon Vijay Abraham I { 53585d5e707SKishon Vijay Abraham I dwc3_free_scratch_buffers(dwc); 53685d5e707SKishon Vijay Abraham I } 53785d5e707SKishon Vijay Abraham I 53885d5e707SKishon Vijay Abraham I static int dwc3_core_init_mode(struct dwc3 *dwc) 53985d5e707SKishon Vijay Abraham I { 54085d5e707SKishon Vijay Abraham I int ret; 54185d5e707SKishon Vijay Abraham I 54285d5e707SKishon Vijay Abraham I switch (dwc->dr_mode) { 54385d5e707SKishon Vijay Abraham I case USB_DR_MODE_PERIPHERAL: 54485d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); 54585d5e707SKishon Vijay Abraham I ret = dwc3_gadget_init(dwc); 54685d5e707SKishon Vijay Abraham I if (ret) { 54785d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize gadget\n"); 54885d5e707SKishon Vijay Abraham I return ret; 54985d5e707SKishon Vijay Abraham I } 55085d5e707SKishon Vijay Abraham I break; 55185d5e707SKishon Vijay Abraham I case USB_DR_MODE_HOST: 55285d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); 55385d5e707SKishon Vijay Abraham I ret = dwc3_host_init(dwc); 55485d5e707SKishon Vijay Abraham I if (ret) { 55585d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize host\n"); 55685d5e707SKishon Vijay Abraham I return ret; 55785d5e707SKishon Vijay Abraham I } 55885d5e707SKishon Vijay Abraham I break; 55985d5e707SKishon Vijay Abraham I case USB_DR_MODE_OTG: 56085d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); 56185d5e707SKishon Vijay Abraham I ret = dwc3_host_init(dwc); 56285d5e707SKishon Vijay Abraham I if (ret) { 56385d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize host\n"); 56485d5e707SKishon Vijay Abraham I return ret; 56585d5e707SKishon Vijay Abraham I } 56685d5e707SKishon Vijay Abraham I 56785d5e707SKishon Vijay Abraham I ret = dwc3_gadget_init(dwc); 56885d5e707SKishon Vijay Abraham I if (ret) { 56985d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize gadget\n"); 57085d5e707SKishon Vijay Abraham I return ret; 57185d5e707SKishon Vijay Abraham I } 57285d5e707SKishon Vijay Abraham I break; 57385d5e707SKishon Vijay Abraham I default: 57485d5e707SKishon Vijay Abraham I dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); 57585d5e707SKishon Vijay Abraham I return -EINVAL; 57685d5e707SKishon Vijay Abraham I } 57785d5e707SKishon Vijay Abraham I 57885d5e707SKishon Vijay Abraham I return 0; 57985d5e707SKishon Vijay Abraham I } 58085d5e707SKishon Vijay Abraham I 58185d5e707SKishon Vijay Abraham I static void dwc3_core_exit_mode(struct dwc3 *dwc) 58285d5e707SKishon Vijay Abraham I { 58385d5e707SKishon Vijay Abraham I switch (dwc->dr_mode) { 58485d5e707SKishon Vijay Abraham I case USB_DR_MODE_PERIPHERAL: 58585d5e707SKishon Vijay Abraham I dwc3_gadget_exit(dwc); 58685d5e707SKishon Vijay Abraham I break; 58785d5e707SKishon Vijay Abraham I case USB_DR_MODE_HOST: 58885d5e707SKishon Vijay Abraham I dwc3_host_exit(dwc); 58985d5e707SKishon Vijay Abraham I break; 59085d5e707SKishon Vijay Abraham I case USB_DR_MODE_OTG: 59185d5e707SKishon Vijay Abraham I dwc3_host_exit(dwc); 59285d5e707SKishon Vijay Abraham I dwc3_gadget_exit(dwc); 59385d5e707SKishon Vijay Abraham I break; 59485d5e707SKishon Vijay Abraham I default: 59585d5e707SKishon Vijay Abraham I /* do nothing */ 59685d5e707SKishon Vijay Abraham I break; 59785d5e707SKishon Vijay Abraham I } 59885d5e707SKishon Vijay Abraham I } 59985d5e707SKishon Vijay Abraham I 60085d5e707SKishon Vijay Abraham I #define DWC3_ALIGN_MASK (16 - 1) 60185d5e707SKishon Vijay Abraham I 6028e1906a8SKishon Vijay Abraham I /** 6038e1906a8SKishon Vijay Abraham I * dwc3_uboot_init - dwc3 core uboot initialization code 6048e1906a8SKishon Vijay Abraham I * @dwc3_dev: struct dwc3_device containing initialization data 6058e1906a8SKishon Vijay Abraham I * 6068e1906a8SKishon Vijay Abraham I * Entry point for dwc3 driver (equivalent to dwc3_probe in linux 6078e1906a8SKishon Vijay Abraham I * kernel driver). Pointer to dwc3_device should be passed containing 6088e1906a8SKishon Vijay Abraham I * base address and other initialization data. Returns '0' on success and 6098e1906a8SKishon Vijay Abraham I * a negative value on failure. 6108e1906a8SKishon Vijay Abraham I * 6118e1906a8SKishon Vijay Abraham I * Generally called from board_usb_init() implemented in board file. 6128e1906a8SKishon Vijay Abraham I */ 6138e1906a8SKishon Vijay Abraham I int dwc3_uboot_init(struct dwc3_device *dwc3_dev) 61485d5e707SKishon Vijay Abraham I { 615793d347fSKishon Vijay Abraham I struct dwc3 *dwc; 6168e1906a8SKishon Vijay Abraham I struct device *dev; 61785d5e707SKishon Vijay Abraham I u8 lpm_nyet_threshold; 61885d5e707SKishon Vijay Abraham I u8 tx_de_emphasis; 61985d5e707SKishon Vijay Abraham I u8 hird_threshold; 62085d5e707SKishon Vijay Abraham I 62185d5e707SKishon Vijay Abraham I int ret; 62285d5e707SKishon Vijay Abraham I 62385d5e707SKishon Vijay Abraham I void *mem; 62485d5e707SKishon Vijay Abraham I 62585d5e707SKishon Vijay Abraham I mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); 62685d5e707SKishon Vijay Abraham I if (!mem) 62785d5e707SKishon Vijay Abraham I return -ENOMEM; 62885d5e707SKishon Vijay Abraham I 62985d5e707SKishon Vijay Abraham I dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); 63085d5e707SKishon Vijay Abraham I dwc->mem = mem; 63185d5e707SKishon Vijay Abraham I 6328e1906a8SKishon Vijay Abraham I dwc->regs = (int *)(dwc3_dev->base + DWC3_GLOBALS_REGS_START); 63385d5e707SKishon Vijay Abraham I 63485d5e707SKishon Vijay Abraham I /* default to highest possible threshold */ 63585d5e707SKishon Vijay Abraham I lpm_nyet_threshold = 0xff; 63685d5e707SKishon Vijay Abraham I 63785d5e707SKishon Vijay Abraham I /* default to -3.5dB de-emphasis */ 63885d5e707SKishon Vijay Abraham I tx_de_emphasis = 1; 63985d5e707SKishon Vijay Abraham I 64085d5e707SKishon Vijay Abraham I /* 64185d5e707SKishon Vijay Abraham I * default to assert utmi_sleep_n and use maximum allowed HIRD 64285d5e707SKishon Vijay Abraham I * threshold value of 0b1100 64385d5e707SKishon Vijay Abraham I */ 64485d5e707SKishon Vijay Abraham I hird_threshold = 12; 64585d5e707SKishon Vijay Abraham I 6468e1906a8SKishon Vijay Abraham I dwc->maximum_speed = dwc3_dev->maximum_speed; 6478e1906a8SKishon Vijay Abraham I dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum; 6488e1906a8SKishon Vijay Abraham I if (dwc3_dev->lpm_nyet_threshold) 6498e1906a8SKishon Vijay Abraham I lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold; 6508e1906a8SKishon Vijay Abraham I dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend; 6518e1906a8SKishon Vijay Abraham I if (dwc3_dev->hird_threshold) 6528e1906a8SKishon Vijay Abraham I hird_threshold = dwc3_dev->hird_threshold; 65385d5e707SKishon Vijay Abraham I 6548e1906a8SKishon Vijay Abraham I dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize; 6558e1906a8SKishon Vijay Abraham I dwc->dr_mode = dwc3_dev->dr_mode; 65685d5e707SKishon Vijay Abraham I 6578e1906a8SKishon Vijay Abraham I dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk; 6588e1906a8SKishon Vijay Abraham I dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk; 6598e1906a8SKishon Vijay Abraham I dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk; 6608e1906a8SKishon Vijay Abraham I dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk; 6618e1906a8SKishon Vijay Abraham I dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk; 6628e1906a8SKishon Vijay Abraham I dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk; 6638e1906a8SKishon Vijay Abraham I dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk; 6648e1906a8SKishon Vijay Abraham I dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk; 6658e1906a8SKishon Vijay Abraham I dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk; 6668e1906a8SKishon Vijay Abraham I dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk; 66785d5e707SKishon Vijay Abraham I 6688e1906a8SKishon Vijay Abraham I dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk; 6698e1906a8SKishon Vijay Abraham I if (dwc3_dev->tx_de_emphasis) 6708e1906a8SKishon Vijay Abraham I tx_de_emphasis = dwc3_dev->tx_de_emphasis; 67185d5e707SKishon Vijay Abraham I 67285d5e707SKishon Vijay Abraham I /* default to superspeed if no maximum_speed passed */ 67385d5e707SKishon Vijay Abraham I if (dwc->maximum_speed == USB_SPEED_UNKNOWN) 67485d5e707SKishon Vijay Abraham I dwc->maximum_speed = USB_SPEED_SUPER; 67585d5e707SKishon Vijay Abraham I 67685d5e707SKishon Vijay Abraham I dwc->lpm_nyet_threshold = lpm_nyet_threshold; 67785d5e707SKishon Vijay Abraham I dwc->tx_de_emphasis = tx_de_emphasis; 67885d5e707SKishon Vijay Abraham I 67985d5e707SKishon Vijay Abraham I dwc->hird_threshold = hird_threshold 68085d5e707SKishon Vijay Abraham I | (dwc->is_utmi_l1_suspend << 4); 68185d5e707SKishon Vijay Abraham I 682793d347fSKishon Vijay Abraham I dwc->index = dwc3_dev->index; 683793d347fSKishon Vijay Abraham I 68485d5e707SKishon Vijay Abraham I dwc3_cache_hwparams(dwc); 68585d5e707SKishon Vijay Abraham I 68685d5e707SKishon Vijay Abraham I ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); 68785d5e707SKishon Vijay Abraham I if (ret) { 68885d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to allocate event buffers\n"); 68971744d0dSKishon Vijay Abraham I return -ENOMEM; 69085d5e707SKishon Vijay Abraham I } 69185d5e707SKishon Vijay Abraham I 69285d5e707SKishon Vijay Abraham I if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) 69385d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_HOST; 69485d5e707SKishon Vijay Abraham I else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) 69585d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_PERIPHERAL; 69685d5e707SKishon Vijay Abraham I 69785d5e707SKishon Vijay Abraham I if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) 69885d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_OTG; 69985d5e707SKishon Vijay Abraham I 70085d5e707SKishon Vijay Abraham I ret = dwc3_core_init(dwc); 70185d5e707SKishon Vijay Abraham I if (ret) { 70285d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize core\n"); 70385d5e707SKishon Vijay Abraham I goto err0; 70485d5e707SKishon Vijay Abraham I } 70585d5e707SKishon Vijay Abraham I 70685d5e707SKishon Vijay Abraham I ret = dwc3_event_buffers_setup(dwc); 70785d5e707SKishon Vijay Abraham I if (ret) { 70885d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to setup event buffers\n"); 70971744d0dSKishon Vijay Abraham I goto err1; 71085d5e707SKishon Vijay Abraham I } 71185d5e707SKishon Vijay Abraham I 71285d5e707SKishon Vijay Abraham I ret = dwc3_core_init_mode(dwc); 71385d5e707SKishon Vijay Abraham I if (ret) 71485d5e707SKishon Vijay Abraham I goto err2; 71585d5e707SKishon Vijay Abraham I 716793d347fSKishon Vijay Abraham I list_add_tail(&dwc->list, &dwc3_list); 717793d347fSKishon Vijay Abraham I 71885d5e707SKishon Vijay Abraham I return 0; 71985d5e707SKishon Vijay Abraham I 72085d5e707SKishon Vijay Abraham I err2: 72185d5e707SKishon Vijay Abraham I dwc3_event_buffers_cleanup(dwc); 72285d5e707SKishon Vijay Abraham I 72385d5e707SKishon Vijay Abraham I err1: 72485d5e707SKishon Vijay Abraham I dwc3_core_exit(dwc); 72585d5e707SKishon Vijay Abraham I 72685d5e707SKishon Vijay Abraham I err0: 72785d5e707SKishon Vijay Abraham I dwc3_free_event_buffers(dwc); 72885d5e707SKishon Vijay Abraham I 72985d5e707SKishon Vijay Abraham I return ret; 73085d5e707SKishon Vijay Abraham I } 73185d5e707SKishon Vijay Abraham I 7328e1906a8SKishon Vijay Abraham I /** 7338e1906a8SKishon Vijay Abraham I * dwc3_uboot_exit - dwc3 core uboot cleanup code 7348e1906a8SKishon Vijay Abraham I * @index: index of this controller 7358e1906a8SKishon Vijay Abraham I * 7368e1906a8SKishon Vijay Abraham I * Performs cleanup of memory allocated in dwc3_uboot_init and other misc 737793d347fSKishon Vijay Abraham I * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller 738793d347fSKishon Vijay Abraham I * should be passed and should match with the index passed in 739793d347fSKishon Vijay Abraham I * dwc3_device during init. 7408e1906a8SKishon Vijay Abraham I * 7418e1906a8SKishon Vijay Abraham I * Generally called from board file. 7428e1906a8SKishon Vijay Abraham I */ 743793d347fSKishon Vijay Abraham I void dwc3_uboot_exit(int index) 74485d5e707SKishon Vijay Abraham I { 745793d347fSKishon Vijay Abraham I struct dwc3 *dwc; 746793d347fSKishon Vijay Abraham I 747793d347fSKishon Vijay Abraham I list_for_each_entry(dwc, &dwc3_list, list) { 748793d347fSKishon Vijay Abraham I if (dwc->index != index) 749793d347fSKishon Vijay Abraham I continue; 750793d347fSKishon Vijay Abraham I 75185d5e707SKishon Vijay Abraham I dwc3_core_exit_mode(dwc); 75285d5e707SKishon Vijay Abraham I dwc3_event_buffers_cleanup(dwc); 75385d5e707SKishon Vijay Abraham I dwc3_free_event_buffers(dwc); 75485d5e707SKishon Vijay Abraham I dwc3_core_exit(dwc); 755793d347fSKishon Vijay Abraham I list_del(&dwc->list); 7568e1906a8SKishon Vijay Abraham I kfree(dwc->mem); 757793d347fSKishon Vijay Abraham I break; 758793d347fSKishon Vijay Abraham I } 75985d5e707SKishon Vijay Abraham I } 76085d5e707SKishon Vijay Abraham I 76127d3b89dSKishon Vijay Abraham I /** 76227d3b89dSKishon Vijay Abraham I * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt 76327d3b89dSKishon Vijay Abraham I * @index: index of this controller 76427d3b89dSKishon Vijay Abraham I * 76527d3b89dSKishon Vijay Abraham I * Invokes dwc3 gadget interrupts. 76627d3b89dSKishon Vijay Abraham I * 76727d3b89dSKishon Vijay Abraham I * Generally called from board file. 76827d3b89dSKishon Vijay Abraham I */ 76927d3b89dSKishon Vijay Abraham I void dwc3_uboot_handle_interrupt(int index) 77027d3b89dSKishon Vijay Abraham I { 77127d3b89dSKishon Vijay Abraham I struct dwc3 *dwc = NULL; 772*526a50f8SKishon Vijay Abraham I int i; 773*526a50f8SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 77427d3b89dSKishon Vijay Abraham I 77527d3b89dSKishon Vijay Abraham I list_for_each_entry(dwc, &dwc3_list, list) { 77627d3b89dSKishon Vijay Abraham I if (dwc->index != index) 77727d3b89dSKishon Vijay Abraham I continue; 77827d3b89dSKishon Vijay Abraham I 779*526a50f8SKishon Vijay Abraham I for (i = 0; i < dwc->num_event_buffers; i++) { 780*526a50f8SKishon Vijay Abraham I evt = dwc->ev_buffs[i]; 781*526a50f8SKishon Vijay Abraham I dwc3_flush_cache((int)evt->buf, evt->length); 782*526a50f8SKishon Vijay Abraham I } 783*526a50f8SKishon Vijay Abraham I 78427d3b89dSKishon Vijay Abraham I dwc3_gadget_uboot_handle_interrupt(dwc); 78527d3b89dSKishon Vijay Abraham I break; 78627d3b89dSKishon Vijay Abraham I } 78727d3b89dSKishon Vijay Abraham I } 78827d3b89dSKishon Vijay Abraham I 78985d5e707SKishon Vijay Abraham I MODULE_ALIAS("platform:dwc3"); 79085d5e707SKishon Vijay Abraham I MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 79185d5e707SKishon Vijay Abraham I MODULE_LICENSE("GPL v2"); 79285d5e707SKishon Vijay Abraham I MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); 793