blob: a57f6a3d48d340e94ad5e07ca6b6901ad4ff562f [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
2 * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver
3 *
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>
16
17#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080018
19#include <asm/scatterlist.h>
20
21#include "sdhci.h"
22
23#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080024
Pierre Ossmand129bce2006-03-24 03:18:17 -080025#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010026 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossman67435272006-06-30 02:22:31 -070028static unsigned int debug_nodma = 0;
29static unsigned int debug_forcedma = 0;
Pierre Ossmandf673b22006-06-30 02:22:31 -070030static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070031
Pierre Ossman645289d2006-06-30 02:22:33 -070032#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
Pierre Ossman98608072006-06-30 02:22:34 -070033#define SDHCI_QUIRK_FORCE_DMA (1<<1)
Pierre Ossman8a4da142006-10-04 02:15:40 -070034/* Controller doesn't like some resets when there is no card inserted. */
35#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
Darren Salt9e9dc5f2007-01-27 15:32:31 +010036#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
Pierre Ossman645289d2006-06-30 02:22:33 -070037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038static const struct pci_device_id pci_ids[] __devinitdata = {
Pierre Ossman645289d2006-06-30 02:22:33 -070039 {
40 .vendor = PCI_VENDOR_ID_RICOH,
41 .device = PCI_DEVICE_ID_RICOH_R5C822,
42 .subvendor = PCI_VENDOR_ID_IBM,
43 .subdevice = PCI_ANY_ID,
Pierre Ossman98608072006-06-30 02:22:34 -070044 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
45 SDHCI_QUIRK_FORCE_DMA,
46 },
47
48 {
49 .vendor = PCI_VENDOR_ID_RICOH,
50 .device = PCI_DEVICE_ID_RICOH_R5C822,
51 .subvendor = PCI_ANY_ID,
52 .subdevice = PCI_ANY_ID,
Pierre Ossman8a4da142006-10-04 02:15:40 -070053 .driver_data = SDHCI_QUIRK_FORCE_DMA |
54 SDHCI_QUIRK_NO_CARD_NO_RESET,
Pierre Ossman98608072006-06-30 02:22:34 -070055 },
56
57 {
58 .vendor = PCI_VENDOR_ID_TI,
59 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
60 .subvendor = PCI_ANY_ID,
61 .subdevice = PCI_ANY_ID,
62 .driver_data = SDHCI_QUIRK_FORCE_DMA,
Pierre Ossman645289d2006-06-30 02:22:33 -070063 },
64
Darren Salt9e9dc5f2007-01-27 15:32:31 +010065 {
66 .vendor = PCI_VENDOR_ID_ENE,
67 .device = PCI_DEVICE_ID_ENE_CB712_SD,
68 .subvendor = PCI_ANY_ID,
69 .subdevice = PCI_ANY_ID,
70 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE,
71 },
72
Pierre Ossman645289d2006-06-30 02:22:33 -070073 { /* Generic SD host controller */
74 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
75 },
76
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 { /* end: all zeroes */ },
78};
79
80MODULE_DEVICE_TABLE(pci, pci_ids);
81
82static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
83static void sdhci_finish_data(struct sdhci_host *);
84
85static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
86static void sdhci_finish_command(struct sdhci_host *);
87
88static void sdhci_dumpregs(struct sdhci_host *host)
89{
90 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
91
92 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
93 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
94 readw(host->ioaddr + SDHCI_HOST_VERSION));
95 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
96 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
97 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
98 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
99 readl(host->ioaddr + SDHCI_ARGUMENT),
100 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
101 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
102 readl(host->ioaddr + SDHCI_PRESENT_STATE),
103 readb(host->ioaddr + SDHCI_HOST_CONTROL));
104 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
105 readb(host->ioaddr + SDHCI_POWER_CONTROL),
106 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
107 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
108 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
109 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
110 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
111 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
112 readl(host->ioaddr + SDHCI_INT_STATUS));
113 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
114 readl(host->ioaddr + SDHCI_INT_ENABLE),
115 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
116 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
117 readw(host->ioaddr + SDHCI_ACMD12_ERR),
118 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
119 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
120 readl(host->ioaddr + SDHCI_CAPABILITIES),
121 readl(host->ioaddr + SDHCI_MAX_CURRENT));
122
123 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
124}
125
126/*****************************************************************************\
127 * *
128 * Low level functions *
129 * *
130\*****************************************************************************/
131
132static void sdhci_reset(struct sdhci_host *host, u8 mask)
133{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700134 unsigned long timeout;
135
Pierre Ossman8a4da142006-10-04 02:15:40 -0700136 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
137 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
138 SDHCI_CARD_PRESENT))
139 return;
140 }
141
Pierre Ossmand129bce2006-03-24 03:18:17 -0800142 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
143
Pierre Ossmane16514d2006-06-30 02:22:24 -0700144 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800145 host->clock = 0;
146
Pierre Ossmane16514d2006-06-30 02:22:24 -0700147 /* Wait max 100 ms */
148 timeout = 100;
149
150 /* hw clears the bit when it's done */
151 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
152 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100153 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700154 mmc_hostname(host->mmc), (int)mask);
155 sdhci_dumpregs(host);
156 return;
157 }
158 timeout--;
159 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800160 }
161}
162
163static void sdhci_init(struct sdhci_host *host)
164{
165 u32 intmask;
166
167 sdhci_reset(host, SDHCI_RESET_ALL);
168
Pierre Ossman3192a282006-06-30 02:22:26 -0700169 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
170 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
171 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
172 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100173 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
Pierre Ossman3192a282006-06-30 02:22:26 -0700174 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175
176 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
177 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178}
179
180static void sdhci_activate_led(struct sdhci_host *host)
181{
182 u8 ctrl;
183
184 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
185 ctrl |= SDHCI_CTRL_LED;
186 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
187}
188
189static void sdhci_deactivate_led(struct sdhci_host *host)
190{
191 u8 ctrl;
192
193 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
194 ctrl &= ~SDHCI_CTRL_LED;
195 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
196}
197
198/*****************************************************************************\
199 * *
200 * Core functions *
201 * *
202\*****************************************************************************/
203
Pierre Ossman2a22b142007-02-02 18:27:42 +0100204static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205{
Pierre Ossman2a22b142007-02-02 18:27:42 +0100206 return page_address(host->cur_sg->page) + host->cur_sg->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800207}
208
209static inline int sdhci_next_sg(struct sdhci_host* host)
210{
211 /*
212 * Skip to next SG entry.
213 */
214 host->cur_sg++;
215 host->num_sg--;
216
217 /*
218 * Any entries left?
219 */
220 if (host->num_sg > 0) {
221 host->offset = 0;
222 host->remain = host->cur_sg->length;
223 }
224
225 return host->num_sg;
226}
227
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100228static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100230 int blksize, chunk_remain;
231 u32 data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232 char *buffer;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100233 int size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100235 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100237 blksize = host->data->blksz;
238 chunk_remain = 0;
239 data = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800240
Pierre Ossman2a22b142007-02-02 18:27:42 +0100241 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100243 while (blksize) {
244 if (chunk_remain == 0) {
245 data = readl(host->ioaddr + SDHCI_BUFFER);
246 chunk_remain = min(blksize, 4);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247 }
248
Alex Dubov14d836e2007-04-13 19:04:38 +0200249 size = min(host->remain, chunk_remain);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100251 chunk_remain -= size;
252 blksize -= size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253 host->offset += size;
254 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200255
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100256 while (size) {
257 *buffer = data & 0xFF;
258 buffer++;
259 data >>= 8;
260 size--;
261 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262
263 if (host->remain == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264 if (sdhci_next_sg(host) == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100265 BUG_ON(blksize != 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266 return;
267 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100268 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269 }
270 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100271}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100273static void sdhci_write_block_pio(struct sdhci_host *host)
274{
275 int blksize, chunk_remain;
276 u32 data;
277 char *buffer;
278 int bytes, size;
279
280 DBG("PIO writing\n");
281
282 blksize = host->data->blksz;
283 chunk_remain = 4;
284 data = 0;
285
286 bytes = 0;
Pierre Ossman2a22b142007-02-02 18:27:42 +0100287 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100288
289 while (blksize) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200290 size = min(host->remain, chunk_remain);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100291
292 chunk_remain -= size;
293 blksize -= size;
294 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 data >>= 8;
299 data |= (u32)*buffer << 24;
300 buffer++;
301 size--;
302 }
303
304 if (chunk_remain == 0) {
305 writel(data, host->ioaddr + SDHCI_BUFFER);
306 chunk_remain = min(blksize, 4);
307 }
308
309 if (host->remain == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310 if (sdhci_next_sg(host) == 0) {
311 BUG_ON(blksize != 0);
312 return;
313 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100314 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315 }
316 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317}
318
319static void sdhci_transfer_pio(struct sdhci_host *host)
320{
321 u32 mask;
322
323 BUG_ON(!host->data);
324
Alex Dubov14d836e2007-04-13 19:04:38 +0200325 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100326 return;
327
328 if (host->data->flags & MMC_DATA_READ)
329 mask = SDHCI_DATA_AVAILABLE;
330 else
331 mask = SDHCI_SPACE_AVAILABLE;
332
333 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
334 if (host->data->flags & MMC_DATA_READ)
335 sdhci_read_block_pio(host);
336 else
337 sdhci_write_block_pio(host);
338
Alex Dubov14d836e2007-04-13 19:04:38 +0200339 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100340 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100341 }
342
343 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800344}
345
346static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
347{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700348 u8 count;
349 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350
351 WARN_ON(host->data);
352
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700353 if (data == NULL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800355
356 DBG("blksz %04x blks %04x flags %08x\n",
Russell Kinga3fd4a12006-06-04 17:51:15 +0100357 data->blksz, data->blocks, data->flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800358 DBG("tsac %d ms nsac %d clk\n",
359 data->timeout_ns / 1000000, data->timeout_clks);
360
Pierre Ossmanbab76962006-07-02 16:51:35 +0100361 /* Sanity checks */
362 BUG_ON(data->blksz * data->blocks > 524288);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100363 BUG_ON(data->blksz > host->mmc->max_blk_size);
Pierre Ossman1d676e02006-07-02 16:52:10 +0100364 BUG_ON(data->blocks > 65535);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800365
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700366 /* timeout in us */
367 target_timeout = data->timeout_ns / 1000 +
368 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800369
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700370 /*
371 * Figure out needed cycles.
372 * We do this in steps in order to fit inside a 32 bit int.
373 * The first step is the minimum timeout, which will have a
374 * minimum resolution of 6 bits:
375 * (1) 2^13*1000 > 2^22,
376 * (2) host->timeout_clk < 2^16
377 * =>
378 * (1) / (2) > 2^6
379 */
380 count = 0;
381 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
382 while (current_timeout < target_timeout) {
383 count++;
384 current_timeout <<= 1;
385 if (count >= 0xF)
386 break;
387 }
388
389 if (count >= 0xF) {
390 printk(KERN_WARNING "%s: Too large timeout requested!\n",
391 mmc_hostname(host->mmc));
392 count = 0xE;
393 }
394
395 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800396
397 if (host->flags & SDHCI_USE_DMA) {
398 int count;
399
400 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
401 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
402 BUG_ON(count != 1);
403
404 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
405 } else {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800406 host->cur_sg = data->sg;
407 host->num_sg = data->sg_len;
408
409 host->offset = 0;
410 host->remain = host->cur_sg->length;
411 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700412
Pierre Ossmanbab76962006-07-02 16:51:35 +0100413 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
414 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
415 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700416 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
417}
418
419static void sdhci_set_transfer_mode(struct sdhci_host *host,
420 struct mmc_data *data)
421{
422 u16 mode;
423
424 WARN_ON(host->data);
425
426 if (data == NULL)
427 return;
428
429 mode = SDHCI_TRNS_BLK_CNT_EN;
430 if (data->blocks > 1)
431 mode |= SDHCI_TRNS_MULTI;
432 if (data->flags & MMC_DATA_READ)
433 mode |= SDHCI_TRNS_READ;
434 if (host->flags & SDHCI_USE_DMA)
435 mode |= SDHCI_TRNS_DMA;
436
437 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800438}
439
440static void sdhci_finish_data(struct sdhci_host *host)
441{
442 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800443 u16 blocks;
444
445 BUG_ON(!host->data);
446
447 data = host->data;
448 host->data = NULL;
449
450 if (host->flags & SDHCI_USE_DMA) {
451 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
452 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800453 }
454
455 /*
456 * Controller doesn't count down when in single block mode.
457 */
458 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
459 blocks = 0;
460 else
461 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
Russell Kinga3fd4a12006-06-04 17:51:15 +0100462 data->bytes_xfered = data->blksz * (data->blocks - blocks);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800463
464 if ((data->error == MMC_ERR_NONE) && blocks) {
465 printk(KERN_ERR "%s: Controller signalled completion even "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100466 "though there were blocks left.\n",
467 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800468 data->error = MMC_ERR_FAILED;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800469 }
470
471 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
472
473 if (data->stop) {
474 /*
475 * The controller needs a reset of internal state machines
476 * upon error conditions.
477 */
478 if (data->error != MMC_ERR_NONE) {
479 sdhci_reset(host, SDHCI_RESET_CMD);
480 sdhci_reset(host, SDHCI_RESET_DATA);
481 }
482
483 sdhci_send_command(host, data->stop);
484 } else
485 tasklet_schedule(&host->finish_tasklet);
486}
487
488static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
489{
490 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700491 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700492 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800493
494 WARN_ON(host->cmd);
495
496 DBG("Sending cmd (%x)\n", cmd->opcode);
497
498 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700499 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700500
501 mask = SDHCI_CMD_INHIBIT;
502 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
503 mask |= SDHCI_DATA_INHIBIT;
504
505 /* We shouldn't wait for data inihibit for stop commands, even
506 though they might use busy signaling */
507 if (host->mrq->data && (cmd == host->mrq->data->stop))
508 mask &= ~SDHCI_DATA_INHIBIT;
509
510 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700511 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800512 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100513 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800514 sdhci_dumpregs(host);
515 cmd->error = MMC_ERR_FAILED;
516 tasklet_schedule(&host->finish_tasklet);
517 return;
518 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700519 timeout--;
520 mdelay(1);
521 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800522
523 mod_timer(&host->timer, jiffies + 10 * HZ);
524
525 host->cmd = cmd;
526
527 sdhci_prepare_data(host, cmd->data);
528
529 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
530
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700531 sdhci_set_transfer_mode(host, cmd->data);
532
Pierre Ossmand129bce2006-03-24 03:18:17 -0800533 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100534 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800535 mmc_hostname(host->mmc));
536 cmd->error = MMC_ERR_INVALID;
537 tasklet_schedule(&host->finish_tasklet);
538 return;
539 }
540
541 if (!(cmd->flags & MMC_RSP_PRESENT))
542 flags = SDHCI_CMD_RESP_NONE;
543 else if (cmd->flags & MMC_RSP_136)
544 flags = SDHCI_CMD_RESP_LONG;
545 else if (cmd->flags & MMC_RSP_BUSY)
546 flags = SDHCI_CMD_RESP_SHORT_BUSY;
547 else
548 flags = SDHCI_CMD_RESP_SHORT;
549
550 if (cmd->flags & MMC_RSP_CRC)
551 flags |= SDHCI_CMD_CRC;
552 if (cmd->flags & MMC_RSP_OPCODE)
553 flags |= SDHCI_CMD_INDEX;
554 if (cmd->data)
555 flags |= SDHCI_CMD_DATA;
556
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200557 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800558 host->ioaddr + SDHCI_COMMAND);
559}
560
561static void sdhci_finish_command(struct sdhci_host *host)
562{
563 int i;
564
565 BUG_ON(host->cmd == NULL);
566
567 if (host->cmd->flags & MMC_RSP_PRESENT) {
568 if (host->cmd->flags & MMC_RSP_136) {
569 /* CRC is stripped so we need to do some shifting. */
570 for (i = 0;i < 4;i++) {
571 host->cmd->resp[i] = readl(host->ioaddr +
572 SDHCI_RESPONSE + (3-i)*4) << 8;
573 if (i != 3)
574 host->cmd->resp[i] |=
575 readb(host->ioaddr +
576 SDHCI_RESPONSE + (3-i)*4-1);
577 }
578 } else {
579 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
580 }
581 }
582
583 host->cmd->error = MMC_ERR_NONE;
584
585 DBG("Ending cmd (%x)\n", host->cmd->opcode);
586
Pierre Ossman3192a282006-06-30 02:22:26 -0700587 if (host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800588 host->data = host->cmd->data;
Pierre Ossman3192a282006-06-30 02:22:26 -0700589 else
Pierre Ossmand129bce2006-03-24 03:18:17 -0800590 tasklet_schedule(&host->finish_tasklet);
591
592 host->cmd = NULL;
593}
594
595static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
596{
597 int div;
598 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700599 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800600
601 if (clock == host->clock)
602 return;
603
604 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
605
606 if (clock == 0)
607 goto out;
608
609 for (div = 1;div < 256;div *= 2) {
610 if ((host->max_clk / div) <= clock)
611 break;
612 }
613 div >>= 1;
614
615 clk = div << SDHCI_DIVIDER_SHIFT;
616 clk |= SDHCI_CLOCK_INT_EN;
617 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
618
619 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700620 timeout = 10;
621 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
622 & SDHCI_CLOCK_INT_STABLE)) {
623 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100624 printk(KERN_ERR "%s: Internal clock never "
625 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800626 sdhci_dumpregs(host);
627 return;
628 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700629 timeout--;
630 mdelay(1);
631 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800632
633 clk |= SDHCI_CLOCK_CARD_EN;
634 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
635
636out:
637 host->clock = clock;
638}
639
Pierre Ossman146ad662006-06-30 02:22:23 -0700640static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
641{
642 u8 pwr;
643
644 if (host->power == power)
645 return;
646
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100647 if (power == (unsigned short)-1) {
648 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700649 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100650 }
651
652 /*
653 * Spec says that we should clear the power reg before setting
654 * a new value. Some controllers don't seem to like this though.
655 */
656 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
657 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700658
659 pwr = SDHCI_POWER_ON;
660
Philip Langdale4be34c92007-03-11 17:15:15 -0700661 switch (1 << power) {
662 case MMC_VDD_17_18:
663 case MMC_VDD_18_19:
Pierre Ossman146ad662006-06-30 02:22:23 -0700664 pwr |= SDHCI_POWER_180;
665 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700666 case MMC_VDD_29_30:
667 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700668 pwr |= SDHCI_POWER_300;
669 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700670 case MMC_VDD_32_33:
671 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700672 pwr |= SDHCI_POWER_330;
673 break;
674 default:
675 BUG();
676 }
677
678 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
679
680out:
681 host->power = power;
682}
683
Pierre Ossmand129bce2006-03-24 03:18:17 -0800684/*****************************************************************************\
685 * *
686 * MMC callbacks *
687 * *
688\*****************************************************************************/
689
690static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
691{
692 struct sdhci_host *host;
693 unsigned long flags;
694
695 host = mmc_priv(mmc);
696
697 spin_lock_irqsave(&host->lock, flags);
698
699 WARN_ON(host->mrq != NULL);
700
701 sdhci_activate_led(host);
702
703 host->mrq = mrq;
704
705 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
706 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
707 tasklet_schedule(&host->finish_tasklet);
708 } else
709 sdhci_send_command(host, mrq->cmd);
710
Pierre Ossman5f25a662006-10-04 02:15:39 -0700711 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800712 spin_unlock_irqrestore(&host->lock, flags);
713}
714
715static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
716{
717 struct sdhci_host *host;
718 unsigned long flags;
719 u8 ctrl;
720
721 host = mmc_priv(mmc);
722
723 spin_lock_irqsave(&host->lock, flags);
724
Pierre Ossmand129bce2006-03-24 03:18:17 -0800725 /*
726 * Reset the chip on each power off.
727 * Should clear out any weird states.
728 */
729 if (ios->power_mode == MMC_POWER_OFF) {
730 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800731 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800732 }
733
734 sdhci_set_clock(host, ios->clock);
735
736 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -0700737 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800738 else
Pierre Ossman146ad662006-06-30 02:22:23 -0700739 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800740
741 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100742
Pierre Ossmand129bce2006-03-24 03:18:17 -0800743 if (ios->bus_width == MMC_BUS_WIDTH_4)
744 ctrl |= SDHCI_CTRL_4BITBUS;
745 else
746 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100747
748 if (ios->timing == MMC_TIMING_SD_HS)
749 ctrl |= SDHCI_CTRL_HISPD;
750 else
751 ctrl &= ~SDHCI_CTRL_HISPD;
752
Pierre Ossmand129bce2006-03-24 03:18:17 -0800753 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
754
Pierre Ossman5f25a662006-10-04 02:15:39 -0700755 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800756 spin_unlock_irqrestore(&host->lock, flags);
757}
758
759static int sdhci_get_ro(struct mmc_host *mmc)
760{
761 struct sdhci_host *host;
762 unsigned long flags;
763 int present;
764
765 host = mmc_priv(mmc);
766
767 spin_lock_irqsave(&host->lock, flags);
768
769 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
770
771 spin_unlock_irqrestore(&host->lock, flags);
772
773 return !(present & SDHCI_WRITE_PROTECT);
774}
775
David Brownellab7aefd2006-11-12 17:55:30 -0800776static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800777 .request = sdhci_request,
778 .set_ios = sdhci_set_ios,
779 .get_ro = sdhci_get_ro,
780};
781
782/*****************************************************************************\
783 * *
784 * Tasklets *
785 * *
786\*****************************************************************************/
787
788static void sdhci_tasklet_card(unsigned long param)
789{
790 struct sdhci_host *host;
791 unsigned long flags;
792
793 host = (struct sdhci_host*)param;
794
795 spin_lock_irqsave(&host->lock, flags);
796
797 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
798 if (host->mrq) {
799 printk(KERN_ERR "%s: Card removed during transfer!\n",
800 mmc_hostname(host->mmc));
801 printk(KERN_ERR "%s: Resetting controller.\n",
802 mmc_hostname(host->mmc));
803
804 sdhci_reset(host, SDHCI_RESET_CMD);
805 sdhci_reset(host, SDHCI_RESET_DATA);
806
807 host->mrq->cmd->error = MMC_ERR_FAILED;
808 tasklet_schedule(&host->finish_tasklet);
809 }
810 }
811
812 spin_unlock_irqrestore(&host->lock, flags);
813
814 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
815}
816
817static void sdhci_tasklet_finish(unsigned long param)
818{
819 struct sdhci_host *host;
820 unsigned long flags;
821 struct mmc_request *mrq;
822
823 host = (struct sdhci_host*)param;
824
825 spin_lock_irqsave(&host->lock, flags);
826
827 del_timer(&host->timer);
828
829 mrq = host->mrq;
830
831 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
832
833 /*
834 * The controller needs a reset of internal state machines
835 * upon error conditions.
836 */
837 if ((mrq->cmd->error != MMC_ERR_NONE) ||
838 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
839 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
Pierre Ossman645289d2006-06-30 02:22:33 -0700840
841 /* Some controllers need this kick or reset won't work here */
842 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
843 unsigned int clock;
844
845 /* This is to force an update */
846 clock = host->clock;
847 host->clock = 0;
848 sdhci_set_clock(host, clock);
849 }
850
851 /* Spec says we should do both at the same time, but Ricoh
852 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800853 sdhci_reset(host, SDHCI_RESET_CMD);
854 sdhci_reset(host, SDHCI_RESET_DATA);
855 }
856
857 host->mrq = NULL;
858 host->cmd = NULL;
859 host->data = NULL;
860
861 sdhci_deactivate_led(host);
862
Pierre Ossman5f25a662006-10-04 02:15:39 -0700863 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800864 spin_unlock_irqrestore(&host->lock, flags);
865
866 mmc_request_done(host->mmc, mrq);
867}
868
869static void sdhci_timeout_timer(unsigned long data)
870{
871 struct sdhci_host *host;
872 unsigned long flags;
873
874 host = (struct sdhci_host*)data;
875
876 spin_lock_irqsave(&host->lock, flags);
877
878 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100879 printk(KERN_ERR "%s: Timeout waiting for hardware "
880 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800881 sdhci_dumpregs(host);
882
883 if (host->data) {
884 host->data->error = MMC_ERR_TIMEOUT;
885 sdhci_finish_data(host);
886 } else {
887 if (host->cmd)
888 host->cmd->error = MMC_ERR_TIMEOUT;
889 else
890 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
891
892 tasklet_schedule(&host->finish_tasklet);
893 }
894 }
895
Pierre Ossman5f25a662006-10-04 02:15:39 -0700896 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800897 spin_unlock_irqrestore(&host->lock, flags);
898}
899
900/*****************************************************************************\
901 * *
902 * Interrupt handling *
903 * *
904\*****************************************************************************/
905
906static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
907{
908 BUG_ON(intmask == 0);
909
910 if (!host->cmd) {
911 printk(KERN_ERR "%s: Got command interrupt even though no "
912 "command operation was in progress.\n",
913 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914 sdhci_dumpregs(host);
915 return;
916 }
917
918 if (intmask & SDHCI_INT_RESPONSE)
919 sdhci_finish_command(host);
920 else {
921 if (intmask & SDHCI_INT_TIMEOUT)
922 host->cmd->error = MMC_ERR_TIMEOUT;
923 else if (intmask & SDHCI_INT_CRC)
924 host->cmd->error = MMC_ERR_BADCRC;
925 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
926 host->cmd->error = MMC_ERR_FAILED;
927 else
928 host->cmd->error = MMC_ERR_INVALID;
929
930 tasklet_schedule(&host->finish_tasklet);
931 }
932}
933
934static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
935{
936 BUG_ON(intmask == 0);
937
938 if (!host->data) {
939 /*
940 * A data end interrupt is sent together with the response
941 * for the stop command.
942 */
943 if (intmask & SDHCI_INT_DATA_END)
944 return;
945
946 printk(KERN_ERR "%s: Got data interrupt even though no "
947 "data operation was in progress.\n",
948 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 sdhci_dumpregs(host);
950
951 return;
952 }
953
954 if (intmask & SDHCI_INT_DATA_TIMEOUT)
955 host->data->error = MMC_ERR_TIMEOUT;
956 else if (intmask & SDHCI_INT_DATA_CRC)
957 host->data->error = MMC_ERR_BADCRC;
958 else if (intmask & SDHCI_INT_DATA_END_BIT)
959 host->data->error = MMC_ERR_FAILED;
960
961 if (host->data->error != MMC_ERR_NONE)
962 sdhci_finish_data(host);
963 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100964 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 sdhci_transfer_pio(host);
966
967 if (intmask & SDHCI_INT_DATA_END)
968 sdhci_finish_data(host);
969 }
970}
971
David Howells7d12e782006-10-05 14:55:46 +0100972static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973{
974 irqreturn_t result;
975 struct sdhci_host* host = dev_id;
976 u32 intmask;
977
978 spin_lock(&host->lock);
979
980 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
981
Mark Lord62df67a2007-03-06 13:30:13 +0100982 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 result = IRQ_NONE;
984 goto out;
985 }
986
987 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
988
Pierre Ossman3192a282006-06-30 02:22:26 -0700989 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
990 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
991 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -0700993 }
994
995 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996
997 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 writel(intmask & SDHCI_INT_CMD_MASK,
999 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001000 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001 }
1002
1003 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 writel(intmask & SDHCI_INT_DATA_MASK,
1005 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001006 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 }
1008
1009 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1010
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001012 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001014 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 }
1016
Pierre Ossman3192a282006-06-30 02:22:26 -07001017 intmask &= SDHCI_INT_BUS_POWER;
1018
1019 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001020 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001021 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 sdhci_dumpregs(host);
1023
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001025 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
1027 result = IRQ_HANDLED;
1028
Pierre Ossman5f25a662006-10-04 02:15:39 -07001029 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030out:
1031 spin_unlock(&host->lock);
1032
1033 return result;
1034}
1035
1036/*****************************************************************************\
1037 * *
1038 * Suspend/resume *
1039 * *
1040\*****************************************************************************/
1041
1042#ifdef CONFIG_PM
1043
1044static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1045{
1046 struct sdhci_chip *chip;
1047 int i, ret;
1048
1049 chip = pci_get_drvdata(pdev);
1050 if (!chip)
1051 return 0;
1052
1053 DBG("Suspending...\n");
1054
1055 for (i = 0;i < chip->num_slots;i++) {
1056 if (!chip->hosts[i])
1057 continue;
1058 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1059 if (ret) {
1060 for (i--;i >= 0;i--)
1061 mmc_resume_host(chip->hosts[i]->mmc);
1062 return ret;
1063 }
1064 }
1065
1066 pci_save_state(pdev);
1067 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001068
1069 for (i = 0;i < chip->num_slots;i++) {
1070 if (!chip->hosts[i])
1071 continue;
1072 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1073 }
1074
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075 pci_disable_device(pdev);
1076 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1077
1078 return 0;
1079}
1080
1081static int sdhci_resume (struct pci_dev *pdev)
1082{
1083 struct sdhci_chip *chip;
1084 int i, ret;
1085
1086 chip = pci_get_drvdata(pdev);
1087 if (!chip)
1088 return 0;
1089
1090 DBG("Resuming...\n");
1091
1092 pci_set_power_state(pdev, PCI_D0);
1093 pci_restore_state(pdev);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001094 ret = pci_enable_device(pdev);
1095 if (ret)
1096 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097
1098 for (i = 0;i < chip->num_slots;i++) {
1099 if (!chip->hosts[i])
1100 continue;
1101 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1102 pci_set_master(pdev);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001103 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
1104 IRQF_SHARED, chip->hosts[i]->slot_descr,
1105 chip->hosts[i]);
1106 if (ret)
1107 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001108 sdhci_init(chip->hosts[i]);
Pierre Ossman5f25a662006-10-04 02:15:39 -07001109 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110 ret = mmc_resume_host(chip->hosts[i]->mmc);
1111 if (ret)
1112 return ret;
1113 }
1114
1115 return 0;
1116}
1117
1118#else /* CONFIG_PM */
1119
1120#define sdhci_suspend NULL
1121#define sdhci_resume NULL
1122
1123#endif /* CONFIG_PM */
1124
1125/*****************************************************************************\
1126 * *
1127 * Device probing/removal *
1128 * *
1129\*****************************************************************************/
1130
1131static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1132{
1133 int ret;
Pierre Ossman4a965502006-06-30 02:22:29 -07001134 unsigned int version;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135 struct sdhci_chip *chip;
1136 struct mmc_host *mmc;
1137 struct sdhci_host *host;
1138
1139 u8 first_bar;
1140 unsigned int caps;
1141
1142 chip = pci_get_drvdata(pdev);
1143 BUG_ON(!chip);
1144
1145 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1146 if (ret)
1147 return ret;
1148
1149 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1150
1151 if (first_bar > 5) {
1152 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1153 return -ENODEV;
1154 }
1155
1156 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1157 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1158 return -ENODEV;
1159 }
1160
1161 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
Pierre Ossmana98087c2006-12-07 19:17:20 +01001162 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1163 "You may experience problems.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164 }
1165
Pierre Ossman67435272006-06-30 02:22:31 -07001166 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1167 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1168 return -ENODEV;
1169 }
1170
1171 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1172 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1173 return -ENODEV;
1174 }
1175
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1177 if (!mmc)
1178 return -ENOMEM;
1179
1180 host = mmc_priv(mmc);
1181 host->mmc = mmc;
1182
Pierre Ossman8a4da142006-10-04 02:15:40 -07001183 host->chip = chip;
1184 chip->hosts[slot] = host;
1185
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 host->bar = first_bar + slot;
1187
1188 host->addr = pci_resource_start(pdev, host->bar);
1189 host->irq = pdev->irq;
1190
1191 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1192
1193 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1194
1195 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1196 if (ret)
1197 goto free;
1198
1199 host->ioaddr = ioremap_nocache(host->addr,
1200 pci_resource_len(pdev, host->bar));
1201 if (!host->ioaddr) {
1202 ret = -ENOMEM;
1203 goto release;
1204 }
1205
Pierre Ossmand96649e2006-06-30 02:22:30 -07001206 sdhci_reset(host, SDHCI_RESET_ALL);
1207
Pierre Ossman4a965502006-06-30 02:22:29 -07001208 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1209 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1210 if (version != 0) {
1211 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossman8b1b2182006-07-11 21:07:10 +02001212 "You may experience problems.\n", host->slot_descr,
Pierre Ossman4a965502006-06-30 02:22:29 -07001213 version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001214 }
1215
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1217
Pierre Ossman67435272006-06-30 02:22:31 -07001218 if (debug_nodma)
1219 DBG("DMA forced off\n");
1220 else if (debug_forcedma) {
1221 DBG("DMA forced on\n");
1222 host->flags |= SDHCI_USE_DMA;
Pierre Ossman98608072006-06-30 02:22:34 -07001223 } else if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1224 host->flags |= SDHCI_USE_DMA;
1225 else if ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA)
Pierre Ossman67435272006-06-30 02:22:31 -07001226 DBG("Controller doesn't have DMA interface\n");
1227 else if (!(caps & SDHCI_CAN_DO_DMA))
1228 DBG("Controller doesn't have DMA capability\n");
1229 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 host->flags |= SDHCI_USE_DMA;
1231
1232 if (host->flags & SDHCI_USE_DMA) {
1233 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1234 printk(KERN_WARNING "%s: No suitable DMA available. "
1235 "Falling back to PIO.\n", host->slot_descr);
1236 host->flags &= ~SDHCI_USE_DMA;
1237 }
1238 }
1239
1240 if (host->flags & SDHCI_USE_DMA)
1241 pci_set_master(pdev);
1242 else /* XXX: Hack to get MMC layer to avoid highmem */
1243 pdev->dma_mask = 0;
1244
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001245 host->max_clk =
1246 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1247 if (host->max_clk == 0) {
1248 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1249 "frequency.\n", host->slot_descr);
1250 ret = -ENODEV;
1251 goto unmap;
1252 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 host->max_clk *= 1000000;
1254
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001255 host->timeout_clk =
1256 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1257 if (host->timeout_clk == 0) {
1258 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1259 "frequency.\n", host->slot_descr);
1260 ret = -ENODEV;
1261 goto unmap;
1262 }
1263 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1264 host->timeout_clk *= 1000;
1265
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266 /*
1267 * Set host parameters.
1268 */
1269 mmc->ops = &sdhci_ops;
1270 mmc->f_min = host->max_clk / 256;
1271 mmc->f_max = host->max_clk;
Russell King42431ac2006-09-24 10:44:09 +01001272 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001274 if (caps & SDHCI_CAN_DO_HISPD)
1275 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1276
Pierre Ossman146ad662006-06-30 02:22:23 -07001277 mmc->ocr_avail = 0;
1278 if (caps & SDHCI_CAN_VDD_330)
1279 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001280 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001281 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001282 if (caps & SDHCI_CAN_VDD_180)
Pierre Ossman146ad662006-06-30 02:22:23 -07001283 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1284
1285 if (mmc->ocr_avail == 0) {
1286 printk(KERN_ERR "%s: Hardware doesn't report any "
1287 "support voltages.\n", host->slot_descr);
1288 ret = -ENODEV;
1289 goto unmap;
1290 }
1291
Pierre Ossmand129bce2006-03-24 03:18:17 -08001292 spin_lock_init(&host->lock);
1293
1294 /*
1295 * Maximum number of segments. Hardware cannot do scatter lists.
1296 */
1297 if (host->flags & SDHCI_USE_DMA)
1298 mmc->max_hw_segs = 1;
1299 else
1300 mmc->max_hw_segs = 16;
1301 mmc->max_phys_segs = 16;
1302
1303 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001304 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001305 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001307 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308
1309 /*
1310 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001311 * of bytes.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001313 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314
1315 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001316 * Maximum block size. This varies from controller to controller and
1317 * is specified in the capabilities register.
1318 */
1319 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1320 if (mmc->max_blk_size >= 3) {
1321 printk(KERN_ERR "%s: Invalid maximum block size.\n",
1322 host->slot_descr);
1323 ret = -ENODEV;
1324 goto unmap;
1325 }
1326 mmc->max_blk_size = 512 << mmc->max_blk_size;
1327
1328 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001329 * Maximum block count.
1330 */
1331 mmc->max_blk_count = 65535;
1332
1333 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 * Init tasklets.
1335 */
1336 tasklet_init(&host->card_tasklet,
1337 sdhci_tasklet_card, (unsigned long)host);
1338 tasklet_init(&host->finish_tasklet,
1339 sdhci_tasklet_finish, (unsigned long)host);
1340
Al Viroe4cad1b2006-10-10 22:47:07 +01001341 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001342
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001343 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 host->slot_descr, host);
1345 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001346 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347
1348 sdhci_init(host);
1349
1350#ifdef CONFIG_MMC_DEBUG
1351 sdhci_dumpregs(host);
1352#endif
1353
Pierre Ossman5f25a662006-10-04 02:15:39 -07001354 mmiowb();
1355
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356 mmc_add_host(mmc);
1357
1358 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1359 host->addr, host->irq,
1360 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1361
1362 return 0;
1363
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001364untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365 tasklet_kill(&host->card_tasklet);
1366 tasklet_kill(&host->finish_tasklet);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001367unmap:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 iounmap(host->ioaddr);
1369release:
1370 pci_release_region(pdev, host->bar);
1371free:
1372 mmc_free_host(mmc);
1373
1374 return ret;
1375}
1376
1377static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1378{
1379 struct sdhci_chip *chip;
1380 struct mmc_host *mmc;
1381 struct sdhci_host *host;
1382
1383 chip = pci_get_drvdata(pdev);
1384 host = chip->hosts[slot];
1385 mmc = host->mmc;
1386
1387 chip->hosts[slot] = NULL;
1388
1389 mmc_remove_host(mmc);
1390
1391 sdhci_reset(host, SDHCI_RESET_ALL);
1392
1393 free_irq(host->irq, host);
1394
1395 del_timer_sync(&host->timer);
1396
1397 tasklet_kill(&host->card_tasklet);
1398 tasklet_kill(&host->finish_tasklet);
1399
1400 iounmap(host->ioaddr);
1401
1402 pci_release_region(pdev, host->bar);
1403
1404 mmc_free_host(mmc);
1405}
1406
1407static int __devinit sdhci_probe(struct pci_dev *pdev,
1408 const struct pci_device_id *ent)
1409{
1410 int ret, i;
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001411 u8 slots, rev;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412 struct sdhci_chip *chip;
1413
1414 BUG_ON(pdev == NULL);
1415 BUG_ON(ent == NULL);
1416
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001417 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1418
1419 printk(KERN_INFO DRIVER_NAME
1420 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1421 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1422 (int)rev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001423
1424 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1425 if (ret)
1426 return ret;
1427
1428 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1429 DBG("found %d slot(s)\n", slots);
1430 if (slots == 0)
1431 return -ENODEV;
1432
1433 ret = pci_enable_device(pdev);
1434 if (ret)
1435 return ret;
1436
1437 chip = kzalloc(sizeof(struct sdhci_chip) +
1438 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1439 if (!chip) {
1440 ret = -ENOMEM;
1441 goto err;
1442 }
1443
1444 chip->pdev = pdev;
Pierre Ossmandf673b22006-06-30 02:22:31 -07001445 chip->quirks = ent->driver_data;
1446
1447 if (debug_quirks)
1448 chip->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449
1450 chip->num_slots = slots;
1451 pci_set_drvdata(pdev, chip);
1452
1453 for (i = 0;i < slots;i++) {
1454 ret = sdhci_probe_slot(pdev, i);
1455 if (ret) {
1456 for (i--;i >= 0;i--)
1457 sdhci_remove_slot(pdev, i);
1458 goto free;
1459 }
1460 }
1461
1462 return 0;
1463
1464free:
1465 pci_set_drvdata(pdev, NULL);
1466 kfree(chip);
1467
1468err:
1469 pci_disable_device(pdev);
1470 return ret;
1471}
1472
1473static void __devexit sdhci_remove(struct pci_dev *pdev)
1474{
1475 int i;
1476 struct sdhci_chip *chip;
1477
1478 chip = pci_get_drvdata(pdev);
1479
1480 if (chip) {
1481 for (i = 0;i < chip->num_slots;i++)
1482 sdhci_remove_slot(pdev, i);
1483
1484 pci_set_drvdata(pdev, NULL);
1485
1486 kfree(chip);
1487 }
1488
1489 pci_disable_device(pdev);
1490}
1491
1492static struct pci_driver sdhci_driver = {
1493 .name = DRIVER_NAME,
1494 .id_table = pci_ids,
1495 .probe = sdhci_probe,
1496 .remove = __devexit_p(sdhci_remove),
1497 .suspend = sdhci_suspend,
1498 .resume = sdhci_resume,
1499};
1500
1501/*****************************************************************************\
1502 * *
1503 * Driver init/exit *
1504 * *
1505\*****************************************************************************/
1506
1507static int __init sdhci_drv_init(void)
1508{
1509 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001510 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1512
1513 return pci_register_driver(&sdhci_driver);
1514}
1515
1516static void __exit sdhci_drv_exit(void)
1517{
1518 DBG("Exiting\n");
1519
1520 pci_unregister_driver(&sdhci_driver);
1521}
1522
1523module_init(sdhci_drv_init);
1524module_exit(sdhci_drv_exit);
1525
Pierre Ossman67435272006-06-30 02:22:31 -07001526module_param(debug_nodma, uint, 0444);
1527module_param(debug_forcedma, uint, 0444);
Pierre Ossmandf673b22006-06-30 02:22:31 -07001528module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001529
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1531MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001532MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001533
1534MODULE_PARM_DESC(debug_nodma, "Forcefully disable DMA transfers. (default 0)");
1535MODULE_PARM_DESC(debug_forcedma, "Forcefully enable DMA transfers. (default 0)");
Pierre Ossmandf673b22006-06-30 02:22:31 -07001536MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");