blob: 24754b7b120e38a730c34dc5cf6d5cdb022d78ee [file] [log] [blame]
Simon Glass4ecaa6d2015-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 Glass4bb9ce42016-07-04 11:58:27 -060010#include <dt-structs.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060011#include <dwmmc.h>
12#include <errno.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>
Simon Glass4ecaa6d2015-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 Glassae696102016-05-14 14:03:08 -060023struct rockchip_mmc_plat {
Simon Glass4bb9ce42016-07-04 11:58:27 -060024#if CONFIG_IS_ENABLED(OF_PLATDATA)
25 struct dtd_rockchip_rk3288_dw_mshc dtplat;
26#endif
Simon Glassae696102016-05-14 14:03:08 -060027 struct mmc_config cfg;
28 struct mmc mmc;
29};
30
Simon Glass4ecaa6d2015-08-30 16:55:37 -060031struct rockchip_dwmmc_priv {
Stephen Warrena9622432016-06-17 09:44:00 -060032 struct clk clk;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060033 struct dwmci_host host;
Simon Glass4188d942016-07-04 11:58:26 -060034 int fifo_depth;
35 bool fifo_mode;
36 u32 minmax[2];
Simon Glass4ecaa6d2015-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 Warrena9622432016-06-17 09:44:00 -060045 ret = clk_set_rate(&priv->clk, freq);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060046 if (ret < 0) {
Xu Ziyuanb7df12d2017-04-16 17:44:42 +080047 printf("%s: err=%d\n", __func__, ret);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060048 return ret;
49 }
50
51 return freq;
52}
53
54static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
55{
Simon Glass4bb9ce42016-07-04 11:58:27 -060056#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass4ecaa6d2015-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;
Simon Glassba1dea42017-05-17 17:18:05 -060061 host->ioaddr = (void *)devfdt_get_addr(dev);
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020062 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060063 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
64 host->priv = dev;
65
huang lin8799fc12015-11-18 09:37:25 +080066 /* use non-removeable as sdcard and emmc as judgement */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020067 if (dev_read_bool(dev, "non-removable"))
huang linb06352f2016-01-08 14:06:49 +080068 host->dev_index = 0;
69 else
huang lin8799fc12015-11-18 09:37:25 +080070 host->dev_index = 1;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060071
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020072 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
73
Simon Glass4188d942016-07-04 11:58:26 -060074 if (priv->fifo_depth < 0)
75 return -EINVAL;
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020076 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
Philipp Tomsich56b38d82017-04-25 09:52:07 +020077
78 /*
79 * 'clock-freq-min-max' is deprecated
80 * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
81 */
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020082 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
83 int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
Philipp Tomsich56b38d82017-04-25 09:52:07 +020084
85 if (val < 0)
86 return val;
87
88 priv->minmax[0] = 400000; /* 400 kHz */
89 priv->minmax[1] = val;
90 } else {
91 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
92 __func__);
93 }
Simon Glass4bb9ce42016-07-04 11:58:27 -060094#endif
Simon Glass4ecaa6d2015-08-30 16:55:37 -060095 return 0;
96}
97
98static int rockchip_dwmmc_probe(struct udevice *dev)
99{
Simon Glassae696102016-05-14 14:03:08 -0600100 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600101 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
102 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
103 struct dwmci_host *host = &priv->host;
Simon Glass947fd982016-01-21 19:43:34 -0700104 struct udevice *pwr_dev __maybe_unused;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600105 int ret;
106
Simon Glass4bb9ce42016-07-04 11:58:27 -0600107#if CONFIG_IS_ENABLED(OF_PLATDATA)
108 struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
109
110 host->name = dev->name;
111 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
112 host->buswidth = dtplat->bus_width;
113 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
114 host->priv = dev;
115 host->dev_index = 0;
116 priv->fifo_depth = dtplat->fifo_depth;
117 priv->fifo_mode = 0;
118 memcpy(priv->minmax, dtplat->clock_freq_min_max, sizeof(priv->minmax));
119
120 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
121 if (ret < 0)
122 return ret;
123#else
Xu Ziyuanb7df12d2017-04-16 17:44:42 +0800124 ret = clk_get_by_name(dev, "ciu", &priv->clk);
Simon Glass8d32f4b2016-01-21 19:43:38 -0700125 if (ret < 0)
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600126 return ret;
Simon Glass4bb9ce42016-07-04 11:58:27 -0600127#endif
huang linb1b71cd2015-11-17 14:20:24 +0800128 host->fifoth_val = MSIZE(0x2) |
Simon Glass4188d942016-07-04 11:58:26 -0600129 RX_WMARK(priv->fifo_depth / 2 - 1) |
130 TX_WMARK(priv->fifo_depth / 2);
huang linb1b71cd2015-11-17 14:20:24 +0800131
Simon Glass4188d942016-07-04 11:58:26 -0600132 host->fifo_mode = priv->fifo_mode;
huang linb1b71cd2015-11-17 14:20:24 +0800133
Simon Glass947fd982016-01-21 19:43:34 -0700134#ifdef CONFIG_PWRSEQ
135 /* Enable power if needed */
136 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
137 &pwr_dev);
138 if (!ret) {
139 ret = pwrseq_set_power(pwr_dev, true);
140 if (ret)
141 return ret;
142 }
143#endif
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900144 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
Simon Glassae696102016-05-14 14:03:08 -0600145 host->mmc = &plat->mmc;
Simon Glassae696102016-05-14 14:03:08 -0600146 host->mmc->priv = &priv->host;
Simon Glass77ca42b2016-05-01 13:52:34 -0600147 host->mmc->dev = dev;
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600148 upriv->mmc = host->mmc;
149
Simon Glassfaeef3b2016-06-12 23:30:24 -0600150 return dwmci_probe(dev);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600151}
152
Simon Glassae696102016-05-14 14:03:08 -0600153static int rockchip_dwmmc_bind(struct udevice *dev)
154{
Simon Glassae696102016-05-14 14:03:08 -0600155 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassae696102016-05-14 14:03:08 -0600156
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900157 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glassae696102016-05-14 14:03:08 -0600158}
159
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600160static const struct udevice_id rockchip_dwmmc_ids[] = {
161 { .compatible = "rockchip,rk3288-dw-mshc" },
162 { }
163};
164
165U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
Simon Glass4bb9ce42016-07-04 11:58:27 -0600166 .name = "rockchip_rk3288_dw_mshc",
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600167 .id = UCLASS_MMC,
168 .of_match = rockchip_dwmmc_ids,
169 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
Simon Glassfaeef3b2016-06-12 23:30:24 -0600170 .ops = &dm_dwmci_ops,
Simon Glassae696102016-05-14 14:03:08 -0600171 .bind = rockchip_dwmmc_bind,
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600172 .probe = rockchip_dwmmc_probe,
173 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
Simon Glassae696102016-05-14 14:03:08 -0600174 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600175};
Simon Glass947fd982016-01-21 19:43:34 -0700176
177#ifdef CONFIG_PWRSEQ
178static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
179{
180 struct gpio_desc reset;
181 int ret;
182
183 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
184 if (ret)
185 return ret;
186 dm_gpio_set_value(&reset, 1);
187 udelay(1);
188 dm_gpio_set_value(&reset, 0);
189 udelay(200);
190
191 return 0;
192}
193
194static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
195 .set_power = rockchip_dwmmc_pwrseq_set_power,
196};
197
198static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
199 { .compatible = "mmc-pwrseq-emmc" },
200 { }
201};
202
203U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
204 .name = "mmc_pwrseq_emmc",
205 .id = UCLASS_PWRSEQ,
206 .of_match = rockchip_dwmmc_pwrseq_ids,
207 .ops = &rockchip_dwmmc_pwrseq_ops,
208};
209#endif