2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2007 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>
39 #include <asm/scatterlist.h>
43 #define DRIVER_NAME "wbsd"
46 pr_debug(DRIVER_NAME ": " x)
47 #define DBGF(f, x...) \
48 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
56 static const struct pnp_device_id pnp_dev_table[] = {
62 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
64 #endif /* CONFIG_PNP */
66 static const int config_ports[] = { 0x2E, 0x4E };
67 static const int unlock_codes[] = { 0x83, 0x87 };
69 static const int valid_ids[] = {
74 static unsigned int nopnp = 0;
76 static const unsigned int nopnp = 1;
78 static unsigned int io = 0x248;
79 static unsigned int irq = 6;
86 static inline void wbsd_unlock_config(struct wbsd_host *host)
88 BUG_ON(host->config == 0);
90 outb(host->unlock_code, host->config);
91 outb(host->unlock_code, host->config);
94 static inline void wbsd_lock_config(struct wbsd_host *host)
96 BUG_ON(host->config == 0);
98 outb(LOCK_CODE, host->config);
101 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
103 BUG_ON(host->config == 0);
105 outb(reg, host->config);
106 outb(value, host->config + 1);
109 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
111 BUG_ON(host->config == 0);
113 outb(reg, host->config);
114 return inb(host->config + 1);
117 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
119 outb(index, host->base + WBSD_IDXR);
120 outb(value, host->base + WBSD_DATAR);
123 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
125 outb(index, host->base + WBSD_IDXR);
126 return inb(host->base + WBSD_DATAR);
133 static void wbsd_init_device(struct wbsd_host *host)
138 * Reset chip (SD/MMC part) and fifo.
140 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
141 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
142 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
147 setup &= ~WBSD_DAT3_H;
148 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
149 host->flags &= ~WBSD_FIGNORE_DETECT;
152 * Read back default clock.
154 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
159 outb(WBSD_POWER_N, host->base + WBSD_CSR);
162 * Set maximum timeout.
164 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
167 * Test for card presence
169 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
170 host->flags |= WBSD_FCARD_PRESENT;
172 host->flags &= ~WBSD_FCARD_PRESENT;
175 * Enable interesting interrupts.
178 ier |= WBSD_EINT_CARD;
179 ier |= WBSD_EINT_FIFO_THRE;
180 ier |= WBSD_EINT_CRC;
181 ier |= WBSD_EINT_TIMEOUT;
184 outb(ier, host->base + WBSD_EIR);
189 inb(host->base + WBSD_ISR);
192 static void wbsd_reset(struct wbsd_host *host)
196 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
199 * Soft reset of chip (SD/MMC part).
201 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
202 setup |= WBSD_SOFT_RESET;
203 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
206 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
208 unsigned long dmaflags;
210 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
212 if (host->dma >= 0) {
214 * Release ISA DMA controller.
216 dmaflags = claim_dma_lock();
217 disable_dma(host->dma);
218 clear_dma_ff(host->dma);
219 release_dma_lock(dmaflags);
222 * Disable DMA on host.
224 wbsd_write_index(host, WBSD_IDX_DMA, 0);
230 * MMC layer might call back into the driver so first unlock.
232 spin_unlock(&host->lock);
233 mmc_request_done(host->mmc, mrq);
234 spin_lock(&host->lock);
238 * Scatter/gather functions
241 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
244 * Get info. about SG list from data structure.
246 host->cur_sg = data->sg;
247 host->num_sg = data->sg_len;
250 host->remain = host->cur_sg->length;
253 static inline int wbsd_next_sg(struct wbsd_host *host)
256 * Skip to next SG entry.
264 if (host->num_sg > 0) {
266 host->remain = host->cur_sg->length;
272 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
274 return page_address(host->cur_sg->page) + host->cur_sg->offset;
277 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
280 struct scatterlist *sg;
281 char *dmabuf = host->dma_buffer;
287 for (i = 0; i < len; i++) {
288 sgbuf = page_address(sg[i].page) + sg[i].offset;
289 memcpy(dmabuf, sgbuf, sg[i].length);
290 dmabuf += sg[i].length;
294 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
297 struct scatterlist *sg;
298 char *dmabuf = host->dma_buffer;
304 for (i = 0; i < len; i++) {
305 sgbuf = page_address(sg[i].page) + sg[i].offset;
306 memcpy(sgbuf, dmabuf, sg[i].length);
307 dmabuf += sg[i].length;
315 static inline void wbsd_get_short_reply(struct wbsd_host *host,
316 struct mmc_command *cmd)
319 * Correct response type?
321 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
322 cmd->error = MMC_ERR_INVALID;
326 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
327 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
328 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
329 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
330 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
333 static inline void wbsd_get_long_reply(struct wbsd_host *host,
334 struct mmc_command *cmd)
339 * Correct response type?
341 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
342 cmd->error = MMC_ERR_INVALID;
346 for (i = 0; i < 4; i++) {
348 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
350 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
352 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
354 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
358 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
363 DBGF("Sending cmd (%x)\n", cmd->opcode);
366 * Clear accumulated ISR. The interrupt routine
367 * will fill this one with events that occur during
373 * Send the command (CRC calculated by host).
375 outb(cmd->opcode, host->base + WBSD_CMDR);
376 for (i = 3; i >= 0; i--)
377 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
379 cmd->error = MMC_ERR_NONE;
382 * Wait for the request to complete.
385 status = wbsd_read_index(host, WBSD_IDX_STATUS);
386 } while (status & WBSD_CARDTRAFFIC);
389 * Do we expect a reply?
391 if (cmd->flags & MMC_RSP_PRESENT) {
398 if (isr & WBSD_INT_CARD)
399 cmd->error = MMC_ERR_TIMEOUT;
401 else if (isr & WBSD_INT_TIMEOUT)
402 cmd->error = MMC_ERR_TIMEOUT;
404 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
405 cmd->error = MMC_ERR_BADCRC;
408 if (cmd->flags & MMC_RSP_136)
409 wbsd_get_long_reply(host, cmd);
411 wbsd_get_short_reply(host, cmd);
415 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
422 static void wbsd_empty_fifo(struct wbsd_host *host)
424 struct mmc_data *data = host->mrq->cmd->data;
429 * Handle excessive data.
431 if (host->num_sg == 0)
434 buffer = wbsd_sg_to_buffer(host) + host->offset;
437 * Drain the fifo. This has a tendency to loop longer
438 * than the FIFO length (usually one block).
440 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
442 * The size field in the FSR is broken so we have to
445 if (fsr & WBSD_FIFO_FULL)
447 else if (fsr & WBSD_FIFO_FUTHRE)
452 for (i = 0; i < fifo; i++) {
453 *buffer = inb(host->base + WBSD_DFR);
458 data->bytes_xfered++;
461 * End of scatter list entry?
463 if (host->remain == 0) {
465 * Get next entry. Check if last.
467 if (!wbsd_next_sg(host))
470 buffer = wbsd_sg_to_buffer(host);
476 * This is a very dirty hack to solve a
477 * hardware problem. The chip doesn't trigger
478 * FIFO threshold interrupts properly.
480 if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
481 tasklet_schedule(&host->fifo_tasklet);
484 static void wbsd_fill_fifo(struct wbsd_host *host)
486 struct mmc_data *data = host->mrq->cmd->data;
491 * Check that we aren't being called after the
492 * entire buffer has been transfered.
494 if (host->num_sg == 0)
497 buffer = wbsd_sg_to_buffer(host) + host->offset;
500 * Fill the fifo. This has a tendency to loop longer
501 * than the FIFO length (usually one block).
503 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
505 * The size field in the FSR is broken so we have to
508 if (fsr & WBSD_FIFO_EMPTY)
510 else if (fsr & WBSD_FIFO_EMTHRE)
515 for (i = 16; i > fifo; i--) {
516 outb(*buffer, host->base + WBSD_DFR);
521 data->bytes_xfered++;
524 * End of scatter list entry?
526 if (host->remain == 0) {
528 * Get next entry. Check if last.
530 if (!wbsd_next_sg(host))
533 buffer = wbsd_sg_to_buffer(host);
539 * The controller stops sending interrupts for
540 * 'FIFO empty' under certain conditions. So we
541 * need to be a bit more pro-active.
543 tasklet_schedule(&host->fifo_tasklet);
546 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
550 unsigned long dmaflags;
553 DBGF("blksz %04x blks %04x flags %08x\n",
554 data->blksz, data->blocks, data->flags);
555 DBGF("tsac %d ms nsac %d clk\n",
556 data->timeout_ns / 1000000, data->timeout_clks);
561 size = data->blocks * data->blksz;
564 * Check timeout values for overflow.
565 * (Yes, some cards cause this value to overflow).
567 if (data->timeout_ns > 127000000)
568 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
570 wbsd_write_index(host, WBSD_IDX_TAAC,
571 data->timeout_ns / 1000000);
574 if (data->timeout_clks > 255)
575 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
577 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
580 * Inform the chip of how large blocks will be
581 * sent. It needs this to determine when to
584 * Space for CRC must be included in the size.
585 * Two bytes are needed for each data line.
587 if (host->bus_width == MMC_BUS_WIDTH_1) {
588 blksize = data->blksz + 2;
590 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
591 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
592 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
593 blksize = data->blksz + 2 * 4;
595 wbsd_write_index(host, WBSD_IDX_PBSMSB,
596 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
597 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
599 data->error = MMC_ERR_INVALID;
604 * Clear the FIFO. This is needed even for DMA
605 * transfers since the chip still uses the FIFO
608 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
609 setup |= WBSD_FIFO_RESET;
610 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
615 if (host->dma >= 0) {
617 * The buffer for DMA is only 64 kB.
619 BUG_ON(size > 0x10000);
620 if (size > 0x10000) {
621 data->error = MMC_ERR_INVALID;
626 * Transfer data from the SG list to
629 if (data->flags & MMC_DATA_WRITE)
630 wbsd_sg_to_dma(host, data);
633 * Initialise the ISA DMA controller.
635 dmaflags = claim_dma_lock();
636 disable_dma(host->dma);
637 clear_dma_ff(host->dma);
638 if (data->flags & MMC_DATA_READ)
639 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
641 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
642 set_dma_addr(host->dma, host->dma_addr);
643 set_dma_count(host->dma, size);
645 enable_dma(host->dma);
646 release_dma_lock(dmaflags);
649 * Enable DMA on the host.
651 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
654 * This flag is used to keep printk
655 * output to a minimum.
660 * Initialise the SG list.
662 wbsd_init_sg(host, data);
667 wbsd_write_index(host, WBSD_IDX_DMA, 0);
670 * Set up FIFO threshold levels (and fill
671 * buffer if doing a write).
673 if (data->flags & MMC_DATA_READ) {
674 wbsd_write_index(host, WBSD_IDX_FIFOEN,
675 WBSD_FIFOEN_FULL | 8);
677 wbsd_write_index(host, WBSD_IDX_FIFOEN,
678 WBSD_FIFOEN_EMPTY | 8);
679 wbsd_fill_fifo(host);
683 data->error = MMC_ERR_NONE;
686 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
688 unsigned long dmaflags;
692 WARN_ON(host->mrq == NULL);
695 * Send a stop command if needed.
698 wbsd_send_command(host, data->stop);
701 * Wait for the controller to leave data
705 status = wbsd_read_index(host, WBSD_IDX_STATUS);
706 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
711 if (host->dma >= 0) {
713 * Disable DMA on the host.
715 wbsd_write_index(host, WBSD_IDX_DMA, 0);
718 * Turn of ISA DMA controller.
720 dmaflags = claim_dma_lock();
721 disable_dma(host->dma);
722 clear_dma_ff(host->dma);
723 count = get_dma_residue(host->dma);
724 release_dma_lock(dmaflags);
726 data->bytes_xfered = host->mrq->data->blocks *
727 host->mrq->data->blksz - count;
728 data->bytes_xfered -= data->bytes_xfered % data->blksz;
734 printk(KERN_ERR "%s: Incomplete DMA transfer. "
736 mmc_hostname(host->mmc), count);
738 if (data->error == MMC_ERR_NONE)
739 data->error = MMC_ERR_FAILED;
742 * Transfer data from DMA buffer to
745 if (data->flags & MMC_DATA_READ)
746 wbsd_dma_to_sg(host, data);
749 if (data->error != MMC_ERR_NONE) {
750 if (data->bytes_xfered)
751 data->bytes_xfered -= data->blksz;
755 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
757 wbsd_request_end(host, host->mrq);
760 /*****************************************************************************\
762 * MMC layer callbacks *
764 \*****************************************************************************/
766 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
768 struct wbsd_host *host = mmc_priv(mmc);
769 struct mmc_command *cmd;
772 * Disable tasklets to avoid a deadlock.
774 spin_lock_bh(&host->lock);
776 BUG_ON(host->mrq != NULL);
783 * If there is no card in the slot then
784 * timeout immediatly.
786 if (!(host->flags & WBSD_FCARD_PRESENT)) {
787 cmd->error = MMC_ERR_TIMEOUT;
793 * The hardware is so delightfully stupid that it has a list
794 * of "data" commands. If a command isn't on this list, it'll
795 * just go back to the idle state and won't send any data
798 switch (cmd->opcode) {
812 /* ACMDs. We don't keep track of state, so we just treat them
813 * like any other command. */
818 #ifdef CONFIG_MMC_DEBUG
819 printk(KERN_WARNING "%s: Data command %d is not "
820 "supported by this controller.\n",
821 mmc_hostname(host->mmc), cmd->opcode);
823 cmd->error = MMC_ERR_INVALID;
830 * Does the request include data?
833 wbsd_prepare_data(host, cmd->data);
835 if (cmd->data->error != MMC_ERR_NONE)
839 wbsd_send_command(host, cmd);
842 * If this is a data transfer the request
843 * will be finished after the data has
846 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
848 * Dirty fix for hardware bug.
851 tasklet_schedule(&host->fifo_tasklet);
853 spin_unlock_bh(&host->lock);
859 wbsd_request_end(host, mrq);
861 spin_unlock_bh(&host->lock);
864 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
866 struct wbsd_host *host = mmc_priv(mmc);
869 spin_lock_bh(&host->lock);
872 * Reset the chip on each power off.
873 * Should clear out any weird states.
875 if (ios->power_mode == MMC_POWER_OFF)
876 wbsd_init_device(host);
878 if (ios->clock >= 24000000)
880 else if (ios->clock >= 16000000)
882 else if (ios->clock >= 12000000)
888 * Only write to the clock register when
889 * there is an actual change.
891 if (clk != host->clk) {
892 wbsd_write_index(host, WBSD_IDX_CLK, clk);
899 if (ios->power_mode != MMC_POWER_OFF) {
900 pwr = inb(host->base + WBSD_CSR);
901 pwr &= ~WBSD_POWER_N;
902 outb(pwr, host->base + WBSD_CSR);
906 * MMC cards need to have pin 1 high during init.
907 * It wreaks havoc with the card detection though so
908 * that needs to be disabled.
910 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
911 if (ios->chip_select == MMC_CS_HIGH) {
912 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
913 setup |= WBSD_DAT3_H;
914 host->flags |= WBSD_FIGNORE_DETECT;
916 if (setup & WBSD_DAT3_H) {
917 setup &= ~WBSD_DAT3_H;
920 * We cannot resume card detection immediatly
921 * because of capacitance and delays in the chip.
923 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
926 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
929 * Store bus width for later. Will be used when
930 * setting up the data transfer.
932 host->bus_width = ios->bus_width;
934 spin_unlock_bh(&host->lock);
937 static int wbsd_get_ro(struct mmc_host *mmc)
939 struct wbsd_host *host = mmc_priv(mmc);
942 spin_lock_bh(&host->lock);
944 csr = inb(host->base + WBSD_CSR);
946 outb(csr, host->base + WBSD_CSR);
950 csr = inb(host->base + WBSD_CSR);
952 outb(csr, host->base + WBSD_CSR);
954 spin_unlock_bh(&host->lock);
956 return csr & WBSD_WRPT;
959 static const struct mmc_host_ops wbsd_ops = {
960 .request = wbsd_request,
961 .set_ios = wbsd_set_ios,
962 .get_ro = wbsd_get_ro,
965 /*****************************************************************************\
967 * Interrupt handling *
969 \*****************************************************************************/
972 * Helper function to reset detection ignore
975 static void wbsd_reset_ignore(unsigned long data)
977 struct wbsd_host *host = (struct wbsd_host *)data;
979 BUG_ON(host == NULL);
981 DBG("Resetting card detection ignore\n");
983 spin_lock_bh(&host->lock);
985 host->flags &= ~WBSD_FIGNORE_DETECT;
988 * Card status might have changed during the
991 tasklet_schedule(&host->card_tasklet);
993 spin_unlock_bh(&host->lock);
1000 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1002 WARN_ON(!host->mrq);
1006 WARN_ON(!host->mrq->cmd);
1007 if (!host->mrq->cmd)
1010 WARN_ON(!host->mrq->cmd->data);
1011 if (!host->mrq->cmd->data)
1014 return host->mrq->cmd->data;
1017 static void wbsd_tasklet_card(unsigned long param)
1019 struct wbsd_host *host = (struct wbsd_host *)param;
1023 spin_lock(&host->lock);
1025 if (host->flags & WBSD_FIGNORE_DETECT) {
1026 spin_unlock(&host->lock);
1030 csr = inb(host->base + WBSD_CSR);
1031 WARN_ON(csr == 0xff);
1033 if (csr & WBSD_CARDPRESENT) {
1034 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1035 DBG("Card inserted\n");
1036 host->flags |= WBSD_FCARD_PRESENT;
1040 } else if (host->flags & WBSD_FCARD_PRESENT) {
1041 DBG("Card removed\n");
1042 host->flags &= ~WBSD_FCARD_PRESENT;
1045 printk(KERN_ERR "%s: Card removed during transfer!\n",
1046 mmc_hostname(host->mmc));
1049 host->mrq->cmd->error = MMC_ERR_FAILED;
1050 tasklet_schedule(&host->finish_tasklet);
1057 * Unlock first since we might get a call back.
1060 spin_unlock(&host->lock);
1063 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1066 static void wbsd_tasklet_fifo(unsigned long param)
1068 struct wbsd_host *host = (struct wbsd_host *)param;
1069 struct mmc_data *data;
1071 spin_lock(&host->lock);
1076 data = wbsd_get_data(host);
1080 if (data->flags & MMC_DATA_WRITE)
1081 wbsd_fill_fifo(host);
1083 wbsd_empty_fifo(host);
1088 if (host->num_sg == 0) {
1089 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1090 tasklet_schedule(&host->finish_tasklet);
1094 spin_unlock(&host->lock);
1097 static void wbsd_tasklet_crc(unsigned long param)
1099 struct wbsd_host *host = (struct wbsd_host *)param;
1100 struct mmc_data *data;
1102 spin_lock(&host->lock);
1107 data = wbsd_get_data(host);
1111 DBGF("CRC error\n");
1113 data->error = MMC_ERR_BADCRC;
1115 tasklet_schedule(&host->finish_tasklet);
1118 spin_unlock(&host->lock);
1121 static void wbsd_tasklet_timeout(unsigned long param)
1123 struct wbsd_host *host = (struct wbsd_host *)param;
1124 struct mmc_data *data;
1126 spin_lock(&host->lock);
1131 data = wbsd_get_data(host);
1137 data->error = MMC_ERR_TIMEOUT;
1139 tasklet_schedule(&host->finish_tasklet);
1142 spin_unlock(&host->lock);
1145 static void wbsd_tasklet_finish(unsigned long param)
1147 struct wbsd_host *host = (struct wbsd_host *)param;
1148 struct mmc_data *data;
1150 spin_lock(&host->lock);
1152 WARN_ON(!host->mrq);
1156 data = wbsd_get_data(host);
1160 wbsd_finish_data(host, data);
1163 spin_unlock(&host->lock);
1167 * Interrupt handling
1170 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1172 struct wbsd_host *host = dev_id;
1175 isr = inb(host->base + WBSD_ISR);
1178 * Was it actually our hardware that caused the interrupt?
1180 if (isr == 0xff || isr == 0x00)
1186 * Schedule tasklets as needed.
1188 if (isr & WBSD_INT_CARD)
1189 tasklet_schedule(&host->card_tasklet);
1190 if (isr & WBSD_INT_FIFO_THRE)
1191 tasklet_schedule(&host->fifo_tasklet);
1192 if (isr & WBSD_INT_CRC)
1193 tasklet_hi_schedule(&host->crc_tasklet);
1194 if (isr & WBSD_INT_TIMEOUT)
1195 tasklet_hi_schedule(&host->timeout_tasklet);
1196 if (isr & WBSD_INT_TC)
1197 tasklet_schedule(&host->finish_tasklet);
1202 /*****************************************************************************\
1204 * Device initialisation and shutdown *
1206 \*****************************************************************************/
1209 * Allocate/free MMC structure.
1212 static int __devinit wbsd_alloc_mmc(struct device *dev)
1214 struct mmc_host *mmc;
1215 struct wbsd_host *host;
1218 * Allocate MMC structure.
1220 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1224 host = mmc_priv(mmc);
1230 * Set host parameters.
1232 mmc->ops = &wbsd_ops;
1233 mmc->f_min = 375000;
1234 mmc->f_max = 24000000;
1235 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1236 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1238 spin_lock_init(&host->lock);
1243 init_timer(&host->ignore_timer);
1244 host->ignore_timer.data = (unsigned long)host;
1245 host->ignore_timer.function = wbsd_reset_ignore;
1248 * Maximum number of segments. Worst case is one sector per segment
1249 * so this will be 64kB/512.
1251 mmc->max_hw_segs = 128;
1252 mmc->max_phys_segs = 128;
1255 * Maximum request size. Also limited by 64KiB buffer.
1257 mmc->max_req_size = 65536;
1260 * Maximum segment size. Could be one segment with the maximum number
1263 mmc->max_seg_size = mmc->max_req_size;
1266 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1267 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1269 mmc->max_blk_size = 4087;
1272 * Maximum block count. There is no real limit so the maximum
1273 * request size will be the only restriction.
1275 mmc->max_blk_count = mmc->max_req_size;
1277 dev_set_drvdata(dev, mmc);
1282 static void __devexit wbsd_free_mmc(struct device *dev)
1284 struct mmc_host *mmc;
1285 struct wbsd_host *host;
1287 mmc = dev_get_drvdata(dev);
1291 host = mmc_priv(mmc);
1292 BUG_ON(host == NULL);
1294 del_timer_sync(&host->ignore_timer);
1298 dev_set_drvdata(dev, NULL);
1302 * Scan for known chip id:s
1305 static int __devinit wbsd_scan(struct wbsd_host *host)
1311 * Iterate through all ports, all codes to
1312 * find hardware that is in our known list.
1314 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1315 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1318 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1321 host->config = config_ports[i];
1322 host->unlock_code = unlock_codes[j];
1324 wbsd_unlock_config(host);
1326 outb(WBSD_CONF_ID_HI, config_ports[i]);
1327 id = inb(config_ports[i] + 1) << 8;
1329 outb(WBSD_CONF_ID_LO, config_ports[i]);
1330 id |= inb(config_ports[i] + 1);
1332 wbsd_lock_config(host);
1334 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1335 if (id == valid_ids[k]) {
1343 DBG("Unknown hardware (id %x) found at %x\n",
1344 id, config_ports[i]);
1348 release_region(config_ports[i], 2);
1352 host->unlock_code = 0;
1358 * Allocate/free io port ranges
1361 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1366 if (!request_region(base, 8, DRIVER_NAME))
1374 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1377 release_region(host->base, 8);
1382 release_region(host->config, 2);
1388 * Allocate/free DMA port and buffer
1391 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1396 if (request_dma(dma, DRIVER_NAME))
1400 * We need to allocate a special buffer in
1401 * order for ISA to be able to DMA to it.
1403 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1404 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1405 if (!host->dma_buffer)
1409 * Translate the address to a physical address.
1411 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1412 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1415 * ISA DMA must be aligned on a 64k basis.
1417 if ((host->dma_addr & 0xffff) != 0)
1420 * ISA cannot access memory above 16 MB.
1422 else if (host->dma_addr >= 0x1000000)
1431 * If we've gotten here then there is some kind of alignment bug
1435 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1436 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1437 host->dma_addr = (dma_addr_t)NULL;
1439 kfree(host->dma_buffer);
1440 host->dma_buffer = NULL;
1446 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1447 "Falling back on FIFO.\n", dma);
1450 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1452 if (host->dma_addr) {
1453 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1454 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1456 kfree(host->dma_buffer);
1458 free_dma(host->dma);
1461 host->dma_buffer = NULL;
1462 host->dma_addr = (dma_addr_t)NULL;
1466 * Allocate/free IRQ.
1469 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1474 * Allocate interrupt.
1477 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1486 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1487 (unsigned long)host);
1488 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1489 (unsigned long)host);
1490 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1491 (unsigned long)host);
1492 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1493 (unsigned long)host);
1494 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1495 (unsigned long)host);
1500 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1505 free_irq(host->irq, host);
1509 tasklet_kill(&host->card_tasklet);
1510 tasklet_kill(&host->fifo_tasklet);
1511 tasklet_kill(&host->crc_tasklet);
1512 tasklet_kill(&host->timeout_tasklet);
1513 tasklet_kill(&host->finish_tasklet);
1517 * Allocate all resources for the host.
1520 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1521 int base, int irq, int dma)
1526 * Allocate I/O ports.
1528 ret = wbsd_request_region(host, base);
1533 * Allocate interrupt.
1535 ret = wbsd_request_irq(host, irq);
1542 wbsd_request_dma(host, dma);
1548 * Release all resources for the host.
1551 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1553 wbsd_release_dma(host);
1554 wbsd_release_irq(host);
1555 wbsd_release_regions(host);
1559 * Configure the resources the chip should use.
1562 static void wbsd_chip_config(struct wbsd_host *host)
1564 wbsd_unlock_config(host);
1569 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1570 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1573 * Select SD/MMC function.
1575 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1578 * Set up card detection.
1580 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1585 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1586 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1588 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1591 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1594 * Enable and power up chip.
1596 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1597 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1599 wbsd_lock_config(host);
1603 * Check that configured resources are correct.
1606 static int wbsd_chip_validate(struct wbsd_host *host)
1610 wbsd_unlock_config(host);
1613 * Select SD/MMC function.
1615 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1618 * Read configuration.
1620 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1621 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1623 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1625 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1627 wbsd_lock_config(host);
1630 * Validate against given configuration.
1632 if (base != host->base)
1634 if (irq != host->irq)
1636 if ((dma != host->dma) && (host->dma != -1))
1643 * Powers down the SD function
1646 static void wbsd_chip_poweroff(struct wbsd_host *host)
1648 wbsd_unlock_config(host);
1650 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1651 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1653 wbsd_lock_config(host);
1656 /*****************************************************************************\
1658 * Devices setup and shutdown *
1660 \*****************************************************************************/
1662 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1665 struct wbsd_host *host = NULL;
1666 struct mmc_host *mmc = NULL;
1669 ret = wbsd_alloc_mmc(dev);
1673 mmc = dev_get_drvdata(dev);
1674 host = mmc_priv(mmc);
1677 * Scan for hardware.
1679 ret = wbsd_scan(host);
1681 if (pnp && (ret == -ENODEV)) {
1682 printk(KERN_WARNING DRIVER_NAME
1683 ": Unable to confirm device presence. You may "
1684 "experience lock-ups.\n");
1692 * Request resources.
1694 ret = wbsd_request_resources(host, base, irq, dma);
1696 wbsd_release_resources(host);
1702 * See if chip needs to be configured.
1705 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1706 printk(KERN_WARNING DRIVER_NAME
1707 ": PnP active but chip not configured! "
1708 "You probably have a buggy BIOS. "
1709 "Configuring chip manually.\n");
1710 wbsd_chip_config(host);
1713 wbsd_chip_config(host);
1716 * Power Management stuff. No idea how this works.
1721 wbsd_unlock_config(host);
1722 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1723 wbsd_lock_config(host);
1727 * Allow device to initialise itself properly.
1732 * Reset the chip into a known state.
1734 wbsd_init_device(host);
1738 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1739 if (host->chip_id != 0)
1740 printk(" id %x", (int)host->chip_id);
1741 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1743 printk(" dma %d", (int)host->dma);
1753 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1755 struct mmc_host *mmc = dev_get_drvdata(dev);
1756 struct wbsd_host *host;
1761 host = mmc_priv(mmc);
1763 mmc_remove_host(mmc);
1766 * Power down the SD/MMC function.
1769 wbsd_chip_poweroff(host);
1771 wbsd_release_resources(host);
1780 static int __devinit wbsd_probe(struct platform_device *dev)
1782 /* Use the module parameters for resources */
1783 return wbsd_init(&dev->dev, io, irq, dma, 0);
1786 static int __devexit wbsd_remove(struct platform_device *dev)
1788 wbsd_shutdown(&dev->dev, 0);
1799 static int __devinit
1800 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1805 * Get resources from PnP layer.
1807 io = pnp_port_start(pnpdev, 0);
1808 irq = pnp_irq(pnpdev, 0);
1809 if (pnp_dma_valid(pnpdev, 0))
1810 dma = pnp_dma(pnpdev, 0);
1814 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1816 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1819 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1821 wbsd_shutdown(&dev->dev, 1);
1824 #endif /* CONFIG_PNP */
1832 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1834 BUG_ON(host == NULL);
1836 return mmc_suspend_host(host->mmc, state);
1839 static int wbsd_resume(struct wbsd_host *host)
1841 BUG_ON(host == NULL);
1843 wbsd_init_device(host);
1845 return mmc_resume_host(host->mmc);
1848 static int wbsd_platform_suspend(struct platform_device *dev,
1851 struct mmc_host *mmc = platform_get_drvdata(dev);
1852 struct wbsd_host *host;
1858 DBGF("Suspending...\n");
1860 host = mmc_priv(mmc);
1862 ret = wbsd_suspend(host, state);
1866 wbsd_chip_poweroff(host);
1871 static int wbsd_platform_resume(struct platform_device *dev)
1873 struct mmc_host *mmc = platform_get_drvdata(dev);
1874 struct wbsd_host *host;
1879 DBGF("Resuming...\n");
1881 host = mmc_priv(mmc);
1883 wbsd_chip_config(host);
1886 * Allow device to initialise itself properly.
1890 return wbsd_resume(host);
1895 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1897 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1898 struct wbsd_host *host;
1903 DBGF("Suspending...\n");
1905 host = mmc_priv(mmc);
1907 return wbsd_suspend(host, state);
1910 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1912 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1913 struct wbsd_host *host;
1918 DBGF("Resuming...\n");
1920 host = mmc_priv(mmc);
1923 * See if chip needs to be configured.
1925 if (host->config != 0) {
1926 if (!wbsd_chip_validate(host)) {
1927 printk(KERN_WARNING DRIVER_NAME
1928 ": PnP active but chip not configured! "
1929 "You probably have a buggy BIOS. "
1930 "Configuring chip manually.\n");
1931 wbsd_chip_config(host);
1936 * Allow device to initialise itself properly.
1940 return wbsd_resume(host);
1943 #endif /* CONFIG_PNP */
1945 #else /* CONFIG_PM */
1947 #define wbsd_platform_suspend NULL
1948 #define wbsd_platform_resume NULL
1950 #define wbsd_pnp_suspend NULL
1951 #define wbsd_pnp_resume NULL
1953 #endif /* CONFIG_PM */
1955 static struct platform_device *wbsd_device;
1957 static struct platform_driver wbsd_driver = {
1958 .probe = wbsd_probe,
1959 .remove = __devexit_p(wbsd_remove),
1961 .suspend = wbsd_platform_suspend,
1962 .resume = wbsd_platform_resume,
1964 .name = DRIVER_NAME,
1970 static struct pnp_driver wbsd_pnp_driver = {
1971 .name = DRIVER_NAME,
1972 .id_table = pnp_dev_table,
1973 .probe = wbsd_pnp_probe,
1974 .remove = __devexit_p(wbsd_pnp_remove),
1976 .suspend = wbsd_pnp_suspend,
1977 .resume = wbsd_pnp_resume,
1980 #endif /* CONFIG_PNP */
1983 * Module loading/unloading
1986 static int __init wbsd_drv_init(void)
1990 printk(KERN_INFO DRIVER_NAME
1991 ": Winbond W83L51xD SD/MMC card interface driver\n");
1992 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1997 result = pnp_register_driver(&wbsd_pnp_driver);
2001 #endif /* CONFIG_PNP */
2004 result = platform_driver_register(&wbsd_driver);
2008 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2010 platform_driver_unregister(&wbsd_driver);
2014 result = platform_device_add(wbsd_device);
2016 platform_device_put(wbsd_device);
2017 platform_driver_unregister(&wbsd_driver);
2025 static void __exit wbsd_drv_exit(void)
2030 pnp_unregister_driver(&wbsd_pnp_driver);
2032 #endif /* CONFIG_PNP */
2035 platform_device_unregister(wbsd_device);
2037 platform_driver_unregister(&wbsd_driver);
2043 module_init(wbsd_drv_init);
2044 module_exit(wbsd_drv_exit);
2046 module_param(nopnp, uint, 0444);
2048 module_param(io, uint, 0444);
2049 module_param(irq, uint, 0444);
2050 module_param(dma, int, 0444);
2052 MODULE_LICENSE("GPL");
2053 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2054 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2057 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2059 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2060 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2061 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");