2
0

sdram.sv 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. // 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. // I/O cell input register for SDRAM data
  161. reg [15:0] dram_q;
  162. always @(posedge clk)
  163. dram_q <= sr_dq;
  164. // State machine and counters
  165. reg [t_refi_lg2-2:0] rfsh_ctr; // Refresh timer
  166. wire rfsh_ctr_msb = rfsh_ctr[t_refi_lg2-2];
  167. reg rfsh_ctr_last_msb;
  168. wire rfsh_tick = rfsh_ctr_last_msb & ~rfsh_ctr_msb;
  169. reg [t_p_lg2:t_refi_lg2-1] init_ctr; // Reset to init counter
  170. reg [1:0] rfsh_prio; // Refresh priority
  171. // Bit 0 - refresh if opportune
  172. // Bit 1 - refresh urgent
  173. reg [3:0] op_cycle; // Cycles into the current operation
  174. // The actual values are unimportant; the compiler will optimize
  175. // the state machine implementation.
  176. typedef enum logic [2:0] {
  177. st_reset, // Reset until init timer expires
  178. st_init, // 1st refresh during initialization
  179. st_idle, // Idle state: all banks precharged
  180. st_rfsh,
  181. st_rd,
  182. st_wr
  183. } state_t;
  184. state_t state = st_reset;
  185. always @(posedge clk or negedge rst_n)
  186. if (~rst_n)
  187. begin
  188. rfsh_ctr <= 1'b0;
  189. init_ctr <= 1'b0;
  190. rfsh_prio <= 2'b00;
  191. end
  192. else
  193. begin
  194. rfsh_ctr <= rfsh_ctr + 1'b1;
  195. rfsh_ctr_last_msb <= rfsh_ctr_msb;
  196. // Refresh priority management
  197. if (is_rfsh)
  198. rfsh_prio <= 2'b00; // This is a refresh cycle
  199. else if (rfsh_tick)
  200. rfsh_prio <= { rfsh_prio[0], 1'b1 };
  201. // The refresh counter is also used as a prescaler
  202. // for the initialization counter.
  203. // Note that means init_ctr is two cycles "behind"
  204. // rfsh_ctr; this is totally fine.
  205. init_ctr <= init_ctr + rfsh_tick;
  206. end // else: !if(~rst_n)
  207. // Handle bank wraparound
  208. reg last_dword; // This is the last dword in this bank
  209. reg [14:0] next_bank; // Row:bank for the next bank
  210. reg [11:2] col_addr; // Current bank:column
  211. always @(posedge clk)
  212. begin
  213. if (op_cycle == 0)
  214. begin
  215. next_bank <= { dram_a, dram_ba } + 1'b1;
  216. last_dword <= &col_addr[9:2]; // last dword in bank?
  217. end
  218. end // else: !if(~rst_n)
  219. reg [31:0] wdata_q;
  220. reg [ 7:0] be_q;
  221. //
  222. // Careful with the timing here... there is one cycle between
  223. // registers and wires, and the DRAM observes the clock 1/2
  224. // cycle from the internal logic.
  225. //
  226. always @(posedge clk or negedge rst_n)
  227. if (~rst_n)
  228. begin
  229. dram_cke <= 1'b0;
  230. dram_cmd <= cmd_desl;
  231. dram_a <= 13'hxxxx;
  232. dram_ba <= 2'bxx;
  233. dram_dqm <= 2'b00;
  234. dram_d <= 16'hxxxx;
  235. dram_d_en <= 1'b1; // Don't float except during read
  236. op_cycle <= 1'b0;
  237. state <= st_reset;
  238. rack0 <= 1'b0;
  239. rvalid0 <= 1'b0;
  240. wack0 <= 1'b0;
  241. rack1 <= 1'b0;
  242. rvalid1 <= 1'b0;
  243. wack1 <= 1'b0;
  244. wdata_q <= 32'hxxxx_xxxx;
  245. be_q <= 8'hxx;
  246. end
  247. else
  248. begin
  249. dram_cke <= 1'b1; // Always true once out of reset
  250. // Default values
  251. dram_ba <= 2'bxx;
  252. dram_dqm <= 2'b00;
  253. dram_d <= 16'hxxxx;
  254. dram_cmd <= cmd_nop;
  255. dram_d_en <= 1'b1; // Don't float except during read
  256. rack0 <= 1'b0;
  257. wack0 <= 1'b0;
  258. rack1 <= 1'b0;
  259. wack1 <= 1'b0;
  260. if (state == st_reset || state == st_idle)
  261. op_cycle <= 1'b0;
  262. else
  263. op_cycle <= op_cycle + 1'b1;
  264. case (state)
  265. st_reset:
  266. begin
  267. dram_cmd <= cmd_desl;
  268. if (init_ctr[t_p_lg2])
  269. begin
  270. dram_cmd <= cmd_pre;
  271. dram_a[10] <= 1'b1; // Precharge All Banks
  272. state <= st_init;
  273. end
  274. end
  275. st_init:
  276. begin
  277. // Add 3 to the count to account for skew between rfsh_ctr
  278. // and init_ctr
  279. if ( rfsh_ctr[4:0] == t_rp+3 || rfsh_ctr[4:0] == t_rp+t_rfc+3 )
  280. begin
  281. dram_cmd <= cmd_ref;
  282. end
  283. if ( rfsh_ctr[4:0] == t_rp+t_rfc*2+3 )
  284. begin
  285. dram_cmd <= cmd_mrd;
  286. dram_a <= mrd_val;
  287. end
  288. if ( rfsh_ctr[4:0] >= t_rp+t_rfc*2+t_mrd-1+3 )
  289. state <= st_idle;
  290. end // case: st_init
  291. st_idle:
  292. begin
  293. // A data transaction starts with ACTIVE command;
  294. // a refresh transaction starts with REFRESH.
  295. // Port 0 has the highest priority, then
  296. // refresh, then port 1; a refresh transaction
  297. // is started opportunistically if nothing is
  298. // pending and the refresh counter is no less than
  299. // half expired.
  300. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  301. 4'b1???:
  302. begin
  303. // Begin port 0 transaction
  304. dram_cmd <= cmd_act;
  305. dram_a <= a0[24:12];
  306. dram_ba <= a0[11:10];
  307. col_addr <= a0[11:2];
  308. be_q <= 1'b1 << a0[1:0];
  309. if ( wrq0 )
  310. begin
  311. state <= st_wr;
  312. wack0 <= 1'b1;
  313. wdata_q <= {4{wd0}};
  314. end
  315. else
  316. begin
  317. state <= st_rd;
  318. rack0 <= 1'b1;
  319. rvalid0 <= 1'b0;
  320. end
  321. end
  322. 4'b010?:
  323. begin
  324. // Begin port 1 transaction
  325. dram_cmd <= cmd_act;
  326. dram_a <= a1[24:12];
  327. dram_ba <= a1[11:10];
  328. col_addr <= a1[11:2];
  329. be_q <= be1;
  330. if ( wrq1 )
  331. begin
  332. state <= st_wr;
  333. wack1 <= 1'b1;
  334. wdata_q <= wd1;
  335. end
  336. else
  337. begin
  338. state <= st_rd;
  339. rack1 <= 1'b1;
  340. rvalid1 <= 1'b0;
  341. end
  342. end
  343. 4'b0?1?, 4'b0001:
  344. begin
  345. // Begin refresh transaction
  346. dram_cmd <= cmd_ref;
  347. state <= st_rfsh;
  348. end
  349. default:
  350. begin
  351. dram_cmd <= cmd_desl;
  352. state <= st_idle;
  353. end
  354. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_ref:t_ref-1]} )
  355. end // case: st_idle
  356. st_rfsh:
  357. begin
  358. if (op_cycle == t_rfc-2)
  359. state <= st_idle;
  360. end
  361. st_rd:
  362. begin
  363. rack0 <= rack0;
  364. rack1 <= rack1;
  365. dram_d_en <= 1'b0; // Tristate data bus
  366. // Commands
  367. //
  368. // This assumes:
  369. // tRCD = 3
  370. // rRRD = 2
  371. // CL = 3
  372. // tRC = 10
  373. case (op_cycle)
  374. 1: begin
  375. dram_a <= next_bank[14:2];
  376. dram_ba <= next_bank[1:0];
  377. dram_cmd <= last_dword ? cmd_act : cmd_nop;
  378. end
  379. 2, 4: begin
  380. dram_a[12:9] <= 4'b0000;
  381. dram_a[8:1] <= col_addr[9:2];
  382. dram_a[0] <= 1'b0;
  383. dram_cmd <= cmd_rd;
  384. col_addr <= col_addr + 1'b1;
  385. end
  386. 8: begin
  387. // Precharge all banks
  388. dram_a[12:9] <= 4'b0010;
  389. dram_cmd <= cmd_pre;
  390. end
  391. 10: begin
  392. state <= st_idle;
  393. rack0 <= 1'b0;
  394. rack1 <= 1'b0;
  395. end
  396. endcase // case (op_cycle)
  397. // +2 for bus turnaround latencies
  398. // Assert rvalid in the same cycle the last requested
  399. // data byte (as given by byte enables) is latched.
  400. if (op_cycle >= 2+t_cl+2)
  401. begin
  402. if (rack0)
  403. begin
  404. rvalid0 <= ~|be_q[7:2];
  405. if (be_q[0])
  406. rd0 <= dram_q[7:0];
  407. if (be_q[1])
  408. rd0 <= dram_q[15:8];
  409. end
  410. if (rack1)
  411. begin
  412. rvalid1 <= ~|be_q[7:2];
  413. if (op_cycle[0] ^ ((2+t_cl+2) & 1'b1))
  414. begin
  415. // Odd word
  416. if (be_q[0])
  417. rd1[23:16] <= dram_q[ 7:0];
  418. if (be_q[1])
  419. rd1[31:24] <= dram_q[15:8];
  420. end
  421. else
  422. begin
  423. // Even word
  424. if (be_q[0])
  425. rd1[ 7:0] <= dram_q[ 7:0];
  426. if (be_q[1])
  427. rd1[15:8] <= dram_q[15:8];
  428. end // else: !if(op_cycle[0] ^ ((t_rcd-1+t_cl+2) & 1'b1))
  429. end // if (rack1)
  430. be_q <= be_q >> 2;
  431. end // if (op_cycle >= 2+t_cl+2)
  432. end // case: st_rd
  433. st_wr:
  434. begin
  435. wack0 <= wack0;
  436. wack1 <= wack1;
  437. dram_dqm <= 2'b11; // Mask data bytes unless we mean it
  438. // Commands
  439. //
  440. // This assumes:
  441. // tRCD = 3
  442. // tRRD = 2
  443. // CL = 3
  444. // tRC = 10
  445. // tWR = 2
  446. // tRP = 3
  447. case (op_cycle)
  448. 1: begin
  449. dram_a <= next_bank[14:2];
  450. dram_ba <= next_bank[1:0];
  451. dram_cmd <= last_dword ? cmd_act : cmd_nop;
  452. end
  453. 2, 4: begin
  454. dram_a[12:9] <= 4'b0000;
  455. dram_a[8:1] <= col_addr[9:2];
  456. dram_a[0] <= 1'b0;
  457. dram_cmd <= cmd_wr;
  458. col_addr <= col_addr + 1'b1;
  459. end
  460. 7: begin
  461. // Precharge all banks
  462. dram_a[12:9] <= 4'b0010;
  463. dram_cmd <= cmd_pre;
  464. end
  465. 10: begin
  466. state <= st_idle;
  467. wack0 <= 1'b0;
  468. wack1 <= 1'b0;
  469. end
  470. endcase // case (op_cycle)
  471. if (op_cycle >= 2)
  472. begin
  473. dram_d <= wdata_q[15:0];
  474. // Flip data between odd and even words
  475. wdata_q <= { wdata_q[15:0], wdata_q[31:16] };
  476. dram_dqm <= ~be_q[1:0];
  477. be_q <= be_q >> 2;
  478. end
  479. end // case: st_wr
  480. endcase // case(state)
  481. end // else: !if(~rst_n)
  482. endmodule // dram