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