mmc.c 33 KB

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