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