mcb-pci.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * MEN Chameleon Bus.
  3. *
  4. * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
  5. * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/mcb.h>
  14. #include "mcb-internal.h"
  15. struct priv {
  16. struct mcb_bus *bus;
  17. phys_addr_t mapbase;
  18. void __iomem *base;
  19. };
  20. static int mcb_pci_get_irq(struct mcb_device *mdev)
  21. {
  22. struct mcb_bus *mbus = mdev->bus;
  23. struct device *dev = mbus->carrier;
  24. struct pci_dev *pdev = to_pci_dev(dev);
  25. return pdev->irq;
  26. }
  27. static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  28. {
  29. struct resource *res;
  30. struct priv *priv;
  31. int ret;
  32. int num_cells;
  33. unsigned long flags;
  34. priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
  35. if (!priv)
  36. return -ENOMEM;
  37. ret = pci_enable_device(pdev);
  38. if (ret) {
  39. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  40. return -ENODEV;
  41. }
  42. priv->mapbase = pci_resource_start(pdev, 0);
  43. if (!priv->mapbase) {
  44. dev_err(&pdev->dev, "No PCI resource\n");
  45. ret = -ENODEV;
  46. goto out_disable;
  47. }
  48. res = request_mem_region(priv->mapbase, CHAM_HEADER_SIZE,
  49. KBUILD_MODNAME);
  50. if (!res) {
  51. dev_err(&pdev->dev, "Failed to request PCI memory\n");
  52. ret = -EBUSY;
  53. goto out_disable;
  54. }
  55. priv->base = ioremap(priv->mapbase, CHAM_HEADER_SIZE);
  56. if (!priv->base) {
  57. dev_err(&pdev->dev, "Cannot ioremap\n");
  58. ret = -ENOMEM;
  59. goto out_release;
  60. }
  61. flags = pci_resource_flags(pdev, 0);
  62. if (flags & IORESOURCE_IO) {
  63. ret = -ENOTSUPP;
  64. dev_err(&pdev->dev,
  65. "IO mapped PCI devices are not supported\n");
  66. goto out_iounmap;
  67. }
  68. pci_set_drvdata(pdev, priv);
  69. priv->bus = mcb_alloc_bus(&pdev->dev);
  70. if (IS_ERR(priv->bus)) {
  71. ret = PTR_ERR(priv->bus);
  72. goto out_iounmap;
  73. }
  74. priv->bus->get_irq = mcb_pci_get_irq;
  75. ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
  76. if (ret < 0)
  77. goto out_mcb_bus;
  78. num_cells = ret;
  79. dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
  80. mcb_bus_add_devices(priv->bus);
  81. return 0;
  82. out_mcb_bus:
  83. mcb_release_bus(priv->bus);
  84. out_iounmap:
  85. iounmap(priv->base);
  86. out_release:
  87. pci_release_region(pdev, 0);
  88. out_disable:
  89. pci_disable_device(pdev);
  90. return ret;
  91. }
  92. static void mcb_pci_remove(struct pci_dev *pdev)
  93. {
  94. struct priv *priv = pci_get_drvdata(pdev);
  95. mcb_release_bus(priv->bus);
  96. iounmap(priv->base);
  97. release_region(priv->mapbase, CHAM_HEADER_SIZE);
  98. pci_disable_device(pdev);
  99. }
  100. static const struct pci_device_id mcb_pci_tbl[] = {
  101. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  102. { 0 },
  103. };
  104. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  105. static struct pci_driver mcb_pci_driver = {
  106. .name = "mcb-pci",
  107. .id_table = mcb_pci_tbl,
  108. .probe = mcb_pci_probe,
  109. .remove = mcb_pci_remove,
  110. };
  111. module_pci_driver(mcb_pci_driver);
  112. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  113. MODULE_LICENSE("GPL");
  114. MODULE_DESCRIPTION("MCB over PCI support");