ehci-tegra.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 <asm/arch/usb.h>
  16. #include <usb.h>
  17. #include <usb/ulpi.h>
  18. #include <libfdt.h>
  19. #include <fdtdec.h>
  20. #include "ehci.h"
  21. #define USB1_ADDR_MASK 0xFFFF0000
  22. #define HOSTPC1_DEVLC 0x84
  23. #define HOSTPC1_PSPD(x) (((x) >> 25) & 0x3)
  24. #ifdef CONFIG_USB_ULPI
  25. #ifndef CONFIG_USB_ULPI_VIEWPORT
  26. #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
  27. define CONFIG_USB_ULPI_VIEWPORT"
  28. #endif
  29. #endif
  30. enum {
  31. USB_PORTS_MAX = 3, /* Maximum ports we allow */
  32. };
  33. /* Parameters we need for USB */
  34. enum {
  35. PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
  36. PARAM_DIVM, /* PLL INPUT DIVIDER */
  37. PARAM_DIVP, /* POST DIVIDER (2^N) */
  38. PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
  39. PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
  40. PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
  41. PARAM_STABLE_COUNT, /* PLL-U STABLE count */
  42. PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
  43. PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
  44. PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
  45. PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
  46. PARAM_COUNT
  47. };
  48. /* Possible port types (dual role mode) */
  49. enum dr_mode {
  50. DR_MODE_NONE = 0,
  51. DR_MODE_HOST, /* supports host operation */
  52. DR_MODE_DEVICE, /* supports device operation */
  53. DR_MODE_OTG, /* supports both */
  54. };
  55. /* Information about a USB port */
  56. struct fdt_usb {
  57. struct usb_ctlr *reg; /* address of registers in physical memory */
  58. unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
  59. unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
  60. unsigned enabled:1; /* 1 to enable, 0 to disable */
  61. unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
  62. unsigned initialized:1; /* has this port already been initialized? */
  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. /* Put the port into host mode */
  214. static void set_host_mode(struct fdt_usb *config)
  215. {
  216. /*
  217. * If we are an OTG port, check if remote host is driving VBus and
  218. * bail out in this case.
  219. */
  220. if (config->dr_mode == DR_MODE_OTG &&
  221. (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
  222. return;
  223. /*
  224. * If not driving, we set the GPIO to enable VBUS. We assume
  225. * that the pinmux is set up correctly for this.
  226. */
  227. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  228. fdtdec_setup_gpio(&config->vbus_gpio);
  229. gpio_direction_output(config->vbus_gpio.gpio,
  230. (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
  231. 0 : 1);
  232. debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
  233. (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
  234. "low" : "high");
  235. }
  236. }
  237. void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
  238. {
  239. /* Reset the USB controller with 2us delay */
  240. reset_periph(config->periph_id, 2);
  241. /*
  242. * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
  243. * base address
  244. */
  245. if (config->has_legacy_mode)
  246. setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
  247. /* Put UTMIP1/3 in reset */
  248. setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  249. /* Enable the UTMIP PHY */
  250. if (config->utmi)
  251. setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
  252. }
  253. static const unsigned *get_pll_timing(void)
  254. {
  255. const unsigned *timing;
  256. timing = controller->pll_parameter +
  257. clock_get_osc_freq() * PARAM_COUNT;
  258. return timing;
  259. }
  260. /* set up the UTMI USB controller with the parameters provided */
  261. static int init_utmi_usb_controller(struct fdt_usb *config)
  262. {
  263. u32 val;
  264. int loop_count;
  265. const unsigned *timing;
  266. struct usb_ctlr *usbctlr = config->reg;
  267. struct clk_rst_ctlr *clkrst;
  268. struct usb_ctlr *usb1ctlr;
  269. clock_enable(config->periph_id);
  270. /* Reset the usb controller */
  271. usbf_reset_controller(config, usbctlr);
  272. /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
  273. clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  274. /* Follow the crystal clock disable by >100ns delay */
  275. udelay(1);
  276. /*
  277. * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
  278. * mux must be switched to actually use a_sess_vld threshold.
  279. */
  280. if (config->dr_mode == DR_MODE_OTG &&
  281. fdt_gpio_isvalid(&config->vbus_gpio))
  282. clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
  283. VBUS_SENSE_CTL_MASK,
  284. VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
  285. /*
  286. * PLL Delay CONFIGURATION settings. The following parameters control
  287. * the bring up of the plls.
  288. */
  289. timing = get_pll_timing();
  290. if (!controller->has_hostpc) {
  291. val = readl(&usbctlr->utmip_misc_cfg1);
  292. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  293. timing[PARAM_STABLE_COUNT] <<
  294. UTMIP_PLLU_STABLE_COUNT_SHIFT);
  295. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  296. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  297. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  298. writel(val, &usbctlr->utmip_misc_cfg1);
  299. /* Set PLL enable delay count and crystal frequency count */
  300. val = readl(&usbctlr->utmip_pll_cfg1);
  301. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  302. timing[PARAM_ENABLE_DELAY_COUNT] <<
  303. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  304. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  305. timing[PARAM_XTAL_FREQ_COUNT] <<
  306. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  307. writel(val, &usbctlr->utmip_pll_cfg1);
  308. } else {
  309. clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  310. val = readl(&clkrst->crc_utmip_pll_cfg2);
  311. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  312. timing[PARAM_STABLE_COUNT] <<
  313. UTMIP_PLLU_STABLE_COUNT_SHIFT);
  314. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  315. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  316. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  317. writel(val, &clkrst->crc_utmip_pll_cfg2);
  318. /* Set PLL enable delay count and crystal frequency count */
  319. val = readl(&clkrst->crc_utmip_pll_cfg1);
  320. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  321. timing[PARAM_ENABLE_DELAY_COUNT] <<
  322. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  323. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  324. timing[PARAM_XTAL_FREQ_COUNT] <<
  325. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  326. writel(val, &clkrst->crc_utmip_pll_cfg1);
  327. /* Disable Power Down state for PLL */
  328. clrbits_le32(&clkrst->crc_utmip_pll_cfg1,
  329. PLLU_POWERDOWN | PLL_ENABLE_POWERDOWN |
  330. PLL_ACTIVE_POWERDOWN);
  331. /* Recommended PHY settings for EYE diagram */
  332. val = readl(&usbctlr->utmip_xcvr_cfg0);
  333. clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MASK,
  334. 0x4 << UTMIP_XCVR_SETUP_SHIFT);
  335. clrsetbits_le32(&val, UTMIP_XCVR_SETUP_MSB_MASK,
  336. 0x3 << UTMIP_XCVR_SETUP_MSB_SHIFT);
  337. clrsetbits_le32(&val, UTMIP_XCVR_HSSLEW_MSB_MASK,
  338. 0x8 << UTMIP_XCVR_HSSLEW_MSB_SHIFT);
  339. writel(val, &usbctlr->utmip_xcvr_cfg0);
  340. clrsetbits_le32(&usbctlr->utmip_xcvr_cfg1,
  341. UTMIP_XCVR_TERM_RANGE_ADJ_MASK,
  342. 0x7 << UTMIP_XCVR_TERM_RANGE_ADJ_SHIFT);
  343. /* Some registers can be controlled from USB1 only. */
  344. if (config->periph_id != PERIPH_ID_USBD) {
  345. clock_enable(PERIPH_ID_USBD);
  346. /* Disable Reset if in Reset state */
  347. reset_set_enable(PERIPH_ID_USBD, 0);
  348. }
  349. usb1ctlr = (struct usb_ctlr *)
  350. ((u32)config->reg & USB1_ADDR_MASK);
  351. val = readl(&usb1ctlr->utmip_bias_cfg0);
  352. setbits_le32(&val, UTMIP_HSDISCON_LEVEL_MSB);
  353. clrsetbits_le32(&val, UTMIP_HSDISCON_LEVEL_MASK,
  354. 0x1 << UTMIP_HSDISCON_LEVEL_SHIFT);
  355. clrsetbits_le32(&val, UTMIP_HSSQUELCH_LEVEL_MASK,
  356. 0x2 << UTMIP_HSSQUELCH_LEVEL_SHIFT);
  357. writel(val, &usb1ctlr->utmip_bias_cfg0);
  358. /* Miscellaneous setting mentioned in Programming Guide */
  359. clrbits_le32(&usbctlr->utmip_misc_cfg0,
  360. UTMIP_SUSPEND_EXIT_ON_EDGE);
  361. }
  362. /* Setting the tracking length time */
  363. clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
  364. UTMIP_BIAS_PDTRK_COUNT_MASK,
  365. timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
  366. /* Program debounce time for VBUS to become valid */
  367. clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
  368. UTMIP_DEBOUNCE_CFG0_MASK,
  369. timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
  370. setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
  371. /* Disable battery charge enabling bit */
  372. setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
  373. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
  374. setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
  375. /*
  376. * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
  377. * Setting these fields, together with default values of the
  378. * other fields, results in programming the registers below as
  379. * follows:
  380. * UTMIP_HSRX_CFG0 = 0x9168c000
  381. * UTMIP_HSRX_CFG1 = 0x13
  382. */
  383. /* Set PLL enable delay count and Crystal frequency count */
  384. val = readl(&usbctlr->utmip_hsrx_cfg0);
  385. clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
  386. utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
  387. clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
  388. utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
  389. writel(val, &usbctlr->utmip_hsrx_cfg0);
  390. /* Configure the UTMIP_HS_SYNC_START_DLY */
  391. clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
  392. UTMIP_HS_SYNC_START_DLY_MASK,
  393. utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
  394. /* Preceed the crystal clock disable by >100ns delay. */
  395. udelay(1);
  396. /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
  397. setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  398. if (controller->has_hostpc) {
  399. if (config->periph_id == PERIPH_ID_USBD)
  400. clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
  401. UTMIP_FORCE_PD_SAMP_A_POWERDOWN);
  402. if (config->periph_id == PERIPH_ID_USB3)
  403. clrbits_le32(&clkrst->crc_utmip_pll_cfg2,
  404. UTMIP_FORCE_PD_SAMP_C_POWERDOWN);
  405. }
  406. /* Finished the per-controller init. */
  407. /* De-assert UTMIP_RESET to bring out of reset. */
  408. clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  409. /* Wait for the phy clock to become valid in 100 ms */
  410. for (loop_count = 100000; loop_count != 0; loop_count--) {
  411. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  412. break;
  413. udelay(1);
  414. }
  415. if (!loop_count)
  416. return -1;
  417. /* Disable ICUSB FS/LS transceiver */
  418. clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
  419. /* Select UTMI parallel interface */
  420. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
  421. PTS_UTMI << PTS_SHIFT);
  422. clrbits_le32(&usbctlr->port_sc1, STS);
  423. /* Deassert power down state */
  424. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
  425. UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
  426. clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
  427. UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
  428. if (controller->has_hostpc) {
  429. /*
  430. * BIAS Pad Power Down is common among all 3 USB
  431. * controllers and can be controlled from USB1 only.
  432. */
  433. usb1ctlr = (struct usb_ctlr *)
  434. ((u32)config->reg & USB1_ADDR_MASK);
  435. clrbits_le32(&usb1ctlr->utmip_bias_cfg0, UTMIP_BIASPD);
  436. udelay(25);
  437. clrbits_le32(&usb1ctlr->utmip_bias_cfg1,
  438. UTMIP_FORCE_PDTRK_POWERDOWN);
  439. }
  440. return 0;
  441. }
  442. #ifdef CONFIG_USB_ULPI
  443. /* if board file does not set a ULPI reference frequency we default to 24MHz */
  444. #ifndef CONFIG_ULPI_REF_CLK
  445. #define CONFIG_ULPI_REF_CLK 24000000
  446. #endif
  447. /* set up the ULPI USB controller with the parameters provided */
  448. static int init_ulpi_usb_controller(struct fdt_usb *config)
  449. {
  450. u32 val;
  451. int loop_count;
  452. struct ulpi_viewport ulpi_vp;
  453. struct usb_ctlr *usbctlr = config->reg;
  454. /* set up ULPI reference clock on pllp_out4 */
  455. clock_enable(PERIPH_ID_DEV2_OUT);
  456. clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
  457. /* reset ULPI phy */
  458. if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
  459. fdtdec_setup_gpio(&config->phy_reset_gpio);
  460. gpio_direction_output(config->phy_reset_gpio.gpio, 0);
  461. mdelay(5);
  462. gpio_set_value(config->phy_reset_gpio.gpio, 1);
  463. }
  464. /* Reset the usb controller */
  465. clock_enable(config->periph_id);
  466. usbf_reset_controller(config, usbctlr);
  467. /* enable pinmux bypass */
  468. setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
  469. ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
  470. /* Select ULPI parallel interface */
  471. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
  472. /* enable ULPI transceiver */
  473. setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
  474. /* configure ULPI transceiver timings */
  475. val = 0;
  476. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  477. val |= ULPI_DATA_TRIMMER_SEL(4);
  478. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  479. val |= ULPI_DIR_TRIMMER_SEL(4);
  480. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  481. udelay(10);
  482. val |= ULPI_DATA_TRIMMER_LOAD;
  483. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  484. val |= ULPI_DIR_TRIMMER_LOAD;
  485. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  486. /* set up phy for host operation with external vbus supply */
  487. ulpi_vp.port_num = 0;
  488. ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
  489. if (ulpi_init(&ulpi_vp)) {
  490. printf("Tegra ULPI viewport init failed\n");
  491. return -1;
  492. }
  493. ulpi_set_vbus(&ulpi_vp, 1, 1);
  494. ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
  495. /* enable wakeup events */
  496. setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
  497. /* Enable and wait for the phy clock to become valid in 100 ms */
  498. setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  499. for (loop_count = 100000; loop_count != 0; loop_count--) {
  500. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  501. break;
  502. udelay(1);
  503. }
  504. if (!loop_count)
  505. return -1;
  506. clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  507. return 0;
  508. }
  509. #else
  510. static int init_ulpi_usb_controller(struct fdt_usb *config)
  511. {
  512. printf("No code to set up ULPI controller, please enable"
  513. "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
  514. return -1;
  515. }
  516. #endif
  517. static void config_clock(const u32 timing[])
  518. {
  519. clock_start_pll(CLOCK_ID_USB,
  520. timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
  521. timing[PARAM_CPCON], timing[PARAM_LFCON]);
  522. }
  523. static int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
  524. {
  525. const char *phy, *mode;
  526. config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
  527. mode = fdt_getprop(blob, node, "dr_mode", NULL);
  528. if (mode) {
  529. if (0 == strcmp(mode, "host"))
  530. config->dr_mode = DR_MODE_HOST;
  531. else if (0 == strcmp(mode, "peripheral"))
  532. config->dr_mode = DR_MODE_DEVICE;
  533. else if (0 == strcmp(mode, "otg"))
  534. config->dr_mode = DR_MODE_OTG;
  535. else {
  536. debug("%s: Cannot decode dr_mode '%s'\n", __func__,
  537. mode);
  538. return -FDT_ERR_NOTFOUND;
  539. }
  540. } else {
  541. config->dr_mode = DR_MODE_HOST;
  542. }
  543. phy = fdt_getprop(blob, node, "phy_type", NULL);
  544. config->utmi = phy && 0 == strcmp("utmi", phy);
  545. config->ulpi = phy && 0 == strcmp("ulpi", phy);
  546. config->enabled = fdtdec_get_is_enabled(blob, node);
  547. config->has_legacy_mode = fdtdec_get_bool(blob, node,
  548. "nvidia,has-legacy-mode");
  549. if (config->has_legacy_mode)
  550. port_addr_clear_csc = (u32) config->reg;
  551. config->periph_id = clock_decode_periph_id(blob, node);
  552. if (config->periph_id == PERIPH_ID_NONE) {
  553. debug("%s: Missing/invalid peripheral ID\n", __func__);
  554. return -FDT_ERR_NOTFOUND;
  555. }
  556. fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
  557. fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
  558. &config->phy_reset_gpio);
  559. debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
  560. "vbus=%d, phy_reset=%d, dr_mode=%d\n",
  561. config->enabled, config->has_legacy_mode, config->utmi,
  562. config->ulpi, config->periph_id, config->vbus_gpio.gpio,
  563. config->phy_reset_gpio.gpio, config->dr_mode);
  564. return 0;
  565. }
  566. /*
  567. * process_usb_nodes() - Process a list of USB nodes, adding them to our list
  568. * of USB ports.
  569. * @blob: fdt blob
  570. * @node_list: list of nodes to process (any <=0 are ignored)
  571. * @count: number of nodes to process
  572. *
  573. * Return: 0 - ok, -1 - error
  574. */
  575. static int process_usb_nodes(const void *blob, int node_list[], int count)
  576. {
  577. struct fdt_usb config;
  578. int node, i;
  579. int clk_done = 0;
  580. port_count = 0;
  581. for (i = 0; i < count; i++) {
  582. if (port_count == USB_PORTS_MAX) {
  583. printf("tegrausb: Cannot register more than %d ports\n",
  584. USB_PORTS_MAX);
  585. return -1;
  586. }
  587. debug("USB %d: ", i);
  588. node = node_list[i];
  589. if (!node)
  590. continue;
  591. if (fdt_decode_usb(blob, node, &config)) {
  592. debug("Cannot decode USB node %s\n",
  593. fdt_get_name(blob, node, NULL));
  594. return -1;
  595. }
  596. if (!clk_done) {
  597. config_clock(get_pll_timing());
  598. clk_done = 1;
  599. }
  600. config.initialized = 0;
  601. /* add new USB port to the list of available ports */
  602. port[port_count++] = config;
  603. }
  604. return 0;
  605. }
  606. int board_usb_init(const void *blob)
  607. {
  608. int node_list[USB_PORTS_MAX];
  609. int count, err = 0;
  610. int i;
  611. for (i = 0; i < ARRAY_SIZE(fdt_usb_controllers); i++) {
  612. controller = &fdt_usb_controllers[i];
  613. count = fdtdec_find_aliases_for_id(blob, "usb",
  614. controller->compat, node_list, USB_PORTS_MAX);
  615. if (count) {
  616. err = process_usb_nodes(blob, node_list, count);
  617. if (err)
  618. printf("%s: Error processing USB node!\n",
  619. __func__);
  620. return err;
  621. }
  622. }
  623. if (i == ARRAY_SIZE(fdt_usb_controllers))
  624. controller = NULL;
  625. return err;
  626. }
  627. /**
  628. * Start up the given port number (ports are numbered from 0 on each board).
  629. * This returns values for the appropriate hccr and hcor addresses to use for
  630. * USB EHCI operations.
  631. *
  632. * @param index port number to start
  633. * @param hccr returns start address of EHCI HCCR registers
  634. * @param hcor returns start address of EHCI HCOR registers
  635. * @return 0 if ok, -1 on error (generally invalid port number)
  636. */
  637. int ehci_hcd_init(int index, struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  638. {
  639. struct fdt_usb *config;
  640. struct usb_ctlr *usbctlr;
  641. if (index >= port_count)
  642. return -1;
  643. config = &port[index];
  644. /* skip init, if the port is already initialized */
  645. if (config->initialized)
  646. goto success;
  647. if (config->utmi && init_utmi_usb_controller(config)) {
  648. printf("tegrausb: Cannot init port %d\n", index);
  649. return -1;
  650. }
  651. if (config->ulpi && init_ulpi_usb_controller(config)) {
  652. printf("tegrausb: Cannot init port %d\n", index);
  653. return -1;
  654. }
  655. set_host_mode(config);
  656. config->initialized = 1;
  657. success:
  658. usbctlr = config->reg;
  659. *hccr = (struct ehci_hccr *)&usbctlr->cap_length;
  660. *hcor = (struct ehci_hcor *)&usbctlr->usb_cmd;
  661. if (controller->has_hostpc) {
  662. /* Set to Host mode after Controller Reset was done */
  663. clrsetbits_le32(&usbctlr->usb_mode, USBMODE_CM_HC,
  664. USBMODE_CM_HC);
  665. /* Select UTMI parallel interface after setting host mode */
  666. if (config->utmi) {
  667. clrsetbits_le32((char *)&usbctlr->usb_cmd +
  668. HOSTPC1_DEVLC, PTS_MASK,
  669. PTS_UTMI << PTS_SHIFT);
  670. clrbits_le32((char *)&usbctlr->usb_cmd +
  671. HOSTPC1_DEVLC, STS);
  672. }
  673. }
  674. return 0;
  675. }
  676. /*
  677. * Bring down the specified USB controller
  678. */
  679. int ehci_hcd_stop(int index)
  680. {
  681. struct usb_ctlr *usbctlr;
  682. usbctlr = port[index].reg;
  683. /* Stop controller */
  684. writel(0, &usbctlr->usb_cmd);
  685. udelay(1000);
  686. /* Initiate controller reset */
  687. writel(2, &usbctlr->usb_cmd);
  688. udelay(1000);
  689. port[index].initialized = 0;
  690. return 0;
  691. }