mmc.c 34 KB

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