blob: 2b5ceeab94b5415ed7fc4595a58e3b1615eade97 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang11fd15d2015-11-02 10:57:09 +08002/*
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yang11fd15d2015-11-02 10:57:09 +08005 */
6
7#include <common.h>
Wenyou Yang83e88a42016-08-10 10:51:05 +08008#include <clk.h>
9#include <dm.h>
Wenyou Yang11fd15d2015-11-02 10:57:09 +080010#include <malloc.h>
11#include <sdhci.h>
12#include <asm/arch/clk.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wenyou Yang11fd15d2015-11-02 10:57:09 +080014
15#define ATMEL_SDHC_MIN_FREQ 400000
Ludovic Desrochese5a07b82017-11-17 14:51:27 +080016#define ATMEL_SDHC_GCK_RATE 240000000
Wenyou Yang11fd15d2015-11-02 10:57:09 +080017
Wenyou Yang83e88a42016-08-10 10:51:05 +080018#ifndef CONFIG_DM_MMC
Wenyou Yang11fd15d2015-11-02 10:57:09 +080019int 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 Yang4fadd972017-05-11 08:25:12 +080032 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Wenyou Yang11fd15d2015-11-02 10:57:09 +080033 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 Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010039 host->max_clk = max_clk;
Wenyou Yang11fd15d2015-11-02 10:57:09 +080040
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010041 add_sdhci(host, 0, min_clk);
Wenyou Yang11fd15d2015-11-02 10:57:09 +080042
43 return 0;
44}
Wenyou Yang83e88a42016-08-10 10:51:05 +080045
46#else
47
48DECLARE_GLOBAL_DATA_PTR;
49
50struct atmel_sdhci_plat {
51 struct mmc_config cfg;
52 struct mmc mmc;
53};
54
Wenyou Yang83e88a42016-08-10 10:51:05 +080055static int atmel_sdhci_probe(struct udevice *dev)
56{
57 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070058 struct atmel_sdhci_plat *plat = dev_get_plat(dev);
Wenyou Yang83e88a42016-08-10 10:51:05 +080059 struct sdhci_host *host = dev_get_priv(dev);
60 u32 max_clk;
Wenyou Yang83e88a42016-08-10 10:51:05 +080061 struct clk clk;
62 int ret;
63
Wenyou Yang038dff82016-09-27 11:00:34 +080064 ret = clk_get_by_index(dev, 0, &clk);
Wenyou Yang83e88a42016-08-10 10:51:05 +080065 if (ret)
66 return ret;
67
68 ret = clk_enable(&clk);
69 if (ret)
70 return ret;
71
72 host->name = dev->name;
Masahiro Yamada1096ae12020-07-17 14:36:46 +090073 host->ioaddr = dev_read_addr_ptr(dev);
Wenyou Yang83e88a42016-08-10 10:51:05 +080074
Wenyou Yang4fadd972017-05-11 08:25:12 +080075 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
Simon Glassdd79d6e2017-01-17 16:52:55 -070076 host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Wenyou Yang83e88a42016-08-10 10:51:05 +080077 "bus-width", 4);
78
Wenyou Yang038dff82016-09-27 11:00:34 +080079 ret = clk_get_by_index(dev, 1, &clk);
Wenyou Yang83e88a42016-08-10 10:51:05 +080080 if (ret)
81 return ret;
82
Eugen Hristev90805e12020-08-27 12:16:15 +030083 clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
Wenyou Yang83e88a42016-08-10 10:51:05 +080084
85 max_clk = clk_get_rate(&clk);
86 if (!max_clk)
87 return -EINVAL;
88
Eugen Hristev3110c752020-08-27 12:25:56 +030089 ret = clk_enable(&clk);
Eugen Hristeva5630512020-11-09 13:02:17 +020090 /* return error only if the clock really has a clock enable func */
91 if (ret && ret != -ENOSYS)
Eugen Hristev3110c752020-08-27 12:25:56 +030092 return ret;
93
Eugen Hristev6fb8abf2020-08-27 12:25:57 +030094 ret = mmc_of_parse(dev, &plat->cfg);
95 if (ret)
96 return ret;
97
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010098 host->max_clk = max_clk;
Peng Fan9a8f4992019-08-06 02:47:47 +000099 host->mmc = &plat->mmc;
100 host->mmc->dev = dev;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100101
102 ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
Wenyou Yang83e88a42016-08-10 10:51:05 +0800103 if (ret)
104 return ret;
105
Wenyou Yang83e88a42016-08-10 10:51:05 +0800106 host->mmc->priv = host;
107 upriv->mmc = host->mmc;
108
109 clk_free(&clk);
110
111 return sdhci_probe(dev);
112}
113
114static int atmel_sdhci_bind(struct udevice *dev)
115{
Simon Glassfa20e932020-12-03 16:55:20 -0700116 struct atmel_sdhci_plat *plat = dev_get_plat(dev);
Wenyou Yang83e88a42016-08-10 10:51:05 +0800117
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900118 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Wenyou Yang83e88a42016-08-10 10:51:05 +0800119}
120
121static const struct udevice_id atmel_sdhci_ids[] = {
122 { .compatible = "atmel,sama5d2-sdhci" },
Sandeep Sheriker Mallikarjuncba5eb62019-09-27 13:08:36 +0000123 { .compatible = "microchip,sam9x60-sdhci" },
Eugen Hristev3e742992020-08-27 12:04:41 +0300124 { .compatible = "microchip,sama7g5-sdhci" },
Wenyou Yang83e88a42016-08-10 10:51:05 +0800125 { }
126};
127
128U_BOOT_DRIVER(atmel_sdhci_drv) = {
129 .name = "atmel_sdhci",
130 .id = UCLASS_MMC,
131 .of_match = atmel_sdhci_ids,
132 .ops = &sdhci_ops,
133 .bind = atmel_sdhci_bind,
134 .probe = atmel_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700135 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700136 .plat_auto = sizeof(struct atmel_sdhci_plat),
Wenyou Yang83e88a42016-08-10 10:51:05 +0800137};
138#endif