Start to extract generic interface from the sd driver implementation.
[bertos.git] / bertos / cpu / cortex-m3 / drv / sd_sam3.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Function library for secure digital memory.
33  *
34  * \author Francesco Sacchi <batt@develer.com>
35  */
36
37
38 #include "hw/hw_sd.h"
39 #include "cfg/cfg_sd.h"
40
41 #include <drv/sd.h>
42 #include <drv/timer.h>
43 #include <drv/hsmci_sam3.h>
44
45 #include <io/kfile.h>
46 #include <io/kblock.h>
47
48 #include <fs/fat.h>
49
50 #define LOG_LEVEL  SD_LOG_LEVEL
51 #define LOG_FORMAT SD_LOG_FORMAT
52 #include <cfg/log.h>
53 #include <cpu/power.h>
54
55 #include <string.h> /* memset */
56
57
58 #define SD_STATUS_APP_CMD      BV(5)
59 #define SD_STATUS_READY        BV(8)
60 #define SD_STATUS_CURR_MASK    0x1E00
61 #define SD_STATUS_CURR_SHIFT   9
62
63 #define SD_ADDR_TO_RCA(addr)    (uint32_t)(((addr) << 16) & 0xFFFF0000)
64 #define SD_GET_STATE(status)    (uint8_t)(((status) & SD_STATUS_CURR_MASK) >> SD_STATUS_CURR_SHIFT)
65
66 static const uint32_t tran_exp[] =
67 {
68     10000,      100000,     1000000,    10000000,
69     0,      0,      0,      0
70 };
71
72 static const uint8_t tran_mant[] =
73 {
74     0,  10, 12, 13, 15, 20, 25, 30,
75     35, 40, 45, 50, 55, 60, 70, 80,
76 };
77
78
79 LOG_INFOB(
80 static void dump(const char *label, uint32_t *r, size_t len)
81 {
82         ASSERT(r);
83         size_t i;
84         int j = 0;
85         kprintf("\n%s [\n", label);
86         for (i = 0; i < len; i++)
87         {
88                 if (j == 5)
89                 {
90                         kputs("\n");
91                         j = 0;
92                 }
93                 kprintf("%08lx ", r[i]);
94                 j++;
95         }
96         kprintf("\n] len=%d\n\n", i);
97 }
98 )
99
100
101 static int sd_decodeCsd(SDcsd *csd, uint32_t *resp, size_t len)
102 {
103         ASSERT(csd);
104         ASSERT(resp);
105         ASSERT(len >= 4);
106
107     csd->structure = UNSTUFF_BITS(resp, 126, 2);
108         csd->ccc = UNSTUFF_BITS(resp, 84, 12);
109
110     csd->max_data_rate  = tran_exp[UNSTUFF_BITS(resp, 96, 3)] * tran_mant[UNSTUFF_BITS(resp, 99, 4)];
111
112         /*
113          * CSD structure:
114          * - 0:
115          *              - Version 1.01-1.10
116          *              - Version 2.00/Standard Capacity
117          * - 1:
118          *              - Version 2.00/High Capacity
119          * - >1: not defined.
120          */
121
122     if (csd->structure == 0)
123         {
124                 // (C_size + 1) x 2^(C_SIZE_MUL+2)
125                 csd->blk_num = (1 + UNSTUFF_BITS(resp, 62, 12)) << (UNSTUFF_BITS(resp, 47, 3) + 2);
126
127                 csd->read_blk_bits = UNSTUFF_BITS(resp, 80, 4);
128                 csd->write_blk_bits = UNSTUFF_BITS(resp, 22, 4);
129
130                 csd->blk_len = 1 << csd->read_blk_bits;
131         csd->capacity  = csd->blk_num * csd->blk_len;
132
133         csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
134         csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
135
136         csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
137         csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
138
139         if (UNSTUFF_BITS(resp, 46, 1))
140                 {
141             csd->erase_size = 1;
142         }
143                 else if(csd->write_blk_bits >= 9)
144                 {
145             csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
146             csd->erase_size <<= csd->write_blk_bits - 9;
147         }
148
149                 return 0;
150         }
151         else if (csd->structure == 1)
152         {
153                 kprintf("csize %ld\n", UNSTUFF_BITS(resp, 48, 22));
154         csd->capacity  = (1 + UNSTUFF_BITS(resp, 48, 22)) << 10;
155
156                 csd->write_blk_bits = 9;
157                 csd->write_partial = 0;
158         csd->write_misalign = 0;
159
160                 csd->read_blk_bits = 9;
161                 csd->read_partial = 0;
162         csd->read_misalign = 0;
163
164         csd->erase_size = 1;
165                 // the block size if fixed to 512kb
166                 csd->blk_len = (1 << csd->write_blk_bits) << 10;
167
168         return 0;
169         }
170     else
171         {
172         kprintf("Unrecognised CSD structure version %d\n", csd->structure);
173         return -1;
174     }
175
176     return 0;
177 }
178
179
180 void sd_dumpCsd(Sd *sd)
181 {
182         ASSERT(sd);
183
184         LOG_INFO("VERSION: %d.0\n", sd->csd.structure ? 2 : 1);
185     LOG_INFO("CARD COMMAND CLASS: %d\n", sd->csd.ccc);
186         LOG_INFO("MAX DATA RATE: %ld\n", sd->csd.max_data_rate);
187         LOG_INFO("WRITE BLK LEN BITS: %ld\n", sd->csd.write_blk_bits);
188         LOG_INFO("READ BLK LEN BITS: %ld\n", sd->csd.read_blk_bits);
189         LOG_INFO("ERASE SIZE: %ld\n", sd->csd.erase_size);
190         LOG_INFO("BLK NUM: %ld\n", sd->csd.blk_num);
191         LOG_INFO("BLK LEN: %ld\n", sd->csd.blk_len);
192         LOG_INFO("CAPACITY %ld\n", sd->csd.capacity);
193         LOG_INFO("FLAG Write: WP %d, W MISALIGN %d\n", sd->csd.write_partial, sd->csd.write_misalign);
194         LOG_INFO("FLAG Read: RP %d, R MISALIGN %d\n", sd->csd.read_partial, sd->csd.read_misalign);
195
196 }
197
198 void sd_dumpCid(Sd *sd)
199 {
200         ASSERT(sd);
201
202         LOG_INFO("MANFID: %d\n", sd->cid.manfid);
203     LOG_INFO("OEMID: %d\n", sd->cid.oemid);
204         LOG_INFO("SERIAL: %ld\n", sd->cid.serial);
205     LOG_INFO("PROD_NAME: %s\n", sd->cid.prod_name);
206     LOG_INFO("REV: %d.%d\n", sd->cid.m_rev, sd->cid.l_rev);
207     LOG_INFO("OFF,Y,M: %lx, %ld %ld\n", sd->cid.year_off, (BCD_TO_INT_32BIT(sd->cid.year_off) / 12) + 2000,
208                                                                                                 (BCD_TO_INT_32BIT(sd->cid.year_off) % 12));
209 }
210
211 void sd_dumpSsr(Sd *sd)
212 {
213         ASSERT(sd);
214
215         LOG_INFO("BUS_WIDTH: %d\n", sd->ssr.bus_width);
216     LOG_INFO("TYPE: %d\n", sd->ssr.card_type);
217         LOG_INFO("AU_TYPE: %d\n", sd->ssr.au_size);
218         LOG_INFO("ERASE_SIZE: %d\n", sd->ssr.erase_size);
219         LOG_INFO("SPEED_CLASS: %d\n", sd->ssr.speed_class);
220 }
221
222
223 void sd_sendInit(void)
224 {
225         hsmci_init(NULL); //TODO: REMOVE IT!
226
227         if (hsmci_sendCmd(0, 0, HSMCI_CMDR_SPCMD_INIT | HSMCI_CMDR_RSPTYP_NORESP))
228                 LOG_ERR("INIT: %lx\n", HSMCI_SR);
229 }
230
231
232 void sd_goIdle(void)
233 {
234         hsmci_setSpeed(HSMCI_INIT_SPEED, false);
235         if (hsmci_sendCmd(0, 0, HSMCI_CMDR_RSPTYP_NORESP))
236                 LOG_ERR("GO_IDLE: %lx\n", HSMCI_SR);
237 }
238
239 int sd_sendIfCond(Sd *sd)
240 {
241         if (hsmci_sendCmd(8, CMD8_V_RANGE_27V_36V, HSMCI_CMDR_RSPTYP_48_BIT))
242         {
243                 LOG_ERR("IF_COND %lx\n", HSMCI_SR);
244                 return -1;
245         }
246         hsmci_readResp(&(sd->status), 1);
247         if (((sd->status) & 0xFFF) == CMD8_V_RANGE_27V_36V)
248         {
249                 LOG_INFO("IF_COND: %lx\n", (sd->status));
250                 return 0;
251         }
252         LOG_ERR("IF_COND: %lx\n", (sd->status));
253
254         return -1;
255 }
256
257 int sd_sendAppOpCond(Sd *sd)
258 {
259         if (hsmci_sendCmd(55, 0, HSMCI_CMDR_RSPTYP_48_BIT))
260         {
261                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
262                 return -1;
263         }
264
265         hsmci_readResp(&(sd->status), 1);
266         if ((sd->status) & (SD_STATUS_APP_CMD | SD_STATUS_READY))
267         {
268                 if (hsmci_sendCmd(41, SD_HOST_VOLTAGE_RANGE | SD_OCR_CCS, HSMCI_CMDR_RSPTYP_48_BIT))// se cmd 8 va ok.
269                 {
270                         LOG_ERR("APP_OP_COND %lx\n", HSMCI_SR);
271                         return -1;
272                 }
273                 else
274                 {
275                         hsmci_readResp(&(sd->status), 1);
276                         if ((sd->status) & SD_OCR_BUSY)
277                         {
278                                 LOG_INFO("SD power up! Hight Capability [%d]\n", (bool)((sd->status) & SD_OCR_CCS));
279                                 return 0;
280                         }
281
282                         LOG_ERR("sd not ready.\n");
283                 }
284         }
285
286         return -1;
287 }
288
289
290 int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag)
291 {
292         ASSERT(sd);
293         memset(&(sd->cid), 0, sizeof(SDcid));
294
295         uint8_t idx = 9; // CMD9 get cid from gived sd address (RCA)
296         if (flag & SD_SEND_ALL_CID)
297                 idx = 2;
298
299
300         if (hsmci_sendCmd(idx, SD_ADDR_TO_RCA(addr), HSMCI_CMDR_RSPTYP_136_BIT))
301         {
302                 LOG_ERR("GET_CID %lx\n", HSMCI_SR);
303                 return -1;
304         }
305         else
306         {
307                 uint32_t resp[4];
308                 hsmci_readResp(resp, 4);
309                 LOG_INFOB(dump("CID", resp, 4););
310
311                 sd->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
312                 sd->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
313                 sd->cid.prod_name[0]      = UNSTUFF_BITS(resp, 96, 8);
314                 sd->cid.prod_name[1]      = UNSTUFF_BITS(resp, 88, 8);
315                 sd->cid.prod_name[2]      = UNSTUFF_BITS(resp, 80, 8);
316                 sd->cid.prod_name[3]      = UNSTUFF_BITS(resp, 72, 8);
317                 sd->cid.prod_name[4]      = UNSTUFF_BITS(resp, 64, 8);
318                 sd->cid.m_rev         = UNSTUFF_BITS(resp, 60, 4);
319                 sd->cid.l_rev         = UNSTUFF_BITS(resp, 56, 4);
320                 sd->cid.serial        = (uint32_t)UNSTUFF_BITS(resp, 24, 32);
321                 sd->cid.year_off      = UNSTUFF_BITS(resp, 8, 12);
322         }
323
324         return 0;
325 }
326
327 int sd_getCsd(Sd *sd)
328 {
329         ASSERT(sd);
330         memset(&(sd->csd), 0, sizeof(SDcsd));
331
332         LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr));
333         if (hsmci_sendCmd(9, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_136_BIT))
334         {
335                 LOG_ERR("GET_CSD %lx\n", HSMCI_SR);
336                 return -1;
337         }
338         else
339         {
340                 uint32_t resp[4];
341                 hsmci_readResp(resp, 4);
342                 LOG_INFOB(dump("CSD", resp, 4););
343                 sd_decodeCsd(&(sd->csd), resp, 4);
344         }
345
346         return 0;
347 }
348
349 int sd_getRelativeAddr(Sd *sd)
350 {
351         ASSERT(sd);
352         if (hsmci_sendCmd(3, 0, HSMCI_CMDR_RSPTYP_48_BIT))
353         {
354                 LOG_ERR("RCA: %lx\n", HSMCI_SR);
355                 return -1;
356         }
357
358         hsmci_readResp(&sd->addr, 1);
359         sd->addr = sd->addr >> 16;
360
361         LOG_INFOB(dump("RCA", &sd->addr, 1););
362
363         return 0;
364 }
365
366 int sd_appStatus(Sd *sd)
367 {
368         ASSERT(sd);
369         LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr));
370         if (hsmci_sendCmd(13, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
371         {
372                 LOG_ERR("STATUS: %lx\n", HSMCI_SR);
373                 return -1;
374         }
375
376         hsmci_readResp(&(sd->status), 1);
377         LOG_INFOB(dump("STATUS", &(sd->status), 1););
378
379         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
380
381         if (sd->status & SD_STATUS_READY)
382                 return 0;
383
384         return -1;
385 }
386
387
388 INLINE int sd_cardSelection(Sd *sd, uint32_t rca)
389 {
390         ASSERT(sd);
391         LOG_INFO("Select RCA: %lx\n", rca);
392         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_R1B))
393         {
394                 LOG_ERR("SELECT_SD: %lx\n", HSMCI_SR);
395                 return -1;
396         }
397
398         HSMCI_CHECK_BUSY();
399         hsmci_readResp(&(sd->status), 1);
400         LOG_INFOB(dump("SELECT_SD", &(sd->status), 1););
401
402         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
403
404         if (sd->status & SD_STATUS_READY)
405                 return 0;
406
407         return -1;
408 }
409
410 int sd_selectCard(Sd *sd)
411 {
412         ASSERT(sd);
413         uint32_t rca = SD_ADDR_TO_RCA(sd->addr);
414         LOG_INFO("Select RCA: %lx\n", rca);
415         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_R1B))
416         {
417                 LOG_ERR("SELECT_SD: %lx\n", HSMCI_SR);
418                 return -1;
419         }
420
421         HSMCI_CHECK_BUSY();
422         hsmci_readResp(&(sd->status), 1);
423
424         LOG_INFOB(dump("SELECT_SD", &(sd->status), 1););
425         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
426
427         if (sd->status & SD_STATUS_READY)
428                 return 0;
429
430         return -1;
431 }
432
433 int sd_deSelectCard(Sd *sd)
434 {
435         ASSERT(sd);
436
437         uint32_t rca = 0;
438         if (!sd->addr)
439                 rca = SD_ADDR_TO_RCA(sd->addr + 1);
440
441         LOG_INFO("Select RCA: %lx\n", rca);
442
443         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_NORESP))
444         {
445                 LOG_ERR("DESELECT_SD: %lx\n", HSMCI_SR);
446                 return -1;
447         }
448
449         return 0;
450 }
451
452 int sd_setBusWidth(Sd *sd, size_t len)
453 {
454         ASSERT(sd);
455
456         if (hsmci_sendCmd(55, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
457         {
458                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
459                 return -1;
460         }
461
462         hsmci_readResp(&(sd->status), 1);
463         if ((sd->status) & (SD_STATUS_APP_CMD | SD_STATUS_READY))
464         {
465                 hsmci_setBusWidth(len);
466
467                 uint8_t arg = 0;
468                 if (len == 4)
469                         arg = 2;
470
471                 if (hsmci_sendCmd(6, arg, HSMCI_CMDR_RSPTYP_48_BIT))
472                 {
473                         LOG_ERR("SET_BUS_WIDTH CMD: %lx\n", HSMCI_SR);
474                         return -1;
475                 }
476
477                 hsmci_readResp(&(sd->status), 1);
478
479                 LOG_INFOB(dump("SET_BUS_WIDTH", &(sd->status), 1););
480                 LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
481
482                 if (sd->status & SD_STATUS_READY)
483                         return 0;
484         }
485
486         LOG_ERR("SET_BUS_WIDTH REP %lx\n", (sd->status));
487         return -1;
488 }
489
490
491 int sd_set_BlockLen(Sd *sd, size_t len)
492 {
493         ASSERT(sd);
494
495         if (hsmci_sendCmd(16, len, HSMCI_CMDR_RSPTYP_48_BIT))
496         {
497                 LOG_ERR("SET_BLK_LEN: %lx\n", HSMCI_SR);
498                 return -1;
499         }
500
501         hsmci_readResp(&(sd->status), 1);
502
503         LOG_INFOB(dump("SET_BLK_LEN", &(sd->status), 1););
504         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
505
506         sd->csd.blk_len = len;
507
508         if (sd->status & SD_STATUS_READY)
509                 return 0;
510
511         return -1;
512 }
513
514 int sd_getStatus(Sd *sd, uint32_t *buf, size_t words)
515 {
516         ASSERT(sd);
517
518         // Status reply with 512bit data, so the block size in byte is 64
519         hsmci_prgRxDMA(buf, words, 64);
520
521         if (hsmci_sendCmd(55, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
522         {
523                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
524                 return -1;
525         }
526
527         uint32_t status = HSMCI_RSPR;
528         if (status & (SD_STATUS_APP_CMD | SD_STATUS_READY))
529         {
530                 if (hsmci_sendCmd(13, 0, HSMCI_CMDR_RSPTYP_48_BIT |
531                                 BV(HSMCI_CMDR_TRDIR) | HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
532                 {
533                         LOG_ERR("STATUS CMD: %lx\n", HSMCI_SR);
534                         return -1;
535                 }
536
537                 hsmci_readResp(&(sd->status), 1);
538                 LOG_INFOB(dump("STATUS", &(sd->status), 1););
539                 LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
540
541                 if (sd->status & SD_STATUS_READY)
542                 {
543                         hsmci_waitTransfer();
544
545                         LOG_INFOB(dump("STATUS", buf, words););
546                         memset(&(sd->ssr), 0, sizeof(SDssr));
547
548                         sd->ssr.bus_width  = UNSTUFF_BITS(buf, 510, 2);
549                         sd->ssr.card_type  = UNSTUFF_BITS(buf, 480, 16);
550                         sd->ssr.au_size  = UNSTUFF_BITS(buf, 432, 8);
551                         sd->ssr.speed_class  = UNSTUFF_BITS(buf, 440, 8);
552                         sd->ssr.erase_size = UNSTUFF_BITS(buf, 408, 24);
553
554                         return 0;
555                 }
556         }
557
558         return -1;
559 }
560
561
562 void sd_setHightSpeed(Sd *sd)
563 {
564         (void)sd;
565         hsmci_setSpeed(2100000, true);
566 }
567
568
569 static size_t sd_SdReadDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
570 {
571         ASSERT(buf);
572         Sd *sd = SD_CAST(b);
573         LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
574
575         if (sd_selectCard(sd) < 0)
576                 return -1;
577
578         hsmci_prgRxDMA(buf, size / 4, sd->csd.blk_len);
579
580         if (hsmci_sendCmd(17, idx * sd->csd.blk_len + offset, HSMCI_CMDR_RSPTYP_48_BIT |
581                         BV(HSMCI_CMDR_TRDIR) | HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
582         {
583                 LOG_ERR("SIGLE_BLK_READ: %lx\n", HSMCI_SR);
584                 sd_deSelectCard(sd);
585                 return -1;
586         }
587
588         hsmci_readResp(&(sd->status), 1);
589
590         LOG_INFOB(dump("SIGLE_BLK_READ", &(sd->status), 1););
591         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
592
593         if (sd->status & SD_STATUS_READY)
594         {
595                 hsmci_waitTransfer();
596                 sd_deSelectCard(sd);
597                 return size;
598         }
599
600         sd_deSelectCard(sd);
601         return -1;
602 }
603
604 static size_t sd_SdWriteDirect(KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
605 {
606         ASSERT(buf);
607         Sd *sd = SD_CAST(b);
608         const uint32_t *_buf = (const uint32_t *)buf;
609         LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
610
611         if (sd_selectCard(sd) < 0)
612                 return 0;
613
614         hsmci_prgTxDMA(_buf, size / 4, sd->csd.blk_len);
615
616         if (hsmci_sendCmd(24, idx * sd->csd.blk_len + offset, HSMCI_CMDR_RSPTYP_48_BIT |
617                                                 HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
618         {
619                 LOG_ERR("SIGLE_BLK_WRITE: %lx\n", HSMCI_SR);
620                 sd_deSelectCard(sd);
621                 return -1;
622         }
623
624         hsmci_readResp(&(sd->status), 1);
625
626         LOG_INFOB(dump("SIGLE_BLK_WR", &(sd->status), 1););
627         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
628
629         if (sd->status & SD_STATUS_READY)
630         {
631                 hsmci_waitTransfer();
632                 sd_deSelectCard(sd);
633                 return size;
634         }
635
636         sd_deSelectCard(sd);
637         return -1;
638 }
639
640
641 static int sd_SdError(KBlock *b)
642 {
643         Sd *sd = SD_CAST(b);
644         return 0;//sd->status;
645 }
646
647 static void sd_SdClearerr(KBlock *b)
648 {
649         Sd *sd = SD_CAST(b);
650         sd->status = 0;
651 }
652
653 static bool sd_blockInit(Sd *sd, KFile *ch)
654 {
655         ASSERT(sd);
656         memset(sd, 0, sizeof(*sd));
657         DB(sd->b.priv.type = KBT_SD);
658
659         /* Wait a few moments for supply voltage to stabilize */
660         timer_delay(SD_START_DELAY);
661
662         sd_sendInit();
663         sd_goIdle();
664
665         sd_sendIfCond(sd);
666
667         ticks_t start = timer_clock();
668         bool sd_power_on = false;
669         do
670         {
671                 if (!sd_sendAppOpCond(sd))
672                 {
673                         sd_power_on = true;
674                         break;
675                 }
676                 cpu_relax();
677         }
678         while (timer_clock() - start < SD_INIT_TIMEOUT);
679
680
681         if (sd_power_on)
682         {
683                 if(sd_getCid(sd, 0, SD_SEND_ALL_CID) < 0)
684                         return false;
685                 else
686                 {
687                         sd_dumpCid(sd);
688                 }
689
690                 if (sd_getRelativeAddr(sd) < 0)
691                         return false;
692                 else
693                 {
694                         LOG_INFO("RCA: %0lx\n", sd->addr);
695                 }
696
697
698                 if (sd_getCsd(sd) < 0)
699                         return false;
700                 else
701                 {
702                         sd_dumpCsd(sd);
703                 }
704
705                 if (sd_appStatus(sd) < 0)
706                 {
707                         LOG_INFO("STATUS: %ld\n", sd->status);
708                         return false;
709                 }
710
711                 if (sd->status & SD_CARD_IS_LOCKED)
712                 {
713                         LOG_INFO("SD is locked!\n");
714                         return false;
715                 }
716
717                 if (sd->status & SD_READY_FOR_DATA)
718                 {
719                         sd_selectCard(sd);
720                         sd_set_BlockLen(sd, SD_DEFAULT_BLOCKLEN);
721                         sd_setBus4bit(sd);
722                         sd_setHightSpeed(sd);
723                         sd_deSelectCard(sd);
724
725                         sd->b.blk_size = SD_DEFAULT_BLOCKLEN;
726                         sd->b.blk_cnt = sd->csd.blk_num * (sd->csd.blk_len / SD_DEFAULT_BLOCKLEN);
727                         LOG_INFO("blk_size %d, blk_cnt %ld\n", sd->b.blk_size, sd->b.blk_cnt);
728
729                         #if CONFIG_SD_AUTOASSIGN_FAT
730                                 disk_assignDrive(&sd->b, 0);
731                         #endif
732
733                         return true;
734                 }
735         }
736
737         return false;
738 }
739
740 static const KBlockVTable sd_unbuffered_vt =
741 {
742         .readDirect = sd_SdReadDirect,
743         .writeDirect = sd_SdWriteDirect,
744
745         .error = sd_SdError,
746         .clearerr = sd_SdClearerr,
747 };
748
749 static const KBlockVTable sd_buffered_vt =
750 {
751         .readDirect = sd_SdReadDirect,
752         .writeDirect = sd_SdWriteDirect,
753
754         .readBuf = kblock_swReadBuf,
755         .writeBuf = kblock_swWriteBuf,
756         .load = kblock_swLoad,
757         .store = kblock_swStore,
758
759         .error = sd_SdError,
760         .clearerr = sd_SdClearerr,
761 };
762
763 bool sd_hw_initUnbuf(Sd *sd, KFile *ch)
764 {
765         if (sd_blockInit(sd, ch))
766         {
767                 sd->b.priv.vt = &sd_unbuffered_vt;
768                 return true;
769         }
770         else
771                 return false;
772 }
773
774 static uint8_t sd_buf[SD_DEFAULT_BLOCKLEN];
775
776 bool sd_hw_initBuf(Sd *sd, KFile *ch)
777 {
778         if (sd_blockInit(sd, ch))
779         {
780                 sd->b.priv.buf = sd_buf;
781                 sd->b.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
782                 sd->b.priv.vt = &sd_buffered_vt;
783                 sd->b.priv.vt->load(&sd->b, 0);
784                 return true;
785         }
786         else
787                 return false;
788 }
789
790
791