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> 30f95f3850SWill Newton #include <linux/mmc/host.h> 31f95f3850SWill Newton #include <linux/mmc/mmc.h> 32f95f3850SWill Newton #include <linux/mmc/dw_mmc.h> 33f95f3850SWill Newton #include <linux/bitops.h> 34c07946a3SJaehoon Chung #include <linux/regulator/consumer.h> 351791b13eSJames Hogan #include <linux/workqueue.h> 36f95f3850SWill Newton 37f95f3850SWill Newton #include "dw_mmc.h" 38f95f3850SWill Newton 39f95f3850SWill Newton /* Common flag combinations */ 40f95f3850SWill Newton #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \ 41f95f3850SWill Newton SDMMC_INT_HTO | SDMMC_INT_SBE | \ 42f95f3850SWill Newton SDMMC_INT_EBE) 43f95f3850SWill Newton #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \ 44f95f3850SWill Newton SDMMC_INT_RESP_ERR) 45f95f3850SWill Newton #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \ 46f95f3850SWill Newton DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE) 47f95f3850SWill Newton #define DW_MCI_SEND_STATUS 1 48f95f3850SWill Newton #define DW_MCI_RECV_STATUS 2 49f95f3850SWill Newton #define DW_MCI_DMA_THRESHOLD 16 50f95f3850SWill Newton 51f95f3850SWill Newton #ifdef CONFIG_MMC_DW_IDMAC 52f95f3850SWill Newton struct idmac_desc { 53f95f3850SWill Newton u32 des0; /* Control Descriptor */ 54f95f3850SWill Newton #define IDMAC_DES0_DIC BIT(1) 55f95f3850SWill Newton #define IDMAC_DES0_LD BIT(2) 56f95f3850SWill Newton #define IDMAC_DES0_FD BIT(3) 57f95f3850SWill Newton #define IDMAC_DES0_CH BIT(4) 58f95f3850SWill Newton #define IDMAC_DES0_ER BIT(5) 59f95f3850SWill Newton #define IDMAC_DES0_CES BIT(30) 60f95f3850SWill Newton #define IDMAC_DES0_OWN BIT(31) 61f95f3850SWill Newton 62f95f3850SWill Newton u32 des1; /* Buffer sizes */ 63f95f3850SWill Newton #define IDMAC_SET_BUFFER1_SIZE(d, s) \ 649b7bbe10SShashidhar Hiremath ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff)) 65f95f3850SWill Newton 66f95f3850SWill Newton u32 des2; /* buffer 1 physical address */ 67f95f3850SWill Newton 68f95f3850SWill Newton u32 des3; /* buffer 2 physical address */ 69f95f3850SWill Newton }; 70f95f3850SWill Newton #endif /* CONFIG_MMC_DW_IDMAC */ 71f95f3850SWill Newton 72f95f3850SWill Newton /** 73f95f3850SWill Newton * struct dw_mci_slot - MMC slot state 74f95f3850SWill Newton * @mmc: The mmc_host representing this slot. 75f95f3850SWill Newton * @host: The MMC controller this slot is using. 76f95f3850SWill Newton * @ctype: Card type for this slot. 77f95f3850SWill Newton * @mrq: mmc_request currently being processed or waiting to be 78f95f3850SWill Newton * processed, or NULL when the slot is idle. 79f95f3850SWill Newton * @queue_node: List node for placing this node in the @queue list of 80f95f3850SWill Newton * &struct dw_mci. 81f95f3850SWill Newton * @clock: Clock rate configured by set_ios(). Protected by host->lock. 82f95f3850SWill Newton * @flags: Random state bits associated with the slot. 83f95f3850SWill Newton * @id: Number of this slot. 84f95f3850SWill Newton * @last_detect_state: Most recently observed card detect state. 85f95f3850SWill Newton */ 86f95f3850SWill Newton struct dw_mci_slot { 87f95f3850SWill Newton struct mmc_host *mmc; 88f95f3850SWill Newton struct dw_mci *host; 89f95f3850SWill Newton 90f95f3850SWill Newton u32 ctype; 91f95f3850SWill Newton 92f95f3850SWill Newton struct mmc_request *mrq; 93f95f3850SWill Newton struct list_head queue_node; 94f95f3850SWill Newton 95f95f3850SWill Newton unsigned int clock; 96f95f3850SWill Newton unsigned long flags; 97f95f3850SWill Newton #define DW_MMC_CARD_PRESENT 0 98f95f3850SWill Newton #define DW_MMC_CARD_NEED_INIT 1 99f95f3850SWill Newton int id; 100f95f3850SWill Newton int last_detect_state; 101f95f3850SWill Newton }; 102f95f3850SWill Newton 103f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS) 104f95f3850SWill Newton static int dw_mci_req_show(struct seq_file *s, void *v) 105f95f3850SWill Newton { 106f95f3850SWill Newton struct dw_mci_slot *slot = s->private; 107f95f3850SWill Newton struct mmc_request *mrq; 108f95f3850SWill Newton struct mmc_command *cmd; 109f95f3850SWill Newton struct mmc_command *stop; 110f95f3850SWill Newton struct mmc_data *data; 111f95f3850SWill Newton 112f95f3850SWill Newton /* Make sure we get a consistent snapshot */ 113f95f3850SWill Newton spin_lock_bh(&slot->host->lock); 114f95f3850SWill Newton mrq = slot->mrq; 115f95f3850SWill Newton 116f95f3850SWill Newton if (mrq) { 117f95f3850SWill Newton cmd = mrq->cmd; 118f95f3850SWill Newton data = mrq->data; 119f95f3850SWill Newton stop = mrq->stop; 120f95f3850SWill Newton 121f95f3850SWill Newton if (cmd) 122f95f3850SWill Newton seq_printf(s, 123f95f3850SWill Newton "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", 124f95f3850SWill Newton cmd->opcode, cmd->arg, cmd->flags, 125f95f3850SWill Newton cmd->resp[0], cmd->resp[1], cmd->resp[2], 126f95f3850SWill Newton cmd->resp[2], cmd->error); 127f95f3850SWill Newton if (data) 128f95f3850SWill Newton seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", 129f95f3850SWill Newton data->bytes_xfered, data->blocks, 130f95f3850SWill Newton data->blksz, data->flags, data->error); 131f95f3850SWill Newton if (stop) 132f95f3850SWill Newton seq_printf(s, 133f95f3850SWill Newton "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", 134f95f3850SWill Newton stop->opcode, stop->arg, stop->flags, 135f95f3850SWill Newton stop->resp[0], stop->resp[1], stop->resp[2], 136f95f3850SWill Newton stop->resp[2], stop->error); 137f95f3850SWill Newton } 138f95f3850SWill Newton 139f95f3850SWill Newton spin_unlock_bh(&slot->host->lock); 140f95f3850SWill Newton 141f95f3850SWill Newton return 0; 142f95f3850SWill Newton } 143f95f3850SWill Newton 144f95f3850SWill Newton static int dw_mci_req_open(struct inode *inode, struct file *file) 145f95f3850SWill Newton { 146f95f3850SWill Newton return single_open(file, dw_mci_req_show, inode->i_private); 147f95f3850SWill Newton } 148f95f3850SWill Newton 149f95f3850SWill Newton static const struct file_operations dw_mci_req_fops = { 150f95f3850SWill Newton .owner = THIS_MODULE, 151f95f3850SWill Newton .open = dw_mci_req_open, 152f95f3850SWill Newton .read = seq_read, 153f95f3850SWill Newton .llseek = seq_lseek, 154f95f3850SWill Newton .release = single_release, 155f95f3850SWill Newton }; 156f95f3850SWill Newton 157f95f3850SWill Newton static int dw_mci_regs_show(struct seq_file *s, void *v) 158f95f3850SWill Newton { 159f95f3850SWill Newton seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS); 160f95f3850SWill Newton seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS); 161f95f3850SWill Newton seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD); 162f95f3850SWill Newton seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL); 163f95f3850SWill Newton seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK); 164f95f3850SWill Newton seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA); 165f95f3850SWill Newton 166f95f3850SWill Newton return 0; 167f95f3850SWill Newton } 168f95f3850SWill Newton 169f95f3850SWill Newton static int dw_mci_regs_open(struct inode *inode, struct file *file) 170f95f3850SWill Newton { 171f95f3850SWill Newton return single_open(file, dw_mci_regs_show, inode->i_private); 172f95f3850SWill Newton } 173f95f3850SWill Newton 174f95f3850SWill Newton static const struct file_operations dw_mci_regs_fops = { 175f95f3850SWill Newton .owner = THIS_MODULE, 176f95f3850SWill Newton .open = dw_mci_regs_open, 177f95f3850SWill Newton .read = seq_read, 178f95f3850SWill Newton .llseek = seq_lseek, 179f95f3850SWill Newton .release = single_release, 180f95f3850SWill Newton }; 181f95f3850SWill Newton 182f95f3850SWill Newton static void dw_mci_init_debugfs(struct dw_mci_slot *slot) 183f95f3850SWill Newton { 184f95f3850SWill Newton struct mmc_host *mmc = slot->mmc; 185f95f3850SWill Newton struct dw_mci *host = slot->host; 186f95f3850SWill Newton struct dentry *root; 187f95f3850SWill Newton struct dentry *node; 188f95f3850SWill Newton 189f95f3850SWill Newton root = mmc->debugfs_root; 190f95f3850SWill Newton if (!root) 191f95f3850SWill Newton return; 192f95f3850SWill Newton 193f95f3850SWill Newton node = debugfs_create_file("regs", S_IRUSR, root, host, 194f95f3850SWill Newton &dw_mci_regs_fops); 195f95f3850SWill Newton if (!node) 196f95f3850SWill Newton goto err; 197f95f3850SWill Newton 198f95f3850SWill Newton node = debugfs_create_file("req", S_IRUSR, root, slot, 199f95f3850SWill Newton &dw_mci_req_fops); 200f95f3850SWill Newton if (!node) 201f95f3850SWill Newton goto err; 202f95f3850SWill Newton 203f95f3850SWill Newton node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); 204f95f3850SWill Newton if (!node) 205f95f3850SWill Newton goto err; 206f95f3850SWill Newton 207f95f3850SWill Newton node = debugfs_create_x32("pending_events", S_IRUSR, root, 208f95f3850SWill Newton (u32 *)&host->pending_events); 209f95f3850SWill Newton if (!node) 210f95f3850SWill Newton goto err; 211f95f3850SWill Newton 212f95f3850SWill Newton node = debugfs_create_x32("completed_events", S_IRUSR, root, 213f95f3850SWill Newton (u32 *)&host->completed_events); 214f95f3850SWill Newton if (!node) 215f95f3850SWill Newton goto err; 216f95f3850SWill Newton 217f95f3850SWill Newton return; 218f95f3850SWill Newton 219f95f3850SWill Newton err: 220f95f3850SWill Newton dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); 221f95f3850SWill Newton } 222f95f3850SWill Newton #endif /* defined(CONFIG_DEBUG_FS) */ 223f95f3850SWill Newton 224f95f3850SWill Newton static void dw_mci_set_timeout(struct dw_mci *host) 225f95f3850SWill Newton { 226f95f3850SWill Newton /* timeout (maximum) */ 227f95f3850SWill Newton mci_writel(host, TMOUT, 0xffffffff); 228f95f3850SWill Newton } 229f95f3850SWill Newton 230f95f3850SWill Newton static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd) 231f95f3850SWill Newton { 232f95f3850SWill Newton struct mmc_data *data; 233f95f3850SWill Newton u32 cmdr; 234f95f3850SWill Newton cmd->error = -EINPROGRESS; 235f95f3850SWill Newton 236f95f3850SWill Newton cmdr = cmd->opcode; 237f95f3850SWill Newton 238f95f3850SWill Newton if (cmdr == MMC_STOP_TRANSMISSION) 239f95f3850SWill Newton cmdr |= SDMMC_CMD_STOP; 240f95f3850SWill Newton else 241f95f3850SWill Newton cmdr |= SDMMC_CMD_PRV_DAT_WAIT; 242f95f3850SWill Newton 243f95f3850SWill Newton if (cmd->flags & MMC_RSP_PRESENT) { 244f95f3850SWill Newton /* We expect a response, so set this bit */ 245f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_EXP; 246f95f3850SWill Newton if (cmd->flags & MMC_RSP_136) 247f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_LONG; 248f95f3850SWill Newton } 249f95f3850SWill Newton 250f95f3850SWill Newton if (cmd->flags & MMC_RSP_CRC) 251f95f3850SWill Newton cmdr |= SDMMC_CMD_RESP_CRC; 252f95f3850SWill Newton 253f95f3850SWill Newton data = cmd->data; 254f95f3850SWill Newton if (data) { 255f95f3850SWill Newton cmdr |= SDMMC_CMD_DAT_EXP; 256f95f3850SWill Newton if (data->flags & MMC_DATA_STREAM) 257f95f3850SWill Newton cmdr |= SDMMC_CMD_STRM_MODE; 258f95f3850SWill Newton if (data->flags & MMC_DATA_WRITE) 259f95f3850SWill Newton cmdr |= SDMMC_CMD_DAT_WR; 260f95f3850SWill Newton } 261f95f3850SWill Newton 262f95f3850SWill Newton return cmdr; 263f95f3850SWill Newton } 264f95f3850SWill Newton 265f95f3850SWill Newton static void dw_mci_start_command(struct dw_mci *host, 266f95f3850SWill Newton struct mmc_command *cmd, u32 cmd_flags) 267f95f3850SWill Newton { 268f95f3850SWill Newton host->cmd = cmd; 26962ca8034SShashidhar Hiremath dev_vdbg(&host->dev, 270f95f3850SWill Newton "start command: ARGR=0x%08x CMDR=0x%08x\n", 271f95f3850SWill Newton cmd->arg, cmd_flags); 272f95f3850SWill Newton 273f95f3850SWill Newton mci_writel(host, CMDARG, cmd->arg); 274f95f3850SWill Newton wmb(); 275f95f3850SWill Newton 276f95f3850SWill Newton mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); 277f95f3850SWill Newton } 278f95f3850SWill Newton 279f95f3850SWill Newton static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data) 280f95f3850SWill Newton { 281f95f3850SWill Newton dw_mci_start_command(host, data->stop, host->stop_cmdr); 282f95f3850SWill Newton } 283f95f3850SWill Newton 284f95f3850SWill Newton /* DMA interface functions */ 285f95f3850SWill Newton static void dw_mci_stop_dma(struct dw_mci *host) 286f95f3850SWill Newton { 28703e8cb53SJames Hogan if (host->using_dma) { 288f95f3850SWill Newton host->dma_ops->stop(host); 289f95f3850SWill Newton host->dma_ops->cleanup(host); 290f95f3850SWill Newton } else { 291f95f3850SWill Newton /* Data transfer was stopped by the interrupt handler */ 292f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 293f95f3850SWill Newton } 294f95f3850SWill Newton } 295f95f3850SWill Newton 2969aa51408SSeungwon Jeon static int dw_mci_get_dma_dir(struct mmc_data *data) 2979aa51408SSeungwon Jeon { 2989aa51408SSeungwon Jeon if (data->flags & MMC_DATA_WRITE) 2999aa51408SSeungwon Jeon return DMA_TO_DEVICE; 3009aa51408SSeungwon Jeon else 3019aa51408SSeungwon Jeon return DMA_FROM_DEVICE; 3029aa51408SSeungwon Jeon } 3039aa51408SSeungwon Jeon 3049beee912SJaehoon Chung #ifdef CONFIG_MMC_DW_IDMAC 305f95f3850SWill Newton static void dw_mci_dma_cleanup(struct dw_mci *host) 306f95f3850SWill Newton { 307f95f3850SWill Newton struct mmc_data *data = host->data; 308f95f3850SWill Newton 309f95f3850SWill Newton if (data) 3109aa51408SSeungwon Jeon if (!data->host_cookie) 3119aa51408SSeungwon Jeon dma_unmap_sg(&host->dev, 3129aa51408SSeungwon Jeon data->sg, 3139aa51408SSeungwon Jeon data->sg_len, 3149aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 315f95f3850SWill Newton } 316f95f3850SWill Newton 317f95f3850SWill Newton static void dw_mci_idmac_stop_dma(struct dw_mci *host) 318f95f3850SWill Newton { 319f95f3850SWill Newton u32 temp; 320f95f3850SWill Newton 321f95f3850SWill Newton /* Disable and reset the IDMAC interface */ 322f95f3850SWill Newton temp = mci_readl(host, CTRL); 323f95f3850SWill Newton temp &= ~SDMMC_CTRL_USE_IDMAC; 324f95f3850SWill Newton temp |= SDMMC_CTRL_DMA_RESET; 325f95f3850SWill Newton mci_writel(host, CTRL, temp); 326f95f3850SWill Newton 327f95f3850SWill Newton /* Stop the IDMAC running */ 328f95f3850SWill Newton temp = mci_readl(host, BMOD); 329a5289a43SJaehoon Chung temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB); 330f95f3850SWill Newton mci_writel(host, BMOD, temp); 331f95f3850SWill Newton } 332f95f3850SWill Newton 333f95f3850SWill Newton static void dw_mci_idmac_complete_dma(struct dw_mci *host) 334f95f3850SWill Newton { 335f95f3850SWill Newton struct mmc_data *data = host->data; 336f95f3850SWill Newton 33762ca8034SShashidhar Hiremath dev_vdbg(&host->dev, "DMA complete\n"); 338f95f3850SWill Newton 339f95f3850SWill Newton host->dma_ops->cleanup(host); 340f95f3850SWill Newton 341f95f3850SWill Newton /* 342f95f3850SWill Newton * If the card was removed, data will be NULL. No point in trying to 343f95f3850SWill Newton * send the stop command or waiting for NBUSY in this case. 344f95f3850SWill Newton */ 345f95f3850SWill Newton if (data) { 346f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 347f95f3850SWill Newton tasklet_schedule(&host->tasklet); 348f95f3850SWill Newton } 349f95f3850SWill Newton } 350f95f3850SWill Newton 351f95f3850SWill Newton static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data, 352f95f3850SWill Newton unsigned int sg_len) 353f95f3850SWill Newton { 354f95f3850SWill Newton int i; 355f95f3850SWill Newton struct idmac_desc *desc = host->sg_cpu; 356f95f3850SWill Newton 357f95f3850SWill Newton for (i = 0; i < sg_len; i++, desc++) { 358f95f3850SWill Newton unsigned int length = sg_dma_len(&data->sg[i]); 359f95f3850SWill Newton u32 mem_addr = sg_dma_address(&data->sg[i]); 360f95f3850SWill Newton 361f95f3850SWill Newton /* Set the OWN bit and disable interrupts for this descriptor */ 362f95f3850SWill Newton desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH; 363f95f3850SWill Newton 364f95f3850SWill Newton /* Buffer length */ 365f95f3850SWill Newton IDMAC_SET_BUFFER1_SIZE(desc, length); 366f95f3850SWill Newton 367f95f3850SWill Newton /* Physical address to DMA to/from */ 368f95f3850SWill Newton desc->des2 = mem_addr; 369f95f3850SWill Newton } 370f95f3850SWill Newton 371f95f3850SWill Newton /* Set first descriptor */ 372f95f3850SWill Newton desc = host->sg_cpu; 373f95f3850SWill Newton desc->des0 |= IDMAC_DES0_FD; 374f95f3850SWill Newton 375f95f3850SWill Newton /* Set last descriptor */ 376f95f3850SWill Newton desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc); 377f95f3850SWill Newton desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); 378f95f3850SWill Newton desc->des0 |= IDMAC_DES0_LD; 379f95f3850SWill Newton 380f95f3850SWill Newton wmb(); 381f95f3850SWill Newton } 382f95f3850SWill Newton 383f95f3850SWill Newton static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len) 384f95f3850SWill Newton { 385f95f3850SWill Newton u32 temp; 386f95f3850SWill Newton 387f95f3850SWill Newton dw_mci_translate_sglist(host, host->data, sg_len); 388f95f3850SWill Newton 389f95f3850SWill Newton /* Select IDMAC interface */ 390f95f3850SWill Newton temp = mci_readl(host, CTRL); 391f95f3850SWill Newton temp |= SDMMC_CTRL_USE_IDMAC; 392f95f3850SWill Newton mci_writel(host, CTRL, temp); 393f95f3850SWill Newton 394f95f3850SWill Newton wmb(); 395f95f3850SWill Newton 396f95f3850SWill Newton /* Enable the IDMAC */ 397f95f3850SWill Newton temp = mci_readl(host, BMOD); 398a5289a43SJaehoon Chung temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB; 399f95f3850SWill Newton mci_writel(host, BMOD, temp); 400f95f3850SWill Newton 401f95f3850SWill Newton /* Start it running */ 402f95f3850SWill Newton mci_writel(host, PLDMND, 1); 403f95f3850SWill Newton } 404f95f3850SWill Newton 405f95f3850SWill Newton static int dw_mci_idmac_init(struct dw_mci *host) 406f95f3850SWill Newton { 407f95f3850SWill Newton struct idmac_desc *p; 40894c6cee9SGirish K S int i, dma_support; 409f95f3850SWill Newton 410f95f3850SWill Newton /* Number of descriptors in the ring buffer */ 411f95f3850SWill Newton host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc); 412f95f3850SWill Newton 41394c6cee9SGirish K S /* Check if Hardware Configuration Register has support for DMA */ 41494c6cee9SGirish K S dma_support = (mci_readl(host, HCON) >> 16) & 0x3; 41594c6cee9SGirish K S 41694c6cee9SGirish K S if (!dma_support || dma_support > 2) { 41794c6cee9SGirish K S dev_err(&host->dev, 41894c6cee9SGirish K S "Host Controller does not support IDMA Tx.\n"); 41994c6cee9SGirish K S host->dma_ops = NULL; 42094c6cee9SGirish K S return -ENODEV; 42194c6cee9SGirish K S } 42294c6cee9SGirish K S 42394c6cee9SGirish K S dev_info(&host->dev, "Using internal DMA controller.\n"); 42494c6cee9SGirish K S 425f95f3850SWill Newton /* Forward link the descriptor list */ 426f95f3850SWill Newton for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) 427f95f3850SWill Newton p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1)); 428f95f3850SWill Newton 429f95f3850SWill Newton /* Set the last descriptor as the end-of-ring descriptor */ 430f95f3850SWill Newton p->des3 = host->sg_dma; 431f95f3850SWill Newton p->des0 = IDMAC_DES0_ER; 432f95f3850SWill Newton 433141a712aSSeungwon Jeon mci_writel(host, BMOD, SDMMC_IDMAC_SWRESET); 434141a712aSSeungwon Jeon 435f95f3850SWill Newton /* Mask out interrupts - get Tx & Rx complete only */ 436f95f3850SWill Newton mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI | 437f95f3850SWill Newton SDMMC_IDMAC_INT_TI); 438f95f3850SWill Newton 439f95f3850SWill Newton /* Set the descriptor base address */ 440f95f3850SWill Newton mci_writel(host, DBADDR, host->sg_dma); 441f95f3850SWill Newton return 0; 442f95f3850SWill Newton } 443f95f3850SWill Newton 444885c3e80SSeungwon Jeon static struct dw_mci_dma_ops dw_mci_idmac_ops = { 445885c3e80SSeungwon Jeon .init = dw_mci_idmac_init, 446885c3e80SSeungwon Jeon .start = dw_mci_idmac_start_dma, 447885c3e80SSeungwon Jeon .stop = dw_mci_idmac_stop_dma, 448885c3e80SSeungwon Jeon .complete = dw_mci_idmac_complete_dma, 449885c3e80SSeungwon Jeon .cleanup = dw_mci_dma_cleanup, 450885c3e80SSeungwon Jeon }; 451885c3e80SSeungwon Jeon #endif /* CONFIG_MMC_DW_IDMAC */ 452885c3e80SSeungwon Jeon 4539aa51408SSeungwon Jeon static int dw_mci_pre_dma_transfer(struct dw_mci *host, 4549aa51408SSeungwon Jeon struct mmc_data *data, 4559aa51408SSeungwon Jeon bool next) 456f95f3850SWill Newton { 457f95f3850SWill Newton struct scatterlist *sg; 4589aa51408SSeungwon Jeon unsigned int i, sg_len; 459f95f3850SWill Newton 4609aa51408SSeungwon Jeon if (!next && data->host_cookie) 4619aa51408SSeungwon Jeon return data->host_cookie; 462f95f3850SWill Newton 463f95f3850SWill Newton /* 464f95f3850SWill Newton * We don't do DMA on "complex" transfers, i.e. with 465f95f3850SWill Newton * non-word-aligned buffers or lengths. Also, we don't bother 466f95f3850SWill Newton * with all the DMA setup overhead for short transfers. 467f95f3850SWill Newton */ 468f95f3850SWill Newton if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD) 469f95f3850SWill Newton return -EINVAL; 4709aa51408SSeungwon Jeon 471f95f3850SWill Newton if (data->blksz & 3) 472f95f3850SWill Newton return -EINVAL; 473f95f3850SWill Newton 474f95f3850SWill Newton for_each_sg(data->sg, sg, data->sg_len, i) { 475f95f3850SWill Newton if (sg->offset & 3 || sg->length & 3) 476f95f3850SWill Newton return -EINVAL; 477f95f3850SWill Newton } 478f95f3850SWill Newton 4799aa51408SSeungwon Jeon sg_len = dma_map_sg(&host->dev, 4809aa51408SSeungwon Jeon data->sg, 4819aa51408SSeungwon Jeon data->sg_len, 4829aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 4839aa51408SSeungwon Jeon if (sg_len == 0) 4849aa51408SSeungwon Jeon return -EINVAL; 4859aa51408SSeungwon Jeon 4869aa51408SSeungwon Jeon if (next) 4879aa51408SSeungwon Jeon data->host_cookie = sg_len; 4889aa51408SSeungwon Jeon 4899aa51408SSeungwon Jeon return sg_len; 4909aa51408SSeungwon Jeon } 4919aa51408SSeungwon Jeon 4929aa51408SSeungwon Jeon static void dw_mci_pre_req(struct mmc_host *mmc, 4939aa51408SSeungwon Jeon struct mmc_request *mrq, 4949aa51408SSeungwon Jeon bool is_first_req) 4959aa51408SSeungwon Jeon { 4969aa51408SSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 4979aa51408SSeungwon Jeon struct mmc_data *data = mrq->data; 4989aa51408SSeungwon Jeon 4999aa51408SSeungwon Jeon if (!slot->host->use_dma || !data) 5009aa51408SSeungwon Jeon return; 5019aa51408SSeungwon Jeon 5029aa51408SSeungwon Jeon if (data->host_cookie) { 5039aa51408SSeungwon Jeon data->host_cookie = 0; 5049aa51408SSeungwon Jeon return; 5059aa51408SSeungwon Jeon } 5069aa51408SSeungwon Jeon 5079aa51408SSeungwon Jeon if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0) 5089aa51408SSeungwon Jeon data->host_cookie = 0; 5099aa51408SSeungwon Jeon } 5109aa51408SSeungwon Jeon 5119aa51408SSeungwon Jeon static void dw_mci_post_req(struct mmc_host *mmc, 5129aa51408SSeungwon Jeon struct mmc_request *mrq, 5139aa51408SSeungwon Jeon int err) 5149aa51408SSeungwon Jeon { 5159aa51408SSeungwon Jeon struct dw_mci_slot *slot = mmc_priv(mmc); 5169aa51408SSeungwon Jeon struct mmc_data *data = mrq->data; 5179aa51408SSeungwon Jeon 5189aa51408SSeungwon Jeon if (!slot->host->use_dma || !data) 5199aa51408SSeungwon Jeon return; 5209aa51408SSeungwon Jeon 5219aa51408SSeungwon Jeon if (data->host_cookie) 5229aa51408SSeungwon Jeon dma_unmap_sg(&slot->host->dev, 5239aa51408SSeungwon Jeon data->sg, 5249aa51408SSeungwon Jeon data->sg_len, 5259aa51408SSeungwon Jeon dw_mci_get_dma_dir(data)); 5269aa51408SSeungwon Jeon data->host_cookie = 0; 5279aa51408SSeungwon Jeon } 5289aa51408SSeungwon Jeon 5299aa51408SSeungwon Jeon static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data) 5309aa51408SSeungwon Jeon { 5319aa51408SSeungwon Jeon int sg_len; 5329aa51408SSeungwon Jeon u32 temp; 5339aa51408SSeungwon Jeon 5349aa51408SSeungwon Jeon host->using_dma = 0; 5359aa51408SSeungwon Jeon 5369aa51408SSeungwon Jeon /* If we don't have a channel, we can't do DMA */ 5379aa51408SSeungwon Jeon if (!host->use_dma) 5389aa51408SSeungwon Jeon return -ENODEV; 5399aa51408SSeungwon Jeon 5409aa51408SSeungwon Jeon sg_len = dw_mci_pre_dma_transfer(host, data, 0); 541a99aa9b9SSeungwon Jeon if (sg_len < 0) { 542a99aa9b9SSeungwon Jeon host->dma_ops->stop(host); 5439aa51408SSeungwon Jeon return sg_len; 544a99aa9b9SSeungwon Jeon } 5459aa51408SSeungwon Jeon 54603e8cb53SJames Hogan host->using_dma = 1; 54703e8cb53SJames Hogan 54862ca8034SShashidhar Hiremath dev_vdbg(&host->dev, 549f95f3850SWill Newton "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n", 550f95f3850SWill Newton (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma, 551f95f3850SWill Newton sg_len); 552f95f3850SWill Newton 553f95f3850SWill Newton /* Enable the DMA interface */ 554f95f3850SWill Newton temp = mci_readl(host, CTRL); 555f95f3850SWill Newton temp |= SDMMC_CTRL_DMA_ENABLE; 556f95f3850SWill Newton mci_writel(host, CTRL, temp); 557f95f3850SWill Newton 558f95f3850SWill Newton /* Disable RX/TX IRQs, let DMA handle it */ 559f95f3850SWill Newton temp = mci_readl(host, INTMASK); 560f95f3850SWill Newton temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR); 561f95f3850SWill Newton mci_writel(host, INTMASK, temp); 562f95f3850SWill Newton 563f95f3850SWill Newton host->dma_ops->start(host, sg_len); 564f95f3850SWill Newton 565f95f3850SWill Newton return 0; 566f95f3850SWill Newton } 567f95f3850SWill Newton 568f95f3850SWill Newton static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) 569f95f3850SWill Newton { 570f95f3850SWill Newton u32 temp; 571f95f3850SWill Newton 572f95f3850SWill Newton data->error = -EINPROGRESS; 573f95f3850SWill Newton 574f95f3850SWill Newton WARN_ON(host->data); 575f95f3850SWill Newton host->sg = NULL; 576f95f3850SWill Newton host->data = data; 577f95f3850SWill Newton 57855c5efbcSJames Hogan if (data->flags & MMC_DATA_READ) 57955c5efbcSJames Hogan host->dir_status = DW_MCI_RECV_STATUS; 58055c5efbcSJames Hogan else 58155c5efbcSJames Hogan host->dir_status = DW_MCI_SEND_STATUS; 58255c5efbcSJames Hogan 583f95f3850SWill Newton if (dw_mci_submit_data_dma(host, data)) { 584f9c2a0dcSSeungwon Jeon int flags = SG_MITER_ATOMIC; 585f9c2a0dcSSeungwon Jeon if (host->data->flags & MMC_DATA_READ) 586f9c2a0dcSSeungwon Jeon flags |= SG_MITER_TO_SG; 587f9c2a0dcSSeungwon Jeon else 588f9c2a0dcSSeungwon Jeon flags |= SG_MITER_FROM_SG; 589f9c2a0dcSSeungwon Jeon 590f9c2a0dcSSeungwon Jeon sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); 591f95f3850SWill Newton host->sg = data->sg; 59234b664a2SJames Hogan host->part_buf_start = 0; 59334b664a2SJames Hogan host->part_buf_count = 0; 594f95f3850SWill Newton 595b40af3aaSJames Hogan mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR); 596f95f3850SWill Newton temp = mci_readl(host, INTMASK); 597f95f3850SWill Newton temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR; 598f95f3850SWill Newton mci_writel(host, INTMASK, temp); 599f95f3850SWill Newton 600f95f3850SWill Newton temp = mci_readl(host, CTRL); 601f95f3850SWill Newton temp &= ~SDMMC_CTRL_DMA_ENABLE; 602f95f3850SWill Newton mci_writel(host, CTRL, temp); 603f95f3850SWill Newton } 604f95f3850SWill Newton } 605f95f3850SWill Newton 606f95f3850SWill Newton static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg) 607f95f3850SWill Newton { 608f95f3850SWill Newton struct dw_mci *host = slot->host; 609f95f3850SWill Newton unsigned long timeout = jiffies + msecs_to_jiffies(500); 610f95f3850SWill Newton unsigned int cmd_status = 0; 611f95f3850SWill Newton 612f95f3850SWill Newton mci_writel(host, CMDARG, arg); 613f95f3850SWill Newton wmb(); 614f95f3850SWill Newton mci_writel(host, CMD, SDMMC_CMD_START | cmd); 615f95f3850SWill Newton 616f95f3850SWill Newton while (time_before(jiffies, timeout)) { 617f95f3850SWill Newton cmd_status = mci_readl(host, CMD); 618f95f3850SWill Newton if (!(cmd_status & SDMMC_CMD_START)) 619f95f3850SWill Newton return; 620f95f3850SWill Newton } 621f95f3850SWill Newton dev_err(&slot->mmc->class_dev, 622f95f3850SWill Newton "Timeout sending command (cmd %#x arg %#x status %#x)\n", 623f95f3850SWill Newton cmd, arg, cmd_status); 624f95f3850SWill Newton } 625f95f3850SWill Newton 626f95f3850SWill Newton static void dw_mci_setup_bus(struct dw_mci_slot *slot) 627f95f3850SWill Newton { 628f95f3850SWill Newton struct dw_mci *host = slot->host; 629f95f3850SWill Newton u32 div; 630*9623b5b9SDoug Anderson u32 clk_en_a; 631f95f3850SWill Newton 632f95f3850SWill Newton if (slot->clock != host->current_speed) { 633e419990bSSeungwon Jeon div = host->bus_hz / slot->clock; 634e419990bSSeungwon Jeon if (host->bus_hz % slot->clock && host->bus_hz > slot->clock) 635f95f3850SWill Newton /* 636f95f3850SWill Newton * move the + 1 after the divide to prevent 637f95f3850SWill Newton * over-clocking the card. 638f95f3850SWill Newton */ 639e419990bSSeungwon Jeon div += 1; 640e419990bSSeungwon Jeon 641e419990bSSeungwon Jeon div = (host->bus_hz != slot->clock) ? DIV_ROUND_UP(div, 2) : 0; 642f95f3850SWill Newton 643f95f3850SWill Newton dev_info(&slot->mmc->class_dev, 644f95f3850SWill Newton "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ" 645f95f3850SWill Newton " div = %d)\n", slot->id, host->bus_hz, slot->clock, 646f95f3850SWill Newton div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div); 647f95f3850SWill Newton 648f95f3850SWill Newton /* disable clock */ 649f95f3850SWill Newton mci_writel(host, CLKENA, 0); 650f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 651f95f3850SWill Newton 652f95f3850SWill Newton /* inform CIU */ 653f95f3850SWill Newton mci_send_cmd(slot, 654f95f3850SWill Newton SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); 655f95f3850SWill Newton 656f95f3850SWill Newton /* set clock to desired speed */ 657f95f3850SWill Newton mci_writel(host, CLKDIV, div); 658f95f3850SWill Newton 659f95f3850SWill Newton /* inform CIU */ 660f95f3850SWill Newton mci_send_cmd(slot, 661f95f3850SWill Newton SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); 662f95f3850SWill Newton 663*9623b5b9SDoug Anderson /* enable clock; only low power if no SDIO */ 664*9623b5b9SDoug Anderson clk_en_a = SDMMC_CLKEN_ENABLE << slot->id; 665*9623b5b9SDoug Anderson if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->id))) 666*9623b5b9SDoug Anderson clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id; 667*9623b5b9SDoug Anderson mci_writel(host, CLKENA, clk_en_a); 668f95f3850SWill Newton 669f95f3850SWill Newton /* inform CIU */ 670f95f3850SWill Newton mci_send_cmd(slot, 671f95f3850SWill Newton SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); 672f95f3850SWill Newton 673f95f3850SWill Newton host->current_speed = slot->clock; 674f95f3850SWill Newton } 675f95f3850SWill Newton 676f95f3850SWill Newton /* Set the current slot bus width */ 6771d56c453SSeungwon Jeon mci_writel(host, CTYPE, (slot->ctype << slot->id)); 678f95f3850SWill Newton } 679f95f3850SWill Newton 680053b3ce6SSeungwon Jeon static void __dw_mci_start_request(struct dw_mci *host, 681053b3ce6SSeungwon Jeon struct dw_mci_slot *slot, 682053b3ce6SSeungwon Jeon struct mmc_command *cmd) 683f95f3850SWill Newton { 684f95f3850SWill Newton struct mmc_request *mrq; 685f95f3850SWill Newton struct mmc_data *data; 686f95f3850SWill Newton u32 cmdflags; 687f95f3850SWill Newton 688f95f3850SWill Newton mrq = slot->mrq; 689f95f3850SWill Newton if (host->pdata->select_slot) 690f95f3850SWill Newton host->pdata->select_slot(slot->id); 691f95f3850SWill Newton 692f95f3850SWill Newton /* Slot specific timing and width adjustment */ 693f95f3850SWill Newton dw_mci_setup_bus(slot); 694f95f3850SWill Newton 695f95f3850SWill Newton host->cur_slot = slot; 696f95f3850SWill Newton host->mrq = mrq; 697f95f3850SWill Newton 698f95f3850SWill Newton host->pending_events = 0; 699f95f3850SWill Newton host->completed_events = 0; 700f95f3850SWill Newton host->data_status = 0; 701f95f3850SWill Newton 702053b3ce6SSeungwon Jeon data = cmd->data; 703f95f3850SWill Newton if (data) { 704f95f3850SWill Newton dw_mci_set_timeout(host); 705f95f3850SWill Newton mci_writel(host, BYTCNT, data->blksz*data->blocks); 706f95f3850SWill Newton mci_writel(host, BLKSIZ, data->blksz); 707f95f3850SWill Newton } 708f95f3850SWill Newton 709f95f3850SWill Newton cmdflags = dw_mci_prepare_command(slot->mmc, cmd); 710f95f3850SWill Newton 711f95f3850SWill Newton /* this is the first command, send the initialization clock */ 712f95f3850SWill Newton if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags)) 713f95f3850SWill Newton cmdflags |= SDMMC_CMD_INIT; 714f95f3850SWill Newton 715f95f3850SWill Newton if (data) { 716f95f3850SWill Newton dw_mci_submit_data(host, data); 717f95f3850SWill Newton wmb(); 718f95f3850SWill Newton } 719f95f3850SWill Newton 720f95f3850SWill Newton dw_mci_start_command(host, cmd, cmdflags); 721f95f3850SWill Newton 722f95f3850SWill Newton if (mrq->stop) 723f95f3850SWill Newton host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop); 724f95f3850SWill Newton } 725f95f3850SWill Newton 726053b3ce6SSeungwon Jeon static void dw_mci_start_request(struct dw_mci *host, 727053b3ce6SSeungwon Jeon struct dw_mci_slot *slot) 728053b3ce6SSeungwon Jeon { 729053b3ce6SSeungwon Jeon struct mmc_request *mrq = slot->mrq; 730053b3ce6SSeungwon Jeon struct mmc_command *cmd; 731053b3ce6SSeungwon Jeon 732053b3ce6SSeungwon Jeon cmd = mrq->sbc ? mrq->sbc : mrq->cmd; 733053b3ce6SSeungwon Jeon __dw_mci_start_request(host, slot, cmd); 734053b3ce6SSeungwon Jeon } 735053b3ce6SSeungwon Jeon 7367456caaeSJames Hogan /* must be called with host->lock held */ 737f95f3850SWill Newton static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot, 738f95f3850SWill Newton struct mmc_request *mrq) 739f95f3850SWill Newton { 740f95f3850SWill Newton dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", 741f95f3850SWill Newton host->state); 742f95f3850SWill Newton 743f95f3850SWill Newton slot->mrq = mrq; 744f95f3850SWill Newton 745f95f3850SWill Newton if (host->state == STATE_IDLE) { 746f95f3850SWill Newton host->state = STATE_SENDING_CMD; 747f95f3850SWill Newton dw_mci_start_request(host, slot); 748f95f3850SWill Newton } else { 749f95f3850SWill Newton list_add_tail(&slot->queue_node, &host->queue); 750f95f3850SWill Newton } 751f95f3850SWill Newton } 752f95f3850SWill Newton 753f95f3850SWill Newton static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) 754f95f3850SWill Newton { 755f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 756f95f3850SWill Newton struct dw_mci *host = slot->host; 757f95f3850SWill Newton 758f95f3850SWill Newton WARN_ON(slot->mrq); 759f95f3850SWill Newton 7607456caaeSJames Hogan /* 7617456caaeSJames Hogan * The check for card presence and queueing of the request must be 7627456caaeSJames Hogan * atomic, otherwise the card could be removed in between and the 7637456caaeSJames Hogan * request wouldn't fail until another card was inserted. 7647456caaeSJames Hogan */ 7657456caaeSJames Hogan spin_lock_bh(&host->lock); 7667456caaeSJames Hogan 767f95f3850SWill Newton if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) { 7687456caaeSJames Hogan spin_unlock_bh(&host->lock); 769f95f3850SWill Newton mrq->cmd->error = -ENOMEDIUM; 770f95f3850SWill Newton mmc_request_done(mmc, mrq); 771f95f3850SWill Newton return; 772f95f3850SWill Newton } 773f95f3850SWill Newton 774f95f3850SWill Newton dw_mci_queue_request(host, slot, mrq); 7757456caaeSJames Hogan 7767456caaeSJames Hogan spin_unlock_bh(&host->lock); 777f95f3850SWill Newton } 778f95f3850SWill Newton 779f95f3850SWill Newton static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 780f95f3850SWill Newton { 781f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 78241babf75SJaehoon Chung u32 regs; 783f95f3850SWill Newton 784f95f3850SWill Newton /* set default 1 bit mode */ 785f95f3850SWill Newton slot->ctype = SDMMC_CTYPE_1BIT; 786f95f3850SWill Newton 787f95f3850SWill Newton switch (ios->bus_width) { 788f95f3850SWill Newton case MMC_BUS_WIDTH_1: 789f95f3850SWill Newton slot->ctype = SDMMC_CTYPE_1BIT; 790f95f3850SWill Newton break; 791f95f3850SWill Newton case MMC_BUS_WIDTH_4: 792f95f3850SWill Newton slot->ctype = SDMMC_CTYPE_4BIT; 793f95f3850SWill Newton break; 794c9b2a06fSJaehoon Chung case MMC_BUS_WIDTH_8: 795c9b2a06fSJaehoon Chung slot->ctype = SDMMC_CTYPE_8BIT; 796c9b2a06fSJaehoon Chung break; 797f95f3850SWill Newton } 798f95f3850SWill Newton 79941babf75SJaehoon Chung regs = mci_readl(slot->host, UHS_REG); 8003f514291SSeungwon Jeon 8013f514291SSeungwon Jeon /* DDR mode set */ 8023f514291SSeungwon Jeon if (ios->timing == MMC_TIMING_UHS_DDR50) 80341babf75SJaehoon Chung regs |= (0x1 << slot->id) << 16; 8043f514291SSeungwon Jeon else 8053f514291SSeungwon Jeon regs &= ~(0x1 << slot->id) << 16; 8063f514291SSeungwon Jeon 80741babf75SJaehoon Chung mci_writel(slot->host, UHS_REG, regs); 80841babf75SJaehoon Chung 809f95f3850SWill Newton if (ios->clock) { 810f95f3850SWill Newton /* 811f95f3850SWill Newton * Use mirror of ios->clock to prevent race with mmc 812f95f3850SWill Newton * core ios update when finding the minimum. 813f95f3850SWill Newton */ 814f95f3850SWill Newton slot->clock = ios->clock; 815f95f3850SWill Newton } 816f95f3850SWill Newton 817f95f3850SWill Newton switch (ios->power_mode) { 818f95f3850SWill Newton case MMC_POWER_UP: 819f95f3850SWill Newton set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags); 820f95f3850SWill Newton break; 821f95f3850SWill Newton default: 822f95f3850SWill Newton break; 823f95f3850SWill Newton } 824f95f3850SWill Newton } 825f95f3850SWill Newton 826f95f3850SWill Newton static int dw_mci_get_ro(struct mmc_host *mmc) 827f95f3850SWill Newton { 828f95f3850SWill Newton int read_only; 829f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 830f95f3850SWill Newton struct dw_mci_board *brd = slot->host->pdata; 831f95f3850SWill Newton 832f95f3850SWill Newton /* Use platform get_ro function, else try on board write protect */ 833f95f3850SWill Newton if (brd->get_ro) 834f95f3850SWill Newton read_only = brd->get_ro(slot->id); 835f95f3850SWill Newton else 836f95f3850SWill Newton read_only = 837f95f3850SWill Newton mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0; 838f95f3850SWill Newton 839f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is %s\n", 840f95f3850SWill Newton read_only ? "read-only" : "read-write"); 841f95f3850SWill Newton 842f95f3850SWill Newton return read_only; 843f95f3850SWill Newton } 844f95f3850SWill Newton 845f95f3850SWill Newton static int dw_mci_get_cd(struct mmc_host *mmc) 846f95f3850SWill Newton { 847f95f3850SWill Newton int present; 848f95f3850SWill Newton struct dw_mci_slot *slot = mmc_priv(mmc); 849f95f3850SWill Newton struct dw_mci_board *brd = slot->host->pdata; 850f95f3850SWill Newton 851f95f3850SWill Newton /* Use platform get_cd function, else try onboard card detect */ 852fc3d7720SJaehoon Chung if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) 853fc3d7720SJaehoon Chung present = 1; 854fc3d7720SJaehoon Chung else if (brd->get_cd) 855f95f3850SWill Newton present = !brd->get_cd(slot->id); 856f95f3850SWill Newton else 857f95f3850SWill Newton present = (mci_readl(slot->host, CDETECT) & (1 << slot->id)) 858f95f3850SWill Newton == 0 ? 1 : 0; 859f95f3850SWill Newton 860f95f3850SWill Newton if (present) 861f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is present\n"); 862f95f3850SWill Newton else 863f95f3850SWill Newton dev_dbg(&mmc->class_dev, "card is not present\n"); 864f95f3850SWill Newton 865f95f3850SWill Newton return present; 866f95f3850SWill Newton } 867f95f3850SWill Newton 868*9623b5b9SDoug Anderson /* 869*9623b5b9SDoug Anderson * Disable lower power mode. 870*9623b5b9SDoug Anderson * 871*9623b5b9SDoug Anderson * Low power mode will stop the card clock when idle. According to the 872*9623b5b9SDoug Anderson * description of the CLKENA register we should disable low power mode 873*9623b5b9SDoug Anderson * for SDIO cards if we need SDIO interrupts to work. 874*9623b5b9SDoug Anderson * 875*9623b5b9SDoug Anderson * This function is fast if low power mode is already disabled. 876*9623b5b9SDoug Anderson */ 877*9623b5b9SDoug Anderson static void dw_mci_disable_low_power(struct dw_mci_slot *slot) 878*9623b5b9SDoug Anderson { 879*9623b5b9SDoug Anderson struct dw_mci *host = slot->host; 880*9623b5b9SDoug Anderson u32 clk_en_a; 881*9623b5b9SDoug Anderson const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id; 882*9623b5b9SDoug Anderson 883*9623b5b9SDoug Anderson clk_en_a = mci_readl(host, CLKENA); 884*9623b5b9SDoug Anderson 885*9623b5b9SDoug Anderson if (clk_en_a & clken_low_pwr) { 886*9623b5b9SDoug Anderson mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr); 887*9623b5b9SDoug Anderson mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | 888*9623b5b9SDoug Anderson SDMMC_CMD_PRV_DAT_WAIT, 0); 889*9623b5b9SDoug Anderson } 890*9623b5b9SDoug Anderson } 891*9623b5b9SDoug Anderson 8921a5c8e1fSShashidhar Hiremath static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb) 8931a5c8e1fSShashidhar Hiremath { 8941a5c8e1fSShashidhar Hiremath struct dw_mci_slot *slot = mmc_priv(mmc); 8951a5c8e1fSShashidhar Hiremath struct dw_mci *host = slot->host; 8961a5c8e1fSShashidhar Hiremath u32 int_mask; 8971a5c8e1fSShashidhar Hiremath 8981a5c8e1fSShashidhar Hiremath /* Enable/disable Slot Specific SDIO interrupt */ 8991a5c8e1fSShashidhar Hiremath int_mask = mci_readl(host, INTMASK); 9001a5c8e1fSShashidhar Hiremath if (enb) { 901*9623b5b9SDoug Anderson /* 902*9623b5b9SDoug Anderson * Turn off low power mode if it was enabled. This is a bit of 903*9623b5b9SDoug Anderson * a heavy operation and we disable / enable IRQs a lot, so 904*9623b5b9SDoug Anderson * we'll leave low power mode disabled and it will get 905*9623b5b9SDoug Anderson * re-enabled again in dw_mci_setup_bus(). 906*9623b5b9SDoug Anderson */ 907*9623b5b9SDoug Anderson dw_mci_disable_low_power(slot); 908*9623b5b9SDoug Anderson 9091a5c8e1fSShashidhar Hiremath mci_writel(host, INTMASK, 910705ad047SKyoungil Kim (int_mask | SDMMC_INT_SDIO(slot->id))); 9111a5c8e1fSShashidhar Hiremath } else { 9121a5c8e1fSShashidhar Hiremath mci_writel(host, INTMASK, 913705ad047SKyoungil Kim (int_mask & ~SDMMC_INT_SDIO(slot->id))); 9141a5c8e1fSShashidhar Hiremath } 9151a5c8e1fSShashidhar Hiremath } 9161a5c8e1fSShashidhar Hiremath 917f95f3850SWill Newton static const struct mmc_host_ops dw_mci_ops = { 918f95f3850SWill Newton .request = dw_mci_request, 9199aa51408SSeungwon Jeon .pre_req = dw_mci_pre_req, 9209aa51408SSeungwon Jeon .post_req = dw_mci_post_req, 921f95f3850SWill Newton .set_ios = dw_mci_set_ios, 922f95f3850SWill Newton .get_ro = dw_mci_get_ro, 923f95f3850SWill Newton .get_cd = dw_mci_get_cd, 9241a5c8e1fSShashidhar Hiremath .enable_sdio_irq = dw_mci_enable_sdio_irq, 925f95f3850SWill Newton }; 926f95f3850SWill Newton 927f95f3850SWill Newton static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq) 928f95f3850SWill Newton __releases(&host->lock) 929f95f3850SWill Newton __acquires(&host->lock) 930f95f3850SWill Newton { 931f95f3850SWill Newton struct dw_mci_slot *slot; 932f95f3850SWill Newton struct mmc_host *prev_mmc = host->cur_slot->mmc; 933f95f3850SWill Newton 934f95f3850SWill Newton WARN_ON(host->cmd || host->data); 935f95f3850SWill Newton 936f95f3850SWill Newton host->cur_slot->mrq = NULL; 937f95f3850SWill Newton host->mrq = NULL; 938f95f3850SWill Newton if (!list_empty(&host->queue)) { 939f95f3850SWill Newton slot = list_entry(host->queue.next, 940f95f3850SWill Newton struct dw_mci_slot, queue_node); 941f95f3850SWill Newton list_del(&slot->queue_node); 94262ca8034SShashidhar Hiremath dev_vdbg(&host->dev, "list not empty: %s is next\n", 943f95f3850SWill Newton mmc_hostname(slot->mmc)); 944f95f3850SWill Newton host->state = STATE_SENDING_CMD; 945f95f3850SWill Newton dw_mci_start_request(host, slot); 946f95f3850SWill Newton } else { 94762ca8034SShashidhar Hiremath dev_vdbg(&host->dev, "list empty\n"); 948f95f3850SWill Newton host->state = STATE_IDLE; 949f95f3850SWill Newton } 950f95f3850SWill Newton 951f95f3850SWill Newton spin_unlock(&host->lock); 952f95f3850SWill Newton mmc_request_done(prev_mmc, mrq); 953f95f3850SWill Newton spin_lock(&host->lock); 954f95f3850SWill Newton } 955f95f3850SWill Newton 956f95f3850SWill Newton static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd) 957f95f3850SWill Newton { 958f95f3850SWill Newton u32 status = host->cmd_status; 959f95f3850SWill Newton 960f95f3850SWill Newton host->cmd_status = 0; 961f95f3850SWill Newton 962f95f3850SWill Newton /* Read the response from the card (up to 16 bytes) */ 963f95f3850SWill Newton if (cmd->flags & MMC_RSP_PRESENT) { 964f95f3850SWill Newton if (cmd->flags & MMC_RSP_136) { 965f95f3850SWill Newton cmd->resp[3] = mci_readl(host, RESP0); 966f95f3850SWill Newton cmd->resp[2] = mci_readl(host, RESP1); 967f95f3850SWill Newton cmd->resp[1] = mci_readl(host, RESP2); 968f95f3850SWill Newton cmd->resp[0] = mci_readl(host, RESP3); 969f95f3850SWill Newton } else { 970f95f3850SWill Newton cmd->resp[0] = mci_readl(host, RESP0); 971f95f3850SWill Newton cmd->resp[1] = 0; 972f95f3850SWill Newton cmd->resp[2] = 0; 973f95f3850SWill Newton cmd->resp[3] = 0; 974f95f3850SWill Newton } 975f95f3850SWill Newton } 976f95f3850SWill Newton 977f95f3850SWill Newton if (status & SDMMC_INT_RTO) 978f95f3850SWill Newton cmd->error = -ETIMEDOUT; 979f95f3850SWill Newton else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)) 980f95f3850SWill Newton cmd->error = -EILSEQ; 981f95f3850SWill Newton else if (status & SDMMC_INT_RESP_ERR) 982f95f3850SWill Newton cmd->error = -EIO; 983f95f3850SWill Newton else 984f95f3850SWill Newton cmd->error = 0; 985f95f3850SWill Newton 986f95f3850SWill Newton if (cmd->error) { 987f95f3850SWill Newton /* newer ip versions need a delay between retries */ 988f95f3850SWill Newton if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY) 989f95f3850SWill Newton mdelay(20); 990f95f3850SWill Newton 991f95f3850SWill Newton if (cmd->data) { 992f95f3850SWill Newton dw_mci_stop_dma(host); 993fda5f736SSeungwon Jeon host->data = NULL; 994f95f3850SWill Newton } 995f95f3850SWill Newton } 996f95f3850SWill Newton } 997f95f3850SWill Newton 998f95f3850SWill Newton static void dw_mci_tasklet_func(unsigned long priv) 999f95f3850SWill Newton { 1000f95f3850SWill Newton struct dw_mci *host = (struct dw_mci *)priv; 1001f95f3850SWill Newton struct mmc_data *data; 1002f95f3850SWill Newton struct mmc_command *cmd; 1003f95f3850SWill Newton enum dw_mci_state state; 1004f95f3850SWill Newton enum dw_mci_state prev_state; 100594dd5b33SJames Hogan u32 status, ctrl; 1006f95f3850SWill Newton 1007f95f3850SWill Newton spin_lock(&host->lock); 1008f95f3850SWill Newton 1009f95f3850SWill Newton state = host->state; 1010f95f3850SWill Newton data = host->data; 1011f95f3850SWill Newton 1012f95f3850SWill Newton do { 1013f95f3850SWill Newton prev_state = state; 1014f95f3850SWill Newton 1015f95f3850SWill Newton switch (state) { 1016f95f3850SWill Newton case STATE_IDLE: 1017f95f3850SWill Newton break; 1018f95f3850SWill Newton 1019f95f3850SWill Newton case STATE_SENDING_CMD: 1020f95f3850SWill Newton if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 1021f95f3850SWill Newton &host->pending_events)) 1022f95f3850SWill Newton break; 1023f95f3850SWill Newton 1024f95f3850SWill Newton cmd = host->cmd; 1025f95f3850SWill Newton host->cmd = NULL; 1026f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->completed_events); 1027053b3ce6SSeungwon Jeon dw_mci_command_complete(host, cmd); 1028053b3ce6SSeungwon Jeon if (cmd == host->mrq->sbc && !cmd->error) { 1029053b3ce6SSeungwon Jeon prev_state = state = STATE_SENDING_CMD; 1030053b3ce6SSeungwon Jeon __dw_mci_start_request(host, host->cur_slot, 1031053b3ce6SSeungwon Jeon host->mrq->cmd); 1032053b3ce6SSeungwon Jeon goto unlock; 1033053b3ce6SSeungwon Jeon } 1034053b3ce6SSeungwon Jeon 1035f95f3850SWill Newton if (!host->mrq->data || cmd->error) { 1036f95f3850SWill Newton dw_mci_request_end(host, host->mrq); 1037f95f3850SWill Newton goto unlock; 1038f95f3850SWill Newton } 1039f95f3850SWill Newton 1040f95f3850SWill Newton prev_state = state = STATE_SENDING_DATA; 1041f95f3850SWill Newton /* fall through */ 1042f95f3850SWill Newton 1043f95f3850SWill Newton case STATE_SENDING_DATA: 1044f95f3850SWill Newton if (test_and_clear_bit(EVENT_DATA_ERROR, 1045f95f3850SWill Newton &host->pending_events)) { 1046f95f3850SWill Newton dw_mci_stop_dma(host); 1047f95f3850SWill Newton if (data->stop) 1048f95f3850SWill Newton send_stop_cmd(host, data); 1049f95f3850SWill Newton state = STATE_DATA_ERROR; 1050f95f3850SWill Newton break; 1051f95f3850SWill Newton } 1052f95f3850SWill Newton 1053f95f3850SWill Newton if (!test_and_clear_bit(EVENT_XFER_COMPLETE, 1054f95f3850SWill Newton &host->pending_events)) 1055f95f3850SWill Newton break; 1056f95f3850SWill Newton 1057f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->completed_events); 1058f95f3850SWill Newton prev_state = state = STATE_DATA_BUSY; 1059f95f3850SWill Newton /* fall through */ 1060f95f3850SWill Newton 1061f95f3850SWill Newton case STATE_DATA_BUSY: 1062f95f3850SWill Newton if (!test_and_clear_bit(EVENT_DATA_COMPLETE, 1063f95f3850SWill Newton &host->pending_events)) 1064f95f3850SWill Newton break; 1065f95f3850SWill Newton 1066f95f3850SWill Newton host->data = NULL; 1067f95f3850SWill Newton set_bit(EVENT_DATA_COMPLETE, &host->completed_events); 1068f95f3850SWill Newton status = host->data_status; 1069f95f3850SWill Newton 1070f95f3850SWill Newton if (status & DW_MCI_DATA_ERROR_FLAGS) { 1071f95f3850SWill Newton if (status & SDMMC_INT_DTO) { 1072f95f3850SWill Newton data->error = -ETIMEDOUT; 1073f95f3850SWill Newton } else if (status & SDMMC_INT_DCRC) { 1074f95f3850SWill Newton data->error = -EILSEQ; 107555c5efbcSJames Hogan } else if (status & SDMMC_INT_EBE && 107655c5efbcSJames Hogan host->dir_status == 107755c5efbcSJames Hogan DW_MCI_SEND_STATUS) { 107855c5efbcSJames Hogan /* 107955c5efbcSJames Hogan * No data CRC status was returned. 108055c5efbcSJames Hogan * The number of bytes transferred will 108155c5efbcSJames Hogan * be exaggerated in PIO mode. 108255c5efbcSJames Hogan */ 108355c5efbcSJames Hogan data->bytes_xfered = 0; 108455c5efbcSJames Hogan data->error = -ETIMEDOUT; 1085f95f3850SWill Newton } else { 108662ca8034SShashidhar Hiremath dev_err(&host->dev, 1087f95f3850SWill Newton "data FIFO error " 1088f95f3850SWill Newton "(status=%08x)\n", 1089f95f3850SWill Newton status); 1090f95f3850SWill Newton data->error = -EIO; 1091f95f3850SWill Newton } 109294dd5b33SJames Hogan /* 109394dd5b33SJames Hogan * After an error, there may be data lingering 109494dd5b33SJames Hogan * in the FIFO, so reset it - doing so 109594dd5b33SJames Hogan * generates a block interrupt, hence setting 109694dd5b33SJames Hogan * the scatter-gather pointer to NULL. 109794dd5b33SJames Hogan */ 1098f9c2a0dcSSeungwon Jeon sg_miter_stop(&host->sg_miter); 109994dd5b33SJames Hogan host->sg = NULL; 110094dd5b33SJames Hogan ctrl = mci_readl(host, CTRL); 110194dd5b33SJames Hogan ctrl |= SDMMC_CTRL_FIFO_RESET; 110294dd5b33SJames Hogan mci_writel(host, CTRL, ctrl); 1103f95f3850SWill Newton } else { 1104f95f3850SWill Newton data->bytes_xfered = data->blocks * data->blksz; 1105f95f3850SWill Newton data->error = 0; 1106f95f3850SWill Newton } 1107f95f3850SWill Newton 1108f95f3850SWill Newton if (!data->stop) { 1109f95f3850SWill Newton dw_mci_request_end(host, host->mrq); 1110f95f3850SWill Newton goto unlock; 1111f95f3850SWill Newton } 1112f95f3850SWill Newton 1113053b3ce6SSeungwon Jeon if (host->mrq->sbc && !data->error) { 1114053b3ce6SSeungwon Jeon data->stop->error = 0; 1115053b3ce6SSeungwon Jeon dw_mci_request_end(host, host->mrq); 1116053b3ce6SSeungwon Jeon goto unlock; 1117053b3ce6SSeungwon Jeon } 1118053b3ce6SSeungwon Jeon 1119f95f3850SWill Newton prev_state = state = STATE_SENDING_STOP; 1120f95f3850SWill Newton if (!data->error) 1121f95f3850SWill Newton send_stop_cmd(host, data); 1122f95f3850SWill Newton /* fall through */ 1123f95f3850SWill Newton 1124f95f3850SWill Newton case STATE_SENDING_STOP: 1125f95f3850SWill Newton if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 1126f95f3850SWill Newton &host->pending_events)) 1127f95f3850SWill Newton break; 1128f95f3850SWill Newton 1129f95f3850SWill Newton host->cmd = NULL; 1130f95f3850SWill Newton dw_mci_command_complete(host, host->mrq->stop); 1131f95f3850SWill Newton dw_mci_request_end(host, host->mrq); 1132f95f3850SWill Newton goto unlock; 1133f95f3850SWill Newton 1134f95f3850SWill Newton case STATE_DATA_ERROR: 1135f95f3850SWill Newton if (!test_and_clear_bit(EVENT_XFER_COMPLETE, 1136f95f3850SWill Newton &host->pending_events)) 1137f95f3850SWill Newton break; 1138f95f3850SWill Newton 1139f95f3850SWill Newton state = STATE_DATA_BUSY; 1140f95f3850SWill Newton break; 1141f95f3850SWill Newton } 1142f95f3850SWill Newton } while (state != prev_state); 1143f95f3850SWill Newton 1144f95f3850SWill Newton host->state = state; 1145f95f3850SWill Newton unlock: 1146f95f3850SWill Newton spin_unlock(&host->lock); 1147f95f3850SWill Newton 1148f95f3850SWill Newton } 1149f95f3850SWill Newton 115034b664a2SJames Hogan /* push final bytes to part_buf, only use during push */ 115134b664a2SJames Hogan static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt) 115234b664a2SJames Hogan { 115334b664a2SJames Hogan memcpy((void *)&host->part_buf, buf, cnt); 115434b664a2SJames Hogan host->part_buf_count = cnt; 115534b664a2SJames Hogan } 115634b664a2SJames Hogan 115734b664a2SJames Hogan /* append bytes to part_buf, only use during push */ 115834b664a2SJames Hogan static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt) 115934b664a2SJames Hogan { 116034b664a2SJames Hogan cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count); 116134b664a2SJames Hogan memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt); 116234b664a2SJames Hogan host->part_buf_count += cnt; 116334b664a2SJames Hogan return cnt; 116434b664a2SJames Hogan } 116534b664a2SJames Hogan 116634b664a2SJames Hogan /* pull first bytes from part_buf, only use during pull */ 116734b664a2SJames Hogan static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt) 116834b664a2SJames Hogan { 116934b664a2SJames Hogan cnt = min(cnt, (int)host->part_buf_count); 117034b664a2SJames Hogan if (cnt) { 117134b664a2SJames Hogan memcpy(buf, (void *)&host->part_buf + host->part_buf_start, 117234b664a2SJames Hogan cnt); 117334b664a2SJames Hogan host->part_buf_count -= cnt; 117434b664a2SJames Hogan host->part_buf_start += cnt; 117534b664a2SJames Hogan } 117634b664a2SJames Hogan return cnt; 117734b664a2SJames Hogan } 117834b664a2SJames Hogan 117934b664a2SJames Hogan /* pull final bytes from the part_buf, assuming it's just been filled */ 118034b664a2SJames Hogan static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt) 118134b664a2SJames Hogan { 118234b664a2SJames Hogan memcpy(buf, &host->part_buf, cnt); 118334b664a2SJames Hogan host->part_buf_start = cnt; 118434b664a2SJames Hogan host->part_buf_count = (1 << host->data_shift) - cnt; 118534b664a2SJames Hogan } 118634b664a2SJames Hogan 1187f95f3850SWill Newton static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt) 1188f95f3850SWill Newton { 118934b664a2SJames Hogan /* try and push anything in the part_buf */ 119034b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 119134b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 119234b664a2SJames Hogan buf += len; 119334b664a2SJames Hogan cnt -= len; 119434b664a2SJames Hogan if (!sg_next(host->sg) || host->part_buf_count == 2) { 11954e0a5adfSJaehoon Chung mci_writew(host, DATA(host->data_offset), 11964e0a5adfSJaehoon Chung host->part_buf16); 119734b664a2SJames Hogan host->part_buf_count = 0; 119834b664a2SJames Hogan } 119934b664a2SJames Hogan } 120034b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 120134b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x1)) { 120234b664a2SJames Hogan while (cnt >= 2) { 120334b664a2SJames Hogan u16 aligned_buf[64]; 120434b664a2SJames Hogan int len = min(cnt & -2, (int)sizeof(aligned_buf)); 120534b664a2SJames Hogan int items = len >> 1; 120634b664a2SJames Hogan int i; 120734b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 120834b664a2SJames Hogan memcpy(aligned_buf, buf, len); 120934b664a2SJames Hogan buf += len; 121034b664a2SJames Hogan cnt -= len; 121134b664a2SJames Hogan /* push data from aligned buffer into fifo */ 121234b664a2SJames Hogan for (i = 0; i < items; ++i) 12134e0a5adfSJaehoon Chung mci_writew(host, DATA(host->data_offset), 12144e0a5adfSJaehoon Chung aligned_buf[i]); 121534b664a2SJames Hogan } 121634b664a2SJames Hogan } else 121734b664a2SJames Hogan #endif 121834b664a2SJames Hogan { 121934b664a2SJames Hogan u16 *pdata = buf; 122034b664a2SJames Hogan for (; cnt >= 2; cnt -= 2) 12214e0a5adfSJaehoon Chung mci_writew(host, DATA(host->data_offset), *pdata++); 122234b664a2SJames Hogan buf = pdata; 122334b664a2SJames Hogan } 122434b664a2SJames Hogan /* put anything remaining in the part_buf */ 122534b664a2SJames Hogan if (cnt) { 122634b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 122734b664a2SJames Hogan if (!sg_next(host->sg)) 12284e0a5adfSJaehoon Chung mci_writew(host, DATA(host->data_offset), 12294e0a5adfSJaehoon Chung host->part_buf16); 1230f95f3850SWill Newton } 1231f95f3850SWill Newton } 1232f95f3850SWill Newton 1233f95f3850SWill Newton static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt) 1234f95f3850SWill Newton { 123534b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 123634b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x1)) { 123734b664a2SJames Hogan while (cnt >= 2) { 123834b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 123934b664a2SJames Hogan u16 aligned_buf[64]; 124034b664a2SJames Hogan int len = min(cnt & -2, (int)sizeof(aligned_buf)); 124134b664a2SJames Hogan int items = len >> 1; 124234b664a2SJames Hogan int i; 124334b664a2SJames Hogan for (i = 0; i < items; ++i) 12444e0a5adfSJaehoon Chung aligned_buf[i] = mci_readw(host, 12454e0a5adfSJaehoon Chung DATA(host->data_offset)); 124634b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 124734b664a2SJames Hogan memcpy(buf, aligned_buf, len); 124834b664a2SJames Hogan buf += len; 124934b664a2SJames Hogan cnt -= len; 125034b664a2SJames Hogan } 125134b664a2SJames Hogan } else 125234b664a2SJames Hogan #endif 125334b664a2SJames Hogan { 125434b664a2SJames Hogan u16 *pdata = buf; 125534b664a2SJames Hogan for (; cnt >= 2; cnt -= 2) 12564e0a5adfSJaehoon Chung *pdata++ = mci_readw(host, DATA(host->data_offset)); 125734b664a2SJames Hogan buf = pdata; 125834b664a2SJames Hogan } 125934b664a2SJames Hogan if (cnt) { 12604e0a5adfSJaehoon Chung host->part_buf16 = mci_readw(host, DATA(host->data_offset)); 126134b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 1262f95f3850SWill Newton } 1263f95f3850SWill Newton } 1264f95f3850SWill Newton 1265f95f3850SWill Newton static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt) 1266f95f3850SWill Newton { 126734b664a2SJames Hogan /* try and push anything in the part_buf */ 126834b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 126934b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 127034b664a2SJames Hogan buf += len; 127134b664a2SJames Hogan cnt -= len; 127234b664a2SJames Hogan if (!sg_next(host->sg) || host->part_buf_count == 4) { 12734e0a5adfSJaehoon Chung mci_writel(host, DATA(host->data_offset), 12744e0a5adfSJaehoon Chung host->part_buf32); 127534b664a2SJames Hogan host->part_buf_count = 0; 127634b664a2SJames Hogan } 127734b664a2SJames Hogan } 127834b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 127934b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x3)) { 128034b664a2SJames Hogan while (cnt >= 4) { 128134b664a2SJames Hogan u32 aligned_buf[32]; 128234b664a2SJames Hogan int len = min(cnt & -4, (int)sizeof(aligned_buf)); 128334b664a2SJames Hogan int items = len >> 2; 128434b664a2SJames Hogan int i; 128534b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 128634b664a2SJames Hogan memcpy(aligned_buf, buf, len); 128734b664a2SJames Hogan buf += len; 128834b664a2SJames Hogan cnt -= len; 128934b664a2SJames Hogan /* push data from aligned buffer into fifo */ 129034b664a2SJames Hogan for (i = 0; i < items; ++i) 12914e0a5adfSJaehoon Chung mci_writel(host, DATA(host->data_offset), 12924e0a5adfSJaehoon Chung aligned_buf[i]); 129334b664a2SJames Hogan } 129434b664a2SJames Hogan } else 129534b664a2SJames Hogan #endif 129634b664a2SJames Hogan { 129734b664a2SJames Hogan u32 *pdata = buf; 129834b664a2SJames Hogan for (; cnt >= 4; cnt -= 4) 12994e0a5adfSJaehoon Chung mci_writel(host, DATA(host->data_offset), *pdata++); 130034b664a2SJames Hogan buf = pdata; 130134b664a2SJames Hogan } 130234b664a2SJames Hogan /* put anything remaining in the part_buf */ 130334b664a2SJames Hogan if (cnt) { 130434b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 130534b664a2SJames Hogan if (!sg_next(host->sg)) 13064e0a5adfSJaehoon Chung mci_writel(host, DATA(host->data_offset), 13074e0a5adfSJaehoon Chung host->part_buf32); 1308f95f3850SWill Newton } 1309f95f3850SWill Newton } 1310f95f3850SWill Newton 1311f95f3850SWill Newton static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt) 1312f95f3850SWill Newton { 131334b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 131434b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x3)) { 131534b664a2SJames Hogan while (cnt >= 4) { 131634b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 131734b664a2SJames Hogan u32 aligned_buf[32]; 131834b664a2SJames Hogan int len = min(cnt & -4, (int)sizeof(aligned_buf)); 131934b664a2SJames Hogan int items = len >> 2; 132034b664a2SJames Hogan int i; 132134b664a2SJames Hogan for (i = 0; i < items; ++i) 13224e0a5adfSJaehoon Chung aligned_buf[i] = mci_readl(host, 13234e0a5adfSJaehoon Chung DATA(host->data_offset)); 132434b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 132534b664a2SJames Hogan memcpy(buf, aligned_buf, len); 132634b664a2SJames Hogan buf += len; 132734b664a2SJames Hogan cnt -= len; 132834b664a2SJames Hogan } 132934b664a2SJames Hogan } else 133034b664a2SJames Hogan #endif 133134b664a2SJames Hogan { 133234b664a2SJames Hogan u32 *pdata = buf; 133334b664a2SJames Hogan for (; cnt >= 4; cnt -= 4) 13344e0a5adfSJaehoon Chung *pdata++ = mci_readl(host, DATA(host->data_offset)); 133534b664a2SJames Hogan buf = pdata; 133634b664a2SJames Hogan } 133734b664a2SJames Hogan if (cnt) { 13384e0a5adfSJaehoon Chung host->part_buf32 = mci_readl(host, DATA(host->data_offset)); 133934b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 1340f95f3850SWill Newton } 1341f95f3850SWill Newton } 1342f95f3850SWill Newton 1343f95f3850SWill Newton static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) 1344f95f3850SWill Newton { 134534b664a2SJames Hogan /* try and push anything in the part_buf */ 134634b664a2SJames Hogan if (unlikely(host->part_buf_count)) { 134734b664a2SJames Hogan int len = dw_mci_push_part_bytes(host, buf, cnt); 134834b664a2SJames Hogan buf += len; 134934b664a2SJames Hogan cnt -= len; 135034b664a2SJames Hogan if (!sg_next(host->sg) || host->part_buf_count == 8) { 13514e0a5adfSJaehoon Chung mci_writew(host, DATA(host->data_offset), 13524e0a5adfSJaehoon Chung host->part_buf); 135334b664a2SJames Hogan host->part_buf_count = 0; 135434b664a2SJames Hogan } 135534b664a2SJames Hogan } 135634b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 135734b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x7)) { 135834b664a2SJames Hogan while (cnt >= 8) { 135934b664a2SJames Hogan u64 aligned_buf[16]; 136034b664a2SJames Hogan int len = min(cnt & -8, (int)sizeof(aligned_buf)); 136134b664a2SJames Hogan int items = len >> 3; 136234b664a2SJames Hogan int i; 136334b664a2SJames Hogan /* memcpy from input buffer into aligned buffer */ 136434b664a2SJames Hogan memcpy(aligned_buf, buf, len); 136534b664a2SJames Hogan buf += len; 136634b664a2SJames Hogan cnt -= len; 136734b664a2SJames Hogan /* push data from aligned buffer into fifo */ 136834b664a2SJames Hogan for (i = 0; i < items; ++i) 13694e0a5adfSJaehoon Chung mci_writeq(host, DATA(host->data_offset), 13704e0a5adfSJaehoon Chung aligned_buf[i]); 137134b664a2SJames Hogan } 137234b664a2SJames Hogan } else 137334b664a2SJames Hogan #endif 137434b664a2SJames Hogan { 137534b664a2SJames Hogan u64 *pdata = buf; 137634b664a2SJames Hogan for (; cnt >= 8; cnt -= 8) 13774e0a5adfSJaehoon Chung mci_writeq(host, DATA(host->data_offset), *pdata++); 137834b664a2SJames Hogan buf = pdata; 137934b664a2SJames Hogan } 138034b664a2SJames Hogan /* put anything remaining in the part_buf */ 138134b664a2SJames Hogan if (cnt) { 138234b664a2SJames Hogan dw_mci_set_part_bytes(host, buf, cnt); 138334b664a2SJames Hogan if (!sg_next(host->sg)) 13844e0a5adfSJaehoon Chung mci_writeq(host, DATA(host->data_offset), 13854e0a5adfSJaehoon Chung host->part_buf); 1386f95f3850SWill Newton } 1387f95f3850SWill Newton } 1388f95f3850SWill Newton 1389f95f3850SWill Newton static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt) 1390f95f3850SWill Newton { 139134b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 139234b664a2SJames Hogan if (unlikely((unsigned long)buf & 0x7)) { 139334b664a2SJames Hogan while (cnt >= 8) { 139434b664a2SJames Hogan /* pull data from fifo into aligned buffer */ 139534b664a2SJames Hogan u64 aligned_buf[16]; 139634b664a2SJames Hogan int len = min(cnt & -8, (int)sizeof(aligned_buf)); 139734b664a2SJames Hogan int items = len >> 3; 139834b664a2SJames Hogan int i; 139934b664a2SJames Hogan for (i = 0; i < items; ++i) 14004e0a5adfSJaehoon Chung aligned_buf[i] = mci_readq(host, 14014e0a5adfSJaehoon Chung DATA(host->data_offset)); 140234b664a2SJames Hogan /* memcpy from aligned buffer into output buffer */ 140334b664a2SJames Hogan memcpy(buf, aligned_buf, len); 140434b664a2SJames Hogan buf += len; 140534b664a2SJames Hogan cnt -= len; 1406f95f3850SWill Newton } 140734b664a2SJames Hogan } else 140834b664a2SJames Hogan #endif 140934b664a2SJames Hogan { 141034b664a2SJames Hogan u64 *pdata = buf; 141134b664a2SJames Hogan for (; cnt >= 8; cnt -= 8) 14124e0a5adfSJaehoon Chung *pdata++ = mci_readq(host, DATA(host->data_offset)); 141334b664a2SJames Hogan buf = pdata; 141434b664a2SJames Hogan } 141534b664a2SJames Hogan if (cnt) { 14164e0a5adfSJaehoon Chung host->part_buf = mci_readq(host, DATA(host->data_offset)); 141734b664a2SJames Hogan dw_mci_pull_final_bytes(host, buf, cnt); 141834b664a2SJames Hogan } 141934b664a2SJames Hogan } 142034b664a2SJames Hogan 142134b664a2SJames Hogan static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt) 142234b664a2SJames Hogan { 142334b664a2SJames Hogan int len; 142434b664a2SJames Hogan 142534b664a2SJames Hogan /* get remaining partial bytes */ 142634b664a2SJames Hogan len = dw_mci_pull_part_bytes(host, buf, cnt); 142734b664a2SJames Hogan if (unlikely(len == cnt)) 142834b664a2SJames Hogan return; 142934b664a2SJames Hogan buf += len; 143034b664a2SJames Hogan cnt -= len; 143134b664a2SJames Hogan 143234b664a2SJames Hogan /* get the rest of the data */ 143334b664a2SJames Hogan host->pull_data(host, buf, cnt); 1434f95f3850SWill Newton } 1435f95f3850SWill Newton 1436f95f3850SWill Newton static void dw_mci_read_data_pio(struct dw_mci *host) 1437f95f3850SWill Newton { 1438f9c2a0dcSSeungwon Jeon struct sg_mapping_iter *sg_miter = &host->sg_miter; 1439f9c2a0dcSSeungwon Jeon void *buf; 1440f9c2a0dcSSeungwon Jeon unsigned int offset; 1441f95f3850SWill Newton struct mmc_data *data = host->data; 1442f95f3850SWill Newton int shift = host->data_shift; 1443f95f3850SWill Newton u32 status; 1444ba6a902dSChris Ball unsigned int nbytes = 0, len; 1445f9c2a0dcSSeungwon Jeon unsigned int remain, fcnt; 1446f95f3850SWill Newton 1447f95f3850SWill Newton do { 1448f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 1449f9c2a0dcSSeungwon Jeon goto done; 1450f95f3850SWill Newton 1451f9c2a0dcSSeungwon Jeon host->sg = sg_miter->__sg; 1452f9c2a0dcSSeungwon Jeon buf = sg_miter->addr; 1453f9c2a0dcSSeungwon Jeon remain = sg_miter->length; 1454f9c2a0dcSSeungwon Jeon offset = 0; 1455f9c2a0dcSSeungwon Jeon 1456f9c2a0dcSSeungwon Jeon do { 1457f9c2a0dcSSeungwon Jeon fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS)) 1458f9c2a0dcSSeungwon Jeon << shift) + host->part_buf_count; 1459f9c2a0dcSSeungwon Jeon len = min(remain, fcnt); 1460f9c2a0dcSSeungwon Jeon if (!len) 1461f9c2a0dcSSeungwon Jeon break; 1462f9c2a0dcSSeungwon Jeon dw_mci_pull_data(host, (void *)(buf + offset), len); 1463f95f3850SWill Newton offset += len; 1464f95f3850SWill Newton nbytes += len; 1465f9c2a0dcSSeungwon Jeon remain -= len; 1466f9c2a0dcSSeungwon Jeon } while (remain); 1467f95f3850SWill Newton 1468e74f3a9cSSeungwon Jeon sg_miter->consumed = offset; 1469f95f3850SWill Newton status = mci_readl(host, MINTSTS); 1470f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_RXDR); 1471f95f3850SWill Newton } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/ 1472f95f3850SWill Newton data->bytes_xfered += nbytes; 1473f9c2a0dcSSeungwon Jeon 1474f9c2a0dcSSeungwon Jeon if (!remain) { 1475f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 1476f9c2a0dcSSeungwon Jeon goto done; 1477f9c2a0dcSSeungwon Jeon sg_miter->consumed = 0; 1478f9c2a0dcSSeungwon Jeon } 1479f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 1480f95f3850SWill Newton return; 1481f95f3850SWill Newton 1482f95f3850SWill Newton done: 1483f95f3850SWill Newton data->bytes_xfered += nbytes; 1484f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 1485f9c2a0dcSSeungwon Jeon host->sg = NULL; 1486f95f3850SWill Newton smp_wmb(); 1487f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 1488f95f3850SWill Newton } 1489f95f3850SWill Newton 1490f95f3850SWill Newton static void dw_mci_write_data_pio(struct dw_mci *host) 1491f95f3850SWill Newton { 1492f9c2a0dcSSeungwon Jeon struct sg_mapping_iter *sg_miter = &host->sg_miter; 1493f9c2a0dcSSeungwon Jeon void *buf; 1494f9c2a0dcSSeungwon Jeon unsigned int offset; 1495f95f3850SWill Newton struct mmc_data *data = host->data; 1496f95f3850SWill Newton int shift = host->data_shift; 1497f95f3850SWill Newton u32 status; 1498f95f3850SWill Newton unsigned int nbytes = 0, len; 1499f9c2a0dcSSeungwon Jeon unsigned int fifo_depth = host->fifo_depth; 1500f9c2a0dcSSeungwon Jeon unsigned int remain, fcnt; 1501f95f3850SWill Newton 1502f95f3850SWill Newton do { 1503f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 1504f9c2a0dcSSeungwon Jeon goto done; 1505f95f3850SWill Newton 1506f9c2a0dcSSeungwon Jeon host->sg = sg_miter->__sg; 1507f9c2a0dcSSeungwon Jeon buf = sg_miter->addr; 1508f9c2a0dcSSeungwon Jeon remain = sg_miter->length; 1509f9c2a0dcSSeungwon Jeon offset = 0; 1510f9c2a0dcSSeungwon Jeon 1511f9c2a0dcSSeungwon Jeon do { 1512f9c2a0dcSSeungwon Jeon fcnt = ((fifo_depth - 1513f9c2a0dcSSeungwon Jeon SDMMC_GET_FCNT(mci_readl(host, STATUS))) 1514f9c2a0dcSSeungwon Jeon << shift) - host->part_buf_count; 1515f9c2a0dcSSeungwon Jeon len = min(remain, fcnt); 1516f9c2a0dcSSeungwon Jeon if (!len) 1517f9c2a0dcSSeungwon Jeon break; 1518f9c2a0dcSSeungwon Jeon host->push_data(host, (void *)(buf + offset), len); 1519f95f3850SWill Newton offset += len; 1520f95f3850SWill Newton nbytes += len; 1521f9c2a0dcSSeungwon Jeon remain -= len; 1522f9c2a0dcSSeungwon Jeon } while (remain); 1523f95f3850SWill Newton 1524e74f3a9cSSeungwon Jeon sg_miter->consumed = offset; 1525f95f3850SWill Newton status = mci_readl(host, MINTSTS); 1526f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_TXDR); 1527f95f3850SWill Newton } while (status & SDMMC_INT_TXDR); /* if TXDR write again */ 1528f95f3850SWill Newton data->bytes_xfered += nbytes; 1529f9c2a0dcSSeungwon Jeon 1530f9c2a0dcSSeungwon Jeon if (!remain) { 1531f9c2a0dcSSeungwon Jeon if (!sg_miter_next(sg_miter)) 1532f9c2a0dcSSeungwon Jeon goto done; 1533f9c2a0dcSSeungwon Jeon sg_miter->consumed = 0; 1534f9c2a0dcSSeungwon Jeon } 1535f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 1536f95f3850SWill Newton return; 1537f95f3850SWill Newton 1538f95f3850SWill Newton done: 1539f95f3850SWill Newton data->bytes_xfered += nbytes; 1540f9c2a0dcSSeungwon Jeon sg_miter_stop(sg_miter); 1541f9c2a0dcSSeungwon Jeon host->sg = NULL; 1542f95f3850SWill Newton smp_wmb(); 1543f95f3850SWill Newton set_bit(EVENT_XFER_COMPLETE, &host->pending_events); 1544f95f3850SWill Newton } 1545f95f3850SWill Newton 1546f95f3850SWill Newton static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) 1547f95f3850SWill Newton { 1548f95f3850SWill Newton if (!host->cmd_status) 1549f95f3850SWill Newton host->cmd_status = status; 1550f95f3850SWill Newton 1551f95f3850SWill Newton smp_wmb(); 1552f95f3850SWill Newton 1553f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 1554f95f3850SWill Newton tasklet_schedule(&host->tasklet); 1555f95f3850SWill Newton } 1556f95f3850SWill Newton 1557f95f3850SWill Newton static irqreturn_t dw_mci_interrupt(int irq, void *dev_id) 1558f95f3850SWill Newton { 1559f95f3850SWill Newton struct dw_mci *host = dev_id; 1560182c9081SSeungwon Jeon u32 pending; 1561f95f3850SWill Newton unsigned int pass_count = 0; 15621a5c8e1fSShashidhar Hiremath int i; 1563f95f3850SWill Newton 1564f95f3850SWill Newton do { 1565f95f3850SWill Newton pending = mci_readl(host, MINTSTS); /* read-only mask reg */ 1566f95f3850SWill Newton 1567f95f3850SWill Newton /* 1568f95f3850SWill Newton * DTO fix - version 2.10a and below, and only if internal DMA 1569f95f3850SWill Newton * is configured. 1570f95f3850SWill Newton */ 1571f95f3850SWill Newton if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) { 1572f95f3850SWill Newton if (!pending && 1573f95f3850SWill Newton ((mci_readl(host, STATUS) >> 17) & 0x1fff)) 1574f95f3850SWill Newton pending |= SDMMC_INT_DATA_OVER; 1575f95f3850SWill Newton } 1576f95f3850SWill Newton 1577f95f3850SWill Newton if (!pending) 1578f95f3850SWill Newton break; 1579f95f3850SWill Newton 1580f95f3850SWill Newton if (pending & DW_MCI_CMD_ERROR_FLAGS) { 1581f95f3850SWill Newton mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); 1582182c9081SSeungwon Jeon host->cmd_status = pending; 1583f95f3850SWill Newton smp_wmb(); 1584f95f3850SWill Newton set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 1585f95f3850SWill Newton } 1586f95f3850SWill Newton 1587f95f3850SWill Newton if (pending & DW_MCI_DATA_ERROR_FLAGS) { 1588f95f3850SWill Newton /* if there is an error report DATA_ERROR */ 1589f95f3850SWill Newton mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS); 1590182c9081SSeungwon Jeon host->data_status = pending; 1591f95f3850SWill Newton smp_wmb(); 1592f95f3850SWill Newton set_bit(EVENT_DATA_ERROR, &host->pending_events); 1593f95f3850SWill Newton tasklet_schedule(&host->tasklet); 1594f95f3850SWill Newton } 1595f95f3850SWill Newton 1596f95f3850SWill Newton if (pending & SDMMC_INT_DATA_OVER) { 1597f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER); 1598f95f3850SWill Newton if (!host->data_status) 1599182c9081SSeungwon Jeon host->data_status = pending; 1600f95f3850SWill Newton smp_wmb(); 1601f95f3850SWill Newton if (host->dir_status == DW_MCI_RECV_STATUS) { 1602f95f3850SWill Newton if (host->sg != NULL) 1603f95f3850SWill Newton dw_mci_read_data_pio(host); 1604f95f3850SWill Newton } 1605f95f3850SWill Newton set_bit(EVENT_DATA_COMPLETE, &host->pending_events); 1606f95f3850SWill Newton tasklet_schedule(&host->tasklet); 1607f95f3850SWill Newton } 1608f95f3850SWill Newton 1609f95f3850SWill Newton if (pending & SDMMC_INT_RXDR) { 1610f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_RXDR); 1611b40af3aaSJames Hogan if (host->dir_status == DW_MCI_RECV_STATUS && host->sg) 1612f95f3850SWill Newton dw_mci_read_data_pio(host); 1613f95f3850SWill Newton } 1614f95f3850SWill Newton 1615f95f3850SWill Newton if (pending & SDMMC_INT_TXDR) { 1616f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_TXDR); 1617b40af3aaSJames Hogan if (host->dir_status == DW_MCI_SEND_STATUS && host->sg) 1618f95f3850SWill Newton dw_mci_write_data_pio(host); 1619f95f3850SWill Newton } 1620f95f3850SWill Newton 1621f95f3850SWill Newton if (pending & SDMMC_INT_CMD_DONE) { 1622f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); 1623182c9081SSeungwon Jeon dw_mci_cmd_interrupt(host, pending); 1624f95f3850SWill Newton } 1625f95f3850SWill Newton 1626f95f3850SWill Newton if (pending & SDMMC_INT_CD) { 1627f95f3850SWill Newton mci_writel(host, RINTSTS, SDMMC_INT_CD); 162895dcc2cbSThomas Abraham queue_work(host->card_workqueue, &host->card_work); 1629f95f3850SWill Newton } 1630f95f3850SWill Newton 16311a5c8e1fSShashidhar Hiremath /* Handle SDIO Interrupts */ 16321a5c8e1fSShashidhar Hiremath for (i = 0; i < host->num_slots; i++) { 16331a5c8e1fSShashidhar Hiremath struct dw_mci_slot *slot = host->slot[i]; 16341a5c8e1fSShashidhar Hiremath if (pending & SDMMC_INT_SDIO(i)) { 16351a5c8e1fSShashidhar Hiremath mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i)); 16361a5c8e1fSShashidhar Hiremath mmc_signal_sdio_irq(slot->mmc); 16371a5c8e1fSShashidhar Hiremath } 16381a5c8e1fSShashidhar Hiremath } 16391a5c8e1fSShashidhar Hiremath 1640f95f3850SWill Newton } while (pass_count++ < 5); 1641f95f3850SWill Newton 1642f95f3850SWill Newton #ifdef CONFIG_MMC_DW_IDMAC 1643f95f3850SWill Newton /* Handle DMA interrupts */ 1644f95f3850SWill Newton pending = mci_readl(host, IDSTS); 1645f95f3850SWill Newton if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { 1646f95f3850SWill Newton mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI); 1647f95f3850SWill Newton mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI); 1648f95f3850SWill Newton host->dma_ops->complete(host); 1649f95f3850SWill Newton } 1650f95f3850SWill Newton #endif 1651f95f3850SWill Newton 1652f95f3850SWill Newton return IRQ_HANDLED; 1653f95f3850SWill Newton } 1654f95f3850SWill Newton 16551791b13eSJames Hogan static void dw_mci_work_routine_card(struct work_struct *work) 1656f95f3850SWill Newton { 16571791b13eSJames Hogan struct dw_mci *host = container_of(work, struct dw_mci, card_work); 1658f95f3850SWill Newton int i; 1659f95f3850SWill Newton 1660f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 1661f95f3850SWill Newton struct dw_mci_slot *slot = host->slot[i]; 1662f95f3850SWill Newton struct mmc_host *mmc = slot->mmc; 1663f95f3850SWill Newton struct mmc_request *mrq; 1664f95f3850SWill Newton int present; 1665f95f3850SWill Newton u32 ctrl; 1666f95f3850SWill Newton 1667f95f3850SWill Newton present = dw_mci_get_cd(mmc); 1668f95f3850SWill Newton while (present != slot->last_detect_state) { 1669f95f3850SWill Newton dev_dbg(&slot->mmc->class_dev, "card %s\n", 1670f95f3850SWill Newton present ? "inserted" : "removed"); 1671f95f3850SWill Newton 16721791b13eSJames Hogan /* Power up slot (before spin_lock, may sleep) */ 16731791b13eSJames Hogan if (present != 0 && host->pdata->setpower) 16741791b13eSJames Hogan host->pdata->setpower(slot->id, mmc->ocr_avail); 16751791b13eSJames Hogan 16761791b13eSJames Hogan spin_lock_bh(&host->lock); 16771791b13eSJames Hogan 1678f95f3850SWill Newton /* Card change detected */ 1679f95f3850SWill Newton slot->last_detect_state = present; 1680f95f3850SWill Newton 16811791b13eSJames Hogan /* Mark card as present if applicable */ 16821791b13eSJames Hogan if (present != 0) 1683f95f3850SWill Newton set_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1684f95f3850SWill Newton 1685f95f3850SWill Newton /* Clean up queue if present */ 1686f95f3850SWill Newton mrq = slot->mrq; 1687f95f3850SWill Newton if (mrq) { 1688f95f3850SWill Newton if (mrq == host->mrq) { 1689f95f3850SWill Newton host->data = NULL; 1690f95f3850SWill Newton host->cmd = NULL; 1691f95f3850SWill Newton 1692f95f3850SWill Newton switch (host->state) { 1693f95f3850SWill Newton case STATE_IDLE: 1694f95f3850SWill Newton break; 1695f95f3850SWill Newton case STATE_SENDING_CMD: 1696f95f3850SWill Newton mrq->cmd->error = -ENOMEDIUM; 1697f95f3850SWill Newton if (!mrq->data) 1698f95f3850SWill Newton break; 1699f95f3850SWill Newton /* fall through */ 1700f95f3850SWill Newton case STATE_SENDING_DATA: 1701f95f3850SWill Newton mrq->data->error = -ENOMEDIUM; 1702f95f3850SWill Newton dw_mci_stop_dma(host); 1703f95f3850SWill Newton break; 1704f95f3850SWill Newton case STATE_DATA_BUSY: 1705f95f3850SWill Newton case STATE_DATA_ERROR: 1706f95f3850SWill Newton if (mrq->data->error == -EINPROGRESS) 1707f95f3850SWill Newton mrq->data->error = -ENOMEDIUM; 1708f95f3850SWill Newton if (!mrq->stop) 1709f95f3850SWill Newton break; 1710f95f3850SWill Newton /* fall through */ 1711f95f3850SWill Newton case STATE_SENDING_STOP: 1712f95f3850SWill Newton mrq->stop->error = -ENOMEDIUM; 1713f95f3850SWill Newton break; 1714f95f3850SWill Newton } 1715f95f3850SWill Newton 1716f95f3850SWill Newton dw_mci_request_end(host, mrq); 1717f95f3850SWill Newton } else { 1718f95f3850SWill Newton list_del(&slot->queue_node); 1719f95f3850SWill Newton mrq->cmd->error = -ENOMEDIUM; 1720f95f3850SWill Newton if (mrq->data) 1721f95f3850SWill Newton mrq->data->error = -ENOMEDIUM; 1722f95f3850SWill Newton if (mrq->stop) 1723f95f3850SWill Newton mrq->stop->error = -ENOMEDIUM; 1724f95f3850SWill Newton 1725f95f3850SWill Newton spin_unlock(&host->lock); 1726f95f3850SWill Newton mmc_request_done(slot->mmc, mrq); 1727f95f3850SWill Newton spin_lock(&host->lock); 1728f95f3850SWill Newton } 1729f95f3850SWill Newton } 1730f95f3850SWill Newton 1731f95f3850SWill Newton /* Power down slot */ 1732f95f3850SWill Newton if (present == 0) { 1733f95f3850SWill Newton clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1734f95f3850SWill Newton 1735f95f3850SWill Newton /* 1736f95f3850SWill Newton * Clear down the FIFO - doing so generates a 1737f95f3850SWill Newton * block interrupt, hence setting the 1738f95f3850SWill Newton * scatter-gather pointer to NULL. 1739f95f3850SWill Newton */ 1740f9c2a0dcSSeungwon Jeon sg_miter_stop(&host->sg_miter); 1741f95f3850SWill Newton host->sg = NULL; 1742f95f3850SWill Newton 1743f95f3850SWill Newton ctrl = mci_readl(host, CTRL); 1744f95f3850SWill Newton ctrl |= SDMMC_CTRL_FIFO_RESET; 1745f95f3850SWill Newton mci_writel(host, CTRL, ctrl); 1746f95f3850SWill Newton 1747f95f3850SWill Newton #ifdef CONFIG_MMC_DW_IDMAC 1748f95f3850SWill Newton ctrl = mci_readl(host, BMOD); 1749141a712aSSeungwon Jeon /* Software reset of DMA */ 1750141a712aSSeungwon Jeon ctrl |= SDMMC_IDMAC_SWRESET; 1751f95f3850SWill Newton mci_writel(host, BMOD, ctrl); 1752f95f3850SWill Newton #endif 1753f95f3850SWill Newton 1754f95f3850SWill Newton } 1755f95f3850SWill Newton 17561791b13eSJames Hogan spin_unlock_bh(&host->lock); 17571791b13eSJames Hogan 17581791b13eSJames Hogan /* Power down slot (after spin_unlock, may sleep) */ 17591791b13eSJames Hogan if (present == 0 && host->pdata->setpower) 17601791b13eSJames Hogan host->pdata->setpower(slot->id, 0); 17611791b13eSJames Hogan 1762f95f3850SWill Newton present = dw_mci_get_cd(mmc); 1763f95f3850SWill Newton } 1764f95f3850SWill Newton 1765f95f3850SWill Newton mmc_detect_change(slot->mmc, 1766f95f3850SWill Newton msecs_to_jiffies(host->pdata->detect_delay_ms)); 1767f95f3850SWill Newton } 1768f95f3850SWill Newton } 1769f95f3850SWill Newton 1770f95f3850SWill Newton static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id) 1771f95f3850SWill Newton { 1772f95f3850SWill Newton struct mmc_host *mmc; 1773f95f3850SWill Newton struct dw_mci_slot *slot; 1774f95f3850SWill Newton 177562ca8034SShashidhar Hiremath mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->dev); 1776f95f3850SWill Newton if (!mmc) 1777f95f3850SWill Newton return -ENOMEM; 1778f95f3850SWill Newton 1779f95f3850SWill Newton slot = mmc_priv(mmc); 1780f95f3850SWill Newton slot->id = id; 1781f95f3850SWill Newton slot->mmc = mmc; 1782f95f3850SWill Newton slot->host = host; 1783f95f3850SWill Newton 1784f95f3850SWill Newton mmc->ops = &dw_mci_ops; 1785f95f3850SWill Newton mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510); 1786f95f3850SWill Newton mmc->f_max = host->bus_hz; 1787f95f3850SWill Newton 1788f95f3850SWill Newton if (host->pdata->get_ocr) 1789f95f3850SWill Newton mmc->ocr_avail = host->pdata->get_ocr(id); 1790f95f3850SWill Newton else 1791f95f3850SWill Newton mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 1792f95f3850SWill Newton 1793f95f3850SWill Newton /* 1794f95f3850SWill Newton * Start with slot power disabled, it will be enabled when a card 1795f95f3850SWill Newton * is detected. 1796f95f3850SWill Newton */ 1797f95f3850SWill Newton if (host->pdata->setpower) 1798f95f3850SWill Newton host->pdata->setpower(id, 0); 1799f95f3850SWill Newton 1800fc3d7720SJaehoon Chung if (host->pdata->caps) 1801fc3d7720SJaehoon Chung mmc->caps = host->pdata->caps; 1802fc3d7720SJaehoon Chung 18034f408cc6SSeungwon Jeon if (host->pdata->caps2) 18044f408cc6SSeungwon Jeon mmc->caps2 = host->pdata->caps2; 18054f408cc6SSeungwon Jeon 1806f95f3850SWill Newton if (host->pdata->get_bus_wd) 1807f95f3850SWill Newton if (host->pdata->get_bus_wd(slot->id) >= 4) 1808f95f3850SWill Newton mmc->caps |= MMC_CAP_4_BIT_DATA; 1809f95f3850SWill Newton 1810f95f3850SWill Newton if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED) 18116daa7778SSeungwon Jeon mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; 1812f95f3850SWill Newton 1813356ac2cfSJaehoon Chung if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY) 1814356ac2cfSJaehoon Chung mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT; 1815356ac2cfSJaehoon Chung else 1816356ac2cfSJaehoon Chung mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE; 1817356ac2cfSJaehoon Chung 1818f95f3850SWill Newton if (host->pdata->blk_settings) { 1819f95f3850SWill Newton mmc->max_segs = host->pdata->blk_settings->max_segs; 1820f95f3850SWill Newton mmc->max_blk_size = host->pdata->blk_settings->max_blk_size; 1821f95f3850SWill Newton mmc->max_blk_count = host->pdata->blk_settings->max_blk_count; 1822f95f3850SWill Newton mmc->max_req_size = host->pdata->blk_settings->max_req_size; 1823f95f3850SWill Newton mmc->max_seg_size = host->pdata->blk_settings->max_seg_size; 1824f95f3850SWill Newton } else { 1825f95f3850SWill Newton /* Useful defaults if platform data is unset. */ 1826a39e5746SJaehoon Chung #ifdef CONFIG_MMC_DW_IDMAC 1827a39e5746SJaehoon Chung mmc->max_segs = host->ring_size; 1828a39e5746SJaehoon Chung mmc->max_blk_size = 65536; 1829a39e5746SJaehoon Chung mmc->max_blk_count = host->ring_size; 1830a39e5746SJaehoon Chung mmc->max_seg_size = 0x1000; 1831a39e5746SJaehoon Chung mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count; 1832a39e5746SJaehoon Chung #else 1833f95f3850SWill Newton mmc->max_segs = 64; 1834f95f3850SWill Newton mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */ 1835f95f3850SWill Newton mmc->max_blk_count = 512; 1836f95f3850SWill Newton mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; 1837f95f3850SWill Newton mmc->max_seg_size = mmc->max_req_size; 1838f95f3850SWill Newton #endif /* CONFIG_MMC_DW_IDMAC */ 1839a39e5746SJaehoon Chung } 1840f95f3850SWill Newton 1841c07946a3SJaehoon Chung host->vmmc = regulator_get(mmc_dev(mmc), "vmmc"); 1842c07946a3SJaehoon Chung if (IS_ERR(host->vmmc)) { 1843a3c76eb9SGirish K S pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc)); 1844c07946a3SJaehoon Chung host->vmmc = NULL; 1845c07946a3SJaehoon Chung } else 1846c07946a3SJaehoon Chung regulator_enable(host->vmmc); 1847c07946a3SJaehoon Chung 1848f95f3850SWill Newton if (dw_mci_get_cd(mmc)) 1849f95f3850SWill Newton set_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1850f95f3850SWill Newton else 1851f95f3850SWill Newton clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); 1852f95f3850SWill Newton 1853f95f3850SWill Newton host->slot[id] = slot; 1854f95f3850SWill Newton mmc_add_host(mmc); 1855f95f3850SWill Newton 1856f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS) 1857f95f3850SWill Newton dw_mci_init_debugfs(slot); 1858f95f3850SWill Newton #endif 1859f95f3850SWill Newton 1860f95f3850SWill Newton /* Card initially undetected */ 1861f95f3850SWill Newton slot->last_detect_state = 0; 1862f95f3850SWill Newton 1863dd6c4b98SWill Newton /* 1864dd6c4b98SWill Newton * Card may have been plugged in prior to boot so we 1865dd6c4b98SWill Newton * need to run the detect tasklet 1866dd6c4b98SWill Newton */ 186795dcc2cbSThomas Abraham queue_work(host->card_workqueue, &host->card_work); 1868dd6c4b98SWill Newton 1869f95f3850SWill Newton return 0; 1870f95f3850SWill Newton } 1871f95f3850SWill Newton 1872f95f3850SWill Newton static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id) 1873f95f3850SWill Newton { 1874f95f3850SWill Newton /* Shutdown detect IRQ */ 1875f95f3850SWill Newton if (slot->host->pdata->exit) 1876f95f3850SWill Newton slot->host->pdata->exit(id); 1877f95f3850SWill Newton 1878f95f3850SWill Newton /* Debugfs stuff is cleaned up by mmc core */ 1879f95f3850SWill Newton mmc_remove_host(slot->mmc); 1880f95f3850SWill Newton slot->host->slot[id] = NULL; 1881f95f3850SWill Newton mmc_free_host(slot->mmc); 1882f95f3850SWill Newton } 1883f95f3850SWill Newton 1884f95f3850SWill Newton static void dw_mci_init_dma(struct dw_mci *host) 1885f95f3850SWill Newton { 1886f95f3850SWill Newton /* Alloc memory for sg translation */ 188762ca8034SShashidhar Hiremath host->sg_cpu = dma_alloc_coherent(&host->dev, PAGE_SIZE, 1888f95f3850SWill Newton &host->sg_dma, GFP_KERNEL); 1889f95f3850SWill Newton if (!host->sg_cpu) { 189062ca8034SShashidhar Hiremath dev_err(&host->dev, "%s: could not alloc DMA memory\n", 1891f95f3850SWill Newton __func__); 1892f95f3850SWill Newton goto no_dma; 1893f95f3850SWill Newton } 1894f95f3850SWill Newton 1895f95f3850SWill Newton /* Determine which DMA interface to use */ 1896f95f3850SWill Newton #ifdef CONFIG_MMC_DW_IDMAC 1897f95f3850SWill Newton host->dma_ops = &dw_mci_idmac_ops; 1898f95f3850SWill Newton #endif 1899f95f3850SWill Newton 1900f95f3850SWill Newton if (!host->dma_ops) 1901f95f3850SWill Newton goto no_dma; 1902f95f3850SWill Newton 1903e1631f98SJaehoon Chung if (host->dma_ops->init && host->dma_ops->start && 1904e1631f98SJaehoon Chung host->dma_ops->stop && host->dma_ops->cleanup) { 1905f95f3850SWill Newton if (host->dma_ops->init(host)) { 190662ca8034SShashidhar Hiremath dev_err(&host->dev, "%s: Unable to initialize " 1907f95f3850SWill Newton "DMA Controller.\n", __func__); 1908f95f3850SWill Newton goto no_dma; 1909f95f3850SWill Newton } 1910f95f3850SWill Newton } else { 191162ca8034SShashidhar Hiremath dev_err(&host->dev, "DMA initialization not found.\n"); 1912f95f3850SWill Newton goto no_dma; 1913f95f3850SWill Newton } 1914f95f3850SWill Newton 1915f95f3850SWill Newton host->use_dma = 1; 1916f95f3850SWill Newton return; 1917f95f3850SWill Newton 1918f95f3850SWill Newton no_dma: 191962ca8034SShashidhar Hiremath dev_info(&host->dev, "Using PIO mode.\n"); 1920f95f3850SWill Newton host->use_dma = 0; 1921f95f3850SWill Newton return; 1922f95f3850SWill Newton } 1923f95f3850SWill Newton 1924f95f3850SWill Newton static bool mci_wait_reset(struct device *dev, struct dw_mci *host) 1925f95f3850SWill Newton { 1926f95f3850SWill Newton unsigned long timeout = jiffies + msecs_to_jiffies(500); 1927f95f3850SWill Newton unsigned int ctrl; 1928f95f3850SWill Newton 1929f95f3850SWill Newton mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | 1930f95f3850SWill Newton SDMMC_CTRL_DMA_RESET)); 1931f95f3850SWill Newton 1932f95f3850SWill Newton /* wait till resets clear */ 1933f95f3850SWill Newton do { 1934f95f3850SWill Newton ctrl = mci_readl(host, CTRL); 1935f95f3850SWill Newton if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | 1936f95f3850SWill Newton SDMMC_CTRL_DMA_RESET))) 1937f95f3850SWill Newton return true; 1938f95f3850SWill Newton } while (time_before(jiffies, timeout)); 1939f95f3850SWill Newton 1940f95f3850SWill Newton dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl); 1941f95f3850SWill Newton 1942f95f3850SWill Newton return false; 1943f95f3850SWill Newton } 1944f95f3850SWill Newton 194562ca8034SShashidhar Hiremath int dw_mci_probe(struct dw_mci *host) 1946f95f3850SWill Newton { 194762ca8034SShashidhar Hiremath int width, i, ret = 0; 1948f95f3850SWill Newton u32 fifo_size; 1949f95f3850SWill Newton 195062ca8034SShashidhar Hiremath if (!host->pdata || !host->pdata->init) { 195162ca8034SShashidhar Hiremath dev_err(&host->dev, 1952f95f3850SWill Newton "Platform data must supply init function\n"); 195362ca8034SShashidhar Hiremath return -ENODEV; 1954f95f3850SWill Newton } 1955f95f3850SWill Newton 195662ca8034SShashidhar Hiremath if (!host->pdata->select_slot && host->pdata->num_slots > 1) { 195762ca8034SShashidhar Hiremath dev_err(&host->dev, 1958f95f3850SWill Newton "Platform data must supply select_slot function\n"); 195962ca8034SShashidhar Hiremath return -ENODEV; 1960f95f3850SWill Newton } 1961f95f3850SWill Newton 196262ca8034SShashidhar Hiremath if (!host->pdata->bus_hz) { 196362ca8034SShashidhar Hiremath dev_err(&host->dev, 1964f95f3850SWill Newton "Platform data must supply bus speed\n"); 196562ca8034SShashidhar Hiremath return -ENODEV; 1966f95f3850SWill Newton } 1967f95f3850SWill Newton 196862ca8034SShashidhar Hiremath host->bus_hz = host->pdata->bus_hz; 196962ca8034SShashidhar Hiremath host->quirks = host->pdata->quirks; 1970f95f3850SWill Newton 1971f95f3850SWill Newton spin_lock_init(&host->lock); 1972f95f3850SWill Newton INIT_LIST_HEAD(&host->queue); 1973f95f3850SWill Newton 1974f95f3850SWill Newton /* 1975f95f3850SWill Newton * Get the host data width - this assumes that HCON has been set with 1976f95f3850SWill Newton * the correct values. 1977f95f3850SWill Newton */ 1978f95f3850SWill Newton i = (mci_readl(host, HCON) >> 7) & 0x7; 1979f95f3850SWill Newton if (!i) { 1980f95f3850SWill Newton host->push_data = dw_mci_push_data16; 1981f95f3850SWill Newton host->pull_data = dw_mci_pull_data16; 1982f95f3850SWill Newton width = 16; 1983f95f3850SWill Newton host->data_shift = 1; 1984f95f3850SWill Newton } else if (i == 2) { 1985f95f3850SWill Newton host->push_data = dw_mci_push_data64; 1986f95f3850SWill Newton host->pull_data = dw_mci_pull_data64; 1987f95f3850SWill Newton width = 64; 1988f95f3850SWill Newton host->data_shift = 3; 1989f95f3850SWill Newton } else { 1990f95f3850SWill Newton /* Check for a reserved value, and warn if it is */ 1991f95f3850SWill Newton WARN((i != 1), 1992f95f3850SWill Newton "HCON reports a reserved host data width!\n" 1993f95f3850SWill Newton "Defaulting to 32-bit access.\n"); 1994f95f3850SWill Newton host->push_data = dw_mci_push_data32; 1995f95f3850SWill Newton host->pull_data = dw_mci_pull_data32; 1996f95f3850SWill Newton width = 32; 1997f95f3850SWill Newton host->data_shift = 2; 1998f95f3850SWill Newton } 1999f95f3850SWill Newton 2000f95f3850SWill Newton /* Reset all blocks */ 2001141a712aSSeungwon Jeon if (!mci_wait_reset(&host->dev, host)) 2002141a712aSSeungwon Jeon return -ENODEV; 2003141a712aSSeungwon Jeon 2004141a712aSSeungwon Jeon host->dma_ops = host->pdata->dma_ops; 2005141a712aSSeungwon Jeon dw_mci_init_dma(host); 2006f95f3850SWill Newton 2007f95f3850SWill Newton /* Clear the interrupts for the host controller */ 2008f95f3850SWill Newton mci_writel(host, RINTSTS, 0xFFFFFFFF); 2009f95f3850SWill Newton mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ 2010f95f3850SWill Newton 2011f95f3850SWill Newton /* Put in max timeout */ 2012f95f3850SWill Newton mci_writel(host, TMOUT, 0xFFFFFFFF); 2013f95f3850SWill Newton 2014f95f3850SWill Newton /* 2015f95f3850SWill Newton * FIFO threshold settings RxMark = fifo_size / 2 - 1, 2016f95f3850SWill Newton * Tx Mark = fifo_size / 2 DMA Size = 8 2017f95f3850SWill Newton */ 2018b86d8253SJames Hogan if (!host->pdata->fifo_depth) { 2019b86d8253SJames Hogan /* 2020b86d8253SJames Hogan * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may 2021b86d8253SJames Hogan * have been overwritten by the bootloader, just like we're 2022b86d8253SJames Hogan * about to do, so if you know the value for your hardware, you 2023b86d8253SJames Hogan * should put it in the platform data. 2024b86d8253SJames Hogan */ 2025f95f3850SWill Newton fifo_size = mci_readl(host, FIFOTH); 20268234e869SJaehoon Chung fifo_size = 1 + ((fifo_size >> 16) & 0xfff); 2027b86d8253SJames Hogan } else { 2028b86d8253SJames Hogan fifo_size = host->pdata->fifo_depth; 2029b86d8253SJames Hogan } 2030b86d8253SJames Hogan host->fifo_depth = fifo_size; 2031e61cf118SJaehoon Chung host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) | 2032e61cf118SJaehoon Chung ((fifo_size/2) << 0)); 2033e61cf118SJaehoon Chung mci_writel(host, FIFOTH, host->fifoth_val); 2034f95f3850SWill Newton 2035f95f3850SWill Newton /* disable clock to CIU */ 2036f95f3850SWill Newton mci_writel(host, CLKENA, 0); 2037f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 2038f95f3850SWill Newton 2039f95f3850SWill Newton tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host); 204095dcc2cbSThomas Abraham host->card_workqueue = alloc_workqueue("dw-mci-card", 20411791b13eSJames Hogan WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1); 204295dcc2cbSThomas Abraham if (!host->card_workqueue) 20431791b13eSJames Hogan goto err_dmaunmap; 20441791b13eSJames Hogan INIT_WORK(&host->card_work, dw_mci_work_routine_card); 204562ca8034SShashidhar Hiremath ret = request_irq(host->irq, dw_mci_interrupt, host->irq_flags, "dw-mci", host); 2046f95f3850SWill Newton if (ret) 20471791b13eSJames Hogan goto err_workqueue; 2048f95f3850SWill Newton 2049f95f3850SWill Newton if (host->pdata->num_slots) 2050f95f3850SWill Newton host->num_slots = host->pdata->num_slots; 2051f95f3850SWill Newton else 2052f95f3850SWill Newton host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1; 2053f95f3850SWill Newton 2054f95f3850SWill Newton /* We need at least one slot to succeed */ 2055f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 2056f95f3850SWill Newton ret = dw_mci_init_slot(host, i); 2057f95f3850SWill Newton if (ret) { 2058f95f3850SWill Newton ret = -ENODEV; 2059f95f3850SWill Newton goto err_init_slot; 2060f95f3850SWill Newton } 2061f95f3850SWill Newton } 2062f95f3850SWill Newton 2063f95f3850SWill Newton /* 20644e0a5adfSJaehoon Chung * In 2.40a spec, Data offset is changed. 20654e0a5adfSJaehoon Chung * Need to check the version-id and set data-offset for DATA register. 20664e0a5adfSJaehoon Chung */ 20674e0a5adfSJaehoon Chung host->verid = SDMMC_GET_VERID(mci_readl(host, VERID)); 206862ca8034SShashidhar Hiremath dev_info(&host->dev, "Version ID is %04x\n", host->verid); 20694e0a5adfSJaehoon Chung 20704e0a5adfSJaehoon Chung if (host->verid < DW_MMC_240A) 20714e0a5adfSJaehoon Chung host->data_offset = DATA_OFFSET; 20724e0a5adfSJaehoon Chung else 20734e0a5adfSJaehoon Chung host->data_offset = DATA_240A_OFFSET; 20744e0a5adfSJaehoon Chung 20754e0a5adfSJaehoon Chung /* 2076f95f3850SWill Newton * Enable interrupts for command done, data over, data empty, card det, 2077f95f3850SWill Newton * receive ready and error such as transmit, receive timeout, crc error 2078f95f3850SWill Newton */ 2079f95f3850SWill Newton mci_writel(host, RINTSTS, 0xFFFFFFFF); 2080f95f3850SWill Newton mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | 2081f95f3850SWill Newton SDMMC_INT_TXDR | SDMMC_INT_RXDR | 2082f95f3850SWill Newton DW_MCI_ERROR_FLAGS | SDMMC_INT_CD); 2083f95f3850SWill Newton mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */ 2084f95f3850SWill Newton 208562ca8034SShashidhar Hiremath dev_info(&host->dev, "DW MMC controller at irq %d, " 2086b86d8253SJames Hogan "%d bit host data width, " 2087b86d8253SJames Hogan "%u deep fifo\n", 208862ca8034SShashidhar Hiremath host->irq, width, fifo_size); 2089f95f3850SWill Newton if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) 209062ca8034SShashidhar Hiremath dev_info(&host->dev, "Internal DMAC interrupt fix enabled.\n"); 2091f95f3850SWill Newton 2092f95f3850SWill Newton return 0; 2093f95f3850SWill Newton 2094f95f3850SWill Newton err_init_slot: 2095f95f3850SWill Newton /* De-init any initialized slots */ 2096f95f3850SWill Newton while (i > 0) { 2097f95f3850SWill Newton if (host->slot[i]) 2098f95f3850SWill Newton dw_mci_cleanup_slot(host->slot[i], i); 2099f95f3850SWill Newton i--; 2100f95f3850SWill Newton } 210162ca8034SShashidhar Hiremath free_irq(host->irq, host); 2102f95f3850SWill Newton 21031791b13eSJames Hogan err_workqueue: 210495dcc2cbSThomas Abraham destroy_workqueue(host->card_workqueue); 21051791b13eSJames Hogan 2106f95f3850SWill Newton err_dmaunmap: 2107f95f3850SWill Newton if (host->use_dma && host->dma_ops->exit) 2108f95f3850SWill Newton host->dma_ops->exit(host); 210962ca8034SShashidhar Hiremath dma_free_coherent(&host->dev, PAGE_SIZE, 2110f95f3850SWill Newton host->sg_cpu, host->sg_dma); 2111f95f3850SWill Newton 2112c07946a3SJaehoon Chung if (host->vmmc) { 2113c07946a3SJaehoon Chung regulator_disable(host->vmmc); 2114c07946a3SJaehoon Chung regulator_put(host->vmmc); 2115c07946a3SJaehoon Chung } 2116f95f3850SWill Newton return ret; 2117f95f3850SWill Newton } 211862ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_probe); 2119f95f3850SWill Newton 212062ca8034SShashidhar Hiremath void dw_mci_remove(struct dw_mci *host) 2121f95f3850SWill Newton { 2122f95f3850SWill Newton int i; 2123f95f3850SWill Newton 2124f95f3850SWill Newton mci_writel(host, RINTSTS, 0xFFFFFFFF); 2125f95f3850SWill Newton mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ 2126f95f3850SWill Newton 2127f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 212862ca8034SShashidhar Hiremath dev_dbg(&host->dev, "remove slot %d\n", i); 2129f95f3850SWill Newton if (host->slot[i]) 2130f95f3850SWill Newton dw_mci_cleanup_slot(host->slot[i], i); 2131f95f3850SWill Newton } 2132f95f3850SWill Newton 2133f95f3850SWill Newton /* disable clock to CIU */ 2134f95f3850SWill Newton mci_writel(host, CLKENA, 0); 2135f95f3850SWill Newton mci_writel(host, CLKSRC, 0); 2136f95f3850SWill Newton 213762ca8034SShashidhar Hiremath free_irq(host->irq, host); 213895dcc2cbSThomas Abraham destroy_workqueue(host->card_workqueue); 213962ca8034SShashidhar Hiremath dma_free_coherent(&host->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); 2140f95f3850SWill Newton 2141f95f3850SWill Newton if (host->use_dma && host->dma_ops->exit) 2142f95f3850SWill Newton host->dma_ops->exit(host); 2143f95f3850SWill Newton 2144c07946a3SJaehoon Chung if (host->vmmc) { 2145c07946a3SJaehoon Chung regulator_disable(host->vmmc); 2146c07946a3SJaehoon Chung regulator_put(host->vmmc); 2147c07946a3SJaehoon Chung } 2148c07946a3SJaehoon Chung 2149f95f3850SWill Newton } 215062ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_remove); 215162ca8034SShashidhar Hiremath 215262ca8034SShashidhar Hiremath 2153f95f3850SWill Newton 21546fe8890dSJaehoon Chung #ifdef CONFIG_PM_SLEEP 2155f95f3850SWill Newton /* 2156f95f3850SWill Newton * TODO: we should probably disable the clock to the card in the suspend path. 2157f95f3850SWill Newton */ 215862ca8034SShashidhar Hiremath int dw_mci_suspend(struct dw_mci *host) 2159f95f3850SWill Newton { 216062ca8034SShashidhar Hiremath int i, ret = 0; 2161f95f3850SWill Newton 2162f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 2163f95f3850SWill Newton struct dw_mci_slot *slot = host->slot[i]; 2164f95f3850SWill Newton if (!slot) 2165f95f3850SWill Newton continue; 2166f95f3850SWill Newton ret = mmc_suspend_host(slot->mmc); 2167f95f3850SWill Newton if (ret < 0) { 2168f95f3850SWill Newton while (--i >= 0) { 2169f95f3850SWill Newton slot = host->slot[i]; 2170f95f3850SWill Newton if (slot) 2171f95f3850SWill Newton mmc_resume_host(host->slot[i]->mmc); 2172f95f3850SWill Newton } 2173f95f3850SWill Newton return ret; 2174f95f3850SWill Newton } 2175f95f3850SWill Newton } 2176f95f3850SWill Newton 2177c07946a3SJaehoon Chung if (host->vmmc) 2178c07946a3SJaehoon Chung regulator_disable(host->vmmc); 2179c07946a3SJaehoon Chung 2180f95f3850SWill Newton return 0; 2181f95f3850SWill Newton } 218262ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_suspend); 2183f95f3850SWill Newton 218462ca8034SShashidhar Hiremath int dw_mci_resume(struct dw_mci *host) 2185f95f3850SWill Newton { 2186f95f3850SWill Newton int i, ret; 2187f95f3850SWill Newton 21881d6c4e0aSJaehoon Chung if (host->vmmc) 21891d6c4e0aSJaehoon Chung regulator_enable(host->vmmc); 21901d6c4e0aSJaehoon Chung 219162ca8034SShashidhar Hiremath if (!mci_wait_reset(&host->dev, host)) { 2192e61cf118SJaehoon Chung ret = -ENODEV; 2193e61cf118SJaehoon Chung return ret; 2194e61cf118SJaehoon Chung } 2195e61cf118SJaehoon Chung 21963bfe619dSJonathan Kliegman if (host->use_dma && host->dma_ops->init) 2197141a712aSSeungwon Jeon host->dma_ops->init(host); 2198141a712aSSeungwon Jeon 2199e61cf118SJaehoon Chung /* Restore the old value at FIFOTH register */ 2200e61cf118SJaehoon Chung mci_writel(host, FIFOTH, host->fifoth_val); 2201e61cf118SJaehoon Chung 2202e61cf118SJaehoon Chung mci_writel(host, RINTSTS, 0xFFFFFFFF); 2203e61cf118SJaehoon Chung mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | 2204e61cf118SJaehoon Chung SDMMC_INT_TXDR | SDMMC_INT_RXDR | 2205e61cf118SJaehoon Chung DW_MCI_ERROR_FLAGS | SDMMC_INT_CD); 2206e61cf118SJaehoon Chung mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); 2207e61cf118SJaehoon Chung 2208f95f3850SWill Newton for (i = 0; i < host->num_slots; i++) { 2209f95f3850SWill Newton struct dw_mci_slot *slot = host->slot[i]; 2210f95f3850SWill Newton if (!slot) 2211f95f3850SWill Newton continue; 2212f95f3850SWill Newton ret = mmc_resume_host(host->slot[i]->mmc); 2213f95f3850SWill Newton if (ret < 0) 2214f95f3850SWill Newton return ret; 2215f95f3850SWill Newton } 2216f95f3850SWill Newton return 0; 2217f95f3850SWill Newton } 221862ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_resume); 22196fe8890dSJaehoon Chung #endif /* CONFIG_PM_SLEEP */ 22206fe8890dSJaehoon Chung 2221f95f3850SWill Newton static int __init dw_mci_init(void) 2222f95f3850SWill Newton { 222362ca8034SShashidhar Hiremath printk(KERN_INFO "Synopsys Designware Multimedia Card Interface Driver"); 222462ca8034SShashidhar Hiremath return 0; 2225f95f3850SWill Newton } 2226f95f3850SWill Newton 2227f95f3850SWill Newton static void __exit dw_mci_exit(void) 2228f95f3850SWill Newton { 2229f95f3850SWill Newton } 2230f95f3850SWill Newton 2231f95f3850SWill Newton module_init(dw_mci_init); 2232f95f3850SWill Newton module_exit(dw_mci_exit); 2233f95f3850SWill Newton 2234f95f3850SWill Newton MODULE_DESCRIPTION("DW Multimedia Card Interface driver"); 2235f95f3850SWill Newton MODULE_AUTHOR("NXP Semiconductor VietNam"); 2236f95f3850SWill Newton MODULE_AUTHOR("Imagination Technologies Ltd"); 2237f95f3850SWill Newton MODULE_LICENSE("GPL v2"); 2238