mmc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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;
  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. int idx;
  864. /* An array of possible bus widths in order of preference */
  865. static unsigned ext_csd_bits[] = {
  866. EXT_CSD_BUS_WIDTH_8,
  867. EXT_CSD_BUS_WIDTH_4,
  868. EXT_CSD_BUS_WIDTH_1,
  869. };
  870. /* An array to map CSD bus widths to host cap bits */
  871. static unsigned ext_to_hostcaps[] = {
  872. [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
  873. [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
  874. };
  875. /* An array to map chosen bus width to an integer */
  876. static unsigned widths[] = {
  877. 8, 4, 1,
  878. };
  879. for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
  880. unsigned int extw = ext_csd_bits[idx];
  881. /*
  882. * Check to make sure the controller supports
  883. * this bus width, if it's more than 1
  884. */
  885. if (extw != EXT_CSD_BUS_WIDTH_1 &&
  886. !(mmc->host_caps & ext_to_hostcaps[extw]))
  887. continue;
  888. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  889. EXT_CSD_BUS_WIDTH, extw);
  890. if (err)
  891. continue;
  892. mmc_set_bus_width(mmc, widths[idx]);
  893. err = mmc_send_ext_csd(mmc, test_csd);
  894. if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
  895. == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
  896. && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
  897. == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
  898. && ext_csd[EXT_CSD_REV] \
  899. == test_csd[EXT_CSD_REV]
  900. && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
  901. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  902. && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
  903. &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
  904. mmc->card_caps |= ext_to_hostcaps[extw];
  905. break;
  906. }
  907. }
  908. if (mmc->card_caps & MMC_MODE_HS) {
  909. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  910. mmc->tran_speed = 52000000;
  911. else
  912. mmc->tran_speed = 26000000;
  913. }
  914. }
  915. mmc_set_clock(mmc, mmc->tran_speed);
  916. /* fill in device description */
  917. mmc->block_dev.lun = 0;
  918. mmc->block_dev.type = 0;
  919. mmc->block_dev.blksz = mmc->read_bl_len;
  920. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  921. sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
  922. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  923. (mmc->cid[3] >> 16) & 0xffff);
  924. sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  925. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  926. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  927. (mmc->cid[2] >> 24) & 0xff);
  928. sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  929. (mmc->cid[2] >> 16) & 0xf);
  930. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  931. init_part(&mmc->block_dev);
  932. #endif
  933. return 0;
  934. }
  935. static int mmc_send_if_cond(struct mmc *mmc)
  936. {
  937. struct mmc_cmd cmd;
  938. int err;
  939. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  940. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  941. cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
  942. cmd.resp_type = MMC_RSP_R7;
  943. err = mmc_send_cmd(mmc, &cmd, NULL);
  944. if (err)
  945. return err;
  946. if ((cmd.response[0] & 0xff) != 0xaa)
  947. return UNUSABLE_ERR;
  948. else
  949. mmc->version = SD_VERSION_2;
  950. return 0;
  951. }
  952. int mmc_register(struct mmc *mmc)
  953. {
  954. /* Setup the universal parts of the block interface just once */
  955. mmc->block_dev.if_type = IF_TYPE_MMC;
  956. mmc->block_dev.dev = cur_dev_num++;
  957. mmc->block_dev.removable = 1;
  958. mmc->block_dev.block_read = mmc_bread;
  959. mmc->block_dev.block_write = mmc_bwrite;
  960. mmc->block_dev.block_erase = mmc_berase;
  961. if (!mmc->b_max)
  962. mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  963. INIT_LIST_HEAD (&mmc->link);
  964. list_add_tail (&mmc->link, &mmc_devices);
  965. return 0;
  966. }
  967. #ifdef CONFIG_PARTITIONS
  968. block_dev_desc_t *mmc_get_dev(int dev)
  969. {
  970. struct mmc *mmc = find_mmc_device(dev);
  971. if (!mmc || mmc_init(mmc))
  972. return NULL;
  973. return &mmc->block_dev;
  974. }
  975. #endif
  976. int mmc_init(struct mmc *mmc)
  977. {
  978. int err;
  979. if (mmc_getcd(mmc) == 0) {
  980. mmc->has_init = 0;
  981. printf("MMC: no card present\n");
  982. return NO_CARD_ERR;
  983. }
  984. if (mmc->has_init)
  985. return 0;
  986. err = mmc->init(mmc);
  987. if (err)
  988. return err;
  989. mmc_set_bus_width(mmc, 1);
  990. mmc_set_clock(mmc, 1);
  991. /* Reset the Card */
  992. err = mmc_go_idle(mmc);
  993. if (err)
  994. return err;
  995. /* The internal partition reset to user partition(0) at every CMD0*/
  996. mmc->part_num = 0;
  997. /* Test for SD version 2 */
  998. err = mmc_send_if_cond(mmc);
  999. /* Now try to get the SD card's operating condition */
  1000. err = sd_send_op_cond(mmc);
  1001. /* If the command timed out, we check for an MMC card */
  1002. if (err == TIMEOUT) {
  1003. err = mmc_send_op_cond(mmc);
  1004. if (err) {
  1005. printf("Card did not respond to voltage select!\n");
  1006. return UNUSABLE_ERR;
  1007. }
  1008. }
  1009. err = mmc_startup(mmc);
  1010. if (err)
  1011. mmc->has_init = 0;
  1012. else
  1013. mmc->has_init = 1;
  1014. return err;
  1015. }
  1016. /*
  1017. * CPU and board-specific MMC initializations. Aliased function
  1018. * signals caller to move on
  1019. */
  1020. static int __def_mmc_init(bd_t *bis)
  1021. {
  1022. return -1;
  1023. }
  1024. int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1025. int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  1026. void print_mmc_devices(char separator)
  1027. {
  1028. struct mmc *m;
  1029. struct list_head *entry;
  1030. list_for_each(entry, &mmc_devices) {
  1031. m = list_entry(entry, struct mmc, link);
  1032. printf("%s: %d", m->name, m->block_dev.dev);
  1033. if (entry->next != &mmc_devices)
  1034. printf("%c ", separator);
  1035. }
  1036. printf("\n");
  1037. }
  1038. int get_mmc_num(void)
  1039. {
  1040. return cur_dev_num;
  1041. }
  1042. int mmc_initialize(bd_t *bis)
  1043. {
  1044. INIT_LIST_HEAD (&mmc_devices);
  1045. cur_dev_num = 0;
  1046. if (board_mmc_init(bis) < 0)
  1047. cpu_mmc_init(bis);
  1048. print_mmc_devices(',');
  1049. return 0;
  1050. }