blob: 2d323af2002563492b0b31140a79584b48d0d30c [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);
346 kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
347 dmabuf += sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (size < sg[i].length)
350 size = 0;
351 else
352 size -= sg[i].length;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (size == 0)
355 break;
356 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * Check that we didn't get a request to transfer
360 * more data than can fit into the SG list.
361 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 BUG_ON(size != 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 host->size -= size;
366}
367
368/*
369 * Command handling
370 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100371
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000372static inline void wbsd_get_short_reply(struct wbsd_host *host,
373 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 /*
376 * Correct response type?
377 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000378 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cmd->error = MMC_ERR_INVALID;
380 return;
381 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100382
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000383 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
384 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
385 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
386 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
387 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000390static inline void wbsd_get_long_reply(struct wbsd_host *host,
391 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * Correct response type?
397 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000398 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 cmd->error = MMC_ERR_INVALID;
400 return;
401 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100402
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000403 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cmd->resp[i] =
405 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
406 cmd->resp[i] |=
407 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
408 cmd->resp[i] |=
409 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
410 cmd->resp[i] |=
411 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
412 }
413}
414
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000415static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int i;
418 u8 status, isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 DBGF("Sending cmd (%x)\n", cmd->opcode);
421
422 /*
423 * Clear accumulated ISR. The interrupt routine
424 * will fill this one with events that occur during
425 * transfer.
426 */
427 host->isr = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Send the command (CRC calculated by host).
431 */
432 outb(cmd->opcode, host->base + WBSD_CMDR);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000433 for (i = 3; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 cmd->error = MMC_ERR_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /*
439 * Wait for the request to complete.
440 */
441 do {
442 status = wbsd_read_index(host, WBSD_IDX_STATUS);
443 } while (status & WBSD_CARDTRAFFIC);
444
445 /*
446 * Do we expect a reply?
447 */
Russell Kinge9225172006-02-02 12:23:12 +0000448 if (cmd->flags & MMC_RSP_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Read back status.
451 */
452 isr = host->isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Card removed? */
455 if (isr & WBSD_INT_CARD)
456 cmd->error = MMC_ERR_TIMEOUT;
457 /* Timeout? */
458 else if (isr & WBSD_INT_TIMEOUT)
459 cmd->error = MMC_ERR_TIMEOUT;
460 /* CRC? */
461 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
462 cmd->error = MMC_ERR_BADCRC;
463 /* All ok */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000464 else {
Russell Kinge9225172006-02-02 12:23:12 +0000465 if (cmd->flags & MMC_RSP_136)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 wbsd_get_long_reply(host, cmd);
Russell Kinge9225172006-02-02 12:23:12 +0000467 else
468 wbsd_get_short_reply(host, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 }
471
472 DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
473}
474
475/*
476 * Data functions
477 */
478
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000479static void wbsd_empty_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000481 struct mmc_data *data = host->mrq->cmd->data;
482 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
486 * Handle excessive data.
487 */
488 if (data->bytes_xfered == host->size)
489 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100490
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100491 buffer = wbsd_sg_to_buffer(host) + host->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Drain the fifo. This has a tendency to loop longer
495 * than the FIFO length (usually one block).
496 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000497 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /*
499 * The size field in the FSR is broken so we have to
500 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (fsr & WBSD_FIFO_FULL)
503 fifo = 16;
504 else if (fsr & WBSD_FIFO_FUTHRE)
505 fifo = 8;
506 else
507 fifo = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100508
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000509 for (i = 0; i < fifo; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *buffer = inb(host->base + WBSD_DFR);
511 buffer++;
512 host->offset++;
513 host->remain--;
514
515 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * Transfer done?
519 */
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100520 if (data->bytes_xfered == host->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
524 * End of scatter list entry?
525 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000526 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * Get next entry. Check if last.
529 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000530 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * We should never reach this point.
533 * It means that we're trying to
534 * transfer more blocks than can fit
535 * into the scatter list.
536 */
537 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return;
542 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100543
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100544 buffer = wbsd_sg_to_buffer(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * This is a very dirty hack to solve a
551 * hardware problem. The chip doesn't trigger
552 * FIFO threshold interrupts properly.
553 */
554 if ((host->size - data->bytes_xfered) < 16)
555 tasklet_schedule(&host->fifo_tasklet);
556}
557
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000558static void wbsd_fill_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000560 struct mmc_data *data = host->mrq->cmd->data;
561 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int i, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Check that we aren't being called after the
566 * entire buffer has been transfered.
567 */
568 if (data->bytes_xfered == host->size)
569 return;
570
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100571 buffer = wbsd_sg_to_buffer(host) + host->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
574 * Fill the fifo. This has a tendency to loop longer
575 * than the FIFO length (usually one block).
576 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000577 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
579 * The size field in the FSR is broken so we have to
580 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (fsr & WBSD_FIFO_EMPTY)
583 fifo = 0;
584 else if (fsr & WBSD_FIFO_EMTHRE)
585 fifo = 8;
586 else
587 fifo = 15;
588
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000589 for (i = 16; i > fifo; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 outb(*buffer, host->base + WBSD_DFR);
591 buffer++;
592 host->offset++;
593 host->remain--;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * Transfer done?
599 */
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100600 if (data->bytes_xfered == host->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /*
604 * End of scatter list entry?
605 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000606 if (host->remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /*
608 * Get next entry. Check if last.
609 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000610 if (!wbsd_next_sg(host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * We should never reach this point.
613 * It means that we're trying to
614 * transfer more blocks than can fit
615 * into the scatter list.
616 */
617 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 host->size = data->bytes_xfered;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return;
622 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100623
Pierre Ossman4a0ddbd2007-01-31 18:20:48 +0100624 buffer = wbsd_sg_to_buffer(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 }
627 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100628
Pierre Ossman85bcc132005-05-08 19:35:27 +0100629 /*
630 * The controller stops sending interrupts for
631 * 'FIFO empty' under certain conditions. So we
632 * need to be a bit more pro-active.
633 */
634 tasklet_schedule(&host->fifo_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000637static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 u16 blksize;
640 u8 setup;
641 unsigned long dmaflags;
642
643 DBGF("blksz %04x blks %04x flags %08x\n",
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100644 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 DBGF("tsac %d ms nsac %d clk\n",
646 data->timeout_ns / 1000000, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
649 * Calculate size.
650 */
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100651 host->size = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /*
654 * Check timeout values for overflow.
655 * (Yes, some cards cause this value to overflow).
656 */
657 if (data->timeout_ns > 127000000)
658 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000659 else {
660 wbsd_write_index(host, WBSD_IDX_TAAC,
661 data->timeout_ns / 1000000);
662 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (data->timeout_clks > 255)
665 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
666 else
667 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Inform the chip of how large blocks will be
671 * sent. It needs this to determine when to
672 * calculate CRC.
673 *
674 * Space for CRC must be included in the size.
Pierre Ossman65ae2112005-09-06 15:18:57 -0700675 * Two bytes are needed for each data line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000677 if (host->bus_width == MMC_BUS_WIDTH_1) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100678 blksize = data->blksz + 2;
Pierre Ossman65ae2112005-09-06 15:18:57 -0700679
680 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
681 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000682 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100683 blksize = data->blksz + 2 * 4;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100684
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000685 wbsd_write_index(host, WBSD_IDX_PBSMSB,
686 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700687 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000688 } else {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700689 data->error = MMC_ERR_INVALID;
690 return;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /*
694 * Clear the FIFO. This is needed even for DMA
695 * transfers since the chip still uses the FIFO
696 * internally.
697 */
698 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
699 setup |= WBSD_FIFO_RESET;
700 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * DMA transfer?
704 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000705 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
707 * The buffer for DMA is only 64 kB.
708 */
709 BUG_ON(host->size > 0x10000);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000710 if (host->size > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 data->error = MMC_ERR_INVALID;
712 return;
713 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * Transfer data from the SG list to
717 * the DMA buffer.
718 */
719 if (data->flags & MMC_DATA_WRITE)
720 wbsd_sg_to_dma(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /*
723 * Initialise the ISA DMA controller.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 dmaflags = claim_dma_lock();
726 disable_dma(host->dma);
727 clear_dma_ff(host->dma);
728 if (data->flags & MMC_DATA_READ)
729 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
730 else
731 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
732 set_dma_addr(host->dma, host->dma_addr);
733 set_dma_count(host->dma, host->size);
734
735 enable_dma(host->dma);
736 release_dma_lock(dmaflags);
737
738 /*
739 * Enable DMA on the host.
740 */
741 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000742 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /*
744 * This flag is used to keep printk
745 * output to a minimum.
746 */
747 host->firsterr = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Initialise the SG list.
751 */
752 wbsd_init_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /*
755 * Turn off DMA.
756 */
757 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
760 * Set up FIFO threshold levels (and fill
761 * buffer if doing a write).
762 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000763 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 wbsd_write_index(host, WBSD_IDX_FIFOEN,
765 WBSD_FIFOEN_FULL | 8);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000766 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 wbsd_write_index(host, WBSD_IDX_FIFOEN,
768 WBSD_FIFOEN_EMPTY | 8);
769 wbsd_fill_fifo(host);
770 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100771 }
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 data->error = MMC_ERR_NONE;
774}
775
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000776static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 unsigned long dmaflags;
779 int count;
780 u8 status;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 WARN_ON(host->mrq == NULL);
783
784 /*
785 * Send a stop command if needed.
786 */
787 if (data->stop)
788 wbsd_send_command(host, data->stop);
789
790 /*
791 * Wait for the controller to leave data
792 * transfer state.
793 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000794 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 status = wbsd_read_index(host, WBSD_IDX_STATUS);
796 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /*
799 * DMA transfer?
800 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000801 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /*
803 * Disable DMA on the host.
804 */
805 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
808 * Turn of ISA DMA controller.
809 */
810 dmaflags = claim_dma_lock();
811 disable_dma(host->dma);
812 clear_dma_ff(host->dma);
813 count = get_dma_residue(host->dma);
814 release_dma_lock(dmaflags);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Any leftover data?
818 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000819 if (count) {
Pierre Ossmand1916342005-11-05 10:36:35 +0000820 printk(KERN_ERR "%s: Incomplete DMA transfer. "
821 "%d bytes left.\n",
822 mmc_hostname(host->mmc), count);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 data->error = MMC_ERR_FAILED;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Transfer data from DMA buffer to
828 * SG list.
829 */
830 if (data->flags & MMC_DATA_READ)
831 wbsd_dma_to_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 data->bytes_xfered = host->size;
834 }
835 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 wbsd_request_end(host, host->mrq);
840}
841
Pierre Ossman85bcc132005-05-08 19:35:27 +0100842/*****************************************************************************\
843 * *
844 * MMC layer callbacks *
845 * *
846\*****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000848static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000850 struct wbsd_host *host = mmc_priv(mmc);
851 struct mmc_command *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /*
854 * Disable tasklets to avoid a deadlock.
855 */
856 spin_lock_bh(&host->lock);
857
858 BUG_ON(host->mrq != NULL);
859
860 cmd = mrq->cmd;
861
862 host->mrq = mrq;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * If there is no card in the slot then
866 * timeout immediatly.
867 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000868 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 cmd->error = MMC_ERR_TIMEOUT;
870 goto done;
871 }
872
873 /*
874 * Does the request include data?
875 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000876 if (cmd->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 wbsd_prepare_data(host, cmd->data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (cmd->data->error != MMC_ERR_NONE)
880 goto done;
881 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 wbsd_send_command(host, cmd);
884
885 /*
886 * If this is a data transfer the request
887 * will be finished after the data has
888 * transfered.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100889 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000890 if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100892 * The hardware is so delightfully stupid that it has a list
893 * of "data" commands. If a command isn't on this list, it'll
894 * just go back to the idle state and won't send any data
895 * interrupts.
896 */
897 switch (cmd->opcode) {
898 case 11:
899 case 17:
900 case 18:
901 case 20:
902 case 24:
903 case 25:
904 case 26:
905 case 27:
906 case 30:
907 case 42:
908 case 56:
909 break;
910
911 /* ACMDs. We don't keep track of state, so we just treat them
912 * like any other command. */
913 case 51:
914 break;
915
916 default:
917#ifdef CONFIG_MMC_DEBUG
918 printk(KERN_WARNING "%s: Data command %d is not "
919 "supported by this controller.\n",
920 mmc_hostname(host->mmc), cmd->opcode);
921#endif
922 cmd->data->error = MMC_ERR_INVALID;
923
924 if (cmd->data->stop)
925 wbsd_send_command(host, cmd->data->stop);
926
927 goto done;
928 };
929
930 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * Dirty fix for hardware bug.
932 */
933 if (host->dma == -1)
934 tasklet_schedule(&host->fifo_tasklet);
935
936 spin_unlock_bh(&host->lock);
937
938 return;
939 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941done:
942 wbsd_request_end(host, mrq);
943
944 spin_unlock_bh(&host->lock);
945}
946
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000947static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000949 struct wbsd_host *host = mmc_priv(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 u8 clk, setup, pwr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 spin_lock_bh(&host->lock);
953
954 /*
955 * Reset the chip on each power off.
956 * Should clear out any weird states.
957 */
958 if (ios->power_mode == MMC_POWER_OFF)
959 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (ios->clock >= 24000000)
962 clk = WBSD_CLK_24M;
963 else if (ios->clock >= 16000000)
964 clk = WBSD_CLK_16M;
965 else if (ios->clock >= 12000000)
966 clk = WBSD_CLK_12M;
967 else
968 clk = WBSD_CLK_375K;
969
970 /*
971 * Only write to the clock register when
972 * there is an actual change.
973 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000974 if (clk != host->clk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 wbsd_write_index(host, WBSD_IDX_CLK, clk);
976 host->clk = clk;
977 }
978
Pierre Ossman85bcc132005-05-08 19:35:27 +0100979 /*
980 * Power up card.
981 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000982 if (ios->power_mode != MMC_POWER_OFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 pwr = inb(host->base + WBSD_CSR);
984 pwr &= ~WBSD_POWER_N;
985 outb(pwr, host->base + WBSD_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Pierre Ossman85bcc132005-05-08 19:35:27 +0100988 /*
989 * MMC cards need to have pin 1 high during init.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100990 * It wreaks havoc with the card detection though so
Pierre Ossman1656fa52005-09-03 16:45:49 +0100991 * that needs to be disabled.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100992 */
993 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000994 if (ios->chip_select == MMC_CS_HIGH) {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700995 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
Pierre Ossman85bcc132005-05-08 19:35:27 +0100996 setup |= WBSD_DAT3_H;
997 host->flags |= WBSD_FIGNORE_DETECT;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000998 } else {
999 if (setup & WBSD_DAT3_H) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001000 setup &= ~WBSD_DAT3_H;
Pierre Ossman1656fa52005-09-03 16:45:49 +01001001
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001002 /*
1003 * We cannot resume card detection immediatly
1004 * because of capacitance and delays in the chip.
1005 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001006 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001007 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001008 }
1009 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001010
Pierre Ossman65ae2112005-09-06 15:18:57 -07001011 /*
1012 * Store bus width for later. Will be used when
1013 * setting up the data transfer.
1014 */
1015 host->bus_width = ios->bus_width;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock_bh(&host->lock);
1018}
1019
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001020static int wbsd_get_ro(struct mmc_host *mmc)
Pierre Ossman65ae2112005-09-06 15:18:57 -07001021{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001022 struct wbsd_host *host = mmc_priv(mmc);
Pierre Ossman65ae2112005-09-06 15:18:57 -07001023 u8 csr;
1024
1025 spin_lock_bh(&host->lock);
1026
1027 csr = inb(host->base + WBSD_CSR);
1028 csr |= WBSD_MSLED;
1029 outb(csr, host->base + WBSD_CSR);
1030
1031 mdelay(1);
1032
1033 csr = inb(host->base + WBSD_CSR);
1034 csr &= ~WBSD_MSLED;
1035 outb(csr, host->base + WBSD_CSR);
1036
1037 spin_unlock_bh(&host->lock);
1038
1039 return csr & WBSD_WRPT;
1040}
1041
David Brownellab7aefd2006-11-12 17:55:30 -08001042static const struct mmc_host_ops wbsd_ops = {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001043 .request = wbsd_request,
1044 .set_ios = wbsd_set_ios,
Pierre Ossman65ae2112005-09-06 15:18:57 -07001045 .get_ro = wbsd_get_ro,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001046};
1047
1048/*****************************************************************************\
1049 * *
1050 * Interrupt handling *
1051 * *
1052\*****************************************************************************/
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001055 * Helper function to reset detection ignore
1056 */
1057
1058static void wbsd_reset_ignore(unsigned long data)
1059{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001060 struct wbsd_host *host = (struct wbsd_host *)data;
Pierre Ossman1656fa52005-09-03 16:45:49 +01001061
1062 BUG_ON(host == NULL);
1063
1064 DBG("Resetting card detection ignore\n");
1065
1066 spin_lock_bh(&host->lock);
1067
1068 host->flags &= ~WBSD_FIGNORE_DETECT;
1069
1070 /*
1071 * Card status might have changed during the
1072 * blackout.
1073 */
1074 tasklet_schedule(&host->card_tasklet);
1075
1076 spin_unlock_bh(&host->lock);
1077}
1078
1079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * Tasklets
1081 */
1082
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001083static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 WARN_ON(!host->mrq);
1086 if (!host->mrq)
1087 return NULL;
1088
1089 WARN_ON(!host->mrq->cmd);
1090 if (!host->mrq->cmd)
1091 return NULL;
1092
1093 WARN_ON(!host->mrq->cmd->data);
1094 if (!host->mrq->cmd->data)
1095 return NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return host->mrq->cmd->data;
1098}
1099
1100static void wbsd_tasklet_card(unsigned long param)
1101{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001102 struct wbsd_host *host = (struct wbsd_host *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 u8 csr;
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001104 int delay = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001107
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001108 if (host->flags & WBSD_FIGNORE_DETECT) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001109 spin_unlock(&host->lock);
1110 return;
1111 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 csr = inb(host->base + WBSD_CSR);
1114 WARN_ON(csr == 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001115
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001116 if (csr & WBSD_CARDPRESENT) {
1117 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001118 DBG("Card inserted\n");
1119 host->flags |= WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001120
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001121 delay = 500;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001122 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001123 } else if (host->flags & WBSD_FCARD_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 DBG("Card removed\n");
Pierre Ossman85bcc132005-05-08 19:35:27 +01001125 host->flags &= ~WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001126
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001127 if (host->mrq) {
Pierre Ossmand1916342005-11-05 10:36:35 +00001128 printk(KERN_ERR "%s: Card removed during transfer!\n",
1129 mmc_hostname(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 wbsd_reset(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 host->mrq->cmd->error = MMC_ERR_FAILED;
1133 tasklet_schedule(&host->finish_tasklet);
1134 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001135
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001136 delay = 0;
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001137 }
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001138
1139 /*
1140 * Unlock first since we might get a call back.
1141 */
1142
1143 spin_unlock(&host->lock);
1144
1145 if (delay != -1)
1146 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static void wbsd_tasklet_fifo(unsigned long param)
1150{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001151 struct wbsd_host *host = (struct wbsd_host *)param;
1152 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!host->mrq)
1157 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 data = wbsd_get_data(host);
1160 if (!data)
1161 goto end;
1162
1163 if (data->flags & MMC_DATA_WRITE)
1164 wbsd_fill_fifo(host);
1165 else
1166 wbsd_empty_fifo(host);
1167
1168 /*
1169 * Done?
1170 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001171 if (host->size == data->bytes_xfered) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1173 tasklet_schedule(&host->finish_tasklet);
1174 }
1175
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001176end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 spin_unlock(&host->lock);
1178}
1179
1180static void wbsd_tasklet_crc(unsigned long param)
1181{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001182 struct wbsd_host *host = (struct wbsd_host *)param;
1183 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!host->mrq)
1188 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 data = wbsd_get_data(host);
1191 if (!data)
1192 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 DBGF("CRC error\n");
1195
1196 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 tasklet_schedule(&host->finish_tasklet);
1199
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001200end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 spin_unlock(&host->lock);
1202}
1203
1204static void wbsd_tasklet_timeout(unsigned long param)
1205{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001206 struct wbsd_host *host = (struct wbsd_host *)param;
1207 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!host->mrq)
1212 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 data = wbsd_get_data(host);
1215 if (!data)
1216 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 DBGF("Timeout\n");
1219
1220 data->error = MMC_ERR_TIMEOUT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 tasklet_schedule(&host->finish_tasklet);
1223
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001224end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 spin_unlock(&host->lock);
1226}
1227
1228static void wbsd_tasklet_finish(unsigned long param)
1229{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001230 struct wbsd_host *host = (struct wbsd_host *)param;
1231 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 WARN_ON(!host->mrq);
1236 if (!host->mrq)
1237 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 data = wbsd_get_data(host);
1240 if (!data)
1241 goto end;
1242
1243 wbsd_finish_data(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001244
1245end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 spin_unlock(&host->lock);
1247}
1248
1249static void wbsd_tasklet_block(unsigned long param)
1250{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001251 struct wbsd_host *host = (struct wbsd_host *)param;
1252 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 spin_lock(&host->lock);
1255
1256 if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001257 WBSD_CRC_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 data = wbsd_get_data(host);
1259 if (!data)
1260 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 DBGF("CRC error\n");
1263
1264 data->error = MMC_ERR_BADCRC;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 tasklet_schedule(&host->finish_tasklet);
1267 }
1268
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001269end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_unlock(&host->lock);
1271}
1272
1273/*
1274 * Interrupt handling
1275 */
1276
David Howells7d12e782006-10-05 14:55:46 +01001277static irqreturn_t wbsd_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001279 struct wbsd_host *host = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 isr = inb(host->base + WBSD_ISR);
1283
1284 /*
1285 * Was it actually our hardware that caused the interrupt?
1286 */
1287 if (isr == 0xff || isr == 0x00)
1288 return IRQ_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 host->isr |= isr;
1291
1292 /*
1293 * Schedule tasklets as needed.
1294 */
1295 if (isr & WBSD_INT_CARD)
1296 tasklet_schedule(&host->card_tasklet);
1297 if (isr & WBSD_INT_FIFO_THRE)
1298 tasklet_schedule(&host->fifo_tasklet);
1299 if (isr & WBSD_INT_CRC)
1300 tasklet_hi_schedule(&host->crc_tasklet);
1301 if (isr & WBSD_INT_TIMEOUT)
1302 tasklet_hi_schedule(&host->timeout_tasklet);
1303 if (isr & WBSD_INT_BUSYEND)
1304 tasklet_hi_schedule(&host->block_tasklet);
1305 if (isr & WBSD_INT_TC)
1306 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return IRQ_HANDLED;
1309}
1310
Pierre Ossman85bcc132005-05-08 19:35:27 +01001311/*****************************************************************************\
1312 * *
1313 * Device initialisation and shutdown *
1314 * *
1315\*****************************************************************************/
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001318 * Allocate/free MMC structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
1320
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001321static int __devinit wbsd_alloc_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001322{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001323 struct mmc_host *mmc;
1324 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001325
Pierre Ossman85bcc132005-05-08 19:35:27 +01001326 /*
1327 * Allocate MMC structure.
1328 */
1329 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1330 if (!mmc)
1331 return -ENOMEM;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001332
Pierre Ossman85bcc132005-05-08 19:35:27 +01001333 host = mmc_priv(mmc);
1334 host->mmc = mmc;
1335
1336 host->dma = -1;
1337
1338 /*
1339 * Set host parameters.
1340 */
1341 mmc->ops = &wbsd_ops;
1342 mmc->f_min = 375000;
1343 mmc->f_max = 24000000;
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001344 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Russell King42431ac2006-09-24 10:44:09 +01001345 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001346
Pierre Ossman85bcc132005-05-08 19:35:27 +01001347 spin_lock_init(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001348
Pierre Ossman85bcc132005-05-08 19:35:27 +01001349 /*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001350 * Set up timers
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001351 */
Pierre Ossman1656fa52005-09-03 16:45:49 +01001352 init_timer(&host->ignore_timer);
1353 host->ignore_timer.data = (unsigned long)host;
1354 host->ignore_timer.function = wbsd_reset_ignore;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001355
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001356 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001357 * Maximum number of segments. Worst case is one sector per segment
1358 * so this will be 64kB/512.
1359 */
1360 mmc->max_hw_segs = 128;
1361 mmc->max_phys_segs = 128;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001362
Pierre Ossman85bcc132005-05-08 19:35:27 +01001363 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001364 * Maximum request size. Also limited by 64KiB buffer.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001365 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001366 mmc->max_req_size = 65536;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001367
Pierre Ossman85bcc132005-05-08 19:35:27 +01001368 /*
1369 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001370 * of bytes.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001371 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001372 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001373
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001374 /*
1375 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1376 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1377 */
1378 mmc->max_blk_size = 4087;
1379
Pierre Ossman55db8902006-11-21 17:55:45 +01001380 /*
1381 * Maximum block count. There is no real limit so the maximum
1382 * request size will be the only restriction.
1383 */
1384 mmc->max_blk_count = mmc->max_req_size;
1385
Pierre Ossman85bcc132005-05-08 19:35:27 +01001386 dev_set_drvdata(dev, mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001387
Pierre Ossman85bcc132005-05-08 19:35:27 +01001388 return 0;
1389}
1390
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001391static void __devexit wbsd_free_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001392{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001393 struct mmc_host *mmc;
1394 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001395
Pierre Ossman85bcc132005-05-08 19:35:27 +01001396 mmc = dev_get_drvdata(dev);
1397 if (!mmc)
1398 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001399
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001400 host = mmc_priv(mmc);
1401 BUG_ON(host == NULL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001402
Pierre Ossman1656fa52005-09-03 16:45:49 +01001403 del_timer_sync(&host->ignore_timer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001404
Pierre Ossman85bcc132005-05-08 19:35:27 +01001405 mmc_free_host(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001406
Pierre Ossman85bcc132005-05-08 19:35:27 +01001407 dev_set_drvdata(dev, NULL);
1408}
1409
1410/*
1411 * Scan for known chip id:s
1412 */
1413
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001414static int __devinit wbsd_scan(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 int i, j, k;
1417 int id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
1420 * Iterate through all ports, all codes to
1421 * find hardware that is in our known list.
1422 */
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001423 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1425 continue;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001426
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001427 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 id = 0xFFFF;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001429
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001430 host->config = config_ports[i];
1431 host->unlock_code = unlock_codes[j];
1432
1433 wbsd_unlock_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 outb(WBSD_CONF_ID_HI, config_ports[i]);
1436 id = inb(config_ports[i] + 1) << 8;
1437
1438 outb(WBSD_CONF_ID_LO, config_ports[i]);
1439 id |= inb(config_ports[i] + 1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001440
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001441 wbsd_lock_config(host);
1442
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001443 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001444 if (id == valid_ids[k]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 host->chip_id = id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448 }
1449 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001450
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001451 if (id != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 DBG("Unknown hardware (id %x) found at %x\n",
1453 id, config_ports[i]);
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 release_region(config_ports[i], 2);
1458 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001459
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001460 host->config = 0;
1461 host->unlock_code = 0;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -ENODEV;
1464}
1465
Pierre Ossman85bcc132005-05-08 19:35:27 +01001466/*
1467 * Allocate/free io port ranges
1468 */
1469
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001470static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001472 if (base & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EINVAL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001474
Pierre Ossman85bcc132005-05-08 19:35:27 +01001475 if (!request_region(base, 8, DRIVER_NAME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EIO;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001477
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001478 host->base = base;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001483static void __devexit wbsd_release_regions(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 if (host->base)
1486 release_region(host->base, 8);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001487
Pierre Ossman85bcc132005-05-08 19:35:27 +01001488 host->base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (host->config)
1491 release_region(host->config, 2);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001492
Pierre Ossman85bcc132005-05-08 19:35:27 +01001493 host->config = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Pierre Ossman85bcc132005-05-08 19:35:27 +01001496/*
1497 * Allocate/free DMA port and buffer
1498 */
1499
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001500static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (dma < 0)
1503 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (request_dma(dma, DRIVER_NAME))
1506 goto err;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * We need to allocate a special buffer in
1510 * order for ISA to be able to DMA to it.
1511 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001512 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
1514 if (!host->dma_buffer)
1515 goto free;
1516
1517 /*
1518 * Translate the address to a physical address.
1519 */
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001520 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001521 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * ISA DMA must be aligned on a 64k basis.
1525 */
1526 if ((host->dma_addr & 0xffff) != 0)
1527 goto kfree;
1528 /*
1529 * ISA cannot access memory above 16 MB.
1530 */
1531 else if (host->dma_addr >= 0x1000000)
1532 goto kfree;
1533
1534 host->dma = dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538kfree:
1539 /*
1540 * If we've gotten here then there is some kind of alignment bug
1541 */
1542 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001543
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001544 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001545 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001546 host->dma_addr = (dma_addr_t)NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 kfree(host->dma_buffer);
1549 host->dma_buffer = NULL;
1550
1551free:
1552 free_dma(dma);
1553
1554err:
1555 printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
1556 "Falling back on FIFO.\n", dma);
1557}
1558
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001559static void __devexit wbsd_release_dma(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001560{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001561 if (host->dma_addr) {
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001562 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001563 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1564 }
Jesper Juhl6044ec82005-11-07 01:01:32 -08001565 kfree(host->dma_buffer);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001566 if (host->dma >= 0)
1567 free_dma(host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001568
Pierre Ossman85bcc132005-05-08 19:35:27 +01001569 host->dma = -1;
1570 host->dma_buffer = NULL;
1571 host->dma_addr = (dma_addr_t)NULL;
1572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001575 * Allocate/free IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
1577
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001578static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 * Allocate interrupt.
1584 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001585
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001586 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (ret)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001588 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 host->irq = irq;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * Set up tasklets.
1594 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001595 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1596 (unsigned long)host);
1597 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1598 (unsigned long)host);
1599 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1600 (unsigned long)host);
1601 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1602 (unsigned long)host);
1603 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1604 (unsigned long)host);
1605 tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
1606 (unsigned long)host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001611static void __devexit wbsd_release_irq(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Pierre Ossman85bcc132005-05-08 19:35:27 +01001613 if (!host->irq)
1614 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 free_irq(host->irq, host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001617
Pierre Ossman85bcc132005-05-08 19:35:27 +01001618 host->irq = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 tasklet_kill(&host->card_tasklet);
1621 tasklet_kill(&host->fifo_tasklet);
1622 tasklet_kill(&host->crc_tasklet);
1623 tasklet_kill(&host->timeout_tasklet);
1624 tasklet_kill(&host->finish_tasklet);
1625 tasklet_kill(&host->block_tasklet);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001626}
1627
1628/*
1629 * Allocate all resources for the host.
1630 */
1631
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001632static int __devinit wbsd_request_resources(struct wbsd_host *host,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001633 int base, int irq, int dma)
1634{
1635 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001636
Pierre Ossman85bcc132005-05-08 19:35:27 +01001637 /*
1638 * Allocate I/O ports.
1639 */
1640 ret = wbsd_request_region(host, base);
1641 if (ret)
1642 return ret;
1643
1644 /*
1645 * Allocate interrupt.
1646 */
1647 ret = wbsd_request_irq(host, irq);
1648 if (ret)
1649 return ret;
1650
1651 /*
1652 * Allocate DMA.
1653 */
1654 wbsd_request_dma(host, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001655
Pierre Ossman85bcc132005-05-08 19:35:27 +01001656 return 0;
1657}
1658
1659/*
1660 * Release all resources for the host.
1661 */
1662
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001663static void __devexit wbsd_release_resources(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001664{
1665 wbsd_release_dma(host);
1666 wbsd_release_irq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 wbsd_release_regions(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001668}
1669
1670/*
1671 * Configure the resources the chip should use.
1672 */
1673
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001674static void wbsd_chip_config(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001675{
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001676 wbsd_unlock_config(host);
1677
Pierre Ossman85bcc132005-05-08 19:35:27 +01001678 /*
1679 * Reset the chip.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001680 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001681 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1682 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1683
1684 /*
1685 * Select SD/MMC function.
1686 */
1687 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001688
Pierre Ossman85bcc132005-05-08 19:35:27 +01001689 /*
1690 * Set up card detection.
1691 */
1692 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001693
Pierre Ossman85bcc132005-05-08 19:35:27 +01001694 /*
1695 * Configure chip
1696 */
1697 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1698 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001699
Pierre Ossman85bcc132005-05-08 19:35:27 +01001700 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001701
Pierre Ossman85bcc132005-05-08 19:35:27 +01001702 if (host->dma >= 0)
1703 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001704
Pierre Ossman85bcc132005-05-08 19:35:27 +01001705 /*
1706 * Enable and power up chip.
1707 */
1708 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1709 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001710
1711 wbsd_lock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001712}
1713
1714/*
1715 * Check that configured resources are correct.
1716 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001717
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001718static int wbsd_chip_validate(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001719{
1720 int base, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001721
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001722 wbsd_unlock_config(host);
1723
Pierre Ossman85bcc132005-05-08 19:35:27 +01001724 /*
1725 * Select SD/MMC function.
1726 */
1727 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001728
Pierre Ossman85bcc132005-05-08 19:35:27 +01001729 /*
1730 * Read configuration.
1731 */
1732 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1733 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001734
Pierre Ossman85bcc132005-05-08 19:35:27 +01001735 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001736
Pierre Ossman85bcc132005-05-08 19:35:27 +01001737 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001738
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001739 wbsd_lock_config(host);
1740
Pierre Ossman85bcc132005-05-08 19:35:27 +01001741 /*
1742 * Validate against given configuration.
1743 */
1744 if (base != host->base)
1745 return 0;
1746 if (irq != host->irq)
1747 return 0;
1748 if ((dma != host->dma) && (host->dma != -1))
1749 return 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001750
Pierre Ossman85bcc132005-05-08 19:35:27 +01001751 return 1;
1752}
1753
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001754/*
1755 * Powers down the SD function
1756 */
1757
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001758static void wbsd_chip_poweroff(struct wbsd_host *host)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001759{
1760 wbsd_unlock_config(host);
1761
1762 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1763 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1764
1765 wbsd_lock_config(host);
1766}
1767
Pierre Ossman85bcc132005-05-08 19:35:27 +01001768/*****************************************************************************\
1769 * *
1770 * Devices setup and shutdown *
1771 * *
1772\*****************************************************************************/
1773
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001774static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001775 int pnp)
1776{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001777 struct wbsd_host *host = NULL;
1778 struct mmc_host *mmc = NULL;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001779 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001780
Pierre Ossman85bcc132005-05-08 19:35:27 +01001781 ret = wbsd_alloc_mmc(dev);
1782 if (ret)
1783 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001784
Pierre Ossman85bcc132005-05-08 19:35:27 +01001785 mmc = dev_get_drvdata(dev);
1786 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001787
Pierre Ossman85bcc132005-05-08 19:35:27 +01001788 /*
1789 * Scan for hardware.
1790 */
1791 ret = wbsd_scan(host);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001792 if (ret) {
1793 if (pnp && (ret == -ENODEV)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001794 printk(KERN_WARNING DRIVER_NAME
1795 ": Unable to confirm device presence. You may "
1796 "experience lock-ups.\n");
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001797 } else {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001798 wbsd_free_mmc(dev);
1799 return ret;
1800 }
1801 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001802
Pierre Ossman85bcc132005-05-08 19:35:27 +01001803 /*
1804 * Request resources.
1805 */
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001806 ret = wbsd_request_resources(host, base, irq, dma);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001807 if (ret) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001808 wbsd_release_resources(host);
1809 wbsd_free_mmc(dev);
1810 return ret;
1811 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001812
Pierre Ossman85bcc132005-05-08 19:35:27 +01001813 /*
1814 * See if chip needs to be configured.
1815 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001816 if (pnp) {
1817 if ((host->config != 0) && !wbsd_chip_validate(host)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001818 printk(KERN_WARNING DRIVER_NAME
1819 ": PnP active but chip not configured! "
1820 "You probably have a buggy BIOS. "
1821 "Configuring chip manually.\n");
1822 wbsd_chip_config(host);
1823 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001824 } else
Pierre Ossman85bcc132005-05-08 19:35:27 +01001825 wbsd_chip_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001826
Pierre Ossman85bcc132005-05-08 19:35:27 +01001827 /*
1828 * Power Management stuff. No idea how this works.
1829 * Not tested.
1830 */
1831#ifdef CONFIG_PM
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001832 if (host->config) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001833 wbsd_unlock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001834 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001835 wbsd_lock_config(host);
1836 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001837#endif
1838 /*
1839 * Allow device to initialise itself properly.
1840 */
1841 mdelay(5);
1842
1843 /*
1844 * Reset the chip into a known state.
1845 */
1846 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001847
Pierre Ossman85bcc132005-05-08 19:35:27 +01001848 mmc_add_host(mmc);
1849
Russell Kingd366b642005-08-19 09:40:08 +01001850 printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
Pierre Ossman85bcc132005-05-08 19:35:27 +01001851 if (host->chip_id != 0)
1852 printk(" id %x", (int)host->chip_id);
1853 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1854 if (host->dma >= 0)
1855 printk(" dma %d", (int)host->dma);
1856 else
1857 printk(" FIFO");
1858 if (pnp)
1859 printk(" PnP");
1860 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 return 0;
1863}
1864
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001865static void __devexit wbsd_shutdown(struct device *dev, int pnp)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001866{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001867 struct mmc_host *mmc = dev_get_drvdata(dev);
1868 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001869
Pierre Ossman85bcc132005-05-08 19:35:27 +01001870 if (!mmc)
1871 return;
1872
1873 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001874
Pierre Ossman85bcc132005-05-08 19:35:27 +01001875 mmc_remove_host(mmc);
1876
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001877 /*
1878 * Power down the SD/MMC function.
1879 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001880 if (!pnp)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001881 wbsd_chip_poweroff(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001882
Pierre Ossman85bcc132005-05-08 19:35:27 +01001883 wbsd_release_resources(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001884
Pierre Ossman85bcc132005-05-08 19:35:27 +01001885 wbsd_free_mmc(dev);
1886}
1887
1888/*
1889 * Non-PnP
1890 */
1891
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001892static int __devinit wbsd_probe(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001893{
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001894 /* Use the module parameters for resources */
Russell King3ae5eae2005-11-09 22:32:44 +00001895 return wbsd_init(&dev->dev, io, irq, dma, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001896}
1897
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001898static int __devexit wbsd_remove(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001899{
Russell King3ae5eae2005-11-09 22:32:44 +00001900 wbsd_shutdown(&dev->dev, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001901
1902 return 0;
1903}
1904
1905/*
1906 * PnP
1907 */
1908
1909#ifdef CONFIG_PNP
1910
1911static int __devinit
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001912wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001913{
1914 int io, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001915
Pierre Ossman85bcc132005-05-08 19:35:27 +01001916 /*
1917 * Get resources from PnP layer.
1918 */
1919 io = pnp_port_start(pnpdev, 0);
1920 irq = pnp_irq(pnpdev, 0);
1921 if (pnp_dma_valid(pnpdev, 0))
1922 dma = pnp_dma(pnpdev, 0);
1923 else
1924 dma = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001925
Pierre Ossman85bcc132005-05-08 19:35:27 +01001926 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001927
Pierre Ossman85bcc132005-05-08 19:35:27 +01001928 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1929}
1930
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001931static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001932{
1933 wbsd_shutdown(&dev->dev, 1);
1934}
1935
1936#endif /* CONFIG_PNP */
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/*
1939 * Power management
1940 */
1941
1942#ifdef CONFIG_PM
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001943
Pierre Ossman5e68d952006-01-08 14:21:52 +00001944static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
1945{
1946 BUG_ON(host == NULL);
1947
1948 return mmc_suspend_host(host->mmc, state);
1949}
1950
1951static int wbsd_resume(struct wbsd_host *host)
1952{
1953 BUG_ON(host == NULL);
1954
1955 wbsd_init_device(host);
1956
1957 return mmc_resume_host(host->mmc);
1958}
1959
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001960static int wbsd_platform_suspend(struct platform_device *dev,
1961 pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Russell King3ae5eae2005-11-09 22:32:44 +00001963 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001964 struct wbsd_host *host;
1965 int ret;
1966
Pierre Ossman5e68d952006-01-08 14:21:52 +00001967 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001968 return 0;
1969
Pierre Ossman5e68d952006-01-08 14:21:52 +00001970 DBGF("Suspending...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001971
1972 host = mmc_priv(mmc);
1973
Pierre Ossman5e68d952006-01-08 14:21:52 +00001974 ret = wbsd_suspend(host, state);
1975 if (ret)
1976 return ret;
1977
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001978 wbsd_chip_poweroff(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 return 0;
1981}
1982
Pierre Ossman5e68d952006-01-08 14:21:52 +00001983static int wbsd_platform_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Russell King3ae5eae2005-11-09 22:32:44 +00001985 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001986 struct wbsd_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Pierre Ossman5e68d952006-01-08 14:21:52 +00001988 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001989 return 0;
1990
Pierre Ossman5e68d952006-01-08 14:21:52 +00001991 DBGF("Resuming...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001992
1993 host = mmc_priv(mmc);
1994
1995 wbsd_chip_config(host);
1996
1997 /*
1998 * Allow device to initialise itself properly.
1999 */
2000 mdelay(5);
2001
Pierre Ossman5e68d952006-01-08 14:21:52 +00002002 return wbsd_resume(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002004
Pierre Ossman5e68d952006-01-08 14:21:52 +00002005#ifdef CONFIG_PNP
2006
2007static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
2008{
2009 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2010 struct wbsd_host *host;
2011
2012 if (mmc == NULL)
2013 return 0;
2014
2015 DBGF("Suspending...\n");
2016
2017 host = mmc_priv(mmc);
2018
2019 return wbsd_suspend(host, state);
2020}
2021
2022static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
2023{
2024 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
2025 struct wbsd_host *host;
2026
2027 if (mmc == NULL)
2028 return 0;
2029
2030 DBGF("Resuming...\n");
2031
2032 host = mmc_priv(mmc);
2033
2034 /*
2035 * See if chip needs to be configured.
2036 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002037 if (host->config != 0) {
2038 if (!wbsd_chip_validate(host)) {
Pierre Ossman5e68d952006-01-08 14:21:52 +00002039 printk(KERN_WARNING DRIVER_NAME
2040 ": PnP active but chip not configured! "
2041 "You probably have a buggy BIOS. "
2042 "Configuring chip manually.\n");
2043 wbsd_chip_config(host);
2044 }
2045 }
2046
2047 /*
2048 * Allow device to initialise itself properly.
2049 */
2050 mdelay(5);
2051
2052 return wbsd_resume(host);
2053}
2054
2055#endif /* CONFIG_PNP */
2056
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002057#else /* CONFIG_PM */
2058
Pierre Ossman5e68d952006-01-08 14:21:52 +00002059#define wbsd_platform_suspend NULL
2060#define wbsd_platform_resume NULL
2061
2062#define wbsd_pnp_suspend NULL
2063#define wbsd_pnp_resume NULL
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01002064
2065#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Pierre Ossman85bcc132005-05-08 19:35:27 +01002067static struct platform_device *wbsd_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Russell King3ae5eae2005-11-09 22:32:44 +00002069static struct platform_driver wbsd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 .probe = wbsd_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002071 .remove = __devexit_p(wbsd_remove),
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002072
Pierre Ossman5e68d952006-01-08 14:21:52 +00002073 .suspend = wbsd_platform_suspend,
2074 .resume = wbsd_platform_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002075 .driver = {
2076 .name = DRIVER_NAME,
2077 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
2079
Pierre Ossman85bcc132005-05-08 19:35:27 +01002080#ifdef CONFIG_PNP
2081
2082static struct pnp_driver wbsd_pnp_driver = {
2083 .name = DRIVER_NAME,
2084 .id_table = pnp_dev_table,
2085 .probe = wbsd_pnp_probe,
Pierre Ossman93968d72005-11-09 23:21:06 +00002086 .remove = __devexit_p(wbsd_pnp_remove),
Pierre Ossman5e68d952006-01-08 14:21:52 +00002087
2088 .suspend = wbsd_pnp_suspend,
2089 .resume = wbsd_pnp_resume,
Pierre Ossman85bcc132005-05-08 19:35:27 +01002090};
2091
2092#endif /* CONFIG_PNP */
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * Module loading/unloading
2096 */
2097
2098static int __init wbsd_drv_init(void)
2099{
2100 int result;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 printk(KERN_INFO DRIVER_NAME
Pierre Ossman1615cc22007-02-09 08:19:07 +01002103 ": Winbond W83L51xD SD/MMC card interface driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Pierre Ossman85bcc132005-05-08 19:35:27 +01002106#ifdef CONFIG_PNP
2107
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002108 if (!nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002109 result = pnp_register_driver(&wbsd_pnp_driver);
2110 if (result < 0)
2111 return result;
2112 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002113#endif /* CONFIG_PNP */
2114
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002115 if (nopnp) {
Russell King3ae5eae2005-11-09 22:32:44 +00002116 result = platform_driver_register(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002117 if (result < 0)
2118 return result;
2119
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002120 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002121 if (!wbsd_device) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002122 platform_driver_unregister(&wbsd_driver);
2123 return -ENOMEM;
2124 }
2125
2126 result = platform_device_add(wbsd_device);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002127 if (result) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00002128 platform_device_put(wbsd_device);
2129 platform_driver_unregister(&wbsd_driver);
2130 return result;
2131 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01002132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 return 0;
2135}
2136
2137static void __exit wbsd_drv_exit(void)
2138{
Pierre Ossman85bcc132005-05-08 19:35:27 +01002139#ifdef CONFIG_PNP
2140
2141 if (!nopnp)
2142 pnp_unregister_driver(&wbsd_pnp_driver);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002143
2144#endif /* CONFIG_PNP */
Pierre Ossman85bcc132005-05-08 19:35:27 +01002145
Pierre Ossmancfa7f522006-01-08 18:17:55 +00002146 if (nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01002147 platform_device_unregister(wbsd_device);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01002148
Russell King3ae5eae2005-11-09 22:32:44 +00002149 platform_driver_unregister(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 DBG("unloaded\n");
2153}
2154
2155module_init(wbsd_drv_init);
2156module_exit(wbsd_drv_exit);
Pierre Ossman85bcc132005-05-08 19:35:27 +01002157#ifdef CONFIG_PNP
2158module_param(nopnp, uint, 0444);
2159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160module_param(io, uint, 0444);
2161module_param(irq, uint, 0444);
2162module_param(dma, int, 0444);
2163
2164MODULE_LICENSE("GPL");
Pierre Ossmande1d09e2005-11-09 23:21:49 +00002165MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Pierre Ossman85bcc132005-05-08 19:35:27 +01002168#ifdef CONFIG_PNP
2169MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
2170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2172MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2173MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");