sdhci-esdhc-imx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/slab.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/mmc.h>
  21. #include <linux/mmc/sdio.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #include <mach/esdhc.h>
  26. #include "sdhci-pltfm.h"
  27. #include "sdhci-esdhc.h"
  28. /* VENDOR SPEC register */
  29. #define SDHCI_VENDOR_SPEC 0xC0
  30. #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
  31. /*
  32. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  33. * "11" when the STOP CMD12 is issued on imx53 to abort one
  34. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  35. * be generated.
  36. * In exact block transfer, the controller doesn't complete the
  37. * operations automatically as required at the end of the
  38. * transfer and remains on hold if the abort command is not sent.
  39. * As a result, the TC flag is not asserted and SW received timeout
  40. * exeception. Bit1 of Vendor Spec registor is used to fix it.
  41. */
  42. #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
  43. enum imx_esdhc_type {
  44. IMX25_ESDHC,
  45. IMX35_ESDHC,
  46. IMX51_ESDHC,
  47. IMX53_ESDHC,
  48. };
  49. struct pltfm_imx_data {
  50. int flags;
  51. u32 scratchpad;
  52. enum imx_esdhc_type devtype;
  53. struct esdhc_platform_data boarddata;
  54. };
  55. static struct platform_device_id imx_esdhc_devtype[] = {
  56. {
  57. .name = "sdhci-esdhc-imx25",
  58. .driver_data = IMX25_ESDHC,
  59. }, {
  60. .name = "sdhci-esdhc-imx35",
  61. .driver_data = IMX35_ESDHC,
  62. }, {
  63. .name = "sdhci-esdhc-imx51",
  64. .driver_data = IMX51_ESDHC,
  65. }, {
  66. .name = "sdhci-esdhc-imx53",
  67. .driver_data = IMX53_ESDHC,
  68. }, {
  69. /* sentinel */
  70. }
  71. };
  72. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  73. static const struct of_device_id imx_esdhc_dt_ids[] = {
  74. { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
  75. { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
  76. { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
  77. { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
  78. { /* sentinel */ }
  79. };
  80. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  81. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  82. {
  83. return data->devtype == IMX25_ESDHC;
  84. }
  85. static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
  86. {
  87. return data->devtype == IMX35_ESDHC;
  88. }
  89. static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
  90. {
  91. return data->devtype == IMX51_ESDHC;
  92. }
  93. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  94. {
  95. return data->devtype == IMX53_ESDHC;
  96. }
  97. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  98. {
  99. void __iomem *base = host->ioaddr + (reg & ~0x3);
  100. u32 shift = (reg & 0x3) * 8;
  101. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  102. }
  103. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  104. {
  105. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  106. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  107. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  108. /* fake CARD_PRESENT flag */
  109. u32 val = readl(host->ioaddr + reg);
  110. if (unlikely((reg == SDHCI_PRESENT_STATE)
  111. && gpio_is_valid(boarddata->cd_gpio))) {
  112. if (gpio_get_value(boarddata->cd_gpio))
  113. /* no card, if a valid gpio says so... */
  114. val &= ~SDHCI_CARD_PRESENT;
  115. else
  116. /* ... in all other cases assume card is present */
  117. val |= SDHCI_CARD_PRESENT;
  118. }
  119. return val;
  120. }
  121. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  122. {
  123. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  124. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  125. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  126. if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)
  127. && (boarddata->cd_type == ESDHC_CD_GPIO)))
  128. /*
  129. * these interrupts won't work with a custom card_detect gpio
  130. */
  131. val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  132. if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  133. && (reg == SDHCI_INT_STATUS)
  134. && (val & SDHCI_INT_DATA_END))) {
  135. u32 v;
  136. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  137. v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  138. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  139. }
  140. writel(val, host->ioaddr + reg);
  141. }
  142. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  143. {
  144. if (unlikely(reg == SDHCI_HOST_VERSION))
  145. reg ^= 2;
  146. return readw(host->ioaddr + reg);
  147. }
  148. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  149. {
  150. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  151. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  152. switch (reg) {
  153. case SDHCI_TRANSFER_MODE:
  154. /*
  155. * Postpone this write, we must do it together with a
  156. * command write that is down below.
  157. */
  158. if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  159. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  160. && (host->cmd->data->blocks > 1)
  161. && (host->cmd->data->flags & MMC_DATA_READ)) {
  162. u32 v;
  163. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  164. v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  165. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  166. }
  167. imx_data->scratchpad = val;
  168. return;
  169. case SDHCI_COMMAND:
  170. if ((host->cmd->opcode == MMC_STOP_TRANSMISSION)
  171. && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  172. val |= SDHCI_CMD_ABORTCMD;
  173. writel(val << 16 | imx_data->scratchpad,
  174. host->ioaddr + SDHCI_TRANSFER_MODE);
  175. return;
  176. case SDHCI_BLOCK_SIZE:
  177. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  178. break;
  179. }
  180. esdhc_clrset_le(host, 0xffff, val, reg);
  181. }
  182. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  183. {
  184. u32 new_val;
  185. switch (reg) {
  186. case SDHCI_POWER_CONTROL:
  187. /*
  188. * FSL put some DMA bits here
  189. * If your board has a regulator, code should be here
  190. */
  191. return;
  192. case SDHCI_HOST_CONTROL:
  193. /* FSL messed up here, so we can just keep those two */
  194. new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
  195. /* ensure the endianess */
  196. new_val |= ESDHC_HOST_CONTROL_LE;
  197. /* DMA mode bits are shifted */
  198. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  199. esdhc_clrset_le(host, 0xffff, new_val, reg);
  200. return;
  201. }
  202. esdhc_clrset_le(host, 0xff, val, reg);
  203. /*
  204. * The esdhc has a design violation to SDHC spec which tells
  205. * that software reset should not affect card detection circuit.
  206. * But esdhc clears its SYSCTL register bits [0..2] during the
  207. * software reset. This will stop those clocks that card detection
  208. * circuit relies on. To work around it, we turn the clocks on back
  209. * to keep card detection circuit functional.
  210. */
  211. if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
  212. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  213. }
  214. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  215. {
  216. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  217. return clk_get_rate(pltfm_host->clk);
  218. }
  219. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  220. {
  221. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  222. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  223. }
  224. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  225. {
  226. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  227. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  228. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  229. switch (boarddata->wp_type) {
  230. case ESDHC_WP_GPIO:
  231. if (gpio_is_valid(boarddata->wp_gpio))
  232. return gpio_get_value(boarddata->wp_gpio);
  233. case ESDHC_WP_CONTROLLER:
  234. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  235. SDHCI_WRITE_PROTECT);
  236. case ESDHC_WP_NONE:
  237. break;
  238. }
  239. return -ENOSYS;
  240. }
  241. static struct sdhci_ops sdhci_esdhc_ops = {
  242. .read_l = esdhc_readl_le,
  243. .read_w = esdhc_readw_le,
  244. .write_l = esdhc_writel_le,
  245. .write_w = esdhc_writew_le,
  246. .write_b = esdhc_writeb_le,
  247. .set_clock = esdhc_set_clock,
  248. .get_max_clock = esdhc_pltfm_get_max_clock,
  249. .get_min_clock = esdhc_pltfm_get_min_clock,
  250. .get_ro = esdhc_pltfm_get_ro,
  251. };
  252. static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  253. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
  254. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  255. /* ADMA has issues. Might be fixable */
  256. .ops = &sdhci_esdhc_ops,
  257. };
  258. static irqreturn_t cd_irq(int irq, void *data)
  259. {
  260. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  261. tasklet_schedule(&sdhost->card_tasklet);
  262. return IRQ_HANDLED;
  263. };
  264. #ifdef CONFIG_OF
  265. static int __devinit
  266. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  267. struct esdhc_platform_data *boarddata)
  268. {
  269. struct device_node *np = pdev->dev.of_node;
  270. if (!np)
  271. return -ENODEV;
  272. if (of_get_property(np, "fsl,card-wired", NULL))
  273. boarddata->cd_type = ESDHC_CD_PERMANENT;
  274. if (of_get_property(np, "fsl,cd-controller", NULL))
  275. boarddata->cd_type = ESDHC_CD_CONTROLLER;
  276. if (of_get_property(np, "fsl,wp-controller", NULL))
  277. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  278. boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  279. if (gpio_is_valid(boarddata->cd_gpio))
  280. boarddata->cd_type = ESDHC_CD_GPIO;
  281. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  282. if (gpio_is_valid(boarddata->wp_gpio))
  283. boarddata->wp_type = ESDHC_WP_GPIO;
  284. return 0;
  285. }
  286. #else
  287. static inline int
  288. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  289. struct esdhc_platform_data *boarddata)
  290. {
  291. return -ENODEV;
  292. }
  293. #endif
  294. static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
  295. {
  296. const struct of_device_id *of_id =
  297. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  298. struct sdhci_pltfm_host *pltfm_host;
  299. struct sdhci_host *host;
  300. struct esdhc_platform_data *boarddata;
  301. struct clk *clk;
  302. int err;
  303. struct pltfm_imx_data *imx_data;
  304. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
  305. if (IS_ERR(host))
  306. return PTR_ERR(host);
  307. pltfm_host = sdhci_priv(host);
  308. imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
  309. if (!imx_data) {
  310. err = -ENOMEM;
  311. goto err_imx_data;
  312. }
  313. if (of_id)
  314. pdev->id_entry = of_id->data;
  315. imx_data->devtype = pdev->id_entry->driver_data;
  316. pltfm_host->priv = imx_data;
  317. clk = clk_get(mmc_dev(host->mmc), NULL);
  318. if (IS_ERR(clk)) {
  319. dev_err(mmc_dev(host->mmc), "clk err\n");
  320. err = PTR_ERR(clk);
  321. goto err_clk_get;
  322. }
  323. clk_enable(clk);
  324. pltfm_host->clk = clk;
  325. if (!is_imx25_esdhc(imx_data))
  326. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  327. if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
  328. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  329. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
  330. if (is_imx53_esdhc(imx_data))
  331. imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
  332. boarddata = &imx_data->boarddata;
  333. if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
  334. if (!host->mmc->parent->platform_data) {
  335. dev_err(mmc_dev(host->mmc), "no board data!\n");
  336. err = -EINVAL;
  337. goto no_board_data;
  338. }
  339. imx_data->boarddata = *((struct esdhc_platform_data *)
  340. host->mmc->parent->platform_data);
  341. }
  342. /* write_protect */
  343. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  344. err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
  345. if (err) {
  346. dev_warn(mmc_dev(host->mmc),
  347. "no write-protect pin available!\n");
  348. boarddata->wp_gpio = -EINVAL;
  349. }
  350. } else {
  351. boarddata->wp_gpio = -EINVAL;
  352. }
  353. /* card_detect */
  354. if (boarddata->cd_type != ESDHC_CD_GPIO)
  355. boarddata->cd_gpio = -EINVAL;
  356. switch (boarddata->cd_type) {
  357. case ESDHC_CD_GPIO:
  358. err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
  359. if (err) {
  360. dev_err(mmc_dev(host->mmc),
  361. "no card-detect pin available!\n");
  362. goto no_card_detect_pin;
  363. }
  364. err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
  365. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  366. mmc_hostname(host->mmc), host);
  367. if (err) {
  368. dev_err(mmc_dev(host->mmc), "request irq error\n");
  369. goto no_card_detect_irq;
  370. }
  371. /* fall through */
  372. case ESDHC_CD_CONTROLLER:
  373. /* we have a working card_detect back */
  374. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  375. break;
  376. case ESDHC_CD_PERMANENT:
  377. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  378. break;
  379. case ESDHC_CD_NONE:
  380. break;
  381. }
  382. err = sdhci_add_host(host);
  383. if (err)
  384. goto err_add_host;
  385. return 0;
  386. err_add_host:
  387. if (gpio_is_valid(boarddata->cd_gpio))
  388. free_irq(gpio_to_irq(boarddata->cd_gpio), host);
  389. no_card_detect_irq:
  390. if (gpio_is_valid(boarddata->cd_gpio))
  391. gpio_free(boarddata->cd_gpio);
  392. if (gpio_is_valid(boarddata->wp_gpio))
  393. gpio_free(boarddata->wp_gpio);
  394. no_card_detect_pin:
  395. no_board_data:
  396. clk_disable(pltfm_host->clk);
  397. clk_put(pltfm_host->clk);
  398. err_clk_get:
  399. kfree(imx_data);
  400. err_imx_data:
  401. sdhci_pltfm_free(pdev);
  402. return err;
  403. }
  404. static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
  405. {
  406. struct sdhci_host *host = platform_get_drvdata(pdev);
  407. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  408. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  409. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  410. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  411. sdhci_remove_host(host, dead);
  412. if (gpio_is_valid(boarddata->wp_gpio))
  413. gpio_free(boarddata->wp_gpio);
  414. if (gpio_is_valid(boarddata->cd_gpio)) {
  415. free_irq(gpio_to_irq(boarddata->cd_gpio), host);
  416. gpio_free(boarddata->cd_gpio);
  417. }
  418. clk_disable(pltfm_host->clk);
  419. clk_put(pltfm_host->clk);
  420. kfree(imx_data);
  421. sdhci_pltfm_free(pdev);
  422. return 0;
  423. }
  424. static struct platform_driver sdhci_esdhc_imx_driver = {
  425. .driver = {
  426. .name = "sdhci-esdhc-imx",
  427. .owner = THIS_MODULE,
  428. .of_match_table = imx_esdhc_dt_ids,
  429. },
  430. .id_table = imx_esdhc_devtype,
  431. .probe = sdhci_esdhc_imx_probe,
  432. .remove = __devexit_p(sdhci_esdhc_imx_remove),
  433. #ifdef CONFIG_PM
  434. .suspend = sdhci_pltfm_suspend,
  435. .resume = sdhci_pltfm_resume,
  436. #endif
  437. };
  438. static int __init sdhci_esdhc_imx_init(void)
  439. {
  440. return platform_driver_register(&sdhci_esdhc_imx_driver);
  441. }
  442. module_init(sdhci_esdhc_imx_init);
  443. static void __exit sdhci_esdhc_imx_exit(void)
  444. {
  445. platform_driver_unregister(&sdhci_esdhc_imx_driver);
  446. }
  447. module_exit(sdhci_esdhc_imx_exit);
  448. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  449. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  450. MODULE_LICENSE("GPL v2");