blob: b802044ea940a7e597ed46927a7f72c92bff1400 [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
330static void sdhci_adma_table_pre(struct sdhci_host *host,
331 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);
363 BUG_ON(host->align_addr & 0x3);
364
365 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
366 data->sg, data->sg_len, direction);
367
368 desc = host->adma_desc;
369 align = host->align_buffer;
370
371 align_addr = host->align_addr;
372
373 for_each_sg(data->sg, sg, host->sg_count, i) {
374 addr = sg_dma_address(sg);
375 len = sg_dma_len(sg);
376
377 /*
378 * The SDHCI specification states that ADMA
379 * addresses must be 32-bit aligned. If they
380 * aren't, then we use a bounce buffer for
381 * the (up to three) bytes that screw up the
382 * alignment.
383 */
384 offset = (4 - (addr & 0x3)) & 0x3;
385 if (offset) {
386 if (data->flags & MMC_DATA_WRITE) {
387 buffer = sdhci_kmap_atomic(sg, &flags);
388 memcpy(align, buffer, offset);
389 sdhci_kunmap_atomic(buffer, &flags);
390 }
391
392 desc[7] = (align_addr >> 24) & 0xff;
393 desc[6] = (align_addr >> 16) & 0xff;
394 desc[5] = (align_addr >> 8) & 0xff;
395 desc[4] = (align_addr >> 0) & 0xff;
396
397 BUG_ON(offset > 65536);
398
399 desc[3] = (offset >> 8) & 0xff;
400 desc[2] = (offset >> 0) & 0xff;
401
402 desc[1] = 0x00;
403 desc[0] = 0x21; /* tran, valid */
404
405 align += 4;
406 align_addr += 4;
407
408 desc += 8;
409
410 addr += offset;
411 len -= offset;
412 }
413
414 desc[7] = (addr >> 24) & 0xff;
415 desc[6] = (addr >> 16) & 0xff;
416 desc[5] = (addr >> 8) & 0xff;
417 desc[4] = (addr >> 0) & 0xff;
418
419 BUG_ON(len > 65536);
420
421 desc[3] = (len >> 8) & 0xff;
422 desc[2] = (len >> 0) & 0xff;
423
424 desc[1] = 0x00;
425 desc[0] = 0x21; /* tran, valid */
426
427 desc += 8;
428
429 /*
430 * If this triggers then we have a calculation bug
431 * somewhere. :/
432 */
433 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
434 }
435
436 /*
437 * Add a terminating entry.
438 */
439 desc[7] = 0;
440 desc[6] = 0;
441 desc[5] = 0;
442 desc[4] = 0;
443
444 desc[3] = 0;
445 desc[2] = 0;
446
447 desc[1] = 0x00;
448 desc[0] = 0x03; /* nop, end, valid */
449
450 /*
451 * Resync align buffer as we might have changed it.
452 */
453 if (data->flags & MMC_DATA_WRITE) {
454 dma_sync_single_for_device(mmc_dev(host->mmc),
455 host->align_addr, 128 * 4, direction);
456 }
457
458 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
459 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
460 BUG_ON(host->adma_addr & 0x3);
461}
462
463static void sdhci_adma_table_post(struct sdhci_host *host,
464 struct mmc_data *data)
465{
466 int direction;
467
468 struct scatterlist *sg;
469 int i, size;
470 u8 *align;
471 char *buffer;
472 unsigned long flags;
473
474 if (data->flags & MMC_DATA_READ)
475 direction = DMA_FROM_DEVICE;
476 else
477 direction = DMA_TO_DEVICE;
478
479 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
480 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
481
482 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
483 128 * 4, direction);
484
485 if (data->flags & MMC_DATA_READ) {
486 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
487 data->sg_len, direction);
488
489 align = host->align_buffer;
490
491 for_each_sg(data->sg, sg, host->sg_count, i) {
492 if (sg_dma_address(sg) & 0x3) {
493 size = 4 - (sg_dma_address(sg) & 0x3);
494
495 buffer = sdhci_kmap_atomic(sg, &flags);
496 memcpy(buffer, align, size);
497 sdhci_kunmap_atomic(buffer, &flags);
498
499 align += 4;
500 }
501 }
502 }
503
504 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
505 data->sg_len, direction);
506}
507
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200508static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800509{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700510 u8 count;
511 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800512
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200513 /*
514 * If the host controller provides us with an incorrect timeout
515 * value, just skip the check and use 0xE. The hardware may take
516 * longer to time out, but that's much better than having a too-short
517 * timeout value.
518 */
519 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
520 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200521
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700522 /* timeout in us */
523 target_timeout = data->timeout_ns / 1000 +
524 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800525
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700526 /*
527 * Figure out needed cycles.
528 * We do this in steps in order to fit inside a 32 bit int.
529 * The first step is the minimum timeout, which will have a
530 * minimum resolution of 6 bits:
531 * (1) 2^13*1000 > 2^22,
532 * (2) host->timeout_clk < 2^16
533 * =>
534 * (1) / (2) > 2^6
535 */
536 count = 0;
537 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
538 while (current_timeout < target_timeout) {
539 count++;
540 current_timeout <<= 1;
541 if (count >= 0xF)
542 break;
543 }
544
545 if (count >= 0xF) {
546 printk(KERN_WARNING "%s: Too large timeout requested!\n",
547 mmc_hostname(host->mmc));
548 count = 0xE;
549 }
550
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200551 return count;
552}
553
554static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
555{
556 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200557 u8 ctrl;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200558
559 WARN_ON(host->data);
560
561 if (data == NULL)
562 return;
563
564 /* Sanity checks */
565 BUG_ON(data->blksz * data->blocks > 524288);
566 BUG_ON(data->blksz > host->mmc->max_blk_size);
567 BUG_ON(data->blocks > 65535);
568
569 host->data = data;
570 host->data_early = 0;
571
572 count = sdhci_calc_timeout(host, data);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700573 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800574
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100575 if (host->flags & SDHCI_USE_DMA)
576 host->flags |= SDHCI_REQ_USE_DMA;
577
Pierre Ossman2134a922008-06-28 18:28:51 +0200578 /*
579 * FIXME: This doesn't account for merging when mapping the
580 * scatterlist.
581 */
582 if (host->flags & SDHCI_REQ_USE_DMA) {
583 int broken, i;
584 struct scatterlist *sg;
585
586 broken = 0;
587 if (host->flags & SDHCI_USE_ADMA) {
588 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
589 broken = 1;
590 } else {
591 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
592 broken = 1;
593 }
594
595 if (unlikely(broken)) {
596 for_each_sg(data->sg, sg, data->sg_len, i) {
597 if (sg->length & 0x3) {
598 DBG("Reverting to PIO because of "
599 "transfer size (%d)\n",
600 sg->length);
601 host->flags &= ~SDHCI_REQ_USE_DMA;
602 break;
603 }
604 }
605 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100606 }
607
608 /*
609 * The assumption here being that alignment is the same after
610 * translation to device address space.
611 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200612 if (host->flags & SDHCI_REQ_USE_DMA) {
613 int broken, i;
614 struct scatterlist *sg;
615
616 broken = 0;
617 if (host->flags & SDHCI_USE_ADMA) {
618 /*
619 * As we use 3 byte chunks to work around
620 * alignment problems, we need to check this
621 * quirk.
622 */
623 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
624 broken = 1;
625 } else {
626 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
627 broken = 1;
628 }
629
630 if (unlikely(broken)) {
631 for_each_sg(data->sg, sg, data->sg_len, i) {
632 if (sg->offset & 0x3) {
633 DBG("Reverting to PIO because of "
634 "bad alignment\n");
635 host->flags &= ~SDHCI_REQ_USE_DMA;
636 break;
637 }
638 }
639 }
640 }
641
642 /*
643 * Always adjust the DMA selection as some controllers
644 * (e.g. JMicron) can't do PIO properly when the selection
645 * is ADMA.
646 */
647 if (host->version >= SDHCI_SPEC_200) {
648 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
649 ctrl &= ~SDHCI_CTRL_DMA_MASK;
650 if ((host->flags & SDHCI_REQ_USE_DMA) &&
651 (host->flags & SDHCI_USE_ADMA))
652 ctrl |= SDHCI_CTRL_ADMA32;
653 else
654 ctrl |= SDHCI_CTRL_SDMA;
655 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100656 }
657
658 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200659 if (host->flags & SDHCI_USE_ADMA) {
660 sdhci_adma_table_pre(host, data);
661 writel(host->adma_addr,
662 host->ioaddr + SDHCI_ADMA_ADDRESS);
663 } else {
664 int count;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800665
Pierre Ossman2134a922008-06-28 18:28:51 +0200666 count = dma_map_sg(mmc_dev(host->mmc),
667 data->sg, data->sg_len,
668 (data->flags & MMC_DATA_READ) ?
669 DMA_FROM_DEVICE :
670 DMA_TO_DEVICE);
671 WARN_ON(count != 1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800672
Pierre Ossman2134a922008-06-28 18:28:51 +0200673 writel(sg_dma_address(data->sg),
674 host->ioaddr + SDHCI_DMA_ADDRESS);
675 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800676 } else {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800677 host->cur_sg = data->sg;
678 host->num_sg = data->sg_len;
679
680 host->offset = 0;
681 host->remain = host->cur_sg->length;
682 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700683
Pierre Ossmanbab76962006-07-02 16:51:35 +0100684 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
685 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
686 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700687 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
688}
689
690static void sdhci_set_transfer_mode(struct sdhci_host *host,
691 struct mmc_data *data)
692{
693 u16 mode;
694
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700695 if (data == NULL)
696 return;
697
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200698 WARN_ON(!host->data);
699
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700700 mode = SDHCI_TRNS_BLK_CNT_EN;
701 if (data->blocks > 1)
702 mode |= SDHCI_TRNS_MULTI;
703 if (data->flags & MMC_DATA_READ)
704 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100705 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700706 mode |= SDHCI_TRNS_DMA;
707
708 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800709}
710
711static void sdhci_finish_data(struct sdhci_host *host)
712{
713 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800714
715 BUG_ON(!host->data);
716
717 data = host->data;
718 host->data = NULL;
719
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100720 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200721 if (host->flags & SDHCI_USE_ADMA)
722 sdhci_adma_table_post(host, data);
723 else {
724 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
725 data->sg_len, (data->flags & MMC_DATA_READ) ?
726 DMA_FROM_DEVICE : DMA_TO_DEVICE);
727 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800728 }
729
730 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200731 * The specification states that the block count register must
732 * be updated, but it does not specify at what point in the
733 * data flow. That makes the register entirely useless to read
734 * back so we have to assume that nothing made it to the card
735 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800736 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200737 if (data->error)
738 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800739 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200740 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800741
Pierre Ossmand129bce2006-03-24 03:18:17 -0800742 if (data->stop) {
743 /*
744 * The controller needs a reset of internal state machines
745 * upon error conditions.
746 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200747 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800748 sdhci_reset(host, SDHCI_RESET_CMD);
749 sdhci_reset(host, SDHCI_RESET_DATA);
750 }
751
752 sdhci_send_command(host, data->stop);
753 } else
754 tasklet_schedule(&host->finish_tasklet);
755}
756
757static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
758{
759 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700760 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700761 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800762
763 WARN_ON(host->cmd);
764
Pierre Ossmand129bce2006-03-24 03:18:17 -0800765 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700766 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700767
768 mask = SDHCI_CMD_INHIBIT;
769 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
770 mask |= SDHCI_DATA_INHIBIT;
771
772 /* We shouldn't wait for data inihibit for stop commands, even
773 though they might use busy signaling */
774 if (host->mrq->data && (cmd == host->mrq->data->stop))
775 mask &= ~SDHCI_DATA_INHIBIT;
776
777 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700778 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800779 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100780 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800781 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200782 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800783 tasklet_schedule(&host->finish_tasklet);
784 return;
785 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700786 timeout--;
787 mdelay(1);
788 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800789
790 mod_timer(&host->timer, jiffies + 10 * HZ);
791
792 host->cmd = cmd;
793
794 sdhci_prepare_data(host, cmd->data);
795
796 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
797
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700798 sdhci_set_transfer_mode(host, cmd->data);
799
Pierre Ossmand129bce2006-03-24 03:18:17 -0800800 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100801 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800802 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200803 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800804 tasklet_schedule(&host->finish_tasklet);
805 return;
806 }
807
808 if (!(cmd->flags & MMC_RSP_PRESENT))
809 flags = SDHCI_CMD_RESP_NONE;
810 else if (cmd->flags & MMC_RSP_136)
811 flags = SDHCI_CMD_RESP_LONG;
812 else if (cmd->flags & MMC_RSP_BUSY)
813 flags = SDHCI_CMD_RESP_SHORT_BUSY;
814 else
815 flags = SDHCI_CMD_RESP_SHORT;
816
817 if (cmd->flags & MMC_RSP_CRC)
818 flags |= SDHCI_CMD_CRC;
819 if (cmd->flags & MMC_RSP_OPCODE)
820 flags |= SDHCI_CMD_INDEX;
821 if (cmd->data)
822 flags |= SDHCI_CMD_DATA;
823
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200824 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800825 host->ioaddr + SDHCI_COMMAND);
826}
827
828static void sdhci_finish_command(struct sdhci_host *host)
829{
830 int i;
831
832 BUG_ON(host->cmd == NULL);
833
834 if (host->cmd->flags & MMC_RSP_PRESENT) {
835 if (host->cmd->flags & MMC_RSP_136) {
836 /* CRC is stripped so we need to do some shifting. */
837 for (i = 0;i < 4;i++) {
838 host->cmd->resp[i] = readl(host->ioaddr +
839 SDHCI_RESPONSE + (3-i)*4) << 8;
840 if (i != 3)
841 host->cmd->resp[i] |=
842 readb(host->ioaddr +
843 SDHCI_RESPONSE + (3-i)*4-1);
844 }
845 } else {
846 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
847 }
848 }
849
Pierre Ossman17b04292007-07-22 22:18:46 +0200850 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800851
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200852 if (host->data && host->data_early)
853 sdhci_finish_data(host);
854
855 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800856 tasklet_schedule(&host->finish_tasklet);
857
858 host->cmd = NULL;
859}
860
861static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
862{
863 int div;
864 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700865 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866
867 if (clock == host->clock)
868 return;
869
870 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
871
872 if (clock == 0)
873 goto out;
874
875 for (div = 1;div < 256;div *= 2) {
876 if ((host->max_clk / div) <= clock)
877 break;
878 }
879 div >>= 1;
880
881 clk = div << SDHCI_DIVIDER_SHIFT;
882 clk |= SDHCI_CLOCK_INT_EN;
883 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
884
885 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700886 timeout = 10;
887 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
888 & SDHCI_CLOCK_INT_STABLE)) {
889 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100890 printk(KERN_ERR "%s: Internal clock never "
891 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800892 sdhci_dumpregs(host);
893 return;
894 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700895 timeout--;
896 mdelay(1);
897 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800898
899 clk |= SDHCI_CLOCK_CARD_EN;
900 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
901
902out:
903 host->clock = clock;
904}
905
Pierre Ossman146ad662006-06-30 02:22:23 -0700906static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
907{
908 u8 pwr;
909
910 if (host->power == power)
911 return;
912
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100913 if (power == (unsigned short)-1) {
914 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700915 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100916 }
917
918 /*
919 * Spec says that we should clear the power reg before setting
920 * a new value. Some controllers don't seem to like this though.
921 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100922 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100923 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700924
925 pwr = SDHCI_POWER_ON;
926
Philip Langdale4be34c92007-03-11 17:15:15 -0700927 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -0700928 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -0700929 pwr |= SDHCI_POWER_180;
930 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700931 case MMC_VDD_29_30:
932 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700933 pwr |= SDHCI_POWER_300;
934 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700935 case MMC_VDD_32_33:
936 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700937 pwr |= SDHCI_POWER_330;
938 break;
939 default:
940 BUG();
941 }
942
Andres Salomone08c1692008-07-04 10:00:03 -0700943 /*
944 * At least the CaFe chip gets confused if we set the voltage
945 * and set turn on power at the same time, so set the voltage first.
946 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100947 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Andres Salomone08c1692008-07-04 10:00:03 -0700948 writeb(pwr & ~SDHCI_POWER_ON,
949 host->ioaddr + SDHCI_POWER_CONTROL);
950
Pierre Ossman146ad662006-06-30 02:22:23 -0700951 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
952
953out:
954 host->power = power;
955}
956
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957/*****************************************************************************\
958 * *
959 * MMC callbacks *
960 * *
961\*****************************************************************************/
962
963static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
964{
965 struct sdhci_host *host;
966 unsigned long flags;
967
968 host = mmc_priv(mmc);
969
970 spin_lock_irqsave(&host->lock, flags);
971
972 WARN_ON(host->mrq != NULL);
973
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100974#ifndef CONFIG_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100976#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977
978 host->mrq = mrq;
979
Pierre Ossman1e728592008-04-16 19:13:13 +0200980 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)
981 || (host->flags & SDHCI_DEVICE_DEAD)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200982 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 tasklet_schedule(&host->finish_tasklet);
984 } else
985 sdhci_send_command(host, mrq->cmd);
986
Pierre Ossman5f25a662006-10-04 02:15:39 -0700987 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 spin_unlock_irqrestore(&host->lock, flags);
989}
990
991static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
992{
993 struct sdhci_host *host;
994 unsigned long flags;
995 u8 ctrl;
996
997 host = mmc_priv(mmc);
998
999 spin_lock_irqsave(&host->lock, flags);
1000
Pierre Ossman1e728592008-04-16 19:13:13 +02001001 if (host->flags & SDHCI_DEVICE_DEAD)
1002 goto out;
1003
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 /*
1005 * Reset the chip on each power off.
1006 * Should clear out any weird states.
1007 */
1008 if (ios->power_mode == MMC_POWER_OFF) {
1009 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011 }
1012
1013 sdhci_set_clock(host, ios->clock);
1014
1015 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001016 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001018 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
1020 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001021
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 if (ios->bus_width == MMC_BUS_WIDTH_4)
1023 ctrl |= SDHCI_CTRL_4BITBUS;
1024 else
1025 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001026
1027 if (ios->timing == MMC_TIMING_SD_HS)
1028 ctrl |= SDHCI_CTRL_HISPD;
1029 else
1030 ctrl &= ~SDHCI_CTRL_HISPD;
1031
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
1033
Leandro Dorileob8352262007-07-25 23:47:04 +02001034 /*
1035 * Some (ENE) controllers go apeshit on some ios operation,
1036 * signalling timeout and CRC errors even on CMD0. Resetting
1037 * it on each ios seems to solve the problem.
1038 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001039 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001040 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1041
Pierre Ossman1e728592008-04-16 19:13:13 +02001042out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001043 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 spin_unlock_irqrestore(&host->lock, flags);
1045}
1046
1047static int sdhci_get_ro(struct mmc_host *mmc)
1048{
1049 struct sdhci_host *host;
1050 unsigned long flags;
1051 int present;
1052
1053 host = mmc_priv(mmc);
1054
1055 spin_lock_irqsave(&host->lock, flags);
1056
Pierre Ossman1e728592008-04-16 19:13:13 +02001057 if (host->flags & SDHCI_DEVICE_DEAD)
1058 present = 0;
1059 else
1060 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061
1062 spin_unlock_irqrestore(&host->lock, flags);
1063
1064 return !(present & SDHCI_WRITE_PROTECT);
1065}
1066
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001067static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1068{
1069 struct sdhci_host *host;
1070 unsigned long flags;
1071 u32 ier;
1072
1073 host = mmc_priv(mmc);
1074
1075 spin_lock_irqsave(&host->lock, flags);
1076
Pierre Ossman1e728592008-04-16 19:13:13 +02001077 if (host->flags & SDHCI_DEVICE_DEAD)
1078 goto out;
1079
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001080 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
1081
1082 ier &= ~SDHCI_INT_CARD_INT;
1083 if (enable)
1084 ier |= SDHCI_INT_CARD_INT;
1085
1086 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
1087 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
1088
Pierre Ossman1e728592008-04-16 19:13:13 +02001089out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001090 mmiowb();
1091
1092 spin_unlock_irqrestore(&host->lock, flags);
1093}
1094
David Brownellab7aefd2006-11-12 17:55:30 -08001095static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096 .request = sdhci_request,
1097 .set_ios = sdhci_set_ios,
1098 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001099 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100};
1101
1102/*****************************************************************************\
1103 * *
1104 * Tasklets *
1105 * *
1106\*****************************************************************************/
1107
1108static void sdhci_tasklet_card(unsigned long param)
1109{
1110 struct sdhci_host *host;
1111 unsigned long flags;
1112
1113 host = (struct sdhci_host*)param;
1114
1115 spin_lock_irqsave(&host->lock, flags);
1116
1117 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
1118 if (host->mrq) {
1119 printk(KERN_ERR "%s: Card removed during transfer!\n",
1120 mmc_hostname(host->mmc));
1121 printk(KERN_ERR "%s: Resetting controller.\n",
1122 mmc_hostname(host->mmc));
1123
1124 sdhci_reset(host, SDHCI_RESET_CMD);
1125 sdhci_reset(host, SDHCI_RESET_DATA);
1126
Pierre Ossman17b04292007-07-22 22:18:46 +02001127 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128 tasklet_schedule(&host->finish_tasklet);
1129 }
1130 }
1131
1132 spin_unlock_irqrestore(&host->lock, flags);
1133
1134 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1135}
1136
1137static void sdhci_tasklet_finish(unsigned long param)
1138{
1139 struct sdhci_host *host;
1140 unsigned long flags;
1141 struct mmc_request *mrq;
1142
1143 host = (struct sdhci_host*)param;
1144
1145 spin_lock_irqsave(&host->lock, flags);
1146
1147 del_timer(&host->timer);
1148
1149 mrq = host->mrq;
1150
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 /*
1152 * The controller needs a reset of internal state machines
1153 * upon error conditions.
1154 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001155 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1156 (mrq->cmd->error ||
1157 (mrq->data && (mrq->data->error ||
1158 (mrq->data->stop && mrq->data->stop->error))) ||
1159 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001160
1161 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001162 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001163 unsigned int clock;
1164
1165 /* This is to force an update */
1166 clock = host->clock;
1167 host->clock = 0;
1168 sdhci_set_clock(host, clock);
1169 }
1170
1171 /* Spec says we should do both at the same time, but Ricoh
1172 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 sdhci_reset(host, SDHCI_RESET_CMD);
1174 sdhci_reset(host, SDHCI_RESET_DATA);
1175 }
1176
1177 host->mrq = NULL;
1178 host->cmd = NULL;
1179 host->data = NULL;
1180
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001181#ifndef CONFIG_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001182 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001183#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184
Pierre Ossman5f25a662006-10-04 02:15:39 -07001185 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 spin_unlock_irqrestore(&host->lock, flags);
1187
1188 mmc_request_done(host->mmc, mrq);
1189}
1190
1191static void sdhci_timeout_timer(unsigned long data)
1192{
1193 struct sdhci_host *host;
1194 unsigned long flags;
1195
1196 host = (struct sdhci_host*)data;
1197
1198 spin_lock_irqsave(&host->lock, flags);
1199
1200 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001201 printk(KERN_ERR "%s: Timeout waiting for hardware "
1202 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203 sdhci_dumpregs(host);
1204
1205 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001206 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 sdhci_finish_data(host);
1208 } else {
1209 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001210 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001212 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
1214 tasklet_schedule(&host->finish_tasklet);
1215 }
1216 }
1217
Pierre Ossman5f25a662006-10-04 02:15:39 -07001218 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219 spin_unlock_irqrestore(&host->lock, flags);
1220}
1221
1222/*****************************************************************************\
1223 * *
1224 * Interrupt handling *
1225 * *
1226\*****************************************************************************/
1227
1228static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1229{
1230 BUG_ON(intmask == 0);
1231
1232 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001233 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1234 "though no command operation was in progress.\n",
1235 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236 sdhci_dumpregs(host);
1237 return;
1238 }
1239
Pierre Ossman43b58b32007-07-25 23:15:27 +02001240 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001241 host->cmd->error = -ETIMEDOUT;
1242 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1243 SDHCI_INT_INDEX))
1244 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245
Pierre Ossman17b04292007-07-22 22:18:46 +02001246 if (host->cmd->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247 tasklet_schedule(&host->finish_tasklet);
Pierre Ossman43b58b32007-07-25 23:15:27 +02001248 else if (intmask & SDHCI_INT_RESPONSE)
1249 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250}
1251
1252static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1253{
1254 BUG_ON(intmask == 0);
1255
1256 if (!host->data) {
1257 /*
1258 * A data end interrupt is sent together with the response
1259 * for the stop command.
1260 */
1261 if (intmask & SDHCI_INT_DATA_END)
1262 return;
1263
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001264 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1265 "though no data operation was in progress.\n",
1266 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001267 sdhci_dumpregs(host);
1268
1269 return;
1270 }
1271
1272 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001273 host->data->error = -ETIMEDOUT;
1274 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1275 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001276 else if (intmask & SDHCI_INT_ADMA_ERROR)
1277 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278
Pierre Ossman17b04292007-07-22 22:18:46 +02001279 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001280 sdhci_finish_data(host);
1281 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001282 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283 sdhci_transfer_pio(host);
1284
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001285 /*
1286 * We currently don't do anything fancy with DMA
1287 * boundaries, but as we can't disable the feature
1288 * we need to at least restart the transfer.
1289 */
1290 if (intmask & SDHCI_INT_DMA_END)
1291 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1292 host->ioaddr + SDHCI_DMA_ADDRESS);
1293
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001294 if (intmask & SDHCI_INT_DATA_END) {
1295 if (host->cmd) {
1296 /*
1297 * Data managed to finish before the
1298 * command completed. Make sure we do
1299 * things in the proper order.
1300 */
1301 host->data_early = 1;
1302 } else {
1303 sdhci_finish_data(host);
1304 }
1305 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 }
1307}
1308
David Howells7d12e782006-10-05 14:55:46 +01001309static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310{
1311 irqreturn_t result;
1312 struct sdhci_host* host = dev_id;
1313 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001314 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315
1316 spin_lock(&host->lock);
1317
1318 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1319
Mark Lord62df67a52007-03-06 13:30:13 +01001320 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001321 result = IRQ_NONE;
1322 goto out;
1323 }
1324
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001325 DBG("*** %s got interrupt: 0x%08x\n",
1326 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
Pierre Ossman3192a282006-06-30 02:22:26 -07001328 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1329 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1330 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001331 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001332 }
1333
1334 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335
1336 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 writel(intmask & SDHCI_INT_CMD_MASK,
1338 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001339 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 }
1341
1342 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343 writel(intmask & SDHCI_INT_DATA_MASK,
1344 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001345 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346 }
1347
1348 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1349
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001350 intmask &= ~SDHCI_INT_ERROR;
1351
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001353 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001354 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001355 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356 }
1357
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001358 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001359
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001360 if (intmask & SDHCI_INT_CARD_INT)
1361 cardint = 1;
1362
1363 intmask &= ~SDHCI_INT_CARD_INT;
1364
Pierre Ossman3192a282006-06-30 02:22:26 -07001365 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001366 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001367 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 sdhci_dumpregs(host);
1369
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001371 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372
1373 result = IRQ_HANDLED;
1374
Pierre Ossman5f25a662006-10-04 02:15:39 -07001375 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376out:
1377 spin_unlock(&host->lock);
1378
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001379 /*
1380 * We have to delay this as it calls back into the driver.
1381 */
1382 if (cardint)
1383 mmc_signal_sdio_irq(host->mmc);
1384
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 return result;
1386}
1387
1388/*****************************************************************************\
1389 * *
1390 * Suspend/resume *
1391 * *
1392\*****************************************************************************/
1393
1394#ifdef CONFIG_PM
1395
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001396int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001398 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001399
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001400 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001401 if (ret)
1402 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001404 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001405
1406 return 0;
1407}
1408
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001409EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001411int sdhci_resume_host(struct sdhci_host *host)
1412{
1413 int ret;
1414
1415 if (host->flags & SDHCI_USE_DMA) {
1416 if (host->ops->enable_dma)
1417 host->ops->enable_dma(host);
1418 }
1419
1420 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1421 mmc_hostname(host->mmc), host);
1422 if (ret)
1423 return ret;
1424
1425 sdhci_init(host);
1426 mmiowb();
1427
1428 ret = mmc_resume_host(host->mmc);
1429 if (ret)
1430 return ret;
1431
1432 return 0;
1433}
1434
1435EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436
1437#endif /* CONFIG_PM */
1438
1439/*****************************************************************************\
1440 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001441 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442 * *
1443\*****************************************************************************/
1444
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001445struct sdhci_host *sdhci_alloc_host(struct device *dev,
1446 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448 struct mmc_host *mmc;
1449 struct sdhci_host *host;
1450
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001451 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001453 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001455 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456
1457 host = mmc_priv(mmc);
1458 host->mmc = mmc;
1459
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001460 return host;
1461}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001462
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001463EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001465int sdhci_add_host(struct sdhci_host *host)
1466{
1467 struct mmc_host *mmc;
1468 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001469 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001471 WARN_ON(host == NULL);
1472 if (host == NULL)
1473 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001475 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001477 if (debug_quirks)
1478 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479
Pierre Ossmand96649e2006-06-30 02:22:30 -07001480 sdhci_reset(host, SDHCI_RESET_ALL);
1481
Pierre Ossman2134a922008-06-28 18:28:51 +02001482 host->version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1483 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1484 >> SDHCI_SPEC_VER_SHIFT;
1485 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001486 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001487 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001488 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001489 }
1490
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1492
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001493 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001494 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001495 else if (!(caps & SDHCI_CAN_DO_DMA))
1496 DBG("Controller doesn't have DMA capability\n");
1497 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498 host->flags |= SDHCI_USE_DMA;
1499
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001500 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001501 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001502 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001503 host->flags &= ~SDHCI_USE_DMA;
1504 }
1505
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001507 if ((host->version >= SDHCI_SPEC_200) &&
1508 (caps & SDHCI_CAN_DO_ADMA2))
1509 host->flags |= SDHCI_USE_ADMA;
1510 }
1511
1512 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1513 (host->flags & SDHCI_USE_ADMA)) {
1514 DBG("Disabling ADMA as it is marked broken\n");
1515 host->flags &= ~SDHCI_USE_ADMA;
1516 }
1517
1518 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001519 if (host->ops->enable_dma) {
1520 if (host->ops->enable_dma(host)) {
1521 printk(KERN_WARNING "%s: No suitable DMA "
1522 "available. Falling back to PIO.\n",
1523 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001524 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001525 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526 }
1527 }
1528
Pierre Ossman2134a922008-06-28 18:28:51 +02001529 if (host->flags & SDHCI_USE_ADMA) {
1530 /*
1531 * We need to allocate descriptors for all sg entries
1532 * (128) and potentially one alignment transfer for
1533 * each of those entries.
1534 */
1535 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1536 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1537 if (!host->adma_desc || !host->align_buffer) {
1538 kfree(host->adma_desc);
1539 kfree(host->align_buffer);
1540 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1541 "buffers. Falling back to standard DMA.\n",
1542 mmc_hostname(mmc));
1543 host->flags &= ~SDHCI_USE_ADMA;
1544 }
1545 }
1546
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001547 /* XXX: Hack to get MMC layer to avoid highmem */
1548 if (!(host->flags & SDHCI_USE_DMA))
1549 mmc_dev(host->mmc)->dma_mask = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001550
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001551 host->max_clk =
1552 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1553 if (host->max_clk == 0) {
1554 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001555 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001556 return -ENODEV;
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001557 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001558 host->max_clk *= 1000000;
1559
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001560 host->timeout_clk =
1561 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1562 if (host->timeout_clk == 0) {
1563 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001564 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001565 return -ENODEV;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001566 }
1567 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1568 host->timeout_clk *= 1000;
1569
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570 /*
1571 * Set host parameters.
1572 */
1573 mmc->ops = &sdhci_ops;
1574 mmc->f_min = host->max_clk / 256;
1575 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001576 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001577
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001578 if (caps & SDHCI_CAN_DO_HISPD)
1579 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1580
Pierre Ossman146ad662006-06-30 02:22:23 -07001581 mmc->ocr_avail = 0;
1582 if (caps & SDHCI_CAN_VDD_330)
1583 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001584 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001585 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001586 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001587 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001588
1589 if (mmc->ocr_avail == 0) {
1590 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001591 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001592 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001593 }
1594
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595 spin_lock_init(&host->lock);
1596
1597 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001598 * Maximum number of segments. Depends on if the hardware
1599 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001601 if (host->flags & SDHCI_USE_ADMA)
1602 mmc->max_hw_segs = 128;
1603 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001604 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001605 else /* PIO */
1606 mmc->max_hw_segs = 128;
1607 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608
1609 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001610 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001611 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001613 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001614
1615 /*
1616 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001617 * of bytes. When doing hardware scatter/gather, each entry cannot
1618 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001620 if (host->flags & SDHCI_USE_ADMA)
1621 mmc->max_seg_size = 65536;
1622 else
1623 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
1625 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001626 * Maximum block size. This varies from controller to controller and
1627 * is specified in the capabilities register.
1628 */
1629 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1630 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001631 printk(KERN_WARNING "%s: Invalid maximum block size, "
1632 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001633 mmc->max_blk_size = 512;
1634 } else
1635 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001636
1637 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001638 * Maximum block count.
1639 */
1640 mmc->max_blk_count = 65535;
1641
1642 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643 * Init tasklets.
1644 */
1645 tasklet_init(&host->card_tasklet,
1646 sdhci_tasklet_card, (unsigned long)host);
1647 tasklet_init(&host->finish_tasklet,
1648 sdhci_tasklet_finish, (unsigned long)host);
1649
Al Viroe4cad1b2006-10-10 22:47:07 +01001650 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001652 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001653 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001654 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001655 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001656
1657 sdhci_init(host);
1658
1659#ifdef CONFIG_MMC_DEBUG
1660 sdhci_dumpregs(host);
1661#endif
1662
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001663#ifdef CONFIG_LEDS_CLASS
1664 host->led.name = mmc_hostname(mmc);
1665 host->led.brightness = LED_OFF;
1666 host->led.default_trigger = mmc_hostname(mmc);
1667 host->led.brightness_set = sdhci_led_control;
1668
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001669 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001670 if (ret)
1671 goto reset;
1672#endif
1673
Pierre Ossman5f25a662006-10-04 02:15:39 -07001674 mmiowb();
1675
Pierre Ossmand129bce2006-03-24 03:18:17 -08001676 mmc_add_host(mmc);
1677
Pierre Ossman2134a922008-06-28 18:28:51 +02001678 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001679 mmc_hostname(mmc), host->hw_name, mmc_dev(mmc)->bus_id,
Pierre Ossman2134a922008-06-28 18:28:51 +02001680 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1682
1683 return 0;
1684
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001685#ifdef CONFIG_LEDS_CLASS
1686reset:
1687 sdhci_reset(host, SDHCI_RESET_ALL);
1688 free_irq(host->irq, host);
1689#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001690untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001691 tasklet_kill(&host->card_tasklet);
1692 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693
1694 return ret;
1695}
1696
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001697EXPORT_SYMBOL_GPL(sdhci_add_host);
1698
Pierre Ossman1e728592008-04-16 19:13:13 +02001699void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001700{
Pierre Ossman1e728592008-04-16 19:13:13 +02001701 unsigned long flags;
1702
1703 if (dead) {
1704 spin_lock_irqsave(&host->lock, flags);
1705
1706 host->flags |= SDHCI_DEVICE_DEAD;
1707
1708 if (host->mrq) {
1709 printk(KERN_ERR "%s: Controller removed during "
1710 " transfer!\n", mmc_hostname(host->mmc));
1711
1712 host->mrq->cmd->error = -ENOMEDIUM;
1713 tasklet_schedule(&host->finish_tasklet);
1714 }
1715
1716 spin_unlock_irqrestore(&host->lock, flags);
1717 }
1718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001719 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001720
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001721#ifdef CONFIG_LEDS_CLASS
1722 led_classdev_unregister(&host->led);
1723#endif
1724
Pierre Ossman1e728592008-04-16 19:13:13 +02001725 if (!dead)
1726 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001727
1728 free_irq(host->irq, host);
1729
1730 del_timer_sync(&host->timer);
1731
1732 tasklet_kill(&host->card_tasklet);
1733 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001734
1735 kfree(host->adma_desc);
1736 kfree(host->align_buffer);
1737
1738 host->adma_desc = NULL;
1739 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001740}
1741
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001742EXPORT_SYMBOL_GPL(sdhci_remove_host);
1743
1744void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001745{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001746 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001747}
1748
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001749EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750
1751/*****************************************************************************\
1752 * *
1753 * Driver init/exit *
1754 * *
1755\*****************************************************************************/
1756
1757static int __init sdhci_drv_init(void)
1758{
1759 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001760 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001761 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1762
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001763 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764}
1765
1766static void __exit sdhci_drv_exit(void)
1767{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768}
1769
1770module_init(sdhci_drv_init);
1771module_exit(sdhci_drv_exit);
1772
Pierre Ossmandf673b22006-06-30 02:22:31 -07001773module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001774
Pierre Ossmand129bce2006-03-24 03:18:17 -08001775MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001776MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001777MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001778
Pierre Ossmandf673b22006-06-30 02:22:31 -07001779MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");