mmc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. * Copyright 2008, Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based vaguely on the Linux code
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <config.h>
  26. #include <common.h>
  27. #include <command.h>
  28. #include <mmc.h>
  29. #include <part.h>
  30. #include <malloc.h>
  31. #include <linux/list.h>
  32. #include <div64.h>
  33. /* Set block count limit because of 16 bit register limit on some hardware*/
  34. #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
  35. #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
  36. #endif
  37. static struct list_head mmc_devices;
  38. static int cur_dev_num = -1;
  39. int __board_mmc_getcd(struct mmc *mmc) {
  40. return -1;
  41. }
  42. int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
  43. alias("__board_mmc_getcd")));
  44. int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  45. {
  46. struct mmc_data backup;
  47. int ret;
  48. memset(&backup, 0, sizeof(backup));
  49. #ifdef CONFIG_MMC_TRACE
  50. int i;
  51. u8 *ptr;
  52. printf("CMD_SEND:%d\n", cmd->cmdidx);
  53. printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
  54. ret = mmc->send_cmd(mmc, cmd, data);
  55. switch (cmd->resp_type) {
  56. case MMC_RSP_NONE:
  57. printf("\t\tMMC_RSP_NONE\n");
  58. break;
  59. case MMC_RSP_R1:
  60. printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
  61. cmd->response[0]);
  62. break;
  63. case MMC_RSP_R1b:
  64. printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
  65. cmd->response[0]);
  66. break;
  67. case MMC_RSP_R2:
  68. printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
  69. cmd->response[0]);
  70. printf("\t\t \t\t 0x%08X \n",
  71. cmd->response[1]);
  72. printf("\t\t \t\t 0x%08X \n",
  73. cmd->response[2]);
  74. printf("\t\t \t\t 0x%08X \n",
  75. cmd->response[3]);
  76. printf("\n");
  77. printf("\t\t\t\t\tDUMPING DATA\n");
  78. for (i = 0; i < 4; i++) {
  79. int j;
  80. printf("\t\t\t\t\t%03d - ", i*4);
  81. ptr = (u8 *)&cmd->response[i];
  82. ptr += 3;
  83. for (j = 0; j < 4; j++)
  84. printf("%02X ", *ptr--);
  85. printf("\n");
  86. }
  87. break;
  88. case MMC_RSP_R3:
  89. printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
  90. cmd->response[0]);
  91. break;
  92. default:
  93. printf("\t\tERROR MMC rsp not supported\n");
  94. break;
  95. }
  96. #else
  97. ret = mmc->send_cmd(mmc, cmd, data);
  98. #endif
  99. return ret;
  100. }
  101. int mmc_send_status(struct mmc *mmc, int timeout)
  102. {
  103. struct mmc_cmd cmd;
  104. int err, retries = 5;
  105. #ifdef CONFIG_MMC_TRACE
  106. int status;
  107. #endif
  108. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  109. cmd.resp_type = MMC_RSP_R1;
  110. if (!mmc_host_is_spi(mmc))
  111. cmd.cmdarg = mmc->rca << 16;
  112. do {
  113. err = mmc_send_cmd(mmc, &cmd, NULL);
  114. if (!err) {
  115. if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
  116. (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
  117. MMC_STATE_PRG)
  118. break;
  119. else if (cmd.response[0] & MMC_STATUS_MASK) {
  120. printf("Status Error: 0x%08X\n",
  121. cmd.response[0]);
  122. return COMM_ERR;
  123. }
  124. } else if (--retries < 0)
  125. return err;
  126. udelay(1000);
  127. } while (timeout--);
  128. #ifdef CONFIG_MMC_TRACE
  129. status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
  130. printf("CURR STATE:%d\n", status);
  131. #endif
  132. if (timeout <= 0) {
  133. printf("Timeout waiting card ready\n");
  134. return TIMEOUT;
  135. }
  136. return 0;
  137. }
  138. int mmc_set_blocklen(struct mmc *mmc, int len)
  139. {
  140. struct mmc_cmd cmd;
  141. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  142. cmd.resp_type = MMC_RSP_R1;
  143. cmd.cmdarg = len;
  144. return mmc_send_cmd(mmc, &cmd, NULL);
  145. }
  146. struct mmc *find_mmc_device(int dev_num)
  147. {
  148. struct mmc *m;
  149. struct list_head *entry;
  150. list_for_each(entry, &mmc_devices) {
  151. m = list_entry(entry, struct mmc, link);
  152. if (m->block_dev.dev == dev_num)
  153. return m;
  154. }
  155. printf("MMC Device %d not found\n", dev_num);
  156. return NULL;
  157. }
  158. static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
  159. {
  160. struct mmc_cmd cmd;
  161. ulong end;
  162. int err, start_cmd, end_cmd;
  163. if (mmc->high_capacity)
  164. end = start + blkcnt - 1;
  165. else {
  166. end = (start + blkcnt - 1) * mmc->write_bl_len;
  167. start *= mmc->write_bl_len;
  168. }
  169. if (IS_SD(mmc)) {
  170. start_cmd = SD_CMD_ERASE_WR_BLK_START;
  171. end_cmd = SD_CMD_ERASE_WR_BLK_END;
  172. } else {
  173. start_cmd = MMC_CMD_ERASE_GROUP_START;
  174. end_cmd = MMC_CMD_ERASE_GROUP_END;
  175. }
  176. cmd.cmdidx = start_cmd;
  177. cmd.cmdarg = start;
  178. cmd.resp_type = MMC_RSP_R1;
  179. err = mmc_send_cmd(mmc, &cmd, NULL);
  180. if (err)
  181. goto err_out;
  182. cmd.cmdidx = end_cmd;
  183. cmd.cmdarg = end;
  184. err = mmc_send_cmd(mmc, &cmd, NULL);
  185. if (err)
  186. goto err_out;
  187. cmd.cmdidx = MMC_CMD_ERASE;
  188. cmd.cmdarg = SECURE_ERASE;
  189. cmd.resp_type = MMC_RSP_R1b;
  190. err = mmc_send_cmd(mmc, &cmd, NULL);
  191. if (err)
  192. goto err_out;
  193. return 0;
  194. err_out:
  195. puts("mmc erase failed\n");
  196. return err;
  197. }
  198. static unsigned long
  199. mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
  200. {
  201. int err = 0;
  202. struct mmc *mmc = find_mmc_device(dev_num);
  203. lbaint_t blk = 0, blk_r = 0;
  204. int timeout = 1000;
  205. if (!mmc)
  206. return -1;
  207. if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
  208. printf("\n\nCaution! Your devices Erase group is 0x%x\n"
  209. "The erase range would be change to 0x%lx~0x%lx\n\n",
  210. mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
  211. ((start + blkcnt + mmc->erase_grp_size)
  212. & ~(mmc->erase_grp_size - 1)) - 1);
  213. while (blk < blkcnt) {
  214. blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
  215. mmc->erase_grp_size : (blkcnt - blk);
  216. err = mmc_erase_t(mmc, start + blk, blk_r);
  217. if (err)
  218. break;
  219. blk += blk_r;
  220. /* Waiting for the ready status */
  221. if (mmc_send_status(mmc, timeout))
  222. return 0;
  223. }
  224. return blk;
  225. }
  226. static ulong
  227. mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
  228. {
  229. struct mmc_cmd cmd;
  230. struct mmc_data data;
  231. int timeout = 1000;
  232. if ((start + blkcnt) > mmc->block_dev.lba) {
  233. printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
  234. start + blkcnt, mmc->block_dev.lba);
  235. return 0;
  236. }
  237. if (blkcnt > 1)
  238. cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
  239. else
  240. cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
  241. if (mmc->high_capacity)
  242. cmd.cmdarg = start;
  243. else
  244. cmd.cmdarg = start * mmc->write_bl_len;
  245. cmd.resp_type = MMC_RSP_R1;
  246. data.src = src;
  247. data.blocks = blkcnt;
  248. data.blocksize = mmc->write_bl_len;
  249. data.flags = MMC_DATA_WRITE;
  250. if (mmc_send_cmd(mmc, &cmd, &data)) {
  251. printf("mmc write failed\n");
  252. return 0;
  253. }
  254. /* SPI multiblock writes terminate using a special
  255. * token, not a STOP_TRANSMISSION request.
  256. */
  257. if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
  258. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  259. cmd.cmdarg = 0;
  260. cmd.resp_type = MMC_RSP_R1b;
  261. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  262. printf("mmc fail to send stop cmd\n");
  263. return 0;
  264. }
  265. }
  266. /* Waiting for the ready status */
  267. if (mmc_send_status(mmc, timeout))
  268. return 0;
  269. return blkcnt;
  270. }
  271. static ulong
  272. mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
  273. {
  274. lbaint_t cur, blocks_todo = blkcnt;
  275. struct mmc *mmc = find_mmc_device(dev_num);
  276. if (!mmc)
  277. return 0;
  278. if (mmc_set_blocklen(mmc, mmc->write_bl_len))
  279. return 0;
  280. do {
  281. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  282. if(mmc_write_blocks(mmc, start, cur, src) != cur)
  283. return 0;
  284. blocks_todo -= cur;
  285. start += cur;
  286. src += cur * mmc->write_bl_len;
  287. } while (blocks_todo > 0);
  288. return blkcnt;
  289. }
  290. int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
  291. {
  292. struct mmc_cmd cmd;
  293. struct mmc_data data;
  294. if (blkcnt > 1)
  295. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  296. else
  297. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  298. if (mmc->high_capacity)
  299. cmd.cmdarg = start;
  300. else
  301. cmd.cmdarg = start * mmc->read_bl_len;
  302. cmd.resp_type = MMC_RSP_R1;
  303. data.dest = dst;
  304. data.blocks = blkcnt;
  305. data.blocksize = mmc->read_bl_len;
  306. data.flags = MMC_DATA_READ;
  307. if (mmc_send_cmd(mmc, &cmd, &data))
  308. return 0;
  309. if (blkcnt > 1) {
  310. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  311. cmd.cmdarg = 0;
  312. cmd.resp_type = MMC_RSP_R1b;
  313. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  314. printf("mmc fail to send stop cmd\n");
  315. return 0;
  316. }
  317. }
  318. return blkcnt;
  319. }
  320. static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
  321. {
  322. lbaint_t cur, blocks_todo = blkcnt;
  323. if (blkcnt == 0)
  324. return 0;
  325. struct mmc *mmc = find_mmc_device(dev_num);
  326. if (!mmc)
  327. return 0;
  328. if ((start + blkcnt) > mmc->block_dev.lba) {
  329. printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
  330. start + blkcnt, mmc->block_dev.lba);
  331. return 0;
  332. }
  333. if (mmc_set_blocklen(mmc, mmc->read_bl_len))
  334. return 0;
  335. do {
  336. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  337. if(mmc_read_blocks(mmc, dst, start, cur) != cur)
  338. return 0;
  339. blocks_todo -= cur;
  340. start += cur;
  341. dst += cur * mmc->read_bl_len;
  342. } while (blocks_todo > 0);
  343. return blkcnt;
  344. }
  345. int mmc_go_idle(struct mmc* mmc)
  346. {
  347. struct mmc_cmd cmd;
  348. int err;
  349. udelay(1000);
  350. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  351. cmd.cmdarg = 0;
  352. cmd.resp_type = MMC_RSP_NONE;
  353. err = mmc_send_cmd(mmc, &cmd, NULL);
  354. if (err)
  355. return err;
  356. udelay(2000);
  357. return 0;
  358. }
  359. int
  360. sd_send_op_cond(struct mmc *mmc)
  361. {
  362. int timeout = 1000;
  363. int err;
  364. struct mmc_cmd cmd;
  365. do {
  366. cmd.cmdidx = MMC_CMD_APP_CMD;
  367. cmd.resp_type = MMC_RSP_R1;
  368. cmd.cmdarg = 0;
  369. err = mmc_send_cmd(mmc, &cmd, NULL);
  370. if (err)
  371. return err;
  372. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  373. cmd.resp_type = MMC_RSP_R3;
  374. /*
  375. * Most cards do not answer if some reserved bits
  376. * in the ocr are set. However, Some controller
  377. * can set bit 7 (reserved for low voltages), but
  378. * how to manage low voltages SD card is not yet
  379. * specified.
  380. */
  381. cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
  382. (mmc->voltages & 0xff8000);
  383. if (mmc->version == SD_VERSION_2)
  384. cmd.cmdarg |= OCR_HCS;
  385. err = mmc_send_cmd(mmc, &cmd, NULL);
  386. if (err)
  387. return err;
  388. udelay(1000);
  389. } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
  390. if (timeout <= 0)
  391. return UNUSABLE_ERR;
  392. if (mmc->version != SD_VERSION_2)
  393. mmc->version = SD_VERSION_1_0;
  394. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  395. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  396. cmd.resp_type = MMC_RSP_R3;
  397. cmd.cmdarg = 0;
  398. err = mmc_send_cmd(mmc, &cmd, NULL);
  399. if (err)
  400. return err;
  401. }
  402. mmc->ocr = cmd.response[0];
  403. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  404. mmc->rca = 0;
  405. return 0;
  406. }
  407. int mmc_send_op_cond(struct mmc *mmc)
  408. {
  409. int timeout = 10000;
  410. struct mmc_cmd cmd;
  411. int err;
  412. /* Some cards seem to need this */
  413. mmc_go_idle(mmc);
  414. /* Asking to the card its capabilities */
  415. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  416. cmd.resp_type = MMC_RSP_R3;
  417. cmd.cmdarg = 0;
  418. err = mmc_send_cmd(mmc, &cmd, NULL);
  419. if (err)
  420. return err;
  421. udelay(1000);
  422. do {
  423. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  424. cmd.resp_type = MMC_RSP_R3;
  425. cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
  426. (mmc->voltages &
  427. (cmd.response[0] & OCR_VOLTAGE_MASK)) |
  428. (cmd.response[0] & OCR_ACCESS_MODE));
  429. if (mmc->host_caps & MMC_MODE_HC)
  430. cmd.cmdarg |= OCR_HCS;
  431. err = mmc_send_cmd(mmc, &cmd, NULL);
  432. if (err)
  433. return err;
  434. udelay(1000);
  435. } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
  436. if (timeout <= 0)
  437. return UNUSABLE_ERR;
  438. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  439. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  440. cmd.resp_type = MMC_RSP_R3;
  441. cmd.cmdarg = 0;
  442. err = mmc_send_cmd(mmc, &cmd, NULL);
  443. if (err)
  444. return err;
  445. }
  446. mmc->version = MMC_VERSION_UNKNOWN;
  447. mmc->ocr = cmd.response[0];
  448. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  449. mmc->rca = 0;
  450. return 0;
  451. }
  452. int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
  453. {
  454. struct mmc_cmd cmd;
  455. struct mmc_data data;
  456. int err;
  457. /* Get the Card Status Register */
  458. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  459. cmd.resp_type = MMC_RSP_R1;
  460. cmd.cmdarg = 0;
  461. data.dest = (char *)ext_csd;
  462. data.blocks = 1;
  463. data.blocksize = 512;
  464. data.flags = MMC_DATA_READ;
  465. err = mmc_send_cmd(mmc, &cmd, &data);
  466. return err;
  467. }
  468. int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
  469. {
  470. struct mmc_cmd cmd;
  471. int timeout = 1000;
  472. int ret;
  473. cmd.cmdidx = MMC_CMD_SWITCH;
  474. cmd.resp_type = MMC_RSP_R1b;
  475. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  476. (index << 16) |
  477. (value << 8);
  478. ret = mmc_send_cmd(mmc, &cmd, NULL);
  479. /* Waiting for the ready status */
  480. if (!ret)
  481. ret = mmc_send_status(mmc, timeout);
  482. return ret;
  483. }
  484. int mmc_change_freq(struct mmc *mmc)
  485. {
  486. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
  487. char cardtype;
  488. int err;
  489. mmc->card_caps = 0;
  490. if (mmc_host_is_spi(mmc))
  491. return 0;
  492. /* Only version 4 supports high-speed */
  493. if (mmc->version < MMC_VERSION_4)
  494. return 0;
  495. err = mmc_send_ext_csd(mmc, ext_csd);
  496. if (err)
  497. return err;
  498. cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
  499. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
  500. if (err)
  501. return err;
  502. /* Now check to see that it worked */
  503. err = mmc_send_ext_csd(mmc, ext_csd);
  504. if (err)
  505. return err;
  506. /* No high-speed support */
  507. if (!ext_csd[EXT_CSD_HS_TIMING])
  508. return 0;
  509. /* High Speed is set, there are two types: 52MHz and 26MHz */
  510. if (cardtype & MMC_HS_52MHZ)
  511. mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  512. else
  513. mmc->card_caps |= MMC_MODE_HS;
  514. return 0;
  515. }
  516. int mmc_switch_part(int dev_num, unsigned int part_num)
  517. {
  518. struct mmc *mmc = find_mmc_device(dev_num);
  519. if (!mmc)
  520. return -1;
  521. return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  522. (mmc->part_config & ~PART_ACCESS_MASK)
  523. | (part_num & PART_ACCESS_MASK));
  524. }
  525. int mmc_getcd(struct mmc *mmc)
  526. {
  527. int cd;
  528. cd = board_mmc_getcd(mmc);
  529. if ((cd < 0) && mmc->getcd)
  530. cd = mmc->getcd(mmc);
  531. return cd;
  532. }
  533. int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  534. {
  535. struct mmc_cmd cmd;
  536. struct mmc_data data;
  537. /* Switch the frequency */
  538. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  539. cmd.resp_type = MMC_RSP_R1;
  540. cmd.cmdarg = (mode << 31) | 0xffffff;
  541. cmd.cmdarg &= ~(0xf << (group * 4));
  542. cmd.cmdarg |= value << (group * 4);
  543. data.dest = (char *)resp;
  544. data.blocksize = 64;
  545. data.blocks = 1;
  546. data.flags = MMC_DATA_READ;
  547. return mmc_send_cmd(mmc, &cmd, &data);
  548. }
  549. int sd_change_freq(struct mmc *mmc)
  550. {
  551. int err;
  552. struct mmc_cmd cmd;
  553. ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
  554. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  555. struct mmc_data data;
  556. int timeout;
  557. mmc->card_caps = 0;
  558. if (mmc_host_is_spi(mmc))
  559. return 0;
  560. /* Read the SCR to find out if this card supports higher speeds */
  561. cmd.cmdidx = MMC_CMD_APP_CMD;
  562. cmd.resp_type = MMC_RSP_R1;
  563. cmd.cmdarg = mmc->rca << 16;
  564. err = mmc_send_cmd(mmc, &cmd, NULL);
  565. if (err)
  566. return err;
  567. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  568. cmd.resp_type = MMC_RSP_R1;
  569. cmd.cmdarg = 0;
  570. timeout = 3;
  571. retry_scr:
  572. data.dest = (char *)scr;
  573. data.blocksize = 8;
  574. data.blocks = 1;
  575. data.flags = MMC_DATA_READ;
  576. err = mmc_send_cmd(mmc, &cmd, &data);
  577. if (err) {
  578. if (timeout--)
  579. goto retry_scr;
  580. return err;
  581. }
  582. mmc->scr[0] = __be32_to_cpu(scr[0]);
  583. mmc->scr[1] = __be32_to_cpu(scr[1]);
  584. switch ((mmc->scr[0] >> 24) & 0xf) {
  585. case 0:
  586. mmc->version = SD_VERSION_1_0;
  587. break;
  588. case 1:
  589. mmc->version = SD_VERSION_1_10;
  590. break;
  591. case 2:
  592. mmc->version = SD_VERSION_2;
  593. break;
  594. default:
  595. mmc->version = SD_VERSION_1_0;
  596. break;
  597. }
  598. if (mmc->scr[0] & SD_DATA_4BIT)
  599. mmc->card_caps |= MMC_MODE_4BIT;
  600. /* Version 1.0 doesn't support switching */
  601. if (mmc->version == SD_VERSION_1_0)
  602. return 0;
  603. timeout = 4;
  604. while (timeout--) {
  605. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  606. (u8 *)switch_status);
  607. if (err)
  608. return err;
  609. /* The high-speed function is busy. Try again */
  610. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  611. break;
  612. }
  613. /* If high-speed isn't supported, we return */
  614. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  615. return 0;
  616. /*
  617. * If the host doesn't support SD_HIGHSPEED, do not switch card to
  618. * HIGHSPEED mode even if the card support SD_HIGHSPPED.
  619. * This can avoid furthur problem when the card runs in different
  620. * mode between the host.
  621. */
  622. if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
  623. (mmc->host_caps & MMC_MODE_HS)))
  624. return 0;
  625. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
  626. if (err)
  627. return err;
  628. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  629. mmc->card_caps |= MMC_MODE_HS;
  630. return 0;
  631. }
  632. /* frequency bases */
  633. /* divided by 10 to be nice to platforms without floating point */
  634. static const int fbase[] = {
  635. 10000,
  636. 100000,
  637. 1000000,
  638. 10000000,
  639. };
  640. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  641. * to platforms without floating point.
  642. */
  643. static const int multipliers[] = {
  644. 0, /* reserved */
  645. 10,
  646. 12,
  647. 13,
  648. 15,
  649. 20,
  650. 25,
  651. 30,
  652. 35,
  653. 40,
  654. 45,
  655. 50,
  656. 55,
  657. 60,
  658. 70,
  659. 80,
  660. };
  661. void mmc_set_ios(struct mmc *mmc)
  662. {
  663. mmc->set_ios(mmc);
  664. }
  665. void mmc_set_clock(struct mmc *mmc, uint clock)
  666. {
  667. if (clock > mmc->f_max)
  668. clock = mmc->f_max;
  669. if (clock < mmc->f_min)
  670. clock = mmc->f_min;
  671. mmc->clock = clock;
  672. mmc_set_ios(mmc);
  673. }
  674. void mmc_set_bus_width(struct mmc *mmc, uint width)
  675. {
  676. mmc->bus_width = width;
  677. mmc_set_ios(mmc);
  678. }
  679. int mmc_startup(struct mmc *mmc)
  680. {
  681. int err, width;
  682. uint mult, freq;
  683. u64 cmult, csize, capacity;
  684. struct mmc_cmd cmd;
  685. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
  686. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
  687. int timeout = 1000;
  688. #ifdef CONFIG_MMC_SPI_CRC_ON
  689. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  690. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  691. cmd.resp_type = MMC_RSP_R1;
  692. cmd.cmdarg = 1;
  693. err = mmc_send_cmd(mmc, &cmd, NULL);
  694. if (err)
  695. return err;
  696. }
  697. #endif
  698. /* Put the Card in Identify Mode */
  699. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  700. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  701. cmd.resp_type = MMC_RSP_R2;
  702. cmd.cmdarg = 0;
  703. err = mmc_send_cmd(mmc, &cmd, NULL);
  704. if (err)
  705. return err;
  706. memcpy(mmc->cid, cmd.response, 16);
  707. /*
  708. * For MMC cards, set the Relative Address.
  709. * For SD cards, get the Relatvie Address.
  710. * This also puts the cards into Standby State
  711. */
  712. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  713. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  714. cmd.cmdarg = mmc->rca << 16;
  715. cmd.resp_type = MMC_RSP_R6;
  716. err = mmc_send_cmd(mmc, &cmd, NULL);
  717. if (err)
  718. return err;
  719. if (IS_SD(mmc))
  720. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  721. }
  722. /* Get the Card-Specific Data */
  723. cmd.cmdidx = MMC_CMD_SEND_CSD;
  724. cmd.resp_type = MMC_RSP_R2;
  725. cmd.cmdarg = mmc->rca << 16;
  726. err = mmc_send_cmd(mmc, &cmd, NULL);
  727. /* Waiting for the ready status */
  728. mmc_send_status(mmc, timeout);
  729. if (err)
  730. return err;
  731. mmc->csd[0] = cmd.response[0];
  732. mmc->csd[1] = cmd.response[1];
  733. mmc->csd[2] = cmd.response[2];
  734. mmc->csd[3] = cmd.response[3];
  735. if (mmc->version == MMC_VERSION_UNKNOWN) {
  736. int version = (cmd.response[0] >> 26) & 0xf;
  737. switch (version) {
  738. case 0:
  739. mmc->version = MMC_VERSION_1_2;
  740. break;
  741. case 1:
  742. mmc->version = MMC_VERSION_1_4;
  743. break;
  744. case 2:
  745. mmc->version = MMC_VERSION_2_2;
  746. break;
  747. case 3:
  748. mmc->version = MMC_VERSION_3;
  749. break;
  750. case 4:
  751. mmc->version = MMC_VERSION_4;
  752. break;
  753. default:
  754. mmc->version = MMC_VERSION_1_2;
  755. break;
  756. }
  757. }
  758. /* divide frequency by 10, since the mults are 10x bigger */
  759. freq = fbase[(cmd.response[0] & 0x7)];
  760. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  761. mmc->tran_speed = freq * mult;
  762. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  763. if (IS_SD(mmc))
  764. mmc->write_bl_len = mmc->read_bl_len;
  765. else
  766. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  767. if (mmc->high_capacity) {
  768. csize = (mmc->csd[1] & 0x3f) << 16
  769. | (mmc->csd[2] & 0xffff0000) >> 16;
  770. cmult = 8;
  771. } else {
  772. csize = (mmc->csd[1] & 0x3ff) << 2
  773. | (mmc->csd[2] & 0xc0000000) >> 30;
  774. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  775. }
  776. mmc->capacity = (csize + 1) << (cmult + 2);
  777. mmc->capacity *= mmc->read_bl_len;
  778. if (mmc->read_bl_len > 512)
  779. mmc->read_bl_len = 512;
  780. if (mmc->write_bl_len > 512)
  781. mmc->write_bl_len = 512;
  782. /* Select the card, and put it into Transfer Mode */
  783. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  784. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  785. cmd.resp_type = MMC_RSP_R1;
  786. cmd.cmdarg = mmc->rca << 16;
  787. err = mmc_send_cmd(mmc, &cmd, NULL);
  788. if (err)
  789. return err;
  790. }
  791. /*
  792. * For SD, its erase group is always one sector
  793. */
  794. mmc->erase_grp_size = 1;
  795. mmc->part_config = MMCPART_NOAVAILABLE;
  796. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  797. /* check ext_csd version and capacity */
  798. err = mmc_send_ext_csd(mmc, ext_csd);
  799. if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
  800. /*
  801. * According to the JEDEC Standard, the value of
  802. * ext_csd's capacity is valid if the value is more
  803. * than 2GB
  804. */
  805. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  806. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  807. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  808. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  809. capacity *= 512;
  810. if ((capacity >> 20) > 2 * 1024)
  811. mmc->capacity = capacity;
  812. }
  813. /*
  814. * Check whether GROUP_DEF is set, if yes, read out
  815. * group size from ext_csd directly, or calculate
  816. * the group size from the csd value.
  817. */
  818. if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
  819. mmc->erase_grp_size =
  820. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
  821. else {
  822. int erase_gsz, erase_gmul;
  823. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  824. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  825. mmc->erase_grp_size = (erase_gsz + 1)
  826. * (erase_gmul + 1);
  827. }
  828. /* store the partition info of emmc */
  829. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  830. ext_csd[EXT_CSD_BOOT_MULT])
  831. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  832. }
  833. if (IS_SD(mmc))
  834. err = sd_change_freq(mmc);
  835. else
  836. err = mmc_change_freq(mmc);
  837. if (err)
  838. return err;
  839. /* Restrict card's capabilities by what the host can do */
  840. mmc->card_caps &= mmc->host_caps;
  841. if (IS_SD(mmc)) {
  842. if (mmc->card_caps & MMC_MODE_4BIT) {
  843. cmd.cmdidx = MMC_CMD_APP_CMD;
  844. cmd.resp_type = MMC_RSP_R1;
  845. cmd.cmdarg = mmc->rca << 16;
  846. err = mmc_send_cmd(mmc, &cmd, NULL);
  847. if (err)
  848. return err;
  849. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  850. cmd.resp_type = MMC_RSP_R1;
  851. cmd.cmdarg = 2;
  852. err = mmc_send_cmd(mmc, &cmd, NULL);
  853. if (err)
  854. return err;
  855. mmc_set_bus_width(mmc, 4);
  856. }
  857. if (mmc->card_caps & MMC_MODE_HS)
  858. mmc->tran_speed = 50000000;
  859. else
  860. mmc->tran_speed = 25000000;
  861. } else {
  862. width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
  863. MMC_MODE_WIDTH_BITS_SHIFT);
  864. for (; width >= 0; width--) {
  865. /* Set the card to use 4 bit*/
  866. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  867. EXT_CSD_BUS_WIDTH, width);
  868. if (err)
  869. continue;
  870. if (!width) {
  871. mmc_set_bus_width(mmc, 1);
  872. break;
  873. } else
  874. mmc_set_bus_width(mmc, 4 * width);
  875. err = mmc_send_ext_csd(mmc, test_csd);
  876. if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
  877. == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
  878. && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
  879. == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
  880. && ext_csd[EXT_CSD_REV] \
  881. == test_csd[EXT_CSD_REV]
  882. && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
  883. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  884. && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
  885. &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
  886. mmc->card_caps |= width;
  887. break;
  888. }
  889. }
  890. if (mmc->card_caps & MMC_MODE_HS) {
  891. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  892. mmc->tran_speed = 52000000;
  893. else
  894. mmc->tran_speed = 26000000;
  895. }
  896. }
  897. mmc_set_clock(mmc, mmc->tran_speed);
  898. /* fill in device description */
  899. mmc->block_dev.lun = 0;
  900. mmc->block_dev.type = 0;
  901. mmc->block_dev.blksz = mmc->read_bl_len;
  902. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  903. sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
  904. (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
  905. sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
  906. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  907. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  908. sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
  909. (mmc->cid[2] >> 24) & 0xf);
  910. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  911. init_part(&mmc->block_dev);
  912. #endif
  913. return 0;
  914. }
  915. int mmc_send_if_cond(struct mmc *mmc)
  916. {
  917. struct mmc_cmd cmd;
  918. int err;
  919. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  920. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  921. cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
  922. cmd.resp_type = MMC_RSP_R7;
  923. err = mmc_send_cmd(mmc, &cmd, NULL);
  924. if (err)
  925. return err;
  926. if ((cmd.response[0] & 0xff) != 0xaa)
  927. return UNUSABLE_ERR;
  928. else
  929. mmc->version = SD_VERSION_2;
  930. return 0;
  931. }
  932. int mmc_register(struct mmc *mmc)
  933. {
  934. /* Setup the universal parts of the block interface just once */
  935. mmc->block_dev.if_type = IF_TYPE_MMC;
  936. mmc->block_dev.dev = cur_dev_num++;
  937. mmc->block_dev.removable = 1;
  938. mmc->block_dev.block_read = mmc_bread;
  939. mmc->block_dev.block_write = mmc_bwrite;
  940. mmc->block_dev.block_erase = mmc_berase;
  941. if (!mmc->b_max)
  942. mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  943. INIT_LIST_HEAD (&mmc->link);
  944. list_add_tail (&mmc->link, &mmc_devices);
  945. return 0;
  946. }
  947. #ifdef CONFIG_PARTITIONS
  948. block_dev_desc_t *mmc_get_dev(int dev)
  949. {
  950. struct mmc *mmc = find_mmc_device(dev);
  951. if (!mmc || mmc_init(mmc))
  952. return NULL;
  953. return &mmc->block_dev;
  954. }
  955. #endif
  956. int mmc_init(struct mmc *mmc)
  957. {
  958. int err;
  959. if (mmc_getcd(mmc) == 0) {
  960. mmc->has_init = 0;
  961. printf("MMC: no card present\n");
  962. return NO_CARD_ERR;
  963. }
  964. if (mmc->has_init)
  965. return 0;
  966. err = mmc->init(mmc);
  967. if (err)
  968. return err;
  969. mmc_set_bus_width(mmc, 1);
  970. mmc_set_clock(mmc, 1);
  971. /* Reset the Card */
  972. err = mmc_go_idle(mmc);
  973. if (err)
  974. return err;
  975. /* The internal partition reset to user partition(0) at every CMD0*/
  976. mmc->part_num = 0;
  977. /* Test for SD version 2 */
  978. err = mmc_send_if_cond(mmc);
  979. /* Now try to get the SD card's operating condition */
  980. err = sd_send_op_cond(mmc);
  981. /* If the command timed out, we check for an MMC card */
  982. if (err == TIMEOUT) {
  983. err = mmc_send_op_cond(mmc);
  984. if (err) {
  985. printf("Card did not respond to voltage select!\n");
  986. return UNUSABLE_ERR;
  987. }
  988. }
  989. err = mmc_startup(mmc);
  990. if (err)
  991. mmc->has_init = 0;
  992. else
  993. mmc->has_init = 1;
  994. return err;
  995. }
  996. /*
  997. * CPU and board-specific MMC initializations. Aliased function
  998. * signals caller to move on
  999. */
  1000. static int __def_mmc_init(bd_t *bis)
  1001. {
  1002. return -1;
  1003. }
  1004. int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1005. int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1006. void print_mmc_devices(char separator)
  1007. {
  1008. struct mmc *m;
  1009. struct list_head *entry;
  1010. list_for_each(entry, &mmc_devices) {
  1011. m = list_entry(entry, struct mmc, link);
  1012. printf("%s: %d", m->name, m->block_dev.dev);
  1013. if (entry->next != &mmc_devices)
  1014. printf("%c ", separator);
  1015. }
  1016. printf("\n");
  1017. }
  1018. int get_mmc_num(void)
  1019. {
  1020. return cur_dev_num;
  1021. }
  1022. int mmc_initialize(bd_t *bis)
  1023. {
  1024. INIT_LIST_HEAD (&mmc_devices);
  1025. cur_dev_num = 0;
  1026. if (board_mmc_init(bis) < 0)
  1027. cpu_mmc_init(bis);
  1028. print_mmc_devices(',');
  1029. return 0;
  1030. }