blob: e4c5288876f827fdfa21c8f53875e7087f6d40b9 [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 Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060020#include <linux/err.h>
21
Simon Glassae696102016-05-14 14:03:08 -060022struct rockchip_mmc_plat {
Simon Glass4bb9ce42016-07-04 11:58:27 -060023#if CONFIG_IS_ENABLED(OF_PLATDATA)
24 struct dtd_rockchip_rk3288_dw_mshc dtplat;
25#endif
Simon Glassae696102016-05-14 14:03:08 -060026 struct mmc_config cfg;
27 struct mmc mmc;
28};
29
Simon Glass4ecaa6d2015-08-30 16:55:37 -060030struct rockchip_dwmmc_priv {
Stephen Warrena9622432016-06-17 09:44:00 -060031 struct clk clk;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060032 struct dwmci_host host;
Simon Glass4188d942016-07-04 11:58:26 -060033 int fifo_depth;
34 bool fifo_mode;
35 u32 minmax[2];
Simon Glass4ecaa6d2015-08-30 16:55:37 -060036};
37
38static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
39{
40 struct udevice *dev = host->priv;
41 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
42 int ret;
43
Stephen Warrena9622432016-06-17 09:44:00 -060044 ret = clk_set_rate(&priv->clk, freq);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060045 if (ret < 0) {
Kever Yanga70d1ea2017-06-14 16:31:49 +080046 debug("%s: err=%d\n", __func__, ret);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060047 return ret;
48 }
49
50 return freq;
51}
52
53static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
54{
Simon Glass4bb9ce42016-07-04 11:58:27 -060055#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass4ecaa6d2015-08-30 16:55:37 -060056 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
57 struct dwmci_host *host = &priv->host;
58
59 host->name = dev->name;
Philipp Tomsichff788812017-09-11 22:04:15 +020060 host->ioaddr = dev_read_addr_ptr(dev);
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020061 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060062 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
63 host->priv = dev;
64
huang lin8799fc12015-11-18 09:37:25 +080065 /* use non-removeable as sdcard and emmc as judgement */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020066 if (dev_read_bool(dev, "non-removable"))
huang linb06352f2016-01-08 14:06:49 +080067 host->dev_index = 0;
68 else
huang lin8799fc12015-11-18 09:37:25 +080069 host->dev_index = 1;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060070
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020071 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
72
Simon Glass4188d942016-07-04 11:58:26 -060073 if (priv->fifo_depth < 0)
74 return -EINVAL;
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020075 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
Philipp Tomsich56b38d82017-04-25 09:52:07 +020076
Heiko Stuebner13f1f722019-11-19 12:04:01 +010077#ifdef CONFIG_SPL_BUILD
78 if (!priv->fifo_mode)
79 priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode");
80#endif
81
Philipp Tomsich56b38d82017-04-25 09:52:07 +020082 /*
83 * 'clock-freq-min-max' is deprecated
84 * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
85 */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020086 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
87 int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
Philipp Tomsich56b38d82017-04-25 09:52:07 +020088
89 if (val < 0)
90 return val;
91
92 priv->minmax[0] = 400000; /* 400 kHz */
93 priv->minmax[1] = val;
94 } else {
95 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
96 __func__);
97 }
Simon Glass4bb9ce42016-07-04 11:58:27 -060098#endif
Simon Glass4ecaa6d2015-08-30 16:55:37 -060099 return 0;
100}
101
102static int rockchip_dwmmc_probe(struct udevice *dev)
103{
Simon Glassae696102016-05-14 14:03:08 -0600104 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600105 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
106 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
107 struct dwmci_host *host = &priv->host;
Simon Glass947fd982016-01-21 19:43:34 -0700108 struct udevice *pwr_dev __maybe_unused;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600109 int ret;
110
Simon Glass4bb9ce42016-07-04 11:58:27 -0600111#if CONFIG_IS_ENABLED(OF_PLATDATA)
112 struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
113
114 host->name = dev->name;
115 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
116 host->buswidth = dtplat->bus_width;
117 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
118 host->priv = dev;
119 host->dev_index = 0;
120 priv->fifo_depth = dtplat->fifo_depth;
121 priv->fifo_mode = 0;
Kever Yang97087392017-06-14 16:31:46 +0800122 priv->minmax[0] = 400000; /* 400 kHz */
123 priv->minmax[1] = dtplat->max_frequency;
Simon Glass4bb9ce42016-07-04 11:58:27 -0600124
Walter Lozanodc5b4372020-06-25 01:10:13 -0300125 ret = clk_get_by_driver_info(dev, dtplat->clocks, &priv->clk);
Simon Glass4bb9ce42016-07-04 11:58:27 -0600126 if (ret < 0)
127 return ret;
128#else
Kever Yanga70d1ea2017-06-14 16:31:49 +0800129 ret = clk_get_by_index(dev, 0, &priv->clk);
Simon Glass8d32f4b2016-01-21 19:43:38 -0700130 if (ret < 0)
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600131 return ret;
Simon Glass4bb9ce42016-07-04 11:58:27 -0600132#endif
huang linb1b71cd2015-11-17 14:20:24 +0800133 host->fifoth_val = MSIZE(0x2) |
Simon Glass4188d942016-07-04 11:58:26 -0600134 RX_WMARK(priv->fifo_depth / 2 - 1) |
135 TX_WMARK(priv->fifo_depth / 2);
huang linb1b71cd2015-11-17 14:20:24 +0800136
Simon Glass4188d942016-07-04 11:58:26 -0600137 host->fifo_mode = priv->fifo_mode;
huang linb1b71cd2015-11-17 14:20:24 +0800138
Simon Glass947fd982016-01-21 19:43:34 -0700139#ifdef CONFIG_PWRSEQ
140 /* Enable power if needed */
141 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
142 &pwr_dev);
143 if (!ret) {
144 ret = pwrseq_set_power(pwr_dev, true);
145 if (ret)
146 return ret;
147 }
148#endif
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900149 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
Simon Glassae696102016-05-14 14:03:08 -0600150 host->mmc = &plat->mmc;
Simon Glassae696102016-05-14 14:03:08 -0600151 host->mmc->priv = &priv->host;
Simon Glass77ca42b2016-05-01 13:52:34 -0600152 host->mmc->dev = dev;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600153 upriv->mmc = host->mmc;
154
Simon Glassfaeef3b2016-06-12 23:30:24 -0600155 return dwmci_probe(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600156}
157
Simon Glassae696102016-05-14 14:03:08 -0600158static int rockchip_dwmmc_bind(struct udevice *dev)
159{
Simon Glassae696102016-05-14 14:03:08 -0600160 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassae696102016-05-14 14:03:08 -0600161
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900162 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glassae696102016-05-14 14:03:08 -0600163}
164
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600165static const struct udevice_id rockchip_dwmmc_ids[] = {
Heiko Stuebner52c55a22018-09-21 10:59:46 +0200166 { .compatible = "rockchip,rk2928-dw-mshc" },
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600167 { .compatible = "rockchip,rk3288-dw-mshc" },
168 { }
169};
170
Walter Lozano2901ac62020-06-25 01:10:04 -0300171U_BOOT_DRIVER(rockchip_rk3288_dw_mshc) = {
Simon Glass4bb9ce42016-07-04 11:58:27 -0600172 .name = "rockchip_rk3288_dw_mshc",
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600173 .id = UCLASS_MMC,
174 .of_match = rockchip_dwmmc_ids,
175 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
Simon Glassfaeef3b2016-06-12 23:30:24 -0600176 .ops = &dm_dwmci_ops,
Simon Glassae696102016-05-14 14:03:08 -0600177 .bind = rockchip_dwmmc_bind,
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600178 .probe = rockchip_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700179 .priv_auto = sizeof(struct rockchip_dwmmc_priv),
180 .platdata_auto = sizeof(struct rockchip_mmc_plat),
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600181};
Simon Glass947fd982016-01-21 19:43:34 -0700182
Walter Lozano48e5b042020-06-25 01:10:06 -0300183U_BOOT_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3328_dw_mshc)
184U_BOOT_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3368_dw_mshc)
185
Simon Glass947fd982016-01-21 19:43:34 -0700186#ifdef CONFIG_PWRSEQ
187static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
188{
189 struct gpio_desc reset;
190 int ret;
191
192 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
193 if (ret)
194 return ret;
195 dm_gpio_set_value(&reset, 1);
196 udelay(1);
197 dm_gpio_set_value(&reset, 0);
198 udelay(200);
199
200 return 0;
201}
202
203static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
204 .set_power = rockchip_dwmmc_pwrseq_set_power,
205};
206
207static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
208 { .compatible = "mmc-pwrseq-emmc" },
209 { }
210};
211
212U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
213 .name = "mmc_pwrseq_emmc",
214 .id = UCLASS_PWRSEQ,
215 .of_match = rockchip_dwmmc_pwrseq_ids,
216 .ops = &rockchip_dwmmc_pwrseq_ops,
217};
218#endif