host.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * host.c - ChipIdea USB host controller driver
  4. *
  5. * Copyright (c) 2012 Intel Corporation
  6. *
  7. * Author: Alexander Shishkin
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/usb.h>
  12. #include <linux/usb/hcd.h>
  13. #include <linux/usb/chipidea.h>
  14. #include <linux/regulator/consumer.h>
  15. #include "../host/ehci.h"
  16. #include "ci.h"
  17. #include "bits.h"
  18. #include "host.h"
  19. static struct hc_driver __read_mostly ci_ehci_hc_driver;
  20. static int (*orig_bus_suspend)(struct usb_hcd *hcd);
  21. struct ehci_ci_priv {
  22. struct regulator *reg_vbus;
  23. };
  24. static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
  25. {
  26. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  27. struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
  28. struct device *dev = hcd->self.controller;
  29. struct ci_hdrc *ci = dev_get_drvdata(dev);
  30. int ret = 0;
  31. int port = HCS_N_PORTS(ehci->hcs_params);
  32. if (priv->reg_vbus) {
  33. if (port > 1) {
  34. dev_warn(dev,
  35. "Not support multi-port regulator control\n");
  36. return 0;
  37. }
  38. if (enable)
  39. ret = regulator_enable(priv->reg_vbus);
  40. else
  41. ret = regulator_disable(priv->reg_vbus);
  42. if (ret) {
  43. dev_err(dev,
  44. "Failed to %s vbus regulator, ret=%d\n",
  45. enable ? "enable" : "disable", ret);
  46. return ret;
  47. }
  48. }
  49. if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
  50. /*
  51. * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
  52. * As HSIC is always HS, this should be safe for others.
  53. */
  54. hw_port_test_set(ci, 5);
  55. hw_port_test_set(ci, 0);
  56. }
  57. return 0;
  58. };
  59. static int ehci_ci_reset(struct usb_hcd *hcd)
  60. {
  61. struct device *dev = hcd->self.controller;
  62. struct ci_hdrc *ci = dev_get_drvdata(dev);
  63. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  64. int ret;
  65. ret = ehci_setup(hcd);
  66. if (ret)
  67. return ret;
  68. ehci->need_io_watchdog = 0;
  69. if (ci->platdata->notify_event) {
  70. ret = ci->platdata->notify_event(ci,
  71. CI_HDRC_CONTROLLER_RESET_EVENT);
  72. if (ret)
  73. return ret;
  74. }
  75. ci_platform_configure(ci);
  76. return ret;
  77. }
  78. static const struct ehci_driver_overrides ehci_ci_overrides = {
  79. .extra_priv_size = sizeof(struct ehci_ci_priv),
  80. .port_power = ehci_ci_portpower,
  81. .reset = ehci_ci_reset,
  82. };
  83. static irqreturn_t host_irq(struct ci_hdrc *ci)
  84. {
  85. return usb_hcd_irq(ci->irq, ci->hcd);
  86. }
  87. static int host_start(struct ci_hdrc *ci)
  88. {
  89. struct usb_hcd *hcd;
  90. struct ehci_hcd *ehci;
  91. struct ehci_ci_priv *priv;
  92. int ret;
  93. if (usb_disabled())
  94. return -ENODEV;
  95. hcd = __usb_create_hcd(&ci_ehci_hc_driver, ci->dev->parent,
  96. ci->dev, dev_name(ci->dev), NULL);
  97. if (!hcd)
  98. return -ENOMEM;
  99. dev_set_drvdata(ci->dev, ci);
  100. hcd->rsrc_start = ci->hw_bank.phys;
  101. hcd->rsrc_len = ci->hw_bank.size;
  102. hcd->regs = ci->hw_bank.abs;
  103. hcd->has_tt = 1;
  104. hcd->power_budget = ci->platdata->power_budget;
  105. hcd->tpl_support = ci->platdata->tpl_support;
  106. if (ci->phy || ci->usb_phy)
  107. hcd->skip_phy_initialization = 1;
  108. ehci = hcd_to_ehci(hcd);
  109. ehci->caps = ci->hw_bank.cap;
  110. ehci->has_hostpc = ci->hw_bank.lpm;
  111. ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
  112. ehci->imx28_write_fix = ci->imx28_write_fix;
  113. priv = (struct ehci_ci_priv *)ehci->priv;
  114. priv->reg_vbus = NULL;
  115. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
  116. if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
  117. ret = regulator_enable(ci->platdata->reg_vbus);
  118. if (ret) {
  119. dev_err(ci->dev,
  120. "Failed to enable vbus regulator, ret=%d\n",
  121. ret);
  122. goto put_hcd;
  123. }
  124. } else {
  125. priv->reg_vbus = ci->platdata->reg_vbus;
  126. }
  127. }
  128. ret = usb_add_hcd(hcd, 0, 0);
  129. if (ret) {
  130. goto disable_reg;
  131. } else {
  132. struct usb_otg *otg = &ci->otg;
  133. ci->hcd = hcd;
  134. if (ci_otg_is_fsm_mode(ci)) {
  135. otg->host = &hcd->self;
  136. hcd->self.otg_port = 1;
  137. }
  138. }
  139. return ret;
  140. disable_reg:
  141. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  142. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  143. regulator_disable(ci->platdata->reg_vbus);
  144. put_hcd:
  145. usb_put_hcd(hcd);
  146. return ret;
  147. }
  148. static void host_stop(struct ci_hdrc *ci)
  149. {
  150. struct usb_hcd *hcd = ci->hcd;
  151. if (hcd) {
  152. if (ci->platdata->notify_event)
  153. ci->platdata->notify_event(ci,
  154. CI_HDRC_CONTROLLER_STOPPED_EVENT);
  155. usb_remove_hcd(hcd);
  156. ci->role = CI_ROLE_END;
  157. synchronize_irq(ci->irq);
  158. usb_put_hcd(hcd);
  159. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  160. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  161. regulator_disable(ci->platdata->reg_vbus);
  162. }
  163. ci->hcd = NULL;
  164. ci->otg.host = NULL;
  165. }
  166. void ci_hdrc_host_destroy(struct ci_hdrc *ci)
  167. {
  168. if (ci->role == CI_ROLE_HOST && ci->hcd)
  169. host_stop(ci);
  170. }
  171. static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
  172. {
  173. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  174. int port;
  175. u32 tmp;
  176. int ret = orig_bus_suspend(hcd);
  177. if (ret)
  178. return ret;
  179. port = HCS_N_PORTS(ehci->hcs_params);
  180. while (port--) {
  181. u32 __iomem *reg = &ehci->regs->port_status[port];
  182. u32 portsc = ehci_readl(ehci, reg);
  183. if (portsc & PORT_CONNECT) {
  184. /*
  185. * For chipidea, the resume signal will be ended
  186. * automatically, so for remote wakeup case, the
  187. * usbcmd.rs may not be set before the resume has
  188. * ended if other resume paths consumes too much
  189. * time (~24ms), in that case, the SOF will not
  190. * send out within 3ms after resume ends, then the
  191. * high speed device will enter full speed mode.
  192. */
  193. tmp = ehci_readl(ehci, &ehci->regs->command);
  194. tmp |= CMD_RUN;
  195. ehci_writel(ehci, tmp, &ehci->regs->command);
  196. /*
  197. * It needs a short delay between set RS bit and PHCD.
  198. */
  199. usleep_range(150, 200);
  200. break;
  201. }
  202. }
  203. return 0;
  204. }
  205. int ci_hdrc_host_init(struct ci_hdrc *ci)
  206. {
  207. struct ci_role_driver *rdrv;
  208. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
  209. return -ENXIO;
  210. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  211. if (!rdrv)
  212. return -ENOMEM;
  213. rdrv->start = host_start;
  214. rdrv->stop = host_stop;
  215. rdrv->irq = host_irq;
  216. rdrv->name = "host";
  217. ci->roles[CI_ROLE_HOST] = rdrv;
  218. return 0;
  219. }
  220. void ci_hdrc_host_driver_init(void)
  221. {
  222. ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
  223. orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
  224. ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
  225. }