blob: b5fe828dd60a566337f2a4403300cdc9507d149a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung442d5562012-04-23 02:36:28 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
Jaehoon Chung442d5562012-04-23 02:36:28 +00005 */
6
7#include <common.h>
Jaehoon Chung7aedafd2016-09-09 18:23:23 +09008#include <dm.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +00009#include <malloc.h>
10#include <sdhci.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010011#include <fdtdec.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010013#include <asm/gpio.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +000014#include <asm/arch/mmc.h>
Jaehoon Chungb09ed6e2012-08-30 16:24:11 +000015#include <asm/arch/clk.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010016#include <errno.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010017#include <asm/arch/pinmux.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +000018
Jaehoon Chung7aedafd2016-09-09 18:23:23 +090019#ifdef CONFIG_DM_MMC
20struct s5p_sdhci_plat {
21 struct mmc_config cfg;
22 struct mmc mmc;
23};
24
25DECLARE_GLOBAL_DATA_PTR;
26#endif
27
Jaehoon Chung442d5562012-04-23 02:36:28 +000028static char *S5P_NAME = "SAMSUNG SDHCI";
29static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
30{
31 unsigned long val, ctrl;
32 /*
33 * SELCLKPADDS[17:16]
34 * 00 = 2mA
35 * 01 = 4mA
36 * 10 = 7mA
37 * 11 = 9mA
38 */
39 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
40
41 val = sdhci_readl(host, SDHCI_CONTROL2);
Matt Reimer8ebde4f2015-02-23 14:52:22 -070042 val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
Jaehoon Chung442d5562012-04-23 02:36:28 +000043
44 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
45 SDHCI_CTRL2_ENCMDCNFMSK |
46 SDHCI_CTRL2_ENFBCLKRX |
47 SDHCI_CTRL2_ENCLKOUTHOLD;
48
49 sdhci_writel(host, val, SDHCI_CONTROL2);
50
51 /*
52 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
53 * FCSel[1:0] : Rx Feedback Clock Delay Control
54 * Inverter delay means10ns delay if SDCLK 50MHz setting
55 * 01 = Delay1 (basic delay)
56 * 11 = Delay2 (basic delay + 2ns)
57 * 00 = Delay3 (inverter delay)
58 * 10 = Delay4 (inverter delay + 2ns)
59 */
Jaehoon Chungb2686602012-08-30 16:24:08 +000060 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
Jaehoon Chung442d5562012-04-23 02:36:28 +000061 sdhci_writel(host, val, SDHCI_CONTROL3);
62
63 /*
64 * SELBASECLK[5:4]
65 * 00/01 = HCLK
66 * 10 = EPLL
67 * 11 = XTI or XEXTCLK
68 */
69 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
70 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
71 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
72 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
73}
74
Jaehoon Chungf73b33f2016-12-30 15:30:17 +090075static void s5p_set_clock(struct sdhci_host *host, u32 div)
76{
77 /* ToDo : Use the Clock Framework */
78 set_mmc_clk(host->index, div);
79}
80
Jaehoon Chung62226b62016-12-30 15:30:18 +090081static const struct sdhci_ops s5p_sdhci_ops = {
82 .set_clock = &s5p_set_clock,
83 .set_control_reg = &s5p_sdhci_set_control_reg,
84};
85
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +090086static int s5p_sdhci_core_init(struct sdhci_host *host)
Jaehoon Chung442d5562012-04-23 02:36:28 +000087{
Jaehoon Chung442d5562012-04-23 02:36:28 +000088 host->name = S5P_NAME;
Jaehoon Chung442d5562012-04-23 02:36:28 +000089
Jaehoon Chungb2686602012-08-30 16:24:08 +000090 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
Jaehoon Chunga034ec02016-07-12 21:18:47 +090091 SDHCI_QUIRK_32BIT_DMA_ADDR |
Jaehoon Chung113e5df2013-07-19 17:44:49 +090092 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010093 host->max_clk = 52000000;
Jaehoon Chung442d5562012-04-23 02:36:28 +000094 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung62226b62016-12-30 15:30:18 +090095 host->ops = &s5p_sdhci_ops;
Jaehoon Chung442d5562012-04-23 02:36:28 +000096
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +090097 if (host->bus_width == 8)
Jaehoon Chung113e5df2013-07-19 17:44:49 +090098 host->host_caps |= MMC_MODE_8BIT;
Jaehoon Chung442d5562012-04-23 02:36:28 +000099
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900100#ifndef CONFIG_BLK
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100101 return add_sdhci(host, 0, 400000);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900102#else
103 return 0;
104#endif
Jaehoon Chung442d5562012-04-23 02:36:28 +0000105}
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100106
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900107int s5p_sdhci_init(u32 regbase, int index, int bus_width)
108{
Tobias Jakobi1a9d1732015-10-05 13:47:50 +0200109 struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900110 if (!host) {
Tobias Jakobi1a9d1732015-10-05 13:47:50 +0200111 printf("sdhci__host allocation fail!\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900112 return -ENOMEM;
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900113 }
114 host->ioaddr = (void *)regbase;
115 host->index = index;
116 host->bus_width = bus_width;
117
118 return s5p_sdhci_core_init(host);
119}
120
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100121static int do_sdhci_init(struct sdhci_host *host)
122{
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200123 int dev_id, flag, ret;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100124
125 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
126 dev_id = host->index + PERIPH_ID_SDMMC0;
127
Przemyslaw Marczak96094d42015-10-28 15:41:50 +0100128 ret = exynos_pinmux_config(dev_id, flag);
129 if (ret) {
130 printf("external SD not configured\n");
131 return ret;
132 }
133
Simon Glass03479602015-01-05 20:05:38 -0700134 if (dm_gpio_is_valid(&host->pwr_gpio)) {
135 dm_gpio_set_value(&host->pwr_gpio, 1);
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200136 ret = exynos_pinmux_config(dev_id, flag);
137 if (ret) {
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100138 debug("MMC not configured\n");
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200139 return ret;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100140 }
141 }
142
Simon Glass03479602015-01-05 20:05:38 -0700143 if (dm_gpio_is_valid(&host->cd_gpio)) {
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200144 ret = dm_gpio_get_value(&host->cd_gpio);
145 if (ret) {
146 debug("no SD card detected (%d)\n", ret);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100147 return -ENODEV;
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200148 }
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100149 }
150
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900151 return s5p_sdhci_core_init(host);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100152}
153
154static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
155{
156 int bus_width, dev_id;
157 unsigned int base;
158
159 /* Get device id */
160 dev_id = pinmux_decode_periph_id(blob, node);
Seung-Woo Kimf0ecfc52016-11-24 15:05:51 +0900161 if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100162 debug("MMC: Can't get device id\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900163 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100164 }
165 host->index = dev_id - PERIPH_ID_SDMMC0;
166
167 /* Get bus width */
168 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
169 if (bus_width <= 0) {
170 debug("MMC: Can't get bus-width\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900171 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100172 }
173 host->bus_width = bus_width;
174
175 /* Get the base address from the device node */
176 base = fdtdec_get_addr(blob, node, "reg");
177 if (!base) {
178 debug("MMC: Can't get base address\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900179 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100180 }
181 host->ioaddr = (void *)base;
182
Simon Glass150c5af2017-05-30 21:47:09 -0600183 gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
184 &host->pwr_gpio, GPIOD_IS_OUT);
185 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0,
186 &host->cd_gpio, GPIOD_IS_IN);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100187
188 return 0;
189}
190
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900191#ifdef CONFIG_DM_MMC
192static int s5p_sdhci_probe(struct udevice *dev)
193{
194 struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
195 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
196 struct sdhci_host *host = dev_get_priv(dev);
197 int ret;
198
Simon Glasse160f7d2017-01-17 16:52:55 -0700199 ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900200 if (ret)
201 return ret;
202
203 ret = do_sdhci_init(host);
204 if (ret)
205 return ret;
206
Marek Szyprowskie27108c2020-01-16 16:25:33 +0100207 ret = mmc_of_parse(dev, &plat->cfg);
208 if (ret)
209 return ret;
210
Peng Fan6f16cbe2019-08-06 02:47:59 +0000211 host->mmc = &plat->mmc;
212 host->mmc->dev = dev;
Marek Szyprowskie27108c2020-01-16 16:25:33 +0100213
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100214 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900215 if (ret)
216 return ret;
217
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900218 host->mmc->priv = host;
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900219 upriv->mmc = host->mmc;
220
221 return sdhci_probe(dev);
222}
223
224static int s5p_sdhci_bind(struct udevice *dev)
225{
226 struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
227 int ret;
228
229 ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
230 if (ret)
231 return ret;
232
233 return 0;
234}
235
236static const struct udevice_id s5p_sdhci_ids[] = {
237 { .compatible = "samsung,exynos4412-sdhci"},
238 { }
239};
240
241U_BOOT_DRIVER(s5p_sdhci_drv) = {
242 .name = "s5p_sdhci",
243 .id = UCLASS_MMC,
244 .of_match = s5p_sdhci_ids,
245 .bind = s5p_sdhci_bind,
246 .ops = &sdhci_ops,
247 .probe = s5p_sdhci_probe,
248 .priv_auto_alloc_size = sizeof(struct sdhci_host),
249 .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
250};
251#endif /* CONFIG_DM_MMC */