ohci-sm501.c 6.7 KB

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