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 dmabuf += sg[i].length;
348 if (size < sg[i].length)
351 size -= sg[i].length;
358 * Check that we didn't get a request to transfer
359 * more data than can fit into the SG list.
371 static inline void wbsd_get_short_reply(struct wbsd_host *host,
372 struct mmc_command *cmd)
375 * Correct response type?
377 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
378 cmd->error = MMC_ERR_INVALID;
382 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
383 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
386 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
389 static inline void wbsd_get_long_reply(struct wbsd_host *host,
390 struct mmc_command *cmd)
395 * Correct response type?
397 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
398 cmd->error = MMC_ERR_INVALID;
402 for (i = 0; i < 4; i++) {
404 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
406 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
408 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
410 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
414 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
419 DBGF("Sending cmd (%x)\n", cmd->opcode);
422 * Clear accumulated ISR. The interrupt routine
423 * will fill this one with events that occur during
429 * Send the command (CRC calculated by host).
431 outb(cmd->opcode, host->base + WBSD_CMDR);
432 for (i = 3; i >= 0; i--)
433 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
435 cmd->error = MMC_ERR_NONE;
438 * Wait for the request to complete.
441 status = wbsd_read_index(host, WBSD_IDX_STATUS);
442 } while (status & WBSD_CARDTRAFFIC);
445 * Do we expect a reply?
447 if (cmd->flags & MMC_RSP_PRESENT) {
454 if (isr & WBSD_INT_CARD)
455 cmd->error = MMC_ERR_TIMEOUT;
457 else if (isr & WBSD_INT_TIMEOUT)
458 cmd->error = MMC_ERR_TIMEOUT;
460 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
461 cmd->error = MMC_ERR_BADCRC;
464 if (cmd->flags & MMC_RSP_136)
465 wbsd_get_long_reply(host, cmd);
467 wbsd_get_short_reply(host, cmd);
471 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
478 static void wbsd_empty_fifo(struct wbsd_host *host)
480 struct mmc_data *data = host->mrq->cmd->data;
485 * Handle excessive data.
487 if (data->bytes_xfered == host->size)
490 buffer = wbsd_sg_to_buffer(host) + host->offset;
493 * Drain the fifo. This has a tendency to loop longer
494 * than the FIFO length (usually one block).
496 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
498 * The size field in the FSR is broken so we have to
501 if (fsr & WBSD_FIFO_FULL)
503 else if (fsr & WBSD_FIFO_FUTHRE)
508 for (i = 0; i < fifo; i++) {
509 *buffer = inb(host->base + WBSD_DFR);
514 data->bytes_xfered++;
519 if (data->bytes_xfered == host->size)
523 * End of scatter list entry?
525 if (host->remain == 0) {
527 * Get next entry. Check if last.
529 if (!wbsd_next_sg(host)) {
531 * We should never reach this point.
532 * It means that we're trying to
533 * transfer more blocks than can fit
534 * into the scatter list.
538 host->size = data->bytes_xfered;
543 buffer = wbsd_sg_to_buffer(host);
549 * This is a very dirty hack to solve a
550 * hardware problem. The chip doesn't trigger
551 * FIFO threshold interrupts properly.
553 if ((host->size - data->bytes_xfered) < 16)
554 tasklet_schedule(&host->fifo_tasklet);
557 static void wbsd_fill_fifo(struct wbsd_host *host)
559 struct mmc_data *data = host->mrq->cmd->data;
564 * Check that we aren't being called after the
565 * entire buffer has been transfered.
567 if (data->bytes_xfered == host->size)
570 buffer = wbsd_sg_to_buffer(host) + host->offset;
573 * Fill the fifo. This has a tendency to loop longer
574 * than the FIFO length (usually one block).
576 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
578 * The size field in the FSR is broken so we have to
581 if (fsr & WBSD_FIFO_EMPTY)
583 else if (fsr & WBSD_FIFO_EMTHRE)
588 for (i = 16; i > fifo; i--) {
589 outb(*buffer, host->base + WBSD_DFR);
594 data->bytes_xfered++;
599 if (data->bytes_xfered == host->size)
603 * End of scatter list entry?
605 if (host->remain == 0) {
607 * Get next entry. Check if last.
609 if (!wbsd_next_sg(host)) {
611 * We should never reach this point.
612 * It means that we're trying to
613 * transfer more blocks than can fit
614 * into the scatter list.
618 host->size = data->bytes_xfered;
623 buffer = wbsd_sg_to_buffer(host);
629 * The controller stops sending interrupts for
630 * 'FIFO empty' under certain conditions. So we
631 * need to be a bit more pro-active.
633 tasklet_schedule(&host->fifo_tasklet);
636 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
640 unsigned long dmaflags;
642 DBGF("blksz %04x blks %04x flags %08x\n",
643 data->blksz, data->blocks, data->flags);
644 DBGF("tsac %d ms nsac %d clk\n",
645 data->timeout_ns / 1000000, data->timeout_clks);
650 host->size = data->blocks * data->blksz;
653 * Check timeout values for overflow.
654 * (Yes, some cards cause this value to overflow).
656 if (data->timeout_ns > 127000000)
657 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
659 wbsd_write_index(host, WBSD_IDX_TAAC,
660 data->timeout_ns / 1000000);
663 if (data->timeout_clks > 255)
664 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
666 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
669 * Inform the chip of how large blocks will be
670 * sent. It needs this to determine when to
673 * Space for CRC must be included in the size.
674 * Two bytes are needed for each data line.
676 if (host->bus_width == MMC_BUS_WIDTH_1) {
677 blksize = data->blksz + 2;
679 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
680 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
681 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
682 blksize = data->blksz + 2 * 4;
684 wbsd_write_index(host, WBSD_IDX_PBSMSB,
685 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
686 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
688 data->error = MMC_ERR_INVALID;
693 * Clear the FIFO. This is needed even for DMA
694 * transfers since the chip still uses the FIFO
697 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
698 setup |= WBSD_FIFO_RESET;
699 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
704 if (host->dma >= 0) {
706 * The buffer for DMA is only 64 kB.
708 BUG_ON(host->size > 0x10000);
709 if (host->size > 0x10000) {
710 data->error = MMC_ERR_INVALID;
715 * Transfer data from the SG list to
718 if (data->flags & MMC_DATA_WRITE)
719 wbsd_sg_to_dma(host, data);
722 * Initialise the ISA DMA controller.
724 dmaflags = claim_dma_lock();
725 disable_dma(host->dma);
726 clear_dma_ff(host->dma);
727 if (data->flags & MMC_DATA_READ)
728 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
730 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
731 set_dma_addr(host->dma, host->dma_addr);
732 set_dma_count(host->dma, host->size);
734 enable_dma(host->dma);
735 release_dma_lock(dmaflags);
738 * Enable DMA on the host.
740 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
743 * This flag is used to keep printk
744 * output to a minimum.
749 * Initialise the SG list.
751 wbsd_init_sg(host, data);
756 wbsd_write_index(host, WBSD_IDX_DMA, 0);
759 * Set up FIFO threshold levels (and fill
760 * buffer if doing a write).
762 if (data->flags & MMC_DATA_READ) {
763 wbsd_write_index(host, WBSD_IDX_FIFOEN,
764 WBSD_FIFOEN_FULL | 8);
766 wbsd_write_index(host, WBSD_IDX_FIFOEN,
767 WBSD_FIFOEN_EMPTY | 8);
768 wbsd_fill_fifo(host);
772 data->error = MMC_ERR_NONE;
775 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
777 unsigned long dmaflags;
781 WARN_ON(host->mrq == NULL);
784 * Send a stop command if needed.
787 wbsd_send_command(host, data->stop);
790 * Wait for the controller to leave data
794 status = wbsd_read_index(host, WBSD_IDX_STATUS);
795 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
800 if (host->dma >= 0) {
802 * Disable DMA on the host.
804 wbsd_write_index(host, WBSD_IDX_DMA, 0);
807 * Turn of ISA DMA controller.
809 dmaflags = claim_dma_lock();
810 disable_dma(host->dma);
811 clear_dma_ff(host->dma);
812 count = get_dma_residue(host->dma);
813 release_dma_lock(dmaflags);
819 printk(KERN_ERR "%s: Incomplete DMA transfer. "
821 mmc_hostname(host->mmc), count);
823 data->error = MMC_ERR_FAILED;
826 * Transfer data from DMA buffer to
829 if (data->flags & MMC_DATA_READ)
830 wbsd_dma_to_sg(host, data);
832 data->bytes_xfered = host->size;
836 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
838 wbsd_request_end(host, host->mrq);
841 /*****************************************************************************\
843 * MMC layer callbacks *
845 \*****************************************************************************/
847 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
849 struct wbsd_host *host = mmc_priv(mmc);
850 struct mmc_command *cmd;
853 * Disable tasklets to avoid a deadlock.
855 spin_lock_bh(&host->lock);
857 BUG_ON(host->mrq != NULL);
864 * If there is no card in the slot then
865 * timeout immediatly.
867 if (!(host->flags & WBSD_FCARD_PRESENT)) {
868 cmd->error = MMC_ERR_TIMEOUT;
873 * Does the request include data?
876 wbsd_prepare_data(host, cmd->data);
878 if (cmd->data->error != MMC_ERR_NONE)
882 wbsd_send_command(host, cmd);
885 * If this is a data transfer the request
886 * will be finished after the data has
889 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
891 * The hardware is so delightfully stupid that it has a list
892 * of "data" commands. If a command isn't on this list, it'll
893 * just go back to the idle state and won't send any data
896 switch (cmd->opcode) {
910 /* ACMDs. We don't keep track of state, so we just treat them
911 * like any other command. */
916 #ifdef CONFIG_MMC_DEBUG
917 printk(KERN_WARNING "%s: Data command %d is not "
918 "supported by this controller.\n",
919 mmc_hostname(host->mmc), cmd->opcode);
921 cmd->data->error = MMC_ERR_INVALID;
924 wbsd_send_command(host, cmd->data->stop);
930 * Dirty fix for hardware bug.
933 tasklet_schedule(&host->fifo_tasklet);
935 spin_unlock_bh(&host->lock);
941 wbsd_request_end(host, mrq);
943 spin_unlock_bh(&host->lock);
946 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
948 struct wbsd_host *host = mmc_priv(mmc);
951 spin_lock_bh(&host->lock);
954 * Reset the chip on each power off.
955 * Should clear out any weird states.
957 if (ios->power_mode == MMC_POWER_OFF)
958 wbsd_init_device(host);
960 if (ios->clock >= 24000000)
962 else if (ios->clock >= 16000000)
964 else if (ios->clock >= 12000000)
970 * Only write to the clock register when
971 * there is an actual change.
973 if (clk != host->clk) {
974 wbsd_write_index(host, WBSD_IDX_CLK, clk);
981 if (ios->power_mode != MMC_POWER_OFF) {
982 pwr = inb(host->base + WBSD_CSR);
983 pwr &= ~WBSD_POWER_N;
984 outb(pwr, host->base + WBSD_CSR);
988 * MMC cards need to have pin 1 high during init.
989 * It wreaks havoc with the card detection though so
990 * that needs to be disabled.
992 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
993 if (ios->chip_select == MMC_CS_HIGH) {
994 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
995 setup |= WBSD_DAT3_H;
996 host->flags |= WBSD_FIGNORE_DETECT;
998 if (setup & WBSD_DAT3_H) {
999 setup &= ~WBSD_DAT3_H;
1002 * We cannot resume card detection immediatly
1003 * because of capacitance and delays in the chip.
1005 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1008 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1011 * Store bus width for later. Will be used when
1012 * setting up the data transfer.
1014 host->bus_width = ios->bus_width;
1016 spin_unlock_bh(&host->lock);
1019 static int wbsd_get_ro(struct mmc_host *mmc)
1021 struct wbsd_host *host = mmc_priv(mmc);
1024 spin_lock_bh(&host->lock);
1026 csr = inb(host->base + WBSD_CSR);
1028 outb(csr, host->base + WBSD_CSR);
1032 csr = inb(host->base + WBSD_CSR);
1034 outb(csr, host->base + WBSD_CSR);
1036 spin_unlock_bh(&host->lock);
1038 return csr & WBSD_WRPT;
1041 static const struct mmc_host_ops wbsd_ops = {
1042 .request = wbsd_request,
1043 .set_ios = wbsd_set_ios,
1044 .get_ro = wbsd_get_ro,
1047 /*****************************************************************************\
1049 * Interrupt handling *
1051 \*****************************************************************************/
1054 * Helper function to reset detection ignore
1057 static void wbsd_reset_ignore(unsigned long data)
1059 struct wbsd_host *host = (struct wbsd_host *)data;
1061 BUG_ON(host == NULL);
1063 DBG("Resetting card detection ignore\n");
1065 spin_lock_bh(&host->lock);
1067 host->flags &= ~WBSD_FIGNORE_DETECT;
1070 * Card status might have changed during the
1073 tasklet_schedule(&host->card_tasklet);
1075 spin_unlock_bh(&host->lock);
1082 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1084 WARN_ON(!host->mrq);
1088 WARN_ON(!host->mrq->cmd);
1089 if (!host->mrq->cmd)
1092 WARN_ON(!host->mrq->cmd->data);
1093 if (!host->mrq->cmd->data)
1096 return host->mrq->cmd->data;
1099 static void wbsd_tasklet_card(unsigned long param)
1101 struct wbsd_host *host = (struct wbsd_host *)param;
1105 spin_lock(&host->lock);
1107 if (host->flags & WBSD_FIGNORE_DETECT) {
1108 spin_unlock(&host->lock);
1112 csr = inb(host->base + WBSD_CSR);
1113 WARN_ON(csr == 0xff);
1115 if (csr & WBSD_CARDPRESENT) {
1116 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1117 DBG("Card inserted\n");
1118 host->flags |= WBSD_FCARD_PRESENT;
1122 } else if (host->flags & WBSD_FCARD_PRESENT) {
1123 DBG("Card removed\n");
1124 host->flags &= ~WBSD_FCARD_PRESENT;
1127 printk(KERN_ERR "%s: Card removed during transfer!\n",
1128 mmc_hostname(host->mmc));
1131 host->mrq->cmd->error = MMC_ERR_FAILED;
1132 tasklet_schedule(&host->finish_tasklet);
1139 * Unlock first since we might get a call back.
1142 spin_unlock(&host->lock);
1145 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1148 static void wbsd_tasklet_fifo(unsigned long param)
1150 struct wbsd_host *host = (struct wbsd_host *)param;
1151 struct mmc_data *data;
1153 spin_lock(&host->lock);
1158 data = wbsd_get_data(host);
1162 if (data->flags & MMC_DATA_WRITE)
1163 wbsd_fill_fifo(host);
1165 wbsd_empty_fifo(host);
1170 if (host->size == data->bytes_xfered) {
1171 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1172 tasklet_schedule(&host->finish_tasklet);
1176 spin_unlock(&host->lock);
1179 static void wbsd_tasklet_crc(unsigned long param)
1181 struct wbsd_host *host = (struct wbsd_host *)param;
1182 struct mmc_data *data;
1184 spin_lock(&host->lock);
1189 data = wbsd_get_data(host);
1193 DBGF("CRC error\n");
1195 data->error = MMC_ERR_BADCRC;
1197 tasklet_schedule(&host->finish_tasklet);
1200 spin_unlock(&host->lock);
1203 static void wbsd_tasklet_timeout(unsigned long param)
1205 struct wbsd_host *host = (struct wbsd_host *)param;
1206 struct mmc_data *data;
1208 spin_lock(&host->lock);
1213 data = wbsd_get_data(host);
1219 data->error = MMC_ERR_TIMEOUT;
1221 tasklet_schedule(&host->finish_tasklet);
1224 spin_unlock(&host->lock);
1227 static void wbsd_tasklet_finish(unsigned long param)
1229 struct wbsd_host *host = (struct wbsd_host *)param;
1230 struct mmc_data *data;
1232 spin_lock(&host->lock);
1234 WARN_ON(!host->mrq);
1238 data = wbsd_get_data(host);
1242 wbsd_finish_data(host, data);
1245 spin_unlock(&host->lock);
1248 static void wbsd_tasklet_block(unsigned long param)
1250 struct wbsd_host *host = (struct wbsd_host *)param;
1251 struct mmc_data *data;
1253 spin_lock(&host->lock);
1255 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1257 data = wbsd_get_data(host);
1261 DBGF("CRC error\n");
1263 data->error = MMC_ERR_BADCRC;
1265 tasklet_schedule(&host->finish_tasklet);
1269 spin_unlock(&host->lock);
1273 * Interrupt handling
1276 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1278 struct wbsd_host *host = dev_id;
1281 isr = inb(host->base + WBSD_ISR);
1284 * Was it actually our hardware that caused the interrupt?
1286 if (isr == 0xff || isr == 0x00)
1292 * Schedule tasklets as needed.
1294 if (isr & WBSD_INT_CARD)
1295 tasklet_schedule(&host->card_tasklet);
1296 if (isr & WBSD_INT_FIFO_THRE)
1297 tasklet_schedule(&host->fifo_tasklet);
1298 if (isr & WBSD_INT_CRC)
1299 tasklet_hi_schedule(&host->crc_tasklet);
1300 if (isr & WBSD_INT_TIMEOUT)
1301 tasklet_hi_schedule(&host->timeout_tasklet);
1302 if (isr & WBSD_INT_BUSYEND)
1303 tasklet_hi_schedule(&host->block_tasklet);
1304 if (isr & WBSD_INT_TC)
1305 tasklet_schedule(&host->finish_tasklet);
1310 /*****************************************************************************\
1312 * Device initialisation and shutdown *
1314 \*****************************************************************************/
1317 * Allocate/free MMC structure.
1320 static int __devinit wbsd_alloc_mmc(struct device *dev)
1322 struct mmc_host *mmc;
1323 struct wbsd_host *host;
1326 * Allocate MMC structure.
1328 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1332 host = mmc_priv(mmc);
1338 * Set host parameters.
1340 mmc->ops = &wbsd_ops;
1341 mmc->f_min = 375000;
1342 mmc->f_max = 24000000;
1343 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1344 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1346 spin_lock_init(&host->lock);
1351 init_timer(&host->ignore_timer);
1352 host->ignore_timer.data = (unsigned long)host;
1353 host->ignore_timer.function = wbsd_reset_ignore;
1356 * Maximum number of segments. Worst case is one sector per segment
1357 * so this will be 64kB/512.
1359 mmc->max_hw_segs = 128;
1360 mmc->max_phys_segs = 128;
1363 * Maximum request size. Also limited by 64KiB buffer.
1365 mmc->max_req_size = 65536;
1368 * Maximum segment size. Could be one segment with the maximum number
1371 mmc->max_seg_size = mmc->max_req_size;
1374 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1375 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1377 mmc->max_blk_size = 4087;
1380 * Maximum block count. There is no real limit so the maximum
1381 * request size will be the only restriction.
1383 mmc->max_blk_count = mmc->max_req_size;
1385 dev_set_drvdata(dev, mmc);
1390 static void __devexit wbsd_free_mmc(struct device *dev)
1392 struct mmc_host *mmc;
1393 struct wbsd_host *host;
1395 mmc = dev_get_drvdata(dev);
1399 host = mmc_priv(mmc);
1400 BUG_ON(host == NULL);
1402 del_timer_sync(&host->ignore_timer);
1406 dev_set_drvdata(dev, NULL);
1410 * Scan for known chip id:s
1413 static int __devinit wbsd_scan(struct wbsd_host *host)
1419 * Iterate through all ports, all codes to
1420 * find hardware that is in our known list.
1422 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1423 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1426 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1429 host->config = config_ports[i];
1430 host->unlock_code = unlock_codes[j];
1432 wbsd_unlock_config(host);
1434 outb(WBSD_CONF_ID_HI, config_ports[i]);
1435 id = inb(config_ports[i] + 1) << 8;
1437 outb(WBSD_CONF_ID_LO, config_ports[i]);
1438 id |= inb(config_ports[i] + 1);
1440 wbsd_lock_config(host);
1442 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1443 if (id == valid_ids[k]) {
1451 DBG("Unknown hardware (id %x) found at %x\n",
1452 id, config_ports[i]);
1456 release_region(config_ports[i], 2);
1460 host->unlock_code = 0;
1466 * Allocate/free io port ranges
1469 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1474 if (!request_region(base, 8, DRIVER_NAME))
1482 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1485 release_region(host->base, 8);
1490 release_region(host->config, 2);
1496 * Allocate/free DMA port and buffer
1499 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1504 if (request_dma(dma, DRIVER_NAME))
1508 * We need to allocate a special buffer in
1509 * order for ISA to be able to DMA to it.
1511 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1512 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1513 if (!host->dma_buffer)
1517 * Translate the address to a physical address.
1519 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1520 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1523 * ISA DMA must be aligned on a 64k basis.
1525 if ((host->dma_addr & 0xffff) != 0)
1528 * ISA cannot access memory above 16 MB.
1530 else if (host->dma_addr >= 0x1000000)
1539 * If we've gotten here then there is some kind of alignment bug
1543 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1544 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1545 host->dma_addr = (dma_addr_t)NULL;
1547 kfree(host->dma_buffer);
1548 host->dma_buffer = NULL;
1554 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1555 "Falling back on FIFO.\n", dma);
1558 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1560 if (host->dma_addr) {
1561 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1562 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1564 kfree(host->dma_buffer);
1566 free_dma(host->dma);
1569 host->dma_buffer = NULL;
1570 host->dma_addr = (dma_addr_t)NULL;
1574 * Allocate/free IRQ.
1577 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1582 * Allocate interrupt.
1585 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1594 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1595 (unsigned long)host);
1596 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1597 (unsigned long)host);
1598 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1599 (unsigned long)host);
1600 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1601 (unsigned long)host);
1602 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1603 (unsigned long)host);
1604 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1605 (unsigned long)host);
1610 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1615 free_irq(host->irq, host);
1619 tasklet_kill(&host->card_tasklet);
1620 tasklet_kill(&host->fifo_tasklet);
1621 tasklet_kill(&host->crc_tasklet);
1622 tasklet_kill(&host->timeout_tasklet);
1623 tasklet_kill(&host->finish_tasklet);
1624 tasklet_kill(&host->block_tasklet);
1628 * Allocate all resources for the host.
1631 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1632 int base, int irq, int dma)
1637 * Allocate I/O ports.
1639 ret = wbsd_request_region(host, base);
1644 * Allocate interrupt.
1646 ret = wbsd_request_irq(host, irq);
1653 wbsd_request_dma(host, dma);
1659 * Release all resources for the host.
1662 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1664 wbsd_release_dma(host);
1665 wbsd_release_irq(host);
1666 wbsd_release_regions(host);
1670 * Configure the resources the chip should use.
1673 static void wbsd_chip_config(struct wbsd_host *host)
1675 wbsd_unlock_config(host);
1680 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1681 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1684 * Select SD/MMC function.
1686 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1689 * Set up card detection.
1691 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1696 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1697 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1699 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1702 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1705 * Enable and power up chip.
1707 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1708 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1710 wbsd_lock_config(host);
1714 * Check that configured resources are correct.
1717 static int wbsd_chip_validate(struct wbsd_host *host)
1721 wbsd_unlock_config(host);
1724 * Select SD/MMC function.
1726 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1729 * Read configuration.
1731 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1732 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1734 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1736 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1738 wbsd_lock_config(host);
1741 * Validate against given configuration.
1743 if (base != host->base)
1745 if (irq != host->irq)
1747 if ((dma != host->dma) && (host->dma != -1))
1754 * Powers down the SD function
1757 static void wbsd_chip_poweroff(struct wbsd_host *host)
1759 wbsd_unlock_config(host);
1761 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1762 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1764 wbsd_lock_config(host);
1767 /*****************************************************************************\
1769 * Devices setup and shutdown *
1771 \*****************************************************************************/
1773 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1776 struct wbsd_host *host = NULL;
1777 struct mmc_host *mmc = NULL;
1780 ret = wbsd_alloc_mmc(dev);
1784 mmc = dev_get_drvdata(dev);
1785 host = mmc_priv(mmc);
1788 * Scan for hardware.
1790 ret = wbsd_scan(host);
1792 if (pnp && (ret == -ENODEV)) {
1793 printk(KERN_WARNING DRIVER_NAME
1794 ": Unable to confirm device presence. You may "
1795 "experience lock-ups.\n");
1803 * Request resources.
1805 ret = wbsd_request_resources(host, base, irq, dma);
1807 wbsd_release_resources(host);
1813 * See if chip needs to be configured.
1816 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1817 printk(KERN_WARNING DRIVER_NAME
1818 ": PnP active but chip not configured! "
1819 "You probably have a buggy BIOS. "
1820 "Configuring chip manually.\n");
1821 wbsd_chip_config(host);
1824 wbsd_chip_config(host);
1827 * Power Management stuff. No idea how this works.
1832 wbsd_unlock_config(host);
1833 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1834 wbsd_lock_config(host);
1838 * Allow device to initialise itself properly.
1843 * Reset the chip into a known state.
1845 wbsd_init_device(host);
1849 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1850 if (host->chip_id != 0)
1851 printk(" id %x", (int)host->chip_id);
1852 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1854 printk(" dma %d", (int)host->dma);
1864 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1866 struct mmc_host *mmc = dev_get_drvdata(dev);
1867 struct wbsd_host *host;
1872 host = mmc_priv(mmc);
1874 mmc_remove_host(mmc);
1877 * Power down the SD/MMC function.
1880 wbsd_chip_poweroff(host);
1882 wbsd_release_resources(host);
1891 static int __devinit wbsd_probe(struct platform_device *dev)
1893 /* Use the module parameters for resources */
1894 return wbsd_init(&dev->dev, io, irq, dma, 0);
1897 static int __devexit wbsd_remove(struct platform_device *dev)
1899 wbsd_shutdown(&dev->dev, 0);
1910 static int __devinit
1911 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1916 * Get resources from PnP layer.
1918 io = pnp_port_start(pnpdev, 0);
1919 irq = pnp_irq(pnpdev, 0);
1920 if (pnp_dma_valid(pnpdev, 0))
1921 dma = pnp_dma(pnpdev, 0);
1925 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1927 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1930 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1932 wbsd_shutdown(&dev->dev, 1);
1935 #endif /* CONFIG_PNP */
1943 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1945 BUG_ON(host == NULL);
1947 return mmc_suspend_host(host->mmc, state);
1950 static int wbsd_resume(struct wbsd_host *host)
1952 BUG_ON(host == NULL);
1954 wbsd_init_device(host);
1956 return mmc_resume_host(host->mmc);
1959 static int wbsd_platform_suspend(struct platform_device *dev,
1962 struct mmc_host *mmc = platform_get_drvdata(dev);
1963 struct wbsd_host *host;
1969 DBGF("Suspending...\n");
1971 host = mmc_priv(mmc);
1973 ret = wbsd_suspend(host, state);
1977 wbsd_chip_poweroff(host);
1982 static int wbsd_platform_resume(struct platform_device *dev)
1984 struct mmc_host *mmc = platform_get_drvdata(dev);
1985 struct wbsd_host *host;
1990 DBGF("Resuming...\n");
1992 host = mmc_priv(mmc);
1994 wbsd_chip_config(host);
1997 * Allow device to initialise itself properly.
2001 return wbsd_resume(host);
2006 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2008 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2009 struct wbsd_host *host;
2014 DBGF("Suspending...\n");
2016 host = mmc_priv(mmc);
2018 return wbsd_suspend(host, state);
2021 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2023 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2024 struct wbsd_host *host;
2029 DBGF("Resuming...\n");
2031 host = mmc_priv(mmc);
2034 * See if chip needs to be configured.
2036 if (host->config != 0) {
2037 if (!wbsd_chip_validate(host)) {
2038 printk(KERN_WARNING DRIVER_NAME
2039 ": PnP active but chip not configured! "
2040 "You probably have a buggy BIOS. "
2041 "Configuring chip manually.\n");
2042 wbsd_chip_config(host);
2047 * Allow device to initialise itself properly.
2051 return wbsd_resume(host);
2054 #endif /* CONFIG_PNP */
2056 #else /* CONFIG_PM */
2058 #define wbsd_platform_suspend NULL
2059 #define wbsd_platform_resume NULL
2061 #define wbsd_pnp_suspend NULL
2062 #define wbsd_pnp_resume NULL
2064 #endif /* CONFIG_PM */
2066 static struct platform_device *wbsd_device;
2068 static struct platform_driver wbsd_driver = {
2069 .probe = wbsd_probe,
2070 .remove = __devexit_p(wbsd_remove),
2072 .suspend = wbsd_platform_suspend,
2073 .resume = wbsd_platform_resume,
2075 .name = DRIVER_NAME,
2081 static struct pnp_driver wbsd_pnp_driver = {
2082 .name = DRIVER_NAME,
2083 .id_table = pnp_dev_table,
2084 .probe = wbsd_pnp_probe,
2085 .remove = __devexit_p(wbsd_pnp_remove),
2087 .suspend = wbsd_pnp_suspend,
2088 .resume = wbsd_pnp_resume,
2091 #endif /* CONFIG_PNP */
2094 * Module loading/unloading
2097 static int __init wbsd_drv_init(void)
2101 printk(KERN_INFO DRIVER_NAME
2102 ": Winbond W83L51xD SD/MMC card interface driver\n");
2103 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2108 result = pnp_register_driver(&wbsd_pnp_driver);
2112 #endif /* CONFIG_PNP */
2115 result = platform_driver_register(&wbsd_driver);
2119 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2121 platform_driver_unregister(&wbsd_driver);
2125 result = platform_device_add(wbsd_device);
2127 platform_device_put(wbsd_device);
2128 platform_driver_unregister(&wbsd_driver);
2136 static void __exit wbsd_drv_exit(void)
2141 pnp_unregister_driver(&wbsd_pnp_driver);
2143 #endif /* CONFIG_PNP */
2146 platform_device_unregister(wbsd_device);
2148 platform_driver_unregister(&wbsd_driver);
2154 module_init(wbsd_drv_init);
2155 module_exit(wbsd_drv_exit);
2157 module_param(nopnp, uint, 0444);
2159 module_param(io, uint, 0444);
2160 module_param(irq, uint, 0444);
2161 module_param(dma, int, 0444);
2163 MODULE_LICENSE("GPL");
2164 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2165 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2168 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2170 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2171 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2172 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");