1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 */
5
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/clk.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>
14
15 #include "dw_mmc.h"
16 #include "dw_mmc-pltfm.h"
17
18 #define RK3288_CLKGEN_DIV 2
19 #define RK3288_CLKGEN_MUL 2
20 #define RK_NINTY_DEGREE_PHASE 90
21 #define RK_ONE_HUNDRED_EIGHTY_DEGREE_PHASE 180
22 #define RK_TWO_HUNDRED_SEVENTY_DEGREE_PHASE 270
23 #define RK_THREE_HUNDRED_SIXTY_DEGREE_PHASE 360
24 #define RK3288_PHASE_DIV 2
25 #define RK3288_PHASE_MUL 20
26 #define RK3288_CLK_MIN_ONE 375000
27 #define RK3288_CLK_MIN_TWO 100000
28 #define RK_SDIO_ID0_SLOT_NUM 8
29 #define PM_AUTOSUSPEND_DELAY_COUNT 50
30
31 struct dw_mci_rockchip_priv_data {
32 struct clk *drv_clk;
33 struct clk *sample_clk;
34 int default_sample_phase;
35 int num_phases;
36 bool use_v2_tuning;
37 int last_degree;
38 u32 f_min;
39 };
40
dw_mci_rk3288_set_ios(struct dw_mci * host,struct mmc_ios * ios)41 static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
42 {
43 struct dw_mci_rockchip_priv_data *priv = host->priv;
44 int ret;
45 unsigned int cclkin;
46 u32 bus_hz;
47
48 if (ios->clock == 0) {
49 return;
50 }
51
52 /*
53 * cclkin: source clock of mmc controller
54 * bus_hz: card interface clock generated by CLKGEN
55 * bus_hz = cclkin / RK3288_CLKGEN_DIV
56 * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
57 *
58 * Note: div can only be 0 or 1, but div must be set to 1 for eMMC
59 * DDR52 8-bit mode.
60 */
61 if (ios->clock < priv->f_min) {
62 ios->clock = priv->f_min;
63 }
64
65 if (ios->bus_width == MMC_BUS_WIDTH_8 && ios->timing == MMC_TIMING_MMC_DDR52) {
66 cclkin = RK3288_CLKGEN_MUL * ios->clock * RK3288_CLKGEN_DIV;
67 } else {
68 cclkin = ios->clock * RK3288_CLKGEN_DIV;
69 }
70
71 ret = clk_set_rate(host->ciu_clk, cclkin);
72 if (ret) {
73 dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
74 }
75
76 bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
77 if (bus_hz != host->bus_hz) {
78 host->bus_hz = bus_hz;
79 /* force dw_mci_setup_bus() */
80 host->current_speed = 0;
81 }
82
83 /* Make sure we use phases which we can enumerate with */
84 if (!IS_ERR(priv->sample_clk) && ios->timing <= MMC_TIMING_SD_HS) {
85 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
86 }
87
88 /*
89 * Set the drive phase offset based on speed mode to achieve hold times.
90 *
91 * NOTE: this is _not_ a value that is dynamically tuned and is also
92 * _not_ a value that will vary from board to board. It is a value
93 * that could vary between different SoC models if they had massively
94 * different output clock delays inside their dw_mmc IP block (delay_o),
95 * but since it's OK to overshoot a little we don't need to do complex
96 * calculations and can pick values that will just work for everyone.
97 *
98 * When picking values we'll stick with picking 0/90/180/270 since
99 * those can be made very accurately on all known Rockchip SoCs.
100 *
101 * Note that these values match values from the DesignWare Databook
102 * tables for the most part except for SDR12 and "ID mode". For those
103 * two modes the databook calculations assume a clock in of 50MHz. As
104 * seen above, we always use a clock in rate that is exactly the
105 * card's input clock (times RK3288_CLKGEN_DIV, but that gets divided
106 * back out before the controller sees it).
107 *
108 * From measurement of a single device, it appears that delay_o is
109 * about .5 ns. Since we try to leave a bit of margin, it's expected
110 * that numbers here will be fine even with much larger delay_o
111 * (the 1.4 ns assumed by the DesignWare Databook would result in the
112 * same results, for instance).
113 */
114 if (!IS_ERR(priv->drv_clk)) {
115 int phase;
116
117 /*
118 * In almost all cases a 90 degree phase offset will provide
119 * sufficient hold times across all valid input clock rates
120 * assuming delay_o is not absurd for a given SoC. We'll use
121 * that as a default.
122 */
123 phase = RK_NINTY_DEGREE_PHASE;
124
125 switch (ios->timing) {
126 case MMC_TIMING_MMC_DDR52:
127 /*
128 * Since clock in rate with MMC_DDR52 is doubled when
129 * bus width is 8 we need to double the phase offset
130 * to get the same timings.
131 */
132 if (ios->bus_width == MMC_BUS_WIDTH_8) {
133 phase = RK_ONE_HUNDRED_EIGHTY_DEGREE_PHASE;
134 }
135 break;
136 case MMC_TIMING_UHS_SDR104:
137 case MMC_TIMING_MMC_HS200:
138 /*
139 * In the case of 150 MHz clock (typical max for
140 * Rockchip SoCs), 90 degree offset will add a delay
141 * of 1.67 ns. That will meet min hold time of .8 ns
142 * as long as clock output delay is < .87 ns. On
143 * SoCs measured this seems to be OK, but it doesn't
144 * hurt to give margin here, so we use 180.
145 */
146 phase = RK_ONE_HUNDRED_EIGHTY_DEGREE_PHASE;
147 break;
148 }
149
150 clk_set_phase(priv->drv_clk, phase);
151 }
152 }
153
154 #define TUNING_ITERATION_TO_PHASE(i, num_phases) (DIV_ROUND_UP((i)*RK_THREE_HUNDRED_SIXTY_DEGREE_PHASE, num_phases))
155
dw_mci_v2_execute_tuning(struct dw_mci_slot * slot,u32 opcode)156 static int dw_mci_v2_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
157 {
158 struct dw_mci *host = slot->host;
159 struct dw_mci_rockchip_priv_data *priv = host->priv;
160 struct mmc_host *mmc = slot->mmc;
161 u32 degrees[4] = {RK_NINTY_DEGREE_PHASE, RK_ONE_HUNDRED_EIGHTY_DEGREE_PHASE, RK_TWO_HUNDRED_SEVENTY_DEGREE_PHASE,
162 RK_THREE_HUNDRED_SIXTY_DEGREE_PHASE};
163 int i;
164 static bool inherit = true;
165
166 if (inherit) {
167 inherit = false;
168 i = clk_get_phase(priv->sample_clk) / RK_NINTY_DEGREE_PHASE - 1;
169 goto done;
170 }
171
172 /* v2 only support 4 degrees in theory */
173 for (i = 0; i < ARRAY_SIZE(degrees); i++) {
174 if (degrees[i] == priv->last_degree) {
175 continue;
176 }
177
178 clk_set_phase(priv->sample_clk, degrees[i]);
179 if (!mmc_send_tuning(mmc, opcode, NULL)) {
180 break;
181 }
182 }
183
184 if (i == ARRAY_SIZE(degrees)) {
185 dev_warn(host->dev, "All phases bad!");
186 return -EIO;
187 }
188
189 done:
190 dev_info(host->dev, "Successfully tuned phase to %d\n", degrees[i]);
191 priv->last_degree = degrees[i];
192 return 0;
193 }
194
dw_mci_rk3288_execute_tuning(struct dw_mci_slot * slot,u32 opcode)195 static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
196 {
197 struct dw_mci *host = slot->host;
198 struct dw_mci_rockchip_priv_data *priv = host->priv;
199 struct mmc_host *mmc = slot->mmc;
200 int ret = 0;
201 int i;
202 bool v, prev_v = 0, first_v;
203 struct range_t {
204 int start;
205 int end; /* inclusive */
206 };
207 struct range_t *ranges;
208 unsigned int range_count = 0;
209 int longest_range_len = -1;
210 int longest_range = -1;
211 int middle_phase;
212
213 if (IS_ERR(priv->sample_clk)) {
214 dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
215 return -EIO;
216 }
217
218 if (priv->use_v2_tuning) {
219 ret = dw_mci_v2_execute_tuning(slot, opcode);
220 if (!ret) {
221 return 0;
222 }
223 /* Otherwise we continue using fine tuning */
224 }
225
226 ranges = kmalloc_array(priv->num_phases / RK3288_PHASE_DIV + 1, sizeof(*ranges), GFP_KERNEL);
227 if (!ranges) {
228 return -ENOMEM;
229 }
230
231 /* Try each phase and extract good ranges */
232 for (i = 0; i < priv->num_phases;) {
233 clk_set_phase(priv->sample_clk, TUNING_ITERATION_TO_PHASE(i, priv->num_phases));
234
235 v = !mmc_send_tuning(mmc, opcode, NULL);
236
237 if (i == 0) {
238 first_v = v;
239 }
240
241 if ((!prev_v) && v) {
242 range_count++;
243 ranges[range_count - 1].start = i;
244 }
245 if (v) {
246 ranges[range_count - 1].end = i;
247 i++;
248 } else if (i == priv->num_phases - 1) {
249 /* No extra skipping rules if we're at the end */
250 i++;
251 } else {
252 /*
253 * No need to check too close to an invalid
254 * one since testing bad phases is slow. Skip
255 * 20 degrees.
256 */
257 i += DIV_ROUND_UP(RK3288_PHASE_MUL * priv->num_phases, RK_THREE_HUNDRED_SIXTY_DEGREE_PHASE);
258 /* Always test the last one */
259 if (i >= priv->num_phases) {
260 i = priv->num_phases - 1;
261 }
262 }
263
264 prev_v = v;
265 }
266
267 if (range_count == 0) {
268 dev_warn(host->dev, "All phases bad!");
269 ret = -EIO;
270 goto free;
271 }
272
273 /* wrap around case, merge the end points */
274 if ((range_count > 1) && first_v && v) {
275 ranges[0].start = ranges[range_count - 1].start;
276 range_count--;
277 }
278
279 if (ranges[0].start == 0 && ranges[0].end == priv->num_phases - 1) {
280 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
281 dev_info(host->dev, "All phases work, using default phase %d.", priv->default_sample_phase);
282 goto free;
283 }
284
285 /* Find the longest range */
286 for (i = 0; i < range_count; i++) {
287 int len = (ranges[i].end - ranges[i].start + 1);
288
289 if (len < 0) {
290 len += priv->num_phases;
291 }
292
293 if (longest_range_len < len) {
294 longest_range_len = len;
295 longest_range = i;
296 }
297
298 dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
299 TUNING_ITERATION_TO_PHASE(ranges[i].start, priv->num_phases),
300 TUNING_ITERATION_TO_PHASE(ranges[i].end, priv->num_phases), len);
301 }
302
303 dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
304 TUNING_ITERATION_TO_PHASE(ranges[longest_range].start, priv->num_phases),
305 TUNING_ITERATION_TO_PHASE(ranges[longest_range].end, priv->num_phases), longest_range_len);
306
307 middle_phase = ranges[longest_range].start + longest_range_len / RK3288_PHASE_DIV;
308 middle_phase %= priv->num_phases;
309 dev_info(host->dev, "Successfully tuned phase to %d\n", TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases));
310
311 clk_set_phase(priv->sample_clk, TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases));
312
313 free:
314 kfree(ranges);
315 return ret;
316 }
317
dw_mci_rk3288_parse_dt(struct dw_mci * host)318 static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
319 {
320 struct device_node *np = host->dev->of_node;
321 struct dw_mci_rockchip_priv_data *priv;
322
323 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
324 if (!priv) {
325 return -ENOMEM;
326 }
327
328 /*
329 * RK356X SoCs only support 375KHz for ID mode, so any clk request
330 * that less than 1.6MHz(2 * 400KHz * RK3288_CLKGEN_DIV) should be
331 * wrapped into 375KHz
332 */
333 if (of_device_is_compatible(host->dev->of_node, "rockchip,rk3568-dw-mshc")) {
334 priv->f_min = RK3288_CLK_MIN_ONE;
335 } else {
336 priv->f_min = RK3288_CLK_MIN_TWO;
337 }
338
339 if (of_property_read_u32(np, "rockchip,desired-num-phases", &priv->num_phases)) {
340 priv->num_phases = RK_THREE_HUNDRED_SIXTY_DEGREE_PHASE;
341 }
342
343 if (of_property_read_u32(np, "rockchip,default-sample-phase", &priv->default_sample_phase)) {
344 priv->default_sample_phase = 0;
345 }
346
347 if (of_property_read_bool(np, "rockchip,use-v2-tuning")) {
348 priv->use_v2_tuning = true;
349 }
350
351 priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
352 if (IS_ERR(priv->drv_clk)) {
353 dev_dbg(host->dev, "ciu-drive not available\n");
354 }
355
356 priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
357 if (IS_ERR(priv->sample_clk)) {
358 dev_dbg(host->dev, "ciu-sample not available\n");
359 }
360
361 host->priv = priv;
362
363 return 0;
364 }
365
dw_mci_rockchip_init(struct dw_mci * host)366 static int dw_mci_rockchip_init(struct dw_mci *host)
367 {
368 /* It is slot 8 on Rockchip SoCs */
369 host->sdio_id0 = RK_SDIO_ID0_SLOT_NUM;
370
371 if (of_device_is_compatible(host->dev->of_node, "rockchip,rk3288-dw-mshc")) {
372 host->bus_hz /= RK3288_CLKGEN_DIV;
373 }
374
375 host->need_xfer_timer = true;
376 return 0;
377 }
378
379 /* Common capabilities of RK3288 SoC */
380 static unsigned long dw_mci_rk3288_dwmmc_caps[4] = {
381 MMC_CAP_CMD23,
382 MMC_CAP_CMD23,
383 MMC_CAP_CMD23,
384 MMC_CAP_CMD23,
385 };
386
387 static const struct dw_mci_drv_data rk2928_drv_data = {
388 .init = dw_mci_rockchip_init,
389 };
390
391 static const struct dw_mci_drv_data rk3288_drv_data = {
392 .caps = dw_mci_rk3288_dwmmc_caps,
393 .num_caps = ARRAY_SIZE(dw_mci_rk3288_dwmmc_caps),
394 .set_ios = dw_mci_rk3288_set_ios,
395 .execute_tuning = dw_mci_rk3288_execute_tuning,
396 .parse_dt = dw_mci_rk3288_parse_dt,
397 .init = dw_mci_rockchip_init,
398 };
399
400 static const struct of_device_id dw_mci_rockchip_match[] = {
401 {.compatible = "rockchip,rk2928-dw-mshc", .data = &rk2928_drv_data},
402 {.compatible = "rockchip,rk3288-dw-mshc", .data = &rk3288_drv_data},
403 {},
404 };
405 MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
406
dw_mci_rockchip_probe(struct platform_device * pdev)407 static int dw_mci_rockchip_probe(struct platform_device *pdev)
408 {
409 const struct dw_mci_drv_data *drv_data;
410 const struct of_device_id *match;
411 int ret;
412 bool use_rpm = true;
413
414 if (!pdev->dev.of_node) {
415 return -ENODEV;
416 }
417
418 if (!device_property_read_bool(&pdev->dev, "non-removable") && !device_property_read_bool(&pdev->dev, "cd-gpios")) {
419 use_rpm = false;
420 }
421
422 match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
423 drv_data = match->data;
424
425 /*
426 * increase rpm usage count in order to make
427 * pm_runtime_force_resume calls rpm resume callback
428 */
429 pm_runtime_get_noresume(&pdev->dev);
430
431 if (use_rpm) {
432 pm_runtime_set_active(&pdev->dev);
433 pm_runtime_enable(&pdev->dev);
434 pm_runtime_set_autosuspend_delay(&pdev->dev, PM_AUTOSUSPEND_DELAY_COUNT);
435 pm_runtime_use_autosuspend(&pdev->dev);
436 }
437
438 ret = dw_mci_pltfm_register(pdev, drv_data);
439 if (ret) {
440 if (use_rpm) {
441 pm_runtime_disable(&pdev->dev);
442 pm_runtime_set_suspended(&pdev->dev);
443 }
444 pm_runtime_put_noidle(&pdev->dev);
445 return ret;
446 }
447
448 if (use_rpm) {
449 pm_runtime_put_autosuspend(&pdev->dev);
450 }
451
452 return 0;
453 }
454
dw_mci_rockchip_remove(struct platform_device * pdev)455 static int dw_mci_rockchip_remove(struct platform_device *pdev)
456 {
457 pm_runtime_get_sync(&pdev->dev);
458 pm_runtime_disable(&pdev->dev);
459 pm_runtime_put_noidle(&pdev->dev);
460
461 return dw_mci_pltfm_remove(pdev);
462 }
463
464 static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops = {
465 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
466 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend, dw_mci_runtime_resume, NULL)};
467
468 static struct platform_driver dw_mci_rockchip_pltfm_driver = {
469 .probe = dw_mci_rockchip_probe,
470 .remove = dw_mci_rockchip_remove,
471 .driver =
472 {
473 .name = "dwmmc_rockchip",
474 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
475 .of_match_table = dw_mci_rockchip_match,
476 .pm = &dw_mci_rockchip_dev_pm_ops,
477 },
478 };
479
480 module_platform_driver(dw_mci_rockchip_pltfm_driver);
481
482 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
483 MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
484 MODULE_ALIAS("platform:dwmmc_rockchip");
485 MODULE_LICENSE("GPL v2");
486