sdhci-esdhc-imx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 <w.sang@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_CTRL_D3CD 0x08
  33. /* VENDOR SPEC register */
  34. #define ESDHC_VENDOR_SPEC 0xc0
  35. #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
  36. #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
  37. #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
  38. #define ESDHC_WTMK_LVL 0x44
  39. #define ESDHC_MIX_CTRL 0x48
  40. #define ESDHC_MIX_CTRL_DDREN (1 << 3)
  41. #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
  42. #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
  43. #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
  44. #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
  45. /* Bits 3 and 6 are not SDHCI standard definitions */
  46. #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
  47. /* Tuning bits */
  48. #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
  49. /* dll control register */
  50. #define ESDHC_DLL_CTRL 0x60
  51. #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
  52. #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
  53. /* tune control register */
  54. #define ESDHC_TUNE_CTRL_STATUS 0x68
  55. #define ESDHC_TUNE_CTRL_STEP 1
  56. #define ESDHC_TUNE_CTRL_MIN 0
  57. #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
  58. #define ESDHC_TUNING_CTRL 0xcc
  59. #define ESDHC_STD_TUNING_EN (1 << 24)
  60. /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
  61. #define ESDHC_TUNING_START_TAP 0x1
  62. #define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
  63. /* pinctrl state */
  64. #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
  65. #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
  66. /*
  67. * Our interpretation of the SDHCI_HOST_CONTROL register
  68. */
  69. #define ESDHC_CTRL_4BITBUS (0x1 << 1)
  70. #define ESDHC_CTRL_8BITBUS (0x2 << 1)
  71. #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
  72. /*
  73. * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
  74. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  75. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  76. * Define this macro DMA error INT for fsl eSDHC
  77. */
  78. #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
  79. /*
  80. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  81. * "11" when the STOP CMD12 is issued on imx53 to abort one
  82. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  83. * be generated.
  84. * In exact block transfer, the controller doesn't complete the
  85. * operations automatically as required at the end of the
  86. * transfer and remains on hold if the abort command is not sent.
  87. * As a result, the TC flag is not asserted and SW received timeout
  88. * exeception. Bit1 of Vendor Spec registor is used to fix it.
  89. */
  90. #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
  91. /*
  92. * The flag enables the workaround for ESDHC errata ENGcm07207 which
  93. * affects i.MX25 and i.MX35.
  94. */
  95. #define ESDHC_FLAG_ENGCM07207 BIT(2)
  96. /*
  97. * The flag tells that the ESDHC controller is an USDHC block that is
  98. * integrated on the i.MX6 series.
  99. */
  100. #define ESDHC_FLAG_USDHC BIT(3)
  101. /* The IP supports manual tuning process */
  102. #define ESDHC_FLAG_MAN_TUNING BIT(4)
  103. /* The IP supports standard tuning process */
  104. #define ESDHC_FLAG_STD_TUNING BIT(5)
  105. /* The IP has SDHCI_CAPABILITIES_1 register */
  106. #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
  107. struct esdhc_soc_data {
  108. u32 flags;
  109. };
  110. static struct esdhc_soc_data esdhc_imx25_data = {
  111. .flags = ESDHC_FLAG_ENGCM07207,
  112. };
  113. static struct esdhc_soc_data esdhc_imx35_data = {
  114. .flags = ESDHC_FLAG_ENGCM07207,
  115. };
  116. static struct esdhc_soc_data esdhc_imx51_data = {
  117. .flags = 0,
  118. };
  119. static struct esdhc_soc_data esdhc_imx53_data = {
  120. .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
  121. };
  122. static struct esdhc_soc_data usdhc_imx6q_data = {
  123. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
  124. };
  125. static struct esdhc_soc_data usdhc_imx6sl_data = {
  126. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  127. | ESDHC_FLAG_HAVE_CAP1,
  128. };
  129. struct pltfm_imx_data {
  130. u32 scratchpad;
  131. struct pinctrl *pinctrl;
  132. struct pinctrl_state *pins_default;
  133. struct pinctrl_state *pins_100mhz;
  134. struct pinctrl_state *pins_200mhz;
  135. const struct esdhc_soc_data *socdata;
  136. struct esdhc_platform_data boarddata;
  137. struct clk *clk_ipg;
  138. struct clk *clk_ahb;
  139. struct clk *clk_per;
  140. enum {
  141. NO_CMD_PENDING, /* no multiblock command pending*/
  142. MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
  143. WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
  144. } multiblock_status;
  145. u32 is_ddr;
  146. };
  147. static struct platform_device_id imx_esdhc_devtype[] = {
  148. {
  149. .name = "sdhci-esdhc-imx25",
  150. .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
  151. }, {
  152. .name = "sdhci-esdhc-imx35",
  153. .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
  154. }, {
  155. .name = "sdhci-esdhc-imx51",
  156. .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
  157. }, {
  158. /* sentinel */
  159. }
  160. };
  161. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  162. static const struct of_device_id imx_esdhc_dt_ids[] = {
  163. { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
  164. { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
  165. { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
  166. { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
  167. { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
  168. { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
  169. { /* sentinel */ }
  170. };
  171. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  172. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  173. {
  174. return data->socdata == &esdhc_imx25_data;
  175. }
  176. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  177. {
  178. return data->socdata == &esdhc_imx53_data;
  179. }
  180. static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
  181. {
  182. return data->socdata == &usdhc_imx6q_data;
  183. }
  184. static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
  185. {
  186. return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
  187. }
  188. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  189. {
  190. void __iomem *base = host->ioaddr + (reg & ~0x3);
  191. u32 shift = (reg & 0x3) * 8;
  192. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  193. }
  194. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  195. {
  196. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  197. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  198. u32 val = readl(host->ioaddr + reg);
  199. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  200. u32 fsl_prss = val;
  201. /* save the least 20 bits */
  202. val = fsl_prss & 0x000FFFFF;
  203. /* move dat[0-3] bits */
  204. val |= (fsl_prss & 0x0F000000) >> 4;
  205. /* move cmd line bit */
  206. val |= (fsl_prss & 0x00800000) << 1;
  207. }
  208. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  209. /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
  210. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  211. val &= 0xffff0000;
  212. /* In FSL esdhc IC module, only bit20 is used to indicate the
  213. * ADMA2 capability of esdhc, but this bit is messed up on
  214. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  215. * don't actually support ADMA2). So set the BROKEN_ADMA
  216. * uirk on MX25/35 platforms.
  217. */
  218. if (val & SDHCI_CAN_DO_ADMA1) {
  219. val &= ~SDHCI_CAN_DO_ADMA1;
  220. val |= SDHCI_CAN_DO_ADMA2;
  221. }
  222. }
  223. if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
  224. if (esdhc_is_usdhc(imx_data)) {
  225. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  226. val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
  227. else
  228. /* imx6q/dl does not have cap_1 register, fake one */
  229. val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
  230. | SDHCI_SUPPORT_SDR50
  231. | SDHCI_USE_SDR50_TUNING;
  232. }
  233. }
  234. if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
  235. val = 0;
  236. val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
  237. val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
  238. val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
  239. }
  240. if (unlikely(reg == SDHCI_INT_STATUS)) {
  241. if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
  242. val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  243. val |= SDHCI_INT_ADMA_ERROR;
  244. }
  245. /*
  246. * mask off the interrupt we get in response to the manually
  247. * sent CMD12
  248. */
  249. if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
  250. ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
  251. val &= ~SDHCI_INT_RESPONSE;
  252. writel(SDHCI_INT_RESPONSE, host->ioaddr +
  253. SDHCI_INT_STATUS);
  254. imx_data->multiblock_status = NO_CMD_PENDING;
  255. }
  256. }
  257. return val;
  258. }
  259. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  260. {
  261. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  262. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  263. u32 data;
  264. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  265. if (val & SDHCI_INT_CARD_INT) {
  266. /*
  267. * Clear and then set D3CD bit to avoid missing the
  268. * card interrupt. This is a eSDHC controller problem
  269. * so we need to apply the following workaround: clear
  270. * and set D3CD bit will make eSDHC re-sample the card
  271. * interrupt. In case a card interrupt was lost,
  272. * re-sample it by the following steps.
  273. */
  274. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  275. data &= ~ESDHC_CTRL_D3CD;
  276. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  277. data |= ESDHC_CTRL_D3CD;
  278. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  279. }
  280. }
  281. if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  282. && (reg == SDHCI_INT_STATUS)
  283. && (val & SDHCI_INT_DATA_END))) {
  284. u32 v;
  285. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  286. v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  287. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  288. if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
  289. {
  290. /* send a manual CMD12 with RESPTYP=none */
  291. data = MMC_STOP_TRANSMISSION << 24 |
  292. SDHCI_CMD_ABORTCMD << 16;
  293. writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
  294. imx_data->multiblock_status = WAIT_FOR_INT;
  295. }
  296. }
  297. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  298. if (val & SDHCI_INT_ADMA_ERROR) {
  299. val &= ~SDHCI_INT_ADMA_ERROR;
  300. val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  301. }
  302. }
  303. writel(val, host->ioaddr + reg);
  304. }
  305. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  306. {
  307. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  308. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  309. u16 ret = 0;
  310. u32 val;
  311. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  312. reg ^= 2;
  313. if (esdhc_is_usdhc(imx_data)) {
  314. /*
  315. * The usdhc register returns a wrong host version.
  316. * Correct it here.
  317. */
  318. return SDHCI_SPEC_300;
  319. }
  320. }
  321. if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
  322. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  323. if (val & ESDHC_VENDOR_SPEC_VSELECT)
  324. ret |= SDHCI_CTRL_VDD_180;
  325. if (esdhc_is_usdhc(imx_data)) {
  326. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  327. val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  328. else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
  329. /* the std tuning bits is in ACMD12_ERR for imx6sl */
  330. val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  331. }
  332. if (val & ESDHC_MIX_CTRL_EXE_TUNE)
  333. ret |= SDHCI_CTRL_EXEC_TUNING;
  334. if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
  335. ret |= SDHCI_CTRL_TUNED_CLK;
  336. ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  337. return ret;
  338. }
  339. if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
  340. if (esdhc_is_usdhc(imx_data)) {
  341. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  342. ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
  343. /* Swap AC23 bit */
  344. if (m & ESDHC_MIX_CTRL_AC23EN) {
  345. ret &= ~ESDHC_MIX_CTRL_AC23EN;
  346. ret |= SDHCI_TRNS_AUTO_CMD23;
  347. }
  348. } else {
  349. ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
  350. }
  351. return ret;
  352. }
  353. return readw(host->ioaddr + reg);
  354. }
  355. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  356. {
  357. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  358. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  359. u32 new_val = 0;
  360. switch (reg) {
  361. case SDHCI_CLOCK_CONTROL:
  362. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  363. if (val & SDHCI_CLOCK_CARD_EN)
  364. new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  365. else
  366. new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  367. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  368. return;
  369. case SDHCI_HOST_CONTROL2:
  370. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  371. if (val & SDHCI_CTRL_VDD_180)
  372. new_val |= ESDHC_VENDOR_SPEC_VSELECT;
  373. else
  374. new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
  375. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  376. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  377. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  378. if (val & SDHCI_CTRL_TUNED_CLK)
  379. new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  380. else
  381. new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  382. writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
  383. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  384. u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  385. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  386. if (val & SDHCI_CTRL_TUNED_CLK) {
  387. v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  388. } else {
  389. v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  390. m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  391. }
  392. if (val & SDHCI_CTRL_EXEC_TUNING) {
  393. v |= ESDHC_MIX_CTRL_EXE_TUNE;
  394. m |= ESDHC_MIX_CTRL_FBCLK_SEL;
  395. } else {
  396. v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  397. }
  398. writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
  399. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  400. }
  401. return;
  402. case SDHCI_TRANSFER_MODE:
  403. if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  404. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  405. && (host->cmd->data->blocks > 1)
  406. && (host->cmd->data->flags & MMC_DATA_READ)) {
  407. u32 v;
  408. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  409. v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  410. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  411. }
  412. if (esdhc_is_usdhc(imx_data)) {
  413. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  414. /* Swap AC23 bit */
  415. if (val & SDHCI_TRNS_AUTO_CMD23) {
  416. val &= ~SDHCI_TRNS_AUTO_CMD23;
  417. val |= ESDHC_MIX_CTRL_AC23EN;
  418. }
  419. m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
  420. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  421. } else {
  422. /*
  423. * Postpone this write, we must do it together with a
  424. * command write that is down below.
  425. */
  426. imx_data->scratchpad = val;
  427. }
  428. return;
  429. case SDHCI_COMMAND:
  430. if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
  431. val |= SDHCI_CMD_ABORTCMD;
  432. if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  433. (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  434. imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
  435. if (esdhc_is_usdhc(imx_data))
  436. writel(val << 16,
  437. host->ioaddr + SDHCI_TRANSFER_MODE);
  438. else
  439. writel(val << 16 | imx_data->scratchpad,
  440. host->ioaddr + SDHCI_TRANSFER_MODE);
  441. return;
  442. case SDHCI_BLOCK_SIZE:
  443. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  444. break;
  445. }
  446. esdhc_clrset_le(host, 0xffff, val, reg);
  447. }
  448. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  449. {
  450. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  451. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  452. u32 new_val;
  453. u32 mask;
  454. switch (reg) {
  455. case SDHCI_POWER_CONTROL:
  456. /*
  457. * FSL put some DMA bits here
  458. * If your board has a regulator, code should be here
  459. */
  460. return;
  461. case SDHCI_HOST_CONTROL:
  462. /* FSL messed up here, so we need to manually compose it. */
  463. new_val = val & SDHCI_CTRL_LED;
  464. /* ensure the endianness */
  465. new_val |= ESDHC_HOST_CONTROL_LE;
  466. /* bits 8&9 are reserved on mx25 */
  467. if (!is_imx25_esdhc(imx_data)) {
  468. /* DMA mode bits are shifted */
  469. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  470. }
  471. /*
  472. * Do not touch buswidth bits here. This is done in
  473. * esdhc_pltfm_bus_width.
  474. * Do not touch the D3CD bit either which is used for the
  475. * SDIO interrupt errata workaround.
  476. */
  477. mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
  478. esdhc_clrset_le(host, mask, new_val, reg);
  479. return;
  480. }
  481. esdhc_clrset_le(host, 0xff, val, reg);
  482. /*
  483. * The esdhc has a design violation to SDHC spec which tells
  484. * that software reset should not affect card detection circuit.
  485. * But esdhc clears its SYSCTL register bits [0..2] during the
  486. * software reset. This will stop those clocks that card detection
  487. * circuit relies on. To work around it, we turn the clocks on back
  488. * to keep card detection circuit functional.
  489. */
  490. if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
  491. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  492. /*
  493. * The reset on usdhc fails to clear MIX_CTRL register.
  494. * Do it manually here.
  495. */
  496. if (esdhc_is_usdhc(imx_data)) {
  497. /* the tuning bits should be kept during reset */
  498. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  499. writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
  500. host->ioaddr + ESDHC_MIX_CTRL);
  501. imx_data->is_ddr = 0;
  502. }
  503. }
  504. }
  505. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  506. {
  507. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  508. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  509. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  510. if (boarddata->f_max && (boarddata->f_max < pltfm_host->clock))
  511. return boarddata->f_max;
  512. else
  513. return pltfm_host->clock;
  514. }
  515. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  516. {
  517. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  518. return pltfm_host->clock / 256 / 16;
  519. }
  520. static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
  521. unsigned int clock)
  522. {
  523. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  524. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  525. unsigned int host_clock = pltfm_host->clock;
  526. int pre_div = 2;
  527. int div = 1;
  528. u32 temp, val;
  529. if (clock == 0) {
  530. host->mmc->actual_clock = 0;
  531. if (esdhc_is_usdhc(imx_data)) {
  532. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  533. writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  534. host->ioaddr + ESDHC_VENDOR_SPEC);
  535. }
  536. return;
  537. }
  538. if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
  539. pre_div = 1;
  540. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  541. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  542. | ESDHC_CLOCK_MASK);
  543. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  544. while (host_clock / pre_div / 16 > clock && pre_div < 256)
  545. pre_div *= 2;
  546. while (host_clock / pre_div / div > clock && div < 16)
  547. div++;
  548. host->mmc->actual_clock = host_clock / pre_div / div;
  549. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  550. clock, host->mmc->actual_clock);
  551. if (imx_data->is_ddr)
  552. pre_div >>= 2;
  553. else
  554. pre_div >>= 1;
  555. div--;
  556. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  557. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  558. | (div << ESDHC_DIVIDER_SHIFT)
  559. | (pre_div << ESDHC_PREDIV_SHIFT));
  560. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  561. if (esdhc_is_usdhc(imx_data)) {
  562. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  563. writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  564. host->ioaddr + ESDHC_VENDOR_SPEC);
  565. }
  566. mdelay(1);
  567. }
  568. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  569. {
  570. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  571. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  572. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  573. switch (boarddata->wp_type) {
  574. case ESDHC_WP_GPIO:
  575. return mmc_gpio_get_ro(host->mmc);
  576. case ESDHC_WP_CONTROLLER:
  577. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  578. SDHCI_WRITE_PROTECT);
  579. case ESDHC_WP_NONE:
  580. break;
  581. }
  582. return -ENOSYS;
  583. }
  584. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  585. {
  586. u32 ctrl;
  587. switch (width) {
  588. case MMC_BUS_WIDTH_8:
  589. ctrl = ESDHC_CTRL_8BITBUS;
  590. break;
  591. case MMC_BUS_WIDTH_4:
  592. ctrl = ESDHC_CTRL_4BITBUS;
  593. break;
  594. default:
  595. ctrl = 0;
  596. break;
  597. }
  598. esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
  599. SDHCI_HOST_CONTROL);
  600. }
  601. static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
  602. {
  603. u32 reg;
  604. /* FIXME: delay a bit for card to be ready for next tuning due to errors */
  605. mdelay(1);
  606. /* This is balanced by the runtime put in sdhci_tasklet_finish */
  607. pm_runtime_get_sync(host->mmc->parent);
  608. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  609. reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
  610. ESDHC_MIX_CTRL_FBCLK_SEL;
  611. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  612. writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  613. dev_dbg(mmc_dev(host->mmc),
  614. "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
  615. val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
  616. }
  617. static void esdhc_request_done(struct mmc_request *mrq)
  618. {
  619. complete(&mrq->completion);
  620. }
  621. static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode,
  622. struct scatterlist *sg)
  623. {
  624. struct mmc_command cmd = {0};
  625. struct mmc_request mrq = {NULL};
  626. struct mmc_data data = {0};
  627. cmd.opcode = opcode;
  628. cmd.arg = 0;
  629. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  630. data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
  631. data.blocks = 1;
  632. data.flags = MMC_DATA_READ;
  633. data.sg = sg;
  634. data.sg_len = 1;
  635. mrq.cmd = &cmd;
  636. mrq.cmd->mrq = &mrq;
  637. mrq.data = &data;
  638. mrq.data->mrq = &mrq;
  639. mrq.cmd->data = mrq.data;
  640. mrq.done = esdhc_request_done;
  641. init_completion(&(mrq.completion));
  642. spin_lock_irq(&host->lock);
  643. host->mrq = &mrq;
  644. sdhci_send_command(host, mrq.cmd);
  645. spin_unlock_irq(&host->lock);
  646. wait_for_completion(&mrq.completion);
  647. if (cmd.error)
  648. return cmd.error;
  649. if (data.error)
  650. return data.error;
  651. return 0;
  652. }
  653. static void esdhc_post_tuning(struct sdhci_host *host)
  654. {
  655. u32 reg;
  656. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  657. reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  658. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  659. }
  660. static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
  661. {
  662. struct scatterlist sg;
  663. char *tuning_pattern;
  664. int min, max, avg, ret;
  665. tuning_pattern = kmalloc(ESDHC_TUNING_BLOCK_PATTERN_LEN, GFP_KERNEL);
  666. if (!tuning_pattern)
  667. return -ENOMEM;
  668. sg_init_one(&sg, tuning_pattern, ESDHC_TUNING_BLOCK_PATTERN_LEN);
  669. /* find the mininum delay first which can pass tuning */
  670. min = ESDHC_TUNE_CTRL_MIN;
  671. while (min < ESDHC_TUNE_CTRL_MAX) {
  672. esdhc_prepare_tuning(host, min);
  673. if (!esdhc_send_tuning_cmd(host, opcode, &sg))
  674. break;
  675. min += ESDHC_TUNE_CTRL_STEP;
  676. }
  677. /* find the maxinum delay which can not pass tuning */
  678. max = min + ESDHC_TUNE_CTRL_STEP;
  679. while (max < ESDHC_TUNE_CTRL_MAX) {
  680. esdhc_prepare_tuning(host, max);
  681. if (esdhc_send_tuning_cmd(host, opcode, &sg)) {
  682. max -= ESDHC_TUNE_CTRL_STEP;
  683. break;
  684. }
  685. max += ESDHC_TUNE_CTRL_STEP;
  686. }
  687. /* use average delay to get the best timing */
  688. avg = (min + max) / 2;
  689. esdhc_prepare_tuning(host, avg);
  690. ret = esdhc_send_tuning_cmd(host, opcode, &sg);
  691. esdhc_post_tuning(host);
  692. kfree(tuning_pattern);
  693. dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
  694. ret ? "failed" : "passed", avg, ret);
  695. return ret;
  696. }
  697. static int esdhc_change_pinstate(struct sdhci_host *host,
  698. unsigned int uhs)
  699. {
  700. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  701. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  702. struct pinctrl_state *pinctrl;
  703. dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
  704. if (IS_ERR(imx_data->pinctrl) ||
  705. IS_ERR(imx_data->pins_default) ||
  706. IS_ERR(imx_data->pins_100mhz) ||
  707. IS_ERR(imx_data->pins_200mhz))
  708. return -EINVAL;
  709. switch (uhs) {
  710. case MMC_TIMING_UHS_SDR50:
  711. pinctrl = imx_data->pins_100mhz;
  712. break;
  713. case MMC_TIMING_UHS_SDR104:
  714. case MMC_TIMING_MMC_HS200:
  715. pinctrl = imx_data->pins_200mhz;
  716. break;
  717. default:
  718. /* back to default state for other legacy timing */
  719. pinctrl = imx_data->pins_default;
  720. }
  721. return pinctrl_select_state(imx_data->pinctrl, pinctrl);
  722. }
  723. static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  724. {
  725. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  726. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  727. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  728. switch (timing) {
  729. case MMC_TIMING_UHS_SDR12:
  730. case MMC_TIMING_UHS_SDR25:
  731. case MMC_TIMING_UHS_SDR50:
  732. case MMC_TIMING_UHS_SDR104:
  733. case MMC_TIMING_MMC_HS200:
  734. break;
  735. case MMC_TIMING_UHS_DDR50:
  736. case MMC_TIMING_MMC_DDR52:
  737. writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
  738. ESDHC_MIX_CTRL_DDREN,
  739. host->ioaddr + ESDHC_MIX_CTRL);
  740. imx_data->is_ddr = 1;
  741. if (boarddata->delay_line) {
  742. u32 v;
  743. v = boarddata->delay_line <<
  744. ESDHC_DLL_OVERRIDE_VAL_SHIFT |
  745. (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
  746. if (is_imx53_esdhc(imx_data))
  747. v <<= 1;
  748. writel(v, host->ioaddr + ESDHC_DLL_CTRL);
  749. }
  750. break;
  751. }
  752. esdhc_change_pinstate(host, timing);
  753. }
  754. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  755. {
  756. sdhci_reset(host, mask);
  757. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  758. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  759. }
  760. static struct sdhci_ops sdhci_esdhc_ops = {
  761. .read_l = esdhc_readl_le,
  762. .read_w = esdhc_readw_le,
  763. .write_l = esdhc_writel_le,
  764. .write_w = esdhc_writew_le,
  765. .write_b = esdhc_writeb_le,
  766. .set_clock = esdhc_pltfm_set_clock,
  767. .get_max_clock = esdhc_pltfm_get_max_clock,
  768. .get_min_clock = esdhc_pltfm_get_min_clock,
  769. .get_ro = esdhc_pltfm_get_ro,
  770. .set_bus_width = esdhc_pltfm_set_bus_width,
  771. .set_uhs_signaling = esdhc_set_uhs_signaling,
  772. .reset = esdhc_reset,
  773. };
  774. static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  775. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  776. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  777. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  778. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  779. .ops = &sdhci_esdhc_ops,
  780. };
  781. #ifdef CONFIG_OF
  782. static int
  783. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  784. struct esdhc_platform_data *boarddata)
  785. {
  786. struct device_node *np = pdev->dev.of_node;
  787. if (!np)
  788. return -ENODEV;
  789. if (of_get_property(np, "non-removable", NULL))
  790. boarddata->cd_type = ESDHC_CD_PERMANENT;
  791. if (of_get_property(np, "fsl,cd-controller", NULL))
  792. boarddata->cd_type = ESDHC_CD_CONTROLLER;
  793. if (of_get_property(np, "fsl,wp-controller", NULL))
  794. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  795. boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  796. if (gpio_is_valid(boarddata->cd_gpio))
  797. boarddata->cd_type = ESDHC_CD_GPIO;
  798. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  799. if (gpio_is_valid(boarddata->wp_gpio))
  800. boarddata->wp_type = ESDHC_WP_GPIO;
  801. of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
  802. of_property_read_u32(np, "max-frequency", &boarddata->f_max);
  803. if (of_find_property(np, "no-1-8-v", NULL))
  804. boarddata->support_vsel = false;
  805. else
  806. boarddata->support_vsel = true;
  807. if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
  808. boarddata->delay_line = 0;
  809. return 0;
  810. }
  811. #else
  812. static inline int
  813. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  814. struct esdhc_platform_data *boarddata)
  815. {
  816. return -ENODEV;
  817. }
  818. #endif
  819. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  820. {
  821. const struct of_device_id *of_id =
  822. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  823. struct sdhci_pltfm_host *pltfm_host;
  824. struct sdhci_host *host;
  825. struct esdhc_platform_data *boarddata;
  826. int err;
  827. struct pltfm_imx_data *imx_data;
  828. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
  829. if (IS_ERR(host))
  830. return PTR_ERR(host);
  831. pltfm_host = sdhci_priv(host);
  832. imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
  833. if (!imx_data) {
  834. err = -ENOMEM;
  835. goto free_sdhci;
  836. }
  837. imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
  838. pdev->id_entry->driver_data;
  839. pltfm_host->priv = imx_data;
  840. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  841. if (IS_ERR(imx_data->clk_ipg)) {
  842. err = PTR_ERR(imx_data->clk_ipg);
  843. goto free_sdhci;
  844. }
  845. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  846. if (IS_ERR(imx_data->clk_ahb)) {
  847. err = PTR_ERR(imx_data->clk_ahb);
  848. goto free_sdhci;
  849. }
  850. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  851. if (IS_ERR(imx_data->clk_per)) {
  852. err = PTR_ERR(imx_data->clk_per);
  853. goto free_sdhci;
  854. }
  855. pltfm_host->clk = imx_data->clk_per;
  856. pltfm_host->clock = clk_get_rate(pltfm_host->clk);
  857. clk_prepare_enable(imx_data->clk_per);
  858. clk_prepare_enable(imx_data->clk_ipg);
  859. clk_prepare_enable(imx_data->clk_ahb);
  860. imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  861. if (IS_ERR(imx_data->pinctrl)) {
  862. err = PTR_ERR(imx_data->pinctrl);
  863. goto disable_clk;
  864. }
  865. imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
  866. PINCTRL_STATE_DEFAULT);
  867. if (IS_ERR(imx_data->pins_default)) {
  868. err = PTR_ERR(imx_data->pins_default);
  869. dev_err(mmc_dev(host->mmc), "could not get default state\n");
  870. goto disable_clk;
  871. }
  872. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  873. if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
  874. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  875. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
  876. | SDHCI_QUIRK_BROKEN_ADMA;
  877. /*
  878. * The imx6q ROM code will change the default watermark level setting
  879. * to something insane. Change it back here.
  880. */
  881. if (esdhc_is_usdhc(imx_data)) {
  882. writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
  883. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  884. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  885. }
  886. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  887. sdhci_esdhc_ops.platform_execute_tuning =
  888. esdhc_executing_tuning;
  889. if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
  890. writel(readl(host->ioaddr + ESDHC_TUNING_CTRL) |
  891. ESDHC_STD_TUNING_EN | ESDHC_TUNING_START_TAP,
  892. host->ioaddr + ESDHC_TUNING_CTRL);
  893. boarddata = &imx_data->boarddata;
  894. if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
  895. if (!host->mmc->parent->platform_data) {
  896. dev_err(mmc_dev(host->mmc), "no board data!\n");
  897. err = -EINVAL;
  898. goto disable_clk;
  899. }
  900. imx_data->boarddata = *((struct esdhc_platform_data *)
  901. host->mmc->parent->platform_data);
  902. }
  903. /* write_protect */
  904. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  905. err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
  906. if (err) {
  907. dev_err(mmc_dev(host->mmc),
  908. "failed to request write-protect gpio!\n");
  909. goto disable_clk;
  910. }
  911. host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  912. }
  913. /* card_detect */
  914. switch (boarddata->cd_type) {
  915. case ESDHC_CD_GPIO:
  916. err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
  917. if (err) {
  918. dev_err(mmc_dev(host->mmc),
  919. "failed to request card-detect gpio!\n");
  920. goto disable_clk;
  921. }
  922. /* fall through */
  923. case ESDHC_CD_CONTROLLER:
  924. /* we have a working card_detect back */
  925. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  926. break;
  927. case ESDHC_CD_PERMANENT:
  928. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  929. break;
  930. case ESDHC_CD_NONE:
  931. break;
  932. }
  933. switch (boarddata->max_bus_width) {
  934. case 8:
  935. host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  936. break;
  937. case 4:
  938. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  939. break;
  940. case 1:
  941. default:
  942. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  943. break;
  944. }
  945. /* sdr50 and sdr104 needs work on 1.8v signal voltage */
  946. if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
  947. imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
  948. ESDHC_PINCTRL_STATE_100MHZ);
  949. imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
  950. ESDHC_PINCTRL_STATE_200MHZ);
  951. if (IS_ERR(imx_data->pins_100mhz) ||
  952. IS_ERR(imx_data->pins_200mhz)) {
  953. dev_warn(mmc_dev(host->mmc),
  954. "could not get ultra high speed state, work on normal mode\n");
  955. /* fall back to not support uhs by specify no 1.8v quirk */
  956. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  957. }
  958. } else {
  959. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  960. }
  961. err = sdhci_add_host(host);
  962. if (err)
  963. goto disable_clk;
  964. pm_runtime_set_active(&pdev->dev);
  965. pm_runtime_enable(&pdev->dev);
  966. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  967. pm_runtime_use_autosuspend(&pdev->dev);
  968. pm_suspend_ignore_children(&pdev->dev, 1);
  969. return 0;
  970. disable_clk:
  971. clk_disable_unprepare(imx_data->clk_per);
  972. clk_disable_unprepare(imx_data->clk_ipg);
  973. clk_disable_unprepare(imx_data->clk_ahb);
  974. free_sdhci:
  975. sdhci_pltfm_free(pdev);
  976. return err;
  977. }
  978. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  979. {
  980. struct sdhci_host *host = platform_get_drvdata(pdev);
  981. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  982. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  983. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  984. sdhci_remove_host(host, dead);
  985. pm_runtime_dont_use_autosuspend(&pdev->dev);
  986. pm_runtime_disable(&pdev->dev);
  987. if (!IS_ENABLED(CONFIG_PM_RUNTIME)) {
  988. clk_disable_unprepare(imx_data->clk_per);
  989. clk_disable_unprepare(imx_data->clk_ipg);
  990. clk_disable_unprepare(imx_data->clk_ahb);
  991. }
  992. sdhci_pltfm_free(pdev);
  993. return 0;
  994. }
  995. #ifdef CONFIG_PM_RUNTIME
  996. static int sdhci_esdhc_runtime_suspend(struct device *dev)
  997. {
  998. struct sdhci_host *host = dev_get_drvdata(dev);
  999. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1000. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  1001. int ret;
  1002. ret = sdhci_runtime_suspend_host(host);
  1003. if (!sdhci_sdio_irq_enabled(host)) {
  1004. clk_disable_unprepare(imx_data->clk_per);
  1005. clk_disable_unprepare(imx_data->clk_ipg);
  1006. }
  1007. clk_disable_unprepare(imx_data->clk_ahb);
  1008. return ret;
  1009. }
  1010. static int sdhci_esdhc_runtime_resume(struct device *dev)
  1011. {
  1012. struct sdhci_host *host = dev_get_drvdata(dev);
  1013. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1014. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  1015. if (!sdhci_sdio_irq_enabled(host)) {
  1016. clk_prepare_enable(imx_data->clk_per);
  1017. clk_prepare_enable(imx_data->clk_ipg);
  1018. }
  1019. clk_prepare_enable(imx_data->clk_ahb);
  1020. return sdhci_runtime_resume_host(host);
  1021. }
  1022. #endif
  1023. static const struct dev_pm_ops sdhci_esdhc_pmops = {
  1024. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
  1025. SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
  1026. sdhci_esdhc_runtime_resume, NULL)
  1027. };
  1028. static struct platform_driver sdhci_esdhc_imx_driver = {
  1029. .driver = {
  1030. .name = "sdhci-esdhc-imx",
  1031. .owner = THIS_MODULE,
  1032. .of_match_table = imx_esdhc_dt_ids,
  1033. .pm = &sdhci_esdhc_pmops,
  1034. },
  1035. .id_table = imx_esdhc_devtype,
  1036. .probe = sdhci_esdhc_imx_probe,
  1037. .remove = sdhci_esdhc_imx_remove,
  1038. };
  1039. module_platform_driver(sdhci_esdhc_imx_driver);
  1040. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  1041. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  1042. MODULE_LICENSE("GPL v2");