liblitesdcard/sdcard: use max divider of 256 (128 was not enough for the initial...
[litex.git] / litex / soc / software / liblitesdcard / sdcard.c
1 // This file is Copyright (c) 2017-2020 Florent Kermarrec <florent@enjoy-digital.fr>
2 // This file is Copyright (c) 2019 Kees Jongenburger <kees.jongenburger@gmail.com>
3 // This file is Copyright (c) 2018 bunnie <bunnie@kosagi.com>
4 // This file is Copyright (c) 2020 Antmicro <www.antmicro.com>
5 // License: BSD
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <generated/csr.h>
12 #include <generated/mem.h>
13 #include <system.h>
14
15 #include "fat/ff.h"
16 #include "fat/diskio.h"
17 #include "sdcard.h"
18
19 #ifdef CSR_SDCORE_BASE
20
21 //#define SDCARD_DEBUG
22 //#define SDCARD_CMD23_SUPPORT
23
24 #ifndef SDCARD_CLK_FREQ_INIT
25 #define SDCARD_CLK_FREQ_INIT 400000
26 #endif
27
28 #ifndef SDCARD_CLK_FREQ
29 #define SDCARD_CLK_FREQ 25000000
30 #endif
31
32 unsigned int sdcard_response[SD_CMD_RESPONSE_SIZE/4];
33
34 /*-----------------------------------------------------------------------*/
35 /* Helpers */
36 /*-----------------------------------------------------------------------*/
37
38 #define max(x, y) (((x) > (y)) ? (x) : (y))
39 #define min(x, y) (((x) < (y)) ? (x) : (y))
40
41 /*-----------------------------------------------------------------------*/
42 /* SDCard command helpers */
43 /*-----------------------------------------------------------------------*/
44
45 static void busy_wait_us(unsigned int us)
46 {
47 timer0_en_write(0);
48 timer0_reload_write(0);
49 timer0_load_write(CONFIG_CLOCK_FREQUENCY/1000000*us);
50 timer0_en_write(1);
51 timer0_update_value_write(1);
52 while(timer0_value_read()) timer0_update_value_write(1);
53 }
54
55 int sdcard_wait_cmd_done(void) {
56 unsigned int cmdevt;
57 while (1) {
58 cmdevt = sdcore_cmd_event_read();
59 busy_wait_us(1);
60 #ifdef SDCARD_DEBUG
61 printf("cmdevt: %08x\n", cmdevt);
62 #endif
63 if (cmdevt & 0x1) {
64 if (cmdevt & 0x4) {
65 #ifdef SDCARD_DEBUG
66 printf("cmdevt: SD_TIMEOUT\n");
67 #endif
68 return SD_TIMEOUT;
69 }
70 else if (cmdevt & 0x8) {
71 #ifdef SDCARD_DEBUG
72 printf("cmdevt: SD_CRCERROR\n");
73 #endif
74 return SD_CRCERROR;
75 }
76 return SD_OK;
77 }
78 }
79 }
80
81 int sdcard_wait_data_done(void) {
82 unsigned int dataevt;
83 while (1) {
84 dataevt = sdcore_data_event_read();
85 busy_wait_us(1);
86 #ifdef SDCARD_DEBUG
87 printf("dataevt: %08x\n", dataevt);
88 #endif
89 if (dataevt & 0x1) {
90 if (dataevt & 0x4)
91 return SD_TIMEOUT;
92 else if (dataevt & 0x8)
93 return SD_CRCERROR;
94 return SD_OK;
95 }
96 }
97 }
98
99 int sdcard_wait_response(void) {
100 #ifdef SDCARD_DEBUG
101 int i;
102 #endif
103 int status;
104
105 status = sdcard_wait_cmd_done();
106
107 csr_rd_buf_uint32(CSR_SDCORE_CMD_RESPONSE_ADDR, sdcard_response, SD_CMD_RESPONSE_SIZE/4);
108
109 #ifdef SDCARD_DEBUG
110 for(i = 0; i < SD_CMD_RESPONSE_SIZE/4; i++) {
111 printf("%08x ", sdcard_response[i]);
112 }
113 printf("\n");
114 #endif
115
116 return status;
117 }
118
119 /*-----------------------------------------------------------------------*/
120 /* SDCard clocker functions */
121 /*-----------------------------------------------------------------------*/
122
123 static uint32_t log2(uint32_t x)
124 {
125 uint32_t r = 0 ;
126 while(x >>= 1) r++;
127 return r;
128 }
129
130 static void sdcard_set_clk_freq(uint32_t clk_freq) {
131 uint32_t divider;
132 divider = CONFIG_CLOCK_FREQUENCY/clk_freq + 1;
133 divider = (1 << log2(divider));
134 divider = max(divider, 2);
135 divider = min(divider, 256);
136 #ifdef SDCARD_DEBUG
137 printf("Setting SDCard clk freq to ");
138 if (clk_freq > 1000000)
139 printf("%d MHz\n", (CONFIG_CLOCK_FREQUENCY/divider)/1000000);
140 else
141 printf("%d KHz\n", (CONFIG_CLOCK_FREQUENCY/divider)/1000);
142 #endif
143 sdphy_clocker_divider_write(divider);
144 }
145
146 /*-----------------------------------------------------------------------*/
147 /* SDCard commands functions */
148 /*-----------------------------------------------------------------------*/
149
150 int sdcard_go_idle(void) {
151 #ifdef SDCARD_DEBUG
152 printf("CMD0: GO_IDLE\n");
153 #endif
154 sdcore_cmd_argument_write(0x00000000);
155 sdcore_cmd_command_write((0 << 8) | SDCARD_CTRL_RESPONSE_NONE);
156 sdcore_cmd_send_write(1);
157 return sdcard_wait_response();
158 }
159
160 int sdcard_send_ext_csd(void) {
161 unsigned int arg;
162 arg = 0x000001aa;
163 #ifdef SDCARD_DEBUG
164 printf("CMD8: SEND_EXT_CSD, arg: 0x%08x\n", arg);
165 #endif
166 sdcore_cmd_argument_write(arg);
167 sdcore_cmd_command_write((8 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
168 sdcore_cmd_send_write(1);
169 return sdcard_wait_response();
170 }
171
172 int sdcard_app_cmd(int rca) {
173 #ifdef SDCARD_DEBUG
174 printf("CMD55: APP_CMD\n");
175 #endif
176 sdcore_cmd_argument_write(rca << 16);
177 sdcore_cmd_command_write((55 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
178 sdcore_cmd_send_write(1);
179 return sdcard_wait_response();
180 }
181
182 int sdcard_app_send_op_cond(int hcs) {
183 unsigned int arg;
184 arg = 0x10ff8000;
185 if (hcs)
186 arg |= 0x60000000;
187 #ifdef SDCARD_DEBUG
188 printf("ACMD41: APP_SEND_OP_COND, arg: %08x\n", arg);
189 #endif
190 sdcore_cmd_argument_write(arg);
191 sdcore_cmd_command_write((41 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
192 sdcore_cmd_send_write(1);
193 return sdcard_wait_response();
194 }
195
196 int sdcard_all_send_cid(void) {
197 #ifdef SDCARD_DEBUG
198 printf("CMD2: ALL_SEND_CID\n");
199 #endif
200 sdcore_cmd_argument_write(0x00000000);
201 sdcore_cmd_command_write((2 << 8) | SDCARD_CTRL_RESPONSE_LONG);
202 sdcore_cmd_send_write(1);
203 return sdcard_wait_response();
204 }
205
206 int sdcard_set_relative_address(void) {
207 #ifdef SDCARD_DEBUG
208 printf("CMD3: SET_RELATIVE_ADDRESS\n");
209 #endif
210 sdcore_cmd_argument_write(0x00000000);
211 sdcore_cmd_command_write((3 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
212 sdcore_cmd_send_write(1);
213 return sdcard_wait_response();
214 }
215
216 int sdcard_send_cid(unsigned int rca) {
217 #ifdef SDCARD_DEBUG
218 printf("CMD10: SEND_CID\n");
219 #endif
220 sdcore_cmd_argument_write(rca << 16);
221 sdcore_cmd_command_write((10 << 8) | SDCARD_CTRL_RESPONSE_LONG);
222 sdcore_cmd_send_write(1);
223 return sdcard_wait_response();
224 }
225
226 int sdcard_send_csd(unsigned int rca) {
227 #ifdef SDCARD_DEBUG
228 printf("CMD9: SEND_CSD\n");
229 #endif
230 sdcore_cmd_argument_write(rca << 16);
231 sdcore_cmd_command_write((9 << 8) | SDCARD_CTRL_RESPONSE_LONG);
232 sdcore_cmd_send_write(1);
233 return sdcard_wait_response();
234 }
235
236 int sdcard_select_card(unsigned int rca) {
237 #ifdef SDCARD_DEBUG
238 printf("CMD7: SELECT_CARD\n");
239 #endif
240 sdcore_cmd_argument_write(rca << 16);
241 sdcore_cmd_command_write((7 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
242 sdcore_cmd_send_write(1);
243 return sdcard_wait_response();
244 }
245
246 int sdcard_app_set_bus_width(void) {
247 #ifdef SDCARD_DEBUG
248 printf("ACMD6: SET_BUS_WIDTH\n");
249 #endif
250 sdcore_cmd_argument_write(0x00000002);
251 sdcore_cmd_command_write((6 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
252 sdcore_cmd_send_write(1);
253 return sdcard_wait_response();
254 }
255
256 int sdcard_switch(unsigned int mode, unsigned int group, unsigned int value) {
257 unsigned int arg;
258
259 #ifdef SDCARD_DEBUG
260 printf("CMD6: SWITCH_FUNC\n");
261 #endif
262 arg = (mode << 31) | 0xffffff;
263 arg &= ~(0xf << (group * 4));
264 arg |= value << (group * 4);
265
266 sdcore_cmd_argument_write(arg);
267 sdcore_block_length_write(64);
268 sdcore_block_count_write(1);
269 sdcore_cmd_command_write((6 << 8) |
270 SDCARD_CTRL_RESPONSE_SHORT |
271 (SDCARD_CTRL_DATA_TRANSFER_READ << 5));
272 sdcore_cmd_send_write(1);
273 sdcard_wait_response();
274 return sdcard_wait_data_done();
275 }
276
277 int sdcard_app_send_scr(void) {
278 #ifdef SDCARD_DEBUG
279 printf("CMD51: APP_SEND_SCR\n");
280 #endif
281 sdcore_cmd_argument_write(0x00000000);
282 sdcore_block_length_write(8);
283 sdcore_block_count_write(1);
284 sdcore_cmd_command_write((51 << 8) |
285 SDCARD_CTRL_RESPONSE_SHORT |
286 (SDCARD_CTRL_DATA_TRANSFER_READ << 5));
287 sdcore_cmd_send_write(1);
288 sdcard_wait_response();
289 return sdcard_wait_data_done();
290 }
291
292
293 int sdcard_app_set_blocklen(unsigned int blocklen) {
294 #ifdef SDCARD_DEBUG
295 printf("CMD16: SET_BLOCKLEN\n");
296 #endif
297 sdcore_cmd_argument_write(blocklen);
298 sdcore_cmd_command_write((16 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
299 sdcore_cmd_send_write(1);
300 return sdcard_wait_response();
301 }
302
303 int sdcard_write_single_block(unsigned int blockaddr) {
304 #ifdef SDCARD_DEBUG
305 printf("CMD24: WRITE_SINGLE_BLOCK\n");
306 #endif
307 int cmd_response = -1;
308 while (cmd_response != SD_OK) {
309 sdcore_cmd_argument_write(blockaddr);
310 sdcore_block_length_write(512);
311 sdcore_block_count_write(1);
312 sdcore_cmd_command_write((24 << 8) |
313 SDCARD_CTRL_RESPONSE_SHORT |
314 (SDCARD_CTRL_DATA_TRANSFER_WRITE << 5));
315 sdcore_cmd_send_write(1);
316 cmd_response = sdcard_wait_response();
317 }
318 return cmd_response;
319 }
320
321 int sdcard_write_multiple_block(unsigned int blockaddr, unsigned int blockcnt) {
322 #ifdef SDCARD_DEBUG
323 printf("CMD25: WRITE_MULTIPLE_BLOCK\n");
324 #endif
325 int cmd_response = -1;
326 while (cmd_response != SD_OK) {
327 sdcore_cmd_argument_write(blockaddr);
328 sdcore_block_length_write(512);
329 sdcore_block_count_write(blockcnt);
330 sdcore_cmd_command_write((25 << 8) |
331 SDCARD_CTRL_RESPONSE_SHORT |
332 (SDCARD_CTRL_DATA_TRANSFER_WRITE << 5));
333 sdcore_cmd_send_write(1);
334 cmd_response = sdcard_wait_response();
335 }
336 return cmd_response;
337 }
338
339 int sdcard_read_single_block(unsigned int blockaddr) {
340 #ifdef SDCARD_DEBUG
341 printf("CMD17: READ_SINGLE_BLOCK\n");
342 #endif
343 int cmd_response = -1;
344 while (cmd_response != SD_OK) {
345 sdcore_cmd_argument_write(blockaddr);
346 sdcore_block_length_write(512);
347 sdcore_block_count_write(1);
348 sdcore_cmd_command_write((17 << 8) |
349 SDCARD_CTRL_RESPONSE_SHORT |
350 (SDCARD_CTRL_DATA_TRANSFER_READ << 5));
351 sdcore_cmd_send_write(1);
352 cmd_response = sdcard_wait_response();
353 }
354 return sdcard_wait_data_done();
355 }
356
357 int sdcard_read_multiple_block(unsigned int blockaddr, unsigned int blockcnt) {
358 #ifdef SDCARD_DEBUG
359 printf("CMD18: READ_MULTIPLE_BLOCK\n");
360 #endif
361 int cmd_response = -1;
362 while (cmd_response != SD_OK) {
363 sdcore_cmd_argument_write(blockaddr);
364 sdcore_block_length_write(512);
365 sdcore_block_count_write(blockcnt);
366 sdcore_cmd_command_write((18 << 8) |
367 SDCARD_CTRL_RESPONSE_SHORT |
368 (SDCARD_CTRL_DATA_TRANSFER_READ << 5));
369 sdcore_cmd_send_write(1);
370 cmd_response = sdcard_wait_response();
371 }
372 return cmd_response;
373 }
374
375 int sdcard_stop_transmission(void) {
376 #ifdef SDCARD_DEBUG
377 printf("CMD12: STOP_TRANSMISSION\n");
378 #endif
379 sdcore_cmd_argument_write(0x0000000);
380 sdcore_cmd_command_write((12 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
381 sdcore_cmd_send_write(1);
382 return sdcard_wait_response();
383 }
384
385 int sdcard_send_status(unsigned int rca) {
386 #ifdef SDCARD_DEBUG
387 printf("CMD13: SEND_STATUS\n");
388 #endif
389 sdcore_cmd_argument_write(rca << 16);
390 sdcore_cmd_command_write((13 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
391 sdcore_cmd_send_write(1);
392 return sdcard_wait_response();
393 }
394
395 int sdcard_set_block_count(unsigned int blockcnt) {
396 #ifdef SDCARD_DEBUG
397 printf("CMD23: SET_BLOCK_COUNT\n");
398 #endif
399 sdcore_cmd_argument_write(blockcnt);
400 sdcore_cmd_command_write((23 << 8) | SDCARD_CTRL_RESPONSE_SHORT);
401 sdcore_cmd_send_write(1);
402 return sdcard_wait_response();
403 }
404
405 void sdcard_decode_cid(void) {
406 printf(
407 "CID Register: 0x%08x%08x%08x%08x\n"
408 "Manufacturer ID: 0x%x\n"
409 "Application ID 0x%x\n"
410 "Product name: %c%c%c%c%c\n",
411 sdcard_response[0],
412 sdcard_response[1],
413 sdcard_response[2],
414 sdcard_response[3],
415
416 (sdcard_response[0] >> 16) & 0xffff,
417
418 sdcard_response[0] & 0xffff,
419
420 (sdcard_response[1] >> 24) & 0xff,
421 (sdcard_response[1] >> 16) & 0xff,
422 (sdcard_response[1] >> 8) & 0xff,
423 (sdcard_response[1] >> 0) & 0xff,
424 (sdcard_response[2] >> 24) & 0xff
425 );
426 int crc = sdcard_response[3] & 0x000000FF;
427 int month = (sdcard_response[3] & 0x00000F00) >> 8;
428 int year = (sdcard_response[3] & 0x000FF000) >> 12;
429 int psn = ((sdcard_response[3] & 0xFF000000) >> 24) | ((sdcard_response[2] & 0x00FFFFFF) << 8);
430 printf( "CRC: %02x\n", crc);
431 printf( "Production date(m/yy): %d/%d\n", month, year);
432 printf( "PSN: %08x\n", psn);
433 printf( "OID: %c%c\n", (sdcard_response[0] & 0x00FF0000) >> 16, (sdcard_response[0] & 0x0000FF00) >> 8);
434 }
435
436 void sdcard_decode_csd(void) {
437 /* FIXME: only support CSR structure version 2.0 */
438
439 int size = ((sdcard_response[2] & 0xFFFF0000) >> 16) + ((sdcard_response[1] & 0x000000FF) << 16) + 1;
440 printf(
441 "CSD Register: 0x%x%08x%08x%08x\n"
442 "Max data transfer rate: %d MB/s\n"
443 "Max read block length: %d bytes\n"
444 "Device size: %d GB\n",
445 sdcard_response[0],
446 sdcard_response[1],
447 sdcard_response[2],
448 sdcard_response[3],
449
450 (sdcard_response[0] >> 24) & 0xff,
451
452 (1 << ((sdcard_response[1] >> 16) & 0xf)),
453
454 size * 512 / (1024 * 1024)
455 );
456 }
457
458 /*-----------------------------------------------------------------------*/
459 /* SDCard user functions */
460 /*-----------------------------------------------------------------------*/
461
462 int sdcard_init(void) {
463 unsigned short rca;
464 uint16_t timeout;
465
466 /* Set SD clk freq to Initialization frequency */
467 sdcard_set_clk_freq(SDCARD_CLK_FREQ_INIT);
468 busy_wait(1);
469
470 for (timeout=1000; timeout>0; timeout--) {
471 /* Set SDCard in SPI Mode (generate 80 dummy clocks) */
472 sdphy_init_initialize_write(1);
473 busy_wait(1);
474
475 /* Set SDCard in Idle state */
476 if (sdcard_go_idle() == SD_OK)
477 break;
478 busy_wait(1);
479 }
480 if (timeout == 0)
481 return 0;
482
483 /* Set SDCard voltages, only supported by ver2.00+ SDCards */
484 if (sdcard_send_ext_csd() != SD_OK)
485 return 0;
486
487 /* Set SD clk freq to Operational frequency */
488 sdcard_set_clk_freq(SDCARD_CLK_FREQ);
489 busy_wait(1);
490
491 /* Set SDCard in Operational state */
492 for (timeout=1000; timeout>0; timeout--) {
493 sdcard_app_cmd(0);
494 if (sdcard_app_send_op_cond(1) != SD_OK)
495 break;
496 busy_wait(1);
497 }
498 if (timeout == 0)
499 return 0;
500
501 /* Send identification */
502 if (sdcard_all_send_cid() != SD_OK)
503 return 0;
504 #ifdef SDCARD_DEBUG
505 sdcard_decode_cid();
506 #endif
507 /* Set Relative Card Address (RCA) */
508 if (sdcard_set_relative_address() != SD_OK)
509 return 0;
510 rca = (sdcard_response[3] >> 16) & 0xffff;
511
512 /* Set CID */
513 if (sdcard_send_cid(rca) != SD_OK)
514 return 0;
515 #ifdef SDCARD_DEBUG
516 /* FIXME: add cid decoding (optional) */
517 #endif
518
519 /* Set CSD */
520 if (sdcard_send_csd(rca) != SD_OK)
521 return 0;
522 #ifdef SDCARD_DEBUG
523 sdcard_decode_csd();
524 #endif
525
526 /* Select card */
527 if (sdcard_select_card(rca) != SD_OK)
528 return 0;
529
530 /* Set bus width */
531 if (sdcard_app_cmd(rca) != SD_OK)
532 return 0;
533 if(sdcard_app_set_bus_width() != SD_OK)
534 return 0;
535
536 /* Switch speed */
537 if (sdcard_switch(SD_SWITCH_SWITCH, SD_GROUP_ACCESSMODE, SD_SPEED_SDR50) != SD_OK)
538 return 0;
539
540 /* Switch driver strength */
541 if (sdcard_switch(SD_SWITCH_SWITCH, SD_GROUP_DRIVERSTRENGTH, SD_DRIVER_STRENGTH_D) != SD_OK)
542 return 0;
543
544 /* Send SCR */
545 /* FIXME: add scr decoding (optional) */
546 if (sdcard_app_cmd(rca) != SD_OK)
547 return 0;
548 if (sdcard_app_send_scr() != SD_OK)
549 return 0;
550
551 /* Set block length */
552 if (sdcard_app_set_blocklen(512) != SD_OK)
553 return 0;
554
555 return 1;
556 }
557
558 #ifdef CSR_SDBLOCK2MEM_BASE
559
560 void sdcard_read(uint32_t sector, uint32_t count, uint8_t* buf)
561 {
562 /* Initialize DMA Writer */
563 sdblock2mem_dma_enable_write(0);
564 sdblock2mem_dma_base_write((uint32_t) buf);
565 sdblock2mem_dma_length_write(512*count);
566 sdblock2mem_dma_enable_write(1);
567
568 /* Read Block(s) from SDCard */
569 #ifdef SDCARD_CMD23_SUPPORT
570 sdcard_set_block_count(count);
571 #endif
572 sdcard_read_multiple_block(sector, count);
573
574 /* Wait for DMA Writer to complete */
575 while ((sdblock2mem_dma_done_read() & 0x1) == 0);
576
577 sdcard_stop_transmission();
578
579 #ifndef CONFIG_CPU_HAS_DMA_BUS
580 /* Flush CPU caches */
581 flush_cpu_dcache();
582 #ifdef CONFIG_L2_SIZE
583 flush_l2_cache();
584 #endif
585 #endif
586 }
587
588 #endif
589
590 #ifdef CSR_SDMEM2BLOCK_BASE
591
592 void sdcard_write(uint32_t sector, uint32_t count, uint8_t* buf)
593 {
594 while (count--) {
595 /* Initialize DMA Reader */
596 sdmem2block_dma_enable_write(0);
597 sdmem2block_dma_base_write((uint32_t) buf);
598 sdmem2block_dma_length_write(512);
599 sdmem2block_dma_enable_write(1);
600
601 /* Wait for DMA Reader to complete */
602 while ((sdmem2block_dma_done_read() & 0x1) == 0);
603
604 /* Write Single Block to SDCard */
605 #ifndef SDCARD_CMD23_SUPPORT
606 sdcard_set_block_count(1);
607 #endif
608 sdcard_write_single_block(sector);
609
610 sdcard_stop_transmission();
611
612 /* Update buf/sector */
613 buf += 512;
614 sector += 1;
615 }
616 }
617 #endif
618
619 /*-----------------------------------------------------------------------*/
620 /* SDCard FatFs disk functions */
621 /*-----------------------------------------------------------------------*/
622
623 static DSTATUS sdcardstatus = STA_NOINIT;
624
625 DSTATUS disk_status(uint8_t drv) {
626 if (drv) return STA_NOINIT;
627 return sdcardstatus;
628 }
629
630 DSTATUS disk_initialize(uint8_t drv) {
631 if (drv) return STA_NOINIT;
632 if (sdcardstatus)
633 sdcardstatus = sdcard_init() ? 0 : STA_NOINIT;
634 return sdcardstatus;
635 }
636
637 DRESULT disk_read(uint8_t drv, uint8_t *buf, uint32_t sector, uint32_t count) {
638 sdcard_read(sector, count, buf);
639 return RES_OK;
640 }
641
642 #endif /* CSR_SDCORE_BASE */