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