sdram.sv 15 KB

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