2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2005 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 * Changes to the FIFO system should be done with extreme care since
14 * the hardware is full of bugs related to the FIFO. Known issues are:
16 * - FIFO size field in FSR is always zero.
18 * - FIFO interrupts tend not to work as they should. Interrupts are
19 * triggered only for full/empty events, not for threshold values.
21 * - On APIC systems the FIFO empty interrupt is sometimes lost.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <linux/device.h>
30 #include <linux/interrupt.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/delay.h>
33 #include <linux/pnp.h>
34 #include <linux/highmem.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
40 #include <asm/scatterlist.h>
44 #define DRIVER_NAME "wbsd"
45 #define DRIVER_VERSION "1.2"
47 #ifdef CONFIG_MMC_DEBUG
49 printk(KERN_DEBUG DRIVER_NAME ": " x)
50 #define DBGF(f, x...) \
51 printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__ , ##x)
53 #define DBG(x...) do { } while (0)
54 #define DBGF(x...) do { } while (0)
57 #ifdef CONFIG_MMC_DEBUG
58 void DBG_REG(int reg, u8 value)
62 printk(KERN_DEBUG "wbsd: Register %d: 0x%02X %3d '%c' ",
63 reg, (int)value, (int)value, (value < 0x20)?'.':value);
65 for (i = 7;i >= 0;i--)
76 #define DBG_REG(r, v) do {} while (0)
85 static const struct pnp_device_id pnp_dev_table[] = {
91 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
93 #endif /* CONFIG_PNP */
96 static unsigned int nopnp = 0;
98 static const unsigned int nopnp = 1;
100 static unsigned int io = 0x248;
101 static unsigned int irq = 6;
108 static inline void wbsd_unlock_config(struct wbsd_host* host)
110 BUG_ON(host->config == 0);
112 outb(host->unlock_code, host->config);
113 outb(host->unlock_code, host->config);
116 static inline void wbsd_lock_config(struct wbsd_host* host)
118 BUG_ON(host->config == 0);
120 outb(LOCK_CODE, host->config);
123 static inline void wbsd_write_config(struct wbsd_host* host, u8 reg, u8 value)
125 BUG_ON(host->config == 0);
127 outb(reg, host->config);
128 outb(value, host->config + 1);
131 static inline u8 wbsd_read_config(struct wbsd_host* host, u8 reg)
133 BUG_ON(host->config == 0);
135 outb(reg, host->config);
136 return inb(host->config + 1);
139 static inline void wbsd_write_index(struct wbsd_host* host, u8 index, u8 value)
141 outb(index, host->base + WBSD_IDXR);
142 outb(value, host->base + WBSD_DATAR);
145 static inline u8 wbsd_read_index(struct wbsd_host* host, u8 index)
147 outb(index, host->base + WBSD_IDXR);
148 return inb(host->base + WBSD_DATAR);
155 static void wbsd_init_device(struct wbsd_host* host)
160 * Reset chip (SD/MMC part) and fifo.
162 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
163 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
164 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
169 setup &= ~WBSD_DAT3_H;
170 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
171 host->flags &= ~WBSD_FIGNORE_DETECT;
174 * Read back default clock.
176 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
181 outb(WBSD_POWER_N, host->base + WBSD_CSR);
184 * Set maximum timeout.
186 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
189 * Test for card presence
191 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
192 host->flags |= WBSD_FCARD_PRESENT;
194 host->flags &= ~WBSD_FCARD_PRESENT;
197 * Enable interesting interrupts.
200 ier |= WBSD_EINT_CARD;
201 ier |= WBSD_EINT_FIFO_THRE;
202 ier |= WBSD_EINT_CCRC;
203 ier |= WBSD_EINT_TIMEOUT;
204 ier |= WBSD_EINT_CRC;
207 outb(ier, host->base + WBSD_EIR);
212 inb(host->base + WBSD_ISR);
215 static void wbsd_reset(struct wbsd_host* host)
219 printk(KERN_ERR DRIVER_NAME ": Resetting chip\n");
222 * Soft reset of chip (SD/MMC part).
224 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
225 setup |= WBSD_SOFT_RESET;
226 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
229 static void wbsd_request_end(struct wbsd_host* host, struct mmc_request* mrq)
231 unsigned long dmaflags;
233 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
238 * Release ISA DMA controller.
240 dmaflags = claim_dma_lock();
241 disable_dma(host->dma);
242 clear_dma_ff(host->dma);
243 release_dma_lock(dmaflags);
246 * Disable DMA on host.
248 wbsd_write_index(host, WBSD_IDX_DMA, 0);
254 * MMC layer might call back into the driver so first unlock.
256 spin_unlock(&host->lock);
257 mmc_request_done(host->mmc, mrq);
258 spin_lock(&host->lock);
262 * Scatter/gather functions
265 static inline void wbsd_init_sg(struct wbsd_host* host, struct mmc_data* data)
268 * Get info. about SG list from data structure.
270 host->cur_sg = data->sg;
271 host->num_sg = data->sg_len;
274 host->remain = host->cur_sg->length;
277 static inline int wbsd_next_sg(struct wbsd_host* host)
280 * Skip to next SG entry.
288 if (host->num_sg > 0)
291 host->remain = host->cur_sg->length;
297 static inline char* wbsd_kmap_sg(struct wbsd_host* host)
299 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
300 host->cur_sg->offset;
301 return host->mapped_sg;
304 static inline void wbsd_kunmap_sg(struct wbsd_host* host)
306 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
309 static inline void wbsd_sg_to_dma(struct wbsd_host* host, struct mmc_data* data)
311 unsigned int len, i, size;
312 struct scatterlist* sg;
313 char* dmabuf = host->dma_buffer;
322 * Just loop through all entries. Size might not
323 * be the entire list though so make sure that
324 * we do not transfer too much.
326 for (i = 0;i < len;i++)
328 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
329 if (size < sg[i].length)
330 memcpy(dmabuf, sgbuf, size);
332 memcpy(dmabuf, sgbuf, sg[i].length);
333 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
334 dmabuf += sg[i].length;
336 if (size < sg[i].length)
339 size -= sg[i].length;
346 * Check that we didn't get a request to transfer
347 * more data than can fit into the SG list.
355 static inline void wbsd_dma_to_sg(struct wbsd_host* host, struct mmc_data* data)
357 unsigned int len, i, size;
358 struct scatterlist* sg;
359 char* dmabuf = host->dma_buffer;
368 * Just loop through all entries. Size might not
369 * be the entire list though so make sure that
370 * we do not transfer too much.
372 for (i = 0;i < len;i++)
374 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
375 if (size < sg[i].length)
376 memcpy(sgbuf, dmabuf, size);
378 memcpy(sgbuf, dmabuf, sg[i].length);
379 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
380 dmabuf += sg[i].length;
382 if (size < sg[i].length)
385 size -= sg[i].length;
392 * Check that we didn't get a request to transfer
393 * more data than can fit into the SG list.
405 static inline void wbsd_get_short_reply(struct wbsd_host* host,
406 struct mmc_command* cmd)
409 * Correct response type?
411 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT)
413 cmd->error = MMC_ERR_INVALID;
418 wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
420 wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
422 wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
424 wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
426 wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
429 static inline void wbsd_get_long_reply(struct wbsd_host* host,
430 struct mmc_command* cmd)
435 * Correct response type?
437 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG)
439 cmd->error = MMC_ERR_INVALID;
443 for (i = 0;i < 4;i++)
446 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
448 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
450 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
452 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
456 static void wbsd_send_command(struct wbsd_host* host, struct mmc_command* cmd)
461 DBGF("Sending cmd (%x)\n", cmd->opcode);
464 * Clear accumulated ISR. The interrupt routine
465 * will fill this one with events that occur during
471 * Send the command (CRC calculated by host).
473 outb(cmd->opcode, host->base + WBSD_CMDR);
474 for (i = 3;i >= 0;i--)
475 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
477 cmd->error = MMC_ERR_NONE;
480 * Wait for the request to complete.
483 status = wbsd_read_index(host, WBSD_IDX_STATUS);
484 } while (status & WBSD_CARDTRAFFIC);
487 * Do we expect a reply?
489 if ((cmd->flags & MMC_RSP_MASK) != MMC_RSP_NONE)
497 if (isr & WBSD_INT_CARD)
498 cmd->error = MMC_ERR_TIMEOUT;
500 else if (isr & WBSD_INT_TIMEOUT)
501 cmd->error = MMC_ERR_TIMEOUT;
503 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
504 cmd->error = MMC_ERR_BADCRC;
508 if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_SHORT)
509 wbsd_get_short_reply(host, cmd);
511 wbsd_get_long_reply(host, cmd);
515 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
522 static void wbsd_empty_fifo(struct wbsd_host* host)
524 struct mmc_data* data = host->mrq->cmd->data;
529 * Handle excessive data.
531 if (data->bytes_xfered == host->size)
534 buffer = wbsd_kmap_sg(host) + host->offset;
537 * Drain the fifo. This has a tendency to loop longer
538 * than the FIFO length (usually one block).
540 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY))
543 * The size field in the FSR is broken so we have to
546 if (fsr & WBSD_FIFO_FULL)
548 else if (fsr & WBSD_FIFO_FUTHRE)
553 for (i = 0;i < fifo;i++)
555 *buffer = inb(host->base + WBSD_DFR);
560 data->bytes_xfered++;
565 if (data->bytes_xfered == host->size)
567 wbsd_kunmap_sg(host);
572 * End of scatter list entry?
574 if (host->remain == 0)
576 wbsd_kunmap_sg(host);
579 * Get next entry. Check if last.
581 if (!wbsd_next_sg(host))
584 * We should never reach this point.
585 * It means that we're trying to
586 * transfer more blocks than can fit
587 * into the scatter list.
591 host->size = data->bytes_xfered;
596 buffer = wbsd_kmap_sg(host);
601 wbsd_kunmap_sg(host);
604 * This is a very dirty hack to solve a
605 * hardware problem. The chip doesn't trigger
606 * FIFO threshold interrupts properly.
608 if ((host->size - data->bytes_xfered) < 16)
609 tasklet_schedule(&host->fifo_tasklet);
612 static void wbsd_fill_fifo(struct wbsd_host* host)
614 struct mmc_data* data = host->mrq->cmd->data;
619 * Check that we aren't being called after the
620 * entire buffer has been transfered.
622 if (data->bytes_xfered == host->size)
625 buffer = wbsd_kmap_sg(host) + host->offset;
628 * Fill the fifo. This has a tendency to loop longer
629 * than the FIFO length (usually one block).
631 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL))
634 * The size field in the FSR is broken so we have to
637 if (fsr & WBSD_FIFO_EMPTY)
639 else if (fsr & WBSD_FIFO_EMTHRE)
644 for (i = 16;i > fifo;i--)
646 outb(*buffer, host->base + WBSD_DFR);
651 data->bytes_xfered++;
656 if (data->bytes_xfered == host->size)
658 wbsd_kunmap_sg(host);
663 * End of scatter list entry?
665 if (host->remain == 0)
667 wbsd_kunmap_sg(host);
670 * Get next entry. Check if last.
672 if (!wbsd_next_sg(host))
675 * We should never reach this point.
676 * It means that we're trying to
677 * transfer more blocks than can fit
678 * into the scatter list.
682 host->size = data->bytes_xfered;
687 buffer = wbsd_kmap_sg(host);
692 wbsd_kunmap_sg(host);
695 * The controller stops sending interrupts for
696 * 'FIFO empty' under certain conditions. So we
697 * need to be a bit more pro-active.
699 tasklet_schedule(&host->fifo_tasklet);
702 static void wbsd_prepare_data(struct wbsd_host* host, struct mmc_data* data)
706 unsigned long dmaflags;
708 DBGF("blksz %04x blks %04x flags %08x\n",
709 1 << data->blksz_bits, data->blocks, data->flags);
710 DBGF("tsac %d ms nsac %d clk\n",
711 data->timeout_ns / 1000000, data->timeout_clks);
716 host->size = data->blocks << data->blksz_bits;
719 * Check timeout values for overflow.
720 * (Yes, some cards cause this value to overflow).
722 if (data->timeout_ns > 127000000)
723 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
725 wbsd_write_index(host, WBSD_IDX_TAAC, data->timeout_ns/1000000);
727 if (data->timeout_clks > 255)
728 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
730 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
733 * Inform the chip of how large blocks will be
734 * sent. It needs this to determine when to
737 * Space for CRC must be included in the size.
739 blksize = (1 << data->blksz_bits) + 2;
741 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
742 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
745 * Clear the FIFO. This is needed even for DMA
746 * transfers since the chip still uses the FIFO
749 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
750 setup |= WBSD_FIFO_RESET;
751 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
759 * The buffer for DMA is only 64 kB.
761 BUG_ON(host->size > 0x10000);
762 if (host->size > 0x10000)
764 data->error = MMC_ERR_INVALID;
769 * Transfer data from the SG list to
772 if (data->flags & MMC_DATA_WRITE)
773 wbsd_sg_to_dma(host, data);
776 * Initialise the ISA DMA controller.
778 dmaflags = claim_dma_lock();
779 disable_dma(host->dma);
780 clear_dma_ff(host->dma);
781 if (data->flags & MMC_DATA_READ)
782 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
784 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
785 set_dma_addr(host->dma, host->dma_addr);
786 set_dma_count(host->dma, host->size);
788 enable_dma(host->dma);
789 release_dma_lock(dmaflags);
792 * Enable DMA on the host.
794 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
799 * This flag is used to keep printk
800 * output to a minimum.
805 * Initialise the SG list.
807 wbsd_init_sg(host, data);
812 wbsd_write_index(host, WBSD_IDX_DMA, 0);
815 * Set up FIFO threshold levels (and fill
816 * buffer if doing a write).
818 if (data->flags & MMC_DATA_READ)
820 wbsd_write_index(host, WBSD_IDX_FIFOEN,
821 WBSD_FIFOEN_FULL | 8);
825 wbsd_write_index(host, WBSD_IDX_FIFOEN,
826 WBSD_FIFOEN_EMPTY | 8);
827 wbsd_fill_fifo(host);
831 data->error = MMC_ERR_NONE;
834 static void wbsd_finish_data(struct wbsd_host* host, struct mmc_data* data)
836 unsigned long dmaflags;
840 WARN_ON(host->mrq == NULL);
843 * Send a stop command if needed.
846 wbsd_send_command(host, data->stop);
849 * Wait for the controller to leave data
854 status = wbsd_read_index(host, WBSD_IDX_STATUS);
855 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
863 * Disable DMA on the host.
865 wbsd_write_index(host, WBSD_IDX_DMA, 0);
868 * Turn of ISA DMA controller.
870 dmaflags = claim_dma_lock();
871 disable_dma(host->dma);
872 clear_dma_ff(host->dma);
873 count = get_dma_residue(host->dma);
874 release_dma_lock(dmaflags);
881 printk(KERN_ERR DRIVER_NAME ": Incomplete DMA "
882 "transfer. %d bytes left.\n", count);
884 data->error = MMC_ERR_FAILED;
889 * Transfer data from DMA buffer to
892 if (data->flags & MMC_DATA_READ)
893 wbsd_dma_to_sg(host, data);
895 data->bytes_xfered = host->size;
899 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
901 wbsd_request_end(host, host->mrq);
904 /*****************************************************************************\
906 * MMC layer callbacks *
908 \*****************************************************************************/
910 static void wbsd_request(struct mmc_host* mmc, struct mmc_request* mrq)
912 struct wbsd_host* host = mmc_priv(mmc);
913 struct mmc_command* cmd;
916 * Disable tasklets to avoid a deadlock.
918 spin_lock_bh(&host->lock);
920 BUG_ON(host->mrq != NULL);
927 * If there is no card in the slot then
928 * timeout immediatly.
930 if (!(host->flags & WBSD_FCARD_PRESENT))
932 cmd->error = MMC_ERR_TIMEOUT;
937 * Does the request include data?
941 wbsd_prepare_data(host, cmd->data);
943 if (cmd->data->error != MMC_ERR_NONE)
947 wbsd_send_command(host, cmd);
950 * If this is a data transfer the request
951 * will be finished after the data has
954 if (cmd->data && (cmd->error == MMC_ERR_NONE))
957 * Dirty fix for hardware bug.
960 tasklet_schedule(&host->fifo_tasklet);
962 spin_unlock_bh(&host->lock);
968 wbsd_request_end(host, mrq);
970 spin_unlock_bh(&host->lock);
973 static void wbsd_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
975 struct wbsd_host* host = mmc_priv(mmc);
978 DBGF("clock %uHz busmode %u powermode %u Vdd %u\n",
979 ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
981 spin_lock_bh(&host->lock);
984 * Reset the chip on each power off.
985 * Should clear out any weird states.
987 if (ios->power_mode == MMC_POWER_OFF)
988 wbsd_init_device(host);
990 if (ios->clock >= 24000000)
992 else if (ios->clock >= 16000000)
994 else if (ios->clock >= 12000000)
1000 * Only write to the clock register when
1001 * there is an actual change.
1003 if (clk != host->clk)
1005 wbsd_write_index(host, WBSD_IDX_CLK, clk);
1012 if (ios->power_mode != MMC_POWER_OFF)
1014 pwr = inb(host->base + WBSD_CSR);
1015 pwr &= ~WBSD_POWER_N;
1016 outb(pwr, host->base + WBSD_CSR);
1020 * MMC cards need to have pin 1 high during init.
1021 * Init time corresponds rather nicely with the bus mode.
1022 * It wreaks havoc with the card detection though so
1023 * that needs to be disabed.
1025 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
1026 if ((ios->power_mode == MMC_POWER_ON) &&
1027 (ios->bus_mode == MMC_BUSMODE_OPENDRAIN))
1029 setup |= WBSD_DAT3_H;
1030 host->flags |= WBSD_FIGNORE_DETECT;
1034 setup &= ~WBSD_DAT3_H;
1035 host->flags &= ~WBSD_FIGNORE_DETECT;
1037 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1039 spin_unlock_bh(&host->lock);
1042 static struct mmc_host_ops wbsd_ops = {
1043 .request = wbsd_request,
1044 .set_ios = wbsd_set_ios,
1047 /*****************************************************************************\
1049 * Interrupt handling *
1051 \*****************************************************************************/
1057 inline static struct mmc_data* wbsd_get_data(struct wbsd_host* host)
1059 WARN_ON(!host->mrq);
1063 WARN_ON(!host->mrq->cmd);
1064 if (!host->mrq->cmd)
1067 WARN_ON(!host->mrq->cmd->data);
1068 if (!host->mrq->cmd->data)
1071 return host->mrq->cmd->data;
1074 static void wbsd_tasklet_card(unsigned long param)
1076 struct wbsd_host* host = (struct wbsd_host*)param;
1080 spin_lock(&host->lock);
1082 if (host->flags & WBSD_FIGNORE_DETECT)
1084 spin_unlock(&host->lock);
1088 csr = inb(host->base + WBSD_CSR);
1089 WARN_ON(csr == 0xff);
1091 if (csr & WBSD_CARDPRESENT)
1093 if (!(host->flags & WBSD_FCARD_PRESENT))
1095 DBG("Card inserted\n");
1096 host->flags |= WBSD_FCARD_PRESENT;
1100 else if (host->flags & WBSD_FCARD_PRESENT)
1102 DBG("Card removed\n");
1103 host->flags &= ~WBSD_FCARD_PRESENT;
1108 printk(KERN_ERR DRIVER_NAME
1109 ": Card removed during transfer!\n");
1112 host->mrq->cmd->error = MMC_ERR_FAILED;
1113 tasklet_schedule(&host->finish_tasklet);
1118 * Unlock first since we might get a call back.
1120 spin_unlock(&host->lock);
1123 mmc_detect_change(host->mmc);
1126 static void wbsd_tasklet_fifo(unsigned long param)
1128 struct wbsd_host* host = (struct wbsd_host*)param;
1129 struct mmc_data* data;
1131 spin_lock(&host->lock);
1136 data = wbsd_get_data(host);
1140 if (data->flags & MMC_DATA_WRITE)
1141 wbsd_fill_fifo(host);
1143 wbsd_empty_fifo(host);
1148 if (host->size == data->bytes_xfered)
1150 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1151 tasklet_schedule(&host->finish_tasklet);
1155 spin_unlock(&host->lock);
1158 static void wbsd_tasklet_crc(unsigned long param)
1160 struct wbsd_host* host = (struct wbsd_host*)param;
1161 struct mmc_data* data;
1163 spin_lock(&host->lock);
1168 data = wbsd_get_data(host);
1172 DBGF("CRC error\n");
1174 data->error = MMC_ERR_BADCRC;
1176 tasklet_schedule(&host->finish_tasklet);
1179 spin_unlock(&host->lock);
1182 static void wbsd_tasklet_timeout(unsigned long param)
1184 struct wbsd_host* host = (struct wbsd_host*)param;
1185 struct mmc_data* data;
1187 spin_lock(&host->lock);
1192 data = wbsd_get_data(host);
1198 data->error = MMC_ERR_TIMEOUT;
1200 tasklet_schedule(&host->finish_tasklet);
1203 spin_unlock(&host->lock);
1206 static void wbsd_tasklet_finish(unsigned long param)
1208 struct wbsd_host* host = (struct wbsd_host*)param;
1209 struct mmc_data* data;
1211 spin_lock(&host->lock);
1213 WARN_ON(!host->mrq);
1217 data = wbsd_get_data(host);
1221 wbsd_finish_data(host, data);
1224 spin_unlock(&host->lock);
1227 static void wbsd_tasklet_block(unsigned long param)
1229 struct wbsd_host* host = (struct wbsd_host*)param;
1230 struct mmc_data* data;
1232 spin_lock(&host->lock);
1234 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1237 data = wbsd_get_data(host);
1241 DBGF("CRC error\n");
1243 data->error = MMC_ERR_BADCRC;
1245 tasklet_schedule(&host->finish_tasklet);
1249 spin_unlock(&host->lock);
1253 * Interrupt handling
1256 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1258 struct wbsd_host* host = dev_id;
1261 isr = inb(host->base + WBSD_ISR);
1264 * Was it actually our hardware that caused the interrupt?
1266 if (isr == 0xff || isr == 0x00)
1272 * Schedule tasklets as needed.
1274 if (isr & WBSD_INT_CARD)
1275 tasklet_schedule(&host->card_tasklet);
1276 if (isr & WBSD_INT_FIFO_THRE)
1277 tasklet_schedule(&host->fifo_tasklet);
1278 if (isr & WBSD_INT_CRC)
1279 tasklet_hi_schedule(&host->crc_tasklet);
1280 if (isr & WBSD_INT_TIMEOUT)
1281 tasklet_hi_schedule(&host->timeout_tasklet);
1282 if (isr & WBSD_INT_BUSYEND)
1283 tasklet_hi_schedule(&host->block_tasklet);
1284 if (isr & WBSD_INT_TC)
1285 tasklet_schedule(&host->finish_tasklet);
1290 /*****************************************************************************\
1292 * Device initialisation and shutdown *
1294 \*****************************************************************************/
1297 * Allocate/free MMC structure.
1300 static int __devinit wbsd_alloc_mmc(struct device* dev)
1302 struct mmc_host* mmc;
1303 struct wbsd_host* host;
1306 * Allocate MMC structure.
1308 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1312 host = mmc_priv(mmc);
1318 * Set host parameters.
1320 mmc->ops = &wbsd_ops;
1321 mmc->f_min = 375000;
1322 mmc->f_max = 24000000;
1323 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1325 spin_lock_init(&host->lock);
1328 * Maximum number of segments. Worst case is one sector per segment
1329 * so this will be 64kB/512.
1331 mmc->max_hw_segs = 128;
1332 mmc->max_phys_segs = 128;
1335 * Maximum number of sectors in one transfer. Also limited by 64kB
1338 mmc->max_sectors = 128;
1341 * Maximum segment size. Could be one segment with the maximum number
1344 mmc->max_seg_size = mmc->max_sectors * 512;
1346 dev_set_drvdata(dev, mmc);
1351 static void __devexit wbsd_free_mmc(struct device* dev)
1353 struct mmc_host* mmc;
1355 mmc = dev_get_drvdata(dev);
1361 dev_set_drvdata(dev, NULL);
1365 * Scan for known chip id:s
1368 static int __devinit wbsd_scan(struct wbsd_host* host)
1374 * Iterate through all ports, all codes to
1375 * find hardware that is in our known list.
1377 for (i = 0;i < sizeof(config_ports)/sizeof(int);i++)
1379 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1382 for (j = 0;j < sizeof(unlock_codes)/sizeof(int);j++)
1386 outb(unlock_codes[j], config_ports[i]);
1387 outb(unlock_codes[j], config_ports[i]);
1389 outb(WBSD_CONF_ID_HI, config_ports[i]);
1390 id = inb(config_ports[i] + 1) << 8;
1392 outb(WBSD_CONF_ID_LO, config_ports[i]);
1393 id |= inb(config_ports[i] + 1);
1395 for (k = 0;k < sizeof(valid_ids)/sizeof(int);k++)
1397 if (id == valid_ids[k])
1400 host->config = config_ports[i];
1401 host->unlock_code = unlock_codes[i];
1409 DBG("Unknown hardware (id %x) found at %x\n",
1410 id, config_ports[i]);
1413 outb(LOCK_CODE, config_ports[i]);
1416 release_region(config_ports[i], 2);
1423 * Allocate/free io port ranges
1426 static int __devinit wbsd_request_region(struct wbsd_host* host, int base)
1431 if (!request_region(base, 8, DRIVER_NAME))
1439 static void __devexit wbsd_release_regions(struct wbsd_host* host)
1442 release_region(host->base, 8);
1447 release_region(host->config, 2);
1453 * Allocate/free DMA port and buffer
1456 static void __devinit wbsd_request_dma(struct wbsd_host* host, int dma)
1461 if (request_dma(dma, DRIVER_NAME))
1465 * We need to allocate a special buffer in
1466 * order for ISA to be able to DMA to it.
1468 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1469 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1470 if (!host->dma_buffer)
1474 * Translate the address to a physical address.
1476 host->dma_addr = dma_map_single(host->mmc->dev, host->dma_buffer,
1477 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1480 * ISA DMA must be aligned on a 64k basis.
1482 if ((host->dma_addr & 0xffff) != 0)
1485 * ISA cannot access memory above 16 MB.
1487 else if (host->dma_addr >= 0x1000000)
1496 * If we've gotten here then there is some kind of alignment bug
1500 dma_unmap_single(host->mmc->dev, host->dma_addr, WBSD_DMA_SIZE,
1502 host->dma_addr = (dma_addr_t)NULL;
1504 kfree(host->dma_buffer);
1505 host->dma_buffer = NULL;
1511 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1512 "Falling back on FIFO.\n", dma);
1515 static void __devexit wbsd_release_dma(struct wbsd_host* host)
1518 dma_unmap_single(host->mmc->dev, host->dma_addr, WBSD_DMA_SIZE,
1520 if (host->dma_buffer)
1521 kfree(host->dma_buffer);
1523 free_dma(host->dma);
1526 host->dma_buffer = NULL;
1527 host->dma_addr = (dma_addr_t)NULL;
1531 * Allocate/free IRQ.
1534 static int __devinit wbsd_request_irq(struct wbsd_host* host, int irq)
1539 * Allocate interrupt.
1542 ret = request_irq(irq, wbsd_irq, SA_SHIRQ, DRIVER_NAME, host);
1551 tasklet_init(&host->card_tasklet, wbsd_tasklet_card, (unsigned long)host);
1552 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo, (unsigned long)host);
1553 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc, (unsigned long)host);
1554 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout, (unsigned long)host);
1555 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish, (unsigned long)host);
1556 tasklet_init(&host->block_tasklet, wbsd_tasklet_block, (unsigned long)host);
1561 static void __devexit wbsd_release_irq(struct wbsd_host* host)
1566 free_irq(host->irq, host);
1570 tasklet_kill(&host->card_tasklet);
1571 tasklet_kill(&host->fifo_tasklet);
1572 tasklet_kill(&host->crc_tasklet);
1573 tasklet_kill(&host->timeout_tasklet);
1574 tasklet_kill(&host->finish_tasklet);
1575 tasklet_kill(&host->block_tasklet);
1579 * Allocate all resources for the host.
1582 static int __devinit wbsd_request_resources(struct wbsd_host* host,
1583 int base, int irq, int dma)
1588 * Allocate I/O ports.
1590 ret = wbsd_request_region(host, base);
1595 * Allocate interrupt.
1597 ret = wbsd_request_irq(host, irq);
1604 wbsd_request_dma(host, dma);
1610 * Release all resources for the host.
1613 static void __devexit wbsd_release_resources(struct wbsd_host* host)
1615 wbsd_release_dma(host);
1616 wbsd_release_irq(host);
1617 wbsd_release_regions(host);
1621 * Configure the resources the chip should use.
1624 static void __devinit wbsd_chip_config(struct wbsd_host* host)
1629 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1630 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1633 * Select SD/MMC function.
1635 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1638 * Set up card detection.
1640 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1645 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1646 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1648 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1651 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1654 * Enable and power up chip.
1656 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1657 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1661 * Check that configured resources are correct.
1664 static int __devinit wbsd_chip_validate(struct wbsd_host* host)
1669 * Select SD/MMC function.
1671 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1674 * Read configuration.
1676 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1677 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1679 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1681 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1684 * Validate against given configuration.
1686 if (base != host->base)
1688 if (irq != host->irq)
1690 if ((dma != host->dma) && (host->dma != -1))
1696 /*****************************************************************************\
1698 * Devices setup and shutdown *
1700 \*****************************************************************************/
1702 static int __devinit wbsd_init(struct device* dev, int base, int irq, int dma,
1705 struct wbsd_host* host = NULL;
1706 struct mmc_host* mmc = NULL;
1709 ret = wbsd_alloc_mmc(dev);
1713 mmc = dev_get_drvdata(dev);
1714 host = mmc_priv(mmc);
1717 * Scan for hardware.
1719 ret = wbsd_scan(host);
1722 if (pnp && (ret == -ENODEV))
1724 printk(KERN_WARNING DRIVER_NAME
1725 ": Unable to confirm device presence. You may "
1726 "experience lock-ups.\n");
1736 * Request resources.
1738 ret = wbsd_request_resources(host, io, irq, dma);
1741 wbsd_release_resources(host);
1747 * See if chip needs to be configured.
1749 if (pnp && (host->config != 0))
1751 if (!wbsd_chip_validate(host))
1753 printk(KERN_WARNING DRIVER_NAME
1754 ": PnP active but chip not configured! "
1755 "You probably have a buggy BIOS. "
1756 "Configuring chip manually.\n");
1757 wbsd_chip_config(host);
1761 wbsd_chip_config(host);
1764 * Power Management stuff. No idea how this works.
1769 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1772 * Allow device to initialise itself properly.
1777 * Reset the chip into a known state.
1779 wbsd_init_device(host);
1783 printk(KERN_INFO "%s: W83L51xD", mmc->host_name);
1784 if (host->chip_id != 0)
1785 printk(" id %x", (int)host->chip_id);
1786 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1788 printk(" dma %d", (int)host->dma);
1798 static void __devexit wbsd_shutdown(struct device* dev, int pnp)
1800 struct mmc_host* mmc = dev_get_drvdata(dev);
1801 struct wbsd_host* host;
1806 host = mmc_priv(mmc);
1808 mmc_remove_host(mmc);
1813 * Power down the SD/MMC function.
1815 wbsd_unlock_config(host);
1816 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1817 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1818 wbsd_lock_config(host);
1821 wbsd_release_resources(host);
1830 static int __devinit wbsd_probe(struct device* dev)
1832 return wbsd_init(dev, io, irq, dma, 0);
1835 static int __devexit wbsd_remove(struct device* dev)
1837 wbsd_shutdown(dev, 0);
1848 static int __devinit
1849 wbsd_pnp_probe(struct pnp_dev * pnpdev, const struct pnp_device_id *dev_id)
1854 * Get resources from PnP layer.
1856 io = pnp_port_start(pnpdev, 0);
1857 irq = pnp_irq(pnpdev, 0);
1858 if (pnp_dma_valid(pnpdev, 0))
1859 dma = pnp_dma(pnpdev, 0);
1863 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1865 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1868 static void __devexit wbsd_pnp_remove(struct pnp_dev * dev)
1870 wbsd_shutdown(&dev->dev, 1);
1873 #endif /* CONFIG_PNP */
1880 static int wbsd_suspend(struct device *dev, pm_message_t state, u32 level)
1882 DBGF("Not yet supported\n");
1887 static int wbsd_resume(struct device *dev, u32 level)
1889 DBGF("Not yet supported\n");
1894 #define wbsd_suspend NULL
1895 #define wbsd_resume NULL
1898 static struct platform_device *wbsd_device;
1900 static struct device_driver wbsd_driver = {
1901 .name = DRIVER_NAME,
1902 .bus = &platform_bus_type,
1903 .probe = wbsd_probe,
1904 .remove = wbsd_remove,
1906 .suspend = wbsd_suspend,
1907 .resume = wbsd_resume,
1912 static struct pnp_driver wbsd_pnp_driver = {
1913 .name = DRIVER_NAME,
1914 .id_table = pnp_dev_table,
1915 .probe = wbsd_pnp_probe,
1916 .remove = wbsd_pnp_remove,
1919 #endif /* CONFIG_PNP */
1922 * Module loading/unloading
1925 static int __init wbsd_drv_init(void)
1929 printk(KERN_INFO DRIVER_NAME
1930 ": Winbond W83L51xD SD/MMC card interface driver, "
1931 DRIVER_VERSION "\n");
1932 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1938 result = pnp_register_driver(&wbsd_pnp_driver);
1943 #endif /* CONFIG_PNP */
1947 result = driver_register(&wbsd_driver);
1951 wbsd_device = platform_device_register_simple(DRIVER_NAME, -1,
1953 if (IS_ERR(wbsd_device))
1954 return PTR_ERR(wbsd_device);
1960 static void __exit wbsd_drv_exit(void)
1965 pnp_unregister_driver(&wbsd_pnp_driver);
1967 #endif /* CONFIG_PNP */
1971 platform_device_unregister(wbsd_device);
1973 driver_unregister(&wbsd_driver);
1979 module_init(wbsd_drv_init);
1980 module_exit(wbsd_drv_exit);
1982 module_param(nopnp, uint, 0444);
1984 module_param(io, uint, 0444);
1985 module_param(irq, uint, 0444);
1986 module_param(dma, int, 0444);
1988 MODULE_LICENSE("GPL");
1989 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
1990 MODULE_VERSION(DRIVER_VERSION);
1993 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
1995 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1996 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
1997 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");