fsl_esdhc.c 26 KB

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