sdram.sv 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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:
  19. // Port 1 does aligned 4-byte accesses with byte enables.
  20. // Port 2 does aligned 8-byte accesses, write only, with no byte
  21. // enables; it supports streaming from a FIFO.
  22. //
  23. // Port 1 is multiplexed via an arbiter, which receives a bus
  24. // defined by the sdram_bus interface.
  25. //
  26. // All signals are in the sdram clock domain.
  27. //
  28. // [rw]ack is asserted at the beginning of a read- or write cycle and
  29. // deasserted afterwards; rready is asserted once all data is read and
  30. // the read data (rdX port) is valid; it remains asserted after the
  31. // transaction is complete and rack is deasserted.
  32. //
  33. //
  34. // The interface to the port modules. The read data is 16 bits
  35. // at a time, and is only valid in the cycle rstrb[x] is asserted.
  36. //
  37. // The only output signal that is unique to this port
  38. // is "start". All other signals are broadcast.
  39. //
  40. interface dram_bus;
  41. logic [1:0] prio; // Priority vs refresh
  42. logic rst_n;
  43. logic clk;
  44. tri [24:0] addr;
  45. logic addr0; // addr[0] latched at transaction start
  46. logic [15:0] rd;
  47. logic req;
  48. logic [1:0] rstrb; // Data read strobe
  49. tri [31:0] wd;
  50. tri [3:0] wstrb;
  51. logic start; // Transaction start
  52. logic wrack; // Transaction is a write
  53. // Upstream direction
  54. modport ustr (
  55. input prio,
  56. output rst_n,
  57. output clk,
  58. input addr,
  59. output addr0,
  60. output rd,
  61. input req,
  62. output rstrb,
  63. input wd,
  64. input wstrb,
  65. output start,
  66. output wrack
  67. );
  68. // Downstream direction
  69. modport dstr (
  70. output prio,
  71. input rst_n,
  72. input clk,
  73. output addr,
  74. input addr0,
  75. input rd,
  76. output req,
  77. input rstrb,
  78. output wd,
  79. output wstrb,
  80. input start,
  81. input wrack
  82. );
  83. endinterface // dram_bus
  84. // Port into the DRAM
  85. module dram_port
  86. #(parameter width = 32)
  87. (
  88. dram_bus.dstr bus,
  89. input [1:0] prio,
  90. input [24:0] addr,
  91. output reg [width-1:0] rd,
  92. input valid,
  93. output reg ready,
  94. input [width-1:0] wd,
  95. input [(width >> 3)-1:0] wstrb
  96. );
  97. reg started;
  98. assign bus.prio = prio;
  99. assign bus.addr = addr;
  100. assign bus.req = valid & ~started;
  101. always_comb
  102. begin
  103. bus.wd = 32'hxxxx_xxxx;
  104. bus.wstrb = 4'b0000;
  105. if (width == 8)
  106. begin
  107. bus.wd[15:0] = { wd, wd };
  108. bus.wstrb[1:0] = { wstrb[0] & addr[0], wstrb[0] & ~addr[0] };
  109. end
  110. else
  111. begin
  112. bus.wd[width-1:0] = wd;
  113. bus.wstrb[(width >> 3)-1:0] = wstrb;
  114. end
  115. end
  116. always @(negedge bus.rst_n or posedge bus.clk)
  117. if (~bus.rst_n)
  118. begin
  119. ready <= 1'b0;
  120. started <= 1'b0;
  121. end
  122. else
  123. begin
  124. if (~valid)
  125. begin
  126. ready <= 1'b0;
  127. started <= 1'b0;
  128. end
  129. else if (bus.start)
  130. begin
  131. started <= 1'b1;
  132. ready <= bus.wrack;
  133. end
  134. else if (started & ~ready)
  135. begin
  136. ready <= bus.rstrb[(width - 1) >> 4];
  137. end
  138. end // else: !if(~bus.rst_n)
  139. genvar i;
  140. generate
  141. for (i = 0; i < ((width + 15) >> 4); i++)
  142. begin : w
  143. always @(posedge bus.clk)
  144. if (started & ~ready & bus.rstrb[i])
  145. begin
  146. if (width == 8)
  147. rd <= bus.addr0 ? bus.rd[15:8] : bus.rd[7:0];
  148. else
  149. rd[i*16+15:i*16] <= bus.rd;
  150. end
  151. end
  152. endgenerate
  153. endmodule // dram_port
  154. module dram_arbiter
  155. #(parameter port_count = 1)
  156. (
  157. dram_bus.ustr ustr [1:port_count],
  158. dram_bus.dstr dstr,
  159. input [1:0] rfsh_prio,
  160. output logic do_rfsh
  161. );
  162. logic [port_count:0] grant;
  163. assign grant[0] = 1'b0; // Dummy to make the below logic simpler
  164. generate
  165. genvar i;
  166. for (i = 1; i <= port_count; i++)
  167. begin : u
  168. assign ustr[i].rst_n = dstr.rst_n;
  169. assign ustr[i].clk = dstr.clk;
  170. assign ustr[i].addr0 = dstr.addr0;
  171. assign ustr[i].rd = dstr.rd;
  172. assign ustr[i].rstrb = dstr.rstrb;
  173. assign ustr[i].wrack = dstr.wrack;
  174. assign grant[i] = ~|grant[i-1:0] & ustr[i].req &
  175. (ustr[i].prio >= rfsh_prio);
  176. assign ustr[i].start = grant[i] & dstr.start;
  177. assign dstr.addr = grant[i] ? ustr[i].addr : 'bz;
  178. assign dstr.wd = grant[i] ? ustr[i].wd : 'bz;
  179. assign dstr.wstrb = grant[i] ? ustr[i].wstrb : 'bz;
  180. end // block: u
  181. endgenerate
  182. assign dstr.req = |grant;
  183. assign do_rfsh = ~|grant & |rfsh_prio;
  184. endmodule // dram_arbiter
  185. module sdram
  186. #( parameter
  187. port1_count = 1,
  188. // Timing parameters
  189. // The parameters are hardcoded for Micron MT48LC16M16A2-6A,
  190. // per datasheet:
  191. // 100 MHz 167 MHz
  192. // ----------------------------------------------------------
  193. // CL 2 3 READ to data out
  194. // tRCD 18 ns 2 3 ACTIVE to READ/WRITE
  195. // tRFC 60 ns 6 10 REFRESH to ACTIVE
  196. // tRP 18 ns 2 3 PRECHARGE to ACTIVE/REFRESH
  197. // tRAS 42 ns 5 7 ACTIVE to PRECHARGE
  198. // tRC 60 ns 6 10 ACTIVE to ACTIVE (same bank)
  199. // tRRD 12 ns 2 2 ACTICE to ACTIVE (different bank)
  200. // tWR 12 ns 2 2 Last write data to PRECHARGE
  201. // tMRD 2 2 MODE REGISTER to ACTIVE/REFRESH
  202. //
  203. // These parameters are set by power of 2:
  204. // tREFi 64/8192 ms 781 1302 Refresh time per row (max)
  205. // tP 100 us 10000 16667 Time until first command (min)
  206. t_cl = 3,
  207. t_rcd = 3,
  208. t_rfc = 10,
  209. t_rp = 3,
  210. t_ras = 7,
  211. t_rc = 10,
  212. t_rrd = 2,
  213. t_wr = 2,
  214. t_mrd = 2,
  215. t_refi_lg2 = 10, // 1024 cycles
  216. t_p_lg2 = 15, // 32768 cycles
  217. burst_lg2 = 1 // log2(burst length)
  218. )
  219. (
  220. // Reset and clock
  221. input rst_n,
  222. input clk,
  223. input init_tmr, // tRP timer
  224. input rfsh_tmr, // tREFI/2 timer
  225. // SDRAM hardware interface
  226. output sr_cs_n, // SDRAM CS#
  227. output sr_ras_n, // SDRAM RAS#
  228. output sr_cas_n, // SDRAM CAS#
  229. output sr_we_n, // SDRAM WE#
  230. output [1:0] sr_dqm, // SDRAM DQM (per byte)
  231. output [1:0] sr_ba, // SDRAM bank selects
  232. output [12:0] sr_a, // SDRAM address bus
  233. inout [15:0] sr_dq, // SDRAM data bus
  234. // Port 1
  235. dram_bus.ustr port1 [1:port1_count],
  236. // Port 2
  237. input [24:1] a2,
  238. input [15:0] wd2,
  239. input [1:0] wrq2,
  240. output reg wacc2 // Data accepted, advance data & addr
  241. );
  242. `include "functions.sv" // For modelsim
  243. // Mode register data
  244. wire mrd_wburst = 1'b1; // Write bursts enabled
  245. wire [2:0] mrd_cl = t_cl;
  246. wire [2:0] mrd_burst = burst_lg2;
  247. wire mrd_interleave = 1'b0; // Interleaved bursts
  248. wire [12:0] mrd_val = { 3'b000, // Reserved
  249. ~mrd_wburst, // Write burst disable
  250. 2'b00, // Normal operation
  251. mrd_cl, // CAS latency
  252. mrd_interleave, // Interleaved bursts
  253. mrd_burst }; // Burst length
  254. // Where to issue a PRECHARGE when we only want to read one word
  255. // (terminate the burst as soon as possible, but no sooner...)
  256. localparam t_pre_rd_when = max(t_ras, t_rcd + 1);
  257. // Where to issue a PRECHARGE when we only want to write one word
  258. // (terminate the burst as soon as possible, but no sooner...)
  259. localparam t_pre_wr_when = max(t_ras, t_rcd + t_wr);
  260. // Actual burst length (2^burst_lg2)
  261. localparam burst_n = 1 << burst_lg2;
  262. // Command opcodes and attributes (is_rfsh, CS#, RAS#, CAS#, WE#)
  263. localparam cmd_desl = 5'b0_1111; // Deselect (= NOP)
  264. localparam cmd_nop = 5'b0_0111; // NO OPERATION
  265. localparam cmd_bst = 5'b0_0110; // BURST TERMINATE
  266. localparam cmd_rd = 5'b0_0101; // READ
  267. localparam cmd_wr = 5'b0_0100; // WRITE
  268. localparam cmd_act = 5'b0_0011; // ACTIVE
  269. localparam cmd_pre = 5'b0_0010; // PRECHARGE
  270. localparam cmd_ref = 5'b1_0001; // AUTO REFRESH
  271. localparam cmd_mrd = 5'b0_0000; // LOAD MODE REGISTER
  272. reg [4:0] dram_cmd;
  273. wire is_rfsh = dram_cmd[4];
  274. assign sr_cs_n = dram_cmd[3];
  275. assign sr_ras_n = dram_cmd[2];
  276. assign sr_cas_n = dram_cmd[1];
  277. assign sr_we_n = dram_cmd[0];
  278. // SDRAM output signal registers
  279. reg [12:0] dram_a;
  280. assign sr_a = dram_a;
  281. reg [1:0] dram_ba;
  282. assign sr_ba = dram_ba;
  283. reg [1:0] dram_dqm;
  284. assign sr_dqm = dram_dqm;
  285. reg [15:0] dram_d; // Data to DRAM
  286. reg [15:0] dram_q; // Data from DRAM (I/O buffers)
  287. reg dram_d_en; // Drive data out
  288. assign sr_dq = dram_d_en ? dram_d : 16'hzzzz;
  289. // Refresh timer logic
  290. reg rfsh_tmr_q;
  291. reg [1:0] rfsh_prio; // Refresh priority (0-3)
  292. // Port1 and refresh arbiter
  293. dram_bus p1 ();
  294. wire do_rfsh;
  295. assign p1.rst_n = rst_n;
  296. assign p1.clk = clk;
  297. dram_arbiter #(.port_count(port1_count))
  298. arbiter (
  299. .ustr ( port1 ),
  300. .dstr ( p1.dstr ),
  301. .rfsh_prio ( rfsh_prio ),
  302. .do_rfsh ( do_rfsh )
  303. );
  304. // The actual values are unimportant; the compiler will optimize
  305. // the state machine implementation.
  306. typedef enum logic [3:0] {
  307. st_reset, // Reset until init timer expires
  308. st_init_rfsh, // Refresh cycles during initialization
  309. st_init_mrd, // MRD register write during initialization
  310. st_ready, // Ready to issue command in the next cycle
  311. st_rfsh, // Refresh cycle
  312. st_rd_wr_act, // Port 1 ACT command
  313. st_rd_wr, // Port 1 transaction
  314. st_wr2_act, // Port 2 write ACT command
  315. st_wr2 // Port 2 write (burstable)
  316. } state_t;
  317. state_t state = st_reset;
  318. always @(posedge clk or negedge rst_n)
  319. if (~rst_n)
  320. begin
  321. rfsh_tmr_q <= 1'b0;
  322. rfsh_prio <= 2'b00;
  323. end
  324. else
  325. begin
  326. rfsh_tmr_q <= rfsh_tmr; // Edge detect
  327. // Refresh priority management: saturating 2-bit counter
  328. if (is_rfsh)
  329. rfsh_prio <= 2'b00; // This is a refresh cycle
  330. else if (rfsh_tmr & ~rfsh_tmr_q)
  331. rfsh_prio <= rfsh_prio + (~&rfsh_prio);
  332. end // else: !if(~rst_n)
  333. reg [5:0] op_ctr; // Cycle into the current state
  334. wire [3:0] op_cycle = op_ctr[3:0]; // Cycle into the current command
  335. wire [1:0] init_op_ctr = op_ctr[5:4]; // Init operation counter
  336. reg op_zero; // op_cycle wrap around (init_op_ctr changed)
  337. reg [31:0] wdata_q;
  338. reg [ 3:0] be_q;
  339. reg [24:0] addr;
  340. reg wrq2_more;
  341. wire [13:0] row_addr = addr[24:12];
  342. wire [1:0] bank_addr = addr[11:10];
  343. wire [8:0] col_addr = addr[9:1];
  344. assign p1.addr0 = addr[0];
  345. assign p1.rd = dram_q;
  346. //
  347. // Careful with the timing here... there is one cycle between
  348. // registers and wires, and the DRAM observes the clock 1/2
  349. // cycle from the internal logic. This affects read timing.
  350. //
  351. // Note that rready starts out as 1. This allows a 0->1 detection
  352. // on the rready line to be used as cycle termination signal.
  353. //
  354. always @(posedge clk or negedge rst_n)
  355. if (~rst_n)
  356. begin
  357. dram_cmd <= cmd_desl;
  358. dram_a <= 13'hxxxx;
  359. dram_ba <= 2'bxx;
  360. dram_dqm <= 2'b00;
  361. dram_d <= 16'hxxxx;
  362. dram_q <= 16'hxxxx;
  363. dram_d_en <= 1'b1; // Don't float except during read
  364. op_ctr <= 6'h0;
  365. op_zero <= 1'b0;
  366. state <= st_reset;
  367. p1.start <= 1'b0;
  368. p1.wrack <= 1'bx;
  369. p1.rd <= 16'hxxxx;
  370. p1.rstrb <= 2'b00;
  371. wacc2 <= 1'b0;
  372. wrq2_more <= 1'bx;
  373. wdata_q <= 32'hxxxx_xxxx;
  374. be_q <= 4'bxxxx;
  375. addr <= 25'bx;
  376. end
  377. else
  378. begin
  379. // Default values
  380. dram_a <= 13'b0;
  381. dram_ba <= bank_addr;
  382. dram_dqm <= 2'b00;
  383. dram_d <= { 8'hAA, 3'b000, dram_cmd };
  384. dram_cmd <= cmd_nop;
  385. dram_d_en <= 1'b1; // Don't float except during read
  386. dram_q <= sr_dq;
  387. p1.rstrb <= 2'b00;
  388. wacc2 <= 1'b0;
  389. op_ctr <= op_ctr + 1'b1;
  390. op_zero <= &op_cycle; // About to wrap around
  391. p1.start <= 1'b0;
  392. case (state)
  393. st_reset:
  394. begin
  395. op_ctr <= 6'b0;
  396. op_zero <= 1'b0;
  397. dram_a[10] <= 1'b1; // Precharge all banks
  398. dram_cmd <= cmd_nop;
  399. if (init_tmr)
  400. begin
  401. dram_cmd <= cmd_pre;
  402. state <= st_init_rfsh;
  403. end
  404. end
  405. st_init_rfsh:
  406. begin
  407. if (op_zero)
  408. begin
  409. dram_cmd <= cmd_ref;
  410. if (init_op_ctr == 2'b11)
  411. state <= st_init_mrd;
  412. end
  413. end
  414. st_init_mrd:
  415. begin
  416. dram_a <= mrd_val;
  417. dram_ba <= 2'b00;
  418. if (op_zero)
  419. if (init_op_ctr[0])
  420. state <= st_ready;
  421. else
  422. dram_cmd <= cmd_mrd;
  423. end
  424. st_ready:
  425. begin
  426. op_ctr <= 6'b0;
  427. op_zero <= 1'b0;
  428. dram_cmd <= cmd_desl;
  429. p1.wrack <= 1'bx;
  430. be_q <= 4'bxxxx;
  431. wdata_q <= 32'hxxxx_xxxx;
  432. addr <= 25'bx;
  433. dram_a <= 13'h1bb;
  434. dram_d <= 16'hbbbb;
  435. // Port 1 and refresh have priority over port 2;
  436. // the various port 1 instances and refresh have
  437. // priorities set by the arbiter block.
  438. if (do_rfsh)
  439. begin
  440. state <= st_rfsh;
  441. end
  442. else if (p1.req)
  443. begin
  444. addr <= p1.addr;
  445. p1.wrack <= |p1.wstrb;
  446. wdata_q <= p1.wd;
  447. be_q <= p1.wstrb;
  448. state <= st_rd_wr_act;
  449. p1.start <= 1'b1;
  450. end // if (p1.req)
  451. else if (wrq2[0])
  452. begin
  453. // Begin port 2 write
  454. addr <= { a2, 1'b0 };
  455. state <= st_wr2_act;
  456. end
  457. end // case: st_ready
  458. st_rfsh: begin
  459. if (op_cycle == 0)
  460. dram_cmd <= cmd_ref;
  461. else if (op_cycle == t_rfc-2)
  462. state <= st_ready;
  463. end
  464. st_rd_wr_act: begin
  465. op_ctr <= 6'b0;
  466. op_zero <= 1'b0;
  467. dram_cmd <= cmd_act;
  468. dram_a <= row_addr;
  469. dram_ba <= bank_addr;
  470. state <= st_rd_wr;
  471. end
  472. st_rd_wr:
  473. begin
  474. dram_d_en <= p1.wrack;
  475. dram_dqm <= {2{p1.wrack}};
  476. dram_d <= 16'hcccc ^ {16{p1.wrack}};
  477. // Commands
  478. //
  479. // This assumes:
  480. // tRCD = 3
  481. // rRRD = 2
  482. // CL = 3
  483. // tRC = 10
  484. // tRAS = 7
  485. // tWR = 2
  486. // tRP = 3
  487. //
  488. case (op_cycle)
  489. 2: begin
  490. dram_a[10] <= 1'b0; // No auto precharge
  491. dram_a[8:0] <= col_addr;
  492. dram_cmd <= p1.wrack ? cmd_wr : cmd_rd;
  493. dram_d <= wdata_q[15:0];
  494. dram_dqm <= {2{p1.wrack}} & ~be_q[1:0];
  495. end
  496. 3: begin
  497. dram_d <= wdata_q[31:16];
  498. dram_dqm <= {2{p1.wrack}} & ~be_q[3:2];
  499. end
  500. 6: begin
  501. // Earliest legal cycle to precharge
  502. // It seems auto precharge violates tRAS(?)
  503. // so do it explicitly.
  504. dram_a[10] <= 1'b1; // One bank
  505. dram_cmd <= cmd_pre;
  506. end
  507. // CL+2 cycles after the read command
  508. // The +2 accounts for internal and I/O delays
  509. 7: begin
  510. p1.rstrb[0] <= ~p1.wrack;
  511. end
  512. 8: begin
  513. p1.rstrb[1] <= ~p1.wrack;
  514. state <= st_ready;
  515. end
  516. default: begin
  517. // Do nothing
  518. end
  519. endcase // case (op_cycle)
  520. end // case: st_rd_wr
  521. st_wr2_act:
  522. begin
  523. op_ctr <= 6'b0;
  524. op_zero <= 1'b0;
  525. dram_a <= row_addr;
  526. dram_ba <= bank_addr;
  527. dram_cmd <= cmd_act;
  528. state <= st_wr2;
  529. end
  530. st_wr2:
  531. begin
  532. // Streamable write from flash ROM
  533. // Note: wacc is asserted in the cycle *before* the
  534. // data and address is latched/consumed.
  535. dram_d <= wd2;
  536. dram_a[10] <= 1'b0; // No auto precharge/precharge one bank
  537. dram_a[8:0] <= a2[9:1];
  538. dram_dqm <= 2'b11;
  539. case (op_cycle)
  540. 0, 1: begin
  541. wacc2 <= 1'b1;
  542. dram_dqm <= 2'b00;
  543. end
  544. 2: begin
  545. dram_cmd <= cmd_wr;
  546. wacc2 <= 1'b1;
  547. dram_dqm <= 2'b00;
  548. wrq2_more <= wrq2[1] & (~&a2[9:3]);
  549. end
  550. 3: begin
  551. wacc2 <= 1'b1;
  552. dram_dqm <= 2'b00;
  553. end
  554. 4: begin
  555. dram_cmd <= cmd_wr;
  556. dram_dqm <= 2'b00;
  557. if (wrq2_more & ~(p1.req | do_rfsh))
  558. begin
  559. // Burst can continue
  560. wacc2 <= 1'b1;
  561. op_ctr[3:0] <= 4'd1;
  562. end
  563. end
  564. 5: begin
  565. dram_dqm <= 2'b00;
  566. end
  567. 6: begin
  568. // Nothing
  569. end
  570. 7: begin
  571. // tWR completed
  572. dram_cmd <= cmd_pre;
  573. end
  574. 8: begin
  575. // tRP will be complete before the next ACT
  576. state <= st_ready;
  577. end
  578. default: begin
  579. // Do nothing
  580. end
  581. endcase // case (op_cycle)
  582. end // case: st_wr2
  583. endcase // case(state)
  584. end // else: !if(~rst_n)
  585. endmodule // dram