sdhci-esdhc-imx.c 40 KB

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