backtrace: replace timer with tasklet + completions
[linux-2.6/mini2440.git] / drivers / mmc / host / au1xmmc.c
blobcc5f7bc546afc95d5379faca5634a1f439363e50
1 /*
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 is a timer used to detect insert events?
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).
34 * So we use the timer to check the status manually.
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/platform_device.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/scatterlist.h>
45 #include <linux/mmc/host.h>
46 #include <asm/io.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 #include <au1xxx.h>
52 #include "au1xmmc.h"
54 #define DRIVER_NAME "au1xxx-mmc"
56 /* Set this to enable special debugging macros */
58 #ifdef DEBUG
59 #define DBG(fmt, idx, args...) printk("au1xx(%d): DEBUG: " fmt, idx, ##args)
60 #else
61 #define DBG(fmt, idx, args...)
62 #endif
64 const struct {
65 u32 iobase;
66 u32 tx_devid, rx_devid;
67 u16 bcsrpwr;
68 u16 bcsrstatus;
69 u16 wpstatus;
70 } au1xmmc_card_table[] = {
71 { SD0_BASE, DSCR_CMD0_SDMS_TX0, DSCR_CMD0_SDMS_RX0,
72 BCSR_BOARD_SD0PWR, BCSR_INT_SD0INSERT, BCSR_STATUS_SD0WP },
73 #ifndef CONFIG_MIPS_DB1200
74 { SD1_BASE, DSCR_CMD0_SDMS_TX1, DSCR_CMD0_SDMS_RX1,
75 BCSR_BOARD_DS1PWR, BCSR_INT_SD1INSERT, BCSR_STATUS_SD1WP }
76 #endif
79 #define AU1XMMC_CONTROLLER_COUNT (ARRAY_SIZE(au1xmmc_card_table))
81 /* This array stores pointers for the hosts (used by the IRQ handler) */
82 struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT];
83 static int dma = 1;
85 #ifdef MODULE
86 module_param(dma, bool, 0);
87 MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)");
88 #endif
90 static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
92 u32 val = au_readl(HOST_CONFIG(host));
93 val |= mask;
94 au_writel(val, HOST_CONFIG(host));
95 au_sync();
98 static inline void FLUSH_FIFO(struct au1xmmc_host *host)
100 u32 val = au_readl(HOST_CONFIG2(host));
102 au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
103 au_sync_delay(1);
105 /* SEND_STOP will turn off clock control - this re-enables it */
106 val &= ~SD_CONFIG2_DF;
108 au_writel(val, HOST_CONFIG2(host));
109 au_sync();
112 static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
114 u32 val = au_readl(HOST_CONFIG(host));
115 val &= ~mask;
116 au_writel(val, HOST_CONFIG(host));
117 au_sync();
120 static inline void SEND_STOP(struct au1xmmc_host *host)
123 /* We know the value of CONFIG2, so avoid a read we don't need */
124 u32 mask = SD_CONFIG2_EN;
126 WARN_ON(host->status != HOST_S_DATA);
127 host->status = HOST_S_STOP;
129 au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host));
130 au_sync();
132 /* Send the stop commmand */
133 au_writel(STOP_CMD, HOST_CMD(host));
136 static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
139 u32 val = au1xmmc_card_table[host->id].bcsrpwr;
141 bcsr->board &= ~val;
142 if (state) bcsr->board |= val;
144 au_sync_delay(1);
147 static inline int au1xmmc_card_inserted(struct au1xmmc_host *host)
149 return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus)
150 ? 1 : 0;
153 static int au1xmmc_card_readonly(struct mmc_host *mmc)
155 struct au1xmmc_host *host = mmc_priv(mmc);
156 return (bcsr->status & au1xmmc_card_table[host->id].wpstatus)
157 ? 1 : 0;
160 static void au1xmmc_finish_request(struct au1xmmc_host *host)
163 struct mmc_request *mrq = host->mrq;
165 host->mrq = NULL;
166 host->flags &= HOST_F_ACTIVE;
168 host->dma.len = 0;
169 host->dma.dir = 0;
171 host->pio.index = 0;
172 host->pio.offset = 0;
173 host->pio.len = 0;
175 host->status = HOST_S_IDLE;
177 bcsr->disk_leds |= (1 << 8);
179 mmc_request_done(host->mmc, mrq);
182 static void au1xmmc_tasklet_finish(unsigned long param)
184 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
185 au1xmmc_finish_request(host);
188 static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
189 struct mmc_command *cmd, struct mmc_data *data)
191 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
193 switch (mmc_resp_type(cmd)) {
194 case MMC_RSP_NONE:
195 break;
196 case MMC_RSP_R1:
197 mmccmd |= SD_CMD_RT_1;
198 break;
199 case MMC_RSP_R1B:
200 mmccmd |= SD_CMD_RT_1B;
201 break;
202 case MMC_RSP_R2:
203 mmccmd |= SD_CMD_RT_2;
204 break;
205 case MMC_RSP_R3:
206 mmccmd |= SD_CMD_RT_3;
207 break;
208 default:
209 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
210 mmc_resp_type(cmd));
211 return -EINVAL;
214 if (data) {
215 if (data->flags & MMC_DATA_READ) {
216 if (data->blocks > 1)
217 mmccmd |= SD_CMD_CT_4;
218 else
219 mmccmd |= SD_CMD_CT_2;
220 } else if (data->flags & MMC_DATA_WRITE) {
221 if (data->blocks > 1)
222 mmccmd |= SD_CMD_CT_3;
223 else
224 mmccmd |= SD_CMD_CT_1;
228 au_writel(cmd->arg, HOST_CMDARG(host));
229 au_sync();
231 if (wait)
232 IRQ_OFF(host, SD_CONFIG_CR);
234 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
235 au_sync();
237 /* Wait for the command to go on the line */
239 while(1) {
240 if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
241 break;
244 /* Wait for the command to come back */
246 if (wait) {
247 u32 status = au_readl(HOST_STATUS(host));
249 while(!(status & SD_STATUS_CR))
250 status = au_readl(HOST_STATUS(host));
252 /* Clear the CR status */
253 au_writel(SD_STATUS_CR, HOST_STATUS(host));
255 IRQ_ON(host, SD_CONFIG_CR);
258 return 0;
261 static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
264 struct mmc_request *mrq = host->mrq;
265 struct mmc_data *data;
266 u32 crc;
268 WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP);
270 if (host->mrq == NULL)
271 return;
273 data = mrq->cmd->data;
275 if (status == 0)
276 status = au_readl(HOST_STATUS(host));
278 /* The transaction is really over when the SD_STATUS_DB bit is clear */
280 while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
281 status = au_readl(HOST_STATUS(host));
283 data->error = 0;
284 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
286 /* Process any errors */
288 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
289 if (host->flags & HOST_F_XMIT)
290 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
292 if (crc)
293 data->error = -EILSEQ;
295 /* Clear the CRC bits */
296 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
298 data->bytes_xfered = 0;
300 if (!data->error) {
301 if (host->flags & HOST_F_DMA) {
302 u32 chan = DMA_CHANNEL(host);
304 chan_tab_t *c = *((chan_tab_t **) chan);
305 au1x_dma_chan_t *cp = c->chan_ptr;
306 data->bytes_xfered = cp->ddma_bytecnt;
308 else
309 data->bytes_xfered =
310 (data->blocks * data->blksz) -
311 host->pio.len;
314 au1xmmc_finish_request(host);
317 static void au1xmmc_tasklet_data(unsigned long param)
319 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
321 u32 status = au_readl(HOST_STATUS(host));
322 au1xmmc_data_complete(host, status);
325 #define AU1XMMC_MAX_TRANSFER 8
327 static void au1xmmc_send_pio(struct au1xmmc_host *host)
330 struct mmc_data *data = 0;
331 int sg_len, max, count = 0;
332 unsigned char *sg_ptr;
333 u32 status = 0;
334 struct scatterlist *sg;
336 data = host->mrq->data;
338 if (!(host->flags & HOST_F_XMIT))
339 return;
341 /* This is the pointer to the data buffer */
342 sg = &data->sg[host->pio.index];
343 sg_ptr = sg_virt(sg) + host->pio.offset;
345 /* This is the space left inside the buffer */
346 sg_len = data->sg[host->pio.index].length - host->pio.offset;
348 /* Check to if we need less then the size of the sg_buffer */
350 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
351 if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER;
353 for(count = 0; count < max; count++ ) {
354 unsigned char val;
356 status = au_readl(HOST_STATUS(host));
358 if (!(status & SD_STATUS_TH))
359 break;
361 val = *sg_ptr++;
363 au_writel((unsigned long) val, HOST_TXPORT(host));
364 au_sync();
367 host->pio.len -= count;
368 host->pio.offset += count;
370 if (count == sg_len) {
371 host->pio.index++;
372 host->pio.offset = 0;
375 if (host->pio.len == 0) {
376 IRQ_OFF(host, SD_CONFIG_TH);
378 if (host->flags & HOST_F_STOP)
379 SEND_STOP(host);
381 tasklet_schedule(&host->data_task);
385 static void au1xmmc_receive_pio(struct au1xmmc_host *host)
388 struct mmc_data *data = 0;
389 int sg_len = 0, max = 0, count = 0;
390 unsigned char *sg_ptr = 0;
391 u32 status = 0;
392 struct scatterlist *sg;
394 data = host->mrq->data;
396 if (!(host->flags & HOST_F_RECV))
397 return;
399 max = host->pio.len;
401 if (host->pio.index < host->dma.len) {
402 sg = &data->sg[host->pio.index];
403 sg_ptr = sg_virt(sg) + host->pio.offset;
405 /* This is the space left inside the buffer */
406 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
408 /* Check to if we need less then the size of the sg_buffer */
409 if (sg_len < max) max = sg_len;
412 if (max > AU1XMMC_MAX_TRANSFER)
413 max = AU1XMMC_MAX_TRANSFER;
415 for(count = 0; count < max; count++ ) {
416 u32 val;
417 status = au_readl(HOST_STATUS(host));
419 if (!(status & SD_STATUS_NE))
420 break;
422 if (status & SD_STATUS_RC) {
423 DBG("RX CRC Error [%d + %d].\n", host->id,
424 host->pio.len, count);
425 break;
428 if (status & SD_STATUS_RO) {
429 DBG("RX Overrun [%d + %d]\n", host->id,
430 host->pio.len, count);
431 break;
433 else if (status & SD_STATUS_RU) {
434 DBG("RX Underrun [%d + %d]\n", host->id,
435 host->pio.len, count);
436 break;
439 val = au_readl(HOST_RXPORT(host));
441 if (sg_ptr)
442 *sg_ptr++ = (unsigned char) (val & 0xFF);
445 host->pio.len -= count;
446 host->pio.offset += count;
448 if (sg_len && count == sg_len) {
449 host->pio.index++;
450 host->pio.offset = 0;
453 if (host->pio.len == 0) {
454 //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF);
455 IRQ_OFF(host, SD_CONFIG_NE);
457 if (host->flags & HOST_F_STOP)
458 SEND_STOP(host);
460 tasklet_schedule(&host->data_task);
464 /* static void au1xmmc_cmd_complete
465 This is called when a command has been completed - grab the response
466 and check for errors. Then start the data transfer if it is indicated.
469 static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
472 struct mmc_request *mrq = host->mrq;
473 struct mmc_command *cmd;
474 int trans;
476 if (!host->mrq)
477 return;
479 cmd = mrq->cmd;
480 cmd->error = 0;
482 if (cmd->flags & MMC_RSP_PRESENT) {
483 if (cmd->flags & MMC_RSP_136) {
484 u32 r[4];
485 int i;
487 r[0] = au_readl(host->iobase + SD_RESP3);
488 r[1] = au_readl(host->iobase + SD_RESP2);
489 r[2] = au_readl(host->iobase + SD_RESP1);
490 r[3] = au_readl(host->iobase + SD_RESP0);
492 /* The CRC is omitted from the response, so really
493 * we only got 120 bytes, but the engine expects
494 * 128 bits, so we have to shift things up
497 for(i = 0; i < 4; i++) {
498 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
499 if (i != 3)
500 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
502 } else {
503 /* Techincally, we should be getting all 48 bits of
504 * the response (SD_RESP1 + SD_RESP2), but because
505 * our response omits the CRC, our data ends up
506 * being shifted 8 bits to the right. In this case,
507 * that means that the OSR data starts at bit 31,
508 * so we can just read RESP0 and return that
510 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
514 /* Figure out errors */
516 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
517 cmd->error = -EILSEQ;
519 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
521 if (!trans || cmd->error) {
523 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
524 tasklet_schedule(&host->finish_task);
525 return;
528 host->status = HOST_S_DATA;
530 if (host->flags & HOST_F_DMA) {
531 u32 channel = DMA_CHANNEL(host);
533 /* Start the DMA as soon as the buffer gets something in it */
535 if (host->flags & HOST_F_RECV) {
536 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
538 while((status & mask) != mask)
539 status = au_readl(HOST_STATUS(host));
542 au1xxx_dbdma_start(channel);
546 static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
549 unsigned int pbus = get_au1x00_speed();
550 unsigned int divisor;
551 u32 config;
553 /* From databook:
554 divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
557 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
558 pbus /= 2;
560 divisor = ((pbus / rate) / 2) - 1;
562 config = au_readl(HOST_CONFIG(host));
564 config &= ~(SD_CONFIG_DIV);
565 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
567 au_writel(config, HOST_CONFIG(host));
568 au_sync();
571 static int
572 au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
575 int datalen = data->blocks * data->blksz;
577 if (dma != 0)
578 host->flags |= HOST_F_DMA;
580 if (data->flags & MMC_DATA_READ)
581 host->flags |= HOST_F_RECV;
582 else
583 host->flags |= HOST_F_XMIT;
585 if (host->mrq->stop)
586 host->flags |= HOST_F_STOP;
588 host->dma.dir = DMA_BIDIRECTIONAL;
590 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
591 data->sg_len, host->dma.dir);
593 if (host->dma.len == 0)
594 return -ETIMEDOUT;
596 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
598 if (host->flags & HOST_F_DMA) {
599 int i;
600 u32 channel = DMA_CHANNEL(host);
602 au1xxx_dbdma_stop(channel);
604 for(i = 0; i < host->dma.len; i++) {
605 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
606 struct scatterlist *sg = &data->sg[i];
607 int sg_len = sg->length;
609 int len = (datalen > sg_len) ? sg_len : datalen;
611 if (i == host->dma.len - 1)
612 flags = DDMA_FLAGS_IE;
614 if (host->flags & HOST_F_XMIT){
615 ret = au1xxx_dbdma_put_source_flags(channel,
616 (void *) sg_virt(sg), len, flags);
618 else {
619 ret = au1xxx_dbdma_put_dest_flags(channel,
620 (void *) sg_virt(sg),
621 len, flags);
624 if (!ret)
625 goto dataerr;
627 datalen -= len;
630 else {
631 host->pio.index = 0;
632 host->pio.offset = 0;
633 host->pio.len = datalen;
635 if (host->flags & HOST_F_XMIT)
636 IRQ_ON(host, SD_CONFIG_TH);
637 else
638 IRQ_ON(host, SD_CONFIG_NE);
639 //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
642 return 0;
644 dataerr:
645 dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
646 return -ETIMEDOUT;
649 /* static void au1xmmc_request
650 This actually starts a command or data transaction
653 static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
656 struct au1xmmc_host *host = mmc_priv(mmc);
657 unsigned int flags = 0;
658 int ret = 0;
660 WARN_ON(irqs_disabled());
661 WARN_ON(host->status != HOST_S_IDLE);
663 host->mrq = mrq;
664 host->status = HOST_S_CMD;
666 bcsr->disk_leds &= ~(1 << 8);
668 if (mrq->data) {
669 FLUSH_FIFO(host);
670 flags = mrq->data->flags;
671 ret = au1xmmc_prepare_data(host, mrq->data);
674 if (!ret)
675 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
677 if (ret) {
678 mrq->cmd->error = ret;
679 au1xmmc_finish_request(host);
683 static void au1xmmc_reset_controller(struct au1xmmc_host *host)
686 /* Apply the clock */
687 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
688 au_sync_delay(1);
690 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
691 au_sync_delay(5);
693 au_writel(~0, HOST_STATUS(host));
694 au_sync();
696 au_writel(0, HOST_BLKSIZE(host));
697 au_writel(0x001fffff, HOST_TIMEOUT(host));
698 au_sync();
700 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
701 au_sync();
703 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
704 au_sync_delay(1);
706 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
707 au_sync();
709 /* Configure interrupts */
710 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
711 au_sync();
715 static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
717 struct au1xmmc_host *host = mmc_priv(mmc);
719 if (ios->power_mode == MMC_POWER_OFF)
720 au1xmmc_set_power(host, 0);
721 else if (ios->power_mode == MMC_POWER_ON) {
722 au1xmmc_set_power(host, 1);
725 if (ios->clock && ios->clock != host->clock) {
726 au1xmmc_set_clock(host, ios->clock);
727 host->clock = ios->clock;
731 static void au1xmmc_dma_callback(int irq, void *dev_id)
733 struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
735 /* Avoid spurious interrupts */
737 if (!host->mrq)
738 return;
740 if (host->flags & HOST_F_STOP)
741 SEND_STOP(host);
743 tasklet_schedule(&host->data_task);
746 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
747 #define STATUS_DATA_IN (SD_STATUS_NE)
748 #define STATUS_DATA_OUT (SD_STATUS_TH)
750 static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
753 u32 status;
754 int i, ret = 0;
756 disable_irq(AU1100_SD_IRQ);
758 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
759 struct au1xmmc_host * host = au1xmmc_hosts[i];
760 u32 handled = 1;
762 status = au_readl(HOST_STATUS(host));
764 if (host->mrq && (status & STATUS_TIMEOUT)) {
765 if (status & SD_STATUS_RAT)
766 host->mrq->cmd->error = -ETIMEDOUT;
768 else if (status & SD_STATUS_DT)
769 host->mrq->data->error = -ETIMEDOUT;
771 /* In PIO mode, interrupts might still be enabled */
772 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
774 //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
775 tasklet_schedule(&host->finish_task);
777 #if 0
778 else if (status & SD_STATUS_DD) {
780 /* Sometimes we get a DD before a NE in PIO mode */
782 if (!(host->flags & HOST_F_DMA) &&
783 (status & SD_STATUS_NE))
784 au1xmmc_receive_pio(host);
785 else {
786 au1xmmc_data_complete(host, status);
787 //tasklet_schedule(&host->data_task);
790 #endif
791 else if (status & (SD_STATUS_CR)) {
792 if (host->status == HOST_S_CMD)
793 au1xmmc_cmd_complete(host,status);
795 else if (!(host->flags & HOST_F_DMA)) {
796 if ((host->flags & HOST_F_XMIT) &&
797 (status & STATUS_DATA_OUT))
798 au1xmmc_send_pio(host);
799 else if ((host->flags & HOST_F_RECV) &&
800 (status & STATUS_DATA_IN))
801 au1xmmc_receive_pio(host);
803 else if (status & 0x203FBC70) {
804 DBG("Unhandled status %8.8x\n", host->id, status);
805 handled = 0;
808 au_writel(status, HOST_STATUS(host));
809 au_sync();
811 ret |= handled;
814 enable_irq(AU1100_SD_IRQ);
815 return ret;
818 static void au1xmmc_poll_event(unsigned long arg)
820 struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
822 int card = au1xmmc_card_inserted(host);
823 int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
825 if (card != controller) {
826 host->flags &= ~HOST_F_ACTIVE;
827 if (card) host->flags |= HOST_F_ACTIVE;
828 mmc_detect_change(host->mmc, 0);
831 if (host->mrq != NULL) {
832 u32 status = au_readl(HOST_STATUS(host));
833 DBG("PENDING - %8.8x\n", host->id, status);
836 mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
839 static dbdev_tab_t au1xmmc_mem_dbdev =
841 DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
844 static void au1xmmc_init_dma(struct au1xmmc_host *host)
847 u32 rxchan, txchan;
849 int txid = au1xmmc_card_table[host->id].tx_devid;
850 int rxid = au1xmmc_card_table[host->id].rx_devid;
852 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
853 of 8 bits. And since devices are shared, we need to create
854 our own to avoid freaking out other devices
857 int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
859 txchan = au1xxx_dbdma_chan_alloc(memid, txid,
860 au1xmmc_dma_callback, (void *) host);
862 rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
863 au1xmmc_dma_callback, (void *) host);
865 au1xxx_dbdma_set_devwidth(txchan, 8);
866 au1xxx_dbdma_set_devwidth(rxchan, 8);
868 au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
869 au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
871 host->tx_chan = txchan;
872 host->rx_chan = rxchan;
875 static const struct mmc_host_ops au1xmmc_ops = {
876 .request = au1xmmc_request,
877 .set_ios = au1xmmc_set_ios,
878 .get_ro = au1xmmc_card_readonly,
881 static int __devinit au1xmmc_probe(struct platform_device *pdev)
884 int i, ret = 0;
886 /* THe interrupt is shared among all controllers */
887 ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, IRQF_DISABLED, "MMC", 0);
889 if (ret) {
890 printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
891 AU1100_SD_IRQ, ret);
892 return -ENXIO;
895 disable_irq(AU1100_SD_IRQ);
897 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
898 struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
899 struct au1xmmc_host *host = 0;
901 if (!mmc) {
902 printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
903 au1xmmc_hosts[i] = 0;
904 continue;
907 mmc->ops = &au1xmmc_ops;
909 mmc->f_min = 450000;
910 mmc->f_max = 24000000;
912 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
913 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
915 mmc->max_blk_size = 2048;
916 mmc->max_blk_count = 512;
918 mmc->ocr_avail = AU1XMMC_OCR;
920 host = mmc_priv(mmc);
921 host->mmc = mmc;
923 host->id = i;
924 host->iobase = au1xmmc_card_table[host->id].iobase;
925 host->clock = 0;
926 host->power_mode = MMC_POWER_OFF;
928 host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
929 host->status = HOST_S_IDLE;
931 init_timer(&host->timer);
933 host->timer.function = au1xmmc_poll_event;
934 host->timer.data = (unsigned long) host;
935 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
937 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
938 (unsigned long) host);
940 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
941 (unsigned long) host);
943 spin_lock_init(&host->lock);
945 if (dma != 0)
946 au1xmmc_init_dma(host);
948 au1xmmc_reset_controller(host);
950 mmc_add_host(mmc);
951 au1xmmc_hosts[i] = host;
953 add_timer(&host->timer);
955 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
956 host->id, host->iobase, dma ? "dma" : "pio");
959 enable_irq(AU1100_SD_IRQ);
961 return 0;
964 static int __devexit au1xmmc_remove(struct platform_device *pdev)
967 int i;
969 disable_irq(AU1100_SD_IRQ);
971 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
972 struct au1xmmc_host *host = au1xmmc_hosts[i];
973 if (!host) continue;
975 tasklet_kill(&host->data_task);
976 tasklet_kill(&host->finish_task);
978 del_timer_sync(&host->timer);
979 au1xmmc_set_power(host, 0);
981 mmc_remove_host(host->mmc);
983 au1xxx_dbdma_chan_free(host->tx_chan);
984 au1xxx_dbdma_chan_free(host->rx_chan);
986 au_writel(0x0, HOST_ENABLE(host));
987 au_sync();
990 free_irq(AU1100_SD_IRQ, 0);
991 return 0;
994 static struct platform_driver au1xmmc_driver = {
995 .probe = au1xmmc_probe,
996 .remove = au1xmmc_remove,
997 .suspend = NULL,
998 .resume = NULL,
999 .driver = {
1000 .name = DRIVER_NAME,
1001 .owner = THIS_MODULE,
1005 static int __init au1xmmc_init(void)
1007 return platform_driver_register(&au1xmmc_driver);
1010 static void __exit au1xmmc_exit(void)
1012 platform_driver_unregister(&au1xmmc_driver);
1015 module_init(au1xmmc_init);
1016 module_exit(au1xmmc_exit);
1018 #ifdef MODULE
1019 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1020 MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1021 MODULE_LICENSE("GPL");
1022 MODULE_ALIAS("platform:au1xxx-mmc");
1023 #endif