blob: 17b4e391db3590a02feb53b931cfc3f30abed30d [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Alex Dubov14d836e2007-04-13 19:04:38 +02004 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
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.
Pierre Ossmand129bce2006-03-24 03:18:17 -080010 */
11
Pierre Ossmand129bce2006-03-24 03:18:17 -080012#include <linux/delay.h>
13#include <linux/highmem.h>
14#include <linux/pci.h>
15#include <linux/dma-mapping.h>
Ralf Baechle11763602007-10-23 20:42:11 +020016#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080017
18#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include "sdhci.h"
21
22#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080023
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010025 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080026
Pierre Ossmandf673b22006-06-30 02:22:31 -070027static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070028
Pierre Ossmandc934412007-12-02 19:45:19 +010029/*
30 * Different quirks to handle when the hardware deviates from a strict
31 * interpretation of the SDHCI specification.
32 */
33
34/* Controller doesn't honor resets unless we touch the clock register */
Pierre Ossman645289d2006-06-30 02:22:33 -070035#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
Pierre Ossmandc934412007-12-02 19:45:19 +010036/* Controller has bad caps bits, but really supports DMA */
Pierre Ossman98608072006-06-30 02:22:34 -070037#define SDHCI_QUIRK_FORCE_DMA (1<<1)
Pierre Ossman8a4da142006-10-04 02:15:40 -070038/* Controller doesn't like some resets when there is no card inserted. */
39#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
Pierre Ossmandc934412007-12-02 19:45:19 +010040/* Controller doesn't like clearing the power reg before a change */
Darren Salt9e9dc5f2007-01-27 15:32:31 +010041#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
Pierre Ossmandc934412007-12-02 19:45:19 +010042/* Controller has flaky internal state so reset it on each ios change */
Leandro Dorileob8352262007-07-25 23:47:04 +020043#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
Pierre Ossmandc934412007-12-02 19:45:19 +010044/* Controller has an unusable DMA engine */
Feng Tang7c168e32007-09-30 12:44:18 +020045#define SDHCI_QUIRK_BROKEN_DMA (1<<5)
Pierre Ossman645289d2006-06-30 02:22:33 -070046
Pierre Ossmand129bce2006-03-24 03:18:17 -080047static const struct pci_device_id pci_ids[] __devinitdata = {
Pierre Ossman645289d2006-06-30 02:22:33 -070048 {
49 .vendor = PCI_VENDOR_ID_RICOH,
50 .device = PCI_DEVICE_ID_RICOH_R5C822,
51 .subvendor = PCI_VENDOR_ID_IBM,
52 .subdevice = PCI_ANY_ID,
Pierre Ossman98608072006-06-30 02:22:34 -070053 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
54 SDHCI_QUIRK_FORCE_DMA,
55 },
56
57 {
58 .vendor = PCI_VENDOR_ID_RICOH,
59 .device = PCI_DEVICE_ID_RICOH_R5C822,
60 .subvendor = PCI_ANY_ID,
61 .subdevice = PCI_ANY_ID,
Pierre Ossman8a4da142006-10-04 02:15:40 -070062 .driver_data = SDHCI_QUIRK_FORCE_DMA |
63 SDHCI_QUIRK_NO_CARD_NO_RESET,
Pierre Ossman98608072006-06-30 02:22:34 -070064 },
65
66 {
67 .vendor = PCI_VENDOR_ID_TI,
68 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
69 .subvendor = PCI_ANY_ID,
70 .subdevice = PCI_ANY_ID,
71 .driver_data = SDHCI_QUIRK_FORCE_DMA,
Pierre Ossman645289d2006-06-30 02:22:33 -070072 },
73
Darren Salt9e9dc5f2007-01-27 15:32:31 +010074 {
75 .vendor = PCI_VENDOR_ID_ENE,
76 .device = PCI_DEVICE_ID_ENE_CB712_SD,
77 .subvendor = PCI_ANY_ID,
78 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020079 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
80 SDHCI_QUIRK_BROKEN_DMA,
Darren Salt9e9dc5f2007-01-27 15:32:31 +010081 },
82
Milko Krachounov7de064e2007-05-19 01:18:03 +020083 {
84 .vendor = PCI_VENDOR_ID_ENE,
85 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
86 .subvendor = PCI_ANY_ID,
87 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020088 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
89 SDHCI_QUIRK_BROKEN_DMA,
Milko Krachounov7de064e2007-05-19 01:18:03 +020090 },
91
Leandro Dorileob8352262007-07-25 23:47:04 +020092 {
93 .vendor = PCI_VENDOR_ID_ENE,
94 .device = PCI_DEVICE_ID_ENE_CB714_SD,
95 .subvendor = PCI_ANY_ID,
96 .subdevice = PCI_ANY_ID,
97 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
98 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
99 },
100
101 {
102 .vendor = PCI_VENDOR_ID_ENE,
103 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
104 .subvendor = PCI_ANY_ID,
105 .subdevice = PCI_ANY_ID,
106 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
107 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
108 },
109
Pierre Ossman645289d2006-06-30 02:22:33 -0700110 { /* Generic SD host controller */
111 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
112 },
113
Pierre Ossmand129bce2006-03-24 03:18:17 -0800114 { /* end: all zeroes */ },
115};
116
117MODULE_DEVICE_TABLE(pci, pci_ids);
118
119static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
120static void sdhci_finish_data(struct sdhci_host *);
121
122static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
123static void sdhci_finish_command(struct sdhci_host *);
124
125static void sdhci_dumpregs(struct sdhci_host *host)
126{
127 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
128
129 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
130 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
131 readw(host->ioaddr + SDHCI_HOST_VERSION));
132 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
133 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
134 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
135 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
136 readl(host->ioaddr + SDHCI_ARGUMENT),
137 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
138 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
139 readl(host->ioaddr + SDHCI_PRESENT_STATE),
140 readb(host->ioaddr + SDHCI_HOST_CONTROL));
141 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
142 readb(host->ioaddr + SDHCI_POWER_CONTROL),
143 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
144 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Nicolas Pitre2df3b712007-09-29 10:46:20 -0400145 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800146 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
147 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
148 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
149 readl(host->ioaddr + SDHCI_INT_STATUS));
150 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
151 readl(host->ioaddr + SDHCI_INT_ENABLE),
152 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
153 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
154 readw(host->ioaddr + SDHCI_ACMD12_ERR),
155 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
156 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
157 readl(host->ioaddr + SDHCI_CAPABILITIES),
158 readl(host->ioaddr + SDHCI_MAX_CURRENT));
159
160 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
161}
162
163/*****************************************************************************\
164 * *
165 * Low level functions *
166 * *
167\*****************************************************************************/
168
169static void sdhci_reset(struct sdhci_host *host, u8 mask)
170{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700171 unsigned long timeout;
172
Pierre Ossman8a4da142006-10-04 02:15:40 -0700173 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
174 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
175 SDHCI_CARD_PRESENT))
176 return;
177 }
178
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
180
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182 host->clock = 0;
183
Pierre Ossmane16514d82006-06-30 02:22:24 -0700184 /* Wait max 100 ms */
185 timeout = 100;
186
187 /* hw clears the bit when it's done */
188 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
189 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100190 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700191 mmc_hostname(host->mmc), (int)mask);
192 sdhci_dumpregs(host);
193 return;
194 }
195 timeout--;
196 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800197 }
198}
199
200static void sdhci_init(struct sdhci_host *host)
201{
202 u32 intmask;
203
204 sdhci_reset(host, SDHCI_RESET_ALL);
205
Pierre Ossman3192a282006-06-30 02:22:26 -0700206 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
207 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
208 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
209 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100210 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
Pierre Ossman3192a282006-06-30 02:22:26 -0700211 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212
213 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
214 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
217static void sdhci_activate_led(struct sdhci_host *host)
218{
219 u8 ctrl;
220
221 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
222 ctrl |= SDHCI_CTRL_LED;
223 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
224}
225
226static void sdhci_deactivate_led(struct sdhci_host *host)
227{
228 u8 ctrl;
229
230 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
231 ctrl &= ~SDHCI_CTRL_LED;
232 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
233}
234
235/*****************************************************************************\
236 * *
237 * Core functions *
238 * *
239\*****************************************************************************/
240
Pierre Ossman2a22b142007-02-02 18:27:42 +0100241static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242{
Jens Axboe45711f12007-10-22 21:19:53 +0200243 return sg_virt(host->cur_sg);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244}
245
246static inline int sdhci_next_sg(struct sdhci_host* host)
247{
248 /*
249 * Skip to next SG entry.
250 */
251 host->cur_sg++;
252 host->num_sg--;
253
254 /*
255 * Any entries left?
256 */
257 if (host->num_sg > 0) {
258 host->offset = 0;
259 host->remain = host->cur_sg->length;
260 }
261
262 return host->num_sg;
263}
264
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100265static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266{
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100267 int blksize, chunk_remain;
268 u32 data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269 char *buffer;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100270 int size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100272 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100274 blksize = host->data->blksz;
275 chunk_remain = 0;
276 data = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277
Pierre Ossman2a22b142007-02-02 18:27:42 +0100278 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100280 while (blksize) {
281 if (chunk_remain == 0) {
282 data = readl(host->ioaddr + SDHCI_BUFFER);
283 chunk_remain = min(blksize, 4);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284 }
285
Alex Dubov14d836e2007-04-13 19:04:38 +0200286 size = min(host->remain, chunk_remain);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800287
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100288 chunk_remain -= size;
289 blksize -= size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800290 host->offset += size;
291 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200292
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100293 while (size) {
294 *buffer = data & 0xFF;
295 buffer++;
296 data >>= 8;
297 size--;
298 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800299
300 if (host->remain == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301 if (sdhci_next_sg(host) == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100302 BUG_ON(blksize != 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303 return;
304 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100305 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306 }
307 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310static void sdhci_write_block_pio(struct sdhci_host *host)
311{
312 int blksize, chunk_remain;
313 u32 data;
314 char *buffer;
315 int bytes, size;
316
317 DBG("PIO writing\n");
318
319 blksize = host->data->blksz;
320 chunk_remain = 4;
321 data = 0;
322
323 bytes = 0;
Pierre Ossman2a22b142007-02-02 18:27:42 +0100324 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100325
326 while (blksize) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200327 size = min(host->remain, chunk_remain);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100328
329 chunk_remain -= size;
330 blksize -= size;
331 host->offset += size;
332 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200333
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100334 while (size) {
335 data >>= 8;
336 data |= (u32)*buffer << 24;
337 buffer++;
338 size--;
339 }
340
341 if (chunk_remain == 0) {
342 writel(data, host->ioaddr + SDHCI_BUFFER);
343 chunk_remain = min(blksize, 4);
344 }
345
346 if (host->remain == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347 if (sdhci_next_sg(host) == 0) {
348 BUG_ON(blksize != 0);
349 return;
350 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100351 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352 }
353 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354}
355
356static void sdhci_transfer_pio(struct sdhci_host *host)
357{
358 u32 mask;
359
360 BUG_ON(!host->data);
361
Alex Dubov14d836e2007-04-13 19:04:38 +0200362 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363 return;
364
365 if (host->data->flags & MMC_DATA_READ)
366 mask = SDHCI_DATA_AVAILABLE;
367 else
368 mask = SDHCI_SPACE_AVAILABLE;
369
370 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
371 if (host->data->flags & MMC_DATA_READ)
372 sdhci_read_block_pio(host);
373 else
374 sdhci_write_block_pio(host);
375
Alex Dubov14d836e2007-04-13 19:04:38 +0200376 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378 }
379
380 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800381}
382
383static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
384{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700385 u8 count;
386 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800387
388 WARN_ON(host->data);
389
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700390 if (data == NULL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800391 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800392
Pierre Ossmanbab76962006-07-02 16:51:35 +0100393 /* Sanity checks */
394 BUG_ON(data->blksz * data->blocks > 524288);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100395 BUG_ON(data->blksz > host->mmc->max_blk_size);
Pierre Ossman1d676e02006-07-02 16:52:10 +0100396 BUG_ON(data->blocks > 65535);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800397
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200398 host->data = data;
399 host->data_early = 0;
400
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700401 /* timeout in us */
402 target_timeout = data->timeout_ns / 1000 +
403 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800404
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700405 /*
406 * Figure out needed cycles.
407 * We do this in steps in order to fit inside a 32 bit int.
408 * The first step is the minimum timeout, which will have a
409 * minimum resolution of 6 bits:
410 * (1) 2^13*1000 > 2^22,
411 * (2) host->timeout_clk < 2^16
412 * =>
413 * (1) / (2) > 2^6
414 */
415 count = 0;
416 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
417 while (current_timeout < target_timeout) {
418 count++;
419 current_timeout <<= 1;
420 if (count >= 0xF)
421 break;
422 }
423
424 if (count >= 0xF) {
425 printk(KERN_WARNING "%s: Too large timeout requested!\n",
426 mmc_hostname(host->mmc));
427 count = 0xE;
428 }
429
430 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800431
432 if (host->flags & SDHCI_USE_DMA) {
433 int count;
434
435 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
436 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
437 BUG_ON(count != 1);
438
439 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
440 } else {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800441 host->cur_sg = data->sg;
442 host->num_sg = data->sg_len;
443
444 host->offset = 0;
445 host->remain = host->cur_sg->length;
446 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700447
Pierre Ossmanbab76962006-07-02 16:51:35 +0100448 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
449 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
450 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700451 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
452}
453
454static void sdhci_set_transfer_mode(struct sdhci_host *host,
455 struct mmc_data *data)
456{
457 u16 mode;
458
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700459 if (data == NULL)
460 return;
461
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200462 WARN_ON(!host->data);
463
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700464 mode = SDHCI_TRNS_BLK_CNT_EN;
465 if (data->blocks > 1)
466 mode |= SDHCI_TRNS_MULTI;
467 if (data->flags & MMC_DATA_READ)
468 mode |= SDHCI_TRNS_READ;
469 if (host->flags & SDHCI_USE_DMA)
470 mode |= SDHCI_TRNS_DMA;
471
472 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800473}
474
475static void sdhci_finish_data(struct sdhci_host *host)
476{
477 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800478 u16 blocks;
479
480 BUG_ON(!host->data);
481
482 data = host->data;
483 host->data = NULL;
484
485 if (host->flags & SDHCI_USE_DMA) {
486 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
487 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800488 }
489
490 /*
491 * Controller doesn't count down when in single block mode.
492 */
Pierre Ossman2b061972007-08-12 13:13:24 +0200493 if (data->blocks == 1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200494 blocks = (data->error == 0) ? 0 : 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800495 else
496 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
Russell Kinga3fd4a12006-06-04 17:51:15 +0100497 data->bytes_xfered = data->blksz * (data->blocks - blocks);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800498
Pierre Ossman17b04292007-07-22 22:18:46 +0200499 if (!data->error && blocks) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800500 printk(KERN_ERR "%s: Controller signalled completion even "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100501 "though there were blocks left.\n",
502 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200503 data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800504 }
505
Pierre Ossmand129bce2006-03-24 03:18:17 -0800506 if (data->stop) {
507 /*
508 * The controller needs a reset of internal state machines
509 * upon error conditions.
510 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200511 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800512 sdhci_reset(host, SDHCI_RESET_CMD);
513 sdhci_reset(host, SDHCI_RESET_DATA);
514 }
515
516 sdhci_send_command(host, data->stop);
517 } else
518 tasklet_schedule(&host->finish_tasklet);
519}
520
521static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
522{
523 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700524 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700525 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800526
527 WARN_ON(host->cmd);
528
Pierre Ossmand129bce2006-03-24 03:18:17 -0800529 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700530 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700531
532 mask = SDHCI_CMD_INHIBIT;
533 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
534 mask |= SDHCI_DATA_INHIBIT;
535
536 /* We shouldn't wait for data inihibit for stop commands, even
537 though they might use busy signaling */
538 if (host->mrq->data && (cmd == host->mrq->data->stop))
539 mask &= ~SDHCI_DATA_INHIBIT;
540
541 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700542 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800543 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100544 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800545 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200546 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800547 tasklet_schedule(&host->finish_tasklet);
548 return;
549 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700550 timeout--;
551 mdelay(1);
552 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800553
554 mod_timer(&host->timer, jiffies + 10 * HZ);
555
556 host->cmd = cmd;
557
558 sdhci_prepare_data(host, cmd->data);
559
560 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
561
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700562 sdhci_set_transfer_mode(host, cmd->data);
563
Pierre Ossmand129bce2006-03-24 03:18:17 -0800564 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100565 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800566 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200567 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800568 tasklet_schedule(&host->finish_tasklet);
569 return;
570 }
571
572 if (!(cmd->flags & MMC_RSP_PRESENT))
573 flags = SDHCI_CMD_RESP_NONE;
574 else if (cmd->flags & MMC_RSP_136)
575 flags = SDHCI_CMD_RESP_LONG;
576 else if (cmd->flags & MMC_RSP_BUSY)
577 flags = SDHCI_CMD_RESP_SHORT_BUSY;
578 else
579 flags = SDHCI_CMD_RESP_SHORT;
580
581 if (cmd->flags & MMC_RSP_CRC)
582 flags |= SDHCI_CMD_CRC;
583 if (cmd->flags & MMC_RSP_OPCODE)
584 flags |= SDHCI_CMD_INDEX;
585 if (cmd->data)
586 flags |= SDHCI_CMD_DATA;
587
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200588 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800589 host->ioaddr + SDHCI_COMMAND);
590}
591
592static void sdhci_finish_command(struct sdhci_host *host)
593{
594 int i;
595
596 BUG_ON(host->cmd == NULL);
597
598 if (host->cmd->flags & MMC_RSP_PRESENT) {
599 if (host->cmd->flags & MMC_RSP_136) {
600 /* CRC is stripped so we need to do some shifting. */
601 for (i = 0;i < 4;i++) {
602 host->cmd->resp[i] = readl(host->ioaddr +
603 SDHCI_RESPONSE + (3-i)*4) << 8;
604 if (i != 3)
605 host->cmd->resp[i] |=
606 readb(host->ioaddr +
607 SDHCI_RESPONSE + (3-i)*4-1);
608 }
609 } else {
610 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
611 }
612 }
613
Pierre Ossman17b04292007-07-22 22:18:46 +0200614 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800615
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200616 if (host->data && host->data_early)
617 sdhci_finish_data(host);
618
619 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800620 tasklet_schedule(&host->finish_tasklet);
621
622 host->cmd = NULL;
623}
624
625static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
626{
627 int div;
628 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700629 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800630
631 if (clock == host->clock)
632 return;
633
634 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
635
636 if (clock == 0)
637 goto out;
638
639 for (div = 1;div < 256;div *= 2) {
640 if ((host->max_clk / div) <= clock)
641 break;
642 }
643 div >>= 1;
644
645 clk = div << SDHCI_DIVIDER_SHIFT;
646 clk |= SDHCI_CLOCK_INT_EN;
647 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
648
649 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700650 timeout = 10;
651 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
652 & SDHCI_CLOCK_INT_STABLE)) {
653 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100654 printk(KERN_ERR "%s: Internal clock never "
655 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800656 sdhci_dumpregs(host);
657 return;
658 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700659 timeout--;
660 mdelay(1);
661 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800662
663 clk |= SDHCI_CLOCK_CARD_EN;
664 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
665
666out:
667 host->clock = clock;
668}
669
Pierre Ossman146ad662006-06-30 02:22:23 -0700670static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
671{
672 u8 pwr;
673
674 if (host->power == power)
675 return;
676
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100677 if (power == (unsigned short)-1) {
678 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700679 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100680 }
681
682 /*
683 * Spec says that we should clear the power reg before setting
684 * a new value. Some controllers don't seem to like this though.
685 */
686 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
687 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700688
689 pwr = SDHCI_POWER_ON;
690
Philip Langdale4be34c92007-03-11 17:15:15 -0700691 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -0700692 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -0700693 pwr |= SDHCI_POWER_180;
694 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700695 case MMC_VDD_29_30:
696 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700697 pwr |= SDHCI_POWER_300;
698 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700699 case MMC_VDD_32_33:
700 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700701 pwr |= SDHCI_POWER_330;
702 break;
703 default:
704 BUG();
705 }
706
707 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
708
709out:
710 host->power = power;
711}
712
Pierre Ossmand129bce2006-03-24 03:18:17 -0800713/*****************************************************************************\
714 * *
715 * MMC callbacks *
716 * *
717\*****************************************************************************/
718
719static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
720{
721 struct sdhci_host *host;
722 unsigned long flags;
723
724 host = mmc_priv(mmc);
725
726 spin_lock_irqsave(&host->lock, flags);
727
728 WARN_ON(host->mrq != NULL);
729
730 sdhci_activate_led(host);
731
732 host->mrq = mrq;
733
734 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200735 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800736 tasklet_schedule(&host->finish_tasklet);
737 } else
738 sdhci_send_command(host, mrq->cmd);
739
Pierre Ossman5f25a662006-10-04 02:15:39 -0700740 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800741 spin_unlock_irqrestore(&host->lock, flags);
742}
743
744static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
745{
746 struct sdhci_host *host;
747 unsigned long flags;
748 u8 ctrl;
749
750 host = mmc_priv(mmc);
751
752 spin_lock_irqsave(&host->lock, flags);
753
Pierre Ossmand129bce2006-03-24 03:18:17 -0800754 /*
755 * Reset the chip on each power off.
756 * Should clear out any weird states.
757 */
758 if (ios->power_mode == MMC_POWER_OFF) {
759 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800760 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800761 }
762
763 sdhci_set_clock(host, ios->clock);
764
765 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -0700766 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800767 else
Pierre Ossman146ad662006-06-30 02:22:23 -0700768 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800769
770 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100771
Pierre Ossmand129bce2006-03-24 03:18:17 -0800772 if (ios->bus_width == MMC_BUS_WIDTH_4)
773 ctrl |= SDHCI_CTRL_4BITBUS;
774 else
775 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100776
777 if (ios->timing == MMC_TIMING_SD_HS)
778 ctrl |= SDHCI_CTRL_HISPD;
779 else
780 ctrl &= ~SDHCI_CTRL_HISPD;
781
Pierre Ossmand129bce2006-03-24 03:18:17 -0800782 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
783
Leandro Dorileob8352262007-07-25 23:47:04 +0200784 /*
785 * Some (ENE) controllers go apeshit on some ios operation,
786 * signalling timeout and CRC errors even on CMD0. Resetting
787 * it on each ios seems to solve the problem.
788 */
789 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
790 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
791
Pierre Ossman5f25a662006-10-04 02:15:39 -0700792 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800793 spin_unlock_irqrestore(&host->lock, flags);
794}
795
796static int sdhci_get_ro(struct mmc_host *mmc)
797{
798 struct sdhci_host *host;
799 unsigned long flags;
800 int present;
801
802 host = mmc_priv(mmc);
803
804 spin_lock_irqsave(&host->lock, flags);
805
806 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
807
808 spin_unlock_irqrestore(&host->lock, flags);
809
810 return !(present & SDHCI_WRITE_PROTECT);
811}
812
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200813static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
814{
815 struct sdhci_host *host;
816 unsigned long flags;
817 u32 ier;
818
819 host = mmc_priv(mmc);
820
821 spin_lock_irqsave(&host->lock, flags);
822
823 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
824
825 ier &= ~SDHCI_INT_CARD_INT;
826 if (enable)
827 ier |= SDHCI_INT_CARD_INT;
828
829 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
830 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
831
832 mmiowb();
833
834 spin_unlock_irqrestore(&host->lock, flags);
835}
836
David Brownellab7aefd2006-11-12 17:55:30 -0800837static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800838 .request = sdhci_request,
839 .set_ios = sdhci_set_ios,
840 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200841 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800842};
843
844/*****************************************************************************\
845 * *
846 * Tasklets *
847 * *
848\*****************************************************************************/
849
850static void sdhci_tasklet_card(unsigned long param)
851{
852 struct sdhci_host *host;
853 unsigned long flags;
854
855 host = (struct sdhci_host*)param;
856
857 spin_lock_irqsave(&host->lock, flags);
858
859 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
860 if (host->mrq) {
861 printk(KERN_ERR "%s: Card removed during transfer!\n",
862 mmc_hostname(host->mmc));
863 printk(KERN_ERR "%s: Resetting controller.\n",
864 mmc_hostname(host->mmc));
865
866 sdhci_reset(host, SDHCI_RESET_CMD);
867 sdhci_reset(host, SDHCI_RESET_DATA);
868
Pierre Ossman17b04292007-07-22 22:18:46 +0200869 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800870 tasklet_schedule(&host->finish_tasklet);
871 }
872 }
873
874 spin_unlock_irqrestore(&host->lock, flags);
875
876 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
877}
878
879static void sdhci_tasklet_finish(unsigned long param)
880{
881 struct sdhci_host *host;
882 unsigned long flags;
883 struct mmc_request *mrq;
884
885 host = (struct sdhci_host*)param;
886
887 spin_lock_irqsave(&host->lock, flags);
888
889 del_timer(&host->timer);
890
891 mrq = host->mrq;
892
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893 /*
894 * The controller needs a reset of internal state machines
895 * upon error conditions.
896 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200897 if (mrq->cmd->error ||
898 (mrq->data && (mrq->data->error ||
899 (mrq->data->stop && mrq->data->stop->error)))) {
Pierre Ossman645289d2006-06-30 02:22:33 -0700900
901 /* Some controllers need this kick or reset won't work here */
902 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
903 unsigned int clock;
904
905 /* This is to force an update */
906 clock = host->clock;
907 host->clock = 0;
908 sdhci_set_clock(host, clock);
909 }
910
911 /* Spec says we should do both at the same time, but Ricoh
912 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913 sdhci_reset(host, SDHCI_RESET_CMD);
914 sdhci_reset(host, SDHCI_RESET_DATA);
915 }
916
917 host->mrq = NULL;
918 host->cmd = NULL;
919 host->data = NULL;
920
921 sdhci_deactivate_led(host);
922
Pierre Ossman5f25a662006-10-04 02:15:39 -0700923 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 spin_unlock_irqrestore(&host->lock, flags);
925
926 mmc_request_done(host->mmc, mrq);
927}
928
929static void sdhci_timeout_timer(unsigned long data)
930{
931 struct sdhci_host *host;
932 unsigned long flags;
933
934 host = (struct sdhci_host*)data;
935
936 spin_lock_irqsave(&host->lock, flags);
937
938 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100939 printk(KERN_ERR "%s: Timeout waiting for hardware "
940 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 sdhci_dumpregs(host);
942
943 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200944 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945 sdhci_finish_data(host);
946 } else {
947 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +0200948 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200950 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951
952 tasklet_schedule(&host->finish_tasklet);
953 }
954 }
955
Pierre Ossman5f25a662006-10-04 02:15:39 -0700956 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 spin_unlock_irqrestore(&host->lock, flags);
958}
959
960/*****************************************************************************\
961 * *
962 * Interrupt handling *
963 * *
964\*****************************************************************************/
965
966static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
967{
968 BUG_ON(intmask == 0);
969
970 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +0200971 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
972 "though no command operation was in progress.\n",
973 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 sdhci_dumpregs(host);
975 return;
976 }
977
Pierre Ossman43b58b32007-07-25 23:15:27 +0200978 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200979 host->cmd->error = -ETIMEDOUT;
980 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
981 SDHCI_INT_INDEX))
982 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
Pierre Ossman17b04292007-07-22 22:18:46 +0200984 if (host->cmd->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800985 tasklet_schedule(&host->finish_tasklet);
Pierre Ossman43b58b32007-07-25 23:15:27 +0200986 else if (intmask & SDHCI_INT_RESPONSE)
987 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988}
989
990static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
991{
992 BUG_ON(intmask == 0);
993
994 if (!host->data) {
995 /*
996 * A data end interrupt is sent together with the response
997 * for the stop command.
998 */
999 if (intmask & SDHCI_INT_DATA_END)
1000 return;
1001
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001002 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1003 "though no data operation was in progress.\n",
1004 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 sdhci_dumpregs(host);
1006
1007 return;
1008 }
1009
1010 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001011 host->data->error = -ETIMEDOUT;
1012 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1013 host->data->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Pierre Ossman17b04292007-07-22 22:18:46 +02001015 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 sdhci_finish_data(host);
1017 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001018 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 sdhci_transfer_pio(host);
1020
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001021 /*
1022 * We currently don't do anything fancy with DMA
1023 * boundaries, but as we can't disable the feature
1024 * we need to at least restart the transfer.
1025 */
1026 if (intmask & SDHCI_INT_DMA_END)
1027 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1028 host->ioaddr + SDHCI_DMA_ADDRESS);
1029
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001030 if (intmask & SDHCI_INT_DATA_END) {
1031 if (host->cmd) {
1032 /*
1033 * Data managed to finish before the
1034 * command completed. Make sure we do
1035 * things in the proper order.
1036 */
1037 host->data_early = 1;
1038 } else {
1039 sdhci_finish_data(host);
1040 }
1041 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 }
1043}
1044
David Howells7d12e782006-10-05 14:55:46 +01001045static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046{
1047 irqreturn_t result;
1048 struct sdhci_host* host = dev_id;
1049 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001050 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051
1052 spin_lock(&host->lock);
1053
1054 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1055
Mark Lord62df67a52007-03-06 13:30:13 +01001056 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 result = IRQ_NONE;
1058 goto out;
1059 }
1060
1061 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
1062
Pierre Ossman3192a282006-06-30 02:22:26 -07001063 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1064 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1065 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001067 }
1068
1069 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070
1071 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 writel(intmask & SDHCI_INT_CMD_MASK,
1073 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001074 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075 }
1076
1077 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 writel(intmask & SDHCI_INT_DATA_MASK,
1079 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001080 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 }
1082
1083 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1084
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001085 intmask &= ~SDHCI_INT_ERROR;
1086
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001088 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001090 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091 }
1092
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001093 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001094
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001095 if (intmask & SDHCI_INT_CARD_INT)
1096 cardint = 1;
1097
1098 intmask &= ~SDHCI_INT_CARD_INT;
1099
Pierre Ossman3192a282006-06-30 02:22:26 -07001100 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001101 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001102 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103 sdhci_dumpregs(host);
1104
Pierre Ossmand129bce2006-03-24 03:18:17 -08001105 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001106 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
1108 result = IRQ_HANDLED;
1109
Pierre Ossman5f25a662006-10-04 02:15:39 -07001110 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111out:
1112 spin_unlock(&host->lock);
1113
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001114 /*
1115 * We have to delay this as it calls back into the driver.
1116 */
1117 if (cardint)
1118 mmc_signal_sdio_irq(host->mmc);
1119
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120 return result;
1121}
1122
1123/*****************************************************************************\
1124 * *
1125 * Suspend/resume *
1126 * *
1127\*****************************************************************************/
1128
1129#ifdef CONFIG_PM
1130
1131static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1132{
1133 struct sdhci_chip *chip;
1134 int i, ret;
1135
1136 chip = pci_get_drvdata(pdev);
1137 if (!chip)
1138 return 0;
1139
1140 DBG("Suspending...\n");
1141
1142 for (i = 0;i < chip->num_slots;i++) {
1143 if (!chip->hosts[i])
1144 continue;
1145 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1146 if (ret) {
1147 for (i--;i >= 0;i--)
1148 mmc_resume_host(chip->hosts[i]->mmc);
1149 return ret;
1150 }
1151 }
1152
1153 pci_save_state(pdev);
1154 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001155
1156 for (i = 0;i < chip->num_slots;i++) {
1157 if (!chip->hosts[i])
1158 continue;
1159 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1160 }
1161
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 pci_disable_device(pdev);
1163 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1164
1165 return 0;
1166}
1167
1168static int sdhci_resume (struct pci_dev *pdev)
1169{
1170 struct sdhci_chip *chip;
1171 int i, ret;
1172
1173 chip = pci_get_drvdata(pdev);
1174 if (!chip)
1175 return 0;
1176
1177 DBG("Resuming...\n");
1178
1179 pci_set_power_state(pdev, PCI_D0);
1180 pci_restore_state(pdev);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001181 ret = pci_enable_device(pdev);
1182 if (ret)
1183 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184
1185 for (i = 0;i < chip->num_slots;i++) {
1186 if (!chip->hosts[i])
1187 continue;
1188 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1189 pci_set_master(pdev);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001190 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
1191 IRQF_SHARED, chip->hosts[i]->slot_descr,
1192 chip->hosts[i]);
1193 if (ret)
1194 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001195 sdhci_init(chip->hosts[i]);
Pierre Ossman5f25a662006-10-04 02:15:39 -07001196 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197 ret = mmc_resume_host(chip->hosts[i]->mmc);
1198 if (ret)
1199 return ret;
1200 }
1201
1202 return 0;
1203}
1204
1205#else /* CONFIG_PM */
1206
1207#define sdhci_suspend NULL
1208#define sdhci_resume NULL
1209
1210#endif /* CONFIG_PM */
1211
1212/*****************************************************************************\
1213 * *
1214 * Device probing/removal *
1215 * *
1216\*****************************************************************************/
1217
1218static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1219{
1220 int ret;
Pierre Ossman4a965502006-06-30 02:22:29 -07001221 unsigned int version;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222 struct sdhci_chip *chip;
1223 struct mmc_host *mmc;
1224 struct sdhci_host *host;
1225
1226 u8 first_bar;
1227 unsigned int caps;
1228
1229 chip = pci_get_drvdata(pdev);
1230 BUG_ON(!chip);
1231
1232 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1233 if (ret)
1234 return ret;
1235
1236 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1237
1238 if (first_bar > 5) {
1239 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1240 return -ENODEV;
1241 }
1242
1243 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1244 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1245 return -ENODEV;
1246 }
1247
1248 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
Pierre Ossmana98087c2006-12-07 19:17:20 +01001249 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1250 "You may experience problems.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251 }
1252
Pierre Ossman67435272006-06-30 02:22:31 -07001253 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1254 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1255 return -ENODEV;
1256 }
1257
1258 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1259 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1260 return -ENODEV;
1261 }
1262
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1264 if (!mmc)
1265 return -ENOMEM;
1266
1267 host = mmc_priv(mmc);
1268 host->mmc = mmc;
1269
Pierre Ossman8a4da142006-10-04 02:15:40 -07001270 host->chip = chip;
1271 chip->hosts[slot] = host;
1272
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273 host->bar = first_bar + slot;
1274
1275 host->addr = pci_resource_start(pdev, host->bar);
1276 host->irq = pdev->irq;
1277
1278 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1279
1280 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1281
1282 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1283 if (ret)
1284 goto free;
1285
1286 host->ioaddr = ioremap_nocache(host->addr,
1287 pci_resource_len(pdev, host->bar));
1288 if (!host->ioaddr) {
1289 ret = -ENOMEM;
1290 goto release;
1291 }
1292
Pierre Ossmand96649e2006-06-30 02:22:30 -07001293 sdhci_reset(host, SDHCI_RESET_ALL);
1294
Pierre Ossman4a965502006-06-30 02:22:29 -07001295 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1296 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1297 if (version != 0) {
1298 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossman8b1b2182006-07-11 21:07:10 +02001299 "You may experience problems.\n", host->slot_descr,
Pierre Ossman4a965502006-06-30 02:22:29 -07001300 version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001301 }
1302
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1304
Pierre Ossmand6f8dee2007-09-30 12:47:05 +02001305 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001306 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001307 else if (!(caps & SDHCI_CAN_DO_DMA))
1308 DBG("Controller doesn't have DMA capability\n");
1309 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 host->flags |= SDHCI_USE_DMA;
1311
Feng Tang7c168e32007-09-30 12:44:18 +02001312 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1313 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001314 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001315 host->flags &= ~SDHCI_USE_DMA;
1316 }
1317
Feng Tang56e71ef2007-09-29 14:15:05 +08001318 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1319 (host->flags & SDHCI_USE_DMA)) {
1320 printk(KERN_WARNING "%s: Will use DMA "
1321 "mode even though HW doesn't fully "
1322 "claim to support it.\n", host->slot_descr);
1323 }
1324
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 if (host->flags & SDHCI_USE_DMA) {
1326 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1327 printk(KERN_WARNING "%s: No suitable DMA available. "
1328 "Falling back to PIO.\n", host->slot_descr);
1329 host->flags &= ~SDHCI_USE_DMA;
1330 }
1331 }
1332
1333 if (host->flags & SDHCI_USE_DMA)
1334 pci_set_master(pdev);
1335 else /* XXX: Hack to get MMC layer to avoid highmem */
1336 pdev->dma_mask = 0;
1337
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001338 host->max_clk =
1339 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1340 if (host->max_clk == 0) {
1341 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1342 "frequency.\n", host->slot_descr);
1343 ret = -ENODEV;
1344 goto unmap;
1345 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346 host->max_clk *= 1000000;
1347
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001348 host->timeout_clk =
1349 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1350 if (host->timeout_clk == 0) {
1351 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1352 "frequency.\n", host->slot_descr);
1353 ret = -ENODEV;
1354 goto unmap;
1355 }
1356 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1357 host->timeout_clk *= 1000;
1358
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359 /*
1360 * Set host parameters.
1361 */
1362 mmc->ops = &sdhci_ops;
1363 mmc->f_min = host->max_clk / 256;
1364 mmc->f_max = host->max_clk;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001365 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001366
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001367 if (caps & SDHCI_CAN_DO_HISPD)
1368 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1369
Pierre Ossman146ad662006-06-30 02:22:23 -07001370 mmc->ocr_avail = 0;
1371 if (caps & SDHCI_CAN_VDD_330)
1372 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001373 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001374 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001375 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001376 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001377
1378 if (mmc->ocr_avail == 0) {
1379 printk(KERN_ERR "%s: Hardware doesn't report any "
1380 "support voltages.\n", host->slot_descr);
1381 ret = -ENODEV;
1382 goto unmap;
1383 }
1384
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 spin_lock_init(&host->lock);
1386
1387 /*
1388 * Maximum number of segments. Hardware cannot do scatter lists.
1389 */
1390 if (host->flags & SDHCI_USE_DMA)
1391 mmc->max_hw_segs = 1;
1392 else
1393 mmc->max_hw_segs = 16;
1394 mmc->max_phys_segs = 16;
1395
1396 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001397 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001398 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001399 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001400 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001401
1402 /*
1403 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001404 * of bytes.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001405 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001406 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001407
1408 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001409 * Maximum block size. This varies from controller to controller and
1410 * is specified in the capabilities register.
1411 */
1412 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1413 if (mmc->max_blk_size >= 3) {
David Vrabel03f85902007-08-10 13:25:03 +01001414 printk(KERN_WARNING "%s: Invalid maximum block size, assuming 512\n",
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001415 host->slot_descr);
David Vrabel03f85902007-08-10 13:25:03 +01001416 mmc->max_blk_size = 512;
1417 } else
1418 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001419
1420 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001421 * Maximum block count.
1422 */
1423 mmc->max_blk_count = 65535;
1424
1425 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001426 * Init tasklets.
1427 */
1428 tasklet_init(&host->card_tasklet,
1429 sdhci_tasklet_card, (unsigned long)host);
1430 tasklet_init(&host->finish_tasklet,
1431 sdhci_tasklet_finish, (unsigned long)host);
1432
Al Viroe4cad1b2006-10-10 22:47:07 +01001433 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001434
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001435 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436 host->slot_descr, host);
1437 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001438 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439
1440 sdhci_init(host);
1441
1442#ifdef CONFIG_MMC_DEBUG
1443 sdhci_dumpregs(host);
1444#endif
1445
Pierre Ossman5f25a662006-10-04 02:15:39 -07001446 mmiowb();
1447
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448 mmc_add_host(mmc);
1449
1450 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1451 host->addr, host->irq,
1452 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1453
1454 return 0;
1455
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001456untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457 tasklet_kill(&host->card_tasklet);
1458 tasklet_kill(&host->finish_tasklet);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001459unmap:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460 iounmap(host->ioaddr);
1461release:
1462 pci_release_region(pdev, host->bar);
1463free:
1464 mmc_free_host(mmc);
1465
1466 return ret;
1467}
1468
1469static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1470{
1471 struct sdhci_chip *chip;
1472 struct mmc_host *mmc;
1473 struct sdhci_host *host;
1474
1475 chip = pci_get_drvdata(pdev);
1476 host = chip->hosts[slot];
1477 mmc = host->mmc;
1478
1479 chip->hosts[slot] = NULL;
1480
1481 mmc_remove_host(mmc);
1482
1483 sdhci_reset(host, SDHCI_RESET_ALL);
1484
1485 free_irq(host->irq, host);
1486
1487 del_timer_sync(&host->timer);
1488
1489 tasklet_kill(&host->card_tasklet);
1490 tasklet_kill(&host->finish_tasklet);
1491
1492 iounmap(host->ioaddr);
1493
1494 pci_release_region(pdev, host->bar);
1495
1496 mmc_free_host(mmc);
1497}
1498
1499static int __devinit sdhci_probe(struct pci_dev *pdev,
1500 const struct pci_device_id *ent)
1501{
1502 int ret, i;
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001503 u8 slots, rev;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504 struct sdhci_chip *chip;
1505
1506 BUG_ON(pdev == NULL);
1507 BUG_ON(ent == NULL);
1508
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001509 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1510
1511 printk(KERN_INFO DRIVER_NAME
1512 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1513 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1514 (int)rev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515
1516 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1517 if (ret)
1518 return ret;
1519
1520 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1521 DBG("found %d slot(s)\n", slots);
1522 if (slots == 0)
1523 return -ENODEV;
1524
1525 ret = pci_enable_device(pdev);
1526 if (ret)
1527 return ret;
1528
1529 chip = kzalloc(sizeof(struct sdhci_chip) +
1530 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1531 if (!chip) {
1532 ret = -ENOMEM;
1533 goto err;
1534 }
1535
1536 chip->pdev = pdev;
Pierre Ossmandf673b22006-06-30 02:22:31 -07001537 chip->quirks = ent->driver_data;
1538
1539 if (debug_quirks)
1540 chip->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541
1542 chip->num_slots = slots;
1543 pci_set_drvdata(pdev, chip);
1544
1545 for (i = 0;i < slots;i++) {
1546 ret = sdhci_probe_slot(pdev, i);
1547 if (ret) {
1548 for (i--;i >= 0;i--)
1549 sdhci_remove_slot(pdev, i);
1550 goto free;
1551 }
1552 }
1553
1554 return 0;
1555
1556free:
1557 pci_set_drvdata(pdev, NULL);
1558 kfree(chip);
1559
1560err:
1561 pci_disable_device(pdev);
1562 return ret;
1563}
1564
1565static void __devexit sdhci_remove(struct pci_dev *pdev)
1566{
1567 int i;
1568 struct sdhci_chip *chip;
1569
1570 chip = pci_get_drvdata(pdev);
1571
1572 if (chip) {
1573 for (i = 0;i < chip->num_slots;i++)
1574 sdhci_remove_slot(pdev, i);
1575
1576 pci_set_drvdata(pdev, NULL);
1577
1578 kfree(chip);
1579 }
1580
1581 pci_disable_device(pdev);
1582}
1583
1584static struct pci_driver sdhci_driver = {
1585 .name = DRIVER_NAME,
1586 .id_table = pci_ids,
1587 .probe = sdhci_probe,
1588 .remove = __devexit_p(sdhci_remove),
1589 .suspend = sdhci_suspend,
1590 .resume = sdhci_resume,
1591};
1592
1593/*****************************************************************************\
1594 * *
1595 * Driver init/exit *
1596 * *
1597\*****************************************************************************/
1598
1599static int __init sdhci_drv_init(void)
1600{
1601 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001602 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1604
1605 return pci_register_driver(&sdhci_driver);
1606}
1607
1608static void __exit sdhci_drv_exit(void)
1609{
1610 DBG("Exiting\n");
1611
1612 pci_unregister_driver(&sdhci_driver);
1613}
1614
1615module_init(sdhci_drv_init);
1616module_exit(sdhci_drv_exit);
1617
Pierre Ossmandf673b22006-06-30 02:22:31 -07001618module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001619
Pierre Ossmand129bce2006-03-24 03:18:17 -08001620MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1621MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001623
Pierre Ossmandf673b22006-06-30 02:22:31 -07001624MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");