Make configurable the i2s word size to play, using cfg macros.
[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 2011 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Function library for secure digital memory.
33  *
34  * \author Daniele Basile <asterix@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_SEND_ALL_CID        BV(0)
59 #define SD_STATUS_APP_CMD      BV(5)
60 #define SD_STATUS_READY        BV(8)
61 #define SD_CARD_IS_LOCKED      BV(25)
62
63 #define SD_OCR_CCS              BV(30)     /**< SD Card Capacity Status (CCS) */
64 #define SD_OCR_BUSY             BV(31)     /**< SD/MMC Card power up status bit (busy) */
65
66 #define SD_OCR_VDD_27_28        BV(15)
67 #define SD_OCR_VDD_28_29        BV(16)
68 #define SD_OCR_VDD_29_30        BV(17)
69 #define SD_OCR_VDD_30_31        BV(18)
70 #define SD_OCR_VDD_31_32        BV(19)
71 #define SD_OCR_VDD_32_33        BV(20)
72
73
74 #define SD_HOST_VOLTAGE_RANGE     (SD_OCR_VDD_27_28 | \
75                                    SD_OCR_VDD_28_29 | \
76                                    SD_OCR_VDD_29_30 | \
77                                    SD_OCR_VDD_30_31 | \
78                                    SD_OCR_VDD_31_32 | \
79                                    SD_OCR_VDD_32_33)
80
81
82 #define CMD8_V_RANGE_CHECK_PAT    0xAA
83 #define CMD8_V_RANGE_27V_36V      (0x100 | CMD8_V_RANGE_CHECK_PAT)
84 #define CMD8_V_RANGE_LOW          (0x1000 | CMD8_V_RANGE_CHECK_PAT)
85 #define CMD8_V_ECHO_REPLY         0xFF
86 #define CMD8_SUPP_V_RANGE_REPLY   0xFF00
87
88 #define SD_STATUS_ERROR          BV(19)
89
90 #define SD_GET_ERRORS(status)   ((status) & 0xFFF80000)
91 #define SD_ADDR_TO_RCA(addr)    (uint32_t)(((addr) << 16) & 0xFFFF0000)
92 #define SD_GET_STATE(status)    (uint8_t)(((status) & 0x1E00) >> 9)
93
94 static const uint32_t tran_exp[] =
95 {
96         10000,      100000,     1000000,    10000000,
97         0,      0,      0,      0
98 };
99
100 static const uint8_t tran_mant[] =
101 {
102         0,  10, 12, 13, 15, 20, 25, 30,
103         35, 40, 45, 50, 55, 60, 70, 80,
104 };
105
106
107 void sd_dumpCsd(SdCSD *csd);
108 void sd_dumpCid(SdCID *cid);
109 void sd_dumpSsr(SdSSR *ssr);
110
111 void sd_sendInit(void);
112 void sd_goIdle(void);
113 int sd_sendIfCond(Sd *sd);
114 int sd_sendAppOpCond(Sd *sd);
115
116 int sd_getCid(Sd *sd, SdCID *cid, uint32_t addr, uint8_t flag);
117 int sd_getCsd(Sd *sd, SdCSD *csd);
118 int sd_getSrc(Sd *sd);
119
120 int sd_appStatus(Sd *sd);
121 int sd_getRelativeAddr(Sd *sd);
122 int sd_getStatus(Sd *sd, SdSSR *ssr, uint32_t *buf, size_t words);
123
124 int sd_selectCard(Sd *sd);
125 int sd_deSelectCard(Sd *sd);
126 int sd_setBusWidth(Sd *sd, size_t len);
127 int sd_set_BlockLen(Sd *sd, size_t len);
128 void sd_setHightSpeed(Sd *sd);
129
130
131 INLINE int sd_setBus4bit(Sd *sd)
132 {
133         return sd_setBusWidth(sd, 4);
134 }
135
136 INLINE int sd_setBus1bit(Sd *sd)
137 {
138         return sd_setBusWidth(sd, 1);
139 }
140
141
142 LOG_INFOB(
143 static void dump(const char *label, uint32_t *r, size_t len)
144 {
145         ASSERT(r);
146         size_t i;
147         int j = 0;
148         kprintf("\n%s [\n", label);
149         for (i = 0; i < len; i++)
150         {
151                 if (j == 5)
152                 {
153                         kputs("\n");
154                         j = 0;
155                 }
156                 kprintf("%08lx ", r[i]);
157                 j++;
158         }
159         kprintf("\n] len=%d\n\n", i);
160 }
161 )
162
163 void sd_dumpCsd(SdCSD *csd)
164 {
165         ASSERT(csd);
166
167         LOG_INFO("VERSION: %d.0\n", csd->structure ? 2 : 1);
168         LOG_INFO("CARD COMMAND CLASS: %d\n", csd->ccc);
169         LOG_INFO("MAX DATA RATE: %ld\n", csd->max_data_rate);
170         LOG_INFO("WRITE BLK LEN BITS: %ld\n", csd->write_blk_bits);
171         LOG_INFO("READ BLK LEN BITS: %ld\n", csd->read_blk_bits);
172         LOG_INFO("ERASE SIZE: %ld\n", csd->erase_size);
173         LOG_INFO("BLK NUM: %ld\n", csd->block_num);
174         LOG_INFO("BLK LEN: %ld\n", csd->block_len);
175         LOG_INFO("CAPACITY %ld\n", csd->capacity);
176         LOG_INFO("FLAG Write: WP %d, W MISALIGN %d\n", csd->write_partial, csd->write_misalign);
177         LOG_INFO("FLAG Read: RP %d, R MISALIGN %d\n", csd->read_partial, csd->read_misalign);
178
179 }
180
181 void sd_dumpCid(SdCID *cid)
182 {
183         ASSERT(cid);
184
185         LOG_INFO("MANFID: %d\n", cid->manfid);
186         LOG_INFO("OEMID: %d\n", cid->oemid);
187         LOG_INFO("SERIAL: %ld\n", cid->serial);
188         LOG_INFO("PROD_NAME: %s\n", cid->prod_name);
189         LOG_INFO("REV: %d.%d\n", cid->m_rev, cid->l_rev);
190         LOG_INFO("OFF,Y,M: %lx, %ld %ld\n", cid->year_off, (BCD_TO_INT_32BIT(cid->year_off) / 12) + 2000,
191                                                                                                 (BCD_TO_INT_32BIT(cid->year_off) % 12));
192 }
193
194 void sd_dumpSsr(SdSSR *ssr)
195 {
196         ASSERT(ssr);
197
198         LOG_INFO("BUS_WIDTH: %d\n", ssr->bus_width);
199         LOG_INFO("TYPE: %d\n", ssr->card_type);
200         LOG_INFO("AU_TYPE: %d\n", ssr->au_size);
201         LOG_INFO("ERASE_SIZE: %d\n", ssr->erase_size);
202         LOG_INFO("SPEED_CLASS: %d\n", ssr->speed_class);
203 }
204
205
206 static int sd_decodeCsd(SdCSD *csd, uint32_t *resp, size_t len)
207 {
208         ASSERT(csd);
209         ASSERT(resp);
210         ASSERT(len >= 4);
211
212         csd->structure = UNSTUFF_BITS(resp, 126, 2);
213         csd->ccc = UNSTUFF_BITS(resp, 84, 12);
214
215         csd->max_data_rate  = tran_exp[UNSTUFF_BITS(resp, 96, 3)] * tran_mant[UNSTUFF_BITS(resp, 99, 4)];
216
217         /*
218          * CSD structure:
219          * - 0:
220          *              - Version 1.01-1.10
221          *              - Version 2.00/Standard Capacity
222          * - 1:
223          *              - Version 2.00/High Capacity
224          * - >1: not defined.
225          */
226
227         if (csd->structure == 0)
228         {
229                 // (C_size + 1) x 2^(C_SIZE_MUL+2)
230                 csd->block_num = (1 + UNSTUFF_BITS(resp, 62, 12)) << (UNSTUFF_BITS(resp, 47, 3) + 2);
231
232                 csd->read_blk_bits = UNSTUFF_BITS(resp, 80, 4);
233                 csd->write_blk_bits = UNSTUFF_BITS(resp, 22, 4);
234
235                 csd->block_len = 1 << csd->read_blk_bits;
236                 csd->capacity  = csd->block_num * csd->block_len;
237
238                 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
239                 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
240
241                 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
242                 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
243
244                 if (UNSTUFF_BITS(resp, 46, 1))
245                 {
246                         csd->erase_size = 1;
247                 }
248                 else if(csd->write_blk_bits >= 9)
249                 {
250                         csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
251                         csd->erase_size <<= csd->write_blk_bits - 9;
252                 }
253
254                 return 0;
255         }
256         else if (csd->structure == 1)
257         {
258                 kprintf("csize %ld\n", UNSTUFF_BITS(resp, 48, 22));
259                 csd->capacity  = (1 + UNSTUFF_BITS(resp, 48, 22)) << 10;
260
261                 csd->write_blk_bits = 9;
262                 csd->write_partial = 0;
263                 csd->write_misalign = 0;
264
265                 csd->read_blk_bits = 9;
266                 csd->read_partial = 0;
267                 csd->read_misalign = 0;
268
269                 csd->erase_size = 1;
270                 // the block size if fixed to 512kb
271                 csd->block_len = (1 << csd->write_blk_bits) << 10;
272
273                 return 0;
274         }
275         else
276         {
277                 kprintf("Unrecognised CSD structure version %d\n", csd->structure);
278                 return -1;
279         }
280
281         return 0;
282 }
283
284 void sd_sendInit(void)
285 {
286         hsmci_init(NULL); //TODO: REMOVE IT!
287
288         if (hsmci_sendCmd(0, 0, HSMCI_CMDR_SPCMD_INIT | HSMCI_CMDR_RSPTYP_NORESP))
289                 LOG_ERR("INIT: %lx\n", HSMCI_SR);
290 }
291
292
293 void sd_goIdle(void)
294 {
295         hsmci_setSpeed(HSMCI_INIT_SPEED, false);
296         if (hsmci_sendCmd(0, 0, HSMCI_CMDR_RSPTYP_NORESP))
297                 LOG_ERR("GO_IDLE: %lx\n", HSMCI_SR);
298 }
299
300 int sd_sendIfCond(Sd *sd)
301 {
302         if (hsmci_sendCmd(8, CMD8_V_RANGE_27V_36V, HSMCI_CMDR_RSPTYP_48_BIT))
303         {
304                 LOG_ERR("IF_COND %lx\n", HSMCI_SR);
305                 return -1;
306         }
307         hsmci_readResp(&(sd->status), 1);
308         if (((sd->status) & 0xFFF) == CMD8_V_RANGE_27V_36V)
309         {
310                 LOG_INFO("IF_COND: %lx\n", (sd->status));
311                 return 0;
312         }
313         LOG_ERR("IF_COND: %lx\n", (sd->status));
314         return -1;
315 }
316
317 int sd_sendAppOpCond(Sd *sd)
318 {
319         if (hsmci_sendCmd(55, 0, HSMCI_CMDR_RSPTYP_48_BIT))
320         {
321                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
322                 return -1;
323         }
324
325         hsmci_readResp(&(sd->status), 1);
326         if ((sd->status) & (SD_STATUS_APP_CMD | SD_STATUS_READY))
327         {
328                 if (hsmci_sendCmd(41, SD_HOST_VOLTAGE_RANGE | SD_OCR_CCS, HSMCI_CMDR_RSPTYP_48_BIT))// se cmd 8 va ok.
329                 {
330                         LOG_ERR("APP_OP_COND %lx\n", HSMCI_SR);
331                         return -1;
332                 }
333                 else
334                 {
335                         hsmci_readResp(&(sd->status), 1);
336                         if ((sd->status) & SD_OCR_BUSY)
337                         {
338                                 LOG_INFO("SD power up! Hight Capability [%d]\n", (bool)((sd->status) & SD_OCR_CCS));
339                                 return 0;
340                         }
341                 }
342         }
343
344         return -1;
345 }
346
347
348 int sd_getCid(Sd *sd, SdCID *cid, uint32_t addr, uint8_t flag)
349 {
350         ASSERT(sd);
351         ASSERT(cid);
352         memset(cid, 0, sizeof(SdCID));
353
354         uint8_t idx = 9; // CMD9 get cid from gived sd address (RCA)
355         if (flag & SD_SEND_ALL_CID)
356                 idx = 2;
357
358
359         if (hsmci_sendCmd(idx, SD_ADDR_TO_RCA(addr), HSMCI_CMDR_RSPTYP_136_BIT))
360         {
361                 LOG_ERR("GET_CID %lx\n", HSMCI_SR);
362                 return -1;
363         }
364         else
365         {
366                 uint32_t resp[4];
367                 hsmci_readResp(resp, 4);
368                 LOG_INFOB(dump("CID", resp, 4););
369
370                 cid->manfid        = UNSTUFF_BITS(resp, 120, 8);
371                 cid->oemid         = UNSTUFF_BITS(resp, 104, 16);
372                 cid->prod_name[0]      = UNSTUFF_BITS(resp, 96, 8);
373                 cid->prod_name[1]      = UNSTUFF_BITS(resp, 88, 8);
374                 cid->prod_name[2]      = UNSTUFF_BITS(resp, 80, 8);
375                 cid->prod_name[3]      = UNSTUFF_BITS(resp, 72, 8);
376                 cid->prod_name[4]      = UNSTUFF_BITS(resp, 64, 8);
377                 cid->m_rev         = UNSTUFF_BITS(resp, 60, 4);
378                 cid->l_rev         = UNSTUFF_BITS(resp, 56, 4);
379                 cid->serial        = (uint32_t)UNSTUFF_BITS(resp, 24, 32);
380                 cid->year_off      = UNSTUFF_BITS(resp, 8, 12);
381         }
382
383         return 0;
384 }
385
386 int sd_getCsd(Sd *sd, SdCSD *csd)
387 {
388         ASSERT(sd);
389         ASSERT(csd);
390         memset(csd, 0, sizeof(SdCSD));
391
392         LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr));
393         if (hsmci_sendCmd(9, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_136_BIT))
394         {
395                 LOG_ERR("GET_CSD %lx\n", HSMCI_SR);
396                 return -1;
397         }
398         else
399         {
400                 uint32_t resp[4];
401                 hsmci_readResp(resp, 4);
402                 LOG_INFOB(dump("CSD", resp, 4););
403                 sd_decodeCsd(csd, resp, 4);
404         }
405
406         return 0;
407 }
408
409 int sd_getRelativeAddr(Sd *sd)
410 {
411         ASSERT(sd);
412         if (hsmci_sendCmd(3, 0, HSMCI_CMDR_RSPTYP_48_BIT))
413         {
414                 LOG_ERR("RCA: %lx\n", HSMCI_SR);
415                 return -1;
416         }
417
418         hsmci_readResp(&sd->addr, 1);
419         sd->addr = sd->addr >> 16;
420
421         LOG_INFOB(dump("RCA", &sd->addr, 1););
422
423         return 0;
424 }
425
426 int sd_appStatus(Sd *sd)
427 {
428         ASSERT(sd);
429         LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr));
430         if (hsmci_sendCmd(13, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
431         {
432                 LOG_ERR("STATUS: %lx\n", HSMCI_SR);
433                 sd->status |= SD_STATUS_ERROR;
434                 return -1;
435         }
436
437         hsmci_readResp(&(sd->status), 1);
438         LOG_INFOB(dump("STATUS", &(sd->status), 1););
439
440         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
441
442         if (sd->status & SD_STATUS_READY)
443                 return 0;
444
445         return -1;
446 }
447
448
449 INLINE int sd_cardSelection(Sd *sd, uint32_t rca)
450 {
451         ASSERT(sd);
452         LOG_INFO("Select RCA: %lx\n", rca);
453         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_R1B))
454         {
455                 LOG_ERR("SELECT_SD: %lx\n", HSMCI_SR);
456                 sd->status |= SD_STATUS_ERROR;
457                 return -1;
458         }
459
460         HSMCI_CHECK_BUSY();
461         hsmci_readResp(&(sd->status), 1);
462         LOG_INFOB(dump("SELECT_SD", &(sd->status), 1););
463
464         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
465
466         if (sd->status & SD_STATUS_READY)
467                 return 0;
468
469         return -1;
470 }
471
472 int sd_selectCard(Sd *sd)
473 {
474         ASSERT(sd);
475         uint32_t rca = SD_ADDR_TO_RCA(sd->addr);
476         LOG_INFO("Select RCA: %lx\n", rca);
477         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_R1B))
478         {
479                 LOG_ERR("SELECT_SD: %lx\n", HSMCI_SR);
480                 return -1;
481         }
482
483         HSMCI_CHECK_BUSY();
484         hsmci_readResp(&(sd->status), 1);
485
486         LOG_INFOB(dump("SELECT_SD", &(sd->status), 1););
487         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
488
489         if (sd->status & SD_STATUS_READY)
490                 return 0;
491
492         return -1;
493 }
494
495 int sd_deSelectCard(Sd *sd)
496 {
497         ASSERT(sd);
498
499         uint32_t rca = 0;
500         if (!sd->addr)
501                 rca = SD_ADDR_TO_RCA(sd->addr + 1);
502
503         LOG_INFO("Select RCA: %lx\n", rca);
504
505         if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_NORESP))
506         {
507                 LOG_ERR("DESELECT_SD: %lx\n", HSMCI_SR);
508                 return -1;
509         }
510
511         return 0;
512 }
513
514 int sd_setBusWidth(Sd *sd, size_t len)
515 {
516         ASSERT(sd);
517
518         if (hsmci_sendCmd(55, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
519         {
520                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
521                 return -1;
522         }
523
524         hsmci_readResp(&(sd->status), 1);
525         if ((sd->status) & (SD_STATUS_APP_CMD | SD_STATUS_READY))
526         {
527                 hsmci_setBusWidth(len);
528
529                 uint8_t arg = 0;
530                 if (len == 4)
531                         arg = 2;
532
533                 if (hsmci_sendCmd(6, arg, HSMCI_CMDR_RSPTYP_48_BIT))
534                 {
535                         LOG_ERR("SET_BUS_WIDTH CMD: %lx\n", HSMCI_SR);
536                         return -1;
537                 }
538
539                 hsmci_readResp(&(sd->status), 1);
540
541                 LOG_INFOB(dump("SET_BUS_WIDTH", &(sd->status), 1););
542                 LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
543
544                 if (sd->status & SD_STATUS_READY)
545                         return 0;
546         }
547
548         LOG_ERR("SET_BUS_WIDTH REP %lx\n", (sd->status));
549         return -1;
550 }
551
552
553 int sd_set_BlockLen(Sd *sd, size_t len)
554 {
555         ASSERT(sd);
556
557         if (hsmci_sendCmd(16, len, HSMCI_CMDR_RSPTYP_48_BIT))
558         {
559                 LOG_ERR("SET_BLK_LEN: %lx\n", HSMCI_SR);
560                 return -1;
561         }
562
563         hsmci_readResp(&(sd->status), 1);
564
565         LOG_INFOB(dump("SET_BLK_LEN", &(sd->status), 1););
566         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
567
568         sd->b.blk_size = len;
569
570         if (sd->status & SD_STATUS_READY)
571                 return 0;
572
573         return -1;
574 }
575
576 int sd_getStatus(Sd *sd, SdSSR *ssr, uint32_t *buf, size_t words)
577 {
578         ASSERT(sd);
579         ASSERT(ssr);
580
581         // Status reply with 512bit data, so the block size in byte is 64
582         hsmci_read(buf, words, 64);
583
584         if (hsmci_sendCmd(55, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT))
585         {
586                 LOG_ERR("APP_CMD %lx\n", HSMCI_SR);
587                 return -1;
588         }
589
590         hsmci_readResp(&(sd->status), 1);
591         if (sd->status & (SD_STATUS_APP_CMD | SD_STATUS_READY))
592         {
593                 if (hsmci_sendCmd(13, 0, HSMCI_CMDR_RSPTYP_48_BIT |
594                                 BV(HSMCI_CMDR_TRDIR) | HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
595                 {
596                         LOG_ERR("STATUS CMD: %lx\n", HSMCI_SR);
597                         return -1;
598                 }
599
600                 hsmci_readResp(&(sd->status), 1);
601                 LOG_INFOB(dump("STATUS", &(sd->status), 1););
602                 LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
603
604                 if (sd->status & SD_STATUS_READY)
605                 {
606                         hsmci_waitTransfer();
607
608                         LOG_INFOB(dump("STATUS", buf, words););
609                         memset(ssr, 0, sizeof(SdSSR));
610                         ssr->bus_width  = UNSTUFF_BITS(buf, 510, 2);
611                         ssr->card_type  = UNSTUFF_BITS(buf, 480, 16);
612                         ssr->au_size  = UNSTUFF_BITS(buf, 432, 8);
613                         ssr->speed_class  = UNSTUFF_BITS(buf, 440, 8);
614                         ssr->erase_size = UNSTUFF_BITS(buf, 408, 24);
615
616                         return 0;
617                 }
618         }
619
620         return -1;
621 }
622
623
624 void sd_setHightSpeed(Sd *sd)
625 {
626         (void)sd;
627         hsmci_setSpeed(HSMCI_HIGH_SPEED, HSMCI_HS_MODE);
628 }
629
630
631 static size_t sd_SdReadDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
632 {
633         ASSERT(buf);
634         ASSERT(!((uint32_t)buf & 0x3));
635
636         Sd *sd = SD_CAST(b);
637         LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
638
639         if (sd_selectCard(sd) < 0)
640         {
641                 sd->status |= SD_STATUS_ERROR;
642                 return -1;
643         }
644
645         hsmci_read(buf, size / 4, sd->b.blk_size);
646
647         if (hsmci_sendCmd(17, idx * sd->b.blk_size + offset, HSMCI_CMDR_RSPTYP_48_BIT |
648                         BV(HSMCI_CMDR_TRDIR) | HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
649         {
650                 LOG_ERR("SIGLE_BLK_READ: %lx\n", HSMCI_SR);
651                 sd_deSelectCard(sd);
652                 return -1;
653         }
654
655         hsmci_readResp(&(sd->status), 1);
656
657         LOG_INFOB(dump("SIGLE_BLK_READ", &(sd->status), 1););
658         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
659
660         if (sd->status & SD_STATUS_READY)
661         {
662                 hsmci_waitTransfer();
663                 sd_deSelectCard(sd);
664                 return size;
665         }
666
667         sd_deSelectCard(sd);
668         return -1;
669 }
670
671 static size_t sd_SdWriteDirect(KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
672 {
673         ASSERT(buf);
674         ASSERT(!((uint32_t)buf & 0x3));
675
676         Sd *sd = SD_CAST(b);
677         const uint32_t *_buf = (const uint32_t *)buf;
678         LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
679
680         if (sd_selectCard(sd) < 0)
681         {
682                 sd->status |= SD_STATUS_ERROR;
683                 return -1;
684         }
685
686         hsmci_write(_buf, size / 4, sd->b.blk_size);
687
688         if (hsmci_sendCmd(24, idx * sd->b.blk_size + offset, HSMCI_CMDR_RSPTYP_48_BIT |
689                                                 HSMCI_CMDR_TRCMD_START_DATA | HSMCI_CMDR_TRTYP_SINGLE))
690         {
691                 LOG_ERR("SIGLE_BLK_WRITE: %lx\n", HSMCI_SR);
692                 sd_deSelectCard(sd);
693                 return -1;
694         }
695
696         hsmci_readResp(&(sd->status), 1);
697
698         LOG_INFOB(dump("SIGLE_BLK_WR", &(sd->status), 1););
699         LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status));
700
701         if (sd->status & SD_STATUS_READY)
702         {
703                 hsmci_waitTransfer();
704                 sd_deSelectCard(sd);
705                 return size;
706         }
707
708         sd_deSelectCard(sd);
709         return -1;
710 }
711
712
713 static int sd_SdError(KBlock *b)
714 {
715         Sd *sd = SD_CAST(b);
716         return SD_GET_ERRORS(sd->status);
717 }
718
719 static void sd_SdClearerr(KBlock *b)
720 {
721         Sd *sd = SD_CAST(b);
722         sd->status = 0;
723 }
724
725 static bool sd_blockInit(Sd *sd, KFile *ch)
726 {
727         (void)ch;
728         ASSERT(sd);
729         memset(sd, 0, sizeof(*sd));
730         DB(sd->b.priv.type = KBT_SD);
731
732         /* Wait a few moments for supply voltage to stabilize */
733         timer_delay(SD_START_DELAY);
734
735         sd_sendInit();
736         sd_goIdle();
737
738         sd_sendIfCond(sd);
739
740         ticks_t start = timer_clock();
741         bool sd_power_on = false;
742         do
743         {
744                 if (!sd_sendAppOpCond(sd))
745                 {
746                         sd_power_on = true;
747                         break;
748                 }
749                 cpu_relax();
750         }
751         while (timer_clock() - start < SD_INIT_TIMEOUT);
752
753
754         if (sd_power_on)
755         {
756                 SdCID cid;
757                 if(sd_getCid(sd, &cid, 0, SD_SEND_ALL_CID) < 0)
758                         return false;
759                 else
760                 {
761                         sd_dumpCid(&cid);
762                 }
763
764                 if (sd_getRelativeAddr(sd) < 0)
765                         return false;
766                 else
767                 {
768                         LOG_INFO("RCA: %0lx\n", sd->addr);
769                 }
770
771                 SdCSD csd;
772                 if (sd_getCsd(sd, &csd) < 0)
773                         return false;
774                 else
775                 {
776                         sd->b.blk_cnt = csd.block_num * (csd.block_len / SD_DEFAULT_BLOCKLEN);
777                         LOG_INFO("blk_size %d, blk_cnt %ld\n", sd->b.blk_size, sd->b.blk_cnt);
778                         sd_dumpCsd(&csd);
779                 }
780
781                 if (sd_appStatus(sd) < 0)
782                 {
783                         LOG_INFO("STATUS: %ld\n", sd->status);
784                         return false;
785                 }
786
787                 if (sd->status & SD_CARD_IS_LOCKED)
788                 {
789                         LOG_INFO("SD is locked!\n");
790                         return false;
791                 }
792
793                 if (sd->status & SD_STATUS_READY)
794                 {
795                         sd_selectCard(sd);
796                         sd_set_BlockLen(sd, SD_DEFAULT_BLOCKLEN);
797                         sd_setBus4bit(sd);
798                         sd_setHightSpeed(sd);
799                         sd_deSelectCard(sd);
800
801                         #if CONFIG_SD_AUTOASSIGN_FAT
802                                 disk_assignDrive(&sd->b, 0);
803                         #endif
804
805                         return true;
806                 }
807         }
808         LOG_ERR("SD not ready.\n");
809         return false;
810 }
811
812 static const KBlockVTable sd_unbuffered_vt =
813 {
814         .readDirect = sd_SdReadDirect,
815         .writeDirect = sd_SdWriteDirect,
816
817         .error = sd_SdError,
818         .clearerr = sd_SdClearerr,
819 };
820
821 static const KBlockVTable sd_buffered_vt =
822 {
823         .readDirect = sd_SdReadDirect,
824         .writeDirect = sd_SdWriteDirect,
825
826         .readBuf = kblock_swReadBuf,
827         .writeBuf = kblock_swWriteBuf,
828         .load = kblock_swLoad,
829         .store = kblock_swStore,
830
831         .error = sd_SdError,
832         .clearerr = sd_SdClearerr,
833 };
834
835 bool sd_hw_initUnbuf(Sd *sd, KFile *ch)
836 {
837         if (sd_blockInit(sd, ch))
838         {
839                 sd->b.priv.vt = &sd_unbuffered_vt;
840                 return true;
841         }
842         else
843                 return false;
844 }
845
846 static uint8_t sd_buf[SD_DEFAULT_BLOCKLEN];
847
848 bool sd_hw_initBuf(Sd *sd, KFile *ch)
849 {
850         if (sd_blockInit(sd, ch))
851         {
852                 sd->b.priv.buf = sd_buf;
853                 sd->b.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
854                 sd->b.priv.vt = &sd_buffered_vt;
855                 sd->b.priv.vt->load(&sd->b, 0);
856                 return true;
857         }
858         else
859                 return false;
860 }
861
862
863