dwc3-pci.c 5.2 KB

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