0108-mmc-starfive-Add-sdio-emmc-driver-support.patch 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. From ee990c664fad993275b2615d0ff06af5d04922f0 Mon Sep 17 00:00:00 2001
  2. From: William Qiu <[email protected]>
  3. Date: Wed, 15 Feb 2023 19:32:47 +0800
  4. Subject: [PATCH 108/122] mmc: starfive: Add sdio/emmc driver support
  5. Add sdio/emmc driver support for StarFive JH7110 soc.
  6. Tested-by: Conor Dooley <[email protected]>
  7. Signed-off-by: William Qiu <[email protected]>
  8. ---
  9. MAINTAINERS | 6 +
  10. drivers/mmc/host/Kconfig | 10 ++
  11. drivers/mmc/host/Makefile | 1 +
  12. drivers/mmc/host/dw_mmc-starfive.c | 186 +++++++++++++++++++++++++++++
  13. 4 files changed, 203 insertions(+)
  14. create mode 100644 drivers/mmc/host/dw_mmc-starfive.c
  15. --- a/MAINTAINERS
  16. +++ b/MAINTAINERS
  17. @@ -19664,6 +19664,12 @@ S: Maintained
  18. F: Documentation/devicetree/bindings/net/starfive,jh7110-dwmac.yaml
  19. F: drivers/net/ethernet/stmicro/stmmac/dwmac-starfive.c
  20. +STARFIVE JH7110 MMC/SD/SDIO DRIVER
  21. +M: William Qiu <[email protected]>
  22. +S: Supported
  23. +F: Documentation/devicetree/bindings/mmc/starfive*
  24. +F: drivers/mmc/host/dw_mmc-starfive.c
  25. +
  26. STARFIVE JH7110 PLL CLOCK DRIVER
  27. M: Xingyu Wu <[email protected]>
  28. S: Supported
  29. --- a/drivers/mmc/host/Kconfig
  30. +++ b/drivers/mmc/host/Kconfig
  31. @@ -872,6 +872,16 @@ config MMC_DW_ROCKCHIP
  32. Synopsys DesignWare Memory Card Interface driver. Select this option
  33. for platforms based on RK3066, RK3188 and RK3288 SoC's.
  34. +config MMC_DW_STARFIVE
  35. + tristate "StarFive specific extensions for Synopsys DW Memory Card Interface"
  36. + depends on SOC_STARFIVE
  37. + depends on MMC_DW
  38. + select MMC_DW_PLTFM
  39. + help
  40. + This selects support for StarFive JH7110 SoC specific extensions to the
  41. + Synopsys DesignWare Memory Card Interface driver. Select this option
  42. + for platforms based on StarFive JH7110 SoC.
  43. +
  44. config MMC_SH_MMCIF
  45. tristate "SuperH Internal MMCIF support"
  46. depends on SUPERH || ARCH_RENESAS || COMPILE_TEST
  47. --- a/drivers/mmc/host/Makefile
  48. +++ b/drivers/mmc/host/Makefile
  49. @@ -56,6 +56,7 @@ obj-$(CONFIG_MMC_DW_HI3798CV200) += dw_m
  50. obj-$(CONFIG_MMC_DW_K3) += dw_mmc-k3.o
  51. obj-$(CONFIG_MMC_DW_PCI) += dw_mmc-pci.o
  52. obj-$(CONFIG_MMC_DW_ROCKCHIP) += dw_mmc-rockchip.o
  53. +obj-$(CONFIG_MMC_DW_STARFIVE) += dw_mmc-starfive.o
  54. obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o
  55. obj-$(CONFIG_MMC_JZ4740) += jz4740_mmc.o
  56. obj-$(CONFIG_MMC_VUB300) += vub300.o
  57. --- /dev/null
  58. +++ b/drivers/mmc/host/dw_mmc-starfive.c
  59. @@ -0,0 +1,186 @@
  60. +// SPDX-License-Identifier: GPL-2.0
  61. +/*
  62. + * StarFive Designware Mobile Storage Host Controller Driver
  63. + *
  64. + * Copyright (c) 2022 StarFive Technology Co., Ltd.
  65. + */
  66. +
  67. +#include <linux/clk.h>
  68. +#include <linux/delay.h>
  69. +#include <linux/mfd/syscon.h>
  70. +#include <linux/mmc/host.h>
  71. +#include <linux/module.h>
  72. +#include <linux/of_address.h>
  73. +#include <linux/platform_device.h>
  74. +#include <linux/regmap.h>
  75. +
  76. +#include "dw_mmc.h"
  77. +#include "dw_mmc-pltfm.h"
  78. +
  79. +#define ALL_INT_CLR 0x1ffff
  80. +#define MAX_DELAY_CHAIN 32
  81. +
  82. +struct starfive_priv {
  83. + struct device *dev;
  84. + struct regmap *reg_syscon;
  85. + u32 syscon_offset;
  86. + u32 syscon_shift;
  87. + u32 syscon_mask;
  88. +};
  89. +
  90. +static void dw_mci_starfive_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  91. +{
  92. + int ret;
  93. + unsigned int clock;
  94. +
  95. + if (ios->timing == MMC_TIMING_MMC_DDR52 || ios->timing == MMC_TIMING_UHS_DDR50) {
  96. + clock = (ios->clock > 50000000 && ios->clock <= 52000000) ? 100000000 : ios->clock;
  97. + ret = clk_set_rate(host->ciu_clk, clock);
  98. + if (ret)
  99. + dev_dbg(host->dev, "Use an external frequency divider %uHz\n", ios->clock);
  100. + host->bus_hz = clk_get_rate(host->ciu_clk);
  101. + } else {
  102. + dev_dbg(host->dev, "Using the internal divider\n");
  103. + }
  104. +}
  105. +
  106. +static int dw_mci_starfive_execute_tuning(struct dw_mci_slot *slot,
  107. + u32 opcode)
  108. +{
  109. + static const int grade = MAX_DELAY_CHAIN;
  110. + struct dw_mci *host = slot->host;
  111. + struct starfive_priv *priv = host->priv;
  112. + int rise_point = -1, fall_point = -1;
  113. + int err, prev_err;
  114. + int i;
  115. + bool found = 0;
  116. + u32 regval;
  117. +
  118. + /*
  119. + * Use grade as the max delay chain, and use the rise_point and
  120. + * fall_point to ensure the best sampling point of a data input
  121. + * signals.
  122. + */
  123. + for (i = 0; i < grade; i++) {
  124. + regval = i << priv->syscon_shift;
  125. + err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset,
  126. + priv->syscon_mask, regval);
  127. + if (err)
  128. + return err;
  129. + mci_writel(host, RINTSTS, ALL_INT_CLR);
  130. +
  131. + err = mmc_send_tuning(slot->mmc, opcode, NULL);
  132. + if (!err)
  133. + found = 1;
  134. +
  135. + if (i > 0) {
  136. + if (err && !prev_err)
  137. + fall_point = i - 1;
  138. + if (!err && prev_err)
  139. + rise_point = i;
  140. + }
  141. +
  142. + if (rise_point != -1 && fall_point != -1)
  143. + goto tuning_out;
  144. +
  145. + prev_err = err;
  146. + err = 0;
  147. + }
  148. +
  149. +tuning_out:
  150. + if (found) {
  151. + if (rise_point == -1)
  152. + rise_point = 0;
  153. + if (fall_point == -1)
  154. + fall_point = grade - 1;
  155. + if (fall_point < rise_point) {
  156. + if ((rise_point + fall_point) >
  157. + (grade - 1))
  158. + i = fall_point / 2;
  159. + else
  160. + i = (rise_point + grade - 1) / 2;
  161. + } else {
  162. + i = (rise_point + fall_point) / 2;
  163. + }
  164. +
  165. + regval = i << priv->syscon_shift;
  166. + err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset,
  167. + priv->syscon_mask, regval);
  168. + if (err)
  169. + return err;
  170. + mci_writel(host, RINTSTS, ALL_INT_CLR);
  171. +
  172. + dev_info(host->dev, "Found valid delay chain! use it [delay=%d]\n", i);
  173. + } else {
  174. + dev_err(host->dev, "No valid delay chain! use default\n");
  175. + err = -EINVAL;
  176. + }
  177. +
  178. + mci_writel(host, RINTSTS, ALL_INT_CLR);
  179. + return err;
  180. +}
  181. +
  182. +static int dw_mci_starfive_parse_dt(struct dw_mci *host)
  183. +{
  184. + struct of_phandle_args args;
  185. + struct starfive_priv *priv;
  186. + int ret;
  187. +
  188. + priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  189. + if (!priv)
  190. + return -ENOMEM;
  191. +
  192. + ret = of_parse_phandle_with_fixed_args(host->dev->of_node,
  193. + "starfive,sysreg", 3, 0, &args);
  194. + if (ret) {
  195. + dev_err(host->dev, "Failed to parse starfive,sysreg\n");
  196. + return -EINVAL;
  197. + }
  198. +
  199. + priv->reg_syscon = syscon_node_to_regmap(args.np);
  200. + of_node_put(args.np);
  201. + if (IS_ERR(priv->reg_syscon))
  202. + return PTR_ERR(priv->reg_syscon);
  203. +
  204. + priv->syscon_offset = args.args[0];
  205. + priv->syscon_shift = args.args[1];
  206. + priv->syscon_mask = args.args[2];
  207. +
  208. + host->priv = priv;
  209. +
  210. + return 0;
  211. +}
  212. +
  213. +static const struct dw_mci_drv_data starfive_data = {
  214. + .common_caps = MMC_CAP_CMD23,
  215. + .set_ios = dw_mci_starfive_set_ios,
  216. + .parse_dt = dw_mci_starfive_parse_dt,
  217. + .execute_tuning = dw_mci_starfive_execute_tuning,
  218. +};
  219. +
  220. +static const struct of_device_id dw_mci_starfive_match[] = {
  221. + { .compatible = "starfive,jh7110-mmc",
  222. + .data = &starfive_data },
  223. + {},
  224. +};
  225. +MODULE_DEVICE_TABLE(of, dw_mci_starfive_match);
  226. +
  227. +static int dw_mci_starfive_probe(struct platform_device *pdev)
  228. +{
  229. + return dw_mci_pltfm_register(pdev, &starfive_data);
  230. +}
  231. +
  232. +static struct platform_driver dw_mci_starfive_driver = {
  233. + .probe = dw_mci_starfive_probe,
  234. + .remove = dw_mci_pltfm_remove,
  235. + .driver = {
  236. + .name = "dwmmc_starfive",
  237. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  238. + .of_match_table = dw_mci_starfive_match,
  239. + },
  240. +};
  241. +module_platform_driver(dw_mci_starfive_driver);
  242. +
  243. +MODULE_DESCRIPTION("StarFive JH7110 Specific DW-MSHC Driver Extension");
  244. +MODULE_LICENSE("GPL");
  245. +MODULE_ALIAS("platform:dwmmc_starfive");