Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Atmel Corporation |
| 3 | * Wenyou.Yang <wenyou.yang@atmel.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 9 | #include <clk.h> |
| 10 | #include <dm.h> |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <sdhci.h> |
| 13 | #include <asm/arch/clk.h> |
| 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 | |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 18 | #ifndef CONFIG_DM_MMC |
Wenyou Yang | 11fd15d | 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 | 4fadd97 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 32 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Wenyou Yang | 11fd15d | 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 | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 39 | host->max_clk = max_clk; |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 40 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 41 | add_sdhci(host, 0, min_clk); |
Wenyou Yang | 11fd15d | 2015-11-02 10:57:09 +0800 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
Wenyou Yang | 83e88a4 | 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 | |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 55 | static int atmel_sdhci_probe(struct udevice *dev) |
| 56 | { |
| 57 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 58 | struct atmel_sdhci_plat *plat = dev_get_platdata(dev); |
| 59 | struct sdhci_host *host = dev_get_priv(dev); |
| 60 | u32 max_clk; |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 61 | struct clk clk; |
| 62 | int ret; |
| 63 | |
Wenyou Yang | 038dff8 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 64 | ret = clk_get_by_index(dev, 0, &clk); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 65 | if (ret) |
| 66 | return ret; |
| 67 | |
| 68 | ret = clk_enable(&clk); |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | |
| 72 | host->name = dev->name; |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 73 | host->ioaddr = (void *)devfdt_get_addr(dev); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 74 | |
Wenyou Yang | 4fadd97 | 2017-05-11 08:25:12 +0800 | [diff] [blame] | 75 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 76 | 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] | 77 | "bus-width", 4); |
| 78 | |
Wenyou Yang | 038dff8 | 2016-09-27 11:00:34 +0800 | [diff] [blame] | 79 | ret = clk_get_by_index(dev, 1, &clk); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 80 | if (ret) |
| 81 | return ret; |
| 82 | |
Ludovic Desroches | e5a07b8 | 2017-11-17 14:51:27 +0800 | [diff] [blame] | 83 | ret = clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 84 | if (ret) |
| 85 | return ret; |
| 86 | |
| 87 | max_clk = clk_get_rate(&clk); |
| 88 | if (!max_clk) |
| 89 | return -EINVAL; |
| 90 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 91 | host->max_clk = max_clk; |
| 92 | |
| 93 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 94 | if (ret) |
| 95 | return ret; |
| 96 | |
| 97 | host->mmc = &plat->mmc; |
| 98 | host->mmc->dev = dev; |
| 99 | host->mmc->priv = host; |
| 100 | upriv->mmc = host->mmc; |
| 101 | |
| 102 | clk_free(&clk); |
| 103 | |
| 104 | return sdhci_probe(dev); |
| 105 | } |
| 106 | |
| 107 | static int atmel_sdhci_bind(struct udevice *dev) |
| 108 | { |
| 109 | struct atmel_sdhci_plat *plat = dev_get_platdata(dev); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 110 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 111 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Wenyou Yang | 83e88a4 | 2016-08-10 10:51:05 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static const struct udevice_id atmel_sdhci_ids[] = { |
| 115 | { .compatible = "atmel,sama5d2-sdhci" }, |
| 116 | { } |
| 117 | }; |
| 118 | |
| 119 | U_BOOT_DRIVER(atmel_sdhci_drv) = { |
| 120 | .name = "atmel_sdhci", |
| 121 | .id = UCLASS_MMC, |
| 122 | .of_match = atmel_sdhci_ids, |
| 123 | .ops = &sdhci_ops, |
| 124 | .bind = atmel_sdhci_bind, |
| 125 | .probe = atmel_sdhci_probe, |
| 126 | .priv_auto_alloc_size = sizeof(struct sdhci_host), |
| 127 | .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat), |
| 128 | }; |
| 129 | #endif |