pci-host-common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/platform_device.h>
  23. #include "../ecam.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. goto out_release_res;
  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. continue;
  46. }
  47. break;
  48. case IORESOURCE_MEM:
  49. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  50. break;
  51. case IORESOURCE_BUS:
  52. *bus_range = res;
  53. default:
  54. continue;
  55. }
  56. }
  57. if (!res_valid) {
  58. dev_err(dev, "non-prefetchable memory resource required\n");
  59. err = -EINVAL;
  60. goto out_release_res;
  61. }
  62. return 0;
  63. out_release_res:
  64. return err;
  65. }
  66. static void gen_pci_unmap_cfg(void *ptr)
  67. {
  68. pci_ecam_free((struct pci_config_window *)ptr);
  69. }
  70. static struct pci_config_window *gen_pci_init(struct device *dev,
  71. struct list_head *resources, struct pci_ecam_ops *ops)
  72. {
  73. int err;
  74. struct resource cfgres;
  75. struct resource *bus_range = NULL;
  76. struct pci_config_window *cfg;
  77. /* Parse our PCI ranges and request their resources */
  78. err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
  79. if (err)
  80. goto err_out;
  81. err = of_address_to_resource(dev->of_node, 0, &cfgres);
  82. if (err) {
  83. dev_err(dev, "missing \"reg\" property\n");
  84. goto err_out;
  85. }
  86. cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
  87. if (IS_ERR(cfg)) {
  88. err = PTR_ERR(cfg);
  89. goto err_out;
  90. }
  91. err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
  92. if (err) {
  93. gen_pci_unmap_cfg(cfg);
  94. goto err_out;
  95. }
  96. return cfg;
  97. err_out:
  98. pci_free_resource_list(resources);
  99. return ERR_PTR(err);
  100. }
  101. int pci_host_common_probe(struct platform_device *pdev,
  102. struct pci_ecam_ops *ops)
  103. {
  104. const char *type;
  105. struct device *dev = &pdev->dev;
  106. struct device_node *np = dev->of_node;
  107. struct pci_bus *bus, *child;
  108. struct pci_config_window *cfg;
  109. struct list_head resources;
  110. type = of_get_property(np, "device_type", NULL);
  111. if (!type || strcmp(type, "pci")) {
  112. dev_err(dev, "invalid \"device_type\" %s\n", type);
  113. return -EINVAL;
  114. }
  115. of_pci_check_probe_only();
  116. /* Parse and map our Configuration Space windows */
  117. INIT_LIST_HEAD(&resources);
  118. cfg = gen_pci_init(dev, &resources, ops);
  119. if (IS_ERR(cfg))
  120. return PTR_ERR(cfg);
  121. /* Do not reassign resources if probe only */
  122. if (!pci_has_flag(PCI_PROBE_ONLY))
  123. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  124. bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
  125. &resources);
  126. if (!bus) {
  127. dev_err(dev, "Scanning rootbus failed");
  128. return -ENODEV;
  129. }
  130. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  131. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  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");