dwc3-pci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * dwc3-pci.c - PCI Specific glue layer
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/usb/usb_phy_generic.h>
  25. /* FIXME define these in <linux/pci_ids.h> */
  26. #define PCI_VENDOR_ID_SYNOPSYS 0x16c3
  27. #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
  28. #define PCI_DEVICE_ID_INTEL_BYT 0x0f37
  29. #define PCI_DEVICE_ID_INTEL_MRFLD 0x119e
  30. struct dwc3_pci {
  31. struct device *dev;
  32. struct platform_device *dwc3;
  33. struct platform_device *usb2_phy;
  34. struct platform_device *usb3_phy;
  35. };
  36. static int dwc3_pci_register_phys(struct dwc3_pci *glue)
  37. {
  38. struct usb_phy_generic_platform_data pdata;
  39. struct platform_device *pdev;
  40. int ret;
  41. memset(&pdata, 0x00, sizeof(pdata));
  42. pdev = platform_device_alloc("usb_phy_generic", 0);
  43. if (!pdev)
  44. return -ENOMEM;
  45. glue->usb2_phy = pdev;
  46. pdata.type = USB_PHY_TYPE_USB2;
  47. pdata.gpio_reset = -1;
  48. ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
  49. if (ret)
  50. goto err1;
  51. pdev = platform_device_alloc("usb_phy_generic", 1);
  52. if (!pdev) {
  53. ret = -ENOMEM;
  54. goto err1;
  55. }
  56. glue->usb3_phy = pdev;
  57. pdata.type = USB_PHY_TYPE_USB3;
  58. ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
  59. if (ret)
  60. goto err2;
  61. ret = platform_device_add(glue->usb2_phy);
  62. if (ret)
  63. goto err2;
  64. ret = platform_device_add(glue->usb3_phy);
  65. if (ret)
  66. goto err3;
  67. return 0;
  68. err3:
  69. platform_device_del(glue->usb2_phy);
  70. err2:
  71. platform_device_put(glue->usb3_phy);
  72. err1:
  73. platform_device_put(glue->usb2_phy);
  74. return ret;
  75. }
  76. static int dwc3_pci_probe(struct pci_dev *pci,
  77. const struct pci_device_id *id)
  78. {
  79. struct resource res[2];
  80. struct platform_device *dwc3;
  81. struct dwc3_pci *glue;
  82. int ret;
  83. struct device *dev = &pci->dev;
  84. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  85. if (!glue) {
  86. dev_err(dev, "not enough memory\n");
  87. return -ENOMEM;
  88. }
  89. glue->dev = dev;
  90. ret = pcim_enable_device(pci);
  91. if (ret) {
  92. dev_err(dev, "failed to enable pci device\n");
  93. return -ENODEV;
  94. }
  95. pci_set_master(pci);
  96. ret = dwc3_pci_register_phys(glue);
  97. if (ret) {
  98. dev_err(dev, "couldn't register PHYs\n");
  99. return ret;
  100. }
  101. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  102. if (!dwc3) {
  103. dev_err(dev, "couldn't allocate dwc3 device\n");
  104. return -ENOMEM;
  105. }
  106. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  107. res[0].start = pci_resource_start(pci, 0);
  108. res[0].end = pci_resource_end(pci, 0);
  109. res[0].name = "dwc_usb3";
  110. res[0].flags = IORESOURCE_MEM;
  111. res[1].start = pci->irq;
  112. res[1].name = "dwc_usb3";
  113. res[1].flags = IORESOURCE_IRQ;
  114. ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
  115. if (ret) {
  116. dev_err(dev, "couldn't add resources to dwc3 device\n");
  117. return ret;
  118. }
  119. pci_set_drvdata(pci, glue);
  120. dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
  121. dwc3->dev.dma_mask = dev->dma_mask;
  122. dwc3->dev.dma_parms = dev->dma_parms;
  123. dwc3->dev.parent = dev;
  124. glue->dwc3 = dwc3;
  125. ret = platform_device_add(dwc3);
  126. if (ret) {
  127. dev_err(dev, "failed to register dwc3 device\n");
  128. goto err3;
  129. }
  130. return 0;
  131. err3:
  132. platform_device_put(dwc3);
  133. return ret;
  134. }
  135. static void dwc3_pci_remove(struct pci_dev *pci)
  136. {
  137. struct dwc3_pci *glue = pci_get_drvdata(pci);
  138. platform_device_unregister(glue->dwc3);
  139. platform_device_unregister(glue->usb2_phy);
  140. platform_device_unregister(glue->usb3_phy);
  141. }
  142. static const struct pci_device_id dwc3_pci_id_table[] = {
  143. {
  144. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  145. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  146. },
  147. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BYT), },
  148. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_MRFLD), },
  149. { } /* Terminating Entry */
  150. };
  151. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  152. #ifdef CONFIG_PM_SLEEP
  153. static int dwc3_pci_suspend(struct device *dev)
  154. {
  155. struct pci_dev *pci = to_pci_dev(dev);
  156. pci_disable_device(pci);
  157. return 0;
  158. }
  159. static int dwc3_pci_resume(struct device *dev)
  160. {
  161. struct pci_dev *pci = to_pci_dev(dev);
  162. int ret;
  163. ret = pci_enable_device(pci);
  164. if (ret) {
  165. dev_err(dev, "can't re-enable device --> %d\n", ret);
  166. return ret;
  167. }
  168. pci_set_master(pci);
  169. return 0;
  170. }
  171. #endif /* CONFIG_PM_SLEEP */
  172. static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
  173. SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
  174. };
  175. static struct pci_driver dwc3_pci_driver = {
  176. .name = "dwc3-pci",
  177. .id_table = dwc3_pci_id_table,
  178. .probe = dwc3_pci_probe,
  179. .remove = dwc3_pci_remove,
  180. .driver = {
  181. .pm = &dwc3_pci_dev_pm_ops,
  182. },
  183. };
  184. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  185. MODULE_LICENSE("GPL v2");
  186. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  187. module_pci_driver(dwc3_pci_driver);