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> 122a809093SSimon Glass #include <errno.h> 13af62a557SLei Wen #include <malloc.h> 14af62a557SLei Wen #include <mmc.h> 15af62a557SLei Wen #include <sdhci.h> 16af62a557SLei Wen 17492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER) 18492d3223SStefan Roese void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER; 19492d3223SStefan Roese #else 20af62a557SLei Wen void *aligned_buffer; 21492d3223SStefan Roese #endif 22af62a557SLei Wen 23af62a557SLei Wen static void sdhci_reset(struct sdhci_host *host, u8 mask) 24af62a557SLei Wen { 25af62a557SLei Wen unsigned long timeout; 26af62a557SLei Wen 27af62a557SLei Wen /* Wait max 100 ms */ 28af62a557SLei Wen timeout = 100; 29af62a557SLei Wen sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET); 30af62a557SLei Wen while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) { 31af62a557SLei Wen if (timeout == 0) { 3230e6d979SDarwin Rambo printf("%s: Reset 0x%x never completed.\n", 3330e6d979SDarwin Rambo __func__, (int)mask); 34af62a557SLei Wen return; 35af62a557SLei Wen } 36af62a557SLei Wen timeout--; 37af62a557SLei Wen udelay(1000); 38af62a557SLei Wen } 39af62a557SLei Wen } 40af62a557SLei Wen 41af62a557SLei Wen static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd) 42af62a557SLei Wen { 43af62a557SLei Wen int i; 44af62a557SLei Wen if (cmd->resp_type & MMC_RSP_136) { 45af62a557SLei Wen /* CRC is stripped so we need to do some shifting. */ 46af62a557SLei Wen for (i = 0; i < 4; i++) { 47af62a557SLei Wen cmd->response[i] = sdhci_readl(host, 48af62a557SLei Wen SDHCI_RESPONSE + (3-i)*4) << 8; 49af62a557SLei Wen if (i != 3) 50af62a557SLei Wen cmd->response[i] |= sdhci_readb(host, 51af62a557SLei Wen SDHCI_RESPONSE + (3-i)*4-1); 52af62a557SLei Wen } 53af62a557SLei Wen } else { 54af62a557SLei Wen cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE); 55af62a557SLei Wen } 56af62a557SLei Wen } 57af62a557SLei Wen 58af62a557SLei Wen static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data) 59af62a557SLei Wen { 60af62a557SLei Wen int i; 61af62a557SLei Wen char *offs; 62af62a557SLei Wen for (i = 0; i < data->blocksize; i += 4) { 63af62a557SLei Wen offs = data->dest + i; 64af62a557SLei Wen if (data->flags == MMC_DATA_READ) 65af62a557SLei Wen *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER); 66af62a557SLei Wen else 67af62a557SLei Wen sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER); 68af62a557SLei Wen } 69af62a557SLei Wen } 70af62a557SLei Wen 71af62a557SLei Wen static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data, 72af62a557SLei Wen unsigned int start_addr) 73af62a557SLei Wen { 74a004abdeSLei Wen unsigned int stat, rdy, mask, timeout, block = 0; 75804c7f42SJaehoon Chung #ifdef CONFIG_MMC_SDMA 76804c7f42SJaehoon Chung unsigned char ctrl; 772c011847SJuhyun \(Justin\) Oh ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 78804c7f42SJaehoon Chung ctrl &= ~SDHCI_CTRL_DMA_MASK; 792c011847SJuhyun \(Justin\) Oh sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 80804c7f42SJaehoon Chung #endif 81af62a557SLei Wen 825d48e422SJaehoon Chung timeout = 1000000; 83af62a557SLei Wen rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL; 84af62a557SLei Wen mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE; 85af62a557SLei Wen do { 86af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 87af62a557SLei Wen if (stat & SDHCI_INT_ERROR) { 8830e6d979SDarwin Rambo printf("%s: Error detected in status(0x%X)!\n", 8930e6d979SDarwin Rambo __func__, stat); 90af62a557SLei Wen return -1; 91af62a557SLei Wen } 92af62a557SLei Wen if (stat & rdy) { 93af62a557SLei Wen if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask)) 94af62a557SLei Wen continue; 95af62a557SLei Wen sdhci_writel(host, rdy, SDHCI_INT_STATUS); 96af62a557SLei Wen sdhci_transfer_pio(host, data); 97af62a557SLei Wen data->dest += data->blocksize; 98af62a557SLei Wen if (++block >= data->blocks) 99af62a557SLei Wen break; 100af62a557SLei Wen } 101af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 102af62a557SLei Wen if (stat & SDHCI_INT_DMA_END) { 103af62a557SLei Wen sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS); 1043e81c772SLei Wen start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1); 105af62a557SLei Wen start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE; 106af62a557SLei Wen sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS); 107af62a557SLei Wen } 108af62a557SLei Wen #endif 109a004abdeSLei Wen if (timeout-- > 0) 110a004abdeSLei Wen udelay(10); 111a004abdeSLei Wen else { 11230e6d979SDarwin Rambo printf("%s: Transfer data timeout\n", __func__); 113a004abdeSLei Wen return -1; 114a004abdeSLei Wen } 115af62a557SLei Wen } while (!(stat & SDHCI_INT_DATA_END)); 116af62a557SLei Wen return 0; 117af62a557SLei Wen } 118af62a557SLei Wen 11956b34bc6SPrzemyslaw Marczak /* 12056b34bc6SPrzemyslaw Marczak * No command will be sent by driver if card is busy, so driver must wait 12156b34bc6SPrzemyslaw Marczak * for card ready state. 12256b34bc6SPrzemyslaw Marczak * Every time when card is busy after timeout then (last) timeout value will be 12356b34bc6SPrzemyslaw Marczak * increased twice but only if it doesn't exceed global defined maximum. 12456b34bc6SPrzemyslaw Marczak * Each function call will use last timeout value. Max timeout can be redefined 12556b34bc6SPrzemyslaw Marczak * in board config file. 12656b34bc6SPrzemyslaw Marczak */ 12756b34bc6SPrzemyslaw Marczak #ifndef CONFIG_SDHCI_CMD_MAX_TIMEOUT 12856b34bc6SPrzemyslaw Marczak #define CONFIG_SDHCI_CMD_MAX_TIMEOUT 3200 12956b34bc6SPrzemyslaw Marczak #endif 13056b34bc6SPrzemyslaw Marczak #define CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT 100 131d90bb439SSteve Rae #define SDHCI_READ_STATUS_TIMEOUT 1000 13256b34bc6SPrzemyslaw Marczak 133ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 134ef1e4edaSSimon Glass static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd, 135ef1e4edaSSimon Glass struct mmc_data *data) 136ef1e4edaSSimon Glass { 137ef1e4edaSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 138ef1e4edaSSimon Glass 139ef1e4edaSSimon Glass #else 1406588c78bSJeroen Hofstee static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd, 141af62a557SLei Wen struct mmc_data *data) 142af62a557SLei Wen { 143ef1e4edaSSimon Glass #endif 14493bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 145af62a557SLei Wen unsigned int stat = 0; 146af62a557SLei Wen int ret = 0; 147af62a557SLei Wen int trans_bytes = 0, is_aligned = 1; 148af62a557SLei Wen u32 mask, flags, mode; 14956b34bc6SPrzemyslaw Marczak unsigned int time = 0, start_addr = 0; 15019d2e342SSimon Glass int mmc_dev = mmc_get_blk_desc(mmc)->devnum; 15129905a45SStefan Roese unsigned start = get_timer(0); 152af62a557SLei Wen 15356b34bc6SPrzemyslaw Marczak /* Timeout unit - ms */ 15456b34bc6SPrzemyslaw Marczak static unsigned int cmd_timeout = CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT; 155af62a557SLei Wen 156af62a557SLei Wen sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS); 157af62a557SLei Wen mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT; 158af62a557SLei Wen 159af62a557SLei Wen /* We shouldn't wait for data inihibit for stop commands, even 160af62a557SLei Wen though they might use busy signaling */ 161af62a557SLei Wen if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 162af62a557SLei Wen mask &= ~SDHCI_DATA_INHIBIT; 163af62a557SLei Wen 164af62a557SLei Wen while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { 16556b34bc6SPrzemyslaw Marczak if (time >= cmd_timeout) { 16630e6d979SDarwin Rambo printf("%s: MMC: %d busy ", __func__, mmc_dev); 16756b34bc6SPrzemyslaw Marczak if (2 * cmd_timeout <= CONFIG_SDHCI_CMD_MAX_TIMEOUT) { 16856b34bc6SPrzemyslaw Marczak cmd_timeout += cmd_timeout; 16956b34bc6SPrzemyslaw Marczak printf("timeout increasing to: %u ms.\n", 17056b34bc6SPrzemyslaw Marczak cmd_timeout); 17156b34bc6SPrzemyslaw Marczak } else { 17256b34bc6SPrzemyslaw Marczak puts("timeout.\n"); 173af62a557SLei Wen return COMM_ERR; 174af62a557SLei Wen } 17556b34bc6SPrzemyslaw Marczak } 17656b34bc6SPrzemyslaw Marczak time++; 177af62a557SLei Wen udelay(1000); 178af62a557SLei Wen } 179af62a557SLei Wen 180af62a557SLei Wen mask = SDHCI_INT_RESPONSE; 181af62a557SLei Wen if (!(cmd->resp_type & MMC_RSP_PRESENT)) 182af62a557SLei Wen flags = SDHCI_CMD_RESP_NONE; 183af62a557SLei Wen else if (cmd->resp_type & MMC_RSP_136) 184af62a557SLei Wen flags = SDHCI_CMD_RESP_LONG; 185af62a557SLei Wen else if (cmd->resp_type & MMC_RSP_BUSY) { 186af62a557SLei Wen flags = SDHCI_CMD_RESP_SHORT_BUSY; 187af62a557SLei Wen mask |= SDHCI_INT_DATA_END; 188af62a557SLei Wen } else 189af62a557SLei Wen flags = SDHCI_CMD_RESP_SHORT; 190af62a557SLei Wen 191af62a557SLei Wen if (cmd->resp_type & MMC_RSP_CRC) 192af62a557SLei Wen flags |= SDHCI_CMD_CRC; 193af62a557SLei Wen if (cmd->resp_type & MMC_RSP_OPCODE) 194af62a557SLei Wen flags |= SDHCI_CMD_INDEX; 195af62a557SLei Wen if (data) 196af62a557SLei Wen flags |= SDHCI_CMD_DATA; 197af62a557SLei Wen 198af62a557SLei Wen /* Set Transfer mode regarding to data flag */ 199af62a557SLei Wen if (data != 0) { 200af62a557SLei Wen sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); 201af62a557SLei Wen mode = SDHCI_TRNS_BLK_CNT_EN; 202af62a557SLei Wen trans_bytes = data->blocks * data->blocksize; 203af62a557SLei Wen if (data->blocks > 1) 204af62a557SLei Wen mode |= SDHCI_TRNS_MULTI; 205af62a557SLei Wen 206af62a557SLei Wen if (data->flags == MMC_DATA_READ) 207af62a557SLei Wen mode |= SDHCI_TRNS_READ; 208af62a557SLei Wen 209af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 210af62a557SLei Wen if (data->flags == MMC_DATA_READ) 2113c1fcb77SRob Herring start_addr = (unsigned long)data->dest; 212af62a557SLei Wen else 2133c1fcb77SRob Herring start_addr = (unsigned long)data->src; 214af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 215af62a557SLei Wen (start_addr & 0x7) != 0x0) { 216af62a557SLei Wen is_aligned = 0; 2173c1fcb77SRob Herring start_addr = (unsigned long)aligned_buffer; 218af62a557SLei Wen if (data->flags != MMC_DATA_READ) 219af62a557SLei Wen memcpy(aligned_buffer, data->src, trans_bytes); 220af62a557SLei Wen } 221af62a557SLei Wen 222492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER) 223492d3223SStefan Roese /* 224492d3223SStefan Roese * Always use this bounce-buffer when 225492d3223SStefan Roese * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined 226492d3223SStefan Roese */ 227492d3223SStefan Roese is_aligned = 0; 228492d3223SStefan Roese start_addr = (unsigned long)aligned_buffer; 229492d3223SStefan Roese if (data->flags != MMC_DATA_READ) 230492d3223SStefan Roese memcpy(aligned_buffer, data->src, trans_bytes); 231492d3223SStefan Roese #endif 232492d3223SStefan Roese 233af62a557SLei Wen sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS); 234af62a557SLei Wen mode |= SDHCI_TRNS_DMA; 235af62a557SLei Wen #endif 236af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 237af62a557SLei Wen data->blocksize), 238af62a557SLei Wen SDHCI_BLOCK_SIZE); 239af62a557SLei Wen sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); 240af62a557SLei Wen sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); 2415e1c23cdSKevin Liu } else if (cmd->resp_type & MMC_RSP_BUSY) { 2425e1c23cdSKevin Liu sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); 243af62a557SLei Wen } 244af62a557SLei Wen 245af62a557SLei Wen sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT); 246af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 2472c2ec4c9SLei Wen flush_cache(start_addr, trans_bytes); 248af62a557SLei Wen #endif 249af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND); 25029905a45SStefan Roese start = get_timer(0); 251af62a557SLei Wen do { 252af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 253af62a557SLei Wen if (stat & SDHCI_INT_ERROR) 254af62a557SLei Wen break; 255af62a557SLei Wen 256d90bb439SSteve Rae if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) { 257*bae4a1fdSMasahiro Yamada if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) { 2583a638320SJaehoon Chung return 0; 259*bae4a1fdSMasahiro Yamada } else { 260*bae4a1fdSMasahiro Yamada printf("%s: Timeout for status update!\n", 261*bae4a1fdSMasahiro Yamada __func__); 2623a638320SJaehoon Chung return TIMEOUT; 2633a638320SJaehoon Chung } 2643a638320SJaehoon Chung } 265*bae4a1fdSMasahiro Yamada } while ((stat & mask) != mask); 2663a638320SJaehoon Chung 267af62a557SLei Wen if ((stat & (SDHCI_INT_ERROR | mask)) == mask) { 268af62a557SLei Wen sdhci_cmd_done(host, cmd); 269af62a557SLei Wen sdhci_writel(host, mask, SDHCI_INT_STATUS); 270af62a557SLei Wen } else 271af62a557SLei Wen ret = -1; 272af62a557SLei Wen 273af62a557SLei Wen if (!ret && data) 274af62a557SLei Wen ret = sdhci_transfer_data(host, data, start_addr); 275af62a557SLei Wen 27613243f2eSTushar Behera if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD) 27713243f2eSTushar Behera udelay(1000); 27813243f2eSTushar Behera 279af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 280af62a557SLei Wen sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS); 281af62a557SLei Wen if (!ret) { 282af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 283af62a557SLei Wen !is_aligned && (data->flags == MMC_DATA_READ)) 284af62a557SLei Wen memcpy(data->dest, aligned_buffer, trans_bytes); 285af62a557SLei Wen return 0; 286af62a557SLei Wen } 287af62a557SLei Wen 288af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_CMD); 289af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_DATA); 290af62a557SLei Wen if (stat & SDHCI_INT_TIMEOUT) 291af62a557SLei Wen return TIMEOUT; 292af62a557SLei Wen else 293af62a557SLei Wen return COMM_ERR; 294af62a557SLei Wen } 295af62a557SLei Wen 296af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock) 297af62a557SLei Wen { 29893bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 29979667b7bSWenyou Yang unsigned int div, clk, timeout, reg; 300af62a557SLei Wen 30179667b7bSWenyou Yang /* Wait max 20 ms */ 30279667b7bSWenyou Yang timeout = 200; 30379667b7bSWenyou Yang while (sdhci_readl(host, SDHCI_PRESENT_STATE) & 30479667b7bSWenyou Yang (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) { 30579667b7bSWenyou Yang if (timeout == 0) { 30679667b7bSWenyou Yang printf("%s: Timeout to wait cmd & data inhibit\n", 30779667b7bSWenyou Yang __func__); 30879667b7bSWenyou Yang return -1; 30979667b7bSWenyou Yang } 31079667b7bSWenyou Yang 31179667b7bSWenyou Yang timeout--; 31279667b7bSWenyou Yang udelay(100); 31379667b7bSWenyou Yang } 31479667b7bSWenyou Yang 31579667b7bSWenyou Yang reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 3161d405e20SSiva Durga Prasad Paladugu reg &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN); 31779667b7bSWenyou Yang sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL); 318af62a557SLei Wen 319af62a557SLei Wen if (clock == 0) 320af62a557SLei Wen return 0; 321af62a557SLei Wen 322113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) { 323af62a557SLei Wen /* Version 3.00 divisors must be a multiple of 2. */ 32493bfd616SPantelis Antoniou if (mmc->cfg->f_max <= clock) 325af62a557SLei Wen div = 1; 326af62a557SLei Wen else { 327af62a557SLei Wen for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) { 32893bfd616SPantelis Antoniou if ((mmc->cfg->f_max / div) <= clock) 329af62a557SLei Wen break; 330af62a557SLei Wen } 331af62a557SLei Wen } 332af62a557SLei Wen } else { 333af62a557SLei Wen /* Version 2.00 divisors must be a power of 2. */ 334af62a557SLei Wen for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) { 33593bfd616SPantelis Antoniou if ((mmc->cfg->f_max / div) <= clock) 336af62a557SLei Wen break; 337af62a557SLei Wen } 338af62a557SLei Wen } 339af62a557SLei Wen div >>= 1; 340af62a557SLei Wen 341b09ed6e4SJaehoon Chung if (host->set_clock) 342b09ed6e4SJaehoon Chung host->set_clock(host->index, div); 343b09ed6e4SJaehoon Chung 344af62a557SLei Wen clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; 345af62a557SLei Wen clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) 346af62a557SLei Wen << SDHCI_DIVIDER_HI_SHIFT; 347af62a557SLei Wen clk |= SDHCI_CLOCK_INT_EN; 348af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 349af62a557SLei Wen 350af62a557SLei Wen /* Wait max 20 ms */ 351af62a557SLei Wen timeout = 20; 352af62a557SLei Wen while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) 353af62a557SLei Wen & SDHCI_CLOCK_INT_STABLE)) { 354af62a557SLei Wen if (timeout == 0) { 35530e6d979SDarwin Rambo printf("%s: Internal clock never stabilised.\n", 35630e6d979SDarwin Rambo __func__); 357af62a557SLei Wen return -1; 358af62a557SLei Wen } 359af62a557SLei Wen timeout--; 360af62a557SLei Wen udelay(1000); 361af62a557SLei Wen } 362af62a557SLei Wen 363af62a557SLei Wen clk |= SDHCI_CLOCK_CARD_EN; 364af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 365af62a557SLei Wen return 0; 366af62a557SLei Wen } 367af62a557SLei Wen 368af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power) 369af62a557SLei Wen { 370af62a557SLei Wen u8 pwr = 0; 371af62a557SLei Wen 372af62a557SLei Wen if (power != (unsigned short)-1) { 373af62a557SLei Wen switch (1 << power) { 374af62a557SLei Wen case MMC_VDD_165_195: 375af62a557SLei Wen pwr = SDHCI_POWER_180; 376af62a557SLei Wen break; 377af62a557SLei Wen case MMC_VDD_29_30: 378af62a557SLei Wen case MMC_VDD_30_31: 379af62a557SLei Wen pwr = SDHCI_POWER_300; 380af62a557SLei Wen break; 381af62a557SLei Wen case MMC_VDD_32_33: 382af62a557SLei Wen case MMC_VDD_33_34: 383af62a557SLei Wen pwr = SDHCI_POWER_330; 384af62a557SLei Wen break; 385af62a557SLei Wen } 386af62a557SLei Wen } 387af62a557SLei Wen 388af62a557SLei Wen if (pwr == 0) { 389af62a557SLei Wen sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); 390af62a557SLei Wen return; 391af62a557SLei Wen } 392af62a557SLei Wen 393688c2d14SMela Custodio if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) 394688c2d14SMela Custodio sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 395688c2d14SMela Custodio 396af62a557SLei Wen pwr |= SDHCI_POWER_ON; 397af62a557SLei Wen 398af62a557SLei Wen sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 399af62a557SLei Wen } 400af62a557SLei Wen 401ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 402ef1e4edaSSimon Glass static int sdhci_set_ios(struct udevice *dev) 403ef1e4edaSSimon Glass { 404ef1e4edaSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 405ef1e4edaSSimon Glass #else 4066588c78bSJeroen Hofstee static void sdhci_set_ios(struct mmc *mmc) 407af62a557SLei Wen { 408ef1e4edaSSimon Glass #endif 409af62a557SLei Wen u32 ctrl; 41093bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 411af62a557SLei Wen 412236bfecfSJaehoon Chung if (host->set_control_reg) 413236bfecfSJaehoon Chung host->set_control_reg(host); 414236bfecfSJaehoon Chung 415af62a557SLei Wen if (mmc->clock != host->clock) 416af62a557SLei Wen sdhci_set_clock(mmc, mmc->clock); 417af62a557SLei Wen 418af62a557SLei Wen /* Set bus width */ 419af62a557SLei Wen ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 420af62a557SLei Wen if (mmc->bus_width == 8) { 421af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 422113e5dfcSJaehoon Chung if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) || 423113e5dfcSJaehoon Chung (host->quirks & SDHCI_QUIRK_USE_WIDE8)) 424af62a557SLei Wen ctrl |= SDHCI_CTRL_8BITBUS; 425af62a557SLei Wen } else { 426f88a429fSMatt Reimer if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) || 427f88a429fSMatt Reimer (host->quirks & SDHCI_QUIRK_USE_WIDE8)) 428af62a557SLei Wen ctrl &= ~SDHCI_CTRL_8BITBUS; 429af62a557SLei Wen if (mmc->bus_width == 4) 430af62a557SLei Wen ctrl |= SDHCI_CTRL_4BITBUS; 431af62a557SLei Wen else 432af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 433af62a557SLei Wen } 434af62a557SLei Wen 435af62a557SLei Wen if (mmc->clock > 26000000) 436af62a557SLei Wen ctrl |= SDHCI_CTRL_HISPD; 437af62a557SLei Wen else 438af62a557SLei Wen ctrl &= ~SDHCI_CTRL_HISPD; 439af62a557SLei Wen 440236bfecfSJaehoon Chung if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) 441236bfecfSJaehoon Chung ctrl &= ~SDHCI_CTRL_HISPD; 442236bfecfSJaehoon Chung 443af62a557SLei Wen sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 444ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 445ef1e4edaSSimon Glass return 0; 446ef1e4edaSSimon Glass #endif 447af62a557SLei Wen } 448af62a557SLei Wen 4496588c78bSJeroen Hofstee static int sdhci_init(struct mmc *mmc) 450af62a557SLei Wen { 45193bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 452af62a557SLei Wen 453af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) { 454af62a557SLei Wen aligned_buffer = memalign(8, 512*1024); 455af62a557SLei Wen if (!aligned_buffer) { 45630e6d979SDarwin Rambo printf("%s: Aligned buffer alloc failed!!!\n", 45730e6d979SDarwin Rambo __func__); 458af62a557SLei Wen return -1; 459af62a557SLei Wen } 460af62a557SLei Wen } 461af62a557SLei Wen 46293bfd616SPantelis Antoniou sdhci_set_power(host, fls(mmc->cfg->voltages) - 1); 463470dcc75SJoe Hershberger 464470dcc75SJoe Hershberger if (host->quirks & SDHCI_QUIRK_NO_CD) { 465102142c9SAndrei Pistirica #if defined(CONFIG_PIC32_SDHCI) 466102142c9SAndrei Pistirica /* PIC32 SDHCI CD errata: 467102142c9SAndrei Pistirica * - set CD_TEST and clear CD_TEST_INS bit 468102142c9SAndrei Pistirica */ 469102142c9SAndrei Pistirica sdhci_writeb(host, SDHCI_CTRL_CD_TEST, SDHCI_HOST_CONTROL); 470102142c9SAndrei Pistirica #else 471470dcc75SJoe Hershberger unsigned int status; 472470dcc75SJoe Hershberger 473e113fe3cSMatt Reimer sdhci_writeb(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST, 474470dcc75SJoe Hershberger SDHCI_HOST_CONTROL); 475470dcc75SJoe Hershberger 476470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 477470dcc75SJoe Hershberger while ((!(status & SDHCI_CARD_PRESENT)) || 478470dcc75SJoe Hershberger (!(status & SDHCI_CARD_STATE_STABLE)) || 479470dcc75SJoe Hershberger (!(status & SDHCI_CARD_DETECT_PIN_LEVEL))) 480470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 481102142c9SAndrei Pistirica #endif 482470dcc75SJoe Hershberger } 483470dcc75SJoe Hershberger 484ce0c1bc1SŁukasz Majewski /* Enable only interrupts served by the SD controller */ 48530e6d979SDarwin Rambo sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, 48630e6d979SDarwin Rambo SDHCI_INT_ENABLE); 487ce0c1bc1SŁukasz Majewski /* Mask all sdhci interrupt sources */ 488ce0c1bc1SŁukasz Majewski sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE); 489af62a557SLei Wen 490af62a557SLei Wen return 0; 491af62a557SLei Wen } 492af62a557SLei Wen 493ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 494ef1e4edaSSimon Glass int sdhci_probe(struct udevice *dev) 495ef1e4edaSSimon Glass { 496ef1e4edaSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 497ab769f22SPantelis Antoniou 498ef1e4edaSSimon Glass return sdhci_init(mmc); 499ef1e4edaSSimon Glass } 500ef1e4edaSSimon Glass 501ef1e4edaSSimon Glass const struct dm_mmc_ops sdhci_ops = { 502ef1e4edaSSimon Glass .send_cmd = sdhci_send_command, 503ef1e4edaSSimon Glass .set_ios = sdhci_set_ios, 504ef1e4edaSSimon Glass }; 505ef1e4edaSSimon Glass #else 506ab769f22SPantelis Antoniou static const struct mmc_ops sdhci_ops = { 507ab769f22SPantelis Antoniou .send_cmd = sdhci_send_command, 508ab769f22SPantelis Antoniou .set_ios = sdhci_set_ios, 509ab769f22SPantelis Antoniou .init = sdhci_init, 510ab769f22SPantelis Antoniou }; 511ef1e4edaSSimon Glass #endif 512ab769f22SPantelis Antoniou 5132a809093SSimon Glass int sdhci_setup_cfg(struct mmc_config *cfg, const char *name, int buswidth, 5142a809093SSimon Glass uint caps, u32 max_clk, u32 min_clk, uint version, 5152a809093SSimon Glass uint quirks, uint host_caps) 5162a809093SSimon Glass { 5172a809093SSimon Glass cfg->name = name; 5182a809093SSimon Glass #ifndef CONFIG_DM_MMC_OPS 5192a809093SSimon Glass cfg->ops = &sdhci_ops; 5202a809093SSimon Glass #endif 5212a809093SSimon Glass if (max_clk) 5222a809093SSimon Glass cfg->f_max = max_clk; 5232a809093SSimon Glass else { 5242a809093SSimon Glass if (version >= SDHCI_SPEC_300) 5252a809093SSimon Glass cfg->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 5262a809093SSimon Glass SDHCI_CLOCK_BASE_SHIFT; 5272a809093SSimon Glass else 5282a809093SSimon Glass cfg->f_max = (caps & SDHCI_CLOCK_BASE_MASK) >> 5292a809093SSimon Glass SDHCI_CLOCK_BASE_SHIFT; 5302a809093SSimon Glass cfg->f_max *= 1000000; 5312a809093SSimon Glass } 5322a809093SSimon Glass if (cfg->f_max == 0) 5332a809093SSimon Glass return -EINVAL; 5342a809093SSimon Glass if (min_clk) 5352a809093SSimon Glass cfg->f_min = min_clk; 5362a809093SSimon Glass else { 5372a809093SSimon Glass if (version >= SDHCI_SPEC_300) 5382a809093SSimon Glass cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300; 5392a809093SSimon Glass else 5402a809093SSimon Glass cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200; 5412a809093SSimon Glass } 5422a809093SSimon Glass cfg->voltages = 0; 5432a809093SSimon Glass if (caps & SDHCI_CAN_VDD_330) 5442a809093SSimon Glass cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; 5452a809093SSimon Glass if (caps & SDHCI_CAN_VDD_300) 5462a809093SSimon Glass cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; 5472a809093SSimon Glass if (caps & SDHCI_CAN_VDD_180) 5482a809093SSimon Glass cfg->voltages |= MMC_VDD_165_195; 5492a809093SSimon Glass 5502a809093SSimon Glass cfg->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT; 5512a809093SSimon Glass if (version >= SDHCI_SPEC_300) { 5522a809093SSimon Glass if (caps & SDHCI_CAN_DO_8BIT) 5532a809093SSimon Glass cfg->host_caps |= MMC_MODE_8BIT; 5542a809093SSimon Glass } 5552a809093SSimon Glass 5562a809093SSimon Glass if (quirks & SDHCI_QUIRK_NO_HISPD_BIT) 5572a809093SSimon Glass cfg->host_caps &= ~(MMC_MODE_HS | MMC_MODE_HS_52MHz); 5582a809093SSimon Glass 5592a809093SSimon Glass if (host_caps) 5602a809093SSimon Glass cfg->host_caps |= host_caps; 5612a809093SSimon Glass 562ef1e4edaSSimon Glass 5632a809093SSimon Glass cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 5642a809093SSimon Glass 5652a809093SSimon Glass return 0; 5662a809093SSimon Glass } 5672a809093SSimon Glass 568ef1e4edaSSimon Glass #ifdef CONFIG_BLK 569ef1e4edaSSimon Glass int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) 570ef1e4edaSSimon Glass { 571ef1e4edaSSimon Glass return mmc_bind(dev, mmc, cfg); 572ef1e4edaSSimon Glass } 573ef1e4edaSSimon Glass #else 574af62a557SLei Wen int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk) 575af62a557SLei Wen { 576af62a557SLei Wen unsigned int caps; 577af62a557SLei Wen 578af62a557SLei Wen caps = sdhci_readl(host, SDHCI_CAPABILITIES); 579af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 580af62a557SLei Wen if (!(caps & SDHCI_CAN_DO_SDMA)) { 58130e6d979SDarwin Rambo printf("%s: Your controller doesn't support SDMA!!\n", 58230e6d979SDarwin Rambo __func__); 583af62a557SLei Wen return -1; 584af62a557SLei Wen } 585af62a557SLei Wen #endif 586af62a557SLei Wen 5872a809093SSimon Glass if (sdhci_setup_cfg(&host->cfg, host->name, host->bus_width, caps, 5882a809093SSimon Glass max_clk, min_clk, SDHCI_GET_VERSION(host), 5892a809093SSimon Glass host->quirks, host->host_caps)) { 59030e6d979SDarwin Rambo printf("%s: Hardware doesn't specify base clock frequency\n", 59130e6d979SDarwin Rambo __func__); 5922a809093SSimon Glass return -EINVAL; 593af62a557SLei Wen } 594236bfecfSJaehoon Chung 595236bfecfSJaehoon Chung if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE) 59693bfd616SPantelis Antoniou host->cfg.voltages |= host->voltages; 597236bfecfSJaehoon Chung 598af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_ALL); 59993bfd616SPantelis Antoniou 60093bfd616SPantelis Antoniou host->mmc = mmc_create(&host->cfg, host); 60193bfd616SPantelis Antoniou if (host->mmc == NULL) { 60293bfd616SPantelis Antoniou printf("%s: mmc create fail!\n", __func__); 60393bfd616SPantelis Antoniou return -1; 60493bfd616SPantelis Antoniou } 605af62a557SLei Wen 606af62a557SLei Wen return 0; 607af62a557SLei Wen } 608ef1e4edaSSimon Glass #endif 609