pci-host-common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "pci-host-common.h"
  24. static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
  25. {
  26. pci_free_resource_list(&pci->resources);
  27. }
  28. static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
  29. {
  30. int err, res_valid = 0;
  31. struct device *dev = pci->host.dev.parent;
  32. struct device_node *np = dev->of_node;
  33. resource_size_t iobase;
  34. struct resource_entry *win;
  35. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
  36. &iobase);
  37. if (err)
  38. return err;
  39. resource_list_for_each_entry(win, &pci->resources) {
  40. struct resource *parent, *res = win->res;
  41. switch (resource_type(res)) {
  42. case IORESOURCE_IO:
  43. parent = &ioport_resource;
  44. err = pci_remap_iospace(res, iobase);
  45. if (err) {
  46. dev_warn(dev, "error %d: failed to map resource %pR\n",
  47. err, res);
  48. continue;
  49. }
  50. break;
  51. case IORESOURCE_MEM:
  52. parent = &iomem_resource;
  53. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  54. break;
  55. case IORESOURCE_BUS:
  56. pci->cfg.bus_range = res;
  57. default:
  58. continue;
  59. }
  60. err = devm_request_resource(dev, parent, res);
  61. if (err)
  62. goto out_release_res;
  63. }
  64. if (!res_valid) {
  65. dev_err(dev, "non-prefetchable memory resource required\n");
  66. err = -EINVAL;
  67. goto out_release_res;
  68. }
  69. return 0;
  70. out_release_res:
  71. gen_pci_release_of_pci_ranges(pci);
  72. return err;
  73. }
  74. static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
  75. {
  76. int err;
  77. u8 bus_max;
  78. resource_size_t busn;
  79. struct resource *bus_range;
  80. struct device *dev = pci->host.dev.parent;
  81. struct device_node *np = dev->of_node;
  82. u32 sz = 1 << pci->cfg.ops->bus_shift;
  83. err = of_address_to_resource(np, 0, &pci->cfg.res);
  84. if (err) {
  85. dev_err(dev, "missing \"reg\" property\n");
  86. return err;
  87. }
  88. /* Limit the bus-range to fit within reg */
  89. bus_max = pci->cfg.bus_range->start +
  90. (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
  91. pci->cfg.bus_range->end = min_t(resource_size_t,
  92. pci->cfg.bus_range->end, bus_max);
  93. pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
  94. sizeof(*pci->cfg.win), GFP_KERNEL);
  95. if (!pci->cfg.win)
  96. return -ENOMEM;
  97. /* Map our Configuration Space windows */
  98. if (!devm_request_mem_region(dev, pci->cfg.res.start,
  99. resource_size(&pci->cfg.res),
  100. "Configuration Space"))
  101. return -ENOMEM;
  102. bus_range = pci->cfg.bus_range;
  103. for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
  104. u32 idx = busn - bus_range->start;
  105. pci->cfg.win[idx] = devm_ioremap(dev,
  106. pci->cfg.res.start + idx * sz,
  107. sz);
  108. if (!pci->cfg.win[idx])
  109. return -ENOMEM;
  110. }
  111. return 0;
  112. }
  113. int pci_host_common_probe(struct platform_device *pdev,
  114. struct gen_pci *pci)
  115. {
  116. int err;
  117. const char *type;
  118. struct device *dev = &pdev->dev;
  119. struct device_node *np = dev->of_node;
  120. struct pci_bus *bus, *child;
  121. type = of_get_property(np, "device_type", NULL);
  122. if (!type || strcmp(type, "pci")) {
  123. dev_err(dev, "invalid \"device_type\" %s\n", type);
  124. return -EINVAL;
  125. }
  126. of_pci_check_probe_only();
  127. pci->host.dev.parent = dev;
  128. INIT_LIST_HEAD(&pci->host.windows);
  129. INIT_LIST_HEAD(&pci->resources);
  130. /* Parse our PCI ranges and request their resources */
  131. err = gen_pci_parse_request_of_pci_ranges(pci);
  132. if (err)
  133. return err;
  134. /* Parse and map our Configuration Space windows */
  135. err = gen_pci_parse_map_cfg_windows(pci);
  136. if (err) {
  137. gen_pci_release_of_pci_ranges(pci);
  138. return err;
  139. }
  140. /* Do not reassign resources if probe only */
  141. if (!pci_has_flag(PCI_PROBE_ONLY))
  142. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  143. bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start,
  144. &pci->cfg.ops->ops, pci, &pci->resources);
  145. if (!bus) {
  146. dev_err(dev, "Scanning rootbus failed");
  147. return -ENODEV;
  148. }
  149. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  150. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  151. pci_bus_size_bridges(bus);
  152. pci_bus_assign_resources(bus);
  153. list_for_each_entry(child, &bus->children, node)
  154. pcie_bus_configure_settings(child);
  155. }
  156. pci_bus_add_devices(bus);
  157. return 0;
  158. }
  159. MODULE_DESCRIPTION("Generic PCI host driver common code");
  160. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  161. MODULE_LICENSE("GPL v2");