mmc.c 32 KB

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