ehci-tegra.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 <asm/errno.h>
  10. #include <asm/io.h>
  11. #include <asm-generic/gpio.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch-tegra/usb.h>
  14. #include <asm/arch-tegra/clk_rst.h>
  15. #include <usb.h>
  16. #include <usb/ulpi.h>
  17. #include <libfdt.h>
  18. #include <fdtdec.h>
  19. #include "ehci.h"
  20. #define USB1_ADDR_MASK 0xFFFF0000
  21. #define HOSTPC1_DEVLC 0x84
  22. #define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3)
  23. #ifdef CONFIG_USB_ULPI
  24. #ifndef CONFIG_USB_ULPI_VIEWPORT
  25. #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
  26. define CONFIG_USB_ULPI_VIEWPORT"
  27. #endif
  28. #endif
  29. enum {
  30. USB_PORTS_MAX = 3, /* Maximum ports we allow */
  31. };
  32. /* Parameters we need for USB */
  33. enum {
  34. PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
  35. PARAM_DIVM, /* PLL INPUT DIVIDER */
  36. PARAM_DIVP, /* POST DIVIDER (2^N) */
  37. PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
  38. PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
  39. PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
  40. PARAM_STABLE_COUNT, /* PLL-U STABLE count */
  41. PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
  42. PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
  43. PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
  44. PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
  45. PARAM_COUNT
  46. };
  47. /* Possible port types (dual role mode) */
  48. enum dr_mode {
  49. DR_MODE_NONE = 0,
  50. DR_MODE_HOST, /* supports host operation */
  51. DR_MODE_DEVICE, /* supports device operation */
  52. DR_MODE_OTG, /* supports both */
  53. };
  54. /* Information about a USB port */
  55. struct fdt_usb {
  56. struct usb_ctlr *reg; /* address of registers in physical memory */
  57. unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
  58. unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
  59. unsigned enabled:1; /* 1 to enable, 0 to disable */
  60. unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
  61. unsigned initialized:1; /* has this port already been initialized? */
  62. enum usb_init_type init_type;
  63. enum dr_mode dr_mode; /* dual role mode */
  64. enum periph_id periph_id;/* peripheral id */
  65. struct fdt_gpio_state vbus_gpio; /* GPIO for vbus enable */
  66. struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
  67. };
  68. static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
  69. static unsigned port_count; /* Number of available ports */
  70. /* Port that needs to clear CSC after Port Reset */
  71. static u32 port_addr_clear_csc;
  72. /*
  73. * This table has USB timing parameters for each Oscillator frequency we
  74. * support. There are four sets of values:
  75. *
  76. * 1. PLLU configuration information (reference clock is osc/clk_m and
  77. * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
  78. *
  79. * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
  80. * ----------------------------------------------------------------------
  81. * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
  82. * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
  83. * Filter frequency (MHz) 1 4.8 6 2
  84. * CPCON 1100b 0011b 1100b 1100b
  85. * LFCON0 0 0 0 0
  86. *
  87. * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
  88. *
  89. * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
  90. * ---------------------------------------------------------------------------
  91. * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
  92. * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
  93. * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
  94. * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
  95. *
  96. * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
  97. * SessEnd. Each of these signals have their own debouncer and for each of
  98. * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
  99. * BIAS_DEBOUNCE_B).
  100. *
  101. * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
  102. * 0xffff -> No debouncing at all
  103. * <n> ms = <n> *1000 / (1/19.2MHz) / 4
  104. *
  105. * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
  106. * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
  107. *
  108. * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
  109. * values, so we can keep those to default.
  110. *
  111. * 4. The 20 microsecond delay after bias cell operation.
  112. */
  113. static const unsigned T20_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
  114. /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
  115. { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
  116. { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
  117. { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
  118. { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
  119. };
  120. static const unsigned T30_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
  121. /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
  122. { 0x3C0, 0x0D, 0x00, 0xC, 1, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 5 },
  123. { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 7 },
  124. { 0x3C0, 0x0C, 0x00, 0xC, 1, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
  125. { 0x3C0, 0x1A, 0x00, 0xC, 1, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
  126. };
  127. static const unsigned T114_usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
  128. /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
  129. { 0x3C0, 0x0D, 0x00, 0xC, 2, 0x02, 0x33, 0x09, 0x7F, 0x7EF4, 6 },
  130. { 0x0C8, 0x04, 0x00, 0x3, 2, 0x03, 0x4B, 0x0C, 0xBB, 0xBB80, 8 },
  131. { 0x3C0, 0x0C, 0x00, 0xC, 2, 0x02, 0x2F, 0x08, 0x76, 0x7530, 5 },
  132. { 0x3C0, 0x1A, 0x00, 0xC, 2, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 0xB }
  133. };
  134. /* UTMIP Idle Wait Delay */
  135. static const u8 utmip_idle_wait_delay = 17;
  136. /* UTMIP Elastic limit */
  137. static const u8 utmip_elastic_limit = 16;
  138. /* UTMIP High Speed Sync Start Delay */
  139. static const u8 utmip_hs_sync_start_delay = 9;
  140. struct fdt_usb_controller {
  141. int compat;
  142. /* flag to determine whether controller supports hostpc register */
  143. u32 has_hostpc:1;
  144. const unsigned *pll_parameter;
  145. };
  146. static struct fdt_usb_controller fdt_usb_controllers[] = {
  147. {
  148. .compat = COMPAT_NVIDIA_TEGRA20_USB,
  149. .has_hostpc = 0,
  150. .pll_parameter = (const unsigned *)T20_usb_pll,
  151. },
  152. {
  153. .compat = COMPAT_NVIDIA_TEGRA30_USB,
  154. .has_hostpc = 1,
  155. .pll_parameter = (const unsigned *)T30_usb_pll,
  156. },
  157. {
  158. .compat = COMPAT_NVIDIA_TEGRA114_USB,
  159. .has_hostpc = 1,
  160. .pll_parameter = (const unsigned *)T114_usb_pll,
  161. },
  162. };
  163. static struct fdt_usb_controller *controller;
  164. /*
  165. * A known hardware issue where Connect Status Change bit of PORTSC register
  166. * of USB1 controller will be set after Port Reset.
  167. * We have to clear it in order for later device enumeration to proceed.
  168. * This ehci_powerup_fixup overrides the weak function ehci_powerup_fixup
  169. * in "ehci-hcd.c".
  170. */
  171. void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  172. {
  173. mdelay(50);
  174. /* This is to avoid PORT_ENABLE bit to be cleared in "ehci-hcd.c". */
  175. if (controller->has_hostpc)
  176. *reg |= EHCI_PS_PE;
  177. if (((u32)status_reg & TEGRA_USB_ADDR_MASK) != port_addr_clear_csc)
  178. return;
  179. /* For EHCI_PS_CSC to be cleared in ehci_hcd.c */
  180. if (ehci_readl(status_reg) & EHCI_PS_CSC)
  181. *reg |= EHCI_PS_CSC;
  182. }
  183. /*
  184. * This ehci_set_usbmode overrides the weak function ehci_set_usbmode
  185. * in "ehci-hcd.c".
  186. */
  187. void ehci_set_usbmode(int index)
  188. {
  189. struct fdt_usb *config;
  190. struct usb_ctlr *usbctlr;
  191. uint32_t tmp;
  192. config = &port[index];
  193. usbctlr = config->reg;
  194. tmp = ehci_readl(&usbctlr->usb_mode);
  195. tmp |= USBMODE_CM_HC;
  196. ehci_writel(&usbctlr->usb_mode, tmp);
  197. }
  198. /*
  199. * This ehci_get_port_speed overrides the weak function ehci_get_port_speed
  200. * in "ehci-hcd.c".
  201. */
  202. int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
  203. {
  204. uint32_t tmp;
  205. uint32_t *reg_ptr;
  206. if (controller->has_hostpc) {
  207. reg_ptr = (uint32_t *)((u8 *)&hcor->or_usbcmd + HOSTPC1_DEVLC);
  208. tmp = ehci_readl(reg_ptr);
  209. return HOSTPC1_PSPD(tmp);
  210. } else
  211. return PORTSC_PSPD(reg);
  212. }
  213. /* Set up VBUS for host/device mode */
  214. static void set_up_vbus(struct fdt_usb *config, enum usb_init_type init)
  215. {
  216. /*
  217. * If we are an OTG port initializing in host mode,
  218. * check if remote host is driving VBus and bail out in this case.
  219. */
  220. if (init == USB_INIT_HOST &&
  221. config->dr_mode == DR_MODE_OTG &&
  222. (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)) {
  223. printf("tegrausb: VBUS input active; not enabling as host\n");
  224. return;
  225. }
  226. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  227. int vbus_value;
  228. fdtdec_setup_gpio(&config->vbus_gpio);
  229. vbus_value = (init == USB_INIT_HOST) ^
  230. !!(config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW);
  231. gpio_direction_output(config->vbus_gpio.gpio, vbus_value);
  232. debug("set_up_vbus: GPIO %d %d\n", config->vbus_gpio.gpio,
  233. vbus_value);
  234. }
  235. }
  236. void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
  237. {
  238. /* Reset the USB controller with 2us delay */
  239. reset_periph(config->periph_id, 2);
  240. /*
  241. * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
  242. * base address
  243. */
  244. if (config->has_legacy_mode)
  245. setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
  246. /* Put UTMIP1/3 in reset */
  247. setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  248. /* Enable the UTMIP PHY */
  249. if (config->utmi)
  250. setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
  251. }
  252. static const unsigned *get_pll_timing(void)
  253. {
  254. const unsigned *timing;
  255. timing = controller->pll_parameter +
  256. clock_get_osc_freq() * PARAM_COUNT;
  257. return timing;
  258. }
  259. /* select the PHY to use with a USB controller */
  260. static void init_phy_mux(struct fdt_usb *config, uint pts,
  261. enum usb_init_type init)
  262. {
  263. struct usb_ctlr *usbctlr = config->reg;
  264. #if defined(CONFIG_TEGRA20)
  265. if (config->periph_id == PERIPH_ID_USBD) {
  266. clrsetbits_le32(&usbctlr->port_sc1, PTS1_MASK,
  267. PTS_UTMI << PTS1_SHIFT);
  268. clrbits_le32(&usbctlr->port_sc1, STS1);
  269. } else {
  270. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
  271. PTS_UTMI << PTS_SHIFT);
  272. clrbits_le32(&usbctlr->port_sc1, STS);
  273. }
  274. #else
  275. /* Set to Host mode (if applicable) after Controller Reset was done */
  276. clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
  277. (init == USB_INIT_HOST) ? USBMODE_CM_HC : 0);
  278. /*
  279. * Select PHY interface after setting host mode.
  280. * For device mode, the ordering requirement is not an issue, since
  281. * only the first USB controller supports device mode, and that USB
  282. * controller can only talk to a UTMI PHY, so the PHY selection is
  283. * already made at reset time, so this write is a no-op.
  284. */
  285. clrsetbits_le32(&usbctlr->hostpc1_devlc, PTS_MASK,
  286. pts << PTS_SHIFT);
  287. clrbits_le32(&usbctlr->hostpc1_devlc, STS);
  288. #endif
  289. }
  290. /* set up the UTMI USB controller with the parameters provided */
  291. static int init_utmi_usb_controller(struct fdt_usb *config,
  292. enum usb_init_type init)
  293. {
  294. u32 b_sess_valid_mask, val;
  295. int loop_count;
  296. const unsigned *timing;
  297. struct usb_ctlr *usbctlr = config->reg;
  298. struct clk_rst_ctlr *clkrst;
  299. struct usb_ctlr *usb1ctlr;
  300. clock_enable(config->periph_id);
  301. /* Reset the usb controller */
  302. usbf_reset_controller(config, usbctlr);
  303. /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
  304. clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  305. /* Follow the crystal clock disable by >100ns delay */
  306. udelay(1);
  307. b_sess_valid_mask = (VBUS_B_SESS_VLD_SW_VALUE | VBUS_B_SESS_VLD_SW_EN);
  308. clrsetbits_le32(&usbctlr->phy_vbus_sensors, b_sess_valid_mask,
  309. (init == USB_INIT_DEVICE) ? b_sess_valid_mask : 0);
  310. /*
  311. * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
  312. * mux must be switched to actually use a_sess_vld threshold.
  313. */
  314. if (config->dr_mode == DR_MODE_OTG &&
  315. fdt_gpio_isvalid(&config->vbus_gpio))
  316. clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
  317. VBUS_SENSE_CTL_MASK,
  318. VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
  319. /*
  320. * PLL Delay CONFIGURATION settings. The following parameters control
  321. * the bring up of the plls.
  322. */
  323. timing = get_pll_timing();
  324. if (!controller->has_hostpc) {
  325. val = readl(&usbctlr->utmip_misc_cfg1);
  326. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  327. timing[PARAM_STABLE_COUNT] <<
  328. UTMIP_PLLU_STABLE_COUNT_SHIFT);
  329. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  330. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  331. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  332. writel(val, &usbctlr->utmip_misc_cfg1);
  333. /* Set PLL enable delay count and crystal frequency count */
  334. val = readl(&usbctlr->utmip_pll_cfg1);
  335. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  336. timing[PARAM_ENABLE_DELAY_COUNT] <<
  337. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  338. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  339. timing[PARAM_XTAL_FREQ_COUNT] <<
  340. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  341. writel(val, &usbctlr->utmip_pll_cfg1);
  342. } else {
  343. clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  344. val = readl(&clkrst->crc_utmip_pll_cfg2);
  345. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  346. timing[PARAM_STABLE_COUNT] <<
  347. UTMIP_PLLU_STABLE_COUNT_SHIFT);
  348. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  349. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  350. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  351. writel(val, &clkrst->crc_utmip_pll_cfg2);
  352. /* Set PLL enable delay count and crystal frequency count */
  353. val = readl(&clkrst->crc_utmip_pll_cfg1);
  354. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  355. timing[PARAM_ENABLE_DELAY_COUNT] <<
  356. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  357. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  358. timing[PARAM_XTAL_FREQ_COUNT] <<
  359. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  360. writel(val, &clkrst->crc_utmip_pll_cfg1);
  361. /* Disable Power Down state for PLL */
  362. clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
  363. PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
  364. PLL_ACTIVE_POWERDOWN);
  365. /* Recommended PHY settings for EYE diagram */
  366. val = readl(&usbctlr->utmip_xcvr_cfg0);
  367. clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
  368. 0x4 << UTMIP_XCVR_SETUP_SHIFT);
  369. clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
  370. 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
  371. clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
  372. 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
  373. writel(val, &usbctlr->utmip_xcvr_cfg0);
  374. clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
  375. UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
  376. 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
  377. /* Some registers can be controlled from USB1 only. */
  378. if (config->periph_id != PERIPH_ID_USBD) {
  379. clock_enable(PERIPH_ID_USBD);
  380. /* Disable Reset if in Reset state */
  381. reset_set_enable(PERIPH_ID_USBD, 0);
  382. }
  383. usb1ctlr = (struct usb_ctlr *)
  384. ((u32)config->reg & USB1_ADDR_MASK);
  385. val = readl(&usb1ctlr->utmip_bias_cfg0);
  386. setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
  387. clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
  388. 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
  389. clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
  390. 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
  391. writel(val, &usb1ctlr->utmip_bias_cfg0);
  392. /* Miscellaneous setting mentioned in Programming Guide */
  393. clrbits_le32(&usbctlr->utmip_misc_cfg0,
  394. UTMIP_SUSPEND_EXIT_ON_EDGE);
  395. }
  396. /* Setting the tracking length time */
  397. clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
  398. UTMIP_BIAS_PDTRK_COUNT_MASK,
  399. timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
  400. /* Program debounce time for VBUS to become valid */
  401. clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
  402. UTMIP_DEBOUNCE_CFG0_MASK,
  403. timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
  404. setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
  405. /* Disable battery charge enabling bit */
  406. setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
  407. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
  408. setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
  409. /*
  410. * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
  411. * Setting these fields, together with default values of the
  412. * other fields, results in programming the registers below as
  413. * follows:
  414. * UTMIP_HSRX_CFG0 = 0x9168c000
  415. * UTMIP_HSRX_CFG1 = 0x13
  416. */
  417. /* Set PLL enable delay count and Crystal frequency count */
  418. val = readl(&usbctlr->utmip_hsrx_cfg0);
  419. clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
  420. utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
  421. clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
  422. utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
  423. writel(val, &usbctlr->utmip_hsrx_cfg0);
  424. /* Configure the UTMIP_HS_SYNC_START_DLY */
  425. clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
  426. UTMIP_HS_SYNC_START_DLY_MASK,
  427. utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
  428. /* Preceed the crystal clock disable by >100ns delay. */
  429. udelay(1);
  430. /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
  431. setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  432. if (controller->has_hostpc) {
  433. if (config->periph_id == PERIPH_ID_USBD)
  434. clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
  435. UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
  436. if (config->periph_id == PERIPH_ID_USB2)
  437. clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
  438. UTMIP_FORCE_PD_SAMP_B_POWERDOWN);
  439. if (config->periph_id == PERIPH_ID_USB3)
  440. clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
  441. UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
  442. }
  443. /* Finished the per-controller init. */
  444. /* De-assert UTMIP_RESET to bring out of reset. */
  445. clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  446. /* Wait for the phy clock to become valid in 100 ms */
  447. for (loop_count = 100000; loop_count != 0; loop_count--) {
  448. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  449. break;
  450. udelay(1);
  451. }
  452. if (!loop_count)
  453. return -1;
  454. /* Disable ICUSB FS/LS transceiver */
  455. clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
  456. /* Select UTMI parallel interface */
  457. init_phy_mux(config, PTS_UTMI, init);
  458. /* Deassert power down state */
  459. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
  460. UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
  461. clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
  462. UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
  463. if (controller->has_hostpc) {
  464. /*
  465. * BIAS Pad Power Down is common among all 3 USB
  466. * controllers and can be controlled from USB1 only.
  467. */
  468. usb1ctlr = (struct usb_ctlr *)
  469. ((u32)config->reg & USB1_ADDR_MASK);
  470. clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
  471. udelay(25);
  472. clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
  473. UTMIP_FORCE_PDTRK_POWERDOWN);
  474. }
  475. return 0;
  476. }
  477. #ifdef CONFIG_USB_ULPI
  478. /* if board file does not set a ULPI reference frequency we default to 24MHz */
  479. #ifndef CONFIG_ULPI_REF_CLK
  480. #define CONFIG_ULPI_REF_CLK 24000000
  481. #endif
  482. /* set up the ULPI USB controller with the parameters provided */
  483. static int init_ulpi_usb_controller(struct fdt_usb *config,
  484. enum usb_init_type init)
  485. {
  486. u32 val;
  487. int loop_count;
  488. struct ulpi_viewport ulpi_vp;
  489. struct usb_ctlr *usbctlr = config->reg;
  490. /* set up ULPI reference clock on pllp_out4 */
  491. clock_enable(PERIPH_ID_DEV2_OUT);
  492. clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
  493. /* reset ULPI phy */
  494. if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
  495. fdtdec_setup_gpio(&config->phy_reset_gpio);
  496. gpio_direction_output(config->phy_reset_gpio.gpio, 0);
  497. mdelay(5);
  498. gpio_set_value(config->phy_reset_gpio.gpio, 1);
  499. }
  500. /* Reset the usb controller */
  501. clock_enable(config->periph_id);
  502. usbf_reset_controller(config, usbctlr);
  503. /* enable pinmux bypass */
  504. setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
  505. ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
  506. /* Select ULPI parallel interface */
  507. init_phy_mux(config, PTS_ULPI, init);
  508. /* enable ULPI transceiver */
  509. setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
  510. /* configure ULPI transceiver timings */
  511. val = 0;
  512. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  513. val |= ULPI_DATA_TRIMMER_SEL(4);
  514. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  515. val |= ULPI_DIR_TRIMMER_SEL(4);
  516. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  517. udelay(10);
  518. val |= ULPI_DATA_TRIMMER_LOAD;
  519. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  520. val |= ULPI_DIR_TRIMMER_LOAD;
  521. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  522. /* set up phy for host operation with external vbus supply */
  523. ulpi_vp.port_num = 0;
  524. ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
  525. if (ulpi_init(&ulpi_vp)) {
  526. printf("Tegra ULPI viewport init failed\n");
  527. return -1;
  528. }
  529. ulpi_set_vbus(&ulpi_vp, 1, 1);
  530. ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
  531. /* enable wakeup events */
  532. setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
  533. /* Enable and wait for the phy clock to become valid in 100 ms */
  534. setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  535. for (loop_count = 100000; loop_count != 0; loop_count--) {
  536. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  537. break;
  538. udelay(1);
  539. }
  540. if (!loop_count)
  541. return -1;
  542. clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  543. return 0;
  544. }
  545. #else
  546. static int init_ulpi_usb_controller(struct fdt_usb *config,
  547. enum usb_init_type init)
  548. {
  549. printf("No code to set up ULPI controller, please enable"
  550. "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
  551. return -1;
  552. }
  553. #endif
  554. static void config_clock(const u32 timing[])
  555. {
  556. clock_start_pll(CLOCK_ID_USB,
  557. timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
  558. timing[PARAM_CPCON], timing[PARAM_LFCON]);
  559. }
  560. static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
  561. {
  562. const char *phy, *mode;
  563. config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
  564. mode = fdt_getprop(blob, node, "dr_mode", NULL);
  565. if (mode) {
  566. if (0 == strcmp(mode, "host"))
  567. config->dr_mode = DR_MODE_HOST;
  568. else if (0 == strcmp(mode, "peripheral"))
  569. config->dr_mode = DR_MODE_DEVICE;
  570. else if (0 == strcmp(mode, "otg"))
  571. config->dr_mode = DR_MODE_OTG;
  572. else {
  573. debug("%s: Cannot decode dr_mode '%s'\n", __func__,
  574. mode);
  575. return -FDT_ERR_NOTFOUND;
  576. }
  577. } else {
  578. config->dr_mode = DR_MODE_HOST;
  579. }
  580. phy = fdt_getprop(blob, node, "phy_type", NULL);
  581. config->utmi = phy && 0 == strcmp("utmi", phy);
  582. config->ulpi = phy && 0 == strcmp("ulpi", phy);
  583. config->enabled = fdtdec_get_is_enabled(blob, node);
  584. config->has_legacy_mode = fdtdec_get_bool(blob, node,
  585. "nvidia,has-legacy-mode");
  586. if (config->has_legacy_mode)
  587. port_addr_clear_csc = (u32) config->reg;
  588. config->periph_id = clock_decode_periph_id(blob, node);
  589. if (config->periph_id == PERIPH_ID_NONE) {
  590. debug("%s: Missing/invalid peripheral ID\n", __func__);
  591. return -FDT_ERR_NOTFOUND;
  592. }
  593. fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
  594. fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
  595. &config->phy_reset_gpio);
  596. debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
  597. "vbus=%d, phy_reset=%d, dr_mode=%d\n",
  598. config->enabled, config->has_legacy_mode, config->utmi,
  599. config->ulpi, config->periph_id, config->vbus_gpio.gpio,
  600. config->phy_reset_gpio.gpio, config->dr_mode);
  601. return 0;
  602. }
  603. /*
  604. * process_usb_nodes() - Process a list of USB nodes, adding them to our list
  605. * of USB ports.
  606. * @blob: fdt blob
  607. * @node_list: list of nodes to process (any <=0 are ignored)
  608. * @count: number of nodes to process
  609. *
  610. * Return: 0 - ok, -1 - error
  611. */
  612. static int process_usb_nodes(const void *blob, int node_list[], int count)
  613. {
  614. struct fdt_usb config;
  615. int node, i;
  616. int clk_done = 0;
  617. port_count = 0;
  618. for (i = 0; i < count; i++) {
  619. if (port_count == USB_PORTS_MAX) {
  620. printf("tegrausb: Cannot register more than %d ports\n",
  621. USB_PORTS_MAX);
  622. return -1;
  623. }
  624. debug("USB %d: ", i);
  625. node = node_list[i];
  626. if (!node)
  627. continue;
  628. if (fdt_decode_usb(blob, node, &config)) {
  629. debug("Cannot decode USB node %s\n",
  630. fdt_get_name(blob, node, NULL));
  631. return -1;
  632. }
  633. if (!clk_done) {
  634. config_clock(get_pll_timing());
  635. clk_done = 1;
  636. }
  637. config.initialized = 0;
  638. /* add new USB port to the list of available ports */
  639. port[port_count++] = config;
  640. }
  641. return 0;
  642. }
  643. int usb_process_devicetree(const void *blob)
  644. {
  645. int node_list[USB_PORTS_MAX];
  646. int count, err = 0;
  647. int i;
  648. for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
  649. controller = &fdt_usb_controllers[i];
  650. count = fdtdec_find_aliases_for_id(blob, "usb",
  651. controller->compat, node_list, USB_PORTS_MAX);
  652. if (count) {
  653. err = process_usb_nodes(blob, node_list, count);
  654. if (err)
  655. printf("%s: Error processing USB node!\n",
  656. __func__);
  657. return err;
  658. }
  659. }
  660. if (i == ARRAY_SIZE(fdt_usb_controllers))
  661. controller = NULL;
  662. return err;
  663. }
  664. /**
  665. * Start up the given port number (ports are numbered from 0 on each board).
  666. * This returns values for the appropriate hccr and hcor addresses to use for
  667. * USB EHCI operations.
  668. *
  669. * @param index port number to start
  670. * @param hccr returns start address of EHCI HCCR registers
  671. * @param hcor returns start address of EHCI HCOR registers
  672. * @return 0 if ok, -1 on error (generally invalid port number)
  673. */
  674. int ehci_hcd_init(int index, enum usb_init_type init,
  675. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  676. {
  677. struct fdt_usb *config;
  678. struct usb_ctlr *usbctlr;
  679. if (index >= port_count)
  680. return -1;
  681. config = &port[index];
  682. switch (init) {
  683. case USB_INIT_HOST:
  684. switch (config->dr_mode) {
  685. case DR_MODE_HOST:
  686. case DR_MODE_OTG:
  687. break;
  688. default:
  689. printf("tegrausb: Invalid dr_mode %d for host mode\n",
  690. config->dr_mode);
  691. return -1;
  692. }
  693. break;
  694. case USB_INIT_DEVICE:
  695. if (config->periph_id != PERIPH_ID_USBD) {
  696. printf("tegrausb: Device mode only supported on first USB controller\n");
  697. return -1;
  698. }
  699. if (!config->utmi) {
  700. printf("tegrausb: Device mode only supported with UTMI PHY\n");
  701. return -1;
  702. }
  703. switch (config->dr_mode) {
  704. case DR_MODE_DEVICE:
  705. case DR_MODE_OTG:
  706. break;
  707. default:
  708. printf("tegrausb: Invalid dr_mode %d for device mode\n",
  709. config->dr_mode);
  710. return -1;
  711. }
  712. break;
  713. default:
  714. printf("tegrausb: Unknown USB_INIT_* %d\n", init);
  715. return -1;
  716. }
  717. /* skip init, if the port is already initialized */
  718. if (config->initialized && config->init_type == init)
  719. goto success;
  720. if (config->utmi && init_utmi_usb_controller(config, init)) {
  721. printf("tegrausb: Cannot init port %d\n", index);
  722. return -1;
  723. }
  724. if (config->ulpi && init_ulpi_usb_controller(config, init)) {
  725. printf("tegrausb: Cannot init port %d\n", index);
  726. return -1;
  727. }
  728. set_up_vbus(config, init);
  729. config->initialized = 1;
  730. config->init_type = init;
  731. success:
  732. usbctlr = config->reg;
  733. *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
  734. *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
  735. return 0;
  736. }
  737. /*
  738. * Bring down the specified USB controller
  739. */
  740. int ehci_hcd_stop(int index)
  741. {
  742. struct usb_ctlr *usbctlr;
  743. usbctlr = port[index].reg;
  744. /* Stop controller */
  745. writel(0, &usbctlr->usb_cmd);
  746. udelay(1000);
  747. /* Initiate controller reset */
  748. writel(2, &usbctlr->usb_cmd);
  749. udelay(1000);
  750. port[index].initialized = 0;
  751. return 0;
  752. }