sdhci-of-esdhc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Freescale eSDHC controller driver.
  3. *
  4. * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
  5. * Copyright (c) 2009 MontaVista Software, Inc.
  6. *
  7. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  8. * Anton Vorontsov <avorontsov@ru.mvista.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/sys_soc.h>
  22. #include <linux/clk.h>
  23. #include <linux/ktime.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/mmc/host.h>
  26. #include "sdhci-pltfm.h"
  27. #include "sdhci-esdhc.h"
  28. #define VENDOR_V_22 0x12
  29. #define VENDOR_V_23 0x13
  30. #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
  31. struct esdhc_clk_fixup {
  32. const unsigned int sd_dflt_max_clk;
  33. const unsigned int max_clk[MMC_TIMING_NUM];
  34. };
  35. static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
  36. .sd_dflt_max_clk = 25000000,
  37. .max_clk[MMC_TIMING_MMC_HS] = 46500000,
  38. .max_clk[MMC_TIMING_SD_HS] = 46500000,
  39. };
  40. static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
  41. .sd_dflt_max_clk = 25000000,
  42. .max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
  43. .max_clk[MMC_TIMING_MMC_HS200] = 167000000,
  44. };
  45. static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
  46. .sd_dflt_max_clk = 25000000,
  47. .max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
  48. .max_clk[MMC_TIMING_MMC_HS200] = 125000000,
  49. };
  50. static const struct esdhc_clk_fixup p1010_esdhc_clk = {
  51. .sd_dflt_max_clk = 20000000,
  52. .max_clk[MMC_TIMING_LEGACY] = 20000000,
  53. .max_clk[MMC_TIMING_MMC_HS] = 42000000,
  54. .max_clk[MMC_TIMING_SD_HS] = 40000000,
  55. };
  56. static const struct of_device_id sdhci_esdhc_of_match[] = {
  57. { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
  58. { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
  59. { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
  60. { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk},
  61. { .compatible = "fsl,mpc8379-esdhc" },
  62. { .compatible = "fsl,mpc8536-esdhc" },
  63. { .compatible = "fsl,esdhc" },
  64. { }
  65. };
  66. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  67. struct sdhci_esdhc {
  68. u8 vendor_ver;
  69. u8 spec_ver;
  70. bool quirk_incorrect_hostver;
  71. bool quirk_fixup_tuning;
  72. unsigned int peripheral_clock;
  73. const struct esdhc_clk_fixup *clk_fixup;
  74. u32 div_ratio;
  75. };
  76. /**
  77. * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
  78. * to make it compatible with SD spec.
  79. *
  80. * @host: pointer to sdhci_host
  81. * @spec_reg: SD spec register address
  82. * @value: 32bit eSDHC register value on spec_reg address
  83. *
  84. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  85. * registers are 32 bits. There are differences in register size, register
  86. * address, register function, bit position and function between eSDHC spec
  87. * and SD spec.
  88. *
  89. * Return a fixed up register value
  90. */
  91. static u32 esdhc_readl_fixup(struct sdhci_host *host,
  92. int spec_reg, u32 value)
  93. {
  94. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  95. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  96. u32 ret;
  97. /*
  98. * The bit of ADMA flag in eSDHC is not compatible with standard
  99. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  100. * supported by eSDHC.
  101. * And for many FSL eSDHC controller, the reset value of field
  102. * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
  103. * only these vendor version is greater than 2.2/0x12 support ADMA.
  104. */
  105. if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
  106. if (esdhc->vendor_ver > VENDOR_V_22) {
  107. ret = value | SDHCI_CAN_DO_ADMA2;
  108. return ret;
  109. }
  110. }
  111. /*
  112. * The DAT[3:0] line signal levels and the CMD line signal level are
  113. * not compatible with standard SDHC register. The line signal levels
  114. * DAT[7:0] are at bits 31:24 and the command line signal level is at
  115. * bit 23. All other bits are the same as in the standard SDHC
  116. * register.
  117. */
  118. if (spec_reg == SDHCI_PRESENT_STATE) {
  119. ret = value & 0x000fffff;
  120. ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
  121. ret |= (value << 1) & SDHCI_CMD_LVL;
  122. return ret;
  123. }
  124. /*
  125. * DTS properties of mmc host are used to enable each speed mode
  126. * according to soc and board capability. So clean up
  127. * SDR50/SDR104/DDR50 support bits here.
  128. */
  129. if (spec_reg == SDHCI_CAPABILITIES_1) {
  130. ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
  131. SDHCI_SUPPORT_DDR50);
  132. return ret;
  133. }
  134. ret = value;
  135. return ret;
  136. }
  137. static u16 esdhc_readw_fixup(struct sdhci_host *host,
  138. int spec_reg, u32 value)
  139. {
  140. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  141. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  142. u16 ret;
  143. int shift = (spec_reg & 0x2) * 8;
  144. if (spec_reg == SDHCI_HOST_VERSION)
  145. ret = value & 0xffff;
  146. else
  147. ret = (value >> shift) & 0xffff;
  148. /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
  149. * vendor version and spec version information.
  150. */
  151. if ((spec_reg == SDHCI_HOST_VERSION) &&
  152. (esdhc->quirk_incorrect_hostver))
  153. ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
  154. return ret;
  155. }
  156. static u8 esdhc_readb_fixup(struct sdhci_host *host,
  157. int spec_reg, u32 value)
  158. {
  159. u8 ret;
  160. u8 dma_bits;
  161. int shift = (spec_reg & 0x3) * 8;
  162. ret = (value >> shift) & 0xff;
  163. /*
  164. * "DMA select" locates at offset 0x28 in SD specification, but on
  165. * P5020 or P3041, it locates at 0x29.
  166. */
  167. if (spec_reg == SDHCI_HOST_CONTROL) {
  168. /* DMA select is 22,23 bits in Protocol Control Register */
  169. dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
  170. /* fixup the result */
  171. ret &= ~SDHCI_CTRL_DMA_MASK;
  172. ret |= dma_bits;
  173. }
  174. return ret;
  175. }
  176. /**
  177. * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
  178. * written into eSDHC register.
  179. *
  180. * @host: pointer to sdhci_host
  181. * @spec_reg: SD spec register address
  182. * @value: 8/16/32bit SD spec register value that would be written
  183. * @old_value: 32bit eSDHC register value on spec_reg address
  184. *
  185. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  186. * registers are 32 bits. There are differences in register size, register
  187. * address, register function, bit position and function between eSDHC spec
  188. * and SD spec.
  189. *
  190. * Return a fixed up register value
  191. */
  192. static u32 esdhc_writel_fixup(struct sdhci_host *host,
  193. int spec_reg, u32 value, u32 old_value)
  194. {
  195. u32 ret;
  196. /*
  197. * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
  198. * when SYSCTL[RSTD] is set for some special operations.
  199. * No any impact on other operation.
  200. */
  201. if (spec_reg == SDHCI_INT_ENABLE)
  202. ret = value | SDHCI_INT_BLK_GAP;
  203. else
  204. ret = value;
  205. return ret;
  206. }
  207. static u32 esdhc_writew_fixup(struct sdhci_host *host,
  208. int spec_reg, u16 value, u32 old_value)
  209. {
  210. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  211. int shift = (spec_reg & 0x2) * 8;
  212. u32 ret;
  213. switch (spec_reg) {
  214. case SDHCI_TRANSFER_MODE:
  215. /*
  216. * Postpone this write, we must do it together with a
  217. * command write that is down below. Return old value.
  218. */
  219. pltfm_host->xfer_mode_shadow = value;
  220. return old_value;
  221. case SDHCI_COMMAND:
  222. ret = (value << 16) | pltfm_host->xfer_mode_shadow;
  223. return ret;
  224. }
  225. ret = old_value & (~(0xffff << shift));
  226. ret |= (value << shift);
  227. if (spec_reg == SDHCI_BLOCK_SIZE) {
  228. /*
  229. * Two last DMA bits are reserved, and first one is used for
  230. * non-standard blksz of 4096 bytes that we don't support
  231. * yet. So clear the DMA boundary bits.
  232. */
  233. ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
  234. }
  235. return ret;
  236. }
  237. static u32 esdhc_writeb_fixup(struct sdhci_host *host,
  238. int spec_reg, u8 value, u32 old_value)
  239. {
  240. u32 ret;
  241. u32 dma_bits;
  242. u8 tmp;
  243. int shift = (spec_reg & 0x3) * 8;
  244. /*
  245. * eSDHC doesn't have a standard power control register, so we do
  246. * nothing here to avoid incorrect operation.
  247. */
  248. if (spec_reg == SDHCI_POWER_CONTROL)
  249. return old_value;
  250. /*
  251. * "DMA select" location is offset 0x28 in SD specification, but on
  252. * P5020 or P3041, it's located at 0x29.
  253. */
  254. if (spec_reg == SDHCI_HOST_CONTROL) {
  255. /*
  256. * If host control register is not standard, exit
  257. * this function
  258. */
  259. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
  260. return old_value;
  261. /* DMA select is 22,23 bits in Protocol Control Register */
  262. dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
  263. ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
  264. tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
  265. (old_value & SDHCI_CTRL_DMA_MASK);
  266. ret = (ret & (~0xff)) | tmp;
  267. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
  268. ret &= ~ESDHC_HOST_CONTROL_RES;
  269. return ret;
  270. }
  271. ret = (old_value & (~(0xff << shift))) | (value << shift);
  272. return ret;
  273. }
  274. static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
  275. {
  276. u32 ret;
  277. u32 value;
  278. if (reg == SDHCI_CAPABILITIES_1)
  279. value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
  280. else
  281. value = ioread32be(host->ioaddr + reg);
  282. ret = esdhc_readl_fixup(host, reg, value);
  283. return ret;
  284. }
  285. static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
  286. {
  287. u32 ret;
  288. u32 value;
  289. if (reg == SDHCI_CAPABILITIES_1)
  290. value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
  291. else
  292. value = ioread32(host->ioaddr + reg);
  293. ret = esdhc_readl_fixup(host, reg, value);
  294. return ret;
  295. }
  296. static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
  297. {
  298. u16 ret;
  299. u32 value;
  300. int base = reg & ~0x3;
  301. value = ioread32be(host->ioaddr + base);
  302. ret = esdhc_readw_fixup(host, reg, value);
  303. return ret;
  304. }
  305. static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
  306. {
  307. u16 ret;
  308. u32 value;
  309. int base = reg & ~0x3;
  310. value = ioread32(host->ioaddr + base);
  311. ret = esdhc_readw_fixup(host, reg, value);
  312. return ret;
  313. }
  314. static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
  315. {
  316. u8 ret;
  317. u32 value;
  318. int base = reg & ~0x3;
  319. value = ioread32be(host->ioaddr + base);
  320. ret = esdhc_readb_fixup(host, reg, value);
  321. return ret;
  322. }
  323. static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
  324. {
  325. u8 ret;
  326. u32 value;
  327. int base = reg & ~0x3;
  328. value = ioread32(host->ioaddr + base);
  329. ret = esdhc_readb_fixup(host, reg, value);
  330. return ret;
  331. }
  332. static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
  333. {
  334. u32 value;
  335. value = esdhc_writel_fixup(host, reg, val, 0);
  336. iowrite32be(value, host->ioaddr + reg);
  337. }
  338. static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
  339. {
  340. u32 value;
  341. value = esdhc_writel_fixup(host, reg, val, 0);
  342. iowrite32(value, host->ioaddr + reg);
  343. }
  344. static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
  345. {
  346. int base = reg & ~0x3;
  347. u32 value;
  348. u32 ret;
  349. value = ioread32be(host->ioaddr + base);
  350. ret = esdhc_writew_fixup(host, reg, val, value);
  351. if (reg != SDHCI_TRANSFER_MODE)
  352. iowrite32be(ret, host->ioaddr + base);
  353. }
  354. static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
  355. {
  356. int base = reg & ~0x3;
  357. u32 value;
  358. u32 ret;
  359. value = ioread32(host->ioaddr + base);
  360. ret = esdhc_writew_fixup(host, reg, val, value);
  361. if (reg != SDHCI_TRANSFER_MODE)
  362. iowrite32(ret, host->ioaddr + base);
  363. }
  364. static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
  365. {
  366. int base = reg & ~0x3;
  367. u32 value;
  368. u32 ret;
  369. value = ioread32be(host->ioaddr + base);
  370. ret = esdhc_writeb_fixup(host, reg, val, value);
  371. iowrite32be(ret, host->ioaddr + base);
  372. }
  373. static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
  374. {
  375. int base = reg & ~0x3;
  376. u32 value;
  377. u32 ret;
  378. value = ioread32(host->ioaddr + base);
  379. ret = esdhc_writeb_fixup(host, reg, val, value);
  380. iowrite32(ret, host->ioaddr + base);
  381. }
  382. /*
  383. * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
  384. * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
  385. * and Block Gap Event(IRQSTAT[BGE]) are also set.
  386. * For Continue, apply soft reset for data(SYSCTL[RSTD]);
  387. * and re-issue the entire read transaction from beginning.
  388. */
  389. static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
  390. {
  391. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  392. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  393. bool applicable;
  394. dma_addr_t dmastart;
  395. dma_addr_t dmanow;
  396. applicable = (intmask & SDHCI_INT_DATA_END) &&
  397. (intmask & SDHCI_INT_BLK_GAP) &&
  398. (esdhc->vendor_ver == VENDOR_V_23);
  399. if (!applicable)
  400. return;
  401. host->data->error = 0;
  402. dmastart = sg_dma_address(host->data->sg);
  403. dmanow = dmastart + host->data->bytes_xfered;
  404. /*
  405. * Force update to the next DMA block boundary.
  406. */
  407. dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  408. SDHCI_DEFAULT_BOUNDARY_SIZE;
  409. host->data->bytes_xfered = dmanow - dmastart;
  410. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  411. }
  412. static int esdhc_of_enable_dma(struct sdhci_host *host)
  413. {
  414. u32 value;
  415. struct device *dev = mmc_dev(host->mmc);
  416. if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
  417. of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
  418. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  419. value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  420. value |= ESDHC_DMA_SNOOP;
  421. sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
  422. return 0;
  423. }
  424. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  425. {
  426. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  427. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  428. if (esdhc->peripheral_clock)
  429. return esdhc->peripheral_clock;
  430. else
  431. return pltfm_host->clock;
  432. }
  433. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  434. {
  435. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  436. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  437. unsigned int clock;
  438. if (esdhc->peripheral_clock)
  439. clock = esdhc->peripheral_clock;
  440. else
  441. clock = pltfm_host->clock;
  442. return clock / 256 / 16;
  443. }
  444. static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
  445. {
  446. u32 val;
  447. ktime_t timeout;
  448. val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  449. if (enable)
  450. val |= ESDHC_CLOCK_SDCLKEN;
  451. else
  452. val &= ~ESDHC_CLOCK_SDCLKEN;
  453. sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
  454. /* Wait max 20 ms */
  455. timeout = ktime_add_ms(ktime_get(), 20);
  456. val = ESDHC_CLOCK_STABLE;
  457. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) {
  458. if (ktime_after(ktime_get(), timeout)) {
  459. pr_err("%s: Internal clock never stabilised.\n",
  460. mmc_hostname(host->mmc));
  461. break;
  462. }
  463. udelay(10);
  464. }
  465. }
  466. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  467. {
  468. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  469. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  470. int pre_div = 1;
  471. int div = 1;
  472. ktime_t timeout;
  473. long fixup = 0;
  474. u32 temp;
  475. host->mmc->actual_clock = 0;
  476. if (clock == 0) {
  477. esdhc_clock_enable(host, false);
  478. return;
  479. }
  480. /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
  481. if (esdhc->vendor_ver < VENDOR_V_23)
  482. pre_div = 2;
  483. if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
  484. esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
  485. fixup = esdhc->clk_fixup->sd_dflt_max_clk;
  486. else if (esdhc->clk_fixup)
  487. fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
  488. if (fixup && clock > fixup)
  489. clock = fixup;
  490. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  491. temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
  492. ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
  493. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  494. while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
  495. pre_div *= 2;
  496. while (host->max_clk / pre_div / div > clock && div < 16)
  497. div++;
  498. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  499. clock, host->max_clk / pre_div / div);
  500. host->mmc->actual_clock = host->max_clk / pre_div / div;
  501. esdhc->div_ratio = pre_div * div;
  502. pre_div >>= 1;
  503. div--;
  504. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  505. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  506. | (div << ESDHC_DIVIDER_SHIFT)
  507. | (pre_div << ESDHC_PREDIV_SHIFT));
  508. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  509. /* Wait max 20 ms */
  510. timeout = ktime_add_ms(ktime_get(), 20);
  511. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
  512. if (ktime_after(ktime_get(), timeout)) {
  513. pr_err("%s: Internal clock never stabilised.\n",
  514. mmc_hostname(host->mmc));
  515. return;
  516. }
  517. udelay(10);
  518. }
  519. temp |= ESDHC_CLOCK_SDCLKEN;
  520. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  521. }
  522. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  523. {
  524. u32 ctrl;
  525. ctrl = sdhci_readl(host, ESDHC_PROCTL);
  526. ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
  527. switch (width) {
  528. case MMC_BUS_WIDTH_8:
  529. ctrl |= ESDHC_CTRL_8BITBUS;
  530. break;
  531. case MMC_BUS_WIDTH_4:
  532. ctrl |= ESDHC_CTRL_4BITBUS;
  533. break;
  534. default:
  535. break;
  536. }
  537. sdhci_writel(host, ctrl, ESDHC_PROCTL);
  538. }
  539. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  540. {
  541. u32 val;
  542. sdhci_reset(host, mask);
  543. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  544. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  545. if (mask & SDHCI_RESET_ALL) {
  546. val = sdhci_readl(host, ESDHC_TBCTL);
  547. val &= ~ESDHC_TB_EN;
  548. sdhci_writel(host, val, ESDHC_TBCTL);
  549. }
  550. }
  551. /* The SCFG, Supplemental Configuration Unit, provides SoC specific
  552. * configuration and status registers for the device. There is a
  553. * SDHC IO VSEL control register on SCFG for some platforms. It's
  554. * used to support SDHC IO voltage switching.
  555. */
  556. static const struct of_device_id scfg_device_ids[] = {
  557. { .compatible = "fsl,t1040-scfg", },
  558. { .compatible = "fsl,ls1012a-scfg", },
  559. { .compatible = "fsl,ls1046a-scfg", },
  560. {}
  561. };
  562. /* SDHC IO VSEL control register definition */
  563. #define SCFG_SDHCIOVSELCR 0x408
  564. #define SDHCIOVSELCR_TGLEN 0x80000000
  565. #define SDHCIOVSELCR_VSELVAL 0x60000000
  566. #define SDHCIOVSELCR_SDHC_VS 0x00000001
  567. static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
  568. struct mmc_ios *ios)
  569. {
  570. struct sdhci_host *host = mmc_priv(mmc);
  571. struct device_node *scfg_node;
  572. void __iomem *scfg_base = NULL;
  573. u32 sdhciovselcr;
  574. u32 val;
  575. /*
  576. * Signal Voltage Switching is only applicable for Host Controllers
  577. * v3.00 and above.
  578. */
  579. if (host->version < SDHCI_SPEC_300)
  580. return 0;
  581. val = sdhci_readl(host, ESDHC_PROCTL);
  582. switch (ios->signal_voltage) {
  583. case MMC_SIGNAL_VOLTAGE_330:
  584. val &= ~ESDHC_VOLT_SEL;
  585. sdhci_writel(host, val, ESDHC_PROCTL);
  586. return 0;
  587. case MMC_SIGNAL_VOLTAGE_180:
  588. scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  589. if (scfg_node)
  590. scfg_base = of_iomap(scfg_node, 0);
  591. if (scfg_base) {
  592. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  593. SDHCIOVSELCR_VSELVAL;
  594. iowrite32be(sdhciovselcr,
  595. scfg_base + SCFG_SDHCIOVSELCR);
  596. val |= ESDHC_VOLT_SEL;
  597. sdhci_writel(host, val, ESDHC_PROCTL);
  598. mdelay(5);
  599. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  600. SDHCIOVSELCR_SDHC_VS;
  601. iowrite32be(sdhciovselcr,
  602. scfg_base + SCFG_SDHCIOVSELCR);
  603. iounmap(scfg_base);
  604. } else {
  605. val |= ESDHC_VOLT_SEL;
  606. sdhci_writel(host, val, ESDHC_PROCTL);
  607. }
  608. return 0;
  609. default:
  610. return 0;
  611. }
  612. }
  613. static struct soc_device_attribute soc_fixup_tuning[] = {
  614. { .family = "QorIQ T1040", .revision = "1.0", },
  615. { .family = "QorIQ T2080", .revision = "1.0", },
  616. { .family = "QorIQ T1023", .revision = "1.0", },
  617. { .family = "QorIQ LS1021A", .revision = "1.0", },
  618. { .family = "QorIQ LS1080A", .revision = "1.0", },
  619. { .family = "QorIQ LS2080A", .revision = "1.0", },
  620. { .family = "QorIQ LS1012A", .revision = "1.0", },
  621. { .family = "QorIQ LS1043A", .revision = "1.*", },
  622. { .family = "QorIQ LS1046A", .revision = "1.0", },
  623. { },
  624. };
  625. static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  626. {
  627. struct sdhci_host *host = mmc_priv(mmc);
  628. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  629. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  630. u32 val;
  631. /* Use tuning block for tuning procedure */
  632. esdhc_clock_enable(host, false);
  633. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  634. val |= ESDHC_FLUSH_ASYNC_FIFO;
  635. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  636. val = sdhci_readl(host, ESDHC_TBCTL);
  637. val |= ESDHC_TB_EN;
  638. sdhci_writel(host, val, ESDHC_TBCTL);
  639. esdhc_clock_enable(host, true);
  640. sdhci_execute_tuning(mmc, opcode);
  641. if (host->tuning_err == -EAGAIN && esdhc->quirk_fixup_tuning) {
  642. /* program TBPTR[TB_WNDW_END_PTR] = 3*DIV_RATIO and
  643. * program TBPTR[TB_WNDW_START_PTR] = 5*DIV_RATIO
  644. */
  645. val = sdhci_readl(host, ESDHC_TBPTR);
  646. val = (val & ~((0x7f << 8) | 0x7f)) |
  647. (3 * esdhc->div_ratio) | ((5 * esdhc->div_ratio) << 8);
  648. sdhci_writel(host, val, ESDHC_TBPTR);
  649. /* program the software tuning mode by setting
  650. * TBCTL[TB_MODE]=2'h3
  651. */
  652. val = sdhci_readl(host, ESDHC_TBCTL);
  653. val |= 0x3;
  654. sdhci_writel(host, val, ESDHC_TBCTL);
  655. sdhci_execute_tuning(mmc, opcode);
  656. }
  657. return 0;
  658. }
  659. #ifdef CONFIG_PM_SLEEP
  660. static u32 esdhc_proctl;
  661. static int esdhc_of_suspend(struct device *dev)
  662. {
  663. struct sdhci_host *host = dev_get_drvdata(dev);
  664. esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
  665. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  666. mmc_retune_needed(host->mmc);
  667. return sdhci_suspend_host(host);
  668. }
  669. static int esdhc_of_resume(struct device *dev)
  670. {
  671. struct sdhci_host *host = dev_get_drvdata(dev);
  672. int ret = sdhci_resume_host(host);
  673. if (ret == 0) {
  674. /* Isn't this already done by sdhci_resume_host() ? --rmk */
  675. esdhc_of_enable_dma(host);
  676. sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  677. }
  678. return ret;
  679. }
  680. #endif
  681. static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
  682. esdhc_of_suspend,
  683. esdhc_of_resume);
  684. static const struct sdhci_ops sdhci_esdhc_be_ops = {
  685. .read_l = esdhc_be_readl,
  686. .read_w = esdhc_be_readw,
  687. .read_b = esdhc_be_readb,
  688. .write_l = esdhc_be_writel,
  689. .write_w = esdhc_be_writew,
  690. .write_b = esdhc_be_writeb,
  691. .set_clock = esdhc_of_set_clock,
  692. .enable_dma = esdhc_of_enable_dma,
  693. .get_max_clock = esdhc_of_get_max_clock,
  694. .get_min_clock = esdhc_of_get_min_clock,
  695. .adma_workaround = esdhc_of_adma_workaround,
  696. .set_bus_width = esdhc_pltfm_set_bus_width,
  697. .reset = esdhc_reset,
  698. .set_uhs_signaling = sdhci_set_uhs_signaling,
  699. };
  700. static const struct sdhci_ops sdhci_esdhc_le_ops = {
  701. .read_l = esdhc_le_readl,
  702. .read_w = esdhc_le_readw,
  703. .read_b = esdhc_le_readb,
  704. .write_l = esdhc_le_writel,
  705. .write_w = esdhc_le_writew,
  706. .write_b = esdhc_le_writeb,
  707. .set_clock = esdhc_of_set_clock,
  708. .enable_dma = esdhc_of_enable_dma,
  709. .get_max_clock = esdhc_of_get_max_clock,
  710. .get_min_clock = esdhc_of_get_min_clock,
  711. .adma_workaround = esdhc_of_adma_workaround,
  712. .set_bus_width = esdhc_pltfm_set_bus_width,
  713. .reset = esdhc_reset,
  714. .set_uhs_signaling = sdhci_set_uhs_signaling,
  715. };
  716. static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
  717. .quirks = ESDHC_DEFAULT_QUIRKS |
  718. #ifdef CONFIG_PPC
  719. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  720. #endif
  721. SDHCI_QUIRK_NO_CARD_NO_RESET |
  722. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  723. .ops = &sdhci_esdhc_be_ops,
  724. };
  725. static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
  726. .quirks = ESDHC_DEFAULT_QUIRKS |
  727. SDHCI_QUIRK_NO_CARD_NO_RESET |
  728. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  729. .ops = &sdhci_esdhc_le_ops,
  730. };
  731. static struct soc_device_attribute soc_incorrect_hostver[] = {
  732. { .family = "QorIQ T4240", .revision = "1.0", },
  733. { .family = "QorIQ T4240", .revision = "2.0", },
  734. { },
  735. };
  736. static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
  737. {
  738. const struct of_device_id *match;
  739. struct sdhci_pltfm_host *pltfm_host;
  740. struct sdhci_esdhc *esdhc;
  741. struct device_node *np;
  742. struct clk *clk;
  743. u32 val;
  744. u16 host_ver;
  745. pltfm_host = sdhci_priv(host);
  746. esdhc = sdhci_pltfm_priv(pltfm_host);
  747. host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
  748. esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
  749. SDHCI_VENDOR_VER_SHIFT;
  750. esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
  751. if (soc_device_match(soc_incorrect_hostver))
  752. esdhc->quirk_incorrect_hostver = true;
  753. else
  754. esdhc->quirk_incorrect_hostver = false;
  755. match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
  756. if (match)
  757. esdhc->clk_fixup = match->data;
  758. np = pdev->dev.of_node;
  759. clk = of_clk_get(np, 0);
  760. if (!IS_ERR(clk)) {
  761. /*
  762. * esdhc->peripheral_clock would be assigned with a value
  763. * which is eSDHC base clock when use periperal clock.
  764. * For ls1046a, the clock value got by common clk API is
  765. * peripheral clock while the eSDHC base clock is 1/2
  766. * peripheral clock.
  767. */
  768. if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
  769. esdhc->peripheral_clock = clk_get_rate(clk) / 2;
  770. else
  771. esdhc->peripheral_clock = clk_get_rate(clk);
  772. clk_put(clk);
  773. }
  774. if (esdhc->peripheral_clock) {
  775. esdhc_clock_enable(host, false);
  776. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  777. val |= ESDHC_PERIPHERAL_CLK_SEL;
  778. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  779. esdhc_clock_enable(host, true);
  780. }
  781. }
  782. static int sdhci_esdhc_probe(struct platform_device *pdev)
  783. {
  784. struct sdhci_host *host;
  785. struct device_node *np;
  786. struct sdhci_pltfm_host *pltfm_host;
  787. struct sdhci_esdhc *esdhc;
  788. int ret;
  789. np = pdev->dev.of_node;
  790. if (of_property_read_bool(np, "little-endian"))
  791. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
  792. sizeof(struct sdhci_esdhc));
  793. else
  794. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
  795. sizeof(struct sdhci_esdhc));
  796. if (IS_ERR(host))
  797. return PTR_ERR(host);
  798. host->mmc_host_ops.start_signal_voltage_switch =
  799. esdhc_signal_voltage_switch;
  800. host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
  801. host->tuning_delay = 1;
  802. esdhc_init(pdev, host);
  803. sdhci_get_of_property(pdev);
  804. pltfm_host = sdhci_priv(host);
  805. esdhc = sdhci_pltfm_priv(pltfm_host);
  806. if (soc_device_match(soc_fixup_tuning))
  807. esdhc->quirk_fixup_tuning = true;
  808. else
  809. esdhc->quirk_fixup_tuning = false;
  810. if (esdhc->vendor_ver == VENDOR_V_22)
  811. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  812. if (esdhc->vendor_ver > VENDOR_V_22)
  813. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  814. if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
  815. of_device_is_compatible(np, "fsl,p5020-esdhc") ||
  816. of_device_is_compatible(np, "fsl,p4080-esdhc") ||
  817. of_device_is_compatible(np, "fsl,p1020-esdhc") ||
  818. of_device_is_compatible(np, "fsl,t1040-esdhc"))
  819. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  820. if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
  821. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  822. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  823. /*
  824. * Freescale messed up with P2020 as it has a non-standard
  825. * host control register
  826. */
  827. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  828. }
  829. /* call to generic mmc_of_parse to support additional capabilities */
  830. ret = mmc_of_parse(host->mmc);
  831. if (ret)
  832. goto err;
  833. mmc_of_parse_voltage(np, &host->ocr_mask);
  834. ret = sdhci_add_host(host);
  835. if (ret)
  836. goto err;
  837. return 0;
  838. err:
  839. sdhci_pltfm_free(pdev);
  840. return ret;
  841. }
  842. static struct platform_driver sdhci_esdhc_driver = {
  843. .driver = {
  844. .name = "sdhci-esdhc",
  845. .of_match_table = sdhci_esdhc_of_match,
  846. .pm = &esdhc_of_dev_pm_ops,
  847. },
  848. .probe = sdhci_esdhc_probe,
  849. .remove = sdhci_pltfm_unregister,
  850. };
  851. module_platform_driver(sdhci_esdhc_driver);
  852. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  853. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  854. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  855. MODULE_LICENSE("GPL v2");