ohci-pxa27x.c 15 KB

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