1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
9 #include <linux/mmc/host.h>
10 #include <linux/of_address.h>
11 #include <linux/mmc/slot-gpio.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
16 #include "dw_mmc-pltfm.h"
18 #define RK3288_CLKGEN_DIV 2
19 #define SDMMC_TIMING_CON0 0x130
20 #define SDMMC_TIMING_CON1 0x134
21 #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
22 #define ROCKCHIP_MMC_DEGREE_MASK 0x3
23 #define ROCKCHIP_MMC_DEGREE_OFFSET 1
24 #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
25 #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
26 #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
27 #define HIWORD_UPDATE(val, mask, shift) \
28 ((val) << (shift) | (mask) << ((shift) + 16))
30 static const unsigned int freqs
[] = { 100000, 200000, 300000, 400000 };
32 struct dw_mci_rockchip_priv_data
{
34 struct clk
*sample_clk
;
35 int default_sample_phase
;
41 * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
42 * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
44 static int rockchip_mmc_get_internal_phase(struct dw_mci
*host
, bool sample
)
46 unsigned long rate
= clk_get_rate(host
->ciu_clk
);
51 /* Constant signal, no measurable phase shift */
56 raw_value
= mci_readl(host
, TIMING_CON1
);
58 raw_value
= mci_readl(host
, TIMING_CON0
);
60 raw_value
>>= ROCKCHIP_MMC_DEGREE_OFFSET
;
61 degrees
= (raw_value
& ROCKCHIP_MMC_DEGREE_MASK
) * 90;
63 if (raw_value
& ROCKCHIP_MMC_DELAY_SEL
) {
64 /* degrees/delaynum * 1000000 */
65 unsigned long factor
= (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC
/ 10) *
68 delay_num
= (raw_value
& ROCKCHIP_MMC_DELAYNUM_MASK
);
69 delay_num
>>= ROCKCHIP_MMC_DELAYNUM_OFFSET
;
70 degrees
+= DIV_ROUND_CLOSEST(delay_num
* factor
, 1000000);
76 static int rockchip_mmc_get_phase(struct dw_mci
*host
, bool sample
)
78 struct dw_mci_rockchip_priv_data
*priv
= host
->priv
;
79 struct clk
*clock
= sample
? priv
->sample_clk
: priv
->drv_clk
;
81 if (priv
->internal_phase
)
82 return rockchip_mmc_get_internal_phase(host
, sample
);
84 return clk_get_phase(clock
);
87 static int rockchip_mmc_set_internal_phase(struct dw_mci
*host
, bool sample
, int degrees
)
89 unsigned long rate
= clk_get_rate(host
->ciu_clk
);
90 u8 nineties
, remainder
;
96 * The below calculation is based on the output clock from
97 * MMC host to the card, which expects the phase clock inherits
98 * the clock rate from its parent, namely the output clock
99 * provider of MMC host. However, things may go wrong if
101 * (2) It is assigned to the wrong parent.
103 * This check help debug the case (1), which seems to be the
104 * most likely problem we often face and which makes it difficult
105 * for people to debug unstable mmc tuning results.
108 dev_err(host
->dev
, "%s: invalid clk rate\n", __func__
);
112 nineties
= degrees
/ 90;
113 remainder
= (degrees
% 90);
116 * Due to the inexact nature of the "fine" delay, we might
117 * actually go non-monotonic. We don't go _too_ monotonic
118 * though, so we should be OK. Here are options of how we may
121 * Ideally we end up with:
122 * 1.0, 2.0, ..., 69.0, 70.0, ..., 89.0, 90.0
124 * On one extreme (if delay is actually 44ps):
125 * .73, 1.5, ..., 50.6, 51.3, ..., 65.3, 90.0
126 * The other (if delay is actually 77ps):
127 * 1.3, 2.6, ..., 88.6. 89.8, ..., 114.0, 90
129 * It's possible we might make a delay that is up to 25
130 * degrees off from what we think we're making. That's OK
131 * though because we should be REALLY far from any bad range.
135 * Convert to delay; do a little extra work to make sure we
136 * don't overflow 32-bit / 64-bit numbers.
138 delay
= 10000000; /* PSECS_PER_SEC / 10000 / 10 */
140 delay
= DIV_ROUND_CLOSEST(delay
,
142 (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC
/ 10));
144 delay_num
= (u8
) min_t(u32
, delay
, 255);
146 raw_value
= delay_num
? ROCKCHIP_MMC_DELAY_SEL
: 0;
147 raw_value
|= delay_num
<< ROCKCHIP_MMC_DELAYNUM_OFFSET
;
148 raw_value
|= nineties
;
151 mci_writel(host
, TIMING_CON1
, HIWORD_UPDATE(raw_value
, 0x07ff, 1));
153 mci_writel(host
, TIMING_CON0
, HIWORD_UPDATE(raw_value
, 0x07ff, 1));
155 dev_dbg(host
->dev
, "set %s_phase(%d) delay_nums=%u actual_degrees=%d\n",
156 sample
? "sample" : "drv", degrees
, delay_num
,
157 rockchip_mmc_get_phase(host
, sample
)
163 static int rockchip_mmc_set_phase(struct dw_mci
*host
, bool sample
, int degrees
)
165 struct dw_mci_rockchip_priv_data
*priv
= host
->priv
;
166 struct clk
*clock
= sample
? priv
->sample_clk
: priv
->drv_clk
;
168 if (priv
->internal_phase
)
169 return rockchip_mmc_set_internal_phase(host
, sample
, degrees
);
171 return clk_set_phase(clock
, degrees
);
174 static void dw_mci_rk3288_set_ios(struct dw_mci
*host
, struct mmc_ios
*ios
)
176 struct dw_mci_rockchip_priv_data
*priv
= host
->priv
;
185 * cclkin: source clock of mmc controller
186 * bus_hz: card interface clock generated by CLKGEN
187 * bus_hz = cclkin / RK3288_CLKGEN_DIV
188 * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
190 * Note: div can only be 0 or 1, but div must be set to 1 for eMMC
193 if (ios
->bus_width
== MMC_BUS_WIDTH_8
&&
194 ios
->timing
== MMC_TIMING_MMC_DDR52
)
195 cclkin
= 2 * ios
->clock
* RK3288_CLKGEN_DIV
;
197 cclkin
= ios
->clock
* RK3288_CLKGEN_DIV
;
199 ret
= clk_set_rate(host
->ciu_clk
, cclkin
);
201 dev_warn(host
->dev
, "failed to set rate %uHz err: %d\n", cclkin
, ret
);
203 bus_hz
= clk_get_rate(host
->ciu_clk
) / RK3288_CLKGEN_DIV
;
204 if (bus_hz
!= host
->bus_hz
) {
205 host
->bus_hz
= bus_hz
;
206 /* force dw_mci_setup_bus() */
207 host
->current_speed
= 0;
210 /* Make sure we use phases which we can enumerate with */
211 if (!IS_ERR(priv
->sample_clk
) && ios
->timing
<= MMC_TIMING_SD_HS
)
212 rockchip_mmc_set_phase(host
, true, priv
->default_sample_phase
);
215 * Set the drive phase offset based on speed mode to achieve hold times.
217 * NOTE: this is _not_ a value that is dynamically tuned and is also
218 * _not_ a value that will vary from board to board. It is a value
219 * that could vary between different SoC models if they had massively
220 * different output clock delays inside their dw_mmc IP block (delay_o),
221 * but since it's OK to overshoot a little we don't need to do complex
222 * calculations and can pick values that will just work for everyone.
224 * When picking values we'll stick with picking 0/90/180/270 since
225 * those can be made very accurately on all known Rockchip SoCs.
227 * Note that these values match values from the DesignWare Databook
228 * tables for the most part except for SDR12 and "ID mode". For those
229 * two modes the databook calculations assume a clock in of 50MHz. As
230 * seen above, we always use a clock in rate that is exactly the
231 * card's input clock (times RK3288_CLKGEN_DIV, but that gets divided
232 * back out before the controller sees it).
234 * From measurement of a single device, it appears that delay_o is
235 * about .5 ns. Since we try to leave a bit of margin, it's expected
236 * that numbers here will be fine even with much larger delay_o
237 * (the 1.4 ns assumed by the DesignWare Databook would result in the
238 * same results, for instance).
240 if (!IS_ERR(priv
->drv_clk
)) {
244 * In almost all cases a 90 degree phase offset will provide
245 * sufficient hold times across all valid input clock rates
246 * assuming delay_o is not absurd for a given SoC. We'll use
251 switch (ios
->timing
) {
252 case MMC_TIMING_MMC_DDR52
:
254 * Since clock in rate with MMC_DDR52 is doubled when
255 * bus width is 8 we need to double the phase offset
256 * to get the same timings.
258 if (ios
->bus_width
== MMC_BUS_WIDTH_8
)
261 case MMC_TIMING_UHS_SDR104
:
262 case MMC_TIMING_MMC_HS200
:
264 * In the case of 150 MHz clock (typical max for
265 * Rockchip SoCs), 90 degree offset will add a delay
266 * of 1.67 ns. That will meet min hold time of .8 ns
267 * as long as clock output delay is < .87 ns. On
268 * SoCs measured this seems to be OK, but it doesn't
269 * hurt to give margin here, so we use 180.
275 rockchip_mmc_set_phase(host
, false, phase
);
279 #define TUNING_ITERATION_TO_PHASE(i, num_phases) \
280 (DIV_ROUND_UP((i) * 360, num_phases))
282 static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot
*slot
, u32 opcode
)
284 struct dw_mci
*host
= slot
->host
;
285 struct dw_mci_rockchip_priv_data
*priv
= host
->priv
;
286 struct mmc_host
*mmc
= slot
->mmc
;
289 bool v
, prev_v
= 0, first_v
;
292 int end
; /* inclusive */
294 struct range_t
*ranges
;
295 unsigned int range_count
= 0;
296 int longest_range_len
= -1;
297 int longest_range
= -1;
301 if (IS_ERR(priv
->sample_clk
)) {
302 dev_err(host
->dev
, "Tuning clock (sample_clk) not defined.\n");
306 ranges
= kmalloc_array(priv
->num_phases
/ 2 + 1,
307 sizeof(*ranges
), GFP_KERNEL
);
311 /* Try each phase and extract good ranges */
312 for (i
= 0; i
< priv
->num_phases
; ) {
313 rockchip_mmc_set_phase(host
, true,
314 TUNING_ITERATION_TO_PHASE(
318 v
= !mmc_send_tuning(mmc
, opcode
, NULL
);
323 if ((!prev_v
) && v
) {
325 ranges
[range_count
-1].start
= i
;
328 ranges
[range_count
-1].end
= i
;
330 } else if (i
== priv
->num_phases
- 1) {
331 /* No extra skipping rules if we're at the end */
335 * No need to check too close to an invalid
336 * one since testing bad phases is slow. Skip
339 i
+= DIV_ROUND_UP(20 * priv
->num_phases
, 360);
341 /* Always test the last one */
342 if (i
>= priv
->num_phases
)
343 i
= priv
->num_phases
- 1;
349 if (range_count
== 0) {
350 dev_warn(host
->dev
, "All phases bad!");
355 /* wrap around case, merge the end points */
356 if ((range_count
> 1) && first_v
&& v
) {
357 ranges
[0].start
= ranges
[range_count
-1].start
;
361 if (ranges
[0].start
== 0 && ranges
[0].end
== priv
->num_phases
- 1) {
362 rockchip_mmc_set_phase(host
, true, priv
->default_sample_phase
);
364 dev_info(host
->dev
, "All phases work, using default phase %d.",
365 priv
->default_sample_phase
);
369 /* Find the longest range */
370 for (i
= 0; i
< range_count
; i
++) {
371 int len
= (ranges
[i
].end
- ranges
[i
].start
+ 1);
374 len
+= priv
->num_phases
;
376 if (longest_range_len
< len
) {
377 longest_range_len
= len
;
381 dev_dbg(host
->dev
, "Good phase range %d-%d (%d len)\n",
382 TUNING_ITERATION_TO_PHASE(ranges
[i
].start
,
384 TUNING_ITERATION_TO_PHASE(ranges
[i
].end
,
390 dev_dbg(host
->dev
, "Best phase range %d-%d (%d len)\n",
391 TUNING_ITERATION_TO_PHASE(ranges
[longest_range
].start
,
393 TUNING_ITERATION_TO_PHASE(ranges
[longest_range
].end
,
398 middle_phase
= ranges
[longest_range
].start
+ longest_range_len
/ 2;
399 middle_phase
%= priv
->num_phases
;
400 phase
= TUNING_ITERATION_TO_PHASE(middle_phase
, priv
->num_phases
);
401 dev_info(host
->dev
, "Successfully tuned phase to %d\n", phase
);
403 rockchip_mmc_set_phase(host
, true, phase
);
410 static int dw_mci_common_parse_dt(struct dw_mci
*host
)
412 struct device_node
*np
= host
->dev
->of_node
;
413 struct dw_mci_rockchip_priv_data
*priv
;
415 priv
= devm_kzalloc(host
->dev
, sizeof(*priv
), GFP_KERNEL
);
419 if (of_property_read_u32(np
, "rockchip,desired-num-phases",
421 priv
->num_phases
= 360;
423 if (of_property_read_u32(np
, "rockchip,default-sample-phase",
424 &priv
->default_sample_phase
))
425 priv
->default_sample_phase
= 0;
432 static int dw_mci_rk3288_parse_dt(struct dw_mci
*host
)
434 struct dw_mci_rockchip_priv_data
*priv
;
437 err
= dw_mci_common_parse_dt(host
);
443 priv
->drv_clk
= devm_clk_get(host
->dev
, "ciu-drive");
444 if (IS_ERR(priv
->drv_clk
))
445 dev_dbg(host
->dev
, "ciu-drive not available\n");
447 priv
->sample_clk
= devm_clk_get(host
->dev
, "ciu-sample");
448 if (IS_ERR(priv
->sample_clk
))
449 dev_dbg(host
->dev
, "ciu-sample not available\n");
451 priv
->internal_phase
= false;
456 static int dw_mci_rk3576_parse_dt(struct dw_mci
*host
)
458 struct dw_mci_rockchip_priv_data
*priv
;
459 int err
= dw_mci_common_parse_dt(host
);
465 priv
->internal_phase
= true;
470 static int dw_mci_rockchip_init(struct dw_mci
*host
)
474 /* It is slot 8 on Rockchip SoCs */
477 if (of_device_is_compatible(host
->dev
->of_node
, "rockchip,rk3288-dw-mshc")) {
478 host
->bus_hz
/= RK3288_CLKGEN_DIV
;
480 /* clock driver will fail if the clock is less than the lowest source clock
481 * divided by the internal clock divider. Test for the lowest available
482 * clock and set the minimum freq to clock / clock divider.
485 for (i
= 0; i
< ARRAY_SIZE(freqs
); i
++) {
486 ret
= clk_round_rate(host
->ciu_clk
, freqs
[i
] * RK3288_CLKGEN_DIV
);
488 host
->minimum_speed
= ret
/ RK3288_CLKGEN_DIV
;
493 dev_warn(host
->dev
, "no valid minimum freq: %d\n", ret
);
499 static const struct dw_mci_drv_data rk2928_drv_data
= {
500 .init
= dw_mci_rockchip_init
,
503 static const struct dw_mci_drv_data rk3288_drv_data
= {
504 .common_caps
= MMC_CAP_CMD23
,
505 .set_ios
= dw_mci_rk3288_set_ios
,
506 .execute_tuning
= dw_mci_rk3288_execute_tuning
,
507 .parse_dt
= dw_mci_rk3288_parse_dt
,
508 .init
= dw_mci_rockchip_init
,
511 static const struct dw_mci_drv_data rk3576_drv_data
= {
512 .common_caps
= MMC_CAP_CMD23
,
513 .set_ios
= dw_mci_rk3288_set_ios
,
514 .execute_tuning
= dw_mci_rk3288_execute_tuning
,
515 .parse_dt
= dw_mci_rk3576_parse_dt
,
516 .init
= dw_mci_rockchip_init
,
519 static const struct of_device_id dw_mci_rockchip_match
[] = {
520 { .compatible
= "rockchip,rk2928-dw-mshc",
521 .data
= &rk2928_drv_data
},
522 { .compatible
= "rockchip,rk3288-dw-mshc",
523 .data
= &rk3288_drv_data
},
524 { .compatible
= "rockchip,rk3576-dw-mshc",
525 .data
= &rk3576_drv_data
},
528 MODULE_DEVICE_TABLE(of
, dw_mci_rockchip_match
);
530 static int dw_mci_rockchip_probe(struct platform_device
*pdev
)
532 const struct dw_mci_drv_data
*drv_data
;
533 const struct of_device_id
*match
;
536 if (!pdev
->dev
.of_node
)
539 match
= of_match_node(dw_mci_rockchip_match
, pdev
->dev
.of_node
);
540 drv_data
= match
->data
;
542 pm_runtime_get_noresume(&pdev
->dev
);
543 pm_runtime_set_active(&pdev
->dev
);
544 pm_runtime_enable(&pdev
->dev
);
545 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 50);
546 pm_runtime_use_autosuspend(&pdev
->dev
);
548 ret
= dw_mci_pltfm_register(pdev
, drv_data
);
550 pm_runtime_disable(&pdev
->dev
);
551 pm_runtime_set_suspended(&pdev
->dev
);
552 pm_runtime_put_noidle(&pdev
->dev
);
556 pm_runtime_put_autosuspend(&pdev
->dev
);
561 static void dw_mci_rockchip_remove(struct platform_device
*pdev
)
563 pm_runtime_get_sync(&pdev
->dev
);
564 pm_runtime_disable(&pdev
->dev
);
565 pm_runtime_put_noidle(&pdev
->dev
);
567 dw_mci_pltfm_remove(pdev
);
570 static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops
= {
571 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
572 pm_runtime_force_resume
)
573 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend
,
574 dw_mci_runtime_resume
,
578 static struct platform_driver dw_mci_rockchip_pltfm_driver
= {
579 .probe
= dw_mci_rockchip_probe
,
580 .remove
= dw_mci_rockchip_remove
,
582 .name
= "dwmmc_rockchip",
583 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
584 .of_match_table
= dw_mci_rockchip_match
,
585 .pm
= &dw_mci_rockchip_dev_pm_ops
,
589 module_platform_driver(dw_mci_rockchip_pltfm_driver
);
591 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
592 MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
593 MODULE_ALIAS("platform:dwmmc_rockchip");
594 MODULE_LICENSE("GPL v2");