Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd |
| 4 | * |
| 5 | * Rockchip SD Host Controller Interface |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 10 | #include <dt-structs.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <linux/err.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 | |
| 18 | /* 400KHz is max freq for card ID etc. Use that as min */ |
| 19 | #define EMMC_MIN_FREQ 400000 |
| 20 | |
| 21 | struct rockchip_sdhc_plat { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 22 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 23 | struct dtd_rockchip_rk3399_sdhci_5_1 dtplat; |
| 24 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 25 | struct mmc_config cfg; |
| 26 | struct mmc mmc; |
| 27 | }; |
| 28 | |
| 29 | struct rockchip_sdhc { |
| 30 | struct sdhci_host host; |
| 31 | void *base; |
| 32 | }; |
| 33 | |
| 34 | static int arasan_sdhci_probe(struct udevice *dev) |
| 35 | { |
| 36 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 37 | struct rockchip_sdhc_plat *plat = dev_get_platdata(dev); |
| 38 | struct rockchip_sdhc *prv = dev_get_priv(dev); |
| 39 | struct sdhci_host *host = &prv->host; |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 40 | int max_frequency, ret; |
| 41 | struct clk clk; |
| 42 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 43 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 44 | struct dtd_rockchip_rk3399_sdhci_5_1 *dtplat = &plat->dtplat; |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 45 | |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 46 | host->name = dev->name; |
Kever Yang | 5184dad | 2017-09-07 11:20:50 +0800 | [diff] [blame] | 47 | host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 48 | max_frequency = dtplat->max_frequency; |
Walter Lozano | 51f1263 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 49 | ret = clk_get_by_driver_info(dev, dtplat->clocks, &clk); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 50 | #else |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 51 | max_frequency = dev_read_u32_default(dev, "max-frequency", 0); |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 52 | ret = clk_get_by_index(dev, 0, &clk); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 53 | #endif |
Kever Yang | 39fbb56 | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 54 | if (!ret) { |
| 55 | ret = clk_set_rate(&clk, max_frequency); |
| 56 | if (IS_ERR_VALUE(ret)) |
| 57 | printf("%s clk set rate fail!\n", __func__); |
| 58 | } else { |
| 59 | printf("%s fail to get clk\n", __func__); |
| 60 | } |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 61 | |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 62 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 63 | host->max_clk = max_frequency; |
Philipp Tomsich | 6837c58 | 2018-03-26 19:59:10 +0200 | [diff] [blame] | 64 | /* |
| 65 | * The sdhci-driver only supports 4bit and 8bit, as sdhci_setup_cfg |
| 66 | * doesn't allow us to clear MMC_MODE_4BIT. Consequently, we don't |
| 67 | * check for other bus-width values. |
| 68 | */ |
| 69 | if (host->bus_width == 8) |
| 70 | host->host_caps |= MMC_MODE_8BIT; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 71 | |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 72 | host->mmc = &plat->mmc; |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 73 | host->mmc->priv = &prv->host; |
| 74 | host->mmc->dev = dev; |
| 75 | upriv->mmc = host->mmc; |
| 76 | |
Kever Yang | 4dcdc5c | 2019-07-19 18:01:11 +0800 | [diff] [blame] | 77 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, EMMC_MIN_FREQ); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 81 | return sdhci_probe(dev); |
| 82 | } |
| 83 | |
| 84 | static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev) |
| 85 | { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 86 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 87 | struct sdhci_host *host = dev_get_priv(dev); |
| 88 | |
| 89 | host->name = dev->name; |
Philipp Tomsich | 327b2b3 | 2017-09-11 22:04:21 +0200 | [diff] [blame] | 90 | host->ioaddr = dev_read_addr_ptr(dev); |
Philipp Tomsich | 6837c58 | 2018-03-26 19:59:10 +0200 | [diff] [blame] | 91 | host->bus_width = dev_read_u32_default(dev, "bus-width", 4); |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 92 | #endif |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int rockchip_sdhci_bind(struct udevice *dev) |
| 98 | { |
| 99 | struct rockchip_sdhc_plat *plat = dev_get_platdata(dev); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 100 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 101 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static const struct udevice_id arasan_sdhci_ids[] = { |
| 105 | { .compatible = "arasan,sdhci-5.1" }, |
| 106 | { } |
| 107 | }; |
| 108 | |
| 109 | U_BOOT_DRIVER(arasan_sdhci_drv) = { |
Kever Yang | c286821 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 110 | .name = "rockchip_rk3399_sdhci_5_1", |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 111 | .id = UCLASS_MMC, |
| 112 | .of_match = arasan_sdhci_ids, |
| 113 | .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata, |
| 114 | .ops = &sdhci_ops, |
| 115 | .bind = rockchip_sdhci_bind, |
| 116 | .probe = arasan_sdhci_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 117 | .priv_auto = sizeof(struct rockchip_sdhc), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 118 | .plat_auto = sizeof(struct rockchip_sdhc_plat), |
Kever Yang | 79c8306 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 119 | }; |