sdnative.c 31 KB

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