San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Google Inc, |
| 5 | * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Based on mmci.c |
| 12 | * |
| 13 | * Author: San Mehat (san@android.com) |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/log2.h> |
| 27 | #include <linux/mmc/host.h> |
| 28 | #include <linux/mmc/card.h> |
San Mehat | b3fa579 | 2009-11-02 18:46:09 -0800 | [diff] [blame] | 29 | #include <linux/mmc/sdio.h> |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 30 | #include <linux/clk.h> |
| 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <linux/debugfs.h> |
| 35 | #include <linux/io.h> |
| 36 | #include <linux/memory.h> |
| 37 | |
| 38 | #include <asm/cacheflush.h> |
| 39 | #include <asm/div64.h> |
| 40 | #include <asm/sizes.h> |
| 41 | |
Pavel Machek | 3989d17 | 2009-12-08 11:11:36 -0800 | [diff] [blame] | 42 | #include <mach/mmc.h> |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 43 | #include <mach/msm_iomap.h> |
| 44 | #include <mach/dma.h> |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 45 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 46 | #include "msm_sdcc.h" |
| 47 | |
| 48 | #define DRIVER_NAME "msm-sdcc" |
| 49 | |
| 50 | static unsigned int msmsdcc_fmin = 144000; |
| 51 | static unsigned int msmsdcc_fmax = 50000000; |
| 52 | static unsigned int msmsdcc_4bit = 1; |
| 53 | static unsigned int msmsdcc_pwrsave = 1; |
| 54 | static unsigned int msmsdcc_piopoll = 1; |
| 55 | static unsigned int msmsdcc_sdioirq; |
| 56 | |
| 57 | #define PIO_SPINMAX 30 |
| 58 | #define CMD_SPINMAX 20 |
| 59 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 60 | static void |
| 61 | msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, |
| 62 | u32 c); |
| 63 | |
| 64 | static void |
| 65 | msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq) |
| 66 | { |
| 67 | writel(0, host->base + MMCICOMMAND); |
| 68 | |
| 69 | BUG_ON(host->curr.data); |
| 70 | |
| 71 | host->curr.mrq = NULL; |
| 72 | host->curr.cmd = NULL; |
| 73 | |
| 74 | if (mrq->data) |
| 75 | mrq->data->bytes_xfered = host->curr.data_xfered; |
| 76 | if (mrq->cmd->error == -ETIMEDOUT) |
| 77 | mdelay(5); |
| 78 | |
| 79 | /* |
| 80 | * Need to drop the host lock here; mmc_request_done may call |
| 81 | * back into the driver... |
| 82 | */ |
| 83 | spin_unlock(&host->lock); |
| 84 | mmc_request_done(host->mmc, mrq); |
| 85 | spin_lock(&host->lock); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | msmsdcc_stop_data(struct msmsdcc_host *host) |
| 90 | { |
| 91 | writel(0, host->base + MMCIDATACTRL); |
| 92 | host->curr.data = NULL; |
| 93 | host->curr.got_dataend = host->curr.got_datablkend = 0; |
| 94 | } |
| 95 | |
| 96 | uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host) |
| 97 | { |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 98 | switch (host->pdev_id) { |
| 99 | case 1: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 100 | return MSM_SDC1_PHYS + MMCIFIFO; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 101 | case 2: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 102 | return MSM_SDC2_PHYS + MMCIFIFO; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 103 | case 3: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 104 | return MSM_SDC3_PHYS + MMCIFIFO; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 105 | case 4: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 106 | return MSM_SDC4_PHYS + MMCIFIFO; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 107 | } |
| 108 | BUG(); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd, |
| 114 | unsigned int result, |
| 115 | struct msm_dmov_errdata *err) |
| 116 | { |
| 117 | struct msmsdcc_dma_data *dma_data = |
| 118 | container_of(cmd, struct msmsdcc_dma_data, hdr); |
| 119 | struct msmsdcc_host *host = dma_data->host; |
| 120 | unsigned long flags; |
| 121 | struct mmc_request *mrq; |
| 122 | |
| 123 | spin_lock_irqsave(&host->lock, flags); |
| 124 | mrq = host->curr.mrq; |
| 125 | BUG_ON(!mrq); |
| 126 | |
| 127 | if (!(result & DMOV_RSLT_VALID)) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 128 | pr_err("msmsdcc: Invalid DataMover result\n"); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | if (result & DMOV_RSLT_DONE) { |
| 133 | host->curr.data_xfered = host->curr.xfer_size; |
| 134 | } else { |
| 135 | /* Error or flush */ |
| 136 | if (result & DMOV_RSLT_ERROR) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 137 | pr_err("%s: DMA error (0x%.8x)\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 138 | mmc_hostname(host->mmc), result); |
| 139 | if (result & DMOV_RSLT_FLUSH) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 140 | pr_err("%s: DMA channel flushed (0x%.8x)\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 141 | mmc_hostname(host->mmc), result); |
| 142 | if (err) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 143 | pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 144 | err->flush[0], err->flush[1], err->flush[2], |
| 145 | err->flush[3], err->flush[4], err->flush[5]); |
| 146 | if (!mrq->data->error) |
| 147 | mrq->data->error = -EIO; |
| 148 | } |
| 149 | host->dma.busy = 0; |
| 150 | dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents, |
| 151 | host->dma.dir); |
| 152 | |
| 153 | if (host->curr.user_pages) { |
| 154 | struct scatterlist *sg = host->dma.sg; |
| 155 | int i; |
| 156 | |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 157 | for (i = 0; i < host->dma.num_ents; i++) |
| 158 | flush_dcache_page(sg_page(sg++)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | host->dma.sg = NULL; |
| 162 | |
| 163 | if ((host->curr.got_dataend && host->curr.got_datablkend) |
| 164 | || mrq->data->error) { |
| 165 | |
| 166 | /* |
| 167 | * If we've already gotten our DATAEND / DATABLKEND |
| 168 | * for this request, then complete it through here. |
| 169 | */ |
| 170 | msmsdcc_stop_data(host); |
| 171 | |
| 172 | if (!mrq->data->error) |
| 173 | host->curr.data_xfered = host->curr.xfer_size; |
| 174 | if (!mrq->data->stop || mrq->cmd->error) { |
| 175 | writel(0, host->base + MMCICOMMAND); |
| 176 | host->curr.mrq = NULL; |
| 177 | host->curr.cmd = NULL; |
| 178 | mrq->data->bytes_xfered = host->curr.data_xfered; |
| 179 | |
| 180 | spin_unlock_irqrestore(&host->lock, flags); |
| 181 | mmc_request_done(host->mmc, mrq); |
| 182 | return; |
| 183 | } else |
| 184 | msmsdcc_start_command(host, mrq->data->stop, 0); |
| 185 | } |
| 186 | |
| 187 | out: |
| 188 | spin_unlock_irqrestore(&host->lock, flags); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data) |
| 193 | { |
| 194 | if (host->dma.channel == -1) |
| 195 | return -ENOENT; |
| 196 | |
| 197 | if ((data->blksz * data->blocks) < MCI_FIFOSIZE) |
| 198 | return -EINVAL; |
| 199 | if ((data->blksz * data->blocks) % MCI_FIFOSIZE) |
| 200 | return -EINVAL; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data) |
| 205 | { |
| 206 | struct msmsdcc_nc_dmadata *nc; |
| 207 | dmov_box *box; |
| 208 | uint32_t rows; |
| 209 | uint32_t crci; |
| 210 | unsigned int n; |
| 211 | int i, rc; |
| 212 | struct scatterlist *sg = data->sg; |
| 213 | |
| 214 | rc = validate_dma(host, data); |
| 215 | if (rc) |
| 216 | return rc; |
| 217 | |
| 218 | host->dma.sg = data->sg; |
| 219 | host->dma.num_ents = data->sg_len; |
| 220 | |
| 221 | nc = host->dma.nc; |
| 222 | |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 223 | switch (host->pdev_id) { |
| 224 | case 1: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 225 | crci = MSMSDCC_CRCI_SDC1; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 226 | break; |
| 227 | case 2: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 228 | crci = MSMSDCC_CRCI_SDC2; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 229 | break; |
| 230 | case 3: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 231 | crci = MSMSDCC_CRCI_SDC3; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 232 | break; |
| 233 | case 4: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 234 | crci = MSMSDCC_CRCI_SDC4; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 235 | break; |
| 236 | default: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 237 | host->dma.sg = NULL; |
| 238 | host->dma.num_ents = 0; |
| 239 | return -ENOENT; |
| 240 | } |
| 241 | |
| 242 | if (data->flags & MMC_DATA_READ) |
| 243 | host->dma.dir = DMA_FROM_DEVICE; |
| 244 | else |
| 245 | host->dma.dir = DMA_TO_DEVICE; |
| 246 | |
| 247 | host->curr.user_pages = 0; |
| 248 | |
| 249 | n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg, |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 250 | host->dma.num_ents, host->dma.dir); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 251 | |
| 252 | if (n != host->dma.num_ents) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 253 | pr_err("%s: Unable to map in all sg elements\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 254 | mmc_hostname(host->mmc)); |
| 255 | host->dma.sg = NULL; |
| 256 | host->dma.num_ents = 0; |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | |
| 260 | box = &nc->cmd[0]; |
| 261 | for (i = 0; i < host->dma.num_ents; i++) { |
| 262 | box->cmd = CMD_MODE_BOX; |
| 263 | |
| 264 | if (i == (host->dma.num_ents - 1)) |
| 265 | box->cmd |= CMD_LC; |
| 266 | rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ? |
| 267 | (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 : |
| 268 | (sg_dma_len(sg) / MCI_FIFOSIZE) ; |
| 269 | |
| 270 | if (data->flags & MMC_DATA_READ) { |
| 271 | box->src_row_addr = msmsdcc_fifo_addr(host); |
| 272 | box->dst_row_addr = sg_dma_address(sg); |
| 273 | |
| 274 | box->src_dst_len = (MCI_FIFOSIZE << 16) | |
| 275 | (MCI_FIFOSIZE); |
| 276 | box->row_offset = MCI_FIFOSIZE; |
| 277 | |
| 278 | box->num_rows = rows * ((1 << 16) + 1); |
| 279 | box->cmd |= CMD_SRC_CRCI(crci); |
| 280 | } else { |
| 281 | box->src_row_addr = sg_dma_address(sg); |
| 282 | box->dst_row_addr = msmsdcc_fifo_addr(host); |
| 283 | |
| 284 | box->src_dst_len = (MCI_FIFOSIZE << 16) | |
| 285 | (MCI_FIFOSIZE); |
| 286 | box->row_offset = (MCI_FIFOSIZE << 16); |
| 287 | |
| 288 | box->num_rows = rows * ((1 << 16) + 1); |
| 289 | box->cmd |= CMD_DST_CRCI(crci); |
| 290 | } |
| 291 | box++; |
| 292 | sg++; |
| 293 | } |
| 294 | |
| 295 | /* location of command block must be 64 bit aligned */ |
| 296 | BUG_ON(host->dma.cmd_busaddr & 0x07); |
| 297 | |
| 298 | nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP; |
| 299 | host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST | |
| 300 | DMOV_CMD_ADDR(host->dma.cmdptr_busaddr); |
| 301 | host->dma.hdr.complete_func = msmsdcc_dma_complete_func; |
San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame^] | 302 | host->dma.hdr.execute_func = NULL; |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data) |
| 309 | { |
| 310 | unsigned int datactrl, timeout; |
| 311 | unsigned long long clks; |
| 312 | void __iomem *base = host->base; |
| 313 | unsigned int pio_irqmask = 0; |
| 314 | |
| 315 | host->curr.data = data; |
| 316 | host->curr.xfer_size = data->blksz * data->blocks; |
| 317 | host->curr.xfer_remain = host->curr.xfer_size; |
| 318 | host->curr.data_xfered = 0; |
| 319 | host->curr.got_dataend = 0; |
| 320 | host->curr.got_datablkend = 0; |
| 321 | |
| 322 | memset(&host->pio, 0, sizeof(host->pio)); |
| 323 | |
| 324 | clks = (unsigned long long)data->timeout_ns * host->clk_rate; |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 325 | do_div(clks, NSEC_PER_SEC); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 326 | timeout = data->timeout_clks + (unsigned int)clks; |
| 327 | writel(timeout, base + MMCIDATATIMER); |
| 328 | |
| 329 | writel(host->curr.xfer_size, base + MMCIDATALENGTH); |
| 330 | |
| 331 | datactrl = MCI_DPSM_ENABLE | (data->blksz << 4); |
| 332 | |
| 333 | if (!msmsdcc_config_dma(host, data)) |
| 334 | datactrl |= MCI_DPSM_DMAENABLE; |
| 335 | else { |
| 336 | host->pio.sg = data->sg; |
| 337 | host->pio.sg_len = data->sg_len; |
| 338 | host->pio.sg_off = 0; |
| 339 | |
| 340 | if (data->flags & MMC_DATA_READ) { |
| 341 | pio_irqmask = MCI_RXFIFOHALFFULLMASK; |
| 342 | if (host->curr.xfer_remain < MCI_FIFOSIZE) |
| 343 | pio_irqmask |= MCI_RXDATAAVLBLMASK; |
| 344 | } else |
| 345 | pio_irqmask = MCI_TXFIFOHALFEMPTYMASK; |
| 346 | } |
| 347 | |
| 348 | if (data->flags & MMC_DATA_READ) |
| 349 | datactrl |= MCI_DPSM_DIRECTION; |
| 350 | |
| 351 | writel(pio_irqmask, base + MMCIMASK1); |
| 352 | writel(datactrl, base + MMCIDATACTRL); |
| 353 | |
| 354 | if (datactrl & MCI_DPSM_DMAENABLE) { |
| 355 | host->dma.busy = 1; |
| 356 | msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr); |
| 357 | } |
| 358 | } |
| 359 | |
San Mehat | b3fa579 | 2009-11-02 18:46:09 -0800 | [diff] [blame] | 360 | static int |
| 361 | snoop_cccr_abort(struct mmc_command *cmd) |
| 362 | { |
| 363 | if ((cmd->opcode == 52) && |
| 364 | (cmd->arg & 0x80000000) && |
| 365 | (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT)) |
| 366 | return 1; |
| 367 | return 0; |
| 368 | } |
| 369 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 370 | static void |
| 371 | msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c) |
| 372 | { |
| 373 | void __iomem *base = host->base; |
| 374 | |
| 375 | if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) { |
| 376 | writel(0, base + MMCICOMMAND); |
| 377 | udelay(2 + ((5 * 1000000) / host->clk_rate)); |
| 378 | } |
| 379 | |
| 380 | c |= cmd->opcode | MCI_CPSM_ENABLE; |
| 381 | |
| 382 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 383 | if (cmd->flags & MMC_RSP_136) |
| 384 | c |= MCI_CPSM_LONGRSP; |
| 385 | c |= MCI_CPSM_RESPONSE; |
| 386 | } |
| 387 | |
Joe Perches | 75d1452 | 2009-09-22 16:44:24 -0700 | [diff] [blame] | 388 | if (cmd->opcode == 17 || cmd->opcode == 18 || |
| 389 | cmd->opcode == 24 || cmd->opcode == 25 || |
| 390 | cmd->opcode == 53) |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 391 | c |= MCI_CSPM_DATCMD; |
| 392 | |
| 393 | if (cmd == cmd->mrq->stop) |
| 394 | c |= MCI_CSPM_MCIABORT; |
| 395 | |
San Mehat | b3fa579 | 2009-11-02 18:46:09 -0800 | [diff] [blame] | 396 | if (snoop_cccr_abort(cmd)) |
| 397 | c |= MCI_CSPM_MCIABORT; |
| 398 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 399 | host->curr.cmd = cmd; |
| 400 | |
| 401 | host->stats.cmds++; |
| 402 | |
| 403 | writel(cmd->arg, base + MMCIARGUMENT); |
| 404 | writel(c, base + MMCICOMMAND); |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data, |
| 409 | unsigned int status) |
| 410 | { |
| 411 | if (status & MCI_DATACRCFAIL) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 412 | pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc)); |
| 413 | pr_err("%s: opcode 0x%.8x\n", __func__, |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 414 | data->mrq->cmd->opcode); |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 415 | pr_err("%s: blksz %d, blocks %d\n", __func__, |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 416 | data->blksz, data->blocks); |
| 417 | data->error = -EILSEQ; |
| 418 | } else if (status & MCI_DATATIMEOUT) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 419 | pr_err("%s: Data timeout\n", mmc_hostname(host->mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 420 | data->error = -ETIMEDOUT; |
| 421 | } else if (status & MCI_RXOVERRUN) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 422 | pr_err("%s: RX overrun\n", mmc_hostname(host->mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 423 | data->error = -EIO; |
| 424 | } else if (status & MCI_TXUNDERRUN) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 425 | pr_err("%s: TX underrun\n", mmc_hostname(host->mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 426 | data->error = -EIO; |
| 427 | } else { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 428 | pr_err("%s: Unknown error (0x%.8x)\n", |
| 429 | mmc_hostname(host->mmc), status); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 430 | data->error = -EIO; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | static int |
| 436 | msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain) |
| 437 | { |
| 438 | void __iomem *base = host->base; |
| 439 | uint32_t *ptr = (uint32_t *) buffer; |
| 440 | int count = 0; |
| 441 | |
| 442 | while (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL) { |
| 443 | |
| 444 | *ptr = readl(base + MMCIFIFO + (count % MCI_FIFOSIZE)); |
| 445 | ptr++; |
| 446 | count += sizeof(uint32_t); |
| 447 | |
| 448 | remain -= sizeof(uint32_t); |
| 449 | if (remain == 0) |
| 450 | break; |
| 451 | } |
| 452 | return count; |
| 453 | } |
| 454 | |
| 455 | static int |
| 456 | msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer, |
| 457 | unsigned int remain, u32 status) |
| 458 | { |
| 459 | void __iomem *base = host->base; |
| 460 | char *ptr = buffer; |
| 461 | |
| 462 | do { |
| 463 | unsigned int count, maxcnt; |
| 464 | |
| 465 | maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : |
| 466 | MCI_FIFOHALFSIZE; |
| 467 | count = min(remain, maxcnt); |
| 468 | |
| 469 | writesl(base + MMCIFIFO, ptr, count >> 2); |
| 470 | ptr += count; |
| 471 | remain -= count; |
| 472 | |
| 473 | if (remain == 0) |
| 474 | break; |
| 475 | |
| 476 | status = readl(base + MMCISTATUS); |
| 477 | } while (status & MCI_TXFIFOHALFEMPTY); |
| 478 | |
| 479 | return ptr - buffer; |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin) |
| 484 | { |
| 485 | while (maxspin) { |
| 486 | if ((readl(host->base + MMCISTATUS) & mask)) |
| 487 | return 0; |
| 488 | udelay(1); |
| 489 | --maxspin; |
| 490 | } |
| 491 | return -ETIMEDOUT; |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | msmsdcc_pio_irq(int irq, void *dev_id) |
| 496 | { |
| 497 | struct msmsdcc_host *host = dev_id; |
| 498 | void __iomem *base = host->base; |
| 499 | uint32_t status; |
| 500 | |
| 501 | status = readl(base + MMCISTATUS); |
| 502 | |
| 503 | do { |
| 504 | unsigned long flags; |
| 505 | unsigned int remain, len; |
| 506 | char *buffer; |
| 507 | |
| 508 | if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) { |
| 509 | if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll) |
| 510 | break; |
| 511 | |
| 512 | if (msmsdcc_spin_on_status(host, |
| 513 | (MCI_TXFIFOHALFEMPTY | |
| 514 | MCI_RXDATAAVLBL), |
| 515 | PIO_SPINMAX)) { |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* Map the current scatter buffer */ |
| 521 | local_irq_save(flags); |
| 522 | buffer = kmap_atomic(sg_page(host->pio.sg), |
| 523 | KM_BIO_SRC_IRQ) + host->pio.sg->offset; |
| 524 | buffer += host->pio.sg_off; |
| 525 | remain = host->pio.sg->length - host->pio.sg_off; |
| 526 | len = 0; |
| 527 | if (status & MCI_RXACTIVE) |
| 528 | len = msmsdcc_pio_read(host, buffer, remain); |
| 529 | if (status & MCI_TXACTIVE) |
| 530 | len = msmsdcc_pio_write(host, buffer, remain, status); |
| 531 | |
| 532 | /* Unmap the buffer */ |
| 533 | kunmap_atomic(buffer, KM_BIO_SRC_IRQ); |
| 534 | local_irq_restore(flags); |
| 535 | |
| 536 | host->pio.sg_off += len; |
| 537 | host->curr.xfer_remain -= len; |
| 538 | host->curr.data_xfered += len; |
| 539 | remain -= len; |
| 540 | |
| 541 | if (remain == 0) { |
| 542 | /* This sg page is full - do some housekeeping */ |
| 543 | if (status & MCI_RXACTIVE && host->curr.user_pages) |
| 544 | flush_dcache_page(sg_page(host->pio.sg)); |
| 545 | |
| 546 | if (!--host->pio.sg_len) { |
| 547 | memset(&host->pio, 0, sizeof(host->pio)); |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | /* Advance to next sg */ |
| 552 | host->pio.sg++; |
| 553 | host->pio.sg_off = 0; |
| 554 | } |
| 555 | |
| 556 | status = readl(base + MMCISTATUS); |
| 557 | } while (1); |
| 558 | |
| 559 | if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE) |
| 560 | writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1); |
| 561 | |
| 562 | if (!host->curr.xfer_remain) |
| 563 | writel(0, base + MMCIMASK1); |
| 564 | |
| 565 | return IRQ_HANDLED; |
| 566 | } |
| 567 | |
| 568 | static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status) |
| 569 | { |
| 570 | struct mmc_command *cmd = host->curr.cmd; |
| 571 | void __iomem *base = host->base; |
| 572 | |
| 573 | host->curr.cmd = NULL; |
| 574 | cmd->resp[0] = readl(base + MMCIRESPONSE0); |
| 575 | cmd->resp[1] = readl(base + MMCIRESPONSE1); |
| 576 | cmd->resp[2] = readl(base + MMCIRESPONSE2); |
| 577 | cmd->resp[3] = readl(base + MMCIRESPONSE3); |
| 578 | |
| 579 | del_timer(&host->command_timer); |
| 580 | if (status & MCI_CMDTIMEOUT) { |
| 581 | cmd->error = -ETIMEDOUT; |
| 582 | } else if (status & MCI_CMDCRCFAIL && |
| 583 | cmd->flags & MMC_RSP_CRC) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 584 | pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 585 | cmd->error = -EILSEQ; |
| 586 | } |
| 587 | |
| 588 | if (!cmd->data || cmd->error) { |
| 589 | if (host->curr.data && host->dma.sg) |
| 590 | msm_dmov_stop_cmd(host->dma.channel, |
| 591 | &host->dma.hdr, 0); |
| 592 | else if (host->curr.data) { /* Non DMA */ |
| 593 | msmsdcc_stop_data(host); |
| 594 | msmsdcc_request_end(host, cmd->mrq); |
| 595 | } else /* host->data == NULL */ |
| 596 | msmsdcc_request_end(host, cmd->mrq); |
| 597 | } else if (!(cmd->data->flags & MMC_DATA_READ)) |
| 598 | msmsdcc_start_data(host, cmd->data); |
| 599 | } |
| 600 | |
Joe Perches | b5a74d6 | 2009-09-22 16:44:25 -0700 | [diff] [blame] | 601 | static void |
| 602 | msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status, |
| 603 | void __iomem *base) |
| 604 | { |
| 605 | struct mmc_data *data = host->curr.data; |
| 606 | |
| 607 | if (!data) |
| 608 | return; |
| 609 | |
| 610 | /* Check for data errors */ |
| 611 | if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT | |
| 612 | MCI_TXUNDERRUN | MCI_RXOVERRUN)) { |
| 613 | msmsdcc_data_err(host, data, status); |
| 614 | host->curr.data_xfered = 0; |
| 615 | if (host->dma.sg) |
| 616 | msm_dmov_stop_cmd(host->dma.channel, |
| 617 | &host->dma.hdr, 0); |
| 618 | else { |
| 619 | msmsdcc_stop_data(host); |
| 620 | if (!data->stop) |
| 621 | msmsdcc_request_end(host, data->mrq); |
| 622 | else |
| 623 | msmsdcc_start_command(host, data->stop, 0); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* Check for data done */ |
| 628 | if (!host->curr.got_dataend && (status & MCI_DATAEND)) |
| 629 | host->curr.got_dataend = 1; |
| 630 | |
| 631 | if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND)) |
| 632 | host->curr.got_datablkend = 1; |
| 633 | |
| 634 | /* |
| 635 | * If DMA is still in progress, we complete via the completion handler |
| 636 | */ |
| 637 | if (host->curr.got_dataend && host->curr.got_datablkend && |
| 638 | !host->dma.busy) { |
| 639 | /* |
| 640 | * There appears to be an issue in the controller where |
| 641 | * if you request a small block transfer (< fifo size), |
| 642 | * you may get your DATAEND/DATABLKEND irq without the |
| 643 | * PIO data irq. |
| 644 | * |
| 645 | * Check to see if there is still data to be read, |
| 646 | * and simulate a PIO irq. |
| 647 | */ |
| 648 | if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL) |
| 649 | msmsdcc_pio_irq(1, host); |
| 650 | |
| 651 | msmsdcc_stop_data(host); |
| 652 | if (!data->error) |
| 653 | host->curr.data_xfered = host->curr.xfer_size; |
| 654 | |
| 655 | if (!data->stop) |
| 656 | msmsdcc_request_end(host, data->mrq); |
| 657 | else |
| 658 | msmsdcc_start_command(host, data->stop, 0); |
| 659 | } |
| 660 | } |
| 661 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 662 | static irqreturn_t |
| 663 | msmsdcc_irq(int irq, void *dev_id) |
| 664 | { |
| 665 | struct msmsdcc_host *host = dev_id; |
| 666 | void __iomem *base = host->base; |
| 667 | u32 status; |
| 668 | int ret = 0; |
| 669 | int cardint = 0; |
| 670 | |
| 671 | spin_lock(&host->lock); |
| 672 | |
| 673 | do { |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 674 | status = readl(base + MMCISTATUS); |
| 675 | |
Joe Perches | b5a74d6 | 2009-09-22 16:44:25 -0700 | [diff] [blame] | 676 | status &= (readl(base + MMCIMASK0) | MCI_DATABLOCKENDMASK); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 677 | writel(status, base + MMCICLEAR); |
| 678 | |
Joe Perches | b5a74d6 | 2009-09-22 16:44:25 -0700 | [diff] [blame] | 679 | msmsdcc_handle_irq_data(host, status, base); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 680 | |
| 681 | if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL | |
| 682 | MCI_CMDTIMEOUT) && host->curr.cmd) { |
| 683 | msmsdcc_do_cmdirq(host, status); |
| 684 | } |
| 685 | |
| 686 | if (status & MCI_SDIOINTOPER) { |
| 687 | cardint = 1; |
| 688 | status &= ~MCI_SDIOINTOPER; |
| 689 | } |
| 690 | ret = 1; |
| 691 | } while (status); |
| 692 | |
| 693 | spin_unlock(&host->lock); |
| 694 | |
| 695 | /* |
| 696 | * We have to delay handling the card interrupt as it calls |
| 697 | * back into the driver. |
| 698 | */ |
| 699 | if (cardint) |
| 700 | mmc_signal_sdio_irq(host->mmc); |
| 701 | |
| 702 | return IRQ_RETVAL(ret); |
| 703 | } |
| 704 | |
| 705 | static void |
| 706 | msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 707 | { |
| 708 | struct msmsdcc_host *host = mmc_priv(mmc); |
| 709 | unsigned long flags; |
| 710 | |
| 711 | WARN_ON(host->curr.mrq != NULL); |
| 712 | WARN_ON(host->pwr == 0); |
| 713 | |
| 714 | spin_lock_irqsave(&host->lock, flags); |
| 715 | |
| 716 | host->stats.reqs++; |
| 717 | |
| 718 | if (host->eject) { |
| 719 | if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) { |
| 720 | mrq->cmd->error = 0; |
| 721 | mrq->data->bytes_xfered = mrq->data->blksz * |
| 722 | mrq->data->blocks; |
| 723 | } else |
| 724 | mrq->cmd->error = -ENOMEDIUM; |
| 725 | |
| 726 | spin_unlock_irqrestore(&host->lock, flags); |
| 727 | mmc_request_done(mmc, mrq); |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | host->curr.mrq = mrq; |
| 732 | |
| 733 | if (mrq->data && mrq->data->flags & MMC_DATA_READ) |
| 734 | msmsdcc_start_data(host, mrq->data); |
| 735 | |
| 736 | msmsdcc_start_command(host, mrq->cmd, 0); |
| 737 | |
| 738 | if (host->cmdpoll && !msmsdcc_spin_on_status(host, |
| 739 | MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT, |
| 740 | CMD_SPINMAX)) { |
| 741 | uint32_t status = readl(host->base + MMCISTATUS); |
| 742 | msmsdcc_do_cmdirq(host, status); |
| 743 | writel(MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT, |
| 744 | host->base + MMCICLEAR); |
| 745 | host->stats.cmdpoll_hits++; |
| 746 | } else { |
| 747 | host->stats.cmdpoll_misses++; |
| 748 | mod_timer(&host->command_timer, jiffies + HZ); |
| 749 | } |
| 750 | spin_unlock_irqrestore(&host->lock, flags); |
| 751 | } |
| 752 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 753 | static int inline |
| 754 | msmsdcc_enable_clocks(struct msmsdcc_host *host, int enable) |
| 755 | { |
| 756 | int rc; |
| 757 | if (enable) { |
| 758 | rc = clk_enable(host->pclk); |
| 759 | if (rc) |
| 760 | return rc; |
| 761 | rc = clk_enable(host->clk); |
| 762 | if (rc) { |
| 763 | clk_disable(host->pclk); |
| 764 | return rc; |
| 765 | } |
| 766 | host->clks_on = 1; |
| 767 | udelay(10); |
| 768 | } else { |
| 769 | clk_disable(host->clk); |
| 770 | clk_disable(host->pclk); |
| 771 | host->clks_on = 0; |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 776 | static void |
| 777 | msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 778 | { |
| 779 | struct msmsdcc_host *host = mmc_priv(mmc); |
| 780 | u32 clk = 0, pwr = 0; |
| 781 | int rc; |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 782 | unsigned long flags; |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 783 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 784 | spin_lock_irqsave(&host->lock, flags); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 785 | if (ios->clock) { |
| 786 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 787 | if (!host->clks_on) |
| 788 | msmsdcc_enable_clocks(host, 1); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 789 | if (ios->clock != host->clk_rate) { |
| 790 | rc = clk_set_rate(host->clk, ios->clock); |
| 791 | if (rc < 0) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 792 | pr_err("%s: Error setting clock rate (%d)\n", |
| 793 | mmc_hostname(host->mmc), rc); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 794 | else |
| 795 | host->clk_rate = ios->clock; |
| 796 | } |
| 797 | clk |= MCI_CLK_ENABLE; |
| 798 | } |
| 799 | |
| 800 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 801 | clk |= (2 << 10); /* Set WIDEBUS */ |
| 802 | |
| 803 | if (ios->clock > 400000 && msmsdcc_pwrsave) |
| 804 | clk |= (1 << 9); /* PWRSAVE */ |
| 805 | |
| 806 | clk |= (1 << 12); /* FLOW_ENA */ |
| 807 | clk |= (1 << 15); /* feedback clock */ |
| 808 | |
| 809 | if (host->plat->translate_vdd) |
| 810 | pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd); |
| 811 | |
| 812 | switch (ios->power_mode) { |
| 813 | case MMC_POWER_OFF: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 814 | break; |
| 815 | case MMC_POWER_UP: |
| 816 | pwr |= MCI_PWR_UP; |
| 817 | break; |
| 818 | case MMC_POWER_ON: |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 819 | pwr |= MCI_PWR_ON; |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) |
| 824 | pwr |= MCI_OD; |
| 825 | |
| 826 | writel(clk, host->base + MMCICLOCK); |
| 827 | |
| 828 | if (host->pwr != pwr) { |
| 829 | host->pwr = pwr; |
| 830 | writel(pwr, host->base + MMCIPOWER); |
| 831 | } |
| 832 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 833 | if (!(clk & MCI_CLK_ENABLE) && host->clks_on) |
| 834 | msmsdcc_enable_clocks(host, 0); |
| 835 | spin_unlock_irqrestore(&host->lock, flags); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 839 | { |
| 840 | struct msmsdcc_host *host = mmc_priv(mmc); |
| 841 | unsigned long flags; |
| 842 | u32 status; |
| 843 | |
| 844 | spin_lock_irqsave(&host->lock, flags); |
| 845 | if (msmsdcc_sdioirq == 1) { |
| 846 | status = readl(host->base + MMCIMASK0); |
| 847 | if (enable) |
| 848 | status |= MCI_SDIOINTOPERMASK; |
| 849 | else |
| 850 | status &= ~MCI_SDIOINTOPERMASK; |
| 851 | host->saved_irq0mask = status; |
| 852 | writel(status, host->base + MMCIMASK0); |
| 853 | } |
| 854 | spin_unlock_irqrestore(&host->lock, flags); |
| 855 | } |
| 856 | |
| 857 | static const struct mmc_host_ops msmsdcc_ops = { |
| 858 | .request = msmsdcc_request, |
| 859 | .set_ios = msmsdcc_set_ios, |
| 860 | .enable_sdio_irq = msmsdcc_enable_sdio_irq, |
| 861 | }; |
| 862 | |
| 863 | static void |
| 864 | msmsdcc_check_status(unsigned long data) |
| 865 | { |
| 866 | struct msmsdcc_host *host = (struct msmsdcc_host *)data; |
| 867 | unsigned int status; |
| 868 | |
| 869 | if (!host->plat->status) { |
| 870 | mmc_detect_change(host->mmc, 0); |
| 871 | goto out; |
| 872 | } |
| 873 | |
| 874 | status = host->plat->status(mmc_dev(host->mmc)); |
| 875 | host->eject = !status; |
| 876 | if (status ^ host->oldstat) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 877 | pr_info("%s: Slot status change detected (%d -> %d)\n", |
| 878 | mmc_hostname(host->mmc), host->oldstat, status); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 879 | if (status) |
| 880 | mmc_detect_change(host->mmc, (5 * HZ) / 2); |
| 881 | else |
| 882 | mmc_detect_change(host->mmc, 0); |
| 883 | } |
| 884 | |
| 885 | host->oldstat = status; |
| 886 | |
| 887 | out: |
| 888 | if (host->timer.function) |
| 889 | mod_timer(&host->timer, jiffies + HZ); |
| 890 | } |
| 891 | |
| 892 | static irqreturn_t |
| 893 | msmsdcc_platform_status_irq(int irq, void *dev_id) |
| 894 | { |
| 895 | struct msmsdcc_host *host = dev_id; |
| 896 | |
| 897 | printk(KERN_DEBUG "%s: %d\n", __func__, irq); |
| 898 | msmsdcc_check_status((unsigned long) host); |
| 899 | return IRQ_HANDLED; |
| 900 | } |
| 901 | |
| 902 | static void |
| 903 | msmsdcc_status_notify_cb(int card_present, void *dev_id) |
| 904 | { |
| 905 | struct msmsdcc_host *host = dev_id; |
| 906 | |
| 907 | printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc), |
| 908 | card_present); |
| 909 | msmsdcc_check_status((unsigned long) host); |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * called when a command expires. |
| 914 | * Dump some debugging, and then error |
| 915 | * out the transaction. |
| 916 | */ |
| 917 | static void |
| 918 | msmsdcc_command_expired(unsigned long _data) |
| 919 | { |
| 920 | struct msmsdcc_host *host = (struct msmsdcc_host *) _data; |
| 921 | struct mmc_request *mrq; |
| 922 | unsigned long flags; |
| 923 | |
| 924 | spin_lock_irqsave(&host->lock, flags); |
| 925 | mrq = host->curr.mrq; |
| 926 | |
| 927 | if (!mrq) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 928 | pr_info("%s: Command expiry misfire\n", |
| 929 | mmc_hostname(host->mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 930 | spin_unlock_irqrestore(&host->lock, flags); |
| 931 | return; |
| 932 | } |
| 933 | |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 934 | pr_err("%s: Command timeout (%p %p %p %p)\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 935 | mmc_hostname(host->mmc), mrq, mrq->cmd, |
| 936 | mrq->data, host->dma.sg); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 937 | mrq->cmd->error = -ETIMEDOUT; |
| 938 | msmsdcc_stop_data(host); |
| 939 | |
| 940 | writel(0, host->base + MMCICOMMAND); |
| 941 | |
| 942 | host->curr.mrq = NULL; |
| 943 | host->curr.cmd = NULL; |
| 944 | |
| 945 | spin_unlock_irqrestore(&host->lock, flags); |
| 946 | mmc_request_done(host->mmc, mrq); |
| 947 | } |
| 948 | |
| 949 | static int |
| 950 | msmsdcc_init_dma(struct msmsdcc_host *host) |
| 951 | { |
| 952 | memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data)); |
| 953 | host->dma.host = host; |
| 954 | host->dma.channel = -1; |
| 955 | |
| 956 | if (!host->dmares) |
| 957 | return -ENODEV; |
| 958 | |
| 959 | host->dma.nc = dma_alloc_coherent(NULL, |
| 960 | sizeof(struct msmsdcc_nc_dmadata), |
| 961 | &host->dma.nc_busaddr, |
| 962 | GFP_KERNEL); |
| 963 | if (host->dma.nc == NULL) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 964 | pr_err("Unable to allocate DMA buffer\n"); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 965 | return -ENOMEM; |
| 966 | } |
| 967 | memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata)); |
| 968 | host->dma.cmd_busaddr = host->dma.nc_busaddr; |
| 969 | host->dma.cmdptr_busaddr = host->dma.nc_busaddr + |
| 970 | offsetof(struct msmsdcc_nc_dmadata, cmdptr); |
| 971 | host->dma.channel = host->dmares->start; |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ |
| 977 | static void |
| 978 | do_resume_work(struct work_struct *work) |
| 979 | { |
| 980 | struct msmsdcc_host *host = |
| 981 | container_of(work, struct msmsdcc_host, resume_task); |
| 982 | struct mmc_host *mmc = host->mmc; |
| 983 | |
| 984 | if (mmc) { |
| 985 | mmc_resume_host(mmc); |
| 986 | if (host->stat_irq) |
| 987 | enable_irq(host->stat_irq); |
| 988 | } |
| 989 | } |
| 990 | #endif |
| 991 | |
| 992 | static int |
| 993 | msmsdcc_probe(struct platform_device *pdev) |
| 994 | { |
| 995 | struct mmc_platform_data *plat = pdev->dev.platform_data; |
| 996 | struct msmsdcc_host *host; |
| 997 | struct mmc_host *mmc; |
| 998 | struct resource *cmd_irqres = NULL; |
| 999 | struct resource *pio_irqres = NULL; |
| 1000 | struct resource *stat_irqres = NULL; |
| 1001 | struct resource *memres = NULL; |
| 1002 | struct resource *dmares = NULL; |
| 1003 | int ret; |
| 1004 | |
| 1005 | /* must have platform data */ |
| 1006 | if (!plat) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1007 | pr_err("%s: Platform data not available\n", __func__); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1008 | ret = -EINVAL; |
| 1009 | goto out; |
| 1010 | } |
| 1011 | |
| 1012 | if (pdev->id < 1 || pdev->id > 4) |
| 1013 | return -EINVAL; |
| 1014 | |
| 1015 | if (pdev->resource == NULL || pdev->num_resources < 2) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1016 | pr_err("%s: Invalid resource\n", __func__); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1017 | return -ENXIO; |
| 1018 | } |
| 1019 | |
| 1020 | memres = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1021 | dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1022 | cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1023 | "cmd_irq"); |
| 1024 | pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1025 | "pio_irq"); |
| 1026 | stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1027 | "status_irq"); |
| 1028 | |
| 1029 | if (!cmd_irqres || !pio_irqres || !memres) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1030 | pr_err("%s: Invalid resource\n", __func__); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1031 | return -ENXIO; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Setup our host structure |
| 1036 | */ |
| 1037 | |
| 1038 | mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev); |
| 1039 | if (!mmc) { |
| 1040 | ret = -ENOMEM; |
| 1041 | goto out; |
| 1042 | } |
| 1043 | |
| 1044 | host = mmc_priv(mmc); |
| 1045 | host->pdev_id = pdev->id; |
| 1046 | host->plat = plat; |
| 1047 | host->mmc = mmc; |
| 1048 | |
| 1049 | host->cmdpoll = 1; |
| 1050 | |
| 1051 | host->base = ioremap(memres->start, PAGE_SIZE); |
| 1052 | if (!host->base) { |
| 1053 | ret = -ENOMEM; |
| 1054 | goto out; |
| 1055 | } |
| 1056 | |
| 1057 | host->cmd_irqres = cmd_irqres; |
| 1058 | host->pio_irqres = pio_irqres; |
| 1059 | host->memres = memres; |
| 1060 | host->dmares = dmares; |
| 1061 | spin_lock_init(&host->lock); |
| 1062 | |
| 1063 | /* |
| 1064 | * Setup DMA |
| 1065 | */ |
| 1066 | msmsdcc_init_dma(host); |
| 1067 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1068 | /* Get our clocks */ |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1069 | host->pclk = clk_get(&pdev->dev, "sdc_pclk"); |
| 1070 | if (IS_ERR(host->pclk)) { |
| 1071 | ret = PTR_ERR(host->pclk); |
| 1072 | goto host_free; |
| 1073 | } |
| 1074 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1075 | host->clk = clk_get(&pdev->dev, "sdc_clk"); |
| 1076 | if (IS_ERR(host->clk)) { |
| 1077 | ret = PTR_ERR(host->clk); |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1078 | goto pclk_put; |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1081 | /* Enable clocks */ |
| 1082 | ret = msmsdcc_enable_clocks(host, 1); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1083 | if (ret) |
| 1084 | goto clk_put; |
| 1085 | |
| 1086 | ret = clk_set_rate(host->clk, msmsdcc_fmin); |
| 1087 | if (ret) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1088 | pr_err("%s: Clock rate set failed (%d)\n", __func__, ret); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1089 | goto clk_disable; |
| 1090 | } |
| 1091 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1092 | host->pclk_rate = clk_get_rate(host->pclk); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1093 | host->clk_rate = clk_get_rate(host->clk); |
| 1094 | |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * Setup MMC host structure |
| 1097 | */ |
| 1098 | mmc->ops = &msmsdcc_ops; |
| 1099 | mmc->f_min = msmsdcc_fmin; |
| 1100 | mmc->f_max = msmsdcc_fmax; |
| 1101 | mmc->ocr_avail = plat->ocr_mask; |
| 1102 | |
| 1103 | if (msmsdcc_4bit) |
| 1104 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 1105 | if (msmsdcc_sdioirq) |
| 1106 | mmc->caps |= MMC_CAP_SDIO_IRQ; |
| 1107 | mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED; |
| 1108 | |
| 1109 | mmc->max_phys_segs = NR_SG; |
| 1110 | mmc->max_hw_segs = NR_SG; |
| 1111 | mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */ |
| 1112 | mmc->max_blk_count = 65536; |
| 1113 | |
| 1114 | mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */ |
| 1115 | mmc->max_seg_size = mmc->max_req_size; |
| 1116 | |
| 1117 | writel(0, host->base + MMCIMASK0); |
| 1118 | writel(0x5e007ff, host->base + MMCICLEAR); /* Add: 1 << 25 */ |
| 1119 | |
| 1120 | writel(MCI_IRQENABLE, host->base + MMCIMASK0); |
| 1121 | host->saved_irq0mask = MCI_IRQENABLE; |
| 1122 | |
| 1123 | /* |
| 1124 | * Setup card detect change |
| 1125 | */ |
| 1126 | |
| 1127 | memset(&host->timer, 0, sizeof(host->timer)); |
| 1128 | |
| 1129 | if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) { |
| 1130 | unsigned long irqflags = IRQF_SHARED | |
| 1131 | (stat_irqres->flags & IRQF_TRIGGER_MASK); |
| 1132 | |
| 1133 | host->stat_irq = stat_irqres->start; |
| 1134 | ret = request_irq(host->stat_irq, |
| 1135 | msmsdcc_platform_status_irq, |
| 1136 | irqflags, |
| 1137 | DRIVER_NAME " (slot)", |
| 1138 | host); |
| 1139 | if (ret) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1140 | pr_err("%s: Unable to get slot IRQ %d (%d)\n", |
| 1141 | mmc_hostname(mmc), host->stat_irq, ret); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1142 | goto clk_disable; |
| 1143 | } |
| 1144 | } else if (plat->register_status_notify) { |
| 1145 | plat->register_status_notify(msmsdcc_status_notify_cb, host); |
| 1146 | } else if (!plat->status) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1147 | pr_err("%s: No card detect facilities available\n", |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1148 | mmc_hostname(mmc)); |
| 1149 | else { |
| 1150 | init_timer(&host->timer); |
| 1151 | host->timer.data = (unsigned long)host; |
| 1152 | host->timer.function = msmsdcc_check_status; |
| 1153 | host->timer.expires = jiffies + HZ; |
| 1154 | add_timer(&host->timer); |
| 1155 | } |
| 1156 | |
| 1157 | if (plat->status) { |
| 1158 | host->oldstat = host->plat->status(mmc_dev(host->mmc)); |
| 1159 | host->eject = !host->oldstat; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Setup a command timer. We currently need this due to |
| 1164 | * some 'strange' timeout / error handling situations. |
| 1165 | */ |
| 1166 | init_timer(&host->command_timer); |
| 1167 | host->command_timer.data = (unsigned long) host; |
| 1168 | host->command_timer.function = msmsdcc_command_expired; |
| 1169 | |
| 1170 | ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED, |
| 1171 | DRIVER_NAME " (cmd)", host); |
| 1172 | if (ret) |
| 1173 | goto stat_irq_free; |
| 1174 | |
| 1175 | ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED, |
| 1176 | DRIVER_NAME " (pio)", host); |
| 1177 | if (ret) |
| 1178 | goto cmd_irq_free; |
| 1179 | |
| 1180 | mmc_set_drvdata(pdev, mmc); |
| 1181 | mmc_add_host(mmc); |
| 1182 | |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1183 | pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n", |
| 1184 | mmc_hostname(mmc), (unsigned long long)memres->start, |
| 1185 | (unsigned int) cmd_irqres->start, |
| 1186 | (unsigned int) host->stat_irq, host->dma.channel); |
| 1187 | pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc), |
| 1188 | (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled")); |
| 1189 | pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n", |
| 1190 | mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate); |
| 1191 | pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject); |
| 1192 | pr_info("%s: Power save feature enable = %d\n", |
| 1193 | mmc_hostname(mmc), msmsdcc_pwrsave); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1194 | |
| 1195 | if (host->dma.channel != -1) { |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1196 | pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n", |
| 1197 | mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr); |
| 1198 | pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n", |
| 1199 | mmc_hostname(mmc), host->dma.cmd_busaddr, |
| 1200 | host->dma.cmdptr_busaddr); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1201 | } else |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1202 | pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1203 | if (host->timer.function) |
Joe Perches | 0a7ff7c | 2009-09-22 16:44:23 -0700 | [diff] [blame] | 1204 | pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc)); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1205 | |
| 1206 | return 0; |
| 1207 | cmd_irq_free: |
| 1208 | free_irq(cmd_irqres->start, host); |
| 1209 | stat_irq_free: |
| 1210 | if (host->stat_irq) |
| 1211 | free_irq(host->stat_irq, host); |
| 1212 | clk_disable: |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1213 | msmsdcc_enable_clocks(host, 0); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1214 | clk_put: |
| 1215 | clk_put(host->clk); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1216 | pclk_put: |
| 1217 | clk_put(host->pclk); |
| 1218 | host_free: |
| 1219 | mmc_free_host(mmc); |
| 1220 | out: |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
| 1224 | static int |
| 1225 | msmsdcc_suspend(struct platform_device *dev, pm_message_t state) |
| 1226 | { |
| 1227 | struct mmc_host *mmc = mmc_get_drvdata(dev); |
| 1228 | int rc = 0; |
| 1229 | |
| 1230 | if (mmc) { |
| 1231 | struct msmsdcc_host *host = mmc_priv(mmc); |
| 1232 | |
| 1233 | if (host->stat_irq) |
| 1234 | disable_irq(host->stat_irq); |
| 1235 | |
| 1236 | if (mmc->card && mmc->card->type != MMC_TYPE_SDIO) |
| 1237 | rc = mmc_suspend_host(mmc, state); |
| 1238 | if (!rc) { |
| 1239 | writel(0, host->base + MMCIMASK0); |
| 1240 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1241 | if (host->clks_on) |
| 1242 | msmsdcc_enable_clocks(host, 0); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | return rc; |
| 1246 | } |
| 1247 | |
| 1248 | static int |
| 1249 | msmsdcc_resume(struct platform_device *dev) |
| 1250 | { |
| 1251 | struct mmc_host *mmc = mmc_get_drvdata(dev); |
| 1252 | unsigned long flags; |
| 1253 | |
| 1254 | if (mmc) { |
| 1255 | struct msmsdcc_host *host = mmc_priv(mmc); |
| 1256 | |
| 1257 | spin_lock_irqsave(&host->lock, flags); |
| 1258 | |
San Mehat | 4adbbcc | 2009-11-08 13:00:37 -0800 | [diff] [blame] | 1259 | if (!host->clks_on) |
| 1260 | msmsdcc_enable_clocks(host, 1); |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1261 | |
| 1262 | writel(host->saved_irq0mask, host->base + MMCIMASK0); |
| 1263 | |
| 1264 | spin_unlock_irqrestore(&host->lock, flags); |
| 1265 | |
| 1266 | if (mmc->card && mmc->card->type != MMC_TYPE_SDIO) |
| 1267 | mmc_resume_host(mmc); |
Roel Kluin | 5b8a2fb | 2010-01-17 20:25:36 +0100 | [diff] [blame] | 1268 | if (host->stat_irq) |
San Mehat | 9d2bd73 | 2009-09-22 16:44:22 -0700 | [diff] [blame] | 1269 | enable_irq(host->stat_irq); |
| 1270 | } |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static struct platform_driver msmsdcc_driver = { |
| 1275 | .probe = msmsdcc_probe, |
| 1276 | .suspend = msmsdcc_suspend, |
| 1277 | .resume = msmsdcc_resume, |
| 1278 | .driver = { |
| 1279 | .name = "msm_sdcc", |
| 1280 | }, |
| 1281 | }; |
| 1282 | |
| 1283 | static int __init msmsdcc_init(void) |
| 1284 | { |
| 1285 | return platform_driver_register(&msmsdcc_driver); |
| 1286 | } |
| 1287 | |
| 1288 | static void __exit msmsdcc_exit(void) |
| 1289 | { |
| 1290 | platform_driver_unregister(&msmsdcc_driver); |
| 1291 | } |
| 1292 | |
| 1293 | module_init(msmsdcc_init); |
| 1294 | module_exit(msmsdcc_exit); |
| 1295 | |
| 1296 | MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver"); |
| 1297 | MODULE_LICENSE("GPL"); |