-
Notifications
You must be signed in to change notification settings - Fork 35
/
sdiodrv.c
2027 lines (1769 loc) · 53.3 KB
/
sdiodrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
//
// Filename: sw/sdiodrv.c
// {{{
// Project: SD-Card controller
//
// Purpose: This is the software driver for the SDIO controller, used for
// interacting with an SD Card in SDIO (4b) mode.
//
// Entry points: This driver has four external entry points, which can be used
// to define it's operation:
//
// 1. sdio_init
// This should be called first. It will generate the driver's
// data structure, and attempt to interact with the card.
// It needs to be passed the hardware address of the device in
// the address map.
// 2. sdio_write(dev, sector, count, buf)
// Writes "count" sectors of data to the device, starting at
// the sector numbered "sector". The data are sourced from the
// *buf pointer, which *must* either be word aligned or the CPU
// must be able to handle unaligned accesses.
//
// If SDMULTI is set, the write multiple blocks command will be
// used for better performance. SDMULTI *must* be set in order to
// use the DMA.
//
// 3. sdio_read(dev, sector, count, buf)
// Reads "count" sectors of data to the device, starting at
// the sector numbered "sector". The data are saved into the
// *buf pointer, which *must* either be word aligned or the CPU
// must be able to handle unaligned accesses.
//
// If SDMULTI is set, the read multiple blocks command will be
// used for better performance. This requires that the clock
// shutdown bit be set, to avoid losing data. As with writing,
// SDMULTI must be set in order to use the DMA.
//
// 4. sdio_ioctl
//
// Issues:
// - This controller only handles 3.3V mode. Even if hardware exists for
// switching to 1.8V, this driver doesn't (yet) enable it.
// - The controller doesn't really recover well from a failed init.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2024, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD, LBA_t, UINT;
#include <diskio.h>
#include "sdiodrv.h"
// tx* -- debugging output functions
// {{{
// Debugging isn't quite as simple as using printf(), since I want to guarantee
// that I can still compile and build this into a memory that isn't large enough
// to hold a printf function. The txstr(), txchr(), and txhex() functions fit
// that low memory footprint need. For cases where these are not sufficient,
// we use the STDIO_DEBUG flag to determine if the regular ?rintf() functions
// are available.
#ifndef TXFNS_H
#include <stdio.h>
#define txchr(A) putchar(A)
#define txstr(A) fputs(A, stdout)
#define txhex(A) printf("%08x", A)
#define txdecimal(A) printf("%d", A)
#define STDIO_DEBUG
#else
// extern void txstr(const char *);
// extern void txhex(unsigned);
// extern void txdecimal(int);
#define printf(...)
#endif
// }}}
// WBScope
// {{{
// The following defines may be useful when a WBScope has been attached
// to the device. They are mostly useful for debugging. If no WBScope
// is attached, these macros are given null definitions.
#ifdef _BOARD_HAS_SDIOSCOPE
#define SET_SCOPE _sdioscope->s_ctrl = 0x04000100
#define TRIGGER_SCOPE _sdioscope->s_ctrl = 0xff000100
#else
#define SET_SCOPE
#define TRIGGER_SCOPE
#endif
// }}}
// SDINFO & SDDEBUG
// {{{
// These are designed to be compile time constants, to allow the compiler to
// remove the logic they generate for space reasons.
// SDDEBUG: Set to turn on debugging output. Ideally, debugging output
// should not be necessary in a *working* design, so its use is
// primary until the design starts working. This debugging
// output will tell you (among other things) when the design
// issues commands, what command are issued, which routines are
// called. etc.
// SDINFO: Set to turns on a verbose reporting. This will dump values of
// registers, together with their meanings. When reading,
// it will dump sectors read. Often requires SDDEBUG.
static const int SDINFO = 0, SDDEBUG = 0;
// }}}
// Compile time DMA controls
// {{{
// OPT_SDIODMA will be defined if the design was built in an environment that
// (may have potentially) had a hardware DMA built into it. (We'll still
// check.) If not, SDEXTDMA controls whether or not calls to an _external_
// DMA should be generated. These would be calls to the ZipCPU's DMA. To
// avoid all external DMA calls, simply set SDEXTDMA to zero.
#ifdef OPT_SDIODMA
static const int SDEXTDMA=0;
#else
static const int SDEXTDMA=1;
#endif
// }}}
// SDMULTI
// {{{
// SDMULTI: Controls whether the read multiple block or write multiple block
// commands will be used. Set to 1 to use these commands, 0 otherwise.
// SDMULTI *must* be set to use the internal DMA, otherwise only block level
// commands will be issued.
static const int SDMULTI = 1;
// }}}
// }}}
#define NEW_MUTEX
#define GRAB_MUTEX
#define RELEASE_MUTEX
#ifndef CLEAR_DCACHE
#define CLEAR_DCACHE
#endif
typedef struct SDIODRV_S {
SDIO *d_dev;
uint32_t d_CID[4], d_OCR;
char d_SCR[8], d_CSD[16];
uint16_t d_RCA;
uint32_t d_sector_count, d_block_size;
} SDIODRV;
static const uint32_t
// Command bit enumerations
SDIO_RNONE = 0x00000000,
SDIO_R1 = 0x00000100,
SDIO_R2 = 0x00000200,
SDIO_R1b = 0x00000300,
SDIO_WRITE = 0x00000400,
SDIO_MEM = 0x00000800,
SDIO_FIFO = 0x00001000,
SDIO_DMA = 0x00002000,
SDIO_CMDBUSY = 0x00004000,
SDIO_ERR = 0x00008000,
SDIO_CMDTMOUT = 0x00000000,
SDIO_CMDEOKAY = 0x00010000,
SDIO_CMDCRCER = 0x00020000,
SDIO_CMDFRMER = 0x00030000,
SDIO_CMDECODE = 0x00030000,
SDIO_REMOVED = 0x00040000,
SDIO_PRESENTN = 0x00080000,
SDIO_CARDBUSY = 0x00100000,
SDIO_BUSY = (SDIO_CARDBUSY|SDIO_CMDBUSY|SDIO_DMA|SDIO_MEM),
SDIO_CMDERR = 0x00200000,
SDIO_RXERR = 0x00400000, // RX Error present
SDIO_RXECODE = 0x00800000, // RX Error code
SDIO_DMAERR = 0x01000000,
SDIO_HWRESET = 0x02000000,
SDIO_ACK = 0x04000000, // Expect a CRC ACK token
SDIO_RESET = 0x52000000,
// PHY enumerations
SDPHY_DDR = 0x00004100, // Requires CK90
SDPHY_DS = 0x00004300, // Requires DDR & CK90
SDPHY_W1 = 0x00000000,
SDPHY_W4 = 0x00000400,
SDPHY_W8 = 0x00000800,
SDPHY_WBEST = 0x00000c00,
SDPHY_PPDAT = 0x00001000, // Push pull drive for data
SDPHY_PPCMD = 0x00002000, // Push pull drive for cmd wire
SDPHY_PUSHPULL = SDPHY_PPDAT | SDPHY_PPCMD,
SDIOCK_CK90 = 0x00004000,
SDIOCK_SHUTDN = 0x00008000,
SDPHY_PHASEMSK= 0x001f0000,
// IO clock speeds
SDIOCK_100KHZ = 0x000000fc,
SDIOCK_200KHZ = 0x0000007f,
SDIOCK_400KHZ = 0x00000041,
SDIOCK_1MHZ = 0x0000001b,
SDIOCK_5MHZ = 0x00000007,
SDIOCK_12MHZ = 0x00000004,
SDIOCK_25MHZ = 0x00000003,
SDIOCK_50MHZ = 0x00000002,
SDIOCK_100MHZ = 0x00000001,
SDIOCK_200MHZ = 0x00000000,
SDIOCK_MASK = 0x000000ff,
SDPHY_1P2V = 0x00400000,
SDIOCK_DS = SDIOCK_25MHZ | SDPHY_W4 | SDPHY_PUSHPULL,
SDIOCK_HS = SDIOCK_50MHZ | SDPHY_W4 | SDPHY_PUSHPULL,
// Speed abbreviations
SDIOCK_SDR50 = SDIOCK_50MHZ | SDPHY_W4 | SDPHY_PUSHPULL | SDPHY_1P2V,
SDIOCK_DDR50 = SDIOCK_50MHZ | SDPHY_W4 | SDPHY_PUSHPULL | SDPHY_DDR | SDPHY_1P2V,
SDIOCK_SDR104 = SDIOCK_100MHZ | SDPHY_W4 | SDPHY_PUSHPULL | SDPHY_1P2V,
SDIOCK_SDR200 = SDIOCK_200MHZ | SDPHY_W4 | SDPHY_PUSHPULL | SDPHY_1P2V,
// SDIOCK_HS400= SDIOCK_200MHZ | SDPHY_W4 | SDPHY_PUSHPULL | SDPHY_DS,
//
SPEED_SLOW = SDIOCK_400KHZ,
SPEED_DEFAULT= SDIOCK_DS,
SPEED_FAST = SDIOCK_HS,
//
SECTOR_16B = 0x04000000,
SECTOR_512B = 0x09000000,
SECTOR_MASK = 0x0f000000,
//
SDIO_CMD = 0x00000040,
SDIO_READREG = SDIO_CMD | SDIO_R1,
SDIO_READREGb = SDIO_CMD | SDIO_R1b,
SDIO_READR2 = (SDIO_CMD | SDIO_R2),
SDIO_WRITEBLK = (SDIO_CMD | SDIO_R1b | SDIO_ERR
| SDIO_ACK | SDIO_WRITE | SDIO_MEM) + 24,
SDIO_WRMULTI = (SDIO_CMD | SDIO_R1b
| SDIO_ACK | SDIO_WRITE | SDIO_MEM) + 25,
SDIO_WRDMA = SDIO_WRMULTI | SDIO_DMA,
SDIO_READBLK = (SDIO_CMD | SDIO_R1
| SDIO_MEM) + 17,
SDIO_RDMULTI = (SDIO_CMD | SDIO_R1
| SDIO_MEM) + 18,
SDIO_READDMA = SDIO_RDMULTI | SDIO_DMA,
SDIO_R1ERR = 0xff800000;
static void sdio_wait_while_busy(SDIODRV *dev);
static void sdio_go_idle(SDIODRV *dev);
static void sdio_all_send_cid(SDIODRV *dev);
static void sdio_dump_cid(SDIODRV *dev);
static uint32_t sdio_send_rca(SDIODRV *dev);
static void sdio_select_card(SDIODRV *dev); // CMD7
static uint32_t sdio_send_if_cond(SDIODRV *dev, uint32_t ifcond); // CMD8
static uint32_t sdio_send_op_cond(SDIODRV *dev, uint32_t opcond); // ACMD41
static void sdio_set_bus_width(SDIODRV *dev, uint32_t width); // CMD6
static void sdio_send_app_cmd(SDIODRV *dev); // CMD 55
static uint32_t sdio_read_ocr(SDIODRV *dev, uint32_t width); // CMD 58
static void sdio_dump_err(unsigned);
static void sdio_dump_scr(SDIODRV *dev);
static void sdio_dump_ocr(SDIODRV *dev);
static unsigned sdio_get_r1(SDIODRV *dev);
static void sdio_dump_r1(unsigned);
static int sdio_write_block(SDIODRV *dev, uint32_t sector, uint32_t *buf); // CMD 24
static int sdio_read_block(SDIODRV *dev, uint32_t sector, uint32_t *buf); // CMD 17
extern SDIODRV *sdio_init(SDIO *dev);
extern int sdio_write(SDIODRV *dev, const unsigned sector, const unsigned count, const char *buf);
extern int sdio_read(SDIODRV *dev, const unsigned sector, const unsigned count, char *buf);
extern int sdio_ioctl(SDIODRV *dev, char cmd, char *buf);
void sdio_wait_while_busy(SDIODRV *dev) {
// {{{
// Could also do a system call and yield to the scheduler while waiting
// if (SDIO_OS) {
// os_wait(dev->d_int);
// } else if (SDIO_INT) {
// // Can wait for an interrupt here, such as by calling an
// // external wait_int function with our interrupt ID.
// wait_int(dev->d_int);
// } else {
// Busy wait implementation
uint32_t st;
st = dev->d_dev->sd_cmd;
while(st & SDIO_BUSY)
st = dev->d_dev->sd_cmd;
// }
}
// }}}
void sdio_go_idle(SDIODRV *dev) { // CMD0
// {{{
dev->d_dev->sd_data = 0;
dev->d_dev->sd_cmd = SDIO_REMOVED | SDIO_CMD | SDIO_RNONE | SDIO_ERR;
sdio_wait_while_busy(dev);
if (SDDEBUG && SDINFO) {
unsigned c = dev->d_dev->sd_cmd;
unsigned r = dev->d_dev->sd_data;
txstr("CMD0: SEND_GO_IDLE\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
}
// }}}
void sdio_all_send_cid(SDIODRV *dev) { // CMD2
// {{{
unsigned c;
if (SDDEBUG) txstr("READ-CID\n");
dev->d_dev->sd_data = 0;
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_READR2)+2;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
dev->d_CID[0] = dev->d_dev->sd_fifa;
dev->d_CID[1] = dev->d_dev->sd_fifa;
dev->d_CID[2] = dev->d_dev->sd_fifa;
dev->d_CID[3] = dev->d_dev->sd_fifa;
if (SDDEBUG && (c & SDIO_ERR)) {
TRIGGER_SCOPE;
txstr(" SD-ERR: "); txhex(c); txstr("\n");
}
if (SDINFO) {
unsigned r = dev->d_dev->sd_data;
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
sdio_dump_cid(dev);
}
}
// }}}
void sdio_send_cid(SDIODRV *dev) { // CMD10
// {{{
if (SDDEBUG) txstr("SEND-CID\n");
dev->d_dev->sd_data = (dev->d_RCA << 16);
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_READR2) + 10;
sdio_wait_while_busy(dev);
dev->d_CID[0] = dev->d_dev->sd_fifa;
dev->d_CID[1] = dev->d_dev->sd_fifa;
dev->d_CID[2] = dev->d_dev->sd_fifa;
dev->d_CID[3] = dev->d_dev->sd_fifa;
if (SDINFO) {
unsigned c = dev->d_dev->sd_cmd;
unsigned r = dev->d_dev->sd_data;
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
sdio_dump_cid(dev);
}
}
// }}}
void sdio_dump_cid(SDIODRV *dev) {
// {{{
txstr("CID: ");
txhex(dev->d_CID[0]); txstr(":");
txhex(dev->d_CID[1]); txstr(":");
txhex(dev->d_CID[2]); txstr(":");
txhex(dev->d_CID[3]); txstr("\n");
#ifdef STDIO_DEBUG
unsigned sn, md;
sn = dev->d_CID[2];
sn = (sn << 8) | (dev->d_CID[3] >> 24) & 0x0ff;
md = (dev->d_CID[3] >> 8) & 0x0fff;
printf("CID:\n"
"\tManufacturer ID: 0x%02x\n"
"\tApplication ID: %c%c\n"
"\tProduct Name: %c%c%c%c%c\n"
"\tProduct Revision: %x.%x\n"
"\tSerial Number: 0x%0x\n",
(dev->d_CID[0] >> 24)&0x0ff, // MFR ID
(dev->d_CID[0] >> 16)&0x0ff, // APP ID
(dev->d_CID[0] >> 8)&0x0ff,
(dev->d_CID[0] )&0x0ff, // Prod Name[0]
(dev->d_CID[1] >> 24)&0x0ff, // Prod Name[1]
(dev->d_CID[1] >> 16)&0x0ff, // Prod Name[2]
(dev->d_CID[1] >> 8)&0x0ff, // Prod Name[3]
(dev->d_CID[1] )&0x0ff, // Prod Name[4]
(dev->d_CID[2] >> 28)&0x00f, // Revision Hi
(dev->d_CID[2] >> 24)&0x00f, sn); // Lo, and Serial #
printf(
"\tYear of Man.: %d\n"
"\tMonth of Man.: %d\n",
((md>>4)+2000), md&0x0f);
#endif
}
// }}}
uint32_t sdio_send_rca(SDIODRV *dev) { // CMD3
// {{{
unsigned c, r;
if (SDDEBUG) txstr("SEND-RCA\n");
dev->d_dev->sd_data = 0;
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_READREG)+3;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
if (c & SDIO_ERR) {
TRIGGER_SCOPE;
if (SDDEBUG) {
txstr(" SD-ERR: ");
txhex(c);
txstr("\n");
if (SDINFO)
sdio_dump_err(c);
}
dev->d_dev->sd_data = 0;
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_READREG)+3;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
if (c & SDIO_ERR) {
if (SDDEBUG) {
txstr(" SD-ERR: ");
txhex(c);
txstr("\n");
if (SDINFO)
sdio_dump_err(c);
}
return 0;
}
}
r = dev->d_dev->sd_data;
if (SDINFO) {
if (!SDDEBUG) txstr("SEND-RCA\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
return dev->d_RCA = (r >> 16)&0x0ffff;
}
// }}}
void sdio_select_card(SDIODRV *dev) { // CMD7
// {{{
unsigned c, r;
dev->d_dev->sd_data = dev->d_RCA << 16;
dev->d_dev->sd_cmd = SDIO_READREGb + 7;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
if (SDDEBUG && ((c & SDIO_ERR) || SDINFO)) {
if (c & SDIO_ERR)
TRIGGER_SCOPE;
txstr("CMD7: SELECT_CARD ("); txhex(dev->d_RCA);txstr(")\n");
if (c & SDIO_ERR) {
txstr(" SD-ERR: "); txhex(c); txstr("\n");
sdio_dump_err(c);
}
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
}
// }}}
uint32_t sdio_send_if_cond(SDIODRV *dev, uint32_t ifcond) { // CMD8
// {{{
unsigned c, r;
dev->d_dev->sd_data = ifcond;
dev->d_dev->sd_cmd = SDIO_READREG+8;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
if (SDDEBUG && SDINFO) {
txstr("CMD8: SEND_IF_COND ("); txhex(ifcond); txstr(")\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
return r;
}
// }}}
uint32_t sdio_send_op_cond(SDIODRV *dev, uint32_t opcond) { // ACMD41
// {{{
unsigned c, r;
sdio_send_app_cmd(dev);
dev->d_dev->sd_data = opcond;
dev->d_dev->sd_cmd = SDIO_READREG+41;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
dev->d_OCR = r;
if (SDDEBUG && SDINFO) {
txstr("ACMD41: SEND_OP_COND : "); txhex(opcond); txstr("\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
return r;
}
// }}}
void sdio_set_bus_width(SDIODRV *dev, uint32_t width) { // CMD6
// {{{
sdio_send_app_cmd(dev);
dev->d_dev->sd_data = width;
dev->d_dev->sd_cmd = SDIO_READREG+6;
sdio_wait_while_busy(dev);
if (SDDEBUG && SDINFO) {
unsigned c, r;
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
txstr("CMD6: SET_BUS_WIDTH\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
}
// }}}
void sdio_send_app_cmd(SDIODRV *dev) { // CMD 55
// {{{
dev->d_dev->sd_data = dev->d_RCA << 16;
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_READREG)+55;
sdio_wait_while_busy(dev);
if (SDDEBUG && SDINFO) {
unsigned c, r;
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
txstr("CMD55: SEND_APP_CMD\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
}
// }}}
void sdio_dump_ocr(SDIODRV *dev) {
// {{{
if (SDINFO) {
txstr("READ-OCR: OCR = "); txhex(dev->d_OCR); txstr("\n");
if (0 == (dev->d_OCR & 0x80000000))
txstr(" Card is still powering up\n");
if (dev->d_OCR & 0x40000000)
txstr(" CCS: High capacity support\n");
if (dev->d_OCR & 0x20000000)
txstr(" UHS: UHS-II card\n");
if (dev->d_OCR & 0x01000000)
txstr(" S18A: Switching to 1.8V allowed\n");
#ifdef STDIO_DEBUG
int mxv = 0, mnv = 0;
if (dev->d_OCR & 0x00800000) {
if (mxv == 0||mxv <= 36)
mxv = 36;
if (mnv == 0||mnv >= 35)
mnv = 35;
} if (dev->d_OCR & 0x00400000) {
if (mxv == 0||mxv <= 35)
mxv = 35;
if (mnv == 0||mnv >= 34)
mnv = 34;
} if (dev->d_OCR & 0x00200000) {
if (mxv == 0||mxv <= 34)
mxv = 34;
if (mnv == 0||mnv >= 33)
mnv = 33;
} if (dev->d_OCR & 0x00100000) {
if (mxv == 0||mxv <= 33)
mxv = 33;
if (mnv == 0||mnv >= 32)
mnv = 32;
} if (dev->d_OCR & 0x00080000) {
if (mxv == 0||mxv <= 32)
mxv = 32;
if (mnv == 0||mnv >= 31)
mnv = 31;
} if (dev->d_OCR & 0x00040000) {
if (mxv == 0||mxv <= 31)
mxv = 31;
if (mnv == 0||mnv >= 30)
mnv = 30;
} if (dev->d_OCR & 0x00020000) {
if (mxv == 0||mxv <= 30)
mxv = 30;
if (mnv == 0||mnv >= 29)
mnv = 29;
} if (dev->d_OCR & 0x00010000) {
if (mxv == 0||mxv <= 29)
mxv = 29;
if (mnv == 0||mnv >= 28)
mnv = 28;
} if (dev->d_OCR & 0x00008000) {
if (mxv == 0||mxv <= 28)
mxv = 28;
if (mnv == 0||mnv >= 27)
mnv = 27;
} printf(" Voltage ranges supported: %d.%dV - %d.%dV\n",
(mxv/10), (mxv%10), (mnv/10), (mnv%10));
#else
if (dev->d_OCR & 0x00800000) txstr(" 3.6-3.5 V allowed\n");
if (dev->d_OCR & 0x00400000) txstr(" 3.5-3.4 V allowed\n");
if (dev->d_OCR & 0x00200000) txstr(" 3.4-3.3 V allowed\n");
if (dev->d_OCR & 0x00100000) txstr(" 3.3-3.2 V allowed\n");
if (dev->d_OCR & 0x00080000) txstr(" 3.2-3.1 V allowed\n");
if (dev->d_OCR & 0x00040000) txstr(" 3.1-3.0 V allowed\n");
if (dev->d_OCR & 0x00020000) txstr(" 3.0-2.9 V allowed\n");
if (dev->d_OCR & 0x00010000) txstr(" 2.9-2.8 V allowed\n");
if (dev->d_OCR & 0x00008000) txstr(" 2.8-2.7 V allowed\n");
#endif
}
}
// }}}
void sdio_read_scr(SDIODRV *dev) { // ACMD 51
// {{{
uint32_t phy = dev->d_dev->sd_phy;
phy &= ~SECTOR_MASK;
phy |= (3 << 24); // 64 bits = 8 bytes = 2^3 bytes
dev->d_dev->sd_phy = phy;
sdio_send_app_cmd(dev);
dev->d_dev->sd_data = 0;
dev->d_dev->sd_cmd = (SDIO_ERR|SDIO_MEM|SDIO_READREG)+51;
sdio_wait_while_busy(dev);
if (SDDEBUG && SDINFO) {
// {{{
unsigned c, r;
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
txstr("ACMD51: SEND_SCR\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
}
// }}}
if (SDINFO) txstr(" SCR : ");
for(int k=0; k<8; k+= sizeof(uint32_t)) {
unsigned uv;
uv = dev->d_dev->sd_fifa;
if (SDINFO) { txhex(uv); if (k < 4) txstr(":"); }
dev->d_SCR[k + 3] = uv & 0x0ff; uv >>= 8;
dev->d_SCR[k + 2] = uv & 0x0ff; uv >>= 8;
dev->d_SCR[k + 1] = uv & 0x0ff; uv >>= 8;
dev->d_SCR[k + 0] = uv;
} if (SDINFO) txstr("\n");
phy &= ~SECTOR_MASK;
phy |= SECTOR_512B;
dev->d_dev->sd_phy = phy;
if (SDINFO)
sdio_dump_scr(dev);
}
// }}}
void sdio_dump_scr(SDIODRV *dev) {
// {{{
txstr(" SCR_STRUCTURE: "); txhex((dev->d_SCR[0] >> 4)& 0x0f); txstr("\n");
txstr(" SD_SPEC : "); txhex(dev->d_SCR[0]& 0x0f); txstr("\n");
txstr(" STATAFTERERAS: "); txhex((dev->d_SCR[1]&0x80)?1:0); txstr("\n");
txstr(" SD_SECURITY : "); txhex((dev->d_SCR[1]>>4) & 0x07); txstr("\n");
txstr(" SD_BUS_WIDTHS: "); txhex(dev->d_SCR[1] & 0x0f); txstr("\n");
txstr(" EX_SECURITY : "); txhex((dev->d_SCR[2]>>3) & 0x07); txstr("\n");
txstr(" SD_SPEC3 : "); txhex((dev->d_SCR[2] & 0x80) ? 1:0); txstr("\n");
txstr(" SD_SPEC4 : "); txhex((dev->d_SCR[2] & 0x04) ? 1:0); txstr("\n");
txstr(" SD_SPECX : "); txhex(((dev->d_SCR[3]>>6)&0x03)
| ((dev->d_SCR[2]&0x03)<<2)); txstr("\n");
txstr(" CMD_SUPPORT : "); txhex(dev->d_SCR[3] & 0x0f); txstr("\n");
}
// }}}
void sdio_dump_err(unsigned c) {
// {{{
if (SDINFO && (c & SDIO_ERR)) {
if (c & SDIO_REMOVED)
txstr(" Card has been removed\n");
if (c & SDIO_DMAERR)
txstr(" DMA Bus Error\n");
if (c & SDIO_CMDERR) {
if (SDIO_CMDTMOUT == (c & SDIO_CMDECODE))
txstr(" CMD-Timeout\n");
else if (SDIO_CMDCRCER == (c & SDIO_CMDECODE))
txstr(" CMD-CRC Error\n");
else if (SDIO_CMDFRMER == (c & SDIO_CMDECODE))
txstr(" CMD-Frame Error (Bad stop bit)\n");
} else if (c & SDIO_RXERR) {
if (c & SDIO_RXECODE)
txstr(" Rx CRC Error\n");
else
txstr(" Data Watchdog timeout\n");
}
}
}
// }}}
void sdio_read_csd(SDIODRV *dev) { // CMD 9
// {{{
if (SDDEBUG) txstr("READ-CSD\n");
dev->d_dev->sd_data = dev->d_RCA << 16;
dev->d_dev->sd_cmd = (SDIO_CMD | SDIO_R2 | SDIO_ERR)+9;
sdio_wait_while_busy(dev);
if (SDDEBUG && SDINFO) {
// {{{
unsigned c, r;
c = dev->d_dev->sd_cmd;
r = dev->d_dev->sd_data;
txstr("CMD9: SEND_CSD\n");
txstr(" Cmd: "); txhex(c); txstr("\n");
txstr(" Data: "); txhex(r); txstr("\n");
if (c & SDIO_ERR) {
TRIGGER_SCOPE;
txstr(" SD-ERR: "); txhex(c); txstr("\n");
sdio_dump_err(c);
}
}
// }}}
if (SDINFO) txstr(" CSD : ");
for(int k=0; k<16; k+= sizeof(uint32_t)) {
unsigned uv;
uv = dev->d_dev->sd_fifa;
if (SDINFO) { txhex(uv); if (k < 12) txstr(":"); }
dev->d_CSD[k + 3] = uv & 0x0ff; uv >>= 8;
dev->d_CSD[k + 2] = uv & 0x0ff; uv >>= 8;
dev->d_CSD[k + 1] = uv & 0x0ff; uv >>= 8;
dev->d_CSD[k + 0] = uv;
}
unsigned C_SIZE, READ_BL_LEN, CSD_STRUCTURE;
CSD_STRUCTURE = (dev->d_CSD[0]>>6)&3;
if (SDINFO && SDDEBUG) {
txstr("\n ");
for(int k=0; k<16; k++) {
unsigned v;
v = (dev->d_CSD[k] >> 4)&0x0f;
if (v < 10)
txchr('0'+v);
else
txchr('A'+v-10);
v = dev->d_CSD[k] & 0x0f;
if (v < 10)
txchr('0'+v);
else
txchr('A'+v-10);
if (k<15)
txstr(":");
} txstr("\n");
}
if (SDINFO) {
txstr("\n");
txstr(" CSD_STRUCTURE : "); txhex(CSD_STRUCTURE); txstr("\n");
} if (0 == CSD_STRUCTURE) {
// {{{
unsigned ERASE_BLK_EN, C_SIZE_MULT, BLOCK_LEN, MULT,
BLOCKNR, FILE_FORMAT_GRP, FILE_FORMAT,
WRITE_BL_LEN, SECTOR_SIZE;
READ_BL_LEN = dev->d_CSD[5] & 0x0f;
BLOCK_LEN = (READ_BL_LEN < 12) ? (1<<READ_BL_LEN) : READ_BL_LEN;
C_SIZE = ((dev->d_CSD[6]&0x3)<<10)
|((dev->d_CSD[7]&0x0ff)<<2)
|((dev->d_CSD[8]>>6)&0x03);
C_SIZE_MULT = ((dev->d_CSD[9]&3)<<2)|((dev->d_CSD[10]>>7)&1);
MULT = 1<<(C_SIZE_MULT+2);
BLOCKNR = MULT * (C_SIZE+1);
SECTOR_SIZE = ((dev->d_CSD[10]&0x03f)<<2)|((dev->d_CSD[11]>>7)&1);
WRITE_BL_LEN = ((dev->d_CSD[12]&3)<<2)|((dev->d_CSD[13]>>6)&3);
FILE_FORMAT_GRP = (dev->d_CSD[14]&0x80)?1:0;
FILE_FORMAT = dev->d_CSD[14]&3;
ERASE_BLK_EN = (dev->d_CSD[10] & 0x40)?1:0;
dev->d_sector_count = BLOCKNR / 512;
if (ERASE_BLK_EN)
dev->d_block_size = 512;
else
dev->d_block_size = SECTOR_SIZE;
if (SDINFO) {
txstr(" TAAC : "); txhex(dev->d_CSD[1]); txstr("\n");
txstr(" NSAC : "); txhex(dev->d_CSD[2]); txstr("\n");
txstr(" TRAN_SPEED : "); txhex(dev->d_CSD[3]); txstr("\n");
txstr(" CCC : "); txhex((dev->d_CSD[4]<<4)|((dev->d_CSD[10]&0x0f0)>>4)); txstr("\n");
txstr(" READ_BL_LEN : "); txhex(READ_BL_LEN); txstr("\n");
txstr(" READ_BL_PARTIAL : "); txhex((dev->d_CSD[6] & 0x80) ? 1:0); txstr("\n");
txstr(" WRITE_BLK_MISALIGN: "); txhex((dev->d_CSD[6] & 0x40) ? 1:0); txstr("\n");
txstr(" READ_BLK_MISALIGN : "); txhex((dev->d_CSD[6] & 0x20) ? 1:0); txstr("\n");
txstr(" DSR_IMP : "); txhex((dev->d_CSD[6] & 0x10) ? 1:0); txstr("\n");
txstr(" C_SIZE : "); txhex(C_SIZE); txstr("\n");
txstr(" VDD_R_CURR_MIN : "); txhex((dev->d_CSD[8]>>3)&0x07); txstr("\n");
txstr(" VDD_R_CURR_MAX : "); txhex(dev->d_CSD[8]&0x07); txstr("\n");
txstr(" VDD_W_CURR_MIN : "); txhex((dev->d_CSD[9]>>5)&0x07); txstr("\n");
txstr(" VDD_W_CURR_MAX : "); txhex((dev->d_CSD[9]>>2)&0x07); txstr("\n");
txstr(" C_SIZE_MULT : "); txhex(C_SIZE_MULT); txstr("\n");
txstr(" ERASE_BLK_EN : "); txhex((dev->d_CSD[10] & 0x40)?1:0); txstr("\n");
txstr(" SECTOR_SIZE : "); txhex(SECTOR_SIZE);
txstr(" (");
txdecimal((SECTOR_SIZE+1)*(1<<WRITE_BL_LEN));
txstr(" bytes)\n");
txstr(" WP_GRP_SIZE : "); txhex(dev->d_CSD[11]&0x07f); txstr("\n");
txstr(" WP_GRP_ENABLE : "); txhex((dev->d_CSD[12]&0x080)?1:0); txstr("\n");
txstr(" R2W_FACTOR : "); txhex((dev->d_CSD[12]>>2)&7); txstr("\n");
txstr(" WRITE_BL_LEN : "); txhex(WRITE_BL_LEN);
if (WRITE_BL_LEN < 9 || WRITE_BL_LEN > 11)
txstr(" (Reserved)\n");
else {
txstr(" (");
txdecimal(1<<WRITE_BL_LEN); txstr(" bytes)\n");
}
txstr(" WRITE_BL_PARTIAL : "); txhex((dev->d_CSD[13]&0x20)?1:0); txstr("\n");
txstr(" FILE_FORMAT_GRP : "); txhex(FILE_FORMAT_GRP); txstr("\n");
txstr(" COPY : "); txhex((dev->d_CSD[14]&0x40)?1:0); txstr("\n");
txstr(" PERM_WRITE_PROTECT: "); txhex((dev->d_CSD[14]&0x20)?1:0); txstr("\n");
txstr(" TMP_WRITE_PROTECT : "); txhex((dev->d_CSD[14]&0x10)?1:0); txstr("\n");
txstr(" FILE_FORMAT : "); txhex(FILE_FORMAT);
if (0 == FILE_FORMAT_GRP) {
if (0 == FILE_FORMAT)
txstr(" (Has parition tbl)\n");
else if (1 == FILE_FORMAT)
txstr(" (DOS FAT w/ boot sector, no partition)\n");
else if (2 == FILE_FORMAT)
txstr(" (Universal file format)\n");
else
txstr(" (Others/unknown)\n");
} else
txstr(" (Reserved)\n");
txstr(" Size = "); txdecimal(BLOCKNR); txstr(" blocks of ");
txdecimal(BLOCK_LEN); txstr(" bytes each\n");
}
// }}}
} else if (1 == CSD_STRUCTURE) {
// {{{
unsigned SECTOR_SIZE;
READ_BL_LEN = 9;
C_SIZE = ((dev->d_CSD[7] & 0x03f)<<16)
|((dev->d_CSD[8] & 0x0ff)<<8)
|(dev->d_CSD[9] & 0x0ff);
dev->d_sector_count = (C_SIZE+1) * 1024;
dev->d_block_size = 512;
SECTOR_SIZE = ((dev->d_CSD[10] & 0x03f) << 1) | ((dev->d_CSD[11]>>7)&1);
if (SDINFO) {
unsigned TRAN_SPEED;
TRAN_SPEED = dev->d_CSD[3] & 0x0ff;
txstr(" TAAC : "); txhex(dev->d_CSD[1] & 0x0ff); txstr("\n");
txstr(" NSAC : "); txhex(dev->d_CSD[2] & 0x0ff); txstr("\n");
txstr(" TRAN_SPEED : ");
if (TRAN_SPEED = 0x32)
txstr("400kHz (Startup)\n");
else if (TRAN_SPEED == 0x0b)
txstr("100Mb/s, SDR50 or DDR50\n");
else if (TRAN_SPEED == 0x2b)
txstr("200Mb/s, SDR104\n");
else {
txhex(TRAN_SPEED); txstr(" (Unexpected)\n");
}
txstr(" CCC : "); txhex((dev->d_CSD[4]<<4)|((dev->d_CSD[5]&0x0f0)>>4)); txstr("\n");
txstr(" READ_BL_LEN : "); txdecimal(1 << (dev->d_CSD[5] & 0x0f)); txstr("\n");
txstr(" READ_BL_PARTIAL : "); txstr((dev->d_CSD[6]&0x80) ? "Yes\n":"No\n");
txstr(" WRITE_BLK_MISALIGN: "); txstr((dev->d_CSD[6]&0x40) ? "Yes\n":"No\n");
txstr(" READ_BLK_MISALIGN : "); txstr((dev->d_CSD[6]&0x20) ? "Yes\n":"No\n");
txstr(" DSR_IMP : "); txstr((dev->d_CSD[6] & 0x10) ? "Yes, implemented\n" : "NOT implemented\n");
txstr(" C_SIZE : "); txhex(C_SIZE); txstr("\n");
txstr(" SECTOR_SIZE : ");
if (SECTOR_SIZE == 127) txstr("127 (Good)\n");
else { txhex(SECTOR_SIZE); txstr(" -- ERR\n"); }
txstr(" WP_GRP_SIZE : "); txhex(dev->d_CSD[11]&0x7f); txstr("\n");
txstr(" WP_GRP_ENABLE : "); txstr((dev->d_CSD[12]&0x80) ? "Error!\n":"(As expected)\n");
txstr(" R2W_FACTOR : "); txhex((dev->d_CSD[12]>>2)&7); txstr("\n");
txstr(" WR_BL_LEN : "); txhex(((dev->d_CSD[12]&3)<<2)|((dev->d_CSD[13]>>6)&3)); txstr("\n");
txstr(" WR_BL_PARTIAL : "); txstr((dev->d_CSD[13]&0x20) ? "Error!\n":"(As expected)\n");
txstr(" FILE_FORMAT_GRP : "); txstr((dev->d_CSD[14]&0x80) ? "Error!\n":"(As expected)\n");
txstr(" COPY : "); txhex((dev->d_CSD[14]&0x40)?1:0); txstr("\n");
txstr(" PERM_WRITE_PROTECT: "); txhex((dev->d_CSD[14]&0x20)?1:0); txstr("\n");
txstr(" TMP_WRITE_PROTECT : "); txhex((dev->d_CSD[14]&0x10)?1:0); txstr("\n");
txstr(" Size = "); txdecimal((C_SIZE+1) * 512); txstr(" kB\n");
}
// }}}
} else {
txstr("ERROR: Unknown CSD type: "); txhex(CSD_STRUCTURE); txstr("\n");
dev->d_sector_count = 0;
dev->d_block_size = 0;
}
}
// }}}
unsigned sdio_switch(SDIODRV *dev, unsigned swcmd, unsigned *ubuf) { // CMD 6
// {{{
unsigned c, phy, fail = 0;
if (SDDEBUG) txstr("CMD-SWITCH_FUNC\n");
phy = dev->d_dev->sd_phy;
phy &= ~SECTOR_MASK;
phy |= (6 << 24); // 512bit response => 64 bytes
dev->d_dev->sd_phy = phy;
// bit 31: 0 => check function, 1 => switch function
// [30:24] Must == 0
// [15:12] funtion group 4 (Power Limit)
// 4'h0 => Default value
// 4'hf => No change, keep the same value
// [11: 8] funtion group 3 (Drive Strength)
// [ 7: 4] funtion group 2 (Command system)
// [ 3: 0] funtion group 1 (Access mode)
//
// Set to 0x80fffff1 to switch to HS/SDR25 mode (50MHz clock)
// Requiring 1.8V ...
// Set to 0x80fffff2 to switch to SDR50 mode (104MHz clock)
// Set to 0x80fffff3 to switch to SDR104 mode (208MHz clock)
// Set to 0x80fffff4 to switch to DDR50 mode (50MHz clock)
dev->d_dev->sd_data = swcmd;
dev->d_dev->sd_cmd = (SDIO_CMD | SDIO_R1 | SDIO_ERR | SDIO_MEM)+6;
sdio_wait_while_busy(dev);
c = dev->d_dev->sd_cmd;
if ((c & SDIO_RXERR) && (c & SDIO_RXECODE))
fail = 1;
if (SDDEBUG && SDINFO) {
// {{{
unsigned r;
r = dev->d_dev->sd_data;
txstr("CMD6: SWITCH_FUNC\n");
txstr(" Cmd: "); txhex(c);