2 * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
4 * Copyright (c) 2005, Advanced Micro Devices, Inc.
6 * Developed with help from the 2.4.30 MMC AU1XXX controller including
7 * the following copyright notices:
8 * Copyright (c) 2003-2004 Embedded Edge, LLC.
9 * Portions Copyright (C) 2002 Embedix, Inc
10 * Copyright 2002 Hewlett-Packard Company
12 * 2.6 version of this driver inspired by:
13 * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
14 * All Rights Reserved.
15 * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
16 * All Rights Reserved.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
24 /* Why don't we use the SD controllers' carddetect feature?
26 * From the AU1100 MMC application guide:
27 * If the Au1100-based design is intended to support both MultiMediaCards
28 * and 1- or 4-data bit SecureDigital cards, then the solution is to
29 * connect a weak (560KOhm) pull-up resistor to connector pin 1.
30 * In doing so, a MMC card never enters SPI-mode communications,
31 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
32 * (the low to high transition will not occur).
35 #include <linux/clk.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/platform_device.h>
40 #include <linux/interrupt.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/scatterlist.h>
43 #include <linux/highmem.h>
44 #include <linux/leds.h>
45 #include <linux/mmc/host.h>
46 #include <linux/slab.h>
49 #include <asm/mach-au1x00/au1000.h>
50 #include <asm/mach-au1x00/au1xxx_dbdma.h>
51 #include <asm/mach-au1x00/au1100_mmc.h>
53 #define DRIVER_NAME "au1xxx-mmc"
55 /* Set this to enable special debugging macros */
59 #define DBG(fmt, idx, args...) \
60 pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
62 #define DBG(fmt, idx, args...) do {} while (0)
65 /* Hardware definitions */
66 #define AU1XMMC_DESCRIPTOR_COUNT 1
68 /* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
69 #define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
70 #define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
72 #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
73 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
74 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
76 /* This gives us a hard value for the stop command that we can write directly
77 * to the command register.
80 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
82 /* This is the set of interrupts that we configure by default. */
83 #define AU1XMMC_INTERRUPTS \
84 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
85 SD_CONFIG_CR | SD_CONFIG_I)
87 /* The poll event (looking for insert/remove events runs twice a second. */
88 #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
92 struct mmc_request
*mrq
;
118 struct tasklet_struct finish_task
;
119 struct tasklet_struct data_task
;
120 struct au1xmmc_platform_data
*platdata
;
121 struct platform_device
*pdev
;
122 struct resource
*ioarea
;
126 /* Status flags used by the host structure */
127 #define HOST_F_XMIT 0x0001
128 #define HOST_F_RECV 0x0002
129 #define HOST_F_DMA 0x0010
130 #define HOST_F_DBDMA 0x0020
131 #define HOST_F_ACTIVE 0x0100
132 #define HOST_F_STOP 0x1000
134 #define HOST_S_IDLE 0x0001
135 #define HOST_S_CMD 0x0002
136 #define HOST_S_DATA 0x0003
137 #define HOST_S_STOP 0x0004
139 /* Easy access macros */
140 #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
141 #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
142 #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
143 #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
144 #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
145 #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
146 #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
147 #define HOST_CMD(h) ((h)->iobase + SD_CMD)
148 #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
149 #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
150 #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
152 #define DMA_CHANNEL(h) \
153 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
155 static inline int has_dbdma(void)
157 switch (alchemy_get_cputype()) {
158 case ALCHEMY_CPU_AU1200
:
159 case ALCHEMY_CPU_AU1300
:
166 static inline void IRQ_ON(struct au1xmmc_host
*host
, u32 mask
)
168 u32 val
= __raw_readl(HOST_CONFIG(host
));
170 __raw_writel(val
, HOST_CONFIG(host
));
171 wmb(); /* drain writebuffer */
174 static inline void FLUSH_FIFO(struct au1xmmc_host
*host
)
176 u32 val
= __raw_readl(HOST_CONFIG2(host
));
178 __raw_writel(val
| SD_CONFIG2_FF
, HOST_CONFIG2(host
));
179 wmb(); /* drain writebuffer */
182 /* SEND_STOP will turn off clock control - this re-enables it */
183 val
&= ~SD_CONFIG2_DF
;
185 __raw_writel(val
, HOST_CONFIG2(host
));
186 wmb(); /* drain writebuffer */
189 static inline void IRQ_OFF(struct au1xmmc_host
*host
, u32 mask
)
191 u32 val
= __raw_readl(HOST_CONFIG(host
));
193 __raw_writel(val
, HOST_CONFIG(host
));
194 wmb(); /* drain writebuffer */
197 static inline void SEND_STOP(struct au1xmmc_host
*host
)
201 WARN_ON(host
->status
!= HOST_S_DATA
);
202 host
->status
= HOST_S_STOP
;
204 config2
= __raw_readl(HOST_CONFIG2(host
));
205 __raw_writel(config2
| SD_CONFIG2_DF
, HOST_CONFIG2(host
));
206 wmb(); /* drain writebuffer */
208 /* Send the stop command */
209 __raw_writel(STOP_CMD
, HOST_CMD(host
));
210 wmb(); /* drain writebuffer */
213 static void au1xmmc_set_power(struct au1xmmc_host
*host
, int state
)
215 if (host
->platdata
&& host
->platdata
->set_power
)
216 host
->platdata
->set_power(host
->mmc
, state
);
219 static int au1xmmc_card_inserted(struct mmc_host
*mmc
)
221 struct au1xmmc_host
*host
= mmc_priv(mmc
);
223 if (host
->platdata
&& host
->platdata
->card_inserted
)
224 return !!host
->platdata
->card_inserted(host
->mmc
);
229 static int au1xmmc_card_readonly(struct mmc_host
*mmc
)
231 struct au1xmmc_host
*host
= mmc_priv(mmc
);
233 if (host
->platdata
&& host
->platdata
->card_readonly
)
234 return !!host
->platdata
->card_readonly(mmc
);
239 static void au1xmmc_finish_request(struct au1xmmc_host
*host
)
241 struct mmc_request
*mrq
= host
->mrq
;
244 host
->flags
&= HOST_F_ACTIVE
| HOST_F_DMA
;
250 host
->pio
.offset
= 0;
253 host
->status
= HOST_S_IDLE
;
255 mmc_request_done(host
->mmc
, mrq
);
258 static void au1xmmc_tasklet_finish(unsigned long param
)
260 struct au1xmmc_host
*host
= (struct au1xmmc_host
*) param
;
261 au1xmmc_finish_request(host
);
264 static int au1xmmc_send_command(struct au1xmmc_host
*host
, int wait
,
265 struct mmc_command
*cmd
, struct mmc_data
*data
)
267 u32 mmccmd
= (cmd
->opcode
<< SD_CMD_CI_SHIFT
);
269 switch (mmc_resp_type(cmd
)) {
273 mmccmd
|= SD_CMD_RT_1
;
276 mmccmd
|= SD_CMD_RT_1B
;
279 mmccmd
|= SD_CMD_RT_2
;
282 mmccmd
|= SD_CMD_RT_3
;
285 pr_info("au1xmmc: unhandled response type %02x\n",
291 if (data
->flags
& MMC_DATA_READ
) {
292 if (data
->blocks
> 1)
293 mmccmd
|= SD_CMD_CT_4
;
295 mmccmd
|= SD_CMD_CT_2
;
296 } else if (data
->flags
& MMC_DATA_WRITE
) {
297 if (data
->blocks
> 1)
298 mmccmd
|= SD_CMD_CT_3
;
300 mmccmd
|= SD_CMD_CT_1
;
304 __raw_writel(cmd
->arg
, HOST_CMDARG(host
));
305 wmb(); /* drain writebuffer */
308 IRQ_OFF(host
, SD_CONFIG_CR
);
310 __raw_writel((mmccmd
| SD_CMD_GO
), HOST_CMD(host
));
311 wmb(); /* drain writebuffer */
313 /* Wait for the command to go on the line */
314 while (__raw_readl(HOST_CMD(host
)) & SD_CMD_GO
)
317 /* Wait for the command to come back */
319 u32 status
= __raw_readl(HOST_STATUS(host
));
321 while (!(status
& SD_STATUS_CR
))
322 status
= __raw_readl(HOST_STATUS(host
));
324 /* Clear the CR status */
325 __raw_writel(SD_STATUS_CR
, HOST_STATUS(host
));
327 IRQ_ON(host
, SD_CONFIG_CR
);
333 static void au1xmmc_data_complete(struct au1xmmc_host
*host
, u32 status
)
335 struct mmc_request
*mrq
= host
->mrq
;
336 struct mmc_data
*data
;
339 WARN_ON((host
->status
!= HOST_S_DATA
) && (host
->status
!= HOST_S_STOP
));
341 if (host
->mrq
== NULL
)
344 data
= mrq
->cmd
->data
;
347 status
= __raw_readl(HOST_STATUS(host
));
349 /* The transaction is really over when the SD_STATUS_DB bit is clear */
350 while ((host
->flags
& HOST_F_XMIT
) && (status
& SD_STATUS_DB
))
351 status
= __raw_readl(HOST_STATUS(host
));
354 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
, host
->dma
.dir
);
356 /* Process any errors */
357 crc
= (status
& (SD_STATUS_WC
| SD_STATUS_RC
));
358 if (host
->flags
& HOST_F_XMIT
)
359 crc
|= ((status
& 0x07) == 0x02) ? 0 : 1;
362 data
->error
= -EILSEQ
;
364 /* Clear the CRC bits */
365 __raw_writel(SD_STATUS_WC
| SD_STATUS_RC
, HOST_STATUS(host
));
367 data
->bytes_xfered
= 0;
370 if (host
->flags
& (HOST_F_DMA
| HOST_F_DBDMA
)) {
371 u32 chan
= DMA_CHANNEL(host
);
373 chan_tab_t
*c
= *((chan_tab_t
**)chan
);
374 au1x_dma_chan_t
*cp
= c
->chan_ptr
;
375 data
->bytes_xfered
= cp
->ddma_bytecnt
;
378 (data
->blocks
* data
->blksz
) - host
->pio
.len
;
381 au1xmmc_finish_request(host
);
384 static void au1xmmc_tasklet_data(unsigned long param
)
386 struct au1xmmc_host
*host
= (struct au1xmmc_host
*)param
;
388 u32 status
= __raw_readl(HOST_STATUS(host
));
389 au1xmmc_data_complete(host
, status
);
392 #define AU1XMMC_MAX_TRANSFER 8
394 static void au1xmmc_send_pio(struct au1xmmc_host
*host
)
396 struct mmc_data
*data
;
397 int sg_len
, max
, count
;
398 unsigned char *sg_ptr
, val
;
400 struct scatterlist
*sg
;
402 data
= host
->mrq
->data
;
404 if (!(host
->flags
& HOST_F_XMIT
))
407 /* This is the pointer to the data buffer */
408 sg
= &data
->sg
[host
->pio
.index
];
409 sg_ptr
= kmap_atomic(sg_page(sg
)) + sg
->offset
+ host
->pio
.offset
;
411 /* This is the space left inside the buffer */
412 sg_len
= data
->sg
[host
->pio
.index
].length
- host
->pio
.offset
;
414 /* Check if we need less than the size of the sg_buffer */
415 max
= (sg_len
> host
->pio
.len
) ? host
->pio
.len
: sg_len
;
416 if (max
> AU1XMMC_MAX_TRANSFER
)
417 max
= AU1XMMC_MAX_TRANSFER
;
419 for (count
= 0; count
< max
; count
++) {
420 status
= __raw_readl(HOST_STATUS(host
));
422 if (!(status
& SD_STATUS_TH
))
427 __raw_writel((unsigned long)val
, HOST_TXPORT(host
));
428 wmb(); /* drain writebuffer */
430 kunmap_atomic(sg_ptr
);
432 host
->pio
.len
-= count
;
433 host
->pio
.offset
+= count
;
435 if (count
== sg_len
) {
437 host
->pio
.offset
= 0;
440 if (host
->pio
.len
== 0) {
441 IRQ_OFF(host
, SD_CONFIG_TH
);
443 if (host
->flags
& HOST_F_STOP
)
446 tasklet_schedule(&host
->data_task
);
450 static void au1xmmc_receive_pio(struct au1xmmc_host
*host
)
452 struct mmc_data
*data
;
453 int max
, count
, sg_len
= 0;
454 unsigned char *sg_ptr
= NULL
;
456 struct scatterlist
*sg
;
458 data
= host
->mrq
->data
;
460 if (!(host
->flags
& HOST_F_RECV
))
465 if (host
->pio
.index
< host
->dma
.len
) {
466 sg
= &data
->sg
[host
->pio
.index
];
467 sg_ptr
= kmap_atomic(sg_page(sg
)) + sg
->offset
+ host
->pio
.offset
;
469 /* This is the space left inside the buffer */
470 sg_len
= sg_dma_len(&data
->sg
[host
->pio
.index
]) - host
->pio
.offset
;
472 /* Check if we need less than the size of the sg_buffer */
477 if (max
> AU1XMMC_MAX_TRANSFER
)
478 max
= AU1XMMC_MAX_TRANSFER
;
480 for (count
= 0; count
< max
; count
++) {
481 status
= __raw_readl(HOST_STATUS(host
));
483 if (!(status
& SD_STATUS_NE
))
486 if (status
& SD_STATUS_RC
) {
487 DBG("RX CRC Error [%d + %d].\n", host
->pdev
->id
,
488 host
->pio
.len
, count
);
492 if (status
& SD_STATUS_RO
) {
493 DBG("RX Overrun [%d + %d]\n", host
->pdev
->id
,
494 host
->pio
.len
, count
);
497 else if (status
& SD_STATUS_RU
) {
498 DBG("RX Underrun [%d + %d]\n", host
->pdev
->id
,
499 host
->pio
.len
, count
);
503 val
= __raw_readl(HOST_RXPORT(host
));
506 sg_ptr
[count
] = (unsigned char)(val
& 0xFF);
509 kunmap_atomic(sg_ptr
);
511 host
->pio
.len
-= count
;
512 host
->pio
.offset
+= count
;
514 if (sg_len
&& count
== sg_len
) {
516 host
->pio
.offset
= 0;
519 if (host
->pio
.len
== 0) {
520 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
521 IRQ_OFF(host
, SD_CONFIG_NE
);
523 if (host
->flags
& HOST_F_STOP
)
526 tasklet_schedule(&host
->data_task
);
530 /* This is called when a command has been completed - grab the response
531 * and check for errors. Then start the data transfer if it is indicated.
533 static void au1xmmc_cmd_complete(struct au1xmmc_host
*host
, u32 status
)
535 struct mmc_request
*mrq
= host
->mrq
;
536 struct mmc_command
*cmd
;
546 if (cmd
->flags
& MMC_RSP_PRESENT
) {
547 if (cmd
->flags
& MMC_RSP_136
) {
548 r
[0] = __raw_readl(host
->iobase
+ SD_RESP3
);
549 r
[1] = __raw_readl(host
->iobase
+ SD_RESP2
);
550 r
[2] = __raw_readl(host
->iobase
+ SD_RESP1
);
551 r
[3] = __raw_readl(host
->iobase
+ SD_RESP0
);
553 /* The CRC is omitted from the response, so really
554 * we only got 120 bytes, but the engine expects
555 * 128 bits, so we have to shift things up.
557 for (i
= 0; i
< 4; i
++) {
558 cmd
->resp
[i
] = (r
[i
] & 0x00FFFFFF) << 8;
560 cmd
->resp
[i
] |= (r
[i
+ 1] & 0xFF000000) >> 24;
563 /* Techincally, we should be getting all 48 bits of
564 * the response (SD_RESP1 + SD_RESP2), but because
565 * our response omits the CRC, our data ends up
566 * being shifted 8 bits to the right. In this case,
567 * that means that the OSR data starts at bit 31,
568 * so we can just read RESP0 and return that.
570 cmd
->resp
[0] = __raw_readl(host
->iobase
+ SD_RESP0
);
574 /* Figure out errors */
575 if (status
& (SD_STATUS_SC
| SD_STATUS_WC
| SD_STATUS_RC
))
576 cmd
->error
= -EILSEQ
;
578 trans
= host
->flags
& (HOST_F_XMIT
| HOST_F_RECV
);
580 if (!trans
|| cmd
->error
) {
581 IRQ_OFF(host
, SD_CONFIG_TH
| SD_CONFIG_RA
| SD_CONFIG_RF
);
582 tasklet_schedule(&host
->finish_task
);
586 host
->status
= HOST_S_DATA
;
588 if ((host
->flags
& (HOST_F_DMA
| HOST_F_DBDMA
))) {
589 u32 channel
= DMA_CHANNEL(host
);
591 /* Start the DBDMA as soon as the buffer gets something in it */
593 if (host
->flags
& HOST_F_RECV
) {
594 u32 mask
= SD_STATUS_DB
| SD_STATUS_NE
;
596 while((status
& mask
) != mask
)
597 status
= __raw_readl(HOST_STATUS(host
));
600 au1xxx_dbdma_start(channel
);
604 static void au1xmmc_set_clock(struct au1xmmc_host
*host
, int rate
)
606 unsigned int pbus
= clk_get_rate(host
->clk
);
607 unsigned int divisor
= ((pbus
/ rate
) / 2) - 1;
610 config
= __raw_readl(HOST_CONFIG(host
));
612 config
&= ~(SD_CONFIG_DIV
);
613 config
|= (divisor
& SD_CONFIG_DIV
) | SD_CONFIG_DE
;
615 __raw_writel(config
, HOST_CONFIG(host
));
616 wmb(); /* drain writebuffer */
619 static int au1xmmc_prepare_data(struct au1xmmc_host
*host
,
620 struct mmc_data
*data
)
622 int datalen
= data
->blocks
* data
->blksz
;
624 if (data
->flags
& MMC_DATA_READ
)
625 host
->flags
|= HOST_F_RECV
;
627 host
->flags
|= HOST_F_XMIT
;
630 host
->flags
|= HOST_F_STOP
;
632 host
->dma
.dir
= DMA_BIDIRECTIONAL
;
634 host
->dma
.len
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
,
635 data
->sg_len
, host
->dma
.dir
);
637 if (host
->dma
.len
== 0)
640 __raw_writel(data
->blksz
- 1, HOST_BLKSIZE(host
));
642 if (host
->flags
& (HOST_F_DMA
| HOST_F_DBDMA
)) {
644 u32 channel
= DMA_CHANNEL(host
);
646 au1xxx_dbdma_stop(channel
);
648 for (i
= 0; i
< host
->dma
.len
; i
++) {
649 u32 ret
= 0, flags
= DDMA_FLAGS_NOIE
;
650 struct scatterlist
*sg
= &data
->sg
[i
];
651 int sg_len
= sg
->length
;
653 int len
= (datalen
> sg_len
) ? sg_len
: datalen
;
655 if (i
== host
->dma
.len
- 1)
656 flags
= DDMA_FLAGS_IE
;
658 if (host
->flags
& HOST_F_XMIT
) {
659 ret
= au1xxx_dbdma_put_source(channel
,
660 sg_phys(sg
), len
, flags
);
662 ret
= au1xxx_dbdma_put_dest(channel
,
663 sg_phys(sg
), len
, flags
);
673 host
->pio
.offset
= 0;
674 host
->pio
.len
= datalen
;
676 if (host
->flags
& HOST_F_XMIT
)
677 IRQ_ON(host
, SD_CONFIG_TH
);
679 IRQ_ON(host
, SD_CONFIG_NE
);
680 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
686 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
,
691 /* This actually starts a command or data transaction */
692 static void au1xmmc_request(struct mmc_host
* mmc
, struct mmc_request
* mrq
)
694 struct au1xmmc_host
*host
= mmc_priv(mmc
);
697 WARN_ON(irqs_disabled());
698 WARN_ON(host
->status
!= HOST_S_IDLE
);
701 host
->status
= HOST_S_CMD
;
703 /* fail request immediately if no card is present */
704 if (0 == au1xmmc_card_inserted(mmc
)) {
705 mrq
->cmd
->error
= -ENOMEDIUM
;
706 au1xmmc_finish_request(host
);
712 ret
= au1xmmc_prepare_data(host
, mrq
->data
);
716 ret
= au1xmmc_send_command(host
, 0, mrq
->cmd
, mrq
->data
);
719 mrq
->cmd
->error
= ret
;
720 au1xmmc_finish_request(host
);
724 static void au1xmmc_reset_controller(struct au1xmmc_host
*host
)
726 /* Apply the clock */
727 __raw_writel(SD_ENABLE_CE
, HOST_ENABLE(host
));
728 wmb(); /* drain writebuffer */
731 __raw_writel(SD_ENABLE_R
| SD_ENABLE_CE
, HOST_ENABLE(host
));
732 wmb(); /* drain writebuffer */
735 __raw_writel(~0, HOST_STATUS(host
));
736 wmb(); /* drain writebuffer */
738 __raw_writel(0, HOST_BLKSIZE(host
));
739 __raw_writel(0x001fffff, HOST_TIMEOUT(host
));
740 wmb(); /* drain writebuffer */
742 __raw_writel(SD_CONFIG2_EN
, HOST_CONFIG2(host
));
743 wmb(); /* drain writebuffer */
745 __raw_writel(SD_CONFIG2_EN
| SD_CONFIG2_FF
, HOST_CONFIG2(host
));
746 wmb(); /* drain writebuffer */
749 __raw_writel(SD_CONFIG2_EN
, HOST_CONFIG2(host
));
750 wmb(); /* drain writebuffer */
752 /* Configure interrupts */
753 __raw_writel(AU1XMMC_INTERRUPTS
, HOST_CONFIG(host
));
754 wmb(); /* drain writebuffer */
758 static void au1xmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
760 struct au1xmmc_host
*host
= mmc_priv(mmc
);
763 if (ios
->power_mode
== MMC_POWER_OFF
)
764 au1xmmc_set_power(host
, 0);
765 else if (ios
->power_mode
== MMC_POWER_ON
) {
766 au1xmmc_set_power(host
, 1);
769 if (ios
->clock
&& ios
->clock
!= host
->clock
) {
770 au1xmmc_set_clock(host
, ios
->clock
);
771 host
->clock
= ios
->clock
;
774 config2
= __raw_readl(HOST_CONFIG2(host
));
775 switch (ios
->bus_width
) {
776 case MMC_BUS_WIDTH_8
:
777 config2
|= SD_CONFIG2_BB
;
779 case MMC_BUS_WIDTH_4
:
780 config2
&= ~SD_CONFIG2_BB
;
781 config2
|= SD_CONFIG2_WB
;
783 case MMC_BUS_WIDTH_1
:
784 config2
&= ~(SD_CONFIG2_WB
| SD_CONFIG2_BB
);
787 __raw_writel(config2
, HOST_CONFIG2(host
));
788 wmb(); /* drain writebuffer */
791 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
792 #define STATUS_DATA_IN (SD_STATUS_NE)
793 #define STATUS_DATA_OUT (SD_STATUS_TH)
795 static irqreturn_t
au1xmmc_irq(int irq
, void *dev_id
)
797 struct au1xmmc_host
*host
= dev_id
;
800 status
= __raw_readl(HOST_STATUS(host
));
802 if (!(status
& SD_STATUS_I
))
803 return IRQ_NONE
; /* not ours */
805 if (status
& SD_STATUS_SI
) /* SDIO */
806 mmc_signal_sdio_irq(host
->mmc
);
808 if (host
->mrq
&& (status
& STATUS_TIMEOUT
)) {
809 if (status
& SD_STATUS_RAT
)
810 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
811 else if (status
& SD_STATUS_DT
)
812 host
->mrq
->data
->error
= -ETIMEDOUT
;
814 /* In PIO mode, interrupts might still be enabled */
815 IRQ_OFF(host
, SD_CONFIG_NE
| SD_CONFIG_TH
);
817 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
818 tasklet_schedule(&host
->finish_task
);
821 else if (status
& SD_STATUS_DD
) {
822 /* Sometimes we get a DD before a NE in PIO mode */
823 if (!(host
->flags
& HOST_F_DMA
) && (status
& SD_STATUS_NE
))
824 au1xmmc_receive_pio(host
);
826 au1xmmc_data_complete(host
, status
);
827 /* tasklet_schedule(&host->data_task); */
831 else if (status
& SD_STATUS_CR
) {
832 if (host
->status
== HOST_S_CMD
)
833 au1xmmc_cmd_complete(host
, status
);
835 } else if (!(host
->flags
& HOST_F_DMA
)) {
836 if ((host
->flags
& HOST_F_XMIT
) && (status
& STATUS_DATA_OUT
))
837 au1xmmc_send_pio(host
);
838 else if ((host
->flags
& HOST_F_RECV
) && (status
& STATUS_DATA_IN
))
839 au1xmmc_receive_pio(host
);
841 } else if (status
& 0x203F3C70) {
842 DBG("Unhandled status %8.8x\n", host
->pdev
->id
,
846 __raw_writel(status
, HOST_STATUS(host
));
847 wmb(); /* drain writebuffer */
852 /* 8bit memory DMA device */
853 static dbdev_tab_t au1xmmc_mem_dbdev
= {
854 .dev_id
= DSCR_CMD0_ALWAYS
,
855 .dev_flags
= DEV_FLAGS_ANYUSE
,
858 .dev_physaddr
= 0x00000000,
860 .dev_intpolarity
= 0,
864 static void au1xmmc_dbdma_callback(int irq
, void *dev_id
)
866 struct au1xmmc_host
*host
= (struct au1xmmc_host
*)dev_id
;
868 /* Avoid spurious interrupts */
872 if (host
->flags
& HOST_F_STOP
)
875 tasklet_schedule(&host
->data_task
);
878 static int au1xmmc_dbdma_init(struct au1xmmc_host
*host
)
880 struct resource
*res
;
883 res
= platform_get_resource(host
->pdev
, IORESOURCE_DMA
, 0);
888 res
= platform_get_resource(host
->pdev
, IORESOURCE_DMA
, 1);
896 host
->tx_chan
= au1xxx_dbdma_chan_alloc(memid
, txid
,
897 au1xmmc_dbdma_callback
, (void *)host
);
898 if (!host
->tx_chan
) {
899 dev_err(&host
->pdev
->dev
, "cannot allocate TX DMA\n");
903 host
->rx_chan
= au1xxx_dbdma_chan_alloc(rxid
, memid
,
904 au1xmmc_dbdma_callback
, (void *)host
);
905 if (!host
->rx_chan
) {
906 dev_err(&host
->pdev
->dev
, "cannot allocate RX DMA\n");
907 au1xxx_dbdma_chan_free(host
->tx_chan
);
911 au1xxx_dbdma_set_devwidth(host
->tx_chan
, 8);
912 au1xxx_dbdma_set_devwidth(host
->rx_chan
, 8);
914 au1xxx_dbdma_ring_alloc(host
->tx_chan
, AU1XMMC_DESCRIPTOR_COUNT
);
915 au1xxx_dbdma_ring_alloc(host
->rx_chan
, AU1XMMC_DESCRIPTOR_COUNT
);
917 /* DBDMA is good to go */
918 host
->flags
|= HOST_F_DMA
| HOST_F_DBDMA
;
923 static void au1xmmc_dbdma_shutdown(struct au1xmmc_host
*host
)
925 if (host
->flags
& HOST_F_DMA
) {
926 host
->flags
&= ~HOST_F_DMA
;
927 au1xxx_dbdma_chan_free(host
->tx_chan
);
928 au1xxx_dbdma_chan_free(host
->rx_chan
);
932 static void au1xmmc_enable_sdio_irq(struct mmc_host
*mmc
, int en
)
934 struct au1xmmc_host
*host
= mmc_priv(mmc
);
937 IRQ_ON(host
, SD_CONFIG_SI
);
939 IRQ_OFF(host
, SD_CONFIG_SI
);
942 static const struct mmc_host_ops au1xmmc_ops
= {
943 .request
= au1xmmc_request
,
944 .set_ios
= au1xmmc_set_ios
,
945 .get_ro
= au1xmmc_card_readonly
,
946 .get_cd
= au1xmmc_card_inserted
,
947 .enable_sdio_irq
= au1xmmc_enable_sdio_irq
,
950 static int au1xmmc_probe(struct platform_device
*pdev
)
952 struct mmc_host
*mmc
;
953 struct au1xmmc_host
*host
;
957 mmc
= mmc_alloc_host(sizeof(struct au1xmmc_host
), &pdev
->dev
);
959 dev_err(&pdev
->dev
, "no memory for mmc_host\n");
964 host
= mmc_priv(mmc
);
966 host
->platdata
= pdev
->dev
.platform_data
;
970 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
972 dev_err(&pdev
->dev
, "no mmio defined\n");
976 host
->ioarea
= request_mem_region(r
->start
, resource_size(r
),
979 dev_err(&pdev
->dev
, "mmio already in use\n");
983 host
->iobase
= ioremap(r
->start
, 0x3c);
985 dev_err(&pdev
->dev
, "cannot remap mmio\n");
989 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
991 dev_err(&pdev
->dev
, "no IRQ defined\n");
994 host
->irq
= r
->start
;
996 mmc
->ops
= &au1xmmc_ops
;
999 mmc
->f_max
= 24000000;
1001 mmc
->max_blk_size
= 2048;
1002 mmc
->max_blk_count
= 512;
1004 mmc
->ocr_avail
= AU1XMMC_OCR
;
1005 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SDIO_IRQ
;
1006 mmc
->max_segs
= AU1XMMC_DESCRIPTOR_COUNT
;
1008 iflag
= IRQF_SHARED
; /* Au1100/Au1200: one int for both ctrls */
1010 switch (alchemy_get_cputype()) {
1011 case ALCHEMY_CPU_AU1100
:
1012 mmc
->max_seg_size
= AU1100_MMC_DESCRIPTOR_SIZE
;
1014 case ALCHEMY_CPU_AU1200
:
1015 mmc
->max_seg_size
= AU1200_MMC_DESCRIPTOR_SIZE
;
1017 case ALCHEMY_CPU_AU1300
:
1018 iflag
= 0; /* nothing is shared */
1019 mmc
->max_seg_size
= AU1200_MMC_DESCRIPTOR_SIZE
;
1020 mmc
->f_max
= 52000000;
1021 if (host
->ioarea
->start
== AU1100_SD0_PHYS_ADDR
)
1022 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1026 ret
= request_irq(host
->irq
, au1xmmc_irq
, iflag
, DRIVER_NAME
, host
);
1028 dev_err(&pdev
->dev
, "cannot grab IRQ\n");
1032 host
->clk
= clk_get(&pdev
->dev
, ALCHEMY_PERIPH_CLK
);
1033 if (IS_ERR(host
->clk
)) {
1034 dev_err(&pdev
->dev
, "cannot find clock\n");
1035 ret
= PTR_ERR(host
->clk
);
1039 ret
= clk_prepare_enable(host
->clk
);
1041 dev_err(&pdev
->dev
, "cannot enable clock\n");
1045 host
->status
= HOST_S_IDLE
;
1047 /* board-specific carddetect setup, if any */
1048 if (host
->platdata
&& host
->platdata
->cd_setup
) {
1049 ret
= host
->platdata
->cd_setup(mmc
, 1);
1051 dev_warn(&pdev
->dev
, "board CD setup failed\n");
1052 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1055 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1057 /* platform may not be able to use all advertised caps */
1059 mmc
->caps
&= ~(host
->platdata
->mask_host_caps
);
1061 tasklet_init(&host
->data_task
, au1xmmc_tasklet_data
,
1062 (unsigned long)host
);
1064 tasklet_init(&host
->finish_task
, au1xmmc_tasklet_finish
,
1065 (unsigned long)host
);
1068 ret
= au1xmmc_dbdma_init(host
);
1070 pr_info(DRIVER_NAME
": DBDMA init failed; using PIO\n");
1073 #ifdef CONFIG_LEDS_CLASS
1074 if (host
->platdata
&& host
->platdata
->led
) {
1075 struct led_classdev
*led
= host
->platdata
->led
;
1076 led
->name
= mmc_hostname(mmc
);
1077 led
->brightness
= LED_OFF
;
1078 led
->default_trigger
= mmc_hostname(mmc
);
1079 ret
= led_classdev_register(mmc_dev(mmc
), led
);
1085 au1xmmc_reset_controller(host
);
1087 ret
= mmc_add_host(mmc
);
1089 dev_err(&pdev
->dev
, "cannot add mmc host\n");
1093 platform_set_drvdata(pdev
, host
);
1095 pr_info(DRIVER_NAME
": MMC Controller %d set up at %p"
1096 " (mode=%s)\n", pdev
->id
, host
->iobase
,
1097 host
->flags
& HOST_F_DMA
? "dma" : "pio");
1099 return 0; /* all ok */
1102 #ifdef CONFIG_LEDS_CLASS
1103 if (host
->platdata
&& host
->platdata
->led
)
1104 led_classdev_unregister(host
->platdata
->led
);
1107 __raw_writel(0, HOST_ENABLE(host
));
1108 __raw_writel(0, HOST_CONFIG(host
));
1109 __raw_writel(0, HOST_CONFIG2(host
));
1110 wmb(); /* drain writebuffer */
1112 if (host
->flags
& HOST_F_DBDMA
)
1113 au1xmmc_dbdma_shutdown(host
);
1115 tasklet_kill(&host
->data_task
);
1116 tasklet_kill(&host
->finish_task
);
1118 if (host
->platdata
&& host
->platdata
->cd_setup
&&
1119 !(mmc
->caps
& MMC_CAP_NEEDS_POLL
))
1120 host
->platdata
->cd_setup(mmc
, 0);
1122 clk_disable_unprepare(host
->clk
);
1125 free_irq(host
->irq
, host
);
1127 iounmap((void *)host
->iobase
);
1129 release_resource(host
->ioarea
);
1130 kfree(host
->ioarea
);
1137 static int au1xmmc_remove(struct platform_device
*pdev
)
1139 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1142 mmc_remove_host(host
->mmc
);
1144 #ifdef CONFIG_LEDS_CLASS
1145 if (host
->platdata
&& host
->platdata
->led
)
1146 led_classdev_unregister(host
->platdata
->led
);
1149 if (host
->platdata
&& host
->platdata
->cd_setup
&&
1150 !(host
->mmc
->caps
& MMC_CAP_NEEDS_POLL
))
1151 host
->platdata
->cd_setup(host
->mmc
, 0);
1153 __raw_writel(0, HOST_ENABLE(host
));
1154 __raw_writel(0, HOST_CONFIG(host
));
1155 __raw_writel(0, HOST_CONFIG2(host
));
1156 wmb(); /* drain writebuffer */
1158 tasklet_kill(&host
->data_task
);
1159 tasklet_kill(&host
->finish_task
);
1161 if (host
->flags
& HOST_F_DBDMA
)
1162 au1xmmc_dbdma_shutdown(host
);
1164 au1xmmc_set_power(host
, 0);
1166 clk_disable_unprepare(host
->clk
);
1169 free_irq(host
->irq
, host
);
1170 iounmap((void *)host
->iobase
);
1171 release_resource(host
->ioarea
);
1172 kfree(host
->ioarea
);
1174 mmc_free_host(host
->mmc
);
1180 static int au1xmmc_suspend(struct platform_device
*pdev
, pm_message_t state
)
1182 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1184 __raw_writel(0, HOST_CONFIG2(host
));
1185 __raw_writel(0, HOST_CONFIG(host
));
1186 __raw_writel(0xffffffff, HOST_STATUS(host
));
1187 __raw_writel(0, HOST_ENABLE(host
));
1188 wmb(); /* drain writebuffer */
1193 static int au1xmmc_resume(struct platform_device
*pdev
)
1195 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1197 au1xmmc_reset_controller(host
);
1202 #define au1xmmc_suspend NULL
1203 #define au1xmmc_resume NULL
1206 static struct platform_driver au1xmmc_driver
= {
1207 .probe
= au1xmmc_probe
,
1208 .remove
= au1xmmc_remove
,
1209 .suspend
= au1xmmc_suspend
,
1210 .resume
= au1xmmc_resume
,
1212 .name
= DRIVER_NAME
,
1216 static int __init
au1xmmc_init(void)
1219 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1220 * of 8 bits. And since devices are shared, we need to create
1221 * our own to avoid freaking out other devices.
1223 memid
= au1xxx_ddma_add_device(&au1xmmc_mem_dbdev
);
1225 pr_err("au1xmmc: cannot add memory dbdma\n");
1227 return platform_driver_register(&au1xmmc_driver
);
1230 static void __exit
au1xmmc_exit(void)
1232 if (has_dbdma() && memid
)
1233 au1xxx_ddma_del_device(memid
);
1235 platform_driver_unregister(&au1xmmc_driver
);
1238 module_init(au1xmmc_init
);
1239 module_exit(au1xmmc_exit
);
1241 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1242 MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1243 MODULE_LICENSE("GPL");
1244 MODULE_ALIAS("platform:au1xxx-mmc");