romcopy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "fw.h"
  2. #include "console.h"
  3. #include "io.h"
  4. extern char __dram_init_start[], __dram_init_end[];
  5. extern char __dram_bss_start[], __dram_bss_end[];
  6. enum romcmd {
  7. ROM_WRITE_ENABLE = 0x06,
  8. ROM_VOLATILE_SR_WRITE_ENABLE = 0x50,
  9. ROM_WRITE_DISABLE = 0x04,
  10. ROM_RELEASE_POWERDOWN_ID = 0xab,
  11. ROM_MANUFACTURER_DEVICE_ID = 0x90,
  12. ROM_JEDEC_ID = 0x9f,
  13. ROM_READ_UNIQUE_ID = 0x4b,
  14. ROM_READ_DATA = 0x03, /* DO NOT USE */
  15. ROM_FAST_READ = 0x0b,
  16. ROM_PAGE_PROGRAM = 0x02,
  17. ROM_ERASE_4K = 0x20,
  18. ROM_ERASE_32K = 0x52,
  19. ROM_ERASE_64K = 0xd8,
  20. ROM_ERASE_ALL = 0xc7,
  21. ROM_READ_SR1 = 0x05,
  22. ROM_WRITE_SR1 = 0x01,
  23. ROM_READ_SR2 = 0x35,
  24. ROM_WRITE_SR2 = 0x31,
  25. ROM_READ_SR3 = 0x15,
  26. ROM_WRITE_SR3 = 0x11,
  27. ROM_READ_SFPD = 0x5a,
  28. ROM_ERASE_SECURITY = 0x44,
  29. ROM_PROGRAM_SECURITY = 0x42,
  30. ROM_READ_SECURITY = 0x48,
  31. ROM_GLOBAL_BLOCK_LOCK = 0x7e,
  32. ROM_GLOBAL_BLOCK_UNLOCK = 0x98,
  33. ROM_READ_BLOCK_LOCK = 0x3d,
  34. ROM_ONE_BLOCK_LOCK = 0x36,
  35. ROM_ONE_BLOCK_UNLOCK = 0x39,
  36. ROM_ERASE_PROGRAM_SUSPEND = 0x75,
  37. ROM_ERASE_PROGRAM_RESUME = 0x7a,
  38. ROM_POWER_DOWN = 0xb9,
  39. ROM_ENABLE_RESET = 0x66,
  40. ROM_RESET = 0x99,
  41. ROM_FAST_READ_DUAL = 0x3b
  42. };
  43. #define SPIROM_DUAL_MODE 1
  44. void __hot romcopy_download(void *dst, size_t offset, size_t len)
  45. {
  46. unsigned int cmd;
  47. unsigned int flags = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_WRITE_RAM;
  48. if (SPIROM_DUAL_MODE) {
  49. cmd = ROM_FAST_READ_DUAL;
  50. flags |= ROMCOPY_SPI_DUAL;
  51. } else {
  52. cmd = ROM_FAST_READ;
  53. }
  54. ROMCOPY_RAMADDR = (size_t)dst;
  55. ROMCOPY_ROMCMD = __rom_offset + offset + (cmd << 24);
  56. ROMCOPY_DATALEN = len | flags;
  57. }
  58. void __hot romcopy_bzero(void *dst, size_t len)
  59. {
  60. ROMCOPY_RAMADDR = (size_t)dst;
  61. ROMCOPY_ROMCMD = 0;
  62. ROMCOPY_DATALEN = len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
  63. }
  64. /*
  65. * Read unique serial number programmed into ROM
  66. *
  67. */
  68. char __bss_hot rom_serial_str[16];
  69. qword_t __bss_hot rom_serial;
  70. static __must_inline void rom_read_serial(void)
  71. {
  72. ROMCOPY_ROMCMD = ROM_READ_UNIQUE_ID << 24;
  73. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
  74. waitfor(ROMCOPY_IRQ);
  75. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  76. waitfor(ROMCOPY_IRQ);
  77. rom_serial.l[1] = ROMCOPY_INPUT;
  78. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
  79. waitfor(ROMCOPY_IRQ);
  80. rom_serial.l[0] = ROMCOPY_INPUT;
  81. }
  82. /*
  83. * Convert the ROM serial number to a hex string and poke it into the
  84. * USB descriptor "ROM". Used to use base36, but this has to be done
  85. * very early, and it has turned out that making it consistent with
  86. * what one can easily get out of a debugger is really useful.
  87. *
  88. * Doing this as early as possible means a much better chance to see
  89. * the proper serial number during USB enumeration, so doing it
  90. * immediately after SPI ROM conditioning is a great time.
  91. */
  92. static __must_inline void rom_mangle_serial(void)
  93. {
  94. volatile uint32_t *udp = &usbdesc_rom[2];
  95. for (int i = 7; i >= 0; i--) {
  96. unsigned int v = rom_serial.b[i];
  97. unsigned int c;
  98. c = (v >> 4)+'0';
  99. if (c > '9')
  100. c += 'A'-'9'-1;
  101. udp[0] = c;
  102. c = (v & 15)+'0';
  103. if (c > '9')
  104. c += 'A'-'9'-1;
  105. udp[2] = c;
  106. udp += 4;
  107. }
  108. }
  109. void rom_print_serial(void)
  110. {
  111. /* Print the ROM serial when we actually can */
  112. con_printf("ROM serial: %08X%08X (%08x-%08x)\n",
  113. rom_serial.l[1], rom_serial.l[0],
  114. rom_serial.l[1], rom_serial.l[0]);
  115. }
  116. static __must_inline void romcopy_config_flash(void)
  117. {
  118. /* Enable writing volatile status register bits */
  119. ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
  120. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
  121. waitfor(ROMCOPY_IRQ);
  122. /* Write SR3 = 0; this sets the drive strength to maximum */
  123. ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
  124. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
  125. waitfor(ROMCOPY_IRQ);
  126. }
  127. IRQHANDLER(romcopy,0)
  128. {
  129. static __sbss unsigned int romcopy_state;
  130. size_t len;
  131. switch (romcopy_state++) {
  132. case 0:
  133. /* Condition flash ROM */
  134. romcopy_config_flash();
  135. /* Read serial number */
  136. rom_read_serial();
  137. /* Start copy DRAM data */
  138. len = __dram_init_end - __dram_init_start;
  139. romcopy_download(__dram_init_start, 0, len);
  140. /* Convert serial number and export to USB */
  141. rom_mangle_serial();
  142. break;
  143. case 1:
  144. /* Zero .dram.bss */
  145. len = __dram_bss_end - __dram_bss_start;
  146. romcopy_bzero(__dram_bss_start, len);
  147. break;
  148. default:
  149. mask_irq(ROMCOPY_IRQ);
  150. break;
  151. }
  152. }