]> Git Repo - J-u-boot.git/blob - drivers/mmc/socfpga_dw_mmc.c
Merge tag 'xilinx-for-v2024.07-rc1' of https://source.denx.de/u-boot/custodians/u...
[J-u-boot.git] / drivers / mmc / socfpga_dw_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2013 Altera Corporation <www.altera.com>
4  */
5
6 #include <common.h>
7 #include <log.h>
8 #include <asm/arch/clock_manager.h>
9 #include <asm/arch/secure_reg_helper.h>
10 #include <asm/arch/system_manager.h>
11 #include <clk.h>
12 #include <dm.h>
13 #include <dwmmc.h>
14 #include <errno.h>
15 #include <fdtdec.h>
16 #include <asm/global_data.h>
17 #include <dm/device_compat.h>
18 #include <linux/intel-smc.h>
19 #include <linux/libfdt.h>
20 #include <linux/err.h>
21 #include <malloc.h>
22 #include <reset.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 struct socfpga_dwmci_plat {
27         struct mmc_config cfg;
28         struct mmc mmc;
29 };
30
31 /* socfpga implmentation specific driver private data */
32 struct dwmci_socfpga_priv_data {
33         struct dwmci_host       host;
34         unsigned int            drvsel;
35         unsigned int            smplsel;
36 };
37
38 static void socfpga_dwmci_reset(struct udevice *dev)
39 {
40         struct reset_ctl_bulk reset_bulk;
41         int ret;
42
43         ret = reset_get_bulk(dev, &reset_bulk);
44         if (ret) {
45                 dev_warn(dev, "Can't get reset: %d\n", ret);
46                 return;
47         }
48
49         reset_deassert_bulk(&reset_bulk);
50 }
51
52 static int socfpga_dwmci_clksel(struct dwmci_host *host)
53 {
54         struct dwmci_socfpga_priv_data *priv = host->priv;
55         u32 sdmmc_mask = ((priv->smplsel & 0x7) << SYSMGR_SDMMC_SMPLSEL_SHIFT) |
56                          ((priv->drvsel & 0x7) << SYSMGR_SDMMC_DRVSEL_SHIFT);
57
58         /* Disable SDMMC clock. */
59         clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_PERPLL_EN,
60                      CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
61
62         debug("%s: drvsel %d smplsel %d\n", __func__,
63               priv->drvsel, priv->smplsel);
64
65 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_ATF)
66         int ret;
67
68         ret = socfpga_secure_reg_write32(SOCFPGA_SECURE_REG_SYSMGR_SOC64_SDMMC,
69                                          sdmmc_mask);
70         if (ret) {
71                 printf("DWMMC: Failed to set clksel via SMC call");
72                 return ret;
73         }
74 #else
75         writel(sdmmc_mask, socfpga_get_sysmgr_addr() + SYSMGR_SDMMC);
76
77         debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__,
78                 readl(socfpga_get_sysmgr_addr() + SYSMGR_SDMMC));
79 #endif
80
81         /* Enable SDMMC clock */
82         setbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_PERPLL_EN,
83                      CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
84
85         return 0;
86 }
87
88 static int socfpga_dwmmc_get_clk_rate(struct udevice *dev)
89 {
90         struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
91         struct dwmci_host *host = &priv->host;
92 #if CONFIG_IS_ENABLED(CLK)
93         struct clk clk;
94         int ret;
95
96         ret = clk_get_by_index(dev, 1, &clk);
97         if (ret)
98                 return ret;
99
100         host->bus_hz = clk_get_rate(&clk);
101
102 #else
103         /* Fixed clock divide by 4 which due to the SDMMC wrapper */
104         host->bus_hz = cm_get_mmc_controller_clk_hz();
105 #endif
106         if (host->bus_hz == 0) {
107                 printf("DWMMC: MMC clock is zero!");
108                 return -EINVAL;
109         }
110
111         return 0;
112 }
113
114 static int socfpga_dwmmc_of_to_plat(struct udevice *dev)
115 {
116         struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
117         struct dwmci_host *host = &priv->host;
118         int fifo_depth;
119
120         fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
121                                     "fifo-depth", 0);
122         if (fifo_depth < 0) {
123                 printf("DWMMC: Can't get FIFO depth\n");
124                 return -EINVAL;
125         }
126
127         host->name = dev->name;
128         host->ioaddr = dev_read_addr_ptr(dev);
129         host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
130                                         "bus-width", 4);
131         host->clksel = socfpga_dwmci_clksel;
132
133         /*
134          * TODO([email protected]): Remove the need for this hack.
135          * We only have one dwmmc block on gen5 SoCFPGA.
136          */
137         host->dev_index = 0;
138         host->fifoth_val = MSIZE(0x2) |
139                 RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
140         priv->drvsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
141                                        "drvsel", 3);
142         priv->smplsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
143                                         "smplsel", 0);
144         host->priv = priv;
145
146         host->fifo_mode = dev_read_bool(dev, "fifo-mode");
147
148         return 0;
149 }
150
151 static int socfpga_dwmmc_probe(struct udevice *dev)
152 {
153 #ifdef CONFIG_BLK
154         struct socfpga_dwmci_plat *plat = dev_get_plat(dev);
155 #endif
156         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
157         struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
158         struct dwmci_host *host = &priv->host;
159         int ret;
160
161         ret = socfpga_dwmmc_get_clk_rate(dev);
162         if (ret)
163                 return ret;
164
165         socfpga_dwmci_reset(dev);
166
167 #ifdef CONFIG_BLK
168         dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000);
169         host->mmc = &plat->mmc;
170 #else
171
172         ret = add_dwmci(host, host->bus_hz, 400000);
173         if (ret)
174                 return ret;
175 #endif
176         host->mmc->priv = &priv->host;
177         upriv->mmc = host->mmc;
178         host->mmc->dev = dev;
179
180         return dwmci_probe(dev);
181 }
182
183 static int socfpga_dwmmc_bind(struct udevice *dev)
184 {
185 #ifdef CONFIG_BLK
186         struct socfpga_dwmci_plat *plat = dev_get_plat(dev);
187         int ret;
188
189         ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
190         if (ret)
191                 return ret;
192 #endif
193
194         return 0;
195 }
196
197 static const struct udevice_id socfpga_dwmmc_ids[] = {
198         { .compatible = "altr,socfpga-dw-mshc" },
199         { }
200 };
201
202 U_BOOT_DRIVER(socfpga_dwmmc_drv) = {
203         .name           = "socfpga_dwmmc",
204         .id             = UCLASS_MMC,
205         .of_match       = socfpga_dwmmc_ids,
206         .of_to_plat = socfpga_dwmmc_of_to_plat,
207         .ops            = &dm_dwmci_ops,
208         .bind           = socfpga_dwmmc_bind,
209         .probe          = socfpga_dwmmc_probe,
210         .priv_auto      = sizeof(struct dwmci_socfpga_priv_data),
211         .plat_auto      = sizeof(struct socfpga_dwmci_plat),
212 };
This page took 0.038974 seconds and 4 git commands to generate.