GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / mmc / host / sdhci-of-arasan.c
1 /*
2  * Arasan Secure Digital Host Controller Interface.
3  * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
4  * Copyright (c) 2012 Wind River Systems, Inc.
5  * Copyright (C) 2013 Pengutronix e.K.
6  * Copyright (C) 2013 Xilinx Inc.
7  *
8  * Based on sdhci-of-esdhc.c
9  *
10  * Copyright (c) 2007 Freescale Semiconductor, Inc.
11  * Copyright (c) 2009 MontaVista Software, Inc.
12  *
13  * Authors: Xiaobo Xie <X.Xie@freescale.com>
14  *          Anton Vorontsov <avorontsov@ru.mvista.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or (at
19  * your option) any later version.
20  */
21
22 #include <linux/clk-provider.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/phy/phy.h>
27 #include <linux/regmap.h>
28 #include <linux/of.h>
29
30 #include "cqhci.h"
31 #include "sdhci-pltfm.h"
32
33 #define SDHCI_ARASAN_VENDOR_REGISTER    0x78
34 #define SDHCI_ARASAN_CQE_BASE_ADDR      0x200
35 #define VENDOR_ENHANCED_STROBE          BIT(0)
36
37 #define PHY_CLK_TOO_SLOW_HZ             400000
38
39 /*
40  * On some SoCs the syscon area has a feature where the upper 16-bits of
41  * each 32-bit register act as a write mask for the lower 16-bits.  This allows
42  * atomic updates of the register without locking.  This macro is used on SoCs
43  * that have that feature.
44  */
45 #define HIWORD_UPDATE(val, mask, shift) \
46                 ((val) << (shift) | (mask) << ((shift) + 16))
47
48 /**
49  * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
50  *
51  * @reg:        Offset within the syscon of the register containing this field
52  * @width:      Number of bits for this field
53  * @shift:      Bit offset within @reg of this field (or -1 if not avail)
54  */
55 struct sdhci_arasan_soc_ctl_field {
56         u32 reg;
57         u16 width;
58         s16 shift;
59 };
60
61 /**
62  * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
63  *
64  * It's up to the licensee of the Arsan IP block to make these available
65  * somewhere if needed.  Presumably these will be scattered somewhere that's
66  * accessible via the syscon API.
67  *
68  * @baseclkfreq:        Where to find corecfg_baseclkfreq
69  * @clockmultiplier:    Where to find corecfg_clockmultiplier
70  * @hiword_update:      If true, use HIWORD_UPDATE to access the syscon
71  */
72 struct sdhci_arasan_soc_ctl_map {
73         struct sdhci_arasan_soc_ctl_field       baseclkfreq;
74         struct sdhci_arasan_soc_ctl_field       clockmultiplier;
75         bool                                    hiword_update;
76 };
77
78 /**
79  * struct sdhci_arasan_data
80  * @host:               Pointer to the main SDHCI host structure.
81  * @clk_ahb:            Pointer to the AHB clock
82  * @phy:                Pointer to the generic phy
83  * @is_phy_on:          True if the PHY is on; false if not.
84  * @sdcardclk_hw:       Struct for the clock we might provide to a PHY.
85  * @sdcardclk:          Pointer to normal 'struct clock' for sdcardclk_hw.
86  * @soc_ctl_base:       Pointer to regmap for syscon for soc_ctl registers.
87  * @soc_ctl_map:        Map to get offsets into soc_ctl registers.
88  */
89 struct sdhci_arasan_data {
90         struct sdhci_host *host;
91         struct clk      *clk_ahb;
92         struct phy      *phy;
93         bool            is_phy_on;
94
95         bool            has_cqe;
96         struct clk_hw   sdcardclk_hw;
97         struct clk      *sdcardclk;
98
99         struct regmap   *soc_ctl_base;
100         const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
101         unsigned int    quirks; /* Arasan deviations from spec */
102
103 /* Controller does not have CD wired and will not function normally without */
104 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
105 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
106  * internal clock even when the clock isn't stable */
107 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
108 };
109
110 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
111         .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
112         .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
113         .hiword_update = true,
114 };
115
116 /**
117  * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
118  *
119  * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
120  * Note that if a field is specified as not available (shift < 0) then
121  * this function will silently return an error code.  It will be noisy
122  * and print errors for any other (unexpected) errors.
123  *
124  * @host:       The sdhci_host
125  * @fld:        The field to write to
126  * @val:        The value to write
127  */
128 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
129                                    const struct sdhci_arasan_soc_ctl_field *fld,
130                                    u32 val)
131 {
132         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
133         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
134         struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
135         u32 reg = fld->reg;
136         u16 width = fld->width;
137         s16 shift = fld->shift;
138         int ret;
139
140         /*
141          * Silently return errors for shift < 0 so caller doesn't have
142          * to check for fields which are optional.  For fields that
143          * are required then caller needs to do something special
144          * anyway.
145          */
146         if (shift < 0)
147                 return -EINVAL;
148
149         if (sdhci_arasan->soc_ctl_map->hiword_update)
150                 ret = regmap_write(soc_ctl_base, reg,
151                                    HIWORD_UPDATE(val, GENMASK(width, 0),
152                                                  shift));
153         else
154                 ret = regmap_update_bits(soc_ctl_base, reg,
155                                          GENMASK(shift + width, shift),
156                                          val << shift);
157
158         /* Yell about (unexpected) regmap errors */
159         if (ret)
160                 pr_warn("%s: Regmap write fail: %d\n",
161                          mmc_hostname(host->mmc), ret);
162
163         return ret;
164 }
165
166 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
167 {
168         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
169         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
170         bool ctrl_phy = false;
171
172         if (!IS_ERR(sdhci_arasan->phy)) {
173                 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
174                         /*
175                          * If PHY off, set clock to max speed and power PHY on.
176                          *
177                          * Although PHY docs apparently suggest power cycling
178                          * when changing the clock the PHY doesn't like to be
179                          * powered on while at low speeds like those used in ID
180                          * mode.  Even worse is powering the PHY on while the
181                          * clock is off.
182                          *
183                          * To workaround the PHY limitations, the best we can
184                          * do is to power it on at a faster speed and then slam
185                          * through low speeds without power cycling.
186                          */
187                         sdhci_set_clock(host, host->max_clk);
188                         if (phy_power_on(sdhci_arasan->phy)) {
189                                 pr_err("%s: Cannot power on phy.\n",
190                                        mmc_hostname(host->mmc));
191                                 return;
192                         }
193
194                         sdhci_arasan->is_phy_on = true;
195
196                         /*
197                          * We'll now fall through to the below case with
198                          * ctrl_phy = false (so we won't turn off/on).  The
199                          * sdhci_set_clock() will set the real clock.
200                          */
201                 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
202                         /*
203                          * At higher clock speeds the PHY is fine being power
204                          * cycled and docs say you _should_ power cycle when
205                          * changing clock speeds.
206                          */
207                         ctrl_phy = true;
208                 }
209         }
210
211         if (ctrl_phy && sdhci_arasan->is_phy_on) {
212                 phy_power_off(sdhci_arasan->phy);
213                 sdhci_arasan->is_phy_on = false;
214         }
215
216         sdhci_set_clock(host, clock);
217
218         if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
219                 /*
220                  * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
221                  * after enabling the clock even though the clock is not
222                  * stable. Trying to use a clock without waiting here results
223                  * in EILSEQ while detecting some older/slower cards. The
224                  * chosen delay is the maximum delay from sdhci_set_clock.
225                  */
226                 msleep(20);
227
228         if (ctrl_phy) {
229                 if (phy_power_on(sdhci_arasan->phy)) {
230                         pr_err("%s: Cannot power on phy.\n",
231                                mmc_hostname(host->mmc));
232                         return;
233                 }
234
235                 sdhci_arasan->is_phy_on = true;
236         }
237 }
238
239 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
240                                         struct mmc_ios *ios)
241 {
242         u32 vendor;
243         struct sdhci_host *host = mmc_priv(mmc);
244
245         vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
246         if (ios->enhanced_strobe)
247                 vendor |= VENDOR_ENHANCED_STROBE;
248         else
249                 vendor &= ~VENDOR_ENHANCED_STROBE;
250
251         sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
252 }
253
254 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
255 {
256         u8 ctrl;
257         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
258         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
259
260         sdhci_reset(host, mask);
261
262         if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
263                 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
264                 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
265                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
266         }
267 }
268
269 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
270                                        struct mmc_ios *ios)
271 {
272         switch (ios->signal_voltage) {
273         case MMC_SIGNAL_VOLTAGE_180:
274                 /*
275                  * Plese don't switch to 1V8 as arasan,5.1 doesn't
276                  * actually refer to this setting to indicate the
277                  * signal voltage and the state machine will be broken
278                  * actually if we force to enable 1V8. That's something
279                  * like broken quirk but we could work around here.
280                  */
281                 return 0;
282         case MMC_SIGNAL_VOLTAGE_330:
283         case MMC_SIGNAL_VOLTAGE_120:
284                 /* We don't support 3V3 and 1V2 */
285                 break;
286         }
287
288         return -EINVAL;
289 }
290
291 static void sdhci_arasan_set_power(struct sdhci_host *host, unsigned char mode,
292                      unsigned short vdd)
293 {
294         if (!IS_ERR(host->mmc->supply.vmmc)) {
295                 struct mmc_host *mmc = host->mmc;
296
297                 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
298         }
299         sdhci_set_power_noreg(host, mode, vdd);
300 }
301
302 static const struct sdhci_ops sdhci_arasan_ops = {
303         .set_clock = sdhci_arasan_set_clock,
304         .get_max_clock = sdhci_pltfm_clk_get_max_clock,
305         .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
306         .set_bus_width = sdhci_set_bus_width,
307         .reset = sdhci_arasan_reset,
308         .set_uhs_signaling = sdhci_set_uhs_signaling,
309         .set_power = sdhci_arasan_set_power,
310 };
311
312 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
313         .ops = &sdhci_arasan_ops,
314         .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
315         .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
316                         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
317                         SDHCI_QUIRK2_STOP_WITH_TC,
318 };
319
320 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
321 {
322         int cmd_error = 0;
323         int data_error = 0;
324
325         if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
326                 return intmask;
327
328         cqhci_irq(host->mmc, intmask, cmd_error, data_error);
329
330         return 0;
331 }
332
333 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
334 {
335         sdhci_dumpregs(mmc_priv(mmc));
336 }
337
338 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
339 {
340         struct sdhci_host *host = mmc_priv(mmc);
341         u32 reg;
342
343         reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
344         while (reg & SDHCI_DATA_AVAILABLE) {
345                 sdhci_readl(host, SDHCI_BUFFER);
346                 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
347         }
348
349         sdhci_cqe_enable(mmc);
350 }
351
352 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
353         .enable         = sdhci_arasan_cqe_enable,
354         .disable        = sdhci_cqe_disable,
355         .dumpregs       = sdhci_arasan_dumpregs,
356 };
357
358 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
359         .set_clock = sdhci_arasan_set_clock,
360         .get_max_clock = sdhci_pltfm_clk_get_max_clock,
361         .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
362         .set_bus_width = sdhci_set_bus_width,
363         .reset = sdhci_arasan_reset,
364         .set_uhs_signaling = sdhci_set_uhs_signaling,
365         .set_power = sdhci_arasan_set_power,
366         .irq = sdhci_arasan_cqhci_irq,
367 };
368
369 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
370         .ops = &sdhci_arasan_cqe_ops,
371         .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
372         .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
373                         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
374 };
375
376 #ifdef CONFIG_PM_SLEEP
377 /**
378  * sdhci_arasan_suspend - Suspend method for the driver
379  * @dev:        Address of the device structure
380  * Returns 0 on success and error value on error
381  *
382  * Put the device in a low power state.
383  */
384 static int sdhci_arasan_suspend(struct device *dev)
385 {
386         struct sdhci_host *host = dev_get_drvdata(dev);
387         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
388         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
389         int ret;
390
391         if (host->tuning_mode != SDHCI_TUNING_MODE_3)
392                 mmc_retune_needed(host->mmc);
393
394         if (sdhci_arasan->has_cqe) {
395                 ret = cqhci_suspend(host->mmc);
396                 if (ret)
397                         return ret;
398         }
399
400         ret = sdhci_suspend_host(host);
401         if (ret)
402                 return ret;
403
404         if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
405                 ret = phy_power_off(sdhci_arasan->phy);
406                 if (ret) {
407                         dev_err(dev, "Cannot power off phy.\n");
408                         if (sdhci_resume_host(host))
409                                 dev_err(dev, "Cannot resume host.\n");
410
411                         return ret;
412                 }
413                 sdhci_arasan->is_phy_on = false;
414         }
415
416         clk_disable(pltfm_host->clk);
417         clk_disable(sdhci_arasan->clk_ahb);
418
419         return 0;
420 }
421
422 /**
423  * sdhci_arasan_resume - Resume method for the driver
424  * @dev:        Address of the device structure
425  * Returns 0 on success and error value on error
426  *
427  * Resume operation after suspend
428  */
429 static int sdhci_arasan_resume(struct device *dev)
430 {
431         struct sdhci_host *host = dev_get_drvdata(dev);
432         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
433         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
434         int ret;
435
436         ret = clk_enable(sdhci_arasan->clk_ahb);
437         if (ret) {
438                 dev_err(dev, "Cannot enable AHB clock.\n");
439                 return ret;
440         }
441
442         ret = clk_enable(pltfm_host->clk);
443         if (ret) {
444                 dev_err(dev, "Cannot enable SD clock.\n");
445                 return ret;
446         }
447
448         if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
449                 ret = phy_power_on(sdhci_arasan->phy);
450                 if (ret) {
451                         dev_err(dev, "Cannot power on phy.\n");
452                         return ret;
453                 }
454                 sdhci_arasan->is_phy_on = true;
455         }
456
457         ret = sdhci_resume_host(host);
458         if (ret) {
459                 dev_err(dev, "Cannot resume host.\n");
460                 return ret;
461         }
462
463         if (sdhci_arasan->has_cqe)
464                 return cqhci_resume(host->mmc);
465
466         return 0;
467 }
468 #endif /* ! CONFIG_PM_SLEEP */
469
470 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
471                          sdhci_arasan_resume);
472
473 static const struct of_device_id sdhci_arasan_of_match[] = {
474         /* SoC-specific compatible strings w/ soc_ctl_map */
475         {
476                 .compatible = "rockchip,rk3399-sdhci-5.1",
477                 .data = &rk3399_soc_ctl_map,
478         },
479
480         /* Generic compatible below here */
481         { .compatible = "arasan,sdhci-8.9a" },
482         { .compatible = "arasan,sdhci-5.1" },
483         { .compatible = "arasan,sdhci-4.9a" },
484
485         { /* sentinel */ }
486 };
487 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
488
489 /**
490  * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
491  *
492  * Return the current actual rate of the SD card clock.  This can be used
493  * to communicate with out PHY.
494  *
495  * @hw:                 Pointer to the hardware clock structure.
496  * @parent_rate         The parent rate (should be rate of clk_xin).
497  * Returns the card clock rate.
498  */
499 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
500                                                       unsigned long parent_rate)
501
502 {
503         struct sdhci_arasan_data *sdhci_arasan =
504                 container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
505         struct sdhci_host *host = sdhci_arasan->host;
506
507         return host->mmc->actual_clock;
508 }
509
510 static const struct clk_ops arasan_sdcardclk_ops = {
511         .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
512 };
513
514 /**
515  * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
516  *
517  * The corecfg_clockmultiplier is supposed to contain clock multiplier
518  * value of programmable clock generator.
519  *
520  * NOTES:
521  * - Many existing devices don't seem to do this and work fine.  To keep
522  *   compatibility for old hardware where the device tree doesn't provide a
523  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
524  *   for this platform.
525  * - The value of corecfg_clockmultiplier should sync with that of corresponding
526  *   value reading from sdhci_capability_register. So this function is called
527  *   once at probe time and never called again.
528  *
529  * @host:               The sdhci_host
530  */
531 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
532                                                 u32 value)
533 {
534         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
535         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
536         const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
537                 sdhci_arasan->soc_ctl_map;
538
539         /* Having a map is optional */
540         if (!soc_ctl_map)
541                 return;
542
543         /* If we have a map, we expect to have a syscon */
544         if (!sdhci_arasan->soc_ctl_base) {
545                 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
546                         mmc_hostname(host->mmc));
547                 return;
548         }
549
550         sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
551 }
552
553 /**
554  * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
555  *
556  * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
557  * function can be used to make that happen.
558  *
559  * NOTES:
560  * - Many existing devices don't seem to do this and work fine.  To keep
561  *   compatibility for old hardware where the device tree doesn't provide a
562  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
563  *   for this platform.
564  * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
565  *   to achieve lower clock rates.  That means that this function is called once
566  *   at probe time and never called again.
567  *
568  * @host:               The sdhci_host
569  */
570 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
571 {
572         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
573         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
574         const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
575                 sdhci_arasan->soc_ctl_map;
576         u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
577
578         /* Having a map is optional */
579         if (!soc_ctl_map)
580                 return;
581
582         /* If we have a map, we expect to have a syscon */
583         if (!sdhci_arasan->soc_ctl_base) {
584                 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
585                         mmc_hostname(host->mmc));
586                 return;
587         }
588
589         sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
590 }
591
592 /**
593  * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
594  *
595  * Some PHY devices need to know what the actual card clock is.  In order for
596  * them to find out, we'll provide a clock through the common clock framework
597  * for them to query.
598  *
599  * Note: without seriously re-architecting SDHCI's clock code and testing on
600  * all platforms, there's no way to create a totally beautiful clock here
601  * with all clock ops implemented.  Instead, we'll just create a clock that can
602  * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
603  * framework that we're doing things behind its back.  This should be sufficient
604  * to create nice clean device tree bindings and later (if needed) we can try
605  * re-architecting SDHCI if we see some benefit to it.
606  *
607  * @sdhci_arasan:       Our private data structure.
608  * @clk_xin:            Pointer to the functional clock
609  * @dev:                Pointer to our struct device.
610  * Returns 0 on success and error value on error
611  */
612 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
613                                        struct clk *clk_xin,
614                                        struct device *dev)
615 {
616         struct device_node *np = dev->of_node;
617         struct clk_init_data sdcardclk_init;
618         const char *parent_clk_name;
619         int ret;
620
621         /* Providing a clock to the PHY is optional; no error if missing */
622         if (!of_find_property(np, "#clock-cells", NULL))
623                 return 0;
624
625         ret = of_property_read_string_index(np, "clock-output-names", 0,
626                                             &sdcardclk_init.name);
627         if (ret) {
628                 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
629                 return ret;
630         }
631
632         parent_clk_name = __clk_get_name(clk_xin);
633         sdcardclk_init.parent_names = &parent_clk_name;
634         sdcardclk_init.num_parents = 1;
635         sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
636         sdcardclk_init.ops = &arasan_sdcardclk_ops;
637
638         sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
639         sdhci_arasan->sdcardclk =
640                 devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
641         sdhci_arasan->sdcardclk_hw.init = NULL;
642
643         ret = of_clk_add_provider(np, of_clk_src_simple_get,
644                                   sdhci_arasan->sdcardclk);
645         if (ret)
646                 dev_err(dev, "Failed to add clock provider\n");
647
648         return ret;
649 }
650
651 /**
652  * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
653  *
654  * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
655  * returned success.
656  *
657  * @dev:                Pointer to our struct device.
658  */
659 static void sdhci_arasan_unregister_sdclk(struct device *dev)
660 {
661         struct device_node *np = dev->of_node;
662
663         if (!of_find_property(np, "#clock-cells", NULL))
664                 return;
665
666         of_clk_del_provider(dev->of_node);
667 }
668
669 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
670 {
671         struct sdhci_host *host = sdhci_arasan->host;
672         struct cqhci_host *cq_host;
673         bool dma64;
674         int ret;
675
676         if (!sdhci_arasan->has_cqe)
677                 return sdhci_add_host(host);
678
679         ret = sdhci_setup_host(host);
680         if (ret)
681                 return ret;
682
683         cq_host = devm_kzalloc(host->mmc->parent,
684                                sizeof(*cq_host), GFP_KERNEL);
685         if (!cq_host) {
686                 ret = -ENOMEM;
687                 goto cleanup;
688         }
689
690         cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
691         cq_host->ops = &sdhci_arasan_cqhci_ops;
692
693         dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
694         if (dma64)
695                 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
696
697         ret = cqhci_init(cq_host, host->mmc, dma64);
698         if (ret)
699                 goto cleanup;
700
701         ret = __sdhci_add_host(host);
702         if (ret)
703                 goto cleanup;
704
705         return 0;
706
707 cleanup:
708         sdhci_cleanup_host(host);
709         return ret;
710 }
711
712 static int sdhci_arasan_probe(struct platform_device *pdev)
713 {
714         int ret;
715         const struct of_device_id *match;
716         struct device_node *node;
717         struct clk *clk_xin;
718         struct sdhci_host *host;
719         struct sdhci_pltfm_host *pltfm_host;
720         struct sdhci_arasan_data *sdhci_arasan;
721         struct device_node *np = pdev->dev.of_node;
722         const struct sdhci_pltfm_data *pdata;
723
724         if (of_device_is_compatible(pdev->dev.of_node, "arasan,sdhci-5.1"))
725                 pdata = &sdhci_arasan_cqe_pdata;
726         else
727                 pdata = &sdhci_arasan_pdata;
728
729         host = sdhci_pltfm_init(pdev, pdata, sizeof(*sdhci_arasan));
730
731         if (IS_ERR(host))
732                 return PTR_ERR(host);
733
734         pltfm_host = sdhci_priv(host);
735         sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
736         sdhci_arasan->host = host;
737
738         match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
739         sdhci_arasan->soc_ctl_map = match->data;
740
741         node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
742         if (node) {
743                 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
744                 of_node_put(node);
745
746                 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
747                         ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
748                         if (ret != -EPROBE_DEFER)
749                                 dev_err(&pdev->dev, "Can't get syscon: %d\n",
750                                         ret);
751                         goto err_pltfm_free;
752                 }
753         }
754
755         sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
756         if (IS_ERR(sdhci_arasan->clk_ahb)) {
757                 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
758                 ret = PTR_ERR(sdhci_arasan->clk_ahb);
759                 goto err_pltfm_free;
760         }
761
762         clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
763         if (IS_ERR(clk_xin)) {
764                 dev_err(&pdev->dev, "clk_xin clock not found.\n");
765                 ret = PTR_ERR(clk_xin);
766                 goto err_pltfm_free;
767         }
768
769         ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
770         if (ret) {
771                 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
772                 goto err_pltfm_free;
773         }
774
775         ret = clk_prepare_enable(clk_xin);
776         if (ret) {
777                 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
778                 goto clk_dis_ahb;
779         }
780
781         sdhci_get_of_property(pdev);
782
783         if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
784                 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
785
786         if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
787                 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
788
789         pltfm_host->clk = clk_xin;
790
791         if (of_device_is_compatible(pdev->dev.of_node,
792                                     "rockchip,rk3399-sdhci-5.1"))
793                 sdhci_arasan_update_clockmultiplier(host, 0x0);
794
795         sdhci_arasan_update_baseclkfreq(host);
796
797         ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
798         if (ret)
799                 goto clk_disable_all;
800
801         ret = mmc_of_parse(host->mmc);
802         if (ret) {
803                 if (ret != -EPROBE_DEFER)
804                         dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
805                 goto unreg_clk;
806         }
807
808         sdhci_arasan->phy = ERR_PTR(-ENODEV);
809         if (of_device_is_compatible(pdev->dev.of_node,
810                                     "arasan,sdhci-5.1")) {
811                 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
812                                                  "phy_arasan");
813                 if (IS_ERR(sdhci_arasan->phy)) {
814                         ret = PTR_ERR(sdhci_arasan->phy);
815                         dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
816                         goto unreg_clk;
817                 }
818
819                 ret = phy_init(sdhci_arasan->phy);
820                 if (ret < 0) {
821                         dev_err(&pdev->dev, "phy_init err.\n");
822                         goto unreg_clk;
823                 }
824
825                 host->mmc_host_ops.hs400_enhanced_strobe =
826                                         sdhci_arasan_hs400_enhanced_strobe;
827                 host->mmc_host_ops.start_signal_voltage_switch =
828                                         sdhci_arasan_voltage_switch;
829                 sdhci_arasan->has_cqe = true;
830                 host->mmc->caps2 |= MMC_CAP2_CQE;
831
832                 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
833                         host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
834         }
835
836         ret = sdhci_arasan_add_host(sdhci_arasan);
837         if (ret)
838                 goto err_add_host;
839
840         return 0;
841
842 err_add_host:
843         if (!IS_ERR(sdhci_arasan->phy))
844                 phy_exit(sdhci_arasan->phy);
845 unreg_clk:
846         sdhci_arasan_unregister_sdclk(&pdev->dev);
847 clk_disable_all:
848         clk_disable_unprepare(clk_xin);
849 clk_dis_ahb:
850         clk_disable_unprepare(sdhci_arasan->clk_ahb);
851 err_pltfm_free:
852         sdhci_pltfm_free(pdev);
853         return ret;
854 }
855
856 static int sdhci_arasan_remove(struct platform_device *pdev)
857 {
858         int ret;
859         struct sdhci_host *host = platform_get_drvdata(pdev);
860         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
861         struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
862         struct clk *clk_ahb = sdhci_arasan->clk_ahb;
863
864         if (!IS_ERR(sdhci_arasan->phy)) {
865                 if (sdhci_arasan->is_phy_on)
866                         phy_power_off(sdhci_arasan->phy);
867                 phy_exit(sdhci_arasan->phy);
868         }
869
870         sdhci_arasan_unregister_sdclk(&pdev->dev);
871
872         ret = sdhci_pltfm_unregister(pdev);
873
874         clk_disable_unprepare(clk_ahb);
875
876         return ret;
877 }
878
879 static struct platform_driver sdhci_arasan_driver = {
880         .driver = {
881                 .name = "sdhci-arasan",
882                 .of_match_table = sdhci_arasan_of_match,
883                 .pm = &sdhci_arasan_dev_pm_ops,
884         },
885         .probe = sdhci_arasan_probe,
886         .remove = sdhci_arasan_remove,
887 };
888
889 module_platform_driver(sdhci_arasan_driver);
890
891 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
892 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
893 MODULE_LICENSE("GPL");