romcopy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Convert the serial
  66. * number to 12 characters in base32; we lose four bits but that is never
  67. * going to matter. It's more fun than plain hex... :)
  68. *
  69. * This is run before the 64-bit division routines are available in SDRAM,
  70. * so it needs to be an encoding that can be generated with only plain
  71. * 32-bit instructions.
  72. *
  73. * Doing this as early as possible means a much better chance to see
  74. * the proper serial number during USB enumeration, so doing it
  75. * immediately after SPI ROM conditioning is a great time.
  76. */
  77. char __bss_hot rom_serial_str[16];
  78. qword_t __bss_hot rom_serial;
  79. static __must_inline void rom_read_serial(void)
  80. {
  81. ROMCOPY_ROMCMD = ROM_READ_UNIQUE_ID << 24;
  82. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
  83. waitfor(ROMCOPY_IRQ);
  84. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  85. waitfor(ROMCOPY_IRQ);
  86. rom_serial.l[1] = ROMCOPY_INPUT;
  87. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
  88. waitfor(ROMCOPY_IRQ);
  89. rom_serial.l[0] = ROMCOPY_INPUT;
  90. }
  91. static __must_inline void rom_mangle_serial(void)
  92. {
  93. rom_serial_str[12] = '\0';
  94. uint64_t v = rom_serial.q;
  95. for (int i = 11; i >= 0; i--) {
  96. unsigned char c;
  97. unsigned int d = v & 31;
  98. v >>= 5;
  99. c = d + '0';
  100. if (c > '9')
  101. c += 'A'-'9'-1;
  102. if (c >= 'I')
  103. c++;
  104. if (c >= 'O')
  105. c++;
  106. if (c >= 'S')
  107. c++;
  108. /* IOSZ not used to avoid confusion with 1052 */
  109. rom_serial_str[i] = c;
  110. usbdesc_rom[2+2*i] = c;
  111. }
  112. }
  113. void rom_print_serial(void)
  114. {
  115. /* Print the ROM serial when we actually can */
  116. con_printf("ROM serial: %08x-%08x (%s)\n",
  117. rom_serial.l[1], rom_serial.l[0], rom_serial_str);
  118. }
  119. static __must_inline void romcopy_config_flash(void)
  120. {
  121. /* Enable writing volatile status register bits */
  122. ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
  123. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
  124. waitfor(ROMCOPY_IRQ);
  125. /* Write SR3 = 0; this sets the drive strength to maximum */
  126. ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
  127. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
  128. waitfor(ROMCOPY_IRQ);
  129. asm volatile("nop; nop"); /* Make extra sure tSHSL2 is OK */
  130. }
  131. IRQHANDLER(romcopy,0)
  132. {
  133. static __sbss unsigned int romcopy_state;
  134. size_t len;
  135. switch (romcopy_state++) {
  136. case 0:
  137. /* Condition flash ROM */
  138. romcopy_config_flash();
  139. /* Read serial number */
  140. rom_read_serial();
  141. /* Start copy DRAM data */
  142. len = __dram_init_end - __dram_init_start;
  143. romcopy_download(__dram_init_start, 0, len);
  144. /* Convert serial number and export to USB */
  145. rom_mangle_serial();
  146. break;
  147. case 1:
  148. /* Zero .dram.bss */
  149. len = __dram_bss_end - __dram_bss_start;
  150. romcopy_bzero(__dram_bss_start, len);
  151. break;
  152. default:
  153. mask_irq(ROMCOPY_IRQ);
  154. break;
  155. }
  156. }