blob: 9bdbe5070b1dde414de521591e51360af8bd526f [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>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030016#include <linux/libfdt.h>
17#include <linux/err.h>
18#include <malloc.h>
19
20#define CLOCK_MIN 400000 /* 400 kHz */
21#define FIFO_MIN 8
22#define FIFO_MAX 4096
23
24struct snps_dwmci_plat {
25 struct mmc_config cfg;
26 struct mmc mmc;
27};
28
29struct snps_dwmci_priv_data {
30 struct dwmci_host host;
31 u32 f_max;
32};
33
34static int snps_dwmmc_clk_setup(struct udevice *dev)
35{
36 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
37 struct dwmci_host *host = &priv->host;
38
39 struct clk clk_ciu, clk_biu;
40 int ret;
41
42 ret = clk_get_by_name(dev, "ciu", &clk_ciu);
43 if (ret)
44 goto clk_err;
45
46 ret = clk_enable(&clk_ciu);
47 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
Sean Andersond318eb32023-12-16 14:38:42 -050048 goto clk_err;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030049
50 host->bus_hz = clk_get_rate(&clk_ciu);
51 if (host->bus_hz < CLOCK_MIN) {
52 ret = -EINVAL;
53 goto clk_err_ciu_dis;
54 }
55
56 ret = clk_get_by_name(dev, "biu", &clk_biu);
57 if (ret)
58 goto clk_err_ciu_dis;
59
60 ret = clk_enable(&clk_biu);
61 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
Sean Andersond318eb32023-12-16 14:38:42 -050062 goto clk_err_ciu_dis;
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030063
64 return 0;
65
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030066clk_err_ciu_dis:
67 clk_disable(&clk_ciu);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030068clk_err:
69 dev_err(dev, "failed to setup clocks, ret %d\n", ret);
70
71 return ret;
72}
73
Simon Glassaad29ae2020-12-03 16:55:21 -070074static int snps_dwmmc_of_to_plat(struct udevice *dev)
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030075{
76 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
77 struct dwmci_host *host = &priv->host;
78 u32 fifo_depth;
79 int ret;
80
Masahiro Yamada32822d02020-08-04 14:14:43 +090081 host->ioaddr = dev_read_addr_ptr(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +030082
83 /*
84 * If fifo-depth is unset don't set fifoth_val - we will try to
85 * auto detect it.
86 */
87 ret = dev_read_u32(dev, "fifo-depth", &fifo_depth);
88 if (!ret) {
89 if (fifo_depth < FIFO_MIN || fifo_depth > FIFO_MAX)
90 return -EINVAL;
91
92 host->fifoth_val = MSIZE(0x2) |
93 RX_WMARK(fifo_depth / 2 - 1) |
94 TX_WMARK(fifo_depth / 2);
95 }
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
109 host->fifo_mode = dev_read_bool(dev, "fifo-mode");
110 host->name = dev->name;
111 host->dev_index = 0;
112 host->priv = priv;
113
114 return 0;
115}
116
117int snps_dwmmc_getcd(struct udevice *dev)
118{
119 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
120 struct dwmci_host *host = &priv->host;
121
122 return !(dwmci_readl(host, DWMCI_CDETECT) & 1);
123}
124
125struct dm_mmc_ops snps_dwmci_dm_ops;
126
127static int snps_dwmmc_probe(struct udevice *dev)
128{
129#ifdef CONFIG_BLK
Simon Glassfa20e932020-12-03 16:55:20 -0700130 struct snps_dwmci_plat *plat = dev_get_plat(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300131#endif
132 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
133 struct snps_dwmci_priv_data *priv = dev_get_priv(dev);
134 struct dwmci_host *host = &priv->host;
135 unsigned int clock_max;
136 int ret;
137
138 /* Extend generic 'dm_dwmci_ops' with our 'getcd' implementation */
139 memcpy(&snps_dwmci_dm_ops, &dm_dwmci_ops, sizeof(struct dm_mmc_ops));
140 snps_dwmci_dm_ops.get_cd = snps_dwmmc_getcd;
141
142 ret = snps_dwmmc_clk_setup(dev);
143 if (ret)
144 return ret;
145
146 if (!priv->f_max)
147 clock_max = host->bus_hz;
148 else
149 clock_max = min_t(unsigned int, host->bus_hz, priv->f_max);
150
151#ifdef CONFIG_BLK
152 dwmci_setup_cfg(&plat->cfg, host, clock_max, CLOCK_MIN);
153 host->mmc = &plat->mmc;
154#else
155 ret = add_dwmci(host, clock_max, CLOCK_MIN);
156 if (ret)
157 return ret;
158#endif
159 host->mmc->priv = &priv->host;
160 upriv->mmc = host->mmc;
161 host->mmc->dev = dev;
162
163 return dwmci_probe(dev);
164}
165
166static int snps_dwmmc_bind(struct udevice *dev)
167{
168#ifdef CONFIG_BLK
Simon Glassfa20e932020-12-03 16:55:20 -0700169 struct snps_dwmci_plat *plat = dev_get_plat(dev);
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300170 int ret;
171
172 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
173 if (ret)
174 return ret;
175#endif
176
177 return 0;
178}
179
180static const struct udevice_id snps_dwmmc_ids[] = {
181 { .compatible = "snps,dw-mshc" },
182 { }
183};
184
185U_BOOT_DRIVER(snps_dwmmc_drv) = {
186 .name = "snps_dw_mmc",
187 .id = UCLASS_MMC,
188 .of_match = snps_dwmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700189 .of_to_plat = snps_dwmmc_of_to_plat,
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300190 .ops = &snps_dwmci_dm_ops,
191 .bind = snps_dwmmc_bind,
192 .probe = snps_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700193 .priv_auto = sizeof(struct snps_dwmci_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700194 .plat_auto = sizeof(struct snps_dwmci_plat),
Eugeniy Paltsevb050c2c2019-02-25 18:35:28 +0300195};