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_sg_to_buffer(struct wbsd_host *host)
277 return page_address(host->cur_sg->page) + host->cur_sg->offset;
280 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
282 unsigned int len, i, size;
283 struct scatterlist *sg;
284 char *dmabuf = host->dma_buffer;
293 * Just loop through all entries. Size might not
294 * be the entire list though so make sure that
295 * we do not transfer too much.
297 for (i = 0; i < len; i++) {
298 sgbuf = page_address(sg[i].page) + sg[i].offset;
299 if (size < sg[i].length)
300 memcpy(dmabuf, sgbuf, size);
302 memcpy(dmabuf, sgbuf, sg[i].length);
303 dmabuf += sg[i].length;
305 if (size < sg[i].length)
308 size -= sg[i].length;
315 * Check that we didn't get a request to transfer
316 * more data than can fit into the SG list.
324 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
326 unsigned int len, i, size;
327 struct scatterlist *sg;
328 char *dmabuf = host->dma_buffer;
337 * Just loop through all entries. Size might not
338 * be the entire list though so make sure that
339 * we do not transfer too much.
341 for (i = 0; i < len; i++) {
342 sgbuf = page_address(sg[i].page) + sg[i].offset;
343 if (size < sg[i].length)
344 memcpy(sgbuf, dmabuf, size);
346 memcpy(sgbuf, dmabuf, sg[i].length);
347 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
348 dmabuf += sg[i].length;
350 if (size < sg[i].length)
353 size -= sg[i].length;
360 * Check that we didn't get a request to transfer
361 * more data than can fit into the SG list.
373 static inline void wbsd_get_short_reply(struct wbsd_host *host,
374 struct mmc_command *cmd)
377 * Correct response type?
379 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
380 cmd->error = MMC_ERR_INVALID;
384 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
386 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
387 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
388 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
391 static inline void wbsd_get_long_reply(struct wbsd_host *host,
392 struct mmc_command *cmd)
397 * Correct response type?
399 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
400 cmd->error = MMC_ERR_INVALID;
404 for (i = 0; i < 4; i++) {
406 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
408 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
410 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
412 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
416 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
421 DBGF("Sending cmd (%x)\n", cmd->opcode);
424 * Clear accumulated ISR. The interrupt routine
425 * will fill this one with events that occur during
431 * Send the command (CRC calculated by host).
433 outb(cmd->opcode, host->base + WBSD_CMDR);
434 for (i = 3; i >= 0; i--)
435 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
437 cmd->error = MMC_ERR_NONE;
440 * Wait for the request to complete.
443 status = wbsd_read_index(host, WBSD_IDX_STATUS);
444 } while (status & WBSD_CARDTRAFFIC);
447 * Do we expect a reply?
449 if (cmd->flags & MMC_RSP_PRESENT) {
456 if (isr & WBSD_INT_CARD)
457 cmd->error = MMC_ERR_TIMEOUT;
459 else if (isr & WBSD_INT_TIMEOUT)
460 cmd->error = MMC_ERR_TIMEOUT;
462 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
463 cmd->error = MMC_ERR_BADCRC;
466 if (cmd->flags & MMC_RSP_136)
467 wbsd_get_long_reply(host, cmd);
469 wbsd_get_short_reply(host, cmd);
473 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
480 static void wbsd_empty_fifo(struct wbsd_host *host)
482 struct mmc_data *data = host->mrq->cmd->data;
487 * Handle excessive data.
489 if (data->bytes_xfered == host->size)
492 buffer = wbsd_sg_to_buffer(host) + host->offset;
495 * Drain the fifo. This has a tendency to loop longer
496 * than the FIFO length (usually one block).
498 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
500 * The size field in the FSR is broken so we have to
503 if (fsr & WBSD_FIFO_FULL)
505 else if (fsr & WBSD_FIFO_FUTHRE)
510 for (i = 0; i < fifo; i++) {
511 *buffer = inb(host->base + WBSD_DFR);
516 data->bytes_xfered++;
521 if (data->bytes_xfered == host->size)
525 * End of scatter list entry?
527 if (host->remain == 0) {
529 * Get next entry. Check if last.
531 if (!wbsd_next_sg(host)) {
533 * We should never reach this point.
534 * It means that we're trying to
535 * transfer more blocks than can fit
536 * into the scatter list.
540 host->size = data->bytes_xfered;
545 buffer = wbsd_sg_to_buffer(host);
551 * This is a very dirty hack to solve a
552 * hardware problem. The chip doesn't trigger
553 * FIFO threshold interrupts properly.
555 if ((host->size - data->bytes_xfered) < 16)
556 tasklet_schedule(&host->fifo_tasklet);
559 static void wbsd_fill_fifo(struct wbsd_host *host)
561 struct mmc_data *data = host->mrq->cmd->data;
566 * Check that we aren't being called after the
567 * entire buffer has been transfered.
569 if (data->bytes_xfered == host->size)
572 buffer = wbsd_sg_to_buffer(host) + host->offset;
575 * Fill the fifo. This has a tendency to loop longer
576 * than the FIFO length (usually one block).
578 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
580 * The size field in the FSR is broken so we have to
583 if (fsr & WBSD_FIFO_EMPTY)
585 else if (fsr & WBSD_FIFO_EMTHRE)
590 for (i = 16; i > fifo; i--) {
591 outb(*buffer, host->base + WBSD_DFR);
596 data->bytes_xfered++;
601 if (data->bytes_xfered == host->size)
605 * End of scatter list entry?
607 if (host->remain == 0) {
609 * Get next entry. Check if last.
611 if (!wbsd_next_sg(host)) {
613 * We should never reach this point.
614 * It means that we're trying to
615 * transfer more blocks than can fit
616 * into the scatter list.
620 host->size = data->bytes_xfered;
625 buffer = wbsd_sg_to_buffer(host);
631 * The controller stops sending interrupts for
632 * 'FIFO empty' under certain conditions. So we
633 * need to be a bit more pro-active.
635 tasklet_schedule(&host->fifo_tasklet);
638 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
642 unsigned long dmaflags;
644 DBGF("blksz %04x blks %04x flags %08x\n",
645 data->blksz, data->blocks, data->flags);
646 DBGF("tsac %d ms nsac %d clk\n",
647 data->timeout_ns / 1000000, data->timeout_clks);
652 host->size = data->blocks * data->blksz;
655 * Check timeout values for overflow.
656 * (Yes, some cards cause this value to overflow).
658 if (data->timeout_ns > 127000000)
659 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
661 wbsd_write_index(host, WBSD_IDX_TAAC,
662 data->timeout_ns / 1000000);
665 if (data->timeout_clks > 255)
666 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
668 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
671 * Inform the chip of how large blocks will be
672 * sent. It needs this to determine when to
675 * Space for CRC must be included in the size.
676 * Two bytes are needed for each data line.
678 if (host->bus_width == MMC_BUS_WIDTH_1) {
679 blksize = data->blksz + 2;
681 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
682 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
683 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
684 blksize = data->blksz + 2 * 4;
686 wbsd_write_index(host, WBSD_IDX_PBSMSB,
687 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
688 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
690 data->error = MMC_ERR_INVALID;
695 * Clear the FIFO. This is needed even for DMA
696 * transfers since the chip still uses the FIFO
699 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
700 setup |= WBSD_FIFO_RESET;
701 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
706 if (host->dma >= 0) {
708 * The buffer for DMA is only 64 kB.
710 BUG_ON(host->size > 0x10000);
711 if (host->size > 0x10000) {
712 data->error = MMC_ERR_INVALID;
717 * Transfer data from the SG list to
720 if (data->flags & MMC_DATA_WRITE)
721 wbsd_sg_to_dma(host, data);
724 * Initialise the ISA DMA controller.
726 dmaflags = claim_dma_lock();
727 disable_dma(host->dma);
728 clear_dma_ff(host->dma);
729 if (data->flags & MMC_DATA_READ)
730 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
732 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
733 set_dma_addr(host->dma, host->dma_addr);
734 set_dma_count(host->dma, host->size);
736 enable_dma(host->dma);
737 release_dma_lock(dmaflags);
740 * Enable DMA on the host.
742 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
745 * This flag is used to keep printk
746 * output to a minimum.
751 * Initialise the SG list.
753 wbsd_init_sg(host, data);
758 wbsd_write_index(host, WBSD_IDX_DMA, 0);
761 * Set up FIFO threshold levels (and fill
762 * buffer if doing a write).
764 if (data->flags & MMC_DATA_READ) {
765 wbsd_write_index(host, WBSD_IDX_FIFOEN,
766 WBSD_FIFOEN_FULL | 8);
768 wbsd_write_index(host, WBSD_IDX_FIFOEN,
769 WBSD_FIFOEN_EMPTY | 8);
770 wbsd_fill_fifo(host);
774 data->error = MMC_ERR_NONE;
777 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
779 unsigned long dmaflags;
783 WARN_ON(host->mrq == NULL);
786 * Send a stop command if needed.
789 wbsd_send_command(host, data->stop);
792 * Wait for the controller to leave data
796 status = wbsd_read_index(host, WBSD_IDX_STATUS);
797 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
802 if (host->dma >= 0) {
804 * Disable DMA on the host.
806 wbsd_write_index(host, WBSD_IDX_DMA, 0);
809 * Turn of ISA DMA controller.
811 dmaflags = claim_dma_lock();
812 disable_dma(host->dma);
813 clear_dma_ff(host->dma);
814 count = get_dma_residue(host->dma);
815 release_dma_lock(dmaflags);
821 printk(KERN_ERR "%s: Incomplete DMA transfer. "
823 mmc_hostname(host->mmc), count);
825 data->error = MMC_ERR_FAILED;
828 * Transfer data from DMA buffer to
831 if (data->flags & MMC_DATA_READ)
832 wbsd_dma_to_sg(host, data);
834 data->bytes_xfered = host->size;
838 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
840 wbsd_request_end(host, host->mrq);
843 /*****************************************************************************\
845 * MMC layer callbacks *
847 \*****************************************************************************/
849 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
851 struct wbsd_host *host = mmc_priv(mmc);
852 struct mmc_command *cmd;
855 * Disable tasklets to avoid a deadlock.
857 spin_lock_bh(&host->lock);
859 BUG_ON(host->mrq != NULL);
866 * If there is no card in the slot then
867 * timeout immediatly.
869 if (!(host->flags & WBSD_FCARD_PRESENT)) {
870 cmd->error = MMC_ERR_TIMEOUT;
875 * Does the request include data?
878 wbsd_prepare_data(host, cmd->data);
880 if (cmd->data->error != MMC_ERR_NONE)
884 wbsd_send_command(host, cmd);
887 * If this is a data transfer the request
888 * will be finished after the data has
891 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
893 * The hardware is so delightfully stupid that it has a list
894 * of "data" commands. If a command isn't on this list, it'll
895 * just go back to the idle state and won't send any data
898 switch (cmd->opcode) {
912 /* ACMDs. We don't keep track of state, so we just treat them
913 * like any other command. */
918 #ifdef CONFIG_MMC_DEBUG
919 printk(KERN_WARNING "%s: Data command %d is not "
920 "supported by this controller.\n",
921 mmc_hostname(host->mmc), cmd->opcode);
923 cmd->data->error = MMC_ERR_INVALID;
926 wbsd_send_command(host, cmd->data->stop);
932 * Dirty fix for hardware bug.
935 tasklet_schedule(&host->fifo_tasklet);
937 spin_unlock_bh(&host->lock);
943 wbsd_request_end(host, mrq);
945 spin_unlock_bh(&host->lock);
948 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
950 struct wbsd_host *host = mmc_priv(mmc);
953 spin_lock_bh(&host->lock);
956 * Reset the chip on each power off.
957 * Should clear out any weird states.
959 if (ios->power_mode == MMC_POWER_OFF)
960 wbsd_init_device(host);
962 if (ios->clock >= 24000000)
964 else if (ios->clock >= 16000000)
966 else if (ios->clock >= 12000000)
972 * Only write to the clock register when
973 * there is an actual change.
975 if (clk != host->clk) {
976 wbsd_write_index(host, WBSD_IDX_CLK, clk);
983 if (ios->power_mode != MMC_POWER_OFF) {
984 pwr = inb(host->base + WBSD_CSR);
985 pwr &= ~WBSD_POWER_N;
986 outb(pwr, host->base + WBSD_CSR);
990 * MMC cards need to have pin 1 high during init.
991 * It wreaks havoc with the card detection though so
992 * that needs to be disabled.
994 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
995 if (ios->chip_select == MMC_CS_HIGH) {
996 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
997 setup |= WBSD_DAT3_H;
998 host->flags |= WBSD_FIGNORE_DETECT;
1000 if (setup & WBSD_DAT3_H) {
1001 setup &= ~WBSD_DAT3_H;
1004 * We cannot resume card detection immediatly
1005 * because of capacitance and delays in the chip.
1007 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1010 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1013 * Store bus width for later. Will be used when
1014 * setting up the data transfer.
1016 host->bus_width = ios->bus_width;
1018 spin_unlock_bh(&host->lock);
1021 static int wbsd_get_ro(struct mmc_host *mmc)
1023 struct wbsd_host *host = mmc_priv(mmc);
1026 spin_lock_bh(&host->lock);
1028 csr = inb(host->base + WBSD_CSR);
1030 outb(csr, host->base + WBSD_CSR);
1034 csr = inb(host->base + WBSD_CSR);
1036 outb(csr, host->base + WBSD_CSR);
1038 spin_unlock_bh(&host->lock);
1040 return csr & WBSD_WRPT;
1043 static const struct mmc_host_ops wbsd_ops = {
1044 .request = wbsd_request,
1045 .set_ios = wbsd_set_ios,
1046 .get_ro = wbsd_get_ro,
1049 /*****************************************************************************\
1051 * Interrupt handling *
1053 \*****************************************************************************/
1056 * Helper function to reset detection ignore
1059 static void wbsd_reset_ignore(unsigned long data)
1061 struct wbsd_host *host = (struct wbsd_host *)data;
1063 BUG_ON(host == NULL);
1065 DBG("Resetting card detection ignore\n");
1067 spin_lock_bh(&host->lock);
1069 host->flags &= ~WBSD_FIGNORE_DETECT;
1072 * Card status might have changed during the
1075 tasklet_schedule(&host->card_tasklet);
1077 spin_unlock_bh(&host->lock);
1084 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1086 WARN_ON(!host->mrq);
1090 WARN_ON(!host->mrq->cmd);
1091 if (!host->mrq->cmd)
1094 WARN_ON(!host->mrq->cmd->data);
1095 if (!host->mrq->cmd->data)
1098 return host->mrq->cmd->data;
1101 static void wbsd_tasklet_card(unsigned long param)
1103 struct wbsd_host *host = (struct wbsd_host *)param;
1107 spin_lock(&host->lock);
1109 if (host->flags & WBSD_FIGNORE_DETECT) {
1110 spin_unlock(&host->lock);
1114 csr = inb(host->base + WBSD_CSR);
1115 WARN_ON(csr == 0xff);
1117 if (csr & WBSD_CARDPRESENT) {
1118 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1119 DBG("Card inserted\n");
1120 host->flags |= WBSD_FCARD_PRESENT;
1124 } else if (host->flags & WBSD_FCARD_PRESENT) {
1125 DBG("Card removed\n");
1126 host->flags &= ~WBSD_FCARD_PRESENT;
1129 printk(KERN_ERR "%s: Card removed during transfer!\n",
1130 mmc_hostname(host->mmc));
1133 host->mrq->cmd->error = MMC_ERR_FAILED;
1134 tasklet_schedule(&host->finish_tasklet);
1141 * Unlock first since we might get a call back.
1144 spin_unlock(&host->lock);
1147 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1150 static void wbsd_tasklet_fifo(unsigned long param)
1152 struct wbsd_host *host = (struct wbsd_host *)param;
1153 struct mmc_data *data;
1155 spin_lock(&host->lock);
1160 data = wbsd_get_data(host);
1164 if (data->flags & MMC_DATA_WRITE)
1165 wbsd_fill_fifo(host);
1167 wbsd_empty_fifo(host);
1172 if (host->size == data->bytes_xfered) {
1173 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1174 tasklet_schedule(&host->finish_tasklet);
1178 spin_unlock(&host->lock);
1181 static void wbsd_tasklet_crc(unsigned long param)
1183 struct wbsd_host *host = (struct wbsd_host *)param;
1184 struct mmc_data *data;
1186 spin_lock(&host->lock);
1191 data = wbsd_get_data(host);
1195 DBGF("CRC error\n");
1197 data->error = MMC_ERR_BADCRC;
1199 tasklet_schedule(&host->finish_tasklet);
1202 spin_unlock(&host->lock);
1205 static void wbsd_tasklet_timeout(unsigned long param)
1207 struct wbsd_host *host = (struct wbsd_host *)param;
1208 struct mmc_data *data;
1210 spin_lock(&host->lock);
1215 data = wbsd_get_data(host);
1221 data->error = MMC_ERR_TIMEOUT;
1223 tasklet_schedule(&host->finish_tasklet);
1226 spin_unlock(&host->lock);
1229 static void wbsd_tasklet_finish(unsigned long param)
1231 struct wbsd_host *host = (struct wbsd_host *)param;
1232 struct mmc_data *data;
1234 spin_lock(&host->lock);
1236 WARN_ON(!host->mrq);
1240 data = wbsd_get_data(host);
1244 wbsd_finish_data(host, data);
1247 spin_unlock(&host->lock);
1250 static void wbsd_tasklet_block(unsigned long param)
1252 struct wbsd_host *host = (struct wbsd_host *)param;
1253 struct mmc_data *data;
1255 spin_lock(&host->lock);
1257 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1259 data = wbsd_get_data(host);
1263 DBGF("CRC error\n");
1265 data->error = MMC_ERR_BADCRC;
1267 tasklet_schedule(&host->finish_tasklet);
1271 spin_unlock(&host->lock);
1275 * Interrupt handling
1278 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1280 struct wbsd_host *host = dev_id;
1283 isr = inb(host->base + WBSD_ISR);
1286 * Was it actually our hardware that caused the interrupt?
1288 if (isr == 0xff || isr == 0x00)
1294 * Schedule tasklets as needed.
1296 if (isr & WBSD_INT_CARD)
1297 tasklet_schedule(&host->card_tasklet);
1298 if (isr & WBSD_INT_FIFO_THRE)
1299 tasklet_schedule(&host->fifo_tasklet);
1300 if (isr & WBSD_INT_CRC)
1301 tasklet_hi_schedule(&host->crc_tasklet);
1302 if (isr & WBSD_INT_TIMEOUT)
1303 tasklet_hi_schedule(&host->timeout_tasklet);
1304 if (isr & WBSD_INT_BUSYEND)
1305 tasklet_hi_schedule(&host->block_tasklet);
1306 if (isr & WBSD_INT_TC)
1307 tasklet_schedule(&host->finish_tasklet);
1312 /*****************************************************************************\
1314 * Device initialisation and shutdown *
1316 \*****************************************************************************/
1319 * Allocate/free MMC structure.
1322 static int __devinit wbsd_alloc_mmc(struct device *dev)
1324 struct mmc_host *mmc;
1325 struct wbsd_host *host;
1328 * Allocate MMC structure.
1330 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1334 host = mmc_priv(mmc);
1340 * Set host parameters.
1342 mmc->ops = &wbsd_ops;
1343 mmc->f_min = 375000;
1344 mmc->f_max = 24000000;
1345 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1346 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1348 spin_lock_init(&host->lock);
1353 init_timer(&host->ignore_timer);
1354 host->ignore_timer.data = (unsigned long)host;
1355 host->ignore_timer.function = wbsd_reset_ignore;
1358 * Maximum number of segments. Worst case is one sector per segment
1359 * so this will be 64kB/512.
1361 mmc->max_hw_segs = 128;
1362 mmc->max_phys_segs = 128;
1365 * Maximum request size. Also limited by 64KiB buffer.
1367 mmc->max_req_size = 65536;
1370 * Maximum segment size. Could be one segment with the maximum number
1373 mmc->max_seg_size = mmc->max_req_size;
1376 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1377 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1379 mmc->max_blk_size = 4087;
1382 * Maximum block count. There is no real limit so the maximum
1383 * request size will be the only restriction.
1385 mmc->max_blk_count = mmc->max_req_size;
1387 dev_set_drvdata(dev, mmc);
1392 static void __devexit wbsd_free_mmc(struct device *dev)
1394 struct mmc_host *mmc;
1395 struct wbsd_host *host;
1397 mmc = dev_get_drvdata(dev);
1401 host = mmc_priv(mmc);
1402 BUG_ON(host == NULL);
1404 del_timer_sync(&host->ignore_timer);
1408 dev_set_drvdata(dev, NULL);
1412 * Scan for known chip id:s
1415 static int __devinit wbsd_scan(struct wbsd_host *host)
1421 * Iterate through all ports, all codes to
1422 * find hardware that is in our known list.
1424 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1425 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1428 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1431 host->config = config_ports[i];
1432 host->unlock_code = unlock_codes[j];
1434 wbsd_unlock_config(host);
1436 outb(WBSD_CONF_ID_HI, config_ports[i]);
1437 id = inb(config_ports[i] + 1) << 8;
1439 outb(WBSD_CONF_ID_LO, config_ports[i]);
1440 id |= inb(config_ports[i] + 1);
1442 wbsd_lock_config(host);
1444 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1445 if (id == valid_ids[k]) {
1453 DBG("Unknown hardware (id %x) found at %x\n",
1454 id, config_ports[i]);
1458 release_region(config_ports[i], 2);
1462 host->unlock_code = 0;
1468 * Allocate/free io port ranges
1471 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1476 if (!request_region(base, 8, DRIVER_NAME))
1484 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1487 release_region(host->base, 8);
1492 release_region(host->config, 2);
1498 * Allocate/free DMA port and buffer
1501 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1506 if (request_dma(dma, DRIVER_NAME))
1510 * We need to allocate a special buffer in
1511 * order for ISA to be able to DMA to it.
1513 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1514 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1515 if (!host->dma_buffer)
1519 * Translate the address to a physical address.
1521 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1522 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1525 * ISA DMA must be aligned on a 64k basis.
1527 if ((host->dma_addr & 0xffff) != 0)
1530 * ISA cannot access memory above 16 MB.
1532 else if (host->dma_addr >= 0x1000000)
1541 * If we've gotten here then there is some kind of alignment bug
1545 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1546 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1547 host->dma_addr = (dma_addr_t)NULL;
1549 kfree(host->dma_buffer);
1550 host->dma_buffer = NULL;
1556 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1557 "Falling back on FIFO.\n", dma);
1560 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1562 if (host->dma_addr) {
1563 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1564 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1566 kfree(host->dma_buffer);
1568 free_dma(host->dma);
1571 host->dma_buffer = NULL;
1572 host->dma_addr = (dma_addr_t)NULL;
1576 * Allocate/free IRQ.
1579 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1584 * Allocate interrupt.
1587 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1596 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1597 (unsigned long)host);
1598 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1599 (unsigned long)host);
1600 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1601 (unsigned long)host);
1602 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1603 (unsigned long)host);
1604 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1605 (unsigned long)host);
1606 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1607 (unsigned long)host);
1612 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1617 free_irq(host->irq, host);
1621 tasklet_kill(&host->card_tasklet);
1622 tasklet_kill(&host->fifo_tasklet);
1623 tasklet_kill(&host->crc_tasklet);
1624 tasklet_kill(&host->timeout_tasklet);
1625 tasklet_kill(&host->finish_tasklet);
1626 tasklet_kill(&host->block_tasklet);
1630 * Allocate all resources for the host.
1633 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1634 int base, int irq, int dma)
1639 * Allocate I/O ports.
1641 ret = wbsd_request_region(host, base);
1646 * Allocate interrupt.
1648 ret = wbsd_request_irq(host, irq);
1655 wbsd_request_dma(host, dma);
1661 * Release all resources for the host.
1664 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1666 wbsd_release_dma(host);
1667 wbsd_release_irq(host);
1668 wbsd_release_regions(host);
1672 * Configure the resources the chip should use.
1675 static void wbsd_chip_config(struct wbsd_host *host)
1677 wbsd_unlock_config(host);
1682 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1683 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1686 * Select SD/MMC function.
1688 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1691 * Set up card detection.
1693 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1698 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1699 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1701 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1704 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1707 * Enable and power up chip.
1709 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1710 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1712 wbsd_lock_config(host);
1716 * Check that configured resources are correct.
1719 static int wbsd_chip_validate(struct wbsd_host *host)
1723 wbsd_unlock_config(host);
1726 * Select SD/MMC function.
1728 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1731 * Read configuration.
1733 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1734 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1736 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1738 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1740 wbsd_lock_config(host);
1743 * Validate against given configuration.
1745 if (base != host->base)
1747 if (irq != host->irq)
1749 if ((dma != host->dma) && (host->dma != -1))
1756 * Powers down the SD function
1759 static void wbsd_chip_poweroff(struct wbsd_host *host)
1761 wbsd_unlock_config(host);
1763 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1764 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1766 wbsd_lock_config(host);
1769 /*****************************************************************************\
1771 * Devices setup and shutdown *
1773 \*****************************************************************************/
1775 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1778 struct wbsd_host *host = NULL;
1779 struct mmc_host *mmc = NULL;
1782 ret = wbsd_alloc_mmc(dev);
1786 mmc = dev_get_drvdata(dev);
1787 host = mmc_priv(mmc);
1790 * Scan for hardware.
1792 ret = wbsd_scan(host);
1794 if (pnp && (ret == -ENODEV)) {
1795 printk(KERN_WARNING DRIVER_NAME
1796 ": Unable to confirm device presence. You may "
1797 "experience lock-ups.\n");
1805 * Request resources.
1807 ret = wbsd_request_resources(host, base, irq, dma);
1809 wbsd_release_resources(host);
1815 * See if chip needs to be configured.
1818 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1819 printk(KERN_WARNING DRIVER_NAME
1820 ": PnP active but chip not configured! "
1821 "You probably have a buggy BIOS. "
1822 "Configuring chip manually.\n");
1823 wbsd_chip_config(host);
1826 wbsd_chip_config(host);
1829 * Power Management stuff. No idea how this works.
1834 wbsd_unlock_config(host);
1835 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1836 wbsd_lock_config(host);
1840 * Allow device to initialise itself properly.
1845 * Reset the chip into a known state.
1847 wbsd_init_device(host);
1851 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1852 if (host->chip_id != 0)
1853 printk(" id %x", (int)host->chip_id);
1854 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1856 printk(" dma %d", (int)host->dma);
1866 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1868 struct mmc_host *mmc = dev_get_drvdata(dev);
1869 struct wbsd_host *host;
1874 host = mmc_priv(mmc);
1876 mmc_remove_host(mmc);
1879 * Power down the SD/MMC function.
1882 wbsd_chip_poweroff(host);
1884 wbsd_release_resources(host);
1893 static int __devinit wbsd_probe(struct platform_device *dev)
1895 /* Use the module parameters for resources */
1896 return wbsd_init(&dev->dev, io, irq, dma, 0);
1899 static int __devexit wbsd_remove(struct platform_device *dev)
1901 wbsd_shutdown(&dev->dev, 0);
1912 static int __devinit
1913 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1918 * Get resources from PnP layer.
1920 io = pnp_port_start(pnpdev, 0);
1921 irq = pnp_irq(pnpdev, 0);
1922 if (pnp_dma_valid(pnpdev, 0))
1923 dma = pnp_dma(pnpdev, 0);
1927 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1929 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1932 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1934 wbsd_shutdown(&dev->dev, 1);
1937 #endif /* CONFIG_PNP */
1945 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1947 BUG_ON(host == NULL);
1949 return mmc_suspend_host(host->mmc, state);
1952 static int wbsd_resume(struct wbsd_host *host)
1954 BUG_ON(host == NULL);
1956 wbsd_init_device(host);
1958 return mmc_resume_host(host->mmc);
1961 static int wbsd_platform_suspend(struct platform_device *dev,
1964 struct mmc_host *mmc = platform_get_drvdata(dev);
1965 struct wbsd_host *host;
1971 DBGF("Suspending...\n");
1973 host = mmc_priv(mmc);
1975 ret = wbsd_suspend(host, state);
1979 wbsd_chip_poweroff(host);
1984 static int wbsd_platform_resume(struct platform_device *dev)
1986 struct mmc_host *mmc = platform_get_drvdata(dev);
1987 struct wbsd_host *host;
1992 DBGF("Resuming...\n");
1994 host = mmc_priv(mmc);
1996 wbsd_chip_config(host);
1999 * Allow device to initialise itself properly.
2003 return wbsd_resume(host);
2008 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2010 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2011 struct wbsd_host *host;
2016 DBGF("Suspending...\n");
2018 host = mmc_priv(mmc);
2020 return wbsd_suspend(host, state);
2023 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2025 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2026 struct wbsd_host *host;
2031 DBGF("Resuming...\n");
2033 host = mmc_priv(mmc);
2036 * See if chip needs to be configured.
2038 if (host->config != 0) {
2039 if (!wbsd_chip_validate(host)) {
2040 printk(KERN_WARNING DRIVER_NAME
2041 ": PnP active but chip not configured! "
2042 "You probably have a buggy BIOS. "
2043 "Configuring chip manually.\n");
2044 wbsd_chip_config(host);
2049 * Allow device to initialise itself properly.
2053 return wbsd_resume(host);
2056 #endif /* CONFIG_PNP */
2058 #else /* CONFIG_PM */
2060 #define wbsd_platform_suspend NULL
2061 #define wbsd_platform_resume NULL
2063 #define wbsd_pnp_suspend NULL
2064 #define wbsd_pnp_resume NULL
2066 #endif /* CONFIG_PM */
2068 static struct platform_device *wbsd_device;
2070 static struct platform_driver wbsd_driver = {
2071 .probe = wbsd_probe,
2072 .remove = __devexit_p(wbsd_remove),
2074 .suspend = wbsd_platform_suspend,
2075 .resume = wbsd_platform_resume,
2077 .name = DRIVER_NAME,
2083 static struct pnp_driver wbsd_pnp_driver = {
2084 .name = DRIVER_NAME,
2085 .id_table = pnp_dev_table,
2086 .probe = wbsd_pnp_probe,
2087 .remove = __devexit_p(wbsd_pnp_remove),
2089 .suspend = wbsd_pnp_suspend,
2090 .resume = wbsd_pnp_resume,
2093 #endif /* CONFIG_PNP */
2096 * Module loading/unloading
2099 static int __init wbsd_drv_init(void)
2103 printk(KERN_INFO DRIVER_NAME
2104 ": Winbond W83L51xD SD/MMC card interface driver, "
2105 DRIVER_VERSION "\n");
2106 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2111 result = pnp_register_driver(&wbsd_pnp_driver);
2115 #endif /* CONFIG_PNP */
2118 result = platform_driver_register(&wbsd_driver);
2122 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2124 platform_driver_unregister(&wbsd_driver);
2128 result = platform_device_add(wbsd_device);
2130 platform_device_put(wbsd_device);
2131 platform_driver_unregister(&wbsd_driver);
2139 static void __exit wbsd_drv_exit(void)
2144 pnp_unregister_driver(&wbsd_pnp_driver);
2146 #endif /* CONFIG_PNP */
2149 platform_device_unregister(wbsd_device);
2151 platform_driver_unregister(&wbsd_driver);
2157 module_init(wbsd_drv_init);
2158 module_exit(wbsd_drv_exit);
2160 module_param(nopnp, uint, 0444);
2162 module_param(io, uint, 0444);
2163 module_param(irq, uint, 0444);
2164 module_param(dma, int, 0444);
2166 MODULE_LICENSE("GPL");
2167 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2168 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2169 MODULE_VERSION(DRIVER_VERSION);
2172 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2174 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2175 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2176 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");