romcopy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, /* Only for non-DMA */
  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. size_t __sbss romcopy_log[15];
  44. void __hot romcopy_download(void *dst, size_t offset, size_t len)
  45. {
  46. ROMCOPY_RAMADDR = (size_t)dst;
  47. ROMCOPY_ROMCMD = __rom_offset + offset + (ROM_FAST_READ_DUAL << 24);
  48. ROMCOPY_DATALEN =
  49. len | ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_DUAL | ROMCOPY_WRITE_RAM;
  50. }
  51. void __hot romcopy_bzero(void *dst, size_t len)
  52. {
  53. ROMCOPY_RAMADDR = (size_t)dst;
  54. ROMCOPY_ROMCMD = 0;
  55. ROMCOPY_DATALEN =
  56. len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
  57. }
  58. uint32_t __sbss romcopy_time[2];
  59. IRQHANDLER(romcopy)
  60. {
  61. static __sbss unsigned int romcopy_state;
  62. size_t len;
  63. switch (romcopy_state++) {
  64. case 0:
  65. /* Copy DRAM data */
  66. len = __dram_init_end - __dram_init_start;
  67. romcopy_download(__dram_init_start, 0, len);
  68. break;
  69. case 1:
  70. /* Zero .dram.bss */
  71. len = __dram_bss_end - __dram_bss_start;
  72. romcopy_bzero(__dram_bss_start, len);
  73. break;
  74. default:
  75. mask_irq(ROMCOPY_IRQ);
  76. break;
  77. }
  78. }
  79. /*
  80. * Read unique serial number programmed into ROM. Convert the serial
  81. * number to 12 characters in base36 (technically a wraparound that is
  82. * never going to matter.) It's more fun than plain hex... :)
  83. */
  84. char serial_str[16];
  85. uint64_t rom_get_serial(void)
  86. {
  87. union {
  88. uint32_t l[2];
  89. uint64_t q;
  90. } o;
  91. waitfor(ROMCOPY_IRQ);
  92. ROMCOPY_ROMCMD = ROM_READ_UNIQUE_ID << 24;
  93. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
  94. waitfor(ROMCOPY_IRQ);
  95. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  96. waitfor(ROMCOPY_IRQ);
  97. o.l[1] = ROMCOPY_INPUT;
  98. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
  99. waitfor(ROMCOPY_IRQ);
  100. o.l[0] = ROMCOPY_INPUT;
  101. serial_str[12] = '\0';
  102. uint64_t v = o.q;
  103. for (int i = 11; i >= 0; i--) {
  104. unsigned char c;
  105. unsigned int d = v % 36;
  106. v /= 36;
  107. c = d + '0' + ('A'-'0'-10)*(d >= 10);
  108. serial_str[i] = c;
  109. usbdesc_rom[2+2*i] = c;
  110. }
  111. if ( 1 )
  112. con_printf("ROM serial: %08x-%08x (%s)\n", o.l[1], o.l[0], serial_str);
  113. return o.q;
  114. }