mmc.c 33 KB

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