ehci-spear.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Driver for EHCI HCD on SPEAR SOC
  3. *
  4. * Copyright (C) 2010 ST Micro Electronics,
  5. * Deepak Sikri <deepak.sikri@st.com>
  6. *
  7. * Based on various ehci-*.c drivers
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. struct spear_ehci {
  18. struct ehci_hcd ehci;
  19. struct clk *clk;
  20. };
  21. #define to_spear_ehci(hcd) (struct spear_ehci *)hcd_to_ehci(hcd)
  22. static void spear_start_ehci(struct spear_ehci *ehci)
  23. {
  24. clk_enable(ehci->clk);
  25. }
  26. static void spear_stop_ehci(struct spear_ehci *ehci)
  27. {
  28. clk_disable(ehci->clk);
  29. }
  30. static int ehci_spear_setup(struct usb_hcd *hcd)
  31. {
  32. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  33. int retval = 0;
  34. /* registers start at offset 0x0 */
  35. ehci->caps = hcd->regs;
  36. ehci->regs = hcd->regs + HC_LENGTH(ehci, ehci_readl(ehci,
  37. &ehci->caps->hc_capbase));
  38. /* cache this readonly data; minimize chip reads */
  39. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  40. retval = ehci_halt(ehci);
  41. if (retval)
  42. return retval;
  43. retval = ehci_init(hcd);
  44. if (retval)
  45. return retval;
  46. ehci_reset(ehci);
  47. ehci_port_power(ehci, 0);
  48. return retval;
  49. }
  50. static const struct hc_driver ehci_spear_hc_driver = {
  51. .description = hcd_name,
  52. .product_desc = "SPEAr EHCI",
  53. .hcd_priv_size = sizeof(struct spear_ehci),
  54. /* generic hardware linkage */
  55. .irq = ehci_irq,
  56. .flags = HCD_MEMORY | HCD_USB2,
  57. /* basic lifecycle operations */
  58. .reset = ehci_spear_setup,
  59. .start = ehci_run,
  60. .stop = ehci_stop,
  61. .shutdown = ehci_shutdown,
  62. /* managing i/o requests and associated device resources */
  63. .urb_enqueue = ehci_urb_enqueue,
  64. .urb_dequeue = ehci_urb_dequeue,
  65. .endpoint_disable = ehci_endpoint_disable,
  66. .endpoint_reset = ehci_endpoint_reset,
  67. /* scheduling support */
  68. .get_frame_number = ehci_get_frame,
  69. /* root hub support */
  70. .hub_status_data = ehci_hub_status_data,
  71. .hub_control = ehci_hub_control,
  72. .bus_suspend = ehci_bus_suspend,
  73. .bus_resume = ehci_bus_resume,
  74. .relinquish_port = ehci_relinquish_port,
  75. .port_handed_over = ehci_port_handed_over,
  76. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  77. };
  78. #ifdef CONFIG_PM
  79. static int ehci_spear_drv_suspend(struct device *dev)
  80. {
  81. struct usb_hcd *hcd = dev_get_drvdata(dev);
  82. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  83. unsigned long flags;
  84. int rc = 0;
  85. if (time_before(jiffies, ehci->next_statechange))
  86. msleep(10);
  87. /*
  88. * Root hub was already suspended. Disable irq emission and mark HW
  89. * unaccessible. The PM and USB cores make sure that the root hub is
  90. * either suspended or stopped.
  91. */
  92. spin_lock_irqsave(&ehci->lock, flags);
  93. ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
  94. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  95. ehci_readl(ehci, &ehci->regs->intr_enable);
  96. spin_unlock_irqrestore(&ehci->lock, flags);
  97. return rc;
  98. }
  99. static int ehci_spear_drv_resume(struct device *dev)
  100. {
  101. struct usb_hcd *hcd = dev_get_drvdata(dev);
  102. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  103. if (time_before(jiffies, ehci->next_statechange))
  104. msleep(100);
  105. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
  106. int mask = INTR_MASK;
  107. ehci_prepare_ports_for_controller_resume(ehci);
  108. if (!hcd->self.root_hub->do_remote_wakeup)
  109. mask &= ~STS_PCD;
  110. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  111. ehci_readl(ehci, &ehci->regs->intr_enable);
  112. return 0;
  113. }
  114. usb_root_hub_lost_power(hcd->self.root_hub);
  115. /*
  116. * Else reset, to cope with power loss or flush-to-storage style
  117. * "resume" having let BIOS kick in during reboot.
  118. */
  119. ehci_halt(ehci);
  120. ehci_reset(ehci);
  121. /* emptying the schedule aborts any urbs */
  122. spin_lock_irq(&ehci->lock);
  123. if (ehci->reclaim)
  124. end_unlink_async(ehci);
  125. ehci_work(ehci);
  126. spin_unlock_irq(&ehci->lock);
  127. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  128. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  129. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  130. /* here we "know" root ports should always stay powered */
  131. ehci_port_power(ehci, 1);
  132. return 0;
  133. }
  134. #endif /* CONFIG_PM */
  135. static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend,
  136. ehci_spear_drv_resume);
  137. static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
  138. {
  139. struct usb_hcd *hcd ;
  140. struct spear_ehci *ehci;
  141. struct resource *res;
  142. struct clk *usbh_clk;
  143. const struct hc_driver *driver = &ehci_spear_hc_driver;
  144. int *pdata = pdev->dev.platform_data;
  145. int irq, retval;
  146. char clk_name[20] = "usbh_clk";
  147. if (pdata == NULL)
  148. return -EFAULT;
  149. if (usb_disabled())
  150. return -ENODEV;
  151. irq = platform_get_irq(pdev, 0);
  152. if (irq < 0) {
  153. retval = irq;
  154. goto fail_irq_get;
  155. }
  156. if (*pdata >= 0)
  157. sprintf(clk_name, "usbh.%01d_clk", *pdata);
  158. usbh_clk = clk_get(NULL, clk_name);
  159. if (IS_ERR(usbh_clk)) {
  160. dev_err(&pdev->dev, "Error getting interface clock\n");
  161. retval = PTR_ERR(usbh_clk);
  162. goto fail_get_usbh_clk;
  163. }
  164. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  165. if (!hcd) {
  166. retval = -ENOMEM;
  167. goto fail_create_hcd;
  168. }
  169. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  170. if (!res) {
  171. retval = -ENODEV;
  172. goto fail_request_resource;
  173. }
  174. hcd->rsrc_start = res->start;
  175. hcd->rsrc_len = resource_size(res);
  176. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  177. driver->description)) {
  178. retval = -EBUSY;
  179. goto fail_request_resource;
  180. }
  181. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  182. if (hcd->regs == NULL) {
  183. dev_dbg(&pdev->dev, "error mapping memory\n");
  184. retval = -ENOMEM;
  185. goto fail_ioremap;
  186. }
  187. ehci = (struct spear_ehci *)hcd_to_ehci(hcd);
  188. ehci->clk = usbh_clk;
  189. spear_start_ehci(ehci);
  190. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  191. if (retval)
  192. goto fail_add_hcd;
  193. return retval;
  194. fail_add_hcd:
  195. spear_stop_ehci(ehci);
  196. iounmap(hcd->regs);
  197. fail_ioremap:
  198. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  199. fail_request_resource:
  200. usb_put_hcd(hcd);
  201. fail_create_hcd:
  202. clk_put(usbh_clk);
  203. fail_get_usbh_clk:
  204. fail_irq_get:
  205. dev_err(&pdev->dev, "init fail, %d\n", retval);
  206. return retval ;
  207. }
  208. static int spear_ehci_hcd_drv_remove(struct platform_device *pdev)
  209. {
  210. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  211. struct spear_ehci *ehci_p = to_spear_ehci(hcd);
  212. if (!hcd)
  213. return 0;
  214. if (in_interrupt())
  215. BUG();
  216. usb_remove_hcd(hcd);
  217. if (ehci_p->clk)
  218. spear_stop_ehci(ehci_p);
  219. iounmap(hcd->regs);
  220. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  221. usb_put_hcd(hcd);
  222. if (ehci_p->clk)
  223. clk_put(ehci_p->clk);
  224. return 0;
  225. }
  226. static struct platform_driver spear_ehci_hcd_driver = {
  227. .probe = spear_ehci_hcd_drv_probe,
  228. .remove = spear_ehci_hcd_drv_remove,
  229. .shutdown = usb_hcd_platform_shutdown,
  230. .driver = {
  231. .name = "spear-ehci",
  232. .bus = &platform_bus_type,
  233. .pm = &ehci_spear_pm_ops,
  234. }
  235. };
  236. MODULE_ALIAS("platform:spear-ehci");