sdram.sv 16 KB

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