xref: /openbmc/u-boot/drivers/mmc/aspeed_sdhci.c (revision 3e39fe0d92935acca78fc5f7e913e187175a2fc5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
4  *
5  * Rockchip SD Host Controller Interface
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <dt-structs.h>
11 #include <linux/libfdt.h>
12 #include <malloc.h>
13 #include <mapmem.h>
14 #include <sdhci.h>
15 #include <clk.h>
16 
17 /* 400KHz is max freq for card ID etc. Use that as min */
18 #define EMMC_MIN_FREQ	400000
19 
20 struct aspeed_sdhci_plat {
21 	struct mmc_config cfg;
22 	struct mmc mmc;
23 	unsigned int f_max;
24 };
25 
26 struct aspeed_sdhci_priv {
27 	struct sdhci_host *host;
28 	struct clk clk;
29 };
30 
31 static int aspeed_sdhci_probe(struct udevice *dev)
32 {
33 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
34 	struct aspeed_sdhci_plat *plat = dev_get_platdata(dev);
35 	struct aspeed_sdhci_priv *prv = dev_get_priv(dev);
36 	struct sdhci_host *host = prv->host;
37 	unsigned long clock;
38 	struct clk clk;
39 	int ret;
40 
41 	ret = clk_get_by_index(dev, 0, &clk);
42 	if (ret < 0) {
43 		pr_debug("%s: Can't get clock for %s: %d\n", __func__, dev->name,
44 		      ret);
45 	}
46 
47 	clock = clk_get_rate(&clk);
48 	if (IS_ERR_VALUE(clock)) {
49 		dev_err(dev, "failed to get clock\n");
50 		return ret;
51 	}
52 
53 	debug("%s: CLK %ld\n", __func__, clock);
54 
55 //	host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
56 	host->max_clk = clock;
57 
58 	if (host->bus_width == 8)
59 		host->host_caps |= MMC_MODE_8BIT;
60 
61 	ret = sdhci_setup_cfg(&plat->cfg, host, host->max_clk, EMMC_MIN_FREQ);
62 
63 	host->mmc = &plat->mmc;
64 	if (ret)
65 		return ret;
66 	host->mmc->priv = host;
67 	host->mmc->dev = dev;
68 	upriv->mmc = host->mmc;
69 
70 	return sdhci_probe(dev);
71 }
72 
73 static int aspeed_sdhci_ofdata_to_platdata(struct udevice *dev)
74 {
75 	struct aspeed_sdhci_priv *priv = dev_get_priv(dev);
76 
77 	priv->host = calloc(1, sizeof(struct sdhci_host));
78 	if (!priv->host)
79 			return -1;
80 
81 	priv->host->name = dev->name;
82 	priv->host->ioaddr = (void *)dev_read_addr(dev);
83 
84 	return 0;
85 }
86 
87 static int aspeed_sdhci_bind(struct udevice *dev)
88 {
89 	struct aspeed_sdhci_plat *plat = dev_get_platdata(dev);
90 
91 	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
92 }
93 
94 static const struct udevice_id aspeed_sdhci_ids[] = {
95 	{ .compatible = "aspeed,sdhci-ast2500" },
96 	{ .compatible = "aspeed,sdhci-ast2600" },
97 	{ .compatible = "aspeed,emmc-ast2600" },
98 	{ }
99 };
100 
101 U_BOOT_DRIVER(aspeed_sdhci_drv) = {
102 	.name		= "aspeed_sdhci",
103 	.id		= UCLASS_MMC,
104 	.of_match	= aspeed_sdhci_ids,
105 	.ofdata_to_platdata = aspeed_sdhci_ofdata_to_platdata,
106 	.ops		= &sdhci_ops,
107 	.bind		= aspeed_sdhci_bind,
108 	.probe		= aspeed_sdhci_probe,
109 	.priv_auto_alloc_size = sizeof(struct aspeed_sdhci_priv),
110 	.platdata_auto_alloc_size = sizeof(struct aspeed_sdhci_plat),
111 };
112