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