fsl_esdhc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright 2007,2010 Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based vaguely on the pxa mmc code:
  6. * (C) Copyright 2003
  7. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <config.h>
  28. #include <common.h>
  29. #include <command.h>
  30. #include <hwconfig.h>
  31. #include <mmc.h>
  32. #include <part.h>
  33. #include <malloc.h>
  34. #include <mmc.h>
  35. #include <fsl_esdhc.h>
  36. #include <fdt_support.h>
  37. #include <asm/io.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. struct fsl_esdhc {
  40. uint dsaddr;
  41. uint blkattr;
  42. uint cmdarg;
  43. uint xfertyp;
  44. uint cmdrsp0;
  45. uint cmdrsp1;
  46. uint cmdrsp2;
  47. uint cmdrsp3;
  48. uint datport;
  49. uint prsstat;
  50. uint proctl;
  51. uint sysctl;
  52. uint irqstat;
  53. uint irqstaten;
  54. uint irqsigen;
  55. uint autoc12err;
  56. uint hostcapblt;
  57. uint wml;
  58. char reserved1[8];
  59. uint fevt;
  60. char reserved2[168];
  61. uint hostver;
  62. char reserved3[780];
  63. uint scr;
  64. };
  65. /* Return the XFERTYP flags for a given command and data packet */
  66. uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
  67. {
  68. uint xfertyp = 0;
  69. if (data) {
  70. xfertyp |= XFERTYP_DPSEL;
  71. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  72. xfertyp |= XFERTYP_DMAEN;
  73. #endif
  74. if (data->blocks > 1) {
  75. xfertyp |= XFERTYP_MSBSEL;
  76. xfertyp |= XFERTYP_BCEN;
  77. }
  78. if (data->flags & MMC_DATA_READ)
  79. xfertyp |= XFERTYP_DTDSEL;
  80. }
  81. if (cmd->resp_type & MMC_RSP_CRC)
  82. xfertyp |= XFERTYP_CCCEN;
  83. if (cmd->resp_type & MMC_RSP_OPCODE)
  84. xfertyp |= XFERTYP_CICEN;
  85. if (cmd->resp_type & MMC_RSP_136)
  86. xfertyp |= XFERTYP_RSPTYP_136;
  87. else if (cmd->resp_type & MMC_RSP_BUSY)
  88. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  89. else if (cmd->resp_type & MMC_RSP_PRESENT)
  90. xfertyp |= XFERTYP_RSPTYP_48;
  91. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  92. }
  93. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  94. /*
  95. * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
  96. */
  97. static void
  98. esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
  99. {
  100. struct fsl_esdhc *regs = mmc->priv;
  101. uint blocks;
  102. char *buffer;
  103. uint databuf;
  104. uint size;
  105. uint irqstat;
  106. uint timeout;
  107. if (data->flags & MMC_DATA_READ) {
  108. blocks = data->blocks;
  109. buffer = data->dest;
  110. while (blocks) {
  111. timeout = PIO_TIMEOUT;
  112. size = data->blocksize;
  113. irqstat = esdhc_read32(&regs->irqstat);
  114. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
  115. && --timeout);
  116. if (timeout <= 0) {
  117. printf("\nData Read Failed in PIO Mode.");
  118. return;
  119. }
  120. while (size && (!(irqstat & IRQSTAT_TC))) {
  121. udelay(100); /* Wait before last byte transfer complete */
  122. irqstat = esdhc_read32(&regs->irqstat);
  123. databuf = in_le32(&regs->datport);
  124. *((uint *)buffer) = databuf;
  125. buffer += 4;
  126. size -= 4;
  127. }
  128. blocks--;
  129. }
  130. } else {
  131. blocks = data->blocks;
  132. buffer = (char *)data->src;
  133. while (blocks) {
  134. timeout = PIO_TIMEOUT;
  135. size = data->blocksize;
  136. irqstat = esdhc_read32(&regs->irqstat);
  137. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
  138. && --timeout);
  139. if (timeout <= 0) {
  140. printf("\nData Write Failed in PIO Mode.");
  141. return;
  142. }
  143. while (size && (!(irqstat & IRQSTAT_TC))) {
  144. udelay(100); /* Wait before last byte transfer complete */
  145. databuf = *((uint *)buffer);
  146. buffer += 4;
  147. size -= 4;
  148. irqstat = esdhc_read32(&regs->irqstat);
  149. out_le32(&regs->datport, databuf);
  150. }
  151. blocks--;
  152. }
  153. }
  154. }
  155. #endif
  156. static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
  157. {
  158. int timeout;
  159. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  160. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  161. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  162. uint wml_value;
  163. wml_value = data->blocksize/4;
  164. if (data->flags & MMC_DATA_READ) {
  165. if (wml_value > 0x10)
  166. wml_value = 0x10;
  167. esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
  168. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  169. } else {
  170. if (wml_value > 0x80)
  171. wml_value = 0x80;
  172. if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
  173. printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
  174. return TIMEOUT;
  175. }
  176. esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
  177. wml_value << 16);
  178. esdhc_write32(&regs->dsaddr, (u32)data->src);
  179. }
  180. #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
  181. if (!(data->flags & MMC_DATA_READ)) {
  182. if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
  183. printf("\nThe SD card is locked. "
  184. "Can not write to a locked card.\n\n");
  185. return TIMEOUT;
  186. }
  187. esdhc_write32(&regs->dsaddr, (u32)data->src);
  188. } else
  189. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  190. #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
  191. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  192. /* Calculate the timeout period for data transactions */
  193. timeout = fls(mmc->tran_speed/10) - 1;
  194. timeout -= 13;
  195. if (timeout > 14)
  196. timeout = 14;
  197. if (timeout < 0)
  198. timeout = 0;
  199. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  200. return 0;
  201. }
  202. /*
  203. * Sends a command out on the bus. Takes the mmc pointer,
  204. * a command pointer, and an optional data pointer.
  205. */
  206. static int
  207. esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  208. {
  209. uint xfertyp;
  210. uint irqstat;
  211. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  212. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  213. esdhc_write32(&regs->irqstat, -1);
  214. sync();
  215. /* Wait for the bus to be idle */
  216. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  217. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  218. ;
  219. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  220. ;
  221. /* Wait at least 8 SD clock cycles before the next command */
  222. /*
  223. * Note: This is way more than 8 cycles, but 1ms seems to
  224. * resolve timing issues with some cards
  225. */
  226. udelay(1000);
  227. /* Set up for a data transfer if we have one */
  228. if (data) {
  229. int err;
  230. err = esdhc_setup_data(mmc, data);
  231. if(err)
  232. return err;
  233. }
  234. /* Figure out the transfer arguments */
  235. xfertyp = esdhc_xfertyp(cmd, data);
  236. /* Send the command */
  237. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  238. esdhc_write32(&regs->xfertyp, xfertyp);
  239. /* Wait for the command to complete */
  240. while (!(esdhc_read32(&regs->irqstat) & IRQSTAT_CC))
  241. ;
  242. irqstat = esdhc_read32(&regs->irqstat);
  243. esdhc_write32(&regs->irqstat, irqstat);
  244. if (irqstat & CMD_ERR)
  245. return COMM_ERR;
  246. if (irqstat & IRQSTAT_CTOE)
  247. return TIMEOUT;
  248. /* Copy the response to the response buffer */
  249. if (cmd->resp_type & MMC_RSP_136) {
  250. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  251. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  252. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  253. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  254. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  255. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  256. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  257. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  258. cmd->response[3] = (cmdrsp0 << 8);
  259. } else
  260. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  261. /* Wait until all of the blocks are transferred */
  262. if (data) {
  263. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  264. esdhc_pio_read_write(mmc, data);
  265. #else
  266. do {
  267. irqstat = esdhc_read32(&regs->irqstat);
  268. if (irqstat & DATA_ERR)
  269. return COMM_ERR;
  270. if (irqstat & IRQSTAT_DTOE)
  271. return TIMEOUT;
  272. } while (!(irqstat & IRQSTAT_TC) &&
  273. (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
  274. #endif
  275. }
  276. esdhc_write32(&regs->irqstat, -1);
  277. return 0;
  278. }
  279. void set_sysctl(struct mmc *mmc, uint clock)
  280. {
  281. int sdhc_clk = gd->sdhc_clk;
  282. int div, pre_div;
  283. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  284. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  285. uint clk;
  286. if (clock < mmc->f_min)
  287. clock = mmc->f_min;
  288. if (sdhc_clk / 16 > clock) {
  289. for (pre_div = 2; pre_div < 256; pre_div *= 2)
  290. if ((sdhc_clk / pre_div) <= (clock * 16))
  291. break;
  292. } else
  293. pre_div = 2;
  294. for (div = 1; div <= 16; div++)
  295. if ((sdhc_clk / (div * pre_div)) <= clock)
  296. break;
  297. pre_div >>= 1;
  298. div -= 1;
  299. clk = (pre_div << 8) | (div << 4);
  300. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  301. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  302. udelay(10000);
  303. clk = SYSCTL_PEREN | SYSCTL_CKEN;
  304. esdhc_setbits32(&regs->sysctl, clk);
  305. }
  306. static void esdhc_set_ios(struct mmc *mmc)
  307. {
  308. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  309. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  310. /* Set the clock speed */
  311. set_sysctl(mmc, mmc->clock);
  312. /* Set the bus width */
  313. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  314. if (mmc->bus_width == 4)
  315. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  316. else if (mmc->bus_width == 8)
  317. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  318. }
  319. static int esdhc_init(struct mmc *mmc)
  320. {
  321. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  322. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  323. int timeout = 1000;
  324. int ret = 0;
  325. u8 card_absent;
  326. /* Enable cache snooping */
  327. if (cfg && !cfg->no_snoop)
  328. esdhc_write32(&regs->scr, 0x00000040);
  329. /* Reset the entire host controller */
  330. esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
  331. /* Wait until the controller is available */
  332. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  333. udelay(1000);
  334. esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  335. /* Set the initial clock speed */
  336. set_sysctl(mmc, 400000);
  337. /* Disable the BRR and BWR bits in IRQSTAT */
  338. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  339. /* Put the PROCTL reg back to the default */
  340. esdhc_write32(&regs->proctl, PROCTL_INIT);
  341. /* Set timout to the maximum value */
  342. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  343. /* Check if there is a callback for detecting the card */
  344. if (board_mmc_getcd(&card_absent, mmc)) {
  345. timeout = 1000;
  346. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) &&
  347. --timeout)
  348. udelay(1000);
  349. if (timeout <= 0)
  350. ret = NO_CARD_ERR;
  351. } else {
  352. if (card_absent)
  353. ret = NO_CARD_ERR;
  354. }
  355. return ret;
  356. }
  357. static void esdhc_reset(struct fsl_esdhc *regs)
  358. {
  359. unsigned long timeout = 100; /* wait max 100 ms */
  360. /* reset the controller */
  361. esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
  362. /* hardware clears the bit when it is done */
  363. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  364. udelay(1000);
  365. if (!timeout)
  366. printf("MMC/SD: Reset never completed.\n");
  367. }
  368. int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
  369. {
  370. struct fsl_esdhc *regs;
  371. struct mmc *mmc;
  372. u32 caps;
  373. if (!cfg)
  374. return -1;
  375. mmc = malloc(sizeof(struct mmc));
  376. sprintf(mmc->name, "FSL_ESDHC");
  377. regs = (struct fsl_esdhc *)cfg->esdhc_base;
  378. /* First reset the eSDHC controller */
  379. esdhc_reset(regs);
  380. mmc->priv = cfg;
  381. mmc->send_cmd = esdhc_send_cmd;
  382. mmc->set_ios = esdhc_set_ios;
  383. mmc->init = esdhc_init;
  384. caps = regs->hostcapblt;
  385. if (caps & ESDHC_HOSTCAPBLT_VS18)
  386. mmc->voltages |= MMC_VDD_165_195;
  387. if (caps & ESDHC_HOSTCAPBLT_VS30)
  388. mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  389. if (caps & ESDHC_HOSTCAPBLT_VS33)
  390. mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  391. mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  392. if (caps & ESDHC_HOSTCAPBLT_HSS)
  393. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  394. mmc->f_min = 400000;
  395. mmc->f_max = MIN(gd->sdhc_clk, 50000000);
  396. mmc_register(mmc);
  397. return 0;
  398. }
  399. int fsl_esdhc_mmc_init(bd_t *bis)
  400. {
  401. struct fsl_esdhc_cfg *cfg;
  402. cfg = malloc(sizeof(struct fsl_esdhc_cfg));
  403. memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
  404. cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
  405. return fsl_esdhc_initialize(bis, cfg);
  406. }
  407. #ifdef CONFIG_OF_LIBFDT
  408. void fdt_fixup_esdhc(void *blob, bd_t *bd)
  409. {
  410. const char *compat = "fsl,esdhc";
  411. const char *status = "okay";
  412. if (!hwconfig("esdhc")) {
  413. status = "disabled";
  414. goto out;
  415. }
  416. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  417. gd->sdhc_clk, 1);
  418. out:
  419. do_fixup_by_compat(blob, compat, "status", status,
  420. strlen(status) + 1, 1);
  421. }
  422. #endif