blob: cb623d589b497ee3057aa53e47f3c1c6de72f58f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang79c83062016-07-18 17:00:58 +08002/*
3 * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
4 *
5 * Rockchip SD Host Controller Interface
Kever Yang79c83062016-07-18 17:00:58 +08006 */
7
8#include <common.h>
9#include <dm.h>
Kever Yangc2868212017-02-13 17:38:57 +080010#include <dt-structs.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Kever Yang79c83062016-07-18 17:00:58 +080012#include <malloc.h>
Kever Yangc2868212017-02-13 17:38:57 +080013#include <mapmem.h>
Kever Yang79c83062016-07-18 17:00:58 +080014#include <sdhci.h>
Kever Yang39fbb562016-12-28 11:32:35 +080015#include <clk.h>
Kever Yang79c83062016-07-18 17:00:58 +080016
17/* 400KHz is max freq for card ID etc. Use that as min */
18#define EMMC_MIN_FREQ 400000
19
20struct rockchip_sdhc_plat {
Kever Yangc2868212017-02-13 17:38:57 +080021#if CONFIG_IS_ENABLED(OF_PLATDATA)
22 struct dtd_rockchip_rk3399_sdhci_5_1 dtplat;
23#endif
Kever Yang79c83062016-07-18 17:00:58 +080024 struct mmc_config cfg;
25 struct mmc mmc;
26};
27
28struct rockchip_sdhc {
29 struct sdhci_host host;
30 void *base;
31};
32
33static int arasan_sdhci_probe(struct udevice *dev)
34{
35 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
36 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
37 struct rockchip_sdhc *prv = dev_get_priv(dev);
38 struct sdhci_host *host = &prv->host;
Kever Yang39fbb562016-12-28 11:32:35 +080039 int max_frequency, ret;
40 struct clk clk;
41
Kever Yangc2868212017-02-13 17:38:57 +080042#if CONFIG_IS_ENABLED(OF_PLATDATA)
43 struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat;
Kever Yang39fbb562016-12-28 11:32:35 +080044
Kever Yangc2868212017-02-13 17:38:57 +080045 host->name = dev->name;
Kever Yang5184dad2017-09-07 11:20:50 +080046 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
Kever Yangc2868212017-02-13 17:38:57 +080047 max_frequency = dtplat->max_frequency;
48 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &clk);
49#else
Philipp Tomsichfd1bf8d2017-06-07 18:46:00 +020050 max_frequency = dev_read_u32_default(dev, "max-frequency", 0);
Kever Yang39fbb562016-12-28 11:32:35 +080051 ret = clk_get_by_index(dev, 0, &clk);
Kever Yangc2868212017-02-13 17:38:57 +080052#endif
Kever Yang39fbb562016-12-28 11:32:35 +080053 if (!ret) {
54 ret = clk_set_rate(&clk, max_frequency);
55 if (IS_ERR_VALUE(ret))
56 printf("%s clk set rate fail!\n", __func__);
57 } else {
58 printf("%s fail to get clk\n", __func__);
59 }
Kever Yang79c83062016-07-18 17:00:58 +080060
Kever Yang79c83062016-07-18 17:00:58 +080061 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010062 host->max_clk = max_frequency;
Philipp Tomsich6837c582018-03-26 19:59:10 +020063 /*
64 * The sdhci-driver only supports 4bit and 8bit, as sdhci_setup_cfg
65 * doesn't allow us to clear MMC_MODE_4BIT. Consequently, we don't
66 * check for other bus-width values.
67 */
68 if (host->bus_width == 8)
69 host->host_caps |= MMC_MODE_8BIT;
Kever Yang79c83062016-07-18 17:00:58 +080070
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010071 ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ);
Kever Yang79c83062016-07-18 17:00:58 +080072
73 host->mmc = &plat->mmc;
74 if (ret)
75 return ret;
76 host->mmc->priv = &prv->host;
77 host->mmc->dev = dev;
78 upriv->mmc = host->mmc;
79
80 return sdhci_probe(dev);
81}
82
83static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
84{
Kever Yangc2868212017-02-13 17:38:57 +080085#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yang79c83062016-07-18 17:00:58 +080086 struct sdhci_host *host = dev_get_priv(dev);
87
88 host->name = dev->name;
Philipp Tomsich327b2b32017-09-11 22:04:21 +020089 host->ioaddr = dev_read_addr_ptr(dev);
Philipp Tomsich6837c582018-03-26 19:59:10 +020090 host->bus_width = dev_read_u32_default(dev, "bus-width", 4);
Kever Yangc2868212017-02-13 17:38:57 +080091#endif
Kever Yang79c83062016-07-18 17:00:58 +080092
93 return 0;
94}
95
96static int rockchip_sdhci_bind(struct udevice *dev)
97{
98 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
Kever Yang79c83062016-07-18 17:00:58 +080099
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900100 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang79c83062016-07-18 17:00:58 +0800101}
102
103static const struct udevice_id arasan_sdhci_ids[] = {
104 { .compatible = "arasan,sdhci-5.1" },
105 { }
106};
107
108U_BOOT_DRIVER(arasan_sdhci_drv) = {
Kever Yangc2868212017-02-13 17:38:57 +0800109 .name = "rockchip_rk3399_sdhci_5_1",
Kever Yang79c83062016-07-18 17:00:58 +0800110 .id = UCLASS_MMC,
111 .of_match = arasan_sdhci_ids,
112 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
113 .ops = &sdhci_ops,
114 .bind = rockchip_sdhci_bind,
115 .probe = arasan_sdhci_probe,
116 .priv_auto_alloc_size = sizeof(struct rockchip_sdhc),
117 .platdata_auto_alloc_size = sizeof(struct rockchip_sdhc_plat),
118};