cdns3-ti.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * cdns_ti-ti.c - TI specific Glue layer for Cadence USB Controller
  4. *
  5. * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com
  6. */
  7. #include <common.h>
  8. #include <asm-generic/io.h>
  9. #include <clk.h>
  10. #include <dm.h>
  11. #include <dm/device_compat.h>
  12. #include <linux/bitops.h>
  13. #include <linux/io.h>
  14. #include <linux/usb/otg.h>
  15. #include <malloc.h>
  16. #include "core.h"
  17. /* USB Wrapper register offsets */
  18. #define USBSS_PID 0x0
  19. #define USBSS_W1 0x4
  20. #define USBSS_STATIC_CONFIG 0x8
  21. #define USBSS_PHY_TEST 0xc
  22. #define USBSS_DEBUG_CTRL 0x10
  23. #define USBSS_DEBUG_INFO 0x14
  24. #define USBSS_DEBUG_LINK_STATE 0x18
  25. #define USBSS_DEVICE_CTRL 0x1c
  26. /* Wrapper 1 register bits */
  27. #define USBSS_W1_PWRUP_RST BIT(0)
  28. #define USBSS_W1_OVERCURRENT_SEL BIT(8)
  29. #define USBSS_W1_MODESTRAP_SEL BIT(9)
  30. #define USBSS_W1_OVERCURRENT BIT(16)
  31. #define USBSS_W1_MODESTRAP_MASK GENMASK(18, 17)
  32. #define USBSS_W1_MODESTRAP_SHIFT 17
  33. #define USBSS_W1_USB2_ONLY BIT(19)
  34. /* Static config register bits */
  35. #define USBSS1_STATIC_PLL_REF_SEL_MASK GENMASK(8, 5)
  36. #define USBSS1_STATIC_PLL_REF_SEL_SHIFT 5
  37. #define USBSS1_STATIC_LOOPBACK_MODE_MASK GENMASK(4, 3)
  38. #define USBSS1_STATIC_LOOPBACK_MODE_SHIFT 3
  39. #define USBSS1_STATIC_VBUS_SEL_MASK GENMASK(2, 1)
  40. #define USBSS1_STATIC_VBUS_SEL_SHIFT 1
  41. #define USBSS1_STATIC_LANE_REVERSE BIT(0)
  42. /* Modestrap modes */
  43. enum modestrap_mode { USBSS_MODESTRAP_MODE_NONE,
  44. USBSS_MODESTRAP_MODE_HOST,
  45. USBSS_MODESTRAP_MODE_PERIPHERAL};
  46. struct cdns_ti {
  47. struct udevice *dev;
  48. void __iomem *usbss;
  49. int usb2_only:1;
  50. int vbus_divider:1;
  51. struct clk *usb2_refclk;
  52. struct clk *lpm_clk;
  53. };
  54. static const int cdns_ti_rate_table[] = { /* in KHZ */
  55. 9600,
  56. 10000,
  57. 12000,
  58. 19200,
  59. 20000,
  60. 24000,
  61. 25000,
  62. 26000,
  63. 38400,
  64. 40000,
  65. 58000,
  66. 50000,
  67. 52000,
  68. };
  69. static inline u32 cdns_ti_readl(struct cdns_ti *data, u32 offset)
  70. {
  71. return readl(data->usbss + offset);
  72. }
  73. static inline void cdns_ti_writel(struct cdns_ti *data, u32 offset, u32 value)
  74. {
  75. writel(value, data->usbss + offset);
  76. }
  77. static int cdns_ti_probe(struct udevice *dev)
  78. {
  79. struct cdns_ti *data = dev_get_plat(dev);
  80. struct clk usb2_refclk;
  81. int modestrap_mode;
  82. unsigned long rate;
  83. int rate_code, i;
  84. u32 reg;
  85. int ret;
  86. data->dev = dev;
  87. data->usbss = dev_remap_addr_index(dev, 0);
  88. if (!data->usbss)
  89. return -EINVAL;
  90. ret = clk_get_by_name(dev, "ref", &usb2_refclk);
  91. if (ret) {
  92. dev_err(dev, "Failed to get usb2_refclk\n");
  93. return ret;
  94. }
  95. rate = clk_get_rate(&usb2_refclk);
  96. rate /= 1000; /* To KHz */
  97. for (i = 0; i < ARRAY_SIZE(cdns_ti_rate_table); i++) {
  98. if (cdns_ti_rate_table[i] == rate)
  99. break;
  100. }
  101. if (i == ARRAY_SIZE(cdns_ti_rate_table)) {
  102. dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
  103. return -EINVAL;
  104. }
  105. rate_code = i;
  106. /* assert RESET */
  107. reg = cdns_ti_readl(data, USBSS_W1);
  108. reg &= ~USBSS_W1_PWRUP_RST;
  109. cdns_ti_writel(data, USBSS_W1, reg);
  110. /* set static config */
  111. reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
  112. reg &= ~USBSS1_STATIC_PLL_REF_SEL_MASK;
  113. reg |= rate_code << USBSS1_STATIC_PLL_REF_SEL_SHIFT;
  114. reg &= ~USBSS1_STATIC_VBUS_SEL_MASK;
  115. data->vbus_divider = dev_read_bool(dev, "ti,vbus-divider");
  116. if (data->vbus_divider)
  117. reg |= 1 << USBSS1_STATIC_VBUS_SEL_SHIFT;
  118. cdns_ti_writel(data, USBSS_STATIC_CONFIG, reg);
  119. reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
  120. /* set USB2_ONLY mode if requested */
  121. reg = cdns_ti_readl(data, USBSS_W1);
  122. data->usb2_only = dev_read_bool(dev, "ti,usb2-only");
  123. if (data->usb2_only)
  124. reg |= USBSS_W1_USB2_ONLY;
  125. /* set modestrap */
  126. if (dev_read_bool(dev, "ti,modestrap-host"))
  127. modestrap_mode = USBSS_MODESTRAP_MODE_HOST;
  128. else if (dev_read_bool(dev, "ti,modestrap-peripheral"))
  129. modestrap_mode = USBSS_MODESTRAP_MODE_PERIPHERAL;
  130. else
  131. modestrap_mode = USBSS_MODESTRAP_MODE_NONE;
  132. reg |= USBSS_W1_MODESTRAP_SEL;
  133. reg &= ~USBSS_W1_MODESTRAP_MASK;
  134. reg |= modestrap_mode << USBSS_W1_MODESTRAP_SHIFT;
  135. cdns_ti_writel(data, USBSS_W1, reg);
  136. /* de-assert RESET */
  137. reg |= USBSS_W1_PWRUP_RST;
  138. cdns_ti_writel(data, USBSS_W1, reg);
  139. return 0;
  140. }
  141. static int cdns_ti_remove(struct udevice *dev)
  142. {
  143. struct cdns_ti *data = dev_get_plat(dev);
  144. u32 reg;
  145. /* put device back to RESET*/
  146. reg = cdns_ti_readl(data, USBSS_W1);
  147. reg &= ~USBSS_W1_PWRUP_RST;
  148. cdns_ti_writel(data, USBSS_W1, reg);
  149. return 0;
  150. }
  151. static const struct udevice_id cdns_ti_of_match[] = {
  152. { .compatible = "ti,j721e-usb", },
  153. {},
  154. };
  155. U_BOOT_DRIVER(cdns_ti) = {
  156. .name = "cdns-ti",
  157. .id = UCLASS_NOP,
  158. .of_match = cdns_ti_of_match,
  159. .bind = cdns3_bind,
  160. .probe = cdns_ti_probe,
  161. .remove = cdns_ti_remove,
  162. .plat_auto = sizeof(struct cdns_ti),
  163. .flags = DM_FLAG_OS_PREPARE,
  164. };