mmc.c 27 KB

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