ehci-s5p.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * SAMSUNG S5P USB HOST EHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/platform_device.h>
  16. #include <mach/regs-pmu.h>
  17. #include <plat/cpu.h>
  18. #include <plat/ehci.h>
  19. #include <plat/usb-phy.h>
  20. struct s5p_ehci_hcd {
  21. struct device *dev;
  22. struct usb_hcd *hcd;
  23. struct clk *clk;
  24. };
  25. static const struct hc_driver s5p_ehci_hc_driver = {
  26. .description = hcd_name,
  27. .product_desc = "S5P EHCI Host Controller",
  28. .hcd_priv_size = sizeof(struct ehci_hcd),
  29. .irq = ehci_irq,
  30. .flags = HCD_MEMORY | HCD_USB2,
  31. .reset = ehci_init,
  32. .start = ehci_run,
  33. .stop = ehci_stop,
  34. .shutdown = ehci_shutdown,
  35. .get_frame_number = ehci_get_frame,
  36. .urb_enqueue = ehci_urb_enqueue,
  37. .urb_dequeue = ehci_urb_dequeue,
  38. .endpoint_disable = ehci_endpoint_disable,
  39. .endpoint_reset = ehci_endpoint_reset,
  40. .hub_status_data = ehci_hub_status_data,
  41. .hub_control = ehci_hub_control,
  42. .bus_suspend = ehci_bus_suspend,
  43. .bus_resume = ehci_bus_resume,
  44. .relinquish_port = ehci_relinquish_port,
  45. .port_handed_over = ehci_port_handed_over,
  46. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  47. };
  48. static int __devinit s5p_ehci_probe(struct platform_device *pdev)
  49. {
  50. struct s5p_ehci_platdata *pdata;
  51. struct s5p_ehci_hcd *s5p_ehci;
  52. struct usb_hcd *hcd;
  53. struct ehci_hcd *ehci;
  54. struct resource *res;
  55. int irq;
  56. int err;
  57. pdata = pdev->dev.platform_data;
  58. if (!pdata) {
  59. dev_err(&pdev->dev, "No platform data defined\n");
  60. return -EINVAL;
  61. }
  62. s5p_ehci = kzalloc(sizeof(struct s5p_ehci_hcd), GFP_KERNEL);
  63. if (!s5p_ehci)
  64. return -ENOMEM;
  65. s5p_ehci->dev = &pdev->dev;
  66. hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
  67. dev_name(&pdev->dev));
  68. if (!hcd) {
  69. dev_err(&pdev->dev, "Unable to create HCD\n");
  70. err = -ENOMEM;
  71. goto fail_hcd;
  72. }
  73. s5p_ehci->clk = clk_get(&pdev->dev, "usbhost");
  74. if (IS_ERR(s5p_ehci->clk)) {
  75. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  76. err = PTR_ERR(s5p_ehci->clk);
  77. goto fail_clk;
  78. }
  79. err = clk_enable(s5p_ehci->clk);
  80. if (err)
  81. goto fail_clken;
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. if (!res) {
  84. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  85. err = -ENXIO;
  86. goto fail_io;
  87. }
  88. hcd->rsrc_start = res->start;
  89. hcd->rsrc_len = resource_size(res);
  90. hcd->regs = ioremap(res->start, resource_size(res));
  91. if (!hcd->regs) {
  92. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  93. err = -ENOMEM;
  94. goto fail_io;
  95. }
  96. irq = platform_get_irq(pdev, 0);
  97. if (!irq) {
  98. dev_err(&pdev->dev, "Failed to get IRQ\n");
  99. err = -ENODEV;
  100. goto fail;
  101. }
  102. if (pdata->phy_init)
  103. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  104. ehci = hcd_to_ehci(hcd);
  105. ehci->caps = hcd->regs;
  106. ehci->regs = hcd->regs +
  107. HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
  108. dbg_hcs_params(ehci, "reset");
  109. dbg_hcc_params(ehci, "reset");
  110. /* cache this readonly data; minimize chip reads */
  111. ehci->hcs_params = readl(&ehci->caps->hcs_params);
  112. err = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
  113. if (err) {
  114. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  115. goto fail;
  116. }
  117. platform_set_drvdata(pdev, s5p_ehci);
  118. return 0;
  119. fail:
  120. iounmap(hcd->regs);
  121. fail_io:
  122. clk_disable(s5p_ehci->clk);
  123. fail_clken:
  124. clk_put(s5p_ehci->clk);
  125. fail_clk:
  126. usb_put_hcd(hcd);
  127. fail_hcd:
  128. kfree(s5p_ehci);
  129. return err;
  130. }
  131. static int __devexit s5p_ehci_remove(struct platform_device *pdev)
  132. {
  133. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  134. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  135. struct usb_hcd *hcd = s5p_ehci->hcd;
  136. usb_remove_hcd(hcd);
  137. if (pdata && pdata->phy_exit)
  138. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  139. iounmap(hcd->regs);
  140. clk_disable(s5p_ehci->clk);
  141. clk_put(s5p_ehci->clk);
  142. usb_put_hcd(hcd);
  143. kfree(s5p_ehci);
  144. return 0;
  145. }
  146. static void s5p_ehci_shutdown(struct platform_device *pdev)
  147. {
  148. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  149. struct usb_hcd *hcd = s5p_ehci->hcd;
  150. if (hcd->driver->shutdown)
  151. hcd->driver->shutdown(hcd);
  152. }
  153. #ifdef CONFIG_PM
  154. static int s5p_ehci_suspend(struct device *dev)
  155. {
  156. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  157. struct usb_hcd *hcd = s5p_ehci->hcd;
  158. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  159. struct platform_device *pdev = to_platform_device(dev);
  160. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  161. unsigned long flags;
  162. int rc = 0;
  163. if (time_before(jiffies, ehci->next_statechange))
  164. msleep(20);
  165. /*
  166. * Root hub was already suspended. Disable irq emission and
  167. * mark HW unaccessible. The PM and USB cores make sure that
  168. * the root hub is either suspended or stopped.
  169. */
  170. ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
  171. spin_lock_irqsave(&ehci->lock, flags);
  172. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  173. (void)ehci_readl(ehci, &ehci->regs->intr_enable);
  174. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  175. spin_unlock_irqrestore(&ehci->lock, flags);
  176. if (pdata && pdata->phy_exit)
  177. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  178. return rc;
  179. }
  180. static int s5p_ehci_resume(struct device *dev)
  181. {
  182. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  183. struct usb_hcd *hcd = s5p_ehci->hcd;
  184. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  185. struct platform_device *pdev = to_platform_device(dev);
  186. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  187. if (pdata && pdata->phy_init)
  188. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  189. if (time_before(jiffies, ehci->next_statechange))
  190. msleep(100);
  191. /* Mark hardware accessible again as we are out of D3 state by now */
  192. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  193. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
  194. int mask = INTR_MASK;
  195. ehci_prepare_ports_for_controller_resume(ehci);
  196. if (!hcd->self.root_hub->do_remote_wakeup)
  197. mask &= ~STS_PCD;
  198. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  199. ehci_readl(ehci, &ehci->regs->intr_enable);
  200. return 0;
  201. }
  202. usb_root_hub_lost_power(hcd->self.root_hub);
  203. (void) ehci_halt(ehci);
  204. (void) ehci_reset(ehci);
  205. /* emptying the schedule aborts any urbs */
  206. spin_lock_irq(&ehci->lock);
  207. if (ehci->reclaim)
  208. end_unlink_async(ehci);
  209. ehci_work(ehci);
  210. spin_unlock_irq(&ehci->lock);
  211. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  212. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  213. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  214. /* here we "know" root ports should always stay powered */
  215. ehci_port_power(ehci, 1);
  216. hcd->state = HC_STATE_SUSPENDED;
  217. return 0;
  218. }
  219. #else
  220. #define s5p_ehci_suspend NULL
  221. #define s5p_ehci_resume NULL
  222. #endif
  223. static const struct dev_pm_ops s5p_ehci_pm_ops = {
  224. .suspend = s5p_ehci_suspend,
  225. .resume = s5p_ehci_resume,
  226. };
  227. static struct platform_driver s5p_ehci_driver = {
  228. .probe = s5p_ehci_probe,
  229. .remove = __devexit_p(s5p_ehci_remove),
  230. .shutdown = s5p_ehci_shutdown,
  231. .driver = {
  232. .name = "s5p-ehci",
  233. .owner = THIS_MODULE,
  234. .pm = &s5p_ehci_pm_ops,
  235. }
  236. };
  237. MODULE_ALIAS("platform:s5p-ehci");