xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-tegra.c (revision d7a55e1a4b702bffae3e8ed43a5be125a30ba8ab)
187f938c9SSimon Glass /*
27ae18f37SLucas Stach  * Copyright (c) 2011 The Chromium OS Authors.
38b3f7bf7SJim Lin  * Copyright (c) 2009-2012 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>
3187f938c9SSimon Glass #include <usb.h>
327ae18f37SLucas Stach #include <usb/ulpi.h>
337ae18f37SLucas Stach #include <libfdt.h>
347ae18f37SLucas Stach #include <fdtdec.h>
3587f938c9SSimon Glass 
3687f938c9SSimon Glass #include "ehci.h"
3787f938c9SSimon Glass 
387ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
397ae18f37SLucas Stach 	#ifndef CONFIG_USB_ULPI_VIEWPORT
407ae18f37SLucas Stach 	#error	"To use CONFIG_USB_ULPI on Tegra Boards you have to also \
417ae18f37SLucas Stach 		define CONFIG_USB_ULPI_VIEWPORT"
427ae18f37SLucas Stach 	#endif
437ae18f37SLucas Stach #endif
447ae18f37SLucas Stach 
457ae18f37SLucas Stach enum {
467ae18f37SLucas Stach 	USB_PORTS_MAX	= 3,		/* Maximum ports we allow */
477ae18f37SLucas Stach };
487ae18f37SLucas Stach 
497ae18f37SLucas Stach /* Parameters we need for USB */
507ae18f37SLucas Stach enum {
517ae18f37SLucas Stach 	PARAM_DIVN,                     /* PLL FEEDBACK DIVIDer */
527ae18f37SLucas Stach 	PARAM_DIVM,                     /* PLL INPUT DIVIDER */
537ae18f37SLucas Stach 	PARAM_DIVP,                     /* POST DIVIDER (2^N) */
547ae18f37SLucas Stach 	PARAM_CPCON,                    /* BASE PLLC CHARGE Pump setup ctrl */
557ae18f37SLucas Stach 	PARAM_LFCON,                    /* BASE PLLC LOOP FILter setup ctrl */
567ae18f37SLucas Stach 	PARAM_ENABLE_DELAY_COUNT,       /* PLL-U Enable Delay Count */
577ae18f37SLucas Stach 	PARAM_STABLE_COUNT,             /* PLL-U STABLE count */
587ae18f37SLucas Stach 	PARAM_ACTIVE_DELAY_COUNT,       /* PLL-U Active delay count */
597ae18f37SLucas Stach 	PARAM_XTAL_FREQ_COUNT,          /* PLL-U XTAL frequency count */
607ae18f37SLucas Stach 	PARAM_DEBOUNCE_A_TIME,          /* 10MS DELAY for BIAS_DEBOUNCE_A */
617ae18f37SLucas Stach 	PARAM_BIAS_TIME,                /* 20US DELAY AFter bias cell op */
627ae18f37SLucas Stach 
637ae18f37SLucas Stach 	PARAM_COUNT
647ae18f37SLucas Stach };
657ae18f37SLucas Stach 
667ae18f37SLucas Stach /* Possible port types (dual role mode) */
677ae18f37SLucas Stach enum dr_mode {
687ae18f37SLucas Stach 	DR_MODE_NONE = 0,
697ae18f37SLucas Stach 	DR_MODE_HOST,		/* supports host operation */
707ae18f37SLucas Stach 	DR_MODE_DEVICE,		/* supports device operation */
717ae18f37SLucas Stach 	DR_MODE_OTG,		/* supports both */
727ae18f37SLucas Stach };
737ae18f37SLucas Stach 
747ae18f37SLucas Stach /* Information about a USB port */
757ae18f37SLucas Stach struct fdt_usb {
767ae18f37SLucas Stach 	struct usb_ctlr *reg;	/* address of registers in physical memory */
777ae18f37SLucas Stach 	unsigned utmi:1;	/* 1 if port has external tranceiver, else 0 */
787ae18f37SLucas Stach 	unsigned ulpi:1;	/* 1 if port has external ULPI transceiver */
797ae18f37SLucas Stach 	unsigned enabled:1;	/* 1 to enable, 0 to disable */
807ae18f37SLucas Stach 	unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
817ae18f37SLucas Stach 	unsigned initialized:1; /* has this port already been initialized? */
827ae18f37SLucas Stach 	enum dr_mode dr_mode;	/* dual role mode */
837ae18f37SLucas Stach 	enum periph_id periph_id;/* peripheral id */
847ae18f37SLucas Stach 	struct fdt_gpio_state vbus_gpio;	/* GPIO for vbus enable */
857ae18f37SLucas Stach 	struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
867ae18f37SLucas Stach };
877ae18f37SLucas Stach 
887ae18f37SLucas Stach static struct fdt_usb port[USB_PORTS_MAX];	/* List of valid USB ports */
897ae18f37SLucas Stach static unsigned port_count;			/* Number of available ports */
907ae18f37SLucas Stach 
917ae18f37SLucas Stach /*
927ae18f37SLucas Stach  * This table has USB timing parameters for each Oscillator frequency we
937ae18f37SLucas Stach  * support. There are four sets of values:
947ae18f37SLucas Stach  *
957ae18f37SLucas Stach  * 1. PLLU configuration information (reference clock is osc/clk_m and
967ae18f37SLucas Stach  * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
977ae18f37SLucas Stach  *
987ae18f37SLucas Stach  *  Reference frequency     13.0MHz      19.2MHz      12.0MHz      26.0MHz
997ae18f37SLucas Stach  *  ----------------------------------------------------------------------
1007ae18f37SLucas Stach  *      DIVN                960 (0x3c0)  200 (0c8)    960 (3c0h)   960 (3c0)
1017ae18f37SLucas Stach  *      DIVM                13 (0d)      4 (04)       12 (0c)      26 (1a)
1027ae18f37SLucas Stach  * Filter frequency (MHz)   1            4.8          6            2
1037ae18f37SLucas Stach  * CPCON                    1100b        0011b        1100b        1100b
1047ae18f37SLucas Stach  * LFCON0                   0            0            0            0
1057ae18f37SLucas Stach  *
1067ae18f37SLucas Stach  * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
1077ae18f37SLucas Stach  *
1087ae18f37SLucas Stach  * Reference frequency     13.0MHz         19.2MHz         12.0MHz     26.0MHz
1097ae18f37SLucas Stach  * ---------------------------------------------------------------------------
1107ae18f37SLucas Stach  * PLLU_ENABLE_DLY_COUNT   02 (0x02)       03 (03)         02 (02)     04 (04)
1117ae18f37SLucas Stach  * PLLU_STABLE_COUNT       51 (33)         75 (4B)         47 (2F)    102 (66)
1127ae18f37SLucas Stach  * PLL_ACTIVE_DLY_COUNT    05 (05)         06 (06)         04 (04)     09 (09)
1137ae18f37SLucas Stach  * XTAL_FREQ_COUNT        127 (7F)        187 (BB)        118 (76)    254 (FE)
1147ae18f37SLucas Stach  *
1157ae18f37SLucas Stach  * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
1167ae18f37SLucas Stach  * SessEnd. Each of these signals have their own debouncer and for each of
1177ae18f37SLucas Stach  * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
1187ae18f37SLucas Stach  * BIAS_DEBOUNCE_B).
1197ae18f37SLucas Stach  *
1207ae18f37SLucas Stach  * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
1217ae18f37SLucas Stach  *    0xffff -> No debouncing at all
1227ae18f37SLucas Stach  *    <n> ms = <n> *1000 / (1/19.2MHz) / 4
1237ae18f37SLucas Stach  *
1247ae18f37SLucas Stach  * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
1257ae18f37SLucas Stach  * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4  = 4800 = 0x12c0
1267ae18f37SLucas Stach  *
1277ae18f37SLucas Stach  * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
1287ae18f37SLucas Stach  * values, so we can keep those to default.
1297ae18f37SLucas Stach  *
1307ae18f37SLucas Stach  * 4. The 20 microsecond delay after bias cell operation.
1317ae18f37SLucas Stach  */
1327ae18f37SLucas Stach static const unsigned usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
1337ae18f37SLucas Stach 	/* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
1347ae18f37SLucas Stach 	{ 0x3C0, 0x0D, 0x00, 0xC,   0,  0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
1357ae18f37SLucas Stach 	{ 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
1367ae18f37SLucas Stach 	{ 0x3C0, 0x0C, 0x00, 0xC,   0,  0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
1377ae18f37SLucas Stach 	{ 0x3C0, 0x1A, 0x00, 0xC,   0,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
1387ae18f37SLucas Stach };
1397ae18f37SLucas Stach 
1407ae18f37SLucas Stach /* UTMIP Idle Wait Delay */
1417ae18f37SLucas Stach static const u8 utmip_idle_wait_delay = 17;
1427ae18f37SLucas Stach 
1437ae18f37SLucas Stach /* UTMIP Elastic limit */
1447ae18f37SLucas Stach static const u8 utmip_elastic_limit = 16;
1457ae18f37SLucas Stach 
1467ae18f37SLucas Stach /* UTMIP High Speed Sync Start Delay */
1477ae18f37SLucas 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 
1667ae18f37SLucas Stach /* Put the port into host mode */
1677ae18f37SLucas Stach static void set_host_mode(struct fdt_usb *config)
1687ae18f37SLucas Stach {
1697ae18f37SLucas Stach 	/*
1707ae18f37SLucas Stach 	 * If we are an OTG port, check if remote host is driving VBus and
1717ae18f37SLucas Stach 	 * bail out in this case.
1727ae18f37SLucas Stach 	 */
1737ae18f37SLucas Stach 	if (config->dr_mode == DR_MODE_OTG &&
1747ae18f37SLucas Stach 		(readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
1757ae18f37SLucas Stach 		return;
1767ae18f37SLucas Stach 
1777ae18f37SLucas Stach 	/*
1787ae18f37SLucas Stach 	 * If not driving, we set the GPIO to enable VBUS. We assume
1797ae18f37SLucas Stach 	 * that the pinmux is set up correctly for this.
1807ae18f37SLucas Stach 	 */
1817ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
1827ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->vbus_gpio);
1837ae18f37SLucas Stach 		gpio_direction_output(config->vbus_gpio.gpio,
1847ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
1857ae18f37SLucas Stach 				 0 : 1);
1867ae18f37SLucas Stach 		debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
1877ae18f37SLucas Stach 			(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
1887ae18f37SLucas Stach 				"low" : "high");
1897ae18f37SLucas Stach 	}
1907ae18f37SLucas Stach }
1917ae18f37SLucas Stach 
1927ae18f37SLucas Stach void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
1937ae18f37SLucas Stach {
1947ae18f37SLucas Stach 	/* Reset the USB controller with 2us delay */
1957ae18f37SLucas Stach 	reset_periph(config->periph_id, 2);
1967ae18f37SLucas Stach 
1977ae18f37SLucas Stach 	/*
1987ae18f37SLucas Stach 	 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
1997ae18f37SLucas Stach 	 * base address
2007ae18f37SLucas Stach 	 */
2017ae18f37SLucas Stach 	if (config->has_legacy_mode)
2027ae18f37SLucas Stach 		setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
2037ae18f37SLucas Stach 
2047ae18f37SLucas Stach 	/* Put UTMIP1/3 in reset */
2057ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
2067ae18f37SLucas Stach 
2077ae18f37SLucas Stach 	/* Enable the UTMIP PHY */
2087ae18f37SLucas Stach 	if (config->utmi)
2097ae18f37SLucas Stach 		setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
2107ae18f37SLucas Stach }
2117ae18f37SLucas Stach 
2127ae18f37SLucas Stach /* set up the UTMI USB controller with the parameters provided */
2137ae18f37SLucas Stach static int init_utmi_usb_controller(struct fdt_usb *config)
2147ae18f37SLucas Stach {
2157ae18f37SLucas Stach 	u32 val;
2167ae18f37SLucas Stach 	int loop_count;
2177ae18f37SLucas Stach 	const unsigned *timing;
2187ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
2197ae18f37SLucas Stach 
2207ae18f37SLucas Stach 	clock_enable(config->periph_id);
2217ae18f37SLucas Stach 
2227ae18f37SLucas Stach 	/* Reset the usb controller */
2237ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
2247ae18f37SLucas Stach 
2257ae18f37SLucas Stach 	/* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
2267ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
2277ae18f37SLucas Stach 
2287ae18f37SLucas Stach 	/* Follow the crystal clock disable by >100ns delay */
2297ae18f37SLucas Stach 	udelay(1);
2307ae18f37SLucas Stach 
2317ae18f37SLucas Stach 	/*
2327ae18f37SLucas Stach 	 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
2337ae18f37SLucas Stach 	 * mux must be switched to actually use a_sess_vld threshold.
2347ae18f37SLucas Stach 	 */
2357ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->vbus_gpio)) {
2367ae18f37SLucas Stach 		clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
2377ae18f37SLucas Stach 			VBUS_SENSE_CTL_MASK,
2387ae18f37SLucas Stach 			VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
2397ae18f37SLucas Stach 	}
2407ae18f37SLucas Stach 
2417ae18f37SLucas Stach 	/*
2427ae18f37SLucas Stach 	 * PLL Delay CONFIGURATION settings. The following parameters control
2437ae18f37SLucas Stach 	 * the bring up of the plls.
2447ae18f37SLucas Stach 	 */
2457ae18f37SLucas Stach 	timing = usb_pll[clock_get_osc_freq()];
2467ae18f37SLucas Stach 
2477ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_misc_cfg1);
2487ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
2497ae18f37SLucas Stach 		timing[PARAM_STABLE_COUNT] << UTMIP_PLLU_STABLE_COUNT_SHIFT);
2507ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
2517ae18f37SLucas Stach 		timing[PARAM_ACTIVE_DELAY_COUNT] <<
2527ae18f37SLucas Stach 			UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
2537ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_misc_cfg1);
2547ae18f37SLucas Stach 
2557ae18f37SLucas Stach 	/* Set PLL enable delay count and crystal frequency count */
2567ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_pll_cfg1);
2577ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
2587ae18f37SLucas Stach 		timing[PARAM_ENABLE_DELAY_COUNT] <<
2597ae18f37SLucas Stach 			UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
2607ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
2617ae18f37SLucas Stach 		timing[PARAM_XTAL_FREQ_COUNT] <<
2627ae18f37SLucas Stach 			UTMIP_XTAL_FREQ_COUNT_SHIFT);
2637ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_pll_cfg1);
2647ae18f37SLucas Stach 
2657ae18f37SLucas Stach 	/* Setting the tracking length time */
2667ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
2677ae18f37SLucas Stach 		UTMIP_BIAS_PDTRK_COUNT_MASK,
2687ae18f37SLucas Stach 		timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
2697ae18f37SLucas Stach 
2707ae18f37SLucas Stach 	/* Program debounce time for VBUS to become valid */
2717ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
2727ae18f37SLucas Stach 		UTMIP_DEBOUNCE_CFG0_MASK,
2737ae18f37SLucas Stach 		timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
2747ae18f37SLucas Stach 
2757ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
2767ae18f37SLucas Stach 
2777ae18f37SLucas Stach 	/* Disable battery charge enabling bit */
2787ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
2797ae18f37SLucas Stach 
2807ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
2817ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
2827ae18f37SLucas Stach 
2837ae18f37SLucas Stach 	/*
2847ae18f37SLucas Stach 	 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
2857ae18f37SLucas Stach 	 * Setting these fields, together with default values of the
2867ae18f37SLucas Stach 	 * other fields, results in programming the registers below as
2877ae18f37SLucas Stach 	 * follows:
2887ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG0 = 0x9168c000
2897ae18f37SLucas Stach 	 *         UTMIP_HSRX_CFG1 = 0x13
2907ae18f37SLucas Stach 	 */
2917ae18f37SLucas Stach 
2927ae18f37SLucas Stach 	/* Set PLL enable delay count and Crystal frequency count */
2937ae18f37SLucas Stach 	val = readl(&usbctlr->utmip_hsrx_cfg0);
2947ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
2957ae18f37SLucas Stach 		utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
2967ae18f37SLucas Stach 	clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
2977ae18f37SLucas Stach 		utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
2987ae18f37SLucas Stach 	writel(val, &usbctlr->utmip_hsrx_cfg0);
2997ae18f37SLucas Stach 
3007ae18f37SLucas Stach 	/* Configure the UTMIP_HS_SYNC_START_DLY */
3017ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
3027ae18f37SLucas Stach 		UTMIP_HS_SYNC_START_DLY_MASK,
3037ae18f37SLucas Stach 		utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
3047ae18f37SLucas Stach 
3057ae18f37SLucas Stach 	/* Preceed the crystal clock disable by >100ns delay. */
3067ae18f37SLucas Stach 	udelay(1);
3077ae18f37SLucas Stach 
3087ae18f37SLucas Stach 	/* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
3097ae18f37SLucas Stach 	setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
3107ae18f37SLucas Stach 
3117ae18f37SLucas Stach 	/* Finished the per-controller init. */
3127ae18f37SLucas Stach 
3137ae18f37SLucas Stach 	/* De-assert UTMIP_RESET to bring out of reset. */
3147ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
3157ae18f37SLucas Stach 
3167ae18f37SLucas Stach 	/* Wait for the phy clock to become valid in 100 ms */
3177ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
3187ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
3197ae18f37SLucas Stach 			break;
3207ae18f37SLucas Stach 		udelay(1);
3217ae18f37SLucas Stach 	}
3227ae18f37SLucas Stach 	if (!loop_count)
3237ae18f37SLucas Stach 		return -1;
3247ae18f37SLucas Stach 
3257ae18f37SLucas Stach 	/* Disable ICUSB FS/LS transceiver */
3267ae18f37SLucas Stach 	clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
3277ae18f37SLucas Stach 
3287ae18f37SLucas Stach 	/* Select UTMI parallel interface */
3297ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
3307ae18f37SLucas Stach 			PTS_UTMI << PTS_SHIFT);
3317ae18f37SLucas Stach 	clrbits_le32(&usbctlr->port_sc1, STS);
3327ae18f37SLucas Stach 
3337ae18f37SLucas Stach 	/* Deassert power down state */
3347ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
3357ae18f37SLucas Stach 		UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
3367ae18f37SLucas Stach 	clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
3377ae18f37SLucas Stach 		UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
3387ae18f37SLucas Stach 
3397ae18f37SLucas Stach 	return 0;
3407ae18f37SLucas Stach }
3417ae18f37SLucas Stach 
3427ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI
3437ae18f37SLucas Stach /* if board file does not set a ULPI reference frequency we default to 24MHz */
3447ae18f37SLucas Stach #ifndef CONFIG_ULPI_REF_CLK
3457ae18f37SLucas Stach #define CONFIG_ULPI_REF_CLK 24000000
3467ae18f37SLucas Stach #endif
3477ae18f37SLucas Stach 
3487ae18f37SLucas Stach /* set up the ULPI USB controller with the parameters provided */
3497ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
3507ae18f37SLucas Stach {
3517ae18f37SLucas Stach 	u32 val;
3527ae18f37SLucas Stach 	int loop_count;
3537ae18f37SLucas Stach 	struct ulpi_viewport ulpi_vp;
3547ae18f37SLucas Stach 	struct usb_ctlr *usbctlr = config->reg;
3557ae18f37SLucas Stach 
3567ae18f37SLucas Stach 	/* set up ULPI reference clock on pllp_out4 */
3577ae18f37SLucas Stach 	clock_enable(PERIPH_ID_DEV2_OUT);
3587ae18f37SLucas Stach 	clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
3597ae18f37SLucas Stach 
3607ae18f37SLucas Stach 	/* reset ULPI phy */
3617ae18f37SLucas Stach 	if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
3627ae18f37SLucas Stach 		fdtdec_setup_gpio(&config->phy_reset_gpio);
3637ae18f37SLucas Stach 		gpio_direction_output(config->phy_reset_gpio.gpio, 0);
3647ae18f37SLucas Stach 		mdelay(5);
3657ae18f37SLucas Stach 		gpio_set_value(config->phy_reset_gpio.gpio, 1);
3667ae18f37SLucas Stach 	}
3677ae18f37SLucas Stach 
3687ae18f37SLucas Stach 	/* Reset the usb controller */
3697ae18f37SLucas Stach 	clock_enable(config->periph_id);
3707ae18f37SLucas Stach 	usbf_reset_controller(config, usbctlr);
3717ae18f37SLucas Stach 
3727ae18f37SLucas Stach 	/* enable pinmux bypass */
3737ae18f37SLucas Stach 	setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
3747ae18f37SLucas Stach 			ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
3757ae18f37SLucas Stach 
3767ae18f37SLucas Stach 	/* Select ULPI parallel interface */
3777ae18f37SLucas Stach 	clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
3787ae18f37SLucas Stach 
3797ae18f37SLucas Stach 	/* enable ULPI transceiver */
3807ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
3817ae18f37SLucas Stach 
3827ae18f37SLucas Stach 	/* configure ULPI transceiver timings */
3837ae18f37SLucas Stach 	val = 0;
3847ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
3857ae18f37SLucas Stach 
3867ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_SEL(4);
3877ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
3887ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_SEL(4);
3897ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
3907ae18f37SLucas Stach 	udelay(10);
3917ae18f37SLucas Stach 
3927ae18f37SLucas Stach 	val |= ULPI_DATA_TRIMMER_LOAD;
3937ae18f37SLucas Stach 	val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
3947ae18f37SLucas Stach 	val |= ULPI_DIR_TRIMMER_LOAD;
3957ae18f37SLucas Stach 	writel(val, &usbctlr->ulpi_timing_ctrl_1);
3967ae18f37SLucas Stach 
3977ae18f37SLucas Stach 	/* set up phy for host operation with external vbus supply */
3987ae18f37SLucas Stach 	ulpi_vp.port_num = 0;
3997ae18f37SLucas Stach 	ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
4007ae18f37SLucas Stach 
4017ae18f37SLucas Stach 	if (ulpi_init(&ulpi_vp)) {
4027ae18f37SLucas Stach 		printf("Tegra ULPI viewport init failed\n");
4037ae18f37SLucas Stach 		return -1;
4047ae18f37SLucas Stach 	}
4057ae18f37SLucas Stach 
4067ae18f37SLucas Stach 	ulpi_set_vbus(&ulpi_vp, 1, 1);
4077ae18f37SLucas Stach 	ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
4087ae18f37SLucas Stach 
4097ae18f37SLucas Stach 	/* enable wakeup events */
4107ae18f37SLucas Stach 	setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
4117ae18f37SLucas Stach 
4127ae18f37SLucas Stach 	/* Enable and wait for the phy clock to become valid in 100 ms */
4137ae18f37SLucas Stach 	setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
4147ae18f37SLucas Stach 	for (loop_count = 100000; loop_count != 0; loop_count--) {
4157ae18f37SLucas Stach 		if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
4167ae18f37SLucas Stach 			break;
4177ae18f37SLucas Stach 		udelay(1);
4187ae18f37SLucas Stach 	}
4197ae18f37SLucas Stach 	if (!loop_count)
4207ae18f37SLucas Stach 		return -1;
4217ae18f37SLucas Stach 	clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
4227ae18f37SLucas Stach 
4237ae18f37SLucas Stach 	return 0;
4247ae18f37SLucas Stach }
4257ae18f37SLucas Stach #else
4267ae18f37SLucas Stach static int init_ulpi_usb_controller(struct fdt_usb *config)
4277ae18f37SLucas Stach {
4287ae18f37SLucas Stach 	printf("No code to set up ULPI controller, please enable"
4297ae18f37SLucas Stach 			"CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
4307ae18f37SLucas Stach 	return -1;
4317ae18f37SLucas Stach }
4327ae18f37SLucas Stach #endif
4337ae18f37SLucas Stach 
4347ae18f37SLucas Stach static void config_clock(const u32 timing[])
4357ae18f37SLucas Stach {
4367ae18f37SLucas Stach 	clock_start_pll(CLOCK_ID_USB,
4377ae18f37SLucas Stach 		timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
4387ae18f37SLucas Stach 		timing[PARAM_CPCON], timing[PARAM_LFCON]);
4397ae18f37SLucas Stach }
4407ae18f37SLucas Stach 
4417ae18f37SLucas Stach int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
4427ae18f37SLucas Stach {
4437ae18f37SLucas Stach 	const char *phy, *mode;
4447ae18f37SLucas Stach 
4457ae18f37SLucas Stach 	config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
4467ae18f37SLucas Stach 	mode = fdt_getprop(blob, node, "dr_mode", NULL);
4477ae18f37SLucas Stach 	if (mode) {
4487ae18f37SLucas Stach 		if (0 == strcmp(mode, "host"))
4497ae18f37SLucas Stach 			config->dr_mode = DR_MODE_HOST;
4507ae18f37SLucas Stach 		else if (0 == strcmp(mode, "peripheral"))
4517ae18f37SLucas Stach 			config->dr_mode = DR_MODE_DEVICE;
4527ae18f37SLucas Stach 		else if (0 == strcmp(mode, "otg"))
4537ae18f37SLucas Stach 			config->dr_mode = DR_MODE_OTG;
4547ae18f37SLucas Stach 		else {
4557ae18f37SLucas Stach 			debug("%s: Cannot decode dr_mode '%s'\n", __func__,
4567ae18f37SLucas Stach 			      mode);
4577ae18f37SLucas Stach 			return -FDT_ERR_NOTFOUND;
4587ae18f37SLucas Stach 		}
4597ae18f37SLucas Stach 	} else {
4607ae18f37SLucas Stach 		config->dr_mode = DR_MODE_HOST;
4617ae18f37SLucas Stach 	}
4627ae18f37SLucas Stach 
4637ae18f37SLucas Stach 	phy = fdt_getprop(blob, node, "phy_type", NULL);
4647ae18f37SLucas Stach 	config->utmi = phy && 0 == strcmp("utmi", phy);
4657ae18f37SLucas Stach 	config->ulpi = phy && 0 == strcmp("ulpi", phy);
4667ae18f37SLucas Stach 	config->enabled = fdtdec_get_is_enabled(blob, node);
4677ae18f37SLucas Stach 	config->has_legacy_mode = fdtdec_get_bool(blob, node,
4687ae18f37SLucas Stach 						  "nvidia,has-legacy-mode");
4697ae18f37SLucas Stach 	config->periph_id = clock_decode_periph_id(blob, node);
4707ae18f37SLucas Stach 	if (config->periph_id == PERIPH_ID_NONE) {
4717ae18f37SLucas Stach 		debug("%s: Missing/invalid peripheral ID\n", __func__);
4727ae18f37SLucas Stach 		return -FDT_ERR_NOTFOUND;
4737ae18f37SLucas Stach 	}
4747ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
4757ae18f37SLucas Stach 	fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
4767ae18f37SLucas Stach 			&config->phy_reset_gpio);
4777ae18f37SLucas Stach 	debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
4787ae18f37SLucas Stach 		"vbus=%d, phy_reset=%d, dr_mode=%d\n",
4797ae18f37SLucas Stach 		config->enabled, config->has_legacy_mode, config->utmi,
4807ae18f37SLucas Stach 		config->ulpi, config->periph_id, config->vbus_gpio.gpio,
4817ae18f37SLucas Stach 		config->phy_reset_gpio.gpio, config->dr_mode);
4827ae18f37SLucas Stach 
4837ae18f37SLucas Stach 	return 0;
4847ae18f37SLucas Stach }
4857ae18f37SLucas Stach 
4867ae18f37SLucas Stach int board_usb_init(const void *blob)
4877ae18f37SLucas Stach {
4887ae18f37SLucas Stach 	struct fdt_usb config;
4897ae18f37SLucas Stach 	enum clock_osc_freq freq;
4907ae18f37SLucas Stach 	int node_list[USB_PORTS_MAX];
4917ae18f37SLucas Stach 	int node, count, i;
4927ae18f37SLucas Stach 
4937ae18f37SLucas Stach 	/* Set up the USB clocks correctly based on our oscillator frequency */
4947ae18f37SLucas Stach 	freq = clock_get_osc_freq();
4957ae18f37SLucas Stach 	config_clock(usb_pll[freq]);
4967ae18f37SLucas Stach 
4977ae18f37SLucas Stach 	/* count may return <0 on error */
4987ae18f37SLucas Stach 	count = fdtdec_find_aliases_for_id(blob, "usb",
4997ae18f37SLucas Stach 			COMPAT_NVIDIA_TEGRA20_USB, node_list, USB_PORTS_MAX);
5007ae18f37SLucas Stach 	for (i = 0; i < count; i++) {
5017ae18f37SLucas Stach 		if (port_count == USB_PORTS_MAX) {
5027ae18f37SLucas Stach 			printf("tegrausb: Cannot register more than %d ports\n",
5037ae18f37SLucas Stach 				USB_PORTS_MAX);
5047ae18f37SLucas Stach 			return -1;
5057ae18f37SLucas Stach 		}
5067ae18f37SLucas Stach 
5077ae18f37SLucas Stach 		debug("USB %d: ", i);
5087ae18f37SLucas Stach 		node = node_list[i];
5097ae18f37SLucas Stach 		if (!node)
5107ae18f37SLucas Stach 			continue;
5117ae18f37SLucas Stach 		if (fdt_decode_usb(blob, node, &config)) {
5127ae18f37SLucas Stach 			debug("Cannot decode USB node %s\n",
5137ae18f37SLucas Stach 			      fdt_get_name(blob, node, NULL));
5147ae18f37SLucas Stach 			return -1;
5157ae18f37SLucas Stach 		}
5167ae18f37SLucas Stach 		config.initialized = 0;
5177ae18f37SLucas Stach 
5187ae18f37SLucas Stach 		/* add new USB port to the list of available ports */
5197ae18f37SLucas Stach 		port[port_count++] = config;
5207ae18f37SLucas Stach 	}
5217ae18f37SLucas Stach 
5227ae18f37SLucas Stach 	return 0;
5237ae18f37SLucas Stach }
5247ae18f37SLucas Stach 
525*d7a55e1aSLucas Stach /**
526*d7a55e1aSLucas Stach  * Start up the given port number (ports are numbered from 0 on each board).
527*d7a55e1aSLucas Stach  * This returns values for the appropriate hccr and hcor addresses to use for
528*d7a55e1aSLucas Stach  * USB EHCI operations.
529*d7a55e1aSLucas Stach  *
530*d7a55e1aSLucas Stach  * @param index	port number to start
531*d7a55e1aSLucas Stach  * @param hccr		returns start address of EHCI HCCR registers
532*d7a55e1aSLucas Stach  * @param hcor		returns start address of EHCI HCOR registers
533*d7a55e1aSLucas Stach  * @return 0 if ok, -1 on error (generally invalid port number)
53487f938c9SSimon Glass  */
535676ae068SLucas Stach int ehci_hcd_init(int index, struct ehci_hccr **hccr, struct ehci_hcor **hcor)
53687f938c9SSimon Glass {
537*d7a55e1aSLucas Stach 	struct fdt_usb *config;
538*d7a55e1aSLucas Stach 	struct usb_ctlr *usbctlr;
53987f938c9SSimon Glass 
540*d7a55e1aSLucas Stach 	if (index >= port_count)
54187f938c9SSimon Glass 		return -1;
54287f938c9SSimon Glass 
543*d7a55e1aSLucas Stach 	config = &port[index];
54487f938c9SSimon Glass 
545*d7a55e1aSLucas Stach 	/* skip init, if the port is already initialized */
546*d7a55e1aSLucas Stach 	if (config->initialized)
547*d7a55e1aSLucas Stach 		goto success;
548*d7a55e1aSLucas Stach 
549*d7a55e1aSLucas Stach 	if (config->utmi && init_utmi_usb_controller(config)) {
550*d7a55e1aSLucas Stach 		printf("tegrausb: Cannot init port %d\n", index);
551*d7a55e1aSLucas Stach 		return -1;
552*d7a55e1aSLucas Stach 	}
553*d7a55e1aSLucas Stach 
554*d7a55e1aSLucas Stach 	if (config->ulpi && init_ulpi_usb_controller(config)) {
555*d7a55e1aSLucas Stach 		printf("tegrausb: Cannot init port %d\n", index);
556*d7a55e1aSLucas Stach 		return -1;
557*d7a55e1aSLucas Stach 	}
558*d7a55e1aSLucas Stach 
559*d7a55e1aSLucas Stach 	set_host_mode(config);
560*d7a55e1aSLucas Stach 
561*d7a55e1aSLucas Stach 	config->initialized = 1;
562*d7a55e1aSLucas Stach 
563*d7a55e1aSLucas Stach success:
564*d7a55e1aSLucas Stach 	usbctlr = config->reg;
565*d7a55e1aSLucas Stach 	*hccr = (struct ehci_hccr *)&usbctlr->cap_length;
566*d7a55e1aSLucas Stach 	*hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
56787f938c9SSimon Glass 	return 0;
56887f938c9SSimon Glass }
56987f938c9SSimon Glass 
57087f938c9SSimon Glass /*
571*d7a55e1aSLucas Stach  * Bring down the specified USB controller
57287f938c9SSimon Glass  */
573676ae068SLucas Stach int ehci_hcd_stop(int index)
57487f938c9SSimon Glass {
575*d7a55e1aSLucas Stach 	struct usb_ctlr *usbctlr;
576*d7a55e1aSLucas Stach 
577*d7a55e1aSLucas Stach 	usbctlr = port[index].reg;
578*d7a55e1aSLucas Stach 
579*d7a55e1aSLucas Stach 	/* Stop controller */
580*d7a55e1aSLucas Stach 	writel(0, &usbctlr->usb_cmd);
581*d7a55e1aSLucas Stach 	udelay(1000);
582*d7a55e1aSLucas Stach 
583*d7a55e1aSLucas Stach 	/* Initiate controller reset */
584*d7a55e1aSLucas Stach 	writel(2, &usbctlr->usb_cmd);
585*d7a55e1aSLucas Stach 	udelay(1000);
586*d7a55e1aSLucas Stach 
587*d7a55e1aSLucas Stach 	port[index].initialized = 0;
588*d7a55e1aSLucas Stach 
589*d7a55e1aSLucas Stach 	return 0;
59087f938c9SSimon Glass }
591