sdram.sv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 8-strobe burst transactions. If additional ports
  21. // are needed, the intent is to add an arbiter to port 1.
  22. //
  23. // All signals are in the sdram clock domain.
  24. //
  25. // The ack signals are arrays that give (some) advance notification:
  26. // e.g. rack0[1] is asserted exactly one cycle before rack0[0].
  27. //
  28. // rack*[0] is asserted during the first cycle rd* is valid.
  29. // wack*[0] is asserted during the cycle after wd* was sampled.
  30. //
  31. // Thus, especially for bursts on port 1, generally rack1[0] and
  32. // wack1[1] are the relevant signals.
  33. //
  34. module sdram
  35. #( parameter
  36. // Timing parameters
  37. // The parameters are hardcoded for Micron MT48LC16M16A2-6A,
  38. // per datasheet:
  39. // 100 MHz 167 MHz
  40. // ----------------------------------------------------------
  41. // CL 2 3 READ to data out
  42. // tRCD 18 ns 2 3 ACTIVE to READ/WRITE
  43. // tRFC 60 ns 6 10 REFRESH to ACTIVE
  44. // tRP 18 ns 2 3 PRECHARGE to ACTIVE/REFRESH
  45. // tRAS 42 ns 5 7 ACTIVE to PRECHARGE
  46. // tRC 60 ns 6 10 ACTIVE to ACTIVE (same bank)
  47. // tRRD 12 ns 2 2 ACTICE to ACTIVE (different bank)
  48. // tWR 12 ns 2 2 Last write data to PRECHARGE
  49. // tMRD 2 2 MODE REGISTER to ACTIVE/REFRESH
  50. //
  51. // These parameters are set by power of 2:
  52. // tREFi 64/8192 ms 781 1302 Refresh time per row (max)
  53. // tP 100 us 10000 16667 Time until first command (min)
  54. t_cl = 3,
  55. t_rcd = 3,
  56. t_rfc = 10,
  57. t_rp = 3,
  58. t_ras = 7,
  59. t_rc = 10,
  60. t_rrd = 2,
  61. t_wr = 2,
  62. t_mrd = 2,
  63. t_refi_lg2 = 10, // 1024 cycles
  64. t_p_lg2 = 15, // 32768 cycles
  65. burst_lg2 = 1 // Burst length on port 1
  66. )
  67. (
  68. // Reset and clock
  69. input rst_n,
  70. input clk,
  71. // SDRAM hardware interface
  72. output sr_clk, // SDRAM clock output buffer
  73. output sr_cke, // SDRAM clock enable
  74. output sr_cs_n, // SDRAM CS#
  75. output sr_ras_n, // SDRAM RAS#
  76. output sr_cas_n, // SDRAM CAS#
  77. output sr_we_n, // SDRAM WE#
  78. output [1:0] sr_dqm, // SDRAM DQM (per byte)
  79. output [1:0] sr_ba, // SDRAM bank selects
  80. output [12:0] sr_a, // SDRAM address bus
  81. inout [15:0] sr_dq, // SDRAM data bus
  82. // Port 0: single byte, high priority
  83. input [24:0] a0, // Address, must be stable until ack
  84. output reg [7:0] rd0, // Data from SDRAM
  85. input rrq0, // Read request
  86. output reg [t_rcd+t_cl+3:0] rack0, // Read ack
  87. input [7:0] wd0, // Data to SDRAM
  88. input wrq0, // Write request
  89. output reg wack0, // Write ack (data latched)
  90. // Port 1
  91. input [24:2] a1,
  92. input [7:0] be1, // Write byte enable
  93. output reg [31:0] rd1,
  94. input rrq1,
  95. output reg [t_rcd+t_cl+3:0] rack1,
  96. input [31:0] wd1,
  97. input wrq1,
  98. output reg wack1
  99. );
  100. // Mode register data
  101. wire mrd_wburst = 1'b1; // Write bursts enabled
  102. wire [2:0] mrd_cl = t_cl;
  103. wire [2:0] mrd_burst = burst_lg2;
  104. wire mrd_interleave = 1'b0; // Interleaved bursts
  105. wire [12:0] mrd_val = { 3'b000, // Reserved
  106. ~mrd_wburst, // Write burst disable
  107. 2'b00, // Normal operation
  108. mrd_cl, // CAS latency
  109. mrd_interleave, // Interleaved bursts
  110. mrd_burst }; // Burst length
  111. // Where to issue a PRECHARGE when we only want to read one word
  112. // (terminate the burst as soon as possible, but no sooner...)
  113. localparam t_pre_rd_when = max(t_ras, t_rcd + 1);
  114. // Where to issue a PRECHARGE when we only want to write one word
  115. // (terminate the burst as soon as possible, but no sooner...)
  116. localparam t_pre_wr_when = max(t_ras, t_rcd + t_wr);
  117. // Actual burst length (2^burst_lg2)
  118. localparam burst_n = 1 << burst_lg2;
  119. // Command opcodes and attributes (is_rfsh, CS#, RAS#, CAS#, WE#)
  120. localparam cmd_desl = 5'b0_1111; // Deselect (= NOP)
  121. localparam cmd_nop = 5'b0_0111; // NO OPERATION
  122. localparam cmd_bst = 5'b0_0110; // BURST TERMINATE
  123. localparam cmd_rd = 5'b0_0101; // READ
  124. localparam cmd_wr = 5'b0_0100; // WRITE
  125. localparam cmd_act = 5'b0_0011; // ACTIVE
  126. localparam cmd_pre = 5'b0_0010; // PRECHARGE
  127. localparam cmd_ref = 5'b1_0001; // AUTO REFRESH
  128. localparam cmd_mrd = 5'b0_0000; // LOAD MODE REGISTER
  129. reg [4:0] dram_cmd;
  130. wire is_rfsh = dram_cmd[4];
  131. assign sr_cs_n = dram_cmd[3];
  132. assign sr_ras_n = dram_cmd[2];
  133. assign sr_cas_n = dram_cmd[1];
  134. assign sr_we_n = dram_cmd[0];
  135. // SDRAM output clock buffer. The SDRAM output clock is
  136. // inverted with respect to our internal clock, so that
  137. // the SDRAM sees the positive clock edge in the middle of
  138. // our clocks.
  139. //
  140. // Use a DDIO buffer for best performance
  141. // For EP4CE15 only could use a secondary PLL here, but it
  142. // isn't clear it buys us a whole lot.
  143. ddio_out sr_clk_out (
  144. .aclr ( 1'b0 ),
  145. .datain_h ( 1'b0 ),
  146. .datain_l ( 1'b1 ),
  147. .outclock ( clk ),
  148. .dataout ( sr_clk )
  149. );
  150. // SDRAM output signal registers
  151. reg dram_cke;
  152. assign sr_cke = dram_cke;
  153. reg [12:0] dram_a;
  154. assign sr_a = dram_a;
  155. reg [1:0] dram_ba;
  156. assign sr_ba = dram_ba;
  157. reg [1:0] dram_dqm;
  158. assign sr_dqm = dram_dqm;
  159. reg [15:0] dram_d; // Data to DRAM
  160. reg dram_d_en; // Drive data out
  161. assign sr_dq = dram_d_en ? dram_d : 16'hzzzz;
  162. // Input register for SDRAM data
  163. reg [15:0] dram_q;
  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 port; // Port being serviced
  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. reg latch_next_bank;
  213. always @(negedge rst_n or posedge clk)
  214. if (~rst_n)
  215. begin
  216. latch_next_bank <= 1'b0;
  217. end
  218. else
  219. begin
  220. latch_next_bank <= st_idle;
  221. if (latch_next_bank)
  222. begin
  223. next_bank <= { dram_a, dram_ba } + 1'b1;
  224. last_dword <= &col_addr[9:2]; // last dword in bank?
  225. end
  226. end // else: !if(~rst_n)
  227. reg [31:0] wdata_q;
  228. reg [ 6:0] be_q;
  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.
  233. //
  234. always @(posedge clk or negedge rst_n)
  235. if (~rst_n)
  236. begin
  237. dram_cke <= 1'b0;
  238. dram_cmd <= cmd_desl;
  239. dram_a <= 13'hxxxx;
  240. dram_ba <= 2'bxx;
  241. dram_dqm <= 2'b00;
  242. dram_d <= 16'hxxxx;
  243. dram_d_en <= 1'b1; // Don't float except during read
  244. op_cycle <= 1'b0;
  245. state <= st_reset;
  246. rack0 <= 1'b0;
  247. wack0 <= 1'b0;
  248. rack1 <= 1'b0;
  249. wack1 <= 1'b0;
  250. end
  251. else
  252. begin
  253. dram_cke <= 1'b1; // Always true once out of reset
  254. // Default values
  255. dram_ba <= 2'bxx;
  256. dram_dqm <= 2'b11;
  257. dram_d <= 16'hxxxx;
  258. dram_cmd <= cmd_nop;
  259. rack0 <= rack0 >> 1;
  260. wack0 <= 1'b0;
  261. rack1 <= rack1 >> 1;
  262. wack1 <= 1'b0;
  263. dram_d_en <= 1'b1; // Don't float except during read
  264. if (state == st_reset || state == st_idle)
  265. op_cycle <= 1'b0;
  266. else
  267. op_cycle <= op_cycle + 1'b1;
  268. case (state)
  269. st_reset:
  270. begin
  271. dram_cmd <= cmd_desl;
  272. if (init_ctr[t_p_lg2])
  273. begin
  274. dram_cmd <= cmd_pre;
  275. dram_a[10] <= 1'b1; // Precharge All Banks
  276. state <= st_init;
  277. end
  278. end
  279. st_init:
  280. begin
  281. // Add 3 to the count to account for skew between rfsh_ctr
  282. // and init_ctr
  283. if ( rfsh_ctr[4:0] == t_rp+3 || rfsh_ctr[4:0] == t_rp+t_rfc+3 )
  284. begin
  285. dram_cmd <= cmd_ref;
  286. end
  287. if ( rfsh_ctr[4:0] == t_rp+t_rfc*2+3 )
  288. begin
  289. dram_cmd <= cmd_mrd;
  290. dram_a <= mrd_val;
  291. end
  292. if ( rfsh_ctr[4:0] >= t_rp+t_rfc*2+t_mrd-1+3 )
  293. state <= st_idle;
  294. end // case: st_init
  295. st_idle:
  296. begin
  297. // A data transaction starts with ACTIVE command;
  298. // a refresh transaction starts with REFRESH.
  299. // Port 0 has the highest priority, then
  300. // refresh, then port 1; a refresh transaction
  301. // is started opportunistically if nothing is
  302. // pending and the refresh counter is no less than
  303. // half expired.
  304. casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_prio} )
  305. 4'b1???:
  306. begin
  307. // Begin port 0 transaction
  308. dram_cmd <= cmd_act;
  309. dram_a <= a0[24:12];
  310. dram_ba <= a0[11:10];
  311. col_addr <= a0[11:2];
  312. be_q <= 1'b1 << a0[1:0];
  313. port <= 1'b0;
  314. if ( wrq0 )
  315. begin
  316. state <= st_wr;
  317. wack0 <= 1'b1;
  318. wdata_q <= {4{wd0}};
  319. end
  320. else
  321. begin
  322. state <= st_rd;
  323. rack0[t_rcd+t_cl+3] <= 1'b1;
  324. end
  325. end
  326. 4'b010?:
  327. begin
  328. // Begin port 1 transaction
  329. dram_cmd <= cmd_act;
  330. dram_a <= a1[24:12];
  331. dram_ba <= a1[11:10];
  332. col_addr <= a1[11:2];
  333. be_q <= be1;
  334. port <= 1'b1;
  335. if ( wrq1 )
  336. begin
  337. state <= st_wr;
  338. wack1 <= 1'b1;
  339. wdata_q <= wd1;
  340. end
  341. else
  342. begin
  343. state <= st_rd;
  344. rack1[t_rcd+t_cl+3] <= 1'b1;
  345. end
  346. end
  347. 4'b0?1?, 4'b0001:
  348. begin
  349. // Begin refresh transaction
  350. dram_cmd <= cmd_ref;
  351. state <= st_rfsh;
  352. end
  353. default:
  354. begin
  355. dram_cmd <= cmd_desl;
  356. state <= st_idle;
  357. end
  358. endcase // casez ( {rrq0|wrq0, rrq1|wrq1, rfsh_ctr[t_ref:t_ref-1]} )
  359. end // case: st_idle
  360. st_rfsh:
  361. begin
  362. if (op_cycle == t_rfc-2)
  363. state <= st_idle;
  364. end
  365. st_rd:
  366. begin
  367. dram_d_en <= 1'b0; // Tristate our output
  368. dram_dqm <= 2'b00;
  369. // Commands
  370. //
  371. // This assumes:
  372. // tRCD = 3
  373. // rRRD = 2
  374. // CL = 3
  375. // tRC = 10
  376. case (op_cycle)
  377. 1: begin
  378. dram_a <= next_bank[14:2];
  379. dram_ba <= next_bank[1:0];
  380. dram_cmd <= last_dword ? cmd_act : cmd_nop;
  381. end
  382. 2, 4: begin
  383. dram_a[12:9] <= 4'b0000;
  384. dram_a[8:1] <= col_addr[9:2];
  385. dram_a[0] <= 1'b0;
  386. dram_cmd <= cmd_rd;
  387. col_addr <= col_addr + 1'b1;
  388. end
  389. 8: begin
  390. // Precharge all banks
  391. dram_a[12:9] <= 4'b0010;
  392. dram_cmd <= cmd_pre;
  393. end
  394. 10: begin
  395. state <= st_idle;
  396. end
  397. endcase // case (op_cycle)
  398. // +2 for bus turnaround latencies
  399. if (op_cycle >= 2+t_cl+2)
  400. begin
  401. if (port == 1'b0)
  402. begin
  403. if (be_q[0])
  404. rd0 <= dram_q[7:0];
  405. if (be_q[1])
  406. rd0 <= dram_q[15:8];
  407. end
  408. else if (op_cycle[0] ^ ((2+t_cl+2) & 1'b1))
  409. begin
  410. // Odd word
  411. if (be_q[0])
  412. rd1[23:16] <= dram_q[ 7:0];
  413. if (be_q[1])
  414. rd1[31:24] <= dram_q[15:8];
  415. end
  416. else
  417. begin
  418. // Even word
  419. if (be_q[0])
  420. rd1[ 7:0] <= dram_q[ 7:0];
  421. if (be_q[1])
  422. rd1[15:8] <= dram_q[15:8];
  423. end // else: !if(op_cycle[0] ^ ((t_rcd-1+t_cl+2) & 1'b1))
  424. be_q >>= 2;
  425. end // if (op_cycle >= 2+t_cl+2)
  426. end // case: st_rd
  427. st_wr:
  428. begin
  429. // Commands
  430. //
  431. // This assumes:
  432. // tRCD = 3
  433. // tRRD = 2
  434. // CL = 3
  435. // tRC = 10
  436. // tWR = 2
  437. // tRP = 3
  438. case (op_cycle)
  439. 1: begin
  440. dram_a <= next_bank[14:2];
  441. dram_ba <= next_bank[1:0];
  442. dram_cmd <= last_dword ? cmd_act : cmd_nop;
  443. end
  444. 2, 4: begin
  445. dram_a[12:9] <= 4'b0000;
  446. dram_a[8:1] <= col_addr[9:2];
  447. dram_a[0] <= 1'b0;
  448. dram_cmd <= cmd_wr;
  449. col_addr <= col_addr + 1'b1;
  450. end
  451. 7: begin
  452. // Precharge all banks
  453. dram_a[12:9] <= 4'b0010;
  454. dram_cmd <= cmd_pre;
  455. end
  456. 10: begin
  457. state <= st_idle;
  458. end
  459. endcase // case (op_cycle)
  460. if (op_cycle >= 2)
  461. begin
  462. dram_d <= wdata_q[15:0];
  463. // Flip data between odd and even words
  464. wdata_q <= { wdata_q[15:0], wdata_q[31:16] };
  465. dram_dqm <= ~be_q[1:0];
  466. be_q >>= 2;
  467. end
  468. end // case: st_wr
  469. endcase // case(state)
  470. end // else: !if(~rst_n)
  471. endmodule // dram