blob: d1b293aa03963b07b516e35d6d4f094e68ba4c0c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass4ecaa6d2015-08-30 16:55:37 -06002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass4ecaa6d2015-08-30 16:55:37 -06004 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
Simon Glass4bb9ce42016-07-04 11:58:27 -06009#include <dt-structs.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060010#include <dwmmc.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass4bb9ce42016-07-04 11:58:27 -060013#include <mapmem.h>
Simon Glass947fd982016-01-21 19:43:34 -070014#include <pwrseq.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060015#include <syscon.h>
Simon Glass947fd982016-01-21 19:43:34 -070016#include <asm/gpio.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080017#include <asm/arch-rockchip/clock.h>
18#include <asm/arch-rockchip/periph.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060019#include <linux/err.h>
20
Simon Glassae696102016-05-14 14:03:08 -060021struct rockchip_mmc_plat {
Simon Glass4bb9ce42016-07-04 11:58:27 -060022#if CONFIG_IS_ENABLED(OF_PLATDATA)
23 struct dtd_rockchip_rk3288_dw_mshc dtplat;
24#endif
Simon Glassae696102016-05-14 14:03:08 -060025 struct mmc_config cfg;
26 struct mmc mmc;
27};
28
Simon Glass4ecaa6d2015-08-30 16:55:37 -060029struct rockchip_dwmmc_priv {
Stephen Warrena9622432016-06-17 09:44:00 -060030 struct clk clk;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060031 struct dwmci_host host;
Simon Glass4188d942016-07-04 11:58:26 -060032 int fifo_depth;
33 bool fifo_mode;
34 u32 minmax[2];
Simon Glass4ecaa6d2015-08-30 16:55:37 -060035};
36
37static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
38{
39 struct udevice *dev = host->priv;
40 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
41 int ret;
42
Stephen Warrena9622432016-06-17 09:44:00 -060043 ret = clk_set_rate(&priv->clk, freq);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060044 if (ret < 0) {
Kever Yanga70d1ea2017-06-14 16:31:49 +080045 debug("%s: err=%d\n", __func__, ret);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060046 return ret;
47 }
48
49 return freq;
50}
51
52static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
53{
Simon Glass4bb9ce42016-07-04 11:58:27 -060054#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass4ecaa6d2015-08-30 16:55:37 -060055 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
56 struct dwmci_host *host = &priv->host;
57
58 host->name = dev->name;
Philipp Tomsichff788812017-09-11 22:04:15 +020059 host->ioaddr = dev_read_addr_ptr(dev);
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020060 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060061 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
62 host->priv = dev;
63
huang lin8799fc12015-11-18 09:37:25 +080064 /* use non-removeable as sdcard and emmc as judgement */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020065 if (dev_read_bool(dev, "non-removable"))
huang linb06352f2016-01-08 14:06:49 +080066 host->dev_index = 0;
67 else
huang lin8799fc12015-11-18 09:37:25 +080068 host->dev_index = 1;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060069
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020070 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
71
Simon Glass4188d942016-07-04 11:58:26 -060072 if (priv->fifo_depth < 0)
73 return -EINVAL;
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020074 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
Philipp Tomsich56b38d82017-04-25 09:52:07 +020075
Heiko Stuebner13f1f722019-11-19 12:04:01 +010076#ifdef CONFIG_SPL_BUILD
77 if (!priv->fifo_mode)
78 priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode");
79#endif
80
Philipp Tomsich56b38d82017-04-25 09:52:07 +020081 /*
82 * 'clock-freq-min-max' is deprecated
83 * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
84 */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020085 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
86 int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
Philipp Tomsich56b38d82017-04-25 09:52:07 +020087
88 if (val < 0)
89 return val;
90
91 priv->minmax[0] = 400000; /* 400 kHz */
92 priv->minmax[1] = val;
93 } else {
94 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
95 __func__);
96 }
Simon Glass4bb9ce42016-07-04 11:58:27 -060097#endif
Simon Glass4ecaa6d2015-08-30 16:55:37 -060098 return 0;
99}
100
101static int rockchip_dwmmc_probe(struct udevice *dev)
102{
Simon Glassae696102016-05-14 14:03:08 -0600103 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600104 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
105 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
106 struct dwmci_host *host = &priv->host;
Simon Glass947fd982016-01-21 19:43:34 -0700107 struct udevice *pwr_dev __maybe_unused;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600108 int ret;
109
Simon Glass4bb9ce42016-07-04 11:58:27 -0600110#if CONFIG_IS_ENABLED(OF_PLATDATA)
111 struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
112
113 host->name = dev->name;
114 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
115 host->buswidth = dtplat->bus_width;
116 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
117 host->priv = dev;
118 host->dev_index = 0;
119 priv->fifo_depth = dtplat->fifo_depth;
120 priv->fifo_mode = 0;
Kever Yang97087392017-06-14 16:31:46 +0800121 priv->minmax[0] = 400000; /* 400 kHz */
122 priv->minmax[1] = dtplat->max_frequency;
Simon Glass4bb9ce42016-07-04 11:58:27 -0600123
124 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
125 if (ret < 0)
126 return ret;
127#else
Kever Yanga70d1ea2017-06-14 16:31:49 +0800128 ret = clk_get_by_index(dev, 0, &priv->clk);
Simon Glass8d32f4b2016-01-21 19:43:38 -0700129 if (ret < 0)
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600130 return ret;
Simon Glass4bb9ce42016-07-04 11:58:27 -0600131#endif
huang linb1b71cd2015-11-17 14:20:24 +0800132 host->fifoth_val = MSIZE(0x2) |
Simon Glass4188d942016-07-04 11:58:26 -0600133 RX_WMARK(priv->fifo_depth / 2 - 1) |
134 TX_WMARK(priv->fifo_depth / 2);
huang linb1b71cd2015-11-17 14:20:24 +0800135
Simon Glass4188d942016-07-04 11:58:26 -0600136 host->fifo_mode = priv->fifo_mode;
huang linb1b71cd2015-11-17 14:20:24 +0800137
Simon Glass947fd982016-01-21 19:43:34 -0700138#ifdef CONFIG_PWRSEQ
139 /* Enable power if needed */
140 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
141 &pwr_dev);
142 if (!ret) {
143 ret = pwrseq_set_power(pwr_dev, true);
144 if (ret)
145 return ret;
146 }
147#endif
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900148 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
Simon Glassae696102016-05-14 14:03:08 -0600149 host->mmc = &plat->mmc;
Simon Glassae696102016-05-14 14:03:08 -0600150 host->mmc->priv = &priv->host;
Simon Glass77ca42b2016-05-01 13:52:34 -0600151 host->mmc->dev = dev;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600152 upriv->mmc = host->mmc;
153
Simon Glassfaeef3b2016-06-12 23:30:24 -0600154 return dwmci_probe(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600155}
156
Simon Glassae696102016-05-14 14:03:08 -0600157static int rockchip_dwmmc_bind(struct udevice *dev)
158{
Simon Glassae696102016-05-14 14:03:08 -0600159 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassae696102016-05-14 14:03:08 -0600160
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900161 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glassae696102016-05-14 14:03:08 -0600162}
163
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600164static const struct udevice_id rockchip_dwmmc_ids[] = {
Heiko Stuebner52c55a22018-09-21 10:59:46 +0200165 { .compatible = "rockchip,rk2928-dw-mshc" },
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600166 { .compatible = "rockchip,rk3288-dw-mshc" },
167 { }
168};
169
170U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
Simon Glass4bb9ce42016-07-04 11:58:27 -0600171 .name = "rockchip_rk3288_dw_mshc",
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600172 .id = UCLASS_MMC,
173 .of_match = rockchip_dwmmc_ids,
174 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
Simon Glassfaeef3b2016-06-12 23:30:24 -0600175 .ops = &dm_dwmci_ops,
Simon Glassae696102016-05-14 14:03:08 -0600176 .bind = rockchip_dwmmc_bind,
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600177 .probe = rockchip_dwmmc_probe,
178 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
Simon Glassae696102016-05-14 14:03:08 -0600179 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600180};
Simon Glass947fd982016-01-21 19:43:34 -0700181
182#ifdef CONFIG_PWRSEQ
183static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
184{
185 struct gpio_desc reset;
186 int ret;
187
188 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
189 if (ret)
190 return ret;
191 dm_gpio_set_value(&reset, 1);
192 udelay(1);
193 dm_gpio_set_value(&reset, 0);
194 udelay(200);
195
196 return 0;
197}
198
199static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
200 .set_power = rockchip_dwmmc_pwrseq_set_power,
201};
202
203static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
204 { .compatible = "mmc-pwrseq-emmc" },
205 { }
206};
207
208U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
209 .name = "mmc_pwrseq_emmc",
210 .id = UCLASS_PWRSEQ,
211 .of_match = rockchip_dwmmc_pwrseq_ids,
212 .ops = &rockchip_dwmmc_pwrseq_ops,
213};
214#endif