xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-tegra.c (revision 7e44d9320ed4a9994b97eb1c9b2efd04491ff431)
187f938c9SSimon Glass /*
27ae18f37SLucas Stach  * Copyright (c) 2011 The Chromium OS Authors.
3*7e44d932SJim Lin  * Copyright (c) 2009-2013 NVIDIA Corporation
47ae18f37SLucas 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>
267ae18f37SLucas Stach #include <asm/errno.h>
277ae18f37SLucas Stach #include <asm/io.h>
287ae18f37SLucas Stach #include <asm-generic/gpio.h>
297ae18f37SLucas Stach #include <asm/arch/clock.h>
307ae18f37SLucas Stach #include <asm/arch-tegra/usb.h>
31*7e44d932SJim Lin #include <asm/arch-tegra/clk_rst.h>
32*7e44d932SJim Lin #include <asm/arch/usb.h>
3387f938c9SSimon Glass #include <usb.h>
347ae18f37SLucas Stach #include <usb/ulpi.h>
357ae18f37SLucas Stach #include <libfdt.h>
367ae18f37SLucas Stach #include <fdtdec.h>
3787f938c9SSimon Glass 
3887f938c9SSimon Glass #include "ehci.h"
3987f938c9SSimon Glass 
40*7e44d932SJim Lin #define USB1_ADDR_MASK	0xFFFF0000
41*7e44d932SJim Lin 
42*7e44d932SJim Lin #define HOSTPC1_DEVLC	0x84
43*7e44d932SJim Lin #define HOSTPC1_PSPD(x)		(((x) >> 25) & 0x3)
44*7e44d932SJim Lin 
457ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
467ae18f37SLucas Stach 	#ifndef CONFIG_USB_ULPI_VIEWPORT
477ae18f37SLucas Stach 	#error	"To use CONFIG_USB_ULPI on Tegra Boards you have to also \
487ae18f37SLucas Stach 		define CONFIG_USB_ULPI_VIEWPORT"
497ae18f37SLucas Stach 	#endif
507ae18f37SLucas Stach #endif
517ae18f37SLucas Stach 
527ae18f37SLucas Stach enum {
537ae18f37SLucas Stach 	USB_PORTS_MAX	= 3,		/* Maximum ports we allow */
547ae18f37SLucas Stach };
557ae18f37SLucas Stach 
567ae18f37SLucas Stach /* Parameters we need for USB */
577ae18f37SLucas Stach enum {
587ae18f37SLucas Stach 	PARAM_DIVN,                     /* PLL FEEDBACK DIVIDer */
597ae18f37SLucas Stach 	PARAM_DIVM,                     /* PLL INPUT DIVIDER */
607ae18f37SLucas Stach 	PARAM_DIVP,                     /* POST DIVIDER (2^N) */
617ae18f37SLucas Stach 	PARAM_CPCON,                    /* BASE PLLC CHARGE Pump setup ctrl */
627ae18f37SLucas Stach 	PARAM_LFCON,                    /* BASE PLLC LOOP FILter setup ctrl */
637ae18f37SLucas Stach 	PARAM_ENABLE_DELAY_COUNT,       /* PLL-U Enable Delay Count */
647ae18f37SLucas Stach 	PARAM_STABLE_COUNT,             /* PLL-U STABLE count */
657ae18f37SLucas Stach 	PARAM_ACTIVE_DELAY_COUNT,       /* PLL-U Active delay count */
667ae18f37SLucas Stach 	PARAM_XTAL_FREQ_COUNT,          /* PLL-U XTAL frequency count */
677ae18f37SLucas Stach 	PARAM_DEBOUNCE_A_TIME,          /* 10MS DELAY for BIAS_DEBOUNCE_A */
687ae18f37SLucas Stach 	PARAM_BIAS_TIME,                /* 20US DELAY AFter bias cell op */
697ae18f37SLucas Stach 
707ae18f37SLucas Stach 	PARAM_COUNT
717ae18f37SLucas Stach };
727ae18f37SLucas Stach 
737ae18f37SLucas Stach /* Possible port types (dual role mode) */
747ae18f37SLucas Stach enum dr_mode {
757ae18f37SLucas Stach 	DR_MODE_NONE = 0,
767ae18f37SLucas Stach 	DR_MODE_HOST,		/* supports host operation */
777ae18f37SLucas Stach 	DR_MODE_DEVICE,		/* supports device operation */
787ae18f37SLucas Stach 	DR_MODE_OTG,		/* supports both */
797ae18f37SLucas Stach };
807ae18f37SLucas Stach 
817ae18f37SLucas Stach /* Information about a USB port */
827ae18f37SLucas Stach struct fdt_usb {
837ae18f37SLucas Stach 	struct usb_ctlr *reg;	/* address of registers in physical memory */
847ae18f37SLucas Stach 	unsigned utmi:1;	/* 1 if port has external tranceiver, else 0 */
857ae18f37SLucas Stach 	unsigned ulpi:1;	/* 1 if port has external ULPI transceiver */
867ae18f37SLucas Stach 	unsigned enabled:1;	/* 1 to enable, 0 to disable */
877ae18f37SLucas Stach 	unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
887ae18f37SLucas Stach 	unsigned initialized:1; /* has this port already been initialized? */
897ae18f37SLucas Stach 	enum dr_mode dr_mode;	/* dual role mode */
907ae18f37SLucas Stach 	enum periph_id periph_id;/* peripheral id */
917ae18f37SLucas Stach 	struct fdt_gpio_state vbus_gpio;	/* GPIO for vbus enable */
927ae18f37SLucas Stach 	struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
937ae18f37SLucas Stach };
947ae18f37SLucas Stach 
957ae18f37SLucas Stach static struct fdt_usb port[USB_PORTS_MAX];	/* List of valid USB ports */
967ae18f37SLucas Stach static unsigned port_count;			/* Number of available ports */
97*7e44d932SJim Lin /* Port that needs to clear CSC after Port Reset */
98*7e44d932SJim Lin static u32 port_addr_clear_csc;
997ae18f37SLucas Stach 
1007ae18f37SLucas Stach /*
1017ae18f37SLucas Stach  * This table has USB timing parameters for each Oscillator frequency we
1027ae18f37SLucas Stach  * support. There are four sets of values:
1037ae18f37SLucas Stach  *
1047ae18f37SLucas Stach  * 1. PLLU configuration information (reference clock is osc/clk_m and
1057ae18f37SLucas Stach  * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
1067ae18f37SLucas Stach  *
1077ae18f37SLucas Stach  *  Reference frequency     13.0MHz      19.2MHz      12.0MHz      26.0MHz
1087ae18f37SLucas Stach  *  ----------------------------------------------------------------------
1097ae18f37SLucas Stach  *      DIVN                960 (0x3c0)  200 (0c8)    960 (3c0h)   960 (3c0)
1107ae18f37SLucas Stach  *      DIVM                13 (0d)      4 (04)       12 (0c)      26 (1a)
1117ae18f37SLucas Stach  * Filter frequency (MHz)   1            4.8          6            2
1127ae18f37SLucas Stach  * CPCON                    1100b        0011b        1100b        1100b
1137ae18f37SLucas Stach  * LFCON0                   0            0            0            0
1147ae18f37SLucas Stach  *
1157ae18f37SLucas Stach  * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
1167ae18f37SLucas Stach  *
1177ae18f37SLucas Stach  * Reference frequency     13.0MHz         19.2MHz         12.0MHz     26.0MHz
1187ae18f37SLucas Stach  * ---------------------------------------------------------------------------
1197ae18f37SLucas Stach  * PLLU_ENABLE_DLY_COUNT   02 (0x02)       03 (03)         02 (02)     04 (04)
1207ae18f37SLucas Stach  * PLLU_STABLE_COUNT       51 (33)         75 (4B)         47 (2F)    102 (66)
1217ae18f37SLucas Stach  * PLL_ACTIVE_DLY_COUNT    05 (05)         06 (06)         04 (04)     09 (09)
1227ae18f37SLucas Stach  * XTAL_FREQ_COUNT        127 (7F)        187 (BB)        118 (76)    254 (FE)
1237ae18f37SLucas Stach  *
1247ae18f37SLucas Stach  * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
1257ae18f37SLucas Stach  * SessEnd. Each of these signals have their own debouncer and for each of
1267ae18f37SLucas Stach  * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
1277ae18f37SLucas Stach  * BIAS_DEBOUNCE_B).
1287ae18f37SLucas Stach  *
1297ae18f37SLucas Stach  * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
1307ae18f37SLucas Stach  *    0xffff -> No debouncing at all
1317ae18f37SLucas Stach  *    <n> ms = <n> *1000 / (1/19.2MHz) / 4
1327ae18f37SLucas Stach  *
1337ae18f37SLucas Stach  * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
1347ae18f37SLucas Stach  * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4  = 4800 = 0x12c0
1357ae18f37SLucas Stach  *
1367ae18f37SLucas Stach  * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
1377ae18f37SLucas Stach  * values, so we can keep those to default.
1387ae18f37SLucas Stach  *
1397ae18f37SLucas Stach  * 4. The 20 microsecond delay after bias cell operation.
1407ae18f37SLucas Stach  */
141*7e44d932SJim Lin static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
1427ae18f37SLucas Stach 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
1437ae18f37SLucas Stach 	{ 0x3C0, 0x0D, 0x00, 0xC,   0,  0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
1447ae18f37SLucas Stach 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
1457ae18f37SLucas Stach 	{ 0x3C0, 0x0C, 0x00, 0xC,   0,  0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
1467ae18f37SLucas Stach 	{ 0x3C0, 0x1A, 0x00, 0xC,   0,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
1477ae18f37SLucas Stach };
1487ae18f37SLucas Stach 
149*7e44d932SJim Lin static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
150*7e44d932SJim Lin 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
151*7e44d932SJim Lin 	{ 0x3C0, 0x0D, 0x00, 0xC,   1,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
152*7e44d932SJim Lin 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
153*7e44d932SJim Lin 	{ 0x3C0, 0x0C, 0x00, 0xC,   1,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
154*7e44d932SJim Lin 	{ 0x3C0, 0x1A, 0x00, 0xC,   1,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
155*7e44d932SJim Lin };
156*7e44d932SJim Lin 
157*7e44d932SJim Lin static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
158*7e44d932SJim Lin 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
159*7e44d932SJim Lin 	{ 0x3C0, 0x0D, 0x00, 0xC,   2,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
160*7e44d932SJim Lin 	{ 0x0C8, 0x04, 0x00, 0x3,   2,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
161*7e44d932SJim Lin 	{ 0x3C0, 0x0C, 0x00, 0xC,   2,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
162*7e44d932SJim Lin 	{ 0x3C0, 0x1A, 0x00, 0xC,   2,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
163*7e44d932SJim Lin };
164*7e44d932SJim Lin 
1657ae18f37SLucas Stach /* UTMIP Idle Wait Delay */
1667ae18f37SLucas Stach static const u8 utmip_idle_wait_delay = 17;
1677ae18f37SLucas Stach 
1687ae18f37SLucas Stach /* UTMIP Elastic limit */
1697ae18f37SLucas Stach static const u8 utmip_elastic_limit = 16;
1707ae18f37SLucas Stach 
1717ae18f37SLucas Stach /* UTMIP High Speed Sync Start Delay */
1727ae18f37SLucas Stach static const u8 utmip_hs_sync_start_delay = 9;
17387f938c9SSimon Glass 
174*7e44d932SJim Lin struct fdt_usb_controller {
175*7e44d932SJim Lin 	int compat;
176*7e44d932SJim Lin 	/* flag to determine whether controller supports hostpc register */
177*7e44d932SJim Lin 	u32 has_hostpc:1;
178*7e44d932SJim Lin 	const unsigned *pll_parameter;
179*7e44d932SJim Lin };
180*7e44d932SJim Lin 
181*7e44d932SJim Lin static struct fdt_usb_controller fdt_usb_controllers[] = {
182*7e44d932SJim Lin 	{
183*7e44d932SJim Lin 		.compat		= COMPAT_NVIDIA_TEGRA20_USB,
184*7e44d932SJim Lin 		.has_hostpc	= 0,
185*7e44d932SJim Lin 		.pll_parameter	= (const unsigned *)T20_usb_pll,
186*7e44d932SJim Lin 	},
187*7e44d932SJim Lin 	{
188*7e44d932SJim Lin 		.compat		= COMPAT_NVIDIA_TEGRA30_USB,
189*7e44d932SJim Lin 		.has_hostpc	= 1,
190*7e44d932SJim Lin 		.pll_parameter	= (const unsigned *)T30_usb_pll,
191*7e44d932SJim Lin 	},
192*7e44d932SJim Lin 	{
193*7e44d932SJim Lin 		.compat		= COMPAT_NVIDIA_TEGRA114_USB,
194*7e44d932SJim Lin 		.has_hostpc	= 1,
195*7e44d932SJim Lin 		.pll_parameter	= (const unsigned *)T114_usb_pll,
196*7e44d932SJim Lin 	},
197*7e44d932SJim Lin };
198*7e44d932SJim Lin 
199*7e44d932SJim Lin static struct fdt_usb_controller *controller;
200*7e44d932SJim Lin 
2018b3f7bf7SJim Lin /*
2028b3f7bf7SJim Lin  * A known hardware issue where Connect Status Change bit of PORTSC register
2038b3f7bf7SJim Lin  * of USB1 controller will be set after Port Reset.
2048b3f7bf7SJim Lin  * We have to clear it in order for later device enumeration to proceed.
2058b3f7bf7SJim Lin  * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
2068b3f7bf7SJim Lin  * in "ehci-hcd.c".
2078b3f7bf7SJim Lin  */
2088b3f7bf7SJim Lin void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
2098b3f7bf7SJim Lin {
2108b3f7bf7SJim Lin 	mdelay(50);
211*7e44d932SJim Lin 	/* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
212*7e44d932SJim Lin 	if (controller->has_hostpc)
213*7e44d932SJim Lin 		*reg |= EHCI_PS_PE;
214*7e44d932SJim Lin 
215*7e44d932SJim Lin 	if (((u32)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
2168b3f7bf7SJim Lin 		return;
2178b3f7bf7SJim Lin 	/* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
2188b3f7bf7SJim Lin 	if (ehci_readl(status_reg) & EHCI_PS_CSC)
2198b3f7bf7SJim Lin 		*reg |= EHCI_PS_CSC;
2208b3f7bf7SJim Lin }
22187f938c9SSimon Glass 
222*7e44d932SJim Lin /*
223*7e44d932SJim Lin  * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
224*7e44d932SJim Lin  * in "ehci-hcd.c".
225*7e44d932SJim Lin  */
226*7e44d932SJim Lin void ehci_set_usbmode(int index)
227*7e44d932SJim Lin {
228*7e44d932SJim Lin 	struct fdt_usb *config;
229*7e44d932SJim Lin 	struct usb_ctlr *usbctlr;
230*7e44d932SJim Lin 	uint32_t tmp;
231*7e44d932SJim Lin 
232*7e44d932SJim Lin 	config = &port[index];
233*7e44d932SJim Lin 	usbctlr = config->reg;
234*7e44d932SJim Lin 
235*7e44d932SJim Lin 	tmp = ehci_readl(&usbctlr->usb_mode);
236*7e44d932SJim Lin 	tmp |= USBMODE_CM_HC;
237*7e44d932SJim Lin 	ehci_writel(&usbctlr->usb_mode, tmp);
238*7e44d932SJim Lin }
239*7e44d932SJim Lin 
240*7e44d932SJim Lin /*
241*7e44d932SJim Lin  * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
242*7e44d932SJim Lin  * in "ehci-hcd.c".
243*7e44d932SJim Lin  */
244*7e44d932SJim Lin int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
245*7e44d932SJim Lin {
246*7e44d932SJim Lin 	uint32_t tmp;
247*7e44d932SJim Lin 	uint32_t *reg_ptr;
248*7e44d932SJim Lin 
249*7e44d932SJim Lin 	if (controller->has_hostpc) {
250*7e44d932SJim Lin 		reg_ptr = (uint32_t *)((u8 *)&hcor->or_usbcmd + HOSTPC1_DEVLC);
251*7e44d932SJim Lin 		tmp = ehci_readl(reg_ptr);
252*7e44d932SJim Lin 		return HOSTPC1_PSPD(tmp);
253*7e44d932SJim Lin 	} else
254*7e44d932SJim Lin 		return PORTSC_PSPD(reg);
255*7e44d932SJim Lin }
256*7e44d932SJim Lin 
2577ae18f37SLucas Stach /* Put the port into host mode */
2587ae18f37SLucas Stach static void set_host_mode(struct fdt_usb *config)
2597ae18f37SLucas Stach {
2607ae18f37SLucas Stach 	/*
2617ae18f37SLucas Stach 	 * If we are an OTG port, check if remote host is driving VBus and
2627ae18f37SLucas Stach 	 * bail out in this case.
2637ae18f37SLucas Stach 	 */
2647ae18f37SLucas Stach 	if (config->dr_mode == DR_MODE_OTG &&
2657ae18f37SLucas Stach 		(readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
2667ae18f37SLucas Stach 		return;
2677ae18f37SLucas Stach 
2687ae18f37SLucas Stach 	/*
2697ae18f37SLucas Stach 	 * If not driving, we set the GPIO to enable VBUS. We assume
2707ae18f37SLucas Stach 	 * that the pinmux is set up correctly for this.
2717ae18f37SLucas Stach 	 */
2727ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
2737ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->vbus_gpio);
2747ae18f37SLucas Stach 		gpio_direction_output(config->vbus_gpio.gpio,
2757ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
2767ae18f37SLucas Stach 				 0 : 1);
2777ae18f37SLucas Stach 		debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
2787ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
2797ae18f37SLucas Stach 				"low" : "high");
2807ae18f37SLucas Stach 	}
2817ae18f37SLucas Stach }
2827ae18f37SLucas Stach 
2837ae18f37SLucas Stach void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
2847ae18f37SLucas Stach {
2857ae18f37SLucas Stach 	/* Reset the USB controller with 2us delay */
2867ae18f37SLucas Stach 	reset_periph(config->periph_id, 2);
2877ae18f37SLucas Stach 
2887ae18f37SLucas Stach 	/*
2897ae18f37SLucas Stach 	 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
2907ae18f37SLucas Stach 	 * base address
2917ae18f37SLucas Stach 	 */
2927ae18f37SLucas Stach 	if (config->has_legacy_mode)
2937ae18f37SLucas Stach 		setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
2947ae18f37SLucas Stach 
2957ae18f37SLucas Stach 	/* Put UTMIP1/3 in reset */
2967ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
2977ae18f37SLucas Stach 
2987ae18f37SLucas Stach 	/* Enable the UTMIP PHY */
2997ae18f37SLucas Stach 	if (config->utmi)
3007ae18f37SLucas Stach 		setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
3017ae18f37SLucas Stach }
3027ae18f37SLucas Stach 
303*7e44d932SJim Lin static const unsigned *get_pll_timing(void)
304*7e44d932SJim Lin {
305*7e44d932SJim Lin 	const unsigned *timing;
306*7e44d932SJim Lin 
307*7e44d932SJim Lin 	timing = controller->pll_parameter +
308*7e44d932SJim Lin 		clock_get_osc_freq() * PARAM_COUNT;
309*7e44d932SJim Lin 
310*7e44d932SJim Lin 	return timing;
311*7e44d932SJim Lin }
312*7e44d932SJim Lin 
3137ae18f37SLucas Stach /* set up the UTMI USB controller with the parameters provided */
3147ae18f37SLucas Stach static int init_utmi_usb_controller(struct fdt_usb *config)
3157ae18f37SLucas Stach {
3167ae18f37SLucas Stach 	u32 val;
3177ae18f37SLucas Stach 	int loop_count;
3187ae18f37SLucas Stach 	const unsigned *timing;
3197ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
320*7e44d932SJim Lin 	struct clk_rst_ctlr *clkrst;
321*7e44d932SJim Lin 	struct usb_ctlr *usb1ctlr;
3227ae18f37SLucas Stach 
3237ae18f37SLucas Stach 	clock_enable(config->periph_id);
3247ae18f37SLucas Stach 
3257ae18f37SLucas Stach 	/* Reset the usb controller */
3267ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
3277ae18f37SLucas Stach 
3287ae18f37SLucas Stach 	/* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
3297ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
3307ae18f37SLucas Stach 
3317ae18f37SLucas Stach 	/* Follow the crystal clock disable by >100ns delay */
3327ae18f37SLucas Stach 	udelay(1);
3337ae18f37SLucas Stach 
3347ae18f37SLucas Stach 	/*
3357ae18f37SLucas Stach 	 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
3367ae18f37SLucas Stach 	 * mux must be switched to actually use a_sess_vld threshold.
3377ae18f37SLucas Stach 	 */
338*7e44d932SJim Lin 	if (config->dr_mode == DR_MODE_OTG &&
339*7e44d932SJim Lin 	    fdt_gpio_isvalid(&config->vbus_gpio))
3407ae18f37SLucas Stach 		clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
3417ae18f37SLucas Stach 			VBUS_SENSE_CTL_MASK,
3427ae18f37SLucas Stach 			VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
3437ae18f37SLucas Stach 
3447ae18f37SLucas Stach 	/*
3457ae18f37SLucas Stach 	 * PLL Delay CONFIGURATION settings. The following parameters control
3467ae18f37SLucas Stach 	 * the bring up of the plls.
3477ae18f37SLucas Stach 	 */
348*7e44d932SJim Lin 	timing = get_pll_timing();
3497ae18f37SLucas Stach 
350*7e44d932SJim Lin 	if (!controller->has_hostpc) {
3517ae18f37SLucas Stach 		val = readl(&usbctlr->utmip_misc_cfg1);
3527ae18f37SLucas Stach 		clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
353*7e44d932SJim Lin 				timing[PARAM_STABLE_COUNT] <<
354*7e44d932SJim Lin 				UTMIP_PLLU_STABLE_COUNT_SHIFT);
3557ae18f37SLucas Stach 		clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
3567ae18f37SLucas Stach 				timing[PARAM_ACTIVE_DELAY_COUNT] <<
3577ae18f37SLucas Stach 				UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
3587ae18f37SLucas Stach 		writel(val, &usbctlr->utmip_misc_cfg1);
3597ae18f37SLucas Stach 
3607ae18f37SLucas Stach 		/* Set PLL enable delay count and crystal frequency count */
3617ae18f37SLucas Stach 		val = readl(&usbctlr->utmip_pll_cfg1);
3627ae18f37SLucas Stach 		clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
3637ae18f37SLucas Stach 				timing[PARAM_ENABLE_DELAY_COUNT] <<
3647ae18f37SLucas Stach 				UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
3657ae18f37SLucas Stach 		clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
3667ae18f37SLucas Stach 				timing[PARAM_XTAL_FREQ_COUNT] <<
3677ae18f37SLucas Stach 				UTMIP_XTAL_FREQ_COUNT_SHIFT);
3687ae18f37SLucas Stach 		writel(val, &usbctlr->utmip_pll_cfg1);
369*7e44d932SJim Lin 	} else {
370*7e44d932SJim Lin 		clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
371*7e44d932SJim Lin 
372*7e44d932SJim Lin 		val = readl(&clkrst->crc_utmip_pll_cfg2);
373*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
374*7e44d932SJim Lin 				timing[PARAM_STABLE_COUNT] <<
375*7e44d932SJim Lin 				UTMIP_PLLU_STABLE_COUNT_SHIFT);
376*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
377*7e44d932SJim Lin 				timing[PARAM_ACTIVE_DELAY_COUNT] <<
378*7e44d932SJim Lin 				UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
379*7e44d932SJim Lin 		writel(val, &clkrst->crc_utmip_pll_cfg2);
380*7e44d932SJim Lin 
381*7e44d932SJim Lin 		/* Set PLL enable delay count and crystal frequency count */
382*7e44d932SJim Lin 		val = readl(&clkrst->crc_utmip_pll_cfg1);
383*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
384*7e44d932SJim Lin 				timing[PARAM_ENABLE_DELAY_COUNT] <<
385*7e44d932SJim Lin 				UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
386*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
387*7e44d932SJim Lin 				timing[PARAM_XTAL_FREQ_COUNT] <<
388*7e44d932SJim Lin 				UTMIP_XTAL_FREQ_COUNT_SHIFT);
389*7e44d932SJim Lin 		writel(val, &clkrst->crc_utmip_pll_cfg1);
390*7e44d932SJim Lin 
391*7e44d932SJim Lin 		/* Disable Power Down state for PLL */
392*7e44d932SJim Lin 		clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
393*7e44d932SJim Lin 			     PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
394*7e44d932SJim Lin 			     PLL_ACTIVE_POWERDOWN);
395*7e44d932SJim Lin 
396*7e44d932SJim Lin 		/* Recommended PHY settings for EYE diagram */
397*7e44d932SJim Lin 		val = readl(&usbctlr->utmip_xcvr_cfg0);
398*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
399*7e44d932SJim Lin 				0x4 << UTMIP_XCVR_SETUP_SHIFT);
400*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
401*7e44d932SJim Lin 				0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
402*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
403*7e44d932SJim Lin 				0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
404*7e44d932SJim Lin 		writel(val, &usbctlr->utmip_xcvr_cfg0);
405*7e44d932SJim Lin 		clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
406*7e44d932SJim Lin 				UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
407*7e44d932SJim Lin 				0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
408*7e44d932SJim Lin 
409*7e44d932SJim Lin 		/* Some registers can be controlled from USB1 only. */
410*7e44d932SJim Lin 		if (config->periph_id != PERIPH_ID_USBD) {
411*7e44d932SJim Lin 			clock_enable(PERIPH_ID_USBD);
412*7e44d932SJim Lin 			/* Disable Reset if in Reset state */
413*7e44d932SJim Lin 			reset_set_enable(PERIPH_ID_USBD, 0);
414*7e44d932SJim Lin 		}
415*7e44d932SJim Lin 		usb1ctlr = (struct usb_ctlr *)
416*7e44d932SJim Lin 			((u32)config->reg & USB1_ADDR_MASK);
417*7e44d932SJim Lin 		val = readl(&usb1ctlr->utmip_bias_cfg0);
418*7e44d932SJim Lin 		setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
419*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
420*7e44d932SJim Lin 				0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
421*7e44d932SJim Lin 		clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
422*7e44d932SJim Lin 				0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
423*7e44d932SJim Lin 		writel(val, &usb1ctlr->utmip_bias_cfg0);
424*7e44d932SJim Lin 
425*7e44d932SJim Lin 		/* Miscellaneous setting mentioned in Programming Guide */
426*7e44d932SJim Lin 		clrbits_le32(&usbctlr->utmip_misc_cfg0,
427*7e44d932SJim Lin 			     UTMIP_SUSPEND_EXIT_ON_EDGE);
428*7e44d932SJim Lin 	}
4297ae18f37SLucas Stach 
4307ae18f37SLucas Stach 	/* Setting the tracking length time */
4317ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
4327ae18f37SLucas Stach 		UTMIP_BIAS_PDTRK_COUNT_MASK,
4337ae18f37SLucas Stach 		timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
4347ae18f37SLucas Stach 
4357ae18f37SLucas Stach 	/* Program debounce time for VBUS to become valid */
4367ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
4377ae18f37SLucas Stach 		UTMIP_DEBOUNCE_CFG0_MASK,
4387ae18f37SLucas Stach 		timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
4397ae18f37SLucas Stach 
4407ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
4417ae18f37SLucas Stach 
4427ae18f37SLucas Stach 	/* Disable battery charge enabling bit */
4437ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
4447ae18f37SLucas Stach 
4457ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
4467ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
4477ae18f37SLucas Stach 
4487ae18f37SLucas Stach 	/*
4497ae18f37SLucas Stach 	 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
4507ae18f37SLucas Stach 	 * Setting these fields, together with default values of the
4517ae18f37SLucas Stach 	 * other fields, results in programming the registers below as
4527ae18f37SLucas Stach 	 * follows:
4537ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG0 = 0x9168c000
4547ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG1 = 0x13
4557ae18f37SLucas Stach 	 */
4567ae18f37SLucas Stach 
4577ae18f37SLucas Stach 	/* Set PLL enable delay count and Crystal frequency count */
4587ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_hsrx_cfg0);
4597ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
4607ae18f37SLucas Stach 		utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
4617ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
4627ae18f37SLucas Stach 		utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
4637ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_hsrx_cfg0);
4647ae18f37SLucas Stach 
4657ae18f37SLucas Stach 	/* Configure the UTMIP_HS_SYNC_START_DLY */
4667ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
4677ae18f37SLucas Stach 		UTMIP_HS_SYNC_START_DLY_MASK,
4687ae18f37SLucas Stach 		utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
4697ae18f37SLucas Stach 
4707ae18f37SLucas Stach 	/* Preceed the crystal clock disable by >100ns delay. */
4717ae18f37SLucas Stach 	udelay(1);
4727ae18f37SLucas Stach 
4737ae18f37SLucas Stach 	/* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
4747ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
4757ae18f37SLucas Stach 
476*7e44d932SJim Lin 	if (controller->has_hostpc) {
477*7e44d932SJim Lin 		if (config->periph_id == PERIPH_ID_USBD)
478*7e44d932SJim Lin 			clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
479*7e44d932SJim Lin 				     UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
480*7e44d932SJim Lin 		if (config->periph_id == PERIPH_ID_USB3)
481*7e44d932SJim Lin 			clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
482*7e44d932SJim Lin 				     UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
483*7e44d932SJim Lin 	}
4847ae18f37SLucas Stach 	/* Finished the per-controller init. */
4857ae18f37SLucas Stach 
4867ae18f37SLucas Stach 	/* De-assert UTMIP_RESET to bring out of reset. */
4877ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
4887ae18f37SLucas Stach 
4897ae18f37SLucas Stach 	/* Wait for the phy clock to become valid in 100 ms */
4907ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
4917ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
4927ae18f37SLucas Stach 			break;
4937ae18f37SLucas Stach 		udelay(1);
4947ae18f37SLucas Stach 	}
4957ae18f37SLucas Stach 	if (!loop_count)
4967ae18f37SLucas Stach 		return -1;
4977ae18f37SLucas Stach 
4987ae18f37SLucas Stach 	/* Disable ICUSB FS/LS transceiver */
4997ae18f37SLucas Stach 	clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
5007ae18f37SLucas Stach 
5017ae18f37SLucas Stach 	/* Select UTMI parallel interface */
5027ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
5037ae18f37SLucas Stach 			PTS_UTMI << PTS_SHIFT);
5047ae18f37SLucas Stach 	clrbits_le32(&usbctlr->port_sc1, STS);
5057ae18f37SLucas Stach 
5067ae18f37SLucas Stach 	/* Deassert power down state */
5077ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
5087ae18f37SLucas Stach 		UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
5097ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
5107ae18f37SLucas Stach 		UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
5117ae18f37SLucas Stach 
512*7e44d932SJim Lin 	if (controller->has_hostpc) {
513*7e44d932SJim Lin 		/*
514*7e44d932SJim Lin 		 * BIAS Pad Power Down is common among all 3 USB
515*7e44d932SJim Lin 		 * controllers and can be controlled from USB1 only.
516*7e44d932SJim Lin 		 */
517*7e44d932SJim Lin 		usb1ctlr = (struct usb_ctlr *)
518*7e44d932SJim Lin 			((u32)config->reg & USB1_ADDR_MASK);
519*7e44d932SJim Lin 		clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
520*7e44d932SJim Lin 		udelay(25);
521*7e44d932SJim Lin 		clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
522*7e44d932SJim Lin 			     UTMIP_FORCE_PDTRK_POWERDOWN);
523*7e44d932SJim Lin 	}
5247ae18f37SLucas Stach 	return 0;
5257ae18f37SLucas Stach }
5267ae18f37SLucas Stach 
5277ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
5287ae18f37SLucas Stach /* if board file does not set a ULPI reference frequency we default to 24MHz */
5297ae18f37SLucas Stach #ifndef CONFIG_ULPI_REF_CLK
5307ae18f37SLucas Stach #define CONFIG_ULPI_REF_CLK 24000000
5317ae18f37SLucas Stach #endif
5327ae18f37SLucas Stach 
5337ae18f37SLucas Stach /* set up the ULPI USB controller with the parameters provided */
5347ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
5357ae18f37SLucas Stach {
5367ae18f37SLucas Stach 	u32 val;
5377ae18f37SLucas Stach 	int loop_count;
5387ae18f37SLucas Stach 	struct ulpi_viewport ulpi_vp;
5397ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
5407ae18f37SLucas Stach 
5417ae18f37SLucas Stach 	/* set up ULPI reference clock on pllp_out4 */
5427ae18f37SLucas Stach 	clock_enable(PERIPH_ID_DEV2_OUT);
5437ae18f37SLucas Stach 	clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
5447ae18f37SLucas Stach 
5457ae18f37SLucas Stach 	/* reset ULPI phy */
5467ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
5477ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->phy_reset_gpio);
5487ae18f37SLucas Stach 		gpio_direction_output(config->phy_reset_gpio.gpio, 0);
5497ae18f37SLucas Stach 		mdelay(5);
5507ae18f37SLucas Stach 		gpio_set_value(config->phy_reset_gpio.gpio, 1);
5517ae18f37SLucas Stach 	}
5527ae18f37SLucas Stach 
5537ae18f37SLucas Stach 	/* Reset the usb controller */
5547ae18f37SLucas Stach 	clock_enable(config->periph_id);
5557ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
5567ae18f37SLucas Stach 
5577ae18f37SLucas Stach 	/* enable pinmux bypass */
5587ae18f37SLucas Stach 	setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
5597ae18f37SLucas Stach 			ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
5607ae18f37SLucas Stach 
5617ae18f37SLucas Stach 	/* Select ULPI parallel interface */
5627ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
5637ae18f37SLucas Stach 
5647ae18f37SLucas Stach 	/* enable ULPI transceiver */
5657ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
5667ae18f37SLucas Stach 
5677ae18f37SLucas Stach 	/* configure ULPI transceiver timings */
5687ae18f37SLucas Stach 	val = 0;
5697ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
5707ae18f37SLucas Stach 
5717ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_SEL(4);
5727ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
5737ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_SEL(4);
5747ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
5757ae18f37SLucas Stach 	udelay(10);
5767ae18f37SLucas Stach 
5777ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_LOAD;
5787ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
5797ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_LOAD;
5807ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
5817ae18f37SLucas Stach 
5827ae18f37SLucas Stach 	/* set up phy for host operation with external vbus supply */
5837ae18f37SLucas Stach 	ulpi_vp.port_num = 0;
5847ae18f37SLucas Stach 	ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
5857ae18f37SLucas Stach 
5867ae18f37SLucas Stach 	if (ulpi_init(&ulpi_vp)) {
5877ae18f37SLucas Stach 		printf("Tegra ULPI viewport init failed\n");
5887ae18f37SLucas Stach 		return -1;
5897ae18f37SLucas Stach 	}
5907ae18f37SLucas Stach 
5917ae18f37SLucas Stach 	ulpi_set_vbus(&ulpi_vp, 1, 1);
5927ae18f37SLucas Stach 	ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
5937ae18f37SLucas Stach 
5947ae18f37SLucas Stach 	/* enable wakeup events */
5957ae18f37SLucas Stach 	setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
5967ae18f37SLucas Stach 
5977ae18f37SLucas Stach 	/* Enable and wait for the phy clock to become valid in 100 ms */
5987ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
5997ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
6007ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
6017ae18f37SLucas Stach 			break;
6027ae18f37SLucas Stach 		udelay(1);
6037ae18f37SLucas Stach 	}
6047ae18f37SLucas Stach 	if (!loop_count)
6057ae18f37SLucas Stach 		return -1;
6067ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
6077ae18f37SLucas Stach 
6087ae18f37SLucas Stach 	return 0;
6097ae18f37SLucas Stach }
6107ae18f37SLucas Stach #else
6117ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
6127ae18f37SLucas Stach {
6137ae18f37SLucas Stach 	printf("No code to set up ULPI controller, please enable"
6147ae18f37SLucas Stach 			"CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
6157ae18f37SLucas Stach 	return -1;
6167ae18f37SLucas Stach }
6177ae18f37SLucas Stach #endif
6187ae18f37SLucas Stach 
6197ae18f37SLucas Stach static void config_clock(const u32 timing[])
6207ae18f37SLucas Stach {
6217ae18f37SLucas Stach 	clock_start_pll(CLOCK_ID_USB,
6227ae18f37SLucas Stach 		timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
6237ae18f37SLucas Stach 		timing[PARAM_CPCON], timing[PARAM_LFCON]);
6247ae18f37SLucas Stach }
6257ae18f37SLucas Stach 
626*7e44d932SJim Lin static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
6277ae18f37SLucas Stach {
6287ae18f37SLucas Stach 	const char *phy, *mode;
6297ae18f37SLucas Stach 
6307ae18f37SLucas Stach 	config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
6317ae18f37SLucas Stach 	mode = fdt_getprop(blob, node, "dr_mode", NULL);
6327ae18f37SLucas Stach 	if (mode) {
6337ae18f37SLucas Stach 		if (0 == strcmp(mode, "host"))
6347ae18f37SLucas Stach 			config->dr_mode = DR_MODE_HOST;
6357ae18f37SLucas Stach 		else if (0 == strcmp(mode, "peripheral"))
6367ae18f37SLucas Stach 			config->dr_mode = DR_MODE_DEVICE;
6377ae18f37SLucas Stach 		else if (0 == strcmp(mode, "otg"))
6387ae18f37SLucas Stach 			config->dr_mode = DR_MODE_OTG;
6397ae18f37SLucas Stach 		else {
6407ae18f37SLucas Stach 			debug("%s: Cannot decode dr_mode '%s'\n", __func__,
6417ae18f37SLucas Stach 			      mode);
6427ae18f37SLucas Stach 			return -FDT_ERR_NOTFOUND;
6437ae18f37SLucas Stach 		}
6447ae18f37SLucas Stach 	} else {
6457ae18f37SLucas Stach 		config->dr_mode = DR_MODE_HOST;
6467ae18f37SLucas Stach 	}
6477ae18f37SLucas Stach 
6487ae18f37SLucas Stach 	phy = fdt_getprop(blob, node, "phy_type", NULL);
6497ae18f37SLucas Stach 	config->utmi = phy && 0 == strcmp("utmi", phy);
6507ae18f37SLucas Stach 	config->ulpi = phy && 0 == strcmp("ulpi", phy);
6517ae18f37SLucas Stach 	config->enabled = fdtdec_get_is_enabled(blob, node);
6527ae18f37SLucas Stach 	config->has_legacy_mode = fdtdec_get_bool(blob, node,
6537ae18f37SLucas Stach 						  "nvidia,has-legacy-mode");
654*7e44d932SJim Lin 	if (config->has_legacy_mode)
655*7e44d932SJim Lin 		port_addr_clear_csc = (u32) config->reg;
6567ae18f37SLucas Stach 	config->periph_id = clock_decode_periph_id(blob, node);
6577ae18f37SLucas Stach 	if (config->periph_id == PERIPH_ID_NONE) {
6587ae18f37SLucas Stach 		debug("%s: Missing/invalid peripheral ID\n", __func__);
6597ae18f37SLucas Stach 		return -FDT_ERR_NOTFOUND;
6607ae18f37SLucas Stach 	}
6617ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
6627ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
6637ae18f37SLucas Stach 			&config->phy_reset_gpio);
6647ae18f37SLucas Stach 	debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
6657ae18f37SLucas Stach 		"vbus=%d, phy_reset=%d, dr_mode=%d\n",
6667ae18f37SLucas Stach 		config->enabled, config->has_legacy_mode, config->utmi,
6677ae18f37SLucas Stach 		config->ulpi, config->periph_id, config->vbus_gpio.gpio,
6687ae18f37SLucas Stach 		config->phy_reset_gpio.gpio, config->dr_mode);
6697ae18f37SLucas Stach 
6707ae18f37SLucas Stach 	return 0;
6717ae18f37SLucas Stach }
6727ae18f37SLucas Stach 
673*7e44d932SJim Lin /*
674*7e44d932SJim Lin  * process_usb_nodes() - Process a list of USB nodes, adding them to our list
675*7e44d932SJim Lin  *			of USB ports.
676*7e44d932SJim Lin  * @blob:	fdt blob
677*7e44d932SJim Lin  * @node_list:	list of nodes to process (any <=0 are ignored)
678*7e44d932SJim Lin  * @count:	number of nodes to process
679*7e44d932SJim Lin  *
680*7e44d932SJim Lin  * Return:	0 - ok, -1 - error
681*7e44d932SJim Lin  */
682*7e44d932SJim Lin static int process_usb_nodes(const void *blob, int node_list[], int count)
6837ae18f37SLucas Stach {
6847ae18f37SLucas Stach 	struct fdt_usb config;
685*7e44d932SJim Lin 	int node, i;
686*7e44d932SJim Lin 	int clk_done = 0;
6877ae18f37SLucas Stach 
688*7e44d932SJim Lin 	port_count = 0;
6897ae18f37SLucas Stach 	for (i = 0; i < count; i++) {
6907ae18f37SLucas Stach 		if (port_count == USB_PORTS_MAX) {
6917ae18f37SLucas Stach 			printf("tegrausb: Cannot register more than %d ports\n",
6927ae18f37SLucas Stach 				USB_PORTS_MAX);
6937ae18f37SLucas Stach 			return -1;
6947ae18f37SLucas Stach 		}
6957ae18f37SLucas Stach 
6967ae18f37SLucas Stach 		debug("USB %d: ", i);
6977ae18f37SLucas Stach 		node = node_list[i];
6987ae18f37SLucas Stach 		if (!node)
6997ae18f37SLucas Stach 			continue;
7007ae18f37SLucas Stach 		if (fdt_decode_usb(blob, node, &config)) {
7017ae18f37SLucas Stach 			debug("Cannot decode USB node %s\n",
7027ae18f37SLucas Stach 			      fdt_get_name(blob, node, NULL));
7037ae18f37SLucas Stach 			return -1;
7047ae18f37SLucas Stach 		}
705*7e44d932SJim Lin 		if (!clk_done) {
706*7e44d932SJim Lin 			config_clock(get_pll_timing());
707*7e44d932SJim Lin 			clk_done = 1;
708*7e44d932SJim Lin 		}
7097ae18f37SLucas Stach 		config.initialized = 0;
7107ae18f37SLucas Stach 
7117ae18f37SLucas Stach 		/* add new USB port to the list of available ports */
7127ae18f37SLucas Stach 		port[port_count++] = config;
7137ae18f37SLucas Stach 	}
7147ae18f37SLucas Stach 
7157ae18f37SLucas Stach 	return 0;
7167ae18f37SLucas Stach }
7177ae18f37SLucas Stach 
718*7e44d932SJim Lin int board_usb_init(const void *blob)
719*7e44d932SJim Lin {
720*7e44d932SJim Lin 	int node_list[USB_PORTS_MAX];
721*7e44d932SJim Lin 	int count, err = 0;
722*7e44d932SJim Lin 	int i;
723*7e44d932SJim Lin 
724*7e44d932SJim Lin 	for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
725*7e44d932SJim Lin 		controller = &fdt_usb_controllers[i];
726*7e44d932SJim Lin 
727*7e44d932SJim Lin 		count = fdtdec_find_aliases_for_id(blob, "usb",
728*7e44d932SJim Lin 			controller->compat, node_list, USB_PORTS_MAX);
729*7e44d932SJim Lin 		if (count) {
730*7e44d932SJim Lin 			err = process_usb_nodes(blob, node_list, count);
731*7e44d932SJim Lin 			if (err)
732*7e44d932SJim Lin 				printf("%s: Error processing USB node!\n",
733*7e44d932SJim Lin 				       __func__);
734*7e44d932SJim Lin 			return err;
735*7e44d932SJim Lin 		}
736*7e44d932SJim Lin 	}
737*7e44d932SJim Lin 	if (i == ARRAY_SIZE(fdt_usb_controllers))
738*7e44d932SJim Lin 		controller = NULL;
739*7e44d932SJim Lin 
740*7e44d932SJim Lin 	return err;
741*7e44d932SJim Lin }
742*7e44d932SJim Lin 
743d7a55e1aSLucas Stach /**
744d7a55e1aSLucas Stach  * Start up the given port number (ports are numbered from 0 on each board).
745d7a55e1aSLucas Stach  * This returns values for the appropriate hccr and hcor addresses to use for
746d7a55e1aSLucas Stach  * USB EHCI operations.
747d7a55e1aSLucas Stach  *
748d7a55e1aSLucas Stach  * @param index	port number to start
749d7a55e1aSLucas Stach  * @param hccr		returns start address of EHCI HCCR registers
750d7a55e1aSLucas Stach  * @param hcor		returns start address of EHCI HCOR registers
751d7a55e1aSLucas Stach  * @return 0 if ok, -1 on error (generally invalid port number)
75287f938c9SSimon Glass  */
753676ae068SLucas Stach int ehci_hcd_init(int index, struct ehci_hccr **hccr, struct ehci_hcor **hcor)
75487f938c9SSimon Glass {
755d7a55e1aSLucas Stach 	struct fdt_usb *config;
756d7a55e1aSLucas Stach 	struct usb_ctlr *usbctlr;
75787f938c9SSimon Glass 
758d7a55e1aSLucas Stach 	if (index >= port_count)
75987f938c9SSimon Glass 		return -1;
76087f938c9SSimon Glass 
761d7a55e1aSLucas Stach 	config = &port[index];
76287f938c9SSimon Glass 
763d7a55e1aSLucas Stach 	/* skip init, if the port is already initialized */
764d7a55e1aSLucas Stach 	if (config->initialized)
765d7a55e1aSLucas Stach 		goto success;
766d7a55e1aSLucas Stach 
767d7a55e1aSLucas Stach 	if (config->utmi && init_utmi_usb_controller(config)) {
768d7a55e1aSLucas Stach 		printf("tegrausb: Cannot init port %d\n", index);
769d7a55e1aSLucas Stach 		return -1;
770d7a55e1aSLucas Stach 	}
771d7a55e1aSLucas Stach 
772d7a55e1aSLucas Stach 	if (config->ulpi && init_ulpi_usb_controller(config)) {
773d7a55e1aSLucas Stach 		printf("tegrausb: Cannot init port %d\n", index);
774d7a55e1aSLucas Stach 		return -1;
775d7a55e1aSLucas Stach 	}
776d7a55e1aSLucas Stach 
777d7a55e1aSLucas Stach 	set_host_mode(config);
778d7a55e1aSLucas Stach 
779d7a55e1aSLucas Stach 	config->initialized = 1;
780d7a55e1aSLucas Stach 
781d7a55e1aSLucas Stach success:
782d7a55e1aSLucas Stach 	usbctlr = config->reg;
783d7a55e1aSLucas Stach 	*hccr = (struct ehci_hccr *)&usbctlr->cap_length;
784d7a55e1aSLucas Stach 	*hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
785*7e44d932SJim Lin 
786*7e44d932SJim Lin 	if (controller->has_hostpc) {
787*7e44d932SJim Lin 		/* Set to Host mode after Controller Reset was done */
788*7e44d932SJim Lin 		clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
789*7e44d932SJim Lin 				USBMODE_CM_HC);
790*7e44d932SJim Lin 		/* Select UTMI parallel interface after setting host mode */
791*7e44d932SJim Lin 		if (config->utmi) {
792*7e44d932SJim Lin 			clrsetbits_le32((char *)&usbctlr->usb_cmd +
793*7e44d932SJim Lin 					HOSTPC1_DEVLC, PTS_MASK,
794*7e44d932SJim Lin 					PTS_UTMI << PTS_SHIFT);
795*7e44d932SJim Lin 			clrbits_le32((char *)&usbctlr->usb_cmd +
796*7e44d932SJim Lin 				     HOSTPC1_DEVLC, STS);
797*7e44d932SJim Lin 		}
798*7e44d932SJim Lin 	}
79987f938c9SSimon Glass 	return 0;
80087f938c9SSimon Glass }
80187f938c9SSimon Glass 
80287f938c9SSimon Glass /*
803d7a55e1aSLucas Stach  * Bring down the specified USB controller
80487f938c9SSimon Glass  */
805676ae068SLucas Stach int ehci_hcd_stop(int index)
80687f938c9SSimon Glass {
807d7a55e1aSLucas Stach 	struct usb_ctlr *usbctlr;
808d7a55e1aSLucas Stach 
809d7a55e1aSLucas Stach 	usbctlr = port[index].reg;
810d7a55e1aSLucas Stach 
811d7a55e1aSLucas Stach 	/* Stop controller */
812d7a55e1aSLucas Stach 	writel(0, &usbctlr->usb_cmd);
813d7a55e1aSLucas Stach 	udelay(1000);
814d7a55e1aSLucas Stach 
815d7a55e1aSLucas Stach 	/* Initiate controller reset */
816d7a55e1aSLucas Stach 	writel(2, &usbctlr->usb_cmd);
817d7a55e1aSLucas Stach 	udelay(1000);
818d7a55e1aSLucas Stach 
819d7a55e1aSLucas Stach 	port[index].initialized = 0;
820d7a55e1aSLucas Stach 
821d7a55e1aSLucas Stach 	return 0;
82287f938c9SSimon Glass }
823