mcb-pci.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <johannes.thumshirn@men.de>
  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;
  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. dev_dbg(&pdev->dev, "Found %d cells\n", ret);
  77. mcb_bus_add_devices(priv->bus);
  78. return 0;
  79. out_mcb_bus:
  80. mcb_release_bus(priv->bus);
  81. out_disable:
  82. pci_disable_device(pdev);
  83. return ret;
  84. }
  85. static void mcb_pci_remove(struct pci_dev *pdev)
  86. {
  87. struct priv *priv = pci_get_drvdata(pdev);
  88. mcb_release_bus(priv->bus);
  89. pci_disable_device(pdev);
  90. }
  91. static const struct pci_device_id mcb_pci_tbl[] = {
  92. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  93. { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
  94. { 0 },
  95. };
  96. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  97. static struct pci_driver mcb_pci_driver = {
  98. .name = "mcb-pci",
  99. .id_table = mcb_pci_tbl,
  100. .probe = mcb_pci_probe,
  101. .remove = mcb_pci_remove,
  102. };
  103. module_pci_driver(mcb_pci_driver);
  104. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  105. MODULE_LICENSE("GPL");
  106. MODULE_DESCRIPTION("MCB over PCI support");
  107. MODULE_IMPORT_NS(MCB);