blob: 4f98c777a424612be2123db8788ece82105d071d [file] [log] [blame]
Dan Murphy677ff202013-08-26 08:54:52 -05001/*
2 * OMAP USB HOST xHCI Controller
3 *
4 * (C) Copyright 2013
5 * Texas Instruments, <www.ti.com>
6 *
7 * Author: Dan Murphy <dmurphy@ti.com>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
13#include <usb.h>
14#include <asm-generic/errno.h>
15#include <asm/omap_common.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/sys_proto.h>
Dan Murphy677ff202013-08-26 08:54:52 -050018
19#include <linux/compat.h>
20#include <linux/usb/dwc3.h>
Dan Murphy1aca6192013-10-11 12:28:14 -050021#include <linux/usb/xhci-omap.h>
Dan Murphy677ff202013-08-26 08:54:52 -050022
23#include "xhci.h"
24
25/* Declare global data pointer */
26DECLARE_GLOBAL_DATA_PTR;
27
28static struct omap_xhci omap;
29
30struct usb_dpll_params {
31 u16 m;
32 u8 n;
33 u8 freq:3;
34 u8 sd;
35 u32 mf;
36};
37
38#define NUM_USB_CLKS 6
39
40static struct usb_dpll_params omap_usb3_dpll_params[NUM_USB_CLKS] = {
41 {1250, 5, 4, 20, 0}, /* 12 MHz */
42 {3125, 20, 4, 20, 0}, /* 16.8 MHz */
43 {1172, 8, 4, 20, 65537}, /* 19.2 MHz */
44 {1250, 12, 4, 20, 0}, /* 26 MHz */
45 {3125, 47, 4, 20, 92843}, /* 38.4 MHz */
46 {1000, 7, 4, 10, 0}, /* 20 MHz */
47};
48
49static void omap_usb_dpll_relock(struct omap_usb3_phy *phy_regs)
50{
51 u32 val;
52
53 writel(SET_PLL_GO, &phy_regs->pll_go);
54 do {
55 val = readl(&phy_regs->pll_status);
56 if (val & PLL_LOCK)
57 break;
58 } while (1);
59}
60
61static void omap_usb_dpll_lock(struct omap_usb3_phy *phy_regs)
62{
63 u32 clk_index = get_sys_clk_index();
64 u32 val;
65
66 val = readl(&phy_regs->pll_config_1);
67 val &= ~PLL_REGN_MASK;
68 val |= omap_usb3_dpll_params[clk_index].n << PLL_REGN_SHIFT;
69 writel(val, &phy_regs->pll_config_1);
70
71 val = readl(&phy_regs->pll_config_2);
72 val &= ~PLL_SELFREQDCO_MASK;
73 val |= omap_usb3_dpll_params[clk_index].freq << PLL_SELFREQDCO_SHIFT;
74 writel(val, &phy_regs->pll_config_2);
75
76 val = readl(&phy_regs->pll_config_1);
77 val &= ~PLL_REGM_MASK;
78 val |= omap_usb3_dpll_params[clk_index].m << PLL_REGM_SHIFT;
79 writel(val, &phy_regs->pll_config_1);
80
81 val = readl(&phy_regs->pll_config_4);
82 val &= ~PLL_REGM_F_MASK;
83 val |= omap_usb3_dpll_params[clk_index].mf << PLL_REGM_F_SHIFT;
84 writel(val, &phy_regs->pll_config_4);
85
86 val = readl(&phy_regs->pll_config_3);
87 val &= ~PLL_SD_MASK;
88 val |= omap_usb3_dpll_params[clk_index].sd << PLL_SD_SHIFT;
89 writel(val, &phy_regs->pll_config_3);
90
91 omap_usb_dpll_relock(phy_regs);
92}
93
94static void usb3_phy_partial_powerup(struct omap_usb3_phy *phy_regs)
95{
96 u32 rate = get_sys_clk_freq()/1000000;
97 u32 val;
98
99 val = readl((*ctrl)->control_phy_power_usb);
100 val &= ~(USB3_PWRCTL_CLK_CMD_MASK | USB3_PWRCTL_CLK_FREQ_MASK);
101 val |= (USB3_PHY_PARTIAL_RX_POWERON | USB3_PHY_TX_RX_POWERON);
102 val |= rate << USB3_PWRCTL_CLK_FREQ_SHIFT;
103
104 writel(val, (*ctrl)->control_phy_power_usb);
105}
106
107static void usb3_phy_power(int on)
108{
109 u32 val;
110
111 val = readl((*ctrl)->control_phy_power_usb);
112 if (on) {
113 val &= ~USB3_PWRCTL_CLK_CMD_MASK;
114 val |= USB3_PHY_TX_RX_POWERON;
115 } else {
116 val &= (~USB3_PWRCTL_CLK_CMD_MASK & ~USB3_PHY_TX_RX_POWERON);
117 }
118
119 writel(val, (*ctrl)->control_phy_power_usb);
120}
121
122static void dwc_usb3_phy_init(struct omap_usb3_phy *phy_regs)
123{
124 omap_usb_dpll_lock(phy_regs);
125
126 usb3_phy_partial_powerup(phy_regs);
127 /*
128 * Give enough time for the PHY to partially power-up before
129 * powering it up completely. delay value suggested by the HW
130 * team.
131 */
132 mdelay(100);
133 usb3_phy_power(1);
134}
135
136static void omap_enable_phy_clocks(struct omap_xhci *omap)
137{
138 u32 val;
139
140 /* Setting OCP2SCP1 register */
141 setbits_le32((*prcm)->cm_l3init_ocp2scp1_clkctrl,
142 OCP2SCP1_CLKCTRL_MODULEMODE_HW);
143
144 /* Turn on 32K AON clk */
145 setbits_le32((*prcm)->cm_coreaon_usb_phy_core_clkctrl,
146 USBPHY_CORE_CLKCTRL_OPTFCLKEN_CLK32K);
147
148 /* Setting CM_L3INIT_CLKSTCTRL to 0x0 i.e NO sleep */
149 writel(0x0, (*prcm)->cm_l3init_clkstctrl);
150
151 val = (USBOTGSS_DMADISABLE |
152 USBOTGSS_STANDBYMODE_SMRT_WKUP |
153 USBOTGSS_IDLEMODE_NOIDLE);
154 writel(val, &omap->otg_wrapper->sysconfig);
155
156 /* Clear the utmi OTG status */
157 val = readl(&omap->otg_wrapper->utmi_otg_status);
158 writel(val, &omap->otg_wrapper->utmi_otg_status);
159
160 /* Enable interrupts */
161 writel(USBOTGSS_COREIRQ_EN, &omap->otg_wrapper->irqenable_set_0);
162 val = (USBOTGSS_IRQ_SET_1_IDPULLUP_FALL_EN |
163 USBOTGSS_IRQ_SET_1_DISCHRGVBUS_FALL_EN |
164 USBOTGSS_IRQ_SET_1_CHRGVBUS_FALL_EN |
165 USBOTGSS_IRQ_SET_1_DRVVBUS_FALL_EN |
166 USBOTGSS_IRQ_SET_1_IDPULLUP_RISE_EN |
167 USBOTGSS_IRQ_SET_1_DISCHRGVBUS_RISE_EN |
168 USBOTGSS_IRQ_SET_1_CHRGVBUS_RISE_EN |
169 USBOTGSS_IRQ_SET_1_DRVVBUS_RISE_EN |
170 USBOTGSS_IRQ_SET_1_OEVT_EN);
171 writel(val, &omap->otg_wrapper->irqenable_set_1);
172
173 /* Clear the IRQ status */
174 val = readl(&omap->otg_wrapper->irqstatus_1);
175 writel(val, &omap->otg_wrapper->irqstatus_1);
176 val = readl(&omap->otg_wrapper->irqstatus_0);
177 writel(val, &omap->otg_wrapper->irqstatus_0);
178
179 /* Enable the USB OTG Super speed clocks */
180 val = (OPTFCLKEN_REFCLK960M | OTG_SS_CLKCTRL_MODULEMODE_HW);
181 setbits_le32((*prcm)->cm_l3init_usb_otg_ss_clkctrl, val);
182
183};
184
Dan Murphy90690752013-10-11 12:28:15 -0500185inline int __board_usb_init(int index, enum board_usb_init_type init)
Dan Murphy677ff202013-08-26 08:54:52 -0500186{
187 return 0;
188}
Dan Murphy90690752013-10-11 12:28:15 -0500189int board_usb_init(int index, enum board_usb_init_type init) \
190 __attribute__((weak, alias("__board_usb_init")));
Dan Murphy677ff202013-08-26 08:54:52 -0500191
192static void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
193{
194 clrsetbits_le32(&dwc3_reg->g_ctl,
195 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
196 DWC3_GCTL_PRTCAPDIR(mode));
197}
198
199static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
200{
201 /* Before Resetting PHY, put Core in Reset */
202 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
203
204 /* Assert USB3 PHY reset */
205 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
206
207 /* Assert USB2 PHY reset */
208 setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
209
210 mdelay(100);
211
212 /* Clear USB3 PHY reset */
213 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
214
215 /* Clear USB2 PHY reset */
216 clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
217
218 /* After PHYs are stable we can take Core out of reset state */
219 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
220}
221
222static int dwc3_core_init(struct dwc3 *dwc3_reg)
223{
224 u32 reg;
225 u32 revision;
226 unsigned int dwc3_hwparams1;
227
228 revision = readl(&dwc3_reg->g_snpsid);
229 /* This should read as U3 followed by revision number */
230 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
231 puts("this is not a DesignWare USB3 DRD Core\n");
232 return -1;
233 }
234
235 dwc3_core_soft_reset(dwc3_reg);
236
237 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
238
239 reg = readl(&dwc3_reg->g_ctl);
240 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
241 reg &= ~DWC3_GCTL_DISSCRAMBLE;
242 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
243 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
244 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
245 break;
246 default:
247 debug("No power optimization available\n");
248 }
249
250 /*
251 * WORKAROUND: DWC3 revisions <1.90a have a bug
252 * where the device can fail to connect at SuperSpeed
253 * and falls back to high-speed mode which causes
254 * the device to enter a Connect/Disconnect loop
255 */
256 if ((revision & DWC3_REVISION_MASK) < 0x190a)
257 reg |= DWC3_GCTL_U2RSTECN;
258
259 writel(reg, &dwc3_reg->g_ctl);
260
261 return 0;
262}
263
264static int omap_xhci_core_init(struct omap_xhci *omap)
265{
266 int ret = 0;
267
268 omap_enable_phy_clocks(omap);
269
270 dwc_usb3_phy_init(omap->usb3_phy);
271
272 ret = dwc3_core_init(omap->dwc3_reg);
273 if (ret) {
274 debug("%s:failed to initialize core\n", __func__);
275 return ret;
276 }
277
278 /* We are hard-coding DWC3 core to Host Mode */
279 dwc3_set_mode(omap->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
280
281 return ret;
282}
283
284static void omap_xhci_core_exit(struct omap_xhci *omap)
285{
286 usb3_phy_power(0);
287}
288
289int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
290{
291 struct omap_xhci *ctx = &omap;
292 int ret = 0;
293
294 ctx->hcd = (struct xhci_hccr *)OMAP_XHCI_BASE;
295 ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
296 ctx->usb3_phy = (struct omap_usb3_phy *)OMAP_OCP1_SCP_BASE;
297 ctx->otg_wrapper = (struct omap_dwc_wrapper *)OMAP_OTG_WRAPPER_BASE;
298
Dan Murphy90690752013-10-11 12:28:15 -0500299 ret = board_usb_init(index, USB_INIT_HOST);
Dan Murphy677ff202013-08-26 08:54:52 -0500300 if (ret != 0) {
301 puts("Failed to initialize board for USB\n");
302 return ret;
303 }
304
305 ret = omap_xhci_core_init(ctx);
306 if (ret < 0) {
307 puts("Failed to initialize xhci\n");
308 return ret;
309 }
310
311 *hccr = (struct xhci_hccr *)(OMAP_XHCI_BASE);
312 *hcor = (struct xhci_hcor *)((uint32_t) *hccr
313 + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
314
315 debug("omap-xhci: init hccr %x and hcor %x hc_length %d\n",
316 (uint32_t)*hccr, (uint32_t)*hcor,
317 (uint32_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
318
319 return ret;
320}
321
322void xhci_hcd_stop(int index)
323{
324 struct omap_xhci *ctx = &omap;
325
326 omap_xhci_core_exit(ctx);
327}