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> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <libfdt.h> |
| 14 | #include <malloc.h> |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 15 | #include <mapmem.h> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 16 | #include <sdhci.h> |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 17 | #include <clk.h> |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 18 | |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 20 | /* 400KHz is max freq for card ID etc. Use that as min */ |
| 21 | #define EMMC_MIN_FREQ 400000 |
| 22 | |
| 23 | struct rockchip_sdhc_plat { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 24 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 25 | struct dtd_rockchip_rk3399_sdhci_5_1 dtplat; |
| 26 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 27 | struct mmc_config cfg; |
| 28 | struct mmc mmc; |
| 29 | }; |
| 30 | |
| 31 | struct rockchip_sdhc { |
| 32 | struct sdhci_host host; |
| 33 | void *base; |
| 34 | }; |
| 35 | |
| 36 | static 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 Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 42 | int max_frequency, ret; |
| 43 | struct clk clk; |
| 44 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 45 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 46 | struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat; |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 47 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 48 | 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 |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 53 | max_frequency = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 54 | "max-frequency", 0); |
| 55 | ret = clk_get_by_index(dev, 0, &clk); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 56 | #endif |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 57 | if (!ret) { |
| 58 | ret = clk_set_rate(&clk, max_frequency); |
| 59 | if (IS_ERR_VALUE(ret)) |
| 60 | printf("%s clk set rate fail!\n", __func__); |
| 61 | } else { |
| 62 | printf("%s fail to get clk\n", __func__); |
| 63 | } |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 64 | |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 65 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 66 | host->max_clk = max_frequency; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 67 | |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 68 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 69 | |
| 70 | host->mmc = &plat->mmc; |
| 71 | if (ret) |
| 72 | return ret; |
| 73 | host->mmc->priv = &prv->host; |
| 74 | host->mmc->dev = dev; |
| 75 | upriv->mmc = host->mmc; |
| 76 | |
| 77 | return sdhci_probe(dev); |
| 78 | } |
| 79 | |
| 80 | static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev) |
| 81 | { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 82 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 83 | struct sdhci_host *host = dev_get_priv(dev); |
| 84 | |
| 85 | host->name = dev->name; |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 86 | host->ioaddr = devfdt_get_addr_ptr(dev); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 87 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int rockchip_sdhci_bind(struct udevice *dev) |
| 93 | { |
| 94 | struct rockchip_sdhc_plat *plat = dev_get_platdata(dev); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 95 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 96 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static const struct udevice_id arasan_sdhci_ids[] = { |
| 100 | { .compatible = "arasan,sdhci-5.1" }, |
| 101 | { } |
| 102 | }; |
| 103 | |
| 104 | U_BOOT_DRIVER(arasan_sdhci_drv) = { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 105 | .name = "rockchip_rk3399_sdhci_5_1", |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 106 | .id = UCLASS_MMC, |
| 107 | .of_match = arasan_sdhci_ids, |
| 108 | .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata, |
| 109 | .ops = &sdhci_ops, |
| 110 | .bind = rockchip_sdhci_bind, |
| 111 | .probe = arasan_sdhci_probe, |
| 112 | .priv_auto_alloc_size = sizeof(struct rockchip_sdhc), |
| 113 | .platdata_auto_alloc_size = sizeof(struct rockchip_sdhc_plat), |
| 114 | }; |