sdram.sv 15 KB

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