host.c 3.4 KB

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