Merge tag 'u-boot-amlogic-20200108' of https://gitlab.denx.de/u-boot/custodians/u...
[platform/kernel/u-boot.git] / drivers / mmc / aspeed_sdhci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 IBM Corp.
4  * Eddie James <eajames@linux.ibm.com>
5  */
6
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <malloc.h>
11 #include <sdhci.h>
12
13 struct aspeed_sdhci_plat {
14         struct mmc_config cfg;
15         struct mmc mmc;
16 };
17
18 static int aspeed_sdhci_probe(struct udevice *dev)
19 {
20         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
21         struct aspeed_sdhci_plat *plat = dev_get_platdata(dev);
22         struct sdhci_host *host = dev_get_priv(dev);
23         u32 max_clk;
24         struct clk clk;
25         int ret;
26
27         ret = clk_get_by_index(dev, 0, &clk);
28         if (ret)
29                 return ret;
30
31         ret = clk_enable(&clk);
32         if (ret)
33                 goto free;
34
35         host->name = dev->name;
36         host->ioaddr = (void *)devfdt_get_addr(dev);
37
38         max_clk = clk_get_rate(&clk);
39         if (IS_ERR_VALUE(max_clk)) {
40                 ret = max_clk;
41                 goto err;
42         }
43
44         host->max_clk = max_clk;
45         host->mmc = &plat->mmc;
46         host->mmc->dev = dev;
47         host->mmc->priv = host;
48         upriv->mmc = host->mmc;
49
50         ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
51         if (ret)
52                 goto err;
53
54         ret = sdhci_probe(dev);
55         if (ret)
56                 goto err;
57
58         return 0;
59
60 err:
61         clk_disable(&clk);
62 free:
63         clk_free(&clk);
64         return ret;
65 }
66
67 static int aspeed_sdhci_bind(struct udevice *dev)
68 {
69         struct aspeed_sdhci_plat *plat = dev_get_platdata(dev);
70
71         return sdhci_bind(dev, &plat->mmc, &plat->cfg);
72 }
73
74 static const struct udevice_id aspeed_sdhci_ids[] = {
75         { .compatible = "aspeed,ast2400-sdhci" },
76         { .compatible = "aspeed,ast2500-sdhci" },
77         { .compatible = "aspeed,ast2600-sdhci" },
78         { }
79 };
80
81 U_BOOT_DRIVER(aspeed_sdhci_drv) = {
82         .name           = "aspeed_sdhci",
83         .id             = UCLASS_MMC,
84         .of_match       = aspeed_sdhci_ids,
85         .ops            = &sdhci_ops,
86         .bind           = aspeed_sdhci_bind,
87         .probe          = aspeed_sdhci_probe,
88         .priv_auto_alloc_size = sizeof(struct sdhci_host),
89         .platdata_auto_alloc_size = sizeof(struct aspeed_sdhci_plat),
90 };