mmc.c 26 KB

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