atmel_mci.c 13 KB

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