sd.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "sunxi_hal_common.h"
  30. #include "hal_base.h"
  31. #include "sys/endian.h"
  32. #include "hal_sdhost.h"
  33. #include "sdmmc.h"
  34. #include "_sdhost.h"
  35. #include "_core.h"
  36. #ifdef CONFIG_USE_MMC
  37. #include "_mmc.h"
  38. #endif
  39. #include "_sd.h"
  40. #include "_sd_define.h"
  41. #ifdef CONFIG_USE_SD
  42. static const unsigned int tran_exp[] = { /* about KB/S */
  43. 10000, 100000, 1000000, 10000000,
  44. 0, 0, 0, 0
  45. };
  46. static const unsigned char tran_mant[] = { /* time value*10 */
  47. 0, 10, 12, 13, 15, 20, 25, 30,
  48. 35, 40, 45, 50, 55, 60, 70, 80,
  49. };
  50. /*
  51. static const unsigned int tacc_exp[] = {
  52. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  53. };
  54. static const unsigned int tacc_mant[] = {
  55. 0, 10, 12, 13, 15, 20, 25, 30,
  56. 35, 40, 45, 50, 55, 60, 70, 80,
  57. };
  58. */
  59. static int32_t mmc_send_app_op_cond(struct mmc_host *host, uint32_t ocr, uint32_t *rocr)
  60. {
  61. struct mmc_command cmd = {0};
  62. int32_t i, err;
  63. if (!host) {
  64. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  65. return -1;
  66. }
  67. cmd.opcode = SD_APP_OP_COND;
  68. cmd.arg = ocr;
  69. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  70. for (i = 100; i; i--) {
  71. err = mmc_wait_for_app_cmd(host, NULL, &cmd);
  72. if (err) {
  73. // printf("%s,%d\n",__FUNCTION__,__LINE__);
  74. break;
  75. }
  76. /* otherwise wait until reset completes */
  77. if (cmd.resp[0] & MMC_CARD_BUSY){
  78. // printf("%s,%d\n",__FUNCTION__,__LINE__);
  79. break;
  80. }
  81. if (host->ocr_avail & MMC_VDD_165_195)
  82. cmd.arg = 0x41000000 | (cmd.resp[0] & 0xFF8000);
  83. else
  84. cmd.arg = 0x40000000 | (cmd.resp[0] & 0xFF8000);
  85. err = -1;
  86. mmc_mdelay(20);
  87. // printf("%s,%d %d\n",__FUNCTION__,__LINE__,i);
  88. }
  89. if (rocr)
  90. *rocr = cmd.resp[0];
  91. if(err)
  92. printf("%s,%d %ld\n",__FUNCTION__,__LINE__, HAL_PR_SZ_L(i));
  93. return err;
  94. }
  95. int32_t mmc_app_sd_status(struct mmc_card *card, uint8_t *ssr)
  96. {
  97. struct mmc_request mrq;
  98. struct mmc_command cmd = {0};
  99. struct mmc_data data = {0};
  100. struct scatterlist sg;
  101. if (!ssr) {
  102. SD_LOGE("%s,%d err", __func__, __LINE__);
  103. return -1;
  104. }
  105. if (mmc_app_cmd(card->host, card)) {
  106. return -1;
  107. }
  108. mrq.cmd = &cmd;
  109. mrq.data = &data;
  110. cmd.opcode = SD_APP_SD_STATUS;
  111. cmd.arg = 0;
  112. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
  113. data.blksz = 64;
  114. data.blocks = 1;
  115. data.flags = MMC_DATA_READ;
  116. data.sg = &sg;
  117. data.sg_len = 1;
  118. sg.len = 64;
  119. sg.buffer = ssr;
  120. if (mmc_wait_for_req(card->host, &mrq)) {
  121. return -1;
  122. }
  123. SD_LOGN("card raw SD status:\n");
  124. sd_hex_dump_bytes((void *)ssr, 64);
  125. return 0;
  126. }
  127. int32_t mmc_app_send_scr(struct mmc_card *card, uint32_t *raw_scr)
  128. {
  129. struct mmc_command cmd = {0};
  130. struct mmc_data data = {0};
  131. struct mmc_request mrq;
  132. struct scatterlist sg;
  133. if (mmc_app_cmd(card->host, card)) {
  134. return -1;
  135. }
  136. mrq.cmd = &cmd;
  137. mrq.data = &data;
  138. cmd.opcode = SD_APP_SEND_SCR;
  139. cmd.arg = 0;
  140. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  141. data.blksz = 8;
  142. data.blocks = 1;
  143. data.flags = MMC_DATA_READ;
  144. data.sg = &sg;
  145. data.sg_len = 1;
  146. sg.len = 8;
  147. sg.buffer = (void *)raw_scr;
  148. /* get scr, 8 bytes */
  149. if (mmc_wait_for_req(card->host, &mrq)) {
  150. return -1;
  151. }
  152. raw_scr[0] = be32_to_cpu(raw_scr[0]);
  153. raw_scr[1] = be32_to_cpu(raw_scr[1]);
  154. return 0;
  155. }
  156. #ifdef SD_SUPPORT_VERSION3
  157. int32_t mmc_switch_to_1v8(struct mmc_card *card)
  158. {
  159. struct mmc_command cmd = {0};
  160. cmd.opcode = SD_SWITCH_VOLTAGE;
  161. cmd.arg = 0;
  162. cmd.vol_switch = 1;
  163. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  164. if (mmc_wait_for_cmd(card->host, &cmd)) {
  165. SD_LOGW("sd switch 1v8 failed\n");
  166. return -1;
  167. }
  168. return 0;
  169. }
  170. #endif
  171. /*
  172. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  173. */
  174. void mmc_decode_cid(struct mmc_card *card, uint32_t *resp)
  175. {
  176. SDC_Memset(&card->cid, 0, sizeof(struct mmc_cid));
  177. /*
  178. * SD doesn't currently have a version field so we will
  179. * have to assume we can parse this.
  180. */
  181. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  182. card->cid.oemid = UNSTUFF_BITS(resp, 104, 8);
  183. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  184. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  185. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  186. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  187. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  188. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  189. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  190. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  191. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  192. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  193. card->cid.year += 2000; /* SD cards year offset */
  194. }
  195. int32_t mmc_send_cid(struct mmc_card *card)
  196. {
  197. struct mmc_command cmd = {0};
  198. uint32_t cid[4] = {0};
  199. cmd.opcode = MMC_SEND_CID;
  200. cmd.arg = card->rca << 16;
  201. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  202. if (mmc_wait_for_cmd(card->host, &cmd)) {
  203. return -1;
  204. }
  205. HAL_Memcpy((void *)cid, (void *)cmd.resp, 16);
  206. SD_LOGN("card raw cid:\n");
  207. sd_hex_dump_bytes((void *)cid, 16);
  208. mmc_decode_cid(card, cid);
  209. return 0;
  210. }
  211. /*
  212. * Given a 128-bit response, decode to our card CSD structure.
  213. */
  214. static int32_t mmc_decode_csd(struct mmc_card *card, uint32_t *raw_csd)
  215. {
  216. int32_t e, m, csd_struct;
  217. uint32_t *resp = raw_csd;
  218. struct mmc_csd *csd = &card->csd;
  219. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  220. card->csd.csd_ver = csd_struct;
  221. switch (csd_struct) {
  222. case 0:
  223. //m = UNSTUFF_BITS(resp, 115, 4);
  224. //e = UNSTUFF_BITS(resp, 112, 3);
  225. //csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  226. //csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  227. m = UNSTUFF_BITS(resp, 99, 4);
  228. e = UNSTUFF_BITS(resp, 96, 3);
  229. csd->max_dtr = tran_exp[e] * tran_mant[m];
  230. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  231. m = UNSTUFF_BITS(resp, 80, 4);
  232. csd->read_blk_len = 1 << m;
  233. e = UNSTUFF_BITS(resp, 47, 3);
  234. m = UNSTUFF_BITS(resp, 62, 12);
  235. csd->capacity = (1 + m) * (1 << (e + 2)) * csd->read_blk_len;
  236. csd->capacity >>= 10; /* unit:KB */
  237. //csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  238. //csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  239. //csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  240. //csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  241. //csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  242. //csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  243. //csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  244. //if (UNSTUFF_BITS(resp, 46, 1)) {
  245. // csd->erase_size = 1;
  246. //} else if (csd->write_blkbits >= 9) {
  247. // csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
  248. // csd->erase_size <<= csd->write_blkbits - 9;
  249. //}
  250. break;
  251. case 1:
  252. /*
  253. * This is a block-addressed SDHC or SDXC card. Most
  254. * interesting fields are unused and have fixed
  255. * values. To avoid getting tripped by buggy cards,
  256. * we assume those fixed values ourselves.
  257. */
  258. mmc_card_set_blockaddr(card);
  259. //csd->tacc_ns = 0; /* Unused */
  260. //csd->tacc_clks = 0; /* Unused */
  261. m = UNSTUFF_BITS(resp, 99, 4);
  262. e = UNSTUFF_BITS(resp, 96, 3);
  263. csd->max_dtr = tran_exp[e] * tran_mant[m];
  264. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  265. csd->read_blk_len = UNSTUFF_BITS(resp, 80, 4);
  266. m = UNSTUFF_BITS(resp, 48, 22);
  267. csd->capacity = (1 + m) << 9;
  268. /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
  269. if (m >= 0xFFFF)
  270. mmc_card_set_ext_capacity(card);
  271. //csd->read_blkbits = 9;
  272. //csd->read_partial = 0;
  273. //csd->write_misalign = 0;
  274. //csd->read_misalign = 0;
  275. //csd->r2w_factor = 4; /* Unused */
  276. //csd->write_blkbits = 9;
  277. //csd->write_partial = 0;
  278. //csd->erase_size = 1;
  279. break;
  280. default:
  281. SD_LOGE("%s: unrecognised CSD structure version %d\n",
  282. __func__, (unsigned int)csd_struct);
  283. return -1;
  284. }
  285. //card->erase_size = csd->erase_size;
  286. SD_LOGD("%s %d ca:%d\n", __func__, (unsigned int)csd_struct, (unsigned int)csd->capacity);
  287. return 0;
  288. }
  289. #ifdef SD_SUPPORT_VERSION3
  290. static void sd_update_bus_speed_mode(struct mmc_card *card)
  291. {
  292. /*
  293. * If the host doesn't support any of the UHS-I modes, fallback on
  294. * default speed.
  295. */
  296. if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  297. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
  298. card->sd_bus_speed = 0;
  299. return;
  300. }
  301. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  302. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  303. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  304. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  305. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  306. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  307. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  308. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  309. SD_MODE_UHS_SDR50)) {
  310. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  311. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  312. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  313. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  314. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  315. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  316. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  317. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  318. SD_MODE_UHS_SDR12)) {
  319. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  320. }
  321. }
  322. static int32_t sd_set_bus_speed_mode(struct mmc_card *card, int8_t *status)
  323. {
  324. int32_t err;
  325. uint32_t timing = 0;
  326. switch (card->sd_bus_speed) {
  327. case UHS_SDR104_BUS_SPEED:
  328. timing = MMC_TIMING_UHS_SDR104;
  329. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  330. break;
  331. case UHS_DDR50_BUS_SPEED:
  332. timing = MMC_TIMING_UHS_DDR50;
  333. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  334. break;
  335. case UHS_SDR50_BUS_SPEED:
  336. timing = MMC_TIMING_UHS_SDR50;
  337. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  338. break;
  339. case UHS_SDR25_BUS_SPEED:
  340. timing = MMC_TIMING_UHS_SDR25;
  341. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  342. break;
  343. case UHS_SDR12_BUS_SPEED:
  344. timing = MMC_TIMING_UHS_SDR12;
  345. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  346. break;
  347. default:
  348. return 0;
  349. }
  350. err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
  351. if (err)
  352. return err;
  353. if ((status[16] & 0xF) != card->sd_bus_speed)
  354. SD_LOGW("%s: Problem setting bus speed mode!\n", __func__);
  355. else {
  356. mmc_set_timing(card->host, timing);
  357. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  358. }
  359. return 0;
  360. }
  361. static int32_t sd_set_current_limit(struct mmc_card *card, int8_t *status)
  362. {
  363. int32_t current_limit = 0;
  364. int32_t err;
  365. /*
  366. * Current limit switch is only defined for SDR50, SDR104, and DDR50
  367. * bus speed modes. For other bus speed modes, we set the default
  368. * current limit of 200mA.
  369. */
  370. if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
  371. (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
  372. (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
  373. if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
  374. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
  375. current_limit = SD_SET_CURRENT_LIMIT_800;
  376. else if (card->sw_caps.sd3_curr_limit &
  377. SD_MAX_CURRENT_600)
  378. current_limit = SD_SET_CURRENT_LIMIT_600;
  379. else if (card->sw_caps.sd3_curr_limit &
  380. SD_MAX_CURRENT_400)
  381. current_limit = SD_SET_CURRENT_LIMIT_400;
  382. else if (card->sw_caps.sd3_curr_limit &
  383. SD_MAX_CURRENT_200)
  384. current_limit = SD_SET_CURRENT_LIMIT_200;
  385. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
  386. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
  387. current_limit = SD_SET_CURRENT_LIMIT_600;
  388. else if (card->sw_caps.sd3_curr_limit &
  389. SD_MAX_CURRENT_400)
  390. current_limit = SD_SET_CURRENT_LIMIT_400;
  391. else if (card->sw_caps.sd3_curr_limit &
  392. SD_MAX_CURRENT_200)
  393. current_limit = SD_SET_CURRENT_LIMIT_200;
  394. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
  395. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
  396. current_limit = SD_SET_CURRENT_LIMIT_400;
  397. else if (card->sw_caps.sd3_curr_limit &
  398. SD_MAX_CURRENT_200)
  399. current_limit = SD_SET_CURRENT_LIMIT_200;
  400. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
  401. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
  402. current_limit = SD_SET_CURRENT_LIMIT_200;
  403. }
  404. } else
  405. current_limit = SD_SET_CURRENT_LIMIT_200;
  406. err = mmc_sd_switch(card, 1, 3, current_limit, status);
  407. if (err)
  408. return err;
  409. if (((status[15] >> 4) & 0x0F) != current_limit)
  410. SD_LOGW("%s: Problem setting current limit!\n", __func__);
  411. return 0;
  412. }
  413. int32_t mmc_sd_init_uhs_card(struct mmc_card *card)
  414. {
  415. int32_t err;
  416. struct mmc_command cmd = {0};
  417. struct mmc_data data = {0};
  418. struct mmc_request mrq;
  419. struct scatterlist sg = {0};
  420. uint32_t time = 0;
  421. uint8_t pattern[] = {
  422. 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
  423. 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
  424. 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
  425. 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde
  426. };
  427. uint32_t status[64/4] = {0};
  428. if (!card->scr.sda_spec3)
  429. return 0;
  430. if (!(card->csd.cmdclass & CCC_SWITCH))
  431. return 0;
  432. /* Set 4-bit bus width */
  433. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  434. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  435. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  436. if (err)
  437. goto out;
  438. HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
  439. }
  440. /*
  441. * Select the bus speed mode depending on host
  442. * and card capability.
  443. */
  444. sd_update_bus_speed_mode(card);
  445. /* Set the driver strength for the card */
  446. err = sd_select_driver_type(card, status);
  447. if (err)
  448. goto out;
  449. /* Set current limit for the card */
  450. err = sd_set_current_limit(card, status);
  451. if (err)
  452. goto out;
  453. /* Set bus speed mode of the card */
  454. err = sd_set_bus_speed_mode(card, status);
  455. if (err)
  456. goto out;
  457. /* SPI mode doesn't define CMD19 */
  458. mmc_host_clk_hold(card->host);
  459. sg.len = 512;
  460. sg.buffer = status;
  461. cmd.opcode = MMC_SEND_TUNING_PATTERN;
  462. cmd.arg = 0;
  463. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  464. cmd.data = &data;
  465. data.blksz = 64;
  466. data.sg_len = 1;
  467. data.sg = &sg;
  468. data.flags = MMC_DATA_READ;
  469. mrq.cmd = &cmd;
  470. mrq.data = &data;
  471. do {
  472. if (mmc_wait_for_req(card->host, &mrq)) {
  473. continue;
  474. }
  475. time++;
  476. } while (time < 40);
  477. if (HAL_Memcmp((void *)pattern, (void *)status, 64))
  478. return -1;
  479. mmc_host_clk_release(card->host);
  480. out:
  481. return 0;
  482. }
  483. #endif
  484. /*
  485. * Given a 64-bit response, decode to our card SCR structure.
  486. */
  487. static int32_t mmc_decode_scr(struct mmc_card *card, uint32_t *raw_scr)
  488. {
  489. struct sd_scr *scr = &card->scr;
  490. uint32_t scr_struct;
  491. uint32_t resp[4];
  492. resp[3] = raw_scr[1];
  493. resp[2] = raw_scr[0];
  494. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  495. if (scr_struct != 0) {
  496. SD_LOGW("sdc unrecognised SCR structure version %u\n", (unsigned int)scr_struct);
  497. return -1;
  498. }
  499. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  500. scr->security_sup = UNSTUFF_BITS(resp, 52, 3);
  501. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  502. if (scr->sda_vsn == SCR_SPEC_VER_2)
  503. /* Check if Physical Layer Spec v3.0 is supported */
  504. scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
  505. //if (UNSTUFF_BITS(resp, 55, 1))
  506. // card->erased_byte = 0xFF;
  507. //else
  508. // card->erased_byte = 0x0;
  509. if (scr->sda_spec3) {
  510. scr->cmds = UNSTUFF_BITS(resp, 32, 2);
  511. scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
  512. scr->sda_spec5 = UNSTUFF_BITS(resp, 38, 4);
  513. }
  514. return 0;
  515. }
  516. /*
  517. * Fetch and process SD Status register.
  518. */
  519. static int32_t mmc_read_ssr(struct mmc_card *card)
  520. {
  521. int32_t err, i;
  522. uint32_t ssr[64/4];
  523. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  524. SD_LOGW("%s: card lacks mandatory SD Status function.\n", __func__);
  525. return 0;
  526. }
  527. err = mmc_app_sd_status(card, (uint8_t *)ssr);
  528. if (err) {
  529. SD_LOGW("%s: problem reading SD Status register.\n", __func__);
  530. err = 0;
  531. goto out;
  532. }
  533. for (i = 0; i < 16; i++)
  534. ssr[i] = be32_to_cpu(ssr[i]);
  535. #ifdef SD_SUPPORT_ERASE
  536. uint32_t au, es, et, eo;
  537. /*
  538. * UNSTUFF_BITS only works with four u32s so we have to offset the
  539. * bitfield positions accordingly.
  540. */
  541. au = UNSTUFF_BITS(ssr, 428 - 384, 4);
  542. if (au > 0 && au <= 9) {
  543. card->ssr.au = 1 << (au + 4);
  544. es = UNSTUFF_BITS(ssr, 408 - 384, 16);
  545. et = UNSTUFF_BITS(ssr, 402 - 384, 6);
  546. eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
  547. if (es && et) {
  548. card->ssr.erase_timeout = (et * 1000) / es;
  549. card->ssr.erase_offset = eo * 1000;
  550. }
  551. } else {
  552. SD_LOGW("%s: SD Status: Invalid Allocation Unit size.\n", __func__);
  553. }
  554. #endif
  555. card->speed_class = UNSTUFF_BITS(ssr, 440 - 384, 8) * 2;
  556. if (card->speed_class == 8)
  557. card->speed_class = 10;
  558. out:
  559. return err;
  560. }
  561. /*
  562. * Fetches and decodes switch information
  563. */
  564. static int32_t mmc_read_switch(struct mmc_card *card)
  565. {
  566. int32_t err;
  567. uint32_t status[64/sizeof(uint32_t)] = {0};
  568. uint8_t *p_sta = (uint8_t *)status;
  569. if (card->scr.sda_vsn < SCR_SPEC_VER_1) {
  570. SD_LOGN("Card ver. does not support to read switch info!\n");
  571. return 0;
  572. }
  573. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  574. SD_LOGW("card lacks mandatory switch function, performance might suffer.\n");
  575. return 0;
  576. }
  577. /* Find out the supported Bus Speed Modes. */
  578. err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_ACCESS_MODE,
  579. SD_SWITCH_ACCESS_HS, p_sta);
  580. if (err) {
  581. SD_LOGW("%s: problem reading Bus Speed modes.\n", __func__);
  582. goto out;
  583. }
  584. if (p_sta[13] & SD_MODE_HIGH_SPEED)
  585. card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
  586. if (card->scr.sda_spec3) {
  587. card->sw_caps.sd3_bus_mode = p_sta[13];
  588. /* Find out Driver Strengths supported by the card */
  589. err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_DRV_STRENGTH,
  590. SD_SWITCH_ACCESS_HS, p_sta);
  591. if (err) {
  592. SD_LOGW("%s: problem reading Driver Strength.\n", __func__);
  593. goto out;
  594. }
  595. card->sw_caps.sd3_drv_type = p_sta[9];
  596. /* Find out Current Limits supported by the card */
  597. err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_CUR_LIMIT,
  598. SD_SWITCH_ACCESS_HS, p_sta);
  599. if (err) {
  600. SD_LOGW("%s: problem reading Current Limit.\n", __func__);
  601. goto out;
  602. }
  603. card->sw_caps.sd3_curr_limit = p_sta[7];
  604. }
  605. out:
  606. return err;
  607. }
  608. /*
  609. * Test if the card supports high-speed mode and, if so, switch to it.
  610. */
  611. int32_t mmc_sd_switch_hs(struct mmc_card *card)
  612. {
  613. int32_t err;
  614. uint32_t status[64/sizeof(uint32_t)] = {0};
  615. uint8_t *p_sta = (uint8_t *)status;
  616. if (card->scr.sda_vsn < SCR_SPEC_VER_1) {
  617. SD_LOGN("Card ver. does not support to switch to high speed!\n");
  618. return 0;
  619. }
  620. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  621. SD_LOGN("Card cmdclass not support to switch to high speed!\n");
  622. return 0;
  623. }
  624. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  625. return 0;
  626. if (card->sw_caps.hs_max_dtr == 0)
  627. return 0;
  628. /* Check function */
  629. err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_ACCESS_MODE,
  630. SD_SWITCH_ACCESS_HS, p_sta);
  631. if (err)
  632. return err;
  633. if ((p_sta[16] & 0x0F) != 1) {
  634. SD_LOGW("%s: Problem switching card into high-speed mode!\n", __func__);
  635. err = 0;
  636. } else {
  637. err = 1;
  638. }
  639. return err;
  640. }
  641. static int32_t mmc_send_cxd_native(struct mmc_host *host, uint32_t arg, uint32_t *cxd, int32_t opcode)
  642. {
  643. int32_t err;
  644. struct mmc_command cmd = {0};
  645. if (!host || !cxd) {
  646. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  647. return -1;
  648. }
  649. cmd.opcode = opcode;
  650. cmd.arg = arg;
  651. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  652. err = mmc_wait_for_cmd(host, &cmd);
  653. if (err)
  654. return err;
  655. HAL_Memcpy(cxd, cmd.resp, sizeof(uint32_t) * 4);
  656. return 0;
  657. }
  658. int32_t mmc_send_csd(struct mmc_card *card, uint32_t *csd)
  659. {
  660. return mmc_send_cxd_native(card->host, card->rca << 16,
  661. csd, MMC_SEND_CSD);
  662. }
  663. int32_t mmc_sd_get_csd(struct mmc_card *card)
  664. {
  665. int32_t err;
  666. uint32_t csd[4] = {0};
  667. /* Fetch CSD from card. */
  668. err = mmc_send_csd(card, csd);
  669. if (err)
  670. return err;
  671. SD_LOGN("card raw csd:\n");
  672. sd_hex_dump_bytes((void *)csd, 16);
  673. err = mmc_decode_csd(card, csd);
  674. if (err)
  675. return err;
  676. return 0;
  677. }
  678. int32_t mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card)
  679. {
  680. int32_t err;
  681. int32_t retries;
  682. uint32_t raw_scr[2] = {0};
  683. /* Fetch SCR from card. */
  684. err = mmc_app_send_scr(card, raw_scr);
  685. if (err)
  686. return err;
  687. SD_LOGN("card raw scr:\n");
  688. sd_hex_dump_bytes((void *)raw_scr, 8);
  689. err = mmc_decode_scr(card, raw_scr);
  690. if (err)
  691. return err;
  692. /* Fetch and process SD Status register. */
  693. err = mmc_read_ssr(card);
  694. if (err)
  695. return err;
  696. #ifdef SD_SUPPORT_ERASE
  697. /* Erase init depends on CSD and SSR */
  698. mmc_init_erase(card);
  699. #endif
  700. /* Fetch switch information from card. */
  701. for (retries = 1; retries <= 3; retries++) {
  702. err = mmc_read_switch(card);
  703. if (!err) {
  704. if (retries > 1) {
  705. SD_LOGW("%s: recovered\n", __func__);
  706. }
  707. break;
  708. } else {
  709. SD_LOGW("%s: read switch failed (attempt %d)\n",
  710. __func__, (unsigned int)retries);
  711. }
  712. }
  713. if (err)
  714. return err;
  715. #ifdef SD_SUPPORT_WRITEPROTECT
  716. int32_t ro = -1;
  717. /* Check if read-only switch is active. */
  718. mmc_host_clk_hold(card->host);
  719. ro = HAL_SDC_Get_ReadOnly(card->host);
  720. mmc_host_clk_release(card->host);
  721. if (ro < 0) {
  722. SD_LOGW("%s: host does not support reading read-only switch."
  723. " assuming write-enable.\n", __func__);
  724. } else if (ro > 0) {
  725. mmc_card_set_readonly(card);
  726. }
  727. #endif
  728. return 0;
  729. }
  730. uint32_t mmc_sd_get_max_clock(struct mmc_card *card)
  731. {
  732. uint32_t max_dtr = (uint32_t)-1;
  733. if (mmc_card_highspeed(card)) {
  734. if (max_dtr > card->sw_caps.hs_max_dtr)
  735. max_dtr = card->sw_caps.hs_max_dtr;
  736. } else if (max_dtr > card->csd.max_dtr) {
  737. max_dtr = card->csd.max_dtr;
  738. }
  739. return max_dtr;
  740. }
  741. /*
  742. * Handle the detection and initialisation of a card.
  743. *
  744. * In the case of a resume, "oldcard" will contain the card
  745. * we're trying to reinitialise.
  746. */
  747. static int32_t mmc_sd_init_card(struct mmc_card *card, struct mmc_host *host)
  748. {
  749. int32_t err = 0;
  750. /*
  751. * I/O voltage should be 3.3 V here for the initialization needed.
  752. */
  753. /* cmd2, send cid, check if card support 3.3V */
  754. err = mmc_all_send_cid(host, card->cidno);
  755. if (err) {
  756. SD_LOGW("Cards all send CID number failed !!\n");
  757. return -1;
  758. } else
  759. SD_LOGN("Card CID number:%x\n", (unsigned int)card->cidno[0]);
  760. card->type = MMC_TYPE_SD;
  761. /* cmd3, For native busses: get card RCA and quit open drain mode. */
  762. err = mmc_send_relative_addr(card->host, &card->rca);
  763. if (err) {
  764. SD_LOGW("Card public new RCA failed !!\n");
  765. return -1;
  766. } else
  767. SD_LOGD("Card public new RCA:%x\n", (unsigned int)card->rca);
  768. /* cmd10, get CID register */
  769. if (mmc_send_cid(card)) {
  770. SD_LOGW("Card send CID reg failed !!\n");
  771. return -1;
  772. }
  773. /* cmd9, get CSD register */
  774. if (mmc_sd_get_csd(card)) {
  775. SD_LOGW("Card send CSD reg failed !!\n");
  776. return -1;
  777. }
  778. /* cmd7, Select card to standby state, as all following commands rely on that. */
  779. if (mmc_select_card(card, 1)) {
  780. SD_LOGW("mmc_select_card failed !!\n");
  781. return -1;
  782. }
  783. err = mmc_sd_setup_card(host, card);
  784. if (err)
  785. goto free_card;
  786. /* Initialization sequence for UHS-I cards */
  787. #ifdef SD_SUPPORT_VERSION3
  788. if (card->ocr.ocr & SD_ROCR_S18A) {
  789. err = mmc_sd_init_uhs_card(card);
  790. if (err)
  791. goto free_card;
  792. /* Card is an ultra-high-speed card */
  793. mmc_card_set_uhs(card);
  794. /*
  795. * Since initialization is now complete, enable preset
  796. * value registers for UHS-I cards.
  797. */
  798. mmc_host_clk_hold(card->host);
  799. HAL_SDC_Enable_Preset_Value(card->host, true);
  800. mmc_host_clk_release(card->host);
  801. } else
  802. #endif
  803. {
  804. uint32_t clk;
  805. uint32_t retries = 3;
  806. while (retries) {
  807. err = mmc_sd_switch_hs(card);
  808. if (err < 0) {
  809. SD_LOGE("%s: Re-switch hs, err %d (retries = %u)\n",
  810. __func__, (unsigned int)err, (unsigned int)retries);
  811. mmc_mdelay(5);
  812. retries--;
  813. continue;
  814. }
  815. break;
  816. }
  817. if (err <= 0) {
  818. SD_LOGW("switch to high speed error, use DS: 25 MHz\n");
  819. clk = 25000000;
  820. err = HAL_SDC_Update_Clk(card->host, clk);
  821. if (err)
  822. return -1;
  823. } else {
  824. mmc_card_set_highspeed(card);
  825. card->sd_bus_speed = HIGH_SPEED_BUS_SPEED;
  826. clk = mmc_sd_get_max_clock(card);
  827. err = HAL_SDC_Update_Clk(card->host, clk);
  828. if (err)
  829. return -1;
  830. SD_LOGN("card is switched to high speed mode, clk:%u KHz\n", (unsigned int)clk/1000);
  831. }
  832. /* Switch to wider bus (if supported). */
  833. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  834. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  835. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  836. if (err) {
  837. SD_LOGW("Set bus width error, use default 1 bit !!\n");
  838. return -1;
  839. }
  840. /* config SDMMC controller bus width */
  841. HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
  842. SD_LOGN("Set bus width type: %d\n", MMC_BUS_WIDTH_4);
  843. }
  844. }
  845. mmc_add_card(card);
  846. return 0;
  847. free_card:
  848. return err;
  849. }
  850. #ifdef CONFIG_SD_PM
  851. static int32_t mmc_sd_suspend(struct mmc_host *host)
  852. {
  853. struct mmc_card *card = host->card;
  854. if (card == NULL) {
  855. SD_LOGE_RAW(ROM_ERR_MASK, "card open fail\n");
  856. return -1;
  857. }
  858. mmc_card_open(card->id);
  859. card->suspend = 1;
  860. mmc_card_deinit(host->card);
  861. SD_LOGD("%s ok\n", __func__);
  862. mmc_card_close(card->id);
  863. return 0;
  864. }
  865. static int32_t mmc_sd_resume(struct mmc_host *host)
  866. {
  867. struct mmc_card *card = host->card;
  868. if (card == NULL) {
  869. SD_LOGE_RAW(ROM_ERR_MASK, "card open fail\n");
  870. return -1;
  871. }
  872. mmc_card_open(card->id);
  873. mmc_rescan(card, host->sdc_id);
  874. card->suspend = 0;
  875. SD_LOGD("%s ok\n", __func__);
  876. mmc_card_close(card->id);
  877. return 0;
  878. }
  879. static const struct mmc_bus_ops sd_bus_ops = {
  880. .suspend = mmc_sd_suspend,
  881. .resume = mmc_sd_resume,
  882. };
  883. #endif
  884. /*
  885. * Starting point for SD card init.
  886. */
  887. int32_t mmc_attach_sd(struct mmc_card *card, struct mmc_host *host)
  888. {
  889. int32_t err = 0;
  890. uint32_t ocr = 0;
  891. if (!host) {
  892. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  893. return -1;
  894. }
  895. //SD_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  896. /* send cmd41/55 to check operation condition */
  897. err = mmc_send_app_op_cond(host, 0, &ocr);
  898. if (err) {
  899. return err;
  900. }
  901. SD_LOGN("card ocr: %08x\n", (unsigned int)ocr);
  902. /*
  903. * Sanity check the voltages that the card claims to
  904. * support.
  905. */
  906. if (ocr & 0x7F) {
  907. SD_LOGW("%s: card claims to support voltages below the defined range."
  908. " These will be ignored.\n", __func__);
  909. ocr &= ~0x7F;
  910. }
  911. card->ocr.ocr = 0x7fffffff & ocr; /* set card not in busy state */
  912. err = mmc_sd_init_card(card, host);
  913. if (err) {
  914. goto out;
  915. }
  916. host->card = card;
  917. #ifdef CONFIG_SD_PM
  918. if (!card->suspend) {
  919. mmc_attach_bus(host, &sd_bus_ops);
  920. }
  921. #endif
  922. out:
  923. return err;
  924. }
  925. void mmc_deattach_sd(struct mmc_card *card, struct mmc_host *host)
  926. {
  927. mmc_select_card(card, 0);
  928. card->state &= ~MMC_STATE_HIGHSPEED;
  929. #ifdef CONFIG_SD_PM
  930. if (!card->suspend) {
  931. mmc_detach_bus(host);
  932. host->card = NULL;
  933. }
  934. #else
  935. host->card = NULL;
  936. #endif
  937. }
  938. #endif /* CONFIG_USE_SD */