blob: 17701c3da73326b9fa796086a617b253c506b1e5 [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 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 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 Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019#include <linux/dma-mapping.h>
Ralf Baechle11763602007-10-23 20:42:11 +020020#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021
Pierre Ossman2f730fe2008-03-17 10:29:38 +010022#include <linux/leds.h>
23
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include "sdhci.h"
27
28#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010031 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmandf673b22006-06-30 02:22:31 -070033static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
36static void sdhci_finish_data(struct sdhci_host *);
37
38static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
39static void sdhci_finish_command(struct sdhci_host *);
40
41static void sdhci_dumpregs(struct sdhci_host *host)
42{
43 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
44
45 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
46 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
47 readw(host->ioaddr + SDHCI_HOST_VERSION));
48 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
49 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
50 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
51 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
52 readl(host->ioaddr + SDHCI_ARGUMENT),
53 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
54 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
55 readl(host->ioaddr + SDHCI_PRESENT_STATE),
56 readb(host->ioaddr + SDHCI_HOST_CONTROL));
57 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
58 readb(host->ioaddr + SDHCI_POWER_CONTROL),
59 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
60 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Nicolas Pitre2df3b712007-09-29 10:46:20 -040061 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
63 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
64 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
65 readl(host->ioaddr + SDHCI_INT_STATUS));
66 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
67 readl(host->ioaddr + SDHCI_INT_ENABLE),
68 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
69 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
70 readw(host->ioaddr + SDHCI_ACMD12_ERR),
71 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
72 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
73 readl(host->ioaddr + SDHCI_CAPABILITIES),
74 readl(host->ioaddr + SDHCI_MAX_CURRENT));
75
76 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
77}
78
79/*****************************************************************************\
80 * *
81 * Low level functions *
82 * *
83\*****************************************************************************/
84
85static void sdhci_reset(struct sdhci_host *host, u8 mask)
86{
Pierre Ossmane16514d82006-06-30 02:22:24 -070087 unsigned long timeout;
88
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010089 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Pierre Ossman8a4da142006-10-04 02:15:40 -070090 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
91 SDHCI_CARD_PRESENT))
92 return;
93 }
94
Pierre Ossmand129bce2006-03-24 03:18:17 -080095 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
96
Pierre Ossmane16514d82006-06-30 02:22:24 -070097 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -080098 host->clock = 0;
99
Pierre Ossmane16514d82006-06-30 02:22:24 -0700100 /* Wait max 100 ms */
101 timeout = 100;
102
103 /* hw clears the bit when it's done */
104 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
105 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100106 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700107 mmc_hostname(host->mmc), (int)mask);
108 sdhci_dumpregs(host);
109 return;
110 }
111 timeout--;
112 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800113 }
114}
115
116static void sdhci_init(struct sdhci_host *host)
117{
118 u32 intmask;
119
120 sdhci_reset(host, SDHCI_RESET_ALL);
121
Pierre Ossman3192a282006-06-30 02:22:26 -0700122 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
123 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
124 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
125 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100126 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
Pierre Ossman2134a922008-06-28 18:28:51 +0200127 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
128 SDHCI_INT_ADMA_ERROR;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800129
130 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
131 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800132}
133
134static void sdhci_activate_led(struct sdhci_host *host)
135{
136 u8 ctrl;
137
138 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
139 ctrl |= SDHCI_CTRL_LED;
140 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
141}
142
143static void sdhci_deactivate_led(struct sdhci_host *host)
144{
145 u8 ctrl;
146
147 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
148 ctrl &= ~SDHCI_CTRL_LED;
149 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
150}
151
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100152#ifdef CONFIG_LEDS_CLASS
153static void sdhci_led_control(struct led_classdev *led,
154 enum led_brightness brightness)
155{
156 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
157 unsigned long flags;
158
159 spin_lock_irqsave(&host->lock, flags);
160
161 if (brightness == LED_OFF)
162 sdhci_deactivate_led(host);
163 else
164 sdhci_activate_led(host);
165
166 spin_unlock_irqrestore(&host->lock, flags);
167}
168#endif
169
Pierre Ossmand129bce2006-03-24 03:18:17 -0800170/*****************************************************************************\
171 * *
172 * Core functions *
173 * *
174\*****************************************************************************/
175
Pierre Ossman2a22b142007-02-02 18:27:42 +0100176static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177{
Jens Axboe45711f12007-10-22 21:19:53 +0200178 return sg_virt(host->cur_sg);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179}
180
181static inline int sdhci_next_sg(struct sdhci_host* host)
182{
183 /*
184 * Skip to next SG entry.
185 */
186 host->cur_sg++;
187 host->num_sg--;
188
189 /*
190 * Any entries left?
191 */
192 if (host->num_sg > 0) {
193 host->offset = 0;
194 host->remain = host->cur_sg->length;
195 }
196
197 return host->num_sg;
198}
199
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100200static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201{
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100202 int blksize, chunk_remain;
203 u32 data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800204 char *buffer;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100205 int size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800206
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100207 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100209 blksize = host->data->blksz;
210 chunk_remain = 0;
211 data = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212
Pierre Ossman2a22b142007-02-02 18:27:42 +0100213 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800214
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100215 while (blksize) {
216 if (chunk_remain == 0) {
217 data = readl(host->ioaddr + SDHCI_BUFFER);
218 chunk_remain = min(blksize, 4);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219 }
220
Alex Dubov14d836e2007-04-13 19:04:38 +0200221 size = min(host->remain, chunk_remain);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100223 chunk_remain -= size;
224 blksize -= size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225 host->offset += size;
226 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200227
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100228 while (size) {
229 *buffer = data & 0xFF;
230 buffer++;
231 data >>= 8;
232 size--;
233 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
235 if (host->remain == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236 if (sdhci_next_sg(host) == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100237 BUG_ON(blksize != 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238 return;
239 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100240 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241 }
242 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100243}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100245static void sdhci_write_block_pio(struct sdhci_host *host)
246{
247 int blksize, chunk_remain;
248 u32 data;
249 char *buffer;
250 int bytes, size;
251
252 DBG("PIO writing\n");
253
254 blksize = host->data->blksz;
255 chunk_remain = 4;
256 data = 0;
257
258 bytes = 0;
Pierre Ossman2a22b142007-02-02 18:27:42 +0100259 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100260
261 while (blksize) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200262 size = min(host->remain, chunk_remain);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100263
264 chunk_remain -= size;
265 blksize -= size;
266 host->offset += size;
267 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200268
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100269 while (size) {
270 data >>= 8;
271 data |= (u32)*buffer << 24;
272 buffer++;
273 size--;
274 }
275
276 if (chunk_remain == 0) {
277 writel(data, host->ioaddr + SDHCI_BUFFER);
278 chunk_remain = min(blksize, 4);
279 }
280
281 if (host->remain == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100282 if (sdhci_next_sg(host) == 0) {
283 BUG_ON(blksize != 0);
284 return;
285 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100286 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100287 }
288 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100289}
290
291static void sdhci_transfer_pio(struct sdhci_host *host)
292{
293 u32 mask;
294
295 BUG_ON(!host->data);
296
Alex Dubov14d836e2007-04-13 19:04:38 +0200297 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100298 return;
299
300 if (host->data->flags & MMC_DATA_READ)
301 mask = SDHCI_DATA_AVAILABLE;
302 else
303 mask = SDHCI_SPACE_AVAILABLE;
304
305 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
306 if (host->data->flags & MMC_DATA_READ)
307 sdhci_read_block_pio(host);
308 else
309 sdhci_write_block_pio(host);
310
Alex Dubov14d836e2007-04-13 19:04:38 +0200311 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 }
314
315 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316}
317
Pierre Ossman2134a922008-06-28 18:28:51 +0200318static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
319{
320 local_irq_save(*flags);
321 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
322}
323
324static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
325{
326 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
327 local_irq_restore(*flags);
328}
329
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200330static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200331 struct mmc_data *data)
332{
333 int direction;
334
335 u8 *desc;
336 u8 *align;
337 dma_addr_t addr;
338 dma_addr_t align_addr;
339 int len, offset;
340
341 struct scatterlist *sg;
342 int i;
343 char *buffer;
344 unsigned long flags;
345
346 /*
347 * The spec does not specify endianness of descriptor table.
348 * We currently guess that it is LE.
349 */
350
351 if (data->flags & MMC_DATA_READ)
352 direction = DMA_FROM_DEVICE;
353 else
354 direction = DMA_TO_DEVICE;
355
356 /*
357 * The ADMA descriptor table is mapped further down as we
358 * need to fill it with data first.
359 */
360
361 host->align_addr = dma_map_single(mmc_dev(host->mmc),
362 host->align_buffer, 128 * 4, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200363 if (dma_mapping_error(host->align_addr))
364 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200365 BUG_ON(host->align_addr & 0x3);
366
367 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
368 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200369 if (host->sg_count == 0)
370 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200371
372 desc = host->adma_desc;
373 align = host->align_buffer;
374
375 align_addr = host->align_addr;
376
377 for_each_sg(data->sg, sg, host->sg_count, i) {
378 addr = sg_dma_address(sg);
379 len = sg_dma_len(sg);
380
381 /*
382 * The SDHCI specification states that ADMA
383 * addresses must be 32-bit aligned. If they
384 * aren't, then we use a bounce buffer for
385 * the (up to three) bytes that screw up the
386 * alignment.
387 */
388 offset = (4 - (addr & 0x3)) & 0x3;
389 if (offset) {
390 if (data->flags & MMC_DATA_WRITE) {
391 buffer = sdhci_kmap_atomic(sg, &flags);
392 memcpy(align, buffer, offset);
393 sdhci_kunmap_atomic(buffer, &flags);
394 }
395
396 desc[7] = (align_addr >> 24) & 0xff;
397 desc[6] = (align_addr >> 16) & 0xff;
398 desc[5] = (align_addr >> 8) & 0xff;
399 desc[4] = (align_addr >> 0) & 0xff;
400
401 BUG_ON(offset > 65536);
402
403 desc[3] = (offset >> 8) & 0xff;
404 desc[2] = (offset >> 0) & 0xff;
405
406 desc[1] = 0x00;
407 desc[0] = 0x21; /* tran, valid */
408
409 align += 4;
410 align_addr += 4;
411
412 desc += 8;
413
414 addr += offset;
415 len -= offset;
416 }
417
418 desc[7] = (addr >> 24) & 0xff;
419 desc[6] = (addr >> 16) & 0xff;
420 desc[5] = (addr >> 8) & 0xff;
421 desc[4] = (addr >> 0) & 0xff;
422
423 BUG_ON(len > 65536);
424
425 desc[3] = (len >> 8) & 0xff;
426 desc[2] = (len >> 0) & 0xff;
427
428 desc[1] = 0x00;
429 desc[0] = 0x21; /* tran, valid */
430
431 desc += 8;
432
433 /*
434 * If this triggers then we have a calculation bug
435 * somewhere. :/
436 */
437 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
438 }
439
440 /*
441 * Add a terminating entry.
442 */
443 desc[7] = 0;
444 desc[6] = 0;
445 desc[5] = 0;
446 desc[4] = 0;
447
448 desc[3] = 0;
449 desc[2] = 0;
450
451 desc[1] = 0x00;
452 desc[0] = 0x03; /* nop, end, valid */
453
454 /*
455 * Resync align buffer as we might have changed it.
456 */
457 if (data->flags & MMC_DATA_WRITE) {
458 dma_sync_single_for_device(mmc_dev(host->mmc),
459 host->align_addr, 128 * 4, direction);
460 }
461
462 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
463 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200464 if (dma_mapping_error(host->align_addr))
465 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200466 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200467
468 return 0;
469
470unmap_entries:
471 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
472 data->sg_len, direction);
473unmap_align:
474 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
475 128 * 4, direction);
476fail:
477 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200478}
479
480static void sdhci_adma_table_post(struct sdhci_host *host,
481 struct mmc_data *data)
482{
483 int direction;
484
485 struct scatterlist *sg;
486 int i, size;
487 u8 *align;
488 char *buffer;
489 unsigned long flags;
490
491 if (data->flags & MMC_DATA_READ)
492 direction = DMA_FROM_DEVICE;
493 else
494 direction = DMA_TO_DEVICE;
495
496 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
497 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
498
499 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
500 128 * 4, direction);
501
502 if (data->flags & MMC_DATA_READ) {
503 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
504 data->sg_len, direction);
505
506 align = host->align_buffer;
507
508 for_each_sg(data->sg, sg, host->sg_count, i) {
509 if (sg_dma_address(sg) & 0x3) {
510 size = 4 - (sg_dma_address(sg) & 0x3);
511
512 buffer = sdhci_kmap_atomic(sg, &flags);
513 memcpy(buffer, align, size);
514 sdhci_kunmap_atomic(buffer, &flags);
515
516 align += 4;
517 }
518 }
519 }
520
521 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
522 data->sg_len, direction);
523}
524
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200525static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800526{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700527 u8 count;
528 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800529
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200530 /*
531 * If the host controller provides us with an incorrect timeout
532 * value, just skip the check and use 0xE. The hardware may take
533 * longer to time out, but that's much better than having a too-short
534 * timeout value.
535 */
536 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
537 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200538
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700539 /* timeout in us */
540 target_timeout = data->timeout_ns / 1000 +
541 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800542
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700543 /*
544 * Figure out needed cycles.
545 * We do this in steps in order to fit inside a 32 bit int.
546 * The first step is the minimum timeout, which will have a
547 * minimum resolution of 6 bits:
548 * (1) 2^13*1000 > 2^22,
549 * (2) host->timeout_clk < 2^16
550 * =>
551 * (1) / (2) > 2^6
552 */
553 count = 0;
554 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
555 while (current_timeout < target_timeout) {
556 count++;
557 current_timeout <<= 1;
558 if (count >= 0xF)
559 break;
560 }
561
562 if (count >= 0xF) {
563 printk(KERN_WARNING "%s: Too large timeout requested!\n",
564 mmc_hostname(host->mmc));
565 count = 0xE;
566 }
567
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200568 return count;
569}
570
571static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
572{
573 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200574 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200575 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200576
577 WARN_ON(host->data);
578
579 if (data == NULL)
580 return;
581
582 /* Sanity checks */
583 BUG_ON(data->blksz * data->blocks > 524288);
584 BUG_ON(data->blksz > host->mmc->max_blk_size);
585 BUG_ON(data->blocks > 65535);
586
587 host->data = data;
588 host->data_early = 0;
589
590 count = sdhci_calc_timeout(host, data);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700591 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800592
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100593 if (host->flags & SDHCI_USE_DMA)
594 host->flags |= SDHCI_REQ_USE_DMA;
595
Pierre Ossman2134a922008-06-28 18:28:51 +0200596 /*
597 * FIXME: This doesn't account for merging when mapping the
598 * scatterlist.
599 */
600 if (host->flags & SDHCI_REQ_USE_DMA) {
601 int broken, i;
602 struct scatterlist *sg;
603
604 broken = 0;
605 if (host->flags & SDHCI_USE_ADMA) {
606 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
607 broken = 1;
608 } else {
609 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
610 broken = 1;
611 }
612
613 if (unlikely(broken)) {
614 for_each_sg(data->sg, sg, data->sg_len, i) {
615 if (sg->length & 0x3) {
616 DBG("Reverting to PIO because of "
617 "transfer size (%d)\n",
618 sg->length);
619 host->flags &= ~SDHCI_REQ_USE_DMA;
620 break;
621 }
622 }
623 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100624 }
625
626 /*
627 * The assumption here being that alignment is the same after
628 * translation to device address space.
629 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200630 if (host->flags & SDHCI_REQ_USE_DMA) {
631 int broken, i;
632 struct scatterlist *sg;
633
634 broken = 0;
635 if (host->flags & SDHCI_USE_ADMA) {
636 /*
637 * As we use 3 byte chunks to work around
638 * alignment problems, we need to check this
639 * quirk.
640 */
641 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
642 broken = 1;
643 } else {
644 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
645 broken = 1;
646 }
647
648 if (unlikely(broken)) {
649 for_each_sg(data->sg, sg, data->sg_len, i) {
650 if (sg->offset & 0x3) {
651 DBG("Reverting to PIO because of "
652 "bad alignment\n");
653 host->flags &= ~SDHCI_REQ_USE_DMA;
654 break;
655 }
656 }
657 }
658 }
659
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200660 if (host->flags & SDHCI_REQ_USE_DMA) {
661 if (host->flags & SDHCI_USE_ADMA) {
662 ret = sdhci_adma_table_pre(host, data);
663 if (ret) {
664 /*
665 * This only happens when someone fed
666 * us an invalid request.
667 */
668 WARN_ON(1);
669 host->flags &= ~SDHCI_USE_DMA;
670 } else {
671 writel(host->adma_addr,
672 host->ioaddr + SDHCI_ADMA_ADDRESS);
673 }
674 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300675 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200676
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300677 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200678 data->sg, data->sg_len,
679 (data->flags & MMC_DATA_READ) ?
680 DMA_FROM_DEVICE :
681 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300682 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200683 /*
684 * This only happens when someone fed
685 * us an invalid request.
686 */
687 WARN_ON(1);
688 host->flags &= ~SDHCI_USE_DMA;
689 } else {
690 WARN_ON(count != 1);
691 writel(sg_dma_address(data->sg),
692 host->ioaddr + SDHCI_DMA_ADDRESS);
693 }
694 }
695 }
696
Pierre Ossman2134a922008-06-28 18:28:51 +0200697 /*
698 * Always adjust the DMA selection as some controllers
699 * (e.g. JMicron) can't do PIO properly when the selection
700 * is ADMA.
701 */
702 if (host->version >= SDHCI_SPEC_200) {
703 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
704 ctrl &= ~SDHCI_CTRL_DMA_MASK;
705 if ((host->flags & SDHCI_REQ_USE_DMA) &&
706 (host->flags & SDHCI_USE_ADMA))
707 ctrl |= SDHCI_CTRL_ADMA32;
708 else
709 ctrl |= SDHCI_CTRL_SDMA;
710 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100711 }
712
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200713 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800714 host->cur_sg = data->sg;
715 host->num_sg = data->sg_len;
716
717 host->offset = 0;
718 host->remain = host->cur_sg->length;
719 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700720
Pierre Ossmanbab76962006-07-02 16:51:35 +0100721 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
722 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
723 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700724 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
725}
726
727static void sdhci_set_transfer_mode(struct sdhci_host *host,
728 struct mmc_data *data)
729{
730 u16 mode;
731
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700732 if (data == NULL)
733 return;
734
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200735 WARN_ON(!host->data);
736
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700737 mode = SDHCI_TRNS_BLK_CNT_EN;
738 if (data->blocks > 1)
739 mode |= SDHCI_TRNS_MULTI;
740 if (data->flags & MMC_DATA_READ)
741 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100742 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700743 mode |= SDHCI_TRNS_DMA;
744
745 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800746}
747
748static void sdhci_finish_data(struct sdhci_host *host)
749{
750 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800751
752 BUG_ON(!host->data);
753
754 data = host->data;
755 host->data = NULL;
756
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100757 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200758 if (host->flags & SDHCI_USE_ADMA)
759 sdhci_adma_table_post(host, data);
760 else {
761 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
762 data->sg_len, (data->flags & MMC_DATA_READ) ?
763 DMA_FROM_DEVICE : DMA_TO_DEVICE);
764 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800765 }
766
767 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200768 * The specification states that the block count register must
769 * be updated, but it does not specify at what point in the
770 * data flow. That makes the register entirely useless to read
771 * back so we have to assume that nothing made it to the card
772 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800773 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200774 if (data->error)
775 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800776 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200777 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800778
Pierre Ossmand129bce2006-03-24 03:18:17 -0800779 if (data->stop) {
780 /*
781 * The controller needs a reset of internal state machines
782 * upon error conditions.
783 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200784 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800785 sdhci_reset(host, SDHCI_RESET_CMD);
786 sdhci_reset(host, SDHCI_RESET_DATA);
787 }
788
789 sdhci_send_command(host, data->stop);
790 } else
791 tasklet_schedule(&host->finish_tasklet);
792}
793
794static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
795{
796 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700797 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700798 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800799
800 WARN_ON(host->cmd);
801
Pierre Ossmand129bce2006-03-24 03:18:17 -0800802 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700803 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700804
805 mask = SDHCI_CMD_INHIBIT;
806 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
807 mask |= SDHCI_DATA_INHIBIT;
808
809 /* We shouldn't wait for data inihibit for stop commands, even
810 though they might use busy signaling */
811 if (host->mrq->data && (cmd == host->mrq->data->stop))
812 mask &= ~SDHCI_DATA_INHIBIT;
813
814 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700815 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800816 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100817 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800818 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200819 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800820 tasklet_schedule(&host->finish_tasklet);
821 return;
822 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700823 timeout--;
824 mdelay(1);
825 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800826
827 mod_timer(&host->timer, jiffies + 10 * HZ);
828
829 host->cmd = cmd;
830
831 sdhci_prepare_data(host, cmd->data);
832
833 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
834
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700835 sdhci_set_transfer_mode(host, cmd->data);
836
Pierre Ossmand129bce2006-03-24 03:18:17 -0800837 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100838 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800839 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200840 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800841 tasklet_schedule(&host->finish_tasklet);
842 return;
843 }
844
845 if (!(cmd->flags & MMC_RSP_PRESENT))
846 flags = SDHCI_CMD_RESP_NONE;
847 else if (cmd->flags & MMC_RSP_136)
848 flags = SDHCI_CMD_RESP_LONG;
849 else if (cmd->flags & MMC_RSP_BUSY)
850 flags = SDHCI_CMD_RESP_SHORT_BUSY;
851 else
852 flags = SDHCI_CMD_RESP_SHORT;
853
854 if (cmd->flags & MMC_RSP_CRC)
855 flags |= SDHCI_CMD_CRC;
856 if (cmd->flags & MMC_RSP_OPCODE)
857 flags |= SDHCI_CMD_INDEX;
858 if (cmd->data)
859 flags |= SDHCI_CMD_DATA;
860
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200861 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800862 host->ioaddr + SDHCI_COMMAND);
863}
864
865static void sdhci_finish_command(struct sdhci_host *host)
866{
867 int i;
868
869 BUG_ON(host->cmd == NULL);
870
871 if (host->cmd->flags & MMC_RSP_PRESENT) {
872 if (host->cmd->flags & MMC_RSP_136) {
873 /* CRC is stripped so we need to do some shifting. */
874 for (i = 0;i < 4;i++) {
875 host->cmd->resp[i] = readl(host->ioaddr +
876 SDHCI_RESPONSE + (3-i)*4) << 8;
877 if (i != 3)
878 host->cmd->resp[i] |=
879 readb(host->ioaddr +
880 SDHCI_RESPONSE + (3-i)*4-1);
881 }
882 } else {
883 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
884 }
885 }
886
Pierre Ossman17b04292007-07-22 22:18:46 +0200887 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200889 if (host->data && host->data_early)
890 sdhci_finish_data(host);
891
892 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893 tasklet_schedule(&host->finish_tasklet);
894
895 host->cmd = NULL;
896}
897
898static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
899{
900 int div;
901 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700902 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800903
904 if (clock == host->clock)
905 return;
906
907 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
908
909 if (clock == 0)
910 goto out;
911
912 for (div = 1;div < 256;div *= 2) {
913 if ((host->max_clk / div) <= clock)
914 break;
915 }
916 div >>= 1;
917
918 clk = div << SDHCI_DIVIDER_SHIFT;
919 clk |= SDHCI_CLOCK_INT_EN;
920 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
921
922 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700923 timeout = 10;
924 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
925 & SDHCI_CLOCK_INT_STABLE)) {
926 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100927 printk(KERN_ERR "%s: Internal clock never "
928 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 sdhci_dumpregs(host);
930 return;
931 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700932 timeout--;
933 mdelay(1);
934 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935
936 clk |= SDHCI_CLOCK_CARD_EN;
937 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
938
939out:
940 host->clock = clock;
941}
942
Pierre Ossman146ad662006-06-30 02:22:23 -0700943static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
944{
945 u8 pwr;
946
947 if (host->power == power)
948 return;
949
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100950 if (power == (unsigned short)-1) {
951 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700952 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100953 }
954
955 /*
956 * Spec says that we should clear the power reg before setting
957 * a new value. Some controllers don't seem to like this though.
958 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100959 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100960 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700961
962 pwr = SDHCI_POWER_ON;
963
Philip Langdale4be34c92007-03-11 17:15:15 -0700964 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -0700965 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -0700966 pwr |= SDHCI_POWER_180;
967 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700968 case MMC_VDD_29_30:
969 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700970 pwr |= SDHCI_POWER_300;
971 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700972 case MMC_VDD_32_33:
973 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700974 pwr |= SDHCI_POWER_330;
975 break;
976 default:
977 BUG();
978 }
979
Andres Salomone08c1692008-07-04 10:00:03 -0700980 /*
Andres Salomonc71f6512008-07-07 17:25:56 -0400981 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -0700982 * and set turn on power at the same time, so set the voltage first.
983 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100984 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Andres Salomone08c1692008-07-04 10:00:03 -0700985 writeb(pwr & ~SDHCI_POWER_ON,
986 host->ioaddr + SDHCI_POWER_CONTROL);
987
Pierre Ossman146ad662006-06-30 02:22:23 -0700988 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
989
990out:
991 host->power = power;
992}
993
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994/*****************************************************************************\
995 * *
996 * MMC callbacks *
997 * *
998\*****************************************************************************/
999
1000static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1001{
1002 struct sdhci_host *host;
1003 unsigned long flags;
1004
1005 host = mmc_priv(mmc);
1006
1007 spin_lock_irqsave(&host->lock, flags);
1008
1009 WARN_ON(host->mrq != NULL);
1010
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001011#ifndef CONFIG_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001013#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015 host->mrq = mrq;
1016
Pierre Ossman1e728592008-04-16 19:13:13 +02001017 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)
1018 || (host->flags & SDHCI_DEVICE_DEAD)) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001019 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 tasklet_schedule(&host->finish_tasklet);
1021 } else
1022 sdhci_send_command(host, mrq->cmd);
1023
Pierre Ossman5f25a662006-10-04 02:15:39 -07001024 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025 spin_unlock_irqrestore(&host->lock, flags);
1026}
1027
1028static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1029{
1030 struct sdhci_host *host;
1031 unsigned long flags;
1032 u8 ctrl;
1033
1034 host = mmc_priv(mmc);
1035
1036 spin_lock_irqsave(&host->lock, flags);
1037
Pierre Ossman1e728592008-04-16 19:13:13 +02001038 if (host->flags & SDHCI_DEVICE_DEAD)
1039 goto out;
1040
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041 /*
1042 * Reset the chip on each power off.
1043 * Should clear out any weird states.
1044 */
1045 if (ios->power_mode == MMC_POWER_OFF) {
1046 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 }
1049
1050 sdhci_set_clock(host, ios->clock);
1051
1052 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001053 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001055 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056
1057 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001058
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 if (ios->bus_width == MMC_BUS_WIDTH_4)
1060 ctrl |= SDHCI_CTRL_4BITBUS;
1061 else
1062 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001063
1064 if (ios->timing == MMC_TIMING_SD_HS)
1065 ctrl |= SDHCI_CTRL_HISPD;
1066 else
1067 ctrl &= ~SDHCI_CTRL_HISPD;
1068
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
1070
Leandro Dorileob8352262007-07-25 23:47:04 +02001071 /*
1072 * Some (ENE) controllers go apeshit on some ios operation,
1073 * signalling timeout and CRC errors even on CMD0. Resetting
1074 * it on each ios seems to solve the problem.
1075 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001076 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001077 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1078
Pierre Ossman1e728592008-04-16 19:13:13 +02001079out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001080 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 spin_unlock_irqrestore(&host->lock, flags);
1082}
1083
1084static int sdhci_get_ro(struct mmc_host *mmc)
1085{
1086 struct sdhci_host *host;
1087 unsigned long flags;
1088 int present;
1089
1090 host = mmc_priv(mmc);
1091
1092 spin_lock_irqsave(&host->lock, flags);
1093
Pierre Ossman1e728592008-04-16 19:13:13 +02001094 if (host->flags & SDHCI_DEVICE_DEAD)
1095 present = 0;
1096 else
1097 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098
1099 spin_unlock_irqrestore(&host->lock, flags);
1100
1101 return !(present & SDHCI_WRITE_PROTECT);
1102}
1103
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001104static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1105{
1106 struct sdhci_host *host;
1107 unsigned long flags;
1108 u32 ier;
1109
1110 host = mmc_priv(mmc);
1111
1112 spin_lock_irqsave(&host->lock, flags);
1113
Pierre Ossman1e728592008-04-16 19:13:13 +02001114 if (host->flags & SDHCI_DEVICE_DEAD)
1115 goto out;
1116
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001117 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
1118
1119 ier &= ~SDHCI_INT_CARD_INT;
1120 if (enable)
1121 ier |= SDHCI_INT_CARD_INT;
1122
1123 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
1124 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
1125
Pierre Ossman1e728592008-04-16 19:13:13 +02001126out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001127 mmiowb();
1128
1129 spin_unlock_irqrestore(&host->lock, flags);
1130}
1131
David Brownellab7aefd2006-11-12 17:55:30 -08001132static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133 .request = sdhci_request,
1134 .set_ios = sdhci_set_ios,
1135 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001136 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137};
1138
1139/*****************************************************************************\
1140 * *
1141 * Tasklets *
1142 * *
1143\*****************************************************************************/
1144
1145static void sdhci_tasklet_card(unsigned long param)
1146{
1147 struct sdhci_host *host;
1148 unsigned long flags;
1149
1150 host = (struct sdhci_host*)param;
1151
1152 spin_lock_irqsave(&host->lock, flags);
1153
1154 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
1155 if (host->mrq) {
1156 printk(KERN_ERR "%s: Card removed during transfer!\n",
1157 mmc_hostname(host->mmc));
1158 printk(KERN_ERR "%s: Resetting controller.\n",
1159 mmc_hostname(host->mmc));
1160
1161 sdhci_reset(host, SDHCI_RESET_CMD);
1162 sdhci_reset(host, SDHCI_RESET_DATA);
1163
Pierre Ossman17b04292007-07-22 22:18:46 +02001164 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165 tasklet_schedule(&host->finish_tasklet);
1166 }
1167 }
1168
1169 spin_unlock_irqrestore(&host->lock, flags);
1170
1171 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1172}
1173
1174static void sdhci_tasklet_finish(unsigned long param)
1175{
1176 struct sdhci_host *host;
1177 unsigned long flags;
1178 struct mmc_request *mrq;
1179
1180 host = (struct sdhci_host*)param;
1181
1182 spin_lock_irqsave(&host->lock, flags);
1183
1184 del_timer(&host->timer);
1185
1186 mrq = host->mrq;
1187
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188 /*
1189 * The controller needs a reset of internal state machines
1190 * upon error conditions.
1191 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001192 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1193 (mrq->cmd->error ||
1194 (mrq->data && (mrq->data->error ||
1195 (mrq->data->stop && mrq->data->stop->error))) ||
1196 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001197
1198 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001199 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001200 unsigned int clock;
1201
1202 /* This is to force an update */
1203 clock = host->clock;
1204 host->clock = 0;
1205 sdhci_set_clock(host, clock);
1206 }
1207
1208 /* Spec says we should do both at the same time, but Ricoh
1209 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001210 sdhci_reset(host, SDHCI_RESET_CMD);
1211 sdhci_reset(host, SDHCI_RESET_DATA);
1212 }
1213
1214 host->mrq = NULL;
1215 host->cmd = NULL;
1216 host->data = NULL;
1217
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001218#ifndef CONFIG_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001220#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221
Pierre Ossman5f25a662006-10-04 02:15:39 -07001222 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223 spin_unlock_irqrestore(&host->lock, flags);
1224
1225 mmc_request_done(host->mmc, mrq);
1226}
1227
1228static void sdhci_timeout_timer(unsigned long data)
1229{
1230 struct sdhci_host *host;
1231 unsigned long flags;
1232
1233 host = (struct sdhci_host*)data;
1234
1235 spin_lock_irqsave(&host->lock, flags);
1236
1237 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001238 printk(KERN_ERR "%s: Timeout waiting for hardware "
1239 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001240 sdhci_dumpregs(host);
1241
1242 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001243 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 sdhci_finish_data(host);
1245 } else {
1246 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001247 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001249 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250
1251 tasklet_schedule(&host->finish_tasklet);
1252 }
1253 }
1254
Pierre Ossman5f25a662006-10-04 02:15:39 -07001255 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256 spin_unlock_irqrestore(&host->lock, flags);
1257}
1258
1259/*****************************************************************************\
1260 * *
1261 * Interrupt handling *
1262 * *
1263\*****************************************************************************/
1264
1265static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1266{
1267 BUG_ON(intmask == 0);
1268
1269 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001270 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1271 "though no command operation was in progress.\n",
1272 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273 sdhci_dumpregs(host);
1274 return;
1275 }
1276
Pierre Ossman43b58b32007-07-25 23:15:27 +02001277 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001278 host->cmd->error = -ETIMEDOUT;
1279 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1280 SDHCI_INT_INDEX))
1281 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001282
Pierre Ossman17b04292007-07-22 22:18:46 +02001283 if (host->cmd->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001284 tasklet_schedule(&host->finish_tasklet);
Pierre Ossman43b58b32007-07-25 23:15:27 +02001285 else if (intmask & SDHCI_INT_RESPONSE)
1286 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287}
1288
1289static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1290{
1291 BUG_ON(intmask == 0);
1292
1293 if (!host->data) {
1294 /*
1295 * A data end interrupt is sent together with the response
1296 * for the stop command.
1297 */
1298 if (intmask & SDHCI_INT_DATA_END)
1299 return;
1300
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001301 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1302 "though no data operation was in progress.\n",
1303 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304 sdhci_dumpregs(host);
1305
1306 return;
1307 }
1308
1309 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001310 host->data->error = -ETIMEDOUT;
1311 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1312 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001313 else if (intmask & SDHCI_INT_ADMA_ERROR)
1314 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315
Pierre Ossman17b04292007-07-22 22:18:46 +02001316 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317 sdhci_finish_data(host);
1318 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001319 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 sdhci_transfer_pio(host);
1321
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001322 /*
1323 * We currently don't do anything fancy with DMA
1324 * boundaries, but as we can't disable the feature
1325 * we need to at least restart the transfer.
1326 */
1327 if (intmask & SDHCI_INT_DMA_END)
1328 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1329 host->ioaddr + SDHCI_DMA_ADDRESS);
1330
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001331 if (intmask & SDHCI_INT_DATA_END) {
1332 if (host->cmd) {
1333 /*
1334 * Data managed to finish before the
1335 * command completed. Make sure we do
1336 * things in the proper order.
1337 */
1338 host->data_early = 1;
1339 } else {
1340 sdhci_finish_data(host);
1341 }
1342 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343 }
1344}
1345
David Howells7d12e782006-10-05 14:55:46 +01001346static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347{
1348 irqreturn_t result;
1349 struct sdhci_host* host = dev_id;
1350 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001351 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352
1353 spin_lock(&host->lock);
1354
1355 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1356
Mark Lord62df67a52007-03-06 13:30:13 +01001357 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358 result = IRQ_NONE;
1359 goto out;
1360 }
1361
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001362 DBG("*** %s got interrupt: 0x%08x\n",
1363 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001364
Pierre Ossman3192a282006-06-30 02:22:26 -07001365 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1366 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1367 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001369 }
1370
1371 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372
1373 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374 writel(intmask & SDHCI_INT_CMD_MASK,
1375 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001376 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001377 }
1378
1379 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001380 writel(intmask & SDHCI_INT_DATA_MASK,
1381 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001382 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383 }
1384
1385 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1386
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001387 intmask &= ~SDHCI_INT_ERROR;
1388
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001390 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001391 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001392 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001393 }
1394
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001395 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001396
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001397 if (intmask & SDHCI_INT_CARD_INT)
1398 cardint = 1;
1399
1400 intmask &= ~SDHCI_INT_CARD_INT;
1401
Pierre Ossman3192a282006-06-30 02:22:26 -07001402 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001403 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001404 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001405 sdhci_dumpregs(host);
1406
Pierre Ossmand129bce2006-03-24 03:18:17 -08001407 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001408 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001409
1410 result = IRQ_HANDLED;
1411
Pierre Ossman5f25a662006-10-04 02:15:39 -07001412 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001413out:
1414 spin_unlock(&host->lock);
1415
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001416 /*
1417 * We have to delay this as it calls back into the driver.
1418 */
1419 if (cardint)
1420 mmc_signal_sdio_irq(host->mmc);
1421
Pierre Ossmand129bce2006-03-24 03:18:17 -08001422 return result;
1423}
1424
1425/*****************************************************************************\
1426 * *
1427 * Suspend/resume *
1428 * *
1429\*****************************************************************************/
1430
1431#ifdef CONFIG_PM
1432
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001433int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001434{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001435 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001437 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001438 if (ret)
1439 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001441 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442
1443 return 0;
1444}
1445
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001446EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001448int sdhci_resume_host(struct sdhci_host *host)
1449{
1450 int ret;
1451
1452 if (host->flags & SDHCI_USE_DMA) {
1453 if (host->ops->enable_dma)
1454 host->ops->enable_dma(host);
1455 }
1456
1457 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1458 mmc_hostname(host->mmc), host);
1459 if (ret)
1460 return ret;
1461
1462 sdhci_init(host);
1463 mmiowb();
1464
1465 ret = mmc_resume_host(host->mmc);
1466 if (ret)
1467 return ret;
1468
1469 return 0;
1470}
1471
1472EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001473
1474#endif /* CONFIG_PM */
1475
1476/*****************************************************************************\
1477 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001478 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479 * *
1480\*****************************************************************************/
1481
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001482struct sdhci_host *sdhci_alloc_host(struct device *dev,
1483 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 struct mmc_host *mmc;
1486 struct sdhci_host *host;
1487
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001488 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001490 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001492 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493
1494 host = mmc_priv(mmc);
1495 host->mmc = mmc;
1496
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001497 return host;
1498}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001499
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001500EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001502int sdhci_add_host(struct sdhci_host *host)
1503{
1504 struct mmc_host *mmc;
1505 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001506 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001508 WARN_ON(host == NULL);
1509 if (host == NULL)
1510 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001512 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001514 if (debug_quirks)
1515 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001516
Pierre Ossmand96649e2006-06-30 02:22:30 -07001517 sdhci_reset(host, SDHCI_RESET_ALL);
1518
Pierre Ossman2134a922008-06-28 18:28:51 +02001519 host->version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1520 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1521 >> SDHCI_SPEC_VER_SHIFT;
1522 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001523 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001524 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001525 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001526 }
1527
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1529
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001530 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001531 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001532 else if (!(caps & SDHCI_CAN_DO_DMA))
1533 DBG("Controller doesn't have DMA capability\n");
1534 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535 host->flags |= SDHCI_USE_DMA;
1536
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001537 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001538 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001539 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001540 host->flags &= ~SDHCI_USE_DMA;
1541 }
1542
Pierre Ossmand129bce2006-03-24 03:18:17 -08001543 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001544 if ((host->version >= SDHCI_SPEC_200) &&
1545 (caps & SDHCI_CAN_DO_ADMA2))
1546 host->flags |= SDHCI_USE_ADMA;
1547 }
1548
1549 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1550 (host->flags & SDHCI_USE_ADMA)) {
1551 DBG("Disabling ADMA as it is marked broken\n");
1552 host->flags &= ~SDHCI_USE_ADMA;
1553 }
1554
1555 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001556 if (host->ops->enable_dma) {
1557 if (host->ops->enable_dma(host)) {
1558 printk(KERN_WARNING "%s: No suitable DMA "
1559 "available. Falling back to PIO.\n",
1560 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001561 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001562 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001563 }
1564 }
1565
Pierre Ossman2134a922008-06-28 18:28:51 +02001566 if (host->flags & SDHCI_USE_ADMA) {
1567 /*
1568 * We need to allocate descriptors for all sg entries
1569 * (128) and potentially one alignment transfer for
1570 * each of those entries.
1571 */
1572 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1573 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1574 if (!host->adma_desc || !host->align_buffer) {
1575 kfree(host->adma_desc);
1576 kfree(host->align_buffer);
1577 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1578 "buffers. Falling back to standard DMA.\n",
1579 mmc_hostname(mmc));
1580 host->flags &= ~SDHCI_USE_ADMA;
1581 }
1582 }
1583
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001584 /* XXX: Hack to get MMC layer to avoid highmem */
1585 if (!(host->flags & SDHCI_USE_DMA))
Tomas Winklerc8b3e022008-07-05 19:52:04 +03001586 mmc_dev(host->mmc)->dma_mask = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001587
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001588 host->max_clk =
1589 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1590 if (host->max_clk == 0) {
1591 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001592 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001593 return -ENODEV;
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001594 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595 host->max_clk *= 1000000;
1596
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001597 host->timeout_clk =
1598 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1599 if (host->timeout_clk == 0) {
1600 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001601 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001602 return -ENODEV;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001603 }
1604 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1605 host->timeout_clk *= 1000;
1606
Pierre Ossmand129bce2006-03-24 03:18:17 -08001607 /*
1608 * Set host parameters.
1609 */
1610 mmc->ops = &sdhci_ops;
1611 mmc->f_min = host->max_clk / 256;
1612 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001613 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001614
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001615 if (caps & SDHCI_CAN_DO_HISPD)
1616 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1617
Pierre Ossman146ad662006-06-30 02:22:23 -07001618 mmc->ocr_avail = 0;
1619 if (caps & SDHCI_CAN_VDD_330)
1620 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001621 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001622 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001623 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001624 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001625
1626 if (mmc->ocr_avail == 0) {
1627 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001628 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001629 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001630 }
1631
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632 spin_lock_init(&host->lock);
1633
1634 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001635 * Maximum number of segments. Depends on if the hardware
1636 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001638 if (host->flags & SDHCI_USE_ADMA)
1639 mmc->max_hw_segs = 128;
1640 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001642 else /* PIO */
1643 mmc->max_hw_segs = 128;
1644 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645
1646 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001647 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001648 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001649 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001650 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
1652 /*
1653 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001654 * of bytes. When doing hardware scatter/gather, each entry cannot
1655 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001656 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001657 if (host->flags & SDHCI_USE_ADMA)
1658 mmc->max_seg_size = 65536;
1659 else
1660 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001661
1662 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001663 * Maximum block size. This varies from controller to controller and
1664 * is specified in the capabilities register.
1665 */
1666 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1667 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001668 printk(KERN_WARNING "%s: Invalid maximum block size, "
1669 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001670 mmc->max_blk_size = 512;
1671 } else
1672 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001673
1674 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001675 * Maximum block count.
1676 */
1677 mmc->max_blk_count = 65535;
1678
1679 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001680 * Init tasklets.
1681 */
1682 tasklet_init(&host->card_tasklet,
1683 sdhci_tasklet_card, (unsigned long)host);
1684 tasklet_init(&host->finish_tasklet,
1685 sdhci_tasklet_finish, (unsigned long)host);
1686
Al Viroe4cad1b2006-10-10 22:47:07 +01001687 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001688
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001689 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001690 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001691 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001692 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693
1694 sdhci_init(host);
1695
1696#ifdef CONFIG_MMC_DEBUG
1697 sdhci_dumpregs(host);
1698#endif
1699
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001700#ifdef CONFIG_LEDS_CLASS
1701 host->led.name = mmc_hostname(mmc);
1702 host->led.brightness = LED_OFF;
1703 host->led.default_trigger = mmc_hostname(mmc);
1704 host->led.brightness_set = sdhci_led_control;
1705
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001706 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001707 if (ret)
1708 goto reset;
1709#endif
1710
Pierre Ossman5f25a662006-10-04 02:15:39 -07001711 mmiowb();
1712
Pierre Ossmand129bce2006-03-24 03:18:17 -08001713 mmc_add_host(mmc);
1714
Pierre Ossman2134a922008-06-28 18:28:51 +02001715 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001716 mmc_hostname(mmc), host->hw_name, mmc_dev(mmc)->bus_id,
Pierre Ossman2134a922008-06-28 18:28:51 +02001717 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001718 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1719
1720 return 0;
1721
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001722#ifdef CONFIG_LEDS_CLASS
1723reset:
1724 sdhci_reset(host, SDHCI_RESET_ALL);
1725 free_irq(host->irq, host);
1726#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001727untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001728 tasklet_kill(&host->card_tasklet);
1729 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001730
1731 return ret;
1732}
1733
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001734EXPORT_SYMBOL_GPL(sdhci_add_host);
1735
Pierre Ossman1e728592008-04-16 19:13:13 +02001736void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001737{
Pierre Ossman1e728592008-04-16 19:13:13 +02001738 unsigned long flags;
1739
1740 if (dead) {
1741 spin_lock_irqsave(&host->lock, flags);
1742
1743 host->flags |= SDHCI_DEVICE_DEAD;
1744
1745 if (host->mrq) {
1746 printk(KERN_ERR "%s: Controller removed during "
1747 " transfer!\n", mmc_hostname(host->mmc));
1748
1749 host->mrq->cmd->error = -ENOMEDIUM;
1750 tasklet_schedule(&host->finish_tasklet);
1751 }
1752
1753 spin_unlock_irqrestore(&host->lock, flags);
1754 }
1755
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001756 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001757
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001758#ifdef CONFIG_LEDS_CLASS
1759 led_classdev_unregister(&host->led);
1760#endif
1761
Pierre Ossman1e728592008-04-16 19:13:13 +02001762 if (!dead)
1763 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764
1765 free_irq(host->irq, host);
1766
1767 del_timer_sync(&host->timer);
1768
1769 tasklet_kill(&host->card_tasklet);
1770 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001771
1772 kfree(host->adma_desc);
1773 kfree(host->align_buffer);
1774
1775 host->adma_desc = NULL;
1776 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001777}
1778
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001779EXPORT_SYMBOL_GPL(sdhci_remove_host);
1780
1781void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001782{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001783 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001784}
1785
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001786EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787
1788/*****************************************************************************\
1789 * *
1790 * Driver init/exit *
1791 * *
1792\*****************************************************************************/
1793
1794static int __init sdhci_drv_init(void)
1795{
1796 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001797 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001798 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1799
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001800 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001801}
1802
1803static void __exit sdhci_drv_exit(void)
1804{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001805}
1806
1807module_init(sdhci_drv_init);
1808module_exit(sdhci_drv_exit);
1809
Pierre Ossmandf673b22006-06-30 02:22:31 -07001810module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001811
Pierre Ossmand129bce2006-03-24 03:18:17 -08001812MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001813MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001814MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001815
Pierre Ossmandf673b22006-06-30 02:22:31 -07001816MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");