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> |
| 25 | #include <linux/scatterlist.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/stat.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/irq.h> |
| 31 | #include <linux/mmc/host.h> |
| 32 | #include <linux/mmc/mmc.h> |
| 33 | #include <linux/mmc/dw_mmc.h> |
| 34 | #include <linux/bitops.h> |
| 35 | |
| 36 | #include "dw_mmc.h" |
| 37 | |
| 38 | /* Common flag combinations */ |
| 39 | #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \ |
| 40 | SDMMC_INT_HTO | SDMMC_INT_SBE | \ |
| 41 | SDMMC_INT_EBE) |
| 42 | #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \ |
| 43 | SDMMC_INT_RESP_ERR) |
| 44 | #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \ |
| 45 | DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE) |
| 46 | #define DW_MCI_SEND_STATUS 1 |
| 47 | #define DW_MCI_RECV_STATUS 2 |
| 48 | #define DW_MCI_DMA_THRESHOLD 16 |
| 49 | |
| 50 | #ifdef CONFIG_MMC_DW_IDMAC |
| 51 | struct idmac_desc { |
| 52 | u32 des0; /* Control Descriptor */ |
| 53 | #define IDMAC_DES0_DIC BIT(1) |
| 54 | #define IDMAC_DES0_LD BIT(2) |
| 55 | #define IDMAC_DES0_FD BIT(3) |
| 56 | #define IDMAC_DES0_CH BIT(4) |
| 57 | #define IDMAC_DES0_ER BIT(5) |
| 58 | #define IDMAC_DES0_CES BIT(30) |
| 59 | #define IDMAC_DES0_OWN BIT(31) |
| 60 | |
| 61 | u32 des1; /* Buffer sizes */ |
| 62 | #define IDMAC_SET_BUFFER1_SIZE(d, s) \ |
| 63 | ((d)->des1 = ((d)->des1 & 0x03ffc000) | ((s) & 0x3fff)) |
| 64 | |
| 65 | u32 des2; /* buffer 1 physical address */ |
| 66 | |
| 67 | u32 des3; /* buffer 2 physical address */ |
| 68 | }; |
| 69 | #endif /* CONFIG_MMC_DW_IDMAC */ |
| 70 | |
| 71 | /** |
| 72 | * struct dw_mci_slot - MMC slot state |
| 73 | * @mmc: The mmc_host representing this slot. |
| 74 | * @host: The MMC controller this slot is using. |
| 75 | * @ctype: Card type for this slot. |
| 76 | * @mrq: mmc_request currently being processed or waiting to be |
| 77 | * processed, or NULL when the slot is idle. |
| 78 | * @queue_node: List node for placing this node in the @queue list of |
| 79 | * &struct dw_mci. |
| 80 | * @clock: Clock rate configured by set_ios(). Protected by host->lock. |
| 81 | * @flags: Random state bits associated with the slot. |
| 82 | * @id: Number of this slot. |
| 83 | * @last_detect_state: Most recently observed card detect state. |
| 84 | */ |
| 85 | struct dw_mci_slot { |
| 86 | struct mmc_host *mmc; |
| 87 | struct dw_mci *host; |
| 88 | |
| 89 | u32 ctype; |
| 90 | |
| 91 | struct mmc_request *mrq; |
| 92 | struct list_head queue_node; |
| 93 | |
| 94 | unsigned int clock; |
| 95 | unsigned long flags; |
| 96 | #define DW_MMC_CARD_PRESENT 0 |
| 97 | #define DW_MMC_CARD_NEED_INIT 1 |
| 98 | int id; |
| 99 | int last_detect_state; |
| 100 | }; |
| 101 | |
| 102 | #if defined(CONFIG_DEBUG_FS) |
| 103 | static int dw_mci_req_show(struct seq_file *s, void *v) |
| 104 | { |
| 105 | struct dw_mci_slot *slot = s->private; |
| 106 | struct mmc_request *mrq; |
| 107 | struct mmc_command *cmd; |
| 108 | struct mmc_command *stop; |
| 109 | struct mmc_data *data; |
| 110 | |
| 111 | /* Make sure we get a consistent snapshot */ |
| 112 | spin_lock_bh(&slot->host->lock); |
| 113 | mrq = slot->mrq; |
| 114 | |
| 115 | if (mrq) { |
| 116 | cmd = mrq->cmd; |
| 117 | data = mrq->data; |
| 118 | stop = mrq->stop; |
| 119 | |
| 120 | if (cmd) |
| 121 | seq_printf(s, |
| 122 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 123 | cmd->opcode, cmd->arg, cmd->flags, |
| 124 | cmd->resp[0], cmd->resp[1], cmd->resp[2], |
| 125 | cmd->resp[2], cmd->error); |
| 126 | if (data) |
| 127 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", |
| 128 | data->bytes_xfered, data->blocks, |
| 129 | data->blksz, data->flags, data->error); |
| 130 | if (stop) |
| 131 | seq_printf(s, |
| 132 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 133 | stop->opcode, stop->arg, stop->flags, |
| 134 | stop->resp[0], stop->resp[1], stop->resp[2], |
| 135 | stop->resp[2], stop->error); |
| 136 | } |
| 137 | |
| 138 | spin_unlock_bh(&slot->host->lock); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int dw_mci_req_open(struct inode *inode, struct file *file) |
| 144 | { |
| 145 | return single_open(file, dw_mci_req_show, inode->i_private); |
| 146 | } |
| 147 | |
| 148 | static const struct file_operations dw_mci_req_fops = { |
| 149 | .owner = THIS_MODULE, |
| 150 | .open = dw_mci_req_open, |
| 151 | .read = seq_read, |
| 152 | .llseek = seq_lseek, |
| 153 | .release = single_release, |
| 154 | }; |
| 155 | |
| 156 | static int dw_mci_regs_show(struct seq_file *s, void *v) |
| 157 | { |
| 158 | seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS); |
| 159 | seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS); |
| 160 | seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD); |
| 161 | seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL); |
| 162 | seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK); |
| 163 | seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int dw_mci_regs_open(struct inode *inode, struct file *file) |
| 169 | { |
| 170 | return single_open(file, dw_mci_regs_show, inode->i_private); |
| 171 | } |
| 172 | |
| 173 | static const struct file_operations dw_mci_regs_fops = { |
| 174 | .owner = THIS_MODULE, |
| 175 | .open = dw_mci_regs_open, |
| 176 | .read = seq_read, |
| 177 | .llseek = seq_lseek, |
| 178 | .release = single_release, |
| 179 | }; |
| 180 | |
| 181 | static void dw_mci_init_debugfs(struct dw_mci_slot *slot) |
| 182 | { |
| 183 | struct mmc_host *mmc = slot->mmc; |
| 184 | struct dw_mci *host = slot->host; |
| 185 | struct dentry *root; |
| 186 | struct dentry *node; |
| 187 | |
| 188 | root = mmc->debugfs_root; |
| 189 | if (!root) |
| 190 | return; |
| 191 | |
| 192 | node = debugfs_create_file("regs", S_IRUSR, root, host, |
| 193 | &dw_mci_regs_fops); |
| 194 | if (!node) |
| 195 | goto err; |
| 196 | |
| 197 | node = debugfs_create_file("req", S_IRUSR, root, slot, |
| 198 | &dw_mci_req_fops); |
| 199 | if (!node) |
| 200 | goto err; |
| 201 | |
| 202 | node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
| 203 | if (!node) |
| 204 | goto err; |
| 205 | |
| 206 | node = debugfs_create_x32("pending_events", S_IRUSR, root, |
| 207 | (u32 *)&host->pending_events); |
| 208 | if (!node) |
| 209 | goto err; |
| 210 | |
| 211 | node = debugfs_create_x32("completed_events", S_IRUSR, root, |
| 212 | (u32 *)&host->completed_events); |
| 213 | if (!node) |
| 214 | goto err; |
| 215 | |
| 216 | return; |
| 217 | |
| 218 | err: |
| 219 | dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
| 220 | } |
| 221 | #endif /* defined(CONFIG_DEBUG_FS) */ |
| 222 | |
| 223 | static void dw_mci_set_timeout(struct dw_mci *host) |
| 224 | { |
| 225 | /* timeout (maximum) */ |
| 226 | mci_writel(host, TMOUT, 0xffffffff); |
| 227 | } |
| 228 | |
| 229 | static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd) |
| 230 | { |
| 231 | struct mmc_data *data; |
| 232 | u32 cmdr; |
| 233 | cmd->error = -EINPROGRESS; |
| 234 | |
| 235 | cmdr = cmd->opcode; |
| 236 | |
| 237 | if (cmdr == MMC_STOP_TRANSMISSION) |
| 238 | cmdr |= SDMMC_CMD_STOP; |
| 239 | else |
| 240 | cmdr |= SDMMC_CMD_PRV_DAT_WAIT; |
| 241 | |
| 242 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 243 | /* We expect a response, so set this bit */ |
| 244 | cmdr |= SDMMC_CMD_RESP_EXP; |
| 245 | if (cmd->flags & MMC_RSP_136) |
| 246 | cmdr |= SDMMC_CMD_RESP_LONG; |
| 247 | } |
| 248 | |
| 249 | if (cmd->flags & MMC_RSP_CRC) |
| 250 | cmdr |= SDMMC_CMD_RESP_CRC; |
| 251 | |
| 252 | data = cmd->data; |
| 253 | if (data) { |
| 254 | cmdr |= SDMMC_CMD_DAT_EXP; |
| 255 | if (data->flags & MMC_DATA_STREAM) |
| 256 | cmdr |= SDMMC_CMD_STRM_MODE; |
| 257 | if (data->flags & MMC_DATA_WRITE) |
| 258 | cmdr |= SDMMC_CMD_DAT_WR; |
| 259 | } |
| 260 | |
| 261 | return cmdr; |
| 262 | } |
| 263 | |
| 264 | static void dw_mci_start_command(struct dw_mci *host, |
| 265 | struct mmc_command *cmd, u32 cmd_flags) |
| 266 | { |
| 267 | host->cmd = cmd; |
| 268 | dev_vdbg(&host->pdev->dev, |
| 269 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
| 270 | cmd->arg, cmd_flags); |
| 271 | |
| 272 | mci_writel(host, CMDARG, cmd->arg); |
| 273 | wmb(); |
| 274 | |
| 275 | mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); |
| 276 | } |
| 277 | |
| 278 | static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data) |
| 279 | { |
| 280 | dw_mci_start_command(host, data->stop, host->stop_cmdr); |
| 281 | } |
| 282 | |
| 283 | /* DMA interface functions */ |
| 284 | static void dw_mci_stop_dma(struct dw_mci *host) |
| 285 | { |
| 286 | if (host->use_dma) { |
| 287 | host->dma_ops->stop(host); |
| 288 | host->dma_ops->cleanup(host); |
| 289 | } else { |
| 290 | /* Data transfer was stopped by the interrupt handler */ |
| 291 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #ifdef CONFIG_MMC_DW_IDMAC |
| 296 | static void dw_mci_dma_cleanup(struct dw_mci *host) |
| 297 | { |
| 298 | struct mmc_data *data = host->data; |
| 299 | |
| 300 | if (data) |
| 301 | dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len, |
| 302 | ((data->flags & MMC_DATA_WRITE) |
| 303 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 304 | } |
| 305 | |
| 306 | static void dw_mci_idmac_stop_dma(struct dw_mci *host) |
| 307 | { |
| 308 | u32 temp; |
| 309 | |
| 310 | /* Disable and reset the IDMAC interface */ |
| 311 | temp = mci_readl(host, CTRL); |
| 312 | temp &= ~SDMMC_CTRL_USE_IDMAC; |
| 313 | temp |= SDMMC_CTRL_DMA_RESET; |
| 314 | mci_writel(host, CTRL, temp); |
| 315 | |
| 316 | /* Stop the IDMAC running */ |
| 317 | temp = mci_readl(host, BMOD); |
| 318 | temp &= ~SDMMC_IDMAC_ENABLE; |
| 319 | mci_writel(host, BMOD, temp); |
| 320 | } |
| 321 | |
| 322 | static void dw_mci_idmac_complete_dma(struct dw_mci *host) |
| 323 | { |
| 324 | struct mmc_data *data = host->data; |
| 325 | |
| 326 | dev_vdbg(&host->pdev->dev, "DMA complete\n"); |
| 327 | |
| 328 | host->dma_ops->cleanup(host); |
| 329 | |
| 330 | /* |
| 331 | * If the card was removed, data will be NULL. No point in trying to |
| 332 | * send the stop command or waiting for NBUSY in this case. |
| 333 | */ |
| 334 | if (data) { |
| 335 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 336 | tasklet_schedule(&host->tasklet); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data, |
| 341 | unsigned int sg_len) |
| 342 | { |
| 343 | int i; |
| 344 | struct idmac_desc *desc = host->sg_cpu; |
| 345 | |
| 346 | for (i = 0; i < sg_len; i++, desc++) { |
| 347 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 348 | u32 mem_addr = sg_dma_address(&data->sg[i]); |
| 349 | |
| 350 | /* Set the OWN bit and disable interrupts for this descriptor */ |
| 351 | desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH; |
| 352 | |
| 353 | /* Buffer length */ |
| 354 | IDMAC_SET_BUFFER1_SIZE(desc, length); |
| 355 | |
| 356 | /* Physical address to DMA to/from */ |
| 357 | desc->des2 = mem_addr; |
| 358 | } |
| 359 | |
| 360 | /* Set first descriptor */ |
| 361 | desc = host->sg_cpu; |
| 362 | desc->des0 |= IDMAC_DES0_FD; |
| 363 | |
| 364 | /* Set last descriptor */ |
| 365 | desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc); |
| 366 | desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); |
| 367 | desc->des0 |= IDMAC_DES0_LD; |
| 368 | |
| 369 | wmb(); |
| 370 | } |
| 371 | |
| 372 | static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len) |
| 373 | { |
| 374 | u32 temp; |
| 375 | |
| 376 | dw_mci_translate_sglist(host, host->data, sg_len); |
| 377 | |
| 378 | /* Select IDMAC interface */ |
| 379 | temp = mci_readl(host, CTRL); |
| 380 | temp |= SDMMC_CTRL_USE_IDMAC; |
| 381 | mci_writel(host, CTRL, temp); |
| 382 | |
| 383 | wmb(); |
| 384 | |
| 385 | /* Enable the IDMAC */ |
| 386 | temp = mci_readl(host, BMOD); |
| 387 | temp |= SDMMC_IDMAC_ENABLE; |
| 388 | mci_writel(host, BMOD, temp); |
| 389 | |
| 390 | /* Start it running */ |
| 391 | mci_writel(host, PLDMND, 1); |
| 392 | } |
| 393 | |
| 394 | static int dw_mci_idmac_init(struct dw_mci *host) |
| 395 | { |
| 396 | struct idmac_desc *p; |
| 397 | int i; |
| 398 | |
| 399 | /* Number of descriptors in the ring buffer */ |
| 400 | host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc); |
| 401 | |
| 402 | /* Forward link the descriptor list */ |
| 403 | for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) |
| 404 | p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1)); |
| 405 | |
| 406 | /* Set the last descriptor as the end-of-ring descriptor */ |
| 407 | p->des3 = host->sg_dma; |
| 408 | p->des0 = IDMAC_DES0_ER; |
| 409 | |
| 410 | /* Mask out interrupts - get Tx & Rx complete only */ |
| 411 | mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI | |
| 412 | SDMMC_IDMAC_INT_TI); |
| 413 | |
| 414 | /* Set the descriptor base address */ |
| 415 | mci_writel(host, DBADDR, host->sg_dma); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static struct dw_mci_dma_ops dw_mci_idmac_ops = { |
| 420 | .init = dw_mci_idmac_init, |
| 421 | .start = dw_mci_idmac_start_dma, |
| 422 | .stop = dw_mci_idmac_stop_dma, |
| 423 | .complete = dw_mci_idmac_complete_dma, |
| 424 | .cleanup = dw_mci_dma_cleanup, |
| 425 | }; |
| 426 | #endif /* CONFIG_MMC_DW_IDMAC */ |
| 427 | |
| 428 | static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data) |
| 429 | { |
| 430 | struct scatterlist *sg; |
| 431 | unsigned int i, direction, sg_len; |
| 432 | u32 temp; |
| 433 | |
| 434 | /* If we don't have a channel, we can't do DMA */ |
| 435 | if (!host->use_dma) |
| 436 | return -ENODEV; |
| 437 | |
| 438 | /* |
| 439 | * We don't do DMA on "complex" transfers, i.e. with |
| 440 | * non-word-aligned buffers or lengths. Also, we don't bother |
| 441 | * with all the DMA setup overhead for short transfers. |
| 442 | */ |
| 443 | if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD) |
| 444 | return -EINVAL; |
| 445 | if (data->blksz & 3) |
| 446 | return -EINVAL; |
| 447 | |
| 448 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 449 | if (sg->offset & 3 || sg->length & 3) |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | if (data->flags & MMC_DATA_READ) |
| 454 | direction = DMA_FROM_DEVICE; |
| 455 | else |
| 456 | direction = DMA_TO_DEVICE; |
| 457 | |
| 458 | sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, |
| 459 | direction); |
| 460 | |
| 461 | dev_vdbg(&host->pdev->dev, |
| 462 | "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n", |
| 463 | (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma, |
| 464 | sg_len); |
| 465 | |
| 466 | /* Enable the DMA interface */ |
| 467 | temp = mci_readl(host, CTRL); |
| 468 | temp |= SDMMC_CTRL_DMA_ENABLE; |
| 469 | mci_writel(host, CTRL, temp); |
| 470 | |
| 471 | /* Disable RX/TX IRQs, let DMA handle it */ |
| 472 | temp = mci_readl(host, INTMASK); |
| 473 | temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR); |
| 474 | mci_writel(host, INTMASK, temp); |
| 475 | |
| 476 | host->dma_ops->start(host, sg_len); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) |
| 482 | { |
| 483 | u32 temp; |
| 484 | |
| 485 | data->error = -EINPROGRESS; |
| 486 | |
| 487 | WARN_ON(host->data); |
| 488 | host->sg = NULL; |
| 489 | host->data = data; |
| 490 | |
| 491 | if (dw_mci_submit_data_dma(host, data)) { |
| 492 | host->sg = data->sg; |
| 493 | host->pio_offset = 0; |
| 494 | if (data->flags & MMC_DATA_READ) |
| 495 | host->dir_status = DW_MCI_RECV_STATUS; |
| 496 | else |
| 497 | host->dir_status = DW_MCI_SEND_STATUS; |
| 498 | |
| 499 | temp = mci_readl(host, INTMASK); |
| 500 | temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR; |
| 501 | mci_writel(host, INTMASK, temp); |
| 502 | |
| 503 | temp = mci_readl(host, CTRL); |
| 504 | temp &= ~SDMMC_CTRL_DMA_ENABLE; |
| 505 | mci_writel(host, CTRL, temp); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg) |
| 510 | { |
| 511 | struct dw_mci *host = slot->host; |
| 512 | unsigned long timeout = jiffies + msecs_to_jiffies(500); |
| 513 | unsigned int cmd_status = 0; |
| 514 | |
| 515 | mci_writel(host, CMDARG, arg); |
| 516 | wmb(); |
| 517 | mci_writel(host, CMD, SDMMC_CMD_START | cmd); |
| 518 | |
| 519 | while (time_before(jiffies, timeout)) { |
| 520 | cmd_status = mci_readl(host, CMD); |
| 521 | if (!(cmd_status & SDMMC_CMD_START)) |
| 522 | return; |
| 523 | } |
| 524 | dev_err(&slot->mmc->class_dev, |
| 525 | "Timeout sending command (cmd %#x arg %#x status %#x)\n", |
| 526 | cmd, arg, cmd_status); |
| 527 | } |
| 528 | |
| 529 | static void dw_mci_setup_bus(struct dw_mci_slot *slot) |
| 530 | { |
| 531 | struct dw_mci *host = slot->host; |
| 532 | u32 div; |
| 533 | |
| 534 | if (slot->clock != host->current_speed) { |
| 535 | if (host->bus_hz % slot->clock) |
| 536 | /* |
| 537 | * move the + 1 after the divide to prevent |
| 538 | * over-clocking the card. |
| 539 | */ |
| 540 | div = ((host->bus_hz / slot->clock) >> 1) + 1; |
| 541 | else |
| 542 | div = (host->bus_hz / slot->clock) >> 1; |
| 543 | |
| 544 | dev_info(&slot->mmc->class_dev, |
| 545 | "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ" |
| 546 | " div = %d)\n", slot->id, host->bus_hz, slot->clock, |
| 547 | div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div); |
| 548 | |
| 549 | /* disable clock */ |
| 550 | mci_writel(host, CLKENA, 0); |
| 551 | mci_writel(host, CLKSRC, 0); |
| 552 | |
| 553 | /* inform CIU */ |
| 554 | mci_send_cmd(slot, |
| 555 | SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 556 | |
| 557 | /* set clock to desired speed */ |
| 558 | mci_writel(host, CLKDIV, div); |
| 559 | |
| 560 | /* inform CIU */ |
| 561 | mci_send_cmd(slot, |
| 562 | SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 563 | |
| 564 | /* enable clock */ |
Will Newton | aadb9f4 | 2011-02-10 10:40:57 +0000 | [diff] [blame] | 565 | mci_writel(host, CLKENA, SDMMC_CLKEN_ENABLE | |
| 566 | SDMMC_CLKEN_LOW_PWR); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 567 | |
| 568 | /* inform CIU */ |
| 569 | mci_send_cmd(slot, |
| 570 | SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 571 | |
| 572 | host->current_speed = slot->clock; |
| 573 | } |
| 574 | |
| 575 | /* Set the current slot bus width */ |
| 576 | mci_writel(host, CTYPE, slot->ctype); |
| 577 | } |
| 578 | |
| 579 | static void dw_mci_start_request(struct dw_mci *host, |
| 580 | struct dw_mci_slot *slot) |
| 581 | { |
| 582 | struct mmc_request *mrq; |
| 583 | struct mmc_command *cmd; |
| 584 | struct mmc_data *data; |
| 585 | u32 cmdflags; |
| 586 | |
| 587 | mrq = slot->mrq; |
| 588 | if (host->pdata->select_slot) |
| 589 | host->pdata->select_slot(slot->id); |
| 590 | |
| 591 | /* Slot specific timing and width adjustment */ |
| 592 | dw_mci_setup_bus(slot); |
| 593 | |
| 594 | host->cur_slot = slot; |
| 595 | host->mrq = mrq; |
| 596 | |
| 597 | host->pending_events = 0; |
| 598 | host->completed_events = 0; |
| 599 | host->data_status = 0; |
| 600 | |
| 601 | data = mrq->data; |
| 602 | if (data) { |
| 603 | dw_mci_set_timeout(host); |
| 604 | mci_writel(host, BYTCNT, data->blksz*data->blocks); |
| 605 | mci_writel(host, BLKSIZ, data->blksz); |
| 606 | } |
| 607 | |
| 608 | cmd = mrq->cmd; |
| 609 | cmdflags = dw_mci_prepare_command(slot->mmc, cmd); |
| 610 | |
| 611 | /* this is the first command, send the initialization clock */ |
| 612 | if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags)) |
| 613 | cmdflags |= SDMMC_CMD_INIT; |
| 614 | |
| 615 | if (data) { |
| 616 | dw_mci_submit_data(host, data); |
| 617 | wmb(); |
| 618 | } |
| 619 | |
| 620 | dw_mci_start_command(host, cmd, cmdflags); |
| 621 | |
| 622 | if (mrq->stop) |
| 623 | host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop); |
| 624 | } |
| 625 | |
| 626 | static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot, |
| 627 | struct mmc_request *mrq) |
| 628 | { |
| 629 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", |
| 630 | host->state); |
| 631 | |
| 632 | spin_lock_bh(&host->lock); |
| 633 | slot->mrq = mrq; |
| 634 | |
| 635 | if (host->state == STATE_IDLE) { |
| 636 | host->state = STATE_SENDING_CMD; |
| 637 | dw_mci_start_request(host, slot); |
| 638 | } else { |
| 639 | list_add_tail(&slot->queue_node, &host->queue); |
| 640 | } |
| 641 | |
| 642 | spin_unlock_bh(&host->lock); |
| 643 | } |
| 644 | |
| 645 | static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 646 | { |
| 647 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 648 | struct dw_mci *host = slot->host; |
| 649 | |
| 650 | WARN_ON(slot->mrq); |
| 651 | |
| 652 | if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) { |
| 653 | mrq->cmd->error = -ENOMEDIUM; |
| 654 | mmc_request_done(mmc, mrq); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | /* We don't support multiple blocks of weird lengths. */ |
| 659 | dw_mci_queue_request(host, slot, mrq); |
| 660 | } |
| 661 | |
| 662 | static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 663 | { |
| 664 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 665 | |
| 666 | /* set default 1 bit mode */ |
| 667 | slot->ctype = SDMMC_CTYPE_1BIT; |
| 668 | |
| 669 | switch (ios->bus_width) { |
| 670 | case MMC_BUS_WIDTH_1: |
| 671 | slot->ctype = SDMMC_CTYPE_1BIT; |
| 672 | break; |
| 673 | case MMC_BUS_WIDTH_4: |
| 674 | slot->ctype = SDMMC_CTYPE_4BIT; |
| 675 | break; |
Jaehoon Chung | c9b2a06 | 2011-02-17 16:12:38 +0900 | [diff] [blame^] | 676 | case MMC_BUS_WIDTH_8: |
| 677 | slot->ctype = SDMMC_CTYPE_8BIT; |
| 678 | break; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (ios->clock) { |
| 682 | /* |
| 683 | * Use mirror of ios->clock to prevent race with mmc |
| 684 | * core ios update when finding the minimum. |
| 685 | */ |
| 686 | slot->clock = ios->clock; |
| 687 | } |
| 688 | |
| 689 | switch (ios->power_mode) { |
| 690 | case MMC_POWER_UP: |
| 691 | set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags); |
| 692 | break; |
| 693 | default: |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | static int dw_mci_get_ro(struct mmc_host *mmc) |
| 699 | { |
| 700 | int read_only; |
| 701 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 702 | struct dw_mci_board *brd = slot->host->pdata; |
| 703 | |
| 704 | /* Use platform get_ro function, else try on board write protect */ |
| 705 | if (brd->get_ro) |
| 706 | read_only = brd->get_ro(slot->id); |
| 707 | else |
| 708 | read_only = |
| 709 | mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0; |
| 710 | |
| 711 | dev_dbg(&mmc->class_dev, "card is %s\n", |
| 712 | read_only ? "read-only" : "read-write"); |
| 713 | |
| 714 | return read_only; |
| 715 | } |
| 716 | |
| 717 | static int dw_mci_get_cd(struct mmc_host *mmc) |
| 718 | { |
| 719 | int present; |
| 720 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 721 | struct dw_mci_board *brd = slot->host->pdata; |
| 722 | |
| 723 | /* Use platform get_cd function, else try onboard card detect */ |
| 724 | if (brd->get_cd) |
| 725 | present = !brd->get_cd(slot->id); |
| 726 | else |
| 727 | present = (mci_readl(slot->host, CDETECT) & (1 << slot->id)) |
| 728 | == 0 ? 1 : 0; |
| 729 | |
| 730 | if (present) |
| 731 | dev_dbg(&mmc->class_dev, "card is present\n"); |
| 732 | else |
| 733 | dev_dbg(&mmc->class_dev, "card is not present\n"); |
| 734 | |
| 735 | return present; |
| 736 | } |
| 737 | |
| 738 | static const struct mmc_host_ops dw_mci_ops = { |
| 739 | .request = dw_mci_request, |
| 740 | .set_ios = dw_mci_set_ios, |
| 741 | .get_ro = dw_mci_get_ro, |
| 742 | .get_cd = dw_mci_get_cd, |
| 743 | }; |
| 744 | |
| 745 | static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq) |
| 746 | __releases(&host->lock) |
| 747 | __acquires(&host->lock) |
| 748 | { |
| 749 | struct dw_mci_slot *slot; |
| 750 | struct mmc_host *prev_mmc = host->cur_slot->mmc; |
| 751 | |
| 752 | WARN_ON(host->cmd || host->data); |
| 753 | |
| 754 | host->cur_slot->mrq = NULL; |
| 755 | host->mrq = NULL; |
| 756 | if (!list_empty(&host->queue)) { |
| 757 | slot = list_entry(host->queue.next, |
| 758 | struct dw_mci_slot, queue_node); |
| 759 | list_del(&slot->queue_node); |
| 760 | dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n", |
| 761 | mmc_hostname(slot->mmc)); |
| 762 | host->state = STATE_SENDING_CMD; |
| 763 | dw_mci_start_request(host, slot); |
| 764 | } else { |
| 765 | dev_vdbg(&host->pdev->dev, "list empty\n"); |
| 766 | host->state = STATE_IDLE; |
| 767 | } |
| 768 | |
| 769 | spin_unlock(&host->lock); |
| 770 | mmc_request_done(prev_mmc, mrq); |
| 771 | spin_lock(&host->lock); |
| 772 | } |
| 773 | |
| 774 | static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd) |
| 775 | { |
| 776 | u32 status = host->cmd_status; |
| 777 | |
| 778 | host->cmd_status = 0; |
| 779 | |
| 780 | /* Read the response from the card (up to 16 bytes) */ |
| 781 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 782 | if (cmd->flags & MMC_RSP_136) { |
| 783 | cmd->resp[3] = mci_readl(host, RESP0); |
| 784 | cmd->resp[2] = mci_readl(host, RESP1); |
| 785 | cmd->resp[1] = mci_readl(host, RESP2); |
| 786 | cmd->resp[0] = mci_readl(host, RESP3); |
| 787 | } else { |
| 788 | cmd->resp[0] = mci_readl(host, RESP0); |
| 789 | cmd->resp[1] = 0; |
| 790 | cmd->resp[2] = 0; |
| 791 | cmd->resp[3] = 0; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | if (status & SDMMC_INT_RTO) |
| 796 | cmd->error = -ETIMEDOUT; |
| 797 | else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)) |
| 798 | cmd->error = -EILSEQ; |
| 799 | else if (status & SDMMC_INT_RESP_ERR) |
| 800 | cmd->error = -EIO; |
| 801 | else |
| 802 | cmd->error = 0; |
| 803 | |
| 804 | if (cmd->error) { |
| 805 | /* newer ip versions need a delay between retries */ |
| 806 | if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY) |
| 807 | mdelay(20); |
| 808 | |
| 809 | if (cmd->data) { |
| 810 | host->data = NULL; |
| 811 | dw_mci_stop_dma(host); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | static void dw_mci_tasklet_func(unsigned long priv) |
| 817 | { |
| 818 | struct dw_mci *host = (struct dw_mci *)priv; |
| 819 | struct mmc_data *data; |
| 820 | struct mmc_command *cmd; |
| 821 | enum dw_mci_state state; |
| 822 | enum dw_mci_state prev_state; |
| 823 | u32 status; |
| 824 | |
| 825 | spin_lock(&host->lock); |
| 826 | |
| 827 | state = host->state; |
| 828 | data = host->data; |
| 829 | |
| 830 | do { |
| 831 | prev_state = state; |
| 832 | |
| 833 | switch (state) { |
| 834 | case STATE_IDLE: |
| 835 | break; |
| 836 | |
| 837 | case STATE_SENDING_CMD: |
| 838 | if (!test_and_clear_bit(EVENT_CMD_COMPLETE, |
| 839 | &host->pending_events)) |
| 840 | break; |
| 841 | |
| 842 | cmd = host->cmd; |
| 843 | host->cmd = NULL; |
| 844 | set_bit(EVENT_CMD_COMPLETE, &host->completed_events); |
| 845 | dw_mci_command_complete(host, host->mrq->cmd); |
| 846 | if (!host->mrq->data || cmd->error) { |
| 847 | dw_mci_request_end(host, host->mrq); |
| 848 | goto unlock; |
| 849 | } |
| 850 | |
| 851 | prev_state = state = STATE_SENDING_DATA; |
| 852 | /* fall through */ |
| 853 | |
| 854 | case STATE_SENDING_DATA: |
| 855 | if (test_and_clear_bit(EVENT_DATA_ERROR, |
| 856 | &host->pending_events)) { |
| 857 | dw_mci_stop_dma(host); |
| 858 | if (data->stop) |
| 859 | send_stop_cmd(host, data); |
| 860 | state = STATE_DATA_ERROR; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
| 865 | &host->pending_events)) |
| 866 | break; |
| 867 | |
| 868 | set_bit(EVENT_XFER_COMPLETE, &host->completed_events); |
| 869 | prev_state = state = STATE_DATA_BUSY; |
| 870 | /* fall through */ |
| 871 | |
| 872 | case STATE_DATA_BUSY: |
| 873 | if (!test_and_clear_bit(EVENT_DATA_COMPLETE, |
| 874 | &host->pending_events)) |
| 875 | break; |
| 876 | |
| 877 | host->data = NULL; |
| 878 | set_bit(EVENT_DATA_COMPLETE, &host->completed_events); |
| 879 | status = host->data_status; |
| 880 | |
| 881 | if (status & DW_MCI_DATA_ERROR_FLAGS) { |
| 882 | if (status & SDMMC_INT_DTO) { |
| 883 | dev_err(&host->pdev->dev, |
| 884 | "data timeout error\n"); |
| 885 | data->error = -ETIMEDOUT; |
| 886 | } else if (status & SDMMC_INT_DCRC) { |
| 887 | dev_err(&host->pdev->dev, |
| 888 | "data CRC error\n"); |
| 889 | data->error = -EILSEQ; |
| 890 | } else { |
| 891 | dev_err(&host->pdev->dev, |
| 892 | "data FIFO error " |
| 893 | "(status=%08x)\n", |
| 894 | status); |
| 895 | data->error = -EIO; |
| 896 | } |
| 897 | } else { |
| 898 | data->bytes_xfered = data->blocks * data->blksz; |
| 899 | data->error = 0; |
| 900 | } |
| 901 | |
| 902 | if (!data->stop) { |
| 903 | dw_mci_request_end(host, host->mrq); |
| 904 | goto unlock; |
| 905 | } |
| 906 | |
| 907 | prev_state = state = STATE_SENDING_STOP; |
| 908 | if (!data->error) |
| 909 | send_stop_cmd(host, data); |
| 910 | /* fall through */ |
| 911 | |
| 912 | case STATE_SENDING_STOP: |
| 913 | if (!test_and_clear_bit(EVENT_CMD_COMPLETE, |
| 914 | &host->pending_events)) |
| 915 | break; |
| 916 | |
| 917 | host->cmd = NULL; |
| 918 | dw_mci_command_complete(host, host->mrq->stop); |
| 919 | dw_mci_request_end(host, host->mrq); |
| 920 | goto unlock; |
| 921 | |
| 922 | case STATE_DATA_ERROR: |
| 923 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
| 924 | &host->pending_events)) |
| 925 | break; |
| 926 | |
| 927 | state = STATE_DATA_BUSY; |
| 928 | break; |
| 929 | } |
| 930 | } while (state != prev_state); |
| 931 | |
| 932 | host->state = state; |
| 933 | unlock: |
| 934 | spin_unlock(&host->lock); |
| 935 | |
| 936 | } |
| 937 | |
| 938 | static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt) |
| 939 | { |
| 940 | u16 *pdata = (u16 *)buf; |
| 941 | |
| 942 | WARN_ON(cnt % 2 != 0); |
| 943 | |
| 944 | cnt = cnt >> 1; |
| 945 | while (cnt > 0) { |
| 946 | mci_writew(host, DATA, *pdata++); |
| 947 | cnt--; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt) |
| 952 | { |
| 953 | u16 *pdata = (u16 *)buf; |
| 954 | |
| 955 | WARN_ON(cnt % 2 != 0); |
| 956 | |
| 957 | cnt = cnt >> 1; |
| 958 | while (cnt > 0) { |
| 959 | *pdata++ = mci_readw(host, DATA); |
| 960 | cnt--; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt) |
| 965 | { |
| 966 | u32 *pdata = (u32 *)buf; |
| 967 | |
| 968 | WARN_ON(cnt % 4 != 0); |
| 969 | WARN_ON((unsigned long)pdata & 0x3); |
| 970 | |
| 971 | cnt = cnt >> 2; |
| 972 | while (cnt > 0) { |
| 973 | mci_writel(host, DATA, *pdata++); |
| 974 | cnt--; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt) |
| 979 | { |
| 980 | u32 *pdata = (u32 *)buf; |
| 981 | |
| 982 | WARN_ON(cnt % 4 != 0); |
| 983 | WARN_ON((unsigned long)pdata & 0x3); |
| 984 | |
| 985 | cnt = cnt >> 2; |
| 986 | while (cnt > 0) { |
| 987 | *pdata++ = mci_readl(host, DATA); |
| 988 | cnt--; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) |
| 993 | { |
| 994 | u64 *pdata = (u64 *)buf; |
| 995 | |
| 996 | WARN_ON(cnt % 8 != 0); |
| 997 | |
| 998 | cnt = cnt >> 3; |
| 999 | while (cnt > 0) { |
| 1000 | mci_writeq(host, DATA, *pdata++); |
| 1001 | cnt--; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt) |
| 1006 | { |
| 1007 | u64 *pdata = (u64 *)buf; |
| 1008 | |
| 1009 | WARN_ON(cnt % 8 != 0); |
| 1010 | |
| 1011 | cnt = cnt >> 3; |
| 1012 | while (cnt > 0) { |
| 1013 | *pdata++ = mci_readq(host, DATA); |
| 1014 | cnt--; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static void dw_mci_read_data_pio(struct dw_mci *host) |
| 1019 | { |
| 1020 | struct scatterlist *sg = host->sg; |
| 1021 | void *buf = sg_virt(sg); |
| 1022 | unsigned int offset = host->pio_offset; |
| 1023 | struct mmc_data *data = host->data; |
| 1024 | int shift = host->data_shift; |
| 1025 | u32 status; |
| 1026 | unsigned int nbytes = 0, len, old_len, count = 0; |
| 1027 | |
| 1028 | do { |
| 1029 | len = SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift; |
| 1030 | if (count == 0) |
| 1031 | old_len = len; |
| 1032 | |
| 1033 | if (offset + len <= sg->length) { |
| 1034 | host->pull_data(host, (void *)(buf + offset), len); |
| 1035 | |
| 1036 | offset += len; |
| 1037 | nbytes += len; |
| 1038 | |
| 1039 | if (offset == sg->length) { |
| 1040 | flush_dcache_page(sg_page(sg)); |
| 1041 | host->sg = sg = sg_next(sg); |
| 1042 | if (!sg) |
| 1043 | goto done; |
| 1044 | |
| 1045 | offset = 0; |
| 1046 | buf = sg_virt(sg); |
| 1047 | } |
| 1048 | } else { |
| 1049 | unsigned int remaining = sg->length - offset; |
| 1050 | host->pull_data(host, (void *)(buf + offset), |
| 1051 | remaining); |
| 1052 | nbytes += remaining; |
| 1053 | |
| 1054 | flush_dcache_page(sg_page(sg)); |
| 1055 | host->sg = sg = sg_next(sg); |
| 1056 | if (!sg) |
| 1057 | goto done; |
| 1058 | |
| 1059 | offset = len - remaining; |
| 1060 | buf = sg_virt(sg); |
| 1061 | host->pull_data(host, buf, offset); |
| 1062 | nbytes += offset; |
| 1063 | } |
| 1064 | |
| 1065 | status = mci_readl(host, MINTSTS); |
| 1066 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
| 1067 | if (status & DW_MCI_DATA_ERROR_FLAGS) { |
| 1068 | host->data_status = status; |
| 1069 | data->bytes_xfered += nbytes; |
| 1070 | smp_wmb(); |
| 1071 | |
| 1072 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
| 1073 | |
| 1074 | tasklet_schedule(&host->tasklet); |
| 1075 | return; |
| 1076 | } |
| 1077 | count++; |
| 1078 | } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/ |
| 1079 | len = SDMMC_GET_FCNT(mci_readl(host, STATUS)); |
| 1080 | host->pio_offset = offset; |
| 1081 | data->bytes_xfered += nbytes; |
| 1082 | return; |
| 1083 | |
| 1084 | done: |
| 1085 | data->bytes_xfered += nbytes; |
| 1086 | smp_wmb(); |
| 1087 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 1088 | } |
| 1089 | |
| 1090 | static void dw_mci_write_data_pio(struct dw_mci *host) |
| 1091 | { |
| 1092 | struct scatterlist *sg = host->sg; |
| 1093 | void *buf = sg_virt(sg); |
| 1094 | unsigned int offset = host->pio_offset; |
| 1095 | struct mmc_data *data = host->data; |
| 1096 | int shift = host->data_shift; |
| 1097 | u32 status; |
| 1098 | unsigned int nbytes = 0, len; |
| 1099 | |
| 1100 | do { |
| 1101 | len = SDMMC_FIFO_SZ - |
| 1102 | (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift); |
| 1103 | if (offset + len <= sg->length) { |
| 1104 | host->push_data(host, (void *)(buf + offset), len); |
| 1105 | |
| 1106 | offset += len; |
| 1107 | nbytes += len; |
| 1108 | if (offset == sg->length) { |
| 1109 | host->sg = sg = sg_next(sg); |
| 1110 | if (!sg) |
| 1111 | goto done; |
| 1112 | |
| 1113 | offset = 0; |
| 1114 | buf = sg_virt(sg); |
| 1115 | } |
| 1116 | } else { |
| 1117 | unsigned int remaining = sg->length - offset; |
| 1118 | |
| 1119 | host->push_data(host, (void *)(buf + offset), |
| 1120 | remaining); |
| 1121 | nbytes += remaining; |
| 1122 | |
| 1123 | host->sg = sg = sg_next(sg); |
| 1124 | if (!sg) |
| 1125 | goto done; |
| 1126 | |
| 1127 | offset = len - remaining; |
| 1128 | buf = sg_virt(sg); |
| 1129 | host->push_data(host, (void *)buf, offset); |
| 1130 | nbytes += offset; |
| 1131 | } |
| 1132 | |
| 1133 | status = mci_readl(host, MINTSTS); |
| 1134 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
| 1135 | if (status & DW_MCI_DATA_ERROR_FLAGS) { |
| 1136 | host->data_status = status; |
| 1137 | data->bytes_xfered += nbytes; |
| 1138 | |
| 1139 | smp_wmb(); |
| 1140 | |
| 1141 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
| 1142 | |
| 1143 | tasklet_schedule(&host->tasklet); |
| 1144 | return; |
| 1145 | } |
| 1146 | } while (status & SDMMC_INT_TXDR); /* if TXDR write again */ |
| 1147 | |
| 1148 | host->pio_offset = offset; |
| 1149 | data->bytes_xfered += nbytes; |
| 1150 | |
| 1151 | return; |
| 1152 | |
| 1153 | done: |
| 1154 | data->bytes_xfered += nbytes; |
| 1155 | smp_wmb(); |
| 1156 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 1157 | } |
| 1158 | |
| 1159 | static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) |
| 1160 | { |
| 1161 | if (!host->cmd_status) |
| 1162 | host->cmd_status = status; |
| 1163 | |
| 1164 | smp_wmb(); |
| 1165 | |
| 1166 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 1167 | tasklet_schedule(&host->tasklet); |
| 1168 | } |
| 1169 | |
| 1170 | static irqreturn_t dw_mci_interrupt(int irq, void *dev_id) |
| 1171 | { |
| 1172 | struct dw_mci *host = dev_id; |
| 1173 | u32 status, pending; |
| 1174 | unsigned int pass_count = 0; |
| 1175 | |
| 1176 | do { |
| 1177 | status = mci_readl(host, RINTSTS); |
| 1178 | pending = mci_readl(host, MINTSTS); /* read-only mask reg */ |
| 1179 | |
| 1180 | /* |
| 1181 | * DTO fix - version 2.10a and below, and only if internal DMA |
| 1182 | * is configured. |
| 1183 | */ |
| 1184 | if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) { |
| 1185 | if (!pending && |
| 1186 | ((mci_readl(host, STATUS) >> 17) & 0x1fff)) |
| 1187 | pending |= SDMMC_INT_DATA_OVER; |
| 1188 | } |
| 1189 | |
| 1190 | if (!pending) |
| 1191 | break; |
| 1192 | |
| 1193 | if (pending & DW_MCI_CMD_ERROR_FLAGS) { |
| 1194 | mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); |
| 1195 | host->cmd_status = status; |
| 1196 | smp_wmb(); |
| 1197 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 1198 | tasklet_schedule(&host->tasklet); |
| 1199 | } |
| 1200 | |
| 1201 | if (pending & DW_MCI_DATA_ERROR_FLAGS) { |
| 1202 | /* if there is an error report DATA_ERROR */ |
| 1203 | mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS); |
| 1204 | host->data_status = status; |
| 1205 | smp_wmb(); |
| 1206 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
| 1207 | tasklet_schedule(&host->tasklet); |
| 1208 | } |
| 1209 | |
| 1210 | if (pending & SDMMC_INT_DATA_OVER) { |
| 1211 | mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER); |
| 1212 | if (!host->data_status) |
| 1213 | host->data_status = status; |
| 1214 | smp_wmb(); |
| 1215 | if (host->dir_status == DW_MCI_RECV_STATUS) { |
| 1216 | if (host->sg != NULL) |
| 1217 | dw_mci_read_data_pio(host); |
| 1218 | } |
| 1219 | set_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 1220 | tasklet_schedule(&host->tasklet); |
| 1221 | } |
| 1222 | |
| 1223 | if (pending & SDMMC_INT_RXDR) { |
| 1224 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
| 1225 | if (host->sg) |
| 1226 | dw_mci_read_data_pio(host); |
| 1227 | } |
| 1228 | |
| 1229 | if (pending & SDMMC_INT_TXDR) { |
| 1230 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
| 1231 | if (host->sg) |
| 1232 | dw_mci_write_data_pio(host); |
| 1233 | } |
| 1234 | |
| 1235 | if (pending & SDMMC_INT_CMD_DONE) { |
| 1236 | mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); |
| 1237 | dw_mci_cmd_interrupt(host, status); |
| 1238 | } |
| 1239 | |
| 1240 | if (pending & SDMMC_INT_CD) { |
| 1241 | mci_writel(host, RINTSTS, SDMMC_INT_CD); |
| 1242 | tasklet_schedule(&host->card_tasklet); |
| 1243 | } |
| 1244 | |
| 1245 | } while (pass_count++ < 5); |
| 1246 | |
| 1247 | #ifdef CONFIG_MMC_DW_IDMAC |
| 1248 | /* Handle DMA interrupts */ |
| 1249 | pending = mci_readl(host, IDSTS); |
| 1250 | if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { |
| 1251 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI); |
| 1252 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI); |
| 1253 | set_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 1254 | host->dma_ops->complete(host); |
| 1255 | } |
| 1256 | #endif |
| 1257 | |
| 1258 | return IRQ_HANDLED; |
| 1259 | } |
| 1260 | |
| 1261 | static void dw_mci_tasklet_card(unsigned long data) |
| 1262 | { |
| 1263 | struct dw_mci *host = (struct dw_mci *)data; |
| 1264 | int i; |
| 1265 | |
| 1266 | for (i = 0; i < host->num_slots; i++) { |
| 1267 | struct dw_mci_slot *slot = host->slot[i]; |
| 1268 | struct mmc_host *mmc = slot->mmc; |
| 1269 | struct mmc_request *mrq; |
| 1270 | int present; |
| 1271 | u32 ctrl; |
| 1272 | |
| 1273 | present = dw_mci_get_cd(mmc); |
| 1274 | while (present != slot->last_detect_state) { |
| 1275 | spin_lock(&host->lock); |
| 1276 | |
| 1277 | dev_dbg(&slot->mmc->class_dev, "card %s\n", |
| 1278 | present ? "inserted" : "removed"); |
| 1279 | |
| 1280 | /* Card change detected */ |
| 1281 | slot->last_detect_state = present; |
| 1282 | |
| 1283 | /* Power up slot */ |
| 1284 | if (present != 0) { |
| 1285 | if (host->pdata->setpower) |
| 1286 | host->pdata->setpower(slot->id, |
| 1287 | mmc->ocr_avail); |
| 1288 | |
| 1289 | set_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 1290 | } |
| 1291 | |
| 1292 | /* Clean up queue if present */ |
| 1293 | mrq = slot->mrq; |
| 1294 | if (mrq) { |
| 1295 | if (mrq == host->mrq) { |
| 1296 | host->data = NULL; |
| 1297 | host->cmd = NULL; |
| 1298 | |
| 1299 | switch (host->state) { |
| 1300 | case STATE_IDLE: |
| 1301 | break; |
| 1302 | case STATE_SENDING_CMD: |
| 1303 | mrq->cmd->error = -ENOMEDIUM; |
| 1304 | if (!mrq->data) |
| 1305 | break; |
| 1306 | /* fall through */ |
| 1307 | case STATE_SENDING_DATA: |
| 1308 | mrq->data->error = -ENOMEDIUM; |
| 1309 | dw_mci_stop_dma(host); |
| 1310 | break; |
| 1311 | case STATE_DATA_BUSY: |
| 1312 | case STATE_DATA_ERROR: |
| 1313 | if (mrq->data->error == -EINPROGRESS) |
| 1314 | mrq->data->error = -ENOMEDIUM; |
| 1315 | if (!mrq->stop) |
| 1316 | break; |
| 1317 | /* fall through */ |
| 1318 | case STATE_SENDING_STOP: |
| 1319 | mrq->stop->error = -ENOMEDIUM; |
| 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | dw_mci_request_end(host, mrq); |
| 1324 | } else { |
| 1325 | list_del(&slot->queue_node); |
| 1326 | mrq->cmd->error = -ENOMEDIUM; |
| 1327 | if (mrq->data) |
| 1328 | mrq->data->error = -ENOMEDIUM; |
| 1329 | if (mrq->stop) |
| 1330 | mrq->stop->error = -ENOMEDIUM; |
| 1331 | |
| 1332 | spin_unlock(&host->lock); |
| 1333 | mmc_request_done(slot->mmc, mrq); |
| 1334 | spin_lock(&host->lock); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* Power down slot */ |
| 1339 | if (present == 0) { |
| 1340 | if (host->pdata->setpower) |
| 1341 | host->pdata->setpower(slot->id, 0); |
| 1342 | clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 1343 | |
| 1344 | /* |
| 1345 | * Clear down the FIFO - doing so generates a |
| 1346 | * block interrupt, hence setting the |
| 1347 | * scatter-gather pointer to NULL. |
| 1348 | */ |
| 1349 | host->sg = NULL; |
| 1350 | |
| 1351 | ctrl = mci_readl(host, CTRL); |
| 1352 | ctrl |= SDMMC_CTRL_FIFO_RESET; |
| 1353 | mci_writel(host, CTRL, ctrl); |
| 1354 | |
| 1355 | #ifdef CONFIG_MMC_DW_IDMAC |
| 1356 | ctrl = mci_readl(host, BMOD); |
| 1357 | ctrl |= 0x01; /* Software reset of DMA */ |
| 1358 | mci_writel(host, BMOD, ctrl); |
| 1359 | #endif |
| 1360 | |
| 1361 | } |
| 1362 | |
| 1363 | spin_unlock(&host->lock); |
| 1364 | present = dw_mci_get_cd(mmc); |
| 1365 | } |
| 1366 | |
| 1367 | mmc_detect_change(slot->mmc, |
| 1368 | msecs_to_jiffies(host->pdata->detect_delay_ms)); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id) |
| 1373 | { |
| 1374 | struct mmc_host *mmc; |
| 1375 | struct dw_mci_slot *slot; |
| 1376 | |
| 1377 | mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev); |
| 1378 | if (!mmc) |
| 1379 | return -ENOMEM; |
| 1380 | |
| 1381 | slot = mmc_priv(mmc); |
| 1382 | slot->id = id; |
| 1383 | slot->mmc = mmc; |
| 1384 | slot->host = host; |
| 1385 | |
| 1386 | mmc->ops = &dw_mci_ops; |
| 1387 | mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510); |
| 1388 | mmc->f_max = host->bus_hz; |
| 1389 | |
| 1390 | if (host->pdata->get_ocr) |
| 1391 | mmc->ocr_avail = host->pdata->get_ocr(id); |
| 1392 | else |
| 1393 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1394 | |
| 1395 | /* |
| 1396 | * Start with slot power disabled, it will be enabled when a card |
| 1397 | * is detected. |
| 1398 | */ |
| 1399 | if (host->pdata->setpower) |
| 1400 | host->pdata->setpower(id, 0); |
| 1401 | |
| 1402 | mmc->caps = 0; |
| 1403 | if (host->pdata->get_bus_wd) |
| 1404 | if (host->pdata->get_bus_wd(slot->id) >= 4) |
| 1405 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 1406 | |
| 1407 | if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED) |
| 1408 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
| 1409 | |
| 1410 | #ifdef CONFIG_MMC_DW_IDMAC |
| 1411 | mmc->max_segs = host->ring_size; |
| 1412 | mmc->max_blk_size = 65536; |
| 1413 | mmc->max_blk_count = host->ring_size; |
| 1414 | mmc->max_seg_size = 0x1000; |
| 1415 | mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count; |
| 1416 | #else |
| 1417 | if (host->pdata->blk_settings) { |
| 1418 | mmc->max_segs = host->pdata->blk_settings->max_segs; |
| 1419 | mmc->max_blk_size = host->pdata->blk_settings->max_blk_size; |
| 1420 | mmc->max_blk_count = host->pdata->blk_settings->max_blk_count; |
| 1421 | mmc->max_req_size = host->pdata->blk_settings->max_req_size; |
| 1422 | mmc->max_seg_size = host->pdata->blk_settings->max_seg_size; |
| 1423 | } else { |
| 1424 | /* Useful defaults if platform data is unset. */ |
| 1425 | mmc->max_segs = 64; |
| 1426 | mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */ |
| 1427 | mmc->max_blk_count = 512; |
| 1428 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
| 1429 | mmc->max_seg_size = mmc->max_req_size; |
| 1430 | } |
| 1431 | #endif /* CONFIG_MMC_DW_IDMAC */ |
| 1432 | |
| 1433 | if (dw_mci_get_cd(mmc)) |
| 1434 | set_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 1435 | else |
| 1436 | clear_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 1437 | |
| 1438 | host->slot[id] = slot; |
| 1439 | mmc_add_host(mmc); |
| 1440 | |
| 1441 | #if defined(CONFIG_DEBUG_FS) |
| 1442 | dw_mci_init_debugfs(slot); |
| 1443 | #endif |
| 1444 | |
| 1445 | /* Card initially undetected */ |
| 1446 | slot->last_detect_state = 0; |
| 1447 | |
Will Newton | dd6c4b9 | 2011-02-10 14:37:03 -0500 | [diff] [blame] | 1448 | /* |
| 1449 | * Card may have been plugged in prior to boot so we |
| 1450 | * need to run the detect tasklet |
| 1451 | */ |
| 1452 | tasklet_schedule(&host->card_tasklet); |
| 1453 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id) |
| 1458 | { |
| 1459 | /* Shutdown detect IRQ */ |
| 1460 | if (slot->host->pdata->exit) |
| 1461 | slot->host->pdata->exit(id); |
| 1462 | |
| 1463 | /* Debugfs stuff is cleaned up by mmc core */ |
| 1464 | mmc_remove_host(slot->mmc); |
| 1465 | slot->host->slot[id] = NULL; |
| 1466 | mmc_free_host(slot->mmc); |
| 1467 | } |
| 1468 | |
| 1469 | static void dw_mci_init_dma(struct dw_mci *host) |
| 1470 | { |
| 1471 | /* Alloc memory for sg translation */ |
| 1472 | host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE, |
| 1473 | &host->sg_dma, GFP_KERNEL); |
| 1474 | if (!host->sg_cpu) { |
| 1475 | dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n", |
| 1476 | __func__); |
| 1477 | goto no_dma; |
| 1478 | } |
| 1479 | |
| 1480 | /* Determine which DMA interface to use */ |
| 1481 | #ifdef CONFIG_MMC_DW_IDMAC |
| 1482 | host->dma_ops = &dw_mci_idmac_ops; |
| 1483 | dev_info(&host->pdev->dev, "Using internal DMA controller.\n"); |
| 1484 | #endif |
| 1485 | |
| 1486 | if (!host->dma_ops) |
| 1487 | goto no_dma; |
| 1488 | |
| 1489 | if (host->dma_ops->init) { |
| 1490 | if (host->dma_ops->init(host)) { |
| 1491 | dev_err(&host->pdev->dev, "%s: Unable to initialize " |
| 1492 | "DMA Controller.\n", __func__); |
| 1493 | goto no_dma; |
| 1494 | } |
| 1495 | } else { |
| 1496 | dev_err(&host->pdev->dev, "DMA initialization not found.\n"); |
| 1497 | goto no_dma; |
| 1498 | } |
| 1499 | |
| 1500 | host->use_dma = 1; |
| 1501 | return; |
| 1502 | |
| 1503 | no_dma: |
| 1504 | dev_info(&host->pdev->dev, "Using PIO mode.\n"); |
| 1505 | host->use_dma = 0; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | static bool mci_wait_reset(struct device *dev, struct dw_mci *host) |
| 1510 | { |
| 1511 | unsigned long timeout = jiffies + msecs_to_jiffies(500); |
| 1512 | unsigned int ctrl; |
| 1513 | |
| 1514 | mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | |
| 1515 | SDMMC_CTRL_DMA_RESET)); |
| 1516 | |
| 1517 | /* wait till resets clear */ |
| 1518 | do { |
| 1519 | ctrl = mci_readl(host, CTRL); |
| 1520 | if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | |
| 1521 | SDMMC_CTRL_DMA_RESET))) |
| 1522 | return true; |
| 1523 | } while (time_before(jiffies, timeout)); |
| 1524 | |
| 1525 | dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl); |
| 1526 | |
| 1527 | return false; |
| 1528 | } |
| 1529 | |
| 1530 | static int dw_mci_probe(struct platform_device *pdev) |
| 1531 | { |
| 1532 | struct dw_mci *host; |
| 1533 | struct resource *regs; |
| 1534 | struct dw_mci_board *pdata; |
| 1535 | int irq, ret, i, width; |
| 1536 | u32 fifo_size; |
| 1537 | |
| 1538 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1539 | if (!regs) |
| 1540 | return -ENXIO; |
| 1541 | |
| 1542 | irq = platform_get_irq(pdev, 0); |
| 1543 | if (irq < 0) |
| 1544 | return irq; |
| 1545 | |
| 1546 | host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL); |
| 1547 | if (!host) |
| 1548 | return -ENOMEM; |
| 1549 | |
| 1550 | host->pdev = pdev; |
| 1551 | host->pdata = pdata = pdev->dev.platform_data; |
| 1552 | if (!pdata || !pdata->init) { |
| 1553 | dev_err(&pdev->dev, |
| 1554 | "Platform data must supply init function\n"); |
| 1555 | ret = -ENODEV; |
| 1556 | goto err_freehost; |
| 1557 | } |
| 1558 | |
| 1559 | if (!pdata->select_slot && pdata->num_slots > 1) { |
| 1560 | dev_err(&pdev->dev, |
| 1561 | "Platform data must supply select_slot function\n"); |
| 1562 | ret = -ENODEV; |
| 1563 | goto err_freehost; |
| 1564 | } |
| 1565 | |
| 1566 | if (!pdata->bus_hz) { |
| 1567 | dev_err(&pdev->dev, |
| 1568 | "Platform data must supply bus speed\n"); |
| 1569 | ret = -ENODEV; |
| 1570 | goto err_freehost; |
| 1571 | } |
| 1572 | |
| 1573 | host->bus_hz = pdata->bus_hz; |
| 1574 | host->quirks = pdata->quirks; |
| 1575 | |
| 1576 | spin_lock_init(&host->lock); |
| 1577 | INIT_LIST_HEAD(&host->queue); |
| 1578 | |
| 1579 | ret = -ENOMEM; |
| 1580 | host->regs = ioremap(regs->start, regs->end - regs->start + 1); |
| 1581 | if (!host->regs) |
| 1582 | goto err_freehost; |
| 1583 | |
| 1584 | host->dma_ops = pdata->dma_ops; |
| 1585 | dw_mci_init_dma(host); |
| 1586 | |
| 1587 | /* |
| 1588 | * Get the host data width - this assumes that HCON has been set with |
| 1589 | * the correct values. |
| 1590 | */ |
| 1591 | i = (mci_readl(host, HCON) >> 7) & 0x7; |
| 1592 | if (!i) { |
| 1593 | host->push_data = dw_mci_push_data16; |
| 1594 | host->pull_data = dw_mci_pull_data16; |
| 1595 | width = 16; |
| 1596 | host->data_shift = 1; |
| 1597 | } else if (i == 2) { |
| 1598 | host->push_data = dw_mci_push_data64; |
| 1599 | host->pull_data = dw_mci_pull_data64; |
| 1600 | width = 64; |
| 1601 | host->data_shift = 3; |
| 1602 | } else { |
| 1603 | /* Check for a reserved value, and warn if it is */ |
| 1604 | WARN((i != 1), |
| 1605 | "HCON reports a reserved host data width!\n" |
| 1606 | "Defaulting to 32-bit access.\n"); |
| 1607 | host->push_data = dw_mci_push_data32; |
| 1608 | host->pull_data = dw_mci_pull_data32; |
| 1609 | width = 32; |
| 1610 | host->data_shift = 2; |
| 1611 | } |
| 1612 | |
| 1613 | /* Reset all blocks */ |
| 1614 | if (!mci_wait_reset(&pdev->dev, host)) { |
| 1615 | ret = -ENODEV; |
| 1616 | goto err_dmaunmap; |
| 1617 | } |
| 1618 | |
| 1619 | /* Clear the interrupts for the host controller */ |
| 1620 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 1621 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 1622 | |
| 1623 | /* Put in max timeout */ |
| 1624 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
| 1625 | |
| 1626 | /* |
| 1627 | * FIFO threshold settings RxMark = fifo_size / 2 - 1, |
| 1628 | * Tx Mark = fifo_size / 2 DMA Size = 8 |
| 1629 | */ |
| 1630 | fifo_size = mci_readl(host, FIFOTH); |
| 1631 | fifo_size = (fifo_size >> 16) & 0x7ff; |
| 1632 | mci_writel(host, FIFOTH, ((0x2 << 28) | ((fifo_size/2 - 1) << 16) | |
| 1633 | ((fifo_size/2) << 0))); |
| 1634 | |
| 1635 | /* disable clock to CIU */ |
| 1636 | mci_writel(host, CLKENA, 0); |
| 1637 | mci_writel(host, CLKSRC, 0); |
| 1638 | |
| 1639 | tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host); |
| 1640 | tasklet_init(&host->card_tasklet, |
| 1641 | dw_mci_tasklet_card, (unsigned long)host); |
| 1642 | |
| 1643 | ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host); |
| 1644 | if (ret) |
| 1645 | goto err_dmaunmap; |
| 1646 | |
| 1647 | platform_set_drvdata(pdev, host); |
| 1648 | |
| 1649 | if (host->pdata->num_slots) |
| 1650 | host->num_slots = host->pdata->num_slots; |
| 1651 | else |
| 1652 | host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1; |
| 1653 | |
| 1654 | /* We need at least one slot to succeed */ |
| 1655 | for (i = 0; i < host->num_slots; i++) { |
| 1656 | ret = dw_mci_init_slot(host, i); |
| 1657 | if (ret) { |
| 1658 | ret = -ENODEV; |
| 1659 | goto err_init_slot; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Enable interrupts for command done, data over, data empty, card det, |
| 1665 | * receive ready and error such as transmit, receive timeout, crc error |
| 1666 | */ |
| 1667 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 1668 | mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | |
| 1669 | SDMMC_INT_TXDR | SDMMC_INT_RXDR | |
| 1670 | DW_MCI_ERROR_FLAGS | SDMMC_INT_CD); |
| 1671 | mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */ |
| 1672 | |
| 1673 | dev_info(&pdev->dev, "DW MMC controller at irq %d, " |
| 1674 | "%d bit host data width\n", irq, width); |
| 1675 | if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) |
| 1676 | dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n"); |
| 1677 | |
| 1678 | return 0; |
| 1679 | |
| 1680 | err_init_slot: |
| 1681 | /* De-init any initialized slots */ |
| 1682 | while (i > 0) { |
| 1683 | if (host->slot[i]) |
| 1684 | dw_mci_cleanup_slot(host->slot[i], i); |
| 1685 | i--; |
| 1686 | } |
| 1687 | free_irq(irq, host); |
| 1688 | |
| 1689 | err_dmaunmap: |
| 1690 | if (host->use_dma && host->dma_ops->exit) |
| 1691 | host->dma_ops->exit(host); |
| 1692 | dma_free_coherent(&host->pdev->dev, PAGE_SIZE, |
| 1693 | host->sg_cpu, host->sg_dma); |
| 1694 | iounmap(host->regs); |
| 1695 | |
| 1696 | err_freehost: |
| 1697 | kfree(host); |
| 1698 | return ret; |
| 1699 | } |
| 1700 | |
| 1701 | static int __exit dw_mci_remove(struct platform_device *pdev) |
| 1702 | { |
| 1703 | struct dw_mci *host = platform_get_drvdata(pdev); |
| 1704 | int i; |
| 1705 | |
| 1706 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 1707 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 1708 | |
| 1709 | platform_set_drvdata(pdev, NULL); |
| 1710 | |
| 1711 | for (i = 0; i < host->num_slots; i++) { |
| 1712 | dev_dbg(&pdev->dev, "remove slot %d\n", i); |
| 1713 | if (host->slot[i]) |
| 1714 | dw_mci_cleanup_slot(host->slot[i], i); |
| 1715 | } |
| 1716 | |
| 1717 | /* disable clock to CIU */ |
| 1718 | mci_writel(host, CLKENA, 0); |
| 1719 | mci_writel(host, CLKSRC, 0); |
| 1720 | |
| 1721 | free_irq(platform_get_irq(pdev, 0), host); |
| 1722 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
| 1723 | |
| 1724 | if (host->use_dma && host->dma_ops->exit) |
| 1725 | host->dma_ops->exit(host); |
| 1726 | |
| 1727 | iounmap(host->regs); |
| 1728 | |
| 1729 | kfree(host); |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | #ifdef CONFIG_PM |
| 1734 | /* |
| 1735 | * TODO: we should probably disable the clock to the card in the suspend path. |
| 1736 | */ |
| 1737 | static int dw_mci_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 1738 | { |
| 1739 | int i, ret; |
| 1740 | struct dw_mci *host = platform_get_drvdata(pdev); |
| 1741 | |
| 1742 | for (i = 0; i < host->num_slots; i++) { |
| 1743 | struct dw_mci_slot *slot = host->slot[i]; |
| 1744 | if (!slot) |
| 1745 | continue; |
| 1746 | ret = mmc_suspend_host(slot->mmc); |
| 1747 | if (ret < 0) { |
| 1748 | while (--i >= 0) { |
| 1749 | slot = host->slot[i]; |
| 1750 | if (slot) |
| 1751 | mmc_resume_host(host->slot[i]->mmc); |
| 1752 | } |
| 1753 | return ret; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
| 1760 | static int dw_mci_resume(struct platform_device *pdev) |
| 1761 | { |
| 1762 | int i, ret; |
| 1763 | struct dw_mci *host = platform_get_drvdata(pdev); |
| 1764 | |
| 1765 | for (i = 0; i < host->num_slots; i++) { |
| 1766 | struct dw_mci_slot *slot = host->slot[i]; |
| 1767 | if (!slot) |
| 1768 | continue; |
| 1769 | ret = mmc_resume_host(host->slot[i]->mmc); |
| 1770 | if (ret < 0) |
| 1771 | return ret; |
| 1772 | } |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | #else |
| 1777 | #define dw_mci_suspend NULL |
| 1778 | #define dw_mci_resume NULL |
| 1779 | #endif /* CONFIG_PM */ |
| 1780 | |
| 1781 | static struct platform_driver dw_mci_driver = { |
| 1782 | .remove = __exit_p(dw_mci_remove), |
| 1783 | .suspend = dw_mci_suspend, |
| 1784 | .resume = dw_mci_resume, |
| 1785 | .driver = { |
| 1786 | .name = "dw_mmc", |
| 1787 | }, |
| 1788 | }; |
| 1789 | |
| 1790 | static int __init dw_mci_init(void) |
| 1791 | { |
| 1792 | return platform_driver_probe(&dw_mci_driver, dw_mci_probe); |
| 1793 | } |
| 1794 | |
| 1795 | static void __exit dw_mci_exit(void) |
| 1796 | { |
| 1797 | platform_driver_unregister(&dw_mci_driver); |
| 1798 | } |
| 1799 | |
| 1800 | module_init(dw_mci_init); |
| 1801 | module_exit(dw_mci_exit); |
| 1802 | |
| 1803 | MODULE_DESCRIPTION("DW Multimedia Card Interface driver"); |
| 1804 | MODULE_AUTHOR("NXP Semiconductor VietNam"); |
| 1805 | MODULE_AUTHOR("Imagination Technologies Ltd"); |
| 1806 | MODULE_LICENSE("GPL v2"); |