dw_mmc-rockchip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. int num_phases;
  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, but div must be set to 1 for eMMC
  41. * DDR52 8-bit mode.
  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 TUNING_ITERATION_TO_PHASE(i, num_phases) \
  122. (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(priv->num_phases / 2 + 1,
  145. sizeof(*ranges), GFP_KERNEL);
  146. if (!ranges)
  147. return -ENOMEM;
  148. /* Try each phase and extract good ranges */
  149. for (i = 0; i < priv->num_phases; ) {
  150. clk_set_phase(priv->sample_clk,
  151. TUNING_ITERATION_TO_PHASE(i, priv->num_phases));
  152. v = !mmc_send_tuning(mmc, opcode, NULL);
  153. if (i == 0)
  154. first_v = v;
  155. if ((!prev_v) && v) {
  156. range_count++;
  157. ranges[range_count-1].start = i;
  158. }
  159. if (v) {
  160. ranges[range_count-1].end = i;
  161. i++;
  162. } else if (i == priv->num_phases - 1) {
  163. /* No extra skipping rules if we're at the end */
  164. i++;
  165. } else {
  166. /*
  167. * No need to check too close to an invalid
  168. * one since testing bad phases is slow. Skip
  169. * 20 degrees.
  170. */
  171. i += DIV_ROUND_UP(20 * priv->num_phases, 360);
  172. /* Always test the last one */
  173. if (i >= priv->num_phases)
  174. i = priv->num_phases - 1;
  175. }
  176. prev_v = v;
  177. }
  178. if (range_count == 0) {
  179. dev_warn(host->dev, "All phases bad!");
  180. ret = -EIO;
  181. goto free;
  182. }
  183. /* wrap around case, merge the end points */
  184. if ((range_count > 1) && first_v && v) {
  185. ranges[0].start = ranges[range_count-1].start;
  186. range_count--;
  187. }
  188. if (ranges[0].start == 0 && ranges[0].end == priv->num_phases - 1) {
  189. clk_set_phase(priv->sample_clk, priv->default_sample_phase);
  190. dev_info(host->dev, "All phases work, using default phase %d.",
  191. priv->default_sample_phase);
  192. goto free;
  193. }
  194. /* Find the longest range */
  195. for (i = 0; i < range_count; i++) {
  196. int len = (ranges[i].end - ranges[i].start + 1);
  197. if (len < 0)
  198. len += priv->num_phases;
  199. if (longest_range_len < len) {
  200. longest_range_len = len;
  201. longest_range = i;
  202. }
  203. dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
  204. TUNING_ITERATION_TO_PHASE(ranges[i].start,
  205. priv->num_phases),
  206. TUNING_ITERATION_TO_PHASE(ranges[i].end,
  207. priv->num_phases),
  208. len
  209. );
  210. }
  211. dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
  212. TUNING_ITERATION_TO_PHASE(ranges[longest_range].start,
  213. priv->num_phases),
  214. TUNING_ITERATION_TO_PHASE(ranges[longest_range].end,
  215. priv->num_phases),
  216. longest_range_len
  217. );
  218. middle_phase = ranges[longest_range].start + longest_range_len / 2;
  219. middle_phase %= priv->num_phases;
  220. dev_info(host->dev, "Successfully tuned phase to %d\n",
  221. TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases));
  222. clk_set_phase(priv->sample_clk,
  223. TUNING_ITERATION_TO_PHASE(middle_phase,
  224. priv->num_phases));
  225. free:
  226. kfree(ranges);
  227. return ret;
  228. }
  229. static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
  230. {
  231. struct device_node *np = host->dev->of_node;
  232. struct dw_mci_rockchip_priv_data *priv;
  233. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  234. if (!priv)
  235. return -ENOMEM;
  236. if (of_property_read_u32(np, "rockchip,desired-num-phases",
  237. &priv->num_phases))
  238. priv->num_phases = 360;
  239. if (of_property_read_u32(np, "rockchip,default-sample-phase",
  240. &priv->default_sample_phase))
  241. priv->default_sample_phase = 0;
  242. priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
  243. if (IS_ERR(priv->drv_clk))
  244. dev_dbg(host->dev, "ciu-drive not available\n");
  245. priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
  246. if (IS_ERR(priv->sample_clk))
  247. dev_dbg(host->dev, "ciu-sample not available\n");
  248. host->priv = priv;
  249. return 0;
  250. }
  251. static int dw_mci_rockchip_init(struct dw_mci *host)
  252. {
  253. /* It is slot 8 on Rockchip SoCs */
  254. host->sdio_id0 = 8;
  255. if (of_device_is_compatible(host->dev->of_node,
  256. "rockchip,rk3288-dw-mshc"))
  257. host->bus_hz /= RK3288_CLKGEN_DIV;
  258. return 0;
  259. }
  260. /* Common capabilities of RK3288 SoC */
  261. static unsigned long dw_mci_rk3288_dwmmc_caps[4] = {
  262. MMC_CAP_CMD23,
  263. MMC_CAP_CMD23,
  264. MMC_CAP_CMD23,
  265. MMC_CAP_CMD23,
  266. };
  267. static const struct dw_mci_drv_data rk2928_drv_data = {
  268. .init = dw_mci_rockchip_init,
  269. };
  270. static const struct dw_mci_drv_data rk3288_drv_data = {
  271. .caps = dw_mci_rk3288_dwmmc_caps,
  272. .num_caps = ARRAY_SIZE(dw_mci_rk3288_dwmmc_caps),
  273. .set_ios = dw_mci_rk3288_set_ios,
  274. .execute_tuning = dw_mci_rk3288_execute_tuning,
  275. .parse_dt = dw_mci_rk3288_parse_dt,
  276. .init = dw_mci_rockchip_init,
  277. };
  278. static const struct of_device_id dw_mci_rockchip_match[] = {
  279. { .compatible = "rockchip,rk2928-dw-mshc",
  280. .data = &rk2928_drv_data },
  281. { .compatible = "rockchip,rk3288-dw-mshc",
  282. .data = &rk3288_drv_data },
  283. {},
  284. };
  285. MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
  286. static int dw_mci_rockchip_probe(struct platform_device *pdev)
  287. {
  288. const struct dw_mci_drv_data *drv_data;
  289. const struct of_device_id *match;
  290. int ret;
  291. if (!pdev->dev.of_node)
  292. return -ENODEV;
  293. match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
  294. drv_data = match->data;
  295. pm_runtime_get_noresume(&pdev->dev);
  296. pm_runtime_set_active(&pdev->dev);
  297. pm_runtime_enable(&pdev->dev);
  298. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  299. pm_runtime_use_autosuspend(&pdev->dev);
  300. ret = dw_mci_pltfm_register(pdev, drv_data);
  301. if (ret) {
  302. pm_runtime_disable(&pdev->dev);
  303. pm_runtime_set_suspended(&pdev->dev);
  304. pm_runtime_put_noidle(&pdev->dev);
  305. return ret;
  306. }
  307. pm_runtime_put_autosuspend(&pdev->dev);
  308. return 0;
  309. }
  310. static int dw_mci_rockchip_remove(struct platform_device *pdev)
  311. {
  312. pm_runtime_get_sync(&pdev->dev);
  313. pm_runtime_disable(&pdev->dev);
  314. pm_runtime_put_noidle(&pdev->dev);
  315. return dw_mci_pltfm_remove(pdev);
  316. }
  317. static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops = {
  318. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  319. pm_runtime_force_resume)
  320. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  321. dw_mci_runtime_resume,
  322. NULL)
  323. };
  324. static struct platform_driver dw_mci_rockchip_pltfm_driver = {
  325. .probe = dw_mci_rockchip_probe,
  326. .remove = dw_mci_rockchip_remove,
  327. .driver = {
  328. .name = "dwmmc_rockchip",
  329. .of_match_table = dw_mci_rockchip_match,
  330. .pm = &dw_mci_rockchip_dev_pm_ops,
  331. },
  332. };
  333. module_platform_driver(dw_mci_rockchip_pltfm_driver);
  334. MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
  335. MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
  336. MODULE_ALIAS("platform:dwmmc_rockchip");
  337. MODULE_LICENSE("GPL v2");