summaryrefslogtreecommitdiff
path: root/nuttx/drivers/mmcsd/mmcsd_spi.c
blob: b081401cbc4e5f1f4420d5eb8fa653fbcb8a05a1 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
/****************************************************************************
 * drivers/mmcsd/mmcsd_spi.c
 *
 *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/arch.h>
#include <nuttx/spi.h>
#include <nuttx/fs.h>
#include <nuttx/mmcsd.h>

#include "mmcsd_spi.h"
#include "mmcsd_csd.h"
#include "mmcsd_internal.h"

/****************************************************************************
 * Pre-Processor Definitions
 ****************************************************************************/

/* Configuration ************************************************************/

#ifndef CONFIG_MMCSD_NSLOTS
#  ifdef CONFIG_CPP_HAVE_WARNING
#    warning "CONFIG_MMCSD_NSLOTS not defined"
#  endif
#  define CONFIG_MMCSD_NSLOTS 1
#endif

#define MMCSD_IDMODE_CLOCK           (400000)

#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
#  define MMCSD_MODE 0666
#else
#  define MMCSD_MODE 0444
#endif

/* Slot struct info *********************************************************/
/* Slot status definitions */

#define MMCSD_SLOTSTATUS_NOTREADY    0x01 /* Card not initialized */
#define MMCSD_SLOTSTATUS_NODISK      0x02 /* No card in the slot */
#define MMCSD_SLOTSTATUS_WRPROTECT   0x04 /* Card is write protected */
#define MMCSD_SLOTSTATUS_MEDIACHGD   0x08 /* Media changed in slot */

/* Values in the MMC/SD command table ***************************************/
/* These define the expected arguments of the MMC/SD command */

#define MMCSD_CMDARG_NONE            0
#define MMCSD_CMDARG_BLKLEN          1
#define MMCSD_CMDARG_ADDRESS         2
#define MMCSD_CMDARG_DUMMY           3

/* These define the value returned by the MMC/SD command */

#define MMCSD_CMDRESP_R1             0
#define MMCSD_CMDRESP_R1B            1
#define MMCSD_CMDRESP_R2             2
#define MMCSD_CMDRESP_R3             3


/* Fudge factor for SD read timeout: ~100msec, Write Time out ~250ms.  Units
 * of Hz.
 */

#define SD_READACCESSHZ              7
#define SD_WRITEACCESSHZ             3

/****************************************************************************
 * Private Types
 ****************************************************************************/

/* This structure represents the state of one card slot */

struct mmcsd_slot_s
{
  FAR struct spi_dev_s *spi; /* SPI port bound to this slot */
  sem_t  sem;            /* Assures mutually exclusive accesss to card and SPI */
  ubyte  state;          /* State of the slot (see MMCSD_SLOTSTATUS_* definitions) */
  ubyte  type;           /* Disk type */
  ubyte  csd[16];        /* Copy of card CSD */
  uint16 sectorsize;     /* Media block size (in bytes) */
  uint32 nsectors;       /* Number of blocks on the media */
  uint32 taccess;        /* Card access time */
  uint32 twrite;         /* Card write time */
};

struct mmcsd_cmdinfo_s
{
  ubyte  cmd;
  ubyte  arg;
  ubyte  resp;
};

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/* Misc *********************************************************************/

static void   mmcsd_semtake(sem_t *sem);

/* Card SPI interface *******************************************************/

static int    mmcsd_waitready(FAR struct mmcsd_slot_s *slot);
static uint32 mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
                const struct mmcsd_cmdinfo_s *cmd, uint32 arg);
static void   mmcsd_setblklen(FAR struct mmcsd_slot_s *slot, uint32 length);
static void   mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, ubyte *csd);
static void   mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot, ubyte *csd);
static int    mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot, ubyte *buffer,
                const struct mmcsd_cmdinfo_s *cmd);

#define mmcsd_getcsd(slot, csd) mmcsd_getcardinfo(slot, csd, &g_cmd9);
#define mmcsd_getcid(slot, cid) mmcsd_getcardinfo(slot, cid, &g_cmd10);

/* Block driver interfaces **************************************************/

static int     mmcsd_open(FAR struct inode *inode);
static int     mmcsd_close(FAR struct inode *inode);
static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
                 size_t start_sector, unsigned int nsectors);
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
static ssize_t mmcsd_write(FAR struct inode *inode, const unsigned char *buffer,
                 size_t start_sector, unsigned int nsectors);
#endif
static int     mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry);

/* Initialization ***********************************************************/

static int    mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot);
static void   mmcsd_mediachanged(void *arg);

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* Driver state *************************************************************/

/* These are the lock driver methods supported by this file */

static const struct block_operations g_bops =
{
  mmcsd_open,     /* open     */
  mmcsd_close,    /* close    */
  mmcsd_read,     /* read     */
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
  mmcsd_write,    /* write    */
#else
  NULL,        /* write    */
#endif
  mmcsd_geometry, /* geometry */
  NULL         /* ioctl    */
};

/* A slot structure allocated for each configured slot */

static struct mmcsd_slot_s g_mmcsdslot[CONFIG_MMCSD_NSLOTS];

/* Timing *******************************************************************/

/* We will use the TRAN_SPEED from the CSD to determine the maximum SPI
 * clocking (TRAN_SPEED defines the maximum transfer rate per bit per data
 * line).
 *
 * The CSD TRAN_SPEED is provided as a 3 bit rate unit (RU) and a 4 bit time
 * value (TU). We need the transfer frequency which is:  RU*TU bits/sec
 *
 * g_transpeedru holds RU/10 and g_transpeedtu holds TU*10 so that the
 * correct value is returned in the product
 */

static const uint32 g_transpeedru[8] =
{
     10000,   /*  0:  10 Kbit/sec / 10 */
    100000,   /*  1:  1 Mbit/sec / 10 */
   1000000,   /*  2:  10 Mbit/sec / 10 */
  10000000,   /*  3:  100 Mbit/sec / 10*/

  0, 0, 0, 0  /* 4-7: Reserved values */
};

static const uint32 g_transpeedtu[16] =
{
   0, 10, 12, 13, /*  0-3:  Reserved, 1.0, 1.1, 1.2, 1.3 */
  15, 20, 25, 30, /*  4-7:  1.5, 2.0, 2.5, 3.0 */
  35, 40, 45, 50, /*  8-11: 3.5, 4.0, 4.5, 5.0 */
  55, 60, 70, 80, /* 12-15: 5.5, 6.0, 7.0, 8.0 */
};

/* The TAAC defines the asynchronous part of the data access time.  The
 * read access time the sum of the TAAC and the NSAC.  These define the
 * time from the end bit of the read command to start bit of the data block.
 *
 * The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value (TV).
 * The access we need time is then given by:
 *
 *   taccess = spifrequency / (TU*TV) + NAC
 *
 * g_taactu holds the (1 / TU / 100 ) and g_taactv holds (100 / TV) so
 * that taccess can be computed without division.
 */

static const uint32 g_taactu[8] =
{
  10000000, /* 0:   1 ns -> 1,000,000,000 Hz / 100 = 10,000,000 */
   1000000, /* 1:  10 ns ->   100,000,000 Hz / 100 =  1,000,000 */
    100000, /* 2: 100 ns ->    10,000,000 Hz / 100 =    100,000 */
     10000, /* 3:   1 us ->     1,000,000 Hz / 100 =     10,000 */
      1000, /* 4:  10 us ->       100,000 Hz / 100 =      1,000 */
       100, /* 5: 100 us ->        10,000 Hz / 100 =        100 */
        10, /* 6:   1 ms ->         1,000 Hz / 100 =         10 */
         1, /* 7:  10 ms ->           100 Hz / 100 =          1 */
};

static const uint32 g_taactv[] =
{
   0, 100,  83,  77, /*  0-3:  Reserved, 100/1.0, 100/1.2, 100/1.3 */
  67,  50,  40,  33, /*  4-7:   100/1.5, 100/2.0, 100/2.5, 100/3.0 */
  29,  25,  22,  20, /*  8-11:  100/3.5, 100/4.0, 100/4.5, 100/5.0 */
  18,  17,  14,  13  /* 12-15:  100/5.5, 100/6.0, 100/7.0, 100/8.0 */
};

/* Commands *****************************************************************/

static const struct mmcsd_cmdinfo_s g_cmd0   = {0x40, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd1   = {0x41, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd9   = {0x49, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd10  = {0x4a, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd12  = {0x4c, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd13  = {0x4d, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R2};
static const struct mmcsd_cmdinfo_s g_cmd16  = {0x50, MMCSD_CMDARG_BLKLEN,  MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd17  = {0x51, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd18  = {0x52, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd24  = {0x58, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd25  = {0x59, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd27  = {0x5b, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd28  = {0x5c, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1B};
static const struct mmcsd_cmdinfo_s g_cmd29  = {0x5d, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1B};
static const struct mmcsd_cmdinfo_s g_cmd30  = {0x5e, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd32  = {0x60, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd33  = {0x61, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd34  = {0x62, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd35  = {0x63, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd36  = {0x64, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd37  = {0x65, MMCSD_CMDARG_ADDRESS, MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd38  = {0x66, MMCSD_CMDARG_DUMMY,   MMCSD_CMDRESP_R1B};
static const struct mmcsd_cmdinfo_s g_cmd42  = {0x6a, MMCSD_CMDARG_DUMMY,   MMCSD_CMDRESP_R1B};
static const struct mmcsd_cmdinfo_s g_cmd55  = {0x77, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd56  = {0x78, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_cmd58  = {0x7a, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R3};
static const struct mmcsd_cmdinfo_s g_cmd59  = {0x7b, MMCSD_CMDARG_DUMMY,   MMCSD_CMDRESP_R1};
static const struct mmcsd_cmdinfo_s g_acmd41 = {0x69, MMCSD_CMDARG_NONE,    MMCSD_CMDRESP_R1};

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mmcsd_semtake
 ****************************************************************************/

static void mmcsd_semtake(sem_t *sem)
{
  while (sem_wait(sem) != 0)
    {
      /* The only case that an error should occur here is if the wait was
       * awakened by a signal.
       */

      ASSERT(errno == EINTR);
    }
}

#define mmcsd_semgive(sem) sem_post(sem)

/****************************************************************************
 * Name: mmcsd_waitready
 *
 * Description:
 *   Wait until the the card is no longer busy
 *
 ****************************************************************************/

static int mmcsd_waitready(FAR struct mmcsd_slot_s *slot)
{
  FAR struct spi_dev_s *spi = slot->spi;
  ubyte response;
  int i;

  /* Wait until the card is no longer busy */

  for (i = 0; i < slot->twrite; i++)
    {
      response = SPI_SNDBYTE(spi, 0xff);
      if (response == 0xff)
        {
          return OK;
        }
    }

  fdbg("Card still busy, last response: %02x\n", response);
  return -EBUSY;
}

/****************************************************************************
 * Name: mmcsd_sendcmd
 *
 * Description:
 *   Send a command to MMC
 *
 ****************************************************************************/

static uint32 mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
                            const struct mmcsd_cmdinfo_s *cmd, uint32 arg)
{
  FAR struct spi_dev_s *spi = slot->spi;
  uint32 result;
  ubyte response = 0xff;
  int i;

  /* Select SPI */

  SPI_SELECT(spi, SPIDEV_MMCSD, TRUE);

  /* Send command code */

  SPI_SNDBYTE(spi, cmd->cmd);

  /* Send command's arguments */

  if (cmd->arg == MMCSD_CMDARG_NONE)
    {
      SPI_SNDBYTE(spi, 0x00);
      SPI_SNDBYTE(spi, 0x00);
      SPI_SNDBYTE(spi, 0x00);
      SPI_SNDBYTE(spi, 0x00);
    }
  else
    {
      SPI_SNDBYTE(spi, arg >> 24);
      SPI_SNDBYTE(spi, arg >> 16);
      SPI_SNDBYTE(spi, arg >> 8);
      SPI_SNDBYTE(spi, arg);
    }

  /* Send CRC if needed.  The SPI interface is initialized in non-protected
   * mode.  However, the reset command (CMD0) is received by the card while it
   * is still in SD mode and, therefore, must have a valid CRC field.
   */

  if (cmd->cmd == 0x40)
    {
      SPI_SNDBYTE(spi, 0x95);
    }
  else
    {
      SPI_SNDBYTE(spi, 0xff);
    }

  /* Get the response to the command */

  for (i = 0; i < 9 && response == 0xff; i++)
    {
      response = SPI_SNDBYTE(spi, 0xff);
    }

  if (i == 0)
    {
      fdbg("Failed: i=%d response=%02x\n", i, response);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
      return (uint32)-1;
    }

  /* Interpret the response according to the command */

  switch (cmd->resp)
    {
    case MMCSD_CMDRESP_R1B:
      {
        uint32 busy = 0;
        for (i = 0; i < slot->twrite && busy != 0xff; i++)
          {
            busy = SPI_SNDBYTE(spi, 0xff);
          }
        fvdbg("Return R1B=%02x\n", response);
      }
      return (uint32)response;

    case MMCSD_CMDRESP_R1:
      {
        fvdbg("Return R1=%02x\n", response);
      }
      return (uint32)response;

    case MMCSD_CMDRESP_R2:
      {
        result  = ((uint32) response << 8) & 0x0000ff00;
        result |= SPI_SNDBYTE(spi, 0xff) & 0xff;
        fvdbg("Return R2=%04x\n", result);
      }
      return result;

    case MMCSD_CMDRESP_R3:
    default:
      {
        result  = ((uint32) response << 24) & 0xff000000;
        result |= ((uint32) SPI_SNDBYTE(spi, 0xff) << 16) & 0x00ff0000;
        result |= ((uint32) SPI_SNDBYTE(spi, 0xff) << 8) & 0x0000ff00;
        result |= SPI_SNDBYTE(spi, 0xff) & 0xff;
        fvdbg("Return R3=%08x\n", result);
      }
      return result;
    }
}

/****************************************************************************
 * Name: mmcsd_setblklen
 *
 * Description:
 *   Set block length
 *
 ****************************************************************************/

static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot, uint32 length)
{
  FAR struct spi_dev_s *spi = slot->spi;
  uint32 result;

  result = mmcsd_sendcmd(slot, &g_cmd16, length);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
}

/****************************************************************************
 * Name: mmcsd_decodecsd
 *
 * Description: 
 *
 ****************************************************************************/

static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, ubyte *csd)
{
  FAR struct spi_dev_s *spi = slot->spi;
  uint32 frequency;

  /* Calculate SPI max clock */

  frequency =
    g_transpeedru[MMCSD_CSD_TRANSPEED_TIMEVALUE(csd)] *
    g_transpeedtu[MMCSD_CSD_TRANSPEED_TRANSFERRATEUNIT(csd)];

  if (frequency > 20000000)
    {
      frequency = 20000000;
    }

  /* Set the SPI frequency to that value */

  frequency = SPI_SETFREQUENCY(spi, frequency);

  /* Now determine the delay to */

  if (slot->type == MMCSD_CARDTYPE_MMC)
    {
      slot->taccess =
        g_taactu[MMCSD_CSD_TAAC_TIMEUNIT(csd)] *
        g_taactv[MMCSD_CSD_TAAC_TIMEVALUE(csd)];

      slot->taccess  = frequency / slot->taccess;
      slot->taccess += 1 << (MMCSD_CSD_NSAC(csd) + 4);
      slot->taccess *= 10;
      slot->twrite   = slot->taccess * MMCSD_CSD_R2WFACTOR(csd);
    }
  else
    {
      slot->taccess  = frequency / SD_READACCESSHZ;
      slot->twrite   = frequency / SD_WRITEACCESSHZ;
    }

  fvdbg("Frequency:         %d\n", frequency);
  fvdbg("Read access time:  %d\n", slot->taccess);
  fvdbg("Write access time: %d\n", slot->twrite);

  /* Get the physical geometry of the card: sector size and number of
   * sectors. The card's total capacity is computed from
   *
   *   capacity = BLOCKNR * BLOCK_LEN
   *   BLOCKNR = (C_SIZE+1)*MULT
   *   MULT = 2**(C_SIZE_MULT+2)         (C_SIZE_MULT < 8)
   *   BLOCK_LEN = 2**READD_BL_LEN       (READ_BL_LEN < 12)
   *
   * Or
   *
   *   capacity = ((C_SIZE+1) << (READD_BL_LEN + C_SIZE_MULT + 2))
   *
   * In units of the sector size (1 << READ_BL_LEN), then simplifies to
   *
   *   nsectors = ((C_SIZE+1) << (C_SIZE_MULT + 2))
   */

  if (MMCSD_CSD_CSDSTRUCT(csd) == 1)
    {
      /* SDC ver 2.00 */
      /* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */

      slot->sectorsize = 1 << SD20_CSD_READBLLEN(csd);
      slot->nsectors   = (SD20_CSD_CSIZE(csd) + 1) << (SD20_CSD_CSIZEMULT(csd) + 2);
    }
  else
    {
      /* MMC or SD ver 1.xx */
      /* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */

      slot->sectorsize = 1 << MMCSD_CSD_READBLLEN(csd);
      slot->nsectors = (MMCSD_CSD_CSIZE(csd) + 1) << (MMCSD_CSD_CSIZEMULT(csd) + 2);
    }

  fvdbg("Sector size:       %d\n", slot->sectorsize);
  fvdbg("Number of sectors: %d\n", slot->nsectors);
}

/****************************************************************************
 * Name: mmcsd_checkwrprotect
 *
 * Description: 
 *
 ****************************************************************************/

static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot, ubyte *csd)
{
  FAR struct spi_dev_s *spi = slot->spi;

  /* Check if (1) the slot is reporting that reporting that write protection
   * is set, (2) the card reports permanent write protect, or (2) the card
   * reports temporary write protect.
   */

  if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_WRPROTECTED) != 0 ||
      MMCSD_CSD_PERMWRITEPROTECT(csd) ||
      MMCSD_CSD_TMPWRITEPROTECT(csd))
    {
      slot->state |= MMCSD_SLOTSTATUS_WRPROTECT;
    }
  else
    {
      slot->state &= ~MMCSD_SLOTSTATUS_WRPROTECT;
    }
}

/****************************************************************************
 * Name: mmcsd_getcardinfo
 *
 * Description:
 *   Read CSD or CID  registers
 *
 ****************************************************************************/

static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot, ubyte *buffer,
                             const struct mmcsd_cmdinfo_s *cmd)
{
  FAR struct spi_dev_s *spi = slot->spi;
  uint32 result;
  ubyte response;
  int i;

  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  SPI_SNDBYTE(spi, 0xff);

  /* Send the CMD9 or CMD10 */

  result = mmcsd_sendcmd(slot, cmd, 0);
  if (result != MMCSD_SPIR1_OK)
    {
      fdbg("CMD9/10 failed: R1=%02x\n", result);
      goto errout_with_eio;
    }

  /* Try up to 8 times to find the start of block (or until an error occurs) */

  for (i = 0; i < 8; i++)
    {
      response = SPI_SNDBYTE(spi, 0xff);
      fvdbg("%d. SPI sndbyte returned %02x\n", i, response);

      /* If a read operation fails and the card cannot provide the requested
       * data, it will send a data error token instead.  The 4 least
       * significant bits are the same as those in the R2 response.
       */

      if (response != 0 && (response & MMCSD_SPIDET_UPPER) == 0)
        {
          fdbg("%d. Data transfer error: %02x\n", i, response);
          goto errout_with_eio;
        }
      else if (response == MMCSD_SPIDT_STARTBLKSNGL)
        {
          for (i = 0; i < 16; ++i)
            {
              *buffer++ = SPI_SNDBYTE(spi, 0xff);
            }

          /* CRC receive */

          (void)SPI_SNDBYTE(spi, 0xff);
          (void)SPI_SNDBYTE(spi, 0xff);
          SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
          return OK;
        }
    }

errout_with_eio:
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  return -EIO;
}

/****************************************************************************
 * Block Driver Operations
 ****************************************************************************/

/****************************************************************************
 * Name: mmcsd_open
 *
 * Description: Open the block device
 *
 ****************************************************************************/

static int mmcsd_open(FAR struct inode *inode)
{
  FAR struct mmcsd_slot_s *slot;
  FAR struct spi_dev_s *spi;
  int ret;

  fvdbg("Entry\n");

#ifdef CONFIG_DEBUG
  if (!inode || !inode->i_private)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Extract our private data from the inode structure */

  slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  spi  = slot->spi;

#ifdef CONFIG_DEBUG
  if (!spi)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Select the slave */

  mmcsd_semtake(&slot->sem);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

  /* Verify that the MMC/SD card is alive and ready for business */

  ret = mmcsd_waitready(slot);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  mmcsd_semgive(&slot->sem);
  return ret;
}

/****************************************************************************
 * Name: mmcsd_close
 *
 * Description: close the block device
 *
 ****************************************************************************/

static int mmcsd_close(FAR struct inode *inode)
{
  fvdbg("Entry\n");
  return OK;
}

/****************************************************************************
 * Name: mmcsd_read
 *
 * Description:  Read the specified numer of sectors
 *
 ****************************************************************************/

static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
                          size_t start_sector, unsigned int nsectors)
{
  FAR struct mmcsd_slot_s *slot;
  FAR struct spi_dev_s *spi;
  size_t nbytes;
  off_t  offset;
  ubyte response;
  int i;

  fvdbg("start_sector=%d nsectors=%d\n", start_sector, nsectors);

#ifdef CONFIG_DEBUG
  if (!buffer)
    {
      fdbg("Invalid parameters\n");
      return -EINVAL;
    }

  if (!inode || !inode->i_private)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Extract our private data from the inode structure */

  slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  spi  = slot->spi;

#ifdef CONFIG_DEBUG
  if (!spi)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Verify that card is availabled */

  if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
    {
      fdbg("Slot not ready\n");
      return -ENODEV;
    }

  /* Do nothing on zero-length transfer */

  if (nsectors < 1)
    {
      return 0;
    }

  /* Convert sector and nsectors to nbytes and byte offset */

  nbytes = nsectors * slot->sectorsize;
  offset = start_sector * slot->sectorsize;
  fvdbg("nbytes=%d offset=%d\n", nbytes, offset);

  /* Select the slave and synchronize */

  mmcsd_semtake(&slot->sem);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  (void)SPI_SNDBYTE(spi, 0xff);

  /* Send CMD17: Reads a block of the size selected by the SET_BLOCKLEN
   * command and verify that good R1 status is returned
   */

  response = mmcsd_sendcmd(slot, &g_cmd17, offset);
  if (response != MMCSD_SPIR1_OK)
    {
      fdbg("CMD17 failed: R1=%02x\n", response);
      goto errout_with_eio;
    }

  /* Loop only for the computed timeout */

  for (i = 0; i < slot->taccess; i++)
    {
      /* Synchronize */

      response = SPI_SNDBYTE(spi, 0xff);
      fvdbg("(%d) SPI sndbyte returned %02x\n", i, response);

      /* If a read operation fails and the card cannot provide the requested
       * data, it will send a data error token instead.  The 4 least
       * significant bits are the same as those in the R2 response.
       */

      if (response != 0 && (response & MMCSD_SPIDET_UPPER) == 0)
        {
          fdbg("(%d) Data transfer error: %02x\n", i, response);
          goto errout_with_eio;
        }
      else if (response == MMCSD_SPIDT_STARTBLKSNGL)
        {
          /* Receive the block of data */

          SPI_RECVBLOCK(spi, buffer, nbytes);

          /* Receive and ignore the two CRC bytes */

          (void)SPI_SNDBYTE(spi, 0xff);
          (void)SPI_SNDBYTE(spi, 0xff);

          /* On success, return the number of sectors transfer */

          SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
          mmcsd_semgive(&slot->sem);

          fvdbg("(%d) Read %d bytes:\n", i, nbytes);
          mmcsd_dumpbuffer(buffer, nbytes);
          return nsectors;
        }
    }

errout_with_eio:
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  mmcsd_semgive(&slot->sem);
  return -EIO;
}

/****************************************************************************
 * Name: mmcsd_write
 *
 * Description:
 *   Write the specified number of sectors
 *
 ****************************************************************************/

#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
static ssize_t mmcsd_write(FAR struct inode *inode, const unsigned char *buffer,
                        size_t start_sector, unsigned int nsectors)
{
  FAR struct mmcsd_slot_s *slot;
  FAR struct spi_dev_s *spi;
  size_t nbytes;
  off_t  offset;
  ubyte response;
  int ret;

  fvdbg("start_sector=%d nsectors=%d\n", start_sector, nsectors);

#ifdef CONFIG_DEBUG
  if (!buffer)
    {
      fdbg("Invalid parameters\n");
      return -EINVAL;
    }

  if (!inode || !inode->i_private)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Extract our private data from the inode structure */

  slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  spi  = slot->spi;

#ifdef CONFIG_DEBUG
  if (!spi)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Verify that card is availabled */

  if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
    {
      fdbg("Slot not ready\n");
      return -ENODEV;
    }

  /* Verify that the card is write enabled */

  if (slot->state & MMCSD_SLOTSTATUS_WRPROTECT)
    {
      fdbg("Not write enabled\n");
      return -EACCES;
    }

  /* Do nothing on zero-length transfer */

  if (nsectors < 1)
    {
      return 0;
    }

  /* Convert sector and nsectors to nbytes and byte offset */

  nbytes = nsectors * slot->sectorsize;
  offset = start_sector * slot->sectorsize;
  fvdbg("Writing %d bytes to offset %d:\n", nbytes, offset);
  mmcsd_dumpbuffer(buffer, nbytes);

  /* Select the slave and synchronize */

  mmcsd_semtake(&slot->sem);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  (void)SPI_SNDBYTE(spi, 0xff);

  /* Send CMD24 (WRITE_BLOCK) and verify that good R1 status is returned */

  response = mmcsd_sendcmd(slot, &g_cmd24, offset);
  if (response != MMCSD_SPIR1_OK)
    {
      fdbg("CMD24 failed: R1=%02x\n", response);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
      ret = -EIO;
      goto errout_with_sem;
    }

  /* Start the block transfer:
   * 1. 0xff (sync)
   * 2. 0xfe (start of block)
   * 3. Followed by the block of data
   */

  (void)SPI_SNDBYTE(spi, 0xff);
  (void)SPI_SNDBYTE(spi, MMCSD_SPIDT_STARTBLKSNGL);
  (void)SPI_SNDBLOCK(spi, buffer, nbytes);

  /* Add the bogus CRC.  By default, the SPI interface is initialized in
   * non-protected mode.  However, we still have to send bogus CRC values
   */

  (void)SPI_SNDBYTE(spi, 0xff);
  (void)SPI_SNDBYTE(spi, 0xff);

  /* Now get the data response */

  response = SPI_SNDBYTE(spi, 0xff);
  if ((response & MMCSD_SPIDR_MASK) != MMCSD_SPIDR_ACCEPTED)
    {
      fdbg("Bad data response: %02x\n", response);
      ret = -EIO;
      goto errout_with_sem;
    }

  /* Wait until the card is no longer busy */

  ret = mmcsd_waitready(slot);
  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
  mmcsd_semgive(&slot->sem);

  /* Verify that the card successfully became non-busy */

  if (ret < 0)
    {
      return ret;
    }

  /* The success return value is the number of sectors written */

  return nsectors;

errout_with_sem:
  mmcsd_semgive(&slot->sem);
  return ret;
}
#endif

/****************************************************************************
 * Name: mmcsd_geometry
 *
 * Description:
 *   Return device geometry
 *
 ****************************************************************************/

static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry)
{
  FAR struct mmcsd_slot_s *slot;
  FAR struct spi_dev_s *spi;
  ubyte csd[16];
  int ret;

#ifdef CONFIG_DEBUG
  if (!geometry)
    {
      fdbg("Invalid parameters\n");
      return -EINVAL;
    }

  if (!inode || !inode->i_private)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Extract our private data from the inode structure */

  slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  spi  = slot->spi;

#ifdef CONFIG_DEBUG
  if (!spi)
    {
      fdbg("Internal confusion\n");
      return -EIO;
    }
#endif

  /* Re-sample the CSD */

  mmcsd_semtake(&slot->sem);
  ret = mmcsd_getcsd(slot, csd);
  if (ret < 0)
    {
      mmcsd_semgive(&slot->sem);
      fdbg("mmcsd_getcsd returned %d\n", ret);
      return ret;
    }

  /* Check for changes related to write protection */

  mmcsd_checkwrprotect(slot, csd);

  /* Then return the card geometry */

  geometry->geo_available =
    ((slot->state & (MMCSD_SLOTSTATUS_NOTREADY|MMCSD_SLOTSTATUS_NODISK)) == 0);
  geometry->geo_mediachanged =
    ((slot->state & MMCSD_SLOTSTATUS_MEDIACHGD) != 0);
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
  geometry->geo_writeenabled =
    ((slot->state & MMCSD_SLOTSTATUS_WRPROTECT) == 0);
#else
  geometry->geo_writeenabled = FALSE;
#endif
  geometry->geo_nsectors   = slot->nsectors;
  geometry->geo_sectorsize = slot->sectorsize;

  /* After reporting mediachanged, clear the indication so that it is not
   * reported again.
   */

  slot->state &= ~MMCSD_SLOTSTATUS_MEDIACHGD;
  mmcsd_semgive(&slot->sem);

  fvdbg("geo_available:     %d\n", geometry->geo_available);
  fvdbg("geo_mediachanged:  %d\n", geometry->geo_mediachanged);
  fvdbg("geo_writeenabled:  %d\n", geometry->geo_writeenabled);
  fvdbg("geo_nsectors:      %d\n", geometry->geo_nsectors);
  fvdbg("geo_sectorsize:    %d\n", geometry->geo_sectorsize);

  return OK;
}

/****************************************************************************
 * Initialization
 ****************************************************************************/

/****************************************************************************
 * Name: mmcsd_mediainitialize
 *
 * Description:
 *   Detect media and initialize
 *
 ****************************************************************************/

static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot)
{
  FAR struct spi_dev_s *spi = slot->spi;
  ubyte csd[16];
  uint32 result = MMCSD_SPIR1_IDLESTATE;
  int i, j;

  /* Assume that the card is not ready (we'll clear this on successful car
   * initialization.
   */

  slot->state |= MMCSD_SLOTSTATUS_NOTREADY;


  /* Check if there is a card present in the slot.  This is normally a matter is
   * of GPIO sensing and does not really involve SPI, but by putting this
   * functionality in the SPI interface, we encapuslate the SPI MMC/SD
   * interface
   */

  if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_PRESENT) == 0)
    {
      fdbg("No card present\n");
      slot->state |= MMCSD_SLOTSTATUS_NODISK;
      return -ENODEV;
    }

  /* Clock Freq. Identification Mode < 400kHz */

  SPI_SETFREQUENCY(spi, MMCSD_IDMODE_CLOCK);

  /* Set the maximum access time out */

  slot->taccess = MMCSD_IDMODE_CLOCK / SD_READACCESSHZ;

  /* The SD card wakes up in SD mode. It will enter SPI mode if the chip select signal is
   * asserted (negative) during the reception of the reset command (CMD0) and the card is in
   * IDLE state.
   */

  /* After power up at least 74 clock cycles are required prior to starting bus communication */

  fvdbg("Send CMD0\n");
  for (i = 0; i < 2; i++)
    {
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

      for (j = 10; j; j--)
        {
          SPI_SNDBYTE(spi, 0xff);
        }

      /* Send CMD0 (GO_TO_IDLE) to put MMC/SD in IDLE/SPI mode */

      result = mmcsd_sendcmd(slot, &g_cmd0, 0);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

      /* Return from CMD0 is R1 which should now show IDLE STATE */

      if (result == MMCSD_SPIR1_IDLESTATE)
        {
          fvdbg("Card is in IDLE state\n");
          break;
        }
    }

  /* Verify that we exit the above loop with the card reporting IDLE state */

  if (result != MMCSD_SPIR1_IDLESTATE)
    {
      fdbg("Send CMD0 failed: R1=%02x\n", result);
      return -EIO;
    }

  /* Determinate Card type SD or MMC */

  slot->type = MMCSD_CARDTYPE_MMC;

  for (i = 100; i; --i)
    {
      fvdbg("%d. Send CMD55\n", i);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
      SPI_SNDBYTE(spi, 0xff);
      result = mmcsd_sendcmd(slot, &g_cmd55, 0);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

      fvdbg("%d. Send ACMD41\n", i);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
      SPI_SNDBYTE(spi, 0xff);
      result = mmcsd_sendcmd(slot, &g_acmd41, 0);
      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

      /* If this is an MMC card, it will response with ILLEGAL COMMAND */

      if (result & MMCSD_SPIR1_ILLEGALCMD)
        {
          /* MMC card may be CMD1 for MMC Init sequence will be complete within 
           * 500ms */

          for (i = 100; i; --i)
            {
              fvdbg("%d. Send CMD1\n", i);
              SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
              SPI_SNDBYTE(spi, 0xff);
              result = mmcsd_sendcmd(slot, &g_cmd1, 0);
              SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);

              if (result == MMCSD_SPIR1_OK)
                {
                  fvdbg("%d. Identified MMC card\n", i);
                  slot->type = MMCSD_CARDTYPE_MMC;
                  break;
                }
              up_mdelay(50);
            }
          break;
        }
      else if (result == MMCSD_SPIR1_OK)
        {
          fvdbg("%d. Identified SD card\n", i);
          slot->type = MMCSD_CARDTYPE_SD;
          break;
        }

      up_mdelay(50);
    }

  if (i == 0)
    {
      fdbg("Retry exhausted\n");
      return -EIO;
    }

  /* Read CSD.  CSD must always be valid */

  fvdbg("Get CSD\n");
  result = mmcsd_getcsd(slot, csd);
  if (result != OK)
    {
      fdbg("mmcsd_getcsd(CMD9) failed: %d\n", result);
      return -EIO;
    }

#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_FS)
  if (slot->type == MMCSD_CARDTYPE_MMC)
    {
      fdbg("Found MMC card\n");
    }
  else if (MMCSD_CSD_CSDSTRUCT(csd) == 1)
    {
      fdbg("Found SDHC card\n");
    }
  else
    {
      fdbg("Found SD card\n");
    }
  mmcsd_dmpcsd(csd, slot->type);
#endif

  /* CSD data and set block size */

  mmcsd_decodecsd(slot, csd);
  mmcsd_checkwrprotect(slot, csd);
  mmcsd_setblklen(slot, slot->sectorsize);
  slot->state &= ~MMCSD_SLOTSTATUS_NOTREADY;
  return OK;
}

/****************************************************************************
 * Name: mmcsd_mediachanged
 *
 * Description:
 *   Handle initialization/media change events
 *
 ****************************************************************************/

static void mmcsd_mediachanged(void *arg)
{
  struct mmcsd_slot_s *slot = (struct mmcsd_slot_s*)arg;
  FAR struct spi_dev_s *spi;
  ubyte oldstate;
  int ret;

#ifdef CONFIG_DEBUG
  if (!slot || !slot->spi)
    {
      fdbg("Internal confusion\n");
      return;
    }
#endif
  spi  = slot->spi;

  /* Save the current slot state and reassess the new state */

  mmcsd_semtake(&slot->sem);
  oldstate = slot->state;

  /* Check if media was removed or inserted */

  slot->state &= ~(MMCSD_SLOTSTATUS_NODISK|MMCSD_SLOTSTATUS_NOTREADY|MMCSD_SLOTSTATUS_MEDIACHGD);
  if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_PRESENT) == 0)
    {
      /* Media is not present */

      fdbg("No card present\n");
      slot->state |= (MMCSD_SLOTSTATUS_NODISK|MMCSD_SLOTSTATUS_NOTREADY);

      /* Was media removed? */

      if ((oldstate & MMCSD_SLOTSTATUS_NODISK) == 0)
        {
          slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
        }
    }

  /* Media is present, was it just inserted? Or, if it was previously not ready,
   * then try re-initializing it
   */

  else if ((oldstate & (MMCSD_SLOTSTATUS_NODISK|MMCSD_SLOTSTATUS_NOTREADY)) != 0)
    {
      /* (Re-)ininitialize for the media in the slot */

      ret = mmcsd_mediainitialize(slot);
      if (ret == 0)
        {
          fvdbg("mmcsd_mediainitialize returned OK\n");
          slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
        }
    }
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mmcsd_spislotinitialize
 *
 * Description:
 *   Initialize one slot for operation using the SPI MMC/SD interface
 *
 * Input Parameters:
 *   minor - The MMC/SD minor device number.  The MMC/SD device will be
 *     registered as /dev/mmcsdN where N is the minor number
 *   slotno - The slot number to use.  This is only meaningful for architectures
 *     that support multiple MMC/SD slots.  This value must be in the range
 *     {0, ..., CONFIG_MMCSD_NSLOTS}.
 *   spi - And instance of an SPI interface obtained by called
 *     up_spiinitialize() with the appropriate port number (see spi.h)
 *
 ****************************************************************************/

int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi)
{
  struct mmcsd_slot_s *slot;
  char devname[16];
  int ret;

#ifdef CONFIG_DEBUG
  if ((unsigned)slotno >= CONFIG_MMCSD_NSLOTS || (unsigned)minor > 255 || !spi)
    {
      fdbg("Invalid arguments\n");
      return -EINVAL;
    }
#endif

  /* Select the slot structure */

  slot = &g_mmcsdslot[slotno];
  memset(slot, 0, sizeof(struct mmcsd_slot_s));
  sem_init(&slot->sem, 0, 1);

#ifdef CONFIG_DEBUG
  if (slot->spi)
    {
      fdbg("Already registered\n");
      return -EBUSY;
    }
#endif

  /* Bind the SPI port to the slot */

  slot->spi = spi;

  /* Ininitialize for the media in the slot (if any) */

  ret = mmcsd_mediainitialize(slot);
  if (ret == 0)
    {
      fvdbg("mmcsd_mediainitialize returned OK\n");
      slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
    }

  /* Create a MMC/SD device name */

  snprintf(devname, 16, "/dev/mmcsd%d", minor);

  /* Register the driver, even on a failure condition.  A
   * card may be inserted later, for example.
   */

  ret = register_blockdriver(devname, &g_bops, MMCSD_MODE, slot);
  if (ret < 0)
    {
      fdbg("register_blockdriver failed: %d\n", -ret);
      slot->spi = NULL;
      return ret;
    }

  /* Register a media change callback to handler insertion and
   * removal of cards.
   */

  (void)SPI_REGISTERCALLBACK(spi, mmcsd_mediachanged, (void*)slot);
  return OK;
}