mmc.c 17 KB

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