dwc3-pci.c 11 KB

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