mcb-pci.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. void __iomem *base;
  18. };
  19. static int mcb_pci_get_irq(struct mcb_device *mdev)
  20. {
  21. struct mcb_bus *mbus = mdev->bus;
  22. struct device *dev = mbus->carrier;
  23. struct pci_dev *pdev = to_pci_dev(dev);
  24. return pdev->irq;
  25. }
  26. static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  27. {
  28. struct priv *priv;
  29. phys_addr_t mapbase;
  30. int ret;
  31. int num_cells;
  32. unsigned long flags;
  33. priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
  34. if (!priv)
  35. return -ENOMEM;
  36. ret = pci_enable_device(pdev);
  37. if (ret) {
  38. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  39. return -ENODEV;
  40. }
  41. mapbase = pci_resource_start(pdev, 0);
  42. if (!mapbase) {
  43. dev_err(&pdev->dev, "No PCI resource\n");
  44. goto err_start;
  45. }
  46. ret = pci_request_region(pdev, 0, KBUILD_MODNAME);
  47. if (ret) {
  48. dev_err(&pdev->dev, "Failed to request PCI BARs\n");
  49. goto err_start;
  50. }
  51. priv->base = pci_iomap(pdev, 0, 0);
  52. if (!priv->base) {
  53. dev_err(&pdev->dev, "Cannot ioremap\n");
  54. ret = -ENOMEM;
  55. goto err_ioremap;
  56. }
  57. flags = pci_resource_flags(pdev, 0);
  58. if (flags & IORESOURCE_IO) {
  59. ret = -ENOTSUPP;
  60. dev_err(&pdev->dev,
  61. "IO mapped PCI devices are not supported\n");
  62. goto err_ioremap;
  63. }
  64. pci_set_drvdata(pdev, priv);
  65. priv->bus = mcb_alloc_bus(&pdev->dev);
  66. if (IS_ERR(priv->bus)) {
  67. ret = PTR_ERR(priv->bus);
  68. goto err_drvdata;
  69. }
  70. priv->bus->get_irq = mcb_pci_get_irq;
  71. ret = chameleon_parse_cells(priv->bus, mapbase, priv->base);
  72. if (ret < 0)
  73. goto err_drvdata;
  74. num_cells = ret;
  75. dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
  76. mcb_bus_add_devices(priv->bus);
  77. err_drvdata:
  78. pci_iounmap(pdev, priv->base);
  79. err_ioremap:
  80. pci_release_region(pdev, 0);
  81. err_start:
  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. }
  90. static const struct pci_device_id mcb_pci_tbl[] = {
  91. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  92. { 0 },
  93. };
  94. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  95. static struct pci_driver mcb_pci_driver = {
  96. .name = "mcb-pci",
  97. .id_table = mcb_pci_tbl,
  98. .probe = mcb_pci_probe,
  99. .remove = mcb_pci_remove,
  100. };
  101. module_pci_driver(mcb_pci_driver);
  102. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  103. MODULE_LICENSE("GPL");
  104. MODULE_DESCRIPTION("MCB over PCI support");