ohci-pxa27x.c 14 KB

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