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