1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/clk-provider.h>
23#include <linux/mfd/syscon.h>
24#include <linux/module.h>
25#include <linux/delay.h>
26#include <linux/of_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/phy/phy.h>
29#include <linux/mmc/mmc.h>
30#include <linux/soc/xilinx/zynqmp/tap_delays.h>
31#include <linux/soc/xilinx/zynqmp/fw.h>
32#include <linux/pinctrl/consumer.h>
33#include <linux/regmap.h>
34#include "sdhci-pltfm.h"
35#include <linux/of.h>
36#include <linux/slab.h>
37
38#define SDHCI_ARASAN_VENDOR_REGISTER 0x78
39
40#define VENDOR_ENHANCED_STROBE BIT(0)
41#define CLK_CTRL_TIMEOUT_SHIFT 16
42#define CLK_CTRL_TIMEOUT_MASK (0xf << CLK_CTRL_TIMEOUT_SHIFT)
43#define CLK_CTRL_TIMEOUT_MIN_EXP 13
44#define SD_CLK_25_MHZ 25000000
45#define SD_CLK_19_MHZ 19000000
46#define MAX_TUNING_LOOP 40
47
48#define PHY_CLK_TOO_SLOW_HZ 400000
49
50
51
52
53
54
55
56#define HIWORD_UPDATE(val, mask, shift) \
57 ((val) << (shift) | (mask) << ((shift) + 16))
58
59
60
61
62
63
64
65
66struct sdhci_arasan_soc_ctl_field {
67 u32 reg;
68 u16 width;
69 s16 shift;
70};
71
72
73
74
75
76
77
78
79
80
81
82
83struct sdhci_arasan_soc_ctl_map {
84 struct sdhci_arasan_soc_ctl_field baseclkfreq;
85 struct sdhci_arasan_soc_ctl_field clockmultiplier;
86 bool hiword_update;
87};
88
89
90
91
92
93
94
95
96
97
98
99
100struct sdhci_arasan_data {
101 struct sdhci_host *host;
102 struct clk *clk_ahb;
103 struct phy *phy;
104 u32 mio_bank;
105 u32 device_id;
106 bool is_phy_on;
107
108 struct clk_hw sdcardclk_hw;
109 struct clk *sdcardclk;
110
111 struct regmap *soc_ctl_base;
112 struct pinctrl *pinctrl;
113 struct pinctrl_state *pins_default;
114 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
115 unsigned int quirks;
116
117
118#define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
119};
120
121static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
122 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
123 .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
124 .hiword_update = true,
125};
126
127
128
129
130
131
132
133
134
135
136
137
138
139static int sdhci_arasan_syscon_write(struct sdhci_host *host,
140 const struct sdhci_arasan_soc_ctl_field *fld,
141 u32 val)
142{
143 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
144 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
145 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
146 u32 reg = fld->reg;
147 u16 width = fld->width;
148 s16 shift = fld->shift;
149 int ret;
150
151
152
153
154
155
156
157 if (shift < 0)
158 return -EINVAL;
159
160 if (sdhci_arasan->soc_ctl_map->hiword_update)
161 ret = regmap_write(soc_ctl_base, reg,
162 HIWORD_UPDATE(val, GENMASK(width, 0),
163 shift));
164 else
165 ret = regmap_update_bits(soc_ctl_base, reg,
166 GENMASK(shift + width, shift),
167 val << shift);
168
169
170 if (ret)
171 pr_warn("%s: Regmap write fail: %d\n",
172 mmc_hostname(host->mmc), ret);
173
174 return ret;
175}
176
177static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
178{
179 u16 clk;
180 unsigned long timeout;
181
182 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
183 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
184 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
185
186
187 zynqmp_dll_reset(deviceid);
188
189 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
190 clk |= SDHCI_CLOCK_INT_EN;
191 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
192
193
194 timeout = 20;
195 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
196 & SDHCI_CLOCK_INT_STABLE)) {
197 if (timeout == 0) {
198 dev_err(mmc_dev(host->mmc),
199 ": Internal clock never stabilised.\n");
200 return;
201 }
202 timeout--;
203 mdelay(1);
204 }
205
206 clk |= SDHCI_CLOCK_CARD_EN;
207 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
208}
209
210static int arasan_zynqmp_execute_tuning(struct sdhci_host *host, u32 opcode)
211{
212 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
213 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
214 struct mmc_host *mmc = host->mmc;
215 u16 ctrl;
216 int tuning_loop_counter = MAX_TUNING_LOOP;
217 int err = 0;
218 unsigned long flags;
219 unsigned int tuning_count = 0;
220
221 spin_lock_irqsave(&host->lock, flags);
222
223 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
224 tuning_count = host->tuning_count;
225
226 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
227 ctrl |= SDHCI_CTRL_EXEC_TUNING;
228 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
229 ctrl |= SDHCI_CTRL_TUNED_CLK;
230 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
231
232 mdelay(1);
233
234 arasan_zynqmp_dll_reset(host, sdhci_arasan->device_id);
235
236
237
238
239
240
241
242
243
244
245
246 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
247 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
248
249
250
251
252
253 do {
254 struct mmc_command cmd = {0};
255 struct mmc_request mrq = {NULL};
256
257 cmd.opcode = opcode;
258 cmd.arg = 0;
259 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
260 cmd.retries = 0;
261 cmd.data = NULL;
262 cmd.mrq = &mrq;
263 cmd.error = 0;
264
265 if (tuning_loop_counter-- == 0)
266 break;
267
268 mrq.cmd = &cmd;
269
270
271
272
273
274
275 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
276 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
277 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
278 SDHCI_BLOCK_SIZE);
279 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
280 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
281 SDHCI_BLOCK_SIZE);
282 }
283 } else {
284 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
285 SDHCI_BLOCK_SIZE);
286 }
287
288
289
290
291
292
293
294 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
295
296 sdhci_send_command(host, &cmd);
297
298 host->cmd = NULL;
299
300 spin_unlock_irqrestore(&host->lock, flags);
301
302 wait_event_interruptible_timeout(host->buf_ready_int,
303 (host->tuning_done == 1),
304 msecs_to_jiffies(50));
305 spin_lock_irqsave(&host->lock, flags);
306
307 if (!host->tuning_done) {
308 dev_warn(mmc_dev(host->mmc),
309 ": Timeout for Buffer Read Ready interrupt, back to fixed sampling clock\n");
310 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
311 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
312 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
313 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
314
315 err = -EIO;
316 goto out;
317 }
318
319 host->tuning_done = 0;
320
321 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
322
323
324 if (opcode == MMC_SEND_TUNING_BLOCK)
325 mdelay(1);
326 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
327
328
329
330
331
332 if (tuning_loop_counter < 0) {
333 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
334 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
335 }
336 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
337 dev_warn(mmc_dev(host->mmc),
338 ": Tuning failed, back to fixed sampling clock\n");
339 err = -EIO;
340 } else {
341 arasan_zynqmp_dll_reset(host, sdhci_arasan->device_id);
342 }
343
344out:
345
346
347
348
349
350
351
352 if (tuning_count)
353 err = 0;
354
355 host->mmc->retune_period = err ? 0 : tuning_count;
356
357 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
358 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
359 spin_unlock_irqrestore(&host->lock, flags);
360
361 return err;
362}
363
364static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
365{
366 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
367 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
368 bool ctrl_phy = false;
369
370 if (!IS_ERR(sdhci_arasan->phy)) {
371 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
372
373
374
375
376
377
378
379
380
381
382
383
384
385 sdhci_set_clock(host, host->max_clk);
386 phy_power_on(sdhci_arasan->phy);
387 sdhci_arasan->is_phy_on = true;
388
389
390
391
392
393
394 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
395
396
397
398
399
400 ctrl_phy = true;
401 }
402 }
403
404 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_STANDARD_25_BROKEN) &&
405 (host->version >= SDHCI_SPEC_300)) {
406 if (clock == SD_CLK_25_MHZ)
407 clock = SD_CLK_19_MHZ;
408 if ((host->timing != MMC_TIMING_LEGACY) &&
409 (host->timing != MMC_TIMING_UHS_SDR12))
410 arasan_zynqmp_set_tap_delay(sdhci_arasan->device_id,
411 host->timing,
412 sdhci_arasan->mio_bank);
413 }
414
415 if (ctrl_phy && sdhci_arasan->is_phy_on) {
416 phy_power_off(sdhci_arasan->phy);
417 sdhci_arasan->is_phy_on = false;
418 }
419
420 sdhci_set_clock(host, clock);
421
422 if (ctrl_phy) {
423 phy_power_on(sdhci_arasan->phy);
424 sdhci_arasan->is_phy_on = true;
425 }
426}
427
428static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
429 struct mmc_ios *ios)
430{
431 u32 vendor;
432 struct sdhci_host *host = mmc_priv(mmc);
433
434 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
435 if (ios->enhanced_strobe)
436 vendor |= VENDOR_ENHANCED_STROBE;
437 else
438 vendor &= ~VENDOR_ENHANCED_STROBE;
439
440 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
441}
442
443static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
444{
445 u8 ctrl;
446 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
447 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
448
449 sdhci_reset(host, mask);
450
451 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
452 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
453 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
454 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
455 }
456}
457
458static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
459 struct mmc_ios *ios)
460{
461 switch (ios->signal_voltage) {
462 case MMC_SIGNAL_VOLTAGE_180:
463
464
465
466
467
468
469
470 return 0;
471 case MMC_SIGNAL_VOLTAGE_330:
472 case MMC_SIGNAL_VOLTAGE_120:
473
474 break;
475 }
476
477 return -EINVAL;
478}
479
480static struct sdhci_ops sdhci_arasan_ops = {
481 .set_clock = sdhci_arasan_set_clock,
482 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
483 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
484 .set_bus_width = sdhci_set_bus_width,
485 .reset = sdhci_arasan_reset,
486 .set_uhs_signaling = sdhci_set_uhs_signaling,
487};
488
489static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
490 .ops = &sdhci_arasan_ops,
491 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
492 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
493 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
494};
495
496#ifdef CONFIG_PM_SLEEP
497
498
499
500
501
502
503
504static int sdhci_arasan_suspend(struct device *dev)
505{
506 struct platform_device *pdev = to_platform_device(dev);
507 struct sdhci_host *host = platform_get_drvdata(pdev);
508 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
509 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
510 int ret;
511
512 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
513 mmc_retune_needed(host->mmc);
514
515 ret = sdhci_suspend_host(host);
516 if (ret)
517 return ret;
518
519 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
520 ret = phy_power_off(sdhci_arasan->phy);
521 if (ret) {
522 dev_err(dev, "Cannot power off phy.\n");
523 sdhci_resume_host(host);
524 return ret;
525 }
526 sdhci_arasan->is_phy_on = false;
527 }
528
529 clk_disable(pltfm_host->clk);
530 clk_disable(sdhci_arasan->clk_ahb);
531
532 return 0;
533}
534
535
536
537
538
539
540
541
542static int sdhci_arasan_resume(struct device *dev)
543{
544 struct platform_device *pdev = to_platform_device(dev);
545 struct sdhci_host *host = platform_get_drvdata(pdev);
546 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
547 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
548 int ret;
549
550 ret = clk_enable(sdhci_arasan->clk_ahb);
551 if (ret) {
552 dev_err(dev, "Cannot enable AHB clock.\n");
553 return ret;
554 }
555
556 ret = clk_enable(pltfm_host->clk);
557 if (ret) {
558 dev_err(dev, "Cannot enable SD clock.\n");
559 return ret;
560 }
561
562 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
563 ret = phy_power_on(sdhci_arasan->phy);
564 if (ret) {
565 dev_err(dev, "Cannot power on phy.\n");
566 return ret;
567 }
568 sdhci_arasan->is_phy_on = true;
569 }
570
571 return sdhci_resume_host(host);
572}
573#endif
574
575static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
576 sdhci_arasan_resume);
577
578static const struct of_device_id sdhci_arasan_of_match[] = {
579
580 {
581 .compatible = "rockchip,rk3399-sdhci-5.1",
582 .data = &rk3399_soc_ctl_map,
583 },
584
585
586 { .compatible = "arasan,sdhci-8.9a" },
587 { .compatible = "arasan,sdhci-5.1" },
588 { .compatible = "arasan,sdhci-4.9a" },
589 { .compatible = "xlnx,zynqmp-8.9a" },
590
591 { }
592};
593MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
594
595
596
597
598
599
600
601
602
603
604
605static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
606 unsigned long parent_rate)
607
608{
609 struct sdhci_arasan_data *sdhci_arasan =
610 container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
611 struct sdhci_host *host = sdhci_arasan->host;
612
613 return host->mmc->actual_clock;
614}
615
616static const struct clk_ops arasan_sdcardclk_ops = {
617 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
618};
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
638 u32 value)
639{
640 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
641 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
642 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
643 sdhci_arasan->soc_ctl_map;
644
645
646 if (!soc_ctl_map)
647 return;
648
649
650 if (!sdhci_arasan->soc_ctl_base) {
651 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
652 mmc_hostname(host->mmc));
653 return;
654 }
655
656 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
657}
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
677{
678 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
679 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
680 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
681 sdhci_arasan->soc_ctl_map;
682 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
683
684
685 if (!soc_ctl_map)
686 return;
687
688
689 if (!sdhci_arasan->soc_ctl_base) {
690 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
691 mmc_hostname(host->mmc));
692 return;
693 }
694
695 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
696}
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
719 struct clk *clk_xin,
720 struct device *dev)
721{
722 struct device_node *np = dev->of_node;
723 struct clk_init_data sdcardclk_init;
724 const char *parent_clk_name;
725 int ret;
726
727
728 if (!of_find_property(np, "#clock-cells", NULL))
729 return 0;
730
731 ret = of_property_read_string_index(np, "clock-output-names", 0,
732 &sdcardclk_init.name);
733 if (ret) {
734 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
735 return ret;
736 }
737
738 parent_clk_name = __clk_get_name(clk_xin);
739 sdcardclk_init.parent_names = &parent_clk_name;
740 sdcardclk_init.num_parents = 1;
741 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
742 sdcardclk_init.ops = &arasan_sdcardclk_ops;
743
744 sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
745 sdhci_arasan->sdcardclk =
746 devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
747 sdhci_arasan->sdcardclk_hw.init = NULL;
748
749 ret = of_clk_add_provider(np, of_clk_src_simple_get,
750 sdhci_arasan->sdcardclk);
751 if (ret)
752 dev_err(dev, "Failed to add clock provider\n");
753
754 return ret;
755}
756
757
758
759
760
761
762
763
764
765static void sdhci_arasan_unregister_sdclk(struct device *dev)
766{
767 struct device_node *np = dev->of_node;
768
769 if (!of_find_property(np, "#clock-cells", NULL))
770 return;
771
772 of_clk_del_provider(dev->of_node);
773}
774
775static int sdhci_arasan_probe(struct platform_device *pdev)
776{
777 int ret;
778 const struct of_device_id *match;
779 struct device_node *node;
780 struct clk *clk_xin;
781 struct sdhci_host *host;
782 struct sdhci_pltfm_host *pltfm_host;
783 struct sdhci_arasan_data *sdhci_arasan;
784 struct device_node *np = pdev->dev.of_node;
785 unsigned int host_quirks2 = 0;
786
787 if (of_device_is_compatible(pdev->dev.of_node, "xlnx,zynqmp-8.9a")) {
788 char *soc_rev;
789
790
791 soc_rev = zynqmp_nvmem_get_silicon_version(&pdev->dev,
792 "soc_revision");
793 if (PTR_ERR(soc_rev) == -EPROBE_DEFER)
794
795 return -EPROBE_DEFER;
796 else if (IS_ERR(soc_rev))
797 dev_dbg(&pdev->dev, "Error getting silicon version\n");
798
799
800 if (!IS_ERR(soc_rev) && (*soc_rev == ZYNQMP_SILICON_V1))
801 host_quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
802
803
804
805
806 if (!IS_ERR(soc_rev))
807 kfree(soc_rev);
808 }
809
810 host = sdhci_pltfm_init(pdev, &sdhci_arasan_pdata,
811 sizeof(*sdhci_arasan));
812 if (IS_ERR(host))
813 return PTR_ERR(host);
814
815 pltfm_host = sdhci_priv(host);
816 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
817 sdhci_arasan->host = host;
818
819 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
820 sdhci_arasan->soc_ctl_map = match->data;
821
822 host->quirks2 |= host_quirks2;
823
824 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
825 if (node) {
826 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
827 of_node_put(node);
828
829 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
830 ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
831 if (ret != -EPROBE_DEFER)
832 dev_err(&pdev->dev, "Can't get syscon: %d\n",
833 ret);
834 goto err_pltfm_free;
835 }
836 }
837
838 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
839 if (IS_ERR(sdhci_arasan->clk_ahb)) {
840 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
841 ret = PTR_ERR(sdhci_arasan->clk_ahb);
842 goto err_pltfm_free;
843 }
844
845 clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
846 if (IS_ERR(clk_xin)) {
847 dev_err(&pdev->dev, "clk_xin clock not found.\n");
848 ret = PTR_ERR(clk_xin);
849 goto err_pltfm_free;
850 }
851
852 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
853 if (ret) {
854 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
855 goto err_pltfm_free;
856 }
857
858 ret = clk_prepare_enable(clk_xin);
859 if (ret) {
860 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
861 goto clk_dis_ahb;
862 }
863
864 sdhci_get_of_property(pdev);
865
866 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
867 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
868
869 pltfm_host->clk = clk_xin;
870
871 if (of_device_is_compatible(pdev->dev.of_node,
872 "rockchip,rk3399-sdhci-5.1"))
873 sdhci_arasan_update_clockmultiplier(host, 0x0);
874
875 sdhci_arasan_update_baseclkfreq(host);
876
877 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
878 if (ret)
879 goto clk_disable_all;
880
881 ret = mmc_of_parse(host->mmc);
882 if (ret) {
883 dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
884 goto unreg_clk;
885 }
886
887 if (of_device_is_compatible(pdev->dev.of_node, "xlnx,zynqmp-8.9a") ||
888 of_device_is_compatible(pdev->dev.of_node,
889 "arasan,sdhci-8.9a")) {
890 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
891 host->quirks2 |= SDHCI_QUIRK2_CLOCK_STANDARD_25_BROKEN;
892 if (of_device_is_compatible(pdev->dev.of_node,
893 "xlnx,zynqmp-8.9a")) {
894 ret = of_property_read_u32(pdev->dev.of_node,
895 "xlnx,mio_bank",
896 &sdhci_arasan->mio_bank);
897 if (ret < 0) {
898 dev_err(&pdev->dev,
899 "\"xlnx,mio_bank \" property is missing.\n");
900 goto clk_disable_all;
901 }
902 ret = of_property_read_u32(pdev->dev.of_node,
903 "xlnx,device_id",
904 &sdhci_arasan->device_id);
905 if (ret < 0) {
906 dev_err(&pdev->dev,
907 "\"xlnx,device_id \" property is missing.\n");
908 goto clk_disable_all;
909 }
910 sdhci_arasan_ops.platform_execute_tuning =
911 arasan_zynqmp_execute_tuning;
912 }
913 }
914
915 sdhci_arasan->pinctrl = devm_pinctrl_get(&pdev->dev);
916 if (!IS_ERR(sdhci_arasan->pinctrl)) {
917 sdhci_arasan->pins_default = pinctrl_lookup_state(
918 sdhci_arasan->pinctrl,
919 PINCTRL_STATE_DEFAULT);
920 if (IS_ERR(sdhci_arasan->pins_default)) {
921 dev_err(&pdev->dev, "Missing default pinctrl config\n");
922 return IS_ERR(sdhci_arasan->pins_default);
923 }
924
925 pinctrl_select_state(sdhci_arasan->pinctrl,
926 sdhci_arasan->pins_default);
927 }
928
929 sdhci_arasan->phy = ERR_PTR(-ENODEV);
930 if (of_device_is_compatible(pdev->dev.of_node,
931 "arasan,sdhci-5.1")) {
932 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
933 "phy_arasan");
934 if (IS_ERR(sdhci_arasan->phy)) {
935 ret = PTR_ERR(sdhci_arasan->phy);
936 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
937 goto unreg_clk;
938 }
939
940 ret = phy_init(sdhci_arasan->phy);
941 if (ret < 0) {
942 dev_err(&pdev->dev, "phy_init err.\n");
943 goto unreg_clk;
944 }
945
946 host->mmc_host_ops.hs400_enhanced_strobe =
947 sdhci_arasan_hs400_enhanced_strobe;
948 host->mmc_host_ops.start_signal_voltage_switch =
949 sdhci_arasan_voltage_switch;
950 }
951
952 ret = sdhci_add_host(host);
953 if (ret)
954 goto err_add_host;
955
956 return 0;
957
958err_add_host:
959 if (!IS_ERR(sdhci_arasan->phy))
960 phy_exit(sdhci_arasan->phy);
961unreg_clk:
962 sdhci_arasan_unregister_sdclk(&pdev->dev);
963clk_disable_all:
964 clk_disable_unprepare(clk_xin);
965clk_dis_ahb:
966 clk_disable_unprepare(sdhci_arasan->clk_ahb);
967err_pltfm_free:
968 sdhci_pltfm_free(pdev);
969 return ret;
970}
971
972static int sdhci_arasan_remove(struct platform_device *pdev)
973{
974 int ret;
975 struct sdhci_host *host = platform_get_drvdata(pdev);
976 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
977 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
978 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
979
980 if (!IS_ERR(sdhci_arasan->phy)) {
981 if (sdhci_arasan->is_phy_on)
982 phy_power_off(sdhci_arasan->phy);
983 phy_exit(sdhci_arasan->phy);
984 }
985
986 sdhci_arasan_unregister_sdclk(&pdev->dev);
987
988 ret = sdhci_pltfm_unregister(pdev);
989
990 clk_disable_unprepare(clk_ahb);
991
992 return ret;
993}
994
995static struct platform_driver sdhci_arasan_driver = {
996 .driver = {
997 .name = "sdhci-arasan",
998 .of_match_table = sdhci_arasan_of_match,
999 .pm = &sdhci_arasan_dev_pm_ops,
1000 },
1001 .probe = sdhci_arasan_probe,
1002 .remove = sdhci_arasan_remove,
1003};
1004
1005module_platform_driver(sdhci_arasan_driver);
1006
1007MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1008MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1009MODULE_LICENSE("GPL");
1010