187f938c9SSimon Glass /* 27ae18f37SLucas Stach * Copyright (c) 2011 The Chromium OS Authors. 3*7aaa5a60STom Warren * Copyright (c) 2009-2015 NVIDIA Corporation 47ae18f37SLucas Stach * Copyright (c) 2013 Lucas Stach 587f938c9SSimon Glass * 61a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 787f938c9SSimon Glass */ 887f938c9SSimon Glass 987f938c9SSimon Glass #include <common.h> 10c3980ad3SSimon Glass #include <dm.h> 117ae18f37SLucas Stach #include <asm/errno.h> 127ae18f37SLucas Stach #include <asm/io.h> 137ae18f37SLucas Stach #include <asm-generic/gpio.h> 147ae18f37SLucas Stach #include <asm/arch/clock.h> 157ae18f37SLucas Stach #include <asm/arch-tegra/usb.h> 167e44d932SJim Lin #include <asm/arch-tegra/clk_rst.h> 1787f938c9SSimon Glass #include <usb.h> 187ae18f37SLucas Stach #include <usb/ulpi.h> 197ae18f37SLucas Stach #include <libfdt.h> 207ae18f37SLucas Stach #include <fdtdec.h> 2187f938c9SSimon Glass 2287f938c9SSimon Glass #include "ehci.h" 2387f938c9SSimon Glass 24c3980ad3SSimon Glass DECLARE_GLOBAL_DATA_PTR; 25c3980ad3SSimon Glass 267e44d932SJim Lin #define USB1_ADDR_MASK 0xFFFF0000 277e44d932SJim Lin 287e44d932SJim Lin #define HOSTPC1_DEVLC 0x84 297e44d932SJim Lin #define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3) 307e44d932SJim Lin 317ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI 327ae18f37SLucas Stach #ifndef CONFIG_USB_ULPI_VIEWPORT 337ae18f37SLucas Stach #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \ 347ae18f37SLucas Stach define CONFIG_USB_ULPI_VIEWPORT" 357ae18f37SLucas Stach #endif 367ae18f37SLucas Stach #endif 377ae18f37SLucas Stach 387ae18f37SLucas Stach /* Parameters we need for USB */ 397ae18f37SLucas Stach enum { 407ae18f37SLucas Stach PARAM_DIVN, /* PLL FEEDBACK DIVIDer */ 417ae18f37SLucas Stach PARAM_DIVM, /* PLL INPUT DIVIDER */ 427ae18f37SLucas Stach PARAM_DIVP, /* POST DIVIDER (2^N) */ 437ae18f37SLucas Stach PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */ 447ae18f37SLucas Stach PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */ 457ae18f37SLucas Stach PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */ 467ae18f37SLucas Stach PARAM_STABLE_COUNT, /* PLL-U STABLE count */ 477ae18f37SLucas Stach PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */ 487ae18f37SLucas Stach PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */ 497ae18f37SLucas Stach PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */ 507ae18f37SLucas Stach PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */ 517ae18f37SLucas Stach 527ae18f37SLucas Stach PARAM_COUNT 537ae18f37SLucas Stach }; 547ae18f37SLucas Stach 557ae18f37SLucas Stach /* Possible port types (dual role mode) */ 567ae18f37SLucas Stach enum dr_mode { 577ae18f37SLucas Stach DR_MODE_NONE = 0, 587ae18f37SLucas Stach DR_MODE_HOST, /* supports host operation */ 597ae18f37SLucas Stach DR_MODE_DEVICE, /* supports device operation */ 607ae18f37SLucas Stach DR_MODE_OTG, /* supports both */ 617ae18f37SLucas Stach }; 627ae18f37SLucas Stach 6327f782b6SSimon Glass enum usb_ctlr_type { 6427f782b6SSimon Glass USB_CTLR_T20, 6527f782b6SSimon Glass USB_CTLR_T30, 6627f782b6SSimon Glass USB_CTLR_T114, 67*7aaa5a60STom Warren USB_CTLR_T210, 6827f782b6SSimon Glass 6927f782b6SSimon Glass USB_CTRL_COUNT, 7027f782b6SSimon Glass }; 7127f782b6SSimon Glass 727ae18f37SLucas Stach /* Information about a USB port */ 737ae18f37SLucas Stach struct fdt_usb { 74c3980ad3SSimon Glass struct ehci_ctrl ehci; 757ae18f37SLucas Stach struct usb_ctlr *reg; /* address of registers in physical memory */ 767ae18f37SLucas Stach unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */ 777ae18f37SLucas Stach unsigned ulpi:1; /* 1 if port has external ULPI transceiver */ 787ae18f37SLucas Stach unsigned enabled:1; /* 1 to enable, 0 to disable */ 797ae18f37SLucas Stach unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */ 8027f782b6SSimon Glass enum usb_ctlr_type type; 81a4539a2aSStephen Warren enum usb_init_type init_type; 827ae18f37SLucas Stach enum dr_mode dr_mode; /* dual role mode */ 837ae18f37SLucas Stach enum periph_id periph_id;/* peripheral id */ 8446927e1eSSimon Glass struct gpio_desc vbus_gpio; /* GPIO for vbus enable */ 8546927e1eSSimon Glass struct gpio_desc phy_reset_gpio; /* GPIO to reset ULPI phy */ 867ae18f37SLucas Stach }; 877ae18f37SLucas Stach 887ae18f37SLucas Stach /* 897ae18f37SLucas Stach * This table has USB timing parameters for each Oscillator frequency we 907ae18f37SLucas Stach * support. There are four sets of values: 917ae18f37SLucas Stach * 927ae18f37SLucas Stach * 1. PLLU configuration information (reference clock is osc/clk_m and 937ae18f37SLucas Stach * PLLU-FOs are fixed at 12MHz/60MHz/480MHz). 947ae18f37SLucas Stach * 957ae18f37SLucas Stach * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz 967ae18f37SLucas Stach * ---------------------------------------------------------------------- 977ae18f37SLucas Stach * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0) 987ae18f37SLucas Stach * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a) 997ae18f37SLucas Stach * Filter frequency (MHz) 1 4.8 6 2 1007ae18f37SLucas Stach * CPCON 1100b 0011b 1100b 1100b 1017ae18f37SLucas Stach * LFCON0 0 0 0 0 1027ae18f37SLucas Stach * 1037ae18f37SLucas Stach * 2. PLL CONFIGURATION & PARAMETERS for different clock generators: 1047ae18f37SLucas Stach * 1057ae18f37SLucas Stach * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz 1067ae18f37SLucas Stach * --------------------------------------------------------------------------- 1077ae18f37SLucas Stach * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04) 1087ae18f37SLucas Stach * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66) 1097ae18f37SLucas Stach * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09) 1107ae18f37SLucas Stach * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE) 1117ae18f37SLucas Stach * 1127ae18f37SLucas Stach * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and 1137ae18f37SLucas Stach * SessEnd. Each of these signals have their own debouncer and for each of 1147ae18f37SLucas Stach * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or 1157ae18f37SLucas Stach * BIAS_DEBOUNCE_B). 1167ae18f37SLucas Stach * 1177ae18f37SLucas Stach * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows: 1187ae18f37SLucas Stach * 0xffff -> No debouncing at all 1197ae18f37SLucas Stach * <n> ms = <n> *1000 / (1/19.2MHz) / 4 1207ae18f37SLucas Stach * 1217ae18f37SLucas Stach * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have: 1227ae18f37SLucas Stach * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0 1237ae18f37SLucas Stach * 1247ae18f37SLucas Stach * We need to use only DebounceA for BOOTROM. We don't need the DebounceB 1257ae18f37SLucas Stach * values, so we can keep those to default. 1267ae18f37SLucas Stach * 1277ae18f37SLucas Stach * 4. The 20 microsecond delay after bias cell operation. 1287ae18f37SLucas Stach */ 1297e44d932SJim Lin static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = { 1307ae18f37SLucas Stach /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */ 1317ae18f37SLucas Stach { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 }, 1327ae18f37SLucas Stach { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 }, 1337ae18f37SLucas Stach { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 }, 1347ae18f37SLucas Stach { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 } 1357ae18f37SLucas Stach }; 1367ae18f37SLucas Stach 1377e44d932SJim Lin static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = { 1387e44d932SJim Lin /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */ 1397e44d932SJim Lin { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 }, 1407e44d932SJim Lin { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 }, 1417e44d932SJim Lin { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 }, 1427e44d932SJim Lin { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 } 1437e44d932SJim Lin }; 1447e44d932SJim Lin 1457e44d932SJim Lin static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = { 1467e44d932SJim Lin /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */ 1477e44d932SJim Lin { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 }, 1487e44d932SJim Lin { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 }, 1497e44d932SJim Lin { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 }, 1507e44d932SJim Lin { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB } 1517e44d932SJim Lin }; 1527e44d932SJim Lin 153*7aaa5a60STom Warren /* NOTE: 13/26MHz settings are N/A for T210, so dupe 12MHz settings for now */ 154*7aaa5a60STom Warren static const unsigned T210_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = { 155*7aaa5a60STom Warren /* DivN, DivM, DivP, KCP, KVCO, Delays Debounce, Bias */ 156*7aaa5a60STom Warren { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 30000, 5 }, 157*7aaa5a60STom Warren { 0x019, 0x01, 0x01, 0x0, 0, 0x03, 0x4B, 0x0C, 0xBB, 48000, 8 }, 158*7aaa5a60STom Warren { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 30000, 5 }, 159*7aaa5a60STom Warren { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 30000, 5 }, 160*7aaa5a60STom Warren }; 161*7aaa5a60STom Warren 1627ae18f37SLucas Stach /* UTMIP Idle Wait Delay */ 1637ae18f37SLucas Stach static const u8 utmip_idle_wait_delay = 17; 1647ae18f37SLucas Stach 1657ae18f37SLucas Stach /* UTMIP Elastic limit */ 1667ae18f37SLucas Stach static const u8 utmip_elastic_limit = 16; 1677ae18f37SLucas Stach 1687ae18f37SLucas Stach /* UTMIP High Speed Sync Start Delay */ 1697ae18f37SLucas Stach static const u8 utmip_hs_sync_start_delay = 9; 17087f938c9SSimon Glass 1717e44d932SJim Lin struct fdt_usb_controller { 1727e44d932SJim Lin /* flag to determine whether controller supports hostpc register */ 1737e44d932SJim Lin u32 has_hostpc:1; 1747e44d932SJim Lin const unsigned *pll_parameter; 1757e44d932SJim Lin }; 1767e44d932SJim Lin 17727f782b6SSimon Glass static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = { 1787e44d932SJim Lin { 1797e44d932SJim Lin .has_hostpc = 0, 1807e44d932SJim Lin .pll_parameter = (const unsigned *)T20_usb_pll, 1817e44d932SJim Lin }, 1827e44d932SJim Lin { 1837e44d932SJim Lin .has_hostpc = 1, 1847e44d932SJim Lin .pll_parameter = (const unsigned *)T30_usb_pll, 1857e44d932SJim Lin }, 1867e44d932SJim Lin { 1877e44d932SJim Lin .has_hostpc = 1, 1887e44d932SJim Lin .pll_parameter = (const unsigned *)T114_usb_pll, 1897e44d932SJim Lin }, 190*7aaa5a60STom Warren { 191*7aaa5a60STom Warren .has_hostpc = 1, 192*7aaa5a60STom Warren .pll_parameter = (const unsigned *)T210_usb_pll, 193*7aaa5a60STom Warren }, 1947e44d932SJim Lin }; 1957e44d932SJim Lin 1968b3f7bf7SJim Lin /* 1978b3f7bf7SJim Lin * A known hardware issue where Connect Status Change bit of PORTSC register 1988b3f7bf7SJim Lin * of USB1 controller will be set after Port Reset. 1998b3f7bf7SJim Lin * We have to clear it in order for later device enumeration to proceed. 2008b3f7bf7SJim Lin */ 201deb8508cSSimon Glass static void tegra_ehci_powerup_fixup(struct ehci_ctrl *ctrl, 202deb8508cSSimon Glass uint32_t *status_reg, uint32_t *reg) 2038b3f7bf7SJim Lin { 20456d42730SSimon Glass struct fdt_usb *config = ctrl->priv; 20556d42730SSimon Glass struct fdt_usb_controller *controller; 20656d42730SSimon Glass 20756d42730SSimon Glass controller = &fdt_usb_controllers[config->type]; 2088b3f7bf7SJim Lin mdelay(50); 2097e44d932SJim Lin /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */ 2107e44d932SJim Lin if (controller->has_hostpc) 2117e44d932SJim Lin *reg |= EHCI_PS_PE; 2127e44d932SJim Lin 213943104f0SSimon Glass if (!config->has_legacy_mode) 2148b3f7bf7SJim Lin return; 2158b3f7bf7SJim Lin /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */ 2168b3f7bf7SJim Lin if (ehci_readl(status_reg) & EHCI_PS_CSC) 2178b3f7bf7SJim Lin *reg |= EHCI_PS_CSC; 2188b3f7bf7SJim Lin } 21987f938c9SSimon Glass 220deb8508cSSimon Glass static void tegra_ehci_set_usbmode(struct ehci_ctrl *ctrl) 2217e44d932SJim Lin { 22211d18a19SSimon Glass struct fdt_usb *config = ctrl->priv; 2237e44d932SJim Lin struct usb_ctlr *usbctlr; 2247e44d932SJim Lin uint32_t tmp; 2257e44d932SJim Lin 2267e44d932SJim Lin usbctlr = config->reg; 2277e44d932SJim Lin 2287e44d932SJim Lin tmp = ehci_readl(&usbctlr->usb_mode); 2297e44d932SJim Lin tmp |= USBMODE_CM_HC; 2307e44d932SJim Lin ehci_writel(&usbctlr->usb_mode, tmp); 2317e44d932SJim Lin } 2327e44d932SJim Lin 233deb8508cSSimon Glass static int tegra_ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg) 2347e44d932SJim Lin { 23556d42730SSimon Glass struct fdt_usb *config = ctrl->priv; 23656d42730SSimon Glass struct fdt_usb_controller *controller; 2377e44d932SJim Lin uint32_t tmp; 2387e44d932SJim Lin uint32_t *reg_ptr; 2397e44d932SJim Lin 24056d42730SSimon Glass controller = &fdt_usb_controllers[config->type]; 2417e44d932SJim Lin if (controller->has_hostpc) { 2427338287dSSimon Glass reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + 2437338287dSSimon Glass HOSTPC1_DEVLC); 2447e44d932SJim Lin tmp = ehci_readl(reg_ptr); 2457e44d932SJim Lin return HOSTPC1_PSPD(tmp); 2467e44d932SJim Lin } else 2477e44d932SJim Lin return PORTSC_PSPD(reg); 2487e44d932SJim Lin } 2497e44d932SJim Lin 250a4539a2aSStephen Warren /* Set up VBUS for host/device mode */ 251a4539a2aSStephen Warren static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init) 2527ae18f37SLucas Stach { 2537ae18f37SLucas Stach /* 254a4539a2aSStephen Warren * If we are an OTG port initializing in host mode, 255a4539a2aSStephen Warren * check if remote host is driving VBus and bail out in this case. 2567ae18f37SLucas Stach */ 257a4539a2aSStephen Warren if (init == USB_INIT_HOST && 258a4539a2aSStephen Warren config->dr_mode == DR_MODE_OTG && 259a4539a2aSStephen Warren (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) { 260a4539a2aSStephen Warren printf("tegrausb: VBUS input active; not enabling as host\n"); 2617ae18f37SLucas Stach return; 262a4539a2aSStephen Warren } 2637ae18f37SLucas Stach 26446927e1eSSimon Glass if (dm_gpio_is_valid(&config->vbus_gpio)) { 265a4539a2aSStephen Warren int vbus_value; 266a4539a2aSStephen Warren 26746927e1eSSimon Glass vbus_value = (init == USB_INIT_HOST); 26846927e1eSSimon Glass dm_gpio_set_value(&config->vbus_gpio, vbus_value); 269a4539a2aSStephen Warren 27046927e1eSSimon Glass debug("set_up_vbus: GPIO %d %d\n", 27146927e1eSSimon Glass gpio_get_number(&config->vbus_gpio), vbus_value); 2727ae18f37SLucas Stach } 2737ae18f37SLucas Stach } 2747ae18f37SLucas Stach 2757e27bddaSSimon Glass static void usbf_reset_controller(struct fdt_usb *config, 2767e27bddaSSimon Glass struct usb_ctlr *usbctlr) 2777ae18f37SLucas Stach { 2787ae18f37SLucas Stach /* Reset the USB controller with 2us delay */ 2797ae18f37SLucas Stach reset_periph(config->periph_id, 2); 2807ae18f37SLucas Stach 2817ae18f37SLucas Stach /* 2827ae18f37SLucas Stach * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under 2837ae18f37SLucas Stach * base address 2847ae18f37SLucas Stach */ 2857ae18f37SLucas Stach if (config->has_legacy_mode) 2867ae18f37SLucas Stach setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE); 2877ae18f37SLucas Stach 2887ae18f37SLucas Stach /* Put UTMIP1/3 in reset */ 2897ae18f37SLucas Stach setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET); 2907ae18f37SLucas Stach 2917ae18f37SLucas Stach /* Enable the UTMIP PHY */ 2927ae18f37SLucas Stach if (config->utmi) 2937ae18f37SLucas Stach setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB); 2947ae18f37SLucas Stach } 2957ae18f37SLucas Stach 29627f782b6SSimon Glass static const unsigned *get_pll_timing(struct fdt_usb_controller *controller) 2977e44d932SJim Lin { 2987e44d932SJim Lin const unsigned *timing; 2997e44d932SJim Lin 3007e44d932SJim Lin timing = controller->pll_parameter + 3017e44d932SJim Lin clock_get_osc_freq() * PARAM_COUNT; 3027e44d932SJim Lin 3037e44d932SJim Lin return timing; 3047e44d932SJim Lin } 3057e44d932SJim Lin 3062d34151fSStephen Warren /* select the PHY to use with a USB controller */ 307a4539a2aSStephen Warren static void init_phy_mux(struct fdt_usb *config, uint pts, 308a4539a2aSStephen Warren enum usb_init_type init) 3092d34151fSStephen Warren { 3102d34151fSStephen Warren struct usb_ctlr *usbctlr = config->reg; 3112d34151fSStephen Warren 3122d34151fSStephen Warren #if defined(CONFIG_TEGRA20) 3132d34151fSStephen Warren if (config->periph_id == PERIPH_ID_USBD) { 3142d34151fSStephen Warren clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK, 315d1fcbae1SMarcel Ziswiler pts << PTS1_SHIFT); 3162d34151fSStephen Warren clrbits_le32(&usbctlr->port_sc1, STS1); 3172d34151fSStephen Warren } else { 3182d34151fSStephen Warren clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, 319d1fcbae1SMarcel Ziswiler pts << PTS_SHIFT); 3202d34151fSStephen Warren clrbits_le32(&usbctlr->port_sc1, STS); 3212d34151fSStephen Warren } 3222d34151fSStephen Warren #else 323a4539a2aSStephen Warren /* Set to Host mode (if applicable) after Controller Reset was done */ 3242d34151fSStephen Warren clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC, 325a4539a2aSStephen Warren (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0); 326a4539a2aSStephen Warren /* 327a4539a2aSStephen Warren * Select PHY interface after setting host mode. 328a4539a2aSStephen Warren * For device mode, the ordering requirement is not an issue, since 329a4539a2aSStephen Warren * only the first USB controller supports device mode, and that USB 330a4539a2aSStephen Warren * controller can only talk to a UTMI PHY, so the PHY selection is 331a4539a2aSStephen Warren * already made at reset time, so this write is a no-op. 332a4539a2aSStephen Warren */ 3332d34151fSStephen Warren clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK, 3342d34151fSStephen Warren pts << PTS_SHIFT); 3352d34151fSStephen Warren clrbits_le32(&usbctlr->hostpc1_devlc, STS); 3362d34151fSStephen Warren #endif 3372d34151fSStephen Warren } 3382d34151fSStephen Warren 3397ae18f37SLucas Stach /* set up the UTMI USB controller with the parameters provided */ 340a4539a2aSStephen Warren static int init_utmi_usb_controller(struct fdt_usb *config, 341a4539a2aSStephen Warren enum usb_init_type init) 3427ae18f37SLucas Stach { 34327f782b6SSimon Glass struct fdt_usb_controller *controller; 344a4539a2aSStephen Warren u32 b_sess_valid_mask, val; 3457ae18f37SLucas Stach int loop_count; 3467ae18f37SLucas Stach const unsigned *timing; 3477ae18f37SLucas Stach struct usb_ctlr *usbctlr = config->reg; 3487e44d932SJim Lin struct clk_rst_ctlr *clkrst; 3497e44d932SJim Lin struct usb_ctlr *usb1ctlr; 3507ae18f37SLucas Stach 3517ae18f37SLucas Stach clock_enable(config->periph_id); 3527ae18f37SLucas Stach 3537ae18f37SLucas Stach /* Reset the usb controller */ 3547ae18f37SLucas Stach usbf_reset_controller(config, usbctlr); 3557ae18f37SLucas Stach 3567ae18f37SLucas Stach /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */ 3577ae18f37SLucas Stach clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN); 3587ae18f37SLucas Stach 3597ae18f37SLucas Stach /* Follow the crystal clock disable by >100ns delay */ 3607ae18f37SLucas Stach udelay(1); 3617ae18f37SLucas Stach 362a4539a2aSStephen Warren b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN); 363a4539a2aSStephen Warren clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask, 364a4539a2aSStephen Warren (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0); 365a4539a2aSStephen Warren 3667ae18f37SLucas Stach /* 3677ae18f37SLucas Stach * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP 3687ae18f37SLucas Stach * mux must be switched to actually use a_sess_vld threshold. 3697ae18f37SLucas Stach */ 3707e44d932SJim Lin if (config->dr_mode == DR_MODE_OTG && 37146927e1eSSimon Glass dm_gpio_is_valid(&config->vbus_gpio)) 3727ae18f37SLucas Stach clrsetbits_le32(&usbctlr->usb1_legacy_ctrl, 3737ae18f37SLucas Stach VBUS_SENSE_CTL_MASK, 3747ae18f37SLucas Stach VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT); 3757ae18f37SLucas Stach 37627f782b6SSimon Glass controller = &fdt_usb_controllers[config->type]; 37727f782b6SSimon Glass debug("controller=%p, type=%d\n", controller, config->type); 37827f782b6SSimon Glass 3797ae18f37SLucas Stach /* 3807ae18f37SLucas Stach * PLL Delay CONFIGURATION settings. The following parameters control 3817ae18f37SLucas Stach * the bring up of the plls. 3827ae18f37SLucas Stach */ 38327f782b6SSimon Glass timing = get_pll_timing(controller); 3847ae18f37SLucas Stach 3857e44d932SJim Lin if (!controller->has_hostpc) { 3867ae18f37SLucas Stach val = readl(&usbctlr->utmip_misc_cfg1); 3877ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK, 3887e44d932SJim Lin timing[PARAM_STABLE_COUNT] << 3897e44d932SJim Lin UTMIP_PLLU_STABLE_COUNT_SHIFT); 3907ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK, 3917ae18f37SLucas Stach timing[PARAM_ACTIVE_DELAY_COUNT] << 3927ae18f37SLucas Stach UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT); 3937ae18f37SLucas Stach writel(val, &usbctlr->utmip_misc_cfg1); 3947ae18f37SLucas Stach 3957ae18f37SLucas Stach /* Set PLL enable delay count and crystal frequency count */ 3967ae18f37SLucas Stach val = readl(&usbctlr->utmip_pll_cfg1); 3977ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK, 3987ae18f37SLucas Stach timing[PARAM_ENABLE_DELAY_COUNT] << 3997ae18f37SLucas Stach UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT); 4007ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK, 4017ae18f37SLucas Stach timing[PARAM_XTAL_FREQ_COUNT] << 4027ae18f37SLucas Stach UTMIP_XTAL_FREQ_COUNT_SHIFT); 4037ae18f37SLucas Stach writel(val, &usbctlr->utmip_pll_cfg1); 4047e44d932SJim Lin } else { 4057e44d932SJim Lin clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; 4067e44d932SJim Lin 4077e44d932SJim Lin val = readl(&clkrst->crc_utmip_pll_cfg2); 4087e44d932SJim Lin clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK, 4097e44d932SJim Lin timing[PARAM_STABLE_COUNT] << 4107e44d932SJim Lin UTMIP_PLLU_STABLE_COUNT_SHIFT); 4117e44d932SJim Lin clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK, 4127e44d932SJim Lin timing[PARAM_ACTIVE_DELAY_COUNT] << 4137e44d932SJim Lin UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT); 4147e44d932SJim Lin writel(val, &clkrst->crc_utmip_pll_cfg2); 4157e44d932SJim Lin 4167e44d932SJim Lin /* Set PLL enable delay count and crystal frequency count */ 4177e44d932SJim Lin val = readl(&clkrst->crc_utmip_pll_cfg1); 4187e44d932SJim Lin clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK, 4197e44d932SJim Lin timing[PARAM_ENABLE_DELAY_COUNT] << 4207e44d932SJim Lin UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT); 4217e44d932SJim Lin clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK, 4227e44d932SJim Lin timing[PARAM_XTAL_FREQ_COUNT] << 4237e44d932SJim Lin UTMIP_XTAL_FREQ_COUNT_SHIFT); 4247e44d932SJim Lin writel(val, &clkrst->crc_utmip_pll_cfg1); 4257e44d932SJim Lin 4267e44d932SJim Lin /* Disable Power Down state for PLL */ 4277e44d932SJim Lin clrbits_le32(&clkrst->crc_utmip_pll_cfg1, 4287e44d932SJim Lin PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN | 4297e44d932SJim Lin PLL_ACTIVE_POWERDOWN); 4307e44d932SJim Lin 4317e44d932SJim Lin /* Recommended PHY settings for EYE diagram */ 4327e44d932SJim Lin val = readl(&usbctlr->utmip_xcvr_cfg0); 4337e44d932SJim Lin clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK, 4347e44d932SJim Lin 0x4 << UTMIP_XCVR_SETUP_SHIFT); 4357e44d932SJim Lin clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK, 4367e44d932SJim Lin 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT); 4377e44d932SJim Lin clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK, 4387e44d932SJim Lin 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT); 4397e44d932SJim Lin writel(val, &usbctlr->utmip_xcvr_cfg0); 4407e44d932SJim Lin clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1, 4417e44d932SJim Lin UTMIP_XCVR_TERM_RANGE_ADJ_MASK, 4427e44d932SJim Lin 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT); 4437e44d932SJim Lin 4447e44d932SJim Lin /* Some registers can be controlled from USB1 only. */ 4457e44d932SJim Lin if (config->periph_id != PERIPH_ID_USBD) { 4467e44d932SJim Lin clock_enable(PERIPH_ID_USBD); 4477e44d932SJim Lin /* Disable Reset if in Reset state */ 4487e44d932SJim Lin reset_set_enable(PERIPH_ID_USBD, 0); 4497e44d932SJim Lin } 4507e44d932SJim Lin usb1ctlr = (struct usb_ctlr *) 45196df9c7eSThierry Reding ((unsigned long)config->reg & USB1_ADDR_MASK); 4527e44d932SJim Lin val = readl(&usb1ctlr->utmip_bias_cfg0); 4537e44d932SJim Lin setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB); 4547e44d932SJim Lin clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK, 4557e44d932SJim Lin 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT); 4567e44d932SJim Lin clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK, 4577e44d932SJim Lin 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT); 4587e44d932SJim Lin writel(val, &usb1ctlr->utmip_bias_cfg0); 4597e44d932SJim Lin 4607e44d932SJim Lin /* Miscellaneous setting mentioned in Programming Guide */ 4617e44d932SJim Lin clrbits_le32(&usbctlr->utmip_misc_cfg0, 4627e44d932SJim Lin UTMIP_SUSPEND_EXIT_ON_EDGE); 4637e44d932SJim Lin } 4647ae18f37SLucas Stach 4657ae18f37SLucas Stach /* Setting the tracking length time */ 4667ae18f37SLucas Stach clrsetbits_le32(&usbctlr->utmip_bias_cfg1, 4677ae18f37SLucas Stach UTMIP_BIAS_PDTRK_COUNT_MASK, 4687ae18f37SLucas Stach timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT); 4697ae18f37SLucas Stach 4707ae18f37SLucas Stach /* Program debounce time for VBUS to become valid */ 4717ae18f37SLucas Stach clrsetbits_le32(&usbctlr->utmip_debounce_cfg0, 4727ae18f37SLucas Stach UTMIP_DEBOUNCE_CFG0_MASK, 4737ae18f37SLucas Stach timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT); 4747ae18f37SLucas Stach 475*7aaa5a60STom Warren if (timing[PARAM_DEBOUNCE_A_TIME] > 0xFFFF) { 476*7aaa5a60STom Warren clrsetbits_le32(&usbctlr->utmip_debounce_cfg0, 477*7aaa5a60STom Warren UTMIP_DEBOUNCE_CFG0_MASK, 478*7aaa5a60STom Warren (timing[PARAM_DEBOUNCE_A_TIME] >> 1) 479*7aaa5a60STom Warren << UTMIP_DEBOUNCE_CFG0_SHIFT); 480*7aaa5a60STom Warren clrsetbits_le32(&usbctlr->utmip_bias_cfg1, 481*7aaa5a60STom Warren UTMIP_BIAS_DEBOUNCE_TIMESCALE_MASK, 482*7aaa5a60STom Warren 1 << UTMIP_BIAS_DEBOUNCE_TIMESCALE_SHIFT); 483*7aaa5a60STom Warren } 484*7aaa5a60STom Warren 4857ae18f37SLucas Stach setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J); 4867ae18f37SLucas Stach 4877ae18f37SLucas Stach /* Disable battery charge enabling bit */ 4887ae18f37SLucas Stach setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG); 4897ae18f37SLucas Stach 4907ae18f37SLucas Stach clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE); 4917ae18f37SLucas Stach setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL); 4927ae18f37SLucas Stach 4937ae18f37SLucas Stach /* 4947ae18f37SLucas Stach * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT 4957ae18f37SLucas Stach * Setting these fields, together with default values of the 4967ae18f37SLucas Stach * other fields, results in programming the registers below as 4977ae18f37SLucas Stach * follows: 4987ae18f37SLucas Stach * UTMIP_HSRX_CFG0 = 0x9168c000 4997ae18f37SLucas Stach * UTMIP_HSRX_CFG1 = 0x13 5007ae18f37SLucas Stach */ 5017ae18f37SLucas Stach 5027ae18f37SLucas Stach /* Set PLL enable delay count and Crystal frequency count */ 5037ae18f37SLucas Stach val = readl(&usbctlr->utmip_hsrx_cfg0); 5047ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK, 5057ae18f37SLucas Stach utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT); 5067ae18f37SLucas Stach clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK, 5077ae18f37SLucas Stach utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT); 5087ae18f37SLucas Stach writel(val, &usbctlr->utmip_hsrx_cfg0); 5097ae18f37SLucas Stach 5107ae18f37SLucas Stach /* Configure the UTMIP_HS_SYNC_START_DLY */ 5117ae18f37SLucas Stach clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1, 5127ae18f37SLucas Stach UTMIP_HS_SYNC_START_DLY_MASK, 5137ae18f37SLucas Stach utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT); 5147ae18f37SLucas Stach 5157ae18f37SLucas Stach /* Preceed the crystal clock disable by >100ns delay. */ 5167ae18f37SLucas Stach udelay(1); 5177ae18f37SLucas Stach 5187ae18f37SLucas Stach /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */ 5197ae18f37SLucas Stach setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN); 5207ae18f37SLucas Stach 5217e44d932SJim Lin if (controller->has_hostpc) { 5227e44d932SJim Lin if (config->periph_id == PERIPH_ID_USBD) 5237e44d932SJim Lin clrbits_le32(&clkrst->crc_utmip_pll_cfg2, 5247e44d932SJim Lin UTMIP_FORCE_PD_SAMP_A_POWERDOWN); 525b03f4b37SStefan Agner if (config->periph_id == PERIPH_ID_USB2) 526b03f4b37SStefan Agner clrbits_le32(&clkrst->crc_utmip_pll_cfg2, 527b03f4b37SStefan Agner UTMIP_FORCE_PD_SAMP_B_POWERDOWN); 5287e44d932SJim Lin if (config->periph_id == PERIPH_ID_USB3) 5297e44d932SJim Lin clrbits_le32(&clkrst->crc_utmip_pll_cfg2, 5307e44d932SJim Lin UTMIP_FORCE_PD_SAMP_C_POWERDOWN); 5317e44d932SJim Lin } 5327ae18f37SLucas Stach /* Finished the per-controller init. */ 5337ae18f37SLucas Stach 5347ae18f37SLucas Stach /* De-assert UTMIP_RESET to bring out of reset. */ 5357ae18f37SLucas Stach clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET); 5367ae18f37SLucas Stach 5377ae18f37SLucas Stach /* Wait for the phy clock to become valid in 100 ms */ 5387ae18f37SLucas Stach for (loop_count = 100000; loop_count != 0; loop_count--) { 5397ae18f37SLucas Stach if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID) 5407ae18f37SLucas Stach break; 5417ae18f37SLucas Stach udelay(1); 5427ae18f37SLucas Stach } 5437ae18f37SLucas Stach if (!loop_count) 5447e27bddaSSimon Glass return -ETIMEDOUT; 5457ae18f37SLucas Stach 5467ae18f37SLucas Stach /* Disable ICUSB FS/LS transceiver */ 5477ae18f37SLucas Stach clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1); 5487ae18f37SLucas Stach 5497ae18f37SLucas Stach /* Select UTMI parallel interface */ 550a4539a2aSStephen Warren init_phy_mux(config, PTS_UTMI, init); 5517ae18f37SLucas Stach 5527ae18f37SLucas Stach /* Deassert power down state */ 5537ae18f37SLucas Stach clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN | 5547ae18f37SLucas Stach UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN); 5557ae18f37SLucas Stach clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN | 5567ae18f37SLucas Stach UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN); 5577ae18f37SLucas Stach 5587e44d932SJim Lin if (controller->has_hostpc) { 5597e44d932SJim Lin /* 5607e44d932SJim Lin * BIAS Pad Power Down is common among all 3 USB 5617e44d932SJim Lin * controllers and can be controlled from USB1 only. 5627e44d932SJim Lin */ 5637e44d932SJim Lin usb1ctlr = (struct usb_ctlr *) 56496df9c7eSThierry Reding ((unsigned long)config->reg & USB1_ADDR_MASK); 5657e44d932SJim Lin clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD); 5667e44d932SJim Lin udelay(25); 5677e44d932SJim Lin clrbits_le32(&usb1ctlr->utmip_bias_cfg1, 5687e44d932SJim Lin UTMIP_FORCE_PDTRK_POWERDOWN); 5697e44d932SJim Lin } 5707ae18f37SLucas Stach return 0; 5717ae18f37SLucas Stach } 5727ae18f37SLucas Stach 5737ae18f37SLucas Stach #ifdef CONFIG_USB_ULPI 5747ae18f37SLucas Stach /* if board file does not set a ULPI reference frequency we default to 24MHz */ 5757ae18f37SLucas Stach #ifndef CONFIG_ULPI_REF_CLK 5767ae18f37SLucas Stach #define CONFIG_ULPI_REF_CLK 24000000 5777ae18f37SLucas Stach #endif 5787ae18f37SLucas Stach 5797ae18f37SLucas Stach /* set up the ULPI USB controller with the parameters provided */ 580a4539a2aSStephen Warren static int init_ulpi_usb_controller(struct fdt_usb *config, 581a4539a2aSStephen Warren enum usb_init_type init) 5827ae18f37SLucas Stach { 5837ae18f37SLucas Stach u32 val; 5847ae18f37SLucas Stach int loop_count; 5857ae18f37SLucas Stach struct ulpi_viewport ulpi_vp; 5867ae18f37SLucas Stach struct usb_ctlr *usbctlr = config->reg; 5877e27bddaSSimon Glass int ret; 5887ae18f37SLucas Stach 5897ae18f37SLucas Stach /* set up ULPI reference clock on pllp_out4 */ 5907ae18f37SLucas Stach clock_enable(PERIPH_ID_DEV2_OUT); 5917ae18f37SLucas Stach clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK); 5927ae18f37SLucas Stach 5937ae18f37SLucas Stach /* reset ULPI phy */ 59446927e1eSSimon Glass if (dm_gpio_is_valid(&config->phy_reset_gpio)) { 59546927e1eSSimon Glass dm_gpio_set_value(&config->phy_reset_gpio, 0); 5967ae18f37SLucas Stach mdelay(5); 59746927e1eSSimon Glass dm_gpio_set_value(&config->phy_reset_gpio, 1); 5987ae18f37SLucas Stach } 5997ae18f37SLucas Stach 6007ae18f37SLucas Stach /* Reset the usb controller */ 6017ae18f37SLucas Stach clock_enable(config->periph_id); 6027ae18f37SLucas Stach usbf_reset_controller(config, usbctlr); 6037ae18f37SLucas Stach 6047ae18f37SLucas Stach /* enable pinmux bypass */ 6057ae18f37SLucas Stach setbits_le32(&usbctlr->ulpi_timing_ctrl_0, 6067ae18f37SLucas Stach ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP); 6077ae18f37SLucas Stach 6087ae18f37SLucas Stach /* Select ULPI parallel interface */ 609a4539a2aSStephen Warren init_phy_mux(config, PTS_ULPI, init); 6107ae18f37SLucas Stach 6117ae18f37SLucas Stach /* enable ULPI transceiver */ 6127ae18f37SLucas Stach setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB); 6137ae18f37SLucas Stach 6147ae18f37SLucas Stach /* configure ULPI transceiver timings */ 6157ae18f37SLucas Stach val = 0; 6167ae18f37SLucas Stach writel(val, &usbctlr->ulpi_timing_ctrl_1); 6177ae18f37SLucas Stach 6187ae18f37SLucas Stach val |= ULPI_DATA_TRIMMER_SEL(4); 6197ae18f37SLucas Stach val |= ULPI_STPDIRNXT_TRIMMER_SEL(4); 6207ae18f37SLucas Stach val |= ULPI_DIR_TRIMMER_SEL(4); 6217ae18f37SLucas Stach writel(val, &usbctlr->ulpi_timing_ctrl_1); 6227ae18f37SLucas Stach udelay(10); 6237ae18f37SLucas Stach 6247ae18f37SLucas Stach val |= ULPI_DATA_TRIMMER_LOAD; 6257ae18f37SLucas Stach val |= ULPI_STPDIRNXT_TRIMMER_LOAD; 6267ae18f37SLucas Stach val |= ULPI_DIR_TRIMMER_LOAD; 6277ae18f37SLucas Stach writel(val, &usbctlr->ulpi_timing_ctrl_1); 6287ae18f37SLucas Stach 6297ae18f37SLucas Stach /* set up phy for host operation with external vbus supply */ 6307ae18f37SLucas Stach ulpi_vp.port_num = 0; 6317ae18f37SLucas Stach ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport; 6327ae18f37SLucas Stach 6337e27bddaSSimon Glass ret = ulpi_init(&ulpi_vp); 6347e27bddaSSimon Glass if (ret) { 6357ae18f37SLucas Stach printf("Tegra ULPI viewport init failed\n"); 6367e27bddaSSimon Glass return ret; 6377ae18f37SLucas Stach } 6387ae18f37SLucas Stach 6397ae18f37SLucas Stach ulpi_set_vbus(&ulpi_vp, 1, 1); 6407ae18f37SLucas Stach ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0); 6417ae18f37SLucas Stach 6427ae18f37SLucas Stach /* enable wakeup events */ 6437ae18f37SLucas Stach setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC); 6447ae18f37SLucas Stach 6457ae18f37SLucas Stach /* Enable and wait for the phy clock to become valid in 100 ms */ 6467ae18f37SLucas Stach setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR); 6477ae18f37SLucas Stach for (loop_count = 100000; loop_count != 0; loop_count--) { 6487ae18f37SLucas Stach if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID) 6497ae18f37SLucas Stach break; 6507ae18f37SLucas Stach udelay(1); 6517ae18f37SLucas Stach } 6527ae18f37SLucas Stach if (!loop_count) 6537e27bddaSSimon Glass return -ETIMEDOUT; 6547ae18f37SLucas Stach clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR); 6557ae18f37SLucas Stach 6567ae18f37SLucas Stach return 0; 6577ae18f37SLucas Stach } 6587ae18f37SLucas Stach #else 659a4539a2aSStephen Warren static int init_ulpi_usb_controller(struct fdt_usb *config, 660a4539a2aSStephen Warren enum usb_init_type init) 6617ae18f37SLucas Stach { 6627ae18f37SLucas Stach printf("No code to set up ULPI controller, please enable" 6637ae18f37SLucas Stach "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT"); 6647e27bddaSSimon Glass return -ENOSYS; 6657ae18f37SLucas Stach } 6667ae18f37SLucas Stach #endif 6677ae18f37SLucas Stach 6687ae18f37SLucas Stach static void config_clock(const u32 timing[]) 6697ae18f37SLucas Stach { 670*7aaa5a60STom Warren debug("%s: DIVM = %d, DIVN = %d, DIVP = %d, cpcon/lfcon = %d/%d\n", 671*7aaa5a60STom Warren __func__, timing[PARAM_DIVM], timing[PARAM_DIVN], 672*7aaa5a60STom Warren timing[PARAM_DIVP], timing[PARAM_CPCON], timing[PARAM_LFCON]); 673*7aaa5a60STom Warren 6747ae18f37SLucas Stach clock_start_pll(CLOCK_ID_USB, 6757ae18f37SLucas Stach timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP], 6767ae18f37SLucas Stach timing[PARAM_CPCON], timing[PARAM_LFCON]); 6777ae18f37SLucas Stach } 6787ae18f37SLucas Stach 6797e44d932SJim Lin static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config) 6807ae18f37SLucas Stach { 6817ae18f37SLucas Stach const char *phy, *mode; 6827ae18f37SLucas Stach 6837ae18f37SLucas Stach config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg"); 6847ae18f37SLucas Stach mode = fdt_getprop(blob, node, "dr_mode", NULL); 6857ae18f37SLucas Stach if (mode) { 6867ae18f37SLucas Stach if (0 == strcmp(mode, "host")) 6877ae18f37SLucas Stach config->dr_mode = DR_MODE_HOST; 6887ae18f37SLucas Stach else if (0 == strcmp(mode, "peripheral")) 6897ae18f37SLucas Stach config->dr_mode = DR_MODE_DEVICE; 6907ae18f37SLucas Stach else if (0 == strcmp(mode, "otg")) 6917ae18f37SLucas Stach config->dr_mode = DR_MODE_OTG; 6927ae18f37SLucas Stach else { 6937ae18f37SLucas Stach debug("%s: Cannot decode dr_mode '%s'\n", __func__, 6947ae18f37SLucas Stach mode); 6957e27bddaSSimon Glass return -EINVAL; 6967ae18f37SLucas Stach } 6977ae18f37SLucas Stach } else { 6987ae18f37SLucas Stach config->dr_mode = DR_MODE_HOST; 6997ae18f37SLucas Stach } 7007ae18f37SLucas Stach 7017ae18f37SLucas Stach phy = fdt_getprop(blob, node, "phy_type", NULL); 7027ae18f37SLucas Stach config->utmi = phy && 0 == strcmp("utmi", phy); 7037ae18f37SLucas Stach config->ulpi = phy && 0 == strcmp("ulpi", phy); 7047ae18f37SLucas Stach config->enabled = fdtdec_get_is_enabled(blob, node); 7057ae18f37SLucas Stach config->has_legacy_mode = fdtdec_get_bool(blob, node, 7067ae18f37SLucas Stach "nvidia,has-legacy-mode"); 7077ae18f37SLucas Stach config->periph_id = clock_decode_periph_id(blob, node); 7087ae18f37SLucas Stach if (config->periph_id == PERIPH_ID_NONE) { 7097ae18f37SLucas Stach debug("%s: Missing/invalid peripheral ID\n", __func__); 7107e27bddaSSimon Glass return -EINVAL; 7117ae18f37SLucas Stach } 71246927e1eSSimon Glass gpio_request_by_name_nodev(blob, node, "nvidia,vbus-gpio", 0, 71346927e1eSSimon Glass &config->vbus_gpio, GPIOD_IS_OUT); 71446927e1eSSimon Glass gpio_request_by_name_nodev(blob, node, "nvidia,phy-reset-gpio", 0, 71546927e1eSSimon Glass &config->phy_reset_gpio, GPIOD_IS_OUT); 7167ae18f37SLucas Stach debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, " 7177ae18f37SLucas Stach "vbus=%d, phy_reset=%d, dr_mode=%d\n", 7187ae18f37SLucas Stach config->enabled, config->has_legacy_mode, config->utmi, 71946927e1eSSimon Glass config->ulpi, config->periph_id, 72046927e1eSSimon Glass gpio_get_number(&config->vbus_gpio), 72146927e1eSSimon Glass gpio_get_number(&config->phy_reset_gpio), config->dr_mode); 7227ae18f37SLucas Stach 7237ae18f37SLucas Stach return 0; 7247ae18f37SLucas Stach } 7257ae18f37SLucas Stach 726ddb9a502SSimon Glass int usb_common_init(struct fdt_usb *config, enum usb_init_type init) 727ddb9a502SSimon Glass { 728ddb9a502SSimon Glass int ret = 0; 729ddb9a502SSimon Glass 730ddb9a502SSimon Glass switch (init) { 731ddb9a502SSimon Glass case USB_INIT_HOST: 732ddb9a502SSimon Glass switch (config->dr_mode) { 733ddb9a502SSimon Glass case DR_MODE_HOST: 734ddb9a502SSimon Glass case DR_MODE_OTG: 735ddb9a502SSimon Glass break; 736ddb9a502SSimon Glass default: 737ddb9a502SSimon Glass printf("tegrausb: Invalid dr_mode %d for host mode\n", 738ddb9a502SSimon Glass config->dr_mode); 739ddb9a502SSimon Glass return -1; 740ddb9a502SSimon Glass } 741ddb9a502SSimon Glass break; 742ddb9a502SSimon Glass case USB_INIT_DEVICE: 743ddb9a502SSimon Glass if (config->periph_id != PERIPH_ID_USBD) { 744ddb9a502SSimon Glass printf("tegrausb: Device mode only supported on first USB controller\n"); 745ddb9a502SSimon Glass return -1; 746ddb9a502SSimon Glass } 747ddb9a502SSimon Glass if (!config->utmi) { 748ddb9a502SSimon Glass printf("tegrausb: Device mode only supported with UTMI PHY\n"); 749ddb9a502SSimon Glass return -1; 750ddb9a502SSimon Glass } 751ddb9a502SSimon Glass switch (config->dr_mode) { 752ddb9a502SSimon Glass case DR_MODE_DEVICE: 753ddb9a502SSimon Glass case DR_MODE_OTG: 754ddb9a502SSimon Glass break; 755ddb9a502SSimon Glass default: 756ddb9a502SSimon Glass printf("tegrausb: Invalid dr_mode %d for device mode\n", 757ddb9a502SSimon Glass config->dr_mode); 758ddb9a502SSimon Glass return -1; 759ddb9a502SSimon Glass } 760ddb9a502SSimon Glass break; 761ddb9a502SSimon Glass default: 762ddb9a502SSimon Glass printf("tegrausb: Unknown USB_INIT_* %d\n", init); 763ddb9a502SSimon Glass return -1; 764ddb9a502SSimon Glass } 765ddb9a502SSimon Glass 766ddb9a502SSimon Glass debug("%d, %d\n", config->utmi, config->ulpi); 767ddb9a502SSimon Glass if (config->utmi) 768ddb9a502SSimon Glass ret = init_utmi_usb_controller(config, init); 769ddb9a502SSimon Glass else if (config->ulpi) 770ddb9a502SSimon Glass ret = init_ulpi_usb_controller(config, init); 771ddb9a502SSimon Glass if (ret) 772ddb9a502SSimon Glass return ret; 773ddb9a502SSimon Glass 774ddb9a502SSimon Glass set_up_vbus(config, init); 775ddb9a502SSimon Glass 776ddb9a502SSimon Glass config->init_type = init; 777ddb9a502SSimon Glass 778ddb9a502SSimon Glass return 0; 779ddb9a502SSimon Glass } 780ddb9a502SSimon Glass 781ddb9a502SSimon Glass void usb_common_uninit(struct fdt_usb *priv) 782ddb9a502SSimon Glass { 783ddb9a502SSimon Glass struct usb_ctlr *usbctlr; 784ddb9a502SSimon Glass 785ddb9a502SSimon Glass usbctlr = priv->reg; 786ddb9a502SSimon Glass 787ddb9a502SSimon Glass /* Stop controller */ 788ddb9a502SSimon Glass writel(0, &usbctlr->usb_cmd); 789ddb9a502SSimon Glass udelay(1000); 790ddb9a502SSimon Glass 791ddb9a502SSimon Glass /* Initiate controller reset */ 792ddb9a502SSimon Glass writel(2, &usbctlr->usb_cmd); 793ddb9a502SSimon Glass udelay(1000); 794ddb9a502SSimon Glass } 795ddb9a502SSimon Glass 796deb8508cSSimon Glass static const struct ehci_ops tegra_ehci_ops = { 797deb8508cSSimon Glass .set_usb_mode = tegra_ehci_set_usbmode, 798deb8508cSSimon Glass .get_port_speed = tegra_ehci_get_port_speed, 799deb8508cSSimon Glass .powerup_fixup = tegra_ehci_powerup_fixup, 800deb8508cSSimon Glass }; 801deb8508cSSimon Glass 802c3980ad3SSimon Glass static int ehci_usb_ofdata_to_platdata(struct udevice *dev) 803c3980ad3SSimon Glass { 804c3980ad3SSimon Glass struct fdt_usb *priv = dev_get_priv(dev); 805c3980ad3SSimon Glass int ret; 806c3980ad3SSimon Glass 807c3980ad3SSimon Glass ret = fdt_decode_usb(gd->fdt_blob, dev->of_offset, priv); 808c3980ad3SSimon Glass if (ret) 809c3980ad3SSimon Glass return ret; 810c3980ad3SSimon Glass 811c3980ad3SSimon Glass priv->type = dev_get_driver_data(dev); 812c3980ad3SSimon Glass 813c3980ad3SSimon Glass return 0; 814c3980ad3SSimon Glass } 815c3980ad3SSimon Glass 816c3980ad3SSimon Glass static int ehci_usb_probe(struct udevice *dev) 817c3980ad3SSimon Glass { 818c3980ad3SSimon Glass struct usb_platdata *plat = dev_get_platdata(dev); 819c3980ad3SSimon Glass struct fdt_usb *priv = dev_get_priv(dev); 820c3980ad3SSimon Glass struct ehci_hccr *hccr; 821c3980ad3SSimon Glass struct ehci_hcor *hcor; 822c3980ad3SSimon Glass static bool clk_done; 823c3980ad3SSimon Glass int ret; 824c3980ad3SSimon Glass 825c3980ad3SSimon Glass ret = usb_common_init(priv, plat->init_type); 826c3980ad3SSimon Glass if (ret) 827c3980ad3SSimon Glass return ret; 828c3980ad3SSimon Glass hccr = (struct ehci_hccr *)&priv->reg->cap_length; 829c3980ad3SSimon Glass hcor = (struct ehci_hcor *)&priv->reg->usb_cmd; 830c3980ad3SSimon Glass if (!clk_done) { 831c3980ad3SSimon Glass config_clock(get_pll_timing(&fdt_usb_controllers[priv->type])); 832c3980ad3SSimon Glass clk_done = true; 833c3980ad3SSimon Glass } 834c3980ad3SSimon Glass 835c3980ad3SSimon Glass return ehci_register(dev, hccr, hcor, &tegra_ehci_ops, 0, 836c3980ad3SSimon Glass plat->init_type); 837c3980ad3SSimon Glass } 838c3980ad3SSimon Glass 839c3980ad3SSimon Glass static int ehci_usb_remove(struct udevice *dev) 840c3980ad3SSimon Glass { 841c3980ad3SSimon Glass int ret; 842c3980ad3SSimon Glass 843c3980ad3SSimon Glass ret = ehci_deregister(dev); 844c3980ad3SSimon Glass if (ret) 845c3980ad3SSimon Glass return ret; 846c3980ad3SSimon Glass 847c3980ad3SSimon Glass return 0; 848c3980ad3SSimon Glass } 849c3980ad3SSimon Glass 850c3980ad3SSimon Glass static const struct udevice_id ehci_usb_ids[] = { 851c3980ad3SSimon Glass { .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 }, 852c3980ad3SSimon Glass { .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 }, 853c3980ad3SSimon Glass { .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 }, 854*7aaa5a60STom Warren { .compatible = "nvidia,tegra210-ehci", .data = USB_CTLR_T210 }, 855c3980ad3SSimon Glass { } 856c3980ad3SSimon Glass }; 857c3980ad3SSimon Glass 858c3980ad3SSimon Glass U_BOOT_DRIVER(usb_ehci) = { 859c3980ad3SSimon Glass .name = "ehci_tegra", 860c3980ad3SSimon Glass .id = UCLASS_USB, 861c3980ad3SSimon Glass .of_match = ehci_usb_ids, 862c3980ad3SSimon Glass .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, 863c3980ad3SSimon Glass .probe = ehci_usb_probe, 864c3980ad3SSimon Glass .remove = ehci_usb_remove, 865c3980ad3SSimon Glass .ops = &ehci_usb_ops, 866c3980ad3SSimon Glass .platdata_auto_alloc_size = sizeof(struct usb_platdata), 867c3980ad3SSimon Glass .priv_auto_alloc_size = sizeof(struct fdt_usb), 868c3980ad3SSimon Glass .flags = DM_FLAG_ALLOC_PRIV_DMA, 869c3980ad3SSimon Glass }; 870