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/delay.h>
32 #include <linux/highmem.h>
33 #include <linux/mmc/host.h>
34 #include <linux/mmc/protocol.h>
38 #include <asm/scatterlist.h>
42 #define DRIVER_NAME "wbsd"
43 #define DRIVER_VERSION "1.1"
45 #ifdef CONFIG_MMC_DEBUG
47 printk(KERN_DEBUG DRIVER_NAME ": " x)
48 #define DBGF(f, x...) \
49 printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__ , ##x)
51 #define DBG(x...) do { } while (0)
52 #define DBGF(x...) do { } while (0)
55 static unsigned int io
= 0x248;
56 static unsigned int irq
= 6;
59 #ifdef CONFIG_MMC_DEBUG
60 void DBG_REG(int reg
, u8 value
)
64 printk(KERN_DEBUG
"wbsd: Register %d: 0x%02X %3d '%c' ",
65 reg
, (int)value
, (int)value
, (value
< 0x20)?'.':value
);
67 for (i
= 7;i
>= 0;i
--)
78 #define DBG_REG(r, v) do {} while (0)
85 static inline void wbsd_unlock_config(struct wbsd_host
* host
)
87 outb(host
->unlock_code
, host
->config
);
88 outb(host
->unlock_code
, host
->config
);
91 static inline void wbsd_lock_config(struct wbsd_host
* host
)
93 outb(LOCK_CODE
, host
->config
);
96 static inline void wbsd_write_config(struct wbsd_host
* host
, u8 reg
, u8 value
)
98 outb(reg
, host
->config
);
99 outb(value
, host
->config
+ 1);
102 static inline u8
wbsd_read_config(struct wbsd_host
* host
, u8 reg
)
104 outb(reg
, host
->config
);
105 return inb(host
->config
+ 1);
108 static inline void wbsd_write_index(struct wbsd_host
* host
, u8 index
, u8 value
)
110 outb(index
, host
->base
+ WBSD_IDXR
);
111 outb(value
, host
->base
+ WBSD_DATAR
);
114 static inline u8
wbsd_read_index(struct wbsd_host
* host
, u8 index
)
116 outb(index
, host
->base
+ WBSD_IDXR
);
117 return inb(host
->base
+ WBSD_DATAR
);
124 static void wbsd_init_device(struct wbsd_host
* host
)
129 * Reset chip (SD/MMC part) and fifo.
131 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
132 setup
|= WBSD_FIFO_RESET
| WBSD_SOFT_RESET
;
133 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
136 * Read back default clock.
138 host
->clk
= wbsd_read_index(host
, WBSD_IDX_CLK
);
143 outb(WBSD_POWER_N
, host
->base
+ WBSD_CSR
);
146 * Set maximum timeout.
148 wbsd_write_index(host
, WBSD_IDX_TAAC
, 0x7F);
151 * Enable interesting interrupts.
154 ier
|= WBSD_EINT_CARD
;
155 ier
|= WBSD_EINT_FIFO_THRE
;
156 ier
|= WBSD_EINT_CCRC
;
157 ier
|= WBSD_EINT_TIMEOUT
;
158 ier
|= WBSD_EINT_CRC
;
161 outb(ier
, host
->base
+ WBSD_EIR
);
166 inb(host
->base
+ WBSD_ISR
);
169 static void wbsd_reset(struct wbsd_host
* host
)
173 printk(KERN_ERR DRIVER_NAME
": Resetting chip\n");
176 * Soft reset of chip (SD/MMC part).
178 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
179 setup
|= WBSD_SOFT_RESET
;
180 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
183 static void wbsd_request_end(struct wbsd_host
* host
, struct mmc_request
* mrq
)
185 unsigned long dmaflags
;
187 DBGF("Ending request, cmd (%x)\n", mrq
->cmd
->opcode
);
192 * Release ISA DMA controller.
194 dmaflags
= claim_dma_lock();
195 disable_dma(host
->dma
);
196 clear_dma_ff(host
->dma
);
197 release_dma_lock(dmaflags
);
200 * Disable DMA on host.
202 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
208 * MMC layer might call back into the driver so first unlock.
210 spin_unlock(&host
->lock
);
211 mmc_request_done(host
->mmc
, mrq
);
212 spin_lock(&host
->lock
);
216 * Scatter/gather functions
219 static inline void wbsd_init_sg(struct wbsd_host
* host
, struct mmc_data
* data
)
222 * Get info. about SG list from data structure.
224 host
->cur_sg
= data
->sg
;
225 host
->num_sg
= data
->sg_len
;
228 host
->remain
= host
->cur_sg
->length
;
231 static inline int wbsd_next_sg(struct wbsd_host
* host
)
234 * Skip to next SG entry.
242 if (host
->num_sg
> 0)
245 host
->remain
= host
->cur_sg
->length
;
251 static inline char* wbsd_kmap_sg(struct wbsd_host
* host
)
253 host
->mapped_sg
= kmap_atomic(host
->cur_sg
->page
, KM_BIO_SRC_IRQ
) +
254 host
->cur_sg
->offset
;
255 return host
->mapped_sg
;
258 static inline void wbsd_kunmap_sg(struct wbsd_host
* host
)
260 kunmap_atomic(host
->mapped_sg
, KM_BIO_SRC_IRQ
);
263 static inline void wbsd_sg_to_dma(struct wbsd_host
* host
, struct mmc_data
* data
)
265 unsigned int len
, i
, size
;
266 struct scatterlist
* sg
;
267 char* dmabuf
= host
->dma_buffer
;
276 * Just loop through all entries. Size might not
277 * be the entire list though so make sure that
278 * we do not transfer too much.
280 for (i
= 0;i
< len
;i
++)
282 sgbuf
= kmap_atomic(sg
[i
].page
, KM_BIO_SRC_IRQ
) + sg
[i
].offset
;
283 if (size
< sg
[i
].length
)
284 memcpy(dmabuf
, sgbuf
, size
);
286 memcpy(dmabuf
, sgbuf
, sg
[i
].length
);
287 kunmap_atomic(sgbuf
, KM_BIO_SRC_IRQ
);
288 dmabuf
+= sg
[i
].length
;
290 if (size
< sg
[i
].length
)
293 size
-= sg
[i
].length
;
300 * Check that we didn't get a request to transfer
301 * more data than can fit into the SG list.
309 static inline void wbsd_dma_to_sg(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(sgbuf
, dmabuf
, size
);
332 memcpy(sgbuf
, dmabuf
, 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.
359 static inline void wbsd_get_short_reply(struct wbsd_host
* host
,
360 struct mmc_command
* cmd
)
363 * Correct response type?
365 if (wbsd_read_index(host
, WBSD_IDX_RSPLEN
) != WBSD_RSP_SHORT
)
367 cmd
->error
= MMC_ERR_INVALID
;
372 wbsd_read_index(host
, WBSD_IDX_RESP12
) << 24;
374 wbsd_read_index(host
, WBSD_IDX_RESP13
) << 16;
376 wbsd_read_index(host
, WBSD_IDX_RESP14
) << 8;
378 wbsd_read_index(host
, WBSD_IDX_RESP15
) << 0;
380 wbsd_read_index(host
, WBSD_IDX_RESP16
) << 24;
383 static inline void wbsd_get_long_reply(struct wbsd_host
* host
,
384 struct mmc_command
* cmd
)
389 * Correct response type?
391 if (wbsd_read_index(host
, WBSD_IDX_RSPLEN
) != WBSD_RSP_LONG
)
393 cmd
->error
= MMC_ERR_INVALID
;
397 for (i
= 0;i
< 4;i
++)
400 wbsd_read_index(host
, WBSD_IDX_RESP1
+ i
* 4) << 24;
402 wbsd_read_index(host
, WBSD_IDX_RESP2
+ i
* 4) << 16;
404 wbsd_read_index(host
, WBSD_IDX_RESP3
+ i
* 4) << 8;
406 wbsd_read_index(host
, WBSD_IDX_RESP4
+ i
* 4) << 0;
410 static irqreturn_t
wbsd_irq(int irq
, void *dev_id
, struct pt_regs
*regs
);
412 static void wbsd_send_command(struct wbsd_host
* host
, struct mmc_command
* cmd
)
417 DBGF("Sending cmd (%x)\n", cmd
->opcode
);
420 * Clear accumulated ISR. The interrupt routine
421 * will fill this one with events that occur during
427 * Send the command (CRC calculated by host).
429 outb(cmd
->opcode
, host
->base
+ WBSD_CMDR
);
430 for (i
= 3;i
>= 0;i
--)
431 outb((cmd
->arg
>> (i
* 8)) & 0xff, host
->base
+ WBSD_CMDR
);
433 cmd
->error
= MMC_ERR_NONE
;
436 * Wait for the request to complete.
439 status
= wbsd_read_index(host
, WBSD_IDX_STATUS
);
440 } while (status
& WBSD_CARDTRAFFIC
);
443 * Do we expect a reply?
445 if ((cmd
->flags
& MMC_RSP_MASK
) != MMC_RSP_NONE
)
453 if (isr
& WBSD_INT_CARD
)
454 cmd
->error
= MMC_ERR_TIMEOUT
;
456 else if (isr
& WBSD_INT_TIMEOUT
)
457 cmd
->error
= MMC_ERR_TIMEOUT
;
459 else if ((cmd
->flags
& MMC_RSP_CRC
) && (isr
& WBSD_INT_CRC
))
460 cmd
->error
= MMC_ERR_BADCRC
;
464 if ((cmd
->flags
& MMC_RSP_MASK
) == MMC_RSP_SHORT
)
465 wbsd_get_short_reply(host
, cmd
);
467 wbsd_get_long_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_kmap_sg(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
))
499 * The size field in the FSR is broken so we have to
502 if (fsr
& WBSD_FIFO_FULL
)
504 else if (fsr
& WBSD_FIFO_FUTHRE
)
509 for (i
= 0;i
< fifo
;i
++)
511 *buffer
= inb(host
->base
+ WBSD_DFR
);
516 data
->bytes_xfered
++;
521 if (data
->bytes_xfered
== host
->size
)
523 wbsd_kunmap_sg(host
);
528 * End of scatter list entry?
530 if (host
->remain
== 0)
532 wbsd_kunmap_sg(host
);
535 * Get next entry. Check if last.
537 if (!wbsd_next_sg(host
))
540 * We should never reach this point.
541 * It means that we're trying to
542 * transfer more blocks than can fit
543 * into the scatter list.
547 host
->size
= data
->bytes_xfered
;
552 buffer
= wbsd_kmap_sg(host
);
557 wbsd_kunmap_sg(host
);
560 * This is a very dirty hack to solve a
561 * hardware problem. The chip doesn't trigger
562 * FIFO threshold interrupts properly.
564 if ((host
->size
- data
->bytes_xfered
) < 16)
565 tasklet_schedule(&host
->fifo_tasklet
);
568 static void wbsd_fill_fifo(struct wbsd_host
* host
)
570 struct mmc_data
* data
= host
->mrq
->cmd
->data
;
575 * Check that we aren't being called after the
576 * entire buffer has been transfered.
578 if (data
->bytes_xfered
== host
->size
)
581 buffer
= wbsd_kmap_sg(host
) + host
->offset
;
584 * Fill the fifo. This has a tendency to loop longer
585 * than the FIFO length (usually one block).
587 while (!((fsr
= inb(host
->base
+ WBSD_FSR
)) & WBSD_FIFO_FULL
))
590 * The size field in the FSR is broken so we have to
593 if (fsr
& WBSD_FIFO_EMPTY
)
595 else if (fsr
& WBSD_FIFO_EMTHRE
)
600 for (i
= 16;i
> fifo
;i
--)
602 outb(*buffer
, host
->base
+ WBSD_DFR
);
607 data
->bytes_xfered
++;
612 if (data
->bytes_xfered
== host
->size
)
614 wbsd_kunmap_sg(host
);
619 * End of scatter list entry?
621 if (host
->remain
== 0)
623 wbsd_kunmap_sg(host
);
626 * Get next entry. Check if last.
628 if (!wbsd_next_sg(host
))
631 * We should never reach this point.
632 * It means that we're trying to
633 * transfer more blocks than can fit
634 * into the scatter list.
638 host
->size
= data
->bytes_xfered
;
643 buffer
= wbsd_kmap_sg(host
);
648 wbsd_kunmap_sg(host
);
651 static void wbsd_prepare_data(struct wbsd_host
* host
, struct mmc_data
* data
)
655 unsigned long dmaflags
;
657 DBGF("blksz %04x blks %04x flags %08x\n",
658 1 << data
->blksz_bits
, data
->blocks
, data
->flags
);
659 DBGF("tsac %d ms nsac %d clk\n",
660 data
->timeout_ns
/ 1000000, data
->timeout_clks
);
665 host
->size
= data
->blocks
<< data
->blksz_bits
;
668 * Check timeout values for overflow.
669 * (Yes, some cards cause this value to overflow).
671 if (data
->timeout_ns
> 127000000)
672 wbsd_write_index(host
, WBSD_IDX_TAAC
, 127);
674 wbsd_write_index(host
, WBSD_IDX_TAAC
, data
->timeout_ns
/1000000);
676 if (data
->timeout_clks
> 255)
677 wbsd_write_index(host
, WBSD_IDX_NSAC
, 255);
679 wbsd_write_index(host
, WBSD_IDX_NSAC
, data
->timeout_clks
);
682 * Inform the chip of how large blocks will be
683 * sent. It needs this to determine when to
686 * Space for CRC must be included in the size.
688 blksize
= (1 << data
->blksz_bits
) + 2;
690 wbsd_write_index(host
, WBSD_IDX_PBSMSB
, (blksize
>> 4) & 0xF0);
691 wbsd_write_index(host
, WBSD_IDX_PBSLSB
, blksize
& 0xFF);
694 * Clear the FIFO. This is needed even for DMA
695 * transfers since the chip still uses the FIFO
698 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
699 setup
|= WBSD_FIFO_RESET
;
700 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
708 * The buffer for DMA is only 64 kB.
710 BUG_ON(host
->size
> 0x10000);
711 if (host
->size
> 0x10000)
713 data
->error
= MMC_ERR_INVALID
;
718 * Transfer data from the SG list to
721 if (data
->flags
& MMC_DATA_WRITE
)
722 wbsd_sg_to_dma(host
, data
);
725 * Initialise the ISA DMA controller.
727 dmaflags
= claim_dma_lock();
728 disable_dma(host
->dma
);
729 clear_dma_ff(host
->dma
);
730 if (data
->flags
& MMC_DATA_READ
)
731 set_dma_mode(host
->dma
, DMA_MODE_READ
& ~0x40);
733 set_dma_mode(host
->dma
, DMA_MODE_WRITE
& ~0x40);
734 set_dma_addr(host
->dma
, host
->dma_addr
);
735 set_dma_count(host
->dma
, host
->size
);
737 enable_dma(host
->dma
);
738 release_dma_lock(dmaflags
);
741 * Enable DMA on the host.
743 wbsd_write_index(host
, WBSD_IDX_DMA
, WBSD_DMA_ENABLE
);
748 * This flag is used to keep printk
749 * output to a minimum.
754 * Initialise the SG list.
756 wbsd_init_sg(host
, data
);
761 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
764 * Set up FIFO threshold levels (and fill
765 * buffer if doing a write).
767 if (data
->flags
& MMC_DATA_READ
)
769 wbsd_write_index(host
, WBSD_IDX_FIFOEN
,
770 WBSD_FIFOEN_FULL
| 8);
774 wbsd_write_index(host
, WBSD_IDX_FIFOEN
,
775 WBSD_FIFOEN_EMPTY
| 8);
776 wbsd_fill_fifo(host
);
780 data
->error
= MMC_ERR_NONE
;
783 static void wbsd_finish_data(struct wbsd_host
* host
, struct mmc_data
* data
)
785 unsigned long dmaflags
;
789 WARN_ON(host
->mrq
== NULL
);
792 * Send a stop command if needed.
795 wbsd_send_command(host
, data
->stop
);
798 * Wait for the controller to leave data
803 status
= wbsd_read_index(host
, WBSD_IDX_STATUS
);
804 } while (status
& (WBSD_BLOCK_READ
| WBSD_BLOCK_WRITE
));
812 * Disable DMA on the host.
814 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
817 * Turn of ISA DMA controller.
819 dmaflags
= claim_dma_lock();
820 disable_dma(host
->dma
);
821 clear_dma_ff(host
->dma
);
822 count
= get_dma_residue(host
->dma
);
823 release_dma_lock(dmaflags
);
830 printk(KERN_ERR DRIVER_NAME
": Incomplete DMA "
831 "transfer. %d bytes left.\n", count
);
833 data
->error
= MMC_ERR_FAILED
;
838 * Transfer data from DMA buffer to
841 if (data
->flags
& MMC_DATA_READ
)
842 wbsd_dma_to_sg(host
, data
);
844 data
->bytes_xfered
= host
->size
;
848 DBGF("Ending data transfer (%d bytes)\n", data
->bytes_xfered
);
850 wbsd_request_end(host
, host
->mrq
);
857 static void wbsd_request(struct mmc_host
* mmc
, struct mmc_request
* mrq
)
859 struct wbsd_host
* host
= mmc_priv(mmc
);
860 struct mmc_command
* cmd
;
863 * Disable tasklets to avoid a deadlock.
865 spin_lock_bh(&host
->lock
);
867 BUG_ON(host
->mrq
!= NULL
);
874 * If there is no card in the slot then
875 * timeout immediatly.
877 if (!(inb(host
->base
+ WBSD_CSR
) & WBSD_CARDPRESENT
))
879 cmd
->error
= MMC_ERR_TIMEOUT
;
884 * Does the request include data?
888 wbsd_prepare_data(host
, cmd
->data
);
890 if (cmd
->data
->error
!= MMC_ERR_NONE
)
894 wbsd_send_command(host
, cmd
);
897 * If this is a data transfer the request
898 * will be finished after the data has
901 if (cmd
->data
&& (cmd
->error
== MMC_ERR_NONE
))
904 * Dirty fix for hardware bug.
907 tasklet_schedule(&host
->fifo_tasklet
);
909 spin_unlock_bh(&host
->lock
);
915 wbsd_request_end(host
, mrq
);
917 spin_unlock_bh(&host
->lock
);
920 static void wbsd_set_ios(struct mmc_host
* mmc
, struct mmc_ios
* ios
)
922 struct wbsd_host
* host
= mmc_priv(mmc
);
925 DBGF("clock %uHz busmode %u powermode %u Vdd %u\n",
926 ios
->clock
, ios
->bus_mode
, ios
->power_mode
, ios
->vdd
);
928 spin_lock_bh(&host
->lock
);
931 * Reset the chip on each power off.
932 * Should clear out any weird states.
934 if (ios
->power_mode
== MMC_POWER_OFF
)
935 wbsd_init_device(host
);
937 if (ios
->clock
>= 24000000)
939 else if (ios
->clock
>= 16000000)
941 else if (ios
->clock
>= 12000000)
947 * Only write to the clock register when
948 * there is an actual change.
950 if (clk
!= host
->clk
)
952 wbsd_write_index(host
, WBSD_IDX_CLK
, clk
);
956 if (ios
->power_mode
!= MMC_POWER_OFF
)
961 pwr
= inb(host
->base
+ WBSD_CSR
);
962 pwr
&= ~WBSD_POWER_N
;
963 outb(pwr
, host
->base
+ WBSD_CSR
);
966 * This behaviour is stolen from the
967 * Windows driver. Don't know why, but
970 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
971 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
972 setup
|= WBSD_DAT3_H
;
974 setup
&= ~WBSD_DAT3_H
;
975 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
980 spin_unlock_bh(&host
->lock
);
987 inline static struct mmc_data
* wbsd_get_data(struct wbsd_host
* host
)
993 WARN_ON(!host
->mrq
->cmd
);
997 WARN_ON(!host
->mrq
->cmd
->data
);
998 if (!host
->mrq
->cmd
->data
)
1001 return host
->mrq
->cmd
->data
;
1004 static void wbsd_tasklet_card(unsigned long param
)
1006 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1009 spin_lock(&host
->lock
);
1011 csr
= inb(host
->base
+ WBSD_CSR
);
1012 WARN_ON(csr
== 0xff);
1014 if (csr
& WBSD_CARDPRESENT
)
1015 DBG("Card inserted\n");
1018 DBG("Card removed\n");
1022 printk(KERN_ERR DRIVER_NAME
1023 ": Card removed during transfer!\n");
1026 host
->mrq
->cmd
->error
= MMC_ERR_FAILED
;
1027 tasklet_schedule(&host
->finish_tasklet
);
1032 * Unlock first since we might get a call back.
1034 spin_unlock(&host
->lock
);
1036 mmc_detect_change(host
->mmc
);
1039 static void wbsd_tasklet_fifo(unsigned long param
)
1041 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1042 struct mmc_data
* data
;
1044 spin_lock(&host
->lock
);
1049 data
= wbsd_get_data(host
);
1053 if (data
->flags
& MMC_DATA_WRITE
)
1054 wbsd_fill_fifo(host
);
1056 wbsd_empty_fifo(host
);
1061 if (host
->size
== data
->bytes_xfered
)
1063 wbsd_write_index(host
, WBSD_IDX_FIFOEN
, 0);
1064 tasklet_schedule(&host
->finish_tasklet
);
1068 spin_unlock(&host
->lock
);
1071 static void wbsd_tasklet_crc(unsigned long param
)
1073 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1074 struct mmc_data
* data
;
1076 spin_lock(&host
->lock
);
1081 data
= wbsd_get_data(host
);
1085 DBGF("CRC error\n");
1087 data
->error
= MMC_ERR_BADCRC
;
1089 tasklet_schedule(&host
->finish_tasklet
);
1092 spin_unlock(&host
->lock
);
1095 static void wbsd_tasklet_timeout(unsigned long param
)
1097 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1098 struct mmc_data
* data
;
1100 spin_lock(&host
->lock
);
1105 data
= wbsd_get_data(host
);
1111 data
->error
= MMC_ERR_TIMEOUT
;
1113 tasklet_schedule(&host
->finish_tasklet
);
1116 spin_unlock(&host
->lock
);
1119 static void wbsd_tasklet_finish(unsigned long param
)
1121 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1122 struct mmc_data
* data
;
1124 spin_lock(&host
->lock
);
1126 WARN_ON(!host
->mrq
);
1130 data
= wbsd_get_data(host
);
1134 wbsd_finish_data(host
, data
);
1137 spin_unlock(&host
->lock
);
1140 static void wbsd_tasklet_block(unsigned long param
)
1142 struct wbsd_host
* host
= (struct wbsd_host
*)param
;
1143 struct mmc_data
* data
;
1145 spin_lock(&host
->lock
);
1147 if ((wbsd_read_index(host
, WBSD_IDX_CRCSTATUS
) & WBSD_CRC_MASK
) !=
1150 data
= wbsd_get_data(host
);
1154 DBGF("CRC error\n");
1156 data
->error
= MMC_ERR_BADCRC
;
1158 tasklet_schedule(&host
->finish_tasklet
);
1162 spin_unlock(&host
->lock
);
1166 * Interrupt handling
1169 static irqreturn_t
wbsd_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
1171 struct wbsd_host
* host
= dev_id
;
1174 isr
= inb(host
->base
+ WBSD_ISR
);
1177 * Was it actually our hardware that caused the interrupt?
1179 if (isr
== 0xff || isr
== 0x00)
1185 * Schedule tasklets as needed.
1187 if (isr
& WBSD_INT_CARD
)
1188 tasklet_schedule(&host
->card_tasklet
);
1189 if (isr
& WBSD_INT_FIFO_THRE
)
1190 tasklet_schedule(&host
->fifo_tasklet
);
1191 if (isr
& WBSD_INT_CRC
)
1192 tasklet_hi_schedule(&host
->crc_tasklet
);
1193 if (isr
& WBSD_INT_TIMEOUT
)
1194 tasklet_hi_schedule(&host
->timeout_tasklet
);
1195 if (isr
& WBSD_INT_BUSYEND
)
1196 tasklet_hi_schedule(&host
->block_tasklet
);
1197 if (isr
& WBSD_INT_TC
)
1198 tasklet_schedule(&host
->finish_tasklet
);
1204 * Support functions for probe
1207 static int wbsd_scan(struct wbsd_host
* host
)
1213 * Iterate through all ports, all codes to
1214 * find hardware that is in our known list.
1216 for (i
= 0;i
< sizeof(config_ports
)/sizeof(int);i
++)
1218 if (!request_region(config_ports
[i
], 2, DRIVER_NAME
))
1221 for (j
= 0;j
< sizeof(unlock_codes
)/sizeof(int);j
++)
1225 outb(unlock_codes
[j
], config_ports
[i
]);
1226 outb(unlock_codes
[j
], config_ports
[i
]);
1228 outb(WBSD_CONF_ID_HI
, config_ports
[i
]);
1229 id
= inb(config_ports
[i
] + 1) << 8;
1231 outb(WBSD_CONF_ID_LO
, config_ports
[i
]);
1232 id
|= inb(config_ports
[i
] + 1);
1234 for (k
= 0;k
< sizeof(valid_ids
)/sizeof(int);k
++)
1236 if (id
== valid_ids
[k
])
1239 host
->config
= config_ports
[i
];
1240 host
->unlock_code
= unlock_codes
[i
];
1248 DBG("Unknown hardware (id %x) found at %x\n",
1249 id
, config_ports
[i
]);
1252 outb(LOCK_CODE
, config_ports
[i
]);
1255 release_region(config_ports
[i
], 2);
1261 static int wbsd_request_regions(struct wbsd_host
* host
)
1266 if (!request_region(io
, 8, DRIVER_NAME
))
1274 static void wbsd_release_regions(struct wbsd_host
* host
)
1277 release_region(host
->base
, 8);
1280 release_region(host
->config
, 2);
1283 static void wbsd_init_dma(struct wbsd_host
* host
)
1290 if (request_dma(dma
, DRIVER_NAME
))
1294 * We need to allocate a special buffer in
1295 * order for ISA to be able to DMA to it.
1297 host
->dma_buffer
= kmalloc(65536,
1298 GFP_NOIO
| GFP_DMA
| __GFP_REPEAT
| __GFP_NOWARN
);
1299 if (!host
->dma_buffer
)
1303 * Translate the address to a physical address.
1305 host
->dma_addr
= isa_virt_to_bus(host
->dma_buffer
);
1308 * ISA DMA must be aligned on a 64k basis.
1310 if ((host
->dma_addr
& 0xffff) != 0)
1313 * ISA cannot access memory above 16 MB.
1315 else if (host
->dma_addr
>= 0x1000000)
1324 * If we've gotten here then there is some kind of alignment bug
1328 kfree(host
->dma_buffer
);
1329 host
->dma_buffer
= NULL
;
1335 printk(KERN_WARNING DRIVER_NAME
": Unable to allocate DMA %d. "
1336 "Falling back on FIFO.\n", dma
);
1339 static struct mmc_host_ops wbsd_ops
= {
1340 .request
= wbsd_request
,
1341 .set_ios
= wbsd_set_ios
,
1348 static int wbsd_probe(struct device
* dev
)
1350 struct wbsd_host
* host
= NULL
;
1351 struct mmc_host
* mmc
= NULL
;
1355 * Allocate MMC structure.
1357 mmc
= mmc_alloc_host(sizeof(struct wbsd_host
), dev
);
1361 host
= mmc_priv(mmc
);
1365 * Scan for hardware.
1367 ret
= wbsd_scan(host
);
1374 wbsd_write_config(host
, WBSD_CONF_SWRST
, 1);
1375 wbsd_write_config(host
, WBSD_CONF_SWRST
, 0);
1378 * Allocate I/O ports.
1380 ret
= wbsd_request_regions(host
);
1385 * Set host parameters.
1387 mmc
->ops
= &wbsd_ops
;
1388 mmc
->f_min
= 375000;
1389 mmc
->f_max
= 24000000;
1390 mmc
->ocr_avail
= MMC_VDD_32_33
|MMC_VDD_33_34
;
1392 spin_lock_init(&host
->lock
);
1395 * Select SD/MMC function.
1397 wbsd_write_config(host
, WBSD_CONF_DEVICE
, DEVICE_SD
);
1400 * Set up card detection.
1402 wbsd_write_config(host
, WBSD_CONF_PINS
, 0x02);
1405 * Configure I/O port.
1407 wbsd_write_config(host
, WBSD_CONF_PORT_HI
, host
->base
>> 8);
1408 wbsd_write_config(host
, WBSD_CONF_PORT_LO
, host
->base
& 0xff);
1411 * Allocate interrupt.
1413 ret
= request_irq(irq
, wbsd_irq
, SA_SHIRQ
, DRIVER_NAME
, host
);
1422 tasklet_init(&host
->card_tasklet
, wbsd_tasklet_card
, (unsigned long)host
);
1423 tasklet_init(&host
->fifo_tasklet
, wbsd_tasklet_fifo
, (unsigned long)host
);
1424 tasklet_init(&host
->crc_tasklet
, wbsd_tasklet_crc
, (unsigned long)host
);
1425 tasklet_init(&host
->timeout_tasklet
, wbsd_tasklet_timeout
, (unsigned long)host
);
1426 tasklet_init(&host
->finish_tasklet
, wbsd_tasklet_finish
, (unsigned long)host
);
1427 tasklet_init(&host
->block_tasklet
, wbsd_tasklet_block
, (unsigned long)host
);
1430 * Configure interrupt.
1432 wbsd_write_config(host
, WBSD_CONF_IRQ
, host
->irq
);
1437 wbsd_init_dma(host
);
1440 * If all went well, then configure DMA.
1443 wbsd_write_config(host
, WBSD_CONF_DRQ
, host
->dma
);
1446 * Maximum number of segments. Worst case is one sector per segment
1447 * so this will be 64kB/512.
1449 mmc
->max_hw_segs
= 128;
1450 mmc
->max_phys_segs
= 128;
1453 * Maximum number of sectors in one transfer. Also limited by 64kB
1456 mmc
->max_sectors
= 128;
1459 * Maximum segment size. Could be one segment with the maximum number
1462 mmc
->max_seg_size
= mmc
->max_sectors
* 512;
1467 wbsd_write_config(host
, WBSD_CONF_ENABLE
, 1);
1472 wbsd_write_config(host
, WBSD_CONF_POWER
, 0x20);
1475 * Power Management stuff. No idea how this works.
1479 wbsd_write_config(host
, WBSD_CONF_PME
, 0xA0);
1483 * Reset the chip into a known state.
1485 wbsd_init_device(host
);
1487 dev_set_drvdata(dev
, mmc
);
1490 * Add host to MMC layer.
1494 printk(KERN_INFO
"%s: W83L51xD id %x at 0x%x irq %d dma %d\n",
1495 mmc
->host_name
, (int)host
->chip_id
, (int)host
->base
,
1496 (int)host
->irq
, (int)host
->dma
);
1501 wbsd_release_regions(host
);
1513 static int wbsd_remove(struct device
* dev
)
1515 struct mmc_host
* mmc
= dev_get_drvdata(dev
);
1516 struct wbsd_host
* host
;
1521 host
= mmc_priv(mmc
);
1524 * Unregister host with MMC layer.
1526 mmc_remove_host(mmc
);
1529 * Power down the SD/MMC function.
1531 wbsd_unlock_config(host
);
1532 wbsd_write_config(host
, WBSD_CONF_DEVICE
, DEVICE_SD
);
1533 wbsd_write_config(host
, WBSD_CONF_ENABLE
, 0);
1534 wbsd_lock_config(host
);
1539 if (host
->dma_buffer
)
1540 kfree(host
->dma_buffer
);
1543 free_dma(host
->dma
);
1545 free_irq(host
->irq
, host
);
1547 tasklet_kill(&host
->card_tasklet
);
1548 tasklet_kill(&host
->fifo_tasklet
);
1549 tasklet_kill(&host
->crc_tasklet
);
1550 tasklet_kill(&host
->timeout_tasklet
);
1551 tasklet_kill(&host
->finish_tasklet
);
1552 tasklet_kill(&host
->block_tasklet
);
1554 wbsd_release_regions(host
);
1566 static int wbsd_suspend(struct device
*dev
, pm_message_t state
, u32 level
)
1568 DBGF("Not yet supported\n");
1573 static int wbsd_resume(struct device
*dev
, u32 level
)
1575 DBGF("Not yet supported\n");
1580 #define wbsd_suspend NULL
1581 #define wbsd_resume NULL
1584 static void wbsd_release(struct device
*dev
)
1588 static struct platform_device wbsd_device
= {
1589 .name
= DRIVER_NAME
,
1592 .release
= wbsd_release
,
1596 static struct device_driver wbsd_driver
= {
1597 .name
= DRIVER_NAME
,
1598 .bus
= &platform_bus_type
,
1599 .probe
= wbsd_probe
,
1600 .remove
= wbsd_remove
,
1602 .suspend
= wbsd_suspend
,
1603 .resume
= wbsd_resume
,
1607 * Module loading/unloading
1610 static int __init
wbsd_drv_init(void)
1614 printk(KERN_INFO DRIVER_NAME
1615 ": Winbond W83L51xD SD/MMC card interface driver, "
1616 DRIVER_VERSION
"\n");
1617 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
1619 result
= driver_register(&wbsd_driver
);
1623 result
= platform_device_register(&wbsd_device
);
1630 static void __exit
wbsd_drv_exit(void)
1632 platform_device_unregister(&wbsd_device
);
1634 driver_unregister(&wbsd_driver
);
1639 module_init(wbsd_drv_init
);
1640 module_exit(wbsd_drv_exit
);
1641 module_param(io
, uint
, 0444);
1642 module_param(irq
, uint
, 0444);
1643 module_param(dma
, int, 0444);
1645 MODULE_LICENSE("GPL");
1646 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
1647 MODULE_VERSION(DRIVER_VERSION
);
1649 MODULE_PARM_DESC(io
, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1650 MODULE_PARM_DESC(irq
, "IRQ to allocate. (default 6)");
1651 MODULE_PARM_DESC(dma
, "DMA channel to allocate. -1 for no DMA. (default 2)");