1af62a557SLei Wen /* 2af62a557SLei Wen * Copyright 2011, Marvell Semiconductor Inc. 3af62a557SLei Wen * Lei Wen <leiwen@marvell.com> 4af62a557SLei Wen * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 6af62a557SLei Wen * 7af62a557SLei Wen * Back ported to the 8xx platform (from the 8260 platform) by 8af62a557SLei Wen * Murray.Jensen@cmst.csiro.au, 27-Jan-01. 9af62a557SLei Wen */ 10af62a557SLei Wen 11af62a557SLei Wen #include <common.h> 12af62a557SLei Wen #include <malloc.h> 13af62a557SLei Wen #include <mmc.h> 14af62a557SLei Wen #include <sdhci.h> 15af62a557SLei Wen 16af62a557SLei Wen void *aligned_buffer; 17af62a557SLei Wen 18af62a557SLei Wen static void sdhci_reset(struct sdhci_host *host, u8 mask) 19af62a557SLei Wen { 20af62a557SLei Wen unsigned long timeout; 21af62a557SLei Wen 22af62a557SLei Wen /* Wait max 100 ms */ 23af62a557SLei Wen timeout = 100; 24af62a557SLei Wen sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET); 25af62a557SLei Wen while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) { 26af62a557SLei Wen if (timeout == 0) { 27af62a557SLei Wen printf("Reset 0x%x never completed.\n", (int)mask); 28af62a557SLei Wen return; 29af62a557SLei Wen } 30af62a557SLei Wen timeout--; 31af62a557SLei Wen udelay(1000); 32af62a557SLei Wen } 33af62a557SLei Wen } 34af62a557SLei Wen 35af62a557SLei Wen static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd) 36af62a557SLei Wen { 37af62a557SLei Wen int i; 38af62a557SLei Wen if (cmd->resp_type & MMC_RSP_136) { 39af62a557SLei Wen /* CRC is stripped so we need to do some shifting. */ 40af62a557SLei Wen for (i = 0; i < 4; i++) { 41af62a557SLei Wen cmd->response[i] = sdhci_readl(host, 42af62a557SLei Wen SDHCI_RESPONSE + (3-i)*4) << 8; 43af62a557SLei Wen if (i != 3) 44af62a557SLei Wen cmd->response[i] |= sdhci_readb(host, 45af62a557SLei Wen SDHCI_RESPONSE + (3-i)*4-1); 46af62a557SLei Wen } 47af62a557SLei Wen } else { 48af62a557SLei Wen cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE); 49af62a557SLei Wen } 50af62a557SLei Wen } 51af62a557SLei Wen 52af62a557SLei Wen static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data) 53af62a557SLei Wen { 54af62a557SLei Wen int i; 55af62a557SLei Wen char *offs; 56af62a557SLei Wen for (i = 0; i < data->blocksize; i += 4) { 57af62a557SLei Wen offs = data->dest + i; 58af62a557SLei Wen if (data->flags == MMC_DATA_READ) 59af62a557SLei Wen *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER); 60af62a557SLei Wen else 61af62a557SLei Wen sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER); 62af62a557SLei Wen } 63af62a557SLei Wen } 64af62a557SLei Wen 65af62a557SLei Wen static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data, 66af62a557SLei Wen unsigned int start_addr) 67af62a557SLei Wen { 68a004abdeSLei Wen unsigned int stat, rdy, mask, timeout, block = 0; 69804c7f42SJaehoon Chung #ifdef CONFIG_MMC_SDMA 70804c7f42SJaehoon Chung unsigned char ctrl; 712c011847SJuhyun \(Justin\) Oh ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 72804c7f42SJaehoon Chung ctrl &= ~SDHCI_CTRL_DMA_MASK; 732c011847SJuhyun \(Justin\) Oh sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 74804c7f42SJaehoon Chung #endif 75af62a557SLei Wen 765d48e422SJaehoon Chung timeout = 1000000; 77af62a557SLei Wen rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL; 78af62a557SLei Wen mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE; 79af62a557SLei Wen do { 80af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 81af62a557SLei Wen if (stat & SDHCI_INT_ERROR) { 82af62a557SLei Wen printf("Error detected in status(0x%X)!\n", stat); 83af62a557SLei Wen return -1; 84af62a557SLei Wen } 85af62a557SLei Wen if (stat & rdy) { 86af62a557SLei Wen if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask)) 87af62a557SLei Wen continue; 88af62a557SLei Wen sdhci_writel(host, rdy, SDHCI_INT_STATUS); 89af62a557SLei Wen sdhci_transfer_pio(host, data); 90af62a557SLei Wen data->dest += data->blocksize; 91af62a557SLei Wen if (++block >= data->blocks) 92af62a557SLei Wen break; 93af62a557SLei Wen } 94af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 95af62a557SLei Wen if (stat & SDHCI_INT_DMA_END) { 96af62a557SLei Wen sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS); 973e81c772SLei Wen start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1); 98af62a557SLei Wen start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE; 99af62a557SLei Wen sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS); 100af62a557SLei Wen } 101af62a557SLei Wen #endif 102a004abdeSLei Wen if (timeout-- > 0) 103a004abdeSLei Wen udelay(10); 104a004abdeSLei Wen else { 105a004abdeSLei Wen printf("Transfer data timeout\n"); 106a004abdeSLei Wen return -1; 107a004abdeSLei Wen } 108af62a557SLei Wen } while (!(stat & SDHCI_INT_DATA_END)); 109af62a557SLei Wen return 0; 110af62a557SLei Wen } 111af62a557SLei Wen 112*56b34bc6SPrzemyslaw Marczak /* 113*56b34bc6SPrzemyslaw Marczak * No command will be sent by driver if card is busy, so driver must wait 114*56b34bc6SPrzemyslaw Marczak * for card ready state. 115*56b34bc6SPrzemyslaw Marczak * Every time when card is busy after timeout then (last) timeout value will be 116*56b34bc6SPrzemyslaw Marczak * increased twice but only if it doesn't exceed global defined maximum. 117*56b34bc6SPrzemyslaw Marczak * Each function call will use last timeout value. Max timeout can be redefined 118*56b34bc6SPrzemyslaw Marczak * in board config file. 119*56b34bc6SPrzemyslaw Marczak */ 120*56b34bc6SPrzemyslaw Marczak #ifndef CONFIG_SDHCI_CMD_MAX_TIMEOUT 121*56b34bc6SPrzemyslaw Marczak #define CONFIG_SDHCI_CMD_MAX_TIMEOUT 3200 122*56b34bc6SPrzemyslaw Marczak #endif 123*56b34bc6SPrzemyslaw Marczak #define CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT 100 124*56b34bc6SPrzemyslaw Marczak 125af62a557SLei Wen int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd, 126af62a557SLei Wen struct mmc_data *data) 127af62a557SLei Wen { 128af62a557SLei Wen struct sdhci_host *host = (struct sdhci_host *)mmc->priv; 129af62a557SLei Wen unsigned int stat = 0; 130af62a557SLei Wen int ret = 0; 131af62a557SLei Wen int trans_bytes = 0, is_aligned = 1; 132af62a557SLei Wen u32 mask, flags, mode; 133*56b34bc6SPrzemyslaw Marczak unsigned int time = 0, start_addr = 0; 1343a638320SJaehoon Chung unsigned int retry = 10000; 135*56b34bc6SPrzemyslaw Marczak int mmc_dev = mmc->block_dev.dev; 136af62a557SLei Wen 137*56b34bc6SPrzemyslaw Marczak /* Timeout unit - ms */ 138*56b34bc6SPrzemyslaw Marczak static unsigned int cmd_timeout = CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT; 139af62a557SLei Wen 140af62a557SLei Wen sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS); 141af62a557SLei Wen mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT; 142af62a557SLei Wen 143af62a557SLei Wen /* We shouldn't wait for data inihibit for stop commands, even 144af62a557SLei Wen though they might use busy signaling */ 145af62a557SLei Wen if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 146af62a557SLei Wen mask &= ~SDHCI_DATA_INHIBIT; 147af62a557SLei Wen 148af62a557SLei Wen while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { 149*56b34bc6SPrzemyslaw Marczak if (time >= cmd_timeout) { 150*56b34bc6SPrzemyslaw Marczak printf("MMC: %d busy ", mmc_dev); 151*56b34bc6SPrzemyslaw Marczak if (2 * cmd_timeout <= CONFIG_SDHCI_CMD_MAX_TIMEOUT) { 152*56b34bc6SPrzemyslaw Marczak cmd_timeout += cmd_timeout; 153*56b34bc6SPrzemyslaw Marczak printf("timeout increasing to: %u ms.\n", 154*56b34bc6SPrzemyslaw Marczak cmd_timeout); 155*56b34bc6SPrzemyslaw Marczak } else { 156*56b34bc6SPrzemyslaw Marczak puts("timeout.\n"); 157af62a557SLei Wen return COMM_ERR; 158af62a557SLei Wen } 159*56b34bc6SPrzemyslaw Marczak } 160*56b34bc6SPrzemyslaw Marczak time++; 161af62a557SLei Wen udelay(1000); 162af62a557SLei Wen } 163af62a557SLei Wen 164af62a557SLei Wen mask = SDHCI_INT_RESPONSE; 165af62a557SLei Wen if (!(cmd->resp_type & MMC_RSP_PRESENT)) 166af62a557SLei Wen flags = SDHCI_CMD_RESP_NONE; 167af62a557SLei Wen else if (cmd->resp_type & MMC_RSP_136) 168af62a557SLei Wen flags = SDHCI_CMD_RESP_LONG; 169af62a557SLei Wen else if (cmd->resp_type & MMC_RSP_BUSY) { 170af62a557SLei Wen flags = SDHCI_CMD_RESP_SHORT_BUSY; 171af62a557SLei Wen mask |= SDHCI_INT_DATA_END; 172af62a557SLei Wen } else 173af62a557SLei Wen flags = SDHCI_CMD_RESP_SHORT; 174af62a557SLei Wen 175af62a557SLei Wen if (cmd->resp_type & MMC_RSP_CRC) 176af62a557SLei Wen flags |= SDHCI_CMD_CRC; 177af62a557SLei Wen if (cmd->resp_type & MMC_RSP_OPCODE) 178af62a557SLei Wen flags |= SDHCI_CMD_INDEX; 179af62a557SLei Wen if (data) 180af62a557SLei Wen flags |= SDHCI_CMD_DATA; 181af62a557SLei Wen 182af62a557SLei Wen /*Set Transfer mode regarding to data flag*/ 183af62a557SLei Wen if (data != 0) { 184af62a557SLei Wen sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); 185af62a557SLei Wen mode = SDHCI_TRNS_BLK_CNT_EN; 186af62a557SLei Wen trans_bytes = data->blocks * data->blocksize; 187af62a557SLei Wen if (data->blocks > 1) 188af62a557SLei Wen mode |= SDHCI_TRNS_MULTI; 189af62a557SLei Wen 190af62a557SLei Wen if (data->flags == MMC_DATA_READ) 191af62a557SLei Wen mode |= SDHCI_TRNS_READ; 192af62a557SLei Wen 193af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 194af62a557SLei Wen if (data->flags == MMC_DATA_READ) 195af62a557SLei Wen start_addr = (unsigned int)data->dest; 196af62a557SLei Wen else 197af62a557SLei Wen start_addr = (unsigned int)data->src; 198af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 199af62a557SLei Wen (start_addr & 0x7) != 0x0) { 200af62a557SLei Wen is_aligned = 0; 201af62a557SLei Wen start_addr = (unsigned int)aligned_buffer; 202af62a557SLei Wen if (data->flags != MMC_DATA_READ) 203af62a557SLei Wen memcpy(aligned_buffer, data->src, trans_bytes); 204af62a557SLei Wen } 205af62a557SLei Wen 206af62a557SLei Wen sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS); 207af62a557SLei Wen mode |= SDHCI_TRNS_DMA; 208af62a557SLei Wen #endif 209af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 210af62a557SLei Wen data->blocksize), 211af62a557SLei Wen SDHCI_BLOCK_SIZE); 212af62a557SLei Wen sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); 213af62a557SLei Wen sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); 214af62a557SLei Wen } 215af62a557SLei Wen 216af62a557SLei Wen sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT); 217af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 2182c2ec4c9SLei Wen flush_cache(start_addr, trans_bytes); 219af62a557SLei Wen #endif 220af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND); 221af62a557SLei Wen do { 222af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 223af62a557SLei Wen if (stat & SDHCI_INT_ERROR) 224af62a557SLei Wen break; 2253a638320SJaehoon Chung if (--retry == 0) 2263a638320SJaehoon Chung break; 227af62a557SLei Wen } while ((stat & mask) != mask); 228af62a557SLei Wen 2293a638320SJaehoon Chung if (retry == 0) { 2303a638320SJaehoon Chung if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) 2313a638320SJaehoon Chung return 0; 2323a638320SJaehoon Chung else { 2333a638320SJaehoon Chung printf("Timeout for status update!\n"); 2343a638320SJaehoon Chung return TIMEOUT; 2353a638320SJaehoon Chung } 2363a638320SJaehoon Chung } 2373a638320SJaehoon Chung 238af62a557SLei Wen if ((stat & (SDHCI_INT_ERROR | mask)) == mask) { 239af62a557SLei Wen sdhci_cmd_done(host, cmd); 240af62a557SLei Wen sdhci_writel(host, mask, SDHCI_INT_STATUS); 241af62a557SLei Wen } else 242af62a557SLei Wen ret = -1; 243af62a557SLei Wen 244af62a557SLei Wen if (!ret && data) 245af62a557SLei Wen ret = sdhci_transfer_data(host, data, start_addr); 246af62a557SLei Wen 24713243f2eSTushar Behera if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD) 24813243f2eSTushar Behera udelay(1000); 24913243f2eSTushar Behera 250af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 251af62a557SLei Wen sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS); 252af62a557SLei Wen if (!ret) { 253af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 254af62a557SLei Wen !is_aligned && (data->flags == MMC_DATA_READ)) 255af62a557SLei Wen memcpy(data->dest, aligned_buffer, trans_bytes); 256af62a557SLei Wen return 0; 257af62a557SLei Wen } 258af62a557SLei Wen 259af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_CMD); 260af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_DATA); 261af62a557SLei Wen if (stat & SDHCI_INT_TIMEOUT) 262af62a557SLei Wen return TIMEOUT; 263af62a557SLei Wen else 264af62a557SLei Wen return COMM_ERR; 265af62a557SLei Wen } 266af62a557SLei Wen 267af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock) 268af62a557SLei Wen { 269af62a557SLei Wen struct sdhci_host *host = (struct sdhci_host *)mmc->priv; 270af62a557SLei Wen unsigned int div, clk, timeout; 271af62a557SLei Wen 272af62a557SLei Wen sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); 273af62a557SLei Wen 274af62a557SLei Wen if (clock == 0) 275af62a557SLei Wen return 0; 276af62a557SLei Wen 277113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) { 278af62a557SLei Wen /* Version 3.00 divisors must be a multiple of 2. */ 279af62a557SLei Wen if (mmc->f_max <= clock) 280af62a557SLei Wen div = 1; 281af62a557SLei Wen else { 282af62a557SLei Wen for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) { 283af62a557SLei Wen if ((mmc->f_max / div) <= clock) 284af62a557SLei Wen break; 285af62a557SLei Wen } 286af62a557SLei Wen } 287af62a557SLei Wen } else { 288af62a557SLei Wen /* Version 2.00 divisors must be a power of 2. */ 289af62a557SLei Wen for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) { 290af62a557SLei Wen if ((mmc->f_max / div) <= clock) 291af62a557SLei Wen break; 292af62a557SLei Wen } 293af62a557SLei Wen } 294af62a557SLei Wen div >>= 1; 295af62a557SLei Wen 296b09ed6e4SJaehoon Chung if (host->set_clock) 297b09ed6e4SJaehoon Chung host->set_clock(host->index, div); 298b09ed6e4SJaehoon Chung 299af62a557SLei Wen clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; 300af62a557SLei Wen clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) 301af62a557SLei Wen << SDHCI_DIVIDER_HI_SHIFT; 302af62a557SLei Wen clk |= SDHCI_CLOCK_INT_EN; 303af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 304af62a557SLei Wen 305af62a557SLei Wen /* Wait max 20 ms */ 306af62a557SLei Wen timeout = 20; 307af62a557SLei Wen while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) 308af62a557SLei Wen & SDHCI_CLOCK_INT_STABLE)) { 309af62a557SLei Wen if (timeout == 0) { 310af62a557SLei Wen printf("Internal clock never stabilised.\n"); 311af62a557SLei Wen return -1; 312af62a557SLei Wen } 313af62a557SLei Wen timeout--; 314af62a557SLei Wen udelay(1000); 315af62a557SLei Wen } 316af62a557SLei Wen 317af62a557SLei Wen clk |= SDHCI_CLOCK_CARD_EN; 318af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 319af62a557SLei Wen return 0; 320af62a557SLei Wen } 321af62a557SLei Wen 322af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power) 323af62a557SLei Wen { 324af62a557SLei Wen u8 pwr = 0; 325af62a557SLei Wen 326af62a557SLei Wen if (power != (unsigned short)-1) { 327af62a557SLei Wen switch (1 << power) { 328af62a557SLei Wen case MMC_VDD_165_195: 329af62a557SLei Wen pwr = SDHCI_POWER_180; 330af62a557SLei Wen break; 331af62a557SLei Wen case MMC_VDD_29_30: 332af62a557SLei Wen case MMC_VDD_30_31: 333af62a557SLei Wen pwr = SDHCI_POWER_300; 334af62a557SLei Wen break; 335af62a557SLei Wen case MMC_VDD_32_33: 336af62a557SLei Wen case MMC_VDD_33_34: 337af62a557SLei Wen pwr = SDHCI_POWER_330; 338af62a557SLei Wen break; 339af62a557SLei Wen } 340af62a557SLei Wen } 341af62a557SLei Wen 342af62a557SLei Wen if (pwr == 0) { 343af62a557SLei Wen sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); 344af62a557SLei Wen return; 345af62a557SLei Wen } 346af62a557SLei Wen 347688c2d14SMela Custodio if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) 348688c2d14SMela Custodio sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 349688c2d14SMela Custodio 350af62a557SLei Wen pwr |= SDHCI_POWER_ON; 351af62a557SLei Wen 352af62a557SLei Wen sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 353af62a557SLei Wen } 354af62a557SLei Wen 355af62a557SLei Wen void sdhci_set_ios(struct mmc *mmc) 356af62a557SLei Wen { 357af62a557SLei Wen u32 ctrl; 358af62a557SLei Wen struct sdhci_host *host = (struct sdhci_host *)mmc->priv; 359af62a557SLei Wen 360236bfecfSJaehoon Chung if (host->set_control_reg) 361236bfecfSJaehoon Chung host->set_control_reg(host); 362236bfecfSJaehoon Chung 363af62a557SLei Wen if (mmc->clock != host->clock) 364af62a557SLei Wen sdhci_set_clock(mmc, mmc->clock); 365af62a557SLei Wen 366af62a557SLei Wen /* Set bus width */ 367af62a557SLei Wen ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 368af62a557SLei Wen if (mmc->bus_width == 8) { 369af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 370113e5dfcSJaehoon Chung if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) || 371113e5dfcSJaehoon Chung (host->quirks & SDHCI_QUIRK_USE_WIDE8)) 372af62a557SLei Wen ctrl |= SDHCI_CTRL_8BITBUS; 373af62a557SLei Wen } else { 374113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) 375af62a557SLei Wen ctrl &= ~SDHCI_CTRL_8BITBUS; 376af62a557SLei Wen if (mmc->bus_width == 4) 377af62a557SLei Wen ctrl |= SDHCI_CTRL_4BITBUS; 378af62a557SLei Wen else 379af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 380af62a557SLei Wen } 381af62a557SLei Wen 382af62a557SLei Wen if (mmc->clock > 26000000) 383af62a557SLei Wen ctrl |= SDHCI_CTRL_HISPD; 384af62a557SLei Wen else 385af62a557SLei Wen ctrl &= ~SDHCI_CTRL_HISPD; 386af62a557SLei Wen 387236bfecfSJaehoon Chung if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) 388236bfecfSJaehoon Chung ctrl &= ~SDHCI_CTRL_HISPD; 389236bfecfSJaehoon Chung 390af62a557SLei Wen sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 391af62a557SLei Wen } 392af62a557SLei Wen 393af62a557SLei Wen int sdhci_init(struct mmc *mmc) 394af62a557SLei Wen { 395af62a557SLei Wen struct sdhci_host *host = (struct sdhci_host *)mmc->priv; 396af62a557SLei Wen 397af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) { 398af62a557SLei Wen aligned_buffer = memalign(8, 512*1024); 399af62a557SLei Wen if (!aligned_buffer) { 400af62a557SLei Wen printf("Aligned buffer alloc failed!!!"); 401af62a557SLei Wen return -1; 402af62a557SLei Wen } 403af62a557SLei Wen } 404af62a557SLei Wen 405470dcc75SJoe Hershberger sdhci_set_power(host, fls(mmc->voltages) - 1); 406470dcc75SJoe Hershberger 407470dcc75SJoe Hershberger if (host->quirks & SDHCI_QUIRK_NO_CD) { 408470dcc75SJoe Hershberger unsigned int status; 409470dcc75SJoe Hershberger 410470dcc75SJoe Hershberger sdhci_writel(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST, 411470dcc75SJoe Hershberger SDHCI_HOST_CONTROL); 412470dcc75SJoe Hershberger 413470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 414470dcc75SJoe Hershberger while ((!(status & SDHCI_CARD_PRESENT)) || 415470dcc75SJoe Hershberger (!(status & SDHCI_CARD_STATE_STABLE)) || 416470dcc75SJoe Hershberger (!(status & SDHCI_CARD_DETECT_PIN_LEVEL))) 417470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 418470dcc75SJoe Hershberger } 419470dcc75SJoe Hershberger 420ce0c1bc1SŁukasz Majewski /* Enable only interrupts served by the SD controller */ 421ce0c1bc1SŁukasz Majewski sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK 422ce0c1bc1SŁukasz Majewski , SDHCI_INT_ENABLE); 423ce0c1bc1SŁukasz Majewski /* Mask all sdhci interrupt sources */ 424ce0c1bc1SŁukasz Majewski sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE); 425af62a557SLei Wen 426af62a557SLei Wen return 0; 427af62a557SLei Wen } 428af62a557SLei Wen 429af62a557SLei Wen int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk) 430af62a557SLei Wen { 431af62a557SLei Wen struct mmc *mmc; 432af62a557SLei Wen unsigned int caps; 433af62a557SLei Wen 434af62a557SLei Wen mmc = malloc(sizeof(struct mmc)); 435af62a557SLei Wen if (!mmc) { 436af62a557SLei Wen printf("mmc malloc fail!\n"); 437af62a557SLei Wen return -1; 438af62a557SLei Wen } 439af62a557SLei Wen 440af62a557SLei Wen mmc->priv = host; 4416cf1b17cSLei Wen host->mmc = mmc; 442af62a557SLei Wen 443af62a557SLei Wen sprintf(mmc->name, "%s", host->name); 444af62a557SLei Wen mmc->send_cmd = sdhci_send_command; 445af62a557SLei Wen mmc->set_ios = sdhci_set_ios; 446af62a557SLei Wen mmc->init = sdhci_init; 44748972d90SThierry Reding mmc->getcd = NULL; 448d23d8d7eSNikita Kiryanov mmc->getwp = NULL; 449af62a557SLei Wen 450af62a557SLei Wen caps = sdhci_readl(host, SDHCI_CAPABILITIES); 451af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 452af62a557SLei Wen if (!(caps & SDHCI_CAN_DO_SDMA)) { 453af62a557SLei Wen printf("Your controller don't support sdma!!\n"); 454af62a557SLei Wen return -1; 455af62a557SLei Wen } 456af62a557SLei Wen #endif 457af62a557SLei Wen 458af62a557SLei Wen if (max_clk) 459af62a557SLei Wen mmc->f_max = max_clk; 460af62a557SLei Wen else { 461113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) 462af62a557SLei Wen mmc->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK) 463af62a557SLei Wen >> SDHCI_CLOCK_BASE_SHIFT; 464af62a557SLei Wen else 465af62a557SLei Wen mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK) 466af62a557SLei Wen >> SDHCI_CLOCK_BASE_SHIFT; 467af62a557SLei Wen mmc->f_max *= 1000000; 468af62a557SLei Wen } 469af62a557SLei Wen if (mmc->f_max == 0) { 470af62a557SLei Wen printf("Hardware doesn't specify base clock frequency\n"); 471af62a557SLei Wen return -1; 472af62a557SLei Wen } 473af62a557SLei Wen if (min_clk) 474af62a557SLei Wen mmc->f_min = min_clk; 475af62a557SLei Wen else { 476113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) 477af62a557SLei Wen mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_300; 478af62a557SLei Wen else 479af62a557SLei Wen mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_200; 480af62a557SLei Wen } 481af62a557SLei Wen 482af62a557SLei Wen mmc->voltages = 0; 483af62a557SLei Wen if (caps & SDHCI_CAN_VDD_330) 484af62a557SLei Wen mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; 485af62a557SLei Wen if (caps & SDHCI_CAN_VDD_300) 486af62a557SLei Wen mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; 487af62a557SLei Wen if (caps & SDHCI_CAN_VDD_180) 488af62a557SLei Wen mmc->voltages |= MMC_VDD_165_195; 489236bfecfSJaehoon Chung 490236bfecfSJaehoon Chung if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE) 491236bfecfSJaehoon Chung mmc->voltages |= host->voltages; 492236bfecfSJaehoon Chung 493af62a557SLei Wen mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT; 494113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) { 495af62a557SLei Wen if (caps & SDHCI_CAN_DO_8BIT) 496af62a557SLei Wen mmc->host_caps |= MMC_MODE_8BIT; 4971695b29aSJagannadha Sutradharudu Teki } 498236bfecfSJaehoon Chung if (host->host_caps) 499236bfecfSJaehoon Chung mmc->host_caps |= host->host_caps; 500af62a557SLei Wen 501af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_ALL); 502af62a557SLei Wen mmc_register(mmc); 503af62a557SLei Wen 504af62a557SLei Wen return 0; 505af62a557SLei Wen } 506