pci.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <pci.h>
  8. #include <pci_rom.h>
  9. #include <asm/pci.h>
  10. #include <asm/arch/device.h>
  11. #include <asm/arch/qemu.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. static bool i440fx;
  14. void board_pci_setup_hose(struct pci_controller *hose)
  15. {
  16. hose->first_busno = 0;
  17. hose->last_busno = 0;
  18. /* PCI memory space */
  19. pci_set_region(hose->regions + 0,
  20. CONFIG_PCI_MEM_BUS,
  21. CONFIG_PCI_MEM_PHYS,
  22. CONFIG_PCI_MEM_SIZE,
  23. PCI_REGION_MEM);
  24. /* PCI IO space */
  25. pci_set_region(hose->regions + 1,
  26. CONFIG_PCI_IO_BUS,
  27. CONFIG_PCI_IO_PHYS,
  28. CONFIG_PCI_IO_SIZE,
  29. PCI_REGION_IO);
  30. pci_set_region(hose->regions + 2,
  31. CONFIG_PCI_PREF_BUS,
  32. CONFIG_PCI_PREF_PHYS,
  33. CONFIG_PCI_PREF_SIZE,
  34. PCI_REGION_PREFETCH);
  35. pci_set_region(hose->regions + 3,
  36. 0,
  37. 0,
  38. gd->ram_size,
  39. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  40. hose->region_count = 4;
  41. }
  42. int board_pci_post_scan(struct pci_controller *hose)
  43. {
  44. int ret = 0;
  45. u16 device, xbcs;
  46. int pam, i;
  47. pci_dev_t vga;
  48. ulong start;
  49. /*
  50. * i440FX and Q35 chipset have different PAM register offset, but with
  51. * the same bitfield layout. Here we determine the offset based on its
  52. * PCI device ID.
  53. */
  54. device = x86_pci_read_config16(PCI_BDF(0, 0, 0), PCI_DEVICE_ID);
  55. i440fx = (device == PCI_DEVICE_ID_INTEL_82441);
  56. pam = i440fx ? I440FX_PAM : Q35_PAM;
  57. /*
  58. * Initialize Programmable Attribute Map (PAM) Registers
  59. *
  60. * Configure legacy segments C/D/E/F to system RAM
  61. */
  62. for (i = 0; i < PAM_NUM; i++)
  63. x86_pci_write_config8(PCI_BDF(0, 0, 0), pam + i, PAM_RW);
  64. if (i440fx) {
  65. /*
  66. * Enable legacy IDE I/O ports decode
  67. *
  68. * Note: QEMU always decode legacy IDE I/O port on PIIX chipset.
  69. * However Linux ata_piix driver does sanity check on these two
  70. * registers to see whether legacy ports decode is turned on.
  71. * This is to make Linux ata_piix driver happy.
  72. */
  73. x86_pci_write_config16(PIIX_IDE, IDE0_TIM, IDE_DECODE_EN);
  74. x86_pci_write_config16(PIIX_IDE, IDE1_TIM, IDE_DECODE_EN);
  75. /* Enable I/O APIC */
  76. xbcs = x86_pci_read_config16(PIIX_ISA, XBCS);
  77. xbcs |= APIC_EN;
  78. x86_pci_write_config16(PIIX_ISA, XBCS, xbcs);
  79. } else {
  80. /* Configure PCIe ECAM base address */
  81. x86_pci_write_config32(PCI_BDF(0, 0, 0), PCIEX_BAR,
  82. CONFIG_PCIE_ECAM_BASE | BAR_EN);
  83. }
  84. /*
  85. * QEMU emulated graphic card shows in the PCI configuration space with
  86. * PCI vendor id and device id as an artificial pair 0x1234:0x1111.
  87. * It is on PCI bus 0, function 0, but device number is not consistent
  88. * for the two x86 targets it supports. For i440FX and PIIX chipset
  89. * board, it shows as device 2, while for Q35 and ICH9 chipset board,
  90. * it shows as device 1.
  91. */
  92. vga = i440fx ? I440FX_VGA : Q35_VGA;
  93. start = get_timer(0);
  94. ret = pci_run_vga_bios(vga, NULL, PCI_ROM_USE_NATIVE);
  95. debug("BIOS ran in %lums\n", get_timer(start));
  96. return ret;
  97. }
  98. #ifdef CONFIG_GENERATE_MP_TABLE
  99. int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
  100. {
  101. u8 irq;
  102. if (i440fx) {
  103. /*
  104. * Not like most x86 platforms, the PIRQ[A-D] on PIIX3 are not
  105. * connected to I/O APIC INTPIN#16-19. Instead they are routed
  106. * to an irq number controled by the PIRQ routing register.
  107. */
  108. irq = x86_pci_read_config8(PCI_BDF(bus, dev, func),
  109. PCI_INTERRUPT_LINE);
  110. } else {
  111. /*
  112. * ICH9's PIRQ[A-H] are not consecutive numbers from 0 to 7.
  113. * PIRQ[A-D] still maps to [0-3] but PIRQ[E-H] maps to [8-11].
  114. */
  115. irq = pirq < 8 ? pirq + 16 : pirq + 12;
  116. }
  117. return irq;
  118. }
  119. #endif