sdhci-esdhc-imx.c 31 KB

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