sdcard.sv 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. output sd_cs_n, // SD card CS# (CD, DAT3)
  22. output sd_di, // SD card DI (MOSI, CMD)
  23. output sd_sclk, // SD card CLK (SCLK)
  24. input sd_do, // SD card SO (MISO, DAT0)
  25. input sd_cd_n, // Card detect
  26. input [31:0] wdata, // CPU data out (CPU->controller)
  27. output reg [31:0] rdata, // CPU data in (controller->CPU)
  28. input valid, // Memory valid
  29. input [3:0] wstrb, // Write strobes
  30. input [4:0] addr, // Address bits
  31. output wait_n // Hold mem_ready
  32. );
  33. // ------------------------------------------------------------------------
  34. // SD card interface
  35. //
  36. // This drives the SD card in SPI mode. We support two speeds:
  37. // 84 MHz/4 = 21 MHz for normal operation, and 84 MHz/256 = 328 kHz
  38. // during initialization.
  39. //
  40. // It exports the following I/O ports, address bits can be combined.
  41. // The actual connection to the CPU bus shifts the addresses left
  42. // by two so that dword accesses can be done.
  43. //
  44. // Write:
  45. // 00000 - control register:
  46. // [6:0] - speed divider (CPU_HZ/(2*(divider+1)))
  47. // 7 - CS# active
  48. // 8 - clear read CRC registers
  49. // 9 - clear write CRC registers
  50. // x0xxx - reserved
  51. // x1e00 - load shift register but don't start transaction
  52. // x1e01 - load shift register and start transaction, 8 bits
  53. // x1e10 - load shift register and start transaction, 16 bits
  54. // x1e11 - load shift register and start transaction, 32 bits
  55. // 11xxx - clear write CRC registers
  56. // e = endian; 0 = wire byte order; 1 = bigendian byte order
  57. //
  58. // Note: the triggered bus transaction size is set by byte enables.
  59. // The output latch should be written left-aligned (most significant
  60. // bytes within a dword); the input latch right-aligned.
  61. //
  62. // Read:
  63. // 00000 - control register
  64. // 00010 - [7:0] - read CRC7 + final 1 bit
  65. // [31:16] - read CRC16
  66. // 00011 - [7:0] - write CRC7 + final 1 bit
  67. // [31:16] - write CRC16
  68. // x0xxx - reserved
  69. // x1e00 - read shift register but don't start transaction
  70. // x1e01 - read shift register and start transaction, 8 bits
  71. // x1e10 - read shift register and start transaction, 16 bits
  72. // x1e11 - read shift register and start transaction, 32 bits
  73. // 11xxx - clear read CRC registers
  74. //
  75. // ------------------------------------------------------------------------
  76. reg [31:0] sd_shr_out;
  77. reg [31:0] sd_shr_in;
  78. reg [31:0] sd_shr_in_q;
  79. reg [4:0] sd_out_ctr; // Output bit counter
  80. reg sd_active; // Transfer in progress
  81. reg sd_active_neg; // Transfer in progress, first pos clock seen
  82. reg sd_crcstb; // Strobe for CRC generator
  83. reg sd_cs_reg; // CS# active (positive logic, so inverted)
  84. wire sd_cmd = valid & ~sd_active; // CPU command we can act on
  85. reg sd_cmd_ok; // Valid CPU command received
  86. wire sd_data_out = sd_shr_out[31];
  87. reg sd_clk_out; // Output clock signal
  88. // Output pins - tristate if card not present
  89. assign sd_di = ~sd_cd_n ? sd_data_out : 1'bz;
  90. assign sd_sclk = ~sd_cd_n ? sd_clk_out : 1'bz;
  91. assign sd_cs_n = ~sd_cd_n ? ~sd_cs_reg : 1'bz;
  92. // If we try an action while a bus transaction is in progress,
  93. // wait. The register sd_cmd_ok is used to prevent WAIT# from
  94. // being asserted when we already started a transaction on *this*
  95. // I/O operation.
  96. //
  97. // valid: 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0
  98. // sd_active: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
  99. // sd_cmd: 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
  100. // sd_cmd_ok: 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0
  101. // cpu_wait_n: 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1
  102. always @(negedge rst_n or posedge clk)
  103. if (~rst_n)
  104. sd_cmd_ok <= 1'b0;
  105. else
  106. sd_cmd_ok <= valid & (~sd_active | sd_cmd_ok);
  107. assign wait_n = ~(valid & sd_active) | sd_cmd_ok;
  108. // SD clock generator; this counter is used to generate the slow clock.
  109. reg [6:0] sd_clk_div;
  110. reg [6:0] sd_clk_ctr;
  111. reg sd_clk_stb; // Clock strobe (clock flips next cycle)
  112. reg sd_clk_pol; // Clock polarity
  113. wire sd_clk_pos; // SD clock positive strobe
  114. wire sd_clk_neg; // SD clock negative strobe
  115. always @(posedge clk)
  116. begin
  117. if (|sd_clk_ctr)
  118. begin
  119. sd_clk_stb <= 1'b0;
  120. sd_clk_ctr <= sd_clk_ctr - 1'b1;
  121. end
  122. else
  123. begin
  124. sd_clk_stb <= 1'b1;
  125. sd_clk_pol <= ~sd_clk_pol; // Polarity of clock (one cycle early)
  126. sd_clk_ctr <= sd_clk_div;
  127. end
  128. end // always @ (posedge clk)
  129. // Generate strobes from the sd_clk_ctr; this is defined to be 1
  130. assign sd_clk_pos = sd_active & sd_clk_stb & sd_clk_pol;
  131. assign sd_clk_neg = sd_active_neg & sd_clk_stb & ~sd_clk_pol;
  132. always @(negedge rst_n or posedge clk)
  133. if (~rst_n)
  134. sd_clk_out <= 1'b0;
  135. else
  136. sd_clk_out <= (sd_clk_out | sd_clk_pos) & ~sd_clk_neg;
  137. reg [1:0] clear_crc;
  138. always @(negedge rst_n or posedge clk)
  139. if (~rst_n)
  140. begin
  141. sd_shr_out <= 32'hffff_ffff;
  142. sd_cs_reg <= 1'b0;
  143. sd_clk_div <= 7'h7f;
  144. sd_active <= 1'b0;
  145. sd_active_neg <= 1'b0;
  146. sd_out_ctr <= 5'h0;
  147. sd_crcstb <= 1'b0;
  148. sd_shr_in <= 32'hffff_ffff;
  149. sd_shr_in_q <= 32'hffff_ffff;
  150. clear_crc <= 2'b11;
  151. end
  152. else
  153. begin
  154. if (sd_clk_pos)
  155. begin
  156. sd_shr_in <= {sd_shr_in[30:0], sd_do};
  157. sd_out_ctr <= sd_out_ctr + 1'b1;
  158. sd_active_neg <= 1'b1;
  159. end
  160. if (sd_clk_neg)
  161. begin
  162. sd_shr_out <= {sd_shr_out[30:0], 1'b1};
  163. sd_active <= |sd_out_ctr;
  164. sd_active_neg <= |sd_out_ctr;
  165. if (~|sd_out_ctr)
  166. sd_shr_in_q <= sd_shr_in;
  167. end
  168. clear_crc <= 2'b00; // No clearing by default
  169. sd_crcstb <= sd_clk_pos; // CRCs are computed one cycle after posedge
  170. if (sd_cmd)
  171. begin
  172. if (addr[4:3] == 2'b11)
  173. clear_crc <= {|wstrb, ~|wstrb};
  174. casez (addr)
  175. 5'b?10??: begin
  176. // Load in host (littleendian) byte order
  177. if (wstrb[3]) sd_shr_out[ 7: 0] <= wdata[31:24];
  178. if (wstrb[2]) sd_shr_out[15: 8] <= wdata[23:16];
  179. if (wstrb[1]) sd_shr_out[23:16] <= wdata[15: 8];
  180. if (wstrb[0]) sd_shr_out[31:24] <= wdata[ 7: 0];
  181. end
  182. 5'b?11??: begin
  183. // Load in SPI (bigendian) byte order
  184. if (wstrb[3]) sd_shr_out[31:24] <= wdata[31:24];
  185. if (wstrb[2]) sd_shr_out[23:16] <= wdata[23:16];
  186. if (wstrb[1]) sd_shr_out[15: 8] <= wdata[15: 8];
  187. if (wstrb[0]) sd_shr_out[ 7: 0] <= wdata[ 7: 0];
  188. end
  189. 5'b00000: begin
  190. if (wstrb[0]) {sd_cs_reg, sd_clk_div} <= wdata[7:0];
  191. if (wstrb[1]) clear_crc <= wdata[9:8];
  192. end
  193. default: begin
  194. // do nothing
  195. end
  196. endcase
  197. // Begin transaction
  198. // Note: sd_out_ctr *increments*
  199. if (addr[3])
  200. case (addr[1:0])
  201. 2'b01: begin
  202. /* Start 8-bit transaction */
  203. sd_active <= 1'b1;
  204. sd_out_ctr <= 5'b11_000;
  205. end
  206. 2'b10: begin
  207. /* Start 16-bit transaction */
  208. sd_active <= 1'b1;
  209. sd_out_ctr <= 5'b10_000;
  210. end
  211. 2'b11: begin
  212. /* Start 32-bit transaction */
  213. sd_active <= 1'b1;
  214. sd_out_ctr <= 5'b00_000;
  215. end
  216. default: begin
  217. // do nothing
  218. end
  219. endcase // case (addr[1:0])
  220. end // if (sd_cmd)
  221. end // else: !if(~rst_n)
  222. //
  223. // CRC generators: we have two 7-bit and two 16-bit, one each for
  224. // input [0] and output [1].
  225. //
  226. // The CRC generators run one cycle behind the positive sd_clk strobe.
  227. wire [1:0] sd_crcbit = { sd_data_out, sd_shr_in[0] };
  228. reg [6:0] sd_crc7 [0:1]; // CRC-7 shift register
  229. reg [15:0] sd_crc16[0:1]; // CRC-16 shift register
  230. localparam [6:0] crc7_poly = 7'b000_1001;
  231. localparam [15:0] crc16_poly = 16'b0001_0000_0010_0001;
  232. always @(posedge clk)
  233. for (int i = 0; i < 2; i = i+1)
  234. begin
  235. if (clear_crc[i])
  236. begin
  237. sd_crc7[i] <= 7'h00;
  238. sd_crc16[i] <= 16'h0000;
  239. end
  240. else if (sd_crcstb)
  241. begin
  242. sd_crc7[i] <= { sd_crc7[i][5:0], 1'b0 }
  243. ^ ({7{sd_crcbit[i] ^ sd_crc7[i][6]}}
  244. & crc7_poly);
  245. sd_crc16[i] <= { sd_crc16[i][14:0], 1'b0 }
  246. ^ ({16{sd_crcbit[i] ^ sd_crc16[i][15]}}
  247. & crc16_poly);
  248. end // else: !if(clear_crc[i])
  249. end // for (int i = 0; i < 2; i = i+1)
  250. // Data out MUX
  251. always @(*)
  252. begin
  253. casez (addr)
  254. 5'b0_0000: rdata = { 24'b0, sd_cs_reg, sd_clk_div };
  255. 5'b0_0010: rdata = { sd_crc16[0], 8'b0, sd_crc7[0], 1'b1 };
  256. 5'b0_0011: rdata = { sd_crc16[1], 8'b0, sd_crc7[1], 1'b1 };
  257. 5'b?_10??: rdata = { sd_shr_in_q[7:0], sd_shr_in_q[15:8],
  258. sd_shr_in_q[23:16], sd_shr_in_q[31:24] };
  259. 5'b?_11??: rdata = sd_shr_in_q;
  260. default: rdata = 32'hxxxx_xxxx;
  261. endcase
  262. end
  263. endmodule // sdcard