blob: 604f18dcc96f764d8ff05ec67ccf2d5fc43a349e [file] [log] [blame]
Lei Wenaf62a552011-06-28 21:50:06 +00001/*
2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lei Wenaf62a552011-06-28 21:50:06 +00006 *
7 * Back ported to the 8xx platform (from the 8260 platform) by
8 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9 */
10
11#include <common.h>
12#include <malloc.h>
13#include <mmc.h>
14#include <sdhci.h>
15
Stefan Roese492d3222015-06-29 14:58:09 +020016#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
17void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
18#else
Lei Wenaf62a552011-06-28 21:50:06 +000019void *aligned_buffer;
Stefan Roese492d3222015-06-29 14:58:09 +020020#endif
Lei Wenaf62a552011-06-28 21:50:06 +000021
22static void sdhci_reset(struct sdhci_host *host, u8 mask)
23{
24 unsigned long timeout;
25
26 /* Wait max 100 ms */
27 timeout = 100;
28 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
29 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
30 if (timeout == 0) {
Darwin Rambo30e6d972013-12-19 15:13:25 -080031 printf("%s: Reset 0x%x never completed.\n",
32 __func__, (int)mask);
Lei Wenaf62a552011-06-28 21:50:06 +000033 return;
34 }
35 timeout--;
36 udelay(1000);
37 }
38}
39
40static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
41{
42 int i;
43 if (cmd->resp_type & MMC_RSP_136) {
44 /* CRC is stripped so we need to do some shifting. */
45 for (i = 0; i < 4; i++) {
46 cmd->response[i] = sdhci_readl(host,
47 SDHCI_RESPONSE + (3-i)*4) << 8;
48 if (i != 3)
49 cmd->response[i] |= sdhci_readb(host,
50 SDHCI_RESPONSE + (3-i)*4-1);
51 }
52 } else {
53 cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
54 }
55}
56
57static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
58{
59 int i;
60 char *offs;
61 for (i = 0; i < data->blocksize; i += 4) {
62 offs = data->dest + i;
63 if (data->flags == MMC_DATA_READ)
64 *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
65 else
66 sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
67 }
68}
69
70static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
71 unsigned int start_addr)
72{
Lei Wena004abd2011-10-08 04:14:57 +000073 unsigned int stat, rdy, mask, timeout, block = 0;
Jaehoon Chung804c7f42012-09-20 20:31:55 +000074#ifdef CONFIG_MMC_SDMA
75 unsigned char ctrl;
Juhyun \(Justin\) Oh2c011842013-09-13 18:06:00 +000076 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Jaehoon Chung804c7f42012-09-20 20:31:55 +000077 ctrl &= ~SDHCI_CTRL_DMA_MASK;
Juhyun \(Justin\) Oh2c011842013-09-13 18:06:00 +000078 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Jaehoon Chung804c7f42012-09-20 20:31:55 +000079#endif
Lei Wenaf62a552011-06-28 21:50:06 +000080
Jaehoon Chung5d48e422012-09-20 20:31:54 +000081 timeout = 1000000;
Lei Wenaf62a552011-06-28 21:50:06 +000082 rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
83 mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
84 do {
85 stat = sdhci_readl(host, SDHCI_INT_STATUS);
86 if (stat & SDHCI_INT_ERROR) {
Darwin Rambo30e6d972013-12-19 15:13:25 -080087 printf("%s: Error detected in status(0x%X)!\n",
88 __func__, stat);
Lei Wenaf62a552011-06-28 21:50:06 +000089 return -1;
90 }
91 if (stat & rdy) {
92 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
93 continue;
94 sdhci_writel(host, rdy, SDHCI_INT_STATUS);
95 sdhci_transfer_pio(host, data);
96 data->dest += data->blocksize;
97 if (++block >= data->blocks)
98 break;
99 }
100#ifdef CONFIG_MMC_SDMA
101 if (stat & SDHCI_INT_DMA_END) {
102 sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
Lei Wen3e81c772011-10-08 04:14:58 +0000103 start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
Lei Wenaf62a552011-06-28 21:50:06 +0000104 start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
105 sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
106 }
107#endif
Lei Wena004abd2011-10-08 04:14:57 +0000108 if (timeout-- > 0)
109 udelay(10);
110 else {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800111 printf("%s: Transfer data timeout\n", __func__);
Lei Wena004abd2011-10-08 04:14:57 +0000112 return -1;
113 }
Lei Wenaf62a552011-06-28 21:50:06 +0000114 } while (!(stat & SDHCI_INT_DATA_END));
115 return 0;
116}
117
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200118/*
119 * No command will be sent by driver if card is busy, so driver must wait
120 * for card ready state.
121 * Every time when card is busy after timeout then (last) timeout value will be
122 * increased twice but only if it doesn't exceed global defined maximum.
123 * Each function call will use last timeout value. Max timeout can be redefined
124 * in board config file.
125 */
126#ifndef CONFIG_SDHCI_CMD_MAX_TIMEOUT
127#define CONFIG_SDHCI_CMD_MAX_TIMEOUT 3200
128#endif
129#define CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT 100
Steve Raed90bb432016-06-29 13:42:01 -0700130#define SDHCI_READ_STATUS_TIMEOUT 1000
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200131
Jeroen Hofstee6588c782014-10-08 22:57:43 +0200132static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
Lei Wenaf62a552011-06-28 21:50:06 +0000133 struct mmc_data *data)
134{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200135 struct sdhci_host *host = mmc->priv;
Lei Wenaf62a552011-06-28 21:50:06 +0000136 unsigned int stat = 0;
137 int ret = 0;
138 int trans_bytes = 0, is_aligned = 1;
139 u32 mask, flags, mode;
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200140 unsigned int time = 0, start_addr = 0;
Simon Glass19d2e342016-05-14 14:03:04 -0600141 int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
Stefan Roese29905a42015-06-29 14:58:08 +0200142 unsigned start = get_timer(0);
Lei Wenaf62a552011-06-28 21:50:06 +0000143
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200144 /* Timeout unit - ms */
145 static unsigned int cmd_timeout = CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT;
Lei Wenaf62a552011-06-28 21:50:06 +0000146
147 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
148 mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
149
150 /* We shouldn't wait for data inihibit for stop commands, even
151 though they might use busy signaling */
152 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
153 mask &= ~SDHCI_DATA_INHIBIT;
154
155 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200156 if (time >= cmd_timeout) {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800157 printf("%s: MMC: %d busy ", __func__, mmc_dev);
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200158 if (2 * cmd_timeout <= CONFIG_SDHCI_CMD_MAX_TIMEOUT) {
159 cmd_timeout += cmd_timeout;
160 printf("timeout increasing to: %u ms.\n",
161 cmd_timeout);
162 } else {
163 puts("timeout.\n");
164 return COMM_ERR;
165 }
Lei Wenaf62a552011-06-28 21:50:06 +0000166 }
Przemyslaw Marczak56b34bc2013-10-08 18:12:09 +0200167 time++;
Lei Wenaf62a552011-06-28 21:50:06 +0000168 udelay(1000);
169 }
170
171 mask = SDHCI_INT_RESPONSE;
172 if (!(cmd->resp_type & MMC_RSP_PRESENT))
173 flags = SDHCI_CMD_RESP_NONE;
174 else if (cmd->resp_type & MMC_RSP_136)
175 flags = SDHCI_CMD_RESP_LONG;
176 else if (cmd->resp_type & MMC_RSP_BUSY) {
177 flags = SDHCI_CMD_RESP_SHORT_BUSY;
178 mask |= SDHCI_INT_DATA_END;
179 } else
180 flags = SDHCI_CMD_RESP_SHORT;
181
182 if (cmd->resp_type & MMC_RSP_CRC)
183 flags |= SDHCI_CMD_CRC;
184 if (cmd->resp_type & MMC_RSP_OPCODE)
185 flags |= SDHCI_CMD_INDEX;
186 if (data)
187 flags |= SDHCI_CMD_DATA;
188
Darwin Rambo30e6d972013-12-19 15:13:25 -0800189 /* Set Transfer mode regarding to data flag */
Lei Wenaf62a552011-06-28 21:50:06 +0000190 if (data != 0) {
191 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
192 mode = SDHCI_TRNS_BLK_CNT_EN;
193 trans_bytes = data->blocks * data->blocksize;
194 if (data->blocks > 1)
195 mode |= SDHCI_TRNS_MULTI;
196
197 if (data->flags == MMC_DATA_READ)
198 mode |= SDHCI_TRNS_READ;
199
200#ifdef CONFIG_MMC_SDMA
201 if (data->flags == MMC_DATA_READ)
Rob Herring3c1fcb72015-03-17 15:46:38 -0500202 start_addr = (unsigned long)data->dest;
Lei Wenaf62a552011-06-28 21:50:06 +0000203 else
Rob Herring3c1fcb72015-03-17 15:46:38 -0500204 start_addr = (unsigned long)data->src;
Lei Wenaf62a552011-06-28 21:50:06 +0000205 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
206 (start_addr & 0x7) != 0x0) {
207 is_aligned = 0;
Rob Herring3c1fcb72015-03-17 15:46:38 -0500208 start_addr = (unsigned long)aligned_buffer;
Lei Wenaf62a552011-06-28 21:50:06 +0000209 if (data->flags != MMC_DATA_READ)
210 memcpy(aligned_buffer, data->src, trans_bytes);
211 }
212
Stefan Roese492d3222015-06-29 14:58:09 +0200213#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
214 /*
215 * Always use this bounce-buffer when
216 * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
217 */
218 is_aligned = 0;
219 start_addr = (unsigned long)aligned_buffer;
220 if (data->flags != MMC_DATA_READ)
221 memcpy(aligned_buffer, data->src, trans_bytes);
222#endif
223
Lei Wenaf62a552011-06-28 21:50:06 +0000224 sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
225 mode |= SDHCI_TRNS_DMA;
226#endif
227 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
228 data->blocksize),
229 SDHCI_BLOCK_SIZE);
230 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
231 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Kevin Liu5e1c23c2015-03-23 17:57:00 -0500232 } else if (cmd->resp_type & MMC_RSP_BUSY) {
233 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
Lei Wenaf62a552011-06-28 21:50:06 +0000234 }
235
236 sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
237#ifdef CONFIG_MMC_SDMA
Lei Wen2c2ec4c2011-10-08 04:14:54 +0000238 flush_cache(start_addr, trans_bytes);
Lei Wenaf62a552011-06-28 21:50:06 +0000239#endif
240 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
Stefan Roese29905a42015-06-29 14:58:08 +0200241 start = get_timer(0);
Lei Wenaf62a552011-06-28 21:50:06 +0000242 do {
243 stat = sdhci_readl(host, SDHCI_INT_STATUS);
244 if (stat & SDHCI_INT_ERROR)
245 break;
Stefan Roese29905a42015-06-29 14:58:08 +0200246 } while (((stat & mask) != mask) &&
Steve Raed90bb432016-06-29 13:42:01 -0700247 (get_timer(start) < SDHCI_READ_STATUS_TIMEOUT));
Lei Wenaf62a552011-06-28 21:50:06 +0000248
Steve Raed90bb432016-06-29 13:42:01 -0700249 if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
Jaehoon Chung3a638322012-04-23 02:36:25 +0000250 if (host->quirks & SDHCI_QUIRK_BROKEN_R1B)
251 return 0;
252 else {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800253 printf("%s: Timeout for status update!\n", __func__);
Jaehoon Chung3a638322012-04-23 02:36:25 +0000254 return TIMEOUT;
255 }
256 }
257
Lei Wenaf62a552011-06-28 21:50:06 +0000258 if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
259 sdhci_cmd_done(host, cmd);
260 sdhci_writel(host, mask, SDHCI_INT_STATUS);
261 } else
262 ret = -1;
263
264 if (!ret && data)
265 ret = sdhci_transfer_data(host, data, start_addr);
266
Tushar Behera13243f22012-09-20 20:31:57 +0000267 if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
268 udelay(1000);
269
Lei Wenaf62a552011-06-28 21:50:06 +0000270 stat = sdhci_readl(host, SDHCI_INT_STATUS);
271 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
272 if (!ret) {
273 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
274 !is_aligned && (data->flags == MMC_DATA_READ))
275 memcpy(data->dest, aligned_buffer, trans_bytes);
276 return 0;
277 }
278
279 sdhci_reset(host, SDHCI_RESET_CMD);
280 sdhci_reset(host, SDHCI_RESET_DATA);
281 if (stat & SDHCI_INT_TIMEOUT)
282 return TIMEOUT;
283 else
284 return COMM_ERR;
285}
286
287static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
288{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200289 struct sdhci_host *host = mmc->priv;
Wenyou Yang79667b72015-09-22 14:59:25 +0800290 unsigned int div, clk, timeout, reg;
Lei Wenaf62a552011-06-28 21:50:06 +0000291
Wenyou Yang79667b72015-09-22 14:59:25 +0800292 /* Wait max 20 ms */
293 timeout = 200;
294 while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
295 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
296 if (timeout == 0) {
297 printf("%s: Timeout to wait cmd & data inhibit\n",
298 __func__);
299 return -1;
300 }
301
302 timeout--;
303 udelay(100);
304 }
305
306 reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
307 reg &= ~SDHCI_CLOCK_CARD_EN;
308 sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
Lei Wenaf62a552011-06-28 21:50:06 +0000309
310 if (clock == 0)
311 return 0;
312
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900313 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
Lei Wenaf62a552011-06-28 21:50:06 +0000314 /* Version 3.00 divisors must be a multiple of 2. */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200315 if (mmc->cfg->f_max <= clock)
Lei Wenaf62a552011-06-28 21:50:06 +0000316 div = 1;
317 else {
318 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200319 if ((mmc->cfg->f_max / div) <= clock)
Lei Wenaf62a552011-06-28 21:50:06 +0000320 break;
321 }
322 }
323 } else {
324 /* Version 2.00 divisors must be a power of 2. */
325 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200326 if ((mmc->cfg->f_max / div) <= clock)
Lei Wenaf62a552011-06-28 21:50:06 +0000327 break;
328 }
329 }
330 div >>= 1;
331
Jaehoon Chungb09ed6e2012-08-30 16:24:11 +0000332 if (host->set_clock)
333 host->set_clock(host->index, div);
334
Lei Wenaf62a552011-06-28 21:50:06 +0000335 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
336 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
337 << SDHCI_DIVIDER_HI_SHIFT;
338 clk |= SDHCI_CLOCK_INT_EN;
339 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
340
341 /* Wait max 20 ms */
342 timeout = 20;
343 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
344 & SDHCI_CLOCK_INT_STABLE)) {
345 if (timeout == 0) {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800346 printf("%s: Internal clock never stabilised.\n",
347 __func__);
Lei Wenaf62a552011-06-28 21:50:06 +0000348 return -1;
349 }
350 timeout--;
351 udelay(1000);
352 }
353
354 clk |= SDHCI_CLOCK_CARD_EN;
355 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
356 return 0;
357}
358
359static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
360{
361 u8 pwr = 0;
362
363 if (power != (unsigned short)-1) {
364 switch (1 << power) {
365 case MMC_VDD_165_195:
366 pwr = SDHCI_POWER_180;
367 break;
368 case MMC_VDD_29_30:
369 case MMC_VDD_30_31:
370 pwr = SDHCI_POWER_300;
371 break;
372 case MMC_VDD_32_33:
373 case MMC_VDD_33_34:
374 pwr = SDHCI_POWER_330;
375 break;
376 }
377 }
378
379 if (pwr == 0) {
380 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
381 return;
382 }
383
Mela Custodio688c2d12012-11-03 17:40:16 +0000384 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
385 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
386
Lei Wenaf62a552011-06-28 21:50:06 +0000387 pwr |= SDHCI_POWER_ON;
388
389 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
390}
391
Jeroen Hofstee6588c782014-10-08 22:57:43 +0200392static void sdhci_set_ios(struct mmc *mmc)
Lei Wenaf62a552011-06-28 21:50:06 +0000393{
394 u32 ctrl;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200395 struct sdhci_host *host = mmc->priv;
Lei Wenaf62a552011-06-28 21:50:06 +0000396
Jaehoon Chung236bfec2012-04-23 02:36:26 +0000397 if (host->set_control_reg)
398 host->set_control_reg(host);
399
Lei Wenaf62a552011-06-28 21:50:06 +0000400 if (mmc->clock != host->clock)
401 sdhci_set_clock(mmc, mmc->clock);
402
403 /* Set bus width */
404 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
405 if (mmc->bus_width == 8) {
406 ctrl &= ~SDHCI_CTRL_4BITBUS;
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900407 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
408 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
Lei Wenaf62a552011-06-28 21:50:06 +0000409 ctrl |= SDHCI_CTRL_8BITBUS;
410 } else {
Matt Reimerf88a4292015-02-19 11:22:53 -0700411 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
412 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
Lei Wenaf62a552011-06-28 21:50:06 +0000413 ctrl &= ~SDHCI_CTRL_8BITBUS;
414 if (mmc->bus_width == 4)
415 ctrl |= SDHCI_CTRL_4BITBUS;
416 else
417 ctrl &= ~SDHCI_CTRL_4BITBUS;
418 }
419
420 if (mmc->clock > 26000000)
421 ctrl |= SDHCI_CTRL_HISPD;
422 else
423 ctrl &= ~SDHCI_CTRL_HISPD;
424
Jaehoon Chung236bfec2012-04-23 02:36:26 +0000425 if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
426 ctrl &= ~SDHCI_CTRL_HISPD;
427
Lei Wenaf62a552011-06-28 21:50:06 +0000428 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
429}
430
Jeroen Hofstee6588c782014-10-08 22:57:43 +0200431static int sdhci_init(struct mmc *mmc)
Lei Wenaf62a552011-06-28 21:50:06 +0000432{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200433 struct sdhci_host *host = mmc->priv;
Lei Wenaf62a552011-06-28 21:50:06 +0000434
435 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
436 aligned_buffer = memalign(8, 512*1024);
437 if (!aligned_buffer) {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800438 printf("%s: Aligned buffer alloc failed!!!\n",
439 __func__);
Lei Wenaf62a552011-06-28 21:50:06 +0000440 return -1;
441 }
442 }
443
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200444 sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
Joe Hershberger470dcc72012-08-17 10:18:55 +0000445
446 if (host->quirks & SDHCI_QUIRK_NO_CD) {
Andrei Pistirica102142c2016-01-28 15:30:18 +0530447#if defined(CONFIG_PIC32_SDHCI)
448 /* PIC32 SDHCI CD errata:
449 * - set CD_TEST and clear CD_TEST_INS bit
450 */
451 sdhci_writeb(host, SDHCI_CTRL_CD_TEST, SDHCI_HOST_CONTROL);
452#else
Joe Hershberger470dcc72012-08-17 10:18:55 +0000453 unsigned int status;
454
Matt Reimere113fe32015-02-23 14:56:58 -0700455 sdhci_writeb(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST,
Joe Hershberger470dcc72012-08-17 10:18:55 +0000456 SDHCI_HOST_CONTROL);
457
458 status = sdhci_readl(host, SDHCI_PRESENT_STATE);
459 while ((!(status & SDHCI_CARD_PRESENT)) ||
460 (!(status & SDHCI_CARD_STATE_STABLE)) ||
461 (!(status & SDHCI_CARD_DETECT_PIN_LEVEL)))
462 status = sdhci_readl(host, SDHCI_PRESENT_STATE);
Andrei Pistirica102142c2016-01-28 15:30:18 +0530463#endif
Joe Hershberger470dcc72012-08-17 10:18:55 +0000464 }
465
Łukasz Majewskice0c1bc2013-01-11 05:08:54 +0000466 /* Enable only interrupts served by the SD controller */
Darwin Rambo30e6d972013-12-19 15:13:25 -0800467 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
468 SDHCI_INT_ENABLE);
Łukasz Majewskice0c1bc2013-01-11 05:08:54 +0000469 /* Mask all sdhci interrupt sources */
470 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
Lei Wenaf62a552011-06-28 21:50:06 +0000471
Lei Wenaf62a552011-06-28 21:50:06 +0000472 return 0;
473}
474
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200475
476static const struct mmc_ops sdhci_ops = {
477 .send_cmd = sdhci_send_command,
478 .set_ios = sdhci_set_ios,
479 .init = sdhci_init,
480};
481
Lei Wenaf62a552011-06-28 21:50:06 +0000482int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
483{
Lei Wenaf62a552011-06-28 21:50:06 +0000484 unsigned int caps;
485
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200486 host->cfg.name = host->name;
487 host->cfg.ops = &sdhci_ops;
Lei Wenaf62a552011-06-28 21:50:06 +0000488
489 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
490#ifdef CONFIG_MMC_SDMA
491 if (!(caps & SDHCI_CAN_DO_SDMA)) {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800492 printf("%s: Your controller doesn't support SDMA!!\n",
493 __func__);
Lei Wenaf62a552011-06-28 21:50:06 +0000494 return -1;
495 }
496#endif
497
498 if (max_clk)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200499 host->cfg.f_max = max_clk;
Lei Wenaf62a552011-06-28 21:50:06 +0000500 else {
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900501 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200502 host->cfg.f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
Lei Wenaf62a552011-06-28 21:50:06 +0000503 >> SDHCI_CLOCK_BASE_SHIFT;
504 else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200505 host->cfg.f_max = (caps & SDHCI_CLOCK_BASE_MASK)
Lei Wenaf62a552011-06-28 21:50:06 +0000506 >> SDHCI_CLOCK_BASE_SHIFT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200507 host->cfg.f_max *= 1000000;
Lei Wenaf62a552011-06-28 21:50:06 +0000508 }
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200509 if (host->cfg.f_max == 0) {
Darwin Rambo30e6d972013-12-19 15:13:25 -0800510 printf("%s: Hardware doesn't specify base clock frequency\n",
511 __func__);
Lei Wenaf62a552011-06-28 21:50:06 +0000512 return -1;
513 }
514 if (min_clk)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200515 host->cfg.f_min = min_clk;
Lei Wenaf62a552011-06-28 21:50:06 +0000516 else {
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900517 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200518 host->cfg.f_min = host->cfg.f_max /
519 SDHCI_MAX_DIV_SPEC_300;
Lei Wenaf62a552011-06-28 21:50:06 +0000520 else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200521 host->cfg.f_min = host->cfg.f_max /
522 SDHCI_MAX_DIV_SPEC_200;
Lei Wenaf62a552011-06-28 21:50:06 +0000523 }
524
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200525 host->cfg.voltages = 0;
Lei Wenaf62a552011-06-28 21:50:06 +0000526 if (caps & SDHCI_CAN_VDD_330)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200527 host->cfg.voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Lei Wenaf62a552011-06-28 21:50:06 +0000528 if (caps & SDHCI_CAN_VDD_300)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200529 host->cfg.voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
Lei Wenaf62a552011-06-28 21:50:06 +0000530 if (caps & SDHCI_CAN_VDD_180)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200531 host->cfg.voltages |= MMC_VDD_165_195;
Jaehoon Chung236bfec2012-04-23 02:36:26 +0000532
533 if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200534 host->cfg.voltages |= host->voltages;
Jaehoon Chung236bfec2012-04-23 02:36:26 +0000535
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200536 host->cfg.host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
Jaehoon Chung113e5df2013-07-19 17:44:49 +0900537 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
Jagannadha Sutradharudu Teki1695b292013-05-21 15:01:36 +0530538 if (caps & SDHCI_CAN_DO_8BIT)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200539 host->cfg.host_caps |= MMC_MODE_8BIT;
Jagannadha Sutradharudu Teki1695b292013-05-21 15:01:36 +0530540 }
Siva Durga Prasad Paladugu42979002016-01-12 15:12:15 +0530541
542 if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
543 host->cfg.host_caps &= ~(MMC_MODE_HS | MMC_MODE_HS_52MHz);
544
Jaehoon Chung236bfec2012-04-23 02:36:26 +0000545 if (host->host_caps)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200546 host->cfg.host_caps |= host->host_caps;
547
548 host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Lei Wenaf62a552011-06-28 21:50:06 +0000549
550 sdhci_reset(host, SDHCI_RESET_ALL);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200551
552 host->mmc = mmc_create(&host->cfg, host);
553 if (host->mmc == NULL) {
554 printf("%s: mmc create fail!\n", __func__);
555 return -1;
556 }
Lei Wenaf62a552011-06-28 21:50:06 +0000557
558 return 0;
559}