This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / mmc / wbsd.c
1 /*
2  *  linux/drivers/mmc/wbsd.c
3  *
4  *  Copyright (C) 2004 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 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/blkdev.h>
20
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/protocol.h>
23
24 #include <asm/io.h>
25 #include <asm/dma.h>
26
27 #include "wbsd.h"
28
29 #define DRIVER_NAME "wbsd"
30 #define DRIVER_VERSION "1.0"
31
32 #ifdef CONFIG_MMC_DEBUG
33 #define DBG(x...) \
34         printk(KERN_DEBUG DRIVER_NAME ": " x)
35 #define DBGF(f, x...) \
36         printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__, ##x)
37 #else
38 #define DBG(x...)       do { } while (0)
39 #define DBGF(x...)      do { } while (0)
40 #endif
41
42 static unsigned int io = 0x248;
43 static unsigned int irq = 6;
44 static int dma = 2;
45
46 #ifdef CONFIG_MMC_DEBUG
47 void DBG_REG(int reg, u8 value)
48 {
49         int i;
50         
51         printk(KERN_DEBUG "wbsd: Register %d: 0x%02X %3d '%c' ",
52                 reg, (int)value, (int)value, (value < 0x20)?'.':value);
53         
54         for (i = 7;i >= 0;i--)
55         {
56                 if (value & (1 << i))
57                         printk("x");
58                 else
59                         printk(".");
60         }
61         
62         printk("\n");
63 }
64 #else
65 #define DBG_REG(r, v) do {}  while (0)
66 #endif
67
68 /*
69  * Basic functions
70  */
71
72 static inline void wbsd_unlock_config(struct wbsd_host* host)
73 {
74         outb(host->unlock_code, host->config);
75         outb(host->unlock_code, host->config);
76 }
77
78 static inline void wbsd_lock_config(struct wbsd_host* host)
79 {
80         outb(LOCK_CODE, host->config);
81 }
82
83 static inline void wbsd_write_config(struct wbsd_host* host, u8 reg, u8 value)
84 {
85         outb(reg, host->config);
86         outb(value, host->config + 1);
87 }
88
89 static inline u8 wbsd_read_config(struct wbsd_host* host, u8 reg)
90 {
91         outb(reg, host->config);
92         return inb(host->config + 1);
93 }
94
95 static inline void wbsd_write_index(struct wbsd_host* host, u8 index, u8 value)
96 {
97         outb(index, host->base + WBSD_IDXR);
98         outb(value, host->base + WBSD_DATAR);
99 }
100
101 static inline u8 wbsd_read_index(struct wbsd_host* host, u8 index)
102 {
103         outb(index, host->base + WBSD_IDXR);
104         return inb(host->base + WBSD_DATAR);
105 }
106
107 /*
108  * Common routines
109  */
110
111 static void wbsd_init_device(struct wbsd_host* host)
112 {
113         u8 setup, ier;
114         
115         /*
116          * Reset chip (SD/MMC part) and fifo.
117          */
118         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
119         setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
120         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
121         
122         /*
123          * Read back default clock.
124          */
125         host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
126
127         /*
128          * Power down port.
129          */
130         outb(WBSD_POWER_N, host->base + WBSD_CSR);
131         
132         /*
133          * Set maximum timeout.
134          */
135         wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
136         
137         /*
138          * Enable interesting interrupts.
139          */
140         ier = 0;
141         ier |= WBSD_EINT_CARD;
142         ier |= WBSD_EINT_FIFO_THRE;
143         ier |= WBSD_EINT_CCRC;
144         ier |= WBSD_EINT_TIMEOUT;
145         ier |= WBSD_EINT_CRC;
146         ier |= WBSD_EINT_TC;
147
148         outb(ier, host->base + WBSD_EIR);
149
150         /*
151          * Clear interrupts.
152          */
153         inb(host->base + WBSD_ISR);
154 }
155
156 static void wbsd_reset(struct wbsd_host* host)
157 {
158         u8 setup;
159         
160         printk(KERN_ERR DRIVER_NAME ": Resetting chip\n");
161         
162         /*
163          * Soft reset of chip (SD/MMC part).
164          */
165         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
166         setup |= WBSD_SOFT_RESET;
167         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
168 }
169
170 static void wbsd_request_end(struct wbsd_host* host, struct mmc_request* mrq)
171 {
172         unsigned long dmaflags;
173         
174         DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
175         
176         if (host->dma >= 0)
177         {
178                 /*
179                  * Release ISA DMA controller.
180                  */
181                 dmaflags = claim_dma_lock();
182                 disable_dma(host->dma);
183                 clear_dma_ff(host->dma);
184                 release_dma_lock(dmaflags);
185
186                 /*
187                  * Disable DMA on host.
188                  */
189                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
190         }
191         
192         host->mrq = NULL;
193
194         /*
195          * MMC layer might call back into the driver so first unlock.
196          */
197         spin_unlock(&host->lock);
198         mmc_request_done(host->mmc, mrq);
199         spin_lock(&host->lock);
200 }
201
202 /*
203  * Scatter/gather functions
204  */
205
206 static inline void wbsd_init_sg(struct wbsd_host* host, struct mmc_data* data)
207 {
208         struct request* req = data->req;
209         
210         /*
211          * Get info. about SG list from data structure.
212          */
213         host->cur_sg = data->sg;
214         host->num_sg = data->sg_len;
215
216         host->offset = 0;
217         host->remain = host->cur_sg->length;
218 }
219
220 static inline int wbsd_next_sg(struct wbsd_host* host)
221 {
222         /*
223          * Skip to next SG entry.
224          */
225         host->cur_sg++;
226         host->num_sg--;
227
228         /*
229          * Any entries left?
230          */
231         if (host->num_sg > 0)
232           {
233             host->offset = 0;
234             host->remain = host->cur_sg->length;
235           }
236         
237         return host->num_sg;
238 }
239
240 static inline char* wbsd_kmap_sg(struct wbsd_host* host)
241 {
242         return kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
243                 host->cur_sg->offset;
244 }
245
246 static inline void wbsd_kunmap_sg(struct wbsd_host* host)
247 {
248         kunmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
249 }
250
251 static inline void wbsd_sg_to_dma(struct wbsd_host* host, struct mmc_data* data)
252 {
253         unsigned int len, i, size;
254         struct scatterlist* sg;
255         char* dmabuf = host->dma_buffer;
256         char* sgbuf;
257         
258         size = host->size;
259         
260         sg = data->sg;
261         len = data->sg_len;
262         
263         /*
264          * Just loop through all entries. Size might not
265          * be the entire list though so make sure that
266          * we do not transfer too much.
267          */
268         for (i = 0;i < len;i++)
269         {
270                 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
271                 if (size < sg[i].length)
272                         memcpy(dmabuf, sgbuf, size);
273                 else
274                         memcpy(dmabuf, sgbuf, sg[i].length);
275                 kunmap_atomic(sg[i].page, KM_BIO_SRC_IRQ);
276                 dmabuf += sg[i].length;
277                 
278                 if (size < sg[i].length)
279                         size = 0;
280                 else
281                         size -= sg[i].length;
282         
283                 if (size == 0)
284                         break;
285         }
286         
287         /*
288          * Check that we didn't get a request to transfer
289          * more data than can fit into the SG list.
290          */
291         
292         BUG_ON(size != 0);
293         
294         host->size -= size;
295 }
296
297 static inline void wbsd_dma_to_sg(struct wbsd_host* host, struct mmc_data* data)
298 {
299         unsigned int len, i, size;
300         struct scatterlist* sg;
301         char* dmabuf = host->dma_buffer;
302         char* sgbuf;
303         
304         size = host->size;
305         
306         sg = data->sg;
307         len = data->sg_len;
308         
309         /*
310          * Just loop through all entries. Size might not
311          * be the entire list though so make sure that
312          * we do not transfer too much.
313          */
314         for (i = 0;i < len;i++)
315         {
316                 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
317                 if (size < sg[i].length)
318                         memcpy(sgbuf, dmabuf, size);
319                 else
320                         memcpy(sgbuf, dmabuf, sg[i].length);
321                 kunmap_atomic(sg[i].page, KM_BIO_SRC_IRQ);
322                 dmabuf += sg[i].length;
323                 
324                 if (size < sg[i].length)
325                         size = 0;
326                 else
327                         size -= sg[i].length;
328                 
329                 if (size == 0)
330                         break;
331         }
332         
333         /*
334          * Check that we didn't get a request to transfer
335          * more data than can fit into the SG list.
336          */
337         
338         BUG_ON(size != 0);
339         
340         host->size -= size;
341 }
342
343 /*
344  * Command handling
345  */
346  
347 static inline void wbsd_get_short_reply(struct wbsd_host* host,
348         struct mmc_command* cmd)
349 {
350         /*
351          * Correct response type?
352          */
353         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT)
354         {
355                 cmd->error = MMC_ERR_INVALID;
356                 return;
357         }
358         
359         cmd->resp[0] =
360                 wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
361         cmd->resp[0] |=
362                 wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
363         cmd->resp[0] |=
364                 wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
365         cmd->resp[0] |=
366                 wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
367         cmd->resp[1] =
368                 wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
369 }
370
371 static inline void wbsd_get_long_reply(struct wbsd_host* host,
372         struct mmc_command* cmd)
373 {
374         int i;
375         
376         /*
377          * Correct response type?
378          */
379         if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG)
380         {
381                 cmd->error = MMC_ERR_INVALID;
382                 return;
383         }
384         
385         for (i = 0;i < 4;i++)
386         {
387                 cmd->resp[i] =
388                         wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
389                 cmd->resp[i] |=
390                         wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
391                 cmd->resp[i] |=
392                         wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
393                 cmd->resp[i] |=
394                         wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
395         }
396 }
397
398 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs);
399
400 static void wbsd_send_command(struct wbsd_host* host, struct mmc_command* cmd)
401 {
402         int i;
403         u8 status, eir, isr;
404         
405         DBGF("Sending cmd (%x)\n", cmd->opcode);
406
407         /*
408          * Disable interrupts as the interrupt routine
409          * will destroy the contents of ISR.
410          */
411         eir = inb(host->base + WBSD_EIR);
412         outb(0, host->base + WBSD_EIR);
413         
414         /*
415          * Send the command (CRC calculated by host).
416          */
417         outb(cmd->opcode, host->base + WBSD_CMDR);
418         for (i = 3;i >= 0;i--)
419                 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
420         
421         cmd->error = MMC_ERR_NONE;
422         
423         /*
424          * Wait for the request to complete.
425          */
426         do {
427                 status = wbsd_read_index(host, WBSD_IDX_STATUS);
428         } while (status & WBSD_CARDTRAFFIC);
429
430         /*
431          * Do we expect a reply?
432          */
433         if ((cmd->flags & MMC_RSP_MASK) != MMC_RSP_NONE)
434         {
435                 /*
436                  * Read back status.
437                  */
438                 isr = inb(host->base + WBSD_ISR);
439                 
440                 /* Card removed? */
441                 if (isr & WBSD_INT_CARD)
442                         cmd->error = MMC_ERR_TIMEOUT;
443                 /* Timeout? */
444                 else if (isr & WBSD_INT_TIMEOUT)
445                         cmd->error = MMC_ERR_TIMEOUT;
446                 /* CRC? */
447                 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
448                         cmd->error = MMC_ERR_BADCRC;
449                 /* All ok */
450                 else
451                 {
452                         if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_SHORT)
453                                 wbsd_get_short_reply(host, cmd);
454                         else
455                                 wbsd_get_long_reply(host, cmd);
456                 }
457         }
458
459         /*
460          * Restore interrupt mask to previous value.
461          */
462         outb(eir, host->base + WBSD_EIR);
463         
464         /*
465          * Call the interrupt routine to jump start
466          * interrupts.
467          */
468         wbsd_irq(0, host, NULL);
469
470         DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
471 }
472
473 /*
474  * Data functions
475  */
476
477 static void wbsd_empty_fifo(struct wbsd_host* host)
478 {
479         struct mmc_data* data = host->mrq->cmd->data;
480         char* buffer;
481         
482         /*
483          * Handle excessive data.
484          */
485         if (data->bytes_xfered == host->size)
486                 return;
487         
488         buffer = wbsd_kmap_sg(host) + host->offset;
489
490         /*
491          * Drain the fifo. This has a tendency to loop longer
492          * than the FIFO length (usually one block).
493          */
494         while (!(inb(host->base + WBSD_FSR) & WBSD_FIFO_EMPTY))
495         {
496                 *buffer = inb(host->base + WBSD_DFR);
497                 buffer++;
498                 host->offset++;
499                 host->remain--;
500
501                 data->bytes_xfered++;
502                 
503                 /*
504                  * Transfer done?
505                  */
506                 if (data->bytes_xfered == host->size)
507                 {
508                         wbsd_kunmap_sg(host);                           
509                         return;
510                 }
511                 
512                 /*
513                  * End of scatter list entry?
514                  */
515                 if (host->remain == 0)
516                 {
517                         wbsd_kunmap_sg(host);
518                         
519                         /*
520                          * Get next entry. Check if last.
521                          */
522                         if (!wbsd_next_sg(host))
523                         {
524                                 /*
525                                  * We should never reach this point.
526                                  * It means that we're trying to
527                                  * transfer more blocks than can fit
528                                  * into the scatter list.
529                                  */
530                                 BUG_ON(1);
531                                 
532                                 host->size = data->bytes_xfered;
533                                 
534                                 return;
535                         }
536                         
537                         buffer = wbsd_kmap_sg(host);
538                 }
539         }
540         
541         wbsd_kunmap_sg(host);
542 }
543
544 static void wbsd_fill_fifo(struct wbsd_host* host)
545 {
546         struct mmc_data* data = host->mrq->cmd->data;
547         char* buffer;
548         
549         /*
550          * Check that we aren't being called after the
551          * entire buffer has been transfered.
552          */
553         if (data->bytes_xfered == host->size)
554                 return;
555
556         buffer = wbsd_kmap_sg(host) + host->offset;
557
558         /*
559          * Fill the fifo. This has a tendency to loop longer
560          * than the FIFO length (usually one block).
561          */
562         while (!(inb(host->base + WBSD_FSR) & WBSD_FIFO_FULL))
563         {
564                 outb(*buffer, host->base + WBSD_DFR);
565                 buffer++;
566                 host->offset++;
567                 host->remain--;
568                 
569                 data->bytes_xfered++;
570                 
571                 /*
572                  * Transfer done?
573                  */
574                 if (data->bytes_xfered == host->size)
575                 {
576                         wbsd_kunmap_sg(host);
577                         return;
578                 }
579
580                 /*
581                  * End of scatter list entry?
582                  */
583                 if (host->remain == 0)
584                 {
585                         wbsd_kunmap_sg(host);
586                         
587                         /*
588                          * Get next entry. Check if last.
589                          */
590                         if (!wbsd_next_sg(host))
591                         {
592                                 /*
593                                  * We should never reach this point.
594                                  * It means that we're trying to
595                                  * transfer more blocks than can fit
596                                  * into the scatter list.
597                                  */
598                                 BUG_ON(1);
599                                 
600                                 host->size = data->bytes_xfered;
601                                 
602                                 return;
603                         }
604                         
605                         buffer = wbsd_kmap_sg(host);
606                 }
607         }
608         
609         wbsd_kunmap_sg(host);
610 }
611
612 static void wbsd_prepare_data(struct wbsd_host* host, struct mmc_data* data)
613 {
614         u16 blksize;
615         u8 setup;
616         unsigned long dmaflags;
617
618         DBGF("blksz %04x blks %04x flags %08x\n",
619                 1 << data->blksz_bits, data->blocks, data->flags);
620         DBGF("tsac %d ms nsac %d clk\n",
621                 data->timeout_ns / 1000000, data->timeout_clks);
622         
623         /*
624          * Calculate size.
625          */
626         host->size = data->blocks << data->blksz_bits;
627
628         /*
629          * Check timeout values for overflow.
630          * (Yes, some cards cause this value to overflow).
631          */
632         if (data->timeout_ns > 127000000)
633                 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
634         else
635                 wbsd_write_index(host, WBSD_IDX_TAAC, data->timeout_ns/1000000);
636         
637         if (data->timeout_clks > 255)
638                 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
639         else
640                 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
641         
642         /*
643          * Inform the chip of how large blocks will be
644          * sent. It needs this to determine when to
645          * calculate CRC.
646          *
647          * Space for CRC must be included in the size.
648          */
649         blksize = (1 << data->blksz_bits) + 2;
650         
651         wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
652         wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
653
654         /*
655          * Clear the FIFO. This is needed even for DMA
656          * transfers since the chip still uses the FIFO
657          * internally.
658          */
659         setup = wbsd_read_index(host, WBSD_IDX_SETUP);
660         setup |= WBSD_FIFO_RESET;
661         wbsd_write_index(host, WBSD_IDX_SETUP, setup);
662         
663         /*
664          * DMA transfer?
665          */
666         if (host->dma >= 0)
667         {       
668                 /*
669                  * The buffer for DMA is only 64 kB.
670                  */
671                 BUG_ON(host->size > 0x10000);
672                 if (host->size > 0x10000)
673                 {
674                         data->error = MMC_ERR_INVALID;
675                         return;
676                 }
677                 
678                 /*
679                  * Transfer data from the SG list to
680                  * the DMA buffer.
681                  */
682                 if (data->flags & MMC_DATA_WRITE)
683                         wbsd_sg_to_dma(host, data);
684                 
685                 /*
686                  * Initialise the ISA DMA controller.
687                  */     
688                 dmaflags = claim_dma_lock();
689                 disable_dma(host->dma);
690                 clear_dma_ff(host->dma);
691                 if (data->flags & MMC_DATA_READ)
692                         set_dma_mode(host->dma, DMA_MODE_READ);
693                 else
694                         set_dma_mode(host->dma, DMA_MODE_WRITE);
695                 set_dma_addr(host->dma, host->dma_addr);
696                 set_dma_count(host->dma, host->size);
697
698                 enable_dma(host->dma);
699                 release_dma_lock(dmaflags);
700
701                 /*
702                  * Enable DMA on the host.
703                  */
704                 wbsd_write_index(host, WBSD_IDX_DMA,
705                         WBSD_DMA_SINGLE | WBSD_DMA_ENABLE);
706         }
707         else
708         {
709                 /*
710                  * This flag is used to keep printk
711                  * output to a minimum.
712                  */
713                 host->firsterr = 1;
714                 
715                 /*
716                  * Initialise the SG list.
717                  */
718                 wbsd_init_sg(host, data);
719         
720                 /*
721                  * Turn off DMA.
722                  */
723                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
724         
725                 /*
726                  * Set up FIFO threshold levels (and fill
727                  * buffer if doing a write).
728                  */
729                 if (data->flags & MMC_DATA_READ)
730                 {
731                         wbsd_write_index(host, WBSD_IDX_FIFOEN,
732                                 WBSD_FIFOEN_FULL | 8);
733                 }
734                 else
735                 {
736                         wbsd_write_index(host, WBSD_IDX_FIFOEN,
737                                 WBSD_FIFOEN_EMPTY | 8);
738                         wbsd_fill_fifo(host);
739                 }
740         }       
741                 
742         data->error = MMC_ERR_NONE;
743 }
744
745 static void wbsd_finish_data(struct wbsd_host* host, struct mmc_data* data)
746 {
747         unsigned long dmaflags;
748         int count;
749         
750         WARN_ON(host->mrq == NULL);
751
752         /*
753          * Send a stop command if needed.
754          */
755         if (data->stop)
756                 wbsd_send_command(host, data->stop);
757         
758         /*
759          * DMA transfer?
760          */
761         if (host->dma >= 0)
762         {
763                 /*
764                  * Disable DMA on the host.
765                  */
766                 wbsd_write_index(host, WBSD_IDX_DMA, 0);
767                 
768                 /*
769                  * Turn of ISA DMA controller.
770                  */
771                 dmaflags = claim_dma_lock();
772                 disable_dma(host->dma);
773                 clear_dma_ff(host->dma);
774                 count = get_dma_residue(host->dma);
775                 release_dma_lock(dmaflags);
776                 
777                 /*
778                  * Any leftover data?
779                  */
780                 if (count)
781                 {
782                         printk(KERN_ERR DRIVER_NAME ": Incomplete DMA "
783                                 "transfer. %d bytes left.\n", count);
784                         
785                         data->error = MMC_ERR_FAILED;
786                 }
787                 else
788                 {
789                         /*
790                          * Transfer data from DMA buffer to
791                          * SG list.
792                          */
793                         if (data->flags & MMC_DATA_READ)
794                                 wbsd_dma_to_sg(host, data);
795                         
796                         data->bytes_xfered = host->size;
797                 }
798         }
799         
800         DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
801         
802         wbsd_request_end(host, host->mrq);
803 }
804
805 /*
806  * MMC Callbacks
807  */
808
809 static void wbsd_request(struct mmc_host* mmc, struct mmc_request* mrq)
810 {
811         struct wbsd_host* host = mmc_priv(mmc);
812         struct mmc_command* cmd;
813
814         /*
815          * Disable tasklets to avoid a deadlock.
816          */
817         spin_lock_bh(&host->lock);
818
819         BUG_ON(host->mrq != NULL);
820
821         cmd = mrq->cmd;
822
823         host->mrq = mrq;
824         
825         /*
826          * If there is no card in the slot then
827          * timeout immediatly.
828          */
829         if (!(inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT))
830         {
831                 cmd->error = MMC_ERR_TIMEOUT;
832                 goto done;
833         }
834
835         /*
836          * Does the request include data?
837          */
838         if (cmd->data)
839         {
840                 wbsd_prepare_data(host, cmd->data);
841                 
842                 if (cmd->data->error != MMC_ERR_NONE)
843                         goto done;
844         }
845         
846         wbsd_send_command(host, cmd);
847
848         /*
849          * If this is a data transfer the request
850          * will be finished after the data has
851          * transfered.
852          */     
853         if (cmd->data && (cmd->error == MMC_ERR_NONE))
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         DBGF("clock %uHz busmode %u powermode %u Vdd %u\n",
872                 ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
873
874         spin_lock_bh(&host->lock);
875
876         /*
877          * Reset the chip on each power off.
878          * Should clear out any weird states.
879          */
880         if (ios->power_mode == MMC_POWER_OFF)
881                 wbsd_init_device(host);
882         
883         if (ios->clock >= 24000000)
884                 clk = WBSD_CLK_24M;
885         else if (ios->clock >= 16000000)
886                 clk = WBSD_CLK_16M;
887         else if (ios->clock >= 12000000)
888                 clk = WBSD_CLK_12M;
889         else
890                 clk = WBSD_CLK_375K;
891
892         /*
893          * Only write to the clock register when
894          * there is an actual change.
895          */
896         if (clk != host->clk)
897         {
898                 wbsd_write_index(host, WBSD_IDX_CLK, clk);
899                 host->clk = clk;
900         }
901
902         if (ios->power_mode != MMC_POWER_OFF)
903         {
904                 /*
905                  * Power up card.
906                  */
907                 pwr = inb(host->base + WBSD_CSR);
908                 pwr &= ~WBSD_POWER_N;
909                 outb(pwr, host->base + WBSD_CSR);
910
911                 /*
912                  * This behaviour is stolen from the
913                  * Windows driver. Don't know why, but
914                  * it is needed.
915                  */
916                 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
917                 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
918                         setup |= WBSD_DAT3_H;
919                 else
920                         setup &= ~WBSD_DAT3_H;
921                 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
922
923                 mdelay(1);
924         }
925
926         spin_unlock_bh(&host->lock);
927 }
928
929 /*
930  * Tasklets
931  */
932
933 inline static struct mmc_data* wbsd_get_data(struct wbsd_host* host)
934 {
935         WARN_ON(!host->mrq);
936         if (!host->mrq)
937                 return NULL;
938
939         WARN_ON(!host->mrq->cmd);
940         if (!host->mrq->cmd)
941                 return NULL;
942
943         WARN_ON(!host->mrq->cmd->data);
944         if (!host->mrq->cmd->data)
945                 return NULL;
946         
947         return host->mrq->cmd->data;
948 }
949
950 static void wbsd_tasklet_card(unsigned long param)
951 {
952         struct wbsd_host* host = (struct wbsd_host*)param;
953         u8 csr;
954         
955         spin_lock(&host->lock);
956         
957         csr = inb(host->base + WBSD_CSR);
958         WARN_ON(csr == 0xff);
959         
960         if (csr & WBSD_CARDPRESENT)
961                 DBG("Card inserted\n");
962         else
963         {
964                 DBG("Card removed\n");
965                 
966                 if (host->mrq)
967                 {
968                         printk(KERN_ERR DRIVER_NAME
969                                 ": Card removed during transfer!\n");
970                         wbsd_reset(host);
971                         
972                         host->mrq->cmd->error = MMC_ERR_FAILED;
973                         tasklet_schedule(&host->finish_tasklet);
974                 }
975         }
976         
977         /*
978          * Unlock first since we might get a call back.
979          */
980         spin_unlock(&host->lock);
981
982         mmc_detect_change(host->mmc);
983 }
984
985 static void wbsd_tasklet_fifo(unsigned long param)
986 {
987         struct wbsd_host* host = (struct wbsd_host*)param;
988         struct mmc_data* data;
989         
990         spin_lock(&host->lock);
991                 
992         if (!host->mrq)
993                 goto end;
994         
995         data = wbsd_get_data(host);
996         if (!data)
997                 goto end;
998
999         if (data->flags & MMC_DATA_WRITE)
1000                 wbsd_fill_fifo(host);
1001         else
1002                 wbsd_empty_fifo(host);
1003
1004         /*
1005          * Done?
1006          */
1007         if (host->size == data->bytes_xfered)
1008         {
1009                 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1010                 tasklet_schedule(&host->finish_tasklet);
1011         }
1012
1013 end:    
1014         spin_unlock(&host->lock);
1015 }
1016
1017 static void wbsd_tasklet_crc(unsigned long param)
1018 {
1019         struct wbsd_host* host = (struct wbsd_host*)param;
1020         struct mmc_data* data;
1021         
1022         spin_lock(&host->lock);
1023         
1024         WARN_ON(!host->mrq);
1025         if (!host->mrq)
1026                 goto end;
1027         
1028         data = wbsd_get_data(host);
1029         if (!data)
1030                 goto end;
1031         
1032         DBGF("CRC error\n");
1033
1034         data->error = MMC_ERR_BADCRC;
1035         
1036         tasklet_schedule(&host->finish_tasklet);
1037
1038 end:            
1039         spin_unlock(&host->lock);
1040 }
1041
1042 static void wbsd_tasklet_timeout(unsigned long param)
1043 {
1044         struct wbsd_host* host = (struct wbsd_host*)param;
1045         struct mmc_data* data;
1046         
1047         spin_lock(&host->lock);
1048         
1049         WARN_ON(!host->mrq);
1050         if (!host->mrq)
1051                 goto end;
1052         
1053         data = wbsd_get_data(host);
1054         if (!data)
1055                 goto end;
1056         
1057         DBGF("Timeout\n");
1058
1059         data->error = MMC_ERR_TIMEOUT;
1060         
1061         tasklet_schedule(&host->finish_tasklet);
1062
1063 end:    
1064         spin_unlock(&host->lock);
1065 }
1066
1067 static void wbsd_tasklet_finish(unsigned long param)
1068 {
1069         struct wbsd_host* host = (struct wbsd_host*)param;
1070         struct mmc_data* data;
1071         
1072         spin_lock(&host->lock);
1073         
1074         WARN_ON(!host->mrq);
1075         if (!host->mrq)
1076                 goto end;
1077         
1078         data = wbsd_get_data(host);
1079         if (!data)
1080                 goto end;
1081
1082         wbsd_finish_data(host, data);
1083         
1084 end:    
1085         spin_unlock(&host->lock);
1086 }
1087
1088 static void wbsd_tasklet_block(unsigned long param)
1089 {
1090         struct wbsd_host* host = (struct wbsd_host*)param;
1091         struct mmc_data* data;
1092         
1093         spin_lock(&host->lock);
1094
1095         if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
1096                 WBSD_CRC_OK)
1097         {
1098                 data = wbsd_get_data(host);
1099                 if (!data)
1100                         goto end;
1101                 
1102                 DBGF("CRC error\n");
1103
1104                 data->error = MMC_ERR_BADCRC;
1105         
1106                 tasklet_schedule(&host->finish_tasklet);
1107         }
1108
1109 end:    
1110         spin_unlock(&host->lock);
1111 }
1112
1113 /*
1114  * Interrupt handling
1115  */
1116
1117 static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1118 {
1119         struct wbsd_host* host = dev_id;
1120         int isr;
1121         
1122         isr = inb(host->base + WBSD_ISR);
1123
1124         /*
1125          * Was it actually our hardware that caused the interrupt?
1126          */
1127         if (isr == 0xff || isr == 0x00)
1128                 return IRQ_NONE;
1129
1130         /*
1131          * Schedule tasklets as needed.
1132          */
1133         if (isr & WBSD_INT_CARD)
1134                 tasklet_schedule(&host->card_tasklet);
1135         if (isr & WBSD_INT_FIFO_THRE)
1136                 tasklet_hi_schedule(&host->fifo_tasklet);
1137         if (isr & WBSD_INT_CRC)
1138                 tasklet_hi_schedule(&host->crc_tasklet);
1139         if (isr & WBSD_INT_TIMEOUT)
1140                 tasklet_hi_schedule(&host->timeout_tasklet);
1141         if (isr & WBSD_INT_BUSYEND)
1142                 tasklet_hi_schedule(&host->block_tasklet);
1143         if (isr & WBSD_INT_TC)
1144                 tasklet_schedule(&host->finish_tasklet);
1145         
1146         return IRQ_HANDLED;
1147 }
1148
1149 /*
1150  * Support functions for probe
1151  */
1152
1153 static int wbsd_scan(struct wbsd_host* host)
1154 {
1155         int i, j, k;
1156         int id;
1157         
1158         /*
1159          * Iterate through all ports, all codes to
1160          * find hardware that is in our known list.
1161          */
1162         for (i = 0;i < sizeof(config_ports)/sizeof(int);i++)
1163         {
1164                 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1165                         continue;
1166                         
1167                 for (j = 0;j < sizeof(unlock_codes)/sizeof(int);j++)
1168                 {
1169                         id = 0xFFFF;
1170                         
1171                         outb(unlock_codes[j], config_ports[i]);
1172                         outb(unlock_codes[j], config_ports[i]);
1173                         
1174                         outb(WBSD_CONF_ID_HI, config_ports[i]);
1175                         id = inb(config_ports[i] + 1) << 8;
1176
1177                         outb(WBSD_CONF_ID_LO, config_ports[i]);
1178                         id |= inb(config_ports[i] + 1);
1179                         
1180                         for (k = 0;k < sizeof(valid_ids)/sizeof(int);k++)
1181                         {
1182                                 if (id == valid_ids[k])
1183                                 {                               
1184                                         host->chip_id = id;
1185                                         host->config = config_ports[i];
1186                                         host->unlock_code = unlock_codes[i];
1187                                 
1188                                         return 0;
1189                                 }
1190                         }
1191                         
1192                         if (id != 0xFFFF)
1193                         {
1194                                 DBG("Unknown hardware (id %x) found at %x\n",
1195                                         id, config_ports[i]);
1196                         }
1197
1198                         outb(LOCK_CODE, config_ports[i]);
1199                 }
1200                 
1201                 release_region(config_ports[i], 2);
1202         }
1203         
1204         return -ENODEV;
1205 }
1206
1207 static int wbsd_request_regions(struct wbsd_host* host)
1208 {
1209         if (io & 0x7)
1210                 return -EINVAL;
1211         
1212         if (!request_region(io, 8, DRIVER_NAME))
1213                 return -EIO;
1214         
1215         host->base = io;
1216                 
1217         return 0;
1218 }
1219
1220 static void wbsd_release_regions(struct wbsd_host* host)
1221 {
1222         if (host->base)
1223                 release_region(host->base, 8);
1224
1225         if (host->config)
1226                 release_region(host->config, 2);
1227 }
1228
1229 static void wbsd_init_dma(struct wbsd_host* host)
1230 {
1231         host->dma = -1;
1232         
1233         if (dma < 0)
1234                 return;
1235         
1236         if (request_dma(dma, DRIVER_NAME))
1237                 goto err;
1238         
1239         /*
1240          * We need to allocate a special buffer in
1241          * order for ISA to be able to DMA to it.
1242          */
1243         host->dma_buffer = kmalloc(65536,
1244                 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1245         if (!host->dma_buffer)
1246                 goto free;
1247
1248         /*
1249          * Translate the address to a physical address.
1250          */
1251         host->dma_addr = isa_virt_to_bus(host->dma_buffer);
1252                         
1253         /*
1254          * ISA DMA must be aligned on a 64k basis.
1255          */
1256         if ((host->dma_addr & 0xffff) != 0)
1257                 goto kfree;
1258         /*
1259          * ISA cannot access memory above 16 MB.
1260          */
1261         else if (host->dma_addr >= 0x1000000)
1262                 goto kfree;
1263
1264         host->dma = dma;
1265         
1266         return;
1267         
1268 kfree:
1269         /*
1270          * If we've gotten here then there is some kind of alignment bug
1271          */
1272         BUG_ON(1);
1273         
1274         kfree(host->dma_buffer);
1275         host->dma_buffer = NULL;
1276
1277 free:
1278         free_dma(dma);
1279
1280 err:
1281         printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1282                 "Falling back on FIFO.\n", dma);
1283 }
1284
1285 static struct mmc_host_ops wbsd_ops = {
1286         .request        = wbsd_request,
1287         .set_ios        = wbsd_set_ios,
1288 };
1289
1290 /*
1291  * Device probe
1292  */
1293
1294 static int wbsd_probe(struct device* dev)
1295 {
1296         struct wbsd_host* host = NULL;
1297         struct mmc_host* mmc = NULL;
1298         int ret;
1299         
1300         /*
1301          * Allocate MMC structure.
1302          */
1303         mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1304         if (!mmc)
1305                 return -ENOMEM;
1306         
1307         host = mmc_priv(mmc);
1308         host->mmc = mmc;
1309         
1310         /*
1311          * Scan for hardware.
1312          */
1313         ret = wbsd_scan(host);
1314         if (ret)
1315                 goto freemmc;
1316
1317         /*
1318          * Reset the chip.
1319          */     
1320         wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1321         wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1322
1323         /*
1324          * Allocate I/O ports.
1325          */
1326         ret = wbsd_request_regions(host);
1327         if (ret)
1328                 goto release;
1329
1330         /*
1331          * Set host parameters.
1332          */
1333         mmc->ops = &wbsd_ops;
1334         mmc->f_min = 375000;
1335         mmc->f_max = 24000000;
1336         mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1337         
1338         spin_lock_init(&host->lock);
1339
1340         /*
1341          * Select SD/MMC function.
1342          */
1343         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1344         
1345         /*
1346          * Set up card detection.
1347          */
1348         wbsd_write_config(host, WBSD_CONF_PINS, 0x02);
1349         
1350         /*
1351          * Configure I/O port.
1352          */
1353         wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1354         wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1355
1356         /*
1357          * Allocate interrupt.
1358          */
1359         ret = request_irq(irq, wbsd_irq, SA_SHIRQ, DRIVER_NAME, host);
1360         if (ret)
1361                 goto release;
1362         
1363         host->irq = irq;
1364         
1365         /*
1366          * Set up tasklets.
1367          */
1368         tasklet_init(&host->card_tasklet, wbsd_tasklet_card, (unsigned long)host);
1369         tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo, (unsigned long)host);
1370         tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc, (unsigned long)host);
1371         tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout, (unsigned long)host);
1372         tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish, (unsigned long)host);
1373         tasklet_init(&host->block_tasklet, wbsd_tasklet_block, (unsigned long)host);
1374         
1375         /*
1376          * Configure interrupt.
1377          */
1378         wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1379         
1380         /*
1381          * Allocate DMA.
1382          */
1383         wbsd_init_dma(host);
1384         
1385         /*
1386          * If all went well, then configure DMA.
1387          */
1388         if (host->dma >= 0)
1389                 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1390         
1391         /*
1392          * Maximum number of segments. Worst case is one sector per segment
1393          * so this will be 64kB/512.
1394          */
1395         mmc->max_hw_segs = NR_SG;
1396         mmc->max_phys_segs = NR_SG;
1397         
1398         /*
1399          * Maximum number of sectors in one transfer. Also limited by 64kB
1400          * buffer.
1401          */
1402         mmc->max_sectors = 128;
1403         
1404         /*
1405          * Maximum segment size. Could be one segment with the maximum number
1406          * of segments.
1407          */
1408         mmc->max_seg_size = mmc->max_sectors * 512;
1409         
1410         /*
1411          * Enable chip.
1412          */
1413         wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1414         
1415         /*
1416          * Power up chip.
1417          */
1418         wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1419         
1420         /*
1421          * Power Management stuff. No idea how this works.
1422          * Not tested.
1423          */
1424 #ifdef CONFIG_PM
1425         wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1426 #endif
1427
1428         /*
1429          * Reset the chip into a known state.
1430          */
1431         wbsd_init_device(host);
1432         
1433         dev_set_drvdata(dev, mmc);
1434         
1435         /*
1436          * Add host to MMC layer.
1437          */
1438         mmc_add_host(mmc);
1439
1440         printk(KERN_INFO "%s: W83L51xD id %x at 0x%x irq %d dma %d\n",
1441                 mmc->host_name, (int)host->chip_id, (int)host->base,
1442                 (int)host->irq, (int)host->dma);
1443
1444         return 0;
1445
1446 release:
1447         wbsd_release_regions(host);
1448
1449 freemmc:
1450         mmc_free_host(mmc);
1451
1452         return ret;
1453 }
1454
1455 /*
1456  * Device remove
1457  */
1458
1459 static int wbsd_remove(struct device* dev)
1460 {
1461         struct mmc_host* mmc = dev_get_drvdata(dev);
1462         struct wbsd_host* host;
1463         
1464         if (!mmc)
1465                 return 0;
1466
1467         host = mmc_priv(mmc);
1468         
1469         /*
1470          * Unregister host with MMC layer.
1471          */
1472         mmc_remove_host(mmc);
1473
1474         /*
1475          * Power down the SD/MMC function.
1476          */
1477         wbsd_unlock_config(host);
1478         wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1479         wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1480         wbsd_lock_config(host);
1481         
1482         /*
1483          * Free resources.
1484          */
1485         if (host->dma_buffer)
1486                 kfree(host->dma_buffer);
1487         
1488         if (host->dma >= 0)
1489                 free_dma(host->dma);
1490
1491         free_irq(host->irq, host);
1492         
1493         tasklet_kill(&host->card_tasklet);
1494         tasklet_kill(&host->fifo_tasklet);
1495         tasklet_kill(&host->crc_tasklet);
1496         tasklet_kill(&host->timeout_tasklet);
1497         tasklet_kill(&host->finish_tasklet);
1498         tasklet_kill(&host->block_tasklet);
1499         
1500         wbsd_release_regions(host);
1501         
1502         mmc_free_host(mmc);
1503
1504         return 0;
1505 }
1506
1507 /*
1508  * Power management
1509  */
1510
1511 #ifdef CONFIG_PM
1512 static int wbsd_suspend(struct device *dev, u32 state, u32 level)
1513 {
1514         DBGF("Not yet supported\n");
1515
1516         return 0;
1517 }
1518
1519 static int wbsd_resume(struct device *dev, u32 level)
1520 {
1521         DBGF("Not yet supported\n");
1522
1523         return 0;
1524 }
1525 #else
1526 #define wbsd_suspend NULL
1527 #define wbsd_resume NULL
1528 #endif
1529
1530 static void wbsd_release(struct device *dev)
1531 {
1532 }
1533
1534 static struct platform_device wbsd_device = {
1535         .name           = DRIVER_NAME,
1536         .id                     = -1,
1537         .dev            = {
1538                 .release = wbsd_release,
1539         },
1540 };
1541
1542 static struct device_driver wbsd_driver = {
1543         .name           = DRIVER_NAME,
1544         .bus            = &platform_bus_type,
1545         .probe          = wbsd_probe,
1546         .remove         = wbsd_remove,
1547         
1548         .suspend        = wbsd_suspend,
1549         .resume         = wbsd_resume,
1550 };
1551
1552 /*
1553  * Module loading/unloading
1554  */
1555
1556 static int __init wbsd_drv_init(void)
1557 {
1558         int result;
1559         
1560         printk(KERN_INFO DRIVER_NAME
1561                 ": Winbond W83L51xD SD/MMC card interface driver, "
1562                 DRIVER_VERSION "\n");
1563         printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1564         
1565         result = driver_register(&wbsd_driver);
1566         if (result < 0)
1567                 return result;
1568
1569         result = platform_device_register(&wbsd_device);
1570         if (result < 0)
1571                 return result;
1572
1573         return 0;
1574 }
1575
1576 static void __exit wbsd_drv_exit(void)
1577 {
1578         platform_device_unregister(&wbsd_device);
1579         
1580         driver_unregister(&wbsd_driver);
1581
1582         DBG("unloaded\n");
1583 }
1584
1585 module_init(wbsd_drv_init);
1586 module_exit(wbsd_drv_exit);
1587 module_param(io, uint, 0444);
1588 module_param(irq, uint, 0444);
1589 module_param(dma, int, 0444);
1590
1591 MODULE_LICENSE("GPL");
1592 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
1593
1594 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1595 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
1596 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");