ohci-pxa27x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. else
  153. uhcrhda &= ~UHCRHDA_NOCP;
  154. if (inf->flags & OC_MODE_PERPORT)
  155. uhcrhda |= UHCRHDA_OCPM;
  156. else
  157. uhcrhda &= ~UHCRHDA_OCPM;
  158. if (inf->power_on_delay) {
  159. uhcrhda &= ~UHCRHDA_POTPGT(0xff);
  160. uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
  161. }
  162. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  163. __raw_writel(uhcrhda, ohci->mmio_base + UHCRHDA);
  164. }
  165. static inline void pxa27x_reset_hc(struct pxa27x_ohci *ohci)
  166. {
  167. uint32_t uhchr = __raw_readl(ohci->mmio_base + UHCHR);
  168. __raw_writel(uhchr | UHCHR_FHR, ohci->mmio_base + UHCHR);
  169. udelay(11);
  170. __raw_writel(uhchr & ~UHCHR_FHR, ohci->mmio_base + UHCHR);
  171. }
  172. #ifdef CONFIG_CPU_PXA27x
  173. extern void pxa27x_clear_otgph(void);
  174. #else
  175. #define pxa27x_clear_otgph() do {} while (0)
  176. #endif
  177. static int pxa27x_start_hc(struct pxa27x_ohci *ohci, struct device *dev)
  178. {
  179. int retval = 0;
  180. struct pxaohci_platform_data *inf;
  181. uint32_t uhchr;
  182. inf = dev->platform_data;
  183. clk_enable(ohci->clk);
  184. pxa27x_reset_hc(ohci);
  185. uhchr = __raw_readl(ohci->mmio_base + UHCHR) | UHCHR_FSBIR;
  186. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  187. while (__raw_readl(ohci->mmio_base + UHCHR) & UHCHR_FSBIR)
  188. cpu_relax();
  189. pxa27x_setup_hc(ohci, inf);
  190. if (inf->init)
  191. retval = inf->init(dev);
  192. if (retval < 0)
  193. return retval;
  194. uhchr = __raw_readl(ohci->mmio_base + UHCHR) & ~UHCHR_SSE;
  195. __raw_writel(uhchr, ohci->mmio_base + UHCHR);
  196. __raw_writel(UHCHIE_UPRIE | UHCHIE_RWIE, ohci->mmio_base + UHCHIE);
  197. /* Clear any OTG Pin Hold */
  198. pxa27x_clear_otgph();
  199. return 0;
  200. }
  201. static void pxa27x_stop_hc(struct pxa27x_ohci *ohci, struct device *dev)
  202. {
  203. struct pxaohci_platform_data *inf;
  204. uint32_t uhccoms;
  205. inf = dev->platform_data;
  206. if (inf->exit)
  207. inf->exit(dev);
  208. pxa27x_reset_hc(ohci);
  209. /* Host Controller Reset */
  210. uhccoms = __raw_readl(ohci->mmio_base + UHCCOMS) | 0x01;
  211. __raw_writel(uhccoms, ohci->mmio_base + UHCCOMS);
  212. udelay(10);
  213. clk_disable(ohci->clk);
  214. }
  215. /*-------------------------------------------------------------------------*/
  216. /* configure so an HC device and id are always provided */
  217. /* always called with process context; sleeping is OK */
  218. /**
  219. * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
  220. * Context: !in_interrupt()
  221. *
  222. * Allocates basic resources for this USB host controller, and
  223. * then invokes the start() method for the HCD associated with it
  224. * through the hotplug entry's driver_data.
  225. *
  226. */
  227. int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
  228. {
  229. int retval, irq;
  230. struct usb_hcd *hcd;
  231. struct pxaohci_platform_data *inf;
  232. struct pxa27x_ohci *ohci;
  233. struct resource *r;
  234. struct clk *usb_clk;
  235. inf = pdev->dev.platform_data;
  236. if (!inf)
  237. return -ENODEV;
  238. irq = platform_get_irq(pdev, 0);
  239. if (irq < 0) {
  240. pr_err("no resource of IORESOURCE_IRQ");
  241. return -ENXIO;
  242. }
  243. usb_clk = clk_get(&pdev->dev, NULL);
  244. if (IS_ERR(usb_clk))
  245. return PTR_ERR(usb_clk);
  246. hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
  247. if (!hcd)
  248. return -ENOMEM;
  249. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. if (!r) {
  251. pr_err("no resource of IORESOURCE_MEM");
  252. retval = -ENXIO;
  253. goto err1;
  254. }
  255. hcd->rsrc_start = r->start;
  256. hcd->rsrc_len = resource_size(r);
  257. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  258. pr_debug("request_mem_region failed");
  259. retval = -EBUSY;
  260. goto err1;
  261. }
  262. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  263. if (!hcd->regs) {
  264. pr_debug("ioremap failed");
  265. retval = -ENOMEM;
  266. goto err2;
  267. }
  268. /* initialize "struct pxa27x_ohci" */
  269. ohci = (struct pxa27x_ohci *)hcd_to_ohci(hcd);
  270. ohci->dev = &pdev->dev;
  271. ohci->clk = usb_clk;
  272. ohci->mmio_base = (void __iomem *)hcd->regs;
  273. if ((retval = pxa27x_start_hc(ohci, &pdev->dev)) < 0) {
  274. pr_debug("pxa27x_start_hc failed");
  275. goto err3;
  276. }
  277. /* Select Power Management Mode */
  278. pxa27x_ohci_select_pmm(ohci, inf->port_mode);
  279. if (inf->power_budget)
  280. hcd->power_budget = inf->power_budget;
  281. ohci_hcd_init(hcd_to_ohci(hcd));
  282. retval = usb_add_hcd(hcd, irq, IRQF_DISABLED);
  283. if (retval == 0)
  284. return retval;
  285. pxa27x_stop_hc(ohci, &pdev->dev);
  286. err3:
  287. iounmap(hcd->regs);
  288. err2:
  289. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  290. err1:
  291. usb_put_hcd(hcd);
  292. clk_put(usb_clk);
  293. return retval;
  294. }
  295. /* may be called without controller electrically present */
  296. /* may be called with controller, bus, and devices active */
  297. /**
  298. * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
  299. * @dev: USB Host Controller being removed
  300. * Context: !in_interrupt()
  301. *
  302. * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
  303. * the HCD's stop() method. It is always called from a thread
  304. * context, normally "rmmod", "apmd", or something similar.
  305. *
  306. */
  307. void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
  308. {
  309. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  310. usb_remove_hcd(hcd);
  311. pxa27x_stop_hc(ohci, &pdev->dev);
  312. iounmap(hcd->regs);
  313. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  314. usb_put_hcd(hcd);
  315. clk_put(ohci->clk);
  316. }
  317. /*-------------------------------------------------------------------------*/
  318. static int __devinit
  319. ohci_pxa27x_start (struct usb_hcd *hcd)
  320. {
  321. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  322. int ret;
  323. ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
  324. /* The value of NDP in roothub_a is incorrect on this hardware */
  325. ohci->num_ports = 3;
  326. if ((ret = ohci_init(ohci)) < 0)
  327. return ret;
  328. if ((ret = ohci_run (ohci)) < 0) {
  329. err ("can't start %s", hcd->self.bus_name);
  330. ohci_stop (hcd);
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. /*-------------------------------------------------------------------------*/
  336. static const struct hc_driver ohci_pxa27x_hc_driver = {
  337. .description = hcd_name,
  338. .product_desc = "PXA27x OHCI",
  339. .hcd_priv_size = sizeof(struct pxa27x_ohci),
  340. /*
  341. * generic hardware linkage
  342. */
  343. .irq = ohci_irq,
  344. .flags = HCD_USB11 | HCD_MEMORY,
  345. /*
  346. * basic lifecycle operations
  347. */
  348. .start = ohci_pxa27x_start,
  349. .stop = ohci_stop,
  350. .shutdown = ohci_shutdown,
  351. /*
  352. * managing i/o requests and associated device resources
  353. */
  354. .urb_enqueue = ohci_urb_enqueue,
  355. .urb_dequeue = ohci_urb_dequeue,
  356. .endpoint_disable = ohci_endpoint_disable,
  357. /*
  358. * scheduling support
  359. */
  360. .get_frame_number = ohci_get_frame,
  361. /*
  362. * root hub support
  363. */
  364. .hub_status_data = ohci_hub_status_data,
  365. .hub_control = ohci_hub_control,
  366. #ifdef CONFIG_PM
  367. .bus_suspend = ohci_bus_suspend,
  368. .bus_resume = ohci_bus_resume,
  369. #endif
  370. .start_port_reset = ohci_start_port_reset,
  371. };
  372. /*-------------------------------------------------------------------------*/
  373. static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
  374. {
  375. pr_debug ("In ohci_hcd_pxa27x_drv_probe");
  376. if (usb_disabled())
  377. return -ENODEV;
  378. return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
  379. }
  380. static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
  381. {
  382. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  383. usb_hcd_pxa27x_remove(hcd, pdev);
  384. platform_set_drvdata(pdev, NULL);
  385. return 0;
  386. }
  387. #ifdef CONFIG_PM
  388. static int ohci_hcd_pxa27x_drv_suspend(struct device *dev)
  389. {
  390. struct usb_hcd *hcd = dev_get_drvdata(dev);
  391. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  392. if (time_before(jiffies, ohci->ohci.next_statechange))
  393. msleep(5);
  394. ohci->ohci.next_statechange = jiffies;
  395. pxa27x_stop_hc(ohci, dev);
  396. hcd->state = HC_STATE_SUSPENDED;
  397. return 0;
  398. }
  399. static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
  400. {
  401. struct usb_hcd *hcd = dev_get_drvdata(dev);
  402. struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
  403. struct pxaohci_platform_data *inf = dev->platform_data;
  404. int status;
  405. if (time_before(jiffies, ohci->ohci.next_statechange))
  406. msleep(5);
  407. ohci->ohci.next_statechange = jiffies;
  408. if ((status = pxa27x_start_hc(ohci, dev)) < 0)
  409. return status;
  410. /* Select Power Management Mode */
  411. pxa27x_ohci_select_pmm(ohci, inf->port_mode);
  412. ohci_finish_controller_resume(hcd);
  413. return 0;
  414. }
  415. static const struct dev_pm_ops ohci_hcd_pxa27x_pm_ops = {
  416. .suspend = ohci_hcd_pxa27x_drv_suspend,
  417. .resume = ohci_hcd_pxa27x_drv_resume,
  418. };
  419. #endif
  420. /* work with hotplug and coldplug */
  421. MODULE_ALIAS("platform:pxa27x-ohci");
  422. static struct platform_driver ohci_hcd_pxa27x_driver = {
  423. .probe = ohci_hcd_pxa27x_drv_probe,
  424. .remove = ohci_hcd_pxa27x_drv_remove,
  425. .shutdown = usb_hcd_platform_shutdown,
  426. .driver = {
  427. .name = "pxa27x-ohci",
  428. .owner = THIS_MODULE,
  429. #ifdef CONFIG_PM
  430. .pm = &ohci_hcd_pxa27x_pm_ops,
  431. #endif
  432. },
  433. };