dw_mmc-rockchip.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. #define SDMMC_TIMING_CON0 0x130
  17. #define SDMMC_TIMING_CON1 0x134
  18. #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
  19. #define ROCKCHIP_MMC_DEGREE_MASK 0x3
  20. #define ROCKCHIP_MMC_DEGREE_OFFSET 1
  21. #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
  22. #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
  23. #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
  24. #define HIWORD_UPDATE(val, mask, shift) \
  25. ((val) << (shift) | (mask) << ((shift) + 16))
  26. static const unsigned int freqs[] = { 100000, 200000, 300000, 400000 };
  27. struct dw_mci_rockchip_priv_data {
  28. struct clk *drv_clk;
  29. struct clk *sample_clk;
  30. int default_sample_phase;
  31. int num_phases;
  32. bool internal_phase;
  33. };
  34. /*
  35. * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
  36. * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
  37. */
  38. static int rockchip_mmc_get_internal_phase(struct dw_mci *host, bool sample)
  39. {
  40. unsigned long rate = clk_get_rate(host->ciu_clk);
  41. u32 raw_value;
  42. u16 degrees;
  43. u32 delay_num = 0;
  44. /* Constant signal, no measurable phase shift */
  45. if (!rate)
  46. return 0;
  47. if (sample)
  48. raw_value = mci_readl(host, TIMING_CON1);
  49. else
  50. raw_value = mci_readl(host, TIMING_CON0);
  51. raw_value >>= ROCKCHIP_MMC_DEGREE_OFFSET;
  52. degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
  53. if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
  54. /* degrees/delaynum * 1000000 */
  55. unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
  56. 36 * (rate / 10000);
  57. delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
  58. delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
  59. degrees += DIV_ROUND_CLOSEST(delay_num * factor, 1000000);
  60. }
  61. return degrees % 360;
  62. }
  63. static int rockchip_mmc_get_phase(struct dw_mci *host, bool sample)
  64. {
  65. struct dw_mci_rockchip_priv_data *priv = host->priv;
  66. struct clk *clock = sample ? priv->sample_clk : priv->drv_clk;
  67. if (priv->internal_phase)
  68. return rockchip_mmc_get_internal_phase(host, sample);
  69. else
  70. return clk_get_phase(clock);
  71. }
  72. static int rockchip_mmc_set_internal_phase(struct dw_mci *host, bool sample, int degrees)
  73. {
  74. unsigned long rate = clk_get_rate(host->ciu_clk);
  75. u8 nineties, remainder;
  76. u8 delay_num;
  77. u32 raw_value;
  78. u32 delay;
  79. /*
  80. * The below calculation is based on the output clock from
  81. * MMC host to the card, which expects the phase clock inherits
  82. * the clock rate from its parent, namely the output clock
  83. * provider of MMC host. However, things may go wrong if
  84. * (1) It is orphan.
  85. * (2) It is assigned to the wrong parent.
  86. *
  87. * This check help debug the case (1), which seems to be the
  88. * most likely problem we often face and which makes it difficult
  89. * for people to debug unstable mmc tuning results.
  90. */
  91. if (!rate) {
  92. dev_err(host->dev, "%s: invalid clk rate\n", __func__);
  93. return -EINVAL;
  94. }
  95. nineties = degrees / 90;
  96. remainder = (degrees % 90);
  97. /*
  98. * Due to the inexact nature of the "fine" delay, we might
  99. * actually go non-monotonic. We don't go _too_ monotonic
  100. * though, so we should be OK. Here are options of how we may
  101. * work:
  102. *
  103. * Ideally we end up with:
  104. * 1.0, 2.0, ..., 69.0, 70.0, ..., 89.0, 90.0
  105. *
  106. * On one extreme (if delay is actually 44ps):
  107. * .73, 1.5, ..., 50.6, 51.3, ..., 65.3, 90.0
  108. * The other (if delay is actually 77ps):
  109. * 1.3, 2.6, ..., 88.6. 89.8, ..., 114.0, 90
  110. *
  111. * It's possible we might make a delay that is up to 25
  112. * degrees off from what we think we're making. That's OK
  113. * though because we should be REALLY far from any bad range.
  114. */
  115. /*
  116. * Convert to delay; do a little extra work to make sure we
  117. * don't overflow 32-bit / 64-bit numbers.
  118. */
  119. delay = 10000000; /* PSECS_PER_SEC / 10000 / 10 */
  120. delay *= remainder;
  121. delay = DIV_ROUND_CLOSEST(delay,
  122. (rate / 1000) * 36 *
  123. (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10));
  124. delay_num = (u8) min_t(u32, delay, 255);
  125. raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
  126. raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
  127. raw_value |= nineties;
  128. if (sample)
  129. mci_writel(host, TIMING_CON1, HIWORD_UPDATE(raw_value, 0x07ff, 1));
  130. else
  131. mci_writel(host, TIMING_CON0, HIWORD_UPDATE(raw_value, 0x07ff, 1));
  132. dev_dbg(host->dev, "set %s_phase(%d) delay_nums=%u actual_degrees=%d\n",
  133. sample ? "sample" : "drv", degrees, delay_num,
  134. rockchip_mmc_get_phase(host, sample)
  135. );
  136. return 0;
  137. }
  138. static int rockchip_mmc_set_phase(struct dw_mci *host, bool sample, int degrees)
  139. {
  140. struct dw_mci_rockchip_priv_data *priv = host->priv;
  141. struct clk *clock = sample ? priv->sample_clk : priv->drv_clk;
  142. if (priv->internal_phase)
  143. return rockchip_mmc_set_internal_phase(host, sample, degrees);
  144. else
  145. return clk_set_phase(clock, degrees);
  146. }
  147. static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  148. {
  149. struct dw_mci_rockchip_priv_data *priv = host->priv;
  150. int ret;
  151. unsigned int cclkin;
  152. u32 bus_hz;
  153. if (ios->clock == 0)
  154. return;
  155. /*
  156. * cclkin: source clock of mmc controller
  157. * bus_hz: card interface clock generated by CLKGEN
  158. * bus_hz = cclkin / RK3288_CLKGEN_DIV
  159. * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
  160. *
  161. * Note: div can only be 0 or 1, but div must be set to 1 for eMMC
  162. * DDR52 8-bit mode.
  163. */
  164. if (ios->bus_width == MMC_BUS_WIDTH_8 &&
  165. ios->timing == MMC_TIMING_MMC_DDR52)
  166. cclkin = 2 * ios->clock * RK3288_CLKGEN_DIV;
  167. else
  168. cclkin = ios->clock * RK3288_CLKGEN_DIV;
  169. ret = clk_set_rate(host->ciu_clk, cclkin);
  170. if (ret)
  171. dev_warn(host->dev, "failed to set rate %uHz err: %d\n", cclkin, ret);
  172. bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
  173. if (bus_hz != host->bus_hz) {
  174. host->bus_hz = bus_hz;
  175. /* force dw_mci_setup_bus() */
  176. host->current_speed = 0;
  177. }
  178. /* Make sure we use phases which we can enumerate with */
  179. if (!IS_ERR(priv->sample_clk) && ios->timing <= MMC_TIMING_SD_HS)
  180. rockchip_mmc_set_phase(host, true, priv->default_sample_phase);
  181. /*
  182. * Set the drive phase offset based on speed mode to achieve hold times.
  183. *
  184. * NOTE: this is _not_ a value that is dynamically tuned and is also
  185. * _not_ a value that will vary from board to board. It is a value
  186. * that could vary between different SoC models if they had massively
  187. * different output clock delays inside their dw_mmc IP block (delay_o),
  188. * but since it's OK to overshoot a little we don't need to do complex
  189. * calculations and can pick values that will just work for everyone.
  190. *
  191. * When picking values we'll stick with picking 0/90/180/270 since
  192. * those can be made very accurately on all known Rockchip SoCs.
  193. *
  194. * Note that these values match values from the DesignWare Databook
  195. * tables for the most part except for SDR12 and "ID mode". For those
  196. * two modes the databook calculations assume a clock in of 50MHz. As
  197. * seen above, we always use a clock in rate that is exactly the
  198. * card's input clock (times RK3288_CLKGEN_DIV, but that gets divided
  199. * back out before the controller sees it).
  200. *
  201. * From measurement of a single device, it appears that delay_o is
  202. * about .5 ns. Since we try to leave a bit of margin, it's expected
  203. * that numbers here will be fine even with much larger delay_o
  204. * (the 1.4 ns assumed by the DesignWare Databook would result in the
  205. * same results, for instance).
  206. */
  207. if (!IS_ERR(priv->drv_clk)) {
  208. int phase;
  209. /*
  210. * In almost all cases a 90 degree phase offset will provide
  211. * sufficient hold times across all valid input clock rates
  212. * assuming delay_o is not absurd for a given SoC. We'll use
  213. * that as a default.
  214. */
  215. phase = 90;
  216. switch (ios->timing) {
  217. case MMC_TIMING_MMC_DDR52:
  218. /*
  219. * Since clock in rate with MMC_DDR52 is doubled when
  220. * bus width is 8 we need to double the phase offset
  221. * to get the same timings.
  222. */
  223. if (ios->bus_width == MMC_BUS_WIDTH_8)
  224. phase = 180;
  225. break;
  226. case MMC_TIMING_UHS_SDR104:
  227. case MMC_TIMING_MMC_HS200:
  228. /*
  229. * In the case of 150 MHz clock (typical max for
  230. * Rockchip SoCs), 90 degree offset will add a delay
  231. * of 1.67 ns. That will meet min hold time of .8 ns
  232. * as long as clock output delay is < .87 ns. On
  233. * SoCs measured this seems to be OK, but it doesn't
  234. * hurt to give margin here, so we use 180.
  235. */
  236. phase = 180;
  237. break;
  238. }
  239. rockchip_mmc_set_phase(host, false, phase);
  240. }
  241. }
  242. #define TUNING_ITERATION_TO_PHASE(i, num_phases) \
  243. (DIV_ROUND_UP((i) * 360, num_phases))
  244. static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  245. {
  246. struct dw_mci *host = slot->host;
  247. struct dw_mci_rockchip_priv_data *priv = host->priv;
  248. struct mmc_host *mmc = slot->mmc;
  249. int ret = 0;
  250. int i;
  251. bool v, prev_v = 0, first_v;
  252. struct range_t {
  253. int start;
  254. int end; /* inclusive */
  255. };
  256. struct range_t *ranges;
  257. unsigned int range_count = 0;
  258. int longest_range_len = -1;
  259. int longest_range = -1;
  260. int middle_phase;
  261. int phase;
  262. if (IS_ERR(priv->sample_clk)) {
  263. dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
  264. return -EIO;
  265. }
  266. ranges = kmalloc_array(priv->num_phases / 2 + 1,
  267. sizeof(*ranges), GFP_KERNEL);
  268. if (!ranges)
  269. return -ENOMEM;
  270. /* Try each phase and extract good ranges */
  271. for (i = 0; i < priv->num_phases; ) {
  272. rockchip_mmc_set_phase(host, true,
  273. TUNING_ITERATION_TO_PHASE(
  274. i,
  275. priv->num_phases));
  276. v = !mmc_send_tuning(mmc, opcode, NULL);
  277. if (i == 0)
  278. first_v = v;
  279. if ((!prev_v) && v) {
  280. range_count++;
  281. ranges[range_count-1].start = i;
  282. }
  283. if (v) {
  284. ranges[range_count-1].end = i;
  285. i++;
  286. } else if (i == priv->num_phases - 1) {
  287. /* No extra skipping rules if we're at the end */
  288. i++;
  289. } else {
  290. /*
  291. * No need to check too close to an invalid
  292. * one since testing bad phases is slow. Skip
  293. * 20 degrees.
  294. */
  295. i += DIV_ROUND_UP(20 * priv->num_phases, 360);
  296. /* Always test the last one */
  297. if (i >= priv->num_phases)
  298. i = priv->num_phases - 1;
  299. }
  300. prev_v = v;
  301. }
  302. if (range_count == 0) {
  303. dev_warn(host->dev, "All phases bad!");
  304. ret = -EIO;
  305. goto free;
  306. }
  307. /* wrap around case, merge the end points */
  308. if ((range_count > 1) && first_v && v) {
  309. ranges[0].start = ranges[range_count-1].start;
  310. range_count--;
  311. }
  312. if (ranges[0].start == 0 && ranges[0].end == priv->num_phases - 1) {
  313. rockchip_mmc_set_phase(host, true, priv->default_sample_phase);
  314. dev_info(host->dev, "All phases work, using default phase %d.",
  315. priv->default_sample_phase);
  316. goto free;
  317. }
  318. /* Find the longest range */
  319. for (i = 0; i < range_count; i++) {
  320. int len = (ranges[i].end - ranges[i].start + 1);
  321. if (len < 0)
  322. len += priv->num_phases;
  323. if (longest_range_len < len) {
  324. longest_range_len = len;
  325. longest_range = i;
  326. }
  327. dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
  328. TUNING_ITERATION_TO_PHASE(ranges[i].start,
  329. priv->num_phases),
  330. TUNING_ITERATION_TO_PHASE(ranges[i].end,
  331. priv->num_phases),
  332. len
  333. );
  334. }
  335. dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
  336. TUNING_ITERATION_TO_PHASE(ranges[longest_range].start,
  337. priv->num_phases),
  338. TUNING_ITERATION_TO_PHASE(ranges[longest_range].end,
  339. priv->num_phases),
  340. longest_range_len
  341. );
  342. middle_phase = ranges[longest_range].start + longest_range_len / 2;
  343. middle_phase %= priv->num_phases;
  344. phase = TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases);
  345. dev_info(host->dev, "Successfully tuned phase to %d\n", phase);
  346. rockchip_mmc_set_phase(host, true, phase);
  347. free:
  348. kfree(ranges);
  349. return ret;
  350. }
  351. static int dw_mci_common_parse_dt(struct dw_mci *host)
  352. {
  353. struct device_node *np = host->dev->of_node;
  354. struct dw_mci_rockchip_priv_data *priv;
  355. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  356. if (!priv)
  357. return -ENOMEM;
  358. if (of_property_read_u32(np, "rockchip,desired-num-phases",
  359. &priv->num_phases))
  360. priv->num_phases = 360;
  361. if (of_property_read_u32(np, "rockchip,default-sample-phase",
  362. &priv->default_sample_phase))
  363. priv->default_sample_phase = 0;
  364. host->priv = priv;
  365. return 0;
  366. }
  367. static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
  368. {
  369. struct dw_mci_rockchip_priv_data *priv;
  370. int err;
  371. err = dw_mci_common_parse_dt(host);
  372. if (err)
  373. return err;
  374. priv = host->priv;
  375. priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
  376. if (IS_ERR(priv->drv_clk))
  377. dev_dbg(host->dev, "ciu-drive not available\n");
  378. priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
  379. if (IS_ERR(priv->sample_clk))
  380. dev_dbg(host->dev, "ciu-sample not available\n");
  381. priv->internal_phase = false;
  382. return 0;
  383. }
  384. static int dw_mci_rk3576_parse_dt(struct dw_mci *host)
  385. {
  386. struct dw_mci_rockchip_priv_data *priv;
  387. int err = dw_mci_common_parse_dt(host);
  388. if (err)
  389. return err;
  390. priv = host->priv;
  391. priv->internal_phase = true;
  392. return 0;
  393. }
  394. static int dw_mci_rockchip_init(struct dw_mci *host)
  395. {
  396. int ret, i;
  397. /* It is slot 8 on Rockchip SoCs */
  398. host->sdio_id0 = 8;
  399. if (of_device_is_compatible(host->dev->of_node, "rockchip,rk3288-dw-mshc")) {
  400. host->bus_hz /= RK3288_CLKGEN_DIV;
  401. /* clock driver will fail if the clock is less than the lowest source clock
  402. * divided by the internal clock divider. Test for the lowest available
  403. * clock and set the minimum freq to clock / clock divider.
  404. */
  405. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  406. ret = clk_round_rate(host->ciu_clk, freqs[i] * RK3288_CLKGEN_DIV);
  407. if (ret > 0) {
  408. host->minimum_speed = ret / RK3288_CLKGEN_DIV;
  409. break;
  410. }
  411. }
  412. if (ret < 0)
  413. dev_warn(host->dev, "no valid minimum freq: %d\n", ret);
  414. }
  415. return 0;
  416. }
  417. static const struct dw_mci_drv_data rk2928_drv_data = {
  418. .init = dw_mci_rockchip_init,
  419. };
  420. static const struct dw_mci_drv_data rk3288_drv_data = {
  421. .common_caps = MMC_CAP_CMD23,
  422. .set_ios = dw_mci_rk3288_set_ios,
  423. .execute_tuning = dw_mci_rk3288_execute_tuning,
  424. .parse_dt = dw_mci_rk3288_parse_dt,
  425. .init = dw_mci_rockchip_init,
  426. };
  427. static const struct dw_mci_drv_data rk3576_drv_data = {
  428. .common_caps = MMC_CAP_CMD23,
  429. .set_ios = dw_mci_rk3288_set_ios,
  430. .execute_tuning = dw_mci_rk3288_execute_tuning,
  431. .parse_dt = dw_mci_rk3576_parse_dt,
  432. .init = dw_mci_rockchip_init,
  433. };
  434. static const struct of_device_id dw_mci_rockchip_match[] = {
  435. { .compatible = "rockchip,rk2928-dw-mshc",
  436. .data = &rk2928_drv_data },
  437. { .compatible = "rockchip,rk3288-dw-mshc",
  438. .data = &rk3288_drv_data },
  439. { .compatible = "rockchip,rk3576-dw-mshc",
  440. .data = &rk3576_drv_data },
  441. {},
  442. };
  443. MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
  444. static int dw_mci_rockchip_probe(struct platform_device *pdev)
  445. {
  446. const struct dw_mci_drv_data *drv_data;
  447. const struct of_device_id *match;
  448. int ret;
  449. if (!pdev->dev.of_node)
  450. return -ENODEV;
  451. match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
  452. drv_data = match->data;
  453. pm_runtime_get_noresume(&pdev->dev);
  454. pm_runtime_set_active(&pdev->dev);
  455. pm_runtime_enable(&pdev->dev);
  456. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  457. pm_runtime_use_autosuspend(&pdev->dev);
  458. ret = dw_mci_pltfm_register(pdev, drv_data);
  459. if (ret) {
  460. pm_runtime_disable(&pdev->dev);
  461. pm_runtime_set_suspended(&pdev->dev);
  462. pm_runtime_put_noidle(&pdev->dev);
  463. return ret;
  464. }
  465. pm_runtime_put_autosuspend(&pdev->dev);
  466. return 0;
  467. }
  468. static void dw_mci_rockchip_remove(struct platform_device *pdev)
  469. {
  470. pm_runtime_get_sync(&pdev->dev);
  471. pm_runtime_disable(&pdev->dev);
  472. pm_runtime_put_noidle(&pdev->dev);
  473. dw_mci_pltfm_remove(pdev);
  474. }
  475. static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops = {
  476. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  477. pm_runtime_force_resume)
  478. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  479. dw_mci_runtime_resume,
  480. NULL)
  481. };
  482. static struct platform_driver dw_mci_rockchip_pltfm_driver = {
  483. .probe = dw_mci_rockchip_probe,
  484. .remove_new = dw_mci_rockchip_remove,
  485. .driver = {
  486. .name = "dwmmc_rockchip",
  487. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  488. .of_match_table = dw_mci_rockchip_match,
  489. .pm = &dw_mci_rockchip_dev_pm_ops,
  490. },
  491. };
  492. module_platform_driver(dw_mci_rockchip_pltfm_driver);
  493. MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
  494. MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
  495. MODULE_ALIAS("platform:dwmmc_rockchip");
  496. MODULE_LICENSE("GPL v2");