sdhci-esdhc-imx.c 11 KB

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