ehci-spear.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. struct spear_ehci {
  19. struct ehci_hcd ehci;
  20. struct clk *clk;
  21. };
  22. #define to_spear_ehci(hcd) (struct spear_ehci *)hcd_to_ehci(hcd)
  23. static void spear_start_ehci(struct spear_ehci *ehci)
  24. {
  25. clk_prepare_enable(ehci->clk);
  26. }
  27. static void spear_stop_ehci(struct spear_ehci *ehci)
  28. {
  29. clk_disable_unprepare(ehci->clk);
  30. }
  31. static int ehci_spear_setup(struct usb_hcd *hcd)
  32. {
  33. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  34. int retval = 0;
  35. /* registers start at offset 0x0 */
  36. ehci->caps = hcd->regs;
  37. ehci->regs = hcd->regs + HC_LENGTH(ehci, ehci_readl(ehci,
  38. &ehci->caps->hc_capbase));
  39. /* cache this readonly data; minimize chip reads */
  40. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  41. retval = ehci_halt(ehci);
  42. if (retval)
  43. return retval;
  44. retval = ehci_init(hcd);
  45. if (retval)
  46. return retval;
  47. ehci_reset(ehci);
  48. ehci_port_power(ehci, 0);
  49. return retval;
  50. }
  51. static const struct hc_driver ehci_spear_hc_driver = {
  52. .description = hcd_name,
  53. .product_desc = "SPEAr EHCI",
  54. .hcd_priv_size = sizeof(struct spear_ehci),
  55. /* generic hardware linkage */
  56. .irq = ehci_irq,
  57. .flags = HCD_MEMORY | HCD_USB2,
  58. /* basic lifecycle operations */
  59. .reset = ehci_spear_setup,
  60. .start = ehci_run,
  61. .stop = ehci_stop,
  62. .shutdown = ehci_shutdown,
  63. /* managing i/o requests and associated device resources */
  64. .urb_enqueue = ehci_urb_enqueue,
  65. .urb_dequeue = ehci_urb_dequeue,
  66. .endpoint_disable = ehci_endpoint_disable,
  67. .endpoint_reset = ehci_endpoint_reset,
  68. /* scheduling support */
  69. .get_frame_number = ehci_get_frame,
  70. /* root hub support */
  71. .hub_status_data = ehci_hub_status_data,
  72. .hub_control = ehci_hub_control,
  73. .bus_suspend = ehci_bus_suspend,
  74. .bus_resume = ehci_bus_resume,
  75. .relinquish_port = ehci_relinquish_port,
  76. .port_handed_over = ehci_port_handed_over,
  77. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  78. };
  79. #ifdef CONFIG_PM
  80. static int ehci_spear_drv_suspend(struct device *dev)
  81. {
  82. struct usb_hcd *hcd = dev_get_drvdata(dev);
  83. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  84. unsigned long flags;
  85. int rc = 0;
  86. if (time_before(jiffies, ehci->next_statechange))
  87. msleep(10);
  88. /*
  89. * Root hub was already suspended. Disable irq emission and mark HW
  90. * unaccessible. The PM and USB cores make sure that the root hub is
  91. * either suspended or stopped.
  92. */
  93. spin_lock_irqsave(&ehci->lock, flags);
  94. ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
  95. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  96. ehci_readl(ehci, &ehci->regs->intr_enable);
  97. spin_unlock_irqrestore(&ehci->lock, flags);
  98. return rc;
  99. }
  100. static int ehci_spear_drv_resume(struct device *dev)
  101. {
  102. struct usb_hcd *hcd = dev_get_drvdata(dev);
  103. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  104. if (time_before(jiffies, ehci->next_statechange))
  105. msleep(100);
  106. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
  107. int mask = INTR_MASK;
  108. ehci_prepare_ports_for_controller_resume(ehci);
  109. if (!hcd->self.root_hub->do_remote_wakeup)
  110. mask &= ~STS_PCD;
  111. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  112. ehci_readl(ehci, &ehci->regs->intr_enable);
  113. return 0;
  114. }
  115. usb_root_hub_lost_power(hcd->self.root_hub);
  116. /*
  117. * Else reset, to cope with power loss or flush-to-storage style
  118. * "resume" having let BIOS kick in during reboot.
  119. */
  120. ehci_halt(ehci);
  121. ehci_reset(ehci);
  122. /* emptying the schedule aborts any urbs */
  123. spin_lock_irq(&ehci->lock);
  124. if (ehci->reclaim)
  125. end_unlink_async(ehci);
  126. ehci_work(ehci);
  127. spin_unlock_irq(&ehci->lock);
  128. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  129. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  130. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  131. /* here we "know" root ports should always stay powered */
  132. ehci_port_power(ehci, 1);
  133. return 0;
  134. }
  135. #endif /* CONFIG_PM */
  136. static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend,
  137. ehci_spear_drv_resume);
  138. static u64 spear_ehci_dma_mask = DMA_BIT_MASK(32);
  139. static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
  140. {
  141. struct usb_hcd *hcd ;
  142. struct spear_ehci *ehci;
  143. struct resource *res;
  144. struct clk *usbh_clk;
  145. const struct hc_driver *driver = &ehci_spear_hc_driver;
  146. int irq, retval;
  147. char clk_name[20] = "usbh_clk";
  148. static int instance = -1;
  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. /*
  157. * Right now device-tree probed devices don't get dma_mask set.
  158. * Since shared usb code relies on it, set it here for now.
  159. * Once we have dma capability bindings this can go away.
  160. */
  161. if (!pdev->dev.dma_mask)
  162. pdev->dev.dma_mask = &spear_ehci_dma_mask;
  163. /*
  164. * Increment the device instance, when probing via device-tree
  165. */
  166. if (pdev->id < 0)
  167. instance++;
  168. else
  169. instance = pdev->id;
  170. sprintf(clk_name, "usbh.%01d_clk", instance);
  171. usbh_clk = clk_get(NULL, clk_name);
  172. if (IS_ERR(usbh_clk)) {
  173. dev_err(&pdev->dev, "Error getting interface clock\n");
  174. retval = PTR_ERR(usbh_clk);
  175. goto fail_get_usbh_clk;
  176. }
  177. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  178. if (!hcd) {
  179. retval = -ENOMEM;
  180. goto fail_create_hcd;
  181. }
  182. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  183. if (!res) {
  184. retval = -ENODEV;
  185. goto fail_request_resource;
  186. }
  187. hcd->rsrc_start = res->start;
  188. hcd->rsrc_len = resource_size(res);
  189. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  190. driver->description)) {
  191. retval = -EBUSY;
  192. goto fail_request_resource;
  193. }
  194. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  195. if (hcd->regs == NULL) {
  196. dev_dbg(&pdev->dev, "error mapping memory\n");
  197. retval = -ENOMEM;
  198. goto fail_ioremap;
  199. }
  200. ehci = (struct spear_ehci *)hcd_to_ehci(hcd);
  201. ehci->clk = usbh_clk;
  202. spear_start_ehci(ehci);
  203. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  204. if (retval)
  205. goto fail_add_hcd;
  206. return retval;
  207. fail_add_hcd:
  208. spear_stop_ehci(ehci);
  209. iounmap(hcd->regs);
  210. fail_ioremap:
  211. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  212. fail_request_resource:
  213. usb_put_hcd(hcd);
  214. fail_create_hcd:
  215. clk_put(usbh_clk);
  216. fail_get_usbh_clk:
  217. fail_irq_get:
  218. dev_err(&pdev->dev, "init fail, %d\n", retval);
  219. return retval ;
  220. }
  221. static int spear_ehci_hcd_drv_remove(struct platform_device *pdev)
  222. {
  223. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  224. struct spear_ehci *ehci_p = to_spear_ehci(hcd);
  225. if (!hcd)
  226. return 0;
  227. if (in_interrupt())
  228. BUG();
  229. usb_remove_hcd(hcd);
  230. if (ehci_p->clk)
  231. spear_stop_ehci(ehci_p);
  232. iounmap(hcd->regs);
  233. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  234. usb_put_hcd(hcd);
  235. if (ehci_p->clk)
  236. clk_put(ehci_p->clk);
  237. return 0;
  238. }
  239. static struct of_device_id spear_ehci_id_table[] __devinitdata = {
  240. { .compatible = "st,spear600-ehci", },
  241. { },
  242. };
  243. static struct platform_driver spear_ehci_hcd_driver = {
  244. .probe = spear_ehci_hcd_drv_probe,
  245. .remove = spear_ehci_hcd_drv_remove,
  246. .shutdown = usb_hcd_platform_shutdown,
  247. .driver = {
  248. .name = "spear-ehci",
  249. .bus = &platform_bus_type,
  250. .pm = &ehci_spear_pm_ops,
  251. .of_match_table = of_match_ptr(spear_ehci_id_table),
  252. }
  253. };
  254. MODULE_ALIAS("platform:spear-ehci");