sdhci-esdhc-imx.c 17 KB

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