dwc3-am62.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_platform.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/clk.h>
  16. #include <linux/regmap.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include "core.h"
  19. /* USB WRAPPER register offsets */
  20. #define USBSS_PID 0x0
  21. #define USBSS_OVERCURRENT_CTRL 0x4
  22. #define USBSS_PHY_CONFIG 0x8
  23. #define USBSS_PHY_TEST 0xc
  24. #define USBSS_CORE_STAT 0x14
  25. #define USBSS_HOST_VBUS_CTRL 0x18
  26. #define USBSS_MODE_CONTROL 0x1c
  27. #define USBSS_WAKEUP_CONFIG 0x30
  28. #define USBSS_WAKEUP_STAT 0x34
  29. #define USBSS_OVERRIDE_CONFIG 0x38
  30. #define USBSS_IRQ_MISC_STATUS_RAW 0x430
  31. #define USBSS_IRQ_MISC_STATUS 0x434
  32. #define USBSS_IRQ_MISC_ENABLE_SET 0x438
  33. #define USBSS_IRQ_MISC_ENABLE_CLR 0x43c
  34. #define USBSS_IRQ_MISC_EOI 0x440
  35. #define USBSS_INTR_TEST 0x490
  36. #define USBSS_VBUS_FILTER 0x614
  37. #define USBSS_VBUS_STAT 0x618
  38. #define USBSS_DEBUG_CFG 0x708
  39. #define USBSS_DEBUG_DATA 0x70c
  40. #define USBSS_HOST_HUB_CTRL 0x714
  41. /* PHY CONFIG register bits */
  42. #define USBSS_PHY_VBUS_SEL_MASK GENMASK(2, 1)
  43. #define USBSS_PHY_VBUS_SEL_SHIFT 1
  44. #define USBSS_PHY_LANE_REVERSE BIT(0)
  45. /* CORE STAT register bits */
  46. #define USBSS_CORE_OPERATIONAL_MODE_MASK GENMASK(13, 12)
  47. #define USBSS_CORE_OPERATIONAL_MODE_SHIFT 12
  48. /* MODE CONTROL register bits */
  49. #define USBSS_MODE_VALID BIT(0)
  50. /* WAKEUP CONFIG register bits */
  51. #define USBSS_WAKEUP_CFG_OVERCURRENT_EN BIT(3)
  52. #define USBSS_WAKEUP_CFG_LINESTATE_EN BIT(2)
  53. #define USBSS_WAKEUP_CFG_SESSVALID_EN BIT(1)
  54. #define USBSS_WAKEUP_CFG_VBUSVALID_EN BIT(0)
  55. #define USBSS_WAKEUP_CFG_ALL (USBSS_WAKEUP_CFG_VBUSVALID_EN | \
  56. USBSS_WAKEUP_CFG_SESSVALID_EN | \
  57. USBSS_WAKEUP_CFG_LINESTATE_EN | \
  58. USBSS_WAKEUP_CFG_OVERCURRENT_EN)
  59. #define USBSS_WAKEUP_CFG_NONE 0
  60. /* WAKEUP STAT register bits */
  61. #define USBSS_WAKEUP_STAT_OVERCURRENT BIT(4)
  62. #define USBSS_WAKEUP_STAT_LINESTATE BIT(3)
  63. #define USBSS_WAKEUP_STAT_SESSVALID BIT(2)
  64. #define USBSS_WAKEUP_STAT_VBUSVALID BIT(1)
  65. #define USBSS_WAKEUP_STAT_CLR BIT(0)
  66. /* IRQ_MISC_STATUS_RAW register bits */
  67. #define USBSS_IRQ_MISC_RAW_VBUSVALID BIT(22)
  68. #define USBSS_IRQ_MISC_RAW_SESSVALID BIT(20)
  69. /* IRQ_MISC_STATUS register bits */
  70. #define USBSS_IRQ_MISC_VBUSVALID BIT(22)
  71. #define USBSS_IRQ_MISC_SESSVALID BIT(20)
  72. /* IRQ_MISC_ENABLE_SET register bits */
  73. #define USBSS_IRQ_MISC_ENABLE_SET_VBUSVALID BIT(22)
  74. #define USBSS_IRQ_MISC_ENABLE_SET_SESSVALID BIT(20)
  75. /* IRQ_MISC_ENABLE_CLR register bits */
  76. #define USBSS_IRQ_MISC_ENABLE_CLR_VBUSVALID BIT(22)
  77. #define USBSS_IRQ_MISC_ENABLE_CLR_SESSVALID BIT(20)
  78. /* IRQ_MISC_EOI register bits */
  79. #define USBSS_IRQ_MISC_EOI_VECTOR BIT(0)
  80. /* VBUS_STAT register bits */
  81. #define USBSS_VBUS_STAT_SESSVALID BIT(2)
  82. #define USBSS_VBUS_STAT_VBUSVALID BIT(0)
  83. /* USB_PHY_CTRL register bits in CTRL_MMR */
  84. #define PHY_CORE_VOLTAGE_MASK BIT(31)
  85. #define PHY_PLL_REFCLK_MASK GENMASK(3, 0)
  86. /* USB PHY2 register offsets */
  87. #define USB_PHY_PLL_REG12 0x130
  88. #define USB_PHY_PLL_LDO_REF_EN BIT(5)
  89. #define USB_PHY_PLL_LDO_REF_EN_EN BIT(4)
  90. #define DWC3_AM62_AUTOSUSPEND_DELAY 100
  91. struct dwc3_am62 {
  92. struct device *dev;
  93. void __iomem *usbss;
  94. struct clk *usb2_refclk;
  95. int rate_code;
  96. struct regmap *syscon;
  97. unsigned int offset;
  98. unsigned int vbus_divider;
  99. u32 wakeup_stat;
  100. };
  101. static const int dwc3_ti_rate_table[] = { /* in KHZ */
  102. 9600,
  103. 10000,
  104. 12000,
  105. 19200,
  106. 20000,
  107. 24000,
  108. 25000,
  109. 26000,
  110. 38400,
  111. 40000,
  112. 58000,
  113. 50000,
  114. 52000,
  115. };
  116. static inline u32 dwc3_ti_readl(struct dwc3_am62 *am62, u32 offset)
  117. {
  118. return readl((am62->usbss) + offset);
  119. }
  120. static inline void dwc3_ti_writel(struct dwc3_am62 *am62, u32 offset, u32 value)
  121. {
  122. writel(value, (am62->usbss) + offset);
  123. }
  124. static int phy_syscon_pll_refclk(struct dwc3_am62 *am62)
  125. {
  126. struct device *dev = am62->dev;
  127. struct device_node *node = dev->of_node;
  128. struct of_phandle_args args;
  129. struct regmap *syscon;
  130. int ret;
  131. syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-phy-pll-refclk");
  132. if (IS_ERR(syscon)) {
  133. dev_err(dev, "unable to get ti,syscon-phy-pll-refclk regmap\n");
  134. return PTR_ERR(syscon);
  135. }
  136. am62->syscon = syscon;
  137. ret = of_parse_phandle_with_fixed_args(node, "ti,syscon-phy-pll-refclk", 1,
  138. 0, &args);
  139. if (ret)
  140. return ret;
  141. of_node_put(args.np);
  142. am62->offset = args.args[0];
  143. /* Core voltage. PHY_CORE_VOLTAGE bit Recommended to be 0 always */
  144. ret = regmap_update_bits(am62->syscon, am62->offset, PHY_CORE_VOLTAGE_MASK, 0);
  145. if (ret) {
  146. dev_err(dev, "failed to set phy core voltage\n");
  147. return ret;
  148. }
  149. ret = regmap_update_bits(am62->syscon, am62->offset, PHY_PLL_REFCLK_MASK, am62->rate_code);
  150. if (ret) {
  151. dev_err(dev, "failed to set phy pll reference clock rate\n");
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. static int dwc3_ti_probe(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. struct device_node *node = pdev->dev.of_node;
  160. struct dwc3_am62 *am62;
  161. unsigned long rate;
  162. void __iomem *phy;
  163. int i, ret;
  164. u32 reg;
  165. am62 = devm_kzalloc(dev, sizeof(*am62), GFP_KERNEL);
  166. if (!am62)
  167. return -ENOMEM;
  168. am62->dev = dev;
  169. platform_set_drvdata(pdev, am62);
  170. am62->usbss = devm_platform_ioremap_resource(pdev, 0);
  171. if (IS_ERR(am62->usbss)) {
  172. dev_err(dev, "can't map IOMEM resource\n");
  173. return PTR_ERR(am62->usbss);
  174. }
  175. am62->usb2_refclk = devm_clk_get(dev, "ref");
  176. if (IS_ERR(am62->usb2_refclk)) {
  177. dev_err(dev, "can't get usb2_refclk\n");
  178. return PTR_ERR(am62->usb2_refclk);
  179. }
  180. /* Calculate the rate code */
  181. rate = clk_get_rate(am62->usb2_refclk);
  182. rate /= 1000; // To KHz
  183. for (i = 0; i < ARRAY_SIZE(dwc3_ti_rate_table); i++) {
  184. if (dwc3_ti_rate_table[i] == rate)
  185. break;
  186. }
  187. if (i == ARRAY_SIZE(dwc3_ti_rate_table)) {
  188. dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
  189. return -EINVAL;
  190. }
  191. am62->rate_code = i;
  192. /* Read the syscon property and set the rate code */
  193. ret = phy_syscon_pll_refclk(am62);
  194. if (ret)
  195. return ret;
  196. /* Workaround Errata i2409 */
  197. phy = devm_platform_ioremap_resource(pdev, 1);
  198. if (IS_ERR(phy)) {
  199. dev_err(dev, "can't map PHY IOMEM resource. Won't apply i2409 fix.\n");
  200. phy = NULL;
  201. } else {
  202. reg = readl(phy + USB_PHY_PLL_REG12);
  203. reg |= USB_PHY_PLL_LDO_REF_EN | USB_PHY_PLL_LDO_REF_EN_EN;
  204. writel(reg, phy + USB_PHY_PLL_REG12);
  205. }
  206. /* VBUS divider select */
  207. am62->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
  208. reg = dwc3_ti_readl(am62, USBSS_PHY_CONFIG);
  209. if (am62->vbus_divider)
  210. reg |= 1 << USBSS_PHY_VBUS_SEL_SHIFT;
  211. dwc3_ti_writel(am62, USBSS_PHY_CONFIG, reg);
  212. pm_runtime_set_active(dev);
  213. pm_runtime_enable(dev);
  214. /*
  215. * Don't ignore its dependencies with its children
  216. */
  217. pm_suspend_ignore_children(dev, false);
  218. clk_prepare_enable(am62->usb2_refclk);
  219. pm_runtime_get_noresume(dev);
  220. ret = of_platform_populate(node, NULL, NULL, dev);
  221. if (ret) {
  222. dev_err(dev, "failed to create dwc3 core: %d\n", ret);
  223. goto err_pm_disable;
  224. }
  225. /* Set mode valid bit to indicate role is valid */
  226. reg = dwc3_ti_readl(am62, USBSS_MODE_CONTROL);
  227. reg |= USBSS_MODE_VALID;
  228. dwc3_ti_writel(am62, USBSS_MODE_CONTROL, reg);
  229. /* Device has capability to wakeup system from sleep */
  230. device_set_wakeup_capable(dev, true);
  231. ret = device_wakeup_enable(dev);
  232. if (ret)
  233. dev_err(dev, "couldn't enable device as a wakeup source: %d\n", ret);
  234. /* Setting up autosuspend */
  235. pm_runtime_set_autosuspend_delay(dev, DWC3_AM62_AUTOSUSPEND_DELAY);
  236. pm_runtime_use_autosuspend(dev);
  237. pm_runtime_mark_last_busy(dev);
  238. pm_runtime_put_autosuspend(dev);
  239. return 0;
  240. err_pm_disable:
  241. clk_disable_unprepare(am62->usb2_refclk);
  242. pm_runtime_disable(dev);
  243. pm_runtime_set_suspended(dev);
  244. return ret;
  245. }
  246. static void dwc3_ti_remove(struct platform_device *pdev)
  247. {
  248. struct device *dev = &pdev->dev;
  249. struct dwc3_am62 *am62 = platform_get_drvdata(pdev);
  250. u32 reg;
  251. pm_runtime_get_sync(dev);
  252. device_init_wakeup(dev, false);
  253. of_platform_depopulate(dev);
  254. /* Clear mode valid bit */
  255. reg = dwc3_ti_readl(am62, USBSS_MODE_CONTROL);
  256. reg &= ~USBSS_MODE_VALID;
  257. dwc3_ti_writel(am62, USBSS_MODE_CONTROL, reg);
  258. pm_runtime_put_sync(dev);
  259. pm_runtime_disable(dev);
  260. pm_runtime_dont_use_autosuspend(dev);
  261. pm_runtime_set_suspended(dev);
  262. }
  263. #ifdef CONFIG_PM
  264. static int dwc3_ti_suspend_common(struct device *dev)
  265. {
  266. struct dwc3_am62 *am62 = dev_get_drvdata(dev);
  267. u32 reg, current_prtcap_dir;
  268. if (device_may_wakeup(dev)) {
  269. reg = dwc3_ti_readl(am62, USBSS_CORE_STAT);
  270. current_prtcap_dir = (reg & USBSS_CORE_OPERATIONAL_MODE_MASK)
  271. >> USBSS_CORE_OPERATIONAL_MODE_SHIFT;
  272. /* Set wakeup config enable bits */
  273. reg = dwc3_ti_readl(am62, USBSS_WAKEUP_CONFIG);
  274. if (current_prtcap_dir == DWC3_GCTL_PRTCAP_HOST) {
  275. reg = USBSS_WAKEUP_CFG_LINESTATE_EN | USBSS_WAKEUP_CFG_OVERCURRENT_EN;
  276. } else {
  277. reg = USBSS_WAKEUP_CFG_VBUSVALID_EN | USBSS_WAKEUP_CFG_SESSVALID_EN;
  278. /*
  279. * Enable LINESTATE wake up only if connected to bus
  280. * and in U2/L3 state else it causes spurious wake-up.
  281. */
  282. }
  283. dwc3_ti_writel(am62, USBSS_WAKEUP_CONFIG, reg);
  284. /* clear wakeup status so we know what caused the wake up */
  285. dwc3_ti_writel(am62, USBSS_WAKEUP_STAT, USBSS_WAKEUP_STAT_CLR);
  286. }
  287. clk_disable_unprepare(am62->usb2_refclk);
  288. return 0;
  289. }
  290. static int dwc3_ti_resume_common(struct device *dev)
  291. {
  292. struct dwc3_am62 *am62 = dev_get_drvdata(dev);
  293. u32 reg;
  294. clk_prepare_enable(am62->usb2_refclk);
  295. if (device_may_wakeup(dev)) {
  296. /* Clear wakeup config enable bits */
  297. dwc3_ti_writel(am62, USBSS_WAKEUP_CONFIG, USBSS_WAKEUP_CFG_NONE);
  298. }
  299. reg = dwc3_ti_readl(am62, USBSS_WAKEUP_STAT);
  300. am62->wakeup_stat = reg;
  301. return 0;
  302. }
  303. static UNIVERSAL_DEV_PM_OPS(dwc3_ti_pm_ops, dwc3_ti_suspend_common,
  304. dwc3_ti_resume_common, NULL);
  305. #define DEV_PM_OPS (&dwc3_ti_pm_ops)
  306. #else
  307. #define DEV_PM_OPS NULL
  308. #endif /* CONFIG_PM */
  309. static const struct of_device_id dwc3_ti_of_match[] = {
  310. { .compatible = "ti,am62-usb"},
  311. {},
  312. };
  313. MODULE_DEVICE_TABLE(of, dwc3_ti_of_match);
  314. static struct platform_driver dwc3_ti_driver = {
  315. .probe = dwc3_ti_probe,
  316. .remove_new = dwc3_ti_remove,
  317. .driver = {
  318. .name = "dwc3-am62",
  319. .pm = DEV_PM_OPS,
  320. .of_match_table = dwc3_ti_of_match,
  321. },
  322. };
  323. module_platform_driver(dwc3_ti_driver);
  324. MODULE_ALIAS("platform:dwc3-am62");
  325. MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
  326. MODULE_LICENSE("GPL");
  327. MODULE_DESCRIPTION("DesignWare USB3 TI Glue Layer");