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> |
| 9 | #include <malloc.h> |
| 10 | #include <sdhci.h> |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 11 | #include <fdtdec.h> |
| 12 | #include <libfdt.h> |
| 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 | |
| 19 | static char *S5P_NAME = "SAMSUNG SDHCI"; |
| 20 | static void s5p_sdhci_set_control_reg(struct sdhci_host *host) |
| 21 | { |
| 22 | unsigned long val, ctrl; |
| 23 | /* |
| 24 | * SELCLKPADDS[17:16] |
| 25 | * 00 = 2mA |
| 26 | * 01 = 4mA |
| 27 | * 10 = 7mA |
| 28 | * 11 = 9mA |
| 29 | */ |
| 30 | sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4); |
| 31 | |
| 32 | val = sdhci_readl(host, SDHCI_CONTROL2); |
Matt Reimer | 9af8f39 | 2015-02-23 14:52:22 -0700 | [diff] [blame] | 33 | val &= SDHCI_CTRL2_SELBASECLK_MASK(3); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 34 | |
| 35 | val |= SDHCI_CTRL2_ENSTAASYNCCLR | |
| 36 | SDHCI_CTRL2_ENCMDCNFMSK | |
| 37 | SDHCI_CTRL2_ENFBCLKRX | |
| 38 | SDHCI_CTRL2_ENCLKOUTHOLD; |
| 39 | |
| 40 | sdhci_writel(host, val, SDHCI_CONTROL2); |
| 41 | |
| 42 | /* |
| 43 | * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7] |
| 44 | * FCSel[1:0] : Rx Feedback Clock Delay Control |
| 45 | * Inverter delay means10ns delay if SDCLK 50MHz setting |
| 46 | * 01 = Delay1 (basic delay) |
| 47 | * 11 = Delay2 (basic delay + 2ns) |
| 48 | * 00 = Delay3 (inverter delay) |
| 49 | * 10 = Delay4 (inverter delay + 2ns) |
| 50 | */ |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 51 | val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 52 | sdhci_writel(host, val, SDHCI_CONTROL3); |
| 53 | |
| 54 | /* |
| 55 | * SELBASECLK[5:4] |
| 56 | * 00/01 = HCLK |
| 57 | * 10 = EPLL |
| 58 | * 11 = XTI or XEXTCLK |
| 59 | */ |
| 60 | ctrl = sdhci_readl(host, SDHCI_CONTROL2); |
| 61 | ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3); |
| 62 | ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2); |
| 63 | sdhci_writel(host, ctrl, SDHCI_CONTROL2); |
| 64 | } |
| 65 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 66 | static int s5p_sdhci_core_init(struct sdhci_host *host) |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 67 | { |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 68 | host->name = S5P_NAME; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 69 | |
Jaehoon Chung | b36cbac | 2012-08-30 16:24:08 +0000 | [diff] [blame] | 70 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE | |
Tushar Behera | 0fba4c2 | 2012-09-20 20:31:57 +0000 | [diff] [blame] | 71 | SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR | |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 72 | SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 73 | 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] | 74 | host->version = sdhci_readw(host, SDHCI_HOST_VERSION); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 75 | |
| 76 | host->set_control_reg = &s5p_sdhci_set_control_reg; |
Jaehoon Chung | b1929ea | 2012-08-30 16:24:11 +0000 | [diff] [blame] | 77 | host->set_clock = set_mmc_clk; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 78 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 79 | if (host->bus_width == 8) |
Jaehoon Chung | 46e627c | 2013-07-19 17:44:49 +0900 | [diff] [blame] | 80 | host->host_caps |= MMC_MODE_8BIT; |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 81 | |
Jaehoon Chung | 0c3c255 | 2012-12-13 20:07:12 +0000 | [diff] [blame] | 82 | return add_sdhci(host, 52000000, 400000); |
Jaehoon Chung | b0b0c9d | 2012-04-23 02:36:28 +0000 | [diff] [blame] | 83 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 84 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 85 | int s5p_sdhci_init(u32 regbase, int index, int bus_width) |
| 86 | { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 87 | struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host)); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 88 | if (!host) { |
Tobias Jakobi | 49b330d | 2015-10-05 13:47:50 +0200 | [diff] [blame] | 89 | printf("sdhci__host allocation fail!\n"); |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | host->ioaddr = (void *)regbase; |
| 93 | host->index = index; |
| 94 | host->bus_width = bus_width; |
| 95 | |
| 96 | return s5p_sdhci_core_init(host); |
| 97 | } |
| 98 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 99 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 100 | struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS]; |
| 101 | |
| 102 | static int do_sdhci_init(struct sdhci_host *host) |
| 103 | { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 104 | int dev_id, flag, ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 105 | |
| 106 | flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE; |
| 107 | dev_id = host->index + PERIPH_ID_SDMMC0; |
| 108 | |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 109 | ret = exynos_pinmux_config(dev_id, flag); |
| 110 | if (ret) { |
| 111 | printf("external SD not configured\n"); |
| 112 | return ret; |
| 113 | } |
| 114 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 115 | if (dm_gpio_is_valid(&host->pwr_gpio)) { |
| 116 | dm_gpio_set_value(&host->pwr_gpio, 1); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 117 | ret = exynos_pinmux_config(dev_id, flag); |
| 118 | if (ret) { |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 119 | debug("MMC not configured\n"); |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 120 | return ret; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 124 | if (dm_gpio_is_valid(&host->cd_gpio)) { |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 125 | ret = dm_gpio_get_value(&host->cd_gpio); |
| 126 | if (ret) { |
| 127 | debug("no SD card detected (%d)\n", ret); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 128 | return -ENODEV; |
Tobias Jakobi | c29121b | 2015-10-05 13:47:53 +0200 | [diff] [blame] | 129 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Jaehoon Chung | b494ac8 | 2014-05-16 13:59:59 +0900 | [diff] [blame] | 132 | return s5p_sdhci_core_init(host); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host) |
| 136 | { |
| 137 | int bus_width, dev_id; |
| 138 | unsigned int base; |
| 139 | |
| 140 | /* Get device id */ |
| 141 | dev_id = pinmux_decode_periph_id(blob, node); |
| 142 | if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) { |
| 143 | debug("MMC: Can't get device id\n"); |
| 144 | return -1; |
| 145 | } |
| 146 | host->index = dev_id - PERIPH_ID_SDMMC0; |
| 147 | |
| 148 | /* Get bus width */ |
| 149 | bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0); |
| 150 | if (bus_width <= 0) { |
| 151 | debug("MMC: Can't get bus-width\n"); |
| 152 | return -1; |
| 153 | } |
| 154 | host->bus_width = bus_width; |
| 155 | |
| 156 | /* Get the base address from the device node */ |
| 157 | base = fdtdec_get_addr(blob, node, "reg"); |
| 158 | if (!base) { |
| 159 | debug("MMC: Can't get base address\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | host->ioaddr = (void *)base; |
| 163 | |
Simon Glass | a30d4ba | 2015-01-05 20:05:38 -0700 | [diff] [blame] | 164 | gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio, |
| 165 | GPIOD_IS_OUT); |
| 166 | gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio, |
| 167 | GPIOD_IS_IN); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int process_nodes(const void *blob, int node_list[], int count) |
| 173 | { |
| 174 | struct sdhci_host *host; |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 175 | int i, node, ret; |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 176 | int failed = 0; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 177 | |
| 178 | debug("%s: count = %d\n", __func__, count); |
| 179 | |
| 180 | /* build sdhci_host[] for each controller */ |
| 181 | for (i = 0; i < count; i++) { |
| 182 | node = node_list[i]; |
| 183 | if (node <= 0) |
| 184 | continue; |
| 185 | |
| 186 | host = &sdhci_host[i]; |
| 187 | |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 188 | ret = sdhci_get_config(blob, node, host); |
| 189 | if (ret) { |
| 190 | printf("%s: failed to decode dev %d (%d)\n", __func__, i, ret); |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 191 | failed++; |
| 192 | continue; |
| 193 | } |
| 194 | |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 195 | ret = do_sdhci_init(host); |
Przemyslaw Marczak | 44468ef | 2015-10-28 15:41:50 +0100 | [diff] [blame] | 196 | if (ret && ret != -ENODEV) { |
Tobias Jakobi | 953d21b | 2015-10-05 13:47:52 +0200 | [diff] [blame] | 197 | printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret); |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 198 | failed++; |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 199 | } |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 200 | } |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 201 | |
| 202 | /* we only consider it an error when all nodes fail */ |
| 203 | return (failed == count ? -1 : 0); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int exynos_mmc_init(const void *blob) |
| 207 | { |
| 208 | int count; |
| 209 | int node_list[SDHCI_MAX_HOSTS]; |
| 210 | |
| 211 | count = fdtdec_find_aliases_for_id(blob, "mmc", |
| 212 | COMPAT_SAMSUNG_EXYNOS_MMC, node_list, |
| 213 | SDHCI_MAX_HOSTS); |
| 214 | |
Tobias Jakobi | 0ab570c | 2015-10-05 13:47:51 +0200 | [diff] [blame] | 215 | return process_nodes(blob, node_list, count); |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 216 | } |
| 217 | #endif |