mmc.c 43 KB

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