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 */
72 /* Information about a USB port */
74 struct usb_ctlr *reg; /* address of registers in physical memory */
75 unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
76 unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
77 unsigned enabled:1; /* 1 to enable, 0 to disable */
78 unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
79 unsigned initialized:1; /* has this port already been initialized? */
80 enum usb_ctlr_type type;
81 enum usb_init_type init_type;
82 enum dr_mode dr_mode; /* dual role mode */
83 enum periph_id periph_id;/* peripheral id */
84 struct gpio_desc vbus_gpio; /* GPIO for vbus enable */
85 struct gpio_desc phy_reset_gpio; /* GPIO to reset ULPI phy */
88 static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
89 static unsigned port_count; /* Number of available ports */
90 /* Port that needs to clear CSC after Port Reset */
91 static u32 port_addr_clear_csc;
94 * This table has USB timing parameters for each Oscillator frequency we
95 * support. There are four sets of values:
97 * 1. PLLU configuration information (reference clock is osc/clk_m and
98 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
100 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
101 * ----------------------------------------------------------------------
102 * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
103 * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
104 * Filter frequency (MHz) 1 4.8 6 2
105 * CPCON 1100b 0011b 1100b 1100b
108 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
110 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
111 * ---------------------------------------------------------------------------
112 * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
113 * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
114 * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
115 * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
117 * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
118 * SessEnd. Each of these signals have their own debouncer and for each of
119 * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
122 * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
123 * 0xffff -> No debouncing at all
124 * <n> ms = <n> *1000 / (1/19.2MHz) / 4
126 * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
127 * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
129 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
130 * values, so we can keep those to default.
132 * 4. The 20 microsecond delay after bias cell operation.
134 static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
135 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
136 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
137 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
138 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
139 { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
142 static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
143 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
144 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
145 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
146 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
147 { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
150 static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
151 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
152 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
153 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
154 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
155 { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
158 /* UTMIP Idle Wait Delay */
159 static const u8 utmip_idle_wait_delay = 17;
161 /* UTMIP Elastic limit */
162 static const u8 utmip_elastic_limit = 16;
164 /* UTMIP High Speed Sync Start Delay */
165 static const u8 utmip_hs_sync_start_delay = 9;
167 struct fdt_usb_controller {
169 /* flag to determine whether controller supports hostpc register */
171 const unsigned *pll_parameter;
174 static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = {
176 .compat = COMPAT_NVIDIA_TEGRA20_USB,
178 .pll_parameter = (const unsigned *)T20_usb_pll,
181 .compat = COMPAT_NVIDIA_TEGRA30_USB,
183 .pll_parameter = (const unsigned *)T30_usb_pll,
186 .compat = COMPAT_NVIDIA_TEGRA114_USB,
188 .pll_parameter = (const unsigned *)T114_usb_pll,
192 static struct fdt_usb_controller *controller;
195 * A known hardware issue where Connect Status Change bit of PORTSC register
196 * of USB1 controller will be set after Port Reset.
197 * We have to clear it in order for later device enumeration to proceed.
198 * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
201 void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
205 /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
206 if (controller->has_hostpc)
209 if (((unsigned long)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
211 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
212 if (ehci_readl(status_reg) & EHCI_PS_CSC)
217 * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
220 void ehci_set_usbmode(int index)
222 struct fdt_usb *config;
223 struct usb_ctlr *usbctlr;
226 config = &port[index];
227 usbctlr = config->reg;
229 tmp = ehci_readl(&usbctlr->usb_mode);
230 tmp |= USBMODE_CM_HC;
231 ehci_writel(&usbctlr->usb_mode, tmp);
235 * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
238 int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
243 if (controller->has_hostpc) {
244 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd +
246 tmp = ehci_readl(reg_ptr);
247 return HOSTPC1_PSPD(tmp);
249 return PORTSC_PSPD(reg);
252 /* Set up VBUS for host/device mode */
253 static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
256 * If we are an OTG port initializing in host mode,
257 * check if remote host is driving VBus and bail out in this case.
259 if (init == USB_INIT_HOST &&
260 config->dr_mode == DR_MODE_OTG &&
261 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
262 printf("tegrausb: VBUS input active; not enabling as host\n");
266 if (dm_gpio_is_valid(&config->vbus_gpio)) {
269 vbus_value = (init == USB_INIT_HOST);
270 dm_gpio_set_value(&config->vbus_gpio, vbus_value);
272 debug("set_up_vbus: GPIO %d %d\n",
273 gpio_get_number(&config->vbus_gpio), vbus_value);
277 void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
279 /* Reset the USB controller with 2us delay */
280 reset_periph(config->periph_id, 2);
283 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
286 if (config->has_legacy_mode)
287 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
289 /* Put UTMIP1/3 in reset */
290 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
292 /* Enable the UTMIP PHY */
294 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
297 static const unsigned *get_pll_timing(struct fdt_usb_controller *controller)
299 const unsigned *timing;
301 timing = controller->pll_parameter +
302 clock_get_osc_freq() * PARAM_COUNT;
307 /* select the PHY to use with a USB controller */
308 static void init_phy_mux(struct fdt_usb *config, uint pts,
309 enum usb_init_type init)
311 struct usb_ctlr *usbctlr = config->reg;
313 #if defined(CONFIG_TEGRA20)
314 if (config->periph_id == PERIPH_ID_USBD) {
315 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
317 clrbits_le32(&usbctlr->port_sc1, STS1);
319 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
321 clrbits_le32(&usbctlr->port_sc1, STS);
324 /* Set to Host mode (if applicable) after Controller Reset was done */
325 clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
326 (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
328 * Select PHY interface after setting host mode.
329 * For device mode, the ordering requirement is not an issue, since
330 * only the first USB controller supports device mode, and that USB
331 * controller can only talk to a UTMI PHY, so the PHY selection is
332 * already made at reset time, so this write is a no-op.
334 clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
336 clrbits_le32(&usbctlr->hostpc1_devlc, STS);
340 /* set up the UTMI USB controller with the parameters provided */
341 static int init_utmi_usb_controller(struct fdt_usb *config,
342 enum usb_init_type init)
344 struct fdt_usb_controller *controller;
345 u32 b_sess_valid_mask, val;
347 const unsigned *timing;
348 struct usb_ctlr *usbctlr = config->reg;
349 struct clk_rst_ctlr *clkrst;
350 struct usb_ctlr *usb1ctlr;
352 clock_enable(config->periph_id);
354 /* Reset the usb controller */
355 usbf_reset_controller(config, usbctlr);
357 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
358 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
360 /* Follow the crystal clock disable by >100ns delay */
363 b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
364 clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
365 (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
368 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
369 * mux must be switched to actually use a_sess_vld threshold.
371 if (config->dr_mode == DR_MODE_OTG &&
372 dm_gpio_is_valid(&config->vbus_gpio))
373 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
375 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
377 controller = &fdt_usb_controllers[config->type];
378 debug("controller=%p, type=%d\n", controller, config->type);
381 * PLL Delay CONFIGURATION settings. The following parameters control
382 * the bring up of the plls.
384 timing = get_pll_timing(controller);
386 if (!controller->has_hostpc) {
387 val = readl(&usbctlr->utmip_misc_cfg1);
388 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
389 timing[PARAM_STABLE_COUNT] <<
390 UTMIP_PLLU_STABLE_COUNT_SHIFT);
391 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
392 timing[PARAM_ACTIVE_DELAY_COUNT] <<
393 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
394 writel(val, &usbctlr->utmip_misc_cfg1);
396 /* Set PLL enable delay count and crystal frequency count */
397 val = readl(&usbctlr->utmip_pll_cfg1);
398 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
399 timing[PARAM_ENABLE_DELAY_COUNT] <<
400 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
401 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
402 timing[PARAM_XTAL_FREQ_COUNT] <<
403 UTMIP_XTAL_FREQ_COUNT_SHIFT);
404 writel(val, &usbctlr->utmip_pll_cfg1);
406 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
408 val = readl(&clkrst->crc_utmip_pll_cfg2);
409 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
410 timing[PARAM_STABLE_COUNT] <<
411 UTMIP_PLLU_STABLE_COUNT_SHIFT);
412 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
413 timing[PARAM_ACTIVE_DELAY_COUNT] <<
414 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
415 writel(val, &clkrst->crc_utmip_pll_cfg2);
417 /* Set PLL enable delay count and crystal frequency count */
418 val = readl(&clkrst->crc_utmip_pll_cfg1);
419 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
420 timing[PARAM_ENABLE_DELAY_COUNT] <<
421 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
422 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
423 timing[PARAM_XTAL_FREQ_COUNT] <<
424 UTMIP_XTAL_FREQ_COUNT_SHIFT);
425 writel(val, &clkrst->crc_utmip_pll_cfg1);
427 /* Disable Power Down state for PLL */
428 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
429 PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
430 PLL_ACTIVE_POWERDOWN);
432 /* Recommended PHY settings for EYE diagram */
433 val = readl(&usbctlr->utmip_xcvr_cfg0);
434 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
435 0x4 << UTMIP_XCVR_SETUP_SHIFT);
436 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
437 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
438 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
439 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
440 writel(val, &usbctlr->utmip_xcvr_cfg0);
441 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
442 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
443 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
445 /* Some registers can be controlled from USB1 only. */
446 if (config->periph_id != PERIPH_ID_USBD) {
447 clock_enable(PERIPH_ID_USBD);
448 /* Disable Reset if in Reset state */
449 reset_set_enable(PERIPH_ID_USBD, 0);
451 usb1ctlr = (struct usb_ctlr *)
452 ((unsigned long)config->reg & USB1_ADDR_MASK);
453 val = readl(&usb1ctlr->utmip_bias_cfg0);
454 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
455 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
456 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
457 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
458 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
459 writel(val, &usb1ctlr->utmip_bias_cfg0);
461 /* Miscellaneous setting mentioned in Programming Guide */
462 clrbits_le32(&usbctlr->utmip_misc_cfg0,
463 UTMIP_SUSPEND_EXIT_ON_EDGE);
466 /* Setting the tracking length time */
467 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
468 UTMIP_BIAS_PDTRK_COUNT_MASK,
469 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
471 /* Program debounce time for VBUS to become valid */
472 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
473 UTMIP_DEBOUNCE_CFG0_MASK,
474 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
476 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
478 /* Disable battery charge enabling bit */
479 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
481 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
482 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
485 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
486 * Setting these fields, together with default values of the
487 * other fields, results in programming the registers below as
489 * UTMIP_HSRX_CFG0 = 0x9168c000
490 * UTMIP_HSRX_CFG1 = 0x13
493 /* Set PLL enable delay count and Crystal frequency count */
494 val = readl(&usbctlr->utmip_hsrx_cfg0);
495 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
496 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
497 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
498 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
499 writel(val, &usbctlr->utmip_hsrx_cfg0);
501 /* Configure the UTMIP_HS_SYNC_START_DLY */
502 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
503 UTMIP_HS_SYNC_START_DLY_MASK,
504 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
506 /* Preceed the crystal clock disable by >100ns delay. */
509 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
510 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
512 if (controller->has_hostpc) {
513 if (config->periph_id == PERIPH_ID_USBD)
514 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
515 UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
516 if (config->periph_id == PERIPH_ID_USB2)
517 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
518 UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
519 if (config->periph_id == PERIPH_ID_USB3)
520 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
521 UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
523 /* Finished the per-controller init. */
525 /* De-assert UTMIP_RESET to bring out of reset. */
526 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
528 /* Wait for the phy clock to become valid in 100 ms */
529 for (loop_count = 100000; loop_count != 0; loop_count--) {
530 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
537 /* Disable ICUSB FS/LS transceiver */
538 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
540 /* Select UTMI parallel interface */
541 init_phy_mux(config, PTS_UTMI, init);
543 /* Deassert power down state */
544 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
545 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
546 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
547 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
549 if (controller->has_hostpc) {
551 * BIAS Pad Power Down is common among all 3 USB
552 * controllers and can be controlled from USB1 only.
554 usb1ctlr = (struct usb_ctlr *)
555 ((unsigned long)config->reg & USB1_ADDR_MASK);
556 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
558 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
559 UTMIP_FORCE_PDTRK_POWERDOWN);
564 #ifdef CONFIG_USB_ULPI
565 /* if board file does not set a ULPI reference frequency we default to 24MHz */
566 #ifndef CONFIG_ULPI_REF_CLK
567 #define CONFIG_ULPI_REF_CLK 24000000
570 /* set up the ULPI USB controller with the parameters provided */
571 static int init_ulpi_usb_controller(struct fdt_usb *config,
572 enum usb_init_type init)
576 struct ulpi_viewport ulpi_vp;
577 struct usb_ctlr *usbctlr = config->reg;
579 /* set up ULPI reference clock on pllp_out4 */
580 clock_enable(PERIPH_ID_DEV2_OUT);
581 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
584 if (dm_gpio_is_valid(&config->phy_reset_gpio)) {
585 dm_gpio_set_value(&config->phy_reset_gpio, 0);
587 dm_gpio_set_value(&config->phy_reset_gpio, 1);
590 /* Reset the usb controller */
591 clock_enable(config->periph_id);
592 usbf_reset_controller(config, usbctlr);
594 /* enable pinmux bypass */
595 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
596 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
598 /* Select ULPI parallel interface */
599 init_phy_mux(config, PTS_ULPI, init);
601 /* enable ULPI transceiver */
602 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
604 /* configure ULPI transceiver timings */
606 writel(val, &usbctlr->ulpi_timing_ctrl_1);
608 val |= ULPI_DATA_TRIMMER_SEL(4);
609 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
610 val |= ULPI_DIR_TRIMMER_SEL(4);
611 writel(val, &usbctlr->ulpi_timing_ctrl_1);
614 val |= ULPI_DATA_TRIMMER_LOAD;
615 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
616 val |= ULPI_DIR_TRIMMER_LOAD;
617 writel(val, &usbctlr->ulpi_timing_ctrl_1);
619 /* set up phy for host operation with external vbus supply */
620 ulpi_vp.port_num = 0;
621 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
623 if (ulpi_init(&ulpi_vp)) {
624 printf("Tegra ULPI viewport init failed\n");
628 ulpi_set_vbus(&ulpi_vp, 1, 1);
629 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
631 /* enable wakeup events */
632 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
634 /* Enable and wait for the phy clock to become valid in 100 ms */
635 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
636 for (loop_count = 100000; loop_count != 0; loop_count--) {
637 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
643 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
648 static int init_ulpi_usb_controller(struct fdt_usb *config,
649 enum usb_init_type init)
651 printf("No code to set up ULPI controller, please enable"
652 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
657 static void config_clock(const u32 timing[])
659 clock_start_pll(CLOCK_ID_USB,
660 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
661 timing[PARAM_CPCON], timing[PARAM_LFCON]);
664 static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
666 const char *phy, *mode;
668 config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
669 mode = fdt_getprop(blob, node, "dr_mode", NULL);
671 if (0 == strcmp(mode, "host"))
672 config->dr_mode = DR_MODE_HOST;
673 else if (0 == strcmp(mode, "peripheral"))
674 config->dr_mode = DR_MODE_DEVICE;
675 else if (0 == strcmp(mode, "otg"))
676 config->dr_mode = DR_MODE_OTG;
678 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
680 return -FDT_ERR_NOTFOUND;
683 config->dr_mode = DR_MODE_HOST;
686 phy = fdt_getprop(blob, node, "phy_type", NULL);
687 config->utmi = phy && 0 == strcmp("utmi", phy);
688 config->ulpi = phy && 0 == strcmp("ulpi", phy);
689 config->enabled = fdtdec_get_is_enabled(blob, node);
690 config->has_legacy_mode = fdtdec_get_bool(blob, node,
691 "nvidia,has-legacy-mode");
692 if (config->has_legacy_mode)
693 port_addr_clear_csc = (unsigned long)config->reg;
694 config->periph_id = clock_decode_periph_id(blob, node);
695 if (config->periph_id == PERIPH_ID_NONE) {
696 debug("%s: Missing/invalid peripheral ID\n", __func__);
697 return -FDT_ERR_NOTFOUND;
699 gpio_request_by_name_nodev(blob, node, "nvidia,vbus-gpio", 0,
700 &config->vbus_gpio, GPIOD_IS_OUT);
701 gpio_request_by_name_nodev(blob, node, "nvidia,phy-reset-gpio", 0,
702 &config->phy_reset_gpio, GPIOD_IS_OUT);
703 debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
704 "vbus=%d, phy_reset=%d, dr_mode=%d\n",
705 config->enabled, config->has_legacy_mode, config->utmi,
706 config->ulpi, config->periph_id,
707 gpio_get_number(&config->vbus_gpio),
708 gpio_get_number(&config->phy_reset_gpio), config->dr_mode);
714 * process_usb_nodes() - Process a list of USB nodes, adding them to our list
717 * @node_list: list of nodes to process (any <=0 are ignored)
718 * @count: number of nodes to process
719 * @id: controller type (enum usb_ctlr_type)
721 * Return: 0 - ok, -1 - error
723 static int process_usb_nodes(const void *blob, int node_list[], int count,
724 enum usb_ctlr_type id)
726 struct fdt_usb config;
731 for (i = 0; i < count; i++) {
732 if (port_count == USB_PORTS_MAX) {
733 printf("tegrausb: Cannot register more than %d ports\n",
738 debug("USB %d: ", i);
742 if (fdt_decode_usb(blob, node, &config)) {
743 debug("Cannot decode USB node %s\n",
744 fdt_get_name(blob, node, NULL));
748 config_clock(get_pll_timing(
749 &fdt_usb_controllers[id]));
753 config.initialized = 0;
755 /* add new USB port to the list of available ports */
756 port[port_count++] = config;
762 int usb_process_devicetree(const void *blob)
764 int node_list[USB_PORTS_MAX];
768 for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
769 controller = &fdt_usb_controllers[i];
771 count = fdtdec_find_aliases_for_id(blob, "usb",
772 controller->compat, node_list, USB_PORTS_MAX);
774 err = process_usb_nodes(blob, node_list, count, i);
776 printf("%s: Error processing USB node!\n",
781 if (i == ARRAY_SIZE(fdt_usb_controllers))
788 * Start up the given port number (ports are numbered from 0 on each board).
789 * This returns values for the appropriate hccr and hcor addresses to use for
790 * USB EHCI operations.
792 * @param index port number to start
793 * @param hccr returns start address of EHCI HCCR registers
794 * @param hcor returns start address of EHCI HCOR registers
795 * @return 0 if ok, -1 on error (generally invalid port number)
797 int ehci_hcd_init(int index, enum usb_init_type init,
798 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
800 struct fdt_usb *config;
801 struct usb_ctlr *usbctlr;
803 if (index >= port_count)
806 config = &port[index];
807 ehci_set_controller_priv(index, config);
811 switch (config->dr_mode) {
816 printf("tegrausb: Invalid dr_mode %d for host mode\n",
821 case USB_INIT_DEVICE:
822 if (config->periph_id != PERIPH_ID_USBD) {
823 printf("tegrausb: Device mode only supported on first USB controller\n");
827 printf("tegrausb: Device mode only supported with UTMI PHY\n");
830 switch (config->dr_mode) {
835 printf("tegrausb: Invalid dr_mode %d for device mode\n",
841 printf("tegrausb: Unknown USB_INIT_* %d\n", init);
845 /* skip init, if the port is already initialized */
846 if (config->initialized && config->init_type == init)
849 if (config->utmi && init_utmi_usb_controller(config, init)) {
850 printf("tegrausb: Cannot init port %d\n", index);
854 if (config->ulpi && init_ulpi_usb_controller(config, init)) {
855 printf("tegrausb: Cannot init port %d\n", index);
859 set_up_vbus(config, init);
861 config->initialized = 1;
862 config->init_type = init;
865 usbctlr = config->reg;
866 *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
867 *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
873 * Bring down the specified USB controller
875 int ehci_hcd_stop(int index)
877 struct usb_ctlr *usbctlr;
879 usbctlr = port[index].reg;
881 /* Stop controller */
882 writel(0, &usbctlr->usb_cmd);
885 /* Initiate controller reset */
886 writel(2, &usbctlr->usb_cmd);
889 port[index].initialized = 0;