fsl_esdhc.c 26 KB

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