fsl_esdhc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <command.h>
  14. #include <errno.h>
  15. #include <hwconfig.h>
  16. #include <mmc.h>
  17. #include <part.h>
  18. #include <malloc.h>
  19. #include <fsl_esdhc.h>
  20. #include <fdt_support.h>
  21. #include <asm/io.h>
  22. #include <dm.h>
  23. #include <asm-generic/gpio.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
  26. IRQSTATEN_CINT | \
  27. IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
  28. IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
  29. IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
  30. IRQSTATEN_DINT)
  31. struct fsl_esdhc {
  32. uint dsaddr; /* SDMA system address register */
  33. uint blkattr; /* Block attributes register */
  34. uint cmdarg; /* Command argument register */
  35. uint xfertyp; /* Transfer type register */
  36. uint cmdrsp0; /* Command response 0 register */
  37. uint cmdrsp1; /* Command response 1 register */
  38. uint cmdrsp2; /* Command response 2 register */
  39. uint cmdrsp3; /* Command response 3 register */
  40. uint datport; /* Buffer data port register */
  41. uint prsstat; /* Present state register */
  42. uint proctl; /* Protocol control register */
  43. uint sysctl; /* System Control Register */
  44. uint irqstat; /* Interrupt status register */
  45. uint irqstaten; /* Interrupt status enable register */
  46. uint irqsigen; /* Interrupt signal enable register */
  47. uint autoc12err; /* Auto CMD error status register */
  48. uint hostcapblt; /* Host controller capabilities register */
  49. uint wml; /* Watermark level register */
  50. uint mixctrl; /* For USDHC */
  51. char reserved1[4]; /* reserved */
  52. uint fevt; /* Force event register */
  53. uint admaes; /* ADMA error status register */
  54. uint adsaddr; /* ADMA system address register */
  55. char reserved2[4];
  56. uint dllctrl;
  57. uint dllstat;
  58. uint clktunectrlstatus;
  59. char reserved3[84];
  60. uint vendorspec;
  61. uint mmcboot;
  62. uint vendorspec2;
  63. char reserved4[48];
  64. uint hostver; /* Host controller version register */
  65. char reserved5[4]; /* reserved */
  66. uint dmaerraddr; /* DMA error address register */
  67. char reserved6[4]; /* reserved */
  68. uint dmaerrattr; /* DMA error attribute register */
  69. char reserved7[4]; /* reserved */
  70. uint hostcapblt2; /* Host controller capabilities register 2 */
  71. char reserved8[8]; /* reserved */
  72. uint tcr; /* Tuning control register */
  73. char reserved9[28]; /* reserved */
  74. uint sddirctl; /* SD direction control register */
  75. char reserved10[712];/* reserved */
  76. uint scr; /* eSDHC control register */
  77. };
  78. /**
  79. * struct fsl_esdhc_priv
  80. *
  81. * @esdhc_regs: registers of the sdhc controller
  82. * @sdhc_clk: Current clk of the sdhc controller
  83. * @bus_width: bus width, 1bit, 4bit or 8bit
  84. * @cfg: mmc config
  85. * @mmc: mmc
  86. * Following is used when Driver Model is enabled for MMC
  87. * @dev: pointer for the device
  88. * @non_removable: 0: removable; 1: non-removable
  89. * @wp_enable: 1: enable checking wp; 0: no check
  90. * @cd_gpio: gpio for card detection
  91. * @wp_gpio: gpio for write protection
  92. */
  93. struct fsl_esdhc_priv {
  94. struct fsl_esdhc *esdhc_regs;
  95. unsigned int sdhc_clk;
  96. unsigned int bus_width;
  97. struct mmc_config cfg;
  98. struct mmc *mmc;
  99. struct udevice *dev;
  100. int non_removable;
  101. int wp_enable;
  102. struct gpio_desc cd_gpio;
  103. struct gpio_desc wp_gpio;
  104. };
  105. /* Return the XFERTYP flags for a given command and data packet */
  106. static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
  107. {
  108. uint xfertyp = 0;
  109. if (data) {
  110. xfertyp |= XFERTYP_DPSEL;
  111. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  112. xfertyp |= XFERTYP_DMAEN;
  113. #endif
  114. if (data->blocks > 1) {
  115. xfertyp |= XFERTYP_MSBSEL;
  116. xfertyp |= XFERTYP_BCEN;
  117. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  118. xfertyp |= XFERTYP_AC12EN;
  119. #endif
  120. }
  121. if (data->flags & MMC_DATA_READ)
  122. xfertyp |= XFERTYP_DTDSEL;
  123. }
  124. if (cmd->resp_type & MMC_RSP_CRC)
  125. xfertyp |= XFERTYP_CCCEN;
  126. if (cmd->resp_type & MMC_RSP_OPCODE)
  127. xfertyp |= XFERTYP_CICEN;
  128. if (cmd->resp_type & MMC_RSP_136)
  129. xfertyp |= XFERTYP_RSPTYP_136;
  130. else if (cmd->resp_type & MMC_RSP_BUSY)
  131. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  132. else if (cmd->resp_type & MMC_RSP_PRESENT)
  133. xfertyp |= XFERTYP_RSPTYP_48;
  134. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  135. xfertyp |= XFERTYP_CMDTYP_ABORT;
  136. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  137. }
  138. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  139. /*
  140. * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
  141. */
  142. static void
  143. esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
  144. {
  145. struct fsl_esdhc_priv *priv = mmc->priv;
  146. struct fsl_esdhc *regs = priv->esdhc_regs;
  147. uint blocks;
  148. char *buffer;
  149. uint databuf;
  150. uint size;
  151. uint irqstat;
  152. uint timeout;
  153. if (data->flags & MMC_DATA_READ) {
  154. blocks = data->blocks;
  155. buffer = data->dest;
  156. while (blocks) {
  157. timeout = PIO_TIMEOUT;
  158. size = data->blocksize;
  159. irqstat = esdhc_read32(&regs->irqstat);
  160. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
  161. && --timeout);
  162. if (timeout <= 0) {
  163. printf("\nData Read Failed in PIO Mode.");
  164. return;
  165. }
  166. while (size && (!(irqstat & IRQSTAT_TC))) {
  167. udelay(100); /* Wait before last byte transfer complete */
  168. irqstat = esdhc_read32(&regs->irqstat);
  169. databuf = in_le32(&regs->datport);
  170. *((uint *)buffer) = databuf;
  171. buffer += 4;
  172. size -= 4;
  173. }
  174. blocks--;
  175. }
  176. } else {
  177. blocks = data->blocks;
  178. buffer = (char *)data->src;
  179. while (blocks) {
  180. timeout = PIO_TIMEOUT;
  181. size = data->blocksize;
  182. irqstat = esdhc_read32(&regs->irqstat);
  183. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
  184. && --timeout);
  185. if (timeout <= 0) {
  186. printf("\nData Write Failed in PIO Mode.");
  187. return;
  188. }
  189. while (size && (!(irqstat & IRQSTAT_TC))) {
  190. udelay(100); /* Wait before last byte transfer complete */
  191. databuf = *((uint *)buffer);
  192. buffer += 4;
  193. size -= 4;
  194. irqstat = esdhc_read32(&regs->irqstat);
  195. out_le32(&regs->datport, databuf);
  196. }
  197. blocks--;
  198. }
  199. }
  200. }
  201. #endif
  202. static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
  203. {
  204. int timeout;
  205. struct fsl_esdhc_priv *priv = mmc->priv;
  206. struct fsl_esdhc *regs = priv->esdhc_regs;
  207. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  208. dma_addr_t addr;
  209. #endif
  210. uint wml_value;
  211. wml_value = data->blocksize/4;
  212. if (data->flags & MMC_DATA_READ) {
  213. if (wml_value > WML_RD_WML_MAX)
  214. wml_value = WML_RD_WML_MAX_VAL;
  215. esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
  216. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  217. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  218. addr = virt_to_phys((void *)(data->dest));
  219. if (upper_32_bits(addr))
  220. printf("Error found for upper 32 bits\n");
  221. else
  222. esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
  223. #else
  224. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  225. #endif
  226. #endif
  227. } else {
  228. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  229. flush_dcache_range((ulong)data->src,
  230. (ulong)data->src+data->blocks
  231. *data->blocksize);
  232. #endif
  233. if (wml_value > WML_WR_WML_MAX)
  234. wml_value = WML_WR_WML_MAX_VAL;
  235. if (priv->wp_enable) {
  236. if ((esdhc_read32(&regs->prsstat) &
  237. PRSSTAT_WPSPL) == 0) {
  238. printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
  239. return -ETIMEDOUT;
  240. }
  241. }
  242. esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
  243. wml_value << 16);
  244. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  245. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  246. addr = virt_to_phys((void *)(data->src));
  247. if (upper_32_bits(addr))
  248. printf("Error found for upper 32 bits\n");
  249. else
  250. esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
  251. #else
  252. esdhc_write32(&regs->dsaddr, (u32)data->src);
  253. #endif
  254. #endif
  255. }
  256. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  257. /* Calculate the timeout period for data transactions */
  258. /*
  259. * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
  260. * 2)Timeout period should be minimum 0.250sec as per SD Card spec
  261. * So, Number of SD Clock cycles for 0.25sec should be minimum
  262. * (SD Clock/sec * 0.25 sec) SD Clock cycles
  263. * = (mmc->clock * 1/4) SD Clock cycles
  264. * As 1) >= 2)
  265. * => (2^(timeout+13)) >= mmc->clock * 1/4
  266. * Taking log2 both the sides
  267. * => timeout + 13 >= log2(mmc->clock/4)
  268. * Rounding up to next power of 2
  269. * => timeout + 13 = log2(mmc->clock/4) + 1
  270. * => timeout + 13 = fls(mmc->clock/4)
  271. *
  272. * However, the MMC spec "It is strongly recommended for hosts to
  273. * implement more than 500ms timeout value even if the card
  274. * indicates the 250ms maximum busy length." Even the previous
  275. * value of 300ms is known to be insufficient for some cards.
  276. * So, we use
  277. * => timeout + 13 = fls(mmc->clock/2)
  278. */
  279. timeout = fls(mmc->clock/2);
  280. timeout -= 13;
  281. if (timeout > 14)
  282. timeout = 14;
  283. if (timeout < 0)
  284. timeout = 0;
  285. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
  286. if ((timeout == 4) || (timeout == 8) || (timeout == 12))
  287. timeout++;
  288. #endif
  289. #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
  290. timeout = 0xE;
  291. #endif
  292. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  293. return 0;
  294. }
  295. static void check_and_invalidate_dcache_range
  296. (struct mmc_cmd *cmd,
  297. struct mmc_data *data) {
  298. unsigned start = 0;
  299. unsigned end = 0;
  300. unsigned size = roundup(ARCH_DMA_MINALIGN,
  301. data->blocks*data->blocksize);
  302. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  303. dma_addr_t addr;
  304. addr = virt_to_phys((void *)(data->dest));
  305. if (upper_32_bits(addr))
  306. printf("Error found for upper 32 bits\n");
  307. else
  308. start = lower_32_bits(addr);
  309. #else
  310. start = (unsigned)data->dest;
  311. #endif
  312. end = start + size;
  313. invalidate_dcache_range(start, end);
  314. }
  315. /*
  316. * Sends a command out on the bus. Takes the mmc pointer,
  317. * a command pointer, and an optional data pointer.
  318. */
  319. static int
  320. esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  321. {
  322. int err = 0;
  323. uint xfertyp;
  324. uint irqstat;
  325. struct fsl_esdhc_priv *priv = mmc->priv;
  326. struct fsl_esdhc *regs = priv->esdhc_regs;
  327. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  328. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  329. return 0;
  330. #endif
  331. esdhc_write32(&regs->irqstat, -1);
  332. sync();
  333. /* Wait for the bus to be idle */
  334. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  335. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  336. ;
  337. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  338. ;
  339. /* Wait at least 8 SD clock cycles before the next command */
  340. /*
  341. * Note: This is way more than 8 cycles, but 1ms seems to
  342. * resolve timing issues with some cards
  343. */
  344. udelay(1000);
  345. /* Set up for a data transfer if we have one */
  346. if (data) {
  347. err = esdhc_setup_data(mmc, data);
  348. if(err)
  349. return err;
  350. if (data->flags & MMC_DATA_READ)
  351. check_and_invalidate_dcache_range(cmd, data);
  352. }
  353. /* Figure out the transfer arguments */
  354. xfertyp = esdhc_xfertyp(cmd, data);
  355. /* Mask all irqs */
  356. esdhc_write32(&regs->irqsigen, 0);
  357. /* Send the command */
  358. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  359. #if defined(CONFIG_FSL_USDHC)
  360. esdhc_write32(&regs->mixctrl,
  361. (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
  362. | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
  363. esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
  364. #else
  365. esdhc_write32(&regs->xfertyp, xfertyp);
  366. #endif
  367. /* Wait for the command to complete */
  368. while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
  369. ;
  370. irqstat = esdhc_read32(&regs->irqstat);
  371. if (irqstat & CMD_ERR) {
  372. err = -ECOMM;
  373. goto out;
  374. }
  375. if (irqstat & IRQSTAT_CTOE) {
  376. err = -ETIMEDOUT;
  377. goto out;
  378. }
  379. /* Switch voltage to 1.8V if CMD11 succeeded */
  380. if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
  381. esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
  382. printf("Run CMD11 1.8V switch\n");
  383. /* Sleep for 5 ms - max time for card to switch to 1.8V */
  384. udelay(5000);
  385. }
  386. /* Workaround for ESDHC errata ENGcm03648 */
  387. if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
  388. int timeout = 6000;
  389. /* Poll on DATA0 line for cmd with busy signal for 600 ms */
  390. while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
  391. PRSSTAT_DAT0)) {
  392. udelay(100);
  393. timeout--;
  394. }
  395. if (timeout <= 0) {
  396. printf("Timeout waiting for DAT0 to go high!\n");
  397. err = -ETIMEDOUT;
  398. goto out;
  399. }
  400. }
  401. /* Copy the response to the response buffer */
  402. if (cmd->resp_type & MMC_RSP_136) {
  403. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  404. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  405. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  406. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  407. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  408. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  409. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  410. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  411. cmd->response[3] = (cmdrsp0 << 8);
  412. } else
  413. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  414. /* Wait until all of the blocks are transferred */
  415. if (data) {
  416. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  417. esdhc_pio_read_write(mmc, data);
  418. #else
  419. do {
  420. irqstat = esdhc_read32(&regs->irqstat);
  421. if (irqstat & IRQSTAT_DTOE) {
  422. err = -ETIMEDOUT;
  423. goto out;
  424. }
  425. if (irqstat & DATA_ERR) {
  426. err = -ECOMM;
  427. goto out;
  428. }
  429. } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
  430. /*
  431. * Need invalidate the dcache here again to avoid any
  432. * cache-fill during the DMA operations such as the
  433. * speculative pre-fetching etc.
  434. */
  435. if (data->flags & MMC_DATA_READ)
  436. check_and_invalidate_dcache_range(cmd, data);
  437. #endif
  438. }
  439. out:
  440. /* Reset CMD and DATA portions on error */
  441. if (err) {
  442. esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
  443. SYSCTL_RSTC);
  444. while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
  445. ;
  446. if (data) {
  447. esdhc_write32(&regs->sysctl,
  448. esdhc_read32(&regs->sysctl) |
  449. SYSCTL_RSTD);
  450. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
  451. ;
  452. }
  453. /* If this was CMD11, then notify that power cycle is needed */
  454. if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
  455. printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
  456. }
  457. esdhc_write32(&regs->irqstat, -1);
  458. return err;
  459. }
  460. static void set_sysctl(struct mmc *mmc, uint clock)
  461. {
  462. int div, pre_div;
  463. struct fsl_esdhc_priv *priv = mmc->priv;
  464. struct fsl_esdhc *regs = priv->esdhc_regs;
  465. int sdhc_clk = priv->sdhc_clk;
  466. uint clk;
  467. if (clock < mmc->cfg->f_min)
  468. clock = mmc->cfg->f_min;
  469. if (sdhc_clk / 16 > clock) {
  470. for (pre_div = 2; pre_div < 256; pre_div *= 2)
  471. if ((sdhc_clk / pre_div) <= (clock * 16))
  472. break;
  473. } else
  474. pre_div = 2;
  475. for (div = 1; div <= 16; div++)
  476. if ((sdhc_clk / (div * pre_div)) <= clock)
  477. break;
  478. pre_div >>= mmc->ddr_mode ? 2 : 1;
  479. div -= 1;
  480. clk = (pre_div << 8) | (div << 4);
  481. #ifdef CONFIG_FSL_USDHC
  482. esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
  483. #else
  484. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  485. #endif
  486. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  487. udelay(10000);
  488. #ifdef CONFIG_FSL_USDHC
  489. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
  490. #else
  491. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
  492. #endif
  493. }
  494. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  495. static void esdhc_clock_control(struct mmc *mmc, bool enable)
  496. {
  497. struct fsl_esdhc_priv *priv = mmc->priv;
  498. struct fsl_esdhc *regs = priv->esdhc_regs;
  499. u32 value;
  500. u32 time_out;
  501. value = esdhc_read32(&regs->sysctl);
  502. if (enable)
  503. value |= SYSCTL_CKEN;
  504. else
  505. value &= ~SYSCTL_CKEN;
  506. esdhc_write32(&regs->sysctl, value);
  507. time_out = 20;
  508. value = PRSSTAT_SDSTB;
  509. while (!(esdhc_read32(&regs->prsstat) & value)) {
  510. if (time_out == 0) {
  511. printf("fsl_esdhc: Internal clock never stabilised.\n");
  512. break;
  513. }
  514. time_out--;
  515. mdelay(1);
  516. }
  517. }
  518. #endif
  519. static int esdhc_set_ios(struct mmc *mmc)
  520. {
  521. struct fsl_esdhc_priv *priv = mmc->priv;
  522. struct fsl_esdhc *regs = priv->esdhc_regs;
  523. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  524. /* Select to use peripheral clock */
  525. esdhc_clock_control(mmc, false);
  526. esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
  527. esdhc_clock_control(mmc, true);
  528. #endif
  529. /* Set the clock speed */
  530. set_sysctl(mmc, mmc->clock);
  531. /* Set the bus width */
  532. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  533. if (mmc->bus_width == 4)
  534. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  535. else if (mmc->bus_width == 8)
  536. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  537. return 0;
  538. }
  539. static int esdhc_init(struct mmc *mmc)
  540. {
  541. struct fsl_esdhc_priv *priv = mmc->priv;
  542. struct fsl_esdhc *regs = priv->esdhc_regs;
  543. int timeout = 1000;
  544. /* Reset the entire host controller */
  545. esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
  546. /* Wait until the controller is available */
  547. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  548. udelay(1000);
  549. #if defined(CONFIG_FSL_USDHC)
  550. /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
  551. esdhc_write32(&regs->mmcboot, 0x0);
  552. /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
  553. esdhc_write32(&regs->mixctrl, 0x0);
  554. esdhc_write32(&regs->clktunectrlstatus, 0x0);
  555. /* Put VEND_SPEC to default value */
  556. esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
  557. /* Disable DLL_CTRL delay line */
  558. esdhc_write32(&regs->dllctrl, 0x0);
  559. #endif
  560. #ifndef ARCH_MXC
  561. /* Enable cache snooping */
  562. esdhc_write32(&regs->scr, 0x00000040);
  563. #endif
  564. #ifndef CONFIG_FSL_USDHC
  565. esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  566. #else
  567. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
  568. #endif
  569. /* Set the initial clock speed */
  570. mmc_set_clock(mmc, 400000, false);
  571. /* Disable the BRR and BWR bits in IRQSTAT */
  572. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  573. /* Put the PROCTL reg back to the default */
  574. esdhc_write32(&regs->proctl, PROCTL_INIT);
  575. /* Set timout to the maximum value */
  576. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  577. #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
  578. esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
  579. #endif
  580. return 0;
  581. }
  582. static int esdhc_getcd(struct mmc *mmc)
  583. {
  584. struct fsl_esdhc_priv *priv = mmc->priv;
  585. struct fsl_esdhc *regs = priv->esdhc_regs;
  586. int timeout = 1000;
  587. #ifdef CONFIG_ESDHC_DETECT_QUIRK
  588. if (CONFIG_ESDHC_DETECT_QUIRK)
  589. return 1;
  590. #endif
  591. #ifdef CONFIG_DM_MMC
  592. if (priv->non_removable)
  593. return 1;
  594. if (dm_gpio_is_valid(&priv->cd_gpio))
  595. return dm_gpio_get_value(&priv->cd_gpio);
  596. #endif
  597. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
  598. udelay(1000);
  599. return timeout > 0;
  600. }
  601. static void esdhc_reset(struct fsl_esdhc *regs)
  602. {
  603. unsigned long timeout = 100; /* wait max 100 ms */
  604. /* reset the controller */
  605. esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
  606. /* hardware clears the bit when it is done */
  607. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  608. udelay(1000);
  609. if (!timeout)
  610. printf("MMC/SD: Reset never completed.\n");
  611. }
  612. static const struct mmc_ops esdhc_ops = {
  613. .send_cmd = esdhc_send_cmd,
  614. .set_ios = esdhc_set_ios,
  615. .init = esdhc_init,
  616. .getcd = esdhc_getcd,
  617. };
  618. static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
  619. struct fsl_esdhc_priv *priv)
  620. {
  621. if (!cfg || !priv)
  622. return -EINVAL;
  623. priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
  624. priv->bus_width = cfg->max_bus_width;
  625. priv->sdhc_clk = cfg->sdhc_clk;
  626. priv->wp_enable = cfg->wp_enable;
  627. return 0;
  628. };
  629. static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
  630. {
  631. struct fsl_esdhc *regs;
  632. struct mmc *mmc;
  633. u32 caps, voltage_caps;
  634. if (!priv)
  635. return -EINVAL;
  636. regs = priv->esdhc_regs;
  637. /* First reset the eSDHC controller */
  638. esdhc_reset(regs);
  639. #ifndef CONFIG_FSL_USDHC
  640. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
  641. | SYSCTL_IPGEN | SYSCTL_CKEN);
  642. #else
  643. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
  644. VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
  645. #endif
  646. writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
  647. memset(&priv->cfg, 0, sizeof(priv->cfg));
  648. voltage_caps = 0;
  649. caps = esdhc_read32(&regs->hostcapblt);
  650. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
  651. caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
  652. ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
  653. #endif
  654. /* T4240 host controller capabilities register should have VS33 bit */
  655. #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
  656. caps = caps | ESDHC_HOSTCAPBLT_VS33;
  657. #endif
  658. if (caps & ESDHC_HOSTCAPBLT_VS18)
  659. voltage_caps |= MMC_VDD_165_195;
  660. if (caps & ESDHC_HOSTCAPBLT_VS30)
  661. voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
  662. if (caps & ESDHC_HOSTCAPBLT_VS33)
  663. voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
  664. priv->cfg.name = "FSL_SDHC";
  665. priv->cfg.ops = &esdhc_ops;
  666. #ifdef CONFIG_SYS_SD_VOLTAGE
  667. priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
  668. #else
  669. priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  670. #endif
  671. if ((priv->cfg.voltages & voltage_caps) == 0) {
  672. printf("voltage not supported by controller\n");
  673. return -1;
  674. }
  675. if (priv->bus_width == 8)
  676. priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  677. else if (priv->bus_width == 4)
  678. priv->cfg.host_caps = MMC_MODE_4BIT;
  679. priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  680. #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
  681. priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
  682. #endif
  683. if (priv->bus_width > 0) {
  684. if (priv->bus_width < 8)
  685. priv->cfg.host_caps &= ~MMC_MODE_8BIT;
  686. if (priv->bus_width < 4)
  687. priv->cfg.host_caps &= ~MMC_MODE_4BIT;
  688. }
  689. if (caps & ESDHC_HOSTCAPBLT_HSS)
  690. priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  691. #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
  692. if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
  693. priv->cfg.host_caps &= ~MMC_MODE_8BIT;
  694. #endif
  695. priv->cfg.f_min = 400000;
  696. priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
  697. priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  698. mmc = mmc_create(&priv->cfg, priv);
  699. if (mmc == NULL)
  700. return -1;
  701. priv->mmc = mmc;
  702. return 0;
  703. }
  704. int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
  705. {
  706. struct fsl_esdhc_priv *priv;
  707. int ret;
  708. if (!cfg)
  709. return -EINVAL;
  710. priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
  711. if (!priv)
  712. return -ENOMEM;
  713. ret = fsl_esdhc_cfg_to_priv(cfg, priv);
  714. if (ret) {
  715. debug("%s xlate failure\n", __func__);
  716. free(priv);
  717. return ret;
  718. }
  719. ret = fsl_esdhc_init(priv);
  720. if (ret) {
  721. debug("%s init failure\n", __func__);
  722. free(priv);
  723. return ret;
  724. }
  725. return 0;
  726. }
  727. int fsl_esdhc_mmc_init(bd_t *bis)
  728. {
  729. struct fsl_esdhc_cfg *cfg;
  730. cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
  731. cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
  732. cfg->sdhc_clk = gd->arch.sdhc_clk;
  733. return fsl_esdhc_initialize(bis, cfg);
  734. }
  735. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  736. void mmc_adapter_card_type_ident(void)
  737. {
  738. u8 card_id;
  739. u8 value;
  740. card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
  741. gd->arch.sdhc_adapter = card_id;
  742. switch (card_id) {
  743. case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
  744. value = QIXIS_READ(brdcfg[5]);
  745. value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
  746. QIXIS_WRITE(brdcfg[5], value);
  747. break;
  748. case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
  749. value = QIXIS_READ(pwr_ctl[1]);
  750. value |= QIXIS_EVDD_BY_SDHC_VS;
  751. QIXIS_WRITE(pwr_ctl[1], value);
  752. break;
  753. case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
  754. value = QIXIS_READ(brdcfg[5]);
  755. value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
  756. QIXIS_WRITE(brdcfg[5], value);
  757. break;
  758. case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
  759. break;
  760. case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
  761. break;
  762. case QIXIS_ESDHC_ADAPTER_TYPE_SD:
  763. break;
  764. case QIXIS_ESDHC_NO_ADAPTER:
  765. break;
  766. default:
  767. break;
  768. }
  769. }
  770. #endif
  771. #ifdef CONFIG_OF_LIBFDT
  772. void fdt_fixup_esdhc(void *blob, bd_t *bd)
  773. {
  774. const char *compat = "fsl,esdhc";
  775. #ifdef CONFIG_FSL_ESDHC_PIN_MUX
  776. if (!hwconfig("esdhc")) {
  777. do_fixup_by_compat(blob, compat, "status", "disabled",
  778. 8 + 1, 1);
  779. return;
  780. }
  781. #endif
  782. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  783. do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
  784. gd->arch.sdhc_clk, 1);
  785. #else
  786. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  787. gd->arch.sdhc_clk, 1);
  788. #endif
  789. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  790. do_fixup_by_compat_u32(blob, compat, "adapter-type",
  791. (u32)(gd->arch.sdhc_adapter), 1);
  792. #endif
  793. do_fixup_by_compat(blob, compat, "status", "okay",
  794. 4 + 1, 1);
  795. }
  796. #endif
  797. #ifdef CONFIG_DM_MMC
  798. #include <asm/arch/clock.h>
  799. static int fsl_esdhc_probe(struct udevice *dev)
  800. {
  801. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  802. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  803. const void *fdt = gd->fdt_blob;
  804. int node = dev->of_offset;
  805. fdt_addr_t addr;
  806. unsigned int val;
  807. int ret;
  808. addr = dev_get_addr(dev);
  809. if (addr == FDT_ADDR_T_NONE)
  810. return -EINVAL;
  811. priv->esdhc_regs = (struct fsl_esdhc *)addr;
  812. priv->dev = dev;
  813. val = fdtdec_get_int(fdt, node, "bus-width", -1);
  814. if (val == 8)
  815. priv->bus_width = 8;
  816. else if (val == 4)
  817. priv->bus_width = 4;
  818. else
  819. priv->bus_width = 1;
  820. if (fdt_get_property(fdt, node, "non-removable", NULL)) {
  821. priv->non_removable = 1;
  822. } else {
  823. priv->non_removable = 0;
  824. gpio_request_by_name_nodev(fdt, node, "cd-gpios", 0,
  825. &priv->cd_gpio, GPIOD_IS_IN);
  826. }
  827. priv->wp_enable = 1;
  828. ret = gpio_request_by_name_nodev(fdt, node, "wp-gpios", 0,
  829. &priv->wp_gpio, GPIOD_IS_IN);
  830. if (ret)
  831. priv->wp_enable = 0;
  832. /*
  833. * TODO:
  834. * Because lack of clk driver, if SDHC clk is not enabled,
  835. * need to enable it first before this driver is invoked.
  836. *
  837. * we use MXC_ESDHC_CLK to get clk freq.
  838. * If one would like to make this function work,
  839. * the aliases should be provided in dts as this:
  840. *
  841. * aliases {
  842. * mmc0 = &usdhc1;
  843. * mmc1 = &usdhc2;
  844. * mmc2 = &usdhc3;
  845. * mmc3 = &usdhc4;
  846. * };
  847. * Then if your board only supports mmc2 and mmc3, but we can
  848. * correctly get the seq as 2 and 3, then let mxc_get_clock
  849. * work as expected.
  850. */
  851. priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
  852. if (priv->sdhc_clk <= 0) {
  853. dev_err(dev, "Unable to get clk for %s\n", dev->name);
  854. return -EINVAL;
  855. }
  856. ret = fsl_esdhc_init(priv);
  857. if (ret) {
  858. dev_err(dev, "fsl_esdhc_init failure\n");
  859. return ret;
  860. }
  861. upriv->mmc = priv->mmc;
  862. priv->mmc->dev = dev;
  863. return 0;
  864. }
  865. static const struct udevice_id fsl_esdhc_ids[] = {
  866. { .compatible = "fsl,imx6ul-usdhc", },
  867. { .compatible = "fsl,imx6sx-usdhc", },
  868. { .compatible = "fsl,imx6sl-usdhc", },
  869. { .compatible = "fsl,imx6q-usdhc", },
  870. { .compatible = "fsl,imx7d-usdhc", },
  871. { /* sentinel */ }
  872. };
  873. U_BOOT_DRIVER(fsl_esdhc) = {
  874. .name = "fsl-esdhc-mmc",
  875. .id = UCLASS_MMC,
  876. .of_match = fsl_esdhc_ids,
  877. .probe = fsl_esdhc_probe,
  878. .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
  879. };
  880. #endif