]> Pileus Git - ~andy/linux/blob - drivers/mmc/wbsd.c
mmc: wbsd: Remove driver version
[~andy/linux] / 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                 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
347                 dmabuf += sg[i].length;
348
349                 if (size < sg[i].length)
350                         size = 0;
351                 else
352                         size -= sg[i].length;
353
354                 if (size == 0)
355                         break;
356         }
357
358         /*
359          * Check that we didn't get a request to transfer
360          * more data than can fit into the SG list.
361          */
362
363         BUG_ON(size != 0);
364
365         host->size -= size;
366 }
367
368 /*
369  * Command handling
370  */
371
372 static inline void wbsd_get_short_reply(struct wbsd_host *host,
373                                         struct mmc_command *cmd)
374 {
375         /*
376          * Correct response type?
377          */
378         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
379                 cmd->error = MMC_ERR_INVALID;
380                 return;
381         }
382
383         cmd->resp[0]  = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
384         cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
385         cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
386         cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
387         cmd->resp[1]  = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
388 }
389
390 static inline void wbsd_get_long_reply(struct wbsd_host *host,
391         struct mmc_command *cmd)
392 {
393         int i;
394
395         /*
396          * Correct response type?
397          */
398         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
399                 cmd->error = MMC_ERR_INVALID;
400                 return;
401         }
402
403         for (i = 0; i < 4; i++) {
404                 cmd->resp[i] =
405                         wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
406                 cmd->resp[i] |=
407                         wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
408                 cmd->resp[i] |=
409                         wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
410                 cmd->resp[i] |=
411                         wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
412         }
413 }
414
415 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
416 {
417         int i;
418         u8 status, isr;
419
420         DBGF("Sending cmd (%x)\n", cmd->opcode);
421
422         /*
423          * Clear accumulated ISR. The interrupt routine
424          * will fill this one with events that occur during
425          * transfer.
426          */
427         host->isr = 0;
428
429         /*
430          * Send the command (CRC calculated by host).
431          */
432         outb(cmd->opcode, host->base + WBSD_CMDR);
433         for (i = 3; i >= 0; i--)
434                 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
435
436         cmd->error = MMC_ERR_NONE;
437
438         /*
439          * Wait for the request to complete.
440          */
441         do {
442                 status = wbsd_read_index(host, WBSD_IDX_STATUS);
443         } while (status & WBSD_CARDTRAFFIC);
444
445         /*
446          * Do we expect a reply?
447          */
448         if (cmd->flags & MMC_RSP_PRESENT) {
449                 /*
450                  * Read back status.
451                  */
452                 isr = host->isr;
453
454                 /* Card removed? */
455                 if (isr & WBSD_INT_CARD)
456                         cmd->error = MMC_ERR_TIMEOUT;
457                 /* Timeout? */
458                 else if (isr & WBSD_INT_TIMEOUT)
459                         cmd->error = MMC_ERR_TIMEOUT;
460                 /* CRC? */
461                 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
462                         cmd->error = MMC_ERR_BADCRC;
463                 /* All ok */
464                 else {
465                         if (cmd->flags & MMC_RSP_136)
466                                 wbsd_get_long_reply(host, cmd);
467                         else
468                                 wbsd_get_short_reply(host, cmd);
469                 }
470         }
471
472         DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
473 }
474
475 /*
476  * Data functions
477  */
478
479 static void wbsd_empty_fifo(struct wbsd_host *host)
480 {
481         struct mmc_data *data = host->mrq->cmd->data;
482         char *buffer;
483         int i, fsr, fifo;
484
485         /*
486          * Handle excessive data.
487          */
488         if (data->bytes_xfered == host->size)
489                 return;
490
491         buffer = wbsd_sg_to_buffer(host) + host->offset;
492
493         /*
494          * Drain the fifo. This has a tendency to loop longer
495          * than the FIFO length (usually one block).
496          */
497         while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
498                 /*
499                  * The size field in the FSR is broken so we have to
500                  * do some guessing.
501                  */
502                 if (fsr & WBSD_FIFO_FULL)
503                         fifo = 16;
504                 else if (fsr & WBSD_FIFO_FUTHRE)
505                         fifo = 8;
506                 else
507                         fifo = 1;
508
509                 for (i = 0; i < fifo; i++) {
510                         *buffer = inb(host->base + WBSD_DFR);
511                         buffer++;
512                         host->offset++;
513                         host->remain--;
514
515                         data->bytes_xfered++;
516
517                         /*
518                          * Transfer done?
519                          */
520                         if (data->bytes_xfered == host->size)
521                                 return;
522
523                         /*
524                          * End of scatter list entry?
525                          */
526                         if (host->remain == 0) {
527                                 /*
528                                  * Get next entry. Check if last.
529                                  */
530                                 if (!wbsd_next_sg(host)) {
531                                         /*
532                                          * We should never reach this point.
533                                          * It means that we're trying to
534                                          * transfer more blocks than can fit
535                                          * into the scatter list.
536                                          */
537                                         BUG_ON(1);
538
539                                         host->size = data->bytes_xfered;
540
541                                         return;
542                                 }
543
544                                 buffer = wbsd_sg_to_buffer(host);
545                         }
546                 }
547         }
548
549         /*
550          * This is a very dirty hack to solve a
551          * hardware problem. The chip doesn't trigger
552          * FIFO threshold interrupts properly.
553          */
554         if ((host->size - data->bytes_xfered) < 16)
555                 tasklet_schedule(&host->fifo_tasklet);
556 }
557
558 static void wbsd_fill_fifo(struct wbsd_host *host)
559 {
560         struct mmc_data *data = host->mrq->cmd->data;
561         char *buffer;
562         int i, fsr, fifo;
563
564         /*
565          * Check that we aren't being called after the
566          * entire buffer has been transfered.
567          */
568         if (data->bytes_xfered == host->size)
569                 return;
570
571         buffer = wbsd_sg_to_buffer(host) + host->offset;
572
573         /*
574          * Fill the fifo. This has a tendency to loop longer
575          * than the FIFO length (usually one block).
576          */
577         while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
578                 /*
579                  * The size field in the FSR is broken so we have to
580                  * do some guessing.
581                  */
582                 if (fsr & WBSD_FIFO_EMPTY)
583                         fifo = 0;
584                 else if (fsr & WBSD_FIFO_EMTHRE)
585                         fifo = 8;
586                 else
587                         fifo = 15;
588
589                 for (i = 16; i > fifo; i--) {
590                         outb(*buffer, host->base + WBSD_DFR);
591                         buffer++;
592                         host->offset++;
593                         host->remain--;
594
595                         data->bytes_xfered++;
596
597                         /*
598                          * Transfer done?
599                          */
600                         if (data->bytes_xfered == host->size)
601                                 return;
602
603                         /*
604                          * End of scatter list entry?
605                          */
606                         if (host->remain == 0) {
607                                 /*
608                                  * Get next entry. Check if last.
609                                  */
610                                 if (!wbsd_next_sg(host)) {
611                                         /*
612                                          * We should never reach this point.
613                                          * It means that we're trying to
614                                          * transfer more blocks than can fit
615                                          * into the scatter list.
616                                          */
617                                         BUG_ON(1);
618
619                                         host->size = data->bytes_xfered;
620
621                                         return;
622                                 }
623
624                                 buffer = wbsd_sg_to_buffer(host);
625                         }
626                 }
627         }
628
629         /*
630          * The controller stops sending interrupts for
631          * 'FIFO empty' under certain conditions. So we
632          * need to be a bit more pro-active.
633          */
634         tasklet_schedule(&host->fifo_tasklet);
635 }
636
637 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
638 {
639         u16 blksize;
640         u8 setup;
641         unsigned long dmaflags;
642
643         DBGF("blksz %04x blks %04x flags %08x\n",
644                 data->blksz, data->blocks, data->flags);
645         DBGF("tsac %d ms nsac %d clk\n",
646                 data->timeout_ns / 1000000, data->timeout_clks);
647
648         /*
649          * Calculate size.
650          */
651         host->size = data->blocks * data->blksz;
652
653         /*
654          * Check timeout values for overflow.
655          * (Yes, some cards cause this value to overflow).
656          */
657         if (data->timeout_ns > 127000000)
658                 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
659         else {
660                 wbsd_write_index(host, WBSD_IDX_TAAC,
661                         data->timeout_ns / 1000000);
662         }
663
664         if (data->timeout_clks > 255)
665                 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
666         else
667                 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
668
669         /*
670          * Inform the chip of how large blocks will be
671          * sent. It needs this to determine when to
672          * calculate CRC.
673          *
674          * Space for CRC must be included in the size.
675          * Two bytes are needed for each data line.
676          */
677         if (host->bus_width == MMC_BUS_WIDTH_1) {
678                 blksize = data->blksz + 2;
679
680                 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
681                 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
682         } else if (host->bus_width == MMC_BUS_WIDTH_4) {
683                 blksize = data->blksz + 2 * 4;
684
685                 wbsd_write_index(host, WBSD_IDX_PBSMSB,
686                         ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
687                 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
688         } else {
689                 data->error = MMC_ERR_INVALID;
690                 return;
691         }
692
693         /*
694          * Clear the FIFO. This is needed even for DMA
695          * transfers since the chip still uses the FIFO
696          * internally.
697          */
698         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
699         setup |= WBSD_FIFO_RESET;
700         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
701
702         /*
703          * DMA transfer?
704          */
705         if (host->dma >= 0) {
706                 /*
707                  * The buffer for DMA is only 64 kB.
708                  */
709                 BUG_ON(host->size > 0x10000);
710                 if (host->size > 0x10000) {
711                         data->error = MMC_ERR_INVALID;
712                         return;
713                 }
714
715                 /*
716                  * Transfer data from the SG list to
717                  * the DMA buffer.
718                  */
719                 if (data->flags & MMC_DATA_WRITE)
720                         wbsd_sg_to_dma(host, data);
721
722                 /*
723                  * Initialise the ISA DMA controller.
724                  */
725                 dmaflags = claim_dma_lock();
726                 disable_dma(host->dma);
727                 clear_dma_ff(host->dma);
728                 if (data->flags & MMC_DATA_READ)
729                         set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
730                 else
731                         set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
732                 set_dma_addr(host->dma, host->dma_addr);
733                 set_dma_count(host->dma, host->size);
734
735                 enable_dma(host->dma);
736                 release_dma_lock(dmaflags);
737
738                 /*
739                  * Enable DMA on the host.
740                  */
741                 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
742         } else {
743                 /*
744                  * This flag is used to keep printk
745                  * output to a minimum.
746                  */
747                 host->firsterr = 1;
748
749                 /*
750                  * Initialise the SG list.
751                  */
752                 wbsd_init_sg(host, data);
753
754                 /*
755                  * Turn off DMA.
756                  */
757                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
758
759                 /*
760                  * Set up FIFO threshold levels (and fill
761                  * buffer if doing a write).
762                  */
763                 if (data->flags & MMC_DATA_READ) {
764                         wbsd_write_index(host, WBSD_IDX_FIFOEN,
765                                 WBSD_FIFOEN_FULL | 8);
766                 } else {
767                         wbsd_write_index(host, WBSD_IDX_FIFOEN,
768                                 WBSD_FIFOEN_EMPTY | 8);
769                         wbsd_fill_fifo(host);
770                 }
771         }
772
773         data->error = MMC_ERR_NONE;
774 }
775
776 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
777 {
778         unsigned long dmaflags;
779         int count;
780         u8 status;
781
782         WARN_ON(host->mrq == NULL);
783
784         /*
785          * Send a stop command if needed.
786          */
787         if (data->stop)
788                 wbsd_send_command(host, data->stop);
789
790         /*
791          * Wait for the controller to leave data
792          * transfer state.
793          */
794         do {
795                 status = wbsd_read_index(host, WBSD_IDX_STATUS);
796         } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
797
798         /*
799          * DMA transfer?
800          */
801         if (host->dma >= 0) {
802                 /*
803                  * Disable DMA on the host.
804                  */
805                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
806
807                 /*
808                  * Turn of ISA DMA controller.
809                  */
810                 dmaflags = claim_dma_lock();
811                 disable_dma(host->dma);
812                 clear_dma_ff(host->dma);
813                 count = get_dma_residue(host->dma);
814                 release_dma_lock(dmaflags);
815
816                 /*
817                  * Any leftover data?
818                  */
819                 if (count) {
820                         printk(KERN_ERR "%s: Incomplete DMA transfer. "
821                                 "%d bytes left.\n",
822                                 mmc_hostname(host->mmc), count);
823
824                         data->error = MMC_ERR_FAILED;
825                 } else {
826                         /*
827                          * Transfer data from DMA buffer to
828                          * SG list.
829                          */
830                         if (data->flags & MMC_DATA_READ)
831                                 wbsd_dma_to_sg(host, data);
832
833                         data->bytes_xfered = host->size;
834                 }
835         }
836
837         DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
838
839         wbsd_request_end(host, host->mrq);
840 }
841
842 /*****************************************************************************\
843  *                                                                           *
844  * MMC layer callbacks                                                       *
845  *                                                                           *
846 \*****************************************************************************/
847
848 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
849 {
850         struct wbsd_host *host = mmc_priv(mmc);
851         struct mmc_command *cmd;
852
853         /*
854          * Disable tasklets to avoid a deadlock.
855          */
856         spin_lock_bh(&host->lock);
857
858         BUG_ON(host->mrq != NULL);
859
860         cmd = mrq->cmd;
861
862         host->mrq = mrq;
863
864         /*
865          * If there is no card in the slot then
866          * timeout immediatly.
867          */
868         if (!(host->flags & WBSD_FCARD_PRESENT)) {
869                 cmd->error = MMC_ERR_TIMEOUT;
870                 goto done;
871         }
872
873         /*
874          * Does the request include data?
875          */
876         if (cmd->data) {
877                 wbsd_prepare_data(host, cmd->data);
878
879                 if (cmd->data->error != MMC_ERR_NONE)
880                         goto done;
881         }
882
883         wbsd_send_command(host, cmd);
884
885         /*
886          * If this is a data transfer the request
887          * will be finished after the data has
888          * transfered.
889          */
890         if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
891                 /*
892                  * The hardware is so delightfully stupid that it has a list
893                  * of "data" commands. If a command isn't on this list, it'll
894                  * just go back to the idle state and won't send any data
895                  * interrupts.
896                  */
897                 switch (cmd->opcode) {
898                 case 11:
899                 case 17:
900                 case 18:
901                 case 20:
902                 case 24:
903                 case 25:
904                 case 26:
905                 case 27:
906                 case 30:
907                 case 42:
908                 case 56:
909                         break;
910
911                 /* ACMDs. We don't keep track of state, so we just treat them
912                  * like any other command. */
913                 case 51:
914                         break;
915
916                 default:
917 #ifdef CONFIG_MMC_DEBUG
918                         printk(KERN_WARNING "%s: Data command %d is not "
919                                 "supported by this controller.\n",
920                                 mmc_hostname(host->mmc), cmd->opcode);
921 #endif
922                         cmd->data->error = MMC_ERR_INVALID;
923
924                         if (cmd->data->stop)
925                                 wbsd_send_command(host, cmd->data->stop);
926
927                         goto done;
928                 };
929
930                 /*
931                  * Dirty fix for hardware bug.
932                  */
933                 if (host->dma == -1)
934                         tasklet_schedule(&host->fifo_tasklet);
935
936                 spin_unlock_bh(&host->lock);
937
938                 return;
939         }
940
941 done:
942         wbsd_request_end(host, mrq);
943
944         spin_unlock_bh(&host->lock);
945 }
946
947 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
948 {
949         struct wbsd_host *host = mmc_priv(mmc);
950         u8 clk, setup, pwr;
951
952         spin_lock_bh(&host->lock);
953
954         /*
955          * Reset the chip on each power off.
956          * Should clear out any weird states.
957          */
958         if (ios->power_mode == MMC_POWER_OFF)
959                 wbsd_init_device(host);
960
961         if (ios->clock >= 24000000)
962                 clk = WBSD_CLK_24M;
963         else if (ios->clock >= 16000000)
964                 clk = WBSD_CLK_16M;
965         else if (ios->clock >= 12000000)
966                 clk = WBSD_CLK_12M;
967         else
968                 clk = WBSD_CLK_375K;
969
970         /*
971          * Only write to the clock register when
972          * there is an actual change.
973          */
974         if (clk != host->clk) {
975                 wbsd_write_index(host, WBSD_IDX_CLK, clk);
976                 host->clk = clk;
977         }
978
979         /*
980          * Power up card.
981          */
982         if (ios->power_mode != MMC_POWER_OFF) {
983                 pwr = inb(host->base + WBSD_CSR);
984                 pwr &= ~WBSD_POWER_N;
985                 outb(pwr, host->base + WBSD_CSR);
986         }
987
988         /*
989          * MMC cards need to have pin 1 high during init.
990          * It wreaks havoc with the card detection though so
991          * that needs to be disabled.
992          */
993         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
994         if (ios->chip_select == MMC_CS_HIGH) {
995                 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
996                 setup |= WBSD_DAT3_H;
997                 host->flags |= WBSD_FIGNORE_DETECT;
998         } else {
999                 if (setup & WBSD_DAT3_H) {
1000                         setup &= ~WBSD_DAT3_H;
1001
1002                         /*
1003                          * We cannot resume card detection immediatly
1004                          * because of capacitance and delays in the chip.
1005                          */
1006                         mod_timer(&host->ignore_timer, jiffies + HZ / 100);
1007                 }
1008         }
1009         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
1010
1011         /*
1012          * Store bus width for later. Will be used when
1013          * setting up the data transfer.
1014          */
1015         host->bus_width = ios->bus_width;
1016
1017         spin_unlock_bh(&host->lock);
1018 }
1019
1020 static int wbsd_get_ro(struct mmc_host *mmc)
1021 {
1022         struct wbsd_host *host = mmc_priv(mmc);
1023         u8 csr;
1024
1025         spin_lock_bh(&host->lock);
1026
1027         csr = inb(host->base + WBSD_CSR);
1028         csr |= WBSD_MSLED;
1029         outb(csr, host->base + WBSD_CSR);
1030
1031         mdelay(1);
1032
1033         csr = inb(host->base + WBSD_CSR);
1034         csr &= ~WBSD_MSLED;
1035         outb(csr, host->base + WBSD_CSR);
1036
1037         spin_unlock_bh(&host->lock);
1038
1039         return csr & WBSD_WRPT;
1040 }
1041
1042 static const struct mmc_host_ops wbsd_ops = {
1043         .request        = wbsd_request,
1044         .set_ios        = wbsd_set_ios,
1045         .get_ro         = wbsd_get_ro,
1046 };
1047
1048 /*****************************************************************************\
1049  *                                                                           *
1050  * Interrupt handling                                                        *
1051  *                                                                           *
1052 \*****************************************************************************/
1053
1054 /*
1055  * Helper function to reset detection ignore
1056  */
1057
1058 static void wbsd_reset_ignore(unsigned long data)
1059 {
1060         struct wbsd_host *host = (struct wbsd_host *)data;
1061
1062         BUG_ON(host == NULL);
1063
1064         DBG("Resetting card detection ignore\n");
1065
1066         spin_lock_bh(&host->lock);
1067
1068         host->flags &= ~WBSD_FIGNORE_DETECT;
1069
1070         /*
1071          * Card status might have changed during the
1072          * blackout.
1073          */
1074         tasklet_schedule(&host->card_tasklet);
1075
1076         spin_unlock_bh(&host->lock);
1077 }
1078
1079 /*
1080  * Tasklets
1081  */
1082
1083 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
1084 {
1085         WARN_ON(!host->mrq);
1086         if (!host->mrq)
1087                 return NULL;
1088
1089         WARN_ON(!host->mrq->cmd);
1090         if (!host->mrq->cmd)
1091                 return NULL;
1092
1093         WARN_ON(!host->mrq->cmd->data);
1094         if (!host->mrq->cmd->data)
1095                 return NULL;
1096
1097         return host->mrq->cmd->data;
1098 }
1099
1100 static void wbsd_tasklet_card(unsigned long param)
1101 {
1102         struct wbsd_host *host = (struct wbsd_host *)param;
1103         u8 csr;
1104         int delay = -1;
1105
1106         spin_lock(&host->lock);
1107
1108         if (host->flags & WBSD_FIGNORE_DETECT) {
1109                 spin_unlock(&host->lock);
1110                 return;
1111         }
1112
1113         csr = inb(host->base + WBSD_CSR);
1114         WARN_ON(csr == 0xff);
1115
1116         if (csr & WBSD_CARDPRESENT) {
1117                 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1118                         DBG("Card inserted\n");
1119                         host->flags |= WBSD_FCARD_PRESENT;
1120
1121                         delay = 500;
1122                 }
1123         } else if (host->flags & WBSD_FCARD_PRESENT) {
1124                 DBG("Card removed\n");
1125                 host->flags &= ~WBSD_FCARD_PRESENT;
1126
1127                 if (host->mrq) {
1128                         printk(KERN_ERR "%s: Card removed during transfer!\n",
1129                                 mmc_hostname(host->mmc));
1130                         wbsd_reset(host);
1131
1132                         host->mrq->cmd->error = MMC_ERR_FAILED;
1133                         tasklet_schedule(&host->finish_tasklet);
1134                 }
1135
1136                 delay = 0;
1137         }
1138
1139         /*
1140          * Unlock first since we might get a call back.
1141          */
1142
1143         spin_unlock(&host->lock);
1144
1145         if (delay != -1)
1146                 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1147 }
1148
1149 static void wbsd_tasklet_fifo(unsigned long param)
1150 {
1151         struct wbsd_host *host = (struct wbsd_host *)param;
1152         struct mmc_data *data;
1153
1154         spin_lock(&host->lock);
1155
1156         if (!host->mrq)
1157                 goto end;
1158
1159         data = wbsd_get_data(host);
1160         if (!data)
1161                 goto end;
1162
1163         if (data->flags & MMC_DATA_WRITE)
1164                 wbsd_fill_fifo(host);
1165         else
1166                 wbsd_empty_fifo(host);
1167
1168         /*
1169          * Done?
1170          */
1171         if (host->size == data->bytes_xfered) {
1172                 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1173                 tasklet_schedule(&host->finish_tasklet);
1174         }
1175
1176 end:
1177         spin_unlock(&host->lock);
1178 }
1179
1180 static void wbsd_tasklet_crc(unsigned long param)
1181 {
1182         struct wbsd_host *host = (struct wbsd_host *)param;
1183         struct mmc_data *data;
1184
1185         spin_lock(&host->lock);
1186
1187         if (!host->mrq)
1188                 goto end;
1189
1190         data = wbsd_get_data(host);
1191         if (!data)
1192                 goto end;
1193
1194         DBGF("CRC error\n");
1195
1196         data->error = MMC_ERR_BADCRC;
1197
1198         tasklet_schedule(&host->finish_tasklet);
1199
1200 end:
1201         spin_unlock(&host->lock);
1202 }
1203
1204 static void wbsd_tasklet_timeout(unsigned long param)
1205 {
1206         struct wbsd_host *host = (struct wbsd_host *)param;
1207         struct mmc_data *data;
1208
1209         spin_lock(&host->lock);
1210
1211         if (!host->mrq)
1212                 goto end;
1213
1214         data = wbsd_get_data(host);
1215         if (!data)
1216                 goto end;
1217
1218         DBGF("Timeout\n");
1219
1220         data->error = MMC_ERR_TIMEOUT;
1221
1222         tasklet_schedule(&host->finish_tasklet);
1223
1224 end:
1225         spin_unlock(&host->lock);
1226 }
1227
1228 static void wbsd_tasklet_finish(unsigned long param)
1229 {
1230         struct wbsd_host *host = (struct wbsd_host *)param;
1231         struct mmc_data *data;
1232
1233         spin_lock(&host->lock);
1234
1235         WARN_ON(!host->mrq);
1236         if (!host->mrq)
1237                 goto end;
1238
1239         data = wbsd_get_data(host);
1240         if (!data)
1241                 goto end;
1242
1243         wbsd_finish_data(host, data);
1244
1245 end:
1246         spin_unlock(&host->lock);
1247 }
1248
1249 static void wbsd_tasklet_block(unsigned long param)
1250 {
1251         struct wbsd_host *host = (struct wbsd_host *)param;
1252         struct mmc_data *data;
1253
1254         spin_lock(&host->lock);
1255
1256         if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1257                 WBSD_CRC_OK) {
1258                 data = wbsd_get_data(host);
1259                 if (!data)
1260                         goto end;
1261
1262                 DBGF("CRC error\n");
1263
1264                 data->error = MMC_ERR_BADCRC;
1265
1266                 tasklet_schedule(&host->finish_tasklet);
1267         }
1268
1269 end:
1270         spin_unlock(&host->lock);
1271 }
1272
1273 /*
1274  * Interrupt handling
1275  */
1276
1277 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1278 {
1279         struct wbsd_host *host = dev_id;
1280         int isr;
1281
1282         isr = inb(host->base + WBSD_ISR);
1283
1284         /*
1285          * Was it actually our hardware that caused the interrupt?
1286          */
1287         if (isr == 0xff || isr == 0x00)
1288                 return IRQ_NONE;
1289
1290         host->isr |= isr;
1291
1292         /*
1293          * Schedule tasklets as needed.
1294          */
1295         if (isr & WBSD_INT_CARD)
1296                 tasklet_schedule(&host->card_tasklet);
1297         if (isr & WBSD_INT_FIFO_THRE)
1298                 tasklet_schedule(&host->fifo_tasklet);
1299         if (isr & WBSD_INT_CRC)
1300                 tasklet_hi_schedule(&host->crc_tasklet);
1301         if (isr & WBSD_INT_TIMEOUT)
1302                 tasklet_hi_schedule(&host->timeout_tasklet);
1303         if (isr & WBSD_INT_BUSYEND)
1304                 tasklet_hi_schedule(&host->block_tasklet);
1305         if (isr & WBSD_INT_TC)
1306                 tasklet_schedule(&host->finish_tasklet);
1307
1308         return IRQ_HANDLED;
1309 }
1310
1311 /*****************************************************************************\
1312  *                                                                           *
1313  * Device initialisation and shutdown                                        *
1314  *                                                                           *
1315 \*****************************************************************************/
1316
1317 /*
1318  * Allocate/free MMC structure.
1319  */
1320
1321 static int __devinit wbsd_alloc_mmc(struct device *dev)
1322 {
1323         struct mmc_host *mmc;
1324         struct wbsd_host *host;
1325
1326         /*
1327          * Allocate MMC structure.
1328          */
1329         mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1330         if (!mmc)
1331                 return -ENOMEM;
1332
1333         host = mmc_priv(mmc);
1334         host->mmc = mmc;
1335
1336         host->dma = -1;
1337
1338         /*
1339          * Set host parameters.
1340          */
1341         mmc->ops = &wbsd_ops;
1342         mmc->f_min = 375000;
1343         mmc->f_max = 24000000;
1344         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1345         mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1346
1347         spin_lock_init(&host->lock);
1348
1349         /*
1350          * Set up timers
1351          */
1352         init_timer(&host->ignore_timer);
1353         host->ignore_timer.data = (unsigned long)host;
1354         host->ignore_timer.function = wbsd_reset_ignore;
1355
1356         /*
1357          * Maximum number of segments. Worst case is one sector per segment
1358          * so this will be 64kB/512.
1359          */
1360         mmc->max_hw_segs = 128;
1361         mmc->max_phys_segs = 128;
1362
1363         /*
1364          * Maximum request size. Also limited by 64KiB buffer.
1365          */
1366         mmc->max_req_size = 65536;
1367
1368         /*
1369          * Maximum segment size. Could be one segment with the maximum number
1370          * of bytes.
1371          */
1372         mmc->max_seg_size = mmc->max_req_size;
1373
1374         /*
1375          * Maximum block size. We have 12 bits (= 4095) but have to subtract
1376          * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1377          */
1378         mmc->max_blk_size = 4087;
1379
1380         /*
1381          * Maximum block count. There is no real limit so the maximum
1382          * request size will be the only restriction.
1383          */
1384         mmc->max_blk_count = mmc->max_req_size;
1385
1386         dev_set_drvdata(dev, mmc);
1387
1388         return 0;
1389 }
1390
1391 static void __devexit wbsd_free_mmc(struct device *dev)
1392 {
1393         struct mmc_host *mmc;
1394         struct wbsd_host *host;
1395
1396         mmc = dev_get_drvdata(dev);
1397         if (!mmc)
1398                 return;
1399
1400         host = mmc_priv(mmc);
1401         BUG_ON(host == NULL);
1402
1403         del_timer_sync(&host->ignore_timer);
1404
1405         mmc_free_host(mmc);
1406
1407         dev_set_drvdata(dev, NULL);
1408 }
1409
1410 /*
1411  * Scan for known chip id:s
1412  */
1413
1414 static int __devinit wbsd_scan(struct wbsd_host *host)
1415 {
1416         int i, j, k;
1417         int id;
1418
1419         /*
1420          * Iterate through all ports, all codes to
1421          * find hardware that is in our known list.
1422          */
1423         for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1424                 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1425                         continue;
1426
1427                 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1428                         id = 0xFFFF;
1429
1430                         host->config = config_ports[i];
1431                         host->unlock_code = unlock_codes[j];
1432
1433                         wbsd_unlock_config(host);
1434
1435                         outb(WBSD_CONF_ID_HI, config_ports[i]);
1436                         id = inb(config_ports[i] + 1) << 8;
1437
1438                         outb(WBSD_CONF_ID_LO, config_ports[i]);
1439                         id |= inb(config_ports[i] + 1);
1440
1441                         wbsd_lock_config(host);
1442
1443                         for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1444                                 if (id == valid_ids[k]) {
1445                                         host->chip_id = id;
1446
1447                                         return 0;
1448                                 }
1449                         }
1450
1451                         if (id != 0xFFFF) {
1452                                 DBG("Unknown hardware (id %x) found at %x\n",
1453                                         id, config_ports[i]);
1454                         }
1455                 }
1456
1457                 release_region(config_ports[i], 2);
1458         }
1459
1460         host->config = 0;
1461         host->unlock_code = 0;
1462
1463         return -ENODEV;
1464 }
1465
1466 /*
1467  * Allocate/free io port ranges
1468  */
1469
1470 static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
1471 {
1472         if (base & 0x7)
1473                 return -EINVAL;
1474
1475         if (!request_region(base, 8, DRIVER_NAME))
1476                 return -EIO;
1477
1478         host->base = base;
1479
1480         return 0;
1481 }
1482
1483 static void __devexit wbsd_release_regions(struct wbsd_host *host)
1484 {
1485         if (host->base)
1486                 release_region(host->base, 8);
1487
1488         host->base = 0;
1489
1490         if (host->config)
1491                 release_region(host->config, 2);
1492
1493         host->config = 0;
1494 }
1495
1496 /*
1497  * Allocate/free DMA port and buffer
1498  */
1499
1500 static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
1501 {
1502         if (dma < 0)
1503                 return;
1504
1505         if (request_dma(dma, DRIVER_NAME))
1506                 goto err;
1507
1508         /*
1509          * We need to allocate a special buffer in
1510          * order for ISA to be able to DMA to it.
1511          */
1512         host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1513                 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1514         if (!host->dma_buffer)
1515                 goto free;
1516
1517         /*
1518          * Translate the address to a physical address.
1519          */
1520         host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1521                 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1522
1523         /*
1524          * ISA DMA must be aligned on a 64k basis.
1525          */
1526         if ((host->dma_addr & 0xffff) != 0)
1527                 goto kfree;
1528         /*
1529          * ISA cannot access memory above 16 MB.
1530          */
1531         else if (host->dma_addr >= 0x1000000)
1532                 goto kfree;
1533
1534         host->dma = dma;
1535
1536         return;
1537
1538 kfree:
1539         /*
1540          * If we've gotten here then there is some kind of alignment bug
1541          */
1542         BUG_ON(1);
1543
1544         dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1545                 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1546         host->dma_addr = (dma_addr_t)NULL;
1547
1548         kfree(host->dma_buffer);
1549         host->dma_buffer = NULL;
1550
1551 free:
1552         free_dma(dma);
1553
1554 err:
1555         printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1556                 "Falling back on FIFO.\n", dma);
1557 }
1558
1559 static void __devexit wbsd_release_dma(struct wbsd_host *host)
1560 {
1561         if (host->dma_addr) {
1562                 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1563                         WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1564         }
1565         kfree(host->dma_buffer);
1566         if (host->dma >= 0)
1567                 free_dma(host->dma);
1568
1569         host->dma = -1;
1570         host->dma_buffer = NULL;
1571         host->dma_addr = (dma_addr_t)NULL;
1572 }
1573
1574 /*
1575  * Allocate/free IRQ.
1576  */
1577
1578 static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
1579 {
1580         int ret;
1581
1582         /*
1583          * Allocate interrupt.
1584          */
1585
1586         ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1587         if (ret)
1588                 return ret;
1589
1590         host->irq = irq;
1591
1592         /*
1593          * Set up tasklets.
1594          */
1595         tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1596                         (unsigned long)host);
1597         tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1598                         (unsigned long)host);
1599         tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1600                         (unsigned long)host);
1601         tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1602                         (unsigned long)host);
1603         tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1604                         (unsigned long)host);
1605         tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1606                         (unsigned long)host);
1607
1608         return 0;
1609 }
1610
1611 static void __devexit wbsd_release_irq(struct wbsd_host *host)
1612 {
1613         if (!host->irq)
1614                 return;
1615
1616         free_irq(host->irq, host);
1617
1618         host->irq = 0;
1619
1620         tasklet_kill(&host->card_tasklet);
1621         tasklet_kill(&host->fifo_tasklet);
1622         tasklet_kill(&host->crc_tasklet);
1623         tasklet_kill(&host->timeout_tasklet);
1624         tasklet_kill(&host->finish_tasklet);
1625         tasklet_kill(&host->block_tasklet);
1626 }
1627
1628 /*
1629  * Allocate all resources for the host.
1630  */
1631
1632 static int __devinit wbsd_request_resources(struct wbsd_host *host,
1633         int base, int irq, int dma)
1634 {
1635         int ret;
1636
1637         /*
1638          * Allocate I/O ports.
1639          */
1640         ret = wbsd_request_region(host, base);
1641         if (ret)
1642                 return ret;
1643
1644         /*
1645          * Allocate interrupt.
1646          */
1647         ret = wbsd_request_irq(host, irq);
1648         if (ret)
1649                 return ret;
1650
1651         /*
1652          * Allocate DMA.
1653          */
1654         wbsd_request_dma(host, dma);
1655
1656         return 0;
1657 }
1658
1659 /*
1660  * Release all resources for the host.
1661  */
1662
1663 static void __devexit wbsd_release_resources(struct wbsd_host *host)
1664 {
1665         wbsd_release_dma(host);
1666         wbsd_release_irq(host);
1667         wbsd_release_regions(host);
1668 }
1669
1670 /*
1671  * Configure the resources the chip should use.
1672  */
1673
1674 static void wbsd_chip_config(struct wbsd_host *host)
1675 {
1676         wbsd_unlock_config(host);
1677
1678         /*
1679          * Reset the chip.
1680          */
1681         wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1682         wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1683
1684         /*
1685          * Select SD/MMC function.
1686          */
1687         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1688
1689         /*
1690          * Set up card detection.
1691          */
1692         wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1693
1694         /*
1695          * Configure chip
1696          */
1697         wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1698         wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1699
1700         wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1701
1702         if (host->dma >= 0)
1703                 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1704
1705         /*
1706          * Enable and power up chip.
1707          */
1708         wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1709         wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1710
1711         wbsd_lock_config(host);
1712 }
1713
1714 /*
1715  * Check that configured resources are correct.
1716  */
1717
1718 static int wbsd_chip_validate(struct wbsd_host *host)
1719 {
1720         int base, irq, dma;
1721
1722         wbsd_unlock_config(host);
1723
1724         /*
1725          * Select SD/MMC function.
1726          */
1727         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1728
1729         /*
1730          * Read configuration.
1731          */
1732         base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1733         base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1734
1735         irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1736
1737         dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1738
1739         wbsd_lock_config(host);
1740
1741         /*
1742          * Validate against given configuration.
1743          */
1744         if (base != host->base)
1745                 return 0;
1746         if (irq != host->irq)
1747                 return 0;
1748         if ((dma != host->dma) && (host->dma != -1))
1749                 return 0;
1750
1751         return 1;
1752 }
1753
1754 /*
1755  * Powers down the SD function
1756  */
1757
1758 static void wbsd_chip_poweroff(struct wbsd_host *host)
1759 {
1760         wbsd_unlock_config(host);
1761
1762         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1763         wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1764
1765         wbsd_lock_config(host);
1766 }
1767
1768 /*****************************************************************************\
1769  *                                                                           *
1770  * Devices setup and shutdown                                                *
1771  *                                                                           *
1772 \*****************************************************************************/
1773
1774 static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
1775         int pnp)
1776 {
1777         struct wbsd_host *host = NULL;
1778         struct mmc_host *mmc = NULL;
1779         int ret;
1780
1781         ret = wbsd_alloc_mmc(dev);
1782         if (ret)
1783                 return ret;
1784
1785         mmc = dev_get_drvdata(dev);
1786         host = mmc_priv(mmc);
1787
1788         /*
1789          * Scan for hardware.
1790          */
1791         ret = wbsd_scan(host);
1792         if (ret) {
1793                 if (pnp && (ret == -ENODEV)) {
1794                         printk(KERN_WARNING DRIVER_NAME
1795                                 ": Unable to confirm device presence. You may "
1796                                 "experience lock-ups.\n");
1797                 } else {
1798                         wbsd_free_mmc(dev);
1799                         return ret;
1800                 }
1801         }
1802
1803         /*
1804          * Request resources.
1805          */
1806         ret = wbsd_request_resources(host, base, irq, dma);
1807         if (ret) {
1808                 wbsd_release_resources(host);
1809                 wbsd_free_mmc(dev);
1810                 return ret;
1811         }
1812
1813         /*
1814          * See if chip needs to be configured.
1815          */
1816         if (pnp) {
1817                 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1818                         printk(KERN_WARNING DRIVER_NAME
1819                                 ": PnP active but chip not configured! "
1820                                 "You probably have a buggy BIOS. "
1821                                 "Configuring chip manually.\n");
1822                         wbsd_chip_config(host);
1823                 }
1824         } else
1825                 wbsd_chip_config(host);
1826
1827         /*
1828          * Power Management stuff. No idea how this works.
1829          * Not tested.
1830          */
1831 #ifdef CONFIG_PM
1832         if (host->config) {
1833                 wbsd_unlock_config(host);
1834                 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1835                 wbsd_lock_config(host);
1836         }
1837 #endif
1838         /*
1839          * Allow device to initialise itself properly.
1840          */
1841         mdelay(5);
1842
1843         /*
1844          * Reset the chip into a known state.
1845          */
1846         wbsd_init_device(host);
1847
1848         mmc_add_host(mmc);
1849
1850         printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
1851         if (host->chip_id != 0)
1852                 printk(" id %x", (int)host->chip_id);
1853         printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1854         if (host->dma >= 0)
1855                 printk(" dma %d", (int)host->dma);
1856         else
1857                 printk(" FIFO");
1858         if (pnp)
1859                 printk(" PnP");
1860         printk("\n");
1861
1862         return 0;
1863 }
1864
1865 static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1866 {
1867         struct mmc_host *mmc = dev_get_drvdata(dev);
1868         struct wbsd_host *host;
1869
1870         if (!mmc)
1871                 return;
1872
1873         host = mmc_priv(mmc);
1874
1875         mmc_remove_host(mmc);
1876
1877         /*
1878          * Power down the SD/MMC function.
1879          */
1880         if (!pnp)
1881                 wbsd_chip_poweroff(host);
1882
1883         wbsd_release_resources(host);
1884
1885         wbsd_free_mmc(dev);
1886 }
1887
1888 /*
1889  * Non-PnP
1890  */
1891
1892 static int __devinit wbsd_probe(struct platform_device *dev)
1893 {
1894         /* Use the module parameters for resources */
1895         return wbsd_init(&dev->dev, io, irq, dma, 0);
1896 }
1897
1898 static int __devexit wbsd_remove(struct platform_device *dev)
1899 {
1900         wbsd_shutdown(&dev->dev, 0);
1901
1902         return 0;
1903 }
1904
1905 /*
1906  * PnP
1907  */
1908
1909 #ifdef CONFIG_PNP
1910
1911 static int __devinit
1912 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1913 {
1914         int io, irq, dma;
1915
1916         /*
1917          * Get resources from PnP layer.
1918          */
1919         io = pnp_port_start(pnpdev, 0);
1920         irq = pnp_irq(pnpdev, 0);
1921         if (pnp_dma_valid(pnpdev, 0))
1922                 dma = pnp_dma(pnpdev, 0);
1923         else
1924                 dma = -1;
1925
1926         DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1927
1928         return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1929 }
1930
1931 static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
1932 {
1933         wbsd_shutdown(&dev->dev, 1);
1934 }
1935
1936 #endif /* CONFIG_PNP */
1937
1938 /*
1939  * Power management
1940  */
1941
1942 #ifdef CONFIG_PM
1943
1944 static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1945 {
1946         BUG_ON(host == NULL);
1947
1948         return mmc_suspend_host(host->mmc, state);
1949 }
1950
1951 static int wbsd_resume(struct wbsd_host *host)
1952 {
1953         BUG_ON(host == NULL);
1954
1955         wbsd_init_device(host);
1956
1957         return mmc_resume_host(host->mmc);
1958 }
1959
1960 static int wbsd_platform_suspend(struct platform_device *dev,
1961                                  pm_message_t state)
1962 {
1963         struct mmc_host *mmc = platform_get_drvdata(dev);
1964         struct wbsd_host *host;
1965         int ret;
1966
1967         if (mmc == NULL)
1968                 return 0;
1969
1970         DBGF("Suspending...\n");
1971
1972         host = mmc_priv(mmc);
1973
1974         ret = wbsd_suspend(host, state);
1975         if (ret)
1976                 return ret;
1977
1978         wbsd_chip_poweroff(host);
1979
1980         return 0;
1981 }
1982
1983 static int wbsd_platform_resume(struct platform_device *dev)
1984 {
1985         struct mmc_host *mmc = platform_get_drvdata(dev);
1986         struct wbsd_host *host;
1987
1988         if (mmc == NULL)
1989                 return 0;
1990
1991         DBGF("Resuming...\n");
1992
1993         host = mmc_priv(mmc);
1994
1995         wbsd_chip_config(host);
1996
1997         /*
1998          * Allow device to initialise itself properly.
1999          */
2000         mdelay(5);
2001
2002         return wbsd_resume(host);
2003 }
2004
2005 #ifdef CONFIG_PNP
2006
2007 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2008 {
2009         struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2010         struct wbsd_host *host;
2011
2012         if (mmc == NULL)
2013                 return 0;
2014
2015         DBGF("Suspending...\n");
2016
2017         host = mmc_priv(mmc);
2018
2019         return wbsd_suspend(host, state);
2020 }
2021
2022 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2023 {
2024         struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2025         struct wbsd_host *host;
2026
2027         if (mmc == NULL)
2028                 return 0;
2029
2030         DBGF("Resuming...\n");
2031
2032         host = mmc_priv(mmc);
2033
2034         /*
2035          * See if chip needs to be configured.
2036          */
2037         if (host->config != 0) {
2038                 if (!wbsd_chip_validate(host)) {
2039                         printk(KERN_WARNING DRIVER_NAME
2040                                 ": PnP active but chip not configured! "
2041                                 "You probably have a buggy BIOS. "
2042                                 "Configuring chip manually.\n");
2043                         wbsd_chip_config(host);
2044                 }
2045         }
2046
2047         /*
2048          * Allow device to initialise itself properly.
2049          */
2050         mdelay(5);
2051
2052         return wbsd_resume(host);
2053 }
2054
2055 #endif /* CONFIG_PNP */
2056
2057 #else /* CONFIG_PM */
2058
2059 #define wbsd_platform_suspend NULL
2060 #define wbsd_platform_resume NULL
2061
2062 #define wbsd_pnp_suspend NULL
2063 #define wbsd_pnp_resume NULL
2064
2065 #endif /* CONFIG_PM */
2066
2067 static struct platform_device *wbsd_device;
2068
2069 static struct platform_driver wbsd_driver = {
2070         .probe          = wbsd_probe,
2071         .remove         = __devexit_p(wbsd_remove),
2072
2073         .suspend        = wbsd_platform_suspend,
2074         .resume         = wbsd_platform_resume,
2075         .driver         = {
2076                 .name   = DRIVER_NAME,
2077         },
2078 };
2079
2080 #ifdef CONFIG_PNP
2081
2082 static struct pnp_driver wbsd_pnp_driver = {
2083         .name           = DRIVER_NAME,
2084         .id_table       = pnp_dev_table,
2085         .probe          = wbsd_pnp_probe,
2086         .remove         = __devexit_p(wbsd_pnp_remove),
2087
2088         .suspend        = wbsd_pnp_suspend,
2089         .resume         = wbsd_pnp_resume,
2090 };
2091
2092 #endif /* CONFIG_PNP */
2093
2094 /*
2095  * Module loading/unloading
2096  */
2097
2098 static int __init wbsd_drv_init(void)
2099 {
2100         int result;
2101
2102         printk(KERN_INFO DRIVER_NAME
2103                 ": Winbond W83L51xD SD/MMC card interface driver\n");
2104         printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2105
2106 #ifdef CONFIG_PNP
2107
2108         if (!nopnp) {
2109                 result = pnp_register_driver(&wbsd_pnp_driver);
2110                 if (result < 0)
2111                         return result;
2112         }
2113 #endif /* CONFIG_PNP */
2114
2115         if (nopnp) {
2116                 result = platform_driver_register(&wbsd_driver);
2117                 if (result < 0)
2118                         return result;
2119
2120                 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
2121                 if (!wbsd_device) {
2122                         platform_driver_unregister(&wbsd_driver);
2123                         return -ENOMEM;
2124                 }
2125
2126                 result = platform_device_add(wbsd_device);
2127                 if (result) {
2128                         platform_device_put(wbsd_device);
2129                         platform_driver_unregister(&wbsd_driver);
2130                         return result;
2131                 }
2132         }
2133
2134         return 0;
2135 }
2136
2137 static void __exit wbsd_drv_exit(void)
2138 {
2139 #ifdef CONFIG_PNP
2140
2141         if (!nopnp)
2142                 pnp_unregister_driver(&wbsd_pnp_driver);
2143
2144 #endif /* CONFIG_PNP */
2145
2146         if (nopnp) {
2147                 platform_device_unregister(wbsd_device);
2148
2149                 platform_driver_unregister(&wbsd_driver);
2150         }
2151
2152         DBG("unloaded\n");
2153 }
2154
2155 module_init(wbsd_drv_init);
2156 module_exit(wbsd_drv_exit);
2157 #ifdef CONFIG_PNP
2158 module_param(nopnp, uint, 0444);
2159 #endif
2160 module_param(io, uint, 0444);
2161 module_param(irq, uint, 0444);
2162 module_param(dma, int, 0444);
2163
2164 MODULE_LICENSE("GPL");
2165 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
2166 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2167
2168 #ifdef CONFIG_PNP
2169 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2170 #endif
2171 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2172 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2173 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");