pci.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #undef PCI_ROM_SCAN_VERBOSE
  28. int pci_shadow_rom(pci_dev_t dev, unsigned char *dest)
  29. {
  30. struct pci_controller *hose;
  31. int res = -1;
  32. int i;
  33. u32 rom_addr;
  34. u32 addr_reg;
  35. u32 size;
  36. u16 vendor;
  37. u16 device;
  38. u32 class_code;
  39. hose = pci_bus_to_hose(PCI_BUS(dev));
  40. #if 0
  41. printf("pci_shadow_rom() asked to shadow device %x to %x\n",
  42. dev, (u32)dest);
  43. #endif
  44. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  45. pci_read_config_word(dev, PCI_DEVICE_ID, &device);
  46. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code);
  47. class_code &= 0xffffff00;
  48. class_code >>= 8;
  49. #if 0
  50. printf("PCI Header Vendor %04x device %04x class %06x\n",
  51. vendor, device, class_code);
  52. #endif
  53. /* Enable the rom addess decoder */
  54. pci_write_config_dword(dev, PCI_ROM_ADDRESS, (u32)PCI_ROM_ADDRESS_MASK);
  55. pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg);
  56. if (!addr_reg) {
  57. /* register unimplemented */
  58. printf("pci_chadow_rom: device do not seem to have a rom\n");
  59. return -1;
  60. }
  61. size = (~(addr_reg&PCI_ROM_ADDRESS_MASK))+1;
  62. #if 0
  63. printf("ROM is %d bytes\n", size);
  64. #endif
  65. rom_addr = pci_get_rom_window(hose, size);
  66. #if 0
  67. printf("ROM mapped at %x \n", rom_addr);
  68. #endif
  69. pci_write_config_dword(dev, PCI_ROM_ADDRESS,
  70. pci_phys_to_mem(dev, rom_addr)
  71. |PCI_ROM_ADDRESS_ENABLE);
  72. for (i=rom_addr;i<rom_addr+size; i+=512) {
  73. if (readw(i) == 0xaa55) {
  74. u32 pci_data;
  75. #ifdef PCI_ROM_SCAN_VERBOSE
  76. printf("ROM signature found\n");
  77. #endif
  78. pci_data = readw(0x18+i);
  79. pci_data += i;
  80. if (0==memcmp((void*)pci_data, "PCIR", 4)) {
  81. #ifdef PCI_ROM_SCAN_VERBOSE
  82. printf("Fount PCI rom image at offset %d\n", i-rom_addr);
  83. printf("Vendor %04x device %04x class %06x\n",
  84. readw(pci_data+4), readw(pci_data+6),
  85. readl(pci_data+0x0d)&0xffffff);
  86. printf("%s\n",
  87. (readw(pci_data+0x15) &0x80)?
  88. "Last image":"More images follow");
  89. switch (readb(pci_data+0x14)) {
  90. case 0:
  91. printf("X86 code\n");
  92. break;
  93. case 1:
  94. printf("Openfirmware code\n");
  95. break;
  96. case 2:
  97. printf("PARISC code\n");
  98. break;
  99. }
  100. printf("Image size %d\n", readw(pci_data+0x10) * 512);
  101. #endif
  102. /* FixMe: I think we should compare the class code
  103. * bytes as well but I have no reference on the
  104. * exact order of these bytes in the PCI ROM header */
  105. if (readw(pci_data+4) == vendor &&
  106. readw(pci_data+6) == device &&
  107. /* (readl(pci_data+0x0d)&0xffffff) == class_code && */
  108. readb(pci_data+0x14) == 0 /* x86 code image */ ) {
  109. #ifdef PCI_ROM_SCAN_VERBOSE
  110. printf("Suitable ROM image found, copying\n");
  111. #endif
  112. memmove(dest, (void*)rom_addr, readw(pci_data+0x10) * 512);
  113. res = 0;
  114. break;
  115. }
  116. if (readw(pci_data+0x15) &0x80) {
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. #ifdef PCI_ROM_SCAN_VERBOSE
  123. if (res) {
  124. printf("No suitable image found\n");
  125. }
  126. #endif
  127. /* disable PAR register and PCI device ROM address devocer */
  128. pci_remove_rom_window(hose, rom_addr);
  129. pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0);
  130. return res;
  131. }