dw_mmc-rockchip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  4. */
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/clk.h>
  8. #include <linux/mmc/host.h>
  9. #include <linux/of_address.h>
  10. #include <linux/mmc/slot-gpio.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. #include "dw_mmc.h"
  14. #include "dw_mmc-pltfm.h"
  15. #define RK3288_CLKGEN_DIV 2
  16. static const unsigned int freqs[] = { 100000, 200000, 300000, 400000 };
  17. struct dw_mci_rockchip_priv_data {
  18. struct clk *drv_clk;
  19. struct clk *sample_clk;
  20. int default_sample_phase;
  21. int num_phases;
  22. };
  23. static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  24. {
  25. struct dw_mci_rockchip_priv_data *priv = host->priv;
  26. int ret;
  27. unsigned int cclkin;
  28. u32 bus_hz;
  29. if (ios->clock == 0)
  30. return;
  31. /*
  32. * cclkin: source clock of mmc controller
  33. * bus_hz: card interface clock generated by CLKGEN
  34. * bus_hz = cclkin / RK3288_CLKGEN_DIV
  35. * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
  36. *
  37. * Note: div can only be 0 or 1, but div must be set to 1 for eMMC
  38. * DDR52 8-bit mode.
  39. */
  40. if (ios->bus_width == MMC_BUS_WIDTH_8 &&
  41. ios->timing == MMC_TIMING_MMC_DDR52)
  42. cclkin = 2 * ios->clock * RK3288_CLKGEN_DIV;
  43. else
  44. cclkin = ios->clock * RK3288_CLKGEN_DIV;
  45. ret = clk_set_rate(host->ciu_clk, cclkin);
  46. if (ret)
  47. dev_warn(host->dev, "failed to set rate %uHz err: %d\n", cclkin, ret);
  48. bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
  49. if (bus_hz != host->bus_hz) {
  50. host->bus_hz = bus_hz;
  51. /* force dw_mci_setup_bus() */
  52. host->current_speed = 0;
  53. }
  54. /* Make sure we use phases which we can enumerate with */
  55. if (!IS_ERR(priv->sample_clk) && ios->timing <= MMC_TIMING_SD_HS)
  56. clk_set_phase(priv->sample_clk, priv->default_sample_phase);
  57. /*
  58. * Set the drive phase offset based on speed mode to achieve hold times.
  59. *
  60. * NOTE: this is _not_ a value that is dynamically tuned and is also
  61. * _not_ a value that will vary from board to board. It is a value
  62. * that could vary between different SoC models if they had massively
  63. * different output clock delays inside their dw_mmc IP block (delay_o),
  64. * but since it's OK to overshoot a little we don't need to do complex
  65. * calculations and can pick values that will just work for everyone.
  66. *
  67. * When picking values we'll stick with picking 0/90/180/270 since
  68. * those can be made very accurately on all known Rockchip SoCs.
  69. *
  70. * Note that these values match values from the DesignWare Databook
  71. * tables for the most part except for SDR12 and "ID mode". For those
  72. * two modes the databook calculations assume a clock in of 50MHz. As
  73. * seen above, we always use a clock in rate that is exactly the
  74. * card's input clock (times RK3288_CLKGEN_DIV, but that gets divided
  75. * back out before the controller sees it).
  76. *
  77. * From measurement of a single device, it appears that delay_o is
  78. * about .5 ns. Since we try to leave a bit of margin, it's expected
  79. * that numbers here will be fine even with much larger delay_o
  80. * (the 1.4 ns assumed by the DesignWare Databook would result in the
  81. * same results, for instance).
  82. */
  83. if (!IS_ERR(priv->drv_clk)) {
  84. int phase;
  85. /*
  86. * In almost all cases a 90 degree phase offset will provide
  87. * sufficient hold times across all valid input clock rates
  88. * assuming delay_o is not absurd for a given SoC. We'll use
  89. * that as a default.
  90. */
  91. phase = 90;
  92. switch (ios->timing) {
  93. case MMC_TIMING_MMC_DDR52:
  94. /*
  95. * Since clock in rate with MMC_DDR52 is doubled when
  96. * bus width is 8 we need to double the phase offset
  97. * to get the same timings.
  98. */
  99. if (ios->bus_width == MMC_BUS_WIDTH_8)
  100. phase = 180;
  101. break;
  102. case MMC_TIMING_UHS_SDR104:
  103. case MMC_TIMING_MMC_HS200:
  104. /*
  105. * In the case of 150 MHz clock (typical max for
  106. * Rockchip SoCs), 90 degree offset will add a delay
  107. * of 1.67 ns. That will meet min hold time of .8 ns
  108. * as long as clock output delay is < .87 ns. On
  109. * SoCs measured this seems to be OK, but it doesn't
  110. * hurt to give margin here, so we use 180.
  111. */
  112. phase = 180;
  113. break;
  114. }
  115. clk_set_phase(priv->drv_clk, phase);
  116. }
  117. }
  118. #define TUNING_ITERATION_TO_PHASE(i, num_phases) \
  119. (DIV_ROUND_UP((i) * 360, num_phases))
  120. static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  121. {
  122. struct dw_mci *host = slot->host;
  123. struct dw_mci_rockchip_priv_data *priv = host->priv;
  124. struct mmc_host *mmc = slot->mmc;
  125. int ret = 0;
  126. int i;
  127. bool v, prev_v = 0, first_v;
  128. struct range_t {
  129. int start;
  130. int end; /* inclusive */
  131. };
  132. struct range_t *ranges;
  133. unsigned int range_count = 0;
  134. int longest_range_len = -1;
  135. int longest_range = -1;
  136. int middle_phase;
  137. if (IS_ERR(priv->sample_clk)) {
  138. dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
  139. return -EIO;
  140. }
  141. ranges = kmalloc_array(priv->num_phases / 2 + 1,
  142. sizeof(*ranges), GFP_KERNEL);
  143. if (!ranges)
  144. return -ENOMEM;
  145. /* Try each phase and extract good ranges */
  146. for (i = 0; i < priv->num_phases; ) {
  147. clk_set_phase(priv->sample_clk,
  148. TUNING_ITERATION_TO_PHASE(i, priv->num_phases));
  149. v = !mmc_send_tuning(mmc, opcode, NULL);
  150. if (i == 0)
  151. first_v = v;
  152. if ((!prev_v) && v) {
  153. range_count++;
  154. ranges[range_count-1].start = i;
  155. }
  156. if (v) {
  157. ranges[range_count-1].end = i;
  158. i++;
  159. } else if (i == priv->num_phases - 1) {
  160. /* No extra skipping rules if we're at the end */
  161. i++;
  162. } else {
  163. /*
  164. * No need to check too close to an invalid
  165. * one since testing bad phases is slow. Skip
  166. * 20 degrees.
  167. */
  168. i += DIV_ROUND_UP(20 * priv->num_phases, 360);
  169. /* Always test the last one */
  170. if (i >= priv->num_phases)
  171. i = priv->num_phases - 1;
  172. }
  173. prev_v = v;
  174. }
  175. if (range_count == 0) {
  176. dev_warn(host->dev, "All phases bad!");
  177. ret = -EIO;
  178. goto free;
  179. }
  180. /* wrap around case, merge the end points */
  181. if ((range_count > 1) && first_v && v) {
  182. ranges[0].start = ranges[range_count-1].start;
  183. range_count--;
  184. }
  185. if (ranges[0].start == 0 && ranges[0].end == priv->num_phases - 1) {
  186. clk_set_phase(priv->sample_clk, priv->default_sample_phase);
  187. dev_info(host->dev, "All phases work, using default phase %d.",
  188. priv->default_sample_phase);
  189. goto free;
  190. }
  191. /* Find the longest range */
  192. for (i = 0; i < range_count; i++) {
  193. int len = (ranges[i].end - ranges[i].start + 1);
  194. if (len < 0)
  195. len += priv->num_phases;
  196. if (longest_range_len < len) {
  197. longest_range_len = len;
  198. longest_range = i;
  199. }
  200. dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
  201. TUNING_ITERATION_TO_PHASE(ranges[i].start,
  202. priv->num_phases),
  203. TUNING_ITERATION_TO_PHASE(ranges[i].end,
  204. priv->num_phases),
  205. len
  206. );
  207. }
  208. dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
  209. TUNING_ITERATION_TO_PHASE(ranges[longest_range].start,
  210. priv->num_phases),
  211. TUNING_ITERATION_TO_PHASE(ranges[longest_range].end,
  212. priv->num_phases),
  213. longest_range_len
  214. );
  215. middle_phase = ranges[longest_range].start + longest_range_len / 2;
  216. middle_phase %= priv->num_phases;
  217. dev_info(host->dev, "Successfully tuned phase to %d\n",
  218. TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases));
  219. clk_set_phase(priv->sample_clk,
  220. TUNING_ITERATION_TO_PHASE(middle_phase,
  221. priv->num_phases));
  222. free:
  223. kfree(ranges);
  224. return ret;
  225. }
  226. static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
  227. {
  228. struct device_node *np = host->dev->of_node;
  229. struct dw_mci_rockchip_priv_data *priv;
  230. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  231. if (!priv)
  232. return -ENOMEM;
  233. if (of_property_read_u32(np, "rockchip,desired-num-phases",
  234. &priv->num_phases))
  235. priv->num_phases = 360;
  236. if (of_property_read_u32(np, "rockchip,default-sample-phase",
  237. &priv->default_sample_phase))
  238. priv->default_sample_phase = 0;
  239. priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
  240. if (IS_ERR(priv->drv_clk))
  241. dev_dbg(host->dev, "ciu-drive not available\n");
  242. priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
  243. if (IS_ERR(priv->sample_clk))
  244. dev_dbg(host->dev, "ciu-sample not available\n");
  245. host->priv = priv;
  246. return 0;
  247. }
  248. static int dw_mci_rockchip_init(struct dw_mci *host)
  249. {
  250. int ret, i;
  251. /* It is slot 8 on Rockchip SoCs */
  252. host->sdio_id0 = 8;
  253. if (of_device_is_compatible(host->dev->of_node, "rockchip,rk3288-dw-mshc")) {
  254. host->bus_hz /= RK3288_CLKGEN_DIV;
  255. /* clock driver will fail if the clock is less than the lowest source clock
  256. * divided by the internal clock divider. Test for the lowest available
  257. * clock and set the minimum freq to clock / clock divider.
  258. */
  259. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  260. ret = clk_round_rate(host->ciu_clk, freqs[i] * RK3288_CLKGEN_DIV);
  261. if (ret > 0) {
  262. host->minimum_speed = ret / RK3288_CLKGEN_DIV;
  263. break;
  264. }
  265. }
  266. if (ret < 0)
  267. dev_warn(host->dev, "no valid minimum freq: %d\n", ret);
  268. }
  269. return 0;
  270. }
  271. static const struct dw_mci_drv_data rk2928_drv_data = {
  272. .init = dw_mci_rockchip_init,
  273. };
  274. static const struct dw_mci_drv_data rk3288_drv_data = {
  275. .common_caps = MMC_CAP_CMD23,
  276. .set_ios = dw_mci_rk3288_set_ios,
  277. .execute_tuning = dw_mci_rk3288_execute_tuning,
  278. .parse_dt = dw_mci_rk3288_parse_dt,
  279. .init = dw_mci_rockchip_init,
  280. };
  281. static const struct of_device_id dw_mci_rockchip_match[] = {
  282. { .compatible = "rockchip,rk2928-dw-mshc",
  283. .data = &rk2928_drv_data },
  284. { .compatible = "rockchip,rk3288-dw-mshc",
  285. .data = &rk3288_drv_data },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
  289. static int dw_mci_rockchip_probe(struct platform_device *pdev)
  290. {
  291. const struct dw_mci_drv_data *drv_data;
  292. const struct of_device_id *match;
  293. int ret;
  294. if (!pdev->dev.of_node)
  295. return -ENODEV;
  296. match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
  297. drv_data = match->data;
  298. pm_runtime_get_noresume(&pdev->dev);
  299. pm_runtime_set_active(&pdev->dev);
  300. pm_runtime_enable(&pdev->dev);
  301. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  302. pm_runtime_use_autosuspend(&pdev->dev);
  303. ret = dw_mci_pltfm_register(pdev, drv_data);
  304. if (ret) {
  305. pm_runtime_disable(&pdev->dev);
  306. pm_runtime_set_suspended(&pdev->dev);
  307. pm_runtime_put_noidle(&pdev->dev);
  308. return ret;
  309. }
  310. pm_runtime_put_autosuspend(&pdev->dev);
  311. return 0;
  312. }
  313. static int dw_mci_rockchip_remove(struct platform_device *pdev)
  314. {
  315. pm_runtime_get_sync(&pdev->dev);
  316. pm_runtime_disable(&pdev->dev);
  317. pm_runtime_put_noidle(&pdev->dev);
  318. dw_mci_pltfm_remove(pdev);
  319. return 0;
  320. }
  321. static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops = {
  322. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  323. pm_runtime_force_resume)
  324. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  325. dw_mci_runtime_resume,
  326. NULL)
  327. };
  328. static struct platform_driver dw_mci_rockchip_pltfm_driver = {
  329. .probe = dw_mci_rockchip_probe,
  330. .remove = dw_mci_rockchip_remove,
  331. .driver = {
  332. .name = "dwmmc_rockchip",
  333. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  334. .of_match_table = dw_mci_rockchip_match,
  335. .pm = &dw_mci_rockchip_dev_pm_ops,
  336. },
  337. };
  338. module_platform_driver(dw_mci_rockchip_pltfm_driver);
  339. MODULE_AUTHOR("Addy Ke <[email protected]>");
  340. MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
  341. MODULE_ALIAS("platform:dwmmc_rockchip");
  342. MODULE_LICENSE("GPL v2");