mmc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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. while (1) {
  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. if (timeout-- <= 0)
  124. break;
  125. udelay(1000);
  126. }
  127. #ifdef CONFIG_MMC_TRACE
  128. status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
  129. printf("CURR STATE:%d\n", status);
  130. #endif
  131. if (timeout <= 0) {
  132. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  133. printf("Timeout waiting card ready\n");
  134. #endif
  135. return TIMEOUT;
  136. }
  137. if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
  138. return SWITCH_ERR;
  139. return 0;
  140. }
  141. int mmc_set_blocklen(struct mmc *mmc, int len)
  142. {
  143. struct mmc_cmd cmd;
  144. if (mmc->ddr_mode)
  145. return 0;
  146. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  147. cmd.resp_type = MMC_RSP_R1;
  148. cmd.cmdarg = len;
  149. return mmc_send_cmd(mmc, &cmd, NULL);
  150. }
  151. struct mmc *find_mmc_device(int dev_num)
  152. {
  153. struct mmc *m;
  154. struct list_head *entry;
  155. list_for_each(entry, &mmc_devices) {
  156. m = list_entry(entry, struct mmc, link);
  157. if (m->block_dev.dev == dev_num)
  158. return m;
  159. }
  160. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  161. printf("MMC Device %d not found\n", dev_num);
  162. #endif
  163. return NULL;
  164. }
  165. static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
  166. lbaint_t blkcnt)
  167. {
  168. struct mmc_cmd cmd;
  169. struct mmc_data data;
  170. if (blkcnt > 1)
  171. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  172. else
  173. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  174. if (mmc->high_capacity)
  175. cmd.cmdarg = start;
  176. else
  177. cmd.cmdarg = start * mmc->read_bl_len;
  178. cmd.resp_type = MMC_RSP_R1;
  179. data.dest = dst;
  180. data.blocks = blkcnt;
  181. data.blocksize = mmc->read_bl_len;
  182. data.flags = MMC_DATA_READ;
  183. if (mmc_send_cmd(mmc, &cmd, &data))
  184. return 0;
  185. if (blkcnt > 1) {
  186. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  187. cmd.cmdarg = 0;
  188. cmd.resp_type = MMC_RSP_R1b;
  189. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  190. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  191. printf("mmc fail to send stop cmd\n");
  192. #endif
  193. return 0;
  194. }
  195. }
  196. return blkcnt;
  197. }
  198. static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
  199. {
  200. lbaint_t cur, blocks_todo = blkcnt;
  201. if (blkcnt == 0)
  202. return 0;
  203. struct mmc *mmc = find_mmc_device(dev_num);
  204. if (!mmc)
  205. return 0;
  206. if ((start + blkcnt) > mmc->block_dev.lba) {
  207. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  208. printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
  209. start + blkcnt, mmc->block_dev.lba);
  210. #endif
  211. return 0;
  212. }
  213. if (mmc_set_blocklen(mmc, mmc->read_bl_len))
  214. return 0;
  215. do {
  216. cur = (blocks_todo > mmc->cfg->b_max) ?
  217. mmc->cfg->b_max : blocks_todo;
  218. if(mmc_read_blocks(mmc, dst, start, cur) != cur)
  219. return 0;
  220. blocks_todo -= cur;
  221. start += cur;
  222. dst += cur * mmc->read_bl_len;
  223. } while (blocks_todo > 0);
  224. return blkcnt;
  225. }
  226. static int mmc_go_idle(struct mmc *mmc)
  227. {
  228. struct mmc_cmd cmd;
  229. int err;
  230. udelay(1000);
  231. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  232. cmd.cmdarg = 0;
  233. cmd.resp_type = MMC_RSP_NONE;
  234. err = mmc_send_cmd(mmc, &cmd, NULL);
  235. if (err)
  236. return err;
  237. udelay(2000);
  238. return 0;
  239. }
  240. static int sd_send_op_cond(struct mmc *mmc)
  241. {
  242. int timeout = 1000;
  243. int err;
  244. struct mmc_cmd cmd;
  245. while (1) {
  246. cmd.cmdidx = MMC_CMD_APP_CMD;
  247. cmd.resp_type = MMC_RSP_R1;
  248. cmd.cmdarg = 0;
  249. err = mmc_send_cmd(mmc, &cmd, NULL);
  250. if (err)
  251. return err;
  252. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  253. cmd.resp_type = MMC_RSP_R3;
  254. /*
  255. * Most cards do not answer if some reserved bits
  256. * in the ocr are set. However, Some controller
  257. * can set bit 7 (reserved for low voltages), but
  258. * how to manage low voltages SD card is not yet
  259. * specified.
  260. */
  261. cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
  262. (mmc->cfg->voltages & 0xff8000);
  263. if (mmc->version == SD_VERSION_2)
  264. cmd.cmdarg |= OCR_HCS;
  265. err = mmc_send_cmd(mmc, &cmd, NULL);
  266. if (err)
  267. return err;
  268. if (cmd.response[0] & OCR_BUSY)
  269. break;
  270. if (timeout-- <= 0)
  271. return UNUSABLE_ERR;
  272. udelay(1000);
  273. }
  274. if (mmc->version != SD_VERSION_2)
  275. mmc->version = SD_VERSION_1_0;
  276. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  277. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  278. cmd.resp_type = MMC_RSP_R3;
  279. cmd.cmdarg = 0;
  280. err = mmc_send_cmd(mmc, &cmd, NULL);
  281. if (err)
  282. return err;
  283. }
  284. mmc->ocr = cmd.response[0];
  285. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  286. mmc->rca = 0;
  287. return 0;
  288. }
  289. static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
  290. {
  291. struct mmc_cmd cmd;
  292. int err;
  293. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  294. cmd.resp_type = MMC_RSP_R3;
  295. cmd.cmdarg = 0;
  296. if (use_arg && !mmc_host_is_spi(mmc))
  297. cmd.cmdarg = OCR_HCS |
  298. (mmc->cfg->voltages &
  299. (mmc->ocr & OCR_VOLTAGE_MASK)) |
  300. (mmc->ocr & OCR_ACCESS_MODE);
  301. err = mmc_send_cmd(mmc, &cmd, NULL);
  302. if (err)
  303. return err;
  304. mmc->ocr = cmd.response[0];
  305. return 0;
  306. }
  307. static int mmc_send_op_cond(struct mmc *mmc)
  308. {
  309. int err, i;
  310. /* Some cards seem to need this */
  311. mmc_go_idle(mmc);
  312. /* Asking to the card its capabilities */
  313. for (i = 0; i < 2; i++) {
  314. err = mmc_send_op_cond_iter(mmc, i != 0);
  315. if (err)
  316. return err;
  317. /* exit if not busy (flag seems to be inverted) */
  318. if (mmc->ocr & OCR_BUSY)
  319. break;
  320. }
  321. mmc->op_cond_pending = 1;
  322. return 0;
  323. }
  324. static 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. if (!(mmc->ocr & OCR_BUSY)) {
  332. start = get_timer(0);
  333. while (1) {
  334. err = mmc_send_op_cond_iter(mmc, 1);
  335. if (err)
  336. return err;
  337. if (mmc->ocr & OCR_BUSY)
  338. break;
  339. if (get_timer(start) > timeout)
  340. return UNUSABLE_ERR;
  341. udelay(100);
  342. }
  343. }
  344. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  345. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  346. cmd.resp_type = MMC_RSP_R3;
  347. cmd.cmdarg = 0;
  348. err = mmc_send_cmd(mmc, &cmd, NULL);
  349. if (err)
  350. return err;
  351. mmc->ocr = cmd.response[0];
  352. }
  353. mmc->version = MMC_VERSION_UNKNOWN;
  354. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  355. mmc->rca = 1;
  356. return 0;
  357. }
  358. static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
  359. {
  360. struct mmc_cmd cmd;
  361. struct mmc_data data;
  362. int err;
  363. /* Get the Card Status Register */
  364. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  365. cmd.resp_type = MMC_RSP_R1;
  366. cmd.cmdarg = 0;
  367. data.dest = (char *)ext_csd;
  368. data.blocks = 1;
  369. data.blocksize = MMC_MAX_BLOCK_LEN;
  370. data.flags = MMC_DATA_READ;
  371. err = mmc_send_cmd(mmc, &cmd, &data);
  372. return err;
  373. }
  374. static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
  375. {
  376. struct mmc_cmd cmd;
  377. int timeout = 1000;
  378. int ret;
  379. cmd.cmdidx = MMC_CMD_SWITCH;
  380. cmd.resp_type = MMC_RSP_R1b;
  381. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  382. (index << 16) |
  383. (value << 8);
  384. ret = mmc_send_cmd(mmc, &cmd, NULL);
  385. /* Waiting for the ready status */
  386. if (!ret)
  387. ret = mmc_send_status(mmc, timeout);
  388. return ret;
  389. }
  390. static int mmc_change_freq(struct mmc *mmc)
  391. {
  392. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  393. char cardtype;
  394. int err;
  395. mmc->card_caps = 0;
  396. if (mmc_host_is_spi(mmc))
  397. return 0;
  398. /* Only version 4 supports high-speed */
  399. if (mmc->version < MMC_VERSION_4)
  400. return 0;
  401. mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
  402. err = mmc_send_ext_csd(mmc, ext_csd);
  403. if (err)
  404. return err;
  405. cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
  406. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
  407. if (err)
  408. return err == SWITCH_ERR ? 0 : err;
  409. /* Now check to see that it worked */
  410. err = mmc_send_ext_csd(mmc, ext_csd);
  411. if (err)
  412. return err;
  413. /* No high-speed support */
  414. if (!ext_csd[EXT_CSD_HS_TIMING])
  415. return 0;
  416. /* High Speed is set, there are two types: 52MHz and 26MHz */
  417. if (cardtype & EXT_CSD_CARD_TYPE_52) {
  418. if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
  419. mmc->card_caps |= MMC_MODE_DDR_52MHz;
  420. mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  421. } else {
  422. mmc->card_caps |= MMC_MODE_HS;
  423. }
  424. return 0;
  425. }
  426. static int mmc_set_capacity(struct mmc *mmc, int part_num)
  427. {
  428. switch (part_num) {
  429. case 0:
  430. mmc->capacity = mmc->capacity_user;
  431. break;
  432. case 1:
  433. case 2:
  434. mmc->capacity = mmc->capacity_boot;
  435. break;
  436. case 3:
  437. mmc->capacity = mmc->capacity_rpmb;
  438. break;
  439. case 4:
  440. case 5:
  441. case 6:
  442. case 7:
  443. mmc->capacity = mmc->capacity_gp[part_num - 4];
  444. break;
  445. default:
  446. return -1;
  447. }
  448. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  449. return 0;
  450. }
  451. int mmc_select_hwpart(int dev_num, int hwpart)
  452. {
  453. struct mmc *mmc = find_mmc_device(dev_num);
  454. int ret;
  455. if (!mmc)
  456. return -ENODEV;
  457. if (mmc->part_num == hwpart)
  458. return 0;
  459. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  460. printf("Card doesn't support part_switch\n");
  461. return -EMEDIUMTYPE;
  462. }
  463. ret = mmc_switch_part(dev_num, hwpart);
  464. if (ret)
  465. return ret;
  466. mmc->part_num = hwpart;
  467. return 0;
  468. }
  469. int mmc_switch_part(int dev_num, unsigned int part_num)
  470. {
  471. struct mmc *mmc = find_mmc_device(dev_num);
  472. int ret;
  473. if (!mmc)
  474. return -1;
  475. ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  476. (mmc->part_config & ~PART_ACCESS_MASK)
  477. | (part_num & PART_ACCESS_MASK));
  478. /*
  479. * Set the capacity if the switch succeeded or was intended
  480. * to return to representing the raw device.
  481. */
  482. if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
  483. ret = mmc_set_capacity(mmc, part_num);
  484. return ret;
  485. }
  486. int mmc_hwpart_config(struct mmc *mmc,
  487. const struct mmc_hwpart_conf *conf,
  488. enum mmc_hwpart_conf_mode mode)
  489. {
  490. u8 part_attrs = 0;
  491. u32 enh_size_mult;
  492. u32 enh_start_addr;
  493. u32 gp_size_mult[4];
  494. u32 max_enh_size_mult;
  495. u32 tot_enh_size_mult = 0;
  496. u8 wr_rel_set;
  497. int i, pidx, err;
  498. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  499. if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
  500. return -EINVAL;
  501. if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
  502. printf("eMMC >= 4.4 required for enhanced user data area\n");
  503. return -EMEDIUMTYPE;
  504. }
  505. if (!(mmc->part_support & PART_SUPPORT)) {
  506. printf("Card does not support partitioning\n");
  507. return -EMEDIUMTYPE;
  508. }
  509. if (!mmc->hc_wp_grp_size) {
  510. printf("Card does not define HC WP group size\n");
  511. return -EMEDIUMTYPE;
  512. }
  513. /* check partition alignment and total enhanced size */
  514. if (conf->user.enh_size) {
  515. if (conf->user.enh_size % mmc->hc_wp_grp_size ||
  516. conf->user.enh_start % mmc->hc_wp_grp_size) {
  517. printf("User data enhanced area not HC WP group "
  518. "size aligned\n");
  519. return -EINVAL;
  520. }
  521. part_attrs |= EXT_CSD_ENH_USR;
  522. enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
  523. if (mmc->high_capacity) {
  524. enh_start_addr = conf->user.enh_start;
  525. } else {
  526. enh_start_addr = (conf->user.enh_start << 9);
  527. }
  528. } else {
  529. enh_size_mult = 0;
  530. enh_start_addr = 0;
  531. }
  532. tot_enh_size_mult += enh_size_mult;
  533. for (pidx = 0; pidx < 4; pidx++) {
  534. if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
  535. printf("GP%i partition not HC WP group size "
  536. "aligned\n", pidx+1);
  537. return -EINVAL;
  538. }
  539. gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
  540. if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
  541. part_attrs |= EXT_CSD_ENH_GP(pidx);
  542. tot_enh_size_mult += gp_size_mult[pidx];
  543. }
  544. }
  545. if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
  546. printf("Card does not support enhanced attribute\n");
  547. return -EMEDIUMTYPE;
  548. }
  549. err = mmc_send_ext_csd(mmc, ext_csd);
  550. if (err)
  551. return err;
  552. max_enh_size_mult =
  553. (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
  554. (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
  555. ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
  556. if (tot_enh_size_mult > max_enh_size_mult) {
  557. printf("Total enhanced size exceeds maximum (%u > %u)\n",
  558. tot_enh_size_mult, max_enh_size_mult);
  559. return -EMEDIUMTYPE;
  560. }
  561. /* The default value of EXT_CSD_WR_REL_SET is device
  562. * dependent, the values can only be changed if the
  563. * EXT_CSD_HS_CTRL_REL bit is set. The values can be
  564. * changed only once and before partitioning is completed. */
  565. wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
  566. if (conf->user.wr_rel_change) {
  567. if (conf->user.wr_rel_set)
  568. wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
  569. else
  570. wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
  571. }
  572. for (pidx = 0; pidx < 4; pidx++) {
  573. if (conf->gp_part[pidx].wr_rel_change) {
  574. if (conf->gp_part[pidx].wr_rel_set)
  575. wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
  576. else
  577. wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
  578. }
  579. }
  580. if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
  581. !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
  582. puts("Card does not support host controlled partition write "
  583. "reliability settings\n");
  584. return -EMEDIUMTYPE;
  585. }
  586. if (ext_csd[EXT_CSD_PARTITION_SETTING] &
  587. EXT_CSD_PARTITION_SETTING_COMPLETED) {
  588. printf("Card already partitioned\n");
  589. return -EPERM;
  590. }
  591. if (mode == MMC_HWPART_CONF_CHECK)
  592. return 0;
  593. /* Partitioning requires high-capacity size definitions */
  594. if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
  595. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  596. EXT_CSD_ERASE_GROUP_DEF, 1);
  597. if (err)
  598. return err;
  599. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  600. /* update erase group size to be high-capacity */
  601. mmc->erase_grp_size =
  602. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
  603. }
  604. /* all OK, write the configuration */
  605. for (i = 0; i < 4; i++) {
  606. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  607. EXT_CSD_ENH_START_ADDR+i,
  608. (enh_start_addr >> (i*8)) & 0xFF);
  609. if (err)
  610. return err;
  611. }
  612. for (i = 0; i < 3; i++) {
  613. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  614. EXT_CSD_ENH_SIZE_MULT+i,
  615. (enh_size_mult >> (i*8)) & 0xFF);
  616. if (err)
  617. return err;
  618. }
  619. for (pidx = 0; pidx < 4; pidx++) {
  620. for (i = 0; i < 3; i++) {
  621. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  622. EXT_CSD_GP_SIZE_MULT+pidx*3+i,
  623. (gp_size_mult[pidx] >> (i*8)) & 0xFF);
  624. if (err)
  625. return err;
  626. }
  627. }
  628. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  629. EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
  630. if (err)
  631. return err;
  632. if (mode == MMC_HWPART_CONF_SET)
  633. return 0;
  634. /* The WR_REL_SET is a write-once register but shall be
  635. * written before setting PART_SETTING_COMPLETED. As it is
  636. * write-once we can only write it when completing the
  637. * partitioning. */
  638. if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
  639. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  640. EXT_CSD_WR_REL_SET, wr_rel_set);
  641. if (err)
  642. return err;
  643. }
  644. /* Setting PART_SETTING_COMPLETED confirms the partition
  645. * configuration but it only becomes effective after power
  646. * cycle, so we do not adjust the partition related settings
  647. * in the mmc struct. */
  648. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  649. EXT_CSD_PARTITION_SETTING,
  650. EXT_CSD_PARTITION_SETTING_COMPLETED);
  651. if (err)
  652. return err;
  653. return 0;
  654. }
  655. int mmc_getcd(struct mmc *mmc)
  656. {
  657. int cd;
  658. cd = board_mmc_getcd(mmc);
  659. if (cd < 0) {
  660. if (mmc->cfg->ops->getcd)
  661. cd = mmc->cfg->ops->getcd(mmc);
  662. else
  663. cd = 1;
  664. }
  665. return cd;
  666. }
  667. static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  668. {
  669. struct mmc_cmd cmd;
  670. struct mmc_data data;
  671. /* Switch the frequency */
  672. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  673. cmd.resp_type = MMC_RSP_R1;
  674. cmd.cmdarg = (mode << 31) | 0xffffff;
  675. cmd.cmdarg &= ~(0xf << (group * 4));
  676. cmd.cmdarg |= value << (group * 4);
  677. data.dest = (char *)resp;
  678. data.blocksize = 64;
  679. data.blocks = 1;
  680. data.flags = MMC_DATA_READ;
  681. return mmc_send_cmd(mmc, &cmd, &data);
  682. }
  683. static int sd_change_freq(struct mmc *mmc)
  684. {
  685. int err;
  686. struct mmc_cmd cmd;
  687. ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
  688. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  689. struct mmc_data data;
  690. int timeout;
  691. mmc->card_caps = 0;
  692. if (mmc_host_is_spi(mmc))
  693. return 0;
  694. /* Read the SCR to find out if this card supports higher speeds */
  695. cmd.cmdidx = MMC_CMD_APP_CMD;
  696. cmd.resp_type = MMC_RSP_R1;
  697. cmd.cmdarg = mmc->rca << 16;
  698. err = mmc_send_cmd(mmc, &cmd, NULL);
  699. if (err)
  700. return err;
  701. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  702. cmd.resp_type = MMC_RSP_R1;
  703. cmd.cmdarg = 0;
  704. timeout = 3;
  705. retry_scr:
  706. data.dest = (char *)scr;
  707. data.blocksize = 8;
  708. data.blocks = 1;
  709. data.flags = MMC_DATA_READ;
  710. err = mmc_send_cmd(mmc, &cmd, &data);
  711. if (err) {
  712. if (timeout--)
  713. goto retry_scr;
  714. return err;
  715. }
  716. mmc->scr[0] = __be32_to_cpu(scr[0]);
  717. mmc->scr[1] = __be32_to_cpu(scr[1]);
  718. switch ((mmc->scr[0] >> 24) & 0xf) {
  719. case 0:
  720. mmc->version = SD_VERSION_1_0;
  721. break;
  722. case 1:
  723. mmc->version = SD_VERSION_1_10;
  724. break;
  725. case 2:
  726. mmc->version = SD_VERSION_2;
  727. if ((mmc->scr[0] >> 15) & 0x1)
  728. mmc->version = SD_VERSION_3;
  729. break;
  730. default:
  731. mmc->version = SD_VERSION_1_0;
  732. break;
  733. }
  734. if (mmc->scr[0] & SD_DATA_4BIT)
  735. mmc->card_caps |= MMC_MODE_4BIT;
  736. /* Version 1.0 doesn't support switching */
  737. if (mmc->version == SD_VERSION_1_0)
  738. return 0;
  739. timeout = 4;
  740. while (timeout--) {
  741. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  742. (u8 *)switch_status);
  743. if (err)
  744. return err;
  745. /* The high-speed function is busy. Try again */
  746. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  747. break;
  748. }
  749. /* If high-speed isn't supported, we return */
  750. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  751. return 0;
  752. /*
  753. * If the host doesn't support SD_HIGHSPEED, do not switch card to
  754. * HIGHSPEED mode even if the card support SD_HIGHSPPED.
  755. * This can avoid furthur problem when the card runs in different
  756. * mode between the host.
  757. */
  758. if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
  759. (mmc->cfg->host_caps & MMC_MODE_HS)))
  760. return 0;
  761. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
  762. if (err)
  763. return err;
  764. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  765. mmc->card_caps |= MMC_MODE_HS;
  766. return 0;
  767. }
  768. /* frequency bases */
  769. /* divided by 10 to be nice to platforms without floating point */
  770. static const int fbase[] = {
  771. 10000,
  772. 100000,
  773. 1000000,
  774. 10000000,
  775. };
  776. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  777. * to platforms without floating point.
  778. */
  779. static const int multipliers[] = {
  780. 0, /* reserved */
  781. 10,
  782. 12,
  783. 13,
  784. 15,
  785. 20,
  786. 25,
  787. 30,
  788. 35,
  789. 40,
  790. 45,
  791. 50,
  792. 55,
  793. 60,
  794. 70,
  795. 80,
  796. };
  797. static void mmc_set_ios(struct mmc *mmc)
  798. {
  799. if (mmc->cfg->ops->set_ios)
  800. mmc->cfg->ops->set_ios(mmc);
  801. }
  802. void mmc_set_clock(struct mmc *mmc, uint clock)
  803. {
  804. if (clock > mmc->cfg->f_max)
  805. clock = mmc->cfg->f_max;
  806. if (clock < mmc->cfg->f_min)
  807. clock = mmc->cfg->f_min;
  808. mmc->clock = clock;
  809. mmc_set_ios(mmc);
  810. }
  811. static void mmc_set_bus_width(struct mmc *mmc, uint width)
  812. {
  813. mmc->bus_width = width;
  814. mmc_set_ios(mmc);
  815. }
  816. static int mmc_startup(struct mmc *mmc)
  817. {
  818. int err, i;
  819. uint mult, freq;
  820. u64 cmult, csize, capacity;
  821. struct mmc_cmd cmd;
  822. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  823. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  824. int timeout = 1000;
  825. bool has_parts = false;
  826. bool part_completed;
  827. #ifdef CONFIG_MMC_SPI_CRC_ON
  828. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  829. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  830. cmd.resp_type = MMC_RSP_R1;
  831. cmd.cmdarg = 1;
  832. err = mmc_send_cmd(mmc, &cmd, NULL);
  833. if (err)
  834. return err;
  835. }
  836. #endif
  837. /* Put the Card in Identify Mode */
  838. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  839. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  840. cmd.resp_type = MMC_RSP_R2;
  841. cmd.cmdarg = 0;
  842. err = mmc_send_cmd(mmc, &cmd, NULL);
  843. if (err)
  844. return err;
  845. memcpy(mmc->cid, cmd.response, 16);
  846. /*
  847. * For MMC cards, set the Relative Address.
  848. * For SD cards, get the Relatvie Address.
  849. * This also puts the cards into Standby State
  850. */
  851. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  852. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  853. cmd.cmdarg = mmc->rca << 16;
  854. cmd.resp_type = MMC_RSP_R6;
  855. err = mmc_send_cmd(mmc, &cmd, NULL);
  856. if (err)
  857. return err;
  858. if (IS_SD(mmc))
  859. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  860. }
  861. /* Get the Card-Specific Data */
  862. cmd.cmdidx = MMC_CMD_SEND_CSD;
  863. cmd.resp_type = MMC_RSP_R2;
  864. cmd.cmdarg = mmc->rca << 16;
  865. err = mmc_send_cmd(mmc, &cmd, NULL);
  866. /* Waiting for the ready status */
  867. mmc_send_status(mmc, timeout);
  868. if (err)
  869. return err;
  870. mmc->csd[0] = cmd.response[0];
  871. mmc->csd[1] = cmd.response[1];
  872. mmc->csd[2] = cmd.response[2];
  873. mmc->csd[3] = cmd.response[3];
  874. if (mmc->version == MMC_VERSION_UNKNOWN) {
  875. int version = (cmd.response[0] >> 26) & 0xf;
  876. switch (version) {
  877. case 0:
  878. mmc->version = MMC_VERSION_1_2;
  879. break;
  880. case 1:
  881. mmc->version = MMC_VERSION_1_4;
  882. break;
  883. case 2:
  884. mmc->version = MMC_VERSION_2_2;
  885. break;
  886. case 3:
  887. mmc->version = MMC_VERSION_3;
  888. break;
  889. case 4:
  890. mmc->version = MMC_VERSION_4;
  891. break;
  892. default:
  893. mmc->version = MMC_VERSION_1_2;
  894. break;
  895. }
  896. }
  897. /* divide frequency by 10, since the mults are 10x bigger */
  898. freq = fbase[(cmd.response[0] & 0x7)];
  899. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  900. mmc->tran_speed = freq * mult;
  901. mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
  902. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  903. if (IS_SD(mmc))
  904. mmc->write_bl_len = mmc->read_bl_len;
  905. else
  906. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  907. if (mmc->high_capacity) {
  908. csize = (mmc->csd[1] & 0x3f) << 16
  909. | (mmc->csd[2] & 0xffff0000) >> 16;
  910. cmult = 8;
  911. } else {
  912. csize = (mmc->csd[1] & 0x3ff) << 2
  913. | (mmc->csd[2] & 0xc0000000) >> 30;
  914. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  915. }
  916. mmc->capacity_user = (csize + 1) << (cmult + 2);
  917. mmc->capacity_user *= mmc->read_bl_len;
  918. mmc->capacity_boot = 0;
  919. mmc->capacity_rpmb = 0;
  920. for (i = 0; i < 4; i++)
  921. mmc->capacity_gp[i] = 0;
  922. if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
  923. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  924. if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
  925. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  926. if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
  927. cmd.cmdidx = MMC_CMD_SET_DSR;
  928. cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
  929. cmd.resp_type = MMC_RSP_NONE;
  930. if (mmc_send_cmd(mmc, &cmd, NULL))
  931. printf("MMC: SET_DSR failed\n");
  932. }
  933. /* Select the card, and put it into Transfer Mode */
  934. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  935. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  936. cmd.resp_type = MMC_RSP_R1;
  937. cmd.cmdarg = mmc->rca << 16;
  938. err = mmc_send_cmd(mmc, &cmd, NULL);
  939. if (err)
  940. return err;
  941. }
  942. /*
  943. * For SD, its erase group is always one sector
  944. */
  945. mmc->erase_grp_size = 1;
  946. mmc->part_config = MMCPART_NOAVAILABLE;
  947. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  948. /* check ext_csd version and capacity */
  949. err = mmc_send_ext_csd(mmc, ext_csd);
  950. if (err)
  951. return err;
  952. if (ext_csd[EXT_CSD_REV] >= 2) {
  953. /*
  954. * According to the JEDEC Standard, the value of
  955. * ext_csd's capacity is valid if the value is more
  956. * than 2GB
  957. */
  958. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  959. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  960. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  961. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  962. capacity *= MMC_MAX_BLOCK_LEN;
  963. if ((capacity >> 20) > 2 * 1024)
  964. mmc->capacity_user = capacity;
  965. }
  966. switch (ext_csd[EXT_CSD_REV]) {
  967. case 1:
  968. mmc->version = MMC_VERSION_4_1;
  969. break;
  970. case 2:
  971. mmc->version = MMC_VERSION_4_2;
  972. break;
  973. case 3:
  974. mmc->version = MMC_VERSION_4_3;
  975. break;
  976. case 5:
  977. mmc->version = MMC_VERSION_4_41;
  978. break;
  979. case 6:
  980. mmc->version = MMC_VERSION_4_5;
  981. break;
  982. case 7:
  983. mmc->version = MMC_VERSION_5_0;
  984. break;
  985. }
  986. /* The partition data may be non-zero but it is only
  987. * effective if PARTITION_SETTING_COMPLETED is set in
  988. * EXT_CSD, so ignore any data if this bit is not set,
  989. * except for enabling the high-capacity group size
  990. * definition (see below). */
  991. part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
  992. EXT_CSD_PARTITION_SETTING_COMPLETED);
  993. /* store the partition info of emmc */
  994. mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
  995. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  996. ext_csd[EXT_CSD_BOOT_MULT])
  997. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  998. if (part_completed &&
  999. (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
  1000. mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
  1001. mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  1002. mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
  1003. for (i = 0; i < 4; i++) {
  1004. int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
  1005. uint mult = (ext_csd[idx + 2] << 16) +
  1006. (ext_csd[idx + 1] << 8) + ext_csd[idx];
  1007. if (mult)
  1008. has_parts = true;
  1009. if (!part_completed)
  1010. continue;
  1011. mmc->capacity_gp[i] = mult;
  1012. mmc->capacity_gp[i] *=
  1013. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  1014. mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1015. mmc->capacity_gp[i] <<= 19;
  1016. }
  1017. if (part_completed) {
  1018. mmc->enh_user_size =
  1019. (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
  1020. (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
  1021. ext_csd[EXT_CSD_ENH_SIZE_MULT];
  1022. mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  1023. mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1024. mmc->enh_user_size <<= 19;
  1025. mmc->enh_user_start =
  1026. (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
  1027. (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
  1028. (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
  1029. ext_csd[EXT_CSD_ENH_START_ADDR];
  1030. if (mmc->high_capacity)
  1031. mmc->enh_user_start <<= 9;
  1032. }
  1033. /*
  1034. * Host needs to enable ERASE_GRP_DEF bit if device is
  1035. * partitioned. This bit will be lost every time after a reset
  1036. * or power off. This will affect erase size.
  1037. */
  1038. if (part_completed)
  1039. has_parts = true;
  1040. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
  1041. (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
  1042. has_parts = true;
  1043. if (has_parts) {
  1044. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1045. EXT_CSD_ERASE_GROUP_DEF, 1);
  1046. if (err)
  1047. return err;
  1048. else
  1049. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  1050. }
  1051. if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
  1052. /* Read out group size from ext_csd */
  1053. mmc->erase_grp_size =
  1054. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
  1055. /*
  1056. * if high capacity and partition setting completed
  1057. * SEC_COUNT is valid even if it is smaller than 2 GiB
  1058. * JEDEC Standard JESD84-B45, 6.2.4
  1059. */
  1060. if (mmc->high_capacity && part_completed) {
  1061. capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
  1062. (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
  1063. (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
  1064. (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
  1065. capacity *= MMC_MAX_BLOCK_LEN;
  1066. mmc->capacity_user = capacity;
  1067. }
  1068. } else {
  1069. /* Calculate the group size from the csd value. */
  1070. int erase_gsz, erase_gmul;
  1071. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  1072. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  1073. mmc->erase_grp_size = (erase_gsz + 1)
  1074. * (erase_gmul + 1);
  1075. }
  1076. mmc->hc_wp_grp_size = 1024
  1077. * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  1078. * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1079. mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
  1080. }
  1081. err = mmc_set_capacity(mmc, mmc->part_num);
  1082. if (err)
  1083. return err;
  1084. if (IS_SD(mmc))
  1085. err = sd_change_freq(mmc);
  1086. else
  1087. err = mmc_change_freq(mmc);
  1088. if (err)
  1089. return err;
  1090. /* Restrict card's capabilities by what the host can do */
  1091. mmc->card_caps &= mmc->cfg->host_caps;
  1092. if (IS_SD(mmc)) {
  1093. if (mmc->card_caps & MMC_MODE_4BIT) {
  1094. cmd.cmdidx = MMC_CMD_APP_CMD;
  1095. cmd.resp_type = MMC_RSP_R1;
  1096. cmd.cmdarg = mmc->rca << 16;
  1097. err = mmc_send_cmd(mmc, &cmd, NULL);
  1098. if (err)
  1099. return err;
  1100. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  1101. cmd.resp_type = MMC_RSP_R1;
  1102. cmd.cmdarg = 2;
  1103. err = mmc_send_cmd(mmc, &cmd, NULL);
  1104. if (err)
  1105. return err;
  1106. mmc_set_bus_width(mmc, 4);
  1107. }
  1108. if (mmc->card_caps & MMC_MODE_HS)
  1109. mmc->tran_speed = 50000000;
  1110. else
  1111. mmc->tran_speed = 25000000;
  1112. } else if (mmc->version >= MMC_VERSION_4) {
  1113. /* Only version 4 of MMC supports wider bus widths */
  1114. int idx;
  1115. /* An array of possible bus widths in order of preference */
  1116. static unsigned ext_csd_bits[] = {
  1117. EXT_CSD_DDR_BUS_WIDTH_8,
  1118. EXT_CSD_DDR_BUS_WIDTH_4,
  1119. EXT_CSD_BUS_WIDTH_8,
  1120. EXT_CSD_BUS_WIDTH_4,
  1121. EXT_CSD_BUS_WIDTH_1,
  1122. };
  1123. /* An array to map CSD bus widths to host cap bits */
  1124. static unsigned ext_to_hostcaps[] = {
  1125. [EXT_CSD_DDR_BUS_WIDTH_4] =
  1126. MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
  1127. [EXT_CSD_DDR_BUS_WIDTH_8] =
  1128. MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
  1129. [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
  1130. [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
  1131. };
  1132. /* An array to map chosen bus width to an integer */
  1133. static unsigned widths[] = {
  1134. 8, 4, 8, 4, 1,
  1135. };
  1136. for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
  1137. unsigned int extw = ext_csd_bits[idx];
  1138. unsigned int caps = ext_to_hostcaps[extw];
  1139. /*
  1140. * If the bus width is still not changed,
  1141. * don't try to set the default again.
  1142. * Otherwise, recover from switch attempts
  1143. * by switching to 1-bit bus width.
  1144. */
  1145. if (extw == EXT_CSD_BUS_WIDTH_1 &&
  1146. mmc->bus_width == 1) {
  1147. err = 0;
  1148. break;
  1149. }
  1150. /*
  1151. * Check to make sure the card and controller support
  1152. * these capabilities
  1153. */
  1154. if ((mmc->card_caps & caps) != caps)
  1155. continue;
  1156. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1157. EXT_CSD_BUS_WIDTH, extw);
  1158. if (err)
  1159. continue;
  1160. mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
  1161. mmc_set_bus_width(mmc, widths[idx]);
  1162. err = mmc_send_ext_csd(mmc, test_csd);
  1163. if (err)
  1164. continue;
  1165. /* Only compare read only fields */
  1166. if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
  1167. == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
  1168. ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
  1169. == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
  1170. ext_csd[EXT_CSD_REV]
  1171. == test_csd[EXT_CSD_REV] &&
  1172. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  1173. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
  1174. memcmp(&ext_csd[EXT_CSD_SEC_CNT],
  1175. &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
  1176. break;
  1177. else
  1178. err = SWITCH_ERR;
  1179. }
  1180. if (err)
  1181. return err;
  1182. if (mmc->card_caps & MMC_MODE_HS) {
  1183. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  1184. mmc->tran_speed = 52000000;
  1185. else
  1186. mmc->tran_speed = 26000000;
  1187. }
  1188. }
  1189. mmc_set_clock(mmc, mmc->tran_speed);
  1190. /* Fix the block length for DDR mode */
  1191. if (mmc->ddr_mode) {
  1192. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  1193. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  1194. }
  1195. /* fill in device description */
  1196. mmc->block_dev.lun = 0;
  1197. mmc->block_dev.type = 0;
  1198. mmc->block_dev.blksz = mmc->read_bl_len;
  1199. mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
  1200. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  1201. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1202. sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
  1203. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  1204. (mmc->cid[3] >> 16) & 0xffff);
  1205. sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  1206. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  1207. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  1208. (mmc->cid[2] >> 24) & 0xff);
  1209. sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  1210. (mmc->cid[2] >> 16) & 0xf);
  1211. #else
  1212. mmc->block_dev.vendor[0] = 0;
  1213. mmc->block_dev.product[0] = 0;
  1214. mmc->block_dev.revision[0] = 0;
  1215. #endif
  1216. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  1217. init_part(&mmc->block_dev);
  1218. #endif
  1219. return 0;
  1220. }
  1221. static int mmc_send_if_cond(struct mmc *mmc)
  1222. {
  1223. struct mmc_cmd cmd;
  1224. int err;
  1225. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  1226. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  1227. cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
  1228. cmd.resp_type = MMC_RSP_R7;
  1229. err = mmc_send_cmd(mmc, &cmd, NULL);
  1230. if (err)
  1231. return err;
  1232. if ((cmd.response[0] & 0xff) != 0xaa)
  1233. return UNUSABLE_ERR;
  1234. else
  1235. mmc->version = SD_VERSION_2;
  1236. return 0;
  1237. }
  1238. /* not used any more */
  1239. int __deprecated mmc_register(struct mmc *mmc)
  1240. {
  1241. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1242. printf("%s is deprecated! use mmc_create() instead.\n", __func__);
  1243. #endif
  1244. return -1;
  1245. }
  1246. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
  1247. {
  1248. struct mmc *mmc;
  1249. /* quick validation */
  1250. if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
  1251. cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
  1252. return NULL;
  1253. mmc = calloc(1, sizeof(*mmc));
  1254. if (mmc == NULL)
  1255. return NULL;
  1256. mmc->cfg = cfg;
  1257. mmc->priv = priv;
  1258. /* the following chunk was mmc_register() */
  1259. /* Setup dsr related values */
  1260. mmc->dsr_imp = 0;
  1261. mmc->dsr = 0xffffffff;
  1262. /* Setup the universal parts of the block interface just once */
  1263. mmc->block_dev.if_type = IF_TYPE_MMC;
  1264. mmc->block_dev.dev = cur_dev_num++;
  1265. mmc->block_dev.removable = 1;
  1266. mmc->block_dev.block_read = mmc_bread;
  1267. mmc->block_dev.block_write = mmc_bwrite;
  1268. mmc->block_dev.block_erase = mmc_berase;
  1269. /* setup initial part type */
  1270. mmc->block_dev.part_type = mmc->cfg->part_type;
  1271. INIT_LIST_HEAD(&mmc->link);
  1272. list_add_tail(&mmc->link, &mmc_devices);
  1273. return mmc;
  1274. }
  1275. void mmc_destroy(struct mmc *mmc)
  1276. {
  1277. /* only freeing memory for now */
  1278. free(mmc);
  1279. }
  1280. #ifdef CONFIG_PARTITIONS
  1281. block_dev_desc_t *mmc_get_dev(int dev)
  1282. {
  1283. struct mmc *mmc = find_mmc_device(dev);
  1284. if (!mmc || mmc_init(mmc))
  1285. return NULL;
  1286. return &mmc->block_dev;
  1287. }
  1288. #endif
  1289. /* board-specific MMC power initializations. */
  1290. __weak void board_mmc_power_init(void)
  1291. {
  1292. }
  1293. int mmc_start_init(struct mmc *mmc)
  1294. {
  1295. int err;
  1296. /* we pretend there's no card when init is NULL */
  1297. if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
  1298. mmc->has_init = 0;
  1299. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1300. printf("MMC: no card present\n");
  1301. #endif
  1302. return NO_CARD_ERR;
  1303. }
  1304. if (mmc->has_init)
  1305. return 0;
  1306. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  1307. mmc_adapter_card_type_ident();
  1308. #endif
  1309. board_mmc_power_init();
  1310. /* made sure it's not NULL earlier */
  1311. err = mmc->cfg->ops->init(mmc);
  1312. if (err)
  1313. return err;
  1314. mmc->ddr_mode = 0;
  1315. mmc_set_bus_width(mmc, 1);
  1316. mmc_set_clock(mmc, 1);
  1317. /* Reset the Card */
  1318. err = mmc_go_idle(mmc);
  1319. if (err)
  1320. return err;
  1321. /* The internal partition reset to user partition(0) at every CMD0*/
  1322. mmc->part_num = 0;
  1323. /* Test for SD version 2 */
  1324. err = mmc_send_if_cond(mmc);
  1325. /* Now try to get the SD card's operating condition */
  1326. err = sd_send_op_cond(mmc);
  1327. /* If the command timed out, we check for an MMC card */
  1328. if (err == TIMEOUT) {
  1329. err = mmc_send_op_cond(mmc);
  1330. if (err) {
  1331. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1332. printf("Card did not respond to voltage select!\n");
  1333. #endif
  1334. return UNUSABLE_ERR;
  1335. }
  1336. }
  1337. if (!err)
  1338. mmc->init_in_progress = 1;
  1339. return err;
  1340. }
  1341. static int mmc_complete_init(struct mmc *mmc)
  1342. {
  1343. int err = 0;
  1344. mmc->init_in_progress = 0;
  1345. if (mmc->op_cond_pending)
  1346. err = mmc_complete_op_cond(mmc);
  1347. if (!err)
  1348. err = mmc_startup(mmc);
  1349. if (err)
  1350. mmc->has_init = 0;
  1351. else
  1352. mmc->has_init = 1;
  1353. return err;
  1354. }
  1355. int mmc_init(struct mmc *mmc)
  1356. {
  1357. int err = 0;
  1358. unsigned start;
  1359. if (mmc->has_init)
  1360. return 0;
  1361. start = get_timer(0);
  1362. if (!mmc->init_in_progress)
  1363. err = mmc_start_init(mmc);
  1364. if (!err)
  1365. err = mmc_complete_init(mmc);
  1366. debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
  1367. return err;
  1368. }
  1369. int mmc_set_dsr(struct mmc *mmc, u16 val)
  1370. {
  1371. mmc->dsr = val;
  1372. return 0;
  1373. }
  1374. /* CPU-specific MMC initializations */
  1375. __weak int cpu_mmc_init(bd_t *bis)
  1376. {
  1377. return -1;
  1378. }
  1379. /* board-specific MMC initializations. */
  1380. __weak int board_mmc_init(bd_t *bis)
  1381. {
  1382. return -1;
  1383. }
  1384. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  1385. void print_mmc_devices(char separator)
  1386. {
  1387. struct mmc *m;
  1388. struct list_head *entry;
  1389. char *mmc_type;
  1390. list_for_each(entry, &mmc_devices) {
  1391. m = list_entry(entry, struct mmc, link);
  1392. if (m->has_init)
  1393. mmc_type = IS_SD(m) ? "SD" : "eMMC";
  1394. else
  1395. mmc_type = NULL;
  1396. printf("%s: %d", m->cfg->name, m->block_dev.dev);
  1397. if (mmc_type)
  1398. printf(" (%s)", mmc_type);
  1399. if (entry->next != &mmc_devices) {
  1400. printf("%c", separator);
  1401. if (separator != '\n')
  1402. puts (" ");
  1403. }
  1404. }
  1405. printf("\n");
  1406. }
  1407. #else
  1408. void print_mmc_devices(char separator) { }
  1409. #endif
  1410. int get_mmc_num(void)
  1411. {
  1412. return cur_dev_num;
  1413. }
  1414. void mmc_set_preinit(struct mmc *mmc, int preinit)
  1415. {
  1416. mmc->preinit = preinit;
  1417. }
  1418. static void do_preinit(void)
  1419. {
  1420. struct mmc *m;
  1421. struct list_head *entry;
  1422. list_for_each(entry, &mmc_devices) {
  1423. m = list_entry(entry, struct mmc, link);
  1424. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  1425. mmc_set_preinit(m, 1);
  1426. #endif
  1427. if (m->preinit)
  1428. mmc_start_init(m);
  1429. }
  1430. }
  1431. int mmc_initialize(bd_t *bis)
  1432. {
  1433. INIT_LIST_HEAD (&mmc_devices);
  1434. cur_dev_num = 0;
  1435. #ifndef CONFIG_DM_MMC
  1436. if (board_mmc_init(bis) < 0)
  1437. cpu_mmc_init(bis);
  1438. #endif
  1439. #ifndef CONFIG_SPL_BUILD
  1440. print_mmc_devices(',');
  1441. #endif
  1442. do_preinit();
  1443. return 0;
  1444. }
  1445. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  1446. /*
  1447. * This function changes the size of boot partition and the size of rpmb
  1448. * partition present on EMMC devices.
  1449. *
  1450. * Input Parameters:
  1451. * struct *mmc: pointer for the mmc device strcuture
  1452. * bootsize: size of boot partition
  1453. * rpmbsize: size of rpmb partition
  1454. *
  1455. * Returns 0 on success.
  1456. */
  1457. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  1458. unsigned long rpmbsize)
  1459. {
  1460. int err;
  1461. struct mmc_cmd cmd;
  1462. /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
  1463. cmd.cmdidx = MMC_CMD_RES_MAN;
  1464. cmd.resp_type = MMC_RSP_R1b;
  1465. cmd.cmdarg = MMC_CMD62_ARG1;
  1466. err = mmc_send_cmd(mmc, &cmd, NULL);
  1467. if (err) {
  1468. debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
  1469. return err;
  1470. }
  1471. /* Boot partition changing mode */
  1472. cmd.cmdidx = MMC_CMD_RES_MAN;
  1473. cmd.resp_type = MMC_RSP_R1b;
  1474. cmd.cmdarg = MMC_CMD62_ARG2;
  1475. err = mmc_send_cmd(mmc, &cmd, NULL);
  1476. if (err) {
  1477. debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
  1478. return err;
  1479. }
  1480. /* boot partition size is multiple of 128KB */
  1481. bootsize = (bootsize * 1024) / 128;
  1482. /* Arg: boot partition size */
  1483. cmd.cmdidx = MMC_CMD_RES_MAN;
  1484. cmd.resp_type = MMC_RSP_R1b;
  1485. cmd.cmdarg = bootsize;
  1486. err = mmc_send_cmd(mmc, &cmd, NULL);
  1487. if (err) {
  1488. debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
  1489. return err;
  1490. }
  1491. /* RPMB partition size is multiple of 128KB */
  1492. rpmbsize = (rpmbsize * 1024) / 128;
  1493. /* Arg: RPMB partition size */
  1494. cmd.cmdidx = MMC_CMD_RES_MAN;
  1495. cmd.resp_type = MMC_RSP_R1b;
  1496. cmd.cmdarg = rpmbsize;
  1497. err = mmc_send_cmd(mmc, &cmd, NULL);
  1498. if (err) {
  1499. debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
  1500. return err;
  1501. }
  1502. return 0;
  1503. }
  1504. /*
  1505. * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
  1506. * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
  1507. * and BOOT_MODE.
  1508. *
  1509. * Returns 0 on success.
  1510. */
  1511. int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
  1512. {
  1513. int err;
  1514. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
  1515. EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
  1516. EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
  1517. EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
  1518. if (err)
  1519. return err;
  1520. return 0;
  1521. }
  1522. /*
  1523. * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
  1524. * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
  1525. * PARTITION_ACCESS.
  1526. *
  1527. * Returns 0 on success.
  1528. */
  1529. int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  1530. {
  1531. int err;
  1532. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  1533. EXT_CSD_BOOT_ACK(ack) |
  1534. EXT_CSD_BOOT_PART_NUM(part_num) |
  1535. EXT_CSD_PARTITION_ACCESS(access));
  1536. if (err)
  1537. return err;
  1538. return 0;
  1539. }
  1540. /*
  1541. * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
  1542. * for enable. Note that this is a write-once field for non-zero values.
  1543. *
  1544. * Returns 0 on success.
  1545. */
  1546. int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
  1547. {
  1548. return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
  1549. enable);
  1550. }
  1551. #endif