usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * arch/arm/mach-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/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/mach-types.h>
  34. #include <asm/arch/mux.h>
  35. #include <asm/arch/usb.h>
  36. #include <asm/arch/board.h>
  37. /* These routines should handle the standard chip-specific modes
  38. * for usb0/1/2 ports, covering basic mux and transceiver setup.
  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. static u32 __init omap_usb0_init(unsigned nwires, unsigned is_device)
  81. {
  82. u32 syscon1 = 0;
  83. if (nwires == 0) {
  84. if (!cpu_is_omap15xx()) {
  85. /* pulldown D+/D- */
  86. USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
  87. }
  88. return 0;
  89. }
  90. if (is_device)
  91. omap_cfg_reg(W4_USB_PUEN);
  92. /* internal transceiver */
  93. if (nwires == 2) {
  94. // omap_cfg_reg(P9_USB_DP);
  95. // omap_cfg_reg(R8_USB_DM);
  96. if (cpu_is_omap15xx()) {
  97. /* This works on 1510-Innovator */
  98. return 0;
  99. }
  100. /* NOTES:
  101. * - peripheral should configure VBUS detection!
  102. * - only peripherals may use the internal D+/D- pulldowns
  103. * - OTG support on this port not yet written
  104. */
  105. USB_TRANSCEIVER_CTRL_REG &= ~(7 << 4);
  106. if (!is_device)
  107. USB_TRANSCEIVER_CTRL_REG |= (3 << 1);
  108. return 3 << 16;
  109. }
  110. /* alternate pin config, external transceiver */
  111. if (cpu_is_omap15xx()) {
  112. printk(KERN_ERR "no usb0 alt pin config on 15xx\n");
  113. return 0;
  114. }
  115. omap_cfg_reg(V6_USB0_TXD);
  116. omap_cfg_reg(W9_USB0_TXEN);
  117. omap_cfg_reg(W5_USB0_SE0);
  118. /* NOTE: SPEED and SUSP aren't configured here */
  119. if (nwires != 3)
  120. omap_cfg_reg(Y5_USB0_RCV);
  121. if (nwires != 6)
  122. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
  123. switch (nwires) {
  124. case 3:
  125. syscon1 = 2;
  126. break;
  127. case 4:
  128. syscon1 = 1;
  129. break;
  130. case 6:
  131. syscon1 = 3;
  132. omap_cfg_reg(AA9_USB0_VP);
  133. omap_cfg_reg(R9_USB0_VM);
  134. USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
  135. break;
  136. default:
  137. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  138. 0, nwires);
  139. }
  140. return syscon1 << 16;
  141. }
  142. static u32 __init omap_usb1_init(unsigned nwires)
  143. {
  144. u32 syscon1 = 0;
  145. if (nwires != 6 && !cpu_is_omap15xx())
  146. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB1_UNI_R;
  147. if (nwires == 0)
  148. return 0;
  149. /* external transceiver */
  150. omap_cfg_reg(USB1_TXD);
  151. omap_cfg_reg(USB1_TXEN);
  152. if (cpu_is_omap15xx()) {
  153. omap_cfg_reg(USB1_SEO);
  154. omap_cfg_reg(USB1_SPEED);
  155. // SUSP
  156. } else if (cpu_is_omap1610() || cpu_is_omap5912()) {
  157. omap_cfg_reg(W13_1610_USB1_SE0);
  158. omap_cfg_reg(R13_1610_USB1_SPEED);
  159. // SUSP
  160. } else if (cpu_is_omap1710()) {
  161. omap_cfg_reg(R13_1710_USB1_SE0);
  162. // SUSP
  163. } else {
  164. pr_debug("usb unrecognized\n");
  165. }
  166. if (nwires != 3)
  167. omap_cfg_reg(USB1_RCV);
  168. switch (nwires) {
  169. case 3:
  170. syscon1 = 2;
  171. break;
  172. case 4:
  173. syscon1 = 1;
  174. break;
  175. case 6:
  176. syscon1 = 3;
  177. omap_cfg_reg(USB1_VP);
  178. omap_cfg_reg(USB1_VM);
  179. if (!cpu_is_omap15xx())
  180. USB_TRANSCEIVER_CTRL_REG |= CONF_USB1_UNI_R;
  181. break;
  182. default:
  183. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  184. 1, nwires);
  185. }
  186. return syscon1 << 20;
  187. }
  188. static u32 __init omap_usb2_init(unsigned nwires, unsigned alt_pingroup)
  189. {
  190. u32 syscon1 = 0;
  191. /* NOTE erratum: must leave USB2_UNI_R set if usb0 in use */
  192. if (alt_pingroup || nwires == 0)
  193. return 0;
  194. if (nwires != 6 && !cpu_is_omap15xx())
  195. USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
  196. /* external transceiver */
  197. if (cpu_is_omap15xx()) {
  198. omap_cfg_reg(USB2_TXD);
  199. omap_cfg_reg(USB2_TXEN);
  200. omap_cfg_reg(USB2_SEO);
  201. if (nwires != 3)
  202. omap_cfg_reg(USB2_RCV);
  203. /* there is no USB2_SPEED */
  204. } else if (cpu_is_omap16xx()) {
  205. omap_cfg_reg(V6_USB2_TXD);
  206. omap_cfg_reg(W9_USB2_TXEN);
  207. omap_cfg_reg(W5_USB2_SE0);
  208. if (nwires != 3)
  209. omap_cfg_reg(Y5_USB2_RCV);
  210. // FIXME omap_cfg_reg(USB2_SPEED);
  211. } else {
  212. pr_debug("usb unrecognized\n");
  213. }
  214. // omap_cfg_reg(USB2_SUSP);
  215. switch (nwires) {
  216. case 3:
  217. syscon1 = 2;
  218. break;
  219. case 4:
  220. syscon1 = 1;
  221. break;
  222. case 6:
  223. syscon1 = 3;
  224. if (cpu_is_omap15xx()) {
  225. omap_cfg_reg(USB2_VP);
  226. omap_cfg_reg(USB2_VM);
  227. } else {
  228. omap_cfg_reg(AA9_USB2_VP);
  229. omap_cfg_reg(R9_USB2_VM);
  230. USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
  231. }
  232. break;
  233. default:
  234. printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
  235. 2, nwires);
  236. }
  237. return syscon1 << 24;
  238. }
  239. /*-------------------------------------------------------------------------*/
  240. #if defined(CONFIG_USB_GADGET_OMAP) || \
  241. defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
  242. (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
  243. static void usb_release(struct device *dev)
  244. {
  245. /* normally not freed */
  246. }
  247. #endif
  248. #ifdef CONFIG_USB_GADGET_OMAP
  249. static struct resource udc_resources[] = {
  250. /* order is significant! */
  251. { /* registers */
  252. .start = UDC_BASE,
  253. .end = UDC_BASE + 0xff,
  254. .flags = IORESOURCE_MEM,
  255. }, { /* general IRQ */
  256. .start = IH2_BASE + 20,
  257. .flags = IORESOURCE_IRQ,
  258. }, { /* PIO IRQ */
  259. .start = IH2_BASE + 30,
  260. .flags = IORESOURCE_IRQ,
  261. }, { /* SOF IRQ */
  262. .start = IH2_BASE + 29,
  263. .flags = IORESOURCE_IRQ,
  264. },
  265. };
  266. static u64 udc_dmamask = ~(u32)0;
  267. static struct platform_device udc_device = {
  268. .name = "omap_udc",
  269. .id = -1,
  270. .dev = {
  271. .release = usb_release,
  272. .dma_mask = &udc_dmamask,
  273. .coherent_dma_mask = 0xffffffff,
  274. },
  275. .num_resources = ARRAY_SIZE(udc_resources),
  276. .resource = udc_resources,
  277. };
  278. #endif
  279. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  280. /* The dmamask must be set for OHCI to work */
  281. static u64 ohci_dmamask = ~(u32)0;
  282. static struct resource ohci_resources[] = {
  283. {
  284. .start = OMAP_OHCI_BASE,
  285. .end = OMAP_OHCI_BASE + 4096,
  286. .flags = IORESOURCE_MEM,
  287. },
  288. {
  289. .start = INT_USB_HHC_1,
  290. .flags = IORESOURCE_IRQ,
  291. },
  292. };
  293. static struct platform_device ohci_device = {
  294. .name = "ohci",
  295. .id = -1,
  296. .dev = {
  297. .release = usb_release,
  298. .dma_mask = &ohci_dmamask,
  299. .coherent_dma_mask = 0xffffffff,
  300. },
  301. .num_resources = ARRAY_SIZE(ohci_resources),
  302. .resource = ohci_resources,
  303. };
  304. #endif
  305. #if defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
  306. static struct resource otg_resources[] = {
  307. /* order is significant! */
  308. {
  309. .start = OTG_BASE,
  310. .end = OTG_BASE + 0xff,
  311. .flags = IORESOURCE_MEM,
  312. }, {
  313. .start = IH2_BASE + 8,
  314. .flags = IORESOURCE_IRQ,
  315. },
  316. };
  317. static struct platform_device otg_device = {
  318. .name = "omap_otg",
  319. .id = -1,
  320. .dev = {
  321. .release = usb_release,
  322. },
  323. .num_resources = ARRAY_SIZE(otg_resources),
  324. .resource = otg_resources,
  325. };
  326. #endif
  327. /*-------------------------------------------------------------------------*/
  328. #define ULPD_CLOCK_CTRL_REG __REG16(ULPD_CLOCK_CTRL)
  329. #define ULPD_SOFT_REQ_REG __REG16(ULPD_SOFT_REQ)
  330. // FIXME correct answer depends on hmc_mode,
  331. // as does any nonzero value for config->otg port number
  332. #ifdef CONFIG_USB_GADGET_OMAP
  333. #define is_usb0_device(config) 1
  334. #else
  335. #define is_usb0_device(config) 0
  336. #endif
  337. /*-------------------------------------------------------------------------*/
  338. #ifdef CONFIG_ARCH_OMAP_OTG
  339. void __init
  340. omap_otg_init(struct omap_usb_config *config)
  341. {
  342. u32 syscon = OTG_SYSCON_1_REG & 0xffff;
  343. int status;
  344. int alt_pingroup = 0;
  345. /* NOTE: no bus or clock setup (yet?) */
  346. syscon = OTG_SYSCON_1_REG & 0xffff;
  347. if (!(syscon & OTG_RESET_DONE))
  348. pr_debug("USB resets not complete?\n");
  349. // OTG_IRQ_EN_REG = 0;
  350. /* pin muxing and transceiver pinouts */
  351. if (config->pins[0] > 2) /* alt pingroup 2 */
  352. alt_pingroup = 1;
  353. syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
  354. syscon |= omap_usb1_init(config->pins[1]);
  355. syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
  356. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  357. OTG_SYSCON_1_REG = syscon;
  358. syscon = config->hmc_mode;
  359. syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
  360. #ifdef CONFIG_USB_OTG
  361. if (config->otg)
  362. syscon |= OTG_EN;
  363. #endif
  364. pr_debug("USB_TRANSCEIVER_CTRL_REG = %03x\n", USB_TRANSCEIVER_CTRL_REG);
  365. pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
  366. OTG_SYSCON_2_REG = syscon;
  367. printk("USB: hmc %d", config->hmc_mode);
  368. if (alt_pingroup)
  369. printk(", usb2 alt %d wires", config->pins[2]);
  370. else if (config->pins[0])
  371. printk(", usb0 %d wires%s", config->pins[0],
  372. is_usb0_device(config) ? " (dev)" : "");
  373. if (config->pins[1])
  374. printk(", usb1 %d wires", config->pins[1]);
  375. if (!alt_pingroup && config->pins[2])
  376. printk(", usb2 %d wires", config->pins[2]);
  377. if (config->otg)
  378. printk(", Mini-AB on usb%d", config->otg - 1);
  379. printk("\n");
  380. /* leave USB clocks/controllers off until needed */
  381. ULPD_SOFT_REQ_REG &= ~SOFT_USB_CLK_REQ;
  382. ULPD_CLOCK_CTRL_REG &= ~USB_MCLK_EN;
  383. ULPD_CLOCK_CTRL_REG |= DIS_USB_PVCI_CLK;
  384. syscon = OTG_SYSCON_1_REG;
  385. syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
  386. #ifdef CONFIG_USB_GADGET_OMAP
  387. if (config->otg || config->register_dev) {
  388. syscon &= ~DEV_IDLE_EN;
  389. udc_device.dev.platform_data = config;
  390. /* FIXME patch IRQ numbers for omap730 */
  391. status = platform_device_register(&udc_device);
  392. if (status)
  393. pr_debug("can't register UDC device, %d\n", status);
  394. }
  395. #endif
  396. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  397. if (config->otg || config->register_host) {
  398. syscon &= ~HST_IDLE_EN;
  399. ohci_device.dev.platform_data = config;
  400. if (cpu_is_omap730())
  401. ohci_resources[1].start = INT_730_USB_HHC_1;
  402. status = platform_device_register(&ohci_device);
  403. if (status)
  404. pr_debug("can't register OHCI device, %d\n", status);
  405. }
  406. #endif
  407. #ifdef CONFIG_USB_OTG
  408. if (config->otg) {
  409. syscon &= ~OTG_IDLE_EN;
  410. otg_device.dev.platform_data = config;
  411. if (cpu_is_omap730())
  412. otg_resources[1].start = INT_730_USB_OTG;
  413. status = platform_device_register(&otg_device);
  414. if (status)
  415. pr_debug("can't register OTG device, %d\n", status);
  416. }
  417. #endif
  418. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  419. OTG_SYSCON_1_REG = syscon;
  420. status = 0;
  421. }
  422. #else
  423. static inline void omap_otg_init(struct omap_usb_config *config) {}
  424. #endif
  425. /*-------------------------------------------------------------------------*/
  426. #ifdef CONFIG_ARCH_OMAP1510
  427. #define ULPD_DPLL_CTRL_REG __REG16(ULPD_DPLL_CTRL)
  428. #define DPLL_IOB (1 << 13)
  429. #define DPLL_PLL_ENABLE (1 << 4)
  430. #define DPLL_LOCK (1 << 0)
  431. #define ULPD_APLL_CTRL_REG __REG16(ULPD_APLL_CTRL)
  432. #define APLL_NDPLL_SWITCH (1 << 0)
  433. static void __init omap_1510_usb_init(struct omap_usb_config *config)
  434. {
  435. int status;
  436. unsigned int val;
  437. omap_usb0_init(config->pins[0], is_usb0_device(config));
  438. omap_usb1_init(config->pins[1]);
  439. omap_usb2_init(config->pins[2], 0);
  440. val = omap_readl(MOD_CONF_CTRL_0) & ~(0x3f << 1);
  441. val |= (config->hmc_mode << 1);
  442. omap_writel(val, MOD_CONF_CTRL_0);
  443. printk("USB: hmc %d", config->hmc_mode);
  444. if (config->pins[0])
  445. printk(", usb0 %d wires%s", config->pins[0],
  446. is_usb0_device(config) ? " (dev)" : "");
  447. if (config->pins[1])
  448. printk(", usb1 %d wires", config->pins[1]);
  449. if (config->pins[2])
  450. printk(", usb2 %d wires", config->pins[2]);
  451. printk("\n");
  452. /* use DPLL for 48 MHz function clock */
  453. pr_debug("APLL %04x DPLL %04x REQ %04x\n", ULPD_APLL_CTRL_REG,
  454. ULPD_DPLL_CTRL_REG, ULPD_SOFT_REQ_REG);
  455. ULPD_APLL_CTRL_REG &= ~APLL_NDPLL_SWITCH;
  456. ULPD_DPLL_CTRL_REG |= DPLL_IOB | DPLL_PLL_ENABLE;
  457. ULPD_SOFT_REQ_REG |= SOFT_UDC_REQ | SOFT_DPLL_REQ;
  458. while (!(ULPD_DPLL_CTRL_REG & DPLL_LOCK))
  459. cpu_relax();
  460. #ifdef CONFIG_USB_GADGET_OMAP
  461. if (config->register_dev) {
  462. udc_device.dev.platform_data = config;
  463. status = platform_device_register(&udc_device);
  464. if (status)
  465. pr_debug("can't register UDC device, %d\n", status);
  466. /* udc driver gates 48MHz by D+ pullup */
  467. }
  468. #endif
  469. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  470. if (config->register_host) {
  471. ohci_device.dev.platform_data = config;
  472. status = platform_device_register(&ohci_device);
  473. if (status)
  474. pr_debug("can't register OHCI device, %d\n", status);
  475. /* hcd explicitly gates 48MHz */
  476. }
  477. #endif
  478. }
  479. #else
  480. static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
  481. #endif
  482. /*-------------------------------------------------------------------------*/
  483. static struct omap_usb_config platform_data;
  484. static int __init
  485. omap_usb_init(void)
  486. {
  487. const struct omap_usb_config *config;
  488. config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
  489. if (config == NULL) {
  490. printk(KERN_ERR "USB: No board-specific "
  491. "platform config found\n");
  492. return -ENODEV;
  493. }
  494. platform_data = *config;
  495. if (cpu_is_omap730() || cpu_is_omap16xx())
  496. omap_otg_init(&platform_data);
  497. else if (cpu_is_omap15xx())
  498. omap_1510_usb_init(&platform_data);
  499. else {
  500. printk(KERN_ERR "USB: No init for your chip yet\n");
  501. return -ENODEV;
  502. }
  503. return 0;
  504. }
  505. subsys_initcall(omap_usb_init);