2
0

sdram.sv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // 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'b1 ),
  144. .datain_l ( 1'b0 ),
  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. // State machine and counters
  161. reg [t_refi_lg2-2:0] rfsh_ctr; // Refresh timer
  162. wire rfsh_ctr_msb = rfsh_ctr[t_refi_lg2-2];
  163. reg rfsh_ctr_last_msb;
  164. wire rfsh_tick = rfsh_ctr_last_msb & ~rfsh_ctr_msb;
  165. reg [t_p_lg2:t_refi_lg2-1] init_ctr; // Reset to init counter
  166. reg [1:0] rfsh_prio; // Refresh priority
  167. // Bit 0 - refresh if opportune
  168. // Bit 1 - refresh urgent
  169. // The actual values are unimportant; the compiler will optimize
  170. // the state machine implementation.
  171. typedef enum logic [2:0] {
  172. st_reset, // Reset until init timer expires
  173. st_init_rfsh, // Refresh cycles during initialization
  174. st_init_mrd, // MRD register write during initialization
  175. st_idle, // Idle state: all banks precharged
  176. st_rfsh,
  177. st_rd_wr,
  178. st_pre_idle
  179. } state_t;
  180. state_t state = st_reset;
  181. reg is_write;
  182. always @(posedge clk or negedge rst_n)
  183. if (~rst_n)
  184. begin
  185. rfsh_ctr <= 1'b0;
  186. rfsh_prio <= 2'b00;
  187. init_ctr <= 1'b0;
  188. end
  189. else
  190. begin
  191. rfsh_ctr <= rfsh_ctr + 1'b1;
  192. rfsh_ctr_last_msb <= rfsh_ctr_msb;
  193. // Refresh priority management
  194. if (is_rfsh)
  195. rfsh_prio <= 2'b00; // This is a refresh cycle
  196. else if (rfsh_tick)
  197. rfsh_prio <= { rfsh_prio[0], 1'b1 };
  198. // The refresh counter is also used as a prescaler
  199. // for the initialization counter.
  200. // Note that means init_ctr is two cycles "behind"
  201. // rfsh_ctr; this is totally fine.
  202. init_ctr <= init_ctr + rfsh_tick;
  203. end // else: !if(~rst_n)
  204. reg [3:0] op_cycle; // Cycle into the current operation
  205. reg op_zero; // op_cycle wrap around
  206. reg [1:0] init_op_ctr; // op_cycle extension for init states
  207. reg [31:0] wdata_q;
  208. reg [ 3:0] be_q;
  209. reg [ 9:0] col_addr;
  210. //
  211. // Careful with the timing here... there is one cycle between
  212. // registers and wires, and the DRAM observes the clock 1/2
  213. // cycle from the internal logic. This affects read timing.
  214. //
  215. // Note that rready starts out as 1. This allows a 0->1 detection
  216. // on the rready line to be used as cycle termination signal.
  217. //
  218. always @(posedge clk or negedge rst_n)
  219. if (~rst_n)
  220. begin
  221. dram_cke <= 1'b0;
  222. dram_cmd <= cmd_desl;
  223. dram_a <= 13'hxxxx;
  224. dram_ba <= 2'bxx;
  225. dram_dqm <= 2'b00;
  226. dram_d <= 16'hxxxx;
  227. dram_d_en <= 1'b1; // Don't float except during read
  228. op_cycle <= 4'h0;
  229. op_zero <= 1'b0;
  230. init_op_ctr <= 2'b00;
  231. state <= st_reset;
  232. is_write <= 1'bx;
  233. rack0 <= 1'b0;
  234. rready0 <= 1'b1;
  235. wack0 <= 1'b0;
  236. rack1 <= 1'b0;
  237. rready1 <= 1'b1;
  238. wack1 <= 1'b0;
  239. wdata_q <= 32'hxxxx_xxxx;
  240. be_q <= 4'bxxxx;
  241. col_addr <= 10'hxxx;
  242. end
  243. else
  244. begin
  245. dram_cke <= 1'b1; // Always true once out of reset
  246. // Default values
  247. // Note: dram_ba are preserved
  248. dram_a <= 13'hxxxx;
  249. dram_dqm <= 2'b00;
  250. dram_d <= 16'haaaa;
  251. dram_cmd <= cmd_nop;
  252. dram_d_en <= 1'b1; // Don't float except during read
  253. if (state != st_rd_wr)
  254. begin
  255. rack0 <= 1'b0;
  256. wack0 <= 1'b0;
  257. rack1 <= 1'b0;
  258. wack1 <= 1'b0;
  259. end
  260. if (state == st_reset || state == st_idle)
  261. op_cycle <= 1'b0;
  262. else
  263. op_cycle <= op_cycle + 1'b1;
  264. op_zero <= |op_cycle;
  265. if (|op_cycle)
  266. init_op_ctr <= init_op_ctr + 1'b1;
  267. case (state)
  268. st_reset:
  269. begin
  270. dram_a[10] <= 1'b1; // Precharge all banks
  271. dram_cmd <= cmd_nop;
  272. if (init_ctr[t_p_lg2])
  273. begin
  274. dram_cmd <= cmd_pre;
  275. state <= st_init_rfsh;
  276. end
  277. end
  278. st_init_rfsh:
  279. begin
  280. if (op_zero)
  281. begin
  282. dram_cmd <= cmd_ref;
  283. if (init_op_ctr == 2'b11)
  284. state <= st_init_mrd;
  285. end
  286. end
  287. st_init_mrd:
  288. begin
  289. dram_a <= mrd_val;
  290. dram_ba <= 2'b00;
  291. if (op_zero)
  292. if (init_op_ctr[0])
  293. state <= st_idle;
  294. else
  295. dram_cmd <= cmd_mrd;
  296. end
  297. st_idle:
  298. begin
  299. is_write <= 1'bx;
  300. be_q <= 4'bxxxx;
  301. wdata_q <= 32'hxxxx_xxxx;
  302. // A data transaction starts with ACTIVE command;
  303. // a refresh transaction starts with REFRESH.
  304. // Port 0 has the highest priority, then
  305. // refresh, then port 1; a refresh transaction
  306. // is started opportunistically if nothing is
  307. // pending and the refresh counter is no less than
  308. // half expired.
  309. dram_d <= 16'hbbbb;
  310. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  311. 4'b1???:
  312. begin
  313. // Begin port 0 transaction
  314. dram_cmd <= cmd_act;
  315. dram_a <= a0[24:12];
  316. dram_ba <= a0[11:10];
  317. col_addr <= a0[9:0];
  318. if ( wrq0 )
  319. begin
  320. state <= st_rd_wr;
  321. wack0 <= 1'b1;
  322. wdata_q <= {16'hxxxx, wd0, wd0};
  323. be_q <= {2'b00, a0[0], ~a0[0]};
  324. is_write <= 1'b1;
  325. end
  326. else
  327. begin
  328. state <= st_rd_wr;
  329. rack0 <= 1'b1;
  330. rready0 <= 1'b0;
  331. is_write <= 1'b0;
  332. end
  333. end
  334. 4'b010?:
  335. begin
  336. // Begin port 1 transaction
  337. dram_cmd <= cmd_act;
  338. dram_a <= a1[24:12];
  339. dram_ba <= a1[11:10];
  340. col_addr <= { a1[9:2], 2'b00 };
  341. if ( wrq1 )
  342. begin
  343. state <= st_rd_wr;
  344. wack1 <= 1'b1;
  345. wdata_q <= wd1;
  346. be_q <= wstrb1;
  347. is_write <= 1'b1;
  348. end
  349. else
  350. begin
  351. state <= st_rd_wr;
  352. rack1 <= 1'b1;
  353. rready1 <= 1'b0;
  354. is_write <= 1'b0;
  355. end
  356. end
  357. 4'b0?1?, 4'b0001:
  358. begin
  359. // Begin refresh transaction
  360. dram_cmd <= cmd_ref;
  361. state <= st_rfsh;
  362. end
  363. default:
  364. begin
  365. dram_cmd <= cmd_desl;
  366. state <= st_idle;
  367. end
  368. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  369. end // case: st_idle
  370. st_rfsh:
  371. begin
  372. if (op_cycle == t_rfc-2)
  373. state <= st_idle;
  374. end
  375. st_rd_wr:
  376. begin
  377. dram_d_en <= is_write;
  378. dram_dqm <= {2{is_write}};
  379. dram_d <= 16'hcccc;
  380. // Commands
  381. //
  382. // This assumes:
  383. // tRCD = 3
  384. // rRRD = 2
  385. // CL = 3
  386. // tRC = 10
  387. // tRAS = 7
  388. // tRP = 3
  389. //
  390. case (op_cycle)
  391. 2: begin
  392. dram_a[10] <= 1'b0; // No auto precharge
  393. dram_a[8:0] <= col_addr[9:1];
  394. dram_cmd <= is_write ? cmd_wr : cmd_rd;
  395. dram_d <= wdata_q[15:0];
  396. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  397. wdata_q <= { 16'hdddd, wdata_q[31:16] };
  398. be_q <= { 2'hxx, be_q[3:2] };
  399. end
  400. 3: begin
  401. dram_d <= wdata_q[15:0];
  402. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  403. wdata_q <= { 16'heeee, wdata_q[31:16] };
  404. be_q <= 4'bxxxx;
  405. end
  406. 6: begin
  407. // Earliest legal cycle to precharge
  408. // It seems auto precharge violates tRAS(?)
  409. // so do it explicitly.
  410. dram_a[10] <= 1'b1; // One bank
  411. dram_cmd <= cmd_pre;
  412. end
  413. // CL+2 cycles after the read command
  414. // The +2 accounts for internal and I/O delays
  415. 7: begin
  416. if (rack0)
  417. rd0 <= col_addr[0] ? sr_dq[15:8] : sr_dq[7:0];
  418. rready0 <= rready0 | rack0;
  419. if (rack1)
  420. rd1[15:0] <= sr_dq;
  421. end
  422. 8: begin
  423. if (rack1)
  424. rd1[31:16] <= sr_dq;
  425. rready1 <= rready1 | rack1;
  426. state <= st_pre_idle;
  427. end
  428. endcase // case (op_cycle)
  429. end // case: st_rd_wr
  430. st_pre_idle:
  431. begin
  432. // Last cycle before tRC is a separate state
  433. // so that rack/wack will be cleared
  434. dram_d_en <= is_write;
  435. dram_dqm <= {2{is_write}};
  436. state <= st_idle;
  437. end
  438. endcase // case(state)
  439. end // else: !if(~rst_n)
  440. endmodule // dram