sdcard.sv 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. // Note: this is also usable as generic SPI master in many cases.
  19. module sdcard
  20. #(
  21. parameter [0:0] with_crc7 = 1'b1,
  22. parameter [6:0] crc7_poly = 7'b000_1001,
  23. parameter [0:0] with_crc16 = 1'b1,
  24. parameter [15:0] crc16_poly = 16'b0001_0000_0010_0001,
  25. parameter [7:0] with_irq_mask = 8'b0000_0000
  26. )
  27. (
  28. input rst_n, // Global reset
  29. input clk, // System clock (84 MHz)
  30. output sd_cs_n, // SD card CS# (CD, DAT3)
  31. output sd_di, // SD card DI (MOSI, CMD)
  32. output sd_sclk, // SD card CLK (SCLK)
  33. input sd_do, // SD card SO (MISO, DAT0)
  34. input sd_cd_n, // Card detect
  35. input sd_irq_n, // External IRQ input (optional)
  36. input [31:0] wdata, // CPU data out (CPU->controller)
  37. output reg [31:0] rdata, // CPU data in (controller->CPU)
  38. input valid, // Memory valid
  39. input [3:0] wstrb, // Write strobes
  40. input [4:0] addr, // Address bits
  41. output wait_n, // Hold mem_ready
  42. output irq // CPU interrupt request
  43. );
  44. // ------------------------------------------------------------------------
  45. // SD card interface
  46. //
  47. // This drives the SD card in SPI mode. We support two speeds:
  48. // 84 MHz/4 = 21 MHz for normal operation, and 84 MHz/256 = 328 kHz
  49. // during initialization.
  50. //
  51. // It exports the following I/O ports, address bits can be combined.
  52. // The actual connection to the CPU bus shifts the addresses left
  53. // by two so that dword accesses can be done.
  54. //
  55. // Write:
  56. // 00000 - control register:
  57. // [6:0] - speed divider (CPU_HZ/(2*(divider+1)))
  58. // [7] - CS# active
  59. // [15:8] - IRQ enable mask
  60. // [22] - clear read CRC
  61. // [23] - clear write CRC
  62. //
  63. // x0xxx - reserved
  64. // x1e00 - load shift register but don't start transaction
  65. // x1e01 - load shift register and start transaction, 8 bits
  66. // x1e10 - load shift register and start transaction, 16 bits
  67. // x1e11 - load shift register and start transaction, 32 bits
  68. // 11xxx - clear write CRC registers
  69. // e = endian; 0 = wire byte order; 1 = bigendian byte order
  70. //
  71. // Note: the triggered bus transaction size is set by byte enables.
  72. // The output latch should be written left-aligned (most significant
  73. // bytes within a dword); the input latch right-aligned.
  74. //
  75. // Read:
  76. // 00000 - [15:0] - control register
  77. // [16] - busy status
  78. // [31:24] - IRQ status
  79. // 00100 - [7:0] - read CRC7 + final 1 bit
  80. // [31:16] - read CRC16
  81. // 00101 - [7:0] - write CRC7 + final 1 bit
  82. // [31:16] - write CRC16
  83. // x0xxx - reserved
  84. // x1e00 - read shift register but don't start transaction
  85. // x1e01 - read shift register and start transaction, 8 bits
  86. // x1e10 - read shift register and start transaction, 16 bits
  87. // x1e11 - read shift register and start transaction, 32 bits
  88. // 11xxx - clear read CRC registers
  89. //
  90. // Addresses of the form 000xx are non-blocking; others stall the CPU
  91. // until the current transaction is complete.
  92. //
  93. // Available interrupts are:
  94. // 0 - unit idle
  95. // 1 - card detect (sd_cd_n low)
  96. // 2 - external interrupt (sd_irq_n low)
  97. // ------------------------------------------------------------------------
  98. reg [31:0] sd_shr_out;
  99. reg [31:0] sd_shr_in;
  100. reg [31:0] sd_shr_in_q;
  101. reg [4:0] sd_out_ctr; // Output bit counter
  102. reg sd_active; // Transfer in progress
  103. reg sd_active_neg; // Transfer in progress, first pos clock seen
  104. reg sd_crcstb; // Strobe for CRC generator
  105. reg sd_cs_reg; // CS# active (positive logic, so inverted)
  106. wire sd_data_out = sd_shr_out[31];
  107. reg sd_clk_out; // Output clock signal
  108. // Output pins - tristate if card not present
  109. assign sd_di = ~sd_cd_n ? sd_data_out : 1'bz;
  110. assign sd_sclk = ~sd_cd_n ? sd_clk_out : 1'bz;
  111. assign sd_cs_n = ~sd_cd_n ? ~sd_cs_reg : 1'bz;
  112. // If we try an action while a bus transaction is in progress,
  113. // wait. The register sd_cmd_ok is used to prevent WAIT# from
  114. // being asserted when we already started a transaction on *this*
  115. // I/O operation.
  116. //
  117. // valid: 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0
  118. // sd_active: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
  119. // sd_cmd: 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
  120. // sd_cmd_ok: 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0
  121. // cpu_wait_n: 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1
  122. //
  123. wire valid_blocking = valid & (addr[4:2] != 3'b000);
  124. wire sd_cmd = valid_blocking & ~sd_active;
  125. // CPU command we can act on
  126. reg sd_cmd_ok; // Valid CPU command received
  127. always @(negedge rst_n or posedge clk)
  128. if (~rst_n)
  129. sd_cmd_ok <= 1'b0;
  130. else
  131. sd_cmd_ok <= valid_blocking & (~sd_active | sd_cmd_ok);
  132. //assign wait_n = ~(valid_blocking & sd_active) | sd_cmd_ok;
  133. assign wait_n = 1'b1;
  134. // Valid *nonblocking* command
  135. wire sd_cmd_nonblock = valid & (addr[4:2] == 3'b000);
  136. // SD clock generator; this counter is used to generate the slow clock.
  137. reg [6:0] sd_clk_div;
  138. reg [6:0] sd_clk_ctr;
  139. reg sd_clk_stb; // Clock strobe (clock flips next cycle)
  140. reg sd_clk_pol; // Clock polarity
  141. wire sd_clk_pos; // SD clock positive strobe
  142. wire sd_clk_neg; // SD clock negative strobe
  143. always @(posedge clk)
  144. begin
  145. if (|sd_clk_ctr)
  146. begin
  147. sd_clk_stb <= 1'b0;
  148. sd_clk_ctr <= sd_clk_ctr - 1'b1;
  149. end
  150. else
  151. begin
  152. sd_clk_stb <= 1'b1;
  153. sd_clk_pol <= ~sd_clk_pol; // Polarity of clock (one cycle early)
  154. sd_clk_ctr <= sd_clk_div;
  155. end
  156. end // always @ (posedge clk)
  157. // Generate strobes from the sd_clk_ctr; this is defined to be 1
  158. assign sd_clk_pos = sd_active & sd_clk_stb & sd_clk_pol;
  159. assign sd_clk_neg = sd_active_neg & sd_clk_stb & ~sd_clk_pol;
  160. always @(negedge rst_n or posedge clk)
  161. if (~rst_n)
  162. sd_clk_out <= 1'b0;
  163. else
  164. sd_clk_out <= (sd_clk_out | sd_clk_pos) & ~sd_clk_neg;
  165. // IRQ handling (extensible for future uses)
  166. reg [7:0] irq_status;
  167. always @(posedge clk)
  168. begin
  169. irq_status <= with_irq_mask &
  170. {
  171. 5'b0, // Reserved for future uses
  172. ~sd_irq_n,
  173. ~sd_cd_n,
  174. ~sd_active
  175. };
  176. end
  177. reg [7:0] irq_en;
  178. assign irq = |(irq_status & irq_en & with_irq_mask);
  179. //
  180. // Main shift register state machine
  181. //
  182. reg [1:0] clear_crc;
  183. always @(negedge rst_n or posedge clk)
  184. if (~rst_n)
  185. begin
  186. sd_shr_out <= 32'hffff_ffff;
  187. sd_cs_reg <= 1'b0;
  188. sd_clk_div <= 7'h7f;
  189. sd_active <= 1'b0;
  190. sd_active_neg <= 1'b0;
  191. sd_out_ctr <= 5'h0;
  192. sd_crcstb <= 1'b0;
  193. sd_shr_in <= 32'hffff_ffff;
  194. sd_shr_in_q <= 32'hffff_ffff;
  195. clear_crc <= 2'b11;
  196. irq_en <= 8'b0;
  197. end
  198. else
  199. begin
  200. if (sd_clk_pos)
  201. begin
  202. sd_shr_in <= {sd_shr_in[30:0], sd_do};
  203. sd_out_ctr <= sd_out_ctr + 1'b1;
  204. sd_active_neg <= 1'b1;
  205. end
  206. if (sd_clk_neg)
  207. begin
  208. sd_shr_out <= {sd_shr_out[30:0], 1'b1};
  209. sd_active <= |sd_out_ctr;
  210. sd_active_neg <= |sd_out_ctr;
  211. if (~|sd_out_ctr)
  212. sd_shr_in_q <= sd_shr_in;
  213. end
  214. clear_crc <= 2'b00; // No clearing by default
  215. sd_crcstb <= sd_clk_pos; // CRCs are computed one cycle after posedge
  216. if (sd_cmd_nonblock)
  217. casez(addr[1:0])
  218. 2'b00: begin
  219. if (wstrb[0]) {sd_cs_reg, sd_clk_div} <= wdata[7:0];
  220. if (wstrb[1]) irq_en <= wdata[15:8] & with_irq_mask;
  221. if (wstrb[2]) clear_crc <= wdata[23:22];
  222. end
  223. default: begin
  224. // Do nothing
  225. end
  226. endcase
  227. if (sd_cmd)
  228. begin
  229. if (addr[4:3] == 2'b11)
  230. clear_crc <= {|wstrb, ~|wstrb};
  231. casez (addr)
  232. 5'b?10??: begin
  233. // Load in host (littleendian) byte order
  234. if (wstrb[3]) sd_shr_out[ 7: 0] <= wdata[31:24];
  235. if (wstrb[2]) sd_shr_out[15: 8] <= wdata[23:16];
  236. if (wstrb[1]) sd_shr_out[23:16] <= wdata[15: 8];
  237. if (wstrb[0]) sd_shr_out[31:24] <= wdata[ 7: 0];
  238. end
  239. 5'b?11??: begin
  240. // Load in SPI (bigendian) byte order
  241. if (wstrb[3]) sd_shr_out[31:24] <= wdata[31:24];
  242. if (wstrb[2]) sd_shr_out[23:16] <= wdata[23:16];
  243. if (wstrb[1]) sd_shr_out[15: 8] <= wdata[15: 8];
  244. if (wstrb[0]) sd_shr_out[ 7: 0] <= wdata[ 7: 0];
  245. end
  246. default: begin
  247. // do nothing
  248. end
  249. endcase
  250. // Begin transaction
  251. // Note: sd_out_ctr *increments*
  252. if (addr[3])
  253. case (addr[1:0])
  254. 2'b01: begin
  255. /* Start 8-bit transaction */
  256. sd_active <= 1'b1;
  257. sd_out_ctr <= 5'b11_000;
  258. end
  259. 2'b10: begin
  260. /* Start 16-bit transaction */
  261. sd_active <= 1'b1;
  262. sd_out_ctr <= 5'b10_000;
  263. end
  264. 2'b11: begin
  265. /* Start 32-bit transaction */
  266. sd_active <= 1'b1;
  267. sd_out_ctr <= 5'b00_000;
  268. end
  269. default: begin
  270. // do nothing
  271. end
  272. endcase // case (addr[1:0])
  273. end // if (sd_cmd)
  274. end // else: !if(~rst_n)
  275. //
  276. // CRC generators: we have two 7-bit and two 16-bit, one each for
  277. // input [0] and output [1].
  278. //
  279. // The CRC generators run one cycle behind the positive sd_clk strobe.
  280. wire [1:0] sd_crcbit = { sd_data_out, sd_shr_in[0] };
  281. reg [6:0] sd_crc7 [0:1]; // CRC-7 shift register
  282. reg [15:0] sd_crc16[0:1]; // CRC-16 shift register
  283. always @(negedge rst_n or posedge clk)
  284. if (~rst_n)
  285. for (int i = 0; i < 2; i = i+1)
  286. begin
  287. sd_crc7[i] <= 7'hxx;
  288. sd_crc16[i] <= 16'hxxxx;
  289. end
  290. else
  291. for (int i = 0; i < 2; i = i+1)
  292. begin
  293. if (clear_crc[i])
  294. begin
  295. sd_crc7[i] <= 7'h00;
  296. sd_crc16[i] <= 16'h0000;
  297. end
  298. else if (sd_crcstb)
  299. begin
  300. if (with_crc7)
  301. sd_crc7[i] <= { sd_crc7[i][5:0], 1'b0 }
  302. ^ ({7{sd_crcbit[i] ^ sd_crc7[i][6]}}
  303. & crc7_poly);
  304. if (with_crc16)
  305. sd_crc16[i] <= { sd_crc16[i][14:0], 1'b0 }
  306. ^ ({16{sd_crcbit[i] ^ sd_crc16[i][15]}}
  307. & crc16_poly);
  308. end // else: !if(clear_crc[i])
  309. end // for (int i = 0; i < 2; i = i+1)
  310. // Data out MUX
  311. always_comb
  312. begin
  313. casez (addr)
  314. 5'b0_0000: begin
  315. rdata[31:16] = { irq_status, 7'b0, sd_active };
  316. rdata[15: 0] = { irq_en, sd_cs_reg, sd_clk_div };
  317. end
  318. 5'b0_0100: begin
  319. rdata[31:16] = with_crc16 ? sd_crc16[0] : 16'b0;
  320. rdata[15: 8] = 8'b0;
  321. rdata[ 7: 0] = with_crc7 ? { sd_crc7[0], 1'b1 } : 8'b0;
  322. end
  323. 5'b0_0101: begin
  324. rdata[31:16] = with_crc16 ? sd_crc16[1] : 16'b0;
  325. rdata[15: 8] = 8'b0;
  326. rdata[ 7: 0] = with_crc7 ? { sd_crc7[1], 1'b1 } : 8'b0;
  327. end
  328. 5'b?_10??: begin
  329. rdata = { sd_shr_in_q[7:0], sd_shr_in_q[15:8],
  330. sd_shr_in_q[23:16], sd_shr_in_q[31:24] };
  331. end
  332. 5'b?_11??: begin
  333. rdata = sd_shr_in_q;
  334. end
  335. default: begin
  336. rdata = 32'hxxxx_xxxx;
  337. end
  338. endcase
  339. end
  340. endmodule // sdcard