pxa_mmc.c 18 KB

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