blob: 08023783de48d78dc1c8a2735646e3de12ce6801 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek293eb332013-04-22 14:56:49 +02002/*
Michal Simekd9ae52c2015-11-30 16:13:03 +01003 * (C) Copyright 2013 - 2015 Xilinx, Inc.
Michal Simek293eb332013-04-22 14:56:49 +02004 *
5 * Xilinx Zynq SD Host Controller Interface
Michal Simek293eb332013-04-22 14:56:49 +02006 */
7
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +01008#include <clk.h>
Michal Simek293eb332013-04-22 14:56:49 +02009#include <common.h>
Michal Simekd9ae52c2015-11-30 16:13:03 +010010#include <dm.h>
Michal Simek345d3c02014-02-24 11:16:31 +010011#include <fdtdec.h>
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053012#include "mmc_private.h"
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Michal Simek293eb332013-04-22 14:56:49 +020014#include <malloc.h>
15#include <sdhci.h>
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053016#include <zynqmp_tap_delay.h>
Michal Simek293eb332013-04-22 14:56:49 +020017
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +010018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass329a4492016-07-05 17:10:15 -060020struct arasan_sdhci_plat {
21 struct mmc_config cfg;
22 struct mmc mmc;
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +010023 unsigned int f_max;
Simon Glass329a4492016-07-05 17:10:15 -060024};
25
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053026struct arasan_sdhci_priv {
27 struct sdhci_host *host;
28 u8 deviceid;
29 u8 bank;
30 u8 no_1p8;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053031};
32
33#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +053034#define MMC_HS200_BUS_SPEED 5
35
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053036static const u8 mode2timing[] = {
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +053037 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
38 [SD_LEGACY] = UHS_SDR12_BUS_SPEED,
39 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
40 [SD_HS] = HIGH_SPEED_BUS_SPEED,
41 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
42 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
43 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
44 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
45 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
46 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
47 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
48 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053049};
50
51#define SDHCI_HOST_CTRL2 0x3E
52#define SDHCI_CTRL2_MODE_MASK 0x7
53#define SDHCI_18V_SIGNAL 0x8
54#define SDHCI_CTRL_EXEC_TUNING 0x0040
55#define SDHCI_CTRL_TUNED_CLK 0x80
56#define SDHCI_TUNING_LOOP_COUNT 40
57
58static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
59{
60 u16 clk;
61 unsigned long timeout;
62
63 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
64 clk &= ~(SDHCI_CLOCK_CARD_EN);
65 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
66
67 /* Issue DLL Reset */
68 zynqmp_dll_reset(deviceid);
69
70 /* Wait max 20 ms */
71 timeout = 100;
72 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
73 & SDHCI_CLOCK_INT_STABLE)) {
74 if (timeout == 0) {
75 dev_err(mmc_dev(host->mmc),
76 ": Internal clock never stabilised.\n");
77 return;
78 }
79 timeout--;
80 udelay(1000);
81 }
82
83 clk |= SDHCI_CLOCK_CARD_EN;
84 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
85}
86
87static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
88{
89 struct mmc_cmd cmd;
90 struct mmc_data data;
91 u32 ctrl;
92 struct sdhci_host *host;
93 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simekb6911782018-06-13 09:12:29 +020094 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053095 u8 deviceid;
96
97 debug("%s\n", __func__);
98
99 host = priv->host;
100 deviceid = priv->deviceid;
101
102 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
103 ctrl |= SDHCI_CTRL_EXEC_TUNING;
104 sdhci_writew(host, ctrl, SDHCI_HOST_CTRL2);
105
106 mdelay(1);
107
108 arasan_zynqmp_dll_reset(host, deviceid);
109
110 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
111 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
112
113 do {
114 cmd.cmdidx = opcode;
115 cmd.resp_type = MMC_RSP_R1;
116 cmd.cmdarg = 0;
117
118 data.blocksize = 64;
119 data.blocks = 1;
120 data.flags = MMC_DATA_READ;
121
122 if (tuning_loop_counter-- == 0)
123 break;
124
125 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
126 mmc->bus_width == 8)
127 data.blocksize = 128;
128
129 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
130 data.blocksize),
131 SDHCI_BLOCK_SIZE);
132 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
133 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
134
135 mmc_send_cmd(mmc, &cmd, NULL);
136 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
137
138 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
139 udelay(1);
140
141 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
142
143 if (tuning_loop_counter < 0) {
144 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
145 sdhci_writel(host, ctrl, SDHCI_HOST_CTRL2);
146 }
147
148 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
149 printf("%s:Tuning failed\n", __func__);
150 return -1;
151 }
152
153 udelay(1);
154 arasan_zynqmp_dll_reset(host, deviceid);
155
156 /* Enable only interrupts served by the SD controller */
157 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
158 SDHCI_INT_ENABLE);
159 /* Mask all sdhci interrupt sources */
160 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
161
162 return 0;
163}
164
165static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
166{
167 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
168 struct mmc *mmc = (struct mmc *)host->mmc;
169 u8 uhsmode;
170
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530171 uhsmode = mode2timing[mmc->selected_mode];
172
173 if (uhsmode >= UHS_SDR25_BUS_SPEED)
174 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
175 priv->bank);
176}
177
178static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
179{
180 struct mmc *mmc = (struct mmc *)host->mmc;
181 u32 reg;
182
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +0530183 if (!IS_SD(mmc))
184 return;
185
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530186 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
187 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
188 reg |= SDHCI_18V_SIGNAL;
189 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
190 }
191
192 if (mmc->selected_mode > SD_HS &&
193 mmc->selected_mode <= UHS_DDR50) {
194 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
195 reg &= ~SDHCI_CTRL2_MODE_MASK;
196 switch (mmc->selected_mode) {
197 case UHS_SDR12:
198 reg |= UHS_SDR12_BUS_SPEED;
199 break;
200 case UHS_SDR25:
201 reg |= UHS_SDR25_BUS_SPEED;
202 break;
203 case UHS_SDR50:
204 reg |= UHS_SDR50_BUS_SPEED;
205 break;
206 case UHS_SDR104:
207 reg |= UHS_SDR104_BUS_SPEED;
208 break;
209 case UHS_DDR50:
210 reg |= UHS_DDR50_BUS_SPEED;
211 break;
212 default:
213 break;
214 }
215 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
216 }
217}
218#endif
219
220#if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
221const struct sdhci_ops arasan_ops = {
222 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
223 .set_delay = &arasan_sdhci_set_tapdelay,
224 .set_control_reg = &arasan_sdhci_set_control_reg,
225};
226#endif
227
Michal Simekd9ae52c2015-11-30 16:13:03 +0100228static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek293eb332013-04-22 14:56:49 +0200229{
Simon Glass329a4492016-07-05 17:10:15 -0600230 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100231 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530232 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
233 struct sdhci_host *host;
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100234 struct clk clk;
235 unsigned long clock;
Simon Glass329a4492016-07-05 17:10:15 -0600236 int ret;
Michal Simek293eb332013-04-22 14:56:49 +0200237
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530238 host = priv->host;
239
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100240 ret = clk_get_by_index(dev, 0, &clk);
241 if (ret < 0) {
242 dev_err(dev, "failed to get clock\n");
243 return ret;
244 }
245
246 clock = clk_get_rate(&clk);
247 if (IS_ERR_VALUE(clock)) {
248 dev_err(dev, "failed to get rate\n");
249 return clock;
250 }
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530251
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100252 debug("%s: CLK %ld\n", __func__, clock);
253
254 ret = clk_enable(&clk);
255 if (ret && ret != -ENOSYS) {
256 dev_err(dev, "failed to enable clock\n");
257 return ret;
258 }
259
Siva Durga Prasad Paladugueddabd12014-07-08 15:31:04 +0530260 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladuguf9ec45d2014-01-22 09:17:09 +0100261 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530262
263#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer47819212018-03-07 08:00:57 +0100264 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530265#endif
266
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530267 if (priv->no_1p8)
268 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
269
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100270 host->max_clk = clock;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100271
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100272 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
Jaehoon Chung14bed522016-07-26 19:06:24 +0900273 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass329a4492016-07-05 17:10:15 -0600274 host->mmc = &plat->mmc;
275 if (ret)
276 return ret;
277 host->mmc->priv = host;
Simon Glasscffe5d82016-05-01 13:52:34 -0600278 host->mmc->dev = dev;
Simon Glass329a4492016-07-05 17:10:15 -0600279 upriv->mmc = host->mmc;
Michal Simekd9ae52c2015-11-30 16:13:03 +0100280
Simon Glass329a4492016-07-05 17:10:15 -0600281 return sdhci_probe(dev);
Michal Simek293eb332013-04-22 14:56:49 +0200282}
Michal Simekd9ae52c2015-11-30 16:13:03 +0100283
284static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
285{
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100286 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530287 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100288
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530289 priv->host = calloc(1, sizeof(struct sdhci_host));
290 if (!priv->host)
291 return -1;
292
293 priv->host->name = dev->name;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530294
295#if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
296 priv->host->ops = &arasan_ops;
297#endif
Michal Simekd9ae52c2015-11-30 16:13:03 +0100298
Michal Simek458e8d82018-05-16 10:57:07 +0200299 priv->host->ioaddr = (void *)dev_read_addr(dev);
300 if (IS_ERR(priv->host->ioaddr))
301 return PTR_ERR(priv->host->ioaddr);
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100302
Michal Simek458e8d82018-05-16 10:57:07 +0200303 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
304 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
305 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
306
307 plat->f_max = dev_read_u32_default(dev, "max-frequency",
308 CONFIG_ZYNQ_SDHCI_MAX_FREQ);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100309 return 0;
310}
311
Simon Glass329a4492016-07-05 17:10:15 -0600312static int arasan_sdhci_bind(struct udevice *dev)
313{
314 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass329a4492016-07-05 17:10:15 -0600315
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900316 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass329a4492016-07-05 17:10:15 -0600317}
318
Michal Simekd9ae52c2015-11-30 16:13:03 +0100319static const struct udevice_id arasan_sdhci_ids[] = {
320 { .compatible = "arasan,sdhci-8.9a" },
321 { }
322};
323
324U_BOOT_DRIVER(arasan_sdhci_drv) = {
325 .name = "arasan_sdhci",
326 .id = UCLASS_MMC,
327 .of_match = arasan_sdhci_ids,
328 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass329a4492016-07-05 17:10:15 -0600329 .ops = &sdhci_ops,
330 .bind = arasan_sdhci_bind,
Michal Simekd9ae52c2015-11-30 16:13:03 +0100331 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530332 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass329a4492016-07-05 17:10:15 -0600333 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simekd9ae52c2015-11-30 16:13:03 +0100334};