sdcard.sv 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // -----------------------------------------------------------------------
  2. //
  3. // Copyright 2003-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., 53 Temple Place Ste 330,
  8. // Bostom MA 02111-1307, USA; either version 2 of the License, or
  9. // (at your option) any later version; incorporated herein by reference.
  10. //
  11. // -----------------------------------------------------------------------
  12. //
  13. // MMC/SD controller for MAX80
  14. //
  15. // This runs the SD card in SPI mode. In the future, consider improving
  16. // performance by switching to quad SD mode.
  17. //
  18. module sdcard (
  19. input rst_n, // Global reset
  20. input clk, // System clock (84 MHz)
  21. inout sd_cs_n, // SD card CS# (CD, DAT3)
  22. inout sd_di, // SD card DI (MOSI, CMD)
  23. inout sd_sclk, // SD card CLK (SCLK)
  24. inout sd_do, // SD card SO (MISO, DAT0)
  25. input sd_cd_n, // SD socket CD# (Card Detect) switch
  26. input sd_we_n, // SD socket WE# (Write Enable) switch
  27. input [31:0] wdata, // CPU data out (CPU->controller)
  28. output reg [31:0] rdata, // CPU data in (controller->CPU)
  29. input valid, // Memory valid
  30. input [3:0] wstrb, // Write strobes
  31. input [1:0] addr, // Address bits
  32. output wait_n // Hold mem_ready
  33. );
  34. // ------------------------------------------------------------------------
  35. // SD card interface
  36. //
  37. // This drives the SD card in SPI mode. We support two speeds:
  38. // 84 MHz/4 = 21 MHz for normal operation, and 84 MHz/256 = 328 kHz
  39. // during initialization.
  40. //
  41. // It exports the following I/O ports, address bits can be combined.
  42. // The actual connection to the CPU bus shifts the addresses left
  43. // by two so that dword accesses can be done.
  44. //
  45. // Write, A[1:0]:
  46. // 00 - control register:
  47. // [6:0] - speed divider (CPU_HZ/(2*(divider+1)))
  48. // 7 - CS# active
  49. // 8 - clear CRC registers
  50. // 9 - select CRC register inputs (0 = input, 1 = output)
  51. // 01 - load output shift register from the CPU
  52. // 10 - start transaction without loading
  53. // 11 - load output shift register and start transaction
  54. //
  55. // Note: the triggered bus transaction size is set by byte enables.
  56. // The output latch should be written left-aligned (most significant
  57. // bytes within a dword); the input latch right-aligned.
  58. //
  59. // On read, A[1:0]:
  60. // 00 - control register
  61. // 01 - read input latch
  62. // 10 - read CRC7 (in D[7:1], D0 = 1)
  63. // 11 - read CRC16
  64. // ------------------------------------------------------------------------
  65. reg [31:0] sd_shr_out;
  66. reg [31:0] sd_shr_in;
  67. reg [4:0] sd_out_ctr; // Output bit counter
  68. reg sd_active; // Transfer in progress
  69. reg sd_active_neg; // Transfer in progress, first pos clock seen
  70. reg sd_crcsrc; // CRC generator input
  71. reg sd_crcstb; // Strobe for CRC generator
  72. reg sd_cs_reg; // CS# active (positive logic, so inverted)
  73. reg [6:0] sd_crc7; // CRC-7 generator
  74. reg [15:0] sd_crc16; // CRC-16 generator
  75. wire sd_cmd = valid & ~sd_active; // CPU command we can act on
  76. reg sd_cmd_ok; // Valid CPU command received
  77. wire sd_data_out = sd_shr_out[31];
  78. reg sd_clk_out; // Output clock signal
  79. // Output pins - tristate if card not present
  80. assign sd_di = ~sd_cd_n ? sd_data_out : 1'bz;
  81. assign sd_sclk = ~sd_cd_n ? sd_clk_out : 1'bz;
  82. assign sd_cs_n = ~sd_cd_n ? ~sd_cs_reg : 1'bz;
  83. assign sd_do = 1'bz; // Always an input
  84. // If we try an action while a bus transaction is in progress,
  85. // wait. The register sd_cmd_ok is used to prevent WAIT# from
  86. // being asserted when we already started a transaction on *this*
  87. // I/O operation.
  88. //
  89. // valid: 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0
  90. // sd_active: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
  91. // sd_cmd: 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
  92. // sd_cmd_ok: 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0
  93. // cpu_wait_n: 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1
  94. always @(negedge rst_n or posedge clk)
  95. if (~rst_n)
  96. sd_cmd_ok <= 1'b0;
  97. else
  98. sd_cmd_ok <= valid & (~sd_active | sd_cmd_ok);
  99. assign wait_n = ~(valid & sd_active) | sd_cmd_ok;
  100. // SD clock generator; this counter is used to generate the slow clock.
  101. reg [6:0] sd_clk_div;
  102. reg [6:0] sd_clk_ctr;
  103. reg sd_clk_stb; // Clock strobe (clock flips next cycle)
  104. reg sd_clk_pol; // Clock polarity
  105. wire sd_clk_pos; // SD clock positive strobe
  106. wire sd_clk_neg; // SD clock negative strobe
  107. always @(posedge clk)
  108. begin
  109. if (|sd_clk_ctr)
  110. begin
  111. sd_clk_stb <= 1'b0;
  112. sd_clk_ctr <= sd_clk_ctr - 1'b1;
  113. end
  114. else
  115. begin
  116. sd_clk_stb <= 1'b1;
  117. sd_clk_pol <= ~sd_clk_pol; // Polarity of clock (one cycle early)
  118. sd_clk_ctr <= sd_clk_div;
  119. end
  120. end // always @ (posedge clk)
  121. // Generate strobes from the sd_clk_ctr; this is defined to be 1
  122. assign sd_clk_pos = sd_active & sd_clk_stb & sd_clk_pol;
  123. assign sd_clk_neg = sd_active_neg & sd_clk_stb & ~sd_clk_pol;
  124. always @(negedge rst_n or posedge clk)
  125. if (~rst_n)
  126. sd_clk_out <= 1'b0;
  127. else
  128. sd_clk_out <= (sd_clk_out | sd_clk_pos) & ~sd_clk_neg;
  129. wire [2:0] nwrite = wstrb[3] + wstrb[2] + wstrb[1] + wstrb[0];
  130. wire rstrb = valid & ~|wstrb;
  131. always @(negedge rst_n or posedge clk)
  132. if (~rst_n)
  133. begin
  134. sd_shr_out <= 32'hffff_ffff;
  135. sd_cs_reg <= 1'b0;
  136. sd_clk_div <= 7'h7f;
  137. sd_active <= 1'b0;
  138. sd_active_neg <= 1'b0;
  139. sd_out_ctr <= 5'h0;
  140. sd_crcstb <= 1'b0;
  141. sd_shr_in <= 32'hffff_ffff;
  142. sd_crcsrc <= 1'b0;
  143. end
  144. else
  145. begin
  146. if (sd_clk_pos)
  147. begin
  148. sd_shr_in <= {sd_shr_in[30:0], sd_do};
  149. sd_out_ctr <= sd_out_ctr + 1'b1;
  150. sd_active_neg <= 1'b1;
  151. end
  152. if (sd_clk_neg)
  153. begin
  154. sd_shr_out <= {sd_shr_out[30:0], 1'b1};
  155. sd_active <= |sd_out_ctr;
  156. sd_active_neg <= |sd_out_ctr;
  157. end
  158. sd_crcstb <= sd_clk_pos; // CRCs are computed one cycle after posedge
  159. if (sd_cmd)
  160. begin
  161. if (addr[1:0] == 2'b00)
  162. begin
  163. if (wstrb[0]) {sd_cs_reg, sd_clk_div} <= wdata[7:0];
  164. if (wstrb[1]) sd_crcsrc <= wdata[9];
  165. end
  166. if (addr[0])
  167. begin
  168. if (wstrb[3]) sd_shr_out[31:24] <= wdata[31:24];
  169. if (wstrb[2]) sd_shr_out[23:16] <= wdata[23:16];
  170. if (wstrb[1]) sd_shr_out[15: 8] <= wdata[15: 8];
  171. if (wstrb[0]) sd_shr_out[ 7: 0] <= wdata[ 7: 0];
  172. end
  173. if (addr[1])
  174. begin
  175. sd_active <= |wstrb;
  176. sd_out_ctr <= { nwrite[1:0], 3'b000 };
  177. end
  178. end // if (sd_cmd)
  179. end // else: !if(~rst_n)
  180. wire clear_crc = ~rst_n |
  181. (sd_cmd & (addr[1:0] == 2'b00) & wstrb[1] & wdata[8]);
  182. // CRC generators: we have one 7-bit and one 16-bit, shared between
  183. // input and output. The controller CPU has to specify where it wants
  184. // the input from by setting A3 properly when starting a bus
  185. // transaction (A4 = 1).
  186. //
  187. // The CRC generators run one cycle behind the positive sd_clk strobe.
  188. wire sd_crcbit = sd_crcsrc ? sd_data_out : sd_shr_in[0];
  189. wire sd_crc7in = sd_crcbit ^ sd_crc7[6];
  190. always @(posedge clk)
  191. if (clear_crc)
  192. sd_crc7 <= 7'h00;
  193. else if (sd_crcstb)
  194. sd_crc7 <= {sd_crc7[5:3], sd_crc7[2]^sd_crc7in,
  195. sd_crc7[1:0], sd_crc7in};
  196. wire sd_crc16in = sd_crcbit ^ sd_crc16[15];
  197. always @(posedge clk)
  198. if (clear_crc)
  199. sd_crc16 <= 16'h0000;
  200. else if (sd_crcstb)
  201. sd_crc16 <= {sd_crc16[14:12], sd_crc16[11]^sd_crc16in,
  202. sd_crc16[10:5], sd_crc16[4]^sd_crc16in,
  203. sd_crc16[3:0], sd_crc16in};
  204. // Data out MUX
  205. // Currently no registers with read side effects
  206. always @(*)
  207. begin
  208. case (addr)
  209. 2'b00: rdata = { 22'b0, sd_crcsrc, 1'b0, sd_cs_reg, sd_clk_div };
  210. 2'b01: rdata = sd_shr_in;
  211. 2'b10: rdata = { 24'b0, sd_crc7, 1'b1 };
  212. 2'b11: rdata = { 16'b0, sd_crc16 };
  213. endcase // case (addr)
  214. end
  215. endmodule // sdcard