blob: 554145a2506f5f999ec20173b33e2d60dcb7c4bf [file] [log] [blame]
Simon Glass87f938c2012-02-27 10:52:49 +00001/*
Lucas Stach7ae18f32013-02-07 07:16:29 +00002 * Copyright (c) 2011 The Chromium OS Authors.
Jim Lin8b3f7bf2012-06-24 20:40:57 +00003 * Copyright (c) 2009-2012 NVIDIA Corporation
Lucas Stach7ae18f32013-02-07 07:16:29 +00004 * Copyright (c) 2013 Lucas Stach
Simon Glass87f938c2012-02-27 10:52:49 +00005 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000026#include <asm/errno.h>
27#include <asm/io.h>
28#include <asm-generic/gpio.h>
29#include <asm/arch/clock.h>
30#include <asm/arch-tegra/usb.h>
Simon Glass87f938c2012-02-27 10:52:49 +000031#include <usb.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000032#include <usb/ulpi.h>
33#include <libfdt.h>
34#include <fdtdec.h>
Simon Glass87f938c2012-02-27 10:52:49 +000035
36#include "ehci.h"
Simon Glass87f938c2012-02-27 10:52:49 +000037
Lucas Stach7ae18f32013-02-07 07:16:29 +000038#ifdef CONFIG_USB_ULPI
39 #ifndef CONFIG_USB_ULPI_VIEWPORT
40 #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
41 define CONFIG_USB_ULPI_VIEWPORT"
42 #endif
43#endif
44
45enum {
46 USB_PORTS_MAX = 3, /* Maximum ports we allow */
47};
48
49/* Parameters we need for USB */
50enum {
51 PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
52 PARAM_DIVM, /* PLL INPUT DIVIDER */
53 PARAM_DIVP, /* POST DIVIDER (2^N) */
54 PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
55 PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
56 PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
57 PARAM_STABLE_COUNT, /* PLL-U STABLE count */
58 PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
59 PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
60 PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
61 PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
62
63 PARAM_COUNT
64};
65
66/* Possible port types (dual role mode) */
67enum dr_mode {
68 DR_MODE_NONE = 0,
69 DR_MODE_HOST, /* supports host operation */
70 DR_MODE_DEVICE, /* supports device operation */
71 DR_MODE_OTG, /* supports both */
72};
73
74/* Information about a USB port */
75struct fdt_usb {
76 struct usb_ctlr *reg; /* address of registers in physical memory */
77 unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
78 unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
79 unsigned enabled:1; /* 1 to enable, 0 to disable */
80 unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
81 unsigned initialized:1; /* has this port already been initialized? */
82 enum dr_mode dr_mode; /* dual role mode */
83 enum periph_id periph_id;/* peripheral id */
84 struct fdt_gpio_state vbus_gpio; /* GPIO for vbus enable */
85 struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
86};
87
88static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
89static unsigned port_count; /* Number of available ports */
90
91/*
92 * This table has USB timing parameters for each Oscillator frequency we
93 * support. There are four sets of values:
94 *
95 * 1. PLLU configuration information (reference clock is osc/clk_m and
96 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
97 *
98 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
99 * ----------------------------------------------------------------------
100 * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
101 * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
102 * Filter frequency (MHz) 1 4.8 6 2
103 * CPCON 1100b 0011b 1100b 1100b
104 * LFCON0 0 0 0 0
105 *
106 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
107 *
108 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
109 * ---------------------------------------------------------------------------
110 * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
111 * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
112 * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
113 * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
114 *
115 * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
116 * SessEnd. Each of these signals have their own debouncer and for each of
117 * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
118 * BIAS_DEBOUNCE_B).
119 *
120 * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
121 * 0xffff -> No debouncing at all
122 * <n> ms = <n> *1000 / (1/19.2MHz) / 4
123 *
124 * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
125 * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
126 *
127 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
128 * values, so we can keep those to default.
129 *
130 * 4. The 20 microsecond delay after bias cell operation.
131 */
132static const unsigned usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
133 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
134 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
135 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
136 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
137 { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
138};
139
140/* UTMIP Idle Wait Delay */
141static const u8 utmip_idle_wait_delay = 17;
142
143/* UTMIP Elastic limit */
144static const u8 utmip_elastic_limit = 16;
145
146/* UTMIP High Speed Sync Start Delay */
147static const u8 utmip_hs_sync_start_delay = 9;
Simon Glass87f938c2012-02-27 10:52:49 +0000148
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000149/*
150 * A known hardware issue where Connect Status Change bit of PORTSC register
151 * of USB1 controller will be set after Port Reset.
152 * We have to clear it in order for later device enumeration to proceed.
153 * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
154 * in "ehci-hcd.c".
155 */
156void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
157{
158 mdelay(50);
159 if (((u32) status_reg & TEGRA_USB_ADDR_MASK) != TEGRA_USB1_BASE)
160 return;
161 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
162 if (ehci_readl(status_reg) & EHCI_PS_CSC)
163 *reg |= EHCI_PS_CSC;
164}
Simon Glass87f938c2012-02-27 10:52:49 +0000165
Lucas Stach7ae18f32013-02-07 07:16:29 +0000166/* Put the port into host mode */
167static void set_host_mode(struct fdt_usb *config)
168{
169 /*
170 * If we are an OTG port, check if remote host is driving VBus and
171 * bail out in this case.
172 */
173 if (config->dr_mode == DR_MODE_OTG &&
174 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
175 return;
176
177 /*
178 * If not driving, we set the GPIO to enable VBUS. We assume
179 * that the pinmux is set up correctly for this.
180 */
181 if (fdt_gpio_isvalid(&config->vbus_gpio)) {
182 fdtdec_setup_gpio(&config->vbus_gpio);
183 gpio_direction_output(config->vbus_gpio.gpio,
184 (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
185 0 : 1);
186 debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
187 (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
188 "low" : "high");
189 }
190}
191
192void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
193{
194 /* Reset the USB controller with 2us delay */
195 reset_periph(config->periph_id, 2);
196
197 /*
198 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
199 * base address
200 */
201 if (config->has_legacy_mode)
202 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
203
204 /* Put UTMIP1/3 in reset */
205 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
206
207 /* Enable the UTMIP PHY */
208 if (config->utmi)
209 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
210}
211
212/* set up the UTMI USB controller with the parameters provided */
213static int init_utmi_usb_controller(struct fdt_usb *config)
214{
215 u32 val;
216 int loop_count;
217 const unsigned *timing;
218 struct usb_ctlr *usbctlr = config->reg;
219
220 clock_enable(config->periph_id);
221
222 /* Reset the usb controller */
223 usbf_reset_controller(config, usbctlr);
224
225 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
226 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
227
228 /* Follow the crystal clock disable by >100ns delay */
229 udelay(1);
230
231 /*
232 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
233 * mux must be switched to actually use a_sess_vld threshold.
234 */
235 if (fdt_gpio_isvalid(&config->vbus_gpio)) {
236 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
237 VBUS_SENSE_CTL_MASK,
238 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
239 }
240
241 /*
242 * PLL Delay CONFIGURATION settings. The following parameters control
243 * the bring up of the plls.
244 */
245 timing = usb_pll[clock_get_osc_freq()];
246
247 val = readl(&usbctlr->utmip_misc_cfg1);
248 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
249 timing[PARAM_STABLE_COUNT] << UTMIP_PLLU_STABLE_COUNT_SHIFT);
250 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
251 timing[PARAM_ACTIVE_DELAY_COUNT] <<
252 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
253 writel(val, &usbctlr->utmip_misc_cfg1);
254
255 /* Set PLL enable delay count and crystal frequency count */
256 val = readl(&usbctlr->utmip_pll_cfg1);
257 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
258 timing[PARAM_ENABLE_DELAY_COUNT] <<
259 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
260 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
261 timing[PARAM_XTAL_FREQ_COUNT] <<
262 UTMIP_XTAL_FREQ_COUNT_SHIFT);
263 writel(val, &usbctlr->utmip_pll_cfg1);
264
265 /* Setting the tracking length time */
266 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
267 UTMIP_BIAS_PDTRK_COUNT_MASK,
268 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
269
270 /* Program debounce time for VBUS to become valid */
271 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
272 UTMIP_DEBOUNCE_CFG0_MASK,
273 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
274
275 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
276
277 /* Disable battery charge enabling bit */
278 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
279
280 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
281 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
282
283 /*
284 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
285 * Setting these fields, together with default values of the
286 * other fields, results in programming the registers below as
287 * follows:
288 * UTMIP_HSRX_CFG0 = 0x9168c000
289 * UTMIP_HSRX_CFG1 = 0x13
290 */
291
292 /* Set PLL enable delay count and Crystal frequency count */
293 val = readl(&usbctlr->utmip_hsrx_cfg0);
294 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
295 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
296 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
297 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
298 writel(val, &usbctlr->utmip_hsrx_cfg0);
299
300 /* Configure the UTMIP_HS_SYNC_START_DLY */
301 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
302 UTMIP_HS_SYNC_START_DLY_MASK,
303 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
304
305 /* Preceed the crystal clock disable by >100ns delay. */
306 udelay(1);
307
308 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
309 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
310
311 /* Finished the per-controller init. */
312
313 /* De-assert UTMIP_RESET to bring out of reset. */
314 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
315
316 /* Wait for the phy clock to become valid in 100 ms */
317 for (loop_count = 100000; loop_count != 0; loop_count--) {
318 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
319 break;
320 udelay(1);
321 }
322 if (!loop_count)
323 return -1;
324
325 /* Disable ICUSB FS/LS transceiver */
326 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
327
328 /* Select UTMI parallel interface */
329 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
330 PTS_UTMI << PTS_SHIFT);
331 clrbits_le32(&usbctlr->port_sc1, STS);
332
333 /* Deassert power down state */
334 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
335 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
336 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
337 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
338
339 return 0;
340}
341
342#ifdef CONFIG_USB_ULPI
343/* if board file does not set a ULPI reference frequency we default to 24MHz */
344#ifndef CONFIG_ULPI_REF_CLK
345#define CONFIG_ULPI_REF_CLK 24000000
346#endif
347
348/* set up the ULPI USB controller with the parameters provided */
349static int init_ulpi_usb_controller(struct fdt_usb *config)
350{
351 u32 val;
352 int loop_count;
353 struct ulpi_viewport ulpi_vp;
354 struct usb_ctlr *usbctlr = config->reg;
355
356 /* set up ULPI reference clock on pllp_out4 */
357 clock_enable(PERIPH_ID_DEV2_OUT);
358 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
359
360 /* reset ULPI phy */
361 if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
362 fdtdec_setup_gpio(&config->phy_reset_gpio);
363 gpio_direction_output(config->phy_reset_gpio.gpio, 0);
364 mdelay(5);
365 gpio_set_value(config->phy_reset_gpio.gpio, 1);
366 }
367
368 /* Reset the usb controller */
369 clock_enable(config->periph_id);
370 usbf_reset_controller(config, usbctlr);
371
372 /* enable pinmux bypass */
373 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
374 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
375
376 /* Select ULPI parallel interface */
377 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
378
379 /* enable ULPI transceiver */
380 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
381
382 /* configure ULPI transceiver timings */
383 val = 0;
384 writel(val, &usbctlr->ulpi_timing_ctrl_1);
385
386 val |= ULPI_DATA_TRIMMER_SEL(4);
387 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
388 val |= ULPI_DIR_TRIMMER_SEL(4);
389 writel(val, &usbctlr->ulpi_timing_ctrl_1);
390 udelay(10);
391
392 val |= ULPI_DATA_TRIMMER_LOAD;
393 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
394 val |= ULPI_DIR_TRIMMER_LOAD;
395 writel(val, &usbctlr->ulpi_timing_ctrl_1);
396
397 /* set up phy for host operation with external vbus supply */
398 ulpi_vp.port_num = 0;
399 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
400
401 if (ulpi_init(&ulpi_vp)) {
402 printf("Tegra ULPI viewport init failed\n");
403 return -1;
404 }
405
406 ulpi_set_vbus(&ulpi_vp, 1, 1);
407 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
408
409 /* enable wakeup events */
410 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
411
412 /* Enable and wait for the phy clock to become valid in 100 ms */
413 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
414 for (loop_count = 100000; loop_count != 0; loop_count--) {
415 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
416 break;
417 udelay(1);
418 }
419 if (!loop_count)
420 return -1;
421 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
422
423 return 0;
424}
425#else
426static int init_ulpi_usb_controller(struct fdt_usb *config)
427{
428 printf("No code to set up ULPI controller, please enable"
429 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
430 return -1;
431}
432#endif
433
434static void config_clock(const u32 timing[])
435{
436 clock_start_pll(CLOCK_ID_USB,
437 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
438 timing[PARAM_CPCON], timing[PARAM_LFCON]);
439}
440
Lucas Stach7ae18f32013-02-07 07:16:29 +0000441int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
442{
443 const char *phy, *mode;
444
445 config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
446 mode = fdt_getprop(blob, node, "dr_mode", NULL);
447 if (mode) {
448 if (0 == strcmp(mode, "host"))
449 config->dr_mode = DR_MODE_HOST;
450 else if (0 == strcmp(mode, "peripheral"))
451 config->dr_mode = DR_MODE_DEVICE;
452 else if (0 == strcmp(mode, "otg"))
453 config->dr_mode = DR_MODE_OTG;
454 else {
455 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
456 mode);
457 return -FDT_ERR_NOTFOUND;
458 }
459 } else {
460 config->dr_mode = DR_MODE_HOST;
461 }
462
463 phy = fdt_getprop(blob, node, "phy_type", NULL);
464 config->utmi = phy && 0 == strcmp("utmi", phy);
465 config->ulpi = phy && 0 == strcmp("ulpi", phy);
466 config->enabled = fdtdec_get_is_enabled(blob, node);
467 config->has_legacy_mode = fdtdec_get_bool(blob, node,
468 "nvidia,has-legacy-mode");
469 config->periph_id = clock_decode_periph_id(blob, node);
470 if (config->periph_id == PERIPH_ID_NONE) {
471 debug("%s: Missing/invalid peripheral ID\n", __func__);
472 return -FDT_ERR_NOTFOUND;
473 }
474 fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
475 fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
476 &config->phy_reset_gpio);
477 debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
478 "vbus=%d, phy_reset=%d, dr_mode=%d\n",
479 config->enabled, config->has_legacy_mode, config->utmi,
480 config->ulpi, config->periph_id, config->vbus_gpio.gpio,
481 config->phy_reset_gpio.gpio, config->dr_mode);
482
483 return 0;
484}
485
486int board_usb_init(const void *blob)
487{
488 struct fdt_usb config;
489 enum clock_osc_freq freq;
490 int node_list[USB_PORTS_MAX];
491 int node, count, i;
492
493 /* Set up the USB clocks correctly based on our oscillator frequency */
494 freq = clock_get_osc_freq();
495 config_clock(usb_pll[freq]);
496
497 /* count may return <0 on error */
498 count = fdtdec_find_aliases_for_id(blob, "usb",
499 COMPAT_NVIDIA_TEGRA20_USB, node_list, USB_PORTS_MAX);
500 for (i = 0; i < count; i++) {
501 if (port_count == USB_PORTS_MAX) {
502 printf("tegrausb: Cannot register more than %d ports\n",
503 USB_PORTS_MAX);
504 return -1;
505 }
506
507 debug("USB %d: ", i);
508 node = node_list[i];
509 if (!node)
510 continue;
511 if (fdt_decode_usb(blob, node, &config)) {
512 debug("Cannot decode USB node %s\n",
513 fdt_get_name(blob, node, NULL));
514 return -1;
515 }
516 config.initialized = 0;
517
518 /* add new USB port to the list of available ports */
519 port[port_count++] = config;
520 }
521
522 return 0;
523}
524
Lucas Stachd7a55e12013-02-07 07:16:30 +0000525/**
526 * Start up the given port number (ports are numbered from 0 on each board).
527 * This returns values for the appropriate hccr and hcor addresses to use for
528 * USB EHCI operations.
529 *
530 * @param index port number to start
531 * @param hccr returns start address of EHCI HCCR registers
532 * @param hcor returns start address of EHCI HCOR registers
533 * @return 0 if ok, -1 on error (generally invalid port number)
Simon Glass87f938c2012-02-27 10:52:49 +0000534 */
Lucas Stach676ae062012-09-26 00:14:35 +0200535int ehci_hcd_init(int index, struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Simon Glass87f938c2012-02-27 10:52:49 +0000536{
Lucas Stachd7a55e12013-02-07 07:16:30 +0000537 struct fdt_usb *config;
538 struct usb_ctlr *usbctlr;
Simon Glass87f938c2012-02-27 10:52:49 +0000539
Lucas Stachd7a55e12013-02-07 07:16:30 +0000540 if (index >= port_count)
Simon Glass87f938c2012-02-27 10:52:49 +0000541 return -1;
542
Lucas Stachd7a55e12013-02-07 07:16:30 +0000543 config = &port[index];
Simon Glass87f938c2012-02-27 10:52:49 +0000544
Lucas Stachd7a55e12013-02-07 07:16:30 +0000545 /* skip init, if the port is already initialized */
546 if (config->initialized)
547 goto success;
548
549 if (config->utmi && init_utmi_usb_controller(config)) {
550 printf("tegrausb: Cannot init port %d\n", index);
551 return -1;
552 }
553
554 if (config->ulpi && init_ulpi_usb_controller(config)) {
555 printf("tegrausb: Cannot init port %d\n", index);
556 return -1;
557 }
558
559 set_host_mode(config);
560
561 config->initialized = 1;
562
563success:
564 usbctlr = config->reg;
565 *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
566 *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
Simon Glass87f938c2012-02-27 10:52:49 +0000567 return 0;
568}
569
570/*
Lucas Stachd7a55e12013-02-07 07:16:30 +0000571 * Bring down the specified USB controller
Simon Glass87f938c2012-02-27 10:52:49 +0000572 */
Lucas Stach676ae062012-09-26 00:14:35 +0200573int ehci_hcd_stop(int index)
Simon Glass87f938c2012-02-27 10:52:49 +0000574{
Lucas Stachd7a55e12013-02-07 07:16:30 +0000575 struct usb_ctlr *usbctlr;
576
577 usbctlr = port[index].reg;
578
579 /* Stop controller */
580 writel(0, &usbctlr->usb_cmd);
581 udelay(1000);
582
583 /* Initiate controller reset */
584 writel(2, &usbctlr->usb_cmd);
585 udelay(1000);
586
587 port[index].initialized = 0;
588
589 return 0;
Simon Glass87f938c2012-02-27 10:52:49 +0000590}