blob: ab89be47644e9dc3359255ce0a4be7a4d42b7ddb [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>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Kever Yang65922e02016-07-18 17:00:58 +080013#include <malloc.h>
Kever Yangdd99a022017-02-13 17:38:57 +080014#include <mapmem.h>
Kever Yang65922e02016-07-18 17:00:58 +080015#include <sdhci.h>
Kever Yang9ea1fdf2016-12-28 11:32:35 +080016#include <clk.h>
Kever Yang65922e02016-07-18 17:00:58 +080017
Kever Yang9ea1fdf2016-12-28 11:32:35 +080018DECLARE_GLOBAL_DATA_PTR;
Kever Yang65922e02016-07-18 17:00:58 +080019/* 400KHz is max freq for card ID etc. Use that as min */
20#define EMMC_MIN_FREQ 400000
21
22struct rockchip_sdhc_plat {
Kever Yangdd99a022017-02-13 17:38:57 +080023#if CONFIG_IS_ENABLED(OF_PLATDATA)
24 struct dtd_rockchip_rk3399_sdhci_5_1 dtplat;
25#endif
Kever Yang65922e02016-07-18 17:00:58 +080026 struct mmc_config cfg;
27 struct mmc mmc;
28};
29
30struct rockchip_sdhc {
31 struct sdhci_host host;
32 void *base;
33};
34
35static int arasan_sdhci_probe(struct udevice *dev)
36{
37 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
38 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
39 struct rockchip_sdhc *prv = dev_get_priv(dev);
40 struct sdhci_host *host = &prv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +080041 int max_frequency, ret;
42 struct clk clk;
43
Kever Yangdd99a022017-02-13 17:38:57 +080044#if CONFIG_IS_ENABLED(OF_PLATDATA)
45 struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat;
Kever Yang9ea1fdf2016-12-28 11:32:35 +080046
Kever Yangdd99a022017-02-13 17:38:57 +080047 host->name = dev->name;
Kever Yangd94bcb12017-09-07 11:20:50 +080048 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
Kever Yangdd99a022017-02-13 17:38:57 +080049 max_frequency = dtplat->max_frequency;
50 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &clk);
51#else
Philipp Tomsich9b4c3802017-06-07 18:46:00 +020052 max_frequency = dev_read_u32_default(dev, "max-frequency", 0);
Kever Yang9ea1fdf2016-12-28 11:32:35 +080053 ret = clk_get_by_index(dev, 0, &clk);
Kever Yangdd99a022017-02-13 17:38:57 +080054#endif
Kever Yang9ea1fdf2016-12-28 11:32:35 +080055 if (!ret) {
56 ret = clk_set_rate(&clk, max_frequency);
57 if (IS_ERR_VALUE(ret))
58 printf("%s clk set rate fail!\n", __func__);
59 } else {
60 printf("%s fail to get clk\n", __func__);
61 }
Kever Yang65922e02016-07-18 17:00:58 +080062
Kever Yang65922e02016-07-18 17:00:58 +080063 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010064 host->max_clk = max_frequency;
Philipp Tomsichafe2de22018-03-26 19:59:10 +020065 /*
66 * The sdhci-driver only supports 4bit and 8bit, as sdhci_setup_cfg
67 * doesn't allow us to clear MMC_MODE_4BIT. Consequently, we don't
68 * check for other bus-width values.
69 */
70 if (host->bus_width == 8)
71 host->host_caps |= MMC_MODE_8BIT;
Kever Yang65922e02016-07-18 17:00:58 +080072
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010073 ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ);
Kever Yang65922e02016-07-18 17:00:58 +080074
75 host->mmc = &plat->mmc;
76 if (ret)
77 return ret;
78 host->mmc->priv = &prv->host;
79 host->mmc->dev = dev;
80 upriv->mmc = host->mmc;
81
82 return sdhci_probe(dev);
83}
84
85static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
86{
Kever Yangdd99a022017-02-13 17:38:57 +080087#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yang65922e02016-07-18 17:00:58 +080088 struct sdhci_host *host = dev_get_priv(dev);
89
90 host->name = dev->name;
Philipp Tomsichdbb28282017-09-11 22:04:21 +020091 host->ioaddr = dev_read_addr_ptr(dev);
Philipp Tomsichafe2de22018-03-26 19:59:10 +020092 host->bus_width = dev_read_u32_default(dev, "bus-width", 4);
Kever Yangdd99a022017-02-13 17:38:57 +080093#endif
Kever Yang65922e02016-07-18 17:00:58 +080094
95 return 0;
96}
97
98static int rockchip_sdhci_bind(struct udevice *dev)
99{
100 struct rockchip_sdhc_plat *plat = dev_get_platdata(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800101
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900102 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +0800103}
104
105static const struct udevice_id arasan_sdhci_ids[] = {
106 { .compatible = "arasan,sdhci-5.1" },
107 { }
108};
109
110U_BOOT_DRIVER(arasan_sdhci_drv) = {
Kever Yangdd99a022017-02-13 17:38:57 +0800111 .name = "rockchip_rk3399_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800112 .id = UCLASS_MMC,
113 .of_match = arasan_sdhci_ids,
114 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
115 .ops = &sdhci_ops,
116 .bind = rockchip_sdhci_bind,
117 .probe = arasan_sdhci_probe,
118 .priv_auto_alloc_size = sizeof(struct rockchip_sdhc),
119 .platdata_auto_alloc_size = sizeof(struct rockchip_sdhc_plat),
120};