mmc.c 25 KB

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