[SCTP]: Fix sctp_getsockopt_local_addrs_old() to use local storage.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / wbsd.c
blob05ccfc43168fb2c173305bcb003c765e6eafd24a
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 dmabuf += sg[i].length;
348 if (size < sg[i].length)
349 size = 0;
350 else
351 size -= sg[i].length;
353 if (size == 0)
354 break;
358 * Check that we didn't get a request to transfer
359 * more data than can fit into the SG list.
362 BUG_ON(size != 0);
364 host->size -= size;
368 * Command handling
371 static inline void wbsd_get_short_reply(struct wbsd_host *host,
372 struct mmc_command *cmd)
375 * Correct response type?
377 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
378 cmd->error = MMC_ERR_INVALID;
379 return;
382 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
383 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
386 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
389 static inline void wbsd_get_long_reply(struct wbsd_host *host,
390 struct mmc_command *cmd)
392 int i;
395 * Correct response type?
397 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
398 cmd->error = MMC_ERR_INVALID;
399 return;
402 for (i = 0; i < 4; i++) {
403 cmd->resp[i] =
404 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
405 cmd->resp[i] |=
406 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
407 cmd->resp[i] |=
408 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
409 cmd->resp[i] |=
410 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
414 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
416 int i;
417 u8 status, isr;
419 DBGF("Sending cmd (%x)\n", cmd->opcode);
422 * Clear accumulated ISR. The interrupt routine
423 * will fill this one with events that occur during
424 * transfer.
426 host->isr = 0;
429 * Send the command (CRC calculated by host).
431 outb(cmd->opcode, host->base + WBSD_CMDR);
432 for (i = 3; i >= 0; i--)
433 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
435 cmd->error = MMC_ERR_NONE;
438 * Wait for the request to complete.
440 do {
441 status = wbsd_read_index(host, WBSD_IDX_STATUS);
442 } while (status & WBSD_CARDTRAFFIC);
445 * Do we expect a reply?
447 if (cmd->flags & MMC_RSP_PRESENT) {
449 * Read back status.
451 isr = host->isr;
453 /* Card removed? */
454 if (isr & WBSD_INT_CARD)
455 cmd->error = MMC_ERR_TIMEOUT;
456 /* Timeout? */
457 else if (isr & WBSD_INT_TIMEOUT)
458 cmd->error = MMC_ERR_TIMEOUT;
459 /* CRC? */
460 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
461 cmd->error = MMC_ERR_BADCRC;
462 /* All ok */
463 else {
464 if (cmd->flags & MMC_RSP_136)
465 wbsd_get_long_reply(host, cmd);
466 else
467 wbsd_get_short_reply(host, cmd);
471 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
475 * Data functions
478 static void wbsd_empty_fifo(struct wbsd_host *host)
480 struct mmc_data *data = host->mrq->cmd->data;
481 char *buffer;
482 int i, fsr, fifo;
485 * Handle excessive data.
487 if (data->bytes_xfered == host->size)
488 return;
490 buffer = wbsd_sg_to_buffer(host) + host->offset;
493 * Drain the fifo. This has a tendency to loop longer
494 * than the FIFO length (usually one block).
496 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
498 * The size field in the FSR is broken so we have to
499 * do some guessing.
501 if (fsr & WBSD_FIFO_FULL)
502 fifo = 16;
503 else if (fsr & WBSD_FIFO_FUTHRE)
504 fifo = 8;
505 else
506 fifo = 1;
508 for (i = 0; i < fifo; i++) {
509 *buffer = inb(host->base + WBSD_DFR);
510 buffer++;
511 host->offset++;
512 host->remain--;
514 data->bytes_xfered++;
517 * Transfer done?
519 if (data->bytes_xfered == host->size)
520 return;
523 * End of scatter list entry?
525 if (host->remain == 0) {
527 * Get next entry. Check if last.
529 if (!wbsd_next_sg(host)) {
531 * We should never reach this point.
532 * It means that we're trying to
533 * transfer more blocks than can fit
534 * into the scatter list.
536 BUG_ON(1);
538 host->size = data->bytes_xfered;
540 return;
543 buffer = wbsd_sg_to_buffer(host);
549 * This is a very dirty hack to solve a
550 * hardware problem. The chip doesn't trigger
551 * FIFO threshold interrupts properly.
553 if ((host->size - data->bytes_xfered) < 16)
554 tasklet_schedule(&host->fifo_tasklet);
557 static void wbsd_fill_fifo(struct wbsd_host *host)
559 struct mmc_data *data = host->mrq->cmd->data;
560 char *buffer;
561 int i, fsr, fifo;
564 * Check that we aren't being called after the
565 * entire buffer has been transfered.
567 if (data->bytes_xfered == host->size)
568 return;
570 buffer = wbsd_sg_to_buffer(host) + host->offset;
573 * Fill the fifo. This has a tendency to loop longer
574 * than the FIFO length (usually one block).
576 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
578 * The size field in the FSR is broken so we have to
579 * do some guessing.
581 if (fsr & WBSD_FIFO_EMPTY)
582 fifo = 0;
583 else if (fsr & WBSD_FIFO_EMTHRE)
584 fifo = 8;
585 else
586 fifo = 15;
588 for (i = 16; i > fifo; i--) {
589 outb(*buffer, host->base + WBSD_DFR);
590 buffer++;
591 host->offset++;
592 host->remain--;
594 data->bytes_xfered++;
597 * Transfer done?
599 if (data->bytes_xfered == host->size)
600 return;
603 * End of scatter list entry?
605 if (host->remain == 0) {
607 * Get next entry. Check if last.
609 if (!wbsd_next_sg(host)) {
611 * We should never reach this point.
612 * It means that we're trying to
613 * transfer more blocks than can fit
614 * into the scatter list.
616 BUG_ON(1);
618 host->size = data->bytes_xfered;
620 return;
623 buffer = wbsd_sg_to_buffer(host);
629 * The controller stops sending interrupts for
630 * 'FIFO empty' under certain conditions. So we
631 * need to be a bit more pro-active.
633 tasklet_schedule(&host->fifo_tasklet);
636 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
638 u16 blksize;
639 u8 setup;
640 unsigned long dmaflags;
642 DBGF("blksz %04x blks %04x flags %08x\n",
643 data->blksz, data->blocks, data->flags);
644 DBGF("tsac %d ms nsac %d clk\n",
645 data->timeout_ns / 1000000, data->timeout_clks);
648 * Calculate size.
650 host->size = data->blocks * data->blksz;
653 * Check timeout values for overflow.
654 * (Yes, some cards cause this value to overflow).
656 if (data->timeout_ns > 127000000)
657 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
658 else {
659 wbsd_write_index(host, WBSD_IDX_TAAC,
660 data->timeout_ns / 1000000);
663 if (data->timeout_clks > 255)
664 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
665 else
666 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
669 * Inform the chip of how large blocks will be
670 * sent. It needs this to determine when to
671 * calculate CRC.
673 * Space for CRC must be included in the size.
674 * Two bytes are needed for each data line.
676 if (host->bus_width == MMC_BUS_WIDTH_1) {
677 blksize = data->blksz + 2;
679 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
680 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
681 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
682 blksize = data->blksz + 2 * 4;
684 wbsd_write_index(host, WBSD_IDX_PBSMSB,
685 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
686 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
687 } else {
688 data->error = MMC_ERR_INVALID;
689 return;
693 * Clear the FIFO. This is needed even for DMA
694 * transfers since the chip still uses the FIFO
695 * internally.
697 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
698 setup |= WBSD_FIFO_RESET;
699 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
702 * DMA transfer?
704 if (host->dma >= 0) {
706 * The buffer for DMA is only 64 kB.
708 BUG_ON(host->size > 0x10000);
709 if (host->size > 0x10000) {
710 data->error = MMC_ERR_INVALID;
711 return;
715 * Transfer data from the SG list to
716 * the DMA buffer.
718 if (data->flags & MMC_DATA_WRITE)
719 wbsd_sg_to_dma(host, data);
722 * Initialise the ISA DMA controller.
724 dmaflags = claim_dma_lock();
725 disable_dma(host->dma);
726 clear_dma_ff(host->dma);
727 if (data->flags & MMC_DATA_READ)
728 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
729 else
730 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
731 set_dma_addr(host->dma, host->dma_addr);
732 set_dma_count(host->dma, host->size);
734 enable_dma(host->dma);
735 release_dma_lock(dmaflags);
738 * Enable DMA on the host.
740 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
741 } else {
743 * This flag is used to keep printk
744 * output to a minimum.
746 host->firsterr = 1;
749 * Initialise the SG list.
751 wbsd_init_sg(host, data);
754 * Turn off DMA.
756 wbsd_write_index(host, WBSD_IDX_DMA, 0);
759 * Set up FIFO threshold levels (and fill
760 * buffer if doing a write).
762 if (data->flags & MMC_DATA_READ) {
763 wbsd_write_index(host, WBSD_IDX_FIFOEN,
764 WBSD_FIFOEN_FULL | 8);
765 } else {
766 wbsd_write_index(host, WBSD_IDX_FIFOEN,
767 WBSD_FIFOEN_EMPTY | 8);
768 wbsd_fill_fifo(host);
772 data->error = MMC_ERR_NONE;
775 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
777 unsigned long dmaflags;
778 int count;
779 u8 status;
781 WARN_ON(host->mrq == NULL);
784 * Send a stop command if needed.
786 if (data->stop)
787 wbsd_send_command(host, data->stop);
790 * Wait for the controller to leave data
791 * transfer state.
793 do {
794 status = wbsd_read_index(host, WBSD_IDX_STATUS);
795 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
798 * DMA transfer?
800 if (host->dma >= 0) {
802 * Disable DMA on the host.
804 wbsd_write_index(host, WBSD_IDX_DMA, 0);
807 * Turn of ISA DMA controller.
809 dmaflags = claim_dma_lock();
810 disable_dma(host->dma);
811 clear_dma_ff(host->dma);
812 count = get_dma_residue(host->dma);
813 release_dma_lock(dmaflags);
816 * Any leftover data?
818 if (count) {
819 printk(KERN_ERR "%s: Incomplete DMA transfer. "
820 "%d bytes left.\n",
821 mmc_hostname(host->mmc), count);
823 data->error = MMC_ERR_FAILED;
824 } else {
826 * Transfer data from DMA buffer to
827 * SG list.
829 if (data->flags & MMC_DATA_READ)
830 wbsd_dma_to_sg(host, data);
832 data->bytes_xfered = host->size;
836 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
838 wbsd_request_end(host, host->mrq);
841 /*****************************************************************************\
843 * MMC layer callbacks *
845 \*****************************************************************************/
847 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
849 struct wbsd_host *host = mmc_priv(mmc);
850 struct mmc_command *cmd;
853 * Disable tasklets to avoid a deadlock.
855 spin_lock_bh(&host->lock);
857 BUG_ON(host->mrq != NULL);
859 cmd = mrq->cmd;
861 host->mrq = mrq;
864 * If there is no card in the slot then
865 * timeout immediatly.
867 if (!(host->flags & WBSD_FCARD_PRESENT)) {
868 cmd->error = MMC_ERR_TIMEOUT;
869 goto done;
873 * Does the request include data?
875 if (cmd->data) {
876 wbsd_prepare_data(host, cmd->data);
878 if (cmd->data->error != MMC_ERR_NONE)
879 goto done;
882 wbsd_send_command(host, cmd);
885 * If this is a data transfer the request
886 * will be finished after the data has
887 * transfered.
889 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
891 * The hardware is so delightfully stupid that it has a list
892 * of "data" commands. If a command isn't on this list, it'll
893 * just go back to the idle state and won't send any data
894 * interrupts.
896 switch (cmd->opcode) {
897 case 11:
898 case 17:
899 case 18:
900 case 20:
901 case 24:
902 case 25:
903 case 26:
904 case 27:
905 case 30:
906 case 42:
907 case 56:
908 break;
910 /* ACMDs. We don't keep track of state, so we just treat them
911 * like any other command. */
912 case 51:
913 break;
915 default:
916 #ifdef CONFIG_MMC_DEBUG
917 printk(KERN_WARNING "%s: Data command %d is not "
918 "supported by this controller.\n",
919 mmc_hostname(host->mmc), cmd->opcode);
920 #endif
921 cmd->data->error = MMC_ERR_INVALID;
923 if (cmd->data->stop)
924 wbsd_send_command(host, cmd->data->stop);
926 goto done;
930 * Dirty fix for hardware bug.
932 if (host->dma == -1)
933 tasklet_schedule(&host->fifo_tasklet);
935 spin_unlock_bh(&host->lock);
937 return;
940 done:
941 wbsd_request_end(host, mrq);
943 spin_unlock_bh(&host->lock);
946 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
948 struct wbsd_host *host = mmc_priv(mmc);
949 u8 clk, setup, pwr;
951 spin_lock_bh(&host->lock);
954 * Reset the chip on each power off.
955 * Should clear out any weird states.
957 if (ios->power_mode == MMC_POWER_OFF)
958 wbsd_init_device(host);
960 if (ios->clock >= 24000000)
961 clk = WBSD_CLK_24M;
962 else if (ios->clock >= 16000000)
963 clk = WBSD_CLK_16M;
964 else if (ios->clock >= 12000000)
965 clk = WBSD_CLK_12M;
966 else
967 clk = WBSD_CLK_375K;
970 * Only write to the clock register when
971 * there is an actual change.
973 if (clk != host->clk) {
974 wbsd_write_index(host, WBSD_IDX_CLK, clk);
975 host->clk = clk;
979 * Power up card.
981 if (ios->power_mode != MMC_POWER_OFF) {
982 pwr = inb(host->base + WBSD_CSR);
983 pwr &= ~WBSD_POWER_N;
984 outb(pwr, host->base + WBSD_CSR);
988 * MMC cards need to have pin 1 high during init.
989 * It wreaks havoc with the card detection though so
990 * that needs to be disabled.
992 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
993 if (ios->chip_select == MMC_CS_HIGH) {
994 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
995 setup |= WBSD_DAT3_H;
996 host->flags |= WBSD_FIGNORE_DETECT;
997 } else {
998 if (setup & WBSD_DAT3_H) {
999 setup &= ~WBSD_DAT3_H;
1002 * We cannot resume card detection immediatly
1003 * because of capacitance and delays in the chip.
1005 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1008 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1011 * Store bus width for later. Will be used when
1012 * setting up the data transfer.
1014 host->bus_width = ios->bus_width;
1016 spin_unlock_bh(&host->lock);
1019 static int wbsd_get_ro(struct mmc_host *mmc)
1021 struct wbsd_host *host = mmc_priv(mmc);
1022 u8 csr;
1024 spin_lock_bh(&host->lock);
1026 csr = inb(host->base + WBSD_CSR);
1027 csr |= WBSD_MSLED;
1028 outb(csr, host->base + WBSD_CSR);
1030 mdelay(1);
1032 csr = inb(host->base + WBSD_CSR);
1033 csr &= ~WBSD_MSLED;
1034 outb(csr, host->base + WBSD_CSR);
1036 spin_unlock_bh(&host->lock);
1038 return csr & WBSD_WRPT;
1041 static const struct mmc_host_ops wbsd_ops = {
1042 .request = wbsd_request,
1043 .set_ios = wbsd_set_ios,
1044 .get_ro = wbsd_get_ro,
1047 /*****************************************************************************\
1049 * Interrupt handling *
1051 \*****************************************************************************/
1054 * Helper function to reset detection ignore
1057 static void wbsd_reset_ignore(unsigned long data)
1059 struct wbsd_host *host = (struct wbsd_host *)data;
1061 BUG_ON(host == NULL);
1063 DBG("Resetting card detection ignore\n");
1065 spin_lock_bh(&host->lock);
1067 host->flags &= ~WBSD_FIGNORE_DETECT;
1070 * Card status might have changed during the
1071 * blackout.
1073 tasklet_schedule(&host->card_tasklet);
1075 spin_unlock_bh(&host->lock);
1079 * Tasklets
1082 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1084 WARN_ON(!host->mrq);
1085 if (!host->mrq)
1086 return NULL;
1088 WARN_ON(!host->mrq->cmd);
1089 if (!host->mrq->cmd)
1090 return NULL;
1092 WARN_ON(!host->mrq->cmd->data);
1093 if (!host->mrq->cmd->data)
1094 return NULL;
1096 return host->mrq->cmd->data;
1099 static void wbsd_tasklet_card(unsigned long param)
1101 struct wbsd_host *host = (struct wbsd_host *)param;
1102 u8 csr;
1103 int delay = -1;
1105 spin_lock(&host->lock);
1107 if (host->flags & WBSD_FIGNORE_DETECT) {
1108 spin_unlock(&host->lock);
1109 return;
1112 csr = inb(host->base + WBSD_CSR);
1113 WARN_ON(csr == 0xff);
1115 if (csr & WBSD_CARDPRESENT) {
1116 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1117 DBG("Card inserted\n");
1118 host->flags |= WBSD_FCARD_PRESENT;
1120 delay = 500;
1122 } else if (host->flags & WBSD_FCARD_PRESENT) {
1123 DBG("Card removed\n");
1124 host->flags &= ~WBSD_FCARD_PRESENT;
1126 if (host->mrq) {
1127 printk(KERN_ERR "%s: Card removed during transfer!\n",
1128 mmc_hostname(host->mmc));
1129 wbsd_reset(host);
1131 host->mrq->cmd->error = MMC_ERR_FAILED;
1132 tasklet_schedule(&host->finish_tasklet);
1135 delay = 0;
1139 * Unlock first since we might get a call back.
1142 spin_unlock(&host->lock);
1144 if (delay != -1)
1145 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1148 static void wbsd_tasklet_fifo(unsigned long param)
1150 struct wbsd_host *host = (struct wbsd_host *)param;
1151 struct mmc_data *data;
1153 spin_lock(&host->lock);
1155 if (!host->mrq)
1156 goto end;
1158 data = wbsd_get_data(host);
1159 if (!data)
1160 goto end;
1162 if (data->flags & MMC_DATA_WRITE)
1163 wbsd_fill_fifo(host);
1164 else
1165 wbsd_empty_fifo(host);
1168 * Done?
1170 if (host->size == data->bytes_xfered) {
1171 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1172 tasklet_schedule(&host->finish_tasklet);
1175 end:
1176 spin_unlock(&host->lock);
1179 static void wbsd_tasklet_crc(unsigned long param)
1181 struct wbsd_host *host = (struct wbsd_host *)param;
1182 struct mmc_data *data;
1184 spin_lock(&host->lock);
1186 if (!host->mrq)
1187 goto end;
1189 data = wbsd_get_data(host);
1190 if (!data)
1191 goto end;
1193 DBGF("CRC error\n");
1195 data->error = MMC_ERR_BADCRC;
1197 tasklet_schedule(&host->finish_tasklet);
1199 end:
1200 spin_unlock(&host->lock);
1203 static void wbsd_tasklet_timeout(unsigned long param)
1205 struct wbsd_host *host = (struct wbsd_host *)param;
1206 struct mmc_data *data;
1208 spin_lock(&host->lock);
1210 if (!host->mrq)
1211 goto end;
1213 data = wbsd_get_data(host);
1214 if (!data)
1215 goto end;
1217 DBGF("Timeout\n");
1219 data->error = MMC_ERR_TIMEOUT;
1221 tasklet_schedule(&host->finish_tasklet);
1223 end:
1224 spin_unlock(&host->lock);
1227 static void wbsd_tasklet_finish(unsigned long param)
1229 struct wbsd_host *host = (struct wbsd_host *)param;
1230 struct mmc_data *data;
1232 spin_lock(&host->lock);
1234 WARN_ON(!host->mrq);
1235 if (!host->mrq)
1236 goto end;
1238 data = wbsd_get_data(host);
1239 if (!data)
1240 goto end;
1242 wbsd_finish_data(host, data);
1244 end:
1245 spin_unlock(&host->lock);
1248 static void wbsd_tasklet_block(unsigned long param)
1250 struct wbsd_host *host = (struct wbsd_host *)param;
1251 struct mmc_data *data;
1253 spin_lock(&host->lock);
1255 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1256 WBSD_CRC_OK) {
1257 data = wbsd_get_data(host);
1258 if (!data)
1259 goto end;
1261 DBGF("CRC error\n");
1263 data->error = MMC_ERR_BADCRC;
1265 tasklet_schedule(&host->finish_tasklet);
1268 end:
1269 spin_unlock(&host->lock);
1273 * Interrupt handling
1276 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1278 struct wbsd_host *host = dev_id;
1279 int isr;
1281 isr = inb(host->base + WBSD_ISR);
1284 * Was it actually our hardware that caused the interrupt?
1286 if (isr == 0xff || isr == 0x00)
1287 return IRQ_NONE;
1289 host->isr |= isr;
1292 * Schedule tasklets as needed.
1294 if (isr & WBSD_INT_CARD)
1295 tasklet_schedule(&host->card_tasklet);
1296 if (isr & WBSD_INT_FIFO_THRE)
1297 tasklet_schedule(&host->fifo_tasklet);
1298 if (isr & WBSD_INT_CRC)
1299 tasklet_hi_schedule(&host->crc_tasklet);
1300 if (isr & WBSD_INT_TIMEOUT)
1301 tasklet_hi_schedule(&host->timeout_tasklet);
1302 if (isr & WBSD_INT_BUSYEND)
1303 tasklet_hi_schedule(&host->block_tasklet);
1304 if (isr & WBSD_INT_TC)
1305 tasklet_schedule(&host->finish_tasklet);
1307 return IRQ_HANDLED;
1310 /*****************************************************************************\
1312 * Device initialisation and shutdown *
1314 \*****************************************************************************/
1317 * Allocate/free MMC structure.
1320 static int __devinit wbsd_alloc_mmc(struct device *dev)
1322 struct mmc_host *mmc;
1323 struct wbsd_host *host;
1326 * Allocate MMC structure.
1328 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1329 if (!mmc)
1330 return -ENOMEM;
1332 host = mmc_priv(mmc);
1333 host->mmc = mmc;
1335 host->dma = -1;
1338 * Set host parameters.
1340 mmc->ops = &wbsd_ops;
1341 mmc->f_min = 375000;
1342 mmc->f_max = 24000000;
1343 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1344 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1346 spin_lock_init(&host->lock);
1349 * Set up timers
1351 init_timer(&host->ignore_timer);
1352 host->ignore_timer.data = (unsigned long)host;
1353 host->ignore_timer.function = wbsd_reset_ignore;
1356 * Maximum number of segments. Worst case is one sector per segment
1357 * so this will be 64kB/512.
1359 mmc->max_hw_segs = 128;
1360 mmc->max_phys_segs = 128;
1363 * Maximum request size. Also limited by 64KiB buffer.
1365 mmc->max_req_size = 65536;
1368 * Maximum segment size. Could be one segment with the maximum number
1369 * of bytes.
1371 mmc->max_seg_size = mmc->max_req_size;
1374 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1375 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1377 mmc->max_blk_size = 4087;
1380 * Maximum block count. There is no real limit so the maximum
1381 * request size will be the only restriction.
1383 mmc->max_blk_count = mmc->max_req_size;
1385 dev_set_drvdata(dev, mmc);
1387 return 0;
1390 static void __devexit wbsd_free_mmc(struct device *dev)
1392 struct mmc_host *mmc;
1393 struct wbsd_host *host;
1395 mmc = dev_get_drvdata(dev);
1396 if (!mmc)
1397 return;
1399 host = mmc_priv(mmc);
1400 BUG_ON(host == NULL);
1402 del_timer_sync(&host->ignore_timer);
1404 mmc_free_host(mmc);
1406 dev_set_drvdata(dev, NULL);
1410 * Scan for known chip id:s
1413 static int __devinit wbsd_scan(struct wbsd_host *host)
1415 int i, j, k;
1416 int id;
1419 * Iterate through all ports, all codes to
1420 * find hardware that is in our known list.
1422 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1423 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1424 continue;
1426 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1427 id = 0xFFFF;
1429 host->config = config_ports[i];
1430 host->unlock_code = unlock_codes[j];
1432 wbsd_unlock_config(host);
1434 outb(WBSD_CONF_ID_HI, config_ports[i]);
1435 id = inb(config_ports[i] + 1) << 8;
1437 outb(WBSD_CONF_ID_LO, config_ports[i]);
1438 id |= inb(config_ports[i] + 1);
1440 wbsd_lock_config(host);
1442 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1443 if (id == valid_ids[k]) {
1444 host->chip_id = id;
1446 return 0;
1450 if (id != 0xFFFF) {
1451 DBG("Unknown hardware (id %x) found at %x\n",
1452 id, config_ports[i]);
1456 release_region(config_ports[i], 2);
1459 host->config = 0;
1460 host->unlock_code = 0;
1462 return -ENODEV;
1466 * Allocate/free io port ranges
1469 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1471 if (base & 0x7)
1472 return -EINVAL;
1474 if (!request_region(base, 8, DRIVER_NAME))
1475 return -EIO;
1477 host->base = base;
1479 return 0;
1482 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1484 if (host->base)
1485 release_region(host->base, 8);
1487 host->base = 0;
1489 if (host->config)
1490 release_region(host->config, 2);
1492 host->config = 0;
1496 * Allocate/free DMA port and buffer
1499 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1501 if (dma < 0)
1502 return;
1504 if (request_dma(dma, DRIVER_NAME))
1505 goto err;
1508 * We need to allocate a special buffer in
1509 * order for ISA to be able to DMA to it.
1511 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1512 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1513 if (!host->dma_buffer)
1514 goto free;
1517 * Translate the address to a physical address.
1519 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1520 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1523 * ISA DMA must be aligned on a 64k basis.
1525 if ((host->dma_addr & 0xffff) != 0)
1526 goto kfree;
1528 * ISA cannot access memory above 16 MB.
1530 else if (host->dma_addr >= 0x1000000)
1531 goto kfree;
1533 host->dma = dma;
1535 return;
1537 kfree:
1539 * If we've gotten here then there is some kind of alignment bug
1541 BUG_ON(1);
1543 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1544 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1545 host->dma_addr = (dma_addr_t)NULL;
1547 kfree(host->dma_buffer);
1548 host->dma_buffer = NULL;
1550 free:
1551 free_dma(dma);
1553 err:
1554 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1555 "Falling back on FIFO.\n", dma);
1558 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1560 if (host->dma_addr) {
1561 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1562 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1564 kfree(host->dma_buffer);
1565 if (host->dma >= 0)
1566 free_dma(host->dma);
1568 host->dma = -1;
1569 host->dma_buffer = NULL;
1570 host->dma_addr = (dma_addr_t)NULL;
1574 * Allocate/free IRQ.
1577 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1579 int ret;
1582 * Allocate interrupt.
1585 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1586 if (ret)
1587 return ret;
1589 host->irq = irq;
1592 * Set up tasklets.
1594 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1595 (unsigned long)host);
1596 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1597 (unsigned long)host);
1598 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1599 (unsigned long)host);
1600 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1601 (unsigned long)host);
1602 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1603 (unsigned long)host);
1604 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1605 (unsigned long)host);
1607 return 0;
1610 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1612 if (!host->irq)
1613 return;
1615 free_irq(host->irq, host);
1617 host->irq = 0;
1619 tasklet_kill(&host->card_tasklet);
1620 tasklet_kill(&host->fifo_tasklet);
1621 tasklet_kill(&host->crc_tasklet);
1622 tasklet_kill(&host->timeout_tasklet);
1623 tasklet_kill(&host->finish_tasklet);
1624 tasklet_kill(&host->block_tasklet);
1628 * Allocate all resources for the host.
1631 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1632 int base, int irq, int dma)
1634 int ret;
1637 * Allocate I/O ports.
1639 ret = wbsd_request_region(host, base);
1640 if (ret)
1641 return ret;
1644 * Allocate interrupt.
1646 ret = wbsd_request_irq(host, irq);
1647 if (ret)
1648 return ret;
1651 * Allocate DMA.
1653 wbsd_request_dma(host, dma);
1655 return 0;
1659 * Release all resources for the host.
1662 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1664 wbsd_release_dma(host);
1665 wbsd_release_irq(host);
1666 wbsd_release_regions(host);
1670 * Configure the resources the chip should use.
1673 static void wbsd_chip_config(struct wbsd_host *host)
1675 wbsd_unlock_config(host);
1678 * Reset the chip.
1680 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1681 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1684 * Select SD/MMC function.
1686 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1689 * Set up card detection.
1691 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1694 * Configure chip
1696 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1697 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1699 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1701 if (host->dma >= 0)
1702 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1705 * Enable and power up chip.
1707 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1708 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1710 wbsd_lock_config(host);
1714 * Check that configured resources are correct.
1717 static int wbsd_chip_validate(struct wbsd_host *host)
1719 int base, irq, dma;
1721 wbsd_unlock_config(host);
1724 * Select SD/MMC function.
1726 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1729 * Read configuration.
1731 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1732 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1734 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1736 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1738 wbsd_lock_config(host);
1741 * Validate against given configuration.
1743 if (base != host->base)
1744 return 0;
1745 if (irq != host->irq)
1746 return 0;
1747 if ((dma != host->dma) && (host->dma != -1))
1748 return 0;
1750 return 1;
1754 * Powers down the SD function
1757 static void wbsd_chip_poweroff(struct wbsd_host *host)
1759 wbsd_unlock_config(host);
1761 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1762 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1764 wbsd_lock_config(host);
1767 /*****************************************************************************\
1769 * Devices setup and shutdown *
1771 \*****************************************************************************/
1773 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1774 int pnp)
1776 struct wbsd_host *host = NULL;
1777 struct mmc_host *mmc = NULL;
1778 int ret;
1780 ret = wbsd_alloc_mmc(dev);
1781 if (ret)
1782 return ret;
1784 mmc = dev_get_drvdata(dev);
1785 host = mmc_priv(mmc);
1788 * Scan for hardware.
1790 ret = wbsd_scan(host);
1791 if (ret) {
1792 if (pnp && (ret == -ENODEV)) {
1793 printk(KERN_WARNING DRIVER_NAME
1794 ": Unable to confirm device presence. You may "
1795 "experience lock-ups.\n");
1796 } else {
1797 wbsd_free_mmc(dev);
1798 return ret;
1803 * Request resources.
1805 ret = wbsd_request_resources(host, base, irq, dma);
1806 if (ret) {
1807 wbsd_release_resources(host);
1808 wbsd_free_mmc(dev);
1809 return ret;
1813 * See if chip needs to be configured.
1815 if (pnp) {
1816 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1817 printk(KERN_WARNING DRIVER_NAME
1818 ": PnP active but chip not configured! "
1819 "You probably have a buggy BIOS. "
1820 "Configuring chip manually.\n");
1821 wbsd_chip_config(host);
1823 } else
1824 wbsd_chip_config(host);
1827 * Power Management stuff. No idea how this works.
1828 * Not tested.
1830 #ifdef CONFIG_PM
1831 if (host->config) {
1832 wbsd_unlock_config(host);
1833 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1834 wbsd_lock_config(host);
1836 #endif
1838 * Allow device to initialise itself properly.
1840 mdelay(5);
1843 * Reset the chip into a known state.
1845 wbsd_init_device(host);
1847 mmc_add_host(mmc);
1849 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1850 if (host->chip_id != 0)
1851 printk(" id %x", (int)host->chip_id);
1852 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1853 if (host->dma >= 0)
1854 printk(" dma %d", (int)host->dma);
1855 else
1856 printk(" FIFO");
1857 if (pnp)
1858 printk(" PnP");
1859 printk("\n");
1861 return 0;
1864 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1866 struct mmc_host *mmc = dev_get_drvdata(dev);
1867 struct wbsd_host *host;
1869 if (!mmc)
1870 return;
1872 host = mmc_priv(mmc);
1874 mmc_remove_host(mmc);
1877 * Power down the SD/MMC function.
1879 if (!pnp)
1880 wbsd_chip_poweroff(host);
1882 wbsd_release_resources(host);
1884 wbsd_free_mmc(dev);
1888 * Non-PnP
1891 static int __devinit wbsd_probe(struct platform_device *dev)
1893 /* Use the module parameters for resources */
1894 return wbsd_init(&dev->dev, io, irq, dma, 0);
1897 static int __devexit wbsd_remove(struct platform_device *dev)
1899 wbsd_shutdown(&dev->dev, 0);
1901 return 0;
1905 * PnP
1908 #ifdef CONFIG_PNP
1910 static int __devinit
1911 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1913 int io, irq, dma;
1916 * Get resources from PnP layer.
1918 io = pnp_port_start(pnpdev, 0);
1919 irq = pnp_irq(pnpdev, 0);
1920 if (pnp_dma_valid(pnpdev, 0))
1921 dma = pnp_dma(pnpdev, 0);
1922 else
1923 dma = -1;
1925 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1927 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1930 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1932 wbsd_shutdown(&dev->dev, 1);
1935 #endif /* CONFIG_PNP */
1938 * Power management
1941 #ifdef CONFIG_PM
1943 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1945 BUG_ON(host == NULL);
1947 return mmc_suspend_host(host->mmc, state);
1950 static int wbsd_resume(struct wbsd_host *host)
1952 BUG_ON(host == NULL);
1954 wbsd_init_device(host);
1956 return mmc_resume_host(host->mmc);
1959 static int wbsd_platform_suspend(struct platform_device *dev,
1960 pm_message_t state)
1962 struct mmc_host *mmc = platform_get_drvdata(dev);
1963 struct wbsd_host *host;
1964 int ret;
1966 if (mmc == NULL)
1967 return 0;
1969 DBGF("Suspending...\n");
1971 host = mmc_priv(mmc);
1973 ret = wbsd_suspend(host, state);
1974 if (ret)
1975 return ret;
1977 wbsd_chip_poweroff(host);
1979 return 0;
1982 static int wbsd_platform_resume(struct platform_device *dev)
1984 struct mmc_host *mmc = platform_get_drvdata(dev);
1985 struct wbsd_host *host;
1987 if (mmc == NULL)
1988 return 0;
1990 DBGF("Resuming...\n");
1992 host = mmc_priv(mmc);
1994 wbsd_chip_config(host);
1997 * Allow device to initialise itself properly.
1999 mdelay(5);
2001 return wbsd_resume(host);
2004 #ifdef CONFIG_PNP
2006 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2008 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2009 struct wbsd_host *host;
2011 if (mmc == NULL)
2012 return 0;
2014 DBGF("Suspending...\n");
2016 host = mmc_priv(mmc);
2018 return wbsd_suspend(host, state);
2021 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2023 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2024 struct wbsd_host *host;
2026 if (mmc == NULL)
2027 return 0;
2029 DBGF("Resuming...\n");
2031 host = mmc_priv(mmc);
2034 * See if chip needs to be configured.
2036 if (host->config != 0) {
2037 if (!wbsd_chip_validate(host)) {
2038 printk(KERN_WARNING DRIVER_NAME
2039 ": PnP active but chip not configured! "
2040 "You probably have a buggy BIOS. "
2041 "Configuring chip manually.\n");
2042 wbsd_chip_config(host);
2047 * Allow device to initialise itself properly.
2049 mdelay(5);
2051 return wbsd_resume(host);
2054 #endif /* CONFIG_PNP */
2056 #else /* CONFIG_PM */
2058 #define wbsd_platform_suspend NULL
2059 #define wbsd_platform_resume NULL
2061 #define wbsd_pnp_suspend NULL
2062 #define wbsd_pnp_resume NULL
2064 #endif /* CONFIG_PM */
2066 static struct platform_device *wbsd_device;
2068 static struct platform_driver wbsd_driver = {
2069 .probe = wbsd_probe,
2070 .remove = __devexit_p(wbsd_remove),
2072 .suspend = wbsd_platform_suspend,
2073 .resume = wbsd_platform_resume,
2074 .driver = {
2075 .name = DRIVER_NAME,
2079 #ifdef CONFIG_PNP
2081 static struct pnp_driver wbsd_pnp_driver = {
2082 .name = DRIVER_NAME,
2083 .id_table = pnp_dev_table,
2084 .probe = wbsd_pnp_probe,
2085 .remove = __devexit_p(wbsd_pnp_remove),
2087 .suspend = wbsd_pnp_suspend,
2088 .resume = wbsd_pnp_resume,
2091 #endif /* CONFIG_PNP */
2094 * Module loading/unloading
2097 static int __init wbsd_drv_init(void)
2099 int result;
2101 printk(KERN_INFO DRIVER_NAME
2102 ": Winbond W83L51xD SD/MMC card interface driver\n");
2103 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2105 #ifdef CONFIG_PNP
2107 if (!nopnp) {
2108 result = pnp_register_driver(&wbsd_pnp_driver);
2109 if (result < 0)
2110 return result;
2112 #endif /* CONFIG_PNP */
2114 if (nopnp) {
2115 result = platform_driver_register(&wbsd_driver);
2116 if (result < 0)
2117 return result;
2119 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2120 if (!wbsd_device) {
2121 platform_driver_unregister(&wbsd_driver);
2122 return -ENOMEM;
2125 result = platform_device_add(wbsd_device);
2126 if (result) {
2127 platform_device_put(wbsd_device);
2128 platform_driver_unregister(&wbsd_driver);
2129 return result;
2133 return 0;
2136 static void __exit wbsd_drv_exit(void)
2138 #ifdef CONFIG_PNP
2140 if (!nopnp)
2141 pnp_unregister_driver(&wbsd_pnp_driver);
2143 #endif /* CONFIG_PNP */
2145 if (nopnp) {
2146 platform_device_unregister(wbsd_device);
2148 platform_driver_unregister(&wbsd_driver);
2151 DBG("unloaded\n");
2154 module_init(wbsd_drv_init);
2155 module_exit(wbsd_drv_exit);
2156 #ifdef CONFIG_PNP
2157 module_param(nopnp, uint, 0444);
2158 #endif
2159 module_param(io, uint, 0444);
2160 module_param(irq, uint, 0444);
2161 module_param(dma, int, 0444);
2163 MODULE_LICENSE("GPL");
2164 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2165 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2167 #ifdef CONFIG_PNP
2168 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2169 #endif
2170 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2171 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2172 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");