2 * linux/drivers/mmc/host/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2007 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/scatterlist.h>
37 #include <linux/slab.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 param_nopnp
= 0;
77 static const unsigned int param_nopnp
= 1;
79 static unsigned int param_io
= 0x248;
80 static unsigned int param_irq
= 6;
81 static int param_dma
= 2;
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_CRC
;
182 ier
|= WBSD_EINT_TIMEOUT
;
185 outb(ier
, host
->base
+ WBSD_EIR
);
190 inb(host
->base
+ WBSD_ISR
);
193 static void wbsd_reset(struct wbsd_host
*host
)
197 pr_err("%s: Resetting chip\n", mmc_hostname(host
->mmc
));
200 * Soft reset of chip (SD/MMC part).
202 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
203 setup
|= WBSD_SOFT_RESET
;
204 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
207 static void wbsd_request_end(struct wbsd_host
*host
, struct mmc_request
*mrq
)
209 unsigned long dmaflags
;
211 if (host
->dma
>= 0) {
213 * Release ISA DMA controller.
215 dmaflags
= claim_dma_lock();
216 disable_dma(host
->dma
);
217 clear_dma_ff(host
->dma
);
218 release_dma_lock(dmaflags
);
221 * Disable DMA on host.
223 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
229 * MMC layer might call back into the driver so first unlock.
231 spin_unlock(&host
->lock
);
232 mmc_request_done(host
->mmc
, mrq
);
233 spin_lock(&host
->lock
);
237 * Scatter/gather functions
240 static inline void wbsd_init_sg(struct wbsd_host
*host
, struct mmc_data
*data
)
243 * Get info. about SG list from data structure.
245 host
->cur_sg
= data
->sg
;
246 host
->num_sg
= data
->sg_len
;
249 host
->remain
= host
->cur_sg
->length
;
252 static inline int wbsd_next_sg(struct wbsd_host
*host
)
255 * Skip to next SG entry.
263 if (host
->num_sg
> 0) {
265 host
->remain
= host
->cur_sg
->length
;
271 static inline char *wbsd_map_sg(struct wbsd_host
*host
)
273 return kmap_atomic(sg_page(host
->cur_sg
)) + host
->cur_sg
->offset
;
276 static inline void wbsd_sg_to_dma(struct wbsd_host
*host
, struct mmc_data
*data
)
281 for (i
= 0; i
< data
->sg_len
; i
++)
282 len
+= data
->sg
[i
].length
;
283 sg_copy_to_buffer(data
->sg
, data
->sg_len
, host
->dma_buffer
, len
);
286 static inline void wbsd_dma_to_sg(struct wbsd_host
*host
, struct mmc_data
*data
)
291 for (i
= 0; i
< data
->sg_len
; i
++)
292 len
+= data
->sg
[i
].length
;
293 sg_copy_from_buffer(data
->sg
, data
->sg_len
, host
->dma_buffer
, len
);
300 static inline void wbsd_get_short_reply(struct wbsd_host
*host
,
301 struct mmc_command
*cmd
)
304 * Correct response type?
306 if (wbsd_read_index(host
, WBSD_IDX_RSPLEN
) != WBSD_RSP_SHORT
) {
307 cmd
->error
= -EILSEQ
;
311 cmd
->resp
[0] = wbsd_read_index(host
, WBSD_IDX_RESP12
) << 24;
312 cmd
->resp
[0] |= wbsd_read_index(host
, WBSD_IDX_RESP13
) << 16;
313 cmd
->resp
[0] |= wbsd_read_index(host
, WBSD_IDX_RESP14
) << 8;
314 cmd
->resp
[0] |= wbsd_read_index(host
, WBSD_IDX_RESP15
) << 0;
315 cmd
->resp
[1] = wbsd_read_index(host
, WBSD_IDX_RESP16
) << 24;
318 static inline void wbsd_get_long_reply(struct wbsd_host
*host
,
319 struct mmc_command
*cmd
)
324 * Correct response type?
326 if (wbsd_read_index(host
, WBSD_IDX_RSPLEN
) != WBSD_RSP_LONG
) {
327 cmd
->error
= -EILSEQ
;
331 for (i
= 0; i
< 4; i
++) {
333 wbsd_read_index(host
, WBSD_IDX_RESP1
+ i
* 4) << 24;
335 wbsd_read_index(host
, WBSD_IDX_RESP2
+ i
* 4) << 16;
337 wbsd_read_index(host
, WBSD_IDX_RESP3
+ i
* 4) << 8;
339 wbsd_read_index(host
, WBSD_IDX_RESP4
+ i
* 4) << 0;
343 static void wbsd_send_command(struct wbsd_host
*host
, struct mmc_command
*cmd
)
349 * Clear accumulated ISR. The interrupt routine
350 * will fill this one with events that occur during
356 * Send the command (CRC calculated by host).
358 outb(cmd
->opcode
, host
->base
+ WBSD_CMDR
);
359 for (i
= 3; i
>= 0; i
--)
360 outb((cmd
->arg
>> (i
* 8)) & 0xff, host
->base
+ WBSD_CMDR
);
365 * Wait for the request to complete.
368 status
= wbsd_read_index(host
, WBSD_IDX_STATUS
);
369 } while (status
& WBSD_CARDTRAFFIC
);
372 * Do we expect a reply?
374 if (cmd
->flags
& MMC_RSP_PRESENT
) {
381 if (isr
& WBSD_INT_CARD
)
382 cmd
->error
= -ENOMEDIUM
;
384 else if (isr
& WBSD_INT_TIMEOUT
)
385 cmd
->error
= -ETIMEDOUT
;
387 else if ((cmd
->flags
& MMC_RSP_CRC
) && (isr
& WBSD_INT_CRC
))
388 cmd
->error
= -EILSEQ
;
391 if (cmd
->flags
& MMC_RSP_136
)
392 wbsd_get_long_reply(host
, cmd
);
394 wbsd_get_short_reply(host
, cmd
);
403 static void wbsd_empty_fifo(struct wbsd_host
*host
)
405 struct mmc_data
*data
= host
->mrq
->cmd
->data
;
407 int i
, idx
, fsr
, fifo
;
410 * Handle excessive data.
412 if (host
->num_sg
== 0)
415 buffer
= wbsd_map_sg(host
) + host
->offset
;
419 * Drain the fifo. This has a tendency to loop longer
420 * than the FIFO length (usually one block).
422 while (!((fsr
= inb(host
->base
+ WBSD_FSR
)) & WBSD_FIFO_EMPTY
)) {
424 * The size field in the FSR is broken so we have to
427 if (fsr
& WBSD_FIFO_FULL
)
429 else if (fsr
& WBSD_FIFO_FUTHRE
)
434 for (i
= 0; i
< fifo
; i
++) {
435 buffer
[idx
++] = inb(host
->base
+ WBSD_DFR
);
439 data
->bytes_xfered
++;
442 * End of scatter list entry?
444 if (host
->remain
== 0) {
445 kunmap_atomic(buffer
);
447 * Get next entry. Check if last.
449 if (!wbsd_next_sg(host
))
452 buffer
= wbsd_map_sg(host
);
457 kunmap_atomic(buffer
);
460 * This is a very dirty hack to solve a
461 * hardware problem. The chip doesn't trigger
462 * FIFO threshold interrupts properly.
464 if ((data
->blocks
* data
->blksz
- data
->bytes_xfered
) < 16)
465 tasklet_schedule(&host
->fifo_tasklet
);
468 static void wbsd_fill_fifo(struct wbsd_host
*host
)
470 struct mmc_data
*data
= host
->mrq
->cmd
->data
;
472 int i
, idx
, fsr
, fifo
;
475 * Check that we aren't being called after the
476 * entire buffer has been transferred.
478 if (host
->num_sg
== 0)
481 buffer
= wbsd_map_sg(host
) + host
->offset
;
485 * Fill the fifo. This has a tendency to loop longer
486 * than the FIFO length (usually one block).
488 while (!((fsr
= inb(host
->base
+ WBSD_FSR
)) & WBSD_FIFO_FULL
)) {
490 * The size field in the FSR is broken so we have to
493 if (fsr
& WBSD_FIFO_EMPTY
)
495 else if (fsr
& WBSD_FIFO_EMTHRE
)
500 for (i
= 16; i
> fifo
; i
--) {
501 outb(buffer
[idx
], host
->base
+ WBSD_DFR
);
505 data
->bytes_xfered
++;
508 * End of scatter list entry?
510 if (host
->remain
== 0) {
511 kunmap_atomic(buffer
);
513 * Get next entry. Check if last.
515 if (!wbsd_next_sg(host
))
518 buffer
= wbsd_map_sg(host
);
523 kunmap_atomic(buffer
);
526 * The controller stops sending interrupts for
527 * 'FIFO empty' under certain conditions. So we
528 * need to be a bit more pro-active.
530 tasklet_schedule(&host
->fifo_tasklet
);
533 static void wbsd_prepare_data(struct wbsd_host
*host
, struct mmc_data
*data
)
537 unsigned long dmaflags
;
543 size
= data
->blocks
* data
->blksz
;
546 * Check timeout values for overflow.
547 * (Yes, some cards cause this value to overflow).
549 if (data
->timeout_ns
> 127000000)
550 wbsd_write_index(host
, WBSD_IDX_TAAC
, 127);
552 wbsd_write_index(host
, WBSD_IDX_TAAC
,
553 data
->timeout_ns
/ 1000000);
556 if (data
->timeout_clks
> 255)
557 wbsd_write_index(host
, WBSD_IDX_NSAC
, 255);
559 wbsd_write_index(host
, WBSD_IDX_NSAC
, data
->timeout_clks
);
562 * Inform the chip of how large blocks will be
563 * sent. It needs this to determine when to
566 * Space for CRC must be included in the size.
567 * Two bytes are needed for each data line.
569 if (host
->bus_width
== MMC_BUS_WIDTH_1
) {
570 blksize
= data
->blksz
+ 2;
572 wbsd_write_index(host
, WBSD_IDX_PBSMSB
, (blksize
>> 4) & 0xF0);
573 wbsd_write_index(host
, WBSD_IDX_PBSLSB
, blksize
& 0xFF);
574 } else if (host
->bus_width
== MMC_BUS_WIDTH_4
) {
575 blksize
= data
->blksz
+ 2 * 4;
577 wbsd_write_index(host
, WBSD_IDX_PBSMSB
,
578 ((blksize
>> 4) & 0xF0) | WBSD_DATA_WIDTH
);
579 wbsd_write_index(host
, WBSD_IDX_PBSLSB
, blksize
& 0xFF);
581 data
->error
= -EINVAL
;
586 * Clear the FIFO. This is needed even for DMA
587 * transfers since the chip still uses the FIFO
590 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
591 setup
|= WBSD_FIFO_RESET
;
592 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
597 if (host
->dma
>= 0) {
599 * The buffer for DMA is only 64 kB.
601 BUG_ON(size
> 0x10000);
602 if (size
> 0x10000) {
603 data
->error
= -EINVAL
;
608 * Transfer data from the SG list to
611 if (data
->flags
& MMC_DATA_WRITE
)
612 wbsd_sg_to_dma(host
, data
);
615 * Initialise the ISA DMA controller.
617 dmaflags
= claim_dma_lock();
618 disable_dma(host
->dma
);
619 clear_dma_ff(host
->dma
);
620 if (data
->flags
& MMC_DATA_READ
)
621 set_dma_mode(host
->dma
, DMA_MODE_READ
& ~0x40);
623 set_dma_mode(host
->dma
, DMA_MODE_WRITE
& ~0x40);
624 set_dma_addr(host
->dma
, host
->dma_addr
);
625 set_dma_count(host
->dma
, size
);
627 enable_dma(host
->dma
);
628 release_dma_lock(dmaflags
);
631 * Enable DMA on the host.
633 wbsd_write_index(host
, WBSD_IDX_DMA
, WBSD_DMA_ENABLE
);
636 * This flag is used to keep printk
637 * output to a minimum.
642 * Initialise the SG list.
644 wbsd_init_sg(host
, data
);
649 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
652 * Set up FIFO threshold levels (and fill
653 * buffer if doing a write).
655 if (data
->flags
& MMC_DATA_READ
) {
656 wbsd_write_index(host
, WBSD_IDX_FIFOEN
,
657 WBSD_FIFOEN_FULL
| 8);
659 wbsd_write_index(host
, WBSD_IDX_FIFOEN
,
660 WBSD_FIFOEN_EMPTY
| 8);
661 wbsd_fill_fifo(host
);
668 static void wbsd_finish_data(struct wbsd_host
*host
, struct mmc_data
*data
)
670 unsigned long dmaflags
;
674 WARN_ON(host
->mrq
== NULL
);
677 * Send a stop command if needed.
680 wbsd_send_command(host
, data
->stop
);
683 * Wait for the controller to leave data
687 status
= wbsd_read_index(host
, WBSD_IDX_STATUS
);
688 } while (status
& (WBSD_BLOCK_READ
| WBSD_BLOCK_WRITE
));
693 if (host
->dma
>= 0) {
695 * Disable DMA on the host.
697 wbsd_write_index(host
, WBSD_IDX_DMA
, 0);
700 * Turn of ISA DMA controller.
702 dmaflags
= claim_dma_lock();
703 disable_dma(host
->dma
);
704 clear_dma_ff(host
->dma
);
705 count
= get_dma_residue(host
->dma
);
706 release_dma_lock(dmaflags
);
708 data
->bytes_xfered
= host
->mrq
->data
->blocks
*
709 host
->mrq
->data
->blksz
- count
;
710 data
->bytes_xfered
-= data
->bytes_xfered
% data
->blksz
;
716 pr_err("%s: Incomplete DMA transfer. "
718 mmc_hostname(host
->mmc
), count
);
724 * Transfer data from DMA buffer to
727 if (data
->flags
& MMC_DATA_READ
)
728 wbsd_dma_to_sg(host
, data
);
732 if (data
->bytes_xfered
)
733 data
->bytes_xfered
-= data
->blksz
;
737 wbsd_request_end(host
, host
->mrq
);
740 /*****************************************************************************\
742 * MMC layer callbacks *
744 \*****************************************************************************/
746 static void wbsd_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
748 struct wbsd_host
*host
= mmc_priv(mmc
);
749 struct mmc_command
*cmd
;
752 * Disable tasklets to avoid a deadlock.
754 spin_lock_bh(&host
->lock
);
756 BUG_ON(host
->mrq
!= NULL
);
763 * Check that there is actually a card in the slot.
765 if (!(host
->flags
& WBSD_FCARD_PRESENT
)) {
766 cmd
->error
= -ENOMEDIUM
;
772 * The hardware is so delightfully stupid that it has a list
773 * of "data" commands. If a command isn't on this list, it'll
774 * just go back to the idle state and won't send any data
777 switch (cmd
->opcode
) {
791 /* ACMDs. We don't keep track of state, so we just treat them
792 * like any other command. */
797 pr_warn("%s: Data command %d is not supported by this controller\n",
798 mmc_hostname(host
->mmc
), cmd
->opcode
);
799 cmd
->error
= -EINVAL
;
806 * Does the request include data?
809 wbsd_prepare_data(host
, cmd
->data
);
811 if (cmd
->data
->error
)
815 wbsd_send_command(host
, cmd
);
818 * If this is a data transfer the request
819 * will be finished after the data has
822 if (cmd
->data
&& !cmd
->error
) {
824 * Dirty fix for hardware bug.
827 tasklet_schedule(&host
->fifo_tasklet
);
829 spin_unlock_bh(&host
->lock
);
835 wbsd_request_end(host
, mrq
);
837 spin_unlock_bh(&host
->lock
);
840 static void wbsd_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
842 struct wbsd_host
*host
= mmc_priv(mmc
);
845 spin_lock_bh(&host
->lock
);
848 * Reset the chip on each power off.
849 * Should clear out any weird states.
851 if (ios
->power_mode
== MMC_POWER_OFF
)
852 wbsd_init_device(host
);
854 if (ios
->clock
>= 24000000)
856 else if (ios
->clock
>= 16000000)
858 else if (ios
->clock
>= 12000000)
864 * Only write to the clock register when
865 * there is an actual change.
867 if (clk
!= host
->clk
) {
868 wbsd_write_index(host
, WBSD_IDX_CLK
, clk
);
875 if (ios
->power_mode
!= MMC_POWER_OFF
) {
876 pwr
= inb(host
->base
+ WBSD_CSR
);
877 pwr
&= ~WBSD_POWER_N
;
878 outb(pwr
, host
->base
+ WBSD_CSR
);
882 * MMC cards need to have pin 1 high during init.
883 * It wreaks havoc with the card detection though so
884 * that needs to be disabled.
886 setup
= wbsd_read_index(host
, WBSD_IDX_SETUP
);
887 if (ios
->chip_select
== MMC_CS_HIGH
) {
888 BUG_ON(ios
->bus_width
!= MMC_BUS_WIDTH_1
);
889 setup
|= WBSD_DAT3_H
;
890 host
->flags
|= WBSD_FIGNORE_DETECT
;
892 if (setup
& WBSD_DAT3_H
) {
893 setup
&= ~WBSD_DAT3_H
;
896 * We cannot resume card detection immediately
897 * because of capacitance and delays in the chip.
899 mod_timer(&host
->ignore_timer
, jiffies
+ HZ
/ 100);
902 wbsd_write_index(host
, WBSD_IDX_SETUP
, setup
);
905 * Store bus width for later. Will be used when
906 * setting up the data transfer.
908 host
->bus_width
= ios
->bus_width
;
910 spin_unlock_bh(&host
->lock
);
913 static int wbsd_get_ro(struct mmc_host
*mmc
)
915 struct wbsd_host
*host
= mmc_priv(mmc
);
918 spin_lock_bh(&host
->lock
);
920 csr
= inb(host
->base
+ WBSD_CSR
);
922 outb(csr
, host
->base
+ WBSD_CSR
);
926 csr
= inb(host
->base
+ WBSD_CSR
);
928 outb(csr
, host
->base
+ WBSD_CSR
);
930 spin_unlock_bh(&host
->lock
);
932 return !!(csr
& WBSD_WRPT
);
935 static const struct mmc_host_ops wbsd_ops
= {
936 .request
= wbsd_request
,
937 .set_ios
= wbsd_set_ios
,
938 .get_ro
= wbsd_get_ro
,
941 /*****************************************************************************\
943 * Interrupt handling *
945 \*****************************************************************************/
948 * Helper function to reset detection ignore
951 static void wbsd_reset_ignore(struct timer_list
*t
)
953 struct wbsd_host
*host
= from_timer(host
, t
, ignore_timer
);
955 BUG_ON(host
== NULL
);
957 DBG("Resetting card detection ignore\n");
959 spin_lock_bh(&host
->lock
);
961 host
->flags
&= ~WBSD_FIGNORE_DETECT
;
964 * Card status might have changed during the
967 tasklet_schedule(&host
->card_tasklet
);
969 spin_unlock_bh(&host
->lock
);
976 static inline struct mmc_data
*wbsd_get_data(struct wbsd_host
*host
)
982 WARN_ON(!host
->mrq
->cmd
);
986 WARN_ON(!host
->mrq
->cmd
->data
);
987 if (!host
->mrq
->cmd
->data
)
990 return host
->mrq
->cmd
->data
;
993 static void wbsd_tasklet_card(unsigned long param
)
995 struct wbsd_host
*host
= (struct wbsd_host
*)param
;
999 spin_lock(&host
->lock
);
1001 if (host
->flags
& WBSD_FIGNORE_DETECT
) {
1002 spin_unlock(&host
->lock
);
1006 csr
= inb(host
->base
+ WBSD_CSR
);
1007 WARN_ON(csr
== 0xff);
1009 if (csr
& WBSD_CARDPRESENT
) {
1010 if (!(host
->flags
& WBSD_FCARD_PRESENT
)) {
1011 DBG("Card inserted\n");
1012 host
->flags
|= WBSD_FCARD_PRESENT
;
1016 } else if (host
->flags
& WBSD_FCARD_PRESENT
) {
1017 DBG("Card removed\n");
1018 host
->flags
&= ~WBSD_FCARD_PRESENT
;
1021 pr_err("%s: Card removed during transfer!\n",
1022 mmc_hostname(host
->mmc
));
1025 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1026 tasklet_schedule(&host
->finish_tasklet
);
1033 * Unlock first since we might get a call back.
1036 spin_unlock(&host
->lock
);
1039 mmc_detect_change(host
->mmc
, msecs_to_jiffies(delay
));
1042 static void wbsd_tasklet_fifo(unsigned long param
)
1044 struct wbsd_host
*host
= (struct wbsd_host
*)param
;
1045 struct mmc_data
*data
;
1047 spin_lock(&host
->lock
);
1052 data
= wbsd_get_data(host
);
1056 if (data
->flags
& MMC_DATA_WRITE
)
1057 wbsd_fill_fifo(host
);
1059 wbsd_empty_fifo(host
);
1064 if (host
->num_sg
== 0) {
1065 wbsd_write_index(host
, WBSD_IDX_FIFOEN
, 0);
1066 tasklet_schedule(&host
->finish_tasklet
);
1070 spin_unlock(&host
->lock
);
1073 static void wbsd_tasklet_crc(unsigned long param
)
1075 struct wbsd_host
*host
= (struct wbsd_host
*)param
;
1076 struct mmc_data
*data
;
1078 spin_lock(&host
->lock
);
1083 data
= wbsd_get_data(host
);
1087 DBGF("CRC error\n");
1089 data
->error
= -EILSEQ
;
1091 tasklet_schedule(&host
->finish_tasklet
);
1094 spin_unlock(&host
->lock
);
1097 static void wbsd_tasklet_timeout(unsigned long param
)
1099 struct wbsd_host
*host
= (struct wbsd_host
*)param
;
1100 struct mmc_data
*data
;
1102 spin_lock(&host
->lock
);
1107 data
= wbsd_get_data(host
);
1113 data
->error
= -ETIMEDOUT
;
1115 tasklet_schedule(&host
->finish_tasklet
);
1118 spin_unlock(&host
->lock
);
1121 static void wbsd_tasklet_finish(unsigned long param
)
1123 struct wbsd_host
*host
= (struct wbsd_host
*)param
;
1124 struct mmc_data
*data
;
1126 spin_lock(&host
->lock
);
1128 WARN_ON(!host
->mrq
);
1132 data
= wbsd_get_data(host
);
1136 wbsd_finish_data(host
, data
);
1139 spin_unlock(&host
->lock
);
1143 * Interrupt handling
1146 static irqreturn_t
wbsd_irq(int irq
, void *dev_id
)
1148 struct wbsd_host
*host
= dev_id
;
1151 isr
= inb(host
->base
+ WBSD_ISR
);
1154 * Was it actually our hardware that caused the interrupt?
1156 if (isr
== 0xff || isr
== 0x00)
1162 * Schedule tasklets as needed.
1164 if (isr
& WBSD_INT_CARD
)
1165 tasklet_schedule(&host
->card_tasklet
);
1166 if (isr
& WBSD_INT_FIFO_THRE
)
1167 tasklet_schedule(&host
->fifo_tasklet
);
1168 if (isr
& WBSD_INT_CRC
)
1169 tasklet_hi_schedule(&host
->crc_tasklet
);
1170 if (isr
& WBSD_INT_TIMEOUT
)
1171 tasklet_hi_schedule(&host
->timeout_tasklet
);
1172 if (isr
& WBSD_INT_TC
)
1173 tasklet_schedule(&host
->finish_tasklet
);
1178 /*****************************************************************************\
1180 * Device initialisation and shutdown *
1182 \*****************************************************************************/
1185 * Allocate/free MMC structure.
1188 static int wbsd_alloc_mmc(struct device
*dev
)
1190 struct mmc_host
*mmc
;
1191 struct wbsd_host
*host
;
1194 * Allocate MMC structure.
1196 mmc
= mmc_alloc_host(sizeof(struct wbsd_host
), dev
);
1200 host
= mmc_priv(mmc
);
1206 * Set host parameters.
1208 mmc
->ops
= &wbsd_ops
;
1209 mmc
->f_min
= 375000;
1210 mmc
->f_max
= 24000000;
1211 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1212 mmc
->caps
= MMC_CAP_4_BIT_DATA
;
1214 spin_lock_init(&host
->lock
);
1219 timer_setup(&host
->ignore_timer
, wbsd_reset_ignore
, 0);
1222 * Maximum number of segments. Worst case is one sector per segment
1223 * so this will be 64kB/512.
1225 mmc
->max_segs
= 128;
1228 * Maximum request size. Also limited by 64KiB buffer.
1230 mmc
->max_req_size
= 65536;
1233 * Maximum segment size. Could be one segment with the maximum number
1236 mmc
->max_seg_size
= mmc
->max_req_size
;
1239 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1240 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1242 mmc
->max_blk_size
= 4087;
1245 * Maximum block count. There is no real limit so the maximum
1246 * request size will be the only restriction.
1248 mmc
->max_blk_count
= mmc
->max_req_size
;
1250 dev_set_drvdata(dev
, mmc
);
1255 static void wbsd_free_mmc(struct device
*dev
)
1257 struct mmc_host
*mmc
;
1258 struct wbsd_host
*host
;
1260 mmc
= dev_get_drvdata(dev
);
1264 host
= mmc_priv(mmc
);
1265 BUG_ON(host
== NULL
);
1267 del_timer_sync(&host
->ignore_timer
);
1271 dev_set_drvdata(dev
, NULL
);
1275 * Scan for known chip id:s
1278 static int wbsd_scan(struct wbsd_host
*host
)
1284 * Iterate through all ports, all codes to
1285 * find hardware that is in our known list.
1287 for (i
= 0; i
< ARRAY_SIZE(config_ports
); i
++) {
1288 if (!request_region(config_ports
[i
], 2, DRIVER_NAME
))
1291 for (j
= 0; j
< ARRAY_SIZE(unlock_codes
); j
++) {
1294 host
->config
= config_ports
[i
];
1295 host
->unlock_code
= unlock_codes
[j
];
1297 wbsd_unlock_config(host
);
1299 outb(WBSD_CONF_ID_HI
, config_ports
[i
]);
1300 id
= inb(config_ports
[i
] + 1) << 8;
1302 outb(WBSD_CONF_ID_LO
, config_ports
[i
]);
1303 id
|= inb(config_ports
[i
] + 1);
1305 wbsd_lock_config(host
);
1307 for (k
= 0; k
< ARRAY_SIZE(valid_ids
); k
++) {
1308 if (id
== valid_ids
[k
]) {
1316 DBG("Unknown hardware (id %x) found at %x\n",
1317 id
, config_ports
[i
]);
1321 release_region(config_ports
[i
], 2);
1325 host
->unlock_code
= 0;
1331 * Allocate/free io port ranges
1334 static int wbsd_request_region(struct wbsd_host
*host
, int base
)
1339 if (!request_region(base
, 8, DRIVER_NAME
))
1347 static void wbsd_release_regions(struct wbsd_host
*host
)
1350 release_region(host
->base
, 8);
1355 release_region(host
->config
, 2);
1361 * Allocate/free DMA port and buffer
1364 static void wbsd_request_dma(struct wbsd_host
*host
, int dma
)
1369 if (request_dma(dma
, DRIVER_NAME
))
1373 * We need to allocate a special buffer in
1374 * order for ISA to be able to DMA to it.
1376 host
->dma_buffer
= kmalloc(WBSD_DMA_SIZE
,
1377 GFP_NOIO
| GFP_DMA
| __GFP_RETRY_MAYFAIL
| __GFP_NOWARN
);
1378 if (!host
->dma_buffer
)
1382 * Translate the address to a physical address.
1384 host
->dma_addr
= dma_map_single(mmc_dev(host
->mmc
), host
->dma_buffer
,
1385 WBSD_DMA_SIZE
, DMA_BIDIRECTIONAL
);
1386 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->dma_addr
))
1390 * ISA DMA must be aligned on a 64k basis.
1392 if ((host
->dma_addr
& 0xffff) != 0)
1395 * ISA cannot access memory above 16 MB.
1397 else if (host
->dma_addr
>= 0x1000000)
1406 * If we've gotten here then there is some kind of alignment bug
1410 dma_unmap_single(mmc_dev(host
->mmc
), host
->dma_addr
,
1411 WBSD_DMA_SIZE
, DMA_BIDIRECTIONAL
);
1415 kfree(host
->dma_buffer
);
1416 host
->dma_buffer
= NULL
;
1422 pr_warn(DRIVER_NAME
": Unable to allocate DMA %d - falling back on FIFO\n",
1426 static void wbsd_release_dma(struct wbsd_host
*host
)
1429 * host->dma_addr is valid here iff host->dma_buffer is not NULL.
1431 if (host
->dma_buffer
) {
1432 dma_unmap_single(mmc_dev(host
->mmc
), host
->dma_addr
,
1433 WBSD_DMA_SIZE
, DMA_BIDIRECTIONAL
);
1434 kfree(host
->dma_buffer
);
1437 free_dma(host
->dma
);
1440 host
->dma_buffer
= NULL
;
1445 * Allocate/free IRQ.
1448 static int wbsd_request_irq(struct wbsd_host
*host
, int irq
)
1453 * Set up tasklets. Must be done before requesting interrupt.
1455 tasklet_init(&host
->card_tasklet
, wbsd_tasklet_card
,
1456 (unsigned long)host
);
1457 tasklet_init(&host
->fifo_tasklet
, wbsd_tasklet_fifo
,
1458 (unsigned long)host
);
1459 tasklet_init(&host
->crc_tasklet
, wbsd_tasklet_crc
,
1460 (unsigned long)host
);
1461 tasklet_init(&host
->timeout_tasklet
, wbsd_tasklet_timeout
,
1462 (unsigned long)host
);
1463 tasklet_init(&host
->finish_tasklet
, wbsd_tasklet_finish
,
1464 (unsigned long)host
);
1467 * Allocate interrupt.
1469 ret
= request_irq(irq
, wbsd_irq
, IRQF_SHARED
, DRIVER_NAME
, host
);
1478 static void wbsd_release_irq(struct wbsd_host
*host
)
1483 free_irq(host
->irq
, host
);
1487 tasklet_kill(&host
->card_tasklet
);
1488 tasklet_kill(&host
->fifo_tasklet
);
1489 tasklet_kill(&host
->crc_tasklet
);
1490 tasklet_kill(&host
->timeout_tasklet
);
1491 tasklet_kill(&host
->finish_tasklet
);
1495 * Allocate all resources for the host.
1498 static int wbsd_request_resources(struct wbsd_host
*host
,
1499 int base
, int irq
, int dma
)
1504 * Allocate I/O ports.
1506 ret
= wbsd_request_region(host
, base
);
1511 * Allocate interrupt.
1513 ret
= wbsd_request_irq(host
, irq
);
1520 wbsd_request_dma(host
, dma
);
1526 * Release all resources for the host.
1529 static void wbsd_release_resources(struct wbsd_host
*host
)
1531 wbsd_release_dma(host
);
1532 wbsd_release_irq(host
);
1533 wbsd_release_regions(host
);
1537 * Configure the resources the chip should use.
1540 static void wbsd_chip_config(struct wbsd_host
*host
)
1542 wbsd_unlock_config(host
);
1547 wbsd_write_config(host
, WBSD_CONF_SWRST
, 1);
1548 wbsd_write_config(host
, WBSD_CONF_SWRST
, 0);
1551 * Select SD/MMC function.
1553 wbsd_write_config(host
, WBSD_CONF_DEVICE
, DEVICE_SD
);
1556 * Set up card detection.
1558 wbsd_write_config(host
, WBSD_CONF_PINS
, WBSD_PINS_DETECT_GP11
);
1563 wbsd_write_config(host
, WBSD_CONF_PORT_HI
, host
->base
>> 8);
1564 wbsd_write_config(host
, WBSD_CONF_PORT_LO
, host
->base
& 0xff);
1566 wbsd_write_config(host
, WBSD_CONF_IRQ
, host
->irq
);
1569 wbsd_write_config(host
, WBSD_CONF_DRQ
, host
->dma
);
1572 * Enable and power up chip.
1574 wbsd_write_config(host
, WBSD_CONF_ENABLE
, 1);
1575 wbsd_write_config(host
, WBSD_CONF_POWER
, 0x20);
1577 wbsd_lock_config(host
);
1581 * Check that configured resources are correct.
1584 static int wbsd_chip_validate(struct wbsd_host
*host
)
1588 wbsd_unlock_config(host
);
1591 * Select SD/MMC function.
1593 wbsd_write_config(host
, WBSD_CONF_DEVICE
, DEVICE_SD
);
1596 * Read configuration.
1598 base
= wbsd_read_config(host
, WBSD_CONF_PORT_HI
) << 8;
1599 base
|= wbsd_read_config(host
, WBSD_CONF_PORT_LO
);
1601 irq
= wbsd_read_config(host
, WBSD_CONF_IRQ
);
1603 dma
= wbsd_read_config(host
, WBSD_CONF_DRQ
);
1605 wbsd_lock_config(host
);
1608 * Validate against given configuration.
1610 if (base
!= host
->base
)
1612 if (irq
!= host
->irq
)
1614 if ((dma
!= host
->dma
) && (host
->dma
!= -1))
1621 * Powers down the SD function
1624 static void wbsd_chip_poweroff(struct wbsd_host
*host
)
1626 wbsd_unlock_config(host
);
1628 wbsd_write_config(host
, WBSD_CONF_DEVICE
, DEVICE_SD
);
1629 wbsd_write_config(host
, WBSD_CONF_ENABLE
, 0);
1631 wbsd_lock_config(host
);
1634 /*****************************************************************************\
1636 * Devices setup and shutdown *
1638 \*****************************************************************************/
1640 static int wbsd_init(struct device
*dev
, int base
, int irq
, int dma
,
1643 struct wbsd_host
*host
= NULL
;
1644 struct mmc_host
*mmc
= NULL
;
1647 ret
= wbsd_alloc_mmc(dev
);
1651 mmc
= dev_get_drvdata(dev
);
1652 host
= mmc_priv(mmc
);
1655 * Scan for hardware.
1657 ret
= wbsd_scan(host
);
1659 if (pnp
&& (ret
== -ENODEV
)) {
1660 pr_warn(DRIVER_NAME
": Unable to confirm device presence - you may experience lock-ups\n");
1668 * Request resources.
1670 ret
= wbsd_request_resources(host
, base
, irq
, dma
);
1672 wbsd_release_resources(host
);
1678 * See if chip needs to be configured.
1681 if ((host
->config
!= 0) && !wbsd_chip_validate(host
)) {
1682 pr_warn(DRIVER_NAME
": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
1683 wbsd_chip_config(host
);
1686 wbsd_chip_config(host
);
1689 * Power Management stuff. No idea how this works.
1694 wbsd_unlock_config(host
);
1695 wbsd_write_config(host
, WBSD_CONF_PME
, 0xA0);
1696 wbsd_lock_config(host
);
1700 * Allow device to initialise itself properly.
1705 * Reset the chip into a known state.
1707 wbsd_init_device(host
);
1711 pr_info("%s: W83L51xD", mmc_hostname(mmc
));
1712 if (host
->chip_id
!= 0)
1713 printk(" id %x", (int)host
->chip_id
);
1714 printk(" at 0x%x irq %d", (int)host
->base
, (int)host
->irq
);
1716 printk(" dma %d", (int)host
->dma
);
1726 static void wbsd_shutdown(struct device
*dev
, int pnp
)
1728 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
1729 struct wbsd_host
*host
;
1734 host
= mmc_priv(mmc
);
1736 mmc_remove_host(mmc
);
1739 * Power down the SD/MMC function.
1742 wbsd_chip_poweroff(host
);
1744 wbsd_release_resources(host
);
1753 static int wbsd_probe(struct platform_device
*dev
)
1755 /* Use the module parameters for resources */
1756 return wbsd_init(&dev
->dev
, param_io
, param_irq
, param_dma
, 0);
1759 static int wbsd_remove(struct platform_device
*dev
)
1761 wbsd_shutdown(&dev
->dev
, 0);
1773 wbsd_pnp_probe(struct pnp_dev
*pnpdev
, const struct pnp_device_id
*dev_id
)
1778 * Get resources from PnP layer.
1780 io
= pnp_port_start(pnpdev
, 0);
1781 irq
= pnp_irq(pnpdev
, 0);
1782 if (pnp_dma_valid(pnpdev
, 0))
1783 dma
= pnp_dma(pnpdev
, 0);
1787 DBGF("PnP resources: port %3x irq %d dma %d\n", io
, irq
, dma
);
1789 return wbsd_init(&pnpdev
->dev
, io
, irq
, dma
, 1);
1792 static void wbsd_pnp_remove(struct pnp_dev
*dev
)
1794 wbsd_shutdown(&dev
->dev
, 1);
1797 #endif /* CONFIG_PNP */
1805 static int wbsd_platform_suspend(struct platform_device
*dev
,
1808 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
1809 struct wbsd_host
*host
;
1814 DBGF("Suspending...\n");
1816 host
= mmc_priv(mmc
);
1818 wbsd_chip_poweroff(host
);
1822 static int wbsd_platform_resume(struct platform_device
*dev
)
1824 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
1825 struct wbsd_host
*host
;
1830 DBGF("Resuming...\n");
1832 host
= mmc_priv(mmc
);
1834 wbsd_chip_config(host
);
1837 * Allow device to initialise itself properly.
1841 wbsd_init_device(host
);
1847 static int wbsd_pnp_suspend(struct pnp_dev
*pnp_dev
, pm_message_t state
)
1849 struct mmc_host
*mmc
= dev_get_drvdata(&pnp_dev
->dev
);
1854 DBGF("Suspending...\n");
1858 static int wbsd_pnp_resume(struct pnp_dev
*pnp_dev
)
1860 struct mmc_host
*mmc
= dev_get_drvdata(&pnp_dev
->dev
);
1861 struct wbsd_host
*host
;
1866 DBGF("Resuming...\n");
1868 host
= mmc_priv(mmc
);
1871 * See if chip needs to be configured.
1873 if (host
->config
!= 0) {
1874 if (!wbsd_chip_validate(host
)) {
1875 pr_warn(DRIVER_NAME
": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
1876 wbsd_chip_config(host
);
1881 * Allow device to initialise itself properly.
1885 wbsd_init_device(host
);
1889 #endif /* CONFIG_PNP */
1891 #else /* CONFIG_PM */
1893 #define wbsd_platform_suspend NULL
1894 #define wbsd_platform_resume NULL
1896 #define wbsd_pnp_suspend NULL
1897 #define wbsd_pnp_resume NULL
1899 #endif /* CONFIG_PM */
1901 static struct platform_device
*wbsd_device
;
1903 static struct platform_driver wbsd_driver
= {
1904 .probe
= wbsd_probe
,
1905 .remove
= wbsd_remove
,
1907 .suspend
= wbsd_platform_suspend
,
1908 .resume
= wbsd_platform_resume
,
1910 .name
= DRIVER_NAME
,
1916 static struct pnp_driver wbsd_pnp_driver
= {
1917 .name
= DRIVER_NAME
,
1918 .id_table
= pnp_dev_table
,
1919 .probe
= wbsd_pnp_probe
,
1920 .remove
= wbsd_pnp_remove
,
1922 .suspend
= wbsd_pnp_suspend
,
1923 .resume
= wbsd_pnp_resume
,
1926 #endif /* CONFIG_PNP */
1929 * Module loading/unloading
1932 static int __init
wbsd_drv_init(void)
1937 ": Winbond W83L51xD SD/MMC card interface driver\n");
1938 pr_info(DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
1943 result
= pnp_register_driver(&wbsd_pnp_driver
);
1947 #endif /* CONFIG_PNP */
1950 result
= platform_driver_register(&wbsd_driver
);
1954 wbsd_device
= platform_device_alloc(DRIVER_NAME
, -1);
1956 platform_driver_unregister(&wbsd_driver
);
1960 result
= platform_device_add(wbsd_device
);
1962 platform_device_put(wbsd_device
);
1963 platform_driver_unregister(&wbsd_driver
);
1971 static void __exit
wbsd_drv_exit(void)
1976 pnp_unregister_driver(&wbsd_pnp_driver
);
1978 #endif /* CONFIG_PNP */
1981 platform_device_unregister(wbsd_device
);
1983 platform_driver_unregister(&wbsd_driver
);
1989 module_init(wbsd_drv_init
);
1990 module_exit(wbsd_drv_exit
);
1992 module_param_hw_named(nopnp
, param_nopnp
, uint
, other
, 0444);
1994 module_param_hw_named(io
, param_io
, uint
, ioport
, 0444);
1995 module_param_hw_named(irq
, param_irq
, uint
, irq
, 0444);
1996 module_param_hw_named(dma
, param_dma
, int, dma
, 0444);
1998 MODULE_LICENSE("GPL");
1999 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2000 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2003 MODULE_PARM_DESC(nopnp
, "Scan for device instead of relying on PNP. (default 0)");
2005 MODULE_PARM_DESC(io
, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2006 MODULE_PARM_DESC(irq
, "IRQ to allocate. (default 6)");
2007 MODULE_PARM_DESC(dma
, "DMA channel to allocate. -1 for no DMA. (default 2)");