Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 SAMSUNG Electronics |
| 3 | * Jaehoon Chung <jh80.chung@samsung.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame^] | 9 | #include <dm.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <sdhci.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <libfdt.h> |
| 14 | #include <asm/gpio.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 15 | #include <asm/arch/mmc.h> |
Jaehoon Chung | b1929ea | 2012-08-30 16:24:11 +0000 | [diff] [blame] | 16 | #include <asm/arch/clk.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 17 | #include <errno.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 18 | #include <asm/arch/pinmux.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 19 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame^] | 20 | #ifdef CONFIG_DM_MMC |
| 21 | struct s5p_sdhci_plat { |
| 22 | struct mmc_config cfg; |
| 23 | struct mmc mmc; |
| 24 | }; |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | #endif |
| 28 | |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 29 | static char *S5P_NAME = "SAMSUNG SDHCI"; |
| 30 | static void s5p_sdhci_set_control_reg(struct sdhci_host *host) |
| 31 | { |
| 32 | unsigned long val, ctrl; |
| 33 | /* |
| 34 | * SELCLKPADDS[17:16] |
| 35 | * 00 = 2mA |
| 36 | * 01 = 4mA |
| 37 | * 10 = 7mA |
| 38 | * 11 = 9mA |
| 39 | */ |
| 40 | sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4); |
| 41 | |
| 42 | val = sdhci_readl(host, SDHCI_CONTROL2); |
Matt Reimer | 9af8f39 | 2015-02-23 14:52:22 -0700 | [diff] [blame] | 43 | val &= SDHCI_CTRL2_SELBASECLK_MASK(3); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 44 | |
| 45 | val |= SDHCI_CTRL2_ENSTAASYNCCLR | |
| 46 | SDHCI_CTRL2_ENCMDCNFMSK | |
| 47 | SDHCI_CTRL2_ENFBCLKRX | |
| 48 | SDHCI_CTRL2_ENCLKOUTHOLD; |
| 49 | |
| 50 | sdhci_writel(host, val, SDHCI_CONTROL2); |
| 51 | |
| 52 | /* |
| 53 | * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7] |
| 54 | * FCSel[1:0] : Rx Feedback Clock Delay Control |
| 55 | * Inverter delay means10ns delay if SDCLK 50MHz setting |
| 56 | * 01 = Delay1 (basic delay) |
| 57 | * 11 = Delay2 (basic delay + 2ns) |
| 58 | * 00 = Delay3 (inverter delay) |
| 59 | * 10 = Delay4 (inverter delay + 2ns) |
| 60 | */ |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 61 | val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 62 | sdhci_writel(host, val, SDHCI_CONTROL3); |
| 63 | |
| 64 | /* |
| 65 | * SELBASECLK[5:4] |
| 66 | * 00/01 = HCLK |
| 67 | * 10 = EPLL |
| 68 | * 11 = XTI or XEXTCLK |
| 69 | */ |
| 70 | ctrl = sdhci_readl(host, SDHCI_CONTROL2); |
| 71 | ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3); |
| 72 | ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2); |
| 73 | sdhci_writel(host, ctrl, SDHCI_CONTROL2); |
| 74 | } |
| 75 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 76 | static int s5p_sdhci_core_init(struct sdhci_host *host) |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 77 | { |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 78 | host->name = S5P_NAME; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 79 | |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 80 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE | |
Jaehoon Chung | a2f94cd | 2016-07-12 21:18:47 +0900 | [diff] [blame] | 81 | SDHCI_QUIRK_32BIT_DMA_ADDR | |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 82 | SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 83 | host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 84 | host->version = sdhci_readw(host, SDHCI_HOST_VERSION); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 85 | |
| 86 | host->set_control_reg = &s5p_sdhci_set_control_reg; |
Jaehoon Chung | b1929ea | 2012-08-30 16:24:11 +0000 | [diff] [blame] | 87 | host->set_clock = set_mmc_clk; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 88 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 89 | if (host->bus_width == 8) |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 90 | host->host_caps |= MMC_MODE_8BIT; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 91 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame^] | 92 | #ifndef CONFIG_BLK |
Jaehoon Chung | 0c3c255 | 2012-12-13 20:07:12 +0000 | [diff] [blame] | 93 | return add_sdhci(host, 52000000, 400000); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame^] | 94 | #else |
| 95 | return 0; |
| 96 | #endif |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 97 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 98 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 99 | int s5p_sdhci_init(u32 regbase, int index, int bus_width) |
| 100 | { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 101 | struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host)); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 102 | if (!host) { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 103 | printf("sdhci__host allocation fail!\n"); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 104 | return 1; |
| 105 | } |
| 106 | host->ioaddr = (void *)regbase; |
| 107 | host->index = index; |
| 108 | host->bus_width = bus_width; |
| 109 | |
| 110 | return s5p_sdhci_core_init(host); |
| 111 | } |
| 112 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 113 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 114 | struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS]; |
| 115 | |
| 116 | static int do_sdhci_init(struct sdhci_host *host) |
| 117 | { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 118 | int dev_id, flag, ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 119 | |
| 120 | flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE; |
| 121 | dev_id = host->index + PERIPH_ID_SDMMC0; |
| 122 | |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 123 | ret = exynos_pinmux_config(dev_id, flag); |
| 124 | if (ret) { |
| 125 | printf("external SD not configured\n"); |
| 126 | return ret; |
| 127 | } |
| 128 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 129 | if (dm_gpio_is_valid(&host->pwr_gpio)) { |
| 130 | dm_gpio_set_value(&host->pwr_gpio, 1); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 131 | ret = exynos_pinmux_config(dev_id, flag); |
| 132 | if (ret) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 133 | debug("MMC not configured\n"); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 134 | return ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 138 | if (dm_gpio_is_valid(&host->cd_gpio)) { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 139 | ret = dm_gpio_get_value(&host->cd_gpio); |
| 140 | if (ret) { |
| 141 | debug("no SD card detected (%d)\n", ret); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 142 | return -ENODEV; |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 143 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 146 | return s5p_sdhci_core_init(host); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host) |
| 150 | { |
| 151 | int bus_width, dev_id; |
| 152 | unsigned int base; |
| 153 | |
| 154 | /* Get device id */ |
| 155 | dev_id = pinmux_decode_periph_id(blob, node); |
| 156 | if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) { |
| 157 | debug("MMC: Can't get device id\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | host->index = dev_id - PERIPH_ID_SDMMC0; |
| 161 | |
| 162 | /* Get bus width */ |
| 163 | bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0); |
| 164 | if (bus_width <= 0) { |
| 165 | debug("MMC: Can't get bus-width\n"); |
| 166 | return -1; |
| 167 | } |
| 168 | host->bus_width = bus_width; |
| 169 | |
| 170 | /* Get the base address from the device node */ |
| 171 | base = fdtdec_get_addr(blob, node, "reg"); |
| 172 | if (!base) { |
| 173 | debug("MMC: Can't get base address\n"); |
| 174 | return -1; |
| 175 | } |
| 176 | host->ioaddr = (void *)base; |
| 177 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 178 | gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio, |
| 179 | GPIOD_IS_OUT); |
| 180 | gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio, |
| 181 | GPIOD_IS_IN); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int process_nodes(const void *blob, int node_list[], int count) |
| 187 | { |
| 188 | struct sdhci_host *host; |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 189 | int i, node, ret; |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 190 | int failed = 0; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 191 | |
| 192 | debug("%s: count = %d\n", __func__, count); |
| 193 | |
| 194 | /* build sdhci_host[] for each controller */ |
| 195 | for (i = 0; i < count; i++) { |
| 196 | node = node_list[i]; |
| 197 | if (node <= 0) |
| 198 | continue; |
| 199 | |
| 200 | host = &sdhci_host[i]; |
| 201 | |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 202 | ret = sdhci_get_config(blob, node, host); |
| 203 | if (ret) { |
| 204 | printf("%s: failed to decode dev %d (%d)\n", __func__, i, ret); |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 205 | failed++; |
| 206 | continue; |
| 207 | } |
| 208 | |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 209 | ret = do_sdhci_init(host); |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 210 | if (ret && ret != -ENODEV) { |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 211 | printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret); |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 212 | failed++; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 213 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 214 | } |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 215 | |
| 216 | /* we only consider it an error when all nodes fail */ |
| 217 | return (failed == count ? -1 : 0); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int exynos_mmc_init(const void *blob) |
| 221 | { |
| 222 | int count; |
| 223 | int node_list[SDHCI_MAX_HOSTS]; |
| 224 | |
| 225 | count = fdtdec_find_aliases_for_id(blob, "mmc", |
| 226 | COMPAT_SAMSUNG_EXYNOS_MMC, node_list, |
| 227 | SDHCI_MAX_HOSTS); |
| 228 | |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 229 | return process_nodes(blob, node_list, count); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 230 | } |
| 231 | #endif |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame^] | 232 | |
| 233 | #ifdef CONFIG_DM_MMC |
| 234 | static int s5p_sdhci_probe(struct udevice *dev) |
| 235 | { |
| 236 | struct s5p_sdhci_plat *plat = dev_get_platdata(dev); |
| 237 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 238 | struct sdhci_host *host = dev_get_priv(dev); |
| 239 | int ret; |
| 240 | |
| 241 | ret = sdhci_get_config(gd->fdt_blob, dev->of_offset, host); |
| 242 | if (ret) |
| 243 | return ret; |
| 244 | |
| 245 | ret = do_sdhci_init(host); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | ret = sdhci_setup_cfg(&plat->cfg, host, 52000000, 400000); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | host->mmc = &plat->mmc; |
| 254 | host->mmc->priv = host; |
| 255 | host->mmc->dev = dev; |
| 256 | upriv->mmc = host->mmc; |
| 257 | |
| 258 | return sdhci_probe(dev); |
| 259 | } |
| 260 | |
| 261 | static int s5p_sdhci_bind(struct udevice *dev) |
| 262 | { |
| 263 | struct s5p_sdhci_plat *plat = dev_get_platdata(dev); |
| 264 | int ret; |
| 265 | |
| 266 | ret = sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static const struct udevice_id s5p_sdhci_ids[] = { |
| 274 | { .compatible = "samsung,exynos4412-sdhci"}, |
| 275 | { } |
| 276 | }; |
| 277 | |
| 278 | U_BOOT_DRIVER(s5p_sdhci_drv) = { |
| 279 | .name = "s5p_sdhci", |
| 280 | .id = UCLASS_MMC, |
| 281 | .of_match = s5p_sdhci_ids, |
| 282 | .bind = s5p_sdhci_bind, |
| 283 | .ops = &sdhci_ops, |
| 284 | .probe = s5p_sdhci_probe, |
| 285 | .priv_auto_alloc_size = sizeof(struct sdhci_host), |
| 286 | .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat), |
| 287 | }; |
| 288 | #endif /* CONFIG_DM_MMC */ |