Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Atmel Corporation |
| 4 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <sdhci.h> |
| 12 | #include <asm/arch/clk.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 14 | |
| 15 | #define ATMEL_SDHC_MIN_FREQ 400000 |
Ludovic Desroches | e5a07b8 | 2017-11-17 14:51:27 +0800 | [diff] [blame] | 16 | #define ATMEL_SDHC_GCK_RATE 240000000 |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 17 | |
Zixun LI | d46f289 | 2023-05-17 15:49:45 +0200 | [diff] [blame] | 18 | #define ATMEL_SDHC_MC1R 0x204 |
| 19 | #define ATMEL_SDHC_MC1R_FCD 0x80 |
| 20 | |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 21 | #ifndef CONFIG_DM_MMC |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 22 | int atmel_sdhci_init(void *regbase, u32 id) |
| 23 | { |
| 24 | struct sdhci_host *host; |
| 25 | u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ; |
| 26 | |
| 27 | host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host)); |
| 28 | if (!host) { |
| 29 | printf("%s: sdhci_host calloc failed\n", __func__); |
| 30 | return -ENOMEM; |
| 31 | } |
| 32 | |
| 33 | host->name = "atmel_sdhci"; |
| 34 | host->ioaddr = regbase; |
Wenyou Yang | 4fadd97 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 35 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 36 | max_clk = at91_get_periph_generated_clk(id); |
| 37 | if (!max_clk) { |
| 38 | printf("%s: Failed to get the proper clock\n", __func__); |
| 39 | free(host); |
| 40 | return -ENODEV; |
| 41 | } |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 42 | host->max_clk = max_clk; |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 43 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 44 | add_sdhci(host, 0, min_clk); |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 45 | |
| 46 | return 0; |
| 47 | } |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 48 | |
| 49 | #else |
| 50 | |
| 51 | DECLARE_GLOBAL_DATA_PTR; |
| 52 | |
| 53 | struct atmel_sdhci_plat { |
| 54 | struct mmc_config cfg; |
| 55 | struct mmc mmc; |
| 56 | }; |
| 57 | |
Zixun LI | d46f289 | 2023-05-17 15:49:45 +0200 | [diff] [blame] | 58 | static void atmel_sdhci_config_fcd(struct sdhci_host *host) |
| 59 | { |
| 60 | u8 mc1r; |
| 61 | |
| 62 | /* If nonremovable, assume that the card is always present. |
| 63 | * |
| 64 | * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line. |
| 65 | */ |
| 66 | if ((host->mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) |
| 67 | #if CONFIG_IS_ENABLED(DM_GPIO) |
| 68 | || dm_gpio_get_value(&host->cd_gpio) >= 0 |
| 69 | #endif |
| 70 | ) { |
| 71 | sdhci_readb(host, ATMEL_SDHC_MC1R); |
| 72 | mc1r |= ATMEL_SDHC_MC1R_FCD; |
| 73 | sdhci_writeb(host, mc1r, ATMEL_SDHC_MC1R); |
| 74 | } |
| 75 | } |
| 76 | |
Sergiu Moga | e554c3e | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 77 | static int atmel_sdhci_deferred_probe(struct sdhci_host *host) |
| 78 | { |
| 79 | struct udevice *dev = host->mmc->dev; |
Zixun LI | d46f289 | 2023-05-17 15:49:45 +0200 | [diff] [blame] | 80 | int ret; |
Sergiu Moga | e554c3e | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 81 | |
Zixun LI | d46f289 | 2023-05-17 15:49:45 +0200 | [diff] [blame] | 82 | ret = sdhci_probe(dev); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | |
| 86 | atmel_sdhci_config_fcd(host); |
| 87 | |
| 88 | return 0; |
Sergiu Moga | e554c3e | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static const struct sdhci_ops atmel_sdhci_ops = { |
| 92 | .deferred_probe = atmel_sdhci_deferred_probe, |
| 93 | }; |
| 94 | |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 95 | static int atmel_sdhci_probe(struct udevice *dev) |
| 96 | { |
| 97 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 98 | struct atmel_sdhci_plat *plat = dev_get_plat(dev); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 99 | struct sdhci_host *host = dev_get_priv(dev); |
| 100 | u32 max_clk; |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 101 | struct clk clk; |
| 102 | int ret; |
| 103 | |
Wenyou Yang | 038dff8 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 104 | ret = clk_get_by_index(dev, 0, &clk); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | ret = clk_enable(&clk); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | host->name = dev->name; |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 113 | host->ioaddr = dev_read_addr_ptr(dev); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 114 | |
Wenyou Yang | 4fadd97 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 115 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 116 | host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 117 | "bus-width", 4); |
| 118 | |
Wenyou Yang | 038dff8 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 119 | ret = clk_get_by_index(dev, 1, &clk); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 120 | if (ret) |
| 121 | return ret; |
| 122 | |
Eugen Hristev | 90805e1 | 2020-08-27 12:16:15 +0300 | [diff] [blame] | 123 | clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 124 | |
| 125 | max_clk = clk_get_rate(&clk); |
| 126 | if (!max_clk) |
| 127 | return -EINVAL; |
| 128 | |
Eugen Hristev | 3110c75 | 2020-08-27 12:25:56 +0300 | [diff] [blame] | 129 | ret = clk_enable(&clk); |
Eugen Hristev | a563051 | 2020-11-09 13:02:17 +0200 | [diff] [blame] | 130 | /* return error only if the clock really has a clock enable func */ |
| 131 | if (ret && ret != -ENOSYS) |
Eugen Hristev | 3110c75 | 2020-08-27 12:25:56 +0300 | [diff] [blame] | 132 | return ret; |
| 133 | |
Eugen Hristev | 6fb8abf | 2020-08-27 12:25:57 +0300 | [diff] [blame] | 134 | ret = mmc_of_parse(dev, &plat->cfg); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 138 | host->max_clk = max_clk; |
Peng Fan | 9a8f499 | 2019-08-06 02:47:47 +0000 | [diff] [blame] | 139 | host->mmc = &plat->mmc; |
| 140 | host->mmc->dev = dev; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 141 | |
| 142 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 143 | if (ret) |
| 144 | return ret; |
| 145 | |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 146 | host->mmc->priv = host; |
Sergiu Moga | e554c3e | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 147 | host->ops = &atmel_sdhci_ops; |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 148 | upriv->mmc = host->mmc; |
| 149 | |
| 150 | clk_free(&clk); |
| 151 | |
Zixun LI | d46f289 | 2023-05-17 15:49:45 +0200 | [diff] [blame] | 152 | ret = sdhci_probe(dev); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | atmel_sdhci_config_fcd(host); |
| 157 | |
| 158 | return 0; |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static int atmel_sdhci_bind(struct udevice *dev) |
| 162 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 163 | struct atmel_sdhci_plat *plat = dev_get_plat(dev); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 164 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 165 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static const struct udevice_id atmel_sdhci_ids[] = { |
| 169 | { .compatible = "atmel,sama5d2-sdhci" }, |
Sandeep Sheriker Mallikarjun | cba5eb6 | 2019-09-27 13:08:36 +0000 | [diff] [blame] | 170 | { .compatible = "microchip,sam9x60-sdhci" }, |
Eugen Hristev | 3e74299 | 2020-08-27 12:04:41 +0300 | [diff] [blame] | 171 | { .compatible = "microchip,sama7g5-sdhci" }, |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 172 | { } |
| 173 | }; |
| 174 | |
| 175 | U_BOOT_DRIVER(atmel_sdhci_drv) = { |
| 176 | .name = "atmel_sdhci", |
| 177 | .id = UCLASS_MMC, |
| 178 | .of_match = atmel_sdhci_ids, |
| 179 | .ops = &sdhci_ops, |
| 180 | .bind = atmel_sdhci_bind, |
| 181 | .probe = atmel_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 182 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 183 | .plat_auto = sizeof(struct atmel_sdhci_plat), |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 184 | }; |
| 185 | #endif |