atmel_mci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. unsigned long *buffer)
  153. {
  154. int ret, i = 0;
  155. unsigned long resp[4];
  156. unsigned long card_status, data;
  157. unsigned long wordcount;
  158. u32 status;
  159. if (blkcnt == 0)
  160. return 0;
  161. pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n",
  162. dev, start, blkcnt);
  163. /* Put the device into Transfer state */
  164. ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR);
  165. if (ret) goto fail;
  166. /* Set block length */
  167. ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR);
  168. if (ret) goto fail;
  169. pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR));
  170. for (i = 0; i < blkcnt; i++, start++) {
  171. ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK,
  172. start * mmc_blkdev.blksz, resp,
  173. (R1 | NCR | TRCMD_START | TRDIR_READ
  174. | TRTYP_BLOCK));
  175. if (ret) goto fail;
  176. ret = -EIO;
  177. wordcount = 0;
  178. do {
  179. do {
  180. status = mmci_readl(SR);
  181. if (status & (ERROR_FLAGS | MMCI_BIT(OVRE)))
  182. goto fail;
  183. } while (!(status & MMCI_BIT(RXRDY)));
  184. if (status & MMCI_BIT(RXRDY)) {
  185. data = mmci_readl(RDR);
  186. /* pr_debug("%x\n", data); */
  187. *buffer++ = data;
  188. wordcount++;
  189. }
  190. } while(wordcount < (mmc_blkdev.blksz / 4));
  191. pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount);
  192. do {
  193. status = mmci_readl(SR);
  194. } while (!(status & MMCI_BIT(BLKE)));
  195. putc('.');
  196. }
  197. out:
  198. /* Put the device back into Standby state */
  199. mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR);
  200. return i;
  201. fail:
  202. mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR);
  203. printf("mmc: bread failed, card status = %08x\n", card_status);
  204. goto out;
  205. }
  206. static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  207. {
  208. cid->mid = resp[0] >> 24;
  209. cid->oid = (resp[0] >> 8) & 0xffff;
  210. cid->pnm[0] = resp[0];
  211. cid->pnm[1] = resp[1] >> 24;
  212. cid->pnm[2] = resp[1] >> 16;
  213. cid->pnm[3] = resp[1] >> 8;
  214. cid->pnm[4] = resp[1];
  215. cid->pnm[5] = resp[2] >> 24;
  216. cid->pnm[6] = 0;
  217. cid->prv = resp[2] >> 16;
  218. cid->psn = (resp[2] << 16) | (resp[3] >> 16);
  219. cid->mdt = resp[3] >> 8;
  220. }
  221. static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  222. {
  223. cid->mid = resp[0] >> 24;
  224. cid->oid = (resp[0] >> 8) & 0xffff;
  225. cid->pnm[0] = resp[0];
  226. cid->pnm[1] = resp[1] >> 24;
  227. cid->pnm[2] = resp[1] >> 16;
  228. cid->pnm[3] = resp[1] >> 8;
  229. cid->pnm[4] = resp[1];
  230. cid->pnm[5] = 0;
  231. cid->pnm[6] = 0;
  232. cid->prv = resp[2] >> 24;
  233. cid->psn = (resp[2] << 8) | (resp[3] >> 24);
  234. cid->mdt = (resp[3] >> 8) & 0x0fff;
  235. }
  236. static void mmc_dump_cid(const struct mmc_cid *cid)
  237. {
  238. printf("Manufacturer ID: %02lX\n", cid->mid);
  239. printf("OEM/Application ID: %04lX\n", cid->oid);
  240. printf("Product name: %s\n", cid->pnm);
  241. printf("Product Revision: %lu.%lu\n",
  242. cid->prv >> 4, cid->prv & 0x0f);
  243. printf("Product Serial Number: %lu\n", cid->psn);
  244. printf("Manufacturing Date: %02lu/%02lu\n",
  245. cid->mdt >> 4, cid->mdt & 0x0f);
  246. }
  247. static void mmc_dump_csd(const struct mmc_csd *csd)
  248. {
  249. unsigned long *csd_raw = (unsigned long *)csd;
  250. printf("CSD data: %08lx %08lx %08lx %08lx\n",
  251. csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]);
  252. printf("CSD structure version: 1.%u\n", csd->csd_structure);
  253. printf("MMC System Spec version: %u\n", csd->spec_vers);
  254. printf("Card command classes: %03x\n", csd->ccc);
  255. printf("Read block length: %u\n", 1 << csd->read_bl_len);
  256. if (csd->read_bl_partial)
  257. puts("Supports partial reads\n");
  258. else
  259. puts("Does not support partial reads\n");
  260. printf("Write block length: %u\n", 1 << csd->write_bl_len);
  261. if (csd->write_bl_partial)
  262. puts("Supports partial writes\n");
  263. else
  264. puts("Does not support partial writes\n");
  265. if (csd->wp_grp_enable)
  266. printf("Supports group WP: %u\n", csd->wp_grp_size + 1);
  267. else
  268. puts("Does not support group WP\n");
  269. printf("Card capacity: %u bytes\n",
  270. (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) *
  271. (1 << csd->read_bl_len));
  272. printf("File format: %u/%u\n",
  273. csd->file_format_grp, csd->file_format);
  274. puts("Write protection: ");
  275. if (csd->perm_write_protect)
  276. puts(" permanent");
  277. if (csd->tmp_write_protect)
  278. puts(" temporary");
  279. putc('\n');
  280. }
  281. static int mmc_idle_cards(void)
  282. {
  283. int ret;
  284. /* Reset and initialize all cards */
  285. ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0);
  286. if (ret)
  287. return ret;
  288. /* Keep the bus idle for 74 clock cycles */
  289. return mmc_cmd(0, 0, NULL, INIT_CMD);
  290. }
  291. static int sd_init_card(struct mmc_cid *cid, int verbose)
  292. {
  293. unsigned long resp[4];
  294. int i, ret = 0;
  295. mmc_idle_cards();
  296. for (i = 0; i < 1000; i++) {
  297. ret = mmc_acmd(MMC_ACMD_SD_SEND_OP_COND, CFG_MMC_OP_COND,
  298. resp, R3 | NID);
  299. if (ret || (resp[0] & 0x80000000))
  300. break;
  301. ret = -ETIMEDOUT;
  302. }
  303. if (ret)
  304. return ret;
  305. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID);
  306. if (ret)
  307. return ret;
  308. sd_parse_cid(cid, resp);
  309. if (verbose)
  310. mmc_dump_cid(cid);
  311. /* Get RCA of the card that responded */
  312. ret = mmc_cmd(MMC_CMD_SD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR);
  313. if (ret)
  314. return ret;
  315. mmc_rca = resp[0] >> 16;
  316. if (verbose)
  317. printf("SD Card detected (RCA %u)\n", mmc_rca);
  318. mmc_card_is_sd = 1;
  319. return 0;
  320. }
  321. static int mmc_init_card(struct mmc_cid *cid, int verbose)
  322. {
  323. unsigned long resp[4];
  324. int i, ret = 0;
  325. mmc_idle_cards();
  326. for (i = 0; i < 1000; i++) {
  327. ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CFG_MMC_OP_COND, resp,
  328. R3 | NID | OPEN_DRAIN);
  329. if (ret || (resp[0] & 0x80000000))
  330. break;
  331. ret = -ETIMEDOUT;
  332. }
  333. if (ret)
  334. return ret;
  335. /* Get CID of all cards. FIXME: Support more than one card */
  336. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN);
  337. if (ret)
  338. return ret;
  339. mmc_parse_cid(cid, resp);
  340. if (verbose)
  341. mmc_dump_cid(cid);
  342. /* Set Relative Address of the card that responded */
  343. ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp,
  344. R1 | NCR | OPEN_DRAIN);
  345. return ret;
  346. }
  347. static void mci_set_data_timeout(struct mmc_csd *csd)
  348. {
  349. static const unsigned int dtomul_to_shift[] = {
  350. 0, 4, 7, 8, 10, 12, 16, 20,
  351. };
  352. static const unsigned int taac_exp[] = {
  353. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  354. };
  355. static const unsigned int taac_mant[] = {
  356. 0, 10, 12, 13, 15, 60, 25, 30,
  357. 35, 40, 45, 50, 55, 60, 70, 80,
  358. };
  359. unsigned int timeout_ns, timeout_clks;
  360. unsigned int e, m;
  361. unsigned int dtocyc, dtomul;
  362. unsigned int shift;
  363. u32 dtor;
  364. e = csd->taac & 0x07;
  365. m = (csd->taac >> 3) & 0x0f;
  366. timeout_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
  367. timeout_clks = csd->nsac * 100;
  368. timeout_clks += (((timeout_ns + 9) / 10)
  369. * ((CFG_MMC_CLK_PP + 99999) / 100000) + 9999) / 10000;
  370. if (!mmc_card_is_sd)
  371. timeout_clks *= 10;
  372. else
  373. timeout_clks *= 100;
  374. dtocyc = timeout_clks;
  375. dtomul = 0;
  376. while (dtocyc > 15 && dtomul < 8) {
  377. dtomul++;
  378. shift = dtomul_to_shift[dtomul];
  379. dtocyc = (timeout_clks + (1 << shift) - 1) >> shift;
  380. }
  381. if (dtomul >= 8) {
  382. dtomul = 7;
  383. dtocyc = 15;
  384. puts("Warning: Using maximum data timeout\n");
  385. }
  386. dtor = (MMCI_BF(DTOMUL, dtomul)
  387. | MMCI_BF(DTOCYC, dtocyc));
  388. mmci_writel(DTOR, dtor);
  389. printf("mmc: Using %u cycles data timeout (DTOR=0x%x)\n",
  390. dtocyc << shift, dtor);
  391. }
  392. int mmc_init(int verbose)
  393. {
  394. struct mmc_cid cid;
  395. struct mmc_csd csd;
  396. unsigned int max_blksz;
  397. int ret;
  398. /* Initialize controller */
  399. mmci_writel(CR, MMCI_BIT(SWRST));
  400. mmci_writel(CR, MMCI_BIT(MCIEN));
  401. mmci_writel(DTOR, 0x5f);
  402. mmci_writel(IDR, ~0UL);
  403. mci_set_mode(CFG_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  404. mmc_card_is_sd = 0;
  405. ret = sd_init_card(&cid, verbose);
  406. if (ret) {
  407. mmc_rca = MMC_DEFAULT_RCA;
  408. ret = mmc_init_card(&cid, verbose);
  409. }
  410. if (ret)
  411. return ret;
  412. /* Get CSD from the card */
  413. ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR);
  414. if (ret)
  415. return ret;
  416. if (verbose)
  417. mmc_dump_csd(&csd);
  418. mci_set_data_timeout(&csd);
  419. /* Initialize the blockdev structure */
  420. mmc_blkdev.if_type = IF_TYPE_MMC;
  421. mmc_blkdev.part_type = PART_TYPE_DOS;
  422. mmc_blkdev.block_read = mmc_bread;
  423. sprintf((char *)mmc_blkdev.vendor,
  424. "Man %02x%04x Snr %08x",
  425. cid.mid, cid.oid, cid.psn);
  426. strncpy((char *)mmc_blkdev.product, cid.pnm,
  427. sizeof(mmc_blkdev.product));
  428. sprintf((char *)mmc_blkdev.revision, "%x %x",
  429. cid.prv >> 4, cid.prv & 0x0f);
  430. /*
  431. * If we can't use 512 byte blocks, refuse to deal with the
  432. * card. Tons of code elsewhere seems to depend on this.
  433. */
  434. max_blksz = 1 << csd.read_bl_len;
  435. if (max_blksz < 512 || (max_blksz > 512 && !csd.read_bl_partial)) {
  436. printf("Card does not support 512 byte reads, aborting.\n");
  437. return -ENODEV;
  438. }
  439. mmc_blkdev.blksz = 512;
  440. mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2));
  441. mci_set_mode(CFG_MMC_CLK_PP, mmc_blkdev.blksz);
  442. #if 0
  443. if (fat_register_device(&mmc_blkdev, 1))
  444. printf("Could not register MMC fat device\n");
  445. #else
  446. init_part(&mmc_blkdev);
  447. #endif
  448. return 0;
  449. }
  450. int mmc_read(ulong src, uchar *dst, int size)
  451. {
  452. return -ENOSYS;
  453. }
  454. int mmc_write(uchar *src, ulong dst, int size)
  455. {
  456. return -ENOSYS;
  457. }
  458. int mmc2info(ulong addr)
  459. {
  460. return 0;
  461. }
  462. #endif /* CONFIG_MMC */