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