blob: b147971a96ef0a4c267c1b7453f3f6a8d6548ffb [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
441 if (cmd == cmd->mrq->stop)
442 *c |= MCI_CSPM_MCIABORT;
443
444 if (snoop_cccr_abort(cmd))
445 *c |= MCI_CSPM_MCIABORT;
446
447 if (host->curr.cmd != NULL) {
448 printk(KERN_ERR "%s: Overlapping command requests\n",
449 mmc_hostname(host->mmc));
450 }
451 host->curr.cmd = cmd;
452}
453
454static void
455msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
456 struct mmc_command *cmd, u32 c)
San Mehat9d2bd732009-09-22 16:44:22 -0700457{
458 unsigned int datactrl, timeout;
459 unsigned long long clks;
San Mehat9d2bd732009-09-22 16:44:22 -0700460 unsigned int pio_irqmask = 0;
461
462 host->curr.data = data;
463 host->curr.xfer_size = data->blksz * data->blocks;
464 host->curr.xfer_remain = host->curr.xfer_size;
465 host->curr.data_xfered = 0;
466 host->curr.got_dataend = 0;
467 host->curr.got_datablkend = 0;
468
469 memset(&host->pio, 0, sizeof(host->pio));
470
San Mehat9d2bd732009-09-22 16:44:22 -0700471 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
472
473 if (!msmsdcc_config_dma(host, data))
474 datactrl |= MCI_DPSM_DMAENABLE;
475 else {
476 host->pio.sg = data->sg;
477 host->pio.sg_len = data->sg_len;
478 host->pio.sg_off = 0;
479
480 if (data->flags & MMC_DATA_READ) {
481 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
482 if (host->curr.xfer_remain < MCI_FIFOSIZE)
483 pio_irqmask |= MCI_RXDATAAVLBLMASK;
484 } else
485 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
486 }
487
488 if (data->flags & MMC_DATA_READ)
489 datactrl |= MCI_DPSM_DIRECTION;
490
San Mehat56a8b5b2009-11-21 12:29:46 -0800491 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
492 do_div(clks, NSEC_PER_SEC);
493 timeout = data->timeout_clks + (unsigned int)clks*2 ;
San Mehat9d2bd732009-09-22 16:44:22 -0700494
495 if (datactrl & MCI_DPSM_DMAENABLE) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800496 /* Save parameters for the exec function */
497 host->cmd_timeout = timeout;
498 host->cmd_pio_irqmask = pio_irqmask;
499 host->cmd_datactrl = datactrl;
500 host->cmd_cmd = cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700501
San Mehat56a8b5b2009-11-21 12:29:46 -0800502 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
503 host->dma.hdr.data = (void *)host;
San Mehat9d2bd732009-09-22 16:44:22 -0700504 host->dma.busy = 1;
San Mehat56a8b5b2009-11-21 12:29:46 -0800505
506 if (cmd) {
507 msmsdcc_start_command_deferred(host, cmd, &c);
508 host->cmd_c = c;
509 }
San Mehat9d2bd732009-09-22 16:44:22 -0700510 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
San Mehat56a8b5b2009-11-21 12:29:46 -0800511 } else {
512 msmsdcc_writel(host, timeout, MMCIDATATIMER);
513
514 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
515
516 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
517 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
518
519 if (cmd) {
520 /* Daisy-chain the command if requested */
521 msmsdcc_start_command(host, cmd, c);
522 }
San Mehat9d2bd732009-09-22 16:44:22 -0700523 }
524}
525
526static void
527msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
528{
San Mehat9d2bd732009-09-22 16:44:22 -0700529 if (cmd == cmd->mrq->stop)
530 c |= MCI_CSPM_MCIABORT;
531
San Mehat9d2bd732009-09-22 16:44:22 -0700532 host->stats.cmds++;
533
San Mehat56a8b5b2009-11-21 12:29:46 -0800534 msmsdcc_start_command_deferred(host, cmd, &c);
535 msmsdcc_start_command_exec(host, cmd->arg, c);
San Mehat9d2bd732009-09-22 16:44:22 -0700536}
537
538static void
539msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
540 unsigned int status)
541{
542 if (status & MCI_DATACRCFAIL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700543 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
544 pr_err("%s: opcode 0x%.8x\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700545 data->mrq->cmd->opcode);
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700546 pr_err("%s: blksz %d, blocks %d\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700547 data->blksz, data->blocks);
548 data->error = -EILSEQ;
549 } else if (status & MCI_DATATIMEOUT) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700550 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700551 data->error = -ETIMEDOUT;
552 } else if (status & MCI_RXOVERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700553 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700554 data->error = -EIO;
555 } else if (status & MCI_TXUNDERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700556 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700557 data->error = -EIO;
558 } else {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700559 pr_err("%s: Unknown error (0x%.8x)\n",
560 mmc_hostname(host->mmc), status);
San Mehat9d2bd732009-09-22 16:44:22 -0700561 data->error = -EIO;
562 }
563}
564
565
566static int
567msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
568{
San Mehat9d2bd732009-09-22 16:44:22 -0700569 uint32_t *ptr = (uint32_t *) buffer;
570 int count = 0;
571
San Mehat8b1c2ba2009-11-16 10:17:30 -0800572 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
573 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
San Mehat9d2bd732009-09-22 16:44:22 -0700574 ptr++;
575 count += sizeof(uint32_t);
576
577 remain -= sizeof(uint32_t);
578 if (remain == 0)
579 break;
580 }
581 return count;
582}
583
584static int
585msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
586 unsigned int remain, u32 status)
587{
588 void __iomem *base = host->base;
589 char *ptr = buffer;
590
591 do {
592 unsigned int count, maxcnt;
593
594 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
595 MCI_FIFOHALFSIZE;
596 count = min(remain, maxcnt);
597
598 writesl(base + MMCIFIFO, ptr, count >> 2);
599 ptr += count;
600 remain -= count;
601
602 if (remain == 0)
603 break;
604
San Mehat8b1c2ba2009-11-16 10:17:30 -0800605 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700606 } while (status & MCI_TXFIFOHALFEMPTY);
607
608 return ptr - buffer;
609}
610
611static int
612msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
613{
614 while (maxspin) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800615 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
San Mehat9d2bd732009-09-22 16:44:22 -0700616 return 0;
617 udelay(1);
618 --maxspin;
619 }
620 return -ETIMEDOUT;
621}
622
San Mehat1cd22962010-02-03 12:59:29 -0800623static irqreturn_t
San Mehat9d2bd732009-09-22 16:44:22 -0700624msmsdcc_pio_irq(int irq, void *dev_id)
625{
626 struct msmsdcc_host *host = dev_id;
San Mehat9d2bd732009-09-22 16:44:22 -0700627 uint32_t status;
628
San Mehat8b1c2ba2009-11-16 10:17:30 -0800629 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700630
631 do {
632 unsigned long flags;
633 unsigned int remain, len;
634 char *buffer;
635
636 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
637 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
638 break;
639
640 if (msmsdcc_spin_on_status(host,
641 (MCI_TXFIFOHALFEMPTY |
642 MCI_RXDATAAVLBL),
643 PIO_SPINMAX)) {
644 break;
645 }
646 }
647
648 /* Map the current scatter buffer */
649 local_irq_save(flags);
650 buffer = kmap_atomic(sg_page(host->pio.sg),
651 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
652 buffer += host->pio.sg_off;
653 remain = host->pio.sg->length - host->pio.sg_off;
654 len = 0;
655 if (status & MCI_RXACTIVE)
656 len = msmsdcc_pio_read(host, buffer, remain);
657 if (status & MCI_TXACTIVE)
658 len = msmsdcc_pio_write(host, buffer, remain, status);
659
660 /* Unmap the buffer */
661 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
662 local_irq_restore(flags);
663
664 host->pio.sg_off += len;
665 host->curr.xfer_remain -= len;
666 host->curr.data_xfered += len;
667 remain -= len;
668
669 if (remain == 0) {
670 /* This sg page is full - do some housekeeping */
671 if (status & MCI_RXACTIVE && host->curr.user_pages)
672 flush_dcache_page(sg_page(host->pio.sg));
673
674 if (!--host->pio.sg_len) {
675 memset(&host->pio, 0, sizeof(host->pio));
676 break;
677 }
678
679 /* Advance to next sg */
680 host->pio.sg++;
681 host->pio.sg_off = 0;
682 }
683
San Mehat8b1c2ba2009-11-16 10:17:30 -0800684 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700685 } while (1);
686
687 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800688 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700689
690 if (!host->curr.xfer_remain)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800691 msmsdcc_writel(host, 0, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700692
693 return IRQ_HANDLED;
694}
695
696static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
697{
698 struct mmc_command *cmd = host->curr.cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700699
700 host->curr.cmd = NULL;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800701 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
702 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
703 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
704 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
San Mehat9d2bd732009-09-22 16:44:22 -0700705
San Mehat9d2bd732009-09-22 16:44:22 -0700706 if (status & MCI_CMDTIMEOUT) {
707 cmd->error = -ETIMEDOUT;
708 } else if (status & MCI_CMDCRCFAIL &&
709 cmd->flags & MMC_RSP_CRC) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700710 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700711 cmd->error = -EILSEQ;
712 }
713
714 if (!cmd->data || cmd->error) {
715 if (host->curr.data && host->dma.sg)
716 msm_dmov_stop_cmd(host->dma.channel,
717 &host->dma.hdr, 0);
718 else if (host->curr.data) { /* Non DMA */
719 msmsdcc_stop_data(host);
720 msmsdcc_request_end(host, cmd->mrq);
721 } else /* host->data == NULL */
722 msmsdcc_request_end(host, cmd->mrq);
San Mehat56a8b5b2009-11-21 12:29:46 -0800723 } else if (cmd->data)
724 if (!(cmd->data->flags & MMC_DATA_READ))
725 msmsdcc_start_data(host, cmd->data,
726 NULL, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700727}
728
Joe Perchesb5a74d62009-09-22 16:44:25 -0700729static void
730msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
731 void __iomem *base)
732{
733 struct mmc_data *data = host->curr.data;
734
San Mehat56a8b5b2009-11-21 12:29:46 -0800735 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
736 MCI_CMDTIMEOUT) && host->curr.cmd) {
737 msmsdcc_do_cmdirq(host, status);
738 }
739
Joe Perchesb5a74d62009-09-22 16:44:25 -0700740 if (!data)
741 return;
742
743 /* Check for data errors */
744 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
745 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
746 msmsdcc_data_err(host, data, status);
747 host->curr.data_xfered = 0;
748 if (host->dma.sg)
749 msm_dmov_stop_cmd(host->dma.channel,
750 &host->dma.hdr, 0);
751 else {
San Mehatb3b0ca82009-11-24 12:24:55 -0800752 if (host->curr.data)
753 msmsdcc_stop_data(host);
Joe Perchesb5a74d62009-09-22 16:44:25 -0700754 if (!data->stop)
755 msmsdcc_request_end(host, data->mrq);
756 else
757 msmsdcc_start_command(host, data->stop, 0);
758 }
759 }
760
761 /* Check for data done */
762 if (!host->curr.got_dataend && (status & MCI_DATAEND))
763 host->curr.got_dataend = 1;
764
765 if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND))
766 host->curr.got_datablkend = 1;
767
768 /*
769 * If DMA is still in progress, we complete via the completion handler
770 */
771 if (host->curr.got_dataend && host->curr.got_datablkend &&
772 !host->dma.busy) {
773 /*
774 * There appears to be an issue in the controller where
775 * if you request a small block transfer (< fifo size),
776 * you may get your DATAEND/DATABLKEND irq without the
777 * PIO data irq.
778 *
779 * Check to see if there is still data to be read,
780 * and simulate a PIO irq.
781 */
782 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
783 msmsdcc_pio_irq(1, host);
784
785 msmsdcc_stop_data(host);
786 if (!data->error)
787 host->curr.data_xfered = host->curr.xfer_size;
788
789 if (!data->stop)
790 msmsdcc_request_end(host, data->mrq);
791 else
792 msmsdcc_start_command(host, data->stop, 0);
793 }
794}
795
San Mehat9d2bd732009-09-22 16:44:22 -0700796static irqreturn_t
797msmsdcc_irq(int irq, void *dev_id)
798{
799 struct msmsdcc_host *host = dev_id;
800 void __iomem *base = host->base;
801 u32 status;
802 int ret = 0;
803 int cardint = 0;
804
805 spin_lock(&host->lock);
806
807 do {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800808 status = msmsdcc_readl(host, MMCISTATUS);
809 status &= (msmsdcc_readl(host, MMCIMASK0) |
810 MCI_DATABLOCKENDMASK);
811 msmsdcc_writel(host, status, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700812
San Mehat865c80642009-11-13 13:42:06 -0800813 if (status & MCI_SDIOINTR)
814 status &= ~MCI_SDIOINTR;
815
816 if (!status)
817 break;
San Mehat9d2bd732009-09-22 16:44:22 -0700818
Joe Perchesb5a74d62009-09-22 16:44:25 -0700819 msmsdcc_handle_irq_data(host, status, base);
San Mehat9d2bd732009-09-22 16:44:22 -0700820
San Mehat9d2bd732009-09-22 16:44:22 -0700821 if (status & MCI_SDIOINTOPER) {
822 cardint = 1;
823 status &= ~MCI_SDIOINTOPER;
824 }
825 ret = 1;
826 } while (status);
827
828 spin_unlock(&host->lock);
829
830 /*
831 * We have to delay handling the card interrupt as it calls
832 * back into the driver.
833 */
834 if (cardint)
835 mmc_signal_sdio_irq(host->mmc);
836
837 return IRQ_RETVAL(ret);
838}
839
840static void
841msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
842{
843 struct msmsdcc_host *host = mmc_priv(mmc);
844 unsigned long flags;
845
846 WARN_ON(host->curr.mrq != NULL);
847 WARN_ON(host->pwr == 0);
848
849 spin_lock_irqsave(&host->lock, flags);
850
851 host->stats.reqs++;
852
853 if (host->eject) {
854 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
855 mrq->cmd->error = 0;
856 mrq->data->bytes_xfered = mrq->data->blksz *
857 mrq->data->blocks;
858 } else
859 mrq->cmd->error = -ENOMEDIUM;
860
861 spin_unlock_irqrestore(&host->lock, flags);
862 mmc_request_done(mmc, mrq);
863 return;
864 }
865
San Mehatd0719e52009-12-03 10:58:54 -0800866 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700867
San Mehat9d2bd732009-09-22 16:44:22 -0700868 host->curr.mrq = mrq;
869
870 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
San Mehat56a8b5b2009-11-21 12:29:46 -0800871 /* Queue/read data, daisy-chain command when data starts */
872 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
873 else
874 msmsdcc_start_command(host, mrq->cmd, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700875
876 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
877 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
878 CMD_SPINMAX)) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800879 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700880 msmsdcc_do_cmdirq(host, status);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800881 msmsdcc_writel(host,
882 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
883 MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700884 host->stats.cmdpoll_hits++;
885 } else {
886 host->stats.cmdpoll_misses++;
San Mehat9d2bd732009-09-22 16:44:22 -0700887 }
888 spin_unlock_irqrestore(&host->lock, flags);
889}
890
891static void
892msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
893{
894 struct msmsdcc_host *host = mmc_priv(mmc);
895 u32 clk = 0, pwr = 0;
896 int rc;
San Mehat4adbbcc2009-11-08 13:00:37 -0800897 unsigned long flags;
San Mehat9d2bd732009-09-22 16:44:22 -0700898
San Mehatc7fc9372009-11-22 17:19:07 -0800899 spin_lock_irqsave(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700900
San Mehatd0719e52009-12-03 10:58:54 -0800901 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700902
903 if (ios->clock) {
San Mehat9d2bd732009-09-22 16:44:22 -0700904 if (ios->clock != host->clk_rate) {
905 rc = clk_set_rate(host->clk, ios->clock);
906 if (rc < 0)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700907 pr_err("%s: Error setting clock rate (%d)\n",
908 mmc_hostname(host->mmc), rc);
San Mehat9d2bd732009-09-22 16:44:22 -0700909 else
910 host->clk_rate = ios->clock;
911 }
912 clk |= MCI_CLK_ENABLE;
913 }
914
915 if (ios->bus_width == MMC_BUS_WIDTH_4)
916 clk |= (2 << 10); /* Set WIDEBUS */
917
918 if (ios->clock > 400000 && msmsdcc_pwrsave)
919 clk |= (1 << 9); /* PWRSAVE */
920
921 clk |= (1 << 12); /* FLOW_ENA */
922 clk |= (1 << 15); /* feedback clock */
923
924 if (host->plat->translate_vdd)
925 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
926
927 switch (ios->power_mode) {
928 case MMC_POWER_OFF:
San Mehat9d2bd732009-09-22 16:44:22 -0700929 break;
930 case MMC_POWER_UP:
931 pwr |= MCI_PWR_UP;
932 break;
933 case MMC_POWER_ON:
San Mehat9d2bd732009-09-22 16:44:22 -0700934 pwr |= MCI_PWR_ON;
935 break;
936 }
937
938 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
939 pwr |= MCI_OD;
940
San Mehat8b1c2ba2009-11-16 10:17:30 -0800941 msmsdcc_writel(host, clk, MMCICLOCK);
San Mehat9d2bd732009-09-22 16:44:22 -0700942
943 if (host->pwr != pwr) {
944 host->pwr = pwr;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800945 msmsdcc_writel(host, pwr, MMCIPOWER);
San Mehat9d2bd732009-09-22 16:44:22 -0700946 }
San Mehatf4748492009-11-23 15:36:31 -0800947#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800948 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800949#endif
San Mehat4adbbcc2009-11-08 13:00:37 -0800950 spin_unlock_irqrestore(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700951}
952
953static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
954{
955 struct msmsdcc_host *host = mmc_priv(mmc);
956 unsigned long flags;
957 u32 status;
958
959 spin_lock_irqsave(&host->lock, flags);
960 if (msmsdcc_sdioirq == 1) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800961 status = msmsdcc_readl(host, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700962 if (enable)
963 status |= MCI_SDIOINTOPERMASK;
964 else
965 status &= ~MCI_SDIOINTOPERMASK;
966 host->saved_irq0mask = status;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800967 msmsdcc_writel(host, status, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700968 }
969 spin_unlock_irqrestore(&host->lock, flags);
970}
971
972static const struct mmc_host_ops msmsdcc_ops = {
973 .request = msmsdcc_request,
974 .set_ios = msmsdcc_set_ios,
975 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
976};
977
978static void
979msmsdcc_check_status(unsigned long data)
980{
981 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
982 unsigned int status;
983
984 if (!host->plat->status) {
985 mmc_detect_change(host->mmc, 0);
986 goto out;
987 }
988
989 status = host->plat->status(mmc_dev(host->mmc));
990 host->eject = !status;
991 if (status ^ host->oldstat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700992 pr_info("%s: Slot status change detected (%d -> %d)\n",
993 mmc_hostname(host->mmc), host->oldstat, status);
San Mehat9d2bd732009-09-22 16:44:22 -0700994 if (status)
995 mmc_detect_change(host->mmc, (5 * HZ) / 2);
996 else
997 mmc_detect_change(host->mmc, 0);
998 }
999
1000 host->oldstat = status;
1001
1002out:
1003 if (host->timer.function)
1004 mod_timer(&host->timer, jiffies + HZ);
1005}
1006
1007static irqreturn_t
1008msmsdcc_platform_status_irq(int irq, void *dev_id)
1009{
1010 struct msmsdcc_host *host = dev_id;
1011
1012 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
1013 msmsdcc_check_status((unsigned long) host);
1014 return IRQ_HANDLED;
1015}
1016
1017static void
1018msmsdcc_status_notify_cb(int card_present, void *dev_id)
1019{
1020 struct msmsdcc_host *host = dev_id;
1021
1022 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1023 card_present);
1024 msmsdcc_check_status((unsigned long) host);
1025}
1026
San Mehat9d2bd732009-09-22 16:44:22 -07001027static void
San Mehat865c80642009-11-13 13:42:06 -08001028msmsdcc_busclk_expired(unsigned long _data)
San Mehat9d2bd732009-09-22 16:44:22 -07001029{
1030 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
San Mehat9d2bd732009-09-22 16:44:22 -07001031
San Mehat865c80642009-11-13 13:42:06 -08001032 if (host->clks_on)
San Mehatc7fc9372009-11-22 17:19:07 -08001033 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001034}
1035
1036static int
1037msmsdcc_init_dma(struct msmsdcc_host *host)
1038{
1039 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1040 host->dma.host = host;
1041 host->dma.channel = -1;
1042
1043 if (!host->dmares)
1044 return -ENODEV;
1045
1046 host->dma.nc = dma_alloc_coherent(NULL,
1047 sizeof(struct msmsdcc_nc_dmadata),
1048 &host->dma.nc_busaddr,
1049 GFP_KERNEL);
1050 if (host->dma.nc == NULL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001051 pr_err("Unable to allocate DMA buffer\n");
San Mehat9d2bd732009-09-22 16:44:22 -07001052 return -ENOMEM;
1053 }
1054 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1055 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1056 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1057 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1058 host->dma.channel = host->dmares->start;
1059
1060 return 0;
1061}
1062
San Mehat9d2bd732009-09-22 16:44:22 -07001063static int
1064msmsdcc_probe(struct platform_device *pdev)
1065{
Sahitya Tummalab5d643d2010-07-29 16:55:34 +05301066 struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
San Mehat9d2bd732009-09-22 16:44:22 -07001067 struct msmsdcc_host *host;
1068 struct mmc_host *mmc;
1069 struct resource *cmd_irqres = NULL;
1070 struct resource *pio_irqres = NULL;
1071 struct resource *stat_irqres = NULL;
1072 struct resource *memres = NULL;
1073 struct resource *dmares = NULL;
1074 int ret;
1075
1076 /* must have platform data */
1077 if (!plat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001078 pr_err("%s: Platform data not available\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001079 ret = -EINVAL;
1080 goto out;
1081 }
1082
1083 if (pdev->id < 1 || pdev->id > 4)
1084 return -EINVAL;
1085
1086 if (pdev->resource == NULL || pdev->num_resources < 2) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001087 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001088 return -ENXIO;
1089 }
1090
1091 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1092 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1093 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1094 "cmd_irq");
1095 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1096 "pio_irq");
1097 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1098 "status_irq");
1099
1100 if (!cmd_irqres || !pio_irqres || !memres) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001101 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001102 return -ENXIO;
1103 }
1104
1105 /*
1106 * Setup our host structure
1107 */
1108
1109 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1110 if (!mmc) {
1111 ret = -ENOMEM;
1112 goto out;
1113 }
1114
1115 host = mmc_priv(mmc);
1116 host->pdev_id = pdev->id;
1117 host->plat = plat;
1118 host->mmc = mmc;
San Mehat56a8b5b2009-11-21 12:29:46 -08001119 host->curr.cmd = NULL;
San Mehat9d2bd732009-09-22 16:44:22 -07001120
1121 host->cmdpoll = 1;
1122
1123 host->base = ioremap(memres->start, PAGE_SIZE);
1124 if (!host->base) {
1125 ret = -ENOMEM;
1126 goto out;
1127 }
1128
1129 host->cmd_irqres = cmd_irqres;
1130 host->pio_irqres = pio_irqres;
1131 host->memres = memres;
1132 host->dmares = dmares;
1133 spin_lock_init(&host->lock);
1134
Sahitya Tummala62612cf2010-12-08 15:03:03 +05301135 tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
1136 (unsigned long)host);
1137
San Mehat9d2bd732009-09-22 16:44:22 -07001138 /*
1139 * Setup DMA
1140 */
1141 msmsdcc_init_dma(host);
1142
San Mehat4adbbcc2009-11-08 13:00:37 -08001143 /* Get our clocks */
San Mehat9d2bd732009-09-22 16:44:22 -07001144 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1145 if (IS_ERR(host->pclk)) {
1146 ret = PTR_ERR(host->pclk);
1147 goto host_free;
1148 }
1149
San Mehat9d2bd732009-09-22 16:44:22 -07001150 host->clk = clk_get(&pdev->dev, "sdc_clk");
1151 if (IS_ERR(host->clk)) {
1152 ret = PTR_ERR(host->clk);
San Mehat4adbbcc2009-11-08 13:00:37 -08001153 goto pclk_put;
San Mehat9d2bd732009-09-22 16:44:22 -07001154 }
1155
San Mehat4adbbcc2009-11-08 13:00:37 -08001156 /* Enable clocks */
San Mehatc7fc9372009-11-22 17:19:07 -08001157 ret = msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001158 if (ret)
1159 goto clk_put;
1160
1161 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1162 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001163 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001164 goto clk_disable;
1165 }
1166
San Mehat4adbbcc2009-11-08 13:00:37 -08001167 host->pclk_rate = clk_get_rate(host->pclk);
San Mehat9d2bd732009-09-22 16:44:22 -07001168 host->clk_rate = clk_get_rate(host->clk);
1169
San Mehat9d2bd732009-09-22 16:44:22 -07001170 /*
1171 * Setup MMC host structure
1172 */
1173 mmc->ops = &msmsdcc_ops;
1174 mmc->f_min = msmsdcc_fmin;
1175 mmc->f_max = msmsdcc_fmax;
1176 mmc->ocr_avail = plat->ocr_mask;
1177
1178 if (msmsdcc_4bit)
1179 mmc->caps |= MMC_CAP_4_BIT_DATA;
1180 if (msmsdcc_sdioirq)
1181 mmc->caps |= MMC_CAP_SDIO_IRQ;
1182 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1183
Martin K. Petersena36274e2010-09-10 01:33:59 -04001184 mmc->max_segs = NR_SG;
San Mehat9d2bd732009-09-22 16:44:22 -07001185 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1186 mmc->max_blk_count = 65536;
1187
1188 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1189 mmc->max_seg_size = mmc->max_req_size;
1190
San Mehat8b1c2ba2009-11-16 10:17:30 -08001191 msmsdcc_writel(host, 0, MMCIMASK0);
1192 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -07001193
San Mehat8b1c2ba2009-11-16 10:17:30 -08001194 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001195 host->saved_irq0mask = MCI_IRQENABLE;
1196
1197 /*
1198 * Setup card detect change
1199 */
1200
1201 memset(&host->timer, 0, sizeof(host->timer));
1202
1203 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1204 unsigned long irqflags = IRQF_SHARED |
1205 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1206
1207 host->stat_irq = stat_irqres->start;
1208 ret = request_irq(host->stat_irq,
1209 msmsdcc_platform_status_irq,
1210 irqflags,
1211 DRIVER_NAME " (slot)",
1212 host);
1213 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001214 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1215 mmc_hostname(mmc), host->stat_irq, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001216 goto clk_disable;
1217 }
1218 } else if (plat->register_status_notify) {
1219 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1220 } else if (!plat->status)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001221 pr_err("%s: No card detect facilities available\n",
San Mehat9d2bd732009-09-22 16:44:22 -07001222 mmc_hostname(mmc));
1223 else {
1224 init_timer(&host->timer);
1225 host->timer.data = (unsigned long)host;
1226 host->timer.function = msmsdcc_check_status;
1227 host->timer.expires = jiffies + HZ;
1228 add_timer(&host->timer);
1229 }
1230
1231 if (plat->status) {
1232 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1233 host->eject = !host->oldstat;
1234 }
1235
San Mehat865c80642009-11-13 13:42:06 -08001236 init_timer(&host->busclk_timer);
1237 host->busclk_timer.data = (unsigned long) host;
1238 host->busclk_timer.function = msmsdcc_busclk_expired;
San Mehat9d2bd732009-09-22 16:44:22 -07001239
1240 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1241 DRIVER_NAME " (cmd)", host);
1242 if (ret)
1243 goto stat_irq_free;
1244
1245 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1246 DRIVER_NAME " (pio)", host);
1247 if (ret)
1248 goto cmd_irq_free;
1249
1250 mmc_set_drvdata(pdev, mmc);
1251 mmc_add_host(mmc);
1252
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001253 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1254 mmc_hostname(mmc), (unsigned long long)memres->start,
1255 (unsigned int) cmd_irqres->start,
1256 (unsigned int) host->stat_irq, host->dma.channel);
1257 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1258 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1259 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1260 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1261 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1262 pr_info("%s: Power save feature enable = %d\n",
1263 mmc_hostname(mmc), msmsdcc_pwrsave);
San Mehat9d2bd732009-09-22 16:44:22 -07001264
1265 if (host->dma.channel != -1) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001266 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1267 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1268 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1269 mmc_hostname(mmc), host->dma.cmd_busaddr,
1270 host->dma.cmdptr_busaddr);
San Mehat9d2bd732009-09-22 16:44:22 -07001271 } else
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001272 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001273 if (host->timer.function)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001274 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001275
San Mehatf4748492009-11-23 15:36:31 -08001276#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001277 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001278#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001279 return 0;
1280 cmd_irq_free:
1281 free_irq(cmd_irqres->start, host);
1282 stat_irq_free:
1283 if (host->stat_irq)
1284 free_irq(host->stat_irq, host);
1285 clk_disable:
San Mehatc7fc9372009-11-22 17:19:07 -08001286 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001287 clk_put:
1288 clk_put(host->clk);
San Mehat9d2bd732009-09-22 16:44:22 -07001289 pclk_put:
1290 clk_put(host->pclk);
1291 host_free:
1292 mmc_free_host(mmc);
1293 out:
1294 return ret;
1295}
1296
Daniel Walker08ecfde2010-06-23 12:32:20 -07001297#ifdef CONFIG_PM
1298#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1299static void
1300do_resume_work(struct work_struct *work)
1301{
1302 struct msmsdcc_host *host =
1303 container_of(work, struct msmsdcc_host, resume_task);
1304 struct mmc_host *mmc = host->mmc;
1305
1306 if (mmc) {
1307 mmc_resume_host(mmc);
1308 if (host->stat_irq)
1309 enable_irq(host->stat_irq);
1310 }
1311}
1312#endif
1313
1314
San Mehat9d2bd732009-09-22 16:44:22 -07001315static int
1316msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1317{
1318 struct mmc_host *mmc = mmc_get_drvdata(dev);
1319 int rc = 0;
1320
1321 if (mmc) {
1322 struct msmsdcc_host *host = mmc_priv(mmc);
1323
1324 if (host->stat_irq)
1325 disable_irq(host->stat_irq);
1326
1327 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001328 rc = mmc_suspend_host(mmc);
San Mehatd0719e52009-12-03 10:58:54 -08001329 if (!rc)
San Mehat8b1c2ba2009-11-16 10:17:30 -08001330 msmsdcc_writel(host, 0, MMCIMASK0);
San Mehatc7fc9372009-11-22 17:19:07 -08001331 if (host->clks_on)
1332 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001333 }
1334 return rc;
1335}
1336
1337static int
1338msmsdcc_resume(struct platform_device *dev)
1339{
1340 struct mmc_host *mmc = mmc_get_drvdata(dev);
San Mehat9d2bd732009-09-22 16:44:22 -07001341
1342 if (mmc) {
1343 struct msmsdcc_host *host = mmc_priv(mmc);
1344
San Mehatc7fc9372009-11-22 17:19:07 -08001345 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001346
San Mehat8b1c2ba2009-11-16 10:17:30 -08001347 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001348
1349 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1350 mmc_resume_host(mmc);
Roel Kluin5b8a2fb2010-01-17 20:25:36 +01001351 if (host->stat_irq)
San Mehat9d2bd732009-09-22 16:44:22 -07001352 enable_irq(host->stat_irq);
San Mehatf4748492009-11-23 15:36:31 -08001353#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001354 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001355#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001356 }
1357 return 0;
1358}
Daniel Walker08ecfde2010-06-23 12:32:20 -07001359#else
1360#define msmsdcc_suspend 0
1361#define msmsdcc_resume 0
1362#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001363
1364static struct platform_driver msmsdcc_driver = {
1365 .probe = msmsdcc_probe,
1366 .suspend = msmsdcc_suspend,
1367 .resume = msmsdcc_resume,
1368 .driver = {
1369 .name = "msm_sdcc",
1370 },
1371};
1372
1373static int __init msmsdcc_init(void)
1374{
1375 return platform_driver_register(&msmsdcc_driver);
1376}
1377
1378static void __exit msmsdcc_exit(void)
1379{
1380 platform_driver_unregister(&msmsdcc_driver);
1381}
1382
1383module_init(msmsdcc_init);
1384module_exit(msmsdcc_exit);
1385
1386MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1387MODULE_LICENSE("GPL");