blob: 4e75051c3171b268f79c9e24e413bc767f2be234 [file] [log] [blame]
Kongyang Liueb36f282024-03-10 01:51:55 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2024, Kongyang Liu <seashell11234455@gmail.com>
4 */
5
6#include <dm.h>
7#include <mmc.h>
8#include <sdhci.h>
9#include <linux/delay.h>
10
11#define SDHCI_PHY_TX_RX_DLY 0x240
12#define MMC_MAX_CLOCK 375000000
13#define TUNE_MAX_PHCODE 128
14
Kongyang Liu085b3b22024-04-16 15:31:05 +080015#define PHY_TX_SRC_INVERT BIT(8)
16
Kongyang Liueb36f282024-03-10 01:51:55 +080017struct cv1800b_sdhci_plat {
18 struct mmc_config cfg;
19 struct mmc mmc;
20};
21
22static void cv1800b_set_tap_delay(struct sdhci_host *host, u16 tap)
23{
Kongyang Liu085b3b22024-04-16 15:31:05 +080024 sdhci_writel(host, PHY_TX_SRC_INVERT | tap << 16, SDHCI_PHY_TX_RX_DLY);
Kongyang Liueb36f282024-03-10 01:51:55 +080025}
26
27static void cv1800b_sdhci_reset(struct sdhci_host *host, u8 mask)
28{
29 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
30 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)
31 udelay(10);
32}
33
34static int cv1800b_execute_tuning(struct mmc *mmc, u8 opcode)
35{
36 struct sdhci_host *host = dev_get_priv(mmc->dev);
37
38 u16 tap;
39
40 int current_size = 0;
41 int max_size = 0;
42 int max_window = 0;
43
44 for (tap = 0; tap < TUNE_MAX_PHCODE; tap++) {
45 cv1800b_set_tap_delay(host, tap);
46
Jaehoon Chung3657ef72024-04-15 16:56:50 +090047 if (mmc_send_tuning(host->mmc, opcode)) {
Kongyang Liueb36f282024-03-10 01:51:55 +080048 current_size = 0;
49 } else {
50 current_size++;
51 if (current_size > max_size) {
52 max_size = current_size;
53 max_window = tap;
54 }
55 }
56 }
57
58 cv1800b_sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
59
60 cv1800b_set_tap_delay(host, max_window - max_size / 2);
61
62 return 0;
63}
64
65const struct sdhci_ops cv1800b_sdhci_sd_ops = {
66 .platform_execute_tuning = cv1800b_execute_tuning,
67};
68
69static int cv1800b_sdhci_bind(struct udevice *dev)
70{
71 struct cv1800b_sdhci_plat *plat = dev_get_plat(dev);
72
73 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
74}
75
76static int cv1800b_sdhci_probe(struct udevice *dev)
77{
78 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
79 struct cv1800b_sdhci_plat *plat = dev_get_plat(dev);
80 struct sdhci_host *host = dev_get_priv(dev);
81 int ret;
82
83 host->name = dev->name;
84 host->ioaddr = devfdt_get_addr_ptr(dev);
85
86 upriv->mmc = &plat->mmc;
87 host->mmc = &plat->mmc;
88 host->mmc->priv = host;
89 host->mmc->dev = dev;
90 host->ops = &cv1800b_sdhci_sd_ops;
91 host->max_clk = MMC_MAX_CLOCK;
92
93 ret = mmc_of_parse(dev, &plat->cfg);
94 if (ret)
95 return ret;
96
97 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 200000);
98 if (ret)
99 return ret;
100
101 return sdhci_probe(dev);
102}
103
104static const struct udevice_id cv1800b_sdhci_match[] = {
105 { .compatible = "sophgo,cv1800b-dwcmshc" },
106 { }
107};
108
109U_BOOT_DRIVER(cv1800b_sdhci) = {
110 .name = "sdhci-cv1800b",
111 .id = UCLASS_MMC,
112 .of_match = cv1800b_sdhci_match,
113 .bind = cv1800b_sdhci_bind,
114 .probe = cv1800b_sdhci_probe,
115 .priv_auto = sizeof(struct sdhci_host),
116 .plat_auto = sizeof(struct cv1800b_sdhci_plat),
117 .ops = &sdhci_ops,
118};