blob: 9b4268ecdd913a640c42d164f72ec2a345aaaac8 [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>
12#include <syscon.h>
13#include <asm/arch/clock.h>
14#include <asm/arch/periph.h>
15#include <linux/err.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19struct rockchip_dwmmc_priv {
20 struct udevice *clk;
21 struct rk3288_grf *grf;
22 struct dwmci_host host;
23};
24
25static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
26{
27 struct udevice *dev = host->priv;
28 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
29 int ret;
30
31 ret = clk_set_periph_rate(priv->clk, PERIPH_ID_SDMMC0 + host->dev_index,
32 freq);
33 if (ret < 0) {
34 debug("%s: err=%d\n", __func__, ret);
35 return ret;
36 }
37
38 return freq;
39}
40
41static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
42{
43 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
44 struct dwmci_host *host = &priv->host;
45
46 host->name = dev->name;
47 host->ioaddr = (void *)dev_get_addr(dev);
48 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
49 "bus-width", 4);
50 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
51 host->priv = dev;
52
huang lin8799fc12015-11-18 09:37:25 +080053 /* use non-removeable as sdcard and emmc as judgement */
54 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
huang linb06352f2016-01-08 14:06:49 +080055 host->dev_index = 0;
56 else
huang lin8799fc12015-11-18 09:37:25 +080057 host->dev_index = 1;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060058
59 return 0;
60}
61
62static int rockchip_dwmmc_probe(struct udevice *dev)
63{
64 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
65 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
66 struct dwmci_host *host = &priv->host;
67 u32 minmax[2];
68 int ret;
huang linb1b71cd2015-11-17 14:20:24 +080069 int fifo_depth;
Simon Glass4ecaa6d2015-08-30 16:55:37 -060070
71 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
72 if (IS_ERR(priv->grf))
73 return PTR_ERR(priv->grf);
74 ret = uclass_get_device(UCLASS_CLK, CLK_GENERAL, &priv->clk);
75 if (ret)
76 return ret;
77
huang linb1b71cd2015-11-17 14:20:24 +080078 if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
79 "clock-freq-min-max", minmax, 2))
80 return -EINVAL;
81
82 fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
83 "fifo-depth", 0);
84 if (fifo_depth < 0)
85 return -EINVAL;
86
87 host->fifoth_val = MSIZE(0x2) |
88 RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
89
90 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "fifo-mode"))
91 host->fifo_mode = true;
92
93 ret = add_dwmci(host, minmax[1], minmax[0]);
Simon Glass4ecaa6d2015-08-30 16:55:37 -060094 if (ret)
95 return ret;
96
97 upriv->mmc = host->mmc;
98
99 return 0;
100}
101
102static const struct udevice_id rockchip_dwmmc_ids[] = {
103 { .compatible = "rockchip,rk3288-dw-mshc" },
104 { }
105};
106
107U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
108 .name = "rockchip_dwmmc",
109 .id = UCLASS_MMC,
110 .of_match = rockchip_dwmmc_ids,
111 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
112 .probe = rockchip_dwmmc_probe,
113 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
114};