2
0

bootldr.sv 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Top level module for absolutely trivial bootloader for the FPGA
  3. // for the MAX80 project by Per Mårtensson and H. Peter Anvin.
  4. //
  5. // This bootloader simply gives the ESP32 processor control of the
  6. // connections to the FPGA flash.
  7. //
  8. // Sharing JTAG pins (via JTAGEN)
  9. `undef SHARED_JTAG
  10. module bootldr
  11. #(parameter logic [6:1] x_mosfet,
  12. parameter logic [7:0] fpga_ver)
  13. (
  14. // Clock oscillator
  15. input master_clk, // 336 MHz from PLL2
  16. input master_pll_locked, // PLL2 is locked, master_clk is good
  17. output reset_plls, // Reset all PLLs including PLL2
  18. input board_id, // This better match the firmware
  19. // ABC-bus
  20. inout abc_clk, // ABC-bus 3 MHz clock
  21. inout [15:0] abc_a, // ABC address bus
  22. inout [7:0] abc_d, // ABC data bus
  23. output abc_d_oe, // Data bus output enable
  24. inout abc_rst_n, // ABC bus reset strobe
  25. inout abc_cs_n, // ABC card select strobe
  26. inout [4:0] abc_out_n, // OUT, C1-C4 strobe
  27. inout [1:0] abc_inp_n, // INP, STATUS strobe
  28. inout abc_xmemfl_n, // Memory read strobe
  29. inout abc_xmemw800_n, // Memory write strobe (ABC800)
  30. inout abc_xmemw80_n, // Memory write strobe (ABC80)
  31. inout abc_xinpstb_n, // I/O read strobe (ABC800)
  32. inout abc_xoutpstb_n, // I/O write strobe (ABC80)
  33. // The following are inverted versus the bus IF
  34. // the corresponding MOSFETs are installed
  35. inout abc_rdy_x, // RDY = WAIT#
  36. inout abc_resin_x, // System reset request
  37. inout abc_int80_x, // System INT request (ABC80)
  38. inout abc_int800_x, // System INT request (ABC800)
  39. inout abc_nmi_x, // System NMI request (ABC800)
  40. inout abc_xm_x, // System memory override (ABC800)
  41. // Host/device control
  42. output abc_host, // 1 = host, 0 = target
  43. // ABC-bus extension header
  44. // (Note: cannot use an array here because HC and HH are
  45. // input only.)
  46. inout exth_ha,
  47. inout exth_hb,
  48. input exth_hc,
  49. inout exth_hd,
  50. inout exth_he,
  51. inout exth_hf,
  52. inout exth_hg,
  53. input exth_hh,
  54. // SDRAM bus
  55. output sr_clk,
  56. output [1:0] sr_ba, // Bank address
  57. output [12:0] sr_a, // Address within bank
  58. inout [15:0] sr_dq, // Also known as D or IO
  59. output [1:0] sr_dqm, // DQML and DQMH
  60. output sr_cs_n,
  61. output sr_we_n,
  62. output sr_cas_n,
  63. output sr_ras_n,
  64. // SD card
  65. input sd_cd_n,
  66. output sd_cs_n,
  67. output sd_clk,
  68. output sd_di,
  69. input sd_do,
  70. // Serial console (naming is FPGA as DCE)
  71. input tty_txd,
  72. output tty_rxd,
  73. input tty_rts,
  74. output tty_cts,
  75. input tty_dtr,
  76. // SPI flash memory (also configuration)
  77. output flash_cs_n,
  78. output flash_sck,
  79. inout [1:0] flash_io,
  80. // SPI bus (connected to ESP32 so can be bidirectional)
  81. inout spi_clk,
  82. inout spi_miso,
  83. inout spi_mosi,
  84. inout spi_cs_esp_n, // ESP32 IO10
  85. inout spi_cs_flash_n, // ESP32 IO01
  86. // Other ESP32 connections
  87. inout esp_io0, // ESP32 IO00
  88. inout esp_int, // ESP32 IO09
  89. // I2C bus (RTC and external)
  90. inout i2c_scl,
  91. inout i2c_sda,
  92. input rtc_32khz,
  93. input rtc_int_n,
  94. // LEDs
  95. output [2:0] led,
  96. // USB
  97. inout usb_dp,
  98. inout usb_dn,
  99. output usb_pu,
  100. input usb_rx,
  101. input usb_rx_ok,
  102. // HDMI
  103. output [2:0] hdmi_d,
  104. output hdmi_clk,
  105. inout hdmi_scl,
  106. inout hdmi_sda,
  107. inout hdmi_hpd,
  108. // Unconnected pins with pullups, used for randomness
  109. inout [2:0] rngio,
  110. // Various clocks available to the top level as well as internally
  111. output sdram_clk, // 168 MHz SDRAM clock
  112. output sys_clk, // 84 MHz System clock
  113. output flash_clk, // 134 MHz Serial flash ROM clock
  114. output usb_clk, // 48 MHz USB clock
  115. output vid_clk, // 56 MHz Video pixel clock
  116. output vid_hdmiclk // 280 MHz HDMI serializer clock = vid_clk x 5
  117. );
  118. // -----------------------------------------------------------------------
  119. // Unused I/O devices
  120. // -----------------------------------------------------------------------
  121. assign reset_plls = 1'b0;
  122. assign abc_d_oe = 1'b0;
  123. assign abc_host = 1'b0;
  124. assign sr_clk = 1'b0;
  125. assign sr_ba = 2'b0;
  126. assign sr_a = 'b0;
  127. assign sr_dq = 'b0;
  128. assign sr_dqm = 2'b11;
  129. assign sr_cs_n = 1'b1;
  130. assign sr_we_n = 1'b1;
  131. assign sr_cas_n = 1'b1;
  132. assign sr_ras_n = 1'b1;
  133. assign sd_cs_n = 1'b1;
  134. assign sd_clk = 1'b1;
  135. assign sd_di = 1'b1;
  136. assign tty_rxd = 1'b1;
  137. assign tty_cts = 1'b1;
  138. assign i2c_scl = 1'bz;
  139. assign i2c_sda = 1'bz;
  140. assign hdmi_d = 3'b000;
  141. assign hdmi_clk = 1'b0;
  142. assign sdram_clk = 1'b0;
  143. assign sys_clk = 1'b0;
  144. assign flash_clk = 1'b0;
  145. assign usb_clk = 1'b0;
  146. assign vid_clk = 1'b0;
  147. assign vid_hdmiclk = 1'b0;
  148. // -----------------------------------------------------------------------
  149. // PLLs, clocks and reset
  150. // -----------------------------------------------------------------------
  151. wire rst_n = ~master_pll_locked;
  152. // -----------------------------------------------------------------------
  153. // Flashing of LEDs
  154. // -----------------------------------------------------------------------
  155. reg [13:0] led_flash_ctr; // Flash LED @ 2 Hz to show in boot loader
  156. // The negedge of rtc_32khz is more stable than the posedge
  157. always @(negedge rtc_32khz)
  158. led_flash_ctr <= led_flash_ctr + 1'b1;
  159. assign led[0] = flash_io[1];
  160. assign led[1] = ~flash_cs_n;
  161. assign led[2] = led_flash_ctr[13];
  162. // -----------------------------------------------------------------------
  163. // ESP32 <-> flash SPI logic
  164. // -----------------------------------------------------------------------
  165. assign flash_cs_n = spi_cs_flash_n;
  166. assign flash_sck = spi_clk;
  167. assign flash_io[0] = spi_miso;
  168. assign spi_mosi = flash_io[1];
  169. assign spi_int = 1'b0; // Let ESP32 know configuration is done
  170. endmodule