blob: 67ecd342fe5f1abe3cd45a61b61d8cd3f130ebbc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Pierre Ossman70f10482007-07-11 20:04:50 +02003 * linux/drivers/mmc/host/wbsd.c - Winbond W83L51xD SD/MMC driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Alex Dubov14d836e2007-04-13 19:04:38 +02005 * Copyright (C) 2004-2007 Pierre Ossman, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Warning!
8 *
9 * Changes to the FIFO system should be done with extreme care since
10 * the hardware is full of bugs related to the FIFO. Known issues are:
11 *
12 * - FIFO size field in FSR is always zero.
13 *
14 * - FIFO interrupts tend not to work as they should. Interrupts are
15 * triggered only for full/empty events, not for threshold values.
16 *
17 * - On APIC systems the FIFO empty interrupt is sometimes lost.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010026#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
Pierre Ossman85bcc132005-05-08 19:35:27 +010028#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/highmem.h>
30#include <linux/mmc/host.h>
Ulf Hansson2871ec92020-04-09 14:54:22 +020031#include <linux/mmc/mmc.h>
32#include <linux/mmc/sd.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020033#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/io.h>
37#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "wbsd.h"
40
41#define DRIVER_NAME "wbsd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define DBG(x...) \
Russell Kingc6563172006-03-29 09:30:20 +010044 pr_debug(DRIVER_NAME ": " x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DBGF(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010046 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
Pierre Ossman85bcc132005-05-08 19:35:27 +010049 * Device resources
50 */
51
52#ifdef CONFIG_PNP
53
54static const struct pnp_device_id pnp_dev_table[] = {
55 { "WEC0517", 0 },
56 { "WEC0518", 0 },
57 { "", 0 },
58};
59
60MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
61
62#endif /* CONFIG_PNP */
63
Adrian Bunk3eee0d02005-07-01 13:07:37 +010064static const int config_ports[] = { 0x2E, 0x4E };
65static const int unlock_codes[] = { 0x83, 0x87 };
66
67static const int valid_ids[] = {
68 0x7112,
Tomas Winkler9eeebd22008-06-30 10:50:23 +030069};
Adrian Bunk3eee0d02005-07-01 13:07:37 +010070
Pierre Ossman85bcc132005-05-08 19:35:27 +010071#ifdef CONFIG_PNP
Tomas Winkler9eeebd22008-06-30 10:50:23 +030072static unsigned int param_nopnp = 0;
Pierre Ossman85bcc132005-05-08 19:35:27 +010073#else
Tomas Winkler9eeebd22008-06-30 10:50:23 +030074static const unsigned int param_nopnp = 1;
Pierre Ossman85bcc132005-05-08 19:35:27 +010075#endif
Tomas Winkler9eeebd22008-06-30 10:50:23 +030076static unsigned int param_io = 0x248;
77static unsigned int param_irq = 6;
78static int param_dma = 2;
Pierre Ossman85bcc132005-05-08 19:35:27 +010079
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Basic functions
82 */
83
Pierre Ossmancfa7f522006-01-08 18:17:55 +000084static inline void wbsd_unlock_config(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Pierre Ossman85bcc132005-05-08 19:35:27 +010086 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 outb(host->unlock_code, host->config);
89 outb(host->unlock_code, host->config);
90}
91
Pierre Ossmancfa7f522006-01-08 18:17:55 +000092static inline void wbsd_lock_config(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Pierre Ossman85bcc132005-05-08 19:35:27 +010094 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 outb(LOCK_CODE, host->config);
97}
98
Pierre Ossmancfa7f522006-01-08 18:17:55 +000099static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Pierre Ossman85bcc132005-05-08 19:35:27 +0100101 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 outb(reg, host->config);
104 outb(value, host->config + 1);
105}
106
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000107static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Pierre Ossman85bcc132005-05-08 19:35:27 +0100109 BUG_ON(host->config == 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 outb(reg, host->config);
112 return inb(host->config + 1);
113}
114
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000115static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 outb(index, host->base + WBSD_IDXR);
118 outb(value, host->base + WBSD_DATAR);
119}
120
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000121static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 outb(index, host->base + WBSD_IDXR);
124 return inb(host->base + WBSD_DATAR);
125}
126
127/*
128 * Common routines
129 */
130
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000131static void wbsd_init_device(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 u8 setup, ier;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
136 * Reset chip (SD/MMC part) and fifo.
137 */
138 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
139 setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
140 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +0100143 * Set DAT3 to input
144 */
145 setup &= ~WBSD_DAT3_H;
146 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
147 host->flags &= ~WBSD_FIGNORE_DETECT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100148
Pierre Ossman85bcc132005-05-08 19:35:27 +0100149 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * Read back default clock.
151 */
152 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
153
154 /*
155 * Power down port.
156 */
157 outb(WBSD_POWER_N, host->base + WBSD_CSR);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * Set maximum timeout.
161 */
162 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +0100165 * Test for card presence
166 */
167 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
168 host->flags |= WBSD_FCARD_PRESENT;
169 else
170 host->flags &= ~WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100171
Pierre Ossman85bcc132005-05-08 19:35:27 +0100172 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Enable interesting interrupts.
174 */
175 ier = 0;
176 ier |= WBSD_EINT_CARD;
177 ier |= WBSD_EINT_FIFO_THRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ier |= WBSD_EINT_CRC;
Pierre Ossman5721dbf2007-04-13 23:25:59 +0200179 ier |= WBSD_EINT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ier |= WBSD_EINT_TC;
181
182 outb(ier, host->base + WBSD_EIR);
183
184 /*
185 * Clear interrupts.
186 */
187 inb(host->base + WBSD_ISR);
188}
189
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000190static void wbsd_reset(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 u8 setup;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100193
Girish K Sa3c76eb2011-10-11 11:44:09 +0530194 pr_err("%s: Resetting chip\n", mmc_hostname(host->mmc));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Soft reset of chip (SD/MMC part).
198 */
199 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
200 setup |= WBSD_SOFT_RESET;
201 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
202}
203
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000204static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 unsigned long dmaflags;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100207
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000208 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /*
210 * Release ISA DMA controller.
211 */
212 dmaflags = claim_dma_lock();
213 disable_dma(host->dma);
214 clear_dma_ff(host->dma);
215 release_dma_lock(dmaflags);
216
217 /*
218 * Disable DMA on host.
219 */
220 wbsd_write_index(host, WBSD_IDX_DMA, 0);
221 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 host->mrq = NULL;
224
225 /*
226 * MMC layer might call back into the driver so first unlock.
227 */
228 spin_unlock(&host->lock);
229 mmc_request_done(host->mmc, mrq);
230 spin_lock(&host->lock);
231}
232
233/*
234 * Scatter/gather functions
235 */
236
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000237static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 /*
240 * Get info. about SG list from data structure.
241 */
242 host->cur_sg = data->sg;
243 host->num_sg = data->sg_len;
244
245 host->offset = 0;
246 host->remain = host->cur_sg->length;
247}
248
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000249static inline int wbsd_next_sg(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 /*
252 * Skip to next SG entry.
253 */
254 host->cur_sg++;
255 host->num_sg--;
256
257 /*
258 * Any entries left?
259 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000260 if (host->num_sg > 0) {
261 host->offset = 0;
262 host->remain = host->cur_sg->length;
263 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return host->num_sg;
266}
267
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200268static inline char *wbsd_map_sg(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200270 return kmap_atomic(sg_page(host->cur_sg)) + host->cur_sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000273static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200275 size_t len = 0;
276 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100277
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200278 for (i = 0; i < data->sg_len; i++)
279 len += data->sg[i].length;
280 sg_copy_to_buffer(data->sg, data->sg_len, host->dma_buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000283static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200285 size_t len = 0;
286 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100287
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200288 for (i = 0; i < data->sg_len; i++)
289 len += data->sg[i].length;
290 sg_copy_from_buffer(data->sg, data->sg_len, host->dma_buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * Command handling
295 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100296
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000297static inline void wbsd_get_short_reply(struct wbsd_host *host,
298 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 /*
301 * Correct response type?
302 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000303 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200304 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return;
306 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100307
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000308 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
309 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
310 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
311 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
312 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000315static inline void wbsd_get_long_reply(struct wbsd_host *host,
316 struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 int i;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Correct response type?
322 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000323 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200324 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return;
326 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100327
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000328 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 cmd->resp[i] =
330 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
331 cmd->resp[i] |=
332 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
333 cmd->resp[i] |=
334 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
335 cmd->resp[i] |=
336 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
337 }
338}
339
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000340static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 int i;
343 u8 status, isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
346 * Clear accumulated ISR. The interrupt routine
347 * will fill this one with events that occur during
348 * transfer.
349 */
350 host->isr = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
353 * Send the command (CRC calculated by host).
354 */
355 outb(cmd->opcode, host->base + WBSD_CMDR);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000356 for (i = 3; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100358
Pierre Ossman17b04292007-07-22 22:18:46 +0200359 cmd->error = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /*
362 * Wait for the request to complete.
363 */
364 do {
365 status = wbsd_read_index(host, WBSD_IDX_STATUS);
366 } while (status & WBSD_CARDTRAFFIC);
367
368 /*
369 * Do we expect a reply?
370 */
Russell Kinge9225172006-02-02 12:23:12 +0000371 if (cmd->flags & MMC_RSP_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Read back status.
374 */
375 isr = host->isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Card removed? */
378 if (isr & WBSD_INT_CARD)
Pierre Ossman17b04292007-07-22 22:18:46 +0200379 cmd->error = -ENOMEDIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Timeout? */
381 else if (isr & WBSD_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200382 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* CRC? */
384 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200385 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* All ok */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000387 else {
Russell Kinge9225172006-02-02 12:23:12 +0000388 if (cmd->flags & MMC_RSP_136)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 wbsd_get_long_reply(host, cmd);
Russell Kinge9225172006-02-02 12:23:12 +0000390 else
391 wbsd_get_short_reply(host, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * Data functions
398 */
399
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000400static void wbsd_empty_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000402 struct mmc_data *data = host->mrq->cmd->data;
403 char *buffer;
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200404 int i, idx, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Handle excessive data.
408 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200409 if (host->num_sg == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100411
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200412 buffer = wbsd_map_sg(host) + host->offset;
413 idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
416 * Drain the fifo. This has a tendency to loop longer
417 * than the FIFO length (usually one block).
418 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000419 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * The size field in the FSR is broken so we have to
422 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (fsr & WBSD_FIFO_FULL)
425 fifo = 16;
426 else if (fsr & WBSD_FIFO_FUTHRE)
427 fifo = 8;
428 else
429 fifo = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100430
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000431 for (i = 0; i < fifo; i++) {
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200432 buffer[idx++] = inb(host->base + WBSD_DFR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 host->offset++;
434 host->remain--;
435
436 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * End of scatter list entry?
440 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000441 if (host->remain == 0) {
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200442 kunmap_atomic(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Get next entry. Check if last.
445 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200446 if (!wbsd_next_sg(host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100448
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200449 buffer = wbsd_map_sg(host);
450 idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453 }
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200454 kunmap_atomic(buffer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * This is a very dirty hack to solve a
458 * hardware problem. The chip doesn't trigger
459 * FIFO threshold interrupts properly.
460 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200461 if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 tasklet_schedule(&host->fifo_tasklet);
463}
464
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000465static void wbsd_fill_fifo(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000467 struct mmc_data *data = host->mrq->cmd->data;
468 char *buffer;
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200469 int i, idx, fsr, fifo;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Check that we aren't being called after the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300473 * entire buffer has been transferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200475 if (host->num_sg == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return;
477
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200478 buffer = wbsd_map_sg(host) + host->offset;
479 idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /*
482 * Fill the fifo. This has a tendency to loop longer
483 * than the FIFO length (usually one block).
484 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000485 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * The size field in the FSR is broken so we have to
488 * do some guessing.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (fsr & WBSD_FIFO_EMPTY)
491 fifo = 0;
492 else if (fsr & WBSD_FIFO_EMTHRE)
493 fifo = 8;
494 else
495 fifo = 15;
496
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000497 for (i = 16; i > fifo; i--) {
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200498 outb(buffer[idx], host->base + WBSD_DFR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 host->offset++;
500 host->remain--;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 data->bytes_xfered++;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * End of scatter list entry?
506 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000507 if (host->remain == 0) {
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200508 kunmap_atomic(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
510 * Get next entry. Check if last.
511 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200512 if (!wbsd_next_sg(host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100514
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200515 buffer = wbsd_map_sg(host);
516 idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
519 }
Christoph Hellwig9181ece2018-05-18 19:18:46 +0200520 kunmap_atomic(buffer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100521
Pierre Ossman85bcc132005-05-08 19:35:27 +0100522 /*
523 * The controller stops sending interrupts for
524 * 'FIFO empty' under certain conditions. So we
525 * need to be a bit more pro-active.
526 */
527 tasklet_schedule(&host->fifo_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000530static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 u16 blksize;
533 u8 setup;
534 unsigned long dmaflags;
Alex Dubov14d836e2007-04-13 19:04:38 +0200535 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Calculate size.
539 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200540 size = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * Check timeout values for overflow.
544 * (Yes, some cards cause this value to overflow).
545 */
546 if (data->timeout_ns > 127000000)
547 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000548 else {
549 wbsd_write_index(host, WBSD_IDX_TAAC,
550 data->timeout_ns / 1000000);
551 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (data->timeout_clks > 255)
554 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
555 else
556 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Inform the chip of how large blocks will be
560 * sent. It needs this to determine when to
561 * calculate CRC.
562 *
563 * Space for CRC must be included in the size.
Pierre Ossman65ae2112005-09-06 15:18:57 -0700564 * Two bytes are needed for each data line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000566 if (host->bus_width == MMC_BUS_WIDTH_1) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100567 blksize = data->blksz + 2;
Pierre Ossman65ae2112005-09-06 15:18:57 -0700568
569 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
570 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000571 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100572 blksize = data->blksz + 2 * 4;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100573
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000574 wbsd_write_index(host, WBSD_IDX_PBSMSB,
575 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700576 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000577 } else {
Pierre Ossman17b04292007-07-22 22:18:46 +0200578 data->error = -EINVAL;
Pierre Ossman65ae2112005-09-06 15:18:57 -0700579 return;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /*
583 * Clear the FIFO. This is needed even for DMA
584 * transfers since the chip still uses the FIFO
585 * internally.
586 */
587 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
588 setup |= WBSD_FIFO_RESET;
589 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * DMA transfer?
593 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000594 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
596 * The buffer for DMA is only 64 kB.
597 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200598 BUG_ON(size > 0x10000);
599 if (size > 0x10000) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200600 data->error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return;
602 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Transfer data from the SG list to
606 * the DMA buffer.
607 */
608 if (data->flags & MMC_DATA_WRITE)
609 wbsd_sg_to_dma(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Initialise the ISA DMA controller.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 dmaflags = claim_dma_lock();
615 disable_dma(host->dma);
616 clear_dma_ff(host->dma);
617 if (data->flags & MMC_DATA_READ)
618 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
619 else
620 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
621 set_dma_addr(host->dma, host->dma_addr);
Alex Dubov14d836e2007-04-13 19:04:38 +0200622 set_dma_count(host->dma, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 enable_dma(host->dma);
625 release_dma_lock(dmaflags);
626
627 /*
628 * Enable DMA on the host.
629 */
630 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * This flag is used to keep printk
634 * output to a minimum.
635 */
636 host->firsterr = 1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
639 * Initialise the SG list.
640 */
641 wbsd_init_sg(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * Turn off DMA.
645 */
646 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
649 * Set up FIFO threshold levels (and fill
650 * buffer if doing a write).
651 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000652 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 wbsd_write_index(host, WBSD_IDX_FIFOEN,
654 WBSD_FIFOEN_FULL | 8);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000655 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 wbsd_write_index(host, WBSD_IDX_FIFOEN,
657 WBSD_FIFOEN_EMPTY | 8);
658 wbsd_fill_fifo(host);
659 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100660 }
661
Pierre Ossman17b04292007-07-22 22:18:46 +0200662 data->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000665static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 unsigned long dmaflags;
668 int count;
669 u8 status;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 WARN_ON(host->mrq == NULL);
672
673 /*
674 * Send a stop command if needed.
675 */
676 if (data->stop)
677 wbsd_send_command(host, data->stop);
678
679 /*
680 * Wait for the controller to leave data
681 * transfer state.
682 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000683 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 status = wbsd_read_index(host, WBSD_IDX_STATUS);
685 } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /*
688 * DMA transfer?
689 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000690 if (host->dma >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Disable DMA on the host.
693 */
694 wbsd_write_index(host, WBSD_IDX_DMA, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
697 * Turn of ISA DMA controller.
698 */
699 dmaflags = claim_dma_lock();
700 disable_dma(host->dma);
701 clear_dma_ff(host->dma);
702 count = get_dma_residue(host->dma);
703 release_dma_lock(dmaflags);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100704
Alex Dubov14d836e2007-04-13 19:04:38 +0200705 data->bytes_xfered = host->mrq->data->blocks *
706 host->mrq->data->blksz - count;
707 data->bytes_xfered -= data->bytes_xfered % data->blksz;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /*
710 * Any leftover data?
711 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000712 if (count) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530713 pr_err("%s: Incomplete DMA transfer. "
Pierre Ossmand1916342005-11-05 10:36:35 +0000714 "%d bytes left.\n",
715 mmc_hostname(host->mmc), count);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100716
Pierre Ossman17b04292007-07-22 22:18:46 +0200717 if (!data->error)
718 data->error = -EIO;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000719 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Transfer data from DMA buffer to
722 * SG list.
723 */
724 if (data->flags & MMC_DATA_READ)
725 wbsd_dma_to_sg(host, data);
Alex Dubov14d836e2007-04-13 19:04:38 +0200726 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100727
Pierre Ossman17b04292007-07-22 22:18:46 +0200728 if (data->error) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200729 if (data->bytes_xfered)
730 data->bytes_xfered -= data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 wbsd_request_end(host, host->mrq);
735}
736
Pierre Ossman85bcc132005-05-08 19:35:27 +0100737/*****************************************************************************\
738 * *
739 * MMC layer callbacks *
740 * *
741\*****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000743static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000745 struct wbsd_host *host = mmc_priv(mmc);
746 struct mmc_command *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * Disable tasklets to avoid a deadlock.
750 */
751 spin_lock_bh(&host->lock);
752
753 BUG_ON(host->mrq != NULL);
754
755 cmd = mrq->cmd;
756
757 host->mrq = mrq;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
Pierre Ossman17b04292007-07-22 22:18:46 +0200760 * Check that there is actually a card in the slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000762 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200763 cmd->error = -ENOMEDIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto done;
765 }
766
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000767 if (cmd->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100769 * The hardware is so delightfully stupid that it has a list
770 * of "data" commands. If a command isn't on this list, it'll
771 * just go back to the idle state and won't send any data
772 * interrupts.
773 */
774 switch (cmd->opcode) {
Ulf Hansson2871ec92020-04-09 14:54:22 +0200775 case SD_SWITCH_VOLTAGE:
776 case MMC_READ_SINGLE_BLOCK:
777 case MMC_READ_MULTIPLE_BLOCK:
778 case MMC_WRITE_DAT_UNTIL_STOP:
779 case MMC_WRITE_BLOCK:
780 case MMC_WRITE_MULTIPLE_BLOCK:
781 case MMC_PROGRAM_CID:
782 case MMC_PROGRAM_CSD:
783 case MMC_SEND_WRITE_PROT:
784 case MMC_LOCK_UNLOCK:
785 case MMC_GEN_CMD:
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100786 break;
787
788 /* ACMDs. We don't keep track of state, so we just treat them
789 * like any other command. */
Ulf Hansson2871ec92020-04-09 14:54:22 +0200790 case SD_APP_SEND_SCR:
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100791 break;
792
793 default:
Joe Perches66061102014-09-12 14:56:56 -0700794 pr_warn("%s: Data command %d is not supported by this controller\n",
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100795 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman17b04292007-07-22 22:18:46 +0200796 cmd->error = -EINVAL;
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100797
798 goto done;
Javier Martinez Canillas17a90532015-09-16 11:30:03 +0200799 }
Pierre Ossmanb2670b12007-05-01 13:35:19 +0200800 }
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100801
Pierre Ossmanb2670b12007-05-01 13:35:19 +0200802 /*
803 * Does the request include data?
804 */
805 if (cmd->data) {
806 wbsd_prepare_data(host, cmd->data);
807
Pierre Ossman17b04292007-07-22 22:18:46 +0200808 if (cmd->data->error)
Pierre Ossmanb2670b12007-05-01 13:35:19 +0200809 goto done;
810 }
811
812 wbsd_send_command(host, cmd);
813
814 /*
815 * If this is a data transfer the request
816 * will be finished after the data has
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300817 * transferred.
Pierre Ossmanb2670b12007-05-01 13:35:19 +0200818 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200819 if (cmd->data && !cmd->error) {
Pierre Ossman5ba593a2006-11-21 17:45:37 +0100820 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * Dirty fix for hardware bug.
822 */
823 if (host->dma == -1)
824 tasklet_schedule(&host->fifo_tasklet);
825
826 spin_unlock_bh(&host->lock);
827
828 return;
829 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831done:
832 wbsd_request_end(host, mrq);
833
834 spin_unlock_bh(&host->lock);
835}
836
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000837static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000839 struct wbsd_host *host = mmc_priv(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 u8 clk, setup, pwr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 spin_lock_bh(&host->lock);
843
844 /*
845 * Reset the chip on each power off.
846 * Should clear out any weird states.
847 */
848 if (ios->power_mode == MMC_POWER_OFF)
849 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ios->clock >= 24000000)
852 clk = WBSD_CLK_24M;
853 else if (ios->clock >= 16000000)
854 clk = WBSD_CLK_16M;
855 else if (ios->clock >= 12000000)
856 clk = WBSD_CLK_12M;
857 else
858 clk = WBSD_CLK_375K;
859
860 /*
861 * Only write to the clock register when
862 * there is an actual change.
863 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000864 if (clk != host->clk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 wbsd_write_index(host, WBSD_IDX_CLK, clk);
866 host->clk = clk;
867 }
868
Pierre Ossman85bcc132005-05-08 19:35:27 +0100869 /*
870 * Power up card.
871 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000872 if (ios->power_mode != MMC_POWER_OFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 pwr = inb(host->base + WBSD_CSR);
874 pwr &= ~WBSD_POWER_N;
875 outb(pwr, host->base + WBSD_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Pierre Ossman85bcc132005-05-08 19:35:27 +0100878 /*
879 * MMC cards need to have pin 1 high during init.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100880 * It wreaks havoc with the card detection though so
Pierre Ossman1656fa52005-09-03 16:45:49 +0100881 * that needs to be disabled.
Pierre Ossman85bcc132005-05-08 19:35:27 +0100882 */
883 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000884 if (ios->chip_select == MMC_CS_HIGH) {
Pierre Ossman65ae2112005-09-06 15:18:57 -0700885 BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
Pierre Ossman85bcc132005-05-08 19:35:27 +0100886 setup |= WBSD_DAT3_H;
887 host->flags |= WBSD_FIGNORE_DETECT;
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000888 } else {
889 if (setup & WBSD_DAT3_H) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100890 setup &= ~WBSD_DAT3_H;
Pierre Ossman1656fa52005-09-03 16:45:49 +0100891
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100892 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300893 * We cannot resume card detection immediately
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100894 * because of capacitance and delays in the chip.
895 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000896 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +0100897 }
Pierre Ossman85bcc132005-05-08 19:35:27 +0100898 }
899 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100900
Pierre Ossman65ae2112005-09-06 15:18:57 -0700901 /*
902 * Store bus width for later. Will be used when
903 * setting up the data transfer.
904 */
905 host->bus_width = ios->bus_width;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 spin_unlock_bh(&host->lock);
908}
909
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000910static int wbsd_get_ro(struct mmc_host *mmc)
Pierre Ossman65ae2112005-09-06 15:18:57 -0700911{
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000912 struct wbsd_host *host = mmc_priv(mmc);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700913 u8 csr;
914
915 spin_lock_bh(&host->lock);
916
917 csr = inb(host->base + WBSD_CSR);
918 csr |= WBSD_MSLED;
919 outb(csr, host->base + WBSD_CSR);
920
921 mdelay(1);
922
923 csr = inb(host->base + WBSD_CSR);
924 csr &= ~WBSD_MSLED;
925 outb(csr, host->base + WBSD_CSR);
926
927 spin_unlock_bh(&host->lock);
928
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400929 return !!(csr & WBSD_WRPT);
Pierre Ossman65ae2112005-09-06 15:18:57 -0700930}
931
David Brownellab7aefd2006-11-12 17:55:30 -0800932static const struct mmc_host_ops wbsd_ops = {
Pierre Ossman85bcc132005-05-08 19:35:27 +0100933 .request = wbsd_request,
934 .set_ios = wbsd_set_ios,
Pierre Ossman65ae2112005-09-06 15:18:57 -0700935 .get_ro = wbsd_get_ro,
Pierre Ossman85bcc132005-05-08 19:35:27 +0100936};
937
938/*****************************************************************************\
939 * *
940 * Interrupt handling *
941 * *
942\*****************************************************************************/
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
Pierre Ossman1656fa52005-09-03 16:45:49 +0100945 * Helper function to reset detection ignore
946 */
947
Kees Cook2ee4f622017-10-24 08:03:45 -0700948static void wbsd_reset_ignore(struct timer_list *t)
Pierre Ossman1656fa52005-09-03 16:45:49 +0100949{
Kees Cook2ee4f622017-10-24 08:03:45 -0700950 struct wbsd_host *host = from_timer(host, t, ignore_timer);
Pierre Ossman1656fa52005-09-03 16:45:49 +0100951
952 BUG_ON(host == NULL);
953
954 DBG("Resetting card detection ignore\n");
955
956 spin_lock_bh(&host->lock);
957
958 host->flags &= ~WBSD_FIGNORE_DETECT;
959
960 /*
961 * Card status might have changed during the
962 * blackout.
963 */
964 tasklet_schedule(&host->card_tasklet);
965
966 spin_unlock_bh(&host->lock);
967}
968
969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * Tasklets
971 */
972
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000973static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 WARN_ON(!host->mrq);
976 if (!host->mrq)
977 return NULL;
978
979 WARN_ON(!host->mrq->cmd);
980 if (!host->mrq->cmd)
981 return NULL;
982
983 WARN_ON(!host->mrq->cmd->data);
984 if (!host->mrq->cmd->data)
985 return NULL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return host->mrq->cmd->data;
988}
989
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +0100990static void wbsd_tasklet_card(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +0100992 struct wbsd_host *host = from_tasklet(host, t, card_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 u8 csr;
Pierre Ossman210ce2a2005-09-12 20:36:19 +0100994 int delay = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +0100997
Pierre Ossmancfa7f522006-01-08 18:17:55 +0000998 if (host->flags & WBSD_FIGNORE_DETECT) {
Pierre Ossman85bcc132005-05-08 19:35:27 +0100999 spin_unlock(&host->lock);
1000 return;
1001 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 csr = inb(host->base + WBSD_CSR);
1004 WARN_ON(csr == 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001005
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001006 if (csr & WBSD_CARDPRESENT) {
1007 if (!(host->flags & WBSD_FCARD_PRESENT)) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001008 DBG("Card inserted\n");
1009 host->flags |= WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001010
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001011 delay = 500;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001012 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001013 } else if (host->flags & WBSD_FCARD_PRESENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 DBG("Card removed\n");
Pierre Ossman85bcc132005-05-08 19:35:27 +01001015 host->flags &= ~WBSD_FCARD_PRESENT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001016
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001017 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301018 pr_err("%s: Card removed during transfer!\n",
Pierre Ossmand1916342005-11-05 10:36:35 +00001019 mmc_hostname(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 wbsd_reset(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001021
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 host->mrq->cmd->error = -ENOMEDIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 tasklet_schedule(&host->finish_tasklet);
1024 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001025
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001026 delay = 0;
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001027 }
Pierre Ossman210ce2a2005-09-12 20:36:19 +01001028
1029 /*
1030 * Unlock first since we might get a call back.
1031 */
1032
1033 spin_unlock(&host->lock);
1034
1035 if (delay != -1)
1036 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001039static void wbsd_tasklet_fifo(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001041 struct wbsd_host *host = from_tasklet(host, t, fifo_tasklet);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001042 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!host->mrq)
1047 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 data = wbsd_get_data(host);
1050 if (!data)
1051 goto end;
1052
1053 if (data->flags & MMC_DATA_WRITE)
1054 wbsd_fill_fifo(host);
1055 else
1056 wbsd_empty_fifo(host);
1057
1058 /*
1059 * Done?
1060 */
Alex Dubov14d836e2007-04-13 19:04:38 +02001061 if (host->num_sg == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1063 tasklet_schedule(&host->finish_tasklet);
1064 }
1065
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001066end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_unlock(&host->lock);
1068}
1069
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001070static void wbsd_tasklet_crc(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001072 struct wbsd_host *host = from_tasklet(host, t, crc_tasklet);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001073 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!host->mrq)
1078 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 data = wbsd_get_data(host);
1081 if (!data)
1082 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 DBGF("CRC error\n");
1085
Pierre Ossman17b04292007-07-22 22:18:46 +02001086 data->error = -EILSEQ;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 tasklet_schedule(&host->finish_tasklet);
1089
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001090end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 spin_unlock(&host->lock);
1092}
1093
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001094static void wbsd_tasklet_timeout(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001096 struct wbsd_host *host = from_tasklet(host, t, timeout_tasklet);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001097 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!host->mrq)
1102 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 data = wbsd_get_data(host);
1105 if (!data)
1106 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 DBGF("Timeout\n");
1109
Pierre Ossman17b04292007-07-22 22:18:46 +02001110 data->error = -ETIMEDOUT;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 tasklet_schedule(&host->finish_tasklet);
1113
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001114end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 spin_unlock(&host->lock);
1116}
1117
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001118static void wbsd_tasklet_finish(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001120 struct wbsd_host *host = from_tasklet(host, t, finish_tasklet);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001121 struct mmc_data *data;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 spin_lock(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 WARN_ON(!host->mrq);
1126 if (!host->mrq)
1127 goto end;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 data = wbsd_get_data(host);
1130 if (!data)
1131 goto end;
1132
1133 wbsd_finish_data(host, data);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001134
1135end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 spin_unlock(&host->lock);
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * Interrupt handling
1141 */
1142
David Howells7d12e782006-10-05 14:55:46 +01001143static irqreturn_t wbsd_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001145 struct wbsd_host *host = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int isr;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 isr = inb(host->base + WBSD_ISR);
1149
1150 /*
1151 * Was it actually our hardware that caused the interrupt?
1152 */
1153 if (isr == 0xff || isr == 0x00)
1154 return IRQ_NONE;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 host->isr |= isr;
1157
1158 /*
1159 * Schedule tasklets as needed.
1160 */
1161 if (isr & WBSD_INT_CARD)
1162 tasklet_schedule(&host->card_tasklet);
1163 if (isr & WBSD_INT_FIFO_THRE)
1164 tasklet_schedule(&host->fifo_tasklet);
1165 if (isr & WBSD_INT_CRC)
1166 tasklet_hi_schedule(&host->crc_tasklet);
1167 if (isr & WBSD_INT_TIMEOUT)
1168 tasklet_hi_schedule(&host->timeout_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (isr & WBSD_INT_TC)
1170 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return IRQ_HANDLED;
1173}
1174
Pierre Ossman85bcc132005-05-08 19:35:27 +01001175/*****************************************************************************\
1176 * *
1177 * Device initialisation and shutdown *
1178 * *
1179\*****************************************************************************/
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001182 * Allocate/free MMC structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
1184
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001185static int wbsd_alloc_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001186{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001187 struct mmc_host *mmc;
1188 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001189
Pierre Ossman85bcc132005-05-08 19:35:27 +01001190 /*
1191 * Allocate MMC structure.
1192 */
1193 mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1194 if (!mmc)
1195 return -ENOMEM;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001196
Pierre Ossman85bcc132005-05-08 19:35:27 +01001197 host = mmc_priv(mmc);
1198 host->mmc = mmc;
1199
1200 host->dma = -1;
1201
1202 /*
1203 * Set host parameters.
1204 */
1205 mmc->ops = &wbsd_ops;
1206 mmc->f_min = 375000;
1207 mmc->f_max = 24000000;
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001208 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossman23af6032008-07-06 01:10:27 +02001209 mmc->caps = MMC_CAP_4_BIT_DATA;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001210
Pierre Ossman85bcc132005-05-08 19:35:27 +01001211 spin_lock_init(&host->lock);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001212
Pierre Ossman85bcc132005-05-08 19:35:27 +01001213 /*
Pierre Ossman1656fa52005-09-03 16:45:49 +01001214 * Set up timers
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001215 */
Kees Cook2ee4f622017-10-24 08:03:45 -07001216 timer_setup(&host->ignore_timer, wbsd_reset_ignore, 0);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001217
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001218 /*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001219 * Maximum number of segments. Worst case is one sector per segment
1220 * so this will be 64kB/512.
1221 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001222 mmc->max_segs = 128;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001223
Pierre Ossman85bcc132005-05-08 19:35:27 +01001224 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001225 * Maximum request size. Also limited by 64KiB buffer.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001226 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001227 mmc->max_req_size = 65536;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001228
Pierre Ossman85bcc132005-05-08 19:35:27 +01001229 /*
1230 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001231 * of bytes.
Pierre Ossman85bcc132005-05-08 19:35:27 +01001232 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001233 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001234
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001235 /*
1236 * Maximum block size. We have 12 bits (= 4095) but have to subtract
1237 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1238 */
1239 mmc->max_blk_size = 4087;
1240
Pierre Ossman55db8902006-11-21 17:55:45 +01001241 /*
1242 * Maximum block count. There is no real limit so the maximum
1243 * request size will be the only restriction.
1244 */
1245 mmc->max_blk_count = mmc->max_req_size;
1246
Pierre Ossman85bcc132005-05-08 19:35:27 +01001247 dev_set_drvdata(dev, mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001248
Pierre Ossman85bcc132005-05-08 19:35:27 +01001249 return 0;
1250}
1251
Gabriel Cb3627bb2007-08-02 20:20:44 +02001252static void wbsd_free_mmc(struct device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001253{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001254 struct mmc_host *mmc;
1255 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001256
Pierre Ossman85bcc132005-05-08 19:35:27 +01001257 mmc = dev_get_drvdata(dev);
1258 if (!mmc)
1259 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001260
Pierre Ossman6e6293d2005-07-01 12:13:55 +01001261 host = mmc_priv(mmc);
1262 BUG_ON(host == NULL);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001263
Pierre Ossman1656fa52005-09-03 16:45:49 +01001264 del_timer_sync(&host->ignore_timer);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001265
Pierre Ossman85bcc132005-05-08 19:35:27 +01001266 mmc_free_host(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001267
Pierre Ossman85bcc132005-05-08 19:35:27 +01001268 dev_set_drvdata(dev, NULL);
1269}
1270
1271/*
1272 * Scan for known chip id:s
1273 */
1274
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001275static int wbsd_scan(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int i, j, k;
1278 int id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * Iterate through all ports, all codes to
1282 * find hardware that is in our known list.
1283 */
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001284 for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (!request_region(config_ports[i], 2, DRIVER_NAME))
1286 continue;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001287
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001288 for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 id = 0xFFFF;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001290
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001291 host->config = config_ports[i];
1292 host->unlock_code = unlock_codes[j];
1293
1294 wbsd_unlock_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 outb(WBSD_CONF_ID_HI, config_ports[i]);
1297 id = inb(config_ports[i] + 1) << 8;
1298
1299 outb(WBSD_CONF_ID_LO, config_ports[i]);
1300 id |= inb(config_ports[i] + 1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001301
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001302 wbsd_lock_config(host);
1303
Dmitry Torokhov63648fb2006-01-03 22:56:56 +00001304 for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001305 if (id == valid_ids[k]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 host->chip_id = id;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309 }
1310 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001311
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001312 if (id != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 DBG("Unknown hardware (id %x) found at %x\n",
1314 id, config_ports[i]);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 release_region(config_ports[i], 2);
1319 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001320
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001321 host->config = 0;
1322 host->unlock_code = 0;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -ENODEV;
1325}
1326
Pierre Ossman85bcc132005-05-08 19:35:27 +01001327/*
1328 * Allocate/free io port ranges
1329 */
1330
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001331static int wbsd_request_region(struct wbsd_host *host, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001333 if (base & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EINVAL;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001335
Pierre Ossman85bcc132005-05-08 19:35:27 +01001336 if (!request_region(base, 8, DRIVER_NAME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EIO;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001338
Pierre Ossman916f3ac2006-08-06 22:22:23 +02001339 host->base = base;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342}
1343
Gabriel Cb3627bb2007-08-02 20:20:44 +02001344static void wbsd_release_regions(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 if (host->base)
1347 release_region(host->base, 8);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001348
Pierre Ossman85bcc132005-05-08 19:35:27 +01001349 host->base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 if (host->config)
1352 release_region(host->config, 2);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001353
Pierre Ossman85bcc132005-05-08 19:35:27 +01001354 host->config = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Pierre Ossman85bcc132005-05-08 19:35:27 +01001357/*
1358 * Allocate/free DMA port and buffer
1359 */
1360
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001361static void wbsd_request_dma(struct wbsd_host *host, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (dma < 0)
1364 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (request_dma(dma, DRIVER_NAME))
1367 goto err;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
1370 * We need to allocate a special buffer in
1371 * order for ISA to be able to DMA to it.
1372 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001373 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
Michal Hockodcda9b02017-07-12 14:36:45 -07001374 GFP_NOIO | GFP_DMA | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!host->dma_buffer)
1376 goto free;
1377
1378 /*
1379 * Translate the address to a physical address.
1380 */
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001381 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001382 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Alexey Khoroshilova5488a32016-11-12 00:42:44 +03001383 if (dma_mapping_error(mmc_dev(host->mmc), host->dma_addr))
1384 goto kfree;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * ISA DMA must be aligned on a 64k basis.
1388 */
1389 if ((host->dma_addr & 0xffff) != 0)
Alexey Khoroshilova5488a32016-11-12 00:42:44 +03001390 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * ISA cannot access memory above 16 MB.
1393 */
1394 else if (host->dma_addr >= 0x1000000)
Alexey Khoroshilova5488a32016-11-12 00:42:44 +03001395 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 host->dma = dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001400
Alexey Khoroshilova5488a32016-11-12 00:42:44 +03001401unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * If we've gotten here then there is some kind of alignment bug
1404 */
1405 BUG_ON(1);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001406
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001407 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001408 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Pierre Ossman97067d52008-07-06 00:51:07 +02001409 host->dma_addr = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001410
Alexey Khoroshilova5488a32016-11-12 00:42:44 +03001411kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 kfree(host->dma_buffer);
1413 host->dma_buffer = NULL;
1414
1415free:
1416 free_dma(dma);
1417
1418err:
Joe Perches66061102014-09-12 14:56:56 -07001419 pr_warn(DRIVER_NAME ": Unable to allocate DMA %d - falling back on FIFO\n",
1420 dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Gabriel Cb3627bb2007-08-02 20:20:44 +02001423static void wbsd_release_dma(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001424{
Alexey Khoroshilove81c0222017-01-14 01:52:51 +03001425 /*
1426 * host->dma_addr is valid here iff host->dma_buffer is not NULL.
1427 */
1428 if (host->dma_buffer) {
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +02001429 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001430 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
Alexey Khoroshilove81c0222017-01-14 01:52:51 +03001431 kfree(host->dma_buffer);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001432 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001433 if (host->dma >= 0)
1434 free_dma(host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001435
Pierre Ossman85bcc132005-05-08 19:35:27 +01001436 host->dma = -1;
1437 host->dma_buffer = NULL;
Pierre Ossman97067d52008-07-06 00:51:07 +02001438 host->dma_addr = 0;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441/*
Pierre Ossman85bcc132005-05-08 19:35:27 +01001442 * Allocate/free IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001445static int wbsd_request_irq(struct wbsd_host *host, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /*
Chuck Ebbertcef33402008-06-12 15:21:42 -07001450 * Set up tasklets. Must be done before requesting interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
Emil Renner Berthing0c1a3e82021-02-04 16:18:47 +01001452 tasklet_setup(&host->card_tasklet, wbsd_tasklet_card);
1453 tasklet_setup(&host->fifo_tasklet, wbsd_tasklet_fifo);
1454 tasklet_setup(&host->crc_tasklet, wbsd_tasklet_crc);
1455 tasklet_setup(&host->timeout_tasklet, wbsd_tasklet_timeout);
1456 tasklet_setup(&host->finish_tasklet, wbsd_tasklet_finish);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001457
Chuck Ebbertcef33402008-06-12 15:21:42 -07001458 /*
1459 * Allocate interrupt.
1460 */
1461 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1462 if (ret)
1463 return ret;
1464
1465 host->irq = irq;
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Gabriel Cb3627bb2007-08-02 20:20:44 +02001470static void wbsd_release_irq(struct wbsd_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Pierre Ossman85bcc132005-05-08 19:35:27 +01001472 if (!host->irq)
1473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 free_irq(host->irq, host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001476
Pierre Ossman85bcc132005-05-08 19:35:27 +01001477 host->irq = 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 tasklet_kill(&host->card_tasklet);
1480 tasklet_kill(&host->fifo_tasklet);
1481 tasklet_kill(&host->crc_tasklet);
1482 tasklet_kill(&host->timeout_tasklet);
1483 tasklet_kill(&host->finish_tasklet);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001484}
1485
1486/*
1487 * Allocate all resources for the host.
1488 */
1489
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001490static int wbsd_request_resources(struct wbsd_host *host,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001491 int base, int irq, int dma)
1492{
1493 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001494
Pierre Ossman85bcc132005-05-08 19:35:27 +01001495 /*
1496 * Allocate I/O ports.
1497 */
1498 ret = wbsd_request_region(host, base);
1499 if (ret)
1500 return ret;
1501
1502 /*
1503 * Allocate interrupt.
1504 */
1505 ret = wbsd_request_irq(host, irq);
1506 if (ret)
1507 return ret;
1508
1509 /*
1510 * Allocate DMA.
1511 */
1512 wbsd_request_dma(host, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001513
Pierre Ossman85bcc132005-05-08 19:35:27 +01001514 return 0;
1515}
1516
1517/*
1518 * Release all resources for the host.
1519 */
1520
Gabriel Cb3627bb2007-08-02 20:20:44 +02001521static void wbsd_release_resources(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001522{
1523 wbsd_release_dma(host);
1524 wbsd_release_irq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 wbsd_release_regions(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001526}
1527
1528/*
1529 * Configure the resources the chip should use.
1530 */
1531
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001532static void wbsd_chip_config(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001533{
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001534 wbsd_unlock_config(host);
1535
Pierre Ossman85bcc132005-05-08 19:35:27 +01001536 /*
1537 * Reset the chip.
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001538 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001539 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1540 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1541
1542 /*
1543 * Select SD/MMC function.
1544 */
1545 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001546
Pierre Ossman85bcc132005-05-08 19:35:27 +01001547 /*
1548 * Set up card detection.
1549 */
1550 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001551
Pierre Ossman85bcc132005-05-08 19:35:27 +01001552 /*
1553 * Configure chip
1554 */
1555 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1556 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001557
Pierre Ossman85bcc132005-05-08 19:35:27 +01001558 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001559
Pierre Ossman85bcc132005-05-08 19:35:27 +01001560 if (host->dma >= 0)
1561 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001562
Pierre Ossman85bcc132005-05-08 19:35:27 +01001563 /*
1564 * Enable and power up chip.
1565 */
1566 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1567 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001568
1569 wbsd_lock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001570}
1571
1572/*
1573 * Check that configured resources are correct.
1574 */
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001575
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001576static int wbsd_chip_validate(struct wbsd_host *host)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001577{
1578 int base, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001579
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001580 wbsd_unlock_config(host);
1581
Pierre Ossman85bcc132005-05-08 19:35:27 +01001582 /*
1583 * Select SD/MMC function.
1584 */
1585 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001586
Pierre Ossman85bcc132005-05-08 19:35:27 +01001587 /*
1588 * Read configuration.
1589 */
1590 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1591 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001592
Pierre Ossman85bcc132005-05-08 19:35:27 +01001593 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001594
Pierre Ossman85bcc132005-05-08 19:35:27 +01001595 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001596
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001597 wbsd_lock_config(host);
1598
Pierre Ossman85bcc132005-05-08 19:35:27 +01001599 /*
1600 * Validate against given configuration.
1601 */
1602 if (base != host->base)
1603 return 0;
1604 if (irq != host->irq)
1605 return 0;
1606 if ((dma != host->dma) && (host->dma != -1))
1607 return 0;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001608
Pierre Ossman85bcc132005-05-08 19:35:27 +01001609 return 1;
1610}
1611
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001612/*
1613 * Powers down the SD function
1614 */
1615
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001616static void wbsd_chip_poweroff(struct wbsd_host *host)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001617{
1618 wbsd_unlock_config(host);
1619
1620 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1621 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1622
1623 wbsd_lock_config(host);
1624}
1625
Pierre Ossman85bcc132005-05-08 19:35:27 +01001626/*****************************************************************************\
1627 * *
1628 * Devices setup and shutdown *
1629 * *
1630\*****************************************************************************/
1631
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001632static int wbsd_init(struct device *dev, int base, int irq, int dma,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001633 int pnp)
1634{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001635 struct wbsd_host *host = NULL;
1636 struct mmc_host *mmc = NULL;
Pierre Ossman85bcc132005-05-08 19:35:27 +01001637 int ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001638
Pierre Ossman85bcc132005-05-08 19:35:27 +01001639 ret = wbsd_alloc_mmc(dev);
1640 if (ret)
1641 return ret;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001642
Pierre Ossman85bcc132005-05-08 19:35:27 +01001643 mmc = dev_get_drvdata(dev);
1644 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001645
Pierre Ossman85bcc132005-05-08 19:35:27 +01001646 /*
1647 * Scan for hardware.
1648 */
1649 ret = wbsd_scan(host);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001650 if (ret) {
1651 if (pnp && (ret == -ENODEV)) {
Joe Perches66061102014-09-12 14:56:56 -07001652 pr_warn(DRIVER_NAME ": Unable to confirm device presence - you may experience lock-ups\n");
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001653 } else {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001654 wbsd_free_mmc(dev);
1655 return ret;
1656 }
1657 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001658
Pierre Ossman85bcc132005-05-08 19:35:27 +01001659 /*
1660 * Request resources.
1661 */
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001662 ret = wbsd_request_resources(host, base, irq, dma);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001663 if (ret) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001664 wbsd_release_resources(host);
1665 wbsd_free_mmc(dev);
1666 return ret;
1667 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001668
Pierre Ossman85bcc132005-05-08 19:35:27 +01001669 /*
1670 * See if chip needs to be configured.
1671 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001672 if (pnp) {
1673 if ((host->config != 0) && !wbsd_chip_validate(host)) {
Joe Perches66061102014-09-12 14:56:56 -07001674 pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
Pierre Ossman85bcc132005-05-08 19:35:27 +01001675 wbsd_chip_config(host);
1676 }
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001677 } else
Pierre Ossman85bcc132005-05-08 19:35:27 +01001678 wbsd_chip_config(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001679
Pierre Ossman85bcc132005-05-08 19:35:27 +01001680 /*
1681 * Power Management stuff. No idea how this works.
1682 * Not tested.
1683 */
1684#ifdef CONFIG_PM
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001685 if (host->config) {
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001686 wbsd_unlock_config(host);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001687 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001688 wbsd_lock_config(host);
1689 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001690#endif
1691 /*
1692 * Allow device to initialise itself properly.
1693 */
1694 mdelay(5);
1695
1696 /*
1697 * Reset the chip into a known state.
1698 */
1699 wbsd_init_device(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001700
Pierre Ossman85bcc132005-05-08 19:35:27 +01001701 mmc_add_host(mmc);
1702
Girish K Sa3c76eb2011-10-11 11:44:09 +05301703 pr_info("%s: W83L51xD", mmc_hostname(mmc));
Pierre Ossman85bcc132005-05-08 19:35:27 +01001704 if (host->chip_id != 0)
1705 printk(" id %x", (int)host->chip_id);
1706 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1707 if (host->dma >= 0)
1708 printk(" dma %d", (int)host->dma);
1709 else
1710 printk(" FIFO");
1711 if (pnp)
1712 printk(" PnP");
1713 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 return 0;
1716}
1717
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001718static void wbsd_shutdown(struct device *dev, int pnp)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001719{
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001720 struct mmc_host *mmc = dev_get_drvdata(dev);
1721 struct wbsd_host *host;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001722
Pierre Ossman85bcc132005-05-08 19:35:27 +01001723 if (!mmc)
1724 return;
1725
1726 host = mmc_priv(mmc);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001727
Pierre Ossman85bcc132005-05-08 19:35:27 +01001728 mmc_remove_host(mmc);
1729
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001730 /*
1731 * Power down the SD/MMC function.
1732 */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001733 if (!pnp)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001734 wbsd_chip_poweroff(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001735
Pierre Ossman85bcc132005-05-08 19:35:27 +01001736 wbsd_release_resources(host);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001737
Pierre Ossman85bcc132005-05-08 19:35:27 +01001738 wbsd_free_mmc(dev);
1739}
1740
1741/*
1742 * Non-PnP
1743 */
1744
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001745static int wbsd_probe(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001746{
Pierre Ossmandd2c6092006-08-07 01:40:04 +02001747 /* Use the module parameters for resources */
Tomas Winkler9eeebd22008-06-30 10:50:23 +03001748 return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001749}
1750
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001751static int wbsd_remove(struct platform_device *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001752{
Russell King3ae5eae2005-11-09 22:32:44 +00001753 wbsd_shutdown(&dev->dev, 0);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001754
1755 return 0;
1756}
1757
1758/*
1759 * PnP
1760 */
1761
1762#ifdef CONFIG_PNP
1763
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001764static int
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001765wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001766{
1767 int io, irq, dma;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001768
Pierre Ossman85bcc132005-05-08 19:35:27 +01001769 /*
1770 * Get resources from PnP layer.
1771 */
1772 io = pnp_port_start(pnpdev, 0);
1773 irq = pnp_irq(pnpdev, 0);
1774 if (pnp_dma_valid(pnpdev, 0))
1775 dma = pnp_dma(pnpdev, 0);
1776 else
1777 dma = -1;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001778
Pierre Ossman85bcc132005-05-08 19:35:27 +01001779 DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001780
Pierre Ossman85bcc132005-05-08 19:35:27 +01001781 return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1782}
1783
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001784static void wbsd_pnp_remove(struct pnp_dev *dev)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001785{
1786 wbsd_shutdown(&dev->dev, 1);
1787}
1788
1789#endif /* CONFIG_PNP */
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
1792 * Power management
1793 */
1794
1795#ifdef CONFIG_PM
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001796
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001797static int wbsd_platform_suspend(struct platform_device *dev,
1798 pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Russell King3ae5eae2005-11-09 22:32:44 +00001800 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001801 struct wbsd_host *host;
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001802
Pierre Ossman5e68d952006-01-08 14:21:52 +00001803 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001804 return 0;
1805
Pierre Ossman5e68d952006-01-08 14:21:52 +00001806 DBGF("Suspending...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001807
1808 host = mmc_priv(mmc);
1809
1810 wbsd_chip_poweroff(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 0;
1812}
1813
Pierre Ossman5e68d952006-01-08 14:21:52 +00001814static int wbsd_platform_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Russell King3ae5eae2005-11-09 22:32:44 +00001816 struct mmc_host *mmc = platform_get_drvdata(dev);
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001817 struct wbsd_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Pierre Ossman5e68d952006-01-08 14:21:52 +00001819 if (mmc == NULL)
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001820 return 0;
1821
Pierre Ossman5e68d952006-01-08 14:21:52 +00001822 DBGF("Resuming...\n");
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001823
1824 host = mmc_priv(mmc);
1825
1826 wbsd_chip_config(host);
1827
1828 /*
1829 * Allow device to initialise itself properly.
1830 */
1831 mdelay(5);
1832
Ulf Hansson83234ac2013-09-25 16:48:25 +02001833 wbsd_init_device(host);
1834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001836
Pierre Ossman5e68d952006-01-08 14:21:52 +00001837#ifdef CONFIG_PNP
1838
1839static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1840{
1841 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
Pierre Ossman5e68d952006-01-08 14:21:52 +00001842
1843 if (mmc == NULL)
1844 return 0;
1845
1846 DBGF("Suspending...\n");
Ulf Hansson83234ac2013-09-25 16:48:25 +02001847 return 0;
Pierre Ossman5e68d952006-01-08 14:21:52 +00001848}
1849
1850static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1851{
1852 struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1853 struct wbsd_host *host;
1854
1855 if (mmc == NULL)
1856 return 0;
1857
1858 DBGF("Resuming...\n");
1859
1860 host = mmc_priv(mmc);
1861
1862 /*
1863 * See if chip needs to be configured.
1864 */
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001865 if (host->config != 0) {
1866 if (!wbsd_chip_validate(host)) {
Joe Perches66061102014-09-12 14:56:56 -07001867 pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
Pierre Ossman5e68d952006-01-08 14:21:52 +00001868 wbsd_chip_config(host);
1869 }
1870 }
1871
1872 /*
1873 * Allow device to initialise itself properly.
1874 */
1875 mdelay(5);
1876
Ulf Hansson83234ac2013-09-25 16:48:25 +02001877 wbsd_init_device(host);
1878 return 0;
Pierre Ossman5e68d952006-01-08 14:21:52 +00001879}
1880
1881#endif /* CONFIG_PNP */
1882
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001883#else /* CONFIG_PM */
1884
Pierre Ossman5e68d952006-01-08 14:21:52 +00001885#define wbsd_platform_suspend NULL
1886#define wbsd_platform_resume NULL
1887
1888#define wbsd_pnp_suspend NULL
1889#define wbsd_pnp_resume NULL
Pierre Ossman19c1f3c2005-10-28 21:37:29 +01001890
1891#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Pierre Ossman85bcc132005-05-08 19:35:27 +01001893static struct platform_device *wbsd_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Russell King3ae5eae2005-11-09 22:32:44 +00001895static struct platform_driver wbsd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .probe = wbsd_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001897 .remove = wbsd_remove,
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001898
Pierre Ossman5e68d952006-01-08 14:21:52 +00001899 .suspend = wbsd_platform_suspend,
1900 .resume = wbsd_platform_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001901 .driver = {
1902 .name = DRIVER_NAME,
Douglas Anderson21b2cec2020-09-03 16:24:36 -07001903 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Russell King3ae5eae2005-11-09 22:32:44 +00001904 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905};
1906
Pierre Ossman85bcc132005-05-08 19:35:27 +01001907#ifdef CONFIG_PNP
1908
1909static struct pnp_driver wbsd_pnp_driver = {
1910 .name = DRIVER_NAME,
1911 .id_table = pnp_dev_table,
1912 .probe = wbsd_pnp_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001913 .remove = wbsd_pnp_remove,
Pierre Ossman5e68d952006-01-08 14:21:52 +00001914
1915 .suspend = wbsd_pnp_suspend,
1916 .resume = wbsd_pnp_resume,
Pierre Ossman85bcc132005-05-08 19:35:27 +01001917};
1918
1919#endif /* CONFIG_PNP */
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/*
1922 * Module loading/unloading
1923 */
1924
1925static int __init wbsd_drv_init(void)
1926{
1927 int result;
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001928
Girish K Sa3c76eb2011-10-11 11:44:09 +05301929 pr_info(DRIVER_NAME
Pierre Ossman1615cc22007-02-09 08:19:07 +01001930 ": Winbond W83L51xD SD/MMC card interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05301931 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Pierre Ossman85bcc132005-05-08 19:35:27 +01001933#ifdef CONFIG_PNP
1934
Tomas Winkler9eeebd22008-06-30 10:50:23 +03001935 if (!param_nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001936 result = pnp_register_driver(&wbsd_pnp_driver);
1937 if (result < 0)
1938 return result;
1939 }
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001940#endif /* CONFIG_PNP */
1941
Tomas Winkler9eeebd22008-06-30 10:50:23 +03001942 if (param_nopnp) {
Russell King3ae5eae2005-11-09 22:32:44 +00001943 result = platform_driver_register(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001944 if (result < 0)
1945 return result;
1946
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00001947 wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001948 if (!wbsd_device) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00001949 platform_driver_unregister(&wbsd_driver);
1950 return -ENOMEM;
1951 }
1952
1953 result = platform_device_add(wbsd_device);
Pierre Ossmancfa7f522006-01-08 18:17:55 +00001954 if (result) {
Dmitry Torokhov21500bb2006-01-03 22:57:35 +00001955 platform_device_put(wbsd_device);
1956 platform_driver_unregister(&wbsd_driver);
1957 return result;
1958 }
Pierre Ossman85bcc132005-05-08 19:35:27 +01001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 return 0;
1962}
1963
1964static void __exit wbsd_drv_exit(void)
1965{
Pierre Ossman85bcc132005-05-08 19:35:27 +01001966#ifdef CONFIG_PNP
1967
Tomas Winkler9eeebd22008-06-30 10:50:23 +03001968 if (!param_nopnp)
Pierre Ossman85bcc132005-05-08 19:35:27 +01001969 pnp_unregister_driver(&wbsd_pnp_driver);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001970
1971#endif /* CONFIG_PNP */
Pierre Ossman85bcc132005-05-08 19:35:27 +01001972
Tomas Winkler9eeebd22008-06-30 10:50:23 +03001973 if (param_nopnp) {
Pierre Ossman85bcc132005-05-08 19:35:27 +01001974 platform_device_unregister(wbsd_device);
Pierre Ossmanfecf92b2005-09-12 12:09:25 +01001975
Russell King3ae5eae2005-11-09 22:32:44 +00001976 platform_driver_unregister(&wbsd_driver);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 DBG("unloaded\n");
1980}
1981
1982module_init(wbsd_drv_init);
1983module_exit(wbsd_drv_exit);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001984#ifdef CONFIG_PNP
David Howellsdac562f2017-04-04 16:54:25 +01001985module_param_hw_named(nopnp, param_nopnp, uint, other, 0444);
Pierre Ossman85bcc132005-05-08 19:35:27 +01001986#endif
David Howellsdac562f2017-04-04 16:54:25 +01001987module_param_hw_named(io, param_io, uint, ioport, 0444);
1988module_param_hw_named(irq, param_irq, uint, irq, 0444);
1989module_param_hw_named(dma, param_dma, int, dma, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991MODULE_LICENSE("GPL");
Pierre Ossman32710e82009-04-08 20:14:54 +02001992MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Pierre Ossman85bcc132005-05-08 19:35:27 +01001995#ifdef CONFIG_PNP
1996MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
1997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1999MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2000MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");