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