sdhci-pxav2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2010 Marvell International Ltd.
  3. * Zhangfei Gao <zhangfei.gao@marvell.com>
  4. * Kevin Wang <dwang4@marvell.com>
  5. * Jun Nie <njun@marvell.com>
  6. * Qiming Wu <wuqm@marvell.com>
  7. * Philip Rakity <prakity@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 "sdhci.h"
  30. #include "sdhci-pltfm.h"
  31. #define SD_FIFO_PARAM 0xe0
  32. #define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
  33. #define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
  34. #define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
  35. #define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
  36. CLK_GATE_ON | CLK_GATE_CTL)
  37. #define SD_CLOCK_BURST_SIZE_SETUP 0xe6
  38. #define SDCLK_SEL_SHIFT 8
  39. #define SDCLK_SEL_MASK 0x3
  40. #define SDCLK_DELAY_SHIFT 10
  41. #define SDCLK_DELAY_MASK 0x3c
  42. #define SD_CE_ATA_2 0xea
  43. #define MMC_CARD 0x1000
  44. #define MMC_WIDTH 0x0100
  45. static void pxav2_set_private_registers(struct sdhci_host *host, u8 mask)
  46. {
  47. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  48. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  49. if (mask == SDHCI_RESET_ALL) {
  50. u16 tmp = 0;
  51. /*
  52. * tune timing of read data/command when crc error happen
  53. * no performance impact
  54. */
  55. if (pdata->clk_delay_sel == 1) {
  56. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  57. tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT);
  58. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  59. << SDCLK_DELAY_SHIFT;
  60. tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT);
  61. tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT;
  62. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  63. }
  64. if (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING) {
  65. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  66. tmp &= ~CLK_GATE_SETTING_BITS;
  67. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  68. } else {
  69. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  70. tmp &= ~CLK_GATE_SETTING_BITS;
  71. tmp |= CLK_GATE_SETTING_BITS;
  72. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  73. }
  74. }
  75. }
  76. static int pxav2_mmc_set_width(struct sdhci_host *host, int width)
  77. {
  78. u8 ctrl;
  79. u16 tmp;
  80. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  81. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  82. if (width == MMC_BUS_WIDTH_8) {
  83. ctrl &= ~SDHCI_CTRL_4BITBUS;
  84. tmp |= MMC_CARD | MMC_WIDTH;
  85. } else {
  86. tmp &= ~(MMC_CARD | MMC_WIDTH);
  87. if (width == MMC_BUS_WIDTH_4)
  88. ctrl |= SDHCI_CTRL_4BITBUS;
  89. else
  90. ctrl &= ~SDHCI_CTRL_4BITBUS;
  91. }
  92. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  93. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  94. return 0;
  95. }
  96. static u32 pxav2_get_max_clock(struct sdhci_host *host)
  97. {
  98. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  99. return clk_get_rate(pltfm_host->clk);
  100. }
  101. static struct sdhci_ops pxav2_sdhci_ops = {
  102. .get_max_clock = pxav2_get_max_clock,
  103. .platform_reset_exit = pxav2_set_private_registers,
  104. .platform_8bit_width = pxav2_mmc_set_width,
  105. };
  106. static int __devinit sdhci_pxav2_probe(struct platform_device *pdev)
  107. {
  108. struct sdhci_pltfm_host *pltfm_host;
  109. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  110. struct device *dev = &pdev->dev;
  111. struct sdhci_host *host = NULL;
  112. struct sdhci_pxa *pxa = NULL;
  113. int ret;
  114. struct clk *clk;
  115. pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
  116. if (!pxa)
  117. return -ENOMEM;
  118. host = sdhci_pltfm_init(pdev, NULL);
  119. if (IS_ERR(host)) {
  120. kfree(pxa);
  121. return PTR_ERR(host);
  122. }
  123. pltfm_host = sdhci_priv(host);
  124. pltfm_host->priv = pxa;
  125. clk = clk_get(dev, "PXA-SDHCLK");
  126. if (IS_ERR(clk)) {
  127. dev_err(dev, "failed to get io clock\n");
  128. ret = PTR_ERR(clk);
  129. goto err_clk_get;
  130. }
  131. pltfm_host->clk = clk;
  132. clk_enable(clk);
  133. host->quirks = SDHCI_QUIRK_BROKEN_ADMA
  134. | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  135. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  136. if (pdata) {
  137. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  138. /* on-chip device */
  139. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  140. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  141. }
  142. /* If slot design supports 8 bit data, indicate this to MMC. */
  143. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  144. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  145. if (pdata->quirks)
  146. host->quirks |= pdata->quirks;
  147. if (pdata->host_caps)
  148. host->mmc->caps |= pdata->host_caps;
  149. if (pdata->pm_caps)
  150. host->mmc->pm_caps |= pdata->pm_caps;
  151. }
  152. host->ops = &pxav2_sdhci_ops;
  153. ret = sdhci_add_host(host);
  154. if (ret) {
  155. dev_err(&pdev->dev, "failed to add host\n");
  156. goto err_add_host;
  157. }
  158. platform_set_drvdata(pdev, host);
  159. return 0;
  160. err_add_host:
  161. clk_disable(clk);
  162. clk_put(clk);
  163. err_clk_get:
  164. sdhci_pltfm_free(pdev);
  165. kfree(pxa);
  166. return ret;
  167. }
  168. static int __devexit sdhci_pxav2_remove(struct platform_device *pdev)
  169. {
  170. struct sdhci_host *host = platform_get_drvdata(pdev);
  171. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  172. struct sdhci_pxa *pxa = pltfm_host->priv;
  173. sdhci_remove_host(host, 1);
  174. clk_disable(pltfm_host->clk);
  175. clk_put(pltfm_host->clk);
  176. sdhci_pltfm_free(pdev);
  177. kfree(pxa);
  178. platform_set_drvdata(pdev, NULL);
  179. return 0;
  180. }
  181. static struct platform_driver sdhci_pxav2_driver = {
  182. .driver = {
  183. .name = "sdhci-pxav2",
  184. .owner = THIS_MODULE,
  185. },
  186. .probe = sdhci_pxav2_probe,
  187. .remove = __devexit_p(sdhci_pxav2_remove),
  188. #ifdef CONFIG_PM
  189. .suspend = sdhci_pltfm_suspend,
  190. .resume = sdhci_pltfm_resume,
  191. #endif
  192. };
  193. static int __init sdhci_pxav2_init(void)
  194. {
  195. return platform_driver_register(&sdhci_pxav2_driver);
  196. }
  197. static void __exit sdhci_pxav2_exit(void)
  198. {
  199. platform_driver_unregister(&sdhci_pxav2_driver);
  200. }
  201. module_init(sdhci_pxav2_init);
  202. module_exit(sdhci_pxav2_exit);
  203. MODULE_DESCRIPTION("SDHCI driver for pxav2");
  204. MODULE_AUTHOR("Marvell International Ltd.");
  205. MODULE_LICENSE("GPL v2");