host.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. struct ehci_ci_priv {
  33. struct regulator *reg_vbus;
  34. };
  35. static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
  36. {
  37. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  38. struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
  39. struct device *dev = hcd->self.controller;
  40. struct ci_hdrc *ci = dev_get_drvdata(dev);
  41. int ret = 0;
  42. int port = HCS_N_PORTS(ehci->hcs_params);
  43. if (priv->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
  44. if (port > 1) {
  45. dev_warn(dev,
  46. "Not support multi-port regulator control\n");
  47. return 0;
  48. }
  49. if (enable)
  50. ret = regulator_enable(priv->reg_vbus);
  51. else
  52. ret = regulator_disable(priv->reg_vbus);
  53. if (ret) {
  54. dev_err(dev,
  55. "Failed to %s vbus regulator, ret=%d\n",
  56. enable ? "enable" : "disable", ret);
  57. return ret;
  58. }
  59. }
  60. return 0;
  61. };
  62. static const struct ehci_driver_overrides ehci_ci_overrides = {
  63. .extra_priv_size = sizeof(struct ehci_ci_priv),
  64. .port_power = ehci_ci_portpower,
  65. };
  66. static irqreturn_t host_irq(struct ci_hdrc *ci)
  67. {
  68. return usb_hcd_irq(ci->irq, ci->hcd);
  69. }
  70. static int host_start(struct ci_hdrc *ci)
  71. {
  72. struct usb_hcd *hcd;
  73. struct ehci_hcd *ehci;
  74. struct ehci_ci_priv *priv;
  75. int ret;
  76. if (usb_disabled())
  77. return -ENODEV;
  78. hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
  79. if (!hcd)
  80. return -ENOMEM;
  81. dev_set_drvdata(ci->dev, ci);
  82. hcd->rsrc_start = ci->hw_bank.phys;
  83. hcd->rsrc_len = ci->hw_bank.size;
  84. hcd->regs = ci->hw_bank.abs;
  85. hcd->has_tt = 1;
  86. hcd->power_budget = ci->platdata->power_budget;
  87. hcd->tpl_support = ci->platdata->tpl_support;
  88. if (ci->phy)
  89. hcd->phy = ci->phy;
  90. else
  91. hcd->usb_phy = ci->usb_phy;
  92. ehci = hcd_to_ehci(hcd);
  93. ehci->caps = ci->hw_bank.cap;
  94. ehci->has_hostpc = ci->hw_bank.lpm;
  95. ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
  96. ehci->imx28_write_fix = ci->imx28_write_fix;
  97. priv = (struct ehci_ci_priv *)ehci->priv;
  98. priv->reg_vbus = NULL;
  99. if (ci->platdata->reg_vbus)
  100. priv->reg_vbus = ci->platdata->reg_vbus;
  101. ret = usb_add_hcd(hcd, 0, 0);
  102. if (ret) {
  103. goto put_hcd;
  104. } else {
  105. struct usb_otg *otg = &ci->otg;
  106. ci->hcd = hcd;
  107. if (ci_otg_is_fsm_mode(ci)) {
  108. otg->host = &hcd->self;
  109. hcd->self.otg_port = 1;
  110. }
  111. }
  112. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  113. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  114. return ret;
  115. put_hcd:
  116. usb_put_hcd(hcd);
  117. return ret;
  118. }
  119. static void host_stop(struct ci_hdrc *ci)
  120. {
  121. struct usb_hcd *hcd = ci->hcd;
  122. if (hcd) {
  123. usb_remove_hcd(hcd);
  124. usb_put_hcd(hcd);
  125. }
  126. }
  127. void ci_hdrc_host_destroy(struct ci_hdrc *ci)
  128. {
  129. if (ci->role == CI_ROLE_HOST && ci->hcd)
  130. host_stop(ci);
  131. }
  132. int ci_hdrc_host_init(struct ci_hdrc *ci)
  133. {
  134. struct ci_role_driver *rdrv;
  135. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
  136. return -ENXIO;
  137. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  138. if (!rdrv)
  139. return -ENOMEM;
  140. rdrv->start = host_start;
  141. rdrv->stop = host_stop;
  142. rdrv->irq = host_irq;
  143. rdrv->name = "host";
  144. ci->roles[CI_ROLE_HOST] = rdrv;
  145. ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
  146. return 0;
  147. }