sdram.sv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // -----------------------------------------------------------------------
  2. //
  3. // Copyright 2010-2021 H. Peter Anvin - All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  8. // Boston MA 02110-1301, USA; either version 2 of the License, or
  9. // (at your option) any later version; incorporated herein by reference.
  10. //
  11. // -----------------------------------------------------------------------
  12. //
  13. // Simple SDRAM controller
  14. //
  15. // Very simple non-parallelizing SDRAM controller.
  16. //
  17. //
  18. // Two ports are provided: port 0 is single byte per transaction,
  19. // and has highest priority; it is intended for transactions from the
  20. // ABC-bus. Port 1 does 8-strobe burst transactions. If additional ports
  21. // are needed, the intent is to add an arbiter to port 1.
  22. //
  23. // All signals are in the sdram clock domain.
  24. //
  25. module sdram (
  26. // Reset and clock
  27. input rst_n,
  28. input clk,
  29. // SDRAM hardware interface
  30. output sr_clk, // SDRAM clock output buffer
  31. output sr_cke, // SDRAM clock enable
  32. output sr_cs_n, // SDRAM CS#
  33. output sr_ras_n, // SDRAM RAS#
  34. output sr_cas_n, // SDRAM CAS#
  35. output sr_we_n, // SDRAM WE#
  36. output [1:0] sr_dqm, // SDRAM DQM (per byte)
  37. output [1:0] sr_ba, // SDRAM bank selects
  38. output [12:0] sr_a, // SDRAM address bus
  39. inout [15:0] sr_dq, // SDRAM data bus
  40. // Port 0: single byte, high priority
  41. input [24:0] a0, // Address, must be stable until ack
  42. output [7:0] rd0, // Data from SDRAM
  43. input rrq0, // Read request
  44. output rack0, // Read ack
  45. input [7:0] wd0, // Data to SDRAM
  46. input wrq0, // Write request
  47. output wack0, // Write ack
  48. // Port 1
  49. input [24:1] a1,
  50. output [15:0] rd1,
  51. input rrq1,
  52. output rack1,
  53. input [15:0] wd1,
  54. input [1:0] wbe1, // Write byte enable
  55. input wrq1,
  56. output wack1
  57. );
  58. // Timing parameters
  59. // The parameters are hardcoded for Micron MT48LC16M16A2-6A,
  60. // per datasheet:
  61. // 100 MHz 167 MHz
  62. // ----------------------------------------------------------
  63. // CL 2 3 READ to data out
  64. // tRCD 18 ns 2 3 ACTIVE to READ/WRITE
  65. // tRFC 60 ns 6 10 REFRESH to ACTIVE
  66. // tRP 18 ns 2 3 PRECHARGE to ACTIVE/REFRESH
  67. // tRAS 42 ns 5 7 ACTIVE to PRECHARGE
  68. // tRC 60 ns 6 10 ACTIVE to ACTIVE
  69. // tWR 12 ns 2 2 Last write data to PRECHARGE
  70. // tMRD 2 2 MODE REGISTER to ACTIVE/REFRESH
  71. //
  72. // These parameters are set by power of 2:
  73. // tREF 64/8192 ms 781 1302 Refresh time per row (max)
  74. // tP 100 us 10000 16667 Time until first command (min)
  75. parameter t_cl = 3;
  76. parameter t_rcd = 3;
  77. parameter t_rfc = 10;
  78. parameter t_rp = 3;
  79. parameter t_ras = 7;
  80. parameter t_rc = 10;
  81. parameter t_wr = 2;
  82. parameter t_mrd = 2;
  83. parameter t_ref = 9; // 512 cycles (extra conservative)
  84. parameter t_p = 15; // 32768 cycles
  85. parameter burst = 3; // 8-cycle bursts
  86. // Mode register data
  87. wire mrd_wburst = 1'b1; // Write bursts enabled
  88. wire [2:0] mrd_cl = t_cl;
  89. wire [2:0] mrd_burst = burst;
  90. wire mrd_interleave = 1'b0; // Interleaved bursts
  91. wire [12:0] mrd_val = { 3'b000, // Reserved
  92. ~mrd_wburst, // Write burst disable
  93. 2'b00, // Normal operation
  94. mrd_cl, // CAS latency
  95. mrd_interleave, // Interleaved bursts
  96. mrd_burst }; // Burst length
  97. // Handy functions for timing calculations
  98. function int max(input int a, b);
  99. if (a > b)
  100. max = a;
  101. else
  102. max = b;
  103. endfunction // max
  104. function int nops(input int need, elapsed);
  105. if (need > elapsed + 1)
  106. nops = need - (elapsed+1);
  107. else
  108. nops = 0;
  109. endfunction // nops
  110. // Command opcodes (CS#, RAS#, CAS#, WE#)
  111. parameter cmd_desl = 4'b1111; // Deselect (= NOP)
  112. parameter cmd_nop = 4'b0111; // NO OPERATION
  113. parameter cmd_bst = 4'b0110; // BURST TERMINATE
  114. parameter cmd_rd = 4'b0101; // READ
  115. parameter cmd_wr = 4'b0100; // WRITE
  116. parameter cmd_act = 4'b0011; // ACTIVE
  117. parameter cmd_pre = 4'b0010; // PRECHARGE
  118. parameter cmd_ref = 4'b0001; // AUTO REFRESH
  119. parameter cmd_mrd = 4'b0000; // LOAD MODE REGISTER
  120. // SDRAM output clock buffer. The SDRAM output clock is
  121. // inverted with respect to our internal clock, so that
  122. // the SDRAM sees the positive clock edge in the middle of
  123. // our clocks.
  124. //
  125. // Use a DDIO buffer for best performance
  126. // For EP4CE15 only could use a secondary PLL here, but it
  127. // isn't clear it buys us a whole lot.
  128. ddio_out sr_clk_out (
  129. .aclr ( 1'b0 ),
  130. .datain_h ( 1'b0 ),
  131. .datain_l ( 1'b1 ),
  132. .outclock ( clk ),
  133. .dataout ( sr_clk )
  134. );
  135. // SDRAM output signal registers
  136. reg dram_cke;
  137. assign sr_cke = dram_cke;
  138. reg [3:0] dram_cmd;
  139. assign sr_cs_n = dram_cmd[3];
  140. assign sr_ras_n = dram_cmd[2];
  141. assign sr_cas_n = dram_cmd[1];
  142. assign sr_we_n = dram_cmd[0];
  143. reg [11:0] dram_a;
  144. assign sr_a = dram_a;
  145. reg [1:0] dram_ba;
  146. assign sr_ba = dram_ba;
  147. reg [1:0] dram_dqm;
  148. assign sr_dqm = dram_dqm;
  149. reg [15:0] dram_d; // Data to DRAM
  150. reg dram_d_en; // Drive data out
  151. assign sr_dq = dram_d_en ? dram_d : 16'hzzzz;
  152. // Input register for SDRAM data; a single register to make it
  153. // possible to put it in the I/O register
  154. reg [15:0] dram_q;
  155. // Port 0 output signals
  156. assign rd0 = a0[0] ? dram_q[15:8] : dram_q[7:0];
  157. reg rack0_q;
  158. assign rack0 = rack0_q;
  159. reg wack0_q;
  160. assign wack0 = wack0_q;
  161. // Port 1 output signals
  162. assign rd1 = dram_q;
  163. reg rack1_q;
  164. assign rack1 = rack1_q;
  165. reg wack1_q;
  166. assign wack1 = wack1_q;
  167. // State machine and counters
  168. reg [t_ref:0] rfsh_ctr; // Refresh timer
  169. reg [t_p:t_ref] init_ctr; // Initialization counter
  170. reg [3:0] nop_ctr; // Insert NOPs into a cycle
  171. reg [burst-1:0] burst_ctr; // Position in the current burst
  172. reg [3:0] state;
  173. parameter st_reset = 4'h00; // Reset until init timer expires
  174. parameter st_init_rfsh1 = 4'h01; // 1st refresh during initialization
  175. parameter st_init_rfsh2 = 4'h02; // 2st refresh during initialization
  176. parameter st_init_mrd = 4'h03;
  177. parameter st_idle = 4'h04; // Idle state: all banks precharged
  178. parameter st_p0_rd_cmd = 4'h06;
  179. parameter st_p0_rd_pre = 4'h07;
  180. parameter st_p0_rd_data = 4'h08;
  181. parameter st_p0_wr_cmd = 4'h09;
  182. parameter st_p0_wr_pre = 4'h0a;
  183. parameter st_p1_rd_cmd = 4'h0b;
  184. parameter st_p1_rd_pre = 4'h0c;
  185. parameter st_p1_rd_data = 4'h0d;
  186. parameter st_p1_wr_cmd = 4'h0e;
  187. parameter st_p1_wr_data = 4'h0f;
  188. reg is_rfsh; // A refresh cycle, clear rfsh_ctr
  189. always @(posedge clk or negedge rst_n)
  190. if (~rst_n)
  191. begin
  192. rfsh_ctr <= 1'b0;
  193. init_ctr <= 1'b0;
  194. end
  195. else
  196. begin
  197. if (is_rfsh)
  198. rfsh_ctr <= 1'b0;
  199. else
  200. rfsh_ctr <= rfsh_ctr + 1'b1;
  201. // The refresh counter is also used as a prescaler
  202. // for the initialization counter.
  203. if (init_ctr[t_ref] ^ rfsh_ctr[t_ref])
  204. begin
  205. init_ctr[t_p:t_ref+1] <= init_ctr[t_p:t_ref+1] + 1'b1;
  206. init_ctr[t_ref] <= rfsh_ctr[t_ref];
  207. end
  208. end // else: !if(~rst_n)
  209. //
  210. // Careful with the timing here... there is one cycle between
  211. // registers and wires, and the DRAM observes the clock 1/2
  212. // cycle from the internal logic.
  213. //
  214. always @(posedge clk or negedge rst_n)
  215. if (~rst_n)
  216. begin
  217. dram_cke <= 1'b0;
  218. dram_cmd <= cmd_desl;
  219. dram_a <= 13'h0000;
  220. dram_ba <= 2'b00;
  221. dram_dqm <= 2'b00;
  222. dram_d <= 16'h0000;
  223. dram_d_en <= 1'b1; // Don't float except during read
  224. nop_ctr <= 4'd0;
  225. burst_ctr <= 1'b0;
  226. state <= st_reset;
  227. is_rfsh <= 1'b0;
  228. rack0_q <= 1'b0;
  229. wack0_q <= 1'b0;
  230. rack1_q <= 1'b0;
  231. wack1_q <= 1'b0;
  232. end
  233. else
  234. begin
  235. dram_cke <= 1'b1; // Always true once out of reset
  236. // Default values
  237. dram_a <= 13'h0000;
  238. dram_ba <= 2'b00;
  239. dram_dqm <= (state == st_p0_wr_pre) ? 2'b11 : 2'b00;
  240. dram_d <= 16'h0000;
  241. is_rfsh <= 1'b0;
  242. rack0_q <= 1'b0;
  243. wack0_q <= 1'b0;
  244. rack1_q <= 1'b0;
  245. wack1_q <= 1'b0;
  246. if (|nop_ctr)
  247. begin
  248. dram_cmd <= cmd_nop;
  249. nop_ctr <= nop_ctr - 1'b1;
  250. end
  251. else
  252. begin
  253. nop_ctr <= 1'b0; // Redundant, but might help compiler
  254. dram_d_en <= 1'b1;
  255. burst_ctr <= 1'b0;
  256. case (state)
  257. st_reset:
  258. begin
  259. dram_cmd <= cmd_desl;
  260. if (init_ctr[t_p])
  261. begin
  262. dram_cmd <= cmd_pre;
  263. dram_a[10] <= 1'b1; // Precharge All Banks
  264. state <= st_init_rfsh1;
  265. nop_ctr <= nops(t_rp, 0);
  266. end
  267. end
  268. st_init_rfsh1:
  269. begin
  270. dram_cmd <= cmd_ref;
  271. state <= st_init_rfsh2;
  272. nop_ctr <= nops(t_rfc, 0);
  273. end
  274. st_init_rfsh2:
  275. begin
  276. dram_cmd <= cmd_ref;
  277. state <= st_init_mrd;
  278. nop_ctr <= nops(t_rfc, 0);
  279. end
  280. st_init_mrd:
  281. begin
  282. dram_cmd <= cmd_mrd;
  283. dram_a <= mrd_val;
  284. state <= st_idle;
  285. nop_ctr <= nops(t_mrd, 0);
  286. end
  287. st_idle:
  288. begin
  289. // A data transaction starts with ACTIVE command;
  290. // a refresh transaction starts with REFRESH.
  291. // Port 0 has the highest priority, then
  292. // refresh, then port 1; a refresh transaction
  293. // is started opportunistically if nothing is
  294. // pending and the refresh counter is no less than
  295. // half expired.
  296. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_ref:t_ref-1]} )
  297. 4'b1zzz:
  298. begin
  299. // Begin port 0 transaction
  300. dram_cmd <= cmd_act;
  301. dram_a <= a0[24:12];
  302. dram_ba <= a0[11:10];
  303. state <= wrq0 ? st_p0_wr_cmd : st_p0_rd_cmd;
  304. nop_ctr <= nops(t_rcd, 0);
  305. end
  306. 4'b010z:
  307. begin
  308. // Begin port 1 transaction
  309. dram_cmd <= cmd_act;
  310. dram_a <= a1[24:12];
  311. dram_ba <= a1[11:10];
  312. state <= wrq1 ? st_p1_wr_cmd : st_p1_rd_cmd;
  313. nop_ctr <= nops(t_rcd, 0);
  314. end
  315. 4'b0z1z, 4'b0001:
  316. begin
  317. // Begin refresh transaction
  318. dram_cmd <= cmd_ref;
  319. state <= st_idle;
  320. nop_ctr <= nops(t_rfc, 0);
  321. is_rfsh <= 1'b1;
  322. end
  323. 4'b0000:
  324. begin
  325. dram_cmd <= cmd_desl;
  326. state <= st_idle;
  327. end
  328. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_ref:t_ref-1]} )
  329. end // case: st_idle
  330. st_p0_rd_cmd:
  331. begin
  332. dram_cmd <= cmd_rd;
  333. dram_d_en <= 1'b0; // Tristate our output
  334. dram_a[8:0] <= a0[9:1];
  335. dram_ba <= a0[11:10];
  336. state <= st_p0_rd_pre;
  337. end
  338. st_p0_rd_pre:
  339. begin
  340. // The PRE command issued in the cycle immediately
  341. // after the read will terminate the burst after
  342. // exactly one data strobe. Note that the nop_ctr
  343. // below is CL+1, not CL, due to bus turnaround.
  344. dram_cmd <= cmd_pre;
  345. dram_d_en <= 1'b0; // Tristate our output
  346. dram_ba <= a0[11:10];
  347. state <= st_p0_rd_data;
  348. nop_ctr <= nops(t_cl + 1, 1);
  349. end
  350. st_p0_rd_data:
  351. begin
  352. // The nop_ctr elapsed calculation has:
  353. // +1 for the bus turnaround cycle added previously
  354. dram_cmd <= cmd_nop;
  355. dram_d_en <= 1'b0; // Tristate one more cycle
  356. dram_q <= sr_dq;
  357. rack0_q <= 1'b1;
  358. state <= st_idle;
  359. nop_ctr <= max(nops(t_rp, t_cl + 1),
  360. nops(t_rc, t_rcd + t_cl + 1));
  361. end
  362. st_p0_wr_cmd:
  363. begin
  364. dram_cmd <= cmd_wr;
  365. dram_a[8:0] <= a0[9:1];
  366. dram_ba <= a0[11:10];
  367. dram_dqm <= { ~a0[0], a0[0] };
  368. dram_d <= { wd0, wd0 };
  369. wack0_q <= 1'b1;
  370. state <= st_p0_wr_pre;
  371. nop_ctr <= nops(t_wr, 0);
  372. end
  373. st_p0_wr_pre:
  374. begin
  375. dram_cmd <= cmd_pre;
  376. nop_ctr <= max(nops(t_rp, 0),
  377. nops(t_rc, t_rcd + t_wr));
  378. state <= st_idle;
  379. end
  380. st_p1_rd_cmd:
  381. begin
  382. dram_cmd <= cmd_rd;
  383. dram_d_en <= 1'b0; // Tristate our output
  384. dram_a[8:0] <= a1[9:1];
  385. dram_ba <= a1[11:10];
  386. dram_a[10] <= 1'b1; // Auto precharge
  387. nop_ctr <= nops(t_cl + 1, 0);
  388. state <= st_p1_rd_data;
  389. end
  390. st_p1_rd_data:
  391. begin
  392. dram_cmd <= cmd_nop;
  393. dram_d_en <= 1'b0; // Tristate our output
  394. dram_q <= sr_dq;
  395. rack1_q <= 1'b1;
  396. burst_ctr <= burst_ctr + 1'b1;
  397. if (&burst_ctr)
  398. begin
  399. state <= st_idle;
  400. nop_ctr <= max(nops(t_rp, t_cl + (1 << burst)),
  401. nops(t_rc, t_rcd + t_cl + (1 << burst)));
  402. end
  403. end
  404. st_p1_wr_cmd:
  405. begin
  406. dram_cmd <= cmd_wr;
  407. dram_a[8:0] <= a1[9:1];
  408. dram_ba <= a1[11:10];
  409. dram_a[10] <= 1'b1; // Auto precharge
  410. dram_d <= wd1;
  411. dram_dqm <= ~wbe1;
  412. wack1_q <= 1'b1;
  413. burst_ctr <= burst_ctr + 1'b1;
  414. state <= st_p1_wr_data;
  415. end
  416. st_p1_wr_data:
  417. begin
  418. dram_cmd <= cmd_nop;
  419. dram_d <= wd1;
  420. dram_dqm <= ~wbe1;
  421. wack1_q <= 1'b1;
  422. burst_ctr <= burst_ctr + 1'b1;
  423. if (&burst_ctr)
  424. begin
  425. state <= st_idle;
  426. nop_ctr <= max(nops(t_wr + t_rp, 0),
  427. nops(t_rc, t_rcd + (1 << burst)));
  428. end
  429. end
  430. endcase // case(state)
  431. end // else: !if(|nop_ctr)
  432. end // else: !if(~rst_n)
  433. endmodule // dram