sdhci-pxav2.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include "sdhci.h"
  33. #include "sdhci-pltfm.h"
  34. #define SD_FIFO_PARAM 0xe0
  35. #define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
  36. #define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
  37. #define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
  38. #define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
  39. CLK_GATE_ON | CLK_GATE_CTL)
  40. #define SD_CLOCK_BURST_SIZE_SETUP 0xe6
  41. #define SDCLK_SEL_SHIFT 8
  42. #define SDCLK_SEL_MASK 0x3
  43. #define SDCLK_DELAY_SHIFT 10
  44. #define SDCLK_DELAY_MASK 0x3c
  45. #define SD_CE_ATA_2 0xea
  46. #define MMC_CARD 0x1000
  47. #define MMC_WIDTH 0x0100
  48. static void pxav2_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. u16 tmp = 0;
  54. /*
  55. * tune timing of read data/command when crc error happen
  56. * no performance impact
  57. */
  58. if (pdata && pdata->clk_delay_sel == 1) {
  59. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  60. tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT);
  61. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  62. << SDCLK_DELAY_SHIFT;
  63. tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT);
  64. tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT;
  65. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  66. }
  67. if (pdata && (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING)) {
  68. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  69. tmp &= ~CLK_GATE_SETTING_BITS;
  70. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  71. } else {
  72. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  73. tmp &= ~CLK_GATE_SETTING_BITS;
  74. tmp |= CLK_GATE_SETTING_BITS;
  75. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  76. }
  77. }
  78. }
  79. static int pxav2_mmc_set_width(struct sdhci_host *host, int width)
  80. {
  81. u8 ctrl;
  82. u16 tmp;
  83. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  84. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  85. if (width == MMC_BUS_WIDTH_8) {
  86. ctrl &= ~SDHCI_CTRL_4BITBUS;
  87. tmp |= MMC_CARD | MMC_WIDTH;
  88. } else {
  89. tmp &= ~(MMC_CARD | MMC_WIDTH);
  90. if (width == MMC_BUS_WIDTH_4)
  91. ctrl |= SDHCI_CTRL_4BITBUS;
  92. else
  93. ctrl &= ~SDHCI_CTRL_4BITBUS;
  94. }
  95. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  96. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  97. return 0;
  98. }
  99. static struct sdhci_ops pxav2_sdhci_ops = {
  100. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  101. .platform_reset_exit = pxav2_set_private_registers,
  102. .platform_bus_width = pxav2_mmc_set_width,
  103. };
  104. #ifdef CONFIG_OF
  105. static const struct of_device_id sdhci_pxav2_of_match[] = {
  106. {
  107. .compatible = "mrvl,pxav2-mmc",
  108. },
  109. {},
  110. };
  111. MODULE_DEVICE_TABLE(of, sdhci_pxav2_of_match);
  112. static struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
  113. {
  114. struct sdhci_pxa_platdata *pdata;
  115. struct device_node *np = dev->of_node;
  116. u32 bus_width;
  117. u32 clk_delay_cycles;
  118. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  119. if (!pdata)
  120. return NULL;
  121. if (of_find_property(np, "non-removable", NULL))
  122. pdata->flags |= PXA_FLAG_CARD_PERMANENT;
  123. of_property_read_u32(np, "bus-width", &bus_width);
  124. if (bus_width == 8)
  125. pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
  126. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  127. if (clk_delay_cycles > 0) {
  128. pdata->clk_delay_sel = 1;
  129. pdata->clk_delay_cycles = clk_delay_cycles;
  130. }
  131. return pdata;
  132. }
  133. #else
  134. static inline struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
  135. {
  136. return NULL;
  137. }
  138. #endif
  139. static int sdhci_pxav2_probe(struct platform_device *pdev)
  140. {
  141. struct sdhci_pltfm_host *pltfm_host;
  142. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  143. struct device *dev = &pdev->dev;
  144. struct sdhci_host *host = NULL;
  145. struct sdhci_pxa *pxa = NULL;
  146. const struct of_device_id *match;
  147. int ret;
  148. struct clk *clk;
  149. pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
  150. if (!pxa)
  151. return -ENOMEM;
  152. host = sdhci_pltfm_init(pdev, NULL);
  153. if (IS_ERR(host)) {
  154. kfree(pxa);
  155. return PTR_ERR(host);
  156. }
  157. pltfm_host = sdhci_priv(host);
  158. pltfm_host->priv = pxa;
  159. clk = clk_get(dev, "PXA-SDHCLK");
  160. if (IS_ERR(clk)) {
  161. dev_err(dev, "failed to get io clock\n");
  162. ret = PTR_ERR(clk);
  163. goto err_clk_get;
  164. }
  165. pltfm_host->clk = clk;
  166. clk_prepare_enable(clk);
  167. host->quirks = SDHCI_QUIRK_BROKEN_ADMA
  168. | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  169. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  170. match = of_match_device(of_match_ptr(sdhci_pxav2_of_match), &pdev->dev);
  171. if (match) {
  172. pdata = pxav2_get_mmc_pdata(dev);
  173. }
  174. if (pdata) {
  175. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  176. /* on-chip device */
  177. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  178. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  179. }
  180. /* If slot design supports 8 bit data, indicate this to MMC. */
  181. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  182. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  183. if (pdata->quirks)
  184. host->quirks |= pdata->quirks;
  185. if (pdata->host_caps)
  186. host->mmc->caps |= pdata->host_caps;
  187. if (pdata->pm_caps)
  188. host->mmc->pm_caps |= pdata->pm_caps;
  189. }
  190. host->ops = &pxav2_sdhci_ops;
  191. ret = sdhci_add_host(host);
  192. if (ret) {
  193. dev_err(&pdev->dev, "failed to add host\n");
  194. goto err_add_host;
  195. }
  196. platform_set_drvdata(pdev, host);
  197. return 0;
  198. err_add_host:
  199. clk_disable_unprepare(clk);
  200. clk_put(clk);
  201. err_clk_get:
  202. sdhci_pltfm_free(pdev);
  203. kfree(pxa);
  204. return ret;
  205. }
  206. static int sdhci_pxav2_remove(struct platform_device *pdev)
  207. {
  208. struct sdhci_host *host = platform_get_drvdata(pdev);
  209. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  210. struct sdhci_pxa *pxa = pltfm_host->priv;
  211. sdhci_remove_host(host, 1);
  212. clk_disable_unprepare(pltfm_host->clk);
  213. clk_put(pltfm_host->clk);
  214. sdhci_pltfm_free(pdev);
  215. kfree(pxa);
  216. platform_set_drvdata(pdev, NULL);
  217. return 0;
  218. }
  219. static struct platform_driver sdhci_pxav2_driver = {
  220. .driver = {
  221. .name = "sdhci-pxav2",
  222. .owner = THIS_MODULE,
  223. #ifdef CONFIG_OF
  224. .of_match_table = sdhci_pxav2_of_match,
  225. #endif
  226. .pm = SDHCI_PLTFM_PMOPS,
  227. },
  228. .probe = sdhci_pxav2_probe,
  229. .remove = sdhci_pxav2_remove,
  230. };
  231. module_platform_driver(sdhci_pxav2_driver);
  232. MODULE_DESCRIPTION("SDHCI driver for pxav2");
  233. MODULE_AUTHOR("Marvell International Ltd.");
  234. MODULE_LICENSE("GPL v2");