sdnative.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. #include <arm/NXP/LPC17xx/LPC17xx.h>
  2. #include <stdio.h>
  3. #include "config.h"
  4. #include "crc.h"
  5. #include "crc16.h"
  6. #include "diskio.h"
  7. #include "spi.h"
  8. #include "timer.h"
  9. #include "uart.h"
  10. #include "led.h"
  11. #include "sdnative.h"
  12. #include "fileops.h"
  13. #include "bits.h"
  14. #include "fpga_spi.h"
  15. #define MAX_CARDS 1
  16. // SD/MMC commands
  17. #define GO_IDLE_STATE 0
  18. #define SEND_OP_COND 1
  19. #define ALL_SEND_CID 2
  20. #define SEND_RELATIVE_ADDR 3
  21. #define SWITCH_FUNC 6
  22. #define SELECT_CARD 7
  23. #define SEND_IF_COND 8
  24. #define SEND_CSD 9
  25. #define SEND_CID 10
  26. #define STOP_TRANSMISSION 12
  27. #define SEND_STATUS 13
  28. #define SET_BLOCKLEN 16
  29. #define READ_SINGLE_BLOCK 17
  30. #define READ_MULTIPLE_BLOCK 18
  31. #define WRITE_BLOCK 24
  32. #define WRITE_MULTIPLE_BLOCK 25
  33. #define PROGRAM_CSD 27
  34. #define SET_WRITE_PROT 28
  35. #define CLR_WRITE_PROT 29
  36. #define SEND_WRITE_PROT 30
  37. #define ERASE_WR_BLK_STAR_ADDR 32
  38. #define ERASE_WR_BLK_END_ADDR 33
  39. #define ERASE 38
  40. #define LOCK_UNLOCK 42
  41. #define APP_CMD 55
  42. #define GEN_CMD 56
  43. #define READ_OCR 58
  44. #define CRC_ON_OFF 59
  45. // SD ACMDs
  46. #define SD_SET_BUS_WIDTH 6
  47. #define SD_STATUS 13
  48. #define SD_SEND_NUM_WR_BLOCKS 22
  49. #define SD_SET_WR_BLK_ERASE_COUNT 23
  50. #define SD_SEND_OP_COND 41
  51. #define SD_SET_CLR_CARD_DETECT 42
  52. #define SD_SEND_SCR 51
  53. // R1 status bits
  54. #define STATUS_IN_IDLE 1
  55. #define STATUS_ERASE_RESET 2
  56. #define STATUS_ILLEGAL_COMMAND 4
  57. #define STATUS_CRC_ERROR 8
  58. #define STATUS_ERASE_SEQ_ERROR 16
  59. #define STATUS_ADDRESS_ERROR 32
  60. #define STATUS_PARAMETER_ERROR 64
  61. /* Card types - cardtype == 0 is MMC */
  62. #define CARD_SD (1<<0)
  63. #define CARD_SDHC (1<<1)
  64. /*
  65. Rev.A Rev.C
  66. 1 DAT3/SS P0.6 P2.3
  67. 2 CMD/DI P0.9 P0.9
  68. 5 Clock P0.7 P0.7
  69. 7 DAT0/DO P0.8 P2.0
  70. 8 DAT1/IRQ P1.14 P2.1
  71. 9 DAT2/NC P1.15 P2.2
  72. */
  73. /* SD init procedure
  74. =================
  75. - initial clock frequency: ~100kHz
  76. - cycle the clock for at least 74 cycles (some more may be safer)
  77. - send CMD0
  78. - send CMD8 (SEND_OP_COND); if no response -> HCS=0; else HCS=1
  79. - send ACMD41 until OCR[31] (busy) becomes 1 (means: ready)
  80. - if OCR[30] (CCS) set -> SDHC; else SDSC
  81. - send CMD2 (read CID) (maybe log some stuff from the CID)
  82. - send CMD3 (read RCA), store RCA
  83. == end of initialisation ==
  84. - send CMD9 (read CSD) with RCA, maybe do sth with TRAN_SPEED
  85. - send CMD7 with RCA, select card, put card in tran
  86. - maybe send CMD13 with RCA to check state (tran)
  87. - send ACMD51 with RCA to read SCR (maybe, to check 4bit support)
  88. - increase clock speed
  89. - send ACMD6 with RCA to set 4bit bus width
  90. - send transfer cmds
  91. */
  92. /*
  93. static CMD payloads. (no CRC calc required)
  94. - CMD0: 0x40 0x00 0x00 0x00 0x00 0x95
  95. - CMD8: 0x48 0x00 0x00 0x01 0xaa 0x87
  96. - CMD2: 0x42 0x00 0x00 0x00 0x00 0x4d
  97. - CMD3: 0x43 0x00 0x00 0x00 0x00 0x21
  98. - CMD55: 0x77 0x00 0x00 0x00 0x00 0x65
  99. */
  100. uint8_t cmd[6]={0,0,0,0,0,0};
  101. uint8_t rsp[17];
  102. uint8_t csd[17];
  103. uint8_t ccs=0;
  104. uint32_t rca;
  105. enum trans_state { TRANS_NONE = 0, TRANS_READ, TRANS_WRITE };
  106. enum cmd_state { CMD_RSP = 0, CMD_RSPDAT, CMD_DAT };
  107. int during_blocktrans = TRANS_NONE;
  108. uint32_t last_block = 0;
  109. volatile int sd_changed;
  110. /**
  111. * getbits - read value from bit buffer
  112. * @buffer: pointer to the data buffer
  113. * @start : index of the first bit in the value
  114. * @bits : number of bits in the value
  115. *
  116. * This function returns a value from the memory region passed as
  117. * buffer, starting with bit "start" and "bits" bit long. The buffer
  118. * is assumed to be MSB first, passing 0 for start will read starting
  119. * from the highest-value bit of the first byte of the buffer.
  120. */
  121. static uint32_t getbits(void *buffer, uint16_t start, int8_t bits) {
  122. uint8_t *buf = buffer;
  123. uint32_t result = 0;
  124. if ((start % 8) != 0) {
  125. /* Unaligned start */
  126. result += buf[start / 8] & (0xff >> (start % 8));
  127. bits -= 8 - (start % 8);
  128. start += 8 - (start % 8);
  129. }
  130. while (bits >= 8) {
  131. result = (result << 8) + buf[start / 8];
  132. start += 8;
  133. bits -= 8;
  134. }
  135. if (bits > 0) {
  136. result = result << bits;
  137. result = result + (buf[start / 8] >> (8-bits));
  138. } else if (bits < 0) {
  139. /* Fraction of a single byte */
  140. result = result >> -bits;
  141. }
  142. return result;
  143. }
  144. static inline void wiggle_slow_pos(uint16_t times) {
  145. while(times--) {
  146. delay_us(2);
  147. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  148. delay_us(2);
  149. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  150. }
  151. }
  152. static inline void wiggle_slow_neg(uint16_t times) {
  153. while(times--) {
  154. delay_us(2);
  155. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  156. delay_us(2);
  157. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  158. }
  159. }
  160. static inline void wiggle_fast_pos(uint16_t times) {
  161. while(times--) {
  162. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  163. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  164. }
  165. }
  166. static inline void wiggle_fast_neg(uint16_t times) {
  167. while(times--) {
  168. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  169. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  170. }
  171. }
  172. static inline void wiggle_fast_neg1(void) {
  173. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  174. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  175. }
  176. static inline void wiggle_fast_pos1(void) {
  177. BITBAND(SD_CLKREG->FIOSET, SD_CLKPIN) = 1;
  178. BITBAND(SD_CLKREG->FIOCLR, SD_CLKPIN) = 1;
  179. }
  180. int get_and_check_datacrc(uint8_t *buf) {
  181. uint16_t crc0=0, crc1=0, crc2=0, crc3=0;
  182. uint16_t sdcrc0=0, sdcrc1=0, sdcrc2=0, sdcrc3=0;
  183. uint8_t d0=0, d1=0, d2=0, d3=0;
  184. uint8_t datdata;
  185. uint16_t datcnt;
  186. /* get crcs from card */
  187. for (datcnt=0; datcnt < 16; datcnt++) {
  188. datdata = SD_DAT;
  189. wiggle_fast_neg1();
  190. sdcrc0 = ((sdcrc0 << 1) & 0xfffe) | ((datdata >> 3) & 0x0001);
  191. sdcrc1 = ((sdcrc1 << 1) & 0xfffe) | ((datdata >> 2) & 0x0001);
  192. sdcrc2 = ((sdcrc2 << 1) & 0xfffe) | ((datdata >> 1) & 0x0001);
  193. sdcrc3 = ((sdcrc3 << 1) & 0xfffe) | ((datdata >> 0) & 0x0001);
  194. }
  195. wiggle_fast_neg1();
  196. /* calc crcs from data */
  197. for (datcnt=0; datcnt < 512; datcnt++) {
  198. d0 = ((d0 << 2) & 0xfc) | ((buf[datcnt] >> 6) & 0x02) | ((buf[datcnt] >> 3) & 0x01) ;
  199. d1 = ((d1 << 2) & 0xfc) | ((buf[datcnt] >> 5) & 0x02) | ((buf[datcnt] >> 2) & 0x01) ;
  200. d2 = ((d2 << 2) & 0xfc) | ((buf[datcnt] >> 4) & 0x02) | ((buf[datcnt] >> 1) & 0x01) ;
  201. d3 = ((d3 << 2) & 0xfc) | ((buf[datcnt] >> 3) & 0x02) | ((buf[datcnt] >> 0) & 0x01) ;
  202. if((datcnt % 4) == 3) {
  203. crc0 = crc_xmodem_update(crc0, d0);
  204. crc1 = crc_xmodem_update(crc1, d1);
  205. crc2 = crc_xmodem_update(crc2, d2);
  206. crc3 = crc_xmodem_update(crc3, d3);
  207. }
  208. }
  209. if((crc0 != sdcrc0) || (crc1 != sdcrc1) || (crc2 != sdcrc2) || (crc3 != sdcrc3)) {
  210. printf("CRC mismatch\nSDCRC CRC\n %04x %04x\n %04x %04x\n %04x %04x\n %04x %04x\n", sdcrc0, crc0, sdcrc1, crc1, sdcrc2, crc2, sdcrc3, crc3);
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. static inline void wait_busy(void) {
  216. while(!(BITBAND(SD_DAT0REG->FIOPIN, SD_DAT0PIN))) {
  217. wiggle_fast_neg1();
  218. }
  219. wiggle_fast_neg(4);
  220. }
  221. /*
  222. send_command_slow
  223. send SD command and put response in rsp.
  224. returns length of response or 0 if there was no response
  225. */
  226. int send_command_slow(uint8_t* cmd, uint8_t* rsp){
  227. uint8_t shift, i=6;
  228. int rsplen;
  229. uint8_t cmdno = *cmd & 0x3f;
  230. wiggle_slow_pos(5);
  231. switch(*cmd & 0x3f) {
  232. case 0:
  233. rsplen = 0;
  234. break;
  235. case 2:
  236. case 9:
  237. case 10:
  238. rsplen = 17;
  239. break;
  240. default:
  241. rsplen = 6;
  242. }
  243. /* send command */
  244. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 1;
  245. while(i--) {
  246. shift = 8;
  247. do {
  248. shift--;
  249. uint8_t data = *cmd;
  250. *cmd<<=1;
  251. if(data&0x80) {
  252. BITBAND(SD_CMDREG->FIOSET, SD_CMDPIN) = 1;
  253. } else {
  254. BITBAND(SD_CMDREG->FIOCLR, SD_CMDPIN) = 1;
  255. }
  256. wiggle_slow_pos(1);
  257. } while (shift);
  258. cmd++;
  259. }
  260. wiggle_slow_pos(1);
  261. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 0;
  262. if(rsplen) {
  263. uint16_t timeout=1000;
  264. while((BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN)) && --timeout) {
  265. wiggle_slow_neg(1);
  266. }
  267. if(!timeout) {
  268. printf("CMD%d timed out\n", cmdno);
  269. return 0; /* no response within timeout */
  270. }
  271. i=rsplen;
  272. while(i--) {
  273. shift = 8;
  274. uint8_t data=0;
  275. do {
  276. shift--;
  277. data |= (BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN)) << shift;
  278. wiggle_slow_neg(1);
  279. } while (shift);
  280. *rsp=data;
  281. rsp++;
  282. }
  283. }
  284. return rsplen;
  285. }
  286. /*
  287. send_command_fast
  288. send SD command and put response in rsp.
  289. returns length of response or 0 if there was no response
  290. */
  291. int send_command_fast(uint8_t* cmd, uint8_t* rsp, uint8_t* buf){
  292. uint8_t datshift=8, cmdshift, i=6;
  293. uint8_t cmdno = *cmd & 0x3f;
  294. int rsplen, dat=0, waitbusy=0, datcnt=512, j=0;
  295. static int state=CMD_RSP;
  296. wiggle_fast_pos(9); /* give the card >=8 cycles after last command */
  297. DBG_SD printf("send_command_fast: sending CMD%d; payload=%02x%02x%02x%02x%02x%02x...\n", cmdno, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5]);
  298. switch(*cmd & 0x3f) {
  299. case 0:
  300. rsplen = 0;
  301. break;
  302. case 2:
  303. case 9:
  304. case 10:
  305. rsplen = 17;
  306. break;
  307. case 12:
  308. rsplen = 6;
  309. waitbusy = 1;
  310. break;
  311. case 13:
  312. case 17:
  313. case 18:
  314. dat = 1;
  315. default:
  316. rsplen = 6;
  317. }
  318. if(dat && (buf==NULL)) {
  319. printf("send_command_fast error: buf is null but data transfer expected.\n");
  320. return 0;
  321. }
  322. /* send command */
  323. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 1;
  324. while(i--) {
  325. uint8_t data = *cmd;
  326. cmdshift = 8;
  327. do {
  328. cmdshift--;
  329. if(data&0x80) {
  330. BITBAND(SD_CMDREG->FIOSET, SD_CMDPIN) = 1;
  331. } else {
  332. BITBAND(SD_CMDREG->FIOCLR, SD_CMDPIN) = 1;
  333. }
  334. data<<=1;
  335. wiggle_fast_pos1();
  336. } while (cmdshift);
  337. cmd++;
  338. }
  339. wiggle_fast_pos1();
  340. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 0;
  341. if(rsplen) {
  342. uint32_t timeout=2000000;
  343. /* wait for response */
  344. while((BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN)) && --timeout) {
  345. wiggle_fast_neg1();
  346. }
  347. if(!timeout) {
  348. printf("CMD%d timed out\n", cmdno);
  349. return 0; /* no response within timeout */
  350. }
  351. i=rsplen;
  352. uint8_t cmddata=0, datdata=0;
  353. while(i--) { /* process response */
  354. cmdshift = 8;
  355. do {
  356. if(dat) {
  357. if(!(BITBAND(SD_DAT0REG->FIOPIN, SD_DAT0PIN))) {
  358. printf("data start during response\n");
  359. j=datcnt;
  360. state=CMD_RSPDAT;
  361. break;
  362. }
  363. }
  364. cmdshift--;
  365. cmddata |= (BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN)) << cmdshift;
  366. wiggle_fast_neg1();
  367. } while (cmdshift);
  368. if(state==CMD_RSPDAT)break;
  369. *rsp=cmddata;
  370. cmddata=0;
  371. rsp++;
  372. }
  373. if(state==CMD_RSPDAT) { /* process response+data */
  374. int startbit=1;
  375. DBG_SD printf("processing rsp+data cmdshift=%d i=%d j=%d\n", cmdshift, i, j);
  376. datshift=8;
  377. while(1) {
  378. cmdshift--;
  379. cmddata |= (BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN)) << cmdshift;
  380. if(!cmdshift) {
  381. cmdshift=8;
  382. *rsp=cmddata;
  383. cmddata=0;
  384. rsp++;
  385. i--;
  386. if(!i) {
  387. DBG_SD printf("response end\n");
  388. if(j) state=CMD_DAT; /* response over, remaining data */
  389. break;
  390. }
  391. }
  392. if(!startbit) {
  393. datshift-=4;
  394. datdata |= SD_DAT << datshift;
  395. if(!datshift) {
  396. datshift=8;
  397. *buf=datdata;
  398. datdata=0;
  399. buf++;
  400. j--;
  401. if(!j) break;
  402. }
  403. }
  404. startbit=0;
  405. wiggle_fast_neg1();
  406. }
  407. }
  408. if(dat && state != CMD_DAT) { /* response ended before data */
  409. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 1;
  410. state=CMD_DAT;
  411. j=datcnt;
  412. datshift=8;
  413. DBG_SD printf("response over, waiting for data...\n");
  414. while((BITBAND(SD_DAT0REG->FIOPIN, SD_DAT0PIN)) && --timeout) {
  415. wiggle_fast_neg1();
  416. }
  417. wiggle_fast_neg1(); /* eat the start bit */
  418. if(sd_offload) {
  419. if(sd_offload_partial) {
  420. fpga_set_sddma_range(sd_offload_partial_start, sd_offload_partial_end);
  421. fpga_sddma(sd_offload_tgt, 1);
  422. sd_offload_partial=0;
  423. } else {
  424. fpga_sddma(sd_offload_tgt, 0);
  425. }
  426. state=CMD_RSP;
  427. return rsplen;
  428. }
  429. }
  430. if(state==CMD_DAT) { /* transfer rest of data */
  431. DBG_SD printf("remaining data: %d\n", j);
  432. if(datshift==8) {
  433. while(1) {
  434. datdata |= SD_DAT << 4;
  435. wiggle_fast_neg1();
  436. datdata |= SD_DAT;
  437. wiggle_fast_neg1();
  438. *buf=datdata;
  439. datdata=0;
  440. buf++;
  441. j--;
  442. if(!j) break;
  443. }
  444. } else {
  445. while(1) {
  446. datshift-=4;
  447. datdata |= SD_DAT << datshift;
  448. if(!datshift) {
  449. datshift=8;
  450. *buf=datdata;
  451. datdata=0;
  452. buf++;
  453. j--;
  454. if(!j) break;
  455. }
  456. wiggle_fast_neg1();
  457. }
  458. }
  459. }
  460. if(dat) {
  461. #ifdef CONFIG_SD_DATACRC
  462. if(get_and_check_datacrc(buf-512)) {
  463. return CRC_ERROR;
  464. }
  465. #else
  466. /* eat the crcs */
  467. wiggle_fast_neg(17);
  468. #endif
  469. }
  470. if(waitbusy) {
  471. DBG_SD printf("waitbusy after send_cmd\n");
  472. wait_busy();
  473. }
  474. state=CMD_RSP;
  475. }
  476. rsp-=rsplen;
  477. DBG_SD printf("send_command_fast: CMD%d response: %02x%02x%02x%02x%02x%02x\n", cmdno, rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5]);
  478. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 1;
  479. return rsplen;
  480. }
  481. static inline void make_crc7(uint8_t* cmd) {
  482. cmd[5]=crc7update(0, cmd[0]);
  483. cmd[5]=crc7update(cmd[5], cmd[1]);
  484. cmd[5]=crc7update(cmd[5], cmd[2]);
  485. cmd[5]=crc7update(cmd[5], cmd[3]);
  486. cmd[5]=crc7update(cmd[5], cmd[4]);
  487. cmd[5]=(cmd[5] << 1) | 1;
  488. }
  489. int cmd_slow(uint8_t cmd, uint32_t param, uint8_t crc, uint8_t* dat, uint8_t* rsp) {
  490. uint8_t cmdbuf[6];
  491. cmdbuf[0] = 0x40 | cmd;
  492. cmdbuf[1] = param >> 24;
  493. cmdbuf[2] = param >> 16;
  494. cmdbuf[3] = param >> 8;
  495. cmdbuf[4] = param;
  496. if(!crc) {
  497. make_crc7(cmdbuf);
  498. } else {
  499. cmdbuf[5] = crc;
  500. }
  501. return send_command_slow(cmdbuf, rsp);
  502. }
  503. int acmd_slow(uint8_t cmd, uint32_t param, uint8_t crc, uint8_t* dat, uint8_t* rsp) {
  504. if(!(cmd_slow(APP_CMD, rca, 0, NULL, rsp))) {
  505. return 0;
  506. }
  507. return cmd_slow(cmd, param, crc, dat, rsp);
  508. }
  509. int cmd_fast(uint8_t cmd, uint32_t param, uint8_t crc, uint8_t* dat, uint8_t* rsp) {
  510. uint8_t cmdbuf[6];
  511. cmdbuf[0] = 0x40 | cmd;
  512. cmdbuf[1] = param >> 24;
  513. cmdbuf[2] = param >> 16;
  514. cmdbuf[3] = param >> 8;
  515. cmdbuf[4] = param;
  516. if(!crc) {
  517. make_crc7(cmdbuf);
  518. } else {
  519. cmdbuf[5] = crc;
  520. }
  521. return send_command_fast(cmdbuf, rsp, dat);
  522. }
  523. int acmd_fast(uint8_t cmd, uint32_t param, uint8_t crc, uint8_t* dat, uint8_t* rsp) {
  524. if(!(cmd_fast(APP_CMD, rca, 0, NULL, rsp))) {
  525. return 0;
  526. }
  527. return cmd_fast(cmd, param, crc, dat, rsp);
  528. }
  529. int stream_datablock(uint8_t *buf) {
  530. // uint8_t datshift=8;
  531. int j=512;
  532. uint8_t datdata=0;
  533. uint32_t timeout=1000000;
  534. while((BITBAND(SD_DAT0REG->FIOPIN, SD_DAT0PIN)) && --timeout) {
  535. wiggle_fast_neg1();
  536. }
  537. wiggle_fast_neg1(); /* eat the start bit */
  538. if(sd_offload) {
  539. if(sd_offload_partial) {
  540. fpga_set_sddma_range(sd_offload_partial_start, sd_offload_partial_end);
  541. fpga_sddma(sd_offload_tgt, 1);
  542. sd_offload_partial=0;
  543. } else {
  544. fpga_sddma(sd_offload_tgt, 0);
  545. }
  546. } else {
  547. while(1) {
  548. datdata = SD_DAT << 4;
  549. wiggle_fast_neg1();
  550. datdata |= SD_DAT;
  551. wiggle_fast_neg1();
  552. *buf=datdata;
  553. buf++;
  554. j--;
  555. if(!j) break;
  556. }
  557. #ifdef CONFIG_SD_DATACRC
  558. return get_and_check_datacrc(buf-512);
  559. #else
  560. /* eat the crcs */
  561. wiggle_fast_neg(17);
  562. #endif
  563. }
  564. return 0;
  565. }
  566. void send_datablock(uint8_t *buf) {
  567. uint16_t crc0=0, crc1=0, crc2=0, crc3=0, cnt=512;
  568. uint8_t dat0=0, dat1=0, dat2=0, dat3=0, crcshift, datshift;
  569. wiggle_fast_pos1();
  570. BITBAND(SD_DAT0REG->FIODIR, SD_DAT0PIN) = 1;
  571. BITBAND(SD_DAT1REG->FIODIR, SD_DAT1PIN) = 1;
  572. BITBAND(SD_DAT2REG->FIODIR, SD_DAT2PIN) = 1;
  573. BITBAND(SD_DAT3REG->FIODIR, SD_DAT3PIN) = 1;
  574. BITBAND(SD_DAT0REG->FIOCLR, SD_DAT0PIN) = 1;
  575. BITBAND(SD_DAT1REG->FIOCLR, SD_DAT1PIN) = 1;
  576. BITBAND(SD_DAT2REG->FIOCLR, SD_DAT2PIN) = 1;
  577. BITBAND(SD_DAT3REG->FIOCLR, SD_DAT3PIN) = 1;
  578. wiggle_fast_pos1(); /* send start bit to card */
  579. crcshift=8;
  580. while(cnt--) {
  581. datshift=8;
  582. do {
  583. datshift-=4;
  584. /* if(((*buf)>>datshift) & 0x8) {
  585. BITBAND(SD_DAT3REG->FIOSET, SD_DAT3PIN) = 1;
  586. } else {
  587. BITBAND(SD_DAT3REG->FIOCLR, SD_DAT3PIN) = 1;
  588. }
  589. if(((*buf)>>datshift) & 0x4) {
  590. BITBAND(SD_DAT2REG->FIOSET, SD_DAT2PIN) = 1;
  591. } else {
  592. BITBAND(SD_DAT2REG->FIOCLR, SD_DAT2PIN) = 1;
  593. }
  594. if(((*buf)>>datshift) & 0x2){
  595. BITBAND(SD_DAT1REG->FIOSET, SD_DAT1PIN) = 1;
  596. } else {
  597. BITBAND(SD_DAT1REG->FIOCLR, SD_DAT1PIN) = 1;
  598. }
  599. if(((*buf)>>datshift) & 0x1){
  600. BITBAND(SD_DAT0REG->FIOSET, SD_DAT0PIN) = 1;
  601. } else {
  602. BITBAND(SD_DAT0REG->FIOCLR, SD_DAT0PIN) = 1;
  603. }*/
  604. SD_DAT0REG->FIOPIN0 = (*buf) >> datshift;
  605. wiggle_fast_pos1();
  606. } while (datshift);
  607. crcshift-=2;
  608. dat0 |= (((*buf)&0x01) | (((*buf)&0x10) >> 3)) << crcshift;
  609. dat1 |= ((((*buf)&0x02) >> 1) | (((*buf)&0x20) >> 4)) << crcshift;
  610. dat2 |= ((((*buf)&0x04) >> 2) | (((*buf)&0x40) >> 5)) << crcshift;
  611. dat3 |= ((((*buf)&0x08) >> 3) | (((*buf)&0x80) >> 6)) << crcshift;
  612. if(!crcshift) {
  613. crc0 = crc_xmodem_update(crc0, dat0);
  614. crc1 = crc_xmodem_update(crc1, dat1);
  615. crc2 = crc_xmodem_update(crc2, dat2);
  616. crc3 = crc_xmodem_update(crc3, dat3);
  617. crcshift=8;
  618. dat0=0;
  619. dat1=0;
  620. dat2=0;
  621. dat3=0;
  622. }
  623. buf++;
  624. }
  625. // printf("crc0=%04x crc1=%04x crc2=%04x crc3=%04x ", crc0, crc1, crc2, crc3);
  626. /* send crcs */
  627. datshift=16;
  628. do {
  629. datshift--;
  630. if((crc0 >> datshift) & 1) {
  631. BITBAND(SD_DAT0REG->FIOSET, SD_DAT0PIN) = 1;
  632. } else {
  633. BITBAND(SD_DAT0REG->FIOCLR, SD_DAT0PIN) = 1;
  634. }
  635. if((crc1 >> datshift) & 1) {
  636. BITBAND(SD_DAT1REG->FIOSET, SD_DAT1PIN) = 1;
  637. } else {
  638. BITBAND(SD_DAT1REG->FIOCLR, SD_DAT1PIN) = 1;
  639. }
  640. if((crc2 >> datshift) & 1) {
  641. BITBAND(SD_DAT2REG->FIOSET, SD_DAT2PIN) = 1;
  642. } else {
  643. BITBAND(SD_DAT2REG->FIOCLR, SD_DAT2PIN) = 1;
  644. }
  645. if((crc3 >> datshift) & 1) {
  646. BITBAND(SD_DAT3REG->FIOSET, SD_DAT3PIN) = 1;
  647. } else {
  648. BITBAND(SD_DAT3REG->FIOCLR, SD_DAT3PIN) = 1;
  649. }
  650. wiggle_fast_pos1();
  651. } while(datshift);
  652. /* send end bit */
  653. BITBAND(SD_DAT0REG->FIOSET, SD_DAT0PIN) = 1;
  654. BITBAND(SD_DAT1REG->FIOSET, SD_DAT1PIN) = 1;
  655. BITBAND(SD_DAT2REG->FIOSET, SD_DAT2PIN) = 1;
  656. BITBAND(SD_DAT3REG->FIOSET, SD_DAT3PIN) = 1;
  657. wiggle_fast_pos1();
  658. BITBAND(SD_DAT0REG->FIODIR, SD_DAT0PIN) = 0;
  659. BITBAND(SD_DAT1REG->FIODIR, SD_DAT1PIN) = 0;
  660. BITBAND(SD_DAT2REG->FIODIR, SD_DAT2PIN) = 0;
  661. BITBAND(SD_DAT3REG->FIODIR, SD_DAT3PIN) = 0;
  662. wiggle_fast_neg(3);
  663. dat0=0;
  664. datshift=4;
  665. do {
  666. datshift--;
  667. dat0 |= ((BITBAND(SD_DAT0REG->FIOPIN, SD_DAT0PIN)) << datshift);
  668. wiggle_fast_neg1();
  669. } while (datshift);
  670. DBG_SD printf("crc %02x\n", dat0);
  671. if((dat0 & 7) != 2) {
  672. printf("crc error! %02x\n", dat0);
  673. while(1);
  674. }
  675. if(dat0 & 8) {
  676. printf("missing start bit in CRC status response...\n");
  677. }
  678. wiggle_fast_neg(2);
  679. wait_busy();
  680. }
  681. void read_block(uint32_t address, uint8_t *buf) {
  682. if(during_blocktrans == TRANS_READ && (last_block == address-1)) {
  683. //uart_putc('r');
  684. #ifdef CONFIG_SD_DATACRC
  685. int cmd_res;
  686. if((cmd_res = stream_datablock(buf)) == CRC_ERROR) {
  687. while(cmd_res == CRC_ERROR) {
  688. cmd_fast(STOP_TRANSMISSION, 0, 0x61, NULL, rsp);
  689. cmd_res = cmd_fast(READ_MULTIPLE_BLOCK, address, 0, buf, rsp);
  690. }
  691. }
  692. #else
  693. stream_datablock(buf);
  694. #endif
  695. last_block=address;
  696. } else {
  697. if(during_blocktrans) {
  698. // uart_putc('_');
  699. //printf("nonseq read (%lx -> %lx), restarting transmission\n", last_block, address);
  700. /* send STOP_TRANSMISSION to end an open READ/WRITE_MULTIPLE_BLOCK */
  701. cmd_fast(STOP_TRANSMISSION, 0, 0x61, NULL, rsp);
  702. }
  703. last_block=address;
  704. if(!ccs) {
  705. address <<= 9;
  706. }
  707. #ifdef CONFIG_SD_DATACRC
  708. while(1) {
  709. if(cmd_fast(READ_MULTIPLE_BLOCK, address, 0, buf, rsp) != CRC_ERROR) break;
  710. cmd_fast(STOP_TRANSMISSION, 0, 0x61, NULL, rsp);
  711. };
  712. #else
  713. cmd_fast(READ_MULTIPLE_BLOCK, address, 0, buf, rsp);
  714. #endif
  715. during_blocktrans = TRANS_READ;
  716. }
  717. }
  718. void write_block(uint32_t address, uint8_t* buf) {
  719. if(during_blocktrans == TRANS_WRITE && (last_block == address-1)) {
  720. wait_busy();
  721. send_datablock(buf);
  722. last_block=address;
  723. } else {
  724. if(during_blocktrans) {
  725. /* send STOP_TRANSMISSION to end an open READ/WRITE_MULTIPLE_BLOCK */
  726. cmd_fast(STOP_TRANSMISSION, 0, 0x61, NULL, rsp);
  727. }
  728. wait_busy();
  729. last_block=address;
  730. if(!ccs) {
  731. address <<= 9;
  732. }
  733. /* only send cmd & get response */
  734. cmd_fast(WRITE_MULTIPLE_BLOCK, address, 0, NULL, rsp);
  735. DBG_SD printf("write_block: CMD25 response = %02x%02x%02x%02x%02x%02x\n", rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5]);
  736. wiggle_fast_pos(8);
  737. send_datablock(buf);
  738. during_blocktrans = TRANS_WRITE;
  739. }
  740. }
  741. //
  742. // Public functions
  743. //
  744. DRESULT sdn_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) {
  745. uint8_t sec;
  746. if(drv >= MAX_CARDS) {
  747. return RES_PARERR;
  748. }
  749. readled(1);
  750. for(sec=0; sec<count; sec++) {
  751. read_block(sector+sec, buffer);
  752. buffer+=512;
  753. }
  754. readled(0);
  755. return RES_OK;
  756. }
  757. DRESULT disk_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) __attribute__ ((weak, alias("sdn_read")));
  758. DRESULT sdn_initialize(BYTE drv) {
  759. uint8_t rsp[17]; /* space for response */
  760. int rsplen;
  761. uint8_t hcs=0;
  762. rca = 0;
  763. if(drv>=MAX_CARDS) {
  764. return STA_NOINIT|STA_NODISK;
  765. }
  766. if(sdn_status(drv) & STA_NODISK) {
  767. return STA_NOINIT|STA_NODISK;
  768. }
  769. /* if the card is sending data from before a reset we try to deselect it
  770. prior to initialization */
  771. for(rsplen=0; rsplen<2042; rsplen++) {
  772. if(!(BITBAND(SD_DAT3REG->FIOPIN, SD_DAT3PIN))) {
  773. printf("card seems to be sending data, attempting deselect\n");
  774. cmd_slow(SELECT_CARD, 0, 0, NULL, rsp);
  775. }
  776. wiggle_slow_neg(1);
  777. }
  778. printf("sd_init start\n");
  779. cmd_slow(GO_IDLE_STATE, 0, 0x95, NULL, rsp);
  780. if((rsplen=cmd_slow(SEND_IF_COND, 0x000001aa, 0x87, NULL, rsp))) {
  781. DBG_SD printf("CMD8 response:\n");
  782. DBG_SD uart_trace(rsp, 0, rsplen);
  783. hcs=1;
  784. }
  785. while(1) {
  786. if(!(acmd_slow(SD_SEND_OP_COND, (hcs << 30) | 0xfc0000, 0, NULL, rsp))) {
  787. printf("ACMD41 no response!\n");
  788. }
  789. if(rsp[1]&0x80) break;
  790. }
  791. ccs = (rsp[1]>>6) & 1; /* SDHC/XC */
  792. cmd_slow(ALL_SEND_CID, 0, 0x4d, NULL, rsp);
  793. if(cmd_slow(SEND_RELATIVE_ADDR, 0, 0x21, NULL, rsp)) {
  794. rca=(rsp[1]<<24) | (rsp[2]<<16);
  795. printf("RCA: %04lx\n", rca>>16);
  796. } else {
  797. printf("CMD3 no response!\n");
  798. rca=0;
  799. }
  800. /* record CSD for getinfo */
  801. cmd_slow(SEND_CSD, rca, 0, NULL, rsp);
  802. /* select the card */
  803. if(cmd_slow(SELECT_CARD, rca, 0, NULL, rsp)) {
  804. printf("card selected!\n");
  805. } else {
  806. printf("CMD7 no response!\n");
  807. }
  808. /* get card status */
  809. cmd_slow(SEND_STATUS, rca, 0, NULL, rsp);
  810. /* set bus width */
  811. acmd_slow(SD_SET_BUS_WIDTH, 0x2, 0, NULL, rsp);
  812. /* set block length */
  813. cmd_slow(SET_BLOCKLEN, 0x200, 0, NULL, rsp);
  814. printf("SD init complete. SDHC/XC=%d\n", ccs);
  815. disk_state = DISK_OK;
  816. during_blocktrans = TRANS_NONE;
  817. return sdn_status(drv);
  818. }
  819. DSTATUS disk_initialize(BYTE drv) __attribute__ ((weak, alias("sdn_initialize")));
  820. void sdn_init(void) {
  821. /* enable GPIO interrupt on SD detect pin, both edges */
  822. /* NVIC_EnableIRQ(EINT3_IRQn);
  823. SD_DT_INT_SETUP(); */
  824. /* disconnect SSP1 */
  825. LPC_PINCON->PINSEL0 &= ~(BV(13) | BV(15) | BV(17) | BV(19));
  826. /* prepare GPIOs */
  827. BITBAND(SD_DAT3REG->FIODIR, SD_DAT3PIN) = 0;
  828. BITBAND(SD_DAT2REG->FIODIR, SD_DAT2PIN) = 0;
  829. BITBAND(SD_DAT1REG->FIODIR, SD_DAT1PIN) = 0;
  830. BITBAND(SD_DAT0REG->FIODIR, SD_DAT0PIN) = 0;
  831. BITBAND(SD_CLKREG->FIODIR, SD_CLKPIN) = 1;
  832. BITBAND(SD_CMDREG->FIODIR, SD_CMDPIN) = 1;
  833. BITBAND(SD_CMDREG->FIOPIN, SD_CMDPIN) = 1;
  834. LPC_GPIO2->FIOPIN0 = 0x00;
  835. LPC_GPIO2->FIOMASK0 = ~0xf;
  836. }
  837. void disk_init(void) __attribute__ ((weak, alias("sdn_init")));
  838. DSTATUS sdn_status(BYTE drv) {
  839. if (SDCARD_DETECT) {
  840. if (SDCARD_WP) {
  841. return STA_PROTECT;
  842. } else {
  843. return RES_OK;
  844. }
  845. } else {
  846. return STA_NOINIT|STA_NODISK;
  847. }
  848. }
  849. DSTATUS disk_status(BYTE drv) __attribute__ ((weak, alias("sdn_status")));
  850. DRESULT sdn_getinfo(BYTE drv, BYTE page, void *buffer) {
  851. uint32_t capacity;
  852. if (drv >= MAX_CARDS) {
  853. return RES_NOTRDY;
  854. }
  855. if (sdn_status(drv) & STA_NODISK) {
  856. return RES_NOTRDY;
  857. }
  858. if (page != 0) {
  859. return RES_ERROR;
  860. }
  861. if (ccs) {
  862. /* Special CSD for SDHC cards */
  863. capacity = (1 + getbits(csd,127-69+8,22)) * 1024;
  864. } else {
  865. /* Assume that MMC-CSD 1.0/1.1/1.2 and SD-CSD 1.1 are the same... */
  866. uint8_t exponent = 2 + getbits(csd, 127-49+8, 3);
  867. capacity = 1 + getbits(csd, 127-73+8, 12);
  868. exponent += getbits(csd, 127-83+8,4) - 9;
  869. while (exponent--) capacity *= 2;
  870. }
  871. diskinfo0_t *di = buffer;
  872. di->validbytes = sizeof(diskinfo0_t);
  873. di->disktype = DISK_TYPE_SD;
  874. di->sectorsize = 2;
  875. di->sectorcount = capacity;
  876. printf("card capacity: %lu sectors\n", capacity);
  877. return RES_OK;
  878. }
  879. DRESULT disk_getinfo(BYTE drv, BYTE page, void *buffer) __attribute__ ((weak, alias("sdn_getinfo")));
  880. DRESULT sdn_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) {
  881. uint8_t sec;
  882. uint8_t *buf = (uint8_t*)buffer;
  883. if(drv >= MAX_CARDS) {
  884. return RES_NOTRDY;
  885. }
  886. if (sdn_status(drv) & STA_NODISK) {
  887. return RES_NOTRDY;
  888. }
  889. writeled(1);
  890. for(sec=0; sec<count; sec++) {
  891. write_block(sector+sec, buf);
  892. buf+=512;
  893. }
  894. writeled(0);
  895. return RES_OK;
  896. }
  897. DRESULT disk_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) __attribute__ ((weak, alias("sdn_write")));
  898. /* Detect changes of SD card 0 */
  899. void sdn_changed() {
  900. if (sd_changed) {
  901. printf("ch ");
  902. if(SDCARD_DETECT) {
  903. disk_state = DISK_CHANGED;
  904. } else {
  905. disk_state = DISK_REMOVED;
  906. }
  907. sd_changed = 0;
  908. }
  909. }