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>
36 #include <linux/mmc/protocol.h>
40 #include <asm/scatterlist.h>
44 #define DRIVER_NAME "wbsd"
47 pr_debug(DRIVER_NAME ": " x)
48 #define DBGF(f, x...) \
49 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
57 static const struct pnp_device_id pnp_dev_table[] = {
63 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
65 #endif /* CONFIG_PNP */
67 static const int config_ports[] = { 0x2E, 0x4E };
68 static const int unlock_codes[] = { 0x83, 0x87 };
70 static const int valid_ids[] = {
75 static unsigned int nopnp = 0;
77 static const unsigned int nopnp = 1;
79 static unsigned int io = 0x248;
80 static unsigned int irq = 6;
87 static inline void wbsd_unlock_config(struct wbsd_host *host)
89 BUG_ON(host->config == 0);
91 outb(host->unlock_code, host->config);
92 outb(host->unlock_code, host->config);
95 static inline void wbsd_lock_config(struct wbsd_host *host)
97 BUG_ON(host->config == 0);
99 outb(LOCK_CODE, host->config);
102 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
104 BUG_ON(host->config == 0);
106 outb(reg, host->config);
107 outb(value, host->config + 1);
110 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
112 BUG_ON(host->config == 0);
114 outb(reg, host->config);
115 return inb(host->config + 1);
118 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
120 outb(index, host->base + WBSD_IDXR);
121 outb(value, host->base + WBSD_DATAR);
124 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
126 outb(index, host->base + WBSD_IDXR);
127 return inb(host->base + WBSD_DATAR);
134 static void wbsd_init_device(struct wbsd_host *host)
139 * Reset chip (SD/MMC part) and fifo.
141 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
142 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
143 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
148 setup &= ~WBSD_DAT3_H;
149 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
150 host->flags &= ~WBSD_FIGNORE_DETECT;
153 * Read back default clock.
155 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
160 outb(WBSD_POWER_N, host->base + WBSD_CSR);
163 * Set maximum timeout.
165 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
168 * Test for card presence
170 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
171 host->flags |= WBSD_FCARD_PRESENT;
173 host->flags &= ~WBSD_FCARD_PRESENT;
176 * Enable interesting interrupts.
179 ier |= WBSD_EINT_CARD;
180 ier |= WBSD_EINT_FIFO_THRE;
181 ier |= WBSD_EINT_CRC;
182 ier |= WBSD_EINT_TIMEOUT;
185 outb(ier, host->base + WBSD_EIR);
190 inb(host->base + WBSD_ISR);
193 static void wbsd_reset(struct wbsd_host *host)
197 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
200 * Soft reset of chip (SD/MMC part).
202 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
203 setup |= WBSD_SOFT_RESET;
204 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
207 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
209 unsigned long dmaflags;
211 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
213 if (host->dma >= 0) {
215 * Release ISA DMA controller.
217 dmaflags = claim_dma_lock();
218 disable_dma(host->dma);
219 clear_dma_ff(host->dma);
220 release_dma_lock(dmaflags);
223 * Disable DMA on host.
225 wbsd_write_index(host, WBSD_IDX_DMA, 0);
231 * MMC layer might call back into the driver so first unlock.
233 spin_unlock(&host->lock);
234 mmc_request_done(host->mmc, mrq);
235 spin_lock(&host->lock);
239 * Scatter/gather functions
242 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
245 * Get info. about SG list from data structure.
247 host->cur_sg = data->sg;
248 host->num_sg = data->sg_len;
251 host->remain = host->cur_sg->length;
254 static inline int wbsd_next_sg(struct wbsd_host *host)
257 * Skip to next SG entry.
265 if (host->num_sg > 0) {
267 host->remain = host->cur_sg->length;
273 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
275 return page_address(host->cur_sg->page) + host->cur_sg->offset;
278 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
281 struct scatterlist *sg;
282 char *dmabuf = host->dma_buffer;
288 for (i = 0; i < len; i++) {
289 sgbuf = page_address(sg[i].page) + sg[i].offset;
290 memcpy(dmabuf, sgbuf, sg[i].length);
291 dmabuf += sg[i].length;
295 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
298 struct scatterlist *sg;
299 char *dmabuf = host->dma_buffer;
305 for (i = 0; i < len; i++) {
306 sgbuf = page_address(sg[i].page) + sg[i].offset;
307 memcpy(sgbuf, dmabuf, sg[i].length);
308 dmabuf += sg[i].length;
316 static inline void wbsd_get_short_reply(struct wbsd_host *host,
317 struct mmc_command *cmd)
320 * Correct response type?
322 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
323 cmd->error = MMC_ERR_INVALID;
327 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
328 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
329 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
330 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
331 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
334 static inline void wbsd_get_long_reply(struct wbsd_host *host,
335 struct mmc_command *cmd)
340 * Correct response type?
342 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
343 cmd->error = MMC_ERR_INVALID;
347 for (i = 0; i < 4; i++) {
349 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
351 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
353 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
355 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
359 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
364 DBGF("Sending cmd (%x)\n", cmd->opcode);
367 * Clear accumulated ISR. The interrupt routine
368 * will fill this one with events that occur during
374 * Send the command (CRC calculated by host).
376 outb(cmd->opcode, host->base + WBSD_CMDR);
377 for (i = 3; i >= 0; i--)
378 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
380 cmd->error = MMC_ERR_NONE;
383 * Wait for the request to complete.
386 status = wbsd_read_index(host, WBSD_IDX_STATUS);
387 } while (status & WBSD_CARDTRAFFIC);
390 * Do we expect a reply?
392 if (cmd->flags & MMC_RSP_PRESENT) {
399 if (isr & WBSD_INT_CARD)
400 cmd->error = MMC_ERR_TIMEOUT;
402 else if (isr & WBSD_INT_TIMEOUT)
403 cmd->error = MMC_ERR_TIMEOUT;
405 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
406 cmd->error = MMC_ERR_BADCRC;
409 if (cmd->flags & MMC_RSP_136)
410 wbsd_get_long_reply(host, cmd);
412 wbsd_get_short_reply(host, cmd);
416 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
423 static void wbsd_empty_fifo(struct wbsd_host *host)
425 struct mmc_data *data = host->mrq->cmd->data;
430 * Handle excessive data.
432 if (host->num_sg == 0)
435 buffer = wbsd_sg_to_buffer(host) + host->offset;
438 * Drain the fifo. This has a tendency to loop longer
439 * than the FIFO length (usually one block).
441 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
443 * The size field in the FSR is broken so we have to
446 if (fsr & WBSD_FIFO_FULL)
448 else if (fsr & WBSD_FIFO_FUTHRE)
453 for (i = 0; i < fifo; i++) {
454 *buffer = inb(host->base + WBSD_DFR);
459 data->bytes_xfered++;
462 * End of scatter list entry?
464 if (host->remain == 0) {
466 * Get next entry. Check if last.
468 if (!wbsd_next_sg(host))
471 buffer = wbsd_sg_to_buffer(host);
477 * This is a very dirty hack to solve a
478 * hardware problem. The chip doesn't trigger
479 * FIFO threshold interrupts properly.
481 if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
482 tasklet_schedule(&host->fifo_tasklet);
485 static void wbsd_fill_fifo(struct wbsd_host *host)
487 struct mmc_data *data = host->mrq->cmd->data;
492 * Check that we aren't being called after the
493 * entire buffer has been transfered.
495 if (host->num_sg == 0)
498 buffer = wbsd_sg_to_buffer(host) + host->offset;
501 * Fill the fifo. This has a tendency to loop longer
502 * than the FIFO length (usually one block).
504 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
506 * The size field in the FSR is broken so we have to
509 if (fsr & WBSD_FIFO_EMPTY)
511 else if (fsr & WBSD_FIFO_EMTHRE)
516 for (i = 16; i > fifo; i--) {
517 outb(*buffer, host->base + WBSD_DFR);
522 data->bytes_xfered++;
525 * End of scatter list entry?
527 if (host->remain == 0) {
529 * Get next entry. Check if last.
531 if (!wbsd_next_sg(host))
534 buffer = wbsd_sg_to_buffer(host);
540 * The controller stops sending interrupts for
541 * 'FIFO empty' under certain conditions. So we
542 * need to be a bit more pro-active.
544 tasklet_schedule(&host->fifo_tasklet);
547 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
551 unsigned long dmaflags;
554 DBGF("blksz %04x blks %04x flags %08x\n",
555 data->blksz, data->blocks, data->flags);
556 DBGF("tsac %d ms nsac %d clk\n",
557 data->timeout_ns / 1000000, data->timeout_clks);
562 size = data->blocks * data->blksz;
565 * Check timeout values for overflow.
566 * (Yes, some cards cause this value to overflow).
568 if (data->timeout_ns > 127000000)
569 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
571 wbsd_write_index(host, WBSD_IDX_TAAC,
572 data->timeout_ns / 1000000);
575 if (data->timeout_clks > 255)
576 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
578 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
581 * Inform the chip of how large blocks will be
582 * sent. It needs this to determine when to
585 * Space for CRC must be included in the size.
586 * Two bytes are needed for each data line.
588 if (host->bus_width == MMC_BUS_WIDTH_1) {
589 blksize = data->blksz + 2;
591 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
592 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
593 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
594 blksize = data->blksz + 2 * 4;
596 wbsd_write_index(host, WBSD_IDX_PBSMSB,
597 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
598 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
600 data->error = MMC_ERR_INVALID;
605 * Clear the FIFO. This is needed even for DMA
606 * transfers since the chip still uses the FIFO
609 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
610 setup |= WBSD_FIFO_RESET;
611 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
616 if (host->dma >= 0) {
618 * The buffer for DMA is only 64 kB.
620 BUG_ON(size > 0x10000);
621 if (size > 0x10000) {
622 data->error = MMC_ERR_INVALID;
627 * Transfer data from the SG list to
630 if (data->flags & MMC_DATA_WRITE)
631 wbsd_sg_to_dma(host, data);
634 * Initialise the ISA DMA controller.
636 dmaflags = claim_dma_lock();
637 disable_dma(host->dma);
638 clear_dma_ff(host->dma);
639 if (data->flags & MMC_DATA_READ)
640 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
642 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
643 set_dma_addr(host->dma, host->dma_addr);
644 set_dma_count(host->dma, size);
646 enable_dma(host->dma);
647 release_dma_lock(dmaflags);
650 * Enable DMA on the host.
652 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
655 * This flag is used to keep printk
656 * output to a minimum.
661 * Initialise the SG list.
663 wbsd_init_sg(host, data);
668 wbsd_write_index(host, WBSD_IDX_DMA, 0);
671 * Set up FIFO threshold levels (and fill
672 * buffer if doing a write).
674 if (data->flags & MMC_DATA_READ) {
675 wbsd_write_index(host, WBSD_IDX_FIFOEN,
676 WBSD_FIFOEN_FULL | 8);
678 wbsd_write_index(host, WBSD_IDX_FIFOEN,
679 WBSD_FIFOEN_EMPTY | 8);
680 wbsd_fill_fifo(host);
684 data->error = MMC_ERR_NONE;
687 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
689 unsigned long dmaflags;
693 WARN_ON(host->mrq == NULL);
696 * Send a stop command if needed.
699 wbsd_send_command(host, data->stop);
702 * Wait for the controller to leave data
706 status = wbsd_read_index(host, WBSD_IDX_STATUS);
707 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
712 if (host->dma >= 0) {
714 * Disable DMA on the host.
716 wbsd_write_index(host, WBSD_IDX_DMA, 0);
719 * Turn of ISA DMA controller.
721 dmaflags = claim_dma_lock();
722 disable_dma(host->dma);
723 clear_dma_ff(host->dma);
724 count = get_dma_residue(host->dma);
725 release_dma_lock(dmaflags);
727 data->bytes_xfered = host->mrq->data->blocks *
728 host->mrq->data->blksz - count;
729 data->bytes_xfered -= data->bytes_xfered % data->blksz;
735 printk(KERN_ERR "%s: Incomplete DMA transfer. "
737 mmc_hostname(host->mmc), count);
739 if (data->error == MMC_ERR_NONE)
740 data->error = MMC_ERR_FAILED;
743 * Transfer data from DMA buffer to
746 if (data->flags & MMC_DATA_READ)
747 wbsd_dma_to_sg(host, data);
750 if (data->error != MMC_ERR_NONE) {
751 if (data->bytes_xfered)
752 data->bytes_xfered -= data->blksz;
756 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
758 wbsd_request_end(host, host->mrq);
761 /*****************************************************************************\
763 * MMC layer callbacks *
765 \*****************************************************************************/
767 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
769 struct wbsd_host *host = mmc_priv(mmc);
770 struct mmc_command *cmd;
773 * Disable tasklets to avoid a deadlock.
775 spin_lock_bh(&host->lock);
777 BUG_ON(host->mrq != NULL);
784 * If there is no card in the slot then
785 * timeout immediatly.
787 if (!(host->flags & WBSD_FCARD_PRESENT)) {
788 cmd->error = MMC_ERR_TIMEOUT;
793 * Does the request include data?
796 wbsd_prepare_data(host, cmd->data);
798 if (cmd->data->error != MMC_ERR_NONE)
802 wbsd_send_command(host, cmd);
805 * If this is a data transfer the request
806 * will be finished after the data has
809 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
811 * The hardware is so delightfully stupid that it has a list
812 * of "data" commands. If a command isn't on this list, it'll
813 * just go back to the idle state and won't send any data
816 switch (cmd->opcode) {
830 /* ACMDs. We don't keep track of state, so we just treat them
831 * like any other command. */
836 #ifdef CONFIG_MMC_DEBUG
837 printk(KERN_WARNING "%s: Data command %d is not "
838 "supported by this controller.\n",
839 mmc_hostname(host->mmc), cmd->opcode);
841 cmd->data->error = MMC_ERR_INVALID;
844 wbsd_send_command(host, cmd->data->stop);
850 * Dirty fix for hardware bug.
853 tasklet_schedule(&host->fifo_tasklet);
855 spin_unlock_bh(&host->lock);
861 wbsd_request_end(host, mrq);
863 spin_unlock_bh(&host->lock);
866 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
868 struct wbsd_host *host = mmc_priv(mmc);
871 spin_lock_bh(&host->lock);
874 * Reset the chip on each power off.
875 * Should clear out any weird states.
877 if (ios->power_mode == MMC_POWER_OFF)
878 wbsd_init_device(host);
880 if (ios->clock >= 24000000)
882 else if (ios->clock >= 16000000)
884 else if (ios->clock >= 12000000)
890 * Only write to the clock register when
891 * there is an actual change.
893 if (clk != host->clk) {
894 wbsd_write_index(host, WBSD_IDX_CLK, clk);
901 if (ios->power_mode != MMC_POWER_OFF) {
902 pwr = inb(host->base + WBSD_CSR);
903 pwr &= ~WBSD_POWER_N;
904 outb(pwr, host->base + WBSD_CSR);
908 * MMC cards need to have pin 1 high during init.
909 * It wreaks havoc with the card detection though so
910 * that needs to be disabled.
912 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
913 if (ios->chip_select == MMC_CS_HIGH) {
914 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
915 setup |= WBSD_DAT3_H;
916 host->flags |= WBSD_FIGNORE_DETECT;
918 if (setup & WBSD_DAT3_H) {
919 setup &= ~WBSD_DAT3_H;
922 * We cannot resume card detection immediatly
923 * because of capacitance and delays in the chip.
925 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
928 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
931 * Store bus width for later. Will be used when
932 * setting up the data transfer.
934 host->bus_width = ios->bus_width;
936 spin_unlock_bh(&host->lock);
939 static int wbsd_get_ro(struct mmc_host *mmc)
941 struct wbsd_host *host = mmc_priv(mmc);
944 spin_lock_bh(&host->lock);
946 csr = inb(host->base + WBSD_CSR);
948 outb(csr, host->base + WBSD_CSR);
952 csr = inb(host->base + WBSD_CSR);
954 outb(csr, host->base + WBSD_CSR);
956 spin_unlock_bh(&host->lock);
958 return csr & WBSD_WRPT;
961 static const struct mmc_host_ops wbsd_ops = {
962 .request = wbsd_request,
963 .set_ios = wbsd_set_ios,
964 .get_ro = wbsd_get_ro,
967 /*****************************************************************************\
969 * Interrupt handling *
971 \*****************************************************************************/
974 * Helper function to reset detection ignore
977 static void wbsd_reset_ignore(unsigned long data)
979 struct wbsd_host *host = (struct wbsd_host *)data;
981 BUG_ON(host == NULL);
983 DBG("Resetting card detection ignore\n");
985 spin_lock_bh(&host->lock);
987 host->flags &= ~WBSD_FIGNORE_DETECT;
990 * Card status might have changed during the
993 tasklet_schedule(&host->card_tasklet);
995 spin_unlock_bh(&host->lock);
1002 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1004 WARN_ON(!host->mrq);
1008 WARN_ON(!host->mrq->cmd);
1009 if (!host->mrq->cmd)
1012 WARN_ON(!host->mrq->cmd->data);
1013 if (!host->mrq->cmd->data)
1016 return host->mrq->cmd->data;
1019 static void wbsd_tasklet_card(unsigned long param)
1021 struct wbsd_host *host = (struct wbsd_host *)param;
1025 spin_lock(&host->lock);
1027 if (host->flags & WBSD_FIGNORE_DETECT) {
1028 spin_unlock(&host->lock);
1032 csr = inb(host->base + WBSD_CSR);
1033 WARN_ON(csr == 0xff);
1035 if (csr & WBSD_CARDPRESENT) {
1036 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1037 DBG("Card inserted\n");
1038 host->flags |= WBSD_FCARD_PRESENT;
1042 } else if (host->flags & WBSD_FCARD_PRESENT) {
1043 DBG("Card removed\n");
1044 host->flags &= ~WBSD_FCARD_PRESENT;
1047 printk(KERN_ERR "%s: Card removed during transfer!\n",
1048 mmc_hostname(host->mmc));
1051 host->mrq->cmd->error = MMC_ERR_FAILED;
1052 tasklet_schedule(&host->finish_tasklet);
1059 * Unlock first since we might get a call back.
1062 spin_unlock(&host->lock);
1065 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1068 static void wbsd_tasklet_fifo(unsigned long param)
1070 struct wbsd_host *host = (struct wbsd_host *)param;
1071 struct mmc_data *data;
1073 spin_lock(&host->lock);
1078 data = wbsd_get_data(host);
1082 if (data->flags & MMC_DATA_WRITE)
1083 wbsd_fill_fifo(host);
1085 wbsd_empty_fifo(host);
1090 if (host->num_sg == 0) {
1091 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1092 tasklet_schedule(&host->finish_tasklet);
1096 spin_unlock(&host->lock);
1099 static void wbsd_tasklet_crc(unsigned long param)
1101 struct wbsd_host *host = (struct wbsd_host *)param;
1102 struct mmc_data *data;
1104 spin_lock(&host->lock);
1109 data = wbsd_get_data(host);
1113 DBGF("CRC error\n");
1115 data->error = MMC_ERR_BADCRC;
1117 tasklet_schedule(&host->finish_tasklet);
1120 spin_unlock(&host->lock);
1123 static void wbsd_tasklet_timeout(unsigned long param)
1125 struct wbsd_host *host = (struct wbsd_host *)param;
1126 struct mmc_data *data;
1128 spin_lock(&host->lock);
1133 data = wbsd_get_data(host);
1139 data->error = MMC_ERR_TIMEOUT;
1141 tasklet_schedule(&host->finish_tasklet);
1144 spin_unlock(&host->lock);
1147 static void wbsd_tasklet_finish(unsigned long param)
1149 struct wbsd_host *host = (struct wbsd_host *)param;
1150 struct mmc_data *data;
1152 spin_lock(&host->lock);
1154 WARN_ON(!host->mrq);
1158 data = wbsd_get_data(host);
1162 wbsd_finish_data(host, data);
1165 spin_unlock(&host->lock);
1169 * Interrupt handling
1172 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1174 struct wbsd_host *host = dev_id;
1177 isr = inb(host->base + WBSD_ISR);
1180 * Was it actually our hardware that caused the interrupt?
1182 if (isr == 0xff || isr == 0x00)
1188 * Schedule tasklets as needed.
1190 if (isr & WBSD_INT_CARD)
1191 tasklet_schedule(&host->card_tasklet);
1192 if (isr & WBSD_INT_FIFO_THRE)
1193 tasklet_schedule(&host->fifo_tasklet);
1194 if (isr & WBSD_INT_CRC)
1195 tasklet_hi_schedule(&host->crc_tasklet);
1196 if (isr & WBSD_INT_TIMEOUT)
1197 tasklet_hi_schedule(&host->timeout_tasklet);
1198 if (isr & WBSD_INT_TC)
1199 tasklet_schedule(&host->finish_tasklet);
1204 /*****************************************************************************\
1206 * Device initialisation and shutdown *
1208 \*****************************************************************************/
1211 * Allocate/free MMC structure.
1214 static int __devinit wbsd_alloc_mmc(struct device *dev)
1216 struct mmc_host *mmc;
1217 struct wbsd_host *host;
1220 * Allocate MMC structure.
1222 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1226 host = mmc_priv(mmc);
1232 * Set host parameters.
1234 mmc->ops = &wbsd_ops;
1235 mmc->f_min = 375000;
1236 mmc->f_max = 24000000;
1237 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1238 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1240 spin_lock_init(&host->lock);
1245 init_timer(&host->ignore_timer);
1246 host->ignore_timer.data = (unsigned long)host;
1247 host->ignore_timer.function = wbsd_reset_ignore;
1250 * Maximum number of segments. Worst case is one sector per segment
1251 * so this will be 64kB/512.
1253 mmc->max_hw_segs = 128;
1254 mmc->max_phys_segs = 128;
1257 * Maximum request size. Also limited by 64KiB buffer.
1259 mmc->max_req_size = 65536;
1262 * Maximum segment size. Could be one segment with the maximum number
1265 mmc->max_seg_size = mmc->max_req_size;
1268 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1269 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1271 mmc->max_blk_size = 4087;
1274 * Maximum block count. There is no real limit so the maximum
1275 * request size will be the only restriction.
1277 mmc->max_blk_count = mmc->max_req_size;
1279 dev_set_drvdata(dev, mmc);
1284 static void __devexit wbsd_free_mmc(struct device *dev)
1286 struct mmc_host *mmc;
1287 struct wbsd_host *host;
1289 mmc = dev_get_drvdata(dev);
1293 host = mmc_priv(mmc);
1294 BUG_ON(host == NULL);
1296 del_timer_sync(&host->ignore_timer);
1300 dev_set_drvdata(dev, NULL);
1304 * Scan for known chip id:s
1307 static int __devinit wbsd_scan(struct wbsd_host *host)
1313 * Iterate through all ports, all codes to
1314 * find hardware that is in our known list.
1316 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1317 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1320 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1323 host->config = config_ports[i];
1324 host->unlock_code = unlock_codes[j];
1326 wbsd_unlock_config(host);
1328 outb(WBSD_CONF_ID_HI, config_ports[i]);
1329 id = inb(config_ports[i] + 1) << 8;
1331 outb(WBSD_CONF_ID_LO, config_ports[i]);
1332 id |= inb(config_ports[i] + 1);
1334 wbsd_lock_config(host);
1336 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1337 if (id == valid_ids[k]) {
1345 DBG("Unknown hardware (id %x) found at %x\n",
1346 id, config_ports[i]);
1350 release_region(config_ports[i], 2);
1354 host->unlock_code = 0;
1360 * Allocate/free io port ranges
1363 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1368 if (!request_region(base, 8, DRIVER_NAME))
1376 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1379 release_region(host->base, 8);
1384 release_region(host->config, 2);
1390 * Allocate/free DMA port and buffer
1393 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1398 if (request_dma(dma, DRIVER_NAME))
1402 * We need to allocate a special buffer in
1403 * order for ISA to be able to DMA to it.
1405 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1406 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1407 if (!host->dma_buffer)
1411 * Translate the address to a physical address.
1413 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1414 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1417 * ISA DMA must be aligned on a 64k basis.
1419 if ((host->dma_addr & 0xffff) != 0)
1422 * ISA cannot access memory above 16 MB.
1424 else if (host->dma_addr >= 0x1000000)
1433 * If we've gotten here then there is some kind of alignment bug
1437 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1438 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1439 host->dma_addr = (dma_addr_t)NULL;
1441 kfree(host->dma_buffer);
1442 host->dma_buffer = NULL;
1448 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1449 "Falling back on FIFO.\n", dma);
1452 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1454 if (host->dma_addr) {
1455 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1456 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1458 kfree(host->dma_buffer);
1460 free_dma(host->dma);
1463 host->dma_buffer = NULL;
1464 host->dma_addr = (dma_addr_t)NULL;
1468 * Allocate/free IRQ.
1471 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1476 * Allocate interrupt.
1479 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1488 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1489 (unsigned long)host);
1490 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1491 (unsigned long)host);
1492 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1493 (unsigned long)host);
1494 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1495 (unsigned long)host);
1496 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1497 (unsigned long)host);
1502 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1507 free_irq(host->irq, host);
1511 tasklet_kill(&host->card_tasklet);
1512 tasklet_kill(&host->fifo_tasklet);
1513 tasklet_kill(&host->crc_tasklet);
1514 tasklet_kill(&host->timeout_tasklet);
1515 tasklet_kill(&host->finish_tasklet);
1519 * Allocate all resources for the host.
1522 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1523 int base, int irq, int dma)
1528 * Allocate I/O ports.
1530 ret = wbsd_request_region(host, base);
1535 * Allocate interrupt.
1537 ret = wbsd_request_irq(host, irq);
1544 wbsd_request_dma(host, dma);
1550 * Release all resources for the host.
1553 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1555 wbsd_release_dma(host);
1556 wbsd_release_irq(host);
1557 wbsd_release_regions(host);
1561 * Configure the resources the chip should use.
1564 static void wbsd_chip_config(struct wbsd_host *host)
1566 wbsd_unlock_config(host);
1571 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1572 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1575 * Select SD/MMC function.
1577 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1580 * Set up card detection.
1582 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1587 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1588 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1590 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1593 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1596 * Enable and power up chip.
1598 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1599 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1601 wbsd_lock_config(host);
1605 * Check that configured resources are correct.
1608 static int wbsd_chip_validate(struct wbsd_host *host)
1612 wbsd_unlock_config(host);
1615 * Select SD/MMC function.
1617 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1620 * Read configuration.
1622 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1623 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1625 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1627 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1629 wbsd_lock_config(host);
1632 * Validate against given configuration.
1634 if (base != host->base)
1636 if (irq != host->irq)
1638 if ((dma != host->dma) && (host->dma != -1))
1645 * Powers down the SD function
1648 static void wbsd_chip_poweroff(struct wbsd_host *host)
1650 wbsd_unlock_config(host);
1652 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1653 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1655 wbsd_lock_config(host);
1658 /*****************************************************************************\
1660 * Devices setup and shutdown *
1662 \*****************************************************************************/
1664 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1667 struct wbsd_host *host = NULL;
1668 struct mmc_host *mmc = NULL;
1671 ret = wbsd_alloc_mmc(dev);
1675 mmc = dev_get_drvdata(dev);
1676 host = mmc_priv(mmc);
1679 * Scan for hardware.
1681 ret = wbsd_scan(host);
1683 if (pnp && (ret == -ENODEV)) {
1684 printk(KERN_WARNING DRIVER_NAME
1685 ": Unable to confirm device presence. You may "
1686 "experience lock-ups.\n");
1694 * Request resources.
1696 ret = wbsd_request_resources(host, base, irq, dma);
1698 wbsd_release_resources(host);
1704 * See if chip needs to be configured.
1707 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1708 printk(KERN_WARNING DRIVER_NAME
1709 ": PnP active but chip not configured! "
1710 "You probably have a buggy BIOS. "
1711 "Configuring chip manually.\n");
1712 wbsd_chip_config(host);
1715 wbsd_chip_config(host);
1718 * Power Management stuff. No idea how this works.
1723 wbsd_unlock_config(host);
1724 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1725 wbsd_lock_config(host);
1729 * Allow device to initialise itself properly.
1734 * Reset the chip into a known state.
1736 wbsd_init_device(host);
1740 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1741 if (host->chip_id != 0)
1742 printk(" id %x", (int)host->chip_id);
1743 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1745 printk(" dma %d", (int)host->dma);
1755 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1757 struct mmc_host *mmc = dev_get_drvdata(dev);
1758 struct wbsd_host *host;
1763 host = mmc_priv(mmc);
1765 mmc_remove_host(mmc);
1768 * Power down the SD/MMC function.
1771 wbsd_chip_poweroff(host);
1773 wbsd_release_resources(host);
1782 static int __devinit wbsd_probe(struct platform_device *dev)
1784 /* Use the module parameters for resources */
1785 return wbsd_init(&dev->dev, io, irq, dma, 0);
1788 static int __devexit wbsd_remove(struct platform_device *dev)
1790 wbsd_shutdown(&dev->dev, 0);
1801 static int __devinit
1802 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1807 * Get resources from PnP layer.
1809 io = pnp_port_start(pnpdev, 0);
1810 irq = pnp_irq(pnpdev, 0);
1811 if (pnp_dma_valid(pnpdev, 0))
1812 dma = pnp_dma(pnpdev, 0);
1816 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1818 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1821 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1823 wbsd_shutdown(&dev->dev, 1);
1826 #endif /* CONFIG_PNP */
1834 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1836 BUG_ON(host == NULL);
1838 return mmc_suspend_host(host->mmc, state);
1841 static int wbsd_resume(struct wbsd_host *host)
1843 BUG_ON(host == NULL);
1845 wbsd_init_device(host);
1847 return mmc_resume_host(host->mmc);
1850 static int wbsd_platform_suspend(struct platform_device *dev,
1853 struct mmc_host *mmc = platform_get_drvdata(dev);
1854 struct wbsd_host *host;
1860 DBGF("Suspending...\n");
1862 host = mmc_priv(mmc);
1864 ret = wbsd_suspend(host, state);
1868 wbsd_chip_poweroff(host);
1873 static int wbsd_platform_resume(struct platform_device *dev)
1875 struct mmc_host *mmc = platform_get_drvdata(dev);
1876 struct wbsd_host *host;
1881 DBGF("Resuming...\n");
1883 host = mmc_priv(mmc);
1885 wbsd_chip_config(host);
1888 * Allow device to initialise itself properly.
1892 return wbsd_resume(host);
1897 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1899 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1900 struct wbsd_host *host;
1905 DBGF("Suspending...\n");
1907 host = mmc_priv(mmc);
1909 return wbsd_suspend(host, state);
1912 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1914 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1915 struct wbsd_host *host;
1920 DBGF("Resuming...\n");
1922 host = mmc_priv(mmc);
1925 * See if chip needs to be configured.
1927 if (host->config != 0) {
1928 if (!wbsd_chip_validate(host)) {
1929 printk(KERN_WARNING DRIVER_NAME
1930 ": PnP active but chip not configured! "
1931 "You probably have a buggy BIOS. "
1932 "Configuring chip manually.\n");
1933 wbsd_chip_config(host);
1938 * Allow device to initialise itself properly.
1942 return wbsd_resume(host);
1945 #endif /* CONFIG_PNP */
1947 #else /* CONFIG_PM */
1949 #define wbsd_platform_suspend NULL
1950 #define wbsd_platform_resume NULL
1952 #define wbsd_pnp_suspend NULL
1953 #define wbsd_pnp_resume NULL
1955 #endif /* CONFIG_PM */
1957 static struct platform_device *wbsd_device;
1959 static struct platform_driver wbsd_driver = {
1960 .probe = wbsd_probe,
1961 .remove = __devexit_p(wbsd_remove),
1963 .suspend = wbsd_platform_suspend,
1964 .resume = wbsd_platform_resume,
1966 .name = DRIVER_NAME,
1972 static struct pnp_driver wbsd_pnp_driver = {
1973 .name = DRIVER_NAME,
1974 .id_table = pnp_dev_table,
1975 .probe = wbsd_pnp_probe,
1976 .remove = __devexit_p(wbsd_pnp_remove),
1978 .suspend = wbsd_pnp_suspend,
1979 .resume = wbsd_pnp_resume,
1982 #endif /* CONFIG_PNP */
1985 * Module loading/unloading
1988 static int __init wbsd_drv_init(void)
1992 printk(KERN_INFO DRIVER_NAME
1993 ": Winbond W83L51xD SD/MMC card interface driver\n");
1994 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1999 result = pnp_register_driver(&wbsd_pnp_driver);
2003 #endif /* CONFIG_PNP */
2006 result = platform_driver_register(&wbsd_driver);
2010 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2012 platform_driver_unregister(&wbsd_driver);
2016 result = platform_device_add(wbsd_device);
2018 platform_device_put(wbsd_device);
2019 platform_driver_unregister(&wbsd_driver);
2027 static void __exit wbsd_drv_exit(void)
2032 pnp_unregister_driver(&wbsd_pnp_driver);
2034 #endif /* CONFIG_PNP */
2037 platform_device_unregister(wbsd_device);
2039 platform_driver_unregister(&wbsd_driver);
2045 module_init(wbsd_drv_init);
2046 module_exit(wbsd_drv_exit);
2048 module_param(nopnp, uint, 0444);
2050 module_param(io, uint, 0444);
2051 module_param(irq, uint, 0444);
2052 module_param(dma, int, 0444);
2054 MODULE_LICENSE("GPL");
2055 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2056 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2059 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2061 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2062 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2063 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");