mmc.c 27 KB

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