blob: dee84263c3fd8ed01d65457d2f27dc49152b1fa6 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +000010#include <malloc.h>
11#include <sdhci.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010012#include <fdtdec.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010015#include <asm/gpio.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +000016#include <asm/arch/mmc.h>
Jaehoon Chungb09ed6e2012-08-30 16:24:11 +000017#include <asm/arch/clk.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010018#include <errno.h>
Piotr Wilczek3577fe82014-03-07 14:59:41 +010019#include <asm/arch/pinmux.h>
Jaehoon Chung442d5562012-04-23 02:36:28 +000020
Jaehoon Chung7aedafd2016-09-09 18:23:23 +090021#ifdef CONFIG_DM_MMC
22struct s5p_sdhci_plat {
23 struct mmc_config cfg;
24 struct mmc mmc;
25};
26
27DECLARE_GLOBAL_DATA_PTR;
28#endif
29
Jaehoon Chung442d5562012-04-23 02:36:28 +000030static char *S5P_NAME = "SAMSUNG SDHCI";
31static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
32{
33 unsigned long val, ctrl;
34 /*
35 * SELCLKPADDS[17:16]
36 * 00 = 2mA
37 * 01 = 4mA
38 * 10 = 7mA
39 * 11 = 9mA
40 */
41 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
42
43 val = sdhci_readl(host, SDHCI_CONTROL2);
Matt Reimer8ebde4f2015-02-23 14:52:22 -070044 val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
Jaehoon Chung442d5562012-04-23 02:36:28 +000045
46 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
47 SDHCI_CTRL2_ENCMDCNFMSK |
48 SDHCI_CTRL2_ENFBCLKRX |
49 SDHCI_CTRL2_ENCLKOUTHOLD;
50
51 sdhci_writel(host, val, SDHCI_CONTROL2);
52
53 /*
54 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
55 * FCSel[1:0] : Rx Feedback Clock Delay Control
56 * Inverter delay means10ns delay if SDCLK 50MHz setting
57 * 01 = Delay1 (basic delay)
58 * 11 = Delay2 (basic delay + 2ns)
59 * 00 = Delay3 (inverter delay)
60 * 10 = Delay4 (inverter delay + 2ns)
61 */
Jaehoon Chungb2686602012-08-30 16:24:08 +000062 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
Jaehoon Chung442d5562012-04-23 02:36:28 +000063 sdhci_writel(host, val, SDHCI_CONTROL3);
64
65 /*
66 * SELBASECLK[5:4]
67 * 00/01 = HCLK
68 * 10 = EPLL
69 * 11 = XTI or XEXTCLK
70 */
71 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
72 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
73 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
74 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
75}
76
Jaehoon Chungf73b33f2016-12-30 15:30:17 +090077static void s5p_set_clock(struct sdhci_host *host, u32 div)
78{
79 /* ToDo : Use the Clock Framework */
80 set_mmc_clk(host->index, div);
81}
82
Jaehoon Chung62226b62016-12-30 15:30:18 +090083static const struct sdhci_ops s5p_sdhci_ops = {
84 .set_clock = &s5p_set_clock,
85 .set_control_reg = &s5p_sdhci_set_control_reg,
86};
87
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +090088static int s5p_sdhci_core_init(struct sdhci_host *host)
Jaehoon Chung442d5562012-04-23 02:36:28 +000089{
Jaehoon Chung442d5562012-04-23 02:36:28 +000090 host->name = S5P_NAME;
Jaehoon Chung442d5562012-04-23 02:36:28 +000091
Jaehoon Chungb2686602012-08-30 16:24:08 +000092 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
Jaehoon Chunga034ec02016-07-12 21:18:47 +090093 SDHCI_QUIRK_32BIT_DMA_ADDR |
Jaehoon Chung113e5df2013-07-19 17:44:49 +090094 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010095 host->max_clk = 52000000;
Jaehoon Chung442d5562012-04-23 02:36:28 +000096 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung62226b62016-12-30 15:30:18 +090097 host->ops = &s5p_sdhci_ops;
Jaehoon Chung442d5562012-04-23 02:36:28 +000098
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +090099 if (host->bus_width == 8)
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900100 host->host_caps |= MMC_MODE_8BIT;
Jaehoon Chung442d5562012-04-23 02:36:28 +0000101
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900102#ifndef CONFIG_BLK
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100103 return add_sdhci(host, 0, 400000);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900104#else
105 return 0;
106#endif
Jaehoon Chung442d5562012-04-23 02:36:28 +0000107}
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100108
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900109int s5p_sdhci_init(u32 regbase, int index, int bus_width)
110{
Tobias Jakobi1a9d1732015-10-05 13:47:50 +0200111 struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900112 if (!host) {
Tobias Jakobi1a9d1732015-10-05 13:47:50 +0200113 printf("sdhci__host allocation fail!\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900114 return -ENOMEM;
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900115 }
116 host->ioaddr = (void *)regbase;
117 host->index = index;
118 host->bus_width = bus_width;
119
120 return s5p_sdhci_core_init(host);
121}
122
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100123static int do_sdhci_init(struct sdhci_host *host)
124{
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200125 int dev_id, flag, ret;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100126
127 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
128 dev_id = host->index + PERIPH_ID_SDMMC0;
129
Przemyslaw Marczak96094d42015-10-28 15:41:50 +0100130 ret = exynos_pinmux_config(dev_id, flag);
131 if (ret) {
132 printf("external SD not configured\n");
133 return ret;
134 }
135
Simon Glass03479602015-01-05 20:05:38 -0700136 if (dm_gpio_is_valid(&host->pwr_gpio)) {
137 dm_gpio_set_value(&host->pwr_gpio, 1);
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200138 ret = exynos_pinmux_config(dev_id, flag);
139 if (ret) {
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100140 debug("MMC not configured\n");
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200141 return ret;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100142 }
143 }
144
Simon Glass03479602015-01-05 20:05:38 -0700145 if (dm_gpio_is_valid(&host->cd_gpio)) {
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200146 ret = dm_gpio_get_value(&host->cd_gpio);
147 if (ret) {
148 debug("no SD card detected (%d)\n", ret);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100149 return -ENODEV;
Tobias Jakobi2308ea72015-10-05 13:47:53 +0200150 }
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100151 }
152
Jaehoon Chung9b8c9a32014-05-16 13:59:59 +0900153 return s5p_sdhci_core_init(host);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100154}
155
156static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
157{
158 int bus_width, dev_id;
159 unsigned int base;
160
161 /* Get device id */
162 dev_id = pinmux_decode_periph_id(blob, node);
Seung-Woo Kimf0ecfc52016-11-24 15:05:51 +0900163 if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100164 debug("MMC: Can't get device id\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900165 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100166 }
167 host->index = dev_id - PERIPH_ID_SDMMC0;
168
169 /* Get bus width */
170 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
171 if (bus_width <= 0) {
172 debug("MMC: Can't get bus-width\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900173 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100174 }
175 host->bus_width = bus_width;
176
177 /* Get the base address from the device node */
178 base = fdtdec_get_addr(blob, node, "reg");
179 if (!base) {
180 debug("MMC: Can't get base address\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +0900181 return -EINVAL;
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100182 }
183 host->ioaddr = (void *)base;
184
Simon Glass150c5af2017-05-30 21:47:09 -0600185 gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
186 &host->pwr_gpio, GPIOD_IS_OUT);
187 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0,
188 &host->cd_gpio, GPIOD_IS_IN);
Piotr Wilczek3577fe82014-03-07 14:59:41 +0100189
190 return 0;
191}
192
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900193#ifdef CONFIG_DM_MMC
194static int s5p_sdhci_probe(struct udevice *dev)
195{
Simon Glassc69cda22020-12-03 16:55:20 -0700196 struct s5p_sdhci_plat *plat = dev_get_plat(dev);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900197 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
198 struct sdhci_host *host = dev_get_priv(dev);
199 int ret;
200
Simon Glasse160f7d2017-01-17 16:52:55 -0700201 ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900202 if (ret)
203 return ret;
204
205 ret = do_sdhci_init(host);
206 if (ret)
207 return ret;
208
Marek Szyprowskie27108c2020-01-16 16:25:33 +0100209 ret = mmc_of_parse(dev, &plat->cfg);
210 if (ret)
211 return ret;
212
Peng Fan6f16cbe2019-08-06 02:47:59 +0000213 host->mmc = &plat->mmc;
214 host->mmc->dev = dev;
Marek Szyprowskie27108c2020-01-16 16:25:33 +0100215
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100216 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900217 if (ret)
218 return ret;
219
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900220 host->mmc->priv = host;
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900221 upriv->mmc = host->mmc;
222
223 return sdhci_probe(dev);
224}
225
226static int s5p_sdhci_bind(struct udevice *dev)
227{
Simon Glassc69cda22020-12-03 16:55:20 -0700228 struct s5p_sdhci_plat *plat = dev_get_plat(dev);
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900229 int ret;
230
231 ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
232 if (ret)
233 return ret;
234
235 return 0;
236}
237
238static const struct udevice_id s5p_sdhci_ids[] = {
239 { .compatible = "samsung,exynos4412-sdhci"},
240 { }
241};
242
243U_BOOT_DRIVER(s5p_sdhci_drv) = {
244 .name = "s5p_sdhci",
245 .id = UCLASS_MMC,
246 .of_match = s5p_sdhci_ids,
247 .bind = s5p_sdhci_bind,
248 .ops = &sdhci_ops,
249 .probe = s5p_sdhci_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700250 .priv_auto = sizeof(struct sdhci_host),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700251 .plat_auto = sizeof(struct s5p_sdhci_plat),
Jaehoon Chung7aedafd2016-09-09 18:23:23 +0900252};
253#endif /* CONFIG_DM_MMC */