pci_auto_common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * PCI auto-configuration library
  3. *
  4. * Author: Matt Porter <mporter@mvista.com>
  5. *
  6. * Copyright 2000 MontaVista Software Inc.
  7. *
  8. * Modifications for driver model:
  9. * Copyright 2015 Google, Inc
  10. * Written by Simon Glass <sjg@chromium.org>
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <errno.h>
  17. #include <pci.h>
  18. void pciauto_region_init(struct pci_region *res)
  19. {
  20. /*
  21. * Avoid allocating PCI resources from address 0 -- this is illegal
  22. * according to PCI 2.1 and moreover, this is known to cause Linux IDE
  23. * drivers to fail. Use a reasonable starting value of 0x1000 instead.
  24. */
  25. res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
  26. }
  27. void pciauto_region_align(struct pci_region *res, pci_size_t size)
  28. {
  29. res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
  30. }
  31. int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
  32. pci_addr_t *bar)
  33. {
  34. pci_addr_t addr;
  35. if (!res) {
  36. debug("No resource");
  37. goto error;
  38. }
  39. addr = ((res->bus_lower - 1) | (size - 1)) + 1;
  40. if (addr - res->bus_start + size > res->size) {
  41. debug("No room in resource");
  42. goto error;
  43. }
  44. res->bus_lower = addr + size;
  45. debug("address=0x%llx bus_lower=0x%llx", (unsigned long long)addr,
  46. (unsigned long long)res->bus_lower);
  47. *bar = addr;
  48. return 0;
  49. error:
  50. *bar = (pci_addr_t)-1;
  51. return -1;
  52. }
  53. void pciauto_config_init(struct pci_controller *hose)
  54. {
  55. int i;
  56. hose->pci_io = NULL;
  57. hose->pci_mem = NULL;
  58. hose->pci_prefetch = NULL;
  59. for (i = 0; i < hose->region_count; i++) {
  60. switch (hose->regions[i].flags) {
  61. case PCI_REGION_IO:
  62. if (!hose->pci_io ||
  63. hose->pci_io->size < hose->regions[i].size)
  64. hose->pci_io = hose->regions + i;
  65. break;
  66. case PCI_REGION_MEM:
  67. if (!hose->pci_mem ||
  68. hose->pci_mem->size < hose->regions[i].size)
  69. hose->pci_mem = hose->regions + i;
  70. break;
  71. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  72. if (!hose->pci_prefetch ||
  73. hose->pci_prefetch->size < hose->regions[i].size)
  74. hose->pci_prefetch = hose->regions + i;
  75. break;
  76. }
  77. }
  78. if (hose->pci_mem) {
  79. pciauto_region_init(hose->pci_mem);
  80. debug("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
  81. "\t\tPhysical Memory [%llx-%llxx]\n",
  82. (u64)hose->pci_mem->bus_start,
  83. (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
  84. (u64)hose->pci_mem->phys_start,
  85. (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
  86. }
  87. if (hose->pci_prefetch) {
  88. pciauto_region_init(hose->pci_prefetch);
  89. debug("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
  90. "\t\tPhysical Memory [%llx-%llx]\n",
  91. (u64)hose->pci_prefetch->bus_start,
  92. (u64)(hose->pci_prefetch->bus_start +
  93. hose->pci_prefetch->size - 1),
  94. (u64)hose->pci_prefetch->phys_start,
  95. (u64)(hose->pci_prefetch->phys_start +
  96. hose->pci_prefetch->size - 1));
  97. }
  98. if (hose->pci_io) {
  99. pciauto_region_init(hose->pci_io);
  100. debug("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
  101. "\t\tPhysical Memory: [%llx-%llx]\n",
  102. (u64)hose->pci_io->bus_start,
  103. (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
  104. (u64)hose->pci_io->phys_start,
  105. (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
  106. }
  107. }