1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou.Yang <wenyou.yang@atmel.com>
12 #include <asm/arch/clk.h>
13 #include <asm/global_data.h>
15 #define ATMEL_SDHC_MIN_FREQ 400000
16 #define ATMEL_SDHC_GCK_RATE 240000000
19 int atmel_sdhci_init(void *regbase, u32 id)
21 struct sdhci_host *host;
22 u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ;
24 host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host));
26 printf("%s: sdhci_host calloc failed\n", __func__);
30 host->name = "atmel_sdhci";
31 host->ioaddr = regbase;
32 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
33 max_clk = at91_get_periph_generated_clk(id);
35 printf("%s: Failed to get the proper clock\n", __func__);
39 host->max_clk = max_clk;
41 add_sdhci(host, 0, min_clk);
48 DECLARE_GLOBAL_DATA_PTR;
50 struct atmel_sdhci_plat {
51 struct mmc_config cfg;
55 static int atmel_sdhci_deferred_probe(struct sdhci_host *host)
57 struct udevice *dev = host->mmc->dev;
59 return sdhci_probe(dev);
62 static const struct sdhci_ops atmel_sdhci_ops = {
63 .deferred_probe = atmel_sdhci_deferred_probe,
66 static int atmel_sdhci_probe(struct udevice *dev)
68 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
69 struct atmel_sdhci_plat *plat = dev_get_plat(dev);
70 struct sdhci_host *host = dev_get_priv(dev);
75 ret = clk_get_by_index(dev, 0, &clk);
79 ret = clk_enable(&clk);
83 host->name = dev->name;
84 host->ioaddr = dev_read_addr_ptr(dev);
86 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
87 host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
90 ret = clk_get_by_index(dev, 1, &clk);
94 clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
96 max_clk = clk_get_rate(&clk);
100 ret = clk_enable(&clk);
101 /* return error only if the clock really has a clock enable func */
102 if (ret && ret != -ENOSYS)
105 ret = mmc_of_parse(dev, &plat->cfg);
109 host->max_clk = max_clk;
110 host->mmc = &plat->mmc;
111 host->mmc->dev = dev;
113 ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
117 host->mmc->priv = host;
118 host->ops = &atmel_sdhci_ops;
119 upriv->mmc = host->mmc;
123 return sdhci_probe(dev);
126 static int atmel_sdhci_bind(struct udevice *dev)
128 struct atmel_sdhci_plat *plat = dev_get_plat(dev);
130 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
133 static const struct udevice_id atmel_sdhci_ids[] = {
134 { .compatible = "atmel,sama5d2-sdhci" },
135 { .compatible = "microchip,sam9x60-sdhci" },
136 { .compatible = "microchip,sama7g5-sdhci" },
140 U_BOOT_DRIVER(atmel_sdhci_drv) = {
141 .name = "atmel_sdhci",
143 .of_match = atmel_sdhci_ids,
145 .bind = atmel_sdhci_bind,
146 .probe = atmel_sdhci_probe,
147 .priv_auto = sizeof(struct sdhci_host),
148 .plat_auto = sizeof(struct atmel_sdhci_plat),