blob: bd9e01474879c631c2c026aebb6901d4d833967b [file] [log] [blame]
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +00001/*
2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Jaehoon Chung <jh80.chung@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +00006 */
7
8#include <common.h>
9#include <malloc.h>
10#include <sdhci.h>
Piotr Wilczek12cf19e2014-03-07 14:59:41 +010011#include <fdtdec.h>
12#include <libfdt.h>
13#include <asm/gpio.h>
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000014#include <asm/arch/mmc.h>
Jaehoon Chungb1929ea2012-08-30 16:24:11 +000015#include <asm/arch/clk.h>
Piotr Wilczek12cf19e2014-03-07 14:59:41 +010016#include <errno.h>
Piotr Wilczek12cf19e2014-03-07 14:59:41 +010017#include <asm/arch/pinmux.h>
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000018
19static char *S5P_NAME = "SAMSUNG SDHCI";
20static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
21{
22 unsigned long val, ctrl;
23 /*
24 * SELCLKPADDS[17:16]
25 * 00 = 2mA
26 * 01 = 4mA
27 * 10 = 7mA
28 * 11 = 9mA
29 */
30 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
31
32 val = sdhci_readl(host, SDHCI_CONTROL2);
Matt Reimer9af8f392015-02-23 14:52:22 -070033 val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000034
35 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
36 SDHCI_CTRL2_ENCMDCNFMSK |
37 SDHCI_CTRL2_ENFBCLKRX |
38 SDHCI_CTRL2_ENCLKOUTHOLD;
39
40 sdhci_writel(host, val, SDHCI_CONTROL2);
41
42 /*
43 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
44 * FCSel[1:0] : Rx Feedback Clock Delay Control
45 * Inverter delay means10ns delay if SDCLK 50MHz setting
46 * 01 = Delay1 (basic delay)
47 * 11 = Delay2 (basic delay + 2ns)
48 * 00 = Delay3 (inverter delay)
49 * 10 = Delay4 (inverter delay + 2ns)
50 */
Jaehoon Chungb36cbac2012-08-30 16:24:08 +000051 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000052 sdhci_writel(host, val, SDHCI_CONTROL3);
53
54 /*
55 * SELBASECLK[5:4]
56 * 00/01 = HCLK
57 * 10 = EPLL
58 * 11 = XTI or XEXTCLK
59 */
60 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
61 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
62 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
63 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
64}
65
Jaehoon Chungb494ac82014-05-16 13:59:59 +090066static int s5p_sdhci_core_init(struct sdhci_host *host)
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000067{
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000068 host->name = S5P_NAME;
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000069
Jaehoon Chungb36cbac2012-08-30 16:24:08 +000070 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
Tushar Behera0fba4c22012-09-20 20:31:57 +000071 SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR |
Jaehoon Chung46e627c2013-07-19 17:44:49 +090072 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000073 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chungb36cbac2012-08-30 16:24:08 +000074 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000075
76 host->set_control_reg = &s5p_sdhci_set_control_reg;
Jaehoon Chungb1929ea2012-08-30 16:24:11 +000077 host->set_clock = set_mmc_clk;
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000078
Jaehoon Chungb494ac82014-05-16 13:59:59 +090079 if (host->bus_width == 8)
Jaehoon Chung46e627c2013-07-19 17:44:49 +090080 host->host_caps |= MMC_MODE_8BIT;
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000081
Jaehoon Chung0c3c2552012-12-13 20:07:12 +000082 return add_sdhci(host, 52000000, 400000);
Jaehoon Chungb0b0c9d2012-04-23 02:36:28 +000083}
Piotr Wilczek12cf19e2014-03-07 14:59:41 +010084
Jaehoon Chungb494ac82014-05-16 13:59:59 +090085int s5p_sdhci_init(u32 regbase, int index, int bus_width)
86{
Tobias Jakobi49b330d2015-10-05 13:47:50 +020087 struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
Jaehoon Chungb494ac82014-05-16 13:59:59 +090088 if (!host) {
Tobias Jakobi49b330d2015-10-05 13:47:50 +020089 printf("sdhci__host allocation fail!\n");
Jaehoon Chungb494ac82014-05-16 13:59:59 +090090 return 1;
91 }
92 host->ioaddr = (void *)regbase;
93 host->index = index;
94 host->bus_width = bus_width;
95
96 return s5p_sdhci_core_init(host);
97}
98
Masahiro Yamada366b24f2015-08-12 07:31:55 +090099#if CONFIG_IS_ENABLED(OF_CONTROL)
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100100struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
101
102static int do_sdhci_init(struct sdhci_host *host)
103{
104 int dev_id, flag;
105 int err = 0;
106
107 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
108 dev_id = host->index + PERIPH_ID_SDMMC0;
109
Simon Glassa30d4ba2015-01-05 20:05:38 -0700110 if (dm_gpio_is_valid(&host->pwr_gpio)) {
111 dm_gpio_set_value(&host->pwr_gpio, 1);
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100112 err = exynos_pinmux_config(dev_id, flag);
113 if (err) {
114 debug("MMC not configured\n");
115 return err;
116 }
117 }
118
Simon Glassa30d4ba2015-01-05 20:05:38 -0700119 if (dm_gpio_is_valid(&host->cd_gpio)) {
120 if (dm_gpio_get_value(&host->cd_gpio))
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100121 return -ENODEV;
122
123 err = exynos_pinmux_config(dev_id, flag);
124 if (err) {
125 printf("external SD not configured\n");
126 return err;
127 }
128 }
129
Jaehoon Chungb494ac82014-05-16 13:59:59 +0900130 return s5p_sdhci_core_init(host);
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100131}
132
133static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
134{
135 int bus_width, dev_id;
136 unsigned int base;
137
138 /* Get device id */
139 dev_id = pinmux_decode_periph_id(blob, node);
140 if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
141 debug("MMC: Can't get device id\n");
142 return -1;
143 }
144 host->index = dev_id - PERIPH_ID_SDMMC0;
145
146 /* Get bus width */
147 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
148 if (bus_width <= 0) {
149 debug("MMC: Can't get bus-width\n");
150 return -1;
151 }
152 host->bus_width = bus_width;
153
154 /* Get the base address from the device node */
155 base = fdtdec_get_addr(blob, node, "reg");
156 if (!base) {
157 debug("MMC: Can't get base address\n");
158 return -1;
159 }
160 host->ioaddr = (void *)base;
161
Simon Glassa30d4ba2015-01-05 20:05:38 -0700162 gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
163 GPIOD_IS_OUT);
164 gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
165 GPIOD_IS_IN);
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100166
167 return 0;
168}
169
170static int process_nodes(const void *blob, int node_list[], int count)
171{
172 struct sdhci_host *host;
173 int i, node;
Tobias Jakobi0ab570c2015-10-05 13:47:51 +0200174 int failed = 0;
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100175
176 debug("%s: count = %d\n", __func__, count);
177
178 /* build sdhci_host[] for each controller */
179 for (i = 0; i < count; i++) {
180 node = node_list[i];
181 if (node <= 0)
182 continue;
183
184 host = &sdhci_host[i];
185
186 if (sdhci_get_config(blob, node, host)) {
187 printf("%s: failed to decode dev %d\n", __func__, i);
Tobias Jakobi0ab570c2015-10-05 13:47:51 +0200188 failed++;
189 continue;
190 }
191
192 if (do_sdhci_init(host)) {
193 printf("%s: failed to initialize dev %d\n", __func__, i);
194 failed++;
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100195 }
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100196 }
Tobias Jakobi0ab570c2015-10-05 13:47:51 +0200197
198 /* we only consider it an error when all nodes fail */
199 return (failed == count ? -1 : 0);
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100200}
201
202int exynos_mmc_init(const void *blob)
203{
204 int count;
205 int node_list[SDHCI_MAX_HOSTS];
206
207 count = fdtdec_find_aliases_for_id(blob, "mmc",
208 COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
209 SDHCI_MAX_HOSTS);
210
Tobias Jakobi0ab570c2015-10-05 13:47:51 +0200211 return process_nodes(blob, node_list, count);
Piotr Wilczek12cf19e2014-03-07 14:59:41 +0100212}
213#endif