mmc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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 __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
  40. return -1;
  41. }
  42. int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
  43. alias("__board_mmc_getcd")));
  44. int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  45. {
  46. #ifdef CONFIG_MMC_TRACE
  47. int ret;
  48. int i;
  49. u8 *ptr;
  50. printf("CMD_SEND:%d\n", cmd->cmdidx);
  51. printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
  52. printf("\t\tFLAG\t\t\t %d\n", cmd->flags);
  53. ret = mmc->send_cmd(mmc, cmd, data);
  54. switch (cmd->resp_type) {
  55. case MMC_RSP_NONE:
  56. printf("\t\tMMC_RSP_NONE\n");
  57. break;
  58. case MMC_RSP_R1:
  59. printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
  60. cmd->response[0]);
  61. break;
  62. case MMC_RSP_R1b:
  63. printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
  64. cmd->response[0]);
  65. break;
  66. case MMC_RSP_R2:
  67. printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
  68. cmd->response[0]);
  69. printf("\t\t \t\t 0x%08X \n",
  70. cmd->response[1]);
  71. printf("\t\t \t\t 0x%08X \n",
  72. cmd->response[2]);
  73. printf("\t\t \t\t 0x%08X \n",
  74. cmd->response[3]);
  75. printf("\n");
  76. printf("\t\t\t\t\tDUMPING DATA\n");
  77. for (i = 0; i < 4; i++) {
  78. int j;
  79. printf("\t\t\t\t\t%03d - ", i*4);
  80. ptr = &cmd->response[i];
  81. ptr += 3;
  82. for (j = 0; j < 4; j++)
  83. printf("%02X ", *ptr--);
  84. printf("\n");
  85. }
  86. break;
  87. case MMC_RSP_R3:
  88. printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
  89. cmd->response[0]);
  90. break;
  91. default:
  92. printf("\t\tERROR MMC rsp not supported\n");
  93. break;
  94. }
  95. return ret;
  96. #else
  97. return mmc->send_cmd(mmc, cmd, data);
  98. #endif
  99. }
  100. int mmc_send_status(struct mmc *mmc, int timeout)
  101. {
  102. struct mmc_cmd cmd;
  103. int err;
  104. #ifdef CONFIG_MMC_TRACE
  105. int status;
  106. #endif
  107. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  108. cmd.resp_type = MMC_RSP_R1;
  109. cmd.cmdarg = 0;
  110. cmd.flags = 0;
  111. do {
  112. err = mmc_send_cmd(mmc, &cmd, NULL);
  113. if (err)
  114. return err;
  115. else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
  116. break;
  117. udelay(1000);
  118. if (cmd.response[0] & MMC_STATUS_MASK) {
  119. printf("Status Error: 0x%08X\n", cmd.response[0]);
  120. return COMM_ERR;
  121. }
  122. } while (timeout--);
  123. #ifdef CONFIG_MMC_TRACE
  124. status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
  125. printf("CURR STATE:%d\n", status);
  126. #endif
  127. if (!timeout) {
  128. printf("Timeout waiting card ready\n");
  129. return TIMEOUT;
  130. }
  131. return 0;
  132. }
  133. int mmc_set_blocklen(struct mmc *mmc, int len)
  134. {
  135. struct mmc_cmd cmd;
  136. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  137. cmd.resp_type = MMC_RSP_R1;
  138. cmd.cmdarg = len;
  139. cmd.flags = 0;
  140. return mmc_send_cmd(mmc, &cmd, NULL);
  141. }
  142. struct mmc *find_mmc_device(int dev_num)
  143. {
  144. struct mmc *m;
  145. struct list_head *entry;
  146. list_for_each(entry, &mmc_devices) {
  147. m = list_entry(entry, struct mmc, link);
  148. if (m->block_dev.dev == dev_num)
  149. return m;
  150. }
  151. printf("MMC Device %d not found\n", dev_num);
  152. return NULL;
  153. }
  154. static ulong
  155. mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
  156. {
  157. struct mmc_cmd cmd;
  158. struct mmc_data data;
  159. int timeout = 1000;
  160. if ((start + blkcnt) > mmc->block_dev.lba) {
  161. printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
  162. start + blkcnt, mmc->block_dev.lba);
  163. return 0;
  164. }
  165. if (blkcnt > 1)
  166. cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
  167. else
  168. cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
  169. if (mmc->high_capacity)
  170. cmd.cmdarg = start;
  171. else
  172. cmd.cmdarg = start * mmc->write_bl_len;
  173. cmd.resp_type = MMC_RSP_R1;
  174. cmd.flags = 0;
  175. data.src = src;
  176. data.blocks = blkcnt;
  177. data.blocksize = mmc->write_bl_len;
  178. data.flags = MMC_DATA_WRITE;
  179. if (mmc_send_cmd(mmc, &cmd, &data)) {
  180. printf("mmc write failed\n");
  181. return 0;
  182. }
  183. /* SPI multiblock writes terminate using a special
  184. * token, not a STOP_TRANSMISSION request.
  185. */
  186. if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
  187. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  188. cmd.cmdarg = 0;
  189. cmd.resp_type = MMC_RSP_R1b;
  190. cmd.flags = 0;
  191. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  192. printf("mmc fail to send stop cmd\n");
  193. return 0;
  194. }
  195. /* Waiting for the ready status */
  196. mmc_send_status(mmc, timeout);
  197. }
  198. return blkcnt;
  199. }
  200. static ulong
  201. mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
  202. {
  203. lbaint_t cur, blocks_todo = blkcnt;
  204. struct mmc *mmc = find_mmc_device(dev_num);
  205. if (!mmc)
  206. return 0;
  207. if (mmc_set_blocklen(mmc, mmc->write_bl_len))
  208. return 0;
  209. do {
  210. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  211. if(mmc_write_blocks(mmc, start, cur, src) != cur)
  212. return 0;
  213. blocks_todo -= cur;
  214. start += cur;
  215. src += cur * mmc->write_bl_len;
  216. } while (blocks_todo > 0);
  217. return blkcnt;
  218. }
  219. int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
  220. {
  221. struct mmc_cmd cmd;
  222. struct mmc_data data;
  223. int timeout = 1000;
  224. if (blkcnt > 1)
  225. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  226. else
  227. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  228. if (mmc->high_capacity)
  229. cmd.cmdarg = start;
  230. else
  231. cmd.cmdarg = start * mmc->read_bl_len;
  232. cmd.resp_type = MMC_RSP_R1;
  233. cmd.flags = 0;
  234. data.dest = dst;
  235. data.blocks = blkcnt;
  236. data.blocksize = mmc->read_bl_len;
  237. data.flags = MMC_DATA_READ;
  238. if (mmc_send_cmd(mmc, &cmd, &data))
  239. return 0;
  240. if (blkcnt > 1) {
  241. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  242. cmd.cmdarg = 0;
  243. cmd.resp_type = MMC_RSP_R1b;
  244. cmd.flags = 0;
  245. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  246. printf("mmc fail to send stop cmd\n");
  247. return 0;
  248. }
  249. /* Waiting for the ready status */
  250. mmc_send_status(mmc, timeout);
  251. }
  252. return blkcnt;
  253. }
  254. static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
  255. {
  256. lbaint_t cur, blocks_todo = blkcnt;
  257. if (blkcnt == 0)
  258. return 0;
  259. struct mmc *mmc = find_mmc_device(dev_num);
  260. if (!mmc)
  261. return 0;
  262. if ((start + blkcnt) > mmc->block_dev.lba) {
  263. printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
  264. start + blkcnt, mmc->block_dev.lba);
  265. return 0;
  266. }
  267. if (mmc_set_blocklen(mmc, mmc->read_bl_len))
  268. return 0;
  269. do {
  270. cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
  271. if(mmc_read_blocks(mmc, dst, start, cur) != cur)
  272. return 0;
  273. blocks_todo -= cur;
  274. start += cur;
  275. dst += cur * mmc->read_bl_len;
  276. } while (blocks_todo > 0);
  277. return blkcnt;
  278. }
  279. int mmc_go_idle(struct mmc* mmc)
  280. {
  281. struct mmc_cmd cmd;
  282. int err;
  283. udelay(1000);
  284. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  285. cmd.cmdarg = 0;
  286. cmd.resp_type = MMC_RSP_NONE;
  287. cmd.flags = 0;
  288. err = mmc_send_cmd(mmc, &cmd, NULL);
  289. if (err)
  290. return err;
  291. udelay(2000);
  292. return 0;
  293. }
  294. int
  295. sd_send_op_cond(struct mmc *mmc)
  296. {
  297. int timeout = 1000;
  298. int err;
  299. struct mmc_cmd cmd;
  300. do {
  301. cmd.cmdidx = MMC_CMD_APP_CMD;
  302. cmd.resp_type = MMC_RSP_R1;
  303. cmd.cmdarg = 0;
  304. cmd.flags = 0;
  305. err = mmc_send_cmd(mmc, &cmd, NULL);
  306. if (err)
  307. return err;
  308. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  309. cmd.resp_type = MMC_RSP_R3;
  310. /*
  311. * Most cards do not answer if some reserved bits
  312. * in the ocr are set. However, Some controller
  313. * can set bit 7 (reserved for low voltages), but
  314. * how to manage low voltages SD card is not yet
  315. * specified.
  316. */
  317. cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
  318. (mmc->voltages & 0xff8000);
  319. if (mmc->version == SD_VERSION_2)
  320. cmd.cmdarg |= OCR_HCS;
  321. err = mmc_send_cmd(mmc, &cmd, NULL);
  322. if (err)
  323. return err;
  324. udelay(1000);
  325. } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
  326. if (timeout <= 0)
  327. return UNUSABLE_ERR;
  328. if (mmc->version != SD_VERSION_2)
  329. mmc->version = SD_VERSION_1_0;
  330. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  331. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  332. cmd.resp_type = MMC_RSP_R3;
  333. cmd.cmdarg = 0;
  334. cmd.flags = 0;
  335. err = mmc_send_cmd(mmc, &cmd, NULL);
  336. if (err)
  337. return err;
  338. }
  339. mmc->ocr = cmd.response[0];
  340. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  341. mmc->rca = 0;
  342. return 0;
  343. }
  344. int mmc_send_op_cond(struct mmc *mmc)
  345. {
  346. int timeout = 10000;
  347. struct mmc_cmd cmd;
  348. int err;
  349. /* Some cards seem to need this */
  350. mmc_go_idle(mmc);
  351. /* Asking to the card its capabilities */
  352. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  353. cmd.resp_type = MMC_RSP_R3;
  354. cmd.cmdarg = 0;
  355. cmd.flags = 0;
  356. err = mmc_send_cmd(mmc, &cmd, NULL);
  357. if (err)
  358. return err;
  359. udelay(1000);
  360. do {
  361. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  362. cmd.resp_type = MMC_RSP_R3;
  363. cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
  364. (mmc->voltages &
  365. (cmd.response[0] & OCR_VOLTAGE_MASK)) |
  366. (cmd.response[0] & OCR_ACCESS_MODE));
  367. cmd.flags = 0;
  368. err = mmc_send_cmd(mmc, &cmd, NULL);
  369. if (err)
  370. return err;
  371. udelay(1000);
  372. } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
  373. if (timeout <= 0)
  374. return UNUSABLE_ERR;
  375. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  376. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  377. cmd.resp_type = MMC_RSP_R3;
  378. cmd.cmdarg = 0;
  379. cmd.flags = 0;
  380. err = mmc_send_cmd(mmc, &cmd, NULL);
  381. if (err)
  382. return err;
  383. }
  384. mmc->version = MMC_VERSION_UNKNOWN;
  385. mmc->ocr = cmd.response[0];
  386. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  387. mmc->rca = 0;
  388. return 0;
  389. }
  390. int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
  391. {
  392. struct mmc_cmd cmd;
  393. struct mmc_data data;
  394. int err;
  395. /* Get the Card Status Register */
  396. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  397. cmd.resp_type = MMC_RSP_R1;
  398. cmd.cmdarg = 0;
  399. cmd.flags = 0;
  400. data.dest = ext_csd;
  401. data.blocks = 1;
  402. data.blocksize = 512;
  403. data.flags = MMC_DATA_READ;
  404. err = mmc_send_cmd(mmc, &cmd, &data);
  405. return err;
  406. }
  407. int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
  408. {
  409. struct mmc_cmd cmd;
  410. int timeout = 1000;
  411. int ret;
  412. cmd.cmdidx = MMC_CMD_SWITCH;
  413. cmd.resp_type = MMC_RSP_R1b;
  414. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  415. (index << 16) |
  416. (value << 8);
  417. cmd.flags = 0;
  418. ret = mmc_send_cmd(mmc, &cmd, NULL);
  419. /* Waiting for the ready status */
  420. mmc_send_status(mmc, timeout);
  421. return ret;
  422. }
  423. int mmc_change_freq(struct mmc *mmc)
  424. {
  425. char ext_csd[512];
  426. char cardtype;
  427. int err;
  428. mmc->card_caps = 0;
  429. if (mmc_host_is_spi(mmc))
  430. return 0;
  431. /* Only version 4 supports high-speed */
  432. if (mmc->version < MMC_VERSION_4)
  433. return 0;
  434. mmc->card_caps |= MMC_MODE_4BIT;
  435. err = mmc_send_ext_csd(mmc, ext_csd);
  436. if (err)
  437. return err;
  438. cardtype = ext_csd[196] & 0xf;
  439. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
  440. if (err)
  441. return err;
  442. /* Now check to see that it worked */
  443. err = mmc_send_ext_csd(mmc, ext_csd);
  444. if (err)
  445. return err;
  446. /* No high-speed support */
  447. if (!ext_csd[185])
  448. return 0;
  449. /* High Speed is set, there are two types: 52MHz and 26MHz */
  450. if (cardtype & MMC_HS_52MHZ)
  451. mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  452. else
  453. mmc->card_caps |= MMC_MODE_HS;
  454. return 0;
  455. }
  456. int mmc_switch_part(int dev_num, unsigned int part_num)
  457. {
  458. struct mmc *mmc = find_mmc_device(dev_num);
  459. if (!mmc)
  460. return -1;
  461. return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  462. (mmc->part_config & ~PART_ACCESS_MASK)
  463. | (part_num & PART_ACCESS_MASK));
  464. }
  465. int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  466. {
  467. struct mmc_cmd cmd;
  468. struct mmc_data data;
  469. /* Switch the frequency */
  470. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  471. cmd.resp_type = MMC_RSP_R1;
  472. cmd.cmdarg = (mode << 31) | 0xffffff;
  473. cmd.cmdarg &= ~(0xf << (group * 4));
  474. cmd.cmdarg |= value << (group * 4);
  475. cmd.flags = 0;
  476. data.dest = (char *)resp;
  477. data.blocksize = 64;
  478. data.blocks = 1;
  479. data.flags = MMC_DATA_READ;
  480. return mmc_send_cmd(mmc, &cmd, &data);
  481. }
  482. int sd_change_freq(struct mmc *mmc)
  483. {
  484. int err;
  485. struct mmc_cmd cmd;
  486. uint scr[2];
  487. uint switch_status[16];
  488. struct mmc_data data;
  489. int timeout;
  490. mmc->card_caps = 0;
  491. if (mmc_host_is_spi(mmc))
  492. return 0;
  493. /* Read the SCR to find out if this card supports higher speeds */
  494. cmd.cmdidx = MMC_CMD_APP_CMD;
  495. cmd.resp_type = MMC_RSP_R1;
  496. cmd.cmdarg = mmc->rca << 16;
  497. cmd.flags = 0;
  498. err = mmc_send_cmd(mmc, &cmd, NULL);
  499. if (err)
  500. return err;
  501. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  502. cmd.resp_type = MMC_RSP_R1;
  503. cmd.cmdarg = 0;
  504. cmd.flags = 0;
  505. timeout = 3;
  506. retry_scr:
  507. data.dest = (char *)&scr;
  508. data.blocksize = 8;
  509. data.blocks = 1;
  510. data.flags = MMC_DATA_READ;
  511. err = mmc_send_cmd(mmc, &cmd, &data);
  512. if (err) {
  513. if (timeout--)
  514. goto retry_scr;
  515. return err;
  516. }
  517. mmc->scr[0] = __be32_to_cpu(scr[0]);
  518. mmc->scr[1] = __be32_to_cpu(scr[1]);
  519. switch ((mmc->scr[0] >> 24) & 0xf) {
  520. case 0:
  521. mmc->version = SD_VERSION_1_0;
  522. break;
  523. case 1:
  524. mmc->version = SD_VERSION_1_10;
  525. break;
  526. case 2:
  527. mmc->version = SD_VERSION_2;
  528. break;
  529. default:
  530. mmc->version = SD_VERSION_1_0;
  531. break;
  532. }
  533. if (mmc->scr[0] & SD_DATA_4BIT)
  534. mmc->card_caps |= MMC_MODE_4BIT;
  535. /* Version 1.0 doesn't support switching */
  536. if (mmc->version == SD_VERSION_1_0)
  537. return 0;
  538. timeout = 4;
  539. while (timeout--) {
  540. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  541. (u8 *)&switch_status);
  542. if (err)
  543. return err;
  544. /* The high-speed function is busy. Try again */
  545. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  546. break;
  547. }
  548. /* If high-speed isn't supported, we return */
  549. if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
  550. return 0;
  551. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
  552. if (err)
  553. return err;
  554. if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
  555. mmc->card_caps |= MMC_MODE_HS;
  556. return 0;
  557. }
  558. /* frequency bases */
  559. /* divided by 10 to be nice to platforms without floating point */
  560. static const int fbase[] = {
  561. 10000,
  562. 100000,
  563. 1000000,
  564. 10000000,
  565. };
  566. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  567. * to platforms without floating point.
  568. */
  569. static const int multipliers[] = {
  570. 0, /* reserved */
  571. 10,
  572. 12,
  573. 13,
  574. 15,
  575. 20,
  576. 25,
  577. 30,
  578. 35,
  579. 40,
  580. 45,
  581. 50,
  582. 55,
  583. 60,
  584. 70,
  585. 80,
  586. };
  587. void mmc_set_ios(struct mmc *mmc)
  588. {
  589. mmc->set_ios(mmc);
  590. }
  591. void mmc_set_clock(struct mmc *mmc, uint clock)
  592. {
  593. if (clock > mmc->f_max)
  594. clock = mmc->f_max;
  595. if (clock < mmc->f_min)
  596. clock = mmc->f_min;
  597. mmc->clock = clock;
  598. mmc_set_ios(mmc);
  599. }
  600. void mmc_set_bus_width(struct mmc *mmc, uint width)
  601. {
  602. mmc->bus_width = width;
  603. mmc_set_ios(mmc);
  604. }
  605. int mmc_startup(struct mmc *mmc)
  606. {
  607. int err;
  608. uint mult, freq;
  609. u64 cmult, csize;
  610. struct mmc_cmd cmd;
  611. char ext_csd[512];
  612. int timeout = 1000;
  613. #ifdef CONFIG_MMC_SPI_CRC_ON
  614. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  615. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  616. cmd.resp_type = MMC_RSP_R1;
  617. cmd.cmdarg = 1;
  618. cmd.flags = 0;
  619. err = mmc_send_cmd(mmc, &cmd, NULL);
  620. if (err)
  621. return err;
  622. }
  623. #endif
  624. /* Put the Card in Identify Mode */
  625. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  626. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  627. cmd.resp_type = MMC_RSP_R2;
  628. cmd.cmdarg = 0;
  629. cmd.flags = 0;
  630. err = mmc_send_cmd(mmc, &cmd, NULL);
  631. if (err)
  632. return err;
  633. memcpy(mmc->cid, cmd.response, 16);
  634. /*
  635. * For MMC cards, set the Relative Address.
  636. * For SD cards, get the Relatvie Address.
  637. * This also puts the cards into Standby State
  638. */
  639. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  640. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  641. cmd.cmdarg = mmc->rca << 16;
  642. cmd.resp_type = MMC_RSP_R6;
  643. cmd.flags = 0;
  644. err = mmc_send_cmd(mmc, &cmd, NULL);
  645. if (err)
  646. return err;
  647. if (IS_SD(mmc))
  648. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  649. }
  650. /* Get the Card-Specific Data */
  651. cmd.cmdidx = MMC_CMD_SEND_CSD;
  652. cmd.resp_type = MMC_RSP_R2;
  653. cmd.cmdarg = mmc->rca << 16;
  654. cmd.flags = 0;
  655. err = mmc_send_cmd(mmc, &cmd, NULL);
  656. /* Waiting for the ready status */
  657. mmc_send_status(mmc, timeout);
  658. if (err)
  659. return err;
  660. mmc->csd[0] = cmd.response[0];
  661. mmc->csd[1] = cmd.response[1];
  662. mmc->csd[2] = cmd.response[2];
  663. mmc->csd[3] = cmd.response[3];
  664. if (mmc->version == MMC_VERSION_UNKNOWN) {
  665. int version = (cmd.response[0] >> 26) & 0xf;
  666. switch (version) {
  667. case 0:
  668. mmc->version = MMC_VERSION_1_2;
  669. break;
  670. case 1:
  671. mmc->version = MMC_VERSION_1_4;
  672. break;
  673. case 2:
  674. mmc->version = MMC_VERSION_2_2;
  675. break;
  676. case 3:
  677. mmc->version = MMC_VERSION_3;
  678. break;
  679. case 4:
  680. mmc->version = MMC_VERSION_4;
  681. break;
  682. default:
  683. mmc->version = MMC_VERSION_1_2;
  684. break;
  685. }
  686. }
  687. /* divide frequency by 10, since the mults are 10x bigger */
  688. freq = fbase[(cmd.response[0] & 0x7)];
  689. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  690. mmc->tran_speed = freq * mult;
  691. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  692. if (IS_SD(mmc))
  693. mmc->write_bl_len = mmc->read_bl_len;
  694. else
  695. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  696. if (mmc->high_capacity) {
  697. csize = (mmc->csd[1] & 0x3f) << 16
  698. | (mmc->csd[2] & 0xffff0000) >> 16;
  699. cmult = 8;
  700. } else {
  701. csize = (mmc->csd[1] & 0x3ff) << 2
  702. | (mmc->csd[2] & 0xc0000000) >> 30;
  703. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  704. }
  705. mmc->capacity = (csize + 1) << (cmult + 2);
  706. mmc->capacity *= mmc->read_bl_len;
  707. if (mmc->read_bl_len > 512)
  708. mmc->read_bl_len = 512;
  709. if (mmc->write_bl_len > 512)
  710. mmc->write_bl_len = 512;
  711. /* Select the card, and put it into Transfer Mode */
  712. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  713. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  714. cmd.resp_type = MMC_RSP_R1b;
  715. cmd.cmdarg = mmc->rca << 16;
  716. cmd.flags = 0;
  717. err = mmc_send_cmd(mmc, &cmd, NULL);
  718. if (err)
  719. return err;
  720. }
  721. mmc->part_config = MMCPART_NOAVAILABLE;
  722. if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
  723. /* check ext_csd version and capacity */
  724. err = mmc_send_ext_csd(mmc, ext_csd);
  725. if (!err & (ext_csd[192] >= 2)) {
  726. mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
  727. ext_csd[214] << 16 | ext_csd[215] << 24;
  728. mmc->capacity *= 512;
  729. }
  730. /* store the partition info of emmc */
  731. if (ext_csd[160] & PART_SUPPORT)
  732. mmc->part_config = ext_csd[179];
  733. }
  734. if (IS_SD(mmc))
  735. err = sd_change_freq(mmc);
  736. else
  737. err = mmc_change_freq(mmc);
  738. if (err)
  739. return err;
  740. /* Restrict card's capabilities by what the host can do */
  741. mmc->card_caps &= mmc->host_caps;
  742. if (IS_SD(mmc)) {
  743. if (mmc->card_caps & MMC_MODE_4BIT) {
  744. cmd.cmdidx = MMC_CMD_APP_CMD;
  745. cmd.resp_type = MMC_RSP_R1;
  746. cmd.cmdarg = mmc->rca << 16;
  747. cmd.flags = 0;
  748. err = mmc_send_cmd(mmc, &cmd, NULL);
  749. if (err)
  750. return err;
  751. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  752. cmd.resp_type = MMC_RSP_R1;
  753. cmd.cmdarg = 2;
  754. cmd.flags = 0;
  755. err = mmc_send_cmd(mmc, &cmd, NULL);
  756. if (err)
  757. return err;
  758. mmc_set_bus_width(mmc, 4);
  759. }
  760. if (mmc->card_caps & MMC_MODE_HS)
  761. mmc_set_clock(mmc, 50000000);
  762. else
  763. mmc_set_clock(mmc, 25000000);
  764. } else {
  765. if (mmc->card_caps & MMC_MODE_4BIT) {
  766. /* Set the card to use 4 bit*/
  767. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  768. EXT_CSD_BUS_WIDTH,
  769. EXT_CSD_BUS_WIDTH_4);
  770. if (err)
  771. return err;
  772. mmc_set_bus_width(mmc, 4);
  773. } else if (mmc->card_caps & MMC_MODE_8BIT) {
  774. /* Set the card to use 8 bit*/
  775. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  776. EXT_CSD_BUS_WIDTH,
  777. EXT_CSD_BUS_WIDTH_8);
  778. if (err)
  779. return err;
  780. mmc_set_bus_width(mmc, 8);
  781. }
  782. if (mmc->card_caps & MMC_MODE_HS) {
  783. if (mmc->card_caps & MMC_MODE_HS_52MHz)
  784. mmc_set_clock(mmc, 52000000);
  785. else
  786. mmc_set_clock(mmc, 26000000);
  787. } else
  788. mmc_set_clock(mmc, 20000000);
  789. }
  790. /* fill in device description */
  791. mmc->block_dev.lun = 0;
  792. mmc->block_dev.type = 0;
  793. mmc->block_dev.blksz = mmc->read_bl_len;
  794. mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
  795. sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
  796. (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
  797. sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
  798. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  799. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  800. sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
  801. (mmc->cid[2] >> 24) & 0xf);
  802. init_part(&mmc->block_dev);
  803. return 0;
  804. }
  805. int mmc_send_if_cond(struct mmc *mmc)
  806. {
  807. struct mmc_cmd cmd;
  808. int err;
  809. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  810. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  811. cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
  812. cmd.resp_type = MMC_RSP_R7;
  813. cmd.flags = 0;
  814. err = mmc_send_cmd(mmc, &cmd, NULL);
  815. if (err)
  816. return err;
  817. if ((cmd.response[0] & 0xff) != 0xaa)
  818. return UNUSABLE_ERR;
  819. else
  820. mmc->version = SD_VERSION_2;
  821. return 0;
  822. }
  823. int mmc_register(struct mmc *mmc)
  824. {
  825. /* Setup the universal parts of the block interface just once */
  826. mmc->block_dev.if_type = IF_TYPE_MMC;
  827. mmc->block_dev.dev = cur_dev_num++;
  828. mmc->block_dev.removable = 1;
  829. mmc->block_dev.block_read = mmc_bread;
  830. mmc->block_dev.block_write = mmc_bwrite;
  831. if (!mmc->b_max)
  832. mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  833. INIT_LIST_HEAD (&mmc->link);
  834. list_add_tail (&mmc->link, &mmc_devices);
  835. return 0;
  836. }
  837. block_dev_desc_t *mmc_get_dev(int dev)
  838. {
  839. struct mmc *mmc = find_mmc_device(dev);
  840. return mmc ? &mmc->block_dev : NULL;
  841. }
  842. int mmc_init(struct mmc *mmc)
  843. {
  844. int err;
  845. if (mmc->has_init)
  846. return 0;
  847. err = mmc->init(mmc);
  848. if (err)
  849. return err;
  850. mmc_set_bus_width(mmc, 1);
  851. mmc_set_clock(mmc, 1);
  852. /* Reset the Card */
  853. err = mmc_go_idle(mmc);
  854. if (err)
  855. return err;
  856. /* The internal partition reset to user partition(0) at every CMD0*/
  857. mmc->part_num = 0;
  858. /* Test for SD version 2 */
  859. err = mmc_send_if_cond(mmc);
  860. /* Now try to get the SD card's operating condition */
  861. err = sd_send_op_cond(mmc);
  862. /* If the command timed out, we check for an MMC card */
  863. if (err == TIMEOUT) {
  864. err = mmc_send_op_cond(mmc);
  865. if (err) {
  866. printf("Card did not respond to voltage select!\n");
  867. return UNUSABLE_ERR;
  868. }
  869. }
  870. err = mmc_startup(mmc);
  871. if (err)
  872. mmc->has_init = 0;
  873. else
  874. mmc->has_init = 1;
  875. return err;
  876. }
  877. /*
  878. * CPU and board-specific MMC initializations. Aliased function
  879. * signals caller to move on
  880. */
  881. static int __def_mmc_init(bd_t *bis)
  882. {
  883. return -1;
  884. }
  885. int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  886. int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
  887. void print_mmc_devices(char separator)
  888. {
  889. struct mmc *m;
  890. struct list_head *entry;
  891. list_for_each(entry, &mmc_devices) {
  892. m = list_entry(entry, struct mmc, link);
  893. printf("%s: %d", m->name, m->block_dev.dev);
  894. if (entry->next != &mmc_devices)
  895. printf("%c ", separator);
  896. }
  897. printf("\n");
  898. }
  899. int get_mmc_num(void)
  900. {
  901. return cur_dev_num;
  902. }
  903. int mmc_initialize(bd_t *bis)
  904. {
  905. INIT_LIST_HEAD (&mmc_devices);
  906. cur_dev_num = 0;
  907. if (board_mmc_init(bis) < 0)
  908. cpu_mmc_init(bis);
  909. print_mmc_devices(',');
  910. return 0;
  911. }