mmc: cull sg list to match mmc request size
[linux-2.6/cjktty.git] / drivers / mmc / wbsd.c
blob7a3e32ec46b8e4dc66437a99a51ed754db77890b
1 /*
2 * linux/drivers/mmc/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.
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;
282 struct scatterlist *sg;
283 char *dmabuf = host->dma_buffer;
284 char *sgbuf;
286 sg = data->sg;
287 len = data->sg_len;
289 for (i = 0; i < len; i++) {
290 sgbuf = page_address(sg[i].page) + sg[i].offset;
291 memcpy(dmabuf, sgbuf, sg[i].length);
292 dmabuf += sg[i].length;
296 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
298 unsigned int len, i;
299 struct scatterlist *sg;
300 char *dmabuf = host->dma_buffer;
301 char *sgbuf;
303 sg = data->sg;
304 len = data->sg_len;
306 for (i = 0; i < len; i++) {
307 sgbuf = page_address(sg[i].page) + sg[i].offset;
308 memcpy(sgbuf, dmabuf, sg[i].length);
309 dmabuf += sg[i].length;
314 * Command handling
317 static inline void wbsd_get_short_reply(struct wbsd_host *host,
318 struct mmc_command *cmd)
321 * Correct response type?
323 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
324 cmd->error = MMC_ERR_INVALID;
325 return;
328 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
329 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
330 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
331 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
332 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
335 static inline void wbsd_get_long_reply(struct wbsd_host *host,
336 struct mmc_command *cmd)
338 int i;
341 * Correct response type?
343 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
344 cmd->error = MMC_ERR_INVALID;
345 return;
348 for (i = 0; i < 4; i++) {
349 cmd->resp[i] =
350 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
351 cmd->resp[i] |=
352 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
353 cmd->resp[i] |=
354 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
355 cmd->resp[i] |=
356 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
360 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
362 int i;
363 u8 status, isr;
365 DBGF("Sending cmd (%x)\n", cmd->opcode);
368 * Clear accumulated ISR. The interrupt routine
369 * will fill this one with events that occur during
370 * transfer.
372 host->isr = 0;
375 * Send the command (CRC calculated by host).
377 outb(cmd->opcode, host->base + WBSD_CMDR);
378 for (i = 3; i >= 0; i--)
379 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
381 cmd->error = MMC_ERR_NONE;
384 * Wait for the request to complete.
386 do {
387 status = wbsd_read_index(host, WBSD_IDX_STATUS);
388 } while (status & WBSD_CARDTRAFFIC);
391 * Do we expect a reply?
393 if (cmd->flags & MMC_RSP_PRESENT) {
395 * Read back status.
397 isr = host->isr;
399 /* Card removed? */
400 if (isr & WBSD_INT_CARD)
401 cmd->error = MMC_ERR_TIMEOUT;
402 /* Timeout? */
403 else if (isr & WBSD_INT_TIMEOUT)
404 cmd->error = MMC_ERR_TIMEOUT;
405 /* CRC? */
406 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
407 cmd->error = MMC_ERR_BADCRC;
408 /* All ok */
409 else {
410 if (cmd->flags & MMC_RSP_136)
411 wbsd_get_long_reply(host, cmd);
412 else
413 wbsd_get_short_reply(host, cmd);
417 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
421 * Data functions
424 static void wbsd_empty_fifo(struct wbsd_host *host)
426 struct mmc_data *data = host->mrq->cmd->data;
427 char *buffer;
428 int i, fsr, fifo;
431 * Handle excessive data.
433 if (host->num_sg == 0)
434 return;
436 buffer = wbsd_sg_to_buffer(host) + host->offset;
439 * Drain the fifo. This has a tendency to loop longer
440 * than the FIFO length (usually one block).
442 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
444 * The size field in the FSR is broken so we have to
445 * do some guessing.
447 if (fsr & WBSD_FIFO_FULL)
448 fifo = 16;
449 else if (fsr & WBSD_FIFO_FUTHRE)
450 fifo = 8;
451 else
452 fifo = 1;
454 for (i = 0; i < fifo; i++) {
455 *buffer = inb(host->base + WBSD_DFR);
456 buffer++;
457 host->offset++;
458 host->remain--;
460 data->bytes_xfered++;
463 * End of scatter list entry?
465 if (host->remain == 0) {
467 * Get next entry. Check if last.
469 if (!wbsd_next_sg(host))
470 return;
472 buffer = wbsd_sg_to_buffer(host);
478 * This is a very dirty hack to solve a
479 * hardware problem. The chip doesn't trigger
480 * FIFO threshold interrupts properly.
482 if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
483 tasklet_schedule(&host->fifo_tasklet);
486 static void wbsd_fill_fifo(struct wbsd_host *host)
488 struct mmc_data *data = host->mrq->cmd->data;
489 char *buffer;
490 int i, fsr, fifo;
493 * Check that we aren't being called after the
494 * entire buffer has been transfered.
496 if (host->num_sg == 0)
497 return;
499 buffer = wbsd_sg_to_buffer(host) + host->offset;
502 * Fill the fifo. This has a tendency to loop longer
503 * than the FIFO length (usually one block).
505 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
507 * The size field in the FSR is broken so we have to
508 * do some guessing.
510 if (fsr & WBSD_FIFO_EMPTY)
511 fifo = 0;
512 else if (fsr & WBSD_FIFO_EMTHRE)
513 fifo = 8;
514 else
515 fifo = 15;
517 for (i = 16; i > fifo; i--) {
518 outb(*buffer, host->base + WBSD_DFR);
519 buffer++;
520 host->offset++;
521 host->remain--;
523 data->bytes_xfered++;
526 * End of scatter list entry?
528 if (host->remain == 0) {
530 * Get next entry. Check if last.
532 if (!wbsd_next_sg(host))
533 return;
535 buffer = wbsd_sg_to_buffer(host);
541 * The controller stops sending interrupts for
542 * 'FIFO empty' under certain conditions. So we
543 * need to be a bit more pro-active.
545 tasklet_schedule(&host->fifo_tasklet);
548 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
550 u16 blksize;
551 u8 setup;
552 unsigned long dmaflags;
553 unsigned int size;
555 DBGF("blksz %04x blks %04x flags %08x\n",
556 data->blksz, data->blocks, data->flags);
557 DBGF("tsac %d ms nsac %d clk\n",
558 data->timeout_ns / 1000000, data->timeout_clks);
561 * Calculate size.
563 size = data->blocks * data->blksz;
566 * Check timeout values for overflow.
567 * (Yes, some cards cause this value to overflow).
569 if (data->timeout_ns > 127000000)
570 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
571 else {
572 wbsd_write_index(host, WBSD_IDX_TAAC,
573 data->timeout_ns / 1000000);
576 if (data->timeout_clks > 255)
577 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
578 else
579 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
582 * Inform the chip of how large blocks will be
583 * sent. It needs this to determine when to
584 * calculate CRC.
586 * Space for CRC must be included in the size.
587 * Two bytes are needed for each data line.
589 if (host->bus_width == MMC_BUS_WIDTH_1) {
590 blksize = data->blksz + 2;
592 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
593 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
594 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
595 blksize = data->blksz + 2 * 4;
597 wbsd_write_index(host, WBSD_IDX_PBSMSB,
598 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
599 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
600 } else {
601 data->error = MMC_ERR_INVALID;
602 return;
606 * Clear the FIFO. This is needed even for DMA
607 * transfers since the chip still uses the FIFO
608 * internally.
610 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
611 setup |= WBSD_FIFO_RESET;
612 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
615 * DMA transfer?
617 if (host->dma >= 0) {
619 * The buffer for DMA is only 64 kB.
621 BUG_ON(size > 0x10000);
622 if (size > 0x10000) {
623 data->error = MMC_ERR_INVALID;
624 return;
628 * Transfer data from the SG list to
629 * the DMA buffer.
631 if (data->flags & MMC_DATA_WRITE)
632 wbsd_sg_to_dma(host, data);
635 * Initialise the ISA DMA controller.
637 dmaflags = claim_dma_lock();
638 disable_dma(host->dma);
639 clear_dma_ff(host->dma);
640 if (data->flags & MMC_DATA_READ)
641 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
642 else
643 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
644 set_dma_addr(host->dma, host->dma_addr);
645 set_dma_count(host->dma, size);
647 enable_dma(host->dma);
648 release_dma_lock(dmaflags);
651 * Enable DMA on the host.
653 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
654 } else {
656 * This flag is used to keep printk
657 * output to a minimum.
659 host->firsterr = 1;
662 * Initialise the SG list.
664 wbsd_init_sg(host, data);
667 * Turn off DMA.
669 wbsd_write_index(host, WBSD_IDX_DMA, 0);
672 * Set up FIFO threshold levels (and fill
673 * buffer if doing a write).
675 if (data->flags & MMC_DATA_READ) {
676 wbsd_write_index(host, WBSD_IDX_FIFOEN,
677 WBSD_FIFOEN_FULL | 8);
678 } else {
679 wbsd_write_index(host, WBSD_IDX_FIFOEN,
680 WBSD_FIFOEN_EMPTY | 8);
681 wbsd_fill_fifo(host);
685 data->error = MMC_ERR_NONE;
688 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
690 unsigned long dmaflags;
691 int count;
692 u8 status;
694 WARN_ON(host->mrq == NULL);
697 * Send a stop command if needed.
699 if (data->stop)
700 wbsd_send_command(host, data->stop);
703 * Wait for the controller to leave data
704 * transfer state.
706 do {
707 status = wbsd_read_index(host, WBSD_IDX_STATUS);
708 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
711 * DMA transfer?
713 if (host->dma >= 0) {
715 * Disable DMA on the host.
717 wbsd_write_index(host, WBSD_IDX_DMA, 0);
720 * Turn of ISA DMA controller.
722 dmaflags = claim_dma_lock();
723 disable_dma(host->dma);
724 clear_dma_ff(host->dma);
725 count = get_dma_residue(host->dma);
726 release_dma_lock(dmaflags);
728 data->bytes_xfered = host->mrq->data->blocks *
729 host->mrq->data->blksz - count;
730 data->bytes_xfered -= data->bytes_xfered % data->blksz;
733 * Any leftover data?
735 if (count) {
736 printk(KERN_ERR "%s: Incomplete DMA transfer. "
737 "%d bytes left.\n",
738 mmc_hostname(host->mmc), count);
740 if (data->error == MMC_ERR_NONE)
741 data->error = MMC_ERR_FAILED;
742 } else {
744 * Transfer data from DMA buffer to
745 * SG list.
747 if (data->flags & MMC_DATA_READ)
748 wbsd_dma_to_sg(host, data);
751 if (data->error != MMC_ERR_NONE) {
752 if (data->bytes_xfered)
753 data->bytes_xfered -= data->blksz;
757 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
759 wbsd_request_end(host, host->mrq);
762 /*****************************************************************************\
764 * MMC layer callbacks *
766 \*****************************************************************************/
768 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
770 struct wbsd_host *host = mmc_priv(mmc);
771 struct mmc_command *cmd;
774 * Disable tasklets to avoid a deadlock.
776 spin_lock_bh(&host->lock);
778 BUG_ON(host->mrq != NULL);
780 cmd = mrq->cmd;
782 host->mrq = mrq;
785 * If there is no card in the slot then
786 * timeout immediatly.
788 if (!(host->flags & WBSD_FCARD_PRESENT)) {
789 cmd->error = MMC_ERR_TIMEOUT;
790 goto done;
794 * Does the request include data?
796 if (cmd->data) {
797 wbsd_prepare_data(host, cmd->data);
799 if (cmd->data->error != MMC_ERR_NONE)
800 goto done;
803 wbsd_send_command(host, cmd);
806 * If this is a data transfer the request
807 * will be finished after the data has
808 * transfered.
810 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
812 * The hardware is so delightfully stupid that it has a list
813 * of "data" commands. If a command isn't on this list, it'll
814 * just go back to the idle state and won't send any data
815 * interrupts.
817 switch (cmd->opcode) {
818 case 11:
819 case 17:
820 case 18:
821 case 20:
822 case 24:
823 case 25:
824 case 26:
825 case 27:
826 case 30:
827 case 42:
828 case 56:
829 break;
831 /* ACMDs. We don't keep track of state, so we just treat them
832 * like any other command. */
833 case 51:
834 break;
836 default:
837 #ifdef CONFIG_MMC_DEBUG
838 printk(KERN_WARNING "%s: Data command %d is not "
839 "supported by this controller.\n",
840 mmc_hostname(host->mmc), cmd->opcode);
841 #endif
842 cmd->data->error = MMC_ERR_INVALID;
844 if (cmd->data->stop)
845 wbsd_send_command(host, cmd->data->stop);
847 goto done;
851 * Dirty fix for hardware bug.
853 if (host->dma == -1)
854 tasklet_schedule(&host->fifo_tasklet);
856 spin_unlock_bh(&host->lock);
858 return;
861 done:
862 wbsd_request_end(host, mrq);
864 spin_unlock_bh(&host->lock);
867 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
869 struct wbsd_host *host = mmc_priv(mmc);
870 u8 clk, setup, pwr;
872 spin_lock_bh(&host->lock);
875 * Reset the chip on each power off.
876 * Should clear out any weird states.
878 if (ios->power_mode == MMC_POWER_OFF)
879 wbsd_init_device(host);
881 if (ios->clock >= 24000000)
882 clk = WBSD_CLK_24M;
883 else if (ios->clock >= 16000000)
884 clk = WBSD_CLK_16M;
885 else if (ios->clock >= 12000000)
886 clk = WBSD_CLK_12M;
887 else
888 clk = WBSD_CLK_375K;
891 * Only write to the clock register when
892 * there is an actual change.
894 if (clk != host->clk) {
895 wbsd_write_index(host, WBSD_IDX_CLK, clk);
896 host->clk = clk;
900 * Power up card.
902 if (ios->power_mode != MMC_POWER_OFF) {
903 pwr = inb(host->base + WBSD_CSR);
904 pwr &= ~WBSD_POWER_N;
905 outb(pwr, host->base + WBSD_CSR);
909 * MMC cards need to have pin 1 high during init.
910 * It wreaks havoc with the card detection though so
911 * that needs to be disabled.
913 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
914 if (ios->chip_select == MMC_CS_HIGH) {
915 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
916 setup |= WBSD_DAT3_H;
917 host->flags |= WBSD_FIGNORE_DETECT;
918 } else {
919 if (setup & WBSD_DAT3_H) {
920 setup &= ~WBSD_DAT3_H;
923 * We cannot resume card detection immediatly
924 * because of capacitance and delays in the chip.
926 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
929 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
932 * Store bus width for later. Will be used when
933 * setting up the data transfer.
935 host->bus_width = ios->bus_width;
937 spin_unlock_bh(&host->lock);
940 static int wbsd_get_ro(struct mmc_host *mmc)
942 struct wbsd_host *host = mmc_priv(mmc);
943 u8 csr;
945 spin_lock_bh(&host->lock);
947 csr = inb(host->base + WBSD_CSR);
948 csr |= WBSD_MSLED;
949 outb(csr, host->base + WBSD_CSR);
951 mdelay(1);
953 csr = inb(host->base + WBSD_CSR);
954 csr &= ~WBSD_MSLED;
955 outb(csr, host->base + WBSD_CSR);
957 spin_unlock_bh(&host->lock);
959 return csr & WBSD_WRPT;
962 static const struct mmc_host_ops wbsd_ops = {
963 .request = wbsd_request,
964 .set_ios = wbsd_set_ios,
965 .get_ro = wbsd_get_ro,
968 /*****************************************************************************\
970 * Interrupt handling *
972 \*****************************************************************************/
975 * Helper function to reset detection ignore
978 static void wbsd_reset_ignore(unsigned long data)
980 struct wbsd_host *host = (struct wbsd_host *)data;
982 BUG_ON(host == NULL);
984 DBG("Resetting card detection ignore\n");
986 spin_lock_bh(&host->lock);
988 host->flags &= ~WBSD_FIGNORE_DETECT;
991 * Card status might have changed during the
992 * blackout.
994 tasklet_schedule(&host->card_tasklet);
996 spin_unlock_bh(&host->lock);
1000 * Tasklets
1003 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1005 WARN_ON(!host->mrq);
1006 if (!host->mrq)
1007 return NULL;
1009 WARN_ON(!host->mrq->cmd);
1010 if (!host->mrq->cmd)
1011 return NULL;
1013 WARN_ON(!host->mrq->cmd->data);
1014 if (!host->mrq->cmd->data)
1015 return NULL;
1017 return host->mrq->cmd->data;
1020 static void wbsd_tasklet_card(unsigned long param)
1022 struct wbsd_host *host = (struct wbsd_host *)param;
1023 u8 csr;
1024 int delay = -1;
1026 spin_lock(&host->lock);
1028 if (host->flags & WBSD_FIGNORE_DETECT) {
1029 spin_unlock(&host->lock);
1030 return;
1033 csr = inb(host->base + WBSD_CSR);
1034 WARN_ON(csr == 0xff);
1036 if (csr & WBSD_CARDPRESENT) {
1037 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1038 DBG("Card inserted\n");
1039 host->flags |= WBSD_FCARD_PRESENT;
1041 delay = 500;
1043 } else if (host->flags & WBSD_FCARD_PRESENT) {
1044 DBG("Card removed\n");
1045 host->flags &= ~WBSD_FCARD_PRESENT;
1047 if (host->mrq) {
1048 printk(KERN_ERR "%s: Card removed during transfer!\n",
1049 mmc_hostname(host->mmc));
1050 wbsd_reset(host);
1052 host->mrq->cmd->error = MMC_ERR_FAILED;
1053 tasklet_schedule(&host->finish_tasklet);
1056 delay = 0;
1060 * Unlock first since we might get a call back.
1063 spin_unlock(&host->lock);
1065 if (delay != -1)
1066 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1069 static void wbsd_tasklet_fifo(unsigned long param)
1071 struct wbsd_host *host = (struct wbsd_host *)param;
1072 struct mmc_data *data;
1074 spin_lock(&host->lock);
1076 if (!host->mrq)
1077 goto end;
1079 data = wbsd_get_data(host);
1080 if (!data)
1081 goto end;
1083 if (data->flags & MMC_DATA_WRITE)
1084 wbsd_fill_fifo(host);
1085 else
1086 wbsd_empty_fifo(host);
1089 * Done?
1091 if (host->num_sg == 0) {
1092 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1093 tasklet_schedule(&host->finish_tasklet);
1096 end:
1097 spin_unlock(&host->lock);
1100 static void wbsd_tasklet_crc(unsigned long param)
1102 struct wbsd_host *host = (struct wbsd_host *)param;
1103 struct mmc_data *data;
1105 spin_lock(&host->lock);
1107 if (!host->mrq)
1108 goto end;
1110 data = wbsd_get_data(host);
1111 if (!data)
1112 goto end;
1114 DBGF("CRC error\n");
1116 data->error = MMC_ERR_BADCRC;
1118 tasklet_schedule(&host->finish_tasklet);
1120 end:
1121 spin_unlock(&host->lock);
1124 static void wbsd_tasklet_timeout(unsigned long param)
1126 struct wbsd_host *host = (struct wbsd_host *)param;
1127 struct mmc_data *data;
1129 spin_lock(&host->lock);
1131 if (!host->mrq)
1132 goto end;
1134 data = wbsd_get_data(host);
1135 if (!data)
1136 goto end;
1138 DBGF("Timeout\n");
1140 data->error = MMC_ERR_TIMEOUT;
1142 tasklet_schedule(&host->finish_tasklet);
1144 end:
1145 spin_unlock(&host->lock);
1148 static void wbsd_tasklet_finish(unsigned long param)
1150 struct wbsd_host *host = (struct wbsd_host *)param;
1151 struct mmc_data *data;
1153 spin_lock(&host->lock);
1155 WARN_ON(!host->mrq);
1156 if (!host->mrq)
1157 goto end;
1159 data = wbsd_get_data(host);
1160 if (!data)
1161 goto end;
1163 wbsd_finish_data(host, data);
1165 end:
1166 spin_unlock(&host->lock);
1169 static void wbsd_tasklet_block(unsigned long param)
1171 struct wbsd_host *host = (struct wbsd_host *)param;
1172 struct mmc_data *data;
1174 spin_lock(&host->lock);
1176 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1177 WBSD_CRC_OK) {
1178 data = wbsd_get_data(host);
1179 if (!data)
1180 goto end;
1182 DBGF("CRC error\n");
1184 data->error = MMC_ERR_BADCRC;
1186 tasklet_schedule(&host->finish_tasklet);
1189 end:
1190 spin_unlock(&host->lock);
1194 * Interrupt handling
1197 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1199 struct wbsd_host *host = dev_id;
1200 int isr;
1202 isr = inb(host->base + WBSD_ISR);
1205 * Was it actually our hardware that caused the interrupt?
1207 if (isr == 0xff || isr == 0x00)
1208 return IRQ_NONE;
1210 host->isr |= isr;
1213 * Schedule tasklets as needed.
1215 if (isr & WBSD_INT_CARD)
1216 tasklet_schedule(&host->card_tasklet);
1217 if (isr & WBSD_INT_FIFO_THRE)
1218 tasklet_schedule(&host->fifo_tasklet);
1219 if (isr & WBSD_INT_CRC)
1220 tasklet_hi_schedule(&host->crc_tasklet);
1221 if (isr & WBSD_INT_TIMEOUT)
1222 tasklet_hi_schedule(&host->timeout_tasklet);
1223 if (isr & WBSD_INT_BUSYEND)
1224 tasklet_hi_schedule(&host->block_tasklet);
1225 if (isr & WBSD_INT_TC)
1226 tasklet_schedule(&host->finish_tasklet);
1228 return IRQ_HANDLED;
1231 /*****************************************************************************\
1233 * Device initialisation and shutdown *
1235 \*****************************************************************************/
1238 * Allocate/free MMC structure.
1241 static int __devinit wbsd_alloc_mmc(struct device *dev)
1243 struct mmc_host *mmc;
1244 struct wbsd_host *host;
1247 * Allocate MMC structure.
1249 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1250 if (!mmc)
1251 return -ENOMEM;
1253 host = mmc_priv(mmc);
1254 host->mmc = mmc;
1256 host->dma = -1;
1259 * Set host parameters.
1261 mmc->ops = &wbsd_ops;
1262 mmc->f_min = 375000;
1263 mmc->f_max = 24000000;
1264 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1265 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1267 spin_lock_init(&host->lock);
1270 * Set up timers
1272 init_timer(&host->ignore_timer);
1273 host->ignore_timer.data = (unsigned long)host;
1274 host->ignore_timer.function = wbsd_reset_ignore;
1277 * Maximum number of segments. Worst case is one sector per segment
1278 * so this will be 64kB/512.
1280 mmc->max_hw_segs = 128;
1281 mmc->max_phys_segs = 128;
1284 * Maximum request size. Also limited by 64KiB buffer.
1286 mmc->max_req_size = 65536;
1289 * Maximum segment size. Could be one segment with the maximum number
1290 * of bytes.
1292 mmc->max_seg_size = mmc->max_req_size;
1295 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1296 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1298 mmc->max_blk_size = 4087;
1301 * Maximum block count. There is no real limit so the maximum
1302 * request size will be the only restriction.
1304 mmc->max_blk_count = mmc->max_req_size;
1306 dev_set_drvdata(dev, mmc);
1308 return 0;
1311 static void __devexit wbsd_free_mmc(struct device *dev)
1313 struct mmc_host *mmc;
1314 struct wbsd_host *host;
1316 mmc = dev_get_drvdata(dev);
1317 if (!mmc)
1318 return;
1320 host = mmc_priv(mmc);
1321 BUG_ON(host == NULL);
1323 del_timer_sync(&host->ignore_timer);
1325 mmc_free_host(mmc);
1327 dev_set_drvdata(dev, NULL);
1331 * Scan for known chip id:s
1334 static int __devinit wbsd_scan(struct wbsd_host *host)
1336 int i, j, k;
1337 int id;
1340 * Iterate through all ports, all codes to
1341 * find hardware that is in our known list.
1343 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1344 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1345 continue;
1347 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1348 id = 0xFFFF;
1350 host->config = config_ports[i];
1351 host->unlock_code = unlock_codes[j];
1353 wbsd_unlock_config(host);
1355 outb(WBSD_CONF_ID_HI, config_ports[i]);
1356 id = inb(config_ports[i] + 1) << 8;
1358 outb(WBSD_CONF_ID_LO, config_ports[i]);
1359 id |= inb(config_ports[i] + 1);
1361 wbsd_lock_config(host);
1363 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1364 if (id == valid_ids[k]) {
1365 host->chip_id = id;
1367 return 0;
1371 if (id != 0xFFFF) {
1372 DBG("Unknown hardware (id %x) found at %x\n",
1373 id, config_ports[i]);
1377 release_region(config_ports[i], 2);
1380 host->config = 0;
1381 host->unlock_code = 0;
1383 return -ENODEV;
1387 * Allocate/free io port ranges
1390 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1392 if (base & 0x7)
1393 return -EINVAL;
1395 if (!request_region(base, 8, DRIVER_NAME))
1396 return -EIO;
1398 host->base = base;
1400 return 0;
1403 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1405 if (host->base)
1406 release_region(host->base, 8);
1408 host->base = 0;
1410 if (host->config)
1411 release_region(host->config, 2);
1413 host->config = 0;
1417 * Allocate/free DMA port and buffer
1420 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1422 if (dma < 0)
1423 return;
1425 if (request_dma(dma, DRIVER_NAME))
1426 goto err;
1429 * We need to allocate a special buffer in
1430 * order for ISA to be able to DMA to it.
1432 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1433 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1434 if (!host->dma_buffer)
1435 goto free;
1438 * Translate the address to a physical address.
1440 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1441 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1444 * ISA DMA must be aligned on a 64k basis.
1446 if ((host->dma_addr & 0xffff) != 0)
1447 goto kfree;
1449 * ISA cannot access memory above 16 MB.
1451 else if (host->dma_addr >= 0x1000000)
1452 goto kfree;
1454 host->dma = dma;
1456 return;
1458 kfree:
1460 * If we've gotten here then there is some kind of alignment bug
1462 BUG_ON(1);
1464 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1465 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1466 host->dma_addr = (dma_addr_t)NULL;
1468 kfree(host->dma_buffer);
1469 host->dma_buffer = NULL;
1471 free:
1472 free_dma(dma);
1474 err:
1475 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1476 "Falling back on FIFO.\n", dma);
1479 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1481 if (host->dma_addr) {
1482 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1483 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1485 kfree(host->dma_buffer);
1486 if (host->dma >= 0)
1487 free_dma(host->dma);
1489 host->dma = -1;
1490 host->dma_buffer = NULL;
1491 host->dma_addr = (dma_addr_t)NULL;
1495 * Allocate/free IRQ.
1498 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1500 int ret;
1503 * Allocate interrupt.
1506 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1507 if (ret)
1508 return ret;
1510 host->irq = irq;
1513 * Set up tasklets.
1515 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1516 (unsigned long)host);
1517 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1518 (unsigned long)host);
1519 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1520 (unsigned long)host);
1521 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1522 (unsigned long)host);
1523 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1524 (unsigned long)host);
1525 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1526 (unsigned long)host);
1528 return 0;
1531 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1533 if (!host->irq)
1534 return;
1536 free_irq(host->irq, host);
1538 host->irq = 0;
1540 tasklet_kill(&host->card_tasklet);
1541 tasklet_kill(&host->fifo_tasklet);
1542 tasklet_kill(&host->crc_tasklet);
1543 tasklet_kill(&host->timeout_tasklet);
1544 tasklet_kill(&host->finish_tasklet);
1545 tasklet_kill(&host->block_tasklet);
1549 * Allocate all resources for the host.
1552 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1553 int base, int irq, int dma)
1555 int ret;
1558 * Allocate I/O ports.
1560 ret = wbsd_request_region(host, base);
1561 if (ret)
1562 return ret;
1565 * Allocate interrupt.
1567 ret = wbsd_request_irq(host, irq);
1568 if (ret)
1569 return ret;
1572 * Allocate DMA.
1574 wbsd_request_dma(host, dma);
1576 return 0;
1580 * Release all resources for the host.
1583 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1585 wbsd_release_dma(host);
1586 wbsd_release_irq(host);
1587 wbsd_release_regions(host);
1591 * Configure the resources the chip should use.
1594 static void wbsd_chip_config(struct wbsd_host *host)
1596 wbsd_unlock_config(host);
1599 * Reset the chip.
1601 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1602 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1605 * Select SD/MMC function.
1607 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1610 * Set up card detection.
1612 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1615 * Configure chip
1617 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1618 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1620 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1622 if (host->dma >= 0)
1623 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1626 * Enable and power up chip.
1628 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1629 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1631 wbsd_lock_config(host);
1635 * Check that configured resources are correct.
1638 static int wbsd_chip_validate(struct wbsd_host *host)
1640 int base, irq, dma;
1642 wbsd_unlock_config(host);
1645 * Select SD/MMC function.
1647 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1650 * Read configuration.
1652 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1653 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1655 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1657 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1659 wbsd_lock_config(host);
1662 * Validate against given configuration.
1664 if (base != host->base)
1665 return 0;
1666 if (irq != host->irq)
1667 return 0;
1668 if ((dma != host->dma) && (host->dma != -1))
1669 return 0;
1671 return 1;
1675 * Powers down the SD function
1678 static void wbsd_chip_poweroff(struct wbsd_host *host)
1680 wbsd_unlock_config(host);
1682 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1683 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1685 wbsd_lock_config(host);
1688 /*****************************************************************************\
1690 * Devices setup and shutdown *
1692 \*****************************************************************************/
1694 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1695 int pnp)
1697 struct wbsd_host *host = NULL;
1698 struct mmc_host *mmc = NULL;
1699 int ret;
1701 ret = wbsd_alloc_mmc(dev);
1702 if (ret)
1703 return ret;
1705 mmc = dev_get_drvdata(dev);
1706 host = mmc_priv(mmc);
1709 * Scan for hardware.
1711 ret = wbsd_scan(host);
1712 if (ret) {
1713 if (pnp && (ret == -ENODEV)) {
1714 printk(KERN_WARNING DRIVER_NAME
1715 ": Unable to confirm device presence. You may "
1716 "experience lock-ups.\n");
1717 } else {
1718 wbsd_free_mmc(dev);
1719 return ret;
1724 * Request resources.
1726 ret = wbsd_request_resources(host, base, irq, dma);
1727 if (ret) {
1728 wbsd_release_resources(host);
1729 wbsd_free_mmc(dev);
1730 return ret;
1734 * See if chip needs to be configured.
1736 if (pnp) {
1737 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1738 printk(KERN_WARNING DRIVER_NAME
1739 ": PnP active but chip not configured! "
1740 "You probably have a buggy BIOS. "
1741 "Configuring chip manually.\n");
1742 wbsd_chip_config(host);
1744 } else
1745 wbsd_chip_config(host);
1748 * Power Management stuff. No idea how this works.
1749 * Not tested.
1751 #ifdef CONFIG_PM
1752 if (host->config) {
1753 wbsd_unlock_config(host);
1754 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1755 wbsd_lock_config(host);
1757 #endif
1759 * Allow device to initialise itself properly.
1761 mdelay(5);
1764 * Reset the chip into a known state.
1766 wbsd_init_device(host);
1768 mmc_add_host(mmc);
1770 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1771 if (host->chip_id != 0)
1772 printk(" id %x", (int)host->chip_id);
1773 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1774 if (host->dma >= 0)
1775 printk(" dma %d", (int)host->dma);
1776 else
1777 printk(" FIFO");
1778 if (pnp)
1779 printk(" PnP");
1780 printk("\n");
1782 return 0;
1785 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1787 struct mmc_host *mmc = dev_get_drvdata(dev);
1788 struct wbsd_host *host;
1790 if (!mmc)
1791 return;
1793 host = mmc_priv(mmc);
1795 mmc_remove_host(mmc);
1798 * Power down the SD/MMC function.
1800 if (!pnp)
1801 wbsd_chip_poweroff(host);
1803 wbsd_release_resources(host);
1805 wbsd_free_mmc(dev);
1809 * Non-PnP
1812 static int __devinit wbsd_probe(struct platform_device *dev)
1814 /* Use the module parameters for resources */
1815 return wbsd_init(&dev->dev, io, irq, dma, 0);
1818 static int __devexit wbsd_remove(struct platform_device *dev)
1820 wbsd_shutdown(&dev->dev, 0);
1822 return 0;
1826 * PnP
1829 #ifdef CONFIG_PNP
1831 static int __devinit
1832 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1834 int io, irq, dma;
1837 * Get resources from PnP layer.
1839 io = pnp_port_start(pnpdev, 0);
1840 irq = pnp_irq(pnpdev, 0);
1841 if (pnp_dma_valid(pnpdev, 0))
1842 dma = pnp_dma(pnpdev, 0);
1843 else
1844 dma = -1;
1846 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1848 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1851 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1853 wbsd_shutdown(&dev->dev, 1);
1856 #endif /* CONFIG_PNP */
1859 * Power management
1862 #ifdef CONFIG_PM
1864 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1866 BUG_ON(host == NULL);
1868 return mmc_suspend_host(host->mmc, state);
1871 static int wbsd_resume(struct wbsd_host *host)
1873 BUG_ON(host == NULL);
1875 wbsd_init_device(host);
1877 return mmc_resume_host(host->mmc);
1880 static int wbsd_platform_suspend(struct platform_device *dev,
1881 pm_message_t state)
1883 struct mmc_host *mmc = platform_get_drvdata(dev);
1884 struct wbsd_host *host;
1885 int ret;
1887 if (mmc == NULL)
1888 return 0;
1890 DBGF("Suspending...\n");
1892 host = mmc_priv(mmc);
1894 ret = wbsd_suspend(host, state);
1895 if (ret)
1896 return ret;
1898 wbsd_chip_poweroff(host);
1900 return 0;
1903 static int wbsd_platform_resume(struct platform_device *dev)
1905 struct mmc_host *mmc = platform_get_drvdata(dev);
1906 struct wbsd_host *host;
1908 if (mmc == NULL)
1909 return 0;
1911 DBGF("Resuming...\n");
1913 host = mmc_priv(mmc);
1915 wbsd_chip_config(host);
1918 * Allow device to initialise itself properly.
1920 mdelay(5);
1922 return wbsd_resume(host);
1925 #ifdef CONFIG_PNP
1927 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1929 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1930 struct wbsd_host *host;
1932 if (mmc == NULL)
1933 return 0;
1935 DBGF("Suspending...\n");
1937 host = mmc_priv(mmc);
1939 return wbsd_suspend(host, state);
1942 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1944 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1945 struct wbsd_host *host;
1947 if (mmc == NULL)
1948 return 0;
1950 DBGF("Resuming...\n");
1952 host = mmc_priv(mmc);
1955 * See if chip needs to be configured.
1957 if (host->config != 0) {
1958 if (!wbsd_chip_validate(host)) {
1959 printk(KERN_WARNING DRIVER_NAME
1960 ": PnP active but chip not configured! "
1961 "You probably have a buggy BIOS. "
1962 "Configuring chip manually.\n");
1963 wbsd_chip_config(host);
1968 * Allow device to initialise itself properly.
1970 mdelay(5);
1972 return wbsd_resume(host);
1975 #endif /* CONFIG_PNP */
1977 #else /* CONFIG_PM */
1979 #define wbsd_platform_suspend NULL
1980 #define wbsd_platform_resume NULL
1982 #define wbsd_pnp_suspend NULL
1983 #define wbsd_pnp_resume NULL
1985 #endif /* CONFIG_PM */
1987 static struct platform_device *wbsd_device;
1989 static struct platform_driver wbsd_driver = {
1990 .probe = wbsd_probe,
1991 .remove = __devexit_p(wbsd_remove),
1993 .suspend = wbsd_platform_suspend,
1994 .resume = wbsd_platform_resume,
1995 .driver = {
1996 .name = DRIVER_NAME,
2000 #ifdef CONFIG_PNP
2002 static struct pnp_driver wbsd_pnp_driver = {
2003 .name = DRIVER_NAME,
2004 .id_table = pnp_dev_table,
2005 .probe = wbsd_pnp_probe,
2006 .remove = __devexit_p(wbsd_pnp_remove),
2008 .suspend = wbsd_pnp_suspend,
2009 .resume = wbsd_pnp_resume,
2012 #endif /* CONFIG_PNP */
2015 * Module loading/unloading
2018 static int __init wbsd_drv_init(void)
2020 int result;
2022 printk(KERN_INFO DRIVER_NAME
2023 ": Winbond W83L51xD SD/MMC card interface driver\n");
2024 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2026 #ifdef CONFIG_PNP
2028 if (!nopnp) {
2029 result = pnp_register_driver(&wbsd_pnp_driver);
2030 if (result < 0)
2031 return result;
2033 #endif /* CONFIG_PNP */
2035 if (nopnp) {
2036 result = platform_driver_register(&wbsd_driver);
2037 if (result < 0)
2038 return result;
2040 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2041 if (!wbsd_device) {
2042 platform_driver_unregister(&wbsd_driver);
2043 return -ENOMEM;
2046 result = platform_device_add(wbsd_device);
2047 if (result) {
2048 platform_device_put(wbsd_device);
2049 platform_driver_unregister(&wbsd_driver);
2050 return result;
2054 return 0;
2057 static void __exit wbsd_drv_exit(void)
2059 #ifdef CONFIG_PNP
2061 if (!nopnp)
2062 pnp_unregister_driver(&wbsd_pnp_driver);
2064 #endif /* CONFIG_PNP */
2066 if (nopnp) {
2067 platform_device_unregister(wbsd_device);
2069 platform_driver_unregister(&wbsd_driver);
2072 DBG("unloaded\n");
2075 module_init(wbsd_drv_init);
2076 module_exit(wbsd_drv_exit);
2077 #ifdef CONFIG_PNP
2078 module_param(nopnp, uint, 0444);
2079 #endif
2080 module_param(io, uint, 0444);
2081 module_param(irq, uint, 0444);
2082 module_param(dma, int, 0444);
2084 MODULE_LICENSE("GPL");
2085 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2086 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2088 #ifdef CONFIG_PNP
2089 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2090 #endif
2091 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2092 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2093 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");