Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Synopsys DesignWare Multimedia Card Interface driver |
| 3 | * (Based on NXP driver for lpc 31xx) |
| 4 | * |
| 5 | * Copyright (C) 2009 NXP Semiconductors |
| 6 | * Copyright (C) 2009, 2010 Imagination Technologies Ltd. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/debugfs.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/ioport.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/stat.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/irq.h> |
| 30 | #include <linux/mmc/host.h> |
| 31 | #include <linux/mmc/mmc.h> |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 32 | #include <linux/mmc/sd.h> |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 33 | #include <linux/mmc/sdio.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 34 | #include <linux/mmc/dw_mmc.h> |
| 35 | #include <linux/bitops.h> |
Jaehoon Chung | c07946a | 2011-02-25 11:08:14 +0900 | [diff] [blame] | 36 | #include <linux/regulator/consumer.h> |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 37 | #include <linux/of.h> |
Doug Anderson | 55a6ceb | 2013-01-11 17:03:53 +0000 | [diff] [blame] | 38 | #include <linux/of_gpio.h> |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 39 | #include <linux/mmc/slot-gpio.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 40 | |
| 41 | #include "dw_mmc.h" |
| 42 | |
| 43 | /* Common flag combinations */ |
Jaehoon Chung | 3f7eec6 | 2013-05-27 13:47:57 +0900 | [diff] [blame] | 44 | #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 45 | SDMMC_INT_HTO | SDMMC_INT_SBE | \ |
| 46 | SDMMC_INT_EBE) |
| 47 | #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \ |
| 48 | SDMMC_INT_RESP_ERR) |
| 49 | #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \ |
| 50 | DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE) |
| 51 | #define DW_MCI_SEND_STATUS 1 |
| 52 | #define DW_MCI_RECV_STATUS 2 |
| 53 | #define DW_MCI_DMA_THRESHOLD 16 |
| 54 | |
Seungwon Jeon | 1f44a2a | 2013-08-31 00:13:31 +0900 | [diff] [blame] | 55 | #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */ |
| 56 | #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */ |
| 57 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 58 | #ifdef CONFIG_MMC_DW_IDMAC |
Joonyoung Shim | fc79a4d | 2013-04-26 15:35:22 +0900 | [diff] [blame] | 59 | #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \ |
| 60 | SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \ |
| 61 | SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \ |
| 62 | SDMMC_IDMAC_INT_TI) |
| 63 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 64 | struct idmac_desc_64addr { |
| 65 | u32 des0; /* Control Descriptor */ |
| 66 | |
| 67 | u32 des1; /* Reserved */ |
| 68 | |
| 69 | u32 des2; /*Buffer sizes */ |
| 70 | #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \ |
| 71 | ((d)->des2 = ((d)->des2 & 0x03ffe000) | ((s) & 0x1fff)) |
| 72 | |
| 73 | u32 des3; /* Reserved */ |
| 74 | |
| 75 | u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/ |
| 76 | u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/ |
| 77 | |
| 78 | u32 des6; /* Lower 32-bits of Next Descriptor Address */ |
| 79 | u32 des7; /* Upper 32-bits of Next Descriptor Address */ |
| 80 | }; |
| 81 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 82 | struct idmac_desc { |
| 83 | u32 des0; /* Control Descriptor */ |
| 84 | #define IDMAC_DES0_DIC BIT(1) |
| 85 | #define IDMAC_DES0_LD BIT(2) |
| 86 | #define IDMAC_DES0_FD BIT(3) |
| 87 | #define IDMAC_DES0_CH BIT(4) |
| 88 | #define IDMAC_DES0_ER BIT(5) |
| 89 | #define IDMAC_DES0_CES BIT(30) |
| 90 | #define IDMAC_DES0_OWN BIT(31) |
| 91 | |
| 92 | u32 des1; /* Buffer sizes */ |
| 93 | #define IDMAC_SET_BUFFER1_SIZE(d, s) \ |
Shashidhar Hiremath | 9b7bbe1 | 2011-07-29 08:49:50 -0400 | [diff] [blame] | 94 | ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 95 | |
| 96 | u32 des2; /* buffer 1 physical address */ |
| 97 | |
| 98 | u32 des3; /* buffer 2 physical address */ |
| 99 | }; |
| 100 | #endif /* CONFIG_MMC_DW_IDMAC */ |
| 101 | |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 102 | static bool dw_mci_reset(struct dw_mci *host); |
Sonny Rao | 536f6b9 | 2014-10-16 09:58:05 -0700 | [diff] [blame] | 103 | static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset); |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 104 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 105 | #if defined(CONFIG_DEBUG_FS) |
| 106 | static int dw_mci_req_show(struct seq_file *s, void *v) |
| 107 | { |
| 108 | struct dw_mci_slot *slot = s->private; |
| 109 | struct mmc_request *mrq; |
| 110 | struct mmc_command *cmd; |
| 111 | struct mmc_command *stop; |
| 112 | struct mmc_data *data; |
| 113 | |
| 114 | /* Make sure we get a consistent snapshot */ |
| 115 | spin_lock_bh(&slot->host->lock); |
| 116 | mrq = slot->mrq; |
| 117 | |
| 118 | if (mrq) { |
| 119 | cmd = mrq->cmd; |
| 120 | data = mrq->data; |
| 121 | stop = mrq->stop; |
| 122 | |
| 123 | if (cmd) |
| 124 | seq_printf(s, |
| 125 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 126 | cmd->opcode, cmd->arg, cmd->flags, |
| 127 | cmd->resp[0], cmd->resp[1], cmd->resp[2], |
| 128 | cmd->resp[2], cmd->error); |
| 129 | if (data) |
| 130 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", |
| 131 | data->bytes_xfered, data->blocks, |
| 132 | data->blksz, data->flags, data->error); |
| 133 | if (stop) |
| 134 | seq_printf(s, |
| 135 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 136 | stop->opcode, stop->arg, stop->flags, |
| 137 | stop->resp[0], stop->resp[1], stop->resp[2], |
| 138 | stop->resp[2], stop->error); |
| 139 | } |
| 140 | |
| 141 | spin_unlock_bh(&slot->host->lock); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int dw_mci_req_open(struct inode *inode, struct file *file) |
| 147 | { |
| 148 | return single_open(file, dw_mci_req_show, inode->i_private); |
| 149 | } |
| 150 | |
| 151 | static const struct file_operations dw_mci_req_fops = { |
| 152 | .owner = THIS_MODULE, |
| 153 | .open = dw_mci_req_open, |
| 154 | .read = seq_read, |
| 155 | .llseek = seq_lseek, |
| 156 | .release = single_release, |
| 157 | }; |
| 158 | |
| 159 | static int dw_mci_regs_show(struct seq_file *s, void *v) |
| 160 | { |
| 161 | seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS); |
| 162 | seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS); |
| 163 | seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD); |
| 164 | seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL); |
| 165 | seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK); |
| 166 | seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int dw_mci_regs_open(struct inode *inode, struct file *file) |
| 172 | { |
| 173 | return single_open(file, dw_mci_regs_show, inode->i_private); |
| 174 | } |
| 175 | |
| 176 | static const struct file_operations dw_mci_regs_fops = { |
| 177 | .owner = THIS_MODULE, |
| 178 | .open = dw_mci_regs_open, |
| 179 | .read = seq_read, |
| 180 | .llseek = seq_lseek, |
| 181 | .release = single_release, |
| 182 | }; |
| 183 | |
| 184 | static void dw_mci_init_debugfs(struct dw_mci_slot *slot) |
| 185 | { |
| 186 | struct mmc_host *mmc = slot->mmc; |
| 187 | struct dw_mci *host = slot->host; |
| 188 | struct dentry *root; |
| 189 | struct dentry *node; |
| 190 | |
| 191 | root = mmc->debugfs_root; |
| 192 | if (!root) |
| 193 | return; |
| 194 | |
| 195 | node = debugfs_create_file("regs", S_IRUSR, root, host, |
| 196 | &dw_mci_regs_fops); |
| 197 | if (!node) |
| 198 | goto err; |
| 199 | |
| 200 | node = debugfs_create_file("req", S_IRUSR, root, slot, |
| 201 | &dw_mci_req_fops); |
| 202 | if (!node) |
| 203 | goto err; |
| 204 | |
| 205 | node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
| 206 | if (!node) |
| 207 | goto err; |
| 208 | |
| 209 | node = debugfs_create_x32("pending_events", S_IRUSR, root, |
| 210 | (u32 *)&host->pending_events); |
| 211 | if (!node) |
| 212 | goto err; |
| 213 | |
| 214 | node = debugfs_create_x32("completed_events", S_IRUSR, root, |
| 215 | (u32 *)&host->completed_events); |
| 216 | if (!node) |
| 217 | goto err; |
| 218 | |
| 219 | return; |
| 220 | |
| 221 | err: |
| 222 | dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
| 223 | } |
| 224 | #endif /* defined(CONFIG_DEBUG_FS) */ |
| 225 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 226 | static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg); |
| 227 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 228 | static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd) |
| 229 | { |
| 230 | struct mmc_data *data; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 231 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 232 | struct dw_mci *host = slot->host; |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 233 | const struct dw_mci_drv_data *drv_data = slot->host->drv_data; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 234 | u32 cmdr; |
| 235 | cmd->error = -EINPROGRESS; |
| 236 | |
| 237 | cmdr = cmd->opcode; |
| 238 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 239 | if (cmd->opcode == MMC_STOP_TRANSMISSION || |
| 240 | cmd->opcode == MMC_GO_IDLE_STATE || |
| 241 | cmd->opcode == MMC_GO_INACTIVE_STATE || |
| 242 | (cmd->opcode == SD_IO_RW_DIRECT && |
| 243 | ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 244 | cmdr |= SDMMC_CMD_STOP; |
Jaehoon Chung | 4a1b27a | 2014-03-03 11:36:44 +0900 | [diff] [blame] | 245 | else if (cmd->opcode != MMC_SEND_STATUS && cmd->data) |
| 246 | cmdr |= SDMMC_CMD_PRV_DAT_WAIT; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 247 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 248 | if (cmd->opcode == SD_SWITCH_VOLTAGE) { |
| 249 | u32 clk_en_a; |
| 250 | |
| 251 | /* Special bit makes CMD11 not die */ |
| 252 | cmdr |= SDMMC_CMD_VOLT_SWITCH; |
| 253 | |
| 254 | /* Change state to continue to handle CMD11 weirdness */ |
| 255 | WARN_ON(slot->host->state != STATE_SENDING_CMD); |
| 256 | slot->host->state = STATE_SENDING_CMD11; |
| 257 | |
| 258 | /* |
| 259 | * We need to disable low power mode (automatic clock stop) |
| 260 | * while doing voltage switch so we don't confuse the card, |
| 261 | * since stopping the clock is a specific part of the UHS |
| 262 | * voltage change dance. |
| 263 | * |
| 264 | * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be |
| 265 | * unconditionally turned back on in dw_mci_setup_bus() if it's |
| 266 | * ever called with a non-zero clock. That shouldn't happen |
| 267 | * until the voltage change is all done. |
| 268 | */ |
| 269 | clk_en_a = mci_readl(host, CLKENA); |
| 270 | clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id); |
| 271 | mci_writel(host, CLKENA, clk_en_a); |
| 272 | mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | |
| 273 | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 274 | } |
| 275 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 276 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 277 | /* We expect a response, so set this bit */ |
| 278 | cmdr |= SDMMC_CMD_RESP_EXP; |
| 279 | if (cmd->flags & MMC_RSP_136) |
| 280 | cmdr |= SDMMC_CMD_RESP_LONG; |
| 281 | } |
| 282 | |
| 283 | if (cmd->flags & MMC_RSP_CRC) |
| 284 | cmdr |= SDMMC_CMD_RESP_CRC; |
| 285 | |
| 286 | data = cmd->data; |
| 287 | if (data) { |
| 288 | cmdr |= SDMMC_CMD_DAT_EXP; |
| 289 | if (data->flags & MMC_DATA_STREAM) |
| 290 | cmdr |= SDMMC_CMD_STRM_MODE; |
| 291 | if (data->flags & MMC_DATA_WRITE) |
| 292 | cmdr |= SDMMC_CMD_DAT_WR; |
| 293 | } |
| 294 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 295 | if (drv_data && drv_data->prepare_command) |
| 296 | drv_data->prepare_command(slot->host, &cmdr); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 297 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 298 | return cmdr; |
| 299 | } |
| 300 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 301 | static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd) |
| 302 | { |
| 303 | struct mmc_command *stop; |
| 304 | u32 cmdr; |
| 305 | |
| 306 | if (!cmd->data) |
| 307 | return 0; |
| 308 | |
| 309 | stop = &host->stop_abort; |
| 310 | cmdr = cmd->opcode; |
| 311 | memset(stop, 0, sizeof(struct mmc_command)); |
| 312 | |
| 313 | if (cmdr == MMC_READ_SINGLE_BLOCK || |
| 314 | cmdr == MMC_READ_MULTIPLE_BLOCK || |
| 315 | cmdr == MMC_WRITE_BLOCK || |
| 316 | cmdr == MMC_WRITE_MULTIPLE_BLOCK) { |
| 317 | stop->opcode = MMC_STOP_TRANSMISSION; |
| 318 | stop->arg = 0; |
| 319 | stop->flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 320 | } else if (cmdr == SD_IO_RW_EXTENDED) { |
| 321 | stop->opcode = SD_IO_RW_DIRECT; |
| 322 | stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) | |
| 323 | ((cmd->arg >> 28) & 0x7); |
| 324 | stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC; |
| 325 | } else { |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | cmdr = stop->opcode | SDMMC_CMD_STOP | |
| 330 | SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP; |
| 331 | |
| 332 | return cmdr; |
| 333 | } |
| 334 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 335 | static void dw_mci_start_command(struct dw_mci *host, |
| 336 | struct mmc_command *cmd, u32 cmd_flags) |
| 337 | { |
| 338 | host->cmd = cmd; |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 339 | dev_vdbg(host->dev, |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 340 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
| 341 | cmd->arg, cmd_flags); |
| 342 | |
| 343 | mci_writel(host, CMDARG, cmd->arg); |
| 344 | wmb(); |
| 345 | |
| 346 | mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); |
| 347 | } |
| 348 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 349 | static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 350 | { |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 351 | struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort; |
| 352 | dw_mci_start_command(host, stop, host->stop_cmdr); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* DMA interface functions */ |
| 356 | static void dw_mci_stop_dma(struct dw_mci *host) |
| 357 | { |
James Hogan | 03e8cb5 | 2011-06-29 09:28:43 +0100 | [diff] [blame] | 358 | if (host->using_dma) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 359 | host->dma_ops->stop(host); |
| 360 | host->dma_ops->cleanup(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 361 | } |
Seungwon Jeon | aa50f25 | 2013-08-31 00:14:38 +0900 | [diff] [blame] | 362 | |
| 363 | /* Data transfer was stopped by the interrupt handler */ |
| 364 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 367 | static int dw_mci_get_dma_dir(struct mmc_data *data) |
| 368 | { |
| 369 | if (data->flags & MMC_DATA_WRITE) |
| 370 | return DMA_TO_DEVICE; |
| 371 | else |
| 372 | return DMA_FROM_DEVICE; |
| 373 | } |
| 374 | |
Jaehoon Chung | 9beee91 | 2012-02-16 11:19:38 +0900 | [diff] [blame] | 375 | #ifdef CONFIG_MMC_DW_IDMAC |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 376 | static void dw_mci_dma_cleanup(struct dw_mci *host) |
| 377 | { |
| 378 | struct mmc_data *data = host->data; |
| 379 | |
| 380 | if (data) |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 381 | if (!data->host_cookie) |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 382 | dma_unmap_sg(host->dev, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 383 | data->sg, |
| 384 | data->sg_len, |
| 385 | dw_mci_get_dma_dir(data)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 388 | static void dw_mci_idmac_reset(struct dw_mci *host) |
| 389 | { |
| 390 | u32 bmod = mci_readl(host, BMOD); |
| 391 | /* Software reset of DMA */ |
| 392 | bmod |= SDMMC_IDMAC_SWRESET; |
| 393 | mci_writel(host, BMOD, bmod); |
| 394 | } |
| 395 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 396 | static void dw_mci_idmac_stop_dma(struct dw_mci *host) |
| 397 | { |
| 398 | u32 temp; |
| 399 | |
| 400 | /* Disable and reset the IDMAC interface */ |
| 401 | temp = mci_readl(host, CTRL); |
| 402 | temp &= ~SDMMC_CTRL_USE_IDMAC; |
| 403 | temp |= SDMMC_CTRL_DMA_RESET; |
| 404 | mci_writel(host, CTRL, temp); |
| 405 | |
| 406 | /* Stop the IDMAC running */ |
| 407 | temp = mci_readl(host, BMOD); |
Jaehoon Chung | a5289a4 | 2011-02-25 11:08:13 +0900 | [diff] [blame] | 408 | temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB); |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 409 | temp |= SDMMC_IDMAC_SWRESET; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 410 | mci_writel(host, BMOD, temp); |
| 411 | } |
| 412 | |
| 413 | static void dw_mci_idmac_complete_dma(struct dw_mci *host) |
| 414 | { |
| 415 | struct mmc_data *data = host->data; |
| 416 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 417 | dev_vdbg(host->dev, "DMA complete\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 418 | |
| 419 | host->dma_ops->cleanup(host); |
| 420 | |
| 421 | /* |
| 422 | * If the card was removed, data will be NULL. No point in trying to |
| 423 | * send the stop command or waiting for NBUSY in this case. |
| 424 | */ |
| 425 | if (data) { |
| 426 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 427 | tasklet_schedule(&host->tasklet); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data, |
| 432 | unsigned int sg_len) |
| 433 | { |
| 434 | int i; |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 435 | if (host->dma_64bit_address == 1) { |
| 436 | struct idmac_desc_64addr *desc = host->sg_cpu; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 437 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 438 | for (i = 0; i < sg_len; i++, desc++) { |
| 439 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 440 | u64 mem_addr = sg_dma_address(&data->sg[i]); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 441 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 442 | /* |
| 443 | * Set the OWN bit and disable interrupts for this |
| 444 | * descriptor |
| 445 | */ |
| 446 | desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | |
| 447 | IDMAC_DES0_CH; |
| 448 | /* Buffer length */ |
| 449 | IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, length); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 450 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 451 | /* Physical address to DMA to/from */ |
| 452 | desc->des4 = mem_addr & 0xffffffff; |
| 453 | desc->des5 = mem_addr >> 32; |
| 454 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 455 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 456 | /* Set first descriptor */ |
| 457 | desc = host->sg_cpu; |
| 458 | desc->des0 |= IDMAC_DES0_FD; |
| 459 | |
| 460 | /* Set last descriptor */ |
| 461 | desc = host->sg_cpu + (i - 1) * |
| 462 | sizeof(struct idmac_desc_64addr); |
| 463 | desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); |
| 464 | desc->des0 |= IDMAC_DES0_LD; |
| 465 | |
| 466 | } else { |
| 467 | struct idmac_desc *desc = host->sg_cpu; |
| 468 | |
| 469 | for (i = 0; i < sg_len; i++, desc++) { |
| 470 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 471 | u32 mem_addr = sg_dma_address(&data->sg[i]); |
| 472 | |
| 473 | /* |
| 474 | * Set the OWN bit and disable interrupts for this |
| 475 | * descriptor |
| 476 | */ |
| 477 | desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | |
| 478 | IDMAC_DES0_CH; |
| 479 | /* Buffer length */ |
| 480 | IDMAC_SET_BUFFER1_SIZE(desc, length); |
| 481 | |
| 482 | /* Physical address to DMA to/from */ |
| 483 | desc->des2 = mem_addr; |
| 484 | } |
| 485 | |
| 486 | /* Set first descriptor */ |
| 487 | desc = host->sg_cpu; |
| 488 | desc->des0 |= IDMAC_DES0_FD; |
| 489 | |
| 490 | /* Set last descriptor */ |
| 491 | desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc); |
| 492 | desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); |
| 493 | desc->des0 |= IDMAC_DES0_LD; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 494 | } |
| 495 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 496 | wmb(); |
| 497 | } |
| 498 | |
| 499 | static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len) |
| 500 | { |
| 501 | u32 temp; |
| 502 | |
| 503 | dw_mci_translate_sglist(host, host->data, sg_len); |
| 504 | |
Sonny Rao | 536f6b9 | 2014-10-16 09:58:05 -0700 | [diff] [blame] | 505 | /* Make sure to reset DMA in case we did PIO before this */ |
| 506 | dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET); |
| 507 | dw_mci_idmac_reset(host); |
| 508 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 509 | /* Select IDMAC interface */ |
| 510 | temp = mci_readl(host, CTRL); |
| 511 | temp |= SDMMC_CTRL_USE_IDMAC; |
| 512 | mci_writel(host, CTRL, temp); |
| 513 | |
| 514 | wmb(); |
| 515 | |
| 516 | /* Enable the IDMAC */ |
| 517 | temp = mci_readl(host, BMOD); |
Jaehoon Chung | a5289a4 | 2011-02-25 11:08:13 +0900 | [diff] [blame] | 518 | temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 519 | mci_writel(host, BMOD, temp); |
| 520 | |
| 521 | /* Start it running */ |
| 522 | mci_writel(host, PLDMND, 1); |
| 523 | } |
| 524 | |
| 525 | static int dw_mci_idmac_init(struct dw_mci *host) |
| 526 | { |
Seungwon Jeon | 897b69e | 2012-09-19 13:58:31 +0800 | [diff] [blame] | 527 | int i; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 528 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 529 | if (host->dma_64bit_address == 1) { |
| 530 | struct idmac_desc_64addr *p; |
| 531 | /* Number of descriptors in the ring buffer */ |
| 532 | host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 533 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 534 | /* Forward link the descriptor list */ |
| 535 | for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; |
| 536 | i++, p++) { |
| 537 | p->des6 = (host->sg_dma + |
| 538 | (sizeof(struct idmac_desc_64addr) * |
| 539 | (i + 1))) & 0xffffffff; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 540 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 541 | p->des7 = (u64)(host->sg_dma + |
| 542 | (sizeof(struct idmac_desc_64addr) * |
| 543 | (i + 1))) >> 32; |
| 544 | /* Initialize reserved and buffer size fields to "0" */ |
| 545 | p->des1 = 0; |
| 546 | p->des2 = 0; |
| 547 | p->des3 = 0; |
| 548 | } |
| 549 | |
| 550 | /* Set the last descriptor as the end-of-ring descriptor */ |
| 551 | p->des6 = host->sg_dma & 0xffffffff; |
| 552 | p->des7 = (u64)host->sg_dma >> 32; |
| 553 | p->des0 = IDMAC_DES0_ER; |
| 554 | |
| 555 | } else { |
| 556 | struct idmac_desc *p; |
| 557 | /* Number of descriptors in the ring buffer */ |
| 558 | host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc); |
| 559 | |
| 560 | /* Forward link the descriptor list */ |
| 561 | for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) |
| 562 | p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * |
| 563 | (i + 1)); |
| 564 | |
| 565 | /* Set the last descriptor as the end-of-ring descriptor */ |
| 566 | p->des3 = host->sg_dma; |
| 567 | p->des0 = IDMAC_DES0_ER; |
| 568 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 569 | |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 570 | dw_mci_idmac_reset(host); |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 571 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 572 | if (host->dma_64bit_address == 1) { |
| 573 | /* Mask out interrupts - get Tx & Rx complete only */ |
| 574 | mci_writel(host, IDSTS64, IDMAC_INT_CLR); |
| 575 | mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI | |
| 576 | SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 577 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 578 | /* Set the descriptor base address */ |
| 579 | mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff); |
| 580 | mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32); |
| 581 | |
| 582 | } else { |
| 583 | /* Mask out interrupts - get Tx & Rx complete only */ |
| 584 | mci_writel(host, IDSTS, IDMAC_INT_CLR); |
| 585 | mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | |
| 586 | SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); |
| 587 | |
| 588 | /* Set the descriptor base address */ |
| 589 | mci_writel(host, DBADDR, host->sg_dma); |
| 590 | } |
| 591 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
Arnd Bergmann | 8e2b36e | 2012-11-06 22:55:31 +0100 | [diff] [blame] | 595 | static const struct dw_mci_dma_ops dw_mci_idmac_ops = { |
Seungwon Jeon | 885c3e8 | 2012-02-20 11:01:43 +0900 | [diff] [blame] | 596 | .init = dw_mci_idmac_init, |
| 597 | .start = dw_mci_idmac_start_dma, |
| 598 | .stop = dw_mci_idmac_stop_dma, |
| 599 | .complete = dw_mci_idmac_complete_dma, |
| 600 | .cleanup = dw_mci_dma_cleanup, |
| 601 | }; |
| 602 | #endif /* CONFIG_MMC_DW_IDMAC */ |
| 603 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 604 | static int dw_mci_pre_dma_transfer(struct dw_mci *host, |
| 605 | struct mmc_data *data, |
| 606 | bool next) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 607 | { |
| 608 | struct scatterlist *sg; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 609 | unsigned int i, sg_len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 610 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 611 | if (!next && data->host_cookie) |
| 612 | return data->host_cookie; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * We don't do DMA on "complex" transfers, i.e. with |
| 616 | * non-word-aligned buffers or lengths. Also, we don't bother |
| 617 | * with all the DMA setup overhead for short transfers. |
| 618 | */ |
| 619 | if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD) |
| 620 | return -EINVAL; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 621 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 622 | if (data->blksz & 3) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 626 | if (sg->offset & 3 || sg->length & 3) |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 630 | sg_len = dma_map_sg(host->dev, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 631 | data->sg, |
| 632 | data->sg_len, |
| 633 | dw_mci_get_dma_dir(data)); |
| 634 | if (sg_len == 0) |
| 635 | return -EINVAL; |
| 636 | |
| 637 | if (next) |
| 638 | data->host_cookie = sg_len; |
| 639 | |
| 640 | return sg_len; |
| 641 | } |
| 642 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 643 | static void dw_mci_pre_req(struct mmc_host *mmc, |
| 644 | struct mmc_request *mrq, |
| 645 | bool is_first_req) |
| 646 | { |
| 647 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 648 | struct mmc_data *data = mrq->data; |
| 649 | |
| 650 | if (!slot->host->use_dma || !data) |
| 651 | return; |
| 652 | |
| 653 | if (data->host_cookie) { |
| 654 | data->host_cookie = 0; |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0) |
| 659 | data->host_cookie = 0; |
| 660 | } |
| 661 | |
| 662 | static void dw_mci_post_req(struct mmc_host *mmc, |
| 663 | struct mmc_request *mrq, |
| 664 | int err) |
| 665 | { |
| 666 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 667 | struct mmc_data *data = mrq->data; |
| 668 | |
| 669 | if (!slot->host->use_dma || !data) |
| 670 | return; |
| 671 | |
| 672 | if (data->host_cookie) |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 673 | dma_unmap_sg(slot->host->dev, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 674 | data->sg, |
| 675 | data->sg_len, |
| 676 | dw_mci_get_dma_dir(data)); |
| 677 | data->host_cookie = 0; |
| 678 | } |
| 679 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 680 | static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data) |
| 681 | { |
| 682 | #ifdef CONFIG_MMC_DW_IDMAC |
| 683 | unsigned int blksz = data->blksz; |
| 684 | const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256}; |
| 685 | u32 fifo_width = 1 << host->data_shift; |
| 686 | u32 blksz_depth = blksz / fifo_width, fifoth_val; |
| 687 | u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers; |
| 688 | int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1; |
| 689 | |
| 690 | tx_wmark = (host->fifo_depth) / 2; |
| 691 | tx_wmark_invers = host->fifo_depth - tx_wmark; |
| 692 | |
| 693 | /* |
| 694 | * MSIZE is '1', |
| 695 | * if blksz is not a multiple of the FIFO width |
| 696 | */ |
| 697 | if (blksz % fifo_width) { |
| 698 | msize = 0; |
| 699 | rx_wmark = 1; |
| 700 | goto done; |
| 701 | } |
| 702 | |
| 703 | do { |
| 704 | if (!((blksz_depth % mszs[idx]) || |
| 705 | (tx_wmark_invers % mszs[idx]))) { |
| 706 | msize = idx; |
| 707 | rx_wmark = mszs[idx] - 1; |
| 708 | break; |
| 709 | } |
| 710 | } while (--idx > 0); |
| 711 | /* |
| 712 | * If idx is '0', it won't be tried |
| 713 | * Thus, initial values are uesed |
| 714 | */ |
| 715 | done: |
| 716 | fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark); |
| 717 | mci_writel(host, FIFOTH, fifoth_val); |
| 718 | #endif |
| 719 | } |
| 720 | |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 721 | static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data) |
| 722 | { |
| 723 | unsigned int blksz = data->blksz; |
| 724 | u32 blksz_depth, fifo_depth; |
| 725 | u16 thld_size; |
| 726 | |
| 727 | WARN_ON(!(data->flags & MMC_DATA_READ)); |
| 728 | |
| 729 | if (host->timing != MMC_TIMING_MMC_HS200 && |
| 730 | host->timing != MMC_TIMING_UHS_SDR104) |
| 731 | goto disable; |
| 732 | |
| 733 | blksz_depth = blksz / (1 << host->data_shift); |
| 734 | fifo_depth = host->fifo_depth; |
| 735 | |
| 736 | if (blksz_depth > fifo_depth) |
| 737 | goto disable; |
| 738 | |
| 739 | /* |
| 740 | * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz' |
| 741 | * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz |
| 742 | * Currently just choose blksz. |
| 743 | */ |
| 744 | thld_size = blksz; |
| 745 | mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1)); |
| 746 | return; |
| 747 | |
| 748 | disable: |
| 749 | mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0)); |
| 750 | } |
| 751 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 752 | static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data) |
| 753 | { |
| 754 | int sg_len; |
| 755 | u32 temp; |
| 756 | |
| 757 | host->using_dma = 0; |
| 758 | |
| 759 | /* If we don't have a channel, we can't do DMA */ |
| 760 | if (!host->use_dma) |
| 761 | return -ENODEV; |
| 762 | |
| 763 | sg_len = dw_mci_pre_dma_transfer(host, data, 0); |
Seungwon Jeon | a99aa9b | 2012-04-10 09:53:32 +0900 | [diff] [blame] | 764 | if (sg_len < 0) { |
| 765 | host->dma_ops->stop(host); |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 766 | return sg_len; |
Seungwon Jeon | a99aa9b | 2012-04-10 09:53:32 +0900 | [diff] [blame] | 767 | } |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 768 | |
James Hogan | 03e8cb5 | 2011-06-29 09:28:43 +0100 | [diff] [blame] | 769 | host->using_dma = 1; |
| 770 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 771 | dev_vdbg(host->dev, |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 772 | "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n", |
| 773 | (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma, |
| 774 | sg_len); |
| 775 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 776 | /* |
| 777 | * Decide the MSIZE and RX/TX Watermark. |
| 778 | * If current block size is same with previous size, |
| 779 | * no need to update fifoth. |
| 780 | */ |
| 781 | if (host->prev_blksz != data->blksz) |
| 782 | dw_mci_adjust_fifoth(host, data); |
| 783 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 784 | /* Enable the DMA interface */ |
| 785 | temp = mci_readl(host, CTRL); |
| 786 | temp |= SDMMC_CTRL_DMA_ENABLE; |
| 787 | mci_writel(host, CTRL, temp); |
| 788 | |
| 789 | /* Disable RX/TX IRQs, let DMA handle it */ |
| 790 | temp = mci_readl(host, INTMASK); |
| 791 | temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR); |
| 792 | mci_writel(host, INTMASK, temp); |
| 793 | |
| 794 | host->dma_ops->start(host, sg_len); |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) |
| 800 | { |
| 801 | u32 temp; |
| 802 | |
| 803 | data->error = -EINPROGRESS; |
| 804 | |
| 805 | WARN_ON(host->data); |
| 806 | host->sg = NULL; |
| 807 | host->data = data; |
| 808 | |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 809 | if (data->flags & MMC_DATA_READ) { |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 810 | host->dir_status = DW_MCI_RECV_STATUS; |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 811 | dw_mci_ctrl_rd_thld(host, data); |
| 812 | } else { |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 813 | host->dir_status = DW_MCI_SEND_STATUS; |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 814 | } |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 815 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 816 | if (dw_mci_submit_data_dma(host, data)) { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 817 | int flags = SG_MITER_ATOMIC; |
| 818 | if (host->data->flags & MMC_DATA_READ) |
| 819 | flags |= SG_MITER_TO_SG; |
| 820 | else |
| 821 | flags |= SG_MITER_FROM_SG; |
| 822 | |
| 823 | sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 824 | host->sg = data->sg; |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 825 | host->part_buf_start = 0; |
| 826 | host->part_buf_count = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 827 | |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 828 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 829 | temp = mci_readl(host, INTMASK); |
| 830 | temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR; |
| 831 | mci_writel(host, INTMASK, temp); |
| 832 | |
| 833 | temp = mci_readl(host, CTRL); |
| 834 | temp &= ~SDMMC_CTRL_DMA_ENABLE; |
| 835 | mci_writel(host, CTRL, temp); |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Use the initial fifoth_val for PIO mode. |
| 839 | * If next issued data may be transfered by DMA mode, |
| 840 | * prev_blksz should be invalidated. |
| 841 | */ |
| 842 | mci_writel(host, FIFOTH, host->fifoth_val); |
| 843 | host->prev_blksz = 0; |
| 844 | } else { |
| 845 | /* |
| 846 | * Keep the current block size. |
| 847 | * It will be used to decide whether to update |
| 848 | * fifoth register next time. |
| 849 | */ |
| 850 | host->prev_blksz = data->blksz; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
| 854 | static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg) |
| 855 | { |
| 856 | struct dw_mci *host = slot->host; |
| 857 | unsigned long timeout = jiffies + msecs_to_jiffies(500); |
| 858 | unsigned int cmd_status = 0; |
| 859 | |
| 860 | mci_writel(host, CMDARG, arg); |
| 861 | wmb(); |
| 862 | mci_writel(host, CMD, SDMMC_CMD_START | cmd); |
| 863 | |
| 864 | while (time_before(jiffies, timeout)) { |
| 865 | cmd_status = mci_readl(host, CMD); |
| 866 | if (!(cmd_status & SDMMC_CMD_START)) |
| 867 | return; |
| 868 | } |
| 869 | dev_err(&slot->mmc->class_dev, |
| 870 | "Timeout sending command (cmd %#x arg %#x status %#x)\n", |
| 871 | cmd, arg, cmd_status); |
| 872 | } |
| 873 | |
Abhilash Kesavan | ab26912 | 2012-11-19 10:26:21 +0530 | [diff] [blame] | 874 | static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 875 | { |
| 876 | struct dw_mci *host = slot->host; |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 877 | unsigned int clock = slot->clock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 878 | u32 div; |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 879 | u32 clk_en_a; |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 880 | u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT; |
| 881 | |
| 882 | /* We must continue to set bit 28 in CMD until the change is complete */ |
| 883 | if (host->state == STATE_WAITING_CMD11_DONE) |
| 884 | sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 885 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 886 | if (!clock) { |
| 887 | mci_writel(host, CLKENA, 0); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 888 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 889 | } else if (clock != host->current_speed || force_clkinit) { |
| 890 | div = host->bus_hz / clock; |
| 891 | if (host->bus_hz % clock && host->bus_hz > clock) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 892 | /* |
| 893 | * move the + 1 after the divide to prevent |
| 894 | * over-clocking the card. |
| 895 | */ |
Seungwon Jeon | e419990 | 2012-05-22 13:01:21 +0900 | [diff] [blame] | 896 | div += 1; |
| 897 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 898 | div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 899 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 900 | if ((clock << div) != slot->__clk_old || force_clkinit) |
| 901 | dev_info(&slot->mmc->class_dev, |
| 902 | "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n", |
| 903 | slot->id, host->bus_hz, clock, |
| 904 | div ? ((host->bus_hz / div) >> 1) : |
| 905 | host->bus_hz, div); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 906 | |
| 907 | /* disable clock */ |
| 908 | mci_writel(host, CLKENA, 0); |
| 909 | mci_writel(host, CLKSRC, 0); |
| 910 | |
| 911 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 912 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 913 | |
| 914 | /* set clock to desired speed */ |
| 915 | mci_writel(host, CLKDIV, div); |
| 916 | |
| 917 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 918 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 919 | |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 920 | /* enable clock; only low power if no SDIO */ |
| 921 | clk_en_a = SDMMC_CLKEN_ENABLE << slot->id; |
Addy Ke | 7675623 | 2014-11-04 22:03:09 +0800 | [diff] [blame^] | 922 | if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->sdio_id))) |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 923 | clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id; |
| 924 | mci_writel(host, CLKENA, clk_en_a); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 925 | |
| 926 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 927 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 928 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 929 | /* keep the clock with reflecting clock dividor */ |
| 930 | slot->__clk_old = clock << div; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 931 | } |
| 932 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 933 | host->current_speed = clock; |
| 934 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 935 | /* Set the current slot bus width */ |
Seungwon Jeon | 1d56c45 | 2011-06-20 17:23:53 +0900 | [diff] [blame] | 936 | mci_writel(host, CTYPE, (slot->ctype << slot->id)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 939 | static void __dw_mci_start_request(struct dw_mci *host, |
| 940 | struct dw_mci_slot *slot, |
| 941 | struct mmc_command *cmd) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 942 | { |
| 943 | struct mmc_request *mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 944 | struct mmc_data *data; |
| 945 | u32 cmdflags; |
| 946 | |
| 947 | mrq = slot->mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 948 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 949 | host->cur_slot = slot; |
| 950 | host->mrq = mrq; |
| 951 | |
| 952 | host->pending_events = 0; |
| 953 | host->completed_events = 0; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 954 | host->cmd_status = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 955 | host->data_status = 0; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 956 | host->dir_status = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 957 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 958 | data = cmd->data; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 959 | if (data) { |
Jaehoon Chung | f16afa8 | 2014-03-03 11:36:45 +0900 | [diff] [blame] | 960 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 961 | mci_writel(host, BYTCNT, data->blksz*data->blocks); |
| 962 | mci_writel(host, BLKSIZ, data->blksz); |
| 963 | } |
| 964 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 965 | cmdflags = dw_mci_prepare_command(slot->mmc, cmd); |
| 966 | |
| 967 | /* this is the first command, send the initialization clock */ |
| 968 | if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags)) |
| 969 | cmdflags |= SDMMC_CMD_INIT; |
| 970 | |
| 971 | if (data) { |
| 972 | dw_mci_submit_data(host, data); |
| 973 | wmb(); |
| 974 | } |
| 975 | |
| 976 | dw_mci_start_command(host, cmd, cmdflags); |
| 977 | |
| 978 | if (mrq->stop) |
| 979 | host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 980 | else |
| 981 | host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 982 | } |
| 983 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 984 | static void dw_mci_start_request(struct dw_mci *host, |
| 985 | struct dw_mci_slot *slot) |
| 986 | { |
| 987 | struct mmc_request *mrq = slot->mrq; |
| 988 | struct mmc_command *cmd; |
| 989 | |
| 990 | cmd = mrq->sbc ? mrq->sbc : mrq->cmd; |
| 991 | __dw_mci_start_request(host, slot, cmd); |
| 992 | } |
| 993 | |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 994 | /* must be called with host->lock held */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 995 | static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot, |
| 996 | struct mmc_request *mrq) |
| 997 | { |
| 998 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", |
| 999 | host->state); |
| 1000 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1001 | slot->mrq = mrq; |
| 1002 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1003 | if (host->state == STATE_WAITING_CMD11_DONE) { |
| 1004 | dev_warn(&slot->mmc->class_dev, |
| 1005 | "Voltage change didn't complete\n"); |
| 1006 | /* |
| 1007 | * this case isn't expected to happen, so we can |
| 1008 | * either crash here or just try to continue on |
| 1009 | * in the closest possible state |
| 1010 | */ |
| 1011 | host->state = STATE_IDLE; |
| 1012 | } |
| 1013 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1014 | if (host->state == STATE_IDLE) { |
| 1015 | host->state = STATE_SENDING_CMD; |
| 1016 | dw_mci_start_request(host, slot); |
| 1017 | } else { |
| 1018 | list_add_tail(&slot->queue_node, &host->queue); |
| 1019 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1023 | { |
| 1024 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1025 | struct dw_mci *host = slot->host; |
| 1026 | |
| 1027 | WARN_ON(slot->mrq); |
| 1028 | |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * The check for card presence and queueing of the request must be |
| 1031 | * atomic, otherwise the card could be removed in between and the |
| 1032 | * request wouldn't fail until another card was inserted. |
| 1033 | */ |
| 1034 | spin_lock_bh(&host->lock); |
| 1035 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1036 | if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) { |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1037 | spin_unlock_bh(&host->lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1038 | mrq->cmd->error = -ENOMEDIUM; |
| 1039 | mmc_request_done(mmc, mrq); |
| 1040 | return; |
| 1041 | } |
| 1042 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1043 | dw_mci_queue_request(host, slot, mrq); |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1044 | |
| 1045 | spin_unlock_bh(&host->lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1049 | { |
| 1050 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 1051 | const struct dw_mci_drv_data *drv_data = slot->host->drv_data; |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1052 | u32 regs; |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1053 | int ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1054 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1055 | switch (ios->bus_width) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1056 | case MMC_BUS_WIDTH_4: |
| 1057 | slot->ctype = SDMMC_CTYPE_4BIT; |
| 1058 | break; |
Jaehoon Chung | c9b2a06 | 2011-02-17 16:12:38 +0900 | [diff] [blame] | 1059 | case MMC_BUS_WIDTH_8: |
| 1060 | slot->ctype = SDMMC_CTYPE_8BIT; |
| 1061 | break; |
Jaehoon Chung | b2f7cb4 | 2012-11-08 17:35:31 +0900 | [diff] [blame] | 1062 | default: |
| 1063 | /* set default 1 bit mode */ |
| 1064 | slot->ctype = SDMMC_CTYPE_1BIT; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1065 | } |
| 1066 | |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1067 | regs = mci_readl(slot->host, UHS_REG); |
| 1068 | |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1069 | /* DDR mode set */ |
Seungwon Jeon | cab3a80 | 2014-03-14 21:12:43 +0900 | [diff] [blame] | 1070 | if (ios->timing == MMC_TIMING_MMC_DDR52) |
Hyeonsu Kim | c69042a | 2013-02-22 09:32:46 +0900 | [diff] [blame] | 1071 | regs |= ((0x1 << slot->id) << 16); |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1072 | else |
Hyeonsu Kim | c69042a | 2013-02-22 09:32:46 +0900 | [diff] [blame] | 1073 | regs &= ~((0x1 << slot->id) << 16); |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1074 | |
| 1075 | mci_writel(slot->host, UHS_REG, regs); |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1076 | slot->host->timing = ios->timing; |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1077 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1078 | /* |
| 1079 | * Use mirror of ios->clock to prevent race with mmc |
| 1080 | * core ios update when finding the minimum. |
| 1081 | */ |
| 1082 | slot->clock = ios->clock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1083 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 1084 | if (drv_data && drv_data->set_ios) |
| 1085 | drv_data->set_ios(slot->host, ios); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 1086 | |
Jaehoon Chung | bf7cb22 | 2012-11-08 17:35:29 +0900 | [diff] [blame] | 1087 | /* Slot specific timing and width adjustment */ |
| 1088 | dw_mci_setup_bus(slot, false); |
| 1089 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1090 | if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0) |
| 1091 | slot->host->state = STATE_IDLE; |
| 1092 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1093 | switch (ios->power_mode) { |
| 1094 | case MMC_POWER_UP: |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1095 | if (!IS_ERR(mmc->supply.vmmc)) { |
| 1096 | ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, |
| 1097 | ios->vdd); |
| 1098 | if (ret) { |
| 1099 | dev_err(slot->host->dev, |
| 1100 | "failed to enable vmmc regulator\n"); |
| 1101 | /*return, if failed turn on vmmc*/ |
| 1102 | return; |
| 1103 | } |
| 1104 | } |
| 1105 | if (!IS_ERR(mmc->supply.vqmmc) && !slot->host->vqmmc_enabled) { |
| 1106 | ret = regulator_enable(mmc->supply.vqmmc); |
| 1107 | if (ret < 0) |
| 1108 | dev_err(slot->host->dev, |
| 1109 | "failed to enable vqmmc regulator\n"); |
| 1110 | else |
| 1111 | slot->host->vqmmc_enabled = true; |
| 1112 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1113 | set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags); |
Jaehoon Chung | 4366dcc | 2013-03-26 21:36:14 +0900 | [diff] [blame] | 1114 | regs = mci_readl(slot->host, PWREN); |
| 1115 | regs |= (1 << slot->id); |
| 1116 | mci_writel(slot->host, PWREN, regs); |
James Hogan | e6f34e2 | 2013-03-12 10:43:32 +0000 | [diff] [blame] | 1117 | break; |
| 1118 | case MMC_POWER_OFF: |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1119 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1120 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 1121 | |
| 1122 | if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) { |
| 1123 | regulator_disable(mmc->supply.vqmmc); |
| 1124 | slot->host->vqmmc_enabled = false; |
| 1125 | } |
| 1126 | |
Jaehoon Chung | 4366dcc | 2013-03-26 21:36:14 +0900 | [diff] [blame] | 1127 | regs = mci_readl(slot->host, PWREN); |
| 1128 | regs &= ~(1 << slot->id); |
| 1129 | mci_writel(slot->host, PWREN, regs); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1130 | break; |
| 1131 | default: |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1136 | static int dw_mci_card_busy(struct mmc_host *mmc) |
| 1137 | { |
| 1138 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1139 | u32 status; |
| 1140 | |
| 1141 | /* |
| 1142 | * Check the busy bit which is low when DAT[3:0] |
| 1143 | * (the data lines) are 0000 |
| 1144 | */ |
| 1145 | status = mci_readl(slot->host, STATUS); |
| 1146 | |
| 1147 | return !!(status & SDMMC_STATUS_BUSY); |
| 1148 | } |
| 1149 | |
| 1150 | static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1151 | { |
| 1152 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1153 | struct dw_mci *host = slot->host; |
| 1154 | u32 uhs; |
| 1155 | u32 v18 = SDMMC_UHS_18V << slot->id; |
| 1156 | int min_uv, max_uv; |
| 1157 | int ret; |
| 1158 | |
| 1159 | /* |
| 1160 | * Program the voltage. Note that some instances of dw_mmc may use |
| 1161 | * the UHS_REG for this. For other instances (like exynos) the UHS_REG |
| 1162 | * does no harm but you need to set the regulator directly. Try both. |
| 1163 | */ |
| 1164 | uhs = mci_readl(host, UHS_REG); |
| 1165 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) { |
| 1166 | min_uv = 2700000; |
| 1167 | max_uv = 3600000; |
| 1168 | uhs &= ~v18; |
| 1169 | } else { |
| 1170 | min_uv = 1700000; |
| 1171 | max_uv = 1950000; |
| 1172 | uhs |= v18; |
| 1173 | } |
| 1174 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 1175 | ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv); |
| 1176 | |
| 1177 | if (ret) { |
Doug Anderson | b19caf3 | 2014-10-10 21:16:16 -0700 | [diff] [blame] | 1178 | dev_dbg(&mmc->class_dev, |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1179 | "Regulator set error %d: %d - %d\n", |
| 1180 | ret, min_uv, max_uv); |
| 1181 | return ret; |
| 1182 | } |
| 1183 | } |
| 1184 | mci_writel(host, UHS_REG, uhs); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1189 | static int dw_mci_get_ro(struct mmc_host *mmc) |
| 1190 | { |
| 1191 | int read_only; |
| 1192 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Jaehoon Chung | 9795a84 | 2014-03-03 11:36:46 +0900 | [diff] [blame] | 1193 | int gpio_ro = mmc_gpio_get_ro(mmc); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1194 | |
| 1195 | /* Use platform get_ro function, else try on board write protect */ |
Jaehoon Chung | 26375b5 | 2014-08-07 16:37:58 +0900 | [diff] [blame] | 1196 | if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) || |
| 1197 | (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT)) |
Thomas Abraham | b4967aa | 2012-09-17 18:16:39 +0000 | [diff] [blame] | 1198 | read_only = 0; |
Jaehoon Chung | 9795a84 | 2014-03-03 11:36:46 +0900 | [diff] [blame] | 1199 | else if (!IS_ERR_VALUE(gpio_ro)) |
| 1200 | read_only = gpio_ro; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1201 | else |
| 1202 | read_only = |
| 1203 | mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0; |
| 1204 | |
| 1205 | dev_dbg(&mmc->class_dev, "card is %s\n", |
| 1206 | read_only ? "read-only" : "read-write"); |
| 1207 | |
| 1208 | return read_only; |
| 1209 | } |
| 1210 | |
| 1211 | static int dw_mci_get_cd(struct mmc_host *mmc) |
| 1212 | { |
| 1213 | int present; |
| 1214 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1215 | struct dw_mci_board *brd = slot->host->pdata; |
Zhangfei Gao | 7cf347b | 2014-01-16 20:48:47 +0800 | [diff] [blame] | 1216 | struct dw_mci *host = slot->host; |
| 1217 | int gpio_cd = mmc_gpio_get_cd(mmc); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1218 | |
| 1219 | /* Use platform get_cd function, else try onboard card detect */ |
Jaehoon Chung | fc3d772 | 2011-02-25 11:08:15 +0900 | [diff] [blame] | 1220 | if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) |
| 1221 | present = 1; |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 1222 | else if (!IS_ERR_VALUE(gpio_cd)) |
Zhangfei Gao | 7cf347b | 2014-01-16 20:48:47 +0800 | [diff] [blame] | 1223 | present = gpio_cd; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1224 | else |
| 1225 | present = (mci_readl(slot->host, CDETECT) & (1 << slot->id)) |
| 1226 | == 0 ? 1 : 0; |
| 1227 | |
Zhangfei Gao | 7cf347b | 2014-01-16 20:48:47 +0800 | [diff] [blame] | 1228 | spin_lock_bh(&host->lock); |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 1229 | if (present) { |
| 1230 | set_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1231 | dev_dbg(&mmc->class_dev, "card is present\n"); |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 1232 | } else { |
| 1233 | clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1234 | dev_dbg(&mmc->class_dev, "card is not present\n"); |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 1235 | } |
Zhangfei Gao | 7cf347b | 2014-01-16 20:48:47 +0800 | [diff] [blame] | 1236 | spin_unlock_bh(&host->lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1237 | |
| 1238 | return present; |
| 1239 | } |
| 1240 | |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * Disable lower power mode. |
| 1243 | * |
| 1244 | * Low power mode will stop the card clock when idle. According to the |
| 1245 | * description of the CLKENA register we should disable low power mode |
| 1246 | * for SDIO cards if we need SDIO interrupts to work. |
| 1247 | * |
| 1248 | * This function is fast if low power mode is already disabled. |
| 1249 | */ |
| 1250 | static void dw_mci_disable_low_power(struct dw_mci_slot *slot) |
| 1251 | { |
| 1252 | struct dw_mci *host = slot->host; |
| 1253 | u32 clk_en_a; |
| 1254 | const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id; |
| 1255 | |
| 1256 | clk_en_a = mci_readl(host, CLKENA); |
| 1257 | |
| 1258 | if (clk_en_a & clken_low_pwr) { |
| 1259 | mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr); |
| 1260 | mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | |
| 1261 | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 1262 | } |
| 1263 | } |
| 1264 | |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1265 | static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb) |
| 1266 | { |
| 1267 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1268 | struct dw_mci *host = slot->host; |
| 1269 | u32 int_mask; |
| 1270 | |
| 1271 | /* Enable/disable Slot Specific SDIO interrupt */ |
| 1272 | int_mask = mci_readl(host, INTMASK); |
| 1273 | if (enb) { |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * Turn off low power mode if it was enabled. This is a bit of |
| 1276 | * a heavy operation and we disable / enable IRQs a lot, so |
| 1277 | * we'll leave low power mode disabled and it will get |
| 1278 | * re-enabled again in dw_mci_setup_bus(). |
| 1279 | */ |
| 1280 | dw_mci_disable_low_power(slot); |
| 1281 | |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1282 | mci_writel(host, INTMASK, |
Addy Ke | 7675623 | 2014-11-04 22:03:09 +0800 | [diff] [blame^] | 1283 | (int_mask | SDMMC_INT_SDIO(slot->sdio_id))); |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1284 | } else { |
| 1285 | mci_writel(host, INTMASK, |
Addy Ke | 7675623 | 2014-11-04 22:03:09 +0800 | [diff] [blame^] | 1286 | (int_mask & ~SDMMC_INT_SDIO(slot->sdio_id))); |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1290 | static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 1291 | { |
| 1292 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1293 | struct dw_mci *host = slot->host; |
| 1294 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
| 1295 | struct dw_mci_tuning_data tuning_data; |
| 1296 | int err = -ENOSYS; |
| 1297 | |
| 1298 | if (opcode == MMC_SEND_TUNING_BLOCK_HS200) { |
| 1299 | if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) { |
| 1300 | tuning_data.blk_pattern = tuning_blk_pattern_8bit; |
| 1301 | tuning_data.blksz = sizeof(tuning_blk_pattern_8bit); |
| 1302 | } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) { |
| 1303 | tuning_data.blk_pattern = tuning_blk_pattern_4bit; |
| 1304 | tuning_data.blksz = sizeof(tuning_blk_pattern_4bit); |
| 1305 | } else { |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | } else if (opcode == MMC_SEND_TUNING_BLOCK) { |
| 1309 | tuning_data.blk_pattern = tuning_blk_pattern_4bit; |
| 1310 | tuning_data.blksz = sizeof(tuning_blk_pattern_4bit); |
| 1311 | } else { |
| 1312 | dev_err(host->dev, |
| 1313 | "Undefined command(%d) for tuning\n", opcode); |
| 1314 | return -EINVAL; |
| 1315 | } |
| 1316 | |
| 1317 | if (drv_data && drv_data->execute_tuning) |
| 1318 | err = drv_data->execute_tuning(slot, opcode, &tuning_data); |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1322 | static const struct mmc_host_ops dw_mci_ops = { |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1323 | .request = dw_mci_request, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1324 | .pre_req = dw_mci_pre_req, |
| 1325 | .post_req = dw_mci_post_req, |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1326 | .set_ios = dw_mci_set_ios, |
| 1327 | .get_ro = dw_mci_get_ro, |
| 1328 | .get_cd = dw_mci_get_cd, |
| 1329 | .enable_sdio_irq = dw_mci_enable_sdio_irq, |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1330 | .execute_tuning = dw_mci_execute_tuning, |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1331 | .card_busy = dw_mci_card_busy, |
| 1332 | .start_signal_voltage_switch = dw_mci_switch_voltage, |
| 1333 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1334 | }; |
| 1335 | |
| 1336 | static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq) |
| 1337 | __releases(&host->lock) |
| 1338 | __acquires(&host->lock) |
| 1339 | { |
| 1340 | struct dw_mci_slot *slot; |
| 1341 | struct mmc_host *prev_mmc = host->cur_slot->mmc; |
| 1342 | |
| 1343 | WARN_ON(host->cmd || host->data); |
| 1344 | |
| 1345 | host->cur_slot->mrq = NULL; |
| 1346 | host->mrq = NULL; |
| 1347 | if (!list_empty(&host->queue)) { |
| 1348 | slot = list_entry(host->queue.next, |
| 1349 | struct dw_mci_slot, queue_node); |
| 1350 | list_del(&slot->queue_node); |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 1351 | dev_vdbg(host->dev, "list not empty: %s is next\n", |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1352 | mmc_hostname(slot->mmc)); |
| 1353 | host->state = STATE_SENDING_CMD; |
| 1354 | dw_mci_start_request(host, slot); |
| 1355 | } else { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 1356 | dev_vdbg(host->dev, "list empty\n"); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1357 | |
| 1358 | if (host->state == STATE_SENDING_CMD11) |
| 1359 | host->state = STATE_WAITING_CMD11_DONE; |
| 1360 | else |
| 1361 | host->state = STATE_IDLE; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | spin_unlock(&host->lock); |
| 1365 | mmc_request_done(prev_mmc, mrq); |
| 1366 | spin_lock(&host->lock); |
| 1367 | } |
| 1368 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1369 | static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1370 | { |
| 1371 | u32 status = host->cmd_status; |
| 1372 | |
| 1373 | host->cmd_status = 0; |
| 1374 | |
| 1375 | /* Read the response from the card (up to 16 bytes) */ |
| 1376 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 1377 | if (cmd->flags & MMC_RSP_136) { |
| 1378 | cmd->resp[3] = mci_readl(host, RESP0); |
| 1379 | cmd->resp[2] = mci_readl(host, RESP1); |
| 1380 | cmd->resp[1] = mci_readl(host, RESP2); |
| 1381 | cmd->resp[0] = mci_readl(host, RESP3); |
| 1382 | } else { |
| 1383 | cmd->resp[0] = mci_readl(host, RESP0); |
| 1384 | cmd->resp[1] = 0; |
| 1385 | cmd->resp[2] = 0; |
| 1386 | cmd->resp[3] = 0; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | if (status & SDMMC_INT_RTO) |
| 1391 | cmd->error = -ETIMEDOUT; |
| 1392 | else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)) |
| 1393 | cmd->error = -EILSEQ; |
| 1394 | else if (status & SDMMC_INT_RESP_ERR) |
| 1395 | cmd->error = -EIO; |
| 1396 | else |
| 1397 | cmd->error = 0; |
| 1398 | |
| 1399 | if (cmd->error) { |
| 1400 | /* newer ip versions need a delay between retries */ |
| 1401 | if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY) |
| 1402 | mdelay(20); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1403 | } |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1404 | |
| 1405 | return cmd->error; |
| 1406 | } |
| 1407 | |
| 1408 | static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data) |
| 1409 | { |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 1410 | u32 status = host->data_status; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1411 | |
| 1412 | if (status & DW_MCI_DATA_ERROR_FLAGS) { |
| 1413 | if (status & SDMMC_INT_DRTO) { |
| 1414 | data->error = -ETIMEDOUT; |
| 1415 | } else if (status & SDMMC_INT_DCRC) { |
| 1416 | data->error = -EILSEQ; |
| 1417 | } else if (status & SDMMC_INT_EBE) { |
| 1418 | if (host->dir_status == |
| 1419 | DW_MCI_SEND_STATUS) { |
| 1420 | /* |
| 1421 | * No data CRC status was returned. |
| 1422 | * The number of bytes transferred |
| 1423 | * will be exaggerated in PIO mode. |
| 1424 | */ |
| 1425 | data->bytes_xfered = 0; |
| 1426 | data->error = -ETIMEDOUT; |
| 1427 | } else if (host->dir_status == |
| 1428 | DW_MCI_RECV_STATUS) { |
| 1429 | data->error = -EIO; |
| 1430 | } |
| 1431 | } else { |
| 1432 | /* SDMMC_INT_SBE is included */ |
| 1433 | data->error = -EIO; |
| 1434 | } |
| 1435 | |
Doug Anderson | e6cc012 | 2014-04-22 16:51:21 -0700 | [diff] [blame] | 1436 | dev_dbg(host->dev, "data error, status 0x%08x\n", status); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1437 | |
| 1438 | /* |
| 1439 | * After an error, there may be data lingering |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 1440 | * in the FIFO |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1441 | */ |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 1442 | dw_mci_reset(host); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1443 | } else { |
| 1444 | data->bytes_xfered = data->blocks * data->blksz; |
| 1445 | data->error = 0; |
| 1446 | } |
| 1447 | |
| 1448 | return data->error; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | static void dw_mci_tasklet_func(unsigned long priv) |
| 1452 | { |
| 1453 | struct dw_mci *host = (struct dw_mci *)priv; |
| 1454 | struct mmc_data *data; |
| 1455 | struct mmc_command *cmd; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1456 | struct mmc_request *mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1457 | enum dw_mci_state state; |
| 1458 | enum dw_mci_state prev_state; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1459 | unsigned int err; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1460 | |
| 1461 | spin_lock(&host->lock); |
| 1462 | |
| 1463 | state = host->state; |
| 1464 | data = host->data; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1465 | mrq = host->mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1466 | |
| 1467 | do { |
| 1468 | prev_state = state; |
| 1469 | |
| 1470 | switch (state) { |
| 1471 | case STATE_IDLE: |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1472 | case STATE_WAITING_CMD11_DONE: |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1473 | break; |
| 1474 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1475 | case STATE_SENDING_CMD11: |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1476 | case STATE_SENDING_CMD: |
| 1477 | if (!test_and_clear_bit(EVENT_CMD_COMPLETE, |
| 1478 | &host->pending_events)) |
| 1479 | break; |
| 1480 | |
| 1481 | cmd = host->cmd; |
| 1482 | host->cmd = NULL; |
| 1483 | set_bit(EVENT_CMD_COMPLETE, &host->completed_events); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1484 | err = dw_mci_command_complete(host, cmd); |
| 1485 | if (cmd == mrq->sbc && !err) { |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 1486 | prev_state = state = STATE_SENDING_CMD; |
| 1487 | __dw_mci_start_request(host, host->cur_slot, |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1488 | mrq->cmd); |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 1489 | goto unlock; |
| 1490 | } |
| 1491 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1492 | if (cmd->data && err) { |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 1493 | dw_mci_stop_dma(host); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1494 | send_stop_abort(host, data); |
| 1495 | state = STATE_SENDING_STOP; |
| 1496 | break; |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 1497 | } |
| 1498 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1499 | if (!cmd->data || err) { |
| 1500 | dw_mci_request_end(host, mrq); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1501 | goto unlock; |
| 1502 | } |
| 1503 | |
| 1504 | prev_state = state = STATE_SENDING_DATA; |
| 1505 | /* fall through */ |
| 1506 | |
| 1507 | case STATE_SENDING_DATA: |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * We could get a data error and never a transfer |
| 1510 | * complete so we'd better check for it here. |
| 1511 | * |
| 1512 | * Note that we don't really care if we also got a |
| 1513 | * transfer complete; stopping the DMA and sending an |
| 1514 | * abort won't hurt. |
| 1515 | */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1516 | if (test_and_clear_bit(EVENT_DATA_ERROR, |
| 1517 | &host->pending_events)) { |
| 1518 | dw_mci_stop_dma(host); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1519 | send_stop_abort(host, data); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1520 | state = STATE_DATA_ERROR; |
| 1521 | break; |
| 1522 | } |
| 1523 | |
| 1524 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
| 1525 | &host->pending_events)) |
| 1526 | break; |
| 1527 | |
| 1528 | set_bit(EVENT_XFER_COMPLETE, &host->completed_events); |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 1529 | |
| 1530 | /* |
| 1531 | * Handle an EVENT_DATA_ERROR that might have shown up |
| 1532 | * before the transfer completed. This might not have |
| 1533 | * been caught by the check above because the interrupt |
| 1534 | * could have gone off between the previous check and |
| 1535 | * the check for transfer complete. |
| 1536 | * |
| 1537 | * Technically this ought not be needed assuming we |
| 1538 | * get a DATA_COMPLETE eventually (we'll notice the |
| 1539 | * error and end the request), but it shouldn't hurt. |
| 1540 | * |
| 1541 | * This has the advantage of sending the stop command. |
| 1542 | */ |
| 1543 | if (test_and_clear_bit(EVENT_DATA_ERROR, |
| 1544 | &host->pending_events)) { |
| 1545 | dw_mci_stop_dma(host); |
| 1546 | send_stop_abort(host, data); |
| 1547 | state = STATE_DATA_ERROR; |
| 1548 | break; |
| 1549 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1550 | prev_state = state = STATE_DATA_BUSY; |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 1551 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1552 | /* fall through */ |
| 1553 | |
| 1554 | case STATE_DATA_BUSY: |
| 1555 | if (!test_and_clear_bit(EVENT_DATA_COMPLETE, |
| 1556 | &host->pending_events)) |
| 1557 | break; |
| 1558 | |
| 1559 | host->data = NULL; |
| 1560 | set_bit(EVENT_DATA_COMPLETE, &host->completed_events); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1561 | err = dw_mci_data_complete(host, data); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1562 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1563 | if (!err) { |
| 1564 | if (!data->stop || mrq->sbc) { |
Sachin Kamat | 17c8bc8 | 2014-02-25 15:18:28 +0530 | [diff] [blame] | 1565 | if (mrq->sbc && data->stop) |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1566 | data->stop->error = 0; |
| 1567 | dw_mci_request_end(host, mrq); |
| 1568 | goto unlock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1569 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1570 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1571 | /* stop command for open-ended transfer*/ |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1572 | if (data->stop) |
| 1573 | send_stop_abort(host, data); |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 1574 | } else { |
| 1575 | /* |
| 1576 | * If we don't have a command complete now we'll |
| 1577 | * never get one since we just reset everything; |
| 1578 | * better end the request. |
| 1579 | * |
| 1580 | * If we do have a command complete we'll fall |
| 1581 | * through to the SENDING_STOP command and |
| 1582 | * everything will be peachy keen. |
| 1583 | */ |
| 1584 | if (!test_bit(EVENT_CMD_COMPLETE, |
| 1585 | &host->pending_events)) { |
| 1586 | host->cmd = NULL; |
| 1587 | dw_mci_request_end(host, mrq); |
| 1588 | goto unlock; |
| 1589 | } |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1590 | } |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * If err has non-zero, |
| 1594 | * stop-abort command has been already issued. |
| 1595 | */ |
| 1596 | prev_state = state = STATE_SENDING_STOP; |
| 1597 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1598 | /* fall through */ |
| 1599 | |
| 1600 | case STATE_SENDING_STOP: |
| 1601 | if (!test_and_clear_bit(EVENT_CMD_COMPLETE, |
| 1602 | &host->pending_events)) |
| 1603 | break; |
| 1604 | |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 1605 | /* CMD error in data command */ |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 1606 | if (mrq->cmd->error && mrq->data) |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 1607 | dw_mci_reset(host); |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 1608 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1609 | host->cmd = NULL; |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 1610 | host->data = NULL; |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1611 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1612 | if (mrq->stop) |
| 1613 | dw_mci_command_complete(host, mrq->stop); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 1614 | else |
| 1615 | host->cmd_status = 0; |
| 1616 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1617 | dw_mci_request_end(host, mrq); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1618 | goto unlock; |
| 1619 | |
| 1620 | case STATE_DATA_ERROR: |
| 1621 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
| 1622 | &host->pending_events)) |
| 1623 | break; |
| 1624 | |
| 1625 | state = STATE_DATA_BUSY; |
| 1626 | break; |
| 1627 | } |
| 1628 | } while (state != prev_state); |
| 1629 | |
| 1630 | host->state = state; |
| 1631 | unlock: |
| 1632 | spin_unlock(&host->lock); |
| 1633 | |
| 1634 | } |
| 1635 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1636 | /* push final bytes to part_buf, only use during push */ |
| 1637 | static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 1638 | { |
| 1639 | memcpy((void *)&host->part_buf, buf, cnt); |
| 1640 | host->part_buf_count = cnt; |
| 1641 | } |
| 1642 | |
| 1643 | /* append bytes to part_buf, only use during push */ |
| 1644 | static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 1645 | { |
| 1646 | cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count); |
| 1647 | memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt); |
| 1648 | host->part_buf_count += cnt; |
| 1649 | return cnt; |
| 1650 | } |
| 1651 | |
| 1652 | /* pull first bytes from part_buf, only use during pull */ |
| 1653 | static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 1654 | { |
| 1655 | cnt = min(cnt, (int)host->part_buf_count); |
| 1656 | if (cnt) { |
| 1657 | memcpy(buf, (void *)&host->part_buf + host->part_buf_start, |
| 1658 | cnt); |
| 1659 | host->part_buf_count -= cnt; |
| 1660 | host->part_buf_start += cnt; |
| 1661 | } |
| 1662 | return cnt; |
| 1663 | } |
| 1664 | |
| 1665 | /* pull final bytes from the part_buf, assuming it's just been filled */ |
| 1666 | static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt) |
| 1667 | { |
| 1668 | memcpy(buf, &host->part_buf, cnt); |
| 1669 | host->part_buf_start = cnt; |
| 1670 | host->part_buf_count = (1 << host->data_shift) - cnt; |
| 1671 | } |
| 1672 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1673 | static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt) |
| 1674 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1675 | struct mmc_data *data = host->data; |
| 1676 | int init_cnt = cnt; |
| 1677 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1678 | /* try and push anything in the part_buf */ |
| 1679 | if (unlikely(host->part_buf_count)) { |
| 1680 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
| 1681 | buf += len; |
| 1682 | cnt -= len; |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1683 | if (host->part_buf_count == 2) { |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1684 | mci_writew(host, DATA(host->data_offset), |
| 1685 | host->part_buf16); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1686 | host->part_buf_count = 0; |
| 1687 | } |
| 1688 | } |
| 1689 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1690 | if (unlikely((unsigned long)buf & 0x1)) { |
| 1691 | while (cnt >= 2) { |
| 1692 | u16 aligned_buf[64]; |
| 1693 | int len = min(cnt & -2, (int)sizeof(aligned_buf)); |
| 1694 | int items = len >> 1; |
| 1695 | int i; |
| 1696 | /* memcpy from input buffer into aligned buffer */ |
| 1697 | memcpy(aligned_buf, buf, len); |
| 1698 | buf += len; |
| 1699 | cnt -= len; |
| 1700 | /* push data from aligned buffer into fifo */ |
| 1701 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1702 | mci_writew(host, DATA(host->data_offset), |
| 1703 | aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1704 | } |
| 1705 | } else |
| 1706 | #endif |
| 1707 | { |
| 1708 | u16 *pdata = buf; |
| 1709 | for (; cnt >= 2; cnt -= 2) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1710 | mci_writew(host, DATA(host->data_offset), *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1711 | buf = pdata; |
| 1712 | } |
| 1713 | /* put anything remaining in the part_buf */ |
| 1714 | if (cnt) { |
| 1715 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1716 | /* Push data if we have reached the expected data length */ |
| 1717 | if ((data->bytes_xfered + init_cnt) == |
| 1718 | (data->blksz * data->blocks)) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1719 | mci_writew(host, DATA(host->data_offset), |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1720 | host->part_buf16); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt) |
| 1725 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1726 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1727 | if (unlikely((unsigned long)buf & 0x1)) { |
| 1728 | while (cnt >= 2) { |
| 1729 | /* pull data from fifo into aligned buffer */ |
| 1730 | u16 aligned_buf[64]; |
| 1731 | int len = min(cnt & -2, (int)sizeof(aligned_buf)); |
| 1732 | int items = len >> 1; |
| 1733 | int i; |
| 1734 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1735 | aligned_buf[i] = mci_readw(host, |
| 1736 | DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1737 | /* memcpy from aligned buffer into output buffer */ |
| 1738 | memcpy(buf, aligned_buf, len); |
| 1739 | buf += len; |
| 1740 | cnt -= len; |
| 1741 | } |
| 1742 | } else |
| 1743 | #endif |
| 1744 | { |
| 1745 | u16 *pdata = buf; |
| 1746 | for (; cnt >= 2; cnt -= 2) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1747 | *pdata++ = mci_readw(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1748 | buf = pdata; |
| 1749 | } |
| 1750 | if (cnt) { |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1751 | host->part_buf16 = mci_readw(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1752 | dw_mci_pull_final_bytes(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt) |
| 1757 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1758 | struct mmc_data *data = host->data; |
| 1759 | int init_cnt = cnt; |
| 1760 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1761 | /* try and push anything in the part_buf */ |
| 1762 | if (unlikely(host->part_buf_count)) { |
| 1763 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
| 1764 | buf += len; |
| 1765 | cnt -= len; |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1766 | if (host->part_buf_count == 4) { |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1767 | mci_writel(host, DATA(host->data_offset), |
| 1768 | host->part_buf32); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1769 | host->part_buf_count = 0; |
| 1770 | } |
| 1771 | } |
| 1772 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1773 | if (unlikely((unsigned long)buf & 0x3)) { |
| 1774 | while (cnt >= 4) { |
| 1775 | u32 aligned_buf[32]; |
| 1776 | int len = min(cnt & -4, (int)sizeof(aligned_buf)); |
| 1777 | int items = len >> 2; |
| 1778 | int i; |
| 1779 | /* memcpy from input buffer into aligned buffer */ |
| 1780 | memcpy(aligned_buf, buf, len); |
| 1781 | buf += len; |
| 1782 | cnt -= len; |
| 1783 | /* push data from aligned buffer into fifo */ |
| 1784 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1785 | mci_writel(host, DATA(host->data_offset), |
| 1786 | aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1787 | } |
| 1788 | } else |
| 1789 | #endif |
| 1790 | { |
| 1791 | u32 *pdata = buf; |
| 1792 | for (; cnt >= 4; cnt -= 4) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1793 | mci_writel(host, DATA(host->data_offset), *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1794 | buf = pdata; |
| 1795 | } |
| 1796 | /* put anything remaining in the part_buf */ |
| 1797 | if (cnt) { |
| 1798 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1799 | /* Push data if we have reached the expected data length */ |
| 1800 | if ((data->bytes_xfered + init_cnt) == |
| 1801 | (data->blksz * data->blocks)) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1802 | mci_writel(host, DATA(host->data_offset), |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1803 | host->part_buf32); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt) |
| 1808 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1809 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1810 | if (unlikely((unsigned long)buf & 0x3)) { |
| 1811 | while (cnt >= 4) { |
| 1812 | /* pull data from fifo into aligned buffer */ |
| 1813 | u32 aligned_buf[32]; |
| 1814 | int len = min(cnt & -4, (int)sizeof(aligned_buf)); |
| 1815 | int items = len >> 2; |
| 1816 | int i; |
| 1817 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1818 | aligned_buf[i] = mci_readl(host, |
| 1819 | DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1820 | /* memcpy from aligned buffer into output buffer */ |
| 1821 | memcpy(buf, aligned_buf, len); |
| 1822 | buf += len; |
| 1823 | cnt -= len; |
| 1824 | } |
| 1825 | } else |
| 1826 | #endif |
| 1827 | { |
| 1828 | u32 *pdata = buf; |
| 1829 | for (; cnt >= 4; cnt -= 4) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1830 | *pdata++ = mci_readl(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1831 | buf = pdata; |
| 1832 | } |
| 1833 | if (cnt) { |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1834 | host->part_buf32 = mci_readl(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1835 | dw_mci_pull_final_bytes(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) |
| 1840 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1841 | struct mmc_data *data = host->data; |
| 1842 | int init_cnt = cnt; |
| 1843 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1844 | /* try and push anything in the part_buf */ |
| 1845 | if (unlikely(host->part_buf_count)) { |
| 1846 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
| 1847 | buf += len; |
| 1848 | cnt -= len; |
Seungwon Jeon | c09fbd7 | 2013-03-25 16:28:22 +0900 | [diff] [blame] | 1849 | |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1850 | if (host->part_buf_count == 8) { |
Seungwon Jeon | c09fbd7 | 2013-03-25 16:28:22 +0900 | [diff] [blame] | 1851 | mci_writeq(host, DATA(host->data_offset), |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1852 | host->part_buf); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1853 | host->part_buf_count = 0; |
| 1854 | } |
| 1855 | } |
| 1856 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1857 | if (unlikely((unsigned long)buf & 0x7)) { |
| 1858 | while (cnt >= 8) { |
| 1859 | u64 aligned_buf[16]; |
| 1860 | int len = min(cnt & -8, (int)sizeof(aligned_buf)); |
| 1861 | int items = len >> 3; |
| 1862 | int i; |
| 1863 | /* memcpy from input buffer into aligned buffer */ |
| 1864 | memcpy(aligned_buf, buf, len); |
| 1865 | buf += len; |
| 1866 | cnt -= len; |
| 1867 | /* push data from aligned buffer into fifo */ |
| 1868 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1869 | mci_writeq(host, DATA(host->data_offset), |
| 1870 | aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1871 | } |
| 1872 | } else |
| 1873 | #endif |
| 1874 | { |
| 1875 | u64 *pdata = buf; |
| 1876 | for (; cnt >= 8; cnt -= 8) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1877 | mci_writeq(host, DATA(host->data_offset), *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1878 | buf = pdata; |
| 1879 | } |
| 1880 | /* put anything remaining in the part_buf */ |
| 1881 | if (cnt) { |
| 1882 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1883 | /* Push data if we have reached the expected data length */ |
| 1884 | if ((data->bytes_xfered + init_cnt) == |
| 1885 | (data->blksz * data->blocks)) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1886 | mci_writeq(host, DATA(host->data_offset), |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 1887 | host->part_buf); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt) |
| 1892 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1893 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 1894 | if (unlikely((unsigned long)buf & 0x7)) { |
| 1895 | while (cnt >= 8) { |
| 1896 | /* pull data from fifo into aligned buffer */ |
| 1897 | u64 aligned_buf[16]; |
| 1898 | int len = min(cnt & -8, (int)sizeof(aligned_buf)); |
| 1899 | int items = len >> 3; |
| 1900 | int i; |
| 1901 | for (i = 0; i < items; ++i) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1902 | aligned_buf[i] = mci_readq(host, |
| 1903 | DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1904 | /* memcpy from aligned buffer into output buffer */ |
| 1905 | memcpy(buf, aligned_buf, len); |
| 1906 | buf += len; |
| 1907 | cnt -= len; |
| 1908 | } |
| 1909 | } else |
| 1910 | #endif |
| 1911 | { |
| 1912 | u64 *pdata = buf; |
| 1913 | for (; cnt >= 8; cnt -= 8) |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1914 | *pdata++ = mci_readq(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1915 | buf = pdata; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1916 | } |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1917 | if (cnt) { |
Jaehoon Chung | 4e0a5ad | 2011-10-17 19:36:23 +0900 | [diff] [blame] | 1918 | host->part_buf = mci_readq(host, DATA(host->data_offset)); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1919 | dw_mci_pull_final_bytes(host, buf, cnt); |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt) |
| 1924 | { |
| 1925 | int len; |
| 1926 | |
| 1927 | /* get remaining partial bytes */ |
| 1928 | len = dw_mci_pull_part_bytes(host, buf, cnt); |
| 1929 | if (unlikely(len == cnt)) |
| 1930 | return; |
| 1931 | buf += len; |
| 1932 | cnt -= len; |
| 1933 | |
| 1934 | /* get the rest of the data */ |
| 1935 | host->pull_data(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1936 | } |
| 1937 | |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 1938 | static void dw_mci_read_data_pio(struct dw_mci *host, bool dto) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1939 | { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1940 | struct sg_mapping_iter *sg_miter = &host->sg_miter; |
| 1941 | void *buf; |
| 1942 | unsigned int offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1943 | struct mmc_data *data = host->data; |
| 1944 | int shift = host->data_shift; |
| 1945 | u32 status; |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 1946 | unsigned int len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1947 | unsigned int remain, fcnt; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1948 | |
| 1949 | do { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1950 | if (!sg_miter_next(sg_miter)) |
| 1951 | goto done; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1952 | |
Imre Deak | 4225fc8 | 2013-02-27 17:02:57 -0800 | [diff] [blame] | 1953 | host->sg = sg_miter->piter.sg; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1954 | buf = sg_miter->addr; |
| 1955 | remain = sg_miter->length; |
| 1956 | offset = 0; |
| 1957 | |
| 1958 | do { |
| 1959 | fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS)) |
| 1960 | << shift) + host->part_buf_count; |
| 1961 | len = min(remain, fcnt); |
| 1962 | if (!len) |
| 1963 | break; |
| 1964 | dw_mci_pull_data(host, (void *)(buf + offset), len); |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 1965 | data->bytes_xfered += len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1966 | offset += len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1967 | remain -= len; |
| 1968 | } while (remain); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1969 | |
Seungwon Jeon | e74f3a9 | 2012-08-01 09:30:46 +0900 | [diff] [blame] | 1970 | sg_miter->consumed = offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1971 | status = mci_readl(host, MINTSTS); |
| 1972 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 1973 | /* if the RXDR is ready read again */ |
| 1974 | } while ((status & SDMMC_INT_RXDR) || |
| 1975 | (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS)))); |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1976 | |
| 1977 | if (!remain) { |
| 1978 | if (!sg_miter_next(sg_miter)) |
| 1979 | goto done; |
| 1980 | sg_miter->consumed = 0; |
| 1981 | } |
| 1982 | sg_miter_stop(sg_miter); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1983 | return; |
| 1984 | |
| 1985 | done: |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1986 | sg_miter_stop(sg_miter); |
| 1987 | host->sg = NULL; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1988 | smp_wmb(); |
| 1989 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 1990 | } |
| 1991 | |
| 1992 | static void dw_mci_write_data_pio(struct dw_mci *host) |
| 1993 | { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1994 | struct sg_mapping_iter *sg_miter = &host->sg_miter; |
| 1995 | void *buf; |
| 1996 | unsigned int offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1997 | struct mmc_data *data = host->data; |
| 1998 | int shift = host->data_shift; |
| 1999 | u32 status; |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2000 | unsigned int len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2001 | unsigned int fifo_depth = host->fifo_depth; |
| 2002 | unsigned int remain, fcnt; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2003 | |
| 2004 | do { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2005 | if (!sg_miter_next(sg_miter)) |
| 2006 | goto done; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2007 | |
Imre Deak | 4225fc8 | 2013-02-27 17:02:57 -0800 | [diff] [blame] | 2008 | host->sg = sg_miter->piter.sg; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2009 | buf = sg_miter->addr; |
| 2010 | remain = sg_miter->length; |
| 2011 | offset = 0; |
| 2012 | |
| 2013 | do { |
| 2014 | fcnt = ((fifo_depth - |
| 2015 | SDMMC_GET_FCNT(mci_readl(host, STATUS))) |
| 2016 | << shift) - host->part_buf_count; |
| 2017 | len = min(remain, fcnt); |
| 2018 | if (!len) |
| 2019 | break; |
| 2020 | host->push_data(host, (void *)(buf + offset), len); |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2021 | data->bytes_xfered += len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2022 | offset += len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2023 | remain -= len; |
| 2024 | } while (remain); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2025 | |
Seungwon Jeon | e74f3a9 | 2012-08-01 09:30:46 +0900 | [diff] [blame] | 2026 | sg_miter->consumed = offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2027 | status = mci_readl(host, MINTSTS); |
| 2028 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2029 | } while (status & SDMMC_INT_TXDR); /* if TXDR write again */ |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2030 | |
| 2031 | if (!remain) { |
| 2032 | if (!sg_miter_next(sg_miter)) |
| 2033 | goto done; |
| 2034 | sg_miter->consumed = 0; |
| 2035 | } |
| 2036 | sg_miter_stop(sg_miter); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2037 | return; |
| 2038 | |
| 2039 | done: |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2040 | sg_miter_stop(sg_miter); |
| 2041 | host->sg = NULL; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2042 | smp_wmb(); |
| 2043 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 2044 | } |
| 2045 | |
| 2046 | static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) |
| 2047 | { |
| 2048 | if (!host->cmd_status) |
| 2049 | host->cmd_status = status; |
| 2050 | |
| 2051 | smp_wmb(); |
| 2052 | |
| 2053 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 2054 | tasklet_schedule(&host->tasklet); |
| 2055 | } |
| 2056 | |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2057 | static void dw_mci_handle_cd(struct dw_mci *host) |
| 2058 | { |
| 2059 | int i; |
| 2060 | |
| 2061 | for (i = 0; i < host->num_slots; i++) { |
| 2062 | struct dw_mci_slot *slot = host->slot[i]; |
| 2063 | |
| 2064 | if (!slot) |
| 2065 | continue; |
| 2066 | |
| 2067 | if (slot->mmc->ops->card_event) |
| 2068 | slot->mmc->ops->card_event(slot->mmc); |
| 2069 | mmc_detect_change(slot->mmc, |
| 2070 | msecs_to_jiffies(host->pdata->detect_delay_ms)); |
| 2071 | } |
| 2072 | } |
| 2073 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2074 | static irqreturn_t dw_mci_interrupt(int irq, void *dev_id) |
| 2075 | { |
| 2076 | struct dw_mci *host = dev_id; |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2077 | u32 pending; |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 2078 | int i; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2079 | |
Markos Chandras | 1fb5f68 | 2013-03-12 10:53:11 +0000 | [diff] [blame] | 2080 | pending = mci_readl(host, MINTSTS); /* read-only mask reg */ |
| 2081 | |
Doug Anderson | 476d79f | 2013-07-09 13:04:40 -0700 | [diff] [blame] | 2082 | /* |
| 2083 | * DTO fix - version 2.10a and below, and only if internal DMA |
| 2084 | * is configured. |
| 2085 | */ |
| 2086 | if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) { |
| 2087 | if (!pending && |
| 2088 | ((mci_readl(host, STATUS) >> 17) & 0x1fff)) |
| 2089 | pending |= SDMMC_INT_DATA_OVER; |
| 2090 | } |
| 2091 | |
Markos Chandras | 1fb5f68 | 2013-03-12 10:53:11 +0000 | [diff] [blame] | 2092 | if (pending) { |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2093 | /* Check volt switch first, since it can look like an error */ |
| 2094 | if ((host->state == STATE_SENDING_CMD11) && |
| 2095 | (pending & SDMMC_INT_VOLT_SWITCH)) { |
| 2096 | mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH); |
| 2097 | pending &= ~SDMMC_INT_VOLT_SWITCH; |
| 2098 | dw_mci_cmd_interrupt(host, pending); |
| 2099 | } |
| 2100 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2101 | if (pending & DW_MCI_CMD_ERROR_FLAGS) { |
| 2102 | mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2103 | host->cmd_status = pending; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2104 | smp_wmb(); |
| 2105 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | if (pending & DW_MCI_DATA_ERROR_FLAGS) { |
| 2109 | /* if there is an error report DATA_ERROR */ |
| 2110 | mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2111 | host->data_status = pending; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2112 | smp_wmb(); |
| 2113 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
Seungwon Jeon | 9b2026a | 2012-08-01 09:30:40 +0900 | [diff] [blame] | 2114 | tasklet_schedule(&host->tasklet); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | if (pending & SDMMC_INT_DATA_OVER) { |
| 2118 | mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER); |
| 2119 | if (!host->data_status) |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2120 | host->data_status = pending; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2121 | smp_wmb(); |
| 2122 | if (host->dir_status == DW_MCI_RECV_STATUS) { |
| 2123 | if (host->sg != NULL) |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2124 | dw_mci_read_data_pio(host, true); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2125 | } |
| 2126 | set_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 2127 | tasklet_schedule(&host->tasklet); |
| 2128 | } |
| 2129 | |
| 2130 | if (pending & SDMMC_INT_RXDR) { |
| 2131 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 2132 | if (host->dir_status == DW_MCI_RECV_STATUS && host->sg) |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2133 | dw_mci_read_data_pio(host, false); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | if (pending & SDMMC_INT_TXDR) { |
| 2137 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 2138 | if (host->dir_status == DW_MCI_SEND_STATUS && host->sg) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2139 | dw_mci_write_data_pio(host); |
| 2140 | } |
| 2141 | |
| 2142 | if (pending & SDMMC_INT_CMD_DONE) { |
| 2143 | mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2144 | dw_mci_cmd_interrupt(host, pending); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | if (pending & SDMMC_INT_CD) { |
| 2148 | mci_writel(host, RINTSTS, SDMMC_INT_CD); |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2149 | dw_mci_handle_cd(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2150 | } |
| 2151 | |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 2152 | /* Handle SDIO Interrupts */ |
| 2153 | for (i = 0; i < host->num_slots; i++) { |
| 2154 | struct dw_mci_slot *slot = host->slot[i]; |
Addy Ke | 7675623 | 2014-11-04 22:03:09 +0800 | [diff] [blame^] | 2155 | if (pending & SDMMC_INT_SDIO(slot->sdio_id)) { |
| 2156 | mci_writel(host, RINTSTS, |
| 2157 | SDMMC_INT_SDIO(slot->sdio_id)); |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 2158 | mmc_signal_sdio_irq(slot->mmc); |
| 2159 | } |
| 2160 | } |
| 2161 | |
Markos Chandras | 1fb5f68 | 2013-03-12 10:53:11 +0000 | [diff] [blame] | 2162 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2163 | |
| 2164 | #ifdef CONFIG_MMC_DW_IDMAC |
| 2165 | /* Handle DMA interrupts */ |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2166 | if (host->dma_64bit_address == 1) { |
| 2167 | pending = mci_readl(host, IDSTS64); |
| 2168 | if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { |
| 2169 | mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI | |
| 2170 | SDMMC_IDMAC_INT_RI); |
| 2171 | mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI); |
| 2172 | host->dma_ops->complete(host); |
| 2173 | } |
| 2174 | } else { |
| 2175 | pending = mci_readl(host, IDSTS); |
| 2176 | if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { |
| 2177 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | |
| 2178 | SDMMC_IDMAC_INT_RI); |
| 2179 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI); |
| 2180 | host->dma_ops->complete(host); |
| 2181 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2182 | } |
| 2183 | #endif |
| 2184 | |
| 2185 | return IRQ_HANDLED; |
| 2186 | } |
| 2187 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2188 | #ifdef CONFIG_OF |
| 2189 | /* given a slot id, find out the device node representing that slot */ |
| 2190 | static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot) |
| 2191 | { |
| 2192 | struct device_node *np; |
| 2193 | const __be32 *addr; |
| 2194 | int len; |
| 2195 | |
| 2196 | if (!dev || !dev->of_node) |
| 2197 | return NULL; |
| 2198 | |
| 2199 | for_each_child_of_node(dev->of_node, np) { |
| 2200 | addr = of_get_property(np, "reg", &len); |
| 2201 | if (!addr || (len < sizeof(int))) |
| 2202 | continue; |
| 2203 | if (be32_to_cpup(addr) == slot) |
| 2204 | return np; |
| 2205 | } |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | |
Doug Anderson | a70aaa6 | 2013-01-11 17:03:50 +0000 | [diff] [blame] | 2209 | static struct dw_mci_of_slot_quirks { |
| 2210 | char *quirk; |
| 2211 | int id; |
| 2212 | } of_slot_quirks[] = { |
| 2213 | { |
| 2214 | .quirk = "disable-wp", |
| 2215 | .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT, |
| 2216 | }, |
| 2217 | }; |
| 2218 | |
| 2219 | static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot) |
| 2220 | { |
| 2221 | struct device_node *np = dw_mci_of_find_slot_node(dev, slot); |
| 2222 | int quirks = 0; |
| 2223 | int idx; |
| 2224 | |
| 2225 | /* get quirks */ |
| 2226 | for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++) |
Jaehoon Chung | 26375b5 | 2014-08-07 16:37:58 +0900 | [diff] [blame] | 2227 | if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) { |
| 2228 | dev_warn(dev, "Slot quirk %s is deprecated\n", |
| 2229 | of_slot_quirks[idx].quirk); |
Doug Anderson | a70aaa6 | 2013-01-11 17:03:50 +0000 | [diff] [blame] | 2230 | quirks |= of_slot_quirks[idx].id; |
Jaehoon Chung | 26375b5 | 2014-08-07 16:37:58 +0900 | [diff] [blame] | 2231 | } |
Doug Anderson | a70aaa6 | 2013-01-11 17:03:50 +0000 | [diff] [blame] | 2232 | |
| 2233 | return quirks; |
| 2234 | } |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2235 | #else /* CONFIG_OF */ |
Doug Anderson | a70aaa6 | 2013-01-11 17:03:50 +0000 | [diff] [blame] | 2236 | static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot) |
| 2237 | { |
| 2238 | return 0; |
| 2239 | } |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2240 | #endif /* CONFIG_OF */ |
| 2241 | |
Jaehoon Chung | 36c179a | 2012-08-23 20:31:48 +0900 | [diff] [blame] | 2242 | static int dw_mci_init_slot(struct dw_mci *host, unsigned int id) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2243 | { |
| 2244 | struct mmc_host *mmc; |
| 2245 | struct dw_mci_slot *slot; |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 2246 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2247 | int ctrl_id, ret; |
Seungwon Jeon | 1f44a2a | 2013-08-31 00:13:31 +0900 | [diff] [blame] | 2248 | u32 freq[2]; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2249 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2250 | mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2251 | if (!mmc) |
| 2252 | return -ENOMEM; |
| 2253 | |
| 2254 | slot = mmc_priv(mmc); |
| 2255 | slot->id = id; |
Addy Ke | 7675623 | 2014-11-04 22:03:09 +0800 | [diff] [blame^] | 2256 | slot->sdio_id = host->sdio_id0 + id; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2257 | slot->mmc = mmc; |
| 2258 | slot->host = host; |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2259 | host->slot[id] = slot; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2260 | |
Doug Anderson | a70aaa6 | 2013-01-11 17:03:50 +0000 | [diff] [blame] | 2261 | slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id); |
| 2262 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2263 | mmc->ops = &dw_mci_ops; |
Seungwon Jeon | 1f44a2a | 2013-08-31 00:13:31 +0900 | [diff] [blame] | 2264 | if (of_property_read_u32_array(host->dev->of_node, |
| 2265 | "clock-freq-min-max", freq, 2)) { |
| 2266 | mmc->f_min = DW_MCI_FREQ_MIN; |
| 2267 | mmc->f_max = DW_MCI_FREQ_MAX; |
| 2268 | } else { |
| 2269 | mmc->f_min = freq[0]; |
| 2270 | mmc->f_max = freq[1]; |
| 2271 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2272 | |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2273 | /*if there are external regulators, get them*/ |
| 2274 | ret = mmc_regulator_get_supply(mmc); |
| 2275 | if (ret == -EPROBE_DEFER) |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2276 | goto err_host_allocated; |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2277 | |
| 2278 | if (!mmc->ocr_avail) |
| 2279 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2280 | |
Jaehoon Chung | fc3d772 | 2011-02-25 11:08:15 +0900 | [diff] [blame] | 2281 | if (host->pdata->caps) |
| 2282 | mmc->caps = host->pdata->caps; |
Jaehoon Chung | fc3d772 | 2011-02-25 11:08:15 +0900 | [diff] [blame] | 2283 | |
Abhilash Kesavan | ab26912 | 2012-11-19 10:26:21 +0530 | [diff] [blame] | 2284 | if (host->pdata->pm_caps) |
| 2285 | mmc->pm_caps = host->pdata->pm_caps; |
| 2286 | |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2287 | if (host->dev->of_node) { |
| 2288 | ctrl_id = of_alias_get_id(host->dev->of_node, "mshc"); |
| 2289 | if (ctrl_id < 0) |
| 2290 | ctrl_id = 0; |
| 2291 | } else { |
| 2292 | ctrl_id = to_platform_device(host->dev)->id; |
| 2293 | } |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 2294 | if (drv_data && drv_data->caps) |
| 2295 | mmc->caps |= drv_data->caps[ctrl_id]; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2296 | |
Seungwon Jeon | 4f408cc | 2011-12-09 14:55:52 +0900 | [diff] [blame] | 2297 | if (host->pdata->caps2) |
| 2298 | mmc->caps2 = host->pdata->caps2; |
Seungwon Jeon | 4f408cc | 2011-12-09 14:55:52 +0900 | [diff] [blame] | 2299 | |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2300 | ret = mmc_of_parse(mmc); |
| 2301 | if (ret) |
| 2302 | goto err_host_allocated; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2303 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2304 | if (host->pdata->blk_settings) { |
| 2305 | mmc->max_segs = host->pdata->blk_settings->max_segs; |
| 2306 | mmc->max_blk_size = host->pdata->blk_settings->max_blk_size; |
| 2307 | mmc->max_blk_count = host->pdata->blk_settings->max_blk_count; |
| 2308 | mmc->max_req_size = host->pdata->blk_settings->max_req_size; |
| 2309 | mmc->max_seg_size = host->pdata->blk_settings->max_seg_size; |
| 2310 | } else { |
| 2311 | /* Useful defaults if platform data is unset. */ |
Jaehoon Chung | a39e574 | 2012-02-04 17:00:27 -0500 | [diff] [blame] | 2312 | #ifdef CONFIG_MMC_DW_IDMAC |
| 2313 | mmc->max_segs = host->ring_size; |
| 2314 | mmc->max_blk_size = 65536; |
| 2315 | mmc->max_blk_count = host->ring_size; |
| 2316 | mmc->max_seg_size = 0x1000; |
| 2317 | mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count; |
| 2318 | #else |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2319 | mmc->max_segs = 64; |
| 2320 | mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */ |
| 2321 | mmc->max_blk_count = 512; |
| 2322 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
| 2323 | mmc->max_seg_size = mmc->max_req_size; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2324 | #endif /* CONFIG_MMC_DW_IDMAC */ |
Jaehoon Chung | a39e574 | 2012-02-04 17:00:27 -0500 | [diff] [blame] | 2325 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2326 | |
Jaehoon Chung | ae0eb34 | 2014-03-03 11:36:48 +0900 | [diff] [blame] | 2327 | if (dw_mci_get_cd(mmc)) |
| 2328 | set_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 2329 | else |
| 2330 | clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 2331 | |
Jaehoon Chung | 0cea529 | 2013-02-15 23:45:45 +0900 | [diff] [blame] | 2332 | ret = mmc_add_host(mmc); |
| 2333 | if (ret) |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2334 | goto err_host_allocated; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2335 | |
| 2336 | #if defined(CONFIG_DEBUG_FS) |
| 2337 | dw_mci_init_debugfs(slot); |
| 2338 | #endif |
| 2339 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2340 | return 0; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2341 | |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2342 | err_host_allocated: |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2343 | mmc_free_host(mmc); |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2344 | return ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id) |
| 2348 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2349 | /* Debugfs stuff is cleaned up by mmc core */ |
| 2350 | mmc_remove_host(slot->mmc); |
| 2351 | slot->host->slot[id] = NULL; |
| 2352 | mmc_free_host(slot->mmc); |
| 2353 | } |
| 2354 | |
| 2355 | static void dw_mci_init_dma(struct dw_mci *host) |
| 2356 | { |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2357 | int addr_config; |
| 2358 | /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */ |
| 2359 | addr_config = (mci_readl(host, HCON) >> 27) & 0x01; |
| 2360 | |
| 2361 | if (addr_config == 1) { |
| 2362 | /* host supports IDMAC in 64-bit address mode */ |
| 2363 | host->dma_64bit_address = 1; |
| 2364 | dev_info(host->dev, "IDMAC supports 64-bit address mode.\n"); |
| 2365 | if (!dma_set_mask(host->dev, DMA_BIT_MASK(64))) |
| 2366 | dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64)); |
| 2367 | } else { |
| 2368 | /* host supports IDMAC in 32-bit address mode */ |
| 2369 | host->dma_64bit_address = 0; |
| 2370 | dev_info(host->dev, "IDMAC supports 32-bit address mode.\n"); |
| 2371 | } |
| 2372 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2373 | /* Alloc memory for sg translation */ |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2374 | host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE, |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2375 | &host->sg_dma, GFP_KERNEL); |
| 2376 | if (!host->sg_cpu) { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2377 | dev_err(host->dev, "%s: could not alloc DMA memory\n", |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2378 | __func__); |
| 2379 | goto no_dma; |
| 2380 | } |
| 2381 | |
| 2382 | /* Determine which DMA interface to use */ |
| 2383 | #ifdef CONFIG_MMC_DW_IDMAC |
| 2384 | host->dma_ops = &dw_mci_idmac_ops; |
Seungwon Jeon | 00956ea | 2012-09-28 19:13:11 +0900 | [diff] [blame] | 2385 | dev_info(host->dev, "Using internal DMA controller.\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2386 | #endif |
| 2387 | |
| 2388 | if (!host->dma_ops) |
| 2389 | goto no_dma; |
| 2390 | |
Jaehoon Chung | e1631f9 | 2012-04-18 15:42:31 +0900 | [diff] [blame] | 2391 | if (host->dma_ops->init && host->dma_ops->start && |
| 2392 | host->dma_ops->stop && host->dma_ops->cleanup) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2393 | if (host->dma_ops->init(host)) { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2394 | dev_err(host->dev, "%s: Unable to initialize " |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2395 | "DMA Controller.\n", __func__); |
| 2396 | goto no_dma; |
| 2397 | } |
| 2398 | } else { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2399 | dev_err(host->dev, "DMA initialization not found.\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2400 | goto no_dma; |
| 2401 | } |
| 2402 | |
| 2403 | host->use_dma = 1; |
| 2404 | return; |
| 2405 | |
| 2406 | no_dma: |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2407 | dev_info(host->dev, "Using PIO mode.\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2408 | host->use_dma = 0; |
| 2409 | return; |
| 2410 | } |
| 2411 | |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2412 | static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2413 | { |
| 2414 | unsigned long timeout = jiffies + msecs_to_jiffies(500); |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2415 | u32 ctrl; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2416 | |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2417 | ctrl = mci_readl(host, CTRL); |
| 2418 | ctrl |= reset; |
| 2419 | mci_writel(host, CTRL, ctrl); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2420 | |
| 2421 | /* wait till resets clear */ |
| 2422 | do { |
| 2423 | ctrl = mci_readl(host, CTRL); |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2424 | if (!(ctrl & reset)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2425 | return true; |
| 2426 | } while (time_before(jiffies, timeout)); |
| 2427 | |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2428 | dev_err(host->dev, |
| 2429 | "Timeout resetting block (ctrl reset %#x)\n", |
| 2430 | ctrl & reset); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2431 | |
| 2432 | return false; |
| 2433 | } |
| 2434 | |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2435 | static bool dw_mci_reset(struct dw_mci *host) |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2436 | { |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2437 | u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET; |
| 2438 | bool ret = false; |
| 2439 | |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2440 | /* |
| 2441 | * Reseting generates a block interrupt, hence setting |
| 2442 | * the scatter-gather pointer to NULL. |
| 2443 | */ |
| 2444 | if (host->sg) { |
| 2445 | sg_miter_stop(&host->sg_miter); |
| 2446 | host->sg = NULL; |
| 2447 | } |
| 2448 | |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2449 | if (host->use_dma) |
| 2450 | flags |= SDMMC_CTRL_DMA_RESET; |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2451 | |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2452 | if (dw_mci_ctrl_reset(host, flags)) { |
| 2453 | /* |
| 2454 | * In all cases we clear the RAWINTS register to clear any |
| 2455 | * interrupts. |
| 2456 | */ |
| 2457 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 2458 | |
| 2459 | /* if using dma we wait for dma_req to clear */ |
| 2460 | if (host->use_dma) { |
| 2461 | unsigned long timeout = jiffies + msecs_to_jiffies(500); |
| 2462 | u32 status; |
| 2463 | do { |
| 2464 | status = mci_readl(host, STATUS); |
| 2465 | if (!(status & SDMMC_STATUS_DMA_REQ)) |
| 2466 | break; |
| 2467 | cpu_relax(); |
| 2468 | } while (time_before(jiffies, timeout)); |
| 2469 | |
| 2470 | if (status & SDMMC_STATUS_DMA_REQ) { |
| 2471 | dev_err(host->dev, |
| 2472 | "%s: Timeout waiting for dma_req to " |
| 2473 | "clear during reset\n", __func__); |
| 2474 | goto ciu_out; |
| 2475 | } |
| 2476 | |
| 2477 | /* when using DMA next we reset the fifo again */ |
| 2478 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET)) |
| 2479 | goto ciu_out; |
| 2480 | } |
| 2481 | } else { |
| 2482 | /* if the controller reset bit did clear, then set clock regs */ |
| 2483 | if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) { |
| 2484 | dev_err(host->dev, "%s: fifo/dma reset bits didn't " |
| 2485 | "clear but ciu was reset, doing clock update\n", |
| 2486 | __func__); |
| 2487 | goto ciu_out; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | #if IS_ENABLED(CONFIG_MMC_DW_IDMAC) |
| 2492 | /* It is also recommended that we reset and reprogram idmac */ |
| 2493 | dw_mci_idmac_reset(host); |
| 2494 | #endif |
| 2495 | |
| 2496 | ret = true; |
| 2497 | |
| 2498 | ciu_out: |
| 2499 | /* After a CTRL reset we need to have CIU set clock registers */ |
| 2500 | mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0); |
| 2501 | |
| 2502 | return ret; |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2503 | } |
| 2504 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2505 | #ifdef CONFIG_OF |
| 2506 | static struct dw_mci_of_quirks { |
| 2507 | char *quirk; |
| 2508 | int id; |
| 2509 | } of_quirks[] = { |
| 2510 | { |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2511 | .quirk = "broken-cd", |
| 2512 | .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION, |
Jaehoon Chung | 26375b5 | 2014-08-07 16:37:58 +0900 | [diff] [blame] | 2513 | }, { |
| 2514 | .quirk = "disable-wp", |
| 2515 | .id = DW_MCI_QUIRK_NO_WRITE_PROTECT, |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2516 | }, |
| 2517 | }; |
| 2518 | |
| 2519 | static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) |
| 2520 | { |
| 2521 | struct dw_mci_board *pdata; |
| 2522 | struct device *dev = host->dev; |
| 2523 | struct device_node *np = dev->of_node; |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 2524 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2525 | int idx, ret; |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2526 | u32 clock_frequency; |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2527 | |
| 2528 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 2529 | if (!pdata) { |
| 2530 | dev_err(dev, "could not allocate memory for pdata\n"); |
| 2531 | return ERR_PTR(-ENOMEM); |
| 2532 | } |
| 2533 | |
| 2534 | /* find out number of slots supported */ |
| 2535 | if (of_property_read_u32(dev->of_node, "num-slots", |
| 2536 | &pdata->num_slots)) { |
| 2537 | dev_info(dev, "num-slots property not found, " |
| 2538 | "assuming 1 slot is available\n"); |
| 2539 | pdata->num_slots = 1; |
| 2540 | } |
| 2541 | |
| 2542 | /* get quirks */ |
| 2543 | for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++) |
| 2544 | if (of_get_property(np, of_quirks[idx].quirk, NULL)) |
| 2545 | pdata->quirks |= of_quirks[idx].id; |
| 2546 | |
| 2547 | if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth)) |
| 2548 | dev_info(dev, "fifo-depth property not found, using " |
| 2549 | "value of FIFOTH register as default\n"); |
| 2550 | |
| 2551 | of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms); |
| 2552 | |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2553 | if (!of_property_read_u32(np, "clock-frequency", &clock_frequency)) |
| 2554 | pdata->bus_hz = clock_frequency; |
| 2555 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 2556 | if (drv_data && drv_data->parse_dt) { |
| 2557 | ret = drv_data->parse_dt(host); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2558 | if (ret) |
| 2559 | return ERR_PTR(ret); |
| 2560 | } |
| 2561 | |
Seungwon Jeon | 10b4984 | 2013-08-31 00:13:22 +0900 | [diff] [blame] | 2562 | if (of_find_property(np, "supports-highspeed", NULL)) |
| 2563 | pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; |
| 2564 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2565 | return pdata; |
| 2566 | } |
| 2567 | |
| 2568 | #else /* CONFIG_OF */ |
| 2569 | static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) |
| 2570 | { |
| 2571 | return ERR_PTR(-EINVAL); |
| 2572 | } |
| 2573 | #endif /* CONFIG_OF */ |
| 2574 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2575 | int dw_mci_probe(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2576 | { |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 2577 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2578 | int width, i, ret = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2579 | u32 fifo_size; |
Thomas Abraham | 1c2215b | 2012-09-17 18:16:37 +0000 | [diff] [blame] | 2580 | int init_slots = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2581 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 2582 | if (!host->pdata) { |
| 2583 | host->pdata = dw_mci_parse_dt(host); |
| 2584 | if (IS_ERR(host->pdata)) { |
| 2585 | dev_err(host->dev, "platform data not available\n"); |
| 2586 | return -EINVAL; |
| 2587 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2588 | } |
| 2589 | |
Jaehoon Chung | 907abd5 | 2014-03-03 11:36:43 +0900 | [diff] [blame] | 2590 | if (host->pdata->num_slots > 1) { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2591 | dev_err(host->dev, |
Jaehoon Chung | 907abd5 | 2014-03-03 11:36:43 +0900 | [diff] [blame] | 2592 | "Platform data must supply num_slots.\n"); |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2593 | return -ENODEV; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2594 | } |
| 2595 | |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2596 | host->biu_clk = devm_clk_get(host->dev, "biu"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2597 | if (IS_ERR(host->biu_clk)) { |
| 2598 | dev_dbg(host->dev, "biu clock not available\n"); |
| 2599 | } else { |
| 2600 | ret = clk_prepare_enable(host->biu_clk); |
| 2601 | if (ret) { |
| 2602 | dev_err(host->dev, "failed to enable biu clock\n"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2603 | return ret; |
| 2604 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2605 | } |
| 2606 | |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2607 | host->ciu_clk = devm_clk_get(host->dev, "ciu"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2608 | if (IS_ERR(host->ciu_clk)) { |
| 2609 | dev_dbg(host->dev, "ciu clock not available\n"); |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2610 | host->bus_hz = host->pdata->bus_hz; |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2611 | } else { |
| 2612 | ret = clk_prepare_enable(host->ciu_clk); |
| 2613 | if (ret) { |
| 2614 | dev_err(host->dev, "failed to enable ciu clock\n"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2615 | goto err_clk_biu; |
| 2616 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2617 | |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2618 | if (host->pdata->bus_hz) { |
| 2619 | ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz); |
| 2620 | if (ret) |
| 2621 | dev_warn(host->dev, |
Jaehoon Chung | 612de4c | 2014-03-03 11:36:42 +0900 | [diff] [blame] | 2622 | "Unable to set bus rate to %uHz\n", |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2623 | host->pdata->bus_hz); |
| 2624 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2625 | host->bus_hz = clk_get_rate(host->ciu_clk); |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 2626 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2627 | |
Jaehoon Chung | 612de4c | 2014-03-03 11:36:42 +0900 | [diff] [blame] | 2628 | if (!host->bus_hz) { |
| 2629 | dev_err(host->dev, |
| 2630 | "Platform data must supply bus speed\n"); |
| 2631 | ret = -ENODEV; |
| 2632 | goto err_clk_ciu; |
| 2633 | } |
| 2634 | |
Yuvaraj Kumar C D | 002f0d5 | 2013-08-31 00:12:19 +0900 | [diff] [blame] | 2635 | if (drv_data && drv_data->init) { |
| 2636 | ret = drv_data->init(host); |
| 2637 | if (ret) { |
| 2638 | dev_err(host->dev, |
| 2639 | "implementation specific init failed\n"); |
| 2640 | goto err_clk_ciu; |
| 2641 | } |
| 2642 | } |
| 2643 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 2644 | if (drv_data && drv_data->setup_clock) { |
| 2645 | ret = drv_data->setup_clock(host); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2646 | if (ret) { |
| 2647 | dev_err(host->dev, |
| 2648 | "implementation specific clock setup failed\n"); |
| 2649 | goto err_clk_ciu; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2653 | host->quirks = host->pdata->quirks; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2654 | |
| 2655 | spin_lock_init(&host->lock); |
| 2656 | INIT_LIST_HEAD(&host->queue); |
| 2657 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2658 | /* |
| 2659 | * Get the host data width - this assumes that HCON has been set with |
| 2660 | * the correct values. |
| 2661 | */ |
| 2662 | i = (mci_readl(host, HCON) >> 7) & 0x7; |
| 2663 | if (!i) { |
| 2664 | host->push_data = dw_mci_push_data16; |
| 2665 | host->pull_data = dw_mci_pull_data16; |
| 2666 | width = 16; |
| 2667 | host->data_shift = 1; |
| 2668 | } else if (i == 2) { |
| 2669 | host->push_data = dw_mci_push_data64; |
| 2670 | host->pull_data = dw_mci_pull_data64; |
| 2671 | width = 64; |
| 2672 | host->data_shift = 3; |
| 2673 | } else { |
| 2674 | /* Check for a reserved value, and warn if it is */ |
| 2675 | WARN((i != 1), |
| 2676 | "HCON reports a reserved host data width!\n" |
| 2677 | "Defaulting to 32-bit access.\n"); |
| 2678 | host->push_data = dw_mci_push_data32; |
| 2679 | host->pull_data = dw_mci_pull_data32; |
| 2680 | width = 32; |
| 2681 | host->data_shift = 2; |
| 2682 | } |
| 2683 | |
| 2684 | /* Reset all blocks */ |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2685 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 2686 | return -ENODEV; |
| 2687 | |
| 2688 | host->dma_ops = host->pdata->dma_ops; |
| 2689 | dw_mci_init_dma(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2690 | |
| 2691 | /* Clear the interrupts for the host controller */ |
| 2692 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 2693 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 2694 | |
| 2695 | /* Put in max timeout */ |
| 2696 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
| 2697 | |
| 2698 | /* |
| 2699 | * FIFO threshold settings RxMark = fifo_size / 2 - 1, |
| 2700 | * Tx Mark = fifo_size / 2 DMA Size = 8 |
| 2701 | */ |
James Hogan | b86d825 | 2011-06-24 13:57:18 +0100 | [diff] [blame] | 2702 | if (!host->pdata->fifo_depth) { |
| 2703 | /* |
| 2704 | * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may |
| 2705 | * have been overwritten by the bootloader, just like we're |
| 2706 | * about to do, so if you know the value for your hardware, you |
| 2707 | * should put it in the platform data. |
| 2708 | */ |
| 2709 | fifo_size = mci_readl(host, FIFOTH); |
Jaehoon Chung | 8234e86 | 2012-01-11 09:28:21 +0000 | [diff] [blame] | 2710 | fifo_size = 1 + ((fifo_size >> 16) & 0xfff); |
James Hogan | b86d825 | 2011-06-24 13:57:18 +0100 | [diff] [blame] | 2711 | } else { |
| 2712 | fifo_size = host->pdata->fifo_depth; |
| 2713 | } |
| 2714 | host->fifo_depth = fifo_size; |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 2715 | host->fifoth_val = |
| 2716 | SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2); |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 2717 | mci_writel(host, FIFOTH, host->fifoth_val); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2718 | |
| 2719 | /* disable clock to CIU */ |
| 2720 | mci_writel(host, CLKENA, 0); |
| 2721 | mci_writel(host, CLKSRC, 0); |
| 2722 | |
James Hogan | 6300876 | 2013-03-12 10:43:54 +0000 | [diff] [blame] | 2723 | /* |
| 2724 | * In 2.40a spec, Data offset is changed. |
| 2725 | * Need to check the version-id and set data-offset for DATA register. |
| 2726 | */ |
| 2727 | host->verid = SDMMC_GET_VERID(mci_readl(host, VERID)); |
| 2728 | dev_info(host->dev, "Version ID is %04x\n", host->verid); |
| 2729 | |
| 2730 | if (host->verid < DW_MMC_240A) |
| 2731 | host->data_offset = DATA_OFFSET; |
| 2732 | else |
| 2733 | host->data_offset = DATA_240A_OFFSET; |
| 2734 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2735 | tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2736 | ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt, |
| 2737 | host->irq_flags, "dw-mci", host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2738 | if (ret) |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2739 | goto err_dmaunmap; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2740 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2741 | if (host->pdata->num_slots) |
| 2742 | host->num_slots = host->pdata->num_slots; |
| 2743 | else |
| 2744 | host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1; |
| 2745 | |
Yuvaraj CD | 2da1d7f | 2012-10-08 14:29:51 +0530 | [diff] [blame] | 2746 | /* |
| 2747 | * Enable interrupts for command done, data over, data empty, card det, |
| 2748 | * receive ready and error such as transmit, receive timeout, crc error |
| 2749 | */ |
| 2750 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 2751 | mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | |
| 2752 | SDMMC_INT_TXDR | SDMMC_INT_RXDR | |
| 2753 | DW_MCI_ERROR_FLAGS | SDMMC_INT_CD); |
| 2754 | mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */ |
| 2755 | |
| 2756 | dev_info(host->dev, "DW MMC controller at irq %d, " |
| 2757 | "%d bit host data width, " |
| 2758 | "%u deep fifo\n", |
| 2759 | host->irq, width, fifo_size); |
| 2760 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2761 | /* We need at least one slot to succeed */ |
| 2762 | for (i = 0; i < host->num_slots; i++) { |
| 2763 | ret = dw_mci_init_slot(host, i); |
Thomas Abraham | 1c2215b | 2012-09-17 18:16:37 +0000 | [diff] [blame] | 2764 | if (ret) |
| 2765 | dev_dbg(host->dev, "slot %d init failed\n", i); |
| 2766 | else |
| 2767 | init_slots++; |
| 2768 | } |
| 2769 | |
| 2770 | if (init_slots) { |
| 2771 | dev_info(host->dev, "%d slots initialized\n", init_slots); |
| 2772 | } else { |
| 2773 | dev_dbg(host->dev, "attempted to initialize %d slots, " |
| 2774 | "but failed on all\n", host->num_slots); |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2775 | goto err_dmaunmap; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2776 | } |
| 2777 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2778 | if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2779 | dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2780 | |
| 2781 | return 0; |
| 2782 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2783 | err_dmaunmap: |
| 2784 | if (host->use_dma && host->dma_ops->exit) |
| 2785 | host->dma_ops->exit(host); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2786 | |
| 2787 | err_clk_ciu: |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2788 | if (!IS_ERR(host->ciu_clk)) |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2789 | clk_disable_unprepare(host->ciu_clk); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2790 | |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2791 | err_clk_biu: |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2792 | if (!IS_ERR(host->biu_clk)) |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2793 | clk_disable_unprepare(host->biu_clk); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2794 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2795 | return ret; |
| 2796 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2797 | EXPORT_SYMBOL(dw_mci_probe); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2798 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2799 | void dw_mci_remove(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2800 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2801 | int i; |
| 2802 | |
| 2803 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 2804 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 2805 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2806 | for (i = 0; i < host->num_slots; i++) { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2807 | dev_dbg(host->dev, "remove slot %d\n", i); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2808 | if (host->slot[i]) |
| 2809 | dw_mci_cleanup_slot(host->slot[i], i); |
| 2810 | } |
| 2811 | |
| 2812 | /* disable clock to CIU */ |
| 2813 | mci_writel(host, CLKENA, 0); |
| 2814 | mci_writel(host, CLKSRC, 0); |
| 2815 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2816 | if (host->use_dma && host->dma_ops->exit) |
| 2817 | host->dma_ops->exit(host); |
| 2818 | |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2819 | if (!IS_ERR(host->ciu_clk)) |
| 2820 | clk_disable_unprepare(host->ciu_clk); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 2821 | |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 2822 | if (!IS_ERR(host->biu_clk)) |
| 2823 | clk_disable_unprepare(host->biu_clk); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2824 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2825 | EXPORT_SYMBOL(dw_mci_remove); |
| 2826 | |
| 2827 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2828 | |
Jaehoon Chung | 6fe8890 | 2011-12-08 19:23:03 +0900 | [diff] [blame] | 2829 | #ifdef CONFIG_PM_SLEEP |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2830 | /* |
| 2831 | * TODO: we should probably disable the clock to the card in the suspend path. |
| 2832 | */ |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2833 | int dw_mci_suspend(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2834 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2835 | return 0; |
| 2836 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2837 | EXPORT_SYMBOL(dw_mci_suspend); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2838 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2839 | int dw_mci_resume(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2840 | { |
| 2841 | int i, ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2842 | |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2843 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) { |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 2844 | ret = -ENODEV; |
| 2845 | return ret; |
| 2846 | } |
| 2847 | |
Jonathan Kliegman | 3bfe619 | 2012-06-14 13:31:55 -0400 | [diff] [blame] | 2848 | if (host->use_dma && host->dma_ops->init) |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 2849 | host->dma_ops->init(host); |
| 2850 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 2851 | /* |
| 2852 | * Restore the initial value at FIFOTH register |
| 2853 | * And Invalidate the prev_blksz with zero |
| 2854 | */ |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 2855 | mci_writel(host, FIFOTH, host->fifoth_val); |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 2856 | host->prev_blksz = 0; |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 2857 | |
Doug Anderson | 2eb2944 | 2013-08-31 00:11:49 +0900 | [diff] [blame] | 2858 | /* Put in max timeout */ |
| 2859 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
| 2860 | |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 2861 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 2862 | mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | |
| 2863 | SDMMC_INT_TXDR | SDMMC_INT_RXDR | |
| 2864 | DW_MCI_ERROR_FLAGS | SDMMC_INT_CD); |
| 2865 | mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); |
| 2866 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2867 | for (i = 0; i < host->num_slots; i++) { |
| 2868 | struct dw_mci_slot *slot = host->slot[i]; |
| 2869 | if (!slot) |
| 2870 | continue; |
Abhilash Kesavan | ab26912 | 2012-11-19 10:26:21 +0530 | [diff] [blame] | 2871 | if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) { |
| 2872 | dw_mci_set_ios(slot->mmc, &slot->mmc->ios); |
| 2873 | dw_mci_setup_bus(slot, true); |
| 2874 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2875 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2876 | return 0; |
| 2877 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2878 | EXPORT_SYMBOL(dw_mci_resume); |
Jaehoon Chung | 6fe8890 | 2011-12-08 19:23:03 +0900 | [diff] [blame] | 2879 | #endif /* CONFIG_PM_SLEEP */ |
| 2880 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2881 | static int __init dw_mci_init(void) |
| 2882 | { |
Sachin Kamat | 8e1c4e4 | 2013-04-04 11:25:11 +0530 | [diff] [blame] | 2883 | pr_info("Synopsys Designware Multimedia Card Interface Driver\n"); |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 2884 | return 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | static void __exit dw_mci_exit(void) |
| 2888 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | module_init(dw_mci_init); |
| 2892 | module_exit(dw_mci_exit); |
| 2893 | |
| 2894 | MODULE_DESCRIPTION("DW Multimedia Card Interface driver"); |
| 2895 | MODULE_AUTHOR("NXP Semiconductor VietNam"); |
| 2896 | MODULE_AUTHOR("Imagination Technologies Ltd"); |
| 2897 | MODULE_LICENSE("GPL v2"); |