xhci-rockchip.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2016 Rockchip, Inc.
  3. * Authors: Daniel Meng <daniel.meng@rock-chips.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <fdtdec.h>
  10. #include <libfdt.h>
  11. #include <malloc.h>
  12. #include <usb.h>
  13. #include <watchdog.h>
  14. #include <asm/gpio.h>
  15. #include <linux/errno.h>
  16. #include <linux/compat.h>
  17. #include <linux/usb/dwc3.h>
  18. #include "xhci.h"
  19. DECLARE_GLOBAL_DATA_PTR;
  20. struct rockchip_xhci_platdata {
  21. fdt_addr_t hcd_base;
  22. fdt_addr_t phy_base;
  23. struct gpio_desc vbus_gpio;
  24. };
  25. /*
  26. * Contains pointers to register base addresses
  27. * for the usb controller.
  28. */
  29. struct rockchip_xhci {
  30. struct usb_platdata usb_plat;
  31. struct xhci_ctrl ctrl;
  32. struct xhci_hccr *hcd;
  33. struct dwc3 *dwc3_reg;
  34. };
  35. static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
  36. {
  37. struct rockchip_xhci_platdata *plat = dev_get_platdata(dev);
  38. struct udevice *child;
  39. int ret = 0;
  40. /*
  41. * Get the base address for XHCI controller from the device node
  42. */
  43. plat->hcd_base = dev_get_addr(dev);
  44. if (plat->hcd_base == FDT_ADDR_T_NONE) {
  45. debug("Can't get the XHCI register base address\n");
  46. return -ENXIO;
  47. }
  48. /* Get the base address for usbphy from the device node */
  49. for (device_find_first_child(dev, &child); child;
  50. device_find_next_child(&child)) {
  51. if (!of_device_is_compatible(child, "rockchip,rk3399-usb3-phy"))
  52. continue;
  53. plat->phy_base = dev_get_addr(child);
  54. break;
  55. }
  56. if (plat->phy_base == FDT_ADDR_T_NONE) {
  57. debug("Can't get the usbphy register address\n");
  58. return -ENXIO;
  59. }
  60. /* Vbus gpio */
  61. ret = gpio_request_by_name(dev, "rockchip,vbus-gpio", 0,
  62. &plat->vbus_gpio, GPIOD_IS_OUT);
  63. if (ret)
  64. debug("rockchip,vbus-gpio node missing!");
  65. return 0;
  66. }
  67. /*
  68. * rockchip_dwc3_phy_setup() - Configure USB PHY Interface of DWC3 Core
  69. * @dwc: Pointer to our controller context structure
  70. * @dev: Pointer to ulcass device
  71. */
  72. static void rockchip_dwc3_phy_setup(struct dwc3 *dwc3_reg,
  73. struct udevice *dev)
  74. {
  75. u32 reg;
  76. const void *blob = gd->fdt_blob;
  77. u32 utmi_bits;
  78. /* Set dwc3 usb2 phy config */
  79. reg = readl(&dwc3_reg->g_usb2phycfg[0]);
  80. if (fdtdec_get_bool(blob, dev->of_offset,
  81. "snps,dis-enblslpm-quirk"))
  82. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  83. utmi_bits = fdtdec_get_int(blob, dev->of_offset,
  84. "snps,phyif-utmi-bits", -1);
  85. if (utmi_bits == 16) {
  86. reg |= DWC3_GUSB2PHYCFG_PHYIF;
  87. reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK;
  88. reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT;
  89. } else if (utmi_bits == 8) {
  90. reg &= ~DWC3_GUSB2PHYCFG_PHYIF;
  91. reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK;
  92. reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_8BIT;
  93. }
  94. if (fdtdec_get_bool(blob, dev->of_offset,
  95. "snps,dis-u2-freeclk-exists-quirk"))
  96. reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
  97. if (fdtdec_get_bool(blob, dev->of_offset,
  98. "snps,dis-u2-susphy-quirk"))
  99. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  100. writel(reg, &dwc3_reg->g_usb2phycfg[0]);
  101. }
  102. static int rockchip_xhci_core_init(struct rockchip_xhci *rkxhci,
  103. struct udevice *dev)
  104. {
  105. int ret;
  106. ret = dwc3_core_init(rkxhci->dwc3_reg);
  107. if (ret) {
  108. debug("failed to initialize core\n");
  109. return ret;
  110. }
  111. rockchip_dwc3_phy_setup(rkxhci->dwc3_reg, dev);
  112. /* We are hard-coding DWC3 core to Host Mode */
  113. dwc3_set_mode(rkxhci->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  114. return 0;
  115. }
  116. static int rockchip_xhci_core_exit(struct rockchip_xhci *rkxhci)
  117. {
  118. return 0;
  119. }
  120. static int xhci_usb_probe(struct udevice *dev)
  121. {
  122. struct rockchip_xhci_platdata *plat = dev_get_platdata(dev);
  123. struct rockchip_xhci *ctx = dev_get_priv(dev);
  124. struct xhci_hcor *hcor;
  125. int ret;
  126. ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
  127. ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
  128. hcor = (struct xhci_hcor *)((uint64_t)ctx->hcd +
  129. HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase)));
  130. /* setup the Vbus gpio here */
  131. if (dm_gpio_is_valid(&plat->vbus_gpio))
  132. dm_gpio_set_value(&plat->vbus_gpio, 1);
  133. ret = rockchip_xhci_core_init(ctx, dev);
  134. if (ret) {
  135. debug("XHCI: failed to initialize controller\n");
  136. return ret;
  137. }
  138. return xhci_register(dev, ctx->hcd, hcor);
  139. }
  140. static int xhci_usb_remove(struct udevice *dev)
  141. {
  142. struct rockchip_xhci *ctx = dev_get_priv(dev);
  143. int ret;
  144. ret = xhci_deregister(dev);
  145. if (ret)
  146. return ret;
  147. ret = rockchip_xhci_core_exit(ctx);
  148. if (ret)
  149. return ret;
  150. return 0;
  151. }
  152. static const struct udevice_id xhci_usb_ids[] = {
  153. { .compatible = "rockchip,rk3399-xhci" },
  154. { }
  155. };
  156. U_BOOT_DRIVER(usb_xhci) = {
  157. .name = "xhci_rockchip",
  158. .id = UCLASS_USB,
  159. .of_match = xhci_usb_ids,
  160. .ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
  161. .probe = xhci_usb_probe,
  162. .remove = xhci_usb_remove,
  163. .ops = &xhci_usb_ops,
  164. .bind = dm_scan_fdt_dev,
  165. .platdata_auto_alloc_size = sizeof(struct rockchip_xhci_platdata),
  166. .priv_auto_alloc_size = sizeof(struct rockchip_xhci),
  167. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  168. };
  169. static const struct udevice_id usb_phy_ids[] = {
  170. { .compatible = "rockchip,rk3399-usb3-phy" },
  171. { }
  172. };
  173. U_BOOT_DRIVER(usb_phy) = {
  174. .name = "usb_phy_rockchip",
  175. .of_match = usb_phy_ids,
  176. };