dw_mmc-k3.c 11 KB

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