fsl_esdhc.c 14 KB

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