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> 1941933c04SKever Yang #include <fdtdec.h> 208e1906a8SKishon Vijay Abraham I #include <dwc3-uboot.h> 2171744d0dSKishon Vijay Abraham I #include <asm/dma-mapping.h> 2285d5e707SKishon Vijay Abraham I #include <linux/ioport.h> 23b6985a21SMugunthan V N #include <dm.h> 2485d5e707SKishon Vijay Abraham I 2585d5e707SKishon Vijay Abraham I #include <linux/usb/ch9.h> 2685d5e707SKishon Vijay Abraham I #include <linux/usb/gadget.h> 2785d5e707SKishon Vijay Abraham I 2885d5e707SKishon Vijay Abraham I #include "core.h" 2985d5e707SKishon Vijay Abraham I #include "gadget.h" 3085d5e707SKishon Vijay Abraham I #include "io.h" 3185d5e707SKishon Vijay Abraham I 3271744d0dSKishon Vijay Abraham I #include "linux-compat.h" 3385d5e707SKishon Vijay Abraham I 3441933c04SKever Yang DECLARE_GLOBAL_DATA_PTR; 3541933c04SKever Yang 36793d347fSKishon Vijay Abraham I static LIST_HEAD(dwc3_list); 3785d5e707SKishon Vijay Abraham I /* -------------------------------------------------------------------------- */ 3885d5e707SKishon Vijay Abraham I 397e9cb796SJoonyoung Shim static void dwc3_set_mode(struct dwc3 *dwc, u32 mode) 4085d5e707SKishon Vijay Abraham I { 4185d5e707SKishon Vijay Abraham I u32 reg; 4285d5e707SKishon Vijay Abraham I 4385d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 4485d5e707SKishon Vijay Abraham I reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); 4585d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_PRTCAPDIR(mode); 4685d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 4785d5e707SKishon Vijay Abraham I } 4885d5e707SKishon Vijay Abraham I 4985d5e707SKishon Vijay Abraham I /** 5085d5e707SKishon Vijay Abraham I * dwc3_core_soft_reset - Issues core soft reset and PHY reset 5185d5e707SKishon Vijay Abraham I * @dwc: pointer to our context structure 5285d5e707SKishon Vijay Abraham I */ 5385d5e707SKishon Vijay Abraham I static int dwc3_core_soft_reset(struct dwc3 *dwc) 5485d5e707SKishon Vijay Abraham I { 5585d5e707SKishon Vijay Abraham I u32 reg; 5685d5e707SKishon Vijay Abraham I 5785d5e707SKishon Vijay Abraham I /* Before Resetting PHY, put Core in Reset */ 5885d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 5985d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_CORESOFTRESET; 6085d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 6185d5e707SKishon Vijay Abraham I 6285d5e707SKishon Vijay Abraham I /* Assert USB3 PHY reset */ 6385d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 6485d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; 6585d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 6685d5e707SKishon Vijay Abraham I 6785d5e707SKishon Vijay Abraham I /* Assert USB2 PHY reset */ 6885d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 6985d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; 7085d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 7185d5e707SKishon Vijay Abraham I 7285d5e707SKishon Vijay Abraham I mdelay(100); 7385d5e707SKishon Vijay Abraham I 7485d5e707SKishon Vijay Abraham I /* Clear USB3 PHY reset */ 7585d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 7685d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; 7785d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 7885d5e707SKishon Vijay Abraham I 7985d5e707SKishon Vijay Abraham I /* Clear USB2 PHY reset */ 8085d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 8185d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; 8285d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 8385d5e707SKishon Vijay Abraham I 8485d5e707SKishon Vijay Abraham I mdelay(100); 8585d5e707SKishon Vijay Abraham I 8685d5e707SKishon Vijay Abraham I /* After PHYs are stable we can take Core out of reset state */ 8785d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 8885d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_CORESOFTRESET; 8985d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 9085d5e707SKishon Vijay Abraham I 9185d5e707SKishon Vijay Abraham I return 0; 9285d5e707SKishon Vijay Abraham I } 9385d5e707SKishon Vijay Abraham I 9485d5e707SKishon Vijay Abraham I /** 9585d5e707SKishon Vijay Abraham I * dwc3_free_one_event_buffer - Frees one event buffer 9685d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 9785d5e707SKishon Vijay Abraham I * @evt: Pointer to event buffer to be freed 9885d5e707SKishon Vijay Abraham I */ 9985d5e707SKishon Vijay Abraham I static void dwc3_free_one_event_buffer(struct dwc3 *dwc, 10085d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt) 10185d5e707SKishon Vijay Abraham I { 10271744d0dSKishon Vijay Abraham I dma_free_coherent(evt->buf); 10385d5e707SKishon Vijay Abraham I } 10485d5e707SKishon Vijay Abraham I 10585d5e707SKishon Vijay Abraham I /** 10685d5e707SKishon Vijay Abraham I * dwc3_alloc_one_event_buffer - Allocates one event buffer structure 10785d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 10885d5e707SKishon Vijay Abraham I * @length: size of the event buffer 10985d5e707SKishon Vijay Abraham I * 11085d5e707SKishon Vijay Abraham I * Returns a pointer to the allocated event buffer structure on success 11185d5e707SKishon Vijay Abraham I * otherwise ERR_PTR(errno). 11285d5e707SKishon Vijay Abraham I */ 11385d5e707SKishon Vijay Abraham I static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, 11485d5e707SKishon Vijay Abraham I unsigned length) 11585d5e707SKishon Vijay Abraham I { 11685d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 11785d5e707SKishon Vijay Abraham I 118b6985a21SMugunthan V N evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt), 119b6985a21SMugunthan V N GFP_KERNEL); 12085d5e707SKishon Vijay Abraham I if (!evt) 12185d5e707SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 12285d5e707SKishon Vijay Abraham I 12385d5e707SKishon Vijay Abraham I evt->dwc = dwc; 12485d5e707SKishon Vijay Abraham I evt->length = length; 12571744d0dSKishon Vijay Abraham I evt->buf = dma_alloc_coherent(length, 12671744d0dSKishon Vijay Abraham I (unsigned long *)&evt->dma); 12785d5e707SKishon Vijay Abraham I if (!evt->buf) 12885d5e707SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 12985d5e707SKishon Vijay Abraham I 130889239d6SPhilipp Tomsich dwc3_flush_cache((uintptr_t)evt->buf, evt->length); 131889239d6SPhilipp Tomsich 13285d5e707SKishon Vijay Abraham I return evt; 13385d5e707SKishon Vijay Abraham I } 13485d5e707SKishon Vijay Abraham I 13585d5e707SKishon Vijay Abraham I /** 13685d5e707SKishon Vijay Abraham I * dwc3_free_event_buffers - frees all allocated event buffers 13785d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 13885d5e707SKishon Vijay Abraham I */ 13985d5e707SKishon Vijay Abraham I static void dwc3_free_event_buffers(struct dwc3 *dwc) 14085d5e707SKishon Vijay Abraham I { 14185d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 14285d5e707SKishon Vijay Abraham I int i; 14385d5e707SKishon Vijay Abraham I 14485d5e707SKishon Vijay Abraham I for (i = 0; i < dwc->num_event_buffers; i++) { 14585d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[i]; 14685d5e707SKishon Vijay Abraham I if (evt) 14785d5e707SKishon Vijay Abraham I dwc3_free_one_event_buffer(dwc, evt); 14885d5e707SKishon Vijay Abraham I } 14985d5e707SKishon Vijay Abraham I } 15085d5e707SKishon Vijay Abraham I 15185d5e707SKishon Vijay Abraham I /** 15285d5e707SKishon Vijay Abraham I * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length 15385d5e707SKishon Vijay Abraham I * @dwc: pointer to our controller context structure 15485d5e707SKishon Vijay Abraham I * @length: size of event buffer 15585d5e707SKishon Vijay Abraham I * 15685d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. In the error case, dwc 15785d5e707SKishon Vijay Abraham I * may contain some buffers allocated but not all which were requested. 15885d5e707SKishon Vijay Abraham I */ 15985d5e707SKishon Vijay Abraham I static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) 16085d5e707SKishon Vijay Abraham I { 16185d5e707SKishon Vijay Abraham I int num; 16285d5e707SKishon Vijay Abraham I int i; 16385d5e707SKishon Vijay Abraham I 16485d5e707SKishon Vijay Abraham I num = DWC3_NUM_INT(dwc->hwparams.hwparams1); 16585d5e707SKishon Vijay Abraham I dwc->num_event_buffers = num; 16685d5e707SKishon Vijay Abraham I 167526a50f8SKishon Vijay Abraham I dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE, 168526a50f8SKishon Vijay Abraham I sizeof(*dwc->ev_buffs) * num); 16985d5e707SKishon Vijay Abraham I if (!dwc->ev_buffs) 17085d5e707SKishon Vijay Abraham I return -ENOMEM; 17185d5e707SKishon Vijay Abraham I 17285d5e707SKishon Vijay Abraham I for (i = 0; i < num; i++) { 17385d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 17485d5e707SKishon Vijay Abraham I 17585d5e707SKishon Vijay Abraham I evt = dwc3_alloc_one_event_buffer(dwc, length); 17685d5e707SKishon Vijay Abraham I if (IS_ERR(evt)) { 17785d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "can't allocate event buffer\n"); 17885d5e707SKishon Vijay Abraham I return PTR_ERR(evt); 17985d5e707SKishon Vijay Abraham I } 18085d5e707SKishon Vijay Abraham I dwc->ev_buffs[i] = evt; 18185d5e707SKishon Vijay Abraham I } 18285d5e707SKishon Vijay Abraham I 18385d5e707SKishon Vijay Abraham I return 0; 18485d5e707SKishon Vijay Abraham I } 18585d5e707SKishon Vijay Abraham I 18685d5e707SKishon Vijay Abraham I /** 18785d5e707SKishon Vijay Abraham I * dwc3_event_buffers_setup - setup our allocated event buffers 18885d5e707SKishon Vijay Abraham I * @dwc: pointer to our controller context structure 18985d5e707SKishon Vijay Abraham I * 19085d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. 19185d5e707SKishon Vijay Abraham I */ 19285d5e707SKishon Vijay Abraham I static int dwc3_event_buffers_setup(struct dwc3 *dwc) 19385d5e707SKishon Vijay Abraham I { 19485d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 19585d5e707SKishon Vijay Abraham I int n; 19685d5e707SKishon Vijay Abraham I 19785d5e707SKishon Vijay Abraham I for (n = 0; n < dwc->num_event_buffers; n++) { 19885d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[n]; 19985d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", 20085d5e707SKishon Vijay Abraham I evt->buf, (unsigned long long) evt->dma, 20185d5e707SKishon Vijay Abraham I evt->length); 20285d5e707SKishon Vijay Abraham I 20385d5e707SKishon Vijay Abraham I evt->lpos = 0; 20485d5e707SKishon Vijay Abraham I 20585d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 20685d5e707SKishon Vijay Abraham I lower_32_bits(evt->dma)); 20785d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 20885d5e707SKishon Vijay Abraham I upper_32_bits(evt->dma)); 20985d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 21085d5e707SKishon Vijay Abraham I DWC3_GEVNTSIZ_SIZE(evt->length)); 21185d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); 21285d5e707SKishon Vijay Abraham I } 21385d5e707SKishon Vijay Abraham I 21485d5e707SKishon Vijay Abraham I return 0; 21585d5e707SKishon Vijay Abraham I } 21685d5e707SKishon Vijay Abraham I 21785d5e707SKishon Vijay Abraham I static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) 21885d5e707SKishon Vijay Abraham I { 21985d5e707SKishon Vijay Abraham I struct dwc3_event_buffer *evt; 22085d5e707SKishon Vijay Abraham I int n; 22185d5e707SKishon Vijay Abraham I 22285d5e707SKishon Vijay Abraham I for (n = 0; n < dwc->num_event_buffers; n++) { 22385d5e707SKishon Vijay Abraham I evt = dwc->ev_buffs[n]; 22485d5e707SKishon Vijay Abraham I 22585d5e707SKishon Vijay Abraham I evt->lpos = 0; 22685d5e707SKishon Vijay Abraham I 22785d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); 22885d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); 22985d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK 23085d5e707SKishon Vijay Abraham I | DWC3_GEVNTSIZ_SIZE(0)); 23185d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); 23285d5e707SKishon Vijay Abraham I } 23385d5e707SKishon Vijay Abraham I } 23485d5e707SKishon Vijay Abraham I 23585d5e707SKishon Vijay Abraham I static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) 23685d5e707SKishon Vijay Abraham I { 23785d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 23885d5e707SKishon Vijay Abraham I return 0; 23985d5e707SKishon Vijay Abraham I 24085d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 24185d5e707SKishon Vijay Abraham I return 0; 24285d5e707SKishon Vijay Abraham I 24385d5e707SKishon Vijay Abraham I dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, 24485d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); 24585d5e707SKishon Vijay Abraham I if (!dwc->scratchbuf) 24685d5e707SKishon Vijay Abraham I return -ENOMEM; 24785d5e707SKishon Vijay Abraham I 24885d5e707SKishon Vijay Abraham I return 0; 24985d5e707SKishon Vijay Abraham I } 25085d5e707SKishon Vijay Abraham I 25185d5e707SKishon Vijay Abraham I static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) 25285d5e707SKishon Vijay Abraham I { 25385d5e707SKishon Vijay Abraham I dma_addr_t scratch_addr; 25485d5e707SKishon Vijay Abraham I u32 param; 25585d5e707SKishon Vijay Abraham I int ret; 25685d5e707SKishon Vijay Abraham I 25785d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 25885d5e707SKishon Vijay Abraham I return 0; 25985d5e707SKishon Vijay Abraham I 26085d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 26185d5e707SKishon Vijay Abraham I return 0; 26285d5e707SKishon Vijay Abraham I 26371744d0dSKishon Vijay Abraham I scratch_addr = dma_map_single(dwc->scratchbuf, 26485d5e707SKishon Vijay Abraham I dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE, 26585d5e707SKishon Vijay Abraham I DMA_BIDIRECTIONAL); 26685d5e707SKishon Vijay Abraham I if (dma_mapping_error(dwc->dev, scratch_addr)) { 26785d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to map scratch buffer\n"); 26885d5e707SKishon Vijay Abraham I ret = -EFAULT; 26985d5e707SKishon Vijay Abraham I goto err0; 27085d5e707SKishon Vijay Abraham I } 27185d5e707SKishon Vijay Abraham I 27285d5e707SKishon Vijay Abraham I dwc->scratch_addr = scratch_addr; 27385d5e707SKishon Vijay Abraham I 27485d5e707SKishon Vijay Abraham I param = lower_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_LO, param); 27885d5e707SKishon Vijay Abraham I if (ret < 0) 27985d5e707SKishon Vijay Abraham I goto err1; 28085d5e707SKishon Vijay Abraham I 28185d5e707SKishon Vijay Abraham I param = upper_32_bits(scratch_addr); 28285d5e707SKishon Vijay Abraham I 28385d5e707SKishon Vijay Abraham I ret = dwc3_send_gadget_generic_command(dwc, 28485d5e707SKishon Vijay Abraham I DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param); 28585d5e707SKishon Vijay Abraham I if (ret < 0) 28685d5e707SKishon Vijay Abraham I goto err1; 28785d5e707SKishon Vijay Abraham I 28885d5e707SKishon Vijay Abraham I return 0; 28985d5e707SKishon Vijay Abraham I 29085d5e707SKishon Vijay Abraham I err1: 29101c94c4aSMichal Simek dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch * 29285d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); 29385d5e707SKishon Vijay Abraham I 29485d5e707SKishon Vijay Abraham I err0: 29585d5e707SKishon Vijay Abraham I return ret; 29685d5e707SKishon Vijay Abraham I } 29785d5e707SKishon Vijay Abraham I 29885d5e707SKishon Vijay Abraham I static void dwc3_free_scratch_buffers(struct dwc3 *dwc) 29985d5e707SKishon Vijay Abraham I { 30085d5e707SKishon Vijay Abraham I if (!dwc->has_hibernation) 30185d5e707SKishon Vijay Abraham I return; 30285d5e707SKishon Vijay Abraham I 30385d5e707SKishon Vijay Abraham I if (!dwc->nr_scratch) 30485d5e707SKishon Vijay Abraham I return; 30585d5e707SKishon Vijay Abraham I 30601c94c4aSMichal Simek dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch * 30785d5e707SKishon Vijay Abraham I DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); 30885d5e707SKishon Vijay Abraham I kfree(dwc->scratchbuf); 30985d5e707SKishon Vijay Abraham I } 31085d5e707SKishon Vijay Abraham I 31185d5e707SKishon Vijay Abraham I static void dwc3_core_num_eps(struct dwc3 *dwc) 31285d5e707SKishon Vijay Abraham I { 31385d5e707SKishon Vijay Abraham I struct dwc3_hwparams *parms = &dwc->hwparams; 31485d5e707SKishon Vijay Abraham I 31585d5e707SKishon Vijay Abraham I dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); 31685d5e707SKishon Vijay Abraham I dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; 31785d5e707SKishon Vijay Abraham I 31885d5e707SKishon Vijay Abraham I dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n", 31985d5e707SKishon Vijay Abraham I dwc->num_in_eps, dwc->num_out_eps); 32085d5e707SKishon Vijay Abraham I } 32185d5e707SKishon Vijay Abraham I 32285d5e707SKishon Vijay Abraham I static void dwc3_cache_hwparams(struct dwc3 *dwc) 32385d5e707SKishon Vijay Abraham I { 32485d5e707SKishon Vijay Abraham I struct dwc3_hwparams *parms = &dwc->hwparams; 32585d5e707SKishon Vijay Abraham I 32685d5e707SKishon Vijay Abraham I parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); 32785d5e707SKishon Vijay Abraham I parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); 32885d5e707SKishon Vijay Abraham I parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); 32985d5e707SKishon Vijay Abraham I parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); 33085d5e707SKishon Vijay Abraham I parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); 33185d5e707SKishon Vijay Abraham I parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); 33285d5e707SKishon Vijay Abraham I parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); 33385d5e707SKishon Vijay Abraham I parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); 33485d5e707SKishon Vijay Abraham I parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); 33585d5e707SKishon Vijay Abraham I } 33685d5e707SKishon Vijay Abraham I 33785d5e707SKishon Vijay Abraham I /** 33885d5e707SKishon Vijay Abraham I * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core 33985d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 34085d5e707SKishon Vijay Abraham I */ 34185d5e707SKishon Vijay Abraham I static void dwc3_phy_setup(struct dwc3 *dwc) 34285d5e707SKishon Vijay Abraham I { 34385d5e707SKishon Vijay Abraham I u32 reg; 34485d5e707SKishon Vijay Abraham I 34585d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 34685d5e707SKishon Vijay Abraham I 34785d5e707SKishon Vijay Abraham I /* 34885d5e707SKishon Vijay Abraham I * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY 34985d5e707SKishon Vijay Abraham I * to '0' during coreConsultant configuration. So default value 35085d5e707SKishon Vijay Abraham I * will be '0' when the core is reset. Application needs to set it 35185d5e707SKishon Vijay Abraham I * to '1' after the core initialization is completed. 35285d5e707SKishon Vijay Abraham I */ 35385d5e707SKishon Vijay Abraham I if (dwc->revision > DWC3_REVISION_194A) 35485d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_SUSPHY; 35585d5e707SKishon Vijay Abraham I 35685d5e707SKishon Vijay Abraham I if (dwc->u2ss_inp3_quirk) 35785d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK; 35885d5e707SKishon Vijay Abraham I 35985d5e707SKishon Vijay Abraham I if (dwc->req_p1p2p3_quirk) 36085d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_REQP1P2P3; 36185d5e707SKishon Vijay Abraham I 36285d5e707SKishon Vijay Abraham I if (dwc->del_p1p2p3_quirk) 36385d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN; 36485d5e707SKishon Vijay Abraham I 36585d5e707SKishon Vijay Abraham I if (dwc->del_phy_power_chg_quirk) 36685d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE; 36785d5e707SKishon Vijay Abraham I 36885d5e707SKishon Vijay Abraham I if (dwc->lfps_filter_quirk) 36985d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_LFPSFILT; 37085d5e707SKishon Vijay Abraham I 37185d5e707SKishon Vijay Abraham I if (dwc->rx_detect_poll_quirk) 37285d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL; 37385d5e707SKishon Vijay Abraham I 37485d5e707SKishon Vijay Abraham I if (dwc->tx_de_emphasis_quirk) 37585d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis); 37685d5e707SKishon Vijay Abraham I 37785d5e707SKishon Vijay Abraham I if (dwc->dis_u3_susphy_quirk) 37885d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; 37985d5e707SKishon Vijay Abraham I 38085d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 38185d5e707SKishon Vijay Abraham I 38285d5e707SKishon Vijay Abraham I mdelay(100); 38385d5e707SKishon Vijay Abraham I 38485d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 38585d5e707SKishon Vijay Abraham I 38685d5e707SKishon Vijay Abraham I /* 38785d5e707SKishon Vijay Abraham I * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to 38885d5e707SKishon Vijay Abraham I * '0' during coreConsultant configuration. So default value will 38985d5e707SKishon Vijay Abraham I * be '0' when the core is reset. Application needs to set it to 39085d5e707SKishon Vijay Abraham I * '1' after the core initialization is completed. 39185d5e707SKishon Vijay Abraham I */ 39285d5e707SKishon Vijay Abraham I if (dwc->revision > DWC3_REVISION_194A) 39385d5e707SKishon Vijay Abraham I reg |= DWC3_GUSB2PHYCFG_SUSPHY; 39485d5e707SKishon Vijay Abraham I 39585d5e707SKishon Vijay Abraham I if (dwc->dis_u2_susphy_quirk) 39685d5e707SKishon Vijay Abraham I reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 39785d5e707SKishon Vijay Abraham I 39841933c04SKever Yang if (dwc->usb2_phyif_utmi_width == 16) { 39941933c04SKever Yang reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK; 40041933c04SKever Yang reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT; 40141933c04SKever Yang reg |= DWC3_GUSB2PHYCFG_PHYIF_16BIT; 40241933c04SKever Yang } 40385d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 40485d5e707SKishon Vijay Abraham I 40585d5e707SKishon Vijay Abraham I mdelay(100); 40685d5e707SKishon Vijay Abraham I } 40785d5e707SKishon Vijay Abraham I 40885d5e707SKishon Vijay Abraham I /** 40985d5e707SKishon Vijay Abraham I * dwc3_core_init - Low-level initialization of DWC3 Core 41085d5e707SKishon Vijay Abraham I * @dwc: Pointer to our controller context structure 41185d5e707SKishon Vijay Abraham I * 41285d5e707SKishon Vijay Abraham I * Returns 0 on success otherwise negative errno. 41385d5e707SKishon Vijay Abraham I */ 41485d5e707SKishon Vijay Abraham I static int dwc3_core_init(struct dwc3 *dwc) 41585d5e707SKishon Vijay Abraham I { 41685d5e707SKishon Vijay Abraham I unsigned long timeout; 41785d5e707SKishon Vijay Abraham I u32 hwparams4 = dwc->hwparams.hwparams4; 41885d5e707SKishon Vijay Abraham I u32 reg; 41985d5e707SKishon Vijay Abraham I int ret; 42085d5e707SKishon Vijay Abraham I 42185d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); 42285d5e707SKishon Vijay Abraham I /* This should read as U3 followed by revision number */ 42385d5e707SKishon Vijay Abraham I if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { 42485d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); 42585d5e707SKishon Vijay Abraham I ret = -ENODEV; 42685d5e707SKishon Vijay Abraham I goto err0; 42785d5e707SKishon Vijay Abraham I } 42885d5e707SKishon Vijay Abraham I dwc->revision = reg; 42985d5e707SKishon Vijay Abraham I 43085d5e707SKishon Vijay Abraham I /* Handle USB2.0-only core configuration */ 43185d5e707SKishon Vijay Abraham I if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) == 43285d5e707SKishon Vijay Abraham I DWC3_GHWPARAMS3_SSPHY_IFC_DIS) { 43385d5e707SKishon Vijay Abraham I if (dwc->maximum_speed == USB_SPEED_SUPER) 43485d5e707SKishon Vijay Abraham I dwc->maximum_speed = USB_SPEED_HIGH; 43585d5e707SKishon Vijay Abraham I } 43685d5e707SKishon Vijay Abraham I 43785d5e707SKishon Vijay Abraham I /* issue device SoftReset too */ 43871744d0dSKishon Vijay Abraham I timeout = 5000; 43985d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); 44071744d0dSKishon Vijay Abraham I while (timeout--) { 44185d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_DCTL); 44285d5e707SKishon Vijay Abraham I if (!(reg & DWC3_DCTL_CSFTRST)) 44385d5e707SKishon Vijay Abraham I break; 44471744d0dSKishon Vijay Abraham I }; 44585d5e707SKishon Vijay Abraham I 44671744d0dSKishon Vijay Abraham I if (!timeout) { 44785d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "Reset Timed Out\n"); 44885d5e707SKishon Vijay Abraham I ret = -ETIMEDOUT; 44985d5e707SKishon Vijay Abraham I goto err0; 45085d5e707SKishon Vijay Abraham I } 45185d5e707SKishon Vijay Abraham I 45285d5e707SKishon Vijay Abraham I ret = dwc3_core_soft_reset(dwc); 45385d5e707SKishon Vijay Abraham I if (ret) 45485d5e707SKishon Vijay Abraham I goto err0; 45585d5e707SKishon Vijay Abraham I 45685d5e707SKishon Vijay Abraham I reg = dwc3_readl(dwc->regs, DWC3_GCTL); 45785d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_SCALEDOWN_MASK; 45885d5e707SKishon Vijay Abraham I 45985d5e707SKishon Vijay Abraham I switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { 46085d5e707SKishon Vijay Abraham I case DWC3_GHWPARAMS1_EN_PWROPT_CLK: 46185d5e707SKishon Vijay Abraham I /** 46285d5e707SKishon Vijay Abraham I * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an 46385d5e707SKishon Vijay Abraham I * issue which would cause xHCI compliance tests to fail. 46485d5e707SKishon Vijay Abraham I * 46585d5e707SKishon Vijay Abraham I * Because of that we cannot enable clock gating on such 46685d5e707SKishon Vijay Abraham I * configurations. 46785d5e707SKishon Vijay Abraham I * 46885d5e707SKishon Vijay Abraham I * Refers to: 46985d5e707SKishon Vijay Abraham I * 47085d5e707SKishon Vijay Abraham I * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based 47185d5e707SKishon Vijay Abraham I * SOF/ITP Mode Used 47285d5e707SKishon Vijay Abraham I */ 47385d5e707SKishon Vijay Abraham I if ((dwc->dr_mode == USB_DR_MODE_HOST || 47485d5e707SKishon Vijay Abraham I dwc->dr_mode == USB_DR_MODE_OTG) && 47585d5e707SKishon Vijay Abraham I (dwc->revision >= DWC3_REVISION_210A && 47685d5e707SKishon Vijay Abraham I dwc->revision <= DWC3_REVISION_250A)) 47785d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC; 47885d5e707SKishon Vijay Abraham I else 47985d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_DSBLCLKGTNG; 48085d5e707SKishon Vijay Abraham I break; 48185d5e707SKishon Vijay Abraham I case DWC3_GHWPARAMS1_EN_PWROPT_HIB: 48285d5e707SKishon Vijay Abraham I /* enable hibernation here */ 48385d5e707SKishon Vijay Abraham I dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4); 48485d5e707SKishon Vijay Abraham I 48585d5e707SKishon Vijay Abraham I /* 48685d5e707SKishon Vijay Abraham I * REVISIT Enabling this bit so that host-mode hibernation 48785d5e707SKishon Vijay Abraham I * will work. Device-mode hibernation is not yet implemented. 48885d5e707SKishon Vijay Abraham I */ 48985d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_GBLHIBERNATIONEN; 49085d5e707SKishon Vijay Abraham I break; 49185d5e707SKishon Vijay Abraham I default: 49285d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "No power optimization available\n"); 49385d5e707SKishon Vijay Abraham I } 49485d5e707SKishon Vijay Abraham I 49585d5e707SKishon Vijay Abraham I /* check if current dwc3 is on simulation board */ 49685d5e707SKishon Vijay Abraham I if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) { 49785d5e707SKishon Vijay Abraham I dev_dbg(dwc->dev, "it is on FPGA board\n"); 49885d5e707SKishon Vijay Abraham I dwc->is_fpga = true; 49985d5e707SKishon Vijay Abraham I } 50085d5e707SKishon Vijay Abraham I 50171744d0dSKishon Vijay Abraham I if(dwc->disable_scramble_quirk && !dwc->is_fpga) 50271744d0dSKishon Vijay Abraham I WARN(true, 50385d5e707SKishon Vijay Abraham I "disable_scramble cannot be used on non-FPGA builds\n"); 50485d5e707SKishon Vijay Abraham I 50585d5e707SKishon Vijay Abraham I if (dwc->disable_scramble_quirk && dwc->is_fpga) 50685d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_DISSCRAMBLE; 50785d5e707SKishon Vijay Abraham I else 50885d5e707SKishon Vijay Abraham I reg &= ~DWC3_GCTL_DISSCRAMBLE; 50985d5e707SKishon Vijay Abraham I 51085d5e707SKishon Vijay Abraham I if (dwc->u2exit_lfps_quirk) 51185d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_U2EXIT_LFPS; 51285d5e707SKishon Vijay Abraham I 51385d5e707SKishon Vijay Abraham I /* 51485d5e707SKishon Vijay Abraham I * WORKAROUND: DWC3 revisions <1.90a have a bug 51585d5e707SKishon Vijay Abraham I * where the device can fail to connect at SuperSpeed 51685d5e707SKishon Vijay Abraham I * and falls back to high-speed mode which causes 51785d5e707SKishon Vijay Abraham I * the device to enter a Connect/Disconnect loop 51885d5e707SKishon Vijay Abraham I */ 51985d5e707SKishon Vijay Abraham I if (dwc->revision < DWC3_REVISION_190A) 52085d5e707SKishon Vijay Abraham I reg |= DWC3_GCTL_U2RSTECN; 52185d5e707SKishon Vijay Abraham I 52285d5e707SKishon Vijay Abraham I dwc3_core_num_eps(dwc); 52385d5e707SKishon Vijay Abraham I 52485d5e707SKishon Vijay Abraham I dwc3_writel(dwc->regs, DWC3_GCTL, reg); 52585d5e707SKishon Vijay Abraham I 52685d5e707SKishon Vijay Abraham I dwc3_phy_setup(dwc); 52785d5e707SKishon Vijay Abraham I 52885d5e707SKishon Vijay Abraham I ret = dwc3_alloc_scratch_buffers(dwc); 52985d5e707SKishon Vijay Abraham I if (ret) 53071744d0dSKishon Vijay Abraham I goto err0; 53185d5e707SKishon Vijay Abraham I 53285d5e707SKishon Vijay Abraham I ret = dwc3_setup_scratch_buffers(dwc); 53385d5e707SKishon Vijay Abraham I if (ret) 53471744d0dSKishon Vijay Abraham I goto err1; 53585d5e707SKishon Vijay Abraham I 53685d5e707SKishon Vijay Abraham I return 0; 53785d5e707SKishon Vijay Abraham I 53885d5e707SKishon Vijay Abraham I err1: 53971744d0dSKishon Vijay Abraham I dwc3_free_scratch_buffers(dwc); 54085d5e707SKishon Vijay Abraham I 54185d5e707SKishon Vijay Abraham I err0: 54285d5e707SKishon Vijay Abraham I return ret; 54385d5e707SKishon Vijay Abraham I } 54485d5e707SKishon Vijay Abraham I 54585d5e707SKishon Vijay Abraham I static void dwc3_core_exit(struct dwc3 *dwc) 54685d5e707SKishon Vijay Abraham I { 54785d5e707SKishon Vijay Abraham I dwc3_free_scratch_buffers(dwc); 54885d5e707SKishon Vijay Abraham I } 54985d5e707SKishon Vijay Abraham I 55085d5e707SKishon Vijay Abraham I static int dwc3_core_init_mode(struct dwc3 *dwc) 55185d5e707SKishon Vijay Abraham I { 55285d5e707SKishon Vijay Abraham I int ret; 55385d5e707SKishon Vijay Abraham I 55485d5e707SKishon Vijay Abraham I switch (dwc->dr_mode) { 55585d5e707SKishon Vijay Abraham I case USB_DR_MODE_PERIPHERAL: 55685d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); 55785d5e707SKishon Vijay Abraham I ret = dwc3_gadget_init(dwc); 55885d5e707SKishon Vijay Abraham I if (ret) { 55985d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize gadget\n"); 56085d5e707SKishon Vijay Abraham I return ret; 56185d5e707SKishon Vijay Abraham I } 56285d5e707SKishon Vijay Abraham I break; 56385d5e707SKishon Vijay Abraham I case USB_DR_MODE_HOST: 56485d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); 56585d5e707SKishon Vijay Abraham I ret = dwc3_host_init(dwc); 56685d5e707SKishon Vijay Abraham I if (ret) { 56785d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize host\n"); 56885d5e707SKishon Vijay Abraham I return ret; 56985d5e707SKishon Vijay Abraham I } 57085d5e707SKishon Vijay Abraham I break; 57185d5e707SKishon Vijay Abraham I case USB_DR_MODE_OTG: 57285d5e707SKishon Vijay Abraham I dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); 57385d5e707SKishon Vijay Abraham I ret = dwc3_host_init(dwc); 57485d5e707SKishon Vijay Abraham I if (ret) { 57585d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize host\n"); 57685d5e707SKishon Vijay Abraham I return ret; 57785d5e707SKishon Vijay Abraham I } 57885d5e707SKishon Vijay Abraham I 57985d5e707SKishon Vijay Abraham I ret = dwc3_gadget_init(dwc); 58085d5e707SKishon Vijay Abraham I if (ret) { 58185d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize gadget\n"); 58285d5e707SKishon Vijay Abraham I return ret; 58385d5e707SKishon Vijay Abraham I } 58485d5e707SKishon Vijay Abraham I break; 58585d5e707SKishon Vijay Abraham I default: 58685d5e707SKishon Vijay Abraham I dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); 58785d5e707SKishon Vijay Abraham I return -EINVAL; 58885d5e707SKishon Vijay Abraham I } 58985d5e707SKishon Vijay Abraham I 59085d5e707SKishon Vijay Abraham I return 0; 59185d5e707SKishon Vijay Abraham I } 59285d5e707SKishon Vijay Abraham I 59385d5e707SKishon Vijay Abraham I static void dwc3_core_exit_mode(struct dwc3 *dwc) 59485d5e707SKishon Vijay Abraham I { 59585d5e707SKishon Vijay Abraham I switch (dwc->dr_mode) { 59685d5e707SKishon Vijay Abraham I case USB_DR_MODE_PERIPHERAL: 59785d5e707SKishon Vijay Abraham I dwc3_gadget_exit(dwc); 59885d5e707SKishon Vijay Abraham I break; 59985d5e707SKishon Vijay Abraham I case USB_DR_MODE_HOST: 60085d5e707SKishon Vijay Abraham I dwc3_host_exit(dwc); 60185d5e707SKishon Vijay Abraham I break; 60285d5e707SKishon Vijay Abraham I case USB_DR_MODE_OTG: 60385d5e707SKishon Vijay Abraham I dwc3_host_exit(dwc); 60485d5e707SKishon Vijay Abraham I dwc3_gadget_exit(dwc); 60585d5e707SKishon Vijay Abraham I break; 60685d5e707SKishon Vijay Abraham I default: 60785d5e707SKishon Vijay Abraham I /* do nothing */ 60885d5e707SKishon Vijay Abraham I break; 60985d5e707SKishon Vijay Abraham I } 61085d5e707SKishon Vijay Abraham I } 61185d5e707SKishon Vijay Abraham I 61285d5e707SKishon Vijay Abraham I #define DWC3_ALIGN_MASK (16 - 1) 61385d5e707SKishon Vijay Abraham I 6148e1906a8SKishon Vijay Abraham I /** 6158e1906a8SKishon Vijay Abraham I * dwc3_uboot_init - dwc3 core uboot initialization code 6168e1906a8SKishon Vijay Abraham I * @dwc3_dev: struct dwc3_device containing initialization data 6178e1906a8SKishon Vijay Abraham I * 6188e1906a8SKishon Vijay Abraham I * Entry point for dwc3 driver (equivalent to dwc3_probe in linux 6198e1906a8SKishon Vijay Abraham I * kernel driver). Pointer to dwc3_device should be passed containing 6208e1906a8SKishon Vijay Abraham I * base address and other initialization data. Returns '0' on success and 6218e1906a8SKishon Vijay Abraham I * a negative value on failure. 6228e1906a8SKishon Vijay Abraham I * 6238e1906a8SKishon Vijay Abraham I * Generally called from board_usb_init() implemented in board file. 6248e1906a8SKishon Vijay Abraham I */ 6258e1906a8SKishon Vijay Abraham I int dwc3_uboot_init(struct dwc3_device *dwc3_dev) 62685d5e707SKishon Vijay Abraham I { 627793d347fSKishon Vijay Abraham I struct dwc3 *dwc; 628c2ad4e1bSFelipe Balbi struct device *dev = NULL; 62985d5e707SKishon Vijay Abraham I u8 lpm_nyet_threshold; 63085d5e707SKishon Vijay Abraham I u8 tx_de_emphasis; 63185d5e707SKishon Vijay Abraham I u8 hird_threshold; 63285d5e707SKishon Vijay Abraham I 63385d5e707SKishon Vijay Abraham I int ret; 63485d5e707SKishon Vijay Abraham I 63585d5e707SKishon Vijay Abraham I void *mem; 63641933c04SKever Yang const void *blob = gd->fdt_blob; 63741933c04SKever Yang int node; 63885d5e707SKishon Vijay Abraham I 639b6985a21SMugunthan V N mem = devm_kzalloc((struct udevice *)dev, 640b6985a21SMugunthan V N sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); 64185d5e707SKishon Vijay Abraham I if (!mem) 64285d5e707SKishon Vijay Abraham I return -ENOMEM; 64385d5e707SKishon Vijay Abraham I 64485d5e707SKishon Vijay Abraham I dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); 64585d5e707SKishon Vijay Abraham I dwc->mem = mem; 64685d5e707SKishon Vijay Abraham I 64701c94c4aSMichal Simek dwc->regs = (void *)(uintptr_t)(dwc3_dev->base + 64801c94c4aSMichal Simek DWC3_GLOBALS_REGS_START); 64985d5e707SKishon Vijay Abraham I 65085d5e707SKishon Vijay Abraham I /* default to highest possible threshold */ 65185d5e707SKishon Vijay Abraham I lpm_nyet_threshold = 0xff; 65285d5e707SKishon Vijay Abraham I 65385d5e707SKishon Vijay Abraham I /* default to -3.5dB de-emphasis */ 65485d5e707SKishon Vijay Abraham I tx_de_emphasis = 1; 65585d5e707SKishon Vijay Abraham I 65685d5e707SKishon Vijay Abraham I /* 65785d5e707SKishon Vijay Abraham I * default to assert utmi_sleep_n and use maximum allowed HIRD 65885d5e707SKishon Vijay Abraham I * threshold value of 0b1100 65985d5e707SKishon Vijay Abraham I */ 66085d5e707SKishon Vijay Abraham I hird_threshold = 12; 66185d5e707SKishon Vijay Abraham I 6628e1906a8SKishon Vijay Abraham I dwc->maximum_speed = dwc3_dev->maximum_speed; 6638e1906a8SKishon Vijay Abraham I dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum; 6648e1906a8SKishon Vijay Abraham I if (dwc3_dev->lpm_nyet_threshold) 6658e1906a8SKishon Vijay Abraham I lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold; 6668e1906a8SKishon Vijay Abraham I dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend; 6678e1906a8SKishon Vijay Abraham I if (dwc3_dev->hird_threshold) 6688e1906a8SKishon Vijay Abraham I hird_threshold = dwc3_dev->hird_threshold; 66985d5e707SKishon Vijay Abraham I 6708e1906a8SKishon Vijay Abraham I dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize; 6718e1906a8SKishon Vijay Abraham I dwc->dr_mode = dwc3_dev->dr_mode; 67285d5e707SKishon Vijay Abraham I 6738e1906a8SKishon Vijay Abraham I dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk; 6748e1906a8SKishon Vijay Abraham I dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk; 6758e1906a8SKishon Vijay Abraham I dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk; 6768e1906a8SKishon Vijay Abraham I dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk; 6778e1906a8SKishon Vijay Abraham I dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk; 6788e1906a8SKishon Vijay Abraham I dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk; 6798e1906a8SKishon Vijay Abraham I dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk; 6808e1906a8SKishon Vijay Abraham I dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk; 6818e1906a8SKishon Vijay Abraham I dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk; 6828e1906a8SKishon Vijay Abraham I dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk; 68385d5e707SKishon Vijay Abraham I 6848e1906a8SKishon Vijay Abraham I dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk; 6858e1906a8SKishon Vijay Abraham I if (dwc3_dev->tx_de_emphasis) 6868e1906a8SKishon Vijay Abraham I tx_de_emphasis = dwc3_dev->tx_de_emphasis; 68785d5e707SKishon Vijay Abraham I 68885d5e707SKishon Vijay Abraham I /* default to superspeed if no maximum_speed passed */ 68985d5e707SKishon Vijay Abraham I if (dwc->maximum_speed == USB_SPEED_UNKNOWN) 69085d5e707SKishon Vijay Abraham I dwc->maximum_speed = USB_SPEED_SUPER; 69185d5e707SKishon Vijay Abraham I 69285d5e707SKishon Vijay Abraham I dwc->lpm_nyet_threshold = lpm_nyet_threshold; 69385d5e707SKishon Vijay Abraham I dwc->tx_de_emphasis = tx_de_emphasis; 69485d5e707SKishon Vijay Abraham I 69585d5e707SKishon Vijay Abraham I dwc->hird_threshold = hird_threshold 69685d5e707SKishon Vijay Abraham I | (dwc->is_utmi_l1_suspend << 4); 69785d5e707SKishon Vijay Abraham I 698793d347fSKishon Vijay Abraham I dwc->index = dwc3_dev->index; 699793d347fSKishon Vijay Abraham I 7002878d5a3SWilliam Wu if (dwc3_dev->usb2_phyif_utmi_width) 7012878d5a3SWilliam Wu dwc->usb2_phyif_utmi_width = dwc3_dev->usb2_phyif_utmi_width; 7022878d5a3SWilliam Wu 70341933c04SKever Yang node = fdt_node_offset_by_compatible(blob, -1, 70441933c04SKever Yang "rockchip,rk3399-xhci"); 70541933c04SKever Yang if (node < 0) 70641933c04SKever Yang debug("%s dwc3 node not found\n", __func__); 70741933c04SKever Yang else 70841933c04SKever Yang dwc->usb2_phyif_utmi_width = 70941933c04SKever Yang fdtdec_get_int(blob, node, "snps,phyif-utmi-bits", -1); 71041933c04SKever Yang 71185d5e707SKishon Vijay Abraham I dwc3_cache_hwparams(dwc); 71285d5e707SKishon Vijay Abraham I 71385d5e707SKishon Vijay Abraham I ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); 71485d5e707SKishon Vijay Abraham I if (ret) { 71585d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to allocate event buffers\n"); 71671744d0dSKishon Vijay Abraham I return -ENOMEM; 71785d5e707SKishon Vijay Abraham I } 71885d5e707SKishon Vijay Abraham I 71985d5e707SKishon Vijay Abraham I if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) 72085d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_HOST; 72185d5e707SKishon Vijay Abraham I else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) 72285d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_PERIPHERAL; 72385d5e707SKishon Vijay Abraham I 72485d5e707SKishon Vijay Abraham I if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) 72585d5e707SKishon Vijay Abraham I dwc->dr_mode = USB_DR_MODE_OTG; 72685d5e707SKishon Vijay Abraham I 72785d5e707SKishon Vijay Abraham I ret = dwc3_core_init(dwc); 72885d5e707SKishon Vijay Abraham I if (ret) { 72985d5e707SKishon Vijay Abraham I dev_err(dev, "failed to initialize core\n"); 73085d5e707SKishon Vijay Abraham I goto err0; 73185d5e707SKishon Vijay Abraham I } 73285d5e707SKishon Vijay Abraham I 73385d5e707SKishon Vijay Abraham I ret = dwc3_event_buffers_setup(dwc); 73485d5e707SKishon Vijay Abraham I if (ret) { 73585d5e707SKishon Vijay Abraham I dev_err(dwc->dev, "failed to setup event buffers\n"); 73671744d0dSKishon Vijay Abraham I goto err1; 73785d5e707SKishon Vijay Abraham I } 73885d5e707SKishon Vijay Abraham I 73985d5e707SKishon Vijay Abraham I ret = dwc3_core_init_mode(dwc); 74085d5e707SKishon Vijay Abraham I if (ret) 74185d5e707SKishon Vijay Abraham I goto err2; 74285d5e707SKishon Vijay Abraham I 743793d347fSKishon Vijay Abraham I list_add_tail(&dwc->list, &dwc3_list); 744793d347fSKishon Vijay Abraham I 74585d5e707SKishon Vijay Abraham I return 0; 74685d5e707SKishon Vijay Abraham I 74785d5e707SKishon Vijay Abraham I err2: 74885d5e707SKishon Vijay Abraham I dwc3_event_buffers_cleanup(dwc); 74985d5e707SKishon Vijay Abraham I 75085d5e707SKishon Vijay Abraham I err1: 75185d5e707SKishon Vijay Abraham I dwc3_core_exit(dwc); 75285d5e707SKishon Vijay Abraham I 75385d5e707SKishon Vijay Abraham I err0: 75485d5e707SKishon Vijay Abraham I dwc3_free_event_buffers(dwc); 75585d5e707SKishon Vijay Abraham I 75685d5e707SKishon Vijay Abraham I return ret; 75785d5e707SKishon Vijay Abraham I } 75885d5e707SKishon Vijay Abraham I 7598e1906a8SKishon Vijay Abraham I /** 7608e1906a8SKishon Vijay Abraham I * dwc3_uboot_exit - dwc3 core uboot cleanup code 7618e1906a8SKishon Vijay Abraham I * @index: index of this controller 7628e1906a8SKishon Vijay Abraham I * 7638e1906a8SKishon Vijay Abraham I * Performs cleanup of memory allocated in dwc3_uboot_init and other misc 764793d347fSKishon Vijay Abraham I * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller 765793d347fSKishon Vijay Abraham I * should be passed and should match with the index passed in 766793d347fSKishon Vijay Abraham I * dwc3_device during init. 7678e1906a8SKishon Vijay Abraham I * 7688e1906a8SKishon Vijay Abraham I * Generally called from board file. 7698e1906a8SKishon Vijay Abraham I */ 770793d347fSKishon Vijay Abraham I void dwc3_uboot_exit(int index) 77185d5e707SKishon Vijay Abraham I { 772793d347fSKishon Vijay Abraham I struct dwc3 *dwc; 773793d347fSKishon Vijay Abraham I 774793d347fSKishon Vijay Abraham I list_for_each_entry(dwc, &dwc3_list, list) { 775793d347fSKishon Vijay Abraham I if (dwc->index != index) 776793d347fSKishon Vijay Abraham I continue; 777793d347fSKishon Vijay Abraham I 77885d5e707SKishon Vijay Abraham I dwc3_core_exit_mode(dwc); 77985d5e707SKishon Vijay Abraham I dwc3_event_buffers_cleanup(dwc); 78085d5e707SKishon Vijay Abraham I dwc3_free_event_buffers(dwc); 78185d5e707SKishon Vijay Abraham I dwc3_core_exit(dwc); 782793d347fSKishon Vijay Abraham I list_del(&dwc->list); 7838e1906a8SKishon Vijay Abraham I kfree(dwc->mem); 784793d347fSKishon Vijay Abraham I break; 785793d347fSKishon Vijay Abraham I } 78685d5e707SKishon Vijay Abraham I } 78785d5e707SKishon Vijay Abraham I 78827d3b89dSKishon Vijay Abraham I /** 78927d3b89dSKishon Vijay Abraham I * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt 79027d3b89dSKishon Vijay Abraham I * @index: index of this controller 79127d3b89dSKishon Vijay Abraham I * 79227d3b89dSKishon Vijay Abraham I * Invokes dwc3 gadget interrupts. 79327d3b89dSKishon Vijay Abraham I * 79427d3b89dSKishon Vijay Abraham I * Generally called from board file. 79527d3b89dSKishon Vijay Abraham I */ 79627d3b89dSKishon Vijay Abraham I void dwc3_uboot_handle_interrupt(int index) 79727d3b89dSKishon Vijay Abraham I { 79827d3b89dSKishon Vijay Abraham I struct dwc3 *dwc = NULL; 79927d3b89dSKishon Vijay Abraham I 80027d3b89dSKishon Vijay Abraham I list_for_each_entry(dwc, &dwc3_list, list) { 80127d3b89dSKishon Vijay Abraham I if (dwc->index != index) 80227d3b89dSKishon Vijay Abraham I continue; 80327d3b89dSKishon Vijay Abraham I 80427d3b89dSKishon Vijay Abraham I dwc3_gadget_uboot_handle_interrupt(dwc); 80527d3b89dSKishon Vijay Abraham I break; 80627d3b89dSKishon Vijay Abraham I } 80727d3b89dSKishon Vijay Abraham I } 80827d3b89dSKishon Vijay Abraham I 80985d5e707SKishon Vijay Abraham I MODULE_ALIAS("platform:dwc3"); 81085d5e707SKishon Vijay Abraham I MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 81185d5e707SKishon Vijay Abraham I MODULE_LICENSE("GPL v2"); 81285d5e707SKishon Vijay Abraham I MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); 813*434f82edSMugunthan V N 814*434f82edSMugunthan V N #ifdef CONFIG_DM_USB 815*434f82edSMugunthan V N 816*434f82edSMugunthan V N int dwc3_init(struct dwc3 *dwc) 817*434f82edSMugunthan V N { 818*434f82edSMugunthan V N int ret; 819*434f82edSMugunthan V N 820*434f82edSMugunthan V N dwc3_cache_hwparams(dwc); 821*434f82edSMugunthan V N 822*434f82edSMugunthan V N ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); 823*434f82edSMugunthan V N if (ret) { 824*434f82edSMugunthan V N dev_err(dwc->dev, "failed to allocate event buffers\n"); 825*434f82edSMugunthan V N return -ENOMEM; 826*434f82edSMugunthan V N } 827*434f82edSMugunthan V N 828*434f82edSMugunthan V N ret = dwc3_core_init(dwc); 829*434f82edSMugunthan V N if (ret) { 830*434f82edSMugunthan V N dev_err(dev, "failed to initialize core\n"); 831*434f82edSMugunthan V N goto core_fail; 832*434f82edSMugunthan V N } 833*434f82edSMugunthan V N 834*434f82edSMugunthan V N ret = dwc3_event_buffers_setup(dwc); 835*434f82edSMugunthan V N if (ret) { 836*434f82edSMugunthan V N dev_err(dwc->dev, "failed to setup event buffers\n"); 837*434f82edSMugunthan V N goto event_fail; 838*434f82edSMugunthan V N } 839*434f82edSMugunthan V N 840*434f82edSMugunthan V N ret = dwc3_core_init_mode(dwc); 841*434f82edSMugunthan V N if (ret) 842*434f82edSMugunthan V N goto mode_fail; 843*434f82edSMugunthan V N 844*434f82edSMugunthan V N return 0; 845*434f82edSMugunthan V N 846*434f82edSMugunthan V N mode_fail: 847*434f82edSMugunthan V N dwc3_event_buffers_cleanup(dwc); 848*434f82edSMugunthan V N 849*434f82edSMugunthan V N event_fail: 850*434f82edSMugunthan V N dwc3_core_exit(dwc); 851*434f82edSMugunthan V N 852*434f82edSMugunthan V N core_fail: 853*434f82edSMugunthan V N dwc3_free_event_buffers(dwc); 854*434f82edSMugunthan V N 855*434f82edSMugunthan V N return ret; 856*434f82edSMugunthan V N } 857*434f82edSMugunthan V N 858*434f82edSMugunthan V N void dwc3_remove(struct dwc3 *dwc) 859*434f82edSMugunthan V N { 860*434f82edSMugunthan V N dwc3_core_exit_mode(dwc); 861*434f82edSMugunthan V N dwc3_event_buffers_cleanup(dwc); 862*434f82edSMugunthan V N dwc3_free_event_buffers(dwc); 863*434f82edSMugunthan V N dwc3_core_exit(dwc); 864*434f82edSMugunthan V N kfree(dwc->mem); 865*434f82edSMugunthan V N } 866*434f82edSMugunthan V N 867*434f82edSMugunthan V N #endif 868