blob: 24fabeee953d78eee44994b2cdb8de66c0115aea [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"
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <linux/err.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Michal Simek293eb332013-04-22 14:56:49 +020016#include <malloc.h>
17#include <sdhci.h>
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053018#include <zynqmp_tap_delay.h>
Michal Simek293eb332013-04-22 14:56:49 +020019
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +010020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass329a4492016-07-05 17:10:15 -060022struct arasan_sdhci_plat {
23 struct mmc_config cfg;
24 struct mmc mmc;
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +010025 unsigned int f_max;
Simon Glass329a4492016-07-05 17:10:15 -060026};
27
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053028struct arasan_sdhci_priv {
29 struct sdhci_host *host;
30 u8 deviceid;
31 u8 bank;
32 u8 no_1p8;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053033};
34
35#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +053036#define MMC_HS200_BUS_SPEED 5
37
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053038static const u8 mode2timing[] = {
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +053039 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
40 [SD_LEGACY] = UHS_SDR12_BUS_SPEED,
41 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
42 [SD_HS] = HIGH_SPEED_BUS_SPEED,
43 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
44 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
45 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
46 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
47 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
48 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
49 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
50 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053051};
52
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053053#define SDHCI_TUNING_LOOP_COUNT 40
54
55static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
56{
57 u16 clk;
58 unsigned long timeout;
59
60 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
61 clk &= ~(SDHCI_CLOCK_CARD_EN);
62 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
63
64 /* Issue DLL Reset */
65 zynqmp_dll_reset(deviceid);
66
67 /* Wait max 20 ms */
68 timeout = 100;
69 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
70 & SDHCI_CLOCK_INT_STABLE)) {
71 if (timeout == 0) {
72 dev_err(mmc_dev(host->mmc),
73 ": Internal clock never stabilised.\n");
74 return;
75 }
76 timeout--;
77 udelay(1000);
78 }
79
80 clk |= SDHCI_CLOCK_CARD_EN;
81 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
82}
83
84static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
85{
86 struct mmc_cmd cmd;
87 struct mmc_data data;
88 u32 ctrl;
89 struct sdhci_host *host;
90 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simekb6911782018-06-13 09:12:29 +020091 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053092 u8 deviceid;
93
94 debug("%s\n", __func__);
95
96 host = priv->host;
97 deviceid = priv->deviceid;
98
Faiz Abbasd1c0a222019-06-11 00:43:40 +053099 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530100 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530101 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530102
103 mdelay(1);
104
105 arasan_zynqmp_dll_reset(host, deviceid);
106
107 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
108 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
109
110 do {
111 cmd.cmdidx = opcode;
112 cmd.resp_type = MMC_RSP_R1;
113 cmd.cmdarg = 0;
114
115 data.blocksize = 64;
116 data.blocks = 1;
117 data.flags = MMC_DATA_READ;
118
119 if (tuning_loop_counter-- == 0)
120 break;
121
122 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
123 mmc->bus_width == 8)
124 data.blocksize = 128;
125
126 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
127 data.blocksize),
128 SDHCI_BLOCK_SIZE);
129 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
130 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
131
132 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530133 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530134
135 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
136 udelay(1);
137
138 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
139
140 if (tuning_loop_counter < 0) {
141 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530142 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530143 }
144
145 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
146 printf("%s:Tuning failed\n", __func__);
147 return -1;
148 }
149
150 udelay(1);
151 arasan_zynqmp_dll_reset(host, deviceid);
152
153 /* Enable only interrupts served by the SD controller */
154 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
155 SDHCI_INT_ENABLE);
156 /* Mask all sdhci interrupt sources */
157 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
158
159 return 0;
160}
161
162static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
163{
164 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
165 struct mmc *mmc = (struct mmc *)host->mmc;
166 u8 uhsmode;
167
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530168 uhsmode = mode2timing[mmc->selected_mode];
169
170 if (uhsmode >= UHS_SDR25_BUS_SPEED)
171 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
172 priv->bank);
173}
174
175static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
176{
177 struct mmc *mmc = (struct mmc *)host->mmc;
178 u32 reg;
179
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +0530180 if (!IS_SD(mmc))
181 return;
182
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530183 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530184 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
185 reg |= SDHCI_CTRL_VDD_180;
186 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530187 }
188
189 if (mmc->selected_mode > SD_HS &&
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530190 mmc->selected_mode <= UHS_DDR50)
191 sdhci_set_uhs_timing(host);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530192}
193#endif
194
Siva Durga Prasad Paladuguc95b19a2019-08-02 16:46:26 +0530195#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530196const struct sdhci_ops arasan_ops = {
197 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
198 .set_delay = &arasan_sdhci_set_tapdelay,
199 .set_control_reg = &arasan_sdhci_set_control_reg,
200};
201#endif
202
Michal Simekd9ae52c2015-11-30 16:13:03 +0100203static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek293eb332013-04-22 14:56:49 +0200204{
Simon Glass329a4492016-07-05 17:10:15 -0600205 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100206 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530207 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
208 struct sdhci_host *host;
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100209 struct clk clk;
210 unsigned long clock;
Simon Glass329a4492016-07-05 17:10:15 -0600211 int ret;
Michal Simek293eb332013-04-22 14:56:49 +0200212
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530213 host = priv->host;
214
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100215 ret = clk_get_by_index(dev, 0, &clk);
216 if (ret < 0) {
217 dev_err(dev, "failed to get clock\n");
218 return ret;
219 }
220
221 clock = clk_get_rate(&clk);
222 if (IS_ERR_VALUE(clock)) {
223 dev_err(dev, "failed to get rate\n");
224 return clock;
225 }
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530226
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100227 debug("%s: CLK %ld\n", __func__, clock);
228
229 ret = clk_enable(&clk);
230 if (ret && ret != -ENOSYS) {
231 dev_err(dev, "failed to enable clock\n");
232 return ret;
233 }
234
Siva Durga Prasad Paladugueddabd12014-07-08 15:31:04 +0530235 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladuguf9ec45d2014-01-22 09:17:09 +0100236 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530237
238#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer47819212018-03-07 08:00:57 +0100239 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530240#endif
241
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530242 if (priv->no_1p8)
243 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
244
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100245 host->max_clk = clock;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100246
Matwey V. Kornilov3148a3c2019-08-01 18:00:05 +0300247 host->mmc = &plat->mmc;
248 host->mmc->dev = dev;
249 host->mmc->priv = host;
250
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100251 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
Jaehoon Chung14bed522016-07-26 19:06:24 +0900252 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass329a4492016-07-05 17:10:15 -0600253 if (ret)
254 return ret;
Simon Glass329a4492016-07-05 17:10:15 -0600255 upriv->mmc = host->mmc;
Michal Simekd9ae52c2015-11-30 16:13:03 +0100256
Simon Glass329a4492016-07-05 17:10:15 -0600257 return sdhci_probe(dev);
Michal Simek293eb332013-04-22 14:56:49 +0200258}
Michal Simekd9ae52c2015-11-30 16:13:03 +0100259
260static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
261{
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100262 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530263 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100264
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530265 priv->host = calloc(1, sizeof(struct sdhci_host));
266 if (!priv->host)
267 return -1;
268
269 priv->host->name = dev->name;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530270
Siva Durga Prasad Paladuguc95b19a2019-08-02 16:46:26 +0530271#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530272 priv->host->ops = &arasan_ops;
273#endif
Michal Simekd9ae52c2015-11-30 16:13:03 +0100274
Michal Simek458e8d82018-05-16 10:57:07 +0200275 priv->host->ioaddr = (void *)dev_read_addr(dev);
276 if (IS_ERR(priv->host->ioaddr))
277 return PTR_ERR(priv->host->ioaddr);
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100278
Michal Simek458e8d82018-05-16 10:57:07 +0200279 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
280 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
281 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
282
283 plat->f_max = dev_read_u32_default(dev, "max-frequency",
284 CONFIG_ZYNQ_SDHCI_MAX_FREQ);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100285 return 0;
286}
287
Simon Glass329a4492016-07-05 17:10:15 -0600288static int arasan_sdhci_bind(struct udevice *dev)
289{
290 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass329a4492016-07-05 17:10:15 -0600291
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900292 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass329a4492016-07-05 17:10:15 -0600293}
294
Michal Simekd9ae52c2015-11-30 16:13:03 +0100295static const struct udevice_id arasan_sdhci_ids[] = {
296 { .compatible = "arasan,sdhci-8.9a" },
297 { }
298};
299
300U_BOOT_DRIVER(arasan_sdhci_drv) = {
301 .name = "arasan_sdhci",
302 .id = UCLASS_MMC,
303 .of_match = arasan_sdhci_ids,
304 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass329a4492016-07-05 17:10:15 -0600305 .ops = &sdhci_ops,
306 .bind = arasan_sdhci_bind,
Michal Simekd9ae52c2015-11-30 16:13:03 +0100307 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530308 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass329a4492016-07-05 17:10:15 -0600309 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simekd9ae52c2015-11-30 16:13:03 +0100310};