dw_mmc-k3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2013 Linaro Ltd.
  4. * Copyright (c) 2013 HiSilicon Limited.
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/module.h>
  12. #include <linux/of_address.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include "dw_mmc.h"
  18. #include "dw_mmc-pltfm.h"
  19. /*
  20. * hi6220 sd only support io voltage 1.8v and 3v
  21. * Also need config AO_SCTRL_SEL18 accordingly
  22. */
  23. #define AO_SCTRL_SEL18 BIT(10)
  24. #define AO_SCTRL_CTRL3 0x40C
  25. #define DWMMC_SDIO_ID 2
  26. #define SOC_SCTRL_SCPERCTRL5 (0x314)
  27. #define SDCARD_IO_SEL18 BIT(2)
  28. #define SDCARD_RD_THRESHOLD (512)
  29. #define GENCLK_DIV (7)
  30. #define GPIO_CLK_ENABLE BIT(16)
  31. #define GPIO_CLK_DIV_MASK GENMASK(11, 8)
  32. #define GPIO_USE_SAMPLE_DLY_MASK GENMASK(13, 13)
  33. #define UHS_REG_EXT_SAMPLE_PHASE_MASK GENMASK(20, 16)
  34. #define UHS_REG_EXT_SAMPLE_DRVPHASE_MASK GENMASK(25, 21)
  35. #define UHS_REG_EXT_SAMPLE_DLY_MASK GENMASK(30, 26)
  36. #define TIMING_MODE 3
  37. #define TIMING_CFG_NUM 10
  38. #define NUM_PHASES (40)
  39. #define ENABLE_SHIFT_MIN_SMPL (4)
  40. #define ENABLE_SHIFT_MAX_SMPL (12)
  41. #define USE_DLY_MIN_SMPL (11)
  42. #define USE_DLY_MAX_SMPL (14)
  43. struct k3_priv {
  44. int ctrl_id;
  45. u32 cur_speed;
  46. struct regmap *reg;
  47. };
  48. static unsigned long dw_mci_hi6220_caps[] = {
  49. MMC_CAP_CMD23,
  50. MMC_CAP_CMD23,
  51. 0
  52. };
  53. struct hs_timing {
  54. u32 drv_phase;
  55. u32 smpl_dly;
  56. u32 smpl_phase_max;
  57. u32 smpl_phase_min;
  58. };
  59. static struct hs_timing hs_timing_cfg[TIMING_MODE][TIMING_CFG_NUM] = {
  60. { /* reserved */ },
  61. { /* SD */
  62. {7, 0, 15, 15,}, /* 0: LEGACY 400k */
  63. {6, 0, 4, 4,}, /* 1: MMC_HS */
  64. {6, 0, 3, 3,}, /* 2: SD_HS */
  65. {6, 0, 15, 15,}, /* 3: SDR12 */
  66. {6, 0, 2, 2,}, /* 4: SDR25 */
  67. {4, 0, 11, 0,}, /* 5: SDR50 */
  68. {6, 4, 15, 0,}, /* 6: SDR104 */
  69. {0}, /* 7: DDR50 */
  70. {0}, /* 8: DDR52 */
  71. {0}, /* 9: HS200 */
  72. },
  73. { /* SDIO */
  74. {7, 0, 15, 15,}, /* 0: LEGACY 400k */
  75. {0}, /* 1: MMC_HS */
  76. {6, 0, 15, 15,}, /* 2: SD_HS */
  77. {6, 0, 15, 15,}, /* 3: SDR12 */
  78. {6, 0, 0, 0,}, /* 4: SDR25 */
  79. {4, 0, 12, 0,}, /* 5: SDR50 */
  80. {5, 4, 15, 0,}, /* 6: SDR104 */
  81. {0}, /* 7: DDR50 */
  82. {0}, /* 8: DDR52 */
  83. {0}, /* 9: HS200 */
  84. }
  85. };
  86. static void dw_mci_k3_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  87. {
  88. int ret;
  89. ret = clk_set_rate(host->ciu_clk, ios->clock);
  90. if (ret)
  91. dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
  92. host->bus_hz = clk_get_rate(host->ciu_clk);
  93. }
  94. static const struct dw_mci_drv_data k3_drv_data = {
  95. .set_ios = dw_mci_k3_set_ios,
  96. };
  97. static int dw_mci_hi6220_parse_dt(struct dw_mci *host)
  98. {
  99. struct k3_priv *priv;
  100. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  101. if (!priv)
  102. return -ENOMEM;
  103. priv->reg = syscon_regmap_lookup_by_phandle(host->dev->of_node,
  104. "hisilicon,peripheral-syscon");
  105. if (IS_ERR(priv->reg))
  106. priv->reg = NULL;
  107. priv->ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  108. if (priv->ctrl_id < 0)
  109. priv->ctrl_id = 0;
  110. if (priv->ctrl_id >= TIMING_MODE)
  111. return -EINVAL;
  112. host->priv = priv;
  113. return 0;
  114. }
  115. static int dw_mci_hi6220_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  116. {
  117. struct dw_mci_slot *slot = mmc_priv(mmc);
  118. struct k3_priv *priv;
  119. struct dw_mci *host;
  120. int min_uv, max_uv;
  121. int ret;
  122. host = slot->host;
  123. priv = host->priv;
  124. if (!priv || !priv->reg)
  125. return 0;
  126. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  127. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  128. AO_SCTRL_SEL18, 0);
  129. min_uv = 3000000;
  130. max_uv = 3000000;
  131. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  132. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  133. AO_SCTRL_SEL18, AO_SCTRL_SEL18);
  134. min_uv = 1800000;
  135. max_uv = 1800000;
  136. } else {
  137. dev_dbg(host->dev, "voltage not supported\n");
  138. return -EINVAL;
  139. }
  140. if (ret) {
  141. dev_dbg(host->dev, "switch voltage failed\n");
  142. return ret;
  143. }
  144. if (IS_ERR_OR_NULL(mmc->supply.vqmmc))
  145. return 0;
  146. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  147. if (ret) {
  148. dev_dbg(host->dev, "Regulator set error %d: %d - %d\n",
  149. ret, min_uv, max_uv);
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static void dw_mci_hi6220_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  155. {
  156. int ret;
  157. unsigned int clock;
  158. clock = (ios->clock <= 25000000) ? 25000000 : ios->clock;
  159. ret = clk_set_rate(host->biu_clk, clock);
  160. if (ret)
  161. dev_warn(host->dev, "failed to set rate %uHz\n", clock);
  162. host->bus_hz = clk_get_rate(host->biu_clk);
  163. }
  164. static int dw_mci_hi6220_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  165. {
  166. return 0;
  167. }
  168. static const struct dw_mci_drv_data hi6220_data = {
  169. .caps = dw_mci_hi6220_caps,
  170. .num_caps = ARRAY_SIZE(dw_mci_hi6220_caps),
  171. .switch_voltage = dw_mci_hi6220_switch_voltage,
  172. .set_ios = dw_mci_hi6220_set_ios,
  173. .parse_dt = dw_mci_hi6220_parse_dt,
  174. .execute_tuning = dw_mci_hi6220_execute_tuning,
  175. };
  176. static void dw_mci_hs_set_timing(struct dw_mci *host, int timing,
  177. int smpl_phase)
  178. {
  179. u32 drv_phase;
  180. u32 smpl_dly;
  181. u32 use_smpl_dly = 0;
  182. u32 enable_shift = 0;
  183. u32 reg_value;
  184. int ctrl_id;
  185. struct k3_priv *priv;
  186. priv = host->priv;
  187. ctrl_id = priv->ctrl_id;
  188. drv_phase = hs_timing_cfg[ctrl_id][timing].drv_phase;
  189. smpl_dly = hs_timing_cfg[ctrl_id][timing].smpl_dly;
  190. if (smpl_phase == -1)
  191. smpl_phase = (hs_timing_cfg[ctrl_id][timing].smpl_phase_max +
  192. hs_timing_cfg[ctrl_id][timing].smpl_phase_min) / 2;
  193. switch (timing) {
  194. case MMC_TIMING_UHS_SDR104:
  195. if (smpl_phase >= USE_DLY_MIN_SMPL &&
  196. smpl_phase <= USE_DLY_MAX_SMPL)
  197. use_smpl_dly = 1;
  198. fallthrough;
  199. case MMC_TIMING_UHS_SDR50:
  200. if (smpl_phase >= ENABLE_SHIFT_MIN_SMPL &&
  201. smpl_phase <= ENABLE_SHIFT_MAX_SMPL)
  202. enable_shift = 1;
  203. break;
  204. }
  205. mci_writel(host, GPIO, 0x0);
  206. usleep_range(5, 10);
  207. reg_value = FIELD_PREP(UHS_REG_EXT_SAMPLE_PHASE_MASK, smpl_phase) |
  208. FIELD_PREP(UHS_REG_EXT_SAMPLE_DLY_MASK, smpl_dly) |
  209. FIELD_PREP(UHS_REG_EXT_SAMPLE_DRVPHASE_MASK, drv_phase);
  210. mci_writel(host, UHS_REG_EXT, reg_value);
  211. mci_writel(host, ENABLE_SHIFT, enable_shift);
  212. reg_value = FIELD_PREP(GPIO_CLK_DIV_MASK, GENCLK_DIV) |
  213. FIELD_PREP(GPIO_USE_SAMPLE_DLY_MASK, use_smpl_dly);
  214. mci_writel(host, GPIO, (unsigned int)reg_value | GPIO_CLK_ENABLE);
  215. /* We should delay 1ms wait for timing setting finished. */
  216. usleep_range(1000, 2000);
  217. }
  218. static int dw_mci_hi3660_init(struct dw_mci *host)
  219. {
  220. mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(SDCARD_RD_THRESHOLD,
  221. SDMMC_CARD_RD_THR_EN));
  222. dw_mci_hs_set_timing(host, MMC_TIMING_LEGACY, -1);
  223. host->bus_hz /= (GENCLK_DIV + 1);
  224. return 0;
  225. }
  226. static int dw_mci_set_sel18(struct dw_mci *host, bool set)
  227. {
  228. int ret;
  229. unsigned int val;
  230. struct k3_priv *priv;
  231. priv = host->priv;
  232. val = set ? SDCARD_IO_SEL18 : 0;
  233. ret = regmap_update_bits(priv->reg, SOC_SCTRL_SCPERCTRL5,
  234. SDCARD_IO_SEL18, val);
  235. if (ret) {
  236. dev_err(host->dev, "sel18 %u error\n", val);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. static void dw_mci_hi3660_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  242. {
  243. int ret;
  244. unsigned long wanted;
  245. unsigned long actual;
  246. struct k3_priv *priv = host->priv;
  247. if (!ios->clock || ios->clock == priv->cur_speed)
  248. return;
  249. wanted = ios->clock * (GENCLK_DIV + 1);
  250. ret = clk_set_rate(host->ciu_clk, wanted);
  251. if (ret) {
  252. dev_err(host->dev, "failed to set rate %luHz\n", wanted);
  253. return;
  254. }
  255. actual = clk_get_rate(host->ciu_clk);
  256. dw_mci_hs_set_timing(host, ios->timing, -1);
  257. host->bus_hz = actual / (GENCLK_DIV + 1);
  258. host->current_speed = 0;
  259. priv->cur_speed = host->bus_hz;
  260. }
  261. static int dw_mci_get_best_clksmpl(unsigned int sample_flag)
  262. {
  263. int i;
  264. int interval;
  265. unsigned int v;
  266. unsigned int len;
  267. unsigned int range_start = 0;
  268. unsigned int range_length = 0;
  269. unsigned int middle_range = 0;
  270. if (!sample_flag)
  271. return -EIO;
  272. if (~sample_flag == 0)
  273. return 0;
  274. i = ffs(sample_flag) - 1;
  275. /*
  276. * A clock cycle is divided into 32 phases,
  277. * each of which is represented by a bit,
  278. * finding the optimal phase.
  279. */
  280. while (i < 32) {
  281. v = ror32(sample_flag, i);
  282. len = ffs(~v) - 1;
  283. if (len > range_length) {
  284. range_length = len;
  285. range_start = i;
  286. }
  287. interval = ffs(v >> len) - 1;
  288. if (interval < 0)
  289. break;
  290. i += len + interval;
  291. }
  292. middle_range = range_start + range_length / 2;
  293. if (middle_range >= 32)
  294. middle_range %= 32;
  295. return middle_range;
  296. }
  297. static int dw_mci_hi3660_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  298. {
  299. int i = 0;
  300. struct dw_mci *host = slot->host;
  301. struct mmc_host *mmc = slot->mmc;
  302. int smpl_phase = 0;
  303. u32 tuning_sample_flag = 0;
  304. int best_clksmpl = 0;
  305. for (i = 0; i < NUM_PHASES; ++i, ++smpl_phase) {
  306. smpl_phase %= 32;
  307. mci_writel(host, TMOUT, ~0);
  308. dw_mci_hs_set_timing(host, mmc->ios.timing, smpl_phase);
  309. if (!mmc_send_tuning(mmc, opcode, NULL))
  310. tuning_sample_flag |= (1 << smpl_phase);
  311. else
  312. tuning_sample_flag &= ~(1 << smpl_phase);
  313. }
  314. best_clksmpl = dw_mci_get_best_clksmpl(tuning_sample_flag);
  315. if (best_clksmpl < 0) {
  316. dev_err(host->dev, "All phases bad!\n");
  317. return -EIO;
  318. }
  319. dw_mci_hs_set_timing(host, mmc->ios.timing, best_clksmpl);
  320. dev_info(host->dev, "tuning ok best_clksmpl %u tuning_sample_flag %x\n",
  321. best_clksmpl, tuning_sample_flag);
  322. return 0;
  323. }
  324. static int dw_mci_hi3660_switch_voltage(struct mmc_host *mmc,
  325. struct mmc_ios *ios)
  326. {
  327. int ret = 0;
  328. struct dw_mci_slot *slot = mmc_priv(mmc);
  329. struct k3_priv *priv;
  330. struct dw_mci *host;
  331. host = slot->host;
  332. priv = host->priv;
  333. if (!priv || !priv->reg)
  334. return 0;
  335. if (priv->ctrl_id == DWMMC_SDIO_ID)
  336. return 0;
  337. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  338. ret = dw_mci_set_sel18(host, 0);
  339. else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
  340. ret = dw_mci_set_sel18(host, 1);
  341. if (ret)
  342. return ret;
  343. if (!IS_ERR(mmc->supply.vqmmc)) {
  344. ret = mmc_regulator_set_vqmmc(mmc, ios);
  345. if (ret < 0) {
  346. dev_err(host->dev, "Regulator set error %d\n", ret);
  347. return ret;
  348. }
  349. }
  350. return 0;
  351. }
  352. static const struct dw_mci_drv_data hi3660_data = {
  353. .init = dw_mci_hi3660_init,
  354. .set_ios = dw_mci_hi3660_set_ios,
  355. .parse_dt = dw_mci_hi6220_parse_dt,
  356. .execute_tuning = dw_mci_hi3660_execute_tuning,
  357. .switch_voltage = dw_mci_hi3660_switch_voltage,
  358. };
  359. static const struct of_device_id dw_mci_k3_match[] = {
  360. { .compatible = "hisilicon,hi3660-dw-mshc", .data = &hi3660_data, },
  361. { .compatible = "hisilicon,hi4511-dw-mshc", .data = &k3_drv_data, },
  362. { .compatible = "hisilicon,hi6220-dw-mshc", .data = &hi6220_data, },
  363. {},
  364. };
  365. MODULE_DEVICE_TABLE(of, dw_mci_k3_match);
  366. static int dw_mci_k3_probe(struct platform_device *pdev)
  367. {
  368. const struct dw_mci_drv_data *drv_data;
  369. const struct of_device_id *match;
  370. match = of_match_node(dw_mci_k3_match, pdev->dev.of_node);
  371. drv_data = match->data;
  372. return dw_mci_pltfm_register(pdev, drv_data);
  373. }
  374. static const struct dev_pm_ops dw_mci_k3_dev_pm_ops = {
  375. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  376. pm_runtime_force_resume)
  377. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  378. dw_mci_runtime_resume,
  379. NULL)
  380. };
  381. static struct platform_driver dw_mci_k3_pltfm_driver = {
  382. .probe = dw_mci_k3_probe,
  383. .remove = dw_mci_pltfm_remove,
  384. .driver = {
  385. .name = "dwmmc_k3",
  386. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  387. .of_match_table = dw_mci_k3_match,
  388. .pm = &dw_mci_k3_dev_pm_ops,
  389. },
  390. };
  391. module_platform_driver(dw_mci_k3_pltfm_driver);
  392. MODULE_DESCRIPTION("K3 Specific DW-MSHC Driver Extension");
  393. MODULE_LICENSE("GPL v2");
  394. MODULE_ALIAS("platform:dwmmc_k3");