mmc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. /*
  475. * Set the capacity if the switch succeeded or was intended
  476. * to return to representing the raw device.
  477. */
  478. if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
  479. ret = mmc_set_capacity(mmc, part_num);
  480. return ret;
  481. }
  482. int mmc_getcd(struct mmc *mmc)
  483. {
  484. int cd;
  485. cd = board_mmc_getcd(mmc);
  486. if (cd < 0) {
  487. if (mmc->cfg->ops->getcd)
  488. cd = mmc->cfg->ops->getcd(mmc);
  489. else
  490. cd = 1;
  491. }
  492. return cd;
  493. }
  494. static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  495. {
  496. struct mmc_cmd cmd;
  497. struct mmc_data data;
  498. /* Switch the frequency */
  499. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  500. cmd.resp_type = MMC_RSP_R1;
  501. cmd.cmdarg = (mode << 31) | 0xffffff;
  502. cmd.cmdarg &= ~(0xf << (group * 4));
  503. cmd.cmdarg |= value << (group * 4);
  504. data.dest = (char *)resp;
  505. data.blocksize = 64;
  506. data.blocks = 1;
  507. data.flags = MMC_DATA_READ;
  508. return mmc_send_cmd(mmc, &cmd, &data);
  509. }
  510. static int sd_change_freq(struct mmc *mmc)
  511. {
  512. int err;
  513. struct mmc_cmd cmd;
  514. ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
  515. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  516. struct mmc_data data;
  517. int timeout;
  518. mmc->card_caps = 0;
  519. if (mmc_host_is_spi(mmc))
  520. return 0;
  521. /* Read the SCR to find out if this card supports higher speeds */
  522. cmd.cmdidx = MMC_CMD_APP_CMD;
  523. cmd.resp_type = MMC_RSP_R1;
  524. cmd.cmdarg = mmc->rca << 16;
  525. err = mmc_send_cmd(mmc, &cmd, NULL);
  526. if (err)
  527. return err;
  528. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  529. cmd.resp_type = MMC_RSP_R1;
  530. cmd.cmdarg = 0;
  531. timeout = 3;
  532. retry_scr:
  533. data.dest = (char *)scr;
  534. data.blocksize = 8;
  535. data.blocks = 1;
  536. data.flags = MMC_DATA_READ;
  537. err = mmc_send_cmd(mmc, &cmd, &data);
  538. if (err) {
  539. if (timeout--)
  540. goto retry_scr;
  541. return err;
  542. }
  543. mmc->scr[0] = __be32_to_cpu(scr[0]);
  544. mmc->scr[1] = __be32_to_cpu(scr[1]);
  545. switch ((mmc->scr[0] >> 24) & 0xf) {
  546. case 0:
  547. mmc->version = SD_VERSION_1_0;
  548. break;
  549. case 1:
  550. mmc->version = SD_VERSION_1_10;
  551. break;
  552. case 2:
  553. mmc->version = SD_VERSION_2;
  554. if ((mmc->scr[0] >> 15) & 0x1)
  555. mmc->version = SD_VERSION_3;
  556. break;
  557. default:
  558. mmc->version = SD_VERSION_1_0;
  559. break;
  560. }
  561. if (mmc->scr[0] & SD_DATA_4BIT)
  562. mmc->card_caps |= MMC_MODE_4BIT;
  563. /* Version 1.0 doesn't support switching */
  564. if (mmc->version == SD_VERSION_1_0)
  565. return 0;
  566. timeout = 4;
  567. while (timeout--) {
  568. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  569. (u8 *)switch_status);
  570. if (err)
  571. return err;
  572. /* The high-speed function is busy. Try again */
  573. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  574. break;
  575. }
  576. /* If high-speed isn't supported, we return */
  577. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  578. return 0;
  579. /*
  580. * If the host doesn't support SD_HIGHSPEED, do not switch card to
  581. * HIGHSPEED mode even if the card support SD_HIGHSPPED.
  582. * This can avoid furthur problem when the card runs in different
  583. * mode between the host.
  584. */
  585. if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
  586. (mmc->cfg->host_caps & MMC_MODE_HS)))
  587. return 0;
  588. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
  589. if (err)
  590. return err;
  591. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  592. mmc->card_caps |= MMC_MODE_HS;
  593. return 0;
  594. }
  595. /* frequency bases */
  596. /* divided by 10 to be nice to platforms without floating point */
  597. static const int fbase[] = {
  598. 10000,
  599. 100000,
  600. 1000000,
  601. 10000000,
  602. };
  603. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  604. * to platforms without floating point.
  605. */
  606. static const int multipliers[] = {
  607. 0, /* reserved */
  608. 10,
  609. 12,
  610. 13,
  611. 15,
  612. 20,
  613. 25,
  614. 30,
  615. 35,
  616. 40,
  617. 45,
  618. 50,
  619. 55,
  620. 60,
  621. 70,
  622. 80,
  623. };
  624. static void mmc_set_ios(struct mmc *mmc)
  625. {
  626. if (mmc->cfg->ops->set_ios)
  627. mmc->cfg->ops->set_ios(mmc);
  628. }
  629. void mmc_set_clock(struct mmc *mmc, uint clock)
  630. {
  631. if (clock > mmc->cfg->f_max)
  632. clock = mmc->cfg->f_max;
  633. if (clock < mmc->cfg->f_min)
  634. clock = mmc->cfg->f_min;
  635. mmc->clock = clock;
  636. mmc_set_ios(mmc);
  637. }
  638. static void mmc_set_bus_width(struct mmc *mmc, uint width)
  639. {
  640. mmc->bus_width = width;
  641. mmc_set_ios(mmc);
  642. }
  643. static int mmc_startup(struct mmc *mmc)
  644. {
  645. int err, i;
  646. uint mult, freq;
  647. u64 cmult, csize, capacity;
  648. struct mmc_cmd cmd;
  649. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  650. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  651. int timeout = 1000;
  652. #ifdef CONFIG_MMC_SPI_CRC_ON
  653. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  654. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  655. cmd.resp_type = MMC_RSP_R1;
  656. cmd.cmdarg = 1;
  657. err = mmc_send_cmd(mmc, &cmd, NULL);
  658. if (err)
  659. return err;
  660. }
  661. #endif
  662. /* Put the Card in Identify Mode */
  663. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  664. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  665. cmd.resp_type = MMC_RSP_R2;
  666. cmd.cmdarg = 0;
  667. err = mmc_send_cmd(mmc, &cmd, NULL);
  668. if (err)
  669. return err;
  670. memcpy(mmc->cid, cmd.response, 16);
  671. /*
  672. * For MMC cards, set the Relative Address.
  673. * For SD cards, get the Relatvie Address.
  674. * This also puts the cards into Standby State
  675. */
  676. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  677. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  678. cmd.cmdarg = mmc->rca << 16;
  679. cmd.resp_type = MMC_RSP_R6;
  680. err = mmc_send_cmd(mmc, &cmd, NULL);
  681. if (err)
  682. return err;
  683. if (IS_SD(mmc))
  684. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  685. }
  686. /* Get the Card-Specific Data */
  687. cmd.cmdidx = MMC_CMD_SEND_CSD;
  688. cmd.resp_type = MMC_RSP_R2;
  689. cmd.cmdarg = mmc->rca << 16;
  690. err = mmc_send_cmd(mmc, &cmd, NULL);
  691. /* Waiting for the ready status */
  692. mmc_send_status(mmc, timeout);
  693. if (err)
  694. return err;
  695. mmc->csd[0] = cmd.response[0];
  696. mmc->csd[1] = cmd.response[1];
  697. mmc->csd[2] = cmd.response[2];
  698. mmc->csd[3] = cmd.response[3];
  699. if (mmc->version == MMC_VERSION_UNKNOWN) {
  700. int version = (cmd.response[0] >> 26) & 0xf;
  701. switch (version) {
  702. case 0:
  703. mmc->version = MMC_VERSION_1_2;
  704. break;
  705. case 1:
  706. mmc->version = MMC_VERSION_1_4;
  707. break;
  708. case 2:
  709. mmc->version = MMC_VERSION_2_2;
  710. break;
  711. case 3:
  712. mmc->version = MMC_VERSION_3;
  713. break;
  714. case 4:
  715. mmc->version = MMC_VERSION_4;
  716. break;
  717. default:
  718. mmc->version = MMC_VERSION_1_2;
  719. break;
  720. }
  721. }
  722. /* divide frequency by 10, since the mults are 10x bigger */
  723. freq = fbase[(cmd.response[0] & 0x7)];
  724. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  725. mmc->tran_speed = freq * mult;
  726. mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
  727. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  728. if (IS_SD(mmc))
  729. mmc->write_bl_len = mmc->read_bl_len;
  730. else
  731. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  732. if (mmc->high_capacity) {
  733. csize = (mmc->csd[1] & 0x3f) << 16
  734. | (mmc->csd[2] & 0xffff0000) >> 16;
  735. cmult = 8;
  736. } else {
  737. csize = (mmc->csd[1] & 0x3ff) << 2
  738. | (mmc->csd[2] & 0xc0000000) >> 30;
  739. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  740. }
  741. mmc->capacity_user = (csize + 1) << (cmult + 2);
  742. mmc->capacity_user *= mmc->read_bl_len;
  743. mmc->capacity_boot = 0;
  744. mmc->capacity_rpmb = 0;
  745. for (i = 0; i < 4; i++)
  746. mmc->capacity_gp[i] = 0;
  747. if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
  748. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  749. if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
  750. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  751. if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
  752. cmd.cmdidx = MMC_CMD_SET_DSR;
  753. cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
  754. cmd.resp_type = MMC_RSP_NONE;
  755. if (mmc_send_cmd(mmc, &cmd, NULL))
  756. printf("MMC: SET_DSR failed\n");
  757. }
  758. /* Select the card, and put it into Transfer Mode */
  759. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  760. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  761. cmd.resp_type = MMC_RSP_R1;
  762. cmd.cmdarg = mmc->rca << 16;
  763. err = mmc_send_cmd(mmc, &cmd, NULL);
  764. if (err)
  765. return err;
  766. }
  767. /*
  768. * For SD, its erase group is always one sector
  769. */
  770. mmc->erase_grp_size = 1;
  771. mmc->part_config = MMCPART_NOAVAILABLE;
  772. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  773. /* check ext_csd version and capacity */
  774. err = mmc_send_ext_csd(mmc, ext_csd);
  775. if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
  776. /*
  777. * According to the JEDEC Standard, the value of
  778. * ext_csd's capacity is valid if the value is more
  779. * than 2GB
  780. */
  781. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  782. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  783. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  784. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  785. capacity *= MMC_MAX_BLOCK_LEN;
  786. if ((capacity >> 20) > 2 * 1024)
  787. mmc->capacity_user = capacity;
  788. }
  789. switch (ext_csd[EXT_CSD_REV]) {
  790. case 1:
  791. mmc->version = MMC_VERSION_4_1;
  792. break;
  793. case 2:
  794. mmc->version = MMC_VERSION_4_2;
  795. break;
  796. case 3:
  797. mmc->version = MMC_VERSION_4_3;
  798. break;
  799. case 5:
  800. mmc->version = MMC_VERSION_4_41;
  801. break;
  802. case 6:
  803. mmc->version = MMC_VERSION_4_5;
  804. break;
  805. case 7:
  806. mmc->version = MMC_VERSION_5_0;
  807. break;
  808. }
  809. /*
  810. * Host needs to enable ERASE_GRP_DEF bit if device is
  811. * partitioned. This bit will be lost every time after a reset
  812. * or power off. This will affect erase size.
  813. */
  814. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
  815. (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
  816. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  817. EXT_CSD_ERASE_GROUP_DEF, 1);
  818. if (err)
  819. return err;
  820. else
  821. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  822. /* Read out group size from ext_csd */
  823. mmc->erase_grp_size =
  824. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
  825. MMC_MAX_BLOCK_LEN * 1024;
  826. /*
  827. * if high capacity and partition setting completed
  828. * SEC_COUNT is valid even if it is smaller than 2 GiB
  829. * JEDEC Standard JESD84-B45, 6.2.4
  830. */
  831. if (mmc->high_capacity &&
  832. (ext_csd[EXT_CSD_PARTITION_SETTING] &
  833. EXT_CSD_PARTITION_SETTING_COMPLETED)) {
  834. capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
  835. (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
  836. (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
  837. (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
  838. capacity *= MMC_MAX_BLOCK_LEN;
  839. mmc->capacity_user = capacity;
  840. }
  841. } else {
  842. /* Calculate the group size from the csd value. */
  843. int erase_gsz, erase_gmul;
  844. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  845. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  846. mmc->erase_grp_size = (erase_gsz + 1)
  847. * (erase_gmul + 1);
  848. }
  849. /* store the partition info of emmc */
  850. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  851. ext_csd[EXT_CSD_BOOT_MULT])
  852. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  853. mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  854. mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
  855. for (i = 0; i < 4; i++) {
  856. int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
  857. mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
  858. (ext_csd[idx + 1] << 8) + ext_csd[idx];
  859. mmc->capacity_gp[i] *=
  860. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  861. mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  862. }
  863. }
  864. err = mmc_set_capacity(mmc, mmc->part_num);
  865. if (err)
  866. return err;
  867. if (IS_SD(mmc))
  868. err = sd_change_freq(mmc);
  869. else
  870. err = mmc_change_freq(mmc);
  871. if (err)
  872. return err;
  873. /* Restrict card's capabilities by what the host can do */
  874. mmc->card_caps &= mmc->cfg->host_caps;
  875. if (IS_SD(mmc)) {
  876. if (mmc->card_caps & MMC_MODE_4BIT) {
  877. cmd.cmdidx = MMC_CMD_APP_CMD;
  878. cmd.resp_type = MMC_RSP_R1;
  879. cmd.cmdarg = mmc->rca << 16;
  880. err = mmc_send_cmd(mmc, &cmd, NULL);
  881. if (err)
  882. return err;
  883. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  884. cmd.resp_type = MMC_RSP_R1;
  885. cmd.cmdarg = 2;
  886. err = mmc_send_cmd(mmc, &cmd, NULL);
  887. if (err)
  888. return err;
  889. mmc_set_bus_width(mmc, 4);
  890. }
  891. if (mmc->card_caps & MMC_MODE_HS)
  892. mmc->tran_speed = 50000000;
  893. else
  894. mmc->tran_speed = 25000000;
  895. } else {
  896. int idx;
  897. /* An array of possible bus widths in order of preference */
  898. static unsigned ext_csd_bits[] = {
  899. EXT_CSD_DDR_BUS_WIDTH_8,
  900. EXT_CSD_DDR_BUS_WIDTH_4,
  901. EXT_CSD_BUS_WIDTH_8,
  902. EXT_CSD_BUS_WIDTH_4,
  903. EXT_CSD_BUS_WIDTH_1,
  904. };
  905. /* An array to map CSD bus widths to host cap bits */
  906. static unsigned ext_to_hostcaps[] = {
  907. [EXT_CSD_DDR_BUS_WIDTH_4] = MMC_MODE_DDR_52MHz,
  908. [EXT_CSD_DDR_BUS_WIDTH_8] = MMC_MODE_DDR_52MHz,
  909. [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
  910. [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
  911. };
  912. /* An array to map chosen bus width to an integer */
  913. static unsigned widths[] = {
  914. 8, 4, 8, 4, 1,
  915. };
  916. for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
  917. unsigned int extw = ext_csd_bits[idx];
  918. /*
  919. * Check to make sure the controller supports
  920. * this bus width, if it's more than 1
  921. */
  922. if (extw != EXT_CSD_BUS_WIDTH_1 &&
  923. !(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
  924. continue;
  925. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  926. EXT_CSD_BUS_WIDTH, extw);
  927. if (err)
  928. continue;
  929. mmc_set_bus_width(mmc, widths[idx]);
  930. err = mmc_send_ext_csd(mmc, test_csd);
  931. /* Only compare read only fields */
  932. if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
  933. == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
  934. && ext_csd[EXT_CSD_HC_WP_GRP_SIZE] \
  935. == test_csd[EXT_CSD_HC_WP_GRP_SIZE] \
  936. && ext_csd[EXT_CSD_REV] \
  937. == test_csd[EXT_CSD_REV]
  938. && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
  939. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  940. && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
  941. &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
  942. mmc->card_caps |= ext_to_hostcaps[extw];
  943. break;
  944. }
  945. }
  946. if (mmc->card_caps & MMC_MODE_HS) {
  947. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  948. mmc->tran_speed = 52000000;
  949. else
  950. mmc->tran_speed = 26000000;
  951. }
  952. }
  953. mmc_set_clock(mmc, mmc->tran_speed);
  954. /* fill in device description */
  955. mmc->block_dev.lun = 0;
  956. mmc->block_dev.type = 0;
  957. mmc->block_dev.blksz = mmc->read_bl_len;
  958. mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
  959. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  960. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  961. sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
  962. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  963. (mmc->cid[3] >> 16) & 0xffff);
  964. sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  965. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  966. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  967. (mmc->cid[2] >> 24) & 0xff);
  968. sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  969. (mmc->cid[2] >> 16) & 0xf);
  970. #else
  971. mmc->block_dev.vendor[0] = 0;
  972. mmc->block_dev.product[0] = 0;
  973. mmc->block_dev.revision[0] = 0;
  974. #endif
  975. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  976. init_part(&mmc->block_dev);
  977. #endif
  978. return 0;
  979. }
  980. static int mmc_send_if_cond(struct mmc *mmc)
  981. {
  982. struct mmc_cmd cmd;
  983. int err;
  984. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  985. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  986. cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
  987. cmd.resp_type = MMC_RSP_R7;
  988. err = mmc_send_cmd(mmc, &cmd, NULL);
  989. if (err)
  990. return err;
  991. if ((cmd.response[0] & 0xff) != 0xaa)
  992. return UNUSABLE_ERR;
  993. else
  994. mmc->version = SD_VERSION_2;
  995. return 0;
  996. }
  997. /* not used any more */
  998. int __deprecated mmc_register(struct mmc *mmc)
  999. {
  1000. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1001. printf("%s is deprecated! use mmc_create() instead.\n", __func__);
  1002. #endif
  1003. return -1;
  1004. }
  1005. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
  1006. {
  1007. struct mmc *mmc;
  1008. /* quick validation */
  1009. if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
  1010. cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
  1011. return NULL;
  1012. mmc = calloc(1, sizeof(*mmc));
  1013. if (mmc == NULL)
  1014. return NULL;
  1015. mmc->cfg = cfg;
  1016. mmc->priv = priv;
  1017. /* the following chunk was mmc_register() */
  1018. /* Setup dsr related values */
  1019. mmc->dsr_imp = 0;
  1020. mmc->dsr = 0xffffffff;
  1021. /* Setup the universal parts of the block interface just once */
  1022. mmc->block_dev.if_type = IF_TYPE_MMC;
  1023. mmc->block_dev.dev = cur_dev_num++;
  1024. mmc->block_dev.removable = 1;
  1025. mmc->block_dev.block_read = mmc_bread;
  1026. mmc->block_dev.block_write = mmc_bwrite;
  1027. mmc->block_dev.block_erase = mmc_berase;
  1028. /* setup initial part type */
  1029. mmc->block_dev.part_type = mmc->cfg->part_type;
  1030. INIT_LIST_HEAD(&mmc->link);
  1031. list_add_tail(&mmc->link, &mmc_devices);
  1032. return mmc;
  1033. }
  1034. void mmc_destroy(struct mmc *mmc)
  1035. {
  1036. /* only freeing memory for now */
  1037. free(mmc);
  1038. }
  1039. #ifdef CONFIG_PARTITIONS
  1040. block_dev_desc_t *mmc_get_dev(int dev)
  1041. {
  1042. struct mmc *mmc = find_mmc_device(dev);
  1043. if (!mmc || mmc_init(mmc))
  1044. return NULL;
  1045. return &mmc->block_dev;
  1046. }
  1047. #endif
  1048. /* board-specific MMC power initializations. */
  1049. __weak void board_mmc_power_init(void)
  1050. {
  1051. }
  1052. int mmc_start_init(struct mmc *mmc)
  1053. {
  1054. int err;
  1055. /* we pretend there's no card when init is NULL */
  1056. if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
  1057. mmc->has_init = 0;
  1058. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1059. printf("MMC: no card present\n");
  1060. #endif
  1061. return NO_CARD_ERR;
  1062. }
  1063. if (mmc->has_init)
  1064. return 0;
  1065. board_mmc_power_init();
  1066. /* made sure it's not NULL earlier */
  1067. err = mmc->cfg->ops->init(mmc);
  1068. if (err)
  1069. return err;
  1070. mmc_set_bus_width(mmc, 1);
  1071. mmc_set_clock(mmc, 1);
  1072. /* Reset the Card */
  1073. err = mmc_go_idle(mmc);
  1074. if (err)
  1075. return err;
  1076. /* The internal partition reset to user partition(0) at every CMD0*/
  1077. mmc->part_num = 0;
  1078. /* Test for SD version 2 */
  1079. err = mmc_send_if_cond(mmc);
  1080. /* Now try to get the SD card's operating condition */
  1081. err = sd_send_op_cond(mmc);
  1082. /* If the command timed out, we check for an MMC card */
  1083. if (err == TIMEOUT) {
  1084. err = mmc_send_op_cond(mmc);
  1085. if (err && err != IN_PROGRESS) {
  1086. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1087. printf("Card did not respond to voltage select!\n");
  1088. #endif
  1089. return UNUSABLE_ERR;
  1090. }
  1091. }
  1092. if (err == IN_PROGRESS)
  1093. mmc->init_in_progress = 1;
  1094. return err;
  1095. }
  1096. static int mmc_complete_init(struct mmc *mmc)
  1097. {
  1098. int err = 0;
  1099. if (mmc->op_cond_pending)
  1100. err = mmc_complete_op_cond(mmc);
  1101. if (!err)
  1102. err = mmc_startup(mmc);
  1103. if (err)
  1104. mmc->has_init = 0;
  1105. else
  1106. mmc->has_init = 1;
  1107. mmc->init_in_progress = 0;
  1108. return err;
  1109. }
  1110. int mmc_init(struct mmc *mmc)
  1111. {
  1112. int err = IN_PROGRESS;
  1113. unsigned start;
  1114. if (mmc->has_init)
  1115. return 0;
  1116. start = get_timer(0);
  1117. if (!mmc->init_in_progress)
  1118. err = mmc_start_init(mmc);
  1119. if (!err || err == IN_PROGRESS)
  1120. err = mmc_complete_init(mmc);
  1121. debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
  1122. return err;
  1123. }
  1124. int mmc_set_dsr(struct mmc *mmc, u16 val)
  1125. {
  1126. mmc->dsr = val;
  1127. return 0;
  1128. }
  1129. /* CPU-specific MMC initializations */
  1130. __weak int cpu_mmc_init(bd_t *bis)
  1131. {
  1132. return -1;
  1133. }
  1134. /* board-specific MMC initializations. */
  1135. __weak int board_mmc_init(bd_t *bis)
  1136. {
  1137. return -1;
  1138. }
  1139. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1140. void print_mmc_devices(char separator)
  1141. {
  1142. struct mmc *m;
  1143. struct list_head *entry;
  1144. list_for_each(entry, &mmc_devices) {
  1145. m = list_entry(entry, struct mmc, link);
  1146. printf("%s: %d", m->cfg->name, m->block_dev.dev);
  1147. if (entry->next != &mmc_devices) {
  1148. printf("%c", separator);
  1149. if (separator != '\n')
  1150. puts (" ");
  1151. }
  1152. }
  1153. printf("\n");
  1154. }
  1155. #else
  1156. void print_mmc_devices(char separator) { }
  1157. #endif
  1158. int get_mmc_num(void)
  1159. {
  1160. return cur_dev_num;
  1161. }
  1162. void mmc_set_preinit(struct mmc *mmc, int preinit)
  1163. {
  1164. mmc->preinit = preinit;
  1165. }
  1166. static void do_preinit(void)
  1167. {
  1168. struct mmc *m;
  1169. struct list_head *entry;
  1170. list_for_each(entry, &mmc_devices) {
  1171. m = list_entry(entry, struct mmc, link);
  1172. if (m->preinit)
  1173. mmc_start_init(m);
  1174. }
  1175. }
  1176. int mmc_initialize(bd_t *bis)
  1177. {
  1178. INIT_LIST_HEAD (&mmc_devices);
  1179. cur_dev_num = 0;
  1180. if (board_mmc_init(bis) < 0)
  1181. cpu_mmc_init(bis);
  1182. #ifndef CONFIG_SPL_BUILD
  1183. print_mmc_devices(',');
  1184. #endif
  1185. do_preinit();
  1186. return 0;
  1187. }
  1188. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  1189. /*
  1190. * This function changes the size of boot partition and the size of rpmb
  1191. * partition present on EMMC devices.
  1192. *
  1193. * Input Parameters:
  1194. * struct *mmc: pointer for the mmc device strcuture
  1195. * bootsize: size of boot partition
  1196. * rpmbsize: size of rpmb partition
  1197. *
  1198. * Returns 0 on success.
  1199. */
  1200. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  1201. unsigned long rpmbsize)
  1202. {
  1203. int err;
  1204. struct mmc_cmd cmd;
  1205. /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
  1206. cmd.cmdidx = MMC_CMD_RES_MAN;
  1207. cmd.resp_type = MMC_RSP_R1b;
  1208. cmd.cmdarg = MMC_CMD62_ARG1;
  1209. err = mmc_send_cmd(mmc, &cmd, NULL);
  1210. if (err) {
  1211. debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
  1212. return err;
  1213. }
  1214. /* Boot partition changing mode */
  1215. cmd.cmdidx = MMC_CMD_RES_MAN;
  1216. cmd.resp_type = MMC_RSP_R1b;
  1217. cmd.cmdarg = MMC_CMD62_ARG2;
  1218. err = mmc_send_cmd(mmc, &cmd, NULL);
  1219. if (err) {
  1220. debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
  1221. return err;
  1222. }
  1223. /* boot partition size is multiple of 128KB */
  1224. bootsize = (bootsize * 1024) / 128;
  1225. /* Arg: boot partition size */
  1226. cmd.cmdidx = MMC_CMD_RES_MAN;
  1227. cmd.resp_type = MMC_RSP_R1b;
  1228. cmd.cmdarg = bootsize;
  1229. err = mmc_send_cmd(mmc, &cmd, NULL);
  1230. if (err) {
  1231. debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
  1232. return err;
  1233. }
  1234. /* RPMB partition size is multiple of 128KB */
  1235. rpmbsize = (rpmbsize * 1024) / 128;
  1236. /* Arg: RPMB partition size */
  1237. cmd.cmdidx = MMC_CMD_RES_MAN;
  1238. cmd.resp_type = MMC_RSP_R1b;
  1239. cmd.cmdarg = rpmbsize;
  1240. err = mmc_send_cmd(mmc, &cmd, NULL);
  1241. if (err) {
  1242. debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
  1243. return err;
  1244. }
  1245. return 0;
  1246. }
  1247. /*
  1248. * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
  1249. * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
  1250. * and BOOT_MODE.
  1251. *
  1252. * Returns 0 on success.
  1253. */
  1254. int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
  1255. {
  1256. int err;
  1257. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
  1258. EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
  1259. EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
  1260. EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
  1261. if (err)
  1262. return err;
  1263. return 0;
  1264. }
  1265. /*
  1266. * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
  1267. * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
  1268. * PARTITION_ACCESS.
  1269. *
  1270. * Returns 0 on success.
  1271. */
  1272. int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  1273. {
  1274. int err;
  1275. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  1276. EXT_CSD_BOOT_ACK(ack) |
  1277. EXT_CSD_BOOT_PART_NUM(part_num) |
  1278. EXT_CSD_PARTITION_ACCESS(access));
  1279. if (err)
  1280. return err;
  1281. return 0;
  1282. }
  1283. /*
  1284. * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
  1285. * for enable. Note that this is a write-once field for non-zero values.
  1286. *
  1287. * Returns 0 on success.
  1288. */
  1289. int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
  1290. {
  1291. return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
  1292. enable);
  1293. }
  1294. #endif