dwc3-am62.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dwc3-am62.c - TI specific Glue layer for AM62 DWC3 USB Controller
  4. *
  5. * Copyright (C) 2022 Texas Instruments Incorporated - https://www.ti.com
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/clk.h>
  16. #include <linux/regmap.h>
  17. #include <linux/pinctrl/consumer.h>
  18. /* USB WRAPPER register offsets */
  19. #define USBSS_PID 0x0
  20. #define USBSS_OVERCURRENT_CTRL 0x4
  21. #define USBSS_PHY_CONFIG 0x8
  22. #define USBSS_PHY_TEST 0xc
  23. #define USBSS_CORE_STAT 0x14
  24. #define USBSS_HOST_VBUS_CTRL 0x18
  25. #define USBSS_MODE_CONTROL 0x1c
  26. #define USBSS_WAKEUP_CONFIG 0x30
  27. #define USBSS_WAKEUP_STAT 0x34
  28. #define USBSS_OVERRIDE_CONFIG 0x38
  29. #define USBSS_IRQ_MISC_STATUS_RAW 0x430
  30. #define USBSS_IRQ_MISC_STATUS 0x434
  31. #define USBSS_IRQ_MISC_ENABLE_SET 0x438
  32. #define USBSS_IRQ_MISC_ENABLE_CLR 0x43c
  33. #define USBSS_IRQ_MISC_EOI 0x440
  34. #define USBSS_INTR_TEST 0x490
  35. #define USBSS_VBUS_FILTER 0x614
  36. #define USBSS_VBUS_STAT 0x618
  37. #define USBSS_DEBUG_CFG 0x708
  38. #define USBSS_DEBUG_DATA 0x70c
  39. #define USBSS_HOST_HUB_CTRL 0x714
  40. /* PHY CONFIG register bits */
  41. #define USBSS_PHY_VBUS_SEL_MASK GENMASK(2, 1)
  42. #define USBSS_PHY_VBUS_SEL_SHIFT 1
  43. #define USBSS_PHY_LANE_REVERSE BIT(0)
  44. /* MODE CONTROL register bits */
  45. #define USBSS_MODE_VALID BIT(0)
  46. /* WAKEUP CONFIG register bits */
  47. #define USBSS_WAKEUP_CFG_OVERCURRENT_EN BIT(3)
  48. #define USBSS_WAKEUP_CFG_LINESTATE_EN BIT(2)
  49. #define USBSS_WAKEUP_CFG_SESSVALID_EN BIT(1)
  50. #define USBSS_WAKEUP_CFG_VBUSVALID_EN BIT(0)
  51. /* WAKEUP STAT register bits */
  52. #define USBSS_WAKEUP_STAT_OVERCURRENT BIT(4)
  53. #define USBSS_WAKEUP_STAT_LINESTATE BIT(3)
  54. #define USBSS_WAKEUP_STAT_SESSVALID BIT(2)
  55. #define USBSS_WAKEUP_STAT_VBUSVALID BIT(1)
  56. #define USBSS_WAKEUP_STAT_CLR BIT(0)
  57. /* IRQ_MISC_STATUS_RAW register bits */
  58. #define USBSS_IRQ_MISC_RAW_VBUSVALID BIT(22)
  59. #define USBSS_IRQ_MISC_RAW_SESSVALID BIT(20)
  60. /* IRQ_MISC_STATUS register bits */
  61. #define USBSS_IRQ_MISC_VBUSVALID BIT(22)
  62. #define USBSS_IRQ_MISC_SESSVALID BIT(20)
  63. /* IRQ_MISC_ENABLE_SET register bits */
  64. #define USBSS_IRQ_MISC_ENABLE_SET_VBUSVALID BIT(22)
  65. #define USBSS_IRQ_MISC_ENABLE_SET_SESSVALID BIT(20)
  66. /* IRQ_MISC_ENABLE_CLR register bits */
  67. #define USBSS_IRQ_MISC_ENABLE_CLR_VBUSVALID BIT(22)
  68. #define USBSS_IRQ_MISC_ENABLE_CLR_SESSVALID BIT(20)
  69. /* IRQ_MISC_EOI register bits */
  70. #define USBSS_IRQ_MISC_EOI_VECTOR BIT(0)
  71. /* VBUS_STAT register bits */
  72. #define USBSS_VBUS_STAT_SESSVALID BIT(2)
  73. #define USBSS_VBUS_STAT_VBUSVALID BIT(0)
  74. /* Mask for PHY PLL REFCLK */
  75. #define PHY_PLL_REFCLK_MASK GENMASK(3, 0)
  76. #define DWC3_AM62_AUTOSUSPEND_DELAY 100
  77. struct dwc3_data {
  78. struct device *dev;
  79. void __iomem *usbss;
  80. struct clk *usb2_refclk;
  81. int rate_code;
  82. struct regmap *syscon;
  83. unsigned int offset;
  84. unsigned int vbus_divider;
  85. };
  86. static const int dwc3_ti_rate_table[] = { /* in KHZ */
  87. 9600,
  88. 10000,
  89. 12000,
  90. 19200,
  91. 20000,
  92. 24000,
  93. 25000,
  94. 26000,
  95. 38400,
  96. 40000,
  97. 58000,
  98. 50000,
  99. 52000,
  100. };
  101. static inline u32 dwc3_ti_readl(struct dwc3_data *data, u32 offset)
  102. {
  103. return readl((data->usbss) + offset);
  104. }
  105. static inline void dwc3_ti_writel(struct dwc3_data *data, u32 offset, u32 value)
  106. {
  107. writel(value, (data->usbss) + offset);
  108. }
  109. static int phy_syscon_pll_refclk(struct dwc3_data *data)
  110. {
  111. struct device *dev = data->dev;
  112. struct device_node *node = dev->of_node;
  113. struct of_phandle_args args;
  114. struct regmap *syscon;
  115. int ret;
  116. syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-phy-pll-refclk");
  117. if (IS_ERR(syscon)) {
  118. dev_err(dev, "unable to get ti,syscon-phy-pll-refclk regmap\n");
  119. return PTR_ERR(syscon);
  120. }
  121. data->syscon = syscon;
  122. ret = of_parse_phandle_with_fixed_args(node, "ti,syscon-phy-pll-refclk", 1,
  123. 0, &args);
  124. if (ret)
  125. return ret;
  126. data->offset = args.args[0];
  127. ret = regmap_update_bits(data->syscon, data->offset, PHY_PLL_REFCLK_MASK, data->rate_code);
  128. if (ret) {
  129. dev_err(dev, "failed to set phy pll reference clock rate\n");
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int dwc3_ti_probe(struct platform_device *pdev)
  135. {
  136. struct device *dev = &pdev->dev;
  137. struct device_node *node = pdev->dev.of_node;
  138. struct dwc3_data *data;
  139. int i, ret;
  140. unsigned long rate;
  141. u32 reg;
  142. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  143. if (!data)
  144. return -ENOMEM;
  145. data->dev = dev;
  146. platform_set_drvdata(pdev, data);
  147. data->usbss = devm_platform_ioremap_resource(pdev, 0);
  148. if (IS_ERR(data->usbss)) {
  149. dev_err(dev, "can't map IOMEM resource\n");
  150. return PTR_ERR(data->usbss);
  151. }
  152. data->usb2_refclk = devm_clk_get(dev, "ref");
  153. if (IS_ERR(data->usb2_refclk)) {
  154. dev_err(dev, "can't get usb2_refclk\n");
  155. return PTR_ERR(data->usb2_refclk);
  156. }
  157. /* Calculate the rate code */
  158. rate = clk_get_rate(data->usb2_refclk);
  159. rate /= 1000; // To KHz
  160. for (i = 0; i < ARRAY_SIZE(dwc3_ti_rate_table); i++) {
  161. if (dwc3_ti_rate_table[i] == rate)
  162. break;
  163. }
  164. if (i == ARRAY_SIZE(dwc3_ti_rate_table)) {
  165. dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
  166. return -EINVAL;
  167. }
  168. data->rate_code = i;
  169. /* Read the syscon property and set the rate code */
  170. ret = phy_syscon_pll_refclk(data);
  171. if (ret)
  172. return ret;
  173. /* VBUS divider select */
  174. data->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
  175. reg = dwc3_ti_readl(data, USBSS_PHY_CONFIG);
  176. if (data->vbus_divider)
  177. reg |= 1 << USBSS_PHY_VBUS_SEL_SHIFT;
  178. dwc3_ti_writel(data, USBSS_PHY_CONFIG, reg);
  179. pm_runtime_set_active(dev);
  180. pm_runtime_enable(dev);
  181. /*
  182. * Don't ignore its dependencies with its children
  183. */
  184. pm_suspend_ignore_children(dev, false);
  185. clk_prepare_enable(data->usb2_refclk);
  186. pm_runtime_get_noresume(dev);
  187. ret = of_platform_populate(node, NULL, NULL, dev);
  188. if (ret) {
  189. dev_err(dev, "failed to create dwc3 core: %d\n", ret);
  190. goto err_pm_disable;
  191. }
  192. /* Set mode valid bit to indicate role is valid */
  193. reg = dwc3_ti_readl(data, USBSS_MODE_CONTROL);
  194. reg |= USBSS_MODE_VALID;
  195. dwc3_ti_writel(data, USBSS_MODE_CONTROL, reg);
  196. /* Setting up autosuspend */
  197. pm_runtime_set_autosuspend_delay(dev, DWC3_AM62_AUTOSUSPEND_DELAY);
  198. pm_runtime_use_autosuspend(dev);
  199. pm_runtime_mark_last_busy(dev);
  200. pm_runtime_put_autosuspend(dev);
  201. return 0;
  202. err_pm_disable:
  203. clk_disable_unprepare(data->usb2_refclk);
  204. pm_runtime_disable(dev);
  205. pm_runtime_set_suspended(dev);
  206. return ret;
  207. }
  208. static int dwc3_ti_remove_core(struct device *dev, void *c)
  209. {
  210. struct platform_device *pdev = to_platform_device(dev);
  211. platform_device_unregister(pdev);
  212. return 0;
  213. }
  214. static int dwc3_ti_remove(struct platform_device *pdev)
  215. {
  216. struct device *dev = &pdev->dev;
  217. struct dwc3_data *data = platform_get_drvdata(pdev);
  218. u32 reg;
  219. device_for_each_child(dev, NULL, dwc3_ti_remove_core);
  220. /* Clear mode valid bit */
  221. reg = dwc3_ti_readl(data, USBSS_MODE_CONTROL);
  222. reg &= ~USBSS_MODE_VALID;
  223. dwc3_ti_writel(data, USBSS_MODE_CONTROL, reg);
  224. pm_runtime_put_sync(dev);
  225. clk_disable_unprepare(data->usb2_refclk);
  226. pm_runtime_disable(dev);
  227. pm_runtime_set_suspended(dev);
  228. platform_set_drvdata(pdev, NULL);
  229. return 0;
  230. }
  231. #ifdef CONFIG_PM
  232. static int dwc3_ti_suspend_common(struct device *dev)
  233. {
  234. struct dwc3_data *data = dev_get_drvdata(dev);
  235. clk_disable_unprepare(data->usb2_refclk);
  236. return 0;
  237. }
  238. static int dwc3_ti_resume_common(struct device *dev)
  239. {
  240. struct dwc3_data *data = dev_get_drvdata(dev);
  241. clk_prepare_enable(data->usb2_refclk);
  242. return 0;
  243. }
  244. static UNIVERSAL_DEV_PM_OPS(dwc3_ti_pm_ops, dwc3_ti_suspend_common,
  245. dwc3_ti_resume_common, NULL);
  246. #define DEV_PM_OPS (&dwc3_ti_pm_ops)
  247. #else
  248. #define DEV_PM_OPS NULL
  249. #endif /* CONFIG_PM */
  250. static const struct of_device_id dwc3_ti_of_match[] = {
  251. { .compatible = "ti,am62-usb"},
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(of, dwc3_ti_of_match);
  255. static struct platform_driver dwc3_ti_driver = {
  256. .probe = dwc3_ti_probe,
  257. .remove = dwc3_ti_remove,
  258. .driver = {
  259. .name = "dwc3-am62",
  260. .pm = DEV_PM_OPS,
  261. .of_match_table = dwc3_ti_of_match,
  262. },
  263. };
  264. module_platform_driver(dwc3_ti_driver);
  265. MODULE_ALIAS("platform:dwc3-am62");
  266. MODULE_AUTHOR("Aswath Govindraju <[email protected]>");
  267. MODULE_LICENSE("GPL");
  268. MODULE_DESCRIPTION("DesignWare USB3 TI Glue Layer");