host.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 irqreturn_t host_irq(struct ci_hdrc *ci)
  33. {
  34. return usb_hcd_irq(ci->irq, ci->hcd);
  35. }
  36. static int host_start(struct ci_hdrc *ci)
  37. {
  38. struct usb_hcd *hcd;
  39. struct ehci_hcd *ehci;
  40. int ret;
  41. if (usb_disabled())
  42. return -ENODEV;
  43. hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
  44. if (!hcd)
  45. return -ENOMEM;
  46. dev_set_drvdata(ci->dev, ci);
  47. hcd->rsrc_start = ci->hw_bank.phys;
  48. hcd->rsrc_len = ci->hw_bank.size;
  49. hcd->regs = ci->hw_bank.abs;
  50. hcd->has_tt = 1;
  51. hcd->power_budget = ci->platdata->power_budget;
  52. hcd->phy = ci->transceiver;
  53. ehci = hcd_to_ehci(hcd);
  54. ehci->caps = ci->hw_bank.cap;
  55. ehci->has_hostpc = ci->hw_bank.lpm;
  56. ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
  57. ehci->imx28_write_fix = ci->imx28_write_fix;
  58. /*
  59. * vbus is always on if host is not in OTG FSM mode,
  60. * otherwise should be controlled by OTG FSM
  61. */
  62. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
  63. ret = regulator_enable(ci->platdata->reg_vbus);
  64. if (ret) {
  65. dev_err(ci->dev,
  66. "Failed to enable vbus regulator, ret=%d\n",
  67. ret);
  68. goto put_hcd;
  69. }
  70. }
  71. ret = usb_add_hcd(hcd, 0, 0);
  72. if (ret) {
  73. goto disable_reg;
  74. } else {
  75. struct usb_otg *otg = ci->transceiver->otg;
  76. ci->hcd = hcd;
  77. if (otg) {
  78. otg->host = &hcd->self;
  79. hcd->self.otg_port = 1;
  80. }
  81. }
  82. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  83. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  84. return ret;
  85. disable_reg:
  86. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci))
  87. regulator_disable(ci->platdata->reg_vbus);
  88. put_hcd:
  89. usb_put_hcd(hcd);
  90. return ret;
  91. }
  92. static void host_stop(struct ci_hdrc *ci)
  93. {
  94. struct usb_hcd *hcd = ci->hcd;
  95. if (hcd) {
  96. usb_remove_hcd(hcd);
  97. usb_put_hcd(hcd);
  98. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci))
  99. regulator_disable(ci->platdata->reg_vbus);
  100. }
  101. }
  102. void ci_hdrc_host_destroy(struct ci_hdrc *ci)
  103. {
  104. if (ci->role == CI_ROLE_HOST && ci->hcd)
  105. host_stop(ci);
  106. }
  107. int ci_hdrc_host_init(struct ci_hdrc *ci)
  108. {
  109. struct ci_role_driver *rdrv;
  110. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
  111. return -ENXIO;
  112. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  113. if (!rdrv)
  114. return -ENOMEM;
  115. rdrv->start = host_start;
  116. rdrv->stop = host_stop;
  117. rdrv->irq = host_irq;
  118. rdrv->name = "host";
  119. ci->roles[CI_ROLE_HOST] = rdrv;
  120. ehci_init_driver(&ci_ehci_hc_driver, NULL);
  121. return 0;
  122. }