blob: de404aa9560d61378c9c150d83260c0ce8cd2773 [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>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053013#include "mmc_private.h"
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020018#include <malloc.h>
19#include <sdhci.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053020#include <zynqmp_tap_delay.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020021
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +010022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass4cc87fb2016-07-05 17:10:15 -060024struct arasan_sdhci_plat {
25 struct mmc_config cfg;
26 struct mmc mmc;
27};
28
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053029struct arasan_sdhci_priv {
30 struct sdhci_host *host;
31 u8 deviceid;
32 u8 bank;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053033};
34
35#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +053036#define MMC_HS200_BUS_SPEED 5
37
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053038static const u8 mode2timing[] = {
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +053039 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
Siva Durga Prasad Paladugued9c0122018-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 Paladugucad14a82018-04-19 12:37:09 +053050};
51
Siva Durga Prasad Paladugucad14a82018-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 Simek91e95ff2018-06-13 09:12:29 +020090 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugucad14a82018-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 Abbas2eddc002019-06-11 00:43:40 +053098 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053099 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530100 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-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 Abbas2eddc002019-06-11 00:43:40 +0530132 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-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 Abbas2eddc002019-06-11 00:43:40 +0530141 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-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 Paladugucad14a82018-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 Paladugued9c0122018-05-29 20:03:11 +0530179 if (!IS_SD(mmc))
180 return;
181
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530182 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
Faiz Abbas2eddc002019-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 Paladugucad14a82018-04-19 12:37:09 +0530186 }
187
188 if (mmc->selected_mode > SD_HS &&
Faiz Abbas2eddc002019-06-11 00:43:40 +0530189 mmc->selected_mode <= UHS_DDR50)
190 sdhci_set_uhs_timing(host);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530191}
192#endif
193
Siva Durga Prasad Paladugu9a8ce6a2019-08-02 16:46:26 +0530194#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugucad14a82018-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 Simek9ecd2682015-11-30 16:13:03 +0100202static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200203{
Simon Glass4cc87fb2016-07-05 17:10:15 -0600204 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100205 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530206 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
207 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100208 struct clk clk;
209 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600210 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200211
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530212 host = priv->host;
213
Stefan Herbrechtsmeier739ae402017-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 Paladugucad14a82018-04-19 12:37:09 +0530225
Stefan Herbrechtsmeier739ae402017-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 Paladugu049e0032014-07-08 15:31:04 +0530234 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100235 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530236
237#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100238 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530239#endif
240
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200241 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
242
243 ret = mmc_of_parse(dev, &plat->cfg);
244 if (ret)
245 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530246
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100247 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100248
Matwey V. Kornilov194b8602019-08-01 18:00:05 +0300249 host->mmc = &plat->mmc;
250 host->mmc->dev = dev;
251 host->mmc->priv = host;
252
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200253 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900254 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600255 if (ret)
256 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600257 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100258
Simon Glass4cc87fb2016-07-05 17:10:15 -0600259 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200260}
Michal Simek9ecd2682015-11-30 16:13:03 +0100261
262static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
263{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530264 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100265
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530266 priv->host = calloc(1, sizeof(struct sdhci_host));
267 if (!priv->host)
268 return -1;
269
270 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530271
Siva Durga Prasad Paladugu9a8ce6a2019-08-02 16:46:26 +0530272#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530273 priv->host->ops = &arasan_ops;
274#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100275
Michal Simek921a8de2018-05-16 10:57:07 +0200276 priv->host->ioaddr = (void *)dev_read_addr(dev);
277 if (IS_ERR(priv->host->ioaddr))
278 return PTR_ERR(priv->host->ioaddr);
279
280 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
281 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100282
Michal Simek9ecd2682015-11-30 16:13:03 +0100283 return 0;
284}
285
Simon Glass4cc87fb2016-07-05 17:10:15 -0600286static int arasan_sdhci_bind(struct udevice *dev)
287{
288 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600289
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900290 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600291}
292
Michal Simek9ecd2682015-11-30 16:13:03 +0100293static const struct udevice_id arasan_sdhci_ids[] = {
294 { .compatible = "arasan,sdhci-8.9a" },
295 { }
296};
297
298U_BOOT_DRIVER(arasan_sdhci_drv) = {
299 .name = "arasan_sdhci",
300 .id = UCLASS_MMC,
301 .of_match = arasan_sdhci_ids,
302 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600303 .ops = &sdhci_ops,
304 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100305 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530306 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass4cc87fb2016-07-05 17:10:15 -0600307 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100308};