mmc.c 31 KB

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