dwc3-qcom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. *
  4. * Inspired by dwc3-of-simple.c
  5. */
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/clk.h>
  9. #include <linux/irq.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/extcon.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/usb/of.h>
  18. #include <linux/reset.h>
  19. #include <linux/iopoll.h>
  20. #include "core.h"
  21. /* USB QSCRATCH Hardware registers */
  22. #define QSCRATCH_HS_PHY_CTRL 0x10
  23. #define UTMI_OTG_VBUS_VALID BIT(20)
  24. #define SW_SESSVLD_SEL BIT(28)
  25. #define QSCRATCH_SS_PHY_CTRL 0x30
  26. #define LANE0_PWR_PRESENT BIT(24)
  27. #define QSCRATCH_GENERAL_CFG 0x08
  28. #define PIPE_UTMI_CLK_SEL BIT(0)
  29. #define PIPE3_PHYSTATUS_SW BIT(3)
  30. #define PIPE_UTMI_CLK_DIS BIT(8)
  31. #define PWR_EVNT_IRQ_STAT_REG 0x58
  32. #define PWR_EVNT_LPM_IN_L2_MASK BIT(4)
  33. #define PWR_EVNT_LPM_OUT_L2_MASK BIT(5)
  34. struct dwc3_qcom {
  35. struct device *dev;
  36. void __iomem *qscratch_base;
  37. struct platform_device *dwc3;
  38. struct clk **clks;
  39. int num_clocks;
  40. struct reset_control *resets;
  41. int hs_phy_irq;
  42. int dp_hs_phy_irq;
  43. int dm_hs_phy_irq;
  44. int ss_phy_irq;
  45. struct extcon_dev *edev;
  46. struct extcon_dev *host_edev;
  47. struct notifier_block vbus_nb;
  48. struct notifier_block host_nb;
  49. enum usb_dr_mode mode;
  50. bool is_suspended;
  51. bool pm_suspended;
  52. };
  53. static inline void dwc3_qcom_setbits(void __iomem *base, u32 offset, u32 val)
  54. {
  55. u32 reg;
  56. reg = readl(base + offset);
  57. reg |= val;
  58. writel(reg, base + offset);
  59. /* ensure that above write is through */
  60. readl(base + offset);
  61. }
  62. static inline void dwc3_qcom_clrbits(void __iomem *base, u32 offset, u32 val)
  63. {
  64. u32 reg;
  65. reg = readl(base + offset);
  66. reg &= ~val;
  67. writel(reg, base + offset);
  68. /* ensure that above write is through */
  69. readl(base + offset);
  70. }
  71. static void dwc3_qcom_vbus_overrride_enable(struct dwc3_qcom *qcom, bool enable)
  72. {
  73. if (enable) {
  74. dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL,
  75. LANE0_PWR_PRESENT);
  76. dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL,
  77. UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
  78. } else {
  79. dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL,
  80. LANE0_PWR_PRESENT);
  81. dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL,
  82. UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
  83. }
  84. }
  85. static int dwc3_qcom_vbus_notifier(struct notifier_block *nb,
  86. unsigned long event, void *ptr)
  87. {
  88. struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, vbus_nb);
  89. /* enable vbus override for device mode */
  90. dwc3_qcom_vbus_overrride_enable(qcom, event);
  91. qcom->mode = event ? USB_DR_MODE_PERIPHERAL : USB_DR_MODE_HOST;
  92. return NOTIFY_DONE;
  93. }
  94. static int dwc3_qcom_host_notifier(struct notifier_block *nb,
  95. unsigned long event, void *ptr)
  96. {
  97. struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, host_nb);
  98. /* disable vbus override in host mode */
  99. dwc3_qcom_vbus_overrride_enable(qcom, !event);
  100. qcom->mode = event ? USB_DR_MODE_HOST : USB_DR_MODE_PERIPHERAL;
  101. return NOTIFY_DONE;
  102. }
  103. static int dwc3_qcom_register_extcon(struct dwc3_qcom *qcom)
  104. {
  105. struct device *dev = qcom->dev;
  106. struct extcon_dev *host_edev;
  107. int ret;
  108. if (!of_property_read_bool(dev->of_node, "extcon"))
  109. return 0;
  110. qcom->edev = extcon_get_edev_by_phandle(dev, 0);
  111. if (IS_ERR(qcom->edev))
  112. return PTR_ERR(qcom->edev);
  113. qcom->vbus_nb.notifier_call = dwc3_qcom_vbus_notifier;
  114. qcom->host_edev = extcon_get_edev_by_phandle(dev, 1);
  115. if (IS_ERR(qcom->host_edev))
  116. qcom->host_edev = NULL;
  117. ret = devm_extcon_register_notifier(dev, qcom->edev, EXTCON_USB,
  118. &qcom->vbus_nb);
  119. if (ret < 0) {
  120. dev_err(dev, "VBUS notifier register failed\n");
  121. return ret;
  122. }
  123. if (qcom->host_edev)
  124. host_edev = qcom->host_edev;
  125. else
  126. host_edev = qcom->edev;
  127. qcom->host_nb.notifier_call = dwc3_qcom_host_notifier;
  128. ret = devm_extcon_register_notifier(dev, host_edev, EXTCON_USB_HOST,
  129. &qcom->host_nb);
  130. if (ret < 0) {
  131. dev_err(dev, "Host notifier register failed\n");
  132. return ret;
  133. }
  134. /* Update initial VBUS override based on extcon state */
  135. if (extcon_get_state(qcom->edev, EXTCON_USB) ||
  136. !extcon_get_state(host_edev, EXTCON_USB_HOST))
  137. dwc3_qcom_vbus_notifier(&qcom->vbus_nb, true, qcom->edev);
  138. else
  139. dwc3_qcom_vbus_notifier(&qcom->vbus_nb, false, qcom->edev);
  140. return 0;
  141. }
  142. static void dwc3_qcom_disable_interrupts(struct dwc3_qcom *qcom)
  143. {
  144. if (qcom->hs_phy_irq) {
  145. disable_irq_wake(qcom->hs_phy_irq);
  146. disable_irq_nosync(qcom->hs_phy_irq);
  147. }
  148. if (qcom->dp_hs_phy_irq) {
  149. disable_irq_wake(qcom->dp_hs_phy_irq);
  150. disable_irq_nosync(qcom->dp_hs_phy_irq);
  151. }
  152. if (qcom->dm_hs_phy_irq) {
  153. disable_irq_wake(qcom->dm_hs_phy_irq);
  154. disable_irq_nosync(qcom->dm_hs_phy_irq);
  155. }
  156. if (qcom->ss_phy_irq) {
  157. disable_irq_wake(qcom->ss_phy_irq);
  158. disable_irq_nosync(qcom->ss_phy_irq);
  159. }
  160. }
  161. static void dwc3_qcom_enable_interrupts(struct dwc3_qcom *qcom)
  162. {
  163. if (qcom->hs_phy_irq) {
  164. enable_irq(qcom->hs_phy_irq);
  165. enable_irq_wake(qcom->hs_phy_irq);
  166. }
  167. if (qcom->dp_hs_phy_irq) {
  168. enable_irq(qcom->dp_hs_phy_irq);
  169. enable_irq_wake(qcom->dp_hs_phy_irq);
  170. }
  171. if (qcom->dm_hs_phy_irq) {
  172. enable_irq(qcom->dm_hs_phy_irq);
  173. enable_irq_wake(qcom->dm_hs_phy_irq);
  174. }
  175. if (qcom->ss_phy_irq) {
  176. enable_irq(qcom->ss_phy_irq);
  177. enable_irq_wake(qcom->ss_phy_irq);
  178. }
  179. }
  180. static int dwc3_qcom_suspend(struct dwc3_qcom *qcom)
  181. {
  182. u32 val;
  183. int i;
  184. if (qcom->is_suspended)
  185. return 0;
  186. val = readl(qcom->qscratch_base + PWR_EVNT_IRQ_STAT_REG);
  187. if (!(val & PWR_EVNT_LPM_IN_L2_MASK))
  188. dev_err(qcom->dev, "HS-PHY not in L2\n");
  189. for (i = qcom->num_clocks - 1; i >= 0; i--)
  190. clk_disable_unprepare(qcom->clks[i]);
  191. qcom->is_suspended = true;
  192. dwc3_qcom_enable_interrupts(qcom);
  193. return 0;
  194. }
  195. static int dwc3_qcom_resume(struct dwc3_qcom *qcom)
  196. {
  197. int ret;
  198. int i;
  199. if (!qcom->is_suspended)
  200. return 0;
  201. dwc3_qcom_disable_interrupts(qcom);
  202. for (i = 0; i < qcom->num_clocks; i++) {
  203. ret = clk_prepare_enable(qcom->clks[i]);
  204. if (ret < 0) {
  205. while (--i >= 0)
  206. clk_disable_unprepare(qcom->clks[i]);
  207. return ret;
  208. }
  209. }
  210. /* Clear existing events from PHY related to L2 in/out */
  211. dwc3_qcom_setbits(qcom->qscratch_base, PWR_EVNT_IRQ_STAT_REG,
  212. PWR_EVNT_LPM_IN_L2_MASK | PWR_EVNT_LPM_OUT_L2_MASK);
  213. qcom->is_suspended = false;
  214. return 0;
  215. }
  216. static irqreturn_t qcom_dwc3_resume_irq(int irq, void *data)
  217. {
  218. struct dwc3_qcom *qcom = data;
  219. struct dwc3 *dwc = platform_get_drvdata(qcom->dwc3);
  220. /* If pm_suspended then let pm_resume take care of resuming h/w */
  221. if (qcom->pm_suspended)
  222. return IRQ_HANDLED;
  223. if (dwc->xhci)
  224. pm_runtime_resume(&dwc->xhci->dev);
  225. return IRQ_HANDLED;
  226. }
  227. static void dwc3_qcom_select_utmi_clk(struct dwc3_qcom *qcom)
  228. {
  229. /* Configure dwc3 to use UTMI clock as PIPE clock not present */
  230. dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
  231. PIPE_UTMI_CLK_DIS);
  232. usleep_range(100, 1000);
  233. dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
  234. PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW);
  235. usleep_range(100, 1000);
  236. dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
  237. PIPE_UTMI_CLK_DIS);
  238. }
  239. static int dwc3_qcom_setup_irq(struct platform_device *pdev)
  240. {
  241. struct dwc3_qcom *qcom = platform_get_drvdata(pdev);
  242. int irq, ret;
  243. irq = platform_get_irq_byname(pdev, "hs_phy_irq");
  244. if (irq > 0) {
  245. /* Keep wakeup interrupts disabled until suspend */
  246. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  247. ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
  248. qcom_dwc3_resume_irq,
  249. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  250. "qcom_dwc3 HS", qcom);
  251. if (ret) {
  252. dev_err(qcom->dev, "hs_phy_irq failed: %d\n", ret);
  253. return ret;
  254. }
  255. qcom->hs_phy_irq = irq;
  256. }
  257. irq = platform_get_irq_byname(pdev, "dp_hs_phy_irq");
  258. if (irq > 0) {
  259. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  260. ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
  261. qcom_dwc3_resume_irq,
  262. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  263. "qcom_dwc3 DP_HS", qcom);
  264. if (ret) {
  265. dev_err(qcom->dev, "dp_hs_phy_irq failed: %d\n", ret);
  266. return ret;
  267. }
  268. qcom->dp_hs_phy_irq = irq;
  269. }
  270. irq = platform_get_irq_byname(pdev, "dm_hs_phy_irq");
  271. if (irq > 0) {
  272. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  273. ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
  274. qcom_dwc3_resume_irq,
  275. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  276. "qcom_dwc3 DM_HS", qcom);
  277. if (ret) {
  278. dev_err(qcom->dev, "dm_hs_phy_irq failed: %d\n", ret);
  279. return ret;
  280. }
  281. qcom->dm_hs_phy_irq = irq;
  282. }
  283. irq = platform_get_irq_byname(pdev, "ss_phy_irq");
  284. if (irq > 0) {
  285. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  286. ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
  287. qcom_dwc3_resume_irq,
  288. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  289. "qcom_dwc3 SS", qcom);
  290. if (ret) {
  291. dev_err(qcom->dev, "ss_phy_irq failed: %d\n", ret);
  292. return ret;
  293. }
  294. qcom->ss_phy_irq = irq;
  295. }
  296. return 0;
  297. }
  298. static int dwc3_qcom_clk_init(struct dwc3_qcom *qcom, int count)
  299. {
  300. struct device *dev = qcom->dev;
  301. struct device_node *np = dev->of_node;
  302. int i;
  303. qcom->num_clocks = count;
  304. if (!count)
  305. return 0;
  306. qcom->clks = devm_kcalloc(dev, qcom->num_clocks,
  307. sizeof(struct clk *), GFP_KERNEL);
  308. if (!qcom->clks)
  309. return -ENOMEM;
  310. for (i = 0; i < qcom->num_clocks; i++) {
  311. struct clk *clk;
  312. int ret;
  313. clk = of_clk_get(np, i);
  314. if (IS_ERR(clk)) {
  315. while (--i >= 0)
  316. clk_put(qcom->clks[i]);
  317. return PTR_ERR(clk);
  318. }
  319. ret = clk_prepare_enable(clk);
  320. if (ret < 0) {
  321. while (--i >= 0) {
  322. clk_disable_unprepare(qcom->clks[i]);
  323. clk_put(qcom->clks[i]);
  324. }
  325. clk_put(clk);
  326. return ret;
  327. }
  328. qcom->clks[i] = clk;
  329. }
  330. return 0;
  331. }
  332. static int dwc3_qcom_probe(struct platform_device *pdev)
  333. {
  334. struct device_node *np = pdev->dev.of_node, *dwc3_np;
  335. struct device *dev = &pdev->dev;
  336. struct dwc3_qcom *qcom;
  337. struct resource *res;
  338. int ret, i;
  339. bool ignore_pipe_clk;
  340. qcom = devm_kzalloc(&pdev->dev, sizeof(*qcom), GFP_KERNEL);
  341. if (!qcom)
  342. return -ENOMEM;
  343. platform_set_drvdata(pdev, qcom);
  344. qcom->dev = &pdev->dev;
  345. qcom->resets = devm_reset_control_array_get_optional_exclusive(dev);
  346. if (IS_ERR(qcom->resets)) {
  347. ret = PTR_ERR(qcom->resets);
  348. dev_err(&pdev->dev, "failed to get resets, err=%d\n", ret);
  349. return ret;
  350. }
  351. ret = reset_control_assert(qcom->resets);
  352. if (ret) {
  353. dev_err(&pdev->dev, "failed to assert resets, err=%d\n", ret);
  354. return ret;
  355. }
  356. usleep_range(10, 1000);
  357. ret = reset_control_deassert(qcom->resets);
  358. if (ret) {
  359. dev_err(&pdev->dev, "failed to deassert resets, err=%d\n", ret);
  360. goto reset_assert;
  361. }
  362. ret = dwc3_qcom_clk_init(qcom, of_count_phandle_with_args(np,
  363. "clocks", "#clock-cells"));
  364. if (ret) {
  365. dev_err(dev, "failed to get clocks\n");
  366. goto reset_assert;
  367. }
  368. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  369. qcom->qscratch_base = devm_ioremap_resource(dev, res);
  370. if (IS_ERR(qcom->qscratch_base)) {
  371. dev_err(dev, "failed to map qscratch, err=%d\n", ret);
  372. ret = PTR_ERR(qcom->qscratch_base);
  373. goto clk_disable;
  374. }
  375. ret = dwc3_qcom_setup_irq(pdev);
  376. if (ret)
  377. goto clk_disable;
  378. dwc3_np = of_get_child_by_name(np, "dwc3");
  379. if (!dwc3_np) {
  380. dev_err(dev, "failed to find dwc3 core child\n");
  381. ret = -ENODEV;
  382. goto clk_disable;
  383. }
  384. /*
  385. * Disable pipe_clk requirement if specified. Used when dwc3
  386. * operates without SSPHY and only HS/FS/LS modes are supported.
  387. */
  388. ignore_pipe_clk = device_property_read_bool(dev,
  389. "qcom,select-utmi-as-pipe-clk");
  390. if (ignore_pipe_clk)
  391. dwc3_qcom_select_utmi_clk(qcom);
  392. ret = of_platform_populate(np, NULL, NULL, dev);
  393. if (ret) {
  394. dev_err(dev, "failed to register dwc3 core - %d\n", ret);
  395. goto clk_disable;
  396. }
  397. qcom->dwc3 = of_find_device_by_node(dwc3_np);
  398. if (!qcom->dwc3) {
  399. dev_err(&pdev->dev, "failed to get dwc3 platform device\n");
  400. ret = -ENODEV;
  401. goto depopulate;
  402. }
  403. qcom->mode = usb_get_dr_mode(&qcom->dwc3->dev);
  404. /* enable vbus override for device mode */
  405. if (qcom->mode == USB_DR_MODE_PERIPHERAL)
  406. dwc3_qcom_vbus_overrride_enable(qcom, true);
  407. /* register extcon to override sw_vbus on Vbus change later */
  408. ret = dwc3_qcom_register_extcon(qcom);
  409. if (ret)
  410. goto depopulate;
  411. device_init_wakeup(&pdev->dev, 1);
  412. qcom->is_suspended = false;
  413. pm_runtime_set_active(dev);
  414. pm_runtime_enable(dev);
  415. pm_runtime_forbid(dev);
  416. return 0;
  417. depopulate:
  418. of_platform_depopulate(&pdev->dev);
  419. clk_disable:
  420. for (i = qcom->num_clocks - 1; i >= 0; i--) {
  421. clk_disable_unprepare(qcom->clks[i]);
  422. clk_put(qcom->clks[i]);
  423. }
  424. reset_assert:
  425. reset_control_assert(qcom->resets);
  426. return ret;
  427. }
  428. static int dwc3_qcom_remove(struct platform_device *pdev)
  429. {
  430. struct dwc3_qcom *qcom = platform_get_drvdata(pdev);
  431. struct device *dev = &pdev->dev;
  432. int i;
  433. of_platform_depopulate(dev);
  434. for (i = qcom->num_clocks - 1; i >= 0; i--) {
  435. clk_disable_unprepare(qcom->clks[i]);
  436. clk_put(qcom->clks[i]);
  437. }
  438. qcom->num_clocks = 0;
  439. reset_control_assert(qcom->resets);
  440. pm_runtime_allow(dev);
  441. pm_runtime_disable(dev);
  442. return 0;
  443. }
  444. static int __maybe_unused dwc3_qcom_pm_suspend(struct device *dev)
  445. {
  446. struct dwc3_qcom *qcom = dev_get_drvdata(dev);
  447. int ret = 0;
  448. ret = dwc3_qcom_suspend(qcom);
  449. if (!ret)
  450. qcom->pm_suspended = true;
  451. return ret;
  452. }
  453. static int __maybe_unused dwc3_qcom_pm_resume(struct device *dev)
  454. {
  455. struct dwc3_qcom *qcom = dev_get_drvdata(dev);
  456. int ret;
  457. ret = dwc3_qcom_resume(qcom);
  458. if (!ret)
  459. qcom->pm_suspended = false;
  460. return ret;
  461. }
  462. static int __maybe_unused dwc3_qcom_runtime_suspend(struct device *dev)
  463. {
  464. struct dwc3_qcom *qcom = dev_get_drvdata(dev);
  465. return dwc3_qcom_suspend(qcom);
  466. }
  467. static int __maybe_unused dwc3_qcom_runtime_resume(struct device *dev)
  468. {
  469. struct dwc3_qcom *qcom = dev_get_drvdata(dev);
  470. return dwc3_qcom_resume(qcom);
  471. }
  472. static const struct dev_pm_ops dwc3_qcom_dev_pm_ops = {
  473. SET_SYSTEM_SLEEP_PM_OPS(dwc3_qcom_pm_suspend, dwc3_qcom_pm_resume)
  474. SET_RUNTIME_PM_OPS(dwc3_qcom_runtime_suspend, dwc3_qcom_runtime_resume,
  475. NULL)
  476. };
  477. static const struct of_device_id dwc3_qcom_of_match[] = {
  478. { .compatible = "qcom,dwc3" },
  479. { .compatible = "qcom,msm8996-dwc3" },
  480. { .compatible = "qcom,sdm845-dwc3" },
  481. { }
  482. };
  483. MODULE_DEVICE_TABLE(of, dwc3_qcom_of_match);
  484. static struct platform_driver dwc3_qcom_driver = {
  485. .probe = dwc3_qcom_probe,
  486. .remove = dwc3_qcom_remove,
  487. .driver = {
  488. .name = "dwc3-qcom",
  489. .pm = &dwc3_qcom_dev_pm_ops,
  490. .of_match_table = dwc3_qcom_of_match,
  491. },
  492. };
  493. module_platform_driver(dwc3_qcom_driver);
  494. MODULE_LICENSE("GPL v2");
  495. MODULE_DESCRIPTION("DesignWare DWC3 QCOM Glue Driver");