sdhci-esdhc-imx.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. * Freescale eSDHC i.MX controller driver for the platform bus.
  3. *
  4. * derived from the OF-version.
  5. *
  6. * Copyright (c) 2010 Pengutronix e.K.
  7. * Author: Wolfram Sang <kernel@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/mmc/host.h>
  21. #include <linux/mmc/mmc.h>
  22. #include <linux/mmc/sdio.h>
  23. #include <linux/mmc/slot-gpio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/platform_data/mmc-esdhc-imx.h>
  29. #include <linux/pm_runtime.h>
  30. #include "sdhci-pltfm.h"
  31. #include "sdhci-esdhc.h"
  32. #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
  33. #define ESDHC_CTRL_D3CD 0x08
  34. #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
  35. /* VENDOR SPEC register */
  36. #define ESDHC_VENDOR_SPEC 0xc0
  37. #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
  38. #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
  39. #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
  40. #define ESDHC_WTMK_LVL 0x44
  41. #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
  42. #define ESDHC_MIX_CTRL 0x48
  43. #define ESDHC_MIX_CTRL_DDREN (1 << 3)
  44. #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
  45. #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
  46. #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
  47. #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
  48. #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
  49. #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
  50. /* Bits 3 and 6 are not SDHCI standard definitions */
  51. #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
  52. /* Tuning bits */
  53. #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
  54. /* dll control register */
  55. #define ESDHC_DLL_CTRL 0x60
  56. #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
  57. #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
  58. /* tune control register */
  59. #define ESDHC_TUNE_CTRL_STATUS 0x68
  60. #define ESDHC_TUNE_CTRL_STEP 1
  61. #define ESDHC_TUNE_CTRL_MIN 0
  62. #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
  63. /* strobe dll register */
  64. #define ESDHC_STROBE_DLL_CTRL 0x70
  65. #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
  66. #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
  67. #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
  68. #define ESDHC_STROBE_DLL_STATUS 0x74
  69. #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
  70. #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
  71. #define ESDHC_TUNING_CTRL 0xcc
  72. #define ESDHC_STD_TUNING_EN (1 << 24)
  73. /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
  74. #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
  75. #define ESDHC_TUNING_START_TAP_MASK 0xff
  76. #define ESDHC_TUNING_STEP_MASK 0x00070000
  77. #define ESDHC_TUNING_STEP_SHIFT 16
  78. /* pinctrl state */
  79. #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
  80. #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
  81. /*
  82. * Our interpretation of the SDHCI_HOST_CONTROL register
  83. */
  84. #define ESDHC_CTRL_4BITBUS (0x1 << 1)
  85. #define ESDHC_CTRL_8BITBUS (0x2 << 1)
  86. #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
  87. /*
  88. * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
  89. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  90. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  91. * Define this macro DMA error INT for fsl eSDHC
  92. */
  93. #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
  94. /*
  95. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  96. * "11" when the STOP CMD12 is issued on imx53 to abort one
  97. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  98. * be generated.
  99. * In exact block transfer, the controller doesn't complete the
  100. * operations automatically as required at the end of the
  101. * transfer and remains on hold if the abort command is not sent.
  102. * As a result, the TC flag is not asserted and SW received timeout
  103. * exception. Bit1 of Vendor Spec register is used to fix it.
  104. */
  105. #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
  106. /*
  107. * The flag tells that the ESDHC controller is an USDHC block that is
  108. * integrated on the i.MX6 series.
  109. */
  110. #define ESDHC_FLAG_USDHC BIT(3)
  111. /* The IP supports manual tuning process */
  112. #define ESDHC_FLAG_MAN_TUNING BIT(4)
  113. /* The IP supports standard tuning process */
  114. #define ESDHC_FLAG_STD_TUNING BIT(5)
  115. /* The IP has SDHCI_CAPABILITIES_1 register */
  116. #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
  117. /*
  118. * The IP has erratum ERR004536
  119. * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
  120. * when reading data from the card
  121. * This flag is also set for i.MX25 and i.MX35 in order to get
  122. * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
  123. */
  124. #define ESDHC_FLAG_ERR004536 BIT(7)
  125. /* The IP supports HS200 mode */
  126. #define ESDHC_FLAG_HS200 BIT(8)
  127. /* The IP supports HS400 mode */
  128. #define ESDHC_FLAG_HS400 BIT(9)
  129. /* A clock frequency higher than this rate requires strobe dll control */
  130. #define ESDHC_STROBE_DLL_CLK_FREQ 100000000
  131. struct esdhc_soc_data {
  132. u32 flags;
  133. };
  134. static struct esdhc_soc_data esdhc_imx25_data = {
  135. .flags = ESDHC_FLAG_ERR004536,
  136. };
  137. static struct esdhc_soc_data esdhc_imx35_data = {
  138. .flags = ESDHC_FLAG_ERR004536,
  139. };
  140. static struct esdhc_soc_data esdhc_imx51_data = {
  141. .flags = 0,
  142. };
  143. static struct esdhc_soc_data esdhc_imx53_data = {
  144. .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
  145. };
  146. static struct esdhc_soc_data usdhc_imx6q_data = {
  147. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
  148. };
  149. static struct esdhc_soc_data usdhc_imx6sl_data = {
  150. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  151. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
  152. | ESDHC_FLAG_HS200,
  153. };
  154. static struct esdhc_soc_data usdhc_imx6sx_data = {
  155. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  156. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
  157. };
  158. static struct esdhc_soc_data usdhc_imx7d_data = {
  159. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  160. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  161. | ESDHC_FLAG_HS400,
  162. };
  163. struct pltfm_imx_data {
  164. u32 scratchpad;
  165. struct pinctrl *pinctrl;
  166. struct pinctrl_state *pins_default;
  167. struct pinctrl_state *pins_100mhz;
  168. struct pinctrl_state *pins_200mhz;
  169. const struct esdhc_soc_data *socdata;
  170. struct esdhc_platform_data boarddata;
  171. struct clk *clk_ipg;
  172. struct clk *clk_ahb;
  173. struct clk *clk_per;
  174. unsigned int actual_clock;
  175. enum {
  176. NO_CMD_PENDING, /* no multiblock command pending */
  177. MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
  178. WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
  179. } multiblock_status;
  180. u32 is_ddr;
  181. };
  182. static const struct platform_device_id imx_esdhc_devtype[] = {
  183. {
  184. .name = "sdhci-esdhc-imx25",
  185. .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
  186. }, {
  187. .name = "sdhci-esdhc-imx35",
  188. .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
  189. }, {
  190. .name = "sdhci-esdhc-imx51",
  191. .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
  192. }, {
  193. /* sentinel */
  194. }
  195. };
  196. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  197. static const struct of_device_id imx_esdhc_dt_ids[] = {
  198. { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
  199. { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
  200. { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
  201. { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
  202. { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
  203. { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
  204. { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
  205. { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
  206. { /* sentinel */ }
  207. };
  208. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  209. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  210. {
  211. return data->socdata == &esdhc_imx25_data;
  212. }
  213. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  214. {
  215. return data->socdata == &esdhc_imx53_data;
  216. }
  217. static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
  218. {
  219. return data->socdata == &usdhc_imx6q_data;
  220. }
  221. static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
  222. {
  223. return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
  224. }
  225. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  226. {
  227. void __iomem *base = host->ioaddr + (reg & ~0x3);
  228. u32 shift = (reg & 0x3) * 8;
  229. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  230. }
  231. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  232. {
  233. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  234. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  235. u32 val = readl(host->ioaddr + reg);
  236. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  237. u32 fsl_prss = val;
  238. /* save the least 20 bits */
  239. val = fsl_prss & 0x000FFFFF;
  240. /* move dat[0-3] bits */
  241. val |= (fsl_prss & 0x0F000000) >> 4;
  242. /* move cmd line bit */
  243. val |= (fsl_prss & 0x00800000) << 1;
  244. }
  245. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  246. /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
  247. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  248. val &= 0xffff0000;
  249. /* In FSL esdhc IC module, only bit20 is used to indicate the
  250. * ADMA2 capability of esdhc, but this bit is messed up on
  251. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  252. * don't actually support ADMA2). So set the BROKEN_ADMA
  253. * quirk on MX25/35 platforms.
  254. */
  255. if (val & SDHCI_CAN_DO_ADMA1) {
  256. val &= ~SDHCI_CAN_DO_ADMA1;
  257. val |= SDHCI_CAN_DO_ADMA2;
  258. }
  259. }
  260. if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
  261. if (esdhc_is_usdhc(imx_data)) {
  262. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  263. val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
  264. else
  265. /* imx6q/dl does not have cap_1 register, fake one */
  266. val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
  267. | SDHCI_SUPPORT_SDR50
  268. | SDHCI_USE_SDR50_TUNING
  269. | (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
  270. if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
  271. val |= SDHCI_SUPPORT_HS400;
  272. }
  273. }
  274. if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
  275. val = 0;
  276. val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
  277. val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
  278. val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
  279. }
  280. if (unlikely(reg == SDHCI_INT_STATUS)) {
  281. if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
  282. val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  283. val |= SDHCI_INT_ADMA_ERROR;
  284. }
  285. /*
  286. * mask off the interrupt we get in response to the manually
  287. * sent CMD12
  288. */
  289. if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
  290. ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
  291. val &= ~SDHCI_INT_RESPONSE;
  292. writel(SDHCI_INT_RESPONSE, host->ioaddr +
  293. SDHCI_INT_STATUS);
  294. imx_data->multiblock_status = NO_CMD_PENDING;
  295. }
  296. }
  297. return val;
  298. }
  299. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  300. {
  301. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  302. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  303. u32 data;
  304. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
  305. reg == SDHCI_INT_STATUS)) {
  306. if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
  307. /*
  308. * Clear and then set D3CD bit to avoid missing the
  309. * card interrupt. This is an eSDHC controller problem
  310. * so we need to apply the following workaround: clear
  311. * and set D3CD bit will make eSDHC re-sample the card
  312. * interrupt. In case a card interrupt was lost,
  313. * re-sample it by the following steps.
  314. */
  315. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  316. data &= ~ESDHC_CTRL_D3CD;
  317. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  318. data |= ESDHC_CTRL_D3CD;
  319. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  320. }
  321. if (val & SDHCI_INT_ADMA_ERROR) {
  322. val &= ~SDHCI_INT_ADMA_ERROR;
  323. val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  324. }
  325. }
  326. if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  327. && (reg == SDHCI_INT_STATUS)
  328. && (val & SDHCI_INT_DATA_END))) {
  329. u32 v;
  330. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  331. v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  332. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  333. if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
  334. {
  335. /* send a manual CMD12 with RESPTYP=none */
  336. data = MMC_STOP_TRANSMISSION << 24 |
  337. SDHCI_CMD_ABORTCMD << 16;
  338. writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
  339. imx_data->multiblock_status = WAIT_FOR_INT;
  340. }
  341. }
  342. writel(val, host->ioaddr + reg);
  343. }
  344. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  345. {
  346. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  347. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  348. u16 ret = 0;
  349. u32 val;
  350. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  351. reg ^= 2;
  352. if (esdhc_is_usdhc(imx_data)) {
  353. /*
  354. * The usdhc register returns a wrong host version.
  355. * Correct it here.
  356. */
  357. return SDHCI_SPEC_300;
  358. }
  359. }
  360. if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
  361. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  362. if (val & ESDHC_VENDOR_SPEC_VSELECT)
  363. ret |= SDHCI_CTRL_VDD_180;
  364. if (esdhc_is_usdhc(imx_data)) {
  365. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  366. val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  367. else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
  368. /* the std tuning bits is in ACMD12_ERR for imx6sl */
  369. val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  370. }
  371. if (val & ESDHC_MIX_CTRL_EXE_TUNE)
  372. ret |= SDHCI_CTRL_EXEC_TUNING;
  373. if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
  374. ret |= SDHCI_CTRL_TUNED_CLK;
  375. ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  376. return ret;
  377. }
  378. if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
  379. if (esdhc_is_usdhc(imx_data)) {
  380. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  381. ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
  382. /* Swap AC23 bit */
  383. if (m & ESDHC_MIX_CTRL_AC23EN) {
  384. ret &= ~ESDHC_MIX_CTRL_AC23EN;
  385. ret |= SDHCI_TRNS_AUTO_CMD23;
  386. }
  387. } else {
  388. ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
  389. }
  390. return ret;
  391. }
  392. return readw(host->ioaddr + reg);
  393. }
  394. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  395. {
  396. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  397. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  398. u32 new_val = 0;
  399. switch (reg) {
  400. case SDHCI_CLOCK_CONTROL:
  401. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  402. if (val & SDHCI_CLOCK_CARD_EN)
  403. new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  404. else
  405. new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  406. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  407. return;
  408. case SDHCI_HOST_CONTROL2:
  409. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  410. if (val & SDHCI_CTRL_VDD_180)
  411. new_val |= ESDHC_VENDOR_SPEC_VSELECT;
  412. else
  413. new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
  414. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  415. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  416. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  417. if (val & SDHCI_CTRL_TUNED_CLK) {
  418. new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  419. new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  420. } else {
  421. new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  422. new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  423. }
  424. writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
  425. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  426. u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  427. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  428. if (val & SDHCI_CTRL_TUNED_CLK) {
  429. v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  430. } else {
  431. v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  432. m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  433. m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  434. }
  435. if (val & SDHCI_CTRL_EXEC_TUNING) {
  436. v |= ESDHC_MIX_CTRL_EXE_TUNE;
  437. m |= ESDHC_MIX_CTRL_FBCLK_SEL;
  438. m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  439. } else {
  440. v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  441. }
  442. writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
  443. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  444. }
  445. return;
  446. case SDHCI_TRANSFER_MODE:
  447. if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  448. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  449. && (host->cmd->data->blocks > 1)
  450. && (host->cmd->data->flags & MMC_DATA_READ)) {
  451. u32 v;
  452. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  453. v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  454. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  455. }
  456. if (esdhc_is_usdhc(imx_data)) {
  457. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  458. /* Swap AC23 bit */
  459. if (val & SDHCI_TRNS_AUTO_CMD23) {
  460. val &= ~SDHCI_TRNS_AUTO_CMD23;
  461. val |= ESDHC_MIX_CTRL_AC23EN;
  462. }
  463. m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
  464. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  465. } else {
  466. /*
  467. * Postpone this write, we must do it together with a
  468. * command write that is down below.
  469. */
  470. imx_data->scratchpad = val;
  471. }
  472. return;
  473. case SDHCI_COMMAND:
  474. if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
  475. val |= SDHCI_CMD_ABORTCMD;
  476. if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  477. (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  478. imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
  479. if (esdhc_is_usdhc(imx_data))
  480. writel(val << 16,
  481. host->ioaddr + SDHCI_TRANSFER_MODE);
  482. else
  483. writel(val << 16 | imx_data->scratchpad,
  484. host->ioaddr + SDHCI_TRANSFER_MODE);
  485. return;
  486. case SDHCI_BLOCK_SIZE:
  487. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  488. break;
  489. }
  490. esdhc_clrset_le(host, 0xffff, val, reg);
  491. }
  492. static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
  493. {
  494. u8 ret;
  495. u32 val;
  496. switch (reg) {
  497. case SDHCI_HOST_CONTROL:
  498. val = readl(host->ioaddr + reg);
  499. ret = val & SDHCI_CTRL_LED;
  500. ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
  501. ret |= (val & ESDHC_CTRL_4BITBUS);
  502. ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
  503. return ret;
  504. }
  505. return readb(host->ioaddr + reg);
  506. }
  507. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  508. {
  509. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  510. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  511. u32 new_val = 0;
  512. u32 mask;
  513. switch (reg) {
  514. case SDHCI_POWER_CONTROL:
  515. /*
  516. * FSL put some DMA bits here
  517. * If your board has a regulator, code should be here
  518. */
  519. return;
  520. case SDHCI_HOST_CONTROL:
  521. /* FSL messed up here, so we need to manually compose it. */
  522. new_val = val & SDHCI_CTRL_LED;
  523. /* ensure the endianness */
  524. new_val |= ESDHC_HOST_CONTROL_LE;
  525. /* bits 8&9 are reserved on mx25 */
  526. if (!is_imx25_esdhc(imx_data)) {
  527. /* DMA mode bits are shifted */
  528. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  529. }
  530. /*
  531. * Do not touch buswidth bits here. This is done in
  532. * esdhc_pltfm_bus_width.
  533. * Do not touch the D3CD bit either which is used for the
  534. * SDIO interrupt erratum workaround.
  535. */
  536. mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
  537. esdhc_clrset_le(host, mask, new_val, reg);
  538. return;
  539. case SDHCI_SOFTWARE_RESET:
  540. if (val & SDHCI_RESET_DATA)
  541. new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  542. break;
  543. }
  544. esdhc_clrset_le(host, 0xff, val, reg);
  545. if (reg == SDHCI_SOFTWARE_RESET) {
  546. if (val & SDHCI_RESET_ALL) {
  547. /*
  548. * The esdhc has a design violation to SDHC spec which
  549. * tells that software reset should not affect card
  550. * detection circuit. But esdhc clears its SYSCTL
  551. * register bits [0..2] during the software reset. This
  552. * will stop those clocks that card detection circuit
  553. * relies on. To work around it, we turn the clocks on
  554. * back to keep card detection circuit functional.
  555. */
  556. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  557. /*
  558. * The reset on usdhc fails to clear MIX_CTRL register.
  559. * Do it manually here.
  560. */
  561. if (esdhc_is_usdhc(imx_data)) {
  562. /*
  563. * the tuning bits should be kept during reset
  564. */
  565. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  566. writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
  567. host->ioaddr + ESDHC_MIX_CTRL);
  568. imx_data->is_ddr = 0;
  569. }
  570. } else if (val & SDHCI_RESET_DATA) {
  571. /*
  572. * The eSDHC DAT line software reset clears at least the
  573. * data transfer width on i.MX25, so make sure that the
  574. * Host Control register is unaffected.
  575. */
  576. esdhc_clrset_le(host, 0xff, new_val,
  577. SDHCI_HOST_CONTROL);
  578. }
  579. }
  580. }
  581. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  582. {
  583. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  584. return pltfm_host->clock;
  585. }
  586. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  587. {
  588. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  589. return pltfm_host->clock / 256 / 16;
  590. }
  591. static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
  592. unsigned int clock)
  593. {
  594. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  595. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  596. unsigned int host_clock = pltfm_host->clock;
  597. int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
  598. int pre_div = 1;
  599. int div = 1;
  600. u32 temp, val;
  601. if (clock == 0) {
  602. host->mmc->actual_clock = 0;
  603. if (esdhc_is_usdhc(imx_data)) {
  604. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  605. writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  606. host->ioaddr + ESDHC_VENDOR_SPEC);
  607. }
  608. return;
  609. }
  610. /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
  611. if (is_imx53_esdhc(imx_data)) {
  612. /*
  613. * According to the i.MX53 reference manual, if DLLCTRL[10] can
  614. * be set, then the controller is eSDHCv3, else it is eSDHCv2.
  615. */
  616. val = readl(host->ioaddr + ESDHC_DLL_CTRL);
  617. writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
  618. temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
  619. writel(val, host->ioaddr + ESDHC_DLL_CTRL);
  620. if (temp & BIT(10))
  621. pre_div = 2;
  622. }
  623. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  624. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  625. | ESDHC_CLOCK_MASK);
  626. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  627. while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
  628. pre_div < 256)
  629. pre_div *= 2;
  630. while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
  631. div++;
  632. host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
  633. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  634. clock, host->mmc->actual_clock);
  635. pre_div >>= 1;
  636. div--;
  637. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  638. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  639. | (div << ESDHC_DIVIDER_SHIFT)
  640. | (pre_div << ESDHC_PREDIV_SHIFT));
  641. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  642. if (esdhc_is_usdhc(imx_data)) {
  643. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  644. writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  645. host->ioaddr + ESDHC_VENDOR_SPEC);
  646. }
  647. mdelay(1);
  648. }
  649. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  650. {
  651. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  652. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  653. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  654. switch (boarddata->wp_type) {
  655. case ESDHC_WP_GPIO:
  656. return mmc_gpio_get_ro(host->mmc);
  657. case ESDHC_WP_CONTROLLER:
  658. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  659. SDHCI_WRITE_PROTECT);
  660. case ESDHC_WP_NONE:
  661. break;
  662. }
  663. return -ENOSYS;
  664. }
  665. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  666. {
  667. u32 ctrl;
  668. switch (width) {
  669. case MMC_BUS_WIDTH_8:
  670. ctrl = ESDHC_CTRL_8BITBUS;
  671. break;
  672. case MMC_BUS_WIDTH_4:
  673. ctrl = ESDHC_CTRL_4BITBUS;
  674. break;
  675. default:
  676. ctrl = 0;
  677. break;
  678. }
  679. esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
  680. SDHCI_HOST_CONTROL);
  681. }
  682. static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
  683. {
  684. u32 reg;
  685. /* FIXME: delay a bit for card to be ready for next tuning due to errors */
  686. mdelay(1);
  687. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  688. reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
  689. ESDHC_MIX_CTRL_FBCLK_SEL;
  690. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  691. writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  692. dev_dbg(mmc_dev(host->mmc),
  693. "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
  694. val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
  695. }
  696. static void esdhc_post_tuning(struct sdhci_host *host)
  697. {
  698. u32 reg;
  699. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  700. reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  701. reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  702. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  703. }
  704. static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
  705. {
  706. int min, max, avg, ret;
  707. /* find the mininum delay first which can pass tuning */
  708. min = ESDHC_TUNE_CTRL_MIN;
  709. while (min < ESDHC_TUNE_CTRL_MAX) {
  710. esdhc_prepare_tuning(host, min);
  711. if (!mmc_send_tuning(host->mmc, opcode, NULL))
  712. break;
  713. min += ESDHC_TUNE_CTRL_STEP;
  714. }
  715. /* find the maxinum delay which can not pass tuning */
  716. max = min + ESDHC_TUNE_CTRL_STEP;
  717. while (max < ESDHC_TUNE_CTRL_MAX) {
  718. esdhc_prepare_tuning(host, max);
  719. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  720. max -= ESDHC_TUNE_CTRL_STEP;
  721. break;
  722. }
  723. max += ESDHC_TUNE_CTRL_STEP;
  724. }
  725. /* use average delay to get the best timing */
  726. avg = (min + max) / 2;
  727. esdhc_prepare_tuning(host, avg);
  728. ret = mmc_send_tuning(host->mmc, opcode, NULL);
  729. esdhc_post_tuning(host);
  730. dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
  731. ret ? "failed" : "passed", avg, ret);
  732. return ret;
  733. }
  734. static int esdhc_change_pinstate(struct sdhci_host *host,
  735. unsigned int uhs)
  736. {
  737. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  738. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  739. struct pinctrl_state *pinctrl;
  740. dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
  741. if (IS_ERR(imx_data->pinctrl) ||
  742. IS_ERR(imx_data->pins_default) ||
  743. IS_ERR(imx_data->pins_100mhz) ||
  744. IS_ERR(imx_data->pins_200mhz))
  745. return -EINVAL;
  746. switch (uhs) {
  747. case MMC_TIMING_UHS_SDR50:
  748. case MMC_TIMING_UHS_DDR50:
  749. pinctrl = imx_data->pins_100mhz;
  750. break;
  751. case MMC_TIMING_UHS_SDR104:
  752. case MMC_TIMING_MMC_HS200:
  753. case MMC_TIMING_MMC_HS400:
  754. pinctrl = imx_data->pins_200mhz;
  755. break;
  756. default:
  757. /* back to default state for other legacy timing */
  758. pinctrl = imx_data->pins_default;
  759. }
  760. return pinctrl_select_state(imx_data->pinctrl, pinctrl);
  761. }
  762. /*
  763. * For HS400 eMMC, there is a data_strobe line. This signal is generated
  764. * by the device and used for data output and CRC status response output
  765. * in HS400 mode. The frequency of this signal follows the frequency of
  766. * CLK generated by host. The host receives the data which is aligned to the
  767. * edge of data_strobe line. Due to the time delay between CLK line and
  768. * data_strobe line, if the delay time is larger than one clock cycle,
  769. * then CLK and data_strobe line will be misaligned, read error shows up.
  770. * So when the CLK is higher than 100MHz, each clock cycle is short enough,
  771. * host should configure the delay target.
  772. */
  773. static void esdhc_set_strobe_dll(struct sdhci_host *host)
  774. {
  775. u32 v;
  776. if (host->mmc->actual_clock > ESDHC_STROBE_DLL_CLK_FREQ) {
  777. /* disable clock before enabling strobe dll */
  778. writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
  779. ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  780. host->ioaddr + ESDHC_VENDOR_SPEC);
  781. /* force a reset on strobe dll */
  782. writel(ESDHC_STROBE_DLL_CTRL_RESET,
  783. host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  784. /*
  785. * enable strobe dll ctrl and adjust the delay target
  786. * for the uSDHC loopback read clock
  787. */
  788. v = ESDHC_STROBE_DLL_CTRL_ENABLE |
  789. (7 << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
  790. writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  791. /* wait 1us to make sure strobe dll status register stable */
  792. udelay(1);
  793. v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
  794. if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
  795. dev_warn(mmc_dev(host->mmc),
  796. "warning! HS400 strobe DLL status REF not lock!\n");
  797. if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
  798. dev_warn(mmc_dev(host->mmc),
  799. "warning! HS400 strobe DLL status SLV not lock!\n");
  800. }
  801. }
  802. static void esdhc_reset_tuning(struct sdhci_host *host)
  803. {
  804. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  805. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  806. u32 ctrl;
  807. /* Reset the tuning circuit */
  808. if (esdhc_is_usdhc(imx_data)) {
  809. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  810. ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
  811. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  812. ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  813. writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
  814. writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  815. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  816. ctrl = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  817. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  818. writel(ctrl, host->ioaddr + SDHCI_ACMD12_ERR);
  819. }
  820. }
  821. }
  822. static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  823. {
  824. u32 m;
  825. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  826. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  827. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  828. /* disable ddr mode and disable HS400 mode */
  829. m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  830. m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
  831. imx_data->is_ddr = 0;
  832. switch (timing) {
  833. case MMC_TIMING_UHS_SDR12:
  834. case MMC_TIMING_UHS_SDR25:
  835. case MMC_TIMING_UHS_SDR50:
  836. case MMC_TIMING_UHS_SDR104:
  837. case MMC_TIMING_MMC_HS200:
  838. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  839. break;
  840. case MMC_TIMING_UHS_DDR50:
  841. case MMC_TIMING_MMC_DDR52:
  842. m |= ESDHC_MIX_CTRL_DDREN;
  843. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  844. imx_data->is_ddr = 1;
  845. if (boarddata->delay_line) {
  846. u32 v;
  847. v = boarddata->delay_line <<
  848. ESDHC_DLL_OVERRIDE_VAL_SHIFT |
  849. (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
  850. if (is_imx53_esdhc(imx_data))
  851. v <<= 1;
  852. writel(v, host->ioaddr + ESDHC_DLL_CTRL);
  853. }
  854. break;
  855. case MMC_TIMING_MMC_HS400:
  856. m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
  857. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  858. imx_data->is_ddr = 1;
  859. /* update clock after enable DDR for strobe DLL lock */
  860. host->ops->set_clock(host, host->clock);
  861. esdhc_set_strobe_dll(host);
  862. break;
  863. case MMC_TIMING_LEGACY:
  864. default:
  865. esdhc_reset_tuning(host);
  866. break;
  867. }
  868. esdhc_change_pinstate(host, timing);
  869. }
  870. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  871. {
  872. sdhci_reset(host, mask);
  873. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  874. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  875. }
  876. static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
  877. {
  878. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  879. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  880. /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
  881. return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
  882. }
  883. static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  884. {
  885. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  886. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  887. /* use maximum timeout counter */
  888. esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
  889. esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
  890. SDHCI_TIMEOUT_CONTROL);
  891. }
  892. static struct sdhci_ops sdhci_esdhc_ops = {
  893. .read_l = esdhc_readl_le,
  894. .read_w = esdhc_readw_le,
  895. .read_b = esdhc_readb_le,
  896. .write_l = esdhc_writel_le,
  897. .write_w = esdhc_writew_le,
  898. .write_b = esdhc_writeb_le,
  899. .set_clock = esdhc_pltfm_set_clock,
  900. .get_max_clock = esdhc_pltfm_get_max_clock,
  901. .get_min_clock = esdhc_pltfm_get_min_clock,
  902. .get_max_timeout_count = esdhc_get_max_timeout_count,
  903. .get_ro = esdhc_pltfm_get_ro,
  904. .set_timeout = esdhc_set_timeout,
  905. .set_bus_width = esdhc_pltfm_set_bus_width,
  906. .set_uhs_signaling = esdhc_set_uhs_signaling,
  907. .reset = esdhc_reset,
  908. };
  909. static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  910. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  911. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  912. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  913. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  914. .ops = &sdhci_esdhc_ops,
  915. };
  916. static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
  917. {
  918. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  919. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  920. int tmp;
  921. if (esdhc_is_usdhc(imx_data)) {
  922. /*
  923. * The imx6q ROM code will change the default watermark
  924. * level setting to something insane. Change it back here.
  925. */
  926. writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
  927. /*
  928. * ROM code will change the bit burst_length_enable setting
  929. * to zero if this usdhc is chosen to boot system. Change
  930. * it back here, otherwise it will impact the performance a
  931. * lot. This bit is used to enable/disable the burst length
  932. * for the external AHB2AXI bridge. It's useful especially
  933. * for INCR transfer because without burst length indicator,
  934. * the AHB2AXI bridge does not know the burst length in
  935. * advance. And without burst length indicator, AHB INCR
  936. * transfer can only be converted to singles on the AXI side.
  937. */
  938. writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
  939. | ESDHC_BURST_LEN_EN_INCR,
  940. host->ioaddr + SDHCI_HOST_CONTROL);
  941. /*
  942. * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
  943. * TO1.1, it's harmless for MX6SL
  944. */
  945. writel(readl(host->ioaddr + 0x6c) | BIT(7),
  946. host->ioaddr + 0x6c);
  947. /* disable DLL_CTRL delay line settings */
  948. writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
  949. if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  950. tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
  951. tmp |= ESDHC_STD_TUNING_EN |
  952. ESDHC_TUNING_START_TAP_DEFAULT;
  953. if (imx_data->boarddata.tuning_start_tap) {
  954. tmp &= ~ESDHC_TUNING_START_TAP_MASK;
  955. tmp |= imx_data->boarddata.tuning_start_tap;
  956. }
  957. if (imx_data->boarddata.tuning_step) {
  958. tmp &= ~ESDHC_TUNING_STEP_MASK;
  959. tmp |= imx_data->boarddata.tuning_step
  960. << ESDHC_TUNING_STEP_SHIFT;
  961. }
  962. writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
  963. }
  964. }
  965. }
  966. #ifdef CONFIG_OF
  967. static int
  968. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  969. struct sdhci_host *host,
  970. struct pltfm_imx_data *imx_data)
  971. {
  972. struct device_node *np = pdev->dev.of_node;
  973. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  974. int ret;
  975. if (of_get_property(np, "fsl,wp-controller", NULL))
  976. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  977. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  978. if (gpio_is_valid(boarddata->wp_gpio))
  979. boarddata->wp_type = ESDHC_WP_GPIO;
  980. of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
  981. of_property_read_u32(np, "fsl,tuning-start-tap",
  982. &boarddata->tuning_start_tap);
  983. if (of_find_property(np, "no-1-8-v", NULL))
  984. boarddata->support_vsel = false;
  985. else
  986. boarddata->support_vsel = true;
  987. if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
  988. boarddata->delay_line = 0;
  989. mmc_of_parse_voltage(np, &host->ocr_mask);
  990. /* sdr50 and sdr104 need work on 1.8v signal voltage */
  991. if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data) &&
  992. !IS_ERR(imx_data->pins_default)) {
  993. imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
  994. ESDHC_PINCTRL_STATE_100MHZ);
  995. imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
  996. ESDHC_PINCTRL_STATE_200MHZ);
  997. if (IS_ERR(imx_data->pins_100mhz) ||
  998. IS_ERR(imx_data->pins_200mhz)) {
  999. dev_warn(mmc_dev(host->mmc),
  1000. "could not get ultra high speed state, work on normal mode\n");
  1001. /*
  1002. * fall back to not supporting uhs by specifying no
  1003. * 1.8v quirk
  1004. */
  1005. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  1006. }
  1007. } else {
  1008. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  1009. }
  1010. /* call to generic mmc_of_parse to support additional capabilities */
  1011. ret = mmc_of_parse(host->mmc);
  1012. if (ret)
  1013. return ret;
  1014. if (mmc_gpio_get_cd(host->mmc) >= 0)
  1015. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1016. return 0;
  1017. }
  1018. #else
  1019. static inline int
  1020. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  1021. struct sdhci_host *host,
  1022. struct pltfm_imx_data *imx_data)
  1023. {
  1024. return -ENODEV;
  1025. }
  1026. #endif
  1027. static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
  1028. struct sdhci_host *host,
  1029. struct pltfm_imx_data *imx_data)
  1030. {
  1031. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  1032. int err;
  1033. if (!host->mmc->parent->platform_data) {
  1034. dev_err(mmc_dev(host->mmc), "no board data!\n");
  1035. return -EINVAL;
  1036. }
  1037. imx_data->boarddata = *((struct esdhc_platform_data *)
  1038. host->mmc->parent->platform_data);
  1039. /* write_protect */
  1040. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  1041. err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
  1042. if (err) {
  1043. dev_err(mmc_dev(host->mmc),
  1044. "failed to request write-protect gpio!\n");
  1045. return err;
  1046. }
  1047. host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  1048. }
  1049. /* card_detect */
  1050. switch (boarddata->cd_type) {
  1051. case ESDHC_CD_GPIO:
  1052. err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
  1053. if (err) {
  1054. dev_err(mmc_dev(host->mmc),
  1055. "failed to request card-detect gpio!\n");
  1056. return err;
  1057. }
  1058. /* fall through */
  1059. case ESDHC_CD_CONTROLLER:
  1060. /* we have a working card_detect back */
  1061. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1062. break;
  1063. case ESDHC_CD_PERMANENT:
  1064. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  1065. break;
  1066. case ESDHC_CD_NONE:
  1067. break;
  1068. }
  1069. switch (boarddata->max_bus_width) {
  1070. case 8:
  1071. host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  1072. break;
  1073. case 4:
  1074. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  1075. break;
  1076. case 1:
  1077. default:
  1078. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  1079. break;
  1080. }
  1081. return 0;
  1082. }
  1083. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  1084. {
  1085. const struct of_device_id *of_id =
  1086. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  1087. struct sdhci_pltfm_host *pltfm_host;
  1088. struct sdhci_host *host;
  1089. int err;
  1090. struct pltfm_imx_data *imx_data;
  1091. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
  1092. sizeof(*imx_data));
  1093. if (IS_ERR(host))
  1094. return PTR_ERR(host);
  1095. pltfm_host = sdhci_priv(host);
  1096. imx_data = sdhci_pltfm_priv(pltfm_host);
  1097. imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
  1098. pdev->id_entry->driver_data;
  1099. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1100. if (IS_ERR(imx_data->clk_ipg)) {
  1101. err = PTR_ERR(imx_data->clk_ipg);
  1102. goto free_sdhci;
  1103. }
  1104. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1105. if (IS_ERR(imx_data->clk_ahb)) {
  1106. err = PTR_ERR(imx_data->clk_ahb);
  1107. goto free_sdhci;
  1108. }
  1109. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  1110. if (IS_ERR(imx_data->clk_per)) {
  1111. err = PTR_ERR(imx_data->clk_per);
  1112. goto free_sdhci;
  1113. }
  1114. pltfm_host->clk = imx_data->clk_per;
  1115. pltfm_host->clock = clk_get_rate(pltfm_host->clk);
  1116. err = clk_prepare_enable(imx_data->clk_per);
  1117. if (err)
  1118. goto free_sdhci;
  1119. err = clk_prepare_enable(imx_data->clk_ipg);
  1120. if (err)
  1121. goto disable_per_clk;
  1122. err = clk_prepare_enable(imx_data->clk_ahb);
  1123. if (err)
  1124. goto disable_ipg_clk;
  1125. imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  1126. if (IS_ERR(imx_data->pinctrl)) {
  1127. err = PTR_ERR(imx_data->pinctrl);
  1128. goto disable_ahb_clk;
  1129. }
  1130. imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
  1131. PINCTRL_STATE_DEFAULT);
  1132. if (IS_ERR(imx_data->pins_default))
  1133. dev_warn(mmc_dev(host->mmc), "could not get default state\n");
  1134. if (esdhc_is_usdhc(imx_data)) {
  1135. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  1136. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  1137. if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
  1138. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  1139. /* clear tuning bits in case ROM has set it already */
  1140. writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
  1141. writel(0x0, host->ioaddr + SDHCI_ACMD12_ERR);
  1142. writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  1143. }
  1144. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  1145. sdhci_esdhc_ops.platform_execute_tuning =
  1146. esdhc_executing_tuning;
  1147. if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
  1148. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  1149. if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
  1150. host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
  1151. if (of_id)
  1152. err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
  1153. else
  1154. err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
  1155. if (err)
  1156. goto disable_ahb_clk;
  1157. sdhci_esdhc_imx_hwinit(host);
  1158. err = sdhci_add_host(host);
  1159. if (err)
  1160. goto disable_ahb_clk;
  1161. pm_runtime_set_active(&pdev->dev);
  1162. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1163. pm_runtime_use_autosuspend(&pdev->dev);
  1164. pm_suspend_ignore_children(&pdev->dev, 1);
  1165. pm_runtime_enable(&pdev->dev);
  1166. return 0;
  1167. disable_ahb_clk:
  1168. clk_disable_unprepare(imx_data->clk_ahb);
  1169. disable_ipg_clk:
  1170. clk_disable_unprepare(imx_data->clk_ipg);
  1171. disable_per_clk:
  1172. clk_disable_unprepare(imx_data->clk_per);
  1173. free_sdhci:
  1174. sdhci_pltfm_free(pdev);
  1175. return err;
  1176. }
  1177. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  1178. {
  1179. struct sdhci_host *host = platform_get_drvdata(pdev);
  1180. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1181. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1182. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  1183. pm_runtime_get_sync(&pdev->dev);
  1184. pm_runtime_disable(&pdev->dev);
  1185. pm_runtime_put_noidle(&pdev->dev);
  1186. sdhci_remove_host(host, dead);
  1187. clk_disable_unprepare(imx_data->clk_per);
  1188. clk_disable_unprepare(imx_data->clk_ipg);
  1189. clk_disable_unprepare(imx_data->clk_ahb);
  1190. sdhci_pltfm_free(pdev);
  1191. return 0;
  1192. }
  1193. #ifdef CONFIG_PM_SLEEP
  1194. static int sdhci_esdhc_suspend(struct device *dev)
  1195. {
  1196. struct sdhci_host *host = dev_get_drvdata(dev);
  1197. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1198. mmc_retune_needed(host->mmc);
  1199. return sdhci_suspend_host(host);
  1200. }
  1201. static int sdhci_esdhc_resume(struct device *dev)
  1202. {
  1203. struct sdhci_host *host = dev_get_drvdata(dev);
  1204. /* re-initialize hw state in case it's lost in low power mode */
  1205. sdhci_esdhc_imx_hwinit(host);
  1206. return sdhci_resume_host(host);
  1207. }
  1208. #endif
  1209. #ifdef CONFIG_PM
  1210. static int sdhci_esdhc_runtime_suspend(struct device *dev)
  1211. {
  1212. struct sdhci_host *host = dev_get_drvdata(dev);
  1213. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1214. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1215. int ret;
  1216. ret = sdhci_runtime_suspend_host(host);
  1217. if (ret)
  1218. return ret;
  1219. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1220. mmc_retune_needed(host->mmc);
  1221. if (!sdhci_sdio_irq_enabled(host)) {
  1222. imx_data->actual_clock = host->mmc->actual_clock;
  1223. esdhc_pltfm_set_clock(host, 0);
  1224. clk_disable_unprepare(imx_data->clk_per);
  1225. clk_disable_unprepare(imx_data->clk_ipg);
  1226. }
  1227. clk_disable_unprepare(imx_data->clk_ahb);
  1228. return ret;
  1229. }
  1230. static int sdhci_esdhc_runtime_resume(struct device *dev)
  1231. {
  1232. struct sdhci_host *host = dev_get_drvdata(dev);
  1233. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1234. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1235. int err;
  1236. err = clk_prepare_enable(imx_data->clk_ahb);
  1237. if (err)
  1238. return err;
  1239. if (!sdhci_sdio_irq_enabled(host)) {
  1240. err = clk_prepare_enable(imx_data->clk_per);
  1241. if (err)
  1242. goto disable_ahb_clk;
  1243. err = clk_prepare_enable(imx_data->clk_ipg);
  1244. if (err)
  1245. goto disable_per_clk;
  1246. esdhc_pltfm_set_clock(host, imx_data->actual_clock);
  1247. }
  1248. err = sdhci_runtime_resume_host(host);
  1249. if (err)
  1250. goto disable_ipg_clk;
  1251. return 0;
  1252. disable_ipg_clk:
  1253. if (!sdhci_sdio_irq_enabled(host))
  1254. clk_disable_unprepare(imx_data->clk_ipg);
  1255. disable_per_clk:
  1256. if (!sdhci_sdio_irq_enabled(host))
  1257. clk_disable_unprepare(imx_data->clk_per);
  1258. disable_ahb_clk:
  1259. clk_disable_unprepare(imx_data->clk_ahb);
  1260. return err;
  1261. }
  1262. #endif
  1263. static const struct dev_pm_ops sdhci_esdhc_pmops = {
  1264. SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
  1265. SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
  1266. sdhci_esdhc_runtime_resume, NULL)
  1267. };
  1268. static struct platform_driver sdhci_esdhc_imx_driver = {
  1269. .driver = {
  1270. .name = "sdhci-esdhc-imx",
  1271. .of_match_table = imx_esdhc_dt_ids,
  1272. .pm = &sdhci_esdhc_pmops,
  1273. },
  1274. .id_table = imx_esdhc_devtype,
  1275. .probe = sdhci_esdhc_imx_probe,
  1276. .remove = sdhci_esdhc_imx_remove,
  1277. };
  1278. module_platform_driver(sdhci_esdhc_imx_driver);
  1279. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  1280. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  1281. MODULE_LICENSE("GPL v2");