dwc3-pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_gen_xceiv.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_gen_xceiv_platform_data pdata;
  39. struct platform_device *pdev;
  40. int ret;
  41. memset(&pdata, 0x00, sizeof(pdata));
  42. pdev = platform_device_alloc("usb_phy_gen_xceiv", 0);
  43. if (!pdev)
  44. return -ENOMEM;
  45. glue->usb2_phy = pdev;
  46. pdata.type = USB_PHY_TYPE_USB2;
  47. ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
  48. if (ret)
  49. goto err1;
  50. pdev = platform_device_alloc("usb_phy_gen_xceiv", 1);
  51. if (!pdev) {
  52. ret = -ENOMEM;
  53. goto err1;
  54. }
  55. glue->usb3_phy = pdev;
  56. pdata.type = USB_PHY_TYPE_USB3;
  57. ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
  58. if (ret)
  59. goto err2;
  60. ret = platform_device_add(glue->usb2_phy);
  61. if (ret)
  62. goto err2;
  63. ret = platform_device_add(glue->usb3_phy);
  64. if (ret)
  65. goto err3;
  66. return 0;
  67. err3:
  68. platform_device_del(glue->usb2_phy);
  69. err2:
  70. platform_device_put(glue->usb3_phy);
  71. err1:
  72. platform_device_put(glue->usb2_phy);
  73. return ret;
  74. }
  75. static int dwc3_pci_probe(struct pci_dev *pci,
  76. const struct pci_device_id *id)
  77. {
  78. struct resource res[2];
  79. struct platform_device *dwc3;
  80. struct dwc3_pci *glue;
  81. int ret = -ENOMEM;
  82. struct device *dev = &pci->dev;
  83. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  84. if (!glue) {
  85. dev_err(dev, "not enough memory\n");
  86. return -ENOMEM;
  87. }
  88. glue->dev = dev;
  89. ret = pci_enable_device(pci);
  90. if (ret) {
  91. dev_err(dev, "failed to enable pci device\n");
  92. return -ENODEV;
  93. }
  94. pci_set_master(pci);
  95. ret = dwc3_pci_register_phys(glue);
  96. if (ret) {
  97. dev_err(dev, "couldn't register PHYs\n");
  98. return ret;
  99. }
  100. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  101. if (!dwc3) {
  102. dev_err(dev, "couldn't allocate dwc3 device\n");
  103. ret = -ENOMEM;
  104. goto err1;
  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. goto err1;
  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. pci_set_drvdata(pci, NULL);
  133. platform_device_put(dwc3);
  134. err1:
  135. pci_disable_device(pci);
  136. return ret;
  137. }
  138. static void dwc3_pci_remove(struct pci_dev *pci)
  139. {
  140. struct dwc3_pci *glue = pci_get_drvdata(pci);
  141. platform_device_unregister(glue->dwc3);
  142. platform_device_unregister(glue->usb2_phy);
  143. platform_device_unregister(glue->usb3_phy);
  144. pci_set_drvdata(pci, NULL);
  145. pci_disable_device(pci);
  146. }
  147. static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
  148. {
  149. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  150. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  151. },
  152. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BYT), },
  153. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_MRFLD), },
  154. { } /* Terminating Entry */
  155. };
  156. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  157. #ifdef CONFIG_PM_SLEEP
  158. static int dwc3_pci_suspend(struct device *dev)
  159. {
  160. struct pci_dev *pci = to_pci_dev(dev);
  161. pci_disable_device(pci);
  162. return 0;
  163. }
  164. static int dwc3_pci_resume(struct device *dev)
  165. {
  166. struct pci_dev *pci = to_pci_dev(dev);
  167. int ret;
  168. ret = pci_enable_device(pci);
  169. if (ret) {
  170. dev_err(dev, "can't re-enable device --> %d\n", ret);
  171. return ret;
  172. }
  173. pci_set_master(pci);
  174. return 0;
  175. }
  176. #endif /* CONFIG_PM_SLEEP */
  177. static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
  178. SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
  179. };
  180. static struct pci_driver dwc3_pci_driver = {
  181. .name = "dwc3-pci",
  182. .id_table = dwc3_pci_id_table,
  183. .probe = dwc3_pci_probe,
  184. .remove = dwc3_pci_remove,
  185. .driver = {
  186. .pm = &dwc3_pci_dev_pm_ops,
  187. },
  188. };
  189. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  192. module_pci_driver(dwc3_pci_driver);