mcb-pci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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. priv->mapbase = pci_resource_start(pdev, 0);
  42. if (!priv->mapbase) {
  43. dev_err(&pdev->dev, "No PCI resource\n");
  44. ret = -ENODEV;
  45. goto out_disable;
  46. }
  47. res = devm_request_mem_region(&pdev->dev, priv->mapbase,
  48. 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 = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
  56. if (!priv->base) {
  57. dev_err(&pdev->dev, "Cannot ioremap\n");
  58. ret = -ENOMEM;
  59. goto out_disable;
  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_disable;
  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_disable;
  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. dev_dbg(&pdev->dev, "Found %d cells\n", ret);
  79. mcb_bus_add_devices(priv->bus);
  80. return 0;
  81. out_mcb_bus:
  82. mcb_release_bus(priv->bus);
  83. out_disable:
  84. pci_disable_device(pdev);
  85. return ret;
  86. }
  87. static void mcb_pci_remove(struct pci_dev *pdev)
  88. {
  89. struct priv *priv = pci_get_drvdata(pdev);
  90. mcb_release_bus(priv->bus);
  91. pci_disable_device(pdev);
  92. }
  93. static const struct pci_device_id mcb_pci_tbl[] = {
  94. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  95. { 0 },
  96. };
  97. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  98. static struct pci_driver mcb_pci_driver = {
  99. .name = "mcb-pci",
  100. .id_table = mcb_pci_tbl,
  101. .probe = mcb_pci_probe,
  102. .remove = mcb_pci_remove,
  103. };
  104. module_pci_driver(mcb_pci_driver);
  105. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  106. MODULE_LICENSE("GPL");
  107. MODULE_DESCRIPTION("MCB over PCI support");