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 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <sdhci.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 11 | #include <fdtdec.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 13 | #include <linux/libfdt.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 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 | d4cb6ba | 2016-12-30 15:30:17 +0900 | [diff] [blame] | 76 | static void s5p_set_clock(struct sdhci_host *host, u32 div) |
| 77 | { |
| 78 | /* ToDo : Use the Clock Framework */ |
| 79 | set_mmc_clk(host->index, div); |
| 80 | } |
| 81 | |
Jaehoon Chung | 46d3c03 | 2016-12-30 15:30:18 +0900 | [diff] [blame] | 82 | static const struct sdhci_ops s5p_sdhci_ops = { |
| 83 | .set_clock = &s5p_set_clock, |
| 84 | .set_control_reg = &s5p_sdhci_set_control_reg, |
| 85 | }; |
| 86 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 87 | static int s5p_sdhci_core_init(struct sdhci_host *host) |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 88 | { |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 89 | host->name = S5P_NAME; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 90 | |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 91 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE | |
Henrik Grimler | 021d1cf | 2023-05-09 21:05:46 +0200 | [diff] [blame] | 92 | SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR | |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 93 | SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 94 | host->max_clk = 52000000; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 95 | 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] | 96 | host->ops = &s5p_sdhci_ops; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 97 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 98 | if (host->bus_width == 8) |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 99 | host->host_caps |= MMC_MODE_8BIT; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 100 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 101 | #ifndef CONFIG_BLK |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 102 | return add_sdhci(host, 0, 400000); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 103 | #else |
| 104 | return 0; |
| 105 | #endif |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 106 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 107 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 108 | int s5p_sdhci_init(u32 regbase, int index, int bus_width) |
| 109 | { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 110 | struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host)); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 111 | if (!host) { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 112 | printf("sdhci__host allocation fail!\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 113 | return -ENOMEM; |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 114 | } |
| 115 | host->ioaddr = (void *)regbase; |
| 116 | host->index = index; |
| 117 | host->bus_width = bus_width; |
| 118 | |
| 119 | return s5p_sdhci_core_init(host); |
| 120 | } |
| 121 | |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 122 | static int do_sdhci_init(struct sdhci_host *host) |
| 123 | { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 124 | int dev_id, flag, ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 125 | |
| 126 | flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE; |
| 127 | dev_id = host->index + PERIPH_ID_SDMMC0; |
| 128 | |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 129 | ret = exynos_pinmux_config(dev_id, flag); |
| 130 | if (ret) { |
| 131 | printf("external SD not configured\n"); |
| 132 | return ret; |
| 133 | } |
| 134 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 135 | if (dm_gpio_is_valid(&host->pwr_gpio)) { |
| 136 | dm_gpio_set_value(&host->pwr_gpio, 1); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 137 | ret = exynos_pinmux_config(dev_id, flag); |
| 138 | if (ret) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 139 | debug("MMC not configured\n"); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 140 | return ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 144 | if (dm_gpio_is_valid(&host->cd_gpio)) { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 145 | ret = dm_gpio_get_value(&host->cd_gpio); |
| 146 | if (ret) { |
| 147 | debug("no SD card detected (%d)\n", ret); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 148 | return -ENODEV; |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 149 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 152 | return s5p_sdhci_core_init(host); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host) |
| 156 | { |
| 157 | int bus_width, dev_id; |
| 158 | unsigned int base; |
| 159 | |
| 160 | /* Get device id */ |
| 161 | dev_id = pinmux_decode_periph_id(blob, node); |
Seung-Woo Kim | 6c5d79a | 2016-11-24 15:05:51 +0900 | [diff] [blame] | 162 | if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 163 | debug("MMC: Can't get device id\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 164 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 165 | } |
| 166 | host->index = dev_id - PERIPH_ID_SDMMC0; |
| 167 | |
| 168 | /* Get bus width */ |
| 169 | bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0); |
| 170 | if (bus_width <= 0) { |
| 171 | debug("MMC: Can't get bus-width\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 172 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 173 | } |
| 174 | host->bus_width = bus_width; |
| 175 | |
| 176 | /* Get the base address from the device node */ |
| 177 | base = fdtdec_get_addr(blob, node, "reg"); |
| 178 | if (!base) { |
| 179 | debug("MMC: Can't get base address\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 180 | return -EINVAL; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 181 | } |
| 182 | host->ioaddr = (void *)base; |
| 183 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 184 | gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0, |
| 185 | &host->pwr_gpio, GPIOD_IS_OUT); |
| 186 | gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0, |
| 187 | &host->cd_gpio, GPIOD_IS_IN); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 191 | |
| 192 | #ifdef CONFIG_DM_MMC |
| 193 | static int s5p_sdhci_probe(struct udevice *dev) |
| 194 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 195 | struct s5p_sdhci_plat *plat = dev_get_plat(dev); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 196 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 197 | struct sdhci_host *host = dev_get_priv(dev); |
| 198 | int ret; |
| 199 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 200 | ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | ret = do_sdhci_init(host); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Marek Szyprowski | e404f26 | 2020-01-16 16:25:33 +0100 | [diff] [blame] | 208 | ret = mmc_of_parse(dev, &plat->cfg); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
Peng Fan | 34f6c60 | 2019-08-06 02:47:59 +0000 | [diff] [blame] | 212 | host->mmc = &plat->mmc; |
| 213 | host->mmc->dev = dev; |
Marek Szyprowski | e404f26 | 2020-01-16 16:25:33 +0100 | [diff] [blame] | 214 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 215 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 216 | if (ret) |
| 217 | return ret; |
| 218 | |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 219 | host->mmc->priv = host; |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 220 | upriv->mmc = host->mmc; |
| 221 | |
| 222 | return sdhci_probe(dev); |
| 223 | } |
| 224 | |
| 225 | static int s5p_sdhci_bind(struct udevice *dev) |
| 226 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 227 | struct s5p_sdhci_plat *plat = dev_get_plat(dev); |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 228 | int ret; |
| 229 | |
| 230 | ret = sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static const struct udevice_id s5p_sdhci_ids[] = { |
| 238 | { .compatible = "samsung,exynos4412-sdhci"}, |
| 239 | { } |
| 240 | }; |
| 241 | |
| 242 | U_BOOT_DRIVER(s5p_sdhci_drv) = { |
| 243 | .name = "s5p_sdhci", |
| 244 | .id = UCLASS_MMC, |
| 245 | .of_match = s5p_sdhci_ids, |
| 246 | .bind = s5p_sdhci_bind, |
| 247 | .ops = &sdhci_ops, |
| 248 | .probe = s5p_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 249 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 250 | .plat_auto = sizeof(struct s5p_sdhci_plat), |
Jaehoon Chung | 990a937 | 2016-09-09 18:23:23 +0900 | [diff] [blame] | 251 | }; |
| 252 | #endif /* CONFIG_DM_MMC */ |