Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 9 | #include <dt-structs.h> |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 10 | #include <dwmmc.h> |
| 11 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 14 | #include <pwrseq.h> |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 15 | #include <syscon.h> |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 17 | #include <asm/arch-rockchip/clock.h> |
| 18 | #include <asm/arch-rockchip/periph.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 22 | struct rockchip_mmc_plat { |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 24 | struct dtd_rockchip_rk3288_dw_mshc dtplat; |
| 25 | #endif |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 26 | struct mmc_config cfg; |
| 27 | struct mmc mmc; |
| 28 | }; |
| 29 | |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 30 | struct rockchip_dwmmc_priv { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 31 | struct clk clk; |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 32 | struct dwmci_host host; |
Simon Glass | 4188d94 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 33 | int fifo_depth; |
| 34 | bool fifo_mode; |
| 35 | u32 minmax[2]; |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq) |
| 39 | { |
| 40 | struct udevice *dev = host->priv; |
| 41 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 42 | int ret; |
| 43 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 44 | ret = clk_set_rate(&priv->clk, freq); |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 45 | if (ret < 0) { |
Kever Yang | a70d1ea | 2017-06-14 16:31:49 +0800 | [diff] [blame] | 46 | debug("%s: err=%d\n", __func__, ret); |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | return freq; |
| 51 | } |
| 52 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 53 | static int rockchip_dwmmc_of_to_plat(struct udevice *dev) |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 54 | { |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 55 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 56 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 57 | struct dwmci_host *host = &priv->host; |
| 58 | |
| 59 | host->name = dev->name; |
Philipp Tomsich | ff78881 | 2017-09-11 22:04:15 +0200 | [diff] [blame] | 60 | host->ioaddr = dev_read_addr_ptr(dev); |
Philipp Tomsich | 9b4c380 | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 61 | host->buswidth = dev_read_u32_default(dev, "bus-width", 4); |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 62 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 63 | host->priv = dev; |
| 64 | |
huang lin | 8799fc1 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 65 | /* use non-removeable as sdcard and emmc as judgement */ |
Philipp Tomsich | 9b4c380 | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 66 | if (dev_read_bool(dev, "non-removable")) |
huang lin | b06352f | 2016-01-08 14:06:49 +0800 | [diff] [blame] | 67 | host->dev_index = 0; |
| 68 | else |
huang lin | 8799fc1 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 69 | host->dev_index = 1; |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 70 | |
Philipp Tomsich | 9b4c380 | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 71 | priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0); |
| 72 | |
Simon Glass | 4188d94 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 73 | if (priv->fifo_depth < 0) |
| 74 | return -EINVAL; |
Philipp Tomsich | 9b4c380 | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 75 | priv->fifo_mode = dev_read_bool(dev, "fifo-mode"); |
Philipp Tomsich | 56b38d8 | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 76 | |
Heiko Stuebner | 13f1f72 | 2019-11-19 12:04:01 +0100 | [diff] [blame] | 77 | #ifdef CONFIG_SPL_BUILD |
| 78 | if (!priv->fifo_mode) |
| 79 | priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode"); |
| 80 | #endif |
| 81 | |
Philipp Tomsich | 56b38d8 | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 82 | /* |
| 83 | * 'clock-freq-min-max' is deprecated |
| 84 | * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b) |
| 85 | */ |
Philipp Tomsich | 9b4c380 | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 86 | if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) { |
| 87 | int val = dev_read_u32_default(dev, "max-frequency", -EINVAL); |
Philipp Tomsich | 56b38d8 | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 88 | |
| 89 | if (val < 0) |
| 90 | return val; |
| 91 | |
| 92 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 93 | priv->minmax[1] = val; |
| 94 | } else { |
| 95 | debug("%s: 'clock-freq-min-max' property was deprecated.\n", |
| 96 | __func__); |
| 97 | } |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 98 | #endif |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int rockchip_dwmmc_probe(struct udevice *dev) |
| 103 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 104 | struct rockchip_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 105 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 106 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 107 | struct dwmci_host *host = &priv->host; |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 108 | int ret; |
| 109 | |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 110 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 111 | struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat; |
| 112 | |
| 113 | host->name = dev->name; |
| 114 | host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
| 115 | host->buswidth = dtplat->bus_width; |
| 116 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 117 | host->priv = dev; |
| 118 | host->dev_index = 0; |
| 119 | priv->fifo_depth = dtplat->fifo_depth; |
| 120 | priv->fifo_mode = 0; |
Kever Yang | 9708739 | 2017-06-14 16:31:46 +0800 | [diff] [blame] | 121 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 122 | priv->minmax[1] = dtplat->max_frequency; |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 123 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 124 | ret = clk_get_by_driver_info(dev, dtplat->clocks, &priv->clk); |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 125 | if (ret < 0) |
| 126 | return ret; |
| 127 | #else |
Kever Yang | a70d1ea | 2017-06-14 16:31:49 +0800 | [diff] [blame] | 128 | ret = clk_get_by_index(dev, 0, &priv->clk); |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 129 | if (ret < 0) |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 130 | return ret; |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 131 | #endif |
huang lin | b1b71cd | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 132 | host->fifoth_val = MSIZE(0x2) | |
Simon Glass | 4188d94 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 133 | RX_WMARK(priv->fifo_depth / 2 - 1) | |
| 134 | TX_WMARK(priv->fifo_depth / 2); |
huang lin | b1b71cd | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 135 | |
Simon Glass | 4188d94 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 136 | host->fifo_mode = priv->fifo_mode; |
huang lin | b1b71cd | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 137 | |
Jaehoon Chung | c0674d4 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 138 | #ifdef CONFIG_MMC_PWRSEQ |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 139 | /* Enable power if needed */ |
Jaehoon Chung | c0674d4 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 140 | ret = mmc_pwrseq_get_power(dev, &plat->cfg); |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 141 | if (!ret) { |
Jaehoon Chung | c0674d4 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 142 | ret = pwrseq_set_power(plat->cfg.pwr_dev, true); |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 143 | if (ret) |
| 144 | return ret; |
| 145 | } |
| 146 | #endif |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 147 | dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]); |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 148 | host->mmc = &plat->mmc; |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 149 | host->mmc->priv = &priv->host; |
Simon Glass | 77ca42b | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 150 | host->mmc->dev = dev; |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 151 | upriv->mmc = host->mmc; |
| 152 | |
Simon Glass | faeef3b | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 153 | return dwmci_probe(dev); |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 156 | static int rockchip_dwmmc_bind(struct udevice *dev) |
| 157 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 158 | struct rockchip_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 159 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 160 | return dwmci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 161 | } |
| 162 | |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 163 | static const struct udevice_id rockchip_dwmmc_ids[] = { |
Heiko Stuebner | 52c55a2 | 2018-09-21 10:59:46 +0200 | [diff] [blame] | 164 | { .compatible = "rockchip,rk2928-dw-mshc" }, |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 165 | { .compatible = "rockchip,rk3288-dw-mshc" }, |
| 166 | { } |
| 167 | }; |
| 168 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 169 | U_BOOT_DRIVER(rockchip_rk3288_dw_mshc) = { |
Simon Glass | 4bb9ce4 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 170 | .name = "rockchip_rk3288_dw_mshc", |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 171 | .id = UCLASS_MMC, |
| 172 | .of_match = rockchip_dwmmc_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 173 | .of_to_plat = rockchip_dwmmc_of_to_plat, |
Simon Glass | faeef3b | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 174 | .ops = &dm_dwmci_ops, |
Simon Glass | ae69610 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 175 | .bind = rockchip_dwmmc_bind, |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 176 | .probe = rockchip_dwmmc_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 177 | .priv_auto = sizeof(struct rockchip_dwmmc_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 178 | .plat_auto = sizeof(struct rockchip_mmc_plat), |
Simon Glass | 4ecaa6d | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 179 | }; |
Simon Glass | 947fd98 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 180 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 181 | DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3328_dw_mshc) |
| 182 | DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3368_dw_mshc) |