blob: 0e6b60e0d6a88c9fe74a708d8a08901c5338fd84 [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 Lin7e44d932013-06-21 19:05:47 +08003 * Copyright (c) 2009-2013 NVIDIA Corporation
Lucas Stach7ae18f32013-02-07 07:16:29 +00004 * Copyright (c) 2013 Lucas Stach
Simon Glass87f938c2012-02-27 10:52:49 +00005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Simon Glass87f938c2012-02-27 10:52:49 +00007 */
8
9#include <common.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000010#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>
Jim Lin7e44d932013-06-21 19:05:47 +080015#include <asm/arch-tegra/clk_rst.h>
Simon Glass87f938c2012-02-27 10:52:49 +000016#include <usb.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000017#include <usb/ulpi.h>
18#include <libfdt.h>
19#include <fdtdec.h>
Simon Glass87f938c2012-02-27 10:52:49 +000020
21#include "ehci.h"
Simon Glass87f938c2012-02-27 10:52:49 +000022
Jim Lin7e44d932013-06-21 19:05:47 +080023#define USB1_ADDR_MASK 0xFFFF0000
24
25#define HOSTPC1_DEVLC 0x84
26#define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3)
27
Lucas Stach7ae18f32013-02-07 07:16:29 +000028#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
35enum {
36 USB_PORTS_MAX = 3, /* Maximum ports we allow */
37};
38
39/* Parameters we need for USB */
40enum {
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) */
57enum 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
Simon Glass27f782b2015-03-25 12:22:20 -060064enum usb_ctlr_type {
65 USB_CTLR_T20,
66 USB_CTLR_T30,
67 USB_CTLR_T114,
68
69 USB_CTRL_COUNT,
70};
71
Lucas Stach7ae18f32013-02-07 07:16:29 +000072/* Information about a USB port */
73struct 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? */
Simon Glass27f782b2015-03-25 12:22:20 -060080 enum usb_ctlr_type type;
Stephen Warrena4539a22014-04-30 15:09:57 -060081 enum usb_init_type init_type;
Lucas Stach7ae18f32013-02-07 07:16:29 +000082 enum dr_mode dr_mode; /* dual role mode */
83 enum periph_id periph_id;/* peripheral id */
Simon Glass46927e12015-01-05 20:05:39 -070084 struct gpio_desc vbus_gpio; /* GPIO for vbus enable */
85 struct gpio_desc phy_reset_gpio; /* GPIO to reset ULPI phy */
Lucas Stach7ae18f32013-02-07 07:16:29 +000086};
87
88static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
89static unsigned port_count; /* Number of available ports */
Jim Lin7e44d932013-06-21 19:05:47 +080090/* Port that needs to clear CSC after Port Reset */
91static u32 port_addr_clear_csc;
Lucas Stach7ae18f32013-02-07 07:16:29 +000092
93/*
94 * This table has USB timing parameters for each Oscillator frequency we
95 * support. There are four sets of values:
96 *
97 * 1. PLLU configuration information (reference clock is osc/clk_m and
98 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
99 *
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
106 * LFCON0 0 0 0 0
107 *
108 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
109 *
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)
116 *
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
120 * BIAS_DEBOUNCE_B).
121 *
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
125 *
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
128 *
129 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
130 * values, so we can keep those to default.
131 *
132 * 4. The 20 microsecond delay after bias cell operation.
133 */
Jim Lin7e44d932013-06-21 19:05:47 +0800134static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
Lucas Stach7ae18f32013-02-07 07:16:29 +0000135 /* 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 }
140};
141
Jim Lin7e44d932013-06-21 19:05:47 +0800142static 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 }
148};
149
150static 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 }
156};
157
Lucas Stach7ae18f32013-02-07 07:16:29 +0000158/* UTMIP Idle Wait Delay */
159static const u8 utmip_idle_wait_delay = 17;
160
161/* UTMIP Elastic limit */
162static const u8 utmip_elastic_limit = 16;
163
164/* UTMIP High Speed Sync Start Delay */
165static const u8 utmip_hs_sync_start_delay = 9;
Simon Glass87f938c2012-02-27 10:52:49 +0000166
Jim Lin7e44d932013-06-21 19:05:47 +0800167struct fdt_usb_controller {
168 int compat;
169 /* flag to determine whether controller supports hostpc register */
170 u32 has_hostpc:1;
171 const unsigned *pll_parameter;
172};
173
Simon Glass27f782b2015-03-25 12:22:20 -0600174static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = {
Jim Lin7e44d932013-06-21 19:05:47 +0800175 {
176 .compat = COMPAT_NVIDIA_TEGRA20_USB,
177 .has_hostpc = 0,
178 .pll_parameter = (const unsigned *)T20_usb_pll,
179 },
180 {
181 .compat = COMPAT_NVIDIA_TEGRA30_USB,
182 .has_hostpc = 1,
183 .pll_parameter = (const unsigned *)T30_usb_pll,
184 },
185 {
186 .compat = COMPAT_NVIDIA_TEGRA114_USB,
187 .has_hostpc = 1,
188 .pll_parameter = (const unsigned *)T114_usb_pll,
189 },
190};
191
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000192/*
193 * A known hardware issue where Connect Status Change bit of PORTSC register
194 * of USB1 controller will be set after Port Reset.
195 * We have to clear it in order for later device enumeration to proceed.
196 * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
197 * in "ehci-hcd.c".
198 */
Simon Glass727fce32015-03-25 12:22:21 -0600199void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
200 uint32_t *reg)
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000201{
Simon Glass56d42732015-03-25 12:22:22 -0600202 struct fdt_usb *config = ctrl->priv;
203 struct fdt_usb_controller *controller;
204
205 controller = &fdt_usb_controllers[config->type];
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000206 mdelay(50);
Jim Lin7e44d932013-06-21 19:05:47 +0800207 /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
208 if (controller->has_hostpc)
209 *reg |= EHCI_PS_PE;
210
Thierry Reding96df9c72015-03-20 12:41:27 +0100211 if (((unsigned long)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000212 return;
213 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
214 if (ehci_readl(status_reg) & EHCI_PS_CSC)
215 *reg |= EHCI_PS_CSC;
216}
Simon Glass87f938c2012-02-27 10:52:49 +0000217
Jim Lin7e44d932013-06-21 19:05:47 +0800218/*
219 * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
220 * in "ehci-hcd.c".
221 */
Simon Glass11d18a12015-03-25 12:22:23 -0600222void ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Lin7e44d932013-06-21 19:05:47 +0800223{
Simon Glass11d18a12015-03-25 12:22:23 -0600224 struct fdt_usb *config = ctrl->priv;
Jim Lin7e44d932013-06-21 19:05:47 +0800225 struct usb_ctlr *usbctlr;
226 uint32_t tmp;
227
Jim Lin7e44d932013-06-21 19:05:47 +0800228 usbctlr = config->reg;
229
230 tmp = ehci_readl(&usbctlr->usb_mode);
231 tmp |= USBMODE_CM_HC;
232 ehci_writel(&usbctlr->usb_mode, tmp);
233}
234
235/*
236 * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
237 * in "ehci-hcd.c".
238 */
Simon Glass73382872015-03-25 12:22:18 -0600239int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Lin7e44d932013-06-21 19:05:47 +0800240{
Simon Glass56d42732015-03-25 12:22:22 -0600241 struct fdt_usb *config = ctrl->priv;
242 struct fdt_usb_controller *controller;
Jim Lin7e44d932013-06-21 19:05:47 +0800243 uint32_t tmp;
244 uint32_t *reg_ptr;
245
Simon Glass56d42732015-03-25 12:22:22 -0600246 controller = &fdt_usb_controllers[config->type];
Jim Lin7e44d932013-06-21 19:05:47 +0800247 if (controller->has_hostpc) {
Simon Glass73382872015-03-25 12:22:18 -0600248 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd +
249 HOSTPC1_DEVLC);
Jim Lin7e44d932013-06-21 19:05:47 +0800250 tmp = ehci_readl(reg_ptr);
251 return HOSTPC1_PSPD(tmp);
252 } else
253 return PORTSC_PSPD(reg);
254}
255
Stephen Warrena4539a22014-04-30 15:09:57 -0600256/* Set up VBUS for host/device mode */
257static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000258{
259 /*
Stephen Warrena4539a22014-04-30 15:09:57 -0600260 * If we are an OTG port initializing in host mode,
261 * check if remote host is driving VBus and bail out in this case.
Lucas Stach7ae18f32013-02-07 07:16:29 +0000262 */
Stephen Warrena4539a22014-04-30 15:09:57 -0600263 if (init == USB_INIT_HOST &&
264 config->dr_mode == DR_MODE_OTG &&
265 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
266 printf("tegrausb: VBUS input active; not enabling as host\n");
Lucas Stach7ae18f32013-02-07 07:16:29 +0000267 return;
Stephen Warrena4539a22014-04-30 15:09:57 -0600268 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000269
Simon Glass46927e12015-01-05 20:05:39 -0700270 if (dm_gpio_is_valid(&config->vbus_gpio)) {
Stephen Warrena4539a22014-04-30 15:09:57 -0600271 int vbus_value;
272
Simon Glass46927e12015-01-05 20:05:39 -0700273 vbus_value = (init == USB_INIT_HOST);
274 dm_gpio_set_value(&config->vbus_gpio, vbus_value);
Stephen Warrena4539a22014-04-30 15:09:57 -0600275
Simon Glass46927e12015-01-05 20:05:39 -0700276 debug("set_up_vbus: GPIO %d %d\n",
277 gpio_get_number(&config->vbus_gpio), vbus_value);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000278 }
279}
280
281void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
282{
283 /* Reset the USB controller with 2us delay */
284 reset_periph(config->periph_id, 2);
285
286 /*
287 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
288 * base address
289 */
290 if (config->has_legacy_mode)
291 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
292
293 /* Put UTMIP1/3 in reset */
294 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
295
296 /* Enable the UTMIP PHY */
297 if (config->utmi)
298 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
299}
300
Simon Glass27f782b2015-03-25 12:22:20 -0600301static const unsigned *get_pll_timing(struct fdt_usb_controller *controller)
Jim Lin7e44d932013-06-21 19:05:47 +0800302{
303 const unsigned *timing;
304
305 timing = controller->pll_parameter +
306 clock_get_osc_freq() * PARAM_COUNT;
307
308 return timing;
309}
310
Stephen Warren2d341512014-04-30 15:09:56 -0600311/* select the PHY to use with a USB controller */
Stephen Warrena4539a22014-04-30 15:09:57 -0600312static void init_phy_mux(struct fdt_usb *config, uint pts,
313 enum usb_init_type init)
Stephen Warren2d341512014-04-30 15:09:56 -0600314{
315 struct usb_ctlr *usbctlr = config->reg;
316
317#if defined(CONFIG_TEGRA20)
318 if (config->periph_id == PERIPH_ID_USBD) {
319 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
Marcel Ziswilerd1fcbae2014-10-04 01:46:10 +0200320 pts << PTS1_SHIFT);
Stephen Warren2d341512014-04-30 15:09:56 -0600321 clrbits_le32(&usbctlr->port_sc1, STS1);
322 } else {
323 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
Marcel Ziswilerd1fcbae2014-10-04 01:46:10 +0200324 pts << PTS_SHIFT);
Stephen Warren2d341512014-04-30 15:09:56 -0600325 clrbits_le32(&usbctlr->port_sc1, STS);
326 }
327#else
Stephen Warrena4539a22014-04-30 15:09:57 -0600328 /* Set to Host mode (if applicable) after Controller Reset was done */
Stephen Warren2d341512014-04-30 15:09:56 -0600329 clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
Stephen Warrena4539a22014-04-30 15:09:57 -0600330 (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
331 /*
332 * Select PHY interface after setting host mode.
333 * For device mode, the ordering requirement is not an issue, since
334 * only the first USB controller supports device mode, and that USB
335 * controller can only talk to a UTMI PHY, so the PHY selection is
336 * already made at reset time, so this write is a no-op.
337 */
Stephen Warren2d341512014-04-30 15:09:56 -0600338 clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
339 pts << PTS_SHIFT);
340 clrbits_le32(&usbctlr->hostpc1_devlc, STS);
341#endif
342}
343
Lucas Stach7ae18f32013-02-07 07:16:29 +0000344/* set up the UTMI USB controller with the parameters provided */
Stephen Warrena4539a22014-04-30 15:09:57 -0600345static int init_utmi_usb_controller(struct fdt_usb *config,
346 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000347{
Simon Glass27f782b2015-03-25 12:22:20 -0600348 struct fdt_usb_controller *controller;
Stephen Warrena4539a22014-04-30 15:09:57 -0600349 u32 b_sess_valid_mask, val;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000350 int loop_count;
351 const unsigned *timing;
352 struct usb_ctlr *usbctlr = config->reg;
Jim Lin7e44d932013-06-21 19:05:47 +0800353 struct clk_rst_ctlr *clkrst;
354 struct usb_ctlr *usb1ctlr;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000355
356 clock_enable(config->periph_id);
357
358 /* Reset the usb controller */
359 usbf_reset_controller(config, usbctlr);
360
361 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
362 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
363
364 /* Follow the crystal clock disable by >100ns delay */
365 udelay(1);
366
Stephen Warrena4539a22014-04-30 15:09:57 -0600367 b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
368 clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
369 (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
370
Lucas Stach7ae18f32013-02-07 07:16:29 +0000371 /*
372 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
373 * mux must be switched to actually use a_sess_vld threshold.
374 */
Jim Lin7e44d932013-06-21 19:05:47 +0800375 if (config->dr_mode == DR_MODE_OTG &&
Simon Glass46927e12015-01-05 20:05:39 -0700376 dm_gpio_is_valid(&config->vbus_gpio))
Lucas Stach7ae18f32013-02-07 07:16:29 +0000377 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
378 VBUS_SENSE_CTL_MASK,
379 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000380
Simon Glass27f782b2015-03-25 12:22:20 -0600381 controller = &fdt_usb_controllers[config->type];
382 debug("controller=%p, type=%d\n", controller, config->type);
383
Lucas Stach7ae18f32013-02-07 07:16:29 +0000384 /*
385 * PLL Delay CONFIGURATION settings. The following parameters control
386 * the bring up of the plls.
387 */
Simon Glass27f782b2015-03-25 12:22:20 -0600388 timing = get_pll_timing(controller);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000389
Jim Lin7e44d932013-06-21 19:05:47 +0800390 if (!controller->has_hostpc) {
391 val = readl(&usbctlr->utmip_misc_cfg1);
392 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
393 timing[PARAM_STABLE_COUNT] <<
394 UTMIP_PLLU_STABLE_COUNT_SHIFT);
395 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
396 timing[PARAM_ACTIVE_DELAY_COUNT] <<
397 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
398 writel(val, &usbctlr->utmip_misc_cfg1);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000399
Jim Lin7e44d932013-06-21 19:05:47 +0800400 /* Set PLL enable delay count and crystal frequency count */
401 val = readl(&usbctlr->utmip_pll_cfg1);
402 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
403 timing[PARAM_ENABLE_DELAY_COUNT] <<
404 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
405 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
406 timing[PARAM_XTAL_FREQ_COUNT] <<
407 UTMIP_XTAL_FREQ_COUNT_SHIFT);
408 writel(val, &usbctlr->utmip_pll_cfg1);
409 } else {
410 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
411
412 val = readl(&clkrst->crc_utmip_pll_cfg2);
413 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
414 timing[PARAM_STABLE_COUNT] <<
415 UTMIP_PLLU_STABLE_COUNT_SHIFT);
416 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
417 timing[PARAM_ACTIVE_DELAY_COUNT] <<
418 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
419 writel(val, &clkrst->crc_utmip_pll_cfg2);
420
421 /* Set PLL enable delay count and crystal frequency count */
422 val = readl(&clkrst->crc_utmip_pll_cfg1);
423 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
424 timing[PARAM_ENABLE_DELAY_COUNT] <<
425 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
426 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
427 timing[PARAM_XTAL_FREQ_COUNT] <<
428 UTMIP_XTAL_FREQ_COUNT_SHIFT);
429 writel(val, &clkrst->crc_utmip_pll_cfg1);
430
431 /* Disable Power Down state for PLL */
432 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
433 PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
434 PLL_ACTIVE_POWERDOWN);
435
436 /* Recommended PHY settings for EYE diagram */
437 val = readl(&usbctlr->utmip_xcvr_cfg0);
438 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
439 0x4 << UTMIP_XCVR_SETUP_SHIFT);
440 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
441 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
442 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
443 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
444 writel(val, &usbctlr->utmip_xcvr_cfg0);
445 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
446 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
447 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
448
449 /* Some registers can be controlled from USB1 only. */
450 if (config->periph_id != PERIPH_ID_USBD) {
451 clock_enable(PERIPH_ID_USBD);
452 /* Disable Reset if in Reset state */
453 reset_set_enable(PERIPH_ID_USBD, 0);
454 }
455 usb1ctlr = (struct usb_ctlr *)
Thierry Reding96df9c72015-03-20 12:41:27 +0100456 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin7e44d932013-06-21 19:05:47 +0800457 val = readl(&usb1ctlr->utmip_bias_cfg0);
458 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
459 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
460 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
461 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
462 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
463 writel(val, &usb1ctlr->utmip_bias_cfg0);
464
465 /* Miscellaneous setting mentioned in Programming Guide */
466 clrbits_le32(&usbctlr->utmip_misc_cfg0,
467 UTMIP_SUSPEND_EXIT_ON_EDGE);
468 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000469
470 /* Setting the tracking length time */
471 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
472 UTMIP_BIAS_PDTRK_COUNT_MASK,
473 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
474
475 /* Program debounce time for VBUS to become valid */
476 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
477 UTMIP_DEBOUNCE_CFG0_MASK,
478 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
479
480 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
481
482 /* Disable battery charge enabling bit */
483 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
484
485 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
486 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
487
488 /*
489 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
490 * Setting these fields, together with default values of the
491 * other fields, results in programming the registers below as
492 * follows:
493 * UTMIP_HSRX_CFG0 = 0x9168c000
494 * UTMIP_HSRX_CFG1 = 0x13
495 */
496
497 /* Set PLL enable delay count and Crystal frequency count */
498 val = readl(&usbctlr->utmip_hsrx_cfg0);
499 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
500 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
501 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
502 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
503 writel(val, &usbctlr->utmip_hsrx_cfg0);
504
505 /* Configure the UTMIP_HS_SYNC_START_DLY */
506 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
507 UTMIP_HS_SYNC_START_DLY_MASK,
508 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
509
510 /* Preceed the crystal clock disable by >100ns delay. */
511 udelay(1);
512
513 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
514 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
515
Jim Lin7e44d932013-06-21 19:05:47 +0800516 if (controller->has_hostpc) {
517 if (config->periph_id == PERIPH_ID_USBD)
518 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
519 UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
Stefan Agnerb03f4b32014-03-02 19:46:48 +0100520 if (config->periph_id == PERIPH_ID_USB2)
521 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
522 UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
Jim Lin7e44d932013-06-21 19:05:47 +0800523 if (config->periph_id == PERIPH_ID_USB3)
524 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
525 UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
526 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000527 /* Finished the per-controller init. */
528
529 /* De-assert UTMIP_RESET to bring out of reset. */
530 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
531
532 /* Wait for the phy clock to become valid in 100 ms */
533 for (loop_count = 100000; loop_count != 0; loop_count--) {
534 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
535 break;
536 udelay(1);
537 }
538 if (!loop_count)
539 return -1;
540
541 /* Disable ICUSB FS/LS transceiver */
542 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
543
544 /* Select UTMI parallel interface */
Stephen Warrena4539a22014-04-30 15:09:57 -0600545 init_phy_mux(config, PTS_UTMI, init);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000546
547 /* Deassert power down state */
548 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
549 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
550 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
551 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
552
Jim Lin7e44d932013-06-21 19:05:47 +0800553 if (controller->has_hostpc) {
554 /*
555 * BIAS Pad Power Down is common among all 3 USB
556 * controllers and can be controlled from USB1 only.
557 */
558 usb1ctlr = (struct usb_ctlr *)
Thierry Reding96df9c72015-03-20 12:41:27 +0100559 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin7e44d932013-06-21 19:05:47 +0800560 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
561 udelay(25);
562 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
563 UTMIP_FORCE_PDTRK_POWERDOWN);
564 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000565 return 0;
566}
567
568#ifdef CONFIG_USB_ULPI
569/* if board file does not set a ULPI reference frequency we default to 24MHz */
570#ifndef CONFIG_ULPI_REF_CLK
571#define CONFIG_ULPI_REF_CLK 24000000
572#endif
573
574/* set up the ULPI USB controller with the parameters provided */
Stephen Warrena4539a22014-04-30 15:09:57 -0600575static int init_ulpi_usb_controller(struct fdt_usb *config,
576 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000577{
578 u32 val;
579 int loop_count;
580 struct ulpi_viewport ulpi_vp;
581 struct usb_ctlr *usbctlr = config->reg;
582
583 /* set up ULPI reference clock on pllp_out4 */
584 clock_enable(PERIPH_ID_DEV2_OUT);
585 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
586
587 /* reset ULPI phy */
Simon Glass46927e12015-01-05 20:05:39 -0700588 if (dm_gpio_is_valid(&config->phy_reset_gpio)) {
589 dm_gpio_set_value(&config->phy_reset_gpio, 0);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000590 mdelay(5);
Simon Glass46927e12015-01-05 20:05:39 -0700591 dm_gpio_set_value(&config->phy_reset_gpio, 1);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000592 }
593
594 /* Reset the usb controller */
595 clock_enable(config->periph_id);
596 usbf_reset_controller(config, usbctlr);
597
598 /* enable pinmux bypass */
599 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
600 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
601
602 /* Select ULPI parallel interface */
Stephen Warrena4539a22014-04-30 15:09:57 -0600603 init_phy_mux(config, PTS_ULPI, init);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000604
605 /* enable ULPI transceiver */
606 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
607
608 /* configure ULPI transceiver timings */
609 val = 0;
610 writel(val, &usbctlr->ulpi_timing_ctrl_1);
611
612 val |= ULPI_DATA_TRIMMER_SEL(4);
613 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
614 val |= ULPI_DIR_TRIMMER_SEL(4);
615 writel(val, &usbctlr->ulpi_timing_ctrl_1);
616 udelay(10);
617
618 val |= ULPI_DATA_TRIMMER_LOAD;
619 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
620 val |= ULPI_DIR_TRIMMER_LOAD;
621 writel(val, &usbctlr->ulpi_timing_ctrl_1);
622
623 /* set up phy for host operation with external vbus supply */
624 ulpi_vp.port_num = 0;
625 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
626
627 if (ulpi_init(&ulpi_vp)) {
628 printf("Tegra ULPI viewport init failed\n");
629 return -1;
630 }
631
632 ulpi_set_vbus(&ulpi_vp, 1, 1);
633 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
634
635 /* enable wakeup events */
636 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
637
638 /* Enable and wait for the phy clock to become valid in 100 ms */
639 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
640 for (loop_count = 100000; loop_count != 0; loop_count--) {
641 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
642 break;
643 udelay(1);
644 }
645 if (!loop_count)
646 return -1;
647 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
648
649 return 0;
650}
651#else
Stephen Warrena4539a22014-04-30 15:09:57 -0600652static int init_ulpi_usb_controller(struct fdt_usb *config,
653 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000654{
655 printf("No code to set up ULPI controller, please enable"
656 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
657 return -1;
658}
659#endif
660
661static void config_clock(const u32 timing[])
662{
663 clock_start_pll(CLOCK_ID_USB,
664 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
665 timing[PARAM_CPCON], timing[PARAM_LFCON]);
666}
667
Jim Lin7e44d932013-06-21 19:05:47 +0800668static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000669{
670 const char *phy, *mode;
671
672 config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
673 mode = fdt_getprop(blob, node, "dr_mode", NULL);
674 if (mode) {
675 if (0 == strcmp(mode, "host"))
676 config->dr_mode = DR_MODE_HOST;
677 else if (0 == strcmp(mode, "peripheral"))
678 config->dr_mode = DR_MODE_DEVICE;
679 else if (0 == strcmp(mode, "otg"))
680 config->dr_mode = DR_MODE_OTG;
681 else {
682 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
683 mode);
684 return -FDT_ERR_NOTFOUND;
685 }
686 } else {
687 config->dr_mode = DR_MODE_HOST;
688 }
689
690 phy = fdt_getprop(blob, node, "phy_type", NULL);
691 config->utmi = phy && 0 == strcmp("utmi", phy);
692 config->ulpi = phy && 0 == strcmp("ulpi", phy);
693 config->enabled = fdtdec_get_is_enabled(blob, node);
694 config->has_legacy_mode = fdtdec_get_bool(blob, node,
695 "nvidia,has-legacy-mode");
Jim Lin7e44d932013-06-21 19:05:47 +0800696 if (config->has_legacy_mode)
Thierry Reding96df9c72015-03-20 12:41:27 +0100697 port_addr_clear_csc = (unsigned long)config->reg;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000698 config->periph_id = clock_decode_periph_id(blob, node);
699 if (config->periph_id == PERIPH_ID_NONE) {
700 debug("%s: Missing/invalid peripheral ID\n", __func__);
701 return -FDT_ERR_NOTFOUND;
702 }
Simon Glass46927e12015-01-05 20:05:39 -0700703 gpio_request_by_name_nodev(blob, node, "nvidia,vbus-gpio", 0,
704 &config->vbus_gpio, GPIOD_IS_OUT);
705 gpio_request_by_name_nodev(blob, node, "nvidia,phy-reset-gpio", 0,
706 &config->phy_reset_gpio, GPIOD_IS_OUT);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000707 debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
708 "vbus=%d, phy_reset=%d, dr_mode=%d\n",
709 config->enabled, config->has_legacy_mode, config->utmi,
Simon Glass46927e12015-01-05 20:05:39 -0700710 config->ulpi, config->periph_id,
711 gpio_get_number(&config->vbus_gpio),
712 gpio_get_number(&config->phy_reset_gpio), config->dr_mode);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000713
714 return 0;
715}
716
Jim Lin7e44d932013-06-21 19:05:47 +0800717/*
718 * process_usb_nodes() - Process a list of USB nodes, adding them to our list
719 * of USB ports.
720 * @blob: fdt blob
721 * @node_list: list of nodes to process (any <=0 are ignored)
722 * @count: number of nodes to process
Simon Glass27f782b2015-03-25 12:22:20 -0600723 * @id: controller type (enum usb_ctlr_type)
Jim Lin7e44d932013-06-21 19:05:47 +0800724 *
725 * Return: 0 - ok, -1 - error
726 */
Simon Glass27f782b2015-03-25 12:22:20 -0600727static int process_usb_nodes(const void *blob, int node_list[], int count,
728 enum usb_ctlr_type id)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000729{
730 struct fdt_usb config;
Jim Lin7e44d932013-06-21 19:05:47 +0800731 int node, i;
732 int clk_done = 0;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000733
Jim Lin7e44d932013-06-21 19:05:47 +0800734 port_count = 0;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000735 for (i = 0; i < count; i++) {
736 if (port_count == USB_PORTS_MAX) {
737 printf("tegrausb: Cannot register more than %d ports\n",
738 USB_PORTS_MAX);
739 return -1;
740 }
741
742 debug("USB %d: ", i);
743 node = node_list[i];
744 if (!node)
745 continue;
746 if (fdt_decode_usb(blob, node, &config)) {
747 debug("Cannot decode USB node %s\n",
748 fdt_get_name(blob, node, NULL));
749 return -1;
750 }
Jim Lin7e44d932013-06-21 19:05:47 +0800751 if (!clk_done) {
Simon Glass27f782b2015-03-25 12:22:20 -0600752 config_clock(get_pll_timing(
753 &fdt_usb_controllers[id]));
Jim Lin7e44d932013-06-21 19:05:47 +0800754 clk_done = 1;
755 }
Simon Glass27f782b2015-03-25 12:22:20 -0600756 config.type = id;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000757 config.initialized = 0;
758
759 /* add new USB port to the list of available ports */
760 port[port_count++] = config;
761 }
762
763 return 0;
764}
765
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200766int usb_process_devicetree(const void *blob)
Jim Lin7e44d932013-06-21 19:05:47 +0800767{
768 int node_list[USB_PORTS_MAX];
769 int count, err = 0;
770 int i;
771
772 for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
Jim Lin7e44d932013-06-21 19:05:47 +0800773 count = fdtdec_find_aliases_for_id(blob, "usb",
Simon Glass56d42732015-03-25 12:22:22 -0600774 fdt_usb_controllers[i].compat, node_list,
775 USB_PORTS_MAX);
Jim Lin7e44d932013-06-21 19:05:47 +0800776 if (count) {
Simon Glass27f782b2015-03-25 12:22:20 -0600777 err = process_usb_nodes(blob, node_list, count, i);
Jim Lin7e44d932013-06-21 19:05:47 +0800778 if (err)
779 printf("%s: Error processing USB node!\n",
780 __func__);
781 return err;
782 }
783 }
Jim Lin7e44d932013-06-21 19:05:47 +0800784
785 return err;
786}
787
Lucas Stachd7a55e12013-02-07 07:16:30 +0000788/**
789 * Start up the given port number (ports are numbered from 0 on each board).
790 * This returns values for the appropriate hccr and hcor addresses to use for
791 * USB EHCI operations.
792 *
793 * @param index port number to start
794 * @param hccr returns start address of EHCI HCCR registers
795 * @param hcor returns start address of EHCI HCOR registers
796 * @return 0 if ok, -1 on error (generally invalid port number)
Simon Glass87f938c2012-02-27 10:52:49 +0000797 */
Troy Kisky127efc42013-10-10 15:27:57 -0700798int ehci_hcd_init(int index, enum usb_init_type init,
799 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Simon Glass87f938c2012-02-27 10:52:49 +0000800{
Lucas Stachd7a55e12013-02-07 07:16:30 +0000801 struct fdt_usb *config;
802 struct usb_ctlr *usbctlr;
Simon Glass87f938c2012-02-27 10:52:49 +0000803
Lucas Stachd7a55e12013-02-07 07:16:30 +0000804 if (index >= port_count)
Simon Glass87f938c2012-02-27 10:52:49 +0000805 return -1;
806
Lucas Stachd7a55e12013-02-07 07:16:30 +0000807 config = &port[index];
Simon Glass27f782b2015-03-25 12:22:20 -0600808 ehci_set_controller_priv(index, config);
Simon Glass87f938c2012-02-27 10:52:49 +0000809
Stephen Warrena4539a22014-04-30 15:09:57 -0600810 switch (init) {
811 case USB_INIT_HOST:
812 switch (config->dr_mode) {
813 case DR_MODE_HOST:
814 case DR_MODE_OTG:
815 break;
816 default:
817 printf("tegrausb: Invalid dr_mode %d for host mode\n",
818 config->dr_mode);
819 return -1;
820 }
821 break;
822 case USB_INIT_DEVICE:
823 if (config->periph_id != PERIPH_ID_USBD) {
824 printf("tegrausb: Device mode only supported on first USB controller\n");
825 return -1;
826 }
827 if (!config->utmi) {
828 printf("tegrausb: Device mode only supported with UTMI PHY\n");
829 return -1;
830 }
831 switch (config->dr_mode) {
832 case DR_MODE_DEVICE:
833 case DR_MODE_OTG:
834 break;
835 default:
836 printf("tegrausb: Invalid dr_mode %d for device mode\n",
837 config->dr_mode);
838 return -1;
839 }
840 break;
841 default:
842 printf("tegrausb: Unknown USB_INIT_* %d\n", init);
843 return -1;
844 }
845
Lucas Stachd7a55e12013-02-07 07:16:30 +0000846 /* skip init, if the port is already initialized */
Stephen Warrena4539a22014-04-30 15:09:57 -0600847 if (config->initialized && config->init_type == init)
Lucas Stachd7a55e12013-02-07 07:16:30 +0000848 goto success;
849
Stephen Warrena4539a22014-04-30 15:09:57 -0600850 if (config->utmi && init_utmi_usb_controller(config, init)) {
Lucas Stachd7a55e12013-02-07 07:16:30 +0000851 printf("tegrausb: Cannot init port %d\n", index);
852 return -1;
853 }
854
Stephen Warrena4539a22014-04-30 15:09:57 -0600855 if (config->ulpi && init_ulpi_usb_controller(config, init)) {
Lucas Stachd7a55e12013-02-07 07:16:30 +0000856 printf("tegrausb: Cannot init port %d\n", index);
857 return -1;
858 }
859
Stephen Warrena4539a22014-04-30 15:09:57 -0600860 set_up_vbus(config, init);
Lucas Stachd7a55e12013-02-07 07:16:30 +0000861
862 config->initialized = 1;
Stephen Warrena4539a22014-04-30 15:09:57 -0600863 config->init_type = init;
Lucas Stachd7a55e12013-02-07 07:16:30 +0000864
865success:
866 usbctlr = config->reg;
867 *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
868 *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
Jim Lin7e44d932013-06-21 19:05:47 +0800869
Simon Glass87f938c2012-02-27 10:52:49 +0000870 return 0;
871}
872
873/*
Lucas Stachd7a55e12013-02-07 07:16:30 +0000874 * Bring down the specified USB controller
Simon Glass87f938c2012-02-27 10:52:49 +0000875 */
Lucas Stach676ae062012-09-26 00:14:35 +0200876int ehci_hcd_stop(int index)
Simon Glass87f938c2012-02-27 10:52:49 +0000877{
Lucas Stachd7a55e12013-02-07 07:16:30 +0000878 struct usb_ctlr *usbctlr;
879
880 usbctlr = port[index].reg;
881
882 /* Stop controller */
883 writel(0, &usbctlr->usb_cmd);
884 udelay(1000);
885
886 /* Initiate controller reset */
887 writel(2, &usbctlr->usb_cmd);
888 udelay(1000);
889
890 port[index].initialized = 0;
891
892 return 0;
Simon Glass87f938c2012-02-27 10:52:49 +0000893}