2 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
5 * Copyright (C) 2010 ST-Ericsson SA
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.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
21 #include <linux/log2.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/card.h>
24 #include <linux/amba/bus.h>
25 #include <linux/clk.h>
26 #include <linux/scatterlist.h>
27 #include <linux/gpio.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/dmaengine.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/amba/mmci.h>
33 #include <asm/div64.h>
35 #include <asm/sizes.h>
39 #define DRIVER_NAME "mmci-pl18x"
41 static unsigned int fmax
= 515633;
44 * struct variant_data - MMCI variant-specific quirks
45 * @clkreg: default value for MCICLOCK register
46 * @clkreg_enable: enable value for MMCICLOCK register
47 * @datalength_bits: number of bits in the MMCIDATALENGTH register
48 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
49 * is asserted (likewise for RX)
50 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
51 * is asserted (likewise for RX)
52 * @sdio: variant supports SDIO
53 * @st_clkdiv: true if using a ST-specific clock divider algorithm
57 unsigned int clkreg_enable
;
58 unsigned int datalength_bits
;
59 unsigned int fifosize
;
60 unsigned int fifohalfsize
;
65 static struct variant_data variant_arm
= {
67 .fifohalfsize
= 8 * 4,
68 .datalength_bits
= 16,
71 static struct variant_data variant_arm_extended_fifo
= {
73 .fifohalfsize
= 64 * 4,
74 .datalength_bits
= 16,
77 static struct variant_data variant_u300
= {
79 .fifohalfsize
= 8 * 4,
80 .clkreg_enable
= 1 << 13, /* HWFCEN */
81 .datalength_bits
= 16,
85 static struct variant_data variant_ux500
= {
87 .fifohalfsize
= 8 * 4,
88 .clkreg
= MCI_CLK_ENABLE
,
89 .clkreg_enable
= 1 << 14, /* HWFCEN */
90 .datalength_bits
= 24,
96 * This must be called with host->lock held
98 static void mmci_set_clkreg(struct mmci_host
*host
, unsigned int desired
)
100 struct variant_data
*variant
= host
->variant
;
101 u32 clk
= variant
->clkreg
;
104 if (desired
>= host
->mclk
) {
105 clk
= MCI_CLK_BYPASS
;
106 host
->cclk
= host
->mclk
;
107 } else if (variant
->st_clkdiv
) {
109 * DB8500 TRM says f = mclk / (clkdiv + 2)
110 * => clkdiv = (mclk / f) - 2
111 * Round the divider up so we don't exceed the max
114 clk
= DIV_ROUND_UP(host
->mclk
, desired
) - 2;
117 host
->cclk
= host
->mclk
/ (clk
+ 2);
120 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
121 * => clkdiv = mclk / (2 * f) - 1
123 clk
= host
->mclk
/ (2 * desired
) - 1;
126 host
->cclk
= host
->mclk
/ (2 * (clk
+ 1));
129 clk
|= variant
->clkreg_enable
;
130 clk
|= MCI_CLK_ENABLE
;
131 /* This hasn't proven to be worthwhile */
132 /* clk |= MCI_CLK_PWRSAVE; */
135 if (host
->mmc
->ios
.bus_width
== MMC_BUS_WIDTH_4
)
137 if (host
->mmc
->ios
.bus_width
== MMC_BUS_WIDTH_8
)
138 clk
|= MCI_ST_8BIT_BUS
;
140 writel(clk
, host
->base
+ MMCICLOCK
);
144 mmci_request_end(struct mmci_host
*host
, struct mmc_request
*mrq
)
146 writel(0, host
->base
+ MMCICOMMAND
);
154 * Need to drop the host lock here; mmc_request_done may call
155 * back into the driver...
157 spin_unlock(&host
->lock
);
158 mmc_request_done(host
->mmc
, mrq
);
159 spin_lock(&host
->lock
);
162 static void mmci_set_mask1(struct mmci_host
*host
, unsigned int mask
)
164 void __iomem
*base
= host
->base
;
166 if (host
->singleirq
) {
167 unsigned int mask0
= readl(base
+ MMCIMASK0
);
169 mask0
&= ~MCI_IRQ1MASK
;
172 writel(mask0
, base
+ MMCIMASK0
);
175 writel(mask
, base
+ MMCIMASK1
);
178 static void mmci_stop_data(struct mmci_host
*host
)
180 writel(0, host
->base
+ MMCIDATACTRL
);
181 mmci_set_mask1(host
, 0);
185 static void mmci_init_sg(struct mmci_host
*host
, struct mmc_data
*data
)
187 unsigned int flags
= SG_MITER_ATOMIC
;
189 if (data
->flags
& MMC_DATA_READ
)
190 flags
|= SG_MITER_TO_SG
;
192 flags
|= SG_MITER_FROM_SG
;
194 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
198 * All the DMA operation mode stuff goes inside this ifdef.
199 * This assumes that you have a generic DMA device interface,
200 * no custom DMA interfaces are supported.
202 #ifdef CONFIG_DMA_ENGINE
203 static void __devinit
mmci_dma_setup(struct mmci_host
*host
)
205 struct mmci_platform_data
*plat
= host
->plat
;
206 const char *rxname
, *txname
;
209 if (!plat
|| !plat
->dma_filter
) {
210 dev_info(mmc_dev(host
->mmc
), "no DMA platform data\n");
214 /* Try to acquire a generic DMA engine slave channel */
216 dma_cap_set(DMA_SLAVE
, mask
);
219 * If only an RX channel is specified, the driver will
220 * attempt to use it bidirectionally, however if it is
221 * is specified but cannot be located, DMA will be disabled.
223 if (plat
->dma_rx_param
) {
224 host
->dma_rx_channel
= dma_request_channel(mask
,
227 /* E.g if no DMA hardware is present */
228 if (!host
->dma_rx_channel
)
229 dev_err(mmc_dev(host
->mmc
), "no RX DMA channel\n");
232 if (plat
->dma_tx_param
) {
233 host
->dma_tx_channel
= dma_request_channel(mask
,
236 if (!host
->dma_tx_channel
)
237 dev_warn(mmc_dev(host
->mmc
), "no TX DMA channel\n");
239 host
->dma_tx_channel
= host
->dma_rx_channel
;
242 if (host
->dma_rx_channel
)
243 rxname
= dma_chan_name(host
->dma_rx_channel
);
247 if (host
->dma_tx_channel
)
248 txname
= dma_chan_name(host
->dma_tx_channel
);
252 dev_info(mmc_dev(host
->mmc
), "DMA channels RX %s, TX %s\n",
256 * Limit the maximum segment size in any SG entry according to
257 * the parameters of the DMA engine device.
259 if (host
->dma_tx_channel
) {
260 struct device
*dev
= host
->dma_tx_channel
->device
->dev
;
261 unsigned int max_seg_size
= dma_get_max_seg_size(dev
);
263 if (max_seg_size
< host
->mmc
->max_seg_size
)
264 host
->mmc
->max_seg_size
= max_seg_size
;
266 if (host
->dma_rx_channel
) {
267 struct device
*dev
= host
->dma_rx_channel
->device
->dev
;
268 unsigned int max_seg_size
= dma_get_max_seg_size(dev
);
270 if (max_seg_size
< host
->mmc
->max_seg_size
)
271 host
->mmc
->max_seg_size
= max_seg_size
;
276 * This is used in __devinit or __devexit so inline it
277 * so it can be discarded.
279 static inline void mmci_dma_release(struct mmci_host
*host
)
281 struct mmci_platform_data
*plat
= host
->plat
;
283 if (host
->dma_rx_channel
)
284 dma_release_channel(host
->dma_rx_channel
);
285 if (host
->dma_tx_channel
&& plat
->dma_tx_param
)
286 dma_release_channel(host
->dma_tx_channel
);
287 host
->dma_rx_channel
= host
->dma_tx_channel
= NULL
;
290 static void mmci_dma_unmap(struct mmci_host
*host
, struct mmc_data
*data
)
292 struct dma_chan
*chan
= host
->dma_current
;
293 enum dma_data_direction dir
;
297 /* Wait up to 1ms for the DMA to complete */
299 status
= readl(host
->base
+ MMCISTATUS
);
300 if (!(status
& MCI_RXDATAAVLBLMASK
) || i
>= 100)
306 * Check to see whether we still have some data left in the FIFO -
307 * this catches DMA controllers which are unable to monitor the
308 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
309 * contiguous buffers. On TX, we'll get a FIFO underrun error.
311 if (status
& MCI_RXDATAAVLBLMASK
) {
312 dmaengine_terminate_all(chan
);
317 if (data
->flags
& MMC_DATA_WRITE
) {
320 dir
= DMA_FROM_DEVICE
;
323 dma_unmap_sg(chan
->device
->dev
, data
->sg
, data
->sg_len
, dir
);
326 * Use of DMA with scatter-gather is impossible.
327 * Give up with DMA and switch back to PIO mode.
329 if (status
& MCI_RXDATAAVLBLMASK
) {
330 dev_err(mmc_dev(host
->mmc
), "buggy DMA detected. Taking evasive action.\n");
331 mmci_dma_release(host
);
335 static void mmci_dma_data_error(struct mmci_host
*host
)
337 dev_err(mmc_dev(host
->mmc
), "error during DMA transfer!\n");
338 dmaengine_terminate_all(host
->dma_current
);
341 static int mmci_dma_start_data(struct mmci_host
*host
, unsigned int datactrl
)
343 struct variant_data
*variant
= host
->variant
;
344 struct dma_slave_config conf
= {
345 .src_addr
= host
->phybase
+ MMCIFIFO
,
346 .dst_addr
= host
->phybase
+ MMCIFIFO
,
347 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
348 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
349 .src_maxburst
= variant
->fifohalfsize
>> 2, /* # of words */
350 .dst_maxburst
= variant
->fifohalfsize
>> 2, /* # of words */
352 struct mmc_data
*data
= host
->data
;
353 struct dma_chan
*chan
;
354 struct dma_device
*device
;
355 struct dma_async_tx_descriptor
*desc
;
358 host
->dma_current
= NULL
;
360 if (data
->flags
& MMC_DATA_READ
) {
361 conf
.direction
= DMA_FROM_DEVICE
;
362 chan
= host
->dma_rx_channel
;
364 conf
.direction
= DMA_TO_DEVICE
;
365 chan
= host
->dma_tx_channel
;
368 /* If there's no DMA channel, fall back to PIO */
372 /* If less than or equal to the fifo size, don't bother with DMA */
373 if (host
->size
<= variant
->fifosize
)
376 device
= chan
->device
;
377 nr_sg
= dma_map_sg(device
->dev
, data
->sg
, data
->sg_len
, conf
.direction
);
381 dmaengine_slave_config(chan
, &conf
);
382 desc
= device
->device_prep_slave_sg(chan
, data
->sg
, nr_sg
,
383 conf
.direction
, DMA_CTRL_ACK
);
387 /* Okay, go for it. */
388 host
->dma_current
= chan
;
390 dev_vdbg(mmc_dev(host
->mmc
),
391 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
392 data
->sg_len
, data
->blksz
, data
->blocks
, data
->flags
);
393 dmaengine_submit(desc
);
394 dma_async_issue_pending(chan
);
396 datactrl
|= MCI_DPSM_DMAENABLE
;
398 /* Trigger the DMA transfer */
399 writel(datactrl
, host
->base
+ MMCIDATACTRL
);
402 * Let the MMCI say when the data is ended and it's time
403 * to fire next DMA request. When that happens, MMCI will
404 * call mmci_data_end()
406 writel(readl(host
->base
+ MMCIMASK0
) | MCI_DATAENDMASK
,
407 host
->base
+ MMCIMASK0
);
411 dmaengine_terminate_all(chan
);
412 dma_unmap_sg(device
->dev
, data
->sg
, data
->sg_len
, conf
.direction
);
416 /* Blank functions if the DMA engine is not available */
417 static inline void mmci_dma_setup(struct mmci_host
*host
)
421 static inline void mmci_dma_release(struct mmci_host
*host
)
425 static inline void mmci_dma_unmap(struct mmci_host
*host
, struct mmc_data
*data
)
429 static inline void mmci_dma_data_error(struct mmci_host
*host
)
433 static inline int mmci_dma_start_data(struct mmci_host
*host
, unsigned int datactrl
)
439 static void mmci_start_data(struct mmci_host
*host
, struct mmc_data
*data
)
441 struct variant_data
*variant
= host
->variant
;
442 unsigned int datactrl
, timeout
, irqmask
;
443 unsigned long long clks
;
447 dev_dbg(mmc_dev(host
->mmc
), "blksz %04x blks %04x flags %08x\n",
448 data
->blksz
, data
->blocks
, data
->flags
);
451 host
->size
= data
->blksz
* data
->blocks
;
452 data
->bytes_xfered
= 0;
454 clks
= (unsigned long long)data
->timeout_ns
* host
->cclk
;
455 do_div(clks
, 1000000000UL);
457 timeout
= data
->timeout_clks
+ (unsigned int)clks
;
460 writel(timeout
, base
+ MMCIDATATIMER
);
461 writel(host
->size
, base
+ MMCIDATALENGTH
);
463 blksz_bits
= ffs(data
->blksz
) - 1;
464 BUG_ON(1 << blksz_bits
!= data
->blksz
);
466 datactrl
= MCI_DPSM_ENABLE
| blksz_bits
<< 4;
468 if (data
->flags
& MMC_DATA_READ
)
469 datactrl
|= MCI_DPSM_DIRECTION
;
472 * Attempt to use DMA operation mode, if this
473 * should fail, fall back to PIO mode
475 if (!mmci_dma_start_data(host
, datactrl
))
478 /* IRQ mode, map the SG list for CPU reading/writing */
479 mmci_init_sg(host
, data
);
481 if (data
->flags
& MMC_DATA_READ
) {
482 irqmask
= MCI_RXFIFOHALFFULLMASK
;
485 * If we have less than the fifo 'half-full' threshold to
486 * transfer, trigger a PIO interrupt as soon as any data
489 if (host
->size
< variant
->fifohalfsize
)
490 irqmask
|= MCI_RXDATAAVLBLMASK
;
493 * We don't actually need to include "FIFO empty" here
494 * since its implicit in "FIFO half empty".
496 irqmask
= MCI_TXFIFOHALFEMPTYMASK
;
499 /* The ST Micro variants has a special bit to enable SDIO */
500 if (variant
->sdio
&& host
->mmc
->card
)
501 if (mmc_card_sdio(host
->mmc
->card
))
502 datactrl
|= MCI_ST_DPSM_SDIOEN
;
504 writel(datactrl
, base
+ MMCIDATACTRL
);
505 writel(readl(base
+ MMCIMASK0
) & ~MCI_DATAENDMASK
, base
+ MMCIMASK0
);
506 mmci_set_mask1(host
, irqmask
);
510 mmci_start_command(struct mmci_host
*host
, struct mmc_command
*cmd
, u32 c
)
512 void __iomem
*base
= host
->base
;
514 dev_dbg(mmc_dev(host
->mmc
), "op %02x arg %08x flags %08x\n",
515 cmd
->opcode
, cmd
->arg
, cmd
->flags
);
517 if (readl(base
+ MMCICOMMAND
) & MCI_CPSM_ENABLE
) {
518 writel(0, base
+ MMCICOMMAND
);
522 c
|= cmd
->opcode
| MCI_CPSM_ENABLE
;
523 if (cmd
->flags
& MMC_RSP_PRESENT
) {
524 if (cmd
->flags
& MMC_RSP_136
)
525 c
|= MCI_CPSM_LONGRSP
;
526 c
|= MCI_CPSM_RESPONSE
;
529 c
|= MCI_CPSM_INTERRUPT
;
533 writel(cmd
->arg
, base
+ MMCIARGUMENT
);
534 writel(c
, base
+ MMCICOMMAND
);
538 mmci_data_irq(struct mmci_host
*host
, struct mmc_data
*data
,
541 /* First check for errors */
542 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|MCI_RXOVERRUN
)) {
545 /* Terminate the DMA transfer */
546 if (dma_inprogress(host
))
547 mmci_dma_data_error(host
);
550 * Calculate how far we are into the transfer. Note that
551 * the data counter gives the number of bytes transferred
552 * on the MMC bus, not on the host side. On reads, this
553 * can be as much as a FIFO-worth of data ahead. This
554 * matters for FIFO overruns only.
556 remain
= readl(host
->base
+ MMCIDATACNT
);
557 success
= data
->blksz
* data
->blocks
- remain
;
559 dev_dbg(mmc_dev(host
->mmc
), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
561 if (status
& MCI_DATACRCFAIL
) {
562 /* Last block was not successful */
564 data
->error
= -EILSEQ
;
565 } else if (status
& MCI_DATATIMEOUT
) {
566 data
->error
= -ETIMEDOUT
;
567 } else if (status
& MCI_TXUNDERRUN
) {
569 } else if (status
& MCI_RXOVERRUN
) {
570 if (success
> host
->variant
->fifosize
)
571 success
-= host
->variant
->fifosize
;
576 data
->bytes_xfered
= round_down(success
, data
->blksz
);
579 if (status
& MCI_DATABLOCKEND
)
580 dev_err(mmc_dev(host
->mmc
), "stray MCI_DATABLOCKEND interrupt\n");
582 if (status
& MCI_DATAEND
|| data
->error
) {
583 if (dma_inprogress(host
))
584 mmci_dma_unmap(host
, data
);
585 mmci_stop_data(host
);
588 /* The error clause is handled above, success! */
589 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
592 mmci_request_end(host
, data
->mrq
);
594 mmci_start_command(host
, data
->stop
, 0);
600 mmci_cmd_irq(struct mmci_host
*host
, struct mmc_command
*cmd
,
603 void __iomem
*base
= host
->base
;
607 if (status
& MCI_CMDTIMEOUT
) {
608 cmd
->error
= -ETIMEDOUT
;
609 } else if (status
& MCI_CMDCRCFAIL
&& cmd
->flags
& MMC_RSP_CRC
) {
610 cmd
->error
= -EILSEQ
;
612 cmd
->resp
[0] = readl(base
+ MMCIRESPONSE0
);
613 cmd
->resp
[1] = readl(base
+ MMCIRESPONSE1
);
614 cmd
->resp
[2] = readl(base
+ MMCIRESPONSE2
);
615 cmd
->resp
[3] = readl(base
+ MMCIRESPONSE3
);
618 if (!cmd
->data
|| cmd
->error
) {
620 mmci_stop_data(host
);
621 mmci_request_end(host
, cmd
->mrq
);
622 } else if (!(cmd
->data
->flags
& MMC_DATA_READ
)) {
623 mmci_start_data(host
, cmd
->data
);
627 static int mmci_pio_read(struct mmci_host
*host
, char *buffer
, unsigned int remain
)
629 void __iomem
*base
= host
->base
;
632 int host_remain
= host
->size
;
635 int count
= host_remain
- (readl(base
+ MMCIFIFOCNT
) << 2);
643 readsl(base
+ MMCIFIFO
, ptr
, count
>> 2);
647 host_remain
-= count
;
652 status
= readl(base
+ MMCISTATUS
);
653 } while (status
& MCI_RXDATAAVLBL
);
658 static int mmci_pio_write(struct mmci_host
*host
, char *buffer
, unsigned int remain
, u32 status
)
660 struct variant_data
*variant
= host
->variant
;
661 void __iomem
*base
= host
->base
;
665 unsigned int count
, maxcnt
;
667 maxcnt
= status
& MCI_TXFIFOEMPTY
?
668 variant
->fifosize
: variant
->fifohalfsize
;
669 count
= min(remain
, maxcnt
);
672 * The ST Micro variant for SDIO transfer sizes
673 * less then 8 bytes should have clock H/W flow
677 mmc_card_sdio(host
->mmc
->card
)) {
679 writel(readl(host
->base
+ MMCICLOCK
) &
680 ~variant
->clkreg_enable
,
681 host
->base
+ MMCICLOCK
);
683 writel(readl(host
->base
+ MMCICLOCK
) |
684 variant
->clkreg_enable
,
685 host
->base
+ MMCICLOCK
);
689 * SDIO especially may want to send something that is
690 * not divisible by 4 (as opposed to card sectors
691 * etc), and the FIFO only accept full 32-bit writes.
692 * So compensate by adding +3 on the count, a single
693 * byte become a 32bit write, 7 bytes will be two
696 writesl(base
+ MMCIFIFO
, ptr
, (count
+ 3) >> 2);
704 status
= readl(base
+ MMCISTATUS
);
705 } while (status
& MCI_TXFIFOHALFEMPTY
);
711 * PIO data transfer IRQ handler.
713 static irqreturn_t
mmci_pio_irq(int irq
, void *dev_id
)
715 struct mmci_host
*host
= dev_id
;
716 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
717 struct variant_data
*variant
= host
->variant
;
718 void __iomem
*base
= host
->base
;
722 status
= readl(base
+ MMCISTATUS
);
724 dev_dbg(mmc_dev(host
->mmc
), "irq1 (pio) %08x\n", status
);
726 local_irq_save(flags
);
729 unsigned int remain
, len
;
733 * For write, we only need to test the half-empty flag
734 * here - if the FIFO is completely empty, then by
735 * definition it is more than half empty.
737 * For read, check for data available.
739 if (!(status
& (MCI_TXFIFOHALFEMPTY
|MCI_RXDATAAVLBL
)))
742 if (!sg_miter_next(sg_miter
))
745 buffer
= sg_miter
->addr
;
746 remain
= sg_miter
->length
;
749 if (status
& MCI_RXACTIVE
)
750 len
= mmci_pio_read(host
, buffer
, remain
);
751 if (status
& MCI_TXACTIVE
)
752 len
= mmci_pio_write(host
, buffer
, remain
, status
);
754 sg_miter
->consumed
= len
;
762 status
= readl(base
+ MMCISTATUS
);
765 sg_miter_stop(sg_miter
);
767 local_irq_restore(flags
);
770 * If we have less than the fifo 'half-full' threshold to transfer,
771 * trigger a PIO interrupt as soon as any data is available.
773 if (status
& MCI_RXACTIVE
&& host
->size
< variant
->fifohalfsize
)
774 mmci_set_mask1(host
, MCI_RXDATAAVLBLMASK
);
777 * If we run out of data, disable the data IRQs; this
778 * prevents a race where the FIFO becomes empty before
779 * the chip itself has disabled the data path, and
780 * stops us racing with our data end IRQ.
782 if (host
->size
== 0) {
783 mmci_set_mask1(host
, 0);
784 writel(readl(base
+ MMCIMASK0
) | MCI_DATAENDMASK
, base
+ MMCIMASK0
);
791 * Handle completion of command and data transfers.
793 static irqreturn_t
mmci_irq(int irq
, void *dev_id
)
795 struct mmci_host
*host
= dev_id
;
799 spin_lock(&host
->lock
);
802 struct mmc_command
*cmd
;
803 struct mmc_data
*data
;
805 status
= readl(host
->base
+ MMCISTATUS
);
807 if (host
->singleirq
) {
808 if (status
& readl(host
->base
+ MMCIMASK1
))
809 mmci_pio_irq(irq
, dev_id
);
811 status
&= ~MCI_IRQ1MASK
;
814 status
&= readl(host
->base
+ MMCIMASK0
);
815 writel(status
, host
->base
+ MMCICLEAR
);
817 dev_dbg(mmc_dev(host
->mmc
), "irq0 (data+cmd) %08x\n", status
);
820 if (status
& (MCI_DATACRCFAIL
|MCI_DATATIMEOUT
|MCI_TXUNDERRUN
|
821 MCI_RXOVERRUN
|MCI_DATAEND
|MCI_DATABLOCKEND
) && data
)
822 mmci_data_irq(host
, data
, status
);
825 if (status
& (MCI_CMDCRCFAIL
|MCI_CMDTIMEOUT
|MCI_CMDSENT
|MCI_CMDRESPEND
) && cmd
)
826 mmci_cmd_irq(host
, cmd
, status
);
831 spin_unlock(&host
->lock
);
833 return IRQ_RETVAL(ret
);
836 static void mmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
838 struct mmci_host
*host
= mmc_priv(mmc
);
841 WARN_ON(host
->mrq
!= NULL
);
843 if (mrq
->data
&& !is_power_of_2(mrq
->data
->blksz
)) {
844 dev_err(mmc_dev(mmc
), "unsupported block size (%d bytes)\n",
846 mrq
->cmd
->error
= -EINVAL
;
847 mmc_request_done(mmc
, mrq
);
851 spin_lock_irqsave(&host
->lock
, flags
);
855 if (mrq
->data
&& mrq
->data
->flags
& MMC_DATA_READ
)
856 mmci_start_data(host
, mrq
->data
);
858 mmci_start_command(host
, mrq
->cmd
, 0);
860 spin_unlock_irqrestore(&host
->lock
, flags
);
863 static void mmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
865 struct mmci_host
*host
= mmc_priv(mmc
);
870 switch (ios
->power_mode
) {
873 ret
= mmc_regulator_set_ocr(mmc
, host
->vcc
, 0);
877 ret
= mmc_regulator_set_ocr(mmc
, host
->vcc
, ios
->vdd
);
879 dev_err(mmc_dev(mmc
), "unable to set OCR\n");
881 * The .set_ios() function in the mmc_host_ops
882 * struct return void, and failing to set the
883 * power should be rare so we print an error
889 if (host
->plat
->vdd_handler
)
890 pwr
|= host
->plat
->vdd_handler(mmc_dev(mmc
), ios
->vdd
,
892 /* The ST version does not have this, fall through to POWER_ON */
893 if (host
->hw_designer
!= AMBA_VENDOR_ST
) {
902 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
) {
903 if (host
->hw_designer
!= AMBA_VENDOR_ST
)
907 * The ST Micro variant use the ROD bit for something
908 * else and only has OD (Open Drain).
914 spin_lock_irqsave(&host
->lock
, flags
);
916 mmci_set_clkreg(host
, ios
->clock
);
918 if (host
->pwr
!= pwr
) {
920 writel(pwr
, host
->base
+ MMCIPOWER
);
923 spin_unlock_irqrestore(&host
->lock
, flags
);
926 static int mmci_get_ro(struct mmc_host
*mmc
)
928 struct mmci_host
*host
= mmc_priv(mmc
);
930 if (host
->gpio_wp
== -ENOSYS
)
933 return gpio_get_value_cansleep(host
->gpio_wp
);
936 static int mmci_get_cd(struct mmc_host
*mmc
)
938 struct mmci_host
*host
= mmc_priv(mmc
);
939 struct mmci_platform_data
*plat
= host
->plat
;
942 if (host
->gpio_cd
== -ENOSYS
) {
944 return 1; /* Assume always present */
946 status
= plat
->status(mmc_dev(host
->mmc
));
948 status
= !!gpio_get_value_cansleep(host
->gpio_cd
)
952 * Use positive logic throughout - status is zero for no card,
953 * non-zero for card inserted.
958 static irqreturn_t
mmci_cd_irq(int irq
, void *dev_id
)
960 struct mmci_host
*host
= dev_id
;
962 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
967 static const struct mmc_host_ops mmci_ops
= {
968 .request
= mmci_request
,
969 .set_ios
= mmci_set_ios
,
970 .get_ro
= mmci_get_ro
,
971 .get_cd
= mmci_get_cd
,
974 static int __devinit
mmci_probe(struct amba_device
*dev
,
975 const struct amba_id
*id
)
977 struct mmci_platform_data
*plat
= dev
->dev
.platform_data
;
978 struct variant_data
*variant
= id
->data
;
979 struct mmci_host
*host
;
980 struct mmc_host
*mmc
;
983 /* must have platform data */
989 ret
= amba_request_regions(dev
, DRIVER_NAME
);
993 mmc
= mmc_alloc_host(sizeof(struct mmci_host
), &dev
->dev
);
999 host
= mmc_priv(mmc
);
1002 host
->gpio_wp
= -ENOSYS
;
1003 host
->gpio_cd
= -ENOSYS
;
1004 host
->gpio_cd_irq
= -1;
1006 host
->hw_designer
= amba_manf(dev
);
1007 host
->hw_revision
= amba_rev(dev
);
1008 dev_dbg(mmc_dev(mmc
), "designer ID = 0x%02x\n", host
->hw_designer
);
1009 dev_dbg(mmc_dev(mmc
), "revision = 0x%01x\n", host
->hw_revision
);
1011 host
->clk
= clk_get(&dev
->dev
, NULL
);
1012 if (IS_ERR(host
->clk
)) {
1013 ret
= PTR_ERR(host
->clk
);
1018 ret
= clk_enable(host
->clk
);
1023 host
->variant
= variant
;
1024 host
->mclk
= clk_get_rate(host
->clk
);
1026 * According to the spec, mclk is max 100 MHz,
1027 * so we try to adjust the clock down to this,
1030 if (host
->mclk
> 100000000) {
1031 ret
= clk_set_rate(host
->clk
, 100000000);
1034 host
->mclk
= clk_get_rate(host
->clk
);
1035 dev_dbg(mmc_dev(mmc
), "eventual mclk rate: %u Hz\n",
1038 host
->phybase
= dev
->res
.start
;
1039 host
->base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
1045 mmc
->ops
= &mmci_ops
;
1046 mmc
->f_min
= (host
->mclk
+ 511) / 512;
1048 * If the platform data supplies a maximum operating
1049 * frequency, this takes precedence. Else, we fall back
1050 * to using the module parameter, which has a (low)
1051 * default value in case it is not specified. Either
1052 * value must not exceed the clock rate into the block,
1056 mmc
->f_max
= min(host
->mclk
, plat
->f_max
);
1058 mmc
->f_max
= min(host
->mclk
, fmax
);
1059 dev_dbg(mmc_dev(mmc
), "clocking block at %u Hz\n", mmc
->f_max
);
1061 #ifdef CONFIG_REGULATOR
1062 /* If we're using the regulator framework, try to fetch a regulator */
1063 host
->vcc
= regulator_get(&dev
->dev
, "vmmc");
1064 if (IS_ERR(host
->vcc
))
1067 int mask
= mmc_regulator_get_ocrmask(host
->vcc
);
1070 dev_err(&dev
->dev
, "error getting OCR mask (%d)\n",
1073 host
->mmc
->ocr_avail
= (u32
) mask
;
1076 "Provided ocr_mask/setpower will not be used "
1077 "(using regulator instead)\n");
1081 /* Fall back to platform data if no regulator is found */
1082 if (host
->vcc
== NULL
)
1083 mmc
->ocr_avail
= plat
->ocr_mask
;
1084 mmc
->caps
= plat
->capabilities
;
1089 mmc
->max_segs
= NR_SG
;
1092 * Since only a certain number of bits are valid in the data length
1093 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1096 mmc
->max_req_size
= (1 << variant
->datalength_bits
) - 1;
1099 * Set the maximum segment size. Since we aren't doing DMA
1100 * (yet) we are only limited by the data length register.
1102 mmc
->max_seg_size
= mmc
->max_req_size
;
1105 * Block size can be up to 2048 bytes, but must be a power of two.
1107 mmc
->max_blk_size
= 2048;
1110 * No limit on the number of blocks transferred.
1112 mmc
->max_blk_count
= mmc
->max_req_size
;
1114 spin_lock_init(&host
->lock
);
1116 writel(0, host
->base
+ MMCIMASK0
);
1117 writel(0, host
->base
+ MMCIMASK1
);
1118 writel(0xfff, host
->base
+ MMCICLEAR
);
1120 if (gpio_is_valid(plat
->gpio_cd
)) {
1121 ret
= gpio_request(plat
->gpio_cd
, DRIVER_NAME
" (cd)");
1123 ret
= gpio_direction_input(plat
->gpio_cd
);
1125 host
->gpio_cd
= plat
->gpio_cd
;
1126 else if (ret
!= -ENOSYS
)
1129 ret
= request_any_context_irq(gpio_to_irq(plat
->gpio_cd
),
1131 DRIVER_NAME
" (cd)", host
);
1133 host
->gpio_cd_irq
= gpio_to_irq(plat
->gpio_cd
);
1135 if (gpio_is_valid(plat
->gpio_wp
)) {
1136 ret
= gpio_request(plat
->gpio_wp
, DRIVER_NAME
" (wp)");
1138 ret
= gpio_direction_input(plat
->gpio_wp
);
1140 host
->gpio_wp
= plat
->gpio_wp
;
1141 else if (ret
!= -ENOSYS
)
1145 if ((host
->plat
->status
|| host
->gpio_cd
!= -ENOSYS
)
1146 && host
->gpio_cd_irq
< 0)
1147 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1149 ret
= request_irq(dev
->irq
[0], mmci_irq
, IRQF_SHARED
, DRIVER_NAME
" (cmd)", host
);
1153 if (dev
->irq
[1] == NO_IRQ
)
1154 host
->singleirq
= true;
1156 ret
= request_irq(dev
->irq
[1], mmci_pio_irq
, IRQF_SHARED
,
1157 DRIVER_NAME
" (pio)", host
);
1162 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
1164 amba_set_drvdata(dev
, mmc
);
1166 dev_info(&dev
->dev
, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1167 mmc_hostname(mmc
), amba_part(dev
), amba_manf(dev
),
1168 amba_rev(dev
), (unsigned long long)dev
->res
.start
,
1169 dev
->irq
[0], dev
->irq
[1]);
1171 mmci_dma_setup(host
);
1178 free_irq(dev
->irq
[0], host
);
1180 if (host
->gpio_wp
!= -ENOSYS
)
1181 gpio_free(host
->gpio_wp
);
1183 if (host
->gpio_cd_irq
>= 0)
1184 free_irq(host
->gpio_cd_irq
, host
);
1185 if (host
->gpio_cd
!= -ENOSYS
)
1186 gpio_free(host
->gpio_cd
);
1188 iounmap(host
->base
);
1190 clk_disable(host
->clk
);
1196 amba_release_regions(dev
);
1201 static int __devexit
mmci_remove(struct amba_device
*dev
)
1203 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
1205 amba_set_drvdata(dev
, NULL
);
1208 struct mmci_host
*host
= mmc_priv(mmc
);
1210 mmc_remove_host(mmc
);
1212 writel(0, host
->base
+ MMCIMASK0
);
1213 writel(0, host
->base
+ MMCIMASK1
);
1215 writel(0, host
->base
+ MMCICOMMAND
);
1216 writel(0, host
->base
+ MMCIDATACTRL
);
1218 mmci_dma_release(host
);
1219 free_irq(dev
->irq
[0], host
);
1220 if (!host
->singleirq
)
1221 free_irq(dev
->irq
[1], host
);
1223 if (host
->gpio_wp
!= -ENOSYS
)
1224 gpio_free(host
->gpio_wp
);
1225 if (host
->gpio_cd_irq
>= 0)
1226 free_irq(host
->gpio_cd_irq
, host
);
1227 if (host
->gpio_cd
!= -ENOSYS
)
1228 gpio_free(host
->gpio_cd
);
1230 iounmap(host
->base
);
1231 clk_disable(host
->clk
);
1235 mmc_regulator_set_ocr(mmc
, host
->vcc
, 0);
1236 regulator_put(host
->vcc
);
1240 amba_release_regions(dev
);
1247 static int mmci_suspend(struct amba_device
*dev
, pm_message_t state
)
1249 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
1253 struct mmci_host
*host
= mmc_priv(mmc
);
1255 ret
= mmc_suspend_host(mmc
);
1257 writel(0, host
->base
+ MMCIMASK0
);
1263 static int mmci_resume(struct amba_device
*dev
)
1265 struct mmc_host
*mmc
= amba_get_drvdata(dev
);
1269 struct mmci_host
*host
= mmc_priv(mmc
);
1271 writel(MCI_IRQENABLE
, host
->base
+ MMCIMASK0
);
1273 ret
= mmc_resume_host(mmc
);
1279 #define mmci_suspend NULL
1280 #define mmci_resume NULL
1283 static struct amba_id mmci_ids
[] = {
1287 .data
= &variant_arm
,
1292 .data
= &variant_arm_extended_fifo
,
1297 .data
= &variant_arm
,
1299 /* ST Micro variants */
1303 .data
= &variant_u300
,
1308 .data
= &variant_u300
,
1313 .data
= &variant_ux500
,
1318 static struct amba_driver mmci_driver
= {
1320 .name
= DRIVER_NAME
,
1322 .probe
= mmci_probe
,
1323 .remove
= __devexit_p(mmci_remove
),
1324 .suspend
= mmci_suspend
,
1325 .resume
= mmci_resume
,
1326 .id_table
= mmci_ids
,
1329 static int __init
mmci_init(void)
1331 return amba_driver_register(&mmci_driver
);
1334 static void __exit
mmci_exit(void)
1336 amba_driver_unregister(&mmci_driver
);
1339 module_init(mmci_init
);
1340 module_exit(mmci_exit
);
1341 module_param(fmax
, uint
, 0444);
1343 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1344 MODULE_LICENSE("GPL");