usb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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/clock.h>
  27. #include <asm/arch/gpio.h>
  28. #include <asm/arch/pinmux.h>
  29. #include <asm/arch/tegra.h>
  30. #include <asm/arch/usb.h>
  31. #include <usb/ulpi.h>
  32. #include <asm/arch-tegra/clk_rst.h>
  33. #include <asm/arch-tegra/sys_proto.h>
  34. #include <asm/arch-tegra/uart.h>
  35. #include <libfdt.h>
  36. #include <fdtdec.h>
  37. #ifdef CONFIG_USB_ULPI
  38. #ifndef CONFIG_USB_ULPI_VIEWPORT
  39. #error "To use CONFIG_USB_ULPI on Tegra Boards you have to also \
  40. define CONFIG_USB_ULPI_VIEWPORT"
  41. #endif
  42. #endif
  43. enum {
  44. USB_PORTS_MAX = 3, /* Maximum ports we allow */
  45. };
  46. /* Parameters we need for USB */
  47. enum {
  48. PARAM_DIVN, /* PLL FEEDBACK DIVIDer */
  49. PARAM_DIVM, /* PLL INPUT DIVIDER */
  50. PARAM_DIVP, /* POST DIVIDER (2^N) */
  51. PARAM_CPCON, /* BASE PLLC CHARGE Pump setup ctrl */
  52. PARAM_LFCON, /* BASE PLLC LOOP FILter setup ctrl */
  53. PARAM_ENABLE_DELAY_COUNT, /* PLL-U Enable Delay Count */
  54. PARAM_STABLE_COUNT, /* PLL-U STABLE count */
  55. PARAM_ACTIVE_DELAY_COUNT, /* PLL-U Active delay count */
  56. PARAM_XTAL_FREQ_COUNT, /* PLL-U XTAL frequency count */
  57. PARAM_DEBOUNCE_A_TIME, /* 10MS DELAY for BIAS_DEBOUNCE_A */
  58. PARAM_BIAS_TIME, /* 20US DELAY AFter bias cell op */
  59. PARAM_COUNT
  60. };
  61. /* Possible port types (dual role mode) */
  62. enum dr_mode {
  63. DR_MODE_NONE = 0,
  64. DR_MODE_HOST, /* supports host operation */
  65. DR_MODE_DEVICE, /* supports device operation */
  66. DR_MODE_OTG, /* supports both */
  67. };
  68. /* Information about a USB port */
  69. struct fdt_usb {
  70. struct usb_ctlr *reg; /* address of registers in physical memory */
  71. unsigned utmi:1; /* 1 if port has external tranceiver, else 0 */
  72. unsigned ulpi:1; /* 1 if port has external ULPI transceiver */
  73. unsigned enabled:1; /* 1 to enable, 0 to disable */
  74. unsigned has_legacy_mode:1; /* 1 if this port has legacy mode */
  75. unsigned initialized:1; /* has this port already been initialized? */
  76. enum dr_mode dr_mode; /* dual role mode */
  77. enum periph_id periph_id;/* peripheral id */
  78. struct fdt_gpio_state vbus_gpio; /* GPIO for vbus enable */
  79. struct fdt_gpio_state phy_reset_gpio; /* GPIO to reset ULPI phy */
  80. };
  81. static struct fdt_usb port[USB_PORTS_MAX]; /* List of valid USB ports */
  82. static unsigned port_count; /* Number of available ports */
  83. /*
  84. * This table has USB timing parameters for each Oscillator frequency we
  85. * support. There are four sets of values:
  86. *
  87. * 1. PLLU configuration information (reference clock is osc/clk_m and
  88. * PLLU-FOs are fixed at 12MHz/60MHz/480MHz).
  89. *
  90. * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
  91. * ----------------------------------------------------------------------
  92. * DIVN 960 (0x3c0) 200 (0c8) 960 (3c0h) 960 (3c0)
  93. * DIVM 13 (0d) 4 (04) 12 (0c) 26 (1a)
  94. * Filter frequency (MHz) 1 4.8 6 2
  95. * CPCON 1100b 0011b 1100b 1100b
  96. * LFCON0 0 0 0 0
  97. *
  98. * 2. PLL CONFIGURATION & PARAMETERS for different clock generators:
  99. *
  100. * Reference frequency 13.0MHz 19.2MHz 12.0MHz 26.0MHz
  101. * ---------------------------------------------------------------------------
  102. * PLLU_ENABLE_DLY_COUNT 02 (0x02) 03 (03) 02 (02) 04 (04)
  103. * PLLU_STABLE_COUNT 51 (33) 75 (4B) 47 (2F) 102 (66)
  104. * PLL_ACTIVE_DLY_COUNT 05 (05) 06 (06) 04 (04) 09 (09)
  105. * XTAL_FREQ_COUNT 127 (7F) 187 (BB) 118 (76) 254 (FE)
  106. *
  107. * 3. Debounce values IdDig, Avalid, Bvalid, VbusValid, VbusWakeUp, and
  108. * SessEnd. Each of these signals have their own debouncer and for each of
  109. * those one out of two debouncing times can be chosen (BIAS_DEBOUNCE_A or
  110. * BIAS_DEBOUNCE_B).
  111. *
  112. * The values of DEBOUNCE_A and DEBOUNCE_B are calculated as follows:
  113. * 0xffff -> No debouncing at all
  114. * <n> ms = <n> *1000 / (1/19.2MHz) / 4
  115. *
  116. * So to program a 1 ms debounce for BIAS_DEBOUNCE_A, we have:
  117. * BIAS_DEBOUNCE_A[15:0] = 1000 * 19.2 / 4 = 4800 = 0x12c0
  118. *
  119. * We need to use only DebounceA for BOOTROM. We don't need the DebounceB
  120. * values, so we can keep those to default.
  121. *
  122. * 4. The 20 microsecond delay after bias cell operation.
  123. */
  124. static const unsigned usb_pll[CLOCK_OSC_FREQ_COUNT][PARAM_COUNT] = {
  125. /* DivN, DivM, DivP, CPCON, LFCON, Delays Debounce, Bias */
  126. { 0x3C0, 0x0D, 0x00, 0xC, 0, 0x02, 0x33, 0x05, 0x7F, 0x7EF4, 5 },
  127. { 0x0C8, 0x04, 0x00, 0x3, 0, 0x03, 0x4B, 0x06, 0xBB, 0xBB80, 7 },
  128. { 0x3C0, 0x0C, 0x00, 0xC, 0, 0x02, 0x2F, 0x04, 0x76, 0x7530, 5 },
  129. { 0x3C0, 0x1A, 0x00, 0xC, 0, 0x04, 0x66, 0x09, 0xFE, 0xFDE8, 9 }
  130. };
  131. /* UTMIP Idle Wait Delay */
  132. static const u8 utmip_idle_wait_delay = 17;
  133. /* UTMIP Elastic limit */
  134. static const u8 utmip_elastic_limit = 16;
  135. /* UTMIP High Speed Sync Start Delay */
  136. static const u8 utmip_hs_sync_start_delay = 9;
  137. /* Put the port into host mode */
  138. static void set_host_mode(struct fdt_usb *config)
  139. {
  140. /*
  141. * If we are an OTG port, check if remote host is driving VBus and
  142. * bail out in this case.
  143. */
  144. if (config->dr_mode == DR_MODE_OTG &&
  145. (readl(&config->reg->phy_vbus_sensors) & VBUS_VLD_STS))
  146. return;
  147. /*
  148. * If not driving, we set the GPIO to enable VBUS. We assume
  149. * that the pinmux is set up correctly for this.
  150. */
  151. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  152. fdtdec_setup_gpio(&config->vbus_gpio);
  153. gpio_direction_output(config->vbus_gpio.gpio,
  154. (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
  155. 0 : 1);
  156. debug("set_host_mode: GPIO %d %s\n", config->vbus_gpio.gpio,
  157. (config->vbus_gpio.flags & FDT_GPIO_ACTIVE_LOW) ?
  158. "low" : "high");
  159. }
  160. }
  161. void usbf_reset_controller(struct fdt_usb *config, struct usb_ctlr *usbctlr)
  162. {
  163. /* Reset the USB controller with 2us delay */
  164. reset_periph(config->periph_id, 2);
  165. /*
  166. * Set USB1_NO_LEGACY_MODE to 1, Registers are accessible under
  167. * base address
  168. */
  169. if (config->has_legacy_mode)
  170. setbits_le32(&usbctlr->usb1_legacy_ctrl, USB1_NO_LEGACY_MODE);
  171. /* Put UTMIP1/3 in reset */
  172. setbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  173. /* Enable the UTMIP PHY */
  174. if (config->utmi)
  175. setbits_le32(&usbctlr->susp_ctrl, UTMIP_PHY_ENB);
  176. /*
  177. * TODO: where do we take the USB1 out of reset? The old code would
  178. * take USB3 out of reset, but not USB1. This code doesn't do either.
  179. */
  180. }
  181. /* set up the UTMI USB controller with the parameters provided */
  182. static int init_utmi_usb_controller(struct fdt_usb *config)
  183. {
  184. u32 val;
  185. int loop_count;
  186. const unsigned *timing;
  187. struct usb_ctlr *usbctlr = config->reg;
  188. clock_enable(config->periph_id);
  189. /* Reset the usb controller */
  190. usbf_reset_controller(config, usbctlr);
  191. /* Stop crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN low */
  192. clrbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  193. /* Follow the crystal clock disable by >100ns delay */
  194. udelay(1);
  195. /*
  196. * To Use the A Session Valid for cable detection logic, VBUS_WAKEUP
  197. * mux must be switched to actually use a_sess_vld threshold.
  198. */
  199. if (fdt_gpio_isvalid(&config->vbus_gpio)) {
  200. clrsetbits_le32(&usbctlr->usb1_legacy_ctrl,
  201. VBUS_SENSE_CTL_MASK,
  202. VBUS_SENSE_CTL_A_SESS_VLD << VBUS_SENSE_CTL_SHIFT);
  203. }
  204. /*
  205. * PLL Delay CONFIGURATION settings. The following parameters control
  206. * the bring up of the plls.
  207. */
  208. timing = usb_pll[clock_get_osc_freq()];
  209. val = readl(&usbctlr->utmip_misc_cfg1);
  210. clrsetbits_le32(&val, UTMIP_PLLU_STABLE_COUNT_MASK,
  211. timing[PARAM_STABLE_COUNT] << UTMIP_PLLU_STABLE_COUNT_SHIFT);
  212. clrsetbits_le32(&val, UTMIP_PLL_ACTIVE_DLY_COUNT_MASK,
  213. timing[PARAM_ACTIVE_DELAY_COUNT] <<
  214. UTMIP_PLL_ACTIVE_DLY_COUNT_SHIFT);
  215. writel(val, &usbctlr->utmip_misc_cfg1);
  216. /* Set PLL enable delay count and crystal frequency count */
  217. val = readl(&usbctlr->utmip_pll_cfg1);
  218. clrsetbits_le32(&val, UTMIP_PLLU_ENABLE_DLY_COUNT_MASK,
  219. timing[PARAM_ENABLE_DELAY_COUNT] <<
  220. UTMIP_PLLU_ENABLE_DLY_COUNT_SHIFT);
  221. clrsetbits_le32(&val, UTMIP_XTAL_FREQ_COUNT_MASK,
  222. timing[PARAM_XTAL_FREQ_COUNT] <<
  223. UTMIP_XTAL_FREQ_COUNT_SHIFT);
  224. writel(val, &usbctlr->utmip_pll_cfg1);
  225. /* Setting the tracking length time */
  226. clrsetbits_le32(&usbctlr->utmip_bias_cfg1,
  227. UTMIP_BIAS_PDTRK_COUNT_MASK,
  228. timing[PARAM_BIAS_TIME] << UTMIP_BIAS_PDTRK_COUNT_SHIFT);
  229. /* Program debounce time for VBUS to become valid */
  230. clrsetbits_le32(&usbctlr->utmip_debounce_cfg0,
  231. UTMIP_DEBOUNCE_CFG0_MASK,
  232. timing[PARAM_DEBOUNCE_A_TIME] << UTMIP_DEBOUNCE_CFG0_SHIFT);
  233. setbits_le32(&usbctlr->utmip_tx_cfg0, UTMIP_FS_PREAMBLE_J);
  234. /* Disable battery charge enabling bit */
  235. setbits_le32(&usbctlr->utmip_bat_chrg_cfg0, UTMIP_PD_CHRG);
  236. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_XCVR_LSBIAS_SE);
  237. setbits_le32(&usbctlr->utmip_spare_cfg0, FUSE_SETUP_SEL);
  238. /*
  239. * Configure the UTMIP_IDLE_WAIT and UTMIP_ELASTIC_LIMIT
  240. * Setting these fields, together with default values of the
  241. * other fields, results in programming the registers below as
  242. * follows:
  243. * UTMIP_HSRX_CFG0 = 0x9168c000
  244. * UTMIP_HSRX_CFG1 = 0x13
  245. */
  246. /* Set PLL enable delay count and Crystal frequency count */
  247. val = readl(&usbctlr->utmip_hsrx_cfg0);
  248. clrsetbits_le32(&val, UTMIP_IDLE_WAIT_MASK,
  249. utmip_idle_wait_delay << UTMIP_IDLE_WAIT_SHIFT);
  250. clrsetbits_le32(&val, UTMIP_ELASTIC_LIMIT_MASK,
  251. utmip_elastic_limit << UTMIP_ELASTIC_LIMIT_SHIFT);
  252. writel(val, &usbctlr->utmip_hsrx_cfg0);
  253. /* Configure the UTMIP_HS_SYNC_START_DLY */
  254. clrsetbits_le32(&usbctlr->utmip_hsrx_cfg1,
  255. UTMIP_HS_SYNC_START_DLY_MASK,
  256. utmip_hs_sync_start_delay << UTMIP_HS_SYNC_START_DLY_SHIFT);
  257. /* Preceed the crystal clock disable by >100ns delay. */
  258. udelay(1);
  259. /* Resuscitate crystal clock by setting UTMIP_PHY_XTAL_CLOCKEN */
  260. setbits_le32(&usbctlr->utmip_misc_cfg1, UTMIP_PHY_XTAL_CLOCKEN);
  261. /* Finished the per-controller init. */
  262. /* De-assert UTMIP_RESET to bring out of reset. */
  263. clrbits_le32(&usbctlr->susp_ctrl, UTMIP_RESET);
  264. /* Wait for the phy clock to become valid in 100 ms */
  265. for (loop_count = 100000; loop_count != 0; loop_count--) {
  266. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  267. break;
  268. udelay(1);
  269. }
  270. if (!loop_count)
  271. return -1;
  272. /* Disable ICUSB FS/LS transceiver */
  273. clrbits_le32(&usbctlr->icusb_ctrl, IC_ENB1);
  274. /* Select UTMI parallel interface */
  275. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK,
  276. PTS_UTMI << PTS_SHIFT);
  277. clrbits_le32(&usbctlr->port_sc1, STS);
  278. /* Deassert power down state */
  279. clrbits_le32(&usbctlr->utmip_xcvr_cfg0, UTMIP_FORCE_PD_POWERDOWN |
  280. UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN);
  281. clrbits_le32(&usbctlr->utmip_xcvr_cfg1, UTMIP_FORCE_PDDISC_POWERDOWN |
  282. UTMIP_FORCE_PDCHRP_POWERDOWN | UTMIP_FORCE_PDDR_POWERDOWN);
  283. return 0;
  284. }
  285. #ifdef CONFIG_USB_ULPI
  286. /* if board file does not set a ULPI reference frequency we default to 24MHz */
  287. #ifndef CONFIG_ULPI_REF_CLK
  288. #define CONFIG_ULPI_REF_CLK 24000000
  289. #endif
  290. /* set up the ULPI USB controller with the parameters provided */
  291. static int init_ulpi_usb_controller(struct fdt_usb *config)
  292. {
  293. u32 val;
  294. int loop_count;
  295. struct ulpi_viewport ulpi_vp;
  296. struct usb_ctlr *usbctlr = config->reg;
  297. /* set up ULPI reference clock on pllp_out4 */
  298. clock_enable(PERIPH_ID_DEV2_OUT);
  299. clock_set_pllout(CLOCK_ID_PERIPH, PLL_OUT4, CONFIG_ULPI_REF_CLK);
  300. /* reset ULPI phy */
  301. if (fdt_gpio_isvalid(&config->phy_reset_gpio)) {
  302. fdtdec_setup_gpio(&config->phy_reset_gpio);
  303. gpio_direction_output(config->phy_reset_gpio.gpio, 0);
  304. mdelay(5);
  305. gpio_set_value(config->phy_reset_gpio.gpio, 1);
  306. }
  307. /* Reset the usb controller */
  308. clock_enable(config->periph_id);
  309. usbf_reset_controller(config, usbctlr);
  310. /* enable pinmux bypass */
  311. setbits_le32(&usbctlr->ulpi_timing_ctrl_0,
  312. ULPI_CLKOUT_PINMUX_BYP | ULPI_OUTPUT_PINMUX_BYP);
  313. /* Select ULPI parallel interface */
  314. clrsetbits_le32(&usbctlr->port_sc1, PTS_MASK, PTS_ULPI << PTS_SHIFT);
  315. /* enable ULPI transceiver */
  316. setbits_le32(&usbctlr->susp_ctrl, ULPI_PHY_ENB);
  317. /* configure ULPI transceiver timings */
  318. val = 0;
  319. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  320. val |= ULPI_DATA_TRIMMER_SEL(4);
  321. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  322. val |= ULPI_DIR_TRIMMER_SEL(4);
  323. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  324. udelay(10);
  325. val |= ULPI_DATA_TRIMMER_LOAD;
  326. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  327. val |= ULPI_DIR_TRIMMER_LOAD;
  328. writel(val, &usbctlr->ulpi_timing_ctrl_1);
  329. /* set up phy for host operation with external vbus supply */
  330. ulpi_vp.port_num = 0;
  331. ulpi_vp.viewport_addr = (u32)&usbctlr->ulpi_viewport;
  332. if (ulpi_init(&ulpi_vp)) {
  333. printf("Tegra ULPI viewport init failed\n");
  334. return -1;
  335. }
  336. ulpi_set_vbus(&ulpi_vp, 1, 1);
  337. ulpi_set_vbus_indicator(&ulpi_vp, 1, 1, 0);
  338. /* enable wakeup events */
  339. setbits_le32(&usbctlr->port_sc1, WKCN | WKDS | WKOC);
  340. /* Enable and wait for the phy clock to become valid in 100 ms */
  341. setbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  342. for (loop_count = 100000; loop_count != 0; loop_count--) {
  343. if (readl(&usbctlr->susp_ctrl) & USB_PHY_CLK_VALID)
  344. break;
  345. udelay(1);
  346. }
  347. if (!loop_count)
  348. return -1;
  349. clrbits_le32(&usbctlr->susp_ctrl, USB_SUSP_CLR);
  350. return 0;
  351. }
  352. #else
  353. static int init_ulpi_usb_controller(struct fdt_usb *config)
  354. {
  355. printf("No code to set up ULPI controller, please enable"
  356. "CONFIG_USB_ULPI and CONFIG_USB_ULPI_VIEWPORT");
  357. return -1;
  358. }
  359. #endif
  360. static void config_clock(const u32 timing[])
  361. {
  362. clock_start_pll(CLOCK_ID_USB,
  363. timing[PARAM_DIVM], timing[PARAM_DIVN], timing[PARAM_DIVP],
  364. timing[PARAM_CPCON], timing[PARAM_LFCON]);
  365. }
  366. int tegrausb_start_port(int portnum, u32 *hccr, u32 *hcor)
  367. {
  368. struct fdt_usb *config;
  369. struct usb_ctlr *usbctlr;
  370. if (portnum >= port_count)
  371. return -1;
  372. config = &port[portnum];
  373. /* skip init, if the port is already initialized */
  374. if (config->initialized)
  375. goto success;
  376. if (config->utmi && init_utmi_usb_controller(config)) {
  377. printf("tegrausb: Cannot init port %d\n", portnum);
  378. return -1;
  379. }
  380. if (config->ulpi && init_ulpi_usb_controller(config)) {
  381. printf("tegrausb: Cannot init port %d\n", portnum);
  382. return -1;
  383. }
  384. set_host_mode(config);
  385. config->initialized = 1;
  386. success:
  387. usbctlr = config->reg;
  388. *hccr = (u32)&usbctlr->cap_length;
  389. *hcor = (u32)&usbctlr->usb_cmd;
  390. return 0;
  391. }
  392. int tegrausb_stop_port(int portnum)
  393. {
  394. struct usb_ctlr *usbctlr;
  395. usbctlr = port[portnum].reg;
  396. /* Stop controller */
  397. writel(0, &usbctlr->usb_cmd);
  398. udelay(1000);
  399. /* Initiate controller reset */
  400. writel(2, &usbctlr->usb_cmd);
  401. udelay(1000);
  402. port[portnum].initialized = 0;
  403. return 0;
  404. }
  405. int fdt_decode_usb(const void *blob, int node, struct fdt_usb *config)
  406. {
  407. const char *phy, *mode;
  408. config->reg = (struct usb_ctlr *)fdtdec_get_addr(blob, node, "reg");
  409. mode = fdt_getprop(blob, node, "dr_mode", NULL);
  410. if (mode) {
  411. if (0 == strcmp(mode, "host"))
  412. config->dr_mode = DR_MODE_HOST;
  413. else if (0 == strcmp(mode, "peripheral"))
  414. config->dr_mode = DR_MODE_DEVICE;
  415. else if (0 == strcmp(mode, "otg"))
  416. config->dr_mode = DR_MODE_OTG;
  417. else {
  418. debug("%s: Cannot decode dr_mode '%s'\n", __func__,
  419. mode);
  420. return -FDT_ERR_NOTFOUND;
  421. }
  422. } else {
  423. config->dr_mode = DR_MODE_HOST;
  424. }
  425. phy = fdt_getprop(blob, node, "phy_type", NULL);
  426. config->utmi = phy && 0 == strcmp("utmi", phy);
  427. config->ulpi = phy && 0 == strcmp("ulpi", phy);
  428. config->enabled = fdtdec_get_is_enabled(blob, node);
  429. config->has_legacy_mode = fdtdec_get_bool(blob, node,
  430. "nvidia,has-legacy-mode");
  431. config->periph_id = clock_decode_periph_id(blob, node);
  432. if (config->periph_id == PERIPH_ID_NONE) {
  433. debug("%s: Missing/invalid peripheral ID\n", __func__);
  434. return -FDT_ERR_NOTFOUND;
  435. }
  436. fdtdec_decode_gpio(blob, node, "nvidia,vbus-gpio", &config->vbus_gpio);
  437. fdtdec_decode_gpio(blob, node, "nvidia,phy-reset-gpio",
  438. &config->phy_reset_gpio);
  439. debug("enabled=%d, legacy_mode=%d, utmi=%d, ulpi=%d, periph_id=%d, "
  440. "vbus=%d, phy_reset=%d, dr_mode=%d\n",
  441. config->enabled, config->has_legacy_mode, config->utmi,
  442. config->ulpi, config->periph_id, config->vbus_gpio.gpio,
  443. config->phy_reset_gpio.gpio, config->dr_mode);
  444. return 0;
  445. }
  446. int board_usb_init(const void *blob)
  447. {
  448. struct fdt_usb config;
  449. enum clock_osc_freq freq;
  450. int node_list[USB_PORTS_MAX];
  451. int node, count, i;
  452. /* Set up the USB clocks correctly based on our oscillator frequency */
  453. freq = clock_get_osc_freq();
  454. config_clock(usb_pll[freq]);
  455. /* count may return <0 on error */
  456. count = fdtdec_find_aliases_for_id(blob, "usb",
  457. COMPAT_NVIDIA_TEGRA20_USB, node_list, USB_PORTS_MAX);
  458. for (i = 0; i < count; i++) {
  459. if (port_count == USB_PORTS_MAX) {
  460. printf("tegrausb: Cannot register more than %d ports\n",
  461. USB_PORTS_MAX);
  462. return -1;
  463. }
  464. debug("USB %d: ", i);
  465. node = node_list[i];
  466. if (!node)
  467. continue;
  468. if (fdt_decode_usb(blob, node, &config)) {
  469. debug("Cannot decode USB node %s\n",
  470. fdt_get_name(blob, node, NULL));
  471. return -1;
  472. }
  473. config.initialized = 0;
  474. /* add new USB port to the list of available ports */
  475. port[port_count++] = config;
  476. }
  477. return 0;
  478. }