usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/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. * Call omap_usb_init() once, from INIT_MACHINE().
  40. *
  41. * Some board-*.c files will need to set up additional mux options,
  42. * like for suspend handling, vbus sensing, GPIOs, and the D+ pullup.
  43. */
  44. /* TESTED ON:
  45. * - 1611B H2 (with usb1 mini-AB) using standard Mini-B or OTG cables
  46. * - 5912 OSK OHCI (with usb0 standard-A), standard A-to-B cables
  47. * - 5912 OSK UDC, with *nonstandard* A-to-A cable
  48. * - 1510 Innovator UDC with bundled usb0 cable
  49. * - 1510 Innovator OHCI with bundled usb1/usb2 cable
  50. * - 1510 Innovator OHCI with custom usb0 cable, feeding 5V VBUS
  51. * - 1710 custom development board using alternate pin group
  52. * - 1710 H3 (with usb1 mini-AB) using standard Mini-B or OTG cables
  53. */
  54. /*-------------------------------------------------------------------------*/
  55. #ifdef CONFIG_ARCH_OMAP_OTG
  56. static struct otg_transceiver *xceiv;
  57. /**
  58. * otg_get_transceiver - find the (single) OTG transceiver driver
  59. *
  60. * Returns the transceiver driver, after getting a refcount to it; or
  61. * null if there is no such transceiver. The caller is responsible for
  62. * releasing that count.
  63. */
  64. struct otg_transceiver *otg_get_transceiver(void)
  65. {
  66. if (xceiv)
  67. get_device(xceiv->dev);
  68. return xceiv;
  69. }
  70. EXPORT_SYMBOL(otg_get_transceiver);
  71. int otg_set_transceiver(struct otg_transceiver *x)
  72. {
  73. if (xceiv && x)
  74. return -EBUSY;
  75. xceiv = x;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(otg_set_transceiver);
  79. #endif
  80. /*-------------------------------------------------------------------------*/
  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. /*-------------------------------------------------------------------------*/
  241. #if defined(CONFIG_USB_GADGET_OMAP) || \
  242. defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
  243. (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
  244. static void usb_release(struct device *dev)
  245. {
  246. /* normally not freed */
  247. }
  248. #endif
  249. #ifdef CONFIG_USB_GADGET_OMAP
  250. static struct resource udc_resources[] = {
  251. /* order is significant! */
  252. { /* registers */
  253. .start = UDC_BASE,
  254. .end = UDC_BASE + 0xff,
  255. .flags = IORESOURCE_MEM,
  256. }, { /* general IRQ */
  257. .start = IH2_BASE + 20,
  258. .flags = IORESOURCE_IRQ,
  259. }, { /* PIO IRQ */
  260. .start = IH2_BASE + 30,
  261. .flags = IORESOURCE_IRQ,
  262. }, { /* SOF IRQ */
  263. .start = IH2_BASE + 29,
  264. .flags = IORESOURCE_IRQ,
  265. },
  266. };
  267. static u64 udc_dmamask = ~(u32)0;
  268. static struct platform_device udc_device = {
  269. .name = "omap_udc",
  270. .id = -1,
  271. .dev = {
  272. .release = usb_release,
  273. .dma_mask = &udc_dmamask,
  274. .coherent_dma_mask = 0xffffffff,
  275. },
  276. .num_resources = ARRAY_SIZE(udc_resources),
  277. .resource = udc_resources,
  278. };
  279. #endif
  280. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  281. /* The dmamask must be set for OHCI to work */
  282. static u64 ohci_dmamask = ~(u32)0;
  283. static struct resource ohci_resources[] = {
  284. {
  285. .start = OMAP_OHCI_BASE,
  286. .end = OMAP_OHCI_BASE + 4096 - 1,
  287. .flags = IORESOURCE_MEM,
  288. },
  289. {
  290. .start = INT_USB_HHC_1,
  291. .flags = IORESOURCE_IRQ,
  292. },
  293. };
  294. static struct platform_device ohci_device = {
  295. .name = "ohci",
  296. .id = -1,
  297. .dev = {
  298. .release = usb_release,
  299. .dma_mask = &ohci_dmamask,
  300. .coherent_dma_mask = 0xffffffff,
  301. },
  302. .num_resources = ARRAY_SIZE(ohci_resources),
  303. .resource = ohci_resources,
  304. };
  305. #endif
  306. #if defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
  307. static struct resource otg_resources[] = {
  308. /* order is significant! */
  309. {
  310. .start = OTG_BASE,
  311. .end = OTG_BASE + 0xff,
  312. .flags = IORESOURCE_MEM,
  313. }, {
  314. .start = IH2_BASE + 8,
  315. .flags = IORESOURCE_IRQ,
  316. },
  317. };
  318. static struct platform_device otg_device = {
  319. .name = "omap_otg",
  320. .id = -1,
  321. .dev = {
  322. .release = usb_release,
  323. },
  324. .num_resources = ARRAY_SIZE(otg_resources),
  325. .resource = otg_resources,
  326. };
  327. #endif
  328. /*-------------------------------------------------------------------------*/
  329. #define ULPD_CLOCK_CTRL_REG __REG16(ULPD_CLOCK_CTRL)
  330. #define ULPD_SOFT_REQ_REG __REG16(ULPD_SOFT_REQ)
  331. // FIXME correct answer depends on hmc_mode,
  332. // as does any nonzero value for config->otg port number
  333. #ifdef CONFIG_USB_GADGET_OMAP
  334. #define is_usb0_device(config) 1
  335. #else
  336. #define is_usb0_device(config) 0
  337. #endif
  338. /*-------------------------------------------------------------------------*/
  339. #ifdef CONFIG_ARCH_OMAP_OTG
  340. void __init
  341. omap_otg_init(struct omap_usb_config *config)
  342. {
  343. u32 syscon = OTG_SYSCON_1_REG & 0xffff;
  344. int status;
  345. int alt_pingroup = 0;
  346. /* NOTE: no bus or clock setup (yet?) */
  347. syscon = OTG_SYSCON_1_REG & 0xffff;
  348. if (!(syscon & OTG_RESET_DONE))
  349. pr_debug("USB resets not complete?\n");
  350. // OTG_IRQ_EN_REG = 0;
  351. /* pin muxing and transceiver pinouts */
  352. if (config->pins[0] > 2) /* alt pingroup 2 */
  353. alt_pingroup = 1;
  354. syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
  355. syscon |= omap_usb1_init(config->pins[1]);
  356. syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
  357. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  358. OTG_SYSCON_1_REG = syscon;
  359. syscon = config->hmc_mode;
  360. syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
  361. #ifdef CONFIG_USB_OTG
  362. if (config->otg)
  363. syscon |= OTG_EN;
  364. #endif
  365. pr_debug("USB_TRANSCEIVER_CTRL_REG = %03x\n", USB_TRANSCEIVER_CTRL_REG);
  366. pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
  367. OTG_SYSCON_2_REG = syscon;
  368. printk("USB: hmc %d", config->hmc_mode);
  369. if (alt_pingroup)
  370. printk(", usb2 alt %d wires", config->pins[2]);
  371. else if (config->pins[0])
  372. printk(", usb0 %d wires%s", config->pins[0],
  373. is_usb0_device(config) ? " (dev)" : "");
  374. if (config->pins[1])
  375. printk(", usb1 %d wires", config->pins[1]);
  376. if (!alt_pingroup && config->pins[2])
  377. printk(", usb2 %d wires", config->pins[2]);
  378. if (config->otg)
  379. printk(", Mini-AB on usb%d", config->otg - 1);
  380. printk("\n");
  381. /* leave USB clocks/controllers off until needed */
  382. ULPD_SOFT_REQ_REG &= ~SOFT_USB_CLK_REQ;
  383. ULPD_CLOCK_CTRL_REG &= ~USB_MCLK_EN;
  384. ULPD_CLOCK_CTRL_REG |= DIS_USB_PVCI_CLK;
  385. syscon = OTG_SYSCON_1_REG;
  386. syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
  387. #ifdef CONFIG_USB_GADGET_OMAP
  388. if (config->otg || config->register_dev) {
  389. syscon &= ~DEV_IDLE_EN;
  390. udc_device.dev.platform_data = config;
  391. /* FIXME patch IRQ numbers for omap730 */
  392. status = platform_device_register(&udc_device);
  393. if (status)
  394. pr_debug("can't register UDC device, %d\n", status);
  395. }
  396. #endif
  397. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  398. if (config->otg || config->register_host) {
  399. syscon &= ~HST_IDLE_EN;
  400. ohci_device.dev.platform_data = config;
  401. if (cpu_is_omap730())
  402. ohci_resources[1].start = INT_730_USB_HHC_1;
  403. status = platform_device_register(&ohci_device);
  404. if (status)
  405. pr_debug("can't register OHCI device, %d\n", status);
  406. }
  407. #endif
  408. #ifdef CONFIG_USB_OTG
  409. if (config->otg) {
  410. syscon &= ~OTG_IDLE_EN;
  411. otg_device.dev.platform_data = config;
  412. if (cpu_is_omap730())
  413. otg_resources[1].start = INT_730_USB_OTG;
  414. status = platform_device_register(&otg_device);
  415. if (status)
  416. pr_debug("can't register OTG device, %d\n", status);
  417. }
  418. #endif
  419. pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
  420. OTG_SYSCON_1_REG = syscon;
  421. status = 0;
  422. }
  423. #else
  424. static inline void omap_otg_init(struct omap_usb_config *config) {}
  425. #endif
  426. /*-------------------------------------------------------------------------*/
  427. #ifdef CONFIG_ARCH_OMAP1510
  428. #define ULPD_DPLL_CTRL_REG __REG16(ULPD_DPLL_CTRL)
  429. #define DPLL_IOB (1 << 13)
  430. #define DPLL_PLL_ENABLE (1 << 4)
  431. #define DPLL_LOCK (1 << 0)
  432. #define ULPD_APLL_CTRL_REG __REG16(ULPD_APLL_CTRL)
  433. #define APLL_NDPLL_SWITCH (1 << 0)
  434. static void __init omap_1510_usb_init(struct omap_usb_config *config)
  435. {
  436. int status;
  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. udc_device.dev.platform_data = config;
  464. status = platform_device_register(&udc_device);
  465. if (status)
  466. pr_debug("can't register UDC device, %d\n", status);
  467. /* udc driver gates 48MHz by D+ pullup */
  468. }
  469. #endif
  470. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  471. if (config->register_host) {
  472. ohci_device.dev.platform_data = config;
  473. status = platform_device_register(&ohci_device);
  474. if (status)
  475. pr_debug("can't register OHCI device, %d\n", status);
  476. /* hcd explicitly gates 48MHz */
  477. }
  478. #endif
  479. }
  480. #else
  481. static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
  482. #endif
  483. /*-------------------------------------------------------------------------*/
  484. static struct omap_usb_config platform_data;
  485. static int __init
  486. omap_usb_init(void)
  487. {
  488. const struct omap_usb_config *config;
  489. config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
  490. if (config == NULL) {
  491. printk(KERN_ERR "USB: No board-specific "
  492. "platform config found\n");
  493. return -ENODEV;
  494. }
  495. platform_data = *config;
  496. if (cpu_is_omap730() || cpu_is_omap16xx())
  497. omap_otg_init(&platform_data);
  498. else if (cpu_is_omap15xx())
  499. omap_1510_usb_init(&platform_data);
  500. else {
  501. printk(KERN_ERR "USB: No init for your chip yet\n");
  502. return -ENODEV;
  503. }
  504. return 0;
  505. }
  506. subsys_initcall(omap_usb_init);