pci-host-common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *
  14. * Copyright (C) 2014 ARM Limited
  15. *
  16. * Author: Will Deacon <will.deacon@arm.com>
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/pci-ecam.h>
  23. #include <linux/platform_device.h>
  24. static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
  25. struct list_head *resources, struct resource **bus_range)
  26. {
  27. int err, res_valid = 0;
  28. struct device_node *np = dev->of_node;
  29. resource_size_t iobase;
  30. struct resource_entry *win;
  31. err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
  32. if (err)
  33. return err;
  34. err = devm_request_pci_bus_resources(dev, resources);
  35. if (err)
  36. return err;
  37. resource_list_for_each_entry(win, resources) {
  38. struct resource *res = win->res;
  39. switch (resource_type(res)) {
  40. case IORESOURCE_IO:
  41. err = pci_remap_iospace(res, iobase);
  42. if (err)
  43. dev_warn(dev, "error %d: failed to map resource %pR\n",
  44. err, res);
  45. break;
  46. case IORESOURCE_MEM:
  47. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  48. break;
  49. case IORESOURCE_BUS:
  50. *bus_range = res;
  51. break;
  52. }
  53. }
  54. if (res_valid)
  55. return 0;
  56. dev_err(dev, "non-prefetchable memory resource required\n");
  57. return -EINVAL;
  58. }
  59. static void gen_pci_unmap_cfg(void *ptr)
  60. {
  61. pci_ecam_free((struct pci_config_window *)ptr);
  62. }
  63. static struct pci_config_window *gen_pci_init(struct device *dev,
  64. struct list_head *resources, struct pci_ecam_ops *ops)
  65. {
  66. int err;
  67. struct resource cfgres;
  68. struct resource *bus_range = NULL;
  69. struct pci_config_window *cfg;
  70. /* Parse our PCI ranges and request their resources */
  71. err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
  72. if (err)
  73. goto err_out;
  74. err = of_address_to_resource(dev->of_node, 0, &cfgres);
  75. if (err) {
  76. dev_err(dev, "missing \"reg\" property\n");
  77. goto err_out;
  78. }
  79. cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
  80. if (IS_ERR(cfg)) {
  81. err = PTR_ERR(cfg);
  82. goto err_out;
  83. }
  84. err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
  85. if (err) {
  86. gen_pci_unmap_cfg(cfg);
  87. goto err_out;
  88. }
  89. return cfg;
  90. err_out:
  91. pci_free_resource_list(resources);
  92. return ERR_PTR(err);
  93. }
  94. int pci_host_common_probe(struct platform_device *pdev,
  95. struct pci_ecam_ops *ops)
  96. {
  97. const char *type;
  98. struct device *dev = &pdev->dev;
  99. struct device_node *np = dev->of_node;
  100. struct pci_bus *bus, *child;
  101. struct pci_config_window *cfg;
  102. struct list_head resources;
  103. type = of_get_property(np, "device_type", NULL);
  104. if (!type || strcmp(type, "pci")) {
  105. dev_err(dev, "invalid \"device_type\" %s\n", type);
  106. return -EINVAL;
  107. }
  108. of_pci_check_probe_only();
  109. /* Parse and map our Configuration Space windows */
  110. INIT_LIST_HEAD(&resources);
  111. cfg = gen_pci_init(dev, &resources, ops);
  112. if (IS_ERR(cfg))
  113. return PTR_ERR(cfg);
  114. /* Do not reassign resources if probe only */
  115. if (!pci_has_flag(PCI_PROBE_ONLY))
  116. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  117. bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
  118. &resources);
  119. if (!bus) {
  120. dev_err(dev, "Scanning rootbus failed");
  121. return -ENODEV;
  122. }
  123. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  124. /*
  125. * We insert PCI resources into the iomem_resource and
  126. * ioport_resource trees in either pci_bus_claim_resources()
  127. * or pci_bus_assign_resources().
  128. */
  129. if (pci_has_flag(PCI_PROBE_ONLY)) {
  130. pci_bus_claim_resources(bus);
  131. } else {
  132. pci_bus_size_bridges(bus);
  133. pci_bus_assign_resources(bus);
  134. list_for_each_entry(child, &bus->children, node)
  135. pcie_bus_configure_settings(child);
  136. }
  137. pci_bus_add_devices(bus);
  138. return 0;
  139. }
  140. MODULE_DESCRIPTION("Generic PCI host driver common code");
  141. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  142. MODULE_LICENSE("GPL v2");