mmc.c 27 KB

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