blob: 47db6786cfce47334d4d387562c3dd6490318289 [file] [log] [blame]
Simon Glassa8cb4fb2015-08-30 16:55:37 -06001/*
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>
Simon Glassbfeb4432016-07-04 11:58:27 -060010#include <dt-structs.h>
Simon Glassa8cb4fb2015-08-30 16:55:37 -060011#include <dwmmc.h>
12#include <errno.h>
Simon Glassbfeb4432016-07-04 11:58:27 -060013#include <mapmem.h>
Simon Glasse1efec42016-01-21 19:43:34 -070014#include <pwrseq.h>
Simon Glassa8cb4fb2015-08-30 16:55:37 -060015#include <syscon.h>
Simon Glasse1efec42016-01-21 19:43:34 -070016#include <asm/gpio.h>
Simon Glassa8cb4fb2015-08-30 16:55:37 -060017#include <asm/arch/clock.h>
18#include <asm/arch/periph.h>
19#include <linux/err.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassf6e41d12016-05-14 14:03:08 -060023struct rockchip_mmc_plat {
Simon Glassbfeb4432016-07-04 11:58:27 -060024#if CONFIG_IS_ENABLED(OF_PLATDATA)
25 struct dtd_rockchip_rk3288_dw_mshc dtplat;
26#endif
Simon Glassf6e41d12016-05-14 14:03:08 -060027 struct mmc_config cfg;
28 struct mmc mmc;
29};
30
Simon Glassa8cb4fb2015-08-30 16:55:37 -060031struct rockchip_dwmmc_priv {
Stephen Warren135aa952016-06-17 09:44:00 -060032 struct clk clk;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060033 struct dwmci_host host;
Simon Glass6809b042016-07-04 11:58:26 -060034 int fifo_depth;
35 bool fifo_mode;
36 u32 minmax[2];
Simon Glassa8cb4fb2015-08-30 16:55:37 -060037};
38
39static 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
Stephen Warren135aa952016-06-17 09:44:00 -060045 ret = clk_set_rate(&priv->clk, freq);
Simon Glassa8cb4fb2015-08-30 16:55:37 -060046 if (ret < 0) {
47 debug("%s: err=%d\n", __func__, ret);
48 return ret;
49 }
50
51 return freq;
52}
53
54static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
55{
Simon Glassbfeb4432016-07-04 11:58:27 -060056#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassa8cb4fb2015-08-30 16:55:37 -060057 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
huang linace21982015-11-18 09:37:25 +080067 /* use non-removeable as sdcard and emmc as judgement */
68 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
huang lin65793852016-01-08 14:06:49 +080069 host->dev_index = 0;
70 else
huang linace21982015-11-18 09:37:25 +080071 host->dev_index = 1;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060072
Simon Glass6809b042016-07-04 11:58:26 -060073 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;
Simon Glassbfeb4432016-07-04 11:58:27 -060082#endif
Simon Glassa8cb4fb2015-08-30 16:55:37 -060083 return 0;
84}
85
86static int rockchip_dwmmc_probe(struct udevice *dev)
87{
Simon Glassf6e41d12016-05-14 14:03:08 -060088 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassa8cb4fb2015-08-30 16:55:37 -060089 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;
Simon Glasse1efec42016-01-21 19:43:34 -070092 struct udevice *pwr_dev __maybe_unused;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060093 int ret;
94
Simon Glassbfeb4432016-07-04 11:58:27 -060095#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
Simon Glass898d6432016-01-21 19:43:38 -0700112 ret = clk_get_by_index(dev, 0, &priv->clk);
113 if (ret < 0)
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600114 return ret;
Simon Glassbfeb4432016-07-04 11:58:27 -0600115#endif
huang lin28637242015-11-17 14:20:24 +0800116 host->fifoth_val = MSIZE(0x2) |
Simon Glass6809b042016-07-04 11:58:26 -0600117 RX_WMARK(priv->fifo_depth / 2 - 1) |
118 TX_WMARK(priv->fifo_depth / 2);
huang lin28637242015-11-17 14:20:24 +0800119
Simon Glass6809b042016-07-04 11:58:26 -0600120 host->fifo_mode = priv->fifo_mode;
huang lin28637242015-11-17 14:20:24 +0800121
Simon Glasse1efec42016-01-21 19:43:34 -0700122#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
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900132 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
Simon Glassf6e41d12016-05-14 14:03:08 -0600133 host->mmc = &plat->mmc;
Simon Glassf6e41d12016-05-14 14:03:08 -0600134 host->mmc->priv = &priv->host;
Simon Glasscffe5d82016-05-01 13:52:34 -0600135 host->mmc->dev = dev;
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600136 upriv->mmc = host->mmc;
137
Simon Glass42b37d82016-06-12 23:30:24 -0600138 return dwmci_probe(dev);
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600139}
140
Simon Glassf6e41d12016-05-14 14:03:08 -0600141static int rockchip_dwmmc_bind(struct udevice *dev)
142{
Simon Glassf6e41d12016-05-14 14:03:08 -0600143 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassf6e41d12016-05-14 14:03:08 -0600144
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900145 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glassf6e41d12016-05-14 14:03:08 -0600146}
147
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600148static const struct udevice_id rockchip_dwmmc_ids[] = {
149 { .compatible = "rockchip,rk3288-dw-mshc" },
150 { }
151};
152
153U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
Simon Glassbfeb4432016-07-04 11:58:27 -0600154 .name = "rockchip_rk3288_dw_mshc",
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600155 .id = UCLASS_MMC,
156 .of_match = rockchip_dwmmc_ids,
157 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
Simon Glass42b37d82016-06-12 23:30:24 -0600158 .ops = &dm_dwmci_ops,
Simon Glassf6e41d12016-05-14 14:03:08 -0600159 .bind = rockchip_dwmmc_bind,
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600160 .probe = rockchip_dwmmc_probe,
161 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
Simon Glassf6e41d12016-05-14 14:03:08 -0600162 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600163};
Simon Glasse1efec42016-01-21 19:43:34 -0700164
165#ifdef CONFIG_PWRSEQ
166static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
167{
168 struct gpio_desc reset;
169 int ret;
170
171 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
172 if (ret)
173 return ret;
174 dm_gpio_set_value(&reset, 1);
175 udelay(1);
176 dm_gpio_set_value(&reset, 0);
177 udelay(200);
178
179 return 0;
180}
181
182static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
183 .set_power = rockchip_dwmmc_pwrseq_set_power,
184};
185
186static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
187 { .compatible = "mmc-pwrseq-emmc" },
188 { }
189};
190
191U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
192 .name = "mmc_pwrseq_emmc",
193 .id = UCLASS_PWRSEQ,
194 .of_match = rockchip_dwmmc_pwrseq_ids,
195 .ops = &rockchip_dwmmc_pwrseq_ops,
196};
197#endif