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/module.h>
36 #include <linux/init.h>
37 #include <linux/platform_device.h>
39 #include <linux/interrupt.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/scatterlist.h>
42 #include <linux/leds.h>
43 #include <linux/mmc/host.h>
46 #include <asm/mach-au1x00/au1000.h>
47 #include <asm/mach-au1x00/au1xxx_dbdma.h>
48 #include <asm/mach-au1x00/au1100_mmc.h>
50 #define DRIVER_NAME "au1xxx-mmc"
52 /* Set this to enable special debugging macros */
56 #define DBG(fmt, idx, args...) \
57 printk(KERN_DEBUG "au1xmmc(%d): DEBUG: " fmt, idx, ##args)
59 #define DBG(fmt, idx, args...) do {} while (0)
62 /* Hardware definitions */
63 #define AU1XMMC_DESCRIPTOR_COUNT 1
64 #define AU1XMMC_DESCRIPTOR_SIZE 2048
66 #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
67 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
68 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
70 /* This gives us a hard value for the stop command that we can write directly
71 * to the command register.
74 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
76 /* This is the set of interrupts that we configure by default. */
77 #define AU1XMMC_INTERRUPTS \
78 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
79 SD_CONFIG_CR | SD_CONFIG_I)
81 /* The poll event (looking for insert/remove events runs twice a second. */
82 #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
86 struct mmc_request
*mrq
;
112 struct tasklet_struct finish_task
;
113 struct tasklet_struct data_task
;
114 struct au1xmmc_platform_data
*platdata
;
115 struct platform_device
*pdev
;
116 struct resource
*ioarea
;
119 /* Status flags used by the host structure */
120 #define HOST_F_XMIT 0x0001
121 #define HOST_F_RECV 0x0002
122 #define HOST_F_DMA 0x0010
123 #define HOST_F_ACTIVE 0x0100
124 #define HOST_F_STOP 0x1000
126 #define HOST_S_IDLE 0x0001
127 #define HOST_S_CMD 0x0002
128 #define HOST_S_DATA 0x0003
129 #define HOST_S_STOP 0x0004
131 /* Easy access macros */
132 #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
133 #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
134 #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
135 #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
136 #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
137 #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
138 #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
139 #define HOST_CMD(h) ((h)->iobase + SD_CMD)
140 #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
141 #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
142 #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
144 #define DMA_CHANNEL(h) \
145 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
147 static inline void IRQ_ON(struct au1xmmc_host
*host
, u32 mask
)
149 u32 val
= au_readl(HOST_CONFIG(host
));
151 au_writel(val
, HOST_CONFIG(host
));
155 static inline void FLUSH_FIFO(struct au1xmmc_host
*host
)
157 u32 val
= au_readl(HOST_CONFIG2(host
));
159 au_writel(val
| SD_CONFIG2_FF
, HOST_CONFIG2(host
));
162 /* SEND_STOP will turn off clock control - this re-enables it */
163 val
&= ~SD_CONFIG2_DF
;
165 au_writel(val
, HOST_CONFIG2(host
));
169 static inline void IRQ_OFF(struct au1xmmc_host
*host
, u32 mask
)
171 u32 val
= au_readl(HOST_CONFIG(host
));
173 au_writel(val
, HOST_CONFIG(host
));
177 static inline void SEND_STOP(struct au1xmmc_host
*host
)
181 WARN_ON(host
->status
!= HOST_S_DATA
);
182 host
->status
= HOST_S_STOP
;
184 config2
= au_readl(HOST_CONFIG2(host
));
185 au_writel(config2
| SD_CONFIG2_DF
, HOST_CONFIG2(host
));
188 /* Send the stop commmand */
189 au_writel(STOP_CMD
, HOST_CMD(host
));
192 static void au1xmmc_set_power(struct au1xmmc_host
*host
, int state
)
194 if (host
->platdata
&& host
->platdata
->set_power
)
195 host
->platdata
->set_power(host
->mmc
, state
);
198 static int au1xmmc_card_inserted(struct mmc_host
*mmc
)
200 struct au1xmmc_host
*host
= mmc_priv(mmc
);
202 if (host
->platdata
&& host
->platdata
->card_inserted
)
203 return !!host
->platdata
->card_inserted(host
->mmc
);
208 static int au1xmmc_card_readonly(struct mmc_host
*mmc
)
210 struct au1xmmc_host
*host
= mmc_priv(mmc
);
212 if (host
->platdata
&& host
->platdata
->card_readonly
)
213 return !!host
->platdata
->card_readonly(mmc
);
218 static void au1xmmc_finish_request(struct au1xmmc_host
*host
)
220 struct mmc_request
*mrq
= host
->mrq
;
223 host
->flags
&= HOST_F_ACTIVE
| HOST_F_DMA
;
229 host
->pio
.offset
= 0;
232 host
->status
= HOST_S_IDLE
;
234 mmc_request_done(host
->mmc
, mrq
);
237 static void au1xmmc_tasklet_finish(unsigned long param
)
239 struct au1xmmc_host
*host
= (struct au1xmmc_host
*) param
;
240 au1xmmc_finish_request(host
);
243 static int au1xmmc_send_command(struct au1xmmc_host
*host
, int wait
,
244 struct mmc_command
*cmd
, struct mmc_data
*data
)
246 u32 mmccmd
= (cmd
->opcode
<< SD_CMD_CI_SHIFT
);
248 switch (mmc_resp_type(cmd
)) {
252 mmccmd
|= SD_CMD_RT_1
;
255 mmccmd
|= SD_CMD_RT_1B
;
258 mmccmd
|= SD_CMD_RT_2
;
261 mmccmd
|= SD_CMD_RT_3
;
264 printk(KERN_INFO
"au1xmmc: unhandled response type %02x\n",
270 if (data
->flags
& MMC_DATA_READ
) {
271 if (data
->blocks
> 1)
272 mmccmd
|= SD_CMD_CT_4
;
274 mmccmd
|= SD_CMD_CT_2
;
275 } else if (data
->flags
& MMC_DATA_WRITE
) {
276 if (data
->blocks
> 1)
277 mmccmd
|= SD_CMD_CT_3
;
279 mmccmd
|= SD_CMD_CT_1
;
283 au_writel(cmd
->arg
, HOST_CMDARG(host
));
287 IRQ_OFF(host
, SD_CONFIG_CR
);
289 au_writel((mmccmd
| SD_CMD_GO
), HOST_CMD(host
));
292 /* Wait for the command to go on the line */
293 while (au_readl(HOST_CMD(host
)) & SD_CMD_GO
)
296 /* Wait for the command to come back */
298 u32 status
= au_readl(HOST_STATUS(host
));
300 while (!(status
& SD_STATUS_CR
))
301 status
= au_readl(HOST_STATUS(host
));
303 /* Clear the CR status */
304 au_writel(SD_STATUS_CR
, HOST_STATUS(host
));
306 IRQ_ON(host
, SD_CONFIG_CR
);
312 static void au1xmmc_data_complete(struct au1xmmc_host
*host
, u32 status
)
314 struct mmc_request
*mrq
= host
->mrq
;
315 struct mmc_data
*data
;
318 WARN_ON((host
->status
!= HOST_S_DATA
) && (host
->status
!= HOST_S_STOP
));
320 if (host
->mrq
== NULL
)
323 data
= mrq
->cmd
->data
;
326 status
= au_readl(HOST_STATUS(host
));
328 /* The transaction is really over when the SD_STATUS_DB bit is clear */
329 while ((host
->flags
& HOST_F_XMIT
) && (status
& SD_STATUS_DB
))
330 status
= au_readl(HOST_STATUS(host
));
333 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
, host
->dma
.dir
);
335 /* Process any errors */
336 crc
= (status
& (SD_STATUS_WC
| SD_STATUS_RC
));
337 if (host
->flags
& HOST_F_XMIT
)
338 crc
|= ((status
& 0x07) == 0x02) ? 0 : 1;
341 data
->error
= -EILSEQ
;
343 /* Clear the CRC bits */
344 au_writel(SD_STATUS_WC
| SD_STATUS_RC
, HOST_STATUS(host
));
346 data
->bytes_xfered
= 0;
349 if (host
->flags
& HOST_F_DMA
) {
350 #ifdef CONFIG_SOC_AU1200 /* DBDMA */
351 u32 chan
= DMA_CHANNEL(host
);
353 chan_tab_t
*c
= *((chan_tab_t
**)chan
);
354 au1x_dma_chan_t
*cp
= c
->chan_ptr
;
355 data
->bytes_xfered
= cp
->ddma_bytecnt
;
359 (data
->blocks
* data
->blksz
) - host
->pio
.len
;
362 au1xmmc_finish_request(host
);
365 static void au1xmmc_tasklet_data(unsigned long param
)
367 struct au1xmmc_host
*host
= (struct au1xmmc_host
*)param
;
369 u32 status
= au_readl(HOST_STATUS(host
));
370 au1xmmc_data_complete(host
, status
);
373 #define AU1XMMC_MAX_TRANSFER 8
375 static void au1xmmc_send_pio(struct au1xmmc_host
*host
)
377 struct mmc_data
*data
;
378 int sg_len
, max
, count
;
379 unsigned char *sg_ptr
, val
;
381 struct scatterlist
*sg
;
383 data
= host
->mrq
->data
;
385 if (!(host
->flags
& HOST_F_XMIT
))
388 /* This is the pointer to the data buffer */
389 sg
= &data
->sg
[host
->pio
.index
];
390 sg_ptr
= sg_virt(sg
) + host
->pio
.offset
;
392 /* This is the space left inside the buffer */
393 sg_len
= data
->sg
[host
->pio
.index
].length
- host
->pio
.offset
;
395 /* Check if we need less than the size of the sg_buffer */
396 max
= (sg_len
> host
->pio
.len
) ? host
->pio
.len
: sg_len
;
397 if (max
> AU1XMMC_MAX_TRANSFER
)
398 max
= AU1XMMC_MAX_TRANSFER
;
400 for (count
= 0; count
< max
; count
++) {
401 status
= au_readl(HOST_STATUS(host
));
403 if (!(status
& SD_STATUS_TH
))
408 au_writel((unsigned long)val
, HOST_TXPORT(host
));
412 host
->pio
.len
-= count
;
413 host
->pio
.offset
+= count
;
415 if (count
== sg_len
) {
417 host
->pio
.offset
= 0;
420 if (host
->pio
.len
== 0) {
421 IRQ_OFF(host
, SD_CONFIG_TH
);
423 if (host
->flags
& HOST_F_STOP
)
426 tasklet_schedule(&host
->data_task
);
430 static void au1xmmc_receive_pio(struct au1xmmc_host
*host
)
432 struct mmc_data
*data
;
433 int max
, count
, sg_len
= 0;
434 unsigned char *sg_ptr
= NULL
;
436 struct scatterlist
*sg
;
438 data
= host
->mrq
->data
;
440 if (!(host
->flags
& HOST_F_RECV
))
445 if (host
->pio
.index
< host
->dma
.len
) {
446 sg
= &data
->sg
[host
->pio
.index
];
447 sg_ptr
= sg_virt(sg
) + host
->pio
.offset
;
449 /* This is the space left inside the buffer */
450 sg_len
= sg_dma_len(&data
->sg
[host
->pio
.index
]) - host
->pio
.offset
;
452 /* Check if we need less than the size of the sg_buffer */
457 if (max
> AU1XMMC_MAX_TRANSFER
)
458 max
= AU1XMMC_MAX_TRANSFER
;
460 for (count
= 0; count
< max
; count
++) {
461 status
= au_readl(HOST_STATUS(host
));
463 if (!(status
& SD_STATUS_NE
))
466 if (status
& SD_STATUS_RC
) {
467 DBG("RX CRC Error [%d + %d].\n", host
->pdev
->id
,
468 host
->pio
.len
, count
);
472 if (status
& SD_STATUS_RO
) {
473 DBG("RX Overrun [%d + %d]\n", host
->pdev
->id
,
474 host
->pio
.len
, count
);
477 else if (status
& SD_STATUS_RU
) {
478 DBG("RX Underrun [%d + %d]\n", host
->pdev
->id
,
479 host
->pio
.len
, count
);
483 val
= au_readl(HOST_RXPORT(host
));
486 *sg_ptr
++ = (unsigned char)(val
& 0xFF);
489 host
->pio
.len
-= count
;
490 host
->pio
.offset
+= count
;
492 if (sg_len
&& count
== sg_len
) {
494 host
->pio
.offset
= 0;
497 if (host
->pio
.len
== 0) {
498 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
499 IRQ_OFF(host
, SD_CONFIG_NE
);
501 if (host
->flags
& HOST_F_STOP
)
504 tasklet_schedule(&host
->data_task
);
508 /* This is called when a command has been completed - grab the response
509 * and check for errors. Then start the data transfer if it is indicated.
511 static void au1xmmc_cmd_complete(struct au1xmmc_host
*host
, u32 status
)
513 struct mmc_request
*mrq
= host
->mrq
;
514 struct mmc_command
*cmd
;
524 if (cmd
->flags
& MMC_RSP_PRESENT
) {
525 if (cmd
->flags
& MMC_RSP_136
) {
526 r
[0] = au_readl(host
->iobase
+ SD_RESP3
);
527 r
[1] = au_readl(host
->iobase
+ SD_RESP2
);
528 r
[2] = au_readl(host
->iobase
+ SD_RESP1
);
529 r
[3] = au_readl(host
->iobase
+ SD_RESP0
);
531 /* The CRC is omitted from the response, so really
532 * we only got 120 bytes, but the engine expects
533 * 128 bits, so we have to shift things up.
535 for (i
= 0; i
< 4; i
++) {
536 cmd
->resp
[i
] = (r
[i
] & 0x00FFFFFF) << 8;
538 cmd
->resp
[i
] |= (r
[i
+ 1] & 0xFF000000) >> 24;
541 /* Techincally, we should be getting all 48 bits of
542 * the response (SD_RESP1 + SD_RESP2), but because
543 * our response omits the CRC, our data ends up
544 * being shifted 8 bits to the right. In this case,
545 * that means that the OSR data starts at bit 31,
546 * so we can just read RESP0 and return that.
548 cmd
->resp
[0] = au_readl(host
->iobase
+ SD_RESP0
);
552 /* Figure out errors */
553 if (status
& (SD_STATUS_SC
| SD_STATUS_WC
| SD_STATUS_RC
))
554 cmd
->error
= -EILSEQ
;
556 trans
= host
->flags
& (HOST_F_XMIT
| HOST_F_RECV
);
558 if (!trans
|| cmd
->error
) {
559 IRQ_OFF(host
, SD_CONFIG_TH
| SD_CONFIG_RA
| SD_CONFIG_RF
);
560 tasklet_schedule(&host
->finish_task
);
564 host
->status
= HOST_S_DATA
;
566 if (host
->flags
& HOST_F_DMA
) {
567 #ifdef CONFIG_SOC_AU1200 /* DBDMA */
568 u32 channel
= DMA_CHANNEL(host
);
570 /* Start the DMA as soon as the buffer gets something in it */
572 if (host
->flags
& HOST_F_RECV
) {
573 u32 mask
= SD_STATUS_DB
| SD_STATUS_NE
;
575 while((status
& mask
) != mask
)
576 status
= au_readl(HOST_STATUS(host
));
579 au1xxx_dbdma_start(channel
);
584 static void au1xmmc_set_clock(struct au1xmmc_host
*host
, int rate
)
586 unsigned int pbus
= get_au1x00_speed();
587 unsigned int divisor
;
591 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
593 pbus
/= ((au_readl(SYS_POWERCTRL
) & 0x3) + 2);
595 divisor
= ((pbus
/ rate
) / 2) - 1;
597 config
= au_readl(HOST_CONFIG(host
));
599 config
&= ~(SD_CONFIG_DIV
);
600 config
|= (divisor
& SD_CONFIG_DIV
) | SD_CONFIG_DE
;
602 au_writel(config
, HOST_CONFIG(host
));
606 static int au1xmmc_prepare_data(struct au1xmmc_host
*host
,
607 struct mmc_data
*data
)
609 int datalen
= data
->blocks
* data
->blksz
;
611 if (data
->flags
& MMC_DATA_READ
)
612 host
->flags
|= HOST_F_RECV
;
614 host
->flags
|= HOST_F_XMIT
;
617 host
->flags
|= HOST_F_STOP
;
619 host
->dma
.dir
= DMA_BIDIRECTIONAL
;
621 host
->dma
.len
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
,
622 data
->sg_len
, host
->dma
.dir
);
624 if (host
->dma
.len
== 0)
627 au_writel(data
->blksz
- 1, HOST_BLKSIZE(host
));
629 if (host
->flags
& HOST_F_DMA
) {
630 #ifdef CONFIG_SOC_AU1200 /* DBDMA */
632 u32 channel
= DMA_CHANNEL(host
);
634 au1xxx_dbdma_stop(channel
);
636 for (i
= 0; i
< host
->dma
.len
; i
++) {
637 u32 ret
= 0, flags
= DDMA_FLAGS_NOIE
;
638 struct scatterlist
*sg
= &data
->sg
[i
];
639 int sg_len
= sg
->length
;
641 int len
= (datalen
> sg_len
) ? sg_len
: datalen
;
643 if (i
== host
->dma
.len
- 1)
644 flags
= DDMA_FLAGS_IE
;
646 if (host
->flags
& HOST_F_XMIT
) {
647 ret
= au1xxx_dbdma_put_source_flags(channel
,
648 (void *)sg_virt(sg
), len
, flags
);
650 ret
= au1xxx_dbdma_put_dest_flags(channel
,
651 (void *)sg_virt(sg
), len
, flags
);
662 host
->pio
.offset
= 0;
663 host
->pio
.len
= datalen
;
665 if (host
->flags
& HOST_F_XMIT
)
666 IRQ_ON(host
, SD_CONFIG_TH
);
668 IRQ_ON(host
, SD_CONFIG_NE
);
669 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
675 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
,
680 /* This actually starts a command or data transaction */
681 static void au1xmmc_request(struct mmc_host
* mmc
, struct mmc_request
* mrq
)
683 struct au1xmmc_host
*host
= mmc_priv(mmc
);
686 WARN_ON(irqs_disabled());
687 WARN_ON(host
->status
!= HOST_S_IDLE
);
690 host
->status
= HOST_S_CMD
;
692 /* fail request immediately if no card is present */
693 if (0 == au1xmmc_card_inserted(mmc
)) {
694 mrq
->cmd
->error
= -ENOMEDIUM
;
695 au1xmmc_finish_request(host
);
701 ret
= au1xmmc_prepare_data(host
, mrq
->data
);
705 ret
= au1xmmc_send_command(host
, 0, mrq
->cmd
, mrq
->data
);
708 mrq
->cmd
->error
= ret
;
709 au1xmmc_finish_request(host
);
713 static void au1xmmc_reset_controller(struct au1xmmc_host
*host
)
715 /* Apply the clock */
716 au_writel(SD_ENABLE_CE
, HOST_ENABLE(host
));
719 au_writel(SD_ENABLE_R
| SD_ENABLE_CE
, HOST_ENABLE(host
));
722 au_writel(~0, HOST_STATUS(host
));
725 au_writel(0, HOST_BLKSIZE(host
));
726 au_writel(0x001fffff, HOST_TIMEOUT(host
));
729 au_writel(SD_CONFIG2_EN
, HOST_CONFIG2(host
));
732 au_writel(SD_CONFIG2_EN
| SD_CONFIG2_FF
, HOST_CONFIG2(host
));
735 au_writel(SD_CONFIG2_EN
, HOST_CONFIG2(host
));
738 /* Configure interrupts */
739 au_writel(AU1XMMC_INTERRUPTS
, HOST_CONFIG(host
));
744 static void au1xmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
746 struct au1xmmc_host
*host
= mmc_priv(mmc
);
749 if (ios
->power_mode
== MMC_POWER_OFF
)
750 au1xmmc_set_power(host
, 0);
751 else if (ios
->power_mode
== MMC_POWER_ON
) {
752 au1xmmc_set_power(host
, 1);
755 if (ios
->clock
&& ios
->clock
!= host
->clock
) {
756 au1xmmc_set_clock(host
, ios
->clock
);
757 host
->clock
= ios
->clock
;
760 config2
= au_readl(HOST_CONFIG2(host
));
761 switch (ios
->bus_width
) {
762 case MMC_BUS_WIDTH_4
:
763 config2
|= SD_CONFIG2_WB
;
765 case MMC_BUS_WIDTH_1
:
766 config2
&= ~SD_CONFIG2_WB
;
769 au_writel(config2
, HOST_CONFIG2(host
));
773 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
774 #define STATUS_DATA_IN (SD_STATUS_NE)
775 #define STATUS_DATA_OUT (SD_STATUS_TH)
777 static irqreturn_t
au1xmmc_irq(int irq
, void *dev_id
)
779 struct au1xmmc_host
*host
= dev_id
;
782 status
= au_readl(HOST_STATUS(host
));
784 if (!(status
& SD_STATUS_I
))
785 return IRQ_NONE
; /* not ours */
787 if (status
& SD_STATUS_SI
) /* SDIO */
788 mmc_signal_sdio_irq(host
->mmc
);
790 if (host
->mrq
&& (status
& STATUS_TIMEOUT
)) {
791 if (status
& SD_STATUS_RAT
)
792 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
793 else if (status
& SD_STATUS_DT
)
794 host
->mrq
->data
->error
= -ETIMEDOUT
;
796 /* In PIO mode, interrupts might still be enabled */
797 IRQ_OFF(host
, SD_CONFIG_NE
| SD_CONFIG_TH
);
799 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
800 tasklet_schedule(&host
->finish_task
);
803 else if (status
& SD_STATUS_DD
) {
804 /* Sometimes we get a DD before a NE in PIO mode */
805 if (!(host
->flags
& HOST_F_DMA
) && (status
& SD_STATUS_NE
))
806 au1xmmc_receive_pio(host
);
808 au1xmmc_data_complete(host
, status
);
809 /* tasklet_schedule(&host->data_task); */
813 else if (status
& SD_STATUS_CR
) {
814 if (host
->status
== HOST_S_CMD
)
815 au1xmmc_cmd_complete(host
, status
);
817 } else if (!(host
->flags
& HOST_F_DMA
)) {
818 if ((host
->flags
& HOST_F_XMIT
) && (status
& STATUS_DATA_OUT
))
819 au1xmmc_send_pio(host
);
820 else if ((host
->flags
& HOST_F_RECV
) && (status
& STATUS_DATA_IN
))
821 au1xmmc_receive_pio(host
);
823 } else if (status
& 0x203F3C70) {
824 DBG("Unhandled status %8.8x\n", host
->pdev
->id
,
828 au_writel(status
, HOST_STATUS(host
));
834 #ifdef CONFIG_SOC_AU1200
835 /* 8bit memory DMA device */
836 static dbdev_tab_t au1xmmc_mem_dbdev
= {
837 .dev_id
= DSCR_CMD0_ALWAYS
,
838 .dev_flags
= DEV_FLAGS_ANYUSE
,
841 .dev_physaddr
= 0x00000000,
843 .dev_intpolarity
= 0,
847 static void au1xmmc_dbdma_callback(int irq
, void *dev_id
)
849 struct au1xmmc_host
*host
= (struct au1xmmc_host
*)dev_id
;
851 /* Avoid spurious interrupts */
855 if (host
->flags
& HOST_F_STOP
)
858 tasklet_schedule(&host
->data_task
);
861 static int au1xmmc_dbdma_init(struct au1xmmc_host
*host
)
863 struct resource
*res
;
866 res
= platform_get_resource(host
->pdev
, IORESOURCE_DMA
, 0);
871 res
= platform_get_resource(host
->pdev
, IORESOURCE_DMA
, 1);
879 host
->tx_chan
= au1xxx_dbdma_chan_alloc(memid
, txid
,
880 au1xmmc_dbdma_callback
, (void *)host
);
881 if (!host
->tx_chan
) {
882 dev_err(&host
->pdev
->dev
, "cannot allocate TX DMA\n");
886 host
->rx_chan
= au1xxx_dbdma_chan_alloc(rxid
, memid
,
887 au1xmmc_dbdma_callback
, (void *)host
);
888 if (!host
->rx_chan
) {
889 dev_err(&host
->pdev
->dev
, "cannot allocate RX DMA\n");
890 au1xxx_dbdma_chan_free(host
->tx_chan
);
894 au1xxx_dbdma_set_devwidth(host
->tx_chan
, 8);
895 au1xxx_dbdma_set_devwidth(host
->rx_chan
, 8);
897 au1xxx_dbdma_ring_alloc(host
->tx_chan
, AU1XMMC_DESCRIPTOR_COUNT
);
898 au1xxx_dbdma_ring_alloc(host
->rx_chan
, AU1XMMC_DESCRIPTOR_COUNT
);
900 /* DBDMA is good to go */
901 host
->flags
|= HOST_F_DMA
;
906 static void au1xmmc_dbdma_shutdown(struct au1xmmc_host
*host
)
908 if (host
->flags
& HOST_F_DMA
) {
909 host
->flags
&= ~HOST_F_DMA
;
910 au1xxx_dbdma_chan_free(host
->tx_chan
);
911 au1xxx_dbdma_chan_free(host
->rx_chan
);
916 static void au1xmmc_enable_sdio_irq(struct mmc_host
*mmc
, int en
)
918 struct au1xmmc_host
*host
= mmc_priv(mmc
);
921 IRQ_ON(host
, SD_CONFIG_SI
);
923 IRQ_OFF(host
, SD_CONFIG_SI
);
926 static const struct mmc_host_ops au1xmmc_ops
= {
927 .request
= au1xmmc_request
,
928 .set_ios
= au1xmmc_set_ios
,
929 .get_ro
= au1xmmc_card_readonly
,
930 .get_cd
= au1xmmc_card_inserted
,
931 .enable_sdio_irq
= au1xmmc_enable_sdio_irq
,
934 static int __devinit
au1xmmc_probe(struct platform_device
*pdev
)
936 struct mmc_host
*mmc
;
937 struct au1xmmc_host
*host
;
941 mmc
= mmc_alloc_host(sizeof(struct au1xmmc_host
), &pdev
->dev
);
943 dev_err(&pdev
->dev
, "no memory for mmc_host\n");
948 host
= mmc_priv(mmc
);
950 host
->platdata
= pdev
->dev
.platform_data
;
954 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
956 dev_err(&pdev
->dev
, "no mmio defined\n");
960 host
->ioarea
= request_mem_region(r
->start
, r
->end
- r
->start
+ 1,
963 dev_err(&pdev
->dev
, "mmio already in use\n");
967 host
->iobase
= (unsigned long)ioremap(r
->start
, 0x3c);
969 dev_err(&pdev
->dev
, "cannot remap mmio\n");
973 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
975 dev_err(&pdev
->dev
, "no IRQ defined\n");
979 host
->irq
= r
->start
;
980 /* IRQ is shared among both SD controllers */
981 ret
= request_irq(host
->irq
, au1xmmc_irq
, IRQF_SHARED
,
984 dev_err(&pdev
->dev
, "cannot grab IRQ\n");
988 mmc
->ops
= &au1xmmc_ops
;
991 mmc
->f_max
= 24000000;
993 mmc
->max_seg_size
= AU1XMMC_DESCRIPTOR_SIZE
;
994 mmc
->max_phys_segs
= AU1XMMC_DESCRIPTOR_COUNT
;
996 mmc
->max_blk_size
= 2048;
997 mmc
->max_blk_count
= 512;
999 mmc
->ocr_avail
= AU1XMMC_OCR
;
1000 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SDIO_IRQ
;
1002 host
->status
= HOST_S_IDLE
;
1004 /* board-specific carddetect setup, if any */
1005 if (host
->platdata
&& host
->platdata
->cd_setup
) {
1006 ret
= host
->platdata
->cd_setup(mmc
, 1);
1008 dev_warn(&pdev
->dev
, "board CD setup failed\n");
1009 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1012 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1014 tasklet_init(&host
->data_task
, au1xmmc_tasklet_data
,
1015 (unsigned long)host
);
1017 tasklet_init(&host
->finish_task
, au1xmmc_tasklet_finish
,
1018 (unsigned long)host
);
1020 #ifdef CONFIG_SOC_AU1200
1021 ret
= au1xmmc_dbdma_init(host
);
1023 printk(KERN_INFO DRIVER_NAME
": DBDMA init failed; using PIO\n");
1026 #ifdef CONFIG_LEDS_CLASS
1027 if (host
->platdata
&& host
->platdata
->led
) {
1028 struct led_classdev
*led
= host
->platdata
->led
;
1029 led
->name
= mmc_hostname(mmc
);
1030 led
->brightness
= LED_OFF
;
1031 led
->default_trigger
= mmc_hostname(mmc
);
1032 ret
= led_classdev_register(mmc_dev(mmc
), led
);
1038 au1xmmc_reset_controller(host
);
1040 ret
= mmc_add_host(mmc
);
1042 dev_err(&pdev
->dev
, "cannot add mmc host\n");
1046 platform_set_drvdata(pdev
, host
);
1048 printk(KERN_INFO DRIVER_NAME
": MMC Controller %d set up at %8.8X"
1049 " (mode=%s)\n", pdev
->id
, host
->iobase
,
1050 host
->flags
& HOST_F_DMA
? "dma" : "pio");
1052 return 0; /* all ok */
1055 #ifdef CONFIG_LEDS_CLASS
1056 if (host
->platdata
&& host
->platdata
->led
)
1057 led_classdev_unregister(host
->platdata
->led
);
1060 au_writel(0, HOST_ENABLE(host
));
1061 au_writel(0, HOST_CONFIG(host
));
1062 au_writel(0, HOST_CONFIG2(host
));
1065 #ifdef CONFIG_SOC_AU1200
1066 au1xmmc_dbdma_shutdown(host
);
1069 tasklet_kill(&host
->data_task
);
1070 tasklet_kill(&host
->finish_task
);
1072 if (host
->platdata
&& host
->platdata
->cd_setup
&&
1073 !(mmc
->caps
& MMC_CAP_NEEDS_POLL
))
1074 host
->platdata
->cd_setup(mmc
, 0);
1076 free_irq(host
->irq
, host
);
1078 iounmap((void *)host
->iobase
);
1080 release_resource(host
->ioarea
);
1081 kfree(host
->ioarea
);
1088 static int __devexit
au1xmmc_remove(struct platform_device
*pdev
)
1090 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1093 mmc_remove_host(host
->mmc
);
1095 #ifdef CONFIG_LEDS_CLASS
1096 if (host
->platdata
&& host
->platdata
->led
)
1097 led_classdev_unregister(host
->platdata
->led
);
1100 if (host
->platdata
&& host
->platdata
->cd_setup
&&
1101 !(host
->mmc
->caps
& MMC_CAP_NEEDS_POLL
))
1102 host
->platdata
->cd_setup(host
->mmc
, 0);
1104 au_writel(0, HOST_ENABLE(host
));
1105 au_writel(0, HOST_CONFIG(host
));
1106 au_writel(0, HOST_CONFIG2(host
));
1109 tasklet_kill(&host
->data_task
);
1110 tasklet_kill(&host
->finish_task
);
1112 #ifdef CONFIG_SOC_AU1200
1113 au1xmmc_dbdma_shutdown(host
);
1115 au1xmmc_set_power(host
, 0);
1117 free_irq(host
->irq
, host
);
1118 iounmap((void *)host
->iobase
);
1119 release_resource(host
->ioarea
);
1120 kfree(host
->ioarea
);
1122 mmc_free_host(host
->mmc
);
1123 platform_set_drvdata(pdev
, NULL
);
1129 static int au1xmmc_suspend(struct platform_device
*pdev
, pm_message_t state
)
1131 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1134 ret
= mmc_suspend_host(host
->mmc
, state
);
1138 au_writel(0, HOST_CONFIG2(host
));
1139 au_writel(0, HOST_CONFIG(host
));
1140 au_writel(0xffffffff, HOST_STATUS(host
));
1141 au_writel(0, HOST_ENABLE(host
));
1147 static int au1xmmc_resume(struct platform_device
*pdev
)
1149 struct au1xmmc_host
*host
= platform_get_drvdata(pdev
);
1151 au1xmmc_reset_controller(host
);
1153 return mmc_resume_host(host
->mmc
);
1156 #define au1xmmc_suspend NULL
1157 #define au1xmmc_resume NULL
1160 static struct platform_driver au1xmmc_driver
= {
1161 .probe
= au1xmmc_probe
,
1162 .remove
= au1xmmc_remove
,
1163 .suspend
= au1xmmc_suspend
,
1164 .resume
= au1xmmc_resume
,
1166 .name
= DRIVER_NAME
,
1167 .owner
= THIS_MODULE
,
1171 static int __init
au1xmmc_init(void)
1173 #ifdef CONFIG_SOC_AU1200
1174 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1175 * of 8 bits. And since devices are shared, we need to create
1176 * our own to avoid freaking out other devices.
1178 memid
= au1xxx_ddma_add_device(&au1xmmc_mem_dbdev
);
1180 printk(KERN_ERR
"au1xmmc: cannot add memory dbdma dev\n");
1182 return platform_driver_register(&au1xmmc_driver
);
1185 static void __exit
au1xmmc_exit(void)
1187 #ifdef CONFIG_SOC_AU1200
1189 au1xxx_ddma_del_device(memid
);
1191 platform_driver_unregister(&au1xmmc_driver
);
1194 module_init(au1xmmc_init
);
1195 module_exit(au1xmmc_exit
);
1197 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1198 MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1199 MODULE_LICENSE("GPL");
1200 MODULE_ALIAS("platform:au1xxx-mmc");