ehci-tegra.c 28 KB

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