2
0

sdram.sv 14 KB

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