]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/usb/host/ehci-tegra.c
dm: usb: tegra: Move most of init/uninit into a function
[karo-tx-uboot.git] / drivers / usb / host / ehci-tegra.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * Copyright (c) 2009-2013 NVIDIA Corporation
4  * Copyright (c) 2013 Lucas Stach
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <asm/errno.h>
11 #include <asm/io.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>
16 #include <usb.h>
17 #include <usb/ulpi.h>
18 #include <libfdt.h>
19 #include <fdtdec.h>
20
21 #include "ehci.h"
22
23 #define USB1_ADDR_MASK  0xFFFF0000
24
25 #define HOSTPC1_DEVLC   0x84
26 #define HOSTPC1_PSPD(x)         (((x) >> 25) & 0x3)
27
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"
32         #endif
33 #endif
34
35 enum {
36         USB_PORTS_MAX   = 3,            /* Maximum ports we allow */
37 };
38
39 /* Parameters we need for USB */
40 enum {
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 */
52
53         PARAM_COUNT
54 };
55
56 /* Possible port types (dual role mode) */
57 enum dr_mode {
58         DR_MODE_NONE = 0,
59         DR_MODE_HOST,           /* supports host operation */
60         DR_MODE_DEVICE,         /* supports device operation */
61         DR_MODE_OTG,            /* supports both */
62 };
63
64 enum usb_ctlr_type {
65         USB_CTLR_T20,
66         USB_CTLR_T30,
67         USB_CTLR_T114,
68
69         USB_CTRL_COUNT,
70 };
71
72 /* Information about a USB port */
73 struct fdt_usb {
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 */
86 };
87
88 static struct fdt_usb port[USB_PORTS_MAX];      /* List of valid USB ports */
89 static 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  */
132 static const unsigned T20_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 static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
141         /* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
142         { 0x3C0, 0x0D, 0x00, 0xC,   1,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
143         { 0x0C8, 0x04, 0x00, 0x3,   0,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
144         { 0x3C0, 0x0C, 0x00, 0xC,   1,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
145         { 0x3C0, 0x1A, 0x00, 0xC,   1,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
146 };
147
148 static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
149         /* DivN, DivM, DivP, CPCON, LFCON, Delays             Debounce, Bias */
150         { 0x3C0, 0x0D, 0x00, 0xC,   2,  0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
151         { 0x0C8, 0x04, 0x00, 0x3,   2,  0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
152         { 0x3C0, 0x0C, 0x00, 0xC,   2,  0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
153         { 0x3C0, 0x1A, 0x00, 0xC,   2,  0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
154 };
155
156 /* UTMIP Idle Wait Delay */
157 static const u8 utmip_idle_wait_delay = 17;
158
159 /* UTMIP Elastic limit */
160 static const u8 utmip_elastic_limit = 16;
161
162 /* UTMIP High Speed Sync Start Delay */
163 static const u8 utmip_hs_sync_start_delay = 9;
164
165 struct fdt_usb_controller {
166         int compat;
167         /* flag to determine whether controller supports hostpc register */
168         u32 has_hostpc:1;
169         const unsigned *pll_parameter;
170 };
171
172 static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = {
173         {
174                 .compat         = COMPAT_NVIDIA_TEGRA20_USB,
175                 .has_hostpc     = 0,
176                 .pll_parameter  = (const unsigned *)T20_usb_pll,
177         },
178         {
179                 .compat         = COMPAT_NVIDIA_TEGRA30_USB,
180                 .has_hostpc     = 1,
181                 .pll_parameter  = (const unsigned *)T30_usb_pll,
182         },
183         {
184                 .compat         = COMPAT_NVIDIA_TEGRA114_USB,
185                 .has_hostpc     = 1,
186                 .pll_parameter  = (const unsigned *)T114_usb_pll,
187         },
188 };
189
190 /*
191  * A known hardware issue where Connect Status Change bit of PORTSC register
192  * of USB1 controller will be set after Port Reset.
193  * We have to clear it in order for later device enumeration to proceed.
194  */
195 static void tegra_ehci_powerup_fixup(struct ehci_ctrl *ctrl,
196                                      uint32_t *status_reg, uint32_t *reg)
197 {
198         struct fdt_usb *config = ctrl->priv;
199         struct fdt_usb_controller *controller;
200
201         controller = &fdt_usb_controllers[config->type];
202         mdelay(50);
203         /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
204         if (controller->has_hostpc)
205                 *reg |= EHCI_PS_PE;
206
207         if (!config->has_legacy_mode)
208                 return;
209         /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
210         if (ehci_readl(status_reg) & EHCI_PS_CSC)
211                 *reg |= EHCI_PS_CSC;
212 }
213
214 static void tegra_ehci_set_usbmode(struct ehci_ctrl *ctrl)
215 {
216         struct fdt_usb *config = ctrl->priv;
217         struct usb_ctlr *usbctlr;
218         uint32_t tmp;
219
220         usbctlr = config->reg;
221
222         tmp = ehci_readl(&usbctlr->usb_mode);
223         tmp |= USBMODE_CM_HC;
224         ehci_writel(&usbctlr->usb_mode, tmp);
225 }
226
227 static int tegra_ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
228 {
229         struct fdt_usb *config = ctrl->priv;
230         struct fdt_usb_controller *controller;
231         uint32_t tmp;
232         uint32_t *reg_ptr;
233
234         controller = &fdt_usb_controllers[config->type];
235         if (controller->has_hostpc) {
236                 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd +
237                                 HOSTPC1_DEVLC);
238                 tmp = ehci_readl(reg_ptr);
239                 return HOSTPC1_PSPD(tmp);
240         } else
241                 return PORTSC_PSPD(reg);
242 }
243
244 /* Set up VBUS for host/device mode */
245 static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
246 {
247         /*
248          * If we are an OTG port initializing in host mode,
249          * check if remote host is driving VBus and bail out in this case.
250          */
251         if (init == USB_INIT_HOST &&
252             config->dr_mode == DR_MODE_OTG &&
253             (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
254                 printf("tegrausb: VBUS input active; not enabling as host\n");
255                 return;
256         }
257
258         if (dm_gpio_is_valid(&config->vbus_gpio)) {
259                 int vbus_value;
260
261                 vbus_value = (init == USB_INIT_HOST);
262                 dm_gpio_set_value(&config->vbus_gpio, vbus_value);
263
264                 debug("set_up_vbus: GPIO %d %d\n",
265                       gpio_get_number(&config->vbus_gpio), vbus_value);
266         }
267 }
268
269 static void usbf_reset_controller(struct fdt_usb *config,
270                                   struct usb_ctlr *usbctlr)
271 {
272         /* Reset the USB controller with 2us delay */
273         reset_periph(config->periph_id, 2);
274
275         /*
276          * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
277          * base address
278          */
279         if (config->has_legacy_mode)
280                 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
281
282         /* Put UTMIP1/3 in reset */
283         setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
284
285         /* Enable the UTMIP PHY */
286         if (config->utmi)
287                 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
288 }
289
290 static const unsigned *get_pll_timing(struct fdt_usb_controller *controller)
291 {
292         const unsigned *timing;
293
294         timing = controller->pll_parameter +
295                 clock_get_osc_freq() * PARAM_COUNT;
296
297         return timing;
298 }
299
300 /* select the PHY to use with a USB controller */
301 static void init_phy_mux(struct fdt_usb *config, uint pts,
302                          enum usb_init_type init)
303 {
304         struct usb_ctlr *usbctlr = config->reg;
305
306 #if defined(CONFIG_TEGRA20)
307         if (config->periph_id == PERIPH_ID_USBD) {
308                 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
309                                 pts << PTS1_SHIFT);
310                 clrbits_le32(&usbctlr->port_sc1, STS1);
311         } else {
312                 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
313                                 pts << PTS_SHIFT);
314                 clrbits_le32(&usbctlr->port_sc1, STS);
315         }
316 #else
317         /* Set to Host mode (if applicable) after Controller Reset was done */
318         clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
319                         (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
320         /*
321          * Select PHY interface after setting host mode.
322          * For device mode, the ordering requirement is not an issue, since
323          * only the first USB controller supports device mode, and that USB
324          * controller can only talk to a UTMI PHY, so the PHY selection is
325          * already made at reset time, so this write is a no-op.
326          */
327         clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
328                         pts << PTS_SHIFT);
329         clrbits_le32(&usbctlr->hostpc1_devlc, STS);
330 #endif
331 }
332
333 /* set up the UTMI USB controller with the parameters provided */
334 static int init_utmi_usb_controller(struct fdt_usb *config,
335                                     enum usb_init_type init)
336 {
337         struct fdt_usb_controller *controller;
338         u32 b_sess_valid_mask, val;
339         int loop_count;
340         const unsigned *timing;
341         struct usb_ctlr *usbctlr = config->reg;
342         struct clk_rst_ctlr *clkrst;
343         struct usb_ctlr *usb1ctlr;
344
345         clock_enable(config->periph_id);
346
347         /* Reset the usb controller */
348         usbf_reset_controller(config, usbctlr);
349
350         /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
351         clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
352
353         /* Follow the crystal clock disable by >100ns delay */
354         udelay(1);
355
356         b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
357         clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
358                         (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
359
360         /*
361          * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
362          * mux must be switched to actually use a_sess_vld threshold.
363          */
364         if (config->dr_mode == DR_MODE_OTG &&
365             dm_gpio_is_valid(&config->vbus_gpio))
366                 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
367                         VBUS_SENSE_CTL_MASK,
368                         VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
369
370         controller = &fdt_usb_controllers[config->type];
371         debug("controller=%p, type=%d\n", controller, config->type);
372
373         /*
374          * PLL Delay CONFIGURATION settings. The following parameters control
375          * the bring up of the plls.
376          */
377         timing = get_pll_timing(controller);
378
379         if (!controller->has_hostpc) {
380                 val = readl(&usbctlr->utmip_misc_cfg1);
381                 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
382                                 timing[PARAM_STABLE_COUNT] <<
383                                 UTMIP_PLLU_STABLE_COUNT_SHIFT);
384                 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
385                                 timing[PARAM_ACTIVE_DELAY_COUNT] <<
386                                 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
387                 writel(val, &usbctlr->utmip_misc_cfg1);
388
389                 /* Set PLL enable delay count and crystal frequency count */
390                 val = readl(&usbctlr->utmip_pll_cfg1);
391                 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
392                                 timing[PARAM_ENABLE_DELAY_COUNT] <<
393                                 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
394                 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
395                                 timing[PARAM_XTAL_FREQ_COUNT] <<
396                                 UTMIP_XTAL_FREQ_COUNT_SHIFT);
397                 writel(val, &usbctlr->utmip_pll_cfg1);
398         } else {
399                 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
400
401                 val = readl(&clkrst->crc_utmip_pll_cfg2);
402                 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
403                                 timing[PARAM_STABLE_COUNT] <<
404                                 UTMIP_PLLU_STABLE_COUNT_SHIFT);
405                 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
406                                 timing[PARAM_ACTIVE_DELAY_COUNT] <<
407                                 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
408                 writel(val, &clkrst->crc_utmip_pll_cfg2);
409
410                 /* Set PLL enable delay count and crystal frequency count */
411                 val = readl(&clkrst->crc_utmip_pll_cfg1);
412                 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
413                                 timing[PARAM_ENABLE_DELAY_COUNT] <<
414                                 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
415                 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
416                                 timing[PARAM_XTAL_FREQ_COUNT] <<
417                                 UTMIP_XTAL_FREQ_COUNT_SHIFT);
418                 writel(val, &clkrst->crc_utmip_pll_cfg1);
419
420                 /* Disable Power Down state for PLL */
421                 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
422                              PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
423                              PLL_ACTIVE_POWERDOWN);
424
425                 /* Recommended PHY settings for EYE diagram */
426                 val = readl(&usbctlr->utmip_xcvr_cfg0);
427                 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
428                                 0x4 << UTMIP_XCVR_SETUP_SHIFT);
429                 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
430                                 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
431                 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
432                                 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
433                 writel(val, &usbctlr->utmip_xcvr_cfg0);
434                 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
435                                 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
436                                 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
437
438                 /* Some registers can be controlled from USB1 only. */
439                 if (config->periph_id != PERIPH_ID_USBD) {
440                         clock_enable(PERIPH_ID_USBD);
441                         /* Disable Reset if in Reset state */
442                         reset_set_enable(PERIPH_ID_USBD, 0);
443                 }
444                 usb1ctlr = (struct usb_ctlr *)
445                         ((unsigned long)config->reg & USB1_ADDR_MASK);
446                 val = readl(&usb1ctlr->utmip_bias_cfg0);
447                 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
448                 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
449                                 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
450                 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
451                                 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
452                 writel(val, &usb1ctlr->utmip_bias_cfg0);
453
454                 /* Miscellaneous setting mentioned in Programming Guide */
455                 clrbits_le32(&usbctlr->utmip_misc_cfg0,
456                              UTMIP_SUSPEND_EXIT_ON_EDGE);
457         }
458
459         /* Setting the tracking length time */
460         clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
461                 UTMIP_BIAS_PDTRK_COUNT_MASK,
462                 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
463
464         /* Program debounce time for VBUS to become valid */
465         clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
466                 UTMIP_DEBOUNCE_CFG0_MASK,
467                 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
468
469         setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
470
471         /* Disable battery charge enabling bit */
472         setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
473
474         clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
475         setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
476
477         /*
478          * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
479          * Setting these fields, together with default values of the
480          * other fields, results in programming the registers below as
481          * follows:
482          *         UTMIP_HSRX_CFG0 = 0x9168c000
483          *         UTMIP_HSRX_CFG1 = 0x13
484          */
485
486         /* Set PLL enable delay count and Crystal frequency count */
487         val = readl(&usbctlr->utmip_hsrx_cfg0);
488         clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
489                 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
490         clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
491                 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
492         writel(val, &usbctlr->utmip_hsrx_cfg0);
493
494         /* Configure the UTMIP_HS_SYNC_START_DLY */
495         clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
496                 UTMIP_HS_SYNC_START_DLY_MASK,
497                 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
498
499         /* Preceed the crystal clock disable by >100ns delay. */
500         udelay(1);
501
502         /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
503         setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
504
505         if (controller->has_hostpc) {
506                 if (config->periph_id == PERIPH_ID_USBD)
507                         clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
508                                      UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
509                 if (config->periph_id == PERIPH_ID_USB2)
510                         clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
511                                      UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
512                 if (config->periph_id == PERIPH_ID_USB3)
513                         clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
514                                      UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
515         }
516         /* Finished the per-controller init. */
517
518         /* De-assert UTMIP_RESET to bring out of reset. */
519         clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
520
521         /* Wait for the phy clock to become valid in 100 ms */
522         for (loop_count = 100000; loop_count != 0; loop_count--) {
523                 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
524                         break;
525                 udelay(1);
526         }
527         if (!loop_count)
528                 return -ETIMEDOUT;
529
530         /* Disable ICUSB FS/LS transceiver */
531         clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
532
533         /* Select UTMI parallel interface */
534         init_phy_mux(config, PTS_UTMI, init);
535
536         /* Deassert power down state */
537         clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
538                 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
539         clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
540                 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
541
542         if (controller->has_hostpc) {
543                 /*
544                  * BIAS Pad Power Down is common among all 3 USB
545                  * controllers and can be controlled from USB1 only.
546                  */
547                 usb1ctlr = (struct usb_ctlr *)
548                         ((unsigned long)config->reg & USB1_ADDR_MASK);
549                 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
550                 udelay(25);
551                 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
552                              UTMIP_FORCE_PDTRK_POWERDOWN);
553         }
554         return 0;
555 }
556
557 #ifdef CONFIG_USB_ULPI
558 /* if board file does not set a ULPI reference frequency we default to 24MHz */
559 #ifndef CONFIG_ULPI_REF_CLK
560 #define CONFIG_ULPI_REF_CLK 24000000
561 #endif
562
563 /* set up the ULPI USB controller with the parameters provided */
564 static int init_ulpi_usb_controller(struct fdt_usb *config,
565                                     enum usb_init_type init)
566 {
567         u32 val;
568         int loop_count;
569         struct ulpi_viewport ulpi_vp;
570         struct usb_ctlr *usbctlr = config->reg;
571         int ret;
572
573         /* set up ULPI reference clock on pllp_out4 */
574         clock_enable(PERIPH_ID_DEV2_OUT);
575         clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
576
577         /* reset ULPI phy */
578         if (dm_gpio_is_valid(&config->phy_reset_gpio)) {
579                 dm_gpio_set_value(&config->phy_reset_gpio, 0);
580                 mdelay(5);
581                 dm_gpio_set_value(&config->phy_reset_gpio, 1);
582         }
583
584         /* Reset the usb controller */
585         clock_enable(config->periph_id);
586         usbf_reset_controller(config, usbctlr);
587
588         /* enable pinmux bypass */
589         setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
590                         ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
591
592         /* Select ULPI parallel interface */
593         init_phy_mux(config, PTS_ULPI, init);
594
595         /* enable ULPI transceiver */
596         setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
597
598         /* configure ULPI transceiver timings */
599         val = 0;
600         writel(val, &usbctlr->ulpi_timing_ctrl_1);
601
602         val |= ULPI_DATA_TRIMMER_SEL(4);
603         val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
604         val |= ULPI_DIR_TRIMMER_SEL(4);
605         writel(val, &usbctlr->ulpi_timing_ctrl_1);
606         udelay(10);
607
608         val |= ULPI_DATA_TRIMMER_LOAD;
609         val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
610         val |= ULPI_DIR_TRIMMER_LOAD;
611         writel(val, &usbctlr->ulpi_timing_ctrl_1);
612
613         /* set up phy for host operation with external vbus supply */
614         ulpi_vp.port_num = 0;
615         ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
616
617         ret = ulpi_init(&ulpi_vp);
618         if (ret) {
619                 printf("Tegra ULPI viewport init failed\n");
620                 return ret;
621         }
622
623         ulpi_set_vbus(&ulpi_vp, 1, 1);
624         ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
625
626         /* enable wakeup events */
627         setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
628
629         /* Enable and wait for the phy clock to become valid in 100 ms */
630         setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
631         for (loop_count = 100000; loop_count != 0; loop_count--) {
632                 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
633                         break;
634                 udelay(1);
635         }
636         if (!loop_count)
637                 return -ETIMEDOUT;
638         clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
639
640         return 0;
641 }
642 #else
643 static int init_ulpi_usb_controller(struct fdt_usb *config,
644                                     enum usb_init_type init)
645 {
646         printf("No code to set up ULPI controller, please enable"
647                         "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
648         return -ENOSYS;
649 }
650 #endif
651
652 static void config_clock(const u32 timing[])
653 {
654         clock_start_pll(CLOCK_ID_USB,
655                 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
656                 timing[PARAM_CPCON], timing[PARAM_LFCON]);
657 }
658
659 static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
660 {
661         const char *phy, *mode;
662
663         config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
664         mode = fdt_getprop(blob, node, "dr_mode", NULL);
665         if (mode) {
666                 if (0 == strcmp(mode, "host"))
667                         config->dr_mode = DR_MODE_HOST;
668                 else if (0 == strcmp(mode, "peripheral"))
669                         config->dr_mode = DR_MODE_DEVICE;
670                 else if (0 == strcmp(mode, "otg"))
671                         config->dr_mode = DR_MODE_OTG;
672                 else {
673                         debug("%s: Cannot decode dr_mode '%s'\n", __func__,
674                               mode);
675                         return -EINVAL;
676                 }
677         } else {
678                 config->dr_mode = DR_MODE_HOST;
679         }
680
681         phy = fdt_getprop(blob, node, "phy_type", NULL);
682         config->utmi = phy && 0 == strcmp("utmi", phy);
683         config->ulpi = phy && 0 == strcmp("ulpi", phy);
684         config->enabled = fdtdec_get_is_enabled(blob, node);
685         config->has_legacy_mode = fdtdec_get_bool(blob, node,
686                                                   "nvidia,has-legacy-mode");
687         config->periph_id = clock_decode_periph_id(blob, node);
688         if (config->periph_id == PERIPH_ID_NONE) {
689                 debug("%s: Missing/invalid peripheral ID\n", __func__);
690                 return -EINVAL;
691         }
692         gpio_request_by_name_nodev(blob, node, "nvidia,vbus-gpio", 0,
693                                    &config->vbus_gpio, GPIOD_IS_OUT);
694         gpio_request_by_name_nodev(blob, node, "nvidia,phy-reset-gpio", 0,
695                                    &config->phy_reset_gpio, GPIOD_IS_OUT);
696         debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
697                 "vbus=%d, phy_reset=%d, dr_mode=%d\n",
698                 config->enabled, config->has_legacy_mode, config->utmi,
699                 config->ulpi, config->periph_id,
700                 gpio_get_number(&config->vbus_gpio),
701                 gpio_get_number(&config->phy_reset_gpio), config->dr_mode);
702
703         return 0;
704 }
705
706 int usb_common_init(struct fdt_usb *config, enum usb_init_type init)
707 {
708         int ret = 0;
709
710         switch (init) {
711         case USB_INIT_HOST:
712                 switch (config->dr_mode) {
713                 case DR_MODE_HOST:
714                 case DR_MODE_OTG:
715                         break;
716                 default:
717                         printf("tegrausb: Invalid dr_mode %d for host mode\n",
718                                config->dr_mode);
719                         return -1;
720                 }
721                 break;
722         case USB_INIT_DEVICE:
723                 if (config->periph_id != PERIPH_ID_USBD) {
724                         printf("tegrausb: Device mode only supported on first USB controller\n");
725                         return -1;
726                 }
727                 if (!config->utmi) {
728                         printf("tegrausb: Device mode only supported with UTMI PHY\n");
729                         return -1;
730                 }
731                 switch (config->dr_mode) {
732                 case DR_MODE_DEVICE:
733                 case DR_MODE_OTG:
734                         break;
735                 default:
736                         printf("tegrausb: Invalid dr_mode %d for device mode\n",
737                                config->dr_mode);
738                         return -1;
739                 }
740                 break;
741         default:
742                 printf("tegrausb: Unknown USB_INIT_* %d\n", init);
743                 return -1;
744         }
745
746 #ifndef CONFIG_DM_USB
747         /* skip init, if the port is already initialized */
748         if (config->initialized && config->init_type == init)
749                 return 0;
750 #endif
751
752         debug("%d, %d\n", config->utmi, config->ulpi);
753         if (config->utmi)
754                 ret = init_utmi_usb_controller(config, init);
755         else if (config->ulpi)
756                 ret = init_ulpi_usb_controller(config, init);
757         if (ret)
758                 return ret;
759
760         set_up_vbus(config, init);
761
762         config->init_type = init;
763
764         return 0;
765 }
766
767 void usb_common_uninit(struct fdt_usb *priv)
768 {
769         struct usb_ctlr *usbctlr;
770
771         usbctlr = priv->reg;
772
773         /* Stop controller */
774         writel(0, &usbctlr->usb_cmd);
775         udelay(1000);
776
777         /* Initiate controller reset */
778         writel(2, &usbctlr->usb_cmd);
779         udelay(1000);
780 }
781
782 static const struct ehci_ops tegra_ehci_ops = {
783         .set_usb_mode           = tegra_ehci_set_usbmode,
784         .get_port_speed         = tegra_ehci_get_port_speed,
785         .powerup_fixup          = tegra_ehci_powerup_fixup,
786 };
787
788 /*
789  * process_usb_nodes() - Process a list of USB nodes, adding them to our list
790  *                      of USB ports.
791  * @blob:       fdt blob
792  * @node_list:  list of nodes to process (any <=0 are ignored)
793  * @count:      number of nodes to process
794  * @id:         controller type (enum usb_ctlr_type)
795  *
796  * Return:      0 - ok, -1 - error
797  */
798 static int process_usb_nodes(const void *blob, int node_list[], int count,
799                              enum usb_ctlr_type id)
800 {
801         struct fdt_usb config;
802         int node, i;
803         int clk_done = 0;
804
805         port_count = 0;
806         for (i = 0; i < count; i++) {
807                 if (port_count == USB_PORTS_MAX) {
808                         printf("tegrausb: Cannot register more than %d ports\n",
809                                 USB_PORTS_MAX);
810                         return -1;
811                 }
812
813                 debug("USB %d: ", i);
814                 node = node_list[i];
815                 if (!node)
816                         continue;
817                 if (fdt_decode_usb(blob, node, &config)) {
818                         debug("Cannot decode USB node %s\n",
819                               fdt_get_name(blob, node, NULL));
820                         return -1;
821                 }
822                 if (!clk_done) {
823                         config_clock(get_pll_timing(
824                                         &fdt_usb_controllers[id]));
825                         clk_done = 1;
826                 }
827                 config.type = id;
828                 config.initialized = 0;
829
830                 /* add new USB port to the list of available ports */
831                 port[port_count++] = config;
832         }
833
834         return 0;
835 }
836
837 int usb_process_devicetree(const void *blob)
838 {
839         int node_list[USB_PORTS_MAX];
840         int count, err = 0;
841         int i;
842
843         for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
844                 count = fdtdec_find_aliases_for_id(blob, "usb",
845                         fdt_usb_controllers[i].compat, node_list,
846                         USB_PORTS_MAX);
847                 if (count) {
848                         err = process_usb_nodes(blob, node_list, count, i);
849                         if (err)
850                                 printf("%s: Error processing USB node!\n",
851                                        __func__);
852                         return err;
853                 }
854         }
855
856         return err;
857 }
858
859 /**
860  * Start up the given port number (ports are numbered from 0 on each board).
861  * This returns values for the appropriate hccr and hcor addresses to use for
862  * USB EHCI operations.
863  *
864  * @param index port number to start
865  * @param hccr          returns start address of EHCI HCCR registers
866  * @param hcor          returns start address of EHCI HCOR registers
867  * @return 0 if ok, -1 on error (generally invalid port number)
868  */
869 int ehci_hcd_init(int index, enum usb_init_type init,
870                 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
871 {
872         struct fdt_usb *config;
873         struct usb_ctlr *usbctlr;
874         int ret;
875
876         if (index >= port_count)
877                 return -1;
878
879         config = &port[index];
880         ehci_set_controller_priv(index, config, &tegra_ehci_ops);
881
882         ret = usb_common_init(config, init);
883         if (ret) {
884                 printf("tegrausb: Cannot init port %d\n", index);
885                 return ret;
886         }
887
888         config->initialized = 1;
889
890         usbctlr = config->reg;
891         *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
892         *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
893
894         return 0;
895 }
896
897 /*
898  * Bring down the specified USB controller
899  */
900 int ehci_hcd_stop(int index)
901 {
902         usb_common_uninit(&port[index]);
903
904         port[index].initialized = 0;
905
906         return 0;
907 }