blob: 343893b9f19acba389263fade3ccc11a89a03adb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass08d6ec22012-02-27 10:52:49 +00002/*
Lucas Stach26c32162013-02-07 07:16:29 +00003 * Copyright (c) 2011 The Chromium OS Authors.
Tom Warrenab0cc6b2015-03-04 16:36:00 -07004 * Copyright (c) 2009-2015 NVIDIA Corporation
Lucas Stach26c32162013-02-07 07:16:29 +00005 * Copyright (c) 2013 Lucas Stach
Simon Glass08d6ec22012-02-27 10:52:49 +00006 */
7
Simon Glass9c574772015-03-25 12:22:48 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Lucas Stach26c32162013-02-07 07:16:29 +000012#include <asm/io.h>
13#include <asm-generic/gpio.h>
14#include <asm/arch/clock.h>
15#include <asm/arch-tegra/usb.h>
Jim Lin2fefb8b2013-06-21 19:05:47 +080016#include <asm/arch-tegra/clk_rst.h>
Simon Glass08d6ec22012-02-27 10:52:49 +000017#include <usb.h>
Lucas Stach26c32162013-02-07 07:16:29 +000018#include <usb/ulpi.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Simon Glass08d6ec22012-02-27 10:52:49 +000020
21#include "ehci.h"
Simon Glass08d6ec22012-02-27 10:52:49 +000022
Jim Lin2fefb8b2013-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 Stach26c32162013-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
Lucas Stach26c32162013-02-07 07:16:29 +000035/* Parameters we need for USB */
36enum {
37 PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
38 PARAM_DIVM, /* PLL INPUT DIVIDER */
39 PARAM_DIVP, /* POST DIVIDER (2^N) */
40 PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
41 PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
42 PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
43 PARAM_STABLE_COUNT, /* PLL-U STABLE count */
44 PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
45 PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
46 PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
47 PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
48
49 PARAM_COUNT
50};
51
52/* Possible port types (dual role mode) */
53enum dr_mode {
54 DR_MODE_NONE = 0,
55 DR_MODE_HOST, /* supports host operation */
56 DR_MODE_DEVICE, /* supports device operation */
57 DR_MODE_OTG, /* supports both */
58};
59
Simon Glass92a419e2015-03-25 12:22:20 -060060enum usb_ctlr_type {
61 USB_CTLR_T20,
62 USB_CTLR_T30,
63 USB_CTLR_T114,
Tom Warrenab0cc6b2015-03-04 16:36:00 -070064 USB_CTLR_T210,
Simon Glass92a419e2015-03-25 12:22:20 -060065
66 USB_CTRL_COUNT,
67};
68
Lucas Stach26c32162013-02-07 07:16:29 +000069/* Information about a USB port */
70struct fdt_usb {
Simon Glass9c574772015-03-25 12:22:48 -060071 struct ehci_ctrl ehci;
Lucas Stach26c32162013-02-07 07:16:29 +000072 struct usb_ctlr *reg; /* address of registers in physical memory */
73 unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
74 unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
75 unsigned enabled:1; /* 1 to enable, 0 to disable */
76 unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
Simon Glass92a419e2015-03-25 12:22:20 -060077 enum usb_ctlr_type type;
Stephen Warren8b263f92014-04-30 15:09:57 -060078 enum usb_init_type init_type;
Lucas Stach26c32162013-02-07 07:16:29 +000079 enum dr_mode dr_mode; /* dual role mode */
80 enum periph_id periph_id;/* peripheral id */
Simon Glassfec09c52015-01-05 20:05:39 -070081 struct gpio_desc vbus_gpio; /* GPIO for vbus enable */
82 struct gpio_desc phy_reset_gpio; /* GPIO to reset ULPI phy */
Lucas Stach26c32162013-02-07 07:16:29 +000083};
84
Lucas Stach26c32162013-02-07 07:16:29 +000085/*
86 * This table has USB timing parameters for each Oscillator frequency we
87 * support. There are four sets of values:
88 *
89 * 1. PLLU configuration information (reference clock is osc/clk_m and
90 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
91 *
92 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
93 * ----------------------------------------------------------------------
94 * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
95 * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
96 * Filter frequency (MHz) 1 4.8 6 2
97 * CPCON 1100b 0011b 1100b 1100b
98 * LFCON0 0 0 0 0
99 *
100 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
101 *
102 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
103 * ---------------------------------------------------------------------------
104 * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
105 * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
106 * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
107 * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
108 *
109 * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
110 * SessEnd. Each of these signals have their own debouncer and for each of
111 * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
112 * BIAS_DEBOUNCE_B).
113 *
114 * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
115 * 0xffff -> No debouncing at all
116 * <n> ms = <n> *1000 / (1/19.2MHz) / 4
117 *
118 * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
119 * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
120 *
121 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
122 * values, so we can keep those to default.
123 *
124 * 4. The 20 microsecond delay after bias cell operation.
125 */
Jim Lin2fefb8b2013-06-21 19:05:47 +0800126static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
Lucas Stach26c32162013-02-07 07:16:29 +0000127 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
128 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200129 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
130 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
131 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Lucas Stach26c32162013-02-07 07:16:29 +0000132 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200133 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
134 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
135 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
136 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
Lucas Stach26c32162013-02-07 07:16:29 +0000137 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
Tom Warren27bce712015-06-22 13:03:44 -0700138 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200139 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
140 { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
Lucas Stach26c32162013-02-07 07:16:29 +0000141};
142
Jim Lin2fefb8b2013-06-21 19:05:47 +0800143static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
144 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
145 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200146 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
147 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
148 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Jim Lin2fefb8b2013-06-21 19:05:47 +0800149 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200150 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
151 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
152 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Jim Lin2fefb8b2013-06-21 19:05:47 +0800153 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200154 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
155 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Tom Warren27bce712015-06-22 13:03:44 -0700156 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200157 { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
Jim Lin2fefb8b2013-06-21 19:05:47 +0800158};
159
160static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
161 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
162 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200163 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
164 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
165 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
166 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
Jim Lin2fefb8b2013-06-21 19:05:47 +0800167 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200168 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
169 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
170 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
Jim Lin2fefb8b2013-06-21 19:05:47 +0800171 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200172 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Tom Warren27bce712015-06-22 13:03:44 -0700173 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200174 { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 11 }
Jim Lin2fefb8b2013-06-21 19:05:47 +0800175};
176
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700177/* NOTE: 13/26MHz settings are N/A for T210, so dupe 12MHz settings for now */
178static const unsigned T210_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
179 /* DivN, DivM, DivP, KCP, KVCO, Delays Debounce, Bias */
Tom Warren27bce712015-06-22 13:03:44 -0700180 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 32500, 5 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200181 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 32500, 5 },
182 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
183 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700184 { 0x019, 0x01, 0x01, 0x0, 0, 0x03, 0x4B, 0x0C, 0xBB, 48000, 8 },
Tom Warren27bce712015-06-22 13:03:44 -0700185 { 0x019, 0x02, 0x01, 0x0, 0, 0x05, 0x96, 0x18, 0x177, 96000, 15 },
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200186 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
187 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
188 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 30000, 5 },
189 { 0x028, 0x04, 0x01, 0x0, 0, 0x04, 0x66, 0x09, 0xFE, 120000, 20 },
190 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
191 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
192 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 65000, 5 }
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700193};
194
Lucas Stach26c32162013-02-07 07:16:29 +0000195/* UTMIP Idle Wait Delay */
196static const u8 utmip_idle_wait_delay = 17;
197
198/* UTMIP Elastic limit */
199static const u8 utmip_elastic_limit = 16;
200
201/* UTMIP High Speed Sync Start Delay */
202static const u8 utmip_hs_sync_start_delay = 9;
Simon Glass08d6ec22012-02-27 10:52:49 +0000203
Jim Lin2fefb8b2013-06-21 19:05:47 +0800204struct fdt_usb_controller {
Jim Lin2fefb8b2013-06-21 19:05:47 +0800205 /* flag to determine whether controller supports hostpc register */
206 u32 has_hostpc:1;
207 const unsigned *pll_parameter;
208};
209
Simon Glass92a419e2015-03-25 12:22:20 -0600210static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = {
Jim Lin2fefb8b2013-06-21 19:05:47 +0800211 {
Jim Lin2fefb8b2013-06-21 19:05:47 +0800212 .has_hostpc = 0,
213 .pll_parameter = (const unsigned *)T20_usb_pll,
214 },
215 {
Jim Lin2fefb8b2013-06-21 19:05:47 +0800216 .has_hostpc = 1,
217 .pll_parameter = (const unsigned *)T30_usb_pll,
218 },
219 {
Jim Lin2fefb8b2013-06-21 19:05:47 +0800220 .has_hostpc = 1,
221 .pll_parameter = (const unsigned *)T114_usb_pll,
222 },
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700223 {
224 .has_hostpc = 1,
225 .pll_parameter = (const unsigned *)T210_usb_pll,
226 },
Jim Lin2fefb8b2013-06-21 19:05:47 +0800227};
228
Jim Lin5a057e32012-06-24 20:40:57 +0000229/*
230 * A known hardware issue where Connect Status Change bit of PORTSC register
231 * of USB1 controller will be set after Port Reset.
232 * We have to clear it in order for later device enumeration to proceed.
Jim Lin5a057e32012-06-24 20:40:57 +0000233 */
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600234static void tegra_ehci_powerup_fixup(struct ehci_ctrl *ctrl,
235 uint32_t *status_reg, uint32_t *reg)
Jim Lin5a057e32012-06-24 20:40:57 +0000236{
Simon Glass66a79f32015-03-25 12:22:22 -0600237 struct fdt_usb *config = ctrl->priv;
238 struct fdt_usb_controller *controller;
239
240 controller = &fdt_usb_controllers[config->type];
Jim Lin5a057e32012-06-24 20:40:57 +0000241 mdelay(50);
Jim Lin2fefb8b2013-06-21 19:05:47 +0800242 /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
243 if (controller->has_hostpc)
244 *reg |= EHCI_PS_PE;
245
Simon Glass57c76c32015-03-25 12:22:45 -0600246 if (!config->has_legacy_mode)
Jim Lin5a057e32012-06-24 20:40:57 +0000247 return;
248 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
249 if (ehci_readl(status_reg) & EHCI_PS_CSC)
250 *reg |= EHCI_PS_CSC;
251}
Simon Glass08d6ec22012-02-27 10:52:49 +0000252
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600253static void tegra_ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Lin2fefb8b2013-06-21 19:05:47 +0800254{
Simon Glass2d387ab2015-03-25 12:22:23 -0600255 struct fdt_usb *config = ctrl->priv;
Jim Lin2fefb8b2013-06-21 19:05:47 +0800256 struct usb_ctlr *usbctlr;
257 uint32_t tmp;
258
Jim Lin2fefb8b2013-06-21 19:05:47 +0800259 usbctlr = config->reg;
260
261 tmp = ehci_readl(&usbctlr->usb_mode);
262 tmp |= USBMODE_CM_HC;
263 ehci_writel(&usbctlr->usb_mode, tmp);
264}
265
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600266static int tegra_ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Lin2fefb8b2013-06-21 19:05:47 +0800267{
Simon Glass66a79f32015-03-25 12:22:22 -0600268 struct fdt_usb *config = ctrl->priv;
269 struct fdt_usb_controller *controller;
Jim Lin2fefb8b2013-06-21 19:05:47 +0800270 uint32_t tmp;
271 uint32_t *reg_ptr;
272
Simon Glass66a79f32015-03-25 12:22:22 -0600273 controller = &fdt_usb_controllers[config->type];
Jim Lin2fefb8b2013-06-21 19:05:47 +0800274 if (controller->has_hostpc) {
Simon Glassc78c7652015-03-25 12:22:18 -0600275 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd +
276 HOSTPC1_DEVLC);
Jim Lin2fefb8b2013-06-21 19:05:47 +0800277 tmp = ehci_readl(reg_ptr);
278 return HOSTPC1_PSPD(tmp);
279 } else
280 return PORTSC_PSPD(reg);
281}
282
Stephen Warren8b263f92014-04-30 15:09:57 -0600283/* Set up VBUS for host/device mode */
284static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
Lucas Stach26c32162013-02-07 07:16:29 +0000285{
286 /*
Stephen Warren8b263f92014-04-30 15:09:57 -0600287 * If we are an OTG port initializing in host mode,
288 * check if remote host is driving VBus and bail out in this case.
Lucas Stach26c32162013-02-07 07:16:29 +0000289 */
Stephen Warren8b263f92014-04-30 15:09:57 -0600290 if (init == USB_INIT_HOST &&
291 config->dr_mode == DR_MODE_OTG &&
292 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
293 printf("tegrausb: VBUS input active; not enabling as host\n");
Lucas Stach26c32162013-02-07 07:16:29 +0000294 return;
Stephen Warren8b263f92014-04-30 15:09:57 -0600295 }
Lucas Stach26c32162013-02-07 07:16:29 +0000296
Simon Glassfec09c52015-01-05 20:05:39 -0700297 if (dm_gpio_is_valid(&config->vbus_gpio)) {
Stephen Warren8b263f92014-04-30 15:09:57 -0600298 int vbus_value;
299
Simon Glassfec09c52015-01-05 20:05:39 -0700300 vbus_value = (init == USB_INIT_HOST);
301 dm_gpio_set_value(&config->vbus_gpio, vbus_value);
Stephen Warren8b263f92014-04-30 15:09:57 -0600302
Simon Glassfec09c52015-01-05 20:05:39 -0700303 debug("set_up_vbus: GPIO %d %d\n",
304 gpio_get_number(&config->vbus_gpio), vbus_value);
Lucas Stach26c32162013-02-07 07:16:29 +0000305 }
306}
307
Simon Glass6f7d3422015-03-25 12:22:46 -0600308static void usbf_reset_controller(struct fdt_usb *config,
309 struct usb_ctlr *usbctlr)
Lucas Stach26c32162013-02-07 07:16:29 +0000310{
311 /* Reset the USB controller with 2us delay */
312 reset_periph(config->periph_id, 2);
313
314 /*
315 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
316 * base address
317 */
318 if (config->has_legacy_mode)
319 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
320
321 /* Put UTMIP1/3 in reset */
322 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
323
324 /* Enable the UTMIP PHY */
325 if (config->utmi)
326 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
327}
328
Simon Glass92a419e2015-03-25 12:22:20 -0600329static const unsigned *get_pll_timing(struct fdt_usb_controller *controller)
Jim Lin2fefb8b2013-06-21 19:05:47 +0800330{
331 const unsigned *timing;
332
333 timing = controller->pll_parameter +
334 clock_get_osc_freq() * PARAM_COUNT;
335
336 return timing;
337}
338
Stephen Warren600246c2014-04-30 15:09:56 -0600339/* select the PHY to use with a USB controller */
Stephen Warren8b263f92014-04-30 15:09:57 -0600340static void init_phy_mux(struct fdt_usb *config, uint pts,
341 enum usb_init_type init)
Stephen Warren600246c2014-04-30 15:09:56 -0600342{
343 struct usb_ctlr *usbctlr = config->reg;
344
345#if defined(CONFIG_TEGRA20)
346 if (config->periph_id == PERIPH_ID_USBD) {
347 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
Marcel Ziswilerafc106e2014-10-04 01:46:10 +0200348 pts << PTS1_SHIFT);
Stephen Warren600246c2014-04-30 15:09:56 -0600349 clrbits_le32(&usbctlr->port_sc1, STS1);
350 } else {
351 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
Marcel Ziswilerafc106e2014-10-04 01:46:10 +0200352 pts << PTS_SHIFT);
Stephen Warren600246c2014-04-30 15:09:56 -0600353 clrbits_le32(&usbctlr->port_sc1, STS);
354 }
355#else
Stephen Warren8b263f92014-04-30 15:09:57 -0600356 /* Set to Host mode (if applicable) after Controller Reset was done */
Stephen Warren600246c2014-04-30 15:09:56 -0600357 clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
Stephen Warren8b263f92014-04-30 15:09:57 -0600358 (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
359 /*
360 * Select PHY interface after setting host mode.
361 * For device mode, the ordering requirement is not an issue, since
362 * only the first USB controller supports device mode, and that USB
363 * controller can only talk to a UTMI PHY, so the PHY selection is
364 * already made at reset time, so this write is a no-op.
365 */
Stephen Warren600246c2014-04-30 15:09:56 -0600366 clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
367 pts << PTS_SHIFT);
368 clrbits_le32(&usbctlr->hostpc1_devlc, STS);
369#endif
370}
371
Lucas Stach26c32162013-02-07 07:16:29 +0000372/* set up the UTMI USB controller with the parameters provided */
Stephen Warren8b263f92014-04-30 15:09:57 -0600373static int init_utmi_usb_controller(struct fdt_usb *config,
374 enum usb_init_type init)
Lucas Stach26c32162013-02-07 07:16:29 +0000375{
Simon Glass92a419e2015-03-25 12:22:20 -0600376 struct fdt_usb_controller *controller;
Stephen Warren8b263f92014-04-30 15:09:57 -0600377 u32 b_sess_valid_mask, val;
Lucas Stach26c32162013-02-07 07:16:29 +0000378 int loop_count;
379 const unsigned *timing;
380 struct usb_ctlr *usbctlr = config->reg;
Jim Lin2fefb8b2013-06-21 19:05:47 +0800381 struct clk_rst_ctlr *clkrst;
382 struct usb_ctlr *usb1ctlr;
Lucas Stach26c32162013-02-07 07:16:29 +0000383
384 clock_enable(config->periph_id);
385
386 /* Reset the usb controller */
387 usbf_reset_controller(config, usbctlr);
388
389 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
390 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
391
392 /* Follow the crystal clock disable by >100ns delay */
393 udelay(1);
394
Stephen Warren8b263f92014-04-30 15:09:57 -0600395 b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
396 clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
397 (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
398
Lucas Stach26c32162013-02-07 07:16:29 +0000399 /*
400 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
401 * mux must be switched to actually use a_sess_vld threshold.
402 */
Jim Lin2fefb8b2013-06-21 19:05:47 +0800403 if (config->dr_mode == DR_MODE_OTG &&
Simon Glassfec09c52015-01-05 20:05:39 -0700404 dm_gpio_is_valid(&config->vbus_gpio))
Lucas Stach26c32162013-02-07 07:16:29 +0000405 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
406 VBUS_SENSE_CTL_MASK,
407 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
Lucas Stach26c32162013-02-07 07:16:29 +0000408
Simon Glass92a419e2015-03-25 12:22:20 -0600409 controller = &fdt_usb_controllers[config->type];
410 debug("controller=%p, type=%d\n", controller, config->type);
411
Lucas Stach26c32162013-02-07 07:16:29 +0000412 /*
413 * PLL Delay CONFIGURATION settings. The following parameters control
414 * the bring up of the plls.
415 */
Simon Glass92a419e2015-03-25 12:22:20 -0600416 timing = get_pll_timing(controller);
Lucas Stach26c32162013-02-07 07:16:29 +0000417
Jim Lin2fefb8b2013-06-21 19:05:47 +0800418 if (!controller->has_hostpc) {
419 val = readl(&usbctlr->utmip_misc_cfg1);
420 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
421 timing[PARAM_STABLE_COUNT] <<
422 UTMIP_PLLU_STABLE_COUNT_SHIFT);
423 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
424 timing[PARAM_ACTIVE_DELAY_COUNT] <<
425 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
426 writel(val, &usbctlr->utmip_misc_cfg1);
Lucas Stach26c32162013-02-07 07:16:29 +0000427
Jim Lin2fefb8b2013-06-21 19:05:47 +0800428 /* Set PLL enable delay count and crystal frequency count */
429 val = readl(&usbctlr->utmip_pll_cfg1);
430 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
431 timing[PARAM_ENABLE_DELAY_COUNT] <<
432 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
433 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
434 timing[PARAM_XTAL_FREQ_COUNT] <<
435 UTMIP_XTAL_FREQ_COUNT_SHIFT);
436 writel(val, &usbctlr->utmip_pll_cfg1);
437 } else {
438 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
439
440 val = readl(&clkrst->crc_utmip_pll_cfg2);
441 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
442 timing[PARAM_STABLE_COUNT] <<
443 UTMIP_PLLU_STABLE_COUNT_SHIFT);
444 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
445 timing[PARAM_ACTIVE_DELAY_COUNT] <<
446 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
447 writel(val, &clkrst->crc_utmip_pll_cfg2);
448
449 /* Set PLL enable delay count and crystal frequency count */
450 val = readl(&clkrst->crc_utmip_pll_cfg1);
451 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
452 timing[PARAM_ENABLE_DELAY_COUNT] <<
453 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
454 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
455 timing[PARAM_XTAL_FREQ_COUNT] <<
456 UTMIP_XTAL_FREQ_COUNT_SHIFT);
457 writel(val, &clkrst->crc_utmip_pll_cfg1);
458
459 /* Disable Power Down state for PLL */
460 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
461 PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
462 PLL_ACTIVE_POWERDOWN);
463
464 /* Recommended PHY settings for EYE diagram */
465 val = readl(&usbctlr->utmip_xcvr_cfg0);
466 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
467 0x4 << UTMIP_XCVR_SETUP_SHIFT);
468 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
469 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
470 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
471 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
472 writel(val, &usbctlr->utmip_xcvr_cfg0);
473 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
474 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
475 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
476
477 /* Some registers can be controlled from USB1 only. */
478 if (config->periph_id != PERIPH_ID_USBD) {
479 clock_enable(PERIPH_ID_USBD);
480 /* Disable Reset if in Reset state */
481 reset_set_enable(PERIPH_ID_USBD, 0);
482 }
483 usb1ctlr = (struct usb_ctlr *)
Thierry Reding4375b442015-03-20 12:41:27 +0100484 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin2fefb8b2013-06-21 19:05:47 +0800485 val = readl(&usb1ctlr->utmip_bias_cfg0);
486 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
487 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
488 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
489 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
490 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
491 writel(val, &usb1ctlr->utmip_bias_cfg0);
492
493 /* Miscellaneous setting mentioned in Programming Guide */
494 clrbits_le32(&usbctlr->utmip_misc_cfg0,
495 UTMIP_SUSPEND_EXIT_ON_EDGE);
496 }
Lucas Stach26c32162013-02-07 07:16:29 +0000497
498 /* Setting the tracking length time */
499 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
500 UTMIP_BIAS_PDTRK_COUNT_MASK,
501 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
502
503 /* Program debounce time for VBUS to become valid */
504 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
505 UTMIP_DEBOUNCE_CFG0_MASK,
506 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
507
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700508 if (timing[PARAM_DEBOUNCE_A_TIME] > 0xFFFF) {
509 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
510 UTMIP_DEBOUNCE_CFG0_MASK,
511 (timing[PARAM_DEBOUNCE_A_TIME] >> 1)
512 << UTMIP_DEBOUNCE_CFG0_SHIFT);
513 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
514 UTMIP_BIAS_DEBOUNCE_TIMESCALE_MASK,
515 1 << UTMIP_BIAS_DEBOUNCE_TIMESCALE_SHIFT);
516 }
517
Lucas Stach26c32162013-02-07 07:16:29 +0000518 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
519
520 /* Disable battery charge enabling bit */
521 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
522
523 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
524 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
525
526 /*
527 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
528 * Setting these fields, together with default values of the
529 * other fields, results in programming the registers below as
530 * follows:
531 * UTMIP_HSRX_CFG0 = 0x9168c000
532 * UTMIP_HSRX_CFG1 = 0x13
533 */
534
535 /* Set PLL enable delay count and Crystal frequency count */
536 val = readl(&usbctlr->utmip_hsrx_cfg0);
537 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
538 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
539 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
540 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
541 writel(val, &usbctlr->utmip_hsrx_cfg0);
542
543 /* Configure the UTMIP_HS_SYNC_START_DLY */
544 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
545 UTMIP_HS_SYNC_START_DLY_MASK,
546 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
547
548 /* Preceed the crystal clock disable by >100ns delay. */
549 udelay(1);
550
551 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
552 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
553
Jim Lin2fefb8b2013-06-21 19:05:47 +0800554 if (controller->has_hostpc) {
555 if (config->periph_id == PERIPH_ID_USBD)
556 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
557 UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
Stefan Agnerafc492b2014-03-02 19:46:48 +0100558 if (config->periph_id == PERIPH_ID_USB2)
559 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
560 UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
Jim Lin2fefb8b2013-06-21 19:05:47 +0800561 if (config->periph_id == PERIPH_ID_USB3)
562 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
563 UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
564 }
Lucas Stach26c32162013-02-07 07:16:29 +0000565 /* Finished the per-controller init. */
566
567 /* De-assert UTMIP_RESET to bring out of reset. */
568 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
569
570 /* Wait for the phy clock to become valid in 100 ms */
571 for (loop_count = 100000; loop_count != 0; loop_count--) {
572 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
573 break;
574 udelay(1);
575 }
576 if (!loop_count)
Simon Glass6f7d3422015-03-25 12:22:46 -0600577 return -ETIMEDOUT;
Lucas Stach26c32162013-02-07 07:16:29 +0000578
579 /* Disable ICUSB FS/LS transceiver */
580 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
581
582 /* Select UTMI parallel interface */
Stephen Warren8b263f92014-04-30 15:09:57 -0600583 init_phy_mux(config, PTS_UTMI, init);
Lucas Stach26c32162013-02-07 07:16:29 +0000584
585 /* Deassert power down state */
586 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
587 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
588 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
589 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
590
Jim Lin2fefb8b2013-06-21 19:05:47 +0800591 if (controller->has_hostpc) {
592 /*
593 * BIAS Pad Power Down is common among all 3 USB
594 * controllers and can be controlled from USB1 only.
595 */
596 usb1ctlr = (struct usb_ctlr *)
Thierry Reding4375b442015-03-20 12:41:27 +0100597 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin2fefb8b2013-06-21 19:05:47 +0800598 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
599 udelay(25);
600 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
601 UTMIP_FORCE_PDTRK_POWERDOWN);
602 }
Lucas Stach26c32162013-02-07 07:16:29 +0000603 return 0;
604}
605
606#ifdef CONFIG_USB_ULPI
607/* if board file does not set a ULPI reference frequency we default to 24MHz */
Tom Rini364d0022023-01-10 11:19:45 -0500608#ifndef CFG_ULPI_REF_CLK
609#define CFG_ULPI_REF_CLK 24000000
Lucas Stach26c32162013-02-07 07:16:29 +0000610#endif
611
612/* set up the ULPI USB controller with the parameters provided */
Stephen Warren8b263f92014-04-30 15:09:57 -0600613static int init_ulpi_usb_controller(struct fdt_usb *config,
614 enum usb_init_type init)
Lucas Stach26c32162013-02-07 07:16:29 +0000615{
616 u32 val;
617 int loop_count;
618 struct ulpi_viewport ulpi_vp;
619 struct usb_ctlr *usbctlr = config->reg;
Simon Glass6f7d3422015-03-25 12:22:46 -0600620 int ret;
Lucas Stach26c32162013-02-07 07:16:29 +0000621
622 /* set up ULPI reference clock on pllp_out4 */
623 clock_enable(PERIPH_ID_DEV2_OUT);
Tom Rini364d0022023-01-10 11:19:45 -0500624 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CFG_ULPI_REF_CLK);
Lucas Stach26c32162013-02-07 07:16:29 +0000625
626 /* reset ULPI phy */
Simon Glassfec09c52015-01-05 20:05:39 -0700627 if (dm_gpio_is_valid(&config->phy_reset_gpio)) {
Stephen Warren7c259752016-09-15 12:19:37 -0600628 /*
629 * This GPIO is typically active-low, and marked as such in
630 * device tree. dm_gpio_set_value() takes this into account
631 * and inverts the value we pass here if required. In other
632 * words, this first call logically asserts the reset signal,
633 * which typically results in driving the physical GPIO low,
634 * and the second call logically de-asserts the reset signal,
635 * which typically results in driver the GPIO high.
636 */
Simon Glassfec09c52015-01-05 20:05:39 -0700637 dm_gpio_set_value(&config->phy_reset_gpio, 1);
Stephen Warren7c259752016-09-15 12:19:37 -0600638 mdelay(5);
639 dm_gpio_set_value(&config->phy_reset_gpio, 0);
Lucas Stach26c32162013-02-07 07:16:29 +0000640 }
641
642 /* Reset the usb controller */
643 clock_enable(config->periph_id);
644 usbf_reset_controller(config, usbctlr);
645
646 /* enable pinmux bypass */
647 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
648 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
649
650 /* Select ULPI parallel interface */
Stephen Warren8b263f92014-04-30 15:09:57 -0600651 init_phy_mux(config, PTS_ULPI, init);
Lucas Stach26c32162013-02-07 07:16:29 +0000652
653 /* enable ULPI transceiver */
654 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
655
656 /* configure ULPI transceiver timings */
657 val = 0;
658 writel(val, &usbctlr->ulpi_timing_ctrl_1);
659
660 val |= ULPI_DATA_TRIMMER_SEL(4);
661 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
662 val |= ULPI_DIR_TRIMMER_SEL(4);
663 writel(val, &usbctlr->ulpi_timing_ctrl_1);
664 udelay(10);
665
666 val |= ULPI_DATA_TRIMMER_LOAD;
667 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
668 val |= ULPI_DIR_TRIMMER_LOAD;
669 writel(val, &usbctlr->ulpi_timing_ctrl_1);
670
671 /* set up phy for host operation with external vbus supply */
672 ulpi_vp.port_num = 0;
673 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
674
Simon Glass6f7d3422015-03-25 12:22:46 -0600675 ret = ulpi_init(&ulpi_vp);
676 if (ret) {
Lucas Stach26c32162013-02-07 07:16:29 +0000677 printf("Tegra ULPI viewport init failed\n");
Simon Glass6f7d3422015-03-25 12:22:46 -0600678 return ret;
Lucas Stach26c32162013-02-07 07:16:29 +0000679 }
680
681 ulpi_set_vbus(&ulpi_vp, 1, 1);
682 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
683
684 /* enable wakeup events */
685 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
686
687 /* Enable and wait for the phy clock to become valid in 100 ms */
688 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
689 for (loop_count = 100000; loop_count != 0; loop_count--) {
690 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
691 break;
692 udelay(1);
693 }
694 if (!loop_count)
Simon Glass6f7d3422015-03-25 12:22:46 -0600695 return -ETIMEDOUT;
Lucas Stach26c32162013-02-07 07:16:29 +0000696 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
697
698 return 0;
699}
700#else
Stephen Warren8b263f92014-04-30 15:09:57 -0600701static int init_ulpi_usb_controller(struct fdt_usb *config,
702 enum usb_init_type init)
Lucas Stach26c32162013-02-07 07:16:29 +0000703{
704 printf("No code to set up ULPI controller, please enable"
705 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
Simon Glass6f7d3422015-03-25 12:22:46 -0600706 return -ENOSYS;
Lucas Stach26c32162013-02-07 07:16:29 +0000707}
708#endif
709
710static void config_clock(const u32 timing[])
711{
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700712 debug("%s: DIVM = %d, DIVN = %d, DIVP = %d, cpcon/lfcon = %d/%d\n",
713 __func__, timing[PARAM_DIVM], timing[PARAM_DIVN],
714 timing[PARAM_DIVP], timing[PARAM_CPCON], timing[PARAM_LFCON]);
715
Lucas Stach26c32162013-02-07 07:16:29 +0000716 clock_start_pll(CLOCK_ID_USB,
717 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
718 timing[PARAM_CPCON], timing[PARAM_LFCON]);
719}
720
Simon Glass09717782015-08-11 08:33:29 -0600721static int fdt_decode_usb(struct udevice *dev, struct fdt_usb *config)
Lucas Stach26c32162013-02-07 07:16:29 +0000722{
723 const char *phy, *mode;
724
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100725 config->reg = dev_read_addr_ptr(dev);
Simon Glass4c36be02017-07-25 08:30:04 -0600726 debug("reg=%p\n", config->reg);
727 mode = dev_read_string(dev, "dr_mode");
Lucas Stach26c32162013-02-07 07:16:29 +0000728 if (mode) {
729 if (0 == strcmp(mode, "host"))
730 config->dr_mode = DR_MODE_HOST;
731 else if (0 == strcmp(mode, "peripheral"))
732 config->dr_mode = DR_MODE_DEVICE;
733 else if (0 == strcmp(mode, "otg"))
734 config->dr_mode = DR_MODE_OTG;
735 else {
736 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
737 mode);
Simon Glass6f7d3422015-03-25 12:22:46 -0600738 return -EINVAL;
Lucas Stach26c32162013-02-07 07:16:29 +0000739 }
740 } else {
741 config->dr_mode = DR_MODE_HOST;
742 }
743
Simon Glass4c36be02017-07-25 08:30:04 -0600744 phy = dev_read_string(dev, "phy_type");
Lucas Stach26c32162013-02-07 07:16:29 +0000745 config->utmi = phy && 0 == strcmp("utmi", phy);
746 config->ulpi = phy && 0 == strcmp("ulpi", phy);
Simon Glass4c36be02017-07-25 08:30:04 -0600747 config->has_legacy_mode = dev_read_bool(dev, "nvidia,has-legacy-mode");
Simon Glassc3f26502017-07-25 08:30:00 -0600748 config->periph_id = clock_decode_periph_id(dev);
Lucas Stach26c32162013-02-07 07:16:29 +0000749 if (config->periph_id == PERIPH_ID_NONE) {
750 debug("%s: Missing/invalid peripheral ID\n", __func__);
Simon Glass6f7d3422015-03-25 12:22:46 -0600751 return -EINVAL;
Lucas Stach26c32162013-02-07 07:16:29 +0000752 }
Simon Glass4c36be02017-07-25 08:30:04 -0600753 gpio_request_by_name(dev, "nvidia,vbus-gpio", 0, &config->vbus_gpio,
754 GPIOD_IS_OUT);
755 gpio_request_by_name(dev, "nvidia,phy-reset-gpio", 0,
756 &config->phy_reset_gpio, GPIOD_IS_OUT);
757 debug("legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, vbus=%d, phy_reset=%d, dr_mode=%d, reg=%p\n",
758 config->has_legacy_mode, config->utmi, config->ulpi,
759 config->periph_id, gpio_get_number(&config->vbus_gpio),
760 gpio_get_number(&config->phy_reset_gpio), config->dr_mode,
761 config->reg);
Lucas Stach26c32162013-02-07 07:16:29 +0000762
763 return 0;
764}
765
Simon Glass2bd08a92015-03-25 12:22:47 -0600766int usb_common_init(struct fdt_usb *config, enum usb_init_type init)
767{
768 int ret = 0;
769
770 switch (init) {
771 case USB_INIT_HOST:
772 switch (config->dr_mode) {
773 case DR_MODE_HOST:
774 case DR_MODE_OTG:
775 break;
776 default:
777 printf("tegrausb: Invalid dr_mode %d for host mode\n",
778 config->dr_mode);
779 return -1;
780 }
781 break;
782 case USB_INIT_DEVICE:
783 if (config->periph_id != PERIPH_ID_USBD) {
784 printf("tegrausb: Device mode only supported on first USB controller\n");
785 return -1;
786 }
787 if (!config->utmi) {
788 printf("tegrausb: Device mode only supported with UTMI PHY\n");
789 return -1;
790 }
791 switch (config->dr_mode) {
792 case DR_MODE_DEVICE:
793 case DR_MODE_OTG:
794 break;
795 default:
796 printf("tegrausb: Invalid dr_mode %d for device mode\n",
797 config->dr_mode);
798 return -1;
799 }
800 break;
801 default:
802 printf("tegrausb: Unknown USB_INIT_* %d\n", init);
803 return -1;
804 }
805
Simon Glass2bd08a92015-03-25 12:22:47 -0600806 debug("%d, %d\n", config->utmi, config->ulpi);
807 if (config->utmi)
808 ret = init_utmi_usb_controller(config, init);
809 else if (config->ulpi)
810 ret = init_ulpi_usb_controller(config, init);
811 if (ret)
812 return ret;
813
814 set_up_vbus(config, init);
815
816 config->init_type = init;
817
818 return 0;
819}
820
821void usb_common_uninit(struct fdt_usb *priv)
822{
823 struct usb_ctlr *usbctlr;
824
825 usbctlr = priv->reg;
826
827 /* Stop controller */
828 writel(0, &usbctlr->usb_cmd);
829 udelay(1000);
830
831 /* Initiate controller reset */
832 writel(2, &usbctlr->usb_cmd);
833 udelay(1000);
834}
835
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600836static const struct ehci_ops tegra_ehci_ops = {
837 .set_usb_mode = tegra_ehci_set_usbmode,
838 .get_port_speed = tegra_ehci_get_port_speed,
839 .powerup_fixup = tegra_ehci_powerup_fixup,
840};
841
Simon Glassaad29ae2020-12-03 16:55:21 -0700842static int ehci_usb_of_to_plat(struct udevice *dev)
Simon Glass9c574772015-03-25 12:22:48 -0600843{
844 struct fdt_usb *priv = dev_get_priv(dev);
845 int ret;
846
Simon Glass09717782015-08-11 08:33:29 -0600847 ret = fdt_decode_usb(dev, priv);
Simon Glass9c574772015-03-25 12:22:48 -0600848 if (ret)
849 return ret;
850
851 priv->type = dev_get_driver_data(dev);
852
853 return 0;
854}
855
856static int ehci_usb_probe(struct udevice *dev)
857{
Simon Glassb75b15b2020-12-03 16:55:23 -0700858 struct usb_plat *plat = dev_get_plat(dev);
Simon Glass9c574772015-03-25 12:22:48 -0600859 struct fdt_usb *priv = dev_get_priv(dev);
860 struct ehci_hccr *hccr;
861 struct ehci_hcor *hcor;
862 static bool clk_done;
863 int ret;
864
865 ret = usb_common_init(priv, plat->init_type);
866 if (ret)
867 return ret;
868 hccr = (struct ehci_hccr *)&priv->reg->cap_length;
869 hcor = (struct ehci_hcor *)&priv->reg->usb_cmd;
870 if (!clk_done) {
871 config_clock(get_pll_timing(&fdt_usb_controllers[priv->type]));
872 clk_done = true;
873 }
874
875 return ehci_register(dev, hccr, hcor, &tegra_ehci_ops, 0,
876 plat->init_type);
877}
878
Simon Glass9c574772015-03-25 12:22:48 -0600879static const struct udevice_id ehci_usb_ids[] = {
880 { .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 },
881 { .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 },
882 { .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 },
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700883 { .compatible = "nvidia,tegra210-ehci", .data = USB_CTLR_T210 },
Simon Glass9c574772015-03-25 12:22:48 -0600884 { }
885};
886
887U_BOOT_DRIVER(usb_ehci) = {
888 .name = "ehci_tegra",
889 .id = UCLASS_USB,
890 .of_match = ehci_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700891 .of_to_plat = ehci_usb_of_to_plat,
Simon Glass9c574772015-03-25 12:22:48 -0600892 .probe = ehci_usb_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900893 .remove = ehci_deregister,
Simon Glass9c574772015-03-25 12:22:48 -0600894 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700895 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700896 .priv_auto = sizeof(struct fdt_usb),
Simon Glass9c574772015-03-25 12:22:48 -0600897 .flags = DM_FLAG_ALLOC_PRIV_DMA,
898};