blob: f56ae63bc2cdf13d09a3db643ac6d197b803b0be [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yanga3b59b12015-11-02 10:57:09 +08002/*
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yanga3b59b12015-11-02 10:57:09 +08005 */
6
7#include <common.h>
Wenyou Yanga0d0d862016-08-10 10:51:05 +08008#include <clk.h>
9#include <dm.h>
Wenyou Yanga3b59b12015-11-02 10:57:09 +080010#include <malloc.h>
11#include <sdhci.h>
12#include <asm/arch/clk.h>
13
14#define ATMEL_SDHC_MIN_FREQ 400000
Ludovic Desroches327713a2017-11-17 14:51:27 +080015#define ATMEL_SDHC_GCK_RATE 240000000
Wenyou Yanga3b59b12015-11-02 10:57:09 +080016
Wenyou Yanga0d0d862016-08-10 10:51:05 +080017#ifndef CONFIG_DM_MMC
Wenyou Yanga3b59b12015-11-02 10:57:09 +080018int atmel_sdhci_init(void *regbase, u32 id)
19{
20 struct sdhci_host *host;
21 u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ;
22
23 host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host));
24 if (!host) {
25 printf("%s: sdhci_host calloc failed\n", __func__);
26 return -ENOMEM;
27 }
28
29 host->name = "atmel_sdhci";
30 host->ioaddr = regbase;
Wenyou Yangb3125082017-05-11 08:25:12 +080031 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Wenyou Yanga3b59b12015-11-02 10:57:09 +080032 max_clk = at91_get_periph_generated_clk(id);
33 if (!max_clk) {
34 printf("%s: Failed to get the proper clock\n", __func__);
35 free(host);
36 return -ENODEV;
37 }
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010038 host->max_clk = max_clk;
Wenyou Yanga3b59b12015-11-02 10:57:09 +080039
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010040 add_sdhci(host, 0, min_clk);
Wenyou Yanga3b59b12015-11-02 10:57:09 +080041
42 return 0;
43}
Wenyou Yanga0d0d862016-08-10 10:51:05 +080044
45#else
46
47DECLARE_GLOBAL_DATA_PTR;
48
49struct atmel_sdhci_plat {
50 struct mmc_config cfg;
51 struct mmc mmc;
52};
53
Wenyou Yanga0d0d862016-08-10 10:51:05 +080054static int atmel_sdhci_probe(struct udevice *dev)
55{
56 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
57 struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
58 struct sdhci_host *host = dev_get_priv(dev);
59 u32 max_clk;
Wenyou Yanga0d0d862016-08-10 10:51:05 +080060 struct clk clk;
61 int ret;
62
Wenyou Yang339cb072016-09-27 11:00:34 +080063 ret = clk_get_by_index(dev, 0, &clk);
Wenyou Yanga0d0d862016-08-10 10:51:05 +080064 if (ret)
65 return ret;
66
67 ret = clk_enable(&clk);
68 if (ret)
69 return ret;
70
71 host->name = dev->name;
Masahiro Yamada8613c8d2020-07-17 14:36:46 +090072 host->ioaddr = dev_read_addr_ptr(dev);
Wenyou Yanga0d0d862016-08-10 10:51:05 +080073
Wenyou Yangb3125082017-05-11 08:25:12 +080074 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Simon Glasse160f7d2017-01-17 16:52:55 -070075 host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Wenyou Yanga0d0d862016-08-10 10:51:05 +080076 "bus-width", 4);
77
Wenyou Yang339cb072016-09-27 11:00:34 +080078 ret = clk_get_by_index(dev, 1, &clk);
Wenyou Yanga0d0d862016-08-10 10:51:05 +080079 if (ret)
80 return ret;
81
Eugen Hristev2e006082020-08-27 12:16:15 +030082 clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
Wenyou Yanga0d0d862016-08-10 10:51:05 +080083
84 max_clk = clk_get_rate(&clk);
85 if (!max_clk)
86 return -EINVAL;
87
Eugen Hristev81f16432020-08-27 12:25:56 +030088 ret = clk_enable(&clk);
89 if (ret)
90 return ret;
91
Eugen Hristev3710b462020-08-27 12:25:57 +030092 ret = mmc_of_parse(dev, &plat->cfg);
93 if (ret)
94 return ret;
95
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010096 host->max_clk = max_clk;
Peng Fan7835e872019-08-06 02:47:47 +000097 host->mmc = &plat->mmc;
98 host->mmc->dev = dev;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010099
100 ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
Wenyou Yanga0d0d862016-08-10 10:51:05 +0800101 if (ret)
102 return ret;
103
Wenyou Yanga0d0d862016-08-10 10:51:05 +0800104 host->mmc->priv = host;
105 upriv->mmc = host->mmc;
106
107 clk_free(&clk);
108
109 return sdhci_probe(dev);
110}
111
112static int atmel_sdhci_bind(struct udevice *dev)
113{
114 struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
Wenyou Yanga0d0d862016-08-10 10:51:05 +0800115
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900116 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Wenyou Yanga0d0d862016-08-10 10:51:05 +0800117}
118
119static const struct udevice_id atmel_sdhci_ids[] = {
120 { .compatible = "atmel,sama5d2-sdhci" },
Sandeep Sheriker Mallikarjunf5663742019-09-27 13:08:36 +0000121 { .compatible = "microchip,sam9x60-sdhci" },
Eugen Hristev4cc08252020-08-27 12:04:41 +0300122 { .compatible = "microchip,sama7g5-sdhci" },
Wenyou Yanga0d0d862016-08-10 10:51:05 +0800123 { }
124};
125
126U_BOOT_DRIVER(atmel_sdhci_drv) = {
127 .name = "atmel_sdhci",
128 .id = UCLASS_MMC,
129 .of_match = atmel_sdhci_ids,
130 .ops = &sdhci_ops,
131 .bind = atmel_sdhci_bind,
132 .probe = atmel_sdhci_probe,
133 .priv_auto_alloc_size = sizeof(struct sdhci_host),
134 .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat),
135};
136#endif