blob: c351c6d1a18a11ee8ed712c77248b4337c5729d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
3 *
4 * Copyright (C) 2004-2005 Pierre Ossman, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 *
11 * Warning!
12 *
13 * Changes to the FIFO system should be done with extreme care since
14 * the hardware is full of bugs related to the FIFO. Known issues are:
15 *
16 * - FIFO size field in FSR is always zero.
17 *
18 * - FIFO interrupts tend not to work as they should. Interrupts are
19 * triggered only for full/empty events, not for threshold values.
20 *
21 * - On APIC systems the FIFO empty interrupt is sometimes lost.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010028#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010032#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/highmem.h>
34#include <linux/mmc/host.h>
35#include <linux/mmc/protocol.h>
36
37#include <asm/io.h>
38#include <asm/dma.h>
39#include <asm/scatterlist.h>
40
41#include "wbsd.h"
42
43#define DRIVER_NAME "wbsd"
Pierre Ossman916f3ac2006-08-06 22:22:23 +020044#define DRIVER_VERSION "1.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define DBG(x...) \
Russell Kingc6563172006-03-29 09:30:20 +010047 pr_debug(DRIVER_NAME ": " x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define DBGF(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010049 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
Pierre Ossman85bcc132005-05-08 19:35:27 +010052 * Device resources
53 */
54
55#ifdef CONFIG_PNP
56
57static const struct pnp_device_id pnp_dev_table[] = {
58 { "WEC0517", 0 },
59 { "WEC0518", 0 },
60 { "", 0 },
61};
62
63MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
64
65#endif /* CONFIG_PNP */
66
Adrian Bunk3eee0d02005-07-01 13:07:37 +010067static const int config_ports[] = { 0x2E, 0x4E };
68static const int unlock_codes[] = { 0x83, 0x87 };
69
70static const int valid_ids[] = {
71 0x7112,
72 };
73
Pierre Ossman85bcc132005-05-08 19:35:27 +010074#ifdef CONFIG_PNP
75static unsigned int nopnp = 0;
76#else
77static const unsigned int nopnp = 1;
78#endif
79static unsigned int io = 0x248;
80static unsigned int irq = 6;
81static int dma = 2;
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Basic functions
85 */
86
Pierre Ossmancfa7f522006-01-08 18:17:55 +000087static inline void wbsd_unlock_config(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Pierre Ossman85bcc132005-05-08 19:35:27 +010089 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +010090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 outb(host->unlock_code, host->config);
92 outb(host->unlock_code, host->config);
93}
94
Pierre Ossmancfa7f522006-01-08 18:17:55 +000095static inline void wbsd_lock_config(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Pierre Ossman85bcc132005-05-08 19:35:27 +010097 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +010098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 outb(LOCK_CODE, host->config);
100}
101
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000102static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Pierre Ossman85bcc132005-05-08 19:35:27 +0100104 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 outb(reg, host->config);
107 outb(value, host->config + 1);
108}
109
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000110static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Pierre Ossman85bcc132005-05-08 19:35:27 +0100112 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 outb(reg, host->config);
115 return inb(host->config + 1);
116}
117
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000118static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 outb(index, host->base + WBSD_IDXR);
121 outb(value, host->base + WBSD_DATAR);
122}
123
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000124static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 outb(index, host->base + WBSD_IDXR);
127 return inb(host->base + WBSD_DATAR);
128}
129
130/*
131 * Common routines
132 */
133
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000134static void wbsd_init_device(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 u8 setup, ier;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
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);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +0100146 * 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;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100151
Pierre Ossman85bcc132005-05-08 19:35:27 +0100152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * 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);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Set maximum timeout.
164 */
165 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +0100168 * 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;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100174
Pierre Ossman85bcc132005-05-08 19:35:27 +0100175 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Enable interesting interrupts.
177 */
178 ier = 0;
179 ier |= WBSD_EINT_CARD;
180 ier |= WBSD_EINT_FIFO_THRE;
181 ier |= WBSD_EINT_CCRC;
182 ier |= WBSD_EINT_TIMEOUT;
183 ier |= WBSD_EINT_CRC;
184 ier |= WBSD_EINT_TC;
185
186 outb(ier, host->base + WBSD_EIR);
187
188 /*
189 * Clear interrupts.
190 */
191 inb(host->base + WBSD_ISR);
192}
193
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000194static void wbsd_reset(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 u8 setup;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100197
Pierre Ossmand1916342005-11-05 10:36:35 +0000198 printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Soft reset of chip (SD/MMC part).
202 */
203 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
204 setup |= WBSD_SOFT_RESET;
205 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
206}
207
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000208static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned long dmaflags;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100213
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000214 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * Release ISA DMA controller.
217 */
218 dmaflags = claim_dma_lock();
219 disable_dma(host->dma);
220 clear_dma_ff(host->dma);
221 release_dma_lock(dmaflags);
222
223 /*
224 * Disable DMA on host.
225 */
226 wbsd_write_index(host, WBSD_IDX_DMA, 0);
227 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 host->mrq = NULL;
230
231 /*
232 * MMC layer might call back into the driver so first unlock.
233 */
234 spin_unlock(&host->lock);
235 mmc_request_done(host->mmc, mrq);
236 spin_lock(&host->lock);
237}
238
239/*
240 * Scatter/gather functions
241 */
242
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000243static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 /*
246 * Get info. about SG list from data structure.
247 */
248 host->cur_sg = data->sg;
249 host->num_sg = data->sg_len;
250
251 host->offset = 0;
252 host->remain = host->cur_sg->length;
253}
254
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000255static inline int wbsd_next_sg(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 /*
258 * Skip to next SG entry.
259 */
260 host->cur_sg++;
261 host->num_sg--;
262
263 /*
264 * Any entries left?
265 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000266 if (host->num_sg > 0) {
267 host->offset = 0;
268 host->remain = host->cur_sg->length;
269 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return host->num_sg;
272}
273
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000274static inline char *wbsd_kmap_sg(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
277 host->cur_sg->offset;
278 return host->mapped_sg;
279}
280
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000281static inline void wbsd_kunmap_sg(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
284}
285
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000286static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned int len, i, size;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000289 struct scatterlist *sg;
290 char *dmabuf = host->dma_buffer;
291 char *sgbuf;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 size = host->size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sg = data->sg;
296 len = data->sg_len;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /*
299 * Just loop through all entries. Size might not
300 * be the entire list though so make sure that
301 * we do not transfer too much.
302 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000303 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
305 if (size < sg[i].length)
306 memcpy(dmabuf, sgbuf, size);
307 else
308 memcpy(dmabuf, sgbuf, sg[i].length);
309 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
310 dmabuf += sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (size < sg[i].length)
313 size = 0;
314 else
315 size -= sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (size == 0)
318 break;
319 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * Check that we didn't get a request to transfer
323 * more data than can fit into the SG list.
324 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 BUG_ON(size != 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 host->size -= size;
329}
330
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000331static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 unsigned int len, i, size;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000334 struct scatterlist *sg;
335 char *dmabuf = host->dma_buffer;
336 char *sgbuf;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 size = host->size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sg = data->sg;
341 len = data->sg_len;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 * Just loop through all entries. Size might not
345 * be the entire list though so make sure that
346 * we do not transfer too much.
347 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000348 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
350 if (size < sg[i].length)
351 memcpy(sgbuf, dmabuf, size);
352 else
353 memcpy(sgbuf, dmabuf, sg[i].length);
354 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
355 dmabuf += sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (size < sg[i].length)
358 size = 0;
359 else
360 size -= sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (size == 0)
363 break;
364 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Check that we didn't get a request to transfer
368 * more data than can fit into the SG list.
369 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 BUG_ON(size != 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 host->size -= size;
374}
375
376/*
377 * Command handling
378 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100379
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000380static inline void wbsd_get_short_reply(struct wbsd_host *host,
381 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 /*
384 * Correct response type?
385 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000386 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 cmd->error = MMC_ERR_INVALID;
388 return;
389 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100390
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000391 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
392 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
393 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
394 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
395 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000398static inline void wbsd_get_long_reply(struct wbsd_host *host,
399 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * Correct response type?
405 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000406 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 cmd->error = MMC_ERR_INVALID;
408 return;
409 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100410
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000411 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 cmd->resp[i] =
413 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
414 cmd->resp[i] |=
415 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
416 cmd->resp[i] |=
417 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
418 cmd->resp[i] |=
419 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
420 }
421}
422
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000423static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int i;
426 u8 status, isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 DBGF("Sending cmd (%x)\n", cmd->opcode);
429
430 /*
431 * Clear accumulated ISR. The interrupt routine
432 * will fill this one with events that occur during
433 * transfer.
434 */
435 host->isr = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /*
438 * Send the command (CRC calculated by host).
439 */
440 outb(cmd->opcode, host->base + WBSD_CMDR);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000441 for (i = 3; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 cmd->error = MMC_ERR_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Wait for the request to complete.
448 */
449 do {
450 status = wbsd_read_index(host, WBSD_IDX_STATUS);
451 } while (status & WBSD_CARDTRAFFIC);
452
453 /*
454 * Do we expect a reply?
455 */
Russell Kinge9225172006-02-02 12:23:12 +0000456 if (cmd->flags & MMC_RSP_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * Read back status.
459 */
460 isr = host->isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Card removed? */
463 if (isr & WBSD_INT_CARD)
464 cmd->error = MMC_ERR_TIMEOUT;
465 /* Timeout? */
466 else if (isr & WBSD_INT_TIMEOUT)
467 cmd->error = MMC_ERR_TIMEOUT;
468 /* CRC? */
469 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
470 cmd->error = MMC_ERR_BADCRC;
471 /* All ok */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000472 else {
Russell Kinge9225172006-02-02 12:23:12 +0000473 if (cmd->flags & MMC_RSP_136)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 wbsd_get_long_reply(host, cmd);
Russell Kinge9225172006-02-02 12:23:12 +0000475 else
476 wbsd_get_short_reply(host, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479
480 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
481}
482
483/*
484 * Data functions
485 */
486
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000487static void wbsd_empty_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000489 struct mmc_data *data = host->mrq->cmd->data;
490 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Handle excessive data.
495 */
496 if (data->bytes_xfered == host->size)
497 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 buffer = wbsd_kmap_sg(host) + host->offset;
500
501 /*
502 * Drain the fifo. This has a tendency to loop longer
503 * than the FIFO length (usually one block).
504 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000505 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * The size field in the FSR is broken so we have to
508 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (fsr & WBSD_FIFO_FULL)
511 fifo = 16;
512 else if (fsr & WBSD_FIFO_FUTHRE)
513 fifo = 8;
514 else
515 fifo = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100516
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000517 for (i = 0; i < fifo; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *buffer = inb(host->base + WBSD_DFR);
519 buffer++;
520 host->offset++;
521 host->remain--;
522
523 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /*
526 * Transfer done?
527 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000528 if (data->bytes_xfered == host->size) {
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100529 wbsd_kunmap_sg(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
531 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * End of scatter list entry?
535 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000536 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 wbsd_kunmap_sg(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Get next entry. Check if last.
541 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000542 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * We should never reach this point.
545 * It means that we're trying to
546 * transfer more blocks than can fit
547 * into the scatter list.
548 */
549 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
554 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 buffer = wbsd_kmap_sg(host);
557 }
558 }
559 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 wbsd_kunmap_sg(host);
562
563 /*
564 * This is a very dirty hack to solve a
565 * hardware problem. The chip doesn't trigger
566 * FIFO threshold interrupts properly.
567 */
568 if ((host->size - data->bytes_xfered) < 16)
569 tasklet_schedule(&host->fifo_tasklet);
570}
571
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000572static void wbsd_fill_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000574 struct mmc_data *data = host->mrq->cmd->data;
575 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
579 * Check that we aren't being called after the
580 * entire buffer has been transfered.
581 */
582 if (data->bytes_xfered == host->size)
583 return;
584
585 buffer = wbsd_kmap_sg(host) + host->offset;
586
587 /*
588 * Fill the fifo. This has a tendency to loop longer
589 * than the FIFO length (usually one block).
590 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000591 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * The size field in the FSR is broken so we have to
594 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (fsr & WBSD_FIFO_EMPTY)
597 fifo = 0;
598 else if (fsr & WBSD_FIFO_EMTHRE)
599 fifo = 8;
600 else
601 fifo = 15;
602
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000603 for (i = 16; i > fifo; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 outb(*buffer, host->base + WBSD_DFR);
605 buffer++;
606 host->offset++;
607 host->remain--;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Transfer done?
613 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000614 if (data->bytes_xfered == host->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 wbsd_kunmap_sg(host);
616 return;
617 }
618
619 /*
620 * End of scatter list entry?
621 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000622 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 wbsd_kunmap_sg(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /*
626 * Get next entry. Check if last.
627 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000628 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * We should never reach this point.
631 * It means that we're trying to
632 * transfer more blocks than can fit
633 * into the scatter list.
634 */
635 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
640 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 buffer = wbsd_kmap_sg(host);
643 }
644 }
645 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 wbsd_kunmap_sg(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100648
Pierre Ossman85bcc132005-05-08 19:35:27 +0100649 /*
650 * The controller stops sending interrupts for
651 * 'FIFO empty' under certain conditions. So we
652 * need to be a bit more pro-active.
653 */
654 tasklet_schedule(&host->fifo_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000657static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 u16 blksize;
660 u8 setup;
661 unsigned long dmaflags;
662
663 DBGF("blksz %04x blks %04x flags %08x\n",
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100664 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 DBGF("tsac %d ms nsac %d clk\n",
666 data->timeout_ns / 1000000, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * Calculate size.
670 */
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100671 host->size = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
674 * Check timeout values for overflow.
675 * (Yes, some cards cause this value to overflow).
676 */
677 if (data->timeout_ns > 127000000)
678 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000679 else {
680 wbsd_write_index(host, WBSD_IDX_TAAC,
681 data->timeout_ns / 1000000);
682 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (data->timeout_clks > 255)
685 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
686 else
687 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * Inform the chip of how large blocks will be
691 * sent. It needs this to determine when to
692 * calculate CRC.
693 *
694 * Space for CRC must be included in the size.
Pierre Ossman65ae2112005-09-06 15:18:57 -0700695 * Two bytes are needed for each data line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000697 if (host->bus_width == MMC_BUS_WIDTH_1) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100698 blksize = data->blksz + 2;
Pierre Ossman65ae2112005-09-06 15:18:57 -0700699
700 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
701 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000702 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100703 blksize = data->blksz + 2 * 4;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100704
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000705 wbsd_write_index(host, WBSD_IDX_PBSMSB,
706 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700707 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000708 } else {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700709 data->error = MMC_ERR_INVALID;
710 return;
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /*
714 * Clear the FIFO. This is needed even for DMA
715 * transfers since the chip still uses the FIFO
716 * internally.
717 */
718 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
719 setup |= WBSD_FIFO_RESET;
720 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /*
723 * DMA transfer?
724 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000725 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * The buffer for DMA is only 64 kB.
728 */
729 BUG_ON(host->size > 0x10000);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000730 if (host->size > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 data->error = MMC_ERR_INVALID;
732 return;
733 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
736 * Transfer data from the SG list to
737 * the DMA buffer.
738 */
739 if (data->flags & MMC_DATA_WRITE)
740 wbsd_sg_to_dma(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * Initialise the ISA DMA controller.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 dmaflags = claim_dma_lock();
746 disable_dma(host->dma);
747 clear_dma_ff(host->dma);
748 if (data->flags & MMC_DATA_READ)
749 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
750 else
751 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
752 set_dma_addr(host->dma, host->dma_addr);
753 set_dma_count(host->dma, host->size);
754
755 enable_dma(host->dma);
756 release_dma_lock(dmaflags);
757
758 /*
759 * Enable DMA on the host.
760 */
761 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000762 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * This flag is used to keep printk
765 * output to a minimum.
766 */
767 host->firsterr = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * Initialise the SG list.
771 */
772 wbsd_init_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /*
775 * Turn off DMA.
776 */
777 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * Set up FIFO threshold levels (and fill
781 * buffer if doing a write).
782 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000783 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 wbsd_write_index(host, WBSD_IDX_FIFOEN,
785 WBSD_FIFOEN_FULL | 8);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000786 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 wbsd_write_index(host, WBSD_IDX_FIFOEN,
788 WBSD_FIFOEN_EMPTY | 8);
789 wbsd_fill_fifo(host);
790 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 data->error = MMC_ERR_NONE;
794}
795
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000796static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 unsigned long dmaflags;
799 int count;
800 u8 status;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 WARN_ON(host->mrq == NULL);
803
804 /*
805 * Send a stop command if needed.
806 */
807 if (data->stop)
808 wbsd_send_command(host, data->stop);
809
810 /*
811 * Wait for the controller to leave data
812 * transfer state.
813 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000814 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 status = wbsd_read_index(host, WBSD_IDX_STATUS);
816 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
819 * DMA transfer?
820 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000821 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Disable DMA on the host.
824 */
825 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Turn of ISA DMA controller.
829 */
830 dmaflags = claim_dma_lock();
831 disable_dma(host->dma);
832 clear_dma_ff(host->dma);
833 count = get_dma_residue(host->dma);
834 release_dma_lock(dmaflags);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * Any leftover data?
838 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000839 if (count) {
Pierre Ossmand1916342005-11-05 10:36:35 +0000840 printk(KERN_ERR "%s: Incomplete DMA transfer. "
841 "%d bytes left.\n",
842 mmc_hostname(host->mmc), count);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 data->error = MMC_ERR_FAILED;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000845 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
847 * Transfer data from DMA buffer to
848 * SG list.
849 */
850 if (data->flags & MMC_DATA_READ)
851 wbsd_dma_to_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 data->bytes_xfered = host->size;
854 }
855 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 wbsd_request_end(host, host->mrq);
860}
861
Pierre Ossman85bcc132005-05-08 19:35:27 +0100862/*****************************************************************************\
863 * *
864 * MMC layer callbacks *
865 * *
866\*****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000868static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000870 struct wbsd_host *host = mmc_priv(mmc);
871 struct mmc_command *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /*
874 * Disable tasklets to avoid a deadlock.
875 */
876 spin_lock_bh(&host->lock);
877
878 BUG_ON(host->mrq != NULL);
879
880 cmd = mrq->cmd;
881
882 host->mrq = mrq;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
885 * If there is no card in the slot then
886 * timeout immediatly.
887 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000888 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 cmd->error = MMC_ERR_TIMEOUT;
890 goto done;
891 }
892
893 /*
894 * Does the request include data?
895 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000896 if (cmd->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 wbsd_prepare_data(host, cmd->data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (cmd->data->error != MMC_ERR_NONE)
900 goto done;
901 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 wbsd_send_command(host, cmd);
904
905 /*
906 * If this is a data transfer the request
907 * will be finished after the data has
908 * transfered.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100909 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000910 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /*
912 * Dirty fix for hardware bug.
913 */
914 if (host->dma == -1)
915 tasklet_schedule(&host->fifo_tasklet);
916
917 spin_unlock_bh(&host->lock);
918
919 return;
920 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922done:
923 wbsd_request_end(host, mrq);
924
925 spin_unlock_bh(&host->lock);
926}
927
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000928static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000930 struct wbsd_host *host = mmc_priv(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 u8 clk, setup, pwr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 spin_lock_bh(&host->lock);
934
935 /*
936 * Reset the chip on each power off.
937 * Should clear out any weird states.
938 */
939 if (ios->power_mode == MMC_POWER_OFF)
940 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (ios->clock >= 24000000)
943 clk = WBSD_CLK_24M;
944 else if (ios->clock >= 16000000)
945 clk = WBSD_CLK_16M;
946 else if (ios->clock >= 12000000)
947 clk = WBSD_CLK_12M;
948 else
949 clk = WBSD_CLK_375K;
950
951 /*
952 * Only write to the clock register when
953 * there is an actual change.
954 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000955 if (clk != host->clk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 wbsd_write_index(host, WBSD_IDX_CLK, clk);
957 host->clk = clk;
958 }
959
Pierre Ossman85bcc132005-05-08 19:35:27 +0100960 /*
961 * Power up card.
962 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000963 if (ios->power_mode != MMC_POWER_OFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 pwr = inb(host->base + WBSD_CSR);
965 pwr &= ~WBSD_POWER_N;
966 outb(pwr, host->base + WBSD_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Pierre Ossman85bcc132005-05-08 19:35:27 +0100969 /*
970 * MMC cards need to have pin 1 high during init.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100971 * It wreaks havoc with the card detection though so
Pierre Ossman1656fa52005-09-03 16:45:49 +0100972 * that needs to be disabled.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100973 */
974 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000975 if (ios->chip_select == MMC_CS_HIGH) {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700976 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
Pierre Ossman85bcc132005-05-08 19:35:27 +0100977 setup |= WBSD_DAT3_H;
978 host->flags |= WBSD_FIGNORE_DETECT;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000979 } else {
980 if (setup & WBSD_DAT3_H) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100981 setup &= ~WBSD_DAT3_H;
Pierre Ossman1656fa52005-09-03 16:45:49 +0100982
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100983 /*
984 * We cannot resume card detection immediatly
985 * because of capacitance and delays in the chip.
986 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000987 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100988 }
Pierre Ossman85bcc132005-05-08 19:35:27 +0100989 }
990 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100991
Pierre Ossman65ae2112005-09-06 15:18:57 -0700992 /*
993 * Store bus width for later. Will be used when
994 * setting up the data transfer.
995 */
996 host->bus_width = ios->bus_width;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 spin_unlock_bh(&host->lock);
999}
1000
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001001static int wbsd_get_ro(struct mmc_host *mmc)
Pierre Ossman65ae2112005-09-06 15:18:57 -07001002{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001003 struct wbsd_host *host = mmc_priv(mmc);
Pierre Ossman65ae2112005-09-06 15:18:57 -07001004 u8 csr;
1005
1006 spin_lock_bh(&host->lock);
1007
1008 csr = inb(host->base + WBSD_CSR);
1009 csr |= WBSD_MSLED;
1010 outb(csr, host->base + WBSD_CSR);
1011
1012 mdelay(1);
1013
1014 csr = inb(host->base + WBSD_CSR);
1015 csr &= ~WBSD_MSLED;
1016 outb(csr, host->base + WBSD_CSR);
1017
1018 spin_unlock_bh(&host->lock);
1019
1020 return csr & WBSD_WRPT;
1021}
1022
Pierre Ossman85bcc132005-05-08 19:35:27 +01001023static struct mmc_host_ops wbsd_ops = {
1024 .request = wbsd_request,
1025 .set_ios = wbsd_set_ios,
Pierre Ossman65ae2112005-09-06 15:18:57 -07001026 .get_ro = wbsd_get_ro,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001027};
1028
1029/*****************************************************************************\
1030 * *
1031 * Interrupt handling *
1032 * *
1033\*****************************************************************************/
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001036 * Helper function to reset detection ignore
1037 */
1038
1039static void wbsd_reset_ignore(unsigned long data)
1040{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001041 struct wbsd_host *host = (struct wbsd_host *)data;
Pierre Ossman1656fa52005-09-03 16:45:49 +01001042
1043 BUG_ON(host == NULL);
1044
1045 DBG("Resetting card detection ignore\n");
1046
1047 spin_lock_bh(&host->lock);
1048
1049 host->flags &= ~WBSD_FIGNORE_DETECT;
1050
1051 /*
1052 * Card status might have changed during the
1053 * blackout.
1054 */
1055 tasklet_schedule(&host->card_tasklet);
1056
1057 spin_unlock_bh(&host->lock);
1058}
1059
1060/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * Tasklets
1062 */
1063
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001064static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 WARN_ON(!host->mrq);
1067 if (!host->mrq)
1068 return NULL;
1069
1070 WARN_ON(!host->mrq->cmd);
1071 if (!host->mrq->cmd)
1072 return NULL;
1073
1074 WARN_ON(!host->mrq->cmd->data);
1075 if (!host->mrq->cmd->data)
1076 return NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return host->mrq->cmd->data;
1079}
1080
1081static void wbsd_tasklet_card(unsigned long param)
1082{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001083 struct wbsd_host *host = (struct wbsd_host *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 u8 csr;
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001085 int delay = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001088
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001089 if (host->flags & WBSD_FIGNORE_DETECT) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001090 spin_unlock(&host->lock);
1091 return;
1092 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 csr = inb(host->base + WBSD_CSR);
1095 WARN_ON(csr == 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001096
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001097 if (csr & WBSD_CARDPRESENT) {
1098 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001099 DBG("Card inserted\n");
1100 host->flags |= WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001101
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001102 delay = 500;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001103 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001104 } else if (host->flags & WBSD_FCARD_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 DBG("Card removed\n");
Pierre Ossman85bcc132005-05-08 19:35:27 +01001106 host->flags &= ~WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001107
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001108 if (host->mrq) {
Pierre Ossmand1916342005-11-05 10:36:35 +00001109 printk(KERN_ERR "%s: Card removed during transfer!\n",
1110 mmc_hostname(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 wbsd_reset(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 host->mrq->cmd->error = MMC_ERR_FAILED;
1114 tasklet_schedule(&host->finish_tasklet);
1115 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001116
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001117 delay = 0;
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001118 }
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001119
1120 /*
1121 * Unlock first since we might get a call back.
1122 */
1123
1124 spin_unlock(&host->lock);
1125
1126 if (delay != -1)
1127 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static void wbsd_tasklet_fifo(unsigned long param)
1131{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001132 struct wbsd_host *host = (struct wbsd_host *)param;
1133 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!host->mrq)
1138 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 data = wbsd_get_data(host);
1141 if (!data)
1142 goto end;
1143
1144 if (data->flags & MMC_DATA_WRITE)
1145 wbsd_fill_fifo(host);
1146 else
1147 wbsd_empty_fifo(host);
1148
1149 /*
1150 * Done?
1151 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001152 if (host->size == data->bytes_xfered) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1154 tasklet_schedule(&host->finish_tasklet);
1155 }
1156
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001157end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_unlock(&host->lock);
1159}
1160
1161static void wbsd_tasklet_crc(unsigned long param)
1162{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001163 struct wbsd_host *host = (struct wbsd_host *)param;
1164 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!host->mrq)
1169 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 data = wbsd_get_data(host);
1172 if (!data)
1173 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 DBGF("CRC error\n");
1176
1177 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 tasklet_schedule(&host->finish_tasklet);
1180
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001181end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 spin_unlock(&host->lock);
1183}
1184
1185static void wbsd_tasklet_timeout(unsigned long param)
1186{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001187 struct wbsd_host *host = (struct wbsd_host *)param;
1188 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!host->mrq)
1193 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 data = wbsd_get_data(host);
1196 if (!data)
1197 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 DBGF("Timeout\n");
1200
1201 data->error = MMC_ERR_TIMEOUT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 tasklet_schedule(&host->finish_tasklet);
1204
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001205end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 spin_unlock(&host->lock);
1207}
1208
1209static void wbsd_tasklet_finish(unsigned long param)
1210{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001211 struct wbsd_host *host = (struct wbsd_host *)param;
1212 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 WARN_ON(!host->mrq);
1217 if (!host->mrq)
1218 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 data = wbsd_get_data(host);
1221 if (!data)
1222 goto end;
1223
1224 wbsd_finish_data(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001225
1226end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 spin_unlock(&host->lock);
1228}
1229
1230static void wbsd_tasklet_block(unsigned long param)
1231{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001232 struct wbsd_host *host = (struct wbsd_host *)param;
1233 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 spin_lock(&host->lock);
1236
1237 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001238 WBSD_CRC_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 data = wbsd_get_data(host);
1240 if (!data)
1241 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 DBGF("CRC error\n");
1244
1245 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 tasklet_schedule(&host->finish_tasklet);
1248 }
1249
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001250end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 spin_unlock(&host->lock);
1252}
1253
1254/*
1255 * Interrupt handling
1256 */
1257
1258static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
1259{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001260 struct wbsd_host *host = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 isr = inb(host->base + WBSD_ISR);
1264
1265 /*
1266 * Was it actually our hardware that caused the interrupt?
1267 */
1268 if (isr == 0xff || isr == 0x00)
1269 return IRQ_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 host->isr |= isr;
1272
1273 /*
1274 * Schedule tasklets as needed.
1275 */
1276 if (isr & WBSD_INT_CARD)
1277 tasklet_schedule(&host->card_tasklet);
1278 if (isr & WBSD_INT_FIFO_THRE)
1279 tasklet_schedule(&host->fifo_tasklet);
1280 if (isr & WBSD_INT_CRC)
1281 tasklet_hi_schedule(&host->crc_tasklet);
1282 if (isr & WBSD_INT_TIMEOUT)
1283 tasklet_hi_schedule(&host->timeout_tasklet);
1284 if (isr & WBSD_INT_BUSYEND)
1285 tasklet_hi_schedule(&host->block_tasklet);
1286 if (isr & WBSD_INT_TC)
1287 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return IRQ_HANDLED;
1290}
1291
Pierre Ossman85bcc132005-05-08 19:35:27 +01001292/*****************************************************************************\
1293 * *
1294 * Device initialisation and shutdown *
1295 * *
1296\*****************************************************************************/
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001299 * Allocate/free MMC structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
1301
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001302static int __devinit wbsd_alloc_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001303{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001304 struct mmc_host *mmc;
1305 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001306
Pierre Ossman85bcc132005-05-08 19:35:27 +01001307 /*
1308 * Allocate MMC structure.
1309 */
1310 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1311 if (!mmc)
1312 return -ENOMEM;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001313
Pierre Ossman85bcc132005-05-08 19:35:27 +01001314 host = mmc_priv(mmc);
1315 host->mmc = mmc;
1316
1317 host->dma = -1;
1318
1319 /*
1320 * Set host parameters.
1321 */
1322 mmc->ops = &wbsd_ops;
1323 mmc->f_min = 375000;
1324 mmc->f_max = 24000000;
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001325 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossman65ae2112005-09-06 15:18:57 -07001326 mmc->caps = MMC_CAP_4_BIT_DATA;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001327
Pierre Ossman85bcc132005-05-08 19:35:27 +01001328 spin_lock_init(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001329
Pierre Ossman85bcc132005-05-08 19:35:27 +01001330 /*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001331 * Set up timers
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001332 */
Pierre Ossman1656fa52005-09-03 16:45:49 +01001333 init_timer(&host->ignore_timer);
1334 host->ignore_timer.data = (unsigned long)host;
1335 host->ignore_timer.function = wbsd_reset_ignore;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001336
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001337 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001338 * Maximum number of segments. Worst case is one sector per segment
1339 * so this will be 64kB/512.
1340 */
1341 mmc->max_hw_segs = 128;
1342 mmc->max_phys_segs = 128;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001343
Pierre Ossman85bcc132005-05-08 19:35:27 +01001344 /*
1345 * Maximum number of sectors in one transfer. Also limited by 64kB
1346 * buffer.
1347 */
1348 mmc->max_sectors = 128;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001349
Pierre Ossman85bcc132005-05-08 19:35:27 +01001350 /*
1351 * Maximum segment size. Could be one segment with the maximum number
1352 * of segments.
1353 */
1354 mmc->max_seg_size = mmc->max_sectors * 512;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001355
Pierre Ossman85bcc132005-05-08 19:35:27 +01001356 dev_set_drvdata(dev, mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001357
Pierre Ossman85bcc132005-05-08 19:35:27 +01001358 return 0;
1359}
1360
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001361static void __devexit wbsd_free_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001362{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001363 struct mmc_host *mmc;
1364 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001365
Pierre Ossman85bcc132005-05-08 19:35:27 +01001366 mmc = dev_get_drvdata(dev);
1367 if (!mmc)
1368 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001369
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001370 host = mmc_priv(mmc);
1371 BUG_ON(host == NULL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001372
Pierre Ossman1656fa52005-09-03 16:45:49 +01001373 del_timer_sync(&host->ignore_timer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001374
Pierre Ossman85bcc132005-05-08 19:35:27 +01001375 mmc_free_host(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001376
Pierre Ossman85bcc132005-05-08 19:35:27 +01001377 dev_set_drvdata(dev, NULL);
1378}
1379
1380/*
1381 * Scan for known chip id:s
1382 */
1383
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001384static int __devinit wbsd_scan(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 int i, j, k;
1387 int id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /*
1390 * Iterate through all ports, all codes to
1391 * find hardware that is in our known list.
1392 */
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001393 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1395 continue;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001396
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001397 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 id = 0xFFFF;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001399
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001400 host->config = config_ports[i];
1401 host->unlock_code = unlock_codes[j];
1402
1403 wbsd_unlock_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 outb(WBSD_CONF_ID_HI, config_ports[i]);
1406 id = inb(config_ports[i] + 1) << 8;
1407
1408 outb(WBSD_CONF_ID_LO, config_ports[i]);
1409 id |= inb(config_ports[i] + 1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001410
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001411 wbsd_lock_config(host);
1412
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001413 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001414 if (id == valid_ids[k]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 host->chip_id = id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418 }
1419 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001420
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001421 if (id != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 DBG("Unknown hardware (id %x) found at %x\n",
1423 id, config_ports[i]);
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 release_region(config_ports[i], 2);
1428 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001429
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001430 host->config = 0;
1431 host->unlock_code = 0;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -ENODEV;
1434}
1435
Pierre Ossman85bcc132005-05-08 19:35:27 +01001436/*
1437 * Allocate/free io port ranges
1438 */
1439
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001440static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001442 if (base & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EINVAL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001444
Pierre Ossman85bcc132005-05-08 19:35:27 +01001445 if (!request_region(base, 8, DRIVER_NAME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EIO;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001447
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001448 host->base = base;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
1452
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001453static void __devexit wbsd_release_regions(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 if (host->base)
1456 release_region(host->base, 8);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001457
Pierre Ossman85bcc132005-05-08 19:35:27 +01001458 host->base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (host->config)
1461 release_region(host->config, 2);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001462
Pierre Ossman85bcc132005-05-08 19:35:27 +01001463 host->config = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Pierre Ossman85bcc132005-05-08 19:35:27 +01001466/*
1467 * Allocate/free DMA port and buffer
1468 */
1469
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001470static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (dma < 0)
1473 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (request_dma(dma, DRIVER_NAME))
1476 goto err;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * We need to allocate a special buffer in
1480 * order for ISA to be able to DMA to it.
1481 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001482 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1484 if (!host->dma_buffer)
1485 goto free;
1486
1487 /*
1488 * Translate the address to a physical address.
1489 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001490 host->dma_addr = dma_map_single(host->mmc->dev, host->dma_buffer,
1491 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * ISA DMA must be aligned on a 64k basis.
1495 */
1496 if ((host->dma_addr & 0xffff) != 0)
1497 goto kfree;
1498 /*
1499 * ISA cannot access memory above 16 MB.
1500 */
1501 else if (host->dma_addr >= 0x1000000)
1502 goto kfree;
1503
1504 host->dma = dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508kfree:
1509 /*
1510 * If we've gotten here then there is some kind of alignment bug
1511 */
1512 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001513
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001514 dma_unmap_single(host->mmc->dev, host->dma_addr,
1515 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001516 host->dma_addr = (dma_addr_t)NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 kfree(host->dma_buffer);
1519 host->dma_buffer = NULL;
1520
1521free:
1522 free_dma(dma);
1523
1524err:
1525 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1526 "Falling back on FIFO.\n", dma);
1527}
1528
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001529static void __devexit wbsd_release_dma(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001530{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001531 if (host->dma_addr) {
1532 dma_unmap_single(host->mmc->dev, host->dma_addr,
1533 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1534 }
Jesper Juhl6044ec82005-11-07 01:01:32 -08001535 kfree(host->dma_buffer);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001536 if (host->dma >= 0)
1537 free_dma(host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001538
Pierre Ossman85bcc132005-05-08 19:35:27 +01001539 host->dma = -1;
1540 host->dma_buffer = NULL;
1541 host->dma_addr = (dma_addr_t)NULL;
1542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001545 * Allocate/free IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
1547
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001548static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * Allocate interrupt.
1554 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001555
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001556 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (ret)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001558 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 host->irq = irq;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Set up tasklets.
1564 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001565 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1566 (unsigned long)host);
1567 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1568 (unsigned long)host);
1569 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1570 (unsigned long)host);
1571 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1572 (unsigned long)host);
1573 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1574 (unsigned long)host);
1575 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1576 (unsigned long)host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001581static void __devexit wbsd_release_irq(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Pierre Ossman85bcc132005-05-08 19:35:27 +01001583 if (!host->irq)
1584 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 free_irq(host->irq, host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001587
Pierre Ossman85bcc132005-05-08 19:35:27 +01001588 host->irq = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 tasklet_kill(&host->card_tasklet);
1591 tasklet_kill(&host->fifo_tasklet);
1592 tasklet_kill(&host->crc_tasklet);
1593 tasklet_kill(&host->timeout_tasklet);
1594 tasklet_kill(&host->finish_tasklet);
1595 tasklet_kill(&host->block_tasklet);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001596}
1597
1598/*
1599 * Allocate all resources for the host.
1600 */
1601
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001602static int __devinit wbsd_request_resources(struct wbsd_host *host,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001603 int base, int irq, int dma)
1604{
1605 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001606
Pierre Ossman85bcc132005-05-08 19:35:27 +01001607 /*
1608 * Allocate I/O ports.
1609 */
1610 ret = wbsd_request_region(host, base);
1611 if (ret)
1612 return ret;
1613
1614 /*
1615 * Allocate interrupt.
1616 */
1617 ret = wbsd_request_irq(host, irq);
1618 if (ret)
1619 return ret;
1620
1621 /*
1622 * Allocate DMA.
1623 */
1624 wbsd_request_dma(host, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001625
Pierre Ossman85bcc132005-05-08 19:35:27 +01001626 return 0;
1627}
1628
1629/*
1630 * Release all resources for the host.
1631 */
1632
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001633static void __devexit wbsd_release_resources(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001634{
1635 wbsd_release_dma(host);
1636 wbsd_release_irq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 wbsd_release_regions(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001638}
1639
1640/*
1641 * Configure the resources the chip should use.
1642 */
1643
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001644static void wbsd_chip_config(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001645{
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001646 wbsd_unlock_config(host);
1647
Pierre Ossman85bcc132005-05-08 19:35:27 +01001648 /*
1649 * Reset the chip.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001650 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001651 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1652 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1653
1654 /*
1655 * Select SD/MMC function.
1656 */
1657 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001658
Pierre Ossman85bcc132005-05-08 19:35:27 +01001659 /*
1660 * Set up card detection.
1661 */
1662 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001663
Pierre Ossman85bcc132005-05-08 19:35:27 +01001664 /*
1665 * Configure chip
1666 */
1667 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1668 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001669
Pierre Ossman85bcc132005-05-08 19:35:27 +01001670 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001671
Pierre Ossman85bcc132005-05-08 19:35:27 +01001672 if (host->dma >= 0)
1673 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001674
Pierre Ossman85bcc132005-05-08 19:35:27 +01001675 /*
1676 * Enable and power up chip.
1677 */
1678 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1679 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001680
1681 wbsd_lock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001682}
1683
1684/*
1685 * Check that configured resources are correct.
1686 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001687
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001688static int wbsd_chip_validate(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001689{
1690 int base, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001691
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001692 wbsd_unlock_config(host);
1693
Pierre Ossman85bcc132005-05-08 19:35:27 +01001694 /*
1695 * Select SD/MMC function.
1696 */
1697 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001698
Pierre Ossman85bcc132005-05-08 19:35:27 +01001699 /*
1700 * Read configuration.
1701 */
1702 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1703 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001704
Pierre Ossman85bcc132005-05-08 19:35:27 +01001705 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001706
Pierre Ossman85bcc132005-05-08 19:35:27 +01001707 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001708
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001709 wbsd_lock_config(host);
1710
Pierre Ossman85bcc132005-05-08 19:35:27 +01001711 /*
1712 * Validate against given configuration.
1713 */
1714 if (base != host->base)
1715 return 0;
1716 if (irq != host->irq)
1717 return 0;
1718 if ((dma != host->dma) && (host->dma != -1))
1719 return 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001720
Pierre Ossman85bcc132005-05-08 19:35:27 +01001721 return 1;
1722}
1723
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001724/*
1725 * Powers down the SD function
1726 */
1727
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001728static void wbsd_chip_poweroff(struct wbsd_host *host)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001729{
1730 wbsd_unlock_config(host);
1731
1732 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1733 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1734
1735 wbsd_lock_config(host);
1736}
1737
Pierre Ossman85bcc132005-05-08 19:35:27 +01001738/*****************************************************************************\
1739 * *
1740 * Devices setup and shutdown *
1741 * *
1742\*****************************************************************************/
1743
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001744static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001745 int pnp)
1746{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001747 struct wbsd_host *host = NULL;
1748 struct mmc_host *mmc = NULL;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001749 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001750
Pierre Ossman85bcc132005-05-08 19:35:27 +01001751 ret = wbsd_alloc_mmc(dev);
1752 if (ret)
1753 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001754
Pierre Ossman85bcc132005-05-08 19:35:27 +01001755 mmc = dev_get_drvdata(dev);
1756 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001757
Pierre Ossman85bcc132005-05-08 19:35:27 +01001758 /*
1759 * Scan for hardware.
1760 */
1761 ret = wbsd_scan(host);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001762 if (ret) {
1763 if (pnp && (ret == -ENODEV)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001764 printk(KERN_WARNING DRIVER_NAME
1765 ": Unable to confirm device presence. You may "
1766 "experience lock-ups.\n");
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001767 } else {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001768 wbsd_free_mmc(dev);
1769 return ret;
1770 }
1771 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001772
Pierre Ossman85bcc132005-05-08 19:35:27 +01001773 /*
1774 * Request resources.
1775 */
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001776 ret = wbsd_request_resources(host, base, irq, dma);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001777 if (ret) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001778 wbsd_release_resources(host);
1779 wbsd_free_mmc(dev);
1780 return ret;
1781 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001782
Pierre Ossman85bcc132005-05-08 19:35:27 +01001783 /*
1784 * See if chip needs to be configured.
1785 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001786 if (pnp) {
1787 if ((host->config != 0) && !wbsd_chip_validate(host)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001788 printk(KERN_WARNING DRIVER_NAME
1789 ": PnP active but chip not configured! "
1790 "You probably have a buggy BIOS. "
1791 "Configuring chip manually.\n");
1792 wbsd_chip_config(host);
1793 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001794 } else
Pierre Ossman85bcc132005-05-08 19:35:27 +01001795 wbsd_chip_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001796
Pierre Ossman85bcc132005-05-08 19:35:27 +01001797 /*
1798 * Power Management stuff. No idea how this works.
1799 * Not tested.
1800 */
1801#ifdef CONFIG_PM
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001802 if (host->config) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001803 wbsd_unlock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001804 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001805 wbsd_lock_config(host);
1806 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001807#endif
1808 /*
1809 * Allow device to initialise itself properly.
1810 */
1811 mdelay(5);
1812
1813 /*
1814 * Reset the chip into a known state.
1815 */
1816 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001817
Pierre Ossman85bcc132005-05-08 19:35:27 +01001818 mmc_add_host(mmc);
1819
Russell Kingd366b642005-08-19 09:40:08 +01001820 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
Pierre Ossman85bcc132005-05-08 19:35:27 +01001821 if (host->chip_id != 0)
1822 printk(" id %x", (int)host->chip_id);
1823 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1824 if (host->dma >= 0)
1825 printk(" dma %d", (int)host->dma);
1826 else
1827 printk(" FIFO");
1828 if (pnp)
1829 printk(" PnP");
1830 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 return 0;
1833}
1834
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001835static void __devexit wbsd_shutdown(struct device *dev, int pnp)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001836{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001837 struct mmc_host *mmc = dev_get_drvdata(dev);
1838 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001839
Pierre Ossman85bcc132005-05-08 19:35:27 +01001840 if (!mmc)
1841 return;
1842
1843 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001844
Pierre Ossman85bcc132005-05-08 19:35:27 +01001845 mmc_remove_host(mmc);
1846
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001847 /*
1848 * Power down the SD/MMC function.
1849 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001850 if (!pnp)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001851 wbsd_chip_poweroff(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001852
Pierre Ossman85bcc132005-05-08 19:35:27 +01001853 wbsd_release_resources(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001854
Pierre Ossman85bcc132005-05-08 19:35:27 +01001855 wbsd_free_mmc(dev);
1856}
1857
1858/*
1859 * Non-PnP
1860 */
1861
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001862static int __devinit wbsd_probe(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001863{
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001864 /* Use the module parameters for resources */
Russell King3ae5eae2005-11-09 22:32:44 +00001865 return wbsd_init(&dev->dev, io, irq, dma, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001866}
1867
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001868static int __devexit wbsd_remove(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001869{
Russell King3ae5eae2005-11-09 22:32:44 +00001870 wbsd_shutdown(&dev->dev, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001871
1872 return 0;
1873}
1874
1875/*
1876 * PnP
1877 */
1878
1879#ifdef CONFIG_PNP
1880
1881static int __devinit
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001882wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001883{
1884 int io, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001885
Pierre Ossman85bcc132005-05-08 19:35:27 +01001886 /*
1887 * Get resources from PnP layer.
1888 */
1889 io = pnp_port_start(pnpdev, 0);
1890 irq = pnp_irq(pnpdev, 0);
1891 if (pnp_dma_valid(pnpdev, 0))
1892 dma = pnp_dma(pnpdev, 0);
1893 else
1894 dma = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001895
Pierre Ossman85bcc132005-05-08 19:35:27 +01001896 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001897
Pierre Ossman85bcc132005-05-08 19:35:27 +01001898 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1899}
1900
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001901static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001902{
1903 wbsd_shutdown(&dev->dev, 1);
1904}
1905
1906#endif /* CONFIG_PNP */
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/*
1909 * Power management
1910 */
1911
1912#ifdef CONFIG_PM
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001913
Pierre Ossman5e68d952006-01-08 14:21:52 +00001914static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1915{
1916 BUG_ON(host == NULL);
1917
1918 return mmc_suspend_host(host->mmc, state);
1919}
1920
1921static int wbsd_resume(struct wbsd_host *host)
1922{
1923 BUG_ON(host == NULL);
1924
1925 wbsd_init_device(host);
1926
1927 return mmc_resume_host(host->mmc);
1928}
1929
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001930static int wbsd_platform_suspend(struct platform_device *dev,
1931 pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Russell King3ae5eae2005-11-09 22:32:44 +00001933 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001934 struct wbsd_host *host;
1935 int ret;
1936
Pierre Ossman5e68d952006-01-08 14:21:52 +00001937 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001938 return 0;
1939
Pierre Ossman5e68d952006-01-08 14:21:52 +00001940 DBGF("Suspending...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001941
1942 host = mmc_priv(mmc);
1943
Pierre Ossman5e68d952006-01-08 14:21:52 +00001944 ret = wbsd_suspend(host, state);
1945 if (ret)
1946 return ret;
1947
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001948 wbsd_chip_poweroff(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 return 0;
1951}
1952
Pierre Ossman5e68d952006-01-08 14:21:52 +00001953static int wbsd_platform_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Russell King3ae5eae2005-11-09 22:32:44 +00001955 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001956 struct wbsd_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Pierre Ossman5e68d952006-01-08 14:21:52 +00001958 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001959 return 0;
1960
Pierre Ossman5e68d952006-01-08 14:21:52 +00001961 DBGF("Resuming...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001962
1963 host = mmc_priv(mmc);
1964
1965 wbsd_chip_config(host);
1966
1967 /*
1968 * Allow device to initialise itself properly.
1969 */
1970 mdelay(5);
1971
Pierre Ossman5e68d952006-01-08 14:21:52 +00001972 return wbsd_resume(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001974
Pierre Ossman5e68d952006-01-08 14:21:52 +00001975#ifdef CONFIG_PNP
1976
1977static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1978{
1979 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1980 struct wbsd_host *host;
1981
1982 if (mmc == NULL)
1983 return 0;
1984
1985 DBGF("Suspending...\n");
1986
1987 host = mmc_priv(mmc);
1988
1989 return wbsd_suspend(host, state);
1990}
1991
1992static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1993{
1994 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1995 struct wbsd_host *host;
1996
1997 if (mmc == NULL)
1998 return 0;
1999
2000 DBGF("Resuming...\n");
2001
2002 host = mmc_priv(mmc);
2003
2004 /*
2005 * See if chip needs to be configured.
2006 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002007 if (host->config != 0) {
2008 if (!wbsd_chip_validate(host)) {
Pierre Ossman5e68d952006-01-08 14:21:52 +00002009 printk(KERN_WARNING DRIVER_NAME
2010 ": PnP active but chip not configured! "
2011 "You probably have a buggy BIOS. "
2012 "Configuring chip manually.\n");
2013 wbsd_chip_config(host);
2014 }
2015 }
2016
2017 /*
2018 * Allow device to initialise itself properly.
2019 */
2020 mdelay(5);
2021
2022 return wbsd_resume(host);
2023}
2024
2025#endif /* CONFIG_PNP */
2026
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002027#else /* CONFIG_PM */
2028
Pierre Ossman5e68d952006-01-08 14:21:52 +00002029#define wbsd_platform_suspend NULL
2030#define wbsd_platform_resume NULL
2031
2032#define wbsd_pnp_suspend NULL
2033#define wbsd_pnp_resume NULL
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002034
2035#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Pierre Ossman85bcc132005-05-08 19:35:27 +01002037static struct platform_device *wbsd_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Russell King3ae5eae2005-11-09 22:32:44 +00002039static struct platform_driver wbsd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 .probe = wbsd_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002041 .remove = __devexit_p(wbsd_remove),
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002042
Pierre Ossman5e68d952006-01-08 14:21:52 +00002043 .suspend = wbsd_platform_suspend,
2044 .resume = wbsd_platform_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002045 .driver = {
2046 .name = DRIVER_NAME,
2047 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048};
2049
Pierre Ossman85bcc132005-05-08 19:35:27 +01002050#ifdef CONFIG_PNP
2051
2052static struct pnp_driver wbsd_pnp_driver = {
2053 .name = DRIVER_NAME,
2054 .id_table = pnp_dev_table,
2055 .probe = wbsd_pnp_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002056 .remove = __devexit_p(wbsd_pnp_remove),
Pierre Ossman5e68d952006-01-08 14:21:52 +00002057
2058 .suspend = wbsd_pnp_suspend,
2059 .resume = wbsd_pnp_resume,
Pierre Ossman85bcc132005-05-08 19:35:27 +01002060};
2061
2062#endif /* CONFIG_PNP */
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/*
2065 * Module loading/unloading
2066 */
2067
2068static int __init wbsd_drv_init(void)
2069{
2070 int result;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 printk(KERN_INFO DRIVER_NAME
2073 ": Winbond W83L51xD SD/MMC card interface driver, "
2074 DRIVER_VERSION "\n");
2075 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Pierre Ossman85bcc132005-05-08 19:35:27 +01002077#ifdef CONFIG_PNP
2078
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002079 if (!nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002080 result = pnp_register_driver(&wbsd_pnp_driver);
2081 if (result < 0)
2082 return result;
2083 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002084#endif /* CONFIG_PNP */
2085
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002086 if (nopnp) {
Russell King3ae5eae2005-11-09 22:32:44 +00002087 result = platform_driver_register(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002088 if (result < 0)
2089 return result;
2090
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002091 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002092 if (!wbsd_device) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002093 platform_driver_unregister(&wbsd_driver);
2094 return -ENOMEM;
2095 }
2096
2097 result = platform_device_add(wbsd_device);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002098 if (result) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002099 platform_device_put(wbsd_device);
2100 platform_driver_unregister(&wbsd_driver);
2101 return result;
2102 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 return 0;
2106}
2107
2108static void __exit wbsd_drv_exit(void)
2109{
Pierre Ossman85bcc132005-05-08 19:35:27 +01002110#ifdef CONFIG_PNP
2111
2112 if (!nopnp)
2113 pnp_unregister_driver(&wbsd_pnp_driver);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002114
2115#endif /* CONFIG_PNP */
Pierre Ossman85bcc132005-05-08 19:35:27 +01002116
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002117 if (nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002118 platform_device_unregister(wbsd_device);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002119
Russell King3ae5eae2005-11-09 22:32:44 +00002120 platform_driver_unregister(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 DBG("unloaded\n");
2124}
2125
2126module_init(wbsd_drv_init);
2127module_exit(wbsd_drv_exit);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002128#ifdef CONFIG_PNP
2129module_param(nopnp, uint, 0444);
2130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131module_param(io, uint, 0444);
2132module_param(irq, uint, 0444);
2133module_param(dma, int, 0444);
2134
2135MODULE_LICENSE("GPL");
Pierre Ossmande1d09e2005-11-09 23:21:49 +00002136MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
2138MODULE_VERSION(DRIVER_VERSION);
2139
Pierre Ossman85bcc132005-05-08 19:35:27 +01002140#ifdef CONFIG_PNP
2141MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2144MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2145MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");