mmc.c 27 KB

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