sdram.sv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. // The ack signals are arrays that give (some) advance notification:
  26. // e.g. rack0[1] is asserted exactly one cycle before rack0[0].
  27. //
  28. // rack*[0] is asserted during the first cycle rd* is valid.
  29. // wack*[0] is asserted during the cycle after wd* was sampled.
  30. //
  31. // Thus, especially for bursts on port 1, generally rack1[0] and
  32. // wack1[1] are the relevant signals.
  33. //
  34. module sdram
  35. #( parameter
  36. // Timing parameters
  37. // The parameters are hardcoded for Micron MT48LC16M16A2-6A,
  38. // per datasheet:
  39. // 100 MHz 167 MHz
  40. // ----------------------------------------------------------
  41. // CL 2 3 READ to data out
  42. // tRCD 18 ns 2 3 ACTIVE to READ/WRITE
  43. // tRFC 60 ns 6 10 REFRESH to ACTIVE
  44. // tRP 18 ns 2 3 PRECHARGE to ACTIVE/REFRESH
  45. // tRAS 42 ns 5 7 ACTIVE to PRECHARGE
  46. // tRC 60 ns 6 10 ACTIVE to ACTIVE
  47. // tWR 12 ns 2 2 Last write data to PRECHARGE
  48. // tMRD 2 2 MODE REGISTER to ACTIVE/REFRESH
  49. //
  50. // These parameters are set by power of 2:
  51. // tREFi 64/8192 ms 781 1302 Refresh time per row (max)
  52. // tP 100 us 10000 16667 Time until first command (min)
  53. t_cl = 3,
  54. t_rcd = 3,
  55. t_rfc = 10,
  56. t_rp = 3,
  57. t_ras = 7,
  58. t_rc = 10,
  59. t_wr = 2,
  60. t_mrd = 2,
  61. t_refi_lg2 = 9, // 512 cycles (extra conservative)
  62. t_p_lg2 = 15, // 32768 cycles
  63. burst_lg2 = 3 // Burst length on port 1
  64. )
  65. (
  66. // Reset and clock
  67. input rst_n,
  68. input clk,
  69. // SDRAM hardware interface
  70. output sr_clk, // SDRAM clock output buffer
  71. output sr_cke, // SDRAM clock enable
  72. output sr_cs_n, // SDRAM CS#
  73. output sr_ras_n, // SDRAM RAS#
  74. output sr_cas_n, // SDRAM CAS#
  75. output sr_we_n, // SDRAM WE#
  76. output [1:0] sr_dqm, // SDRAM DQM (per byte)
  77. output [1:0] sr_ba, // SDRAM bank selects
  78. output [12:0] sr_a, // SDRAM address bus
  79. inout [15:0] sr_dq, // SDRAM data bus
  80. // Port 0: single byte, high priority
  81. input [24:0] a0, // Address, must be stable until ack
  82. output [7:0] rd0, // Data from SDRAM
  83. input rrq0, // Read request
  84. output [t_rcd+t_cl+1:0] rack0, // Read ack
  85. input [7:0] wd0, // Data to SDRAM
  86. input wrq0, // Write request
  87. output [t_rcd:0] wack0, // Write ack
  88. // Port 1
  89. input [24:1] a1,
  90. output [15:0] rd1,
  91. input rrq1,
  92. output [t_rcd+t_cl+1:0] rack1,
  93. input [15:0] wd1,
  94. input [1:0] wbe1, // Write byte enable
  95. input wrq1,
  96. output [t_rcd:0] wack1
  97. );
  98. // Mode register data
  99. wire mrd_wburst = 1'b1; // Write bursts enabled
  100. wire [2:0] mrd_cl = t_cl;
  101. wire [2:0] mrd_burst = burst_lg2;
  102. wire mrd_interleave = 1'b0; // Interleaved bursts
  103. wire [12:0] mrd_val = { 3'b000, // Reserved
  104. ~mrd_wburst, // Write burst disable
  105. 2'b00, // Normal operation
  106. mrd_cl, // CAS latency
  107. mrd_interleave, // Interleaved bursts
  108. mrd_burst }; // Burst length
  109. // Where to issue a PRECHARGE when we only want to read one word
  110. // (terminate the burst as soon as possible, but no sooner...)
  111. localparam t_pre_rd_when = max(t_ras, t_rcd + 1);
  112. // Where to issue a PRECHARGE when we only want to write one word
  113. // (terminate the burst as soon as possible, but no sooner...)
  114. localparam t_pre_wr_when = max(t_ras, t_rcd + t_wr);
  115. // Actual burst length (2^burst_lg2)
  116. localparam burst_n = 1 << burst_lg2;
  117. // Command opcodes and attributes (is_rfsh, CS#, RAS#, CAS#, WE#)
  118. localparam cmd_desl = 5'b0_1111; // Deselect (= NOP)
  119. localparam cmd_nop = 5'b0_0111; // NO OPERATION
  120. localparam cmd_bst = 5'b0_0110; // BURST TERMINATE
  121. localparam cmd_rd = 5'b0_0101; // READ
  122. localparam cmd_wr = 5'b0_0100; // WRITE
  123. localparam cmd_act = 5'b0_0011; // ACTIVE
  124. localparam cmd_pre = 5'b0_0010; // PRECHARGE
  125. localparam cmd_ref = 5'b1_0001; // AUTO REFRESH
  126. localparam cmd_mrd = 5'b0_0000; // LOAD MODE REGISTER
  127. reg [4:0] dram_cmd;
  128. wire is_rfsh = dram_cmd[4];
  129. assign sr_cs_n = dram_cmd[3];
  130. assign sr_ras_n = dram_cmd[2];
  131. assign sr_cas_n = dram_cmd[1];
  132. assign sr_we_n = dram_cmd[0];
  133. // SDRAM output clock buffer. The SDRAM output clock is
  134. // inverted with respect to our internal clock, so that
  135. // the SDRAM sees the positive clock edge in the middle of
  136. // our clocks.
  137. //
  138. // Use a DDIO buffer for best performance
  139. // For EP4CE15 only could use a secondary PLL here, but it
  140. // isn't clear it buys us a whole lot.
  141. ddio_out sr_clk_out (
  142. .aclr ( 1'b0 ),
  143. .datain_h ( 1'b0 ),
  144. .datain_l ( 1'b1 ),
  145. .outclock ( clk ),
  146. .dataout ( sr_clk )
  147. );
  148. // SDRAM output signal registers
  149. reg dram_cke;
  150. assign sr_cke = dram_cke;
  151. reg [12:0] dram_a;
  152. assign sr_a = dram_a;
  153. reg [1:0] dram_ba;
  154. assign sr_ba = dram_ba;
  155. reg [1:0] dram_dqm;
  156. assign sr_dqm = dram_dqm;
  157. reg [15:0] dram_d; // Data to DRAM
  158. reg dram_d_en; // Drive data out
  159. assign sr_dq = dram_d_en ? dram_d : 16'hzzzz;
  160. // Input register for SDRAM data; a single register to make it
  161. // possible to put it in the I/O register
  162. reg [15:0] dram_q;
  163. // Port 0 output signals
  164. assign rd0 = a0[0] ? dram_q[15:8] : dram_q[7:0];
  165. reg [t_rcd+t_cl+1:0] rack0_q;
  166. assign rack0 = rack0_q;
  167. reg [t_rcd:0] wack0_q;
  168. assign wack0 = wack0_q;
  169. // Port 1 output signals
  170. assign rd1 = dram_q;
  171. reg [t_rcd+t_cl+1:0] rack1_q;
  172. assign rack1 = rack1_q;
  173. reg [t_rcd:0] wack1_q;
  174. assign wack1 = wack1_q;
  175. // State machine and counters
  176. reg [t_refi_lg2:0] rfsh_ctr; // Refresh timer
  177. reg [t_p_lg2:t_refi_lg2] init_ctr; // Reset to init counter
  178. // XXX: compute the necessary width of this field elsewhere
  179. reg [4:0] op_cycle; // Cycles into the current operation
  180. // The actual values are unimportant; the compiler will optimize
  181. // the state machine implementation.
  182. typedef enum logic [2:0] {
  183. st_reset, // Reset until init timer expires
  184. st_init, // 1st refresh during initialization
  185. st_idle, // Idle state: all banks precharged
  186. st_rfsh,
  187. st_p0_rd,
  188. st_p0_wr,
  189. st_p1_rd,
  190. st_p1_wr
  191. } state_t;
  192. state_t state = st_reset;
  193. always @(posedge clk or negedge rst_n)
  194. if (~rst_n)
  195. begin
  196. rfsh_ctr <= 1'b0;
  197. init_ctr <= 1'b0;
  198. end
  199. else
  200. begin
  201. rfsh_ctr <= is_rfsh ? 1'b0 : rfsh_ctr + 1'b1;
  202. // The refresh counter is also used as a prescaler
  203. // for the initialization counter.
  204. if (init_ctr[t_refi_lg2] ^ rfsh_ctr[t_refi_lg2])
  205. begin
  206. init_ctr[t_p_lg2:t_refi_lg2+1]
  207. <= init_ctr[t_p_lg2:t_refi_lg2+1] + 1'b1;
  208. init_ctr[t_refi_lg2] <= rfsh_ctr[t_refi_lg2];
  209. end
  210. end // else: !if(~rst_n)
  211. //
  212. // Careful with the timing here... there is one cycle between
  213. // registers and wires, and the DRAM observes the clock 1/2
  214. // cycle from the internal logic.
  215. //
  216. always @(posedge clk or negedge rst_n)
  217. if (~rst_n)
  218. begin
  219. dram_cke <= 1'b0;
  220. dram_cmd <= cmd_desl;
  221. dram_a <= 13'h0000;
  222. dram_ba <= 2'b00;
  223. dram_dqm <= 2'b00;
  224. dram_d <= 16'h0000;
  225. dram_d_en <= 1'b1; // Don't float except during read
  226. op_cycle <= 1'b0;
  227. state <= st_reset;
  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 <= 2'b11;
  240. dram_d <= 16'h0000;
  241. rack0_q <= 1'b0;
  242. wack0_q <= 1'b0;
  243. rack1_q <= 1'b0;
  244. wack1_q <= 1'b0;
  245. dram_d_en <= 1'b1; // Don't float except during read
  246. if (state == st_reset || state == st_idle)
  247. op_cycle <= 1'b1; // == 0 + 1
  248. else
  249. op_cycle <= op_cycle + 1'b1;
  250. case (state)
  251. st_reset:
  252. begin
  253. dram_cmd <= cmd_desl;
  254. if (init_ctr[t_p_lg2])
  255. begin
  256. dram_cmd <= cmd_pre;
  257. dram_a[10] <= 1'b1; // Precharge All Banks
  258. state <= st_init;
  259. end
  260. end
  261. st_init:
  262. begin
  263. if ( op_cycle == t_rp || op_cycle == t_rp + t_rfc )
  264. begin
  265. dram_cmd <= cmd_ref;
  266. end
  267. if ( op_cycle == t_rp + t_rfc*2 )
  268. begin
  269. dram_cmd <= cmd_mrd;
  270. dram_a <= mrd_val;
  271. end
  272. if ( op_cycle >= t_rp + t_rfc*2 + t_mrd - 1 )
  273. state <= st_idle;
  274. end // case: st_init
  275. st_idle:
  276. begin
  277. // A data transaction starts with ACTIVE command;
  278. // a refresh transaction starts with REFRESH.
  279. // Port 0 has the highest priority, then
  280. // refresh, then port 1; a refresh transaction
  281. // is started opportunistically if nothing is
  282. // pending and the refresh counter is no less than
  283. // half expired.
  284. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_refi_lg2:t_refi_lg2-1]} )
  285. 4'b1zzz:
  286. begin
  287. // Begin port 0 transaction
  288. dram_cmd <= cmd_act;
  289. dram_a <= a0[24:12];
  290. dram_ba <= a0[11:10];
  291. if ( wrq0 )
  292. begin
  293. state <= st_p0_wr;
  294. wack0_q[t_rcd] <= 1'b1;
  295. end
  296. else
  297. begin
  298. state <= st_p0_rd;
  299. rack0_q[t_rcd + t_cl + 1] <= 1'b1;
  300. end
  301. end
  302. 4'b010z:
  303. begin
  304. // Begin port 1 transaction
  305. dram_cmd <= cmd_act;
  306. dram_a <= a1[24:12];
  307. dram_ba <= a1[11:10];
  308. if ( wrq1 )
  309. begin
  310. state <= st_p1_wr;
  311. wack1_q[t_rcd] <= 1'b1;
  312. end
  313. else
  314. begin
  315. state <= st_p1_rd;
  316. rack1_q[t_rcd + t_cl + 1] <= 1'b1;
  317. end
  318. end
  319. 4'b0z1z, 4'b0001:
  320. begin
  321. // Begin refresh transaction
  322. dram_cmd <= cmd_ref;
  323. state <= st_rfsh;
  324. end
  325. default:
  326. begin
  327. dram_cmd <= cmd_desl;
  328. state <= st_idle;
  329. end
  330. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_ref:t_ref-1]} )
  331. end // case: st_idle
  332. st_rfsh:
  333. begin
  334. if (op_cycle >= t_rfc-1)
  335. state <= st_idle;
  336. end
  337. st_p0_rd:
  338. begin
  339. dram_d_en <= 1'b0; // Tristate our output
  340. dram_a[8:0] <= a0[9:1];
  341. dram_ba <= a0[11:10];
  342. dram_a[10] <= 1'b0; // No auto precharge
  343. dram_dqm <= 2'b00;
  344. if ( op_cycle == t_rcd )
  345. dram_cmd <= cmd_rd;
  346. if ( op_cycle == t_pre_rd_when )
  347. dram_cmd <= cmd_pre; // Precharge and stop burst
  348. // Latch and ack input data. There is an implicit
  349. // due to bus turnaround.
  350. for (int i = 0; i <= t_rcd + t_cl; i++)
  351. begin
  352. rack0_q[i] <= (op_cycle == t_rcd + t_cl + 1 - i);
  353. end
  354. if ( op_cycle == t_rcd + t_cl + 1 )
  355. begin
  356. dram_q <= sr_dq;
  357. rack0_q <= 1'b1;
  358. end
  359. if ( op_cycle >= max(t_rc, t_pre_rd_when + t_rp) - 1 )
  360. state <= st_idle;
  361. end // case: st_p0_rd
  362. st_p0_wr:
  363. begin
  364. dram_a[8:0] <= a0[9:1];
  365. dram_ba <= a0[11:10];
  366. dram_a[10] <= 1'b0; // No auto precharge
  367. dram_d <= { wd0, wd0 };
  368. // Ack data
  369. for (int i = 0; i <= t_rcd; i++)
  370. begin
  371. wack0_q[i] <= (op_cycle == t_rcd - i);
  372. end
  373. if ( op_cycle == t_rcd )
  374. begin
  375. dram_cmd <= cmd_wr;
  376. dram_dqm <= { ~a0[0], a0[0] };
  377. end
  378. if ( op_cycle == t_pre_wr_when )
  379. begin
  380. dram_cmd <= cmd_pre;
  381. end
  382. if ( op_cycle >= max(t_rc, t_pre_wr_when + t_rp) - 1 )
  383. state <= st_idle;
  384. end // case: st_p0_wr
  385. st_p1_rd:
  386. begin
  387. dram_d_en <= 1'b0; // Tristate our output
  388. dram_a[8:0] <= a1[9:1];
  389. dram_ba <= a1[11:10];
  390. dram_a[10] <= 1'b1; // Auto precharge
  391. dram_dqm <= 2'b00;
  392. if ( op_cycle == t_rcd )
  393. dram_cmd <= cmd_rd;
  394. // Latch and ack input data. There is an implicit +1
  395. // due to bus turnaround.
  396. for (int i = 0; i <= t_rcd + t_cl + 1; i++)
  397. begin
  398. rack1_q[i] <= (op_cycle > t_rcd + t_cl - i &&
  399. op_cycle <= t_rcd + t_cl + burst_n - i);
  400. end
  401. if ( op_cycle > t_rcd + t_cl &&
  402. op_cycle <= t_rcd + t_cl + burst_n )
  403. begin
  404. dram_q <= sr_dq;
  405. end
  406. if ( op_cycle >= max(t_rc - 1, t_rcd + t_cl + burst_n) )
  407. state <= st_idle;
  408. end // case: st_p1_rd
  409. st_p1_wr:
  410. begin
  411. dram_a[8:0] <= a1[9:1];
  412. dram_ba <= a1[11:10];
  413. dram_a[10] <= 1'b1; // Auto precharge
  414. dram_dqm <= ~wbe1;
  415. dram_d <= wd1;
  416. // Ack data
  417. for (int i = 0; i <= t_rcd; i++)
  418. begin
  419. wack1_q[i] <= (op_cycle >= t_rcd - i &&
  420. op_cycle < t_rcd + burst_n);
  421. end
  422. if ( op_cycle == t_rcd )
  423. dram_cmd <= cmd_wr;
  424. if ( op_cycle >= max(t_rcd + burst_n + t_wr + t_rp, t_rc) - 1 )
  425. state <= st_idle;
  426. end // case: st_p1_wr
  427. endcase // case(state)
  428. end // else: !if(~rst_n)
  429. endmodule // dram