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"); 173915ffa52SJaehoon Chung return -ECOMM; 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; 18717ea3c86SJaehoon Chung if (data) 188af62a557SLei Wen mask |= SDHCI_INT_DATA_END; 189af62a557SLei Wen } else 190af62a557SLei Wen flags = SDHCI_CMD_RESP_SHORT; 191af62a557SLei Wen 192af62a557SLei Wen if (cmd->resp_type & MMC_RSP_CRC) 193af62a557SLei Wen flags |= SDHCI_CMD_CRC; 194af62a557SLei Wen if (cmd->resp_type & MMC_RSP_OPCODE) 195af62a557SLei Wen flags |= SDHCI_CMD_INDEX; 196af62a557SLei Wen if (data) 197af62a557SLei Wen flags |= SDHCI_CMD_DATA; 198af62a557SLei Wen 199af62a557SLei Wen /* Set Transfer mode regarding to data flag */ 200af62a557SLei Wen if (data != 0) { 201af62a557SLei Wen sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); 202af62a557SLei Wen mode = SDHCI_TRNS_BLK_CNT_EN; 203af62a557SLei Wen trans_bytes = data->blocks * data->blocksize; 204af62a557SLei Wen if (data->blocks > 1) 205af62a557SLei Wen mode |= SDHCI_TRNS_MULTI; 206af62a557SLei Wen 207af62a557SLei Wen if (data->flags == MMC_DATA_READ) 208af62a557SLei Wen mode |= SDHCI_TRNS_READ; 209af62a557SLei Wen 210af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 211af62a557SLei Wen if (data->flags == MMC_DATA_READ) 2123c1fcb77SRob Herring start_addr = (unsigned long)data->dest; 213af62a557SLei Wen else 2143c1fcb77SRob Herring start_addr = (unsigned long)data->src; 215af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 216af62a557SLei Wen (start_addr & 0x7) != 0x0) { 217af62a557SLei Wen is_aligned = 0; 2183c1fcb77SRob Herring start_addr = (unsigned long)aligned_buffer; 219af62a557SLei Wen if (data->flags != MMC_DATA_READ) 220af62a557SLei Wen memcpy(aligned_buffer, data->src, trans_bytes); 221af62a557SLei Wen } 222af62a557SLei Wen 223492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER) 224492d3223SStefan Roese /* 225492d3223SStefan Roese * Always use this bounce-buffer when 226492d3223SStefan Roese * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined 227492d3223SStefan Roese */ 228492d3223SStefan Roese is_aligned = 0; 229492d3223SStefan Roese start_addr = (unsigned long)aligned_buffer; 230492d3223SStefan Roese if (data->flags != MMC_DATA_READ) 231492d3223SStefan Roese memcpy(aligned_buffer, data->src, trans_bytes); 232492d3223SStefan Roese #endif 233492d3223SStefan Roese 234af62a557SLei Wen sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS); 235af62a557SLei Wen mode |= SDHCI_TRNS_DMA; 236af62a557SLei Wen #endif 237af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 238af62a557SLei Wen data->blocksize), 239af62a557SLei Wen SDHCI_BLOCK_SIZE); 240af62a557SLei Wen sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); 241af62a557SLei Wen sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); 2425e1c23cdSKevin Liu } else if (cmd->resp_type & MMC_RSP_BUSY) { 2435e1c23cdSKevin Liu sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); 244af62a557SLei Wen } 245af62a557SLei Wen 246af62a557SLei Wen sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT); 247af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 2482c2ec4c9SLei Wen flush_cache(start_addr, trans_bytes); 249af62a557SLei Wen #endif 250af62a557SLei Wen sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND); 25129905a45SStefan Roese start = get_timer(0); 252af62a557SLei Wen do { 253af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 254af62a557SLei Wen if (stat & SDHCI_INT_ERROR) 255af62a557SLei Wen break; 256af62a557SLei Wen 257d90bb439SSteve Rae if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) { 258bae4a1fdSMasahiro Yamada if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) { 2593a638320SJaehoon Chung return 0; 260bae4a1fdSMasahiro Yamada } else { 261bae4a1fdSMasahiro Yamada printf("%s: Timeout for status update!\n", 262bae4a1fdSMasahiro Yamada __func__); 263915ffa52SJaehoon Chung return -ETIMEDOUT; 2643a638320SJaehoon Chung } 2653a638320SJaehoon Chung } 266bae4a1fdSMasahiro Yamada } while ((stat & mask) != mask); 2673a638320SJaehoon Chung 268af62a557SLei Wen if ((stat & (SDHCI_INT_ERROR | mask)) == mask) { 269af62a557SLei Wen sdhci_cmd_done(host, cmd); 270af62a557SLei Wen sdhci_writel(host, mask, SDHCI_INT_STATUS); 271af62a557SLei Wen } else 272af62a557SLei Wen ret = -1; 273af62a557SLei Wen 274af62a557SLei Wen if (!ret && data) 275af62a557SLei Wen ret = sdhci_transfer_data(host, data, start_addr); 276af62a557SLei Wen 27713243f2eSTushar Behera if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD) 27813243f2eSTushar Behera udelay(1000); 27913243f2eSTushar Behera 280af62a557SLei Wen stat = sdhci_readl(host, SDHCI_INT_STATUS); 281af62a557SLei Wen sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS); 282af62a557SLei Wen if (!ret) { 283af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 284af62a557SLei Wen !is_aligned && (data->flags == MMC_DATA_READ)) 285af62a557SLei Wen memcpy(data->dest, aligned_buffer, trans_bytes); 286af62a557SLei Wen return 0; 287af62a557SLei Wen } 288af62a557SLei Wen 289af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_CMD); 290af62a557SLei Wen sdhci_reset(host, SDHCI_RESET_DATA); 291af62a557SLei Wen if (stat & SDHCI_INT_TIMEOUT) 292915ffa52SJaehoon Chung return -ETIMEDOUT; 293af62a557SLei Wen else 294915ffa52SJaehoon Chung return -ECOMM; 295af62a557SLei Wen } 296af62a557SLei Wen 297af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock) 298af62a557SLei Wen { 29993bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 30079667b7bSWenyou Yang unsigned int div, clk, timeout, reg; 301af62a557SLei Wen 30279667b7bSWenyou Yang /* Wait max 20 ms */ 30379667b7bSWenyou Yang timeout = 200; 30479667b7bSWenyou Yang while (sdhci_readl(host, SDHCI_PRESENT_STATE) & 30579667b7bSWenyou Yang (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) { 30679667b7bSWenyou Yang if (timeout == 0) { 30779667b7bSWenyou Yang printf("%s: Timeout to wait cmd & data inhibit\n", 30879667b7bSWenyou Yang __func__); 30979667b7bSWenyou Yang return -1; 31079667b7bSWenyou Yang } 31179667b7bSWenyou Yang 31279667b7bSWenyou Yang timeout--; 31379667b7bSWenyou Yang udelay(100); 31479667b7bSWenyou Yang } 31579667b7bSWenyou Yang 31679667b7bSWenyou Yang reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 3171d405e20SSiva Durga Prasad Paladugu reg &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN); 31879667b7bSWenyou Yang sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL); 319af62a557SLei Wen 320af62a557SLei Wen if (clock == 0) 321af62a557SLei Wen return 0; 322af62a557SLei Wen 323113e5dfcSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) { 324af62a557SLei Wen /* Version 3.00 divisors must be a multiple of 2. */ 32593bfd616SPantelis Antoniou if (mmc->cfg->f_max <= clock) 326af62a557SLei Wen div = 1; 327af62a557SLei Wen else { 328af62a557SLei Wen for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) { 32993bfd616SPantelis Antoniou if ((mmc->cfg->f_max / div) <= clock) 330af62a557SLei Wen break; 331af62a557SLei Wen } 332af62a557SLei Wen } 333af62a557SLei Wen } else { 334af62a557SLei Wen /* Version 2.00 divisors must be a power of 2. */ 335af62a557SLei Wen for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) { 33693bfd616SPantelis Antoniou if ((mmc->cfg->f_max / div) <= clock) 337af62a557SLei Wen break; 338af62a557SLei Wen } 339af62a557SLei Wen } 340af62a557SLei Wen div >>= 1; 341af62a557SLei Wen 342b09ed6e4SJaehoon Chung if (host->set_clock) 343b09ed6e4SJaehoon Chung host->set_clock(host->index, div); 344b09ed6e4SJaehoon Chung 345af62a557SLei Wen clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; 346af62a557SLei Wen clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) 347af62a557SLei Wen << SDHCI_DIVIDER_HI_SHIFT; 348af62a557SLei Wen clk |= SDHCI_CLOCK_INT_EN; 349af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 350af62a557SLei Wen 351af62a557SLei Wen /* Wait max 20 ms */ 352af62a557SLei Wen timeout = 20; 353af62a557SLei Wen while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) 354af62a557SLei Wen & SDHCI_CLOCK_INT_STABLE)) { 355af62a557SLei Wen if (timeout == 0) { 35630e6d979SDarwin Rambo printf("%s: Internal clock never stabilised.\n", 35730e6d979SDarwin Rambo __func__); 358af62a557SLei Wen return -1; 359af62a557SLei Wen } 360af62a557SLei Wen timeout--; 361af62a557SLei Wen udelay(1000); 362af62a557SLei Wen } 363af62a557SLei Wen 364af62a557SLei Wen clk |= SDHCI_CLOCK_CARD_EN; 365af62a557SLei Wen sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 366af62a557SLei Wen return 0; 367af62a557SLei Wen } 368af62a557SLei Wen 369af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power) 370af62a557SLei Wen { 371af62a557SLei Wen u8 pwr = 0; 372af62a557SLei Wen 373af62a557SLei Wen if (power != (unsigned short)-1) { 374af62a557SLei Wen switch (1 << power) { 375af62a557SLei Wen case MMC_VDD_165_195: 376af62a557SLei Wen pwr = SDHCI_POWER_180; 377af62a557SLei Wen break; 378af62a557SLei Wen case MMC_VDD_29_30: 379af62a557SLei Wen case MMC_VDD_30_31: 380af62a557SLei Wen pwr = SDHCI_POWER_300; 381af62a557SLei Wen break; 382af62a557SLei Wen case MMC_VDD_32_33: 383af62a557SLei Wen case MMC_VDD_33_34: 384af62a557SLei Wen pwr = SDHCI_POWER_330; 385af62a557SLei Wen break; 386af62a557SLei Wen } 387af62a557SLei Wen } 388af62a557SLei Wen 389af62a557SLei Wen if (pwr == 0) { 390af62a557SLei Wen sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); 391af62a557SLei Wen return; 392af62a557SLei Wen } 393af62a557SLei Wen 394688c2d14SMela Custodio if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) 395688c2d14SMela Custodio sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 396688c2d14SMela Custodio 397af62a557SLei Wen pwr |= SDHCI_POWER_ON; 398af62a557SLei Wen 399af62a557SLei Wen sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); 400af62a557SLei Wen } 401af62a557SLei Wen 402ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 403ef1e4edaSSimon Glass static int sdhci_set_ios(struct udevice *dev) 404ef1e4edaSSimon Glass { 405ef1e4edaSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 406ef1e4edaSSimon Glass #else 4076588c78bSJeroen Hofstee static void sdhci_set_ios(struct mmc *mmc) 408af62a557SLei Wen { 409ef1e4edaSSimon Glass #endif 410af62a557SLei Wen u32 ctrl; 41193bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 412af62a557SLei Wen 413236bfecfSJaehoon Chung if (host->set_control_reg) 414236bfecfSJaehoon Chung host->set_control_reg(host); 415236bfecfSJaehoon Chung 416af62a557SLei Wen if (mmc->clock != host->clock) 417af62a557SLei Wen sdhci_set_clock(mmc, mmc->clock); 418af62a557SLei Wen 419af62a557SLei Wen /* Set bus width */ 420af62a557SLei Wen ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 421af62a557SLei Wen if (mmc->bus_width == 8) { 422af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 423113e5dfcSJaehoon Chung if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) || 424113e5dfcSJaehoon Chung (host->quirks & SDHCI_QUIRK_USE_WIDE8)) 425af62a557SLei Wen ctrl |= SDHCI_CTRL_8BITBUS; 426af62a557SLei Wen } else { 427f88a429fSMatt Reimer if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) || 428f88a429fSMatt Reimer (host->quirks & SDHCI_QUIRK_USE_WIDE8)) 429af62a557SLei Wen ctrl &= ~SDHCI_CTRL_8BITBUS; 430af62a557SLei Wen if (mmc->bus_width == 4) 431af62a557SLei Wen ctrl |= SDHCI_CTRL_4BITBUS; 432af62a557SLei Wen else 433af62a557SLei Wen ctrl &= ~SDHCI_CTRL_4BITBUS; 434af62a557SLei Wen } 435af62a557SLei Wen 436af62a557SLei Wen if (mmc->clock > 26000000) 437af62a557SLei Wen ctrl |= SDHCI_CTRL_HISPD; 438af62a557SLei Wen else 439af62a557SLei Wen ctrl &= ~SDHCI_CTRL_HISPD; 440af62a557SLei Wen 441236bfecfSJaehoon Chung if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) 442236bfecfSJaehoon Chung ctrl &= ~SDHCI_CTRL_HISPD; 443236bfecfSJaehoon Chung 444af62a557SLei Wen sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 445ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 446ef1e4edaSSimon Glass return 0; 447ef1e4edaSSimon Glass #endif 448af62a557SLei Wen } 449af62a557SLei Wen 4506588c78bSJeroen Hofstee static int sdhci_init(struct mmc *mmc) 451af62a557SLei Wen { 45293bfd616SPantelis Antoniou struct sdhci_host *host = mmc->priv; 453af62a557SLei Wen 454af62a557SLei Wen if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) { 455af62a557SLei Wen aligned_buffer = memalign(8, 512*1024); 456af62a557SLei Wen if (!aligned_buffer) { 45730e6d979SDarwin Rambo printf("%s: Aligned buffer alloc failed!!!\n", 45830e6d979SDarwin Rambo __func__); 459af62a557SLei Wen return -1; 460af62a557SLei Wen } 461af62a557SLei Wen } 462af62a557SLei Wen 46393bfd616SPantelis Antoniou sdhci_set_power(host, fls(mmc->cfg->voltages) - 1); 464470dcc75SJoe Hershberger 465470dcc75SJoe Hershberger if (host->quirks & SDHCI_QUIRK_NO_CD) { 466102142c9SAndrei Pistirica #if defined(CONFIG_PIC32_SDHCI) 467102142c9SAndrei Pistirica /* PIC32 SDHCI CD errata: 468102142c9SAndrei Pistirica * - set CD_TEST and clear CD_TEST_INS bit 469102142c9SAndrei Pistirica */ 470102142c9SAndrei Pistirica sdhci_writeb(host, SDHCI_CTRL_CD_TEST, SDHCI_HOST_CONTROL); 471102142c9SAndrei Pistirica #else 472470dcc75SJoe Hershberger unsigned int status; 473470dcc75SJoe Hershberger 474e113fe3cSMatt Reimer sdhci_writeb(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST, 475470dcc75SJoe Hershberger SDHCI_HOST_CONTROL); 476470dcc75SJoe Hershberger 477470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 478470dcc75SJoe Hershberger while ((!(status & SDHCI_CARD_PRESENT)) || 479470dcc75SJoe Hershberger (!(status & SDHCI_CARD_STATE_STABLE)) || 480470dcc75SJoe Hershberger (!(status & SDHCI_CARD_DETECT_PIN_LEVEL))) 481470dcc75SJoe Hershberger status = sdhci_readl(host, SDHCI_PRESENT_STATE); 482102142c9SAndrei Pistirica #endif 483470dcc75SJoe Hershberger } 484470dcc75SJoe Hershberger 485ce0c1bc1SŁukasz Majewski /* Enable only interrupts served by the SD controller */ 48630e6d979SDarwin Rambo sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, 48730e6d979SDarwin Rambo SDHCI_INT_ENABLE); 488ce0c1bc1SŁukasz Majewski /* Mask all sdhci interrupt sources */ 489ce0c1bc1SŁukasz Majewski sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE); 490af62a557SLei Wen 491af62a557SLei Wen return 0; 492af62a557SLei Wen } 493af62a557SLei Wen 494ef1e4edaSSimon Glass #ifdef CONFIG_DM_MMC_OPS 495ef1e4edaSSimon Glass int sdhci_probe(struct udevice *dev) 496ef1e4edaSSimon Glass { 497ef1e4edaSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 498ab769f22SPantelis Antoniou 499ef1e4edaSSimon Glass return sdhci_init(mmc); 500ef1e4edaSSimon Glass } 501ef1e4edaSSimon Glass 502ef1e4edaSSimon Glass const struct dm_mmc_ops sdhci_ops = { 503ef1e4edaSSimon Glass .send_cmd = sdhci_send_command, 504ef1e4edaSSimon Glass .set_ios = sdhci_set_ios, 505ef1e4edaSSimon Glass }; 506ef1e4edaSSimon Glass #else 507ab769f22SPantelis Antoniou static const struct mmc_ops sdhci_ops = { 508ab769f22SPantelis Antoniou .send_cmd = sdhci_send_command, 509ab769f22SPantelis Antoniou .set_ios = sdhci_set_ios, 510ab769f22SPantelis Antoniou .init = sdhci_init, 511ab769f22SPantelis Antoniou }; 512ef1e4edaSSimon Glass #endif 513ab769f22SPantelis Antoniou 514*14bed52dSJaehoon Chung int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host, 515*14bed52dSJaehoon Chung u32 max_clk, u32 min_clk) 5162a809093SSimon Glass { 517*14bed52dSJaehoon Chung u32 caps; 518*14bed52dSJaehoon Chung 519*14bed52dSJaehoon Chung caps = sdhci_readl(host, SDHCI_CAPABILITIES); 520*14bed52dSJaehoon Chung host->version = sdhci_readw(host, SDHCI_HOST_VERSION); 521*14bed52dSJaehoon Chung 522*14bed52dSJaehoon Chung cfg->name = host->name; 5232a809093SSimon Glass #ifndef CONFIG_DM_MMC_OPS 5242a809093SSimon Glass cfg->ops = &sdhci_ops; 5252a809093SSimon Glass #endif 5262a809093SSimon Glass if (max_clk) 5272a809093SSimon Glass cfg->f_max = max_clk; 5282a809093SSimon Glass else { 529*14bed52dSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) 5302a809093SSimon Glass cfg->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 5312a809093SSimon Glass SDHCI_CLOCK_BASE_SHIFT; 5322a809093SSimon Glass else 5332a809093SSimon Glass cfg->f_max = (caps & SDHCI_CLOCK_BASE_MASK) >> 5342a809093SSimon Glass SDHCI_CLOCK_BASE_SHIFT; 5352a809093SSimon Glass cfg->f_max *= 1000000; 5362a809093SSimon Glass } 5372a809093SSimon Glass if (cfg->f_max == 0) 5382a809093SSimon Glass return -EINVAL; 5392a809093SSimon Glass if (min_clk) 5402a809093SSimon Glass cfg->f_min = min_clk; 5412a809093SSimon Glass else { 542*14bed52dSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) 5432a809093SSimon Glass cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300; 5442a809093SSimon Glass else 5452a809093SSimon Glass cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200; 5462a809093SSimon Glass } 5472a809093SSimon Glass cfg->voltages = 0; 5482a809093SSimon Glass if (caps & SDHCI_CAN_VDD_330) 5492a809093SSimon Glass cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; 5502a809093SSimon Glass if (caps & SDHCI_CAN_VDD_300) 5512a809093SSimon Glass cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; 5522a809093SSimon Glass if (caps & SDHCI_CAN_VDD_180) 5532a809093SSimon Glass cfg->voltages |= MMC_VDD_165_195; 5542a809093SSimon Glass 5552a809093SSimon Glass cfg->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT; 556*14bed52dSJaehoon Chung if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) { 5572a809093SSimon Glass if (caps & SDHCI_CAN_DO_8BIT) 5582a809093SSimon Glass cfg->host_caps |= MMC_MODE_8BIT; 5592a809093SSimon Glass } 5602a809093SSimon Glass 561*14bed52dSJaehoon Chung if (host->host_caps) 562*14bed52dSJaehoon Chung cfg->host_caps |= host->host_caps; 5632a809093SSimon Glass 564ef1e4edaSSimon Glass 5652a809093SSimon Glass cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 5662a809093SSimon Glass 5672a809093SSimon Glass return 0; 5682a809093SSimon Glass } 5692a809093SSimon Glass 570ef1e4edaSSimon Glass #ifdef CONFIG_BLK 571ef1e4edaSSimon Glass int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) 572ef1e4edaSSimon Glass { 573ef1e4edaSSimon Glass return mmc_bind(dev, mmc, cfg); 574ef1e4edaSSimon Glass } 575ef1e4edaSSimon Glass #else 576af62a557SLei Wen int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk) 577af62a557SLei Wen { 578af62a557SLei Wen unsigned int caps; 579af62a557SLei Wen 580af62a557SLei Wen caps = sdhci_readl(host, SDHCI_CAPABILITIES); 581af62a557SLei Wen #ifdef CONFIG_MMC_SDMA 582af62a557SLei Wen if (!(caps & SDHCI_CAN_DO_SDMA)) { 58330e6d979SDarwin Rambo printf("%s: Your controller doesn't support SDMA!!\n", 58430e6d979SDarwin Rambo __func__); 585af62a557SLei Wen return -1; 586af62a557SLei Wen } 587af62a557SLei Wen #endif 588af62a557SLei Wen 589*14bed52dSJaehoon Chung if (sdhci_setup_cfg(&host->cfg, host, max_clk, min_clk)) { 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