]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/mmc/rockchip_dw_mmc.c
Merge tag 'video-for-2019.07-rc1' of git://git.denx.de/u-boot-video
[thirdparty/u-boot.git] / drivers / mmc / rockchip_dw_mmc.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
a8cb4fb5
SG
2/*
3 * Copyright (c) 2013 Google, Inc
a8cb4fb5
SG
4 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
bfeb443e 9#include <dt-structs.h>
a8cb4fb5
SG
10#include <dwmmc.h>
11#include <errno.h>
bfeb443e 12#include <mapmem.h>
e1efec4e 13#include <pwrseq.h>
a8cb4fb5 14#include <syscon.h>
e1efec4e 15#include <asm/gpio.h>
a8cb4fb5
SG
16#include <asm/arch/clock.h>
17#include <asm/arch/periph.h>
18#include <linux/err.h>
19
f6e41d17 20struct rockchip_mmc_plat {
bfeb443e
SG
21#if CONFIG_IS_ENABLED(OF_PLATDATA)
22 struct dtd_rockchip_rk3288_dw_mshc dtplat;
23#endif
f6e41d17
SG
24 struct mmc_config cfg;
25 struct mmc mmc;
26};
27
a8cb4fb5 28struct rockchip_dwmmc_priv {
135aa950 29 struct clk clk;
a8cb4fb5 30 struct dwmci_host host;
6809b04f
SG
31 int fifo_depth;
32 bool fifo_mode;
33 u32 minmax[2];
a8cb4fb5
SG
34};
35
36static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
37{
38 struct udevice *dev = host->priv;
39 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
40 int ret;
41
135aa950 42 ret = clk_set_rate(&priv->clk, freq);
a8cb4fb5 43 if (ret < 0) {
419b0801 44 debug("%s: err=%d\n", __func__, ret);
a8cb4fb5
SG
45 return ret;
46 }
47
48 return freq;
49}
50
51static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
52{
bfeb443e 53#if !CONFIG_IS_ENABLED(OF_PLATDATA)
a8cb4fb5
SG
54 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
55 struct dwmci_host *host = &priv->host;
56
57 host->name = dev->name;
be5f04e8 58 host->ioaddr = dev_read_addr_ptr(dev);
fd1bf8df 59 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
a8cb4fb5
SG
60 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
61 host->priv = dev;
62
ace2198b 63 /* use non-removeable as sdcard and emmc as judgement */
fd1bf8df 64 if (dev_read_bool(dev, "non-removable"))
6579385b 65 host->dev_index = 0;
66 else
ace2198b 67 host->dev_index = 1;
a8cb4fb5 68
fd1bf8df
PT
69 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
70
6809b04f
SG
71 if (priv->fifo_depth < 0)
72 return -EINVAL;
fd1bf8df 73 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
ff71f9ac
PT
74
75 /*
76 * 'clock-freq-min-max' is deprecated
77 * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
78 */
fd1bf8df
PT
79 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
80 int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
ff71f9ac
PT
81
82 if (val < 0)
83 return val;
84
85 priv->minmax[0] = 400000; /* 400 kHz */
86 priv->minmax[1] = val;
87 } else {
88 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
89 __func__);
90 }
bfeb443e 91#endif
a8cb4fb5
SG
92 return 0;
93}
94
95static int rockchip_dwmmc_probe(struct udevice *dev)
96{
f6e41d17 97 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
a8cb4fb5
SG
98 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
99 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
100 struct dwmci_host *host = &priv->host;
e1efec4e 101 struct udevice *pwr_dev __maybe_unused;
a8cb4fb5
SG
102 int ret;
103
bfeb443e
SG
104#if CONFIG_IS_ENABLED(OF_PLATDATA)
105 struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
106
107 host->name = dev->name;
108 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
109 host->buswidth = dtplat->bus_width;
110 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
111 host->priv = dev;
112 host->dev_index = 0;
113 priv->fifo_depth = dtplat->fifo_depth;
114 priv->fifo_mode = 0;
80935298
KY
115 priv->minmax[0] = 400000; /* 400 kHz */
116 priv->minmax[1] = dtplat->max_frequency;
bfeb443e
SG
117
118 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
119 if (ret < 0)
120 return ret;
121#else
419b0801 122 ret = clk_get_by_index(dev, 0, &priv->clk);
898d6439 123 if (ret < 0)
a8cb4fb5 124 return ret;
bfeb443e 125#endif
28637248 126 host->fifoth_val = MSIZE(0x2) |
6809b04f
SG
127 RX_WMARK(priv->fifo_depth / 2 - 1) |
128 TX_WMARK(priv->fifo_depth / 2);
28637248 129
6809b04f 130 host->fifo_mode = priv->fifo_mode;
28637248 131
e1efec4e
SG
132#ifdef CONFIG_PWRSEQ
133 /* Enable power if needed */
134 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
135 &pwr_dev);
136 if (!ret) {
137 ret = pwrseq_set_power(pwr_dev, true);
138 if (ret)
139 return ret;
140 }
141#endif
e5113c33 142 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
f6e41d17 143 host->mmc = &plat->mmc;
f6e41d17 144 host->mmc->priv = &priv->host;
cffe5d86 145 host->mmc->dev = dev;
a8cb4fb5
SG
146 upriv->mmc = host->mmc;
147
42b37d8d 148 return dwmci_probe(dev);
a8cb4fb5
SG
149}
150
f6e41d17
SG
151static int rockchip_dwmmc_bind(struct udevice *dev)
152{
f6e41d17 153 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
f6e41d17 154
24f5aec3 155 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
f6e41d17
SG
156}
157
a8cb4fb5 158static const struct udevice_id rockchip_dwmmc_ids[] = {
26a52f34 159 { .compatible = "rockchip,rk2928-dw-mshc" },
a8cb4fb5
SG
160 { .compatible = "rockchip,rk3288-dw-mshc" },
161 { }
162};
163
164U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
bfeb443e 165 .name = "rockchip_rk3288_dw_mshc",
a8cb4fb5
SG
166 .id = UCLASS_MMC,
167 .of_match = rockchip_dwmmc_ids,
168 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
42b37d8d 169 .ops = &dm_dwmci_ops,
f6e41d17 170 .bind = rockchip_dwmmc_bind,
a8cb4fb5
SG
171 .probe = rockchip_dwmmc_probe,
172 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
f6e41d17 173 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
a8cb4fb5 174};
e1efec4e
SG
175
176#ifdef CONFIG_PWRSEQ
177static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
178{
179 struct gpio_desc reset;
180 int ret;
181
182 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
183 if (ret)
184 return ret;
185 dm_gpio_set_value(&reset, 1);
186 udelay(1);
187 dm_gpio_set_value(&reset, 0);
188 udelay(200);
189
190 return 0;
191}
192
193static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
194 .set_power = rockchip_dwmmc_pwrseq_set_power,
195};
196
197static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
198 { .compatible = "mmc-pwrseq-emmc" },
199 { }
200};
201
202U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
203 .name = "mmc_pwrseq_emmc",
204 .id = UCLASS_PWRSEQ,
205 .of_match = rockchip_dwmmc_pwrseq_ids,
206 .ops = &rockchip_dwmmc_pwrseq_ops,
207};
208#endif