fsl_esdhc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright 2007, 2010-2011 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. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  78. xfertyp |= XFERTYP_AC12EN;
  79. #endif
  80. }
  81. if (data->flags & MMC_DATA_READ)
  82. xfertyp |= XFERTYP_DTDSEL;
  83. }
  84. if (cmd->resp_type & MMC_RSP_CRC)
  85. xfertyp |= XFERTYP_CCCEN;
  86. if (cmd->resp_type & MMC_RSP_OPCODE)
  87. xfertyp |= XFERTYP_CICEN;
  88. if (cmd->resp_type & MMC_RSP_136)
  89. xfertyp |= XFERTYP_RSPTYP_136;
  90. else if (cmd->resp_type & MMC_RSP_BUSY)
  91. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  92. else if (cmd->resp_type & MMC_RSP_PRESENT)
  93. xfertyp |= XFERTYP_RSPTYP_48;
  94. #ifdef CONFIG_MX53
  95. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  96. xfertyp |= XFERTYP_CMDTYP_ABORT;
  97. #endif
  98. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  99. }
  100. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  101. /*
  102. * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
  103. */
  104. static void
  105. esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
  106. {
  107. struct fsl_esdhc *regs = mmc->priv;
  108. uint blocks;
  109. char *buffer;
  110. uint databuf;
  111. uint size;
  112. uint irqstat;
  113. uint timeout;
  114. if (data->flags & MMC_DATA_READ) {
  115. blocks = data->blocks;
  116. buffer = data->dest;
  117. while (blocks) {
  118. timeout = PIO_TIMEOUT;
  119. size = data->blocksize;
  120. irqstat = esdhc_read32(&regs->irqstat);
  121. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
  122. && --timeout);
  123. if (timeout <= 0) {
  124. printf("\nData Read Failed in PIO Mode.");
  125. return;
  126. }
  127. while (size && (!(irqstat & IRQSTAT_TC))) {
  128. udelay(100); /* Wait before last byte transfer complete */
  129. irqstat = esdhc_read32(&regs->irqstat);
  130. databuf = in_le32(&regs->datport);
  131. *((uint *)buffer) = databuf;
  132. buffer += 4;
  133. size -= 4;
  134. }
  135. blocks--;
  136. }
  137. } else {
  138. blocks = data->blocks;
  139. buffer = (char *)data->src;
  140. while (blocks) {
  141. timeout = PIO_TIMEOUT;
  142. size = data->blocksize;
  143. irqstat = esdhc_read32(&regs->irqstat);
  144. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
  145. && --timeout);
  146. if (timeout <= 0) {
  147. printf("\nData Write Failed in PIO Mode.");
  148. return;
  149. }
  150. while (size && (!(irqstat & IRQSTAT_TC))) {
  151. udelay(100); /* Wait before last byte transfer complete */
  152. databuf = *((uint *)buffer);
  153. buffer += 4;
  154. size -= 4;
  155. irqstat = esdhc_read32(&regs->irqstat);
  156. out_le32(&regs->datport, databuf);
  157. }
  158. blocks--;
  159. }
  160. }
  161. }
  162. #endif
  163. static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
  164. {
  165. int timeout;
  166. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  167. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  168. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  169. uint wml_value;
  170. wml_value = data->blocksize/4;
  171. if (data->flags & MMC_DATA_READ) {
  172. if (wml_value > WML_RD_WML_MAX)
  173. wml_value = WML_RD_WML_MAX_VAL;
  174. esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
  175. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  176. } else {
  177. if (wml_value > WML_WR_WML_MAX)
  178. wml_value = WML_WR_WML_MAX_VAL;
  179. if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
  180. printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
  181. return TIMEOUT;
  182. }
  183. esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
  184. wml_value << 16);
  185. esdhc_write32(&regs->dsaddr, (u32)data->src);
  186. }
  187. #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
  188. if (!(data->flags & MMC_DATA_READ)) {
  189. if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
  190. printf("\nThe SD card is locked. "
  191. "Can not write to a locked card.\n\n");
  192. return TIMEOUT;
  193. }
  194. esdhc_write32(&regs->dsaddr, (u32)data->src);
  195. } else
  196. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  197. #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
  198. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  199. /* Calculate the timeout period for data transactions */
  200. /*
  201. * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
  202. * 2)Timeout period should be minimum 0.250sec as per SD Card spec
  203. * So, Number of SD Clock cycles for 0.25sec should be minimum
  204. * (SD Clock/sec * 0.25 sec) SD Clock cycles
  205. * = (mmc->tran_speed * 1/4) SD Clock cycles
  206. * As 1) >= 2)
  207. * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
  208. * Taking log2 both the sides
  209. * => timeout + 13 >= log2(mmc->tran_speed/4)
  210. * Rounding up to next power of 2
  211. * => timeout + 13 = log2(mmc->tran_speed/4) + 1
  212. * => timeout + 13 = fls(mmc->tran_speed/4)
  213. */
  214. timeout = fls(mmc->tran_speed/4);
  215. timeout -= 13;
  216. if (timeout > 14)
  217. timeout = 14;
  218. if (timeout < 0)
  219. timeout = 0;
  220. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
  221. if ((timeout == 4) || (timeout == 8) || (timeout == 12))
  222. timeout++;
  223. #endif
  224. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  225. return 0;
  226. }
  227. /*
  228. * Sends a command out on the bus. Takes the mmc pointer,
  229. * a command pointer, and an optional data pointer.
  230. */
  231. static int
  232. esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  233. {
  234. uint xfertyp;
  235. uint irqstat;
  236. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  237. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  238. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  239. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  240. return 0;
  241. #endif
  242. esdhc_write32(&regs->irqstat, -1);
  243. sync();
  244. /* Wait for the bus to be idle */
  245. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  246. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  247. ;
  248. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  249. ;
  250. /* Wait at least 8 SD clock cycles before the next command */
  251. /*
  252. * Note: This is way more than 8 cycles, but 1ms seems to
  253. * resolve timing issues with some cards
  254. */
  255. udelay(1000);
  256. /* Set up for a data transfer if we have one */
  257. if (data) {
  258. int err;
  259. err = esdhc_setup_data(mmc, data);
  260. if(err)
  261. return err;
  262. }
  263. /* Figure out the transfer arguments */
  264. xfertyp = esdhc_xfertyp(cmd, data);
  265. /* Send the command */
  266. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  267. esdhc_write32(&regs->xfertyp, xfertyp);
  268. /* Wait for the command to complete */
  269. while (!(esdhc_read32(&regs->irqstat) & IRQSTAT_CC))
  270. ;
  271. irqstat = esdhc_read32(&regs->irqstat);
  272. esdhc_write32(&regs->irqstat, irqstat);
  273. if (irqstat & CMD_ERR)
  274. return COMM_ERR;
  275. if (irqstat & IRQSTAT_CTOE)
  276. return TIMEOUT;
  277. /* Copy the response to the response buffer */
  278. if (cmd->resp_type & MMC_RSP_136) {
  279. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  280. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  281. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  282. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  283. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  284. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  285. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  286. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  287. cmd->response[3] = (cmdrsp0 << 8);
  288. } else
  289. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  290. /* Wait until all of the blocks are transferred */
  291. if (data) {
  292. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  293. esdhc_pio_read_write(mmc, data);
  294. #else
  295. do {
  296. irqstat = esdhc_read32(&regs->irqstat);
  297. if (irqstat & IRQSTAT_DTOE)
  298. return TIMEOUT;
  299. if (irqstat & DATA_ERR)
  300. return COMM_ERR;
  301. } while (!(irqstat & IRQSTAT_TC) &&
  302. (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
  303. #endif
  304. }
  305. esdhc_write32(&regs->irqstat, -1);
  306. return 0;
  307. }
  308. void set_sysctl(struct mmc *mmc, uint clock)
  309. {
  310. int sdhc_clk = gd->sdhc_clk;
  311. int div, pre_div;
  312. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  313. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  314. uint clk;
  315. if (clock < mmc->f_min)
  316. clock = mmc->f_min;
  317. if (sdhc_clk / 16 > clock) {
  318. for (pre_div = 2; pre_div < 256; pre_div *= 2)
  319. if ((sdhc_clk / pre_div) <= (clock * 16))
  320. break;
  321. } else
  322. pre_div = 2;
  323. for (div = 1; div <= 16; div++)
  324. if ((sdhc_clk / (div * pre_div)) <= clock)
  325. break;
  326. pre_div >>= 1;
  327. div -= 1;
  328. clk = (pre_div << 8) | (div << 4);
  329. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  330. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  331. udelay(10000);
  332. clk = SYSCTL_PEREN | SYSCTL_CKEN;
  333. esdhc_setbits32(&regs->sysctl, clk);
  334. }
  335. static void esdhc_set_ios(struct mmc *mmc)
  336. {
  337. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  338. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  339. /* Set the clock speed */
  340. set_sysctl(mmc, mmc->clock);
  341. /* Set the bus width */
  342. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  343. if (mmc->bus_width == 4)
  344. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  345. else if (mmc->bus_width == 8)
  346. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  347. }
  348. static int esdhc_init(struct mmc *mmc)
  349. {
  350. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  351. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  352. int timeout = 1000;
  353. int ret = 0;
  354. u8 card_absent;
  355. /* Reset the entire host controller */
  356. esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
  357. /* Wait until the controller is available */
  358. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  359. udelay(1000);
  360. /* Enable cache snooping */
  361. if (cfg && !cfg->no_snoop)
  362. esdhc_write32(&regs->scr, 0x00000040);
  363. esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  364. /* Set the initial clock speed */
  365. mmc_set_clock(mmc, 400000);
  366. /* Disable the BRR and BWR bits in IRQSTAT */
  367. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  368. /* Put the PROCTL reg back to the default */
  369. esdhc_write32(&regs->proctl, PROCTL_INIT);
  370. /* Set timout to the maximum value */
  371. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  372. /* Check if there is a callback for detecting the card */
  373. if (board_mmc_getcd(&card_absent, mmc)) {
  374. timeout = 1000;
  375. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) &&
  376. --timeout)
  377. udelay(1000);
  378. if (timeout <= 0)
  379. ret = NO_CARD_ERR;
  380. } else {
  381. if (card_absent)
  382. ret = NO_CARD_ERR;
  383. }
  384. return ret;
  385. }
  386. static void esdhc_reset(struct fsl_esdhc *regs)
  387. {
  388. unsigned long timeout = 100; /* wait max 100 ms */
  389. /* reset the controller */
  390. esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
  391. /* hardware clears the bit when it is done */
  392. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  393. udelay(1000);
  394. if (!timeout)
  395. printf("MMC/SD: Reset never completed.\n");
  396. }
  397. int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
  398. {
  399. struct fsl_esdhc *regs;
  400. struct mmc *mmc;
  401. u32 caps, voltage_caps;
  402. if (!cfg)
  403. return -1;
  404. mmc = malloc(sizeof(struct mmc));
  405. sprintf(mmc->name, "FSL_ESDHC");
  406. regs = (struct fsl_esdhc *)cfg->esdhc_base;
  407. /* First reset the eSDHC controller */
  408. esdhc_reset(regs);
  409. mmc->priv = cfg;
  410. mmc->send_cmd = esdhc_send_cmd;
  411. mmc->set_ios = esdhc_set_ios;
  412. mmc->init = esdhc_init;
  413. voltage_caps = 0;
  414. caps = regs->hostcapblt;
  415. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
  416. caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
  417. ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
  418. #endif
  419. if (caps & ESDHC_HOSTCAPBLT_VS18)
  420. voltage_caps |= MMC_VDD_165_195;
  421. if (caps & ESDHC_HOSTCAPBLT_VS30)
  422. voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
  423. if (caps & ESDHC_HOSTCAPBLT_VS33)
  424. voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
  425. #ifdef CONFIG_SYS_SD_VOLTAGE
  426. mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
  427. #else
  428. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  429. #endif
  430. if ((mmc->voltages & voltage_caps) == 0) {
  431. printf("voltage not supported by controller\n");
  432. return -1;
  433. }
  434. mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  435. if (caps & ESDHC_HOSTCAPBLT_HSS)
  436. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  437. mmc->f_min = 400000;
  438. mmc->f_max = MIN(gd->sdhc_clk, 52000000);
  439. mmc->b_max = 0;
  440. mmc_register(mmc);
  441. return 0;
  442. }
  443. int fsl_esdhc_mmc_init(bd_t *bis)
  444. {
  445. struct fsl_esdhc_cfg *cfg;
  446. cfg = malloc(sizeof(struct fsl_esdhc_cfg));
  447. memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
  448. cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
  449. return fsl_esdhc_initialize(bis, cfg);
  450. }
  451. #ifdef CONFIG_OF_LIBFDT
  452. void fdt_fixup_esdhc(void *blob, bd_t *bd)
  453. {
  454. const char *compat = "fsl,esdhc";
  455. #ifdef CONFIG_FSL_ESDHC_PIN_MUX
  456. if (!hwconfig("esdhc")) {
  457. do_fixup_by_compat(blob, compat, "status", "disabled",
  458. 8 + 1, 1);
  459. return;
  460. }
  461. #endif
  462. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  463. gd->sdhc_clk, 1);
  464. do_fixup_by_compat(blob, compat, "status", "okay",
  465. 4 + 1, 1);
  466. }
  467. #endif