2 * linux/drivers/mmc/wbsd.c
4 * Copyright (C) 2004 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.
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/blkdev.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/protocol.h>
29 #define DRIVER_NAME "wbsd"
30 #define DRIVER_VERSION "1.0"
32 #ifdef CONFIG_MMC_DEBUG
34 printk(KERN_DEBUG DRIVER_NAME ": " x)
35 #define DBGF(f, x...) \
36 printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__, ##x)
38 #define DBG(x...) do { } while (0)
39 #define DBGF(x...) do { } while (0)
42 static unsigned int io = 0x248;
43 static unsigned int irq = 6;
46 #ifdef CONFIG_MMC_DEBUG
47 void DBG_REG(int reg, u8 value)
51 printk(KERN_DEBUG "wbsd: Register %d: 0x%02X %3d '%c' ",
52 reg, (int)value, (int)value, (value < 0x20)?'.':value);
54 for (i = 7;i >= 0;i--)
65 #define DBG_REG(r, v) do {} while (0)
72 static inline void wbsd_unlock_config(struct wbsd_host* host)
74 outb(host->unlock_code, host->config);
75 outb(host->unlock_code, host->config);
78 static inline void wbsd_lock_config(struct wbsd_host* host)
80 outb(LOCK_CODE, host->config);
83 static inline void wbsd_write_config(struct wbsd_host* host, u8 reg, u8 value)
85 outb(reg, host->config);
86 outb(value, host->config + 1);
89 static inline u8 wbsd_read_config(struct wbsd_host* host, u8 reg)
91 outb(reg, host->config);
92 return inb(host->config + 1);
95 static inline void wbsd_write_index(struct wbsd_host* host, u8 index, u8 value)
97 outb(index, host->base + WBSD_IDXR);
98 outb(value, host->base + WBSD_DATAR);
101 static inline u8 wbsd_read_index(struct wbsd_host* host, u8 index)
103 outb(index, host->base + WBSD_IDXR);
104 return inb(host->base + WBSD_DATAR);
111 static void wbsd_init_device(struct wbsd_host* host)
116 * Reset chip (SD/MMC part) and fifo.
118 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
119 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
120 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
123 * Read back default clock.
125 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
130 outb(WBSD_POWER_N, host->base + WBSD_CSR);
133 * Set maximum timeout.
135 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
138 * Enable interesting interrupts.
141 ier |= WBSD_EINT_CARD;
142 ier |= WBSD_EINT_FIFO_THRE;
143 ier |= WBSD_EINT_CCRC;
144 ier |= WBSD_EINT_TIMEOUT;
145 ier |= WBSD_EINT_CRC;
148 outb(ier, host->base + WBSD_EIR);
153 inb(host->base + WBSD_ISR);
156 static void wbsd_reset(struct wbsd_host* host)
160 printk(KERN_ERR DRIVER_NAME ": Resetting chip\n");
163 * Soft reset of chip (SD/MMC part).
165 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
166 setup |= WBSD_SOFT_RESET;
167 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
170 static void wbsd_request_end(struct wbsd_host* host, struct mmc_request* mrq)
172 unsigned long dmaflags;
174 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
179 * Release ISA DMA controller.
181 dmaflags = claim_dma_lock();
182 disable_dma(host->dma);
183 clear_dma_ff(host->dma);
184 release_dma_lock(dmaflags);
187 * Disable DMA on host.
189 wbsd_write_index(host, WBSD_IDX_DMA, 0);
195 * MMC layer might call back into the driver so first unlock.
197 spin_unlock(&host->lock);
198 mmc_request_done(host->mmc, mrq);
199 spin_lock(&host->lock);
203 * Scatter/gather functions
206 static inline void wbsd_init_sg(struct wbsd_host* host, struct mmc_data* data)
208 struct request* req = data->req;
211 * Get info. about SG list from data structure.
213 host->cur_sg = data->sg;
214 host->num_sg = data->sg_len;
217 host->remain = host->cur_sg->length;
220 static inline int wbsd_next_sg(struct wbsd_host* host)
223 * Skip to next SG entry.
231 if (host->num_sg > 0)
234 host->remain = host->cur_sg->length;
240 static inline char* wbsd_kmap_sg(struct wbsd_host* host)
242 return kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
243 host->cur_sg->offset;
246 static inline void wbsd_kunmap_sg(struct wbsd_host* host)
248 kunmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
251 static inline void wbsd_sg_to_dma(struct wbsd_host* host, struct mmc_data* data)
253 unsigned int len, i, size;
254 struct scatterlist* sg;
255 char* dmabuf = host->dma_buffer;
264 * Just loop through all entries. Size might not
265 * be the entire list though so make sure that
266 * we do not transfer too much.
268 for (i = 0;i < len;i++)
270 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
271 if (size < sg[i].length)
272 memcpy(dmabuf, sgbuf, size);
274 memcpy(dmabuf, sgbuf, sg[i].length);
275 kunmap_atomic(sg[i].page, KM_BIO_SRC_IRQ);
276 dmabuf += sg[i].length;
278 if (size < sg[i].length)
281 size -= sg[i].length;
288 * Check that we didn't get a request to transfer
289 * more data than can fit into the SG list.
297 static inline void wbsd_dma_to_sg(struct wbsd_host* host, struct mmc_data* data)
299 unsigned int len, i, size;
300 struct scatterlist* sg;
301 char* dmabuf = host->dma_buffer;
310 * Just loop through all entries. Size might not
311 * be the entire list though so make sure that
312 * we do not transfer too much.
314 for (i = 0;i < len;i++)
316 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
317 if (size < sg[i].length)
318 memcpy(sgbuf, dmabuf, size);
320 memcpy(sgbuf, dmabuf, sg[i].length);
321 kunmap_atomic(sg[i].page, KM_BIO_SRC_IRQ);
322 dmabuf += sg[i].length;
324 if (size < sg[i].length)
327 size -= sg[i].length;
334 * Check that we didn't get a request to transfer
335 * more data than can fit into the SG list.
347 static inline void wbsd_get_short_reply(struct wbsd_host* host,
348 struct mmc_command* cmd)
351 * Correct response type?
353 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT)
355 cmd->error = MMC_ERR_INVALID;
360 wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
362 wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
364 wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
366 wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
368 wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
371 static inline void wbsd_get_long_reply(struct wbsd_host* host,
372 struct mmc_command* cmd)
377 * Correct response type?
379 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG)
381 cmd->error = MMC_ERR_INVALID;
385 for (i = 0;i < 4;i++)
388 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
390 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
392 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
394 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
398 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs);
400 static void wbsd_send_command(struct wbsd_host* host, struct mmc_command* cmd)
405 DBGF("Sending cmd (%x)\n", cmd->opcode);
408 * Disable interrupts as the interrupt routine
409 * will destroy the contents of ISR.
411 eir = inb(host->base + WBSD_EIR);
412 outb(0, host->base + WBSD_EIR);
415 * Send the command (CRC calculated by host).
417 outb(cmd->opcode, host->base + WBSD_CMDR);
418 for (i = 3;i >= 0;i--)
419 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
421 cmd->error = MMC_ERR_NONE;
424 * Wait for the request to complete.
427 status = wbsd_read_index(host, WBSD_IDX_STATUS);
428 } while (status & WBSD_CARDTRAFFIC);
431 * Do we expect a reply?
433 if ((cmd->flags & MMC_RSP_MASK) != MMC_RSP_NONE)
438 isr = inb(host->base + WBSD_ISR);
441 if (isr & WBSD_INT_CARD)
442 cmd->error = MMC_ERR_TIMEOUT;
444 else if (isr & WBSD_INT_TIMEOUT)
445 cmd->error = MMC_ERR_TIMEOUT;
447 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
448 cmd->error = MMC_ERR_BADCRC;
452 if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_SHORT)
453 wbsd_get_short_reply(host, cmd);
455 wbsd_get_long_reply(host, cmd);
460 * Restore interrupt mask to previous value.
462 outb(eir, host->base + WBSD_EIR);
465 * Call the interrupt routine to jump start
468 wbsd_irq(0, host, NULL);
470 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
477 static void wbsd_empty_fifo(struct wbsd_host* host)
479 struct mmc_data* data = host->mrq->cmd->data;
483 * Handle excessive data.
485 if (data->bytes_xfered == host->size)
488 buffer = wbsd_kmap_sg(host) + host->offset;
491 * Drain the fifo. This has a tendency to loop longer
492 * than the FIFO length (usually one block).
494 while (!(inb(host->base + WBSD_FSR) & WBSD_FIFO_EMPTY))
496 *buffer = inb(host->base + WBSD_DFR);
501 data->bytes_xfered++;
506 if (data->bytes_xfered == host->size)
508 wbsd_kunmap_sg(host);
513 * End of scatter list entry?
515 if (host->remain == 0)
517 wbsd_kunmap_sg(host);
520 * Get next entry. Check if last.
522 if (!wbsd_next_sg(host))
525 * We should never reach this point.
526 * It means that we're trying to
527 * transfer more blocks than can fit
528 * into the scatter list.
532 host->size = data->bytes_xfered;
537 buffer = wbsd_kmap_sg(host);
541 wbsd_kunmap_sg(host);
544 static void wbsd_fill_fifo(struct wbsd_host* host)
546 struct mmc_data* data = host->mrq->cmd->data;
550 * Check that we aren't being called after the
551 * entire buffer has been transfered.
553 if (data->bytes_xfered == host->size)
556 buffer = wbsd_kmap_sg(host) + host->offset;
559 * Fill the fifo. This has a tendency to loop longer
560 * than the FIFO length (usually one block).
562 while (!(inb(host->base + WBSD_FSR) & WBSD_FIFO_FULL))
564 outb(*buffer, host->base + WBSD_DFR);
569 data->bytes_xfered++;
574 if (data->bytes_xfered == host->size)
576 wbsd_kunmap_sg(host);
581 * End of scatter list entry?
583 if (host->remain == 0)
585 wbsd_kunmap_sg(host);
588 * Get next entry. Check if last.
590 if (!wbsd_next_sg(host))
593 * We should never reach this point.
594 * It means that we're trying to
595 * transfer more blocks than can fit
596 * into the scatter list.
600 host->size = data->bytes_xfered;
605 buffer = wbsd_kmap_sg(host);
609 wbsd_kunmap_sg(host);
612 static void wbsd_prepare_data(struct wbsd_host* host, struct mmc_data* data)
616 unsigned long dmaflags;
618 DBGF("blksz %04x blks %04x flags %08x\n",
619 1 << data->blksz_bits, data->blocks, data->flags);
620 DBGF("tsac %d ms nsac %d clk\n",
621 data->timeout_ns / 1000000, data->timeout_clks);
626 host->size = data->blocks << data->blksz_bits;
629 * Check timeout values for overflow.
630 * (Yes, some cards cause this value to overflow).
632 if (data->timeout_ns > 127000000)
633 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
635 wbsd_write_index(host, WBSD_IDX_TAAC, data->timeout_ns/1000000);
637 if (data->timeout_clks > 255)
638 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
640 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
643 * Inform the chip of how large blocks will be
644 * sent. It needs this to determine when to
647 * Space for CRC must be included in the size.
649 blksize = (1 << data->blksz_bits) + 2;
651 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
652 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
655 * Clear the FIFO. This is needed even for DMA
656 * transfers since the chip still uses the FIFO
659 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
660 setup |= WBSD_FIFO_RESET;
661 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
669 * The buffer for DMA is only 64 kB.
671 BUG_ON(host->size > 0x10000);
672 if (host->size > 0x10000)
674 data->error = MMC_ERR_INVALID;
679 * Transfer data from the SG list to
682 if (data->flags & MMC_DATA_WRITE)
683 wbsd_sg_to_dma(host, data);
686 * Initialise the ISA DMA controller.
688 dmaflags = claim_dma_lock();
689 disable_dma(host->dma);
690 clear_dma_ff(host->dma);
691 if (data->flags & MMC_DATA_READ)
692 set_dma_mode(host->dma, DMA_MODE_READ);
694 set_dma_mode(host->dma, DMA_MODE_WRITE);
695 set_dma_addr(host->dma, host->dma_addr);
696 set_dma_count(host->dma, host->size);
698 enable_dma(host->dma);
699 release_dma_lock(dmaflags);
702 * Enable DMA on the host.
704 wbsd_write_index(host, WBSD_IDX_DMA,
705 WBSD_DMA_SINGLE | WBSD_DMA_ENABLE);
710 * This flag is used to keep printk
711 * output to a minimum.
716 * Initialise the SG list.
718 wbsd_init_sg(host, data);
723 wbsd_write_index(host, WBSD_IDX_DMA, 0);
726 * Set up FIFO threshold levels (and fill
727 * buffer if doing a write).
729 if (data->flags & MMC_DATA_READ)
731 wbsd_write_index(host, WBSD_IDX_FIFOEN,
732 WBSD_FIFOEN_FULL | 8);
736 wbsd_write_index(host, WBSD_IDX_FIFOEN,
737 WBSD_FIFOEN_EMPTY | 8);
738 wbsd_fill_fifo(host);
742 data->error = MMC_ERR_NONE;
745 static void wbsd_finish_data(struct wbsd_host* host, struct mmc_data* data)
747 unsigned long dmaflags;
750 WARN_ON(host->mrq == NULL);
753 * Send a stop command if needed.
756 wbsd_send_command(host, data->stop);
764 * Disable DMA on the host.
766 wbsd_write_index(host, WBSD_IDX_DMA, 0);
769 * Turn of ISA DMA controller.
771 dmaflags = claim_dma_lock();
772 disable_dma(host->dma);
773 clear_dma_ff(host->dma);
774 count = get_dma_residue(host->dma);
775 release_dma_lock(dmaflags);
782 printk(KERN_ERR DRIVER_NAME ": Incomplete DMA "
783 "transfer. %d bytes left.\n", count);
785 data->error = MMC_ERR_FAILED;
790 * Transfer data from DMA buffer to
793 if (data->flags & MMC_DATA_READ)
794 wbsd_dma_to_sg(host, data);
796 data->bytes_xfered = host->size;
800 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
802 wbsd_request_end(host, host->mrq);
809 static void wbsd_request(struct mmc_host* mmc, struct mmc_request* mrq)
811 struct wbsd_host* host = mmc_priv(mmc);
812 struct mmc_command* cmd;
815 * Disable tasklets to avoid a deadlock.
817 spin_lock_bh(&host->lock);
819 BUG_ON(host->mrq != NULL);
826 * If there is no card in the slot then
827 * timeout immediatly.
829 if (!(inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT))
831 cmd->error = MMC_ERR_TIMEOUT;
836 * Does the request include data?
840 wbsd_prepare_data(host, cmd->data);
842 if (cmd->data->error != MMC_ERR_NONE)
846 wbsd_send_command(host, cmd);
849 * If this is a data transfer the request
850 * will be finished after the data has
853 if (cmd->data && (cmd->error == MMC_ERR_NONE))
855 spin_unlock_bh(&host->lock);
861 wbsd_request_end(host, mrq);
863 spin_unlock_bh(&host->lock);
866 static void wbsd_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
868 struct wbsd_host* host = mmc_priv(mmc);
871 DBGF("clock %uHz busmode %u powermode %u Vdd %u\n",
872 ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
874 spin_lock_bh(&host->lock);
877 * Reset the chip on each power off.
878 * Should clear out any weird states.
880 if (ios->power_mode == MMC_POWER_OFF)
881 wbsd_init_device(host);
883 if (ios->clock >= 24000000)
885 else if (ios->clock >= 16000000)
887 else if (ios->clock >= 12000000)
893 * Only write to the clock register when
894 * there is an actual change.
896 if (clk != host->clk)
898 wbsd_write_index(host, WBSD_IDX_CLK, clk);
902 if (ios->power_mode != MMC_POWER_OFF)
907 pwr = inb(host->base + WBSD_CSR);
908 pwr &= ~WBSD_POWER_N;
909 outb(pwr, host->base + WBSD_CSR);
912 * This behaviour is stolen from the
913 * Windows driver. Don't know why, but
916 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
917 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
918 setup |= WBSD_DAT3_H;
920 setup &= ~WBSD_DAT3_H;
921 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
926 spin_unlock_bh(&host->lock);
933 inline static struct mmc_data* wbsd_get_data(struct wbsd_host* host)
939 WARN_ON(!host->mrq->cmd);
943 WARN_ON(!host->mrq->cmd->data);
944 if (!host->mrq->cmd->data)
947 return host->mrq->cmd->data;
950 static void wbsd_tasklet_card(unsigned long param)
952 struct wbsd_host* host = (struct wbsd_host*)param;
955 spin_lock(&host->lock);
957 csr = inb(host->base + WBSD_CSR);
958 WARN_ON(csr == 0xff);
960 if (csr & WBSD_CARDPRESENT)
961 DBG("Card inserted\n");
964 DBG("Card removed\n");
968 printk(KERN_ERR DRIVER_NAME
969 ": Card removed during transfer!\n");
972 host->mrq->cmd->error = MMC_ERR_FAILED;
973 tasklet_schedule(&host->finish_tasklet);
978 * Unlock first since we might get a call back.
980 spin_unlock(&host->lock);
982 mmc_detect_change(host->mmc);
985 static void wbsd_tasklet_fifo(unsigned long param)
987 struct wbsd_host* host = (struct wbsd_host*)param;
988 struct mmc_data* data;
990 spin_lock(&host->lock);
995 data = wbsd_get_data(host);
999 if (data->flags & MMC_DATA_WRITE)
1000 wbsd_fill_fifo(host);
1002 wbsd_empty_fifo(host);
1007 if (host->size == data->bytes_xfered)
1009 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1010 tasklet_schedule(&host->finish_tasklet);
1014 spin_unlock(&host->lock);
1017 static void wbsd_tasklet_crc(unsigned long param)
1019 struct wbsd_host* host = (struct wbsd_host*)param;
1020 struct mmc_data* data;
1022 spin_lock(&host->lock);
1024 WARN_ON(!host->mrq);
1028 data = wbsd_get_data(host);
1032 DBGF("CRC error\n");
1034 data->error = MMC_ERR_BADCRC;
1036 tasklet_schedule(&host->finish_tasklet);
1039 spin_unlock(&host->lock);
1042 static void wbsd_tasklet_timeout(unsigned long param)
1044 struct wbsd_host* host = (struct wbsd_host*)param;
1045 struct mmc_data* data;
1047 spin_lock(&host->lock);
1049 WARN_ON(!host->mrq);
1053 data = wbsd_get_data(host);
1059 data->error = MMC_ERR_TIMEOUT;
1061 tasklet_schedule(&host->finish_tasklet);
1064 spin_unlock(&host->lock);
1067 static void wbsd_tasklet_finish(unsigned long param)
1069 struct wbsd_host* host = (struct wbsd_host*)param;
1070 struct mmc_data* data;
1072 spin_lock(&host->lock);
1074 WARN_ON(!host->mrq);
1078 data = wbsd_get_data(host);
1082 wbsd_finish_data(host, data);
1085 spin_unlock(&host->lock);
1088 static void wbsd_tasklet_block(unsigned long param)
1090 struct wbsd_host* host = (struct wbsd_host*)param;
1091 struct mmc_data* data;
1093 spin_lock(&host->lock);
1095 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1098 data = wbsd_get_data(host);
1102 DBGF("CRC error\n");
1104 data->error = MMC_ERR_BADCRC;
1106 tasklet_schedule(&host->finish_tasklet);
1110 spin_unlock(&host->lock);
1114 * Interrupt handling
1117 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1119 struct wbsd_host* host = dev_id;
1122 isr = inb(host->base + WBSD_ISR);
1125 * Was it actually our hardware that caused the interrupt?
1127 if (isr == 0xff || isr == 0x00)
1131 * Schedule tasklets as needed.
1133 if (isr & WBSD_INT_CARD)
1134 tasklet_schedule(&host->card_tasklet);
1135 if (isr & WBSD_INT_FIFO_THRE)
1136 tasklet_hi_schedule(&host->fifo_tasklet);
1137 if (isr & WBSD_INT_CRC)
1138 tasklet_hi_schedule(&host->crc_tasklet);
1139 if (isr & WBSD_INT_TIMEOUT)
1140 tasklet_hi_schedule(&host->timeout_tasklet);
1141 if (isr & WBSD_INT_BUSYEND)
1142 tasklet_hi_schedule(&host->block_tasklet);
1143 if (isr & WBSD_INT_TC)
1144 tasklet_schedule(&host->finish_tasklet);
1150 * Support functions for probe
1153 static int wbsd_scan(struct wbsd_host* host)
1159 * Iterate through all ports, all codes to
1160 * find hardware that is in our known list.
1162 for (i = 0;i < sizeof(config_ports)/sizeof(int);i++)
1164 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1167 for (j = 0;j < sizeof(unlock_codes)/sizeof(int);j++)
1171 outb(unlock_codes[j], config_ports[i]);
1172 outb(unlock_codes[j], config_ports[i]);
1174 outb(WBSD_CONF_ID_HI, config_ports[i]);
1175 id = inb(config_ports[i] + 1) << 8;
1177 outb(WBSD_CONF_ID_LO, config_ports[i]);
1178 id |= inb(config_ports[i] + 1);
1180 for (k = 0;k < sizeof(valid_ids)/sizeof(int);k++)
1182 if (id == valid_ids[k])
1185 host->config = config_ports[i];
1186 host->unlock_code = unlock_codes[i];
1194 DBG("Unknown hardware (id %x) found at %x\n",
1195 id, config_ports[i]);
1198 outb(LOCK_CODE, config_ports[i]);
1201 release_region(config_ports[i], 2);
1207 static int wbsd_request_regions(struct wbsd_host* host)
1212 if (!request_region(io, 8, DRIVER_NAME))
1220 static void wbsd_release_regions(struct wbsd_host* host)
1223 release_region(host->base, 8);
1226 release_region(host->config, 2);
1229 static void wbsd_init_dma(struct wbsd_host* host)
1236 if (request_dma(dma, DRIVER_NAME))
1240 * We need to allocate a special buffer in
1241 * order for ISA to be able to DMA to it.
1243 host->dma_buffer = kmalloc(65536,
1244 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1245 if (!host->dma_buffer)
1249 * Translate the address to a physical address.
1251 host->dma_addr = isa_virt_to_bus(host->dma_buffer);
1254 * ISA DMA must be aligned on a 64k basis.
1256 if ((host->dma_addr & 0xffff) != 0)
1259 * ISA cannot access memory above 16 MB.
1261 else if (host->dma_addr >= 0x1000000)
1270 * If we've gotten here then there is some kind of alignment bug
1274 kfree(host->dma_buffer);
1275 host->dma_buffer = NULL;
1281 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1282 "Falling back on FIFO.\n", dma);
1285 static struct mmc_host_ops wbsd_ops = {
1286 .request = wbsd_request,
1287 .set_ios = wbsd_set_ios,
1294 static int wbsd_probe(struct device* dev)
1296 struct wbsd_host* host = NULL;
1297 struct mmc_host* mmc = NULL;
1301 * Allocate MMC structure.
1303 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1307 host = mmc_priv(mmc);
1311 * Scan for hardware.
1313 ret = wbsd_scan(host);
1320 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1321 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1324 * Allocate I/O ports.
1326 ret = wbsd_request_regions(host);
1331 * Set host parameters.
1333 mmc->ops = &wbsd_ops;
1334 mmc->f_min = 375000;
1335 mmc->f_max = 24000000;
1336 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1338 spin_lock_init(&host->lock);
1341 * Select SD/MMC function.
1343 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1346 * Set up card detection.
1348 wbsd_write_config(host, WBSD_CONF_PINS, 0x02);
1351 * Configure I/O port.
1353 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1354 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1357 * Allocate interrupt.
1359 ret = request_irq(irq, wbsd_irq, SA_SHIRQ, DRIVER_NAME, host);
1368 tasklet_init(&host->card_tasklet, wbsd_tasklet_card, (unsigned long)host);
1369 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo, (unsigned long)host);
1370 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc, (unsigned long)host);
1371 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout, (unsigned long)host);
1372 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish, (unsigned long)host);
1373 tasklet_init(&host->block_tasklet, wbsd_tasklet_block, (unsigned long)host);
1376 * Configure interrupt.
1378 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1383 wbsd_init_dma(host);
1386 * If all went well, then configure DMA.
1389 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1392 * Maximum number of segments. Worst case is one sector per segment
1393 * so this will be 64kB/512.
1395 mmc->max_hw_segs = NR_SG;
1396 mmc->max_phys_segs = NR_SG;
1399 * Maximum number of sectors in one transfer. Also limited by 64kB
1402 mmc->max_sectors = 128;
1405 * Maximum segment size. Could be one segment with the maximum number
1408 mmc->max_seg_size = mmc->max_sectors * 512;
1413 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1418 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1421 * Power Management stuff. No idea how this works.
1425 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1429 * Reset the chip into a known state.
1431 wbsd_init_device(host);
1433 dev_set_drvdata(dev, mmc);
1436 * Add host to MMC layer.
1440 printk(KERN_INFO "%s: W83L51xD id %x at 0x%x irq %d dma %d\n",
1441 mmc->host_name, (int)host->chip_id, (int)host->base,
1442 (int)host->irq, (int)host->dma);
1447 wbsd_release_regions(host);
1459 static int wbsd_remove(struct device* dev)
1461 struct mmc_host* mmc = dev_get_drvdata(dev);
1462 struct wbsd_host* host;
1467 host = mmc_priv(mmc);
1470 * Unregister host with MMC layer.
1472 mmc_remove_host(mmc);
1475 * Power down the SD/MMC function.
1477 wbsd_unlock_config(host);
1478 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1479 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1480 wbsd_lock_config(host);
1485 if (host->dma_buffer)
1486 kfree(host->dma_buffer);
1489 free_dma(host->dma);
1491 free_irq(host->irq, host);
1493 tasklet_kill(&host->card_tasklet);
1494 tasklet_kill(&host->fifo_tasklet);
1495 tasklet_kill(&host->crc_tasklet);
1496 tasklet_kill(&host->timeout_tasklet);
1497 tasklet_kill(&host->finish_tasklet);
1498 tasklet_kill(&host->block_tasklet);
1500 wbsd_release_regions(host);
1512 static int wbsd_suspend(struct device *dev, u32 state, u32 level)
1514 DBGF("Not yet supported\n");
1519 static int wbsd_resume(struct device *dev, u32 level)
1521 DBGF("Not yet supported\n");
1526 #define wbsd_suspend NULL
1527 #define wbsd_resume NULL
1530 static void wbsd_release(struct device *dev)
1534 static struct platform_device wbsd_device = {
1535 .name = DRIVER_NAME,
1538 .release = wbsd_release,
1542 static struct device_driver wbsd_driver = {
1543 .name = DRIVER_NAME,
1544 .bus = &platform_bus_type,
1545 .probe = wbsd_probe,
1546 .remove = wbsd_remove,
1548 .suspend = wbsd_suspend,
1549 .resume = wbsd_resume,
1553 * Module loading/unloading
1556 static int __init wbsd_drv_init(void)
1560 printk(KERN_INFO DRIVER_NAME
1561 ": Winbond W83L51xD SD/MMC card interface driver, "
1562 DRIVER_VERSION "\n");
1563 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1565 result = driver_register(&wbsd_driver);
1569 result = platform_device_register(&wbsd_device);
1576 static void __exit wbsd_drv_exit(void)
1578 platform_device_unregister(&wbsd_device);
1580 driver_unregister(&wbsd_driver);
1585 module_init(wbsd_drv_init);
1586 module_exit(wbsd_drv_exit);
1587 module_param(io, uint, 0444);
1588 module_param(irq, uint, 0444);
1589 module_param(dma, int, 0444);
1591 MODULE_LICENSE("GPL");
1592 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
1594 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1595 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
1596 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");