dwc3-pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * dwc3-pci.c - PCI Specific glue layer
  4. *
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/pci.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/gpio/machine.h>
  19. #include <linux/acpi.h>
  20. #include <linux/delay.h>
  21. #define PCI_DEVICE_ID_INTEL_BYT 0x0f37
  22. #define PCI_DEVICE_ID_INTEL_MRFLD 0x119e
  23. #define PCI_DEVICE_ID_INTEL_BSW 0x22b7
  24. #define PCI_DEVICE_ID_INTEL_SPTLP 0x9d30
  25. #define PCI_DEVICE_ID_INTEL_SPTH 0xa130
  26. #define PCI_DEVICE_ID_INTEL_BXT 0x0aaa
  27. #define PCI_DEVICE_ID_INTEL_BXT_M 0x1aaa
  28. #define PCI_DEVICE_ID_INTEL_APL 0x5aaa
  29. #define PCI_DEVICE_ID_INTEL_KBP 0xa2b0
  30. #define PCI_DEVICE_ID_INTEL_CMLLP 0x02ee
  31. #define PCI_DEVICE_ID_INTEL_CMLH 0x06ee
  32. #define PCI_DEVICE_ID_INTEL_GLK 0x31aa
  33. #define PCI_DEVICE_ID_INTEL_CNPLP 0x9dee
  34. #define PCI_DEVICE_ID_INTEL_CNPH 0xa36e
  35. #define PCI_DEVICE_ID_INTEL_CNPV 0xa3b0
  36. #define PCI_DEVICE_ID_INTEL_ICLLP 0x34ee
  37. #define PCI_INTEL_BXT_DSM_GUID "732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
  38. #define PCI_INTEL_BXT_FUNC_PMU_PWR 4
  39. #define PCI_INTEL_BXT_STATE_D0 0
  40. #define PCI_INTEL_BXT_STATE_D3 3
  41. #define GP_RWBAR 1
  42. #define GP_RWREG1 0xa0
  43. #define GP_RWREG1_ULPI_REFCLK_DISABLE (1 << 17)
  44. /**
  45. * struct dwc3_pci - Driver private structure
  46. * @dwc3: child dwc3 platform_device
  47. * @pci: our link to PCI bus
  48. * @guid: _DSM GUID
  49. * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
  50. * @wakeup_work: work for asynchronous resume
  51. */
  52. struct dwc3_pci {
  53. struct platform_device *dwc3;
  54. struct pci_dev *pci;
  55. guid_t guid;
  56. unsigned int has_dsm_for_pm:1;
  57. struct work_struct wakeup_work;
  58. };
  59. static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
  60. static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
  61. static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
  62. { "reset-gpios", &reset_gpios, 1 },
  63. { "cs-gpios", &cs_gpios, 1 },
  64. { },
  65. };
  66. static struct gpiod_lookup_table platform_bytcr_gpios = {
  67. .dev_id = "0000:00:16.0",
  68. .table = {
  69. GPIO_LOOKUP("INT33FC:00", 54, "reset", GPIO_ACTIVE_HIGH),
  70. GPIO_LOOKUP("INT33FC:02", 14, "cs", GPIO_ACTIVE_HIGH),
  71. {}
  72. },
  73. };
  74. static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
  75. {
  76. void __iomem *reg;
  77. u32 value;
  78. reg = pcim_iomap(pci, GP_RWBAR, 0);
  79. if (!reg)
  80. return -ENOMEM;
  81. value = readl(reg + GP_RWREG1);
  82. if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
  83. goto unmap; /* ULPI refclk already enabled */
  84. value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
  85. writel(value, reg + GP_RWREG1);
  86. /* This comes from the Intel Android x86 tree w/o any explanation */
  87. msleep(100);
  88. unmap:
  89. pcim_iounmap(pci, reg);
  90. return 0;
  91. }
  92. static const struct property_entry dwc3_pci_intel_properties[] = {
  93. PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
  94. PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
  95. {}
  96. };
  97. static const struct property_entry dwc3_pci_mrfld_properties[] = {
  98. PROPERTY_ENTRY_STRING("dr_mode", "otg"),
  99. PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
  100. {}
  101. };
  102. static const struct property_entry dwc3_pci_amd_properties[] = {
  103. PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
  104. PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
  105. PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
  106. PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
  107. PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
  108. PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
  109. PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
  110. PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
  111. PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
  112. PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
  113. PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
  114. /* FIXME these quirks should be removed when AMD NL tapes out */
  115. PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
  116. PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
  117. PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
  118. PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
  119. {}
  120. };
  121. static int dwc3_pci_quirks(struct dwc3_pci *dwc)
  122. {
  123. struct pci_dev *pdev = dwc->pci;
  124. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  125. if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
  126. pdev->device == PCI_DEVICE_ID_INTEL_BXT_M) {
  127. guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
  128. dwc->has_dsm_for_pm = true;
  129. }
  130. if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
  131. struct gpio_desc *gpio;
  132. int ret;
  133. /* On BYT the FW does not always enable the refclock */
  134. ret = dwc3_byt_enable_ulpi_refclock(pdev);
  135. if (ret)
  136. return ret;
  137. ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
  138. acpi_dwc3_byt_gpios);
  139. if (ret)
  140. dev_dbg(&pdev->dev, "failed to add mapping table\n");
  141. /*
  142. * A lot of BYT devices lack ACPI resource entries for
  143. * the GPIOs, add a fallback mapping to the reference
  144. * design GPIOs which all boards seem to use.
  145. */
  146. gpiod_add_lookup_table(&platform_bytcr_gpios);
  147. /*
  148. * These GPIOs will turn on the USB2 PHY. Note that we have to
  149. * put the gpio descriptors again here because the phy driver
  150. * might want to grab them, too.
  151. */
  152. gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
  153. if (IS_ERR(gpio))
  154. return PTR_ERR(gpio);
  155. gpiod_set_value_cansleep(gpio, 1);
  156. gpiod_put(gpio);
  157. gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
  158. if (IS_ERR(gpio))
  159. return PTR_ERR(gpio);
  160. if (gpio) {
  161. gpiod_set_value_cansleep(gpio, 1);
  162. gpiod_put(gpio);
  163. usleep_range(10000, 11000);
  164. }
  165. }
  166. }
  167. return 0;
  168. }
  169. #ifdef CONFIG_PM
  170. static void dwc3_pci_resume_work(struct work_struct *work)
  171. {
  172. struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
  173. struct platform_device *dwc3 = dwc->dwc3;
  174. int ret;
  175. ret = pm_runtime_get_sync(&dwc3->dev);
  176. if (ret)
  177. return;
  178. pm_runtime_mark_last_busy(&dwc3->dev);
  179. pm_runtime_put_sync_autosuspend(&dwc3->dev);
  180. }
  181. #endif
  182. static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
  183. {
  184. struct property_entry *p = (struct property_entry *)id->driver_data;
  185. struct dwc3_pci *dwc;
  186. struct resource res[2];
  187. int ret;
  188. struct device *dev = &pci->dev;
  189. ret = pcim_enable_device(pci);
  190. if (ret) {
  191. dev_err(dev, "failed to enable pci device\n");
  192. return -ENODEV;
  193. }
  194. pci_set_master(pci);
  195. dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
  196. if (!dwc)
  197. return -ENOMEM;
  198. dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  199. if (!dwc->dwc3)
  200. return -ENOMEM;
  201. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  202. res[0].start = pci_resource_start(pci, 0);
  203. res[0].end = pci_resource_end(pci, 0);
  204. res[0].name = "dwc_usb3";
  205. res[0].flags = IORESOURCE_MEM;
  206. res[1].start = pci->irq;
  207. res[1].name = "dwc_usb3";
  208. res[1].flags = IORESOURCE_IRQ;
  209. ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
  210. if (ret) {
  211. dev_err(dev, "couldn't add resources to dwc3 device\n");
  212. goto err;
  213. }
  214. dwc->pci = pci;
  215. dwc->dwc3->dev.parent = dev;
  216. ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
  217. ret = platform_device_add_properties(dwc->dwc3, p);
  218. if (ret < 0)
  219. goto err;
  220. ret = dwc3_pci_quirks(dwc);
  221. if (ret)
  222. goto err;
  223. ret = platform_device_add(dwc->dwc3);
  224. if (ret) {
  225. dev_err(dev, "failed to register dwc3 device\n");
  226. goto err;
  227. }
  228. device_init_wakeup(dev, true);
  229. pci_set_drvdata(pci, dwc);
  230. pm_runtime_put(dev);
  231. #ifdef CONFIG_PM
  232. INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
  233. #endif
  234. return 0;
  235. err:
  236. platform_device_put(dwc->dwc3);
  237. return ret;
  238. }
  239. static void dwc3_pci_remove(struct pci_dev *pci)
  240. {
  241. struct dwc3_pci *dwc = pci_get_drvdata(pci);
  242. struct pci_dev *pdev = dwc->pci;
  243. if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
  244. gpiod_remove_lookup_table(&platform_bytcr_gpios);
  245. #ifdef CONFIG_PM
  246. cancel_work_sync(&dwc->wakeup_work);
  247. #endif
  248. device_init_wakeup(&pci->dev, false);
  249. pm_runtime_get(&pci->dev);
  250. platform_device_unregister(dwc->dwc3);
  251. }
  252. static const struct pci_device_id dwc3_pci_id_table[] = {
  253. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW),
  254. (kernel_ulong_t) &dwc3_pci_intel_properties },
  255. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT),
  256. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  257. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD),
  258. (kernel_ulong_t) &dwc3_pci_mrfld_properties, },
  259. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP),
  260. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  261. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH),
  262. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  263. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP),
  264. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  265. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH),
  266. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  267. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT),
  268. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  269. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M),
  270. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  271. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL),
  272. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  273. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP),
  274. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  275. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK),
  276. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  277. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP),
  278. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  279. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH),
  280. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  281. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV),
  282. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  283. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP),
  284. (kernel_ulong_t) &dwc3_pci_intel_properties, },
  285. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB),
  286. (kernel_ulong_t) &dwc3_pci_amd_properties, },
  287. { } /* Terminating Entry */
  288. };
  289. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  290. #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
  291. static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
  292. {
  293. union acpi_object *obj;
  294. union acpi_object tmp;
  295. union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
  296. if (!dwc->has_dsm_for_pm)
  297. return 0;
  298. tmp.type = ACPI_TYPE_INTEGER;
  299. tmp.integer.value = param;
  300. obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
  301. 1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
  302. if (!obj) {
  303. dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
  304. return -EIO;
  305. }
  306. ACPI_FREE(obj);
  307. return 0;
  308. }
  309. #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
  310. #ifdef CONFIG_PM
  311. static int dwc3_pci_runtime_suspend(struct device *dev)
  312. {
  313. struct dwc3_pci *dwc = dev_get_drvdata(dev);
  314. if (device_can_wakeup(dev))
  315. return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
  316. return -EBUSY;
  317. }
  318. static int dwc3_pci_runtime_resume(struct device *dev)
  319. {
  320. struct dwc3_pci *dwc = dev_get_drvdata(dev);
  321. int ret;
  322. ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
  323. if (ret)
  324. return ret;
  325. queue_work(pm_wq, &dwc->wakeup_work);
  326. return 0;
  327. }
  328. #endif /* CONFIG_PM */
  329. #ifdef CONFIG_PM_SLEEP
  330. static int dwc3_pci_suspend(struct device *dev)
  331. {
  332. struct dwc3_pci *dwc = dev_get_drvdata(dev);
  333. return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
  334. }
  335. static int dwc3_pci_resume(struct device *dev)
  336. {
  337. struct dwc3_pci *dwc = dev_get_drvdata(dev);
  338. return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
  339. }
  340. #endif /* CONFIG_PM_SLEEP */
  341. static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
  342. SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
  343. SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
  344. NULL)
  345. };
  346. static struct pci_driver dwc3_pci_driver = {
  347. .name = "dwc3-pci",
  348. .id_table = dwc3_pci_id_table,
  349. .probe = dwc3_pci_probe,
  350. .remove = dwc3_pci_remove,
  351. .driver = {
  352. .pm = &dwc3_pci_dev_pm_ops,
  353. }
  354. };
  355. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  356. MODULE_LICENSE("GPL v2");
  357. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  358. module_pci_driver(dwc3_pci_driver);