sdram.sv 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 [3:0] op_cycle; // Cycle into the current operation
  222. reg op_zero; // op_cycle wrap around
  223. reg [1:0] init_op_ctr; // op_cycle extension for init states
  224. reg [31:0] wdata_q;
  225. reg [ 3:0] be_q;
  226. reg [ 9:0] col_addr;
  227. reg wrq2_more;
  228. //
  229. // Careful with the timing here... there is one cycle between
  230. // registers and wires, and the DRAM observes the clock 1/2
  231. // cycle from the internal logic. This affects read timing.
  232. //
  233. // Note that rready starts out as 1. This allows a 0->1 detection
  234. // on the rready line to be used as cycle termination signal.
  235. //
  236. always @(posedge clk or negedge rst_n)
  237. if (~rst_n)
  238. begin
  239. dram_cmd <= cmd_desl;
  240. dram_a <= 13'hxxxx;
  241. dram_ba <= 2'bxx;
  242. dram_dqm <= 2'b00;
  243. dram_d <= 16'hxxxx;
  244. dram_d_en <= 1'b1; // Don't float except during read
  245. op_cycle <= 4'h0;
  246. op_zero <= 1'b0;
  247. init_op_ctr <= 2'b00;
  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. op_cycle <= 1'b0;
  281. else
  282. op_cycle <= op_cycle + 1'b1;
  283. op_zero <= |op_cycle;
  284. if (|op_cycle)
  285. init_op_ctr <= init_op_ctr + 1'b1;
  286. case (state)
  287. st_reset:
  288. begin
  289. dram_a[10] <= 1'b1; // Precharge all banks
  290. dram_cmd <= cmd_nop;
  291. if (init_ctr[t_p_lg2])
  292. begin
  293. dram_cmd <= cmd_pre;
  294. state <= st_init_rfsh;
  295. end
  296. end
  297. st_init_rfsh:
  298. begin
  299. if (op_zero)
  300. begin
  301. dram_cmd <= cmd_ref;
  302. if (init_op_ctr == 2'b11)
  303. state <= st_init_mrd;
  304. end
  305. end
  306. st_init_mrd:
  307. begin
  308. dram_a <= mrd_val;
  309. dram_ba <= 2'b00;
  310. if (op_zero)
  311. if (init_op_ctr[0])
  312. state <= st_idle;
  313. else
  314. dram_cmd <= cmd_mrd;
  315. end
  316. st_idle:
  317. begin
  318. is_write <= 1'bx;
  319. be_q <= 4'bxxxx;
  320. wdata_q <= 32'hxxxx_xxxx;
  321. // A data transaction starts with ACTIVE command;
  322. // a refresh transaction starts with REFRESH.
  323. // Port 0 has the highest priority, then
  324. // refresh, then port 1; a refresh transaction
  325. // is started opportunistically if nothing is
  326. // pending and the refresh counter is no less than
  327. // half expired.
  328. dram_a <= 13'h1bb;
  329. dram_ba <= 2'bxx;
  330. dram_d <= 16'hbbbb;
  331. casez ( {rrq0|wrq0, rrq1|wrq1, wrq2[0], rfsh_prio} )
  332. 5'b1????:
  333. begin
  334. // Begin port 0 transaction
  335. dram_cmd <= cmd_act;
  336. dram_a <= a0[24:12];
  337. dram_ba <= a0[11:10];
  338. col_addr <= a0[9:0];
  339. if ( wrq0 )
  340. begin
  341. state <= st_rd_wr;
  342. wack0 <= 1'b1;
  343. wdata_q <= {16'hxxxx, wd0, wd0};
  344. be_q <= {2'b00, a0[0], ~a0[0]};
  345. is_write <= 1'b1;
  346. end
  347. else
  348. begin
  349. state <= st_rd_wr;
  350. rack0 <= 1'b1;
  351. rready0 <= 1'b0;
  352. is_write <= 1'b0;
  353. end
  354. end
  355. 5'b01?0?:
  356. begin
  357. // Begin port 1 transaction
  358. dram_cmd <= cmd_act;
  359. dram_a <= a1[24:12];
  360. dram_ba <= a1[11:10];
  361. col_addr <= { a1[9:2], 2'b00 };
  362. if ( wrq1 )
  363. begin
  364. state <= st_rd_wr;
  365. wack1 <= 1'b1;
  366. wdata_q <= wd1;
  367. be_q <= wstrb1;
  368. is_write <= 1'b1;
  369. end
  370. else
  371. begin
  372. state <= st_rd_wr;
  373. rack1 <= 1'b1;
  374. rready1 <= 1'b0;
  375. is_write <= 1'b0;
  376. end
  377. end
  378. 5'b0??1?, 5'b00?01:
  379. begin
  380. // Begin refresh transaction
  381. dram_cmd <= cmd_ref;
  382. state <= st_rfsh;
  383. end
  384. 5'b00100:
  385. begin
  386. // Begin port 2 write
  387. dram_cmd <= cmd_act;
  388. dram_a <= a2[24:12];
  389. dram_ba <= a2[11:10];
  390. state <= st_wr2;
  391. end
  392. default:
  393. begin
  394. dram_cmd <= cmd_desl;
  395. state <= st_idle;
  396. end
  397. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  398. end // case: st_idle
  399. st_rfsh:
  400. begin
  401. if (op_cycle == t_rfc-2)
  402. state <= st_idle;
  403. end
  404. st_rd_wr:
  405. begin
  406. dram_d_en <= is_write;
  407. dram_dqm <= {2{is_write}};
  408. dram_d <= 16'hcccc;
  409. // Commands
  410. //
  411. // This assumes:
  412. // tRCD = 3
  413. // rRRD = 2
  414. // CL = 3
  415. // tRC = 10
  416. // tRAS = 7
  417. // tWR = 2
  418. // tRP = 3
  419. //
  420. case (op_cycle)
  421. 2: begin
  422. dram_a[10] <= 1'b0; // No auto precharge
  423. dram_a[8:0] <= col_addr[9:1];
  424. dram_cmd <= is_write ? cmd_wr : cmd_rd;
  425. dram_d <= wdata_q[15:0];
  426. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  427. wdata_q <= { 16'hdddd, wdata_q[31:16] };
  428. be_q <= { 2'hxx, be_q[3:2] };
  429. end
  430. 3: begin
  431. dram_d <= wdata_q[15:0];
  432. dram_dqm <= {2{is_write}} & ~be_q[1:0];
  433. wdata_q <= { 16'heeee, wdata_q[31:16] };
  434. be_q <= 4'bxxxx;
  435. end
  436. 6: begin
  437. // Earliest legal cycle to precharge
  438. // It seems auto precharge violates tRAS(?)
  439. // so do it explicitly.
  440. dram_a[10] <= 1'b1; // One bank
  441. dram_cmd <= cmd_pre;
  442. end
  443. // CL+2 cycles after the read command
  444. // The +2 accounts for internal and I/O delays
  445. 7: begin
  446. if (rack0)
  447. rd0 <= col_addr[0] ? sr_dq[15:8] : sr_dq[7:0];
  448. rready0 <= rready0 | rack0;
  449. if (rack1)
  450. rd1[15:0] <= sr_dq;
  451. end
  452. 8: begin
  453. if (rack1)
  454. rd1[31:16] <= sr_dq;
  455. rready1 <= rready1 | rack1;
  456. state <= st_pre_idle;
  457. end
  458. endcase // case (op_cycle)
  459. end // case: st_rd_wr
  460. st_pre_idle:
  461. begin
  462. // Last cycle before tRC is a separate state
  463. // so that rack/wack will be cleared
  464. dram_d_en <= is_write;
  465. dram_dqm <= {2{is_write}};
  466. state <= st_idle;
  467. end
  468. st_wr2:
  469. begin
  470. // Streamable write from flash ROM
  471. dram_d <= wd2;
  472. dram_a[10] <= 1'b0; // No auto precharge/precharge one bank
  473. dram_a[8:0] <= a2[9:1];
  474. case (op_cycle)
  475. 0: begin
  476. wacc2 <= 1'b1;
  477. end
  478. 1: begin
  479. wacc2 <= 1'b1;
  480. end
  481. 2: begin
  482. dram_cmd <= cmd_wr;
  483. wacc2 <= 1'b1;
  484. wrq2_more <= wrq2[1];
  485. end
  486. 3: begin
  487. wacc2 <= 1'b1;
  488. end
  489. 4: begin
  490. dram_cmd <= cmd_wr;
  491. if (wrq2_more &
  492. ~(rrq0|wrq0|rrq1|wrq1|(|rfsh_prio)|(&dram_a[8:2])))
  493. begin
  494. // Burst can continue
  495. wacc2 <= 1'b1;
  496. op_cycle <= 1;
  497. end
  498. end // case: 4
  499. 6: begin
  500. dram_dqm <= 2'b11; // This shouldn't be necessary?!
  501. end
  502. 7: begin
  503. // tWR completed
  504. dram_cmd <= cmd_pre;
  505. dram_dqm <= 2'b11;
  506. end
  507. 8: begin
  508. dram_dqm <= 2'b11;
  509. end
  510. 9: begin
  511. // tRP will be complete in the next cycle
  512. dram_dqm <= 2'b11;
  513. state <= st_idle;
  514. end
  515. endcase // case (op_cycle)
  516. end // case: st_wr2
  517. endcase // case(state)
  518. end // else: !if(~rst_n)
  519. endmodule // dram