host.c 6.6 KB

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