mmc.c 34 KB

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