mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * (C) Copyright 2003
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <mmc.h>
  26. #include <asm/errno.h>
  27. #include <asm/arch/hardware.h>
  28. #include <part.h>
  29. #ifdef CONFIG_MMC
  30. extern int fat_register_device(block_dev_desc_t * dev_desc, int part_no);
  31. static block_dev_desc_t mmc_dev;
  32. block_dev_desc_t *mmc_get_dev(int dev)
  33. {
  34. return ((block_dev_desc_t *) & mmc_dev);
  35. }
  36. /*
  37. * FIXME needs to read cid and csd info to determine block size
  38. * and other parameters
  39. */
  40. static uchar mmc_buf[MMC_BLOCK_SIZE];
  41. static uchar spec_ver;
  42. static int mmc_ready = 0;
  43. static int wide = 0;
  44. static uint32_t *
  45. /****************************************************/
  46. mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
  47. /****************************************************/
  48. {
  49. static uint32_t resp[4], a, b, c;
  50. ulong status;
  51. int i;
  52. debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd, argh, argl,
  53. cmdat | wide);
  54. MMC_STRPCL = MMC_STRPCL_STOP_CLK;
  55. MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
  56. while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF)) ;
  57. MMC_CMD = cmd;
  58. MMC_ARGH = argh;
  59. MMC_ARGL = argl;
  60. MMC_CMDAT = cmdat | wide;
  61. MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
  62. MMC_STRPCL = MMC_STRPCL_START_CLK;
  63. while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES)) ;
  64. status = MMC_STAT;
  65. debug("MMC status 0x%08x\n", status);
  66. if (status & MMC_STAT_TIME_OUT_RESPONSE) {
  67. return 0;
  68. }
  69. /* Linux says:
  70. * Did I mention this is Sick. We always need to
  71. * discard the upper 8 bits of the first 16-bit word.
  72. */
  73. a = (MMC_RES & 0xffff);
  74. for (i = 0; i < 4; i++) {
  75. b = (MMC_RES & 0xffff);
  76. c = (MMC_RES & 0xffff);
  77. resp[i] = (a << 24) | (b << 8) | (c >> 8);
  78. a = c;
  79. debug("MMC resp[%d] = %#08x\n", i, resp[i]);
  80. }
  81. return resp;
  82. }
  83. int
  84. /****************************************************/
  85. mmc_block_read(uchar * dst, ulong src, ulong len)
  86. /****************************************************/
  87. {
  88. ushort argh, argl;
  89. ulong status;
  90. if (len == 0) {
  91. return 0;
  92. }
  93. debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong) dst, src, len);
  94. argh = len >> 16;
  95. argl = len & 0xffff;
  96. /* set block len */
  97. mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
  98. /* send read command */
  99. argh = src >> 16;
  100. argl = src & 0xffff;
  101. MMC_STRPCL = MMC_STRPCL_STOP_CLK;
  102. MMC_RDTO = 0xffff;
  103. MMC_NOB = 1;
  104. MMC_BLKLEN = len;
  105. mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK, argh, argl,
  106. MMC_CMDAT_R1 | MMC_CMDAT_READ | MMC_CMDAT_BLOCK |
  107. MMC_CMDAT_DATA_EN);
  108. MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
  109. while (len) {
  110. if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
  111. #ifdef CONFIG_PXA27X
  112. int i;
  113. for (i = min(len, 32); i; i--) {
  114. *dst++ = *((volatile uchar *)&MMC_RXFIFO);
  115. len--;
  116. }
  117. #else
  118. *dst++ = MMC_RXFIFO;
  119. len--;
  120. #endif
  121. }
  122. status = MMC_STAT;
  123. if (status & MMC_STAT_ERRORS) {
  124. printf("MMC_STAT error %lx\n", status);
  125. return -1;
  126. }
  127. }
  128. MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
  129. while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
  130. status = MMC_STAT;
  131. if (status & MMC_STAT_ERRORS) {
  132. printf("MMC_STAT error %lx\n", status);
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. int
  138. /****************************************************/
  139. mmc_block_write(ulong dst, uchar * src, int len)
  140. /****************************************************/
  141. {
  142. ushort argh, argl;
  143. ulong status;
  144. if (len == 0) {
  145. return 0;
  146. }
  147. debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong) src, len);
  148. argh = len >> 16;
  149. argl = len & 0xffff;
  150. /* set block len */
  151. mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
  152. /* send write command */
  153. argh = dst >> 16;
  154. argl = dst & 0xffff;
  155. MMC_STRPCL = MMC_STRPCL_STOP_CLK;
  156. MMC_NOB = 1;
  157. MMC_BLKLEN = len;
  158. mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
  159. MMC_CMDAT_R1 | MMC_CMDAT_WRITE | MMC_CMDAT_BLOCK |
  160. MMC_CMDAT_DATA_EN);
  161. MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
  162. while (len) {
  163. if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
  164. int i, bytes = min(32, len);
  165. for (i = 0; i < bytes; i++) {
  166. MMC_TXFIFO = *src++;
  167. }
  168. if (bytes < 32) {
  169. MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
  170. }
  171. len -= bytes;
  172. }
  173. status = MMC_STAT;
  174. if (status & MMC_STAT_ERRORS) {
  175. printf("MMC_STAT error %lx\n", status);
  176. return -1;
  177. }
  178. }
  179. MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
  180. while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
  181. MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
  182. while (!(MMC_I_REG & MMC_I_REG_PRG_DONE)) ;
  183. status = MMC_STAT;
  184. if (status & MMC_STAT_ERRORS) {
  185. printf("MMC_STAT error %lx\n", status);
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. int
  191. /****************************************************/
  192. mmc_read(ulong src, uchar * dst, int size)
  193. /****************************************************/
  194. {
  195. ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
  196. ulong mmc_block_size, mmc_block_address;
  197. if (size == 0) {
  198. return 0;
  199. }
  200. if (!mmc_ready) {
  201. printf("Please initial the MMC first\n");
  202. return -1;
  203. }
  204. mmc_block_size = MMC_BLOCK_SIZE;
  205. mmc_block_address = ~(mmc_block_size - 1);
  206. src -= CFG_MMC_BASE;
  207. end = src + size;
  208. part_start = ~mmc_block_address & src;
  209. part_end = ~mmc_block_address & end;
  210. aligned_start = mmc_block_address & src;
  211. aligned_end = mmc_block_address & end;
  212. /* all block aligned accesses */
  213. debug
  214. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  215. src, (ulong) dst, end, part_start, part_end, aligned_start,
  216. aligned_end);
  217. if (part_start) {
  218. part_len = mmc_block_size - part_start;
  219. debug
  220. ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  221. src, (ulong) dst, end, part_start, part_end, aligned_start,
  222. aligned_end);
  223. if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
  224. 0) {
  225. return -1;
  226. }
  227. memcpy(dst, mmc_buf + part_start, part_len);
  228. dst += part_len;
  229. src += part_len;
  230. }
  231. debug
  232. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  233. src, (ulong) dst, end, part_start, part_end, aligned_start,
  234. aligned_end);
  235. for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
  236. debug
  237. ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  238. src, (ulong) dst, end, part_start, part_end, aligned_start,
  239. aligned_end);
  240. if ((mmc_block_read((uchar *) (dst), src, mmc_block_size)) < 0) {
  241. return -1;
  242. }
  243. }
  244. debug
  245. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  246. src, (ulong) dst, end, part_start, part_end, aligned_start,
  247. aligned_end);
  248. if (part_end && src < end) {
  249. debug
  250. ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  251. src, (ulong) dst, end, part_start, part_end, aligned_start,
  252. aligned_end);
  253. if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
  254. return -1;
  255. }
  256. memcpy(dst, mmc_buf, part_end);
  257. }
  258. return 0;
  259. }
  260. int
  261. /****************************************************/
  262. mmc_write(uchar * src, ulong dst, int size)
  263. /****************************************************/
  264. {
  265. ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
  266. ulong mmc_block_size, mmc_block_address;
  267. if (size == 0) {
  268. return 0;
  269. }
  270. if (!mmc_ready) {
  271. printf("Please initial the MMC first\n");
  272. return -1;
  273. }
  274. mmc_block_size = MMC_BLOCK_SIZE;
  275. mmc_block_address = ~(mmc_block_size - 1);
  276. dst -= CFG_MMC_BASE;
  277. end = dst + size;
  278. part_start = ~mmc_block_address & dst;
  279. part_end = ~mmc_block_address & end;
  280. aligned_start = mmc_block_address & dst;
  281. aligned_end = mmc_block_address & end;
  282. /* all block aligned accesses */
  283. debug
  284. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  285. src, (ulong) dst, end, part_start, part_end, aligned_start,
  286. aligned_end);
  287. if (part_start) {
  288. part_len = mmc_block_size - part_start;
  289. debug
  290. ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  291. (ulong) src, dst, end, part_start, part_end, aligned_start,
  292. aligned_end);
  293. if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
  294. 0) {
  295. return -1;
  296. }
  297. memcpy(mmc_buf + part_start, src, part_len);
  298. if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) <
  299. 0) {
  300. return -1;
  301. }
  302. dst += part_len;
  303. src += part_len;
  304. }
  305. debug
  306. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  307. src, (ulong) dst, end, part_start, part_end, aligned_start,
  308. aligned_end);
  309. for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
  310. debug
  311. ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  312. src, (ulong) dst, end, part_start, part_end, aligned_start,
  313. aligned_end);
  314. if ((mmc_block_write(dst, (uchar *) src, mmc_block_size)) < 0) {
  315. return -1;
  316. }
  317. }
  318. debug
  319. ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  320. src, (ulong) dst, end, part_start, part_end, aligned_start,
  321. aligned_end);
  322. if (part_end && dst < end) {
  323. debug
  324. ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
  325. src, (ulong) dst, end, part_start, part_end, aligned_start,
  326. aligned_end);
  327. if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
  328. return -1;
  329. }
  330. memcpy(mmc_buf, src, part_end);
  331. if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
  332. return -1;
  333. }
  334. }
  335. return 0;
  336. }
  337. ulong
  338. /****************************************************/
  339. mmc_bread(int dev_num, ulong blknr, lbaint_t blkcnt, void *dst)
  340. /****************************************************/
  341. {
  342. int mmc_block_size = MMC_BLOCK_SIZE;
  343. ulong src = blknr * mmc_block_size + CFG_MMC_BASE;
  344. mmc_read(src, (uchar *) dst, blkcnt * mmc_block_size);
  345. return blkcnt;
  346. }
  347. #ifdef __GNUC__
  348. #define likely(x) __builtin_expect(!!(x), 1)
  349. #define unlikely(x) __builtin_expect(!!(x), 0)
  350. #else
  351. #define likely(x) (x)
  352. #define unlikely(x) (x)
  353. #endif
  354. #define UNSTUFF_BITS(resp,start,size) \
  355. ({ \
  356. const int __size = size; \
  357. const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  358. const int32_t __off = 3 - ((start) / 32); \
  359. const int32_t __shft = (start) & 31; \
  360. uint32_t __res; \
  361. \
  362. __res = resp[__off] >> __shft; \
  363. if (__size + __shft > 32) \
  364. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  365. __res & __mask; \
  366. })
  367. /*
  368. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  369. */
  370. static void mmc_decode_cid(uint32_t * resp)
  371. {
  372. if (IF_TYPE_SD == mmc_dev.if_type) {
  373. /*
  374. * SD doesn't currently have a version field so we will
  375. * have to assume we can parse this.
  376. */
  377. sprintf((char *)mmc_dev.vendor,
  378. "Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
  379. UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp, 112, 8),
  380. UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp, 96, 8),
  381. UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp, 80, 8),
  382. UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp, 64, 8),
  383. UNSTUFF_BITS(resp, 8, 4), UNSTUFF_BITS(resp, 12,
  384. 8) + 2000);
  385. sprintf((char *)mmc_dev.revision, "%d.%d",
  386. UNSTUFF_BITS(resp, 60, 4), UNSTUFF_BITS(resp, 56, 4));
  387. sprintf((char *)mmc_dev.product, "%u",
  388. UNSTUFF_BITS(resp, 24, 32));
  389. } else {
  390. /*
  391. * The selection of the format here is based upon published
  392. * specs from sandisk and from what people have reported.
  393. */
  394. switch (spec_ver) {
  395. case 0: /* MMC v1.0 - v1.2 */
  396. case 1: /* MMC v1.4 */
  397. sprintf((char *)mmc_dev.vendor,
  398. "Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
  399. UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
  400. 112,
  401. 8),
  402. UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp,
  403. 96, 8),
  404. UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp,
  405. 80, 8),
  406. UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp,
  407. 64, 8),
  408. UNSTUFF_BITS(resp, 56, 8), UNSTUFF_BITS(resp,
  409. 48, 8),
  410. UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
  411. 4) +
  412. 1997);
  413. sprintf((char *)mmc_dev.revision, "%d.%d",
  414. UNSTUFF_BITS(resp, 44, 4), UNSTUFF_BITS(resp,
  415. 40, 4));
  416. sprintf((char *)mmc_dev.product, "%u",
  417. UNSTUFF_BITS(resp, 16, 24));
  418. break;
  419. case 2: /* MMC v2.0 - v2.2 */
  420. case 3: /* MMC v3.1 - v3.3 */
  421. case 4: /* MMC v4 */
  422. sprintf((char *)mmc_dev.vendor,
  423. "Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
  424. UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
  425. 104,
  426. 16),
  427. UNSTUFF_BITS(resp, 96, 8), UNSTUFF_BITS(resp,
  428. 88, 8),
  429. UNSTUFF_BITS(resp, 80, 8), UNSTUFF_BITS(resp,
  430. 72, 8),
  431. UNSTUFF_BITS(resp, 64, 8), UNSTUFF_BITS(resp,
  432. 56, 8),
  433. UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
  434. 4) +
  435. 1997);
  436. sprintf((char *)mmc_dev.product, "%u",
  437. UNSTUFF_BITS(resp, 16, 32));
  438. sprintf((char *)mmc_dev.revision, "N/A");
  439. break;
  440. default:
  441. printf("MMC card has unknown MMCA version %d\n",
  442. spec_ver);
  443. break;
  444. }
  445. }
  446. printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
  447. (IF_TYPE_SD == mmc_dev.if_type) ? "SD" : "MMC", mmc_dev.vendor,
  448. mmc_dev.product, mmc_dev.revision);
  449. }
  450. /*
  451. * Given a 128-bit response, decode to our card CSD structure.
  452. */
  453. static void mmc_decode_csd(uint32_t * resp)
  454. {
  455. unsigned int mult, csd_struct;
  456. if (IF_TYPE_SD == mmc_dev.if_type) {
  457. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  458. if (csd_struct != 0) {
  459. printf("SD: unrecognised CSD structure version %d\n",
  460. csd_struct);
  461. return;
  462. }
  463. } else {
  464. /*
  465. * We only understand CSD structure v1.1 and v1.2.
  466. * v1.2 has extra information in bits 15, 11 and 10.
  467. */
  468. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  469. if (csd_struct != 1 && csd_struct != 2) {
  470. printf("MMC: unrecognised CSD structure version %d\n",
  471. csd_struct);
  472. return;
  473. }
  474. spec_ver = UNSTUFF_BITS(resp, 122, 4);
  475. mmc_dev.if_type = IF_TYPE_MMC;
  476. }
  477. mult = 1 << (UNSTUFF_BITS(resp, 47, 3) + 2);
  478. mmc_dev.lba = (1 + UNSTUFF_BITS(resp, 62, 12)) * mult;
  479. mmc_dev.blksz = 1 << UNSTUFF_BITS(resp, 80, 4);
  480. /* FIXME: The following just makes assumes that's the partition type -- should really read it */
  481. mmc_dev.part_type = PART_TYPE_DOS;
  482. mmc_dev.dev = 0;
  483. mmc_dev.lun = 0;
  484. mmc_dev.type = DEV_TYPE_HARDDISK;
  485. mmc_dev.removable = 0;
  486. mmc_dev.block_read = mmc_bread;
  487. printf("Detected: %lu blocks of %lu bytes (%luMB) ",
  488. mmc_dev.lba,
  489. mmc_dev.blksz,
  490. mmc_dev.lba * mmc_dev.blksz / (1024 * 1024));
  491. }
  492. int
  493. /****************************************************/
  494. mmc_init(int verbose)
  495. /****************************************************/
  496. {
  497. int retries, rc = -ENODEV;
  498. uint32_t cid_resp[4];
  499. uint32_t *resp;
  500. uint16_t rca = 0;
  501. /* Reset device interface type */
  502. mmc_dev.if_type = IF_TYPE_UNKNOWN;
  503. #if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
  504. set_GPIO_mode(GPIO6_MMCCLK_MD);
  505. set_GPIO_mode(GPIO8_MMCCS0_MD);
  506. #endif
  507. CKEN |= CKEN12_MMC; /* enable MMC unit clock */
  508. MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
  509. MMC_RESTO = MMC_RES_TO_MAX;
  510. MMC_SPI = MMC_SPI_DISABLE;
  511. /* reset */
  512. mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, 0, MMC_CMDAT_INIT | MMC_CMDAT_R0);
  513. udelay(200000);
  514. retries = 3;
  515. while (retries--) {
  516. resp = mmc_cmd(MMC_CMD_APP_CMD, 0, 0, MMC_CMDAT_R1);
  517. if (!(resp[0] & 0x00000020)) { /* Card does not support APP_CMD */
  518. debug("Card does not support APP_CMD\n");
  519. break;
  520. }
  521. /* Select 3.2-3.3 and 3.3-3.4V */
  522. resp = mmc_cmd(SD_CMD_APP_SEND_OP_COND, 0x0020, 0,
  523. MMC_CMDAT_R3 | (retries < 2 ? 0
  524. : MMC_CMDAT_INIT));
  525. if (resp[0] & 0x80000000) {
  526. mmc_dev.if_type = IF_TYPE_SD;
  527. debug("Detected SD card\n");
  528. break;
  529. }
  530. #ifdef CONFIG_PXA27X
  531. udelay(10000);
  532. #else
  533. udelay(200000);
  534. #endif
  535. }
  536. if (retries <= 0 || !(IF_TYPE_SD == mmc_dev.if_type)) {
  537. debug("Failed to detect SD Card, trying MMC\n");
  538. resp =
  539. mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000, MMC_CMDAT_R3);
  540. retries = 10;
  541. while (retries-- && resp && !(resp[0] & 0x80000000)) {
  542. #ifdef CONFIG_PXA27X
  543. udelay(10000);
  544. #else
  545. udelay(200000);
  546. #endif
  547. resp =
  548. mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000,
  549. MMC_CMDAT_R3);
  550. }
  551. }
  552. /* try to get card id */
  553. resp =
  554. mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, 0, MMC_CMDAT_R2 | MMC_CMDAT_BUSY);
  555. if (resp) {
  556. memcpy(cid_resp, resp, sizeof(cid_resp));
  557. /* MMC exists, get CSD too */
  558. resp = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, 0, 0, MMC_CMDAT_R1);
  559. if (IF_TYPE_SD == mmc_dev.if_type)
  560. rca = ((resp[0] & 0xffff0000) >> 16);
  561. resp = mmc_cmd(MMC_CMD_SEND_CSD, rca, 0, MMC_CMDAT_R2);
  562. if (resp) {
  563. mmc_decode_csd(resp);
  564. rc = 0;
  565. mmc_ready = 1;
  566. }
  567. mmc_decode_cid(cid_resp);
  568. }
  569. MMC_CLKRT = 0; /* 20 MHz */
  570. resp = mmc_cmd(MMC_CMD_SELECT_CARD, rca, 0, MMC_CMDAT_R1);
  571. #ifdef CONFIG_PXA27X
  572. if (IF_TYPE_SD == mmc_dev.if_type) {
  573. resp = mmc_cmd(MMC_CMD_APP_CMD, rca, 0, MMC_CMDAT_R1);
  574. resp = mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH, 0, 2, MMC_CMDAT_R1);
  575. wide = MMC_CMDAT_SD_4DAT;
  576. }
  577. #endif
  578. fat_register_device(&mmc_dev, 1); /* partitions start counting with 1 */
  579. return rc;
  580. }
  581. int mmc_ident(block_dev_desc_t * dev)
  582. {
  583. return 0;
  584. }
  585. int mmc2info(ulong addr)
  586. {
  587. if (addr >= CFG_MMC_BASE
  588. && addr < CFG_MMC_BASE + (mmc_dev.lba * mmc_dev.blksz)) {
  589. return 1;
  590. }
  591. return 0;
  592. }
  593. #endif /* CONFIG_MMC */