mmc.c 32 KB

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