mcb-pci.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MEN Chameleon Bus.
  4. *
  5. * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
  6. * Author: Johannes Thumshirn <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/mcb.h>
  11. #include "mcb-internal.h"
  12. struct priv {
  13. struct mcb_bus *bus;
  14. phys_addr_t mapbase;
  15. void __iomem *base;
  16. };
  17. static int mcb_pci_get_irq(struct mcb_device *mdev)
  18. {
  19. struct mcb_bus *mbus = mdev->bus;
  20. struct device *dev = mbus->carrier;
  21. struct pci_dev *pdev = to_pci_dev(dev);
  22. return pdev->irq;
  23. }
  24. static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  25. {
  26. struct resource *res;
  27. struct priv *priv;
  28. int ret, table_size;
  29. unsigned long flags;
  30. priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
  31. if (!priv)
  32. return -ENOMEM;
  33. ret = pci_enable_device(pdev);
  34. if (ret) {
  35. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  36. return -ENODEV;
  37. }
  38. pci_set_master(pdev);
  39. priv->mapbase = pci_resource_start(pdev, 0);
  40. if (!priv->mapbase) {
  41. dev_err(&pdev->dev, "No PCI resource\n");
  42. ret = -ENODEV;
  43. goto out_disable;
  44. }
  45. res = devm_request_mem_region(&pdev->dev, priv->mapbase,
  46. CHAM_HEADER_SIZE,
  47. KBUILD_MODNAME);
  48. if (!res) {
  49. dev_err(&pdev->dev, "Failed to request PCI memory\n");
  50. ret = -EBUSY;
  51. goto out_disable;
  52. }
  53. priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
  54. if (!priv->base) {
  55. dev_err(&pdev->dev, "Cannot ioremap\n");
  56. ret = -ENOMEM;
  57. goto out_disable;
  58. }
  59. flags = pci_resource_flags(pdev, 0);
  60. if (flags & IORESOURCE_IO) {
  61. ret = -ENOTSUPP;
  62. dev_err(&pdev->dev,
  63. "IO mapped PCI devices are not supported\n");
  64. goto out_disable;
  65. }
  66. pci_set_drvdata(pdev, priv);
  67. priv->bus = mcb_alloc_bus(&pdev->dev);
  68. if (IS_ERR(priv->bus)) {
  69. ret = PTR_ERR(priv->bus);
  70. goto out_disable;
  71. }
  72. priv->bus->get_irq = mcb_pci_get_irq;
  73. ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
  74. if (ret < 0)
  75. goto out_mcb_bus;
  76. table_size = ret;
  77. if (table_size < CHAM_HEADER_SIZE) {
  78. /* Release the previous resources */
  79. devm_iounmap(&pdev->dev, priv->base);
  80. devm_release_mem_region(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
  81. /* Then, allocate it again with the actual chameleon table size */
  82. res = devm_request_mem_region(&pdev->dev, priv->mapbase,
  83. table_size,
  84. KBUILD_MODNAME);
  85. if (!res) {
  86. dev_err(&pdev->dev, "Failed to request PCI memory\n");
  87. ret = -EBUSY;
  88. goto out_mcb_bus;
  89. }
  90. priv->base = devm_ioremap(&pdev->dev, priv->mapbase, table_size);
  91. if (!priv->base) {
  92. dev_err(&pdev->dev, "Cannot ioremap\n");
  93. ret = -ENOMEM;
  94. goto out_mcb_bus;
  95. }
  96. }
  97. mcb_bus_add_devices(priv->bus);
  98. return 0;
  99. out_mcb_bus:
  100. mcb_release_bus(priv->bus);
  101. out_disable:
  102. pci_disable_device(pdev);
  103. return ret;
  104. }
  105. static void mcb_pci_remove(struct pci_dev *pdev)
  106. {
  107. struct priv *priv = pci_get_drvdata(pdev);
  108. mcb_release_bus(priv->bus);
  109. pci_disable_device(pdev);
  110. }
  111. static const struct pci_device_id mcb_pci_tbl[] = {
  112. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  113. { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
  114. { 0 },
  115. };
  116. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  117. static struct pci_driver mcb_pci_driver = {
  118. .name = "mcb-pci",
  119. .id_table = mcb_pci_tbl,
  120. .probe = mcb_pci_probe,
  121. .remove = mcb_pci_remove,
  122. };
  123. module_pci_driver(mcb_pci_driver);
  124. MODULE_AUTHOR("Johannes Thumshirn <[email protected]>");
  125. MODULE_LICENSE("GPL");
  126. MODULE_DESCRIPTION("MCB over PCI support");
  127. MODULE_IMPORT_NS(MCB);