mmc.c 28 KB

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