xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-tegra.c (revision b03f4b3742a728c13a89f3fbf8a9a2ec43061025)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * Copyright (c) 2009-2013 NVIDIA Corporation
4  * Copyright (c) 2013 Lucas Stach
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <asm/errno.h>
11 #include <asm/io.h>
12 #include <asm-generic/gpio.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch-tegra/usb.h>
15 #include <asm/arch-tegra/clk_rst.h>
16 #include <asm/arch/usb.h>
17 #include <usb.h>
18 #include <usb/ulpi.h>
19 #include <libfdt.h>
20 #include <fdtdec.h>
21 
22 #include "ehci.h"
23 
24 #define USB1_ADDR_MASK	0xFFFF0000
25 
26 #define HOSTPC1_DEVLC	0x84
27 #define HOSTPC1_PSPD(x)		(((x) >> 25) & 0x3)
28 
29 #ifdef CONFIG_USB_ULPI
30 	#ifndef CONFIG_USB_ULPI_VIEWPORT
31 	#error	"To use CONFIG_USB_ULPI on Tegra Boards you have to also \
32 		define CONFIG_USB_ULPI_VIEWPORT"
33 	#endif
34 #endif
35 
36 enum {
37 	USB_PORTS_MAX	= 3,		/* Maximum ports we allow */
38 };
39 
40 /* Parameters we need for USB */
41 enum {
42 	PARAM_DIVN,                     /* PLL FEEDBACK DIVIDer */
43 	PARAM_DIVM,                     /* PLL INPUT DIVIDER */
44 	PARAM_DIVP,                     /* POST DIVIDER (2^N) */
45 	PARAM_CPCON,                    /* BASE PLLC CHARGE Pump setup ctrl */
46 	PARAM_LFCON,                    /* BASE PLLC LOOP FILter setup ctrl */
47 	PARAM_ENABLE_DELAY_COUNT,       /* PLL-U Enable Delay Count */
48 	PARAM_STABLE_COUNT,             /* PLL-U STABLE count */
49 	PARAM_ACTIVE_DELAY_COUNT,       /* PLL-U Active delay count */
50 	PARAM_XTAL_FREQ_COUNT,          /* PLL-U XTAL frequency count */
51 	PARAM_DEBOUNCE_A_TIME,          /* 10MS DELAY for BIAS_DEBOUNCE_A */
52 	PARAM_BIAS_TIME,                /* 20US DELAY AFter bias cell op */
53 
54 	PARAM_COUNT
55 };
56 
57 /* Possible port types (dual role mode) */
58 enum dr_mode {
59 	DR_MODE_NONE = 0,
60 	DR_MODE_HOST,		/* supports host operation */
61 	DR_MODE_DEVICE,		/* supports device operation */
62 	DR_MODE_OTG,		/* supports both */
63 };
64 
65 /* Information about a USB port */
66 struct fdt_usb {
67 	struct usb_ctlr *reg;	/* address of registers in physical memory */
68 	unsigned utmi:1;	/* 1 if port has external tranceiver, else 0 */
69 	unsigned ulpi:1;	/* 1 if port has external ULPI transceiver */
70 	unsigned enabled:1;	/* 1 to enable, 0 to disable */
71 	unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
72 	unsigned initialized:1; /* has this port already been initialized? */
73 	enum dr_mode dr_mode;	/* dual role mode */
74 	enum periph_id periph_id;/* peripheral id */
75 	struct fdt_gpio_state vbus_gpio;	/* GPIO for vbus enable */
76 	struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
77 };
78 
79 static struct fdt_usb port[USB_PORTS_MAX];	/* List of valid USB ports */
80 static unsigned port_count;			/* Number of available ports */
81 /* Port that needs to clear CSC after Port Reset */
82 static u32 port_addr_clear_csc;
83 
84 /*
85  * This table has USB timing parameters for each Oscillator frequency we
86  * support. There are four sets of values:
87  *
88  * 1. PLLU configuration information (reference clock is osc/clk_m and
89  * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
90  *
91  *  Reference frequency     13.0MHz      19.2MHz      12.0MHz      26.0MHz
92  *  ----------------------------------------------------------------------
93  *      DIVN                960 (0x3c0)  200 (0c8)    960 (3c0h)   960 (3c0)
94  *      DIVM                13 (0d)      4 (04)       12 (0c)      26 (1a)
95  * Filter frequency (MHz)   1            4.8          6            2
96  * CPCON                    1100b        0011b        1100b        1100b
97  * LFCON0                   0            0            0            0
98  *
99  * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
100  *
101  * Reference frequency     13.0MHz         19.2MHz         12.0MHz     26.0MHz
102  * ---------------------------------------------------------------------------
103  * PLLU_ENABLE_DLY_COUNT   02 (0x02)       03 (03)         02 (02)     04 (04)
104  * PLLU_STABLE_COUNT       51 (33)         75 (4B)         47 (2F)    102 (66)
105  * PLL_ACTIVE_DLY_COUNT    05 (05)         06 (06)         04 (04)     09 (09)
106  * XTAL_FREQ_COUNT        127 (7F)        187 (BB)        118 (76)    254 (FE)
107  *
108  * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
109  * SessEnd. Each of these signals have their own debouncer and for each of
110  * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
111  * BIAS_DEBOUNCE_B).
112  *
113  * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
114  *    0xffff -> No debouncing at all
115  *    <n> ms = <n> *1000 / (1/19.2MHz) / 4
116  *
117  * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
118  * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4  = 4800 = 0x12c0
119  *
120  * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
121  * values, so we can keep those to default.
122  *
123  * 4. The 20 microsecond delay after bias cell operation.
124  */
125 static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
126 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
127 	{ 0x3C0, 0x0D, 0x00, 0xC,   0,  0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
128 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
129 	{ 0x3C0, 0x0C, 0x00, 0xC,   0,  0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
130 	{ 0x3C0, 0x1A, 0x00, 0xC,   0,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
131 };
132 
133 static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
134 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
135 	{ 0x3C0, 0x0D, 0x00, 0xC,   1,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
136 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
137 	{ 0x3C0, 0x0C, 0x00, 0xC,   1,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
138 	{ 0x3C0, 0x1A, 0x00, 0xC,   1,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
139 };
140 
141 static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
142 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
143 	{ 0x3C0, 0x0D, 0x00, 0xC,   2,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
144 	{ 0x0C8, 0x04, 0x00, 0x3,   2,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
145 	{ 0x3C0, 0x0C, 0x00, 0xC,   2,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
146 	{ 0x3C0, 0x1A, 0x00, 0xC,   2,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
147 };
148 
149 /* UTMIP Idle Wait Delay */
150 static const u8 utmip_idle_wait_delay = 17;
151 
152 /* UTMIP Elastic limit */
153 static const u8 utmip_elastic_limit = 16;
154 
155 /* UTMIP High Speed Sync Start Delay */
156 static const u8 utmip_hs_sync_start_delay = 9;
157 
158 struct fdt_usb_controller {
159 	int compat;
160 	/* flag to determine whether controller supports hostpc register */
161 	u32 has_hostpc:1;
162 	const unsigned *pll_parameter;
163 };
164 
165 static struct fdt_usb_controller fdt_usb_controllers[] = {
166 	{
167 		.compat		= COMPAT_NVIDIA_TEGRA20_USB,
168 		.has_hostpc	= 0,
169 		.pll_parameter	= (const unsigned *)T20_usb_pll,
170 	},
171 	{
172 		.compat		= COMPAT_NVIDIA_TEGRA30_USB,
173 		.has_hostpc	= 1,
174 		.pll_parameter	= (const unsigned *)T30_usb_pll,
175 	},
176 	{
177 		.compat		= COMPAT_NVIDIA_TEGRA114_USB,
178 		.has_hostpc	= 1,
179 		.pll_parameter	= (const unsigned *)T114_usb_pll,
180 	},
181 };
182 
183 static struct fdt_usb_controller *controller;
184 
185 /*
186  * A known hardware issue where Connect Status Change bit of PORTSC register
187  * of USB1 controller will be set after Port Reset.
188  * We have to clear it in order for later device enumeration to proceed.
189  * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
190  * in "ehci-hcd.c".
191  */
192 void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
193 {
194 	mdelay(50);
195 	/* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
196 	if (controller->has_hostpc)
197 		*reg |= EHCI_PS_PE;
198 
199 	if (((u32)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
200 		return;
201 	/* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
202 	if (ehci_readl(status_reg) & EHCI_PS_CSC)
203 		*reg |= EHCI_PS_CSC;
204 }
205 
206 /*
207  * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
208  * in "ehci-hcd.c".
209  */
210 void ehci_set_usbmode(int index)
211 {
212 	struct fdt_usb *config;
213 	struct usb_ctlr *usbctlr;
214 	uint32_t tmp;
215 
216 	config = &port[index];
217 	usbctlr = config->reg;
218 
219 	tmp = ehci_readl(&usbctlr->usb_mode);
220 	tmp |= USBMODE_CM_HC;
221 	ehci_writel(&usbctlr->usb_mode, tmp);
222 }
223 
224 /*
225  * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
226  * in "ehci-hcd.c".
227  */
228 int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
229 {
230 	uint32_t tmp;
231 	uint32_t *reg_ptr;
232 
233 	if (controller->has_hostpc) {
234 		reg_ptr = (uint32_t *)((u8 *)&hcor->or_usbcmd + HOSTPC1_DEVLC);
235 		tmp = ehci_readl(reg_ptr);
236 		return HOSTPC1_PSPD(tmp);
237 	} else
238 		return PORTSC_PSPD(reg);
239 }
240 
241 /* Put the port into host mode */
242 static void set_host_mode(struct fdt_usb *config)
243 {
244 	/*
245 	 * If we are an OTG port, check if remote host is driving VBus and
246 	 * bail out in this case.
247 	 */
248 	if (config->dr_mode == DR_MODE_OTG &&
249 		(readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
250 		return;
251 
252 	/*
253 	 * If not driving, we set the GPIO to enable VBUS. We assume
254 	 * that the pinmux is set up correctly for this.
255 	 */
256 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
257 		fdtdec_setup_gpio(&config->vbus_gpio);
258 		gpio_direction_output(config->vbus_gpio.gpio,
259 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
260 				 0 : 1);
261 		debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
262 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
263 				"low" : "high");
264 	}
265 }
266 
267 void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
268 {
269 	/* Reset the USB controller with 2us delay */
270 	reset_periph(config->periph_id, 2);
271 
272 	/*
273 	 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
274 	 * base address
275 	 */
276 	if (config->has_legacy_mode)
277 		setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
278 
279 	/* Put UTMIP1/3 in reset */
280 	setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
281 
282 	/* Enable the UTMIP PHY */
283 	if (config->utmi)
284 		setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
285 }
286 
287 static const unsigned *get_pll_timing(void)
288 {
289 	const unsigned *timing;
290 
291 	timing = controller->pll_parameter +
292 		clock_get_osc_freq() * PARAM_COUNT;
293 
294 	return timing;
295 }
296 
297 /* set up the UTMI USB controller with the parameters provided */
298 static int init_utmi_usb_controller(struct fdt_usb *config)
299 {
300 	u32 val;
301 	int loop_count;
302 	const unsigned *timing;
303 	struct usb_ctlr *usbctlr = config->reg;
304 	struct clk_rst_ctlr *clkrst;
305 	struct usb_ctlr *usb1ctlr;
306 
307 	clock_enable(config->periph_id);
308 
309 	/* Reset the usb controller */
310 	usbf_reset_controller(config, usbctlr);
311 
312 	/* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
313 	clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
314 
315 	/* Follow the crystal clock disable by >100ns delay */
316 	udelay(1);
317 
318 	/*
319 	 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
320 	 * mux must be switched to actually use a_sess_vld threshold.
321 	 */
322 	if (config->dr_mode == DR_MODE_OTG &&
323 	    fdt_gpio_isvalid(&config->vbus_gpio))
324 		clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
325 			VBUS_SENSE_CTL_MASK,
326 			VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
327 
328 	/*
329 	 * PLL Delay CONFIGURATION settings. The following parameters control
330 	 * the bring up of the plls.
331 	 */
332 	timing = get_pll_timing();
333 
334 	if (!controller->has_hostpc) {
335 		val = readl(&usbctlr->utmip_misc_cfg1);
336 		clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
337 				timing[PARAM_STABLE_COUNT] <<
338 				UTMIP_PLLU_STABLE_COUNT_SHIFT);
339 		clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
340 				timing[PARAM_ACTIVE_DELAY_COUNT] <<
341 				UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
342 		writel(val, &usbctlr->utmip_misc_cfg1);
343 
344 		/* Set PLL enable delay count and crystal frequency count */
345 		val = readl(&usbctlr->utmip_pll_cfg1);
346 		clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
347 				timing[PARAM_ENABLE_DELAY_COUNT] <<
348 				UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
349 		clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
350 				timing[PARAM_XTAL_FREQ_COUNT] <<
351 				UTMIP_XTAL_FREQ_COUNT_SHIFT);
352 		writel(val, &usbctlr->utmip_pll_cfg1);
353 	} else {
354 		clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
355 
356 		val = readl(&clkrst->crc_utmip_pll_cfg2);
357 		clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
358 				timing[PARAM_STABLE_COUNT] <<
359 				UTMIP_PLLU_STABLE_COUNT_SHIFT);
360 		clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
361 				timing[PARAM_ACTIVE_DELAY_COUNT] <<
362 				UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
363 		writel(val, &clkrst->crc_utmip_pll_cfg2);
364 
365 		/* Set PLL enable delay count and crystal frequency count */
366 		val = readl(&clkrst->crc_utmip_pll_cfg1);
367 		clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
368 				timing[PARAM_ENABLE_DELAY_COUNT] <<
369 				UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
370 		clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
371 				timing[PARAM_XTAL_FREQ_COUNT] <<
372 				UTMIP_XTAL_FREQ_COUNT_SHIFT);
373 		writel(val, &clkrst->crc_utmip_pll_cfg1);
374 
375 		/* Disable Power Down state for PLL */
376 		clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
377 			     PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
378 			     PLL_ACTIVE_POWERDOWN);
379 
380 		/* Recommended PHY settings for EYE diagram */
381 		val = readl(&usbctlr->utmip_xcvr_cfg0);
382 		clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
383 				0x4 << UTMIP_XCVR_SETUP_SHIFT);
384 		clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
385 				0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
386 		clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
387 				0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
388 		writel(val, &usbctlr->utmip_xcvr_cfg0);
389 		clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
390 				UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
391 				0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
392 
393 		/* Some registers can be controlled from USB1 only. */
394 		if (config->periph_id != PERIPH_ID_USBD) {
395 			clock_enable(PERIPH_ID_USBD);
396 			/* Disable Reset if in Reset state */
397 			reset_set_enable(PERIPH_ID_USBD, 0);
398 		}
399 		usb1ctlr = (struct usb_ctlr *)
400 			((u32)config->reg & USB1_ADDR_MASK);
401 		val = readl(&usb1ctlr->utmip_bias_cfg0);
402 		setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
403 		clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
404 				0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
405 		clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
406 				0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
407 		writel(val, &usb1ctlr->utmip_bias_cfg0);
408 
409 		/* Miscellaneous setting mentioned in Programming Guide */
410 		clrbits_le32(&usbctlr->utmip_misc_cfg0,
411 			     UTMIP_SUSPEND_EXIT_ON_EDGE);
412 	}
413 
414 	/* Setting the tracking length time */
415 	clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
416 		UTMIP_BIAS_PDTRK_COUNT_MASK,
417 		timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
418 
419 	/* Program debounce time for VBUS to become valid */
420 	clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
421 		UTMIP_DEBOUNCE_CFG0_MASK,
422 		timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
423 
424 	setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
425 
426 	/* Disable battery charge enabling bit */
427 	setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
428 
429 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
430 	setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
431 
432 	/*
433 	 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
434 	 * Setting these fields, together with default values of the
435 	 * other fields, results in programming the registers below as
436 	 * follows:
437 	 *         UTMIP_HSRX_CFG0 = 0x9168c000
438 	 *         UTMIP_HSRX_CFG1 = 0x13
439 	 */
440 
441 	/* Set PLL enable delay count and Crystal frequency count */
442 	val = readl(&usbctlr->utmip_hsrx_cfg0);
443 	clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
444 		utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
445 	clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
446 		utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
447 	writel(val, &usbctlr->utmip_hsrx_cfg0);
448 
449 	/* Configure the UTMIP_HS_SYNC_START_DLY */
450 	clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
451 		UTMIP_HS_SYNC_START_DLY_MASK,
452 		utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
453 
454 	/* Preceed the crystal clock disable by >100ns delay. */
455 	udelay(1);
456 
457 	/* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
458 	setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
459 
460 	if (controller->has_hostpc) {
461 		if (config->periph_id == PERIPH_ID_USBD)
462 			clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
463 				     UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
464 		if (config->periph_id == PERIPH_ID_USB2)
465 			clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
466 				     UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
467 		if (config->periph_id == PERIPH_ID_USB3)
468 			clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
469 				     UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
470 	}
471 	/* Finished the per-controller init. */
472 
473 	/* De-assert UTMIP_RESET to bring out of reset. */
474 	clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
475 
476 	/* Wait for the phy clock to become valid in 100 ms */
477 	for (loop_count = 100000; loop_count != 0; loop_count--) {
478 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
479 			break;
480 		udelay(1);
481 	}
482 	if (!loop_count)
483 		return -1;
484 
485 	/* Disable ICUSB FS/LS transceiver */
486 	clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
487 
488 	/* Select UTMI parallel interface */
489 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
490 			PTS_UTMI << PTS_SHIFT);
491 	clrbits_le32(&usbctlr->port_sc1, STS);
492 
493 	/* Deassert power down state */
494 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
495 		UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
496 	clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
497 		UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
498 
499 	if (controller->has_hostpc) {
500 		/*
501 		 * BIAS Pad Power Down is common among all 3 USB
502 		 * controllers and can be controlled from USB1 only.
503 		 */
504 		usb1ctlr = (struct usb_ctlr *)
505 			((u32)config->reg & USB1_ADDR_MASK);
506 		clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
507 		udelay(25);
508 		clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
509 			     UTMIP_FORCE_PDTRK_POWERDOWN);
510 	}
511 	return 0;
512 }
513 
514 #ifdef CONFIG_USB_ULPI
515 /* if board file does not set a ULPI reference frequency we default to 24MHz */
516 #ifndef CONFIG_ULPI_REF_CLK
517 #define CONFIG_ULPI_REF_CLK 24000000
518 #endif
519 
520 /* set up the ULPI USB controller with the parameters provided */
521 static int init_ulpi_usb_controller(struct fdt_usb *config)
522 {
523 	u32 val;
524 	int loop_count;
525 	struct ulpi_viewport ulpi_vp;
526 	struct usb_ctlr *usbctlr = config->reg;
527 
528 	/* set up ULPI reference clock on pllp_out4 */
529 	clock_enable(PERIPH_ID_DEV2_OUT);
530 	clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
531 
532 	/* reset ULPI phy */
533 	if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
534 		fdtdec_setup_gpio(&config->phy_reset_gpio);
535 		gpio_direction_output(config->phy_reset_gpio.gpio, 0);
536 		mdelay(5);
537 		gpio_set_value(config->phy_reset_gpio.gpio, 1);
538 	}
539 
540 	/* Reset the usb controller */
541 	clock_enable(config->periph_id);
542 	usbf_reset_controller(config, usbctlr);
543 
544 	/* enable pinmux bypass */
545 	setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
546 			ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
547 
548 	/* Select ULPI parallel interface */
549 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
550 
551 	/* enable ULPI transceiver */
552 	setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
553 
554 	/* configure ULPI transceiver timings */
555 	val = 0;
556 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
557 
558 	val |= ULPI_DATA_TRIMMER_SEL(4);
559 	val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
560 	val |= ULPI_DIR_TRIMMER_SEL(4);
561 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
562 	udelay(10);
563 
564 	val |= ULPI_DATA_TRIMMER_LOAD;
565 	val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
566 	val |= ULPI_DIR_TRIMMER_LOAD;
567 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
568 
569 	/* set up phy for host operation with external vbus supply */
570 	ulpi_vp.port_num = 0;
571 	ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
572 
573 	if (ulpi_init(&ulpi_vp)) {
574 		printf("Tegra ULPI viewport init failed\n");
575 		return -1;
576 	}
577 
578 	ulpi_set_vbus(&ulpi_vp, 1, 1);
579 	ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
580 
581 	/* enable wakeup events */
582 	setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
583 
584 	/* Enable and wait for the phy clock to become valid in 100 ms */
585 	setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
586 	for (loop_count = 100000; loop_count != 0; loop_count--) {
587 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
588 			break;
589 		udelay(1);
590 	}
591 	if (!loop_count)
592 		return -1;
593 	clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
594 
595 	return 0;
596 }
597 #else
598 static int init_ulpi_usb_controller(struct fdt_usb *config)
599 {
600 	printf("No code to set up ULPI controller, please enable"
601 			"CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
602 	return -1;
603 }
604 #endif
605 
606 static void config_clock(const u32 timing[])
607 {
608 	clock_start_pll(CLOCK_ID_USB,
609 		timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
610 		timing[PARAM_CPCON], timing[PARAM_LFCON]);
611 }
612 
613 static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
614 {
615 	const char *phy, *mode;
616 
617 	config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
618 	mode = fdt_getprop(blob, node, "dr_mode", NULL);
619 	if (mode) {
620 		if (0 == strcmp(mode, "host"))
621 			config->dr_mode = DR_MODE_HOST;
622 		else if (0 == strcmp(mode, "peripheral"))
623 			config->dr_mode = DR_MODE_DEVICE;
624 		else if (0 == strcmp(mode, "otg"))
625 			config->dr_mode = DR_MODE_OTG;
626 		else {
627 			debug("%s: Cannot decode dr_mode '%s'\n", __func__,
628 			      mode);
629 			return -FDT_ERR_NOTFOUND;
630 		}
631 	} else {
632 		config->dr_mode = DR_MODE_HOST;
633 	}
634 
635 	phy = fdt_getprop(blob, node, "phy_type", NULL);
636 	config->utmi = phy && 0 == strcmp("utmi", phy);
637 	config->ulpi = phy && 0 == strcmp("ulpi", phy);
638 	config->enabled = fdtdec_get_is_enabled(blob, node);
639 	config->has_legacy_mode = fdtdec_get_bool(blob, node,
640 						  "nvidia,has-legacy-mode");
641 	if (config->has_legacy_mode)
642 		port_addr_clear_csc = (u32) config->reg;
643 	config->periph_id = clock_decode_periph_id(blob, node);
644 	if (config->periph_id == PERIPH_ID_NONE) {
645 		debug("%s: Missing/invalid peripheral ID\n", __func__);
646 		return -FDT_ERR_NOTFOUND;
647 	}
648 	fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
649 	fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
650 			&config->phy_reset_gpio);
651 	debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
652 		"vbus=%d, phy_reset=%d, dr_mode=%d\n",
653 		config->enabled, config->has_legacy_mode, config->utmi,
654 		config->ulpi, config->periph_id, config->vbus_gpio.gpio,
655 		config->phy_reset_gpio.gpio, config->dr_mode);
656 
657 	return 0;
658 }
659 
660 /*
661  * process_usb_nodes() - Process a list of USB nodes, adding them to our list
662  *			of USB ports.
663  * @blob:	fdt blob
664  * @node_list:	list of nodes to process (any <=0 are ignored)
665  * @count:	number of nodes to process
666  *
667  * Return:	0 - ok, -1 - error
668  */
669 static int process_usb_nodes(const void *blob, int node_list[], int count)
670 {
671 	struct fdt_usb config;
672 	int node, i;
673 	int clk_done = 0;
674 
675 	port_count = 0;
676 	for (i = 0; i < count; i++) {
677 		if (port_count == USB_PORTS_MAX) {
678 			printf("tegrausb: Cannot register more than %d ports\n",
679 				USB_PORTS_MAX);
680 			return -1;
681 		}
682 
683 		debug("USB %d: ", i);
684 		node = node_list[i];
685 		if (!node)
686 			continue;
687 		if (fdt_decode_usb(blob, node, &config)) {
688 			debug("Cannot decode USB node %s\n",
689 			      fdt_get_name(blob, node, NULL));
690 			return -1;
691 		}
692 		if (!clk_done) {
693 			config_clock(get_pll_timing());
694 			clk_done = 1;
695 		}
696 		config.initialized = 0;
697 
698 		/* add new USB port to the list of available ports */
699 		port[port_count++] = config;
700 	}
701 
702 	return 0;
703 }
704 
705 int usb_process_devicetree(const void *blob)
706 {
707 	int node_list[USB_PORTS_MAX];
708 	int count, err = 0;
709 	int i;
710 
711 	for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
712 		controller = &fdt_usb_controllers[i];
713 
714 		count = fdtdec_find_aliases_for_id(blob, "usb",
715 			controller->compat, node_list, USB_PORTS_MAX);
716 		if (count) {
717 			err = process_usb_nodes(blob, node_list, count);
718 			if (err)
719 				printf("%s: Error processing USB node!\n",
720 				       __func__);
721 			return err;
722 		}
723 	}
724 	if (i == ARRAY_SIZE(fdt_usb_controllers))
725 		controller = NULL;
726 
727 	return err;
728 }
729 
730 /**
731  * Start up the given port number (ports are numbered from 0 on each board).
732  * This returns values for the appropriate hccr and hcor addresses to use for
733  * USB EHCI operations.
734  *
735  * @param index	port number to start
736  * @param hccr		returns start address of EHCI HCCR registers
737  * @param hcor		returns start address of EHCI HCOR registers
738  * @return 0 if ok, -1 on error (generally invalid port number)
739  */
740 int ehci_hcd_init(int index, enum usb_init_type init,
741 		struct ehci_hccr **hccr, struct ehci_hcor **hcor)
742 {
743 	struct fdt_usb *config;
744 	struct usb_ctlr *usbctlr;
745 
746 	if (index >= port_count)
747 		return -1;
748 
749 	config = &port[index];
750 
751 	/* skip init, if the port is already initialized */
752 	if (config->initialized)
753 		goto success;
754 
755 	if (config->utmi && init_utmi_usb_controller(config)) {
756 		printf("tegrausb: Cannot init port %d\n", index);
757 		return -1;
758 	}
759 
760 	if (config->ulpi && init_ulpi_usb_controller(config)) {
761 		printf("tegrausb: Cannot init port %d\n", index);
762 		return -1;
763 	}
764 
765 	set_host_mode(config);
766 
767 	config->initialized = 1;
768 
769 success:
770 	usbctlr = config->reg;
771 	*hccr = (struct ehci_hccr *)&usbctlr->cap_length;
772 	*hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
773 
774 	if (controller->has_hostpc) {
775 		/* Set to Host mode after Controller Reset was done */
776 		clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
777 				USBMODE_CM_HC);
778 		/* Select UTMI parallel interface after setting host mode */
779 		if (config->utmi) {
780 			clrsetbits_le32((char *)&usbctlr->usb_cmd +
781 					HOSTPC1_DEVLC, PTS_MASK,
782 					PTS_UTMI << PTS_SHIFT);
783 			clrbits_le32((char *)&usbctlr->usb_cmd +
784 				     HOSTPC1_DEVLC, STS);
785 		}
786 	}
787 	return 0;
788 }
789 
790 /*
791  * Bring down the specified USB controller
792  */
793 int ehci_hcd_stop(int index)
794 {
795 	struct usb_ctlr *usbctlr;
796 
797 	usbctlr = port[index].reg;
798 
799 	/* Stop controller */
800 	writel(0, &usbctlr->usb_cmd);
801 	udelay(1000);
802 
803 	/* Initiate controller reset */
804 	writel(2, &usbctlr->usb_cmd);
805 	udelay(1000);
806 
807 	port[index].initialized = 0;
808 
809 	return 0;
810 }
811