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