1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015-19 Renesas Electronics Corporation
6 * Copyright (C) 2016-19 Sang Engineering, Wolfram Sang
7 * Copyright (C) 2016-17 Horms Solutions, Simon Horman
8 * Copyright (C) 2009 Magnus Damm
10 * Based on "Compaq ASIC3 support":
12 * Copyright 2001 Compaq Computer Corporation.
13 * Copyright 2004-2005 Phil Blundell
14 * Copyright 2007-2008 OpenedHand Ltd.
16 * Authors: Phil Blundell <pb@handhelds.org>,
17 * Samuel Ortiz <sameo@openedhand.com>
21 #include <linux/kernel.h>
22 #include <linux/clk.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/slot-gpio.h>
29 #include <linux/mfd/tmio.h>
30 #include <linux/sh_dma.h>
31 #include <linux/delay.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/pinctrl/pinctrl-state.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/sys_soc.h>
37 #include "renesas_sdhi.h"
40 #define HOST_MODE 0xe4
42 #define SDHI_VER_GEN2_SDR50 0x490c
43 #define SDHI_VER_RZ_A1 0x820b
44 /* very old datasheets said 0x490c for SDR104, too. They are wrong! */
45 #define SDHI_VER_GEN2_SDR104 0xcb0d
46 #define SDHI_VER_GEN3_SD 0xcc10
47 #define SDHI_VER_GEN3_SDMMC 0xcd10
49 static void renesas_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
55 * renesas_sdhi_of_data :: dma_buswidth
57 switch (sd_ctrl_read16(host, CTL_VERSION)) {
58 case SDHI_VER_GEN2_SDR50:
59 val = (width == 32) ? 0x0001 : 0x0000;
61 case SDHI_VER_GEN2_SDR104:
62 val = (width == 32) ? 0x0000 : 0x0001;
64 case SDHI_VER_GEN3_SD:
65 case SDHI_VER_GEN3_SDMMC:
78 sd_ctrl_write16(host, HOST_MODE, val);
81 static int renesas_sdhi_clk_enable(struct tmio_mmc_host *host)
83 struct mmc_host *mmc = host->mmc;
84 struct renesas_sdhi *priv = host_to_priv(host);
85 int ret = clk_prepare_enable(priv->clk);
90 ret = clk_prepare_enable(priv->clk_cd);
92 clk_disable_unprepare(priv->clk);
97 * The clock driver may not know what maximum frequency
98 * actually works, so it should be set with the max-frequency
99 * property which will already have been read to f_max. If it
100 * was missing, assume the current frequency is the maximum.
103 mmc->f_max = clk_get_rate(priv->clk);
106 * Minimum frequency is the minimum input clock frequency
107 * divided by our maximum divider.
109 mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
111 /* enable 16bit data access on SDBUF as default */
112 renesas_sdhi_sdbuf_width(host, 16);
117 static unsigned int renesas_sdhi_clk_update(struct tmio_mmc_host *host,
118 unsigned int new_clock)
120 struct renesas_sdhi *priv = host_to_priv(host);
121 unsigned int freq, diff, best_freq = 0, diff_min = ~0;
124 /* tested only on R-Car Gen2+ currently; may work for others */
125 if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
126 return clk_get_rate(priv->clk);
129 * We want the bus clock to be as close as possible to, but no
130 * greater than, new_clock. As we can divide by 1 << i for
131 * any i in [0, 9] we want the input clock to be as close as
132 * possible, but no greater than, new_clock << i.
134 for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
135 freq = clk_round_rate(priv->clk, new_clock << i);
136 if (freq > (new_clock << i)) {
137 /* Too fast; look for a slightly slower option */
138 freq = clk_round_rate(priv->clk,
139 (new_clock << i) / 4 * 3);
140 if (freq > (new_clock << i))
144 diff = new_clock - (freq >> i);
145 if (diff <= diff_min) {
151 clk_set_rate(priv->clk, best_freq);
153 return clk_get_rate(priv->clk);
156 static void renesas_sdhi_set_clock(struct tmio_mmc_host *host,
157 unsigned int new_clock)
161 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
162 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
164 if (new_clock == 0) {
165 host->mmc->actual_clock = 0;
169 host->mmc->actual_clock = renesas_sdhi_clk_update(host, new_clock);
170 clock = host->mmc->actual_clock / 512;
172 for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1)
175 /* 1/1 clock is option */
176 if ((host->pdata->flags & TMIO_MMC_CLK_ACTUAL) && ((clk >> 22) & 0x1)) {
177 if (!(host->mmc->ios.timing == MMC_TIMING_MMC_HS400))
183 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
184 if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
185 usleep_range(10000, 11000);
187 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
188 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
191 /* HW engineers overrode docs: no sleep needed on R-Car2+ */
192 if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
193 usleep_range(10000, 11000);
196 static void renesas_sdhi_clk_disable(struct tmio_mmc_host *host)
198 struct renesas_sdhi *priv = host_to_priv(host);
200 clk_disable_unprepare(priv->clk);
201 clk_disable_unprepare(priv->clk_cd);
204 static int renesas_sdhi_card_busy(struct mmc_host *mmc)
206 struct tmio_mmc_host *host = mmc_priv(mmc);
208 return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
212 static int renesas_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
215 struct tmio_mmc_host *host = mmc_priv(mmc);
216 struct renesas_sdhi *priv = host_to_priv(host);
217 struct pinctrl_state *pin_state;
220 switch (ios->signal_voltage) {
221 case MMC_SIGNAL_VOLTAGE_330:
222 pin_state = priv->pins_default;
224 case MMC_SIGNAL_VOLTAGE_180:
225 pin_state = priv->pins_uhs;
232 * If anything is missing, assume signal voltage is fixed at
233 * 3.3V and succeed/fail accordingly.
235 if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
236 return ios->signal_voltage ==
237 MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
239 ret = mmc_regulator_set_vqmmc(host->mmc, ios);
243 return pinctrl_select_state(priv->pinctrl, pin_state);
247 #define SH_MOBILE_SDHI_SCC_DTCNTL 0x000
248 #define SH_MOBILE_SDHI_SCC_TAPSET 0x002
249 #define SH_MOBILE_SDHI_SCC_DT2FF 0x004
250 #define SH_MOBILE_SDHI_SCC_CKSEL 0x006
251 #define SH_MOBILE_SDHI_SCC_RVSCNTL 0x008
252 #define SH_MOBILE_SDHI_SCC_RVSREQ 0x00A
253 #define SH_MOBILE_SDHI_SCC_SMPCMP 0x00C
254 #define SH_MOBILE_SDHI_SCC_TMPPORT2 0x00E
256 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN BIT(0)
257 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
258 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
260 #define SH_MOBILE_SDHI_SCC_CKSEL_DTSEL BIT(0)
262 #define SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
264 #define SH_MOBILE_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0)
265 #define SH_MOBILE_SDHI_SCC_RVSREQ_REQTAPUP BIT(1)
266 #define SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR BIT(2)
268 #define SH_MOBILE_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8)
269 #define SH_MOBILE_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24)
270 #define SH_MOBILE_SDHI_SCC_SMPCMP_CMD_ERR (BIT(8) | BIT(24))
272 #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
273 #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
275 static inline u32 sd_scc_read32(struct tmio_mmc_host *host,
276 struct renesas_sdhi *priv, int addr)
278 return readl(priv->scc_ctl + (addr << host->bus_shift));
281 static inline void sd_scc_write32(struct tmio_mmc_host *host,
282 struct renesas_sdhi *priv,
285 writel(val, priv->scc_ctl + (addr << host->bus_shift));
288 static unsigned int renesas_sdhi_init_tuning(struct tmio_mmc_host *host)
290 struct renesas_sdhi *priv;
292 priv = host_to_priv(host);
295 sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, 0x0);
297 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
298 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
300 /* set sampling clock selection range */
301 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
302 SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
303 0x8 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
305 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
306 SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
307 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
309 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
310 ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
311 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
313 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF, priv->scc_tappos);
315 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
316 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
319 return (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL) >>
320 SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
321 SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK;
324 static void renesas_sdhi_hs400_complete(struct mmc_host *mmc)
326 struct tmio_mmc_host *host = mmc_priv(mmc);
327 struct renesas_sdhi *priv = host_to_priv(host);
329 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
330 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
333 sd_ctrl_write16(host, CTL_SDIF_MODE, 0x0001 |
334 sd_ctrl_read16(host, CTL_SDIF_MODE));
336 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF,
337 priv->scc_tappos_hs400);
339 /* Gen3 can't do automatic tap correction with HS400, so disable it */
340 if (sd_ctrl_read16(host, CTL_VERSION) == SDHI_VER_GEN3_SDMMC)
341 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
342 ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
343 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
345 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
346 (SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
347 SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) |
348 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
350 /* Set the sampling clock selection range of HS400 mode */
351 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
352 SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
353 0x4 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
356 if (priv->quirks && priv->quirks->hs400_4taps)
357 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET,
360 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
361 SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
362 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
364 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
365 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
368 static void renesas_sdhi_reset_scc(struct tmio_mmc_host *host,
369 struct renesas_sdhi *priv)
371 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
372 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
374 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
375 ~SH_MOBILE_SDHI_SCC_CKSEL_DTSEL &
376 sd_scc_read32(host, priv,
377 SH_MOBILE_SDHI_SCC_CKSEL));
380 static void renesas_sdhi_disable_scc(struct mmc_host *mmc)
382 struct tmio_mmc_host *host = mmc_priv(mmc);
383 struct renesas_sdhi *priv = host_to_priv(host);
385 renesas_sdhi_reset_scc(host, priv);
387 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
388 ~SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN &
389 sd_scc_read32(host, priv,
390 SH_MOBILE_SDHI_SCC_DTCNTL));
392 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
393 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
396 static void renesas_sdhi_reset_hs400_mode(struct tmio_mmc_host *host,
397 struct renesas_sdhi *priv)
399 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
400 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
402 /* Reset HS400 mode */
403 sd_ctrl_write16(host, CTL_SDIF_MODE, ~0x0001 &
404 sd_ctrl_read16(host, CTL_SDIF_MODE));
406 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF, priv->scc_tappos);
408 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
409 ~(SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
410 SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) &
411 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
413 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
414 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
417 static int renesas_sdhi_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
419 struct tmio_mmc_host *host = mmc_priv(mmc);
421 renesas_sdhi_reset_hs400_mode(host, host_to_priv(host));
425 #define SH_MOBILE_SDHI_MAX_TAP 3
427 static int renesas_sdhi_select_tuning(struct tmio_mmc_host *host)
429 struct renesas_sdhi *priv = host_to_priv(host);
430 unsigned int tap_start = 0, tap_end = 0, tap_cnt = 0, rs, re, i;
431 unsigned int taps_size = priv->tap_num * 2;
433 priv->doing_tune = false;
434 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
437 * When tuning CMD19 is issued twice for each tap, merge the
438 * result requiring the tap to be good in both runs before
439 * considering it for tuning selection.
441 for (i = 0; i < taps_size; i++) {
442 int offset = priv->tap_num * (i < priv->tap_num ? 1 : -1);
444 if (!test_bit(i, priv->taps))
445 clear_bit(i + offset, priv->taps);
449 * Find the longest consecutive run of successful probes. If that
450 * is more than SH_MOBILE_SDHI_MAX_TAP probes long then use the
451 * center index as the tap.
453 bitmap_for_each_set_region(priv->taps, rs, re, 0, taps_size) {
454 if (re - rs > tap_cnt) {
457 tap_cnt = tap_end - tap_start;
461 if (tap_cnt >= SH_MOBILE_SDHI_MAX_TAP)
462 priv->tap_set = (tap_start + tap_end) / 2 % priv->tap_num;
467 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, priv->tap_set);
469 /* Enable auto re-tuning */
470 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
471 SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN |
472 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
477 static int renesas_sdhi_execute_tuning(struct tmio_mmc_host *host, u32 opcode)
479 struct renesas_sdhi *priv = host_to_priv(host);
482 priv->tap_num = renesas_sdhi_init_tuning(host);
484 return 0; /* Tuning is not supported */
486 if (priv->tap_num * 2 >= sizeof(priv->taps) * BITS_PER_BYTE) {
487 dev_err(&host->pdev->dev,
488 "Too many taps, please update 'taps' in tmio_mmc_host!\n");
492 priv->doing_tune = true;
493 bitmap_zero(priv->taps, priv->tap_num * 2);
495 /* Issue CMD19 twice for each tap */
496 for (i = 0; i < 2 * priv->tap_num; i++) {
497 /* Set sampling clock position */
498 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, i % priv->tap_num);
500 if (mmc_send_tuning(host->mmc, opcode, NULL) == 0)
501 set_bit(i, priv->taps);
504 return renesas_sdhi_select_tuning(host);
507 static bool renesas_sdhi_manual_correction(struct tmio_mmc_host *host, bool use_4tap)
509 struct renesas_sdhi *priv = host_to_priv(host);
510 unsigned long new_tap = priv->tap_set;
513 val = sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ);
517 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
519 /* Change TAP position according to correction status */
520 if (sd_ctrl_read16(host, CTL_VERSION) == SDHI_VER_GEN3_SDMMC &&
521 host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
523 * With HS400, the DAT signal is based on DS, not CLK.
524 * Therefore, use only CMD status.
526 u32 smpcmp = sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_SMPCMP) &
527 SH_MOBILE_SDHI_SCC_SMPCMP_CMD_ERR;
529 return false; /* no error in CMD signal */
530 else if (smpcmp == SH_MOBILE_SDHI_SCC_SMPCMP_CMD_REQUP)
532 else if (smpcmp == SH_MOBILE_SDHI_SCC_SMPCMP_CMD_REQDOWN)
535 return true; /* need retune */
537 if (val & SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR)
538 return true; /* need retune */
539 else if (val & SH_MOBILE_SDHI_SCC_RVSREQ_REQTAPUP)
541 else if (val & SH_MOBILE_SDHI_SCC_RVSREQ_REQTAPDOWN)
547 priv->tap_set = (new_tap % priv->tap_num);
548 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET,
549 priv->tap_set / (use_4tap ? 2 : 1));
554 static bool renesas_sdhi_auto_correction(struct tmio_mmc_host *host)
556 struct renesas_sdhi *priv = host_to_priv(host);
558 /* Check SCC error */
559 if (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ) &
560 SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR) {
561 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
568 static bool renesas_sdhi_check_scc_error(struct tmio_mmc_host *host)
570 struct renesas_sdhi *priv = host_to_priv(host);
571 bool use_4tap = priv->quirks && priv->quirks->hs400_4taps;
574 * Skip checking SCC errors when running on 4 taps in HS400 mode as
575 * any retuning would still result in the same 4 taps being used.
577 if (!(host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) &&
578 !(host->mmc->ios.timing == MMC_TIMING_MMC_HS200) &&
579 !(host->mmc->ios.timing == MMC_TIMING_MMC_HS400 && !use_4tap))
582 if (mmc_doing_retune(host->mmc) || priv->doing_tune)
585 if (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL) &
586 SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN)
587 return renesas_sdhi_auto_correction(host);
589 return renesas_sdhi_manual_correction(host, use_4tap);
592 static void renesas_sdhi_hw_reset(struct tmio_mmc_host *host)
594 struct renesas_sdhi *priv;
596 priv = host_to_priv(host);
598 renesas_sdhi_reset_scc(host, priv);
599 renesas_sdhi_reset_hs400_mode(host, priv);
601 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
602 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
604 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
605 ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
606 sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
608 if (host->pdata->flags & TMIO_MMC_MIN_RCAR2)
609 sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK,
610 TMIO_MASK_INIT_RCAR2);
613 static int renesas_sdhi_wait_idle(struct tmio_mmc_host *host, u32 bit)
616 /* CBSY is set when busy, SCLKDIVEN is cleared when busy */
617 u32 wait_state = (bit == TMIO_STAT_CMD_BUSY ? TMIO_STAT_CMD_BUSY : 0);
619 while (--timeout && (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS)
620 & bit) == wait_state)
624 dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
631 static int renesas_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
633 u32 bit = TMIO_STAT_SCLKDIVEN;
637 case CTL_STOP_INTERNAL_ACTION:
638 case CTL_XFER_BLK_COUNT:
639 case CTL_SD_XFER_LEN:
640 case CTL_SD_MEM_CARD_OPT:
641 case CTL_TRANSACTION_CTL:
644 if (host->pdata->flags & TMIO_MMC_HAVE_CBSY)
645 bit = TMIO_STAT_CMD_BUSY;
647 case CTL_SD_CARD_CLK_CTL:
648 return renesas_sdhi_wait_idle(host, bit);
654 static int renesas_sdhi_multi_io_quirk(struct mmc_card *card,
655 unsigned int direction, int blk_size)
658 * In Renesas controllers, when performing a
659 * multiple block read of one or two blocks,
660 * depending on the timing with which the
661 * response register is read, the response
662 * value may not be read properly.
663 * Use single block read for this HW bug
665 if ((direction == MMC_DATA_READ) &&
672 static void renesas_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
674 /* Iff regs are 8 byte apart, sdbuf is 64 bit. Otherwise always 32. */
675 int width = (host->bus_shift == 2) ? 64 : 32;
677 sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? DMA_ENABLE_DMASDRW : 0);
678 renesas_sdhi_sdbuf_width(host, enable ? width : 16);
681 static const struct renesas_sdhi_quirks sdhi_quirks_4tap_nohs400 = {
682 .hs400_disabled = true,
686 static const struct renesas_sdhi_quirks sdhi_quirks_4tap = {
690 static const struct renesas_sdhi_quirks sdhi_quirks_nohs400 = {
691 .hs400_disabled = true,
694 static const struct soc_device_attribute sdhi_quirks_match[] = {
695 { .soc_id = "r8a774a1", .revision = "ES1.[012]", .data = &sdhi_quirks_4tap_nohs400 },
696 { .soc_id = "r8a7795", .revision = "ES1.*", .data = &sdhi_quirks_4tap_nohs400 },
697 { .soc_id = "r8a7795", .revision = "ES2.0", .data = &sdhi_quirks_4tap },
698 { .soc_id = "r8a7796", .revision = "ES1.[012]", .data = &sdhi_quirks_4tap_nohs400 },
699 { .soc_id = "r8a77980", .data = &sdhi_quirks_nohs400 },
703 int renesas_sdhi_probe(struct platform_device *pdev,
704 const struct tmio_mmc_dma_ops *dma_ops)
706 struct tmio_mmc_data *mmd = pdev->dev.platform_data;
707 const struct renesas_sdhi_quirks *quirks = NULL;
708 const struct renesas_sdhi_of_data *of_data;
709 const struct soc_device_attribute *attr;
710 struct tmio_mmc_data *mmc_data;
711 struct tmio_mmc_dma *dma_priv;
712 struct tmio_mmc_host *host;
713 struct renesas_sdhi *priv;
714 int num_irqs, irq, ret, i;
715 struct resource *res;
718 of_data = of_device_get_match_data(&pdev->dev);
720 attr = soc_device_match(sdhi_quirks_match);
724 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
728 priv = devm_kzalloc(&pdev->dev, sizeof(struct renesas_sdhi),
733 priv->quirks = quirks;
734 mmc_data = &priv->mmc_data;
735 dma_priv = &priv->dma_priv;
737 priv->clk = devm_clk_get(&pdev->dev, NULL);
738 if (IS_ERR(priv->clk)) {
739 ret = PTR_ERR(priv->clk);
740 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
745 * Some controllers provide a 2nd clock just to run the internal card
746 * detection logic. Unfortunately, the existing driver architecture does
747 * not support a separation of clocks for runtime PM usage. When
748 * native hotplug is used, the tmio driver assumes that the core
749 * must continue to run for card detect to stay active, so we cannot
751 * Additionally, it is prohibited to supply a clock to the core but not
752 * to the card detect circuit. That leaves us with if separate clocks
753 * are presented, we must treat them both as virtually 1 clock.
755 priv->clk_cd = devm_clk_get(&pdev->dev, "cd");
756 if (IS_ERR(priv->clk_cd))
759 priv->pinctrl = devm_pinctrl_get(&pdev->dev);
760 if (!IS_ERR(priv->pinctrl)) {
761 priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
762 PINCTRL_STATE_DEFAULT);
763 priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
767 host = tmio_mmc_host_alloc(pdev, mmc_data);
769 return PTR_ERR(host);
772 mmc_data->flags |= of_data->tmio_flags;
773 mmc_data->ocr_mask = of_data->tmio_ocr_mask;
774 mmc_data->capabilities |= of_data->capabilities;
775 mmc_data->capabilities2 |= of_data->capabilities2;
776 mmc_data->dma_rx_offset = of_data->dma_rx_offset;
777 mmc_data->max_blk_count = of_data->max_blk_count;
778 mmc_data->max_segs = of_data->max_segs;
779 dma_priv->dma_buswidth = of_data->dma_buswidth;
780 host->bus_shift = of_data->bus_shift;
783 host->write16_hook = renesas_sdhi_write16_hook;
784 host->clk_enable = renesas_sdhi_clk_enable;
785 host->clk_disable = renesas_sdhi_clk_disable;
786 host->set_clock = renesas_sdhi_set_clock;
787 host->multi_io_quirk = renesas_sdhi_multi_io_quirk;
788 host->dma_ops = dma_ops;
790 if (quirks && quirks->hs400_disabled)
791 host->mmc->caps2 &= ~(MMC_CAP2_HS400 | MMC_CAP2_HS400_ES);
793 /* For some SoC, we disable internal WP. GPIO may override this */
794 if (mmc_can_gpio_ro(host->mmc))
795 mmc_data->capabilities2 &= ~MMC_CAP2_NO_WRITE_PROTECT;
797 /* SDR speeds are only available on Gen2+ */
798 if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
799 /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
800 host->ops.card_busy = renesas_sdhi_card_busy;
801 host->ops.start_signal_voltage_switch =
802 renesas_sdhi_start_signal_voltage_switch;
803 host->sdcard_irq_setbit_mask = TMIO_STAT_ALWAYS_SET_27;
805 /* SDR and HS200/400 registers requires HW reset */
806 if (of_data && of_data->scc_offset) {
807 priv->scc_ctl = host->ctl + of_data->scc_offset;
808 host->mmc->caps |= MMC_CAP_HW_RESET;
809 host->hw_reset = renesas_sdhi_hw_reset;
813 /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
814 if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
820 dma_priv->filter = shdma_chan_filter;
821 dma_priv->enable = renesas_sdhi_enable_dma;
823 mmc_data->alignment_shift = 1; /* 2-byte alignment */
824 mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
827 * All SDHI blocks support 2-byte and larger block sizes in 4-bit
830 mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
833 * All SDHI blocks support SDIO IRQ signalling.
835 mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
837 /* All SDHI have CMD12 control bit */
838 mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
840 /* All SDHI have SDIO status bits which must be 1 */
841 mmc_data->flags |= TMIO_MMC_SDIO_STATUS_SETBITS;
843 ret = renesas_sdhi_clk_enable(host);
847 ver = sd_ctrl_read16(host, CTL_VERSION);
848 /* GEN2_SDR104 is first known SDHI to use 32bit block count */
849 if (ver < SDHI_VER_GEN2_SDR104 && mmc_data->max_blk_count > U16_MAX)
850 mmc_data->max_blk_count = U16_MAX;
852 /* One Gen2 SDHI incarnation does NOT have a CBSY bit */
853 if (ver == SDHI_VER_GEN2_SDR50)
854 mmc_data->flags &= ~TMIO_MMC_HAVE_CBSY;
856 ret = tmio_mmc_host_probe(host);
860 /* Enable tuning iff we have an SCC and a supported mode */
861 if (of_data && of_data->scc_offset &&
862 (host->mmc->caps & MMC_CAP_UHS_SDR104 ||
863 host->mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR |
864 MMC_CAP2_HS400_1_8V))) {
865 const struct renesas_sdhi_scc *taps = of_data->taps;
866 bool use_4tap = priv->quirks && priv->quirks->hs400_4taps;
869 for (i = 0; i < of_data->taps_num; i++) {
870 if (taps[i].clk_rate == 0 ||
871 taps[i].clk_rate == host->mmc->f_max) {
872 priv->scc_tappos = taps->tap;
873 priv->scc_tappos_hs400 = use_4tap ?
874 taps->tap_hs400_4tap :
882 dev_warn(&host->pdev->dev, "Unknown clock rate for tuning\n");
884 host->execute_tuning = renesas_sdhi_execute_tuning;
885 host->check_retune = renesas_sdhi_check_scc_error;
886 host->ops.prepare_hs400_tuning = renesas_sdhi_prepare_hs400_tuning;
887 host->ops.hs400_downgrade = renesas_sdhi_disable_scc;
888 host->ops.hs400_complete = renesas_sdhi_hs400_complete;
891 num_irqs = platform_irq_count(pdev);
897 /* There must be at least one IRQ source */
903 for (i = 0; i < num_irqs; i++) {
904 irq = platform_get_irq(pdev, i);
910 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
911 dev_name(&pdev->dev), host);
916 dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
917 mmc_hostname(host->mmc), (unsigned long)
918 (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
919 host->mmc->f_max / 1000000);
924 tmio_mmc_host_remove(host);
926 renesas_sdhi_clk_disable(host);
928 tmio_mmc_host_free(host);
932 EXPORT_SYMBOL_GPL(renesas_sdhi_probe);
934 int renesas_sdhi_remove(struct platform_device *pdev)
936 struct tmio_mmc_host *host = platform_get_drvdata(pdev);
938 tmio_mmc_host_remove(host);
939 renesas_sdhi_clk_disable(host);
943 EXPORT_SYMBOL_GPL(renesas_sdhi_remove);
945 MODULE_LICENSE("GPL v2");