ehci-tegra.c 27 KB

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