sdhci-esdhc-imx.c 28 KB

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