1 // SPDX-License-Identifier: GPL-2.0+
3 * Synopsys DesignWare Multimedia Card Interface driver
4 * extensions used in various Synopsys ARC devboards.
6 * Copyright (C) 2019 Synopsys
7 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
16 #include <linux/libfdt.h>
17 #include <linux/err.h>
20 #define CLOCK_MIN 400000 /* 400 kHz */
24 struct snps_dwmci_plat {
25 struct mmc_config cfg;
29 struct snps_dwmci_priv_data {
30 struct dwmci_host host;
34 static int snps_dwmmc_clk_setup(struct udevice *dev)
36 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
37 struct dwmci_host *host = &priv->host;
39 struct clk clk_ciu, clk_biu;
42 ret = clk_get_by_name(dev, "ciu", &clk_ciu);
46 ret = clk_enable(&clk_ciu);
47 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
50 host->bus_hz = clk_get_rate(&clk_ciu);
51 if (host->bus_hz < CLOCK_MIN) {
56 ret = clk_get_by_name(dev, "biu", &clk_biu);
60 ret = clk_enable(&clk_biu);
61 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
69 clk_disable(&clk_ciu);
73 dev_err(dev, "failed to setup clocks, ret %d\n", ret);
78 static int snps_dwmmc_ofdata_to_platdata(struct udevice *dev)
80 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
81 struct dwmci_host *host = &priv->host;
85 host->ioaddr = devfdt_get_addr_ptr(dev);
88 * If fifo-depth is unset don't set fifoth_val - we will try to
91 ret = dev_read_u32(dev, "fifo-depth", &fifo_depth);
93 if (fifo_depth < FIFO_MIN || fifo_depth > FIFO_MAX)
96 host->fifoth_val = MSIZE(0x2) |
97 RX_WMARK(fifo_depth / 2 - 1) |
98 TX_WMARK(fifo_depth / 2);
101 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
102 if (host->buswidth != 1 && host->buswidth != 4 && host->buswidth != 8)
106 * If max-frequency is unset don't set priv->f_max - we will use
107 * host->bus_hz in probe() instead.
109 ret = dev_read_u32(dev, "max-frequency", &priv->f_max);
110 if (!ret && priv->f_max < CLOCK_MIN)
113 host->fifo_mode = dev_read_bool(dev, "fifo-mode");
114 host->name = dev->name;
121 int snps_dwmmc_getcd(struct udevice *dev)
123 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
124 struct dwmci_host *host = &priv->host;
126 return !(dwmci_readl(host, DWMCI_CDETECT) & 1);
129 struct dm_mmc_ops snps_dwmci_dm_ops;
131 static int snps_dwmmc_probe(struct udevice *dev)
134 struct snps_dwmci_plat *plat = dev_get_platdata(dev);
136 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
137 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
138 struct dwmci_host *host = &priv->host;
139 unsigned int clock_max;
142 /* Extend generic 'dm_dwmci_ops' with our 'getcd' implementation */
143 memcpy(&snps_dwmci_dm_ops, &dm_dwmci_ops, sizeof(struct dm_mmc_ops));
144 snps_dwmci_dm_ops.get_cd = snps_dwmmc_getcd;
146 ret = snps_dwmmc_clk_setup(dev);
151 clock_max = host->bus_hz;
153 clock_max = min_t(unsigned int, host->bus_hz, priv->f_max);
156 dwmci_setup_cfg(&plat->cfg, host, clock_max, CLOCK_MIN);
157 host->mmc = &plat->mmc;
159 ret = add_dwmci(host, clock_max, CLOCK_MIN);
163 host->mmc->priv = &priv->host;
164 upriv->mmc = host->mmc;
165 host->mmc->dev = dev;
167 return dwmci_probe(dev);
170 static int snps_dwmmc_bind(struct udevice *dev)
173 struct snps_dwmci_plat *plat = dev_get_platdata(dev);
176 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
184 static const struct udevice_id snps_dwmmc_ids[] = {
185 { .compatible = "snps,dw-mshc" },
189 U_BOOT_DRIVER(snps_dwmmc_drv) = {
190 .name = "snps_dw_mmc",
192 .of_match = snps_dwmmc_ids,
193 .ofdata_to_platdata = snps_dwmmc_ofdata_to_platdata,
194 .ops = &snps_dwmci_dm_ops,
195 .bind = snps_dwmmc_bind,
196 .probe = snps_dwmmc_probe,
197 .priv_auto_alloc_size = sizeof(struct snps_dwmci_priv_data),
198 .platdata_auto_alloc_size = sizeof(struct snps_dwmci_plat),