MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc / host / au1xmmc.c
blob34c99d4ea0415885e3bff95df2f6cc60a8aa564f
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>
44 #include <linux/mmc/host.h>
45 #include <asm/io.h>
46 #include <asm/mach-au1x00/au1000.h>
47 #include <asm/mach-au1x00/au1xxx_dbdma.h>
48 #include <asm/mach-au1x00/au1100_mmc.h>
49 #include <asm/scatterlist.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, unsigned int flags)
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 MMC_ERR_INVALID;
214 if (flags & MMC_DATA_READ) {
215 if (flags & MMC_DATA_MULTI)
216 mmccmd |= SD_CMD_CT_4;
217 else
218 mmccmd |= SD_CMD_CT_2;
219 } else if (flags & MMC_DATA_WRITE) {
220 if (flags & MMC_DATA_MULTI)
221 mmccmd |= SD_CMD_CT_3;
222 else
223 mmccmd |= SD_CMD_CT_1;
226 au_writel(cmd->arg, HOST_CMDARG(host));
227 au_sync();
229 if (wait)
230 IRQ_OFF(host, SD_CONFIG_CR);
232 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
233 au_sync();
235 /* Wait for the command to go on the line */
237 while(1) {
238 if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
239 break;
242 /* Wait for the command to come back */
244 if (wait) {
245 u32 status = au_readl(HOST_STATUS(host));
247 while(!(status & SD_STATUS_CR))
248 status = au_readl(HOST_STATUS(host));
250 /* Clear the CR status */
251 au_writel(SD_STATUS_CR, HOST_STATUS(host));
253 IRQ_ON(host, SD_CONFIG_CR);
256 return MMC_ERR_NONE;
259 static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
262 struct mmc_request *mrq = host->mrq;
263 struct mmc_data *data;
264 u32 crc;
266 WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP);
268 if (host->mrq == NULL)
269 return;
271 data = mrq->cmd->data;
273 if (status == 0)
274 status = au_readl(HOST_STATUS(host));
276 /* The transaction is really over when the SD_STATUS_DB bit is clear */
278 while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
279 status = au_readl(HOST_STATUS(host));
281 data->error = MMC_ERR_NONE;
282 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
284 /* Process any errors */
286 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
287 if (host->flags & HOST_F_XMIT)
288 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
290 if (crc)
291 data->error = MMC_ERR_BADCRC;
293 /* Clear the CRC bits */
294 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
296 data->bytes_xfered = 0;
298 if (data->error == MMC_ERR_NONE) {
299 if (host->flags & HOST_F_DMA) {
300 u32 chan = DMA_CHANNEL(host);
302 chan_tab_t *c = *((chan_tab_t **) chan);
303 au1x_dma_chan_t *cp = c->chan_ptr;
304 data->bytes_xfered = cp->ddma_bytecnt;
306 else
307 data->bytes_xfered =
308 (data->blocks * data->blksz) -
309 host->pio.len;
312 au1xmmc_finish_request(host);
315 static void au1xmmc_tasklet_data(unsigned long param)
317 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
319 u32 status = au_readl(HOST_STATUS(host));
320 au1xmmc_data_complete(host, status);
323 #define AU1XMMC_MAX_TRANSFER 8
325 static void au1xmmc_send_pio(struct au1xmmc_host *host)
328 struct mmc_data *data = 0;
329 int sg_len, max, count = 0;
330 unsigned char *sg_ptr;
331 u32 status = 0;
332 struct scatterlist *sg;
334 data = host->mrq->data;
336 if (!(host->flags & HOST_F_XMIT))
337 return;
339 /* This is the pointer to the data buffer */
340 sg = &data->sg[host->pio.index];
341 sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
343 /* This is the space left inside the buffer */
344 sg_len = data->sg[host->pio.index].length - host->pio.offset;
346 /* Check to if we need less then the size of the sg_buffer */
348 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
349 if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER;
351 for(count = 0; count < max; count++ ) {
352 unsigned char val;
354 status = au_readl(HOST_STATUS(host));
356 if (!(status & SD_STATUS_TH))
357 break;
359 val = *sg_ptr++;
361 au_writel((unsigned long) val, HOST_TXPORT(host));
362 au_sync();
365 host->pio.len -= count;
366 host->pio.offset += count;
368 if (count == sg_len) {
369 host->pio.index++;
370 host->pio.offset = 0;
373 if (host->pio.len == 0) {
374 IRQ_OFF(host, SD_CONFIG_TH);
376 if (host->flags & HOST_F_STOP)
377 SEND_STOP(host);
379 tasklet_schedule(&host->data_task);
383 static void au1xmmc_receive_pio(struct au1xmmc_host *host)
386 struct mmc_data *data = 0;
387 int sg_len = 0, max = 0, count = 0;
388 unsigned char *sg_ptr = 0;
389 u32 status = 0;
390 struct scatterlist *sg;
392 data = host->mrq->data;
394 if (!(host->flags & HOST_F_RECV))
395 return;
397 max = host->pio.len;
399 if (host->pio.index < host->dma.len) {
400 sg = &data->sg[host->pio.index];
401 sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
403 /* This is the space left inside the buffer */
404 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
406 /* Check to if we need less then the size of the sg_buffer */
407 if (sg_len < max) max = sg_len;
410 if (max > AU1XMMC_MAX_TRANSFER)
411 max = AU1XMMC_MAX_TRANSFER;
413 for(count = 0; count < max; count++ ) {
414 u32 val;
415 status = au_readl(HOST_STATUS(host));
417 if (!(status & SD_STATUS_NE))
418 break;
420 if (status & SD_STATUS_RC) {
421 DBG("RX CRC Error [%d + %d].\n", host->id,
422 host->pio.len, count);
423 break;
426 if (status & SD_STATUS_RO) {
427 DBG("RX Overrun [%d + %d]\n", host->id,
428 host->pio.len, count);
429 break;
431 else if (status & SD_STATUS_RU) {
432 DBG("RX Underrun [%d + %d]\n", host->id,
433 host->pio.len, count);
434 break;
437 val = au_readl(HOST_RXPORT(host));
439 if (sg_ptr)
440 *sg_ptr++ = (unsigned char) (val & 0xFF);
443 host->pio.len -= count;
444 host->pio.offset += count;
446 if (sg_len && count == sg_len) {
447 host->pio.index++;
448 host->pio.offset = 0;
451 if (host->pio.len == 0) {
452 //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF);
453 IRQ_OFF(host, SD_CONFIG_NE);
455 if (host->flags & HOST_F_STOP)
456 SEND_STOP(host);
458 tasklet_schedule(&host->data_task);
462 /* static void au1xmmc_cmd_complete
463 This is called when a command has been completed - grab the response
464 and check for errors. Then start the data transfer if it is indicated.
467 static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
470 struct mmc_request *mrq = host->mrq;
471 struct mmc_command *cmd;
472 int trans;
474 if (!host->mrq)
475 return;
477 cmd = mrq->cmd;
478 cmd->error = MMC_ERR_NONE;
480 if (cmd->flags & MMC_RSP_PRESENT) {
481 if (cmd->flags & MMC_RSP_136) {
482 u32 r[4];
483 int i;
485 r[0] = au_readl(host->iobase + SD_RESP3);
486 r[1] = au_readl(host->iobase + SD_RESP2);
487 r[2] = au_readl(host->iobase + SD_RESP1);
488 r[3] = au_readl(host->iobase + SD_RESP0);
490 /* The CRC is omitted from the response, so really
491 * we only got 120 bytes, but the engine expects
492 * 128 bits, so we have to shift things up
495 for(i = 0; i < 4; i++) {
496 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
497 if (i != 3)
498 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
500 } else {
501 /* Techincally, we should be getting all 48 bits of
502 * the response (SD_RESP1 + SD_RESP2), but because
503 * our response omits the CRC, our data ends up
504 * being shifted 8 bits to the right. In this case,
505 * that means that the OSR data starts at bit 31,
506 * so we can just read RESP0 and return that
508 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
512 /* Figure out errors */
514 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
515 cmd->error = MMC_ERR_BADCRC;
517 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
519 if (!trans || cmd->error != MMC_ERR_NONE) {
521 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
522 tasklet_schedule(&host->finish_task);
523 return;
526 host->status = HOST_S_DATA;
528 if (host->flags & HOST_F_DMA) {
529 u32 channel = DMA_CHANNEL(host);
531 /* Start the DMA as soon as the buffer gets something in it */
533 if (host->flags & HOST_F_RECV) {
534 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
536 while((status & mask) != mask)
537 status = au_readl(HOST_STATUS(host));
540 au1xxx_dbdma_start(channel);
544 static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
547 unsigned int pbus = get_au1x00_speed();
548 unsigned int divisor;
549 u32 config;
551 /* From databook:
552 divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
555 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
556 pbus /= 2;
558 divisor = ((pbus / rate) / 2) - 1;
560 config = au_readl(HOST_CONFIG(host));
562 config &= ~(SD_CONFIG_DIV);
563 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
565 au_writel(config, HOST_CONFIG(host));
566 au_sync();
569 static int
570 au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
573 int datalen = data->blocks * data->blksz;
575 if (dma != 0)
576 host->flags |= HOST_F_DMA;
578 if (data->flags & MMC_DATA_READ)
579 host->flags |= HOST_F_RECV;
580 else
581 host->flags |= HOST_F_XMIT;
583 if (host->mrq->stop)
584 host->flags |= HOST_F_STOP;
586 host->dma.dir = DMA_BIDIRECTIONAL;
588 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
589 data->sg_len, host->dma.dir);
591 if (host->dma.len == 0)
592 return MMC_ERR_TIMEOUT;
594 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
596 if (host->flags & HOST_F_DMA) {
597 int i;
598 u32 channel = DMA_CHANNEL(host);
600 au1xxx_dbdma_stop(channel);
602 for(i = 0; i < host->dma.len; i++) {
603 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
604 struct scatterlist *sg = &data->sg[i];
605 int sg_len = sg->length;
607 int len = (datalen > sg_len) ? sg_len : datalen;
609 if (i == host->dma.len - 1)
610 flags = DDMA_FLAGS_IE;
612 if (host->flags & HOST_F_XMIT){
613 ret = au1xxx_dbdma_put_source_flags(channel,
614 (void *) (page_address(sg->page) +
615 sg->offset),
616 len, flags);
618 else {
619 ret = au1xxx_dbdma_put_dest_flags(channel,
620 (void *) (page_address(sg->page) +
621 sg->offset),
622 len, flags);
625 if (!ret)
626 goto dataerr;
628 datalen -= len;
631 else {
632 host->pio.index = 0;
633 host->pio.offset = 0;
634 host->pio.len = datalen;
636 if (host->flags & HOST_F_XMIT)
637 IRQ_ON(host, SD_CONFIG_TH);
638 else
639 IRQ_ON(host, SD_CONFIG_NE);
640 //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
643 return MMC_ERR_NONE;
645 dataerr:
646 dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
647 return MMC_ERR_TIMEOUT;
650 /* static void au1xmmc_request
651 This actually starts a command or data transaction
654 static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
657 struct au1xmmc_host *host = mmc_priv(mmc);
658 unsigned int flags = 0;
659 int ret = MMC_ERR_NONE;
661 WARN_ON(irqs_disabled());
662 WARN_ON(host->status != HOST_S_IDLE);
664 host->mrq = mrq;
665 host->status = HOST_S_CMD;
667 bcsr->disk_leds &= ~(1 << 8);
669 if (mrq->data) {
670 FLUSH_FIFO(host);
671 flags = mrq->data->flags;
672 ret = au1xmmc_prepare_data(host, mrq->data);
675 if (ret == MMC_ERR_NONE)
676 ret = au1xmmc_send_command(host, 0, mrq->cmd, flags);
678 if (ret != MMC_ERR_NONE) {
679 mrq->cmd->error = ret;
680 au1xmmc_finish_request(host);
684 static void au1xmmc_reset_controller(struct au1xmmc_host *host)
687 /* Apply the clock */
688 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
689 au_sync_delay(1);
691 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
692 au_sync_delay(5);
694 au_writel(~0, HOST_STATUS(host));
695 au_sync();
697 au_writel(0, HOST_BLKSIZE(host));
698 au_writel(0x001fffff, HOST_TIMEOUT(host));
699 au_sync();
701 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
702 au_sync();
704 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
705 au_sync_delay(1);
707 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
708 au_sync();
710 /* Configure interrupts */
711 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
712 au_sync();
716 static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
718 struct au1xmmc_host *host = mmc_priv(mmc);
720 if (ios->power_mode == MMC_POWER_OFF)
721 au1xmmc_set_power(host, 0);
722 else if (ios->power_mode == MMC_POWER_ON) {
723 au1xmmc_set_power(host, 1);
726 if (ios->clock && ios->clock != host->clock) {
727 au1xmmc_set_clock(host, ios->clock);
728 host->clock = ios->clock;
732 static void au1xmmc_dma_callback(int irq, void *dev_id)
734 struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
736 /* Avoid spurious interrupts */
738 if (!host->mrq)
739 return;
741 if (host->flags & HOST_F_STOP)
742 SEND_STOP(host);
744 tasklet_schedule(&host->data_task);
747 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
748 #define STATUS_DATA_IN (SD_STATUS_NE)
749 #define STATUS_DATA_OUT (SD_STATUS_TH)
751 static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
754 u32 status;
755 int i, ret = 0;
757 disable_irq(AU1100_SD_IRQ);
759 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
760 struct au1xmmc_host * host = au1xmmc_hosts[i];
761 u32 handled = 1;
763 status = au_readl(HOST_STATUS(host));
765 if (host->mrq && (status & STATUS_TIMEOUT)) {
766 if (status & SD_STATUS_RAT)
767 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
769 else if (status & SD_STATUS_DT)
770 host->mrq->data->error = MMC_ERR_TIMEOUT;
772 /* In PIO mode, interrupts might still be enabled */
773 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
775 //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
776 tasklet_schedule(&host->finish_task);
778 #if 0
779 else if (status & SD_STATUS_DD) {
781 /* Sometimes we get a DD before a NE in PIO mode */
783 if (!(host->flags & HOST_F_DMA) &&
784 (status & SD_STATUS_NE))
785 au1xmmc_receive_pio(host);
786 else {
787 au1xmmc_data_complete(host, status);
788 //tasklet_schedule(&host->data_task);
791 #endif
792 else if (status & (SD_STATUS_CR)) {
793 if (host->status == HOST_S_CMD)
794 au1xmmc_cmd_complete(host,status);
796 else if (!(host->flags & HOST_F_DMA)) {
797 if ((host->flags & HOST_F_XMIT) &&
798 (status & STATUS_DATA_OUT))
799 au1xmmc_send_pio(host);
800 else if ((host->flags & HOST_F_RECV) &&
801 (status & STATUS_DATA_IN))
802 au1xmmc_receive_pio(host);
804 else if (status & 0x203FBC70) {
805 DBG("Unhandled status %8.8x\n", host->id, status);
806 handled = 0;
809 au_writel(status, HOST_STATUS(host));
810 au_sync();
812 ret |= handled;
815 enable_irq(AU1100_SD_IRQ);
816 return ret;
819 static void au1xmmc_poll_event(unsigned long arg)
821 struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
823 int card = au1xmmc_card_inserted(host);
824 int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
826 if (card != controller) {
827 host->flags &= ~HOST_F_ACTIVE;
828 if (card) host->flags |= HOST_F_ACTIVE;
829 mmc_detect_change(host->mmc, 0);
832 if (host->mrq != NULL) {
833 u32 status = au_readl(HOST_STATUS(host));
834 DBG("PENDING - %8.8x\n", host->id, status);
837 mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
840 static dbdev_tab_t au1xmmc_mem_dbdev =
842 DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
845 static void au1xmmc_init_dma(struct au1xmmc_host *host)
848 u32 rxchan, txchan;
850 int txid = au1xmmc_card_table[host->id].tx_devid;
851 int rxid = au1xmmc_card_table[host->id].rx_devid;
853 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
854 of 8 bits. And since devices are shared, we need to create
855 our own to avoid freaking out other devices
858 int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
860 txchan = au1xxx_dbdma_chan_alloc(memid, txid,
861 au1xmmc_dma_callback, (void *) host);
863 rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
864 au1xmmc_dma_callback, (void *) host);
866 au1xxx_dbdma_set_devwidth(txchan, 8);
867 au1xxx_dbdma_set_devwidth(rxchan, 8);
869 au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
870 au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
872 host->tx_chan = txchan;
873 host->rx_chan = rxchan;
876 static const struct mmc_host_ops au1xmmc_ops = {
877 .request = au1xmmc_request,
878 .set_ios = au1xmmc_set_ios,
879 .get_ro = au1xmmc_card_readonly,
882 static int __devinit au1xmmc_probe(struct platform_device *pdev)
885 int i, ret = 0;
887 /* THe interrupt is shared among all controllers */
888 ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, IRQF_DISABLED, "MMC", 0);
890 if (ret) {
891 printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
892 AU1100_SD_IRQ, ret);
893 return -ENXIO;
896 disable_irq(AU1100_SD_IRQ);
898 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
899 struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
900 struct au1xmmc_host *host = 0;
902 if (!mmc) {
903 printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
904 au1xmmc_hosts[i] = 0;
905 continue;
908 mmc->ops = &au1xmmc_ops;
910 mmc->f_min = 450000;
911 mmc->f_max = 24000000;
913 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
914 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
916 mmc->max_blk_size = 2048;
917 mmc->max_blk_count = 512;
919 mmc->ocr_avail = AU1XMMC_OCR;
921 host = mmc_priv(mmc);
922 host->mmc = mmc;
924 host->id = i;
925 host->iobase = au1xmmc_card_table[host->id].iobase;
926 host->clock = 0;
927 host->power_mode = MMC_POWER_OFF;
929 host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
930 host->status = HOST_S_IDLE;
932 init_timer(&host->timer);
934 host->timer.function = au1xmmc_poll_event;
935 host->timer.data = (unsigned long) host;
936 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
938 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
939 (unsigned long) host);
941 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
942 (unsigned long) host);
944 spin_lock_init(&host->lock);
946 if (dma != 0)
947 au1xmmc_init_dma(host);
949 au1xmmc_reset_controller(host);
951 mmc_add_host(mmc);
952 au1xmmc_hosts[i] = host;
954 add_timer(&host->timer);
956 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
957 host->id, host->iobase, dma ? "dma" : "pio");
960 enable_irq(AU1100_SD_IRQ);
962 return 0;
965 static int __devexit au1xmmc_remove(struct platform_device *pdev)
968 int i;
970 disable_irq(AU1100_SD_IRQ);
972 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
973 struct au1xmmc_host *host = au1xmmc_hosts[i];
974 if (!host) continue;
976 tasklet_kill(&host->data_task);
977 tasklet_kill(&host->finish_task);
979 del_timer_sync(&host->timer);
980 au1xmmc_set_power(host, 0);
982 mmc_remove_host(host->mmc);
984 au1xxx_dbdma_chan_free(host->tx_chan);
985 au1xxx_dbdma_chan_free(host->rx_chan);
987 au_writel(0x0, HOST_ENABLE(host));
988 au_sync();
991 free_irq(AU1100_SD_IRQ, 0);
992 return 0;
995 static struct platform_driver au1xmmc_driver = {
996 .probe = au1xmmc_probe,
997 .remove = au1xmmc_remove,
998 .suspend = NULL,
999 .resume = NULL,
1000 .driver = {
1001 .name = DRIVER_NAME,
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 #endif