Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 1 | /* |
| 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 Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 11 | #include <dt-structs.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 13 | #include <malloc.h> |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 14 | #include <mapmem.h> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 15 | #include <sdhci.h> |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 16 | #include <clk.h> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 17 | |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 19 | /* 400KHz is max freq for card ID etc. Use that as min */ |
| 20 | #define EMMC_MIN_FREQ 400000 |
| 21 | |
| 22 | struct rockchip_sdhc_plat { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 24 | struct dtd_rockchip_rk3399_sdhci_5_1 dtplat; |
| 25 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 26 | struct mmc_config cfg; |
| 27 | struct mmc mmc; |
| 28 | }; |
| 29 | |
| 30 | struct rockchip_sdhc { |
| 31 | struct sdhci_host host; |
| 32 | void *base; |
| 33 | }; |
| 34 | |
| 35 | static 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 Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 41 | int max_frequency, ret; |
| 42 | struct clk clk; |
| 43 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 44 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 45 | struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat; |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 46 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 47 | host->name = dev->name; |
Kever Yang | 5184dad | 2017-09-07 11:20:50 +0800 | [diff] [blame] | 48 | host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 49 | max_frequency = dtplat->max_frequency; |
| 50 | ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &clk); |
| 51 | #else |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 52 | max_frequency = dev_read_u32_default(dev, "max-frequency", 0); |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 53 | ret = clk_get_by_index(dev, 0, &clk); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 54 | #endif |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 55 | 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 Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 62 | |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 63 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 64 | host->max_clk = max_frequency; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 65 | |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 66 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 67 | |
| 68 | host->mmc = &plat->mmc; |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | host->mmc->priv = &prv->host; |
| 72 | host->mmc->dev = dev; |
| 73 | upriv->mmc = host->mmc; |
| 74 | |
| 75 | return sdhci_probe(dev); |
| 76 | } |
| 77 | |
| 78 | static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev) |
| 79 | { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 80 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 81 | struct sdhci_host *host = dev_get_priv(dev); |
| 82 | |
| 83 | host->name = dev->name; |
Philipp Tomsich | 327b2b3 | 2017-09-11 22:04:21 +0200 | [diff] [blame] | 84 | host->ioaddr = dev_read_addr_ptr(dev); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 85 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int rockchip_sdhci_bind(struct udevice *dev) |
| 91 | { |
| 92 | struct rockchip_sdhc_plat *plat = dev_get_platdata(dev); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 93 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 94 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static const struct udevice_id arasan_sdhci_ids[] = { |
| 98 | { .compatible = "arasan,sdhci-5.1" }, |
| 99 | { } |
| 100 | }; |
| 101 | |
| 102 | U_BOOT_DRIVER(arasan_sdhci_drv) = { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 103 | .name = "rockchip_rk3399_sdhci_5_1", |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 104 | .id = UCLASS_MMC, |
| 105 | .of_match = arasan_sdhci_ids, |
| 106 | .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata, |
| 107 | .ops = &sdhci_ops, |
| 108 | .bind = rockchip_sdhci_bind, |
| 109 | .probe = arasan_sdhci_probe, |
| 110 | .priv_auto_alloc_size = sizeof(struct rockchip_sdhc), |
| 111 | .platdata_auto_alloc_size = sizeof(struct rockchip_sdhc_plat), |
| 112 | }; |