blob: 2cf16256703d0dd0e149c9be38325a84432c5e55 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass87f938c2012-02-27 10:52:49 +00002/*
Lucas Stach7ae18f32013-02-07 07:16:29 +00003 * Copyright (c) 2011 The Chromium OS Authors.
Tom Warren7aaa5a62015-03-04 16:36:00 -07004 * Copyright (c) 2009-2015 NVIDIA Corporation
Lucas Stach7ae18f32013-02-07 07:16:29 +00005 * Copyright (c) 2013 Lucas Stach
Simon Glass87f938c2012-02-27 10:52:49 +00006 */
7
8#include <common.h>
Simon Glassc3980ad2015-03-25 12:22:48 -06009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000013#include <asm/io.h>
14#include <asm-generic/gpio.h>
15#include <asm/arch/clock.h>
16#include <asm/arch-tegra/usb.h>
Jim Lin7e44d932013-06-21 19:05:47 +080017#include <asm/arch-tegra/clk_rst.h>
Simon Glass87f938c2012-02-27 10:52:49 +000018#include <usb.h>
Lucas Stach7ae18f32013-02-07 07:16:29 +000019#include <usb/ulpi.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090020#include <linux/libfdt.h>
Simon Glass87f938c2012-02-27 10:52:49 +000021
22#include "ehci.h"
Simon Glass87f938c2012-02-27 10:52:49 +000023
Jim Lin7e44d932013-06-21 19:05:47 +080024#define USB1_ADDR_MASK 0xFFFF0000
25
26#define HOSTPC1_DEVLC 0x84
27#define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3)
28
Lucas Stach7ae18f32013-02-07 07:16:29 +000029#ifdef CONFIG_USB_ULPI
30 #ifndef CONFIG_USB_ULPI_VIEWPORT
31 #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
32 define CONFIG_USB_ULPI_VIEWPORT"
33 #endif
34#endif
35
Lucas Stach7ae18f32013-02-07 07:16:29 +000036/* Parameters we need for USB */
37enum {
38 PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
39 PARAM_DIVM, /* PLL INPUT DIVIDER */
40 PARAM_DIVP, /* POST DIVIDER (2^N) */
41 PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
42 PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
43 PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
44 PARAM_STABLE_COUNT, /* PLL-U STABLE count */
45 PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
46 PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
47 PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
48 PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
49
50 PARAM_COUNT
51};
52
53/* Possible port types (dual role mode) */
54enum dr_mode {
55 DR_MODE_NONE = 0,
56 DR_MODE_HOST, /* supports host operation */
57 DR_MODE_DEVICE, /* supports device operation */
58 DR_MODE_OTG, /* supports both */
59};
60
Simon Glass27f782b2015-03-25 12:22:20 -060061enum usb_ctlr_type {
62 USB_CTLR_T20,
63 USB_CTLR_T30,
64 USB_CTLR_T114,
Tom Warren7aaa5a62015-03-04 16:36:00 -070065 USB_CTLR_T210,
Simon Glass27f782b2015-03-25 12:22:20 -060066
67 USB_CTRL_COUNT,
68};
69
Lucas Stach7ae18f32013-02-07 07:16:29 +000070/* Information about a USB port */
71struct fdt_usb {
Simon Glassc3980ad2015-03-25 12:22:48 -060072 struct ehci_ctrl ehci;
Lucas Stach7ae18f32013-02-07 07:16:29 +000073 struct usb_ctlr *reg; /* address of registers in physical memory */
74 unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
75 unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
76 unsigned enabled:1; /* 1 to enable, 0 to disable */
77 unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
Simon Glass27f782b2015-03-25 12:22:20 -060078 enum usb_ctlr_type type;
Stephen Warrena4539a22014-04-30 15:09:57 -060079 enum usb_init_type init_type;
Lucas Stach7ae18f32013-02-07 07:16:29 +000080 enum dr_mode dr_mode; /* dual role mode */
81 enum periph_id periph_id;/* peripheral id */
Simon Glass46927e12015-01-05 20:05:39 -070082 struct gpio_desc vbus_gpio; /* GPIO for vbus enable */
83 struct gpio_desc phy_reset_gpio; /* GPIO to reset ULPI phy */
Lucas Stach7ae18f32013-02-07 07:16:29 +000084};
85
Lucas Stach7ae18f32013-02-07 07:16:29 +000086/*
87 * This table has USB timing parameters for each Oscillator frequency we
88 * support. There are four sets of values:
89 *
90 * 1. PLLU configuration information (reference clock is osc/clk_m and
91 * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
92 *
93 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
94 * ----------------------------------------------------------------------
95 * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
96 * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
97 * Filter frequency (MHz) 1 4.8 6 2
98 * CPCON 1100b 0011b 1100b 1100b
99 * LFCON0 0 0 0 0
100 *
101 * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
102 *
103 * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
104 * ---------------------------------------------------------------------------
105 * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
106 * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
107 * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
108 * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
109 *
110 * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
111 * SessEnd. Each of these signals have their own debouncer and for each of
112 * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
113 * BIAS_DEBOUNCE_B).
114 *
115 * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
116 * 0xffff -> No debouncing at all
117 * <n> ms = <n> *1000 / (1/19.2MHz) / 4
118 *
119 * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
120 * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
121 *
122 * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
123 * values, so we can keep those to default.
124 *
125 * 4. The 20 microsecond delay after bias cell operation.
126 */
Jim Lin7e44d932013-06-21 19:05:47 +0800127static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
Lucas Stach7ae18f32013-02-07 07:16:29 +0000128 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
129 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200130 { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
Tom Warren3e8650c2015-06-22 13:03:44 -0700131 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200132 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
133 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
134 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
135 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
136 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
137 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
138 { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
139 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
140 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
141 { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000142};
143
Jim Lin7e44d932013-06-21 19:05:47 +0800144static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
145 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
146 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200147 { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
Tom Warren3e8650c2015-06-22 13:03:44 -0700148 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200149 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
150 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
151 { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
152 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
153 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
154 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
155 { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
156 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
157 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
158 { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
Jim Lin7e44d932013-06-21 19:05:47 +0800159};
160
161static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
162 /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
163 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200164 { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
Tom Warren3e8650c2015-06-22 13:03:44 -0700165 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200166 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
167 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
168 { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
169 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
170 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
171 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
172 { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
173 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
174 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0x0000, 0 },
175 { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 11 }
Jim Lin7e44d932013-06-21 19:05:47 +0800176};
177
Tom Warren7aaa5a62015-03-04 16:36:00 -0700178/* NOTE: 13/26MHz settings are N/A for T210, so dupe 12MHz settings for now */
179static const unsigned T210_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
180 /* DivN, DivM, DivP, KCP, KVCO, Delays Debounce, Bias */
Tom Warren3e8650c2015-06-22 13:03:44 -0700181 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 32500, 5 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200182 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 32500, 5 },
183 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
184 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
Tom Warren7aaa5a62015-03-04 16:36:00 -0700185 { 0x019, 0x01, 0x01, 0x0, 0, 0x03, 0x4B, 0x0C, 0xBB, 48000, 8 },
Tom Warren3e8650c2015-06-22 13:03:44 -0700186 { 0x019, 0x02, 0x01, 0x0, 0, 0x05, 0x96, 0x18, 0x177, 96000, 15 },
Svyatoslav Ryhel87a75862023-02-01 10:53:01 +0200187 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
188 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
189 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 30000, 5 },
190 { 0x028, 0x04, 0x01, 0x0, 0, 0x04, 0x66, 0x09, 0xFE, 120000, 20 },
191 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
192 { 0x000, 0x00, 0x00, 0x0, 0, 0x00, 0x00, 0x00, 0x00, 0, 0 },
193 { 0x028, 0x01, 0x01, 0x0, 0, 0x02, 0x2F, 0x08, 0x76, 65000, 5 }
Tom Warren7aaa5a62015-03-04 16:36:00 -0700194};
195
Lucas Stach7ae18f32013-02-07 07:16:29 +0000196/* UTMIP Idle Wait Delay */
197static const u8 utmip_idle_wait_delay = 17;
198
199/* UTMIP Elastic limit */
200static const u8 utmip_elastic_limit = 16;
201
202/* UTMIP High Speed Sync Start Delay */
203static const u8 utmip_hs_sync_start_delay = 9;
Simon Glass87f938c2012-02-27 10:52:49 +0000204
Jim Lin7e44d932013-06-21 19:05:47 +0800205struct fdt_usb_controller {
Jim Lin7e44d932013-06-21 19:05:47 +0800206 /* flag to determine whether controller supports hostpc register */
207 u32 has_hostpc:1;
208 const unsigned *pll_parameter;
209};
210
Simon Glass27f782b2015-03-25 12:22:20 -0600211static struct fdt_usb_controller fdt_usb_controllers[USB_CTRL_COUNT] = {
Jim Lin7e44d932013-06-21 19:05:47 +0800212 {
Jim Lin7e44d932013-06-21 19:05:47 +0800213 .has_hostpc = 0,
214 .pll_parameter = (const unsigned *)T20_usb_pll,
215 },
216 {
Jim Lin7e44d932013-06-21 19:05:47 +0800217 .has_hostpc = 1,
218 .pll_parameter = (const unsigned *)T30_usb_pll,
219 },
220 {
Jim Lin7e44d932013-06-21 19:05:47 +0800221 .has_hostpc = 1,
222 .pll_parameter = (const unsigned *)T114_usb_pll,
223 },
Tom Warren7aaa5a62015-03-04 16:36:00 -0700224 {
225 .has_hostpc = 1,
226 .pll_parameter = (const unsigned *)T210_usb_pll,
227 },
Jim Lin7e44d932013-06-21 19:05:47 +0800228};
229
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000230/*
231 * A known hardware issue where Connect Status Change bit of PORTSC register
232 * of USB1 controller will be set after Port Reset.
233 * We have to clear it in order for later device enumeration to proceed.
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000234 */
Simon Glassdeb85082015-03-25 12:22:27 -0600235static void tegra_ehci_powerup_fixup(struct ehci_ctrl *ctrl,
236 uint32_t *status_reg, uint32_t *reg)
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000237{
Simon Glass56d42732015-03-25 12:22:22 -0600238 struct fdt_usb *config = ctrl->priv;
239 struct fdt_usb_controller *controller;
240
241 controller = &fdt_usb_controllers[config->type];
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000242 mdelay(50);
Jim Lin7e44d932013-06-21 19:05:47 +0800243 /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
244 if (controller->has_hostpc)
245 *reg |= EHCI_PS_PE;
246
Simon Glass943104f2015-03-25 12:22:45 -0600247 if (!config->has_legacy_mode)
Jim Lin8b3f7bf2012-06-24 20:40:57 +0000248 return;
249 /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
250 if (ehci_readl(status_reg) & EHCI_PS_CSC)
251 *reg |= EHCI_PS_CSC;
252}
Simon Glass87f938c2012-02-27 10:52:49 +0000253
Simon Glassdeb85082015-03-25 12:22:27 -0600254static void tegra_ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Lin7e44d932013-06-21 19:05:47 +0800255{
Simon Glass11d18a12015-03-25 12:22:23 -0600256 struct fdt_usb *config = ctrl->priv;
Jim Lin7e44d932013-06-21 19:05:47 +0800257 struct usb_ctlr *usbctlr;
258 uint32_t tmp;
259
Jim Lin7e44d932013-06-21 19:05:47 +0800260 usbctlr = config->reg;
261
262 tmp = ehci_readl(&usbctlr->usb_mode);
263 tmp |= USBMODE_CM_HC;
264 ehci_writel(&usbctlr->usb_mode, tmp);
265}
266
Simon Glassdeb85082015-03-25 12:22:27 -0600267static int tegra_ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Lin7e44d932013-06-21 19:05:47 +0800268{
Simon Glass56d42732015-03-25 12:22:22 -0600269 struct fdt_usb *config = ctrl->priv;
270 struct fdt_usb_controller *controller;
Jim Lin7e44d932013-06-21 19:05:47 +0800271 uint32_t tmp;
272 uint32_t *reg_ptr;
273
Simon Glass56d42732015-03-25 12:22:22 -0600274 controller = &fdt_usb_controllers[config->type];
Jim Lin7e44d932013-06-21 19:05:47 +0800275 if (controller->has_hostpc) {
Simon Glass73382872015-03-25 12:22:18 -0600276 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd +
277 HOSTPC1_DEVLC);
Jim Lin7e44d932013-06-21 19:05:47 +0800278 tmp = ehci_readl(reg_ptr);
279 return HOSTPC1_PSPD(tmp);
280 } else
281 return PORTSC_PSPD(reg);
282}
283
Stephen Warrena4539a22014-04-30 15:09:57 -0600284/* Set up VBUS for host/device mode */
285static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000286{
287 /*
Stephen Warrena4539a22014-04-30 15:09:57 -0600288 * If we are an OTG port initializing in host mode,
289 * check if remote host is driving VBus and bail out in this case.
Lucas Stach7ae18f32013-02-07 07:16:29 +0000290 */
Stephen Warrena4539a22014-04-30 15:09:57 -0600291 if (init == USB_INIT_HOST &&
292 config->dr_mode == DR_MODE_OTG &&
293 (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
294 printf("tegrausb: VBUS input active; not enabling as host\n");
Lucas Stach7ae18f32013-02-07 07:16:29 +0000295 return;
Stephen Warrena4539a22014-04-30 15:09:57 -0600296 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000297
Simon Glass46927e12015-01-05 20:05:39 -0700298 if (dm_gpio_is_valid(&config->vbus_gpio)) {
Stephen Warrena4539a22014-04-30 15:09:57 -0600299 int vbus_value;
300
Simon Glass46927e12015-01-05 20:05:39 -0700301 vbus_value = (init == USB_INIT_HOST);
302 dm_gpio_set_value(&config->vbus_gpio, vbus_value);
Stephen Warrena4539a22014-04-30 15:09:57 -0600303
Simon Glass46927e12015-01-05 20:05:39 -0700304 debug("set_up_vbus: GPIO %d %d\n",
305 gpio_get_number(&config->vbus_gpio), vbus_value);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000306 }
307}
308
Simon Glass7e27bdd2015-03-25 12:22:46 -0600309static void usbf_reset_controller(struct fdt_usb *config,
310 struct usb_ctlr *usbctlr)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000311{
312 /* Reset the USB controller with 2us delay */
313 reset_periph(config->periph_id, 2);
314
315 /*
316 * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
317 * base address
318 */
319 if (config->has_legacy_mode)
320 setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
321
322 /* Put UTMIP1/3 in reset */
323 setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
324
325 /* Enable the UTMIP PHY */
326 if (config->utmi)
327 setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
328}
329
Simon Glass27f782b2015-03-25 12:22:20 -0600330static const unsigned *get_pll_timing(struct fdt_usb_controller *controller)
Jim Lin7e44d932013-06-21 19:05:47 +0800331{
332 const unsigned *timing;
333
334 timing = controller->pll_parameter +
335 clock_get_osc_freq() * PARAM_COUNT;
336
337 return timing;
338}
339
Stephen Warren2d341512014-04-30 15:09:56 -0600340/* select the PHY to use with a USB controller */
Stephen Warrena4539a22014-04-30 15:09:57 -0600341static void init_phy_mux(struct fdt_usb *config, uint pts,
342 enum usb_init_type init)
Stephen Warren2d341512014-04-30 15:09:56 -0600343{
344 struct usb_ctlr *usbctlr = config->reg;
345
346#if defined(CONFIG_TEGRA20)
347 if (config->periph_id == PERIPH_ID_USBD) {
348 clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
Marcel Ziswilerd1fcbae2014-10-04 01:46:10 +0200349 pts << PTS1_SHIFT);
Stephen Warren2d341512014-04-30 15:09:56 -0600350 clrbits_le32(&usbctlr->port_sc1, STS1);
351 } else {
352 clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
Marcel Ziswilerd1fcbae2014-10-04 01:46:10 +0200353 pts << PTS_SHIFT);
Stephen Warren2d341512014-04-30 15:09:56 -0600354 clrbits_le32(&usbctlr->port_sc1, STS);
355 }
356#else
Stephen Warrena4539a22014-04-30 15:09:57 -0600357 /* Set to Host mode (if applicable) after Controller Reset was done */
Stephen Warren2d341512014-04-30 15:09:56 -0600358 clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
Stephen Warrena4539a22014-04-30 15:09:57 -0600359 (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
360 /*
361 * Select PHY interface after setting host mode.
362 * For device mode, the ordering requirement is not an issue, since
363 * only the first USB controller supports device mode, and that USB
364 * controller can only talk to a UTMI PHY, so the PHY selection is
365 * already made at reset time, so this write is a no-op.
366 */
Stephen Warren2d341512014-04-30 15:09:56 -0600367 clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
368 pts << PTS_SHIFT);
369 clrbits_le32(&usbctlr->hostpc1_devlc, STS);
370#endif
371}
372
Lucas Stach7ae18f32013-02-07 07:16:29 +0000373/* set up the UTMI USB controller with the parameters provided */
Stephen Warrena4539a22014-04-30 15:09:57 -0600374static int init_utmi_usb_controller(struct fdt_usb *config,
375 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000376{
Simon Glass27f782b2015-03-25 12:22:20 -0600377 struct fdt_usb_controller *controller;
Stephen Warrena4539a22014-04-30 15:09:57 -0600378 u32 b_sess_valid_mask, val;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000379 int loop_count;
380 const unsigned *timing;
381 struct usb_ctlr *usbctlr = config->reg;
Jim Lin7e44d932013-06-21 19:05:47 +0800382 struct clk_rst_ctlr *clkrst;
383 struct usb_ctlr *usb1ctlr;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000384
385 clock_enable(config->periph_id);
386
387 /* Reset the usb controller */
388 usbf_reset_controller(config, usbctlr);
389
390 /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
391 clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
392
393 /* Follow the crystal clock disable by >100ns delay */
394 udelay(1);
395
Stephen Warrena4539a22014-04-30 15:09:57 -0600396 b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
397 clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
398 (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
399
Lucas Stach7ae18f32013-02-07 07:16:29 +0000400 /*
401 * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
402 * mux must be switched to actually use a_sess_vld threshold.
403 */
Jim Lin7e44d932013-06-21 19:05:47 +0800404 if (config->dr_mode == DR_MODE_OTG &&
Simon Glass46927e12015-01-05 20:05:39 -0700405 dm_gpio_is_valid(&config->vbus_gpio))
Lucas Stach7ae18f32013-02-07 07:16:29 +0000406 clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
407 VBUS_SENSE_CTL_MASK,
408 VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000409
Simon Glass27f782b2015-03-25 12:22:20 -0600410 controller = &fdt_usb_controllers[config->type];
411 debug("controller=%p, type=%d\n", controller, config->type);
412
Lucas Stach7ae18f32013-02-07 07:16:29 +0000413 /*
414 * PLL Delay CONFIGURATION settings. The following parameters control
415 * the bring up of the plls.
416 */
Simon Glass27f782b2015-03-25 12:22:20 -0600417 timing = get_pll_timing(controller);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000418
Jim Lin7e44d932013-06-21 19:05:47 +0800419 if (!controller->has_hostpc) {
420 val = readl(&usbctlr->utmip_misc_cfg1);
421 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
422 timing[PARAM_STABLE_COUNT] <<
423 UTMIP_PLLU_STABLE_COUNT_SHIFT);
424 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
425 timing[PARAM_ACTIVE_DELAY_COUNT] <<
426 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
427 writel(val, &usbctlr->utmip_misc_cfg1);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000428
Jim Lin7e44d932013-06-21 19:05:47 +0800429 /* Set PLL enable delay count and crystal frequency count */
430 val = readl(&usbctlr->utmip_pll_cfg1);
431 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
432 timing[PARAM_ENABLE_DELAY_COUNT] <<
433 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
434 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
435 timing[PARAM_XTAL_FREQ_COUNT] <<
436 UTMIP_XTAL_FREQ_COUNT_SHIFT);
437 writel(val, &usbctlr->utmip_pll_cfg1);
438 } else {
439 clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
440
441 val = readl(&clkrst->crc_utmip_pll_cfg2);
442 clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
443 timing[PARAM_STABLE_COUNT] <<
444 UTMIP_PLLU_STABLE_COUNT_SHIFT);
445 clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
446 timing[PARAM_ACTIVE_DELAY_COUNT] <<
447 UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
448 writel(val, &clkrst->crc_utmip_pll_cfg2);
449
450 /* Set PLL enable delay count and crystal frequency count */
451 val = readl(&clkrst->crc_utmip_pll_cfg1);
452 clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
453 timing[PARAM_ENABLE_DELAY_COUNT] <<
454 UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
455 clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
456 timing[PARAM_XTAL_FREQ_COUNT] <<
457 UTMIP_XTAL_FREQ_COUNT_SHIFT);
458 writel(val, &clkrst->crc_utmip_pll_cfg1);
459
460 /* Disable Power Down state for PLL */
461 clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
462 PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
463 PLL_ACTIVE_POWERDOWN);
464
465 /* Recommended PHY settings for EYE diagram */
466 val = readl(&usbctlr->utmip_xcvr_cfg0);
467 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
468 0x4 << UTMIP_XCVR_SETUP_SHIFT);
469 clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
470 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
471 clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
472 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
473 writel(val, &usbctlr->utmip_xcvr_cfg0);
474 clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
475 UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
476 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
477
478 /* Some registers can be controlled from USB1 only. */
479 if (config->periph_id != PERIPH_ID_USBD) {
480 clock_enable(PERIPH_ID_USBD);
481 /* Disable Reset if in Reset state */
482 reset_set_enable(PERIPH_ID_USBD, 0);
483 }
484 usb1ctlr = (struct usb_ctlr *)
Thierry Reding96df9c72015-03-20 12:41:27 +0100485 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin7e44d932013-06-21 19:05:47 +0800486 val = readl(&usb1ctlr->utmip_bias_cfg0);
487 setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
488 clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
489 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
490 clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
491 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
492 writel(val, &usb1ctlr->utmip_bias_cfg0);
493
494 /* Miscellaneous setting mentioned in Programming Guide */
495 clrbits_le32(&usbctlr->utmip_misc_cfg0,
496 UTMIP_SUSPEND_EXIT_ON_EDGE);
497 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000498
499 /* Setting the tracking length time */
500 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
501 UTMIP_BIAS_PDTRK_COUNT_MASK,
502 timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
503
504 /* Program debounce time for VBUS to become valid */
505 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
506 UTMIP_DEBOUNCE_CFG0_MASK,
507 timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
508
Tom Warren7aaa5a62015-03-04 16:36:00 -0700509 if (timing[PARAM_DEBOUNCE_A_TIME] > 0xFFFF) {
510 clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
511 UTMIP_DEBOUNCE_CFG0_MASK,
512 (timing[PARAM_DEBOUNCE_A_TIME] >> 1)
513 << UTMIP_DEBOUNCE_CFG0_SHIFT);
514 clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
515 UTMIP_BIAS_DEBOUNCE_TIMESCALE_MASK,
516 1 << UTMIP_BIAS_DEBOUNCE_TIMESCALE_SHIFT);
517 }
518
Lucas Stach7ae18f32013-02-07 07:16:29 +0000519 setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
520
521 /* Disable battery charge enabling bit */
522 setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
523
524 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
525 setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
526
527 /*
528 * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
529 * Setting these fields, together with default values of the
530 * other fields, results in programming the registers below as
531 * follows:
532 * UTMIP_HSRX_CFG0 = 0x9168c000
533 * UTMIP_HSRX_CFG1 = 0x13
534 */
535
536 /* Set PLL enable delay count and Crystal frequency count */
537 val = readl(&usbctlr->utmip_hsrx_cfg0);
538 clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
539 utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
540 clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
541 utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
542 writel(val, &usbctlr->utmip_hsrx_cfg0);
543
544 /* Configure the UTMIP_HS_SYNC_START_DLY */
545 clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
546 UTMIP_HS_SYNC_START_DLY_MASK,
547 utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
548
549 /* Preceed the crystal clock disable by >100ns delay. */
550 udelay(1);
551
552 /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
553 setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
554
Jim Lin7e44d932013-06-21 19:05:47 +0800555 if (controller->has_hostpc) {
556 if (config->periph_id == PERIPH_ID_USBD)
557 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
558 UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
Stefan Agnerb03f4b32014-03-02 19:46:48 +0100559 if (config->periph_id == PERIPH_ID_USB2)
560 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
561 UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
Jim Lin7e44d932013-06-21 19:05:47 +0800562 if (config->periph_id == PERIPH_ID_USB3)
563 clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
564 UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
565 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000566 /* Finished the per-controller init. */
567
568 /* De-assert UTMIP_RESET to bring out of reset. */
569 clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
570
571 /* Wait for the phy clock to become valid in 100 ms */
572 for (loop_count = 100000; loop_count != 0; loop_count--) {
573 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
574 break;
575 udelay(1);
576 }
577 if (!loop_count)
Simon Glass7e27bdd2015-03-25 12:22:46 -0600578 return -ETIMEDOUT;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000579
580 /* Disable ICUSB FS/LS transceiver */
581 clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
582
583 /* Select UTMI parallel interface */
Stephen Warrena4539a22014-04-30 15:09:57 -0600584 init_phy_mux(config, PTS_UTMI, init);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000585
586 /* Deassert power down state */
587 clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
588 UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
589 clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
590 UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
591
Jim Lin7e44d932013-06-21 19:05:47 +0800592 if (controller->has_hostpc) {
593 /*
594 * BIAS Pad Power Down is common among all 3 USB
595 * controllers and can be controlled from USB1 only.
596 */
597 usb1ctlr = (struct usb_ctlr *)
Thierry Reding96df9c72015-03-20 12:41:27 +0100598 ((unsigned long)config->reg & USB1_ADDR_MASK);
Jim Lin7e44d932013-06-21 19:05:47 +0800599 clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
600 udelay(25);
601 clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
602 UTMIP_FORCE_PDTRK_POWERDOWN);
603 }
Lucas Stach7ae18f32013-02-07 07:16:29 +0000604 return 0;
605}
606
607#ifdef CONFIG_USB_ULPI
608/* if board file does not set a ULPI reference frequency we default to 24MHz */
Tom Rini6e7df1d2023-01-10 11:19:45 -0500609#ifndef CFG_ULPI_REF_CLK
610#define CFG_ULPI_REF_CLK 24000000
Lucas Stach7ae18f32013-02-07 07:16:29 +0000611#endif
612
613/* set up the ULPI USB controller with the parameters provided */
Stephen Warrena4539a22014-04-30 15:09:57 -0600614static int init_ulpi_usb_controller(struct fdt_usb *config,
615 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000616{
617 u32 val;
618 int loop_count;
619 struct ulpi_viewport ulpi_vp;
620 struct usb_ctlr *usbctlr = config->reg;
Simon Glass7e27bdd2015-03-25 12:22:46 -0600621 int ret;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000622
623 /* set up ULPI reference clock on pllp_out4 */
624 clock_enable(PERIPH_ID_DEV2_OUT);
Tom Rini6e7df1d2023-01-10 11:19:45 -0500625 clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CFG_ULPI_REF_CLK);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000626
627 /* reset ULPI phy */
Simon Glass46927e12015-01-05 20:05:39 -0700628 if (dm_gpio_is_valid(&config->phy_reset_gpio)) {
Stephen Warren2f6a7e82016-09-15 12:19:37 -0600629 /*
630 * This GPIO is typically active-low, and marked as such in
631 * device tree. dm_gpio_set_value() takes this into account
632 * and inverts the value we pass here if required. In other
633 * words, this first call logically asserts the reset signal,
634 * which typically results in driving the physical GPIO low,
635 * and the second call logically de-asserts the reset signal,
636 * which typically results in driver the GPIO high.
637 */
Simon Glass46927e12015-01-05 20:05:39 -0700638 dm_gpio_set_value(&config->phy_reset_gpio, 1);
Stephen Warren2f6a7e82016-09-15 12:19:37 -0600639 mdelay(5);
640 dm_gpio_set_value(&config->phy_reset_gpio, 0);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000641 }
642
643 /* Reset the usb controller */
644 clock_enable(config->periph_id);
645 usbf_reset_controller(config, usbctlr);
646
647 /* enable pinmux bypass */
648 setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
649 ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
650
651 /* Select ULPI parallel interface */
Stephen Warrena4539a22014-04-30 15:09:57 -0600652 init_phy_mux(config, PTS_ULPI, init);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000653
654 /* enable ULPI transceiver */
655 setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
656
657 /* configure ULPI transceiver timings */
658 val = 0;
659 writel(val, &usbctlr->ulpi_timing_ctrl_1);
660
661 val |= ULPI_DATA_TRIMMER_SEL(4);
662 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
663 val |= ULPI_DIR_TRIMMER_SEL(4);
664 writel(val, &usbctlr->ulpi_timing_ctrl_1);
665 udelay(10);
666
667 val |= ULPI_DATA_TRIMMER_LOAD;
668 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
669 val |= ULPI_DIR_TRIMMER_LOAD;
670 writel(val, &usbctlr->ulpi_timing_ctrl_1);
671
672 /* set up phy for host operation with external vbus supply */
673 ulpi_vp.port_num = 0;
674 ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
675
Simon Glass7e27bdd2015-03-25 12:22:46 -0600676 ret = ulpi_init(&ulpi_vp);
677 if (ret) {
Lucas Stach7ae18f32013-02-07 07:16:29 +0000678 printf("Tegra ULPI viewport init failed\n");
Simon Glass7e27bdd2015-03-25 12:22:46 -0600679 return ret;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000680 }
681
682 ulpi_set_vbus(&ulpi_vp, 1, 1);
683 ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
684
685 /* enable wakeup events */
686 setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
687
688 /* Enable and wait for the phy clock to become valid in 100 ms */
689 setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
690 for (loop_count = 100000; loop_count != 0; loop_count--) {
691 if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
692 break;
693 udelay(1);
694 }
695 if (!loop_count)
Simon Glass7e27bdd2015-03-25 12:22:46 -0600696 return -ETIMEDOUT;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000697 clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
698
699 return 0;
700}
701#else
Stephen Warrena4539a22014-04-30 15:09:57 -0600702static int init_ulpi_usb_controller(struct fdt_usb *config,
703 enum usb_init_type init)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000704{
705 printf("No code to set up ULPI controller, please enable"
706 "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
Simon Glass7e27bdd2015-03-25 12:22:46 -0600707 return -ENOSYS;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000708}
709#endif
710
711static void config_clock(const u32 timing[])
712{
Tom Warren7aaa5a62015-03-04 16:36:00 -0700713 debug("%s: DIVM = %d, DIVN = %d, DIVP = %d, cpcon/lfcon = %d/%d\n",
714 __func__, timing[PARAM_DIVM], timing[PARAM_DIVN],
715 timing[PARAM_DIVP], timing[PARAM_CPCON], timing[PARAM_LFCON]);
716
Lucas Stach7ae18f32013-02-07 07:16:29 +0000717 clock_start_pll(CLOCK_ID_USB,
718 timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
719 timing[PARAM_CPCON], timing[PARAM_LFCON]);
720}
721
Simon Glass4e9838c2015-08-11 08:33:29 -0600722static int fdt_decode_usb(struct udevice *dev, struct fdt_usb *config)
Lucas Stach7ae18f32013-02-07 07:16:29 +0000723{
724 const char *phy, *mode;
725
Johan Jonkera12a73b2023-03-13 01:32:04 +0100726 config->reg = dev_read_addr_ptr(dev);
Simon Glass5ae28c02017-07-25 08:30:04 -0600727 debug("reg=%p\n", config->reg);
728 mode = dev_read_string(dev, "dr_mode");
Lucas Stach7ae18f32013-02-07 07:16:29 +0000729 if (mode) {
730 if (0 == strcmp(mode, "host"))
731 config->dr_mode = DR_MODE_HOST;
732 else if (0 == strcmp(mode, "peripheral"))
733 config->dr_mode = DR_MODE_DEVICE;
734 else if (0 == strcmp(mode, "otg"))
735 config->dr_mode = DR_MODE_OTG;
736 else {
737 debug("%s: Cannot decode dr_mode '%s'\n", __func__,
738 mode);
Simon Glass7e27bdd2015-03-25 12:22:46 -0600739 return -EINVAL;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000740 }
741 } else {
742 config->dr_mode = DR_MODE_HOST;
743 }
744
Simon Glass5ae28c02017-07-25 08:30:04 -0600745 phy = dev_read_string(dev, "phy_type");
Lucas Stach7ae18f32013-02-07 07:16:29 +0000746 config->utmi = phy && 0 == strcmp("utmi", phy);
747 config->ulpi = phy && 0 == strcmp("ulpi", phy);
Simon Glass5ae28c02017-07-25 08:30:04 -0600748 config->has_legacy_mode = dev_read_bool(dev, "nvidia,has-legacy-mode");
Simon Glass000f15f2017-07-25 08:30:00 -0600749 config->periph_id = clock_decode_periph_id(dev);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000750 if (config->periph_id == PERIPH_ID_NONE) {
751 debug("%s: Missing/invalid peripheral ID\n", __func__);
Simon Glass7e27bdd2015-03-25 12:22:46 -0600752 return -EINVAL;
Lucas Stach7ae18f32013-02-07 07:16:29 +0000753 }
Simon Glass5ae28c02017-07-25 08:30:04 -0600754 gpio_request_by_name(dev, "nvidia,vbus-gpio", 0, &config->vbus_gpio,
755 GPIOD_IS_OUT);
756 gpio_request_by_name(dev, "nvidia,phy-reset-gpio", 0,
757 &config->phy_reset_gpio, GPIOD_IS_OUT);
758 debug("legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, vbus=%d, phy_reset=%d, dr_mode=%d, reg=%p\n",
759 config->has_legacy_mode, config->utmi, config->ulpi,
760 config->periph_id, gpio_get_number(&config->vbus_gpio),
761 gpio_get_number(&config->phy_reset_gpio), config->dr_mode,
762 config->reg);
Lucas Stach7ae18f32013-02-07 07:16:29 +0000763
764 return 0;
765}
766
Simon Glassddb9a502015-03-25 12:22:47 -0600767int usb_common_init(struct fdt_usb *config, enum usb_init_type init)
768{
769 int ret = 0;
770
771 switch (init) {
772 case USB_INIT_HOST:
773 switch (config->dr_mode) {
774 case DR_MODE_HOST:
775 case DR_MODE_OTG:
776 break;
777 default:
778 printf("tegrausb: Invalid dr_mode %d for host mode\n",
779 config->dr_mode);
780 return -1;
781 }
782 break;
783 case USB_INIT_DEVICE:
784 if (config->periph_id != PERIPH_ID_USBD) {
785 printf("tegrausb: Device mode only supported on first USB controller\n");
786 return -1;
787 }
788 if (!config->utmi) {
789 printf("tegrausb: Device mode only supported with UTMI PHY\n");
790 return -1;
791 }
792 switch (config->dr_mode) {
793 case DR_MODE_DEVICE:
794 case DR_MODE_OTG:
795 break;
796 default:
797 printf("tegrausb: Invalid dr_mode %d for device mode\n",
798 config->dr_mode);
799 return -1;
800 }
801 break;
802 default:
803 printf("tegrausb: Unknown USB_INIT_* %d\n", init);
804 return -1;
805 }
806
Simon Glassddb9a502015-03-25 12:22:47 -0600807 debug("%d, %d\n", config->utmi, config->ulpi);
808 if (config->utmi)
809 ret = init_utmi_usb_controller(config, init);
810 else if (config->ulpi)
811 ret = init_ulpi_usb_controller(config, init);
812 if (ret)
813 return ret;
814
815 set_up_vbus(config, init);
816
817 config->init_type = init;
818
819 return 0;
820}
821
822void usb_common_uninit(struct fdt_usb *priv)
823{
824 struct usb_ctlr *usbctlr;
825
826 usbctlr = priv->reg;
827
828 /* Stop controller */
829 writel(0, &usbctlr->usb_cmd);
830 udelay(1000);
831
832 /* Initiate controller reset */
833 writel(2, &usbctlr->usb_cmd);
834 udelay(1000);
835}
836
Simon Glassdeb85082015-03-25 12:22:27 -0600837static const struct ehci_ops tegra_ehci_ops = {
838 .set_usb_mode = tegra_ehci_set_usbmode,
839 .get_port_speed = tegra_ehci_get_port_speed,
840 .powerup_fixup = tegra_ehci_powerup_fixup,
841};
842
Simon Glassd1998a92020-12-03 16:55:21 -0700843static int ehci_usb_of_to_plat(struct udevice *dev)
Simon Glassc3980ad2015-03-25 12:22:48 -0600844{
845 struct fdt_usb *priv = dev_get_priv(dev);
846 int ret;
847
Simon Glass4e9838c2015-08-11 08:33:29 -0600848 ret = fdt_decode_usb(dev, priv);
Simon Glassc3980ad2015-03-25 12:22:48 -0600849 if (ret)
850 return ret;
851
852 priv->type = dev_get_driver_data(dev);
853
854 return 0;
855}
856
857static int ehci_usb_probe(struct udevice *dev)
858{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700859 struct usb_plat *plat = dev_get_plat(dev);
Simon Glassc3980ad2015-03-25 12:22:48 -0600860 struct fdt_usb *priv = dev_get_priv(dev);
861 struct ehci_hccr *hccr;
862 struct ehci_hcor *hcor;
863 static bool clk_done;
864 int ret;
865
866 ret = usb_common_init(priv, plat->init_type);
867 if (ret)
868 return ret;
869 hccr = (struct ehci_hccr *)&priv->reg->cap_length;
870 hcor = (struct ehci_hcor *)&priv->reg->usb_cmd;
871 if (!clk_done) {
872 config_clock(get_pll_timing(&fdt_usb_controllers[priv->type]));
873 clk_done = true;
874 }
875
876 return ehci_register(dev, hccr, hcor, &tegra_ehci_ops, 0,
877 plat->init_type);
878}
879
Simon Glassc3980ad2015-03-25 12:22:48 -0600880static const struct udevice_id ehci_usb_ids[] = {
881 { .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 },
882 { .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 },
883 { .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 },
Tom Warren7aaa5a62015-03-04 16:36:00 -0700884 { .compatible = "nvidia,tegra210-ehci", .data = USB_CTLR_T210 },
Simon Glassc3980ad2015-03-25 12:22:48 -0600885 { }
886};
887
888U_BOOT_DRIVER(usb_ehci) = {
889 .name = "ehci_tegra",
890 .id = UCLASS_USB,
891 .of_match = ehci_usb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700892 .of_to_plat = ehci_usb_of_to_plat,
Simon Glassc3980ad2015-03-25 12:22:48 -0600893 .probe = ehci_usb_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900894 .remove = ehci_deregister,
Simon Glassc3980ad2015-03-25 12:22:48 -0600895 .ops = &ehci_usb_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700896 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700897 .priv_auto = sizeof(struct fdt_usb),
Simon Glassc3980ad2015-03-25 12:22:48 -0600898 .flags = DM_FLAG_ALLOC_PRIV_DMA,
899};