dw_mmc-rockchip.c 10 KB

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