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>
44 #include <linux/slab.h>
47 #include <asm/mach-au1x00/au1000.h>
48 #include <asm/mach-au1x00/au1xxx_dbdma.h>
49 #include <asm/mach-au1x00/au1100_mmc.h>
51 #define DRIVER_NAME "au1xxx-mmc"
53 /* Set this to enable special debugging macros */
57 #define DBG(fmt, idx, args...) \
58 pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
60 #define DBG(fmt, idx, args...) do {} while (0)
63 /* Hardware definitions */
64 #define AU1XMMC_DESCRIPTOR_COUNT 1
66 /* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
67 #define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
68 #define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
70 #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
71 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
72 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
74 /* This gives us a hard value for the stop command that we can write directly
75 * to the command register.
78 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
80 /* This is the set of interrupts that we configure by default. */
81 #define AU1XMMC_INTERRUPTS \
82 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
83 SD_CONFIG_CR | SD_CONFIG_I)
85 /* The poll event (looking for insert/remove events runs twice a second. */
86 #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
90 struct mmc_request *mrq;
116 struct tasklet_struct finish_task;
117 struct tasklet_struct data_task;
118 struct au1xmmc_platform_data *platdata;
119 struct platform_device *pdev;
120 struct resource *ioarea;
123 /* Status flags used by the host structure */
124 #define HOST_F_XMIT 0x0001
125 #define HOST_F_RECV 0x0002
126 #define HOST_F_DMA 0x0010
127 #define HOST_F_DBDMA 0x0020
128 #define HOST_F_ACTIVE 0x0100
129 #define HOST_F_STOP 0x1000
131 #define HOST_S_IDLE 0x0001
132 #define HOST_S_CMD 0x0002
133 #define HOST_S_DATA 0x0003
134 #define HOST_S_STOP 0x0004
136 /* Easy access macros */
137 #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
138 #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
139 #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
140 #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
141 #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
142 #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
143 #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
144 #define HOST_CMD(h) ((h)->iobase + SD_CMD)
145 #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
146 #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
147 #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
149 #define DMA_CHANNEL(h) \
150 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
152 static inline int has_dbdma(void)
154 switch (alchemy_get_cputype()) {
155 case ALCHEMY_CPU_AU1200:
156 case ALCHEMY_CPU_AU1300:
163 static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
165 u32 val = __raw_readl(HOST_CONFIG(host));
167 __raw_writel(val, HOST_CONFIG(host));
168 wmb(); /* drain writebuffer */
171 static inline void FLUSH_FIFO(struct au1xmmc_host *host)
173 u32 val = __raw_readl(HOST_CONFIG2(host));
175 __raw_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
176 wmb(); /* drain writebuffer */
179 /* SEND_STOP will turn off clock control - this re-enables it */
180 val &= ~SD_CONFIG2_DF;
182 __raw_writel(val, HOST_CONFIG2(host));
183 wmb(); /* drain writebuffer */
186 static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
188 u32 val = __raw_readl(HOST_CONFIG(host));
190 __raw_writel(val, HOST_CONFIG(host));
191 wmb(); /* drain writebuffer */
194 static inline void SEND_STOP(struct au1xmmc_host *host)
198 WARN_ON(host->status != HOST_S_DATA);
199 host->status = HOST_S_STOP;
201 config2 = __raw_readl(HOST_CONFIG2(host));
202 __raw_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
203 wmb(); /* drain writebuffer */
205 /* Send the stop command */
206 __raw_writel(STOP_CMD, HOST_CMD(host));
207 wmb(); /* drain writebuffer */
210 static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
212 if (host->platdata && host->platdata->set_power)
213 host->platdata->set_power(host->mmc, state);
216 static int au1xmmc_card_inserted(struct mmc_host *mmc)
218 struct au1xmmc_host *host = mmc_priv(mmc);
220 if (host->platdata && host->platdata->card_inserted)
221 return !!host->platdata->card_inserted(host->mmc);
226 static int au1xmmc_card_readonly(struct mmc_host *mmc)
228 struct au1xmmc_host *host = mmc_priv(mmc);
230 if (host->platdata && host->platdata->card_readonly)
231 return !!host->platdata->card_readonly(mmc);
236 static void au1xmmc_finish_request(struct au1xmmc_host *host)
238 struct mmc_request *mrq = host->mrq;
241 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
247 host->pio.offset = 0;
250 host->status = HOST_S_IDLE;
252 mmc_request_done(host->mmc, mrq);
255 static void au1xmmc_tasklet_finish(unsigned long param)
257 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
258 au1xmmc_finish_request(host);
261 static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
262 struct mmc_command *cmd, struct mmc_data *data)
264 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
266 switch (mmc_resp_type(cmd)) {
270 mmccmd |= SD_CMD_RT_1;
273 mmccmd |= SD_CMD_RT_1B;
276 mmccmd |= SD_CMD_RT_2;
279 mmccmd |= SD_CMD_RT_3;
282 pr_info("au1xmmc: unhandled response type %02x\n",
288 if (data->flags & MMC_DATA_READ) {
289 if (data->blocks > 1)
290 mmccmd |= SD_CMD_CT_4;
292 mmccmd |= SD_CMD_CT_2;
293 } else if (data->flags & MMC_DATA_WRITE) {
294 if (data->blocks > 1)
295 mmccmd |= SD_CMD_CT_3;
297 mmccmd |= SD_CMD_CT_1;
301 __raw_writel(cmd->arg, HOST_CMDARG(host));
302 wmb(); /* drain writebuffer */
305 IRQ_OFF(host, SD_CONFIG_CR);
307 __raw_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
308 wmb(); /* drain writebuffer */
310 /* Wait for the command to go on the line */
311 while (__raw_readl(HOST_CMD(host)) & SD_CMD_GO)
314 /* Wait for the command to come back */
316 u32 status = __raw_readl(HOST_STATUS(host));
318 while (!(status & SD_STATUS_CR))
319 status = __raw_readl(HOST_STATUS(host));
321 /* Clear the CR status */
322 __raw_writel(SD_STATUS_CR, HOST_STATUS(host));
324 IRQ_ON(host, SD_CONFIG_CR);
330 static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
332 struct mmc_request *mrq = host->mrq;
333 struct mmc_data *data;
336 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
338 if (host->mrq == NULL)
341 data = mrq->cmd->data;
344 status = __raw_readl(HOST_STATUS(host));
346 /* The transaction is really over when the SD_STATUS_DB bit is clear */
347 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
348 status = __raw_readl(HOST_STATUS(host));
351 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
353 /* Process any errors */
354 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
355 if (host->flags & HOST_F_XMIT)
356 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
359 data->error = -EILSEQ;
361 /* Clear the CRC bits */
362 __raw_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
364 data->bytes_xfered = 0;
367 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
368 u32 chan = DMA_CHANNEL(host);
370 chan_tab_t *c = *((chan_tab_t **)chan);
371 au1x_dma_chan_t *cp = c->chan_ptr;
372 data->bytes_xfered = cp->ddma_bytecnt;
375 (data->blocks * data->blksz) - host->pio.len;
378 au1xmmc_finish_request(host);
381 static void au1xmmc_tasklet_data(unsigned long param)
383 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
385 u32 status = __raw_readl(HOST_STATUS(host));
386 au1xmmc_data_complete(host, status);
389 #define AU1XMMC_MAX_TRANSFER 8
391 static void au1xmmc_send_pio(struct au1xmmc_host *host)
393 struct mmc_data *data;
394 int sg_len, max, count;
395 unsigned char *sg_ptr, val;
397 struct scatterlist *sg;
399 data = host->mrq->data;
401 if (!(host->flags & HOST_F_XMIT))
404 /* This is the pointer to the data buffer */
405 sg = &data->sg[host->pio.index];
406 sg_ptr = sg_virt(sg) + host->pio.offset;
408 /* This is the space left inside the buffer */
409 sg_len = data->sg[host->pio.index].length - host->pio.offset;
411 /* Check if we need less than the size of the sg_buffer */
412 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
413 if (max > AU1XMMC_MAX_TRANSFER)
414 max = AU1XMMC_MAX_TRANSFER;
416 for (count = 0; count < max; count++) {
417 status = __raw_readl(HOST_STATUS(host));
419 if (!(status & SD_STATUS_TH))
424 __raw_writel((unsigned long)val, HOST_TXPORT(host));
425 wmb(); /* drain writebuffer */
428 host->pio.len -= count;
429 host->pio.offset += count;
431 if (count == sg_len) {
433 host->pio.offset = 0;
436 if (host->pio.len == 0) {
437 IRQ_OFF(host, SD_CONFIG_TH);
439 if (host->flags & HOST_F_STOP)
442 tasklet_schedule(&host->data_task);
446 static void au1xmmc_receive_pio(struct au1xmmc_host *host)
448 struct mmc_data *data;
449 int max, count, sg_len = 0;
450 unsigned char *sg_ptr = NULL;
452 struct scatterlist *sg;
454 data = host->mrq->data;
456 if (!(host->flags & HOST_F_RECV))
461 if (host->pio.index < host->dma.len) {
462 sg = &data->sg[host->pio.index];
463 sg_ptr = sg_virt(sg) + host->pio.offset;
465 /* This is the space left inside the buffer */
466 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
468 /* Check if we need less than the size of the sg_buffer */
473 if (max > AU1XMMC_MAX_TRANSFER)
474 max = AU1XMMC_MAX_TRANSFER;
476 for (count = 0; count < max; count++) {
477 status = __raw_readl(HOST_STATUS(host));
479 if (!(status & SD_STATUS_NE))
482 if (status & SD_STATUS_RC) {
483 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
484 host->pio.len, count);
488 if (status & SD_STATUS_RO) {
489 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
490 host->pio.len, count);
493 else if (status & SD_STATUS_RU) {
494 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
495 host->pio.len, count);
499 val = __raw_readl(HOST_RXPORT(host));
502 *sg_ptr++ = (unsigned char)(val & 0xFF);
505 host->pio.len -= count;
506 host->pio.offset += count;
508 if (sg_len && count == sg_len) {
510 host->pio.offset = 0;
513 if (host->pio.len == 0) {
514 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
515 IRQ_OFF(host, SD_CONFIG_NE);
517 if (host->flags & HOST_F_STOP)
520 tasklet_schedule(&host->data_task);
524 /* This is called when a command has been completed - grab the response
525 * and check for errors. Then start the data transfer if it is indicated.
527 static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
529 struct mmc_request *mrq = host->mrq;
530 struct mmc_command *cmd;
540 if (cmd->flags & MMC_RSP_PRESENT) {
541 if (cmd->flags & MMC_RSP_136) {
542 r[0] = __raw_readl(host->iobase + SD_RESP3);
543 r[1] = __raw_readl(host->iobase + SD_RESP2);
544 r[2] = __raw_readl(host->iobase + SD_RESP1);
545 r[3] = __raw_readl(host->iobase + SD_RESP0);
547 /* The CRC is omitted from the response, so really
548 * we only got 120 bytes, but the engine expects
549 * 128 bits, so we have to shift things up.
551 for (i = 0; i < 4; i++) {
552 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
554 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
557 /* Techincally, we should be getting all 48 bits of
558 * the response (SD_RESP1 + SD_RESP2), but because
559 * our response omits the CRC, our data ends up
560 * being shifted 8 bits to the right. In this case,
561 * that means that the OSR data starts at bit 31,
562 * so we can just read RESP0 and return that.
564 cmd->resp[0] = __raw_readl(host->iobase + SD_RESP0);
568 /* Figure out errors */
569 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
570 cmd->error = -EILSEQ;
572 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
574 if (!trans || cmd->error) {
575 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
576 tasklet_schedule(&host->finish_task);
580 host->status = HOST_S_DATA;
582 if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
583 u32 channel = DMA_CHANNEL(host);
585 /* Start the DBDMA as soon as the buffer gets something in it */
587 if (host->flags & HOST_F_RECV) {
588 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
590 while((status & mask) != mask)
591 status = __raw_readl(HOST_STATUS(host));
594 au1xxx_dbdma_start(channel);
598 static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
600 unsigned int pbus = get_au1x00_speed();
601 unsigned int divisor;
605 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
607 pbus /= ((alchemy_rdsys(AU1000_SYS_POWERCTRL) & 0x3) + 2);
609 divisor = ((pbus / rate) / 2) - 1;
611 config = __raw_readl(HOST_CONFIG(host));
613 config &= ~(SD_CONFIG_DIV);
614 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
616 __raw_writel(config, HOST_CONFIG(host));
617 wmb(); /* drain writebuffer */
620 static int au1xmmc_prepare_data(struct au1xmmc_host *host,
621 struct mmc_data *data)
623 int datalen = data->blocks * data->blksz;
625 if (data->flags & MMC_DATA_READ)
626 host->flags |= HOST_F_RECV;
628 host->flags |= HOST_F_XMIT;
631 host->flags |= HOST_F_STOP;
633 host->dma.dir = DMA_BIDIRECTIONAL;
635 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
636 data->sg_len, host->dma.dir);
638 if (host->dma.len == 0)
641 __raw_writel(data->blksz - 1, HOST_BLKSIZE(host));
643 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
645 u32 channel = DMA_CHANNEL(host);
647 au1xxx_dbdma_stop(channel);
649 for (i = 0; i < host->dma.len; i++) {
650 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
651 struct scatterlist *sg = &data->sg[i];
652 int sg_len = sg->length;
654 int len = (datalen > sg_len) ? sg_len : datalen;
656 if (i == host->dma.len - 1)
657 flags = DDMA_FLAGS_IE;
659 if (host->flags & HOST_F_XMIT) {
660 ret = au1xxx_dbdma_put_source(channel,
661 sg_phys(sg), len, flags);
663 ret = au1xxx_dbdma_put_dest(channel,
664 sg_phys(sg), len, flags);
674 host->pio.offset = 0;
675 host->pio.len = datalen;
677 if (host->flags & HOST_F_XMIT)
678 IRQ_ON(host, SD_CONFIG_TH);
680 IRQ_ON(host, SD_CONFIG_NE);
681 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
687 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
692 /* This actually starts a command or data transaction */
693 static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
695 struct au1xmmc_host *host = mmc_priv(mmc);
698 WARN_ON(irqs_disabled());
699 WARN_ON(host->status != HOST_S_IDLE);
702 host->status = HOST_S_CMD;
704 /* fail request immediately if no card is present */
705 if (0 == au1xmmc_card_inserted(mmc)) {
706 mrq->cmd->error = -ENOMEDIUM;
707 au1xmmc_finish_request(host);
713 ret = au1xmmc_prepare_data(host, mrq->data);
717 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
720 mrq->cmd->error = ret;
721 au1xmmc_finish_request(host);
725 static void au1xmmc_reset_controller(struct au1xmmc_host *host)
727 /* Apply the clock */
728 __raw_writel(SD_ENABLE_CE, HOST_ENABLE(host));
729 wmb(); /* drain writebuffer */
732 __raw_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
733 wmb(); /* drain writebuffer */
736 __raw_writel(~0, HOST_STATUS(host));
737 wmb(); /* drain writebuffer */
739 __raw_writel(0, HOST_BLKSIZE(host));
740 __raw_writel(0x001fffff, HOST_TIMEOUT(host));
741 wmb(); /* drain writebuffer */
743 __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
744 wmb(); /* drain writebuffer */
746 __raw_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
747 wmb(); /* drain writebuffer */
750 __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
751 wmb(); /* drain writebuffer */
753 /* Configure interrupts */
754 __raw_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
755 wmb(); /* drain writebuffer */
759 static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
761 struct au1xmmc_host *host = mmc_priv(mmc);
764 if (ios->power_mode == MMC_POWER_OFF)
765 au1xmmc_set_power(host, 0);
766 else if (ios->power_mode == MMC_POWER_ON) {
767 au1xmmc_set_power(host, 1);
770 if (ios->clock && ios->clock != host->clock) {
771 au1xmmc_set_clock(host, ios->clock);
772 host->clock = ios->clock;
775 config2 = __raw_readl(HOST_CONFIG2(host));
776 switch (ios->bus_width) {
777 case MMC_BUS_WIDTH_8:
778 config2 |= SD_CONFIG2_BB;
780 case MMC_BUS_WIDTH_4:
781 config2 &= ~SD_CONFIG2_BB;
782 config2 |= SD_CONFIG2_WB;
784 case MMC_BUS_WIDTH_1:
785 config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
788 __raw_writel(config2, HOST_CONFIG2(host));
789 wmb(); /* drain writebuffer */
792 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
793 #define STATUS_DATA_IN (SD_STATUS_NE)
794 #define STATUS_DATA_OUT (SD_STATUS_TH)
796 static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
798 struct au1xmmc_host *host = dev_id;
801 status = __raw_readl(HOST_STATUS(host));
803 if (!(status & SD_STATUS_I))
804 return IRQ_NONE; /* not ours */
806 if (status & SD_STATUS_SI) /* SDIO */
807 mmc_signal_sdio_irq(host->mmc);
809 if (host->mrq && (status & STATUS_TIMEOUT)) {
810 if (status & SD_STATUS_RAT)
811 host->mrq->cmd->error = -ETIMEDOUT;
812 else if (status & SD_STATUS_DT)
813 host->mrq->data->error = -ETIMEDOUT;
815 /* In PIO mode, interrupts might still be enabled */
816 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
818 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
819 tasklet_schedule(&host->finish_task);
822 else if (status & SD_STATUS_DD) {
823 /* Sometimes we get a DD before a NE in PIO mode */
824 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
825 au1xmmc_receive_pio(host);
827 au1xmmc_data_complete(host, status);
828 /* tasklet_schedule(&host->data_task); */
832 else if (status & SD_STATUS_CR) {
833 if (host->status == HOST_S_CMD)
834 au1xmmc_cmd_complete(host, status);
836 } else if (!(host->flags & HOST_F_DMA)) {
837 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
838 au1xmmc_send_pio(host);
839 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
840 au1xmmc_receive_pio(host);
842 } else if (status & 0x203F3C70) {
843 DBG("Unhandled status %8.8x\n", host->pdev->id,
847 __raw_writel(status, HOST_STATUS(host));
848 wmb(); /* drain writebuffer */
853 /* 8bit memory DMA device */
854 static dbdev_tab_t au1xmmc_mem_dbdev = {
855 .dev_id = DSCR_CMD0_ALWAYS,
856 .dev_flags = DEV_FLAGS_ANYUSE,
859 .dev_physaddr = 0x00000000,
861 .dev_intpolarity = 0,
865 static void au1xmmc_dbdma_callback(int irq, void *dev_id)
867 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
869 /* Avoid spurious interrupts */
873 if (host->flags & HOST_F_STOP)
876 tasklet_schedule(&host->data_task);
879 static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
881 struct resource *res;
884 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
889 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
897 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
898 au1xmmc_dbdma_callback, (void *)host);
899 if (!host->tx_chan) {
900 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
904 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
905 au1xmmc_dbdma_callback, (void *)host);
906 if (!host->rx_chan) {
907 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
908 au1xxx_dbdma_chan_free(host->tx_chan);
912 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
913 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
915 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
916 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
918 /* DBDMA is good to go */
919 host->flags |= HOST_F_DMA | HOST_F_DBDMA;
924 static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
926 if (host->flags & HOST_F_DMA) {
927 host->flags &= ~HOST_F_DMA;
928 au1xxx_dbdma_chan_free(host->tx_chan);
929 au1xxx_dbdma_chan_free(host->rx_chan);
933 static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
935 struct au1xmmc_host *host = mmc_priv(mmc);
938 IRQ_ON(host, SD_CONFIG_SI);
940 IRQ_OFF(host, SD_CONFIG_SI);
943 static const struct mmc_host_ops au1xmmc_ops = {
944 .request = au1xmmc_request,
945 .set_ios = au1xmmc_set_ios,
946 .get_ro = au1xmmc_card_readonly,
947 .get_cd = au1xmmc_card_inserted,
948 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
951 static int au1xmmc_probe(struct platform_device *pdev)
953 struct mmc_host *mmc;
954 struct au1xmmc_host *host;
958 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
960 dev_err(&pdev->dev, "no memory for mmc_host\n");
965 host = mmc_priv(mmc);
967 host->platdata = pdev->dev.platform_data;
971 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
973 dev_err(&pdev->dev, "no mmio defined\n");
977 host->ioarea = request_mem_region(r->start, resource_size(r),
980 dev_err(&pdev->dev, "mmio already in use\n");
984 host->iobase = ioremap(r->start, 0x3c);
986 dev_err(&pdev->dev, "cannot remap mmio\n");
990 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
992 dev_err(&pdev->dev, "no IRQ defined\n");
995 host->irq = r->start;
997 mmc->ops = &au1xmmc_ops;
1000 mmc->f_max = 24000000;
1002 mmc->max_blk_size = 2048;
1003 mmc->max_blk_count = 512;
1005 mmc->ocr_avail = AU1XMMC_OCR;
1006 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1007 mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
1009 iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
1011 switch (alchemy_get_cputype()) {
1012 case ALCHEMY_CPU_AU1100:
1013 mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
1015 case ALCHEMY_CPU_AU1200:
1016 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1018 case ALCHEMY_CPU_AU1300:
1019 iflag = 0; /* nothing is shared */
1020 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1021 mmc->f_max = 52000000;
1022 if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
1023 mmc->caps |= MMC_CAP_8_BIT_DATA;
1027 ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
1029 dev_err(&pdev->dev, "cannot grab IRQ\n");
1033 host->status = HOST_S_IDLE;
1035 /* board-specific carddetect setup, if any */
1036 if (host->platdata && host->platdata->cd_setup) {
1037 ret = host->platdata->cd_setup(mmc, 1);
1039 dev_warn(&pdev->dev, "board CD setup failed\n");
1040 mmc->caps |= MMC_CAP_NEEDS_POLL;
1043 mmc->caps |= MMC_CAP_NEEDS_POLL;
1045 /* platform may not be able to use all advertised caps */
1047 mmc->caps &= ~(host->platdata->mask_host_caps);
1049 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1050 (unsigned long)host);
1052 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1053 (unsigned long)host);
1056 ret = au1xmmc_dbdma_init(host);
1058 pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
1061 #ifdef CONFIG_LEDS_CLASS
1062 if (host->platdata && host->platdata->led) {
1063 struct led_classdev *led = host->platdata->led;
1064 led->name = mmc_hostname(mmc);
1065 led->brightness = LED_OFF;
1066 led->default_trigger = mmc_hostname(mmc);
1067 ret = led_classdev_register(mmc_dev(mmc), led);
1073 au1xmmc_reset_controller(host);
1075 ret = mmc_add_host(mmc);
1077 dev_err(&pdev->dev, "cannot add mmc host\n");
1081 platform_set_drvdata(pdev, host);
1083 pr_info(DRIVER_NAME ": MMC Controller %d set up at %p"
1084 " (mode=%s)\n", pdev->id, host->iobase,
1085 host->flags & HOST_F_DMA ? "dma" : "pio");
1087 return 0; /* all ok */
1090 #ifdef CONFIG_LEDS_CLASS
1091 if (host->platdata && host->platdata->led)
1092 led_classdev_unregister(host->platdata->led);
1095 __raw_writel(0, HOST_ENABLE(host));
1096 __raw_writel(0, HOST_CONFIG(host));
1097 __raw_writel(0, HOST_CONFIG2(host));
1098 wmb(); /* drain writebuffer */
1100 if (host->flags & HOST_F_DBDMA)
1101 au1xmmc_dbdma_shutdown(host);
1103 tasklet_kill(&host->data_task);
1104 tasklet_kill(&host->finish_task);
1106 if (host->platdata && host->platdata->cd_setup &&
1107 !(mmc->caps & MMC_CAP_NEEDS_POLL))
1108 host->platdata->cd_setup(mmc, 0);
1110 free_irq(host->irq, host);
1112 iounmap((void *)host->iobase);
1114 release_resource(host->ioarea);
1115 kfree(host->ioarea);
1122 static int au1xmmc_remove(struct platform_device *pdev)
1124 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1127 mmc_remove_host(host->mmc);
1129 #ifdef CONFIG_LEDS_CLASS
1130 if (host->platdata && host->platdata->led)
1131 led_classdev_unregister(host->platdata->led);
1134 if (host->platdata && host->platdata->cd_setup &&
1135 !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
1136 host->platdata->cd_setup(host->mmc, 0);
1138 __raw_writel(0, HOST_ENABLE(host));
1139 __raw_writel(0, HOST_CONFIG(host));
1140 __raw_writel(0, HOST_CONFIG2(host));
1141 wmb(); /* drain writebuffer */
1143 tasklet_kill(&host->data_task);
1144 tasklet_kill(&host->finish_task);
1146 if (host->flags & HOST_F_DBDMA)
1147 au1xmmc_dbdma_shutdown(host);
1149 au1xmmc_set_power(host, 0);
1151 free_irq(host->irq, host);
1152 iounmap((void *)host->iobase);
1153 release_resource(host->ioarea);
1154 kfree(host->ioarea);
1156 mmc_free_host(host->mmc);
1162 static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
1164 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1166 __raw_writel(0, HOST_CONFIG2(host));
1167 __raw_writel(0, HOST_CONFIG(host));
1168 __raw_writel(0xffffffff, HOST_STATUS(host));
1169 __raw_writel(0, HOST_ENABLE(host));
1170 wmb(); /* drain writebuffer */
1175 static int au1xmmc_resume(struct platform_device *pdev)
1177 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1179 au1xmmc_reset_controller(host);
1184 #define au1xmmc_suspend NULL
1185 #define au1xmmc_resume NULL
1188 static struct platform_driver au1xmmc_driver = {
1189 .probe = au1xmmc_probe,
1190 .remove = au1xmmc_remove,
1191 .suspend = au1xmmc_suspend,
1192 .resume = au1xmmc_resume,
1194 .name = DRIVER_NAME,
1195 .owner = THIS_MODULE,
1199 static int __init au1xmmc_init(void)
1202 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1203 * of 8 bits. And since devices are shared, we need to create
1204 * our own to avoid freaking out other devices.
1206 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1208 pr_err("au1xmmc: cannot add memory dbdma\n");
1210 return platform_driver_register(&au1xmmc_driver);
1213 static void __exit au1xmmc_exit(void)
1215 if (has_dbdma() && memid)
1216 au1xxx_ddma_del_device(memid);
1218 platform_driver_unregister(&au1xmmc_driver);
1221 module_init(au1xmmc_init);
1222 module_exit(au1xmmc_exit);
1224 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1225 MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1226 MODULE_LICENSE("GPL");
1227 MODULE_ALIAS("platform:au1xxx-mmc");