mmc: renesas_sdhi: merge clk_{start,stop} functions to set_clock
[platform/kernel/linux-rpi.git] / drivers / mmc / host / renesas_sdhi_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas SDHI
4  *
5  * Copyright (C) 2015-17 Renesas Electronics Corporation
6  * Copyright (C) 2016-17 Sang Engineering, Wolfram Sang
7  * Copyright (C) 2016-17 Horms Solutions, Simon Horman
8  * Copyright (C) 2009 Magnus Damm
9  *
10  * Based on "Compaq ASIC3 support":
11  *
12  * Copyright 2001 Compaq Computer Corporation.
13  * Copyright 2004-2005 Phil Blundell
14  * Copyright 2007-2008 OpenedHand Ltd.
15  *
16  * Authors: Phil Blundell <pb@handhelds.org>,
17  *          Samuel Ortiz <sameo@openedhand.com>
18  *
19  */
20
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
36 #include "renesas_sdhi.h"
37 #include "tmio_mmc.h"
38
39 #define HOST_MODE               0xe4
40
41 #define SDHI_VER_GEN2_SDR50     0x490c
42 #define SDHI_VER_RZ_A1          0x820b
43 /* very old datasheets said 0x490c for SDR104, too. They are wrong! */
44 #define SDHI_VER_GEN2_SDR104    0xcb0d
45 #define SDHI_VER_GEN3_SD        0xcc10
46 #define SDHI_VER_GEN3_SDMMC     0xcd10
47
48 static void renesas_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
49 {
50         u32 val;
51
52         /*
53          * see also
54          *      renesas_sdhi_of_data :: dma_buswidth
55          */
56         switch (sd_ctrl_read16(host, CTL_VERSION)) {
57         case SDHI_VER_GEN2_SDR50:
58                 val = (width == 32) ? 0x0001 : 0x0000;
59                 break;
60         case SDHI_VER_GEN2_SDR104:
61                 val = (width == 32) ? 0x0000 : 0x0001;
62                 break;
63         case SDHI_VER_GEN3_SD:
64         case SDHI_VER_GEN3_SDMMC:
65                 if (width == 64)
66                         val = 0x0000;
67                 else if (width == 32)
68                         val = 0x0101;
69                 else
70                         val = 0x0001;
71                 break;
72         default:
73                 /* nothing to do */
74                 return;
75         }
76
77         sd_ctrl_write16(host, HOST_MODE, val);
78 }
79
80 static int renesas_sdhi_clk_enable(struct tmio_mmc_host *host)
81 {
82         struct mmc_host *mmc = host->mmc;
83         struct renesas_sdhi *priv = host_to_priv(host);
84         int ret = clk_prepare_enable(priv->clk);
85
86         if (ret < 0)
87                 return ret;
88
89         ret = clk_prepare_enable(priv->clk_cd);
90         if (ret < 0) {
91                 clk_disable_unprepare(priv->clk);
92                 return ret;
93         }
94
95         /*
96          * The clock driver may not know what maximum frequency
97          * actually works, so it should be set with the max-frequency
98          * property which will already have been read to f_max.  If it
99          * was missing, assume the current frequency is the maximum.
100          */
101         if (!mmc->f_max)
102                 mmc->f_max = clk_get_rate(priv->clk);
103
104         /*
105          * Minimum frequency is the minimum input clock frequency
106          * divided by our maximum divider.
107          */
108         mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
109
110         /* enable 16bit data access on SDBUF as default */
111         renesas_sdhi_sdbuf_width(host, 16);
112
113         return 0;
114 }
115
116 static unsigned int renesas_sdhi_clk_update(struct tmio_mmc_host *host,
117                                             unsigned int new_clock)
118 {
119         struct renesas_sdhi *priv = host_to_priv(host);
120         unsigned int freq, diff, best_freq = 0, diff_min = ~0;
121         int i, ret;
122
123         /* tested only on R-Car Gen2+ currently; may work for others */
124         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
125                 return clk_get_rate(priv->clk);
126
127         /*
128          * We want the bus clock to be as close as possible to, but no
129          * greater than, new_clock.  As we can divide by 1 << i for
130          * any i in [0, 9] we want the input clock to be as close as
131          * possible, but no greater than, new_clock << i.
132          */
133         for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
134                 freq = clk_round_rate(priv->clk, new_clock << i);
135                 if (freq > (new_clock << i)) {
136                         /* Too fast; look for a slightly slower option */
137                         freq = clk_round_rate(priv->clk,
138                                               (new_clock << i) / 4 * 3);
139                         if (freq > (new_clock << i))
140                                 continue;
141                 }
142
143                 diff = new_clock - (freq >> i);
144                 if (diff <= diff_min) {
145                         best_freq = freq;
146                         diff_min = diff;
147                 }
148         }
149
150         ret = clk_set_rate(priv->clk, best_freq);
151
152         return ret == 0 ? best_freq : clk_get_rate(priv->clk);
153 }
154
155 static void renesas_sdhi_set_clock(struct tmio_mmc_host *host,
156                                    unsigned int new_clock)
157 {
158         u32 clk = 0, clock;
159
160         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
161                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
162
163         if (new_clock == 0)
164                 goto out;
165
166         /*
167          * Both HS400 and HS200/SD104 set 200MHz, but some devices need to
168          * set 400MHz to distinguish the CPG settings in HS400.
169          */
170         if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
171             host->pdata->flags & TMIO_MMC_HAVE_4TAP_HS400 &&
172             new_clock == 200000000)
173                 new_clock = 400000000;
174
175         clock = renesas_sdhi_clk_update(host, new_clock) / 512;
176
177         for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1)
178                 clock <<= 1;
179
180         /* 1/1 clock is option */
181         if ((host->pdata->flags & TMIO_MMC_CLK_ACTUAL) && ((clk >> 22) & 0x1)) {
182                 if (!(host->mmc->ios.timing == MMC_TIMING_MMC_HS400))
183                         clk |= 0xff;
184                 else
185                         clk &= ~0xff;
186         }
187
188         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
189         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
190                 usleep_range(10000, 11000);
191
192         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
193                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
194
195 out:
196         /* HW engineers overrode docs: no sleep needed on R-Car2+ */
197         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
198                 usleep_range(10000, 11000);
199 }
200
201 static void renesas_sdhi_clk_disable(struct tmio_mmc_host *host)
202 {
203         struct renesas_sdhi *priv = host_to_priv(host);
204
205         clk_disable_unprepare(priv->clk);
206         clk_disable_unprepare(priv->clk_cd);
207 }
208
209 static int renesas_sdhi_card_busy(struct mmc_host *mmc)
210 {
211         struct tmio_mmc_host *host = mmc_priv(mmc);
212
213         return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
214                  TMIO_STAT_DAT0);
215 }
216
217 static int renesas_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
218                                                     struct mmc_ios *ios)
219 {
220         struct tmio_mmc_host *host = mmc_priv(mmc);
221         struct renesas_sdhi *priv = host_to_priv(host);
222         struct pinctrl_state *pin_state;
223         int ret;
224
225         switch (ios->signal_voltage) {
226         case MMC_SIGNAL_VOLTAGE_330:
227                 pin_state = priv->pins_default;
228                 break;
229         case MMC_SIGNAL_VOLTAGE_180:
230                 pin_state = priv->pins_uhs;
231                 break;
232         default:
233                 return -EINVAL;
234         }
235
236         /*
237          * If anything is missing, assume signal voltage is fixed at
238          * 3.3V and succeed/fail accordingly.
239          */
240         if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
241                 return ios->signal_voltage ==
242                         MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
243
244         ret = mmc_regulator_set_vqmmc(host->mmc, ios);
245         if (ret)
246                 return ret;
247
248         return pinctrl_select_state(priv->pinctrl, pin_state);
249 }
250
251 /* SCC registers */
252 #define SH_MOBILE_SDHI_SCC_DTCNTL       0x000
253 #define SH_MOBILE_SDHI_SCC_TAPSET       0x002
254 #define SH_MOBILE_SDHI_SCC_DT2FF        0x004
255 #define SH_MOBILE_SDHI_SCC_CKSEL        0x006
256 #define SH_MOBILE_SDHI_SCC_RVSCNTL      0x008
257 #define SH_MOBILE_SDHI_SCC_RVSREQ       0x00A
258 #define SH_MOBILE_SDHI_SCC_TMPPORT2     0x00E
259
260 /* Definitions for values the SH_MOBILE_SDHI_SCC_DTCNTL register */
261 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN         BIT(0)
262 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT  16
263 #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK   0xff
264
265 /* Definitions for values the SH_MOBILE_SDHI_SCC_CKSEL register */
266 #define SH_MOBILE_SDHI_SCC_CKSEL_DTSEL          BIT(0)
267 /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSCNTL register */
268 #define SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN        BIT(0)
269 /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSREQ register */
270 #define SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR        BIT(2)
271 /* Definitions for values the SH_MOBILE_SDHI_SCC_TMPPORT2 register */
272 #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL   BIT(4)
273 #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN     BIT(31)
274
275 static inline u32 sd_scc_read32(struct tmio_mmc_host *host,
276                                 struct renesas_sdhi *priv, int addr)
277 {
278         return readl(priv->scc_ctl + (addr << host->bus_shift));
279 }
280
281 static inline void sd_scc_write32(struct tmio_mmc_host *host,
282                                   struct renesas_sdhi *priv,
283                                   int addr, u32 val)
284 {
285         writel(val, priv->scc_ctl + (addr << host->bus_shift));
286 }
287
288 static unsigned int renesas_sdhi_init_tuning(struct tmio_mmc_host *host)
289 {
290         struct renesas_sdhi *priv;
291
292         priv = host_to_priv(host);
293
294         /* Initialize SCC */
295         sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, 0x0);
296
297         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
298                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
299
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);
304
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));
308
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));
312
313         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF, priv->scc_tappos);
314
315         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
316                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
317
318         /* Read TAPNUM */
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;
322 }
323
324 static void renesas_sdhi_prepare_tuning(struct tmio_mmc_host *host,
325                                         unsigned long tap)
326 {
327         struct renesas_sdhi *priv = host_to_priv(host);
328
329         /* Set sampling clock position */
330         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, tap);
331 }
332
333 static void renesas_sdhi_hs400_complete(struct tmio_mmc_host *host)
334 {
335         struct renesas_sdhi *priv = host_to_priv(host);
336
337         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
338                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
339
340         /* Set HS400 mode */
341         sd_ctrl_write16(host, CTL_SDIF_MODE, 0x0001 |
342                         sd_ctrl_read16(host, CTL_SDIF_MODE));
343         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
344                        (SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
345                         SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) |
346                         sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
347
348         /* Set the sampling clock selection range of HS400 mode */
349         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
350                        SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
351                        0x4 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
352
353
354         if (host->pdata->flags & TMIO_MMC_HAVE_4TAP_HS400)
355                 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET,
356                                host->tap_set / 2);
357
358         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
359                        SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
360                        sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
361
362         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
363                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
364 }
365
366 static void renesas_sdhi_reset_scc(struct tmio_mmc_host *host,
367                                    struct renesas_sdhi *priv)
368 {
369         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
370                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
371
372         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
373                        ~SH_MOBILE_SDHI_SCC_CKSEL_DTSEL &
374                        sd_scc_read32(host, priv,
375                                      SH_MOBILE_SDHI_SCC_CKSEL));
376 }
377
378 static void renesas_sdhi_disable_scc(struct tmio_mmc_host *host)
379 {
380         struct renesas_sdhi *priv = host_to_priv(host);
381
382         renesas_sdhi_reset_scc(host, priv);
383
384         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
385                        ~SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN &
386                        sd_scc_read32(host, priv,
387                                      SH_MOBILE_SDHI_SCC_DTCNTL));
388
389         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
390                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
391 }
392
393 static void renesas_sdhi_reset_hs400_mode(struct tmio_mmc_host *host,
394                                           struct renesas_sdhi *priv)
395 {
396         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
397                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
398
399         /* Reset HS400 mode */
400         sd_ctrl_write16(host, CTL_SDIF_MODE, ~0x0001 &
401                         sd_ctrl_read16(host, CTL_SDIF_MODE));
402         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
403                        ~(SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
404                          SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) &
405                         sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
406
407         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
408                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
409 }
410
411 static void renesas_sdhi_prepare_hs400_tuning(struct tmio_mmc_host *host)
412 {
413         renesas_sdhi_reset_hs400_mode(host, host_to_priv(host));
414 }
415
416 #define SH_MOBILE_SDHI_MAX_TAP 3
417
418 static int renesas_sdhi_select_tuning(struct tmio_mmc_host *host)
419 {
420         struct renesas_sdhi *priv = host_to_priv(host);
421         unsigned long tap_cnt;  /* counter of tuning success */
422         unsigned long tap_start;/* start position of tuning success */
423         unsigned long tap_end;  /* end position of tuning success */
424         unsigned long ntap;     /* temporary counter of tuning success */
425         unsigned long i;
426
427         /* Clear SCC_RVSREQ */
428         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
429
430         /*
431          * When tuning CMD19 is issued twice for each tap, merge the
432          * result requiring the tap to be good in both runs before
433          * considering it for tuning selection.
434          */
435         for (i = 0; i < host->tap_num * 2; i++) {
436                 int offset = host->tap_num * (i < host->tap_num ? 1 : -1);
437
438                 if (!test_bit(i, host->taps))
439                         clear_bit(i + offset, host->taps);
440         }
441
442         /*
443          * Find the longest consecutive run of successful probes.  If that
444          * is more than SH_MOBILE_SDHI_MAX_TAP probes long then use the
445          * center index as the tap.
446          */
447         tap_cnt = 0;
448         ntap = 0;
449         tap_start = 0;
450         tap_end = 0;
451         for (i = 0; i < host->tap_num * 2; i++) {
452                 if (test_bit(i, host->taps)) {
453                         ntap++;
454                 } else {
455                         if (ntap > tap_cnt) {
456                                 tap_start = i - ntap;
457                                 tap_end = i - 1;
458                                 tap_cnt = ntap;
459                         }
460                         ntap = 0;
461                 }
462         }
463
464         if (ntap > tap_cnt) {
465                 tap_start = i - ntap;
466                 tap_end = i - 1;
467                 tap_cnt = ntap;
468         }
469
470         if (tap_cnt >= SH_MOBILE_SDHI_MAX_TAP)
471                 host->tap_set = (tap_start + tap_end) / 2 % host->tap_num;
472         else
473                 return -EIO;
474
475         /* Set SCC */
476         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, host->tap_set);
477
478         /* Enable auto re-tuning */
479         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
480                        SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN |
481                        sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
482
483         return 0;
484 }
485
486 static bool renesas_sdhi_check_scc_error(struct tmio_mmc_host *host)
487 {
488         struct renesas_sdhi *priv = host_to_priv(host);
489
490         /* Check SCC error */
491         if (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL) &
492             SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &&
493             sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ) &
494             SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR) {
495                 /* Clear SCC error */
496                 sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
497                 return true;
498         }
499
500         return false;
501 }
502
503 static void renesas_sdhi_hw_reset(struct tmio_mmc_host *host)
504 {
505         struct renesas_sdhi *priv;
506
507         priv = host_to_priv(host);
508
509         renesas_sdhi_reset_scc(host, priv);
510         renesas_sdhi_reset_hs400_mode(host, priv);
511
512         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
513                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
514
515         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
516                        ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
517                        sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
518
519         sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
520                        ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
521                        sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
522 }
523
524 static int renesas_sdhi_wait_idle(struct tmio_mmc_host *host, u32 bit)
525 {
526         int timeout = 1000;
527         /* CBSY is set when busy, SCLKDIVEN is cleared when busy */
528         u32 wait_state = (bit == TMIO_STAT_CMD_BUSY ? TMIO_STAT_CMD_BUSY : 0);
529
530         while (--timeout && (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS)
531                               & bit) == wait_state)
532                 udelay(1);
533
534         if (!timeout) {
535                 dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
536                 return -EBUSY;
537         }
538
539         return 0;
540 }
541
542 static int renesas_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
543 {
544         u32 bit = TMIO_STAT_SCLKDIVEN;
545
546         switch (addr) {
547         case CTL_SD_CMD:
548         case CTL_STOP_INTERNAL_ACTION:
549         case CTL_XFER_BLK_COUNT:
550         case CTL_SD_XFER_LEN:
551         case CTL_SD_MEM_CARD_OPT:
552         case CTL_TRANSACTION_CTL:
553         case CTL_DMA_ENABLE:
554         case HOST_MODE:
555                 if (host->pdata->flags & TMIO_MMC_HAVE_CBSY)
556                         bit = TMIO_STAT_CMD_BUSY;
557                 /* fallthrough */
558         case CTL_SD_CARD_CLK_CTL:
559                 return renesas_sdhi_wait_idle(host, bit);
560         }
561
562         return 0;
563 }
564
565 static int renesas_sdhi_multi_io_quirk(struct mmc_card *card,
566                                        unsigned int direction, int blk_size)
567 {
568         /*
569          * In Renesas controllers, when performing a
570          * multiple block read of one or two blocks,
571          * depending on the timing with which the
572          * response register is read, the response
573          * value may not be read properly.
574          * Use single block read for this HW bug
575          */
576         if ((direction == MMC_DATA_READ) &&
577             blk_size == 2)
578                 return 1;
579
580         return blk_size;
581 }
582
583 static void renesas_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
584 {
585         /* Iff regs are 8 byte apart, sdbuf is 64 bit. Otherwise always 32. */
586         int width = (host->bus_shift == 2) ? 64 : 32;
587
588         sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? DMA_ENABLE_DMASDRW : 0);
589         renesas_sdhi_sdbuf_width(host, enable ? width : 16);
590 }
591
592 int renesas_sdhi_probe(struct platform_device *pdev,
593                        const struct tmio_mmc_dma_ops *dma_ops)
594 {
595         struct tmio_mmc_data *mmd = pdev->dev.platform_data;
596         const struct renesas_sdhi_of_data *of_data;
597         struct tmio_mmc_data *mmc_data;
598         struct tmio_mmc_dma *dma_priv;
599         struct tmio_mmc_host *host;
600         struct renesas_sdhi *priv;
601         struct resource *res;
602         int irq, ret, i;
603
604         of_data = of_device_get_match_data(&pdev->dev);
605
606         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
607         if (!res)
608                 return -EINVAL;
609
610         priv = devm_kzalloc(&pdev->dev, sizeof(struct renesas_sdhi),
611                             GFP_KERNEL);
612         if (!priv)
613                 return -ENOMEM;
614
615         mmc_data = &priv->mmc_data;
616         dma_priv = &priv->dma_priv;
617
618         priv->clk = devm_clk_get(&pdev->dev, NULL);
619         if (IS_ERR(priv->clk)) {
620                 ret = PTR_ERR(priv->clk);
621                 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
622                 return ret;
623         }
624
625         /*
626          * Some controllers provide a 2nd clock just to run the internal card
627          * detection logic. Unfortunately, the existing driver architecture does
628          * not support a separation of clocks for runtime PM usage. When
629          * native hotplug is used, the tmio driver assumes that the core
630          * must continue to run for card detect to stay active, so we cannot
631          * disable it.
632          * Additionally, it is prohibited to supply a clock to the core but not
633          * to the card detect circuit. That leaves us with if separate clocks
634          * are presented, we must treat them both as virtually 1 clock.
635          */
636         priv->clk_cd = devm_clk_get(&pdev->dev, "cd");
637         if (IS_ERR(priv->clk_cd))
638                 priv->clk_cd = NULL;
639
640         priv->pinctrl = devm_pinctrl_get(&pdev->dev);
641         if (!IS_ERR(priv->pinctrl)) {
642                 priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
643                                                 PINCTRL_STATE_DEFAULT);
644                 priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
645                                                 "state_uhs");
646         }
647
648         host = tmio_mmc_host_alloc(pdev, mmc_data);
649         if (IS_ERR(host))
650                 return PTR_ERR(host);
651
652         if (of_data) {
653                 mmc_data->flags |= of_data->tmio_flags;
654                 mmc_data->ocr_mask = of_data->tmio_ocr_mask;
655                 mmc_data->capabilities |= of_data->capabilities;
656                 mmc_data->capabilities2 |= of_data->capabilities2;
657                 mmc_data->dma_rx_offset = of_data->dma_rx_offset;
658                 mmc_data->max_blk_count = of_data->max_blk_count;
659                 mmc_data->max_segs = of_data->max_segs;
660                 dma_priv->dma_buswidth = of_data->dma_buswidth;
661                 host->bus_shift = of_data->bus_shift;
662         }
663
664         host->write16_hook      = renesas_sdhi_write16_hook;
665         host->clk_enable        = renesas_sdhi_clk_enable;
666         host->clk_disable       = renesas_sdhi_clk_disable;
667         host->set_clock         = renesas_sdhi_set_clock;
668         host->multi_io_quirk    = renesas_sdhi_multi_io_quirk;
669         host->dma_ops           = dma_ops;
670
671         /* For some SoC, we disable internal WP. GPIO may override this */
672         if (mmc_can_gpio_ro(host->mmc))
673                 mmc_data->capabilities2 &= ~MMC_CAP2_NO_WRITE_PROTECT;
674
675         /* SDR speeds are only available on Gen2+ */
676         if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
677                 /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
678                 host->ops.card_busy = renesas_sdhi_card_busy;
679                 host->ops.start_signal_voltage_switch =
680                         renesas_sdhi_start_signal_voltage_switch;
681         }
682
683         /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
684         if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
685                 host->bus_shift = 1;
686
687         if (mmd)
688                 *mmc_data = *mmd;
689
690         dma_priv->filter = shdma_chan_filter;
691         dma_priv->enable = renesas_sdhi_enable_dma;
692
693         mmc_data->alignment_shift = 1; /* 2-byte alignment */
694         mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
695
696         /*
697          * All SDHI blocks support 2-byte and larger block sizes in 4-bit
698          * bus width mode.
699          */
700         mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
701
702         /*
703          * All SDHI blocks support SDIO IRQ signalling.
704          */
705         mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
706
707         /* All SDHI have CMD12 control bit */
708         mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
709
710         /* All SDHI have SDIO status bits which must be 1 */
711         mmc_data->flags |= TMIO_MMC_SDIO_STATUS_SETBITS;
712
713         ret = renesas_sdhi_clk_enable(host);
714         if (ret)
715                 goto efree;
716
717         ret = tmio_mmc_host_probe(host);
718         if (ret < 0)
719                 goto edisclk;
720
721         /* One Gen2 SDHI incarnation does NOT have a CBSY bit */
722         if (sd_ctrl_read16(host, CTL_VERSION) == SDHI_VER_GEN2_SDR50)
723                 mmc_data->flags &= ~TMIO_MMC_HAVE_CBSY;
724
725         /* Enable tuning iff we have an SCC and a supported mode */
726         if (of_data && of_data->scc_offset &&
727             (host->mmc->caps & MMC_CAP_UHS_SDR104 ||
728              host->mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR |
729                                  MMC_CAP2_HS400_1_8V))) {
730                 const struct renesas_sdhi_scc *taps = of_data->taps;
731                 bool hit = false;
732
733                 host->mmc->caps |= MMC_CAP_HW_RESET;
734
735                 for (i = 0; i < of_data->taps_num; i++) {
736                         if (taps[i].clk_rate == 0 ||
737                             taps[i].clk_rate == host->mmc->f_max) {
738                                 priv->scc_tappos = taps->tap;
739                                 hit = true;
740                                 break;
741                         }
742                 }
743
744                 if (!hit)
745                         dev_warn(&host->pdev->dev, "Unknown clock rate for SDR104\n");
746
747                 priv->scc_ctl = host->ctl + of_data->scc_offset;
748                 host->init_tuning = renesas_sdhi_init_tuning;
749                 host->prepare_tuning = renesas_sdhi_prepare_tuning;
750                 host->select_tuning = renesas_sdhi_select_tuning;
751                 host->check_scc_error = renesas_sdhi_check_scc_error;
752                 host->hw_reset = renesas_sdhi_hw_reset;
753                 host->prepare_hs400_tuning =
754                         renesas_sdhi_prepare_hs400_tuning;
755                 host->hs400_downgrade = renesas_sdhi_disable_scc;
756                 host->hs400_complete = renesas_sdhi_hs400_complete;
757         }
758
759         i = 0;
760         while (1) {
761                 irq = platform_get_irq(pdev, i);
762                 if (irq < 0)
763                         break;
764                 i++;
765                 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
766                                        dev_name(&pdev->dev), host);
767                 if (ret)
768                         goto eirq;
769         }
770
771         /* There must be at least one IRQ source */
772         if (!i) {
773                 ret = irq;
774                 goto eirq;
775         }
776
777         dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
778                  mmc_hostname(host->mmc), (unsigned long)
779                  (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
780                  host->mmc->f_max / 1000000);
781
782         return ret;
783
784 eirq:
785         tmio_mmc_host_remove(host);
786 edisclk:
787         renesas_sdhi_clk_disable(host);
788 efree:
789         tmio_mmc_host_free(host);
790
791         return ret;
792 }
793 EXPORT_SYMBOL_GPL(renesas_sdhi_probe);
794
795 int renesas_sdhi_remove(struct platform_device *pdev)
796 {
797         struct tmio_mmc_host *host = platform_get_drvdata(pdev);
798
799         tmio_mmc_host_remove(host);
800         renesas_sdhi_clk_disable(host);
801
802         return 0;
803 }
804 EXPORT_SYMBOL_GPL(renesas_sdhi_remove);
805
806 MODULE_LICENSE("GPL v2");