usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * arch/arm/plat-omap/usb.c -- platform level USB initialization
  3. *
  4. * Copyright (C) 2004 Texas Instruments, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #undef DEBUG
  21. #include <linux/config.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/usb_otg.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/system.h>
  32. #include <asm/hardware.h>
  33. #include <asm/arch/mux.h>
  34. #include <asm/arch/usb.h>
  35. #include <asm/arch/board.h>
  36. /* These routines should handle the standard chip-specific modes
  37. * for usb0/1/2 ports, covering basic mux and transceiver setup.
  38. * Call omap_usb_init() once, from INIT_MACHINE().
  39. *
  40. * Some board-*.c files will need to set up additional mux options,
  41. * like for suspend handling, vbus sensing, GPIOs, and the D+ pullup.
  42. */
  43. /* TESTED ON:
  44. * - 1611B H2 (with usb1 mini-AB) using standard Mini-B or OTG cables
  45. * - 5912 OSK OHCI (with usb0 standard-A), standard A-to-B cables
  46. * - 5912 OSK UDC, with *nonstandard* A-to-A cable
  47. * - 1510 Innovator UDC with bundled usb0 cable
  48. * - 1510 Innovator OHCI with bundled usb1/usb2 cable
  49. * - 1510 Innovator OHCI with custom usb0 cable, feeding 5V VBUS
  50. * - 1710 custom development board using alternate pin group
  51. * - 1710 H3 (with usb1 mini-AB) using standard Mini-B or OTG cables
  52. */
  53. /*-------------------------------------------------------------------------*/
  54. #ifdef CONFIG_ARCH_OMAP_OTG
  55. static struct otg_transceiver *xceiv;
  56. /**
  57. * otg_get_transceiver - find the (single) OTG transceiver driver
  58. *
  59. * Returns the transceiver driver, after getting a refcount to it; or
  60. * null if there is no such transceiver. The caller is responsible for
  61. * releasing that count.
  62. */
  63. struct otg_transceiver *otg_get_transceiver(void)
  64. {
  65. if (xceiv)
  66. get_device(xceiv->dev);
  67. return xceiv;
  68. }
  69. EXPORT_SYMBOL(otg_get_transceiver);
  70. int otg_set_transceiver(struct otg_transceiver *x)
  71. {
  72. if (xceiv && x)
  73. return -EBUSY;
  74. xceiv = x;
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(otg_set_transceiver);
  78. #endif
  79. /*-------------------------------------------------------------------------*/
  80. #if defined(CONFIG_ARCH_OMAP_OTG) || defined(CONFIG_ARCH_OMAP15XX)
  81. static u32 __init omap_usb0_init(unsigned nwires, unsigned is_device)
  82. {
  83. u32 syscon1 = 0;
  84. if (nwires == 0) {
  85. if (!cpu_is_omap15xx()) {
  86. /* pulldown D+/D- */
  87. USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
  88. }
  89. return 0;
  90. }
  91. if (is_device)
  92. omap_cfg_reg(W4_USB_PUEN);
  93. /* internal transceiver */
  94. if (nwires == 2) {
  95. // omap_cfg_reg(P9_USB_DP);
  96. // omap_cfg_reg(R8_USB_DM);
  97. if (cpu_is_omap15xx()) {
  98. /* This works on 1510-Innovator */
  99. return 0;
  100. }
  101. /* NOTES:
  102. * - peripheral should configure VBUS detection!
  103. * - only peripherals may use the internal D+/D- pulldowns
  104. * - OTG support on this port not yet written
  105. */
  106. USB_TRANSCEIVER_CTRL_REG &= ~(7 << 4);
  107. if (!is_device)
  108. USB_TRANSCEIVER_CTRL_REG |= (3 << 1);
  109. return 3 << 16;
  110. }
  111. /* alternate pin config, external transceiver */
  112. if (cpu_is_omap15xx()) {
  113. printk(KERN_ERR "no usb0 alt pin config on 15xx\n");
  114. return 0;
  115. }
  116. omap_cfg_reg(V6_USB0_TXD);
  117. omap_cfg_reg(W9_USB0_TXEN);
  118. omap_cfg_reg(W5_USB0_SE0);
  119. /* NOTE: SPEED and SUSP aren't configured here */
  120. if (nwires != 3)
  121. omap_cfg_reg(Y5_USB0_RCV);
  122. if (nwires != 6)
  123. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
  124. switch (nwires) {
  125. case 3:
  126. syscon1 = 2;
  127. break;
  128. case 4:
  129. syscon1 = 1;
  130. break;
  131. case 6:
  132. syscon1 = 3;
  133. omap_cfg_reg(AA9_USB0_VP);
  134. omap_cfg_reg(R9_USB0_VM);
  135. USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
  136. break;
  137. default:
  138. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  139. 0, nwires);
  140. }
  141. return syscon1 << 16;
  142. }
  143. static u32 __init omap_usb1_init(unsigned nwires)
  144. {
  145. u32 syscon1 = 0;
  146. if (nwires != 6 && !cpu_is_omap15xx())
  147. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB1_UNI_R;
  148. if (nwires == 0)
  149. return 0;
  150. /* external transceiver */
  151. omap_cfg_reg(USB1_TXD);
  152. omap_cfg_reg(USB1_TXEN);
  153. if (cpu_is_omap15xx()) {
  154. omap_cfg_reg(USB1_SEO);
  155. omap_cfg_reg(USB1_SPEED);
  156. // SUSP
  157. } else if (cpu_is_omap1610() || cpu_is_omap5912()) {
  158. omap_cfg_reg(W13_1610_USB1_SE0);
  159. omap_cfg_reg(R13_1610_USB1_SPEED);
  160. // SUSP
  161. } else if (cpu_is_omap1710()) {
  162. omap_cfg_reg(R13_1710_USB1_SE0);
  163. // SUSP
  164. } else {
  165. pr_debug("usb unrecognized\n");
  166. }
  167. if (nwires != 3)
  168. omap_cfg_reg(USB1_RCV);
  169. switch (nwires) {
  170. case 3:
  171. syscon1 = 2;
  172. break;
  173. case 4:
  174. syscon1 = 1;
  175. break;
  176. case 6:
  177. syscon1 = 3;
  178. omap_cfg_reg(USB1_VP);
  179. omap_cfg_reg(USB1_VM);
  180. if (!cpu_is_omap15xx())
  181. USB_TRANSCEIVER_CTRL_REG |= CONF_USB1_UNI_R;
  182. break;
  183. default:
  184. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  185. 1, nwires);
  186. }
  187. return syscon1 << 20;
  188. }
  189. static u32 __init omap_usb2_init(unsigned nwires, unsigned alt_pingroup)
  190. {
  191. u32 syscon1 = 0;
  192. /* NOTE erratum: must leave USB2_UNI_R set if usb0 in use */
  193. if (alt_pingroup || nwires == 0)
  194. return 0;
  195. if (nwires != 6 && !cpu_is_omap15xx())
  196. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
  197. /* external transceiver */
  198. if (cpu_is_omap15xx()) {
  199. omap_cfg_reg(USB2_TXD);
  200. omap_cfg_reg(USB2_TXEN);
  201. omap_cfg_reg(USB2_SEO);
  202. if (nwires != 3)
  203. omap_cfg_reg(USB2_RCV);
  204. /* there is no USB2_SPEED */
  205. } else if (cpu_is_omap16xx()) {
  206. omap_cfg_reg(V6_USB2_TXD);
  207. omap_cfg_reg(W9_USB2_TXEN);
  208. omap_cfg_reg(W5_USB2_SE0);
  209. if (nwires != 3)
  210. omap_cfg_reg(Y5_USB2_RCV);
  211. // FIXME omap_cfg_reg(USB2_SPEED);
  212. } else {
  213. pr_debug("usb unrecognized\n");
  214. }
  215. // omap_cfg_reg(USB2_SUSP);
  216. switch (nwires) {
  217. case 3:
  218. syscon1 = 2;
  219. break;
  220. case 4:
  221. syscon1 = 1;
  222. break;
  223. case 6:
  224. syscon1 = 3;
  225. if (cpu_is_omap15xx()) {
  226. omap_cfg_reg(USB2_VP);
  227. omap_cfg_reg(USB2_VM);
  228. } else {
  229. omap_cfg_reg(AA9_USB2_VP);
  230. omap_cfg_reg(R9_USB2_VM);
  231. USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
  232. }
  233. break;
  234. default:
  235. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  236. 2, nwires);
  237. }
  238. return syscon1 << 24;
  239. }
  240. #endif
  241. /*-------------------------------------------------------------------------*/
  242. #if defined(CONFIG_USB_GADGET_OMAP) || \
  243. defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
  244. (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
  245. static void usb_release(struct device *dev)
  246. {
  247. /* normally not freed */
  248. }
  249. #endif
  250. #ifdef CONFIG_USB_GADGET_OMAP
  251. static struct resource udc_resources[] = {
  252. /* order is significant! */
  253. { /* registers */
  254. .start = UDC_BASE,
  255. .end = UDC_BASE + 0xff,
  256. .flags = IORESOURCE_MEM,
  257. }, { /* general IRQ */
  258. .start = IH2_BASE + 20,
  259. .flags = IORESOURCE_IRQ,
  260. }, { /* PIO IRQ */
  261. .start = IH2_BASE + 30,
  262. .flags = IORESOURCE_IRQ,
  263. }, { /* SOF IRQ */
  264. .start = IH2_BASE + 29,
  265. .flags = IORESOURCE_IRQ,
  266. },
  267. };
  268. static u64 udc_dmamask = ~(u32)0;
  269. static struct platform_device udc_device = {
  270. .name = "omap_udc",
  271. .id = -1,
  272. .dev = {
  273. .release = usb_release,
  274. .dma_mask = &udc_dmamask,
  275. .coherent_dma_mask = 0xffffffff,
  276. },
  277. .num_resources = ARRAY_SIZE(udc_resources),
  278. .resource = udc_resources,
  279. };
  280. #endif
  281. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  282. /* The dmamask must be set for OHCI to work */
  283. static u64 ohci_dmamask = ~(u32)0;
  284. static struct resource ohci_resources[] = {
  285. {
  286. .start = OMAP_OHCI_BASE,
  287. .end = OMAP_OHCI_BASE + 4096 - 1,
  288. .flags = IORESOURCE_MEM,
  289. },
  290. {
  291. .start = INT_USB_HHC_1,
  292. .flags = IORESOURCE_IRQ,
  293. },
  294. };
  295. static struct platform_device ohci_device = {
  296. .name = "ohci",
  297. .id = -1,
  298. .dev = {
  299. .release = usb_release,
  300. .dma_mask = &ohci_dmamask,
  301. .coherent_dma_mask = 0xffffffff,
  302. },
  303. .num_resources = ARRAY_SIZE(ohci_resources),
  304. .resource = ohci_resources,
  305. };
  306. #endif
  307. #if defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
  308. static struct resource otg_resources[] = {
  309. /* order is significant! */
  310. {
  311. .start = OTG_BASE,
  312. .end = OTG_BASE + 0xff,
  313. .flags = IORESOURCE_MEM,
  314. }, {
  315. .start = IH2_BASE + 8,
  316. .flags = IORESOURCE_IRQ,
  317. },
  318. };
  319. static struct platform_device otg_device = {
  320. .name = "omap_otg",
  321. .id = -1,
  322. .dev = {
  323. .release = usb_release,
  324. },
  325. .num_resources = ARRAY_SIZE(otg_resources),
  326. .resource = otg_resources,
  327. };
  328. #endif
  329. /*-------------------------------------------------------------------------*/
  330. #define ULPD_CLOCK_CTRL_REG __REG16(ULPD_CLOCK_CTRL)
  331. #define ULPD_SOFT_REQ_REG __REG16(ULPD_SOFT_REQ)
  332. // FIXME correct answer depends on hmc_mode,
  333. // as does any nonzero value for config->otg port number
  334. #ifdef CONFIG_USB_GADGET_OMAP
  335. #define is_usb0_device(config) 1
  336. #else
  337. #define is_usb0_device(config) 0
  338. #endif
  339. /*-------------------------------------------------------------------------*/
  340. #ifdef CONFIG_ARCH_OMAP_OTG
  341. void __init
  342. omap_otg_init(struct omap_usb_config *config)
  343. {
  344. u32 syscon = OTG_SYSCON_1_REG & 0xffff;
  345. int status;
  346. int alt_pingroup = 0;
  347. /* NOTE: no bus or clock setup (yet?) */
  348. syscon = OTG_SYSCON_1_REG & 0xffff;
  349. if (!(syscon & OTG_RESET_DONE))
  350. pr_debug("USB resets not complete?\n");
  351. // OTG_IRQ_EN_REG = 0;
  352. /* pin muxing and transceiver pinouts */
  353. if (config->pins[0] > 2) /* alt pingroup 2 */
  354. alt_pingroup = 1;
  355. syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
  356. syscon |= omap_usb1_init(config->pins[1]);
  357. syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
  358. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  359. OTG_SYSCON_1_REG = syscon;
  360. syscon = config->hmc_mode;
  361. syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
  362. #ifdef CONFIG_USB_OTG
  363. if (config->otg)
  364. syscon |= OTG_EN;
  365. #endif
  366. pr_debug("USB_TRANSCEIVER_CTRL_REG = %03x\n", USB_TRANSCEIVER_CTRL_REG);
  367. pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
  368. OTG_SYSCON_2_REG = syscon;
  369. printk("USB: hmc %d", config->hmc_mode);
  370. if (alt_pingroup)
  371. printk(", usb2 alt %d wires", config->pins[2]);
  372. else if (config->pins[0])
  373. printk(", usb0 %d wires%s", config->pins[0],
  374. is_usb0_device(config) ? " (dev)" : "");
  375. if (config->pins[1])
  376. printk(", usb1 %d wires", config->pins[1]);
  377. if (!alt_pingroup && config->pins[2])
  378. printk(", usb2 %d wires", config->pins[2]);
  379. if (config->otg)
  380. printk(", Mini-AB on usb%d", config->otg - 1);
  381. printk("\n");
  382. /* leave USB clocks/controllers off until needed */
  383. ULPD_SOFT_REQ_REG &= ~SOFT_USB_CLK_REQ;
  384. ULPD_CLOCK_CTRL_REG &= ~USB_MCLK_EN;
  385. ULPD_CLOCK_CTRL_REG |= DIS_USB_PVCI_CLK;
  386. syscon = OTG_SYSCON_1_REG;
  387. syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
  388. #ifdef CONFIG_USB_GADGET_OMAP
  389. if (config->otg || config->register_dev) {
  390. syscon &= ~DEV_IDLE_EN;
  391. udc_device.dev.platform_data = config;
  392. /* FIXME patch IRQ numbers for omap730 */
  393. status = platform_device_register(&udc_device);
  394. if (status)
  395. pr_debug("can't register UDC device, %d\n", status);
  396. }
  397. #endif
  398. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  399. if (config->otg || config->register_host) {
  400. syscon &= ~HST_IDLE_EN;
  401. ohci_device.dev.platform_data = config;
  402. if (cpu_is_omap730())
  403. ohci_resources[1].start = INT_730_USB_HHC_1;
  404. status = platform_device_register(&ohci_device);
  405. if (status)
  406. pr_debug("can't register OHCI device, %d\n", status);
  407. }
  408. #endif
  409. #ifdef CONFIG_USB_OTG
  410. if (config->otg) {
  411. syscon &= ~OTG_IDLE_EN;
  412. otg_device.dev.platform_data = config;
  413. if (cpu_is_omap730())
  414. otg_resources[1].start = INT_730_USB_OTG;
  415. status = platform_device_register(&otg_device);
  416. if (status)
  417. pr_debug("can't register OTG device, %d\n", status);
  418. }
  419. #endif
  420. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  421. OTG_SYSCON_1_REG = syscon;
  422. status = 0;
  423. }
  424. #else
  425. static inline void omap_otg_init(struct omap_usb_config *config) {}
  426. #endif
  427. /*-------------------------------------------------------------------------*/
  428. #ifdef CONFIG_ARCH_OMAP15XX
  429. #define ULPD_DPLL_CTRL_REG __REG16(ULPD_DPLL_CTRL)
  430. #define DPLL_IOB (1 << 13)
  431. #define DPLL_PLL_ENABLE (1 << 4)
  432. #define DPLL_LOCK (1 << 0)
  433. #define ULPD_APLL_CTRL_REG __REG16(ULPD_APLL_CTRL)
  434. #define APLL_NDPLL_SWITCH (1 << 0)
  435. static void __init omap_1510_usb_init(struct omap_usb_config *config)
  436. {
  437. unsigned int val;
  438. omap_usb0_init(config->pins[0], is_usb0_device(config));
  439. omap_usb1_init(config->pins[1]);
  440. omap_usb2_init(config->pins[2], 0);
  441. val = omap_readl(MOD_CONF_CTRL_0) & ~(0x3f << 1);
  442. val |= (config->hmc_mode << 1);
  443. omap_writel(val, MOD_CONF_CTRL_0);
  444. printk("USB: hmc %d", config->hmc_mode);
  445. if (config->pins[0])
  446. printk(", usb0 %d wires%s", config->pins[0],
  447. is_usb0_device(config) ? " (dev)" : "");
  448. if (config->pins[1])
  449. printk(", usb1 %d wires", config->pins[1]);
  450. if (config->pins[2])
  451. printk(", usb2 %d wires", config->pins[2]);
  452. printk("\n");
  453. /* use DPLL for 48 MHz function clock */
  454. pr_debug("APLL %04x DPLL %04x REQ %04x\n", ULPD_APLL_CTRL_REG,
  455. ULPD_DPLL_CTRL_REG, ULPD_SOFT_REQ_REG);
  456. ULPD_APLL_CTRL_REG &= ~APLL_NDPLL_SWITCH;
  457. ULPD_DPLL_CTRL_REG |= DPLL_IOB | DPLL_PLL_ENABLE;
  458. ULPD_SOFT_REQ_REG |= SOFT_UDC_REQ | SOFT_DPLL_REQ;
  459. while (!(ULPD_DPLL_CTRL_REG & DPLL_LOCK))
  460. cpu_relax();
  461. #ifdef CONFIG_USB_GADGET_OMAP
  462. if (config->register_dev) {
  463. int status;
  464. udc_device.dev.platform_data = config;
  465. status = platform_device_register(&udc_device);
  466. if (status)
  467. pr_debug("can't register UDC device, %d\n", status);
  468. /* udc driver gates 48MHz by D+ pullup */
  469. }
  470. #endif
  471. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  472. if (config->register_host) {
  473. int status;
  474. ohci_device.dev.platform_data = config;
  475. status = platform_device_register(&ohci_device);
  476. if (status)
  477. pr_debug("can't register OHCI device, %d\n", status);
  478. /* hcd explicitly gates 48MHz */
  479. }
  480. #endif
  481. }
  482. #else
  483. static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
  484. #endif
  485. /*-------------------------------------------------------------------------*/
  486. static struct omap_usb_config platform_data;
  487. static int __init
  488. omap_usb_init(void)
  489. {
  490. const struct omap_usb_config *config;
  491. config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
  492. if (config == NULL) {
  493. printk(KERN_ERR "USB: No board-specific "
  494. "platform config found\n");
  495. return -ENODEV;
  496. }
  497. platform_data = *config;
  498. if (cpu_is_omap730() || cpu_is_omap16xx())
  499. omap_otg_init(&platform_data);
  500. else if (cpu_is_omap15xx())
  501. omap_1510_usb_init(&platform_data);
  502. else {
  503. printk(KERN_ERR "USB: No init for your chip yet\n");
  504. return -ENODEV;
  505. }
  506. return 0;
  507. }
  508. subsys_initcall(omap_usb_init);