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