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