2 * Copyright (c) 2011 The Chromium OS Authors.
3 * Copyright (c) 2009-2013 NVIDIA Corporation
4 * Copyright (c) 2013 Lucas Stach
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/errno.h>
12 #include <asm-generic/gpio.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch-tegra/usb.h>
15 #include <asm/arch-tegra/clk_rst.h>
23 #define USB1_ADDR_MASK 0xFFFF0000
25 #define HOSTPC1_DEVLC 0x84
26 #define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3)
28 #ifdef CONFIG_USB_ULPI
29 #ifndef CONFIG_USB_ULPI_VIEWPORT
30 #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
31 define CONFIG_USB_ULPI_VIEWPORT"
36 USB_PORTS_MAX = 3, /* Maximum ports we allow */
39 /* Parameters we need for USB */
41 PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
42 PARAM_DIVM, /* PLL INPUT DIVIDER */
43 PARAM_DIVP, /* POST DIVIDER (2^N) */
44 PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
45 PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
46 PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
47 PARAM_STABLE_COUNT, /* PLL-U STABLE count */
48 PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
49 PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
50 PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
51 PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
56 /* Possible port types (dual role mode) */
59 DR_MODE_HOST, /* supports host operation */
60 DR_MODE_DEVICE, /* supports device operation */
61 DR_MODE_OTG, /* supports both */
64 /* Information about a USB port */
66 struct usb_ctlr *reg; /* address of registers in physical memory */
67 unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
68 unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
69 unsigned enabled:1; /* 1 to enable, 0 to disable */
70 unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
71 unsigned initialized:1; /* has this port already been initialized? */
72 enum dr_mode dr_mode; /* dual role mode */
73 enum periph_id periph_id;/* peripheral id */
74 struct fdt_gpio_state vbus_gpio; /* GPIO for vbus enable */
75 struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
78 static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
79 static unsigned port_count; /* Number of available ports */
80 /* Port that needs to clear CSC after Port Reset */
81 static u32 port_addr_clear_csc;
84 * This table has USB timing parameters for each Oscillator frequency we
85 * support. There are four sets of values:
87 * 1. PLLU configuration information (reference clock is osc/clk_m and
88 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
90 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
91 * ----------------------------------------------------------------------
92 * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
93 * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
94 * Filter frequency (MHz) 1 4.8 6 2
95 * CPCON 1100b 0011b 1100b 1100b
98 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
100 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
101 * ---------------------------------------------------------------------------
102 * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
103 * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
104 * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
105 * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
107 * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
108 * SessEnd. Each of these signals have their own debouncer and for each of
109 * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
112 * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
113 * 0xffff -> No debouncing at all
114 * <n> ms = <n> *1000 / (1/19.2MHz) / 4
116 * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
117 * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
119 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
120 * values, so we can keep those to default.
122 * 4. The 20 microsecond delay after bias cell operation.
124 static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
125 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
126 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
127 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
128 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
129 { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
132 static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
133 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
134 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
135 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
136 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
137 { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
140 static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
141 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
142 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
143 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
144 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
145 { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
148 /* UTMIP Idle Wait Delay */
149 static const u8 utmip_idle_wait_delay = 17;
151 /* UTMIP Elastic limit */
152 static const u8 utmip_elastic_limit = 16;
154 /* UTMIP High Speed Sync Start Delay */
155 static const u8 utmip_hs_sync_start_delay = 9;
157 struct fdt_usb_controller {
159 /* flag to determine whether controller supports hostpc register */
161 const unsigned *pll_parameter;
164 static struct fdt_usb_controller fdt_usb_controllers[] = {
166 .compat = COMPAT_NVIDIA_TEGRA20_USB,
168 .pll_parameter = (const unsigned *)T20_usb_pll,
171 .compat = COMPAT_NVIDIA_TEGRA30_USB,
173 .pll_parameter = (const unsigned *)T30_usb_pll,
176 .compat = COMPAT_NVIDIA_TEGRA114_USB,
178 .pll_parameter = (const unsigned *)T114_usb_pll,
182 static struct fdt_usb_controller *controller;
185 * A known hardware issue where Connect Status Change bit of PORTSC register
186 * of USB1 controller will be set after Port Reset.
187 * We have to clear it in order for later device enumeration to proceed.
188 * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
191 void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
194 /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
195 if (controller->has_hostpc)
198 if (((u32)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
200 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
201 if (ehci_readl(status_reg) & EHCI_PS_CSC)
206 * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
209 void ehci_set_usbmode(int index)
211 struct fdt_usb *config;
212 struct usb_ctlr *usbctlr;
215 config = &port[index];
216 usbctlr = config->reg;
218 tmp = ehci_readl(&usbctlr->usb_mode);
219 tmp |= USBMODE_CM_HC;
220 ehci_writel(&usbctlr->usb_mode, tmp);
224 * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
227 int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
232 if (controller->has_hostpc) {
233 reg_ptr = (uint32_t *)((u8 *)&hcor->or_usbcmd + HOSTPC1_DEVLC);
234 tmp = ehci_readl(reg_ptr);
235 return HOSTPC1_PSPD(tmp);
237 return PORTSC_PSPD(reg);
240 /* Put the port into host mode */
241 static void set_host_mode(struct fdt_usb *config)
244 * If we are an OTG port, check if remote host is driving VBus and
245 * bail out in this case.
247 if (config->dr_mode == DR_MODE_OTG &&
248 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
252 * If not driving, we set the GPIO to enable VBUS. We assume
253 * that the pinmux is set up correctly for this.
255 if (fdt_gpio_isvalid(&config->vbus_gpio)) {
256 fdtdec_setup_gpio(&config->vbus_gpio);
257 gpio_direction_output(config->vbus_gpio.gpio,
258 (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
260 debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
261 (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
266 void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
268 /* Reset the USB controller with 2us delay */
269 reset_periph(config->periph_id, 2);
272 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
275 if (config->has_legacy_mode)
276 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
278 /* Put UTMIP1/3 in reset */
279 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
281 /* Enable the UTMIP PHY */
283 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
286 static const unsigned *get_pll_timing(void)
288 const unsigned *timing;
290 timing = controller->pll_parameter +
291 clock_get_osc_freq() * PARAM_COUNT;
296 /* select the PHY to use with a USB controller */
297 static void init_phy_mux(struct fdt_usb *config, uint pts)
299 struct usb_ctlr *usbctlr = config->reg;
301 #if defined(CONFIG_TEGRA20)
302 if (config->periph_id == PERIPH_ID_USBD) {
303 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
304 PTS_UTMI << PTS1_SHIFT);
305 clrbits_le32(&usbctlr->port_sc1, STS1);
307 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
308 PTS_UTMI << PTS_SHIFT);
309 clrbits_le32(&usbctlr->port_sc1, STS);
312 /* Set to Host mode after Controller Reset was done */
313 clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
315 /* Select PHY interface after setting host mode */
316 clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
318 clrbits_le32(&usbctlr->hostpc1_devlc, STS);
322 /* set up the UTMI USB controller with the parameters provided */
323 static int init_utmi_usb_controller(struct fdt_usb *config)
327 const unsigned *timing;
328 struct usb_ctlr *usbctlr = config->reg;
329 struct clk_rst_ctlr *clkrst;
330 struct usb_ctlr *usb1ctlr;
332 clock_enable(config->periph_id);
334 /* Reset the usb controller */
335 usbf_reset_controller(config, usbctlr);
337 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
338 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
340 /* Follow the crystal clock disable by >100ns delay */
344 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
345 * mux must be switched to actually use a_sess_vld threshold.
347 if (config->dr_mode == DR_MODE_OTG &&
348 fdt_gpio_isvalid(&config->vbus_gpio))
349 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
351 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
354 * PLL Delay CONFIGURATION settings. The following parameters control
355 * the bring up of the plls.
357 timing = get_pll_timing();
359 if (!controller->has_hostpc) {
360 val = readl(&usbctlr->utmip_misc_cfg1);
361 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
362 timing[PARAM_STABLE_COUNT] <<
363 UTMIP_PLLU_STABLE_COUNT_SHIFT);
364 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
365 timing[PARAM_ACTIVE_DELAY_COUNT] <<
366 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
367 writel(val, &usbctlr->utmip_misc_cfg1);
369 /* Set PLL enable delay count and crystal frequency count */
370 val = readl(&usbctlr->utmip_pll_cfg1);
371 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
372 timing[PARAM_ENABLE_DELAY_COUNT] <<
373 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
374 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
375 timing[PARAM_XTAL_FREQ_COUNT] <<
376 UTMIP_XTAL_FREQ_COUNT_SHIFT);
377 writel(val, &usbctlr->utmip_pll_cfg1);
379 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
381 val = readl(&clkrst->crc_utmip_pll_cfg2);
382 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
383 timing[PARAM_STABLE_COUNT] <<
384 UTMIP_PLLU_STABLE_COUNT_SHIFT);
385 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
386 timing[PARAM_ACTIVE_DELAY_COUNT] <<
387 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
388 writel(val, &clkrst->crc_utmip_pll_cfg2);
390 /* Set PLL enable delay count and crystal frequency count */
391 val = readl(&clkrst->crc_utmip_pll_cfg1);
392 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
393 timing[PARAM_ENABLE_DELAY_COUNT] <<
394 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
395 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
396 timing[PARAM_XTAL_FREQ_COUNT] <<
397 UTMIP_XTAL_FREQ_COUNT_SHIFT);
398 writel(val, &clkrst->crc_utmip_pll_cfg1);
400 /* Disable Power Down state for PLL */
401 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
402 PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
403 PLL_ACTIVE_POWERDOWN);
405 /* Recommended PHY settings for EYE diagram */
406 val = readl(&usbctlr->utmip_xcvr_cfg0);
407 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
408 0x4 << UTMIP_XCVR_SETUP_SHIFT);
409 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
410 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
411 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
412 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
413 writel(val, &usbctlr->utmip_xcvr_cfg0);
414 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
415 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
416 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
418 /* Some registers can be controlled from USB1 only. */
419 if (config->periph_id != PERIPH_ID_USBD) {
420 clock_enable(PERIPH_ID_USBD);
421 /* Disable Reset if in Reset state */
422 reset_set_enable(PERIPH_ID_USBD, 0);
424 usb1ctlr = (struct usb_ctlr *)
425 ((u32)config->reg & USB1_ADDR_MASK);
426 val = readl(&usb1ctlr->utmip_bias_cfg0);
427 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
428 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
429 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
430 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
431 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
432 writel(val, &usb1ctlr->utmip_bias_cfg0);
434 /* Miscellaneous setting mentioned in Programming Guide */
435 clrbits_le32(&usbctlr->utmip_misc_cfg0,
436 UTMIP_SUSPEND_EXIT_ON_EDGE);
439 /* Setting the tracking length time */
440 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
441 UTMIP_BIAS_PDTRK_COUNT_MASK,
442 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
444 /* Program debounce time for VBUS to become valid */
445 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
446 UTMIP_DEBOUNCE_CFG0_MASK,
447 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
449 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
451 /* Disable battery charge enabling bit */
452 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
454 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
455 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
458 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
459 * Setting these fields, together with default values of the
460 * other fields, results in programming the registers below as
462 * UTMIP_HSRX_CFG0 = 0x9168c000
463 * UTMIP_HSRX_CFG1 = 0x13
466 /* Set PLL enable delay count and Crystal frequency count */
467 val = readl(&usbctlr->utmip_hsrx_cfg0);
468 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
469 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
470 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
471 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
472 writel(val, &usbctlr->utmip_hsrx_cfg0);
474 /* Configure the UTMIP_HS_SYNC_START_DLY */
475 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
476 UTMIP_HS_SYNC_START_DLY_MASK,
477 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
479 /* Preceed the crystal clock disable by >100ns delay. */
482 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
483 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
485 if (controller->has_hostpc) {
486 if (config->periph_id == PERIPH_ID_USBD)
487 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
488 UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
489 if (config->periph_id == PERIPH_ID_USB2)
490 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
491 UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
492 if (config->periph_id == PERIPH_ID_USB3)
493 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
494 UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
496 /* Finished the per-controller init. */
498 /* De-assert UTMIP_RESET to bring out of reset. */
499 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
501 /* Wait for the phy clock to become valid in 100 ms */
502 for (loop_count = 100000; loop_count != 0; loop_count--) {
503 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
510 /* Disable ICUSB FS/LS transceiver */
511 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
513 /* Select UTMI parallel interface */
514 init_phy_mux(config, PTS_UTMI);
516 /* Deassert power down state */
517 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
518 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
519 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
520 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
522 if (controller->has_hostpc) {
524 * BIAS Pad Power Down is common among all 3 USB
525 * controllers and can be controlled from USB1 only.
527 usb1ctlr = (struct usb_ctlr *)
528 ((u32)config->reg & USB1_ADDR_MASK);
529 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
531 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
532 UTMIP_FORCE_PDTRK_POWERDOWN);
537 #ifdef CONFIG_USB_ULPI
538 /* if board file does not set a ULPI reference frequency we default to 24MHz */
539 #ifndef CONFIG_ULPI_REF_CLK
540 #define CONFIG_ULPI_REF_CLK 24000000
543 /* set up the ULPI USB controller with the parameters provided */
544 static int init_ulpi_usb_controller(struct fdt_usb *config)
548 struct ulpi_viewport ulpi_vp;
549 struct usb_ctlr *usbctlr = config->reg;
551 /* set up ULPI reference clock on pllp_out4 */
552 clock_enable(PERIPH_ID_DEV2_OUT);
553 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
556 if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
557 fdtdec_setup_gpio(&config->phy_reset_gpio);
558 gpio_direction_output(config->phy_reset_gpio.gpio, 0);
560 gpio_set_value(config->phy_reset_gpio.gpio, 1);
563 /* Reset the usb controller */
564 clock_enable(config->periph_id);
565 usbf_reset_controller(config, usbctlr);
567 /* enable pinmux bypass */
568 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
569 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
571 /* Select ULPI parallel interface */
572 init_phy_mux(config, PTS_ULPI);
574 /* enable ULPI transceiver */
575 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
577 /* configure ULPI transceiver timings */
579 writel(val, &usbctlr->ulpi_timing_ctrl_1);
581 val |= ULPI_DATA_TRIMMER_SEL(4);
582 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
583 val |= ULPI_DIR_TRIMMER_SEL(4);
584 writel(val, &usbctlr->ulpi_timing_ctrl_1);
587 val |= ULPI_DATA_TRIMMER_LOAD;
588 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
589 val |= ULPI_DIR_TRIMMER_LOAD;
590 writel(val, &usbctlr->ulpi_timing_ctrl_1);
592 /* set up phy for host operation with external vbus supply */
593 ulpi_vp.port_num = 0;
594 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
596 if (ulpi_init(&ulpi_vp)) {
597 printf("Tegra ULPI viewport init failed\n");
601 ulpi_set_vbus(&ulpi_vp, 1, 1);
602 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
604 /* enable wakeup events */
605 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
607 /* Enable and wait for the phy clock to become valid in 100 ms */
608 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
609 for (loop_count = 100000; loop_count != 0; loop_count--) {
610 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
616 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
621 static int init_ulpi_usb_controller(struct fdt_usb *config)
623 printf("No code to set up ULPI controller, please enable"
624 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
629 static void config_clock(const u32 timing[])
631 clock_start_pll(CLOCK_ID_USB,
632 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
633 timing[PARAM_CPCON], timing[PARAM_LFCON]);
636 static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
638 const char *phy, *mode;
640 config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
641 mode = fdt_getprop(blob, node, "dr_mode", NULL);
643 if (0 == strcmp(mode, "host"))
644 config->dr_mode = DR_MODE_HOST;
645 else if (0 == strcmp(mode, "peripheral"))
646 config->dr_mode = DR_MODE_DEVICE;
647 else if (0 == strcmp(mode, "otg"))
648 config->dr_mode = DR_MODE_OTG;
650 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
652 return -FDT_ERR_NOTFOUND;
655 config->dr_mode = DR_MODE_HOST;
658 phy = fdt_getprop(blob, node, "phy_type", NULL);
659 config->utmi = phy && 0 == strcmp("utmi", phy);
660 config->ulpi = phy && 0 == strcmp("ulpi", phy);
661 config->enabled = fdtdec_get_is_enabled(blob, node);
662 config->has_legacy_mode = fdtdec_get_bool(blob, node,
663 "nvidia,has-legacy-mode");
664 if (config->has_legacy_mode)
665 port_addr_clear_csc = (u32) config->reg;
666 config->periph_id = clock_decode_periph_id(blob, node);
667 if (config->periph_id == PERIPH_ID_NONE) {
668 debug("%s: Missing/invalid peripheral ID\n", __func__);
669 return -FDT_ERR_NOTFOUND;
671 fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
672 fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
673 &config->phy_reset_gpio);
674 debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
675 "vbus=%d, phy_reset=%d, dr_mode=%d\n",
676 config->enabled, config->has_legacy_mode, config->utmi,
677 config->ulpi, config->periph_id, config->vbus_gpio.gpio,
678 config->phy_reset_gpio.gpio, config->dr_mode);
684 * process_usb_nodes() - Process a list of USB nodes, adding them to our list
687 * @node_list: list of nodes to process (any <=0 are ignored)
688 * @count: number of nodes to process
690 * Return: 0 - ok, -1 - error
692 static int process_usb_nodes(const void *blob, int node_list[], int count)
694 struct fdt_usb config;
699 for (i = 0; i < count; i++) {
700 if (port_count == USB_PORTS_MAX) {
701 printf("tegrausb: Cannot register more than %d ports\n",
706 debug("USB %d: ", i);
710 if (fdt_decode_usb(blob, node, &config)) {
711 debug("Cannot decode USB node %s\n",
712 fdt_get_name(blob, node, NULL));
716 config_clock(get_pll_timing());
719 config.initialized = 0;
721 /* add new USB port to the list of available ports */
722 port[port_count++] = config;
728 int usb_process_devicetree(const void *blob)
730 int node_list[USB_PORTS_MAX];
734 for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
735 controller = &fdt_usb_controllers[i];
737 count = fdtdec_find_aliases_for_id(blob, "usb",
738 controller->compat, node_list, USB_PORTS_MAX);
740 err = process_usb_nodes(blob, node_list, count);
742 printf("%s: Error processing USB node!\n",
747 if (i == ARRAY_SIZE(fdt_usb_controllers))
754 * Start up the given port number (ports are numbered from 0 on each board).
755 * This returns values for the appropriate hccr and hcor addresses to use for
756 * USB EHCI operations.
758 * @param index port number to start
759 * @param hccr returns start address of EHCI HCCR registers
760 * @param hcor returns start address of EHCI HCOR registers
761 * @return 0 if ok, -1 on error (generally invalid port number)
763 int ehci_hcd_init(int index, enum usb_init_type init,
764 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
766 struct fdt_usb *config;
767 struct usb_ctlr *usbctlr;
769 if (index >= port_count)
772 config = &port[index];
774 /* skip init, if the port is already initialized */
775 if (config->initialized)
778 if (config->utmi && init_utmi_usb_controller(config)) {
779 printf("tegrausb: Cannot init port %d\n", index);
783 if (config->ulpi && init_ulpi_usb_controller(config)) {
784 printf("tegrausb: Cannot init port %d\n", index);
788 set_host_mode(config);
790 config->initialized = 1;
793 usbctlr = config->reg;
794 *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
795 *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
801 * Bring down the specified USB controller
803 int ehci_hcd_stop(int index)
805 struct usb_ctlr *usbctlr;
807 usbctlr = port[index].reg;
809 /* Stop controller */
810 writel(0, &usbctlr->usb_cmd);
813 /* Initiate controller reset */
814 writel(2, &usbctlr->usb_cmd);
817 port[index].initialized = 0;