spirom.sv 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // Fast data download from 2-bit SPI flash, or zero SDRAM.
  3. //
  4. // Feed a FIFO that then writes to SDRAM.
  5. // Requires writes in aligned 8-byte chunks.
  6. //
  7. // This unit does *not* require a 2x SPI clock;
  8. // it uses a DDR buffer for clock out.
  9. //
  10. module spirom (
  11. input rst_n,
  12. input rom_clk,
  13. input ram_clk,
  14. input sys_clk,
  15. /* SPI ROM interface */
  16. output spi_sck,
  17. inout [1:0] spi_io,
  18. output reg spi_cs_n,
  19. /* SDRAM interface */
  20. output [15:0] wd, // Data to RAM
  21. (* syn_preserve = 1 *) // Don't merge into FIFO
  22. output [24:1] waddr, // RAM address
  23. output reg [1:0] wrq, // Write request (min 4/8 bytes)
  24. input wacc, // Data accepted (ready for next data)
  25. /* CPU control interface */
  26. output reg [31:0] cpu_rdata,
  27. input [31:0] cpu_wdata,
  28. input cpu_valid,
  29. input [3:0] cpu_wstrb,
  30. input [2:0] cpu_addr,
  31. output reg irq
  32. );
  33. reg [24:2] ramstart;
  34. reg [31:0] romcmd;
  35. reg [23:2] datalen;
  36. reg [2:0] cmdlen;
  37. reg go_spi;
  38. reg is_spi;
  39. reg go_ram;
  40. reg is_ram;
  41. reg spi_dual;
  42. reg spi_more; // Do not raise CS# after command done
  43. reg ram_done;
  44. reg ram_done_q;
  45. reg [1:0] cpu_wr_q;
  46. reg [31:0] spi_in_shr; // Input shift register for one-bit input
  47. wire spi_active_s;
  48. wire cpu_wr_w = cpu_valid & cpu_wstrb[0];
  49. always @(negedge rst_n or posedge ram_clk)
  50. if (~rst_n)
  51. begin
  52. ramstart <= 23'b0;
  53. romcmd <= 32'b0;
  54. datalen <= 22'b0;
  55. cmdlen <= 3'b0;
  56. go_spi <= 1'b0;
  57. is_spi <= 1'b0;
  58. go_ram <= 1'b0;
  59. is_ram <= 1'b0;
  60. ram_done_q <= 1'b1;
  61. irq <= 1'b1;
  62. spi_dual <= 1'b0;
  63. spi_more <= 1'b0;
  64. cpu_wr_q <= 2'b0;
  65. end
  66. else
  67. begin
  68. ram_done_q <= ram_done;
  69. if (~ram_done_q)
  70. go_ram <= 1'b0;
  71. if (spi_active_s)
  72. go_spi <= 1'b0;
  73. if (ram_done_q & ~go_ram & ~spi_active_s & ~go_spi)
  74. irq <= 1'b1;
  75. // Don't allow writing unless the unit is idle (IRQ = 1)
  76. // Delay the recognition of the write by one ram_clk
  77. // cycle (so it is recognized on the second half of the
  78. // corresponding sys_clk cycle) to relax timings; this
  79. // is not performance critical at all.
  80. cpu_wr_q <= { cpu_wr_q[0], cpu_wr_w & irq };
  81. if (cpu_wr_q == 2'b01)
  82. begin
  83. // Only full word accesses supported via DMA!!
  84. case (cpu_addr)
  85. 2'b00: begin
  86. ramstart <= cpu_wdata[24:2];
  87. end
  88. 2'b01: begin
  89. romcmd <= cpu_wdata[31:0];
  90. end
  91. 2'b10: begin
  92. datalen <= cpu_wdata[23:2];
  93. cmdlen <= cpu_wdata[26:24];
  94. go_spi <= cpu_wdata[26:24] != 3'd0;
  95. is_spi <= cpu_wdata[26:24] != 3'd0;
  96. spi_dual <= cpu_wdata[27];
  97. spi_more <= cpu_wdata[28];
  98. is_ram <= cpu_wdata[29];
  99. go_ram <= cpu_wdata[29];
  100. irq <= 1'b0;
  101. end
  102. default: begin
  103. // Do nothing
  104. end
  105. endcase // case (cpu_addr)
  106. end // if (cpu_valid & cpu_wstrb[0])
  107. end // else: !if(~rst_n)
  108. always_comb
  109. case (cpu_addr)
  110. 3'b000: cpu_rdata = { 7'b0, ramstart, 2'b0 };
  111. 3'b001: cpu_rdata = romcmd;
  112. 3'b010: cpu_rdata = { 2'b0, is_ram, spi_more, spi_dual,
  113. cmdlen, datalen, 2'b0 };
  114. 3'b011: cpu_rdata = { 31'b0, irq };
  115. 3'b100: cpu_rdata = spi_in_shr;
  116. default: cpu_rdata = 32'bx;
  117. endcase // case (cpu_addr)
  118. //
  119. // FIFO and input latches
  120. //
  121. reg [1:0] spi_in_q;
  122. reg spi_in_req;
  123. reg spi_in_req_q;
  124. wire [11:0] wrusedw;
  125. wire [8:0] rdusedw;
  126. wire [15:0] fifo_out;
  127. ddufifo spirom_fifo (
  128. .aclr ( ~rst_n ),
  129. .wrclk ( rom_clk ),
  130. .data ( spi_in_q ),
  131. .wrreq ( spi_in_req_q ),
  132. .wrusedw ( wrusedw ),
  133. .rdclk ( ram_clk ),
  134. .q ( fifo_out ),
  135. .rdreq ( wacc & is_spi ),
  136. .rdusedw ( rdusedw )
  137. );
  138. //
  139. // Interfacing between FIFO and input signals
  140. //
  141. // Shuffle fifo_out because SPI brings in data in bigendian bit
  142. // order within bytes, but the FIFO IP assumes littleendian
  143. //
  144. wire [15:0] spi_wd;
  145. assign spi_wd[ 7: 6] = fifo_out[ 1: 0];
  146. assign spi_wd[ 5: 4] = fifo_out[ 3: 2];
  147. assign spi_wd[ 3: 2] = fifo_out[ 5: 4];
  148. assign spi_wd[ 1: 0] = fifo_out[ 7: 6];
  149. assign spi_wd[15:14] = fifo_out[ 9: 8];
  150. assign spi_wd[13:12] = fifo_out[11:10];
  151. assign spi_wd[11:10] = fifo_out[13:12];
  152. assign spi_wd[ 9: 8] = fifo_out[15:14];
  153. reg [24:1] waddr_q;
  154. reg [23:1] ram_data_ctr;
  155. reg wacc_q;
  156. assign waddr = waddr_q;
  157. assign wd = is_spi ? spi_wd : 16'h0000;
  158. always @(negedge rst_n or posedge ram_clk)
  159. if (~rst_n)
  160. begin
  161. waddr_q <= 24'bx;
  162. ram_data_ctr <= 23'b0;
  163. wacc_q <= 1'b0;
  164. wrq <= 2'b00;
  165. ram_done <= 1'b1;
  166. end
  167. else
  168. begin
  169. wacc_q <= wacc;
  170. if (|ram_data_ctr)
  171. begin
  172. ram_done <= 1'b0;
  173. if (is_spi)
  174. begin
  175. // Reading from SPI ROM
  176. wrq[0] <= rdusedw >= 9'd4; // 4*2 = 8 bytes min available
  177. wrq[1] <= rdusedw >= 9'd8; // 8*2 = 16 bytes min available
  178. end
  179. else
  180. begin
  181. // Zeroing memory
  182. wrq[0] <= |ram_data_ctr[23:3];
  183. wrq[1] <= |ram_data_ctr[23:4];
  184. end
  185. waddr_q <= waddr_q + wacc_q;
  186. ram_data_ctr <= ram_data_ctr - wacc_q;
  187. end // if (|ram_data_ctr)
  188. else
  189. begin
  190. wrq <= 2'b00;
  191. ram_done <= 1'b1;
  192. if (go_ram)
  193. begin
  194. waddr_q <= { ramstart, 1'b0 };
  195. ram_data_ctr <= { datalen, 1'b0 };
  196. ram_done <= 1'b0;
  197. end
  198. end
  199. end // else: !if(~rst_n)
  200. // Negative indicies refer to fractional bytes
  201. reg [2:-3] spi_cmd_ctr;
  202. reg [23:-2] spi_data_ctr;
  203. reg spi_clk_en = 1'b0;
  204. reg spi_mosi_en;
  205. reg [1:0] go_spi_q;
  206. wire go_spi_s;
  207. reg spi_more_q;
  208. reg spi_active;
  209. // Explicit synchronizers for handshake signals
  210. synchronizer #(.width(1)) go_spi_synchro
  211. (
  212. .rst_n ( rst_n ),
  213. .clk ( rom_clk ),
  214. .d ( go_spi ),
  215. .q ( go_spi_s )
  216. );
  217. synchronizer #(.width(1)) spi_active_synchro
  218. (
  219. .rst_n ( rst_n ),
  220. .clk ( ram_clk ),
  221. .d ( spi_active ),
  222. .q ( spi_active_s )
  223. );
  224. always @(negedge rst_n or posedge rom_clk)
  225. if (~rst_n)
  226. begin
  227. spi_cmd_ctr <= 6'b0;
  228. spi_clk_en <= 1'b0;
  229. spi_data_ctr <= 26'b0;
  230. spi_cs_n <= 1'b1;
  231. spi_in_req <= 1'b0;
  232. spi_in_req_q <= 1'b0;
  233. spi_mosi_en <= 1'b1;
  234. spi_in_q <= 2'bx;
  235. spi_in_shr <= 32'b0;
  236. spi_active <= 1'b0;
  237. spi_more_q <= 1'b0;
  238. end
  239. else
  240. begin
  241. spi_in_q <= spi_io;
  242. spi_in_req <= 1'b0;
  243. spi_in_req_q <= spi_in_req;
  244. spi_clk_en <= 1'b0;
  245. // Note: datalen <- spi_data_ctr is a 2-cycle multipath
  246. if (go_spi_s & ~spi_active)
  247. begin
  248. // Starting new transaction
  249. spi_cmd_ctr <= { cmdlen, 3'b0 };
  250. spi_data_ctr <= { datalen, 4'b0 };
  251. spi_active <= 1'b1;
  252. spi_cs_n <= 1'b0;
  253. spi_more_q <= spi_more;
  254. end
  255. else if ( ~|{spi_data_ctr, spi_cmd_ctr} )
  256. begin
  257. // Transaction completed
  258. spi_clk_en <= 1'b0;
  259. spi_mosi_en <= 1'b1;
  260. spi_active <= 1'b0;
  261. spi_cs_n <= ~spi_more_q;
  262. end
  263. else
  264. begin
  265. spi_active <= 1'b1;
  266. spi_cs_n <= 1'b0;
  267. if ( spi_active )
  268. begin
  269. // 64/4 = 16 bytes min space
  270. spi_clk_en <= (~wrusedw) >= 12'd128;
  271. if ( spi_clk_en )
  272. begin
  273. spi_in_shr <= { spi_in_shr[30:0], spi_io[1] };
  274. if ( spi_cmd_ctr == 6'd1 )
  275. spi_mosi_en <= ~spi_dual;
  276. if ( ~|spi_cmd_ctr )
  277. begin
  278. spi_in_req <= 1'b1;
  279. spi_data_ctr <= spi_data_ctr - 1'b1;
  280. end
  281. else
  282. begin
  283. spi_cmd_ctr <= spi_cmd_ctr - 1'b1;
  284. end
  285. end // if ( spi_clk_en )
  286. end // if ( ~spi_cs_n )
  287. end // else: !if( ~|spi_data_ctr )
  288. end // else: !if(~rst_n)
  289. // SPI output data is shifted on the negative edge
  290. reg [31:0] spi_out_shr;
  291. reg spi_clk_en_q;
  292. assign spi_io[0] = spi_mosi_en ? spi_out_shr[31] : 1'bz;
  293. assign spi_io[1] = 1'bz;
  294. always @(negedge rst_n or negedge rom_clk)
  295. if (~rst_n)
  296. begin
  297. spi_out_shr <= 32'b0;
  298. spi_clk_en_q <= 1'b0;
  299. end
  300. else
  301. begin
  302. spi_clk_en_q <= spi_clk_en;
  303. if (~spi_active)
  304. spi_out_shr <= romcmd;
  305. else if ( spi_clk_en_q )
  306. spi_out_shr <= { spi_out_shr[30:0], 1'b0 };
  307. end
  308. //
  309. // SPI_SCK output buffer
  310. //
  311. ddio_out spi_clk_buf (
  312. .aclr ( ~rst_n ),
  313. .datain_h ( spi_clk_en_q ),
  314. .datain_l ( 1'b0 ),
  315. .outclock ( rom_clk ),
  316. .dataout ( spi_sck )
  317. );
  318. endmodule // spirom