ohci-sm501.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  165. usb_put_hcd(hcd);
  166. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  167. if (mem)
  168. release_mem_region(mem->start, resource_size(mem));
  169. /* mask interrupts and disable power */
  170. sm501_modify_reg(pdev->dev.parent, SM501_IRQ_MASK, 0, 1 << 6);
  171. sm501_unit_power(pdev->dev.parent, SM501_GATE_USB_HOST, 0);
  172. return 0;
  173. }
  174. /*-------------------------------------------------------------------------*/
  175. #ifdef CONFIG_PM
  176. static int ohci_sm501_suspend(struct platform_device *pdev, pm_message_t msg)
  177. {
  178. struct device *dev = &pdev->dev;
  179. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  180. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  181. bool do_wakeup = device_may_wakeup(dev);
  182. int ret;
  183. if (time_before(jiffies, ohci->next_statechange))
  184. msleep(5);
  185. ohci->next_statechange = jiffies;
  186. ret = ohci_suspend(hcd, do_wakeup);
  187. if (ret)
  188. return ret;
  189. sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 0);
  190. return ret;
  191. }
  192. static int ohci_sm501_resume(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  196. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  197. if (time_before(jiffies, ohci->next_statechange))
  198. msleep(5);
  199. ohci->next_statechange = jiffies;
  200. sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1);
  201. ohci_resume(hcd, false);
  202. return 0;
  203. }
  204. #else
  205. #define ohci_sm501_suspend NULL
  206. #define ohci_sm501_resume NULL
  207. #endif
  208. /*-------------------------------------------------------------------------*/
  209. /*
  210. * Driver definition to register with the SM501 bus
  211. */
  212. static struct platform_driver ohci_hcd_sm501_driver = {
  213. .probe = ohci_hcd_sm501_drv_probe,
  214. .remove = ohci_hcd_sm501_drv_remove,
  215. .shutdown = usb_hcd_platform_shutdown,
  216. .suspend = ohci_sm501_suspend,
  217. .resume = ohci_sm501_resume,
  218. .driver = {
  219. .name = "sm501-usb",
  220. },
  221. };
  222. MODULE_ALIAS("platform:sm501-usb");