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/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.2"
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)
57 #ifdef CONFIG_MMC_DEBUG
58 void DBG_REG(int reg, u8 value)
62 printk(KERN_DEBUG "wbsd: Register %d: 0x%02X %3d '%c' ",
63 reg, (int)value, (int)value, (value < 0x20)?'.':value);
65 for (i = 7;i >= 0;i--)
76 #define DBG_REG(r, v) do {} while (0)
85 static const struct pnp_device_id pnp_dev_table[] = {
91 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
93 #endif /* CONFIG_PNP */
96 static unsigned int nopnp = 0;
98 static const unsigned int nopnp = 1;
100 static unsigned int io = 0x248;
101 static unsigned int irq = 6;
108 static inline void wbsd_unlock_config(struct wbsd_host* host)
110 BUG_ON(host->config == 0);
112 outb(host->unlock_code, host->config);
113 outb(host->unlock_code, host->config);
116 static inline void wbsd_lock_config(struct wbsd_host* host)
118 BUG_ON(host->config == 0);
120 outb(LOCK_CODE, host->config);
123 static inline void wbsd_write_config(struct wbsd_host* host, u8 reg, u8 value)
125 BUG_ON(host->config == 0);
127 outb(reg, host->config);
128 outb(value, host->config + 1);
131 static inline u8 wbsd_read_config(struct wbsd_host* host, u8 reg)
133 BUG_ON(host->config == 0);
135 outb(reg, host->config);
136 return inb(host->config + 1);
139 static inline void wbsd_write_index(struct wbsd_host* host, u8 index, u8 value)
141 outb(index, host->base + WBSD_IDXR);
142 outb(value, host->base + WBSD_DATAR);
145 static inline u8 wbsd_read_index(struct wbsd_host* host, u8 index)
147 outb(index, host->base + WBSD_IDXR);
148 return inb(host->base + WBSD_DATAR);
155 static void wbsd_init_device(struct wbsd_host* host)
160 * Reset chip (SD/MMC part) and fifo.
162 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
163 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
164 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
169 setup &= ~WBSD_DAT3_H;
170 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
171 host->flags &= ~WBSD_FIGNORE_DETECT;
174 * Read back default clock.
176 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
181 outb(WBSD_POWER_N, host->base + WBSD_CSR);
184 * Set maximum timeout.
186 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
189 * Test for card presence
191 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
192 host->flags |= WBSD_FCARD_PRESENT;
194 host->flags &= ~WBSD_FCARD_PRESENT;
197 * Enable interesting interrupts.
200 ier |= WBSD_EINT_CARD;
201 ier |= WBSD_EINT_FIFO_THRE;
202 ier |= WBSD_EINT_CCRC;
203 ier |= WBSD_EINT_TIMEOUT;
204 ier |= WBSD_EINT_CRC;
207 outb(ier, host->base + WBSD_EIR);
212 inb(host->base + WBSD_ISR);
215 static void wbsd_reset(struct wbsd_host* host)
219 printk(KERN_ERR DRIVER_NAME ": Resetting chip\n");
222 * Soft reset of chip (SD/MMC part).
224 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
225 setup |= WBSD_SOFT_RESET;
226 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
229 static void wbsd_request_end(struct wbsd_host* host, struct mmc_request* mrq)
231 unsigned long dmaflags;
233 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
238 * Release ISA DMA controller.
240 dmaflags = claim_dma_lock();
241 disable_dma(host->dma);
242 clear_dma_ff(host->dma);
243 release_dma_lock(dmaflags);
246 * Disable DMA on host.
248 wbsd_write_index(host, WBSD_IDX_DMA, 0);
254 * MMC layer might call back into the driver so first unlock.
256 spin_unlock(&host->lock);
257 mmc_request_done(host->mmc, mrq);
258 spin_lock(&host->lock);
262 * Scatter/gather functions
265 static inline void wbsd_init_sg(struct wbsd_host* host, struct mmc_data* data)
268 * Get info. about SG list from data structure.
270 host->cur_sg = data->sg;
271 host->num_sg = data->sg_len;
274 host->remain = host->cur_sg->length;
277 static inline int wbsd_next_sg(struct wbsd_host* host)
280 * Skip to next SG entry.
288 if (host->num_sg > 0)
291 host->remain = host->cur_sg->length;
297 static inline char* wbsd_kmap_sg(struct wbsd_host* host)
299 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
300 host->cur_sg->offset;
301 return host->mapped_sg;
304 static inline void wbsd_kunmap_sg(struct wbsd_host* host)
306 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
309 static inline void wbsd_sg_to_dma(struct wbsd_host* host, struct mmc_data* data)
311 unsigned int len, i, size;
312 struct scatterlist* sg;
313 char* dmabuf = host->dma_buffer;
322 * Just loop through all entries. Size might not
323 * be the entire list though so make sure that
324 * we do not transfer too much.
326 for (i = 0;i < len;i++)
328 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
329 if (size < sg[i].length)
330 memcpy(dmabuf, sgbuf, size);
332 memcpy(dmabuf, sgbuf, sg[i].length);
333 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
334 dmabuf += sg[i].length;
336 if (size < sg[i].length)
339 size -= sg[i].length;
346 * Check that we didn't get a request to transfer
347 * more data than can fit into the SG list.
355 static inline void wbsd_dma_to_sg(struct wbsd_host* host, struct mmc_data* data)
357 unsigned int len, i, size;
358 struct scatterlist* sg;
359 char* dmabuf = host->dma_buffer;
368 * Just loop through all entries. Size might not
369 * be the entire list though so make sure that
370 * we do not transfer too much.
372 for (i = 0;i < len;i++)
374 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
375 if (size < sg[i].length)
376 memcpy(sgbuf, dmabuf, size);
378 memcpy(sgbuf, dmabuf, sg[i].length);
379 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
380 dmabuf += sg[i].length;
382 if (size < sg[i].length)
385 size -= sg[i].length;
392 * Check that we didn't get a request to transfer
393 * more data than can fit into the SG list.
405 static inline void wbsd_get_short_reply(struct wbsd_host* host,
406 struct mmc_command* cmd)
409 * Correct response type?
411 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT)
413 cmd->error = MMC_ERR_INVALID;
418 wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
420 wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
422 wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
424 wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
426 wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
429 static inline void wbsd_get_long_reply(struct wbsd_host* host,
430 struct mmc_command* cmd)
435 * Correct response type?
437 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG)
439 cmd->error = MMC_ERR_INVALID;
443 for (i = 0;i < 4;i++)
446 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
448 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
450 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
452 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
456 static void wbsd_send_command(struct wbsd_host* host, struct mmc_command* cmd)
461 DBGF("Sending cmd (%x)\n", cmd->opcode);
464 * Clear accumulated ISR. The interrupt routine
465 * will fill this one with events that occur during
471 * Send the command (CRC calculated by host).
473 outb(cmd->opcode, host->base + WBSD_CMDR);
474 for (i = 3;i >= 0;i--)
475 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
477 cmd->error = MMC_ERR_NONE;
480 * Wait for the request to complete.
483 status = wbsd_read_index(host, WBSD_IDX_STATUS);
484 } while (status & WBSD_CARDTRAFFIC);
487 * Do we expect a reply?
489 if ((cmd->flags & MMC_RSP_MASK) != MMC_RSP_NONE)
497 if (isr & WBSD_INT_CARD)
498 cmd->error = MMC_ERR_TIMEOUT;
500 else if (isr & WBSD_INT_TIMEOUT)
501 cmd->error = MMC_ERR_TIMEOUT;
503 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
504 cmd->error = MMC_ERR_BADCRC;
508 if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_SHORT)
509 wbsd_get_short_reply(host, cmd);
511 wbsd_get_long_reply(host, cmd);
515 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
522 static void wbsd_empty_fifo(struct wbsd_host* host)
524 struct mmc_data* data = host->mrq->cmd->data;
529 * Handle excessive data.
531 if (data->bytes_xfered == host->size)
534 buffer = wbsd_kmap_sg(host) + host->offset;
537 * Drain the fifo. This has a tendency to loop longer
538 * than the FIFO length (usually one block).
540 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY))
543 * The size field in the FSR is broken so we have to
546 if (fsr & WBSD_FIFO_FULL)
548 else if (fsr & WBSD_FIFO_FUTHRE)
553 for (i = 0;i < fifo;i++)
555 *buffer = inb(host->base + WBSD_DFR);
560 data->bytes_xfered++;
565 if (data->bytes_xfered == host->size)
567 wbsd_kunmap_sg(host);
572 * End of scatter list entry?
574 if (host->remain == 0)
576 wbsd_kunmap_sg(host);
579 * Get next entry. Check if last.
581 if (!wbsd_next_sg(host))
584 * We should never reach this point.
585 * It means that we're trying to
586 * transfer more blocks than can fit
587 * into the scatter list.
591 host->size = data->bytes_xfered;
596 buffer = wbsd_kmap_sg(host);
601 wbsd_kunmap_sg(host);
604 * This is a very dirty hack to solve a
605 * hardware problem. The chip doesn't trigger
606 * FIFO threshold interrupts properly.
608 if ((host->size - data->bytes_xfered) < 16)
609 tasklet_schedule(&host->fifo_tasklet);
612 static void wbsd_fill_fifo(struct wbsd_host* host)
614 struct mmc_data* data = host->mrq->cmd->data;
619 * Check that we aren't being called after the
620 * entire buffer has been transfered.
622 if (data->bytes_xfered == host->size)
625 buffer = wbsd_kmap_sg(host) + host->offset;
628 * Fill the fifo. This has a tendency to loop longer
629 * than the FIFO length (usually one block).
631 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL))
634 * The size field in the FSR is broken so we have to
637 if (fsr & WBSD_FIFO_EMPTY)
639 else if (fsr & WBSD_FIFO_EMTHRE)
644 for (i = 16;i > fifo;i--)
646 outb(*buffer, host->base + WBSD_DFR);
651 data->bytes_xfered++;
656 if (data->bytes_xfered == host->size)
658 wbsd_kunmap_sg(host);
663 * End of scatter list entry?
665 if (host->remain == 0)
667 wbsd_kunmap_sg(host);
670 * Get next entry. Check if last.
672 if (!wbsd_next_sg(host))
675 * We should never reach this point.
676 * It means that we're trying to
677 * transfer more blocks than can fit
678 * into the scatter list.
682 host->size = data->bytes_xfered;
687 buffer = wbsd_kmap_sg(host);
692 wbsd_kunmap_sg(host);
695 * The controller stops sending interrupts for
696 * 'FIFO empty' under certain conditions. So we
697 * need to be a bit more pro-active.
699 tasklet_schedule(&host->fifo_tasklet);
702 static void wbsd_prepare_data(struct wbsd_host* host, struct mmc_data* data)
706 unsigned long dmaflags;
708 DBGF("blksz %04x blks %04x flags %08x\n",
709 1 << data->blksz_bits, data->blocks, data->flags);
710 DBGF("tsac %d ms nsac %d clk\n",
711 data->timeout_ns / 1000000, data->timeout_clks);
716 host->size = data->blocks << data->blksz_bits;
719 * Check timeout values for overflow.
720 * (Yes, some cards cause this value to overflow).
722 if (data->timeout_ns > 127000000)
723 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
725 wbsd_write_index(host, WBSD_IDX_TAAC, data->timeout_ns/1000000);
727 if (data->timeout_clks > 255)
728 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
730 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
733 * Inform the chip of how large blocks will be
734 * sent. It needs this to determine when to
737 * Space for CRC must be included in the size.
739 blksize = (1 << data->blksz_bits) + 2;
741 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
742 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
745 * Clear the FIFO. This is needed even for DMA
746 * transfers since the chip still uses the FIFO
749 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
750 setup |= WBSD_FIFO_RESET;
751 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
759 * The buffer for DMA is only 64 kB.
761 BUG_ON(host->size > 0x10000);
762 if (host->size > 0x10000)
764 data->error = MMC_ERR_INVALID;
769 * Transfer data from the SG list to
772 if (data->flags & MMC_DATA_WRITE)
773 wbsd_sg_to_dma(host, data);
776 * Initialise the ISA DMA controller.
778 dmaflags = claim_dma_lock();
779 disable_dma(host->dma);
780 clear_dma_ff(host->dma);
781 if (data->flags & MMC_DATA_READ)
782 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
784 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
785 set_dma_addr(host->dma, host->dma_addr);
786 set_dma_count(host->dma, host->size);
788 enable_dma(host->dma);
789 release_dma_lock(dmaflags);
792 * Enable DMA on the host.
794 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
799 * This flag is used to keep printk
800 * output to a minimum.
805 * Initialise the SG list.
807 wbsd_init_sg(host, data);
812 wbsd_write_index(host, WBSD_IDX_DMA, 0);
815 * Set up FIFO threshold levels (and fill
816 * buffer if doing a write).
818 if (data->flags & MMC_DATA_READ)
820 wbsd_write_index(host, WBSD_IDX_FIFOEN,
821 WBSD_FIFOEN_FULL | 8);
825 wbsd_write_index(host, WBSD_IDX_FIFOEN,
826 WBSD_FIFOEN_EMPTY | 8);
827 wbsd_fill_fifo(host);
831 data->error = MMC_ERR_NONE;
834 static void wbsd_finish_data(struct wbsd_host* host, struct mmc_data* data)
836 unsigned long dmaflags;
840 WARN_ON(host->mrq == NULL);
843 * Send a stop command if needed.
846 wbsd_send_command(host, data->stop);
849 * Wait for the controller to leave data
854 status = wbsd_read_index(host, WBSD_IDX_STATUS);
855 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
863 * Disable DMA on the host.
865 wbsd_write_index(host, WBSD_IDX_DMA, 0);
868 * Turn of ISA DMA controller.
870 dmaflags = claim_dma_lock();
871 disable_dma(host->dma);
872 clear_dma_ff(host->dma);
873 count = get_dma_residue(host->dma);
874 release_dma_lock(dmaflags);
881 printk(KERN_ERR DRIVER_NAME ": Incomplete DMA "
882 "transfer. %d bytes left.\n", count);
884 data->error = MMC_ERR_FAILED;
889 * Transfer data from DMA buffer to
892 if (data->flags & MMC_DATA_READ)
893 wbsd_dma_to_sg(host, data);
895 data->bytes_xfered = host->size;
899 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
901 wbsd_request_end(host, host->mrq);
904 /*****************************************************************************\
906 * MMC layer callbacks *
908 \*****************************************************************************/
910 static void wbsd_request(struct mmc_host* mmc, struct mmc_request* mrq)
912 struct wbsd_host* host = mmc_priv(mmc);
913 struct mmc_command* cmd;
916 * Disable tasklets to avoid a deadlock.
918 spin_lock_bh(&host->lock);
920 BUG_ON(host->mrq != NULL);
927 * If there is no card in the slot then
928 * timeout immediatly.
930 if (!(host->flags & WBSD_FCARD_PRESENT))
932 cmd->error = MMC_ERR_TIMEOUT;
937 * Does the request include data?
941 wbsd_prepare_data(host, cmd->data);
943 if (cmd->data->error != MMC_ERR_NONE)
947 wbsd_send_command(host, cmd);
950 * If this is a data transfer the request
951 * will be finished after the data has
954 if (cmd->data && (cmd->error == MMC_ERR_NONE))
957 * Dirty fix for hardware bug.
960 tasklet_schedule(&host->fifo_tasklet);
962 spin_unlock_bh(&host->lock);
968 wbsd_request_end(host, mrq);
970 spin_unlock_bh(&host->lock);
973 static void wbsd_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
975 struct wbsd_host* host = mmc_priv(mmc);
978 DBGF("clock %uHz busmode %u powermode %u Vdd %u\n",
979 ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
981 spin_lock_bh(&host->lock);
984 * Reset the chip on each power off.
985 * Should clear out any weird states.
987 if (ios->power_mode == MMC_POWER_OFF)
988 wbsd_init_device(host);
990 if (ios->clock >= 24000000)
992 else if (ios->clock >= 16000000)
994 else if (ios->clock >= 12000000)
1000 * Only write to the clock register when
1001 * there is an actual change.
1003 if (clk != host->clk)
1005 wbsd_write_index(host, WBSD_IDX_CLK, clk);
1012 if (ios->power_mode != MMC_POWER_OFF)
1014 pwr = inb(host->base + WBSD_CSR);
1015 pwr &= ~WBSD_POWER_N;
1016 outb(pwr, host->base + WBSD_CSR);
1020 * MMC cards need to have pin 1 high during init.
1021 * Init time corresponds rather nicely with the bus mode.
1022 * It wreaks havoc with the card detection though so
1023 * that needs to be disabed.
1025 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
1026 if ((ios->power_mode == MMC_POWER_ON) &&
1027 (ios->bus_mode == MMC_BUSMODE_OPENDRAIN))
1029 setup |= WBSD_DAT3_H;
1030 host->flags |= WBSD_FIGNORE_DETECT;
1034 setup &= ~WBSD_DAT3_H;
1035 host->flags &= ~WBSD_FIGNORE_DETECT;
1037 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1039 spin_unlock_bh(&host->lock);
1042 static struct mmc_host_ops wbsd_ops = {
1043 .request = wbsd_request,
1044 .set_ios = wbsd_set_ios,
1047 /*****************************************************************************\
1049 * Interrupt handling *
1051 \*****************************************************************************/
1054 * Helper function for card detection
1056 static void wbsd_detect_card(unsigned long data)
1058 struct wbsd_host *host = (struct wbsd_host*)data;
1060 BUG_ON(host == NULL);
1062 DBG("Executing card detection\n");
1064 mmc_detect_change(host->mmc);
1071 inline static struct mmc_data* wbsd_get_data(struct wbsd_host* host)
1073 WARN_ON(!host->mrq);
1077 WARN_ON(!host->mrq->cmd);
1078 if (!host->mrq->cmd)
1081 WARN_ON(!host->mrq->cmd->data);
1082 if (!host->mrq->cmd->data)
1085 return host->mrq->cmd->data;
1088 static void wbsd_tasklet_card(unsigned long param)
1090 struct wbsd_host* host = (struct wbsd_host*)param;
1093 spin_lock(&host->lock);
1095 if (host->flags & WBSD_FIGNORE_DETECT)
1097 spin_unlock(&host->lock);
1101 csr = inb(host->base + WBSD_CSR);
1102 WARN_ON(csr == 0xff);
1104 if (csr & WBSD_CARDPRESENT)
1106 if (!(host->flags & WBSD_FCARD_PRESENT))
1108 DBG("Card inserted\n");
1109 host->flags |= WBSD_FCARD_PRESENT;
1112 * Delay card detection to allow electrical connections
1115 mod_timer(&host->timer, jiffies + HZ/2);
1118 spin_unlock(&host->lock);
1120 else if (host->flags & WBSD_FCARD_PRESENT)
1122 DBG("Card removed\n");
1123 host->flags &= ~WBSD_FCARD_PRESENT;
1127 printk(KERN_ERR DRIVER_NAME
1128 ": Card removed during transfer!\n");
1131 host->mrq->cmd->error = MMC_ERR_FAILED;
1132 tasklet_schedule(&host->finish_tasklet);
1136 * Unlock first since we might get a call back.
1138 spin_unlock(&host->lock);
1140 mmc_detect_change(host->mmc);
1144 static void wbsd_tasklet_fifo(unsigned long param)
1146 struct wbsd_host* host = (struct wbsd_host*)param;
1147 struct mmc_data* data;
1149 spin_lock(&host->lock);
1154 data = wbsd_get_data(host);
1158 if (data->flags & MMC_DATA_WRITE)
1159 wbsd_fill_fifo(host);
1161 wbsd_empty_fifo(host);
1166 if (host->size == data->bytes_xfered)
1168 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1169 tasklet_schedule(&host->finish_tasklet);
1173 spin_unlock(&host->lock);
1176 static void wbsd_tasklet_crc(unsigned long param)
1178 struct wbsd_host* host = (struct wbsd_host*)param;
1179 struct mmc_data* data;
1181 spin_lock(&host->lock);
1186 data = wbsd_get_data(host);
1190 DBGF("CRC error\n");
1192 data->error = MMC_ERR_BADCRC;
1194 tasklet_schedule(&host->finish_tasklet);
1197 spin_unlock(&host->lock);
1200 static void wbsd_tasklet_timeout(unsigned long param)
1202 struct wbsd_host* host = (struct wbsd_host*)param;
1203 struct mmc_data* data;
1205 spin_lock(&host->lock);
1210 data = wbsd_get_data(host);
1216 data->error = MMC_ERR_TIMEOUT;
1218 tasklet_schedule(&host->finish_tasklet);
1221 spin_unlock(&host->lock);
1224 static void wbsd_tasklet_finish(unsigned long param)
1226 struct wbsd_host* host = (struct wbsd_host*)param;
1227 struct mmc_data* data;
1229 spin_lock(&host->lock);
1231 WARN_ON(!host->mrq);
1235 data = wbsd_get_data(host);
1239 wbsd_finish_data(host, data);
1242 spin_unlock(&host->lock);
1245 static void wbsd_tasklet_block(unsigned long param)
1247 struct wbsd_host* host = (struct wbsd_host*)param;
1248 struct mmc_data* data;
1250 spin_lock(&host->lock);
1252 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1255 data = wbsd_get_data(host);
1259 DBGF("CRC error\n");
1261 data->error = MMC_ERR_BADCRC;
1263 tasklet_schedule(&host->finish_tasklet);
1267 spin_unlock(&host->lock);
1271 * Interrupt handling
1274 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1276 struct wbsd_host* host = dev_id;
1279 isr = inb(host->base + WBSD_ISR);
1282 * Was it actually our hardware that caused the interrupt?
1284 if (isr == 0xff || isr == 0x00)
1290 * Schedule tasklets as needed.
1292 if (isr & WBSD_INT_CARD)
1293 tasklet_schedule(&host->card_tasklet);
1294 if (isr & WBSD_INT_FIFO_THRE)
1295 tasklet_schedule(&host->fifo_tasklet);
1296 if (isr & WBSD_INT_CRC)
1297 tasklet_hi_schedule(&host->crc_tasklet);
1298 if (isr & WBSD_INT_TIMEOUT)
1299 tasklet_hi_schedule(&host->timeout_tasklet);
1300 if (isr & WBSD_INT_BUSYEND)
1301 tasklet_hi_schedule(&host->block_tasklet);
1302 if (isr & WBSD_INT_TC)
1303 tasklet_schedule(&host->finish_tasklet);
1308 /*****************************************************************************\
1310 * Device initialisation and shutdown *
1312 \*****************************************************************************/
1315 * Allocate/free MMC structure.
1318 static int __devinit wbsd_alloc_mmc(struct device* dev)
1320 struct mmc_host* mmc;
1321 struct wbsd_host* host;
1324 * Allocate MMC structure.
1326 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1330 host = mmc_priv(mmc);
1336 * Set host parameters.
1338 mmc->ops = &wbsd_ops;
1339 mmc->f_min = 375000;
1340 mmc->f_max = 24000000;
1341 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1343 spin_lock_init(&host->lock);
1346 * Set up detection timer
1348 init_timer(&host->timer);
1349 host->timer.data = (unsigned long)host;
1350 host->timer.function = wbsd_detect_card;
1353 * Maximum number of segments. Worst case is one sector per segment
1354 * so this will be 64kB/512.
1356 mmc->max_hw_segs = 128;
1357 mmc->max_phys_segs = 128;
1360 * Maximum number of sectors in one transfer. Also limited by 64kB
1363 mmc->max_sectors = 128;
1366 * Maximum segment size. Could be one segment with the maximum number
1369 mmc->max_seg_size = mmc->max_sectors * 512;
1371 dev_set_drvdata(dev, mmc);
1376 static void __devexit wbsd_free_mmc(struct device* dev)
1378 struct mmc_host* mmc;
1379 struct wbsd_host* host;
1381 mmc = dev_get_drvdata(dev);
1385 host = mmc_priv(mmc);
1386 BUG_ON(host == NULL);
1388 del_timer_sync(&host->timer);
1392 dev_set_drvdata(dev, NULL);
1396 * Scan for known chip id:s
1399 static int __devinit wbsd_scan(struct wbsd_host* host)
1405 * Iterate through all ports, all codes to
1406 * find hardware that is in our known list.
1408 for (i = 0;i < sizeof(config_ports)/sizeof(int);i++)
1410 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1413 for (j = 0;j < sizeof(unlock_codes)/sizeof(int);j++)
1417 outb(unlock_codes[j], config_ports[i]);
1418 outb(unlock_codes[j], config_ports[i]);
1420 outb(WBSD_CONF_ID_HI, config_ports[i]);
1421 id = inb(config_ports[i] + 1) << 8;
1423 outb(WBSD_CONF_ID_LO, config_ports[i]);
1424 id |= inb(config_ports[i] + 1);
1426 for (k = 0;k < sizeof(valid_ids)/sizeof(int);k++)
1428 if (id == valid_ids[k])
1431 host->config = config_ports[i];
1432 host->unlock_code = unlock_codes[i];
1440 DBG("Unknown hardware (id %x) found at %x\n",
1441 id, config_ports[i]);
1444 outb(LOCK_CODE, config_ports[i]);
1447 release_region(config_ports[i], 2);
1454 * Allocate/free io port ranges
1457 static int __devinit wbsd_request_region(struct wbsd_host* host, int base)
1462 if (!request_region(base, 8, DRIVER_NAME))
1470 static void __devexit wbsd_release_regions(struct wbsd_host* host)
1473 release_region(host->base, 8);
1478 release_region(host->config, 2);
1484 * Allocate/free DMA port and buffer
1487 static void __devinit wbsd_request_dma(struct wbsd_host* host, int dma)
1492 if (request_dma(dma, DRIVER_NAME))
1496 * We need to allocate a special buffer in
1497 * order for ISA to be able to DMA to it.
1499 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1500 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1501 if (!host->dma_buffer)
1505 * Translate the address to a physical address.
1507 host->dma_addr = dma_map_single(host->mmc->dev, host->dma_buffer,
1508 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1511 * ISA DMA must be aligned on a 64k basis.
1513 if ((host->dma_addr & 0xffff) != 0)
1516 * ISA cannot access memory above 16 MB.
1518 else if (host->dma_addr >= 0x1000000)
1527 * If we've gotten here then there is some kind of alignment bug
1531 dma_unmap_single(host->mmc->dev, host->dma_addr, WBSD_DMA_SIZE,
1533 host->dma_addr = (dma_addr_t)NULL;
1535 kfree(host->dma_buffer);
1536 host->dma_buffer = NULL;
1542 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1543 "Falling back on FIFO.\n", dma);
1546 static void __devexit wbsd_release_dma(struct wbsd_host* host)
1549 dma_unmap_single(host->mmc->dev, host->dma_addr, WBSD_DMA_SIZE,
1551 if (host->dma_buffer)
1552 kfree(host->dma_buffer);
1554 free_dma(host->dma);
1557 host->dma_buffer = NULL;
1558 host->dma_addr = (dma_addr_t)NULL;
1562 * Allocate/free IRQ.
1565 static int __devinit wbsd_request_irq(struct wbsd_host* host, int irq)
1570 * Allocate interrupt.
1573 ret = request_irq(irq, wbsd_irq, SA_SHIRQ, DRIVER_NAME, host);
1582 tasklet_init(&host->card_tasklet, wbsd_tasklet_card, (unsigned long)host);
1583 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo, (unsigned long)host);
1584 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc, (unsigned long)host);
1585 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout, (unsigned long)host);
1586 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish, (unsigned long)host);
1587 tasklet_init(&host->block_tasklet, wbsd_tasklet_block, (unsigned long)host);
1592 static void __devexit wbsd_release_irq(struct wbsd_host* host)
1597 free_irq(host->irq, host);
1601 tasklet_kill(&host->card_tasklet);
1602 tasklet_kill(&host->fifo_tasklet);
1603 tasklet_kill(&host->crc_tasklet);
1604 tasklet_kill(&host->timeout_tasklet);
1605 tasklet_kill(&host->finish_tasklet);
1606 tasklet_kill(&host->block_tasklet);
1610 * Allocate all resources for the host.
1613 static int __devinit wbsd_request_resources(struct wbsd_host* host,
1614 int base, int irq, int dma)
1619 * Allocate I/O ports.
1621 ret = wbsd_request_region(host, base);
1626 * Allocate interrupt.
1628 ret = wbsd_request_irq(host, irq);
1635 wbsd_request_dma(host, dma);
1641 * Release all resources for the host.
1644 static void __devexit wbsd_release_resources(struct wbsd_host* host)
1646 wbsd_release_dma(host);
1647 wbsd_release_irq(host);
1648 wbsd_release_regions(host);
1652 * Configure the resources the chip should use.
1655 static void __devinit wbsd_chip_config(struct wbsd_host* host)
1660 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1661 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1664 * Select SD/MMC function.
1666 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1669 * Set up card detection.
1671 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1676 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1677 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1679 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1682 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1685 * Enable and power up chip.
1687 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1688 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1692 * Check that configured resources are correct.
1695 static int __devinit wbsd_chip_validate(struct wbsd_host* host)
1700 * Select SD/MMC function.
1702 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1705 * Read configuration.
1707 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1708 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1710 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1712 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1715 * Validate against given configuration.
1717 if (base != host->base)
1719 if (irq != host->irq)
1721 if ((dma != host->dma) && (host->dma != -1))
1727 /*****************************************************************************\
1729 * Devices setup and shutdown *
1731 \*****************************************************************************/
1733 static int __devinit wbsd_init(struct device* dev, int base, int irq, int dma,
1736 struct wbsd_host* host = NULL;
1737 struct mmc_host* mmc = NULL;
1740 ret = wbsd_alloc_mmc(dev);
1744 mmc = dev_get_drvdata(dev);
1745 host = mmc_priv(mmc);
1748 * Scan for hardware.
1750 ret = wbsd_scan(host);
1753 if (pnp && (ret == -ENODEV))
1755 printk(KERN_WARNING DRIVER_NAME
1756 ": Unable to confirm device presence. You may "
1757 "experience lock-ups.\n");
1767 * Request resources.
1769 ret = wbsd_request_resources(host, io, irq, dma);
1772 wbsd_release_resources(host);
1778 * See if chip needs to be configured.
1780 if (pnp && (host->config != 0))
1782 if (!wbsd_chip_validate(host))
1784 printk(KERN_WARNING DRIVER_NAME
1785 ": PnP active but chip not configured! "
1786 "You probably have a buggy BIOS. "
1787 "Configuring chip manually.\n");
1788 wbsd_chip_config(host);
1792 wbsd_chip_config(host);
1795 * Power Management stuff. No idea how this works.
1800 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1803 * Allow device to initialise itself properly.
1808 * Reset the chip into a known state.
1810 wbsd_init_device(host);
1814 printk(KERN_INFO "%s: W83L51xD", mmc->host_name);
1815 if (host->chip_id != 0)
1816 printk(" id %x", (int)host->chip_id);
1817 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1819 printk(" dma %d", (int)host->dma);
1829 static void __devexit wbsd_shutdown(struct device* dev, int pnp)
1831 struct mmc_host* mmc = dev_get_drvdata(dev);
1832 struct wbsd_host* host;
1837 host = mmc_priv(mmc);
1839 mmc_remove_host(mmc);
1844 * Power down the SD/MMC function.
1846 wbsd_unlock_config(host);
1847 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1848 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1849 wbsd_lock_config(host);
1852 wbsd_release_resources(host);
1861 static int __devinit wbsd_probe(struct device* dev)
1863 return wbsd_init(dev, io, irq, dma, 0);
1866 static int __devexit wbsd_remove(struct device* dev)
1868 wbsd_shutdown(dev, 0);
1879 static int __devinit
1880 wbsd_pnp_probe(struct pnp_dev * pnpdev, const struct pnp_device_id *dev_id)
1885 * Get resources from PnP layer.
1887 io = pnp_port_start(pnpdev, 0);
1888 irq = pnp_irq(pnpdev, 0);
1889 if (pnp_dma_valid(pnpdev, 0))
1890 dma = pnp_dma(pnpdev, 0);
1894 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1896 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1899 static void __devexit wbsd_pnp_remove(struct pnp_dev * dev)
1901 wbsd_shutdown(&dev->dev, 1);
1904 #endif /* CONFIG_PNP */
1911 static int wbsd_suspend(struct device *dev, pm_message_t state, u32 level)
1913 DBGF("Not yet supported\n");
1918 static int wbsd_resume(struct device *dev, u32 level)
1920 DBGF("Not yet supported\n");
1925 #define wbsd_suspend NULL
1926 #define wbsd_resume NULL
1929 static struct platform_device *wbsd_device;
1931 static struct device_driver wbsd_driver = {
1932 .name = DRIVER_NAME,
1933 .bus = &platform_bus_type,
1934 .probe = wbsd_probe,
1935 .remove = wbsd_remove,
1937 .suspend = wbsd_suspend,
1938 .resume = wbsd_resume,
1943 static struct pnp_driver wbsd_pnp_driver = {
1944 .name = DRIVER_NAME,
1945 .id_table = pnp_dev_table,
1946 .probe = wbsd_pnp_probe,
1947 .remove = wbsd_pnp_remove,
1950 #endif /* CONFIG_PNP */
1953 * Module loading/unloading
1956 static int __init wbsd_drv_init(void)
1960 printk(KERN_INFO DRIVER_NAME
1961 ": Winbond W83L51xD SD/MMC card interface driver, "
1962 DRIVER_VERSION "\n");
1963 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1969 result = pnp_register_driver(&wbsd_pnp_driver);
1974 #endif /* CONFIG_PNP */
1978 result = driver_register(&wbsd_driver);
1982 wbsd_device = platform_device_register_simple(DRIVER_NAME, -1,
1984 if (IS_ERR(wbsd_device))
1985 return PTR_ERR(wbsd_device);
1991 static void __exit wbsd_drv_exit(void)
1996 pnp_unregister_driver(&wbsd_pnp_driver);
1998 #endif /* CONFIG_PNP */
2002 platform_device_unregister(wbsd_device);
2004 driver_unregister(&wbsd_driver);
2010 module_init(wbsd_drv_init);
2011 module_exit(wbsd_drv_exit);
2013 module_param(nopnp, uint, 0444);
2015 module_param(io, uint, 0444);
2016 module_param(irq, uint, 0444);
2017 module_param(dma, int, 0444);
2019 MODULE_LICENSE("GPL");
2020 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2021 MODULE_VERSION(DRIVER_VERSION);
2024 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2026 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2027 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2028 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");