mcb-pci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * MEN Chameleon Bus.
  3. *
  4. * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
  5. * Author: Johannes Thumshirn <[email protected]>
  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. pci_set_master(pdev);
  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 = devm_request_mem_region(&pdev->dev, priv->mapbase,
  49. CHAM_HEADER_SIZE,
  50. KBUILD_MODNAME);
  51. if (!res) {
  52. dev_err(&pdev->dev, "Failed to request PCI memory\n");
  53. ret = -EBUSY;
  54. goto out_disable;
  55. }
  56. priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
  57. if (!priv->base) {
  58. dev_err(&pdev->dev, "Cannot ioremap\n");
  59. ret = -ENOMEM;
  60. goto out_disable;
  61. }
  62. flags = pci_resource_flags(pdev, 0);
  63. if (flags & IORESOURCE_IO) {
  64. ret = -ENOTSUPP;
  65. dev_err(&pdev->dev,
  66. "IO mapped PCI devices are not supported\n");
  67. goto out_disable;
  68. }
  69. pci_set_drvdata(pdev, priv);
  70. priv->bus = mcb_alloc_bus(&pdev->dev);
  71. if (IS_ERR(priv->bus)) {
  72. ret = PTR_ERR(priv->bus);
  73. goto out_disable;
  74. }
  75. priv->bus->get_irq = mcb_pci_get_irq;
  76. ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
  77. if (ret < 0)
  78. goto out_mcb_bus;
  79. dev_dbg(&pdev->dev, "Found %d cells\n", ret);
  80. mcb_bus_add_devices(priv->bus);
  81. return 0;
  82. out_mcb_bus:
  83. mcb_release_bus(priv->bus);
  84. out_disable:
  85. pci_disable_device(pdev);
  86. return ret;
  87. }
  88. static void mcb_pci_remove(struct pci_dev *pdev)
  89. {
  90. struct priv *priv = pci_get_drvdata(pdev);
  91. mcb_release_bus(priv->bus);
  92. pci_disable_device(pdev);
  93. }
  94. static const struct pci_device_id mcb_pci_tbl[] = {
  95. { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
  96. { 0 },
  97. };
  98. MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
  99. static struct pci_driver mcb_pci_driver = {
  100. .name = "mcb-pci",
  101. .id_table = mcb_pci_tbl,
  102. .probe = mcb_pci_probe,
  103. .remove = mcb_pci_remove,
  104. };
  105. module_pci_driver(mcb_pci_driver);
  106. MODULE_AUTHOR("Johannes Thumshirn <[email protected]>");
  107. MODULE_LICENSE("GPL");
  108. MODULE_DESCRIPTION("MCB over PCI support");