mmc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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->card_caps & MMC_MODE_DDR_52MHz)
  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 = 0;
  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_52)
  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. if (ret)
  475. return ret;
  476. return mmc_set_capacity(mmc, part_num);
  477. }
  478. int mmc_getcd(struct mmc *mmc)
  479. {
  480. int cd;
  481. cd = board_mmc_getcd(mmc);
  482. if (cd < 0) {
  483. if (mmc->cfg->ops->getcd)
  484. cd = mmc->cfg->ops->getcd(mmc);
  485. else
  486. cd = 1;
  487. }
  488. return cd;
  489. }
  490. static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  491. {
  492. struct mmc_cmd cmd;
  493. struct mmc_data data;
  494. /* Switch the frequency */
  495. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  496. cmd.resp_type = MMC_RSP_R1;
  497. cmd.cmdarg = (mode << 31) | 0xffffff;
  498. cmd.cmdarg &= ~(0xf << (group * 4));
  499. cmd.cmdarg |= value << (group * 4);
  500. data.dest = (char *)resp;
  501. data.blocksize = 64;
  502. data.blocks = 1;
  503. data.flags = MMC_DATA_READ;
  504. return mmc_send_cmd(mmc, &cmd, &data);
  505. }
  506. static int sd_change_freq(struct mmc *mmc)
  507. {
  508. int err;
  509. struct mmc_cmd cmd;
  510. ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
  511. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  512. struct mmc_data data;
  513. int timeout;
  514. mmc->card_caps = 0;
  515. if (mmc_host_is_spi(mmc))
  516. return 0;
  517. /* Read the SCR to find out if this card supports higher speeds */
  518. cmd.cmdidx = MMC_CMD_APP_CMD;
  519. cmd.resp_type = MMC_RSP_R1;
  520. cmd.cmdarg = mmc->rca << 16;
  521. err = mmc_send_cmd(mmc, &cmd, NULL);
  522. if (err)
  523. return err;
  524. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  525. cmd.resp_type = MMC_RSP_R1;
  526. cmd.cmdarg = 0;
  527. timeout = 3;
  528. retry_scr:
  529. data.dest = (char *)scr;
  530. data.blocksize = 8;
  531. data.blocks = 1;
  532. data.flags = MMC_DATA_READ;
  533. err = mmc_send_cmd(mmc, &cmd, &data);
  534. if (err) {
  535. if (timeout--)
  536. goto retry_scr;
  537. return err;
  538. }
  539. mmc->scr[0] = __be32_to_cpu(scr[0]);
  540. mmc->scr[1] = __be32_to_cpu(scr[1]);
  541. switch ((mmc->scr[0] >> 24) & 0xf) {
  542. case 0:
  543. mmc->version = SD_VERSION_1_0;
  544. break;
  545. case 1:
  546. mmc->version = SD_VERSION_1_10;
  547. break;
  548. case 2:
  549. mmc->version = SD_VERSION_2;
  550. if ((mmc->scr[0] >> 15) & 0x1)
  551. mmc->version = SD_VERSION_3;
  552. break;
  553. default:
  554. mmc->version = SD_VERSION_1_0;
  555. break;
  556. }
  557. if (mmc->scr[0] & SD_DATA_4BIT)
  558. mmc->card_caps |= MMC_MODE_4BIT;
  559. /* Version 1.0 doesn't support switching */
  560. if (mmc->version == SD_VERSION_1_0)
  561. return 0;
  562. timeout = 4;
  563. while (timeout--) {
  564. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  565. (u8 *)switch_status);
  566. if (err)
  567. return err;
  568. /* The high-speed function is busy. Try again */
  569. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  570. break;
  571. }
  572. /* If high-speed isn't supported, we return */
  573. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  574. return 0;
  575. /*
  576. * If the host doesn't support SD_HIGHSPEED, do not switch card to
  577. * HIGHSPEED mode even if the card support SD_HIGHSPPED.
  578. * This can avoid furthur problem when the card runs in different
  579. * mode between the host.
  580. */
  581. if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
  582. (mmc->cfg->host_caps & MMC_MODE_HS)))
  583. return 0;
  584. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
  585. if (err)
  586. return err;
  587. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  588. mmc->card_caps |= MMC_MODE_HS;
  589. return 0;
  590. }
  591. /* frequency bases */
  592. /* divided by 10 to be nice to platforms without floating point */
  593. static const int fbase[] = {
  594. 10000,
  595. 100000,
  596. 1000000,
  597. 10000000,
  598. };
  599. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  600. * to platforms without floating point.
  601. */
  602. static const int multipliers[] = {
  603. 0, /* reserved */
  604. 10,
  605. 12,
  606. 13,
  607. 15,
  608. 20,
  609. 25,
  610. 30,
  611. 35,
  612. 40,
  613. 45,
  614. 50,
  615. 55,
  616. 60,
  617. 70,
  618. 80,
  619. };
  620. static void mmc_set_ios(struct mmc *mmc)
  621. {
  622. if (mmc->cfg->ops->set_ios)
  623. mmc->cfg->ops->set_ios(mmc);
  624. }
  625. void mmc_set_clock(struct mmc *mmc, uint clock)
  626. {
  627. if (clock > mmc->cfg->f_max)
  628. clock = mmc->cfg->f_max;
  629. if (clock < mmc->cfg->f_min)
  630. clock = mmc->cfg->f_min;
  631. mmc->clock = clock;
  632. mmc_set_ios(mmc);
  633. }
  634. static void mmc_set_bus_width(struct mmc *mmc, uint width)
  635. {
  636. mmc->bus_width = width;
  637. mmc_set_ios(mmc);
  638. }
  639. static int mmc_startup(struct mmc *mmc)
  640. {
  641. int err, i;
  642. uint mult, freq;
  643. u64 cmult, csize, capacity;
  644. struct mmc_cmd cmd;
  645. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  646. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  647. int timeout = 1000;
  648. #ifdef CONFIG_MMC_SPI_CRC_ON
  649. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  650. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  651. cmd.resp_type = MMC_RSP_R1;
  652. cmd.cmdarg = 1;
  653. err = mmc_send_cmd(mmc, &cmd, NULL);
  654. if (err)
  655. return err;
  656. }
  657. #endif
  658. /* Put the Card in Identify Mode */
  659. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  660. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  661. cmd.resp_type = MMC_RSP_R2;
  662. cmd.cmdarg = 0;
  663. err = mmc_send_cmd(mmc, &cmd, NULL);
  664. if (err)
  665. return err;
  666. memcpy(mmc->cid, cmd.response, 16);
  667. /*
  668. * For MMC cards, set the Relative Address.
  669. * For SD cards, get the Relatvie Address.
  670. * This also puts the cards into Standby State
  671. */
  672. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  673. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  674. cmd.cmdarg = mmc->rca << 16;
  675. cmd.resp_type = MMC_RSP_R6;
  676. err = mmc_send_cmd(mmc, &cmd, NULL);
  677. if (err)
  678. return err;
  679. if (IS_SD(mmc))
  680. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  681. }
  682. /* Get the Card-Specific Data */
  683. cmd.cmdidx = MMC_CMD_SEND_CSD;
  684. cmd.resp_type = MMC_RSP_R2;
  685. cmd.cmdarg = mmc->rca << 16;
  686. err = mmc_send_cmd(mmc, &cmd, NULL);
  687. /* Waiting for the ready status */
  688. mmc_send_status(mmc, timeout);
  689. if (err)
  690. return err;
  691. mmc->csd[0] = cmd.response[0];
  692. mmc->csd[1] = cmd.response[1];
  693. mmc->csd[2] = cmd.response[2];
  694. mmc->csd[3] = cmd.response[3];
  695. if (mmc->version == MMC_VERSION_UNKNOWN) {
  696. int version = (cmd.response[0] >> 26) & 0xf;
  697. switch (version) {
  698. case 0:
  699. mmc->version = MMC_VERSION_1_2;
  700. break;
  701. case 1:
  702. mmc->version = MMC_VERSION_1_4;
  703. break;
  704. case 2:
  705. mmc->version = MMC_VERSION_2_2;
  706. break;
  707. case 3:
  708. mmc->version = MMC_VERSION_3;
  709. break;
  710. case 4:
  711. mmc->version = MMC_VERSION_4;
  712. break;
  713. default:
  714. mmc->version = MMC_VERSION_1_2;
  715. break;
  716. }
  717. }
  718. /* divide frequency by 10, since the mults are 10x bigger */
  719. freq = fbase[(cmd.response[0] & 0x7)];
  720. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  721. mmc->tran_speed = freq * mult;
  722. mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
  723. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  724. if (IS_SD(mmc))
  725. mmc->write_bl_len = mmc->read_bl_len;
  726. else
  727. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  728. if (mmc->high_capacity) {
  729. csize = (mmc->csd[1] & 0x3f) << 16
  730. | (mmc->csd[2] & 0xffff0000) >> 16;
  731. cmult = 8;
  732. } else {
  733. csize = (mmc->csd[1] & 0x3ff) << 2
  734. | (mmc->csd[2] & 0xc0000000) >> 30;
  735. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  736. }
  737. mmc->capacity_user = (csize + 1) << (cmult + 2);
  738. mmc->capacity_user *= mmc->read_bl_len;
  739. mmc->capacity_boot = 0;
  740. mmc->capacity_rpmb = 0;
  741. for (i = 0; i < 4; i++)
  742. mmc->capacity_gp[i] = 0;
  743. if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
  744. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  745. if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
  746. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  747. if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
  748. cmd.cmdidx = MMC_CMD_SET_DSR;
  749. cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
  750. cmd.resp_type = MMC_RSP_NONE;
  751. if (mmc_send_cmd(mmc, &cmd, NULL))
  752. printf("MMC: SET_DSR failed\n");
  753. }
  754. /* Select the card, and put it into Transfer Mode */
  755. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  756. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  757. cmd.resp_type = MMC_RSP_R1;
  758. cmd.cmdarg = mmc->rca << 16;
  759. err = mmc_send_cmd(mmc, &cmd, NULL);
  760. if (err)
  761. return err;
  762. }
  763. /*
  764. * For SD, its erase group is always one sector
  765. */
  766. mmc->erase_grp_size = 1;
  767. mmc->part_config = MMCPART_NOAVAILABLE;
  768. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  769. /* check ext_csd version and capacity */
  770. err = mmc_send_ext_csd(mmc, ext_csd);
  771. if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
  772. /*
  773. * According to the JEDEC Standard, the value of
  774. * ext_csd's capacity is valid if the value is more
  775. * than 2GB
  776. */
  777. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  778. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  779. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  780. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  781. capacity *= MMC_MAX_BLOCK_LEN;
  782. if ((capacity >> 20) > 2 * 1024)
  783. mmc->capacity_user = capacity;
  784. }
  785. switch (ext_csd[EXT_CSD_REV]) {
  786. case 1:
  787. mmc->version = MMC_VERSION_4_1;
  788. break;
  789. case 2:
  790. mmc->version = MMC_VERSION_4_2;
  791. break;
  792. case 3:
  793. mmc->version = MMC_VERSION_4_3;
  794. break;
  795. case 5:
  796. mmc->version = MMC_VERSION_4_41;
  797. break;
  798. case 6:
  799. mmc->version = MMC_VERSION_4_5;
  800. break;
  801. }
  802. /*
  803. * Host needs to enable ERASE_GRP_DEF bit if device is
  804. * partitioned. This bit will be lost every time after a reset
  805. * or power off. This will affect erase size.
  806. */
  807. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
  808. (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
  809. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  810. EXT_CSD_ERASE_GROUP_DEF, 1);
  811. if (err)
  812. return err;
  813. else
  814. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  815. /* Read out group size from ext_csd */
  816. mmc->erase_grp_size =
  817. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
  818. MMC_MAX_BLOCK_LEN * 1024;
  819. } else {
  820. /* Calculate the group size from the csd value. */
  821. int erase_gsz, erase_gmul;
  822. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  823. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  824. mmc->erase_grp_size = (erase_gsz + 1)
  825. * (erase_gmul + 1);
  826. }
  827. /* store the partition info of emmc */
  828. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  829. ext_csd[EXT_CSD_BOOT_MULT])
  830. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  831. mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  832. mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
  833. for (i = 0; i < 4; i++) {
  834. int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
  835. mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
  836. (ext_csd[idx + 1] << 8) + ext_csd[idx];
  837. mmc->capacity_gp[i] *=
  838. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  839. mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  840. }
  841. }
  842. err = mmc_set_capacity(mmc, mmc->part_num);
  843. if (err)
  844. return err;
  845. if (IS_SD(mmc))
  846. err = sd_change_freq(mmc);
  847. else
  848. err = mmc_change_freq(mmc);
  849. if (err)
  850. return err;
  851. /* Restrict card's capabilities by what the host can do */
  852. mmc->card_caps &= mmc->cfg->host_caps;
  853. if (IS_SD(mmc)) {
  854. if (mmc->card_caps & MMC_MODE_4BIT) {
  855. cmd.cmdidx = MMC_CMD_APP_CMD;
  856. cmd.resp_type = MMC_RSP_R1;
  857. cmd.cmdarg = mmc->rca << 16;
  858. err = mmc_send_cmd(mmc, &cmd, NULL);
  859. if (err)
  860. return err;
  861. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  862. cmd.resp_type = MMC_RSP_R1;
  863. cmd.cmdarg = 2;
  864. err = mmc_send_cmd(mmc, &cmd, NULL);
  865. if (err)
  866. return err;
  867. mmc_set_bus_width(mmc, 4);
  868. }
  869. if (mmc->card_caps & MMC_MODE_HS)
  870. mmc->tran_speed = 50000000;
  871. else
  872. mmc->tran_speed = 25000000;
  873. } else {
  874. int idx;
  875. /* An array of possible bus widths in order of preference */
  876. static unsigned ext_csd_bits[] = {
  877. EXT_CSD_DDR_BUS_WIDTH_8,
  878. EXT_CSD_DDR_BUS_WIDTH_4,
  879. EXT_CSD_BUS_WIDTH_8,
  880. EXT_CSD_BUS_WIDTH_4,
  881. EXT_CSD_BUS_WIDTH_1,
  882. };
  883. /* An array to map CSD bus widths to host cap bits */
  884. static unsigned ext_to_hostcaps[] = {
  885. [EXT_CSD_DDR_BUS_WIDTH_4] = MMC_MODE_DDR_52MHz,
  886. [EXT_CSD_DDR_BUS_WIDTH_8] = MMC_MODE_DDR_52MHz,
  887. [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
  888. [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
  889. };
  890. /* An array to map chosen bus width to an integer */
  891. static unsigned widths[] = {
  892. 8, 4, 8, 4, 1,
  893. };
  894. for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
  895. unsigned int extw = ext_csd_bits[idx];
  896. /*
  897. * Check to make sure the controller supports
  898. * this bus width, if it's more than 1
  899. */
  900. if (extw != EXT_CSD_BUS_WIDTH_1 &&
  901. !(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
  902. continue;
  903. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  904. EXT_CSD_BUS_WIDTH, extw);
  905. if (err)
  906. continue;
  907. mmc_set_bus_width(mmc, widths[idx]);
  908. err = mmc_send_ext_csd(mmc, test_csd);
  909. if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
  910. == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
  911. && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
  912. == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
  913. && ext_csd[EXT_CSD_REV] \
  914. == test_csd[EXT_CSD_REV]
  915. && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
  916. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  917. && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
  918. &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
  919. mmc->card_caps |= ext_to_hostcaps[extw];
  920. break;
  921. }
  922. }
  923. if (mmc->card_caps & MMC_MODE_HS) {
  924. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  925. mmc->tran_speed = 52000000;
  926. else
  927. mmc->tran_speed = 26000000;
  928. }
  929. }
  930. mmc_set_clock(mmc, mmc->tran_speed);
  931. /* fill in device description */
  932. mmc->block_dev.lun = 0;
  933. mmc->block_dev.type = 0;
  934. mmc->block_dev.blksz = mmc->read_bl_len;
  935. mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
  936. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  937. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  938. sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
  939. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  940. (mmc->cid[3] >> 16) & 0xffff);
  941. sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  942. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  943. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  944. (mmc->cid[2] >> 24) & 0xff);
  945. sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  946. (mmc->cid[2] >> 16) & 0xf);
  947. #else
  948. mmc->block_dev.vendor[0] = 0;
  949. mmc->block_dev.product[0] = 0;
  950. mmc->block_dev.revision[0] = 0;
  951. #endif
  952. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  953. init_part(&mmc->block_dev);
  954. #endif
  955. return 0;
  956. }
  957. static int mmc_send_if_cond(struct mmc *mmc)
  958. {
  959. struct mmc_cmd cmd;
  960. int err;
  961. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  962. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  963. cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
  964. cmd.resp_type = MMC_RSP_R7;
  965. err = mmc_send_cmd(mmc, &cmd, NULL);
  966. if (err)
  967. return err;
  968. if ((cmd.response[0] & 0xff) != 0xaa)
  969. return UNUSABLE_ERR;
  970. else
  971. mmc->version = SD_VERSION_2;
  972. return 0;
  973. }
  974. /* not used any more */
  975. int __deprecated mmc_register(struct mmc *mmc)
  976. {
  977. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  978. printf("%s is deprecated! use mmc_create() instead.\n", __func__);
  979. #endif
  980. return -1;
  981. }
  982. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
  983. {
  984. struct mmc *mmc;
  985. /* quick validation */
  986. if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
  987. cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
  988. return NULL;
  989. mmc = calloc(1, sizeof(*mmc));
  990. if (mmc == NULL)
  991. return NULL;
  992. mmc->cfg = cfg;
  993. mmc->priv = priv;
  994. /* the following chunk was mmc_register() */
  995. /* Setup dsr related values */
  996. mmc->dsr_imp = 0;
  997. mmc->dsr = 0xffffffff;
  998. /* Setup the universal parts of the block interface just once */
  999. mmc->block_dev.if_type = IF_TYPE_MMC;
  1000. mmc->block_dev.dev = cur_dev_num++;
  1001. mmc->block_dev.removable = 1;
  1002. mmc->block_dev.block_read = mmc_bread;
  1003. mmc->block_dev.block_write = mmc_bwrite;
  1004. mmc->block_dev.block_erase = mmc_berase;
  1005. /* setup initial part type */
  1006. mmc->block_dev.part_type = mmc->cfg->part_type;
  1007. INIT_LIST_HEAD(&mmc->link);
  1008. list_add_tail(&mmc->link, &mmc_devices);
  1009. return mmc;
  1010. }
  1011. void mmc_destroy(struct mmc *mmc)
  1012. {
  1013. /* only freeing memory for now */
  1014. free(mmc);
  1015. }
  1016. #ifdef CONFIG_PARTITIONS
  1017. block_dev_desc_t *mmc_get_dev(int dev)
  1018. {
  1019. struct mmc *mmc = find_mmc_device(dev);
  1020. if (!mmc || mmc_init(mmc))
  1021. return NULL;
  1022. return &mmc->block_dev;
  1023. }
  1024. #endif
  1025. int mmc_start_init(struct mmc *mmc)
  1026. {
  1027. int err;
  1028. /* we pretend there's no card when init is NULL */
  1029. if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
  1030. mmc->has_init = 0;
  1031. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1032. printf("MMC: no card present\n");
  1033. #endif
  1034. return NO_CARD_ERR;
  1035. }
  1036. if (mmc->has_init)
  1037. return 0;
  1038. /* made sure it's not NULL earlier */
  1039. err = mmc->cfg->ops->init(mmc);
  1040. if (err)
  1041. return err;
  1042. mmc_set_bus_width(mmc, 1);
  1043. mmc_set_clock(mmc, 1);
  1044. /* Reset the Card */
  1045. err = mmc_go_idle(mmc);
  1046. if (err)
  1047. return err;
  1048. /* The internal partition reset to user partition(0) at every CMD0*/
  1049. mmc->part_num = 0;
  1050. /* Test for SD version 2 */
  1051. err = mmc_send_if_cond(mmc);
  1052. /* Now try to get the SD card's operating condition */
  1053. err = sd_send_op_cond(mmc);
  1054. /* If the command timed out, we check for an MMC card */
  1055. if (err == TIMEOUT) {
  1056. err = mmc_send_op_cond(mmc);
  1057. if (err && err != IN_PROGRESS) {
  1058. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1059. printf("Card did not respond to voltage select!\n");
  1060. #endif
  1061. return UNUSABLE_ERR;
  1062. }
  1063. }
  1064. if (err == IN_PROGRESS)
  1065. mmc->init_in_progress = 1;
  1066. return err;
  1067. }
  1068. static int mmc_complete_init(struct mmc *mmc)
  1069. {
  1070. int err = 0;
  1071. if (mmc->op_cond_pending)
  1072. err = mmc_complete_op_cond(mmc);
  1073. if (!err)
  1074. err = mmc_startup(mmc);
  1075. if (err)
  1076. mmc->has_init = 0;
  1077. else
  1078. mmc->has_init = 1;
  1079. mmc->init_in_progress = 0;
  1080. return err;
  1081. }
  1082. int mmc_init(struct mmc *mmc)
  1083. {
  1084. int err = IN_PROGRESS;
  1085. unsigned start;
  1086. if (mmc->has_init)
  1087. return 0;
  1088. start = get_timer(0);
  1089. if (!mmc->init_in_progress)
  1090. err = mmc_start_init(mmc);
  1091. if (!err || err == IN_PROGRESS)
  1092. err = mmc_complete_init(mmc);
  1093. debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
  1094. return err;
  1095. }
  1096. int mmc_set_dsr(struct mmc *mmc, u16 val)
  1097. {
  1098. mmc->dsr = val;
  1099. return 0;
  1100. }
  1101. /* CPU-specific MMC initializations */
  1102. __weak int cpu_mmc_init(bd_t *bis)
  1103. {
  1104. return -1;
  1105. }
  1106. /* board-specific MMC initializations. */
  1107. __weak int board_mmc_init(bd_t *bis)
  1108. {
  1109. return -1;
  1110. }
  1111. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1112. void print_mmc_devices(char separator)
  1113. {
  1114. struct mmc *m;
  1115. struct list_head *entry;
  1116. list_for_each(entry, &mmc_devices) {
  1117. m = list_entry(entry, struct mmc, link);
  1118. printf("%s: %d", m->cfg->name, m->block_dev.dev);
  1119. if (entry->next != &mmc_devices)
  1120. printf("%c ", separator);
  1121. }
  1122. printf("\n");
  1123. }
  1124. #else
  1125. void print_mmc_devices(char separator) { }
  1126. #endif
  1127. int get_mmc_num(void)
  1128. {
  1129. return cur_dev_num;
  1130. }
  1131. void mmc_set_preinit(struct mmc *mmc, int preinit)
  1132. {
  1133. mmc->preinit = preinit;
  1134. }
  1135. static void do_preinit(void)
  1136. {
  1137. struct mmc *m;
  1138. struct list_head *entry;
  1139. list_for_each(entry, &mmc_devices) {
  1140. m = list_entry(entry, struct mmc, link);
  1141. if (m->preinit)
  1142. mmc_start_init(m);
  1143. }
  1144. }
  1145. int mmc_initialize(bd_t *bis)
  1146. {
  1147. INIT_LIST_HEAD (&mmc_devices);
  1148. cur_dev_num = 0;
  1149. if (board_mmc_init(bis) < 0)
  1150. cpu_mmc_init(bis);
  1151. #ifndef CONFIG_SPL_BUILD
  1152. print_mmc_devices(',');
  1153. #endif
  1154. do_preinit();
  1155. return 0;
  1156. }
  1157. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  1158. /*
  1159. * This function changes the size of boot partition and the size of rpmb
  1160. * partition present on EMMC devices.
  1161. *
  1162. * Input Parameters:
  1163. * struct *mmc: pointer for the mmc device strcuture
  1164. * bootsize: size of boot partition
  1165. * rpmbsize: size of rpmb partition
  1166. *
  1167. * Returns 0 on success.
  1168. */
  1169. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  1170. unsigned long rpmbsize)
  1171. {
  1172. int err;
  1173. struct mmc_cmd cmd;
  1174. /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
  1175. cmd.cmdidx = MMC_CMD_RES_MAN;
  1176. cmd.resp_type = MMC_RSP_R1b;
  1177. cmd.cmdarg = MMC_CMD62_ARG1;
  1178. err = mmc_send_cmd(mmc, &cmd, NULL);
  1179. if (err) {
  1180. debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
  1181. return err;
  1182. }
  1183. /* Boot partition changing mode */
  1184. cmd.cmdidx = MMC_CMD_RES_MAN;
  1185. cmd.resp_type = MMC_RSP_R1b;
  1186. cmd.cmdarg = MMC_CMD62_ARG2;
  1187. err = mmc_send_cmd(mmc, &cmd, NULL);
  1188. if (err) {
  1189. debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
  1190. return err;
  1191. }
  1192. /* boot partition size is multiple of 128KB */
  1193. bootsize = (bootsize * 1024) / 128;
  1194. /* Arg: boot partition size */
  1195. cmd.cmdidx = MMC_CMD_RES_MAN;
  1196. cmd.resp_type = MMC_RSP_R1b;
  1197. cmd.cmdarg = bootsize;
  1198. err = mmc_send_cmd(mmc, &cmd, NULL);
  1199. if (err) {
  1200. debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
  1201. return err;
  1202. }
  1203. /* RPMB partition size is multiple of 128KB */
  1204. rpmbsize = (rpmbsize * 1024) / 128;
  1205. /* Arg: RPMB partition size */
  1206. cmd.cmdidx = MMC_CMD_RES_MAN;
  1207. cmd.resp_type = MMC_RSP_R1b;
  1208. cmd.cmdarg = rpmbsize;
  1209. err = mmc_send_cmd(mmc, &cmd, NULL);
  1210. if (err) {
  1211. debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
  1212. return err;
  1213. }
  1214. return 0;
  1215. }
  1216. /*
  1217. * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
  1218. * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
  1219. * and BOOT_MODE.
  1220. *
  1221. * Returns 0 on success.
  1222. */
  1223. int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
  1224. {
  1225. int err;
  1226. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
  1227. EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
  1228. EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
  1229. EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
  1230. if (err)
  1231. return err;
  1232. return 0;
  1233. }
  1234. /*
  1235. * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
  1236. * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
  1237. * PARTITION_ACCESS.
  1238. *
  1239. * Returns 0 on success.
  1240. */
  1241. int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  1242. {
  1243. int err;
  1244. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  1245. EXT_CSD_BOOT_ACK(ack) |
  1246. EXT_CSD_BOOT_PART_NUM(part_num) |
  1247. EXT_CSD_PARTITION_ACCESS(access));
  1248. if (err)
  1249. return err;
  1250. return 0;
  1251. }
  1252. /*
  1253. * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
  1254. * for enable. Note that this is a write-once field for non-zero values.
  1255. *
  1256. * Returns 0 on success.
  1257. */
  1258. int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
  1259. {
  1260. return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
  1261. enable);
  1262. }
  1263. #endif