atmel_mci.c 12 KB

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