blob: f31d329c81da7e7ad4e9269ae7203d5fc62284c6 [file] [log] [blame]
Kever Yang65922e02016-07-18 17:00:58 +08001/*
2 * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
3 *
4 * Rockchip SD Host Controller Interface
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <dm.h>
Kever Yangdd99a022017-02-13 17:38:57 +080011#include <dt-structs.h>
Kever Yang65922e02016-07-18 17:00:58 +080012#include <fdtdec.h>
13#include <libfdt.h>
14#include <malloc.h>
Kever Yangdd99a022017-02-13 17:38:57 +080015#include <mapmem.h>
Kever Yang65922e02016-07-18 17:00:58 +080016#include <sdhci.h>
Kever Yang9ea1fdf2016-12-28 11:32:35 +080017#include <clk.h>
Kever Yang65922e02016-07-18 17:00:58 +080018
Kever Yang9ea1fdf2016-12-28 11:32:35 +080019DECLARE_GLOBAL_DATA_PTR;
Kever Yang65922e02016-07-18 17:00:58 +080020/* 400KHz is max freq for card ID etc. Use that as min */
21#define EMMC_MIN_FREQ 400000
22
23struct rockchip_sdhc_plat {
Kever Yangdd99a022017-02-13 17:38:57 +080024#if CONFIG_IS_ENABLED(OF_PLATDATA)
25 struct dtd_rockchip_rk3399_sdhci_5_1 dtplat;
26#endif
Kever Yang65922e02016-07-18 17:00:58 +080027 struct mmc_config cfg;
28 struct mmc mmc;
29};
30
31struct rockchip_sdhc {
32 struct sdhci_host host;
33 void *base;
34};
35
36static int arasan_sdhci_probe(struct udevice *dev)
37{
38 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
39 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
40 struct rockchip_sdhc *prv = dev_get_priv(dev);
41 struct sdhci_host *host = &prv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +080042 int max_frequency, ret;
43 struct clk clk;
44
Kever Yangdd99a022017-02-13 17:38:57 +080045#if CONFIG_IS_ENABLED(OF_PLATDATA)
46 struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat;
Kever Yang9ea1fdf2016-12-28 11:32:35 +080047
Kever Yangdd99a022017-02-13 17:38:57 +080048 host->name = dev->name;
49 host->ioaddr = map_sysmem(dtplat->reg[1], dtplat->reg[3]);
50 max_frequency = dtplat->max_frequency;
51 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &clk);
52#else
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020053 max_frequency = dev_read_u32_default(dev, "max-frequency", 0);
Kever Yang9ea1fdf2016-12-28 11:32:35 +080054 ret = clk_get_by_index(dev, 0, &clk);
Kever Yangdd99a022017-02-13 17:38:57 +080055#endif
Kever Yang9ea1fdf2016-12-28 11:32:35 +080056 if (!ret) {
57 ret = clk_set_rate(&clk, max_frequency);
58 if (IS_ERR_VALUE(ret))
59 printf("%s clk set rate fail!\n", __func__);
60 } else {
61 printf("%s fail to get clk\n", __func__);
62 }
Kever Yang65922e02016-07-18 17:00:58 +080063
Kever Yang65922e02016-07-18 17:00:58 +080064 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010065 host->max_clk = max_frequency;
Kever Yang65922e02016-07-18 17:00:58 +080066
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010067 ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ);
Kever Yang65922e02016-07-18 17:00:58 +080068
69 host->mmc = &plat->mmc;
70 if (ret)
71 return ret;
72 host->mmc->priv = &prv->host;
73 host->mmc->dev = dev;
74 upriv->mmc = host->mmc;
75
76 return sdhci_probe(dev);
77}
78
79static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
80{
Kever Yangdd99a022017-02-13 17:38:57 +080081#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yang65922e02016-07-18 17:00:58 +080082 struct sdhci_host *host = dev_get_priv(dev);
83
84 host->name = dev->name;
Simon Glassba1dea42017-05-17 17:18:05 -060085 host->ioaddr = devfdt_get_addr_ptr(dev);
Kever Yangdd99a022017-02-13 17:38:57 +080086#endif
Kever Yang65922e02016-07-18 17:00:58 +080087
88 return 0;
89}
90
91static int rockchip_sdhci_bind(struct udevice *dev)
92{
93 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
Kever Yang65922e02016-07-18 17:00:58 +080094
Masahiro Yamadacdb67f32016-09-06 22:17:32 +090095 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +080096}
97
98static const struct udevice_id arasan_sdhci_ids[] = {
99 { .compatible = "arasan,sdhci-5.1" },
100 { }
101};
102
103U_BOOT_DRIVER(arasan_sdhci_drv) = {
Kever Yangdd99a022017-02-13 17:38:57 +0800104 .name = "rockchip_rk3399_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800105 .id = UCLASS_MMC,
106 .of_match = arasan_sdhci_ids,
107 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
108 .ops = &sdhci_ops,
109 .bind = rockchip_sdhci_bind,
110 .probe = arasan_sdhci_probe,
111 .priv_auto_alloc_size = sizeof(struct rockchip_sdhc),
112 .platdata_auto_alloc_size = sizeof(struct rockchip_sdhc_plat),
113};