blob: a5300f238d980931014952bc8fb7698b5c180f97 [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 Ossmanc9fddbc2007-12-02 19:52:11 +010046/* Controller can only DMA from 32-bit aligned addresses */
47#define SDHCI_QUIRK_32BIT_DMA_ADDR (1<<6)
48/* Controller can only DMA chunk sizes that are a multiple of 32 bits */
49#define SDHCI_QUIRK_32BIT_DMA_SIZE (1<<7)
Pierre Ossman645289d2006-06-30 02:22:33 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static const struct pci_device_id pci_ids[] __devinitdata = {
Pierre Ossman645289d2006-06-30 02:22:33 -070052 {
53 .vendor = PCI_VENDOR_ID_RICOH,
54 .device = PCI_DEVICE_ID_RICOH_R5C822,
55 .subvendor = PCI_VENDOR_ID_IBM,
56 .subdevice = PCI_ANY_ID,
Pierre Ossman98608072006-06-30 02:22:34 -070057 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
58 SDHCI_QUIRK_FORCE_DMA,
59 },
60
61 {
62 .vendor = PCI_VENDOR_ID_RICOH,
63 .device = PCI_DEVICE_ID_RICOH_R5C822,
64 .subvendor = PCI_ANY_ID,
65 .subdevice = PCI_ANY_ID,
Pierre Ossman8a4da142006-10-04 02:15:40 -070066 .driver_data = SDHCI_QUIRK_FORCE_DMA |
67 SDHCI_QUIRK_NO_CARD_NO_RESET,
Pierre Ossman98608072006-06-30 02:22:34 -070068 },
69
70 {
71 .vendor = PCI_VENDOR_ID_TI,
72 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
73 .subvendor = PCI_ANY_ID,
74 .subdevice = PCI_ANY_ID,
75 .driver_data = SDHCI_QUIRK_FORCE_DMA,
Pierre Ossman645289d2006-06-30 02:22:33 -070076 },
77
Darren Salt9e9dc5f2007-01-27 15:32:31 +010078 {
79 .vendor = PCI_VENDOR_ID_ENE,
80 .device = PCI_DEVICE_ID_ENE_CB712_SD,
81 .subvendor = PCI_ANY_ID,
82 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020083 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
84 SDHCI_QUIRK_BROKEN_DMA,
Darren Salt9e9dc5f2007-01-27 15:32:31 +010085 },
86
Milko Krachounov7de064e2007-05-19 01:18:03 +020087 {
88 .vendor = PCI_VENDOR_ID_ENE,
89 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
90 .subvendor = PCI_ANY_ID,
91 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020092 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
93 SDHCI_QUIRK_BROKEN_DMA,
Milko Krachounov7de064e2007-05-19 01:18:03 +020094 },
95
Leandro Dorileob8352262007-07-25 23:47:04 +020096 {
97 .vendor = PCI_VENDOR_ID_ENE,
98 .device = PCI_DEVICE_ID_ENE_CB714_SD,
99 .subvendor = PCI_ANY_ID,
100 .subdevice = PCI_ANY_ID,
101 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
102 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
103 },
104
105 {
106 .vendor = PCI_VENDOR_ID_ENE,
107 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
108 .subvendor = PCI_ANY_ID,
109 .subdevice = PCI_ANY_ID,
110 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
111 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
112 },
113
Pierre Ossman645289d2006-06-30 02:22:33 -0700114 { /* Generic SD host controller */
115 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
116 },
117
Pierre Ossmand129bce2006-03-24 03:18:17 -0800118 { /* end: all zeroes */ },
119};
120
121MODULE_DEVICE_TABLE(pci, pci_ids);
122
123static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
124static void sdhci_finish_data(struct sdhci_host *);
125
126static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
127static void sdhci_finish_command(struct sdhci_host *);
128
129static void sdhci_dumpregs(struct sdhci_host *host)
130{
131 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
132
133 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
134 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
135 readw(host->ioaddr + SDHCI_HOST_VERSION));
136 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
137 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
138 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
139 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
140 readl(host->ioaddr + SDHCI_ARGUMENT),
141 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
142 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
143 readl(host->ioaddr + SDHCI_PRESENT_STATE),
144 readb(host->ioaddr + SDHCI_HOST_CONTROL));
145 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
146 readb(host->ioaddr + SDHCI_POWER_CONTROL),
147 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
148 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Nicolas Pitre2df3b712007-09-29 10:46:20 -0400149 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800150 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
151 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
152 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
153 readl(host->ioaddr + SDHCI_INT_STATUS));
154 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
155 readl(host->ioaddr + SDHCI_INT_ENABLE),
156 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
157 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
158 readw(host->ioaddr + SDHCI_ACMD12_ERR),
159 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
160 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
161 readl(host->ioaddr + SDHCI_CAPABILITIES),
162 readl(host->ioaddr + SDHCI_MAX_CURRENT));
163
164 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
165}
166
167/*****************************************************************************\
168 * *
169 * Low level functions *
170 * *
171\*****************************************************************************/
172
173static void sdhci_reset(struct sdhci_host *host, u8 mask)
174{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700175 unsigned long timeout;
176
Pierre Ossman8a4da142006-10-04 02:15:40 -0700177 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
178 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
179 SDHCI_CARD_PRESENT))
180 return;
181 }
182
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
184
Pierre Ossmane16514d82006-06-30 02:22:24 -0700185 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186 host->clock = 0;
187
Pierre Ossmane16514d82006-06-30 02:22:24 -0700188 /* Wait max 100 ms */
189 timeout = 100;
190
191 /* hw clears the bit when it's done */
192 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
193 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100194 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700195 mmc_hostname(host->mmc), (int)mask);
196 sdhci_dumpregs(host);
197 return;
198 }
199 timeout--;
200 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201 }
202}
203
204static void sdhci_init(struct sdhci_host *host)
205{
206 u32 intmask;
207
208 sdhci_reset(host, SDHCI_RESET_ALL);
209
Pierre Ossman3192a282006-06-30 02:22:26 -0700210 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
211 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
212 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
213 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100214 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
Pierre Ossman3192a282006-06-30 02:22:26 -0700215 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216
217 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
218 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219}
220
221static void sdhci_activate_led(struct sdhci_host *host)
222{
223 u8 ctrl;
224
225 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
226 ctrl |= SDHCI_CTRL_LED;
227 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
228}
229
230static void sdhci_deactivate_led(struct sdhci_host *host)
231{
232 u8 ctrl;
233
234 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
235 ctrl &= ~SDHCI_CTRL_LED;
236 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
237}
238
239/*****************************************************************************\
240 * *
241 * Core functions *
242 * *
243\*****************************************************************************/
244
Pierre Ossman2a22b142007-02-02 18:27:42 +0100245static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800246{
Jens Axboe45711f12007-10-22 21:19:53 +0200247 return sg_virt(host->cur_sg);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248}
249
250static inline int sdhci_next_sg(struct sdhci_host* host)
251{
252 /*
253 * Skip to next SG entry.
254 */
255 host->cur_sg++;
256 host->num_sg--;
257
258 /*
259 * Any entries left?
260 */
261 if (host->num_sg > 0) {
262 host->offset = 0;
263 host->remain = host->cur_sg->length;
264 }
265
266 return host->num_sg;
267}
268
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100269static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270{
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100271 int blksize, chunk_remain;
272 u32 data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 char *buffer;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100274 int size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100276 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100278 blksize = host->data->blksz;
279 chunk_remain = 0;
280 data = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800281
Pierre Ossman2a22b142007-02-02 18:27:42 +0100282 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100284 while (blksize) {
285 if (chunk_remain == 0) {
286 data = readl(host->ioaddr + SDHCI_BUFFER);
287 chunk_remain = min(blksize, 4);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800288 }
289
Alex Dubov14d836e2007-04-13 19:04:38 +0200290 size = min(host->remain, chunk_remain);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292 chunk_remain -= size;
293 blksize -= size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800294 host->offset += size;
295 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200296
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100297 while (size) {
298 *buffer = data & 0xFF;
299 buffer++;
300 data >>= 8;
301 size--;
302 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
304 if (host->remain == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305 if (sdhci_next_sg(host) == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306 BUG_ON(blksize != 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307 return;
308 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100309 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310 }
311 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314static void sdhci_write_block_pio(struct sdhci_host *host)
315{
316 int blksize, chunk_remain;
317 u32 data;
318 char *buffer;
319 int bytes, size;
320
321 DBG("PIO writing\n");
322
323 blksize = host->data->blksz;
324 chunk_remain = 4;
325 data = 0;
326
327 bytes = 0;
Pierre Ossman2a22b142007-02-02 18:27:42 +0100328 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329
330 while (blksize) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200331 size = min(host->remain, chunk_remain);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100332
333 chunk_remain -= size;
334 blksize -= size;
335 host->offset += size;
336 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200337
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338 while (size) {
339 data >>= 8;
340 data |= (u32)*buffer << 24;
341 buffer++;
342 size--;
343 }
344
345 if (chunk_remain == 0) {
346 writel(data, host->ioaddr + SDHCI_BUFFER);
347 chunk_remain = min(blksize, 4);
348 }
349
350 if (host->remain == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351 if (sdhci_next_sg(host) == 0) {
352 BUG_ON(blksize != 0);
353 return;
354 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100355 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356 }
357 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358}
359
360static void sdhci_transfer_pio(struct sdhci_host *host)
361{
362 u32 mask;
363
364 BUG_ON(!host->data);
365
Alex Dubov14d836e2007-04-13 19:04:38 +0200366 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367 return;
368
369 if (host->data->flags & MMC_DATA_READ)
370 mask = SDHCI_DATA_AVAILABLE;
371 else
372 mask = SDHCI_SPACE_AVAILABLE;
373
374 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
375 if (host->data->flags & MMC_DATA_READ)
376 sdhci_read_block_pio(host);
377 else
378 sdhci_write_block_pio(host);
379
Alex Dubov14d836e2007-04-13 19:04:38 +0200380 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382 }
383
384 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800385}
386
387static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
388{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700389 u8 count;
390 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800391
392 WARN_ON(host->data);
393
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700394 if (data == NULL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800395 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800396
Pierre Ossmanbab76962006-07-02 16:51:35 +0100397 /* Sanity checks */
398 BUG_ON(data->blksz * data->blocks > 524288);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100399 BUG_ON(data->blksz > host->mmc->max_blk_size);
Pierre Ossman1d676e02006-07-02 16:52:10 +0100400 BUG_ON(data->blocks > 65535);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800401
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200402 host->data = data;
403 host->data_early = 0;
404
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700405 /* timeout in us */
406 target_timeout = data->timeout_ns / 1000 +
407 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800408
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700409 /*
410 * Figure out needed cycles.
411 * We do this in steps in order to fit inside a 32 bit int.
412 * The first step is the minimum timeout, which will have a
413 * minimum resolution of 6 bits:
414 * (1) 2^13*1000 > 2^22,
415 * (2) host->timeout_clk < 2^16
416 * =>
417 * (1) / (2) > 2^6
418 */
419 count = 0;
420 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
421 while (current_timeout < target_timeout) {
422 count++;
423 current_timeout <<= 1;
424 if (count >= 0xF)
425 break;
426 }
427
428 if (count >= 0xF) {
429 printk(KERN_WARNING "%s: Too large timeout requested!\n",
430 mmc_hostname(host->mmc));
431 count = 0xE;
432 }
433
434 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800435
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100436 if (host->flags & SDHCI_USE_DMA)
437 host->flags |= SDHCI_REQ_USE_DMA;
438
439 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) &&
440 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
441 ((data->blksz * data->blocks) & 0x3))) {
442 DBG("Reverting to PIO because of transfer size (%d)\n",
443 data->blksz * data->blocks);
444 host->flags &= ~SDHCI_REQ_USE_DMA;
445 }
446
447 /*
448 * The assumption here being that alignment is the same after
449 * translation to device address space.
450 */
451 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) &&
452 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
453 (data->sg->offset & 0x3))) {
454 DBG("Reverting to PIO because of bad alignment\n");
455 host->flags &= ~SDHCI_REQ_USE_DMA;
456 }
457
458 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800459 int count;
460
461 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
462 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
463 BUG_ON(count != 1);
464
465 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
466 } else {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800467 host->cur_sg = data->sg;
468 host->num_sg = data->sg_len;
469
470 host->offset = 0;
471 host->remain = host->cur_sg->length;
472 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700473
Pierre Ossmanbab76962006-07-02 16:51:35 +0100474 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
475 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
476 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700477 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
478}
479
480static void sdhci_set_transfer_mode(struct sdhci_host *host,
481 struct mmc_data *data)
482{
483 u16 mode;
484
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700485 if (data == NULL)
486 return;
487
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200488 WARN_ON(!host->data);
489
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700490 mode = SDHCI_TRNS_BLK_CNT_EN;
491 if (data->blocks > 1)
492 mode |= SDHCI_TRNS_MULTI;
493 if (data->flags & MMC_DATA_READ)
494 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100495 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700496 mode |= SDHCI_TRNS_DMA;
497
498 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800499}
500
501static void sdhci_finish_data(struct sdhci_host *host)
502{
503 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800504 u16 blocks;
505
506 BUG_ON(!host->data);
507
508 data = host->data;
509 host->data = NULL;
510
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100511 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800512 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
513 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800514 }
515
516 /*
517 * Controller doesn't count down when in single block mode.
518 */
Pierre Ossman2b061972007-08-12 13:13:24 +0200519 if (data->blocks == 1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200520 blocks = (data->error == 0) ? 0 : 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800521 else
522 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
Russell Kinga3fd4a12006-06-04 17:51:15 +0100523 data->bytes_xfered = data->blksz * (data->blocks - blocks);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800524
Pierre Ossman17b04292007-07-22 22:18:46 +0200525 if (!data->error && blocks) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800526 printk(KERN_ERR "%s: Controller signalled completion even "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100527 "though there were blocks left.\n",
528 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200529 data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800530 }
531
Pierre Ossmand129bce2006-03-24 03:18:17 -0800532 if (data->stop) {
533 /*
534 * The controller needs a reset of internal state machines
535 * upon error conditions.
536 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200537 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800538 sdhci_reset(host, SDHCI_RESET_CMD);
539 sdhci_reset(host, SDHCI_RESET_DATA);
540 }
541
542 sdhci_send_command(host, data->stop);
543 } else
544 tasklet_schedule(&host->finish_tasklet);
545}
546
547static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
548{
549 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700550 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700551 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800552
553 WARN_ON(host->cmd);
554
Pierre Ossmand129bce2006-03-24 03:18:17 -0800555 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700556 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700557
558 mask = SDHCI_CMD_INHIBIT;
559 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
560 mask |= SDHCI_DATA_INHIBIT;
561
562 /* We shouldn't wait for data inihibit for stop commands, even
563 though they might use busy signaling */
564 if (host->mrq->data && (cmd == host->mrq->data->stop))
565 mask &= ~SDHCI_DATA_INHIBIT;
566
567 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700568 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800569 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100570 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800571 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200572 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800573 tasklet_schedule(&host->finish_tasklet);
574 return;
575 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700576 timeout--;
577 mdelay(1);
578 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800579
580 mod_timer(&host->timer, jiffies + 10 * HZ);
581
582 host->cmd = cmd;
583
584 sdhci_prepare_data(host, cmd->data);
585
586 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
587
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700588 sdhci_set_transfer_mode(host, cmd->data);
589
Pierre Ossmand129bce2006-03-24 03:18:17 -0800590 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100591 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800592 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200593 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800594 tasklet_schedule(&host->finish_tasklet);
595 return;
596 }
597
598 if (!(cmd->flags & MMC_RSP_PRESENT))
599 flags = SDHCI_CMD_RESP_NONE;
600 else if (cmd->flags & MMC_RSP_136)
601 flags = SDHCI_CMD_RESP_LONG;
602 else if (cmd->flags & MMC_RSP_BUSY)
603 flags = SDHCI_CMD_RESP_SHORT_BUSY;
604 else
605 flags = SDHCI_CMD_RESP_SHORT;
606
607 if (cmd->flags & MMC_RSP_CRC)
608 flags |= SDHCI_CMD_CRC;
609 if (cmd->flags & MMC_RSP_OPCODE)
610 flags |= SDHCI_CMD_INDEX;
611 if (cmd->data)
612 flags |= SDHCI_CMD_DATA;
613
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200614 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800615 host->ioaddr + SDHCI_COMMAND);
616}
617
618static void sdhci_finish_command(struct sdhci_host *host)
619{
620 int i;
621
622 BUG_ON(host->cmd == NULL);
623
624 if (host->cmd->flags & MMC_RSP_PRESENT) {
625 if (host->cmd->flags & MMC_RSP_136) {
626 /* CRC is stripped so we need to do some shifting. */
627 for (i = 0;i < 4;i++) {
628 host->cmd->resp[i] = readl(host->ioaddr +
629 SDHCI_RESPONSE + (3-i)*4) << 8;
630 if (i != 3)
631 host->cmd->resp[i] |=
632 readb(host->ioaddr +
633 SDHCI_RESPONSE + (3-i)*4-1);
634 }
635 } else {
636 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
637 }
638 }
639
Pierre Ossman17b04292007-07-22 22:18:46 +0200640 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800641
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200642 if (host->data && host->data_early)
643 sdhci_finish_data(host);
644
645 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800646 tasklet_schedule(&host->finish_tasklet);
647
648 host->cmd = NULL;
649}
650
651static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
652{
653 int div;
654 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700655 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800656
657 if (clock == host->clock)
658 return;
659
660 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
661
662 if (clock == 0)
663 goto out;
664
665 for (div = 1;div < 256;div *= 2) {
666 if ((host->max_clk / div) <= clock)
667 break;
668 }
669 div >>= 1;
670
671 clk = div << SDHCI_DIVIDER_SHIFT;
672 clk |= SDHCI_CLOCK_INT_EN;
673 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
674
675 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700676 timeout = 10;
677 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
678 & SDHCI_CLOCK_INT_STABLE)) {
679 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100680 printk(KERN_ERR "%s: Internal clock never "
681 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800682 sdhci_dumpregs(host);
683 return;
684 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700685 timeout--;
686 mdelay(1);
687 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800688
689 clk |= SDHCI_CLOCK_CARD_EN;
690 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
691
692out:
693 host->clock = clock;
694}
695
Pierre Ossman146ad662006-06-30 02:22:23 -0700696static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
697{
698 u8 pwr;
699
700 if (host->power == power)
701 return;
702
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100703 if (power == (unsigned short)-1) {
704 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700705 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100706 }
707
708 /*
709 * Spec says that we should clear the power reg before setting
710 * a new value. Some controllers don't seem to like this though.
711 */
712 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
713 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700714
715 pwr = SDHCI_POWER_ON;
716
Philip Langdale4be34c92007-03-11 17:15:15 -0700717 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -0700718 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -0700719 pwr |= SDHCI_POWER_180;
720 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700721 case MMC_VDD_29_30:
722 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700723 pwr |= SDHCI_POWER_300;
724 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700725 case MMC_VDD_32_33:
726 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700727 pwr |= SDHCI_POWER_330;
728 break;
729 default:
730 BUG();
731 }
732
733 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
734
735out:
736 host->power = power;
737}
738
Pierre Ossmand129bce2006-03-24 03:18:17 -0800739/*****************************************************************************\
740 * *
741 * MMC callbacks *
742 * *
743\*****************************************************************************/
744
745static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
746{
747 struct sdhci_host *host;
748 unsigned long flags;
749
750 host = mmc_priv(mmc);
751
752 spin_lock_irqsave(&host->lock, flags);
753
754 WARN_ON(host->mrq != NULL);
755
756 sdhci_activate_led(host);
757
758 host->mrq = mrq;
759
760 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200761 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800762 tasklet_schedule(&host->finish_tasklet);
763 } else
764 sdhci_send_command(host, mrq->cmd);
765
Pierre Ossman5f25a662006-10-04 02:15:39 -0700766 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800767 spin_unlock_irqrestore(&host->lock, flags);
768}
769
770static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
771{
772 struct sdhci_host *host;
773 unsigned long flags;
774 u8 ctrl;
775
776 host = mmc_priv(mmc);
777
778 spin_lock_irqsave(&host->lock, flags);
779
Pierre Ossmand129bce2006-03-24 03:18:17 -0800780 /*
781 * Reset the chip on each power off.
782 * Should clear out any weird states.
783 */
784 if (ios->power_mode == MMC_POWER_OFF) {
785 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800786 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800787 }
788
789 sdhci_set_clock(host, ios->clock);
790
791 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -0700792 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800793 else
Pierre Ossman146ad662006-06-30 02:22:23 -0700794 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800795
796 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100797
Pierre Ossmand129bce2006-03-24 03:18:17 -0800798 if (ios->bus_width == MMC_BUS_WIDTH_4)
799 ctrl |= SDHCI_CTRL_4BITBUS;
800 else
801 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100802
803 if (ios->timing == MMC_TIMING_SD_HS)
804 ctrl |= SDHCI_CTRL_HISPD;
805 else
806 ctrl &= ~SDHCI_CTRL_HISPD;
807
Pierre Ossmand129bce2006-03-24 03:18:17 -0800808 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
809
Leandro Dorileob8352262007-07-25 23:47:04 +0200810 /*
811 * Some (ENE) controllers go apeshit on some ios operation,
812 * signalling timeout and CRC errors even on CMD0. Resetting
813 * it on each ios seems to solve the problem.
814 */
815 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
816 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
817
Pierre Ossman5f25a662006-10-04 02:15:39 -0700818 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800819 spin_unlock_irqrestore(&host->lock, flags);
820}
821
822static int sdhci_get_ro(struct mmc_host *mmc)
823{
824 struct sdhci_host *host;
825 unsigned long flags;
826 int present;
827
828 host = mmc_priv(mmc);
829
830 spin_lock_irqsave(&host->lock, flags);
831
832 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
833
834 spin_unlock_irqrestore(&host->lock, flags);
835
836 return !(present & SDHCI_WRITE_PROTECT);
837}
838
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200839static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
840{
841 struct sdhci_host *host;
842 unsigned long flags;
843 u32 ier;
844
845 host = mmc_priv(mmc);
846
847 spin_lock_irqsave(&host->lock, flags);
848
849 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
850
851 ier &= ~SDHCI_INT_CARD_INT;
852 if (enable)
853 ier |= SDHCI_INT_CARD_INT;
854
855 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
856 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
857
858 mmiowb();
859
860 spin_unlock_irqrestore(&host->lock, flags);
861}
862
David Brownellab7aefd2006-11-12 17:55:30 -0800863static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800864 .request = sdhci_request,
865 .set_ios = sdhci_set_ios,
866 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200867 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800868};
869
870/*****************************************************************************\
871 * *
872 * Tasklets *
873 * *
874\*****************************************************************************/
875
876static void sdhci_tasklet_card(unsigned long param)
877{
878 struct sdhci_host *host;
879 unsigned long flags;
880
881 host = (struct sdhci_host*)param;
882
883 spin_lock_irqsave(&host->lock, flags);
884
885 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
886 if (host->mrq) {
887 printk(KERN_ERR "%s: Card removed during transfer!\n",
888 mmc_hostname(host->mmc));
889 printk(KERN_ERR "%s: Resetting controller.\n",
890 mmc_hostname(host->mmc));
891
892 sdhci_reset(host, SDHCI_RESET_CMD);
893 sdhci_reset(host, SDHCI_RESET_DATA);
894
Pierre Ossman17b04292007-07-22 22:18:46 +0200895 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896 tasklet_schedule(&host->finish_tasklet);
897 }
898 }
899
900 spin_unlock_irqrestore(&host->lock, flags);
901
902 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
903}
904
905static void sdhci_tasklet_finish(unsigned long param)
906{
907 struct sdhci_host *host;
908 unsigned long flags;
909 struct mmc_request *mrq;
910
911 host = (struct sdhci_host*)param;
912
913 spin_lock_irqsave(&host->lock, flags);
914
915 del_timer(&host->timer);
916
917 mrq = host->mrq;
918
Pierre Ossmand129bce2006-03-24 03:18:17 -0800919 /*
920 * The controller needs a reset of internal state machines
921 * upon error conditions.
922 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200923 if (mrq->cmd->error ||
924 (mrq->data && (mrq->data->error ||
925 (mrq->data->stop && mrq->data->stop->error)))) {
Pierre Ossman645289d2006-06-30 02:22:33 -0700926
927 /* Some controllers need this kick or reset won't work here */
928 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
929 unsigned int clock;
930
931 /* This is to force an update */
932 clock = host->clock;
933 host->clock = 0;
934 sdhci_set_clock(host, clock);
935 }
936
937 /* Spec says we should do both at the same time, but Ricoh
938 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 sdhci_reset(host, SDHCI_RESET_CMD);
940 sdhci_reset(host, SDHCI_RESET_DATA);
941 }
942
943 host->mrq = NULL;
944 host->cmd = NULL;
945 host->data = NULL;
946
947 sdhci_deactivate_led(host);
948
Pierre Ossman5f25a662006-10-04 02:15:39 -0700949 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 spin_unlock_irqrestore(&host->lock, flags);
951
952 mmc_request_done(host->mmc, mrq);
953}
954
955static void sdhci_timeout_timer(unsigned long data)
956{
957 struct sdhci_host *host;
958 unsigned long flags;
959
960 host = (struct sdhci_host*)data;
961
962 spin_lock_irqsave(&host->lock, flags);
963
964 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100965 printk(KERN_ERR "%s: Timeout waiting for hardware "
966 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 sdhci_dumpregs(host);
968
969 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200970 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 sdhci_finish_data(host);
972 } else {
973 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +0200974 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200976 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977
978 tasklet_schedule(&host->finish_tasklet);
979 }
980 }
981
Pierre Ossman5f25a662006-10-04 02:15:39 -0700982 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 spin_unlock_irqrestore(&host->lock, flags);
984}
985
986/*****************************************************************************\
987 * *
988 * Interrupt handling *
989 * *
990\*****************************************************************************/
991
992static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
993{
994 BUG_ON(intmask == 0);
995
996 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +0200997 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
998 "though no command operation was in progress.\n",
999 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 sdhci_dumpregs(host);
1001 return;
1002 }
1003
Pierre Ossman43b58b32007-07-25 23:15:27 +02001004 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001005 host->cmd->error = -ETIMEDOUT;
1006 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1007 SDHCI_INT_INDEX))
1008 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
Pierre Ossman17b04292007-07-22 22:18:46 +02001010 if (host->cmd->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011 tasklet_schedule(&host->finish_tasklet);
Pierre Ossman43b58b32007-07-25 23:15:27 +02001012 else if (intmask & SDHCI_INT_RESPONSE)
1013 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014}
1015
1016static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1017{
1018 BUG_ON(intmask == 0);
1019
1020 if (!host->data) {
1021 /*
1022 * A data end interrupt is sent together with the response
1023 * for the stop command.
1024 */
1025 if (intmask & SDHCI_INT_DATA_END)
1026 return;
1027
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001028 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1029 "though no data operation was in progress.\n",
1030 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 sdhci_dumpregs(host);
1032
1033 return;
1034 }
1035
1036 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001037 host->data->error = -ETIMEDOUT;
1038 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1039 host->data->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040
Pierre Ossman17b04292007-07-22 22:18:46 +02001041 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 sdhci_finish_data(host);
1043 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001044 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001045 sdhci_transfer_pio(host);
1046
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001047 /*
1048 * We currently don't do anything fancy with DMA
1049 * boundaries, but as we can't disable the feature
1050 * we need to at least restart the transfer.
1051 */
1052 if (intmask & SDHCI_INT_DMA_END)
1053 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1054 host->ioaddr + SDHCI_DMA_ADDRESS);
1055
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001056 if (intmask & SDHCI_INT_DATA_END) {
1057 if (host->cmd) {
1058 /*
1059 * Data managed to finish before the
1060 * command completed. Make sure we do
1061 * things in the proper order.
1062 */
1063 host->data_early = 1;
1064 } else {
1065 sdhci_finish_data(host);
1066 }
1067 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068 }
1069}
1070
David Howells7d12e782006-10-05 14:55:46 +01001071static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072{
1073 irqreturn_t result;
1074 struct sdhci_host* host = dev_id;
1075 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001076 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
1078 spin_lock(&host->lock);
1079
1080 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1081
Mark Lord62df67a52007-03-06 13:30:13 +01001082 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083 result = IRQ_NONE;
1084 goto out;
1085 }
1086
1087 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
1088
Pierre Ossman3192a282006-06-30 02:22:26 -07001089 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1090 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1091 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001093 }
1094
1095 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096
1097 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098 writel(intmask & SDHCI_INT_CMD_MASK,
1099 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001100 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 }
1102
1103 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104 writel(intmask & SDHCI_INT_DATA_MASK,
1105 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001106 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107 }
1108
1109 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1110
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001111 intmask &= ~SDHCI_INT_ERROR;
1112
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001114 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001116 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 }
1118
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001119 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001120
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001121 if (intmask & SDHCI_INT_CARD_INT)
1122 cardint = 1;
1123
1124 intmask &= ~SDHCI_INT_CARD_INT;
1125
Pierre Ossman3192a282006-06-30 02:22:26 -07001126 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001127 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001128 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129 sdhci_dumpregs(host);
1130
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001132 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
1134 result = IRQ_HANDLED;
1135
Pierre Ossman5f25a662006-10-04 02:15:39 -07001136 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137out:
1138 spin_unlock(&host->lock);
1139
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001140 /*
1141 * We have to delay this as it calls back into the driver.
1142 */
1143 if (cardint)
1144 mmc_signal_sdio_irq(host->mmc);
1145
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146 return result;
1147}
1148
1149/*****************************************************************************\
1150 * *
1151 * Suspend/resume *
1152 * *
1153\*****************************************************************************/
1154
1155#ifdef CONFIG_PM
1156
1157static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1158{
1159 struct sdhci_chip *chip;
1160 int i, ret;
1161
1162 chip = pci_get_drvdata(pdev);
1163 if (!chip)
1164 return 0;
1165
1166 DBG("Suspending...\n");
1167
1168 for (i = 0;i < chip->num_slots;i++) {
1169 if (!chip->hosts[i])
1170 continue;
1171 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1172 if (ret) {
1173 for (i--;i >= 0;i--)
1174 mmc_resume_host(chip->hosts[i]->mmc);
1175 return ret;
1176 }
1177 }
1178
1179 pci_save_state(pdev);
1180 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001181
1182 for (i = 0;i < chip->num_slots;i++) {
1183 if (!chip->hosts[i])
1184 continue;
1185 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1186 }
1187
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188 pci_disable_device(pdev);
1189 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1190
1191 return 0;
1192}
1193
1194static int sdhci_resume (struct pci_dev *pdev)
1195{
1196 struct sdhci_chip *chip;
1197 int i, ret;
1198
1199 chip = pci_get_drvdata(pdev);
1200 if (!chip)
1201 return 0;
1202
1203 DBG("Resuming...\n");
1204
1205 pci_set_power_state(pdev, PCI_D0);
1206 pci_restore_state(pdev);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001207 ret = pci_enable_device(pdev);
1208 if (ret)
1209 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001210
1211 for (i = 0;i < chip->num_slots;i++) {
1212 if (!chip->hosts[i])
1213 continue;
1214 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1215 pci_set_master(pdev);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001216 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
1217 IRQF_SHARED, chip->hosts[i]->slot_descr,
1218 chip->hosts[i]);
1219 if (ret)
1220 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221 sdhci_init(chip->hosts[i]);
Pierre Ossman5f25a662006-10-04 02:15:39 -07001222 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223 ret = mmc_resume_host(chip->hosts[i]->mmc);
1224 if (ret)
1225 return ret;
1226 }
1227
1228 return 0;
1229}
1230
1231#else /* CONFIG_PM */
1232
1233#define sdhci_suspend NULL
1234#define sdhci_resume NULL
1235
1236#endif /* CONFIG_PM */
1237
1238/*****************************************************************************\
1239 * *
1240 * Device probing/removal *
1241 * *
1242\*****************************************************************************/
1243
1244static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1245{
1246 int ret;
Pierre Ossman4a965502006-06-30 02:22:29 -07001247 unsigned int version;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 struct sdhci_chip *chip;
1249 struct mmc_host *mmc;
1250 struct sdhci_host *host;
1251
1252 u8 first_bar;
1253 unsigned int caps;
1254
1255 chip = pci_get_drvdata(pdev);
1256 BUG_ON(!chip);
1257
1258 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1259 if (ret)
1260 return ret;
1261
1262 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1263
1264 if (first_bar > 5) {
1265 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1266 return -ENODEV;
1267 }
1268
1269 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1270 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1271 return -ENODEV;
1272 }
1273
1274 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
Pierre Ossmana98087c2006-12-07 19:17:20 +01001275 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1276 "You may experience problems.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277 }
1278
Pierre Ossman67435272006-06-30 02:22:31 -07001279 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1280 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1281 return -ENODEV;
1282 }
1283
1284 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1285 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1286 return -ENODEV;
1287 }
1288
Pierre Ossmand129bce2006-03-24 03:18:17 -08001289 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1290 if (!mmc)
1291 return -ENOMEM;
1292
1293 host = mmc_priv(mmc);
1294 host->mmc = mmc;
1295
Pierre Ossman8a4da142006-10-04 02:15:40 -07001296 host->chip = chip;
1297 chip->hosts[slot] = host;
1298
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299 host->bar = first_bar + slot;
1300
1301 host->addr = pci_resource_start(pdev, host->bar);
1302 host->irq = pdev->irq;
1303
1304 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1305
1306 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1307
1308 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1309 if (ret)
1310 goto free;
1311
1312 host->ioaddr = ioremap_nocache(host->addr,
1313 pci_resource_len(pdev, host->bar));
1314 if (!host->ioaddr) {
1315 ret = -ENOMEM;
1316 goto release;
1317 }
1318
Pierre Ossmand96649e2006-06-30 02:22:30 -07001319 sdhci_reset(host, SDHCI_RESET_ALL);
1320
Pierre Ossman4a965502006-06-30 02:22:29 -07001321 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1322 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
Pierre Ossmanc6573c92007-12-02 19:46:49 +01001323 if (version > 1) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001324 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossman8b1b2182006-07-11 21:07:10 +02001325 "You may experience problems.\n", host->slot_descr,
Pierre Ossman4a965502006-06-30 02:22:29 -07001326 version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001327 }
1328
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1330
Pierre Ossmand6f8dee2007-09-30 12:47:05 +02001331 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001332 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001333 else if (!(caps & SDHCI_CAN_DO_DMA))
1334 DBG("Controller doesn't have DMA capability\n");
1335 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 host->flags |= SDHCI_USE_DMA;
1337
Feng Tang7c168e32007-09-30 12:44:18 +02001338 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1339 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001340 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001341 host->flags &= ~SDHCI_USE_DMA;
1342 }
1343
Feng Tang56e71ef2007-09-29 14:15:05 +08001344 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1345 (host->flags & SDHCI_USE_DMA)) {
1346 printk(KERN_WARNING "%s: Will use DMA "
1347 "mode even though HW doesn't fully "
1348 "claim to support it.\n", host->slot_descr);
1349 }
1350
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 if (host->flags & SDHCI_USE_DMA) {
1352 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1353 printk(KERN_WARNING "%s: No suitable DMA available. "
1354 "Falling back to PIO.\n", host->slot_descr);
1355 host->flags &= ~SDHCI_USE_DMA;
1356 }
1357 }
1358
1359 if (host->flags & SDHCI_USE_DMA)
1360 pci_set_master(pdev);
1361 else /* XXX: Hack to get MMC layer to avoid highmem */
1362 pdev->dma_mask = 0;
1363
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001364 host->max_clk =
1365 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1366 if (host->max_clk == 0) {
1367 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1368 "frequency.\n", host->slot_descr);
1369 ret = -ENODEV;
1370 goto unmap;
1371 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 host->max_clk *= 1000000;
1373
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001374 host->timeout_clk =
1375 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1376 if (host->timeout_clk == 0) {
1377 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1378 "frequency.\n", host->slot_descr);
1379 ret = -ENODEV;
1380 goto unmap;
1381 }
1382 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1383 host->timeout_clk *= 1000;
1384
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 /*
1386 * Set host parameters.
1387 */
1388 mmc->ops = &sdhci_ops;
1389 mmc->f_min = host->max_clk / 256;
1390 mmc->f_max = host->max_clk;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001391 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001393 if (caps & SDHCI_CAN_DO_HISPD)
1394 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1395
Pierre Ossman146ad662006-06-30 02:22:23 -07001396 mmc->ocr_avail = 0;
1397 if (caps & SDHCI_CAN_VDD_330)
1398 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001399 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001400 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001401 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001402 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001403
1404 if (mmc->ocr_avail == 0) {
1405 printk(KERN_ERR "%s: Hardware doesn't report any "
1406 "support voltages.\n", host->slot_descr);
1407 ret = -ENODEV;
1408 goto unmap;
1409 }
1410
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411 spin_lock_init(&host->lock);
1412
1413 /*
1414 * Maximum number of segments. Hardware cannot do scatter lists.
1415 */
1416 if (host->flags & SDHCI_USE_DMA)
1417 mmc->max_hw_segs = 1;
1418 else
1419 mmc->max_hw_segs = 16;
1420 mmc->max_phys_segs = 16;
1421
1422 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001423 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001424 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001426 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001427
1428 /*
1429 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001430 * of bytes.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001432 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433
1434 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001435 * Maximum block size. This varies from controller to controller and
1436 * is specified in the capabilities register.
1437 */
1438 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1439 if (mmc->max_blk_size >= 3) {
David Vrabel03f85902007-08-10 13:25:03 +01001440 printk(KERN_WARNING "%s: Invalid maximum block size, assuming 512\n",
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001441 host->slot_descr);
David Vrabel03f85902007-08-10 13:25:03 +01001442 mmc->max_blk_size = 512;
1443 } else
1444 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001445
1446 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001447 * Maximum block count.
1448 */
1449 mmc->max_blk_count = 65535;
1450
1451 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452 * Init tasklets.
1453 */
1454 tasklet_init(&host->card_tasklet,
1455 sdhci_tasklet_card, (unsigned long)host);
1456 tasklet_init(&host->finish_tasklet,
1457 sdhci_tasklet_finish, (unsigned long)host);
1458
Al Viroe4cad1b2006-10-10 22:47:07 +01001459 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001461 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462 host->slot_descr, host);
1463 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001464 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465
1466 sdhci_init(host);
1467
1468#ifdef CONFIG_MMC_DEBUG
1469 sdhci_dumpregs(host);
1470#endif
1471
Pierre Ossman5f25a662006-10-04 02:15:39 -07001472 mmiowb();
1473
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474 mmc_add_host(mmc);
1475
1476 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1477 host->addr, host->irq,
1478 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1479
1480 return 0;
1481
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001482untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483 tasklet_kill(&host->card_tasklet);
1484 tasklet_kill(&host->finish_tasklet);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001485unmap:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486 iounmap(host->ioaddr);
1487release:
1488 pci_release_region(pdev, host->bar);
1489free:
1490 mmc_free_host(mmc);
1491
1492 return ret;
1493}
1494
1495static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1496{
1497 struct sdhci_chip *chip;
1498 struct mmc_host *mmc;
1499 struct sdhci_host *host;
1500
1501 chip = pci_get_drvdata(pdev);
1502 host = chip->hosts[slot];
1503 mmc = host->mmc;
1504
1505 chip->hosts[slot] = NULL;
1506
1507 mmc_remove_host(mmc);
1508
1509 sdhci_reset(host, SDHCI_RESET_ALL);
1510
1511 free_irq(host->irq, host);
1512
1513 del_timer_sync(&host->timer);
1514
1515 tasklet_kill(&host->card_tasklet);
1516 tasklet_kill(&host->finish_tasklet);
1517
1518 iounmap(host->ioaddr);
1519
1520 pci_release_region(pdev, host->bar);
1521
1522 mmc_free_host(mmc);
1523}
1524
1525static int __devinit sdhci_probe(struct pci_dev *pdev,
1526 const struct pci_device_id *ent)
1527{
1528 int ret, i;
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001529 u8 slots, rev;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530 struct sdhci_chip *chip;
1531
1532 BUG_ON(pdev == NULL);
1533 BUG_ON(ent == NULL);
1534
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001535 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1536
1537 printk(KERN_INFO DRIVER_NAME
1538 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1539 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1540 (int)rev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541
1542 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1543 if (ret)
1544 return ret;
1545
1546 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1547 DBG("found %d slot(s)\n", slots);
1548 if (slots == 0)
1549 return -ENODEV;
1550
1551 ret = pci_enable_device(pdev);
1552 if (ret)
1553 return ret;
1554
1555 chip = kzalloc(sizeof(struct sdhci_chip) +
1556 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1557 if (!chip) {
1558 ret = -ENOMEM;
1559 goto err;
1560 }
1561
1562 chip->pdev = pdev;
Pierre Ossmandf673b22006-06-30 02:22:31 -07001563 chip->quirks = ent->driver_data;
1564
1565 if (debug_quirks)
1566 chip->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001567
1568 chip->num_slots = slots;
1569 pci_set_drvdata(pdev, chip);
1570
1571 for (i = 0;i < slots;i++) {
1572 ret = sdhci_probe_slot(pdev, i);
1573 if (ret) {
1574 for (i--;i >= 0;i--)
1575 sdhci_remove_slot(pdev, i);
1576 goto free;
1577 }
1578 }
1579
1580 return 0;
1581
1582free:
1583 pci_set_drvdata(pdev, NULL);
1584 kfree(chip);
1585
1586err:
1587 pci_disable_device(pdev);
1588 return ret;
1589}
1590
1591static void __devexit sdhci_remove(struct pci_dev *pdev)
1592{
1593 int i;
1594 struct sdhci_chip *chip;
1595
1596 chip = pci_get_drvdata(pdev);
1597
1598 if (chip) {
1599 for (i = 0;i < chip->num_slots;i++)
1600 sdhci_remove_slot(pdev, i);
1601
1602 pci_set_drvdata(pdev, NULL);
1603
1604 kfree(chip);
1605 }
1606
1607 pci_disable_device(pdev);
1608}
1609
1610static struct pci_driver sdhci_driver = {
1611 .name = DRIVER_NAME,
1612 .id_table = pci_ids,
1613 .probe = sdhci_probe,
1614 .remove = __devexit_p(sdhci_remove),
1615 .suspend = sdhci_suspend,
1616 .resume = sdhci_resume,
1617};
1618
1619/*****************************************************************************\
1620 * *
1621 * Driver init/exit *
1622 * *
1623\*****************************************************************************/
1624
1625static int __init sdhci_drv_init(void)
1626{
1627 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001628 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1630
1631 return pci_register_driver(&sdhci_driver);
1632}
1633
1634static void __exit sdhci_drv_exit(void)
1635{
1636 DBG("Exiting\n");
1637
1638 pci_unregister_driver(&sdhci_driver);
1639}
1640
1641module_init(sdhci_drv_init);
1642module_exit(sdhci_drv_exit);
1643
Pierre Ossmandf673b22006-06-30 02:22:31 -07001644module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001645
Pierre Ossmand129bce2006-03-24 03:18:17 -08001646MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1647MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001649
Pierre Ossmandf673b22006-06-30 02:22:31 -07001650MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");