ehci-brcm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020, Broadcom */
  3. #include <linux/clk.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/err.h>
  6. #include <linux/kernel.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/usb.h>
  11. #include <linux/usb/hcd.h>
  12. #include <linux/iopoll.h>
  13. #include "ehci.h"
  14. #define hcd_to_ehci_priv(h) ((struct brcm_priv *)hcd_to_ehci(h)->priv)
  15. struct brcm_priv {
  16. struct clk *clk;
  17. };
  18. /*
  19. * ehci_brcm_wait_for_sof
  20. * Wait for start of next microframe, then wait extra delay microseconds
  21. */
  22. static inline void ehci_brcm_wait_for_sof(struct ehci_hcd *ehci, u32 delay)
  23. {
  24. u32 frame_idx = ehci_readl(ehci, &ehci->regs->frame_index);
  25. u32 val;
  26. int res;
  27. /* Wait for next microframe (every 125 usecs) */
  28. res = readl_relaxed_poll_timeout(&ehci->regs->frame_index, val,
  29. val != frame_idx, 1, 130);
  30. if (res)
  31. ehci_err(ehci, "Error waiting for SOF\n");
  32. udelay(delay);
  33. }
  34. /*
  35. * ehci_brcm_hub_control
  36. * The EHCI controller has a bug where it can violate the SOF
  37. * interval between the first two SOF's transmitted after resume
  38. * if the resume occurs near the end of the microframe. This causees
  39. * the controller to detect babble on the suspended port and
  40. * will eventually cause the controller to reset the port.
  41. * The fix is to Intercept the echi-hcd request to complete RESUME and
  42. * align it to the start of the next microframe.
  43. * See SWLINUX-1909 for more details
  44. */
  45. static int ehci_brcm_hub_control(
  46. struct usb_hcd *hcd,
  47. u16 typeReq,
  48. u16 wValue,
  49. u16 wIndex,
  50. char *buf,
  51. u16 wLength)
  52. {
  53. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  54. int ports = HCS_N_PORTS(ehci->hcs_params);
  55. u32 __iomem *status_reg;
  56. unsigned long flags;
  57. int retval, irq_disabled = 0;
  58. status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
  59. /*
  60. * RESUME is cleared when GetPortStatus() is called 20ms after start
  61. * of RESUME
  62. */
  63. if ((typeReq == GetPortStatus) &&
  64. (wIndex && wIndex <= ports) &&
  65. ehci->reset_done[wIndex-1] &&
  66. time_after_eq(jiffies, ehci->reset_done[wIndex-1]) &&
  67. (ehci_readl(ehci, status_reg) & PORT_RESUME)) {
  68. /*
  69. * to make sure we are not interrupted until RESUME bit
  70. * is cleared, disable interrupts on current CPU
  71. */
  72. ehci_dbg(ehci, "SOF alignment workaround\n");
  73. irq_disabled = 1;
  74. local_irq_save(flags);
  75. ehci_brcm_wait_for_sof(ehci, 5);
  76. }
  77. retval = ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  78. if (irq_disabled)
  79. local_irq_restore(flags);
  80. return retval;
  81. }
  82. static int ehci_brcm_reset(struct usb_hcd *hcd)
  83. {
  84. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  85. int len;
  86. ehci->big_endian_mmio = 1;
  87. ehci->caps = (void __iomem *)hcd->regs;
  88. len = HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  89. ehci->regs = (void __iomem *)(hcd->regs + len);
  90. /* This fixes the lockup during reboot due to prior interrupts */
  91. ehci_writel(ehci, CMD_RESET, &ehci->regs->command);
  92. mdelay(10);
  93. /*
  94. * SWLINUX-1705: Avoid OUT packet underflows during high memory
  95. * bus usage
  96. * port_status[0x0f] = Broadcom-proprietary USB_EHCI_INSNREG00 @ 0x90
  97. */
  98. ehci_writel(ehci, 0x00800040, &ehci->regs->port_status[0x10]);
  99. ehci_writel(ehci, 0x00000001, &ehci->regs->port_status[0x12]);
  100. return ehci_setup(hcd);
  101. }
  102. static struct hc_driver __read_mostly ehci_brcm_hc_driver;
  103. static const struct ehci_driver_overrides brcm_overrides __initconst = {
  104. .reset = ehci_brcm_reset,
  105. .extra_priv_size = sizeof(struct brcm_priv),
  106. };
  107. static int ehci_brcm_probe(struct platform_device *pdev)
  108. {
  109. struct device *dev = &pdev->dev;
  110. struct resource *res_mem;
  111. struct brcm_priv *priv;
  112. struct usb_hcd *hcd;
  113. int irq;
  114. int err;
  115. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  116. if (err)
  117. return err;
  118. irq = platform_get_irq(pdev, 0);
  119. if (irq <= 0)
  120. return irq ? irq : -EINVAL;
  121. /* Hook the hub control routine to work around a bug */
  122. ehci_brcm_hc_driver.hub_control = ehci_brcm_hub_control;
  123. /* initialize hcd */
  124. hcd = usb_create_hcd(&ehci_brcm_hc_driver, dev, dev_name(dev));
  125. if (!hcd)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, hcd);
  128. priv = hcd_to_ehci_priv(hcd);
  129. priv->clk = devm_clk_get_optional(dev, NULL);
  130. if (IS_ERR(priv->clk)) {
  131. err = PTR_ERR(priv->clk);
  132. goto err_hcd;
  133. }
  134. err = clk_prepare_enable(priv->clk);
  135. if (err)
  136. goto err_hcd;
  137. hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res_mem);
  138. if (IS_ERR(hcd->regs)) {
  139. err = PTR_ERR(hcd->regs);
  140. goto err_clk;
  141. }
  142. hcd->rsrc_start = res_mem->start;
  143. hcd->rsrc_len = resource_size(res_mem);
  144. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  145. if (err)
  146. goto err_clk;
  147. device_wakeup_enable(hcd->self.controller);
  148. device_enable_async_suspend(hcd->self.controller);
  149. return 0;
  150. err_clk:
  151. clk_disable_unprepare(priv->clk);
  152. err_hcd:
  153. usb_put_hcd(hcd);
  154. return err;
  155. }
  156. static int ehci_brcm_remove(struct platform_device *dev)
  157. {
  158. struct usb_hcd *hcd = platform_get_drvdata(dev);
  159. struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
  160. usb_remove_hcd(hcd);
  161. clk_disable_unprepare(priv->clk);
  162. usb_put_hcd(hcd);
  163. return 0;
  164. }
  165. static int __maybe_unused ehci_brcm_suspend(struct device *dev)
  166. {
  167. int ret;
  168. struct usb_hcd *hcd = dev_get_drvdata(dev);
  169. struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
  170. bool do_wakeup = device_may_wakeup(dev);
  171. ret = ehci_suspend(hcd, do_wakeup);
  172. if (ret)
  173. return ret;
  174. clk_disable_unprepare(priv->clk);
  175. return 0;
  176. }
  177. static int __maybe_unused ehci_brcm_resume(struct device *dev)
  178. {
  179. struct usb_hcd *hcd = dev_get_drvdata(dev);
  180. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  181. struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
  182. int err;
  183. err = clk_prepare_enable(priv->clk);
  184. if (err)
  185. return err;
  186. /*
  187. * SWLINUX-1705: Avoid OUT packet underflows during high memory
  188. * bus usage
  189. * port_status[0x0f] = Broadcom-proprietary USB_EHCI_INSNREG00
  190. * @ 0x90
  191. */
  192. ehci_writel(ehci, 0x00800040, &ehci->regs->port_status[0x10]);
  193. ehci_writel(ehci, 0x00000001, &ehci->regs->port_status[0x12]);
  194. ehci_resume(hcd, false);
  195. pm_runtime_disable(dev);
  196. pm_runtime_set_active(dev);
  197. pm_runtime_enable(dev);
  198. return 0;
  199. }
  200. static SIMPLE_DEV_PM_OPS(ehci_brcm_pm_ops, ehci_brcm_suspend,
  201. ehci_brcm_resume);
  202. static const struct of_device_id brcm_ehci_of_match[] = {
  203. { .compatible = "brcm,ehci-brcm-v2", },
  204. { .compatible = "brcm,bcm7445-ehci", },
  205. {}
  206. };
  207. static struct platform_driver ehci_brcm_driver = {
  208. .probe = ehci_brcm_probe,
  209. .remove = ehci_brcm_remove,
  210. .shutdown = usb_hcd_platform_shutdown,
  211. .driver = {
  212. .name = "ehci-brcm",
  213. .pm = &ehci_brcm_pm_ops,
  214. .of_match_table = brcm_ehci_of_match,
  215. }
  216. };
  217. static int __init ehci_brcm_init(void)
  218. {
  219. if (usb_disabled())
  220. return -ENODEV;
  221. ehci_init_driver(&ehci_brcm_hc_driver, &brcm_overrides);
  222. return platform_driver_register(&ehci_brcm_driver);
  223. }
  224. module_init(ehci_brcm_init);
  225. static void __exit ehci_brcm_exit(void)
  226. {
  227. platform_driver_unregister(&ehci_brcm_driver);
  228. }
  229. module_exit(ehci_brcm_exit);
  230. MODULE_ALIAS("platform:ehci-brcm");
  231. MODULE_DESCRIPTION("EHCI Broadcom STB driver");
  232. MODULE_AUTHOR("Al Cooper");
  233. MODULE_LICENSE("GPL");