sdhci-pxav2.c 5.9 KB

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