dwc3-pci.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. struct dwc3_pci {
  29. struct device *dev;
  30. struct platform_device *dwc3;
  31. struct platform_device *usb2_phy;
  32. struct platform_device *usb3_phy;
  33. };
  34. static int dwc3_pci_register_phys(struct dwc3_pci *glue)
  35. {
  36. struct usb_phy_gen_xceiv_platform_data pdata;
  37. struct platform_device *pdev;
  38. int ret;
  39. memset(&pdata, 0x00, sizeof(pdata));
  40. pdev = platform_device_alloc("usb_phy_gen_xceiv", 0);
  41. if (!pdev)
  42. return -ENOMEM;
  43. glue->usb2_phy = pdev;
  44. pdata.type = USB_PHY_TYPE_USB2;
  45. ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
  46. if (ret)
  47. goto err1;
  48. pdev = platform_device_alloc("usb_phy_gen_xceiv", 1);
  49. if (!pdev) {
  50. ret = -ENOMEM;
  51. goto err1;
  52. }
  53. glue->usb3_phy = pdev;
  54. pdata.type = USB_PHY_TYPE_USB3;
  55. ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
  56. if (ret)
  57. goto err2;
  58. ret = platform_device_add(glue->usb2_phy);
  59. if (ret)
  60. goto err2;
  61. ret = platform_device_add(glue->usb3_phy);
  62. if (ret)
  63. goto err3;
  64. return 0;
  65. err3:
  66. platform_device_del(glue->usb2_phy);
  67. err2:
  68. platform_device_put(glue->usb3_phy);
  69. err1:
  70. platform_device_put(glue->usb2_phy);
  71. return ret;
  72. }
  73. static int dwc3_pci_probe(struct pci_dev *pci,
  74. const struct pci_device_id *id)
  75. {
  76. struct resource res[2];
  77. struct platform_device *dwc3;
  78. struct dwc3_pci *glue;
  79. int ret = -ENOMEM;
  80. struct device *dev = &pci->dev;
  81. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  82. if (!glue) {
  83. dev_err(dev, "not enough memory\n");
  84. return -ENOMEM;
  85. }
  86. glue->dev = dev;
  87. ret = pci_enable_device(pci);
  88. if (ret) {
  89. dev_err(dev, "failed to enable pci device\n");
  90. return -ENODEV;
  91. }
  92. pci_set_master(pci);
  93. ret = dwc3_pci_register_phys(glue);
  94. if (ret) {
  95. dev_err(dev, "couldn't register PHYs\n");
  96. return ret;
  97. }
  98. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  99. if (!dwc3) {
  100. dev_err(dev, "couldn't allocate dwc3 device\n");
  101. ret = -ENOMEM;
  102. goto err1;
  103. }
  104. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  105. res[0].start = pci_resource_start(pci, 0);
  106. res[0].end = pci_resource_end(pci, 0);
  107. res[0].name = "dwc_usb3";
  108. res[0].flags = IORESOURCE_MEM;
  109. res[1].start = pci->irq;
  110. res[1].name = "dwc_usb3";
  111. res[1].flags = IORESOURCE_IRQ;
  112. ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
  113. if (ret) {
  114. dev_err(dev, "couldn't add resources to dwc3 device\n");
  115. goto err1;
  116. }
  117. pci_set_drvdata(pci, glue);
  118. dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
  119. dwc3->dev.dma_mask = dev->dma_mask;
  120. dwc3->dev.dma_parms = dev->dma_parms;
  121. dwc3->dev.parent = dev;
  122. glue->dwc3 = dwc3;
  123. ret = platform_device_add(dwc3);
  124. if (ret) {
  125. dev_err(dev, "failed to register dwc3 device\n");
  126. goto err3;
  127. }
  128. return 0;
  129. err3:
  130. pci_set_drvdata(pci, NULL);
  131. platform_device_put(dwc3);
  132. err1:
  133. pci_disable_device(pci);
  134. return ret;
  135. }
  136. static void dwc3_pci_remove(struct pci_dev *pci)
  137. {
  138. struct dwc3_pci *glue = pci_get_drvdata(pci);
  139. platform_device_unregister(glue->dwc3);
  140. platform_device_unregister(glue->usb2_phy);
  141. platform_device_unregister(glue->usb3_phy);
  142. pci_set_drvdata(pci, NULL);
  143. pci_disable_device(pci);
  144. }
  145. static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
  146. {
  147. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  148. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  149. },
  150. { } /* Terminating Entry */
  151. };
  152. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  153. #ifdef CONFIG_PM_SLEEP
  154. static int dwc3_pci_suspend(struct device *dev)
  155. {
  156. struct pci_dev *pci = to_pci_dev(dev);
  157. pci_disable_device(pci);
  158. return 0;
  159. }
  160. static int dwc3_pci_resume(struct device *dev)
  161. {
  162. struct pci_dev *pci = to_pci_dev(dev);
  163. int ret;
  164. ret = pci_enable_device(pci);
  165. if (ret) {
  166. dev_err(dev, "can't re-enable device --> %d\n", ret);
  167. return ret;
  168. }
  169. pci_set_master(pci);
  170. return 0;
  171. }
  172. #endif /* CONFIG_PM_SLEEP */
  173. static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
  174. SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
  175. };
  176. static struct pci_driver dwc3_pci_driver = {
  177. .name = "dwc3-pci",
  178. .id_table = dwc3_pci_id_table,
  179. .probe = dwc3_pci_probe,
  180. .remove = dwc3_pci_remove,
  181. .driver = {
  182. .pm = &dwc3_pci_dev_pm_ops,
  183. },
  184. };
  185. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  186. MODULE_LICENSE("GPL v2");
  187. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  188. module_pci_driver(dwc3_pci_driver);