blob: 67f536ca31d328c1cc137505e3507228d6184da8 [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>
San Mehat9d2bd732009-09-22 16:44:22 -070047
San Mehat9d2bd732009-09-22 16:44:22 -070048#include "msm_sdcc.h"
49
50#define DRIVER_NAME "msm-sdcc"
51
San Mehat24bbd7d2009-12-01 10:10:47 -080052#define BUSCLK_PWRSAVE 1
San Mehatc7fc9372009-11-22 17:19:07 -080053#define BUSCLK_TIMEOUT (HZ)
San Mehat9d2bd732009-09-22 16:44:22 -070054static unsigned int msmsdcc_fmin = 144000;
55static unsigned int msmsdcc_fmax = 50000000;
56static unsigned int msmsdcc_4bit = 1;
57static unsigned int msmsdcc_pwrsave = 1;
58static unsigned int msmsdcc_piopoll = 1;
59static unsigned int msmsdcc_sdioirq;
60
61#define PIO_SPINMAX 30
62#define CMD_SPINMAX 20
63
San Mehat865c80642009-11-13 13:42:06 -080064
San Mehatd0719e52009-12-03 10:58:54 -080065static inline void
San Mehatc7fc9372009-11-22 17:19:07 -080066msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
San Mehat865c80642009-11-13 13:42:06 -080067{
San Mehatc7fc9372009-11-22 17:19:07 -080068 WARN_ON(!host->clks_on);
San Mehat8b1c2ba2009-11-16 10:17:30 -080069
San Mehatf4748492009-11-23 15:36:31 -080070 BUG_ON(host->curr.mrq);
71
San Mehatc7fc9372009-11-22 17:19:07 -080072 if (deferr) {
73 mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
San Mehat865c80642009-11-13 13:42:06 -080074 } else {
San Mehatc7fc9372009-11-22 17:19:07 -080075 del_timer_sync(&host->busclk_timer);
San Mehatd0719e52009-12-03 10:58:54 -080076 /* 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 Mehat865c80642009-11-13 13:42:06 -080084 }
San Mehatc7fc9372009-11-22 17:19:07 -080085}
86
87static inline int
88msmsdcc_enable_clocks(struct msmsdcc_host *host)
89{
90 int rc;
91
San Mehatc7fc9372009-11-22 17:19:07 -080092 del_timer_sync(&host->busclk_timer);
93
San Mehatd0719e52009-12-03 10:58:54 -080094 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 Mehatc7fc9372009-11-22 17:19:07 -0800106 }
San Mehat865c80642009-11-13 13:42:06 -0800107 return 0;
108}
109
San Mehat8b1c2ba2009-11-16 10:17:30 -0800110static inline unsigned int
111msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
112{
113 return readl(host->base + reg);
114}
115
116static inline void
117msmsdcc_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 Mehat865c80642009-11-13 13:42:06 -0800124
San Mehat9d2bd732009-09-22 16:44:22 -0700125static void
126msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
127 u32 c);
128
129static void
130msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
131{
San Mehat9d2bd732009-09-22 16:44:22 -0700132 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 Mehatf4748492009-11-23 15:36:31 -0800142#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800143 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800144#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700145 /*
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
154static void
155msmsdcc_stop_data(struct msmsdcc_host *host)
156{
San Mehat9d2bd732009-09-22 16:44:22 -0700157 host->curr.data = NULL;
158 host->curr.got_dataend = host->curr.got_datablkend = 0;
159}
160
161uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
162{
Sahitya Tummalaedd4dd02010-07-29 16:57:41 +0530163 return host->memres->start + MMCIFIFO;
San Mehat9d2bd732009-09-22 16:44:22 -0700164}
165
San Mehat56a8b5b2009-11-21 12:29:46 -0800166static inline void
167msmsdcc_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
172static void
173msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
174{
San Mehat6ac9ea62009-12-02 17:24:58 -0800175 struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
San Mehat56a8b5b2009-11-21 12:29:46 -0800176
San Mehat6ac9ea62009-12-02 17:24:58 -0800177 msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
San Mehatd0719e52009-12-03 10:58:54 -0800178 msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
179 MMCIDATALENGTH);
San Mehat6ac9ea62009-12-02 17:24:58 -0800180 msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
181 msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
San Mehat56a8b5b2009-11-21 12:29:46 -0800182
San Mehat6ac9ea62009-12-02 17:24:58 -0800183 if (host->cmd_cmd) {
184 msmsdcc_start_command_exec(host,
185 (u32) host->cmd_cmd->arg,
186 (u32) host->cmd_c);
187 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800188 host->dma.active = 1;
189}
190
San Mehat9d2bd732009-09-22 16:44:22 -0700191static void
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530192msmsdcc_dma_complete_tlet(unsigned long data)
San Mehat9d2bd732009-09-22 16:44:22 -0700193{
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530194 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
San Mehat9d2bd732009-09-22 16:44:22 -0700195 unsigned long flags;
196 struct mmc_request *mrq;
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530197 struct msm_dmov_errdata err;
San Mehat9d2bd732009-09-22 16:44:22 -0700198
199 spin_lock_irqsave(&host->lock, flags);
San Mehat56a8b5b2009-11-21 12:29:46 -0800200 host->dma.active = 0;
201
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530202 err = host->dma.err;
San Mehat9d2bd732009-09-22 16:44:22 -0700203 mrq = host->curr.mrq;
204 BUG_ON(!mrq);
San Mehatb3b0ca82009-11-24 12:24:55 -0800205 WARN_ON(!mrq->data);
San Mehat9d2bd732009-09-22 16:44:22 -0700206
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530207 if (!(host->dma.result & DMOV_RSLT_VALID)) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700208 pr_err("msmsdcc: Invalid DataMover result\n");
San Mehat9d2bd732009-09-22 16:44:22 -0700209 goto out;
210 }
211
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530212 if (host->dma.result & DMOV_RSLT_DONE) {
San Mehat9d2bd732009-09-22 16:44:22 -0700213 host->curr.data_xfered = host->curr.xfer_size;
214 } else {
215 /* Error or flush */
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530216 if (host->dma.result & DMOV_RSLT_ERROR)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700217 pr_err("%s: DMA error (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530218 mmc_hostname(host->mmc), host->dma.result);
219 if (host->dma.result & DMOV_RSLT_FLUSH)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700220 pr_err("%s: DMA channel flushed (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530221 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 Mehat9d2bd732009-09-22 16:44:22 -0700226 if (!mrq->data->error)
227 mrq->data->error = -EIO;
228 }
San Mehat9d2bd732009-09-22 16:44:22 -0700229 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 Perches75d14522009-09-22 16:44:24 -0700236 for (i = 0; i < host->dma.num_ents; i++)
237 flush_dcache_page(sg_page(sg++));
San Mehat9d2bd732009-09-22 16:44:22 -0700238 }
239
240 host->dma.sg = NULL;
San Mehat56a8b5b2009-11-21 12:29:46 -0800241 host->dma.busy = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700242
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 Mehat9d2bd732009-09-22 16:44:22 -0700255 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 Mehatf4748492009-11-23 15:36:31 -0800260#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800261 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800262#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700263 mmc_request_done(host->mmc, mrq);
264 return;
265 } else
266 msmsdcc_start_command(host, mrq->data->stop, 0);
267 }
268
269out:
270 spin_unlock_irqrestore(&host->lock, flags);
271 return;
272}
273
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530274static void
275msmsdcc_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 Mehat9d2bd732009-09-22 16:44:22 -0700290static 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
302static 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 Mehat56a8b5b2009-11-21 12:29:46 -0800319 BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
320
San Mehat9d2bd732009-09-22 16:44:22 -0700321 nc = host->dma.nc;
322
Joe Perches75d14522009-09-22 16:44:24 -0700323 switch (host->pdev_id) {
324 case 1:
San Mehat9d2bd732009-09-22 16:44:22 -0700325 crci = MSMSDCC_CRCI_SDC1;
Joe Perches75d14522009-09-22 16:44:24 -0700326 break;
327 case 2:
San Mehat9d2bd732009-09-22 16:44:22 -0700328 crci = MSMSDCC_CRCI_SDC2;
Joe Perches75d14522009-09-22 16:44:24 -0700329 break;
330 case 3:
San Mehat9d2bd732009-09-22 16:44:22 -0700331 crci = MSMSDCC_CRCI_SDC3;
Joe Perches75d14522009-09-22 16:44:24 -0700332 break;
333 case 4:
San Mehat9d2bd732009-09-22 16:44:22 -0700334 crci = MSMSDCC_CRCI_SDC4;
Joe Perches75d14522009-09-22 16:44:24 -0700335 break;
336 default:
San Mehat9d2bd732009-09-22 16:44:22 -0700337 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 Mehat9d2bd732009-09-22 16:44:22 -0700349 box = &nc->cmd[0];
350 for (i = 0; i < host->dma.num_ents; i++) {
351 box->cmd = CMD_MODE_BOX;
352
San Mehat56a8b5b2009-11-21 12:29:46 -0800353 /* 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 Mehat9d2bd732009-09-22 16:44:22 -0700358 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 Mehat56a8b5b2009-11-21 12:29:46 -0800396 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 Mehat9d2bd732009-09-22 16:44:22 -0700407
408 return 0;
409}
410
San Mehat56a8b5b2009-11-21 12:29:46 -0800411static int
412snoop_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 Mehat9d2bd732009-09-22 16:44:22 -0700418 return 0;
419}
420
421static void
San Mehat56a8b5b2009-11-21 12:29:46 -0800422msmsdcc_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
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530441 if (host->prog_scan && (cmd->opcode == 12)) {
442 *c |= MCI_CPSM_PROGENA;
443 host->prog_enable = true;
444 }
445
San Mehat56a8b5b2009-11-21 12:29:46 -0800446 if (cmd == cmd->mrq->stop)
447 *c |= MCI_CSPM_MCIABORT;
448
449 if (snoop_cccr_abort(cmd))
450 *c |= MCI_CSPM_MCIABORT;
451
452 if (host->curr.cmd != NULL) {
453 printk(KERN_ERR "%s: Overlapping command requests\n",
454 mmc_hostname(host->mmc));
455 }
456 host->curr.cmd = cmd;
457}
458
459static void
460msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
461 struct mmc_command *cmd, u32 c)
San Mehat9d2bd732009-09-22 16:44:22 -0700462{
463 unsigned int datactrl, timeout;
464 unsigned long long clks;
San Mehat9d2bd732009-09-22 16:44:22 -0700465 unsigned int pio_irqmask = 0;
466
467 host->curr.data = data;
468 host->curr.xfer_size = data->blksz * data->blocks;
469 host->curr.xfer_remain = host->curr.xfer_size;
470 host->curr.data_xfered = 0;
471 host->curr.got_dataend = 0;
472 host->curr.got_datablkend = 0;
473
474 memset(&host->pio, 0, sizeof(host->pio));
475
San Mehat9d2bd732009-09-22 16:44:22 -0700476 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
477
478 if (!msmsdcc_config_dma(host, data))
479 datactrl |= MCI_DPSM_DMAENABLE;
480 else {
481 host->pio.sg = data->sg;
482 host->pio.sg_len = data->sg_len;
483 host->pio.sg_off = 0;
484
485 if (data->flags & MMC_DATA_READ) {
486 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
487 if (host->curr.xfer_remain < MCI_FIFOSIZE)
488 pio_irqmask |= MCI_RXDATAAVLBLMASK;
489 } else
490 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
491 }
492
493 if (data->flags & MMC_DATA_READ)
494 datactrl |= MCI_DPSM_DIRECTION;
495
San Mehat56a8b5b2009-11-21 12:29:46 -0800496 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
497 do_div(clks, NSEC_PER_SEC);
498 timeout = data->timeout_clks + (unsigned int)clks*2 ;
San Mehat9d2bd732009-09-22 16:44:22 -0700499
500 if (datactrl & MCI_DPSM_DMAENABLE) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800501 /* Save parameters for the exec function */
502 host->cmd_timeout = timeout;
503 host->cmd_pio_irqmask = pio_irqmask;
504 host->cmd_datactrl = datactrl;
505 host->cmd_cmd = cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700506
San Mehat56a8b5b2009-11-21 12:29:46 -0800507 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
508 host->dma.hdr.data = (void *)host;
San Mehat9d2bd732009-09-22 16:44:22 -0700509 host->dma.busy = 1;
San Mehat56a8b5b2009-11-21 12:29:46 -0800510
511 if (cmd) {
512 msmsdcc_start_command_deferred(host, cmd, &c);
513 host->cmd_c = c;
514 }
San Mehat9d2bd732009-09-22 16:44:22 -0700515 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530516 if (data->flags & MMC_DATA_WRITE)
517 host->prog_scan = true;
San Mehat56a8b5b2009-11-21 12:29:46 -0800518 } else {
519 msmsdcc_writel(host, timeout, MMCIDATATIMER);
520
521 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
522
523 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
524 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
525
526 if (cmd) {
527 /* Daisy-chain the command if requested */
528 msmsdcc_start_command(host, cmd, c);
529 }
San Mehat9d2bd732009-09-22 16:44:22 -0700530 }
531}
532
533static void
534msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
535{
San Mehat9d2bd732009-09-22 16:44:22 -0700536 if (cmd == cmd->mrq->stop)
537 c |= MCI_CSPM_MCIABORT;
538
San Mehat9d2bd732009-09-22 16:44:22 -0700539 host->stats.cmds++;
540
San Mehat56a8b5b2009-11-21 12:29:46 -0800541 msmsdcc_start_command_deferred(host, cmd, &c);
542 msmsdcc_start_command_exec(host, cmd->arg, c);
San Mehat9d2bd732009-09-22 16:44:22 -0700543}
544
545static void
546msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
547 unsigned int status)
548{
549 if (status & MCI_DATACRCFAIL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700550 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
551 pr_err("%s: opcode 0x%.8x\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700552 data->mrq->cmd->opcode);
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700553 pr_err("%s: blksz %d, blocks %d\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700554 data->blksz, data->blocks);
555 data->error = -EILSEQ;
556 } else if (status & MCI_DATATIMEOUT) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700557 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700558 data->error = -ETIMEDOUT;
559 } else if (status & MCI_RXOVERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700560 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700561 data->error = -EIO;
562 } else if (status & MCI_TXUNDERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700563 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700564 data->error = -EIO;
565 } else {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700566 pr_err("%s: Unknown error (0x%.8x)\n",
567 mmc_hostname(host->mmc), status);
San Mehat9d2bd732009-09-22 16:44:22 -0700568 data->error = -EIO;
569 }
570}
571
572
573static int
574msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
575{
San Mehat9d2bd732009-09-22 16:44:22 -0700576 uint32_t *ptr = (uint32_t *) buffer;
577 int count = 0;
578
San Mehat8b1c2ba2009-11-16 10:17:30 -0800579 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
580 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
San Mehat9d2bd732009-09-22 16:44:22 -0700581 ptr++;
582 count += sizeof(uint32_t);
583
584 remain -= sizeof(uint32_t);
585 if (remain == 0)
586 break;
587 }
588 return count;
589}
590
591static int
592msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
593 unsigned int remain, u32 status)
594{
595 void __iomem *base = host->base;
596 char *ptr = buffer;
597
598 do {
599 unsigned int count, maxcnt;
600
601 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
602 MCI_FIFOHALFSIZE;
603 count = min(remain, maxcnt);
604
605 writesl(base + MMCIFIFO, ptr, count >> 2);
606 ptr += count;
607 remain -= count;
608
609 if (remain == 0)
610 break;
611
San Mehat8b1c2ba2009-11-16 10:17:30 -0800612 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700613 } while (status & MCI_TXFIFOHALFEMPTY);
614
615 return ptr - buffer;
616}
617
618static int
619msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
620{
621 while (maxspin) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800622 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
San Mehat9d2bd732009-09-22 16:44:22 -0700623 return 0;
624 udelay(1);
625 --maxspin;
626 }
627 return -ETIMEDOUT;
628}
629
San Mehat1cd22962010-02-03 12:59:29 -0800630static irqreturn_t
San Mehat9d2bd732009-09-22 16:44:22 -0700631msmsdcc_pio_irq(int irq, void *dev_id)
632{
633 struct msmsdcc_host *host = dev_id;
San Mehat9d2bd732009-09-22 16:44:22 -0700634 uint32_t status;
635
San Mehat8b1c2ba2009-11-16 10:17:30 -0800636 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700637
638 do {
639 unsigned long flags;
640 unsigned int remain, len;
641 char *buffer;
642
643 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
644 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
645 break;
646
647 if (msmsdcc_spin_on_status(host,
648 (MCI_TXFIFOHALFEMPTY |
649 MCI_RXDATAAVLBL),
650 PIO_SPINMAX)) {
651 break;
652 }
653 }
654
655 /* Map the current scatter buffer */
656 local_irq_save(flags);
657 buffer = kmap_atomic(sg_page(host->pio.sg),
658 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
659 buffer += host->pio.sg_off;
660 remain = host->pio.sg->length - host->pio.sg_off;
661 len = 0;
662 if (status & MCI_RXACTIVE)
663 len = msmsdcc_pio_read(host, buffer, remain);
664 if (status & MCI_TXACTIVE)
665 len = msmsdcc_pio_write(host, buffer, remain, status);
666
667 /* Unmap the buffer */
668 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
669 local_irq_restore(flags);
670
671 host->pio.sg_off += len;
672 host->curr.xfer_remain -= len;
673 host->curr.data_xfered += len;
674 remain -= len;
675
676 if (remain == 0) {
677 /* This sg page is full - do some housekeeping */
678 if (status & MCI_RXACTIVE && host->curr.user_pages)
679 flush_dcache_page(sg_page(host->pio.sg));
680
681 if (!--host->pio.sg_len) {
682 memset(&host->pio, 0, sizeof(host->pio));
683 break;
684 }
685
686 /* Advance to next sg */
687 host->pio.sg++;
688 host->pio.sg_off = 0;
689 }
690
San Mehat8b1c2ba2009-11-16 10:17:30 -0800691 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700692 } while (1);
693
694 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800695 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700696
697 if (!host->curr.xfer_remain)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800698 msmsdcc_writel(host, 0, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700699
700 return IRQ_HANDLED;
701}
702
703static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
704{
705 struct mmc_command *cmd = host->curr.cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700706
707 host->curr.cmd = NULL;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800708 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
709 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
710 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
711 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
San Mehat9d2bd732009-09-22 16:44:22 -0700712
San Mehat9d2bd732009-09-22 16:44:22 -0700713 if (status & MCI_CMDTIMEOUT) {
714 cmd->error = -ETIMEDOUT;
715 } else if (status & MCI_CMDCRCFAIL &&
716 cmd->flags & MMC_RSP_CRC) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700717 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700718 cmd->error = -EILSEQ;
719 }
720
721 if (!cmd->data || cmd->error) {
722 if (host->curr.data && host->dma.sg)
723 msm_dmov_stop_cmd(host->dma.channel,
724 &host->dma.hdr, 0);
725 else if (host->curr.data) { /* Non DMA */
726 msmsdcc_stop_data(host);
727 msmsdcc_request_end(host, cmd->mrq);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530728 } else { /* host->data == NULL */
729 if (!cmd->error && host->prog_enable) {
730 if (status & MCI_PROGDONE) {
731 host->prog_scan = false;
732 host->prog_enable = false;
733 msmsdcc_request_end(host, cmd->mrq);
734 } else {
735 host->curr.cmd = cmd;
736 }
737 } else {
738 if (host->prog_enable) {
739 host->prog_scan = false;
740 host->prog_enable = false;
741 }
742 msmsdcc_request_end(host, cmd->mrq);
743 }
744 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800745 } else if (cmd->data)
746 if (!(cmd->data->flags & MMC_DATA_READ))
747 msmsdcc_start_data(host, cmd->data,
748 NULL, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700749}
750
Joe Perchesb5a74d62009-09-22 16:44:25 -0700751static void
752msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
753 void __iomem *base)
754{
755 struct mmc_data *data = host->curr.data;
756
San Mehat56a8b5b2009-11-21 12:29:46 -0800757 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530758 MCI_CMDTIMEOUT | MCI_PROGDONE) && host->curr.cmd) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800759 msmsdcc_do_cmdirq(host, status);
760 }
761
Joe Perchesb5a74d62009-09-22 16:44:25 -0700762 if (!data)
763 return;
764
765 /* Check for data errors */
766 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
767 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
768 msmsdcc_data_err(host, data, status);
769 host->curr.data_xfered = 0;
770 if (host->dma.sg)
771 msm_dmov_stop_cmd(host->dma.channel,
772 &host->dma.hdr, 0);
773 else {
San Mehatb3b0ca82009-11-24 12:24:55 -0800774 if (host->curr.data)
775 msmsdcc_stop_data(host);
Joe Perchesb5a74d62009-09-22 16:44:25 -0700776 if (!data->stop)
777 msmsdcc_request_end(host, data->mrq);
778 else
779 msmsdcc_start_command(host, data->stop, 0);
780 }
781 }
782
783 /* Check for data done */
784 if (!host->curr.got_dataend && (status & MCI_DATAEND))
785 host->curr.got_dataend = 1;
786
787 if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND))
788 host->curr.got_datablkend = 1;
789
790 /*
791 * If DMA is still in progress, we complete via the completion handler
792 */
793 if (host->curr.got_dataend && host->curr.got_datablkend &&
794 !host->dma.busy) {
795 /*
796 * There appears to be an issue in the controller where
797 * if you request a small block transfer (< fifo size),
798 * you may get your DATAEND/DATABLKEND irq without the
799 * PIO data irq.
800 *
801 * Check to see if there is still data to be read,
802 * and simulate a PIO irq.
803 */
804 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
805 msmsdcc_pio_irq(1, host);
806
807 msmsdcc_stop_data(host);
808 if (!data->error)
809 host->curr.data_xfered = host->curr.xfer_size;
810
811 if (!data->stop)
812 msmsdcc_request_end(host, data->mrq);
813 else
814 msmsdcc_start_command(host, data->stop, 0);
815 }
816}
817
San Mehat9d2bd732009-09-22 16:44:22 -0700818static irqreturn_t
819msmsdcc_irq(int irq, void *dev_id)
820{
821 struct msmsdcc_host *host = dev_id;
822 void __iomem *base = host->base;
823 u32 status;
824 int ret = 0;
825 int cardint = 0;
826
827 spin_lock(&host->lock);
828
829 do {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800830 status = msmsdcc_readl(host, MMCISTATUS);
831 status &= (msmsdcc_readl(host, MMCIMASK0) |
832 MCI_DATABLOCKENDMASK);
833 msmsdcc_writel(host, status, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700834
San Mehat865c80642009-11-13 13:42:06 -0800835 if (status & MCI_SDIOINTR)
836 status &= ~MCI_SDIOINTR;
837
838 if (!status)
839 break;
San Mehat9d2bd732009-09-22 16:44:22 -0700840
Joe Perchesb5a74d62009-09-22 16:44:25 -0700841 msmsdcc_handle_irq_data(host, status, base);
San Mehat9d2bd732009-09-22 16:44:22 -0700842
San Mehat9d2bd732009-09-22 16:44:22 -0700843 if (status & MCI_SDIOINTOPER) {
844 cardint = 1;
845 status &= ~MCI_SDIOINTOPER;
846 }
847 ret = 1;
848 } while (status);
849
850 spin_unlock(&host->lock);
851
852 /*
853 * We have to delay handling the card interrupt as it calls
854 * back into the driver.
855 */
856 if (cardint)
857 mmc_signal_sdio_irq(host->mmc);
858
859 return IRQ_RETVAL(ret);
860}
861
862static void
863msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
864{
865 struct msmsdcc_host *host = mmc_priv(mmc);
866 unsigned long flags;
867
868 WARN_ON(host->curr.mrq != NULL);
869 WARN_ON(host->pwr == 0);
870
871 spin_lock_irqsave(&host->lock, flags);
872
873 host->stats.reqs++;
874
875 if (host->eject) {
876 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
877 mrq->cmd->error = 0;
878 mrq->data->bytes_xfered = mrq->data->blksz *
879 mrq->data->blocks;
880 } else
881 mrq->cmd->error = -ENOMEDIUM;
882
883 spin_unlock_irqrestore(&host->lock, flags);
884 mmc_request_done(mmc, mrq);
885 return;
886 }
887
San Mehatd0719e52009-12-03 10:58:54 -0800888 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700889
San Mehat9d2bd732009-09-22 16:44:22 -0700890 host->curr.mrq = mrq;
891
892 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
San Mehat56a8b5b2009-11-21 12:29:46 -0800893 /* Queue/read data, daisy-chain command when data starts */
894 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
895 else
896 msmsdcc_start_command(host, mrq->cmd, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700897
898 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
899 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
900 CMD_SPINMAX)) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800901 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700902 msmsdcc_do_cmdirq(host, status);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800903 msmsdcc_writel(host,
904 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
905 MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700906 host->stats.cmdpoll_hits++;
907 } else {
908 host->stats.cmdpoll_misses++;
San Mehat9d2bd732009-09-22 16:44:22 -0700909 }
910 spin_unlock_irqrestore(&host->lock, flags);
911}
912
913static void
914msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
915{
916 struct msmsdcc_host *host = mmc_priv(mmc);
917 u32 clk = 0, pwr = 0;
918 int rc;
San Mehat4adbbcc2009-11-08 13:00:37 -0800919 unsigned long flags;
San Mehat9d2bd732009-09-22 16:44:22 -0700920
San Mehatc7fc9372009-11-22 17:19:07 -0800921 spin_lock_irqsave(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700922
San Mehatd0719e52009-12-03 10:58:54 -0800923 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700924
925 if (ios->clock) {
San Mehat9d2bd732009-09-22 16:44:22 -0700926 if (ios->clock != host->clk_rate) {
927 rc = clk_set_rate(host->clk, ios->clock);
928 if (rc < 0)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700929 pr_err("%s: Error setting clock rate (%d)\n",
930 mmc_hostname(host->mmc), rc);
San Mehat9d2bd732009-09-22 16:44:22 -0700931 else
932 host->clk_rate = ios->clock;
933 }
934 clk |= MCI_CLK_ENABLE;
935 }
936
937 if (ios->bus_width == MMC_BUS_WIDTH_4)
938 clk |= (2 << 10); /* Set WIDEBUS */
939
940 if (ios->clock > 400000 && msmsdcc_pwrsave)
941 clk |= (1 << 9); /* PWRSAVE */
942
943 clk |= (1 << 12); /* FLOW_ENA */
944 clk |= (1 << 15); /* feedback clock */
945
946 if (host->plat->translate_vdd)
947 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
948
949 switch (ios->power_mode) {
950 case MMC_POWER_OFF:
San Mehat9d2bd732009-09-22 16:44:22 -0700951 break;
952 case MMC_POWER_UP:
953 pwr |= MCI_PWR_UP;
954 break;
955 case MMC_POWER_ON:
San Mehat9d2bd732009-09-22 16:44:22 -0700956 pwr |= MCI_PWR_ON;
957 break;
958 }
959
960 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
961 pwr |= MCI_OD;
962
San Mehat8b1c2ba2009-11-16 10:17:30 -0800963 msmsdcc_writel(host, clk, MMCICLOCK);
San Mehat9d2bd732009-09-22 16:44:22 -0700964
965 if (host->pwr != pwr) {
966 host->pwr = pwr;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800967 msmsdcc_writel(host, pwr, MMCIPOWER);
San Mehat9d2bd732009-09-22 16:44:22 -0700968 }
San Mehatf4748492009-11-23 15:36:31 -0800969#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800970 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800971#endif
San Mehat4adbbcc2009-11-08 13:00:37 -0800972 spin_unlock_irqrestore(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700973}
974
975static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
976{
977 struct msmsdcc_host *host = mmc_priv(mmc);
978 unsigned long flags;
979 u32 status;
980
981 spin_lock_irqsave(&host->lock, flags);
982 if (msmsdcc_sdioirq == 1) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800983 status = msmsdcc_readl(host, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700984 if (enable)
985 status |= MCI_SDIOINTOPERMASK;
986 else
987 status &= ~MCI_SDIOINTOPERMASK;
988 host->saved_irq0mask = status;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800989 msmsdcc_writel(host, status, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700990 }
991 spin_unlock_irqrestore(&host->lock, flags);
992}
993
994static const struct mmc_host_ops msmsdcc_ops = {
995 .request = msmsdcc_request,
996 .set_ios = msmsdcc_set_ios,
997 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
998};
999
1000static void
1001msmsdcc_check_status(unsigned long data)
1002{
1003 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
1004 unsigned int status;
1005
1006 if (!host->plat->status) {
1007 mmc_detect_change(host->mmc, 0);
1008 goto out;
1009 }
1010
1011 status = host->plat->status(mmc_dev(host->mmc));
1012 host->eject = !status;
1013 if (status ^ host->oldstat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001014 pr_info("%s: Slot status change detected (%d -> %d)\n",
1015 mmc_hostname(host->mmc), host->oldstat, status);
San Mehat9d2bd732009-09-22 16:44:22 -07001016 if (status)
1017 mmc_detect_change(host->mmc, (5 * HZ) / 2);
1018 else
1019 mmc_detect_change(host->mmc, 0);
1020 }
1021
1022 host->oldstat = status;
1023
1024out:
1025 if (host->timer.function)
1026 mod_timer(&host->timer, jiffies + HZ);
1027}
1028
1029static irqreturn_t
1030msmsdcc_platform_status_irq(int irq, void *dev_id)
1031{
1032 struct msmsdcc_host *host = dev_id;
1033
1034 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
1035 msmsdcc_check_status((unsigned long) host);
1036 return IRQ_HANDLED;
1037}
1038
1039static void
1040msmsdcc_status_notify_cb(int card_present, void *dev_id)
1041{
1042 struct msmsdcc_host *host = dev_id;
1043
1044 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1045 card_present);
1046 msmsdcc_check_status((unsigned long) host);
1047}
1048
San Mehat9d2bd732009-09-22 16:44:22 -07001049static void
San Mehat865c80642009-11-13 13:42:06 -08001050msmsdcc_busclk_expired(unsigned long _data)
San Mehat9d2bd732009-09-22 16:44:22 -07001051{
1052 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
San Mehat9d2bd732009-09-22 16:44:22 -07001053
San Mehat865c80642009-11-13 13:42:06 -08001054 if (host->clks_on)
San Mehatc7fc9372009-11-22 17:19:07 -08001055 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001056}
1057
1058static int
1059msmsdcc_init_dma(struct msmsdcc_host *host)
1060{
1061 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1062 host->dma.host = host;
1063 host->dma.channel = -1;
1064
1065 if (!host->dmares)
1066 return -ENODEV;
1067
1068 host->dma.nc = dma_alloc_coherent(NULL,
1069 sizeof(struct msmsdcc_nc_dmadata),
1070 &host->dma.nc_busaddr,
1071 GFP_KERNEL);
1072 if (host->dma.nc == NULL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001073 pr_err("Unable to allocate DMA buffer\n");
San Mehat9d2bd732009-09-22 16:44:22 -07001074 return -ENOMEM;
1075 }
1076 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1077 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1078 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1079 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1080 host->dma.channel = host->dmares->start;
1081
1082 return 0;
1083}
1084
San Mehat9d2bd732009-09-22 16:44:22 -07001085static int
1086msmsdcc_probe(struct platform_device *pdev)
1087{
Sahitya Tummalab5d643d2010-07-29 16:55:34 +05301088 struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
San Mehat9d2bd732009-09-22 16:44:22 -07001089 struct msmsdcc_host *host;
1090 struct mmc_host *mmc;
1091 struct resource *cmd_irqres = NULL;
1092 struct resource *pio_irqres = NULL;
1093 struct resource *stat_irqres = NULL;
1094 struct resource *memres = NULL;
1095 struct resource *dmares = NULL;
1096 int ret;
1097
1098 /* must have platform data */
1099 if (!plat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001100 pr_err("%s: Platform data not available\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001101 ret = -EINVAL;
1102 goto out;
1103 }
1104
1105 if (pdev->id < 1 || pdev->id > 4)
1106 return -EINVAL;
1107
1108 if (pdev->resource == NULL || pdev->num_resources < 2) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001109 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001110 return -ENXIO;
1111 }
1112
1113 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1114 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1115 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1116 "cmd_irq");
1117 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1118 "pio_irq");
1119 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1120 "status_irq");
1121
1122 if (!cmd_irqres || !pio_irqres || !memres) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001123 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001124 return -ENXIO;
1125 }
1126
1127 /*
1128 * Setup our host structure
1129 */
1130
1131 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1132 if (!mmc) {
1133 ret = -ENOMEM;
1134 goto out;
1135 }
1136
1137 host = mmc_priv(mmc);
1138 host->pdev_id = pdev->id;
1139 host->plat = plat;
1140 host->mmc = mmc;
San Mehat56a8b5b2009-11-21 12:29:46 -08001141 host->curr.cmd = NULL;
San Mehat9d2bd732009-09-22 16:44:22 -07001142
1143 host->cmdpoll = 1;
1144
1145 host->base = ioremap(memres->start, PAGE_SIZE);
1146 if (!host->base) {
1147 ret = -ENOMEM;
1148 goto out;
1149 }
1150
1151 host->cmd_irqres = cmd_irqres;
1152 host->pio_irqres = pio_irqres;
1153 host->memres = memres;
1154 host->dmares = dmares;
1155 spin_lock_init(&host->lock);
1156
Sahitya Tummala62612cf2010-12-08 15:03:03 +05301157 tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
1158 (unsigned long)host);
1159
San Mehat9d2bd732009-09-22 16:44:22 -07001160 /*
1161 * Setup DMA
1162 */
1163 msmsdcc_init_dma(host);
1164
San Mehat4adbbcc2009-11-08 13:00:37 -08001165 /* Get our clocks */
San Mehat9d2bd732009-09-22 16:44:22 -07001166 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1167 if (IS_ERR(host->pclk)) {
1168 ret = PTR_ERR(host->pclk);
1169 goto host_free;
1170 }
1171
San Mehat9d2bd732009-09-22 16:44:22 -07001172 host->clk = clk_get(&pdev->dev, "sdc_clk");
1173 if (IS_ERR(host->clk)) {
1174 ret = PTR_ERR(host->clk);
San Mehat4adbbcc2009-11-08 13:00:37 -08001175 goto pclk_put;
San Mehat9d2bd732009-09-22 16:44:22 -07001176 }
1177
San Mehat4adbbcc2009-11-08 13:00:37 -08001178 /* Enable clocks */
San Mehatc7fc9372009-11-22 17:19:07 -08001179 ret = msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001180 if (ret)
1181 goto clk_put;
1182
1183 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1184 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001185 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001186 goto clk_disable;
1187 }
1188
San Mehat4adbbcc2009-11-08 13:00:37 -08001189 host->pclk_rate = clk_get_rate(host->pclk);
San Mehat9d2bd732009-09-22 16:44:22 -07001190 host->clk_rate = clk_get_rate(host->clk);
1191
San Mehat9d2bd732009-09-22 16:44:22 -07001192 /*
1193 * Setup MMC host structure
1194 */
1195 mmc->ops = &msmsdcc_ops;
1196 mmc->f_min = msmsdcc_fmin;
1197 mmc->f_max = msmsdcc_fmax;
1198 mmc->ocr_avail = plat->ocr_mask;
1199
1200 if (msmsdcc_4bit)
1201 mmc->caps |= MMC_CAP_4_BIT_DATA;
1202 if (msmsdcc_sdioirq)
1203 mmc->caps |= MMC_CAP_SDIO_IRQ;
1204 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1205
Martin K. Petersena36274e2010-09-10 01:33:59 -04001206 mmc->max_segs = NR_SG;
San Mehat9d2bd732009-09-22 16:44:22 -07001207 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1208 mmc->max_blk_count = 65536;
1209
1210 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1211 mmc->max_seg_size = mmc->max_req_size;
1212
San Mehat8b1c2ba2009-11-16 10:17:30 -08001213 msmsdcc_writel(host, 0, MMCIMASK0);
1214 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -07001215
San Mehat8b1c2ba2009-11-16 10:17:30 -08001216 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001217 host->saved_irq0mask = MCI_IRQENABLE;
1218
1219 /*
1220 * Setup card detect change
1221 */
1222
1223 memset(&host->timer, 0, sizeof(host->timer));
1224
1225 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1226 unsigned long irqflags = IRQF_SHARED |
1227 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1228
1229 host->stat_irq = stat_irqres->start;
1230 ret = request_irq(host->stat_irq,
1231 msmsdcc_platform_status_irq,
1232 irqflags,
1233 DRIVER_NAME " (slot)",
1234 host);
1235 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001236 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1237 mmc_hostname(mmc), host->stat_irq, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001238 goto clk_disable;
1239 }
1240 } else if (plat->register_status_notify) {
1241 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1242 } else if (!plat->status)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001243 pr_err("%s: No card detect facilities available\n",
San Mehat9d2bd732009-09-22 16:44:22 -07001244 mmc_hostname(mmc));
1245 else {
1246 init_timer(&host->timer);
1247 host->timer.data = (unsigned long)host;
1248 host->timer.function = msmsdcc_check_status;
1249 host->timer.expires = jiffies + HZ;
1250 add_timer(&host->timer);
1251 }
1252
1253 if (plat->status) {
1254 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1255 host->eject = !host->oldstat;
1256 }
1257
San Mehat865c80642009-11-13 13:42:06 -08001258 init_timer(&host->busclk_timer);
1259 host->busclk_timer.data = (unsigned long) host;
1260 host->busclk_timer.function = msmsdcc_busclk_expired;
San Mehat9d2bd732009-09-22 16:44:22 -07001261
1262 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1263 DRIVER_NAME " (cmd)", host);
1264 if (ret)
1265 goto stat_irq_free;
1266
1267 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1268 DRIVER_NAME " (pio)", host);
1269 if (ret)
1270 goto cmd_irq_free;
1271
1272 mmc_set_drvdata(pdev, mmc);
1273 mmc_add_host(mmc);
1274
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001275 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1276 mmc_hostname(mmc), (unsigned long long)memres->start,
1277 (unsigned int) cmd_irqres->start,
1278 (unsigned int) host->stat_irq, host->dma.channel);
1279 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1280 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1281 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1282 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1283 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1284 pr_info("%s: Power save feature enable = %d\n",
1285 mmc_hostname(mmc), msmsdcc_pwrsave);
San Mehat9d2bd732009-09-22 16:44:22 -07001286
1287 if (host->dma.channel != -1) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001288 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1289 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1290 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1291 mmc_hostname(mmc), host->dma.cmd_busaddr,
1292 host->dma.cmdptr_busaddr);
San Mehat9d2bd732009-09-22 16:44:22 -07001293 } else
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001294 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001295 if (host->timer.function)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001296 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001297
San Mehatf4748492009-11-23 15:36:31 -08001298#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001299 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001300#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001301 return 0;
1302 cmd_irq_free:
1303 free_irq(cmd_irqres->start, host);
1304 stat_irq_free:
1305 if (host->stat_irq)
1306 free_irq(host->stat_irq, host);
1307 clk_disable:
San Mehatc7fc9372009-11-22 17:19:07 -08001308 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001309 clk_put:
1310 clk_put(host->clk);
San Mehat9d2bd732009-09-22 16:44:22 -07001311 pclk_put:
1312 clk_put(host->pclk);
1313 host_free:
1314 mmc_free_host(mmc);
1315 out:
1316 return ret;
1317}
1318
Daniel Walker08ecfde2010-06-23 12:32:20 -07001319#ifdef CONFIG_PM
1320#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1321static void
1322do_resume_work(struct work_struct *work)
1323{
1324 struct msmsdcc_host *host =
1325 container_of(work, struct msmsdcc_host, resume_task);
1326 struct mmc_host *mmc = host->mmc;
1327
1328 if (mmc) {
1329 mmc_resume_host(mmc);
1330 if (host->stat_irq)
1331 enable_irq(host->stat_irq);
1332 }
1333}
1334#endif
1335
1336
San Mehat9d2bd732009-09-22 16:44:22 -07001337static int
1338msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1339{
1340 struct mmc_host *mmc = mmc_get_drvdata(dev);
1341 int rc = 0;
1342
1343 if (mmc) {
1344 struct msmsdcc_host *host = mmc_priv(mmc);
1345
1346 if (host->stat_irq)
1347 disable_irq(host->stat_irq);
1348
1349 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001350 rc = mmc_suspend_host(mmc);
San Mehatd0719e52009-12-03 10:58:54 -08001351 if (!rc)
San Mehat8b1c2ba2009-11-16 10:17:30 -08001352 msmsdcc_writel(host, 0, MMCIMASK0);
San Mehatc7fc9372009-11-22 17:19:07 -08001353 if (host->clks_on)
1354 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001355 }
1356 return rc;
1357}
1358
1359static int
1360msmsdcc_resume(struct platform_device *dev)
1361{
1362 struct mmc_host *mmc = mmc_get_drvdata(dev);
San Mehat9d2bd732009-09-22 16:44:22 -07001363
1364 if (mmc) {
1365 struct msmsdcc_host *host = mmc_priv(mmc);
1366
San Mehatc7fc9372009-11-22 17:19:07 -08001367 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001368
San Mehat8b1c2ba2009-11-16 10:17:30 -08001369 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001370
1371 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1372 mmc_resume_host(mmc);
Roel Kluin5b8a2fb2010-01-17 20:25:36 +01001373 if (host->stat_irq)
San Mehat9d2bd732009-09-22 16:44:22 -07001374 enable_irq(host->stat_irq);
San Mehatf4748492009-11-23 15:36:31 -08001375#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001376 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001377#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001378 }
1379 return 0;
1380}
Daniel Walker08ecfde2010-06-23 12:32:20 -07001381#else
1382#define msmsdcc_suspend 0
1383#define msmsdcc_resume 0
1384#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001385
1386static struct platform_driver msmsdcc_driver = {
1387 .probe = msmsdcc_probe,
1388 .suspend = msmsdcc_suspend,
1389 .resume = msmsdcc_resume,
1390 .driver = {
1391 .name = "msm_sdcc",
1392 },
1393};
1394
1395static int __init msmsdcc_init(void)
1396{
1397 return platform_driver_register(&msmsdcc_driver);
1398}
1399
1400static void __exit msmsdcc_exit(void)
1401{
1402 platform_driver_unregister(&msmsdcc_driver);
1403}
1404
1405module_init(msmsdcc_init);
1406module_exit(msmsdcc_exit);
1407
1408MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1409MODULE_LICENSE("GPL");