blob: 81ed16fb42b5c11d7a01548a2a239c0702df9be5 [file] [log] [blame]
San Mehat9d2bd732009-09-22 16:44:22 -07001/*
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 Mehat56a8b5b2009-11-21 12:29:46 -08006 * Copyright (C) 2009, Code Aurora Forum. All Rights Reserved.
San Mehat9d2bd732009-09-22 16:44:22 -07007 *
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 Mehatb3fa5792009-11-02 18:46:09 -080030#include <linux/mmc/sdio.h>
San Mehat9d2bd732009-09-22 16:44:22 -070031#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 Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
San Mehat9d2bd732009-09-22 16:44:22 -070039
40#include <asm/cacheflush.h>
41#include <asm/div64.h>
42#include <asm/sizes.h>
43
Pavel Machek3989d172009-12-08 11:11:36 -080044#include <mach/mmc.h>
San Mehat9d2bd732009-09-22 16:44:22 -070045#include <mach/msm_iomap.h>
46#include <mach/dma.h>
Sahitya Tummalab08bb352010-12-08 15:03:05 +053047#include <mach/clk.h>
San Mehat9d2bd732009-09-22 16:44:22 -070048
San Mehat9d2bd732009-09-22 16:44:22 -070049#include "msm_sdcc.h"
50
51#define DRIVER_NAME "msm-sdcc"
52
San Mehat24bbd7d2009-12-01 10:10:47 -080053#define BUSCLK_PWRSAVE 1
San Mehatc7fc9372009-11-22 17:19:07 -080054#define BUSCLK_TIMEOUT (HZ)
San Mehat9d2bd732009-09-22 16:44:22 -070055static unsigned int msmsdcc_fmin = 144000;
56static unsigned int msmsdcc_fmax = 50000000;
57static unsigned int msmsdcc_4bit = 1;
58static unsigned int msmsdcc_pwrsave = 1;
59static unsigned int msmsdcc_piopoll = 1;
60static unsigned int msmsdcc_sdioirq;
61
62#define PIO_SPINMAX 30
63#define CMD_SPINMAX 20
64
San Mehat865c80642009-11-13 13:42:06 -080065
San Mehatd0719e52009-12-03 10:58:54 -080066static inline void
San Mehatc7fc9372009-11-22 17:19:07 -080067msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
San Mehat865c80642009-11-13 13:42:06 -080068{
San Mehatc7fc9372009-11-22 17:19:07 -080069 WARN_ON(!host->clks_on);
San Mehat8b1c2ba2009-11-16 10:17:30 -080070
San Mehatf4748492009-11-23 15:36:31 -080071 BUG_ON(host->curr.mrq);
72
San Mehatc7fc9372009-11-22 17:19:07 -080073 if (deferr) {
74 mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
San Mehat865c80642009-11-13 13:42:06 -080075 } else {
San Mehatc7fc9372009-11-22 17:19:07 -080076 del_timer_sync(&host->busclk_timer);
San Mehatd0719e52009-12-03 10:58:54 -080077 /* Need to check clks_on again in case the busclk
78 * timer fired
79 */
80 if (host->clks_on) {
81 clk_disable(host->clk);
82 clk_disable(host->pclk);
83 host->clks_on = 0;
84 }
San Mehat865c80642009-11-13 13:42:06 -080085 }
San Mehatc7fc9372009-11-22 17:19:07 -080086}
87
88static inline int
89msmsdcc_enable_clocks(struct msmsdcc_host *host)
90{
91 int rc;
92
San Mehatc7fc9372009-11-22 17:19:07 -080093 del_timer_sync(&host->busclk_timer);
94
San Mehatd0719e52009-12-03 10:58:54 -080095 if (!host->clks_on) {
96 rc = clk_enable(host->pclk);
97 if (rc)
98 return rc;
99 rc = clk_enable(host->clk);
100 if (rc) {
101 clk_disable(host->pclk);
102 return rc;
103 }
104 udelay(1 + ((3 * USEC_PER_SEC) /
105 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
106 host->clks_on = 1;
San Mehatc7fc9372009-11-22 17:19:07 -0800107 }
San Mehat865c80642009-11-13 13:42:06 -0800108 return 0;
109}
110
San Mehat8b1c2ba2009-11-16 10:17:30 -0800111static inline unsigned int
112msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
113{
114 return readl(host->base + reg);
115}
116
117static inline void
118msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
119{
120 writel(data, host->base + reg);
121 /* 3 clk delay required! */
122 udelay(1 + ((3 * USEC_PER_SEC) /
123 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
124}
San Mehat865c80642009-11-13 13:42:06 -0800125
San Mehat9d2bd732009-09-22 16:44:22 -0700126static void
127msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
128 u32 c);
129
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530130static void msmsdcc_reset_and_restore(struct msmsdcc_host *host)
131{
132 u32 mci_clk = 0;
133 u32 mci_mask0 = 0;
134 int ret = 0;
135
136 /* Save the controller state */
137 mci_clk = readl(host->base + MMCICLOCK);
138 mci_mask0 = readl(host->base + MMCIMASK0);
139
140 /* Reset the controller */
141 ret = clk_reset(host->clk, CLK_RESET_ASSERT);
142 if (ret)
143 pr_err("%s: Clock assert failed at %u Hz with err %d\n",
144 mmc_hostname(host->mmc), host->clk_rate, ret);
145
146 ret = clk_reset(host->clk, CLK_RESET_DEASSERT);
147 if (ret)
148 pr_err("%s: Clock deassert failed at %u Hz with err %d\n",
149 mmc_hostname(host->mmc), host->clk_rate, ret);
150
151 pr_info("%s: Controller has been re-initialiazed\n",
152 mmc_hostname(host->mmc));
153
154 /* Restore the contoller state */
155 writel(host->pwr, host->base + MMCIPOWER);
156 writel(mci_clk, host->base + MMCICLOCK);
157 writel(mci_mask0, host->base + MMCIMASK0);
158 ret = clk_set_rate(host->clk, host->clk_rate);
159 if (ret)
160 pr_err("%s: Failed to set clk rate %u Hz (%d)\n",
161 mmc_hostname(host->mmc), host->clk_rate, ret);
162}
163
San Mehat9d2bd732009-09-22 16:44:22 -0700164static void
165msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
166{
San Mehat9d2bd732009-09-22 16:44:22 -0700167 BUG_ON(host->curr.data);
168
169 host->curr.mrq = NULL;
170 host->curr.cmd = NULL;
171
172 if (mrq->data)
173 mrq->data->bytes_xfered = host->curr.data_xfered;
174 if (mrq->cmd->error == -ETIMEDOUT)
175 mdelay(5);
176
San Mehatf4748492009-11-23 15:36:31 -0800177#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800178 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800179#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700180 /*
181 * Need to drop the host lock here; mmc_request_done may call
182 * back into the driver...
183 */
184 spin_unlock(&host->lock);
185 mmc_request_done(host->mmc, mrq);
186 spin_lock(&host->lock);
187}
188
189static void
190msmsdcc_stop_data(struct msmsdcc_host *host)
191{
San Mehat9d2bd732009-09-22 16:44:22 -0700192 host->curr.data = NULL;
193 host->curr.got_dataend = host->curr.got_datablkend = 0;
194}
195
196uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
197{
Sahitya Tummalaedd4dd02010-07-29 16:57:41 +0530198 return host->memres->start + MMCIFIFO;
San Mehat9d2bd732009-09-22 16:44:22 -0700199}
200
San Mehat56a8b5b2009-11-21 12:29:46 -0800201static inline void
202msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
203 msmsdcc_writel(host, arg, MMCIARGUMENT);
204 msmsdcc_writel(host, c, MMCICOMMAND);
205}
206
207static void
208msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
209{
San Mehat6ac9ea62009-12-02 17:24:58 -0800210 struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
San Mehat56a8b5b2009-11-21 12:29:46 -0800211
San Mehat6ac9ea62009-12-02 17:24:58 -0800212 msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
San Mehatd0719e52009-12-03 10:58:54 -0800213 msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
214 MMCIDATALENGTH);
San Mehat6ac9ea62009-12-02 17:24:58 -0800215 msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
216 msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
San Mehat56a8b5b2009-11-21 12:29:46 -0800217
San Mehat6ac9ea62009-12-02 17:24:58 -0800218 if (host->cmd_cmd) {
219 msmsdcc_start_command_exec(host,
220 (u32) host->cmd_cmd->arg,
221 (u32) host->cmd_c);
222 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800223 host->dma.active = 1;
224}
225
San Mehat9d2bd732009-09-22 16:44:22 -0700226static void
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530227msmsdcc_dma_complete_tlet(unsigned long data)
San Mehat9d2bd732009-09-22 16:44:22 -0700228{
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530229 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
San Mehat9d2bd732009-09-22 16:44:22 -0700230 unsigned long flags;
231 struct mmc_request *mrq;
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530232 struct msm_dmov_errdata err;
San Mehat9d2bd732009-09-22 16:44:22 -0700233
234 spin_lock_irqsave(&host->lock, flags);
San Mehat56a8b5b2009-11-21 12:29:46 -0800235 host->dma.active = 0;
236
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530237 err = host->dma.err;
San Mehat9d2bd732009-09-22 16:44:22 -0700238 mrq = host->curr.mrq;
239 BUG_ON(!mrq);
San Mehatb3b0ca82009-11-24 12:24:55 -0800240 WARN_ON(!mrq->data);
San Mehat9d2bd732009-09-22 16:44:22 -0700241
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530242 if (!(host->dma.result & DMOV_RSLT_VALID)) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700243 pr_err("msmsdcc: Invalid DataMover result\n");
San Mehat9d2bd732009-09-22 16:44:22 -0700244 goto out;
245 }
246
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530247 if (host->dma.result & DMOV_RSLT_DONE) {
San Mehat9d2bd732009-09-22 16:44:22 -0700248 host->curr.data_xfered = host->curr.xfer_size;
249 } else {
250 /* Error or flush */
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530251 if (host->dma.result & DMOV_RSLT_ERROR)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700252 pr_err("%s: DMA error (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530253 mmc_hostname(host->mmc), host->dma.result);
254 if (host->dma.result & DMOV_RSLT_FLUSH)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700255 pr_err("%s: DMA channel flushed (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530256 mmc_hostname(host->mmc), host->dma.result);
257
258 pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
259 err.flush[0], err.flush[1], err.flush[2],
260 err.flush[3], err.flush[4], err.flush[5]);
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530261
262 msmsdcc_reset_and_restore(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700263 if (!mrq->data->error)
264 mrq->data->error = -EIO;
265 }
San Mehat9d2bd732009-09-22 16:44:22 -0700266 dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents,
267 host->dma.dir);
268
269 if (host->curr.user_pages) {
270 struct scatterlist *sg = host->dma.sg;
271 int i;
272
Joe Perches75d14522009-09-22 16:44:24 -0700273 for (i = 0; i < host->dma.num_ents; i++)
274 flush_dcache_page(sg_page(sg++));
San Mehat9d2bd732009-09-22 16:44:22 -0700275 }
276
277 host->dma.sg = NULL;
San Mehat56a8b5b2009-11-21 12:29:46 -0800278 host->dma.busy = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700279
280 if ((host->curr.got_dataend && host->curr.got_datablkend)
281 || mrq->data->error) {
282
283 /*
284 * If we've already gotten our DATAEND / DATABLKEND
285 * for this request, then complete it through here.
286 */
287 msmsdcc_stop_data(host);
288
289 if (!mrq->data->error)
290 host->curr.data_xfered = host->curr.xfer_size;
291 if (!mrq->data->stop || mrq->cmd->error) {
San Mehat9d2bd732009-09-22 16:44:22 -0700292 host->curr.mrq = NULL;
293 host->curr.cmd = NULL;
294 mrq->data->bytes_xfered = host->curr.data_xfered;
295
296 spin_unlock_irqrestore(&host->lock, flags);
San Mehatf4748492009-11-23 15:36:31 -0800297#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800298 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800299#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700300 mmc_request_done(host->mmc, mrq);
301 return;
302 } else
303 msmsdcc_start_command(host, mrq->data->stop, 0);
304 }
305
306out:
307 spin_unlock_irqrestore(&host->lock, flags);
308 return;
309}
310
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530311static void
312msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
313 unsigned int result,
314 struct msm_dmov_errdata *err)
315{
316 struct msmsdcc_dma_data *dma_data =
317 container_of(cmd, struct msmsdcc_dma_data, hdr);
318 struct msmsdcc_host *host = dma_data->host;
319
320 dma_data->result = result;
321 if (err)
322 memcpy(&dma_data->err, err, sizeof(struct msm_dmov_errdata));
323
324 tasklet_schedule(&host->dma_tlet);
325}
326
San Mehat9d2bd732009-09-22 16:44:22 -0700327static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data)
328{
329 if (host->dma.channel == -1)
330 return -ENOENT;
331
332 if ((data->blksz * data->blocks) < MCI_FIFOSIZE)
333 return -EINVAL;
334 if ((data->blksz * data->blocks) % MCI_FIFOSIZE)
335 return -EINVAL;
336 return 0;
337}
338
339static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
340{
341 struct msmsdcc_nc_dmadata *nc;
342 dmov_box *box;
343 uint32_t rows;
344 uint32_t crci;
345 unsigned int n;
346 int i, rc;
347 struct scatterlist *sg = data->sg;
348
349 rc = validate_dma(host, data);
350 if (rc)
351 return rc;
352
353 host->dma.sg = data->sg;
354 host->dma.num_ents = data->sg_len;
355
San Mehat56a8b5b2009-11-21 12:29:46 -0800356 BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
357
San Mehat9d2bd732009-09-22 16:44:22 -0700358 nc = host->dma.nc;
359
Joe Perches75d14522009-09-22 16:44:24 -0700360 switch (host->pdev_id) {
361 case 1:
San Mehat9d2bd732009-09-22 16:44:22 -0700362 crci = MSMSDCC_CRCI_SDC1;
Joe Perches75d14522009-09-22 16:44:24 -0700363 break;
364 case 2:
San Mehat9d2bd732009-09-22 16:44:22 -0700365 crci = MSMSDCC_CRCI_SDC2;
Joe Perches75d14522009-09-22 16:44:24 -0700366 break;
367 case 3:
San Mehat9d2bd732009-09-22 16:44:22 -0700368 crci = MSMSDCC_CRCI_SDC3;
Joe Perches75d14522009-09-22 16:44:24 -0700369 break;
370 case 4:
San Mehat9d2bd732009-09-22 16:44:22 -0700371 crci = MSMSDCC_CRCI_SDC4;
Joe Perches75d14522009-09-22 16:44:24 -0700372 break;
373 default:
San Mehat9d2bd732009-09-22 16:44:22 -0700374 host->dma.sg = NULL;
375 host->dma.num_ents = 0;
376 return -ENOENT;
377 }
378
379 if (data->flags & MMC_DATA_READ)
380 host->dma.dir = DMA_FROM_DEVICE;
381 else
382 host->dma.dir = DMA_TO_DEVICE;
383
384 host->curr.user_pages = 0;
385
San Mehat9d2bd732009-09-22 16:44:22 -0700386 box = &nc->cmd[0];
387 for (i = 0; i < host->dma.num_ents; i++) {
388 box->cmd = CMD_MODE_BOX;
389
San Mehat56a8b5b2009-11-21 12:29:46 -0800390 /* Initialize sg dma address */
391 sg->dma_address = page_to_dma(mmc_dev(host->mmc), sg_page(sg))
392 + sg->offset;
393
394 if (i == (host->dma.num_ents - 1))
San Mehat9d2bd732009-09-22 16:44:22 -0700395 box->cmd |= CMD_LC;
396 rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ?
397 (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 :
398 (sg_dma_len(sg) / MCI_FIFOSIZE) ;
399
400 if (data->flags & MMC_DATA_READ) {
401 box->src_row_addr = msmsdcc_fifo_addr(host);
402 box->dst_row_addr = sg_dma_address(sg);
403
404 box->src_dst_len = (MCI_FIFOSIZE << 16) |
405 (MCI_FIFOSIZE);
406 box->row_offset = MCI_FIFOSIZE;
407
408 box->num_rows = rows * ((1 << 16) + 1);
409 box->cmd |= CMD_SRC_CRCI(crci);
410 } else {
411 box->src_row_addr = sg_dma_address(sg);
412 box->dst_row_addr = msmsdcc_fifo_addr(host);
413
414 box->src_dst_len = (MCI_FIFOSIZE << 16) |
415 (MCI_FIFOSIZE);
416 box->row_offset = (MCI_FIFOSIZE << 16);
417
418 box->num_rows = rows * ((1 << 16) + 1);
419 box->cmd |= CMD_DST_CRCI(crci);
420 }
421 box++;
422 sg++;
423 }
424
425 /* location of command block must be 64 bit aligned */
426 BUG_ON(host->dma.cmd_busaddr & 0x07);
427
428 nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
429 host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
430 DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
431 host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
432
San Mehat56a8b5b2009-11-21 12:29:46 -0800433 n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg,
434 host->dma.num_ents, host->dma.dir);
435/* dsb inside dma_map_sg will write nc out to mem as well */
436
437 if (n != host->dma.num_ents) {
438 printk(KERN_ERR "%s: Unable to map in all sg elements\n",
439 mmc_hostname(host->mmc));
440 host->dma.sg = NULL;
441 host->dma.num_ents = 0;
442 return -ENOMEM;
443 }
San Mehat9d2bd732009-09-22 16:44:22 -0700444
445 return 0;
446}
447
San Mehat56a8b5b2009-11-21 12:29:46 -0800448static int
449snoop_cccr_abort(struct mmc_command *cmd)
450{
451 if ((cmd->opcode == 52) &&
452 (cmd->arg & 0x80000000) &&
453 (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT))
454 return 1;
San Mehat9d2bd732009-09-22 16:44:22 -0700455 return 0;
456}
457
458static void
San Mehat56a8b5b2009-11-21 12:29:46 -0800459msmsdcc_start_command_deferred(struct msmsdcc_host *host,
460 struct mmc_command *cmd, u32 *c)
461{
462 *c |= (cmd->opcode | MCI_CPSM_ENABLE);
463
464 if (cmd->flags & MMC_RSP_PRESENT) {
465 if (cmd->flags & MMC_RSP_136)
466 *c |= MCI_CPSM_LONGRSP;
467 *c |= MCI_CPSM_RESPONSE;
468 }
469
470 if (/*interrupt*/0)
471 *c |= MCI_CPSM_INTERRUPT;
472
473 if ((((cmd->opcode == 17) || (cmd->opcode == 18)) ||
474 ((cmd->opcode == 24) || (cmd->opcode == 25))) ||
475 (cmd->opcode == 53))
476 *c |= MCI_CSPM_DATCMD;
477
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530478 if (host->prog_scan && (cmd->opcode == 12)) {
479 *c |= MCI_CPSM_PROGENA;
480 host->prog_enable = true;
481 }
482
San Mehat56a8b5b2009-11-21 12:29:46 -0800483 if (cmd == cmd->mrq->stop)
484 *c |= MCI_CSPM_MCIABORT;
485
486 if (snoop_cccr_abort(cmd))
487 *c |= MCI_CSPM_MCIABORT;
488
489 if (host->curr.cmd != NULL) {
490 printk(KERN_ERR "%s: Overlapping command requests\n",
491 mmc_hostname(host->mmc));
492 }
493 host->curr.cmd = cmd;
494}
495
496static void
497msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
498 struct mmc_command *cmd, u32 c)
San Mehat9d2bd732009-09-22 16:44:22 -0700499{
500 unsigned int datactrl, timeout;
501 unsigned long long clks;
San Mehat9d2bd732009-09-22 16:44:22 -0700502 unsigned int pio_irqmask = 0;
503
504 host->curr.data = data;
505 host->curr.xfer_size = data->blksz * data->blocks;
506 host->curr.xfer_remain = host->curr.xfer_size;
507 host->curr.data_xfered = 0;
508 host->curr.got_dataend = 0;
509 host->curr.got_datablkend = 0;
510
511 memset(&host->pio, 0, sizeof(host->pio));
512
San Mehat9d2bd732009-09-22 16:44:22 -0700513 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
514
515 if (!msmsdcc_config_dma(host, data))
516 datactrl |= MCI_DPSM_DMAENABLE;
517 else {
518 host->pio.sg = data->sg;
519 host->pio.sg_len = data->sg_len;
520 host->pio.sg_off = 0;
521
522 if (data->flags & MMC_DATA_READ) {
523 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
524 if (host->curr.xfer_remain < MCI_FIFOSIZE)
525 pio_irqmask |= MCI_RXDATAAVLBLMASK;
526 } else
527 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
528 }
529
530 if (data->flags & MMC_DATA_READ)
531 datactrl |= MCI_DPSM_DIRECTION;
532
San Mehat56a8b5b2009-11-21 12:29:46 -0800533 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
534 do_div(clks, NSEC_PER_SEC);
535 timeout = data->timeout_clks + (unsigned int)clks*2 ;
San Mehat9d2bd732009-09-22 16:44:22 -0700536
537 if (datactrl & MCI_DPSM_DMAENABLE) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800538 /* Save parameters for the exec function */
539 host->cmd_timeout = timeout;
540 host->cmd_pio_irqmask = pio_irqmask;
541 host->cmd_datactrl = datactrl;
542 host->cmd_cmd = cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700543
San Mehat56a8b5b2009-11-21 12:29:46 -0800544 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
545 host->dma.hdr.data = (void *)host;
San Mehat9d2bd732009-09-22 16:44:22 -0700546 host->dma.busy = 1;
San Mehat56a8b5b2009-11-21 12:29:46 -0800547
548 if (cmd) {
549 msmsdcc_start_command_deferred(host, cmd, &c);
550 host->cmd_c = c;
551 }
San Mehat9d2bd732009-09-22 16:44:22 -0700552 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530553 if (data->flags & MMC_DATA_WRITE)
554 host->prog_scan = true;
San Mehat56a8b5b2009-11-21 12:29:46 -0800555 } else {
556 msmsdcc_writel(host, timeout, MMCIDATATIMER);
557
558 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
559
560 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
561 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
562
563 if (cmd) {
564 /* Daisy-chain the command if requested */
565 msmsdcc_start_command(host, cmd, c);
566 }
San Mehat9d2bd732009-09-22 16:44:22 -0700567 }
568}
569
570static void
571msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
572{
San Mehat9d2bd732009-09-22 16:44:22 -0700573 if (cmd == cmd->mrq->stop)
574 c |= MCI_CSPM_MCIABORT;
575
San Mehat9d2bd732009-09-22 16:44:22 -0700576 host->stats.cmds++;
577
San Mehat56a8b5b2009-11-21 12:29:46 -0800578 msmsdcc_start_command_deferred(host, cmd, &c);
579 msmsdcc_start_command_exec(host, cmd->arg, c);
San Mehat9d2bd732009-09-22 16:44:22 -0700580}
581
582static void
583msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
584 unsigned int status)
585{
586 if (status & MCI_DATACRCFAIL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700587 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
588 pr_err("%s: opcode 0x%.8x\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700589 data->mrq->cmd->opcode);
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700590 pr_err("%s: blksz %d, blocks %d\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700591 data->blksz, data->blocks);
592 data->error = -EILSEQ;
593 } else if (status & MCI_DATATIMEOUT) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700594 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700595 data->error = -ETIMEDOUT;
596 } else if (status & MCI_RXOVERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700597 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700598 data->error = -EIO;
599 } else if (status & MCI_TXUNDERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700600 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700601 data->error = -EIO;
602 } else {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700603 pr_err("%s: Unknown error (0x%.8x)\n",
604 mmc_hostname(host->mmc), status);
San Mehat9d2bd732009-09-22 16:44:22 -0700605 data->error = -EIO;
606 }
607}
608
609
610static int
611msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
612{
San Mehat9d2bd732009-09-22 16:44:22 -0700613 uint32_t *ptr = (uint32_t *) buffer;
614 int count = 0;
615
San Mehat8b1c2ba2009-11-16 10:17:30 -0800616 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
617 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
San Mehat9d2bd732009-09-22 16:44:22 -0700618 ptr++;
619 count += sizeof(uint32_t);
620
621 remain -= sizeof(uint32_t);
622 if (remain == 0)
623 break;
624 }
625 return count;
626}
627
628static int
629msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
630 unsigned int remain, u32 status)
631{
632 void __iomem *base = host->base;
633 char *ptr = buffer;
634
635 do {
636 unsigned int count, maxcnt;
637
638 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
639 MCI_FIFOHALFSIZE;
640 count = min(remain, maxcnt);
641
642 writesl(base + MMCIFIFO, ptr, count >> 2);
643 ptr += count;
644 remain -= count;
645
646 if (remain == 0)
647 break;
648
San Mehat8b1c2ba2009-11-16 10:17:30 -0800649 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700650 } while (status & MCI_TXFIFOHALFEMPTY);
651
652 return ptr - buffer;
653}
654
655static int
656msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
657{
658 while (maxspin) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800659 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
San Mehat9d2bd732009-09-22 16:44:22 -0700660 return 0;
661 udelay(1);
662 --maxspin;
663 }
664 return -ETIMEDOUT;
665}
666
San Mehat1cd22962010-02-03 12:59:29 -0800667static irqreturn_t
San Mehat9d2bd732009-09-22 16:44:22 -0700668msmsdcc_pio_irq(int irq, void *dev_id)
669{
670 struct msmsdcc_host *host = dev_id;
San Mehat9d2bd732009-09-22 16:44:22 -0700671 uint32_t status;
672
San Mehat8b1c2ba2009-11-16 10:17:30 -0800673 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700674
675 do {
676 unsigned long flags;
677 unsigned int remain, len;
678 char *buffer;
679
680 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
681 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
682 break;
683
684 if (msmsdcc_spin_on_status(host,
685 (MCI_TXFIFOHALFEMPTY |
686 MCI_RXDATAAVLBL),
687 PIO_SPINMAX)) {
688 break;
689 }
690 }
691
692 /* Map the current scatter buffer */
693 local_irq_save(flags);
694 buffer = kmap_atomic(sg_page(host->pio.sg),
695 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
696 buffer += host->pio.sg_off;
697 remain = host->pio.sg->length - host->pio.sg_off;
698 len = 0;
699 if (status & MCI_RXACTIVE)
700 len = msmsdcc_pio_read(host, buffer, remain);
701 if (status & MCI_TXACTIVE)
702 len = msmsdcc_pio_write(host, buffer, remain, status);
703
704 /* Unmap the buffer */
705 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
706 local_irq_restore(flags);
707
708 host->pio.sg_off += len;
709 host->curr.xfer_remain -= len;
710 host->curr.data_xfered += len;
711 remain -= len;
712
713 if (remain == 0) {
714 /* This sg page is full - do some housekeeping */
715 if (status & MCI_RXACTIVE && host->curr.user_pages)
716 flush_dcache_page(sg_page(host->pio.sg));
717
718 if (!--host->pio.sg_len) {
719 memset(&host->pio, 0, sizeof(host->pio));
720 break;
721 }
722
723 /* Advance to next sg */
724 host->pio.sg++;
725 host->pio.sg_off = 0;
726 }
727
San Mehat8b1c2ba2009-11-16 10:17:30 -0800728 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700729 } while (1);
730
731 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800732 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700733
734 if (!host->curr.xfer_remain)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800735 msmsdcc_writel(host, 0, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700736
737 return IRQ_HANDLED;
738}
739
740static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
741{
742 struct mmc_command *cmd = host->curr.cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700743
744 host->curr.cmd = NULL;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800745 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
746 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
747 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
748 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
San Mehat9d2bd732009-09-22 16:44:22 -0700749
San Mehat9d2bd732009-09-22 16:44:22 -0700750 if (status & MCI_CMDTIMEOUT) {
751 cmd->error = -ETIMEDOUT;
752 } else if (status & MCI_CMDCRCFAIL &&
753 cmd->flags & MMC_RSP_CRC) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700754 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700755 cmd->error = -EILSEQ;
756 }
757
758 if (!cmd->data || cmd->error) {
759 if (host->curr.data && host->dma.sg)
760 msm_dmov_stop_cmd(host->dma.channel,
761 &host->dma.hdr, 0);
762 else if (host->curr.data) { /* Non DMA */
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530763 msmsdcc_reset_and_restore(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700764 msmsdcc_stop_data(host);
765 msmsdcc_request_end(host, cmd->mrq);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530766 } else { /* host->data == NULL */
767 if (!cmd->error && host->prog_enable) {
768 if (status & MCI_PROGDONE) {
769 host->prog_scan = false;
770 host->prog_enable = false;
771 msmsdcc_request_end(host, cmd->mrq);
772 } else {
773 host->curr.cmd = cmd;
774 }
775 } else {
776 if (host->prog_enable) {
777 host->prog_scan = false;
778 host->prog_enable = false;
779 }
780 msmsdcc_request_end(host, cmd->mrq);
781 }
782 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800783 } else if (cmd->data)
784 if (!(cmd->data->flags & MMC_DATA_READ))
785 msmsdcc_start_data(host, cmd->data,
786 NULL, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700787}
788
Joe Perchesb5a74d62009-09-22 16:44:25 -0700789static void
790msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
791 void __iomem *base)
792{
793 struct mmc_data *data = host->curr.data;
794
San Mehat56a8b5b2009-11-21 12:29:46 -0800795 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530796 MCI_CMDTIMEOUT | MCI_PROGDONE) && host->curr.cmd) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800797 msmsdcc_do_cmdirq(host, status);
798 }
799
Joe Perchesb5a74d62009-09-22 16:44:25 -0700800 if (!data)
801 return;
802
803 /* Check for data errors */
804 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
805 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
806 msmsdcc_data_err(host, data, status);
807 host->curr.data_xfered = 0;
808 if (host->dma.sg)
809 msm_dmov_stop_cmd(host->dma.channel,
810 &host->dma.hdr, 0);
811 else {
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530812 msmsdcc_reset_and_restore(host);
San Mehatb3b0ca82009-11-24 12:24:55 -0800813 if (host->curr.data)
814 msmsdcc_stop_data(host);
Joe Perchesb5a74d62009-09-22 16:44:25 -0700815 if (!data->stop)
816 msmsdcc_request_end(host, data->mrq);
817 else
818 msmsdcc_start_command(host, data->stop, 0);
819 }
820 }
821
822 /* Check for data done */
823 if (!host->curr.got_dataend && (status & MCI_DATAEND))
824 host->curr.got_dataend = 1;
825
826 if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND))
827 host->curr.got_datablkend = 1;
828
829 /*
830 * If DMA is still in progress, we complete via the completion handler
831 */
832 if (host->curr.got_dataend && host->curr.got_datablkend &&
833 !host->dma.busy) {
834 /*
835 * There appears to be an issue in the controller where
836 * if you request a small block transfer (< fifo size),
837 * you may get your DATAEND/DATABLKEND irq without the
838 * PIO data irq.
839 *
840 * Check to see if there is still data to be read,
841 * and simulate a PIO irq.
842 */
843 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
844 msmsdcc_pio_irq(1, host);
845
846 msmsdcc_stop_data(host);
847 if (!data->error)
848 host->curr.data_xfered = host->curr.xfer_size;
849
850 if (!data->stop)
851 msmsdcc_request_end(host, data->mrq);
852 else
853 msmsdcc_start_command(host, data->stop, 0);
854 }
855}
856
San Mehat9d2bd732009-09-22 16:44:22 -0700857static irqreturn_t
858msmsdcc_irq(int irq, void *dev_id)
859{
860 struct msmsdcc_host *host = dev_id;
861 void __iomem *base = host->base;
862 u32 status;
863 int ret = 0;
864 int cardint = 0;
865
866 spin_lock(&host->lock);
867
868 do {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800869 status = msmsdcc_readl(host, MMCISTATUS);
870 status &= (msmsdcc_readl(host, MMCIMASK0) |
871 MCI_DATABLOCKENDMASK);
872 msmsdcc_writel(host, status, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700873
San Mehat865c80642009-11-13 13:42:06 -0800874 if (status & MCI_SDIOINTR)
875 status &= ~MCI_SDIOINTR;
876
877 if (!status)
878 break;
San Mehat9d2bd732009-09-22 16:44:22 -0700879
Joe Perchesb5a74d62009-09-22 16:44:25 -0700880 msmsdcc_handle_irq_data(host, status, base);
San Mehat9d2bd732009-09-22 16:44:22 -0700881
San Mehat9d2bd732009-09-22 16:44:22 -0700882 if (status & MCI_SDIOINTOPER) {
883 cardint = 1;
884 status &= ~MCI_SDIOINTOPER;
885 }
886 ret = 1;
887 } while (status);
888
889 spin_unlock(&host->lock);
890
891 /*
892 * We have to delay handling the card interrupt as it calls
893 * back into the driver.
894 */
895 if (cardint)
896 mmc_signal_sdio_irq(host->mmc);
897
898 return IRQ_RETVAL(ret);
899}
900
901static void
902msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
903{
904 struct msmsdcc_host *host = mmc_priv(mmc);
905 unsigned long flags;
906
907 WARN_ON(host->curr.mrq != NULL);
908 WARN_ON(host->pwr == 0);
909
910 spin_lock_irqsave(&host->lock, flags);
911
912 host->stats.reqs++;
913
914 if (host->eject) {
915 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
916 mrq->cmd->error = 0;
917 mrq->data->bytes_xfered = mrq->data->blksz *
918 mrq->data->blocks;
919 } else
920 mrq->cmd->error = -ENOMEDIUM;
921
922 spin_unlock_irqrestore(&host->lock, flags);
923 mmc_request_done(mmc, mrq);
924 return;
925 }
926
San Mehatd0719e52009-12-03 10:58:54 -0800927 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700928
San Mehat9d2bd732009-09-22 16:44:22 -0700929 host->curr.mrq = mrq;
930
931 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
San Mehat56a8b5b2009-11-21 12:29:46 -0800932 /* Queue/read data, daisy-chain command when data starts */
933 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
934 else
935 msmsdcc_start_command(host, mrq->cmd, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700936
937 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
938 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
939 CMD_SPINMAX)) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800940 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700941 msmsdcc_do_cmdirq(host, status);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800942 msmsdcc_writel(host,
943 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
944 MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700945 host->stats.cmdpoll_hits++;
946 } else {
947 host->stats.cmdpoll_misses++;
San Mehat9d2bd732009-09-22 16:44:22 -0700948 }
949 spin_unlock_irqrestore(&host->lock, flags);
950}
951
952static void
953msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
954{
955 struct msmsdcc_host *host = mmc_priv(mmc);
956 u32 clk = 0, pwr = 0;
957 int rc;
San Mehat4adbbcc2009-11-08 13:00:37 -0800958 unsigned long flags;
San Mehat9d2bd732009-09-22 16:44:22 -0700959
San Mehatc7fc9372009-11-22 17:19:07 -0800960 spin_lock_irqsave(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700961
San Mehatd0719e52009-12-03 10:58:54 -0800962 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700963
964 if (ios->clock) {
San Mehat9d2bd732009-09-22 16:44:22 -0700965 if (ios->clock != host->clk_rate) {
966 rc = clk_set_rate(host->clk, ios->clock);
967 if (rc < 0)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700968 pr_err("%s: Error setting clock rate (%d)\n",
969 mmc_hostname(host->mmc), rc);
San Mehat9d2bd732009-09-22 16:44:22 -0700970 else
971 host->clk_rate = ios->clock;
972 }
973 clk |= MCI_CLK_ENABLE;
974 }
975
976 if (ios->bus_width == MMC_BUS_WIDTH_4)
977 clk |= (2 << 10); /* Set WIDEBUS */
978
979 if (ios->clock > 400000 && msmsdcc_pwrsave)
980 clk |= (1 << 9); /* PWRSAVE */
981
982 clk |= (1 << 12); /* FLOW_ENA */
983 clk |= (1 << 15); /* feedback clock */
984
985 if (host->plat->translate_vdd)
986 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
987
988 switch (ios->power_mode) {
989 case MMC_POWER_OFF:
San Mehat9d2bd732009-09-22 16:44:22 -0700990 break;
991 case MMC_POWER_UP:
992 pwr |= MCI_PWR_UP;
993 break;
994 case MMC_POWER_ON:
San Mehat9d2bd732009-09-22 16:44:22 -0700995 pwr |= MCI_PWR_ON;
996 break;
997 }
998
999 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1000 pwr |= MCI_OD;
1001
San Mehat8b1c2ba2009-11-16 10:17:30 -08001002 msmsdcc_writel(host, clk, MMCICLOCK);
San Mehat9d2bd732009-09-22 16:44:22 -07001003
1004 if (host->pwr != pwr) {
1005 host->pwr = pwr;
San Mehat8b1c2ba2009-11-16 10:17:30 -08001006 msmsdcc_writel(host, pwr, MMCIPOWER);
San Mehat9d2bd732009-09-22 16:44:22 -07001007 }
San Mehatf4748492009-11-23 15:36:31 -08001008#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001009 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001010#endif
San Mehat4adbbcc2009-11-08 13:00:37 -08001011 spin_unlock_irqrestore(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -07001012}
1013
1014static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1015{
1016 struct msmsdcc_host *host = mmc_priv(mmc);
1017 unsigned long flags;
1018 u32 status;
1019
1020 spin_lock_irqsave(&host->lock, flags);
1021 if (msmsdcc_sdioirq == 1) {
San Mehat8b1c2ba2009-11-16 10:17:30 -08001022 status = msmsdcc_readl(host, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001023 if (enable)
1024 status |= MCI_SDIOINTOPERMASK;
1025 else
1026 status &= ~MCI_SDIOINTOPERMASK;
1027 host->saved_irq0mask = status;
San Mehat8b1c2ba2009-11-16 10:17:30 -08001028 msmsdcc_writel(host, status, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001029 }
1030 spin_unlock_irqrestore(&host->lock, flags);
1031}
1032
1033static const struct mmc_host_ops msmsdcc_ops = {
1034 .request = msmsdcc_request,
1035 .set_ios = msmsdcc_set_ios,
1036 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
1037};
1038
1039static void
1040msmsdcc_check_status(unsigned long data)
1041{
1042 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
1043 unsigned int status;
1044
1045 if (!host->plat->status) {
1046 mmc_detect_change(host->mmc, 0);
1047 goto out;
1048 }
1049
1050 status = host->plat->status(mmc_dev(host->mmc));
1051 host->eject = !status;
1052 if (status ^ host->oldstat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001053 pr_info("%s: Slot status change detected (%d -> %d)\n",
1054 mmc_hostname(host->mmc), host->oldstat, status);
San Mehat9d2bd732009-09-22 16:44:22 -07001055 if (status)
1056 mmc_detect_change(host->mmc, (5 * HZ) / 2);
1057 else
1058 mmc_detect_change(host->mmc, 0);
1059 }
1060
1061 host->oldstat = status;
1062
1063out:
1064 if (host->timer.function)
1065 mod_timer(&host->timer, jiffies + HZ);
1066}
1067
1068static irqreturn_t
1069msmsdcc_platform_status_irq(int irq, void *dev_id)
1070{
1071 struct msmsdcc_host *host = dev_id;
1072
1073 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
1074 msmsdcc_check_status((unsigned long) host);
1075 return IRQ_HANDLED;
1076}
1077
1078static void
1079msmsdcc_status_notify_cb(int card_present, void *dev_id)
1080{
1081 struct msmsdcc_host *host = dev_id;
1082
1083 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1084 card_present);
1085 msmsdcc_check_status((unsigned long) host);
1086}
1087
San Mehat9d2bd732009-09-22 16:44:22 -07001088static void
San Mehat865c80642009-11-13 13:42:06 -08001089msmsdcc_busclk_expired(unsigned long _data)
San Mehat9d2bd732009-09-22 16:44:22 -07001090{
1091 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
San Mehat9d2bd732009-09-22 16:44:22 -07001092
San Mehat865c80642009-11-13 13:42:06 -08001093 if (host->clks_on)
San Mehatc7fc9372009-11-22 17:19:07 -08001094 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001095}
1096
1097static int
1098msmsdcc_init_dma(struct msmsdcc_host *host)
1099{
1100 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1101 host->dma.host = host;
1102 host->dma.channel = -1;
1103
1104 if (!host->dmares)
1105 return -ENODEV;
1106
1107 host->dma.nc = dma_alloc_coherent(NULL,
1108 sizeof(struct msmsdcc_nc_dmadata),
1109 &host->dma.nc_busaddr,
1110 GFP_KERNEL);
1111 if (host->dma.nc == NULL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001112 pr_err("Unable to allocate DMA buffer\n");
San Mehat9d2bd732009-09-22 16:44:22 -07001113 return -ENOMEM;
1114 }
1115 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1116 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1117 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1118 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1119 host->dma.channel = host->dmares->start;
1120
1121 return 0;
1122}
1123
San Mehat9d2bd732009-09-22 16:44:22 -07001124static int
1125msmsdcc_probe(struct platform_device *pdev)
1126{
Sahitya Tummalab5d643d2010-07-29 16:55:34 +05301127 struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
San Mehat9d2bd732009-09-22 16:44:22 -07001128 struct msmsdcc_host *host;
1129 struct mmc_host *mmc;
1130 struct resource *cmd_irqres = NULL;
1131 struct resource *pio_irqres = NULL;
1132 struct resource *stat_irqres = NULL;
1133 struct resource *memres = NULL;
1134 struct resource *dmares = NULL;
1135 int ret;
1136
1137 /* must have platform data */
1138 if (!plat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001139 pr_err("%s: Platform data not available\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001140 ret = -EINVAL;
1141 goto out;
1142 }
1143
1144 if (pdev->id < 1 || pdev->id > 4)
1145 return -EINVAL;
1146
1147 if (pdev->resource == NULL || pdev->num_resources < 2) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001148 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001149 return -ENXIO;
1150 }
1151
1152 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1153 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1154 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1155 "cmd_irq");
1156 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1157 "pio_irq");
1158 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1159 "status_irq");
1160
1161 if (!cmd_irqres || !pio_irqres || !memres) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001162 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001163 return -ENXIO;
1164 }
1165
1166 /*
1167 * Setup our host structure
1168 */
1169
1170 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1171 if (!mmc) {
1172 ret = -ENOMEM;
1173 goto out;
1174 }
1175
1176 host = mmc_priv(mmc);
1177 host->pdev_id = pdev->id;
1178 host->plat = plat;
1179 host->mmc = mmc;
San Mehat56a8b5b2009-11-21 12:29:46 -08001180 host->curr.cmd = NULL;
San Mehat9d2bd732009-09-22 16:44:22 -07001181
1182 host->cmdpoll = 1;
1183
1184 host->base = ioremap(memres->start, PAGE_SIZE);
1185 if (!host->base) {
1186 ret = -ENOMEM;
1187 goto out;
1188 }
1189
1190 host->cmd_irqres = cmd_irqres;
1191 host->pio_irqres = pio_irqres;
1192 host->memres = memres;
1193 host->dmares = dmares;
1194 spin_lock_init(&host->lock);
1195
Sahitya Tummala62612cf2010-12-08 15:03:03 +05301196 tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
1197 (unsigned long)host);
1198
San Mehat9d2bd732009-09-22 16:44:22 -07001199 /*
1200 * Setup DMA
1201 */
1202 msmsdcc_init_dma(host);
1203
San Mehat4adbbcc2009-11-08 13:00:37 -08001204 /* Get our clocks */
San Mehat9d2bd732009-09-22 16:44:22 -07001205 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1206 if (IS_ERR(host->pclk)) {
1207 ret = PTR_ERR(host->pclk);
1208 goto host_free;
1209 }
1210
San Mehat9d2bd732009-09-22 16:44:22 -07001211 host->clk = clk_get(&pdev->dev, "sdc_clk");
1212 if (IS_ERR(host->clk)) {
1213 ret = PTR_ERR(host->clk);
San Mehat4adbbcc2009-11-08 13:00:37 -08001214 goto pclk_put;
San Mehat9d2bd732009-09-22 16:44:22 -07001215 }
1216
San Mehat4adbbcc2009-11-08 13:00:37 -08001217 /* Enable clocks */
San Mehatc7fc9372009-11-22 17:19:07 -08001218 ret = msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001219 if (ret)
1220 goto clk_put;
1221
1222 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1223 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001224 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001225 goto clk_disable;
1226 }
1227
San Mehat4adbbcc2009-11-08 13:00:37 -08001228 host->pclk_rate = clk_get_rate(host->pclk);
San Mehat9d2bd732009-09-22 16:44:22 -07001229 host->clk_rate = clk_get_rate(host->clk);
1230
San Mehat9d2bd732009-09-22 16:44:22 -07001231 /*
1232 * Setup MMC host structure
1233 */
1234 mmc->ops = &msmsdcc_ops;
1235 mmc->f_min = msmsdcc_fmin;
1236 mmc->f_max = msmsdcc_fmax;
1237 mmc->ocr_avail = plat->ocr_mask;
1238
1239 if (msmsdcc_4bit)
1240 mmc->caps |= MMC_CAP_4_BIT_DATA;
1241 if (msmsdcc_sdioirq)
1242 mmc->caps |= MMC_CAP_SDIO_IRQ;
1243 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1244
Martin K. Petersena36274e2010-09-10 01:33:59 -04001245 mmc->max_segs = NR_SG;
San Mehat9d2bd732009-09-22 16:44:22 -07001246 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1247 mmc->max_blk_count = 65536;
1248
1249 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1250 mmc->max_seg_size = mmc->max_req_size;
1251
San Mehat8b1c2ba2009-11-16 10:17:30 -08001252 msmsdcc_writel(host, 0, MMCIMASK0);
1253 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -07001254
San Mehat8b1c2ba2009-11-16 10:17:30 -08001255 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001256 host->saved_irq0mask = MCI_IRQENABLE;
1257
1258 /*
1259 * Setup card detect change
1260 */
1261
1262 memset(&host->timer, 0, sizeof(host->timer));
1263
1264 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1265 unsigned long irqflags = IRQF_SHARED |
1266 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1267
1268 host->stat_irq = stat_irqres->start;
1269 ret = request_irq(host->stat_irq,
1270 msmsdcc_platform_status_irq,
1271 irqflags,
1272 DRIVER_NAME " (slot)",
1273 host);
1274 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001275 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1276 mmc_hostname(mmc), host->stat_irq, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001277 goto clk_disable;
1278 }
1279 } else if (plat->register_status_notify) {
1280 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1281 } else if (!plat->status)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001282 pr_err("%s: No card detect facilities available\n",
San Mehat9d2bd732009-09-22 16:44:22 -07001283 mmc_hostname(mmc));
1284 else {
1285 init_timer(&host->timer);
1286 host->timer.data = (unsigned long)host;
1287 host->timer.function = msmsdcc_check_status;
1288 host->timer.expires = jiffies + HZ;
1289 add_timer(&host->timer);
1290 }
1291
1292 if (plat->status) {
1293 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1294 host->eject = !host->oldstat;
1295 }
1296
San Mehat865c80642009-11-13 13:42:06 -08001297 init_timer(&host->busclk_timer);
1298 host->busclk_timer.data = (unsigned long) host;
1299 host->busclk_timer.function = msmsdcc_busclk_expired;
San Mehat9d2bd732009-09-22 16:44:22 -07001300
1301 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1302 DRIVER_NAME " (cmd)", host);
1303 if (ret)
1304 goto stat_irq_free;
1305
1306 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1307 DRIVER_NAME " (pio)", host);
1308 if (ret)
1309 goto cmd_irq_free;
1310
1311 mmc_set_drvdata(pdev, mmc);
1312 mmc_add_host(mmc);
1313
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001314 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1315 mmc_hostname(mmc), (unsigned long long)memres->start,
1316 (unsigned int) cmd_irqres->start,
1317 (unsigned int) host->stat_irq, host->dma.channel);
1318 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1319 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1320 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1321 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1322 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1323 pr_info("%s: Power save feature enable = %d\n",
1324 mmc_hostname(mmc), msmsdcc_pwrsave);
San Mehat9d2bd732009-09-22 16:44:22 -07001325
1326 if (host->dma.channel != -1) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001327 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1328 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1329 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1330 mmc_hostname(mmc), host->dma.cmd_busaddr,
1331 host->dma.cmdptr_busaddr);
San Mehat9d2bd732009-09-22 16:44:22 -07001332 } else
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001333 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001334 if (host->timer.function)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001335 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001336
San Mehatf4748492009-11-23 15:36:31 -08001337#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001338 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001339#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001340 return 0;
1341 cmd_irq_free:
1342 free_irq(cmd_irqres->start, host);
1343 stat_irq_free:
1344 if (host->stat_irq)
1345 free_irq(host->stat_irq, host);
1346 clk_disable:
San Mehatc7fc9372009-11-22 17:19:07 -08001347 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001348 clk_put:
1349 clk_put(host->clk);
San Mehat9d2bd732009-09-22 16:44:22 -07001350 pclk_put:
1351 clk_put(host->pclk);
1352 host_free:
1353 mmc_free_host(mmc);
1354 out:
1355 return ret;
1356}
1357
Daniel Walker08ecfde2010-06-23 12:32:20 -07001358#ifdef CONFIG_PM
1359#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1360static void
1361do_resume_work(struct work_struct *work)
1362{
1363 struct msmsdcc_host *host =
1364 container_of(work, struct msmsdcc_host, resume_task);
1365 struct mmc_host *mmc = host->mmc;
1366
1367 if (mmc) {
1368 mmc_resume_host(mmc);
1369 if (host->stat_irq)
1370 enable_irq(host->stat_irq);
1371 }
1372}
1373#endif
1374
1375
San Mehat9d2bd732009-09-22 16:44:22 -07001376static int
1377msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1378{
1379 struct mmc_host *mmc = mmc_get_drvdata(dev);
1380 int rc = 0;
1381
1382 if (mmc) {
1383 struct msmsdcc_host *host = mmc_priv(mmc);
1384
1385 if (host->stat_irq)
1386 disable_irq(host->stat_irq);
1387
1388 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001389 rc = mmc_suspend_host(mmc);
San Mehatd0719e52009-12-03 10:58:54 -08001390 if (!rc)
San Mehat8b1c2ba2009-11-16 10:17:30 -08001391 msmsdcc_writel(host, 0, MMCIMASK0);
San Mehatc7fc9372009-11-22 17:19:07 -08001392 if (host->clks_on)
1393 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001394 }
1395 return rc;
1396}
1397
1398static int
1399msmsdcc_resume(struct platform_device *dev)
1400{
1401 struct mmc_host *mmc = mmc_get_drvdata(dev);
San Mehat9d2bd732009-09-22 16:44:22 -07001402
1403 if (mmc) {
1404 struct msmsdcc_host *host = mmc_priv(mmc);
1405
San Mehatc7fc9372009-11-22 17:19:07 -08001406 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001407
San Mehat8b1c2ba2009-11-16 10:17:30 -08001408 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001409
1410 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1411 mmc_resume_host(mmc);
Roel Kluin5b8a2fb2010-01-17 20:25:36 +01001412 if (host->stat_irq)
San Mehat9d2bd732009-09-22 16:44:22 -07001413 enable_irq(host->stat_irq);
San Mehatf4748492009-11-23 15:36:31 -08001414#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001415 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001416#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001417 }
1418 return 0;
1419}
Daniel Walker08ecfde2010-06-23 12:32:20 -07001420#else
1421#define msmsdcc_suspend 0
1422#define msmsdcc_resume 0
1423#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001424
1425static struct platform_driver msmsdcc_driver = {
1426 .probe = msmsdcc_probe,
1427 .suspend = msmsdcc_suspend,
1428 .resume = msmsdcc_resume,
1429 .driver = {
1430 .name = "msm_sdcc",
1431 },
1432};
1433
1434static int __init msmsdcc_init(void)
1435{
1436 return platform_driver_register(&msmsdcc_driver);
1437}
1438
1439static void __exit msmsdcc_exit(void)
1440{
1441 platform_driver_unregister(&msmsdcc_driver);
1442}
1443
1444module_init(msmsdcc_init);
1445module_exit(msmsdcc_exit);
1446
1447MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1448MODULE_LICENSE("GPL");