Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 10 | #include <dt-structs.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 11 | #include <dwmmc.h> |
| 12 | #include <errno.h> |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 14 | #include <pwrseq.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 15 | #include <syscon.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 17 | #include <asm/arch/clock.h> |
| 18 | #include <asm/arch/periph.h> |
| 19 | #include <linux/err.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 23 | struct rockchip_mmc_plat { |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 24 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 25 | struct dtd_rockchip_rk3288_dw_mshc dtplat; |
| 26 | #endif |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 27 | struct mmc_config cfg; |
| 28 | struct mmc mmc; |
| 29 | }; |
| 30 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 31 | struct rockchip_dwmmc_priv { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 32 | struct clk clk; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 33 | struct dwmci_host host; |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 34 | int fifo_depth; |
| 35 | bool fifo_mode; |
| 36 | u32 minmax[2]; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq) |
| 40 | { |
| 41 | struct udevice *dev = host->priv; |
| 42 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 43 | int ret; |
| 44 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 45 | ret = clk_set_rate(&priv->clk, freq); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 46 | if (ret < 0) { |
Kever Yang | 419b080 | 2017-06-14 16:31:49 +0800 | [diff] [blame] | 47 | debug("%s: err=%d\n", __func__, ret); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | return freq; |
| 52 | } |
| 53 | |
| 54 | static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev) |
| 55 | { |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 56 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 57 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 58 | struct dwmci_host *host = &priv->host; |
| 59 | |
| 60 | host->name = dev->name; |
Philipp Tomsich | be5f04e | 2017-09-11 22:04:15 +0200 | [diff] [blame] | 61 | host->ioaddr = dev_read_addr_ptr(dev); |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 62 | host->buswidth = dev_read_u32_default(dev, "bus-width", 4); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 63 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 64 | host->priv = dev; |
| 65 | |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 66 | /* use non-removeable as sdcard and emmc as judgement */ |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 67 | if (dev_read_bool(dev, "non-removable")) |
huang lin | 6579385 | 2016-01-08 14:06:49 +0800 | [diff] [blame] | 68 | host->dev_index = 0; |
| 69 | else |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 70 | host->dev_index = 1; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 71 | |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 72 | priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0); |
| 73 | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 74 | if (priv->fifo_depth < 0) |
| 75 | return -EINVAL; |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 76 | priv->fifo_mode = dev_read_bool(dev, "fifo-mode"); |
Philipp Tomsich | ff71f9a | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * 'clock-freq-min-max' is deprecated |
| 80 | * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b) |
| 81 | */ |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 82 | if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) { |
| 83 | int val = dev_read_u32_default(dev, "max-frequency", -EINVAL); |
Philipp Tomsich | ff71f9a | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 84 | |
| 85 | if (val < 0) |
| 86 | return val; |
| 87 | |
| 88 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 89 | priv->minmax[1] = val; |
| 90 | } else { |
| 91 | debug("%s: 'clock-freq-min-max' property was deprecated.\n", |
| 92 | __func__); |
| 93 | } |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 94 | #endif |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int rockchip_dwmmc_probe(struct udevice *dev) |
| 99 | { |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 100 | struct rockchip_mmc_plat *plat = dev_get_platdata(dev); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 101 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 102 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 103 | struct dwmci_host *host = &priv->host; |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 104 | struct udevice *pwr_dev __maybe_unused; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 105 | int ret; |
| 106 | |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 107 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 108 | struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat; |
| 109 | |
| 110 | host->name = dev->name; |
| 111 | host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
| 112 | host->buswidth = dtplat->bus_width; |
| 113 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 114 | host->priv = dev; |
| 115 | host->dev_index = 0; |
| 116 | priv->fifo_depth = dtplat->fifo_depth; |
| 117 | priv->fifo_mode = 0; |
Kever Yang | 8093529 | 2017-06-14 16:31:46 +0800 | [diff] [blame] | 118 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 119 | priv->minmax[1] = dtplat->max_frequency; |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 120 | |
| 121 | ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk); |
| 122 | if (ret < 0) |
| 123 | return ret; |
| 124 | #else |
Kever Yang | 419b080 | 2017-06-14 16:31:49 +0800 | [diff] [blame] | 125 | ret = clk_get_by_index(dev, 0, &priv->clk); |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 126 | if (ret < 0) |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 127 | return ret; |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 128 | #endif |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 129 | host->fifoth_val = MSIZE(0x2) | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 130 | RX_WMARK(priv->fifo_depth / 2 - 1) | |
| 131 | TX_WMARK(priv->fifo_depth / 2); |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 132 | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 133 | host->fifo_mode = priv->fifo_mode; |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 134 | |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 135 | #ifdef CONFIG_PWRSEQ |
| 136 | /* Enable power if needed */ |
| 137 | ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq", |
| 138 | &pwr_dev); |
| 139 | if (!ret) { |
| 140 | ret = pwrseq_set_power(pwr_dev, true); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | } |
| 144 | #endif |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 145 | dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 146 | host->mmc = &plat->mmc; |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 147 | host->mmc->priv = &priv->host; |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 148 | host->mmc->dev = dev; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 149 | upriv->mmc = host->mmc; |
| 150 | |
Simon Glass | 42b37d8 | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 151 | return dwmci_probe(dev); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 152 | } |
| 153 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 154 | static int rockchip_dwmmc_bind(struct udevice *dev) |
| 155 | { |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 156 | struct rockchip_mmc_plat *plat = dev_get_platdata(dev); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 157 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 158 | return dwmci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 161 | static const struct udevice_id rockchip_dwmmc_ids[] = { |
| 162 | { .compatible = "rockchip,rk3288-dw-mshc" }, |
| 163 | { } |
| 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(rockchip_dwmmc_drv) = { |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 167 | .name = "rockchip_rk3288_dw_mshc", |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 168 | .id = UCLASS_MMC, |
| 169 | .of_match = rockchip_dwmmc_ids, |
| 170 | .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata, |
Simon Glass | 42b37d8 | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 171 | .ops = &dm_dwmci_ops, |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 172 | .bind = rockchip_dwmmc_bind, |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 173 | .probe = rockchip_dwmmc_probe, |
| 174 | .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv), |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 175 | .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat), |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 176 | }; |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 177 | |
| 178 | #ifdef CONFIG_PWRSEQ |
| 179 | static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable) |
| 180 | { |
| 181 | struct gpio_desc reset; |
| 182 | int ret; |
| 183 | |
| 184 | ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | dm_gpio_set_value(&reset, 1); |
| 188 | udelay(1); |
| 189 | dm_gpio_set_value(&reset, 0); |
| 190 | udelay(200); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = { |
| 196 | .set_power = rockchip_dwmmc_pwrseq_set_power, |
| 197 | }; |
| 198 | |
| 199 | static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = { |
| 200 | { .compatible = "mmc-pwrseq-emmc" }, |
| 201 | { } |
| 202 | }; |
| 203 | |
| 204 | U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = { |
| 205 | .name = "mmc_pwrseq_emmc", |
| 206 | .id = UCLASS_PWRSEQ, |
| 207 | .of_match = rockchip_dwmmc_pwrseq_ids, |
| 208 | .ops = &rockchip_dwmmc_pwrseq_ops, |
| 209 | }; |
| 210 | #endif |