Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 SAMSUNG Electronics |
| 4 | * Jaehoon Chung <jh80.chung@samsung.com> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 16 | #include <asm/arch/mmc.h> |
Jaehoon Chung | b1929ea | 2012-08-30 16:24:11 +0000 | [diff] [blame] | 17 | #include <asm/arch/clk.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 18 | #include <errno.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 19 | #include <asm/arch/pinmux.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 20 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 21 | #ifdef CONFIG_DM_MMC |
| 22 | struct s5p_sdhci_plat { |
| 23 | struct mmc_config cfg; |
| 24 | struct mmc mmc; |
| 25 | }; |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | #endif |
| 29 | |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 30 | static char *S5P_NAME = "SAMSUNG SDHCI"; |
| 31 | static void s5p_sdhci_set_control_reg(struct sdhci_host *host) |
| 32 | { |
| 33 | unsigned long val, ctrl; |
| 34 | /* |
| 35 | * SELCLKPADDS[17:16] |
| 36 | * 00 = 2mA |
| 37 | * 01 = 4mA |
| 38 | * 10 = 7mA |
| 39 | * 11 = 9mA |
| 40 | */ |
| 41 | sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4); |
| 42 | |
| 43 | val = sdhci_readl(host, SDHCI_CONTROL2); |
Matt Reimer | 9af8f39 | 2015-02-23 14:52:22 -0700 | [diff] [blame] | 44 | val &= SDHCI_CTRL2_SELBASECLK_MASK(3); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 45 | |
| 46 | val |= SDHCI_CTRL2_ENSTAASYNCCLR | |
| 47 | SDHCI_CTRL2_ENCMDCNFMSK | |
| 48 | SDHCI_CTRL2_ENFBCLKRX | |
| 49 | SDHCI_CTRL2_ENCLKOUTHOLD; |
| 50 | |
| 51 | sdhci_writel(host, val, SDHCI_CONTROL2); |
| 52 | |
| 53 | /* |
| 54 | * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7] |
| 55 | * FCSel[1:0] : Rx Feedback Clock Delay Control |
| 56 | * Inverter delay means10ns delay if SDCLK 50MHz setting |
| 57 | * 01 = Delay1 (basic delay) |
| 58 | * 11 = Delay2 (basic delay + 2ns) |
| 59 | * 00 = Delay3 (inverter delay) |
| 60 | * 10 = Delay4 (inverter delay + 2ns) |
| 61 | */ |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 62 | val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 63 | sdhci_writel(host, val, SDHCI_CONTROL3); |
| 64 | |
| 65 | /* |
| 66 | * SELBASECLK[5:4] |
| 67 | * 00/01 = HCLK |
| 68 | * 10 = EPLL |
| 69 | * 11 = XTI or XEXTCLK |
| 70 | */ |
| 71 | ctrl = sdhci_readl(host, SDHCI_CONTROL2); |
| 72 | ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3); |
| 73 | ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2); |
| 74 | sdhci_writel(host, ctrl, SDHCI_CONTROL2); |
| 75 | } |
| 76 | |
Jaehoon Chung | d4cb6ba | 2016-12-30 15:30:17 +0900 | [diff] [blame] | 77 | static void s5p_set_clock(struct sdhci_host *host, u32 div) |
| 78 | { |
| 79 | /* ToDo : Use the Clock Framework */ |
| 80 | set_mmc_clk(host->index, div); |
| 81 | } |
| 82 | |
Jaehoon Chung | 46d3c03 | 2016-12-30 15:30:18 +0900 | [diff] [blame] | 83 | static const struct sdhci_ops s5p_sdhci_ops = { |
| 84 | .set_clock = &s5p_set_clock, |
| 85 | .set_control_reg = &s5p_sdhci_set_control_reg, |
| 86 | }; |
| 87 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 88 | static int s5p_sdhci_core_init(struct sdhci_host *host) |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 89 | { |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 90 | host->name = S5P_NAME; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 91 | |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 92 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE | |
Jaehoon Chung | a2f94cd | 2016-07-12 21:18:47 +0900 | [diff] [blame] | 93 | SDHCI_QUIRK_32BIT_DMA_ADDR | |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 94 | SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 95 | host->max_clk = 52000000; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 96 | host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
Jaehoon Chung | 46d3c03 | 2016-12-30 15:30:18 +0900 | [diff] [blame] | 97 | host->ops = &s5p_sdhci_ops; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 98 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 99 | if (host->bus_width == 8) |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 100 | host->host_caps |= MMC_MODE_8BIT; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 101 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 102 | #ifndef CONFIG_BLK |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 103 | return add_sdhci(host, 0, 400000); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 104 | #else |
| 105 | return 0; |
| 106 | #endif |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 107 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 108 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 109 | int s5p_sdhci_init(u32 regbase, int index, int bus_width) |
| 110 | { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 111 | struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host)); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 112 | if (!host) { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 113 | printf("sdhci__host allocation fail!\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 114 | return -ENOMEM; |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 115 | } |
| 116 | host->ioaddr = (void *)regbase; |
| 117 | host->index = index; |
| 118 | host->bus_width = bus_width; |
| 119 | |
| 120 | return s5p_sdhci_core_init(host); |
| 121 | } |
| 122 | |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 123 | static int do_sdhci_init(struct sdhci_host *host) |
| 124 | { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 125 | int dev_id, flag, ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 126 | |
| 127 | flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE; |
| 128 | dev_id = host->index + PERIPH_ID_SDMMC0; |
| 129 | |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 130 | ret = exynos_pinmux_config(dev_id, flag); |
| 131 | if (ret) { |
| 132 | printf("external SD not configured\n"); |
| 133 | return ret; |
| 134 | } |
| 135 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 136 | if (dm_gpio_is_valid(&host->pwr_gpio)) { |
| 137 | dm_gpio_set_value(&host->pwr_gpio, 1); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 138 | ret = exynos_pinmux_config(dev_id, flag); |
| 139 | if (ret) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 140 | debug("MMC not configured\n"); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 141 | return ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 145 | if (dm_gpio_is_valid(&host->cd_gpio)) { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 146 | ret = dm_gpio_get_value(&host->cd_gpio); |
| 147 | if (ret) { |
| 148 | debug("no SD card detected (%d)\n", ret); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 149 | return -ENODEV; |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 150 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 153 | return s5p_sdhci_core_init(host); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host) |
| 157 | { |
| 158 | int bus_width, dev_id; |
| 159 | unsigned int base; |
| 160 | |
| 161 | /* Get device id */ |
| 162 | dev_id = pinmux_decode_periph_id(blob, node); |
Seung-Woo Kim | 6c5d79a | 2016-11-24 15:05:51 +0900 | [diff] [blame] | 163 | if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 164 | debug("MMC: Can't get device id\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 165 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 166 | } |
| 167 | host->index = dev_id - PERIPH_ID_SDMMC0; |
| 168 | |
| 169 | /* Get bus width */ |
| 170 | bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0); |
| 171 | if (bus_width <= 0) { |
| 172 | debug("MMC: Can't get bus-width\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 173 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 174 | } |
| 175 | host->bus_width = bus_width; |
| 176 | |
| 177 | /* Get the base address from the device node */ |
| 178 | base = fdtdec_get_addr(blob, node, "reg"); |
| 179 | if (!base) { |
| 180 | debug("MMC: Can't get base address\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 181 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 182 | } |
| 183 | host->ioaddr = (void *)base; |
| 184 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 185 | gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0, |
| 186 | &host->pwr_gpio, GPIOD_IS_OUT); |
| 187 | gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0, |
| 188 | &host->cd_gpio, GPIOD_IS_IN); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 192 | |
| 193 | #ifdef CONFIG_DM_MMC |
| 194 | static int s5p_sdhci_probe(struct udevice *dev) |
| 195 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 196 | struct s5p_sdhci_plat *plat = dev_get_plat(dev); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 197 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 198 | struct sdhci_host *host = dev_get_priv(dev); |
| 199 | int ret; |
| 200 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 201 | ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | ret = do_sdhci_init(host); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
Marek Szyprowski | e404f26 | 2020-01-16 16:25:33 +0100 | [diff] [blame] | 209 | ret = mmc_of_parse(dev, &plat->cfg); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
Peng Fan | 34f6c60 | 2019-08-06 02:47:59 +0000 | [diff] [blame] | 213 | host->mmc = &plat->mmc; |
| 214 | host->mmc->dev = dev; |
Marek Szyprowski | e404f26 | 2020-01-16 16:25:33 +0100 | [diff] [blame] | 215 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 216 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 217 | if (ret) |
| 218 | return ret; |
| 219 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 220 | host->mmc->priv = host; |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 221 | upriv->mmc = host->mmc; |
| 222 | |
| 223 | return sdhci_probe(dev); |
| 224 | } |
| 225 | |
| 226 | static int s5p_sdhci_bind(struct udevice *dev) |
| 227 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 228 | struct s5p_sdhci_plat *plat = dev_get_plat(dev); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 229 | int ret; |
| 230 | |
| 231 | ret = sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static const struct udevice_id s5p_sdhci_ids[] = { |
| 239 | { .compatible = "samsung,exynos4412-sdhci"}, |
| 240 | { } |
| 241 | }; |
| 242 | |
| 243 | U_BOOT_DRIVER(s5p_sdhci_drv) = { |
| 244 | .name = "s5p_sdhci", |
| 245 | .id = UCLASS_MMC, |
| 246 | .of_match = s5p_sdhci_ids, |
| 247 | .bind = s5p_sdhci_bind, |
| 248 | .ops = &sdhci_ops, |
| 249 | .probe = s5p_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 250 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 251 | .plat_auto = sizeof(struct s5p_sdhci_plat), |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 252 | }; |
| 253 | #endif /* CONFIG_DM_MMC */ |