ehci-tegra.c 29 KB

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