dw_mmc-exynos.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
  3. *
  4. * Copyright (C) 2012, Samsung Electronics Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/clk.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/dw_mmc.h>
  16. #include <linux/of.h>
  17. #include <linux/of_gpio.h>
  18. #include "dw_mmc.h"
  19. #include "dw_mmc-pltfm.h"
  20. #define NUM_PINS(x) (x + 2)
  21. #define SDMMC_CLKSEL 0x09C
  22. #define SDMMC_CLKSEL_CCLK_SAMPLE(x) (((x) & 7) << 0)
  23. #define SDMMC_CLKSEL_CCLK_DRIVE(x) (((x) & 7) << 16)
  24. #define SDMMC_CLKSEL_CCLK_DIVIDER(x) (((x) & 7) << 24)
  25. #define SDMMC_CLKSEL_GET_DRV_WD3(x) (((x) >> 16) & 0x7)
  26. #define SDMMC_CLKSEL_TIMING(x, y, z) (SDMMC_CLKSEL_CCLK_SAMPLE(x) | \
  27. SDMMC_CLKSEL_CCLK_DRIVE(y) | \
  28. SDMMC_CLKSEL_CCLK_DIVIDER(z))
  29. #define SDMMC_CMD_USE_HOLD_REG BIT(29)
  30. #define EXYNOS4210_FIXED_CIU_CLK_DIV 2
  31. #define EXYNOS4412_FIXED_CIU_CLK_DIV 4
  32. /* Variations in Exynos specific dw-mshc controller */
  33. enum dw_mci_exynos_type {
  34. DW_MCI_TYPE_EXYNOS4210,
  35. DW_MCI_TYPE_EXYNOS4412,
  36. DW_MCI_TYPE_EXYNOS5250,
  37. };
  38. /* Exynos implementation specific driver private data */
  39. struct dw_mci_exynos_priv_data {
  40. enum dw_mci_exynos_type ctrl_type;
  41. u8 ciu_div;
  42. u32 sdr_timing;
  43. u32 ddr_timing;
  44. };
  45. static struct dw_mci_exynos_compatible {
  46. char *compatible;
  47. enum dw_mci_exynos_type ctrl_type;
  48. } exynos_compat[] = {
  49. {
  50. .compatible = "samsung,exynos4210-dw-mshc",
  51. .ctrl_type = DW_MCI_TYPE_EXYNOS4210,
  52. }, {
  53. .compatible = "samsung,exynos4412-dw-mshc",
  54. .ctrl_type = DW_MCI_TYPE_EXYNOS4412,
  55. }, {
  56. .compatible = "samsung,exynos5250-dw-mshc",
  57. .ctrl_type = DW_MCI_TYPE_EXYNOS5250,
  58. },
  59. };
  60. static int dw_mci_exynos_priv_init(struct dw_mci *host)
  61. {
  62. struct dw_mci_exynos_priv_data *priv;
  63. int idx;
  64. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  65. if (!priv) {
  66. dev_err(host->dev, "mem alloc failed for private data\n");
  67. return -ENOMEM;
  68. }
  69. for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
  70. if (of_device_is_compatible(host->dev->of_node,
  71. exynos_compat[idx].compatible))
  72. priv->ctrl_type = exynos_compat[idx].ctrl_type;
  73. }
  74. host->priv = priv;
  75. return 0;
  76. }
  77. static int dw_mci_exynos_setup_clock(struct dw_mci *host)
  78. {
  79. struct dw_mci_exynos_priv_data *priv = host->priv;
  80. if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5250)
  81. host->bus_hz /= (priv->ciu_div + 1);
  82. else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
  83. host->bus_hz /= EXYNOS4412_FIXED_CIU_CLK_DIV;
  84. else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
  85. host->bus_hz /= EXYNOS4210_FIXED_CIU_CLK_DIV;
  86. return 0;
  87. }
  88. static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr)
  89. {
  90. /*
  91. * Exynos4412 and Exynos5250 extends the use of CMD register with the
  92. * use of bit 29 (which is reserved on standard MSHC controllers) for
  93. * optionally bypassing the HOLD register for command and data. The
  94. * HOLD register should be bypassed in case there is no phase shift
  95. * applied on CMD/DATA that is sent to the card.
  96. */
  97. if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL)))
  98. *cmdr |= SDMMC_CMD_USE_HOLD_REG;
  99. }
  100. static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  101. {
  102. struct dw_mci_exynos_priv_data *priv = host->priv;
  103. if (ios->timing == MMC_TIMING_UHS_DDR50)
  104. mci_writel(host, CLKSEL, priv->ddr_timing);
  105. else
  106. mci_writel(host, CLKSEL, priv->sdr_timing);
  107. }
  108. static int dw_mci_exynos_parse_dt(struct dw_mci *host)
  109. {
  110. struct dw_mci_exynos_priv_data *priv = host->priv;
  111. struct device_node *np = host->dev->of_node;
  112. u32 timing[2];
  113. u32 div = 0;
  114. int ret;
  115. of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
  116. priv->ciu_div = div;
  117. ret = of_property_read_u32_array(np,
  118. "samsung,dw-mshc-sdr-timing", timing, 2);
  119. if (ret)
  120. return ret;
  121. priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
  122. ret = of_property_read_u32_array(np,
  123. "samsung,dw-mshc-ddr-timing", timing, 2);
  124. if (ret)
  125. return ret;
  126. priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
  127. return 0;
  128. }
  129. static int dw_mci_exynos_setup_bus(struct dw_mci *host,
  130. struct device_node *slot_np, u8 bus_width)
  131. {
  132. int idx, gpio, ret;
  133. if (!slot_np)
  134. return -EINVAL;
  135. /* cmd + clock + bus-width pins */
  136. for (idx = 0; idx < NUM_PINS(bus_width); idx++) {
  137. gpio = of_get_gpio(slot_np, idx);
  138. if (!gpio_is_valid(gpio)) {
  139. dev_err(host->dev, "invalid gpio: %d\n", gpio);
  140. return -EINVAL;
  141. }
  142. ret = devm_gpio_request(host->dev, gpio, "dw-mci-bus");
  143. if (ret) {
  144. dev_err(host->dev, "gpio [%d] request failed\n", gpio);
  145. return -EBUSY;
  146. }
  147. }
  148. if (host->pdata->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  149. return 0;
  150. gpio = of_get_named_gpio(slot_np, "samsung,cd-pinmux-gpio", 0);
  151. if (gpio_is_valid(gpio)) {
  152. if (devm_gpio_request(host->dev, gpio, "dw-mci-cd"))
  153. dev_err(host->dev, "gpio [%d] request failed\n", gpio);
  154. } else {
  155. dev_info(host->dev, "cd gpio not available");
  156. }
  157. return 0;
  158. }
  159. /* Exynos5250 controller specific capabilities */
  160. static unsigned long exynos5250_dwmmc_caps[4] = {
  161. MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR |
  162. MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
  163. MMC_CAP_CMD23,
  164. MMC_CAP_CMD23,
  165. MMC_CAP_CMD23,
  166. };
  167. static const struct dw_mci_drv_data exynos5250_drv_data = {
  168. .caps = exynos5250_dwmmc_caps,
  169. .init = dw_mci_exynos_priv_init,
  170. .setup_clock = dw_mci_exynos_setup_clock,
  171. .prepare_command = dw_mci_exynos_prepare_command,
  172. .set_ios = dw_mci_exynos_set_ios,
  173. .parse_dt = dw_mci_exynos_parse_dt,
  174. .setup_bus = dw_mci_exynos_setup_bus,
  175. };
  176. static const struct of_device_id dw_mci_exynos_match[] = {
  177. { .compatible = "samsung,exynos5250-dw-mshc",
  178. .data = &exynos5250_drv_data, },
  179. {},
  180. };
  181. MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
  182. int dw_mci_exynos_probe(struct platform_device *pdev)
  183. {
  184. const struct dw_mci_drv_data *drv_data;
  185. const struct of_device_id *match;
  186. match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
  187. drv_data = match->data;
  188. return dw_mci_pltfm_register(pdev, drv_data);
  189. }
  190. static struct platform_driver dw_mci_exynos_pltfm_driver = {
  191. .probe = dw_mci_exynos_probe,
  192. .remove = __exit_p(dw_mci_pltfm_remove),
  193. .driver = {
  194. .name = "dwmmc_exynos",
  195. .of_match_table = of_match_ptr(dw_mci_exynos_match),
  196. .pm = &dw_mci_pltfm_pmops,
  197. },
  198. };
  199. module_platform_driver(dw_mci_exynos_pltfm_driver);
  200. MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
  201. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
  202. MODULE_LICENSE("GPL v2");
  203. MODULE_ALIAS("platform:dwmmc-exynos");