xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-tegra.c (revision 7ae18f3725fe3e2033888cf431f71ac08aaa8e1c)
187f938c9SSimon Glass /*
2*7ae18f37SLucas Stach  * Copyright (c) 2011 The Chromium OS Authors.
38b3f7bf7SJim Lin  * Copyright (c) 2009-2012 NVIDIA Corporation
4*7ae18f37SLucas Stach  * Copyright (c) 2013 Lucas Stach
587f938c9SSimon Glass  *
687f938c9SSimon Glass  * See file CREDITS for list of people who contributed to this
787f938c9SSimon Glass  * project.
887f938c9SSimon Glass  *
987f938c9SSimon Glass  * This program is free software; you can redistribute it and/or
1087f938c9SSimon Glass  * modify it under the terms of the GNU General Public License as
1187f938c9SSimon Glass  * published by the Free Software Foundation; either version 2 of
1287f938c9SSimon Glass  * the License, or (at your option) any later version.
1387f938c9SSimon Glass  *
1487f938c9SSimon Glass  * This program is distributed in the hope that it will be useful,
1587f938c9SSimon Glass  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1687f938c9SSimon Glass  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1787f938c9SSimon Glass  * GNU General Public License for more details.
1887f938c9SSimon Glass  *
1987f938c9SSimon Glass  * You should have received a copy of the GNU General Public License
2087f938c9SSimon Glass  * along with this program; if not, write to the Free Software
2187f938c9SSimon Glass  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2287f938c9SSimon Glass  * MA 02111-1307 USA
2387f938c9SSimon Glass  */
2487f938c9SSimon Glass 
2587f938c9SSimon Glass #include <common.h>
26*7ae18f37SLucas Stach #include <asm/errno.h>
27*7ae18f37SLucas Stach #include <asm/io.h>
28*7ae18f37SLucas Stach #include <asm-generic/gpio.h>
29*7ae18f37SLucas Stach #include <asm/arch/clock.h>
30*7ae18f37SLucas Stach #include <asm/arch-tegra/usb.h>
3187f938c9SSimon Glass #include <usb.h>
32*7ae18f37SLucas Stach #include <usb/ulpi.h>
33*7ae18f37SLucas Stach #include <libfdt.h>
34*7ae18f37SLucas Stach #include <fdtdec.h>
3587f938c9SSimon Glass 
3687f938c9SSimon Glass #include "ehci.h"
3787f938c9SSimon Glass 
38*7ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
39*7ae18f37SLucas Stach 	#ifndef CONFIG_USB_ULPI_VIEWPORT
40*7ae18f37SLucas Stach 	#error	"To use CONFIG_USB_ULPI on Tegra Boards you have to also \
41*7ae18f37SLucas Stach 		define CONFIG_USB_ULPI_VIEWPORT"
42*7ae18f37SLucas Stach 	#endif
43*7ae18f37SLucas Stach #endif
44*7ae18f37SLucas Stach 
45*7ae18f37SLucas Stach enum {
46*7ae18f37SLucas Stach 	USB_PORTS_MAX	= 3,		/* Maximum ports we allow */
47*7ae18f37SLucas Stach };
48*7ae18f37SLucas Stach 
49*7ae18f37SLucas Stach /* Parameters we need for USB */
50*7ae18f37SLucas Stach enum {
51*7ae18f37SLucas Stach 	PARAM_DIVN,                     /* PLL FEEDBACK DIVIDer */
52*7ae18f37SLucas Stach 	PARAM_DIVM,                     /* PLL INPUT DIVIDER */
53*7ae18f37SLucas Stach 	PARAM_DIVP,                     /* POST DIVIDER (2^N) */
54*7ae18f37SLucas Stach 	PARAM_CPCON,                    /* BASE PLLC CHARGE Pump setup ctrl */
55*7ae18f37SLucas Stach 	PARAM_LFCON,                    /* BASE PLLC LOOP FILter setup ctrl */
56*7ae18f37SLucas Stach 	PARAM_ENABLE_DELAY_COUNT,       /* PLL-U Enable Delay Count */
57*7ae18f37SLucas Stach 	PARAM_STABLE_COUNT,             /* PLL-U STABLE count */
58*7ae18f37SLucas Stach 	PARAM_ACTIVE_DELAY_COUNT,       /* PLL-U Active delay count */
59*7ae18f37SLucas Stach 	PARAM_XTAL_FREQ_COUNT,          /* PLL-U XTAL frequency count */
60*7ae18f37SLucas Stach 	PARAM_DEBOUNCE_A_TIME,          /* 10MS DELAY for BIAS_DEBOUNCE_A */
61*7ae18f37SLucas Stach 	PARAM_BIAS_TIME,                /* 20US DELAY AFter bias cell op */
62*7ae18f37SLucas Stach 
63*7ae18f37SLucas Stach 	PARAM_COUNT
64*7ae18f37SLucas Stach };
65*7ae18f37SLucas Stach 
66*7ae18f37SLucas Stach /* Possible port types (dual role mode) */
67*7ae18f37SLucas Stach enum dr_mode {
68*7ae18f37SLucas Stach 	DR_MODE_NONE = 0,
69*7ae18f37SLucas Stach 	DR_MODE_HOST,		/* supports host operation */
70*7ae18f37SLucas Stach 	DR_MODE_DEVICE,		/* supports device operation */
71*7ae18f37SLucas Stach 	DR_MODE_OTG,		/* supports both */
72*7ae18f37SLucas Stach };
73*7ae18f37SLucas Stach 
74*7ae18f37SLucas Stach /* Information about a USB port */
75*7ae18f37SLucas Stach struct fdt_usb {
76*7ae18f37SLucas Stach 	struct usb_ctlr *reg;	/* address of registers in physical memory */
77*7ae18f37SLucas Stach 	unsigned utmi:1;	/* 1 if port has external tranceiver, else 0 */
78*7ae18f37SLucas Stach 	unsigned ulpi:1;	/* 1 if port has external ULPI transceiver */
79*7ae18f37SLucas Stach 	unsigned enabled:1;	/* 1 to enable, 0 to disable */
80*7ae18f37SLucas Stach 	unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
81*7ae18f37SLucas Stach 	unsigned initialized:1; /* has this port already been initialized? */
82*7ae18f37SLucas Stach 	enum dr_mode dr_mode;	/* dual role mode */
83*7ae18f37SLucas Stach 	enum periph_id periph_id;/* peripheral id */
84*7ae18f37SLucas Stach 	struct fdt_gpio_state vbus_gpio;	/* GPIO for vbus enable */
85*7ae18f37SLucas Stach 	struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
86*7ae18f37SLucas Stach };
87*7ae18f37SLucas Stach 
88*7ae18f37SLucas Stach static struct fdt_usb port[USB_PORTS_MAX];	/* List of valid USB ports */
89*7ae18f37SLucas Stach static unsigned port_count;			/* Number of available ports */
90*7ae18f37SLucas Stach 
91*7ae18f37SLucas Stach /*
92*7ae18f37SLucas Stach  * This table has USB timing parameters for each Oscillator frequency we
93*7ae18f37SLucas Stach  * support. There are four sets of values:
94*7ae18f37SLucas Stach  *
95*7ae18f37SLucas Stach  * 1. PLLU configuration information (reference clock is osc/clk_m and
96*7ae18f37SLucas Stach  * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
97*7ae18f37SLucas Stach  *
98*7ae18f37SLucas Stach  *  Reference frequency     13.0MHz      19.2MHz      12.0MHz      26.0MHz
99*7ae18f37SLucas Stach  *  ----------------------------------------------------------------------
100*7ae18f37SLucas Stach  *      DIVN                960 (0x3c0)  200 (0c8)    960 (3c0h)   960 (3c0)
101*7ae18f37SLucas Stach  *      DIVM                13 (0d)      4 (04)       12 (0c)      26 (1a)
102*7ae18f37SLucas Stach  * Filter frequency (MHz)   1            4.8          6            2
103*7ae18f37SLucas Stach  * CPCON                    1100b        0011b        1100b        1100b
104*7ae18f37SLucas Stach  * LFCON0                   0            0            0            0
105*7ae18f37SLucas Stach  *
106*7ae18f37SLucas Stach  * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
107*7ae18f37SLucas Stach  *
108*7ae18f37SLucas Stach  * Reference frequency     13.0MHz         19.2MHz         12.0MHz     26.0MHz
109*7ae18f37SLucas Stach  * ---------------------------------------------------------------------------
110*7ae18f37SLucas Stach  * PLLU_ENABLE_DLY_COUNT   02 (0x02)       03 (03)         02 (02)     04 (04)
111*7ae18f37SLucas Stach  * PLLU_STABLE_COUNT       51 (33)         75 (4B)         47 (2F)    102 (66)
112*7ae18f37SLucas Stach  * PLL_ACTIVE_DLY_COUNT    05 (05)         06 (06)         04 (04)     09 (09)
113*7ae18f37SLucas Stach  * XTAL_FREQ_COUNT        127 (7F)        187 (BB)        118 (76)    254 (FE)
114*7ae18f37SLucas Stach  *
115*7ae18f37SLucas Stach  * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
116*7ae18f37SLucas Stach  * SessEnd. Each of these signals have their own debouncer and for each of
117*7ae18f37SLucas Stach  * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
118*7ae18f37SLucas Stach  * BIAS_DEBOUNCE_B).
119*7ae18f37SLucas Stach  *
120*7ae18f37SLucas Stach  * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
121*7ae18f37SLucas Stach  *    0xffff -> No debouncing at all
122*7ae18f37SLucas Stach  *    <n> ms = <n> *1000 / (1/19.2MHz) / 4
123*7ae18f37SLucas Stach  *
124*7ae18f37SLucas Stach  * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
125*7ae18f37SLucas Stach  * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4  = 4800 = 0x12c0
126*7ae18f37SLucas Stach  *
127*7ae18f37SLucas Stach  * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
128*7ae18f37SLucas Stach  * values, so we can keep those to default.
129*7ae18f37SLucas Stach  *
130*7ae18f37SLucas Stach  * 4. The 20 microsecond delay after bias cell operation.
131*7ae18f37SLucas Stach  */
132*7ae18f37SLucas Stach static const unsigned usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
133*7ae18f37SLucas Stach 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
134*7ae18f37SLucas Stach 	{ 0x3C0, 0x0D, 0x00, 0xC,   0,  0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
135*7ae18f37SLucas Stach 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
136*7ae18f37SLucas Stach 	{ 0x3C0, 0x0C, 0x00, 0xC,   0,  0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
137*7ae18f37SLucas Stach 	{ 0x3C0, 0x1A, 0x00, 0xC,   0,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
138*7ae18f37SLucas Stach };
139*7ae18f37SLucas Stach 
140*7ae18f37SLucas Stach /* UTMIP Idle Wait Delay */
141*7ae18f37SLucas Stach static const u8 utmip_idle_wait_delay = 17;
142*7ae18f37SLucas Stach 
143*7ae18f37SLucas Stach /* UTMIP Elastic limit */
144*7ae18f37SLucas Stach static const u8 utmip_elastic_limit = 16;
145*7ae18f37SLucas Stach 
146*7ae18f37SLucas Stach /* UTMIP High Speed Sync Start Delay */
147*7ae18f37SLucas Stach static const u8 utmip_hs_sync_start_delay = 9;
14887f938c9SSimon Glass 
1498b3f7bf7SJim Lin /*
1508b3f7bf7SJim Lin  * A known hardware issue where Connect Status Change bit of PORTSC register
1518b3f7bf7SJim Lin  * of USB1 controller will be set after Port Reset.
1528b3f7bf7SJim Lin  * We have to clear it in order for later device enumeration to proceed.
1538b3f7bf7SJim Lin  * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
1548b3f7bf7SJim Lin  * in "ehci-hcd.c".
1558b3f7bf7SJim Lin  */
1568b3f7bf7SJim Lin void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
1578b3f7bf7SJim Lin {
1588b3f7bf7SJim Lin 	mdelay(50);
1598b3f7bf7SJim Lin 	if (((u32) status_reg & TEGRA_USB_ADDR_MASK) != TEGRA_USB1_BASE)
1608b3f7bf7SJim Lin 		return;
1618b3f7bf7SJim Lin 	/* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
1628b3f7bf7SJim Lin 	if (ehci_readl(status_reg) & EHCI_PS_CSC)
1638b3f7bf7SJim Lin 		*reg |= EHCI_PS_CSC;
1648b3f7bf7SJim Lin }
16587f938c9SSimon Glass 
166*7ae18f37SLucas Stach /* Put the port into host mode */
167*7ae18f37SLucas Stach static void set_host_mode(struct fdt_usb *config)
168*7ae18f37SLucas Stach {
169*7ae18f37SLucas Stach 	/*
170*7ae18f37SLucas Stach 	 * If we are an OTG port, check if remote host is driving VBus and
171*7ae18f37SLucas Stach 	 * bail out in this case.
172*7ae18f37SLucas Stach 	 */
173*7ae18f37SLucas Stach 	if (config->dr_mode == DR_MODE_OTG &&
174*7ae18f37SLucas Stach 		(readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
175*7ae18f37SLucas Stach 		return;
176*7ae18f37SLucas Stach 
177*7ae18f37SLucas Stach 	/*
178*7ae18f37SLucas Stach 	 * If not driving, we set the GPIO to enable VBUS. We assume
179*7ae18f37SLucas Stach 	 * that the pinmux is set up correctly for this.
180*7ae18f37SLucas Stach 	 */
181*7ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
182*7ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->vbus_gpio);
183*7ae18f37SLucas Stach 		gpio_direction_output(config->vbus_gpio.gpio,
184*7ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
185*7ae18f37SLucas Stach 				 0 : 1);
186*7ae18f37SLucas Stach 		debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
187*7ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
188*7ae18f37SLucas Stach 				"low" : "high");
189*7ae18f37SLucas Stach 	}
190*7ae18f37SLucas Stach }
191*7ae18f37SLucas Stach 
192*7ae18f37SLucas Stach void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
193*7ae18f37SLucas Stach {
194*7ae18f37SLucas Stach 	/* Reset the USB controller with 2us delay */
195*7ae18f37SLucas Stach 	reset_periph(config->periph_id, 2);
196*7ae18f37SLucas Stach 
197*7ae18f37SLucas Stach 	/*
198*7ae18f37SLucas Stach 	 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
199*7ae18f37SLucas Stach 	 * base address
200*7ae18f37SLucas Stach 	 */
201*7ae18f37SLucas Stach 	if (config->has_legacy_mode)
202*7ae18f37SLucas Stach 		setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
203*7ae18f37SLucas Stach 
204*7ae18f37SLucas Stach 	/* Put UTMIP1/3 in reset */
205*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
206*7ae18f37SLucas Stach 
207*7ae18f37SLucas Stach 	/* Enable the UTMIP PHY */
208*7ae18f37SLucas Stach 	if (config->utmi)
209*7ae18f37SLucas Stach 		setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
210*7ae18f37SLucas Stach }
211*7ae18f37SLucas Stach 
212*7ae18f37SLucas Stach /* set up the UTMI USB controller with the parameters provided */
213*7ae18f37SLucas Stach static int init_utmi_usb_controller(struct fdt_usb *config)
214*7ae18f37SLucas Stach {
215*7ae18f37SLucas Stach 	u32 val;
216*7ae18f37SLucas Stach 	int loop_count;
217*7ae18f37SLucas Stach 	const unsigned *timing;
218*7ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
219*7ae18f37SLucas Stach 
220*7ae18f37SLucas Stach 	clock_enable(config->periph_id);
221*7ae18f37SLucas Stach 
222*7ae18f37SLucas Stach 	/* Reset the usb controller */
223*7ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
224*7ae18f37SLucas Stach 
225*7ae18f37SLucas Stach 	/* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
226*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
227*7ae18f37SLucas Stach 
228*7ae18f37SLucas Stach 	/* Follow the crystal clock disable by >100ns delay */
229*7ae18f37SLucas Stach 	udelay(1);
230*7ae18f37SLucas Stach 
231*7ae18f37SLucas Stach 	/*
232*7ae18f37SLucas Stach 	 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
233*7ae18f37SLucas Stach 	 * mux must be switched to actually use a_sess_vld threshold.
234*7ae18f37SLucas Stach 	 */
235*7ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
236*7ae18f37SLucas Stach 		clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
237*7ae18f37SLucas Stach 			VBUS_SENSE_CTL_MASK,
238*7ae18f37SLucas Stach 			VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
239*7ae18f37SLucas Stach 	}
240*7ae18f37SLucas Stach 
241*7ae18f37SLucas Stach 	/*
242*7ae18f37SLucas Stach 	 * PLL Delay CONFIGURATION settings. The following parameters control
243*7ae18f37SLucas Stach 	 * the bring up of the plls.
244*7ae18f37SLucas Stach 	 */
245*7ae18f37SLucas Stach 	timing = usb_pll[clock_get_osc_freq()];
246*7ae18f37SLucas Stach 
247*7ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_misc_cfg1);
248*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
249*7ae18f37SLucas Stach 		timing[PARAM_STABLE_COUNT] << UTMIP_PLLU_STABLE_COUNT_SHIFT);
250*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
251*7ae18f37SLucas Stach 		timing[PARAM_ACTIVE_DELAY_COUNT] <<
252*7ae18f37SLucas Stach 			UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
253*7ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_misc_cfg1);
254*7ae18f37SLucas Stach 
255*7ae18f37SLucas Stach 	/* Set PLL enable delay count and crystal frequency count */
256*7ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_pll_cfg1);
257*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
258*7ae18f37SLucas Stach 		timing[PARAM_ENABLE_DELAY_COUNT] <<
259*7ae18f37SLucas Stach 			UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
260*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
261*7ae18f37SLucas Stach 		timing[PARAM_XTAL_FREQ_COUNT] <<
262*7ae18f37SLucas Stach 			UTMIP_XTAL_FREQ_COUNT_SHIFT);
263*7ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_pll_cfg1);
264*7ae18f37SLucas Stach 
265*7ae18f37SLucas Stach 	/* Setting the tracking length time */
266*7ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
267*7ae18f37SLucas Stach 		UTMIP_BIAS_PDTRK_COUNT_MASK,
268*7ae18f37SLucas Stach 		timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
269*7ae18f37SLucas Stach 
270*7ae18f37SLucas Stach 	/* Program debounce time for VBUS to become valid */
271*7ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
272*7ae18f37SLucas Stach 		UTMIP_DEBOUNCE_CFG0_MASK,
273*7ae18f37SLucas Stach 		timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
274*7ae18f37SLucas Stach 
275*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
276*7ae18f37SLucas Stach 
277*7ae18f37SLucas Stach 	/* Disable battery charge enabling bit */
278*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
279*7ae18f37SLucas Stach 
280*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
281*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
282*7ae18f37SLucas Stach 
283*7ae18f37SLucas Stach 	/*
284*7ae18f37SLucas Stach 	 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
285*7ae18f37SLucas Stach 	 * Setting these fields, together with default values of the
286*7ae18f37SLucas Stach 	 * other fields, results in programming the registers below as
287*7ae18f37SLucas Stach 	 * follows:
288*7ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG0 = 0x9168c000
289*7ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG1 = 0x13
290*7ae18f37SLucas Stach 	 */
291*7ae18f37SLucas Stach 
292*7ae18f37SLucas Stach 	/* Set PLL enable delay count and Crystal frequency count */
293*7ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_hsrx_cfg0);
294*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
295*7ae18f37SLucas Stach 		utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
296*7ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
297*7ae18f37SLucas Stach 		utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
298*7ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_hsrx_cfg0);
299*7ae18f37SLucas Stach 
300*7ae18f37SLucas Stach 	/* Configure the UTMIP_HS_SYNC_START_DLY */
301*7ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
302*7ae18f37SLucas Stach 		UTMIP_HS_SYNC_START_DLY_MASK,
303*7ae18f37SLucas Stach 		utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
304*7ae18f37SLucas Stach 
305*7ae18f37SLucas Stach 	/* Preceed the crystal clock disable by >100ns delay. */
306*7ae18f37SLucas Stach 	udelay(1);
307*7ae18f37SLucas Stach 
308*7ae18f37SLucas Stach 	/* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
309*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
310*7ae18f37SLucas Stach 
311*7ae18f37SLucas Stach 	/* Finished the per-controller init. */
312*7ae18f37SLucas Stach 
313*7ae18f37SLucas Stach 	/* De-assert UTMIP_RESET to bring out of reset. */
314*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
315*7ae18f37SLucas Stach 
316*7ae18f37SLucas Stach 	/* Wait for the phy clock to become valid in 100 ms */
317*7ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
318*7ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
319*7ae18f37SLucas Stach 			break;
320*7ae18f37SLucas Stach 		udelay(1);
321*7ae18f37SLucas Stach 	}
322*7ae18f37SLucas Stach 	if (!loop_count)
323*7ae18f37SLucas Stach 		return -1;
324*7ae18f37SLucas Stach 
325*7ae18f37SLucas Stach 	/* Disable ICUSB FS/LS transceiver */
326*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
327*7ae18f37SLucas Stach 
328*7ae18f37SLucas Stach 	/* Select UTMI parallel interface */
329*7ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
330*7ae18f37SLucas Stach 			PTS_UTMI << PTS_SHIFT);
331*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->port_sc1, STS);
332*7ae18f37SLucas Stach 
333*7ae18f37SLucas Stach 	/* Deassert power down state */
334*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
335*7ae18f37SLucas Stach 		UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
336*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
337*7ae18f37SLucas Stach 		UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
338*7ae18f37SLucas Stach 
339*7ae18f37SLucas Stach 	return 0;
340*7ae18f37SLucas Stach }
341*7ae18f37SLucas Stach 
342*7ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
343*7ae18f37SLucas Stach /* if board file does not set a ULPI reference frequency we default to 24MHz */
344*7ae18f37SLucas Stach #ifndef CONFIG_ULPI_REF_CLK
345*7ae18f37SLucas Stach #define CONFIG_ULPI_REF_CLK 24000000
346*7ae18f37SLucas Stach #endif
347*7ae18f37SLucas Stach 
348*7ae18f37SLucas Stach /* set up the ULPI USB controller with the parameters provided */
349*7ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
350*7ae18f37SLucas Stach {
351*7ae18f37SLucas Stach 	u32 val;
352*7ae18f37SLucas Stach 	int loop_count;
353*7ae18f37SLucas Stach 	struct ulpi_viewport ulpi_vp;
354*7ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
355*7ae18f37SLucas Stach 
356*7ae18f37SLucas Stach 	/* set up ULPI reference clock on pllp_out4 */
357*7ae18f37SLucas Stach 	clock_enable(PERIPH_ID_DEV2_OUT);
358*7ae18f37SLucas Stach 	clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
359*7ae18f37SLucas Stach 
360*7ae18f37SLucas Stach 	/* reset ULPI phy */
361*7ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
362*7ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->phy_reset_gpio);
363*7ae18f37SLucas Stach 		gpio_direction_output(config->phy_reset_gpio.gpio, 0);
364*7ae18f37SLucas Stach 		mdelay(5);
365*7ae18f37SLucas Stach 		gpio_set_value(config->phy_reset_gpio.gpio, 1);
366*7ae18f37SLucas Stach 	}
367*7ae18f37SLucas Stach 
368*7ae18f37SLucas Stach 	/* Reset the usb controller */
369*7ae18f37SLucas Stach 	clock_enable(config->periph_id);
370*7ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
371*7ae18f37SLucas Stach 
372*7ae18f37SLucas Stach 	/* enable pinmux bypass */
373*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
374*7ae18f37SLucas Stach 			ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
375*7ae18f37SLucas Stach 
376*7ae18f37SLucas Stach 	/* Select ULPI parallel interface */
377*7ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
378*7ae18f37SLucas Stach 
379*7ae18f37SLucas Stach 	/* enable ULPI transceiver */
380*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
381*7ae18f37SLucas Stach 
382*7ae18f37SLucas Stach 	/* configure ULPI transceiver timings */
383*7ae18f37SLucas Stach 	val = 0;
384*7ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
385*7ae18f37SLucas Stach 
386*7ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_SEL(4);
387*7ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
388*7ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_SEL(4);
389*7ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
390*7ae18f37SLucas Stach 	udelay(10);
391*7ae18f37SLucas Stach 
392*7ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_LOAD;
393*7ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
394*7ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_LOAD;
395*7ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
396*7ae18f37SLucas Stach 
397*7ae18f37SLucas Stach 	/* set up phy for host operation with external vbus supply */
398*7ae18f37SLucas Stach 	ulpi_vp.port_num = 0;
399*7ae18f37SLucas Stach 	ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
400*7ae18f37SLucas Stach 
401*7ae18f37SLucas Stach 	if (ulpi_init(&ulpi_vp)) {
402*7ae18f37SLucas Stach 		printf("Tegra ULPI viewport init failed\n");
403*7ae18f37SLucas Stach 		return -1;
404*7ae18f37SLucas Stach 	}
405*7ae18f37SLucas Stach 
406*7ae18f37SLucas Stach 	ulpi_set_vbus(&ulpi_vp, 1, 1);
407*7ae18f37SLucas Stach 	ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
408*7ae18f37SLucas Stach 
409*7ae18f37SLucas Stach 	/* enable wakeup events */
410*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
411*7ae18f37SLucas Stach 
412*7ae18f37SLucas Stach 	/* Enable and wait for the phy clock to become valid in 100 ms */
413*7ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
414*7ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
415*7ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
416*7ae18f37SLucas Stach 			break;
417*7ae18f37SLucas Stach 		udelay(1);
418*7ae18f37SLucas Stach 	}
419*7ae18f37SLucas Stach 	if (!loop_count)
420*7ae18f37SLucas Stach 		return -1;
421*7ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
422*7ae18f37SLucas Stach 
423*7ae18f37SLucas Stach 	return 0;
424*7ae18f37SLucas Stach }
425*7ae18f37SLucas Stach #else
426*7ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
427*7ae18f37SLucas Stach {
428*7ae18f37SLucas Stach 	printf("No code to set up ULPI controller, please enable"
429*7ae18f37SLucas Stach 			"CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
430*7ae18f37SLucas Stach 	return -1;
431*7ae18f37SLucas Stach }
432*7ae18f37SLucas Stach #endif
433*7ae18f37SLucas Stach 
434*7ae18f37SLucas Stach static void config_clock(const u32 timing[])
435*7ae18f37SLucas Stach {
436*7ae18f37SLucas Stach 	clock_start_pll(CLOCK_ID_USB,
437*7ae18f37SLucas Stach 		timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
438*7ae18f37SLucas Stach 		timing[PARAM_CPCON], timing[PARAM_LFCON]);
439*7ae18f37SLucas Stach }
440*7ae18f37SLucas Stach 
441*7ae18f37SLucas Stach int tegrausb_start_port(int portnum, u32 *hccr, u32 *hcor)
442*7ae18f37SLucas Stach {
443*7ae18f37SLucas Stach 	struct fdt_usb *config;
444*7ae18f37SLucas Stach 	struct usb_ctlr *usbctlr;
445*7ae18f37SLucas Stach 
446*7ae18f37SLucas Stach 	if (portnum >= port_count)
447*7ae18f37SLucas Stach 		return -1;
448*7ae18f37SLucas Stach 
449*7ae18f37SLucas Stach 	config = &port[portnum];
450*7ae18f37SLucas Stach 
451*7ae18f37SLucas Stach 	/* skip init, if the port is already initialized */
452*7ae18f37SLucas Stach 	if (config->initialized)
453*7ae18f37SLucas Stach 		goto success;
454*7ae18f37SLucas Stach 
455*7ae18f37SLucas Stach 	if (config->utmi && init_utmi_usb_controller(config)) {
456*7ae18f37SLucas Stach 		printf("tegrausb: Cannot init port %d\n", portnum);
457*7ae18f37SLucas Stach 		return -1;
458*7ae18f37SLucas Stach 	}
459*7ae18f37SLucas Stach 
460*7ae18f37SLucas Stach 	if (config->ulpi && init_ulpi_usb_controller(config)) {
461*7ae18f37SLucas Stach 		printf("tegrausb: Cannot init port %d\n", portnum);
462*7ae18f37SLucas Stach 		return -1;
463*7ae18f37SLucas Stach 	}
464*7ae18f37SLucas Stach 
465*7ae18f37SLucas Stach 	set_host_mode(config);
466*7ae18f37SLucas Stach 
467*7ae18f37SLucas Stach 	config->initialized = 1;
468*7ae18f37SLucas Stach 
469*7ae18f37SLucas Stach success:
470*7ae18f37SLucas Stach 	usbctlr = config->reg;
471*7ae18f37SLucas Stach 	*hccr = (u32)&usbctlr->cap_length;
472*7ae18f37SLucas Stach 	*hcor = (u32)&usbctlr->usb_cmd;
473*7ae18f37SLucas Stach 	return 0;
474*7ae18f37SLucas Stach }
475*7ae18f37SLucas Stach 
476*7ae18f37SLucas Stach int tegrausb_stop_port(int portnum)
477*7ae18f37SLucas Stach {
478*7ae18f37SLucas Stach 	struct usb_ctlr *usbctlr;
479*7ae18f37SLucas Stach 
480*7ae18f37SLucas Stach 	usbctlr = port[portnum].reg;
481*7ae18f37SLucas Stach 
482*7ae18f37SLucas Stach 	/* Stop controller */
483*7ae18f37SLucas Stach 	writel(0, &usbctlr->usb_cmd);
484*7ae18f37SLucas Stach 	udelay(1000);
485*7ae18f37SLucas Stach 
486*7ae18f37SLucas Stach 	/* Initiate controller reset */
487*7ae18f37SLucas Stach 	writel(2, &usbctlr->usb_cmd);
488*7ae18f37SLucas Stach 	udelay(1000);
489*7ae18f37SLucas Stach 
490*7ae18f37SLucas Stach 	port[portnum].initialized = 0;
491*7ae18f37SLucas Stach 
492*7ae18f37SLucas Stach 	return 0;
493*7ae18f37SLucas Stach }
494*7ae18f37SLucas Stach 
495*7ae18f37SLucas Stach int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
496*7ae18f37SLucas Stach {
497*7ae18f37SLucas Stach 	const char *phy, *mode;
498*7ae18f37SLucas Stach 
499*7ae18f37SLucas Stach 	config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
500*7ae18f37SLucas Stach 	mode = fdt_getprop(blob, node, "dr_mode", NULL);
501*7ae18f37SLucas Stach 	if (mode) {
502*7ae18f37SLucas Stach 		if (0 == strcmp(mode, "host"))
503*7ae18f37SLucas Stach 			config->dr_mode = DR_MODE_HOST;
504*7ae18f37SLucas Stach 		else if (0 == strcmp(mode, "peripheral"))
505*7ae18f37SLucas Stach 			config->dr_mode = DR_MODE_DEVICE;
506*7ae18f37SLucas Stach 		else if (0 == strcmp(mode, "otg"))
507*7ae18f37SLucas Stach 			config->dr_mode = DR_MODE_OTG;
508*7ae18f37SLucas Stach 		else {
509*7ae18f37SLucas Stach 			debug("%s: Cannot decode dr_mode '%s'\n", __func__,
510*7ae18f37SLucas Stach 			      mode);
511*7ae18f37SLucas Stach 			return -FDT_ERR_NOTFOUND;
512*7ae18f37SLucas Stach 		}
513*7ae18f37SLucas Stach 	} else {
514*7ae18f37SLucas Stach 		config->dr_mode = DR_MODE_HOST;
515*7ae18f37SLucas Stach 	}
516*7ae18f37SLucas Stach 
517*7ae18f37SLucas Stach 	phy = fdt_getprop(blob, node, "phy_type", NULL);
518*7ae18f37SLucas Stach 	config->utmi = phy && 0 == strcmp("utmi", phy);
519*7ae18f37SLucas Stach 	config->ulpi = phy && 0 == strcmp("ulpi", phy);
520*7ae18f37SLucas Stach 	config->enabled = fdtdec_get_is_enabled(blob, node);
521*7ae18f37SLucas Stach 	config->has_legacy_mode = fdtdec_get_bool(blob, node,
522*7ae18f37SLucas Stach 						  "nvidia,has-legacy-mode");
523*7ae18f37SLucas Stach 	config->periph_id = clock_decode_periph_id(blob, node);
524*7ae18f37SLucas Stach 	if (config->periph_id == PERIPH_ID_NONE) {
525*7ae18f37SLucas Stach 		debug("%s: Missing/invalid peripheral ID\n", __func__);
526*7ae18f37SLucas Stach 		return -FDT_ERR_NOTFOUND;
527*7ae18f37SLucas Stach 	}
528*7ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
529*7ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
530*7ae18f37SLucas Stach 			&config->phy_reset_gpio);
531*7ae18f37SLucas Stach 	debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
532*7ae18f37SLucas Stach 		"vbus=%d, phy_reset=%d, dr_mode=%d\n",
533*7ae18f37SLucas Stach 		config->enabled, config->has_legacy_mode, config->utmi,
534*7ae18f37SLucas Stach 		config->ulpi, config->periph_id, config->vbus_gpio.gpio,
535*7ae18f37SLucas Stach 		config->phy_reset_gpio.gpio, config->dr_mode);
536*7ae18f37SLucas Stach 
537*7ae18f37SLucas Stach 	return 0;
538*7ae18f37SLucas Stach }
539*7ae18f37SLucas Stach 
540*7ae18f37SLucas Stach int board_usb_init(const void *blob)
541*7ae18f37SLucas Stach {
542*7ae18f37SLucas Stach 	struct fdt_usb config;
543*7ae18f37SLucas Stach 	enum clock_osc_freq freq;
544*7ae18f37SLucas Stach 	int node_list[USB_PORTS_MAX];
545*7ae18f37SLucas Stach 	int node, count, i;
546*7ae18f37SLucas Stach 
547*7ae18f37SLucas Stach 	/* Set up the USB clocks correctly based on our oscillator frequency */
548*7ae18f37SLucas Stach 	freq = clock_get_osc_freq();
549*7ae18f37SLucas Stach 	config_clock(usb_pll[freq]);
550*7ae18f37SLucas Stach 
551*7ae18f37SLucas Stach 	/* count may return <0 on error */
552*7ae18f37SLucas Stach 	count = fdtdec_find_aliases_for_id(blob, "usb",
553*7ae18f37SLucas Stach 			COMPAT_NVIDIA_TEGRA20_USB, node_list, USB_PORTS_MAX);
554*7ae18f37SLucas Stach 	for (i = 0; i < count; i++) {
555*7ae18f37SLucas Stach 		if (port_count == USB_PORTS_MAX) {
556*7ae18f37SLucas Stach 			printf("tegrausb: Cannot register more than %d ports\n",
557*7ae18f37SLucas Stach 				USB_PORTS_MAX);
558*7ae18f37SLucas Stach 			return -1;
559*7ae18f37SLucas Stach 		}
560*7ae18f37SLucas Stach 
561*7ae18f37SLucas Stach 		debug("USB %d: ", i);
562*7ae18f37SLucas Stach 		node = node_list[i];
563*7ae18f37SLucas Stach 		if (!node)
564*7ae18f37SLucas Stach 			continue;
565*7ae18f37SLucas Stach 		if (fdt_decode_usb(blob, node, &config)) {
566*7ae18f37SLucas Stach 			debug("Cannot decode USB node %s\n",
567*7ae18f37SLucas Stach 			      fdt_get_name(blob, node, NULL));
568*7ae18f37SLucas Stach 			return -1;
569*7ae18f37SLucas Stach 		}
570*7ae18f37SLucas Stach 		config.initialized = 0;
571*7ae18f37SLucas Stach 
572*7ae18f37SLucas Stach 		/* add new USB port to the list of available ports */
573*7ae18f37SLucas Stach 		port[port_count++] = config;
574*7ae18f37SLucas Stach 	}
575*7ae18f37SLucas Stach 
576*7ae18f37SLucas Stach 	return 0;
577*7ae18f37SLucas Stach }
578*7ae18f37SLucas Stach 
57987f938c9SSimon Glass /*
58087f938c9SSimon Glass  * Create the appropriate control structures to manage
58187f938c9SSimon Glass  * a new EHCI host controller.
58287f938c9SSimon Glass  */
583676ae068SLucas Stach int ehci_hcd_init(int index, struct ehci_hccr **hccr, struct ehci_hcor **hcor)
58487f938c9SSimon Glass {
58587f938c9SSimon Glass 	u32 our_hccr, our_hcor;
58687f938c9SSimon Glass 
58787f938c9SSimon Glass 	/*
58887f938c9SSimon Glass 	 * Select the first port, as we don't have a way of selecting others
58987f938c9SSimon Glass 	 * yet
59087f938c9SSimon Glass 	 */
591a896211fSLucas Stach 	if (tegrausb_start_port(index, &our_hccr, &our_hcor))
59287f938c9SSimon Glass 		return -1;
59387f938c9SSimon Glass 
594676ae068SLucas Stach 	*hccr = (struct ehci_hccr *)our_hccr;
595676ae068SLucas Stach 	*hcor = (struct ehci_hcor *)our_hcor;
59687f938c9SSimon Glass 
59787f938c9SSimon Glass 	return 0;
59887f938c9SSimon Glass }
59987f938c9SSimon Glass 
60087f938c9SSimon Glass /*
60187f938c9SSimon Glass  * Destroy the appropriate control structures corresponding
60287f938c9SSimon Glass  * the the EHCI host controller.
60387f938c9SSimon Glass  */
604676ae068SLucas Stach int ehci_hcd_stop(int index)
60587f938c9SSimon Glass {
606a896211fSLucas Stach 	return tegrausb_stop_port(index);
60787f938c9SSimon Glass }
608