xref: /openbmc/u-boot/drivers/mmc/aspeed_sdhci.c (revision 57efeb04)
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 	int node = dev_of_offset(dev);
37 	struct sdhci_host *host = prv->host;
38 	unsigned long clock;
39 	struct clk clk;
40 	int ret;
41 
42 	ret = clk_get_by_index(dev, 0, &clk);
43 	if (ret < 0) {
44 		pr_debug("%s: Can't get clock for %s: %d\n", __func__, dev->name,
45 		      ret);
46 	}
47 
48 	clock = clk_get_rate(&clk);
49 	if (IS_ERR_VALUE(clock)) {
50 		dev_err(dev, "failed to get clock\n");
51 		return ret;
52 	}
53 
54 	debug("%s: CLK %ld\n", __func__, clock);
55 
56 	//1: sd card pwr, 0: no pwr
57 	gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
58 				   &host->pwr_gpio, GPIOD_IS_OUT);
59 	if (dm_gpio_is_valid(&host->pwr_gpio)) {
60 		printf("\n");
61 		dm_gpio_set_value(&host->pwr_gpio, 1);
62 		if (ret) {
63 			debug("MMC not configured\n");
64 			return ret;
65 		}
66 	}
67 
68 	//1: 3.3v, 0: 1.8v
69 	gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-sw-gpios", 0,
70 				   &host->pwr_sw_gpio, GPIOD_IS_OUT);
71 
72 	if (dm_gpio_is_valid(&host->pwr_sw_gpio)) {
73 		dm_gpio_set_value(&host->pwr_sw_gpio, 1);
74 		if (ret) {
75 			debug("MMC not configured\n");
76 			return ret;
77 		}
78 	}
79 
80 //	host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
81 	host->max_clk = clock;
82 
83 	host->bus_width = dev_read_u32_default(dev, "bus-width", 4);
84 
85 	if (host->bus_width == 8)
86 		host->host_caps |= MMC_MODE_8BIT;
87 
88 	ret = sdhci_setup_cfg(&plat->cfg, host, host->max_clk, EMMC_MIN_FREQ);
89 
90 	host->mmc = &plat->mmc;
91 	if (ret)
92 		return ret;
93 	host->mmc->priv = host;
94 	host->mmc->dev = dev;
95 	upriv->mmc = host->mmc;
96 
97 	return sdhci_probe(dev);
98 }
99 
100 static int aspeed_sdhci_ofdata_to_platdata(struct udevice *dev)
101 {
102 	struct aspeed_sdhci_priv *priv = dev_get_priv(dev);
103 
104 	priv->host = calloc(1, sizeof(struct sdhci_host));
105 	if (!priv->host)
106 			return -1;
107 
108 	priv->host->name = dev->name;
109 	priv->host->ioaddr = (void *)dev_read_addr(dev);
110 
111 	return 0;
112 }
113 
114 static int aspeed_sdhci_bind(struct udevice *dev)
115 {
116 	struct aspeed_sdhci_plat *plat = dev_get_platdata(dev);
117 
118 	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
119 }
120 
121 static const struct udevice_id aspeed_sdhci_ids[] = {
122 	{ .compatible = "aspeed,sdhci-ast2500" },
123 	{ .compatible = "aspeed,sdhci-ast2600" },
124 	{ .compatible = "aspeed,emmc-ast2600" },
125 	{ }
126 };
127 
128 U_BOOT_DRIVER(aspeed_sdhci_drv) = {
129 	.name		= "aspeed_sdhci",
130 	.id		= UCLASS_MMC,
131 	.of_match	= aspeed_sdhci_ids,
132 	.ofdata_to_platdata = aspeed_sdhci_ofdata_to_platdata,
133 	.ops		= &sdhci_ops,
134 	.bind		= aspeed_sdhci_bind,
135 	.probe		= aspeed_sdhci_probe,
136 	.priv_auto_alloc_size = sizeof(struct aspeed_sdhci_priv),
137 	.platdata_auto_alloc_size = sizeof(struct aspeed_sdhci_plat),
138 };
139