ohci-sm501.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2005 David Brownell
  6. * (C) Copyright 2002 Hewlett-Packard Company
  7. * (C) Copyright 2008 Magnus Damm
  8. *
  9. * SM501 Bus Glue - based on ohci-omap.c
  10. *
  11. * This file is licenced under the GPL.
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/sm501.h>
  18. #include <linux/sm501-regs.h>
  19. static int ohci_sm501_init(struct usb_hcd *hcd)
  20. {
  21. return ohci_init(hcd_to_ohci(hcd));
  22. }
  23. static int ohci_sm501_start(struct usb_hcd *hcd)
  24. {
  25. struct device *dev = hcd->self.controller;
  26. int ret;
  27. ret = ohci_run(hcd_to_ohci(hcd));
  28. if (ret < 0) {
  29. dev_err(dev, "can't start %s", hcd->self.bus_name);
  30. ohci_stop(hcd);
  31. }
  32. return ret;
  33. }
  34. /*-------------------------------------------------------------------------*/
  35. static const struct hc_driver ohci_sm501_hc_driver = {
  36. .description = hcd_name,
  37. .product_desc = "SM501 OHCI",
  38. .hcd_priv_size = sizeof(struct ohci_hcd),
  39. /*
  40. * generic hardware linkage
  41. */
  42. .irq = ohci_irq,
  43. .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM,
  44. /*
  45. * basic lifecycle operations
  46. */
  47. .reset = ohci_sm501_init,
  48. .start = ohci_sm501_start,
  49. .stop = ohci_stop,
  50. .shutdown = ohci_shutdown,
  51. /*
  52. * managing i/o requests and associated device resources
  53. */
  54. .urb_enqueue = ohci_urb_enqueue,
  55. .urb_dequeue = ohci_urb_dequeue,
  56. .endpoint_disable = ohci_endpoint_disable,
  57. /*
  58. * scheduling support
  59. */
  60. .get_frame_number = ohci_get_frame,
  61. /*
  62. * root hub support
  63. */
  64. .hub_status_data = ohci_hub_status_data,
  65. .hub_control = ohci_hub_control,
  66. #ifdef CONFIG_PM
  67. .bus_suspend = ohci_bus_suspend,
  68. .bus_resume = ohci_bus_resume,
  69. #endif
  70. .start_port_reset = ohci_start_port_reset,
  71. };
  72. /*-------------------------------------------------------------------------*/
  73. static int ohci_hcd_sm501_drv_probe(struct platform_device *pdev)
  74. {
  75. const struct hc_driver *driver = &ohci_sm501_hc_driver;
  76. struct device *dev = &pdev->dev;
  77. struct resource *res, *mem;
  78. int retval, irq;
  79. struct usb_hcd *hcd = NULL;
  80. irq = retval = platform_get_irq(pdev, 0);
  81. if (retval < 0)
  82. goto err0;
  83. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  84. if (mem == NULL) {
  85. dev_err(dev, "no resource definition for memory\n");
  86. retval = -ENOENT;
  87. goto err0;
  88. }
  89. if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
  90. dev_err(dev, "request_mem_region failed\n");
  91. retval = -EBUSY;
  92. goto err0;
  93. }
  94. /* The sm501 chip is equipped with local memory that may be used
  95. * by on-chip devices such as the video controller and the usb host.
  96. * This driver uses dma_declare_coherent_memory() to make sure
  97. * usb allocations with dma_alloc_coherent() allocate from
  98. * this local memory. The dma_handle returned by dma_alloc_coherent()
  99. * will be an offset starting from 0 for the first local memory byte.
  100. *
  101. * So as long as data is allocated using dma_alloc_coherent() all is
  102. * fine. This is however not always the case - buffers may be allocated
  103. * using kmalloc() - so the usb core needs to be told that it must copy
  104. * data into our local memory if the buffers happen to be placed in
  105. * regular memory. The HCD_LOCAL_MEM flag does just that.
  106. */
  107. if (!dma_declare_coherent_memory(dev, mem->start,
  108. mem->start - mem->parent->start,
  109. resource_size(mem),
  110. DMA_MEMORY_MAP |
  111. DMA_MEMORY_EXCLUSIVE)) {
  112. dev_err(dev, "cannot declare coherent memory\n");
  113. retval = -ENXIO;
  114. goto err1;
  115. }
  116. /* allocate, reserve and remap resources for registers */
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. if (res == NULL) {
  119. dev_err(dev, "no resource definition for registers\n");
  120. retval = -ENOENT;
  121. goto err2;
  122. }
  123. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  124. if (!hcd) {
  125. retval = -ENOMEM;
  126. goto err2;
  127. }
  128. hcd->rsrc_start = res->start;
  129. hcd->rsrc_len = resource_size(res);
  130. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, pdev->name)) {
  131. dev_err(dev, "request_mem_region failed\n");
  132. retval = -EBUSY;
  133. goto err3;
  134. }
  135. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  136. if (hcd->regs == NULL) {
  137. dev_err(dev, "cannot remap registers\n");
  138. retval = -ENXIO;
  139. goto err4;
  140. }
  141. ohci_hcd_init(hcd_to_ohci(hcd));
  142. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  143. if (retval)
  144. goto err5;
  145. device_wakeup_enable(hcd->self.controller);
  146. /* enable power and unmask interrupts */
  147. sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1);
  148. sm501_modify_reg(dev->parent, SM501_IRQ_MASK, 1 << 6, 0);
  149. return 0;
  150. err5:
  151. iounmap(hcd->regs);
  152. err4:
  153. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  154. err3:
  155. usb_put_hcd(hcd);
  156. err2:
  157. dma_release_declared_memory(dev);
  158. err1:
  159. release_mem_region(mem->start, resource_size(mem));
  160. err0:
  161. return retval;
  162. }
  163. static int ohci_hcd_sm501_drv_remove(struct platform_device *pdev)
  164. {
  165. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  166. struct resource *mem;
  167. usb_remove_hcd(hcd);
  168. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  169. usb_put_hcd(hcd);
  170. dma_release_declared_memory(&pdev->dev);
  171. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  172. if (mem)
  173. release_mem_region(mem->start, resource_size(mem));
  174. /* mask interrupts and disable power */
  175. sm501_modify_reg(pdev->dev.parent, SM501_IRQ_MASK, 0, 1 << 6);
  176. sm501_unit_power(pdev->dev.parent, SM501_GATE_USB_HOST, 0);
  177. return 0;
  178. }
  179. /*-------------------------------------------------------------------------*/
  180. #ifdef CONFIG_PM
  181. static int ohci_sm501_suspend(struct platform_device *pdev, pm_message_t msg)
  182. {
  183. struct device *dev = &pdev->dev;
  184. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  185. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  186. bool do_wakeup = device_may_wakeup(dev);
  187. int ret;
  188. if (time_before(jiffies, ohci->next_statechange))
  189. msleep(5);
  190. ohci->next_statechange = jiffies;
  191. ret = ohci_suspend(hcd, do_wakeup);
  192. if (ret)
  193. return ret;
  194. sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 0);
  195. return ret;
  196. }
  197. static int ohci_sm501_resume(struct platform_device *pdev)
  198. {
  199. struct device *dev = &pdev->dev;
  200. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  201. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  202. if (time_before(jiffies, ohci->next_statechange))
  203. msleep(5);
  204. ohci->next_statechange = jiffies;
  205. sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1);
  206. ohci_resume(hcd, false);
  207. return 0;
  208. }
  209. #else
  210. #define ohci_sm501_suspend NULL
  211. #define ohci_sm501_resume NULL
  212. #endif
  213. /*-------------------------------------------------------------------------*/
  214. /*
  215. * Driver definition to register with the SM501 bus
  216. */
  217. static struct platform_driver ohci_hcd_sm501_driver = {
  218. .probe = ohci_hcd_sm501_drv_probe,
  219. .remove = ohci_hcd_sm501_drv_remove,
  220. .shutdown = usb_hcd_platform_shutdown,
  221. .suspend = ohci_sm501_suspend,
  222. .resume = ohci_sm501_resume,
  223. .driver = {
  224. .name = "sm501-usb",
  225. },
  226. };
  227. MODULE_ALIAS("platform:sm501-usb");