blob: 080c831d56c17266b114c30424ce19113acb1f45 [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>
10#include <dwmmc.h>
11#include <errno.h>
Simon Glass947fd982016-01-21 19:43:34 -070012#include <pwrseq.h>
Simon Glass4ecaa6d2015-08-30 16:55:37 -060013#include <syscon.h>
Simon Glass947fd982016-01-21 19:43:34 -070014#include <asm/gpio.h>
Simon Glass4ecaa6d2015-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
21struct rockchip_dwmmc_priv {
22 struct udevice *clk;
23 struct rk3288_grf *grf;
24 struct dwmci_host host;
25};
26
27static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
28{
29 struct udevice *dev = host->priv;
30 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
31 int ret;
32
33 ret = clk_set_periph_rate(priv->clk, PERIPH_ID_SDMMC0 + host->dev_index,
34 freq);
35 if (ret < 0) {
36 debug("%s: err=%d\n", __func__, ret);
37 return ret;
38 }
39
40 return freq;
41}
42
43static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
44{
45 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
46 struct dwmci_host *host = &priv->host;
47
48 host->name = dev->name;
49 host->ioaddr = (void *)dev_get_addr(dev);
50 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
51 "bus-width", 4);
52 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
53 host->priv = dev;
54
huang lin8799fc12015-11-18 09:37:25 +080055 /* use non-removeable as sdcard and emmc as judgement */
56 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
huang linb06352f2016-01-08 14:06:49 +080057 host->dev_index = 0;
58 else
huang lin8799fc12015-11-18 09:37:25 +080059 host->dev_index = 1;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060060
61 return 0;
62}
63
64static int rockchip_dwmmc_probe(struct udevice *dev)
65{
66 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
67 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
68 struct dwmci_host *host = &priv->host;
Simon Glass947fd982016-01-21 19:43:34 -070069 struct udevice *pwr_dev __maybe_unused;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060070 u32 minmax[2];
71 int ret;
huang linb1b71cd2015-11-17 14:20:24 +080072 int fifo_depth;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060073
74 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
75 if (IS_ERR(priv->grf))
76 return PTR_ERR(priv->grf);
77 ret = uclass_get_device(UCLASS_CLK, CLK_GENERAL, &priv->clk);
78 if (ret)
79 return ret;
80
huang linb1b71cd2015-11-17 14:20:24 +080081 if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
82 "clock-freq-min-max", minmax, 2))
83 return -EINVAL;
84
85 fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
86 "fifo-depth", 0);
87 if (fifo_depth < 0)
88 return -EINVAL;
89
90 host->fifoth_val = MSIZE(0x2) |
91 RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
92
93 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "fifo-mode"))
94 host->fifo_mode = true;
95
Simon Glass947fd982016-01-21 19:43:34 -070096#ifdef CONFIG_PWRSEQ
97 /* Enable power if needed */
98 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
99 &pwr_dev);
100 if (!ret) {
101 ret = pwrseq_set_power(pwr_dev, true);
102 if (ret)
103 return ret;
104 }
105#endif
huang linb1b71cd2015-11-17 14:20:24 +0800106 ret = add_dwmci(host, minmax[1], minmax[0]);
Simon Glass4ecaa6d2015-08-30 16:55:37 -0600107 if (ret)
108 return ret;
109
110 upriv->mmc = host->mmc;
111
112 return 0;
113}
114
115static const struct udevice_id rockchip_dwmmc_ids[] = {
116 { .compatible = "rockchip,rk3288-dw-mshc" },
117 { }
118};
119
120U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
121 .name = "rockchip_dwmmc",
122 .id = UCLASS_MMC,
123 .of_match = rockchip_dwmmc_ids,
124 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
125 .probe = rockchip_dwmmc_probe,
126 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
127};
Simon Glass947fd982016-01-21 19:43:34 -0700128
129#ifdef CONFIG_PWRSEQ
130static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
131{
132 struct gpio_desc reset;
133 int ret;
134
135 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
136 if (ret)
137 return ret;
138 dm_gpio_set_value(&reset, 1);
139 udelay(1);
140 dm_gpio_set_value(&reset, 0);
141 udelay(200);
142
143 return 0;
144}
145
146static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
147 .set_power = rockchip_dwmmc_pwrseq_set_power,
148};
149
150static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
151 { .compatible = "mmc-pwrseq-emmc" },
152 { }
153};
154
155U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
156 .name = "mmc_pwrseq_emmc",
157 .id = UCLASS_PWRSEQ,
158 .of_match = rockchip_dwmmc_pwrseq_ids,
159 .ops = &rockchip_dwmmc_pwrseq_ops,
160};
161#endif