pci-host-common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. resource_list_for_each_entry(win, resources) {
  35. struct resource *parent, *res = win->res;
  36. switch (resource_type(res)) {
  37. case IORESOURCE_IO:
  38. parent = &ioport_resource;
  39. err = pci_remap_iospace(res, iobase);
  40. if (err) {
  41. dev_warn(dev, "error %d: failed to map resource %pR\n",
  42. err, res);
  43. continue;
  44. }
  45. break;
  46. case IORESOURCE_MEM:
  47. parent = &iomem_resource;
  48. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  49. break;
  50. case IORESOURCE_BUS:
  51. *bus_range = res;
  52. default:
  53. continue;
  54. }
  55. err = devm_request_resource(dev, parent, res);
  56. if (err)
  57. goto out_release_res;
  58. }
  59. if (!res_valid) {
  60. dev_err(dev, "non-prefetchable memory resource required\n");
  61. err = -EINVAL;
  62. goto out_release_res;
  63. }
  64. return 0;
  65. out_release_res:
  66. return err;
  67. }
  68. static void gen_pci_unmap_cfg(void *ptr)
  69. {
  70. pci_ecam_free((struct pci_config_window *)ptr);
  71. }
  72. static struct pci_config_window *gen_pci_init(struct device *dev,
  73. struct list_head *resources, struct pci_ecam_ops *ops)
  74. {
  75. int err;
  76. struct resource cfgres;
  77. struct resource *bus_range = NULL;
  78. struct pci_config_window *cfg;
  79. /* Parse our PCI ranges and request their resources */
  80. err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
  81. if (err)
  82. goto err_out;
  83. err = of_address_to_resource(dev->of_node, 0, &cfgres);
  84. if (err) {
  85. dev_err(dev, "missing \"reg\" property\n");
  86. goto err_out;
  87. }
  88. cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
  89. if (IS_ERR(cfg)) {
  90. err = PTR_ERR(cfg);
  91. goto err_out;
  92. }
  93. err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
  94. if (err) {
  95. gen_pci_unmap_cfg(cfg);
  96. goto err_out;
  97. }
  98. return cfg;
  99. err_out:
  100. pci_free_resource_list(resources);
  101. return ERR_PTR(err);
  102. }
  103. int pci_host_common_probe(struct platform_device *pdev,
  104. struct pci_ecam_ops *ops)
  105. {
  106. const char *type;
  107. struct device *dev = &pdev->dev;
  108. struct device_node *np = dev->of_node;
  109. struct pci_bus *bus, *child;
  110. struct pci_config_window *cfg;
  111. struct list_head resources;
  112. type = of_get_property(np, "device_type", NULL);
  113. if (!type || strcmp(type, "pci")) {
  114. dev_err(dev, "invalid \"device_type\" %s\n", type);
  115. return -EINVAL;
  116. }
  117. of_pci_check_probe_only();
  118. /* Parse and map our Configuration Space windows */
  119. INIT_LIST_HEAD(&resources);
  120. cfg = gen_pci_init(dev, &resources, ops);
  121. if (IS_ERR(cfg))
  122. return PTR_ERR(cfg);
  123. /* Do not reassign resources if probe only */
  124. if (!pci_has_flag(PCI_PROBE_ONLY))
  125. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  126. bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
  127. &resources);
  128. if (!bus) {
  129. dev_err(dev, "Scanning rootbus failed");
  130. return -ENODEV;
  131. }
  132. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  133. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  134. pci_bus_size_bridges(bus);
  135. pci_bus_assign_resources(bus);
  136. list_for_each_entry(child, &bus->children, node)
  137. pcie_bus_configure_settings(child);
  138. }
  139. pci_bus_add_devices(bus);
  140. return 0;
  141. }
  142. MODULE_DESCRIPTION("Generic PCI host driver common code");
  143. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  144. MODULE_LICENSE("GPL v2");