sd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "gpio.h"
  26. #include "uart.h"
  27. #include "delays.h"
  28. #include "sd.h"
  29. #define EMMC_ARG2 ((volatile unsigned int*)(MMIO_BASE+0x00300000))
  30. #define EMMC_BLKSIZECNT ((volatile unsigned int*)(MMIO_BASE+0x00300004))
  31. #define EMMC_ARG1 ((volatile unsigned int*)(MMIO_BASE+0x00300008))
  32. #define EMMC_CMDTM ((volatile unsigned int*)(MMIO_BASE+0x0030000C))
  33. #define EMMC_RESP0 ((volatile unsigned int*)(MMIO_BASE+0x00300010))
  34. #define EMMC_RESP1 ((volatile unsigned int*)(MMIO_BASE+0x00300014))
  35. #define EMMC_RESP2 ((volatile unsigned int*)(MMIO_BASE+0x00300018))
  36. #define EMMC_RESP3 ((volatile unsigned int*)(MMIO_BASE+0x0030001C))
  37. #define EMMC_DATA ((volatile unsigned int*)(MMIO_BASE+0x00300020))
  38. #define EMMC_STATUS ((volatile unsigned int*)(MMIO_BASE+0x00300024))
  39. #define EMMC_CONTROL0 ((volatile unsigned int*)(MMIO_BASE+0x00300028))
  40. #define EMMC_CONTROL1 ((volatile unsigned int*)(MMIO_BASE+0x0030002C))
  41. #define EMMC_INTERRUPT ((volatile unsigned int*)(MMIO_BASE+0x00300030))
  42. #define EMMC_INT_MASK ((volatile unsigned int*)(MMIO_BASE+0x00300034))
  43. #define EMMC_INT_EN ((volatile unsigned int*)(MMIO_BASE+0x00300038))
  44. #define EMMC_CONTROL2 ((volatile unsigned int*)(MMIO_BASE+0x0030003C))
  45. #define EMMC_SLOTISR_VER ((volatile unsigned int*)(MMIO_BASE+0x003000FC))
  46. // command flags
  47. #define CMD_NEED_APP 0x80000000
  48. #define CMD_RSPNS_48 0x00020000
  49. #define CMD_ERRORS_MASK 0xfff9c004
  50. #define CMD_RCA_MASK 0xffff0000
  51. // COMMANDs
  52. #define CMD_GO_IDLE 0x00000000
  53. #define CMD_ALL_SEND_CID 0x02010000
  54. #define CMD_SEND_REL_ADDR 0x03020000
  55. #define CMD_CARD_SELECT 0x07030000
  56. #define CMD_SEND_IF_COND 0x08020000
  57. #define CMD_STOP_TRANS 0x0C030000
  58. #define CMD_READ_SINGLE 0x11220010
  59. #define CMD_READ_MULTI 0x12220032
  60. #define CMD_SET_BLOCKCNT 0x17020000
  61. #define CMD_WRITE_SINGLE 0x18220000
  62. #define CMD_WRITE_MULTI 0x19220022
  63. #define CMD_APP_CMD 0x37000000
  64. #define CMD_SET_BUS_WIDTH (0x06020000|CMD_NEED_APP)
  65. #define CMD_SEND_OP_COND (0x29020000|CMD_NEED_APP)
  66. #define CMD_SEND_SCR (0x33220010|CMD_NEED_APP)
  67. // STATUS register settings
  68. #define SR_READ_AVAILABLE 0x00000800
  69. #define SR_WRITE_AVAILABLE 0x00000400
  70. #define SR_DAT_INHIBIT 0x00000002
  71. #define SR_CMD_INHIBIT 0x00000001
  72. #define SR_APP_CMD 0x00000020
  73. // INTERRUPT register settings
  74. #define INT_DATA_TIMEOUT 0x00100000
  75. #define INT_CMD_TIMEOUT 0x00010000
  76. #define INT_READ_RDY 0x00000020
  77. #define INT_WRITE_RDY 0x00000010
  78. #define INT_DATA_DONE 0x00000002
  79. #define INT_CMD_DONE 0x00000001
  80. #define INT_ERROR_MASK 0x017E8000
  81. // CONTROL register settings
  82. #define C0_SPI_MODE_EN 0x00100000
  83. #define C0_HCTL_HS_EN 0x00000004
  84. #define C0_HCTL_DWITDH 0x00000002
  85. #define C1_SRST_DATA 0x04000000
  86. #define C1_SRST_CMD 0x02000000
  87. #define C1_SRST_HC 0x01000000
  88. #define C1_TOUNIT_DIS 0x000f0000
  89. #define C1_TOUNIT_MAX 0x000e0000
  90. #define C1_CLK_GENSEL 0x00000020
  91. #define C1_CLK_EN 0x00000004
  92. #define C1_CLK_STABLE 0x00000002
  93. #define C1_CLK_INTLEN 0x00000001
  94. // SLOTISR_VER values
  95. #define HOST_SPEC_NUM 0x00ff0000
  96. #define HOST_SPEC_NUM_SHIFT 16
  97. #define HOST_SPEC_V3 2
  98. #define HOST_SPEC_V2 1
  99. #define HOST_SPEC_V1 0
  100. // SCR flags
  101. #define SCR_SD_BUS_WIDTH_4 0x00000400
  102. #define SCR_SUPP_SET_BLKCNT 0x02000000
  103. // added by my driver
  104. #define SCR_SUPP_CCS 0x00000001
  105. #define ACMD41_VOLTAGE 0x00ff8000
  106. #define ACMD41_CMD_COMPLETE 0x80000000
  107. #define ACMD41_CMD_CCS 0x40000000
  108. #define ACMD41_ARG_HC 0x51ff8000
  109. unsigned long sd_scr[2], sd_ocr, sd_rca, sd_err, sd_hv;
  110. /**
  111. * Wait for data or command ready
  112. */
  113. int sd_status(unsigned int mask)
  114. {
  115. int cnt = 1000000; while((*EMMC_STATUS & mask) && !(*EMMC_INTERRUPT & INT_ERROR_MASK) && cnt--) wait_msec(1);
  116. return (cnt <= 0 || (*EMMC_INTERRUPT & INT_ERROR_MASK)) ? SD_ERROR : SD_OK;
  117. }
  118. /**
  119. * Wait for interrupt
  120. */
  121. int sd_int(unsigned int mask)
  122. {
  123. unsigned int r, m=mask | INT_ERROR_MASK;
  124. int cnt = 1000000; while(!(*EMMC_INTERRUPT & m) && cnt--) wait_msec(1);
  125. r=*EMMC_INTERRUPT;
  126. if(cnt<=0 || (r & INT_CMD_TIMEOUT) || (r & INT_DATA_TIMEOUT) ) { *EMMC_INTERRUPT=r; return SD_TIMEOUT; } else
  127. if(r & INT_ERROR_MASK) { *EMMC_INTERRUPT=r; return SD_ERROR; }
  128. *EMMC_INTERRUPT=mask;
  129. return 0;
  130. }
  131. /**
  132. * Send a command
  133. */
  134. int sd_cmd(unsigned int code, unsigned int arg)
  135. {
  136. int r=0;
  137. sd_err=SD_OK;
  138. if(code&CMD_NEED_APP) {
  139. r=sd_cmd(CMD_APP_CMD|(sd_rca?CMD_RSPNS_48:0),sd_rca);
  140. if(sd_rca && !r) { uart_puts("ERROR: failed to send SD APP command\n"); sd_err=SD_ERROR;return 0;}
  141. code &= ~CMD_NEED_APP;
  142. }
  143. if(sd_status(SR_CMD_INHIBIT)) { uart_puts("ERROR: EMMC busy\n"); sd_err= SD_TIMEOUT;return 0;}
  144. uart_puts("EMMC: Sending command ");uart_hex(code);uart_puts(" arg ");uart_hex(arg);uart_puts("\n");
  145. *EMMC_INTERRUPT=*EMMC_INTERRUPT; *EMMC_ARG1=arg; *EMMC_CMDTM=code;
  146. if(code==CMD_SEND_OP_COND) wait_msec(1000); else
  147. if(code==CMD_SEND_IF_COND || code==CMD_APP_CMD) wait_msec(100);
  148. if((r=sd_int(INT_CMD_DONE))) {uart_puts("ERROR: failed to send EMMC command\n");sd_err=r;return 0;}
  149. r=*EMMC_RESP0;
  150. if(code==CMD_GO_IDLE || code==CMD_APP_CMD) return 0; else
  151. if(code==(CMD_APP_CMD|CMD_RSPNS_48)) return r&SR_APP_CMD; else
  152. if(code==CMD_SEND_OP_COND) return r; else
  153. if(code==CMD_SEND_IF_COND) return r==arg? SD_OK : SD_ERROR; else
  154. if(code==CMD_ALL_SEND_CID) {r|=*EMMC_RESP3; r|=*EMMC_RESP2; r|=*EMMC_RESP1; return r; } else
  155. if(code==CMD_SEND_REL_ADDR) {
  156. sd_err=(((r&0x1fff))|((r&0x2000)<<6)|((r&0x4000)<<8)|((r&0x8000)<<8))&CMD_ERRORS_MASK;
  157. return r&CMD_RCA_MASK;
  158. }
  159. return r&CMD_ERRORS_MASK;
  160. // make gcc happy
  161. return 0;
  162. }
  163. /**
  164. * read a block from sd card and return the number of bytes read
  165. * returns 0 on error.
  166. */
  167. int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num)
  168. {
  169. int r,c=0,d;
  170. if(num<1) num=1;
  171. uart_puts("sd_readblock lba ");uart_hex(lba);uart_puts(" num ");uart_hex(num);uart_puts("\n");
  172. if(sd_status(SR_DAT_INHIBIT)) {sd_err=SD_TIMEOUT; return 0;}
  173. unsigned int *buf=(unsigned int *)buffer;
  174. if(sd_scr[0] & SCR_SUPP_CCS) {
  175. if(num > 1 && (sd_scr[0] & SCR_SUPP_SET_BLKCNT)) {
  176. sd_cmd(CMD_SET_BLOCKCNT,num);
  177. if(sd_err) return 0;
  178. }
  179. *EMMC_BLKSIZECNT = (num << 16) | 512;
  180. sd_cmd(num == 1 ? CMD_READ_SINGLE : CMD_READ_MULTI,lba);
  181. if(sd_err) return 0;
  182. } else {
  183. *EMMC_BLKSIZECNT = (1 << 16) | 512;
  184. }
  185. while( c < num ) {
  186. if(!(sd_scr[0] & SCR_SUPP_CCS)) {
  187. sd_cmd(CMD_READ_SINGLE,(lba+c)*512);
  188. if(sd_err) return 0;
  189. }
  190. if((r=sd_int(INT_READ_RDY))){uart_puts("\rERROR: Timeout waiting for ready to read\n");sd_err=r;return 0;}
  191. for(d=0;d<128;d++) buf[d] = *EMMC_DATA;
  192. c++; buf+=128;
  193. }
  194. if( num > 1 && !(sd_scr[0] & SCR_SUPP_SET_BLKCNT) && (sd_scr[0] & SCR_SUPP_CCS)) sd_cmd(CMD_STOP_TRANS,0);
  195. return sd_err!=SD_OK || c!=num? 0 : num*512;
  196. }
  197. /**
  198. * write a block to the sd card and return the number of bytes written
  199. * returns 0 on error.
  200. */
  201. int sd_writeblock(unsigned char *buffer, unsigned int lba, unsigned int num)
  202. {
  203. int r,c=0,d;
  204. if(num<1) num=1;
  205. uart_puts("sd_writeblock lba ");uart_hex(lba);uart_puts(" num ");uart_hex(num);uart_puts("\n");
  206. if(sd_status(SR_DAT_INHIBIT | SR_WRITE_AVAILABLE)) {sd_err=SD_TIMEOUT; return 0;}
  207. unsigned int *buf=(unsigned int *)buffer;
  208. if(sd_scr[0] & SCR_SUPP_CCS) {
  209. if(num > 1 && (sd_scr[0] & SCR_SUPP_SET_BLKCNT)) {
  210. sd_cmd(CMD_SET_BLOCKCNT,num);
  211. if(sd_err) return 0;
  212. }
  213. *EMMC_BLKSIZECNT = (num << 16) | 512;
  214. sd_cmd(num == 1 ? CMD_WRITE_SINGLE : CMD_WRITE_MULTI,lba);
  215. if(sd_err) return 0;
  216. } else {
  217. *EMMC_BLKSIZECNT = (1 << 16) | 512;
  218. }
  219. while( c < num ) {
  220. if(!(sd_scr[0] & SCR_SUPP_CCS)) {
  221. sd_cmd(CMD_WRITE_SINGLE,(lba+c)*512);
  222. if(sd_err) return 0;
  223. }
  224. if((r=sd_int(INT_WRITE_RDY))){uart_puts("\rERROR: Timeout waiting for ready to write\n");sd_err=r;return 0;}
  225. for(d=0;d<128;d++) *EMMC_DATA = buf[d];
  226. c++; buf+=128;
  227. }
  228. if((r=sd_int(INT_DATA_DONE))){uart_puts("\rERROR: Timeout waiting for data done\n");sd_err=r;return 0;}
  229. if( num > 1 && !(sd_scr[0] & SCR_SUPP_SET_BLKCNT) && (sd_scr[0] & SCR_SUPP_CCS)) sd_cmd(CMD_STOP_TRANS,0);
  230. return sd_err!=SD_OK || c!=num? 0 : num*512;
  231. }
  232. /**
  233. * set SD clock to frequency in Hz
  234. */
  235. int sd_clk(unsigned int f)
  236. {
  237. unsigned int d,c=41666666/f,x,s=32,h=0;
  238. int cnt = 100000;
  239. while((*EMMC_STATUS & (SR_CMD_INHIBIT|SR_DAT_INHIBIT)) && cnt--) wait_msec(1);
  240. if(cnt<=0) {
  241. uart_puts("ERROR: timeout waiting for inhibit flag\n");
  242. return SD_ERROR;
  243. }
  244. *EMMC_CONTROL1 &= ~C1_CLK_EN; wait_msec(10);
  245. x=c-1; if(!x) s=0; else {
  246. if(!(x & 0xffff0000u)) { x <<= 16; s -= 16; }
  247. if(!(x & 0xff000000u)) { x <<= 8; s -= 8; }
  248. if(!(x & 0xf0000000u)) { x <<= 4; s -= 4; }
  249. if(!(x & 0xc0000000u)) { x <<= 2; s -= 2; }
  250. if(!(x & 0x80000000u)) { x <<= 1; s -= 1; }
  251. if(s>0) s--;
  252. if(s>7) s=7;
  253. }
  254. if(sd_hv>HOST_SPEC_V2) d=c; else d=(1<<s);
  255. if(d<=2) {d=2;s=0;}
  256. uart_puts("sd_clk divisor ");uart_hex(d);uart_puts(", shift ");uart_hex(s);uart_puts("\n");
  257. if(sd_hv>HOST_SPEC_V2) h=(d&0x300)>>2;
  258. d=(((d&0x0ff)<<8)|h);
  259. *EMMC_CONTROL1=(*EMMC_CONTROL1&0xffff003f)|d; wait_msec(10);
  260. *EMMC_CONTROL1 |= C1_CLK_EN; wait_msec(10);
  261. cnt=10000; while(!(*EMMC_CONTROL1 & C1_CLK_STABLE) && cnt--) wait_msec(10);
  262. if(cnt<=0) {
  263. uart_puts("ERROR: failed to get stable clock\n");
  264. return SD_ERROR;
  265. }
  266. return SD_OK;
  267. }
  268. /**
  269. * initialize EMMC to read SDHC card
  270. */
  271. int sd_init()
  272. {
  273. long r,cnt,ccs=0;
  274. // GPIO_CD
  275. r=*GPFSEL4; r&=~(7<<(7*3)); *GPFSEL4=r;
  276. *GPPUD=2; wait_cycles(150); *GPPUDCLK1=(1<<15); wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  277. r=*GPHEN1; r|=1<<15; *GPHEN1=r;
  278. // GPIO_CLK, GPIO_CMD
  279. r=*GPFSEL4; r|=(7<<(8*3))|(7<<(9*3)); *GPFSEL4=r;
  280. *GPPUD=2; wait_cycles(150); *GPPUDCLK1=(1<<16)|(1<<17); wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  281. // GPIO_DAT0, GPIO_DAT1, GPIO_DAT2, GPIO_DAT3
  282. r=*GPFSEL5; r|=(7<<(0*3)) | (7<<(1*3)) | (7<<(2*3)) | (7<<(3*3)); *GPFSEL5=r;
  283. *GPPUD=2; wait_cycles(150);
  284. *GPPUDCLK1=(1<<18) | (1<<19) | (1<<20) | (1<<21);
  285. wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  286. sd_hv = (*EMMC_SLOTISR_VER & HOST_SPEC_NUM) >> HOST_SPEC_NUM_SHIFT;
  287. uart_puts("EMMC: GPIO set up\n");
  288. // Reset the card.
  289. *EMMC_CONTROL0 = 0; *EMMC_CONTROL1 |= C1_SRST_HC;
  290. cnt=10000; do{wait_msec(10);} while( (*EMMC_CONTROL1 & C1_SRST_HC) && cnt-- );
  291. if(cnt<=0) {
  292. uart_puts("ERROR: failed to reset EMMC\n");
  293. return SD_ERROR;
  294. }
  295. uart_puts("EMMC: reset OK\n");
  296. *EMMC_CONTROL1 |= C1_CLK_INTLEN | C1_TOUNIT_MAX;
  297. wait_msec(10);
  298. // Set clock to setup frequency.
  299. if((r=sd_clk(400000))) return r;
  300. *EMMC_INT_EN = 0xffffffff;
  301. *EMMC_INT_MASK = 0xffffffff;
  302. sd_scr[0]=sd_scr[1]=sd_rca=sd_err=0;
  303. sd_cmd(CMD_GO_IDLE,0);
  304. if(sd_err) return sd_err;
  305. sd_cmd(CMD_SEND_IF_COND,0x000001AA);
  306. if(sd_err) return sd_err;
  307. cnt=6; r=0; while(!(r&ACMD41_CMD_COMPLETE) && cnt--) {
  308. wait_cycles(400);
  309. r=sd_cmd(CMD_SEND_OP_COND,ACMD41_ARG_HC);
  310. uart_puts("EMMC: CMD_SEND_OP_COND returned ");
  311. if(r&ACMD41_CMD_COMPLETE)
  312. uart_puts("COMPLETE ");
  313. if(r&ACMD41_VOLTAGE)
  314. uart_puts("VOLTAGE ");
  315. if(r&ACMD41_CMD_CCS)
  316. uart_puts("CCS ");
  317. uart_hex(r>>32);
  318. uart_hex(r);
  319. uart_puts("\n");
  320. if(sd_err!=SD_TIMEOUT && sd_err!=SD_OK ) {
  321. uart_puts("ERROR: EMMC ACMD41 returned error\n");
  322. return sd_err;
  323. }
  324. }
  325. if(!(r&ACMD41_CMD_COMPLETE) || !cnt ) return SD_TIMEOUT;
  326. if(!(r&ACMD41_VOLTAGE)) return SD_ERROR;
  327. if(r&ACMD41_CMD_CCS) ccs=SCR_SUPP_CCS;
  328. sd_cmd(CMD_ALL_SEND_CID,0);
  329. sd_rca = sd_cmd(CMD_SEND_REL_ADDR,0);
  330. uart_puts("EMMC: CMD_SEND_REL_ADDR returned ");
  331. uart_hex(sd_rca>>32);
  332. uart_hex(sd_rca);
  333. uart_puts("\n");
  334. if(sd_err) return sd_err;
  335. if((r=sd_clk(25000000))) return r;
  336. sd_cmd(CMD_CARD_SELECT,sd_rca);
  337. if(sd_err) return sd_err;
  338. if(sd_status(SR_DAT_INHIBIT)) return SD_TIMEOUT;
  339. *EMMC_BLKSIZECNT = (1<<16) | 8;
  340. sd_cmd(CMD_SEND_SCR,0);
  341. if(sd_err) return sd_err;
  342. if(sd_int(INT_READ_RDY)) return SD_TIMEOUT;
  343. r=0; cnt=100000; while(r<2 && cnt) {
  344. if( *EMMC_STATUS & SR_READ_AVAILABLE )
  345. sd_scr[r++] = *EMMC_DATA;
  346. else
  347. wait_msec(1);
  348. }
  349. if(r!=2) return SD_TIMEOUT;
  350. if(sd_scr[0] & SCR_SD_BUS_WIDTH_4) {
  351. sd_cmd(CMD_SET_BUS_WIDTH,sd_rca|2);
  352. if(sd_err) return sd_err;
  353. *EMMC_CONTROL0 |= C0_HCTL_DWITDH;
  354. }
  355. // add software flag
  356. uart_puts("EMMC: supports ");
  357. if(sd_scr[0] & SCR_SUPP_SET_BLKCNT)
  358. uart_puts("SET_BLKCNT ");
  359. if(ccs)
  360. uart_puts("CCS ");
  361. uart_puts("\n");
  362. sd_scr[0]&=~SCR_SUPP_CCS;
  363. sd_scr[0]|=ccs;
  364. return SD_OK;
  365. }