blob: c4e88790bc68e713c834871164ffa3a53114dc4f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lei Wen142c8f92011-06-28 21:50:06 +00002/*
3 * Copyright 2011, Marvell Semiconductor Inc.
4 * Lei Wen <leiwen@marvell.com>
5 *
Lei Wen142c8f92011-06-28 21:50:06 +00006 * Back ported to the 8xx platform (from the 8260 platform) by
7 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
8 */
9
10#include <common.h>
Simon Glassb0842072016-06-12 23:30:27 -060011#include <errno.h>
Lei Wen142c8f92011-06-28 21:50:06 +000012#include <malloc.h>
13#include <mmc.h>
14#include <sdhci.h>
T Karthik Reddyc8a0ec02019-06-25 13:39:04 +020015#include <dm.h>
Lei Wen142c8f92011-06-28 21:50:06 +000016
Stefan Roese13a547f2015-06-29 14:58:09 +020017#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
18void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
19#else
Lei Wen142c8f92011-06-28 21:50:06 +000020void *aligned_buffer;
Stefan Roese13a547f2015-06-29 14:58:09 +020021#endif
Lei Wen142c8f92011-06-28 21:50:06 +000022
23static void sdhci_reset(struct sdhci_host *host, u8 mask)
24{
25 unsigned long timeout;
26
27 /* Wait max 100 ms */
28 timeout = 100;
29 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
30 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
31 if (timeout == 0) {
Darwin Rambo43558132013-12-19 15:13:25 -080032 printf("%s: Reset 0x%x never completed.\n",
33 __func__, (int)mask);
Lei Wen142c8f92011-06-28 21:50:06 +000034 return;
35 }
36 timeout--;
37 udelay(1000);
38 }
39}
40
41static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
42{
43 int i;
44 if (cmd->resp_type & MMC_RSP_136) {
45 /* CRC is stripped so we need to do some shifting. */
46 for (i = 0; i < 4; i++) {
47 cmd->response[i] = sdhci_readl(host,
48 SDHCI_RESPONSE + (3-i)*4) << 8;
49 if (i != 3)
50 cmd->response[i] |= sdhci_readb(host,
51 SDHCI_RESPONSE + (3-i)*4-1);
52 }
53 } else {
54 cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
55 }
56}
57
58static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
59{
60 int i;
61 char *offs;
62 for (i = 0; i < data->blocksize; i += 4) {
63 offs = data->dest + i;
64 if (data->flags == MMC_DATA_READ)
65 *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
66 else
67 sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
68 }
69}
Faiz Abbas4c082a62019-04-16 23:06:58 +053070
71#if CONFIG_IS_ENABLED(MMC_SDHCI_ADMA)
72static void sdhci_adma_desc(struct sdhci_host *host, char *buf, u16 len,
73 bool end)
74{
75 struct sdhci_adma_desc *desc;
76 u8 attr;
77
78 desc = &host->adma_desc_table[host->desc_slot];
79
80 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
81 if (!end)
82 host->desc_slot++;
83 else
84 attr |= ADMA_DESC_ATTR_END;
85
86 desc->attr = attr;
87 desc->len = len;
88 desc->reserved = 0;
89 desc->addr_lo = (dma_addr_t)buf;
90#ifdef CONFIG_DMA_ADDR_T_64BIT
91 desc->addr_hi = (u64)buf >> 32;
92#endif
93}
94
95static void sdhci_prepare_adma_table(struct sdhci_host *host,
96 struct mmc_data *data)
97{
98 uint trans_bytes = data->blocksize * data->blocks;
99 uint desc_count = DIV_ROUND_UP(trans_bytes, ADMA_MAX_LEN);
100 int i = desc_count;
101 char *buf;
102
103 host->desc_slot = 0;
104
105 if (data->flags & MMC_DATA_READ)
106 buf = data->dest;
107 else
108 buf = (char *)data->src;
109
110 while (--i) {
111 sdhci_adma_desc(host, buf, ADMA_MAX_LEN, false);
112 buf += ADMA_MAX_LEN;
113 trans_bytes -= ADMA_MAX_LEN;
114 }
115
116 sdhci_adma_desc(host, buf, trans_bytes, true);
117
118 flush_cache((dma_addr_t)host->adma_desc_table,
119 ROUND(desc_count * sizeof(struct sdhci_adma_desc),
120 ARCH_DMA_MINALIGN));
121}
122#elif defined(CONFIG_MMC_SDHCI_SDMA)
123static void sdhci_prepare_adma_table(struct sdhci_host *host,
124 struct mmc_data *data)
125{}
126#endif
127#if (defined(CONFIG_MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
Faiz Abbas87102502019-04-16 23:06:57 +0530128static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
129 int *is_aligned, int trans_bytes)
130{
Jaehoon Chungf77f0582012-09-20 20:31:55 +0000131 unsigned char ctrl;
Faiz Abbas87102502019-04-16 23:06:57 +0530132
133 if (data->flags == MMC_DATA_READ)
134 host->start_addr = (dma_addr_t)data->dest;
135 else
136 host->start_addr = (dma_addr_t)data->src;
137
Juhyun \(Justin\) Oh7d48a732013-09-13 18:06:00 +0000138 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Jaehoon Chungf77f0582012-09-20 20:31:55 +0000139 ctrl &= ~SDHCI_CTRL_DMA_MASK;
Faiz Abbas4c082a62019-04-16 23:06:58 +0530140 if (host->flags & USE_ADMA64)
141 ctrl |= SDHCI_CTRL_ADMA64;
142 else if (host->flags & USE_ADMA)
143 ctrl |= SDHCI_CTRL_ADMA32;
Juhyun \(Justin\) Oh7d48a732013-09-13 18:06:00 +0000144 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Faiz Abbas87102502019-04-16 23:06:57 +0530145
Faiz Abbas4c082a62019-04-16 23:06:58 +0530146 if (host->flags & USE_SDMA) {
147 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
148 (host->start_addr & 0x7) != 0x0) {
149 *is_aligned = 0;
150 host->start_addr = (unsigned long)aligned_buffer;
151 if (data->flags != MMC_DATA_READ)
152 memcpy(aligned_buffer, data->src, trans_bytes);
153 }
154
Faiz Abbas87102502019-04-16 23:06:57 +0530155#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
Faiz Abbas4c082a62019-04-16 23:06:58 +0530156 /*
157 * Always use this bounce-buffer when
158 * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
159 */
160 *is_aligned = 0;
161 host->start_addr = (unsigned long)aligned_buffer;
162 if (data->flags != MMC_DATA_READ)
163 memcpy(aligned_buffer, data->src, trans_bytes);
Faiz Abbas87102502019-04-16 23:06:57 +0530164#endif
Faiz Abbas4c082a62019-04-16 23:06:58 +0530165 sdhci_writel(host, host->start_addr, SDHCI_DMA_ADDRESS);
166
167 } else if (host->flags & (USE_ADMA | USE_ADMA64)) {
168 sdhci_prepare_adma_table(host, data);
169
170 sdhci_writel(host, (u32)host->adma_addr, SDHCI_ADMA_ADDRESS);
171 if (host->flags & USE_ADMA64)
172 sdhci_writel(host, (u64)host->adma_addr >> 32,
173 SDHCI_ADMA_ADDRESS_HI);
174 }
175
Faiz Abbas87102502019-04-16 23:06:57 +0530176 flush_cache(host->start_addr, ROUND(trans_bytes, ARCH_DMA_MINALIGN));
177}
178#else
179static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
180 int *is_aligned, int trans_bytes)
181{}
Jaehoon Chungf77f0582012-09-20 20:31:55 +0000182#endif
Faiz Abbas87102502019-04-16 23:06:57 +0530183static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data)
184{
185 dma_addr_t start_addr = host->start_addr;
186 unsigned int stat, rdy, mask, timeout, block = 0;
187 bool transfer_done = false;
Lei Wen142c8f92011-06-28 21:50:06 +0000188
Jaehoon Chung30686bd2012-09-20 20:31:54 +0000189 timeout = 1000000;
Lei Wen142c8f92011-06-28 21:50:06 +0000190 rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
191 mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
192 do {
193 stat = sdhci_readl(host, SDHCI_INT_STATUS);
194 if (stat & SDHCI_INT_ERROR) {
Masahiro Yamada45256c42017-12-30 02:00:12 +0900195 pr_debug("%s: Error detected in status(0x%X)!\n",
196 __func__, stat);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900197 return -EIO;
Lei Wen142c8f92011-06-28 21:50:06 +0000198 }
Alex Deymod9b70232017-04-02 01:24:34 -0700199 if (!transfer_done && (stat & rdy)) {
Lei Wen142c8f92011-06-28 21:50:06 +0000200 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
201 continue;
202 sdhci_writel(host, rdy, SDHCI_INT_STATUS);
203 sdhci_transfer_pio(host, data);
204 data->dest += data->blocksize;
Alex Deymod9b70232017-04-02 01:24:34 -0700205 if (++block >= data->blocks) {
206 /* Keep looping until the SDHCI_INT_DATA_END is
207 * cleared, even if we finished sending all the
208 * blocks.
209 */
210 transfer_done = true;
211 continue;
212 }
Lei Wen142c8f92011-06-28 21:50:06 +0000213 }
Faiz Abbas4c082a62019-04-16 23:06:58 +0530214 if ((host->flags & USE_DMA) && !transfer_done &&
Faiz Abbas87102502019-04-16 23:06:57 +0530215 (stat & SDHCI_INT_DMA_END)) {
Lei Wen142c8f92011-06-28 21:50:06 +0000216 sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
Faiz Abbas4c082a62019-04-16 23:06:58 +0530217 if (host->flags & USE_SDMA) {
218 start_addr &=
219 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
220 start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
221 sdhci_writel(host, start_addr,
222 SDHCI_DMA_ADDRESS);
223 }
Lei Wen142c8f92011-06-28 21:50:06 +0000224 }
Lei Wen6c13c662011-10-08 04:14:57 +0000225 if (timeout-- > 0)
226 udelay(10);
227 else {
Darwin Rambo43558132013-12-19 15:13:25 -0800228 printf("%s: Transfer data timeout\n", __func__);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900229 return -ETIMEDOUT;
Lei Wen6c13c662011-10-08 04:14:57 +0000230 }
Lei Wen142c8f92011-06-28 21:50:06 +0000231 } while (!(stat & SDHCI_INT_DATA_END));
232 return 0;
233}
234
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200235/*
236 * No command will be sent by driver if card is busy, so driver must wait
237 * for card ready state.
238 * Every time when card is busy after timeout then (last) timeout value will be
239 * increased twice but only if it doesn't exceed global defined maximum.
Masahiro Yamada96250112016-08-25 16:07:39 +0900240 * Each function call will use last timeout value.
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200241 */
Masahiro Yamada96250112016-08-25 16:07:39 +0900242#define SDHCI_CMD_MAX_TIMEOUT 3200
Masahiro Yamadad4512312016-08-25 16:07:38 +0900243#define SDHCI_CMD_DEFAULT_TIMEOUT 100
Steve Raed4780832016-06-29 13:42:01 -0700244#define SDHCI_READ_STATUS_TIMEOUT 1000
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200245
Simon Glasseba48f92017-07-29 11:35:31 -0600246#ifdef CONFIG_DM_MMC
Simon Glassb97f0fa2016-06-12 23:30:28 -0600247static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd,
248 struct mmc_data *data)
249{
250 struct mmc *mmc = mmc_get_mmc_dev(dev);
251
252#else
Jeroen Hofsteeee54c7b2014-10-08 22:57:43 +0200253static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
Simon Glassb97f0fa2016-06-12 23:30:28 -0600254 struct mmc_data *data)
Lei Wen142c8f92011-06-28 21:50:06 +0000255{
Simon Glassb97f0fa2016-06-12 23:30:28 -0600256#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200257 struct sdhci_host *host = mmc->priv;
Lei Wen142c8f92011-06-28 21:50:06 +0000258 unsigned int stat = 0;
259 int ret = 0;
260 int trans_bytes = 0, is_aligned = 1;
261 u32 mask, flags, mode;
Faiz Abbas87102502019-04-16 23:06:57 +0530262 unsigned int time = 0;
Simon Glass97c78e82016-05-14 14:03:04 -0600263 int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
Vipul Kumardbad7b42018-05-03 12:20:54 +0530264 ulong start = get_timer(0);
Lei Wen142c8f92011-06-28 21:50:06 +0000265
Faiz Abbas87102502019-04-16 23:06:57 +0530266 host->start_addr = 0;
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200267 /* Timeout unit - ms */
Masahiro Yamadad4512312016-08-25 16:07:38 +0900268 static unsigned int cmd_timeout = SDHCI_CMD_DEFAULT_TIMEOUT;
Lei Wen142c8f92011-06-28 21:50:06 +0000269
Lei Wen142c8f92011-06-28 21:50:06 +0000270 mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
271
272 /* We shouldn't wait for data inihibit for stop commands, even
273 though they might use busy signaling */
Siva Durga Prasad Paladugudb620bd2018-04-19 12:37:05 +0530274 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION ||
Siva Durga Prasad Paladugub97e99f2018-06-13 11:43:01 +0530275 ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
276 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data))
Lei Wen142c8f92011-06-28 21:50:06 +0000277 mask &= ~SDHCI_DATA_INHIBIT;
278
279 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200280 if (time >= cmd_timeout) {
Darwin Rambo43558132013-12-19 15:13:25 -0800281 printf("%s: MMC: %d busy ", __func__, mmc_dev);
Masahiro Yamada96250112016-08-25 16:07:39 +0900282 if (2 * cmd_timeout <= SDHCI_CMD_MAX_TIMEOUT) {
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200283 cmd_timeout += cmd_timeout;
284 printf("timeout increasing to: %u ms.\n",
285 cmd_timeout);
286 } else {
287 puts("timeout.\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900288 return -ECOMM;
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200289 }
Lei Wen142c8f92011-06-28 21:50:06 +0000290 }
Przemyslaw Marczakadccccf2013-10-08 18:12:09 +0200291 time++;
Lei Wen142c8f92011-06-28 21:50:06 +0000292 udelay(1000);
293 }
294
Jorge Ramirez-Ortiz65da8be2017-11-02 15:10:21 +0100295 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
296
Lei Wen142c8f92011-06-28 21:50:06 +0000297 mask = SDHCI_INT_RESPONSE;
Siva Durga Prasad Paladugub97e99f2018-06-13 11:43:01 +0530298 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
299 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data)
Siva Durga Prasad Paladugudb620bd2018-04-19 12:37:05 +0530300 mask = SDHCI_INT_DATA_AVAIL;
301
Lei Wen142c8f92011-06-28 21:50:06 +0000302 if (!(cmd->resp_type & MMC_RSP_PRESENT))
303 flags = SDHCI_CMD_RESP_NONE;
304 else if (cmd->resp_type & MMC_RSP_136)
305 flags = SDHCI_CMD_RESP_LONG;
306 else if (cmd->resp_type & MMC_RSP_BUSY) {
307 flags = SDHCI_CMD_RESP_SHORT_BUSY;
Jaehoon Chungd0d1b252016-07-12 21:18:46 +0900308 if (data)
309 mask |= SDHCI_INT_DATA_END;
Lei Wen142c8f92011-06-28 21:50:06 +0000310 } else
311 flags = SDHCI_CMD_RESP_SHORT;
312
313 if (cmd->resp_type & MMC_RSP_CRC)
314 flags |= SDHCI_CMD_CRC;
315 if (cmd->resp_type & MMC_RSP_OPCODE)
316 flags |= SDHCI_CMD_INDEX;
Siva Durga Prasad Paladugu5d88ba72018-05-29 20:03:10 +0530317 if (data || cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
318 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
Lei Wen142c8f92011-06-28 21:50:06 +0000319 flags |= SDHCI_CMD_DATA;
320
Darwin Rambo43558132013-12-19 15:13:25 -0800321 /* Set Transfer mode regarding to data flag */
Heinrich Schuchardt730636b2017-11-10 21:13:34 +0100322 if (data) {
Lei Wen142c8f92011-06-28 21:50:06 +0000323 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
324 mode = SDHCI_TRNS_BLK_CNT_EN;
325 trans_bytes = data->blocks * data->blocksize;
326 if (data->blocks > 1)
327 mode |= SDHCI_TRNS_MULTI;
328
329 if (data->flags == MMC_DATA_READ)
330 mode |= SDHCI_TRNS_READ;
331
Faiz Abbas4c082a62019-04-16 23:06:58 +0530332 if (host->flags & USE_DMA) {
Faiz Abbas87102502019-04-16 23:06:57 +0530333 mode |= SDHCI_TRNS_DMA;
334 sdhci_prepare_dma(host, data, &is_aligned, trans_bytes);
Lei Wen142c8f92011-06-28 21:50:06 +0000335 }
336
Lei Wen142c8f92011-06-28 21:50:06 +0000337 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
338 data->blocksize),
339 SDHCI_BLOCK_SIZE);
340 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
341 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Kevin Liu8e5db912015-03-23 17:57:00 -0500342 } else if (cmd->resp_type & MMC_RSP_BUSY) {
343 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
Lei Wen142c8f92011-06-28 21:50:06 +0000344 }
345
346 sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
Lei Wen142c8f92011-06-28 21:50:06 +0000347 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
Stefan Roese42817a42015-06-29 14:58:08 +0200348 start = get_timer(0);
Lei Wen142c8f92011-06-28 21:50:06 +0000349 do {
350 stat = sdhci_readl(host, SDHCI_INT_STATUS);
351 if (stat & SDHCI_INT_ERROR)
352 break;
Lei Wen142c8f92011-06-28 21:50:06 +0000353
Masahiro Yamadaa63aaa02016-07-10 00:40:22 +0900354 if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
355 if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
356 return 0;
357 } else {
358 printf("%s: Timeout for status update!\n",
359 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900360 return -ETIMEDOUT;
Masahiro Yamadaa63aaa02016-07-10 00:40:22 +0900361 }
Jaehoon Chung89237a82012-04-23 02:36:25 +0000362 }
Masahiro Yamadaa63aaa02016-07-10 00:40:22 +0900363 } while ((stat & mask) != mask);
Jaehoon Chung89237a82012-04-23 02:36:25 +0000364
Lei Wen142c8f92011-06-28 21:50:06 +0000365 if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
366 sdhci_cmd_done(host, cmd);
367 sdhci_writel(host, mask, SDHCI_INT_STATUS);
368 } else
369 ret = -1;
370
371 if (!ret && data)
Faiz Abbas87102502019-04-16 23:06:57 +0530372 ret = sdhci_transfer_data(host, data);
Lei Wen142c8f92011-06-28 21:50:06 +0000373
Tushar Behera0fba4c22012-09-20 20:31:57 +0000374 if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
375 udelay(1000);
376
Lei Wen142c8f92011-06-28 21:50:06 +0000377 stat = sdhci_readl(host, SDHCI_INT_STATUS);
378 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
379 if (!ret) {
380 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
381 !is_aligned && (data->flags == MMC_DATA_READ))
382 memcpy(data->dest, aligned_buffer, trans_bytes);
383 return 0;
384 }
385
386 sdhci_reset(host, SDHCI_RESET_CMD);
387 sdhci_reset(host, SDHCI_RESET_DATA);
388 if (stat & SDHCI_INT_TIMEOUT)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900389 return -ETIMEDOUT;
Lei Wen142c8f92011-06-28 21:50:06 +0000390 else
Jaehoon Chung7825d202016-07-19 16:33:36 +0900391 return -ECOMM;
Lei Wen142c8f92011-06-28 21:50:06 +0000392}
393
Siva Durga Prasad Paladugu1f67b492018-04-19 12:37:07 +0530394#if defined(CONFIG_DM_MMC) && defined(MMC_SUPPORTS_TUNING)
395static int sdhci_execute_tuning(struct udevice *dev, uint opcode)
396{
397 int err;
398 struct mmc *mmc = mmc_get_mmc_dev(dev);
399 struct sdhci_host *host = mmc->priv;
400
401 debug("%s\n", __func__);
402
Ramon Friedcf6ba6f2018-05-14 15:02:30 +0300403 if (host->ops && host->ops->platform_execute_tuning) {
Siva Durga Prasad Paladugu1f67b492018-04-19 12:37:07 +0530404 err = host->ops->platform_execute_tuning(mmc, opcode);
405 if (err)
406 return err;
407 return 0;
408 }
409 return 0;
410}
411#endif
Lei Wen142c8f92011-06-28 21:50:06 +0000412static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
413{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200414 struct sdhci_host *host = mmc->priv;
Stefan Roesee9161032016-12-12 08:34:42 +0100415 unsigned int div, clk = 0, timeout;
Wenyou Yang09456d92015-09-22 14:59:25 +0800416
417 /* Wait max 20 ms */
418 timeout = 200;
419 while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
420 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
421 if (timeout == 0) {
422 printf("%s: Timeout to wait cmd & data inhibit\n",
423 __func__);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900424 return -EBUSY;
Wenyou Yang09456d92015-09-22 14:59:25 +0800425 }
426
427 timeout--;
428 udelay(100);
429 }
Lei Wen142c8f92011-06-28 21:50:06 +0000430
Stefan Roesee9161032016-12-12 08:34:42 +0100431 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Lei Wen142c8f92011-06-28 21:50:06 +0000432
433 if (clock == 0)
434 return 0;
435
Ramon Friedcf6ba6f2018-05-14 15:02:30 +0300436 if (host->ops && host->ops->set_delay)
Siva Durga Prasad Paladugu1f67b492018-04-19 12:37:07 +0530437 host->ops->set_delay(host);
438
Jaehoon Chung46e627c2013-07-19 17:44:49 +0900439 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
Wenyou Yang3d734042016-09-18 09:01:22 +0800440 /*
441 * Check if the Host Controller supports Programmable Clock
442 * Mode.
443 */
444 if (host->clk_mul) {
445 for (div = 1; div <= 1024; div++) {
Wenyou Yangab877fe2017-04-26 09:32:30 +0800446 if ((host->max_clk / div) <= clock)
Lei Wen142c8f92011-06-28 21:50:06 +0000447 break;
448 }
Wenyou Yang3d734042016-09-18 09:01:22 +0800449
450 /*
451 * Set Programmable Clock Mode in the Clock
452 * Control register.
453 */
454 clk = SDHCI_PROG_CLOCK_MODE;
455 div--;
456 } else {
457 /* Version 3.00 divisors must be a multiple of 2. */
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100458 if (host->max_clk <= clock) {
Wenyou Yang3d734042016-09-18 09:01:22 +0800459 div = 1;
460 } else {
461 for (div = 2;
462 div < SDHCI_MAX_DIV_SPEC_300;
463 div += 2) {
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100464 if ((host->max_clk / div) <= clock)
Wenyou Yang3d734042016-09-18 09:01:22 +0800465 break;
466 }
467 }
468 div >>= 1;
Lei Wen142c8f92011-06-28 21:50:06 +0000469 }
470 } else {
471 /* Version 2.00 divisors must be a power of 2. */
472 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100473 if ((host->max_clk / div) <= clock)
Lei Wen142c8f92011-06-28 21:50:06 +0000474 break;
475 }
Wenyou Yang3d734042016-09-18 09:01:22 +0800476 div >>= 1;
Lei Wen142c8f92011-06-28 21:50:06 +0000477 }
Lei Wen142c8f92011-06-28 21:50:06 +0000478
Masahiro Yamadaeeb91ad2017-01-13 11:51:51 +0900479 if (host->ops && host->ops->set_clock)
Jaehoon Chung46d3c032016-12-30 15:30:18 +0900480 host->ops->set_clock(host, div);
Jaehoon Chungb1929ea2012-08-30 16:24:11 +0000481
Wenyou Yang3d734042016-09-18 09:01:22 +0800482 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Lei Wen142c8f92011-06-28 21:50:06 +0000483 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
484 << SDHCI_DIVIDER_HI_SHIFT;
485 clk |= SDHCI_CLOCK_INT_EN;
486 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
487
488 /* Wait max 20 ms */
489 timeout = 20;
490 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
491 & SDHCI_CLOCK_INT_STABLE)) {
492 if (timeout == 0) {
Darwin Rambo43558132013-12-19 15:13:25 -0800493 printf("%s: Internal clock never stabilised.\n",
494 __func__);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900495 return -EBUSY;
Lei Wen142c8f92011-06-28 21:50:06 +0000496 }
497 timeout--;
498 udelay(1000);
499 }
500
501 clk |= SDHCI_CLOCK_CARD_EN;
502 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
503 return 0;
504}
505
506static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
507{
508 u8 pwr = 0;
509
510 if (power != (unsigned short)-1) {
511 switch (1 << power) {
512 case MMC_VDD_165_195:
513 pwr = SDHCI_POWER_180;
514 break;
515 case MMC_VDD_29_30:
516 case MMC_VDD_30_31:
517 pwr = SDHCI_POWER_300;
518 break;
519 case MMC_VDD_32_33:
520 case MMC_VDD_33_34:
521 pwr = SDHCI_POWER_330;
522 break;
523 }
524 }
525
526 if (pwr == 0) {
527 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
528 return;
529 }
530
531 pwr |= SDHCI_POWER_ON;
532
533 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
534}
535
Simon Glasseba48f92017-07-29 11:35:31 -0600536#ifdef CONFIG_DM_MMC
Simon Glassb97f0fa2016-06-12 23:30:28 -0600537static int sdhci_set_ios(struct udevice *dev)
538{
539 struct mmc *mmc = mmc_get_mmc_dev(dev);
540#else
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900541static int sdhci_set_ios(struct mmc *mmc)
Lei Wen142c8f92011-06-28 21:50:06 +0000542{
Simon Glassb97f0fa2016-06-12 23:30:28 -0600543#endif
Lei Wen142c8f92011-06-28 21:50:06 +0000544 u32 ctrl;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200545 struct sdhci_host *host = mmc->priv;
Lei Wen142c8f92011-06-28 21:50:06 +0000546
Masahiro Yamadaeeb91ad2017-01-13 11:51:51 +0900547 if (host->ops && host->ops->set_control_reg)
Jaehoon Chung46d3c032016-12-30 15:30:18 +0900548 host->ops->set_control_reg(host);
Jaehoon Chung53889ed2012-04-23 02:36:26 +0000549
Lei Wen142c8f92011-06-28 21:50:06 +0000550 if (mmc->clock != host->clock)
551 sdhci_set_clock(mmc, mmc->clock);
552
Siva Durga Prasad Paladugu9fccd8a2018-04-19 12:37:04 +0530553 if (mmc->clk_disable)
554 sdhci_set_clock(mmc, 0);
555
Lei Wen142c8f92011-06-28 21:50:06 +0000556 /* Set bus width */
557 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
558 if (mmc->bus_width == 8) {
559 ctrl &= ~SDHCI_CTRL_4BITBUS;
Jaehoon Chung46e627c2013-07-19 17:44:49 +0900560 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
561 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
Lei Wen142c8f92011-06-28 21:50:06 +0000562 ctrl |= SDHCI_CTRL_8BITBUS;
563 } else {
Matt Reimer9651f592015-02-19 11:22:53 -0700564 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
565 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
Lei Wen142c8f92011-06-28 21:50:06 +0000566 ctrl &= ~SDHCI_CTRL_8BITBUS;
567 if (mmc->bus_width == 4)
568 ctrl |= SDHCI_CTRL_4BITBUS;
569 else
570 ctrl &= ~SDHCI_CTRL_4BITBUS;
571 }
572
573 if (mmc->clock > 26000000)
574 ctrl |= SDHCI_CTRL_HISPD;
575 else
576 ctrl &= ~SDHCI_CTRL_HISPD;
577
Hannes Schmelzer576a0182018-03-07 08:00:56 +0100578 if ((host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) ||
579 (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE))
Jaehoon Chung53889ed2012-04-23 02:36:26 +0000580 ctrl &= ~SDHCI_CTRL_HISPD;
581
Lei Wen142c8f92011-06-28 21:50:06 +0000582 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900583
Stefan Roesea3554ef2016-12-12 08:24:56 +0100584 /* If available, call the driver specific "post" set_ios() function */
585 if (host->ops && host->ops->set_ios_post)
586 host->ops->set_ios_post(host);
587
Simon Glassb97f0fa2016-06-12 23:30:28 -0600588 return 0;
Lei Wen142c8f92011-06-28 21:50:06 +0000589}
590
Jeroen Hofsteeee54c7b2014-10-08 22:57:43 +0200591static int sdhci_init(struct mmc *mmc)
Lei Wen142c8f92011-06-28 21:50:06 +0000592{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200593 struct sdhci_host *host = mmc->priv;
T Karthik Reddy3863f7e2019-06-25 13:39:03 +0200594#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_GPIO)
595 struct udevice *dev = mmc->dev;
596
597 gpio_request_by_name(dev, "cd-gpio", 0,
598 &host->cd_gpio, GPIOD_IS_IN);
599#endif
Lei Wen142c8f92011-06-28 21:50:06 +0000600
Masahiro Yamadaea04d902016-08-25 16:07:34 +0900601 sdhci_reset(host, SDHCI_RESET_ALL);
602
Lei Wen142c8f92011-06-28 21:50:06 +0000603 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
604 aligned_buffer = memalign(8, 512*1024);
605 if (!aligned_buffer) {
Darwin Rambo43558132013-12-19 15:13:25 -0800606 printf("%s: Aligned buffer alloc failed!!!\n",
607 __func__);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900608 return -ENOMEM;
Lei Wen142c8f92011-06-28 21:50:06 +0000609 }
610 }
611
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200612 sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
Joe Hershberger456f34a2012-08-17 10:18:55 +0000613
Masahiro Yamadaeeb91ad2017-01-13 11:51:51 +0900614 if (host->ops && host->ops->get_cd)
Jaehoon Chung730a5952016-12-30 15:30:15 +0900615 host->ops->get_cd(host);
Joe Hershberger456f34a2012-08-17 10:18:55 +0000616
Łukasz Majewskid56a52a2013-01-11 05:08:54 +0000617 /* Enable only interrupts served by the SD controller */
Darwin Rambo43558132013-12-19 15:13:25 -0800618 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
619 SDHCI_INT_ENABLE);
Łukasz Majewskid56a52a2013-01-11 05:08:54 +0000620 /* Mask all sdhci interrupt sources */
621 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
Lei Wen142c8f92011-06-28 21:50:06 +0000622
Lei Wen142c8f92011-06-28 21:50:06 +0000623 return 0;
624}
625
Simon Glasseba48f92017-07-29 11:35:31 -0600626#ifdef CONFIG_DM_MMC
Simon Glassb97f0fa2016-06-12 23:30:28 -0600627int sdhci_probe(struct udevice *dev)
628{
629 struct mmc *mmc = mmc_get_mmc_dev(dev);
630
631 return sdhci_init(mmc);
632}
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200633
T Karthik Reddyc8a0ec02019-06-25 13:39:04 +0200634int sdhci_get_cd(struct udevice *dev)
635{
636 struct mmc *mmc = mmc_get_mmc_dev(dev);
637 struct sdhci_host *host = mmc->priv;
638 int value;
639
640 /* If nonremovable, assume that the card is always present. */
641 if (mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE)
642 return 1;
643 /* If polling, assume that the card is always present. */
644 if (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL)
645 return 1;
646
647#if CONFIG_IS_ENABLED(DM_GPIO)
648 value = dm_gpio_get_value(&host->cd_gpio);
649 if (value >= 0) {
650 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
651 return !value;
652 else
653 return value;
654 }
655#endif
656 value = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
657 SDHCI_CARD_PRESENT);
658 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
659 return !value;
660 else
661 return value;
662}
663
Simon Glassb97f0fa2016-06-12 23:30:28 -0600664const struct dm_mmc_ops sdhci_ops = {
665 .send_cmd = sdhci_send_command,
666 .set_ios = sdhci_set_ios,
T Karthik Reddyc8a0ec02019-06-25 13:39:04 +0200667 .get_cd = sdhci_get_cd,
Siva Durga Prasad Paladugu1f67b492018-04-19 12:37:07 +0530668#ifdef MMC_SUPPORTS_TUNING
669 .execute_tuning = sdhci_execute_tuning,
670#endif
Simon Glassb97f0fa2016-06-12 23:30:28 -0600671};
672#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200673static const struct mmc_ops sdhci_ops = {
674 .send_cmd = sdhci_send_command,
675 .set_ios = sdhci_set_ios,
676 .init = sdhci_init,
677};
Simon Glassb97f0fa2016-06-12 23:30:28 -0600678#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200679
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900680int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100681 u32 f_max, u32 f_min)
Lei Wen142c8f92011-06-28 21:50:06 +0000682{
Siva Durga Prasad Paladuguc0290b42018-04-19 12:37:08 +0530683 u32 caps, caps_1 = 0;
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900684
685 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Masahiro Yamada27bfb712016-08-25 16:07:37 +0900686
Masahiro Yamada124f6ce2016-12-07 22:10:29 +0900687#ifdef CONFIG_MMC_SDHCI_SDMA
Masahiro Yamada27bfb712016-08-25 16:07:37 +0900688 if (!(caps & SDHCI_CAN_DO_SDMA)) {
689 printf("%s: Your controller doesn't support SDMA!!\n",
690 __func__);
691 return -EINVAL;
692 }
Faiz Abbas87102502019-04-16 23:06:57 +0530693
694 host->flags |= USE_SDMA;
Masahiro Yamada27bfb712016-08-25 16:07:37 +0900695#endif
Faiz Abbas4c082a62019-04-16 23:06:58 +0530696#if CONFIG_IS_ENABLED(MMC_SDHCI_ADMA)
697 if (!(caps & SDHCI_CAN_DO_ADMA2)) {
698 printf("%s: Your controller doesn't support SDMA!!\n",
699 __func__);
700 return -EINVAL;
701 }
702 host->adma_desc_table = (struct sdhci_adma_desc *)
703 memalign(ARCH_DMA_MINALIGN, ADMA_TABLE_SZ);
704
705 host->adma_addr = (dma_addr_t)host->adma_desc_table;
706#ifdef CONFIG_DMA_ADDR_T_64BIT
707 host->flags |= USE_ADMA64;
708#else
709 host->flags |= USE_ADMA;
710#endif
711#endif
Jaehoon Chung6c5b3592016-09-26 08:10:01 +0900712 if (host->quirks & SDHCI_QUIRK_REG32_RW)
713 host->version =
714 sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
715 else
716 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900717
718 cfg->name = host->name;
Simon Glasseba48f92017-07-29 11:35:31 -0600719#ifndef CONFIG_DM_MMC
Simon Glassb0842072016-06-12 23:30:27 -0600720 cfg->ops = &sdhci_ops;
Lei Wen142c8f92011-06-28 21:50:06 +0000721#endif
Wenyou Yangab877fe2017-04-26 09:32:30 +0800722
723 /* Check whether the clock multiplier is supported or not */
724 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
725 caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
726 host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
727 SDHCI_CLOCK_MUL_SHIFT;
728 }
729
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100730 if (host->max_clk == 0) {
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900731 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100732 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
Simon Glassb0842072016-06-12 23:30:27 -0600733 SDHCI_CLOCK_BASE_SHIFT;
Lei Wen142c8f92011-06-28 21:50:06 +0000734 else
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100735 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
Simon Glassb0842072016-06-12 23:30:27 -0600736 SDHCI_CLOCK_BASE_SHIFT;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100737 host->max_clk *= 1000000;
Wenyou Yangab877fe2017-04-26 09:32:30 +0800738 if (host->clk_mul)
739 host->max_clk *= host->clk_mul;
Lei Wen142c8f92011-06-28 21:50:06 +0000740 }
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100741 if (host->max_clk == 0) {
Masahiro Yamadada957dd2016-08-25 16:07:35 +0900742 printf("%s: Hardware doesn't specify base clock frequency\n",
743 __func__);
Simon Glassb0842072016-06-12 23:30:27 -0600744 return -EINVAL;
Masahiro Yamadada957dd2016-08-25 16:07:35 +0900745 }
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100746 if (f_max && (f_max < host->max_clk))
747 cfg->f_max = f_max;
748 else
749 cfg->f_max = host->max_clk;
750 if (f_min)
751 cfg->f_min = f_min;
Lei Wen142c8f92011-06-28 21:50:06 +0000752 else {
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900753 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
Simon Glassb0842072016-06-12 23:30:27 -0600754 cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
Lei Wen142c8f92011-06-28 21:50:06 +0000755 else
Simon Glassb0842072016-06-12 23:30:27 -0600756 cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
Lei Wen142c8f92011-06-28 21:50:06 +0000757 }
Simon Glassb0842072016-06-12 23:30:27 -0600758 cfg->voltages = 0;
Lei Wen142c8f92011-06-28 21:50:06 +0000759 if (caps & SDHCI_CAN_VDD_330)
Simon Glassb0842072016-06-12 23:30:27 -0600760 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Lei Wen142c8f92011-06-28 21:50:06 +0000761 if (caps & SDHCI_CAN_VDD_300)
Simon Glassb0842072016-06-12 23:30:27 -0600762 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
Lei Wen142c8f92011-06-28 21:50:06 +0000763 if (caps & SDHCI_CAN_VDD_180)
Simon Glassb0842072016-06-12 23:30:27 -0600764 cfg->voltages |= MMC_VDD_165_195;
Jaehoon Chung53889ed2012-04-23 02:36:26 +0000765
Masahiro Yamada4b338772016-08-25 16:07:36 +0900766 if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
767 cfg->voltages |= host->voltages;
768
Masahiro Yamadaea5b7c02017-12-30 02:00:08 +0900769 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
Jaehoon Chungbc00a542016-12-30 15:30:21 +0900770
771 /* Since Host Controller Version3.0 */
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900772 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
Jaehoon Chung665152e2016-12-30 15:30:11 +0900773 if (!(caps & SDHCI_CAN_DO_8BIT))
774 cfg->host_caps &= ~MMC_MODE_8BIT;
Jagannadha Sutradharudu Teki08706be2013-05-21 15:01:36 +0530775 }
Siva Durga Prasad Paladugub0fbb492016-01-12 15:12:15 +0530776
Hannes Schmelzer576a0182018-03-07 08:00:56 +0100777 if (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE) {
778 cfg->host_caps &= ~MMC_MODE_HS;
779 cfg->host_caps &= ~MMC_MODE_HS_52MHz;
780 }
781
Siva Durga Prasad Paladuguc0290b42018-04-19 12:37:08 +0530782 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
783 caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
784
785 if (!(cfg->voltages & MMC_VDD_165_195) ||
786 (host->quirks & SDHCI_QUIRK_NO_1_8_V))
787 caps_1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
788 SDHCI_SUPPORT_DDR50);
789
790 if (caps_1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
791 SDHCI_SUPPORT_DDR50))
792 cfg->host_caps |= MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25);
793
794 if (caps_1 & SDHCI_SUPPORT_SDR104) {
795 cfg->host_caps |= MMC_CAP(UHS_SDR104) | MMC_CAP(UHS_SDR50);
796 /*
797 * SD3.0: SDR104 is supported so (for eMMC) the caps2
798 * field can be promoted to support HS200.
799 */
800 cfg->host_caps |= MMC_CAP(MMC_HS_200);
801 } else if (caps_1 & SDHCI_SUPPORT_SDR50) {
802 cfg->host_caps |= MMC_CAP(UHS_SDR50);
803 }
804
805 if (caps_1 & SDHCI_SUPPORT_DDR50)
806 cfg->host_caps |= MMC_CAP(UHS_DDR50);
807
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900808 if (host->host_caps)
809 cfg->host_caps |= host->host_caps;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200810
Simon Glassb0842072016-06-12 23:30:27 -0600811 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
812
813 return 0;
814}
815
Simon Glassb97f0fa2016-06-12 23:30:28 -0600816#ifdef CONFIG_BLK
817int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
818{
819 return mmc_bind(dev, mmc, cfg);
820}
821#else
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100822int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
Simon Glassb0842072016-06-12 23:30:27 -0600823{
Masahiro Yamadada957dd2016-08-25 16:07:35 +0900824 int ret;
825
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100826 ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
Masahiro Yamadada957dd2016-08-25 16:07:35 +0900827 if (ret)
828 return ret;
Simon Glassb0842072016-06-12 23:30:27 -0600829
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200830 host->mmc = mmc_create(&host->cfg, host);
831 if (host->mmc == NULL) {
832 printf("%s: mmc create fail!\n", __func__);
Jaehoon Chungfc6c1c62016-09-26 08:10:02 +0900833 return -ENOMEM;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200834 }
Lei Wen142c8f92011-06-28 21:50:06 +0000835
836 return 0;
837}
Simon Glassb97f0fa2016-06-12 23:30:28 -0600838#endif