2 * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver
4 * Copyright (C) 2007 Google Inc,
5 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
6 * Copyright (C) 2009, Code Aurora Forum. All Rights Reserved.
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.
14 * Author: San Mehat (san@android.com)
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>
30 #include <linux/mmc/sdio.h>
31 #include <linux/clk.h>
32 #include <linux/scatterlist.h>
33 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/debugfs.h>
37 #include <linux/memory.h>
38 #include <linux/gfp.h>
39 #include <linux/gpio.h>
41 #include <asm/cacheflush.h>
42 #include <asm/div64.h>
43 #include <asm/sizes.h>
45 #include <linux/platform_data/mmc-msm_sdcc.h>
51 #define DRIVER_NAME "msm-sdcc"
53 #define BUSCLK_PWRSAVE 1
54 #define BUSCLK_TIMEOUT (HZ)
55 static unsigned int msmsdcc_fmin
= 144000;
56 static unsigned int msmsdcc_fmax
= 50000000;
57 static unsigned int msmsdcc_4bit
= 1;
58 static unsigned int msmsdcc_pwrsave
= 1;
59 static unsigned int msmsdcc_piopoll
= 1;
60 static unsigned int msmsdcc_sdioirq
;
62 #define PIO_SPINMAX 30
63 #define CMD_SPINMAX 20
67 msmsdcc_disable_clocks(struct msmsdcc_host
*host
, int deferr
)
69 WARN_ON(!host
->clks_on
);
71 BUG_ON(host
->curr
.mrq
);
74 mod_timer(&host
->busclk_timer
, jiffies
+ BUSCLK_TIMEOUT
);
76 del_timer_sync(&host
->busclk_timer
);
77 /* Need to check clks_on again in case the busclk
81 clk_disable(host
->clk
);
82 clk_disable(host
->pclk
);
89 msmsdcc_enable_clocks(struct msmsdcc_host
*host
)
93 del_timer_sync(&host
->busclk_timer
);
96 rc
= clk_enable(host
->pclk
);
99 rc
= clk_enable(host
->clk
);
101 clk_disable(host
->pclk
);
104 udelay(1 + ((3 * USEC_PER_SEC
) /
105 (host
->clk_rate
? host
->clk_rate
: msmsdcc_fmin
)));
111 static inline unsigned int
112 msmsdcc_readl(struct msmsdcc_host
*host
, unsigned int reg
)
114 return readl(host
->base
+ reg
);
118 msmsdcc_writel(struct msmsdcc_host
*host
, u32 data
, unsigned int reg
)
120 writel(data
, host
->base
+ reg
);
121 /* 3 clk delay required! */
122 udelay(1 + ((3 * USEC_PER_SEC
) /
123 (host
->clk_rate
? host
->clk_rate
: msmsdcc_fmin
)));
127 msmsdcc_start_command(struct msmsdcc_host
*host
, struct mmc_command
*cmd
,
130 static void msmsdcc_reset_and_restore(struct msmsdcc_host
*host
)
136 /* Save the controller state */
137 mci_clk
= readl(host
->base
+ MMCICLOCK
);
138 mci_mask0
= readl(host
->base
+ MMCIMASK0
);
140 /* Reset the controller */
141 ret
= clk_reset(host
->clk
, CLK_RESET_ASSERT
);
143 pr_err("%s: Clock assert failed at %u Hz with err %d\n",
144 mmc_hostname(host
->mmc
), host
->clk_rate
, ret
);
146 ret
= clk_reset(host
->clk
, CLK_RESET_DEASSERT
);
148 pr_err("%s: Clock deassert failed at %u Hz with err %d\n",
149 mmc_hostname(host
->mmc
), host
->clk_rate
, ret
);
151 pr_info("%s: Controller has been re-initialiazed\n",
152 mmc_hostname(host
->mmc
));
154 /* Restore the contoller state */
155 writel(host
->pwr
, host
->base
+ MMCIPOWER
);
156 writel(mci_clk
, host
->base
+ MMCICLOCK
);
157 writel(mci_mask0
, host
->base
+ MMCIMASK0
);
158 ret
= clk_set_rate(host
->clk
, host
->clk_rate
);
160 pr_err("%s: Failed to set clk rate %u Hz (%d)\n",
161 mmc_hostname(host
->mmc
), host
->clk_rate
, ret
);
165 msmsdcc_request_end(struct msmsdcc_host
*host
, struct mmc_request
*mrq
)
167 BUG_ON(host
->curr
.data
);
169 host
->curr
.mrq
= NULL
;
170 host
->curr
.cmd
= NULL
;
173 mrq
->data
->bytes_xfered
= host
->curr
.data_xfered
;
174 if (mrq
->cmd
->error
== -ETIMEDOUT
)
178 msmsdcc_disable_clocks(host
, 1);
181 * Need to drop the host lock here; mmc_request_done may call
182 * back into the driver...
184 spin_unlock(&host
->lock
);
185 mmc_request_done(host
->mmc
, mrq
);
186 spin_lock(&host
->lock
);
190 msmsdcc_stop_data(struct msmsdcc_host
*host
)
192 host
->curr
.data
= NULL
;
193 host
->curr
.got_dataend
= 0;
196 uint32_t msmsdcc_fifo_addr(struct msmsdcc_host
*host
)
198 return host
->memres
->start
+ MMCIFIFO
;
202 msmsdcc_start_command_exec(struct msmsdcc_host
*host
, u32 arg
, u32 c
) {
203 msmsdcc_writel(host
, arg
, MMCIARGUMENT
);
204 msmsdcc_writel(host
, c
, MMCICOMMAND
);
208 msmsdcc_dma_exec_func(struct msm_dmov_cmd
*cmd
)
210 struct msmsdcc_host
*host
= (struct msmsdcc_host
*)cmd
->data
;
212 msmsdcc_writel(host
, host
->cmd_timeout
, MMCIDATATIMER
);
213 msmsdcc_writel(host
, (unsigned int)host
->curr
.xfer_size
,
215 msmsdcc_writel(host
, (msmsdcc_readl(host
, MMCIMASK0
) &
216 (~MCI_IRQ_PIO
)) | host
->cmd_pio_irqmask
, MMCIMASK0
);
217 msmsdcc_writel(host
, host
->cmd_datactrl
, MMCIDATACTRL
);
220 msmsdcc_start_command_exec(host
,
221 (u32
) host
->cmd_cmd
->arg
,
224 host
->dma
.active
= 1;
228 msmsdcc_dma_complete_tlet(unsigned long data
)
230 struct msmsdcc_host
*host
= (struct msmsdcc_host
*)data
;
232 struct mmc_request
*mrq
;
233 struct msm_dmov_errdata err
;
235 spin_lock_irqsave(&host
->lock
, flags
);
236 host
->dma
.active
= 0;
239 mrq
= host
->curr
.mrq
;
243 if (!(host
->dma
.result
& DMOV_RSLT_VALID
)) {
244 pr_err("msmsdcc: Invalid DataMover result\n");
248 if (host
->dma
.result
& DMOV_RSLT_DONE
) {
249 host
->curr
.data_xfered
= host
->curr
.xfer_size
;
252 if (host
->dma
.result
& DMOV_RSLT_ERROR
)
253 pr_err("%s: DMA error (0x%.8x)\n",
254 mmc_hostname(host
->mmc
), host
->dma
.result
);
255 if (host
->dma
.result
& DMOV_RSLT_FLUSH
)
256 pr_err("%s: DMA channel flushed (0x%.8x)\n",
257 mmc_hostname(host
->mmc
), host
->dma
.result
);
259 pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
260 err
.flush
[0], err
.flush
[1], err
.flush
[2],
261 err
.flush
[3], err
.flush
[4], err
.flush
[5]);
263 msmsdcc_reset_and_restore(host
);
264 if (!mrq
->data
->error
)
265 mrq
->data
->error
= -EIO
;
267 dma_unmap_sg(mmc_dev(host
->mmc
), host
->dma
.sg
, host
->dma
.num_ents
,
273 if (host
->curr
.got_dataend
|| mrq
->data
->error
) {
276 * If we've already gotten our DATAEND / DATABLKEND
277 * for this request, then complete it through here.
279 msmsdcc_stop_data(host
);
281 if (!mrq
->data
->error
)
282 host
->curr
.data_xfered
= host
->curr
.xfer_size
;
283 if (!mrq
->data
->stop
|| mrq
->cmd
->error
) {
284 host
->curr
.mrq
= NULL
;
285 host
->curr
.cmd
= NULL
;
286 mrq
->data
->bytes_xfered
= host
->curr
.data_xfered
;
288 spin_unlock_irqrestore(&host
->lock
, flags
);
290 msmsdcc_disable_clocks(host
, 1);
292 mmc_request_done(host
->mmc
, mrq
);
295 msmsdcc_start_command(host
, mrq
->data
->stop
, 0);
299 spin_unlock_irqrestore(&host
->lock
, flags
);
304 msmsdcc_dma_complete_func(struct msm_dmov_cmd
*cmd
,
306 struct msm_dmov_errdata
*err
)
308 struct msmsdcc_dma_data
*dma_data
=
309 container_of(cmd
, struct msmsdcc_dma_data
, hdr
);
310 struct msmsdcc_host
*host
= dma_data
->host
;
312 dma_data
->result
= result
;
314 memcpy(&dma_data
->err
, err
, sizeof(struct msm_dmov_errdata
));
316 tasklet_schedule(&host
->dma_tlet
);
319 static int validate_dma(struct msmsdcc_host
*host
, struct mmc_data
*data
)
321 if (host
->dma
.channel
== -1)
324 if ((data
->blksz
* data
->blocks
) < MCI_FIFOSIZE
)
326 if ((data
->blksz
* data
->blocks
) % MCI_FIFOSIZE
)
331 static int msmsdcc_config_dma(struct msmsdcc_host
*host
, struct mmc_data
*data
)
333 struct msmsdcc_nc_dmadata
*nc
;
339 struct scatterlist
*sg
= data
->sg
;
341 rc
= validate_dma(host
, data
);
345 host
->dma
.sg
= data
->sg
;
346 host
->dma
.num_ents
= data
->sg_len
;
348 BUG_ON(host
->dma
.num_ents
> NR_SG
); /* Prevent memory corruption */
352 switch (host
->pdev_id
) {
354 crci
= MSMSDCC_CRCI_SDC1
;
357 crci
= MSMSDCC_CRCI_SDC2
;
360 crci
= MSMSDCC_CRCI_SDC3
;
363 crci
= MSMSDCC_CRCI_SDC4
;
367 host
->dma
.num_ents
= 0;
371 if (data
->flags
& MMC_DATA_READ
)
372 host
->dma
.dir
= DMA_FROM_DEVICE
;
374 host
->dma
.dir
= DMA_TO_DEVICE
;
376 host
->curr
.user_pages
= 0;
380 /* location of command block must be 64 bit aligned */
381 BUG_ON(host
->dma
.cmd_busaddr
& 0x07);
383 nc
->cmdptr
= (host
->dma
.cmd_busaddr
>> 3) | CMD_PTR_LP
;
384 host
->dma
.hdr
.cmdptr
= DMOV_CMD_PTR_LIST
|
385 DMOV_CMD_ADDR(host
->dma
.cmdptr_busaddr
);
386 host
->dma
.hdr
.complete_func
= msmsdcc_dma_complete_func
;
388 n
= dma_map_sg(mmc_dev(host
->mmc
), host
->dma
.sg
,
389 host
->dma
.num_ents
, host
->dma
.dir
);
391 pr_err("%s: Unable to map in all sg elements\n",
392 mmc_hostname(host
->mmc
));
394 host
->dma
.num_ents
= 0;
398 for_each_sg(host
->dma
.sg
, sg
, n
, i
) {
400 box
->cmd
= CMD_MODE_BOX
;
404 rows
= (sg_dma_len(sg
) % MCI_FIFOSIZE
) ?
405 (sg_dma_len(sg
) / MCI_FIFOSIZE
) + 1 :
406 (sg_dma_len(sg
) / MCI_FIFOSIZE
) ;
408 if (data
->flags
& MMC_DATA_READ
) {
409 box
->src_row_addr
= msmsdcc_fifo_addr(host
);
410 box
->dst_row_addr
= sg_dma_address(sg
);
412 box
->src_dst_len
= (MCI_FIFOSIZE
<< 16) |
414 box
->row_offset
= MCI_FIFOSIZE
;
416 box
->num_rows
= rows
* ((1 << 16) + 1);
417 box
->cmd
|= CMD_SRC_CRCI(crci
);
419 box
->src_row_addr
= sg_dma_address(sg
);
420 box
->dst_row_addr
= msmsdcc_fifo_addr(host
);
422 box
->src_dst_len
= (MCI_FIFOSIZE
<< 16) |
424 box
->row_offset
= (MCI_FIFOSIZE
<< 16);
426 box
->num_rows
= rows
* ((1 << 16) + 1);
427 box
->cmd
|= CMD_DST_CRCI(crci
);
436 snoop_cccr_abort(struct mmc_command
*cmd
)
438 if ((cmd
->opcode
== 52) &&
439 (cmd
->arg
& 0x80000000) &&
440 (((cmd
->arg
>> 9) & 0x1ffff) == SDIO_CCCR_ABORT
))
446 msmsdcc_start_command_deferred(struct msmsdcc_host
*host
,
447 struct mmc_command
*cmd
, u32
*c
)
449 *c
|= (cmd
->opcode
| MCI_CPSM_ENABLE
);
451 if (cmd
->flags
& MMC_RSP_PRESENT
) {
452 if (cmd
->flags
& MMC_RSP_136
)
453 *c
|= MCI_CPSM_LONGRSP
;
454 *c
|= MCI_CPSM_RESPONSE
;
458 *c
|= MCI_CPSM_INTERRUPT
;
460 if ((((cmd
->opcode
== 17) || (cmd
->opcode
== 18)) ||
461 ((cmd
->opcode
== 24) || (cmd
->opcode
== 25))) ||
463 *c
|= MCI_CSPM_DATCMD
;
465 if (host
->prog_scan
&& (cmd
->opcode
== 12)) {
466 *c
|= MCI_CPSM_PROGENA
;
467 host
->prog_enable
= true;
470 if (cmd
== cmd
->mrq
->stop
)
471 *c
|= MCI_CSPM_MCIABORT
;
473 if (snoop_cccr_abort(cmd
))
474 *c
|= MCI_CSPM_MCIABORT
;
476 if (host
->curr
.cmd
!= NULL
) {
477 pr_err("%s: Overlapping command requests\n",
478 mmc_hostname(host
->mmc
));
480 host
->curr
.cmd
= cmd
;
484 msmsdcc_start_data(struct msmsdcc_host
*host
, struct mmc_data
*data
,
485 struct mmc_command
*cmd
, u32 c
)
487 unsigned int datactrl
, timeout
;
488 unsigned long long clks
;
489 unsigned int pio_irqmask
= 0;
491 host
->curr
.data
= data
;
492 host
->curr
.xfer_size
= data
->blksz
* data
->blocks
;
493 host
->curr
.xfer_remain
= host
->curr
.xfer_size
;
494 host
->curr
.data_xfered
= 0;
495 host
->curr
.got_dataend
= 0;
497 memset(&host
->pio
, 0, sizeof(host
->pio
));
499 datactrl
= MCI_DPSM_ENABLE
| (data
->blksz
<< 4);
501 if (!msmsdcc_config_dma(host
, data
))
502 datactrl
|= MCI_DPSM_DMAENABLE
;
504 host
->pio
.sg
= data
->sg
;
505 host
->pio
.sg_len
= data
->sg_len
;
506 host
->pio
.sg_off
= 0;
508 if (data
->flags
& MMC_DATA_READ
) {
509 pio_irqmask
= MCI_RXFIFOHALFFULLMASK
;
510 if (host
->curr
.xfer_remain
< MCI_FIFOSIZE
)
511 pio_irqmask
|= MCI_RXDATAAVLBLMASK
;
513 pio_irqmask
= MCI_TXFIFOHALFEMPTYMASK
;
516 if (data
->flags
& MMC_DATA_READ
)
517 datactrl
|= MCI_DPSM_DIRECTION
;
519 clks
= (unsigned long long)data
->timeout_ns
* host
->clk_rate
;
520 do_div(clks
, NSEC_PER_SEC
);
521 timeout
= data
->timeout_clks
+ (unsigned int)clks
*2 ;
523 if (datactrl
& MCI_DPSM_DMAENABLE
) {
524 /* Save parameters for the exec function */
525 host
->cmd_timeout
= timeout
;
526 host
->cmd_pio_irqmask
= pio_irqmask
;
527 host
->cmd_datactrl
= datactrl
;
530 host
->dma
.hdr
.execute_func
= msmsdcc_dma_exec_func
;
531 host
->dma
.hdr
.data
= (void *)host
;
535 msmsdcc_start_command_deferred(host
, cmd
, &c
);
538 msm_dmov_enqueue_cmd(host
->dma
.channel
, &host
->dma
.hdr
);
539 if (data
->flags
& MMC_DATA_WRITE
)
540 host
->prog_scan
= true;
542 msmsdcc_writel(host
, timeout
, MMCIDATATIMER
);
544 msmsdcc_writel(host
, host
->curr
.xfer_size
, MMCIDATALENGTH
);
546 msmsdcc_writel(host
, (msmsdcc_readl(host
, MMCIMASK0
) &
547 (~MCI_IRQ_PIO
)) | pio_irqmask
, MMCIMASK0
);
549 msmsdcc_writel(host
, datactrl
, MMCIDATACTRL
);
552 /* Daisy-chain the command if requested */
553 msmsdcc_start_command(host
, cmd
, c
);
559 msmsdcc_start_command(struct msmsdcc_host
*host
, struct mmc_command
*cmd
, u32 c
)
561 if (cmd
== cmd
->mrq
->stop
)
562 c
|= MCI_CSPM_MCIABORT
;
566 msmsdcc_start_command_deferred(host
, cmd
, &c
);
567 msmsdcc_start_command_exec(host
, cmd
->arg
, c
);
571 msmsdcc_data_err(struct msmsdcc_host
*host
, struct mmc_data
*data
,
574 if (status
& MCI_DATACRCFAIL
) {
575 pr_err("%s: Data CRC error\n", mmc_hostname(host
->mmc
));
576 pr_err("%s: opcode 0x%.8x\n", __func__
,
577 data
->mrq
->cmd
->opcode
);
578 pr_err("%s: blksz %d, blocks %d\n", __func__
,
579 data
->blksz
, data
->blocks
);
580 data
->error
= -EILSEQ
;
581 } else if (status
& MCI_DATATIMEOUT
) {
582 pr_err("%s: Data timeout\n", mmc_hostname(host
->mmc
));
583 data
->error
= -ETIMEDOUT
;
584 } else if (status
& MCI_RXOVERRUN
) {
585 pr_err("%s: RX overrun\n", mmc_hostname(host
->mmc
));
587 } else if (status
& MCI_TXUNDERRUN
) {
588 pr_err("%s: TX underrun\n", mmc_hostname(host
->mmc
));
591 pr_err("%s: Unknown error (0x%.8x)\n",
592 mmc_hostname(host
->mmc
), status
);
599 msmsdcc_pio_read(struct msmsdcc_host
*host
, char *buffer
, unsigned int remain
)
601 uint32_t *ptr
= (uint32_t *) buffer
;
605 remain
= ((remain
>> 2) + 1) << 2;
607 while (msmsdcc_readl(host
, MMCISTATUS
) & MCI_RXDATAAVLBL
) {
608 *ptr
= msmsdcc_readl(host
, MMCIFIFO
+ (count
% MCI_FIFOSIZE
));
610 count
+= sizeof(uint32_t);
612 remain
-= sizeof(uint32_t);
620 msmsdcc_pio_write(struct msmsdcc_host
*host
, char *buffer
,
621 unsigned int remain
, u32 status
)
623 void __iomem
*base
= host
->base
;
627 unsigned int count
, maxcnt
, sz
;
629 maxcnt
= status
& MCI_TXFIFOEMPTY
? MCI_FIFOSIZE
:
631 count
= min(remain
, maxcnt
);
633 sz
= count
% 4 ? (count
>> 2) + 1 : (count
>> 2);
634 writesl(base
+ MMCIFIFO
, ptr
, sz
);
641 status
= msmsdcc_readl(host
, MMCISTATUS
);
642 } while (status
& MCI_TXFIFOHALFEMPTY
);
648 msmsdcc_spin_on_status(struct msmsdcc_host
*host
, uint32_t mask
, int maxspin
)
651 if ((msmsdcc_readl(host
, MMCISTATUS
) & mask
))
660 msmsdcc_pio_irq(int irq
, void *dev_id
)
662 struct msmsdcc_host
*host
= dev_id
;
666 status
= msmsdcc_readl(host
, MMCISTATUS
);
667 mci_mask0
= msmsdcc_readl(host
, MMCIMASK0
);
669 if (((mci_mask0
& status
) & MCI_IRQ_PIO
) == 0)
674 unsigned int remain
, len
;
677 if (!(status
& (MCI_TXFIFOHALFEMPTY
| MCI_RXDATAAVLBL
))) {
678 if (host
->curr
.xfer_remain
== 0 || !msmsdcc_piopoll
)
681 if (msmsdcc_spin_on_status(host
,
682 (MCI_TXFIFOHALFEMPTY
|
689 /* Map the current scatter buffer */
690 local_irq_save(flags
);
691 buffer
= kmap_atomic(sg_page(host
->pio
.sg
))
692 + host
->pio
.sg
->offset
;
693 buffer
+= host
->pio
.sg_off
;
694 remain
= host
->pio
.sg
->length
- host
->pio
.sg_off
;
696 if (status
& MCI_RXACTIVE
)
697 len
= msmsdcc_pio_read(host
, buffer
, remain
);
698 if (status
& MCI_TXACTIVE
)
699 len
= msmsdcc_pio_write(host
, buffer
, remain
, status
);
701 /* Unmap the buffer */
702 kunmap_atomic(buffer
);
703 local_irq_restore(flags
);
705 host
->pio
.sg_off
+= len
;
706 host
->curr
.xfer_remain
-= len
;
707 host
->curr
.data_xfered
+= len
;
711 /* This sg page is full - do some housekeeping */
712 if (status
& MCI_RXACTIVE
&& host
->curr
.user_pages
)
713 flush_dcache_page(sg_page(host
->pio
.sg
));
715 if (!--host
->pio
.sg_len
) {
716 memset(&host
->pio
, 0, sizeof(host
->pio
));
720 /* Advance to next sg */
722 host
->pio
.sg_off
= 0;
725 status
= msmsdcc_readl(host
, MMCISTATUS
);
728 if (status
& MCI_RXACTIVE
&& host
->curr
.xfer_remain
< MCI_FIFOSIZE
)
729 msmsdcc_writel(host
, (mci_mask0
& (~MCI_IRQ_PIO
)) |
730 MCI_RXDATAAVLBLMASK
, MMCIMASK0
);
732 if (!host
->curr
.xfer_remain
)
733 msmsdcc_writel(host
, (mci_mask0
& (~MCI_IRQ_PIO
)) | 0,
739 static void msmsdcc_do_cmdirq(struct msmsdcc_host
*host
, uint32_t status
)
741 struct mmc_command
*cmd
= host
->curr
.cmd
;
743 host
->curr
.cmd
= NULL
;
744 cmd
->resp
[0] = msmsdcc_readl(host
, MMCIRESPONSE0
);
745 cmd
->resp
[1] = msmsdcc_readl(host
, MMCIRESPONSE1
);
746 cmd
->resp
[2] = msmsdcc_readl(host
, MMCIRESPONSE2
);
747 cmd
->resp
[3] = msmsdcc_readl(host
, MMCIRESPONSE3
);
749 if (status
& MCI_CMDTIMEOUT
) {
750 cmd
->error
= -ETIMEDOUT
;
751 } else if (status
& MCI_CMDCRCFAIL
&&
752 cmd
->flags
& MMC_RSP_CRC
) {
753 pr_err("%s: Command CRC error\n", mmc_hostname(host
->mmc
));
754 cmd
->error
= -EILSEQ
;
757 if (!cmd
->data
|| cmd
->error
) {
758 if (host
->curr
.data
&& host
->dma
.sg
)
759 msm_dmov_stop_cmd(host
->dma
.channel
,
761 else if (host
->curr
.data
) { /* Non DMA */
762 msmsdcc_reset_and_restore(host
);
763 msmsdcc_stop_data(host
);
764 msmsdcc_request_end(host
, cmd
->mrq
);
765 } else { /* host->data == NULL */
766 if (!cmd
->error
&& host
->prog_enable
) {
767 if (status
& MCI_PROGDONE
) {
768 host
->prog_scan
= false;
769 host
->prog_enable
= false;
770 msmsdcc_request_end(host
, cmd
->mrq
);
772 host
->curr
.cmd
= cmd
;
775 if (host
->prog_enable
) {
776 host
->prog_scan
= false;
777 host
->prog_enable
= false;
779 msmsdcc_request_end(host
, cmd
->mrq
);
782 } else if (cmd
->data
)
783 if (!(cmd
->data
->flags
& MMC_DATA_READ
))
784 msmsdcc_start_data(host
, cmd
->data
,
789 msmsdcc_handle_irq_data(struct msmsdcc_host
*host
, u32 status
,
792 struct mmc_data
*data
= host
->curr
.data
;
794 if (status
& (MCI_CMDSENT
| MCI_CMDRESPEND
| MCI_CMDCRCFAIL
|
795 MCI_CMDTIMEOUT
| MCI_PROGDONE
) && host
->curr
.cmd
) {
796 msmsdcc_do_cmdirq(host
, status
);
802 /* Check for data errors */
803 if (status
& (MCI_DATACRCFAIL
| MCI_DATATIMEOUT
|
804 MCI_TXUNDERRUN
| MCI_RXOVERRUN
)) {
805 msmsdcc_data_err(host
, data
, status
);
806 host
->curr
.data_xfered
= 0;
808 msm_dmov_stop_cmd(host
->dma
.channel
,
811 msmsdcc_reset_and_restore(host
);
813 msmsdcc_stop_data(host
);
815 msmsdcc_request_end(host
, data
->mrq
);
817 msmsdcc_start_command(host
, data
->stop
, 0);
821 /* Check for data done */
822 if (!host
->curr
.got_dataend
&& (status
& MCI_DATAEND
))
823 host
->curr
.got_dataend
= 1;
826 * If DMA is still in progress, we complete via the completion handler
828 if (host
->curr
.got_dataend
&& !host
->dma
.busy
) {
830 * There appears to be an issue in the controller where
831 * if you request a small block transfer (< fifo size),
832 * you may get your DATAEND/DATABLKEND irq without the
835 * Check to see if there is still data to be read,
836 * and simulate a PIO irq.
838 if (readl(base
+ MMCISTATUS
) & MCI_RXDATAAVLBL
)
839 msmsdcc_pio_irq(1, host
);
841 msmsdcc_stop_data(host
);
843 host
->curr
.data_xfered
= host
->curr
.xfer_size
;
846 msmsdcc_request_end(host
, data
->mrq
);
848 msmsdcc_start_command(host
, data
->stop
, 0);
853 msmsdcc_irq(int irq
, void *dev_id
)
855 struct msmsdcc_host
*host
= dev_id
;
856 void __iomem
*base
= host
->base
;
861 spin_lock(&host
->lock
);
864 status
= msmsdcc_readl(host
, MMCISTATUS
);
865 status
&= msmsdcc_readl(host
, MMCIMASK0
);
866 if ((status
& (~MCI_IRQ_PIO
)) == 0)
868 msmsdcc_writel(host
, status
, MMCICLEAR
);
870 if (status
& MCI_SDIOINTR
)
871 status
&= ~MCI_SDIOINTR
;
876 msmsdcc_handle_irq_data(host
, status
, base
);
878 if (status
& MCI_SDIOINTOPER
) {
880 status
&= ~MCI_SDIOINTOPER
;
885 spin_unlock(&host
->lock
);
888 * We have to delay handling the card interrupt as it calls
889 * back into the driver.
892 mmc_signal_sdio_irq(host
->mmc
);
894 return IRQ_RETVAL(ret
);
898 msmsdcc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
900 struct msmsdcc_host
*host
= mmc_priv(mmc
);
903 WARN_ON(host
->curr
.mrq
!= NULL
);
904 WARN_ON(host
->pwr
== 0);
906 spin_lock_irqsave(&host
->lock
, flags
);
911 if (mrq
->data
&& !(mrq
->data
->flags
& MMC_DATA_READ
)) {
913 mrq
->data
->bytes_xfered
= mrq
->data
->blksz
*
916 mrq
->cmd
->error
= -ENOMEDIUM
;
918 spin_unlock_irqrestore(&host
->lock
, flags
);
919 mmc_request_done(mmc
, mrq
);
923 msmsdcc_enable_clocks(host
);
925 host
->curr
.mrq
= mrq
;
927 if (mrq
->data
&& mrq
->data
->flags
& MMC_DATA_READ
)
928 /* Queue/read data, daisy-chain command when data starts */
929 msmsdcc_start_data(host
, mrq
->data
, mrq
->cmd
, 0);
931 msmsdcc_start_command(host
, mrq
->cmd
, 0);
933 if (host
->cmdpoll
&& !msmsdcc_spin_on_status(host
,
934 MCI_CMDRESPEND
|MCI_CMDCRCFAIL
|MCI_CMDTIMEOUT
,
936 uint32_t status
= msmsdcc_readl(host
, MMCISTATUS
);
937 msmsdcc_do_cmdirq(host
, status
);
939 MCI_CMDRESPEND
| MCI_CMDCRCFAIL
| MCI_CMDTIMEOUT
,
941 host
->stats
.cmdpoll_hits
++;
943 host
->stats
.cmdpoll_misses
++;
945 spin_unlock_irqrestore(&host
->lock
, flags
);
948 static void msmsdcc_setup_gpio(struct msmsdcc_host
*host
, bool enable
)
950 struct msm_mmc_gpio_data
*curr
;
953 if (!host
->plat
->gpio_data
|| host
->gpio_config_status
== enable
)
956 curr
= host
->plat
->gpio_data
;
957 for (i
= 0; i
< curr
->size
; i
++) {
959 rc
= gpio_request(curr
->gpio
[i
].no
,
962 pr_err("%s: gpio_request(%d, %s) failed %d\n",
963 mmc_hostname(host
->mmc
),
965 curr
->gpio
[i
].name
, rc
);
969 gpio_free(curr
->gpio
[i
].no
);
972 host
->gpio_config_status
= enable
;
977 gpio_free(curr
->gpio
[i
].no
);
981 msmsdcc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
983 struct msmsdcc_host
*host
= mmc_priv(mmc
);
984 u32 clk
= 0, pwr
= 0;
988 spin_lock_irqsave(&host
->lock
, flags
);
990 msmsdcc_enable_clocks(host
);
992 spin_unlock_irqrestore(&host
->lock
, flags
);
995 if (ios
->clock
!= host
->clk_rate
) {
996 rc
= clk_set_rate(host
->clk
, ios
->clock
);
998 pr_err("%s: Error setting clock rate (%d)\n",
999 mmc_hostname(host
->mmc
), rc
);
1001 host
->clk_rate
= ios
->clock
;
1003 clk
|= MCI_CLK_ENABLE
;
1006 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1007 clk
|= (2 << 10); /* Set WIDEBUS */
1009 if (ios
->clock
> 400000 && msmsdcc_pwrsave
)
1010 clk
|= (1 << 9); /* PWRSAVE */
1012 clk
|= (1 << 12); /* FLOW_ENA */
1013 clk
|= (1 << 15); /* feedback clock */
1015 if (host
->plat
->translate_vdd
)
1016 pwr
|= host
->plat
->translate_vdd(mmc_dev(mmc
), ios
->vdd
);
1018 switch (ios
->power_mode
) {
1020 msmsdcc_setup_gpio(host
, false);
1024 msmsdcc_setup_gpio(host
, true);
1031 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
1034 msmsdcc_writel(host
, clk
, MMCICLOCK
);
1036 if (host
->pwr
!= pwr
) {
1038 msmsdcc_writel(host
, pwr
, MMCIPOWER
);
1041 spin_lock_irqsave(&host
->lock
, flags
);
1042 msmsdcc_disable_clocks(host
, 1);
1043 spin_unlock_irqrestore(&host
->lock
, flags
);
1047 static void msmsdcc_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1049 struct msmsdcc_host
*host
= mmc_priv(mmc
);
1050 unsigned long flags
;
1053 spin_lock_irqsave(&host
->lock
, flags
);
1054 if (msmsdcc_sdioirq
== 1) {
1055 status
= msmsdcc_readl(host
, MMCIMASK0
);
1057 status
|= MCI_SDIOINTOPERMASK
;
1059 status
&= ~MCI_SDIOINTOPERMASK
;
1060 host
->saved_irq0mask
= status
;
1061 msmsdcc_writel(host
, status
, MMCIMASK0
);
1063 spin_unlock_irqrestore(&host
->lock
, flags
);
1066 static void msmsdcc_init_card(struct mmc_host
*mmc
, struct mmc_card
*card
)
1068 struct msmsdcc_host
*host
= mmc_priv(mmc
);
1070 if (host
->plat
->init_card
)
1071 host
->plat
->init_card(card
);
1074 static const struct mmc_host_ops msmsdcc_ops
= {
1075 .request
= msmsdcc_request
,
1076 .set_ios
= msmsdcc_set_ios
,
1077 .enable_sdio_irq
= msmsdcc_enable_sdio_irq
,
1078 .init_card
= msmsdcc_init_card
,
1082 msmsdcc_check_status(unsigned long data
)
1084 struct msmsdcc_host
*host
= (struct msmsdcc_host
*)data
;
1085 unsigned int status
;
1087 if (!host
->plat
->status
) {
1088 mmc_detect_change(host
->mmc
, 0);
1092 status
= host
->plat
->status(mmc_dev(host
->mmc
));
1093 host
->eject
= !status
;
1094 if (status
^ host
->oldstat
) {
1095 pr_info("%s: Slot status change detected (%d -> %d)\n",
1096 mmc_hostname(host
->mmc
), host
->oldstat
, status
);
1098 mmc_detect_change(host
->mmc
, (5 * HZ
) / 2);
1100 mmc_detect_change(host
->mmc
, 0);
1103 host
->oldstat
= status
;
1106 if (host
->timer
.function
)
1107 mod_timer(&host
->timer
, jiffies
+ HZ
);
1111 msmsdcc_platform_status_irq(int irq
, void *dev_id
)
1113 struct msmsdcc_host
*host
= dev_id
;
1115 pr_debug("%s: %d\n", __func__
, irq
);
1116 msmsdcc_check_status((unsigned long) host
);
1121 msmsdcc_status_notify_cb(int card_present
, void *dev_id
)
1123 struct msmsdcc_host
*host
= dev_id
;
1125 pr_debug("%s: card_present %d\n", mmc_hostname(host
->mmc
),
1127 msmsdcc_check_status((unsigned long) host
);
1131 msmsdcc_busclk_expired(unsigned long _data
)
1133 struct msmsdcc_host
*host
= (struct msmsdcc_host
*) _data
;
1136 msmsdcc_disable_clocks(host
, 0);
1140 msmsdcc_init_dma(struct msmsdcc_host
*host
)
1142 memset(&host
->dma
, 0, sizeof(struct msmsdcc_dma_data
));
1143 host
->dma
.host
= host
;
1144 host
->dma
.channel
= -1;
1149 host
->dma
.nc
= dma_alloc_coherent(NULL
,
1150 sizeof(struct msmsdcc_nc_dmadata
),
1151 &host
->dma
.nc_busaddr
,
1153 if (host
->dma
.nc
== NULL
) {
1154 pr_err("Unable to allocate DMA buffer\n");
1157 memset(host
->dma
.nc
, 0x00, sizeof(struct msmsdcc_nc_dmadata
));
1158 host
->dma
.cmd_busaddr
= host
->dma
.nc_busaddr
;
1159 host
->dma
.cmdptr_busaddr
= host
->dma
.nc_busaddr
+
1160 offsetof(struct msmsdcc_nc_dmadata
, cmdptr
);
1161 host
->dma
.channel
= host
->dmares
->start
;
1167 msmsdcc_probe(struct platform_device
*pdev
)
1169 struct msm_mmc_platform_data
*plat
= pdev
->dev
.platform_data
;
1170 struct msmsdcc_host
*host
;
1171 struct mmc_host
*mmc
;
1172 struct resource
*cmd_irqres
= NULL
;
1173 struct resource
*stat_irqres
= NULL
;
1174 struct resource
*memres
= NULL
;
1175 struct resource
*dmares
= NULL
;
1178 /* must have platform data */
1180 pr_err("%s: Platform data not available\n", __func__
);
1185 if (pdev
->id
< 1 || pdev
->id
> 4)
1188 if (pdev
->resource
== NULL
|| pdev
->num_resources
< 2) {
1189 pr_err("%s: Invalid resource\n", __func__
);
1193 memres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1194 dmares
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1195 cmd_irqres
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
,
1197 stat_irqres
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
,
1200 if (!cmd_irqres
|| !memres
) {
1201 pr_err("%s: Invalid resource\n", __func__
);
1206 * Setup our host structure
1209 mmc
= mmc_alloc_host(sizeof(struct msmsdcc_host
), &pdev
->dev
);
1215 host
= mmc_priv(mmc
);
1216 host
->pdev_id
= pdev
->id
;
1219 host
->curr
.cmd
= NULL
;
1220 init_timer(&host
->busclk_timer
);
1221 host
->busclk_timer
.data
= (unsigned long) host
;
1222 host
->busclk_timer
.function
= msmsdcc_busclk_expired
;
1227 host
->base
= ioremap(memres
->start
, PAGE_SIZE
);
1233 host
->cmd_irqres
= cmd_irqres
;
1234 host
->memres
= memres
;
1235 host
->dmares
= dmares
;
1236 spin_lock_init(&host
->lock
);
1238 tasklet_init(&host
->dma_tlet
, msmsdcc_dma_complete_tlet
,
1239 (unsigned long)host
);
1245 ret
= msmsdcc_init_dma(host
);
1249 host
->dma
.channel
= -1;
1252 /* Get our clocks */
1253 host
->pclk
= clk_get(&pdev
->dev
, "sdc_pclk");
1254 if (IS_ERR(host
->pclk
)) {
1255 ret
= PTR_ERR(host
->pclk
);
1259 host
->clk
= clk_get(&pdev
->dev
, "sdc_clk");
1260 if (IS_ERR(host
->clk
)) {
1261 ret
= PTR_ERR(host
->clk
);
1265 ret
= clk_set_rate(host
->clk
, msmsdcc_fmin
);
1267 pr_err("%s: Clock rate set failed (%d)\n", __func__
, ret
);
1271 ret
= clk_prepare(host
->pclk
);
1275 ret
= clk_prepare(host
->clk
);
1277 goto clk_unprepare_p
;
1280 ret
= msmsdcc_enable_clocks(host
);
1284 host
->pclk_rate
= clk_get_rate(host
->pclk
);
1285 host
->clk_rate
= clk_get_rate(host
->clk
);
1288 * Setup MMC host structure
1290 mmc
->ops
= &msmsdcc_ops
;
1291 mmc
->f_min
= msmsdcc_fmin
;
1292 mmc
->f_max
= msmsdcc_fmax
;
1293 mmc
->ocr_avail
= plat
->ocr_mask
;
1296 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1297 if (msmsdcc_sdioirq
)
1298 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
1299 mmc
->caps
|= MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_SD_HIGHSPEED
;
1301 mmc
->max_segs
= NR_SG
;
1302 mmc
->max_blk_size
= 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1303 mmc
->max_blk_count
= 65536;
1305 mmc
->max_req_size
= 33554432; /* MCI_DATA_LENGTH is 25 bits */
1306 mmc
->max_seg_size
= mmc
->max_req_size
;
1308 msmsdcc_writel(host
, 0, MMCIMASK0
);
1309 msmsdcc_writel(host
, 0x5e007ff, MMCICLEAR
);
1311 msmsdcc_writel(host
, MCI_IRQENABLE
, MMCIMASK0
);
1312 host
->saved_irq0mask
= MCI_IRQENABLE
;
1315 * Setup card detect change
1318 memset(&host
->timer
, 0, sizeof(host
->timer
));
1320 if (stat_irqres
&& !(stat_irqres
->flags
& IORESOURCE_DISABLED
)) {
1321 unsigned long irqflags
= IRQF_SHARED
|
1322 (stat_irqres
->flags
& IRQF_TRIGGER_MASK
);
1324 host
->stat_irq
= stat_irqres
->start
;
1325 ret
= request_irq(host
->stat_irq
,
1326 msmsdcc_platform_status_irq
,
1328 DRIVER_NAME
" (slot)",
1331 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1332 mmc_hostname(mmc
), host
->stat_irq
, ret
);
1335 } else if (plat
->register_status_notify
) {
1336 plat
->register_status_notify(msmsdcc_status_notify_cb
, host
);
1337 } else if (!plat
->status
)
1338 pr_err("%s: No card detect facilities available\n",
1341 init_timer(&host
->timer
);
1342 host
->timer
.data
= (unsigned long)host
;
1343 host
->timer
.function
= msmsdcc_check_status
;
1344 host
->timer
.expires
= jiffies
+ HZ
;
1345 add_timer(&host
->timer
);
1349 host
->oldstat
= host
->plat
->status(mmc_dev(host
->mmc
));
1350 host
->eject
= !host
->oldstat
;
1353 ret
= request_irq(cmd_irqres
->start
, msmsdcc_irq
, IRQF_SHARED
,
1354 DRIVER_NAME
" (cmd)", host
);
1358 ret
= request_irq(cmd_irqres
->start
, msmsdcc_pio_irq
, IRQF_SHARED
,
1359 DRIVER_NAME
" (pio)", host
);
1363 mmc_set_drvdata(pdev
, mmc
);
1366 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1367 mmc_hostname(mmc
), (unsigned long long)memres
->start
,
1368 (unsigned int) cmd_irqres
->start
,
1369 (unsigned int) host
->stat_irq
, host
->dma
.channel
);
1370 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc
),
1371 (mmc
->caps
& MMC_CAP_4_BIT_DATA
? "enabled" : "disabled"));
1372 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1373 mmc_hostname(mmc
), msmsdcc_fmin
, msmsdcc_fmax
, host
->pclk_rate
);
1374 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc
), host
->eject
);
1375 pr_info("%s: Power save feature enable = %d\n",
1376 mmc_hostname(mmc
), msmsdcc_pwrsave
);
1378 if (host
->dma
.channel
!= -1) {
1379 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1380 mmc_hostname(mmc
), host
->dma
.nc
, host
->dma
.nc_busaddr
);
1381 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1382 mmc_hostname(mmc
), host
->dma
.cmd_busaddr
,
1383 host
->dma
.cmdptr_busaddr
);
1385 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc
));
1386 if (host
->timer
.function
)
1387 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc
));
1391 free_irq(cmd_irqres
->start
, host
);
1394 free_irq(host
->stat_irq
, host
);
1396 msmsdcc_disable_clocks(host
, 0);
1398 clk_unprepare(host
->clk
);
1400 clk_unprepare(host
->pclk
);
1404 clk_put(host
->pclk
);
1407 dma_free_coherent(NULL
, sizeof(struct msmsdcc_nc_dmadata
),
1408 host
->dma
.nc
, host
->dma
.nc_busaddr
);
1410 tasklet_kill(&host
->dma_tlet
);
1411 iounmap(host
->base
);
1419 #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1421 do_resume_work(struct work_struct
*work
)
1423 struct msmsdcc_host
*host
=
1424 container_of(work
, struct msmsdcc_host
, resume_task
);
1425 struct mmc_host
*mmc
= host
->mmc
;
1428 mmc_resume_host(mmc
);
1430 enable_irq(host
->stat_irq
);
1437 msmsdcc_suspend(struct platform_device
*dev
, pm_message_t state
)
1439 struct mmc_host
*mmc
= mmc_get_drvdata(dev
);
1443 struct msmsdcc_host
*host
= mmc_priv(mmc
);
1446 disable_irq(host
->stat_irq
);
1448 if (mmc
->card
&& mmc
->card
->type
!= MMC_TYPE_SDIO
)
1449 rc
= mmc_suspend_host(mmc
);
1451 msmsdcc_writel(host
, 0, MMCIMASK0
);
1453 msmsdcc_disable_clocks(host
, 0);
1459 msmsdcc_resume(struct platform_device
*dev
)
1461 struct mmc_host
*mmc
= mmc_get_drvdata(dev
);
1464 struct msmsdcc_host
*host
= mmc_priv(mmc
);
1466 msmsdcc_enable_clocks(host
);
1468 msmsdcc_writel(host
, host
->saved_irq0mask
, MMCIMASK0
);
1470 if (mmc
->card
&& mmc
->card
->type
!= MMC_TYPE_SDIO
)
1471 mmc_resume_host(mmc
);
1473 enable_irq(host
->stat_irq
);
1475 msmsdcc_disable_clocks(host
, 1);
1481 #define msmsdcc_suspend 0
1482 #define msmsdcc_resume 0
1485 static struct platform_driver msmsdcc_driver
= {
1486 .probe
= msmsdcc_probe
,
1487 .suspend
= msmsdcc_suspend
,
1488 .resume
= msmsdcc_resume
,
1494 module_platform_driver(msmsdcc_driver
);
1496 MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1497 MODULE_LICENSE("GPL");