mmc.c 41 KB

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