mmc.c 30 KB

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