2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2005 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 version 2 as
8 * published by the Free Software Foundation.
13 * Changes to the FIFO system should be done with extreme care since
14 * the hardware is full of bugs related to the FIFO. Known issues are:
16 * - FIFO size field in FSR is always zero.
18 * - FIFO interrupts tend not to work as they should. Interrupts are
19 * triggered only for full/empty events, not for threshold values.
21 * - On APIC systems the FIFO empty interrupt is sometimes lost.
24 #include <linux/config.h>
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.5"
47 #ifdef CONFIG_MMC_DEBUG
49 printk(KERN_DEBUG DRIVER_NAME ": " x)
50 #define DBGF(f, x...) \
51 printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__ , ##x)
53 #define DBG(x...) do { } while (0)
54 #define DBGF(x...) do { } while (0)
63 static const struct pnp_device_id pnp_dev_table[] = {
69 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
71 #endif /* CONFIG_PNP */
73 static const int config_ports[] = { 0x2E, 0x4E };
74 static const int unlock_codes[] = { 0x83, 0x87 };
76 static const int valid_ids[] = {
81 static unsigned int nopnp = 0;
83 static const unsigned int nopnp = 1;
85 static unsigned int io = 0x248;
86 static unsigned int irq = 6;
93 static inline void wbsd_unlock_config(struct wbsd_host *host)
95 BUG_ON(host->config == 0);
97 outb(host->unlock_code, host->config);
98 outb(host->unlock_code, host->config);
101 static inline void wbsd_lock_config(struct wbsd_host *host)
103 BUG_ON(host->config == 0);
105 outb(LOCK_CODE, host->config);
108 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
110 BUG_ON(host->config == 0);
112 outb(reg, host->config);
113 outb(value, host->config + 1);
116 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
118 BUG_ON(host->config == 0);
120 outb(reg, host->config);
121 return inb(host->config + 1);
124 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
126 outb(index, host->base + WBSD_IDXR);
127 outb(value, host->base + WBSD_DATAR);
130 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
132 outb(index, host->base + WBSD_IDXR);
133 return inb(host->base + WBSD_DATAR);
140 static void wbsd_init_device(struct wbsd_host *host)
145 * Reset chip (SD/MMC part) and fifo.
147 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
148 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
149 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
154 setup &= ~WBSD_DAT3_H;
155 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
156 host->flags &= ~WBSD_FIGNORE_DETECT;
159 * Read back default clock.
161 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
166 outb(WBSD_POWER_N, host->base + WBSD_CSR);
169 * Set maximum timeout.
171 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
174 * Test for card presence
176 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
177 host->flags |= WBSD_FCARD_PRESENT;
179 host->flags &= ~WBSD_FCARD_PRESENT;
182 * Enable interesting interrupts.
185 ier |= WBSD_EINT_CARD;
186 ier |= WBSD_EINT_FIFO_THRE;
187 ier |= WBSD_EINT_CCRC;
188 ier |= WBSD_EINT_TIMEOUT;
189 ier |= WBSD_EINT_CRC;
192 outb(ier, host->base + WBSD_EIR);
197 inb(host->base + WBSD_ISR);
200 static void wbsd_reset(struct wbsd_host *host)
204 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
207 * Soft reset of chip (SD/MMC part).
209 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
210 setup |= WBSD_SOFT_RESET;
211 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
214 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
216 unsigned long dmaflags;
218 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
220 if (host->dma >= 0) {
222 * Release ISA DMA controller.
224 dmaflags = claim_dma_lock();
225 disable_dma(host->dma);
226 clear_dma_ff(host->dma);
227 release_dma_lock(dmaflags);
230 * Disable DMA on host.
232 wbsd_write_index(host, WBSD_IDX_DMA, 0);
238 * MMC layer might call back into the driver so first unlock.
240 spin_unlock(&host->lock);
241 mmc_request_done(host->mmc, mrq);
242 spin_lock(&host->lock);
246 * Scatter/gather functions
249 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
252 * Get info. about SG list from data structure.
254 host->cur_sg = data->sg;
255 host->num_sg = data->sg_len;
258 host->remain = host->cur_sg->length;
261 static inline int wbsd_next_sg(struct wbsd_host *host)
264 * Skip to next SG entry.
272 if (host->num_sg > 0) {
274 host->remain = host->cur_sg->length;
280 static inline char *wbsd_kmap_sg(struct wbsd_host *host)
282 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
283 host->cur_sg->offset;
284 return host->mapped_sg;
287 static inline void wbsd_kunmap_sg(struct wbsd_host *host)
289 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
292 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
294 unsigned int len, i, size;
295 struct scatterlist *sg;
296 char *dmabuf = host->dma_buffer;
305 * Just loop through all entries. Size might not
306 * be the entire list though so make sure that
307 * we do not transfer too much.
309 for (i = 0; i < len; i++) {
310 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
311 if (size < sg[i].length)
312 memcpy(dmabuf, sgbuf, size);
314 memcpy(dmabuf, sgbuf, sg[i].length);
315 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
316 dmabuf += sg[i].length;
318 if (size < sg[i].length)
321 size -= sg[i].length;
328 * Check that we didn't get a request to transfer
329 * more data than can fit into the SG list.
337 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
339 unsigned int len, i, size;
340 struct scatterlist *sg;
341 char *dmabuf = host->dma_buffer;
350 * Just loop through all entries. Size might not
351 * be the entire list though so make sure that
352 * we do not transfer too much.
354 for (i = 0; i < len; i++) {
355 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
356 if (size < sg[i].length)
357 memcpy(sgbuf, dmabuf, size);
359 memcpy(sgbuf, dmabuf, sg[i].length);
360 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
361 dmabuf += sg[i].length;
363 if (size < sg[i].length)
366 size -= sg[i].length;
373 * Check that we didn't get a request to transfer
374 * more data than can fit into the SG list.
386 static inline void wbsd_get_short_reply(struct wbsd_host *host,
387 struct mmc_command *cmd)
390 * Correct response type?
392 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
393 cmd->error = MMC_ERR_INVALID;
397 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
398 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
399 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
400 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
401 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
404 static inline void wbsd_get_long_reply(struct wbsd_host *host,
405 struct mmc_command *cmd)
410 * Correct response type?
412 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
413 cmd->error = MMC_ERR_INVALID;
417 for (i = 0; i < 4; i++) {
419 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
421 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
423 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
425 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
429 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
434 DBGF("Sending cmd (%x)\n", cmd->opcode);
437 * Clear accumulated ISR. The interrupt routine
438 * will fill this one with events that occur during
444 * Send the command (CRC calculated by host).
446 outb(cmd->opcode, host->base + WBSD_CMDR);
447 for (i = 3; i >= 0; i--)
448 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
450 cmd->error = MMC_ERR_NONE;
453 * Wait for the request to complete.
456 status = wbsd_read_index(host, WBSD_IDX_STATUS);
457 } while (status & WBSD_CARDTRAFFIC);
460 * Do we expect a reply?
462 if (cmd->flags & MMC_RSP_PRESENT) {
469 if (isr & WBSD_INT_CARD)
470 cmd->error = MMC_ERR_TIMEOUT;
472 else if (isr & WBSD_INT_TIMEOUT)
473 cmd->error = MMC_ERR_TIMEOUT;
475 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
476 cmd->error = MMC_ERR_BADCRC;
479 if (cmd->flags & MMC_RSP_136)
480 wbsd_get_long_reply(host, cmd);
482 wbsd_get_short_reply(host, cmd);
486 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
493 static void wbsd_empty_fifo(struct wbsd_host *host)
495 struct mmc_data *data = host->mrq->cmd->data;
500 * Handle excessive data.
502 if (data->bytes_xfered == host->size)
505 buffer = wbsd_kmap_sg(host) + host->offset;
508 * Drain the fifo. This has a tendency to loop longer
509 * than the FIFO length (usually one block).
511 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
513 * The size field in the FSR is broken so we have to
516 if (fsr & WBSD_FIFO_FULL)
518 else if (fsr & WBSD_FIFO_FUTHRE)
523 for (i = 0; i < fifo; i++) {
524 *buffer = inb(host->base + WBSD_DFR);
529 data->bytes_xfered++;
534 if (data->bytes_xfered == host->size) {
535 wbsd_kunmap_sg(host);
540 * End of scatter list entry?
542 if (host->remain == 0) {
543 wbsd_kunmap_sg(host);
546 * Get next entry. Check if last.
548 if (!wbsd_next_sg(host)) {
550 * We should never reach this point.
551 * It means that we're trying to
552 * transfer more blocks than can fit
553 * into the scatter list.
557 host->size = data->bytes_xfered;
562 buffer = wbsd_kmap_sg(host);
567 wbsd_kunmap_sg(host);
570 * This is a very dirty hack to solve a
571 * hardware problem. The chip doesn't trigger
572 * FIFO threshold interrupts properly.
574 if ((host->size - data->bytes_xfered) < 16)
575 tasklet_schedule(&host->fifo_tasklet);
578 static void wbsd_fill_fifo(struct wbsd_host *host)
580 struct mmc_data *data = host->mrq->cmd->data;
585 * Check that we aren't being called after the
586 * entire buffer has been transfered.
588 if (data->bytes_xfered == host->size)
591 buffer = wbsd_kmap_sg(host) + host->offset;
594 * Fill the fifo. This has a tendency to loop longer
595 * than the FIFO length (usually one block).
597 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
599 * The size field in the FSR is broken so we have to
602 if (fsr & WBSD_FIFO_EMPTY)
604 else if (fsr & WBSD_FIFO_EMTHRE)
609 for (i = 16; i > fifo; i--) {
610 outb(*buffer, host->base + WBSD_DFR);
615 data->bytes_xfered++;
620 if (data->bytes_xfered == host->size) {
621 wbsd_kunmap_sg(host);
626 * End of scatter list entry?
628 if (host->remain == 0) {
629 wbsd_kunmap_sg(host);
632 * Get next entry. Check if last.
634 if (!wbsd_next_sg(host)) {
636 * We should never reach this point.
637 * It means that we're trying to
638 * transfer more blocks than can fit
639 * into the scatter list.
643 host->size = data->bytes_xfered;
648 buffer = wbsd_kmap_sg(host);
653 wbsd_kunmap_sg(host);
656 * The controller stops sending interrupts for
657 * 'FIFO empty' under certain conditions. So we
658 * need to be a bit more pro-active.
660 tasklet_schedule(&host->fifo_tasklet);
663 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
667 unsigned long dmaflags;
669 DBGF("blksz %04x blks %04x flags %08x\n",
670 1 << data->blksz_bits, data->blocks, data->flags);
671 DBGF("tsac %d ms nsac %d clk\n",
672 data->timeout_ns / 1000000, data->timeout_clks);
677 host->size = data->blocks << data->blksz_bits;
680 * Check timeout values for overflow.
681 * (Yes, some cards cause this value to overflow).
683 if (data->timeout_ns > 127000000)
684 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
686 wbsd_write_index(host, WBSD_IDX_TAAC,
687 data->timeout_ns / 1000000);
690 if (data->timeout_clks > 255)
691 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
693 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
696 * Inform the chip of how large blocks will be
697 * sent. It needs this to determine when to
700 * Space for CRC must be included in the size.
701 * Two bytes are needed for each data line.
703 if (host->bus_width == MMC_BUS_WIDTH_1) {
704 blksize = (1 << data->blksz_bits) + 2;
706 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
707 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
708 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
709 blksize = (1 << data->blksz_bits) + 2 * 4;
711 wbsd_write_index(host, WBSD_IDX_PBSMSB,
712 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
713 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
715 data->error = MMC_ERR_INVALID;
720 * Clear the FIFO. This is needed even for DMA
721 * transfers since the chip still uses the FIFO
724 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
725 setup |= WBSD_FIFO_RESET;
726 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
731 if (host->dma >= 0) {
733 * The buffer for DMA is only 64 kB.
735 BUG_ON(host->size > 0x10000);
736 if (host->size > 0x10000) {
737 data->error = MMC_ERR_INVALID;
742 * Transfer data from the SG list to
745 if (data->flags & MMC_DATA_WRITE)
746 wbsd_sg_to_dma(host, data);
749 * Initialise the ISA DMA controller.
751 dmaflags = claim_dma_lock();
752 disable_dma(host->dma);
753 clear_dma_ff(host->dma);
754 if (data->flags & MMC_DATA_READ)
755 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
757 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
758 set_dma_addr(host->dma, host->dma_addr);
759 set_dma_count(host->dma, host->size);
761 enable_dma(host->dma);
762 release_dma_lock(dmaflags);
765 * Enable DMA on the host.
767 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
770 * This flag is used to keep printk
771 * output to a minimum.
776 * Initialise the SG list.
778 wbsd_init_sg(host, data);
783 wbsd_write_index(host, WBSD_IDX_DMA, 0);
786 * Set up FIFO threshold levels (and fill
787 * buffer if doing a write).
789 if (data->flags & MMC_DATA_READ) {
790 wbsd_write_index(host, WBSD_IDX_FIFOEN,
791 WBSD_FIFOEN_FULL | 8);
793 wbsd_write_index(host, WBSD_IDX_FIFOEN,
794 WBSD_FIFOEN_EMPTY | 8);
795 wbsd_fill_fifo(host);
799 data->error = MMC_ERR_NONE;
802 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
804 unsigned long dmaflags;
808 WARN_ON(host->mrq == NULL);
811 * Send a stop command if needed.
814 wbsd_send_command(host, data->stop);
817 * Wait for the controller to leave data
821 status = wbsd_read_index(host, WBSD_IDX_STATUS);
822 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
827 if (host->dma >= 0) {
829 * Disable DMA on the host.
831 wbsd_write_index(host, WBSD_IDX_DMA, 0);
834 * Turn of ISA DMA controller.
836 dmaflags = claim_dma_lock();
837 disable_dma(host->dma);
838 clear_dma_ff(host->dma);
839 count = get_dma_residue(host->dma);
840 release_dma_lock(dmaflags);
846 printk(KERN_ERR "%s: Incomplete DMA transfer. "
848 mmc_hostname(host->mmc), count);
850 data->error = MMC_ERR_FAILED;
853 * Transfer data from DMA buffer to
856 if (data->flags & MMC_DATA_READ)
857 wbsd_dma_to_sg(host, data);
859 data->bytes_xfered = host->size;
863 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
865 wbsd_request_end(host, host->mrq);
868 /*****************************************************************************\
870 * MMC layer callbacks *
872 \*****************************************************************************/
874 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
876 struct wbsd_host *host = mmc_priv(mmc);
877 struct mmc_command *cmd;
880 * Disable tasklets to avoid a deadlock.
882 spin_lock_bh(&host->lock);
884 BUG_ON(host->mrq != NULL);
891 * If there is no card in the slot then
892 * timeout immediatly.
894 if (!(host->flags & WBSD_FCARD_PRESENT)) {
895 cmd->error = MMC_ERR_TIMEOUT;
900 * Does the request include data?
903 wbsd_prepare_data(host, cmd->data);
905 if (cmd->data->error != MMC_ERR_NONE)
909 wbsd_send_command(host, cmd);
912 * If this is a data transfer the request
913 * will be finished after the data has
916 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
918 * Dirty fix for hardware bug.
921 tasklet_schedule(&host->fifo_tasklet);
923 spin_unlock_bh(&host->lock);
929 wbsd_request_end(host, mrq);
931 spin_unlock_bh(&host->lock);
934 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
936 struct wbsd_host *host = mmc_priv(mmc);
939 DBGF("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
940 ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
941 ios->vdd, ios->bus_width);
943 spin_lock_bh(&host->lock);
946 * Reset the chip on each power off.
947 * Should clear out any weird states.
949 if (ios->power_mode == MMC_POWER_OFF)
950 wbsd_init_device(host);
952 if (ios->clock >= 24000000)
954 else if (ios->clock >= 16000000)
956 else if (ios->clock >= 12000000)
962 * Only write to the clock register when
963 * there is an actual change.
965 if (clk != host->clk) {
966 wbsd_write_index(host, WBSD_IDX_CLK, clk);
973 if (ios->power_mode != MMC_POWER_OFF) {
974 pwr = inb(host->base + WBSD_CSR);
975 pwr &= ~WBSD_POWER_N;
976 outb(pwr, host->base + WBSD_CSR);
980 * MMC cards need to have pin 1 high during init.
981 * It wreaks havoc with the card detection though so
982 * that needs to be disabled.
984 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
985 if (ios->chip_select == MMC_CS_HIGH) {
986 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
987 setup |= WBSD_DAT3_H;
988 host->flags |= WBSD_FIGNORE_DETECT;
990 if (setup & WBSD_DAT3_H) {
991 setup &= ~WBSD_DAT3_H;
994 * We cannot resume card detection immediatly
995 * because of capacitance and delays in the chip.
997 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1000 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1003 * Store bus width for later. Will be used when
1004 * setting up the data transfer.
1006 host->bus_width = ios->bus_width;
1008 spin_unlock_bh(&host->lock);
1011 static int wbsd_get_ro(struct mmc_host *mmc)
1013 struct wbsd_host *host = mmc_priv(mmc);
1016 spin_lock_bh(&host->lock);
1018 csr = inb(host->base + WBSD_CSR);
1020 outb(csr, host->base + WBSD_CSR);
1024 csr = inb(host->base + WBSD_CSR);
1026 outb(csr, host->base + WBSD_CSR);
1028 spin_unlock_bh(&host->lock);
1030 return csr & WBSD_WRPT;
1033 static struct mmc_host_ops wbsd_ops = {
1034 .request = wbsd_request,
1035 .set_ios = wbsd_set_ios,
1036 .get_ro = wbsd_get_ro,
1039 /*****************************************************************************\
1041 * Interrupt handling *
1043 \*****************************************************************************/
1046 * Helper function to reset detection ignore
1049 static void wbsd_reset_ignore(unsigned long data)
1051 struct wbsd_host *host = (struct wbsd_host *)data;
1053 BUG_ON(host == NULL);
1055 DBG("Resetting card detection ignore\n");
1057 spin_lock_bh(&host->lock);
1059 host->flags &= ~WBSD_FIGNORE_DETECT;
1062 * Card status might have changed during the
1065 tasklet_schedule(&host->card_tasklet);
1067 spin_unlock_bh(&host->lock);
1074 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1076 WARN_ON(!host->mrq);
1080 WARN_ON(!host->mrq->cmd);
1081 if (!host->mrq->cmd)
1084 WARN_ON(!host->mrq->cmd->data);
1085 if (!host->mrq->cmd->data)
1088 return host->mrq->cmd->data;
1091 static void wbsd_tasklet_card(unsigned long param)
1093 struct wbsd_host *host = (struct wbsd_host *)param;
1097 spin_lock(&host->lock);
1099 if (host->flags & WBSD_FIGNORE_DETECT) {
1100 spin_unlock(&host->lock);
1104 csr = inb(host->base + WBSD_CSR);
1105 WARN_ON(csr == 0xff);
1107 if (csr & WBSD_CARDPRESENT) {
1108 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1109 DBG("Card inserted\n");
1110 host->flags |= WBSD_FCARD_PRESENT;
1114 } else if (host->flags & WBSD_FCARD_PRESENT) {
1115 DBG("Card removed\n");
1116 host->flags &= ~WBSD_FCARD_PRESENT;
1119 printk(KERN_ERR "%s: Card removed during transfer!\n",
1120 mmc_hostname(host->mmc));
1123 host->mrq->cmd->error = MMC_ERR_FAILED;
1124 tasklet_schedule(&host->finish_tasklet);
1131 * Unlock first since we might get a call back.
1134 spin_unlock(&host->lock);
1137 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1140 static void wbsd_tasklet_fifo(unsigned long param)
1142 struct wbsd_host *host = (struct wbsd_host *)param;
1143 struct mmc_data *data;
1145 spin_lock(&host->lock);
1150 data = wbsd_get_data(host);
1154 if (data->flags & MMC_DATA_WRITE)
1155 wbsd_fill_fifo(host);
1157 wbsd_empty_fifo(host);
1162 if (host->size == data->bytes_xfered) {
1163 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1164 tasklet_schedule(&host->finish_tasklet);
1168 spin_unlock(&host->lock);
1171 static void wbsd_tasklet_crc(unsigned long param)
1173 struct wbsd_host *host = (struct wbsd_host *)param;
1174 struct mmc_data *data;
1176 spin_lock(&host->lock);
1181 data = wbsd_get_data(host);
1185 DBGF("CRC error\n");
1187 data->error = MMC_ERR_BADCRC;
1189 tasklet_schedule(&host->finish_tasklet);
1192 spin_unlock(&host->lock);
1195 static void wbsd_tasklet_timeout(unsigned long param)
1197 struct wbsd_host *host = (struct wbsd_host *)param;
1198 struct mmc_data *data;
1200 spin_lock(&host->lock);
1205 data = wbsd_get_data(host);
1211 data->error = MMC_ERR_TIMEOUT;
1213 tasklet_schedule(&host->finish_tasklet);
1216 spin_unlock(&host->lock);
1219 static void wbsd_tasklet_finish(unsigned long param)
1221 struct wbsd_host *host = (struct wbsd_host *)param;
1222 struct mmc_data *data;
1224 spin_lock(&host->lock);
1226 WARN_ON(!host->mrq);
1230 data = wbsd_get_data(host);
1234 wbsd_finish_data(host, data);
1237 spin_unlock(&host->lock);
1240 static void wbsd_tasklet_block(unsigned long param)
1242 struct wbsd_host *host = (struct wbsd_host *)param;
1243 struct mmc_data *data;
1245 spin_lock(&host->lock);
1247 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1249 data = wbsd_get_data(host);
1253 DBGF("CRC error\n");
1255 data->error = MMC_ERR_BADCRC;
1257 tasklet_schedule(&host->finish_tasklet);
1261 spin_unlock(&host->lock);
1265 * Interrupt handling
1268 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1270 struct wbsd_host *host = dev_id;
1273 isr = inb(host->base + WBSD_ISR);
1276 * Was it actually our hardware that caused the interrupt?
1278 if (isr == 0xff || isr == 0x00)
1284 * Schedule tasklets as needed.
1286 if (isr & WBSD_INT_CARD)
1287 tasklet_schedule(&host->card_tasklet);
1288 if (isr & WBSD_INT_FIFO_THRE)
1289 tasklet_schedule(&host->fifo_tasklet);
1290 if (isr & WBSD_INT_CRC)
1291 tasklet_hi_schedule(&host->crc_tasklet);
1292 if (isr & WBSD_INT_TIMEOUT)
1293 tasklet_hi_schedule(&host->timeout_tasklet);
1294 if (isr & WBSD_INT_BUSYEND)
1295 tasklet_hi_schedule(&host->block_tasklet);
1296 if (isr & WBSD_INT_TC)
1297 tasklet_schedule(&host->finish_tasklet);
1302 /*****************************************************************************\
1304 * Device initialisation and shutdown *
1306 \*****************************************************************************/
1309 * Allocate/free MMC structure.
1312 static int __devinit wbsd_alloc_mmc(struct device *dev)
1314 struct mmc_host *mmc;
1315 struct wbsd_host *host;
1318 * Allocate MMC structure.
1320 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1324 host = mmc_priv(mmc);
1330 * Set host parameters.
1332 mmc->ops = &wbsd_ops;
1333 mmc->f_min = 375000;
1334 mmc->f_max = 24000000;
1335 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1336 mmc->caps = MMC_CAP_4_BIT_DATA;
1338 spin_lock_init(&host->lock);
1343 init_timer(&host->ignore_timer);
1344 host->ignore_timer.data = (unsigned long)host;
1345 host->ignore_timer.function = wbsd_reset_ignore;
1348 * Maximum number of segments. Worst case is one sector per segment
1349 * so this will be 64kB/512.
1351 mmc->max_hw_segs = 128;
1352 mmc->max_phys_segs = 128;
1355 * Maximum number of sectors in one transfer. Also limited by 64kB
1358 mmc->max_sectors = 128;
1361 * Maximum segment size. Could be one segment with the maximum number
1364 mmc->max_seg_size = mmc->max_sectors * 512;
1366 dev_set_drvdata(dev, mmc);
1371 static void __devexit wbsd_free_mmc(struct device *dev)
1373 struct mmc_host *mmc;
1374 struct wbsd_host *host;
1376 mmc = dev_get_drvdata(dev);
1380 host = mmc_priv(mmc);
1381 BUG_ON(host == NULL);
1383 del_timer_sync(&host->ignore_timer);
1387 dev_set_drvdata(dev, NULL);
1391 * Scan for known chip id:s
1394 static int __devinit wbsd_scan(struct wbsd_host *host)
1400 * Iterate through all ports, all codes to
1401 * find hardware that is in our known list.
1403 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1404 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1407 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1410 host->config = config_ports[i];
1411 host->unlock_code = unlock_codes[j];
1413 wbsd_unlock_config(host);
1415 outb(WBSD_CONF_ID_HI, config_ports[i]);
1416 id = inb(config_ports[i] + 1) << 8;
1418 outb(WBSD_CONF_ID_LO, config_ports[i]);
1419 id |= inb(config_ports[i] + 1);
1421 wbsd_lock_config(host);
1423 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1424 if (id == valid_ids[k]) {
1432 DBG("Unknown hardware (id %x) found at %x\n",
1433 id, config_ports[i]);
1437 release_region(config_ports[i], 2);
1441 host->unlock_code = 0;
1447 * Allocate/free io port ranges
1450 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1455 if (!request_region(base, 8, DRIVER_NAME))
1463 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1466 release_region(host->base, 8);
1471 release_region(host->config, 2);
1477 * Allocate/free DMA port and buffer
1480 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1485 if (request_dma(dma, DRIVER_NAME))
1489 * We need to allocate a special buffer in
1490 * order for ISA to be able to DMA to it.
1492 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1493 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1494 if (!host->dma_buffer)
1498 * Translate the address to a physical address.
1500 host->dma_addr = dma_map_single(host->mmc->dev, host->dma_buffer,
1501 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1504 * ISA DMA must be aligned on a 64k basis.
1506 if ((host->dma_addr & 0xffff) != 0)
1509 * ISA cannot access memory above 16 MB.
1511 else if (host->dma_addr >= 0x1000000)
1520 * If we've gotten here then there is some kind of alignment bug
1524 dma_unmap_single(host->mmc->dev, host->dma_addr,
1525 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1526 host->dma_addr = (dma_addr_t)NULL;
1528 kfree(host->dma_buffer);
1529 host->dma_buffer = NULL;
1535 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1536 "Falling back on FIFO.\n", dma);
1539 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1541 if (host->dma_addr) {
1542 dma_unmap_single(host->mmc->dev, host->dma_addr,
1543 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1545 kfree(host->dma_buffer);
1547 free_dma(host->dma);
1550 host->dma_buffer = NULL;
1551 host->dma_addr = (dma_addr_t)NULL;
1555 * Allocate/free IRQ.
1558 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1563 * Allocate interrupt.
1566 ret = request_irq(irq, wbsd_irq, SA_SHIRQ, DRIVER_NAME, host);
1575 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1576 (unsigned long)host);
1577 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1578 (unsigned long)host);
1579 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1580 (unsigned long)host);
1581 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1582 (unsigned long)host);
1583 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1584 (unsigned long)host);
1585 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1586 (unsigned long)host);
1591 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1596 free_irq(host->irq, host);
1600 tasklet_kill(&host->card_tasklet);
1601 tasklet_kill(&host->fifo_tasklet);
1602 tasklet_kill(&host->crc_tasklet);
1603 tasklet_kill(&host->timeout_tasklet);
1604 tasklet_kill(&host->finish_tasklet);
1605 tasklet_kill(&host->block_tasklet);
1609 * Allocate all resources for the host.
1612 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1613 int base, int irq, int dma)
1618 * Allocate I/O ports.
1620 ret = wbsd_request_region(host, base);
1625 * Allocate interrupt.
1627 ret = wbsd_request_irq(host, irq);
1634 wbsd_request_dma(host, dma);
1640 * Release all resources for the host.
1643 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1645 wbsd_release_dma(host);
1646 wbsd_release_irq(host);
1647 wbsd_release_regions(host);
1651 * Configure the resources the chip should use.
1654 static void wbsd_chip_config(struct wbsd_host *host)
1656 wbsd_unlock_config(host);
1661 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1662 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1665 * Select SD/MMC function.
1667 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1670 * Set up card detection.
1672 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1677 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1678 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1680 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1683 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1686 * Enable and power up chip.
1688 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1689 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1691 wbsd_lock_config(host);
1695 * Check that configured resources are correct.
1698 static int wbsd_chip_validate(struct wbsd_host *host)
1702 wbsd_unlock_config(host);
1705 * Select SD/MMC function.
1707 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1710 * Read configuration.
1712 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1713 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1715 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1717 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1719 wbsd_lock_config(host);
1722 * Validate against given configuration.
1724 if (base != host->base)
1726 if (irq != host->irq)
1728 if ((dma != host->dma) && (host->dma != -1))
1735 * Powers down the SD function
1738 static void wbsd_chip_poweroff(struct wbsd_host *host)
1740 wbsd_unlock_config(host);
1742 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1743 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1745 wbsd_lock_config(host);
1748 /*****************************************************************************\
1750 * Devices setup and shutdown *
1752 \*****************************************************************************/
1754 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1757 struct wbsd_host *host = NULL;
1758 struct mmc_host *mmc = NULL;
1761 ret = wbsd_alloc_mmc(dev);
1765 mmc = dev_get_drvdata(dev);
1766 host = mmc_priv(mmc);
1769 * Scan for hardware.
1771 ret = wbsd_scan(host);
1773 if (pnp && (ret == -ENODEV)) {
1774 printk(KERN_WARNING DRIVER_NAME
1775 ": Unable to confirm device presence. You may "
1776 "experience lock-ups.\n");
1784 * Request resources.
1786 ret = wbsd_request_resources(host, io, irq, dma);
1788 wbsd_release_resources(host);
1794 * See if chip needs to be configured.
1797 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1798 printk(KERN_WARNING DRIVER_NAME
1799 ": PnP active but chip not configured! "
1800 "You probably have a buggy BIOS. "
1801 "Configuring chip manually.\n");
1802 wbsd_chip_config(host);
1805 wbsd_chip_config(host);
1808 * Power Management stuff. No idea how this works.
1813 wbsd_unlock_config(host);
1814 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1815 wbsd_lock_config(host);
1819 * Allow device to initialise itself properly.
1824 * Reset the chip into a known state.
1826 wbsd_init_device(host);
1830 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1831 if (host->chip_id != 0)
1832 printk(" id %x", (int)host->chip_id);
1833 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1835 printk(" dma %d", (int)host->dma);
1845 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1847 struct mmc_host *mmc = dev_get_drvdata(dev);
1848 struct wbsd_host *host;
1853 host = mmc_priv(mmc);
1855 mmc_remove_host(mmc);
1858 * Power down the SD/MMC function.
1861 wbsd_chip_poweroff(host);
1863 wbsd_release_resources(host);
1872 static int __devinit wbsd_probe(struct platform_device *dev)
1874 return wbsd_init(&dev->dev, io, irq, dma, 0);
1877 static int __devexit wbsd_remove(struct platform_device *dev)
1879 wbsd_shutdown(&dev->dev, 0);
1890 static int __devinit
1891 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1896 * Get resources from PnP layer.
1898 io = pnp_port_start(pnpdev, 0);
1899 irq = pnp_irq(pnpdev, 0);
1900 if (pnp_dma_valid(pnpdev, 0))
1901 dma = pnp_dma(pnpdev, 0);
1905 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1907 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1910 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1912 wbsd_shutdown(&dev->dev, 1);
1915 #endif /* CONFIG_PNP */
1923 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1925 BUG_ON(host == NULL);
1927 return mmc_suspend_host(host->mmc, state);
1930 static int wbsd_resume(struct wbsd_host *host)
1932 BUG_ON(host == NULL);
1934 wbsd_init_device(host);
1936 return mmc_resume_host(host->mmc);
1939 static int wbsd_platform_suspend(struct platform_device *dev,
1942 struct mmc_host *mmc = platform_get_drvdata(dev);
1943 struct wbsd_host *host;
1949 DBGF("Suspending...\n");
1951 host = mmc_priv(mmc);
1953 ret = wbsd_suspend(host, state);
1957 wbsd_chip_poweroff(host);
1962 static int wbsd_platform_resume(struct platform_device *dev)
1964 struct mmc_host *mmc = platform_get_drvdata(dev);
1965 struct wbsd_host *host;
1970 DBGF("Resuming...\n");
1972 host = mmc_priv(mmc);
1974 wbsd_chip_config(host);
1977 * Allow device to initialise itself properly.
1981 return wbsd_resume(host);
1986 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1988 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1989 struct wbsd_host *host;
1994 DBGF("Suspending...\n");
1996 host = mmc_priv(mmc);
1998 return wbsd_suspend(host, state);
2001 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2003 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2004 struct wbsd_host *host;
2009 DBGF("Resuming...\n");
2011 host = mmc_priv(mmc);
2014 * See if chip needs to be configured.
2016 if (host->config != 0) {
2017 if (!wbsd_chip_validate(host)) {
2018 printk(KERN_WARNING DRIVER_NAME
2019 ": PnP active but chip not configured! "
2020 "You probably have a buggy BIOS. "
2021 "Configuring chip manually.\n");
2022 wbsd_chip_config(host);
2027 * Allow device to initialise itself properly.
2031 return wbsd_resume(host);
2034 #endif /* CONFIG_PNP */
2036 #else /* CONFIG_PM */
2038 #define wbsd_platform_suspend NULL
2039 #define wbsd_platform_resume NULL
2041 #define wbsd_pnp_suspend NULL
2042 #define wbsd_pnp_resume NULL
2044 #endif /* CONFIG_PM */
2046 static struct platform_device *wbsd_device;
2048 static struct platform_driver wbsd_driver = {
2049 .probe = wbsd_probe,
2050 .remove = __devexit_p(wbsd_remove),
2052 .suspend = wbsd_platform_suspend,
2053 .resume = wbsd_platform_resume,
2055 .name = DRIVER_NAME,
2061 static struct pnp_driver wbsd_pnp_driver = {
2062 .name = DRIVER_NAME,
2063 .id_table = pnp_dev_table,
2064 .probe = wbsd_pnp_probe,
2065 .remove = __devexit_p(wbsd_pnp_remove),
2067 .suspend = wbsd_pnp_suspend,
2068 .resume = wbsd_pnp_resume,
2071 #endif /* CONFIG_PNP */
2074 * Module loading/unloading
2077 static int __init wbsd_drv_init(void)
2081 printk(KERN_INFO DRIVER_NAME
2082 ": Winbond W83L51xD SD/MMC card interface driver, "
2083 DRIVER_VERSION "\n");
2084 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2089 result = pnp_register_driver(&wbsd_pnp_driver);
2093 #endif /* CONFIG_PNP */
2096 result = platform_driver_register(&wbsd_driver);
2100 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2102 platform_driver_unregister(&wbsd_driver);
2106 result = platform_device_add(wbsd_device);
2108 platform_device_put(wbsd_device);
2109 platform_driver_unregister(&wbsd_driver);
2117 static void __exit wbsd_drv_exit(void)
2122 pnp_unregister_driver(&wbsd_pnp_driver);
2124 #endif /* CONFIG_PNP */
2127 platform_device_unregister(wbsd_device);
2129 platform_driver_unregister(&wbsd_driver);
2135 module_init(wbsd_drv_init);
2136 module_exit(wbsd_drv_exit);
2138 module_param(nopnp, uint, 0444);
2140 module_param(io, uint, 0444);
2141 module_param(irq, uint, 0444);
2142 module_param(dma, int, 0444);
2144 MODULE_LICENSE("GPL");
2145 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2146 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2147 MODULE_VERSION(DRIVER_VERSION);
2150 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2152 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2153 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2154 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");