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