1f95f3850SWill Newton /* 2f95f3850SWill Newton * Synopsys DesignWare Multimedia Card Interface driver 3f95f3850SWill Newton * (Based on NXP driver for lpc 31xx) 4f95f3850SWill Newton * 5f95f3850SWill Newton * Copyright (C) 2009 NXP Semiconductors 6f95f3850SWill Newton * Copyright (C) 2009, 2010 Imagination Technologies Ltd. 7f95f3850SWill Newton * 8f95f3850SWill Newton * This program is free software; you can redistribute it and/or modify 9f95f3850SWill Newton * it under the terms of the GNU General Public License as published by 10f95f3850SWill Newton * the Free Software Foundation; either version 2 of the License, or 11f95f3850SWill Newton * (at your option) any later version. 12f95f3850SWill Newton */ 13f95f3850SWill Newton 14f95f3850SWill Newton #include <linux/blkdev.h> 15f95f3850SWill Newton #include <linux/clk.h> 16f95f3850SWill Newton #include <linux/debugfs.h> 17f95f3850SWill Newton #include <linux/device.h> 18f95f3850SWill Newton #include <linux/dma-mapping.h> 19f95f3850SWill Newton #include <linux/err.h> 20f95f3850SWill Newton #include <linux/init.h> 21f95f3850SWill Newton #include <linux/interrupt.h> 22f95f3850SWill Newton #include <linux/ioport.h> 23f95f3850SWill Newton #include <linux/module.h> 24f95f3850SWill Newton #include <linux/platform_device.h> 25f95f3850SWill Newton #include <linux/seq_file.h> 26f95f3850SWill Newton #include <linux/slab.h> 27f95f3850SWill Newton #include <linux/stat.h> 28f95f3850SWill Newton #include <linux/delay.h> 29f95f3850SWill Newton #include <linux/irq.h> 30b24c8b26SDoug Anderson #include <linux/mmc/card.h> 31f95f3850SWill Newton #include <linux/mmc/host.h> 32f95f3850SWill Newton #include <linux/mmc/mmc.h> 3301730558SDoug Anderson #include <linux/mmc/sd.h> 3490c2143aSSeungwon Jeon #include <linux/mmc/sdio.h> 35f95f3850SWill Newton #include <linux/mmc/dw_mmc.h> 36f95f3850SWill Newton #include <linux/bitops.h> 37c07946a3SJaehoon Chung #include <linux/regulator/consumer.h> 38c91eab4bSThomas Abraham #include <linux/of.h> 3955a6ceb2SDoug Anderson #include <linux/of_gpio.h> 40bf626e55SZhangfei Gao #include <linux/mmc/slot-gpio.h> 41f95f3850SWill Newton 42f95f3850SWill Newton #include "dw_mmc.h" 43f95f3850SWill Newton 44f95f3850SWill Newton /* Common flag combinations */ 453f7eec62SJaehoon Chung #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \ 46f95f3850SWill Newton SDMMC_INT_HTO | SDMMC_INT_SBE | \ 477a3c5677SDoug Anderson SDMMC_INT_EBE | SDMMC_INT_HLE) 48f95f3850SWill Newton #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \ 497a3c5677SDoug Anderson SDMMC_INT_RESP_ERR | SDMMC_INT_HLE) 50f95f3850SWill Newton #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \ 517a3c5677SDoug Anderson DW_MCI_CMD_ERROR_FLAGS) 52f95f3850SWill Newton #define DW_MCI_SEND_STATUS 1 53f95f3850SWill Newton #define DW_MCI_RECV_STATUS 2 54f95f3850SWill Newton #define DW_MCI_DMA_THRESHOLD 16 55f95f3850SWill Newton 561f44a2a5SSeungwon Jeon #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */ 571f44a2a5SSeungwon Jeon #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */ 581f44a2a5SSeungwon Jeon 59fc79a4d6SJoonyoung Shim #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \ 60fc79a4d6SJoonyoung Shim SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \ 61fc79a4d6SJoonyoung Shim SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \ 62fc79a4d6SJoonyoung Shim SDMMC_IDMAC_INT_TI) 63fc79a4d6SJoonyoung Shim 6469d99fdcSPrabu Thangamuthu struct idmac_desc_64addr { 6569d99fdcSPrabu Thangamuthu u32 des0; /* Control Descriptor */ 6669d99fdcSPrabu Thangamuthu 6769d99fdcSPrabu Thangamuthu u32 des1; /* Reserved */ 6869d99fdcSPrabu Thangamuthu 6969d99fdcSPrabu Thangamuthu u32 des2; /*Buffer sizes */ 7069d99fdcSPrabu Thangamuthu #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \ 716687c42fSBen Dooks ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \ 726687c42fSBen Dooks ((cpu_to_le32(s)) & cpu_to_le32(0x1fff))) 7369d99fdcSPrabu Thangamuthu 7469d99fdcSPrabu Thangamuthu u32 des3; /* Reserved */ 7569d99fdcSPrabu Thangamuthu 7669d99fdcSPrabu Thangamuthu u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/ 7769d99fdcSPrabu Thangamuthu u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/ 7869d99fdcSPrabu Thangamuthu 7969d99fdcSPrabu Thangamuthu u32 des6; /* Lower 32-bits of Next Descriptor Address */ 8069d99fdcSPrabu Thangamuthu u32 des7; /* Upper 32-bits of Next Descriptor Address */ 8169d99fdcSPrabu Thangamuthu }; 8269d99fdcSPrabu Thangamuthu 83f95f3850SWill Newton struct idmac_desc { 846687c42fSBen Dooks __le32 des0; /* Control Descriptor */ 85f95f3850SWill Newton #define IDMAC_DES0_DIC BIT(1) 86f95f3850SWill Newton #define IDMAC_DES0_LD BIT(2) 87f95f3850SWill Newton #define IDMAC_DES0_FD BIT(3) 88f95f3850SWill Newton #define IDMAC_DES0_CH BIT(4) 89f95f3850SWill Newton #define IDMAC_DES0_ER BIT(5) 90f95f3850SWill Newton #define IDMAC_DES0_CES BIT(30) 91f95f3850SWill Newton #define IDMAC_DES0_OWN BIT(31) 92f95f3850SWill Newton 936687c42fSBen Dooks __le32 des1; /* Buffer sizes */ 94f95f3850SWill Newton #define IDMAC_SET_BUFFER1_SIZE(d, s) \ 95e5306c3aSBen Dooks ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff))) 96f95f3850SWill Newton 976687c42fSBen Dooks __le32 des2; /* buffer 1 physical address */ 98f95f3850SWill Newton 996687c42fSBen Dooks __le32 des3; /* buffer 2 physical address */ 100f95f3850SWill Newton }; 1015959b32eSAlexey Brodkin 1025959b32eSAlexey Brodkin /* Each descriptor can transfer up to 4KB of data in chained mode */ 1035959b32eSAlexey Brodkin #define DW_MCI_DESC_DATA_LENGTH 0x1000 104f95f3850SWill Newton 1053a33a94cSSonny Rao static bool dw_mci_reset(struct dw_mci *host); 106536f6b91SSonny Rao static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset); 1070bdbd0e8SDoug Anderson static int dw_mci_card_busy(struct mmc_host *mmc); 10856f6911cSShawn Lin static int dw_mci_get_cd(struct mmc_host *mmc); 10931bff450SSeungwon Jeon 110f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS) 111f95f3850SWill Newton static int dw_mci_req_show(struct seq_file *s, void *v) 112f95f3850SWill Newton { 113f95f3850SWill Newton struct dw_mci_slot *slot = s->private; 114f95f3850SWill Newton struct mmc_request *mrq; 115f95f3850SWill Newton struct mmc_command *cmd; 116f95f3850SWill Newton struct mmc_command *stop; 117f95f3850SWill Newton struct mmc_data *data; 118f95f3850SWill Newton 119f95f3850SWill Newton /* Make sure we get a consistent snapshot */ 120f95f3850SWill Newton spin_lock_bh(&slot->host->lock); 121f95f3850SWill Newton mrq = slot->mrq; 122f95f3850SWill Newton 123f95f3850SWill Newton if (mrq) { 124f95f3850SWill Newton cmd = mrq->cmd; 125f95f3850SWill Newton data = mrq->data; 126f95f3850SWill Newton stop = mrq->stop; 127f95f3850SWill Newton 128f95f3850SWill Newton if (cmd) 129f95f3850SWill Newton seq_printf(s, 130f95f3850SWill Newton "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", 131f95f3850SWill Newton cmd->opcode, cmd->arg, cmd->flags, 132f95f3850SWill Newton cmd->resp[0], cmd->resp[1], cmd->resp[2], 133f95f3850SWill Newton cmd->resp[2], cmd->error); 134f95f3850SWill Newton if (data) 135f95f3850SWill Newton seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", 136f95f3850SWill Newton data->bytes_xfered, data->blocks, 137f95f3850SWill Newton data->blksz, data->flags, data->error); 138f95f3850SWill Newton if (stop) 139f95f3850SWill Newton seq_printf(s, 140f95f3850SWill Newton "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", 141f95f3850SWill Newton stop->opcode, stop->arg, stop->flags, 142f95f3850SWill Newton stop->resp[0], stop->resp[1], stop->resp[2], 143f95f3850SWill Newton stop->resp[2], stop->error); 144f95f3850SWill Newton } 145f95f3850SWill Newton 146f95f3850SWill Newton spin_unlock_bh(&slot->host->lock); 147f95f3850SWill Newton 148f95f3850SWill Newton return 0; 149f95f3850SWill Newton } 150f95f3850SWill Newton 151f95f3850SWill Newton static int dw_mci_req_open(struct inode *inode, struct file *file) 152f95f3850SWill Newton { 153f95f3850SWill Newton return single_open(file, dw_mci_req_show, inode->i_private); 154f95f3850SWill Newton } 155f95f3850SWill Newton 156f95f3850SWill Newton static const struct file_operations dw_mci_req_fops = { 157f95f3850SWill Newton .owner = THIS_MODULE, 158f95f3850SWill Newton .open = dw_mci_req_open, 159f95f3850SWill Newton .read = seq_read, 160f95f3850SWill Newton .llseek = seq_lseek, 161f95f3850SWill Newton .release = single_release, 162f95f3850SWill Newton }; 163f95f3850SWill Newton 164f95f3850SWill Newton static int dw_mci_regs_show(struct seq_file *s, void *v) 165f95f3850SWill Newton { 166f95f3850SWill Newton seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS); 167f95f3850SWill Newton seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS); 168f95f3850SWill Newton seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD); 169f95f3850SWill Newton seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL); 170f95f3850SWill Newton seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK); 171f95f3850SWill Newton seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA); 172f95f3850SWill Newton 173f95f3850SWill Newton return 0; 174f95f3850SWill Newton } 175f95f3850SWill Newton 176f95f3850SWill Newton static int dw_mci_regs_open(struct inode *inode, struct file *file) 177f95f3850SWill Newton { 178f95f3850SWill Newton return single_open(file, dw_mci_regs_show, inode->i_private); 179f95f3850SWill Newton } 180f95f3850SWill Newton 181f95f3850SWill Newton static const struct file_operations dw_mci_regs_fops = { 182f95f3850SWill Newton .owner = THIS_MODULE, 183f95f3850SWill Newton .open = dw_mci_regs_open, 184f95f3850SWill Newton .read = seq_read, 185f95f3850SWill Newton .llseek = seq_lseek, 186f95f3850SWill Newton .release = single_release, 187f95f3850SWill Newton }; 188f95f3850SWill Newton 189f95f3850SWill Newton static void dw_mci_init_debugfs(struct dw_mci_slot *slot) 190f95f3850SWill Newton { 191f95f3850SWill Newton struct mmc_host *mmc = slot->mmc; 192f95f3850SWill Newton struct dw_mci *host = slot->host; 193f95f3850SWill Newton struct dentry *root; 194f95f3850SWill Newton struct dentry *node; 195f95f3850SWill Newton 196f95f3850SWill Newton root = mmc->debugfs_root; 197f95f3850SWill Newton if (!root) 198f95f3850SWill Newton return; 199f95f3850SWill Newton 200f95f3850SWill Newton node = debugfs_create_file("regs", S_IRUSR, root, host, 201f95f3850SWill Newton &dw_mci_regs_fops); 202f95f3850SWill Newton if (!node) 203f95f3850SWill Newton goto err; 204f95f3850SWill Newton 205f95f3850SWill Newton node = debugfs_create_file("req", S_IRUSR, root, slot, 206f95f3850SWill Newton &dw_mci_req_fops); 207f95f3850SWill Newton if (!node) 208f95f3850SWill Newton goto err; 209f95f3850SWill Newton 210f95f3850SWill Newton node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); 211f95f3850SWill Newton if (!node) 212f95f3850SWill Newton goto err; 213f95f3850SWill Newton 214f95f3850SWill Newton node = debugfs_create_x32("pending_events", S_IRUSR, root, 215f95f3850SWill Newton (u32 *)&host->pending_events); 216f95f3850SWill Newton if (!node) 217f95f3850SWill Newton goto err; 218f95f3850SWill Newton 219f95f3850SWill Newton node = debugfs_create_x32("completed_events", S_IRUSR, root, 220f95f3850SWill Newton (u32 *)&host->completed_events); 221f95f3850SWill Newton if (!node) 222f95f3850SWill Newton goto err; 223f95f3850SWill Newton 224f95f3850SWill Newton return; 225f95f3850SWill Newton 226f95f3850SWill Newton err: 227f95f3850SWill Newton dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); 228f95f3850SWill Newton } 229f95f3850SWill Newton #endif /* defined(CONFIG_DEBUG_FS) */ 230f95f3850SWill Newton 23101730558SDoug Anderson static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg); 23201730558SDoug Anderson 233f95f3850SWill Newton static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd) 234f95f3850SWill Newton { 235f95f3850SWill Newton struct mmc_data *data; 236800d78bfSThomas Abraham struct dw_mci_slot *slot = mmc_priv(mmc); 23701730558SDoug Anderson struct dw_mci *host = slot->host; 238f95f3850SWill Newton u32 cmdr; 239f95f3850SWill Newton 2400e3a22c0SShawn Lin cmd->error = -EINPROGRESS; 241f95f3850SWill Newton cmdr = cmd->opcode; 242f95f3850SWill Newton 24390c2143aSSeungwon Jeon if (cmd->opcode == MMC_STOP_TRANSMISSION || 24490c2143aSSeungwon Jeon cmd->opcode == MMC_GO_IDLE_STATE || 24590c2143aSSeungwon Jeon cmd->opcode == MMC_GO_INACTIVE_STATE || 24690c2143aSSeungwon Jeon (cmd->opcode == SD_IO_RW_DIRECT && 24790c2143aSSeungwon Jeon ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT)) 248f95f3850SWill Newton cmdr |= SDMMC_CMD_STOP; 2494a1b27adSJaehoon Chung else if (cmd->opcode != MMC_SEND_STATUS && cmd->data) 250f95f3850SWill Newton cmdr |= SDMMC_CMD_PRV_DAT_WAIT; 251f95f3850SWill Newton 25201730558SDoug Anderson if (cmd->opcode == SD_SWITCH_VOLTAGE) { 25301730558SDoug Anderson u32 clk_en_a; 25401730558SDoug Anderson 25501730558SDoug Anderson /* Special bit makes CMD11 not die */ 25601730558SDoug Anderson cmdr |= SDMMC_CMD_VOLT_SWITCH; 25701730558SDoug Anderson 25801730558SDoug Anderson /* Change state to continue to handle CMD11 weirdness */ 25901730558SDoug Anderson WARN_ON(slot->host->state != STATE_SENDING_CMD); 26001730558SDoug Anderson slot->host->state = STATE_SENDING_CMD11; 26101730558SDoug Anderson 26201730558SDoug Anderson /* 26301730558SDoug Anderson * We need to disable low power mode (automatic clock stop) 26401730558SDoug Anderson * while doing voltage switch so we don't confuse the card, 26501730558SDoug Anderson * since stopping the clock is a specific part of the UHS 26601730558SDoug Anderson * voltage change dance. 26701730558SDoug Anderson * 26801730558SDoug Anderson * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be 26901730558SDoug Anderson * unconditionally turned back on in dw_mci_setup_bus() if it's 27001730558SDoug Anderson * ever called with a non-zero clock. That shouldn't happen 27101730558SDoug Anderson * until the voltage change is all done. 27201730558SDoug Anderson */ 27301730558SDoug Anderson clk_en_a = mci_readl(host, CLKENA); 27401730558SDoug Anderson clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id); 27501730558SDoug Anderson mci_writel(host, CLKENA, clk_en_a); 27601730558SDoug Anderson mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | 27701730558SDoug Anderson SDMMC_CMD_PRV_DAT_WAIT, 0); 27801730558SDoug Anderson } 27901730558SDoug Anderson 280f95f3850SWill Newton if (cmd->flags & MMC_RSP_PRESENT) { 281f95f3850SWill Newton /* We expect a response, so set this bit */ 282f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_EXP; 283f95f3850SWill Newton if (cmd->flags & MMC_RSP_136) 284f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_LONG; 285f95f3850SWill Newton } 286f95f3850SWill Newton 287f95f3850SWill Newton if (cmd->flags & MMC_RSP_CRC) 288f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_CRC; 289f95f3850SWill Newton 290f95f3850SWill Newton data = cmd->data; 291f95f3850SWill Newton if (data) { 292f95f3850SWill Newton cmdr |= SDMMC_CMD_DAT_EXP; 293f95f3850SWill Newton if (data->flags & MMC_DATA_WRITE) 294f95f3850SWill Newton cmdr |= SDMMC_CMD_DAT_WR; 295f95f3850SWill Newton } 296f95f3850SWill Newton 297aaaaeb7aSJaehoon Chung if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags)) 298aaaaeb7aSJaehoon Chung cmdr |= SDMMC_CMD_USE_HOLD_REG; 299800d78bfSThomas Abraham 300f95f3850SWill Newton return cmdr; 301f95f3850SWill Newton } 302f95f3850SWill Newton 30390c2143aSSeungwon Jeon static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd) 30490c2143aSSeungwon Jeon { 30590c2143aSSeungwon Jeon struct mmc_command *stop; 30690c2143aSSeungwon Jeon u32 cmdr; 30790c2143aSSeungwon Jeon 30890c2143aSSeungwon Jeon if (!cmd->data) 30990c2143aSSeungwon Jeon return 0; 31090c2143aSSeungwon Jeon 31190c2143aSSeungwon Jeon stop = &host->stop_abort; 31290c2143aSSeungwon Jeon cmdr = cmd->opcode; 31390c2143aSSeungwon Jeon memset(stop, 0, sizeof(struct mmc_command)); 31490c2143aSSeungwon Jeon 31590c2143aSSeungwon Jeon if (cmdr == MMC_READ_SINGLE_BLOCK || 31690c2143aSSeungwon Jeon cmdr == MMC_READ_MULTIPLE_BLOCK || 31790c2143aSSeungwon Jeon cmdr == MMC_WRITE_BLOCK || 3186c2c6506SUlf Hansson cmdr == MMC_WRITE_MULTIPLE_BLOCK || 3196c2c6506SUlf Hansson cmdr == MMC_SEND_TUNING_BLOCK || 3206c2c6506SUlf Hansson cmdr == MMC_SEND_TUNING_BLOCK_HS200) { 32190c2143aSSeungwon Jeon stop->opcode = MMC_STOP_TRANSMISSION; 32290c2143aSSeungwon Jeon stop->arg = 0; 32390c2143aSSeungwon Jeon stop->flags = MMC_RSP_R1B | MMC_CMD_AC; 32490c2143aSSeungwon Jeon } else if (cmdr == SD_IO_RW_EXTENDED) { 32590c2143aSSeungwon Jeon stop->opcode = SD_IO_RW_DIRECT; 32690c2143aSSeungwon Jeon stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) | 32790c2143aSSeungwon Jeon ((cmd->arg >> 28) & 0x7); 32890c2143aSSeungwon Jeon stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC; 32990c2143aSSeungwon Jeon } else { 33090c2143aSSeungwon Jeon return 0; 33190c2143aSSeungwon Jeon } 33290c2143aSSeungwon Jeon 33390c2143aSSeungwon Jeon cmdr = stop->opcode | SDMMC_CMD_STOP | 33490c2143aSSeungwon Jeon SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP; 33590c2143aSSeungwon Jeon 33690c2143aSSeungwon Jeon return cmdr; 33790c2143aSSeungwon Jeon } 33890c2143aSSeungwon Jeon 3390bdbd0e8SDoug Anderson static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags) 3400bdbd0e8SDoug Anderson { 3410bdbd0e8SDoug Anderson unsigned long timeout = jiffies + msecs_to_jiffies(500); 3420bdbd0e8SDoug Anderson 3430bdbd0e8SDoug Anderson /* 3440bdbd0e8SDoug Anderson * Databook says that before issuing a new data transfer command 3450bdbd0e8SDoug Anderson * we need to check to see if the card is busy. Data transfer commands 3460bdbd0e8SDoug Anderson * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that. 3470bdbd0e8SDoug Anderson * 3480bdbd0e8SDoug Anderson * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is 3490bdbd0e8SDoug Anderson * expected. 3500bdbd0e8SDoug Anderson */ 3510bdbd0e8SDoug Anderson if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) && 3520bdbd0e8SDoug Anderson !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) { 3530bdbd0e8SDoug Anderson while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) { 3540bdbd0e8SDoug Anderson if (time_after(jiffies, timeout)) { 3550bdbd0e8SDoug Anderson /* Command will fail; we'll pass error then */ 3560bdbd0e8SDoug Anderson dev_err(host->dev, "Busy; trying anyway\n"); 3570bdbd0e8SDoug Anderson break; 3580bdbd0e8SDoug Anderson } 3590bdbd0e8SDoug Anderson udelay(10); 3600bdbd0e8SDoug Anderson } 3610bdbd0e8SDoug Anderson } 3620bdbd0e8SDoug Anderson } 3630bdbd0e8SDoug Anderson 364f95f3850SWill Newton static void dw_mci_start_command(struct dw_mci *host, 365f95f3850SWill Newton struct mmc_command *cmd, u32 cmd_flags) 366f95f3850SWill Newton { 367f95f3850SWill Newton host->cmd = cmd; 3684a90920cSThomas Abraham dev_vdbg(host->dev, 369f95f3850SWill Newton "start command: ARGR=0x%08x CMDR=0x%08x\n", 370f95f3850SWill Newton cmd->arg, cmd_flags); 371f95f3850SWill Newton 372f95f3850SWill Newton mci_writel(host, CMDARG, cmd->arg); 3730e3a22c0SShawn Lin wmb(); /* drain writebuffer */ 3740bdbd0e8SDoug Anderson dw_mci_wait_while_busy(host, cmd_flags); 375f95f3850SWill Newton 376f95f3850SWill Newton mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); 377f95f3850SWill Newton } 378f95f3850SWill Newton 37990c2143aSSeungwon Jeon static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data) 380f95f3850SWill Newton { 38190c2143aSSeungwon Jeon struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort; 3820e3a22c0SShawn Lin 38390c2143aSSeungwon Jeon dw_mci_start_command(host, stop, host->stop_cmdr); 384f95f3850SWill Newton } 385f95f3850SWill Newton 386f95f3850SWill Newton /* DMA interface functions */ 387f95f3850SWill Newton static void dw_mci_stop_dma(struct dw_mci *host) 388f95f3850SWill Newton { 38903e8cb53SJames Hogan if (host->using_dma) { 390f95f3850SWill Newton host->dma_ops->stop(host); 391f95f3850SWill Newton host->dma_ops->cleanup(host); 392aa50f259SSeungwon Jeon } 393aa50f259SSeungwon Jeon 394f95f3850SWill Newton /* Data transfer was stopped by the interrupt handler */ 395f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 396f95f3850SWill Newton } 397f95f3850SWill Newton 3989aa51408SSeungwon Jeon static int dw_mci_get_dma_dir(struct mmc_data *data) 3999aa51408SSeungwon Jeon { 4009aa51408SSeungwon Jeon if (data->flags & MMC_DATA_WRITE) 4019aa51408SSeungwon Jeon return DMA_TO_DEVICE; 4029aa51408SSeungwon Jeon else 4039aa51408SSeungwon Jeon return DMA_FROM_DEVICE; 4049aa51408SSeungwon Jeon } 4059aa51408SSeungwon Jeon 406f95f3850SWill Newton static void dw_mci_dma_cleanup(struct dw_mci *host) 407f95f3850SWill Newton { 408f95f3850SWill Newton struct mmc_data *data = host->data; 409f95f3850SWill Newton 410f95f3850SWill Newton if (data) 4119aa51408SSeungwon Jeon if (!data->host_cookie) 4124a90920cSThomas Abraham dma_unmap_sg(host->dev, 4139aa51408SSeungwon Jeon data->sg, 4149aa51408SSeungwon Jeon data->sg_len, 4159aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 416f95f3850SWill Newton } 417f95f3850SWill Newton 4185ce9d961SSeungwon Jeon static void dw_mci_idmac_reset(struct dw_mci *host) 4195ce9d961SSeungwon Jeon { 4205ce9d961SSeungwon Jeon u32 bmod = mci_readl(host, BMOD); 4215ce9d961SSeungwon Jeon /* Software reset of DMA */ 4225ce9d961SSeungwon Jeon bmod |= SDMMC_IDMAC_SWRESET; 4235ce9d961SSeungwon Jeon mci_writel(host, BMOD, bmod); 4245ce9d961SSeungwon Jeon } 4255ce9d961SSeungwon Jeon 426f95f3850SWill Newton static void dw_mci_idmac_stop_dma(struct dw_mci *host) 427f95f3850SWill Newton { 428f95f3850SWill Newton u32 temp; 429f95f3850SWill Newton 430f95f3850SWill Newton /* Disable and reset the IDMAC interface */ 431f95f3850SWill Newton temp = mci_readl(host, CTRL); 432f95f3850SWill Newton temp &= ~SDMMC_CTRL_USE_IDMAC; 433f95f3850SWill Newton temp |= SDMMC_CTRL_DMA_RESET; 434f95f3850SWill Newton mci_writel(host, CTRL, temp); 435f95f3850SWill Newton 436f95f3850SWill Newton /* Stop the IDMAC running */ 437f95f3850SWill Newton temp = mci_readl(host, BMOD); 438a5289a43SJaehoon Chung temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB); 4395ce9d961SSeungwon Jeon temp |= SDMMC_IDMAC_SWRESET; 440f95f3850SWill Newton mci_writel(host, BMOD, temp); 441f95f3850SWill Newton } 442f95f3850SWill Newton 4433fc7eaefSShawn Lin static void dw_mci_dmac_complete_dma(void *arg) 444f95f3850SWill Newton { 4453fc7eaefSShawn Lin struct dw_mci *host = arg; 446f95f3850SWill Newton struct mmc_data *data = host->data; 447f95f3850SWill Newton 4484a90920cSThomas Abraham dev_vdbg(host->dev, "DMA complete\n"); 449f95f3850SWill Newton 4503fc7eaefSShawn Lin if ((host->use_dma == TRANS_MODE_EDMAC) && 4513fc7eaefSShawn Lin data && (data->flags & MMC_DATA_READ)) 4523fc7eaefSShawn Lin /* Invalidate cache after read */ 4533fc7eaefSShawn Lin dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc), 4543fc7eaefSShawn Lin data->sg, 4553fc7eaefSShawn Lin data->sg_len, 4563fc7eaefSShawn Lin DMA_FROM_DEVICE); 4573fc7eaefSShawn Lin 458f95f3850SWill Newton host->dma_ops->cleanup(host); 459f95f3850SWill Newton 460f95f3850SWill Newton /* 461f95f3850SWill Newton * If the card was removed, data will be NULL. No point in trying to 462f95f3850SWill Newton * send the stop command or waiting for NBUSY in this case. 463f95f3850SWill Newton */ 464f95f3850SWill Newton if (data) { 465f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 466f95f3850SWill Newton tasklet_schedule(&host->tasklet); 467f95f3850SWill Newton } 468f95f3850SWill Newton } 469f95f3850SWill Newton 470f95f3850SWill Newton static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data, 471f95f3850SWill Newton unsigned int sg_len) 472f95f3850SWill Newton { 4735959b32eSAlexey Brodkin unsigned int desc_len; 474f95f3850SWill Newton int i; 4750e3a22c0SShawn Lin 47669d99fdcSPrabu Thangamuthu if (host->dma_64bit_address == 1) { 4775959b32eSAlexey Brodkin struct idmac_desc_64addr *desc_first, *desc_last, *desc; 47869d99fdcSPrabu Thangamuthu 4795959b32eSAlexey Brodkin desc_first = desc_last = desc = host->sg_cpu; 4805959b32eSAlexey Brodkin 4815959b32eSAlexey Brodkin for (i = 0; i < sg_len; i++) { 48269d99fdcSPrabu Thangamuthu unsigned int length = sg_dma_len(&data->sg[i]); 4830e3a22c0SShawn Lin 48469d99fdcSPrabu Thangamuthu u64 mem_addr = sg_dma_address(&data->sg[i]); 48569d99fdcSPrabu Thangamuthu 4865959b32eSAlexey Brodkin for ( ; length ; desc++) { 4875959b32eSAlexey Brodkin desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ? 4885959b32eSAlexey Brodkin length : DW_MCI_DESC_DATA_LENGTH; 4895959b32eSAlexey Brodkin 4905959b32eSAlexey Brodkin length -= desc_len; 4915959b32eSAlexey Brodkin 49269d99fdcSPrabu Thangamuthu /* 4935959b32eSAlexey Brodkin * Set the OWN bit and disable interrupts 4945959b32eSAlexey Brodkin * for this descriptor 49569d99fdcSPrabu Thangamuthu */ 49669d99fdcSPrabu Thangamuthu desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | 49769d99fdcSPrabu Thangamuthu IDMAC_DES0_CH; 4985959b32eSAlexey Brodkin 49969d99fdcSPrabu Thangamuthu /* Buffer length */ 5005959b32eSAlexey Brodkin IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len); 50169d99fdcSPrabu Thangamuthu 50269d99fdcSPrabu Thangamuthu /* Physical address to DMA to/from */ 50369d99fdcSPrabu Thangamuthu desc->des4 = mem_addr & 0xffffffff; 50469d99fdcSPrabu Thangamuthu desc->des5 = mem_addr >> 32; 5055959b32eSAlexey Brodkin 5065959b32eSAlexey Brodkin /* Update physical address for the next desc */ 5075959b32eSAlexey Brodkin mem_addr += desc_len; 5085959b32eSAlexey Brodkin 5095959b32eSAlexey Brodkin /* Save pointer to the last descriptor */ 5105959b32eSAlexey Brodkin desc_last = desc; 5115959b32eSAlexey Brodkin } 51269d99fdcSPrabu Thangamuthu } 51369d99fdcSPrabu Thangamuthu 51469d99fdcSPrabu Thangamuthu /* Set first descriptor */ 5155959b32eSAlexey Brodkin desc_first->des0 |= IDMAC_DES0_FD; 51669d99fdcSPrabu Thangamuthu 51769d99fdcSPrabu Thangamuthu /* Set last descriptor */ 5185959b32eSAlexey Brodkin desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); 5195959b32eSAlexey Brodkin desc_last->des0 |= IDMAC_DES0_LD; 52069d99fdcSPrabu Thangamuthu 52169d99fdcSPrabu Thangamuthu } else { 5225959b32eSAlexey Brodkin struct idmac_desc *desc_first, *desc_last, *desc; 523f95f3850SWill Newton 5245959b32eSAlexey Brodkin desc_first = desc_last = desc = host->sg_cpu; 5255959b32eSAlexey Brodkin 5265959b32eSAlexey Brodkin for (i = 0; i < sg_len; i++) { 527f95f3850SWill Newton unsigned int length = sg_dma_len(&data->sg[i]); 5280e3a22c0SShawn Lin 529f95f3850SWill Newton u32 mem_addr = sg_dma_address(&data->sg[i]); 530f95f3850SWill Newton 5315959b32eSAlexey Brodkin for ( ; length ; desc++) { 5325959b32eSAlexey Brodkin desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ? 5335959b32eSAlexey Brodkin length : DW_MCI_DESC_DATA_LENGTH; 5345959b32eSAlexey Brodkin 5355959b32eSAlexey Brodkin length -= desc_len; 5365959b32eSAlexey Brodkin 53769d99fdcSPrabu Thangamuthu /* 5385959b32eSAlexey Brodkin * Set the OWN bit and disable interrupts 5395959b32eSAlexey Brodkin * for this descriptor 54069d99fdcSPrabu Thangamuthu */ 5416687c42fSBen Dooks desc->des0 = cpu_to_le32(IDMAC_DES0_OWN | 5425959b32eSAlexey Brodkin IDMAC_DES0_DIC | 5435959b32eSAlexey Brodkin IDMAC_DES0_CH); 5445959b32eSAlexey Brodkin 545f95f3850SWill Newton /* Buffer length */ 5465959b32eSAlexey Brodkin IDMAC_SET_BUFFER1_SIZE(desc, desc_len); 547f95f3850SWill Newton 548f95f3850SWill Newton /* Physical address to DMA to/from */ 5496687c42fSBen Dooks desc->des2 = cpu_to_le32(mem_addr); 5505959b32eSAlexey Brodkin 5515959b32eSAlexey Brodkin /* Update physical address for the next desc */ 5525959b32eSAlexey Brodkin mem_addr += desc_len; 5535959b32eSAlexey Brodkin 5545959b32eSAlexey Brodkin /* Save pointer to the last descriptor */ 5555959b32eSAlexey Brodkin desc_last = desc; 5565959b32eSAlexey Brodkin } 557f95f3850SWill Newton } 558f95f3850SWill Newton 559f95f3850SWill Newton /* Set first descriptor */ 5605959b32eSAlexey Brodkin desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD); 561f95f3850SWill Newton 562f95f3850SWill Newton /* Set last descriptor */ 5635959b32eSAlexey Brodkin desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH | 5645959b32eSAlexey Brodkin IDMAC_DES0_DIC)); 5655959b32eSAlexey Brodkin desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD); 56669d99fdcSPrabu Thangamuthu } 567f95f3850SWill Newton 5680e3a22c0SShawn Lin wmb(); /* drain writebuffer */ 569f95f3850SWill Newton } 570f95f3850SWill Newton 5713fc7eaefSShawn Lin static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len) 572f95f3850SWill Newton { 573f95f3850SWill Newton u32 temp; 574f95f3850SWill Newton 575f95f3850SWill Newton dw_mci_translate_sglist(host, host->data, sg_len); 576f95f3850SWill Newton 577536f6b91SSonny Rao /* Make sure to reset DMA in case we did PIO before this */ 578536f6b91SSonny Rao dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET); 579536f6b91SSonny Rao dw_mci_idmac_reset(host); 580536f6b91SSonny Rao 581f95f3850SWill Newton /* Select IDMAC interface */ 582f95f3850SWill Newton temp = mci_readl(host, CTRL); 583f95f3850SWill Newton temp |= SDMMC_CTRL_USE_IDMAC; 584f95f3850SWill Newton mci_writel(host, CTRL, temp); 585f95f3850SWill Newton 5860e3a22c0SShawn Lin /* drain writebuffer */ 587f95f3850SWill Newton wmb(); 588f95f3850SWill Newton 589f95f3850SWill Newton /* Enable the IDMAC */ 590f95f3850SWill Newton temp = mci_readl(host, BMOD); 591a5289a43SJaehoon Chung temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB; 592f95f3850SWill Newton mci_writel(host, BMOD, temp); 593f95f3850SWill Newton 594f95f3850SWill Newton /* Start it running */ 595f95f3850SWill Newton mci_writel(host, PLDMND, 1); 5963fc7eaefSShawn Lin 5973fc7eaefSShawn Lin return 0; 598f95f3850SWill Newton } 599f95f3850SWill Newton 600f95f3850SWill Newton static int dw_mci_idmac_init(struct dw_mci *host) 601f95f3850SWill Newton { 602897b69e7SSeungwon Jeon int i; 603f95f3850SWill Newton 60469d99fdcSPrabu Thangamuthu if (host->dma_64bit_address == 1) { 60569d99fdcSPrabu Thangamuthu struct idmac_desc_64addr *p; 60669d99fdcSPrabu Thangamuthu /* Number of descriptors in the ring buffer */ 60769d99fdcSPrabu Thangamuthu host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr); 60869d99fdcSPrabu Thangamuthu 60969d99fdcSPrabu Thangamuthu /* Forward link the descriptor list */ 61069d99fdcSPrabu Thangamuthu for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; 61169d99fdcSPrabu Thangamuthu i++, p++) { 61269d99fdcSPrabu Thangamuthu p->des6 = (host->sg_dma + 61369d99fdcSPrabu Thangamuthu (sizeof(struct idmac_desc_64addr) * 61469d99fdcSPrabu Thangamuthu (i + 1))) & 0xffffffff; 61569d99fdcSPrabu Thangamuthu 61669d99fdcSPrabu Thangamuthu p->des7 = (u64)(host->sg_dma + 61769d99fdcSPrabu Thangamuthu (sizeof(struct idmac_desc_64addr) * 61869d99fdcSPrabu Thangamuthu (i + 1))) >> 32; 61969d99fdcSPrabu Thangamuthu /* Initialize reserved and buffer size fields to "0" */ 62069d99fdcSPrabu Thangamuthu p->des1 = 0; 62169d99fdcSPrabu Thangamuthu p->des2 = 0; 62269d99fdcSPrabu Thangamuthu p->des3 = 0; 62369d99fdcSPrabu Thangamuthu } 62469d99fdcSPrabu Thangamuthu 62569d99fdcSPrabu Thangamuthu /* Set the last descriptor as the end-of-ring descriptor */ 62669d99fdcSPrabu Thangamuthu p->des6 = host->sg_dma & 0xffffffff; 62769d99fdcSPrabu Thangamuthu p->des7 = (u64)host->sg_dma >> 32; 62869d99fdcSPrabu Thangamuthu p->des0 = IDMAC_DES0_ER; 62969d99fdcSPrabu Thangamuthu 63069d99fdcSPrabu Thangamuthu } else { 63169d99fdcSPrabu Thangamuthu struct idmac_desc *p; 632f95f3850SWill Newton /* Number of descriptors in the ring buffer */ 633f95f3850SWill Newton host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc); 634f95f3850SWill Newton 635f95f3850SWill Newton /* Forward link the descriptor list */ 6360e3a22c0SShawn Lin for (i = 0, p = host->sg_cpu; 6370e3a22c0SShawn Lin i < host->ring_size - 1; 6380e3a22c0SShawn Lin i++, p++) { 6396687c42fSBen Dooks p->des3 = cpu_to_le32(host->sg_dma + 6406687c42fSBen Dooks (sizeof(struct idmac_desc) * (i + 1))); 6414b244724SZhangfei Gao p->des1 = 0; 6424b244724SZhangfei Gao } 643f95f3850SWill Newton 644f95f3850SWill Newton /* Set the last descriptor as the end-of-ring descriptor */ 6456687c42fSBen Dooks p->des3 = cpu_to_le32(host->sg_dma); 6466687c42fSBen Dooks p->des0 = cpu_to_le32(IDMAC_DES0_ER); 64769d99fdcSPrabu Thangamuthu } 648f95f3850SWill Newton 6495ce9d961SSeungwon Jeon dw_mci_idmac_reset(host); 650141a712aSSeungwon Jeon 65169d99fdcSPrabu Thangamuthu if (host->dma_64bit_address == 1) { 65269d99fdcSPrabu Thangamuthu /* Mask out interrupts - get Tx & Rx complete only */ 65369d99fdcSPrabu Thangamuthu mci_writel(host, IDSTS64, IDMAC_INT_CLR); 65469d99fdcSPrabu Thangamuthu mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI | 65569d99fdcSPrabu Thangamuthu SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); 65669d99fdcSPrabu Thangamuthu 65769d99fdcSPrabu Thangamuthu /* Set the descriptor base address */ 65869d99fdcSPrabu Thangamuthu mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff); 65969d99fdcSPrabu Thangamuthu mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32); 66069d99fdcSPrabu Thangamuthu 66169d99fdcSPrabu Thangamuthu } else { 662f95f3850SWill Newton /* Mask out interrupts - get Tx & Rx complete only */ 663fc79a4d6SJoonyoung Shim mci_writel(host, IDSTS, IDMAC_INT_CLR); 66469d99fdcSPrabu Thangamuthu mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | 66569d99fdcSPrabu Thangamuthu SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); 666f95f3850SWill Newton 667f95f3850SWill Newton /* Set the descriptor base address */ 668f95f3850SWill Newton mci_writel(host, DBADDR, host->sg_dma); 66969d99fdcSPrabu Thangamuthu } 67069d99fdcSPrabu Thangamuthu 671f95f3850SWill Newton return 0; 672f95f3850SWill Newton } 673f95f3850SWill Newton 6748e2b36eaSArnd Bergmann static const struct dw_mci_dma_ops dw_mci_idmac_ops = { 675885c3e80SSeungwon Jeon .init = dw_mci_idmac_init, 676885c3e80SSeungwon Jeon .start = dw_mci_idmac_start_dma, 677885c3e80SSeungwon Jeon .stop = dw_mci_idmac_stop_dma, 6783fc7eaefSShawn Lin .complete = dw_mci_dmac_complete_dma, 679885c3e80SSeungwon Jeon .cleanup = dw_mci_dma_cleanup, 680885c3e80SSeungwon Jeon }; 6813fc7eaefSShawn Lin 6823fc7eaefSShawn Lin static void dw_mci_edmac_stop_dma(struct dw_mci *host) 6833fc7eaefSShawn Lin { 684ab925a31SShawn Lin dmaengine_terminate_async(host->dms->ch); 6853fc7eaefSShawn Lin } 6863fc7eaefSShawn Lin 6873fc7eaefSShawn Lin static int dw_mci_edmac_start_dma(struct dw_mci *host, 6883fc7eaefSShawn Lin unsigned int sg_len) 6893fc7eaefSShawn Lin { 6903fc7eaefSShawn Lin struct dma_slave_config cfg; 6913fc7eaefSShawn Lin struct dma_async_tx_descriptor *desc = NULL; 6923fc7eaefSShawn Lin struct scatterlist *sgl = host->data->sg; 6933fc7eaefSShawn Lin const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256}; 6943fc7eaefSShawn Lin u32 sg_elems = host->data->sg_len; 6953fc7eaefSShawn Lin u32 fifoth_val; 6963fc7eaefSShawn Lin u32 fifo_offset = host->fifo_reg - host->regs; 6973fc7eaefSShawn Lin int ret = 0; 6983fc7eaefSShawn Lin 6993fc7eaefSShawn Lin /* Set external dma config: burst size, burst width */ 700260b3164SArnd Bergmann cfg.dst_addr = host->phy_regs + fifo_offset; 7013fc7eaefSShawn Lin cfg.src_addr = cfg.dst_addr; 7023fc7eaefSShawn Lin cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 7033fc7eaefSShawn Lin cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 7043fc7eaefSShawn Lin 7053fc7eaefSShawn Lin /* Match burst msize with external dma config */ 7063fc7eaefSShawn Lin fifoth_val = mci_readl(host, FIFOTH); 7073fc7eaefSShawn Lin cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7]; 7083fc7eaefSShawn Lin cfg.src_maxburst = cfg.dst_maxburst; 7093fc7eaefSShawn Lin 7103fc7eaefSShawn Lin if (host->data->flags & MMC_DATA_WRITE) 7113fc7eaefSShawn Lin cfg.direction = DMA_MEM_TO_DEV; 7123fc7eaefSShawn Lin else 7133fc7eaefSShawn Lin cfg.direction = DMA_DEV_TO_MEM; 7143fc7eaefSShawn Lin 7153fc7eaefSShawn Lin ret = dmaengine_slave_config(host->dms->ch, &cfg); 7163fc7eaefSShawn Lin if (ret) { 7173fc7eaefSShawn Lin dev_err(host->dev, "Failed to config edmac.\n"); 7183fc7eaefSShawn Lin return -EBUSY; 7193fc7eaefSShawn Lin } 7203fc7eaefSShawn Lin 7213fc7eaefSShawn Lin desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, 7223fc7eaefSShawn Lin sg_len, cfg.direction, 7233fc7eaefSShawn Lin DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 7243fc7eaefSShawn Lin if (!desc) { 7253fc7eaefSShawn Lin dev_err(host->dev, "Can't prepare slave sg.\n"); 7263fc7eaefSShawn Lin return -EBUSY; 7273fc7eaefSShawn Lin } 7283fc7eaefSShawn Lin 7293fc7eaefSShawn Lin /* Set dw_mci_dmac_complete_dma as callback */ 7303fc7eaefSShawn Lin desc->callback = dw_mci_dmac_complete_dma; 7313fc7eaefSShawn Lin desc->callback_param = (void *)host; 7323fc7eaefSShawn Lin dmaengine_submit(desc); 7333fc7eaefSShawn Lin 7343fc7eaefSShawn Lin /* Flush cache before write */ 7353fc7eaefSShawn Lin if (host->data->flags & MMC_DATA_WRITE) 7363fc7eaefSShawn Lin dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl, 7373fc7eaefSShawn Lin sg_elems, DMA_TO_DEVICE); 7383fc7eaefSShawn Lin 7393fc7eaefSShawn Lin dma_async_issue_pending(host->dms->ch); 7403fc7eaefSShawn Lin 7413fc7eaefSShawn Lin return 0; 7423fc7eaefSShawn Lin } 7433fc7eaefSShawn Lin 7443fc7eaefSShawn Lin static int dw_mci_edmac_init(struct dw_mci *host) 7453fc7eaefSShawn Lin { 7463fc7eaefSShawn Lin /* Request external dma channel */ 7473fc7eaefSShawn Lin host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL); 7483fc7eaefSShawn Lin if (!host->dms) 7493fc7eaefSShawn Lin return -ENOMEM; 7503fc7eaefSShawn Lin 7513fc7eaefSShawn Lin host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx"); 7523fc7eaefSShawn Lin if (!host->dms->ch) { 7534539d36eSDan Carpenter dev_err(host->dev, "Failed to get external DMA channel.\n"); 7543fc7eaefSShawn Lin kfree(host->dms); 7553fc7eaefSShawn Lin host->dms = NULL; 7563fc7eaefSShawn Lin return -ENXIO; 7573fc7eaefSShawn Lin } 7583fc7eaefSShawn Lin 7593fc7eaefSShawn Lin return 0; 7603fc7eaefSShawn Lin } 7613fc7eaefSShawn Lin 7623fc7eaefSShawn Lin static void dw_mci_edmac_exit(struct dw_mci *host) 7633fc7eaefSShawn Lin { 7643fc7eaefSShawn Lin if (host->dms) { 7653fc7eaefSShawn Lin if (host->dms->ch) { 7663fc7eaefSShawn Lin dma_release_channel(host->dms->ch); 7673fc7eaefSShawn Lin host->dms->ch = NULL; 7683fc7eaefSShawn Lin } 7693fc7eaefSShawn Lin kfree(host->dms); 7703fc7eaefSShawn Lin host->dms = NULL; 7713fc7eaefSShawn Lin } 7723fc7eaefSShawn Lin } 7733fc7eaefSShawn Lin 7743fc7eaefSShawn Lin static const struct dw_mci_dma_ops dw_mci_edmac_ops = { 7753fc7eaefSShawn Lin .init = dw_mci_edmac_init, 7763fc7eaefSShawn Lin .exit = dw_mci_edmac_exit, 7773fc7eaefSShawn Lin .start = dw_mci_edmac_start_dma, 7783fc7eaefSShawn Lin .stop = dw_mci_edmac_stop_dma, 7793fc7eaefSShawn Lin .complete = dw_mci_dmac_complete_dma, 7803fc7eaefSShawn Lin .cleanup = dw_mci_dma_cleanup, 7813fc7eaefSShawn Lin }; 782885c3e80SSeungwon Jeon 7839aa51408SSeungwon Jeon static int dw_mci_pre_dma_transfer(struct dw_mci *host, 7849aa51408SSeungwon Jeon struct mmc_data *data, 7859aa51408SSeungwon Jeon bool next) 786f95f3850SWill Newton { 787f95f3850SWill Newton struct scatterlist *sg; 7889aa51408SSeungwon Jeon unsigned int i, sg_len; 789f95f3850SWill Newton 7909aa51408SSeungwon Jeon if (!next && data->host_cookie) 7919aa51408SSeungwon Jeon return data->host_cookie; 792f95f3850SWill Newton 793f95f3850SWill Newton /* 794f95f3850SWill Newton * We don't do DMA on "complex" transfers, i.e. with 795f95f3850SWill Newton * non-word-aligned buffers or lengths. Also, we don't bother 796f95f3850SWill Newton * with all the DMA setup overhead for short transfers. 797f95f3850SWill Newton */ 798f95f3850SWill Newton if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD) 799f95f3850SWill Newton return -EINVAL; 8009aa51408SSeungwon Jeon 801f95f3850SWill Newton if (data->blksz & 3) 802f95f3850SWill Newton return -EINVAL; 803f95f3850SWill Newton 804f95f3850SWill Newton for_each_sg(data->sg, sg, data->sg_len, i) { 805f95f3850SWill Newton if (sg->offset & 3 || sg->length & 3) 806f95f3850SWill Newton return -EINVAL; 807f95f3850SWill Newton } 808f95f3850SWill Newton 8094a90920cSThomas Abraham sg_len = dma_map_sg(host->dev, 8109aa51408SSeungwon Jeon data->sg, 8119aa51408SSeungwon Jeon data->sg_len, 8129aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 8139aa51408SSeungwon Jeon if (sg_len == 0) 8149aa51408SSeungwon Jeon return -EINVAL; 8159aa51408SSeungwon Jeon 8169aa51408SSeungwon Jeon if (next) 8179aa51408SSeungwon Jeon data->host_cookie = sg_len; 8189aa51408SSeungwon Jeon 8199aa51408SSeungwon Jeon return sg_len; 8209aa51408SSeungwon Jeon } 8219aa51408SSeungwon Jeon 8229aa51408SSeungwon Jeon static void dw_mci_pre_req(struct mmc_host *mmc, 8239aa51408SSeungwon Jeon struct mmc_request *mrq, 8249aa51408SSeungwon Jeon bool is_first_req) 8259aa51408SSeungwon Jeon { 8269aa51408SSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 8279aa51408SSeungwon Jeon struct mmc_data *data = mrq->data; 8289aa51408SSeungwon Jeon 8299aa51408SSeungwon Jeon if (!slot->host->use_dma || !data) 8309aa51408SSeungwon Jeon return; 8319aa51408SSeungwon Jeon 8329aa51408SSeungwon Jeon if (data->host_cookie) { 8339aa51408SSeungwon Jeon data->host_cookie = 0; 8349aa51408SSeungwon Jeon return; 8359aa51408SSeungwon Jeon } 8369aa51408SSeungwon Jeon 8379aa51408SSeungwon Jeon if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0) 8389aa51408SSeungwon Jeon data->host_cookie = 0; 8399aa51408SSeungwon Jeon } 8409aa51408SSeungwon Jeon 8419aa51408SSeungwon Jeon static void dw_mci_post_req(struct mmc_host *mmc, 8429aa51408SSeungwon Jeon struct mmc_request *mrq, 8439aa51408SSeungwon Jeon int err) 8449aa51408SSeungwon Jeon { 8459aa51408SSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 8469aa51408SSeungwon Jeon struct mmc_data *data = mrq->data; 8479aa51408SSeungwon Jeon 8489aa51408SSeungwon Jeon if (!slot->host->use_dma || !data) 8499aa51408SSeungwon Jeon return; 8509aa51408SSeungwon Jeon 8519aa51408SSeungwon Jeon if (data->host_cookie) 8524a90920cSThomas Abraham dma_unmap_sg(slot->host->dev, 8539aa51408SSeungwon Jeon data->sg, 8549aa51408SSeungwon Jeon data->sg_len, 8559aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 8569aa51408SSeungwon Jeon data->host_cookie = 0; 8579aa51408SSeungwon Jeon } 8589aa51408SSeungwon Jeon 85952426899SSeungwon Jeon static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data) 86052426899SSeungwon Jeon { 86152426899SSeungwon Jeon unsigned int blksz = data->blksz; 86252426899SSeungwon Jeon const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256}; 86352426899SSeungwon Jeon u32 fifo_width = 1 << host->data_shift; 86452426899SSeungwon Jeon u32 blksz_depth = blksz / fifo_width, fifoth_val; 86552426899SSeungwon Jeon u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers; 8660e3a22c0SShawn Lin int idx = ARRAY_SIZE(mszs) - 1; 86752426899SSeungwon Jeon 8683fc7eaefSShawn Lin /* pio should ship this scenario */ 8693fc7eaefSShawn Lin if (!host->use_dma) 8703fc7eaefSShawn Lin return; 8713fc7eaefSShawn Lin 87252426899SSeungwon Jeon tx_wmark = (host->fifo_depth) / 2; 87352426899SSeungwon Jeon tx_wmark_invers = host->fifo_depth - tx_wmark; 87452426899SSeungwon Jeon 87552426899SSeungwon Jeon /* 87652426899SSeungwon Jeon * MSIZE is '1', 87752426899SSeungwon Jeon * if blksz is not a multiple of the FIFO width 87852426899SSeungwon Jeon */ 87952426899SSeungwon Jeon if (blksz % fifo_width) { 88052426899SSeungwon Jeon msize = 0; 88152426899SSeungwon Jeon rx_wmark = 1; 88252426899SSeungwon Jeon goto done; 88352426899SSeungwon Jeon } 88452426899SSeungwon Jeon 88552426899SSeungwon Jeon do { 88652426899SSeungwon Jeon if (!((blksz_depth % mszs[idx]) || 88752426899SSeungwon Jeon (tx_wmark_invers % mszs[idx]))) { 88852426899SSeungwon Jeon msize = idx; 88952426899SSeungwon Jeon rx_wmark = mszs[idx] - 1; 89052426899SSeungwon Jeon break; 89152426899SSeungwon Jeon } 89252426899SSeungwon Jeon } while (--idx > 0); 89352426899SSeungwon Jeon /* 89452426899SSeungwon Jeon * If idx is '0', it won't be tried 89552426899SSeungwon Jeon * Thus, initial values are uesed 89652426899SSeungwon Jeon */ 89752426899SSeungwon Jeon done: 89852426899SSeungwon Jeon fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark); 89952426899SSeungwon Jeon mci_writel(host, FIFOTH, fifoth_val); 90052426899SSeungwon Jeon } 90152426899SSeungwon Jeon 902f1d2736cSSeungwon Jeon static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data) 903f1d2736cSSeungwon Jeon { 904f1d2736cSSeungwon Jeon unsigned int blksz = data->blksz; 905f1d2736cSSeungwon Jeon u32 blksz_depth, fifo_depth; 906f1d2736cSSeungwon Jeon u16 thld_size; 907f1d2736cSSeungwon Jeon 908f1d2736cSSeungwon Jeon WARN_ON(!(data->flags & MMC_DATA_READ)); 909f1d2736cSSeungwon Jeon 91066dfd101SJames Hogan /* 91166dfd101SJames Hogan * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is 91266dfd101SJames Hogan * in the FIFO region, so we really shouldn't access it). 91366dfd101SJames Hogan */ 91466dfd101SJames Hogan if (host->verid < DW_MMC_240A) 91566dfd101SJames Hogan return; 91666dfd101SJames Hogan 917f1d2736cSSeungwon Jeon if (host->timing != MMC_TIMING_MMC_HS200 && 918488b8d63SJaehoon Chung host->timing != MMC_TIMING_MMC_HS400 && 919f1d2736cSSeungwon Jeon host->timing != MMC_TIMING_UHS_SDR104) 920f1d2736cSSeungwon Jeon goto disable; 921f1d2736cSSeungwon Jeon 922f1d2736cSSeungwon Jeon blksz_depth = blksz / (1 << host->data_shift); 923f1d2736cSSeungwon Jeon fifo_depth = host->fifo_depth; 924f1d2736cSSeungwon Jeon 925f1d2736cSSeungwon Jeon if (blksz_depth > fifo_depth) 926f1d2736cSSeungwon Jeon goto disable; 927f1d2736cSSeungwon Jeon 928f1d2736cSSeungwon Jeon /* 929f1d2736cSSeungwon Jeon * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz' 930f1d2736cSSeungwon Jeon * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz 931f1d2736cSSeungwon Jeon * Currently just choose blksz. 932f1d2736cSSeungwon Jeon */ 933f1d2736cSSeungwon Jeon thld_size = blksz; 934f1d2736cSSeungwon Jeon mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1)); 935f1d2736cSSeungwon Jeon return; 936f1d2736cSSeungwon Jeon 937f1d2736cSSeungwon Jeon disable: 938f1d2736cSSeungwon Jeon mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0)); 939f1d2736cSSeungwon Jeon } 940f1d2736cSSeungwon Jeon 9419aa51408SSeungwon Jeon static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data) 9429aa51408SSeungwon Jeon { 943f8c58c11SDoug Anderson unsigned long irqflags; 9449aa51408SSeungwon Jeon int sg_len; 9459aa51408SSeungwon Jeon u32 temp; 9469aa51408SSeungwon Jeon 9479aa51408SSeungwon Jeon host->using_dma = 0; 9489aa51408SSeungwon Jeon 9499aa51408SSeungwon Jeon /* If we don't have a channel, we can't do DMA */ 9509aa51408SSeungwon Jeon if (!host->use_dma) 9519aa51408SSeungwon Jeon return -ENODEV; 9529aa51408SSeungwon Jeon 9539aa51408SSeungwon Jeon sg_len = dw_mci_pre_dma_transfer(host, data, 0); 954a99aa9b9SSeungwon Jeon if (sg_len < 0) { 955a99aa9b9SSeungwon Jeon host->dma_ops->stop(host); 9569aa51408SSeungwon Jeon return sg_len; 957a99aa9b9SSeungwon Jeon } 9589aa51408SSeungwon Jeon 95903e8cb53SJames Hogan host->using_dma = 1; 96003e8cb53SJames Hogan 9613fc7eaefSShawn Lin if (host->use_dma == TRANS_MODE_IDMAC) 9624a90920cSThomas Abraham dev_vdbg(host->dev, 963f95f3850SWill Newton "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n", 9643fc7eaefSShawn Lin (unsigned long)host->sg_cpu, 9653fc7eaefSShawn Lin (unsigned long)host->sg_dma, 966f95f3850SWill Newton sg_len); 967f95f3850SWill Newton 96852426899SSeungwon Jeon /* 96952426899SSeungwon Jeon * Decide the MSIZE and RX/TX Watermark. 97052426899SSeungwon Jeon * If current block size is same with previous size, 97152426899SSeungwon Jeon * no need to update fifoth. 97252426899SSeungwon Jeon */ 97352426899SSeungwon Jeon if (host->prev_blksz != data->blksz) 97452426899SSeungwon Jeon dw_mci_adjust_fifoth(host, data); 97552426899SSeungwon Jeon 976f95f3850SWill Newton /* Enable the DMA interface */ 977f95f3850SWill Newton temp = mci_readl(host, CTRL); 978f95f3850SWill Newton temp |= SDMMC_CTRL_DMA_ENABLE; 979f95f3850SWill Newton mci_writel(host, CTRL, temp); 980f95f3850SWill Newton 981f95f3850SWill Newton /* Disable RX/TX IRQs, let DMA handle it */ 982f8c58c11SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 983f95f3850SWill Newton temp = mci_readl(host, INTMASK); 984f95f3850SWill Newton temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR); 985f95f3850SWill Newton mci_writel(host, INTMASK, temp); 986f8c58c11SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 987f95f3850SWill Newton 9883fc7eaefSShawn Lin if (host->dma_ops->start(host, sg_len)) { 9893fc7eaefSShawn Lin /* We can't do DMA */ 9903fc7eaefSShawn Lin dev_err(host->dev, "%s: failed to start DMA.\n", __func__); 9913fc7eaefSShawn Lin return -ENODEV; 9923fc7eaefSShawn Lin } 993f95f3850SWill Newton 994f95f3850SWill Newton return 0; 995f95f3850SWill Newton } 996f95f3850SWill Newton 997f95f3850SWill Newton static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) 998f95f3850SWill Newton { 999f8c58c11SDoug Anderson unsigned long irqflags; 10000e3a22c0SShawn Lin int flags = SG_MITER_ATOMIC; 1001f95f3850SWill Newton u32 temp; 1002f95f3850SWill Newton 1003f95f3850SWill Newton data->error = -EINPROGRESS; 1004f95f3850SWill Newton 1005f95f3850SWill Newton WARN_ON(host->data); 1006f95f3850SWill Newton host->sg = NULL; 1007f95f3850SWill Newton host->data = data; 1008f95f3850SWill Newton 1009f1d2736cSSeungwon Jeon if (data->flags & MMC_DATA_READ) { 101055c5efbcSJames Hogan host->dir_status = DW_MCI_RECV_STATUS; 1011f1d2736cSSeungwon Jeon dw_mci_ctrl_rd_thld(host, data); 1012f1d2736cSSeungwon Jeon } else { 101355c5efbcSJames Hogan host->dir_status = DW_MCI_SEND_STATUS; 1014f1d2736cSSeungwon Jeon } 101555c5efbcSJames Hogan 1016f95f3850SWill Newton if (dw_mci_submit_data_dma(host, data)) { 1017f9c2a0dcSSeungwon Jeon if (host->data->flags & MMC_DATA_READ) 1018f9c2a0dcSSeungwon Jeon flags |= SG_MITER_TO_SG; 1019f9c2a0dcSSeungwon Jeon else 1020f9c2a0dcSSeungwon Jeon flags |= SG_MITER_FROM_SG; 1021f9c2a0dcSSeungwon Jeon 1022f9c2a0dcSSeungwon Jeon sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); 1023f95f3850SWill Newton host->sg = data->sg; 102434b664a2SJames Hogan host->part_buf_start = 0; 102534b664a2SJames Hogan host->part_buf_count = 0; 1026f95f3850SWill Newton 1027b40af3aaSJames Hogan mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR); 1028f8c58c11SDoug Anderson 1029f8c58c11SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 1030f95f3850SWill Newton temp = mci_readl(host, INTMASK); 1031f95f3850SWill Newton temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR; 1032f95f3850SWill Newton mci_writel(host, INTMASK, temp); 1033f8c58c11SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 1034f95f3850SWill Newton 1035f95f3850SWill Newton temp = mci_readl(host, CTRL); 1036f95f3850SWill Newton temp &= ~SDMMC_CTRL_DMA_ENABLE; 1037f95f3850SWill Newton mci_writel(host, CTRL, temp); 103852426899SSeungwon Jeon 103952426899SSeungwon Jeon /* 104052426899SSeungwon Jeon * Use the initial fifoth_val for PIO mode. 104152426899SSeungwon Jeon * If next issued data may be transfered by DMA mode, 104252426899SSeungwon Jeon * prev_blksz should be invalidated. 104352426899SSeungwon Jeon */ 104452426899SSeungwon Jeon mci_writel(host, FIFOTH, host->fifoth_val); 104552426899SSeungwon Jeon host->prev_blksz = 0; 104652426899SSeungwon Jeon } else { 104752426899SSeungwon Jeon /* 104852426899SSeungwon Jeon * Keep the current block size. 104952426899SSeungwon Jeon * It will be used to decide whether to update 105052426899SSeungwon Jeon * fifoth register next time. 105152426899SSeungwon Jeon */ 105252426899SSeungwon Jeon host->prev_blksz = data->blksz; 1053f95f3850SWill Newton } 1054f95f3850SWill Newton } 1055f95f3850SWill Newton 1056f95f3850SWill Newton static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg) 1057f95f3850SWill Newton { 1058f95f3850SWill Newton struct dw_mci *host = slot->host; 1059f95f3850SWill Newton unsigned long timeout = jiffies + msecs_to_jiffies(500); 1060f95f3850SWill Newton unsigned int cmd_status = 0; 1061f95f3850SWill Newton 1062f95f3850SWill Newton mci_writel(host, CMDARG, arg); 10630e3a22c0SShawn Lin wmb(); /* drain writebuffer */ 10640bdbd0e8SDoug Anderson dw_mci_wait_while_busy(host, cmd); 1065f95f3850SWill Newton mci_writel(host, CMD, SDMMC_CMD_START | cmd); 1066f95f3850SWill Newton 1067f95f3850SWill Newton while (time_before(jiffies, timeout)) { 1068f95f3850SWill Newton cmd_status = mci_readl(host, CMD); 1069f95f3850SWill Newton if (!(cmd_status & SDMMC_CMD_START)) 1070f95f3850SWill Newton return; 1071f95f3850SWill Newton } 1072f95f3850SWill Newton dev_err(&slot->mmc->class_dev, 1073f95f3850SWill Newton "Timeout sending command (cmd %#x arg %#x status %#x)\n", 1074f95f3850SWill Newton cmd, arg, cmd_status); 1075f95f3850SWill Newton } 1076f95f3850SWill Newton 1077ab269128SAbhilash Kesavan static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit) 1078f95f3850SWill Newton { 1079f95f3850SWill Newton struct dw_mci *host = slot->host; 1080fdf492a1SDoug Anderson unsigned int clock = slot->clock; 1081f95f3850SWill Newton u32 div; 10829623b5b9SDoug Anderson u32 clk_en_a; 108301730558SDoug Anderson u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT; 108401730558SDoug Anderson 108501730558SDoug Anderson /* We must continue to set bit 28 in CMD until the change is complete */ 108601730558SDoug Anderson if (host->state == STATE_WAITING_CMD11_DONE) 108701730558SDoug Anderson sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH; 1088f95f3850SWill Newton 1089fdf492a1SDoug Anderson if (!clock) { 1090fdf492a1SDoug Anderson mci_writel(host, CLKENA, 0); 109101730558SDoug Anderson mci_send_cmd(slot, sdmmc_cmd_bits, 0); 1092fdf492a1SDoug Anderson } else if (clock != host->current_speed || force_clkinit) { 1093fdf492a1SDoug Anderson div = host->bus_hz / clock; 1094fdf492a1SDoug Anderson if (host->bus_hz % clock && host->bus_hz > clock) 1095f95f3850SWill Newton /* 1096f95f3850SWill Newton * move the + 1 after the divide to prevent 1097f95f3850SWill Newton * over-clocking the card. 1098f95f3850SWill Newton */ 1099e419990bSSeungwon Jeon div += 1; 1100e419990bSSeungwon Jeon 1101fdf492a1SDoug Anderson div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0; 1102f95f3850SWill Newton 1103f95f3850SWill Newton dev_info(&slot->mmc->class_dev, 1104fdf492a1SDoug Anderson "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n", 1105fdf492a1SDoug Anderson slot->id, host->bus_hz, clock, 1106fdf492a1SDoug Anderson div ? ((host->bus_hz / div) >> 1) : 1107fdf492a1SDoug Anderson host->bus_hz, div); 1108f95f3850SWill Newton 1109f95f3850SWill Newton /* disable clock */ 1110f95f3850SWill Newton mci_writel(host, CLKENA, 0); 1111f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 1112f95f3850SWill Newton 1113f95f3850SWill Newton /* inform CIU */ 111401730558SDoug Anderson mci_send_cmd(slot, sdmmc_cmd_bits, 0); 1115f95f3850SWill Newton 1116f95f3850SWill Newton /* set clock to desired speed */ 1117f95f3850SWill Newton mci_writel(host, CLKDIV, div); 1118f95f3850SWill Newton 1119f95f3850SWill Newton /* inform CIU */ 112001730558SDoug Anderson mci_send_cmd(slot, sdmmc_cmd_bits, 0); 1121f95f3850SWill Newton 11229623b5b9SDoug Anderson /* enable clock; only low power if no SDIO */ 11239623b5b9SDoug Anderson clk_en_a = SDMMC_CLKEN_ENABLE << slot->id; 1124b24c8b26SDoug Anderson if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) 11259623b5b9SDoug Anderson clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id; 11269623b5b9SDoug Anderson mci_writel(host, CLKENA, clk_en_a); 1127f95f3850SWill Newton 1128f95f3850SWill Newton /* inform CIU */ 112901730558SDoug Anderson mci_send_cmd(slot, sdmmc_cmd_bits, 0); 1130f95f3850SWill Newton } 1131f95f3850SWill Newton 1132fdf492a1SDoug Anderson host->current_speed = clock; 1133fdf492a1SDoug Anderson 1134f95f3850SWill Newton /* Set the current slot bus width */ 11351d56c453SSeungwon Jeon mci_writel(host, CTYPE, (slot->ctype << slot->id)); 1136f95f3850SWill Newton } 1137f95f3850SWill Newton 1138053b3ce6SSeungwon Jeon static void __dw_mci_start_request(struct dw_mci *host, 1139053b3ce6SSeungwon Jeon struct dw_mci_slot *slot, 1140053b3ce6SSeungwon Jeon struct mmc_command *cmd) 1141f95f3850SWill Newton { 1142f95f3850SWill Newton struct mmc_request *mrq; 1143f95f3850SWill Newton struct mmc_data *data; 1144f95f3850SWill Newton u32 cmdflags; 1145f95f3850SWill Newton 1146f95f3850SWill Newton mrq = slot->mrq; 1147f95f3850SWill Newton 1148f95f3850SWill Newton host->cur_slot = slot; 1149f95f3850SWill Newton host->mrq = mrq; 1150f95f3850SWill Newton 1151f95f3850SWill Newton host->pending_events = 0; 1152f95f3850SWill Newton host->completed_events = 0; 1153e352c813SSeungwon Jeon host->cmd_status = 0; 1154f95f3850SWill Newton host->data_status = 0; 1155e352c813SSeungwon Jeon host->dir_status = 0; 1156f95f3850SWill Newton 1157053b3ce6SSeungwon Jeon data = cmd->data; 1158f95f3850SWill Newton if (data) { 1159f16afa88SJaehoon Chung mci_writel(host, TMOUT, 0xFFFFFFFF); 1160f95f3850SWill Newton mci_writel(host, BYTCNT, data->blksz*data->blocks); 1161f95f3850SWill Newton mci_writel(host, BLKSIZ, data->blksz); 1162f95f3850SWill Newton } 1163f95f3850SWill Newton 1164f95f3850SWill Newton cmdflags = dw_mci_prepare_command(slot->mmc, cmd); 1165f95f3850SWill Newton 1166f95f3850SWill Newton /* this is the first command, send the initialization clock */ 1167f95f3850SWill Newton if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags)) 1168f95f3850SWill Newton cmdflags |= SDMMC_CMD_INIT; 1169f95f3850SWill Newton 1170f95f3850SWill Newton if (data) { 1171f95f3850SWill Newton dw_mci_submit_data(host, data); 11720e3a22c0SShawn Lin wmb(); /* drain writebuffer */ 1173f95f3850SWill Newton } 1174f95f3850SWill Newton 1175f95f3850SWill Newton dw_mci_start_command(host, cmd, cmdflags); 1176f95f3850SWill Newton 11775c935165SDoug Anderson if (cmd->opcode == SD_SWITCH_VOLTAGE) { 117849ba0302SDoug Anderson unsigned long irqflags; 117949ba0302SDoug Anderson 11805c935165SDoug Anderson /* 11818886a6fdSDoug Anderson * Databook says to fail after 2ms w/ no response, but evidence 11828886a6fdSDoug Anderson * shows that sometimes the cmd11 interrupt takes over 130ms. 11838886a6fdSDoug Anderson * We'll set to 500ms, plus an extra jiffy just in case jiffies 11848886a6fdSDoug Anderson * is just about to roll over. 118549ba0302SDoug Anderson * 118649ba0302SDoug Anderson * We do this whole thing under spinlock and only if the 118749ba0302SDoug Anderson * command hasn't already completed (indicating the the irq 118849ba0302SDoug Anderson * already ran so we don't want the timeout). 11895c935165SDoug Anderson */ 119049ba0302SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 119149ba0302SDoug Anderson if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) 11925c935165SDoug Anderson mod_timer(&host->cmd11_timer, 11938886a6fdSDoug Anderson jiffies + msecs_to_jiffies(500) + 1); 119449ba0302SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 11955c935165SDoug Anderson } 11965c935165SDoug Anderson 1197f95f3850SWill Newton if (mrq->stop) 1198f95f3850SWill Newton host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop); 119990c2143aSSeungwon Jeon else 120090c2143aSSeungwon Jeon host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd); 1201f95f3850SWill Newton } 1202f95f3850SWill Newton 1203053b3ce6SSeungwon Jeon static void dw_mci_start_request(struct dw_mci *host, 1204053b3ce6SSeungwon Jeon struct dw_mci_slot *slot) 1205053b3ce6SSeungwon Jeon { 1206053b3ce6SSeungwon Jeon struct mmc_request *mrq = slot->mrq; 1207053b3ce6SSeungwon Jeon struct mmc_command *cmd; 1208053b3ce6SSeungwon Jeon 1209053b3ce6SSeungwon Jeon cmd = mrq->sbc ? mrq->sbc : mrq->cmd; 1210053b3ce6SSeungwon Jeon __dw_mci_start_request(host, slot, cmd); 1211053b3ce6SSeungwon Jeon } 1212053b3ce6SSeungwon Jeon 12137456caaeSJames Hogan /* must be called with host->lock held */ 1214f95f3850SWill Newton static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot, 1215f95f3850SWill Newton struct mmc_request *mrq) 1216f95f3850SWill Newton { 1217f95f3850SWill Newton dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", 1218f95f3850SWill Newton host->state); 1219f95f3850SWill Newton 1220f95f3850SWill Newton slot->mrq = mrq; 1221f95f3850SWill Newton 122201730558SDoug Anderson if (host->state == STATE_WAITING_CMD11_DONE) { 122301730558SDoug Anderson dev_warn(&slot->mmc->class_dev, 122401730558SDoug Anderson "Voltage change didn't complete\n"); 122501730558SDoug Anderson /* 122601730558SDoug Anderson * this case isn't expected to happen, so we can 122701730558SDoug Anderson * either crash here or just try to continue on 122801730558SDoug Anderson * in the closest possible state 122901730558SDoug Anderson */ 123001730558SDoug Anderson host->state = STATE_IDLE; 123101730558SDoug Anderson } 123201730558SDoug Anderson 1233f95f3850SWill Newton if (host->state == STATE_IDLE) { 1234f95f3850SWill Newton host->state = STATE_SENDING_CMD; 1235f95f3850SWill Newton dw_mci_start_request(host, slot); 1236f95f3850SWill Newton } else { 1237f95f3850SWill Newton list_add_tail(&slot->queue_node, &host->queue); 1238f95f3850SWill Newton } 1239f95f3850SWill Newton } 1240f95f3850SWill Newton 1241f95f3850SWill Newton static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) 1242f95f3850SWill Newton { 1243f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 1244f95f3850SWill Newton struct dw_mci *host = slot->host; 1245f95f3850SWill Newton 1246f95f3850SWill Newton WARN_ON(slot->mrq); 1247f95f3850SWill Newton 12487456caaeSJames Hogan /* 12497456caaeSJames Hogan * The check for card presence and queueing of the request must be 12507456caaeSJames Hogan * atomic, otherwise the card could be removed in between and the 12517456caaeSJames Hogan * request wouldn't fail until another card was inserted. 12527456caaeSJames Hogan */ 12537456caaeSJames Hogan 125456f6911cSShawn Lin if (!dw_mci_get_cd(mmc)) { 1255f95f3850SWill Newton mrq->cmd->error = -ENOMEDIUM; 1256f95f3850SWill Newton mmc_request_done(mmc, mrq); 1257f95f3850SWill Newton return; 1258f95f3850SWill Newton } 1259f95f3850SWill Newton 126056f6911cSShawn Lin spin_lock_bh(&host->lock); 126156f6911cSShawn Lin 1262f95f3850SWill Newton dw_mci_queue_request(host, slot, mrq); 12637456caaeSJames Hogan 12647456caaeSJames Hogan spin_unlock_bh(&host->lock); 1265f95f3850SWill Newton } 1266f95f3850SWill Newton 1267f95f3850SWill Newton static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 1268f95f3850SWill Newton { 1269f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 1270e95baf13SArnd Bergmann const struct dw_mci_drv_data *drv_data = slot->host->drv_data; 127141babf75SJaehoon Chung u32 regs; 127251da2240SYuvaraj CD int ret; 1273f95f3850SWill Newton 1274f95f3850SWill Newton switch (ios->bus_width) { 1275f95f3850SWill Newton case MMC_BUS_WIDTH_4: 1276f95f3850SWill Newton slot->ctype = SDMMC_CTYPE_4BIT; 1277f95f3850SWill Newton break; 1278c9b2a06fSJaehoon Chung case MMC_BUS_WIDTH_8: 1279c9b2a06fSJaehoon Chung slot->ctype = SDMMC_CTYPE_8BIT; 1280c9b2a06fSJaehoon Chung break; 1281b2f7cb45SJaehoon Chung default: 1282b2f7cb45SJaehoon Chung /* set default 1 bit mode */ 1283b2f7cb45SJaehoon Chung slot->ctype = SDMMC_CTYPE_1BIT; 1284f95f3850SWill Newton } 1285f95f3850SWill Newton 128641babf75SJaehoon Chung regs = mci_readl(slot->host, UHS_REG); 12873f514291SSeungwon Jeon 12883f514291SSeungwon Jeon /* DDR mode set */ 128980113132SSeungwon Jeon if (ios->timing == MMC_TIMING_MMC_DDR52 || 12907cc8d580SJaehoon Chung ios->timing == MMC_TIMING_UHS_DDR50 || 129180113132SSeungwon Jeon ios->timing == MMC_TIMING_MMC_HS400) 1292c69042a5SHyeonsu Kim regs |= ((0x1 << slot->id) << 16); 12933f514291SSeungwon Jeon else 1294c69042a5SHyeonsu Kim regs &= ~((0x1 << slot->id) << 16); 12953f514291SSeungwon Jeon 129641babf75SJaehoon Chung mci_writel(slot->host, UHS_REG, regs); 1297f1d2736cSSeungwon Jeon slot->host->timing = ios->timing; 129841babf75SJaehoon Chung 1299f95f3850SWill Newton /* 1300f95f3850SWill Newton * Use mirror of ios->clock to prevent race with mmc 1301f95f3850SWill Newton * core ios update when finding the minimum. 1302f95f3850SWill Newton */ 1303f95f3850SWill Newton slot->clock = ios->clock; 1304f95f3850SWill Newton 1305cb27a843SJames Hogan if (drv_data && drv_data->set_ios) 1306cb27a843SJames Hogan drv_data->set_ios(slot->host, ios); 1307800d78bfSThomas Abraham 1308f95f3850SWill Newton switch (ios->power_mode) { 1309f95f3850SWill Newton case MMC_POWER_UP: 131051da2240SYuvaraj CD if (!IS_ERR(mmc->supply.vmmc)) { 131151da2240SYuvaraj CD ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 131251da2240SYuvaraj CD ios->vdd); 131351da2240SYuvaraj CD if (ret) { 131451da2240SYuvaraj CD dev_err(slot->host->dev, 131551da2240SYuvaraj CD "failed to enable vmmc regulator\n"); 131651da2240SYuvaraj CD /*return, if failed turn on vmmc*/ 131751da2240SYuvaraj CD return; 131851da2240SYuvaraj CD } 131951da2240SYuvaraj CD } 132029d0d161SDoug Anderson set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags); 132129d0d161SDoug Anderson regs = mci_readl(slot->host, PWREN); 132229d0d161SDoug Anderson regs |= (1 << slot->id); 132329d0d161SDoug Anderson mci_writel(slot->host, PWREN, regs); 132429d0d161SDoug Anderson break; 132529d0d161SDoug Anderson case MMC_POWER_ON: 1326d1f1dd86SDoug Anderson if (!slot->host->vqmmc_enabled) { 1327d1f1dd86SDoug Anderson if (!IS_ERR(mmc->supply.vqmmc)) { 132851da2240SYuvaraj CD ret = regulator_enable(mmc->supply.vqmmc); 132951da2240SYuvaraj CD if (ret < 0) 133051da2240SYuvaraj CD dev_err(slot->host->dev, 1331d1f1dd86SDoug Anderson "failed to enable vqmmc\n"); 133251da2240SYuvaraj CD else 133351da2240SYuvaraj CD slot->host->vqmmc_enabled = true; 1334d1f1dd86SDoug Anderson 1335d1f1dd86SDoug Anderson } else { 1336d1f1dd86SDoug Anderson /* Keep track so we don't reset again */ 1337d1f1dd86SDoug Anderson slot->host->vqmmc_enabled = true; 1338d1f1dd86SDoug Anderson } 1339d1f1dd86SDoug Anderson 1340d1f1dd86SDoug Anderson /* Reset our state machine after powering on */ 1341d1f1dd86SDoug Anderson dw_mci_ctrl_reset(slot->host, 1342d1f1dd86SDoug Anderson SDMMC_CTRL_ALL_RESET_FLAGS); 134351da2240SYuvaraj CD } 1344655babbdSDoug Anderson 1345655babbdSDoug Anderson /* Adjust clock / bus width after power is up */ 1346655babbdSDoug Anderson dw_mci_setup_bus(slot, false); 1347655babbdSDoug Anderson 1348e6f34e2fSJames Hogan break; 1349e6f34e2fSJames Hogan case MMC_POWER_OFF: 1350655babbdSDoug Anderson /* Turn clock off before power goes down */ 1351655babbdSDoug Anderson dw_mci_setup_bus(slot, false); 1352655babbdSDoug Anderson 135351da2240SYuvaraj CD if (!IS_ERR(mmc->supply.vmmc)) 135451da2240SYuvaraj CD mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); 135551da2240SYuvaraj CD 1356d1f1dd86SDoug Anderson if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) 135751da2240SYuvaraj CD regulator_disable(mmc->supply.vqmmc); 135851da2240SYuvaraj CD slot->host->vqmmc_enabled = false; 135951da2240SYuvaraj CD 13604366dcc5SJaehoon Chung regs = mci_readl(slot->host, PWREN); 13614366dcc5SJaehoon Chung regs &= ~(1 << slot->id); 13624366dcc5SJaehoon Chung mci_writel(slot->host, PWREN, regs); 1363f95f3850SWill Newton break; 1364f95f3850SWill Newton default: 1365f95f3850SWill Newton break; 1366f95f3850SWill Newton } 1367655babbdSDoug Anderson 1368655babbdSDoug Anderson if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0) 1369655babbdSDoug Anderson slot->host->state = STATE_IDLE; 1370f95f3850SWill Newton } 1371f95f3850SWill Newton 137201730558SDoug Anderson static int dw_mci_card_busy(struct mmc_host *mmc) 137301730558SDoug Anderson { 137401730558SDoug Anderson struct dw_mci_slot *slot = mmc_priv(mmc); 137501730558SDoug Anderson u32 status; 137601730558SDoug Anderson 137701730558SDoug Anderson /* 137801730558SDoug Anderson * Check the busy bit which is low when DAT[3:0] 137901730558SDoug Anderson * (the data lines) are 0000 138001730558SDoug Anderson */ 138101730558SDoug Anderson status = mci_readl(slot->host, STATUS); 138201730558SDoug Anderson 138301730558SDoug Anderson return !!(status & SDMMC_STATUS_BUSY); 138401730558SDoug Anderson } 138501730558SDoug Anderson 138601730558SDoug Anderson static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios) 138701730558SDoug Anderson { 138801730558SDoug Anderson struct dw_mci_slot *slot = mmc_priv(mmc); 138901730558SDoug Anderson struct dw_mci *host = slot->host; 13908f7849c4SZhangfei Gao const struct dw_mci_drv_data *drv_data = host->drv_data; 139101730558SDoug Anderson u32 uhs; 139201730558SDoug Anderson u32 v18 = SDMMC_UHS_18V << slot->id; 139301730558SDoug Anderson int ret; 139401730558SDoug Anderson 13958f7849c4SZhangfei Gao if (drv_data && drv_data->switch_voltage) 13968f7849c4SZhangfei Gao return drv_data->switch_voltage(mmc, ios); 13978f7849c4SZhangfei Gao 139801730558SDoug Anderson /* 139901730558SDoug Anderson * Program the voltage. Note that some instances of dw_mmc may use 140001730558SDoug Anderson * the UHS_REG for this. For other instances (like exynos) the UHS_REG 140101730558SDoug Anderson * does no harm but you need to set the regulator directly. Try both. 140201730558SDoug Anderson */ 140301730558SDoug Anderson uhs = mci_readl(host, UHS_REG); 1404e0848f5dSDouglas Anderson if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) 140501730558SDoug Anderson uhs &= ~v18; 1406e0848f5dSDouglas Anderson else 140701730558SDoug Anderson uhs |= v18; 1408e0848f5dSDouglas Anderson 140901730558SDoug Anderson if (!IS_ERR(mmc->supply.vqmmc)) { 1410e0848f5dSDouglas Anderson ret = mmc_regulator_set_vqmmc(mmc, ios); 141101730558SDoug Anderson 141201730558SDoug Anderson if (ret) { 1413b19caf37SDoug Anderson dev_dbg(&mmc->class_dev, 1414e0848f5dSDouglas Anderson "Regulator set error %d - %s V\n", 1415e0848f5dSDouglas Anderson ret, uhs & v18 ? "1.8" : "3.3"); 141601730558SDoug Anderson return ret; 141701730558SDoug Anderson } 141801730558SDoug Anderson } 141901730558SDoug Anderson mci_writel(host, UHS_REG, uhs); 142001730558SDoug Anderson 142101730558SDoug Anderson return 0; 142201730558SDoug Anderson } 142301730558SDoug Anderson 1424f95f3850SWill Newton static int dw_mci_get_ro(struct mmc_host *mmc) 1425f95f3850SWill Newton { 1426f95f3850SWill Newton int read_only; 1427f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 14289795a846SJaehoon Chung int gpio_ro = mmc_gpio_get_ro(mmc); 1429f95f3850SWill Newton 1430f95f3850SWill Newton /* Use platform get_ro function, else try on board write protect */ 1431287980e4SArnd Bergmann if (gpio_ro >= 0) 14329795a846SJaehoon Chung read_only = gpio_ro; 1433f95f3850SWill Newton else 1434f95f3850SWill Newton read_only = 1435f95f3850SWill Newton mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0; 1436f95f3850SWill Newton 1437f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is %s\n", 1438f95f3850SWill Newton read_only ? "read-only" : "read-write"); 1439f95f3850SWill Newton 1440f95f3850SWill Newton return read_only; 1441f95f3850SWill Newton } 1442f95f3850SWill Newton 1443f95f3850SWill Newton static int dw_mci_get_cd(struct mmc_host *mmc) 1444f95f3850SWill Newton { 1445f95f3850SWill Newton int present; 1446f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 14477cf347bdSZhangfei Gao struct dw_mci *host = slot->host; 14487cf347bdSZhangfei Gao int gpio_cd = mmc_gpio_get_cd(mmc); 1449f95f3850SWill Newton 1450f95f3850SWill Newton /* Use platform get_cd function, else try onboard card detect */ 1451860951c5SJaehoon Chung if ((mmc->caps & MMC_CAP_NEEDS_POLL) || !mmc_card_is_removable(mmc)) 1452fc3d7720SJaehoon Chung present = 1; 1453287980e4SArnd Bergmann else if (gpio_cd >= 0) 14547cf347bdSZhangfei Gao present = gpio_cd; 1455f95f3850SWill Newton else 1456f95f3850SWill Newton present = (mci_readl(slot->host, CDETECT) & (1 << slot->id)) 1457f95f3850SWill Newton == 0 ? 1 : 0; 1458f95f3850SWill Newton 14597cf347bdSZhangfei Gao spin_lock_bh(&host->lock); 1460bf626e55SZhangfei Gao if (present) { 1461bf626e55SZhangfei Gao set_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1462f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is present\n"); 1463bf626e55SZhangfei Gao } else { 1464bf626e55SZhangfei Gao clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1465f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is not present\n"); 1466bf626e55SZhangfei Gao } 14677cf347bdSZhangfei Gao spin_unlock_bh(&host->lock); 1468f95f3850SWill Newton 1469f95f3850SWill Newton return present; 1470f95f3850SWill Newton } 1471f95f3850SWill Newton 1472935a665eSShawn Lin static void dw_mci_hw_reset(struct mmc_host *mmc) 1473935a665eSShawn Lin { 1474935a665eSShawn Lin struct dw_mci_slot *slot = mmc_priv(mmc); 1475935a665eSShawn Lin struct dw_mci *host = slot->host; 1476935a665eSShawn Lin int reset; 1477935a665eSShawn Lin 1478935a665eSShawn Lin if (host->use_dma == TRANS_MODE_IDMAC) 1479935a665eSShawn Lin dw_mci_idmac_reset(host); 1480935a665eSShawn Lin 1481935a665eSShawn Lin if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET | 1482935a665eSShawn Lin SDMMC_CTRL_FIFO_RESET)) 1483935a665eSShawn Lin return; 1484935a665eSShawn Lin 1485935a665eSShawn Lin /* 1486935a665eSShawn Lin * According to eMMC spec, card reset procedure: 1487935a665eSShawn Lin * tRstW >= 1us: RST_n pulse width 1488935a665eSShawn Lin * tRSCA >= 200us: RST_n to Command time 1489935a665eSShawn Lin * tRSTH >= 1us: RST_n high period 1490935a665eSShawn Lin */ 1491935a665eSShawn Lin reset = mci_readl(host, RST_N); 1492935a665eSShawn Lin reset &= ~(SDMMC_RST_HWACTIVE << slot->id); 1493935a665eSShawn Lin mci_writel(host, RST_N, reset); 1494935a665eSShawn Lin usleep_range(1, 2); 1495935a665eSShawn Lin reset |= SDMMC_RST_HWACTIVE << slot->id; 1496935a665eSShawn Lin mci_writel(host, RST_N, reset); 1497935a665eSShawn Lin usleep_range(200, 300); 1498935a665eSShawn Lin } 1499935a665eSShawn Lin 1500b24c8b26SDoug Anderson static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card) 1501b24c8b26SDoug Anderson { 1502b24c8b26SDoug Anderson struct dw_mci_slot *slot = mmc_priv(mmc); 1503b24c8b26SDoug Anderson struct dw_mci *host = slot->host; 1504b24c8b26SDoug Anderson 15059623b5b9SDoug Anderson /* 15069623b5b9SDoug Anderson * Low power mode will stop the card clock when idle. According to the 15079623b5b9SDoug Anderson * description of the CLKENA register we should disable low power mode 15089623b5b9SDoug Anderson * for SDIO cards if we need SDIO interrupts to work. 15099623b5b9SDoug Anderson */ 1510b24c8b26SDoug Anderson if (mmc->caps & MMC_CAP_SDIO_IRQ) { 15119623b5b9SDoug Anderson const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id; 1512b24c8b26SDoug Anderson u32 clk_en_a_old; 1513b24c8b26SDoug Anderson u32 clk_en_a; 15149623b5b9SDoug Anderson 1515b24c8b26SDoug Anderson clk_en_a_old = mci_readl(host, CLKENA); 15169623b5b9SDoug Anderson 1517b24c8b26SDoug Anderson if (card->type == MMC_TYPE_SDIO || 1518b24c8b26SDoug Anderson card->type == MMC_TYPE_SD_COMBO) { 1519b24c8b26SDoug Anderson set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags); 1520b24c8b26SDoug Anderson clk_en_a = clk_en_a_old & ~clken_low_pwr; 1521b24c8b26SDoug Anderson } else { 1522b24c8b26SDoug Anderson clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags); 1523b24c8b26SDoug Anderson clk_en_a = clk_en_a_old | clken_low_pwr; 1524b24c8b26SDoug Anderson } 1525b24c8b26SDoug Anderson 1526b24c8b26SDoug Anderson if (clk_en_a != clk_en_a_old) { 1527b24c8b26SDoug Anderson mci_writel(host, CLKENA, clk_en_a); 15289623b5b9SDoug Anderson mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | 15299623b5b9SDoug Anderson SDMMC_CMD_PRV_DAT_WAIT, 0); 15309623b5b9SDoug Anderson } 15319623b5b9SDoug Anderson } 1532b24c8b26SDoug Anderson } 15339623b5b9SDoug Anderson 15341a5c8e1fSShashidhar Hiremath static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb) 15351a5c8e1fSShashidhar Hiremath { 15361a5c8e1fSShashidhar Hiremath struct dw_mci_slot *slot = mmc_priv(mmc); 15371a5c8e1fSShashidhar Hiremath struct dw_mci *host = slot->host; 1538f8c58c11SDoug Anderson unsigned long irqflags; 15391a5c8e1fSShashidhar Hiremath u32 int_mask; 15401a5c8e1fSShashidhar Hiremath 1541f8c58c11SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 1542f8c58c11SDoug Anderson 15431a5c8e1fSShashidhar Hiremath /* Enable/disable Slot Specific SDIO interrupt */ 15441a5c8e1fSShashidhar Hiremath int_mask = mci_readl(host, INTMASK); 1545b24c8b26SDoug Anderson if (enb) 1546b24c8b26SDoug Anderson int_mask |= SDMMC_INT_SDIO(slot->sdio_id); 1547b24c8b26SDoug Anderson else 1548b24c8b26SDoug Anderson int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id); 1549b24c8b26SDoug Anderson mci_writel(host, INTMASK, int_mask); 1550f8c58c11SDoug Anderson 1551f8c58c11SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 15521a5c8e1fSShashidhar Hiremath } 15531a5c8e1fSShashidhar Hiremath 15540976f16dSSeungwon Jeon static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode) 15550976f16dSSeungwon Jeon { 15560976f16dSSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 15570976f16dSSeungwon Jeon struct dw_mci *host = slot->host; 15580976f16dSSeungwon Jeon const struct dw_mci_drv_data *drv_data = host->drv_data; 15590e3a22c0SShawn Lin int err = -EINVAL; 15600976f16dSSeungwon Jeon 15610976f16dSSeungwon Jeon if (drv_data && drv_data->execute_tuning) 15629979dbe5SChaotian Jing err = drv_data->execute_tuning(slot, opcode); 15630976f16dSSeungwon Jeon return err; 15640976f16dSSeungwon Jeon } 15650976f16dSSeungwon Jeon 15660e3a22c0SShawn Lin static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc, 15670e3a22c0SShawn Lin struct mmc_ios *ios) 156880113132SSeungwon Jeon { 156980113132SSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 157080113132SSeungwon Jeon struct dw_mci *host = slot->host; 157180113132SSeungwon Jeon const struct dw_mci_drv_data *drv_data = host->drv_data; 157280113132SSeungwon Jeon 157380113132SSeungwon Jeon if (drv_data && drv_data->prepare_hs400_tuning) 157480113132SSeungwon Jeon return drv_data->prepare_hs400_tuning(host, ios); 157580113132SSeungwon Jeon 157680113132SSeungwon Jeon return 0; 157780113132SSeungwon Jeon } 157880113132SSeungwon Jeon 1579f95f3850SWill Newton static const struct mmc_host_ops dw_mci_ops = { 1580f95f3850SWill Newton .request = dw_mci_request, 15819aa51408SSeungwon Jeon .pre_req = dw_mci_pre_req, 15829aa51408SSeungwon Jeon .post_req = dw_mci_post_req, 1583f95f3850SWill Newton .set_ios = dw_mci_set_ios, 1584f95f3850SWill Newton .get_ro = dw_mci_get_ro, 1585f95f3850SWill Newton .get_cd = dw_mci_get_cd, 1586935a665eSShawn Lin .hw_reset = dw_mci_hw_reset, 15871a5c8e1fSShashidhar Hiremath .enable_sdio_irq = dw_mci_enable_sdio_irq, 15880976f16dSSeungwon Jeon .execute_tuning = dw_mci_execute_tuning, 158901730558SDoug Anderson .card_busy = dw_mci_card_busy, 159001730558SDoug Anderson .start_signal_voltage_switch = dw_mci_switch_voltage, 1591b24c8b26SDoug Anderson .init_card = dw_mci_init_card, 159280113132SSeungwon Jeon .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning, 1593f95f3850SWill Newton }; 1594f95f3850SWill Newton 1595f95f3850SWill Newton static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq) 1596f95f3850SWill Newton __releases(&host->lock) 1597f95f3850SWill Newton __acquires(&host->lock) 1598f95f3850SWill Newton { 1599f95f3850SWill Newton struct dw_mci_slot *slot; 1600f95f3850SWill Newton struct mmc_host *prev_mmc = host->cur_slot->mmc; 1601f95f3850SWill Newton 1602f95f3850SWill Newton WARN_ON(host->cmd || host->data); 1603f95f3850SWill Newton 1604f95f3850SWill Newton host->cur_slot->mrq = NULL; 1605f95f3850SWill Newton host->mrq = NULL; 1606f95f3850SWill Newton if (!list_empty(&host->queue)) { 1607f95f3850SWill Newton slot = list_entry(host->queue.next, 1608f95f3850SWill Newton struct dw_mci_slot, queue_node); 1609f95f3850SWill Newton list_del(&slot->queue_node); 16104a90920cSThomas Abraham dev_vdbg(host->dev, "list not empty: %s is next\n", 1611f95f3850SWill Newton mmc_hostname(slot->mmc)); 1612f95f3850SWill Newton host->state = STATE_SENDING_CMD; 1613f95f3850SWill Newton dw_mci_start_request(host, slot); 1614f95f3850SWill Newton } else { 16154a90920cSThomas Abraham dev_vdbg(host->dev, "list empty\n"); 161601730558SDoug Anderson 161701730558SDoug Anderson if (host->state == STATE_SENDING_CMD11) 161801730558SDoug Anderson host->state = STATE_WAITING_CMD11_DONE; 161901730558SDoug Anderson else 1620f95f3850SWill Newton host->state = STATE_IDLE; 1621f95f3850SWill Newton } 1622f95f3850SWill Newton 1623f95f3850SWill Newton spin_unlock(&host->lock); 1624f95f3850SWill Newton mmc_request_done(prev_mmc, mrq); 1625f95f3850SWill Newton spin_lock(&host->lock); 1626f95f3850SWill Newton } 1627f95f3850SWill Newton 1628e352c813SSeungwon Jeon static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd) 1629f95f3850SWill Newton { 1630f95f3850SWill Newton u32 status = host->cmd_status; 1631f95f3850SWill Newton 1632f95f3850SWill Newton host->cmd_status = 0; 1633f95f3850SWill Newton 1634f95f3850SWill Newton /* Read the response from the card (up to 16 bytes) */ 1635f95f3850SWill Newton if (cmd->flags & MMC_RSP_PRESENT) { 1636f95f3850SWill Newton if (cmd->flags & MMC_RSP_136) { 1637f95f3850SWill Newton cmd->resp[3] = mci_readl(host, RESP0); 1638f95f3850SWill Newton cmd->resp[2] = mci_readl(host, RESP1); 1639f95f3850SWill Newton cmd->resp[1] = mci_readl(host, RESP2); 1640f95f3850SWill Newton cmd->resp[0] = mci_readl(host, RESP3); 1641f95f3850SWill Newton } else { 1642f95f3850SWill Newton cmd->resp[0] = mci_readl(host, RESP0); 1643f95f3850SWill Newton cmd->resp[1] = 0; 1644f95f3850SWill Newton cmd->resp[2] = 0; 1645f95f3850SWill Newton cmd->resp[3] = 0; 1646f95f3850SWill Newton } 1647f95f3850SWill Newton } 1648f95f3850SWill Newton 1649f95f3850SWill Newton if (status & SDMMC_INT_RTO) 1650f95f3850SWill Newton cmd->error = -ETIMEDOUT; 1651f95f3850SWill Newton else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)) 1652f95f3850SWill Newton cmd->error = -EILSEQ; 1653f95f3850SWill Newton else if (status & SDMMC_INT_RESP_ERR) 1654f95f3850SWill Newton cmd->error = -EIO; 1655f95f3850SWill Newton else 1656f95f3850SWill Newton cmd->error = 0; 1657f95f3850SWill Newton 1658e352c813SSeungwon Jeon return cmd->error; 1659e352c813SSeungwon Jeon } 1660e352c813SSeungwon Jeon 1661e352c813SSeungwon Jeon static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data) 1662e352c813SSeungwon Jeon { 166331bff450SSeungwon Jeon u32 status = host->data_status; 1664e352c813SSeungwon Jeon 1665e352c813SSeungwon Jeon if (status & DW_MCI_DATA_ERROR_FLAGS) { 1666e352c813SSeungwon Jeon if (status & SDMMC_INT_DRTO) { 1667e352c813SSeungwon Jeon data->error = -ETIMEDOUT; 1668e352c813SSeungwon Jeon } else if (status & SDMMC_INT_DCRC) { 1669e352c813SSeungwon Jeon data->error = -EILSEQ; 1670e352c813SSeungwon Jeon } else if (status & SDMMC_INT_EBE) { 1671e352c813SSeungwon Jeon if (host->dir_status == 1672e352c813SSeungwon Jeon DW_MCI_SEND_STATUS) { 1673e352c813SSeungwon Jeon /* 1674e352c813SSeungwon Jeon * No data CRC status was returned. 1675e352c813SSeungwon Jeon * The number of bytes transferred 1676e352c813SSeungwon Jeon * will be exaggerated in PIO mode. 1677e352c813SSeungwon Jeon */ 1678e352c813SSeungwon Jeon data->bytes_xfered = 0; 1679e352c813SSeungwon Jeon data->error = -ETIMEDOUT; 1680e352c813SSeungwon Jeon } else if (host->dir_status == 1681e352c813SSeungwon Jeon DW_MCI_RECV_STATUS) { 1682e352c813SSeungwon Jeon data->error = -EIO; 1683e352c813SSeungwon Jeon } 1684e352c813SSeungwon Jeon } else { 1685e352c813SSeungwon Jeon /* SDMMC_INT_SBE is included */ 1686e352c813SSeungwon Jeon data->error = -EIO; 1687e352c813SSeungwon Jeon } 1688e352c813SSeungwon Jeon 1689e6cc0123SDoug Anderson dev_dbg(host->dev, "data error, status 0x%08x\n", status); 1690e352c813SSeungwon Jeon 1691e352c813SSeungwon Jeon /* 1692e352c813SSeungwon Jeon * After an error, there may be data lingering 169331bff450SSeungwon Jeon * in the FIFO 1694e352c813SSeungwon Jeon */ 16953a33a94cSSonny Rao dw_mci_reset(host); 1696e352c813SSeungwon Jeon } else { 1697e352c813SSeungwon Jeon data->bytes_xfered = data->blocks * data->blksz; 1698e352c813SSeungwon Jeon data->error = 0; 1699e352c813SSeungwon Jeon } 1700e352c813SSeungwon Jeon 1701e352c813SSeungwon Jeon return data->error; 1702f95f3850SWill Newton } 1703f95f3850SWill Newton 170457e10486SAddy Ke static void dw_mci_set_drto(struct dw_mci *host) 170557e10486SAddy Ke { 170657e10486SAddy Ke unsigned int drto_clks; 170757e10486SAddy Ke unsigned int drto_ms; 170857e10486SAddy Ke 170957e10486SAddy Ke drto_clks = mci_readl(host, TMOUT) >> 8; 171057e10486SAddy Ke drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000); 171157e10486SAddy Ke 171257e10486SAddy Ke /* add a bit spare time */ 171357e10486SAddy Ke drto_ms += 10; 171457e10486SAddy Ke 171557e10486SAddy Ke mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms)); 171657e10486SAddy Ke } 171757e10486SAddy Ke 1718f95f3850SWill Newton static void dw_mci_tasklet_func(unsigned long priv) 1719f95f3850SWill Newton { 1720f95f3850SWill Newton struct dw_mci *host = (struct dw_mci *)priv; 1721f95f3850SWill Newton struct mmc_data *data; 1722f95f3850SWill Newton struct mmc_command *cmd; 1723e352c813SSeungwon Jeon struct mmc_request *mrq; 1724f95f3850SWill Newton enum dw_mci_state state; 1725f95f3850SWill Newton enum dw_mci_state prev_state; 1726e352c813SSeungwon Jeon unsigned int err; 1727f95f3850SWill Newton 1728f95f3850SWill Newton spin_lock(&host->lock); 1729f95f3850SWill Newton 1730f95f3850SWill Newton state = host->state; 1731f95f3850SWill Newton data = host->data; 1732e352c813SSeungwon Jeon mrq = host->mrq; 1733f95f3850SWill Newton 1734f95f3850SWill Newton do { 1735f95f3850SWill Newton prev_state = state; 1736f95f3850SWill Newton 1737f95f3850SWill Newton switch (state) { 1738f95f3850SWill Newton case STATE_IDLE: 173901730558SDoug Anderson case STATE_WAITING_CMD11_DONE: 1740f95f3850SWill Newton break; 1741f95f3850SWill Newton 174201730558SDoug Anderson case STATE_SENDING_CMD11: 1743f95f3850SWill Newton case STATE_SENDING_CMD: 1744f95f3850SWill Newton if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 1745f95f3850SWill Newton &host->pending_events)) 1746f95f3850SWill Newton break; 1747f95f3850SWill Newton 1748f95f3850SWill Newton cmd = host->cmd; 1749f95f3850SWill Newton host->cmd = NULL; 1750f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->completed_events); 1751e352c813SSeungwon Jeon err = dw_mci_command_complete(host, cmd); 1752e352c813SSeungwon Jeon if (cmd == mrq->sbc && !err) { 1753053b3ce6SSeungwon Jeon prev_state = state = STATE_SENDING_CMD; 1754053b3ce6SSeungwon Jeon __dw_mci_start_request(host, host->cur_slot, 1755e352c813SSeungwon Jeon mrq->cmd); 1756053b3ce6SSeungwon Jeon goto unlock; 1757053b3ce6SSeungwon Jeon } 1758053b3ce6SSeungwon Jeon 1759e352c813SSeungwon Jeon if (cmd->data && err) { 176046d17952SDoug Anderson /* 176146d17952SDoug Anderson * During UHS tuning sequence, sending the stop 176246d17952SDoug Anderson * command after the response CRC error would 176346d17952SDoug Anderson * throw the system into a confused state 176446d17952SDoug Anderson * causing all future tuning phases to report 176546d17952SDoug Anderson * failure. 176646d17952SDoug Anderson * 176746d17952SDoug Anderson * In such case controller will move into a data 176846d17952SDoug Anderson * transfer state after a response error or 176946d17952SDoug Anderson * response CRC error. Let's let that finish 177046d17952SDoug Anderson * before trying to send a stop, so we'll go to 177146d17952SDoug Anderson * STATE_SENDING_DATA. 177246d17952SDoug Anderson * 177346d17952SDoug Anderson * Although letting the data transfer take place 177446d17952SDoug Anderson * will waste a bit of time (we already know 177546d17952SDoug Anderson * the command was bad), it can't cause any 177646d17952SDoug Anderson * errors since it's possible it would have 177746d17952SDoug Anderson * taken place anyway if this tasklet got 177846d17952SDoug Anderson * delayed. Allowing the transfer to take place 177946d17952SDoug Anderson * avoids races and keeps things simple. 178046d17952SDoug Anderson */ 178146d17952SDoug Anderson if ((err != -ETIMEDOUT) && 178246d17952SDoug Anderson (cmd->opcode == MMC_SEND_TUNING_BLOCK)) { 178346d17952SDoug Anderson state = STATE_SENDING_DATA; 178446d17952SDoug Anderson continue; 178546d17952SDoug Anderson } 178646d17952SDoug Anderson 178771abb133SSeungwon Jeon dw_mci_stop_dma(host); 178890c2143aSSeungwon Jeon send_stop_abort(host, data); 178971abb133SSeungwon Jeon state = STATE_SENDING_STOP; 179071abb133SSeungwon Jeon break; 179171abb133SSeungwon Jeon } 179271abb133SSeungwon Jeon 1793e352c813SSeungwon Jeon if (!cmd->data || err) { 1794e352c813SSeungwon Jeon dw_mci_request_end(host, mrq); 1795f95f3850SWill Newton goto unlock; 1796f95f3850SWill Newton } 1797f95f3850SWill Newton 1798f95f3850SWill Newton prev_state = state = STATE_SENDING_DATA; 1799f95f3850SWill Newton /* fall through */ 1800f95f3850SWill Newton 1801f95f3850SWill Newton case STATE_SENDING_DATA: 18022aa35465SDoug Anderson /* 18032aa35465SDoug Anderson * We could get a data error and never a transfer 18042aa35465SDoug Anderson * complete so we'd better check for it here. 18052aa35465SDoug Anderson * 18062aa35465SDoug Anderson * Note that we don't really care if we also got a 18072aa35465SDoug Anderson * transfer complete; stopping the DMA and sending an 18082aa35465SDoug Anderson * abort won't hurt. 18092aa35465SDoug Anderson */ 1810f95f3850SWill Newton if (test_and_clear_bit(EVENT_DATA_ERROR, 1811f95f3850SWill Newton &host->pending_events)) { 1812f95f3850SWill Newton dw_mci_stop_dma(host); 1813bdb9a90bSaddy ke if (data->stop || 1814bdb9a90bSaddy ke !(host->data_status & (SDMMC_INT_DRTO | 1815bdb9a90bSaddy ke SDMMC_INT_EBE))) 181690c2143aSSeungwon Jeon send_stop_abort(host, data); 1817f95f3850SWill Newton state = STATE_DATA_ERROR; 1818f95f3850SWill Newton break; 1819f95f3850SWill Newton } 1820f95f3850SWill Newton 1821f95f3850SWill Newton if (!test_and_clear_bit(EVENT_XFER_COMPLETE, 182257e10486SAddy Ke &host->pending_events)) { 182357e10486SAddy Ke /* 182457e10486SAddy Ke * If all data-related interrupts don't come 182557e10486SAddy Ke * within the given time in reading data state. 182657e10486SAddy Ke */ 1827*16a34574SJaehoon Chung if (host->dir_status == DW_MCI_RECV_STATUS) 182857e10486SAddy Ke dw_mci_set_drto(host); 1829f95f3850SWill Newton break; 183057e10486SAddy Ke } 1831f95f3850SWill Newton 1832f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->completed_events); 18332aa35465SDoug Anderson 18342aa35465SDoug Anderson /* 18352aa35465SDoug Anderson * Handle an EVENT_DATA_ERROR that might have shown up 18362aa35465SDoug Anderson * before the transfer completed. This might not have 18372aa35465SDoug Anderson * been caught by the check above because the interrupt 18382aa35465SDoug Anderson * could have gone off between the previous check and 18392aa35465SDoug Anderson * the check for transfer complete. 18402aa35465SDoug Anderson * 18412aa35465SDoug Anderson * Technically this ought not be needed assuming we 18422aa35465SDoug Anderson * get a DATA_COMPLETE eventually (we'll notice the 18432aa35465SDoug Anderson * error and end the request), but it shouldn't hurt. 18442aa35465SDoug Anderson * 18452aa35465SDoug Anderson * This has the advantage of sending the stop command. 18462aa35465SDoug Anderson */ 18472aa35465SDoug Anderson if (test_and_clear_bit(EVENT_DATA_ERROR, 18482aa35465SDoug Anderson &host->pending_events)) { 18492aa35465SDoug Anderson dw_mci_stop_dma(host); 1850bdb9a90bSaddy ke if (data->stop || 1851bdb9a90bSaddy ke !(host->data_status & (SDMMC_INT_DRTO | 1852bdb9a90bSaddy ke SDMMC_INT_EBE))) 18532aa35465SDoug Anderson send_stop_abort(host, data); 18542aa35465SDoug Anderson state = STATE_DATA_ERROR; 18552aa35465SDoug Anderson break; 18562aa35465SDoug Anderson } 1857f95f3850SWill Newton prev_state = state = STATE_DATA_BUSY; 18582aa35465SDoug Anderson 1859f95f3850SWill Newton /* fall through */ 1860f95f3850SWill Newton 1861f95f3850SWill Newton case STATE_DATA_BUSY: 1862f95f3850SWill Newton if (!test_and_clear_bit(EVENT_DATA_COMPLETE, 186357e10486SAddy Ke &host->pending_events)) { 186457e10486SAddy Ke /* 186557e10486SAddy Ke * If data error interrupt comes but data over 186657e10486SAddy Ke * interrupt doesn't come within the given time. 186757e10486SAddy Ke * in reading data state. 186857e10486SAddy Ke */ 1869*16a34574SJaehoon Chung if (host->dir_status == DW_MCI_RECV_STATUS) 187057e10486SAddy Ke dw_mci_set_drto(host); 1871f95f3850SWill Newton break; 187257e10486SAddy Ke } 1873f95f3850SWill Newton 1874f95f3850SWill Newton host->data = NULL; 1875f95f3850SWill Newton set_bit(EVENT_DATA_COMPLETE, &host->completed_events); 1876e352c813SSeungwon Jeon err = dw_mci_data_complete(host, data); 1877f95f3850SWill Newton 1878e352c813SSeungwon Jeon if (!err) { 1879e352c813SSeungwon Jeon if (!data->stop || mrq->sbc) { 188017c8bc85SSachin Kamat if (mrq->sbc && data->stop) 1881053b3ce6SSeungwon Jeon data->stop->error = 0; 1882e352c813SSeungwon Jeon dw_mci_request_end(host, mrq); 1883053b3ce6SSeungwon Jeon goto unlock; 1884053b3ce6SSeungwon Jeon } 1885053b3ce6SSeungwon Jeon 188690c2143aSSeungwon Jeon /* stop command for open-ended transfer*/ 1887e352c813SSeungwon Jeon if (data->stop) 188890c2143aSSeungwon Jeon send_stop_abort(host, data); 18892aa35465SDoug Anderson } else { 18902aa35465SDoug Anderson /* 18912aa35465SDoug Anderson * If we don't have a command complete now we'll 18922aa35465SDoug Anderson * never get one since we just reset everything; 18932aa35465SDoug Anderson * better end the request. 18942aa35465SDoug Anderson * 18952aa35465SDoug Anderson * If we do have a command complete we'll fall 18962aa35465SDoug Anderson * through to the SENDING_STOP command and 18972aa35465SDoug Anderson * everything will be peachy keen. 18982aa35465SDoug Anderson */ 18992aa35465SDoug Anderson if (!test_bit(EVENT_CMD_COMPLETE, 19002aa35465SDoug Anderson &host->pending_events)) { 19012aa35465SDoug Anderson host->cmd = NULL; 19022aa35465SDoug Anderson dw_mci_request_end(host, mrq); 19032aa35465SDoug Anderson goto unlock; 19042aa35465SDoug Anderson } 190590c2143aSSeungwon Jeon } 1906e352c813SSeungwon Jeon 1907e352c813SSeungwon Jeon /* 1908e352c813SSeungwon Jeon * If err has non-zero, 1909e352c813SSeungwon Jeon * stop-abort command has been already issued. 1910e352c813SSeungwon Jeon */ 1911e352c813SSeungwon Jeon prev_state = state = STATE_SENDING_STOP; 1912e352c813SSeungwon Jeon 1913f95f3850SWill Newton /* fall through */ 1914f95f3850SWill Newton 1915f95f3850SWill Newton case STATE_SENDING_STOP: 1916f95f3850SWill Newton if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 1917f95f3850SWill Newton &host->pending_events)) 1918f95f3850SWill Newton break; 1919f95f3850SWill Newton 192071abb133SSeungwon Jeon /* CMD error in data command */ 192131bff450SSeungwon Jeon if (mrq->cmd->error && mrq->data) 19223a33a94cSSonny Rao dw_mci_reset(host); 192371abb133SSeungwon Jeon 1924f95f3850SWill Newton host->cmd = NULL; 192571abb133SSeungwon Jeon host->data = NULL; 192690c2143aSSeungwon Jeon 1927e352c813SSeungwon Jeon if (mrq->stop) 1928e352c813SSeungwon Jeon dw_mci_command_complete(host, mrq->stop); 192990c2143aSSeungwon Jeon else 193090c2143aSSeungwon Jeon host->cmd_status = 0; 193190c2143aSSeungwon Jeon 1932e352c813SSeungwon Jeon dw_mci_request_end(host, mrq); 1933f95f3850SWill Newton goto unlock; 1934f95f3850SWill Newton 1935f95f3850SWill Newton case STATE_DATA_ERROR: 1936f95f3850SWill Newton if (!test_and_clear_bit(EVENT_XFER_COMPLETE, 1937f95f3850SWill Newton &host->pending_events)) 1938f95f3850SWill Newton break; 1939f95f3850SWill Newton 1940f95f3850SWill Newton state = STATE_DATA_BUSY; 1941f95f3850SWill Newton break; 1942f95f3850SWill Newton } 1943f95f3850SWill Newton } while (state != prev_state); 1944f95f3850SWill Newton 1945f95f3850SWill Newton host->state = state; 1946f95f3850SWill Newton unlock: 1947f95f3850SWill Newton spin_unlock(&host->lock); 1948f95f3850SWill Newton 1949f95f3850SWill Newton } 1950f95f3850SWill Newton 195134b664a2SJames Hogan /* push final bytes to part_buf, only use during push */ 195234b664a2SJames Hogan static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt) 195334b664a2SJames Hogan { 195434b664a2SJames Hogan memcpy((void *)&host->part_buf, buf, cnt); 195534b664a2SJames Hogan host->part_buf_count = cnt; 195634b664a2SJames Hogan } 195734b664a2SJames Hogan 195834b664a2SJames Hogan /* append bytes to part_buf, only use during push */ 195934b664a2SJames Hogan static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt) 196034b664a2SJames Hogan { 196134b664a2SJames Hogan cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count); 196234b664a2SJames Hogan memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt); 196334b664a2SJames Hogan host->part_buf_count += cnt; 196434b664a2SJames Hogan return cnt; 196534b664a2SJames Hogan } 196634b664a2SJames Hogan 196734b664a2SJames Hogan /* pull first bytes from part_buf, only use during pull */ 196834b664a2SJames Hogan static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt) 196934b664a2SJames Hogan { 19700e3a22c0SShawn Lin cnt = min_t(int, cnt, host->part_buf_count); 197134b664a2SJames Hogan if (cnt) { 197234b664a2SJames Hogan memcpy(buf, (void *)&host->part_buf + host->part_buf_start, 197334b664a2SJames Hogan cnt); 197434b664a2SJames Hogan host->part_buf_count -= cnt; 197534b664a2SJames Hogan host->part_buf_start += cnt; 197634b664a2SJames Hogan } 197734b664a2SJames Hogan return cnt; 197834b664a2SJames Hogan } 197934b664a2SJames Hogan 198034b664a2SJames Hogan /* pull final bytes from the part_buf, assuming it's just been filled */ 198134b664a2SJames Hogan static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt) 198234b664a2SJames Hogan { 198334b664a2SJames Hogan memcpy(buf, &host->part_buf, cnt); 198434b664a2SJames Hogan host->part_buf_start = cnt; 198534b664a2SJames Hogan host->part_buf_count = (1 << host->data_shift) - cnt; 198634b664a2SJames Hogan } 198734b664a2SJames Hogan 1988f95f3850SWill Newton static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt) 1989f95f3850SWill Newton { 1990cfbeb59cSMarkos Chandras struct mmc_data *data = host->data; 1991cfbeb59cSMarkos Chandras int init_cnt = cnt; 1992cfbeb59cSMarkos Chandras 199334b664a2SJames Hogan /* try and push anything in the part_buf */ 199434b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 199534b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 19960e3a22c0SShawn Lin 199734b664a2SJames Hogan buf += len; 199834b664a2SJames Hogan cnt -= len; 1999cfbeb59cSMarkos Chandras if (host->part_buf_count == 2) { 200076184ac1SBen Dooks mci_fifo_writew(host->fifo_reg, host->part_buf16); 200134b664a2SJames Hogan host->part_buf_count = 0; 200234b664a2SJames Hogan } 200334b664a2SJames Hogan } 200434b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 200534b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x1)) { 200634b664a2SJames Hogan while (cnt >= 2) { 200734b664a2SJames Hogan u16 aligned_buf[64]; 200834b664a2SJames Hogan int len = min(cnt & -2, (int)sizeof(aligned_buf)); 200934b664a2SJames Hogan int items = len >> 1; 201034b664a2SJames Hogan int i; 201134b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 201234b664a2SJames Hogan memcpy(aligned_buf, buf, len); 201334b664a2SJames Hogan buf += len; 201434b664a2SJames Hogan cnt -= len; 201534b664a2SJames Hogan /* push data from aligned buffer into fifo */ 201634b664a2SJames Hogan for (i = 0; i < items; ++i) 201776184ac1SBen Dooks mci_fifo_writew(host->fifo_reg, aligned_buf[i]); 201834b664a2SJames Hogan } 201934b664a2SJames Hogan } else 202034b664a2SJames Hogan #endif 202134b664a2SJames Hogan { 202234b664a2SJames Hogan u16 *pdata = buf; 20230e3a22c0SShawn Lin 202434b664a2SJames Hogan for (; cnt >= 2; cnt -= 2) 202576184ac1SBen Dooks mci_fifo_writew(host->fifo_reg, *pdata++); 202634b664a2SJames Hogan buf = pdata; 202734b664a2SJames Hogan } 202834b664a2SJames Hogan /* put anything remaining in the part_buf */ 202934b664a2SJames Hogan if (cnt) { 203034b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 2031cfbeb59cSMarkos Chandras /* Push data if we have reached the expected data length */ 2032cfbeb59cSMarkos Chandras if ((data->bytes_xfered + init_cnt) == 2033cfbeb59cSMarkos Chandras (data->blksz * data->blocks)) 203476184ac1SBen Dooks mci_fifo_writew(host->fifo_reg, host->part_buf16); 2035f95f3850SWill Newton } 2036f95f3850SWill Newton } 2037f95f3850SWill Newton 2038f95f3850SWill Newton static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt) 2039f95f3850SWill Newton { 204034b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 204134b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x1)) { 204234b664a2SJames Hogan while (cnt >= 2) { 204334b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 204434b664a2SJames Hogan u16 aligned_buf[64]; 204534b664a2SJames Hogan int len = min(cnt & -2, (int)sizeof(aligned_buf)); 204634b664a2SJames Hogan int items = len >> 1; 204734b664a2SJames Hogan int i; 20480e3a22c0SShawn Lin 204934b664a2SJames Hogan for (i = 0; i < items; ++i) 205076184ac1SBen Dooks aligned_buf[i] = mci_fifo_readw(host->fifo_reg); 205134b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 205234b664a2SJames Hogan memcpy(buf, aligned_buf, len); 205334b664a2SJames Hogan buf += len; 205434b664a2SJames Hogan cnt -= len; 205534b664a2SJames Hogan } 205634b664a2SJames Hogan } else 205734b664a2SJames Hogan #endif 205834b664a2SJames Hogan { 205934b664a2SJames Hogan u16 *pdata = buf; 20600e3a22c0SShawn Lin 206134b664a2SJames Hogan for (; cnt >= 2; cnt -= 2) 206276184ac1SBen Dooks *pdata++ = mci_fifo_readw(host->fifo_reg); 206334b664a2SJames Hogan buf = pdata; 206434b664a2SJames Hogan } 206534b664a2SJames Hogan if (cnt) { 206676184ac1SBen Dooks host->part_buf16 = mci_fifo_readw(host->fifo_reg); 206734b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 2068f95f3850SWill Newton } 2069f95f3850SWill Newton } 2070f95f3850SWill Newton 2071f95f3850SWill Newton static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt) 2072f95f3850SWill Newton { 2073cfbeb59cSMarkos Chandras struct mmc_data *data = host->data; 2074cfbeb59cSMarkos Chandras int init_cnt = cnt; 2075cfbeb59cSMarkos Chandras 207634b664a2SJames Hogan /* try and push anything in the part_buf */ 207734b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 207834b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 20790e3a22c0SShawn Lin 208034b664a2SJames Hogan buf += len; 208134b664a2SJames Hogan cnt -= len; 2082cfbeb59cSMarkos Chandras if (host->part_buf_count == 4) { 208376184ac1SBen Dooks mci_fifo_writel(host->fifo_reg, host->part_buf32); 208434b664a2SJames Hogan host->part_buf_count = 0; 208534b664a2SJames Hogan } 208634b664a2SJames Hogan } 208734b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 208834b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x3)) { 208934b664a2SJames Hogan while (cnt >= 4) { 209034b664a2SJames Hogan u32 aligned_buf[32]; 209134b664a2SJames Hogan int len = min(cnt & -4, (int)sizeof(aligned_buf)); 209234b664a2SJames Hogan int items = len >> 2; 209334b664a2SJames Hogan int i; 209434b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 209534b664a2SJames Hogan memcpy(aligned_buf, buf, len); 209634b664a2SJames Hogan buf += len; 209734b664a2SJames Hogan cnt -= len; 209834b664a2SJames Hogan /* push data from aligned buffer into fifo */ 209934b664a2SJames Hogan for (i = 0; i < items; ++i) 210076184ac1SBen Dooks mci_fifo_writel(host->fifo_reg, aligned_buf[i]); 210134b664a2SJames Hogan } 210234b664a2SJames Hogan } else 210334b664a2SJames Hogan #endif 210434b664a2SJames Hogan { 210534b664a2SJames Hogan u32 *pdata = buf; 21060e3a22c0SShawn Lin 210734b664a2SJames Hogan for (; cnt >= 4; cnt -= 4) 210876184ac1SBen Dooks mci_fifo_writel(host->fifo_reg, *pdata++); 210934b664a2SJames Hogan buf = pdata; 211034b664a2SJames Hogan } 211134b664a2SJames Hogan /* put anything remaining in the part_buf */ 211234b664a2SJames Hogan if (cnt) { 211334b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 2114cfbeb59cSMarkos Chandras /* Push data if we have reached the expected data length */ 2115cfbeb59cSMarkos Chandras if ((data->bytes_xfered + init_cnt) == 2116cfbeb59cSMarkos Chandras (data->blksz * data->blocks)) 211776184ac1SBen Dooks mci_fifo_writel(host->fifo_reg, host->part_buf32); 2118f95f3850SWill Newton } 2119f95f3850SWill Newton } 2120f95f3850SWill Newton 2121f95f3850SWill Newton static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt) 2122f95f3850SWill Newton { 212334b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 212434b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x3)) { 212534b664a2SJames Hogan while (cnt >= 4) { 212634b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 212734b664a2SJames Hogan u32 aligned_buf[32]; 212834b664a2SJames Hogan int len = min(cnt & -4, (int)sizeof(aligned_buf)); 212934b664a2SJames Hogan int items = len >> 2; 213034b664a2SJames Hogan int i; 21310e3a22c0SShawn Lin 213234b664a2SJames Hogan for (i = 0; i < items; ++i) 213376184ac1SBen Dooks aligned_buf[i] = mci_fifo_readl(host->fifo_reg); 213434b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 213534b664a2SJames Hogan memcpy(buf, aligned_buf, len); 213634b664a2SJames Hogan buf += len; 213734b664a2SJames Hogan cnt -= len; 213834b664a2SJames Hogan } 213934b664a2SJames Hogan } else 214034b664a2SJames Hogan #endif 214134b664a2SJames Hogan { 214234b664a2SJames Hogan u32 *pdata = buf; 21430e3a22c0SShawn Lin 214434b664a2SJames Hogan for (; cnt >= 4; cnt -= 4) 214576184ac1SBen Dooks *pdata++ = mci_fifo_readl(host->fifo_reg); 214634b664a2SJames Hogan buf = pdata; 214734b664a2SJames Hogan } 214834b664a2SJames Hogan if (cnt) { 214976184ac1SBen Dooks host->part_buf32 = mci_fifo_readl(host->fifo_reg); 215034b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 2151f95f3850SWill Newton } 2152f95f3850SWill Newton } 2153f95f3850SWill Newton 2154f95f3850SWill Newton static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) 2155f95f3850SWill Newton { 2156cfbeb59cSMarkos Chandras struct mmc_data *data = host->data; 2157cfbeb59cSMarkos Chandras int init_cnt = cnt; 2158cfbeb59cSMarkos Chandras 215934b664a2SJames Hogan /* try and push anything in the part_buf */ 216034b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 216134b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 21620e3a22c0SShawn Lin 216334b664a2SJames Hogan buf += len; 216434b664a2SJames Hogan cnt -= len; 2165c09fbd74SSeungwon Jeon 2166cfbeb59cSMarkos Chandras if (host->part_buf_count == 8) { 216776184ac1SBen Dooks mci_fifo_writeq(host->fifo_reg, host->part_buf); 216834b664a2SJames Hogan host->part_buf_count = 0; 216934b664a2SJames Hogan } 217034b664a2SJames Hogan } 217134b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 217234b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x7)) { 217334b664a2SJames Hogan while (cnt >= 8) { 217434b664a2SJames Hogan u64 aligned_buf[16]; 217534b664a2SJames Hogan int len = min(cnt & -8, (int)sizeof(aligned_buf)); 217634b664a2SJames Hogan int items = len >> 3; 217734b664a2SJames Hogan int i; 217834b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 217934b664a2SJames Hogan memcpy(aligned_buf, buf, len); 218034b664a2SJames Hogan buf += len; 218134b664a2SJames Hogan cnt -= len; 218234b664a2SJames Hogan /* push data from aligned buffer into fifo */ 218334b664a2SJames Hogan for (i = 0; i < items; ++i) 218476184ac1SBen Dooks mci_fifo_writeq(host->fifo_reg, aligned_buf[i]); 218534b664a2SJames Hogan } 218634b664a2SJames Hogan } else 218734b664a2SJames Hogan #endif 218834b664a2SJames Hogan { 218934b664a2SJames Hogan u64 *pdata = buf; 21900e3a22c0SShawn Lin 219134b664a2SJames Hogan for (; cnt >= 8; cnt -= 8) 219276184ac1SBen Dooks mci_fifo_writeq(host->fifo_reg, *pdata++); 219334b664a2SJames Hogan buf = pdata; 219434b664a2SJames Hogan } 219534b664a2SJames Hogan /* put anything remaining in the part_buf */ 219634b664a2SJames Hogan if (cnt) { 219734b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 2198cfbeb59cSMarkos Chandras /* Push data if we have reached the expected data length */ 2199cfbeb59cSMarkos Chandras if ((data->bytes_xfered + init_cnt) == 2200cfbeb59cSMarkos Chandras (data->blksz * data->blocks)) 220176184ac1SBen Dooks mci_fifo_writeq(host->fifo_reg, host->part_buf); 2202f95f3850SWill Newton } 2203f95f3850SWill Newton } 2204f95f3850SWill Newton 2205f95f3850SWill Newton static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt) 2206f95f3850SWill Newton { 220734b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 220834b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x7)) { 220934b664a2SJames Hogan while (cnt >= 8) { 221034b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 221134b664a2SJames Hogan u64 aligned_buf[16]; 221234b664a2SJames Hogan int len = min(cnt & -8, (int)sizeof(aligned_buf)); 221334b664a2SJames Hogan int items = len >> 3; 221434b664a2SJames Hogan int i; 22150e3a22c0SShawn Lin 221634b664a2SJames Hogan for (i = 0; i < items; ++i) 221776184ac1SBen Dooks aligned_buf[i] = mci_fifo_readq(host->fifo_reg); 221876184ac1SBen Dooks 221934b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 222034b664a2SJames Hogan memcpy(buf, aligned_buf, len); 222134b664a2SJames Hogan buf += len; 222234b664a2SJames Hogan cnt -= len; 2223f95f3850SWill Newton } 222434b664a2SJames Hogan } else 222534b664a2SJames Hogan #endif 222634b664a2SJames Hogan { 222734b664a2SJames Hogan u64 *pdata = buf; 22280e3a22c0SShawn Lin 222934b664a2SJames Hogan for (; cnt >= 8; cnt -= 8) 223076184ac1SBen Dooks *pdata++ = mci_fifo_readq(host->fifo_reg); 223134b664a2SJames Hogan buf = pdata; 223234b664a2SJames Hogan } 223334b664a2SJames Hogan if (cnt) { 223476184ac1SBen Dooks host->part_buf = mci_fifo_readq(host->fifo_reg); 223534b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 223634b664a2SJames Hogan } 223734b664a2SJames Hogan } 223834b664a2SJames Hogan 223934b664a2SJames Hogan static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt) 224034b664a2SJames Hogan { 224134b664a2SJames Hogan int len; 224234b664a2SJames Hogan 224334b664a2SJames Hogan /* get remaining partial bytes */ 224434b664a2SJames Hogan len = dw_mci_pull_part_bytes(host, buf, cnt); 224534b664a2SJames Hogan if (unlikely(len == cnt)) 224634b664a2SJames Hogan return; 224734b664a2SJames Hogan buf += len; 224834b664a2SJames Hogan cnt -= len; 224934b664a2SJames Hogan 225034b664a2SJames Hogan /* get the rest of the data */ 225134b664a2SJames Hogan host->pull_data(host, buf, cnt); 2252f95f3850SWill Newton } 2253f95f3850SWill Newton 225487a74d39SKyoungil Kim static void dw_mci_read_data_pio(struct dw_mci *host, bool dto) 2255f95f3850SWill Newton { 2256f9c2a0dcSSeungwon Jeon struct sg_mapping_iter *sg_miter = &host->sg_miter; 2257f9c2a0dcSSeungwon Jeon void *buf; 2258f9c2a0dcSSeungwon Jeon unsigned int offset; 2259f95f3850SWill Newton struct mmc_data *data = host->data; 2260f95f3850SWill Newton int shift = host->data_shift; 2261f95f3850SWill Newton u32 status; 22623e4b0d8bSMarkos Chandras unsigned int len; 2263f9c2a0dcSSeungwon Jeon unsigned int remain, fcnt; 2264f95f3850SWill Newton 2265f95f3850SWill Newton do { 2266f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 2267f9c2a0dcSSeungwon Jeon goto done; 2268f95f3850SWill Newton 22694225fc85SImre Deak host->sg = sg_miter->piter.sg; 2270f9c2a0dcSSeungwon Jeon buf = sg_miter->addr; 2271f9c2a0dcSSeungwon Jeon remain = sg_miter->length; 2272f9c2a0dcSSeungwon Jeon offset = 0; 2273f9c2a0dcSSeungwon Jeon 2274f9c2a0dcSSeungwon Jeon do { 2275f9c2a0dcSSeungwon Jeon fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS)) 2276f9c2a0dcSSeungwon Jeon << shift) + host->part_buf_count; 2277f9c2a0dcSSeungwon Jeon len = min(remain, fcnt); 2278f9c2a0dcSSeungwon Jeon if (!len) 2279f9c2a0dcSSeungwon Jeon break; 2280f9c2a0dcSSeungwon Jeon dw_mci_pull_data(host, (void *)(buf + offset), len); 22813e4b0d8bSMarkos Chandras data->bytes_xfered += len; 2282f95f3850SWill Newton offset += len; 2283f9c2a0dcSSeungwon Jeon remain -= len; 2284f9c2a0dcSSeungwon Jeon } while (remain); 2285f95f3850SWill Newton 2286e74f3a9cSSeungwon Jeon sg_miter->consumed = offset; 2287f95f3850SWill Newton status = mci_readl(host, MINTSTS); 2288f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_RXDR); 228987a74d39SKyoungil Kim /* if the RXDR is ready read again */ 229087a74d39SKyoungil Kim } while ((status & SDMMC_INT_RXDR) || 229187a74d39SKyoungil Kim (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS)))); 2292f9c2a0dcSSeungwon Jeon 2293f9c2a0dcSSeungwon Jeon if (!remain) { 2294f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 2295f9c2a0dcSSeungwon Jeon goto done; 2296f9c2a0dcSSeungwon Jeon sg_miter->consumed = 0; 2297f9c2a0dcSSeungwon Jeon } 2298f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 2299f95f3850SWill Newton return; 2300f95f3850SWill Newton 2301f95f3850SWill Newton done: 2302f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 2303f9c2a0dcSSeungwon Jeon host->sg = NULL; 23040e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2305f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 2306f95f3850SWill Newton } 2307f95f3850SWill Newton 2308f95f3850SWill Newton static void dw_mci_write_data_pio(struct dw_mci *host) 2309f95f3850SWill Newton { 2310f9c2a0dcSSeungwon Jeon struct sg_mapping_iter *sg_miter = &host->sg_miter; 2311f9c2a0dcSSeungwon Jeon void *buf; 2312f9c2a0dcSSeungwon Jeon unsigned int offset; 2313f95f3850SWill Newton struct mmc_data *data = host->data; 2314f95f3850SWill Newton int shift = host->data_shift; 2315f95f3850SWill Newton u32 status; 23163e4b0d8bSMarkos Chandras unsigned int len; 2317f9c2a0dcSSeungwon Jeon unsigned int fifo_depth = host->fifo_depth; 2318f9c2a0dcSSeungwon Jeon unsigned int remain, fcnt; 2319f95f3850SWill Newton 2320f95f3850SWill Newton do { 2321f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 2322f9c2a0dcSSeungwon Jeon goto done; 2323f95f3850SWill Newton 23244225fc85SImre Deak host->sg = sg_miter->piter.sg; 2325f9c2a0dcSSeungwon Jeon buf = sg_miter->addr; 2326f9c2a0dcSSeungwon Jeon remain = sg_miter->length; 2327f9c2a0dcSSeungwon Jeon offset = 0; 2328f9c2a0dcSSeungwon Jeon 2329f9c2a0dcSSeungwon Jeon do { 2330f9c2a0dcSSeungwon Jeon fcnt = ((fifo_depth - 2331f9c2a0dcSSeungwon Jeon SDMMC_GET_FCNT(mci_readl(host, STATUS))) 2332f9c2a0dcSSeungwon Jeon << shift) - host->part_buf_count; 2333f9c2a0dcSSeungwon Jeon len = min(remain, fcnt); 2334f9c2a0dcSSeungwon Jeon if (!len) 2335f9c2a0dcSSeungwon Jeon break; 2336f9c2a0dcSSeungwon Jeon host->push_data(host, (void *)(buf + offset), len); 23373e4b0d8bSMarkos Chandras data->bytes_xfered += len; 2338f95f3850SWill Newton offset += len; 2339f9c2a0dcSSeungwon Jeon remain -= len; 2340f9c2a0dcSSeungwon Jeon } while (remain); 2341f95f3850SWill Newton 2342e74f3a9cSSeungwon Jeon sg_miter->consumed = offset; 2343f95f3850SWill Newton status = mci_readl(host, MINTSTS); 2344f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_TXDR); 2345f95f3850SWill Newton } while (status & SDMMC_INT_TXDR); /* if TXDR write again */ 2346f9c2a0dcSSeungwon Jeon 2347f9c2a0dcSSeungwon Jeon if (!remain) { 2348f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 2349f9c2a0dcSSeungwon Jeon goto done; 2350f9c2a0dcSSeungwon Jeon sg_miter->consumed = 0; 2351f9c2a0dcSSeungwon Jeon } 2352f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 2353f95f3850SWill Newton return; 2354f95f3850SWill Newton 2355f95f3850SWill Newton done: 2356f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 2357f9c2a0dcSSeungwon Jeon host->sg = NULL; 23580e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2359f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 2360f95f3850SWill Newton } 2361f95f3850SWill Newton 2362f95f3850SWill Newton static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) 2363f95f3850SWill Newton { 2364f95f3850SWill Newton if (!host->cmd_status) 2365f95f3850SWill Newton host->cmd_status = status; 2366f95f3850SWill Newton 23670e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2368f95f3850SWill Newton 2369f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 2370f95f3850SWill Newton tasklet_schedule(&host->tasklet); 2371f95f3850SWill Newton } 2372f95f3850SWill Newton 23736130e7a9SDoug Anderson static void dw_mci_handle_cd(struct dw_mci *host) 23746130e7a9SDoug Anderson { 23756130e7a9SDoug Anderson int i; 23766130e7a9SDoug Anderson 23776130e7a9SDoug Anderson for (i = 0; i < host->num_slots; i++) { 23786130e7a9SDoug Anderson struct dw_mci_slot *slot = host->slot[i]; 23796130e7a9SDoug Anderson 23806130e7a9SDoug Anderson if (!slot) 23816130e7a9SDoug Anderson continue; 23826130e7a9SDoug Anderson 23836130e7a9SDoug Anderson if (slot->mmc->ops->card_event) 23846130e7a9SDoug Anderson slot->mmc->ops->card_event(slot->mmc); 23856130e7a9SDoug Anderson mmc_detect_change(slot->mmc, 23866130e7a9SDoug Anderson msecs_to_jiffies(host->pdata->detect_delay_ms)); 23876130e7a9SDoug Anderson } 23886130e7a9SDoug Anderson } 23896130e7a9SDoug Anderson 2390f95f3850SWill Newton static irqreturn_t dw_mci_interrupt(int irq, void *dev_id) 2391f95f3850SWill Newton { 2392f95f3850SWill Newton struct dw_mci *host = dev_id; 2393182c9081SSeungwon Jeon u32 pending; 23941a5c8e1fSShashidhar Hiremath int i; 2395f95f3850SWill Newton 2396f95f3850SWill Newton pending = mci_readl(host, MINTSTS); /* read-only mask reg */ 2397f95f3850SWill Newton 2398476d79f1SDoug Anderson if (pending) { 239901730558SDoug Anderson /* Check volt switch first, since it can look like an error */ 240001730558SDoug Anderson if ((host->state == STATE_SENDING_CMD11) && 240101730558SDoug Anderson (pending & SDMMC_INT_VOLT_SWITCH)) { 240249ba0302SDoug Anderson unsigned long irqflags; 24035c935165SDoug Anderson 240401730558SDoug Anderson mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH); 240501730558SDoug Anderson pending &= ~SDMMC_INT_VOLT_SWITCH; 240649ba0302SDoug Anderson 240749ba0302SDoug Anderson /* 240849ba0302SDoug Anderson * Hold the lock; we know cmd11_timer can't be kicked 240949ba0302SDoug Anderson * off after the lock is released, so safe to delete. 241049ba0302SDoug Anderson */ 241149ba0302SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 241201730558SDoug Anderson dw_mci_cmd_interrupt(host, pending); 241349ba0302SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 241449ba0302SDoug Anderson 241549ba0302SDoug Anderson del_timer(&host->cmd11_timer); 241601730558SDoug Anderson } 241701730558SDoug Anderson 2418f95f3850SWill Newton if (pending & DW_MCI_CMD_ERROR_FLAGS) { 2419f95f3850SWill Newton mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); 2420182c9081SSeungwon Jeon host->cmd_status = pending; 24210e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2422f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 2423f95f3850SWill Newton } 2424f95f3850SWill Newton 2425f95f3850SWill Newton if (pending & DW_MCI_DATA_ERROR_FLAGS) { 2426f95f3850SWill Newton /* if there is an error report DATA_ERROR */ 2427f95f3850SWill Newton mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS); 2428182c9081SSeungwon Jeon host->data_status = pending; 24290e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2430f95f3850SWill Newton set_bit(EVENT_DATA_ERROR, &host->pending_events); 2431f95f3850SWill Newton tasklet_schedule(&host->tasklet); 2432f95f3850SWill Newton } 2433f95f3850SWill Newton 2434f95f3850SWill Newton if (pending & SDMMC_INT_DATA_OVER) { 243557e10486SAddy Ke del_timer(&host->dto_timer); 243657e10486SAddy Ke 2437f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER); 2438f95f3850SWill Newton if (!host->data_status) 2439182c9081SSeungwon Jeon host->data_status = pending; 24400e3a22c0SShawn Lin smp_wmb(); /* drain writebuffer */ 2441f95f3850SWill Newton if (host->dir_status == DW_MCI_RECV_STATUS) { 2442f95f3850SWill Newton if (host->sg != NULL) 244387a74d39SKyoungil Kim dw_mci_read_data_pio(host, true); 2444f95f3850SWill Newton } 2445f95f3850SWill Newton set_bit(EVENT_DATA_COMPLETE, &host->pending_events); 2446f95f3850SWill Newton tasklet_schedule(&host->tasklet); 2447f95f3850SWill Newton } 2448f95f3850SWill Newton 2449f95f3850SWill Newton if (pending & SDMMC_INT_RXDR) { 2450f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_RXDR); 2451b40af3aaSJames Hogan if (host->dir_status == DW_MCI_RECV_STATUS && host->sg) 245287a74d39SKyoungil Kim dw_mci_read_data_pio(host, false); 2453f95f3850SWill Newton } 2454f95f3850SWill Newton 2455f95f3850SWill Newton if (pending & SDMMC_INT_TXDR) { 2456f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_TXDR); 2457b40af3aaSJames Hogan if (host->dir_status == DW_MCI_SEND_STATUS && host->sg) 2458f95f3850SWill Newton dw_mci_write_data_pio(host); 2459f95f3850SWill Newton } 2460f95f3850SWill Newton 2461f95f3850SWill Newton if (pending & SDMMC_INT_CMD_DONE) { 2462f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); 2463182c9081SSeungwon Jeon dw_mci_cmd_interrupt(host, pending); 2464f95f3850SWill Newton } 2465f95f3850SWill Newton 2466f95f3850SWill Newton if (pending & SDMMC_INT_CD) { 2467f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_CD); 24686130e7a9SDoug Anderson dw_mci_handle_cd(host); 2469f95f3850SWill Newton } 2470f95f3850SWill Newton 24711a5c8e1fSShashidhar Hiremath /* Handle SDIO Interrupts */ 24721a5c8e1fSShashidhar Hiremath for (i = 0; i < host->num_slots; i++) { 24731a5c8e1fSShashidhar Hiremath struct dw_mci_slot *slot = host->slot[i]; 2474ed2540efSDoug Anderson 2475ed2540efSDoug Anderson if (!slot) 2476ed2540efSDoug Anderson continue; 2477ed2540efSDoug Anderson 247876756234SAddy Ke if (pending & SDMMC_INT_SDIO(slot->sdio_id)) { 247976756234SAddy Ke mci_writel(host, RINTSTS, 248076756234SAddy Ke SDMMC_INT_SDIO(slot->sdio_id)); 24811a5c8e1fSShashidhar Hiremath mmc_signal_sdio_irq(slot->mmc); 24821a5c8e1fSShashidhar Hiremath } 24831a5c8e1fSShashidhar Hiremath } 24841a5c8e1fSShashidhar Hiremath 24851fb5f68aSMarkos Chandras } 2486f95f3850SWill Newton 24873fc7eaefSShawn Lin if (host->use_dma != TRANS_MODE_IDMAC) 24883fc7eaefSShawn Lin return IRQ_HANDLED; 24893fc7eaefSShawn Lin 24903fc7eaefSShawn Lin /* Handle IDMA interrupts */ 249169d99fdcSPrabu Thangamuthu if (host->dma_64bit_address == 1) { 249269d99fdcSPrabu Thangamuthu pending = mci_readl(host, IDSTS64); 249369d99fdcSPrabu Thangamuthu if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { 249469d99fdcSPrabu Thangamuthu mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI | 249569d99fdcSPrabu Thangamuthu SDMMC_IDMAC_INT_RI); 249669d99fdcSPrabu Thangamuthu mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI); 24973fc7eaefSShawn Lin host->dma_ops->complete((void *)host); 249869d99fdcSPrabu Thangamuthu } 249969d99fdcSPrabu Thangamuthu } else { 2500f95f3850SWill Newton pending = mci_readl(host, IDSTS); 2501f95f3850SWill Newton if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { 250269d99fdcSPrabu Thangamuthu mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | 250369d99fdcSPrabu Thangamuthu SDMMC_IDMAC_INT_RI); 2504f95f3850SWill Newton mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI); 25053fc7eaefSShawn Lin host->dma_ops->complete((void *)host); 2506f95f3850SWill Newton } 250769d99fdcSPrabu Thangamuthu } 2508f95f3850SWill Newton 2509f95f3850SWill Newton return IRQ_HANDLED; 2510f95f3850SWill Newton } 2511f95f3850SWill Newton 2512c91eab4bSThomas Abraham #ifdef CONFIG_OF 2513eff8f2f5SLars-Peter Clausen /* given a slot, find out the device node representing that slot */ 2514eff8f2f5SLars-Peter Clausen static struct device_node *dw_mci_of_find_slot_node(struct dw_mci_slot *slot) 2515c91eab4bSThomas Abraham { 2516eff8f2f5SLars-Peter Clausen struct device *dev = slot->mmc->parent; 2517c91eab4bSThomas Abraham struct device_node *np; 2518c91eab4bSThomas Abraham const __be32 *addr; 2519c91eab4bSThomas Abraham int len; 2520c91eab4bSThomas Abraham 2521c91eab4bSThomas Abraham if (!dev || !dev->of_node) 2522c91eab4bSThomas Abraham return NULL; 2523c91eab4bSThomas Abraham 2524c91eab4bSThomas Abraham for_each_child_of_node(dev->of_node, np) { 2525c91eab4bSThomas Abraham addr = of_get_property(np, "reg", &len); 2526c91eab4bSThomas Abraham if (!addr || (len < sizeof(int))) 2527c91eab4bSThomas Abraham continue; 2528eff8f2f5SLars-Peter Clausen if (be32_to_cpup(addr) == slot->id) 2529c91eab4bSThomas Abraham return np; 2530c91eab4bSThomas Abraham } 2531c91eab4bSThomas Abraham return NULL; 2532c91eab4bSThomas Abraham } 2533c91eab4bSThomas Abraham 2534eff8f2f5SLars-Peter Clausen static void dw_mci_slot_of_parse(struct dw_mci_slot *slot) 2535a70aaa64SDoug Anderson { 2536eff8f2f5SLars-Peter Clausen struct device_node *np = dw_mci_of_find_slot_node(slot); 2537a70aaa64SDoug Anderson 2538eff8f2f5SLars-Peter Clausen if (!np) 2539eff8f2f5SLars-Peter Clausen return; 2540a70aaa64SDoug Anderson 2541eff8f2f5SLars-Peter Clausen if (of_property_read_bool(np, "disable-wp")) { 2542eff8f2f5SLars-Peter Clausen slot->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT; 2543eff8f2f5SLars-Peter Clausen dev_warn(slot->mmc->parent, 2544eff8f2f5SLars-Peter Clausen "Slot quirk 'disable-wp' is deprecated\n"); 254526375b5cSJaehoon Chung } 2546a70aaa64SDoug Anderson } 2547c91eab4bSThomas Abraham #else /* CONFIG_OF */ 2548eff8f2f5SLars-Peter Clausen static void dw_mci_slot_of_parse(struct dw_mci_slot *slot) 2549a70aaa64SDoug Anderson { 2550a70aaa64SDoug Anderson } 2551c91eab4bSThomas Abraham #endif /* CONFIG_OF */ 2552c91eab4bSThomas Abraham 255336c179a9SJaehoon Chung static int dw_mci_init_slot(struct dw_mci *host, unsigned int id) 2554f95f3850SWill Newton { 2555f95f3850SWill Newton struct mmc_host *mmc; 2556f95f3850SWill Newton struct dw_mci_slot *slot; 2557e95baf13SArnd Bergmann const struct dw_mci_drv_data *drv_data = host->drv_data; 2558800d78bfSThomas Abraham int ctrl_id, ret; 25591f44a2a5SSeungwon Jeon u32 freq[2]; 2560f95f3850SWill Newton 25614a90920cSThomas Abraham mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev); 2562f95f3850SWill Newton if (!mmc) 2563f95f3850SWill Newton return -ENOMEM; 2564f95f3850SWill Newton 2565f95f3850SWill Newton slot = mmc_priv(mmc); 2566f95f3850SWill Newton slot->id = id; 256776756234SAddy Ke slot->sdio_id = host->sdio_id0 + id; 2568f95f3850SWill Newton slot->mmc = mmc; 2569f95f3850SWill Newton slot->host = host; 2570c91eab4bSThomas Abraham host->slot[id] = slot; 2571f95f3850SWill Newton 2572f95f3850SWill Newton mmc->ops = &dw_mci_ops; 25731f44a2a5SSeungwon Jeon if (of_property_read_u32_array(host->dev->of_node, 25741f44a2a5SSeungwon Jeon "clock-freq-min-max", freq, 2)) { 25751f44a2a5SSeungwon Jeon mmc->f_min = DW_MCI_FREQ_MIN; 25761f44a2a5SSeungwon Jeon mmc->f_max = DW_MCI_FREQ_MAX; 25771f44a2a5SSeungwon Jeon } else { 25781f44a2a5SSeungwon Jeon mmc->f_min = freq[0]; 25791f44a2a5SSeungwon Jeon mmc->f_max = freq[1]; 25801f44a2a5SSeungwon Jeon } 2581f95f3850SWill Newton 258251da2240SYuvaraj CD /*if there are external regulators, get them*/ 258351da2240SYuvaraj CD ret = mmc_regulator_get_supply(mmc); 258451da2240SYuvaraj CD if (ret == -EPROBE_DEFER) 25853cf890fcSDoug Anderson goto err_host_allocated; 258651da2240SYuvaraj CD 258751da2240SYuvaraj CD if (!mmc->ocr_avail) 2588f95f3850SWill Newton mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 2589f95f3850SWill Newton 2590fc3d7720SJaehoon Chung if (host->pdata->caps) 2591fc3d7720SJaehoon Chung mmc->caps = host->pdata->caps; 2592fc3d7720SJaehoon Chung 2593ab269128SAbhilash Kesavan if (host->pdata->pm_caps) 2594ab269128SAbhilash Kesavan mmc->pm_caps = host->pdata->pm_caps; 2595ab269128SAbhilash Kesavan 2596800d78bfSThomas Abraham if (host->dev->of_node) { 2597800d78bfSThomas Abraham ctrl_id = of_alias_get_id(host->dev->of_node, "mshc"); 2598800d78bfSThomas Abraham if (ctrl_id < 0) 2599800d78bfSThomas Abraham ctrl_id = 0; 2600800d78bfSThomas Abraham } else { 2601800d78bfSThomas Abraham ctrl_id = to_platform_device(host->dev)->id; 2602800d78bfSThomas Abraham } 2603cb27a843SJames Hogan if (drv_data && drv_data->caps) 2604cb27a843SJames Hogan mmc->caps |= drv_data->caps[ctrl_id]; 2605800d78bfSThomas Abraham 26064f408cc6SSeungwon Jeon if (host->pdata->caps2) 26074f408cc6SSeungwon Jeon mmc->caps2 = host->pdata->caps2; 26084f408cc6SSeungwon Jeon 2609eff8f2f5SLars-Peter Clausen dw_mci_slot_of_parse(slot); 2610eff8f2f5SLars-Peter Clausen 26113cf890fcSDoug Anderson ret = mmc_of_parse(mmc); 26123cf890fcSDoug Anderson if (ret) 26133cf890fcSDoug Anderson goto err_host_allocated; 2614f95f3850SWill Newton 2615f95f3850SWill Newton /* Useful defaults if platform data is unset. */ 26163fc7eaefSShawn Lin if (host->use_dma == TRANS_MODE_IDMAC) { 2617a39e5746SJaehoon Chung mmc->max_segs = host->ring_size; 2618225faf87SJaehoon Chung mmc->max_blk_size = 65535; 2619575c319dSHeiko Stuebner mmc->max_seg_size = 0x1000; 26201a25b1b4SSeungwon Jeon mmc->max_req_size = mmc->max_seg_size * host->ring_size; 26211a25b1b4SSeungwon Jeon mmc->max_blk_count = mmc->max_req_size / 512; 26223fc7eaefSShawn Lin } else if (host->use_dma == TRANS_MODE_EDMAC) { 26233fc7eaefSShawn Lin mmc->max_segs = 64; 2624225faf87SJaehoon Chung mmc->max_blk_size = 65535; 26253fc7eaefSShawn Lin mmc->max_blk_count = 65535; 26263fc7eaefSShawn Lin mmc->max_req_size = 26273fc7eaefSShawn Lin mmc->max_blk_size * mmc->max_blk_count; 26283fc7eaefSShawn Lin mmc->max_seg_size = mmc->max_req_size; 2629575c319dSHeiko Stuebner } else { 26303fc7eaefSShawn Lin /* TRANS_MODE_PIO */ 2631f95f3850SWill Newton mmc->max_segs = 64; 2632225faf87SJaehoon Chung mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */ 2633f95f3850SWill Newton mmc->max_blk_count = 512; 2634575c319dSHeiko Stuebner mmc->max_req_size = mmc->max_blk_size * 2635575c319dSHeiko Stuebner mmc->max_blk_count; 2636f95f3850SWill Newton mmc->max_seg_size = mmc->max_req_size; 2637575c319dSHeiko Stuebner } 2638f95f3850SWill Newton 2639c0834a58SShawn Lin dw_mci_get_cd(mmc); 2640ae0eb348SJaehoon Chung 26410cea529dSJaehoon Chung ret = mmc_add_host(mmc); 26420cea529dSJaehoon Chung if (ret) 26433cf890fcSDoug Anderson goto err_host_allocated; 2644f95f3850SWill Newton 2645f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS) 2646f95f3850SWill Newton dw_mci_init_debugfs(slot); 2647f95f3850SWill Newton #endif 2648f95f3850SWill Newton 2649f95f3850SWill Newton return 0; 2650800d78bfSThomas Abraham 26513cf890fcSDoug Anderson err_host_allocated: 2652800d78bfSThomas Abraham mmc_free_host(mmc); 265351da2240SYuvaraj CD return ret; 2654f95f3850SWill Newton } 2655f95f3850SWill Newton 2656f95f3850SWill Newton static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id) 2657f95f3850SWill Newton { 2658f95f3850SWill Newton /* Debugfs stuff is cleaned up by mmc core */ 2659f95f3850SWill Newton mmc_remove_host(slot->mmc); 2660f95f3850SWill Newton slot->host->slot[id] = NULL; 2661f95f3850SWill Newton mmc_free_host(slot->mmc); 2662f95f3850SWill Newton } 2663f95f3850SWill Newton 2664f95f3850SWill Newton static void dw_mci_init_dma(struct dw_mci *host) 2665f95f3850SWill Newton { 266669d99fdcSPrabu Thangamuthu int addr_config; 26673fc7eaefSShawn Lin struct device *dev = host->dev; 26683fc7eaefSShawn Lin struct device_node *np = dev->of_node; 26693fc7eaefSShawn Lin 26703fc7eaefSShawn Lin /* 26713fc7eaefSShawn Lin * Check tansfer mode from HCON[17:16] 26723fc7eaefSShawn Lin * Clear the ambiguous description of dw_mmc databook: 26733fc7eaefSShawn Lin * 2b'00: No DMA Interface -> Actually means using Internal DMA block 26743fc7eaefSShawn Lin * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block 26753fc7eaefSShawn Lin * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block 26763fc7eaefSShawn Lin * 2b'11: Non DW DMA Interface -> pio only 26773fc7eaefSShawn Lin * Compared to DesignWare DMA Interface, Generic DMA Interface has a 26783fc7eaefSShawn Lin * simpler request/acknowledge handshake mechanism and both of them 26793fc7eaefSShawn Lin * are regarded as external dma master for dw_mmc. 26803fc7eaefSShawn Lin */ 26813fc7eaefSShawn Lin host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON)); 26823fc7eaefSShawn Lin if (host->use_dma == DMA_INTERFACE_IDMA) { 26833fc7eaefSShawn Lin host->use_dma = TRANS_MODE_IDMAC; 26843fc7eaefSShawn Lin } else if (host->use_dma == DMA_INTERFACE_DWDMA || 26853fc7eaefSShawn Lin host->use_dma == DMA_INTERFACE_GDMA) { 26863fc7eaefSShawn Lin host->use_dma = TRANS_MODE_EDMAC; 26873fc7eaefSShawn Lin } else { 26883fc7eaefSShawn Lin goto no_dma; 26893fc7eaefSShawn Lin } 26903fc7eaefSShawn Lin 26913fc7eaefSShawn Lin /* Determine which DMA interface to use */ 26923fc7eaefSShawn Lin if (host->use_dma == TRANS_MODE_IDMAC) { 26933fc7eaefSShawn Lin /* 26943fc7eaefSShawn Lin * Check ADDR_CONFIG bit in HCON to find 26953fc7eaefSShawn Lin * IDMAC address bus width 26963fc7eaefSShawn Lin */ 269770692752SShawn Lin addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON)); 269869d99fdcSPrabu Thangamuthu 269969d99fdcSPrabu Thangamuthu if (addr_config == 1) { 270069d99fdcSPrabu Thangamuthu /* host supports IDMAC in 64-bit address mode */ 270169d99fdcSPrabu Thangamuthu host->dma_64bit_address = 1; 27023fc7eaefSShawn Lin dev_info(host->dev, 27033fc7eaefSShawn Lin "IDMAC supports 64-bit address mode.\n"); 270469d99fdcSPrabu Thangamuthu if (!dma_set_mask(host->dev, DMA_BIT_MASK(64))) 27053fc7eaefSShawn Lin dma_set_coherent_mask(host->dev, 27063fc7eaefSShawn Lin DMA_BIT_MASK(64)); 270769d99fdcSPrabu Thangamuthu } else { 270869d99fdcSPrabu Thangamuthu /* host supports IDMAC in 32-bit address mode */ 270969d99fdcSPrabu Thangamuthu host->dma_64bit_address = 0; 27103fc7eaefSShawn Lin dev_info(host->dev, 27113fc7eaefSShawn Lin "IDMAC supports 32-bit address mode.\n"); 271269d99fdcSPrabu Thangamuthu } 271369d99fdcSPrabu Thangamuthu 2714f95f3850SWill Newton /* Alloc memory for sg translation */ 2715780f22afSSeungwon Jeon host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE, 2716f95f3850SWill Newton &host->sg_dma, GFP_KERNEL); 2717f95f3850SWill Newton if (!host->sg_cpu) { 27183fc7eaefSShawn Lin dev_err(host->dev, 27193fc7eaefSShawn Lin "%s: could not alloc DMA memory\n", 2720f95f3850SWill Newton __func__); 2721f95f3850SWill Newton goto no_dma; 2722f95f3850SWill Newton } 2723f95f3850SWill Newton 2724f95f3850SWill Newton host->dma_ops = &dw_mci_idmac_ops; 272500956ea3SSeungwon Jeon dev_info(host->dev, "Using internal DMA controller.\n"); 27263fc7eaefSShawn Lin } else { 27273fc7eaefSShawn Lin /* TRANS_MODE_EDMAC: check dma bindings again */ 27283fc7eaefSShawn Lin if ((of_property_count_strings(np, "dma-names") < 0) || 27293fc7eaefSShawn Lin (!of_find_property(np, "dmas", NULL))) { 2730f95f3850SWill Newton goto no_dma; 27313fc7eaefSShawn Lin } 27323fc7eaefSShawn Lin host->dma_ops = &dw_mci_edmac_ops; 27333fc7eaefSShawn Lin dev_info(host->dev, "Using external DMA controller.\n"); 27343fc7eaefSShawn Lin } 2735f95f3850SWill Newton 2736e1631f98SJaehoon Chung if (host->dma_ops->init && host->dma_ops->start && 2737e1631f98SJaehoon Chung host->dma_ops->stop && host->dma_ops->cleanup) { 2738f95f3850SWill Newton if (host->dma_ops->init(host)) { 27390e3a22c0SShawn Lin dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n", 27400e3a22c0SShawn Lin __func__); 2741f95f3850SWill Newton goto no_dma; 2742f95f3850SWill Newton } 2743f95f3850SWill Newton } else { 27444a90920cSThomas Abraham dev_err(host->dev, "DMA initialization not found.\n"); 2745f95f3850SWill Newton goto no_dma; 2746f95f3850SWill Newton } 2747f95f3850SWill Newton 2748f95f3850SWill Newton return; 2749f95f3850SWill Newton 2750f95f3850SWill Newton no_dma: 27514a90920cSThomas Abraham dev_info(host->dev, "Using PIO mode.\n"); 27523fc7eaefSShawn Lin host->use_dma = TRANS_MODE_PIO; 2753f95f3850SWill Newton } 2754f95f3850SWill Newton 275531bff450SSeungwon Jeon static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset) 2756f95f3850SWill Newton { 2757f95f3850SWill Newton unsigned long timeout = jiffies + msecs_to_jiffies(500); 275831bff450SSeungwon Jeon u32 ctrl; 2759f95f3850SWill Newton 276031bff450SSeungwon Jeon ctrl = mci_readl(host, CTRL); 276131bff450SSeungwon Jeon ctrl |= reset; 276231bff450SSeungwon Jeon mci_writel(host, CTRL, ctrl); 2763f95f3850SWill Newton 2764f95f3850SWill Newton /* wait till resets clear */ 2765f95f3850SWill Newton do { 2766f95f3850SWill Newton ctrl = mci_readl(host, CTRL); 276731bff450SSeungwon Jeon if (!(ctrl & reset)) 2768f95f3850SWill Newton return true; 2769f95f3850SWill Newton } while (time_before(jiffies, timeout)); 2770f95f3850SWill Newton 277131bff450SSeungwon Jeon dev_err(host->dev, 277231bff450SSeungwon Jeon "Timeout resetting block (ctrl reset %#x)\n", 277331bff450SSeungwon Jeon ctrl & reset); 2774f95f3850SWill Newton 2775f95f3850SWill Newton return false; 2776f95f3850SWill Newton } 2777f95f3850SWill Newton 27783a33a94cSSonny Rao static bool dw_mci_reset(struct dw_mci *host) 277931bff450SSeungwon Jeon { 27803a33a94cSSonny Rao u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET; 27813a33a94cSSonny Rao bool ret = false; 27823a33a94cSSonny Rao 278331bff450SSeungwon Jeon /* 278431bff450SSeungwon Jeon * Reseting generates a block interrupt, hence setting 278531bff450SSeungwon Jeon * the scatter-gather pointer to NULL. 278631bff450SSeungwon Jeon */ 278731bff450SSeungwon Jeon if (host->sg) { 278831bff450SSeungwon Jeon sg_miter_stop(&host->sg_miter); 278931bff450SSeungwon Jeon host->sg = NULL; 279031bff450SSeungwon Jeon } 279131bff450SSeungwon Jeon 27923a33a94cSSonny Rao if (host->use_dma) 27933a33a94cSSonny Rao flags |= SDMMC_CTRL_DMA_RESET; 27943a33a94cSSonny Rao 27953a33a94cSSonny Rao if (dw_mci_ctrl_reset(host, flags)) { 27963a33a94cSSonny Rao /* 27973a33a94cSSonny Rao * In all cases we clear the RAWINTS register to clear any 27983a33a94cSSonny Rao * interrupts. 27993a33a94cSSonny Rao */ 28003a33a94cSSonny Rao mci_writel(host, RINTSTS, 0xFFFFFFFF); 28013a33a94cSSonny Rao 28023a33a94cSSonny Rao /* if using dma we wait for dma_req to clear */ 28033a33a94cSSonny Rao if (host->use_dma) { 28043a33a94cSSonny Rao unsigned long timeout = jiffies + msecs_to_jiffies(500); 28053a33a94cSSonny Rao u32 status; 28060e3a22c0SShawn Lin 28073a33a94cSSonny Rao do { 28083a33a94cSSonny Rao status = mci_readl(host, STATUS); 28093a33a94cSSonny Rao if (!(status & SDMMC_STATUS_DMA_REQ)) 28103a33a94cSSonny Rao break; 28113a33a94cSSonny Rao cpu_relax(); 28123a33a94cSSonny Rao } while (time_before(jiffies, timeout)); 28133a33a94cSSonny Rao 28143a33a94cSSonny Rao if (status & SDMMC_STATUS_DMA_REQ) { 28153a33a94cSSonny Rao dev_err(host->dev, 28160e3a22c0SShawn Lin "%s: Timeout waiting for dma_req to clear during reset\n", 28170e3a22c0SShawn Lin __func__); 28183a33a94cSSonny Rao goto ciu_out; 281931bff450SSeungwon Jeon } 282031bff450SSeungwon Jeon 28213a33a94cSSonny Rao /* when using DMA next we reset the fifo again */ 28223a33a94cSSonny Rao if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET)) 28233a33a94cSSonny Rao goto ciu_out; 28243a33a94cSSonny Rao } 28253a33a94cSSonny Rao } else { 28263a33a94cSSonny Rao /* if the controller reset bit did clear, then set clock regs */ 28273a33a94cSSonny Rao if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) { 28280e3a22c0SShawn Lin dev_err(host->dev, 28290e3a22c0SShawn Lin "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n", 28303a33a94cSSonny Rao __func__); 28313a33a94cSSonny Rao goto ciu_out; 28323a33a94cSSonny Rao } 28333a33a94cSSonny Rao } 28343a33a94cSSonny Rao 28353fc7eaefSShawn Lin if (host->use_dma == TRANS_MODE_IDMAC) 28363a33a94cSSonny Rao /* It is also recommended that we reset and reprogram idmac */ 28373a33a94cSSonny Rao dw_mci_idmac_reset(host); 28383a33a94cSSonny Rao 28393a33a94cSSonny Rao ret = true; 28403a33a94cSSonny Rao 28413a33a94cSSonny Rao ciu_out: 28423a33a94cSSonny Rao /* After a CTRL reset we need to have CIU set clock registers */ 28433a33a94cSSonny Rao mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0); 28443a33a94cSSonny Rao 28453a33a94cSSonny Rao return ret; 284631bff450SSeungwon Jeon } 284731bff450SSeungwon Jeon 28485c935165SDoug Anderson static void dw_mci_cmd11_timer(unsigned long arg) 28495c935165SDoug Anderson { 28505c935165SDoug Anderson struct dw_mci *host = (struct dw_mci *)arg; 28515c935165SDoug Anderson 2852fd674198SDoug Anderson if (host->state != STATE_SENDING_CMD11) { 2853fd674198SDoug Anderson dev_warn(host->dev, "Unexpected CMD11 timeout\n"); 2854fd674198SDoug Anderson return; 2855fd674198SDoug Anderson } 28565c935165SDoug Anderson 28575c935165SDoug Anderson host->cmd_status = SDMMC_INT_RTO; 28585c935165SDoug Anderson set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 28595c935165SDoug Anderson tasklet_schedule(&host->tasklet); 28605c935165SDoug Anderson } 28615c935165SDoug Anderson 286257e10486SAddy Ke static void dw_mci_dto_timer(unsigned long arg) 286357e10486SAddy Ke { 286457e10486SAddy Ke struct dw_mci *host = (struct dw_mci *)arg; 286557e10486SAddy Ke 286657e10486SAddy Ke switch (host->state) { 286757e10486SAddy Ke case STATE_SENDING_DATA: 286857e10486SAddy Ke case STATE_DATA_BUSY: 286957e10486SAddy Ke /* 287057e10486SAddy Ke * If DTO interrupt does NOT come in sending data state, 287157e10486SAddy Ke * we should notify the driver to terminate current transfer 287257e10486SAddy Ke * and report a data timeout to the core. 287357e10486SAddy Ke */ 287457e10486SAddy Ke host->data_status = SDMMC_INT_DRTO; 287557e10486SAddy Ke set_bit(EVENT_DATA_ERROR, &host->pending_events); 287657e10486SAddy Ke set_bit(EVENT_DATA_COMPLETE, &host->pending_events); 287757e10486SAddy Ke tasklet_schedule(&host->tasklet); 287857e10486SAddy Ke break; 287957e10486SAddy Ke default: 288057e10486SAddy Ke break; 288157e10486SAddy Ke } 288257e10486SAddy Ke } 288357e10486SAddy Ke 2884c91eab4bSThomas Abraham #ifdef CONFIG_OF 2885c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) 2886c91eab4bSThomas Abraham { 2887c91eab4bSThomas Abraham struct dw_mci_board *pdata; 2888c91eab4bSThomas Abraham struct device *dev = host->dev; 2889c91eab4bSThomas Abraham struct device_node *np = dev->of_node; 2890e95baf13SArnd Bergmann const struct dw_mci_drv_data *drv_data = host->drv_data; 2891e8cc37b8SShawn Lin int ret; 28923c6d89eaSDoug Anderson u32 clock_frequency; 2893c91eab4bSThomas Abraham 2894c91eab4bSThomas Abraham pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 2895bf3707eaSBeomho Seo if (!pdata) 2896c91eab4bSThomas Abraham return ERR_PTR(-ENOMEM); 2897c91eab4bSThomas Abraham 2898c91eab4bSThomas Abraham /* find out number of slots supported */ 28998a629d26SShawn Lin of_property_read_u32(np, "num-slots", &pdata->num_slots); 2900c91eab4bSThomas Abraham 2901c91eab4bSThomas Abraham if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth)) 29020e3a22c0SShawn Lin dev_info(dev, 29030e3a22c0SShawn Lin "fifo-depth property not found, using value of FIFOTH register as default\n"); 2904c91eab4bSThomas Abraham 2905c91eab4bSThomas Abraham of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms); 2906c91eab4bSThomas Abraham 29073c6d89eaSDoug Anderson if (!of_property_read_u32(np, "clock-frequency", &clock_frequency)) 29083c6d89eaSDoug Anderson pdata->bus_hz = clock_frequency; 29093c6d89eaSDoug Anderson 2910cb27a843SJames Hogan if (drv_data && drv_data->parse_dt) { 2911cb27a843SJames Hogan ret = drv_data->parse_dt(host); 2912800d78bfSThomas Abraham if (ret) 2913800d78bfSThomas Abraham return ERR_PTR(ret); 2914800d78bfSThomas Abraham } 2915800d78bfSThomas Abraham 291640a7a463SJaehoon Chung if (of_find_property(np, "supports-highspeed", NULL)) { 291740a7a463SJaehoon Chung dev_info(dev, "supports-highspeed property is deprecated.\n"); 291810b49841SSeungwon Jeon pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; 291940a7a463SJaehoon Chung } 292010b49841SSeungwon Jeon 2921c91eab4bSThomas Abraham return pdata; 2922c91eab4bSThomas Abraham } 2923c91eab4bSThomas Abraham 2924c91eab4bSThomas Abraham #else /* CONFIG_OF */ 2925c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) 2926c91eab4bSThomas Abraham { 2927c91eab4bSThomas Abraham return ERR_PTR(-EINVAL); 2928c91eab4bSThomas Abraham } 2929c91eab4bSThomas Abraham #endif /* CONFIG_OF */ 2930c91eab4bSThomas Abraham 2931fa0c3283SDoug Anderson static void dw_mci_enable_cd(struct dw_mci *host) 2932fa0c3283SDoug Anderson { 2933fa0c3283SDoug Anderson unsigned long irqflags; 2934fa0c3283SDoug Anderson u32 temp; 2935fa0c3283SDoug Anderson int i; 2936e8cc37b8SShawn Lin struct dw_mci_slot *slot; 2937fa0c3283SDoug Anderson 2938e8cc37b8SShawn Lin /* 2939e8cc37b8SShawn Lin * No need for CD if all slots have a non-error GPIO 2940e8cc37b8SShawn Lin * as well as broken card detection is found. 2941e8cc37b8SShawn Lin */ 2942fa0c3283SDoug Anderson for (i = 0; i < host->num_slots; i++) { 2943e8cc37b8SShawn Lin slot = host->slot[i]; 2944e8cc37b8SShawn Lin if (slot->mmc->caps & MMC_CAP_NEEDS_POLL) 2945e8cc37b8SShawn Lin return; 2946fa0c3283SDoug Anderson 2947287980e4SArnd Bergmann if (mmc_gpio_get_cd(slot->mmc) < 0) 2948fa0c3283SDoug Anderson break; 2949fa0c3283SDoug Anderson } 2950fa0c3283SDoug Anderson if (i == host->num_slots) 2951fa0c3283SDoug Anderson return; 2952fa0c3283SDoug Anderson 2953fa0c3283SDoug Anderson spin_lock_irqsave(&host->irq_lock, irqflags); 2954fa0c3283SDoug Anderson temp = mci_readl(host, INTMASK); 2955fa0c3283SDoug Anderson temp |= SDMMC_INT_CD; 2956fa0c3283SDoug Anderson mci_writel(host, INTMASK, temp); 2957fa0c3283SDoug Anderson spin_unlock_irqrestore(&host->irq_lock, irqflags); 2958fa0c3283SDoug Anderson } 2959fa0c3283SDoug Anderson 296062ca8034SShashidhar Hiremath int dw_mci_probe(struct dw_mci *host) 2961f95f3850SWill Newton { 2962e95baf13SArnd Bergmann const struct dw_mci_drv_data *drv_data = host->drv_data; 296362ca8034SShashidhar Hiremath int width, i, ret = 0; 2964f95f3850SWill Newton u32 fifo_size; 29651c2215b7SThomas Abraham int init_slots = 0; 2966f95f3850SWill Newton 2967c91eab4bSThomas Abraham if (!host->pdata) { 2968c91eab4bSThomas Abraham host->pdata = dw_mci_parse_dt(host); 2969c91eab4bSThomas Abraham if (IS_ERR(host->pdata)) { 2970c91eab4bSThomas Abraham dev_err(host->dev, "platform data not available\n"); 2971c91eab4bSThomas Abraham return -EINVAL; 2972c91eab4bSThomas Abraham } 2973f95f3850SWill Newton } 2974f95f3850SWill Newton 2975780f22afSSeungwon Jeon host->biu_clk = devm_clk_get(host->dev, "biu"); 2976f90a0612SThomas Abraham if (IS_ERR(host->biu_clk)) { 2977f90a0612SThomas Abraham dev_dbg(host->dev, "biu clock not available\n"); 2978f90a0612SThomas Abraham } else { 2979f90a0612SThomas Abraham ret = clk_prepare_enable(host->biu_clk); 2980f90a0612SThomas Abraham if (ret) { 2981f90a0612SThomas Abraham dev_err(host->dev, "failed to enable biu clock\n"); 2982f90a0612SThomas Abraham return ret; 2983f90a0612SThomas Abraham } 2984f95f3850SWill Newton } 2985f95f3850SWill Newton 2986780f22afSSeungwon Jeon host->ciu_clk = devm_clk_get(host->dev, "ciu"); 2987f90a0612SThomas Abraham if (IS_ERR(host->ciu_clk)) { 2988f90a0612SThomas Abraham dev_dbg(host->dev, "ciu clock not available\n"); 29893c6d89eaSDoug Anderson host->bus_hz = host->pdata->bus_hz; 2990f90a0612SThomas Abraham } else { 2991f90a0612SThomas Abraham ret = clk_prepare_enable(host->ciu_clk); 2992f90a0612SThomas Abraham if (ret) { 2993f90a0612SThomas Abraham dev_err(host->dev, "failed to enable ciu clock\n"); 2994f90a0612SThomas Abraham goto err_clk_biu; 2995f90a0612SThomas Abraham } 2996f90a0612SThomas Abraham 29973c6d89eaSDoug Anderson if (host->pdata->bus_hz) { 29983c6d89eaSDoug Anderson ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz); 29993c6d89eaSDoug Anderson if (ret) 30003c6d89eaSDoug Anderson dev_warn(host->dev, 3001612de4c1SJaehoon Chung "Unable to set bus rate to %uHz\n", 30023c6d89eaSDoug Anderson host->pdata->bus_hz); 30033c6d89eaSDoug Anderson } 3004f90a0612SThomas Abraham host->bus_hz = clk_get_rate(host->ciu_clk); 30053c6d89eaSDoug Anderson } 3006f90a0612SThomas Abraham 3007612de4c1SJaehoon Chung if (!host->bus_hz) { 3008612de4c1SJaehoon Chung dev_err(host->dev, 3009612de4c1SJaehoon Chung "Platform data must supply bus speed\n"); 3010612de4c1SJaehoon Chung ret = -ENODEV; 3011612de4c1SJaehoon Chung goto err_clk_ciu; 3012612de4c1SJaehoon Chung } 3013612de4c1SJaehoon Chung 3014002f0d5cSYuvaraj Kumar C D if (drv_data && drv_data->init) { 3015002f0d5cSYuvaraj Kumar C D ret = drv_data->init(host); 3016002f0d5cSYuvaraj Kumar C D if (ret) { 3017002f0d5cSYuvaraj Kumar C D dev_err(host->dev, 3018002f0d5cSYuvaraj Kumar C D "implementation specific init failed\n"); 3019002f0d5cSYuvaraj Kumar C D goto err_clk_ciu; 3020002f0d5cSYuvaraj Kumar C D } 3021002f0d5cSYuvaraj Kumar C D } 3022002f0d5cSYuvaraj Kumar C D 30235c935165SDoug Anderson setup_timer(&host->cmd11_timer, 30245c935165SDoug Anderson dw_mci_cmd11_timer, (unsigned long)host); 30255c935165SDoug Anderson 302657e10486SAddy Ke setup_timer(&host->dto_timer, 302757e10486SAddy Ke dw_mci_dto_timer, (unsigned long)host); 302857e10486SAddy Ke 3029f95f3850SWill Newton spin_lock_init(&host->lock); 3030f8c58c11SDoug Anderson spin_lock_init(&host->irq_lock); 3031f95f3850SWill Newton INIT_LIST_HEAD(&host->queue); 3032f95f3850SWill Newton 3033f95f3850SWill Newton /* 3034f95f3850SWill Newton * Get the host data width - this assumes that HCON has been set with 3035f95f3850SWill Newton * the correct values. 3036f95f3850SWill Newton */ 303770692752SShawn Lin i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON)); 3038f95f3850SWill Newton if (!i) { 3039f95f3850SWill Newton host->push_data = dw_mci_push_data16; 3040f95f3850SWill Newton host->pull_data = dw_mci_pull_data16; 3041f95f3850SWill Newton width = 16; 3042f95f3850SWill Newton host->data_shift = 1; 3043f95f3850SWill Newton } else if (i == 2) { 3044f95f3850SWill Newton host->push_data = dw_mci_push_data64; 3045f95f3850SWill Newton host->pull_data = dw_mci_pull_data64; 3046f95f3850SWill Newton width = 64; 3047f95f3850SWill Newton host->data_shift = 3; 3048f95f3850SWill Newton } else { 3049f95f3850SWill Newton /* Check for a reserved value, and warn if it is */ 3050f95f3850SWill Newton WARN((i != 1), 3051f95f3850SWill Newton "HCON reports a reserved host data width!\n" 3052f95f3850SWill Newton "Defaulting to 32-bit access.\n"); 3053f95f3850SWill Newton host->push_data = dw_mci_push_data32; 3054f95f3850SWill Newton host->pull_data = dw_mci_pull_data32; 3055f95f3850SWill Newton width = 32; 3056f95f3850SWill Newton host->data_shift = 2; 3057f95f3850SWill Newton } 3058f95f3850SWill Newton 3059f95f3850SWill Newton /* Reset all blocks */ 30603744415cSShawn Lin if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) { 30613744415cSShawn Lin ret = -ENODEV; 30623744415cSShawn Lin goto err_clk_ciu; 30633744415cSShawn Lin } 3064141a712aSSeungwon Jeon 3065141a712aSSeungwon Jeon host->dma_ops = host->pdata->dma_ops; 3066141a712aSSeungwon Jeon dw_mci_init_dma(host); 3067f95f3850SWill Newton 3068f95f3850SWill Newton /* Clear the interrupts for the host controller */ 3069f95f3850SWill Newton mci_writel(host, RINTSTS, 0xFFFFFFFF); 3070f95f3850SWill Newton mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ 3071f95f3850SWill Newton 3072f95f3850SWill Newton /* Put in max timeout */ 3073f95f3850SWill Newton mci_writel(host, TMOUT, 0xFFFFFFFF); 3074f95f3850SWill Newton 3075f95f3850SWill Newton /* 3076f95f3850SWill Newton * FIFO threshold settings RxMark = fifo_size / 2 - 1, 3077f95f3850SWill Newton * Tx Mark = fifo_size / 2 DMA Size = 8 3078f95f3850SWill Newton */ 3079b86d8253SJames Hogan if (!host->pdata->fifo_depth) { 3080b86d8253SJames Hogan /* 3081b86d8253SJames Hogan * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may 3082b86d8253SJames Hogan * have been overwritten by the bootloader, just like we're 3083b86d8253SJames Hogan * about to do, so if you know the value for your hardware, you 3084b86d8253SJames Hogan * should put it in the platform data. 3085b86d8253SJames Hogan */ 3086f95f3850SWill Newton fifo_size = mci_readl(host, FIFOTH); 30878234e869SJaehoon Chung fifo_size = 1 + ((fifo_size >> 16) & 0xfff); 3088b86d8253SJames Hogan } else { 3089b86d8253SJames Hogan fifo_size = host->pdata->fifo_depth; 3090b86d8253SJames Hogan } 3091b86d8253SJames Hogan host->fifo_depth = fifo_size; 309252426899SSeungwon Jeon host->fifoth_val = 309352426899SSeungwon Jeon SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2); 3094e61cf118SJaehoon Chung mci_writel(host, FIFOTH, host->fifoth_val); 3095f95f3850SWill Newton 3096f95f3850SWill Newton /* disable clock to CIU */ 3097f95f3850SWill Newton mci_writel(host, CLKENA, 0); 3098f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 3099f95f3850SWill Newton 310063008768SJames Hogan /* 310163008768SJames Hogan * In 2.40a spec, Data offset is changed. 310263008768SJames Hogan * Need to check the version-id and set data-offset for DATA register. 310363008768SJames Hogan */ 310463008768SJames Hogan host->verid = SDMMC_GET_VERID(mci_readl(host, VERID)); 310563008768SJames Hogan dev_info(host->dev, "Version ID is %04x\n", host->verid); 310663008768SJames Hogan 310763008768SJames Hogan if (host->verid < DW_MMC_240A) 310876184ac1SBen Dooks host->fifo_reg = host->regs + DATA_OFFSET; 310963008768SJames Hogan else 311076184ac1SBen Dooks host->fifo_reg = host->regs + DATA_240A_OFFSET; 311163008768SJames Hogan 3112f95f3850SWill Newton tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host); 3113780f22afSSeungwon Jeon ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt, 3114780f22afSSeungwon Jeon host->irq_flags, "dw-mci", host); 3115f95f3850SWill Newton if (ret) 31166130e7a9SDoug Anderson goto err_dmaunmap; 3117f95f3850SWill Newton 3118f95f3850SWill Newton if (host->pdata->num_slots) 3119f95f3850SWill Newton host->num_slots = host->pdata->num_slots; 3120f95f3850SWill Newton else 31218a629d26SShawn Lin host->num_slots = 1; 31228a629d26SShawn Lin 31238a629d26SShawn Lin if (host->num_slots < 1 || 31248a629d26SShawn Lin host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) { 31258a629d26SShawn Lin dev_err(host->dev, 31268a629d26SShawn Lin "Platform data must supply correct num_slots.\n"); 31278a629d26SShawn Lin ret = -ENODEV; 31288a629d26SShawn Lin goto err_clk_ciu; 31298a629d26SShawn Lin } 3130f95f3850SWill Newton 31312da1d7f2SYuvaraj CD /* 3132fa0c3283SDoug Anderson * Enable interrupts for command done, data over, data empty, 31332da1d7f2SYuvaraj CD * receive ready and error such as transmit, receive timeout, crc error 31342da1d7f2SYuvaraj CD */ 31352da1d7f2SYuvaraj CD mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | 31362da1d7f2SYuvaraj CD SDMMC_INT_TXDR | SDMMC_INT_RXDR | 3137fa0c3283SDoug Anderson DW_MCI_ERROR_FLAGS); 31380e3a22c0SShawn Lin /* Enable mci interrupt */ 31390e3a22c0SShawn Lin mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); 31402da1d7f2SYuvaraj CD 31410e3a22c0SShawn Lin dev_info(host->dev, 31420e3a22c0SShawn Lin "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n", 31432da1d7f2SYuvaraj CD host->irq, width, fifo_size); 31442da1d7f2SYuvaraj CD 3145f95f3850SWill Newton /* We need at least one slot to succeed */ 3146f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 3147f95f3850SWill Newton ret = dw_mci_init_slot(host, i); 31481c2215b7SThomas Abraham if (ret) 31491c2215b7SThomas Abraham dev_dbg(host->dev, "slot %d init failed\n", i); 31501c2215b7SThomas Abraham else 31511c2215b7SThomas Abraham init_slots++; 3152f95f3850SWill Newton } 31531c2215b7SThomas Abraham 31541c2215b7SThomas Abraham if (init_slots) { 31551c2215b7SThomas Abraham dev_info(host->dev, "%d slots initialized\n", init_slots); 31561c2215b7SThomas Abraham } else { 31570e3a22c0SShawn Lin dev_dbg(host->dev, 31580e3a22c0SShawn Lin "attempted to initialize %d slots, but failed on all\n", 31590e3a22c0SShawn Lin host->num_slots); 31606130e7a9SDoug Anderson goto err_dmaunmap; 3161f95f3850SWill Newton } 3162f95f3850SWill Newton 3163b793f658SDoug Anderson /* Now that slots are all setup, we can enable card detect */ 3164b793f658SDoug Anderson dw_mci_enable_cd(host); 3165b793f658SDoug Anderson 3166f95f3850SWill Newton return 0; 3167f95f3850SWill Newton 3168f95f3850SWill Newton err_dmaunmap: 3169f95f3850SWill Newton if (host->use_dma && host->dma_ops->exit) 3170f95f3850SWill Newton host->dma_ops->exit(host); 3171f90a0612SThomas Abraham 3172f90a0612SThomas Abraham err_clk_ciu: 3173780f22afSSeungwon Jeon if (!IS_ERR(host->ciu_clk)) 3174f90a0612SThomas Abraham clk_disable_unprepare(host->ciu_clk); 3175780f22afSSeungwon Jeon 3176f90a0612SThomas Abraham err_clk_biu: 3177780f22afSSeungwon Jeon if (!IS_ERR(host->biu_clk)) 3178f90a0612SThomas Abraham clk_disable_unprepare(host->biu_clk); 3179780f22afSSeungwon Jeon 3180f95f3850SWill Newton return ret; 3181f95f3850SWill Newton } 318262ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_probe); 3183f95f3850SWill Newton 318462ca8034SShashidhar Hiremath void dw_mci_remove(struct dw_mci *host) 3185f95f3850SWill Newton { 3186f95f3850SWill Newton int i; 3187f95f3850SWill Newton 3188f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 31894a90920cSThomas Abraham dev_dbg(host->dev, "remove slot %d\n", i); 3190f95f3850SWill Newton if (host->slot[i]) 3191f95f3850SWill Newton dw_mci_cleanup_slot(host->slot[i], i); 3192f95f3850SWill Newton } 3193f95f3850SWill Newton 3194048fd7e6SPrabu Thangamuthu mci_writel(host, RINTSTS, 0xFFFFFFFF); 3195048fd7e6SPrabu Thangamuthu mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ 3196048fd7e6SPrabu Thangamuthu 3197f95f3850SWill Newton /* disable clock to CIU */ 3198f95f3850SWill Newton mci_writel(host, CLKENA, 0); 3199f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 3200f95f3850SWill Newton 3201f95f3850SWill Newton if (host->use_dma && host->dma_ops->exit) 3202f95f3850SWill Newton host->dma_ops->exit(host); 3203f95f3850SWill Newton 3204f90a0612SThomas Abraham if (!IS_ERR(host->ciu_clk)) 3205f90a0612SThomas Abraham clk_disable_unprepare(host->ciu_clk); 3206780f22afSSeungwon Jeon 3207f90a0612SThomas Abraham if (!IS_ERR(host->biu_clk)) 3208f90a0612SThomas Abraham clk_disable_unprepare(host->biu_clk); 3209f95f3850SWill Newton } 321062ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_remove); 321162ca8034SShashidhar Hiremath 321262ca8034SShashidhar Hiremath 3213f95f3850SWill Newton 32146fe8890dSJaehoon Chung #ifdef CONFIG_PM_SLEEP 3215f95f3850SWill Newton /* 3216f95f3850SWill Newton * TODO: we should probably disable the clock to the card in the suspend path. 3217f95f3850SWill Newton */ 321862ca8034SShashidhar Hiremath int dw_mci_suspend(struct dw_mci *host) 3219f95f3850SWill Newton { 32203fc7eaefSShawn Lin if (host->use_dma && host->dma_ops->exit) 32213fc7eaefSShawn Lin host->dma_ops->exit(host); 32223fc7eaefSShawn Lin 3223f95f3850SWill Newton return 0; 3224f95f3850SWill Newton } 322562ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_suspend); 3226f95f3850SWill Newton 322762ca8034SShashidhar Hiremath int dw_mci_resume(struct dw_mci *host) 3228f95f3850SWill Newton { 3229f95f3850SWill Newton int i, ret; 3230f95f3850SWill Newton 32313a33a94cSSonny Rao if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) { 3232e61cf118SJaehoon Chung ret = -ENODEV; 3233e61cf118SJaehoon Chung return ret; 3234e61cf118SJaehoon Chung } 3235e61cf118SJaehoon Chung 32363bfe619dSJonathan Kliegman if (host->use_dma && host->dma_ops->init) 3237141a712aSSeungwon Jeon host->dma_ops->init(host); 3238141a712aSSeungwon Jeon 323952426899SSeungwon Jeon /* 324052426899SSeungwon Jeon * Restore the initial value at FIFOTH register 324152426899SSeungwon Jeon * And Invalidate the prev_blksz with zero 324252426899SSeungwon Jeon */ 3243e61cf118SJaehoon Chung mci_writel(host, FIFOTH, host->fifoth_val); 324452426899SSeungwon Jeon host->prev_blksz = 0; 3245e61cf118SJaehoon Chung 32462eb2944fSDoug Anderson /* Put in max timeout */ 32472eb2944fSDoug Anderson mci_writel(host, TMOUT, 0xFFFFFFFF); 32482eb2944fSDoug Anderson 3249e61cf118SJaehoon Chung mci_writel(host, RINTSTS, 0xFFFFFFFF); 3250e61cf118SJaehoon Chung mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | 3251e61cf118SJaehoon Chung SDMMC_INT_TXDR | SDMMC_INT_RXDR | 3252fa0c3283SDoug Anderson DW_MCI_ERROR_FLAGS); 3253e61cf118SJaehoon Chung mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); 3254e61cf118SJaehoon Chung 3255f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 3256f95f3850SWill Newton struct dw_mci_slot *slot = host->slot[i]; 32570e3a22c0SShawn Lin 3258f95f3850SWill Newton if (!slot) 3259f95f3850SWill Newton continue; 3260ab269128SAbhilash Kesavan if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) { 3261ab269128SAbhilash Kesavan dw_mci_set_ios(slot->mmc, &slot->mmc->ios); 3262ab269128SAbhilash Kesavan dw_mci_setup_bus(slot, true); 3263ab269128SAbhilash Kesavan } 3264f95f3850SWill Newton } 3265fa0c3283SDoug Anderson 3266fa0c3283SDoug Anderson /* Now that slots are all setup, we can enable card detect */ 3267fa0c3283SDoug Anderson dw_mci_enable_cd(host); 3268fa0c3283SDoug Anderson 3269f95f3850SWill Newton return 0; 3270f95f3850SWill Newton } 327162ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_resume); 32726fe8890dSJaehoon Chung #endif /* CONFIG_PM_SLEEP */ 32736fe8890dSJaehoon Chung 3274f95f3850SWill Newton static int __init dw_mci_init(void) 3275f95f3850SWill Newton { 32768e1c4e4dSSachin Kamat pr_info("Synopsys Designware Multimedia Card Interface Driver\n"); 327762ca8034SShashidhar Hiremath return 0; 3278f95f3850SWill Newton } 3279f95f3850SWill Newton 3280f95f3850SWill Newton static void __exit dw_mci_exit(void) 3281f95f3850SWill Newton { 3282f95f3850SWill Newton } 3283f95f3850SWill Newton 3284f95f3850SWill Newton module_init(dw_mci_init); 3285f95f3850SWill Newton module_exit(dw_mci_exit); 3286f95f3850SWill Newton 3287f95f3850SWill Newton MODULE_DESCRIPTION("DW Multimedia Card Interface driver"); 3288f95f3850SWill Newton MODULE_AUTHOR("NXP Semiconductor VietNam"); 3289f95f3850SWill Newton MODULE_AUTHOR("Imagination Technologies Ltd"); 3290f95f3850SWill Newton MODULE_LICENSE("GPL v2"); 3291