atmel_mci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. }
  71. #define RESP_NO_CRC 1
  72. #define R1 MMCI_BF(RSPTYP, 1)
  73. #define R2 MMCI_BF(RSPTYP, 2)
  74. #define R3 (R1 | RESP_NO_CRC)
  75. #define R6 R1
  76. #define NID MMCI_BF(MAXLAT, 0)
  77. #define NCR MMCI_BF(MAXLAT, 1)
  78. #define TRCMD_START MMCI_BF(TRCMD, 1)
  79. #define TRDIR_READ MMCI_BF(TRDIR, 1)
  80. #define TRTYP_BLOCK MMCI_BF(TRTYP, 0)
  81. #define INIT_CMD MMCI_BF(SPCMD, 1)
  82. #define OPEN_DRAIN MMCI_BF(OPDCMD, 1)
  83. #define ERROR_FLAGS (MMCI_BIT(DTOE) \
  84. | MMCI_BIT(RDIRE) \
  85. | MMCI_BIT(RENDE) \
  86. | MMCI_BIT(RINDE) \
  87. | MMCI_BIT(RTOE))
  88. static int
  89. mmc_cmd(unsigned long cmd, unsigned long arg,
  90. void *resp, unsigned long flags)
  91. {
  92. unsigned long *response = resp;
  93. int i, response_words = 0;
  94. unsigned long error_flags;
  95. u32 status;
  96. pr_debug("mmc: CMD%lu 0x%lx (flags 0x%lx)\n",
  97. cmd, arg, flags);
  98. error_flags = ERROR_FLAGS;
  99. if (!(flags & RESP_NO_CRC))
  100. error_flags |= MMCI_BIT(RCRCE);
  101. flags &= ~MMCI_BF(CMDNB, ~0UL);
  102. if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_48_BIT_RESP)
  103. response_words = 1;
  104. else if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_136_BIT_RESP)
  105. response_words = 4;
  106. mmci_writel(ARGR, arg);
  107. mmci_writel(CMDR, cmd | flags);
  108. do {
  109. udelay(40);
  110. status = mmci_readl(SR);
  111. } while (!(status & MMCI_BIT(CMDRDY)));
  112. pr_debug("mmc: status 0x%08lx\n", status);
  113. if (status & ERROR_FLAGS) {
  114. printf("mmc: command %lu failed (status: 0x%08lx)\n",
  115. cmd, status);
  116. return -EIO;
  117. }
  118. if (response_words)
  119. pr_debug("mmc: response:");
  120. for (i = 0; i < response_words; i++) {
  121. response[i] = mmci_readl(RSPR);
  122. pr_debug(" %08lx", response[i]);
  123. }
  124. pr_debug("\n");
  125. return 0;
  126. }
  127. static int mmc_acmd(unsigned long cmd, unsigned long arg,
  128. void *resp, unsigned long flags)
  129. {
  130. unsigned long aresp[4];
  131. int ret;
  132. /*
  133. * Seems like the APP_CMD part of an ACMD has 64 cycles max
  134. * latency even though the ACMD part doesn't. This isn't
  135. * entirely clear in the SD Card spec, but some cards refuse
  136. * to work if we attempt to use 5 cycles max latency here...
  137. */
  138. ret = mmc_cmd(MMC_CMD_APP_CMD, 0, aresp,
  139. R1 | NCR | (flags & OPEN_DRAIN));
  140. if (ret)
  141. return ret;
  142. if ((aresp[0] & (R1_ILLEGAL_COMMAND | R1_APP_CMD)) != R1_APP_CMD)
  143. return -ENODEV;
  144. ret = mmc_cmd(cmd, arg, resp, flags);
  145. return ret;
  146. }
  147. static unsigned long
  148. mmc_bread(int dev, unsigned long start, lbaint_t blkcnt,
  149. unsigned long *buffer)
  150. {
  151. int ret, i = 0;
  152. unsigned long resp[4];
  153. unsigned long card_status, data;
  154. unsigned long wordcount;
  155. u32 status;
  156. if (blkcnt == 0)
  157. return 0;
  158. pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n",
  159. dev, start, blkcnt);
  160. /* Put the device into Transfer state */
  161. ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR);
  162. if (ret) goto fail;
  163. /* Set block length */
  164. ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR);
  165. if (ret) goto fail;
  166. pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR));
  167. for (i = 0; i < blkcnt; i++, start++) {
  168. ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK,
  169. start * mmc_blkdev.blksz, resp,
  170. (R1 | NCR | TRCMD_START | TRDIR_READ
  171. | TRTYP_BLOCK));
  172. if (ret) goto fail;
  173. ret = -EIO;
  174. wordcount = 0;
  175. do {
  176. do {
  177. status = mmci_readl(SR);
  178. if (status & (ERROR_FLAGS | MMCI_BIT(OVRE)))
  179. goto fail;
  180. } while (!(status & MMCI_BIT(RXRDY)));
  181. if (status & MMCI_BIT(RXRDY)) {
  182. data = mmci_readl(RDR);
  183. /* pr_debug("%x\n", data); */
  184. *buffer++ = data;
  185. wordcount++;
  186. }
  187. } while(wordcount < (512 / 4));
  188. pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount);
  189. do {
  190. status = mmci_readl(SR);
  191. } while (!(status & MMCI_BIT(BLKE)));
  192. putc('.');
  193. }
  194. out:
  195. /* Put the device back into Standby state */
  196. mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR);
  197. return i;
  198. fail:
  199. mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR);
  200. printf("mmc: bread failed, card status = ", card_status);
  201. goto out;
  202. }
  203. static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  204. {
  205. cid->mid = resp[0] >> 24;
  206. cid->oid = (resp[0] >> 8) & 0xffff;
  207. cid->pnm[0] = resp[0];
  208. cid->pnm[1] = resp[1] >> 24;
  209. cid->pnm[2] = resp[1] >> 16;
  210. cid->pnm[3] = resp[1] >> 8;
  211. cid->pnm[4] = resp[1];
  212. cid->pnm[5] = resp[2] >> 24;
  213. cid->pnm[6] = 0;
  214. cid->prv = resp[2] >> 16;
  215. cid->psn = (resp[2] << 16) | (resp[3] >> 16);
  216. cid->mdt = resp[3] >> 8;
  217. }
  218. static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp)
  219. {
  220. cid->mid = resp[0] >> 24;
  221. cid->oid = (resp[0] >> 8) & 0xffff;
  222. cid->pnm[0] = resp[0];
  223. cid->pnm[1] = resp[1] >> 24;
  224. cid->pnm[2] = resp[1] >> 16;
  225. cid->pnm[3] = resp[1] >> 8;
  226. cid->pnm[4] = resp[1];
  227. cid->pnm[5] = 0;
  228. cid->pnm[6] = 0;
  229. cid->prv = resp[2] >> 24;
  230. cid->psn = (resp[2] << 8) | (resp[3] >> 24);
  231. cid->mdt = (resp[3] >> 8) & 0x0fff;
  232. }
  233. static void mmc_dump_cid(const struct mmc_cid *cid)
  234. {
  235. printf("Manufacturer ID: %02lX\n", cid->mid);
  236. printf("OEM/Application ID: %04lX\n", cid->oid);
  237. printf("Product name: %s\n", cid->pnm);
  238. printf("Product Revision: %lu.%lu\n",
  239. cid->prv >> 4, cid->prv & 0x0f);
  240. printf("Product Serial Number: %lu\n", cid->psn);
  241. printf("Manufacturing Date: %02lu/%02lu\n",
  242. cid->mdt >> 4, cid->mdt & 0x0f);
  243. }
  244. static void mmc_dump_csd(const struct mmc_csd *csd)
  245. {
  246. unsigned long *csd_raw = (unsigned long *)csd;
  247. printf("CSD data: %08lx %08lx %08lx %08lx\n",
  248. csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]);
  249. printf("CSD structure version: 1.%u\n", csd->csd_structure);
  250. printf("MMC System Spec version: %u\n", csd->spec_vers);
  251. printf("Card command classes: %03x\n", csd->ccc);
  252. printf("Read block length: %u\n", 1 << csd->read_bl_len);
  253. if (csd->read_bl_partial)
  254. puts("Supports partial reads\n");
  255. else
  256. puts("Does not support partial reads\n");
  257. printf("Write block length: %u\n", 1 << csd->write_bl_len);
  258. if (csd->write_bl_partial)
  259. puts("Supports partial writes\n");
  260. else
  261. puts("Does not support partial writes\n");
  262. if (csd->wp_grp_enable)
  263. printf("Supports group WP: %u\n", csd->wp_grp_size + 1);
  264. else
  265. puts("Does not support group WP\n");
  266. printf("Card capacity: %u bytes\n",
  267. (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) *
  268. (1 << csd->read_bl_len));
  269. printf("File format: %u/%u\n",
  270. csd->file_format_grp, csd->file_format);
  271. puts("Write protection: ");
  272. if (csd->perm_write_protect)
  273. puts(" permanent");
  274. if (csd->tmp_write_protect)
  275. puts(" temporary");
  276. putc('\n');
  277. }
  278. static int mmc_idle_cards(void)
  279. {
  280. int ret;
  281. /* Reset and initialize all cards */
  282. ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0);
  283. if (ret)
  284. return ret;
  285. /* Keep the bus idle for 74 clock cycles */
  286. return mmc_cmd(0, 0, NULL, INIT_CMD);
  287. }
  288. static int sd_init_card(struct mmc_cid *cid, int verbose)
  289. {
  290. unsigned long resp[4];
  291. int i, ret = 0;
  292. mmc_idle_cards();
  293. for (i = 0; i < 1000; i++) {
  294. ret = mmc_acmd(MMC_ACMD_SD_SEND_OP_COND, CFG_MMC_OP_COND,
  295. resp, R3 | NID);
  296. if (ret || (resp[0] & 0x80000000))
  297. break;
  298. ret = -ETIMEDOUT;
  299. }
  300. if (ret)
  301. return ret;
  302. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID);
  303. if (ret)
  304. return ret;
  305. sd_parse_cid(cid, resp);
  306. if (verbose)
  307. mmc_dump_cid(cid);
  308. /* Get RCA of the card that responded */
  309. ret = mmc_cmd(MMC_CMD_SD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR);
  310. if (ret)
  311. return ret;
  312. mmc_rca = resp[0] >> 16;
  313. if (verbose)
  314. printf("SD Card detected (RCA %u)\n", mmc_rca);
  315. return 0;
  316. }
  317. static int mmc_init_card(struct mmc_cid *cid, int verbose)
  318. {
  319. unsigned long resp[4];
  320. int i, ret = 0;
  321. mmc_idle_cards();
  322. for (i = 0; i < 1000; i++) {
  323. ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CFG_MMC_OP_COND, resp,
  324. R3 | NID | OPEN_DRAIN);
  325. if (ret || (resp[0] & 0x80000000))
  326. break;
  327. ret = -ETIMEDOUT;
  328. }
  329. if (ret)
  330. return ret;
  331. /* Get CID of all cards. FIXME: Support more than one card */
  332. ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN);
  333. if (ret)
  334. return ret;
  335. mmc_parse_cid(cid, resp);
  336. if (verbose)
  337. mmc_dump_cid(cid);
  338. /* Set Relative Address of the card that responded */
  339. ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp,
  340. R1 | NCR | OPEN_DRAIN);
  341. return ret;
  342. }
  343. int mmc_init(int verbose)
  344. {
  345. struct mmc_cid cid;
  346. struct mmc_csd csd;
  347. int ret;
  348. /* Initialize controller */
  349. mmci_writel(CR, MMCI_BIT(SWRST));
  350. mmci_writel(CR, MMCI_BIT(MCIEN));
  351. mmci_writel(DTOR, 0x5f);
  352. mmci_writel(IDR, ~0UL);
  353. mci_set_mode(CFG_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  354. ret = sd_init_card(&cid, verbose);
  355. if (ret) {
  356. mmc_rca = MMC_DEFAULT_RCA;
  357. ret = mmc_init_card(&cid, verbose);
  358. }
  359. if (ret)
  360. return ret;
  361. /* Get CSD from the card */
  362. ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR);
  363. if (ret)
  364. return ret;
  365. if (verbose)
  366. mmc_dump_csd(&csd);
  367. /* Initialize the blockdev structure */
  368. mmc_blkdev.if_type = IF_TYPE_MMC;
  369. mmc_blkdev.part_type = PART_TYPE_DOS;
  370. mmc_blkdev.block_read = mmc_bread;
  371. sprintf((char *)mmc_blkdev.vendor,
  372. "Man %02x%04x Snr %08x",
  373. cid.mid, cid.oid, cid.psn);
  374. strncpy((char *)mmc_blkdev.product, cid.pnm,
  375. sizeof(mmc_blkdev.product));
  376. sprintf((char *)mmc_blkdev.revision, "%x %x",
  377. cid.prv >> 4, cid.prv & 0x0f);
  378. mmc_blkdev.blksz = 1 << csd.read_bl_len;
  379. mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2));
  380. mci_set_mode(CFG_MMC_CLK_PP, mmc_blkdev.blksz);
  381. #if 0
  382. if (fat_register_device(&mmc_blkdev, 1))
  383. printf("Could not register MMC fat device\n");
  384. #else
  385. init_part(&mmc_blkdev);
  386. #endif
  387. return 0;
  388. }
  389. int mmc_read(ulong src, uchar *dst, int size)
  390. {
  391. return -ENOSYS;
  392. }
  393. int mmc_write(uchar *src, ulong dst, int size)
  394. {
  395. return -ENOSYS;
  396. }
  397. int mmc2info(ulong addr)
  398. {
  399. return 0;
  400. }
  401. #endif /* CONFIG_MMC */