usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/usb/otg.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/system.h>
  31. #include <asm/hardware.h>
  32. #include <asm/arch/mux.h>
  33. #include <asm/arch/usb.h>
  34. #include <asm/arch/board.h>
  35. /* These routines should handle the standard chip-specific modes
  36. * for usb0/1/2 ports, covering basic mux and transceiver setup.
  37. * Call omap_usb_init() once, from INIT_MACHINE().
  38. *
  39. * Some board-*.c files will need to set up additional mux options,
  40. * like for suspend handling, vbus sensing, GPIOs, and the D+ pullup.
  41. */
  42. /* TESTED ON:
  43. * - 1611B H2 (with usb1 mini-AB) using standard Mini-B or OTG cables
  44. * - 5912 OSK OHCI (with usb0 standard-A), standard A-to-B cables
  45. * - 5912 OSK UDC, with *nonstandard* A-to-A cable
  46. * - 1510 Innovator UDC with bundled usb0 cable
  47. * - 1510 Innovator OHCI with bundled usb1/usb2 cable
  48. * - 1510 Innovator OHCI with custom usb0 cable, feeding 5V VBUS
  49. * - 1710 custom development board using alternate pin group
  50. * - 1710 H3 (with usb1 mini-AB) using standard Mini-B or OTG cables
  51. */
  52. /*-------------------------------------------------------------------------*/
  53. #ifdef CONFIG_ARCH_OMAP_OTG
  54. static struct otg_transceiver *xceiv;
  55. /**
  56. * otg_get_transceiver - find the (single) OTG transceiver driver
  57. *
  58. * Returns the transceiver driver, after getting a refcount to it; or
  59. * null if there is no such transceiver. The caller is responsible for
  60. * releasing that count.
  61. */
  62. struct otg_transceiver *otg_get_transceiver(void)
  63. {
  64. if (xceiv)
  65. get_device(xceiv->dev);
  66. return xceiv;
  67. }
  68. EXPORT_SYMBOL(otg_get_transceiver);
  69. int otg_set_transceiver(struct otg_transceiver *x)
  70. {
  71. if (xceiv && x)
  72. return -EBUSY;
  73. xceiv = x;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(otg_set_transceiver);
  77. #endif
  78. /*-------------------------------------------------------------------------*/
  79. #if defined(CONFIG_ARCH_OMAP_OTG) || defined(CONFIG_ARCH_OMAP15XX)
  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. #endif
  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_OMAP15XX
  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. 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. int status;
  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. int status;
  473. ohci_device.dev.platform_data = config;
  474. status = platform_device_register(&ohci_device);
  475. if (status)
  476. pr_debug("can't register OHCI device, %d\n", status);
  477. /* hcd explicitly gates 48MHz */
  478. }
  479. #endif
  480. }
  481. #else
  482. static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
  483. #endif
  484. /*-------------------------------------------------------------------------*/
  485. static struct omap_usb_config platform_data;
  486. static int __init
  487. omap_usb_init(void)
  488. {
  489. const struct omap_usb_config *config;
  490. config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
  491. if (config == NULL) {
  492. printk(KERN_ERR "USB: No board-specific "
  493. "platform config found\n");
  494. return -ENODEV;
  495. }
  496. platform_data = *config;
  497. if (cpu_is_omap730() || cpu_is_omap16xx())
  498. omap_otg_init(&platform_data);
  499. else if (cpu_is_omap15xx())
  500. omap_1510_usb_init(&platform_data);
  501. else {
  502. printk(KERN_ERR "USB: No init for your chip yet\n");
  503. return -ENODEV;
  504. }
  505. return 0;
  506. }
  507. subsys_initcall(omap_usb_init);