blob: 691a51557de4e89b41f3b975618fe6ba28a7c111 [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>
10#include <dwmmc.h>
11#include <errno.h>
Simon Glasse1efec42016-01-21 19:43:34 -070012#include <pwrseq.h>
Simon Glassa8cb4fb2015-08-30 16:55:37 -060013#include <syscon.h>
Simon Glasse1efec42016-01-21 19:43:34 -070014#include <asm/gpio.h>
Simon Glassa8cb4fb2015-08-30 16:55:37 -060015#include <asm/arch/clock.h>
16#include <asm/arch/periph.h>
17#include <linux/err.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassf6e41d12016-05-14 14:03:08 -060021struct rockchip_mmc_plat {
22 struct mmc_config cfg;
23 struct mmc mmc;
24};
25
Simon Glassa8cb4fb2015-08-30 16:55:37 -060026struct rockchip_dwmmc_priv {
Stephen Warren135aa952016-06-17 09:44:00 -060027 struct clk clk;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060028 struct dwmci_host host;
29};
30
31static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
32{
33 struct udevice *dev = host->priv;
34 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
35 int ret;
36
Stephen Warren135aa952016-06-17 09:44:00 -060037 ret = clk_set_rate(&priv->clk, freq);
Simon Glassa8cb4fb2015-08-30 16:55:37 -060038 if (ret < 0) {
39 debug("%s: err=%d\n", __func__, ret);
40 return ret;
41 }
42
43 return freq;
44}
45
46static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
47{
48 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
49 struct dwmci_host *host = &priv->host;
50
51 host->name = dev->name;
52 host->ioaddr = (void *)dev_get_addr(dev);
53 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
54 "bus-width", 4);
55 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
56 host->priv = dev;
57
huang linace21982015-11-18 09:37:25 +080058 /* use non-removeable as sdcard and emmc as judgement */
59 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
huang lin65793852016-01-08 14:06:49 +080060 host->dev_index = 0;
61 else
huang linace21982015-11-18 09:37:25 +080062 host->dev_index = 1;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060063
64 return 0;
65}
66
67static int rockchip_dwmmc_probe(struct udevice *dev)
68{
Simon Glassf6e41d12016-05-14 14:03:08 -060069 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
Simon Glassa8cb4fb2015-08-30 16:55:37 -060070 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
71 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
72 struct dwmci_host *host = &priv->host;
Simon Glasse1efec42016-01-21 19:43:34 -070073 struct udevice *pwr_dev __maybe_unused;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060074 u32 minmax[2];
75 int ret;
huang lin28637242015-11-17 14:20:24 +080076 int fifo_depth;
Simon Glassa8cb4fb2015-08-30 16:55:37 -060077
Simon Glass898d6432016-01-21 19:43:38 -070078 ret = clk_get_by_index(dev, 0, &priv->clk);
79 if (ret < 0)
Simon Glassa8cb4fb2015-08-30 16:55:37 -060080 return ret;
81
huang lin28637242015-11-17 14:20:24 +080082 if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
83 "clock-freq-min-max", minmax, 2))
84 return -EINVAL;
85
86 fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
87 "fifo-depth", 0);
88 if (fifo_depth < 0)
89 return -EINVAL;
90
91 host->fifoth_val = MSIZE(0x2) |
92 RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
93
94 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "fifo-mode"))
95 host->fifo_mode = true;
96
Simon Glasse1efec42016-01-21 19:43:34 -070097#ifdef CONFIG_PWRSEQ
98 /* Enable power if needed */
99 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
100 &pwr_dev);
101 if (!ret) {
102 ret = pwrseq_set_power(pwr_dev, true);
103 if (ret)
104 return ret;
105 }
106#endif
Simon Glassf6e41d12016-05-14 14:03:08 -0600107 dwmci_setup_cfg(&plat->cfg, dev->name, host->buswidth, host->caps,
108 minmax[1], minmax[0]);
109 host->mmc = &plat->mmc;
Simon Glassf6e41d12016-05-14 14:03:08 -0600110 host->mmc->priv = &priv->host;
Simon Glasscffe5d82016-05-01 13:52:34 -0600111 host->mmc->dev = dev;
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600112 upriv->mmc = host->mmc;
113
Simon Glass42b37d82016-06-12 23:30:24 -0600114 return dwmci_probe(dev);
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600115}
116
Simon Glassf6e41d12016-05-14 14:03:08 -0600117static int rockchip_dwmmc_bind(struct udevice *dev)
118{
Simon Glassf6e41d12016-05-14 14:03:08 -0600119 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
120 int ret;
121
122 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
123 if (ret)
124 return ret;
Simon Glassf6e41d12016-05-14 14:03:08 -0600125
126 return 0;
127}
128
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600129static const struct udevice_id rockchip_dwmmc_ids[] = {
130 { .compatible = "rockchip,rk3288-dw-mshc" },
131 { }
132};
133
134U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
135 .name = "rockchip_dwmmc",
136 .id = UCLASS_MMC,
137 .of_match = rockchip_dwmmc_ids,
138 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
Simon Glass42b37d82016-06-12 23:30:24 -0600139 .ops = &dm_dwmci_ops,
Simon Glassf6e41d12016-05-14 14:03:08 -0600140 .bind = rockchip_dwmmc_bind,
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600141 .probe = rockchip_dwmmc_probe,
142 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
Simon Glassf6e41d12016-05-14 14:03:08 -0600143 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
Simon Glassa8cb4fb2015-08-30 16:55:37 -0600144};
Simon Glasse1efec42016-01-21 19:43:34 -0700145
146#ifdef CONFIG_PWRSEQ
147static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
148{
149 struct gpio_desc reset;
150 int ret;
151
152 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
153 if (ret)
154 return ret;
155 dm_gpio_set_value(&reset, 1);
156 udelay(1);
157 dm_gpio_set_value(&reset, 0);
158 udelay(200);
159
160 return 0;
161}
162
163static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
164 .set_power = rockchip_dwmmc_pwrseq_set_power,
165};
166
167static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
168 { .compatible = "mmc-pwrseq-emmc" },
169 { }
170};
171
172U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
173 .name = "mmc_pwrseq_emmc",
174 .id = UCLASS_PWRSEQ,
175 .of_match = rockchip_dwmmc_pwrseq_ids,
176 .ops = &rockchip_dwmmc_pwrseq_ops,
177};
178#endif