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