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