fsl_esdhc.c 14 KB

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