pci.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <pci.h>
  25. #include <asm/io.h>
  26. #include <asm/pci.h>
  27. #ifdef CONFIG_PCI
  28. #undef PCI_ROM_SCAN_VERBOSE
  29. int pci_shadow_rom(pci_dev_t dev, unsigned char *dest)
  30. {
  31. struct pci_controller *hose;
  32. int res = -1;
  33. int i;
  34. u32 rom_addr;
  35. u32 addr_reg;
  36. u32 size;
  37. u16 vendor;
  38. u16 device;
  39. u32 class_code;
  40. hose = pci_bus_to_hose(PCI_BUS(dev));
  41. #if 0
  42. printf("pci_shadow_rom() asked to shadow device %x to %x\n",
  43. dev, (u32)dest);
  44. #endif
  45. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  46. pci_read_config_word(dev, PCI_DEVICE_ID, &device);
  47. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code);
  48. class_code &= 0xffffff00;
  49. class_code >>= 8;
  50. #if 0
  51. printf("PCI Header Vendor %04x device %04x class %06x\n",
  52. vendor, device, class_code);
  53. #endif
  54. /* Enable the rom addess decoder */
  55. pci_write_config_dword(dev, PCI_ROM_ADDRESS, PCI_ROM_ADDRESS_MASK);
  56. pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg);
  57. if (!addr_reg) {
  58. /* register unimplemented */
  59. printf("pci_chadow_rom: device do not seem to have a rom\n");
  60. return -1;
  61. }
  62. size = (~(addr_reg&PCI_ROM_ADDRESS_MASK))+1;
  63. #if 0
  64. printf("ROM is %d bytes\n", size);
  65. #endif
  66. rom_addr = pci_get_rom_window(hose, size);
  67. #if 0
  68. printf("ROM mapped at %x \n", rom_addr);
  69. #endif
  70. pci_write_config_dword(dev, PCI_ROM_ADDRESS,
  71. pci_phys_to_mem(dev, rom_addr)
  72. |PCI_ROM_ADDRESS_ENABLE);
  73. for (i=rom_addr;i<rom_addr+size; i+=512) {
  74. if (readw(i) == 0xaa55) {
  75. u32 pci_data;
  76. #ifdef PCI_ROM_SCAN_VERBOSE
  77. printf("ROM signature found\n");
  78. #endif
  79. pci_data = readw(0x18+i);
  80. pci_data += i;
  81. if (0==memcmp((void*)pci_data, "PCIR", 4)) {
  82. #ifdef PCI_ROM_SCAN_VERBOSE
  83. printf("Fount PCI rom image at offset %d\n", i-rom_addr);
  84. printf("Vendor %04x device %04x class %06x\n",
  85. readw(pci_data+4), readw(pci_data+6),
  86. readl(pci_data+0x0d)&0xffffff);
  87. printf("%s\n",
  88. (readw(pci_data+0x15) &0x80)?
  89. "Last image":"More images follow");
  90. switch (readb(pci_data+0x14)) {
  91. case 0:
  92. printf("X86 code\n");
  93. break;
  94. case 1:
  95. printf("Openfirmware code\n");
  96. break;
  97. case 2:
  98. printf("PARISC code\n");
  99. break;
  100. }
  101. printf("Image size %d\n", readw(pci_data+0x10) * 512);
  102. #endif
  103. /* FixMe: I think we should compare the class code
  104. * bytes as well but I have no reference on the
  105. * exact order of these bytes in the PCI ROM header */
  106. if (readw(pci_data+4) == vendor &&
  107. readw(pci_data+6) == device &&
  108. /* (readl(pci_data+0x0d)&0xffffff) == class_code && */
  109. readb(pci_data+0x14) == 0 /* x86 code image */ ) {
  110. #ifdef PCI_ROM_SCAN_VERBOSE
  111. printf("Suitable ROM image found, copying\n");
  112. #endif
  113. memmove(dest, (void*)rom_addr, readw(pci_data+0x10) * 512);
  114. res = 0;
  115. break;
  116. }
  117. if (readw(pci_data+0x15) &0x80) {
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. #ifdef PCI_ROM_SCAN_VERBOSE
  124. if (res) {
  125. printf("No suitable image found\n");
  126. }
  127. #endif
  128. /* disable PAR register and PCI device ROM address devocer */
  129. pci_remove_rom_window(hose, rom_addr);
  130. pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0);
  131. return res;
  132. }
  133. #endif