ohci-pxa27x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  6. * (C) Copyright 2002 Hewlett-Packard Company
  7. *
  8. * Bus Glue for pxa27x
  9. *
  10. * Written by Christopher Hoover <ch@hpl.hp.com>
  11. * Based on fragments of previous driver by Russell King et al.
  12. *
  13. * Modified for LH7A404 from ohci-sa1111.c
  14. * by Durgesh Pattamatta <pattamattad@sharpsec.com>
  15. *
  16. * Modified for pxa27x from ohci-lh7a404.c
  17. * by Nick Bane <nick@cecomputing.co.uk> 26-8-2004
  18. *
  19. * This file is licenced under the GPL.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/signal.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/clk.h>
  25. #include <mach/ohci.h>
  26. #include <mach/pxa3xx-u2d.h>
  27. /*
  28. * UHC: USB Host Controller (OHCI-like) register definitions
  29. */
  30. #define UHCREV (0x0000) /* UHC HCI Spec Revision */
  31. #define UHCHCON (0x0004) /* UHC Host Control Register */
  32. #define UHCCOMS (0x0008) /* UHC Command Status Register */
  33. #define UHCINTS (0x000C) /* UHC Interrupt Status Register */
  34. #define UHCINTE (0x0010) /* UHC Interrupt Enable */
  35. #define UHCINTD (0x0014) /* UHC Interrupt Disable */
  36. #define UHCHCCA (0x0018) /* UHC Host Controller Comm. Area */
  37. #define UHCPCED (0x001C) /* UHC Period Current Endpt Descr */
  38. #define UHCCHED (0x0020) /* UHC Control Head Endpt Descr */
  39. #define UHCCCED (0x0024) /* UHC Control Current Endpt Descr */
  40. #define UHCBHED (0x0028) /* UHC Bulk Head Endpt Descr */
  41. #define UHCBCED (0x002C) /* UHC Bulk Current Endpt Descr */
  42. #define UHCDHEAD (0x0030) /* UHC Done Head */
  43. #define UHCFMI (0x0034) /* UHC Frame Interval */
  44. #define UHCFMR (0x0038) /* UHC Frame Remaining */
  45. #define UHCFMN (0x003C) /* UHC Frame Number */
  46. #define UHCPERS (0x0040) /* UHC Periodic Start */
  47. #define UHCLS (0x0044) /* UHC Low Speed Threshold */
  48. #define UHCRHDA (0x0048) /* UHC Root Hub Descriptor A */
  49. #define UHCRHDA_NOCP (1 << 12) /* No over current protection */
  50. #define UHCRHDA_OCPM (1 << 11) /* Over Current Protection Mode */
  51. #define UHCRHDA_POTPGT(x) \
  52. (((x) & 0xff) << 24) /* Power On To Power Good Time */
  53. #define UHCRHDB (0x004C) /* UHC Root Hub Descriptor B */
  54. #define UHCRHS (0x0050) /* UHC Root Hub Status */
  55. #define UHCRHPS1 (0x0054) /* UHC Root Hub Port 1 Status */
  56. #define UHCRHPS2 (0x0058) /* UHC Root Hub Port 2 Status */
  57. #define UHCRHPS3 (0x005C) /* UHC Root Hub Port 3 Status */
  58. #define UHCSTAT (0x0060) /* UHC Status Register */
  59. #define UHCSTAT_UPS3 (1 << 16) /* USB Power Sense Port3 */
  60. #define UHCSTAT_SBMAI (1 << 15) /* System Bus Master Abort Interrupt*/
  61. #define UHCSTAT_SBTAI (1 << 14) /* System Bus Target Abort Interrupt*/
  62. #define UHCSTAT_UPRI (1 << 13) /* USB Port Resume Interrupt */
  63. #define UHCSTAT_UPS2 (1 << 12) /* USB Power Sense Port 2 */
  64. #define UHCSTAT_UPS1 (1 << 11) /* USB Power Sense Port 1 */
  65. #define UHCSTAT_HTA (1 << 10) /* HCI Target Abort */
  66. #define UHCSTAT_HBA (1 << 8) /* HCI Buffer Active */
  67. #define UHCSTAT_RWUE (1 << 7) /* HCI Remote Wake Up Event */
  68. #define UHCHR (0x0064) /* UHC Reset Register */
  69. #define UHCHR_SSEP3 (1 << 11) /* Sleep Standby Enable for Port3 */
  70. #define UHCHR_SSEP2 (1 << 10) /* Sleep Standby Enable for Port2 */
  71. #define UHCHR_SSEP1 (1 << 9) /* Sleep Standby Enable for Port1 */
  72. #define UHCHR_PCPL (1 << 7) /* Power control polarity low */
  73. #define UHCHR_PSPL (1 << 6) /* Power sense polarity low */
  74. #define UHCHR_SSE (1 << 5) /* Sleep Standby Enable */
  75. #define UHCHR_UIT (1 << 4) /* USB Interrupt Test */
  76. #define UHCHR_SSDC (1 << 3) /* Simulation Scale Down Clock */
  77. #define UHCHR_CGR (1 << 2) /* Clock Generation Reset */
  78. #define UHCHR_FHR (1 << 1) /* Force Host Controller Reset */
  79. #define UHCHR_FSBIR (1 << 0) /* Force System Bus Iface Reset */
  80. #define UHCHIE (0x0068) /* UHC Interrupt Enable Register*/
  81. #define UHCHIE_UPS3IE (1 << 14) /* Power Sense Port3 IntEn */
  82. #define UHCHIE_UPRIE (1 << 13) /* Port Resume IntEn */
  83. #define UHCHIE_UPS2IE (1 << 12) /* Power Sense Port2 IntEn */
  84. #define UHCHIE_UPS1IE (1 << 11) /* Power Sense Port1 IntEn */
  85. #define UHCHIE_TAIE (1 << 10) /* HCI Interface Transfer Abort
  86. Interrupt Enable*/
  87. #define UHCHIE_HBAIE (1 << 8) /* HCI Buffer Active IntEn */
  88. #define UHCHIE_RWIE (1 << 7) /* Remote Wake-up IntEn */
  89. #define UHCHIT (0x006C) /* UHC Interrupt Test register */
  90. #define PXA_UHC_MAX_PORTNUM 3
  91. struct pxa27x_ohci {
  92. /* must be 1st member here for hcd_to_ohci() to work */
  93. struct ohci_hcd ohci;
  94. struct device *dev;
  95. struct clk *clk;
  96. void __iomem *mmio_base;
  97. };
  98. #define to_pxa27x_ohci(hcd) (struct pxa27x_ohci *)hcd_to_ohci(hcd)
  99. /*
  100. PMM_NPS_MODE -- PMM Non-power switching mode
  101. Ports are powered continuously.
  102. PMM_GLOBAL_MODE -- PMM global switching mode
  103. All ports are powered at the same time.
  104. PMM_PERPORT_MODE -- PMM per port switching mode
  105. Ports are powered individually.
  106. */
  107. static int pxa27x_ohci_select_pmm(struct pxa27x_ohci *ohci, int mode)
  108. {
  109. uint32_t uhcrhda = __raw_readl(ohci->mmio_base + UHCRHDA);
  110. uint32_t uhcrhdb = __raw_readl(ohci->mmio_base + UHCRHDB);
  111. switch (mode) {
  112. case PMM_NPS_MODE:
  113. uhcrhda |= RH_A_NPS;
  114. break;
  115. case PMM_GLOBAL_MODE:
  116. uhcrhda &= ~(RH_A_NPS & RH_A_PSM);
  117. break;
  118. case PMM_PERPORT_MODE:
  119. uhcrhda &= ~(RH_A_NPS);
  120. uhcrhda |= RH_A_PSM;
  121. /* Set port power control mask bits, only 3 ports. */
  122. uhcrhdb |= (0x7<<17);
  123. break;
  124. default:
  125. printk( KERN_ERR
  126. "Invalid mode %d, set to non-power switch mode.\n",
  127. mode );
  128. uhcrhda |= RH_A_NPS;
  129. }
  130. __raw_writel(uhcrhda, ohci->mmio_base + UHCRHDA);
  131. __raw_writel(uhcrhdb, ohci->mmio_base + UHCRHDB);
  132. return 0;
  133. }
  134. extern int usb_disabled(void);
  135. /*-------------------------------------------------------------------------*/
  136. static inline void pxa27x_setup_hc(struct pxa27x_ohci *ohci,
  137. struct pxaohci_platform_data *inf)
  138. {
  139. uint32_t uhchr = __raw_readl(ohci->mmio_base + UHCHR);
  140. uint32_t uhcrhda = __raw_readl(ohci->mmio_base + UHCRHDA);
  141. if (inf->flags & ENABLE_PORT1)
  142. uhchr &= ~UHCHR_SSEP1;
  143. if (inf->flags & ENABLE_PORT2)
  144. uhchr &= ~UHCHR_SSEP2;
  145. if (inf->flags & ENABLE_PORT3)
  146. uhchr &= ~UHCHR_SSEP3;
  147. if (inf->flags & POWER_CONTROL_LOW)
  148. uhchr |= UHCHR_PCPL;
  149. if (inf->flags & POWER_SENSE_LOW)
  150. uhchr |= UHCHR_PSPL;
  151. if (inf->flags & NO_OC_PROTECTION)
  152. uhcrhda |= UHCRHDA_NOCP;
  153. else
  154. uhcrhda &= ~UHCRHDA_NOCP;
  155. if (inf->flags & OC_MODE_PERPORT)
  156. uhcrhda |= UHCRHDA_OCPM;
  157. else
  158. uhcrhda &= ~UHCRHDA_OCPM;
  159. if (inf->power_on_delay) {
  160. uhcrhda &= ~UHCRHDA_POTPGT(0xff);
  161. uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
  162. }
  163. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  164. __raw_writel(uhcrhda, ohci->mmio_base + UHCRHDA);
  165. }
  166. static inline void pxa27x_reset_hc(struct pxa27x_ohci *ohci)
  167. {
  168. uint32_t uhchr = __raw_readl(ohci->mmio_base + UHCHR);
  169. __raw_writel(uhchr | UHCHR_FHR, ohci->mmio_base + UHCHR);
  170. udelay(11);
  171. __raw_writel(uhchr & ~UHCHR_FHR, ohci->mmio_base + UHCHR);
  172. }
  173. #ifdef CONFIG_PXA27x
  174. extern void pxa27x_clear_otgph(void);
  175. #else
  176. #define pxa27x_clear_otgph() do {} while (0)
  177. #endif
  178. static int pxa27x_start_hc(struct pxa27x_ohci *ohci, struct device *dev)
  179. {
  180. int retval = 0;
  181. struct pxaohci_platform_data *inf;
  182. uint32_t uhchr;
  183. inf = dev->platform_data;
  184. clk_enable(ohci->clk);
  185. pxa27x_reset_hc(ohci);
  186. uhchr = __raw_readl(ohci->mmio_base + UHCHR) | UHCHR_FSBIR;
  187. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  188. while (__raw_readl(ohci->mmio_base + UHCHR) & UHCHR_FSBIR)
  189. cpu_relax();
  190. pxa27x_setup_hc(ohci, inf);
  191. if (inf->init)
  192. retval = inf->init(dev);
  193. if (retval < 0)
  194. return retval;
  195. if (cpu_is_pxa3xx())
  196. pxa3xx_u2d_start_hc(&ohci_to_hcd(&ohci->ohci)->self);
  197. uhchr = __raw_readl(ohci->mmio_base + UHCHR) & ~UHCHR_SSE;
  198. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  199. __raw_writel(UHCHIE_UPRIE | UHCHIE_RWIE, ohci->mmio_base + UHCHIE);
  200. /* Clear any OTG Pin Hold */
  201. pxa27x_clear_otgph();
  202. return 0;
  203. }
  204. static void pxa27x_stop_hc(struct pxa27x_ohci *ohci, struct device *dev)
  205. {
  206. struct pxaohci_platform_data *inf;
  207. uint32_t uhccoms;
  208. inf = dev->platform_data;
  209. if (cpu_is_pxa3xx())
  210. pxa3xx_u2d_stop_hc(&ohci_to_hcd(&ohci->ohci)->self);
  211. if (inf->exit)
  212. inf->exit(dev);
  213. pxa27x_reset_hc(ohci);
  214. /* Host Controller Reset */
  215. uhccoms = __raw_readl(ohci->mmio_base + UHCCOMS) | 0x01;
  216. __raw_writel(uhccoms, ohci->mmio_base + UHCCOMS);
  217. udelay(10);
  218. clk_disable(ohci->clk);
  219. }
  220. /*-------------------------------------------------------------------------*/
  221. /* configure so an HC device and id are always provided */
  222. /* always called with process context; sleeping is OK */
  223. /**
  224. * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
  225. * Context: !in_interrupt()
  226. *
  227. * Allocates basic resources for this USB host controller, and
  228. * then invokes the start() method for the HCD associated with it
  229. * through the hotplug entry's driver_data.
  230. *
  231. */
  232. int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
  233. {
  234. int retval, irq;
  235. struct usb_hcd *hcd;
  236. struct pxaohci_platform_data *inf;
  237. struct pxa27x_ohci *ohci;
  238. struct resource *r;
  239. struct clk *usb_clk;
  240. inf = pdev->dev.platform_data;
  241. if (!inf)
  242. return -ENODEV;
  243. irq = platform_get_irq(pdev, 0);
  244. if (irq < 0) {
  245. pr_err("no resource of IORESOURCE_IRQ");
  246. return -ENXIO;
  247. }
  248. usb_clk = clk_get(&pdev->dev, NULL);
  249. if (IS_ERR(usb_clk))
  250. return PTR_ERR(usb_clk);
  251. hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
  252. if (!hcd)
  253. return -ENOMEM;
  254. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. if (!r) {
  256. pr_err("no resource of IORESOURCE_MEM");
  257. retval = -ENXIO;
  258. goto err1;
  259. }
  260. hcd->rsrc_start = r->start;
  261. hcd->rsrc_len = resource_size(r);
  262. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  263. pr_debug("request_mem_region failed");
  264. retval = -EBUSY;
  265. goto err1;
  266. }
  267. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  268. if (!hcd->regs) {
  269. pr_debug("ioremap failed");
  270. retval = -ENOMEM;
  271. goto err2;
  272. }
  273. /* initialize "struct pxa27x_ohci" */
  274. ohci = (struct pxa27x_ohci *)hcd_to_ohci(hcd);
  275. ohci->dev = &pdev->dev;
  276. ohci->clk = usb_clk;
  277. ohci->mmio_base = (void __iomem *)hcd->regs;
  278. if ((retval = pxa27x_start_hc(ohci, &pdev->dev)) < 0) {
  279. pr_debug("pxa27x_start_hc failed");
  280. goto err3;
  281. }
  282. /* Select Power Management Mode */
  283. pxa27x_ohci_select_pmm(ohci, inf->port_mode);
  284. if (inf->power_budget)
  285. hcd->power_budget = inf->power_budget;
  286. ohci_hcd_init(hcd_to_ohci(hcd));
  287. retval = usb_add_hcd(hcd, irq, IRQF_DISABLED);
  288. if (retval == 0)
  289. return retval;
  290. pxa27x_stop_hc(ohci, &pdev->dev);
  291. err3:
  292. iounmap(hcd->regs);
  293. err2:
  294. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  295. err1:
  296. usb_put_hcd(hcd);
  297. clk_put(usb_clk);
  298. return retval;
  299. }
  300. /* may be called without controller electrically present */
  301. /* may be called with controller, bus, and devices active */
  302. /**
  303. * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
  304. * @dev: USB Host Controller being removed
  305. * Context: !in_interrupt()
  306. *
  307. * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
  308. * the HCD's stop() method. It is always called from a thread
  309. * context, normally "rmmod", "apmd", or something similar.
  310. *
  311. */
  312. void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
  313. {
  314. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  315. usb_remove_hcd(hcd);
  316. pxa27x_stop_hc(ohci, &pdev->dev);
  317. iounmap(hcd->regs);
  318. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  319. usb_put_hcd(hcd);
  320. clk_put(ohci->clk);
  321. }
  322. /*-------------------------------------------------------------------------*/
  323. static int __devinit
  324. ohci_pxa27x_start (struct usb_hcd *hcd)
  325. {
  326. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  327. int ret;
  328. ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
  329. /* The value of NDP in roothub_a is incorrect on this hardware */
  330. ohci->num_ports = 3;
  331. if ((ret = ohci_init(ohci)) < 0)
  332. return ret;
  333. if ((ret = ohci_run (ohci)) < 0) {
  334. err ("can't start %s", hcd->self.bus_name);
  335. ohci_stop (hcd);
  336. return ret;
  337. }
  338. return 0;
  339. }
  340. /*-------------------------------------------------------------------------*/
  341. static const struct hc_driver ohci_pxa27x_hc_driver = {
  342. .description = hcd_name,
  343. .product_desc = "PXA27x OHCI",
  344. .hcd_priv_size = sizeof(struct pxa27x_ohci),
  345. /*
  346. * generic hardware linkage
  347. */
  348. .irq = ohci_irq,
  349. .flags = HCD_USB11 | HCD_MEMORY,
  350. /*
  351. * basic lifecycle operations
  352. */
  353. .start = ohci_pxa27x_start,
  354. .stop = ohci_stop,
  355. .shutdown = ohci_shutdown,
  356. /*
  357. * managing i/o requests and associated device resources
  358. */
  359. .urb_enqueue = ohci_urb_enqueue,
  360. .urb_dequeue = ohci_urb_dequeue,
  361. .endpoint_disable = ohci_endpoint_disable,
  362. /*
  363. * scheduling support
  364. */
  365. .get_frame_number = ohci_get_frame,
  366. /*
  367. * root hub support
  368. */
  369. .hub_status_data = ohci_hub_status_data,
  370. .hub_control = ohci_hub_control,
  371. #ifdef CONFIG_PM
  372. .bus_suspend = ohci_bus_suspend,
  373. .bus_resume = ohci_bus_resume,
  374. #endif
  375. .start_port_reset = ohci_start_port_reset,
  376. };
  377. /*-------------------------------------------------------------------------*/
  378. static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
  379. {
  380. pr_debug ("In ohci_hcd_pxa27x_drv_probe");
  381. if (usb_disabled())
  382. return -ENODEV;
  383. return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
  384. }
  385. static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
  386. {
  387. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  388. usb_hcd_pxa27x_remove(hcd, pdev);
  389. platform_set_drvdata(pdev, NULL);
  390. return 0;
  391. }
  392. #ifdef CONFIG_PM
  393. static int ohci_hcd_pxa27x_drv_suspend(struct device *dev)
  394. {
  395. struct usb_hcd *hcd = dev_get_drvdata(dev);
  396. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  397. if (time_before(jiffies, ohci->ohci.next_statechange))
  398. msleep(5);
  399. ohci->ohci.next_statechange = jiffies;
  400. pxa27x_stop_hc(ohci, dev);
  401. hcd->state = HC_STATE_SUSPENDED;
  402. return 0;
  403. }
  404. static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
  405. {
  406. struct usb_hcd *hcd = dev_get_drvdata(dev);
  407. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  408. struct pxaohci_platform_data *inf = dev->platform_data;
  409. int status;
  410. if (time_before(jiffies, ohci->ohci.next_statechange))
  411. msleep(5);
  412. ohci->ohci.next_statechange = jiffies;
  413. if ((status = pxa27x_start_hc(ohci, dev)) < 0)
  414. return status;
  415. /* Select Power Management Mode */
  416. pxa27x_ohci_select_pmm(ohci, inf->port_mode);
  417. ohci_finish_controller_resume(hcd);
  418. return 0;
  419. }
  420. static const struct dev_pm_ops ohci_hcd_pxa27x_pm_ops = {
  421. .suspend = ohci_hcd_pxa27x_drv_suspend,
  422. .resume = ohci_hcd_pxa27x_drv_resume,
  423. };
  424. #endif
  425. /* work with hotplug and coldplug */
  426. MODULE_ALIAS("platform:pxa27x-ohci");
  427. static struct platform_driver ohci_hcd_pxa27x_driver = {
  428. .probe = ohci_hcd_pxa27x_drv_probe,
  429. .remove = ohci_hcd_pxa27x_drv_remove,
  430. .shutdown = usb_hcd_platform_shutdown,
  431. .driver = {
  432. .name = "pxa27x-ohci",
  433. .owner = THIS_MODULE,
  434. #ifdef CONFIG_PM
  435. .pm = &ohci_hcd_pxa27x_pm_ops,
  436. #endif
  437. },
  438. };