USB HID: Thustmaster firestorm dual power v1 support
[linux-2.6] / drivers / mmc / wbsd.c
1 /*
2  *  linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
3  *
4  *  Copyright (C) 2004-2006 Pierre Ossman, All Rights Reserved.
5  *
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.
10  *
11  *
12  * Warning!
13  *
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:
16  *
17  * - FIFO size field in FSR is always zero.
18  *
19  * - FIFO interrupts tend not to work as they should. Interrupts are
20  *   triggered only for full/empty events, not for threshold values.
21  *
22  * - On APIC systems the FIFO empty interrupt is sometimes lost.
23  */
24
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>
37
38 #include <asm/io.h>
39 #include <asm/dma.h>
40 #include <asm/scatterlist.h>
41
42 #include "wbsd.h"
43
44 #define DRIVER_NAME "wbsd"
45
46 #define DBG(x...) \
47         pr_debug(DRIVER_NAME ": " x)
48 #define DBGF(f, x...) \
49         pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
50
51 /*
52  * Device resources
53  */
54
55 #ifdef CONFIG_PNP
56
57 static const struct pnp_device_id pnp_dev_table[] = {
58         { "WEC0517", 0 },
59         { "WEC0518", 0 },
60         { "", 0 },
61 };
62
63 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
64
65 #endif /* CONFIG_PNP */
66
67 static const int config_ports[] = { 0x2E, 0x4E };
68 static const int unlock_codes[] = { 0x83, 0x87 };
69
70 static const int valid_ids[] = {
71         0x7112,
72         };
73
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;
82
83 /*
84  * Basic functions
85  */
86
87 static inline void wbsd_unlock_config(struct wbsd_host *host)
88 {
89         BUG_ON(host->config == 0);
90
91         outb(host->unlock_code, host->config);
92         outb(host->unlock_code, host->config);
93 }
94
95 static inline void wbsd_lock_config(struct wbsd_host *host)
96 {
97         BUG_ON(host->config == 0);
98
99         outb(LOCK_CODE, host->config);
100 }
101
102 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
103 {
104         BUG_ON(host->config == 0);
105
106         outb(reg, host->config);
107         outb(value, host->config + 1);
108 }
109
110 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
111 {
112         BUG_ON(host->config == 0);
113
114         outb(reg, host->config);
115         return inb(host->config + 1);
116 }
117
118 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
119 {
120         outb(index, host->base + WBSD_IDXR);
121         outb(value, host->base + WBSD_DATAR);
122 }
123
124 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
125 {
126         outb(index, host->base + WBSD_IDXR);
127         return inb(host->base + WBSD_DATAR);
128 }
129
130 /*
131  * Common routines
132  */
133
134 static void wbsd_init_device(struct wbsd_host *host)
135 {
136         u8 setup, ier;
137
138         /*
139          * Reset chip (SD/MMC part) and fifo.
140          */
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);
144
145         /*
146          * Set DAT3 to input
147          */
148         setup &= ~WBSD_DAT3_H;
149         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
150         host->flags &= ~WBSD_FIGNORE_DETECT;
151
152         /*
153          * Read back default clock.
154          */
155         host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
156
157         /*
158          * Power down port.
159          */
160         outb(WBSD_POWER_N, host->base + WBSD_CSR);
161
162         /*
163          * Set maximum timeout.
164          */
165         wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
166
167         /*
168          * Test for card presence
169          */
170         if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
171                 host->flags |= WBSD_FCARD_PRESENT;
172         else
173                 host->flags &= ~WBSD_FCARD_PRESENT;
174
175         /*
176          * Enable interesting interrupts.
177          */
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;
185
186         outb(ier, host->base + WBSD_EIR);
187
188         /*
189          * Clear interrupts.
190          */
191         inb(host->base + WBSD_ISR);
192 }
193
194 static void wbsd_reset(struct wbsd_host *host)
195 {
196         u8 setup;
197
198         printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
199
200         /*
201          * Soft reset of chip (SD/MMC part).
202          */
203         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
204         setup |= WBSD_SOFT_RESET;
205         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
206 }
207
208 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
209 {
210         unsigned long dmaflags;
211
212         DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
213
214         if (host->dma >= 0) {
215                 /*
216                  * Release ISA DMA controller.
217                  */
218                 dmaflags = claim_dma_lock();
219                 disable_dma(host->dma);
220                 clear_dma_ff(host->dma);
221                 release_dma_lock(dmaflags);
222
223                 /*
224                  * Disable DMA on host.
225                  */
226                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
227         }
228
229         host->mrq = NULL;
230
231         /*
232          * MMC layer might call back into the driver so first unlock.
233          */
234         spin_unlock(&host->lock);
235         mmc_request_done(host->mmc, mrq);
236         spin_lock(&host->lock);
237 }
238
239 /*
240  * Scatter/gather functions
241  */
242
243 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
244 {
245         /*
246          * Get info. about SG list from data structure.
247          */
248         host->cur_sg = data->sg;
249         host->num_sg = data->sg_len;
250
251         host->offset = 0;
252         host->remain = host->cur_sg->length;
253 }
254
255 static inline int wbsd_next_sg(struct wbsd_host *host)
256 {
257         /*
258          * Skip to next SG entry.
259          */
260         host->cur_sg++;
261         host->num_sg--;
262
263         /*
264          * Any entries left?
265          */
266         if (host->num_sg > 0) {
267                 host->offset = 0;
268                 host->remain = host->cur_sg->length;
269         }
270
271         return host->num_sg;
272 }
273
274 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
275 {
276         return page_address(host->cur_sg->page) + host->cur_sg->offset;
277 }
278
279 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
280 {
281         unsigned int len, i, size;
282         struct scatterlist *sg;
283         char *dmabuf = host->dma_buffer;
284         char *sgbuf;
285
286         size = host->size;
287
288         sg = data->sg;
289         len = data->sg_len;
290
291         /*
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.
295          */
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;
303
304                 if (size < sg[i].length)
305                         size = 0;
306                 else
307                         size -= sg[i].length;
308
309                 if (size == 0)
310                         break;
311         }
312
313         /*
314          * Check that we didn't get a request to transfer
315          * more data than can fit into the SG list.
316          */
317
318         BUG_ON(size != 0);
319
320         host->size -= size;
321 }
322
323 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
324 {
325         unsigned int len, i, size;
326         struct scatterlist *sg;
327         char *dmabuf = host->dma_buffer;
328         char *sgbuf;
329
330         size = host->size;
331
332         sg = data->sg;
333         len = data->sg_len;
334
335         /*
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.
339          */
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;
347
348                 if (size < sg[i].length)
349                         size = 0;
350                 else
351                         size -= sg[i].length;
352
353                 if (size == 0)
354                         break;
355         }
356
357         /*
358          * Check that we didn't get a request to transfer
359          * more data than can fit into the SG list.
360          */
361
362         BUG_ON(size != 0);
363
364         host->size -= size;
365 }
366
367 /*
368  * Command handling
369  */
370
371 static inline void wbsd_get_short_reply(struct wbsd_host *host,
372                                         struct mmc_command *cmd)
373 {
374         /*
375          * Correct response type?
376          */
377         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
378                 cmd->error = MMC_ERR_INVALID;
379                 return;
380         }
381
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;
387 }
388
389 static inline void wbsd_get_long_reply(struct wbsd_host *host,
390         struct mmc_command *cmd)
391 {
392         int i;
393
394         /*
395          * Correct response type?
396          */
397         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
398                 cmd->error = MMC_ERR_INVALID;
399                 return;
400         }
401
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;
411         }
412 }
413
414 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
415 {
416         int i;
417         u8 status, isr;
418
419         DBGF("Sending cmd (%x)\n", cmd->opcode);
420
421         /*
422          * Clear accumulated ISR. The interrupt routine
423          * will fill this one with events that occur during
424          * transfer.
425          */
426         host->isr = 0;
427
428         /*
429          * Send the command (CRC calculated by host).
430          */
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);
434
435         cmd->error = MMC_ERR_NONE;
436
437         /*
438          * Wait for the request to complete.
439          */
440         do {
441                 status = wbsd_read_index(host, WBSD_IDX_STATUS);
442         } while (status & WBSD_CARDTRAFFIC);
443
444         /*
445          * Do we expect a reply?
446          */
447         if (cmd->flags & MMC_RSP_PRESENT) {
448                 /*
449                  * Read back status.
450                  */
451                 isr = host->isr;
452
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);
468                 }
469         }
470
471         DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
472 }
473
474 /*
475  * Data functions
476  */
477
478 static void wbsd_empty_fifo(struct wbsd_host *host)
479 {
480         struct mmc_data *data = host->mrq->cmd->data;
481         char *buffer;
482         int i, fsr, fifo;
483
484         /*
485          * Handle excessive data.
486          */
487         if (data->bytes_xfered == host->size)
488                 return;
489
490         buffer = wbsd_sg_to_buffer(host) + host->offset;
491
492         /*
493          * Drain the fifo. This has a tendency to loop longer
494          * than the FIFO length (usually one block).
495          */
496         while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
497                 /*
498                  * The size field in the FSR is broken so we have to
499                  * do some guessing.
500                  */
501                 if (fsr & WBSD_FIFO_FULL)
502                         fifo = 16;
503                 else if (fsr & WBSD_FIFO_FUTHRE)
504                         fifo = 8;
505                 else
506                         fifo = 1;
507
508                 for (i = 0; i < fifo; i++) {
509                         *buffer = inb(host->base + WBSD_DFR);
510                         buffer++;
511                         host->offset++;
512                         host->remain--;
513
514                         data->bytes_xfered++;
515
516                         /*
517                          * Transfer done?
518                          */
519                         if (data->bytes_xfered == host->size)
520                                 return;
521
522                         /*
523                          * End of scatter list entry?
524                          */
525                         if (host->remain == 0) {
526                                 /*
527                                  * Get next entry. Check if last.
528                                  */
529                                 if (!wbsd_next_sg(host)) {
530                                         /*
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.
535                                          */
536                                         BUG_ON(1);
537
538                                         host->size = data->bytes_xfered;
539
540                                         return;
541                                 }
542
543                                 buffer = wbsd_sg_to_buffer(host);
544                         }
545                 }
546         }
547
548         /*
549          * This is a very dirty hack to solve a
550          * hardware problem. The chip doesn't trigger
551          * FIFO threshold interrupts properly.
552          */
553         if ((host->size - data->bytes_xfered) < 16)
554                 tasklet_schedule(&host->fifo_tasklet);
555 }
556
557 static void wbsd_fill_fifo(struct wbsd_host *host)
558 {
559         struct mmc_data *data = host->mrq->cmd->data;
560         char *buffer;
561         int i, fsr, fifo;
562
563         /*
564          * Check that we aren't being called after the
565          * entire buffer has been transfered.
566          */
567         if (data->bytes_xfered == host->size)
568                 return;
569
570         buffer = wbsd_sg_to_buffer(host) + host->offset;
571
572         /*
573          * Fill the fifo. This has a tendency to loop longer
574          * than the FIFO length (usually one block).
575          */
576         while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
577                 /*
578                  * The size field in the FSR is broken so we have to
579                  * do some guessing.
580                  */
581                 if (fsr & WBSD_FIFO_EMPTY)
582                         fifo = 0;
583                 else if (fsr & WBSD_FIFO_EMTHRE)
584                         fifo = 8;
585                 else
586                         fifo = 15;
587
588                 for (i = 16; i > fifo; i--) {
589                         outb(*buffer, host->base + WBSD_DFR);
590                         buffer++;
591                         host->offset++;
592                         host->remain--;
593
594                         data->bytes_xfered++;
595
596                         /*
597                          * Transfer done?
598                          */
599                         if (data->bytes_xfered == host->size)
600                                 return;
601
602                         /*
603                          * End of scatter list entry?
604                          */
605                         if (host->remain == 0) {
606                                 /*
607                                  * Get next entry. Check if last.
608                                  */
609                                 if (!wbsd_next_sg(host)) {
610                                         /*
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.
615                                          */
616                                         BUG_ON(1);
617
618                                         host->size = data->bytes_xfered;
619
620                                         return;
621                                 }
622
623                                 buffer = wbsd_sg_to_buffer(host);
624                         }
625                 }
626         }
627
628         /*
629          * The controller stops sending interrupts for
630          * 'FIFO empty' under certain conditions. So we
631          * need to be a bit more pro-active.
632          */
633         tasklet_schedule(&host->fifo_tasklet);
634 }
635
636 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
637 {
638         u16 blksize;
639         u8 setup;
640         unsigned long dmaflags;
641
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);
646
647         /*
648          * Calculate size.
649          */
650         host->size = data->blocks * data->blksz;
651
652         /*
653          * Check timeout values for overflow.
654          * (Yes, some cards cause this value to overflow).
655          */
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);
661         }
662
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);
667
668         /*
669          * Inform the chip of how large blocks will be
670          * sent. It needs this to determine when to
671          * calculate CRC.
672          *
673          * Space for CRC must be included in the size.
674          * Two bytes are needed for each data line.
675          */
676         if (host->bus_width == MMC_BUS_WIDTH_1) {
677                 blksize = data->blksz + 2;
678
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;
683
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;
690         }
691
692         /*
693          * Clear the FIFO. This is needed even for DMA
694          * transfers since the chip still uses the FIFO
695          * internally.
696          */
697         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
698         setup |= WBSD_FIFO_RESET;
699         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
700
701         /*
702          * DMA transfer?
703          */
704         if (host->dma >= 0) {
705                 /*
706                  * The buffer for DMA is only 64 kB.
707                  */
708                 BUG_ON(host->size > 0x10000);
709                 if (host->size > 0x10000) {
710                         data->error = MMC_ERR_INVALID;
711                         return;
712                 }
713
714                 /*
715                  * Transfer data from the SG list to
716                  * the DMA buffer.
717                  */
718                 if (data->flags & MMC_DATA_WRITE)
719                         wbsd_sg_to_dma(host, data);
720
721                 /*
722                  * Initialise the ISA DMA controller.
723                  */
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);
733
734                 enable_dma(host->dma);
735                 release_dma_lock(dmaflags);
736
737                 /*
738                  * Enable DMA on the host.
739                  */
740                 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
741         } else {
742                 /*
743                  * This flag is used to keep printk
744                  * output to a minimum.
745                  */
746                 host->firsterr = 1;
747
748                 /*
749                  * Initialise the SG list.
750                  */
751                 wbsd_init_sg(host, data);
752
753                 /*
754                  * Turn off DMA.
755                  */
756                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
757
758                 /*
759                  * Set up FIFO threshold levels (and fill
760                  * buffer if doing a write).
761                  */
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);
769                 }
770         }
771
772         data->error = MMC_ERR_NONE;
773 }
774
775 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
776 {
777         unsigned long dmaflags;
778         int count;
779         u8 status;
780
781         WARN_ON(host->mrq == NULL);
782
783         /*
784          * Send a stop command if needed.
785          */
786         if (data->stop)
787                 wbsd_send_command(host, data->stop);
788
789         /*
790          * Wait for the controller to leave data
791          * transfer state.
792          */
793         do {
794                 status = wbsd_read_index(host, WBSD_IDX_STATUS);
795         } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
796
797         /*
798          * DMA transfer?
799          */
800         if (host->dma >= 0) {
801                 /*
802                  * Disable DMA on the host.
803                  */
804                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
805
806                 /*
807                  * Turn of ISA DMA controller.
808                  */
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);
814
815                 /*
816                  * Any leftover data?
817                  */
818                 if (count) {
819                         printk(KERN_ERR "%s: Incomplete DMA transfer. "
820                                 "%d bytes left.\n",
821                                 mmc_hostname(host->mmc), count);
822
823                         data->error = MMC_ERR_FAILED;
824                 } else {
825                         /*
826                          * Transfer data from DMA buffer to
827                          * SG list.
828                          */
829                         if (data->flags & MMC_DATA_READ)
830                                 wbsd_dma_to_sg(host, data);
831
832                         data->bytes_xfered = host->size;
833                 }
834         }
835
836         DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
837
838         wbsd_request_end(host, host->mrq);
839 }
840
841 /*****************************************************************************\
842  *                                                                           *
843  * MMC layer callbacks                                                       *
844  *                                                                           *
845 \*****************************************************************************/
846
847 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
848 {
849         struct wbsd_host *host = mmc_priv(mmc);
850         struct mmc_command *cmd;
851
852         /*
853          * Disable tasklets to avoid a deadlock.
854          */
855         spin_lock_bh(&host->lock);
856
857         BUG_ON(host->mrq != NULL);
858
859         cmd = mrq->cmd;
860
861         host->mrq = mrq;
862
863         /*
864          * If there is no card in the slot then
865          * timeout immediatly.
866          */
867         if (!(host->flags & WBSD_FCARD_PRESENT)) {
868                 cmd->error = MMC_ERR_TIMEOUT;
869                 goto done;
870         }
871
872         /*
873          * Does the request include data?
874          */
875         if (cmd->data) {
876                 wbsd_prepare_data(host, cmd->data);
877
878                 if (cmd->data->error != MMC_ERR_NONE)
879                         goto done;
880         }
881
882         wbsd_send_command(host, cmd);
883
884         /*
885          * If this is a data transfer the request
886          * will be finished after the data has
887          * transfered.
888          */
889         if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
890                 /*
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.
895                  */
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;
909
910                 /* ACMDs. We don't keep track of state, so we just treat them
911                  * like any other command. */
912                 case 51:
913                         break;
914
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;
922
923                         if (cmd->data->stop)
924                                 wbsd_send_command(host, cmd->data->stop);
925
926                         goto done;
927                 };
928
929                 /*
930                  * Dirty fix for hardware bug.
931                  */
932                 if (host->dma == -1)
933                         tasklet_schedule(&host->fifo_tasklet);
934
935                 spin_unlock_bh(&host->lock);
936
937                 return;
938         }
939
940 done:
941         wbsd_request_end(host, mrq);
942
943         spin_unlock_bh(&host->lock);
944 }
945
946 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
947 {
948         struct wbsd_host *host = mmc_priv(mmc);
949         u8 clk, setup, pwr;
950
951         spin_lock_bh(&host->lock);
952
953         /*
954          * Reset the chip on each power off.
955          * Should clear out any weird states.
956          */
957         if (ios->power_mode == MMC_POWER_OFF)
958                 wbsd_init_device(host);
959
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;
968
969         /*
970          * Only write to the clock register when
971          * there is an actual change.
972          */
973         if (clk != host->clk) {
974                 wbsd_write_index(host, WBSD_IDX_CLK, clk);
975                 host->clk = clk;
976         }
977
978         /*
979          * Power up card.
980          */
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);
985         }
986
987         /*
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.
991          */
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;
1000
1001                         /*
1002                          * We cannot resume card detection immediatly
1003                          * because of capacitance and delays in the chip.
1004                          */
1005                         mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1006                 }
1007         }
1008         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1009
1010         /*
1011          * Store bus width for later. Will be used when
1012          * setting up the data transfer.
1013          */
1014         host->bus_width = ios->bus_width;
1015
1016         spin_unlock_bh(&host->lock);
1017 }
1018
1019 static int wbsd_get_ro(struct mmc_host *mmc)
1020 {
1021         struct wbsd_host *host = mmc_priv(mmc);
1022         u8 csr;
1023
1024         spin_lock_bh(&host->lock);
1025
1026         csr = inb(host->base + WBSD_CSR);
1027         csr |= WBSD_MSLED;
1028         outb(csr, host->base + WBSD_CSR);
1029
1030         mdelay(1);
1031
1032         csr = inb(host->base + WBSD_CSR);
1033         csr &= ~WBSD_MSLED;
1034         outb(csr, host->base + WBSD_CSR);
1035
1036         spin_unlock_bh(&host->lock);
1037
1038         return csr & WBSD_WRPT;
1039 }
1040
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,
1045 };
1046
1047 /*****************************************************************************\
1048  *                                                                           *
1049  * Interrupt handling                                                        *
1050  *                                                                           *
1051 \*****************************************************************************/
1052
1053 /*
1054  * Helper function to reset detection ignore
1055  */
1056
1057 static void wbsd_reset_ignore(unsigned long data)
1058 {
1059         struct wbsd_host *host = (struct wbsd_host *)data;
1060
1061         BUG_ON(host == NULL);
1062
1063         DBG("Resetting card detection ignore\n");
1064
1065         spin_lock_bh(&host->lock);
1066
1067         host->flags &= ~WBSD_FIGNORE_DETECT;
1068
1069         /*
1070          * Card status might have changed during the
1071          * blackout.
1072          */
1073         tasklet_schedule(&host->card_tasklet);
1074
1075         spin_unlock_bh(&host->lock);
1076 }
1077
1078 /*
1079  * Tasklets
1080  */
1081
1082 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1083 {
1084         WARN_ON(!host->mrq);
1085         if (!host->mrq)
1086                 return NULL;
1087
1088         WARN_ON(!host->mrq->cmd);
1089         if (!host->mrq->cmd)
1090                 return NULL;
1091
1092         WARN_ON(!host->mrq->cmd->data);
1093         if (!host->mrq->cmd->data)
1094                 return NULL;
1095
1096         return host->mrq->cmd->data;
1097 }
1098
1099 static void wbsd_tasklet_card(unsigned long param)
1100 {
1101         struct wbsd_host *host = (struct wbsd_host *)param;
1102         u8 csr;
1103         int delay = -1;
1104
1105         spin_lock(&host->lock);
1106
1107         if (host->flags & WBSD_FIGNORE_DETECT) {
1108                 spin_unlock(&host->lock);
1109                 return;
1110         }
1111
1112         csr = inb(host->base + WBSD_CSR);
1113         WARN_ON(csr == 0xff);
1114
1115         if (csr & WBSD_CARDPRESENT) {
1116                 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1117                         DBG("Card inserted\n");
1118                         host->flags |= WBSD_FCARD_PRESENT;
1119
1120                         delay = 500;
1121                 }
1122         } else if (host->flags & WBSD_FCARD_PRESENT) {
1123                 DBG("Card removed\n");
1124                 host->flags &= ~WBSD_FCARD_PRESENT;
1125
1126                 if (host->mrq) {
1127                         printk(KERN_ERR "%s: Card removed during transfer!\n",
1128                                 mmc_hostname(host->mmc));
1129                         wbsd_reset(host);
1130
1131                         host->mrq->cmd->error = MMC_ERR_FAILED;
1132                         tasklet_schedule(&host->finish_tasklet);
1133                 }
1134
1135                 delay = 0;
1136         }
1137
1138         /*
1139          * Unlock first since we might get a call back.
1140          */
1141
1142         spin_unlock(&host->lock);
1143
1144         if (delay != -1)
1145                 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1146 }
1147
1148 static void wbsd_tasklet_fifo(unsigned long param)
1149 {
1150         struct wbsd_host *host = (struct wbsd_host *)param;
1151         struct mmc_data *data;
1152
1153         spin_lock(&host->lock);
1154
1155         if (!host->mrq)
1156                 goto end;
1157
1158         data = wbsd_get_data(host);
1159         if (!data)
1160                 goto end;
1161
1162         if (data->flags & MMC_DATA_WRITE)
1163                 wbsd_fill_fifo(host);
1164         else
1165                 wbsd_empty_fifo(host);
1166
1167         /*
1168          * Done?
1169          */
1170         if (host->size == data->bytes_xfered) {
1171                 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1172                 tasklet_schedule(&host->finish_tasklet);
1173         }
1174
1175 end:
1176         spin_unlock(&host->lock);
1177 }
1178
1179 static void wbsd_tasklet_crc(unsigned long param)
1180 {
1181         struct wbsd_host *host = (struct wbsd_host *)param;
1182         struct mmc_data *data;
1183
1184         spin_lock(&host->lock);
1185
1186         if (!host->mrq)
1187                 goto end;
1188
1189         data = wbsd_get_data(host);
1190         if (!data)
1191                 goto end;
1192
1193         DBGF("CRC error\n");
1194
1195         data->error = MMC_ERR_BADCRC;
1196
1197         tasklet_schedule(&host->finish_tasklet);
1198
1199 end:
1200         spin_unlock(&host->lock);
1201 }
1202
1203 static void wbsd_tasklet_timeout(unsigned long param)
1204 {
1205         struct wbsd_host *host = (struct wbsd_host *)param;
1206         struct mmc_data *data;
1207
1208         spin_lock(&host->lock);
1209
1210         if (!host->mrq)
1211                 goto end;
1212
1213         data = wbsd_get_data(host);
1214         if (!data)
1215                 goto end;
1216
1217         DBGF("Timeout\n");
1218
1219         data->error = MMC_ERR_TIMEOUT;
1220
1221         tasklet_schedule(&host->finish_tasklet);
1222
1223 end:
1224         spin_unlock(&host->lock);
1225 }
1226
1227 static void wbsd_tasklet_finish(unsigned long param)
1228 {
1229         struct wbsd_host *host = (struct wbsd_host *)param;
1230         struct mmc_data *data;
1231
1232         spin_lock(&host->lock);
1233
1234         WARN_ON(!host->mrq);
1235         if (!host->mrq)
1236                 goto end;
1237
1238         data = wbsd_get_data(host);
1239         if (!data)
1240                 goto end;
1241
1242         wbsd_finish_data(host, data);
1243
1244 end:
1245         spin_unlock(&host->lock);
1246 }
1247
1248 static void wbsd_tasklet_block(unsigned long param)
1249 {
1250         struct wbsd_host *host = (struct wbsd_host *)param;
1251         struct mmc_data *data;
1252
1253         spin_lock(&host->lock);
1254
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;
1260
1261                 DBGF("CRC error\n");
1262
1263                 data->error = MMC_ERR_BADCRC;
1264
1265                 tasklet_schedule(&host->finish_tasklet);
1266         }
1267
1268 end:
1269         spin_unlock(&host->lock);
1270 }
1271
1272 /*
1273  * Interrupt handling
1274  */
1275
1276 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1277 {
1278         struct wbsd_host *host = dev_id;
1279         int isr;
1280
1281         isr = inb(host->base + WBSD_ISR);
1282
1283         /*
1284          * Was it actually our hardware that caused the interrupt?
1285          */
1286         if (isr == 0xff || isr == 0x00)
1287                 return IRQ_NONE;
1288
1289         host->isr |= isr;
1290
1291         /*
1292          * Schedule tasklets as needed.
1293          */
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);
1306
1307         return IRQ_HANDLED;
1308 }
1309
1310 /*****************************************************************************\
1311  *                                                                           *
1312  * Device initialisation and shutdown                                        *
1313  *                                                                           *
1314 \*****************************************************************************/
1315
1316 /*
1317  * Allocate/free MMC structure.
1318  */
1319
1320 static int __devinit wbsd_alloc_mmc(struct device *dev)
1321 {
1322         struct mmc_host *mmc;
1323         struct wbsd_host *host;
1324
1325         /*
1326          * Allocate MMC structure.
1327          */
1328         mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1329         if (!mmc)
1330                 return -ENOMEM;
1331
1332         host = mmc_priv(mmc);
1333         host->mmc = mmc;
1334
1335         host->dma = -1;
1336
1337         /*
1338          * Set host parameters.
1339          */
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;
1345
1346         spin_lock_init(&host->lock);
1347
1348         /*
1349          * Set up timers
1350          */
1351         init_timer(&host->ignore_timer);
1352         host->ignore_timer.data = (unsigned long)host;
1353         host->ignore_timer.function = wbsd_reset_ignore;
1354
1355         /*
1356          * Maximum number of segments. Worst case is one sector per segment
1357          * so this will be 64kB/512.
1358          */
1359         mmc->max_hw_segs = 128;
1360         mmc->max_phys_segs = 128;
1361
1362         /*
1363          * Maximum request size. Also limited by 64KiB buffer.
1364          */
1365         mmc->max_req_size = 65536;
1366
1367         /*
1368          * Maximum segment size. Could be one segment with the maximum number
1369          * of bytes.
1370          */
1371         mmc->max_seg_size = mmc->max_req_size;
1372
1373         /*
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.
1376          */
1377         mmc->max_blk_size = 4087;
1378
1379         /*
1380          * Maximum block count. There is no real limit so the maximum
1381          * request size will be the only restriction.
1382          */
1383         mmc->max_blk_count = mmc->max_req_size;
1384
1385         dev_set_drvdata(dev, mmc);
1386
1387         return 0;
1388 }
1389
1390 static void __devexit wbsd_free_mmc(struct device *dev)
1391 {
1392         struct mmc_host *mmc;
1393         struct wbsd_host *host;
1394
1395         mmc = dev_get_drvdata(dev);
1396         if (!mmc)
1397                 return;
1398
1399         host = mmc_priv(mmc);
1400         BUG_ON(host == NULL);
1401
1402         del_timer_sync(&host->ignore_timer);
1403
1404         mmc_free_host(mmc);
1405
1406         dev_set_drvdata(dev, NULL);
1407 }
1408
1409 /*
1410  * Scan for known chip id:s
1411  */
1412
1413 static int __devinit wbsd_scan(struct wbsd_host *host)
1414 {
1415         int i, j, k;
1416         int id;
1417
1418         /*
1419          * Iterate through all ports, all codes to
1420          * find hardware that is in our known list.
1421          */
1422         for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1423                 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1424                         continue;
1425
1426                 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1427                         id = 0xFFFF;
1428
1429                         host->config = config_ports[i];
1430                         host->unlock_code = unlock_codes[j];
1431
1432                         wbsd_unlock_config(host);
1433
1434                         outb(WBSD_CONF_ID_HI, config_ports[i]);
1435                         id = inb(config_ports[i] + 1) << 8;
1436
1437                         outb(WBSD_CONF_ID_LO, config_ports[i]);
1438                         id |= inb(config_ports[i] + 1);
1439
1440                         wbsd_lock_config(host);
1441
1442                         for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1443                                 if (id == valid_ids[k]) {
1444                                         host->chip_id = id;
1445
1446                                         return 0;
1447                                 }
1448                         }
1449
1450                         if (id != 0xFFFF) {
1451                                 DBG("Unknown hardware (id %x) found at %x\n",
1452                                         id, config_ports[i]);
1453                         }
1454                 }
1455
1456                 release_region(config_ports[i], 2);
1457         }
1458
1459         host->config = 0;
1460         host->unlock_code = 0;
1461
1462         return -ENODEV;
1463 }
1464
1465 /*
1466  * Allocate/free io port ranges
1467  */
1468
1469 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1470 {
1471         if (base & 0x7)
1472                 return -EINVAL;
1473
1474         if (!request_region(base, 8, DRIVER_NAME))
1475                 return -EIO;
1476
1477         host->base = base;
1478
1479         return 0;
1480 }
1481
1482 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1483 {
1484         if (host->base)
1485                 release_region(host->base, 8);
1486
1487         host->base = 0;
1488
1489         if (host->config)
1490                 release_region(host->config, 2);
1491
1492         host->config = 0;
1493 }
1494
1495 /*
1496  * Allocate/free DMA port and buffer
1497  */
1498
1499 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1500 {
1501         if (dma < 0)
1502                 return;
1503
1504         if (request_dma(dma, DRIVER_NAME))
1505                 goto err;
1506
1507         /*
1508          * We need to allocate a special buffer in
1509          * order for ISA to be able to DMA to it.
1510          */
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;
1515
1516         /*
1517          * Translate the address to a physical address.
1518          */
1519         host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1520                 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1521
1522         /*
1523          * ISA DMA must be aligned on a 64k basis.
1524          */
1525         if ((host->dma_addr & 0xffff) != 0)
1526                 goto kfree;
1527         /*
1528          * ISA cannot access memory above 16 MB.
1529          */
1530         else if (host->dma_addr >= 0x1000000)
1531                 goto kfree;
1532
1533         host->dma = dma;
1534
1535         return;
1536
1537 kfree:
1538         /*
1539          * If we've gotten here then there is some kind of alignment bug
1540          */
1541         BUG_ON(1);
1542
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;
1546
1547         kfree(host->dma_buffer);
1548         host->dma_buffer = NULL;
1549
1550 free:
1551         free_dma(dma);
1552
1553 err:
1554         printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1555                 "Falling back on FIFO.\n", dma);
1556 }
1557
1558 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1559 {
1560         if (host->dma_addr) {
1561                 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1562                         WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1563         }
1564         kfree(host->dma_buffer);
1565         if (host->dma >= 0)
1566                 free_dma(host->dma);
1567
1568         host->dma = -1;
1569         host->dma_buffer = NULL;
1570         host->dma_addr = (dma_addr_t)NULL;
1571 }
1572
1573 /*
1574  * Allocate/free IRQ.
1575  */
1576
1577 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1578 {
1579         int ret;
1580
1581         /*
1582          * Allocate interrupt.
1583          */
1584
1585         ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1586         if (ret)
1587                 return ret;
1588
1589         host->irq = irq;
1590
1591         /*
1592          * Set up tasklets.
1593          */
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);
1606
1607         return 0;
1608 }
1609
1610 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1611 {
1612         if (!host->irq)
1613                 return;
1614
1615         free_irq(host->irq, host);
1616
1617         host->irq = 0;
1618
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);
1625 }
1626
1627 /*
1628  * Allocate all resources for the host.
1629  */
1630
1631 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1632         int base, int irq, int dma)
1633 {
1634         int ret;
1635
1636         /*
1637          * Allocate I/O ports.
1638          */
1639         ret = wbsd_request_region(host, base);
1640         if (ret)
1641                 return ret;
1642
1643         /*
1644          * Allocate interrupt.
1645          */
1646         ret = wbsd_request_irq(host, irq);
1647         if (ret)
1648                 return ret;
1649
1650         /*
1651          * Allocate DMA.
1652          */
1653         wbsd_request_dma(host, dma);
1654
1655         return 0;
1656 }
1657
1658 /*
1659  * Release all resources for the host.
1660  */
1661
1662 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1663 {
1664         wbsd_release_dma(host);
1665         wbsd_release_irq(host);
1666         wbsd_release_regions(host);
1667 }
1668
1669 /*
1670  * Configure the resources the chip should use.
1671  */
1672
1673 static void wbsd_chip_config(struct wbsd_host *host)
1674 {
1675         wbsd_unlock_config(host);
1676
1677         /*
1678          * Reset the chip.
1679          */
1680         wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1681         wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1682
1683         /*
1684          * Select SD/MMC function.
1685          */
1686         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1687
1688         /*
1689          * Set up card detection.
1690          */
1691         wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1692
1693         /*
1694          * Configure chip
1695          */
1696         wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1697         wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1698
1699         wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1700
1701         if (host->dma >= 0)
1702                 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1703
1704         /*
1705          * Enable and power up chip.
1706          */
1707         wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1708         wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1709
1710         wbsd_lock_config(host);
1711 }
1712
1713 /*
1714  * Check that configured resources are correct.
1715  */
1716
1717 static int wbsd_chip_validate(struct wbsd_host *host)
1718 {
1719         int base, irq, dma;
1720
1721         wbsd_unlock_config(host);
1722
1723         /*
1724          * Select SD/MMC function.
1725          */
1726         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1727
1728         /*
1729          * Read configuration.
1730          */
1731         base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1732         base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1733
1734         irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1735
1736         dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1737
1738         wbsd_lock_config(host);
1739
1740         /*
1741          * Validate against given configuration.
1742          */
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;
1749
1750         return 1;
1751 }
1752
1753 /*
1754  * Powers down the SD function
1755  */
1756
1757 static void wbsd_chip_poweroff(struct wbsd_host *host)
1758 {
1759         wbsd_unlock_config(host);
1760
1761         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1762         wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1763
1764         wbsd_lock_config(host);
1765 }
1766
1767 /*****************************************************************************\
1768  *                                                                           *
1769  * Devices setup and shutdown                                                *
1770  *                                                                           *
1771 \*****************************************************************************/
1772
1773 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1774         int pnp)
1775 {
1776         struct wbsd_host *host = NULL;
1777         struct mmc_host *mmc = NULL;
1778         int ret;
1779
1780         ret = wbsd_alloc_mmc(dev);
1781         if (ret)
1782                 return ret;
1783
1784         mmc = dev_get_drvdata(dev);
1785         host = mmc_priv(mmc);
1786
1787         /*
1788          * Scan for hardware.
1789          */
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;
1799                 }
1800         }
1801
1802         /*
1803          * Request resources.
1804          */
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;
1810         }
1811
1812         /*
1813          * See if chip needs to be configured.
1814          */
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);
1822                 }
1823         } else
1824                 wbsd_chip_config(host);
1825
1826         /*
1827          * Power Management stuff. No idea how this works.
1828          * Not tested.
1829          */
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);
1835         }
1836 #endif
1837         /*
1838          * Allow device to initialise itself properly.
1839          */
1840         mdelay(5);
1841
1842         /*
1843          * Reset the chip into a known state.
1844          */
1845         wbsd_init_device(host);
1846
1847         mmc_add_host(mmc);
1848
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");
1860
1861         return 0;
1862 }
1863
1864 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1865 {
1866         struct mmc_host *mmc = dev_get_drvdata(dev);
1867         struct wbsd_host *host;
1868
1869         if (!mmc)
1870                 return;
1871
1872         host = mmc_priv(mmc);
1873
1874         mmc_remove_host(mmc);
1875
1876         /*
1877          * Power down the SD/MMC function.
1878          */
1879         if (!pnp)
1880                 wbsd_chip_poweroff(host);
1881
1882         wbsd_release_resources(host);
1883
1884         wbsd_free_mmc(dev);
1885 }
1886
1887 /*
1888  * Non-PnP
1889  */
1890
1891 static int __devinit wbsd_probe(struct platform_device *dev)
1892 {
1893         /* Use the module parameters for resources */
1894         return wbsd_init(&dev->dev, io, irq, dma, 0);
1895 }
1896
1897 static int __devexit wbsd_remove(struct platform_device *dev)
1898 {
1899         wbsd_shutdown(&dev->dev, 0);
1900
1901         return 0;
1902 }
1903
1904 /*
1905  * PnP
1906  */
1907
1908 #ifdef CONFIG_PNP
1909
1910 static int __devinit
1911 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1912 {
1913         int io, irq, dma;
1914
1915         /*
1916          * Get resources from PnP layer.
1917          */
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;
1924
1925         DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1926
1927         return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1928 }
1929
1930 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1931 {
1932         wbsd_shutdown(&dev->dev, 1);
1933 }
1934
1935 #endif /* CONFIG_PNP */
1936
1937 /*
1938  * Power management
1939  */
1940
1941 #ifdef CONFIG_PM
1942
1943 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1944 {
1945         BUG_ON(host == NULL);
1946
1947         return mmc_suspend_host(host->mmc, state);
1948 }
1949
1950 static int wbsd_resume(struct wbsd_host *host)
1951 {
1952         BUG_ON(host == NULL);
1953
1954         wbsd_init_device(host);
1955
1956         return mmc_resume_host(host->mmc);
1957 }
1958
1959 static int wbsd_platform_suspend(struct platform_device *dev,
1960                                  pm_message_t state)
1961 {
1962         struct mmc_host *mmc = platform_get_drvdata(dev);
1963         struct wbsd_host *host;
1964         int ret;
1965
1966         if (mmc == NULL)
1967                 return 0;
1968
1969         DBGF("Suspending...\n");
1970
1971         host = mmc_priv(mmc);
1972
1973         ret = wbsd_suspend(host, state);
1974         if (ret)
1975                 return ret;
1976
1977         wbsd_chip_poweroff(host);
1978
1979         return 0;
1980 }
1981
1982 static int wbsd_platform_resume(struct platform_device *dev)
1983 {
1984         struct mmc_host *mmc = platform_get_drvdata(dev);
1985         struct wbsd_host *host;
1986
1987         if (mmc == NULL)
1988                 return 0;
1989
1990         DBGF("Resuming...\n");
1991
1992         host = mmc_priv(mmc);
1993
1994         wbsd_chip_config(host);
1995
1996         /*
1997          * Allow device to initialise itself properly.
1998          */
1999         mdelay(5);
2000
2001         return wbsd_resume(host);
2002 }
2003
2004 #ifdef CONFIG_PNP
2005
2006 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2007 {
2008         struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2009         struct wbsd_host *host;
2010
2011         if (mmc == NULL)
2012                 return 0;
2013
2014         DBGF("Suspending...\n");
2015
2016         host = mmc_priv(mmc);
2017
2018         return wbsd_suspend(host, state);
2019 }
2020
2021 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2022 {
2023         struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2024         struct wbsd_host *host;
2025
2026         if (mmc == NULL)
2027                 return 0;
2028
2029         DBGF("Resuming...\n");
2030
2031         host = mmc_priv(mmc);
2032
2033         /*
2034          * See if chip needs to be configured.
2035          */
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);
2043                 }
2044         }
2045
2046         /*
2047          * Allow device to initialise itself properly.
2048          */
2049         mdelay(5);
2050
2051         return wbsd_resume(host);
2052 }
2053
2054 #endif /* CONFIG_PNP */
2055
2056 #else /* CONFIG_PM */
2057
2058 #define wbsd_platform_suspend NULL
2059 #define wbsd_platform_resume NULL
2060
2061 #define wbsd_pnp_suspend NULL
2062 #define wbsd_pnp_resume NULL
2063
2064 #endif /* CONFIG_PM */
2065
2066 static struct platform_device *wbsd_device;
2067
2068 static struct platform_driver wbsd_driver = {
2069         .probe          = wbsd_probe,
2070         .remove         = __devexit_p(wbsd_remove),
2071
2072         .suspend        = wbsd_platform_suspend,
2073         .resume         = wbsd_platform_resume,
2074         .driver         = {
2075                 .name   = DRIVER_NAME,
2076         },
2077 };
2078
2079 #ifdef CONFIG_PNP
2080
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),
2086
2087         .suspend        = wbsd_pnp_suspend,
2088         .resume         = wbsd_pnp_resume,
2089 };
2090
2091 #endif /* CONFIG_PNP */
2092
2093 /*
2094  * Module loading/unloading
2095  */
2096
2097 static int __init wbsd_drv_init(void)
2098 {
2099         int result;
2100
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");
2104
2105 #ifdef CONFIG_PNP
2106
2107         if (!nopnp) {
2108                 result = pnp_register_driver(&wbsd_pnp_driver);
2109                 if (result < 0)
2110                         return result;
2111         }
2112 #endif /* CONFIG_PNP */
2113
2114         if (nopnp) {
2115                 result = platform_driver_register(&wbsd_driver);
2116                 if (result < 0)
2117                         return result;
2118
2119                 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2120                 if (!wbsd_device) {
2121                         platform_driver_unregister(&wbsd_driver);
2122                         return -ENOMEM;
2123                 }
2124
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;
2130                 }
2131         }
2132
2133         return 0;
2134 }
2135
2136 static void __exit wbsd_drv_exit(void)
2137 {
2138 #ifdef CONFIG_PNP
2139
2140         if (!nopnp)
2141                 pnp_unregister_driver(&wbsd_pnp_driver);
2142
2143 #endif /* CONFIG_PNP */
2144
2145         if (nopnp) {
2146                 platform_device_unregister(wbsd_device);
2147
2148                 platform_driver_unregister(&wbsd_driver);
2149         }
2150
2151         DBG("unloaded\n");
2152 }
2153
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);
2162
2163 MODULE_LICENSE("GPL");
2164 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2165 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2166
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)");