fsl_esdhc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 | XFERTYP_DMAEN;
  71. if (data->blocks > 1) {
  72. xfertyp |= XFERTYP_MSBSEL;
  73. xfertyp |= XFERTYP_BCEN;
  74. }
  75. if (data->flags & MMC_DATA_READ)
  76. xfertyp |= XFERTYP_DTDSEL;
  77. }
  78. if (cmd->resp_type & MMC_RSP_CRC)
  79. xfertyp |= XFERTYP_CCCEN;
  80. if (cmd->resp_type & MMC_RSP_OPCODE)
  81. xfertyp |= XFERTYP_CICEN;
  82. if (cmd->resp_type & MMC_RSP_136)
  83. xfertyp |= XFERTYP_RSPTYP_136;
  84. else if (cmd->resp_type & MMC_RSP_BUSY)
  85. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  86. else if (cmd->resp_type & MMC_RSP_PRESENT)
  87. xfertyp |= XFERTYP_RSPTYP_48;
  88. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  89. }
  90. static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
  91. {
  92. uint wml_value;
  93. int timeout;
  94. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  95. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  96. wml_value = data->blocksize/4;
  97. if (data->flags & MMC_DATA_READ) {
  98. if (wml_value > 0x10)
  99. wml_value = 0x10;
  100. wml_value = 0x100000 | wml_value;
  101. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  102. } else {
  103. if (wml_value > 0x80)
  104. wml_value = 0x80;
  105. if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
  106. printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
  107. return TIMEOUT;
  108. }
  109. wml_value = wml_value << 16 | 0x10;
  110. esdhc_write32(&regs->dsaddr, (u32)data->src);
  111. }
  112. esdhc_write32(&regs->wml, wml_value);
  113. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  114. /* Calculate the timeout period for data transactions */
  115. timeout = fls(mmc->tran_speed/10) - 1;
  116. timeout -= 13;
  117. if (timeout > 14)
  118. timeout = 14;
  119. if (timeout < 0)
  120. timeout = 0;
  121. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  122. return 0;
  123. }
  124. /*
  125. * Sends a command out on the bus. Takes the mmc pointer,
  126. * a command pointer, and an optional data pointer.
  127. */
  128. static int
  129. esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  130. {
  131. uint xfertyp;
  132. uint irqstat;
  133. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  134. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  135. esdhc_write32(&regs->irqstat, -1);
  136. sync();
  137. /* Wait for the bus to be idle */
  138. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  139. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  140. ;
  141. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  142. ;
  143. /* Wait at least 8 SD clock cycles before the next command */
  144. /*
  145. * Note: This is way more than 8 cycles, but 1ms seems to
  146. * resolve timing issues with some cards
  147. */
  148. udelay(1000);
  149. /* Set up for a data transfer if we have one */
  150. if (data) {
  151. int err;
  152. err = esdhc_setup_data(mmc, data);
  153. if(err)
  154. return err;
  155. }
  156. /* Figure out the transfer arguments */
  157. xfertyp = esdhc_xfertyp(cmd, data);
  158. /* Send the command */
  159. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  160. esdhc_write32(&regs->xfertyp, xfertyp);
  161. /* Wait for the command to complete */
  162. while (!(esdhc_read32(&regs->irqstat) & IRQSTAT_CC))
  163. ;
  164. irqstat = esdhc_read32(&regs->irqstat);
  165. esdhc_write32(&regs->irqstat, irqstat);
  166. if (irqstat & CMD_ERR)
  167. return COMM_ERR;
  168. if (irqstat & IRQSTAT_CTOE)
  169. return TIMEOUT;
  170. /* Copy the response to the response buffer */
  171. if (cmd->resp_type & MMC_RSP_136) {
  172. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  173. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  174. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  175. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  176. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  177. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  178. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  179. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  180. cmd->response[3] = (cmdrsp0 << 8);
  181. } else
  182. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  183. /* Wait until all of the blocks are transferred */
  184. if (data) {
  185. do {
  186. irqstat = esdhc_read32(&regs->irqstat);
  187. if (irqstat & DATA_ERR)
  188. return COMM_ERR;
  189. if (irqstat & IRQSTAT_DTOE)
  190. return TIMEOUT;
  191. } while (!(irqstat & IRQSTAT_TC) &&
  192. (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
  193. }
  194. esdhc_write32(&regs->irqstat, -1);
  195. return 0;
  196. }
  197. void set_sysctl(struct mmc *mmc, uint clock)
  198. {
  199. int sdhc_clk = gd->sdhc_clk;
  200. int div, pre_div;
  201. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  202. volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  203. uint clk;
  204. if (clock < mmc->f_min)
  205. clock = mmc->f_min;
  206. if (sdhc_clk / 16 > clock) {
  207. for (pre_div = 2; pre_div < 256; pre_div *= 2)
  208. if ((sdhc_clk / pre_div) <= (clock * 16))
  209. break;
  210. } else
  211. pre_div = 2;
  212. for (div = 1; div <= 16; div++)
  213. if ((sdhc_clk / (div * pre_div)) <= clock)
  214. break;
  215. pre_div >>= 1;
  216. div -= 1;
  217. clk = (pre_div << 8) | (div << 4);
  218. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  219. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  220. udelay(10000);
  221. clk = SYSCTL_PEREN | SYSCTL_CKEN;
  222. esdhc_setbits32(&regs->sysctl, clk);
  223. }
  224. static void esdhc_set_ios(struct mmc *mmc)
  225. {
  226. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  227. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  228. /* Set the clock speed */
  229. set_sysctl(mmc, mmc->clock);
  230. /* Set the bus width */
  231. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  232. if (mmc->bus_width == 4)
  233. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  234. else if (mmc->bus_width == 8)
  235. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  236. }
  237. static int esdhc_init(struct mmc *mmc)
  238. {
  239. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  240. struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
  241. int timeout = 1000;
  242. int ret = 0;
  243. u8 card_absent;
  244. /* Enable cache snooping */
  245. if (cfg && !cfg->no_snoop)
  246. esdhc_write32(&regs->scr, 0x00000040);
  247. /* Reset the entire host controller */
  248. esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
  249. /* Wait until the controller is available */
  250. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  251. udelay(1000);
  252. esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  253. /* Set the initial clock speed */
  254. set_sysctl(mmc, 400000);
  255. /* Disable the BRR and BWR bits in IRQSTAT */
  256. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  257. /* Put the PROCTL reg back to the default */
  258. esdhc_write32(&regs->proctl, PROCTL_INIT);
  259. /* Set timout to the maximum value */
  260. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  261. /* Check if there is a callback for detecting the card */
  262. if (board_mmc_getcd(&card_absent, mmc)) {
  263. timeout = 1000;
  264. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) &&
  265. --timeout)
  266. udelay(1000);
  267. if (timeout <= 0)
  268. ret = NO_CARD_ERR;
  269. } else {
  270. if (card_absent)
  271. ret = NO_CARD_ERR;
  272. }
  273. return ret;
  274. }
  275. int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
  276. {
  277. struct fsl_esdhc *regs;
  278. struct mmc *mmc;
  279. u32 caps;
  280. if (!cfg)
  281. return -1;
  282. mmc = malloc(sizeof(struct mmc));
  283. sprintf(mmc->name, "FSL_ESDHC");
  284. regs = (struct fsl_esdhc *)cfg->esdhc_base;
  285. mmc->priv = cfg;
  286. mmc->send_cmd = esdhc_send_cmd;
  287. mmc->set_ios = esdhc_set_ios;
  288. mmc->init = esdhc_init;
  289. caps = regs->hostcapblt;
  290. if (caps & ESDHC_HOSTCAPBLT_VS18)
  291. mmc->voltages |= MMC_VDD_165_195;
  292. if (caps & ESDHC_HOSTCAPBLT_VS30)
  293. mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  294. if (caps & ESDHC_HOSTCAPBLT_VS33)
  295. mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  296. mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  297. if (caps & ESDHC_HOSTCAPBLT_HSS)
  298. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  299. mmc->f_min = 400000;
  300. mmc->f_max = MIN(gd->sdhc_clk, 50000000);
  301. mmc_register(mmc);
  302. return 0;
  303. }
  304. int fsl_esdhc_mmc_init(bd_t *bis)
  305. {
  306. struct fsl_esdhc_cfg *cfg;
  307. cfg = malloc(sizeof(struct fsl_esdhc_cfg));
  308. memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
  309. cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
  310. return fsl_esdhc_initialize(bis, cfg);
  311. }
  312. #ifdef CONFIG_OF_LIBFDT
  313. void fdt_fixup_esdhc(void *blob, bd_t *bd)
  314. {
  315. const char *compat = "fsl,esdhc";
  316. const char *status = "okay";
  317. if (!hwconfig("esdhc")) {
  318. status = "disabled";
  319. goto out;
  320. }
  321. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  322. gd->sdhc_clk, 1);
  323. out:
  324. do_fixup_by_compat(blob, compat, "status", status,
  325. strlen(status) + 1, 1);
  326. }
  327. #endif