pci-host-generic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Simple, generic PCI host controller driver targetting firmware-initialised
  3. * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Copyright (C) 2014 ARM Limited
  18. *
  19. * Author: Will Deacon <will.deacon@arm.com>
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/platform_device.h>
  26. #include "pci-host-common.h"
  27. static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
  28. unsigned int devfn,
  29. int where)
  30. {
  31. struct gen_pci *pci = bus->sysdata;
  32. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  33. return pci->cfg.win[idx] + ((devfn << 8) | where);
  34. }
  35. static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
  36. .bus_shift = 16,
  37. .ops = {
  38. .map_bus = gen_pci_map_cfg_bus_cam,
  39. .read = pci_generic_config_read,
  40. .write = pci_generic_config_write,
  41. }
  42. };
  43. static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
  44. unsigned int devfn,
  45. int where)
  46. {
  47. struct gen_pci *pci = bus->sysdata;
  48. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  49. return pci->cfg.win[idx] + ((devfn << 12) | where);
  50. }
  51. static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
  52. .bus_shift = 20,
  53. .ops = {
  54. .map_bus = gen_pci_map_cfg_bus_ecam,
  55. .read = pci_generic_config_read,
  56. .write = pci_generic_config_write,
  57. }
  58. };
  59. static const struct of_device_id gen_pci_of_match[] = {
  60. { .compatible = "pci-host-cam-generic",
  61. .data = &gen_pci_cfg_cam_bus_ops },
  62. { .compatible = "pci-host-ecam-generic",
  63. .data = &gen_pci_cfg_ecam_bus_ops },
  64. { },
  65. };
  66. MODULE_DEVICE_TABLE(of, gen_pci_of_match);
  67. static int gen_pci_probe(struct platform_device *pdev)
  68. {
  69. struct device *dev = &pdev->dev;
  70. const struct of_device_id *of_id;
  71. struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  72. if (!pci)
  73. return -ENOMEM;
  74. of_id = of_match_node(gen_pci_of_match, dev->of_node);
  75. pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
  76. return pci_host_common_probe(pdev, pci);
  77. }
  78. static struct platform_driver gen_pci_driver = {
  79. .driver = {
  80. .name = "pci-host-generic",
  81. .of_match_table = gen_pci_of_match,
  82. },
  83. .probe = gen_pci_probe,
  84. };
  85. module_platform_driver(gen_pci_driver);
  86. MODULE_DESCRIPTION("Generic PCI host driver");
  87. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  88. MODULE_LICENSE("GPL v2");