ehci-tegra.c 25 KB

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