usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2010,2011 NVIDIA Corporation <www.nvidia.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm-generic/gpio.h>
  26. #include <asm/arch/tegra2.h>
  27. #include <asm/arch/clk_rst.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/gpio.h>
  30. #include <asm/arch/pinmux.h>
  31. #include <asm/arch/sys_proto.h>
  32. #include <asm/arch/uart.h>
  33. #include <asm/arch/usb.h>
  34. #include <libfdt.h>
  35. #include <fdtdec.h>
  36. enum {
  37. USB_PORTS_MAX = 4, /* Maximum ports we allow */
  38. };
  39. /* Parameters we need for USB */
  40. enum {
  41. PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
  42. PARAM_DIVM, /* PLL INPUT DIVIDER */
  43. PARAM_DIVP, /* POST DIVIDER (2^N) */
  44. PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
  45. PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
  46. PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
  47. PARAM_STABLE_COUNT, /* PLL-U STABLE count */
  48. PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
  49. PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
  50. PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
  51. PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
  52. PARAM_COUNT
  53. };
  54. /* Possible port types (dual role mode) */
  55. enum dr_mode {
  56. DR_MODE_NONE = 0,
  57. DR_MODE_HOST, /* supports host operation */
  58. DR_MODE_DEVICE, /* supports device operation */
  59. DR_MODE_OTG, /* supports both */
  60. };
  61. /* Information about a USB port */
  62. struct fdt_usb {
  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 enabled:1; /* 1 to enable, 0 to disable */
  66. unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
  67. enum dr_mode dr_mode; /* dual role mode */
  68. enum periph_id periph_id;/* peripheral id */
  69. struct fdt_gpio_state vbus_gpio; /* GPIO for vbus enable */
  70. };
  71. static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
  72. static unsigned port_count; /* Number of available ports */
  73. static int port_current; /* Current port (-1 = none) */
  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 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. };
  122. /* UTMIP Idle Wait Delay */
  123. static const u8 utmip_idle_wait_delay = 17;
  124. /* UTMIP Elastic limit */
  125. static const u8 utmip_elastic_limit = 16;
  126. /* UTMIP High Speed Sync Start Delay */
  127. static const u8 utmip_hs_sync_start_delay = 9;
  128. /* Put the port into host mode (this only works for OTG ports) */
  129. static void set_host_mode(struct fdt_usb *config)
  130. {
  131. if (config->dr_mode == DR_MODE_OTG) {
  132. /* Check whether remote host from USB1 is driving VBus */
  133. if (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS)
  134. return;
  135. /*
  136. * If not driving, we set the GPIO to enable VBUS. We assume
  137. * that the pinmux is set up correctly for this.
  138. */
  139. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  140. fdtdec_setup_gpio(&config->vbus_gpio);
  141. gpio_direction_output(config->vbus_gpio.gpio, 1);
  142. debug("set_host_mode: GPIO %d high\n",
  143. config->vbus_gpio.gpio);
  144. }
  145. }
  146. }
  147. void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
  148. {
  149. /* Reset the USB controller with 2us delay */
  150. reset_periph(config->periph_id, 2);
  151. /*
  152. * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
  153. * base address
  154. */
  155. if (config->has_legacy_mode)
  156. setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
  157. /* Put UTMIP1/3 in reset */
  158. setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  159. /* Enable the UTMIP PHY */
  160. if (config->utmi)
  161. setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
  162. /*
  163. * TODO: where do we take the USB1 out of reset? The old code would
  164. * take USB3 out of reset, but not USB1. This code doesn't do either.
  165. */
  166. }
  167. /* set up the USB controller with the parameters provided */
  168. static int init_usb_controller(struct fdt_usb *config,
  169. struct usb_ctlr *usbctlr, const u32 timing[])
  170. {
  171. u32 val;
  172. int loop_count;
  173. clock_enable(config->periph_id);
  174. /* Reset the usb controller */
  175. usbf_reset_controller(config, usbctlr);
  176. /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
  177. clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  178. /* Follow the crystal clock disable by >100ns delay */
  179. udelay(1);
  180. /*
  181. * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
  182. * mux must be switched to actually use a_sess_vld threshold.
  183. */
  184. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  185. clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
  186. VBUS_SENSE_CTL_MASK,
  187. VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
  188. }
  189. /*
  190. * PLL Delay CONFIGURATION settings. The following parameters control
  191. * the bring up of the plls.
  192. */
  193. val = readl(&usbctlr->utmip_misc_cfg1);
  194. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  195. timing[PARAM_STABLE_COUNT] << UTMIP_PLLU_STABLE_COUNT_SHIFT);
  196. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  197. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  198. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  199. writel(val, &usbctlr->utmip_misc_cfg1);
  200. /* Set PLL enable delay count and crystal frequency count */
  201. val = readl(&usbctlr->utmip_pll_cfg1);
  202. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  203. timing[PARAM_ENABLE_DELAY_COUNT] <<
  204. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  205. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  206. timing[PARAM_XTAL_FREQ_COUNT] <<
  207. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  208. writel(val, &usbctlr->utmip_pll_cfg1);
  209. /* Setting the tracking length time */
  210. clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
  211. UTMIP_BIAS_PDTRK_COUNT_MASK,
  212. timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
  213. /* Program debounce time for VBUS to become valid */
  214. clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
  215. UTMIP_DEBOUNCE_CFG0_MASK,
  216. timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
  217. setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
  218. /* Disable battery charge enabling bit */
  219. setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
  220. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
  221. setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
  222. /*
  223. * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
  224. * Setting these fields, together with default values of the
  225. * other fields, results in programming the registers below as
  226. * follows:
  227. * UTMIP_HSRX_CFG0 = 0x9168c000
  228. * UTMIP_HSRX_CFG1 = 0x13
  229. */
  230. /* Set PLL enable delay count and Crystal frequency count */
  231. val = readl(&usbctlr->utmip_hsrx_cfg0);
  232. clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
  233. utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
  234. clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
  235. utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
  236. writel(val, &usbctlr->utmip_hsrx_cfg0);
  237. /* Configure the UTMIP_HS_SYNC_START_DLY */
  238. clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
  239. UTMIP_HS_SYNC_START_DLY_MASK,
  240. utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
  241. /* Preceed the crystal clock disable by >100ns delay. */
  242. udelay(1);
  243. /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
  244. setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  245. /* Finished the per-controller init. */
  246. /* De-assert UTMIP_RESET to bring out of reset. */
  247. clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  248. /* Wait for the phy clock to become valid in 100 ms */
  249. for (loop_count = 100000; loop_count != 0; loop_count--) {
  250. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  251. break;
  252. udelay(1);
  253. }
  254. if (loop_count == 100000)
  255. return -1;
  256. return 0;
  257. }
  258. static void power_up_port(struct usb_ctlr *usbctlr)
  259. {
  260. /* Deassert power down state */
  261. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
  262. UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
  263. clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
  264. UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
  265. }
  266. static void config_clock(const u32 timing[])
  267. {
  268. clock_start_pll(CLOCK_ID_USB,
  269. timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
  270. timing[PARAM_CPCON], timing[PARAM_LFCON]);
  271. }
  272. /**
  273. * Add a new USB port to the list of available ports.
  274. *
  275. * @param config USB port configuration
  276. * @return 0 if ok, -1 if error (too many ports)
  277. */
  278. static int add_port(struct fdt_usb *config, const u32 timing[])
  279. {
  280. struct usb_ctlr *usbctlr = config->reg;
  281. if (port_count == USB_PORTS_MAX) {
  282. debug("tegrausb: Cannot register more than %d ports\n",
  283. USB_PORTS_MAX);
  284. return -1;
  285. }
  286. if (init_usb_controller(config, usbctlr, timing)) {
  287. debug("tegrausb: Cannot init port\n");
  288. return -1;
  289. }
  290. if (config->utmi) {
  291. /* Disable ICUSB FS/LS transceiver */
  292. clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
  293. /* Select UTMI parallel interface */
  294. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
  295. PTS_UTMI << PTS_SHIFT);
  296. clrbits_le32(&usbctlr->port_sc1, STS);
  297. power_up_port(usbctlr);
  298. }
  299. port[port_count++] = *config;
  300. return 0;
  301. }
  302. int tegrausb_start_port(unsigned portnum, u32 *hccr, u32 *hcor)
  303. {
  304. struct usb_ctlr *usbctlr;
  305. if (portnum >= port_count)
  306. return -1;
  307. tegrausb_stop_port();
  308. set_host_mode(&port[portnum]);
  309. usbctlr = port[portnum].reg;
  310. *hccr = (u32)&usbctlr->cap_length;
  311. *hcor = (u32)&usbctlr->usb_cmd;
  312. port_current = portnum;
  313. return 0;
  314. }
  315. int tegrausb_stop_port(void)
  316. {
  317. struct usb_ctlr *usbctlr;
  318. if (port_current == -1)
  319. return -1;
  320. usbctlr = port[port_current].reg;
  321. /* Stop controller */
  322. writel(0, &usbctlr->usb_cmd);
  323. udelay(1000);
  324. /* Initiate controller reset */
  325. writel(2, &usbctlr->usb_cmd);
  326. udelay(1000);
  327. port_current = -1;
  328. return 0;
  329. }
  330. int fdt_decode_usb(const void *blob, int node, unsigned osc_frequency_mhz,
  331. struct fdt_usb *config)
  332. {
  333. const char *phy, *mode;
  334. config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
  335. mode = fdt_getprop(blob, node, "dr_mode", NULL);
  336. if (mode) {
  337. if (0 == strcmp(mode, "host"))
  338. config->dr_mode = DR_MODE_HOST;
  339. else if (0 == strcmp(mode, "peripheral"))
  340. config->dr_mode = DR_MODE_DEVICE;
  341. else if (0 == strcmp(mode, "otg"))
  342. config->dr_mode = DR_MODE_OTG;
  343. else {
  344. debug("%s: Cannot decode dr_mode '%s'\n", __func__,
  345. mode);
  346. return -FDT_ERR_NOTFOUND;
  347. }
  348. } else {
  349. config->dr_mode = DR_MODE_HOST;
  350. }
  351. phy = fdt_getprop(blob, node, "phy_type", NULL);
  352. config->utmi = phy && 0 == strcmp("utmi", phy);
  353. config->enabled = fdtdec_get_is_enabled(blob, node);
  354. config->has_legacy_mode = fdtdec_get_bool(blob, node,
  355. "nvidia,has-legacy-mode");
  356. config->periph_id = clock_decode_periph_id(blob, node);
  357. if (config->periph_id == PERIPH_ID_NONE) {
  358. debug("%s: Missing/invalid peripheral ID\n", __func__);
  359. return -FDT_ERR_NOTFOUND;
  360. }
  361. fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
  362. debug("enabled=%d, legacy_mode=%d, utmi=%d, periph_id=%d, vbus=%d, "
  363. "dr_mode=%d\n", config->enabled, config->has_legacy_mode,
  364. config->utmi, config->periph_id, config->vbus_gpio.gpio,
  365. config->dr_mode);
  366. return 0;
  367. }
  368. int board_usb_init(const void *blob)
  369. {
  370. struct fdt_usb config;
  371. unsigned osc_freq = clock_get_rate(CLOCK_ID_OSC);
  372. enum clock_osc_freq freq;
  373. int node_list[USB_PORTS_MAX];
  374. int node, count, i;
  375. /* Set up the USB clocks correctly based on our oscillator frequency */
  376. freq = clock_get_osc_freq();
  377. config_clock(usb_pll[freq]);
  378. /* count may return <0 on error */
  379. count = fdtdec_find_aliases_for_id(blob, "usb",
  380. COMPAT_NVIDIA_TEGRA20_USB, node_list, USB_PORTS_MAX);
  381. for (i = 0; i < count; i++) {
  382. debug("USB %d: ", i);
  383. node = node_list[i];
  384. if (!node)
  385. continue;
  386. if (fdt_decode_usb(blob, node, osc_freq, &config)) {
  387. debug("Cannot decode USB node %s\n",
  388. fdt_get_name(blob, node, NULL));
  389. return -1;
  390. }
  391. if (add_port(&config, usb_pll[freq]))
  392. return -1;
  393. set_host_mode(&config);
  394. }
  395. port_current = -1;
  396. return 0;
  397. }