host.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * host.c - DesignWare USB3 DRD Controller Host Glue
  4. *
  5. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/hcd.h>
  14. #include "../host/xhci-port.h"
  15. #include "../host/xhci-ext-caps.h"
  16. #include "../host/xhci-caps.h"
  17. #include "../host/xhci-plat.h"
  18. #include "core.h"
  19. #define XHCI_HCSPARAMS1 0x4
  20. #define XHCI_PORTSC_BASE 0x400
  21. /**
  22. * dwc3_power_off_all_roothub_ports - Power off all Root hub ports
  23. * @dwc: Pointer to our controller context structure
  24. */
  25. static void dwc3_power_off_all_roothub_ports(struct dwc3 *dwc)
  26. {
  27. void __iomem *xhci_regs;
  28. u32 op_regs_base;
  29. int port_num;
  30. u32 offset;
  31. u32 reg;
  32. int i;
  33. /* xhci regs are not mapped yet, do it temporarily here */
  34. if (dwc->xhci_resources[0].start) {
  35. if (dwc->xhci_resources[0].flags & IORESOURCE_MEM_NONPOSTED)
  36. xhci_regs = ioremap_np(dwc->xhci_resources[0].start, DWC3_XHCI_REGS_END);
  37. else
  38. xhci_regs = ioremap(dwc->xhci_resources[0].start, DWC3_XHCI_REGS_END);
  39. if (!xhci_regs) {
  40. dev_err(dwc->dev, "Failed to ioremap xhci_regs\n");
  41. return;
  42. }
  43. op_regs_base = HC_LENGTH(readl(xhci_regs));
  44. reg = readl(xhci_regs + XHCI_HCSPARAMS1);
  45. port_num = HCS_MAX_PORTS(reg);
  46. for (i = 1; i <= port_num; i++) {
  47. offset = op_regs_base + XHCI_PORTSC_BASE + 0x10 * (i - 1);
  48. reg = readl(xhci_regs + offset);
  49. reg &= ~PORT_POWER;
  50. writel(reg, xhci_regs + offset);
  51. }
  52. iounmap(xhci_regs);
  53. } else {
  54. dev_err(dwc->dev, "xhci base reg invalid\n");
  55. }
  56. }
  57. static void dwc3_xhci_plat_start(struct usb_hcd *hcd)
  58. {
  59. struct platform_device *pdev;
  60. struct dwc3 *dwc;
  61. if (!usb_hcd_is_primary_hcd(hcd))
  62. return;
  63. pdev = to_platform_device(hcd->self.controller);
  64. dwc = dev_get_drvdata(pdev->dev.parent);
  65. dwc3_enable_susphy(dwc, true);
  66. }
  67. static const struct xhci_plat_priv dwc3_xhci_plat_quirk = {
  68. .plat_start = dwc3_xhci_plat_start,
  69. };
  70. static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
  71. int irq, char *name)
  72. {
  73. struct platform_device *pdev = to_platform_device(dwc->dev);
  74. struct device_node *np = dev_of_node(&pdev->dev);
  75. dwc->xhci_resources[1].start = irq;
  76. dwc->xhci_resources[1].end = irq;
  77. dwc->xhci_resources[1].flags = IORESOURCE_IRQ | irq_get_trigger_type(irq);
  78. if (!name && np)
  79. dwc->xhci_resources[1].name = of_node_full_name(pdev->dev.of_node);
  80. else
  81. dwc->xhci_resources[1].name = name;
  82. }
  83. static int dwc3_host_get_irq(struct dwc3 *dwc)
  84. {
  85. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  86. int irq;
  87. irq = platform_get_irq_byname_optional(dwc3_pdev, "host");
  88. if (irq > 0) {
  89. dwc3_host_fill_xhci_irq_res(dwc, irq, "host");
  90. goto out;
  91. }
  92. if (irq == -EPROBE_DEFER)
  93. goto out;
  94. irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
  95. if (irq > 0) {
  96. dwc3_host_fill_xhci_irq_res(dwc, irq, "dwc_usb3");
  97. goto out;
  98. }
  99. if (irq == -EPROBE_DEFER)
  100. goto out;
  101. irq = platform_get_irq(dwc3_pdev, 0);
  102. if (irq > 0)
  103. dwc3_host_fill_xhci_irq_res(dwc, irq, NULL);
  104. out:
  105. return irq;
  106. }
  107. int dwc3_host_init(struct dwc3 *dwc)
  108. {
  109. struct property_entry props[6];
  110. struct platform_device *xhci;
  111. int ret, irq;
  112. int prop_idx = 0;
  113. /*
  114. * Some platforms need to power off all Root hub ports immediately after DWC3 set to host
  115. * mode to avoid VBUS glitch happen when xhci get reset later.
  116. */
  117. dwc3_power_off_all_roothub_ports(dwc);
  118. irq = dwc3_host_get_irq(dwc);
  119. if (irq < 0)
  120. return irq;
  121. xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
  122. if (!xhci) {
  123. dev_err(dwc->dev, "couldn't allocate xHCI device\n");
  124. return -ENOMEM;
  125. }
  126. xhci->dev.parent = dwc->dev;
  127. dwc->xhci = xhci;
  128. ret = platform_device_add_resources(xhci, dwc->xhci_resources,
  129. DWC3_XHCI_RESOURCES_NUM);
  130. if (ret) {
  131. dev_err(dwc->dev, "couldn't add resources to xHCI device\n");
  132. goto err;
  133. }
  134. memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
  135. props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-sg-trb-cache-size-quirk");
  136. props[prop_idx++] = PROPERTY_ENTRY_BOOL("write-64-hi-lo-quirk");
  137. if (dwc->usb3_lpm_capable)
  138. props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb3-lpm-capable");
  139. if (dwc->usb2_lpm_disable)
  140. props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb2-lpm-disable");
  141. /**
  142. * WORKAROUND: dwc3 revisions <=3.00a have a limitation
  143. * where Port Disable command doesn't work.
  144. *
  145. * The suggested workaround is that we avoid Port Disable
  146. * completely.
  147. *
  148. * This following flag tells XHCI to do just that.
  149. */
  150. if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A))
  151. props[prop_idx++] = PROPERTY_ENTRY_BOOL("quirk-broken-port-ped");
  152. props[prop_idx++] = PROPERTY_ENTRY_U16("num-hc-interrupters",
  153. dwc->num_hc_interrupters);
  154. if (prop_idx) {
  155. ret = device_create_managed_software_node(&xhci->dev, props, NULL);
  156. if (ret) {
  157. dev_err(dwc->dev, "failed to add properties to xHCI\n");
  158. goto err;
  159. }
  160. }
  161. ret = platform_device_add_data(xhci, &dwc3_xhci_plat_quirk,
  162. sizeof(struct xhci_plat_priv));
  163. if (ret)
  164. goto err;
  165. ret = platform_device_add(xhci);
  166. if (ret) {
  167. dev_err(dwc->dev, "failed to register xHCI device\n");
  168. goto err;
  169. }
  170. if (dwc->sys_wakeup) {
  171. /* Restore wakeup setting if switched from device */
  172. device_wakeup_enable(dwc->sysdev);
  173. /* Pass on wakeup setting to the new xhci platform device */
  174. device_init_wakeup(&xhci->dev, true);
  175. }
  176. return 0;
  177. err:
  178. platform_device_put(xhci);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(dwc3_host_init);
  182. void dwc3_host_exit(struct dwc3 *dwc)
  183. {
  184. if (dwc->sys_wakeup)
  185. device_init_wakeup(&dwc->xhci->dev, false);
  186. dwc3_enable_susphy(dwc, true);
  187. platform_device_unregister(dwc->xhci);
  188. dwc->xhci = NULL;
  189. }
  190. EXPORT_SYMBOL_GPL(dwc3_host_exit);