sdhci-esdhc-imx.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/sdhci-pltfm.h>
  21. #include <linux/mmc/mmc.h>
  22. #include <linux/mmc/sdio.h>
  23. #include <mach/hardware.h>
  24. #include <mach/esdhc.h>
  25. #include "sdhci.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. #define ESDHC_FLAG_GPIO_FOR_CD_WP (1 << 0)
  32. /*
  33. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  34. * "11" when the STOP CMD12 is issued on imx53 to abort one
  35. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  36. * be generated.
  37. * In exact block transfer, the controller doesn't complete the
  38. * operations automatically as required at the end of the
  39. * transfer and remains on hold if the abort command is not sent.
  40. * As a result, the TC flag is not asserted and SW received timeout
  41. * exeception. Bit1 of Vendor Spec registor is used to fix it.
  42. */
  43. #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
  44. struct pltfm_imx_data {
  45. int flags;
  46. u32 scratchpad;
  47. };
  48. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  49. {
  50. void __iomem *base = host->ioaddr + (reg & ~0x3);
  51. u32 shift = (reg & 0x3) * 8;
  52. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  53. }
  54. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  55. {
  56. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  57. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  58. /* fake CARD_PRESENT flag on mx25/35 */
  59. u32 val = readl(host->ioaddr + reg);
  60. if (unlikely((reg == SDHCI_PRESENT_STATE)
  61. && (imx_data->flags & ESDHC_FLAG_GPIO_FOR_CD_WP))) {
  62. struct esdhc_platform_data *boarddata =
  63. host->mmc->parent->platform_data;
  64. if (boarddata && gpio_is_valid(boarddata->cd_gpio)
  65. && gpio_get_value(boarddata->cd_gpio))
  66. /* no card, if a valid gpio says so... */
  67. val &= SDHCI_CARD_PRESENT;
  68. else
  69. /* ... in all other cases assume card is present */
  70. val |= SDHCI_CARD_PRESENT;
  71. }
  72. return val;
  73. }
  74. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  75. {
  76. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  77. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  78. if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)
  79. && (imx_data->flags & ESDHC_FLAG_GPIO_FOR_CD_WP)))
  80. /*
  81. * these interrupts won't work with a custom card_detect gpio
  82. * (only applied to mx25/35)
  83. */
  84. val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  85. if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  86. && (reg == SDHCI_INT_STATUS)
  87. && (val & SDHCI_INT_DATA_END))) {
  88. u32 v;
  89. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  90. v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  91. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  92. }
  93. writel(val, host->ioaddr + reg);
  94. }
  95. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  96. {
  97. if (unlikely(reg == SDHCI_HOST_VERSION))
  98. reg ^= 2;
  99. return readw(host->ioaddr + reg);
  100. }
  101. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  102. {
  103. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  104. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  105. switch (reg) {
  106. case SDHCI_TRANSFER_MODE:
  107. /*
  108. * Postpone this write, we must do it together with a
  109. * command write that is down below.
  110. */
  111. if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  112. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  113. && (host->cmd->data->blocks > 1)
  114. && (host->cmd->data->flags & MMC_DATA_READ)) {
  115. u32 v;
  116. v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
  117. v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
  118. writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
  119. }
  120. imx_data->scratchpad = val;
  121. return;
  122. case SDHCI_COMMAND:
  123. if ((host->cmd->opcode == MMC_STOP_TRANSMISSION)
  124. && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  125. val |= SDHCI_CMD_ABORTCMD;
  126. writel(val << 16 | imx_data->scratchpad,
  127. host->ioaddr + SDHCI_TRANSFER_MODE);
  128. return;
  129. case SDHCI_BLOCK_SIZE:
  130. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  131. break;
  132. }
  133. esdhc_clrset_le(host, 0xffff, val, reg);
  134. }
  135. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  136. {
  137. u32 new_val;
  138. switch (reg) {
  139. case SDHCI_POWER_CONTROL:
  140. /*
  141. * FSL put some DMA bits here
  142. * If your board has a regulator, code should be here
  143. */
  144. return;
  145. case SDHCI_HOST_CONTROL:
  146. /* FSL messed up here, so we can just keep those two */
  147. new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
  148. /* ensure the endianess */
  149. new_val |= ESDHC_HOST_CONTROL_LE;
  150. /* DMA mode bits are shifted */
  151. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  152. esdhc_clrset_le(host, 0xffff, new_val, reg);
  153. return;
  154. }
  155. esdhc_clrset_le(host, 0xff, val, reg);
  156. }
  157. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  158. {
  159. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  160. return clk_get_rate(pltfm_host->clk);
  161. }
  162. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  163. {
  164. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  165. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  166. }
  167. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  168. {
  169. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  170. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  171. return gpio_get_value(boarddata->wp_gpio);
  172. else
  173. return -ENOSYS;
  174. }
  175. static struct sdhci_ops sdhci_esdhc_ops = {
  176. .read_l = esdhc_readl_le,
  177. .read_w = esdhc_readw_le,
  178. .write_l = esdhc_writel_le,
  179. .write_w = esdhc_writew_le,
  180. .write_b = esdhc_writeb_le,
  181. .set_clock = esdhc_set_clock,
  182. .get_max_clock = esdhc_pltfm_get_max_clock,
  183. .get_min_clock = esdhc_pltfm_get_min_clock,
  184. };
  185. static irqreturn_t cd_irq(int irq, void *data)
  186. {
  187. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  188. tasklet_schedule(&sdhost->card_tasklet);
  189. return IRQ_HANDLED;
  190. };
  191. static int esdhc_pltfm_init(struct sdhci_host *host, struct sdhci_pltfm_data *pdata)
  192. {
  193. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  194. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  195. struct clk *clk;
  196. int err;
  197. struct pltfm_imx_data *imx_data;
  198. clk = clk_get(mmc_dev(host->mmc), NULL);
  199. if (IS_ERR(clk)) {
  200. dev_err(mmc_dev(host->mmc), "clk err\n");
  201. return PTR_ERR(clk);
  202. }
  203. clk_enable(clk);
  204. pltfm_host->clk = clk;
  205. imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
  206. if (!imx_data) {
  207. clk_disable(pltfm_host->clk);
  208. clk_put(pltfm_host->clk);
  209. return -ENOMEM;
  210. }
  211. pltfm_host->priv = imx_data;
  212. if (!cpu_is_mx25())
  213. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  214. if (cpu_is_mx25() || cpu_is_mx35()) {
  215. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  216. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
  217. /* write_protect can't be routed to controller, use gpio */
  218. sdhci_esdhc_ops.get_ro = esdhc_pltfm_get_ro;
  219. }
  220. if (!(cpu_is_mx25() || cpu_is_mx35() || cpu_is_mx51()))
  221. imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
  222. if (boarddata) {
  223. err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
  224. if (err) {
  225. dev_warn(mmc_dev(host->mmc),
  226. "no write-protect pin available!\n");
  227. boarddata->wp_gpio = err;
  228. }
  229. err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
  230. if (err) {
  231. dev_warn(mmc_dev(host->mmc),
  232. "no card-detect pin available!\n");
  233. goto no_card_detect_pin;
  234. }
  235. /* i.MX5x has issues to be researched */
  236. if (!cpu_is_mx25() && !cpu_is_mx35())
  237. goto not_supported;
  238. err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
  239. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  240. mmc_hostname(host->mmc), host);
  241. if (err) {
  242. dev_warn(mmc_dev(host->mmc), "request irq error\n");
  243. goto no_card_detect_irq;
  244. }
  245. imx_data->flags |= ESDHC_FLAG_GPIO_FOR_CD_WP;
  246. /* Now we have a working card_detect again */
  247. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  248. }
  249. return 0;
  250. no_card_detect_irq:
  251. gpio_free(boarddata->cd_gpio);
  252. no_card_detect_pin:
  253. boarddata->cd_gpio = err;
  254. not_supported:
  255. kfree(imx_data);
  256. return 0;
  257. }
  258. static void esdhc_pltfm_exit(struct sdhci_host *host)
  259. {
  260. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  261. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  262. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  263. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  264. gpio_free(boarddata->wp_gpio);
  265. if (boarddata && gpio_is_valid(boarddata->cd_gpio)) {
  266. gpio_free(boarddata->cd_gpio);
  267. if (!(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION))
  268. free_irq(gpio_to_irq(boarddata->cd_gpio), host);
  269. }
  270. clk_disable(pltfm_host->clk);
  271. clk_put(pltfm_host->clk);
  272. kfree(imx_data);
  273. }
  274. struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  275. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
  276. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  277. /* ADMA has issues. Might be fixable */
  278. .ops = &sdhci_esdhc_ops,
  279. .init = esdhc_pltfm_init,
  280. .exit = esdhc_pltfm_exit,
  281. };