blob: 05ccfc43168fb2c173305bcb003c765e6eafd24a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
3 *
Pierre Ossman5ba593a2006-11-21 17:45:37 +01004 * Copyright (C) 2004-2006 Pierre Ossman, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 *
12 * Warning!
13 *
14 * Changes to the FIFO system should be done with extreme care since
15 * the hardware is full of bugs related to the FIFO. Known issues are:
16 *
17 * - FIFO size field in FSR is always zero.
18 *
19 * - FIFO interrupts tend not to work as they should. Interrupts are
20 * triggered only for full/empty events, not for threshold values.
21 *
22 * - On APIC systems the FIFO empty interrupt is sometimes lost.
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010029#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/interrupt.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010031#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010033#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/mmc/host.h>
36#include <linux/mmc/protocol.h>
37
38#include <asm/io.h>
39#include <asm/dma.h>
40#include <asm/scatterlist.h>
41
42#include "wbsd.h"
43
44#define DRIVER_NAME "wbsd"
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 Ossman4a0ddbd2007-01-31 18:20:48 +0100274static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100276 return page_address(host->cur_sg->page) + host->cur_sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000279static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned int len, i, size;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000282 struct scatterlist *sg;
283 char *dmabuf = host->dma_buffer;
284 char *sgbuf;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 size = host->size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 sg = data->sg;
289 len = data->sg_len;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /*
292 * Just loop through all entries. Size might not
293 * be the entire list though so make sure that
294 * we do not transfer too much.
295 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000296 for (i = 0; i < len; i++) {
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100297 sgbuf = page_address(sg[i].page) + sg[i].offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (size < sg[i].length)
299 memcpy(dmabuf, sgbuf, size);
300 else
301 memcpy(dmabuf, sgbuf, sg[i].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dmabuf += sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (size < sg[i].length)
305 size = 0;
306 else
307 size -= sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (size == 0)
310 break;
311 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Check that we didn't get a request to transfer
315 * more data than can fit into the SG list.
316 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 BUG_ON(size != 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 host->size -= size;
321}
322
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000323static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 unsigned int len, i, size;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000326 struct scatterlist *sg;
327 char *dmabuf = host->dma_buffer;
328 char *sgbuf;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 size = host->size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sg = data->sg;
333 len = data->sg_len;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /*
336 * Just loop through all entries. Size might not
337 * be the entire list though so make sure that
338 * we do not transfer too much.
339 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000340 for (i = 0; i < len; i++) {
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100341 sgbuf = page_address(sg[i].page) + sg[i].offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (size < sg[i].length)
343 memcpy(sgbuf, dmabuf, size);
344 else
345 memcpy(sgbuf, dmabuf, sg[i].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dmabuf += sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (size < sg[i].length)
349 size = 0;
350 else
351 size -= sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (size == 0)
354 break;
355 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * Check that we didn't get a request to transfer
359 * more data than can fit into the SG list.
360 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 BUG_ON(size != 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 host->size -= size;
365}
366
367/*
368 * Command handling
369 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100370
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000371static inline void wbsd_get_short_reply(struct wbsd_host *host,
372 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 /*
375 * Correct response type?
376 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000377 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 cmd->error = MMC_ERR_INVALID;
379 return;
380 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100381
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000382 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
383 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
386 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000389static inline void wbsd_get_long_reply(struct wbsd_host *host,
390 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Correct response type?
396 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000397 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 cmd->error = MMC_ERR_INVALID;
399 return;
400 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100401
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000402 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 cmd->resp[i] =
404 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
405 cmd->resp[i] |=
406 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
407 cmd->resp[i] |=
408 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
409 cmd->resp[i] |=
410 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
411 }
412}
413
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000414static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int i;
417 u8 status, isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 DBGF("Sending cmd (%x)\n", cmd->opcode);
420
421 /*
422 * Clear accumulated ISR. The interrupt routine
423 * will fill this one with events that occur during
424 * transfer.
425 */
426 host->isr = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Send the command (CRC calculated by host).
430 */
431 outb(cmd->opcode, host->base + WBSD_CMDR);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000432 for (i = 3; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 cmd->error = MMC_ERR_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /*
438 * Wait for the request to complete.
439 */
440 do {
441 status = wbsd_read_index(host, WBSD_IDX_STATUS);
442 } while (status & WBSD_CARDTRAFFIC);
443
444 /*
445 * Do we expect a reply?
446 */
Russell Kinge9225172006-02-02 12:23:12 +0000447 if (cmd->flags & MMC_RSP_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Read back status.
450 */
451 isr = host->isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Card removed? */
454 if (isr & WBSD_INT_CARD)
455 cmd->error = MMC_ERR_TIMEOUT;
456 /* Timeout? */
457 else if (isr & WBSD_INT_TIMEOUT)
458 cmd->error = MMC_ERR_TIMEOUT;
459 /* CRC? */
460 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
461 cmd->error = MMC_ERR_BADCRC;
462 /* All ok */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000463 else {
Russell Kinge9225172006-02-02 12:23:12 +0000464 if (cmd->flags & MMC_RSP_136)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 wbsd_get_long_reply(host, cmd);
Russell Kinge9225172006-02-02 12:23:12 +0000466 else
467 wbsd_get_short_reply(host, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470
471 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
472}
473
474/*
475 * Data functions
476 */
477
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000478static void wbsd_empty_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000480 struct mmc_data *data = host->mrq->cmd->data;
481 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Handle excessive data.
486 */
487 if (data->bytes_xfered == host->size)
488 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100489
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100490 buffer = wbsd_sg_to_buffer(host) + host->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /*
493 * Drain the fifo. This has a tendency to loop longer
494 * than the FIFO length (usually one block).
495 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000496 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * The size field in the FSR is broken so we have to
499 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (fsr & WBSD_FIFO_FULL)
502 fifo = 16;
503 else if (fsr & WBSD_FIFO_FUTHRE)
504 fifo = 8;
505 else
506 fifo = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100507
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000508 for (i = 0; i < fifo; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buffer = inb(host->base + WBSD_DFR);
510 buffer++;
511 host->offset++;
512 host->remain--;
513
514 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Transfer done?
518 */
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100519 if (data->bytes_xfered == host->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * End of scatter list entry?
524 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000525 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Get next entry. Check if last.
528 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000529 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * We should never reach this point.
532 * It means that we're trying to
533 * transfer more blocks than can fit
534 * into the scatter list.
535 */
536 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return;
541 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100542
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100543 buffer = wbsd_sg_to_buffer(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
546 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * This is a very dirty hack to solve a
550 * hardware problem. The chip doesn't trigger
551 * FIFO threshold interrupts properly.
552 */
553 if ((host->size - data->bytes_xfered) < 16)
554 tasklet_schedule(&host->fifo_tasklet);
555}
556
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000557static void wbsd_fill_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000559 struct mmc_data *data = host->mrq->cmd->data;
560 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * Check that we aren't being called after the
565 * entire buffer has been transfered.
566 */
567 if (data->bytes_xfered == host->size)
568 return;
569
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100570 buffer = wbsd_sg_to_buffer(host) + host->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * Fill the fifo. This has a tendency to loop longer
574 * than the FIFO length (usually one block).
575 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000576 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * The size field in the FSR is broken so we have to
579 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (fsr & WBSD_FIFO_EMPTY)
582 fifo = 0;
583 else if (fsr & WBSD_FIFO_EMTHRE)
584 fifo = 8;
585 else
586 fifo = 15;
587
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000588 for (i = 16; i > fifo; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 outb(*buffer, host->base + WBSD_DFR);
590 buffer++;
591 host->offset++;
592 host->remain--;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Transfer done?
598 */
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100599 if (data->bytes_xfered == host->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
603 * End of scatter list entry?
604 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000605 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * Get next entry. Check if last.
608 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000609 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * We should never reach this point.
612 * It means that we're trying to
613 * transfer more blocks than can fit
614 * into the scatter list.
615 */
616 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return;
621 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100622
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100623 buffer = wbsd_sg_to_buffer(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 }
626 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100627
Pierre Ossman85bcc132005-05-08 19:35:27 +0100628 /*
629 * The controller stops sending interrupts for
630 * 'FIFO empty' under certain conditions. So we
631 * need to be a bit more pro-active.
632 */
633 tasklet_schedule(&host->fifo_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000636static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 u16 blksize;
639 u8 setup;
640 unsigned long dmaflags;
641
642 DBGF("blksz %04x blks %04x flags %08x\n",
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100643 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 DBGF("tsac %d ms nsac %d clk\n",
645 data->timeout_ns / 1000000, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Calculate size.
649 */
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100650 host->size = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
653 * Check timeout values for overflow.
654 * (Yes, some cards cause this value to overflow).
655 */
656 if (data->timeout_ns > 127000000)
657 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000658 else {
659 wbsd_write_index(host, WBSD_IDX_TAAC,
660 data->timeout_ns / 1000000);
661 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (data->timeout_clks > 255)
664 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
665 else
666 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * Inform the chip of how large blocks will be
670 * sent. It needs this to determine when to
671 * calculate CRC.
672 *
673 * Space for CRC must be included in the size.
Pierre Ossman65ae2112005-09-06 15:18:57 -0700674 * Two bytes are needed for each data line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000676 if (host->bus_width == MMC_BUS_WIDTH_1) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100677 blksize = data->blksz + 2;
Pierre Ossman65ae2112005-09-06 15:18:57 -0700678
679 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
680 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000681 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100682 blksize = data->blksz + 2 * 4;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100683
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000684 wbsd_write_index(host, WBSD_IDX_PBSMSB,
685 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700686 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000687 } else {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700688 data->error = MMC_ERR_INVALID;
689 return;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /*
693 * Clear the FIFO. This is needed even for DMA
694 * transfers since the chip still uses the FIFO
695 * internally.
696 */
697 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
698 setup |= WBSD_FIFO_RESET;
699 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * DMA transfer?
703 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000704 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * The buffer for DMA is only 64 kB.
707 */
708 BUG_ON(host->size > 0x10000);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000709 if (host->size > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 data->error = MMC_ERR_INVALID;
711 return;
712 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Transfer data from the SG list to
716 * the DMA buffer.
717 */
718 if (data->flags & MMC_DATA_WRITE)
719 wbsd_sg_to_dma(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
722 * Initialise the ISA DMA controller.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dmaflags = claim_dma_lock();
725 disable_dma(host->dma);
726 clear_dma_ff(host->dma);
727 if (data->flags & MMC_DATA_READ)
728 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
729 else
730 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
731 set_dma_addr(host->dma, host->dma_addr);
732 set_dma_count(host->dma, host->size);
733
734 enable_dma(host->dma);
735 release_dma_lock(dmaflags);
736
737 /*
738 * Enable DMA on the host.
739 */
740 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000741 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * This flag is used to keep printk
744 * output to a minimum.
745 */
746 host->firsterr = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Initialise the SG list.
750 */
751 wbsd_init_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * Turn off DMA.
755 */
756 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /*
759 * Set up FIFO threshold levels (and fill
760 * buffer if doing a write).
761 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000762 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 wbsd_write_index(host, WBSD_IDX_FIFOEN,
764 WBSD_FIFOEN_FULL | 8);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 wbsd_write_index(host, WBSD_IDX_FIFOEN,
767 WBSD_FIFOEN_EMPTY | 8);
768 wbsd_fill_fifo(host);
769 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 data->error = MMC_ERR_NONE;
773}
774
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000775static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 unsigned long dmaflags;
778 int count;
779 u8 status;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 WARN_ON(host->mrq == NULL);
782
783 /*
784 * Send a stop command if needed.
785 */
786 if (data->stop)
787 wbsd_send_command(host, data->stop);
788
789 /*
790 * Wait for the controller to leave data
791 * transfer state.
792 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000793 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 status = wbsd_read_index(host, WBSD_IDX_STATUS);
795 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * DMA transfer?
799 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000800 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Disable DMA on the host.
803 */
804 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Turn of ISA DMA controller.
808 */
809 dmaflags = claim_dma_lock();
810 disable_dma(host->dma);
811 clear_dma_ff(host->dma);
812 count = get_dma_residue(host->dma);
813 release_dma_lock(dmaflags);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * Any leftover data?
817 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000818 if (count) {
Pierre Ossmand1916342005-11-05 10:36:35 +0000819 printk(KERN_ERR "%s: Incomplete DMA transfer. "
820 "%d bytes left.\n",
821 mmc_hostname(host->mmc), count);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 data->error = MMC_ERR_FAILED;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * Transfer data from DMA buffer to
827 * SG list.
828 */
829 if (data->flags & MMC_DATA_READ)
830 wbsd_dma_to_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 data->bytes_xfered = host->size;
833 }
834 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 wbsd_request_end(host, host->mrq);
839}
840
Pierre Ossman85bcc132005-05-08 19:35:27 +0100841/*****************************************************************************\
842 * *
843 * MMC layer callbacks *
844 * *
845\*****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000847static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000849 struct wbsd_host *host = mmc_priv(mmc);
850 struct mmc_command *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Disable tasklets to avoid a deadlock.
854 */
855 spin_lock_bh(&host->lock);
856
857 BUG_ON(host->mrq != NULL);
858
859 cmd = mrq->cmd;
860
861 host->mrq = mrq;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * If there is no card in the slot then
865 * timeout immediatly.
866 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000867 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 cmd->error = MMC_ERR_TIMEOUT;
869 goto done;
870 }
871
872 /*
873 * Does the request include data?
874 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000875 if (cmd->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 wbsd_prepare_data(host, cmd->data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (cmd->data->error != MMC_ERR_NONE)
879 goto done;
880 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 wbsd_send_command(host, cmd);
883
884 /*
885 * If this is a data transfer the request
886 * will be finished after the data has
887 * transfered.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100888 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000889 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100891 * The hardware is so delightfully stupid that it has a list
892 * of "data" commands. If a command isn't on this list, it'll
893 * just go back to the idle state and won't send any data
894 * interrupts.
895 */
896 switch (cmd->opcode) {
897 case 11:
898 case 17:
899 case 18:
900 case 20:
901 case 24:
902 case 25:
903 case 26:
904 case 27:
905 case 30:
906 case 42:
907 case 56:
908 break;
909
910 /* ACMDs. We don't keep track of state, so we just treat them
911 * like any other command. */
912 case 51:
913 break;
914
915 default:
916#ifdef CONFIG_MMC_DEBUG
917 printk(KERN_WARNING "%s: Data command %d is not "
918 "supported by this controller.\n",
919 mmc_hostname(host->mmc), cmd->opcode);
920#endif
921 cmd->data->error = MMC_ERR_INVALID;
922
923 if (cmd->data->stop)
924 wbsd_send_command(host, cmd->data->stop);
925
926 goto done;
927 };
928
929 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * Dirty fix for hardware bug.
931 */
932 if (host->dma == -1)
933 tasklet_schedule(&host->fifo_tasklet);
934
935 spin_unlock_bh(&host->lock);
936
937 return;
938 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940done:
941 wbsd_request_end(host, mrq);
942
943 spin_unlock_bh(&host->lock);
944}
945
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000946static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000948 struct wbsd_host *host = mmc_priv(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u8 clk, setup, pwr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_lock_bh(&host->lock);
952
953 /*
954 * Reset the chip on each power off.
955 * Should clear out any weird states.
956 */
957 if (ios->power_mode == MMC_POWER_OFF)
958 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (ios->clock >= 24000000)
961 clk = WBSD_CLK_24M;
962 else if (ios->clock >= 16000000)
963 clk = WBSD_CLK_16M;
964 else if (ios->clock >= 12000000)
965 clk = WBSD_CLK_12M;
966 else
967 clk = WBSD_CLK_375K;
968
969 /*
970 * Only write to the clock register when
971 * there is an actual change.
972 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000973 if (clk != host->clk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 wbsd_write_index(host, WBSD_IDX_CLK, clk);
975 host->clk = clk;
976 }
977
Pierre Ossman85bcc132005-05-08 19:35:27 +0100978 /*
979 * Power up card.
980 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000981 if (ios->power_mode != MMC_POWER_OFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 pwr = inb(host->base + WBSD_CSR);
983 pwr &= ~WBSD_POWER_N;
984 outb(pwr, host->base + WBSD_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Pierre Ossman85bcc132005-05-08 19:35:27 +0100987 /*
988 * MMC cards need to have pin 1 high during init.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100989 * It wreaks havoc with the card detection though so
Pierre Ossman1656fa52005-09-03 16:45:49 +0100990 * that needs to be disabled.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100991 */
992 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000993 if (ios->chip_select == MMC_CS_HIGH) {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700994 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
Pierre Ossman85bcc132005-05-08 19:35:27 +0100995 setup |= WBSD_DAT3_H;
996 host->flags |= WBSD_FIGNORE_DETECT;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000997 } else {
998 if (setup & WBSD_DAT3_H) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100999 setup &= ~WBSD_DAT3_H;
Pierre Ossman1656fa52005-09-03 16:45:49 +01001000
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001001 /*
1002 * We cannot resume card detection immediatly
1003 * because of capacitance and delays in the chip.
1004 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001005 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001006 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001007 }
1008 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001009
Pierre Ossman65ae2112005-09-06 15:18:57 -07001010 /*
1011 * Store bus width for later. Will be used when
1012 * setting up the data transfer.
1013 */
1014 host->bus_width = ios->bus_width;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 spin_unlock_bh(&host->lock);
1017}
1018
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001019static int wbsd_get_ro(struct mmc_host *mmc)
Pierre Ossman65ae2112005-09-06 15:18:57 -07001020{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001021 struct wbsd_host *host = mmc_priv(mmc);
Pierre Ossman65ae2112005-09-06 15:18:57 -07001022 u8 csr;
1023
1024 spin_lock_bh(&host->lock);
1025
1026 csr = inb(host->base + WBSD_CSR);
1027 csr |= WBSD_MSLED;
1028 outb(csr, host->base + WBSD_CSR);
1029
1030 mdelay(1);
1031
1032 csr = inb(host->base + WBSD_CSR);
1033 csr &= ~WBSD_MSLED;
1034 outb(csr, host->base + WBSD_CSR);
1035
1036 spin_unlock_bh(&host->lock);
1037
1038 return csr & WBSD_WRPT;
1039}
1040
David Brownellab7aefd2006-11-12 17:55:30 -08001041static const struct mmc_host_ops wbsd_ops = {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001042 .request = wbsd_request,
1043 .set_ios = wbsd_set_ios,
Pierre Ossman65ae2112005-09-06 15:18:57 -07001044 .get_ro = wbsd_get_ro,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001045};
1046
1047/*****************************************************************************\
1048 * *
1049 * Interrupt handling *
1050 * *
1051\*****************************************************************************/
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001054 * Helper function to reset detection ignore
1055 */
1056
1057static void wbsd_reset_ignore(unsigned long data)
1058{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001059 struct wbsd_host *host = (struct wbsd_host *)data;
Pierre Ossman1656fa52005-09-03 16:45:49 +01001060
1061 BUG_ON(host == NULL);
1062
1063 DBG("Resetting card detection ignore\n");
1064
1065 spin_lock_bh(&host->lock);
1066
1067 host->flags &= ~WBSD_FIGNORE_DETECT;
1068
1069 /*
1070 * Card status might have changed during the
1071 * blackout.
1072 */
1073 tasklet_schedule(&host->card_tasklet);
1074
1075 spin_unlock_bh(&host->lock);
1076}
1077
1078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * Tasklets
1080 */
1081
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001082static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 WARN_ON(!host->mrq);
1085 if (!host->mrq)
1086 return NULL;
1087
1088 WARN_ON(!host->mrq->cmd);
1089 if (!host->mrq->cmd)
1090 return NULL;
1091
1092 WARN_ON(!host->mrq->cmd->data);
1093 if (!host->mrq->cmd->data)
1094 return NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return host->mrq->cmd->data;
1097}
1098
1099static void wbsd_tasklet_card(unsigned long param)
1100{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001101 struct wbsd_host *host = (struct wbsd_host *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 u8 csr;
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001103 int delay = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001106
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001107 if (host->flags & WBSD_FIGNORE_DETECT) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001108 spin_unlock(&host->lock);
1109 return;
1110 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 csr = inb(host->base + WBSD_CSR);
1113 WARN_ON(csr == 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001114
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001115 if (csr & WBSD_CARDPRESENT) {
1116 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001117 DBG("Card inserted\n");
1118 host->flags |= WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001119
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001120 delay = 500;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001121 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001122 } else if (host->flags & WBSD_FCARD_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 DBG("Card removed\n");
Pierre Ossman85bcc132005-05-08 19:35:27 +01001124 host->flags &= ~WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001125
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001126 if (host->mrq) {
Pierre Ossmand1916342005-11-05 10:36:35 +00001127 printk(KERN_ERR "%s: Card removed during transfer!\n",
1128 mmc_hostname(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 wbsd_reset(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 host->mrq->cmd->error = MMC_ERR_FAILED;
1132 tasklet_schedule(&host->finish_tasklet);
1133 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001134
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001135 delay = 0;
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001136 }
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001137
1138 /*
1139 * Unlock first since we might get a call back.
1140 */
1141
1142 spin_unlock(&host->lock);
1143
1144 if (delay != -1)
1145 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static void wbsd_tasklet_fifo(unsigned long param)
1149{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001150 struct wbsd_host *host = (struct wbsd_host *)param;
1151 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!host->mrq)
1156 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 data = wbsd_get_data(host);
1159 if (!data)
1160 goto end;
1161
1162 if (data->flags & MMC_DATA_WRITE)
1163 wbsd_fill_fifo(host);
1164 else
1165 wbsd_empty_fifo(host);
1166
1167 /*
1168 * Done?
1169 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001170 if (host->size == data->bytes_xfered) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1172 tasklet_schedule(&host->finish_tasklet);
1173 }
1174
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001175end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_unlock(&host->lock);
1177}
1178
1179static void wbsd_tasklet_crc(unsigned long param)
1180{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001181 struct wbsd_host *host = (struct wbsd_host *)param;
1182 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!host->mrq)
1187 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 data = wbsd_get_data(host);
1190 if (!data)
1191 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 DBGF("CRC error\n");
1194
1195 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 tasklet_schedule(&host->finish_tasklet);
1198
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001199end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 spin_unlock(&host->lock);
1201}
1202
1203static void wbsd_tasklet_timeout(unsigned long param)
1204{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001205 struct wbsd_host *host = (struct wbsd_host *)param;
1206 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!host->mrq)
1211 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 data = wbsd_get_data(host);
1214 if (!data)
1215 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 DBGF("Timeout\n");
1218
1219 data->error = MMC_ERR_TIMEOUT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 tasklet_schedule(&host->finish_tasklet);
1222
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001223end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock(&host->lock);
1225}
1226
1227static void wbsd_tasklet_finish(unsigned long param)
1228{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001229 struct wbsd_host *host = (struct wbsd_host *)param;
1230 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 WARN_ON(!host->mrq);
1235 if (!host->mrq)
1236 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 data = wbsd_get_data(host);
1239 if (!data)
1240 goto end;
1241
1242 wbsd_finish_data(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001243
1244end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 spin_unlock(&host->lock);
1246}
1247
1248static void wbsd_tasklet_block(unsigned long param)
1249{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001250 struct wbsd_host *host = (struct wbsd_host *)param;
1251 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 spin_lock(&host->lock);
1254
1255 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001256 WBSD_CRC_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 data = wbsd_get_data(host);
1258 if (!data)
1259 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 DBGF("CRC error\n");
1262
1263 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 tasklet_schedule(&host->finish_tasklet);
1266 }
1267
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001268end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 spin_unlock(&host->lock);
1270}
1271
1272/*
1273 * Interrupt handling
1274 */
1275
David Howells7d12e782006-10-05 14:55:46 +01001276static irqreturn_t wbsd_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001278 struct wbsd_host *host = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 isr = inb(host->base + WBSD_ISR);
1282
1283 /*
1284 * Was it actually our hardware that caused the interrupt?
1285 */
1286 if (isr == 0xff || isr == 0x00)
1287 return IRQ_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 host->isr |= isr;
1290
1291 /*
1292 * Schedule tasklets as needed.
1293 */
1294 if (isr & WBSD_INT_CARD)
1295 tasklet_schedule(&host->card_tasklet);
1296 if (isr & WBSD_INT_FIFO_THRE)
1297 tasklet_schedule(&host->fifo_tasklet);
1298 if (isr & WBSD_INT_CRC)
1299 tasklet_hi_schedule(&host->crc_tasklet);
1300 if (isr & WBSD_INT_TIMEOUT)
1301 tasklet_hi_schedule(&host->timeout_tasklet);
1302 if (isr & WBSD_INT_BUSYEND)
1303 tasklet_hi_schedule(&host->block_tasklet);
1304 if (isr & WBSD_INT_TC)
1305 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return IRQ_HANDLED;
1308}
1309
Pierre Ossman85bcc132005-05-08 19:35:27 +01001310/*****************************************************************************\
1311 * *
1312 * Device initialisation and shutdown *
1313 * *
1314\*****************************************************************************/
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001317 * Allocate/free MMC structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
1319
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001320static int __devinit wbsd_alloc_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001321{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001322 struct mmc_host *mmc;
1323 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001324
Pierre Ossman85bcc132005-05-08 19:35:27 +01001325 /*
1326 * Allocate MMC structure.
1327 */
1328 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1329 if (!mmc)
1330 return -ENOMEM;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001331
Pierre Ossman85bcc132005-05-08 19:35:27 +01001332 host = mmc_priv(mmc);
1333 host->mmc = mmc;
1334
1335 host->dma = -1;
1336
1337 /*
1338 * Set host parameters.
1339 */
1340 mmc->ops = &wbsd_ops;
1341 mmc->f_min = 375000;
1342 mmc->f_max = 24000000;
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001343 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Russell King42431ac2006-09-24 10:44:09 +01001344 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001345
Pierre Ossman85bcc132005-05-08 19:35:27 +01001346 spin_lock_init(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001347
Pierre Ossman85bcc132005-05-08 19:35:27 +01001348 /*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001349 * Set up timers
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001350 */
Pierre Ossman1656fa52005-09-03 16:45:49 +01001351 init_timer(&host->ignore_timer);
1352 host->ignore_timer.data = (unsigned long)host;
1353 host->ignore_timer.function = wbsd_reset_ignore;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001354
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001355 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001356 * Maximum number of segments. Worst case is one sector per segment
1357 * so this will be 64kB/512.
1358 */
1359 mmc->max_hw_segs = 128;
1360 mmc->max_phys_segs = 128;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001361
Pierre Ossman85bcc132005-05-08 19:35:27 +01001362 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001363 * Maximum request size. Also limited by 64KiB buffer.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001364 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001365 mmc->max_req_size = 65536;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001366
Pierre Ossman85bcc132005-05-08 19:35:27 +01001367 /*
1368 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001369 * of bytes.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001370 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001371 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001372
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001373 /*
1374 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1375 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1376 */
1377 mmc->max_blk_size = 4087;
1378
Pierre Ossman55db8902006-11-21 17:55:45 +01001379 /*
1380 * Maximum block count. There is no real limit so the maximum
1381 * request size will be the only restriction.
1382 */
1383 mmc->max_blk_count = mmc->max_req_size;
1384
Pierre Ossman85bcc132005-05-08 19:35:27 +01001385 dev_set_drvdata(dev, mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001386
Pierre Ossman85bcc132005-05-08 19:35:27 +01001387 return 0;
1388}
1389
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001390static void __devexit wbsd_free_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001391{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001392 struct mmc_host *mmc;
1393 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001394
Pierre Ossman85bcc132005-05-08 19:35:27 +01001395 mmc = dev_get_drvdata(dev);
1396 if (!mmc)
1397 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001398
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001399 host = mmc_priv(mmc);
1400 BUG_ON(host == NULL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001401
Pierre Ossman1656fa52005-09-03 16:45:49 +01001402 del_timer_sync(&host->ignore_timer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001403
Pierre Ossman85bcc132005-05-08 19:35:27 +01001404 mmc_free_host(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001405
Pierre Ossman85bcc132005-05-08 19:35:27 +01001406 dev_set_drvdata(dev, NULL);
1407}
1408
1409/*
1410 * Scan for known chip id:s
1411 */
1412
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001413static int __devinit wbsd_scan(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int i, j, k;
1416 int id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * Iterate through all ports, all codes to
1420 * find hardware that is in our known list.
1421 */
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001422 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1424 continue;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001425
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001426 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 id = 0xFFFF;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001428
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001429 host->config = config_ports[i];
1430 host->unlock_code = unlock_codes[j];
1431
1432 wbsd_unlock_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 outb(WBSD_CONF_ID_HI, config_ports[i]);
1435 id = inb(config_ports[i] + 1) << 8;
1436
1437 outb(WBSD_CONF_ID_LO, config_ports[i]);
1438 id |= inb(config_ports[i] + 1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001439
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001440 wbsd_lock_config(host);
1441
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001442 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001443 if (id == valid_ids[k]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 host->chip_id = id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447 }
1448 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001449
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001450 if (id != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 DBG("Unknown hardware (id %x) found at %x\n",
1452 id, config_ports[i]);
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 release_region(config_ports[i], 2);
1457 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001458
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001459 host->config = 0;
1460 host->unlock_code = 0;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENODEV;
1463}
1464
Pierre Ossman85bcc132005-05-08 19:35:27 +01001465/*
1466 * Allocate/free io port ranges
1467 */
1468
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001469static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001471 if (base & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EINVAL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001473
Pierre Ossman85bcc132005-05-08 19:35:27 +01001474 if (!request_region(base, 8, DRIVER_NAME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -EIO;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001476
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001477 host->base = base;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
1481
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001482static void __devexit wbsd_release_regions(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 if (host->base)
1485 release_region(host->base, 8);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001486
Pierre Ossman85bcc132005-05-08 19:35:27 +01001487 host->base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (host->config)
1490 release_region(host->config, 2);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001491
Pierre Ossman85bcc132005-05-08 19:35:27 +01001492 host->config = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Pierre Ossman85bcc132005-05-08 19:35:27 +01001495/*
1496 * Allocate/free DMA port and buffer
1497 */
1498
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001499static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (dma < 0)
1502 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (request_dma(dma, DRIVER_NAME))
1505 goto err;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * We need to allocate a special buffer in
1509 * order for ISA to be able to DMA to it.
1510 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001511 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1513 if (!host->dma_buffer)
1514 goto free;
1515
1516 /*
1517 * Translate the address to a physical address.
1518 */
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001519 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001520 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * ISA DMA must be aligned on a 64k basis.
1524 */
1525 if ((host->dma_addr & 0xffff) != 0)
1526 goto kfree;
1527 /*
1528 * ISA cannot access memory above 16 MB.
1529 */
1530 else if (host->dma_addr >= 0x1000000)
1531 goto kfree;
1532
1533 host->dma = dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537kfree:
1538 /*
1539 * If we've gotten here then there is some kind of alignment bug
1540 */
1541 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001542
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001543 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001544 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001545 host->dma_addr = (dma_addr_t)NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 kfree(host->dma_buffer);
1548 host->dma_buffer = NULL;
1549
1550free:
1551 free_dma(dma);
1552
1553err:
1554 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1555 "Falling back on FIFO.\n", dma);
1556}
1557
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001558static void __devexit wbsd_release_dma(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001559{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001560 if (host->dma_addr) {
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001561 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001562 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1563 }
Jesper Juhl6044ec82005-11-07 01:01:32 -08001564 kfree(host->dma_buffer);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001565 if (host->dma >= 0)
1566 free_dma(host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001567
Pierre Ossman85bcc132005-05-08 19:35:27 +01001568 host->dma = -1;
1569 host->dma_buffer = NULL;
1570 host->dma_addr = (dma_addr_t)NULL;
1571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001574 * Allocate/free IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
1576
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001577static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * Allocate interrupt.
1583 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001584
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001585 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (ret)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001587 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 host->irq = irq;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Set up tasklets.
1593 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001594 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1595 (unsigned long)host);
1596 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1597 (unsigned long)host);
1598 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1599 (unsigned long)host);
1600 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1601 (unsigned long)host);
1602 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1603 (unsigned long)host);
1604 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1605 (unsigned long)host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001610static void __devexit wbsd_release_irq(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Pierre Ossman85bcc132005-05-08 19:35:27 +01001612 if (!host->irq)
1613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 free_irq(host->irq, host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001616
Pierre Ossman85bcc132005-05-08 19:35:27 +01001617 host->irq = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 tasklet_kill(&host->card_tasklet);
1620 tasklet_kill(&host->fifo_tasklet);
1621 tasklet_kill(&host->crc_tasklet);
1622 tasklet_kill(&host->timeout_tasklet);
1623 tasklet_kill(&host->finish_tasklet);
1624 tasklet_kill(&host->block_tasklet);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001625}
1626
1627/*
1628 * Allocate all resources for the host.
1629 */
1630
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001631static int __devinit wbsd_request_resources(struct wbsd_host *host,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001632 int base, int irq, int dma)
1633{
1634 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001635
Pierre Ossman85bcc132005-05-08 19:35:27 +01001636 /*
1637 * Allocate I/O ports.
1638 */
1639 ret = wbsd_request_region(host, base);
1640 if (ret)
1641 return ret;
1642
1643 /*
1644 * Allocate interrupt.
1645 */
1646 ret = wbsd_request_irq(host, irq);
1647 if (ret)
1648 return ret;
1649
1650 /*
1651 * Allocate DMA.
1652 */
1653 wbsd_request_dma(host, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001654
Pierre Ossman85bcc132005-05-08 19:35:27 +01001655 return 0;
1656}
1657
1658/*
1659 * Release all resources for the host.
1660 */
1661
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001662static void __devexit wbsd_release_resources(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001663{
1664 wbsd_release_dma(host);
1665 wbsd_release_irq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 wbsd_release_regions(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001667}
1668
1669/*
1670 * Configure the resources the chip should use.
1671 */
1672
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001673static void wbsd_chip_config(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001674{
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001675 wbsd_unlock_config(host);
1676
Pierre Ossman85bcc132005-05-08 19:35:27 +01001677 /*
1678 * Reset the chip.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001679 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001680 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1681 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1682
1683 /*
1684 * Select SD/MMC function.
1685 */
1686 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001687
Pierre Ossman85bcc132005-05-08 19:35:27 +01001688 /*
1689 * Set up card detection.
1690 */
1691 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001692
Pierre Ossman85bcc132005-05-08 19:35:27 +01001693 /*
1694 * Configure chip
1695 */
1696 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1697 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001698
Pierre Ossman85bcc132005-05-08 19:35:27 +01001699 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001700
Pierre Ossman85bcc132005-05-08 19:35:27 +01001701 if (host->dma >= 0)
1702 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001703
Pierre Ossman85bcc132005-05-08 19:35:27 +01001704 /*
1705 * Enable and power up chip.
1706 */
1707 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1708 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001709
1710 wbsd_lock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001711}
1712
1713/*
1714 * Check that configured resources are correct.
1715 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001716
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001717static int wbsd_chip_validate(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001718{
1719 int base, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001720
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001721 wbsd_unlock_config(host);
1722
Pierre Ossman85bcc132005-05-08 19:35:27 +01001723 /*
1724 * Select SD/MMC function.
1725 */
1726 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001727
Pierre Ossman85bcc132005-05-08 19:35:27 +01001728 /*
1729 * Read configuration.
1730 */
1731 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1732 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001733
Pierre Ossman85bcc132005-05-08 19:35:27 +01001734 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001735
Pierre Ossman85bcc132005-05-08 19:35:27 +01001736 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001737
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001738 wbsd_lock_config(host);
1739
Pierre Ossman85bcc132005-05-08 19:35:27 +01001740 /*
1741 * Validate against given configuration.
1742 */
1743 if (base != host->base)
1744 return 0;
1745 if (irq != host->irq)
1746 return 0;
1747 if ((dma != host->dma) && (host->dma != -1))
1748 return 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001749
Pierre Ossman85bcc132005-05-08 19:35:27 +01001750 return 1;
1751}
1752
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001753/*
1754 * Powers down the SD function
1755 */
1756
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001757static void wbsd_chip_poweroff(struct wbsd_host *host)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001758{
1759 wbsd_unlock_config(host);
1760
1761 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1762 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1763
1764 wbsd_lock_config(host);
1765}
1766
Pierre Ossman85bcc132005-05-08 19:35:27 +01001767/*****************************************************************************\
1768 * *
1769 * Devices setup and shutdown *
1770 * *
1771\*****************************************************************************/
1772
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001773static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001774 int pnp)
1775{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001776 struct wbsd_host *host = NULL;
1777 struct mmc_host *mmc = NULL;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001778 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001779
Pierre Ossman85bcc132005-05-08 19:35:27 +01001780 ret = wbsd_alloc_mmc(dev);
1781 if (ret)
1782 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001783
Pierre Ossman85bcc132005-05-08 19:35:27 +01001784 mmc = dev_get_drvdata(dev);
1785 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001786
Pierre Ossman85bcc132005-05-08 19:35:27 +01001787 /*
1788 * Scan for hardware.
1789 */
1790 ret = wbsd_scan(host);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001791 if (ret) {
1792 if (pnp && (ret == -ENODEV)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001793 printk(KERN_WARNING DRIVER_NAME
1794 ": Unable to confirm device presence. You may "
1795 "experience lock-ups.\n");
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001796 } else {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001797 wbsd_free_mmc(dev);
1798 return ret;
1799 }
1800 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001801
Pierre Ossman85bcc132005-05-08 19:35:27 +01001802 /*
1803 * Request resources.
1804 */
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001805 ret = wbsd_request_resources(host, base, irq, dma);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001806 if (ret) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001807 wbsd_release_resources(host);
1808 wbsd_free_mmc(dev);
1809 return ret;
1810 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001811
Pierre Ossman85bcc132005-05-08 19:35:27 +01001812 /*
1813 * See if chip needs to be configured.
1814 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001815 if (pnp) {
1816 if ((host->config != 0) && !wbsd_chip_validate(host)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001817 printk(KERN_WARNING DRIVER_NAME
1818 ": PnP active but chip not configured! "
1819 "You probably have a buggy BIOS. "
1820 "Configuring chip manually.\n");
1821 wbsd_chip_config(host);
1822 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001823 } else
Pierre Ossman85bcc132005-05-08 19:35:27 +01001824 wbsd_chip_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001825
Pierre Ossman85bcc132005-05-08 19:35:27 +01001826 /*
1827 * Power Management stuff. No idea how this works.
1828 * Not tested.
1829 */
1830#ifdef CONFIG_PM
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001831 if (host->config) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001832 wbsd_unlock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001833 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001834 wbsd_lock_config(host);
1835 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001836#endif
1837 /*
1838 * Allow device to initialise itself properly.
1839 */
1840 mdelay(5);
1841
1842 /*
1843 * Reset the chip into a known state.
1844 */
1845 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001846
Pierre Ossman85bcc132005-05-08 19:35:27 +01001847 mmc_add_host(mmc);
1848
Russell Kingd366b642005-08-19 09:40:08 +01001849 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
Pierre Ossman85bcc132005-05-08 19:35:27 +01001850 if (host->chip_id != 0)
1851 printk(" id %x", (int)host->chip_id);
1852 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1853 if (host->dma >= 0)
1854 printk(" dma %d", (int)host->dma);
1855 else
1856 printk(" FIFO");
1857 if (pnp)
1858 printk(" PnP");
1859 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 return 0;
1862}
1863
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001864static void __devexit wbsd_shutdown(struct device *dev, int pnp)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001865{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001866 struct mmc_host *mmc = dev_get_drvdata(dev);
1867 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001868
Pierre Ossman85bcc132005-05-08 19:35:27 +01001869 if (!mmc)
1870 return;
1871
1872 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001873
Pierre Ossman85bcc132005-05-08 19:35:27 +01001874 mmc_remove_host(mmc);
1875
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001876 /*
1877 * Power down the SD/MMC function.
1878 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001879 if (!pnp)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001880 wbsd_chip_poweroff(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001881
Pierre Ossman85bcc132005-05-08 19:35:27 +01001882 wbsd_release_resources(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001883
Pierre Ossman85bcc132005-05-08 19:35:27 +01001884 wbsd_free_mmc(dev);
1885}
1886
1887/*
1888 * Non-PnP
1889 */
1890
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001891static int __devinit wbsd_probe(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001892{
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001893 /* Use the module parameters for resources */
Russell King3ae5eae2005-11-09 22:32:44 +00001894 return wbsd_init(&dev->dev, io, irq, dma, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001895}
1896
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001897static int __devexit wbsd_remove(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001898{
Russell King3ae5eae2005-11-09 22:32:44 +00001899 wbsd_shutdown(&dev->dev, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001900
1901 return 0;
1902}
1903
1904/*
1905 * PnP
1906 */
1907
1908#ifdef CONFIG_PNP
1909
1910static int __devinit
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001911wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001912{
1913 int io, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001914
Pierre Ossman85bcc132005-05-08 19:35:27 +01001915 /*
1916 * Get resources from PnP layer.
1917 */
1918 io = pnp_port_start(pnpdev, 0);
1919 irq = pnp_irq(pnpdev, 0);
1920 if (pnp_dma_valid(pnpdev, 0))
1921 dma = pnp_dma(pnpdev, 0);
1922 else
1923 dma = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001924
Pierre Ossman85bcc132005-05-08 19:35:27 +01001925 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001926
Pierre Ossman85bcc132005-05-08 19:35:27 +01001927 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1928}
1929
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001930static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001931{
1932 wbsd_shutdown(&dev->dev, 1);
1933}
1934
1935#endif /* CONFIG_PNP */
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/*
1938 * Power management
1939 */
1940
1941#ifdef CONFIG_PM
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001942
Pierre Ossman5e68d952006-01-08 14:21:52 +00001943static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1944{
1945 BUG_ON(host == NULL);
1946
1947 return mmc_suspend_host(host->mmc, state);
1948}
1949
1950static int wbsd_resume(struct wbsd_host *host)
1951{
1952 BUG_ON(host == NULL);
1953
1954 wbsd_init_device(host);
1955
1956 return mmc_resume_host(host->mmc);
1957}
1958
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001959static int wbsd_platform_suspend(struct platform_device *dev,
1960 pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Russell King3ae5eae2005-11-09 22:32:44 +00001962 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001963 struct wbsd_host *host;
1964 int ret;
1965
Pierre Ossman5e68d952006-01-08 14:21:52 +00001966 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001967 return 0;
1968
Pierre Ossman5e68d952006-01-08 14:21:52 +00001969 DBGF("Suspending...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001970
1971 host = mmc_priv(mmc);
1972
Pierre Ossman5e68d952006-01-08 14:21:52 +00001973 ret = wbsd_suspend(host, state);
1974 if (ret)
1975 return ret;
1976
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001977 wbsd_chip_poweroff(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 return 0;
1980}
1981
Pierre Ossman5e68d952006-01-08 14:21:52 +00001982static int wbsd_platform_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Russell King3ae5eae2005-11-09 22:32:44 +00001984 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001985 struct wbsd_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Pierre Ossman5e68d952006-01-08 14:21:52 +00001987 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001988 return 0;
1989
Pierre Ossman5e68d952006-01-08 14:21:52 +00001990 DBGF("Resuming...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001991
1992 host = mmc_priv(mmc);
1993
1994 wbsd_chip_config(host);
1995
1996 /*
1997 * Allow device to initialise itself properly.
1998 */
1999 mdelay(5);
2000
Pierre Ossman5e68d952006-01-08 14:21:52 +00002001 return wbsd_resume(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002003
Pierre Ossman5e68d952006-01-08 14:21:52 +00002004#ifdef CONFIG_PNP
2005
2006static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2007{
2008 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2009 struct wbsd_host *host;
2010
2011 if (mmc == NULL)
2012 return 0;
2013
2014 DBGF("Suspending...\n");
2015
2016 host = mmc_priv(mmc);
2017
2018 return wbsd_suspend(host, state);
2019}
2020
2021static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2022{
2023 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2024 struct wbsd_host *host;
2025
2026 if (mmc == NULL)
2027 return 0;
2028
2029 DBGF("Resuming...\n");
2030
2031 host = mmc_priv(mmc);
2032
2033 /*
2034 * See if chip needs to be configured.
2035 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002036 if (host->config != 0) {
2037 if (!wbsd_chip_validate(host)) {
Pierre Ossman5e68d952006-01-08 14:21:52 +00002038 printk(KERN_WARNING DRIVER_NAME
2039 ": PnP active but chip not configured! "
2040 "You probably have a buggy BIOS. "
2041 "Configuring chip manually.\n");
2042 wbsd_chip_config(host);
2043 }
2044 }
2045
2046 /*
2047 * Allow device to initialise itself properly.
2048 */
2049 mdelay(5);
2050
2051 return wbsd_resume(host);
2052}
2053
2054#endif /* CONFIG_PNP */
2055
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002056#else /* CONFIG_PM */
2057
Pierre Ossman5e68d952006-01-08 14:21:52 +00002058#define wbsd_platform_suspend NULL
2059#define wbsd_platform_resume NULL
2060
2061#define wbsd_pnp_suspend NULL
2062#define wbsd_pnp_resume NULL
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002063
2064#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Pierre Ossman85bcc132005-05-08 19:35:27 +01002066static struct platform_device *wbsd_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Russell King3ae5eae2005-11-09 22:32:44 +00002068static struct platform_driver wbsd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 .probe = wbsd_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002070 .remove = __devexit_p(wbsd_remove),
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002071
Pierre Ossman5e68d952006-01-08 14:21:52 +00002072 .suspend = wbsd_platform_suspend,
2073 .resume = wbsd_platform_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002074 .driver = {
2075 .name = DRIVER_NAME,
2076 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077};
2078
Pierre Ossman85bcc132005-05-08 19:35:27 +01002079#ifdef CONFIG_PNP
2080
2081static struct pnp_driver wbsd_pnp_driver = {
2082 .name = DRIVER_NAME,
2083 .id_table = pnp_dev_table,
2084 .probe = wbsd_pnp_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002085 .remove = __devexit_p(wbsd_pnp_remove),
Pierre Ossman5e68d952006-01-08 14:21:52 +00002086
2087 .suspend = wbsd_pnp_suspend,
2088 .resume = wbsd_pnp_resume,
Pierre Ossman85bcc132005-05-08 19:35:27 +01002089};
2090
2091#endif /* CONFIG_PNP */
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
2094 * Module loading/unloading
2095 */
2096
2097static int __init wbsd_drv_init(void)
2098{
2099 int result;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 printk(KERN_INFO DRIVER_NAME
Pierre Ossman1615cc22007-02-09 08:19:07 +01002102 ": Winbond W83L51xD SD/MMC card interface driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Pierre Ossman85bcc132005-05-08 19:35:27 +01002105#ifdef CONFIG_PNP
2106
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002107 if (!nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002108 result = pnp_register_driver(&wbsd_pnp_driver);
2109 if (result < 0)
2110 return result;
2111 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002112#endif /* CONFIG_PNP */
2113
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002114 if (nopnp) {
Russell King3ae5eae2005-11-09 22:32:44 +00002115 result = platform_driver_register(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002116 if (result < 0)
2117 return result;
2118
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002119 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002120 if (!wbsd_device) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002121 platform_driver_unregister(&wbsd_driver);
2122 return -ENOMEM;
2123 }
2124
2125 result = platform_device_add(wbsd_device);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002126 if (result) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002127 platform_device_put(wbsd_device);
2128 platform_driver_unregister(&wbsd_driver);
2129 return result;
2130 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01002131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 return 0;
2134}
2135
2136static void __exit wbsd_drv_exit(void)
2137{
Pierre Ossman85bcc132005-05-08 19:35:27 +01002138#ifdef CONFIG_PNP
2139
2140 if (!nopnp)
2141 pnp_unregister_driver(&wbsd_pnp_driver);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002142
2143#endif /* CONFIG_PNP */
Pierre Ossman85bcc132005-05-08 19:35:27 +01002144
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002145 if (nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002146 platform_device_unregister(wbsd_device);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002147
Russell King3ae5eae2005-11-09 22:32:44 +00002148 platform_driver_unregister(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 DBG("unloaded\n");
2152}
2153
2154module_init(wbsd_drv_init);
2155module_exit(wbsd_drv_exit);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002156#ifdef CONFIG_PNP
2157module_param(nopnp, uint, 0444);
2158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159module_param(io, uint, 0444);
2160module_param(irq, uint, 0444);
2161module_param(dma, int, 0444);
2162
2163MODULE_LICENSE("GPL");
Pierre Ossmande1d09e2005-11-09 23:21:49 +00002164MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Pierre Ossman85bcc132005-05-08 19:35:27 +01002167#ifdef CONFIG_PNP
2168MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2171MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2172MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");