sd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_APP_CMD 0x37000000
  62. #define CMD_SET_BUS_WIDTH (0x06020000|CMD_NEED_APP)
  63. #define CMD_SEND_OP_COND (0x29020000|CMD_NEED_APP)
  64. #define CMD_SEND_SCR (0x33220010|CMD_NEED_APP)
  65. // STATUS register settings
  66. #define SR_READ_AVAILABLE 0x00000800
  67. #define SR_DAT_INHIBIT 0x00000002
  68. #define SR_CMD_INHIBIT 0x00000001
  69. #define SR_APP_CMD 0x00000020
  70. // INTERRUPT register settings
  71. #define INT_DATA_TIMEOUT 0x00100000
  72. #define INT_CMD_TIMEOUT 0x00010000
  73. #define INT_READ_RDY 0x00000020
  74. #define INT_CMD_DONE 0x00000001
  75. #define INT_ERROR_MASK 0x017E8000
  76. // CONTROL register settings
  77. #define C0_SPI_MODE_EN 0x00100000
  78. #define C0_HCTL_HS_EN 0x00000004
  79. #define C0_HCTL_DWITDH 0x00000002
  80. #define C1_SRST_DATA 0x04000000
  81. #define C1_SRST_CMD 0x02000000
  82. #define C1_SRST_HC 0x01000000
  83. #define C1_TOUNIT_DIS 0x000f0000
  84. #define C1_TOUNIT_MAX 0x000e0000
  85. #define C1_CLK_GENSEL 0x00000020
  86. #define C1_CLK_EN 0x00000004
  87. #define C1_CLK_STABLE 0x00000002
  88. #define C1_CLK_INTLEN 0x00000001
  89. // SLOTISR_VER values
  90. #define HOST_SPEC_NUM 0x00ff0000
  91. #define HOST_SPEC_NUM_SHIFT 16
  92. #define HOST_SPEC_V3 2
  93. #define HOST_SPEC_V2 1
  94. #define HOST_SPEC_V1 0
  95. // SCR flags
  96. #define SCR_SD_BUS_WIDTH_4 0x00000400
  97. #define SCR_SUPP_SET_BLKCNT 0x02000000
  98. // added by my driver
  99. #define SCR_SUPP_CCS 0x00000001
  100. #define ACMD41_VOLTAGE 0x00ff8000
  101. #define ACMD41_CMD_COMPLETE 0x80000000
  102. #define ACMD41_CMD_CCS 0x40000000
  103. #define ACMD41_ARG_HC 0x51ff8000
  104. unsigned long sd_scr[2], sd_ocr, sd_rca, sd_err, sd_hv;
  105. /**
  106. * Wait for data or command ready
  107. */
  108. int sd_status(unsigned int mask)
  109. {
  110. int cnt = 500000; while((*EMMC_STATUS & mask) && !(*EMMC_INTERRUPT & INT_ERROR_MASK) && cnt--) wait_msec(1);
  111. return (cnt <= 0 || (*EMMC_INTERRUPT & INT_ERROR_MASK)) ? SD_ERROR : SD_OK;
  112. }
  113. /**
  114. * Wait for interrupt
  115. */
  116. int sd_int(unsigned int mask)
  117. {
  118. unsigned int r, m=mask | INT_ERROR_MASK;
  119. int cnt = 1000000; while(!(*EMMC_INTERRUPT & m) && cnt--) wait_msec(1);
  120. r=*EMMC_INTERRUPT;
  121. if(cnt<=0 || (r & INT_CMD_TIMEOUT) || (r & INT_DATA_TIMEOUT) ) { *EMMC_INTERRUPT=r; return SD_TIMEOUT; } else
  122. if(r & INT_ERROR_MASK) { *EMMC_INTERRUPT=r; return SD_ERROR; }
  123. *EMMC_INTERRUPT=mask;
  124. return 0;
  125. }
  126. /**
  127. * Send a command
  128. */
  129. int sd_cmd(unsigned int code, unsigned int arg)
  130. {
  131. int r=0;
  132. sd_err=SD_OK;
  133. if(code&CMD_NEED_APP) {
  134. r=sd_cmd(CMD_APP_CMD|(sd_rca?CMD_RSPNS_48:0),sd_rca);
  135. if(sd_rca && !r) { uart_puts("ERROR: failed to send SD APP command\n"); sd_err=SD_ERROR;return 0;}
  136. code &= ~CMD_NEED_APP;
  137. }
  138. if(sd_status(SR_CMD_INHIBIT)) { uart_puts("ERROR: EMMC busy\n"); sd_err= SD_TIMEOUT;return 0;}
  139. uart_puts("EMMC: Sending command ");uart_hex(code);uart_puts(" arg ");uart_hex(arg);uart_puts("\n");
  140. *EMMC_INTERRUPT=*EMMC_INTERRUPT; *EMMC_ARG1=arg; *EMMC_CMDTM=code;
  141. if(code==CMD_SEND_OP_COND) wait_msec(1000); else
  142. if(code==CMD_SEND_IF_COND || code==CMD_APP_CMD) wait_msec(100);
  143. if((r=sd_int(INT_CMD_DONE))) {uart_puts("ERROR: failed to send EMMC command\n");sd_err=r;return 0;}
  144. r=*EMMC_RESP0;
  145. if(code==CMD_GO_IDLE || code==CMD_APP_CMD) return 0; else
  146. if(code==(CMD_APP_CMD|CMD_RSPNS_48)) return r&SR_APP_CMD; else
  147. if(code==CMD_SEND_OP_COND) return r; else
  148. if(code==CMD_SEND_IF_COND) return r==arg? SD_OK : SD_ERROR; else
  149. if(code==CMD_ALL_SEND_CID) {r|=*EMMC_RESP3; r|=*EMMC_RESP2; r|=*EMMC_RESP1; return r; } else
  150. if(code==CMD_SEND_REL_ADDR) {
  151. sd_err=(((r&0x1fff))|((r&0x2000)<<6)|((r&0x4000)<<8)|((r&0x8000)<<8))&CMD_ERRORS_MASK;
  152. return r&CMD_RCA_MASK;
  153. }
  154. return r&CMD_ERRORS_MASK;
  155. // make gcc happy
  156. return 0;
  157. }
  158. /**
  159. * read a block from sd card and return the number of bytes read
  160. * returns 0 on error.
  161. */
  162. int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num)
  163. {
  164. int r,c=0,d;
  165. if(num<1) num=1;
  166. uart_puts("sd_readblock lba ");uart_hex(lba);uart_puts(" num ");uart_hex(num);uart_puts("\n");
  167. if(sd_status(SR_DAT_INHIBIT)) {sd_err=SD_TIMEOUT; return 0;}
  168. unsigned int *buf=(unsigned int *)buffer;
  169. if(sd_scr[0] & SCR_SUPP_CCS) {
  170. if(num > 1 && (sd_scr[0] & SCR_SUPP_SET_BLKCNT)) {
  171. sd_cmd(CMD_SET_BLOCKCNT,num);
  172. if(sd_err) return 0;
  173. }
  174. *EMMC_BLKSIZECNT = (num << 16) | 512;
  175. sd_cmd(num == 1 ? CMD_READ_SINGLE : CMD_READ_MULTI,lba);
  176. if(sd_err) return 0;
  177. } else {
  178. *EMMC_BLKSIZECNT = (1 << 16) | 512;
  179. }
  180. while( c < num ) {
  181. if(!(sd_scr[0] & SCR_SUPP_CCS)) {
  182. sd_cmd(CMD_READ_SINGLE,(lba+c)*512);
  183. if(sd_err) return 0;
  184. }
  185. if((r=sd_int(INT_READ_RDY))){uart_puts("\rERROR: Timeout waiting for ready to read\n");sd_err=r;return 0;}
  186. for(d=0;d<128;d++) buf[d] = *EMMC_DATA;
  187. c++; buf+=128;
  188. }
  189. if( num > 1 && !(sd_scr[0] & SCR_SUPP_SET_BLKCNT) && (sd_scr[0] & SCR_SUPP_CCS)) sd_cmd(CMD_STOP_TRANS,0);
  190. return sd_err!=SD_OK || c!=num? 0 : num*512;
  191. }
  192. /**
  193. * set SD clock to frequency in Hz
  194. */
  195. int sd_clk(unsigned int f)
  196. {
  197. unsigned int d,c=41666666/f,x,s=32,h=0;
  198. int cnt = 100000;
  199. while((*EMMC_STATUS & (SR_CMD_INHIBIT|SR_DAT_INHIBIT)) && cnt--) wait_msec(1);
  200. if(cnt<=0) {
  201. uart_puts("ERROR: timeout waiting for inhibit flag\n");
  202. return SD_ERROR;
  203. }
  204. *EMMC_CONTROL1 &= ~C1_CLK_EN; wait_msec(10);
  205. x=c-1; if(!x) s=0; else {
  206. if(!(x & 0xffff0000u)) { x <<= 16; s -= 16; }
  207. if(!(x & 0xff000000u)) { x <<= 8; s -= 8; }
  208. if(!(x & 0xf0000000u)) { x <<= 4; s -= 4; }
  209. if(!(x & 0xc0000000u)) { x <<= 2; s -= 2; }
  210. if(!(x & 0x80000000u)) { x <<= 1; s -= 1; }
  211. if(s>0) s--;
  212. if(s>7) s=7;
  213. }
  214. if(sd_hv>HOST_SPEC_V2) d=c; else d=(1<<s);
  215. if(d<=2) {d=2;s=0;}
  216. uart_puts("sd_clk divisor ");uart_hex(d);uart_puts(", shift ");uart_hex(s);uart_puts("\n");
  217. if(sd_hv>HOST_SPEC_V2) h=(d&0x300)>>2;
  218. d=(((d&0x0ff)<<8)|h);
  219. *EMMC_CONTROL1=(*EMMC_CONTROL1&0xffff003f)|d; wait_msec(10);
  220. *EMMC_CONTROL1 |= C1_CLK_EN; wait_msec(10);
  221. cnt=10000; while(!(*EMMC_CONTROL1 & C1_CLK_STABLE) && cnt--) wait_msec(10);
  222. if(cnt<=0) {
  223. uart_puts("ERROR: failed to get stable clock\n");
  224. return SD_ERROR;
  225. }
  226. return SD_OK;
  227. }
  228. /**
  229. * initialize EMMC to read SDHC card
  230. */
  231. int sd_init()
  232. {
  233. long r,cnt,ccs=0;
  234. // GPIO_CD
  235. r=*GPFSEL4; r&=~(7<<(7*3)); *GPFSEL4=r;
  236. *GPPUD=2; wait_cycles(150); *GPPUDCLK1=(1<<15); wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  237. r=*GPHEN1; r|=1<<15; *GPHEN1=r;
  238. // GPIO_CLK, GPIO_CMD
  239. r=*GPFSEL4; r|=(7<<(8*3))|(7<<(9*3)); *GPFSEL4=r;
  240. *GPPUD=2; wait_cycles(150); *GPPUDCLK1=(1<<16)|(1<<17); wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  241. // GPIO_DAT0, GPIO_DAT1, GPIO_DAT2, GPIO_DAT3
  242. r=*GPFSEL5; r|=(7<<(0*3)) | (7<<(1*3)) | (7<<(2*3)) | (7<<(3*3)); *GPFSEL5=r;
  243. *GPPUD=2; wait_cycles(150);
  244. *GPPUDCLK1=(1<<18) | (1<<19) | (1<<20) | (1<<21);
  245. wait_cycles(150); *GPPUD=0; *GPPUDCLK1=0;
  246. sd_hv = (*EMMC_SLOTISR_VER & HOST_SPEC_NUM) >> HOST_SPEC_NUM_SHIFT;
  247. uart_puts("EMMC: GPIO set up\n");
  248. // Reset the card.
  249. *EMMC_CONTROL0 = 0; *EMMC_CONTROL1 |= C1_SRST_HC;
  250. cnt=10000; do{wait_msec(10);} while( (*EMMC_CONTROL1 & C1_SRST_HC) && cnt-- );
  251. if(cnt<=0) {
  252. uart_puts("ERROR: failed to reset EMMC\n");
  253. return SD_ERROR;
  254. }
  255. uart_puts("EMMC: reset OK\n");
  256. *EMMC_CONTROL1 |= C1_CLK_INTLEN | C1_TOUNIT_MAX;
  257. wait_msec(10);
  258. // Set clock to setup frequency.
  259. if((r=sd_clk(400000))) return r;
  260. *EMMC_INT_EN = 0xffffffff;
  261. *EMMC_INT_MASK = 0xffffffff;
  262. sd_scr[0]=sd_scr[1]=sd_rca=sd_err=0;
  263. sd_cmd(CMD_GO_IDLE,0);
  264. if(sd_err) return sd_err;
  265. sd_cmd(CMD_SEND_IF_COND,0x000001AA);
  266. if(sd_err) return sd_err;
  267. cnt=6; r=0; while(!(r&ACMD41_CMD_COMPLETE) && cnt--) {
  268. wait_cycles(400);
  269. r=sd_cmd(CMD_SEND_OP_COND,ACMD41_ARG_HC);
  270. uart_puts("EMMC: CMD_SEND_OP_COND returned ");
  271. if(r&ACMD41_CMD_COMPLETE)
  272. uart_puts("COMPLETE ");
  273. if(r&ACMD41_VOLTAGE)
  274. uart_puts("VOLTAGE ");
  275. if(r&ACMD41_CMD_CCS)
  276. uart_puts("CCS ");
  277. uart_hex(r>>32);
  278. uart_hex(r);
  279. uart_puts("\n");
  280. if(sd_err!=SD_TIMEOUT && sd_err!=SD_OK ) {
  281. uart_puts("ERROR: EMMC ACMD41 returned error\n");
  282. return sd_err;
  283. }
  284. }
  285. if(!(r&ACMD41_CMD_COMPLETE) || !cnt ) return SD_TIMEOUT;
  286. if(!(r&ACMD41_VOLTAGE)) return SD_ERROR;
  287. if(r&ACMD41_CMD_CCS) ccs=SCR_SUPP_CCS;
  288. sd_cmd(CMD_ALL_SEND_CID,0);
  289. sd_rca = sd_cmd(CMD_SEND_REL_ADDR,0);
  290. uart_puts("EMMC: CMD_SEND_REL_ADDR returned ");
  291. uart_hex(sd_rca>>32);
  292. uart_hex(sd_rca);
  293. uart_puts("\n");
  294. if(sd_err) return sd_err;
  295. if((r=sd_clk(25000000))) return r;
  296. sd_cmd(CMD_CARD_SELECT,sd_rca);
  297. if(sd_err) return sd_err;
  298. if(sd_status(SR_DAT_INHIBIT)) return SD_TIMEOUT;
  299. *EMMC_BLKSIZECNT = (1<<16) | 8;
  300. sd_cmd(CMD_SEND_SCR,0);
  301. if(sd_err) return sd_err;
  302. if(sd_int(INT_READ_RDY)) return SD_TIMEOUT;
  303. r=0; cnt=100000; while(r<2 && cnt) {
  304. if( *EMMC_STATUS & SR_READ_AVAILABLE )
  305. sd_scr[r++] = *EMMC_DATA;
  306. else
  307. wait_msec(1);
  308. }
  309. if(r!=2) return SD_TIMEOUT;
  310. if(sd_scr[0] & SCR_SD_BUS_WIDTH_4) {
  311. sd_cmd(CMD_SET_BUS_WIDTH,sd_rca|2);
  312. if(sd_err) return sd_err;
  313. *EMMC_CONTROL0 |= C0_HCTL_DWITDH;
  314. }
  315. // add software flag
  316. uart_puts("EMMC: supports ");
  317. if(sd_scr[0] & SCR_SUPP_SET_BLKCNT)
  318. uart_puts("SET_BLKCNT ");
  319. if(ccs)
  320. uart_puts("CCS ");
  321. uart_puts("\n");
  322. sd_scr[0]&=~SCR_SUPP_CCS;
  323. sd_scr[0]|=ccs;
  324. return SD_OK;
  325. }