blob: 47ab5654bd64af21d9b95a5a6d4c8b75aa2ee367 [file] [log] [blame]
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Synopsys DesignWare Multimedia Card Interface driver
4 * extensions used in various Synopsys ARC devboards.
5 *
6 * Copyright (C) 2019 Synopsys
7 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
8 */
9
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030010#include <clk.h>
11#include <dm.h>
12#include <dwmmc.h>
13#include <errno.h>
14#include <fdtdec.h>
Heinrich Schuchardt40006bf2024-07-21 08:59:07 +020015#include <asm/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030017#include <linux/libfdt.h>
18#include <linux/err.h>
19#include <malloc.h>
20
21#define CLOCK_MIN 400000 /* 400 kHz */
22#define FIFO_MIN 8
23#define FIFO_MAX 4096
24
25struct snps_dwmci_plat {
26 struct mmc_config cfg;
27 struct mmc mmc;
28};
29
30struct snps_dwmci_priv_data {
31 struct dwmci_host host;
32 u32 f_max;
Heinrich Schuchardt40006bf2024-07-21 08:59:07 +020033 struct gpio_desc cd_gpio;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030034};
35
36static int snps_dwmmc_clk_setup(struct udevice *dev)
37{
38 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
39 struct dwmci_host *host = &priv->host;
40
41 struct clk clk_ciu, clk_biu;
42 int ret;
43
44 ret = clk_get_by_name(dev, "ciu", &clk_ciu);
45 if (ret)
46 goto clk_err;
47
48 ret = clk_enable(&clk_ciu);
49 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
Sean Andersond318eb32023-12-16 14:38:42 -050050 goto clk_err;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030051
52 host->bus_hz = clk_get_rate(&clk_ciu);
53 if (host->bus_hz < CLOCK_MIN) {
54 ret = -EINVAL;
55 goto clk_err_ciu_dis;
56 }
57
58 ret = clk_get_by_name(dev, "biu", &clk_biu);
59 if (ret)
60 goto clk_err_ciu_dis;
61
62 ret = clk_enable(&clk_biu);
63 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
Sean Andersond318eb32023-12-16 14:38:42 -050064 goto clk_err_ciu_dis;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030065
66 return 0;
67
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030068clk_err_ciu_dis:
69 clk_disable(&clk_ciu);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030070clk_err:
71 dev_err(dev, "failed to setup clocks, ret %d\n", ret);
72
73 return ret;
74}
75
Simon Glassaad29ae2020-12-03 16:55:21 -070076static int snps_dwmmc_of_to_plat(struct udevice *dev)
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030077{
78 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
79 struct dwmci_host *host = &priv->host;
80 u32 fifo_depth;
81 int ret;
82
Masahiro Yamada32822d02020-08-04 14:14:43 +090083 host->ioaddr = dev_read_addr_ptr(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030084
85 /*
Sam Protsenko751fdf12024-08-07 22:14:17 -050086 * If fifo-depth is unset don't set fifo_depth - we will try to
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030087 * auto detect it.
88 */
89 ret = dev_read_u32(dev, "fifo-depth", &fifo_depth);
90 if (!ret) {
91 if (fifo_depth < FIFO_MIN || fifo_depth > FIFO_MAX)
92 return -EINVAL;
93
Sam Protsenko751fdf12024-08-07 22:14:17 -050094 host->fifo_depth = fifo_depth;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030095 }
96
97 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
98 if (host->buswidth != 1 && host->buswidth != 4 && host->buswidth != 8)
99 return -EINVAL;
100
101 /*
102 * If max-frequency is unset don't set priv->f_max - we will use
103 * host->bus_hz in probe() instead.
104 */
105 ret = dev_read_u32(dev, "max-frequency", &priv->f_max);
106 if (!ret && priv->f_max < CLOCK_MIN)
107 return -EINVAL;
108
Heinrich Schuchardt40006bf2024-07-21 08:59:07 +0200109 if (CONFIG_IS_ENABLED(DM_GPIO))
110 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
111 GPIOD_IS_IN);
112
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300113 host->fifo_mode = dev_read_bool(dev, "fifo-mode");
114 host->name = dev->name;
115 host->dev_index = 0;
116 host->priv = priv;
117
118 return 0;
119}
120
121int snps_dwmmc_getcd(struct udevice *dev)
122{
123 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
124 struct dwmci_host *host = &priv->host;
125
Heinrich Schuchardt40006bf2024-07-21 08:59:07 +0200126 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&priv->cd_gpio))
127 return dm_gpio_get_value(&priv->cd_gpio);
128
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300129 return !(dwmci_readl(host, DWMCI_CDETECT) & 1);
130}
131
132struct dm_mmc_ops snps_dwmci_dm_ops;
133
134static int snps_dwmmc_probe(struct udevice *dev)
135{
136#ifdef CONFIG_BLK
Simon Glassfa20e932020-12-03 16:55:20 -0700137 struct snps_dwmci_plat *plat = dev_get_plat(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300138#endif
139 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
140 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
141 struct dwmci_host *host = &priv->host;
142 unsigned int clock_max;
143 int ret;
144
145 /* Extend generic 'dm_dwmci_ops' with our 'getcd' implementation */
146 memcpy(&snps_dwmci_dm_ops, &dm_dwmci_ops, sizeof(struct dm_mmc_ops));
147 snps_dwmci_dm_ops.get_cd = snps_dwmmc_getcd;
148
149 ret = snps_dwmmc_clk_setup(dev);
150 if (ret)
151 return ret;
152
153 if (!priv->f_max)
154 clock_max = host->bus_hz;
155 else
156 clock_max = min_t(unsigned int, host->bus_hz, priv->f_max);
157
158#ifdef CONFIG_BLK
159 dwmci_setup_cfg(&plat->cfg, host, clock_max, CLOCK_MIN);
160 host->mmc = &plat->mmc;
161#else
162 ret = add_dwmci(host, clock_max, CLOCK_MIN);
163 if (ret)
164 return ret;
165#endif
166 host->mmc->priv = &priv->host;
167 upriv->mmc = host->mmc;
168 host->mmc->dev = dev;
169
170 return dwmci_probe(dev);
171}
172
173static int snps_dwmmc_bind(struct udevice *dev)
174{
175#ifdef CONFIG_BLK
Simon Glassfa20e932020-12-03 16:55:20 -0700176 struct snps_dwmci_plat *plat = dev_get_plat(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300177 int ret;
178
179 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
180 if (ret)
181 return ret;
182#endif
183
184 return 0;
185}
186
187static const struct udevice_id snps_dwmmc_ids[] = {
188 { .compatible = "snps,dw-mshc" },
189 { }
190};
191
192U_BOOT_DRIVER(snps_dwmmc_drv) = {
193 .name = "snps_dw_mmc",
194 .id = UCLASS_MMC,
195 .of_match = snps_dwmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700196 .of_to_plat = snps_dwmmc_of_to_plat,
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300197 .ops = &snps_dwmci_dm_ops,
198 .bind = snps_dwmmc_bind,
199 .probe = snps_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700200 .priv_auto = sizeof(struct snps_dwmci_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700201 .plat_auto = sizeof(struct snps_dwmci_plat),
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300202};