Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | a3b59b1 | 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 | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
Wenyou Yang | a3b59b1 | 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 | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 14 | |
| 15 | #define ATMEL_SDHC_MIN_FREQ 400000 |
Ludovic Desroches | 327713a | 2017-11-17 14:51:27 +0800 | [diff] [blame] | 16 | #define ATMEL_SDHC_GCK_RATE 240000000 |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 17 | |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 18 | #ifndef CONFIG_DM_MMC |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 19 | int atmel_sdhci_init(void *regbase, u32 id) |
| 20 | { |
| 21 | struct sdhci_host *host; |
| 22 | u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ; |
| 23 | |
| 24 | host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host)); |
| 25 | if (!host) { |
| 26 | printf("%s: sdhci_host calloc failed\n", __func__); |
| 27 | return -ENOMEM; |
| 28 | } |
| 29 | |
| 30 | host->name = "atmel_sdhci"; |
| 31 | host->ioaddr = regbase; |
Wenyou Yang | b312508 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 32 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 33 | max_clk = at91_get_periph_generated_clk(id); |
| 34 | if (!max_clk) { |
| 35 | printf("%s: Failed to get the proper clock\n", __func__); |
| 36 | free(host); |
| 37 | return -ENODEV; |
| 38 | } |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 39 | host->max_clk = max_clk; |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 40 | |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 41 | add_sdhci(host, 0, min_clk); |
Wenyou Yang | a3b59b1 | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 45 | |
| 46 | #else |
| 47 | |
| 48 | DECLARE_GLOBAL_DATA_PTR; |
| 49 | |
| 50 | struct atmel_sdhci_plat { |
| 51 | struct mmc_config cfg; |
| 52 | struct mmc mmc; |
| 53 | }; |
| 54 | |
Sergiu Moga | 222a1f4 | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 55 | static int atmel_sdhci_deferred_probe(struct sdhci_host *host) |
| 56 | { |
| 57 | struct udevice *dev = host->mmc->dev; |
| 58 | |
| 59 | return sdhci_probe(dev); |
| 60 | } |
| 61 | |
| 62 | static const struct sdhci_ops atmel_sdhci_ops = { |
| 63 | .deferred_probe = atmel_sdhci_deferred_probe, |
| 64 | }; |
| 65 | |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 66 | static int atmel_sdhci_probe(struct udevice *dev) |
| 67 | { |
| 68 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 69 | struct atmel_sdhci_plat *plat = dev_get_plat(dev); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 70 | struct sdhci_host *host = dev_get_priv(dev); |
| 71 | u32 max_clk; |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 72 | struct clk clk; |
| 73 | int ret; |
| 74 | |
Wenyou Yang | 339cb07 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 75 | ret = clk_get_by_index(dev, 0, &clk); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 76 | if (ret) |
| 77 | return ret; |
| 78 | |
| 79 | ret = clk_enable(&clk); |
| 80 | if (ret) |
| 81 | return ret; |
| 82 | |
| 83 | host->name = dev->name; |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 84 | host->ioaddr = dev_read_addr_ptr(dev); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 85 | |
Wenyou Yang | b312508 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 86 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 87 | host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 88 | "bus-width", 4); |
| 89 | |
Wenyou Yang | 339cb07 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 90 | ret = clk_get_by_index(dev, 1, &clk); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | |
Eugen Hristev | 2e00608 | 2020-08-27 12:16:15 +0300 | [diff] [blame] | 94 | clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 95 | |
| 96 | max_clk = clk_get_rate(&clk); |
| 97 | if (!max_clk) |
| 98 | return -EINVAL; |
| 99 | |
Eugen Hristev | 81f1643 | 2020-08-27 12:25:56 +0300 | [diff] [blame] | 100 | ret = clk_enable(&clk); |
Eugen Hristev | 7eace38 | 2020-11-09 13:02:17 +0200 | [diff] [blame] | 101 | /* return error only if the clock really has a clock enable func */ |
| 102 | if (ret && ret != -ENOSYS) |
Eugen Hristev | 81f1643 | 2020-08-27 12:25:56 +0300 | [diff] [blame] | 103 | return ret; |
| 104 | |
Eugen Hristev | 3710b46 | 2020-08-27 12:25:57 +0300 | [diff] [blame] | 105 | ret = mmc_of_parse(dev, &plat->cfg); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 109 | host->max_clk = max_clk; |
Peng Fan | 7835e87 | 2019-08-06 02:47:47 +0000 | [diff] [blame] | 110 | host->mmc = &plat->mmc; |
| 111 | host->mmc->dev = dev; |
Stefan Herbrechtsmeier | 6d0e34b | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 112 | |
| 113 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 114 | if (ret) |
| 115 | return ret; |
| 116 | |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 117 | host->mmc->priv = host; |
Sergiu Moga | 222a1f4 | 2022-06-22 16:30:47 +0300 | [diff] [blame] | 118 | host->ops = &atmel_sdhci_ops; |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 119 | upriv->mmc = host->mmc; |
| 120 | |
| 121 | clk_free(&clk); |
| 122 | |
| 123 | return sdhci_probe(dev); |
| 124 | } |
| 125 | |
| 126 | static int atmel_sdhci_bind(struct udevice *dev) |
| 127 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 128 | struct atmel_sdhci_plat *plat = dev_get_plat(dev); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 129 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 130 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static const struct udevice_id atmel_sdhci_ids[] = { |
| 134 | { .compatible = "atmel,sama5d2-sdhci" }, |
Sandeep Sheriker Mallikarjun | f566374 | 2019-09-27 13:08:36 +0000 | [diff] [blame] | 135 | { .compatible = "microchip,sam9x60-sdhci" }, |
Eugen Hristev | 4cc0825 | 2020-08-27 12:04:41 +0300 | [diff] [blame] | 136 | { .compatible = "microchip,sama7g5-sdhci" }, |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 137 | { } |
| 138 | }; |
| 139 | |
| 140 | U_BOOT_DRIVER(atmel_sdhci_drv) = { |
| 141 | .name = "atmel_sdhci", |
| 142 | .id = UCLASS_MMC, |
| 143 | .of_match = atmel_sdhci_ids, |
| 144 | .ops = &sdhci_ops, |
| 145 | .bind = atmel_sdhci_bind, |
| 146 | .probe = atmel_sdhci_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 147 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 148 | .plat_auto = sizeof(struct atmel_sdhci_plat), |
Wenyou Yang | a0d0d86 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 149 | }; |
| 150 | #endif |