fsl_esdhc.c 14 KB

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