sdram.sv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 aligned 4-byte accesses with byte enables.
  21. //
  22. // All signals are in the sdram clock domain.
  23. //
  24. // [rw]ack is asserted at the beginning of a read- or write cycle and
  25. // deasserted afterwards; rready is asserted once all data is read and
  26. // the read data (rdX port) is valid; it remains asserted after the
  27. // transaction is complete and rack is deasserted.
  28. //
  29. module sdram
  30. #( parameter
  31. // Timing parameters
  32. // The parameters are hardcoded for Micron MT48LC16M16A2-6A,
  33. // per datasheet:
  34. // 100 MHz 167 MHz
  35. // ----------------------------------------------------------
  36. // CL 2 3 READ to data out
  37. // tRCD 18 ns 2 3 ACTIVE to READ/WRITE
  38. // tRFC 60 ns 6 10 REFRESH to ACTIVE
  39. // tRP 18 ns 2 3 PRECHARGE to ACTIVE/REFRESH
  40. // tRAS 42 ns 5 7 ACTIVE to PRECHARGE
  41. // tRC 60 ns 6 10 ACTIVE to ACTIVE (same bank)
  42. // tRRD 12 ns 2 2 ACTICE to ACTIVE (different bank)
  43. // tWR 12 ns 2 2 Last write data to PRECHARGE
  44. // tMRD 2 2 MODE REGISTER to ACTIVE/REFRESH
  45. //
  46. // These parameters are set by power of 2:
  47. // tREFi 64/8192 ms 781 1302 Refresh time per row (max)
  48. // tP 100 us 10000 16667 Time until first command (min)
  49. t_cl = 3,
  50. t_rcd = 3,
  51. t_rfc = 10,
  52. t_rp = 3,
  53. t_ras = 7,
  54. t_rc = 10,
  55. t_rrd = 2,
  56. t_wr = 2,
  57. t_mrd = 2,
  58. t_refi_lg2 = 10, // 1024 cycles
  59. t_p_lg2 = 15, // 32768 cycles
  60. burst_lg2 = 1 // log2(burst length)
  61. )
  62. (
  63. // Reset and clock
  64. input rst_n,
  65. input clk,
  66. // SDRAM hardware interface
  67. output sr_clk, // SDRAM clock output buffer
  68. output sr_cke, // SDRAM clock enable
  69. output sr_cs_n, // SDRAM CS#
  70. output sr_ras_n, // SDRAM RAS#
  71. output sr_cas_n, // SDRAM CAS#
  72. output sr_we_n, // SDRAM WE#
  73. output [1:0] sr_dqm, // SDRAM DQM (per byte)
  74. output [1:0] sr_ba, // SDRAM bank selects
  75. output [12:0] sr_a, // SDRAM address bus
  76. inout [15:0] sr_dq, // SDRAM data bus
  77. // Port 0: single byte, high priority
  78. input [24:0] a0, // Address, must be stable until ack
  79. output reg [7:0] rd0, // Data from SDRAM
  80. input rrq0, // Read request
  81. output reg rack0, // Read ack (transaction started)
  82. output reg rready0, // Read data valid
  83. input [7:0] wd0, // Data to SDRAM
  84. input wrq0, // Write request
  85. output reg wack0, // Write ack (data latched)
  86. // Port 1
  87. input [24:2] a1,
  88. output reg [31:0] rd1,
  89. input rrq1,
  90. output reg rack1,
  91. output reg rready1,
  92. input [31:0] wd1,
  93. input [3:0] wstrb1,
  94. output reg wack1
  95. );
  96. `include "functions.sv" // For modelsim
  97. wire wrq1 = |wstrb1;
  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. assign sr_cke = 1'b1;
  134. // SDRAM output clock buffer. The SDRAM output clock is
  135. // inverted with respect to our internal clock, so that
  136. // the SDRAM sees the positive clock edge in the middle of
  137. // our clocks.
  138. //
  139. // Use a DDIO buffer for best performance
  140. // For EP4CE15 only could use a secondary PLL here, but it
  141. // isn't clear it buys us a whole lot.
  142. ddio_out sr_clk_out (
  143. .aclr ( 1'b0 ),
  144. .datain_h ( 1'b1 ),
  145. .datain_l ( 1'b0 ),
  146. .outclock ( clk ),
  147. .dataout ( sr_clk )
  148. );
  149. // SDRAM output signal registers
  150. reg [12:0] dram_a;
  151. assign sr_a = dram_a;
  152. reg [1:0] dram_ba;
  153. assign sr_ba = dram_ba;
  154. reg [1:0] dram_dqm;
  155. assign sr_dqm = dram_dqm;
  156. reg [15:0] dram_d; // Data to DRAM
  157. reg dram_d_en; // Drive data out
  158. assign sr_dq = dram_d_en ? dram_d : 16'hzzzz;
  159. // State machine and counters
  160. reg [t_refi_lg2-2:0] rfsh_ctr; // Refresh timer
  161. wire rfsh_ctr_msb = rfsh_ctr[t_refi_lg2-2];
  162. reg rfsh_ctr_last_msb;
  163. wire rfsh_tick = rfsh_ctr_last_msb & ~rfsh_ctr_msb;
  164. reg [t_p_lg2:t_refi_lg2-1] init_ctr; // Reset to init counter
  165. reg [1:0] rfsh_prio; // Refresh priority
  166. // Bit 0 - refresh if opportune
  167. // Bit 1 - refresh urgent
  168. // The actual values are unimportant; the compiler will optimize
  169. // the state machine implementation.
  170. typedef enum logic [2:0] {
  171. st_reset, // Reset until init timer expires
  172. st_init_rfsh, // Refresh cycles during initialization
  173. st_init_mrd, // MRD register write during initialization
  174. st_idle, // Idle state: all banks precharged
  175. st_rfsh,
  176. st_rd_wr,
  177. st_pre_idle
  178. } state_t;
  179. state_t state = st_reset;
  180. reg is_write;
  181. always @(posedge clk or negedge rst_n)
  182. if (~rst_n)
  183. begin
  184. rfsh_ctr <= 1'b0;
  185. rfsh_prio <= 2'b00;
  186. init_ctr <= 1'b0;
  187. end
  188. else
  189. begin
  190. rfsh_ctr <= rfsh_ctr + 1'b1;
  191. rfsh_ctr_last_msb <= rfsh_ctr_msb;
  192. // Refresh priority management
  193. if (is_rfsh)
  194. rfsh_prio <= 2'b00; // This is a refresh cycle
  195. else if (rfsh_tick)
  196. rfsh_prio <= { rfsh_prio[0], 1'b1 };
  197. // The refresh counter is also used as a prescaler
  198. // for the initialization counter.
  199. // Note that means init_ctr is two cycles "behind"
  200. // rfsh_ctr; this is totally fine.
  201. init_ctr <= init_ctr + rfsh_tick;
  202. end // else: !if(~rst_n)
  203. reg [3:0] op_cycle; // Cycle into the current operation
  204. reg op_zero; // op_cycle wrap around
  205. reg [1:0] init_op_ctr; // op_cycle extension for init states
  206. reg [31:0] wdata_q;
  207. reg [ 3:0] be_q;
  208. reg [ 9:0] col_addr;
  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. This affects read timing.
  213. //
  214. // Note that rready starts out as 1. This allows a 0->1 detection
  215. // on the rready line to be used as cycle termination signal.
  216. //
  217. always @(posedge clk or negedge rst_n)
  218. if (~rst_n)
  219. begin
  220. dram_cmd <= cmd_desl;
  221. dram_a <= 13'hxxxx;
  222. dram_ba <= 2'bxx;
  223. dram_dqm <= 2'b00;
  224. dram_d <= 16'hxxxx;
  225. dram_d_en <= 1'b1; // Don't float except during read
  226. op_cycle <= 4'h0;
  227. op_zero <= 1'b0;
  228. init_op_ctr <= 2'b00;
  229. state <= st_reset;
  230. is_write <= 1'bx;
  231. rack0 <= 1'b0;
  232. rready0 <= 1'b1;
  233. wack0 <= 1'b0;
  234. rack1 <= 1'b0;
  235. rready1 <= 1'b1;
  236. wack1 <= 1'b0;
  237. wdata_q <= 32'hxxxx_xxxx;
  238. be_q <= 4'bxxxx;
  239. col_addr <= 10'hxxx;
  240. end
  241. else
  242. begin
  243. // Default values
  244. // Note: dram_ba are preserved
  245. dram_a <= 13'hxxxx;
  246. dram_dqm <= 2'b00;
  247. dram_d <= 16'haaaa;
  248. dram_cmd <= cmd_nop;
  249. dram_d_en <= 1'b1; // Don't float except during read
  250. if (state != st_rd_wr)
  251. begin
  252. rack0 <= 1'b0;
  253. wack0 <= 1'b0;
  254. rack1 <= 1'b0;
  255. wack1 <= 1'b0;
  256. end
  257. if (state == st_reset || state == st_idle)
  258. op_cycle <= 1'b0;
  259. else
  260. op_cycle <= op_cycle + 1'b1;
  261. op_zero <= |op_cycle;
  262. if (|op_cycle)
  263. init_op_ctr <= init_op_ctr + 1'b1;
  264. case (state)
  265. st_reset:
  266. begin
  267. dram_a[10] <= 1'b1; // Precharge all banks
  268. dram_cmd <= cmd_nop;
  269. if (init_ctr[t_p_lg2])
  270. begin
  271. dram_cmd <= cmd_pre;
  272. state <= st_init_rfsh;
  273. end
  274. end
  275. st_init_rfsh:
  276. begin
  277. if (op_zero)
  278. begin
  279. dram_cmd <= cmd_ref;
  280. if (init_op_ctr == 2'b11)
  281. state <= st_init_mrd;
  282. end
  283. end
  284. st_init_mrd:
  285. begin
  286. dram_a <= mrd_val;
  287. dram_ba <= 2'b00;
  288. if (op_zero)
  289. if (init_op_ctr[0])
  290. state <= st_idle;
  291. else
  292. dram_cmd <= cmd_mrd;
  293. end
  294. st_idle:
  295. begin
  296. is_write <= 1'bx;
  297. be_q <= 4'bxxxx;
  298. wdata_q <= 32'hxxxx_xxxx;
  299. // A data transaction starts with ACTIVE command;
  300. // a refresh transaction starts with REFRESH.
  301. // Port 0 has the highest priority, then
  302. // refresh, then port 1; a refresh transaction
  303. // is started opportunistically if nothing is
  304. // pending and the refresh counter is no less than
  305. // half expired.
  306. dram_d <= 16'hbbbb;
  307. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  308. 4'b1???:
  309. begin
  310. // Begin port 0 transaction
  311. dram_cmd <= cmd_act;
  312. dram_a <= a0[24:12];
  313. dram_ba <= a0[11:10];
  314. col_addr <= a0[9:0];
  315. if ( wrq0 )
  316. begin
  317. state <= st_rd_wr;
  318. wack0 <= 1'b1;
  319. wdata_q <= {16'hxxxx, wd0, wd0};
  320. be_q <= {2'b00, a0[0], ~a0[0]};
  321. is_write <= 1'b1;
  322. end
  323. else
  324. begin
  325. state <= st_rd_wr;
  326. rack0 <= 1'b1;
  327. rready0 <= 1'b0;
  328. is_write <= 1'b0;
  329. end
  330. end
  331. 4'b010?:
  332. begin
  333. // Begin port 1 transaction
  334. dram_cmd <= cmd_act;
  335. dram_a <= a1[24:12];
  336. dram_ba <= a1[11:10];
  337. col_addr <= { a1[9:2], 2'b00 };
  338. if ( wrq1 )
  339. begin
  340. state <= st_rd_wr;
  341. wack1 <= 1'b1;
  342. wdata_q <= wd1;
  343. be_q <= wstrb1;
  344. is_write <= 1'b1;
  345. end
  346. else
  347. begin
  348. state <= st_rd_wr;
  349. rack1 <= 1'b1;
  350. rready1 <= 1'b0;
  351. is_write <= 1'b0;
  352. end
  353. end
  354. 4'b0?1?, 4'b0001:
  355. begin
  356. // Begin refresh transaction
  357. dram_cmd <= cmd_ref;
  358. state <= st_rfsh;
  359. end
  360. default:
  361. begin
  362. dram_cmd <= cmd_desl;
  363. state <= st_idle;
  364. end
  365. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  366. end // case: st_idle
  367. st_rfsh:
  368. begin
  369. if (op_cycle == t_rfc-2)
  370. state <= st_idle;
  371. end
  372. st_rd_wr:
  373. begin
  374. dram_d_en <= is_write;
  375. dram_dqm <= {2{is_write}};
  376. dram_d <= 16'hcccc;
  377. // Commands
  378. //
  379. // This assumes:
  380. // tRCD = 3
  381. // rRRD = 2
  382. // CL = 3
  383. // tRC = 10
  384. // tRAS = 7
  385. // tRP = 3
  386. //
  387. case (op_cycle)
  388. 2: begin
  389. dram_a[10] <= 1'b0; // No auto precharge
  390. dram_a[8:0] <= col_addr[9:1];
  391. dram_cmd <= is_write ? cmd_wr : cmd_rd;
  392. dram_d <= wdata_q[15:0];
  393. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  394. wdata_q <= { 16'hdddd, wdata_q[31:16] };
  395. be_q <= { 2'hxx, be_q[3:2] };
  396. end
  397. 3: begin
  398. dram_d <= wdata_q[15:0];
  399. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  400. wdata_q <= { 16'heeee, wdata_q[31:16] };
  401. be_q <= 4'bxxxx;
  402. end
  403. 6: begin
  404. // Earliest legal cycle to precharge
  405. // It seems auto precharge violates tRAS(?)
  406. // so do it explicitly.
  407. dram_a[10] <= 1'b1; // One bank
  408. dram_cmd <= cmd_pre;
  409. end
  410. // CL+2 cycles after the read command
  411. // The +2 accounts for internal and I/O delays
  412. 7: begin
  413. if (rack0)
  414. rd0 <= col_addr[0] ? sr_dq[15:8] : sr_dq[7:0];
  415. rready0 <= rready0 | rack0;
  416. if (rack1)
  417. rd1[15:0] <= sr_dq;
  418. end
  419. 8: begin
  420. if (rack1)
  421. rd1[31:16] <= sr_dq;
  422. rready1 <= rready1 | rack1;
  423. state <= st_pre_idle;
  424. end
  425. endcase // case (op_cycle)
  426. end // case: st_rd_wr
  427. st_pre_idle:
  428. begin
  429. // Last cycle before tRC is a separate state
  430. // so that rack/wack will be cleared
  431. dram_d_en <= is_write;
  432. dram_dqm <= {2{is_write}};
  433. state <= st_idle;
  434. end
  435. endcase // case(state)
  436. end // else: !if(~rst_n)
  437. endmodule // dram