dw_mmc-rockchip.c 10 KB

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