2 * linux/drivers/mmc/host/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/amba/bus.h>
21 #include <linux/clk.h>
23 #include <asm/cacheflush.h>
24 #include <asm/div64.h>
26 #include <asm/scatterlist.h>
27 #include <asm/sizes.h>
28 #include <asm/mach/mmc.h>
32 #define DRIVER_NAME "mmci-pl18x"
34 #define DBG(host,fmt,args...) \
35 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
37 static unsigned int fmax
= 515633;
40 mmci_request_end(struct mmci_host
*host
, struct mmc_request
*mrq
)
42 writel(0, host
->base
+ MMCICOMMAND
);
50 mrq
->data
->bytes_xfered
= host
->data_xfered
;
53 * Need to drop the host lock here; mmc_request_done may call
54 * back into the driver...
56 spin_unlock(&host
->lock
);
57 mmc_request_done(host
->mmc
, mrq
);
58 spin_lock(&host
->lock
);
61 static void mmci_stop_data(struct mmci_host
*host
)
63 writel(0, host
->base
+ MMCIDATACTRL
);
64 writel(0, host
->base
+ MMCIMASK1
);
68 static void mmci_start_data(struct mmci_host
*host
, struct mmc_data
*data
)
70 unsigned int datactrl
, timeout
, irqmask
;
71 unsigned long long clks
;
75 DBG(host
, "blksz %04x blks %04x flags %08x\n",
76 data
->blksz
, data
->blocks
, data
->flags
);
79 host
->size
= data
->blksz
;
80 host
->data_xfered
= 0;
82 mmci_init_sg(host
, data
);
84 clks
= (unsigned long long)data
->timeout_ns
* host
->cclk
;
85 do_div(clks
, 1000000000UL);
87 timeout
= data
->timeout_clks
+ (unsigned int)clks
;
90 writel(timeout
, base
+ MMCIDATATIMER
);
91 writel(host
->size
, base
+ MMCIDATALENGTH
);
93 blksz_bits
= ffs(data
->blksz
) - 1;
94 BUG_ON(1 << blksz_bits
!= data
->blksz
);
96 datactrl
= MCI_DPSM_ENABLE
| blksz_bits
<< 4;
97 if (data
->flags
& MMC_DATA_READ
) {
98 datactrl
|= MCI_DPSM_DIRECTION
;
99 irqmask
= MCI_RXFIFOHALFFULLMASK
;
102 * If we have less than a FIFOSIZE of bytes to transfer,
103 * trigger a PIO interrupt as soon as any data is available.
105 if (host
->size
< MCI_FIFOSIZE
)
106 irqmask
|= MCI_RXDATAAVLBLMASK
;
109 * We don't actually need to include "FIFO empty" here
110 * since its implicit in "FIFO half empty".
112 irqmask
= MCI_TXFIFOHALFEMPTYMASK
;
115 writel(datactrl
, base
+ MMCIDATACTRL
);
116 writel(readl(base
+ MMCIMASK0
) & ~MCI_DATAENDMASK
, base
+ MMCIMASK0
);
117 writel(irqmask
, base
+ MMCIMASK1
);
121 mmci_start_command(struct mmci_host
*host
, struct mmc_command
*cmd
, u32 c
)
123 void __iomem
*base
= host
->base
;
125 DBG(host
, "op %02x arg %08x flags %08x\n",
126 cmd
->opcode
, cmd
->arg
, cmd
->flags
);
128 if (readl(base
+ MMCICOMMAND
) & MCI_CPSM_ENABLE
) {
129 writel(0, base
+ MMCICOMMAND
);
133 c
|= cmd
->opcode
| MCI_CPSM_ENABLE
;
134 if (cmd
->flags
& MMC_RSP_PRESENT
) {
135 if (cmd
->flags
& MMC_RSP_136
)
136 c
|= MCI_CPSM_LONGRSP
;
137 c
|= MCI_CPSM_RESPONSE
;
140 c
|= MCI_CPSM_INTERRUPT
;
144 writel(cmd
->arg
, base
+ MMCIARGUMENT
);
145 writel(c
, base
+ MMCICOMMAND
);
149 mmci_data_irq(struct mmci_host
*host
, struct mmc_data
*data
,
152 if (status
& MCI_DATABLOCKEND
) {
153 host
->data_xfered
+= data
->blksz
;
155 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|MCI_RXOVERRUN
)) {
156 if (status
& MCI_DATACRCFAIL
)
157 data
->error
= -EILSEQ
;
158 else if (status
& MCI_DATATIMEOUT
)
159 data
->error
= -ETIMEDOUT
;
160 else if (status
& (MCI_TXUNDERRUN
|MCI_RXOVERRUN
))
162 status
|= MCI_DATAEND
;
165 * We hit an error condition. Ensure that any data
166 * partially written to a page is properly coherent.
168 if (host
->sg_len
&& data
->flags
& MMC_DATA_READ
)
169 flush_dcache_page(host
->sg_ptr
->page
);
171 if (status
& MCI_DATAEND
) {
172 mmci_stop_data(host
);
175 mmci_request_end(host
, data
->mrq
);
177 mmci_start_command(host
, data
->stop
, 0);
183 mmci_cmd_irq(struct mmci_host
*host
, struct mmc_command
*cmd
,
186 void __iomem
*base
= host
->base
;
190 cmd
->resp
[0] = readl(base
+ MMCIRESPONSE0
);
191 cmd
->resp
[1] = readl(base
+ MMCIRESPONSE1
);
192 cmd
->resp
[2] = readl(base
+ MMCIRESPONSE2
);
193 cmd
->resp
[3] = readl(base
+ MMCIRESPONSE3
);
195 if (status
& MCI_CMDTIMEOUT
) {
196 cmd
->error
= -ETIMEDOUT
;
197 } else if (status
& MCI_CMDCRCFAIL
&& cmd
->flags
& MMC_RSP_CRC
) {
198 cmd
->error
= -EILSEQ
;
201 if (!cmd
->data
|| cmd
->error
) {
203 mmci_stop_data(host
);
204 mmci_request_end(host
, cmd
->mrq
);
205 } else if (!(cmd
->data
->flags
& MMC_DATA_READ
)) {
206 mmci_start_data(host
, cmd
->data
);
210 static int mmci_pio_read(struct mmci_host
*host
, char *buffer
, unsigned int remain
)
212 void __iomem
*base
= host
->base
;
217 int count
= host
->size
- (readl(base
+ MMCIFIFOCNT
) << 2);
225 readsl(base
+ MMCIFIFO
, ptr
, count
>> 2);
233 status
= readl(base
+ MMCISTATUS
);
234 } while (status
& MCI_RXDATAAVLBL
);
239 static int mmci_pio_write(struct mmci_host
*host
, char *buffer
, unsigned int remain
, u32 status
)
241 void __iomem
*base
= host
->base
;
245 unsigned int count
, maxcnt
;
247 maxcnt
= status
& MCI_TXFIFOEMPTY
? MCI_FIFOSIZE
: MCI_FIFOHALFSIZE
;
248 count
= min(remain
, maxcnt
);
250 writesl(base
+ MMCIFIFO
, ptr
, count
>> 2);
258 status
= readl(base
+ MMCISTATUS
);
259 } while (status
& MCI_TXFIFOHALFEMPTY
);
265 * PIO data transfer IRQ handler.
267 static irqreturn_t
mmci_pio_irq(int irq
, void *dev_id
)
269 struct mmci_host
*host
= dev_id
;
270 void __iomem
*base
= host
->base
;
273 status
= readl(base
+ MMCISTATUS
);
275 DBG(host
, "irq1 %08x\n", status
);
279 unsigned int remain
, len
;
283 * For write, we only need to test the half-empty flag
284 * here - if the FIFO is completely empty, then by
285 * definition it is more than half empty.
287 * For read, check for data available.
289 if (!(status
& (MCI_TXFIFOHALFEMPTY
|MCI_RXDATAAVLBL
)))
293 * Map the current scatter buffer.
295 buffer
= mmci_kmap_atomic(host
, &flags
) + host
->sg_off
;
296 remain
= host
->sg_ptr
->length
- host
->sg_off
;
299 if (status
& MCI_RXACTIVE
)
300 len
= mmci_pio_read(host
, buffer
, remain
);
301 if (status
& MCI_TXACTIVE
)
302 len
= mmci_pio_write(host
, buffer
, remain
, status
);
307 mmci_kunmap_atomic(host
, buffer
, &flags
);
317 * If we were reading, and we have completed this
318 * page, ensure that the data cache is coherent.
320 if (status
& MCI_RXACTIVE
)
321 flush_dcache_page(host
->sg_ptr
->page
);
323 if (!mmci_next_sg(host
))
326 status
= readl(base
+ MMCISTATUS
);
330 * If we're nearing the end of the read, switch to
331 * "any data available" mode.
333 if (status
& MCI_RXACTIVE
&& host
->size
< MCI_FIFOSIZE
)
334 writel(MCI_RXDATAAVLBLMASK
, base
+ MMCIMASK1
);
337 * If we run out of data, disable the data IRQs; this
338 * prevents a race where the FIFO becomes empty before
339 * the chip itself has disabled the data path, and
340 * stops us racing with our data end IRQ.
342 if (host
->size
== 0) {
343 writel(0, base
+ MMCIMASK1
);
344 writel(readl(base
+ MMCIMASK0
) | MCI_DATAENDMASK
, base
+ MMCIMASK0
);
351 * Handle completion of command and data transfers.
353 static irqreturn_t
mmci_irq(int irq
, void *dev_id
)
355 struct mmci_host
*host
= dev_id
;
359 spin_lock(&host
->lock
);
362 struct mmc_command
*cmd
;
363 struct mmc_data
*data
;
365 status
= readl(host
->base
+ MMCISTATUS
);
366 status
&= readl(host
->base
+ MMCIMASK0
);
367 writel(status
, host
->base
+ MMCICLEAR
);
369 DBG(host
, "irq0 %08x\n", status
);
372 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|
373 MCI_RXOVERRUN
|MCI_DATAEND
|MCI_DATABLOCKEND
) && data
)
374 mmci_data_irq(host
, data
, status
);
377 if (status
& (MCI_CMDCRCFAIL
|MCI_CMDTIMEOUT
|MCI_CMDSENT
|MCI_CMDRESPEND
) && cmd
)
378 mmci_cmd_irq(host
, cmd
, status
);
383 spin_unlock(&host
->lock
);
385 return IRQ_RETVAL(ret
);
388 static void mmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
390 struct mmci_host
*host
= mmc_priv(mmc
);
392 WARN_ON(host
->mrq
!= NULL
);
394 if (mrq
->data
&& (hweight32(mrq
->data
->blksz
) > 1)) {
395 printk(KERN_ERR
"%s: Unsupported block size (%d bytes)\n",
396 mmc_hostname(mmc
), mrq
->data
->blksz
);
397 mrq
->cmd
->error
= -EINVAL
;
398 mmc_request_done(mmc
, mrq
);
402 spin_lock_irq(&host
->lock
);
406 if (mrq
->data
&& mrq
->data
->flags
& MMC_DATA_READ
)
407 mmci_start_data(host
, mrq
->data
);
409 mmci_start_command(host
, mrq
->cmd
, 0);
411 spin_unlock_irq(&host
->lock
);
414 static void mmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
416 struct mmci_host
*host
= mmc_priv(mmc
);
417 u32 clk
= 0, pwr
= 0;
420 if (ios
->clock
>= host
->mclk
) {
421 clk
= MCI_CLK_BYPASS
;
422 host
->cclk
= host
->mclk
;
424 clk
= host
->mclk
/ (2 * ios
->clock
) - 1;
427 host
->cclk
= host
->mclk
/ (2 * (clk
+ 1));
429 clk
|= MCI_CLK_ENABLE
;
432 if (host
->plat
->translate_vdd
)
433 pwr
|= host
->plat
->translate_vdd(mmc_dev(mmc
), ios
->vdd
);
435 switch (ios
->power_mode
) {
446 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
449 writel(clk
, host
->base
+ MMCICLOCK
);
451 if (host
->pwr
!= pwr
) {
453 writel(pwr
, host
->base
+ MMCIPOWER
);
457 static const struct mmc_host_ops mmci_ops
= {
458 .request
= mmci_request
,
459 .set_ios
= mmci_set_ios
,
462 static void mmci_check_status(unsigned long data
)
464 struct mmci_host
*host
= (struct mmci_host
*)data
;
467 status
= host
->plat
->status(mmc_dev(host
->mmc
));
468 if (status
^ host
->oldstat
)
469 mmc_detect_change(host
->mmc
, 0);
471 host
->oldstat
= status
;
472 mod_timer(&host
->timer
, jiffies
+ HZ
);
475 static int mmci_probe(struct amba_device
*dev
, void *id
)
477 struct mmc_platform_data
*plat
= dev
->dev
.platform_data
;
478 struct mmci_host
*host
;
479 struct mmc_host
*mmc
;
482 /* must have platform data */
488 ret
= amba_request_regions(dev
, DRIVER_NAME
);
492 mmc
= mmc_alloc_host(sizeof(struct mmci_host
), &dev
->dev
);
498 host
= mmc_priv(mmc
);
499 host
->clk
= clk_get(&dev
->dev
, "MCLK");
500 if (IS_ERR(host
->clk
)) {
501 ret
= PTR_ERR(host
->clk
);
506 ret
= clk_enable(host
->clk
);
511 host
->mclk
= clk_get_rate(host
->clk
);
513 host
->base
= ioremap(dev
->res
.start
, SZ_4K
);
519 mmc
->ops
= &mmci_ops
;
520 mmc
->f_min
= (host
->mclk
+ 511) / 512;
521 mmc
->f_max
= min(host
->mclk
, fmax
);
522 mmc
->ocr_avail
= plat
->ocr_mask
;
523 mmc
->caps
= MMC_CAP_MULTIWRITE
;
528 mmc
->max_hw_segs
= 16;
529 mmc
->max_phys_segs
= NR_SG
;
532 * Since we only have a 16-bit data length register, we must
533 * ensure that we don't exceed 2^16-1 bytes in a single request.
535 mmc
->max_req_size
= 65535;
538 * Set the maximum segment size. Since we aren't doing DMA
539 * (yet) we are only limited by the data length register.
541 mmc
->max_seg_size
= mmc
->max_req_size
;
544 * Block size can be up to 2048 bytes, but must be a power of two.
546 mmc
->max_blk_size
= 2048;
549 * No limit on the number of blocks transferred.
551 mmc
->max_blk_count
= mmc
->max_req_size
;
553 spin_lock_init(&host
->lock
);
555 writel(0, host
->base
+ MMCIMASK0
);
556 writel(0, host
->base
+ MMCIMASK1
);
557 writel(0xfff, host
->base
+ MMCICLEAR
);
559 ret
= request_irq(dev
->irq
[0], mmci_irq
, IRQF_SHARED
, DRIVER_NAME
" (cmd)", host
);
563 ret
= request_irq(dev
->irq
[1], mmci_pio_irq
, IRQF_SHARED
, DRIVER_NAME
" (pio)", host
);
567 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
569 amba_set_drvdata(dev
, mmc
);
573 printk(KERN_INFO
"%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
574 mmc_hostname(mmc
), amba_rev(dev
), amba_config(dev
),
575 (unsigned long long)dev
->res
.start
, dev
->irq
[0], dev
->irq
[1]);
577 init_timer(&host
->timer
);
578 host
->timer
.data
= (unsigned long)host
;
579 host
->timer
.function
= mmci_check_status
;
580 host
->timer
.expires
= jiffies
+ HZ
;
581 add_timer(&host
->timer
);
586 free_irq(dev
->irq
[0], host
);
590 clk_disable(host
->clk
);
596 amba_release_regions(dev
);
601 static int mmci_remove(struct amba_device
*dev
)
603 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
605 amba_set_drvdata(dev
, NULL
);
608 struct mmci_host
*host
= mmc_priv(mmc
);
610 del_timer_sync(&host
->timer
);
612 mmc_remove_host(mmc
);
614 writel(0, host
->base
+ MMCIMASK0
);
615 writel(0, host
->base
+ MMCIMASK1
);
617 writel(0, host
->base
+ MMCICOMMAND
);
618 writel(0, host
->base
+ MMCIDATACTRL
);
620 free_irq(dev
->irq
[0], host
);
621 free_irq(dev
->irq
[1], host
);
624 clk_disable(host
->clk
);
629 amba_release_regions(dev
);
636 static int mmci_suspend(struct amba_device
*dev
, pm_message_t state
)
638 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
642 struct mmci_host
*host
= mmc_priv(mmc
);
644 ret
= mmc_suspend_host(mmc
, state
);
646 writel(0, host
->base
+ MMCIMASK0
);
652 static int mmci_resume(struct amba_device
*dev
)
654 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
658 struct mmci_host
*host
= mmc_priv(mmc
);
660 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
662 ret
= mmc_resume_host(mmc
);
668 #define mmci_suspend NULL
669 #define mmci_resume NULL
672 static struct amba_id mmci_ids
[] = {
684 static struct amba_driver mmci_driver
= {
689 .remove
= mmci_remove
,
690 .suspend
= mmci_suspend
,
691 .resume
= mmci_resume
,
692 .id_table
= mmci_ids
,
695 static int __init
mmci_init(void)
697 return amba_driver_register(&mmci_driver
);
700 static void __exit
mmci_exit(void)
702 amba_driver_unregister(&mmci_driver
);
705 module_init(mmci_init
);
706 module_exit(mmci_exit
);
707 module_param(fmax
, uint
, 0444);
709 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
710 MODULE_LICENSE("GPL");