dwc3-am62.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define USBSS_DEBUG_CFG_OFF 0x0
  92. #define USBSS_DEBUG_CFG_DISABLED 0x7
  93. struct dwc3_am62 {
  94. struct device *dev;
  95. void __iomem *usbss;
  96. struct clk *usb2_refclk;
  97. int rate_code;
  98. struct regmap *syscon;
  99. unsigned int offset;
  100. unsigned int vbus_divider;
  101. u32 wakeup_stat;
  102. void __iomem *phy_regs;
  103. };
  104. static const int dwc3_ti_rate_table[] = { /* in KHZ */
  105. 9600,
  106. 10000,
  107. 12000,
  108. 19200,
  109. 20000,
  110. 24000,
  111. 25000,
  112. 26000,
  113. 38400,
  114. 40000,
  115. 58000,
  116. 50000,
  117. 52000,
  118. };
  119. static inline u32 dwc3_ti_readl(struct dwc3_am62 *am62, u32 offset)
  120. {
  121. return readl((am62->usbss) + offset);
  122. }
  123. static inline void dwc3_ti_writel(struct dwc3_am62 *am62, u32 offset, u32 value)
  124. {
  125. writel(value, (am62->usbss) + offset);
  126. }
  127. static int phy_syscon_pll_refclk(struct dwc3_am62 *am62)
  128. {
  129. struct device *dev = am62->dev;
  130. struct device_node *node = dev->of_node;
  131. struct regmap *syscon;
  132. int ret;
  133. syscon = syscon_regmap_lookup_by_phandle_args(node, "ti,syscon-phy-pll-refclk",
  134. 1, &am62->offset);
  135. if (IS_ERR(syscon)) {
  136. dev_err(dev, "unable to get ti,syscon-phy-pll-refclk regmap\n");
  137. return PTR_ERR(syscon);
  138. }
  139. am62->syscon = syscon;
  140. /* Core voltage. PHY_CORE_VOLTAGE bit Recommended to be 0 always */
  141. ret = regmap_update_bits(am62->syscon, am62->offset, PHY_CORE_VOLTAGE_MASK, 0);
  142. if (ret) {
  143. dev_err(dev, "failed to set phy core voltage\n");
  144. return ret;
  145. }
  146. ret = regmap_update_bits(am62->syscon, am62->offset, PHY_PLL_REFCLK_MASK, am62->rate_code);
  147. if (ret) {
  148. dev_err(dev, "failed to set phy pll reference clock rate\n");
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static int dwc3_ti_init(struct dwc3_am62 *am62)
  154. {
  155. int ret;
  156. u32 reg;
  157. /* Read the syscon property and set the rate code */
  158. ret = phy_syscon_pll_refclk(am62);
  159. if (ret)
  160. return ret;
  161. /* Workaround Errata i2409 */
  162. if (am62->phy_regs) {
  163. reg = readl(am62->phy_regs + USB_PHY_PLL_REG12);
  164. reg |= USB_PHY_PLL_LDO_REF_EN | USB_PHY_PLL_LDO_REF_EN_EN;
  165. writel(reg, am62->phy_regs + USB_PHY_PLL_REG12);
  166. }
  167. /* VBUS divider select */
  168. reg = dwc3_ti_readl(am62, USBSS_PHY_CONFIG);
  169. if (am62->vbus_divider)
  170. reg |= 1 << USBSS_PHY_VBUS_SEL_SHIFT;
  171. dwc3_ti_writel(am62, USBSS_PHY_CONFIG, reg);
  172. clk_prepare_enable(am62->usb2_refclk);
  173. /* Set mode valid bit to indicate role is valid */
  174. reg = dwc3_ti_readl(am62, USBSS_MODE_CONTROL);
  175. reg |= USBSS_MODE_VALID;
  176. dwc3_ti_writel(am62, USBSS_MODE_CONTROL, reg);
  177. return 0;
  178. }
  179. static int dwc3_ti_probe(struct platform_device *pdev)
  180. {
  181. struct device *dev = &pdev->dev;
  182. struct device_node *node = pdev->dev.of_node;
  183. struct dwc3_am62 *am62;
  184. unsigned long rate;
  185. int i, ret;
  186. am62 = devm_kzalloc(dev, sizeof(*am62), GFP_KERNEL);
  187. if (!am62)
  188. return -ENOMEM;
  189. am62->dev = dev;
  190. platform_set_drvdata(pdev, am62);
  191. am62->usbss = devm_platform_ioremap_resource(pdev, 0);
  192. if (IS_ERR(am62->usbss)) {
  193. dev_err(dev, "can't map IOMEM resource\n");
  194. return PTR_ERR(am62->usbss);
  195. }
  196. am62->usb2_refclk = devm_clk_get(dev, "ref");
  197. if (IS_ERR(am62->usb2_refclk)) {
  198. dev_err(dev, "can't get usb2_refclk\n");
  199. return PTR_ERR(am62->usb2_refclk);
  200. }
  201. /* Calculate the rate code */
  202. rate = clk_get_rate(am62->usb2_refclk);
  203. rate /= 1000; // To KHz
  204. for (i = 0; i < ARRAY_SIZE(dwc3_ti_rate_table); i++) {
  205. if (dwc3_ti_rate_table[i] == rate)
  206. break;
  207. }
  208. if (i == ARRAY_SIZE(dwc3_ti_rate_table)) {
  209. dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
  210. return -EINVAL;
  211. }
  212. am62->rate_code = i;
  213. am62->phy_regs = devm_platform_ioremap_resource(pdev, 1);
  214. if (IS_ERR(am62->phy_regs)) {
  215. dev_err(dev, "can't map PHY IOMEM resource. Won't apply i2409 fix.\n");
  216. am62->phy_regs = NULL;
  217. }
  218. am62->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
  219. ret = dwc3_ti_init(am62);
  220. if (ret)
  221. return ret;
  222. pm_runtime_set_active(dev);
  223. pm_runtime_enable(dev);
  224. /*
  225. * Don't ignore its dependencies with its children
  226. */
  227. pm_suspend_ignore_children(dev, false);
  228. pm_runtime_get_noresume(dev);
  229. ret = of_platform_populate(node, NULL, NULL, dev);
  230. if (ret) {
  231. dev_err(dev, "failed to create dwc3 core: %d\n", ret);
  232. goto err_pm_disable;
  233. }
  234. /* Device has capability to wakeup system from sleep */
  235. device_set_wakeup_capable(dev, true);
  236. ret = device_wakeup_enable(dev);
  237. if (ret)
  238. dev_err(dev, "couldn't enable device as a wakeup source: %d\n", ret);
  239. /* Setting up autosuspend */
  240. pm_runtime_set_autosuspend_delay(dev, DWC3_AM62_AUTOSUSPEND_DELAY);
  241. pm_runtime_use_autosuspend(dev);
  242. pm_runtime_put_autosuspend(dev);
  243. return 0;
  244. err_pm_disable:
  245. clk_disable_unprepare(am62->usb2_refclk);
  246. pm_runtime_disable(dev);
  247. pm_runtime_set_suspended(dev);
  248. return ret;
  249. }
  250. static void dwc3_ti_remove(struct platform_device *pdev)
  251. {
  252. struct device *dev = &pdev->dev;
  253. struct dwc3_am62 *am62 = platform_get_drvdata(pdev);
  254. u32 reg;
  255. pm_runtime_get_sync(dev);
  256. device_init_wakeup(dev, false);
  257. of_platform_depopulate(dev);
  258. /* Clear mode valid bit */
  259. reg = dwc3_ti_readl(am62, USBSS_MODE_CONTROL);
  260. reg &= ~USBSS_MODE_VALID;
  261. dwc3_ti_writel(am62, USBSS_MODE_CONTROL, reg);
  262. pm_runtime_put_sync(dev);
  263. pm_runtime_disable(dev);
  264. pm_runtime_dont_use_autosuspend(dev);
  265. pm_runtime_set_suspended(dev);
  266. }
  267. #ifdef CONFIG_PM
  268. static int dwc3_ti_suspend_common(struct device *dev)
  269. {
  270. struct dwc3_am62 *am62 = dev_get_drvdata(dev);
  271. u32 reg, current_prtcap_dir;
  272. if (device_may_wakeup(dev)) {
  273. reg = dwc3_ti_readl(am62, USBSS_CORE_STAT);
  274. current_prtcap_dir = (reg & USBSS_CORE_OPERATIONAL_MODE_MASK)
  275. >> USBSS_CORE_OPERATIONAL_MODE_SHIFT;
  276. /* Set wakeup config enable bits */
  277. reg = dwc3_ti_readl(am62, USBSS_WAKEUP_CONFIG);
  278. if (current_prtcap_dir == DWC3_GCTL_PRTCAP_HOST) {
  279. reg = USBSS_WAKEUP_CFG_LINESTATE_EN | USBSS_WAKEUP_CFG_OVERCURRENT_EN;
  280. } else {
  281. reg = USBSS_WAKEUP_CFG_VBUSVALID_EN | USBSS_WAKEUP_CFG_SESSVALID_EN;
  282. /*
  283. * Enable LINESTATE wake up only if connected to bus
  284. * and in U2/L3 state else it causes spurious wake-up.
  285. */
  286. }
  287. dwc3_ti_writel(am62, USBSS_WAKEUP_CONFIG, reg);
  288. /* clear wakeup status so we know what caused the wake up */
  289. dwc3_ti_writel(am62, USBSS_WAKEUP_STAT, USBSS_WAKEUP_STAT_CLR);
  290. }
  291. /* just to track if module resets on suspend */
  292. dwc3_ti_writel(am62, USBSS_DEBUG_CFG, USBSS_DEBUG_CFG_DISABLED);
  293. clk_disable_unprepare(am62->usb2_refclk);
  294. return 0;
  295. }
  296. static int dwc3_ti_resume_common(struct device *dev)
  297. {
  298. struct dwc3_am62 *am62 = dev_get_drvdata(dev);
  299. u32 reg;
  300. reg = dwc3_ti_readl(am62, USBSS_DEBUG_CFG);
  301. if (reg != USBSS_DEBUG_CFG_DISABLED) {
  302. /* lost power/context */
  303. dwc3_ti_init(am62);
  304. } else {
  305. dwc3_ti_writel(am62, USBSS_DEBUG_CFG, USBSS_DEBUG_CFG_OFF);
  306. clk_prepare_enable(am62->usb2_refclk);
  307. }
  308. if (device_may_wakeup(dev)) {
  309. /* Clear wakeup config enable bits */
  310. dwc3_ti_writel(am62, USBSS_WAKEUP_CONFIG, USBSS_WAKEUP_CFG_NONE);
  311. }
  312. reg = dwc3_ti_readl(am62, USBSS_WAKEUP_STAT);
  313. am62->wakeup_stat = reg;
  314. return 0;
  315. }
  316. static UNIVERSAL_DEV_PM_OPS(dwc3_ti_pm_ops, dwc3_ti_suspend_common,
  317. dwc3_ti_resume_common, NULL);
  318. #define DEV_PM_OPS (&dwc3_ti_pm_ops)
  319. #else
  320. #define DEV_PM_OPS NULL
  321. #endif /* CONFIG_PM */
  322. static const struct of_device_id dwc3_ti_of_match[] = {
  323. { .compatible = "ti,am62-usb"},
  324. {},
  325. };
  326. MODULE_DEVICE_TABLE(of, dwc3_ti_of_match);
  327. static struct platform_driver dwc3_ti_driver = {
  328. .probe = dwc3_ti_probe,
  329. .remove = dwc3_ti_remove,
  330. .driver = {
  331. .name = "dwc3-am62",
  332. .pm = DEV_PM_OPS,
  333. .of_match_table = dwc3_ti_of_match,
  334. },
  335. };
  336. module_platform_driver(dwc3_ti_driver);
  337. MODULE_ALIAS("platform:dwc3-am62");
  338. MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
  339. MODULE_LICENSE("GPL");
  340. MODULE_DESCRIPTION("DesignWare USB3 TI Glue Layer");