blob: da3ff53da10d2c71079b07d26530f8aaec3b6019 [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,
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +053040 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
41 [SD_HS] = HIGH_SPEED_BUS_SPEED,
42 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
43 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
44 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
45 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
46 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
47 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
48 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
49 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053050};
51
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053052#define SDHCI_TUNING_LOOP_COUNT 40
53
54static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
55{
56 u16 clk;
57 unsigned long timeout;
58
59 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
60 clk &= ~(SDHCI_CLOCK_CARD_EN);
61 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
62
63 /* Issue DLL Reset */
64 zynqmp_dll_reset(deviceid);
65
66 /* Wait max 20 ms */
67 timeout = 100;
68 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
69 & SDHCI_CLOCK_INT_STABLE)) {
70 if (timeout == 0) {
71 dev_err(mmc_dev(host->mmc),
72 ": Internal clock never stabilised.\n");
73 return;
74 }
75 timeout--;
76 udelay(1000);
77 }
78
79 clk |= SDHCI_CLOCK_CARD_EN;
80 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
81}
82
83static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
84{
85 struct mmc_cmd cmd;
86 struct mmc_data data;
87 u32 ctrl;
88 struct sdhci_host *host;
89 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simekb6911782018-06-13 09:12:29 +020090 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053091 u8 deviceid;
92
93 debug("%s\n", __func__);
94
95 host = priv->host;
96 deviceid = priv->deviceid;
97
Faiz Abbasd1c0a222019-06-11 00:43:40 +053098 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +053099 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530100 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530101
102 mdelay(1);
103
104 arasan_zynqmp_dll_reset(host, deviceid);
105
106 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
107 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
108
109 do {
110 cmd.cmdidx = opcode;
111 cmd.resp_type = MMC_RSP_R1;
112 cmd.cmdarg = 0;
113
114 data.blocksize = 64;
115 data.blocks = 1;
116 data.flags = MMC_DATA_READ;
117
118 if (tuning_loop_counter-- == 0)
119 break;
120
121 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
122 mmc->bus_width == 8)
123 data.blocksize = 128;
124
125 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
126 data.blocksize),
127 SDHCI_BLOCK_SIZE);
128 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
129 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
130
131 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530132 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530133
134 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
135 udelay(1);
136
137 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
138
139 if (tuning_loop_counter < 0) {
140 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530141 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530142 }
143
144 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
145 printf("%s:Tuning failed\n", __func__);
146 return -1;
147 }
148
149 udelay(1);
150 arasan_zynqmp_dll_reset(host, deviceid);
151
152 /* Enable only interrupts served by the SD controller */
153 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
154 SDHCI_INT_ENABLE);
155 /* Mask all sdhci interrupt sources */
156 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
157
158 return 0;
159}
160
161static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
162{
163 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
164 struct mmc *mmc = (struct mmc *)host->mmc;
165 u8 uhsmode;
166
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530167 uhsmode = mode2timing[mmc->selected_mode];
168
169 if (uhsmode >= UHS_SDR25_BUS_SPEED)
170 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
171 priv->bank);
172}
173
174static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
175{
176 struct mmc *mmc = (struct mmc *)host->mmc;
177 u32 reg;
178
Siva Durga Prasad Paladugu84333702018-05-29 20:03:11 +0530179 if (!IS_SD(mmc))
180 return;
181
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530182 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530183 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
184 reg |= SDHCI_CTRL_VDD_180;
185 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530186 }
187
188 if (mmc->selected_mode > SD_HS &&
Faiz Abbasd1c0a222019-06-11 00:43:40 +0530189 mmc->selected_mode <= UHS_DDR50)
190 sdhci_set_uhs_timing(host);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530191}
192#endif
193
Siva Durga Prasad Paladuguc95b19a2019-08-02 16:46:26 +0530194#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530195const struct sdhci_ops arasan_ops = {
196 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
197 .set_delay = &arasan_sdhci_set_tapdelay,
198 .set_control_reg = &arasan_sdhci_set_control_reg,
199};
200#endif
201
Michal Simekd9ae52c2015-11-30 16:13:03 +0100202static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek293eb332013-04-22 14:56:49 +0200203{
Simon Glass329a4492016-07-05 17:10:15 -0600204 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100205 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530206 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
207 struct sdhci_host *host;
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100208 struct clk clk;
209 unsigned long clock;
Simon Glass329a4492016-07-05 17:10:15 -0600210 int ret;
Michal Simek293eb332013-04-22 14:56:49 +0200211
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530212 host = priv->host;
213
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100214 ret = clk_get_by_index(dev, 0, &clk);
215 if (ret < 0) {
216 dev_err(dev, "failed to get clock\n");
217 return ret;
218 }
219
220 clock = clk_get_rate(&clk);
221 if (IS_ERR_VALUE(clock)) {
222 dev_err(dev, "failed to get rate\n");
223 return clock;
224 }
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530225
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100226 debug("%s: CLK %ld\n", __func__, clock);
227
228 ret = clk_enable(&clk);
229 if (ret && ret != -ENOSYS) {
230 dev_err(dev, "failed to enable clock\n");
231 return ret;
232 }
233
Siva Durga Prasad Paladugueddabd12014-07-08 15:31:04 +0530234 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladuguf9ec45d2014-01-22 09:17:09 +0100235 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530236
237#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer47819212018-03-07 08:00:57 +0100238 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugub2156142016-01-12 15:12:16 +0530239#endif
240
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530241 if (priv->no_1p8)
242 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
243
Stefan Herbrechtsmeiere0f4de12017-01-17 16:27:32 +0100244 host->max_clk = clock;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100245
Matwey V. Kornilov3148a3c2019-08-01 18:00:05 +0300246 host->mmc = &plat->mmc;
247 host->mmc->dev = dev;
248 host->mmc->priv = host;
249
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100250 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
Jaehoon Chung14bed522016-07-26 19:06:24 +0900251 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass329a4492016-07-05 17:10:15 -0600252 if (ret)
253 return ret;
Simon Glass329a4492016-07-05 17:10:15 -0600254 upriv->mmc = host->mmc;
Michal Simekd9ae52c2015-11-30 16:13:03 +0100255
Simon Glass329a4492016-07-05 17:10:15 -0600256 return sdhci_probe(dev);
Michal Simek293eb332013-04-22 14:56:49 +0200257}
Michal Simekd9ae52c2015-11-30 16:13:03 +0100258
259static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
260{
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100261 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530262 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100263
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530264 priv->host = calloc(1, sizeof(struct sdhci_host));
265 if (!priv->host)
266 return -1;
267
268 priv->host->name = dev->name;
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530269
Siva Durga Prasad Paladuguc95b19a2019-08-02 16:46:26 +0530270#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530271 priv->host->ops = &arasan_ops;
272#endif
Michal Simekd9ae52c2015-11-30 16:13:03 +0100273
Michal Simek458e8d82018-05-16 10:57:07 +0200274 priv->host->ioaddr = (void *)dev_read_addr(dev);
275 if (IS_ERR(priv->host->ioaddr))
276 return PTR_ERR(priv->host->ioaddr);
Stefan Herbrechtsmeier61e745d2017-01-17 16:27:33 +0100277
Michal Simek458e8d82018-05-16 10:57:07 +0200278 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
279 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
280 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
281
282 plat->f_max = dev_read_u32_default(dev, "max-frequency",
283 CONFIG_ZYNQ_SDHCI_MAX_FREQ);
Michal Simekd9ae52c2015-11-30 16:13:03 +0100284 return 0;
285}
286
Simon Glass329a4492016-07-05 17:10:15 -0600287static int arasan_sdhci_bind(struct udevice *dev)
288{
289 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass329a4492016-07-05 17:10:15 -0600290
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900291 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass329a4492016-07-05 17:10:15 -0600292}
293
Michal Simekd9ae52c2015-11-30 16:13:03 +0100294static const struct udevice_id arasan_sdhci_ids[] = {
295 { .compatible = "arasan,sdhci-8.9a" },
296 { }
297};
298
299U_BOOT_DRIVER(arasan_sdhci_drv) = {
300 .name = "arasan_sdhci",
301 .id = UCLASS_MMC,
302 .of_match = arasan_sdhci_ids,
303 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass329a4492016-07-05 17:10:15 -0600304 .ops = &sdhci_ops,
305 .bind = arasan_sdhci_bind,
Michal Simekd9ae52c2015-11-30 16:13:03 +0100306 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugud1f4e392018-04-19 12:37:09 +0530307 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass329a4492016-07-05 17:10:15 -0600308 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simekd9ae52c2015-11-30 16:13:03 +0100309};