sdhci-pxav3.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2010 Marvell International Ltd.
  3. * Zhangfei Gao <zhangfei.gao@marvell.com>
  4. * Kevin Wang <dwang4@marvell.com>
  5. * Mingwei Wang <mwwang@marvell.com>
  6. * Philip Rakity <prakity@marvell.com>
  7. * Mark Brown <markb@marvell.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/platform_data/pxa_sdhci.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include "sdhci.h"
  34. #include "sdhci-pltfm.h"
  35. #define SD_CLOCK_BURST_SIZE_SETUP 0x10A
  36. #define SDCLK_SEL 0x100
  37. #define SDCLK_DELAY_SHIFT 9
  38. #define SDCLK_DELAY_MASK 0x1f
  39. #define SD_CFG_FIFO_PARAM 0x100
  40. #define SDCFG_GEN_PAD_CLK_ON (1<<6)
  41. #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
  42. #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
  43. #define SD_SPI_MODE 0x108
  44. #define SD_CE_ATA_1 0x10C
  45. #define SD_CE_ATA_2 0x10E
  46. #define SDCE_MISC_INT (1<<2)
  47. #define SDCE_MISC_INT_EN (1<<1)
  48. static void pxav3_set_private_registers(struct sdhci_host *host, u8 mask)
  49. {
  50. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  51. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  52. if (mask == SDHCI_RESET_ALL) {
  53. /*
  54. * tune timing of read data/command when crc error happen
  55. * no performance impact
  56. */
  57. if (pdata && 0 != pdata->clk_delay_cycles) {
  58. u16 tmp;
  59. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  60. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  61. << SDCLK_DELAY_SHIFT;
  62. tmp |= SDCLK_SEL;
  63. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  64. }
  65. }
  66. }
  67. #define MAX_WAIT_COUNT 5
  68. static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  69. {
  70. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  71. struct sdhci_pxa *pxa = pltfm_host->priv;
  72. u16 tmp;
  73. int count;
  74. if (pxa->power_mode == MMC_POWER_UP
  75. && power_mode == MMC_POWER_ON) {
  76. dev_dbg(mmc_dev(host->mmc),
  77. "%s: slot->power_mode = %d,"
  78. "ios->power_mode = %d\n",
  79. __func__,
  80. pxa->power_mode,
  81. power_mode);
  82. /* set we want notice of when 74 clocks are sent */
  83. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  84. tmp |= SDCE_MISC_INT_EN;
  85. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  86. /* start sending the 74 clocks */
  87. tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
  88. tmp |= SDCFG_GEN_PAD_CLK_ON;
  89. writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
  90. /* slowest speed is about 100KHz or 10usec per clock */
  91. udelay(740);
  92. count = 0;
  93. while (count++ < MAX_WAIT_COUNT) {
  94. if ((readw(host->ioaddr + SD_CE_ATA_2)
  95. & SDCE_MISC_INT) == 0)
  96. break;
  97. udelay(10);
  98. }
  99. if (count == MAX_WAIT_COUNT)
  100. dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
  101. /* clear the interrupt bit if posted */
  102. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  103. tmp |= SDCE_MISC_INT;
  104. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  105. }
  106. pxa->power_mode = power_mode;
  107. }
  108. static int pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
  109. {
  110. u16 ctrl_2;
  111. /*
  112. * Set V18_EN -- UHS modes do not work without this.
  113. * does not change signaling voltage
  114. */
  115. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  116. /* Select Bus Speed Mode for host */
  117. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  118. switch (uhs) {
  119. case MMC_TIMING_UHS_SDR12:
  120. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  121. break;
  122. case MMC_TIMING_UHS_SDR25:
  123. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  124. break;
  125. case MMC_TIMING_UHS_SDR50:
  126. ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
  127. break;
  128. case MMC_TIMING_UHS_SDR104:
  129. ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
  130. break;
  131. case MMC_TIMING_UHS_DDR50:
  132. ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
  133. break;
  134. }
  135. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  136. dev_dbg(mmc_dev(host->mmc),
  137. "%s uhs = %d, ctrl_2 = %04X\n",
  138. __func__, uhs, ctrl_2);
  139. return 0;
  140. }
  141. static struct sdhci_ops pxav3_sdhci_ops = {
  142. .platform_reset_exit = pxav3_set_private_registers,
  143. .set_uhs_signaling = pxav3_set_uhs_signaling,
  144. .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
  145. };
  146. #ifdef CONFIG_OF
  147. static const struct of_device_id sdhci_pxav3_of_match[] = {
  148. {
  149. .compatible = "mrvl,pxav3-mmc",
  150. },
  151. {},
  152. };
  153. MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
  154. static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  155. {
  156. struct sdhci_pxa_platdata *pdata;
  157. struct device_node *np = dev->of_node;
  158. u32 bus_width;
  159. u32 clk_delay_cycles;
  160. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  161. if (!pdata)
  162. return NULL;
  163. if (of_find_property(np, "non-removable", NULL))
  164. pdata->flags |= PXA_FLAG_CARD_PERMANENT;
  165. of_property_read_u32(np, "bus-width", &bus_width);
  166. if (bus_width == 8)
  167. pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
  168. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  169. if (clk_delay_cycles > 0)
  170. pdata->clk_delay_cycles = clk_delay_cycles;
  171. return pdata;
  172. }
  173. #else
  174. static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  175. {
  176. return NULL;
  177. }
  178. #endif
  179. static int __devinit sdhci_pxav3_probe(struct platform_device *pdev)
  180. {
  181. struct sdhci_pltfm_host *pltfm_host;
  182. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  183. struct device *dev = &pdev->dev;
  184. struct sdhci_host *host = NULL;
  185. struct sdhci_pxa *pxa = NULL;
  186. const struct of_device_id *match;
  187. int ret;
  188. struct clk *clk;
  189. pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
  190. if (!pxa)
  191. return -ENOMEM;
  192. host = sdhci_pltfm_init(pdev, NULL);
  193. if (IS_ERR(host)) {
  194. kfree(pxa);
  195. return PTR_ERR(host);
  196. }
  197. pltfm_host = sdhci_priv(host);
  198. pltfm_host->priv = pxa;
  199. clk = clk_get(dev, "PXA-SDHCLK");
  200. if (IS_ERR(clk)) {
  201. dev_err(dev, "failed to get io clock\n");
  202. ret = PTR_ERR(clk);
  203. goto err_clk_get;
  204. }
  205. pltfm_host->clk = clk;
  206. clk_enable(clk);
  207. host->quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  208. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  209. | SDHCI_QUIRK_32BIT_ADMA_SIZE;
  210. /* enable 1/8V DDR capable */
  211. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  212. match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
  213. if (match)
  214. pdata = pxav3_get_mmc_pdata(dev);
  215. if (pdata) {
  216. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  217. /* on-chip device */
  218. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  219. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  220. }
  221. /* If slot design supports 8 bit data, indicate this to MMC. */
  222. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  223. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  224. if (pdata->quirks)
  225. host->quirks |= pdata->quirks;
  226. if (pdata->host_caps)
  227. host->mmc->caps |= pdata->host_caps;
  228. if (pdata->pm_caps)
  229. host->mmc->pm_caps |= pdata->pm_caps;
  230. }
  231. host->ops = &pxav3_sdhci_ops;
  232. ret = sdhci_add_host(host);
  233. if (ret) {
  234. dev_err(&pdev->dev, "failed to add host\n");
  235. goto err_add_host;
  236. }
  237. platform_set_drvdata(pdev, host);
  238. return 0;
  239. err_add_host:
  240. clk_disable(clk);
  241. clk_put(clk);
  242. err_clk_get:
  243. sdhci_pltfm_free(pdev);
  244. kfree(pxa);
  245. return ret;
  246. }
  247. static int __devexit sdhci_pxav3_remove(struct platform_device *pdev)
  248. {
  249. struct sdhci_host *host = platform_get_drvdata(pdev);
  250. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  251. struct sdhci_pxa *pxa = pltfm_host->priv;
  252. sdhci_remove_host(host, 1);
  253. clk_disable(pltfm_host->clk);
  254. clk_put(pltfm_host->clk);
  255. sdhci_pltfm_free(pdev);
  256. kfree(pxa);
  257. platform_set_drvdata(pdev, NULL);
  258. return 0;
  259. }
  260. static struct platform_driver sdhci_pxav3_driver = {
  261. .driver = {
  262. .name = "sdhci-pxav3",
  263. #ifdef CONFIG_OF
  264. .of_match_table = sdhci_pxav3_of_match,
  265. #endif
  266. .owner = THIS_MODULE,
  267. .pm = SDHCI_PLTFM_PMOPS,
  268. },
  269. .probe = sdhci_pxav3_probe,
  270. .remove = __devexit_p(sdhci_pxav3_remove),
  271. };
  272. module_platform_driver(sdhci_pxav3_driver);
  273. MODULE_DESCRIPTION("SDHCI driver for pxav3");
  274. MODULE_AUTHOR("Marvell International Ltd.");
  275. MODULE_LICENSE("GPL v2");