2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2006 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
14 * Changes to the FIFO system should be done with extreme care since
15 * the hardware is full of bugs related to the FIFO. Known issues are:
17 * - FIFO size field in FSR is always zero.
19 * - FIFO interrupts tend not to work as they should. Interrupts are
20 * triggered only for full/empty events, not for threshold values.
22 * - On APIC systems the FIFO empty interrupt is sometimes lost.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/delay.h>
33 #include <linux/pnp.h>
34 #include <linux/highmem.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
40 #include <asm/scatterlist.h>
44 #define DRIVER_NAME "wbsd"
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_CCRC;
182 ier |= WBSD_EINT_TIMEOUT;
183 ier |= WBSD_EINT_CRC;
186 outb(ier, host->base + WBSD_EIR);
191 inb(host->base + WBSD_ISR);
194 static void wbsd_reset(struct wbsd_host *host)
198 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
201 * Soft reset of chip (SD/MMC part).
203 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
204 setup |= WBSD_SOFT_RESET;
205 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
208 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
210 unsigned long dmaflags;
212 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
214 if (host->dma >= 0) {
216 * Release ISA DMA controller.
218 dmaflags = claim_dma_lock();
219 disable_dma(host->dma);
220 clear_dma_ff(host->dma);
221 release_dma_lock(dmaflags);
224 * Disable DMA on host.
226 wbsd_write_index(host, WBSD_IDX_DMA, 0);
232 * MMC layer might call back into the driver so first unlock.
234 spin_unlock(&host->lock);
235 mmc_request_done(host->mmc, mrq);
236 spin_lock(&host->lock);
240 * Scatter/gather functions
243 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
246 * Get info. about SG list from data structure.
248 host->cur_sg = data->sg;
249 host->num_sg = data->sg_len;
252 host->remain = host->cur_sg->length;
255 static inline int wbsd_next_sg(struct wbsd_host *host)
258 * Skip to next SG entry.
266 if (host->num_sg > 0) {
268 host->remain = host->cur_sg->length;
274 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
276 return page_address(host->cur_sg->page) + host->cur_sg->offset;
279 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
281 unsigned int len, i, size;
282 struct scatterlist *sg;
283 char *dmabuf = host->dma_buffer;
292 * Just loop through all entries. Size might not
293 * be the entire list though so make sure that
294 * we do not transfer too much.
296 for (i = 0; i < len; i++) {
297 sgbuf = page_address(sg[i].page) + sg[i].offset;
298 if (size < sg[i].length)
299 memcpy(dmabuf, sgbuf, size);
301 memcpy(dmabuf, sgbuf, sg[i].length);
302 dmabuf += sg[i].length;
304 if (size < sg[i].length)
307 size -= sg[i].length;
314 * Check that we didn't get a request to transfer
315 * more data than can fit into the SG list.
323 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
325 unsigned int len, i, size;
326 struct scatterlist *sg;
327 char *dmabuf = host->dma_buffer;
336 * Just loop through all entries. Size might not
337 * be the entire list though so make sure that
338 * we do not transfer too much.
340 for (i = 0; i < len; i++) {
341 sgbuf = page_address(sg[i].page) + sg[i].offset;
342 if (size < sg[i].length)
343 memcpy(sgbuf, dmabuf, size);
345 memcpy(sgbuf, dmabuf, sg[i].length);
346 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
347 dmabuf += sg[i].length;
349 if (size < sg[i].length)
352 size -= sg[i].length;
359 * Check that we didn't get a request to transfer
360 * more data than can fit into the SG list.
372 static inline void wbsd_get_short_reply(struct wbsd_host *host,
373 struct mmc_command *cmd)
376 * Correct response type?
378 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
379 cmd->error = MMC_ERR_INVALID;
383 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
386 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
387 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
390 static inline void wbsd_get_long_reply(struct wbsd_host *host,
391 struct mmc_command *cmd)
396 * Correct response type?
398 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
399 cmd->error = MMC_ERR_INVALID;
403 for (i = 0; i < 4; i++) {
405 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
407 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
409 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
411 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
415 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
420 DBGF("Sending cmd (%x)\n", cmd->opcode);
423 * Clear accumulated ISR. The interrupt routine
424 * will fill this one with events that occur during
430 * Send the command (CRC calculated by host).
432 outb(cmd->opcode, host->base + WBSD_CMDR);
433 for (i = 3; i >= 0; i--)
434 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
436 cmd->error = MMC_ERR_NONE;
439 * Wait for the request to complete.
442 status = wbsd_read_index(host, WBSD_IDX_STATUS);
443 } while (status & WBSD_CARDTRAFFIC);
446 * Do we expect a reply?
448 if (cmd->flags & MMC_RSP_PRESENT) {
455 if (isr & WBSD_INT_CARD)
456 cmd->error = MMC_ERR_TIMEOUT;
458 else if (isr & WBSD_INT_TIMEOUT)
459 cmd->error = MMC_ERR_TIMEOUT;
461 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
462 cmd->error = MMC_ERR_BADCRC;
465 if (cmd->flags & MMC_RSP_136)
466 wbsd_get_long_reply(host, cmd);
468 wbsd_get_short_reply(host, cmd);
472 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
479 static void wbsd_empty_fifo(struct wbsd_host *host)
481 struct mmc_data *data = host->mrq->cmd->data;
486 * Handle excessive data.
488 if (data->bytes_xfered == host->size)
491 buffer = wbsd_sg_to_buffer(host) + host->offset;
494 * Drain the fifo. This has a tendency to loop longer
495 * than the FIFO length (usually one block).
497 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
499 * The size field in the FSR is broken so we have to
502 if (fsr & WBSD_FIFO_FULL)
504 else if (fsr & WBSD_FIFO_FUTHRE)
509 for (i = 0; i < fifo; i++) {
510 *buffer = inb(host->base + WBSD_DFR);
515 data->bytes_xfered++;
520 if (data->bytes_xfered == host->size)
524 * End of scatter list entry?
526 if (host->remain == 0) {
528 * Get next entry. Check if last.
530 if (!wbsd_next_sg(host)) {
532 * We should never reach this point.
533 * It means that we're trying to
534 * transfer more blocks than can fit
535 * into the scatter list.
539 host->size = data->bytes_xfered;
544 buffer = wbsd_sg_to_buffer(host);
550 * This is a very dirty hack to solve a
551 * hardware problem. The chip doesn't trigger
552 * FIFO threshold interrupts properly.
554 if ((host->size - data->bytes_xfered) < 16)
555 tasklet_schedule(&host->fifo_tasklet);
558 static void wbsd_fill_fifo(struct wbsd_host *host)
560 struct mmc_data *data = host->mrq->cmd->data;
565 * Check that we aren't being called after the
566 * entire buffer has been transfered.
568 if (data->bytes_xfered == host->size)
571 buffer = wbsd_sg_to_buffer(host) + host->offset;
574 * Fill the fifo. This has a tendency to loop longer
575 * than the FIFO length (usually one block).
577 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
579 * The size field in the FSR is broken so we have to
582 if (fsr & WBSD_FIFO_EMPTY)
584 else if (fsr & WBSD_FIFO_EMTHRE)
589 for (i = 16; i > fifo; i--) {
590 outb(*buffer, host->base + WBSD_DFR);
595 data->bytes_xfered++;
600 if (data->bytes_xfered == host->size)
604 * End of scatter list entry?
606 if (host->remain == 0) {
608 * Get next entry. Check if last.
610 if (!wbsd_next_sg(host)) {
612 * We should never reach this point.
613 * It means that we're trying to
614 * transfer more blocks than can fit
615 * into the scatter list.
619 host->size = data->bytes_xfered;
624 buffer = wbsd_sg_to_buffer(host);
630 * The controller stops sending interrupts for
631 * 'FIFO empty' under certain conditions. So we
632 * need to be a bit more pro-active.
634 tasklet_schedule(&host->fifo_tasklet);
637 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
641 unsigned long dmaflags;
643 DBGF("blksz %04x blks %04x flags %08x\n",
644 data->blksz, data->blocks, data->flags);
645 DBGF("tsac %d ms nsac %d clk\n",
646 data->timeout_ns / 1000000, data->timeout_clks);
651 host->size = data->blocks * data->blksz;
654 * Check timeout values for overflow.
655 * (Yes, some cards cause this value to overflow).
657 if (data->timeout_ns > 127000000)
658 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
660 wbsd_write_index(host, WBSD_IDX_TAAC,
661 data->timeout_ns / 1000000);
664 if (data->timeout_clks > 255)
665 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
667 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
670 * Inform the chip of how large blocks will be
671 * sent. It needs this to determine when to
674 * Space for CRC must be included in the size.
675 * Two bytes are needed for each data line.
677 if (host->bus_width == MMC_BUS_WIDTH_1) {
678 blksize = data->blksz + 2;
680 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
681 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
682 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
683 blksize = data->blksz + 2 * 4;
685 wbsd_write_index(host, WBSD_IDX_PBSMSB,
686 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
687 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
689 data->error = MMC_ERR_INVALID;
694 * Clear the FIFO. This is needed even for DMA
695 * transfers since the chip still uses the FIFO
698 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
699 setup |= WBSD_FIFO_RESET;
700 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
705 if (host->dma >= 0) {
707 * The buffer for DMA is only 64 kB.
709 BUG_ON(host->size > 0x10000);
710 if (host->size > 0x10000) {
711 data->error = MMC_ERR_INVALID;
716 * Transfer data from the SG list to
719 if (data->flags & MMC_DATA_WRITE)
720 wbsd_sg_to_dma(host, data);
723 * Initialise the ISA DMA controller.
725 dmaflags = claim_dma_lock();
726 disable_dma(host->dma);
727 clear_dma_ff(host->dma);
728 if (data->flags & MMC_DATA_READ)
729 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
731 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
732 set_dma_addr(host->dma, host->dma_addr);
733 set_dma_count(host->dma, host->size);
735 enable_dma(host->dma);
736 release_dma_lock(dmaflags);
739 * Enable DMA on the host.
741 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
744 * This flag is used to keep printk
745 * output to a minimum.
750 * Initialise the SG list.
752 wbsd_init_sg(host, data);
757 wbsd_write_index(host, WBSD_IDX_DMA, 0);
760 * Set up FIFO threshold levels (and fill
761 * buffer if doing a write).
763 if (data->flags & MMC_DATA_READ) {
764 wbsd_write_index(host, WBSD_IDX_FIFOEN,
765 WBSD_FIFOEN_FULL | 8);
767 wbsd_write_index(host, WBSD_IDX_FIFOEN,
768 WBSD_FIFOEN_EMPTY | 8);
769 wbsd_fill_fifo(host);
773 data->error = MMC_ERR_NONE;
776 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
778 unsigned long dmaflags;
782 WARN_ON(host->mrq == NULL);
785 * Send a stop command if needed.
788 wbsd_send_command(host, data->stop);
791 * Wait for the controller to leave data
795 status = wbsd_read_index(host, WBSD_IDX_STATUS);
796 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
801 if (host->dma >= 0) {
803 * Disable DMA on the host.
805 wbsd_write_index(host, WBSD_IDX_DMA, 0);
808 * Turn of ISA DMA controller.
810 dmaflags = claim_dma_lock();
811 disable_dma(host->dma);
812 clear_dma_ff(host->dma);
813 count = get_dma_residue(host->dma);
814 release_dma_lock(dmaflags);
820 printk(KERN_ERR "%s: Incomplete DMA transfer. "
822 mmc_hostname(host->mmc), count);
824 data->error = MMC_ERR_FAILED;
827 * Transfer data from DMA buffer to
830 if (data->flags & MMC_DATA_READ)
831 wbsd_dma_to_sg(host, data);
833 data->bytes_xfered = host->size;
837 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
839 wbsd_request_end(host, host->mrq);
842 /*****************************************************************************\
844 * MMC layer callbacks *
846 \*****************************************************************************/
848 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
850 struct wbsd_host *host = mmc_priv(mmc);
851 struct mmc_command *cmd;
854 * Disable tasklets to avoid a deadlock.
856 spin_lock_bh(&host->lock);
858 BUG_ON(host->mrq != NULL);
865 * If there is no card in the slot then
866 * timeout immediatly.
868 if (!(host->flags & WBSD_FCARD_PRESENT)) {
869 cmd->error = MMC_ERR_TIMEOUT;
874 * Does the request include data?
877 wbsd_prepare_data(host, cmd->data);
879 if (cmd->data->error != MMC_ERR_NONE)
883 wbsd_send_command(host, cmd);
886 * If this is a data transfer the request
887 * will be finished after the data has
890 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
892 * The hardware is so delightfully stupid that it has a list
893 * of "data" commands. If a command isn't on this list, it'll
894 * just go back to the idle state and won't send any data
897 switch (cmd->opcode) {
911 /* ACMDs. We don't keep track of state, so we just treat them
912 * like any other command. */
917 #ifdef CONFIG_MMC_DEBUG
918 printk(KERN_WARNING "%s: Data command %d is not "
919 "supported by this controller.\n",
920 mmc_hostname(host->mmc), cmd->opcode);
922 cmd->data->error = MMC_ERR_INVALID;
925 wbsd_send_command(host, cmd->data->stop);
931 * Dirty fix for hardware bug.
934 tasklet_schedule(&host->fifo_tasklet);
936 spin_unlock_bh(&host->lock);
942 wbsd_request_end(host, mrq);
944 spin_unlock_bh(&host->lock);
947 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
949 struct wbsd_host *host = mmc_priv(mmc);
952 spin_lock_bh(&host->lock);
955 * Reset the chip on each power off.
956 * Should clear out any weird states.
958 if (ios->power_mode == MMC_POWER_OFF)
959 wbsd_init_device(host);
961 if (ios->clock >= 24000000)
963 else if (ios->clock >= 16000000)
965 else if (ios->clock >= 12000000)
971 * Only write to the clock register when
972 * there is an actual change.
974 if (clk != host->clk) {
975 wbsd_write_index(host, WBSD_IDX_CLK, clk);
982 if (ios->power_mode != MMC_POWER_OFF) {
983 pwr = inb(host->base + WBSD_CSR);
984 pwr &= ~WBSD_POWER_N;
985 outb(pwr, host->base + WBSD_CSR);
989 * MMC cards need to have pin 1 high during init.
990 * It wreaks havoc with the card detection though so
991 * that needs to be disabled.
993 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
994 if (ios->chip_select == MMC_CS_HIGH) {
995 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
996 setup |= WBSD_DAT3_H;
997 host->flags |= WBSD_FIGNORE_DETECT;
999 if (setup & WBSD_DAT3_H) {
1000 setup &= ~WBSD_DAT3_H;
1003 * We cannot resume card detection immediatly
1004 * because of capacitance and delays in the chip.
1006 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1009 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1012 * Store bus width for later. Will be used when
1013 * setting up the data transfer.
1015 host->bus_width = ios->bus_width;
1017 spin_unlock_bh(&host->lock);
1020 static int wbsd_get_ro(struct mmc_host *mmc)
1022 struct wbsd_host *host = mmc_priv(mmc);
1025 spin_lock_bh(&host->lock);
1027 csr = inb(host->base + WBSD_CSR);
1029 outb(csr, host->base + WBSD_CSR);
1033 csr = inb(host->base + WBSD_CSR);
1035 outb(csr, host->base + WBSD_CSR);
1037 spin_unlock_bh(&host->lock);
1039 return csr & WBSD_WRPT;
1042 static const struct mmc_host_ops wbsd_ops = {
1043 .request = wbsd_request,
1044 .set_ios = wbsd_set_ios,
1045 .get_ro = wbsd_get_ro,
1048 /*****************************************************************************\
1050 * Interrupt handling *
1052 \*****************************************************************************/
1055 * Helper function to reset detection ignore
1058 static void wbsd_reset_ignore(unsigned long data)
1060 struct wbsd_host *host = (struct wbsd_host *)data;
1062 BUG_ON(host == NULL);
1064 DBG("Resetting card detection ignore\n");
1066 spin_lock_bh(&host->lock);
1068 host->flags &= ~WBSD_FIGNORE_DETECT;
1071 * Card status might have changed during the
1074 tasklet_schedule(&host->card_tasklet);
1076 spin_unlock_bh(&host->lock);
1083 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1085 WARN_ON(!host->mrq);
1089 WARN_ON(!host->mrq->cmd);
1090 if (!host->mrq->cmd)
1093 WARN_ON(!host->mrq->cmd->data);
1094 if (!host->mrq->cmd->data)
1097 return host->mrq->cmd->data;
1100 static void wbsd_tasklet_card(unsigned long param)
1102 struct wbsd_host *host = (struct wbsd_host *)param;
1106 spin_lock(&host->lock);
1108 if (host->flags & WBSD_FIGNORE_DETECT) {
1109 spin_unlock(&host->lock);
1113 csr = inb(host->base + WBSD_CSR);
1114 WARN_ON(csr == 0xff);
1116 if (csr & WBSD_CARDPRESENT) {
1117 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1118 DBG("Card inserted\n");
1119 host->flags |= WBSD_FCARD_PRESENT;
1123 } else if (host->flags & WBSD_FCARD_PRESENT) {
1124 DBG("Card removed\n");
1125 host->flags &= ~WBSD_FCARD_PRESENT;
1128 printk(KERN_ERR "%s: Card removed during transfer!\n",
1129 mmc_hostname(host->mmc));
1132 host->mrq->cmd->error = MMC_ERR_FAILED;
1133 tasklet_schedule(&host->finish_tasklet);
1140 * Unlock first since we might get a call back.
1143 spin_unlock(&host->lock);
1146 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1149 static void wbsd_tasklet_fifo(unsigned long param)
1151 struct wbsd_host *host = (struct wbsd_host *)param;
1152 struct mmc_data *data;
1154 spin_lock(&host->lock);
1159 data = wbsd_get_data(host);
1163 if (data->flags & MMC_DATA_WRITE)
1164 wbsd_fill_fifo(host);
1166 wbsd_empty_fifo(host);
1171 if (host->size == data->bytes_xfered) {
1172 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1173 tasklet_schedule(&host->finish_tasklet);
1177 spin_unlock(&host->lock);
1180 static void wbsd_tasklet_crc(unsigned long param)
1182 struct wbsd_host *host = (struct wbsd_host *)param;
1183 struct mmc_data *data;
1185 spin_lock(&host->lock);
1190 data = wbsd_get_data(host);
1194 DBGF("CRC error\n");
1196 data->error = MMC_ERR_BADCRC;
1198 tasklet_schedule(&host->finish_tasklet);
1201 spin_unlock(&host->lock);
1204 static void wbsd_tasklet_timeout(unsigned long param)
1206 struct wbsd_host *host = (struct wbsd_host *)param;
1207 struct mmc_data *data;
1209 spin_lock(&host->lock);
1214 data = wbsd_get_data(host);
1220 data->error = MMC_ERR_TIMEOUT;
1222 tasklet_schedule(&host->finish_tasklet);
1225 spin_unlock(&host->lock);
1228 static void wbsd_tasklet_finish(unsigned long param)
1230 struct wbsd_host *host = (struct wbsd_host *)param;
1231 struct mmc_data *data;
1233 spin_lock(&host->lock);
1235 WARN_ON(!host->mrq);
1239 data = wbsd_get_data(host);
1243 wbsd_finish_data(host, data);
1246 spin_unlock(&host->lock);
1249 static void wbsd_tasklet_block(unsigned long param)
1251 struct wbsd_host *host = (struct wbsd_host *)param;
1252 struct mmc_data *data;
1254 spin_lock(&host->lock);
1256 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1258 data = wbsd_get_data(host);
1262 DBGF("CRC error\n");
1264 data->error = MMC_ERR_BADCRC;
1266 tasklet_schedule(&host->finish_tasklet);
1270 spin_unlock(&host->lock);
1274 * Interrupt handling
1277 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1279 struct wbsd_host *host = dev_id;
1282 isr = inb(host->base + WBSD_ISR);
1285 * Was it actually our hardware that caused the interrupt?
1287 if (isr == 0xff || isr == 0x00)
1293 * Schedule tasklets as needed.
1295 if (isr & WBSD_INT_CARD)
1296 tasklet_schedule(&host->card_tasklet);
1297 if (isr & WBSD_INT_FIFO_THRE)
1298 tasklet_schedule(&host->fifo_tasklet);
1299 if (isr & WBSD_INT_CRC)
1300 tasklet_hi_schedule(&host->crc_tasklet);
1301 if (isr & WBSD_INT_TIMEOUT)
1302 tasklet_hi_schedule(&host->timeout_tasklet);
1303 if (isr & WBSD_INT_BUSYEND)
1304 tasklet_hi_schedule(&host->block_tasklet);
1305 if (isr & WBSD_INT_TC)
1306 tasklet_schedule(&host->finish_tasklet);
1311 /*****************************************************************************\
1313 * Device initialisation and shutdown *
1315 \*****************************************************************************/
1318 * Allocate/free MMC structure.
1321 static int __devinit wbsd_alloc_mmc(struct device *dev)
1323 struct mmc_host *mmc;
1324 struct wbsd_host *host;
1327 * Allocate MMC structure.
1329 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1333 host = mmc_priv(mmc);
1339 * Set host parameters.
1341 mmc->ops = &wbsd_ops;
1342 mmc->f_min = 375000;
1343 mmc->f_max = 24000000;
1344 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1345 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1347 spin_lock_init(&host->lock);
1352 init_timer(&host->ignore_timer);
1353 host->ignore_timer.data = (unsigned long)host;
1354 host->ignore_timer.function = wbsd_reset_ignore;
1357 * Maximum number of segments. Worst case is one sector per segment
1358 * so this will be 64kB/512.
1360 mmc->max_hw_segs = 128;
1361 mmc->max_phys_segs = 128;
1364 * Maximum request size. Also limited by 64KiB buffer.
1366 mmc->max_req_size = 65536;
1369 * Maximum segment size. Could be one segment with the maximum number
1372 mmc->max_seg_size = mmc->max_req_size;
1375 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1376 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1378 mmc->max_blk_size = 4087;
1381 * Maximum block count. There is no real limit so the maximum
1382 * request size will be the only restriction.
1384 mmc->max_blk_count = mmc->max_req_size;
1386 dev_set_drvdata(dev, mmc);
1391 static void __devexit wbsd_free_mmc(struct device *dev)
1393 struct mmc_host *mmc;
1394 struct wbsd_host *host;
1396 mmc = dev_get_drvdata(dev);
1400 host = mmc_priv(mmc);
1401 BUG_ON(host == NULL);
1403 del_timer_sync(&host->ignore_timer);
1407 dev_set_drvdata(dev, NULL);
1411 * Scan for known chip id:s
1414 static int __devinit wbsd_scan(struct wbsd_host *host)
1420 * Iterate through all ports, all codes to
1421 * find hardware that is in our known list.
1423 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1424 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1427 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1430 host->config = config_ports[i];
1431 host->unlock_code = unlock_codes[j];
1433 wbsd_unlock_config(host);
1435 outb(WBSD_CONF_ID_HI, config_ports[i]);
1436 id = inb(config_ports[i] + 1) << 8;
1438 outb(WBSD_CONF_ID_LO, config_ports[i]);
1439 id |= inb(config_ports[i] + 1);
1441 wbsd_lock_config(host);
1443 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1444 if (id == valid_ids[k]) {
1452 DBG("Unknown hardware (id %x) found at %x\n",
1453 id, config_ports[i]);
1457 release_region(config_ports[i], 2);
1461 host->unlock_code = 0;
1467 * Allocate/free io port ranges
1470 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1475 if (!request_region(base, 8, DRIVER_NAME))
1483 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1486 release_region(host->base, 8);
1491 release_region(host->config, 2);
1497 * Allocate/free DMA port and buffer
1500 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1505 if (request_dma(dma, DRIVER_NAME))
1509 * We need to allocate a special buffer in
1510 * order for ISA to be able to DMA to it.
1512 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1513 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1514 if (!host->dma_buffer)
1518 * Translate the address to a physical address.
1520 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1521 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1524 * ISA DMA must be aligned on a 64k basis.
1526 if ((host->dma_addr & 0xffff) != 0)
1529 * ISA cannot access memory above 16 MB.
1531 else if (host->dma_addr >= 0x1000000)
1540 * If we've gotten here then there is some kind of alignment bug
1544 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1545 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1546 host->dma_addr = (dma_addr_t)NULL;
1548 kfree(host->dma_buffer);
1549 host->dma_buffer = NULL;
1555 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1556 "Falling back on FIFO.\n", dma);
1559 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1561 if (host->dma_addr) {
1562 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1563 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1565 kfree(host->dma_buffer);
1567 free_dma(host->dma);
1570 host->dma_buffer = NULL;
1571 host->dma_addr = (dma_addr_t)NULL;
1575 * Allocate/free IRQ.
1578 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1583 * Allocate interrupt.
1586 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1595 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1596 (unsigned long)host);
1597 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1598 (unsigned long)host);
1599 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1600 (unsigned long)host);
1601 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1602 (unsigned long)host);
1603 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1604 (unsigned long)host);
1605 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1606 (unsigned long)host);
1611 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1616 free_irq(host->irq, host);
1620 tasklet_kill(&host->card_tasklet);
1621 tasklet_kill(&host->fifo_tasklet);
1622 tasklet_kill(&host->crc_tasklet);
1623 tasklet_kill(&host->timeout_tasklet);
1624 tasklet_kill(&host->finish_tasklet);
1625 tasklet_kill(&host->block_tasklet);
1629 * Allocate all resources for the host.
1632 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1633 int base, int irq, int dma)
1638 * Allocate I/O ports.
1640 ret = wbsd_request_region(host, base);
1645 * Allocate interrupt.
1647 ret = wbsd_request_irq(host, irq);
1654 wbsd_request_dma(host, dma);
1660 * Release all resources for the host.
1663 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1665 wbsd_release_dma(host);
1666 wbsd_release_irq(host);
1667 wbsd_release_regions(host);
1671 * Configure the resources the chip should use.
1674 static void wbsd_chip_config(struct wbsd_host *host)
1676 wbsd_unlock_config(host);
1681 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1682 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1685 * Select SD/MMC function.
1687 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1690 * Set up card detection.
1692 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1697 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1698 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1700 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1703 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1706 * Enable and power up chip.
1708 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1709 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1711 wbsd_lock_config(host);
1715 * Check that configured resources are correct.
1718 static int wbsd_chip_validate(struct wbsd_host *host)
1722 wbsd_unlock_config(host);
1725 * Select SD/MMC function.
1727 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1730 * Read configuration.
1732 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1733 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1735 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1737 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1739 wbsd_lock_config(host);
1742 * Validate against given configuration.
1744 if (base != host->base)
1746 if (irq != host->irq)
1748 if ((dma != host->dma) && (host->dma != -1))
1755 * Powers down the SD function
1758 static void wbsd_chip_poweroff(struct wbsd_host *host)
1760 wbsd_unlock_config(host);
1762 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1763 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1765 wbsd_lock_config(host);
1768 /*****************************************************************************\
1770 * Devices setup and shutdown *
1772 \*****************************************************************************/
1774 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1777 struct wbsd_host *host = NULL;
1778 struct mmc_host *mmc = NULL;
1781 ret = wbsd_alloc_mmc(dev);
1785 mmc = dev_get_drvdata(dev);
1786 host = mmc_priv(mmc);
1789 * Scan for hardware.
1791 ret = wbsd_scan(host);
1793 if (pnp && (ret == -ENODEV)) {
1794 printk(KERN_WARNING DRIVER_NAME
1795 ": Unable to confirm device presence. You may "
1796 "experience lock-ups.\n");
1804 * Request resources.
1806 ret = wbsd_request_resources(host, base, irq, dma);
1808 wbsd_release_resources(host);
1814 * See if chip needs to be configured.
1817 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1818 printk(KERN_WARNING DRIVER_NAME
1819 ": PnP active but chip not configured! "
1820 "You probably have a buggy BIOS. "
1821 "Configuring chip manually.\n");
1822 wbsd_chip_config(host);
1825 wbsd_chip_config(host);
1828 * Power Management stuff. No idea how this works.
1833 wbsd_unlock_config(host);
1834 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1835 wbsd_lock_config(host);
1839 * Allow device to initialise itself properly.
1844 * Reset the chip into a known state.
1846 wbsd_init_device(host);
1850 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1851 if (host->chip_id != 0)
1852 printk(" id %x", (int)host->chip_id);
1853 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1855 printk(" dma %d", (int)host->dma);
1865 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1867 struct mmc_host *mmc = dev_get_drvdata(dev);
1868 struct wbsd_host *host;
1873 host = mmc_priv(mmc);
1875 mmc_remove_host(mmc);
1878 * Power down the SD/MMC function.
1881 wbsd_chip_poweroff(host);
1883 wbsd_release_resources(host);
1892 static int __devinit wbsd_probe(struct platform_device *dev)
1894 /* Use the module parameters for resources */
1895 return wbsd_init(&dev->dev, io, irq, dma, 0);
1898 static int __devexit wbsd_remove(struct platform_device *dev)
1900 wbsd_shutdown(&dev->dev, 0);
1911 static int __devinit
1912 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1917 * Get resources from PnP layer.
1919 io = pnp_port_start(pnpdev, 0);
1920 irq = pnp_irq(pnpdev, 0);
1921 if (pnp_dma_valid(pnpdev, 0))
1922 dma = pnp_dma(pnpdev, 0);
1926 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1928 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1931 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1933 wbsd_shutdown(&dev->dev, 1);
1936 #endif /* CONFIG_PNP */
1944 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1946 BUG_ON(host == NULL);
1948 return mmc_suspend_host(host->mmc, state);
1951 static int wbsd_resume(struct wbsd_host *host)
1953 BUG_ON(host == NULL);
1955 wbsd_init_device(host);
1957 return mmc_resume_host(host->mmc);
1960 static int wbsd_platform_suspend(struct platform_device *dev,
1963 struct mmc_host *mmc = platform_get_drvdata(dev);
1964 struct wbsd_host *host;
1970 DBGF("Suspending...\n");
1972 host = mmc_priv(mmc);
1974 ret = wbsd_suspend(host, state);
1978 wbsd_chip_poweroff(host);
1983 static int wbsd_platform_resume(struct platform_device *dev)
1985 struct mmc_host *mmc = platform_get_drvdata(dev);
1986 struct wbsd_host *host;
1991 DBGF("Resuming...\n");
1993 host = mmc_priv(mmc);
1995 wbsd_chip_config(host);
1998 * Allow device to initialise itself properly.
2002 return wbsd_resume(host);
2007 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2009 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2010 struct wbsd_host *host;
2015 DBGF("Suspending...\n");
2017 host = mmc_priv(mmc);
2019 return wbsd_suspend(host, state);
2022 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2024 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2025 struct wbsd_host *host;
2030 DBGF("Resuming...\n");
2032 host = mmc_priv(mmc);
2035 * See if chip needs to be configured.
2037 if (host->config != 0) {
2038 if (!wbsd_chip_validate(host)) {
2039 printk(KERN_WARNING DRIVER_NAME
2040 ": PnP active but chip not configured! "
2041 "You probably have a buggy BIOS. "
2042 "Configuring chip manually.\n");
2043 wbsd_chip_config(host);
2048 * Allow device to initialise itself properly.
2052 return wbsd_resume(host);
2055 #endif /* CONFIG_PNP */
2057 #else /* CONFIG_PM */
2059 #define wbsd_platform_suspend NULL
2060 #define wbsd_platform_resume NULL
2062 #define wbsd_pnp_suspend NULL
2063 #define wbsd_pnp_resume NULL
2065 #endif /* CONFIG_PM */
2067 static struct platform_device *wbsd_device;
2069 static struct platform_driver wbsd_driver = {
2070 .probe = wbsd_probe,
2071 .remove = __devexit_p(wbsd_remove),
2073 .suspend = wbsd_platform_suspend,
2074 .resume = wbsd_platform_resume,
2076 .name = DRIVER_NAME,
2082 static struct pnp_driver wbsd_pnp_driver = {
2083 .name = DRIVER_NAME,
2084 .id_table = pnp_dev_table,
2085 .probe = wbsd_pnp_probe,
2086 .remove = __devexit_p(wbsd_pnp_remove),
2088 .suspend = wbsd_pnp_suspend,
2089 .resume = wbsd_pnp_resume,
2092 #endif /* CONFIG_PNP */
2095 * Module loading/unloading
2098 static int __init wbsd_drv_init(void)
2102 printk(KERN_INFO DRIVER_NAME
2103 ": Winbond W83L51xD SD/MMC card interface driver\n");
2104 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2109 result = pnp_register_driver(&wbsd_pnp_driver);
2113 #endif /* CONFIG_PNP */
2116 result = platform_driver_register(&wbsd_driver);
2120 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2122 platform_driver_unregister(&wbsd_driver);
2126 result = platform_device_add(wbsd_device);
2128 platform_device_put(wbsd_device);
2129 platform_driver_unregister(&wbsd_driver);
2137 static void __exit wbsd_drv_exit(void)
2142 pnp_unregister_driver(&wbsd_pnp_driver);
2144 #endif /* CONFIG_PNP */
2147 platform_device_unregister(wbsd_device);
2149 platform_driver_unregister(&wbsd_driver);
2155 module_init(wbsd_drv_init);
2156 module_exit(wbsd_drv_exit);
2158 module_param(nopnp, uint, 0444);
2160 module_param(io, uint, 0444);
2161 module_param(irq, uint, 0444);
2162 module_param(dma, int, 0444);
2164 MODULE_LICENSE("GPL");
2165 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2166 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2169 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2171 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2172 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2173 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");