atmel_mci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #ifdef CONFIG_MMC
  24. #include <part.h>
  25. #include <mmc.h>
  26. #include <asm/io.h>
  27. #include <asm/errno.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/arch/clk.h>
  30. #include <asm/arch/memory-map.h>
  31. #include "atmel_mci.h"
  32. #ifdef DEBUG
  33. #define pr_debug(fmt, args...) printf(fmt, ##args)
  34. #else
  35. #define pr_debug(...) do { } while(0)
  36. #endif
  37. #ifndef CFG_MMC_CLK_OD
  38. #define CFG_MMC_CLK_OD 150000
  39. #endif
  40. #ifndef CFG_MMC_CLK_PP
  41. #define CFG_MMC_CLK_PP 5000000
  42. #endif
  43. #ifndef CFG_MMC_OP_COND
  44. #define CFG_MMC_OP_COND 0x00100000
  45. #endif
  46. #define MMC_DEFAULT_BLKLEN 512
  47. #define MMC_DEFAULT_RCA 1
  48. static unsigned int mmc_rca;
  49. static int mmc_card_is_sd;
  50. static block_dev_desc_t mmc_blkdev;
  51. block_dev_desc_t *mmc_get_dev(int dev)
  52. {
  53. return &mmc_blkdev;
  54. }
  55. static void mci_set_mode(unsigned long hz, unsigned long blklen)
  56. {
  57. unsigned long bus_hz;
  58. unsigned long clkdiv;
  59. bus_hz = get_mci_clk_rate();
  60. clkdiv = (bus_hz / hz) / 2 - 1;
  61. pr_debug("mmc: setting clock %lu Hz, block size %lu\n",
  62. hz, blklen);
  63. if (clkdiv & ~255UL) {
  64. clkdiv = 255;
  65. printf("mmc: clock %lu too low; setting CLKDIV to 255\n",
  66. hz);
  67. }
  68. blklen &= 0xfffc;
  69. mmci_writel(MR, (MMCI_BF(CLKDIV, clkdiv)
  70. | MMCI_BF(BLKLEN, blklen)
  71. | MMCI_BIT(RDPROOF)
  72. | MMCI_BIT(WRPROOF)));
  73. }
  74. #define RESP_NO_CRC 1
  75. #define R1 MMCI_BF(RSPTYP, 1)
  76. #define R2 MMCI_BF(RSPTYP, 2)
  77. #define R3 (R1 | RESP_NO_CRC)
  78. #define R6 R1
  79. #define NID MMCI_BF(MAXLAT, 0)
  80. #define NCR MMCI_BF(MAXLAT, 1)
  81. #define TRCMD_START MMCI_BF(TRCMD, 1)
  82. #define TRDIR_READ MMCI_BF(TRDIR, 1)
  83. #define TRTYP_BLOCK MMCI_BF(TRTYP, 0)
  84. #define INIT_CMD MMCI_BF(SPCMD, 1)
  85. #define OPEN_DRAIN MMCI_BF(OPDCMD, 1)
  86. #define ERROR_FLAGS (MMCI_BIT(DTOE) \
  87. | MMCI_BIT(RDIRE) \
  88. | MMCI_BIT(RENDE) \
  89. | MMCI_BIT(RINDE) \
  90. | MMCI_BIT(RTOE))
  91. static int
  92. mmc_cmd(unsigned long cmd, unsigned long arg,
  93. void *resp, unsigned long flags)
  94. {
  95. unsigned long *response = resp;
  96. int i, response_words = 0;
  97. unsigned long error_flags;
  98. u32 status;
  99. pr_debug("mmc: CMD%lu 0x%lx (flags 0x%lx)\n",
  100. cmd, arg, flags);
  101. error_flags = ERROR_FLAGS;
  102. if (!(flags & RESP_NO_CRC))
  103. error_flags |= MMCI_BIT(RCRCE);
  104. flags &= ~MMCI_BF(CMDNB, ~0UL);
  105. if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_48_BIT_RESP)
  106. response_words = 1;
  107. else if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_136_BIT_RESP)
  108. response_words = 4;
  109. mmci_writel(ARGR, arg);
  110. mmci_writel(CMDR, cmd | flags);
  111. do {
  112. udelay(40);
  113. status = mmci_readl(SR);
  114. } while (!(status & MMCI_BIT(CMDRDY)));
  115. pr_debug("mmc: status 0x%08lx\n", status);
  116. if (status & error_flags) {
  117. printf("mmc: command %lu failed (status: 0x%08lx)\n",
  118. cmd, status);
  119. return -EIO;
  120. }
  121. if (response_words)
  122. pr_debug("mmc: response:");
  123. for (i = 0; i < response_words; i++) {
  124. response[i] = mmci_readl(RSPR);
  125. pr_debug(" %08lx", response[i]);
  126. }
  127. pr_debug("\n");
  128. return 0;
  129. }
  130. static int mmc_acmd(unsigned long cmd, unsigned long arg,
  131. void *resp, unsigned long flags)
  132. {
  133. unsigned long aresp[4];
  134. int ret;
  135. /*
  136. * Seems like the APP_CMD part of an ACMD has 64 cycles max
  137. * latency even though the ACMD part doesn't. This isn't
  138. * entirely clear in the SD Card spec, but some cards refuse
  139. * to work if we attempt to use 5 cycles max latency here...
  140. */
  141. ret = mmc_cmd(MMC_CMD_APP_CMD, 0, aresp,
  142. R1 | NCR | (flags & OPEN_DRAIN));
  143. if (ret)
  144. return ret;
  145. if ((aresp[0] & (R1_ILLEGAL_COMMAND | R1_APP_CMD)) != R1_APP_CMD)
  146. return -ENODEV;
  147. ret = mmc_cmd(cmd, arg, resp, flags);
  148. return ret;
  149. }
  150. static unsigned long
  151. mmc_bread(int dev, unsigned long start, lbaint_t blkcnt,
  152. void *buffer)
  153. {
  154. int ret, i = 0;
  155. unsigned long resp[4];
  156. unsigned long card_status, data;
  157. unsigned long wordcount;
  158. u32 *p = buffer;
  159. u32 status;
  160. if (blkcnt == 0)
  161. return 0;
  162. pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n",
  163. dev, start, blkcnt);
  164. /* Put the device into Transfer state */
  165. ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR);
  166. if (ret) goto out;
  167. /* Set block length */
  168. ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR);
  169. if (ret) goto out;
  170. pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR));
  171. for (i = 0; i < blkcnt; i++, start++) {
  172. ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK,
  173. start * mmc_blkdev.blksz, resp,
  174. (R1 | NCR | TRCMD_START | TRDIR_READ
  175. | TRTYP_BLOCK));
  176. if (ret) goto out;
  177. ret = -EIO;
  178. wordcount = 0;
  179. do {
  180. do {
  181. status = mmci_readl(SR);
  182. if (status & (ERROR_FLAGS | MMCI_BIT(OVRE)))
  183. goto read_error;
  184. } while (!(status & MMCI_BIT(RXRDY)));
  185. if (status & MMCI_BIT(RXRDY)) {
  186. data = mmci_readl(RDR);
  187. /* pr_debug("%x\n", data); */
  188. *p++ = data;
  189. wordcount++;
  190. }
  191. } while(wordcount < (mmc_blkdev.blksz / 4));
  192. pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount);
  193. do {
  194. status = mmci_readl(SR);
  195. } while (!(status & MMCI_BIT(BLKE)));
  196. putc('.');
  197. }
  198. out:
  199. /* Put the device back into Standby state */
  200. mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR);
  201. return i;
  202. read_error:
  203. mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR);
  204. printf("mmc: bread failed, status = %08x, card status = %08x\n",
  205. status, card_status);
  206. goto out;
  207. }
  208. static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  209. {
  210. cid->mid = resp[0] >> 24;
  211. cid->oid = (resp[0] >> 8) & 0xffff;
  212. cid->pnm[0] = resp[0];
  213. cid->pnm[1] = resp[1] >> 24;
  214. cid->pnm[2] = resp[1] >> 16;
  215. cid->pnm[3] = resp[1] >> 8;
  216. cid->pnm[4] = resp[1];
  217. cid->pnm[5] = resp[2] >> 24;
  218. cid->pnm[6] = 0;
  219. cid->prv = resp[2] >> 16;
  220. cid->psn = (resp[2] << 16) | (resp[3] >> 16);
  221. cid->mdt = resp[3] >> 8;
  222. }
  223. static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  224. {
  225. cid->mid = resp[0] >> 24;
  226. cid->oid = (resp[0] >> 8) & 0xffff;
  227. cid->pnm[0] = resp[0];
  228. cid->pnm[1] = resp[1] >> 24;
  229. cid->pnm[2] = resp[1] >> 16;
  230. cid->pnm[3] = resp[1] >> 8;
  231. cid->pnm[4] = resp[1];
  232. cid->pnm[5] = 0;
  233. cid->pnm[6] = 0;
  234. cid->prv = resp[2] >> 24;
  235. cid->psn = (resp[2] << 8) | (resp[3] >> 24);
  236. cid->mdt = (resp[3] >> 8) & 0x0fff;
  237. }
  238. static void mmc_dump_cid(const struct mmc_cid *cid)
  239. {
  240. printf("Manufacturer ID: %02lX\n", cid->mid);
  241. printf("OEM/Application ID: %04lX\n", cid->oid);
  242. printf("Product name: %s\n", cid->pnm);
  243. printf("Product Revision: %lu.%lu\n",
  244. cid->prv >> 4, cid->prv & 0x0f);
  245. printf("Product Serial Number: %lu\n", cid->psn);
  246. printf("Manufacturing Date: %02lu/%02lu\n",
  247. cid->mdt >> 4, cid->mdt & 0x0f);
  248. }
  249. static void mmc_dump_csd(const struct mmc_csd *csd)
  250. {
  251. unsigned long *csd_raw = (unsigned long *)csd;
  252. printf("CSD data: %08lx %08lx %08lx %08lx\n",
  253. csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]);
  254. printf("CSD structure version: 1.%u\n", csd->csd_structure);
  255. printf("MMC System Spec version: %u\n", csd->spec_vers);
  256. printf("Card command classes: %03x\n", csd->ccc);
  257. printf("Read block length: %u\n", 1 << csd->read_bl_len);
  258. if (csd->read_bl_partial)
  259. puts("Supports partial reads\n");
  260. else
  261. puts("Does not support partial reads\n");
  262. printf("Write block length: %u\n", 1 << csd->write_bl_len);
  263. if (csd->write_bl_partial)
  264. puts("Supports partial writes\n");
  265. else
  266. puts("Does not support partial writes\n");
  267. if (csd->wp_grp_enable)
  268. printf("Supports group WP: %u\n", csd->wp_grp_size + 1);
  269. else
  270. puts("Does not support group WP\n");
  271. printf("Card capacity: %u bytes\n",
  272. (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) *
  273. (1 << csd->read_bl_len));
  274. printf("File format: %u/%u\n",
  275. csd->file_format_grp, csd->file_format);
  276. puts("Write protection: ");
  277. if (csd->perm_write_protect)
  278. puts(" permanent");
  279. if (csd->tmp_write_protect)
  280. puts(" temporary");
  281. putc('\n');
  282. }
  283. static int mmc_idle_cards(void)
  284. {
  285. int ret;
  286. /* Reset and initialize all cards */
  287. ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0);
  288. if (ret)
  289. return ret;
  290. /* Keep the bus idle for 74 clock cycles */
  291. return mmc_cmd(0, 0, NULL, INIT_CMD);
  292. }
  293. static int sd_init_card(struct mmc_cid *cid, int verbose)
  294. {
  295. unsigned long resp[4];
  296. int i, ret = 0;
  297. mmc_idle_cards();
  298. for (i = 0; i < 1000; i++) {
  299. ret = mmc_acmd(MMC_ACMD_SD_SEND_OP_COND, CFG_MMC_OP_COND,
  300. resp, R3 | NID);
  301. if (ret || (resp[0] & 0x80000000))
  302. break;
  303. ret = -ETIMEDOUT;
  304. }
  305. if (ret)
  306. return ret;
  307. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID);
  308. if (ret)
  309. return ret;
  310. sd_parse_cid(cid, resp);
  311. if (verbose)
  312. mmc_dump_cid(cid);
  313. /* Get RCA of the card that responded */
  314. ret = mmc_cmd(MMC_CMD_SD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR);
  315. if (ret)
  316. return ret;
  317. mmc_rca = resp[0] >> 16;
  318. if (verbose)
  319. printf("SD Card detected (RCA %u)\n", mmc_rca);
  320. mmc_card_is_sd = 1;
  321. return 0;
  322. }
  323. static int mmc_init_card(struct mmc_cid *cid, int verbose)
  324. {
  325. unsigned long resp[4];
  326. int i, ret = 0;
  327. mmc_idle_cards();
  328. for (i = 0; i < 1000; i++) {
  329. ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CFG_MMC_OP_COND, resp,
  330. R3 | NID | OPEN_DRAIN);
  331. if (ret || (resp[0] & 0x80000000))
  332. break;
  333. ret = -ETIMEDOUT;
  334. }
  335. if (ret)
  336. return ret;
  337. /* Get CID of all cards. FIXME: Support more than one card */
  338. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN);
  339. if (ret)
  340. return ret;
  341. mmc_parse_cid(cid, resp);
  342. if (verbose)
  343. mmc_dump_cid(cid);
  344. /* Set Relative Address of the card that responded */
  345. ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp,
  346. R1 | NCR | OPEN_DRAIN);
  347. return ret;
  348. }
  349. static void mci_set_data_timeout(struct mmc_csd *csd)
  350. {
  351. static const unsigned int dtomul_to_shift[] = {
  352. 0, 4, 7, 8, 10, 12, 16, 20,
  353. };
  354. static const unsigned int taac_exp[] = {
  355. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  356. };
  357. static const unsigned int taac_mant[] = {
  358. 0, 10, 12, 13, 15, 60, 25, 30,
  359. 35, 40, 45, 50, 55, 60, 70, 80,
  360. };
  361. unsigned int timeout_ns, timeout_clks;
  362. unsigned int e, m;
  363. unsigned int dtocyc, dtomul;
  364. unsigned int shift;
  365. u32 dtor;
  366. e = csd->taac & 0x07;
  367. m = (csd->taac >> 3) & 0x0f;
  368. timeout_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
  369. timeout_clks = csd->nsac * 100;
  370. timeout_clks += (((timeout_ns + 9) / 10)
  371. * ((CFG_MMC_CLK_PP + 99999) / 100000) + 9999) / 10000;
  372. if (!mmc_card_is_sd)
  373. timeout_clks *= 10;
  374. else
  375. timeout_clks *= 100;
  376. dtocyc = timeout_clks;
  377. dtomul = 0;
  378. shift = 0;
  379. while (dtocyc > 15 && dtomul < 8) {
  380. dtomul++;
  381. shift = dtomul_to_shift[dtomul];
  382. dtocyc = (timeout_clks + (1 << shift) - 1) >> shift;
  383. }
  384. if (dtomul >= 8) {
  385. dtomul = 7;
  386. dtocyc = 15;
  387. puts("Warning: Using maximum data timeout\n");
  388. }
  389. dtor = (MMCI_BF(DTOMUL, dtomul)
  390. | MMCI_BF(DTOCYC, dtocyc));
  391. mmci_writel(DTOR, dtor);
  392. printf("mmc: Using %u cycles data timeout (DTOR=0x%x)\n",
  393. dtocyc << shift, dtor);
  394. }
  395. int mmc_init(int verbose)
  396. {
  397. struct mmc_cid cid;
  398. struct mmc_csd csd;
  399. unsigned int max_blksz;
  400. int ret;
  401. /* Initialize controller */
  402. mmci_writel(CR, MMCI_BIT(SWRST));
  403. mmci_writel(CR, MMCI_BIT(MCIEN));
  404. mmci_writel(DTOR, 0x5f);
  405. mmci_writel(IDR, ~0UL);
  406. mci_set_mode(CFG_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  407. mmc_card_is_sd = 0;
  408. ret = sd_init_card(&cid, verbose);
  409. if (ret) {
  410. mmc_rca = MMC_DEFAULT_RCA;
  411. ret = mmc_init_card(&cid, verbose);
  412. }
  413. if (ret)
  414. return ret;
  415. /* Get CSD from the card */
  416. ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR);
  417. if (ret)
  418. return ret;
  419. if (verbose)
  420. mmc_dump_csd(&csd);
  421. mci_set_data_timeout(&csd);
  422. /* Initialize the blockdev structure */
  423. mmc_blkdev.if_type = IF_TYPE_MMC;
  424. mmc_blkdev.part_type = PART_TYPE_DOS;
  425. mmc_blkdev.block_read = mmc_bread;
  426. sprintf((char *)mmc_blkdev.vendor,
  427. "Man %02x%04x Snr %08x",
  428. cid.mid, cid.oid, cid.psn);
  429. strncpy((char *)mmc_blkdev.product, cid.pnm,
  430. sizeof(mmc_blkdev.product));
  431. sprintf((char *)mmc_blkdev.revision, "%x %x",
  432. cid.prv >> 4, cid.prv & 0x0f);
  433. /*
  434. * If we can't use 512 byte blocks, refuse to deal with the
  435. * card. Tons of code elsewhere seems to depend on this.
  436. */
  437. max_blksz = 1 << csd.read_bl_len;
  438. if (max_blksz < 512 || (max_blksz > 512 && !csd.read_bl_partial)) {
  439. printf("Card does not support 512 byte reads, aborting.\n");
  440. return -ENODEV;
  441. }
  442. mmc_blkdev.blksz = 512;
  443. mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2));
  444. mci_set_mode(CFG_MMC_CLK_PP, mmc_blkdev.blksz);
  445. #if 0
  446. if (fat_register_device(&mmc_blkdev, 1))
  447. printf("Could not register MMC fat device\n");
  448. #else
  449. init_part(&mmc_blkdev);
  450. #endif
  451. return 0;
  452. }
  453. int mmc_read(ulong src, uchar *dst, int size)
  454. {
  455. return -ENOSYS;
  456. }
  457. int mmc_write(uchar *src, ulong dst, int size)
  458. {
  459. return -ENOSYS;
  460. }
  461. int mmc2info(ulong addr)
  462. {
  463. return 0;
  464. }
  465. #endif /* CONFIG_MMC */