dw_mmc-rockchip.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/mmc/dw_mmc.h>
  14. #include <linux/of_address.h>
  15. #include <linux/slab.h>
  16. #include "dw_mmc.h"
  17. #include "dw_mmc-pltfm.h"
  18. #define RK3288_CLKGEN_DIV 2
  19. struct dw_mci_rockchip_priv_data {
  20. struct clk *drv_clk;
  21. struct clk *sample_clk;
  22. int default_sample_phase;
  23. };
  24. static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  25. {
  26. struct dw_mci_rockchip_priv_data *priv = host->priv;
  27. int ret;
  28. unsigned int cclkin;
  29. u32 bus_hz;
  30. if (ios->clock == 0)
  31. return;
  32. /*
  33. * cclkin: source clock of mmc controller
  34. * bus_hz: card interface clock generated by CLKGEN
  35. * bus_hz = cclkin / RK3288_CLKGEN_DIV
  36. * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
  37. *
  38. * Note: div can only be 0 or 1
  39. * if DDR50 8bit mode(only emmc work in 8bit mode),
  40. * div must be set 1
  41. */
  42. if (ios->bus_width == MMC_BUS_WIDTH_8 &&
  43. ios->timing == MMC_TIMING_MMC_DDR52)
  44. cclkin = 2 * ios->clock * RK3288_CLKGEN_DIV;
  45. else
  46. cclkin = ios->clock * RK3288_CLKGEN_DIV;
  47. ret = clk_set_rate(host->ciu_clk, cclkin);
  48. if (ret)
  49. dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
  50. bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
  51. if (bus_hz != host->bus_hz) {
  52. host->bus_hz = bus_hz;
  53. /* force dw_mci_setup_bus() */
  54. host->current_speed = 0;
  55. }
  56. /* Make sure we use phases which we can enumerate with */
  57. if (!IS_ERR(priv->sample_clk))
  58. clk_set_phase(priv->sample_clk, priv->default_sample_phase);
  59. /*
  60. * Set the drive phase offset based on speed mode to achieve hold times.
  61. *
  62. * NOTE: this is _not_ a value that is dynamically tuned and is also
  63. * _not_ a value that will vary from board to board. It is a value
  64. * that could vary between different SoC models if they had massively
  65. * different output clock delays inside their dw_mmc IP block (delay_o),
  66. * but since it's OK to overshoot a little we don't need to do complex
  67. * calculations and can pick values that will just work for everyone.
  68. *
  69. * When picking values we'll stick with picking 0/90/180/270 since
  70. * those can be made very accurately on all known Rockchip SoCs.
  71. *
  72. * Note that these values match values from the DesignWare Databook
  73. * tables for the most part except for SDR12 and "ID mode". For those
  74. * two modes the databook calculations assume a clock in of 50MHz. As
  75. * seen above, we always use a clock in rate that is exactly the
  76. * card's input clock (times RK3288_CLKGEN_DIV, but that gets divided
  77. * back out before the controller sees it).
  78. *
  79. * From measurement of a single device, it appears that delay_o is
  80. * about .5 ns. Since we try to leave a bit of margin, it's expected
  81. * that numbers here will be fine even with much larger delay_o
  82. * (the 1.4 ns assumed by the DesignWare Databook would result in the
  83. * same results, for instance).
  84. */
  85. if (!IS_ERR(priv->drv_clk)) {
  86. int phase;
  87. /*
  88. * In almost all cases a 90 degree phase offset will provide
  89. * sufficient hold times across all valid input clock rates
  90. * assuming delay_o is not absurd for a given SoC. We'll use
  91. * that as a default.
  92. */
  93. phase = 90;
  94. switch (ios->timing) {
  95. case MMC_TIMING_MMC_DDR52:
  96. /*
  97. * Since clock in rate with MMC_DDR52 is doubled when
  98. * bus width is 8 we need to double the phase offset
  99. * to get the same timings.
  100. */
  101. if (ios->bus_width == MMC_BUS_WIDTH_8)
  102. phase = 180;
  103. break;
  104. case MMC_TIMING_UHS_SDR104:
  105. case MMC_TIMING_MMC_HS200:
  106. /*
  107. * In the case of 150 MHz clock (typical max for
  108. * Rockchip SoCs), 90 degree offset will add a delay
  109. * of 1.67 ns. That will meet min hold time of .8 ns
  110. * as long as clock output delay is < .87 ns. On
  111. * SoCs measured this seems to be OK, but it doesn't
  112. * hurt to give margin here, so we use 180.
  113. */
  114. phase = 180;
  115. break;
  116. }
  117. clk_set_phase(priv->drv_clk, phase);
  118. }
  119. }
  120. #define NUM_PHASES 360
  121. #define TUNING_ITERATION_TO_PHASE(i) (DIV_ROUND_UP((i) * 360, NUM_PHASES))
  122. static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  123. {
  124. struct dw_mci *host = slot->host;
  125. struct dw_mci_rockchip_priv_data *priv = host->priv;
  126. struct mmc_host *mmc = slot->mmc;
  127. int ret = 0;
  128. int i;
  129. bool v, prev_v = 0, first_v;
  130. struct range_t {
  131. int start;
  132. int end; /* inclusive */
  133. };
  134. struct range_t *ranges;
  135. unsigned int range_count = 0;
  136. int longest_range_len = -1;
  137. int longest_range = -1;
  138. int middle_phase;
  139. if (IS_ERR(priv->sample_clk)) {
  140. dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
  141. return -EIO;
  142. }
  143. ranges = kmalloc_array(NUM_PHASES / 2 + 1, sizeof(*ranges), GFP_KERNEL);
  144. if (!ranges)
  145. return -ENOMEM;
  146. /* Try each phase and extract good ranges */
  147. for (i = 0; i < NUM_PHASES; ) {
  148. clk_set_phase(priv->sample_clk, TUNING_ITERATION_TO_PHASE(i));
  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 == 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 * NUM_PHASES, 360);
  169. /* Always test the last one */
  170. if (i >= NUM_PHASES)
  171. i = 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 == 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 += 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. TUNING_ITERATION_TO_PHASE(ranges[i].end),
  203. len
  204. );
  205. }
  206. dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
  207. TUNING_ITERATION_TO_PHASE(ranges[longest_range].start),
  208. TUNING_ITERATION_TO_PHASE(ranges[longest_range].end),
  209. longest_range_len
  210. );
  211. middle_phase = ranges[longest_range].start + longest_range_len / 2;
  212. middle_phase %= NUM_PHASES;
  213. dev_info(host->dev, "Successfully tuned phase to %d\n",
  214. TUNING_ITERATION_TO_PHASE(middle_phase));
  215. clk_set_phase(priv->sample_clk,
  216. TUNING_ITERATION_TO_PHASE(middle_phase));
  217. free:
  218. kfree(ranges);
  219. return ret;
  220. }
  221. static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
  222. {
  223. struct device_node *np = host->dev->of_node;
  224. struct dw_mci_rockchip_priv_data *priv;
  225. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  226. if (!priv)
  227. return -ENOMEM;
  228. if (of_property_read_u32(np, "rockchip,default-sample-phase",
  229. &priv->default_sample_phase))
  230. priv->default_sample_phase = 0;
  231. priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
  232. if (IS_ERR(priv->drv_clk))
  233. dev_dbg(host->dev, "ciu_drv not available\n");
  234. priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
  235. if (IS_ERR(priv->sample_clk))
  236. dev_dbg(host->dev, "ciu_sample not available\n");
  237. host->priv = priv;
  238. return 0;
  239. }
  240. static int dw_mci_rockchip_init(struct dw_mci *host)
  241. {
  242. /* It is slot 8 on Rockchip SoCs */
  243. host->sdio_id0 = 8;
  244. if (of_device_is_compatible(host->dev->of_node,
  245. "rockchip,rk3288-dw-mshc"))
  246. host->bus_hz /= RK3288_CLKGEN_DIV;
  247. return 0;
  248. }
  249. /* Common capabilities of RK3288 SoC */
  250. static unsigned long dw_mci_rk3288_dwmmc_caps[4] = {
  251. MMC_CAP_CMD23,
  252. MMC_CAP_CMD23,
  253. MMC_CAP_CMD23,
  254. MMC_CAP_CMD23,
  255. };
  256. static const struct dw_mci_drv_data rk2928_drv_data = {
  257. .init = dw_mci_rockchip_init,
  258. };
  259. static const struct dw_mci_drv_data rk3288_drv_data = {
  260. .caps = dw_mci_rk3288_dwmmc_caps,
  261. .set_ios = dw_mci_rk3288_set_ios,
  262. .execute_tuning = dw_mci_rk3288_execute_tuning,
  263. .parse_dt = dw_mci_rk3288_parse_dt,
  264. .init = dw_mci_rockchip_init,
  265. };
  266. static const struct of_device_id dw_mci_rockchip_match[] = {
  267. { .compatible = "rockchip,rk2928-dw-mshc",
  268. .data = &rk2928_drv_data },
  269. { .compatible = "rockchip,rk3288-dw-mshc",
  270. .data = &rk3288_drv_data },
  271. {},
  272. };
  273. MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
  274. static int dw_mci_rockchip_probe(struct platform_device *pdev)
  275. {
  276. const struct dw_mci_drv_data *drv_data;
  277. const struct of_device_id *match;
  278. if (!pdev->dev.of_node)
  279. return -ENODEV;
  280. match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
  281. drv_data = match->data;
  282. return dw_mci_pltfm_register(pdev, drv_data);
  283. }
  284. static struct platform_driver dw_mci_rockchip_pltfm_driver = {
  285. .probe = dw_mci_rockchip_probe,
  286. .remove = dw_mci_pltfm_remove,
  287. .driver = {
  288. .name = "dwmmc_rockchip",
  289. .of_match_table = dw_mci_rockchip_match,
  290. .pm = &dw_mci_pltfm_pmops,
  291. },
  292. };
  293. module_platform_driver(dw_mci_rockchip_pltfm_driver);
  294. MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
  295. MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
  296. MODULE_ALIAS("platform:dwmmc_rockchip");
  297. MODULE_LICENSE("GPL v2");