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