amd5536udc_pci.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * amd5536udc_pci.c -- AMD 5536 UDC high/full speed USB device controller
  4. *
  5. * Copyright (C) 2005-2007 AMD (https://www.amd.com)
  6. * Author: Thomas Dahlmann
  7. */
  8. /*
  9. * The AMD5536 UDC is part of the x86 southbridge AMD Geode CS5536.
  10. * It is a USB Highspeed DMA capable USB device controller. Beside ep0 it
  11. * provides 4 IN and 4 OUT endpoints (bulk or interrupt type).
  12. *
  13. * Make sure that UDC is assigned to port 4 by BIOS settings (port can also
  14. * be used as host port) and UOC bits PAD_EN and APU are set (should be done
  15. * by BIOS init).
  16. *
  17. * UDC DMA requires 32-bit aligned buffers so DMA with gadget ether does not
  18. * work without updating NET_IP_ALIGN. Or PIO mode (module param "use_dma=0")
  19. * can be used with gadget ether.
  20. *
  21. * This file does pci device registration, and the core driver implementation
  22. * is done in amd5536udc.c
  23. *
  24. * The driver is split so as to use the core UDC driver which is based on
  25. * Synopsys device controller IP (different than HS OTG IP) in UDCs
  26. * integrated to SoC platforms.
  27. *
  28. */
  29. /* Driver strings */
  30. #define UDC_MOD_DESCRIPTION "AMD 5536 UDC - USB Device Controller"
  31. /* system */
  32. #include <linux/device.h>
  33. #include <linux/dmapool.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/irq.h>
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/prefetch.h>
  40. #include <linux/pci.h>
  41. /* udc specific */
  42. #include "amd5536udc.h"
  43. /* pointer to device object */
  44. static struct udc *udc;
  45. /* description */
  46. static const char name[] = "amd5536udc-pci";
  47. /* Reset all pci context */
  48. static void udc_pci_remove(struct pci_dev *pdev)
  49. {
  50. struct udc *dev;
  51. dev = pci_get_drvdata(pdev);
  52. usb_del_gadget_udc(&udc->gadget);
  53. /* gadget driver must not be registered */
  54. if (WARN_ON(dev->driver))
  55. return;
  56. /* dma pool cleanup */
  57. free_dma_pools(dev);
  58. /* reset controller */
  59. writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
  60. free_irq(pdev->irq, dev);
  61. iounmap(dev->virt_addr);
  62. release_mem_region(pci_resource_start(pdev, 0),
  63. pci_resource_len(pdev, 0));
  64. pci_disable_device(pdev);
  65. udc_remove(dev);
  66. }
  67. /* Called by pci bus driver to init pci context */
  68. static int udc_pci_probe(
  69. struct pci_dev *pdev,
  70. const struct pci_device_id *id
  71. )
  72. {
  73. struct udc *dev;
  74. unsigned long resource;
  75. unsigned long len;
  76. int retval = 0;
  77. /* one udc only */
  78. if (udc) {
  79. dev_dbg(&pdev->dev, "already probed\n");
  80. return -EBUSY;
  81. }
  82. /* init */
  83. dev = kzalloc(sizeof(struct udc), GFP_KERNEL);
  84. if (!dev)
  85. return -ENOMEM;
  86. /* pci setup */
  87. if (pci_enable_device(pdev) < 0) {
  88. retval = -ENODEV;
  89. goto err_pcidev;
  90. }
  91. /* PCI resource allocation */
  92. resource = pci_resource_start(pdev, 0);
  93. len = pci_resource_len(pdev, 0);
  94. if (!request_mem_region(resource, len, name)) {
  95. dev_dbg(&pdev->dev, "pci device used already\n");
  96. retval = -EBUSY;
  97. goto err_memreg;
  98. }
  99. dev->virt_addr = ioremap(resource, len);
  100. if (!dev->virt_addr) {
  101. dev_dbg(&pdev->dev, "start address cannot be mapped\n");
  102. retval = -EFAULT;
  103. goto err_ioremap;
  104. }
  105. if (!pdev->irq) {
  106. dev_err(&pdev->dev, "irq not set\n");
  107. retval = -ENODEV;
  108. goto err_irq;
  109. }
  110. spin_lock_init(&dev->lock);
  111. /* udc csr registers base */
  112. dev->csr = dev->virt_addr + UDC_CSR_ADDR;
  113. /* dev registers base */
  114. dev->regs = dev->virt_addr + UDC_DEVCFG_ADDR;
  115. /* ep registers base */
  116. dev->ep_regs = dev->virt_addr + UDC_EPREGS_ADDR;
  117. /* fifo's base */
  118. dev->rxfifo = (u32 __iomem *)(dev->virt_addr + UDC_RXFIFO_ADDR);
  119. dev->txfifo = (u32 __iomem *)(dev->virt_addr + UDC_TXFIFO_ADDR);
  120. if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) {
  121. dev_dbg(&pdev->dev, "request_irq(%d) fail\n", pdev->irq);
  122. retval = -EBUSY;
  123. goto err_irq;
  124. }
  125. pci_set_drvdata(pdev, dev);
  126. /* chip revision for Hs AMD5536 */
  127. dev->chiprev = pdev->revision;
  128. pci_set_master(pdev);
  129. pci_try_set_mwi(pdev);
  130. dev->phys_addr = resource;
  131. dev->irq = pdev->irq;
  132. dev->pdev = pdev;
  133. dev->dev = &pdev->dev;
  134. /* init dma pools */
  135. if (use_dma) {
  136. retval = init_dma_pools(dev);
  137. if (retval != 0)
  138. goto err_dma;
  139. }
  140. /* general probing */
  141. if (udc_probe(dev)) {
  142. retval = -ENODEV;
  143. goto err_probe;
  144. }
  145. udc = dev;
  146. return 0;
  147. err_probe:
  148. if (use_dma)
  149. free_dma_pools(dev);
  150. err_dma:
  151. free_irq(pdev->irq, dev);
  152. err_irq:
  153. iounmap(dev->virt_addr);
  154. err_ioremap:
  155. release_mem_region(resource, len);
  156. err_memreg:
  157. pci_disable_device(pdev);
  158. err_pcidev:
  159. kfree(dev);
  160. return retval;
  161. }
  162. /* PCI device parameters */
  163. static const struct pci_device_id pci_id[] = {
  164. {
  165. PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x2096),
  166. .class = PCI_CLASS_SERIAL_USB_DEVICE,
  167. .class_mask = 0xffffffff,
  168. },
  169. {},
  170. };
  171. MODULE_DEVICE_TABLE(pci, pci_id);
  172. /* PCI functions */
  173. static struct pci_driver udc_pci_driver = {
  174. .name = name,
  175. .id_table = pci_id,
  176. .probe = udc_pci_probe,
  177. .remove = udc_pci_remove,
  178. };
  179. module_pci_driver(udc_pci_driver);
  180. MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION);
  181. MODULE_AUTHOR("Thomas Dahlmann");
  182. MODULE_LICENSE("GPL");