mmc: wbsd: Remove driver version
[linux-2.6/mini2440.git] / drivers / mmc / wbsd.c
blob2d323af2002563492b0b31140a79584b48d0d30c
1 /*
2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
4 * Copyright (C) 2004-2006 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 * Warning!
14 * Changes to the FIFO system should be done with extreme care since
15 * the hardware is full of bugs related to the FIFO. Known issues are:
17 * - FIFO size field in FSR is always zero.
19 * - FIFO interrupts tend not to work as they should. Interrupts are
20 * triggered only for full/empty events, not for threshold values.
22 * - On APIC systems the FIFO empty interrupt is sometimes lost.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/delay.h>
33 #include <linux/pnp.h>
34 #include <linux/highmem.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
38 #include <asm/io.h>
39 #include <asm/dma.h>
40 #include <asm/scatterlist.h>
42 #include "wbsd.h"
44 #define DRIVER_NAME "wbsd"
46 #define DBG(x...) \
47 pr_debug(DRIVER_NAME ": " x)
48 #define DBGF(f, x...) \
49 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
52 * Device resources
55 #ifdef CONFIG_PNP
57 static const struct pnp_device_id pnp_dev_table[] = {
58 { "WEC0517", 0 },
59 { "WEC0518", 0 },
60 { "", 0 },
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[] = {
71 0x7112,
74 #ifdef CONFIG_PNP
75 static unsigned int nopnp = 0;
76 #else
77 static const unsigned int nopnp = 1;
78 #endif
79 static unsigned int io = 0x248;
80 static unsigned int irq = 6;
81 static int dma = 2;
84 * Basic functions
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);
131 * Common routines
134 static void wbsd_init_device(struct wbsd_host *host)
136 u8 setup, ier;
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);
146 * Set DAT3 to input
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);
158 * Power down port.
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;
172 else
173 host->flags &= ~WBSD_FCARD_PRESENT;
176 * Enable interesting interrupts.
178 ier = 0;
179 ier |= WBSD_EINT_CARD;
180 ier |= WBSD_EINT_FIFO_THRE;
181 ier |= WBSD_EINT_CCRC;
182 ier |= WBSD_EINT_TIMEOUT;
183 ier |= WBSD_EINT_CRC;
184 ier |= WBSD_EINT_TC;
186 outb(ier, host->base + WBSD_EIR);
189 * Clear interrupts.
191 inb(host->base + WBSD_ISR);
194 static void wbsd_reset(struct wbsd_host *host)
196 u8 setup;
198 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
201 * Soft reset of chip (SD/MMC part).
203 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
204 setup |= WBSD_SOFT_RESET;
205 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
208 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
210 unsigned long dmaflags;
212 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
214 if (host->dma >= 0) {
216 * Release ISA DMA controller.
218 dmaflags = claim_dma_lock();
219 disable_dma(host->dma);
220 clear_dma_ff(host->dma);
221 release_dma_lock(dmaflags);
224 * Disable DMA on host.
226 wbsd_write_index(host, WBSD_IDX_DMA, 0);
229 host->mrq = NULL;
232 * MMC layer might call back into the driver so first unlock.
234 spin_unlock(&host->lock);
235 mmc_request_done(host->mmc, mrq);
236 spin_lock(&host->lock);
240 * Scatter/gather functions
243 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
246 * Get info. about SG list from data structure.
248 host->cur_sg = data->sg;
249 host->num_sg = data->sg_len;
251 host->offset = 0;
252 host->remain = host->cur_sg->length;
255 static inline int wbsd_next_sg(struct wbsd_host *host)
258 * Skip to next SG entry.
260 host->cur_sg++;
261 host->num_sg--;
264 * Any entries left?
266 if (host->num_sg > 0) {
267 host->offset = 0;
268 host->remain = host->cur_sg->length;
271 return host->num_sg;
274 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
276 return page_address(host->cur_sg->page) + host->cur_sg->offset;
279 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
281 unsigned int len, i, size;
282 struct scatterlist *sg;
283 char *dmabuf = host->dma_buffer;
284 char *sgbuf;
286 size = host->size;
288 sg = data->sg;
289 len = data->sg_len;
292 * Just loop through all entries. Size might not
293 * be the entire list though so make sure that
294 * we do not transfer too much.
296 for (i = 0; i < len; i++) {
297 sgbuf = page_address(sg[i].page) + sg[i].offset;
298 if (size < sg[i].length)
299 memcpy(dmabuf, sgbuf, size);
300 else
301 memcpy(dmabuf, sgbuf, sg[i].length);
302 dmabuf += sg[i].length;
304 if (size < sg[i].length)
305 size = 0;
306 else
307 size -= sg[i].length;
309 if (size == 0)
310 break;
314 * Check that we didn't get a request to transfer
315 * more data than can fit into the SG list.
318 BUG_ON(size != 0);
320 host->size -= size;
323 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
325 unsigned int len, i, size;
326 struct scatterlist *sg;
327 char *dmabuf = host->dma_buffer;
328 char *sgbuf;
330 size = host->size;
332 sg = data->sg;
333 len = data->sg_len;
336 * Just loop through all entries. Size might not
337 * be the entire list though so make sure that
338 * we do not transfer too much.
340 for (i = 0; i < len; i++) {
341 sgbuf = page_address(sg[i].page) + sg[i].offset;
342 if (size < sg[i].length)
343 memcpy(sgbuf, dmabuf, size);
344 else
345 memcpy(sgbuf, dmabuf, sg[i].length);
346 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
347 dmabuf += sg[i].length;
349 if (size < sg[i].length)
350 size = 0;
351 else
352 size -= sg[i].length;
354 if (size == 0)
355 break;
359 * Check that we didn't get a request to transfer
360 * more data than can fit into the SG list.
363 BUG_ON(size != 0);
365 host->size -= size;
369 * Command handling
372 static inline void wbsd_get_short_reply(struct wbsd_host *host,
373 struct mmc_command *cmd)
376 * Correct response type?
378 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
379 cmd->error = MMC_ERR_INVALID;
380 return;
383 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
386 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
387 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
390 static inline void wbsd_get_long_reply(struct wbsd_host *host,
391 struct mmc_command *cmd)
393 int i;
396 * Correct response type?
398 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
399 cmd->error = MMC_ERR_INVALID;
400 return;
403 for (i = 0; i < 4; i++) {
404 cmd->resp[i] =
405 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
406 cmd->resp[i] |=
407 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
408 cmd->resp[i] |=
409 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
410 cmd->resp[i] |=
411 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
415 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
417 int i;
418 u8 status, isr;
420 DBGF("Sending cmd (%x)\n", cmd->opcode);
423 * Clear accumulated ISR. The interrupt routine
424 * will fill this one with events that occur during
425 * transfer.
427 host->isr = 0;
430 * Send the command (CRC calculated by host).
432 outb(cmd->opcode, host->base + WBSD_CMDR);
433 for (i = 3; i >= 0; i--)
434 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
436 cmd->error = MMC_ERR_NONE;
439 * Wait for the request to complete.
441 do {
442 status = wbsd_read_index(host, WBSD_IDX_STATUS);
443 } while (status & WBSD_CARDTRAFFIC);
446 * Do we expect a reply?
448 if (cmd->flags & MMC_RSP_PRESENT) {
450 * Read back status.
452 isr = host->isr;
454 /* Card removed? */
455 if (isr & WBSD_INT_CARD)
456 cmd->error = MMC_ERR_TIMEOUT;
457 /* Timeout? */
458 else if (isr & WBSD_INT_TIMEOUT)
459 cmd->error = MMC_ERR_TIMEOUT;
460 /* CRC? */
461 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
462 cmd->error = MMC_ERR_BADCRC;
463 /* All ok */
464 else {
465 if (cmd->flags & MMC_RSP_136)
466 wbsd_get_long_reply(host, cmd);
467 else
468 wbsd_get_short_reply(host, cmd);
472 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
476 * Data functions
479 static void wbsd_empty_fifo(struct wbsd_host *host)
481 struct mmc_data *data = host->mrq->cmd->data;
482 char *buffer;
483 int i, fsr, fifo;
486 * Handle excessive data.
488 if (data->bytes_xfered == host->size)
489 return;
491 buffer = wbsd_sg_to_buffer(host) + host->offset;
494 * Drain the fifo. This has a tendency to loop longer
495 * than the FIFO length (usually one block).
497 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
499 * The size field in the FSR is broken so we have to
500 * do some guessing.
502 if (fsr & WBSD_FIFO_FULL)
503 fifo = 16;
504 else if (fsr & WBSD_FIFO_FUTHRE)
505 fifo = 8;
506 else
507 fifo = 1;
509 for (i = 0; i < fifo; i++) {
510 *buffer = inb(host->base + WBSD_DFR);
511 buffer++;
512 host->offset++;
513 host->remain--;
515 data->bytes_xfered++;
518 * Transfer done?
520 if (data->bytes_xfered == host->size)
521 return;
524 * End of scatter list entry?
526 if (host->remain == 0) {
528 * Get next entry. Check if last.
530 if (!wbsd_next_sg(host)) {
532 * We should never reach this point.
533 * It means that we're trying to
534 * transfer more blocks than can fit
535 * into the scatter list.
537 BUG_ON(1);
539 host->size = data->bytes_xfered;
541 return;
544 buffer = wbsd_sg_to_buffer(host);
550 * This is a very dirty hack to solve a
551 * hardware problem. The chip doesn't trigger
552 * FIFO threshold interrupts properly.
554 if ((host->size - data->bytes_xfered) < 16)
555 tasklet_schedule(&host->fifo_tasklet);
558 static void wbsd_fill_fifo(struct wbsd_host *host)
560 struct mmc_data *data = host->mrq->cmd->data;
561 char *buffer;
562 int i, fsr, fifo;
565 * Check that we aren't being called after the
566 * entire buffer has been transfered.
568 if (data->bytes_xfered == host->size)
569 return;
571 buffer = wbsd_sg_to_buffer(host) + host->offset;
574 * Fill the fifo. This has a tendency to loop longer
575 * than the FIFO length (usually one block).
577 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
579 * The size field in the FSR is broken so we have to
580 * do some guessing.
582 if (fsr & WBSD_FIFO_EMPTY)
583 fifo = 0;
584 else if (fsr & WBSD_FIFO_EMTHRE)
585 fifo = 8;
586 else
587 fifo = 15;
589 for (i = 16; i > fifo; i--) {
590 outb(*buffer, host->base + WBSD_DFR);
591 buffer++;
592 host->offset++;
593 host->remain--;
595 data->bytes_xfered++;
598 * Transfer done?
600 if (data->bytes_xfered == host->size)
601 return;
604 * End of scatter list entry?
606 if (host->remain == 0) {
608 * Get next entry. Check if last.
610 if (!wbsd_next_sg(host)) {
612 * We should never reach this point.
613 * It means that we're trying to
614 * transfer more blocks than can fit
615 * into the scatter list.
617 BUG_ON(1);
619 host->size = data->bytes_xfered;
621 return;
624 buffer = wbsd_sg_to_buffer(host);
630 * The controller stops sending interrupts for
631 * 'FIFO empty' under certain conditions. So we
632 * need to be a bit more pro-active.
634 tasklet_schedule(&host->fifo_tasklet);
637 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
639 u16 blksize;
640 u8 setup;
641 unsigned long dmaflags;
643 DBGF("blksz %04x blks %04x flags %08x\n",
644 data->blksz, data->blocks, data->flags);
645 DBGF("tsac %d ms nsac %d clk\n",
646 data->timeout_ns / 1000000, data->timeout_clks);
649 * Calculate size.
651 host->size = data->blocks * data->blksz;
654 * Check timeout values for overflow.
655 * (Yes, some cards cause this value to overflow).
657 if (data->timeout_ns > 127000000)
658 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
659 else {
660 wbsd_write_index(host, WBSD_IDX_TAAC,
661 data->timeout_ns / 1000000);
664 if (data->timeout_clks > 255)
665 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
666 else
667 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
670 * Inform the chip of how large blocks will be
671 * sent. It needs this to determine when to
672 * calculate CRC.
674 * Space for CRC must be included in the size.
675 * Two bytes are needed for each data line.
677 if (host->bus_width == MMC_BUS_WIDTH_1) {
678 blksize = data->blksz + 2;
680 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
681 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
682 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
683 blksize = data->blksz + 2 * 4;
685 wbsd_write_index(host, WBSD_IDX_PBSMSB,
686 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
687 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
688 } else {
689 data->error = MMC_ERR_INVALID;
690 return;
694 * Clear the FIFO. This is needed even for DMA
695 * transfers since the chip still uses the FIFO
696 * internally.
698 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
699 setup |= WBSD_FIFO_RESET;
700 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
703 * DMA transfer?
705 if (host->dma >= 0) {
707 * The buffer for DMA is only 64 kB.
709 BUG_ON(host->size > 0x10000);
710 if (host->size > 0x10000) {
711 data->error = MMC_ERR_INVALID;
712 return;
716 * Transfer data from the SG list to
717 * the DMA buffer.
719 if (data->flags & MMC_DATA_WRITE)
720 wbsd_sg_to_dma(host, data);
723 * Initialise the ISA DMA controller.
725 dmaflags = claim_dma_lock();
726 disable_dma(host->dma);
727 clear_dma_ff(host->dma);
728 if (data->flags & MMC_DATA_READ)
729 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
730 else
731 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
732 set_dma_addr(host->dma, host->dma_addr);
733 set_dma_count(host->dma, host->size);
735 enable_dma(host->dma);
736 release_dma_lock(dmaflags);
739 * Enable DMA on the host.
741 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
742 } else {
744 * This flag is used to keep printk
745 * output to a minimum.
747 host->firsterr = 1;
750 * Initialise the SG list.
752 wbsd_init_sg(host, data);
755 * Turn off DMA.
757 wbsd_write_index(host, WBSD_IDX_DMA, 0);
760 * Set up FIFO threshold levels (and fill
761 * buffer if doing a write).
763 if (data->flags & MMC_DATA_READ) {
764 wbsd_write_index(host, WBSD_IDX_FIFOEN,
765 WBSD_FIFOEN_FULL | 8);
766 } else {
767 wbsd_write_index(host, WBSD_IDX_FIFOEN,
768 WBSD_FIFOEN_EMPTY | 8);
769 wbsd_fill_fifo(host);
773 data->error = MMC_ERR_NONE;
776 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
778 unsigned long dmaflags;
779 int count;
780 u8 status;
782 WARN_ON(host->mrq == NULL);
785 * Send a stop command if needed.
787 if (data->stop)
788 wbsd_send_command(host, data->stop);
791 * Wait for the controller to leave data
792 * transfer state.
794 do {
795 status = wbsd_read_index(host, WBSD_IDX_STATUS);
796 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
799 * DMA transfer?
801 if (host->dma >= 0) {
803 * Disable DMA on the host.
805 wbsd_write_index(host, WBSD_IDX_DMA, 0);
808 * Turn of ISA DMA controller.
810 dmaflags = claim_dma_lock();
811 disable_dma(host->dma);
812 clear_dma_ff(host->dma);
813 count = get_dma_residue(host->dma);
814 release_dma_lock(dmaflags);
817 * Any leftover data?
819 if (count) {
820 printk(KERN_ERR "%s: Incomplete DMA transfer. "
821 "%d bytes left.\n",
822 mmc_hostname(host->mmc), count);
824 data->error = MMC_ERR_FAILED;
825 } else {
827 * Transfer data from DMA buffer to
828 * SG list.
830 if (data->flags & MMC_DATA_READ)
831 wbsd_dma_to_sg(host, data);
833 data->bytes_xfered = host->size;
837 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
839 wbsd_request_end(host, host->mrq);
842 /*****************************************************************************\
844 * MMC layer callbacks *
846 \*****************************************************************************/
848 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
850 struct wbsd_host *host = mmc_priv(mmc);
851 struct mmc_command *cmd;
854 * Disable tasklets to avoid a deadlock.
856 spin_lock_bh(&host->lock);
858 BUG_ON(host->mrq != NULL);
860 cmd = mrq->cmd;
862 host->mrq = mrq;
865 * If there is no card in the slot then
866 * timeout immediatly.
868 if (!(host->flags & WBSD_FCARD_PRESENT)) {
869 cmd->error = MMC_ERR_TIMEOUT;
870 goto done;
874 * Does the request include data?
876 if (cmd->data) {
877 wbsd_prepare_data(host, cmd->data);
879 if (cmd->data->error != MMC_ERR_NONE)
880 goto done;
883 wbsd_send_command(host, cmd);
886 * If this is a data transfer the request
887 * will be finished after the data has
888 * transfered.
890 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
892 * The hardware is so delightfully stupid that it has a list
893 * of "data" commands. If a command isn't on this list, it'll
894 * just go back to the idle state and won't send any data
895 * interrupts.
897 switch (cmd->opcode) {
898 case 11:
899 case 17:
900 case 18:
901 case 20:
902 case 24:
903 case 25:
904 case 26:
905 case 27:
906 case 30:
907 case 42:
908 case 56:
909 break;
911 /* ACMDs. We don't keep track of state, so we just treat them
912 * like any other command. */
913 case 51:
914 break;
916 default:
917 #ifdef CONFIG_MMC_DEBUG
918 printk(KERN_WARNING "%s: Data command %d is not "
919 "supported by this controller.\n",
920 mmc_hostname(host->mmc), cmd->opcode);
921 #endif
922 cmd->data->error = MMC_ERR_INVALID;
924 if (cmd->data->stop)
925 wbsd_send_command(host, cmd->data->stop);
927 goto done;
931 * Dirty fix for hardware bug.
933 if (host->dma == -1)
934 tasklet_schedule(&host->fifo_tasklet);
936 spin_unlock_bh(&host->lock);
938 return;
941 done:
942 wbsd_request_end(host, mrq);
944 spin_unlock_bh(&host->lock);
947 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
949 struct wbsd_host *host = mmc_priv(mmc);
950 u8 clk, setup, pwr;
952 spin_lock_bh(&host->lock);
955 * Reset the chip on each power off.
956 * Should clear out any weird states.
958 if (ios->power_mode == MMC_POWER_OFF)
959 wbsd_init_device(host);
961 if (ios->clock >= 24000000)
962 clk = WBSD_CLK_24M;
963 else if (ios->clock >= 16000000)
964 clk = WBSD_CLK_16M;
965 else if (ios->clock >= 12000000)
966 clk = WBSD_CLK_12M;
967 else
968 clk = WBSD_CLK_375K;
971 * Only write to the clock register when
972 * there is an actual change.
974 if (clk != host->clk) {
975 wbsd_write_index(host, WBSD_IDX_CLK, clk);
976 host->clk = clk;
980 * Power up card.
982 if (ios->power_mode != MMC_POWER_OFF) {
983 pwr = inb(host->base + WBSD_CSR);
984 pwr &= ~WBSD_POWER_N;
985 outb(pwr, host->base + WBSD_CSR);
989 * MMC cards need to have pin 1 high during init.
990 * It wreaks havoc with the card detection though so
991 * that needs to be disabled.
993 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
994 if (ios->chip_select == MMC_CS_HIGH) {
995 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
996 setup |= WBSD_DAT3_H;
997 host->flags |= WBSD_FIGNORE_DETECT;
998 } else {
999 if (setup & WBSD_DAT3_H) {
1000 setup &= ~WBSD_DAT3_H;
1003 * We cannot resume card detection immediatly
1004 * because of capacitance and delays in the chip.
1006 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1009 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1012 * Store bus width for later. Will be used when
1013 * setting up the data transfer.
1015 host->bus_width = ios->bus_width;
1017 spin_unlock_bh(&host->lock);
1020 static int wbsd_get_ro(struct mmc_host *mmc)
1022 struct wbsd_host *host = mmc_priv(mmc);
1023 u8 csr;
1025 spin_lock_bh(&host->lock);
1027 csr = inb(host->base + WBSD_CSR);
1028 csr |= WBSD_MSLED;
1029 outb(csr, host->base + WBSD_CSR);
1031 mdelay(1);
1033 csr = inb(host->base + WBSD_CSR);
1034 csr &= ~WBSD_MSLED;
1035 outb(csr, host->base + WBSD_CSR);
1037 spin_unlock_bh(&host->lock);
1039 return csr & WBSD_WRPT;
1042 static const struct mmc_host_ops wbsd_ops = {
1043 .request = wbsd_request,
1044 .set_ios = wbsd_set_ios,
1045 .get_ro = wbsd_get_ro,
1048 /*****************************************************************************\
1050 * Interrupt handling *
1052 \*****************************************************************************/
1055 * Helper function to reset detection ignore
1058 static void wbsd_reset_ignore(unsigned long data)
1060 struct wbsd_host *host = (struct wbsd_host *)data;
1062 BUG_ON(host == NULL);
1064 DBG("Resetting card detection ignore\n");
1066 spin_lock_bh(&host->lock);
1068 host->flags &= ~WBSD_FIGNORE_DETECT;
1071 * Card status might have changed during the
1072 * blackout.
1074 tasklet_schedule(&host->card_tasklet);
1076 spin_unlock_bh(&host->lock);
1080 * Tasklets
1083 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1085 WARN_ON(!host->mrq);
1086 if (!host->mrq)
1087 return NULL;
1089 WARN_ON(!host->mrq->cmd);
1090 if (!host->mrq->cmd)
1091 return NULL;
1093 WARN_ON(!host->mrq->cmd->data);
1094 if (!host->mrq->cmd->data)
1095 return NULL;
1097 return host->mrq->cmd->data;
1100 static void wbsd_tasklet_card(unsigned long param)
1102 struct wbsd_host *host = (struct wbsd_host *)param;
1103 u8 csr;
1104 int delay = -1;
1106 spin_lock(&host->lock);
1108 if (host->flags & WBSD_FIGNORE_DETECT) {
1109 spin_unlock(&host->lock);
1110 return;
1113 csr = inb(host->base + WBSD_CSR);
1114 WARN_ON(csr == 0xff);
1116 if (csr & WBSD_CARDPRESENT) {
1117 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1118 DBG("Card inserted\n");
1119 host->flags |= WBSD_FCARD_PRESENT;
1121 delay = 500;
1123 } else if (host->flags & WBSD_FCARD_PRESENT) {
1124 DBG("Card removed\n");
1125 host->flags &= ~WBSD_FCARD_PRESENT;
1127 if (host->mrq) {
1128 printk(KERN_ERR "%s: Card removed during transfer!\n",
1129 mmc_hostname(host->mmc));
1130 wbsd_reset(host);
1132 host->mrq->cmd->error = MMC_ERR_FAILED;
1133 tasklet_schedule(&host->finish_tasklet);
1136 delay = 0;
1140 * Unlock first since we might get a call back.
1143 spin_unlock(&host->lock);
1145 if (delay != -1)
1146 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1149 static void wbsd_tasklet_fifo(unsigned long param)
1151 struct wbsd_host *host = (struct wbsd_host *)param;
1152 struct mmc_data *data;
1154 spin_lock(&host->lock);
1156 if (!host->mrq)
1157 goto end;
1159 data = wbsd_get_data(host);
1160 if (!data)
1161 goto end;
1163 if (data->flags & MMC_DATA_WRITE)
1164 wbsd_fill_fifo(host);
1165 else
1166 wbsd_empty_fifo(host);
1169 * Done?
1171 if (host->size == data->bytes_xfered) {
1172 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1173 tasklet_schedule(&host->finish_tasklet);
1176 end:
1177 spin_unlock(&host->lock);
1180 static void wbsd_tasklet_crc(unsigned long param)
1182 struct wbsd_host *host = (struct wbsd_host *)param;
1183 struct mmc_data *data;
1185 spin_lock(&host->lock);
1187 if (!host->mrq)
1188 goto end;
1190 data = wbsd_get_data(host);
1191 if (!data)
1192 goto end;
1194 DBGF("CRC error\n");
1196 data->error = MMC_ERR_BADCRC;
1198 tasklet_schedule(&host->finish_tasklet);
1200 end:
1201 spin_unlock(&host->lock);
1204 static void wbsd_tasklet_timeout(unsigned long param)
1206 struct wbsd_host *host = (struct wbsd_host *)param;
1207 struct mmc_data *data;
1209 spin_lock(&host->lock);
1211 if (!host->mrq)
1212 goto end;
1214 data = wbsd_get_data(host);
1215 if (!data)
1216 goto end;
1218 DBGF("Timeout\n");
1220 data->error = MMC_ERR_TIMEOUT;
1222 tasklet_schedule(&host->finish_tasklet);
1224 end:
1225 spin_unlock(&host->lock);
1228 static void wbsd_tasklet_finish(unsigned long param)
1230 struct wbsd_host *host = (struct wbsd_host *)param;
1231 struct mmc_data *data;
1233 spin_lock(&host->lock);
1235 WARN_ON(!host->mrq);
1236 if (!host->mrq)
1237 goto end;
1239 data = wbsd_get_data(host);
1240 if (!data)
1241 goto end;
1243 wbsd_finish_data(host, data);
1245 end:
1246 spin_unlock(&host->lock);
1249 static void wbsd_tasklet_block(unsigned long param)
1251 struct wbsd_host *host = (struct wbsd_host *)param;
1252 struct mmc_data *data;
1254 spin_lock(&host->lock);
1256 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1257 WBSD_CRC_OK) {
1258 data = wbsd_get_data(host);
1259 if (!data)
1260 goto end;
1262 DBGF("CRC error\n");
1264 data->error = MMC_ERR_BADCRC;
1266 tasklet_schedule(&host->finish_tasklet);
1269 end:
1270 spin_unlock(&host->lock);
1274 * Interrupt handling
1277 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1279 struct wbsd_host *host = dev_id;
1280 int isr;
1282 isr = inb(host->base + WBSD_ISR);
1285 * Was it actually our hardware that caused the interrupt?
1287 if (isr == 0xff || isr == 0x00)
1288 return IRQ_NONE;
1290 host->isr |= isr;
1293 * Schedule tasklets as needed.
1295 if (isr & WBSD_INT_CARD)
1296 tasklet_schedule(&host->card_tasklet);
1297 if (isr & WBSD_INT_FIFO_THRE)
1298 tasklet_schedule(&host->fifo_tasklet);
1299 if (isr & WBSD_INT_CRC)
1300 tasklet_hi_schedule(&host->crc_tasklet);
1301 if (isr & WBSD_INT_TIMEOUT)
1302 tasklet_hi_schedule(&host->timeout_tasklet);
1303 if (isr & WBSD_INT_BUSYEND)
1304 tasklet_hi_schedule(&host->block_tasklet);
1305 if (isr & WBSD_INT_TC)
1306 tasklet_schedule(&host->finish_tasklet);
1308 return IRQ_HANDLED;
1311 /*****************************************************************************\
1313 * Device initialisation and shutdown *
1315 \*****************************************************************************/
1318 * Allocate/free MMC structure.
1321 static int __devinit wbsd_alloc_mmc(struct device *dev)
1323 struct mmc_host *mmc;
1324 struct wbsd_host *host;
1327 * Allocate MMC structure.
1329 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1330 if (!mmc)
1331 return -ENOMEM;
1333 host = mmc_priv(mmc);
1334 host->mmc = mmc;
1336 host->dma = -1;
1339 * Set host parameters.
1341 mmc->ops = &wbsd_ops;
1342 mmc->f_min = 375000;
1343 mmc->f_max = 24000000;
1344 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1345 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1347 spin_lock_init(&host->lock);
1350 * Set up timers
1352 init_timer(&host->ignore_timer);
1353 host->ignore_timer.data = (unsigned long)host;
1354 host->ignore_timer.function = wbsd_reset_ignore;
1357 * Maximum number of segments. Worst case is one sector per segment
1358 * so this will be 64kB/512.
1360 mmc->max_hw_segs = 128;
1361 mmc->max_phys_segs = 128;
1364 * Maximum request size. Also limited by 64KiB buffer.
1366 mmc->max_req_size = 65536;
1369 * Maximum segment size. Could be one segment with the maximum number
1370 * of bytes.
1372 mmc->max_seg_size = mmc->max_req_size;
1375 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1376 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1378 mmc->max_blk_size = 4087;
1381 * Maximum block count. There is no real limit so the maximum
1382 * request size will be the only restriction.
1384 mmc->max_blk_count = mmc->max_req_size;
1386 dev_set_drvdata(dev, mmc);
1388 return 0;
1391 static void __devexit wbsd_free_mmc(struct device *dev)
1393 struct mmc_host *mmc;
1394 struct wbsd_host *host;
1396 mmc = dev_get_drvdata(dev);
1397 if (!mmc)
1398 return;
1400 host = mmc_priv(mmc);
1401 BUG_ON(host == NULL);
1403 del_timer_sync(&host->ignore_timer);
1405 mmc_free_host(mmc);
1407 dev_set_drvdata(dev, NULL);
1411 * Scan for known chip id:s
1414 static int __devinit wbsd_scan(struct wbsd_host *host)
1416 int i, j, k;
1417 int id;
1420 * Iterate through all ports, all codes to
1421 * find hardware that is in our known list.
1423 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1424 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1425 continue;
1427 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1428 id = 0xFFFF;
1430 host->config = config_ports[i];
1431 host->unlock_code = unlock_codes[j];
1433 wbsd_unlock_config(host);
1435 outb(WBSD_CONF_ID_HI, config_ports[i]);
1436 id = inb(config_ports[i] + 1) << 8;
1438 outb(WBSD_CONF_ID_LO, config_ports[i]);
1439 id |= inb(config_ports[i] + 1);
1441 wbsd_lock_config(host);
1443 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1444 if (id == valid_ids[k]) {
1445 host->chip_id = id;
1447 return 0;
1451 if (id != 0xFFFF) {
1452 DBG("Unknown hardware (id %x) found at %x\n",
1453 id, config_ports[i]);
1457 release_region(config_ports[i], 2);
1460 host->config = 0;
1461 host->unlock_code = 0;
1463 return -ENODEV;
1467 * Allocate/free io port ranges
1470 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1472 if (base & 0x7)
1473 return -EINVAL;
1475 if (!request_region(base, 8, DRIVER_NAME))
1476 return -EIO;
1478 host->base = base;
1480 return 0;
1483 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1485 if (host->base)
1486 release_region(host->base, 8);
1488 host->base = 0;
1490 if (host->config)
1491 release_region(host->config, 2);
1493 host->config = 0;
1497 * Allocate/free DMA port and buffer
1500 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1502 if (dma < 0)
1503 return;
1505 if (request_dma(dma, DRIVER_NAME))
1506 goto err;
1509 * We need to allocate a special buffer in
1510 * order for ISA to be able to DMA to it.
1512 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1513 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1514 if (!host->dma_buffer)
1515 goto free;
1518 * Translate the address to a physical address.
1520 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1521 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1524 * ISA DMA must be aligned on a 64k basis.
1526 if ((host->dma_addr & 0xffff) != 0)
1527 goto kfree;
1529 * ISA cannot access memory above 16 MB.
1531 else if (host->dma_addr >= 0x1000000)
1532 goto kfree;
1534 host->dma = dma;
1536 return;
1538 kfree:
1540 * If we've gotten here then there is some kind of alignment bug
1542 BUG_ON(1);
1544 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1545 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1546 host->dma_addr = (dma_addr_t)NULL;
1548 kfree(host->dma_buffer);
1549 host->dma_buffer = NULL;
1551 free:
1552 free_dma(dma);
1554 err:
1555 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1556 "Falling back on FIFO.\n", dma);
1559 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1561 if (host->dma_addr) {
1562 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1563 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1565 kfree(host->dma_buffer);
1566 if (host->dma >= 0)
1567 free_dma(host->dma);
1569 host->dma = -1;
1570 host->dma_buffer = NULL;
1571 host->dma_addr = (dma_addr_t)NULL;
1575 * Allocate/free IRQ.
1578 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1580 int ret;
1583 * Allocate interrupt.
1586 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1587 if (ret)
1588 return ret;
1590 host->irq = irq;
1593 * Set up tasklets.
1595 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1596 (unsigned long)host);
1597 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1598 (unsigned long)host);
1599 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1600 (unsigned long)host);
1601 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1602 (unsigned long)host);
1603 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1604 (unsigned long)host);
1605 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1606 (unsigned long)host);
1608 return 0;
1611 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1613 if (!host->irq)
1614 return;
1616 free_irq(host->irq, host);
1618 host->irq = 0;
1620 tasklet_kill(&host->card_tasklet);
1621 tasklet_kill(&host->fifo_tasklet);
1622 tasklet_kill(&host->crc_tasklet);
1623 tasklet_kill(&host->timeout_tasklet);
1624 tasklet_kill(&host->finish_tasklet);
1625 tasklet_kill(&host->block_tasklet);
1629 * Allocate all resources for the host.
1632 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1633 int base, int irq, int dma)
1635 int ret;
1638 * Allocate I/O ports.
1640 ret = wbsd_request_region(host, base);
1641 if (ret)
1642 return ret;
1645 * Allocate interrupt.
1647 ret = wbsd_request_irq(host, irq);
1648 if (ret)
1649 return ret;
1652 * Allocate DMA.
1654 wbsd_request_dma(host, dma);
1656 return 0;
1660 * Release all resources for the host.
1663 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1665 wbsd_release_dma(host);
1666 wbsd_release_irq(host);
1667 wbsd_release_regions(host);
1671 * Configure the resources the chip should use.
1674 static void wbsd_chip_config(struct wbsd_host *host)
1676 wbsd_unlock_config(host);
1679 * Reset the chip.
1681 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1682 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1685 * Select SD/MMC function.
1687 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1690 * Set up card detection.
1692 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1695 * Configure chip
1697 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1698 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1700 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1702 if (host->dma >= 0)
1703 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1706 * Enable and power up chip.
1708 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1709 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1711 wbsd_lock_config(host);
1715 * Check that configured resources are correct.
1718 static int wbsd_chip_validate(struct wbsd_host *host)
1720 int base, irq, dma;
1722 wbsd_unlock_config(host);
1725 * Select SD/MMC function.
1727 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1730 * Read configuration.
1732 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1733 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1735 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1737 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1739 wbsd_lock_config(host);
1742 * Validate against given configuration.
1744 if (base != host->base)
1745 return 0;
1746 if (irq != host->irq)
1747 return 0;
1748 if ((dma != host->dma) && (host->dma != -1))
1749 return 0;
1751 return 1;
1755 * Powers down the SD function
1758 static void wbsd_chip_poweroff(struct wbsd_host *host)
1760 wbsd_unlock_config(host);
1762 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1763 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1765 wbsd_lock_config(host);
1768 /*****************************************************************************\
1770 * Devices setup and shutdown *
1772 \*****************************************************************************/
1774 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1775 int pnp)
1777 struct wbsd_host *host = NULL;
1778 struct mmc_host *mmc = NULL;
1779 int ret;
1781 ret = wbsd_alloc_mmc(dev);
1782 if (ret)
1783 return ret;
1785 mmc = dev_get_drvdata(dev);
1786 host = mmc_priv(mmc);
1789 * Scan for hardware.
1791 ret = wbsd_scan(host);
1792 if (ret) {
1793 if (pnp && (ret == -ENODEV)) {
1794 printk(KERN_WARNING DRIVER_NAME
1795 ": Unable to confirm device presence. You may "
1796 "experience lock-ups.\n");
1797 } else {
1798 wbsd_free_mmc(dev);
1799 return ret;
1804 * Request resources.
1806 ret = wbsd_request_resources(host, base, irq, dma);
1807 if (ret) {
1808 wbsd_release_resources(host);
1809 wbsd_free_mmc(dev);
1810 return ret;
1814 * See if chip needs to be configured.
1816 if (pnp) {
1817 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1818 printk(KERN_WARNING DRIVER_NAME
1819 ": PnP active but chip not configured! "
1820 "You probably have a buggy BIOS. "
1821 "Configuring chip manually.\n");
1822 wbsd_chip_config(host);
1824 } else
1825 wbsd_chip_config(host);
1828 * Power Management stuff. No idea how this works.
1829 * Not tested.
1831 #ifdef CONFIG_PM
1832 if (host->config) {
1833 wbsd_unlock_config(host);
1834 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1835 wbsd_lock_config(host);
1837 #endif
1839 * Allow device to initialise itself properly.
1841 mdelay(5);
1844 * Reset the chip into a known state.
1846 wbsd_init_device(host);
1848 mmc_add_host(mmc);
1850 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1851 if (host->chip_id != 0)
1852 printk(" id %x", (int)host->chip_id);
1853 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1854 if (host->dma >= 0)
1855 printk(" dma %d", (int)host->dma);
1856 else
1857 printk(" FIFO");
1858 if (pnp)
1859 printk(" PnP");
1860 printk("\n");
1862 return 0;
1865 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1867 struct mmc_host *mmc = dev_get_drvdata(dev);
1868 struct wbsd_host *host;
1870 if (!mmc)
1871 return;
1873 host = mmc_priv(mmc);
1875 mmc_remove_host(mmc);
1878 * Power down the SD/MMC function.
1880 if (!pnp)
1881 wbsd_chip_poweroff(host);
1883 wbsd_release_resources(host);
1885 wbsd_free_mmc(dev);
1889 * Non-PnP
1892 static int __devinit wbsd_probe(struct platform_device *dev)
1894 /* Use the module parameters for resources */
1895 return wbsd_init(&dev->dev, io, irq, dma, 0);
1898 static int __devexit wbsd_remove(struct platform_device *dev)
1900 wbsd_shutdown(&dev->dev, 0);
1902 return 0;
1906 * PnP
1909 #ifdef CONFIG_PNP
1911 static int __devinit
1912 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1914 int io, irq, dma;
1917 * Get resources from PnP layer.
1919 io = pnp_port_start(pnpdev, 0);
1920 irq = pnp_irq(pnpdev, 0);
1921 if (pnp_dma_valid(pnpdev, 0))
1922 dma = pnp_dma(pnpdev, 0);
1923 else
1924 dma = -1;
1926 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1928 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1931 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1933 wbsd_shutdown(&dev->dev, 1);
1936 #endif /* CONFIG_PNP */
1939 * Power management
1942 #ifdef CONFIG_PM
1944 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1946 BUG_ON(host == NULL);
1948 return mmc_suspend_host(host->mmc, state);
1951 static int wbsd_resume(struct wbsd_host *host)
1953 BUG_ON(host == NULL);
1955 wbsd_init_device(host);
1957 return mmc_resume_host(host->mmc);
1960 static int wbsd_platform_suspend(struct platform_device *dev,
1961 pm_message_t state)
1963 struct mmc_host *mmc = platform_get_drvdata(dev);
1964 struct wbsd_host *host;
1965 int ret;
1967 if (mmc == NULL)
1968 return 0;
1970 DBGF("Suspending...\n");
1972 host = mmc_priv(mmc);
1974 ret = wbsd_suspend(host, state);
1975 if (ret)
1976 return ret;
1978 wbsd_chip_poweroff(host);
1980 return 0;
1983 static int wbsd_platform_resume(struct platform_device *dev)
1985 struct mmc_host *mmc = platform_get_drvdata(dev);
1986 struct wbsd_host *host;
1988 if (mmc == NULL)
1989 return 0;
1991 DBGF("Resuming...\n");
1993 host = mmc_priv(mmc);
1995 wbsd_chip_config(host);
1998 * Allow device to initialise itself properly.
2000 mdelay(5);
2002 return wbsd_resume(host);
2005 #ifdef CONFIG_PNP
2007 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2009 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2010 struct wbsd_host *host;
2012 if (mmc == NULL)
2013 return 0;
2015 DBGF("Suspending...\n");
2017 host = mmc_priv(mmc);
2019 return wbsd_suspend(host, state);
2022 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2024 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2025 struct wbsd_host *host;
2027 if (mmc == NULL)
2028 return 0;
2030 DBGF("Resuming...\n");
2032 host = mmc_priv(mmc);
2035 * See if chip needs to be configured.
2037 if (host->config != 0) {
2038 if (!wbsd_chip_validate(host)) {
2039 printk(KERN_WARNING DRIVER_NAME
2040 ": PnP active but chip not configured! "
2041 "You probably have a buggy BIOS. "
2042 "Configuring chip manually.\n");
2043 wbsd_chip_config(host);
2048 * Allow device to initialise itself properly.
2050 mdelay(5);
2052 return wbsd_resume(host);
2055 #endif /* CONFIG_PNP */
2057 #else /* CONFIG_PM */
2059 #define wbsd_platform_suspend NULL
2060 #define wbsd_platform_resume NULL
2062 #define wbsd_pnp_suspend NULL
2063 #define wbsd_pnp_resume NULL
2065 #endif /* CONFIG_PM */
2067 static struct platform_device *wbsd_device;
2069 static struct platform_driver wbsd_driver = {
2070 .probe = wbsd_probe,
2071 .remove = __devexit_p(wbsd_remove),
2073 .suspend = wbsd_platform_suspend,
2074 .resume = wbsd_platform_resume,
2075 .driver = {
2076 .name = DRIVER_NAME,
2080 #ifdef CONFIG_PNP
2082 static struct pnp_driver wbsd_pnp_driver = {
2083 .name = DRIVER_NAME,
2084 .id_table = pnp_dev_table,
2085 .probe = wbsd_pnp_probe,
2086 .remove = __devexit_p(wbsd_pnp_remove),
2088 .suspend = wbsd_pnp_suspend,
2089 .resume = wbsd_pnp_resume,
2092 #endif /* CONFIG_PNP */
2095 * Module loading/unloading
2098 static int __init wbsd_drv_init(void)
2100 int result;
2102 printk(KERN_INFO DRIVER_NAME
2103 ": Winbond W83L51xD SD/MMC card interface driver\n");
2104 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2106 #ifdef CONFIG_PNP
2108 if (!nopnp) {
2109 result = pnp_register_driver(&wbsd_pnp_driver);
2110 if (result < 0)
2111 return result;
2113 #endif /* CONFIG_PNP */
2115 if (nopnp) {
2116 result = platform_driver_register(&wbsd_driver);
2117 if (result < 0)
2118 return result;
2120 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2121 if (!wbsd_device) {
2122 platform_driver_unregister(&wbsd_driver);
2123 return -ENOMEM;
2126 result = platform_device_add(wbsd_device);
2127 if (result) {
2128 platform_device_put(wbsd_device);
2129 platform_driver_unregister(&wbsd_driver);
2130 return result;
2134 return 0;
2137 static void __exit wbsd_drv_exit(void)
2139 #ifdef CONFIG_PNP
2141 if (!nopnp)
2142 pnp_unregister_driver(&wbsd_pnp_driver);
2144 #endif /* CONFIG_PNP */
2146 if (nopnp) {
2147 platform_device_unregister(wbsd_device);
2149 platform_driver_unregister(&wbsd_driver);
2152 DBG("unloaded\n");
2155 module_init(wbsd_drv_init);
2156 module_exit(wbsd_drv_exit);
2157 #ifdef CONFIG_PNP
2158 module_param(nopnp, uint, 0444);
2159 #endif
2160 module_param(io, uint, 0444);
2161 module_param(irq, uint, 0444);
2162 module_param(dma, int, 0444);
2164 MODULE_LICENSE("GPL");
2165 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2166 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2168 #ifdef CONFIG_PNP
2169 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2170 #endif
2171 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2172 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2173 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");