mmc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Copyright 2008, Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based vaguely on the Linux code
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <config.h>
  10. #include <common.h>
  11. #include <command.h>
  12. #include <mmc.h>
  13. #include <part.h>
  14. #include <malloc.h>
  15. #include <linux/list.h>
  16. #include <div64.h>
  17. /* Set block count limit because of 16 bit register limit on some hardware*/
  18. #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
  19. #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
  20. #endif
  21. static struct list_head mmc_devices;
  22. static int cur_dev_num = -1;
  23. int __weak board_mmc_getwp(struct mmc *mmc)
  24. {
  25. return -1;
  26. }
  27. int mmc_getwp(struct mmc *mmc)
  28. {
  29. int wp;
  30. wp = board_mmc_getwp(mmc);
  31. if (wp < 0) {
  32. if (mmc->getwp)
  33. wp = mmc->getwp(mmc);
  34. else
  35. wp = 0;
  36. }
  37. return wp;
  38. }
  39. int __board_mmc_getcd(struct mmc *mmc) {
  40. return -1;
  41. }
  42. int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
  43. alias("__board_mmc_getcd")));
  44. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  45. struct mmc_data *data)
  46. {
  47. struct mmc_data backup;
  48. int ret;
  49. memset(&backup, 0, sizeof(backup));
  50. #ifdef CONFIG_MMC_TRACE
  51. int i;
  52. u8 *ptr;
  53. printf("CMD_SEND:%d\n", cmd->cmdidx);
  54. printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
  55. ret = mmc->send_cmd(mmc, cmd, data);
  56. switch (cmd->resp_type) {
  57. case MMC_RSP_NONE:
  58. printf("\t\tMMC_RSP_NONE\n");
  59. break;
  60. case MMC_RSP_R1:
  61. printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
  62. cmd->response[0]);
  63. break;
  64. case MMC_RSP_R1b:
  65. printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
  66. cmd->response[0]);
  67. break;
  68. case MMC_RSP_R2:
  69. printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
  70. cmd->response[0]);
  71. printf("\t\t \t\t 0x%08X \n",
  72. cmd->response[1]);
  73. printf("\t\t \t\t 0x%08X \n",
  74. cmd->response[2]);
  75. printf("\t\t \t\t 0x%08X \n",
  76. cmd->response[3]);
  77. printf("\n");
  78. printf("\t\t\t\t\tDUMPING DATA\n");
  79. for (i = 0; i < 4; i++) {
  80. int j;
  81. printf("\t\t\t\t\t%03d - ", i*4);
  82. ptr = (u8 *)&cmd->response[i];
  83. ptr += 3;
  84. for (j = 0; j < 4; j++)
  85. printf("%02X ", *ptr--);
  86. printf("\n");
  87. }
  88. break;
  89. case MMC_RSP_R3:
  90. printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
  91. cmd->response[0]);
  92. break;
  93. default:
  94. printf("\t\tERROR MMC rsp not supported\n");
  95. break;
  96. }
  97. #else
  98. ret = mmc->send_cmd(mmc, cmd, data);
  99. #endif
  100. return ret;
  101. }
  102. static int mmc_send_status(struct mmc *mmc, int timeout)
  103. {
  104. struct mmc_cmd cmd;
  105. int err, retries = 5;
  106. #ifdef CONFIG_MMC_TRACE
  107. int status;
  108. #endif
  109. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  110. cmd.resp_type = MMC_RSP_R1;
  111. if (!mmc_host_is_spi(mmc))
  112. cmd.cmdarg = mmc->rca << 16;
  113. do {
  114. err = mmc_send_cmd(mmc, &cmd, NULL);
  115. if (!err) {
  116. if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
  117. (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
  118. MMC_STATE_PRG)
  119. break;
  120. else if (cmd.response[0] & MMC_STATUS_MASK) {
  121. printf("Status Error: 0x%08X\n",
  122. cmd.response[0]);
  123. return COMM_ERR;
  124. }
  125. } else if (--retries < 0)
  126. return err;
  127. udelay(1000);
  128. } while (timeout--);
  129. #ifdef CONFIG_MMC_TRACE
  130. status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
  131. printf("CURR STATE:%d\n", status);
  132. #endif
  133. if (timeout <= 0) {
  134. printf("Timeout waiting card ready\n");
  135. return TIMEOUT;
  136. }
  137. return 0;
  138. }
  139. static int mmc_set_blocklen(struct mmc *mmc, int len)
  140. {
  141. struct mmc_cmd cmd;
  142. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  143. cmd.resp_type = MMC_RSP_R1;
  144. cmd.cmdarg = len;
  145. return mmc_send_cmd(mmc, &cmd, NULL);
  146. }
  147. struct mmc *find_mmc_device(int dev_num)
  148. {
  149. struct mmc *m;
  150. struct list_head *entry;
  151. list_for_each(entry, &mmc_devices) {
  152. m = list_entry(entry, struct mmc, link);
  153. if (m->block_dev.dev == dev_num)
  154. return m;
  155. }
  156. printf("MMC Device %d not found\n", dev_num);
  157. return NULL;
  158. }
  159. static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
  160. {
  161. struct mmc_cmd cmd;
  162. ulong end;
  163. int err, start_cmd, end_cmd;
  164. if (mmc->high_capacity)
  165. end = start + blkcnt - 1;
  166. else {
  167. end = (start + blkcnt - 1) * mmc->write_bl_len;
  168. start *= mmc->write_bl_len;
  169. }
  170. if (IS_SD(mmc)) {
  171. start_cmd = SD_CMD_ERASE_WR_BLK_START;
  172. end_cmd = SD_CMD_ERASE_WR_BLK_END;
  173. } else {
  174. start_cmd = MMC_CMD_ERASE_GROUP_START;
  175. end_cmd = MMC_CMD_ERASE_GROUP_END;
  176. }
  177. cmd.cmdidx = start_cmd;
  178. cmd.cmdarg = start;
  179. cmd.resp_type = MMC_RSP_R1;
  180. err = mmc_send_cmd(mmc, &cmd, NULL);
  181. if (err)
  182. goto err_out;
  183. cmd.cmdidx = end_cmd;
  184. cmd.cmdarg = end;
  185. err = mmc_send_cmd(mmc, &cmd, NULL);
  186. if (err)
  187. goto err_out;
  188. cmd.cmdidx = MMC_CMD_ERASE;
  189. cmd.cmdarg = SECURE_ERASE;
  190. cmd.resp_type = MMC_RSP_R1b;
  191. err = mmc_send_cmd(mmc, &cmd, NULL);
  192. if (err)
  193. goto err_out;
  194. return 0;
  195. err_out:
  196. puts("mmc erase failed\n");
  197. return err;
  198. }
  199. static unsigned long
  200. mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
  201. {
  202. int err = 0;
  203. struct mmc *mmc = find_mmc_device(dev_num);
  204. lbaint_t blk = 0, blk_r = 0;
  205. int timeout = 1000;
  206. if (!mmc)
  207. return -1;
  208. if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
  209. printf("\n\nCaution! Your devices Erase group is 0x%x\n"
  210. "The erase range would be change to "
  211. "0x" LBAF "~0x" LBAF "\n\n",
  212. mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
  213. ((start + blkcnt + mmc->erase_grp_size)
  214. & ~(mmc->erase_grp_size - 1)) - 1);
  215. while (blk < blkcnt) {
  216. blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
  217. mmc->erase_grp_size : (blkcnt - blk);
  218. err = mmc_erase_t(mmc, start + blk, blk_r);
  219. if (err)
  220. break;
  221. blk += blk_r;
  222. /* Waiting for the ready status */
  223. if (mmc_send_status(mmc, timeout))
  224. return 0;
  225. }
  226. return blk;
  227. }
  228. static ulong
  229. mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src)
  230. {
  231. struct mmc_cmd cmd;
  232. struct mmc_data data;
  233. int timeout = 1000;
  234. if ((start + blkcnt) > mmc->block_dev.lba) {
  235. printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
  236. start + blkcnt, mmc->block_dev.lba);
  237. return 0;
  238. }
  239. if (blkcnt == 0)
  240. return 0;
  241. else if (blkcnt == 1)
  242. cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
  243. else
  244. cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
  245. if (mmc->high_capacity)
  246. cmd.cmdarg = start;
  247. else
  248. cmd.cmdarg = start * mmc->write_bl_len;
  249. cmd.resp_type = MMC_RSP_R1;
  250. data.src = src;
  251. data.blocks = blkcnt;
  252. data.blocksize = mmc->write_bl_len;
  253. data.flags = MMC_DATA_WRITE;
  254. if (mmc_send_cmd(mmc, &cmd, &data)) {
  255. printf("mmc write failed\n");
  256. return 0;
  257. }
  258. /* SPI multiblock writes terminate using a special
  259. * token, not a STOP_TRANSMISSION request.
  260. */
  261. if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
  262. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  263. cmd.cmdarg = 0;
  264. cmd.resp_type = MMC_RSP_R1b;
  265. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  266. printf("mmc fail to send stop cmd\n");
  267. return 0;
  268. }
  269. }
  270. /* Waiting for the ready status */
  271. if (mmc_send_status(mmc, timeout))
  272. return 0;
  273. return blkcnt;
  274. }
  275. static ulong
  276. mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src)
  277. {
  278. lbaint_t cur, blocks_todo = blkcnt;
  279. struct mmc *mmc = find_mmc_device(dev_num);
  280. if (!mmc)
  281. return 0;
  282. if (mmc_set_blocklen(mmc, mmc->write_bl_len))
  283. return 0;
  284. do {
  285. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  286. if(mmc_write_blocks(mmc, start, cur, src) != cur)
  287. return 0;
  288. blocks_todo -= cur;
  289. start += cur;
  290. src += cur * mmc->write_bl_len;
  291. } while (blocks_todo > 0);
  292. return blkcnt;
  293. }
  294. static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
  295. lbaint_t blkcnt)
  296. {
  297. struct mmc_cmd cmd;
  298. struct mmc_data data;
  299. if (blkcnt > 1)
  300. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  301. else
  302. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  303. if (mmc->high_capacity)
  304. cmd.cmdarg = start;
  305. else
  306. cmd.cmdarg = start * mmc->read_bl_len;
  307. cmd.resp_type = MMC_RSP_R1;
  308. data.dest = dst;
  309. data.blocks = blkcnt;
  310. data.blocksize = mmc->read_bl_len;
  311. data.flags = MMC_DATA_READ;
  312. if (mmc_send_cmd(mmc, &cmd, &data))
  313. return 0;
  314. if (blkcnt > 1) {
  315. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  316. cmd.cmdarg = 0;
  317. cmd.resp_type = MMC_RSP_R1b;
  318. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  319. printf("mmc fail to send stop cmd\n");
  320. return 0;
  321. }
  322. }
  323. return blkcnt;
  324. }
  325. static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
  326. {
  327. lbaint_t cur, blocks_todo = blkcnt;
  328. if (blkcnt == 0)
  329. return 0;
  330. struct mmc *mmc = find_mmc_device(dev_num);
  331. if (!mmc)
  332. return 0;
  333. if ((start + blkcnt) > mmc->block_dev.lba) {
  334. printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
  335. start + blkcnt, mmc->block_dev.lba);
  336. return 0;
  337. }
  338. if (mmc_set_blocklen(mmc, mmc->read_bl_len))
  339. return 0;
  340. do {
  341. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  342. if(mmc_read_blocks(mmc, dst, start, cur) != cur)
  343. return 0;
  344. blocks_todo -= cur;
  345. start += cur;
  346. dst += cur * mmc->read_bl_len;
  347. } while (blocks_todo > 0);
  348. return blkcnt;
  349. }
  350. static int mmc_go_idle(struct mmc *mmc)
  351. {
  352. struct mmc_cmd cmd;
  353. int err;
  354. udelay(1000);
  355. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  356. cmd.cmdarg = 0;
  357. cmd.resp_type = MMC_RSP_NONE;
  358. err = mmc_send_cmd(mmc, &cmd, NULL);
  359. if (err)
  360. return err;
  361. udelay(2000);
  362. return 0;
  363. }
  364. static int sd_send_op_cond(struct mmc *mmc)
  365. {
  366. int timeout = 1000;
  367. int err;
  368. struct mmc_cmd cmd;
  369. do {
  370. cmd.cmdidx = MMC_CMD_APP_CMD;
  371. cmd.resp_type = MMC_RSP_R1;
  372. cmd.cmdarg = 0;
  373. err = mmc_send_cmd(mmc, &cmd, NULL);
  374. if (err)
  375. return err;
  376. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  377. cmd.resp_type = MMC_RSP_R3;
  378. /*
  379. * Most cards do not answer if some reserved bits
  380. * in the ocr are set. However, Some controller
  381. * can set bit 7 (reserved for low voltages), but
  382. * how to manage low voltages SD card is not yet
  383. * specified.
  384. */
  385. cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
  386. (mmc->voltages & 0xff8000);
  387. if (mmc->version == SD_VERSION_2)
  388. cmd.cmdarg |= OCR_HCS;
  389. err = mmc_send_cmd(mmc, &cmd, NULL);
  390. if (err)
  391. return err;
  392. udelay(1000);
  393. } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
  394. if (timeout <= 0)
  395. return UNUSABLE_ERR;
  396. if (mmc->version != SD_VERSION_2)
  397. mmc->version = SD_VERSION_1_0;
  398. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  399. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  400. cmd.resp_type = MMC_RSP_R3;
  401. cmd.cmdarg = 0;
  402. err = mmc_send_cmd(mmc, &cmd, NULL);
  403. if (err)
  404. return err;
  405. }
  406. mmc->ocr = cmd.response[0];
  407. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  408. mmc->rca = 0;
  409. return 0;
  410. }
  411. /* We pass in the cmd since otherwise the init seems to fail */
  412. static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
  413. int use_arg)
  414. {
  415. int err;
  416. cmd->cmdidx = MMC_CMD_SEND_OP_COND;
  417. cmd->resp_type = MMC_RSP_R3;
  418. cmd->cmdarg = 0;
  419. if (use_arg && !mmc_host_is_spi(mmc)) {
  420. cmd->cmdarg =
  421. (mmc->voltages &
  422. (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
  423. (mmc->op_cond_response & OCR_ACCESS_MODE);
  424. if (mmc->host_caps & MMC_MODE_HC)
  425. cmd->cmdarg |= OCR_HCS;
  426. }
  427. err = mmc_send_cmd(mmc, cmd, NULL);
  428. if (err)
  429. return err;
  430. mmc->op_cond_response = cmd->response[0];
  431. return 0;
  432. }
  433. int mmc_send_op_cond(struct mmc *mmc)
  434. {
  435. struct mmc_cmd cmd;
  436. int err, i;
  437. /* Some cards seem to need this */
  438. mmc_go_idle(mmc);
  439. /* Asking to the card its capabilities */
  440. mmc->op_cond_pending = 1;
  441. for (i = 0; i < 2; i++) {
  442. err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
  443. if (err)
  444. return err;
  445. /* exit if not busy (flag seems to be inverted) */
  446. if (mmc->op_cond_response & OCR_BUSY)
  447. return 0;
  448. }
  449. return IN_PROGRESS;
  450. }
  451. int mmc_complete_op_cond(struct mmc *mmc)
  452. {
  453. struct mmc_cmd cmd;
  454. int timeout = 1000;
  455. uint start;
  456. int err;
  457. mmc->op_cond_pending = 0;
  458. start = get_timer(0);
  459. do {
  460. err = mmc_send_op_cond_iter(mmc, &cmd, 1);
  461. if (err)
  462. return err;
  463. if (get_timer(start) > timeout)
  464. return UNUSABLE_ERR;
  465. udelay(100);
  466. } while (!(mmc->op_cond_response & OCR_BUSY));
  467. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  468. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  469. cmd.resp_type = MMC_RSP_R3;
  470. cmd.cmdarg = 0;
  471. err = mmc_send_cmd(mmc, &cmd, NULL);
  472. if (err)
  473. return err;
  474. }
  475. mmc->version = MMC_VERSION_UNKNOWN;
  476. mmc->ocr = cmd.response[0];
  477. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  478. mmc->rca = 0;
  479. return 0;
  480. }
  481. static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
  482. {
  483. struct mmc_cmd cmd;
  484. struct mmc_data data;
  485. int err;
  486. /* Get the Card Status Register */
  487. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  488. cmd.resp_type = MMC_RSP_R1;
  489. cmd.cmdarg = 0;
  490. data.dest = (char *)ext_csd;
  491. data.blocks = 1;
  492. data.blocksize = MMC_MAX_BLOCK_LEN;
  493. data.flags = MMC_DATA_READ;
  494. err = mmc_send_cmd(mmc, &cmd, &data);
  495. return err;
  496. }
  497. static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
  498. {
  499. struct mmc_cmd cmd;
  500. int timeout = 1000;
  501. int ret;
  502. cmd.cmdidx = MMC_CMD_SWITCH;
  503. cmd.resp_type = MMC_RSP_R1b;
  504. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  505. (index << 16) |
  506. (value << 8);
  507. ret = mmc_send_cmd(mmc, &cmd, NULL);
  508. /* Waiting for the ready status */
  509. if (!ret)
  510. ret = mmc_send_status(mmc, timeout);
  511. return ret;
  512. }
  513. static int mmc_change_freq(struct mmc *mmc)
  514. {
  515. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  516. char cardtype;
  517. int err;
  518. mmc->card_caps = 0;
  519. if (mmc_host_is_spi(mmc))
  520. return 0;
  521. /* Only version 4 supports high-speed */
  522. if (mmc->version < MMC_VERSION_4)
  523. return 0;
  524. err = mmc_send_ext_csd(mmc, ext_csd);
  525. if (err)
  526. return err;
  527. cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
  528. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
  529. if (err)
  530. return err;
  531. /* Now check to see that it worked */
  532. err = mmc_send_ext_csd(mmc, ext_csd);
  533. if (err)
  534. return err;
  535. /* No high-speed support */
  536. if (!ext_csd[EXT_CSD_HS_TIMING])
  537. return 0;
  538. /* High Speed is set, there are two types: 52MHz and 26MHz */
  539. if (cardtype & MMC_HS_52MHZ)
  540. mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  541. else
  542. mmc->card_caps |= MMC_MODE_HS;
  543. return 0;
  544. }
  545. static int mmc_set_capacity(struct mmc *mmc, int part_num)
  546. {
  547. switch (part_num) {
  548. case 0:
  549. mmc->capacity = mmc->capacity_user;
  550. break;
  551. case 1:
  552. case 2:
  553. mmc->capacity = mmc->capacity_boot;
  554. break;
  555. case 3:
  556. mmc->capacity = mmc->capacity_rpmb;
  557. break;
  558. case 4:
  559. case 5:
  560. case 6:
  561. case 7:
  562. mmc->capacity = mmc->capacity_gp[part_num - 4];
  563. break;
  564. default:
  565. return -1;
  566. }
  567. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  568. return 0;
  569. }
  570. int mmc_switch_part(int dev_num, unsigned int part_num)
  571. {
  572. struct mmc *mmc = find_mmc_device(dev_num);
  573. int ret;
  574. if (!mmc)
  575. return -1;
  576. ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  577. (mmc->part_config & ~PART_ACCESS_MASK)
  578. | (part_num & PART_ACCESS_MASK));
  579. if (ret)
  580. return ret;
  581. return mmc_set_capacity(mmc, part_num);
  582. }
  583. int mmc_getcd(struct mmc *mmc)
  584. {
  585. int cd;
  586. cd = board_mmc_getcd(mmc);
  587. if (cd < 0) {
  588. if (mmc->getcd)
  589. cd = mmc->getcd(mmc);
  590. else
  591. cd = 1;
  592. }
  593. return cd;
  594. }
  595. static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  596. {
  597. struct mmc_cmd cmd;
  598. struct mmc_data data;
  599. /* Switch the frequency */
  600. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  601. cmd.resp_type = MMC_RSP_R1;
  602. cmd.cmdarg = (mode << 31) | 0xffffff;
  603. cmd.cmdarg &= ~(0xf << (group * 4));
  604. cmd.cmdarg |= value << (group * 4);
  605. data.dest = (char *)resp;
  606. data.blocksize = 64;
  607. data.blocks = 1;
  608. data.flags = MMC_DATA_READ;
  609. return mmc_send_cmd(mmc, &cmd, &data);
  610. }
  611. static int sd_change_freq(struct mmc *mmc)
  612. {
  613. int err;
  614. struct mmc_cmd cmd;
  615. ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
  616. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  617. struct mmc_data data;
  618. int timeout;
  619. mmc->card_caps = 0;
  620. if (mmc_host_is_spi(mmc))
  621. return 0;
  622. /* Read the SCR to find out if this card supports higher speeds */
  623. cmd.cmdidx = MMC_CMD_APP_CMD;
  624. cmd.resp_type = MMC_RSP_R1;
  625. cmd.cmdarg = mmc->rca << 16;
  626. err = mmc_send_cmd(mmc, &cmd, NULL);
  627. if (err)
  628. return err;
  629. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  630. cmd.resp_type = MMC_RSP_R1;
  631. cmd.cmdarg = 0;
  632. timeout = 3;
  633. retry_scr:
  634. data.dest = (char *)scr;
  635. data.blocksize = 8;
  636. data.blocks = 1;
  637. data.flags = MMC_DATA_READ;
  638. err = mmc_send_cmd(mmc, &cmd, &data);
  639. if (err) {
  640. if (timeout--)
  641. goto retry_scr;
  642. return err;
  643. }
  644. mmc->scr[0] = __be32_to_cpu(scr[0]);
  645. mmc->scr[1] = __be32_to_cpu(scr[1]);
  646. switch ((mmc->scr[0] >> 24) & 0xf) {
  647. case 0:
  648. mmc->version = SD_VERSION_1_0;
  649. break;
  650. case 1:
  651. mmc->version = SD_VERSION_1_10;
  652. break;
  653. case 2:
  654. mmc->version = SD_VERSION_2;
  655. if ((mmc->scr[0] >> 15) & 0x1)
  656. mmc->version = SD_VERSION_3;
  657. break;
  658. default:
  659. mmc->version = SD_VERSION_1_0;
  660. break;
  661. }
  662. if (mmc->scr[0] & SD_DATA_4BIT)
  663. mmc->card_caps |= MMC_MODE_4BIT;
  664. /* Version 1.0 doesn't support switching */
  665. if (mmc->version == SD_VERSION_1_0)
  666. return 0;
  667. timeout = 4;
  668. while (timeout--) {
  669. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  670. (u8 *)switch_status);
  671. if (err)
  672. return err;
  673. /* The high-speed function is busy. Try again */
  674. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  675. break;
  676. }
  677. /* If high-speed isn't supported, we return */
  678. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  679. return 0;
  680. /*
  681. * If the host doesn't support SD_HIGHSPEED, do not switch card to
  682. * HIGHSPEED mode even if the card support SD_HIGHSPPED.
  683. * This can avoid furthur problem when the card runs in different
  684. * mode between the host.
  685. */
  686. if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
  687. (mmc->host_caps & MMC_MODE_HS)))
  688. return 0;
  689. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
  690. if (err)
  691. return err;
  692. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  693. mmc->card_caps |= MMC_MODE_HS;
  694. return 0;
  695. }
  696. /* frequency bases */
  697. /* divided by 10 to be nice to platforms without floating point */
  698. static const int fbase[] = {
  699. 10000,
  700. 100000,
  701. 1000000,
  702. 10000000,
  703. };
  704. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  705. * to platforms without floating point.
  706. */
  707. static const int multipliers[] = {
  708. 0, /* reserved */
  709. 10,
  710. 12,
  711. 13,
  712. 15,
  713. 20,
  714. 25,
  715. 30,
  716. 35,
  717. 40,
  718. 45,
  719. 50,
  720. 55,
  721. 60,
  722. 70,
  723. 80,
  724. };
  725. static void mmc_set_ios(struct mmc *mmc)
  726. {
  727. mmc->set_ios(mmc);
  728. }
  729. void mmc_set_clock(struct mmc *mmc, uint clock)
  730. {
  731. if (clock > mmc->f_max)
  732. clock = mmc->f_max;
  733. if (clock < mmc->f_min)
  734. clock = mmc->f_min;
  735. mmc->clock = clock;
  736. mmc_set_ios(mmc);
  737. }
  738. static void mmc_set_bus_width(struct mmc *mmc, uint width)
  739. {
  740. mmc->bus_width = width;
  741. mmc_set_ios(mmc);
  742. }
  743. static int mmc_startup(struct mmc *mmc)
  744. {
  745. int err, i;
  746. uint mult, freq;
  747. u64 cmult, csize, capacity;
  748. struct mmc_cmd cmd;
  749. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  750. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  751. int timeout = 1000;
  752. #ifdef CONFIG_MMC_SPI_CRC_ON
  753. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  754. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  755. cmd.resp_type = MMC_RSP_R1;
  756. cmd.cmdarg = 1;
  757. err = mmc_send_cmd(mmc, &cmd, NULL);
  758. if (err)
  759. return err;
  760. }
  761. #endif
  762. /* Put the Card in Identify Mode */
  763. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  764. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  765. cmd.resp_type = MMC_RSP_R2;
  766. cmd.cmdarg = 0;
  767. err = mmc_send_cmd(mmc, &cmd, NULL);
  768. if (err)
  769. return err;
  770. memcpy(mmc->cid, cmd.response, 16);
  771. /*
  772. * For MMC cards, set the Relative Address.
  773. * For SD cards, get the Relatvie Address.
  774. * This also puts the cards into Standby State
  775. */
  776. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  777. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  778. cmd.cmdarg = mmc->rca << 16;
  779. cmd.resp_type = MMC_RSP_R6;
  780. err = mmc_send_cmd(mmc, &cmd, NULL);
  781. if (err)
  782. return err;
  783. if (IS_SD(mmc))
  784. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  785. }
  786. /* Get the Card-Specific Data */
  787. cmd.cmdidx = MMC_CMD_SEND_CSD;
  788. cmd.resp_type = MMC_RSP_R2;
  789. cmd.cmdarg = mmc->rca << 16;
  790. err = mmc_send_cmd(mmc, &cmd, NULL);
  791. /* Waiting for the ready status */
  792. mmc_send_status(mmc, timeout);
  793. if (err)
  794. return err;
  795. mmc->csd[0] = cmd.response[0];
  796. mmc->csd[1] = cmd.response[1];
  797. mmc->csd[2] = cmd.response[2];
  798. mmc->csd[3] = cmd.response[3];
  799. if (mmc->version == MMC_VERSION_UNKNOWN) {
  800. int version = (cmd.response[0] >> 26) & 0xf;
  801. switch (version) {
  802. case 0:
  803. mmc->version = MMC_VERSION_1_2;
  804. break;
  805. case 1:
  806. mmc->version = MMC_VERSION_1_4;
  807. break;
  808. case 2:
  809. mmc->version = MMC_VERSION_2_2;
  810. break;
  811. case 3:
  812. mmc->version = MMC_VERSION_3;
  813. break;
  814. case 4:
  815. mmc->version = MMC_VERSION_4;
  816. break;
  817. default:
  818. mmc->version = MMC_VERSION_1_2;
  819. break;
  820. }
  821. }
  822. /* divide frequency by 10, since the mults are 10x bigger */
  823. freq = fbase[(cmd.response[0] & 0x7)];
  824. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  825. mmc->tran_speed = freq * mult;
  826. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  827. if (IS_SD(mmc))
  828. mmc->write_bl_len = mmc->read_bl_len;
  829. else
  830. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  831. if (mmc->high_capacity) {
  832. csize = (mmc->csd[1] & 0x3f) << 16
  833. | (mmc->csd[2] & 0xffff0000) >> 16;
  834. cmult = 8;
  835. } else {
  836. csize = (mmc->csd[1] & 0x3ff) << 2
  837. | (mmc->csd[2] & 0xc0000000) >> 30;
  838. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  839. }
  840. mmc->capacity_user = (csize + 1) << (cmult + 2);
  841. mmc->capacity_user *= mmc->read_bl_len;
  842. mmc->capacity_boot = 0;
  843. mmc->capacity_rpmb = 0;
  844. for (i = 0; i < 4; i++)
  845. mmc->capacity_gp[i] = 0;
  846. if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
  847. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  848. if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
  849. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  850. /* Select the card, and put it into Transfer Mode */
  851. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  852. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  853. cmd.resp_type = MMC_RSP_R1;
  854. cmd.cmdarg = mmc->rca << 16;
  855. err = mmc_send_cmd(mmc, &cmd, NULL);
  856. if (err)
  857. return err;
  858. }
  859. /*
  860. * For SD, its erase group is always one sector
  861. */
  862. mmc->erase_grp_size = 1;
  863. mmc->part_config = MMCPART_NOAVAILABLE;
  864. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  865. /* check ext_csd version and capacity */
  866. err = mmc_send_ext_csd(mmc, ext_csd);
  867. if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
  868. /*
  869. * According to the JEDEC Standard, the value of
  870. * ext_csd's capacity is valid if the value is more
  871. * than 2GB
  872. */
  873. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  874. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  875. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  876. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  877. capacity *= MMC_MAX_BLOCK_LEN;
  878. if ((capacity >> 20) > 2 * 1024)
  879. mmc->capacity_user = capacity;
  880. }
  881. switch (ext_csd[EXT_CSD_REV]) {
  882. case 1:
  883. mmc->version = MMC_VERSION_4_1;
  884. break;
  885. case 2:
  886. mmc->version = MMC_VERSION_4_2;
  887. break;
  888. case 3:
  889. mmc->version = MMC_VERSION_4_3;
  890. break;
  891. case 5:
  892. mmc->version = MMC_VERSION_4_41;
  893. break;
  894. case 6:
  895. mmc->version = MMC_VERSION_4_5;
  896. break;
  897. }
  898. /*
  899. * Check whether GROUP_DEF is set, if yes, read out
  900. * group size from ext_csd directly, or calculate
  901. * the group size from the csd value.
  902. */
  903. if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
  904. mmc->erase_grp_size =
  905. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
  906. MMC_MAX_BLOCK_LEN * 1024;
  907. } else {
  908. int erase_gsz, erase_gmul;
  909. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  910. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  911. mmc->erase_grp_size = (erase_gsz + 1)
  912. * (erase_gmul + 1);
  913. }
  914. /* store the partition info of emmc */
  915. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  916. ext_csd[EXT_CSD_BOOT_MULT])
  917. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  918. mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  919. mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
  920. for (i = 0; i < 4; i++) {
  921. int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
  922. mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
  923. (ext_csd[idx + 1] << 8) + ext_csd[idx];
  924. mmc->capacity_gp[i] *=
  925. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  926. mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  927. }
  928. }
  929. err = mmc_set_capacity(mmc, mmc->part_num);
  930. if (err)
  931. return err;
  932. if (IS_SD(mmc))
  933. err = sd_change_freq(mmc);
  934. else
  935. err = mmc_change_freq(mmc);
  936. if (err)
  937. return err;
  938. /* Restrict card's capabilities by what the host can do */
  939. mmc->card_caps &= mmc->host_caps;
  940. if (IS_SD(mmc)) {
  941. if (mmc->card_caps & MMC_MODE_4BIT) {
  942. cmd.cmdidx = MMC_CMD_APP_CMD;
  943. cmd.resp_type = MMC_RSP_R1;
  944. cmd.cmdarg = mmc->rca << 16;
  945. err = mmc_send_cmd(mmc, &cmd, NULL);
  946. if (err)
  947. return err;
  948. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  949. cmd.resp_type = MMC_RSP_R1;
  950. cmd.cmdarg = 2;
  951. err = mmc_send_cmd(mmc, &cmd, NULL);
  952. if (err)
  953. return err;
  954. mmc_set_bus_width(mmc, 4);
  955. }
  956. if (mmc->card_caps & MMC_MODE_HS)
  957. mmc->tran_speed = 50000000;
  958. else
  959. mmc->tran_speed = 25000000;
  960. } else {
  961. int idx;
  962. /* An array of possible bus widths in order of preference */
  963. static unsigned ext_csd_bits[] = {
  964. EXT_CSD_BUS_WIDTH_8,
  965. EXT_CSD_BUS_WIDTH_4,
  966. EXT_CSD_BUS_WIDTH_1,
  967. };
  968. /* An array to map CSD bus widths to host cap bits */
  969. static unsigned ext_to_hostcaps[] = {
  970. [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
  971. [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
  972. };
  973. /* An array to map chosen bus width to an integer */
  974. static unsigned widths[] = {
  975. 8, 4, 1,
  976. };
  977. for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
  978. unsigned int extw = ext_csd_bits[idx];
  979. /*
  980. * Check to make sure the controller supports
  981. * this bus width, if it's more than 1
  982. */
  983. if (extw != EXT_CSD_BUS_WIDTH_1 &&
  984. !(mmc->host_caps & ext_to_hostcaps[extw]))
  985. continue;
  986. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  987. EXT_CSD_BUS_WIDTH, extw);
  988. if (err)
  989. continue;
  990. mmc_set_bus_width(mmc, widths[idx]);
  991. err = mmc_send_ext_csd(mmc, test_csd);
  992. if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
  993. == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
  994. && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
  995. == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
  996. && ext_csd[EXT_CSD_REV] \
  997. == test_csd[EXT_CSD_REV]
  998. && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
  999. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  1000. && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
  1001. &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
  1002. mmc->card_caps |= ext_to_hostcaps[extw];
  1003. break;
  1004. }
  1005. }
  1006. if (mmc->card_caps & MMC_MODE_HS) {
  1007. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  1008. mmc->tran_speed = 52000000;
  1009. else
  1010. mmc->tran_speed = 26000000;
  1011. }
  1012. }
  1013. mmc_set_clock(mmc, mmc->tran_speed);
  1014. /* fill in device description */
  1015. mmc->block_dev.lun = 0;
  1016. mmc->block_dev.type = 0;
  1017. mmc->block_dev.blksz = mmc->read_bl_len;
  1018. mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
  1019. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  1020. sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
  1021. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  1022. (mmc->cid[3] >> 16) & 0xffff);
  1023. sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  1024. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  1025. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  1026. (mmc->cid[2] >> 24) & 0xff);
  1027. sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  1028. (mmc->cid[2] >> 16) & 0xf);
  1029. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  1030. init_part(&mmc->block_dev);
  1031. #endif
  1032. return 0;
  1033. }
  1034. static int mmc_send_if_cond(struct mmc *mmc)
  1035. {
  1036. struct mmc_cmd cmd;
  1037. int err;
  1038. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  1039. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  1040. cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
  1041. cmd.resp_type = MMC_RSP_R7;
  1042. err = mmc_send_cmd(mmc, &cmd, NULL);
  1043. if (err)
  1044. return err;
  1045. if ((cmd.response[0] & 0xff) != 0xaa)
  1046. return UNUSABLE_ERR;
  1047. else
  1048. mmc->version = SD_VERSION_2;
  1049. return 0;
  1050. }
  1051. int mmc_register(struct mmc *mmc)
  1052. {
  1053. /* Setup the universal parts of the block interface just once */
  1054. mmc->block_dev.if_type = IF_TYPE_MMC;
  1055. mmc->block_dev.dev = cur_dev_num++;
  1056. mmc->block_dev.removable = 1;
  1057. mmc->block_dev.block_read = mmc_bread;
  1058. mmc->block_dev.block_write = mmc_bwrite;
  1059. mmc->block_dev.block_erase = mmc_berase;
  1060. if (!mmc->b_max)
  1061. mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  1062. INIT_LIST_HEAD (&mmc->link);
  1063. list_add_tail (&mmc->link, &mmc_devices);
  1064. return 0;
  1065. }
  1066. #ifdef CONFIG_PARTITIONS
  1067. block_dev_desc_t *mmc_get_dev(int dev)
  1068. {
  1069. struct mmc *mmc = find_mmc_device(dev);
  1070. if (!mmc || mmc_init(mmc))
  1071. return NULL;
  1072. return &mmc->block_dev;
  1073. }
  1074. #endif
  1075. int mmc_start_init(struct mmc *mmc)
  1076. {
  1077. int err;
  1078. if (mmc_getcd(mmc) == 0) {
  1079. mmc->has_init = 0;
  1080. printf("MMC: no card present\n");
  1081. return NO_CARD_ERR;
  1082. }
  1083. if (mmc->has_init)
  1084. return 0;
  1085. err = mmc->init(mmc);
  1086. if (err)
  1087. return err;
  1088. mmc_set_bus_width(mmc, 1);
  1089. mmc_set_clock(mmc, 1);
  1090. /* Reset the Card */
  1091. err = mmc_go_idle(mmc);
  1092. if (err)
  1093. return err;
  1094. /* The internal partition reset to user partition(0) at every CMD0*/
  1095. mmc->part_num = 0;
  1096. /* Test for SD version 2 */
  1097. err = mmc_send_if_cond(mmc);
  1098. /* Now try to get the SD card's operating condition */
  1099. err = sd_send_op_cond(mmc);
  1100. /* If the command timed out, we check for an MMC card */
  1101. if (err == TIMEOUT) {
  1102. err = mmc_send_op_cond(mmc);
  1103. if (err && err != IN_PROGRESS) {
  1104. printf("Card did not respond to voltage select!\n");
  1105. return UNUSABLE_ERR;
  1106. }
  1107. }
  1108. if (err == IN_PROGRESS)
  1109. mmc->init_in_progress = 1;
  1110. return err;
  1111. }
  1112. static int mmc_complete_init(struct mmc *mmc)
  1113. {
  1114. int err = 0;
  1115. if (mmc->op_cond_pending)
  1116. err = mmc_complete_op_cond(mmc);
  1117. if (!err)
  1118. err = mmc_startup(mmc);
  1119. if (err)
  1120. mmc->has_init = 0;
  1121. else
  1122. mmc->has_init = 1;
  1123. mmc->init_in_progress = 0;
  1124. return err;
  1125. }
  1126. int mmc_init(struct mmc *mmc)
  1127. {
  1128. int err = IN_PROGRESS;
  1129. unsigned start = get_timer(0);
  1130. if (mmc->has_init)
  1131. return 0;
  1132. if (!mmc->init_in_progress)
  1133. err = mmc_start_init(mmc);
  1134. if (!err || err == IN_PROGRESS)
  1135. err = mmc_complete_init(mmc);
  1136. debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
  1137. return err;
  1138. }
  1139. /*
  1140. * CPU and board-specific MMC initializations. Aliased function
  1141. * signals caller to move on
  1142. */
  1143. static int __def_mmc_init(bd_t *bis)
  1144. {
  1145. return -1;
  1146. }
  1147. int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1148. int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1149. void print_mmc_devices(char separator)
  1150. {
  1151. struct mmc *m;
  1152. struct list_head *entry;
  1153. list_for_each(entry, &mmc_devices) {
  1154. m = list_entry(entry, struct mmc, link);
  1155. printf("%s: %d", m->name, m->block_dev.dev);
  1156. if (entry->next != &mmc_devices)
  1157. printf("%c ", separator);
  1158. }
  1159. printf("\n");
  1160. }
  1161. int get_mmc_num(void)
  1162. {
  1163. return cur_dev_num;
  1164. }
  1165. void mmc_set_preinit(struct mmc *mmc, int preinit)
  1166. {
  1167. mmc->preinit = preinit;
  1168. }
  1169. static void do_preinit(void)
  1170. {
  1171. struct mmc *m;
  1172. struct list_head *entry;
  1173. list_for_each(entry, &mmc_devices) {
  1174. m = list_entry(entry, struct mmc, link);
  1175. if (m->preinit)
  1176. mmc_start_init(m);
  1177. }
  1178. }
  1179. int mmc_initialize(bd_t *bis)
  1180. {
  1181. INIT_LIST_HEAD (&mmc_devices);
  1182. cur_dev_num = 0;
  1183. if (board_mmc_init(bis) < 0)
  1184. cpu_mmc_init(bis);
  1185. #ifndef CONFIG_SPL_BUILD
  1186. print_mmc_devices(',');
  1187. #endif
  1188. do_preinit();
  1189. return 0;
  1190. }
  1191. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  1192. /*
  1193. * This function changes the size of boot partition and the size of rpmb
  1194. * partition present on EMMC devices.
  1195. *
  1196. * Input Parameters:
  1197. * struct *mmc: pointer for the mmc device strcuture
  1198. * bootsize: size of boot partition
  1199. * rpmbsize: size of rpmb partition
  1200. *
  1201. * Returns 0 on success.
  1202. */
  1203. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  1204. unsigned long rpmbsize)
  1205. {
  1206. int err;
  1207. struct mmc_cmd cmd;
  1208. /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
  1209. cmd.cmdidx = MMC_CMD_RES_MAN;
  1210. cmd.resp_type = MMC_RSP_R1b;
  1211. cmd.cmdarg = MMC_CMD62_ARG1;
  1212. err = mmc_send_cmd(mmc, &cmd, NULL);
  1213. if (err) {
  1214. debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
  1215. return err;
  1216. }
  1217. /* Boot partition changing mode */
  1218. cmd.cmdidx = MMC_CMD_RES_MAN;
  1219. cmd.resp_type = MMC_RSP_R1b;
  1220. cmd.cmdarg = MMC_CMD62_ARG2;
  1221. err = mmc_send_cmd(mmc, &cmd, NULL);
  1222. if (err) {
  1223. debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
  1224. return err;
  1225. }
  1226. /* boot partition size is multiple of 128KB */
  1227. bootsize = (bootsize * 1024) / 128;
  1228. /* Arg: boot partition size */
  1229. cmd.cmdidx = MMC_CMD_RES_MAN;
  1230. cmd.resp_type = MMC_RSP_R1b;
  1231. cmd.cmdarg = bootsize;
  1232. err = mmc_send_cmd(mmc, &cmd, NULL);
  1233. if (err) {
  1234. debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
  1235. return err;
  1236. }
  1237. /* RPMB partition size is multiple of 128KB */
  1238. rpmbsize = (rpmbsize * 1024) / 128;
  1239. /* Arg: RPMB partition size */
  1240. cmd.cmdidx = MMC_CMD_RES_MAN;
  1241. cmd.resp_type = MMC_RSP_R1b;
  1242. cmd.cmdarg = rpmbsize;
  1243. err = mmc_send_cmd(mmc, &cmd, NULL);
  1244. if (err) {
  1245. debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
  1246. return err;
  1247. }
  1248. return 0;
  1249. }
  1250. /*
  1251. * This function shall form and send the commands to open / close the
  1252. * boot partition specified by user.
  1253. *
  1254. * Input Parameters:
  1255. * ack: 0x0 - No boot acknowledge sent (default)
  1256. * 0x1 - Boot acknowledge sent during boot operation
  1257. * part_num: User selects boot data that will be sent to master
  1258. * 0x0 - Device not boot enabled (default)
  1259. * 0x1 - Boot partition 1 enabled for boot
  1260. * 0x2 - Boot partition 2 enabled for boot
  1261. * access: User selects partitions to access
  1262. * 0x0 : No access to boot partition (default)
  1263. * 0x1 : R/W boot partition 1
  1264. * 0x2 : R/W boot partition 2
  1265. * 0x3 : R/W Replay Protected Memory Block (RPMB)
  1266. *
  1267. * Returns 0 on success.
  1268. */
  1269. int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  1270. {
  1271. int err;
  1272. struct mmc_cmd cmd;
  1273. /* Boot ack enable, boot partition enable , boot partition access */
  1274. cmd.cmdidx = MMC_CMD_SWITCH;
  1275. cmd.resp_type = MMC_RSP_R1b;
  1276. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  1277. (EXT_CSD_PART_CONF << 16) |
  1278. ((EXT_CSD_BOOT_ACK(ack) |
  1279. EXT_CSD_BOOT_PART_NUM(part_num) |
  1280. EXT_CSD_PARTITION_ACCESS(access)) << 8);
  1281. err = mmc_send_cmd(mmc, &cmd, NULL);
  1282. if (err) {
  1283. if (access) {
  1284. debug("mmc boot partition#%d open fail:Error1 = %d\n",
  1285. part_num, err);
  1286. } else {
  1287. debug("mmc boot partition#%d close fail:Error = %d\n",
  1288. part_num, err);
  1289. }
  1290. return err;
  1291. }
  1292. if (access) {
  1293. /* 4bit transfer mode at booting time. */
  1294. cmd.cmdidx = MMC_CMD_SWITCH;
  1295. cmd.resp_type = MMC_RSP_R1b;
  1296. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  1297. (EXT_CSD_BOOT_BUS_WIDTH << 16) |
  1298. ((1 << 0) << 8);
  1299. err = mmc_send_cmd(mmc, &cmd, NULL);
  1300. if (err) {
  1301. debug("mmc boot partition#%d open fail:Error2 = %d\n",
  1302. part_num, err);
  1303. return err;
  1304. }
  1305. }
  1306. return 0;
  1307. }
  1308. #endif