blob: 1f6d053f96b33d98cd244b2694cd8bca1c7d6822 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek0dd222b2013-04-22 14:56:49 +02002/*
Michal Simek9ecd2682015-11-30 16:13:03 +01003 * (C) Copyright 2013 - 2015 Xilinx, Inc.
Michal Simek0dd222b2013-04-22 14:56:49 +02004 *
5 * Xilinx Zynq SD Host Controller Interface
Michal Simek0dd222b2013-04-22 14:56:49 +02006 */
7
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01008#include <clk.h>
Michal Simek0dd222b2013-04-22 14:56:49 +02009#include <common.h>
Michal Simek9ecd2682015-11-30 16:13:03 +010010#include <dm.h>
Michal Simekc57ba042014-02-24 11:16:31 +010011#include <fdtdec.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053012#include "mmc_private.h"
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020014#include <malloc.h>
15#include <sdhci.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053016#include <zynqmp_tap_delay.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020017
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +010018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass4cc87fb2016-07-05 17:10:15 -060020struct arasan_sdhci_plat {
21 struct mmc_config cfg;
22 struct mmc mmc;
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +010023 unsigned int f_max;
Simon Glass4cc87fb2016-07-05 17:10:15 -060024};
25
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053026struct arasan_sdhci_priv {
27 struct sdhci_host *host;
28 u8 deviceid;
29 u8 bank;
30 u8 no_1p8;
31 bool pwrseq;
32};
33
34#if defined(CONFIG_ARCH_ZYNQMP)
35static const u8 mode2timing[] = {
36 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
37 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
38 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
39 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
40 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
41};
42
43#define SDHCI_HOST_CTRL2 0x3E
44#define SDHCI_CTRL2_MODE_MASK 0x7
45#define SDHCI_18V_SIGNAL 0x8
46#define SDHCI_CTRL_EXEC_TUNING 0x0040
47#define SDHCI_CTRL_TUNED_CLK 0x80
48#define SDHCI_TUNING_LOOP_COUNT 40
49
50static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
51{
52 u16 clk;
53 unsigned long timeout;
54
55 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
56 clk &= ~(SDHCI_CLOCK_CARD_EN);
57 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
58
59 /* Issue DLL Reset */
60 zynqmp_dll_reset(deviceid);
61
62 /* Wait max 20 ms */
63 timeout = 100;
64 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
65 & SDHCI_CLOCK_INT_STABLE)) {
66 if (timeout == 0) {
67 dev_err(mmc_dev(host->mmc),
68 ": Internal clock never stabilised.\n");
69 return;
70 }
71 timeout--;
72 udelay(1000);
73 }
74
75 clk |= SDHCI_CLOCK_CARD_EN;
76 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
77}
78
79static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
80{
81 struct mmc_cmd cmd;
82 struct mmc_data data;
83 u32 ctrl;
84 struct sdhci_host *host;
85 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
86 u8 tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
87 u8 deviceid;
88
89 debug("%s\n", __func__);
90
91 host = priv->host;
92 deviceid = priv->deviceid;
93
94 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
95 ctrl |= SDHCI_CTRL_EXEC_TUNING;
96 sdhci_writew(host, ctrl, SDHCI_HOST_CTRL2);
97
98 mdelay(1);
99
100 arasan_zynqmp_dll_reset(host, deviceid);
101
102 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
103 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
104
105 do {
106 cmd.cmdidx = opcode;
107 cmd.resp_type = MMC_RSP_R1;
108 cmd.cmdarg = 0;
109
110 data.blocksize = 64;
111 data.blocks = 1;
112 data.flags = MMC_DATA_READ;
113
114 if (tuning_loop_counter-- == 0)
115 break;
116
117 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
118 mmc->bus_width == 8)
119 data.blocksize = 128;
120
121 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
122 data.blocksize),
123 SDHCI_BLOCK_SIZE);
124 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
125 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
126
127 mmc_send_cmd(mmc, &cmd, NULL);
128 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
129
130 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
131 udelay(1);
132
133 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
134
135 if (tuning_loop_counter < 0) {
136 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
137 sdhci_writel(host, ctrl, SDHCI_HOST_CTRL2);
138 }
139
140 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
141 printf("%s:Tuning failed\n", __func__);
142 return -1;
143 }
144
145 udelay(1);
146 arasan_zynqmp_dll_reset(host, deviceid);
147
148 /* Enable only interrupts served by the SD controller */
149 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
150 SDHCI_INT_ENABLE);
151 /* Mask all sdhci interrupt sources */
152 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
153
154 return 0;
155}
156
157static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
158{
159 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
160 struct mmc *mmc = (struct mmc *)host->mmc;
161 u8 uhsmode;
162
163 if (!IS_SD(mmc))
164 return;
165
166 uhsmode = mode2timing[mmc->selected_mode];
167
168 if (uhsmode >= UHS_SDR25_BUS_SPEED)
169 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
170 priv->bank);
171}
172
173static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
174{
175 struct mmc *mmc = (struct mmc *)host->mmc;
176 u32 reg;
177
178 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
179 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
180 reg |= SDHCI_18V_SIGNAL;
181 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
182 }
183
184 if (mmc->selected_mode > SD_HS &&
185 mmc->selected_mode <= UHS_DDR50) {
186 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
187 reg &= ~SDHCI_CTRL2_MODE_MASK;
188 switch (mmc->selected_mode) {
189 case UHS_SDR12:
190 reg |= UHS_SDR12_BUS_SPEED;
191 break;
192 case UHS_SDR25:
193 reg |= UHS_SDR25_BUS_SPEED;
194 break;
195 case UHS_SDR50:
196 reg |= UHS_SDR50_BUS_SPEED;
197 break;
198 case UHS_SDR104:
199 reg |= UHS_SDR104_BUS_SPEED;
200 break;
201 case UHS_DDR50:
202 reg |= UHS_DDR50_BUS_SPEED;
203 break;
204 default:
205 break;
206 }
207 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
208 }
209}
210#endif
211
212#if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
213const struct sdhci_ops arasan_ops = {
214 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
215 .set_delay = &arasan_sdhci_set_tapdelay,
216 .set_control_reg = &arasan_sdhci_set_control_reg,
217};
218#endif
219
Michal Simek9ecd2682015-11-30 16:13:03 +0100220static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200221{
Simon Glass4cc87fb2016-07-05 17:10:15 -0600222 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100223 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530224 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
225 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100226 struct clk clk;
227 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600228 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200229
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530230 host = priv->host;
231
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100232 ret = clk_get_by_index(dev, 0, &clk);
233 if (ret < 0) {
234 dev_err(dev, "failed to get clock\n");
235 return ret;
236 }
237
238 clock = clk_get_rate(&clk);
239 if (IS_ERR_VALUE(clock)) {
240 dev_err(dev, "failed to get rate\n");
241 return clock;
242 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530243
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100244 debug("%s: CLK %ld\n", __func__, clock);
245
246 ret = clk_enable(&clk);
247 if (ret && ret != -ENOSYS) {
248 dev_err(dev, "failed to enable clock\n");
249 return ret;
250 }
251
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +0530252 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100253 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530254
255#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100256 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530257#endif
258
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530259 if (priv->no_1p8)
260 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
261
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100262 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100263
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100264 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900265 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600266 host->mmc = &plat->mmc;
267 if (ret)
268 return ret;
269 host->mmc->priv = host;
Simon Glass77ca42b2016-05-01 13:52:34 -0600270 host->mmc->dev = dev;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600271 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100272
Simon Glass4cc87fb2016-07-05 17:10:15 -0600273 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200274}
Michal Simek9ecd2682015-11-30 16:13:03 +0100275
276static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
277{
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100278 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530279 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100280
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530281 priv->host = calloc(1, sizeof(struct sdhci_host));
282 if (!priv->host)
283 return -1;
284
285 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530286
287#if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
288 priv->host->ops = &arasan_ops;
289#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100290
Michal Simek921a8de2018-05-16 10:57:07 +0200291 priv->host->ioaddr = (void *)dev_read_addr(dev);
292 if (IS_ERR(priv->host->ioaddr))
293 return PTR_ERR(priv->host->ioaddr);
294
295 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
296 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
297 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100298
Michal Simek921a8de2018-05-16 10:57:07 +0200299 plat->f_max = dev_read_u32_default(dev, "max-frequency",
300 CONFIG_ZYNQ_SDHCI_MAX_FREQ);
Michal Simek9ecd2682015-11-30 16:13:03 +0100301 return 0;
302}
303
Simon Glass4cc87fb2016-07-05 17:10:15 -0600304static int arasan_sdhci_bind(struct udevice *dev)
305{
306 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600307
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900308 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600309}
310
Michal Simek9ecd2682015-11-30 16:13:03 +0100311static const struct udevice_id arasan_sdhci_ids[] = {
312 { .compatible = "arasan,sdhci-8.9a" },
313 { }
314};
315
316U_BOOT_DRIVER(arasan_sdhci_drv) = {
317 .name = "arasan_sdhci",
318 .id = UCLASS_MMC,
319 .of_match = arasan_sdhci_ids,
320 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600321 .ops = &sdhci_ops,
322 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100323 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530324 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass4cc87fb2016-07-05 17:10:15 -0600325 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100326};