pci.c 3.9 KB

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