sdhci-esdhc-imx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/platform_data/mmc-esdhc-imx.h>
  28. #include "sdhci-pltfm.h"
  29. #include "sdhci-esdhc.h"
  30. #define SDHCI_CTRL_D3CD 0x08
  31. /* VENDOR SPEC register */
  32. #define SDHCI_VENDOR_SPEC 0xC0
  33. #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
  34. #define SDHCI_WTMK_LVL 0x44
  35. #define SDHCI_MIX_CTRL 0x48
  36. /*
  37. * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
  38. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  39. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  40. * Define this macro DMA error INT for fsl eSDHC
  41. */
  42. #define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
  43. /*
  44. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  45. * "11" when the STOP CMD12 is issued on imx53 to abort one
  46. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  47. * be generated.
  48. * In exact block transfer, the controller doesn't complete the
  49. * operations automatically as required at the end of the
  50. * transfer and remains on hold if the abort command is not sent.
  51. * As a result, the TC flag is not asserted and SW received timeout
  52. * exeception. Bit1 of Vendor Spec registor is used to fix it.
  53. */
  54. #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
  55. enum imx_esdhc_type {
  56. IMX25_ESDHC,
  57. IMX35_ESDHC,
  58. IMX51_ESDHC,
  59. IMX53_ESDHC,
  60. IMX6Q_USDHC,
  61. };
  62. struct pltfm_imx_data {
  63. int flags;
  64. u32 scratchpad;
  65. enum imx_esdhc_type devtype;
  66. struct pinctrl *pinctrl;
  67. struct esdhc_platform_data boarddata;
  68. struct clk *clk_ipg;
  69. struct clk *clk_ahb;
  70. struct clk *clk_per;
  71. };
  72. static struct platform_device_id imx_esdhc_devtype[] = {
  73. {
  74. .name = "sdhci-esdhc-imx25",
  75. .driver_data = IMX25_ESDHC,
  76. }, {
  77. .name = "sdhci-esdhc-imx35",
  78. .driver_data = IMX35_ESDHC,
  79. }, {
  80. .name = "sdhci-esdhc-imx51",
  81. .driver_data = IMX51_ESDHC,
  82. }, {
  83. .name = "sdhci-esdhc-imx53",
  84. .driver_data = IMX53_ESDHC,
  85. }, {
  86. .name = "sdhci-usdhc-imx6q",
  87. .driver_data = IMX6Q_USDHC,
  88. }, {
  89. /* sentinel */
  90. }
  91. };
  92. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  93. static const struct of_device_id imx_esdhc_dt_ids[] = {
  94. { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
  95. { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
  96. { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
  97. { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
  98. { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
  99. { /* sentinel */ }
  100. };
  101. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  102. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  103. {
  104. return data->devtype == IMX25_ESDHC;
  105. }
  106. static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
  107. {
  108. return data->devtype == IMX35_ESDHC;
  109. }
  110. static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
  111. {
  112. return data->devtype == IMX51_ESDHC;
  113. }
  114. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  115. {
  116. return data->devtype == IMX53_ESDHC;
  117. }
  118. static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
  119. {
  120. return data->devtype == IMX6Q_USDHC;
  121. }
  122. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  123. {
  124. void __iomem *base = host->ioaddr + (reg & ~0x3);
  125. u32 shift = (reg & 0x3) * 8;
  126. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  127. }
  128. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  129. {
  130. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  131. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  132. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  133. /* fake CARD_PRESENT flag */
  134. u32 val = readl(host->ioaddr + reg);
  135. if (unlikely((reg == SDHCI_PRESENT_STATE)
  136. && gpio_is_valid(boarddata->cd_gpio))) {
  137. if (gpio_get_value(boarddata->cd_gpio))
  138. /* no card, if a valid gpio says so... */
  139. val &= ~SDHCI_CARD_PRESENT;
  140. else
  141. /* ... in all other cases assume card is present */
  142. val |= SDHCI_CARD_PRESENT;
  143. }
  144. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  145. /* In FSL esdhc IC module, only bit20 is used to indicate the
  146. * ADMA2 capability of esdhc, but this bit is messed up on
  147. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  148. * don't actually support ADMA2). So set the BROKEN_ADMA
  149. * uirk on MX25/35 platforms.
  150. */
  151. if (val & SDHCI_CAN_DO_ADMA1) {
  152. val &= ~SDHCI_CAN_DO_ADMA1;
  153. val |= SDHCI_CAN_DO_ADMA2;
  154. }
  155. }
  156. if (unlikely(reg == SDHCI_INT_STATUS)) {
  157. if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
  158. val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
  159. val |= SDHCI_INT_ADMA_ERROR;
  160. }
  161. }
  162. return val;
  163. }
  164. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  165. {
  166. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  167. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  168. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  169. u32 data;
  170. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  171. if (boarddata->cd_type == ESDHC_CD_GPIO)
  172. /*
  173. * These interrupts won't work with a custom
  174. * card_detect gpio (only applied to mx25/35)
  175. */
  176. val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  177. if (val & SDHCI_INT_CARD_INT) {
  178. /*
  179. * Clear and then set D3CD bit to avoid missing the
  180. * card interrupt. This is a eSDHC controller problem
  181. * so we need to apply the following workaround: clear
  182. * and set D3CD bit will make eSDHC re-sample the card
  183. * interrupt. In case a card interrupt was lost,
  184. * re-sample it by the following steps.
  185. */
  186. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  187. data &= ~SDHCI_CTRL_D3CD;
  188. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  189. data |= SDHCI_CTRL_D3CD;
  190. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  191. }
  192. }
  193. if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  194. && (reg == SDHCI_INT_STATUS)
  195. && (val & SDHCI_INT_DATA_END))) {
  196. u32 v;
  197. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  198. v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  199. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  200. }
  201. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  202. if (val & SDHCI_INT_ADMA_ERROR) {
  203. val &= ~SDHCI_INT_ADMA_ERROR;
  204. val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
  205. }
  206. }
  207. writel(val, host->ioaddr + reg);
  208. }
  209. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  210. {
  211. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  212. u16 val = readw(host->ioaddr + (reg ^ 2));
  213. /*
  214. * uSDHC supports SDHCI v3.0, but it's encoded as value
  215. * 0x3 in host controller version register, which violates
  216. * SDHCI_SPEC_300 definition. Work it around here.
  217. */
  218. if ((val & SDHCI_SPEC_VER_MASK) == 3)
  219. return --val;
  220. }
  221. return readw(host->ioaddr + reg);
  222. }
  223. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  224. {
  225. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  226. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  227. switch (reg) {
  228. case SDHCI_TRANSFER_MODE:
  229. /*
  230. * Postpone this write, we must do it together with a
  231. * command write that is down below.
  232. */
  233. if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  234. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  235. && (host->cmd->data->blocks > 1)
  236. && (host->cmd->data->flags & MMC_DATA_READ)) {
  237. u32 v;
  238. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  239. v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  240. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  241. }
  242. imx_data->scratchpad = val;
  243. return;
  244. case SDHCI_COMMAND:
  245. if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
  246. host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  247. (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  248. val |= SDHCI_CMD_ABORTCMD;
  249. if (is_imx6q_usdhc(imx_data)) {
  250. u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL);
  251. m = imx_data->scratchpad | (m & 0xffff0000);
  252. writel(m, host->ioaddr + SDHCI_MIX_CTRL);
  253. writel(val << 16,
  254. host->ioaddr + SDHCI_TRANSFER_MODE);
  255. } else {
  256. writel(val << 16 | imx_data->scratchpad,
  257. host->ioaddr + SDHCI_TRANSFER_MODE);
  258. }
  259. return;
  260. case SDHCI_BLOCK_SIZE:
  261. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  262. break;
  263. }
  264. esdhc_clrset_le(host, 0xffff, val, reg);
  265. }
  266. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  267. {
  268. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  269. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  270. u32 new_val;
  271. switch (reg) {
  272. case SDHCI_POWER_CONTROL:
  273. /*
  274. * FSL put some DMA bits here
  275. * If your board has a regulator, code should be here
  276. */
  277. return;
  278. case SDHCI_HOST_CONTROL:
  279. /* FSL messed up here, so we can just keep those three */
  280. new_val = val & (SDHCI_CTRL_LED | \
  281. SDHCI_CTRL_4BITBUS | \
  282. SDHCI_CTRL_D3CD);
  283. /* ensure the endianness */
  284. new_val |= ESDHC_HOST_CONTROL_LE;
  285. /* bits 8&9 are reserved on mx25 */
  286. if (!is_imx25_esdhc(imx_data)) {
  287. /* DMA mode bits are shifted */
  288. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  289. }
  290. esdhc_clrset_le(host, 0xffff, new_val, reg);
  291. return;
  292. }
  293. esdhc_clrset_le(host, 0xff, val, reg);
  294. /*
  295. * The esdhc has a design violation to SDHC spec which tells
  296. * that software reset should not affect card detection circuit.
  297. * But esdhc clears its SYSCTL register bits [0..2] during the
  298. * software reset. This will stop those clocks that card detection
  299. * circuit relies on. To work around it, we turn the clocks on back
  300. * to keep card detection circuit functional.
  301. */
  302. if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
  303. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  304. }
  305. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  306. {
  307. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  308. return clk_get_rate(pltfm_host->clk);
  309. }
  310. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  311. {
  312. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  313. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  314. }
  315. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  316. {
  317. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  318. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  319. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  320. switch (boarddata->wp_type) {
  321. case ESDHC_WP_GPIO:
  322. if (gpio_is_valid(boarddata->wp_gpio))
  323. return gpio_get_value(boarddata->wp_gpio);
  324. case ESDHC_WP_CONTROLLER:
  325. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  326. SDHCI_WRITE_PROTECT);
  327. case ESDHC_WP_NONE:
  328. break;
  329. }
  330. return -ENOSYS;
  331. }
  332. static struct sdhci_ops sdhci_esdhc_ops = {
  333. .read_l = esdhc_readl_le,
  334. .read_w = esdhc_readw_le,
  335. .write_l = esdhc_writel_le,
  336. .write_w = esdhc_writew_le,
  337. .write_b = esdhc_writeb_le,
  338. .set_clock = esdhc_set_clock,
  339. .get_max_clock = esdhc_pltfm_get_max_clock,
  340. .get_min_clock = esdhc_pltfm_get_min_clock,
  341. .get_ro = esdhc_pltfm_get_ro,
  342. };
  343. static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  344. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  345. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  346. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  347. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  348. .ops = &sdhci_esdhc_ops,
  349. };
  350. static irqreturn_t cd_irq(int irq, void *data)
  351. {
  352. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  353. tasklet_schedule(&sdhost->card_tasklet);
  354. return IRQ_HANDLED;
  355. };
  356. #ifdef CONFIG_OF
  357. static int
  358. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  359. struct esdhc_platform_data *boarddata)
  360. {
  361. struct device_node *np = pdev->dev.of_node;
  362. if (!np)
  363. return -ENODEV;
  364. if (of_get_property(np, "non-removable", NULL))
  365. boarddata->cd_type = ESDHC_CD_PERMANENT;
  366. if (of_get_property(np, "fsl,cd-controller", NULL))
  367. boarddata->cd_type = ESDHC_CD_CONTROLLER;
  368. if (of_get_property(np, "fsl,wp-controller", NULL))
  369. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  370. boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  371. if (gpio_is_valid(boarddata->cd_gpio))
  372. boarddata->cd_type = ESDHC_CD_GPIO;
  373. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  374. if (gpio_is_valid(boarddata->wp_gpio))
  375. boarddata->wp_type = ESDHC_WP_GPIO;
  376. return 0;
  377. }
  378. #else
  379. static inline int
  380. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  381. struct esdhc_platform_data *boarddata)
  382. {
  383. return -ENODEV;
  384. }
  385. #endif
  386. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  387. {
  388. const struct of_device_id *of_id =
  389. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  390. struct sdhci_pltfm_host *pltfm_host;
  391. struct sdhci_host *host;
  392. struct esdhc_platform_data *boarddata;
  393. int err;
  394. struct pltfm_imx_data *imx_data;
  395. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
  396. if (IS_ERR(host))
  397. return PTR_ERR(host);
  398. pltfm_host = sdhci_priv(host);
  399. imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
  400. if (!imx_data) {
  401. err = -ENOMEM;
  402. goto free_sdhci;
  403. }
  404. if (of_id)
  405. pdev->id_entry = of_id->data;
  406. imx_data->devtype = pdev->id_entry->driver_data;
  407. pltfm_host->priv = imx_data;
  408. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  409. if (IS_ERR(imx_data->clk_ipg)) {
  410. err = PTR_ERR(imx_data->clk_ipg);
  411. goto free_sdhci;
  412. }
  413. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  414. if (IS_ERR(imx_data->clk_ahb)) {
  415. err = PTR_ERR(imx_data->clk_ahb);
  416. goto free_sdhci;
  417. }
  418. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  419. if (IS_ERR(imx_data->clk_per)) {
  420. err = PTR_ERR(imx_data->clk_per);
  421. goto free_sdhci;
  422. }
  423. pltfm_host->clk = imx_data->clk_per;
  424. clk_prepare_enable(imx_data->clk_per);
  425. clk_prepare_enable(imx_data->clk_ipg);
  426. clk_prepare_enable(imx_data->clk_ahb);
  427. imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  428. if (IS_ERR(imx_data->pinctrl)) {
  429. err = PTR_ERR(imx_data->pinctrl);
  430. goto disable_clk;
  431. }
  432. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  433. if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
  434. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  435. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
  436. | SDHCI_QUIRK_BROKEN_ADMA;
  437. if (is_imx53_esdhc(imx_data))
  438. imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
  439. /*
  440. * The imx6q ROM code will change the default watermark level setting
  441. * to something insane. Change it back here.
  442. */
  443. if (is_imx6q_usdhc(imx_data))
  444. writel(0x08100810, host->ioaddr + SDHCI_WTMK_LVL);
  445. boarddata = &imx_data->boarddata;
  446. if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
  447. if (!host->mmc->parent->platform_data) {
  448. dev_err(mmc_dev(host->mmc), "no board data!\n");
  449. err = -EINVAL;
  450. goto disable_clk;
  451. }
  452. imx_data->boarddata = *((struct esdhc_platform_data *)
  453. host->mmc->parent->platform_data);
  454. }
  455. /* write_protect */
  456. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  457. err = devm_gpio_request_one(&pdev->dev, boarddata->wp_gpio,
  458. GPIOF_IN, "ESDHC_WP");
  459. if (err) {
  460. dev_warn(mmc_dev(host->mmc),
  461. "no write-protect pin available!\n");
  462. boarddata->wp_gpio = -EINVAL;
  463. }
  464. } else {
  465. boarddata->wp_gpio = -EINVAL;
  466. }
  467. /* card_detect */
  468. if (boarddata->cd_type != ESDHC_CD_GPIO)
  469. boarddata->cd_gpio = -EINVAL;
  470. switch (boarddata->cd_type) {
  471. case ESDHC_CD_GPIO:
  472. err = devm_gpio_request_one(&pdev->dev, boarddata->cd_gpio,
  473. GPIOF_IN, "ESDHC_CD");
  474. if (err) {
  475. dev_err(mmc_dev(host->mmc),
  476. "no card-detect pin available!\n");
  477. goto disable_clk;
  478. }
  479. err = devm_request_irq(&pdev->dev,
  480. gpio_to_irq(boarddata->cd_gpio), cd_irq,
  481. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  482. mmc_hostname(host->mmc), host);
  483. if (err) {
  484. dev_err(mmc_dev(host->mmc), "request irq error\n");
  485. goto disable_clk;
  486. }
  487. /* fall through */
  488. case ESDHC_CD_CONTROLLER:
  489. /* we have a working card_detect back */
  490. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  491. break;
  492. case ESDHC_CD_PERMANENT:
  493. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  494. break;
  495. case ESDHC_CD_NONE:
  496. break;
  497. }
  498. err = sdhci_add_host(host);
  499. if (err)
  500. goto disable_clk;
  501. return 0;
  502. disable_clk:
  503. clk_disable_unprepare(imx_data->clk_per);
  504. clk_disable_unprepare(imx_data->clk_ipg);
  505. clk_disable_unprepare(imx_data->clk_ahb);
  506. free_sdhci:
  507. sdhci_pltfm_free(pdev);
  508. return err;
  509. }
  510. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  511. {
  512. struct sdhci_host *host = platform_get_drvdata(pdev);
  513. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  514. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  515. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  516. sdhci_remove_host(host, dead);
  517. clk_disable_unprepare(imx_data->clk_per);
  518. clk_disable_unprepare(imx_data->clk_ipg);
  519. clk_disable_unprepare(imx_data->clk_ahb);
  520. sdhci_pltfm_free(pdev);
  521. return 0;
  522. }
  523. static struct platform_driver sdhci_esdhc_imx_driver = {
  524. .driver = {
  525. .name = "sdhci-esdhc-imx",
  526. .owner = THIS_MODULE,
  527. .of_match_table = imx_esdhc_dt_ids,
  528. .pm = SDHCI_PLTFM_PMOPS,
  529. },
  530. .id_table = imx_esdhc_devtype,
  531. .probe = sdhci_esdhc_imx_probe,
  532. .remove = sdhci_esdhc_imx_remove,
  533. };
  534. module_platform_driver(sdhci_esdhc_imx_driver);
  535. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  536. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  537. MODULE_LICENSE("GPL v2");