sdhci-esdhc-imx.c 17 KB

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