2 * linux/drivers/mmc/mmci.c - ARM PrimeCell MMCI PL180/1 driver
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/ioport.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/highmem.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/protocol.h>
21 #include <linux/amba/bus.h>
22 #include <linux/clk.h>
24 #include <asm/cacheflush.h>
25 #include <asm/div64.h>
27 #include <asm/scatterlist.h>
28 #include <asm/sizes.h>
29 #include <asm/mach/mmc.h>
33 #define DRIVER_NAME "mmci-pl18x"
35 #define DBG(host,fmt,args...) \
36 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
38 static unsigned int fmax
= 515633;
41 mmci_request_end(struct mmci_host
*host
, struct mmc_request
*mrq
)
43 writel(0, host
->base
+ MMCICOMMAND
);
49 mrq
->data
->bytes_xfered
= host
->data_xfered
;
52 * Need to drop the host lock here; mmc_request_done may call
53 * back into the driver...
55 spin_unlock(&host
->lock
);
56 mmc_request_done(host
->mmc
, mrq
);
57 spin_lock(&host
->lock
);
60 static void mmci_stop_data(struct mmci_host
*host
)
62 writel(0, host
->base
+ MMCIDATACTRL
);
63 writel(0, host
->base
+ MMCIMASK1
);
67 static void mmci_start_data(struct mmci_host
*host
, struct mmc_data
*data
)
69 unsigned int datactrl
, timeout
, irqmask
;
70 unsigned long long clks
;
73 DBG(host
, "blksz %04x blks %04x flags %08x\n",
74 1 << data
->blksz_bits
, data
->blocks
, data
->flags
);
77 host
->size
= data
->blocks
<< data
->blksz_bits
;
78 host
->data_xfered
= 0;
80 mmci_init_sg(host
, data
);
82 clks
= (unsigned long long)data
->timeout_ns
* host
->cclk
;
83 do_div(clks
, 1000000000UL);
85 timeout
= data
->timeout_clks
+ (unsigned int)clks
;
88 writel(timeout
, base
+ MMCIDATATIMER
);
89 writel(host
->size
, base
+ MMCIDATALENGTH
);
91 datactrl
= MCI_DPSM_ENABLE
| data
->blksz_bits
<< 4;
92 if (data
->flags
& MMC_DATA_READ
) {
93 datactrl
|= MCI_DPSM_DIRECTION
;
94 irqmask
= MCI_RXFIFOHALFFULLMASK
;
97 * If we have less than a FIFOSIZE of bytes to transfer,
98 * trigger a PIO interrupt as soon as any data is available.
100 if (host
->size
< MCI_FIFOSIZE
)
101 irqmask
|= MCI_RXDATAAVLBLMASK
;
104 * We don't actually need to include "FIFO empty" here
105 * since its implicit in "FIFO half empty".
107 irqmask
= MCI_TXFIFOHALFEMPTYMASK
;
110 writel(datactrl
, base
+ MMCIDATACTRL
);
111 writel(readl(base
+ MMCIMASK0
) & ~MCI_DATAENDMASK
, base
+ MMCIMASK0
);
112 writel(irqmask
, base
+ MMCIMASK1
);
116 mmci_start_command(struct mmci_host
*host
, struct mmc_command
*cmd
, u32 c
)
118 void __iomem
*base
= host
->base
;
120 DBG(host
, "op %02x arg %08x flags %08x\n",
121 cmd
->opcode
, cmd
->arg
, cmd
->flags
);
123 if (readl(base
+ MMCICOMMAND
) & MCI_CPSM_ENABLE
) {
124 writel(0, base
+ MMCICOMMAND
);
128 c
|= cmd
->opcode
| MCI_CPSM_ENABLE
;
129 if (cmd
->flags
& MMC_RSP_PRESENT
) {
130 if (cmd
->flags
& MMC_RSP_136
)
131 c
|= MCI_CPSM_LONGRSP
;
132 c
|= MCI_CPSM_RESPONSE
;
135 c
|= MCI_CPSM_INTERRUPT
;
139 writel(cmd
->arg
, base
+ MMCIARGUMENT
);
140 writel(c
, base
+ MMCICOMMAND
);
144 mmci_data_irq(struct mmci_host
*host
, struct mmc_data
*data
,
147 if (status
& MCI_DATABLOCKEND
) {
148 host
->data_xfered
+= 1 << data
->blksz_bits
;
150 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|MCI_RXOVERRUN
)) {
151 if (status
& MCI_DATACRCFAIL
)
152 data
->error
= MMC_ERR_BADCRC
;
153 else if (status
& MCI_DATATIMEOUT
)
154 data
->error
= MMC_ERR_TIMEOUT
;
155 else if (status
& (MCI_TXUNDERRUN
|MCI_RXOVERRUN
))
156 data
->error
= MMC_ERR_FIFO
;
157 status
|= MCI_DATAEND
;
160 * We hit an error condition. Ensure that any data
161 * partially written to a page is properly coherent.
163 if (host
->sg_len
&& data
->flags
& MMC_DATA_READ
)
164 flush_dcache_page(host
->sg_ptr
->page
);
166 if (status
& MCI_DATAEND
) {
167 mmci_stop_data(host
);
170 mmci_request_end(host
, data
->mrq
);
172 mmci_start_command(host
, data
->stop
, 0);
178 mmci_cmd_irq(struct mmci_host
*host
, struct mmc_command
*cmd
,
181 void __iomem
*base
= host
->base
;
185 cmd
->resp
[0] = readl(base
+ MMCIRESPONSE0
);
186 cmd
->resp
[1] = readl(base
+ MMCIRESPONSE1
);
187 cmd
->resp
[2] = readl(base
+ MMCIRESPONSE2
);
188 cmd
->resp
[3] = readl(base
+ MMCIRESPONSE3
);
190 if (status
& MCI_CMDTIMEOUT
) {
191 cmd
->error
= MMC_ERR_TIMEOUT
;
192 } else if (status
& MCI_CMDCRCFAIL
&& cmd
->flags
& MMC_RSP_CRC
) {
193 cmd
->error
= MMC_ERR_BADCRC
;
196 if (!cmd
->data
|| cmd
->error
!= MMC_ERR_NONE
) {
197 mmci_request_end(host
, cmd
->mrq
);
198 } else if (!(cmd
->data
->flags
& MMC_DATA_READ
)) {
199 mmci_start_data(host
, cmd
->data
);
203 static int mmci_pio_read(struct mmci_host
*host
, char *buffer
, unsigned int remain
)
205 void __iomem
*base
= host
->base
;
210 int count
= host
->size
- (readl(base
+ MMCIFIFOCNT
) << 2);
218 readsl(base
+ MMCIFIFO
, ptr
, count
>> 2);
226 status
= readl(base
+ MMCISTATUS
);
227 } while (status
& MCI_RXDATAAVLBL
);
232 static int mmci_pio_write(struct mmci_host
*host
, char *buffer
, unsigned int remain
, u32 status
)
234 void __iomem
*base
= host
->base
;
238 unsigned int count
, maxcnt
;
240 maxcnt
= status
& MCI_TXFIFOEMPTY
? MCI_FIFOSIZE
: MCI_FIFOHALFSIZE
;
241 count
= min(remain
, maxcnt
);
243 writesl(base
+ MMCIFIFO
, ptr
, count
>> 2);
251 status
= readl(base
+ MMCISTATUS
);
252 } while (status
& MCI_TXFIFOHALFEMPTY
);
258 * PIO data transfer IRQ handler.
260 static irqreturn_t
mmci_pio_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
262 struct mmci_host
*host
= dev_id
;
263 void __iomem
*base
= host
->base
;
266 status
= readl(base
+ MMCISTATUS
);
268 DBG(host
, "irq1 %08x\n", status
);
272 unsigned int remain
, len
;
276 * For write, we only need to test the half-empty flag
277 * here - if the FIFO is completely empty, then by
278 * definition it is more than half empty.
280 * For read, check for data available.
282 if (!(status
& (MCI_TXFIFOHALFEMPTY
|MCI_RXDATAAVLBL
)))
286 * Map the current scatter buffer.
288 buffer
= mmci_kmap_atomic(host
, &flags
) + host
->sg_off
;
289 remain
= host
->sg_ptr
->length
- host
->sg_off
;
292 if (status
& MCI_RXACTIVE
)
293 len
= mmci_pio_read(host
, buffer
, remain
);
294 if (status
& MCI_TXACTIVE
)
295 len
= mmci_pio_write(host
, buffer
, remain
, status
);
300 mmci_kunmap_atomic(host
, buffer
, &flags
);
310 * If we were reading, and we have completed this
311 * page, ensure that the data cache is coherent.
313 if (status
& MCI_RXACTIVE
)
314 flush_dcache_page(host
->sg_ptr
->page
);
316 if (!mmci_next_sg(host
))
319 status
= readl(base
+ MMCISTATUS
);
323 * If we're nearing the end of the read, switch to
324 * "any data available" mode.
326 if (status
& MCI_RXACTIVE
&& host
->size
< MCI_FIFOSIZE
)
327 writel(MCI_RXDATAAVLBLMASK
, base
+ MMCIMASK1
);
330 * If we run out of data, disable the data IRQs; this
331 * prevents a race where the FIFO becomes empty before
332 * the chip itself has disabled the data path, and
333 * stops us racing with our data end IRQ.
335 if (host
->size
== 0) {
336 writel(0, base
+ MMCIMASK1
);
337 writel(readl(base
+ MMCIMASK0
) | MCI_DATAENDMASK
, base
+ MMCIMASK0
);
344 * Handle completion of command and data transfers.
346 static irqreturn_t
mmci_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
348 struct mmci_host
*host
= dev_id
;
352 spin_lock(&host
->lock
);
355 struct mmc_command
*cmd
;
356 struct mmc_data
*data
;
358 status
= readl(host
->base
+ MMCISTATUS
);
359 status
&= readl(host
->base
+ MMCIMASK0
);
360 writel(status
, host
->base
+ MMCICLEAR
);
362 DBG(host
, "irq0 %08x\n", status
);
365 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|
366 MCI_RXOVERRUN
|MCI_DATAEND
|MCI_DATABLOCKEND
) && data
)
367 mmci_data_irq(host
, data
, status
);
370 if (status
& (MCI_CMDCRCFAIL
|MCI_CMDTIMEOUT
|MCI_CMDSENT
|MCI_CMDRESPEND
) && cmd
)
371 mmci_cmd_irq(host
, cmd
, status
);
376 spin_unlock(&host
->lock
);
378 return IRQ_RETVAL(ret
);
381 static void mmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
383 struct mmci_host
*host
= mmc_priv(mmc
);
385 WARN_ON(host
->mrq
!= NULL
);
387 spin_lock_irq(&host
->lock
);
391 if (mrq
->data
&& mrq
->data
->flags
& MMC_DATA_READ
)
392 mmci_start_data(host
, mrq
->data
);
394 mmci_start_command(host
, mrq
->cmd
, 0);
396 spin_unlock_irq(&host
->lock
);
399 static void mmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
401 struct mmci_host
*host
= mmc_priv(mmc
);
402 u32 clk
= 0, pwr
= 0;
405 if (ios
->clock
>= host
->mclk
) {
406 clk
= MCI_CLK_BYPASS
;
407 host
->cclk
= host
->mclk
;
409 clk
= host
->mclk
/ (2 * ios
->clock
) - 1;
412 host
->cclk
= host
->mclk
/ (2 * (clk
+ 1));
414 clk
|= MCI_CLK_ENABLE
;
417 if (host
->plat
->translate_vdd
)
418 pwr
|= host
->plat
->translate_vdd(mmc_dev(mmc
), ios
->vdd
);
420 switch (ios
->power_mode
) {
431 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
434 writel(clk
, host
->base
+ MMCICLOCK
);
436 if (host
->pwr
!= pwr
) {
438 writel(pwr
, host
->base
+ MMCIPOWER
);
442 static struct mmc_host_ops mmci_ops
= {
443 .request
= mmci_request
,
444 .set_ios
= mmci_set_ios
,
447 static void mmci_check_status(unsigned long data
)
449 struct mmci_host
*host
= (struct mmci_host
*)data
;
452 status
= host
->plat
->status(mmc_dev(host
->mmc
));
453 if (status
^ host
->oldstat
)
454 mmc_detect_change(host
->mmc
, 0);
456 host
->oldstat
= status
;
457 mod_timer(&host
->timer
, jiffies
+ HZ
);
460 static int mmci_probe(struct amba_device
*dev
, void *id
)
462 struct mmc_platform_data
*plat
= dev
->dev
.platform_data
;
463 struct mmci_host
*host
;
464 struct mmc_host
*mmc
;
467 /* must have platform data */
473 ret
= amba_request_regions(dev
, DRIVER_NAME
);
477 mmc
= mmc_alloc_host(sizeof(struct mmci_host
), &dev
->dev
);
483 host
= mmc_priv(mmc
);
484 host
->clk
= clk_get(&dev
->dev
, "MCLK");
485 if (IS_ERR(host
->clk
)) {
486 ret
= PTR_ERR(host
->clk
);
491 ret
= clk_enable(host
->clk
);
496 host
->mclk
= clk_get_rate(host
->clk
);
498 host
->base
= ioremap(dev
->res
.start
, SZ_4K
);
504 mmc
->ops
= &mmci_ops
;
505 mmc
->f_min
= (host
->mclk
+ 511) / 512;
506 mmc
->f_max
= min(host
->mclk
, fmax
);
507 mmc
->ocr_avail
= plat
->ocr_mask
;
512 mmc
->max_hw_segs
= 16;
513 mmc
->max_phys_segs
= NR_SG
;
516 * Since we only have a 16-bit data length register, we must
517 * ensure that we don't exceed 2^16-1 bytes in a single request.
518 * Choose 64 (512-byte) sectors as the limit.
520 mmc
->max_sectors
= 64;
523 * Set the maximum segment size. Since we aren't doing DMA
524 * (yet) we are only limited by the data length register.
526 mmc
->max_seg_size
= mmc
->max_sectors
<< 9;
528 spin_lock_init(&host
->lock
);
530 writel(0, host
->base
+ MMCIMASK0
);
531 writel(0, host
->base
+ MMCIMASK1
);
532 writel(0xfff, host
->base
+ MMCICLEAR
);
534 ret
= request_irq(dev
->irq
[0], mmci_irq
, IRQF_SHARED
, DRIVER_NAME
" (cmd)", host
);
538 ret
= request_irq(dev
->irq
[1], mmci_pio_irq
, IRQF_SHARED
, DRIVER_NAME
" (pio)", host
);
542 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
544 amba_set_drvdata(dev
, mmc
);
548 printk(KERN_INFO
"%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
549 mmc_hostname(mmc
), amba_rev(dev
), amba_config(dev
),
550 (unsigned long long)dev
->res
.start
, dev
->irq
[0], dev
->irq
[1]);
552 init_timer(&host
->timer
);
553 host
->timer
.data
= (unsigned long)host
;
554 host
->timer
.function
= mmci_check_status
;
555 host
->timer
.expires
= jiffies
+ HZ
;
556 add_timer(&host
->timer
);
561 free_irq(dev
->irq
[0], host
);
565 clk_disable(host
->clk
);
571 amba_release_regions(dev
);
576 static int mmci_remove(struct amba_device
*dev
)
578 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
580 amba_set_drvdata(dev
, NULL
);
583 struct mmci_host
*host
= mmc_priv(mmc
);
585 del_timer_sync(&host
->timer
);
587 mmc_remove_host(mmc
);
589 writel(0, host
->base
+ MMCIMASK0
);
590 writel(0, host
->base
+ MMCIMASK1
);
592 writel(0, host
->base
+ MMCICOMMAND
);
593 writel(0, host
->base
+ MMCIDATACTRL
);
595 free_irq(dev
->irq
[0], host
);
596 free_irq(dev
->irq
[1], host
);
599 clk_disable(host
->clk
);
604 amba_release_regions(dev
);
611 static int mmci_suspend(struct amba_device
*dev
, pm_message_t state
)
613 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
617 struct mmci_host
*host
= mmc_priv(mmc
);
619 ret
= mmc_suspend_host(mmc
, state
);
621 writel(0, host
->base
+ MMCIMASK0
);
627 static int mmci_resume(struct amba_device
*dev
)
629 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
633 struct mmci_host
*host
= mmc_priv(mmc
);
635 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
637 ret
= mmc_resume_host(mmc
);
643 #define mmci_suspend NULL
644 #define mmci_resume NULL
647 static struct amba_id mmci_ids
[] = {
659 static struct amba_driver mmci_driver
= {
664 .remove
= mmci_remove
,
665 .suspend
= mmci_suspend
,
666 .resume
= mmci_resume
,
667 .id_table
= mmci_ids
,
670 static int __init
mmci_init(void)
672 return amba_driver_register(&mmci_driver
);
675 static void __exit
mmci_exit(void)
677 amba_driver_unregister(&mmci_driver
);
680 module_init(mmci_init
);
681 module_exit(mmci_exit
);
682 module_param(fmax
, uint
, 0444);
684 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
685 MODULE_LICENSE("GPL");