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