2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2006 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
14 * Changes to the FIFO system should be done with extreme care since
15 * the hardware is full of bugs related to the FIFO. Known issues are:
17 * - FIFO size field in FSR is always zero.
19 * - FIFO interrupts tend not to work as they should. Interrupts are
20 * triggered only for full/empty events, not for threshold values.
22 * - On APIC systems the FIFO empty interrupt is sometimes lost.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/delay.h>
33 #include <linux/pnp.h>
34 #include <linux/highmem.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
40 #include <asm/scatterlist.h>
44 #define DRIVER_NAME "wbsd"
45 #define DRIVER_VERSION "1.6"
48 pr_debug(DRIVER_NAME ": " x)
49 #define DBGF(f, x...) \
50 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
58 static const struct pnp_device_id pnp_dev_table[] = {
64 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
66 #endif /* CONFIG_PNP */
68 static const int config_ports[] = { 0x2E, 0x4E };
69 static const int unlock_codes[] = { 0x83, 0x87 };
71 static const int valid_ids[] = {
76 static unsigned int nopnp = 0;
78 static const unsigned int nopnp = 1;
80 static unsigned int io = 0x248;
81 static unsigned int irq = 6;
88 static inline void wbsd_unlock_config(struct wbsd_host *host)
90 BUG_ON(host->config == 0);
92 outb(host->unlock_code, host->config);
93 outb(host->unlock_code, host->config);
96 static inline void wbsd_lock_config(struct wbsd_host *host)
98 BUG_ON(host->config == 0);
100 outb(LOCK_CODE, host->config);
103 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
105 BUG_ON(host->config == 0);
107 outb(reg, host->config);
108 outb(value, host->config + 1);
111 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
113 BUG_ON(host->config == 0);
115 outb(reg, host->config);
116 return inb(host->config + 1);
119 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
121 outb(index, host->base + WBSD_IDXR);
122 outb(value, host->base + WBSD_DATAR);
125 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
127 outb(index, host->base + WBSD_IDXR);
128 return inb(host->base + WBSD_DATAR);
135 static void wbsd_init_device(struct wbsd_host *host)
140 * Reset chip (SD/MMC part) and fifo.
142 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
143 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
144 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
149 setup &= ~WBSD_DAT3_H;
150 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
151 host->flags &= ~WBSD_FIGNORE_DETECT;
154 * Read back default clock.
156 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
161 outb(WBSD_POWER_N, host->base + WBSD_CSR);
164 * Set maximum timeout.
166 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
169 * Test for card presence
171 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
172 host->flags |= WBSD_FCARD_PRESENT;
174 host->flags &= ~WBSD_FCARD_PRESENT;
177 * Enable interesting interrupts.
180 ier |= WBSD_EINT_CARD;
181 ier |= WBSD_EINT_FIFO_THRE;
182 ier |= WBSD_EINT_CCRC;
183 ier |= WBSD_EINT_TIMEOUT;
184 ier |= WBSD_EINT_CRC;
187 outb(ier, host->base + WBSD_EIR);
192 inb(host->base + WBSD_ISR);
195 static void wbsd_reset(struct wbsd_host *host)
199 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
202 * Soft reset of chip (SD/MMC part).
204 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
205 setup |= WBSD_SOFT_RESET;
206 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
209 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
211 unsigned long dmaflags;
213 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
215 if (host->dma >= 0) {
217 * Release ISA DMA controller.
219 dmaflags = claim_dma_lock();
220 disable_dma(host->dma);
221 clear_dma_ff(host->dma);
222 release_dma_lock(dmaflags);
225 * Disable DMA on host.
227 wbsd_write_index(host, WBSD_IDX_DMA, 0);
233 * MMC layer might call back into the driver so first unlock.
235 spin_unlock(&host->lock);
236 mmc_request_done(host->mmc, mrq);
237 spin_lock(&host->lock);
241 * Scatter/gather functions
244 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
247 * Get info. about SG list from data structure.
249 host->cur_sg = data->sg;
250 host->num_sg = data->sg_len;
253 host->remain = host->cur_sg->length;
256 static inline int wbsd_next_sg(struct wbsd_host *host)
259 * Skip to next SG entry.
267 if (host->num_sg > 0) {
269 host->remain = host->cur_sg->length;
275 static inline char *wbsd_kmap_sg(struct wbsd_host *host)
277 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
278 host->cur_sg->offset;
279 return host->mapped_sg;
282 static inline void wbsd_kunmap_sg(struct wbsd_host *host)
284 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
287 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
289 unsigned int len, i, size;
290 struct scatterlist *sg;
291 char *dmabuf = host->dma_buffer;
300 * Just loop through all entries. Size might not
301 * be the entire list though so make sure that
302 * we do not transfer too much.
304 for (i = 0; i < len; i++) {
305 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
306 if (size < sg[i].length)
307 memcpy(dmabuf, sgbuf, size);
309 memcpy(dmabuf, sgbuf, sg[i].length);
310 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
311 dmabuf += sg[i].length;
313 if (size < sg[i].length)
316 size -= sg[i].length;
323 * Check that we didn't get a request to transfer
324 * more data than can fit into the SG list.
332 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
334 unsigned int len, i, size;
335 struct scatterlist *sg;
336 char *dmabuf = host->dma_buffer;
345 * Just loop through all entries. Size might not
346 * be the entire list though so make sure that
347 * we do not transfer too much.
349 for (i = 0; i < len; i++) {
350 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
351 if (size < sg[i].length)
352 memcpy(sgbuf, dmabuf, size);
354 memcpy(sgbuf, dmabuf, sg[i].length);
355 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
356 dmabuf += sg[i].length;
358 if (size < sg[i].length)
361 size -= sg[i].length;
368 * Check that we didn't get a request to transfer
369 * more data than can fit into the SG list.
381 static inline void wbsd_get_short_reply(struct wbsd_host *host,
382 struct mmc_command *cmd)
385 * Correct response type?
387 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
388 cmd->error = MMC_ERR_INVALID;
392 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
393 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
394 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
395 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
396 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
399 static inline void wbsd_get_long_reply(struct wbsd_host *host,
400 struct mmc_command *cmd)
405 * Correct response type?
407 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
408 cmd->error = MMC_ERR_INVALID;
412 for (i = 0; i < 4; i++) {
414 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
416 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
418 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
420 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
424 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
429 DBGF("Sending cmd (%x)\n", cmd->opcode);
432 * Clear accumulated ISR. The interrupt routine
433 * will fill this one with events that occur during
439 * Send the command (CRC calculated by host).
441 outb(cmd->opcode, host->base + WBSD_CMDR);
442 for (i = 3; i >= 0; i--)
443 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
445 cmd->error = MMC_ERR_NONE;
448 * Wait for the request to complete.
451 status = wbsd_read_index(host, WBSD_IDX_STATUS);
452 } while (status & WBSD_CARDTRAFFIC);
455 * Do we expect a reply?
457 if (cmd->flags & MMC_RSP_PRESENT) {
464 if (isr & WBSD_INT_CARD)
465 cmd->error = MMC_ERR_TIMEOUT;
467 else if (isr & WBSD_INT_TIMEOUT)
468 cmd->error = MMC_ERR_TIMEOUT;
470 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
471 cmd->error = MMC_ERR_BADCRC;
474 if (cmd->flags & MMC_RSP_136)
475 wbsd_get_long_reply(host, cmd);
477 wbsd_get_short_reply(host, cmd);
481 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
488 static void wbsd_empty_fifo(struct wbsd_host *host)
490 struct mmc_data *data = host->mrq->cmd->data;
495 * Handle excessive data.
497 if (data->bytes_xfered == host->size)
500 buffer = wbsd_kmap_sg(host) + host->offset;
503 * Drain the fifo. This has a tendency to loop longer
504 * than the FIFO length (usually one block).
506 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
508 * The size field in the FSR is broken so we have to
511 if (fsr & WBSD_FIFO_FULL)
513 else if (fsr & WBSD_FIFO_FUTHRE)
518 for (i = 0; i < fifo; i++) {
519 *buffer = inb(host->base + WBSD_DFR);
524 data->bytes_xfered++;
529 if (data->bytes_xfered == host->size) {
530 wbsd_kunmap_sg(host);
535 * End of scatter list entry?
537 if (host->remain == 0) {
538 wbsd_kunmap_sg(host);
541 * Get next entry. Check if last.
543 if (!wbsd_next_sg(host)) {
545 * We should never reach this point.
546 * It means that we're trying to
547 * transfer more blocks than can fit
548 * into the scatter list.
552 host->size = data->bytes_xfered;
557 buffer = wbsd_kmap_sg(host);
562 wbsd_kunmap_sg(host);
565 * This is a very dirty hack to solve a
566 * hardware problem. The chip doesn't trigger
567 * FIFO threshold interrupts properly.
569 if ((host->size - data->bytes_xfered) < 16)
570 tasklet_schedule(&host->fifo_tasklet);
573 static void wbsd_fill_fifo(struct wbsd_host *host)
575 struct mmc_data *data = host->mrq->cmd->data;
580 * Check that we aren't being called after the
581 * entire buffer has been transfered.
583 if (data->bytes_xfered == host->size)
586 buffer = wbsd_kmap_sg(host) + host->offset;
589 * Fill the fifo. This has a tendency to loop longer
590 * than the FIFO length (usually one block).
592 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
594 * The size field in the FSR is broken so we have to
597 if (fsr & WBSD_FIFO_EMPTY)
599 else if (fsr & WBSD_FIFO_EMTHRE)
604 for (i = 16; i > fifo; i--) {
605 outb(*buffer, host->base + WBSD_DFR);
610 data->bytes_xfered++;
615 if (data->bytes_xfered == host->size) {
616 wbsd_kunmap_sg(host);
621 * End of scatter list entry?
623 if (host->remain == 0) {
624 wbsd_kunmap_sg(host);
627 * Get next entry. Check if last.
629 if (!wbsd_next_sg(host)) {
631 * We should never reach this point.
632 * It means that we're trying to
633 * transfer more blocks than can fit
634 * into the scatter list.
638 host->size = data->bytes_xfered;
643 buffer = wbsd_kmap_sg(host);
648 wbsd_kunmap_sg(host);
651 * The controller stops sending interrupts for
652 * 'FIFO empty' under certain conditions. So we
653 * need to be a bit more pro-active.
655 tasklet_schedule(&host->fifo_tasklet);
658 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
662 unsigned long dmaflags;
664 DBGF("blksz %04x blks %04x flags %08x\n",
665 data->blksz, data->blocks, data->flags);
666 DBGF("tsac %d ms nsac %d clk\n",
667 data->timeout_ns / 1000000, data->timeout_clks);
672 host->size = data->blocks * data->blksz;
675 * Check timeout values for overflow.
676 * (Yes, some cards cause this value to overflow).
678 if (data->timeout_ns > 127000000)
679 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
681 wbsd_write_index(host, WBSD_IDX_TAAC,
682 data->timeout_ns / 1000000);
685 if (data->timeout_clks > 255)
686 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
688 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
691 * Inform the chip of how large blocks will be
692 * sent. It needs this to determine when to
695 * Space for CRC must be included in the size.
696 * Two bytes are needed for each data line.
698 if (host->bus_width == MMC_BUS_WIDTH_1) {
699 blksize = data->blksz + 2;
701 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
702 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
703 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
704 blksize = data->blksz + 2 * 4;
706 wbsd_write_index(host, WBSD_IDX_PBSMSB,
707 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
708 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
710 data->error = MMC_ERR_INVALID;
715 * Clear the FIFO. This is needed even for DMA
716 * transfers since the chip still uses the FIFO
719 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
720 setup |= WBSD_FIFO_RESET;
721 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
726 if (host->dma >= 0) {
728 * The buffer for DMA is only 64 kB.
730 BUG_ON(host->size > 0x10000);
731 if (host->size > 0x10000) {
732 data->error = MMC_ERR_INVALID;
737 * Transfer data from the SG list to
740 if (data->flags & MMC_DATA_WRITE)
741 wbsd_sg_to_dma(host, data);
744 * Initialise the ISA DMA controller.
746 dmaflags = claim_dma_lock();
747 disable_dma(host->dma);
748 clear_dma_ff(host->dma);
749 if (data->flags & MMC_DATA_READ)
750 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
752 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
753 set_dma_addr(host->dma, host->dma_addr);
754 set_dma_count(host->dma, host->size);
756 enable_dma(host->dma);
757 release_dma_lock(dmaflags);
760 * Enable DMA on the host.
762 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
765 * This flag is used to keep printk
766 * output to a minimum.
771 * Initialise the SG list.
773 wbsd_init_sg(host, data);
778 wbsd_write_index(host, WBSD_IDX_DMA, 0);
781 * Set up FIFO threshold levels (and fill
782 * buffer if doing a write).
784 if (data->flags & MMC_DATA_READ) {
785 wbsd_write_index(host, WBSD_IDX_FIFOEN,
786 WBSD_FIFOEN_FULL | 8);
788 wbsd_write_index(host, WBSD_IDX_FIFOEN,
789 WBSD_FIFOEN_EMPTY | 8);
790 wbsd_fill_fifo(host);
794 data->error = MMC_ERR_NONE;
797 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
799 unsigned long dmaflags;
803 WARN_ON(host->mrq == NULL);
806 * Send a stop command if needed.
809 wbsd_send_command(host, data->stop);
812 * Wait for the controller to leave data
816 status = wbsd_read_index(host, WBSD_IDX_STATUS);
817 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
822 if (host->dma >= 0) {
824 * Disable DMA on the host.
826 wbsd_write_index(host, WBSD_IDX_DMA, 0);
829 * Turn of ISA DMA controller.
831 dmaflags = claim_dma_lock();
832 disable_dma(host->dma);
833 clear_dma_ff(host->dma);
834 count = get_dma_residue(host->dma);
835 release_dma_lock(dmaflags);
841 printk(KERN_ERR "%s: Incomplete DMA transfer. "
843 mmc_hostname(host->mmc), count);
845 data->error = MMC_ERR_FAILED;
848 * Transfer data from DMA buffer to
851 if (data->flags & MMC_DATA_READ)
852 wbsd_dma_to_sg(host, data);
854 data->bytes_xfered = host->size;
858 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
860 wbsd_request_end(host, host->mrq);
863 /*****************************************************************************\
865 * MMC layer callbacks *
867 \*****************************************************************************/
869 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
871 struct wbsd_host *host = mmc_priv(mmc);
872 struct mmc_command *cmd;
875 * Disable tasklets to avoid a deadlock.
877 spin_lock_bh(&host->lock);
879 BUG_ON(host->mrq != NULL);
886 * If there is no card in the slot then
887 * timeout immediatly.
889 if (!(host->flags & WBSD_FCARD_PRESENT)) {
890 cmd->error = MMC_ERR_TIMEOUT;
895 * Does the request include data?
898 wbsd_prepare_data(host, cmd->data);
900 if (cmd->data->error != MMC_ERR_NONE)
904 wbsd_send_command(host, cmd);
907 * If this is a data transfer the request
908 * will be finished after the data has
911 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
913 * The hardware is so delightfully stupid that it has a list
914 * of "data" commands. If a command isn't on this list, it'll
915 * just go back to the idle state and won't send any data
918 switch (cmd->opcode) {
932 /* ACMDs. We don't keep track of state, so we just treat them
933 * like any other command. */
938 #ifdef CONFIG_MMC_DEBUG
939 printk(KERN_WARNING "%s: Data command %d is not "
940 "supported by this controller.\n",
941 mmc_hostname(host->mmc), cmd->opcode);
943 cmd->data->error = MMC_ERR_INVALID;
946 wbsd_send_command(host, cmd->data->stop);
952 * Dirty fix for hardware bug.
955 tasklet_schedule(&host->fifo_tasklet);
957 spin_unlock_bh(&host->lock);
963 wbsd_request_end(host, mrq);
965 spin_unlock_bh(&host->lock);
968 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
970 struct wbsd_host *host = mmc_priv(mmc);
973 spin_lock_bh(&host->lock);
976 * Reset the chip on each power off.
977 * Should clear out any weird states.
979 if (ios->power_mode == MMC_POWER_OFF)
980 wbsd_init_device(host);
982 if (ios->clock >= 24000000)
984 else if (ios->clock >= 16000000)
986 else if (ios->clock >= 12000000)
992 * Only write to the clock register when
993 * there is an actual change.
995 if (clk != host->clk) {
996 wbsd_write_index(host, WBSD_IDX_CLK, clk);
1003 if (ios->power_mode != MMC_POWER_OFF) {
1004 pwr = inb(host->base + WBSD_CSR);
1005 pwr &= ~WBSD_POWER_N;
1006 outb(pwr, host->base + WBSD_CSR);
1010 * MMC cards need to have pin 1 high during init.
1011 * It wreaks havoc with the card detection though so
1012 * that needs to be disabled.
1014 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
1015 if (ios->chip_select == MMC_CS_HIGH) {
1016 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
1017 setup |= WBSD_DAT3_H;
1018 host->flags |= WBSD_FIGNORE_DETECT;
1020 if (setup & WBSD_DAT3_H) {
1021 setup &= ~WBSD_DAT3_H;
1024 * We cannot resume card detection immediatly
1025 * because of capacitance and delays in the chip.
1027 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1030 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1033 * Store bus width for later. Will be used when
1034 * setting up the data transfer.
1036 host->bus_width = ios->bus_width;
1038 spin_unlock_bh(&host->lock);
1041 static int wbsd_get_ro(struct mmc_host *mmc)
1043 struct wbsd_host *host = mmc_priv(mmc);
1046 spin_lock_bh(&host->lock);
1048 csr = inb(host->base + WBSD_CSR);
1050 outb(csr, host->base + WBSD_CSR);
1054 csr = inb(host->base + WBSD_CSR);
1056 outb(csr, host->base + WBSD_CSR);
1058 spin_unlock_bh(&host->lock);
1060 return csr & WBSD_WRPT;
1063 static const struct mmc_host_ops wbsd_ops = {
1064 .request = wbsd_request,
1065 .set_ios = wbsd_set_ios,
1066 .get_ro = wbsd_get_ro,
1069 /*****************************************************************************\
1071 * Interrupt handling *
1073 \*****************************************************************************/
1076 * Helper function to reset detection ignore
1079 static void wbsd_reset_ignore(unsigned long data)
1081 struct wbsd_host *host = (struct wbsd_host *)data;
1083 BUG_ON(host == NULL);
1085 DBG("Resetting card detection ignore\n");
1087 spin_lock_bh(&host->lock);
1089 host->flags &= ~WBSD_FIGNORE_DETECT;
1092 * Card status might have changed during the
1095 tasklet_schedule(&host->card_tasklet);
1097 spin_unlock_bh(&host->lock);
1104 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1106 WARN_ON(!host->mrq);
1110 WARN_ON(!host->mrq->cmd);
1111 if (!host->mrq->cmd)
1114 WARN_ON(!host->mrq->cmd->data);
1115 if (!host->mrq->cmd->data)
1118 return host->mrq->cmd->data;
1121 static void wbsd_tasklet_card(unsigned long param)
1123 struct wbsd_host *host = (struct wbsd_host *)param;
1127 spin_lock(&host->lock);
1129 if (host->flags & WBSD_FIGNORE_DETECT) {
1130 spin_unlock(&host->lock);
1134 csr = inb(host->base + WBSD_CSR);
1135 WARN_ON(csr == 0xff);
1137 if (csr & WBSD_CARDPRESENT) {
1138 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1139 DBG("Card inserted\n");
1140 host->flags |= WBSD_FCARD_PRESENT;
1144 } else if (host->flags & WBSD_FCARD_PRESENT) {
1145 DBG("Card removed\n");
1146 host->flags &= ~WBSD_FCARD_PRESENT;
1149 printk(KERN_ERR "%s: Card removed during transfer!\n",
1150 mmc_hostname(host->mmc));
1153 host->mrq->cmd->error = MMC_ERR_FAILED;
1154 tasklet_schedule(&host->finish_tasklet);
1161 * Unlock first since we might get a call back.
1164 spin_unlock(&host->lock);
1167 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1170 static void wbsd_tasklet_fifo(unsigned long param)
1172 struct wbsd_host *host = (struct wbsd_host *)param;
1173 struct mmc_data *data;
1175 spin_lock(&host->lock);
1180 data = wbsd_get_data(host);
1184 if (data->flags & MMC_DATA_WRITE)
1185 wbsd_fill_fifo(host);
1187 wbsd_empty_fifo(host);
1192 if (host->size == data->bytes_xfered) {
1193 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1194 tasklet_schedule(&host->finish_tasklet);
1198 spin_unlock(&host->lock);
1201 static void wbsd_tasklet_crc(unsigned long param)
1203 struct wbsd_host *host = (struct wbsd_host *)param;
1204 struct mmc_data *data;
1206 spin_lock(&host->lock);
1211 data = wbsd_get_data(host);
1215 DBGF("CRC error\n");
1217 data->error = MMC_ERR_BADCRC;
1219 tasklet_schedule(&host->finish_tasklet);
1222 spin_unlock(&host->lock);
1225 static void wbsd_tasklet_timeout(unsigned long param)
1227 struct wbsd_host *host = (struct wbsd_host *)param;
1228 struct mmc_data *data;
1230 spin_lock(&host->lock);
1235 data = wbsd_get_data(host);
1241 data->error = MMC_ERR_TIMEOUT;
1243 tasklet_schedule(&host->finish_tasklet);
1246 spin_unlock(&host->lock);
1249 static void wbsd_tasklet_finish(unsigned long param)
1251 struct wbsd_host *host = (struct wbsd_host *)param;
1252 struct mmc_data *data;
1254 spin_lock(&host->lock);
1256 WARN_ON(!host->mrq);
1260 data = wbsd_get_data(host);
1264 wbsd_finish_data(host, data);
1267 spin_unlock(&host->lock);
1270 static void wbsd_tasklet_block(unsigned long param)
1272 struct wbsd_host *host = (struct wbsd_host *)param;
1273 struct mmc_data *data;
1275 spin_lock(&host->lock);
1277 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1279 data = wbsd_get_data(host);
1283 DBGF("CRC error\n");
1285 data->error = MMC_ERR_BADCRC;
1287 tasklet_schedule(&host->finish_tasklet);
1291 spin_unlock(&host->lock);
1295 * Interrupt handling
1298 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1300 struct wbsd_host *host = dev_id;
1303 isr = inb(host->base + WBSD_ISR);
1306 * Was it actually our hardware that caused the interrupt?
1308 if (isr == 0xff || isr == 0x00)
1314 * Schedule tasklets as needed.
1316 if (isr & WBSD_INT_CARD)
1317 tasklet_schedule(&host->card_tasklet);
1318 if (isr & WBSD_INT_FIFO_THRE)
1319 tasklet_schedule(&host->fifo_tasklet);
1320 if (isr & WBSD_INT_CRC)
1321 tasklet_hi_schedule(&host->crc_tasklet);
1322 if (isr & WBSD_INT_TIMEOUT)
1323 tasklet_hi_schedule(&host->timeout_tasklet);
1324 if (isr & WBSD_INT_BUSYEND)
1325 tasklet_hi_schedule(&host->block_tasklet);
1326 if (isr & WBSD_INT_TC)
1327 tasklet_schedule(&host->finish_tasklet);
1332 /*****************************************************************************\
1334 * Device initialisation and shutdown *
1336 \*****************************************************************************/
1339 * Allocate/free MMC structure.
1342 static int __devinit wbsd_alloc_mmc(struct device *dev)
1344 struct mmc_host *mmc;
1345 struct wbsd_host *host;
1348 * Allocate MMC structure.
1350 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1354 host = mmc_priv(mmc);
1360 * Set host parameters.
1362 mmc->ops = &wbsd_ops;
1363 mmc->f_min = 375000;
1364 mmc->f_max = 24000000;
1365 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1366 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1368 spin_lock_init(&host->lock);
1373 init_timer(&host->ignore_timer);
1374 host->ignore_timer.data = (unsigned long)host;
1375 host->ignore_timer.function = wbsd_reset_ignore;
1378 * Maximum number of segments. Worst case is one sector per segment
1379 * so this will be 64kB/512.
1381 mmc->max_hw_segs = 128;
1382 mmc->max_phys_segs = 128;
1385 * Maximum request size. Also limited by 64KiB buffer.
1387 mmc->max_req_size = 65536;
1390 * Maximum segment size. Could be one segment with the maximum number
1393 mmc->max_seg_size = mmc->max_req_size;
1396 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1397 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1399 mmc->max_blk_size = 4087;
1402 * Maximum block count. There is no real limit so the maximum
1403 * request size will be the only restriction.
1405 mmc->max_blk_count = mmc->max_req_size;
1407 dev_set_drvdata(dev, mmc);
1412 static void __devexit wbsd_free_mmc(struct device *dev)
1414 struct mmc_host *mmc;
1415 struct wbsd_host *host;
1417 mmc = dev_get_drvdata(dev);
1421 host = mmc_priv(mmc);
1422 BUG_ON(host == NULL);
1424 del_timer_sync(&host->ignore_timer);
1428 dev_set_drvdata(dev, NULL);
1432 * Scan for known chip id:s
1435 static int __devinit wbsd_scan(struct wbsd_host *host)
1441 * Iterate through all ports, all codes to
1442 * find hardware that is in our known list.
1444 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1445 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1448 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1451 host->config = config_ports[i];
1452 host->unlock_code = unlock_codes[j];
1454 wbsd_unlock_config(host);
1456 outb(WBSD_CONF_ID_HI, config_ports[i]);
1457 id = inb(config_ports[i] + 1) << 8;
1459 outb(WBSD_CONF_ID_LO, config_ports[i]);
1460 id |= inb(config_ports[i] + 1);
1462 wbsd_lock_config(host);
1464 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1465 if (id == valid_ids[k]) {
1473 DBG("Unknown hardware (id %x) found at %x\n",
1474 id, config_ports[i]);
1478 release_region(config_ports[i], 2);
1482 host->unlock_code = 0;
1488 * Allocate/free io port ranges
1491 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1496 if (!request_region(base, 8, DRIVER_NAME))
1504 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1507 release_region(host->base, 8);
1512 release_region(host->config, 2);
1518 * Allocate/free DMA port and buffer
1521 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1526 if (request_dma(dma, DRIVER_NAME))
1530 * We need to allocate a special buffer in
1531 * order for ISA to be able to DMA to it.
1533 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1534 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1535 if (!host->dma_buffer)
1539 * Translate the address to a physical address.
1541 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1542 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1545 * ISA DMA must be aligned on a 64k basis.
1547 if ((host->dma_addr & 0xffff) != 0)
1550 * ISA cannot access memory above 16 MB.
1552 else if (host->dma_addr >= 0x1000000)
1561 * If we've gotten here then there is some kind of alignment bug
1565 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1566 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1567 host->dma_addr = (dma_addr_t)NULL;
1569 kfree(host->dma_buffer);
1570 host->dma_buffer = NULL;
1576 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1577 "Falling back on FIFO.\n", dma);
1580 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1582 if (host->dma_addr) {
1583 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1584 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1586 kfree(host->dma_buffer);
1588 free_dma(host->dma);
1591 host->dma_buffer = NULL;
1592 host->dma_addr = (dma_addr_t)NULL;
1596 * Allocate/free IRQ.
1599 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1604 * Allocate interrupt.
1607 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1616 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1617 (unsigned long)host);
1618 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1619 (unsigned long)host);
1620 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1621 (unsigned long)host);
1622 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1623 (unsigned long)host);
1624 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1625 (unsigned long)host);
1626 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1627 (unsigned long)host);
1632 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1637 free_irq(host->irq, host);
1641 tasklet_kill(&host->card_tasklet);
1642 tasklet_kill(&host->fifo_tasklet);
1643 tasklet_kill(&host->crc_tasklet);
1644 tasklet_kill(&host->timeout_tasklet);
1645 tasklet_kill(&host->finish_tasklet);
1646 tasklet_kill(&host->block_tasklet);
1650 * Allocate all resources for the host.
1653 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1654 int base, int irq, int dma)
1659 * Allocate I/O ports.
1661 ret = wbsd_request_region(host, base);
1666 * Allocate interrupt.
1668 ret = wbsd_request_irq(host, irq);
1675 wbsd_request_dma(host, dma);
1681 * Release all resources for the host.
1684 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1686 wbsd_release_dma(host);
1687 wbsd_release_irq(host);
1688 wbsd_release_regions(host);
1692 * Configure the resources the chip should use.
1695 static void wbsd_chip_config(struct wbsd_host *host)
1697 wbsd_unlock_config(host);
1702 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1703 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1706 * Select SD/MMC function.
1708 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1711 * Set up card detection.
1713 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1718 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1719 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1721 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1724 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1727 * Enable and power up chip.
1729 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1730 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1732 wbsd_lock_config(host);
1736 * Check that configured resources are correct.
1739 static int wbsd_chip_validate(struct wbsd_host *host)
1743 wbsd_unlock_config(host);
1746 * Select SD/MMC function.
1748 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1751 * Read configuration.
1753 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1754 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1756 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1758 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1760 wbsd_lock_config(host);
1763 * Validate against given configuration.
1765 if (base != host->base)
1767 if (irq != host->irq)
1769 if ((dma != host->dma) && (host->dma != -1))
1776 * Powers down the SD function
1779 static void wbsd_chip_poweroff(struct wbsd_host *host)
1781 wbsd_unlock_config(host);
1783 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1784 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1786 wbsd_lock_config(host);
1789 /*****************************************************************************\
1791 * Devices setup and shutdown *
1793 \*****************************************************************************/
1795 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1798 struct wbsd_host *host = NULL;
1799 struct mmc_host *mmc = NULL;
1802 ret = wbsd_alloc_mmc(dev);
1806 mmc = dev_get_drvdata(dev);
1807 host = mmc_priv(mmc);
1810 * Scan for hardware.
1812 ret = wbsd_scan(host);
1814 if (pnp && (ret == -ENODEV)) {
1815 printk(KERN_WARNING DRIVER_NAME
1816 ": Unable to confirm device presence. You may "
1817 "experience lock-ups.\n");
1825 * Request resources.
1827 ret = wbsd_request_resources(host, base, irq, dma);
1829 wbsd_release_resources(host);
1835 * See if chip needs to be configured.
1838 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1839 printk(KERN_WARNING DRIVER_NAME
1840 ": PnP active but chip not configured! "
1841 "You probably have a buggy BIOS. "
1842 "Configuring chip manually.\n");
1843 wbsd_chip_config(host);
1846 wbsd_chip_config(host);
1849 * Power Management stuff. No idea how this works.
1854 wbsd_unlock_config(host);
1855 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1856 wbsd_lock_config(host);
1860 * Allow device to initialise itself properly.
1865 * Reset the chip into a known state.
1867 wbsd_init_device(host);
1871 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1872 if (host->chip_id != 0)
1873 printk(" id %x", (int)host->chip_id);
1874 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1876 printk(" dma %d", (int)host->dma);
1886 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1888 struct mmc_host *mmc = dev_get_drvdata(dev);
1889 struct wbsd_host *host;
1894 host = mmc_priv(mmc);
1896 mmc_remove_host(mmc);
1899 * Power down the SD/MMC function.
1902 wbsd_chip_poweroff(host);
1904 wbsd_release_resources(host);
1913 static int __devinit wbsd_probe(struct platform_device *dev)
1915 /* Use the module parameters for resources */
1916 return wbsd_init(&dev->dev, io, irq, dma, 0);
1919 static int __devexit wbsd_remove(struct platform_device *dev)
1921 wbsd_shutdown(&dev->dev, 0);
1932 static int __devinit
1933 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1938 * Get resources from PnP layer.
1940 io = pnp_port_start(pnpdev, 0);
1941 irq = pnp_irq(pnpdev, 0);
1942 if (pnp_dma_valid(pnpdev, 0))
1943 dma = pnp_dma(pnpdev, 0);
1947 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1949 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1952 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1954 wbsd_shutdown(&dev->dev, 1);
1957 #endif /* CONFIG_PNP */
1965 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1967 BUG_ON(host == NULL);
1969 return mmc_suspend_host(host->mmc, state);
1972 static int wbsd_resume(struct wbsd_host *host)
1974 BUG_ON(host == NULL);
1976 wbsd_init_device(host);
1978 return mmc_resume_host(host->mmc);
1981 static int wbsd_platform_suspend(struct platform_device *dev,
1984 struct mmc_host *mmc = platform_get_drvdata(dev);
1985 struct wbsd_host *host;
1991 DBGF("Suspending...\n");
1993 host = mmc_priv(mmc);
1995 ret = wbsd_suspend(host, state);
1999 wbsd_chip_poweroff(host);
2004 static int wbsd_platform_resume(struct platform_device *dev)
2006 struct mmc_host *mmc = platform_get_drvdata(dev);
2007 struct wbsd_host *host;
2012 DBGF("Resuming...\n");
2014 host = mmc_priv(mmc);
2016 wbsd_chip_config(host);
2019 * Allow device to initialise itself properly.
2023 return wbsd_resume(host);
2028 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2030 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2031 struct wbsd_host *host;
2036 DBGF("Suspending...\n");
2038 host = mmc_priv(mmc);
2040 return wbsd_suspend(host, state);
2043 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2045 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2046 struct wbsd_host *host;
2051 DBGF("Resuming...\n");
2053 host = mmc_priv(mmc);
2056 * See if chip needs to be configured.
2058 if (host->config != 0) {
2059 if (!wbsd_chip_validate(host)) {
2060 printk(KERN_WARNING DRIVER_NAME
2061 ": PnP active but chip not configured! "
2062 "You probably have a buggy BIOS. "
2063 "Configuring chip manually.\n");
2064 wbsd_chip_config(host);
2069 * Allow device to initialise itself properly.
2073 return wbsd_resume(host);
2076 #endif /* CONFIG_PNP */
2078 #else /* CONFIG_PM */
2080 #define wbsd_platform_suspend NULL
2081 #define wbsd_platform_resume NULL
2083 #define wbsd_pnp_suspend NULL
2084 #define wbsd_pnp_resume NULL
2086 #endif /* CONFIG_PM */
2088 static struct platform_device *wbsd_device;
2090 static struct platform_driver wbsd_driver = {
2091 .probe = wbsd_probe,
2092 .remove = __devexit_p(wbsd_remove),
2094 .suspend = wbsd_platform_suspend,
2095 .resume = wbsd_platform_resume,
2097 .name = DRIVER_NAME,
2103 static struct pnp_driver wbsd_pnp_driver = {
2104 .name = DRIVER_NAME,
2105 .id_table = pnp_dev_table,
2106 .probe = wbsd_pnp_probe,
2107 .remove = __devexit_p(wbsd_pnp_remove),
2109 .suspend = wbsd_pnp_suspend,
2110 .resume = wbsd_pnp_resume,
2113 #endif /* CONFIG_PNP */
2116 * Module loading/unloading
2119 static int __init wbsd_drv_init(void)
2123 printk(KERN_INFO DRIVER_NAME
2124 ": Winbond W83L51xD SD/MMC card interface driver, "
2125 DRIVER_VERSION "\n");
2126 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2131 result = pnp_register_driver(&wbsd_pnp_driver);
2135 #endif /* CONFIG_PNP */
2138 result = platform_driver_register(&wbsd_driver);
2142 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2144 platform_driver_unregister(&wbsd_driver);
2148 result = platform_device_add(wbsd_device);
2150 platform_device_put(wbsd_device);
2151 platform_driver_unregister(&wbsd_driver);
2159 static void __exit wbsd_drv_exit(void)
2164 pnp_unregister_driver(&wbsd_pnp_driver);
2166 #endif /* CONFIG_PNP */
2169 platform_device_unregister(wbsd_device);
2171 platform_driver_unregister(&wbsd_driver);
2177 module_init(wbsd_drv_init);
2178 module_exit(wbsd_drv_exit);
2180 module_param(nopnp, uint, 0444);
2182 module_param(io, uint, 0444);
2183 module_param(irq, uint, 0444);
2184 module_param(dma, int, 0444);
2186 MODULE_LICENSE("GPL");
2187 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2188 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2189 MODULE_VERSION(DRIVER_VERSION);
2192 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2194 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2195 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2196 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");