Merge branch 'rbd-sysfs' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[pandora-kernel.git] / drivers / mmc / host / sdhci.c
1 /*
2  *  linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
3  *
4  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * 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.
10  *
11  * Thanks to the following companies for their support:
12  *
13  *     - JMicron (hardware and technical support)
14  */
15
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
18 #include <linux/io.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/slab.h>
21 #include <linux/scatterlist.h>
22 #include <linux/regulator/consumer.h>
23
24 #include <linux/leds.h>
25
26 #include <linux/mmc/host.h>
27
28 #include "sdhci.h"
29
30 #define DRIVER_NAME "sdhci"
31
32 #define DBG(f, x...) \
33         pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
34
35 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
36         defined(CONFIG_MMC_SDHCI_MODULE))
37 #define SDHCI_USE_LEDS_CLASS
38 #endif
39
40 static unsigned int debug_quirks = 0;
41
42 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
43 static void sdhci_finish_data(struct sdhci_host *);
44
45 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46 static void sdhci_finish_command(struct sdhci_host *);
47
48 static void sdhci_dumpregs(struct sdhci_host *host)
49 {
50         printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
51                 mmc_hostname(host->mmc));
52
53         printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version:  0x%08x\n",
54                 sdhci_readl(host, SDHCI_DMA_ADDRESS),
55                 sdhci_readw(host, SDHCI_HOST_VERSION));
56         printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt:  0x%08x\n",
57                 sdhci_readw(host, SDHCI_BLOCK_SIZE),
58                 sdhci_readw(host, SDHCI_BLOCK_COUNT));
59         printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
60                 sdhci_readl(host, SDHCI_ARGUMENT),
61                 sdhci_readw(host, SDHCI_TRANSFER_MODE));
62         printk(KERN_DEBUG DRIVER_NAME ": Present:  0x%08x | Host ctl: 0x%08x\n",
63                 sdhci_readl(host, SDHCI_PRESENT_STATE),
64                 sdhci_readb(host, SDHCI_HOST_CONTROL));
65         printk(KERN_DEBUG DRIVER_NAME ": Power:    0x%08x | Blk gap:  0x%08x\n",
66                 sdhci_readb(host, SDHCI_POWER_CONTROL),
67                 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
68         printk(KERN_DEBUG DRIVER_NAME ": Wake-up:  0x%08x | Clock:    0x%08x\n",
69                 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
70                 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
71         printk(KERN_DEBUG DRIVER_NAME ": Timeout:  0x%08x | Int stat: 0x%08x\n",
72                 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
73                 sdhci_readl(host, SDHCI_INT_STATUS));
74         printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
75                 sdhci_readl(host, SDHCI_INT_ENABLE),
76                 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
77         printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
78                 sdhci_readw(host, SDHCI_ACMD12_ERR),
79                 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
80         printk(KERN_DEBUG DRIVER_NAME ": Caps:     0x%08x | Max curr: 0x%08x\n",
81                 sdhci_readl(host, SDHCI_CAPABILITIES),
82                 sdhci_readl(host, SDHCI_MAX_CURRENT));
83
84         if (host->flags & SDHCI_USE_ADMA)
85                 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
86                        readl(host->ioaddr + SDHCI_ADMA_ERROR),
87                        readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
88
89         printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
90 }
91
92 /*****************************************************************************\
93  *                                                                           *
94  * Low level functions                                                       *
95  *                                                                           *
96 \*****************************************************************************/
97
98 static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
99 {
100         u32 ier;
101
102         ier = sdhci_readl(host, SDHCI_INT_ENABLE);
103         ier &= ~clear;
104         ier |= set;
105         sdhci_writel(host, ier, SDHCI_INT_ENABLE);
106         sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
107 }
108
109 static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
110 {
111         sdhci_clear_set_irqs(host, 0, irqs);
112 }
113
114 static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
115 {
116         sdhci_clear_set_irqs(host, irqs, 0);
117 }
118
119 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
120 {
121         u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
122
123         if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
124                 return;
125
126         if (enable)
127                 sdhci_unmask_irqs(host, irqs);
128         else
129                 sdhci_mask_irqs(host, irqs);
130 }
131
132 static void sdhci_enable_card_detection(struct sdhci_host *host)
133 {
134         sdhci_set_card_detection(host, true);
135 }
136
137 static void sdhci_disable_card_detection(struct sdhci_host *host)
138 {
139         sdhci_set_card_detection(host, false);
140 }
141
142 static void sdhci_reset(struct sdhci_host *host, u8 mask)
143 {
144         unsigned long timeout;
145         u32 uninitialized_var(ier);
146
147         if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
148                 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
149                         SDHCI_CARD_PRESENT))
150                         return;
151         }
152
153         if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
154                 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
155
156         sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
157
158         if (mask & SDHCI_RESET_ALL)
159                 host->clock = 0;
160
161         /* Wait max 100 ms */
162         timeout = 100;
163
164         /* hw clears the bit when it's done */
165         while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
166                 if (timeout == 0) {
167                         printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
168                                 mmc_hostname(host->mmc), (int)mask);
169                         sdhci_dumpregs(host);
170                         return;
171                 }
172                 timeout--;
173                 mdelay(1);
174         }
175
176         if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
177                 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
178 }
179
180 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
181
182 static void sdhci_init(struct sdhci_host *host, int soft)
183 {
184         if (soft)
185                 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
186         else
187                 sdhci_reset(host, SDHCI_RESET_ALL);
188
189         sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
190                 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
191                 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
192                 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
193                 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
194
195         if (soft) {
196                 /* force clock reconfiguration */
197                 host->clock = 0;
198                 sdhci_set_ios(host->mmc, &host->mmc->ios);
199         }
200 }
201
202 static void sdhci_reinit(struct sdhci_host *host)
203 {
204         sdhci_init(host, 0);
205         sdhci_enable_card_detection(host);
206 }
207
208 static void sdhci_activate_led(struct sdhci_host *host)
209 {
210         u8 ctrl;
211
212         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
213         ctrl |= SDHCI_CTRL_LED;
214         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
215 }
216
217 static void sdhci_deactivate_led(struct sdhci_host *host)
218 {
219         u8 ctrl;
220
221         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
222         ctrl &= ~SDHCI_CTRL_LED;
223         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
224 }
225
226 #ifdef SDHCI_USE_LEDS_CLASS
227 static void sdhci_led_control(struct led_classdev *led,
228         enum led_brightness brightness)
229 {
230         struct sdhci_host *host = container_of(led, struct sdhci_host, led);
231         unsigned long flags;
232
233         spin_lock_irqsave(&host->lock, flags);
234
235         if (brightness == LED_OFF)
236                 sdhci_deactivate_led(host);
237         else
238                 sdhci_activate_led(host);
239
240         spin_unlock_irqrestore(&host->lock, flags);
241 }
242 #endif
243
244 /*****************************************************************************\
245  *                                                                           *
246  * Core functions                                                            *
247  *                                                                           *
248 \*****************************************************************************/
249
250 static void sdhci_read_block_pio(struct sdhci_host *host)
251 {
252         unsigned long flags;
253         size_t blksize, len, chunk;
254         u32 uninitialized_var(scratch);
255         u8 *buf;
256
257         DBG("PIO reading\n");
258
259         blksize = host->data->blksz;
260         chunk = 0;
261
262         local_irq_save(flags);
263
264         while (blksize) {
265                 if (!sg_miter_next(&host->sg_miter))
266                         BUG();
267
268                 len = min(host->sg_miter.length, blksize);
269
270                 blksize -= len;
271                 host->sg_miter.consumed = len;
272
273                 buf = host->sg_miter.addr;
274
275                 while (len) {
276                         if (chunk == 0) {
277                                 scratch = sdhci_readl(host, SDHCI_BUFFER);
278                                 chunk = 4;
279                         }
280
281                         *buf = scratch & 0xFF;
282
283                         buf++;
284                         scratch >>= 8;
285                         chunk--;
286                         len--;
287                 }
288         }
289
290         sg_miter_stop(&host->sg_miter);
291
292         local_irq_restore(flags);
293 }
294
295 static void sdhci_write_block_pio(struct sdhci_host *host)
296 {
297         unsigned long flags;
298         size_t blksize, len, chunk;
299         u32 scratch;
300         u8 *buf;
301
302         DBG("PIO writing\n");
303
304         blksize = host->data->blksz;
305         chunk = 0;
306         scratch = 0;
307
308         local_irq_save(flags);
309
310         while (blksize) {
311                 if (!sg_miter_next(&host->sg_miter))
312                         BUG();
313
314                 len = min(host->sg_miter.length, blksize);
315
316                 blksize -= len;
317                 host->sg_miter.consumed = len;
318
319                 buf = host->sg_miter.addr;
320
321                 while (len) {
322                         scratch |= (u32)*buf << (chunk * 8);
323
324                         buf++;
325                         chunk++;
326                         len--;
327
328                         if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
329                                 sdhci_writel(host, scratch, SDHCI_BUFFER);
330                                 chunk = 0;
331                                 scratch = 0;
332                         }
333                 }
334         }
335
336         sg_miter_stop(&host->sg_miter);
337
338         local_irq_restore(flags);
339 }
340
341 static void sdhci_transfer_pio(struct sdhci_host *host)
342 {
343         u32 mask;
344
345         BUG_ON(!host->data);
346
347         if (host->blocks == 0)
348                 return;
349
350         if (host->data->flags & MMC_DATA_READ)
351                 mask = SDHCI_DATA_AVAILABLE;
352         else
353                 mask = SDHCI_SPACE_AVAILABLE;
354
355         /*
356          * Some controllers (JMicron JMB38x) mess up the buffer bits
357          * for transfers < 4 bytes. As long as it is just one block,
358          * we can ignore the bits.
359          */
360         if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
361                 (host->data->blocks == 1))
362                 mask = ~0;
363
364         while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
365                 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
366                         udelay(100);
367
368                 if (host->data->flags & MMC_DATA_READ)
369                         sdhci_read_block_pio(host);
370                 else
371                         sdhci_write_block_pio(host);
372
373                 host->blocks--;
374                 if (host->blocks == 0)
375                         break;
376         }
377
378         DBG("PIO transfer complete.\n");
379 }
380
381 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
382 {
383         local_irq_save(*flags);
384         return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
385 }
386
387 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
388 {
389         kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
390         local_irq_restore(*flags);
391 }
392
393 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
394 {
395         __le32 *dataddr = (__le32 __force *)(desc + 4);
396         __le16 *cmdlen = (__le16 __force *)desc;
397
398         /* SDHCI specification says ADMA descriptors should be 4 byte
399          * aligned, so using 16 or 32bit operations should be safe. */
400
401         cmdlen[0] = cpu_to_le16(cmd);
402         cmdlen[1] = cpu_to_le16(len);
403
404         dataddr[0] = cpu_to_le32(addr);
405 }
406
407 static int sdhci_adma_table_pre(struct sdhci_host *host,
408         struct mmc_data *data)
409 {
410         int direction;
411
412         u8 *desc;
413         u8 *align;
414         dma_addr_t addr;
415         dma_addr_t align_addr;
416         int len, offset;
417
418         struct scatterlist *sg;
419         int i;
420         char *buffer;
421         unsigned long flags;
422
423         /*
424          * The spec does not specify endianness of descriptor table.
425          * We currently guess that it is LE.
426          */
427
428         if (data->flags & MMC_DATA_READ)
429                 direction = DMA_FROM_DEVICE;
430         else
431                 direction = DMA_TO_DEVICE;
432
433         /*
434          * The ADMA descriptor table is mapped further down as we
435          * need to fill it with data first.
436          */
437
438         host->align_addr = dma_map_single(mmc_dev(host->mmc),
439                 host->align_buffer, 128 * 4, direction);
440         if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
441                 goto fail;
442         BUG_ON(host->align_addr & 0x3);
443
444         host->sg_count = dma_map_sg(mmc_dev(host->mmc),
445                 data->sg, data->sg_len, direction);
446         if (host->sg_count == 0)
447                 goto unmap_align;
448
449         desc = host->adma_desc;
450         align = host->align_buffer;
451
452         align_addr = host->align_addr;
453
454         for_each_sg(data->sg, sg, host->sg_count, i) {
455                 addr = sg_dma_address(sg);
456                 len = sg_dma_len(sg);
457
458                 /*
459                  * The SDHCI specification states that ADMA
460                  * addresses must be 32-bit aligned. If they
461                  * aren't, then we use a bounce buffer for
462                  * the (up to three) bytes that screw up the
463                  * alignment.
464                  */
465                 offset = (4 - (addr & 0x3)) & 0x3;
466                 if (offset) {
467                         if (data->flags & MMC_DATA_WRITE) {
468                                 buffer = sdhci_kmap_atomic(sg, &flags);
469                                 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
470                                 memcpy(align, buffer, offset);
471                                 sdhci_kunmap_atomic(buffer, &flags);
472                         }
473
474                         /* tran, valid */
475                         sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
476
477                         BUG_ON(offset > 65536);
478
479                         align += 4;
480                         align_addr += 4;
481
482                         desc += 8;
483
484                         addr += offset;
485                         len -= offset;
486                 }
487
488                 BUG_ON(len > 65536);
489
490                 /* tran, valid */
491                 sdhci_set_adma_desc(desc, addr, len, 0x21);
492                 desc += 8;
493
494                 /*
495                  * If this triggers then we have a calculation bug
496                  * somewhere. :/
497                  */
498                 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
499         }
500
501         if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
502                 /*
503                 * Mark the last descriptor as the terminating descriptor
504                 */
505                 if (desc != host->adma_desc) {
506                         desc -= 8;
507                         desc[0] |= 0x2; /* end */
508                 }
509         } else {
510                 /*
511                 * Add a terminating entry.
512                 */
513
514                 /* nop, end, valid */
515                 sdhci_set_adma_desc(desc, 0, 0, 0x3);
516         }
517
518         /*
519          * Resync align buffer as we might have changed it.
520          */
521         if (data->flags & MMC_DATA_WRITE) {
522                 dma_sync_single_for_device(mmc_dev(host->mmc),
523                         host->align_addr, 128 * 4, direction);
524         }
525
526         host->adma_addr = dma_map_single(mmc_dev(host->mmc),
527                 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
528         if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
529                 goto unmap_entries;
530         BUG_ON(host->adma_addr & 0x3);
531
532         return 0;
533
534 unmap_entries:
535         dma_unmap_sg(mmc_dev(host->mmc), data->sg,
536                 data->sg_len, direction);
537 unmap_align:
538         dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
539                 128 * 4, direction);
540 fail:
541         return -EINVAL;
542 }
543
544 static void sdhci_adma_table_post(struct sdhci_host *host,
545         struct mmc_data *data)
546 {
547         int direction;
548
549         struct scatterlist *sg;
550         int i, size;
551         u8 *align;
552         char *buffer;
553         unsigned long flags;
554
555         if (data->flags & MMC_DATA_READ)
556                 direction = DMA_FROM_DEVICE;
557         else
558                 direction = DMA_TO_DEVICE;
559
560         dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
561                 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
562
563         dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
564                 128 * 4, direction);
565
566         if (data->flags & MMC_DATA_READ) {
567                 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
568                         data->sg_len, direction);
569
570                 align = host->align_buffer;
571
572                 for_each_sg(data->sg, sg, host->sg_count, i) {
573                         if (sg_dma_address(sg) & 0x3) {
574                                 size = 4 - (sg_dma_address(sg) & 0x3);
575
576                                 buffer = sdhci_kmap_atomic(sg, &flags);
577                                 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
578                                 memcpy(buffer, align, size);
579                                 sdhci_kunmap_atomic(buffer, &flags);
580
581                                 align += 4;
582                         }
583                 }
584         }
585
586         dma_unmap_sg(mmc_dev(host->mmc), data->sg,
587                 data->sg_len, direction);
588 }
589
590 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
591 {
592         u8 count;
593         unsigned target_timeout, current_timeout;
594
595         /*
596          * If the host controller provides us with an incorrect timeout
597          * value, just skip the check and use 0xE.  The hardware may take
598          * longer to time out, but that's much better than having a too-short
599          * timeout value.
600          */
601         if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
602                 return 0xE;
603
604         /* timeout in us */
605         target_timeout = data->timeout_ns / 1000 +
606                 data->timeout_clks / host->clock;
607
608         if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
609                 host->timeout_clk = host->clock / 1000;
610
611         /*
612          * Figure out needed cycles.
613          * We do this in steps in order to fit inside a 32 bit int.
614          * The first step is the minimum timeout, which will have a
615          * minimum resolution of 6 bits:
616          * (1) 2^13*1000 > 2^22,
617          * (2) host->timeout_clk < 2^16
618          *     =>
619          *     (1) / (2) > 2^6
620          */
621         count = 0;
622         current_timeout = (1 << 13) * 1000 / host->timeout_clk;
623         while (current_timeout < target_timeout) {
624                 count++;
625                 current_timeout <<= 1;
626                 if (count >= 0xF)
627                         break;
628         }
629
630         if (count >= 0xF) {
631                 printk(KERN_WARNING "%s: Too large timeout requested!\n",
632                         mmc_hostname(host->mmc));
633                 count = 0xE;
634         }
635
636         return count;
637 }
638
639 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
640 {
641         u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
642         u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
643
644         if (host->flags & SDHCI_REQ_USE_DMA)
645                 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
646         else
647                 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
648 }
649
650 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
651 {
652         u8 count;
653         u8 ctrl;
654         int ret;
655
656         WARN_ON(host->data);
657
658         if (data == NULL)
659                 return;
660
661         /* Sanity checks */
662         BUG_ON(data->blksz * data->blocks > 524288);
663         BUG_ON(data->blksz > host->mmc->max_blk_size);
664         BUG_ON(data->blocks > 65535);
665
666         host->data = data;
667         host->data_early = 0;
668
669         count = sdhci_calc_timeout(host, data);
670         sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
671
672         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
673                 host->flags |= SDHCI_REQ_USE_DMA;
674
675         /*
676          * FIXME: This doesn't account for merging when mapping the
677          * scatterlist.
678          */
679         if (host->flags & SDHCI_REQ_USE_DMA) {
680                 int broken, i;
681                 struct scatterlist *sg;
682
683                 broken = 0;
684                 if (host->flags & SDHCI_USE_ADMA) {
685                         if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
686                                 broken = 1;
687                 } else {
688                         if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
689                                 broken = 1;
690                 }
691
692                 if (unlikely(broken)) {
693                         for_each_sg(data->sg, sg, data->sg_len, i) {
694                                 if (sg->length & 0x3) {
695                                         DBG("Reverting to PIO because of "
696                                                 "transfer size (%d)\n",
697                                                 sg->length);
698                                         host->flags &= ~SDHCI_REQ_USE_DMA;
699                                         break;
700                                 }
701                         }
702                 }
703         }
704
705         /*
706          * The assumption here being that alignment is the same after
707          * translation to device address space.
708          */
709         if (host->flags & SDHCI_REQ_USE_DMA) {
710                 int broken, i;
711                 struct scatterlist *sg;
712
713                 broken = 0;
714                 if (host->flags & SDHCI_USE_ADMA) {
715                         /*
716                          * As we use 3 byte chunks to work around
717                          * alignment problems, we need to check this
718                          * quirk.
719                          */
720                         if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
721                                 broken = 1;
722                 } else {
723                         if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
724                                 broken = 1;
725                 }
726
727                 if (unlikely(broken)) {
728                         for_each_sg(data->sg, sg, data->sg_len, i) {
729                                 if (sg->offset & 0x3) {
730                                         DBG("Reverting to PIO because of "
731                                                 "bad alignment\n");
732                                         host->flags &= ~SDHCI_REQ_USE_DMA;
733                                         break;
734                                 }
735                         }
736                 }
737         }
738
739         if (host->flags & SDHCI_REQ_USE_DMA) {
740                 if (host->flags & SDHCI_USE_ADMA) {
741                         ret = sdhci_adma_table_pre(host, data);
742                         if (ret) {
743                                 /*
744                                  * This only happens when someone fed
745                                  * us an invalid request.
746                                  */
747                                 WARN_ON(1);
748                                 host->flags &= ~SDHCI_REQ_USE_DMA;
749                         } else {
750                                 sdhci_writel(host, host->adma_addr,
751                                         SDHCI_ADMA_ADDRESS);
752                         }
753                 } else {
754                         int sg_cnt;
755
756                         sg_cnt = dma_map_sg(mmc_dev(host->mmc),
757                                         data->sg, data->sg_len,
758                                         (data->flags & MMC_DATA_READ) ?
759                                                 DMA_FROM_DEVICE :
760                                                 DMA_TO_DEVICE);
761                         if (sg_cnt == 0) {
762                                 /*
763                                  * This only happens when someone fed
764                                  * us an invalid request.
765                                  */
766                                 WARN_ON(1);
767                                 host->flags &= ~SDHCI_REQ_USE_DMA;
768                         } else {
769                                 WARN_ON(sg_cnt != 1);
770                                 sdhci_writel(host, sg_dma_address(data->sg),
771                                         SDHCI_DMA_ADDRESS);
772                         }
773                 }
774         }
775
776         /*
777          * Always adjust the DMA selection as some controllers
778          * (e.g. JMicron) can't do PIO properly when the selection
779          * is ADMA.
780          */
781         if (host->version >= SDHCI_SPEC_200) {
782                 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
783                 ctrl &= ~SDHCI_CTRL_DMA_MASK;
784                 if ((host->flags & SDHCI_REQ_USE_DMA) &&
785                         (host->flags & SDHCI_USE_ADMA))
786                         ctrl |= SDHCI_CTRL_ADMA32;
787                 else
788                         ctrl |= SDHCI_CTRL_SDMA;
789                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
790         }
791
792         if (!(host->flags & SDHCI_REQ_USE_DMA)) {
793                 int flags;
794
795                 flags = SG_MITER_ATOMIC;
796                 if (host->data->flags & MMC_DATA_READ)
797                         flags |= SG_MITER_TO_SG;
798                 else
799                         flags |= SG_MITER_FROM_SG;
800                 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
801                 host->blocks = data->blocks;
802         }
803
804         sdhci_set_transfer_irqs(host);
805
806         /* We do not handle DMA boundaries, so set it to max (512 KiB) */
807         sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
808         sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
809 }
810
811 static void sdhci_set_transfer_mode(struct sdhci_host *host,
812         struct mmc_data *data)
813 {
814         u16 mode;
815
816         if (data == NULL)
817                 return;
818
819         WARN_ON(!host->data);
820
821         mode = SDHCI_TRNS_BLK_CNT_EN;
822         if (data->blocks > 1) {
823                 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
824                         mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
825                 else
826                         mode |= SDHCI_TRNS_MULTI;
827         }
828         if (data->flags & MMC_DATA_READ)
829                 mode |= SDHCI_TRNS_READ;
830         if (host->flags & SDHCI_REQ_USE_DMA)
831                 mode |= SDHCI_TRNS_DMA;
832
833         sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
834 }
835
836 static void sdhci_finish_data(struct sdhci_host *host)
837 {
838         struct mmc_data *data;
839
840         BUG_ON(!host->data);
841
842         data = host->data;
843         host->data = NULL;
844
845         if (host->flags & SDHCI_REQ_USE_DMA) {
846                 if (host->flags & SDHCI_USE_ADMA)
847                         sdhci_adma_table_post(host, data);
848                 else {
849                         dma_unmap_sg(mmc_dev(host->mmc), data->sg,
850                                 data->sg_len, (data->flags & MMC_DATA_READ) ?
851                                         DMA_FROM_DEVICE : DMA_TO_DEVICE);
852                 }
853         }
854
855         /*
856          * The specification states that the block count register must
857          * be updated, but it does not specify at what point in the
858          * data flow. That makes the register entirely useless to read
859          * back so we have to assume that nothing made it to the card
860          * in the event of an error.
861          */
862         if (data->error)
863                 data->bytes_xfered = 0;
864         else
865                 data->bytes_xfered = data->blksz * data->blocks;
866
867         if (data->stop) {
868                 /*
869                  * The controller needs a reset of internal state machines
870                  * upon error conditions.
871                  */
872                 if (data->error) {
873                         sdhci_reset(host, SDHCI_RESET_CMD);
874                         sdhci_reset(host, SDHCI_RESET_DATA);
875                 }
876
877                 sdhci_send_command(host, data->stop);
878         } else
879                 tasklet_schedule(&host->finish_tasklet);
880 }
881
882 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
883 {
884         int flags;
885         u32 mask;
886         unsigned long timeout;
887
888         WARN_ON(host->cmd);
889
890         /* Wait max 10 ms */
891         timeout = 10;
892
893         mask = SDHCI_CMD_INHIBIT;
894         if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
895                 mask |= SDHCI_DATA_INHIBIT;
896
897         /* We shouldn't wait for data inihibit for stop commands, even
898            though they might use busy signaling */
899         if (host->mrq->data && (cmd == host->mrq->data->stop))
900                 mask &= ~SDHCI_DATA_INHIBIT;
901
902         while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
903                 if (timeout == 0) {
904                         printk(KERN_ERR "%s: Controller never released "
905                                 "inhibit bit(s).\n", mmc_hostname(host->mmc));
906                         sdhci_dumpregs(host);
907                         cmd->error = -EIO;
908                         tasklet_schedule(&host->finish_tasklet);
909                         return;
910                 }
911                 timeout--;
912                 mdelay(1);
913         }
914
915         mod_timer(&host->timer, jiffies + 10 * HZ);
916
917         host->cmd = cmd;
918
919         sdhci_prepare_data(host, cmd->data);
920
921         sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
922
923         sdhci_set_transfer_mode(host, cmd->data);
924
925         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
926                 printk(KERN_ERR "%s: Unsupported response type!\n",
927                         mmc_hostname(host->mmc));
928                 cmd->error = -EINVAL;
929                 tasklet_schedule(&host->finish_tasklet);
930                 return;
931         }
932
933         if (!(cmd->flags & MMC_RSP_PRESENT))
934                 flags = SDHCI_CMD_RESP_NONE;
935         else if (cmd->flags & MMC_RSP_136)
936                 flags = SDHCI_CMD_RESP_LONG;
937         else if (cmd->flags & MMC_RSP_BUSY)
938                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
939         else
940                 flags = SDHCI_CMD_RESP_SHORT;
941
942         if (cmd->flags & MMC_RSP_CRC)
943                 flags |= SDHCI_CMD_CRC;
944         if (cmd->flags & MMC_RSP_OPCODE)
945                 flags |= SDHCI_CMD_INDEX;
946         if (cmd->data)
947                 flags |= SDHCI_CMD_DATA;
948
949         sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
950 }
951
952 static void sdhci_finish_command(struct sdhci_host *host)
953 {
954         int i;
955
956         BUG_ON(host->cmd == NULL);
957
958         if (host->cmd->flags & MMC_RSP_PRESENT) {
959                 if (host->cmd->flags & MMC_RSP_136) {
960                         /* CRC is stripped so we need to do some shifting. */
961                         for (i = 0;i < 4;i++) {
962                                 host->cmd->resp[i] = sdhci_readl(host,
963                                         SDHCI_RESPONSE + (3-i)*4) << 8;
964                                 if (i != 3)
965                                         host->cmd->resp[i] |=
966                                                 sdhci_readb(host,
967                                                 SDHCI_RESPONSE + (3-i)*4-1);
968                         }
969                 } else {
970                         host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
971                 }
972         }
973
974         host->cmd->error = 0;
975
976         if (host->data && host->data_early)
977                 sdhci_finish_data(host);
978
979         if (!host->cmd->data)
980                 tasklet_schedule(&host->finish_tasklet);
981
982         host->cmd = NULL;
983 }
984
985 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
986 {
987         int div;
988         u16 clk;
989         unsigned long timeout;
990
991         if (clock == host->clock)
992                 return;
993
994         if (host->ops->set_clock) {
995                 host->ops->set_clock(host, clock);
996                 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
997                         return;
998         }
999
1000         sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1001
1002         if (clock == 0)
1003                 goto out;
1004
1005         if (host->version >= SDHCI_SPEC_300) {
1006                 /* Version 3.00 divisors must be a multiple of 2. */
1007                 if (host->max_clk <= clock)
1008                         div = 1;
1009                 else {
1010                         for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
1011                                 if ((host->max_clk / div) <= clock)
1012                                         break;
1013                         }
1014                 }
1015         } else {
1016                 /* Version 2.00 divisors must be a power of 2. */
1017                 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1018                         if ((host->max_clk / div) <= clock)
1019                                 break;
1020                 }
1021         }
1022         div >>= 1;
1023
1024         clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1025         clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1026                 << SDHCI_DIVIDER_HI_SHIFT;
1027         clk |= SDHCI_CLOCK_INT_EN;
1028         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1029
1030         /* Wait max 20 ms */
1031         timeout = 20;
1032         while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1033                 & SDHCI_CLOCK_INT_STABLE)) {
1034                 if (timeout == 0) {
1035                         printk(KERN_ERR "%s: Internal clock never "
1036                                 "stabilised.\n", mmc_hostname(host->mmc));
1037                         sdhci_dumpregs(host);
1038                         return;
1039                 }
1040                 timeout--;
1041                 mdelay(1);
1042         }
1043
1044         clk |= SDHCI_CLOCK_CARD_EN;
1045         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1046
1047 out:
1048         host->clock = clock;
1049 }
1050
1051 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1052 {
1053         u8 pwr = 0;
1054
1055         if (power != (unsigned short)-1) {
1056                 switch (1 << power) {
1057                 case MMC_VDD_165_195:
1058                         pwr = SDHCI_POWER_180;
1059                         break;
1060                 case MMC_VDD_29_30:
1061                 case MMC_VDD_30_31:
1062                         pwr = SDHCI_POWER_300;
1063                         break;
1064                 case MMC_VDD_32_33:
1065                 case MMC_VDD_33_34:
1066                         pwr = SDHCI_POWER_330;
1067                         break;
1068                 default:
1069                         BUG();
1070                 }
1071         }
1072
1073         if (host->pwr == pwr)
1074                 return;
1075
1076         host->pwr = pwr;
1077
1078         if (pwr == 0) {
1079                 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1080                 return;
1081         }
1082
1083         /*
1084          * Spec says that we should clear the power reg before setting
1085          * a new value. Some controllers don't seem to like this though.
1086          */
1087         if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1088                 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1089
1090         /*
1091          * At least the Marvell CaFe chip gets confused if we set the voltage
1092          * and set turn on power at the same time, so set the voltage first.
1093          */
1094         if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1095                 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1096
1097         pwr |= SDHCI_POWER_ON;
1098
1099         sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1100
1101         /*
1102          * Some controllers need an extra 10ms delay of 10ms before they
1103          * can apply clock after applying power
1104          */
1105         if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1106                 mdelay(10);
1107 }
1108
1109 /*****************************************************************************\
1110  *                                                                           *
1111  * MMC callbacks                                                             *
1112  *                                                                           *
1113 \*****************************************************************************/
1114
1115 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1116 {
1117         struct sdhci_host *host;
1118         bool present;
1119         unsigned long flags;
1120
1121         host = mmc_priv(mmc);
1122
1123         spin_lock_irqsave(&host->lock, flags);
1124
1125         WARN_ON(host->mrq != NULL);
1126
1127 #ifndef SDHCI_USE_LEDS_CLASS
1128         sdhci_activate_led(host);
1129 #endif
1130         if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1131                 if (mrq->stop) {
1132                         mrq->data->stop = NULL;
1133                         mrq->stop = NULL;
1134                 }
1135         }
1136
1137         host->mrq = mrq;
1138
1139         /* If polling, assume that the card is always present. */
1140         if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1141                 present = true;
1142         else
1143                 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1144                                 SDHCI_CARD_PRESENT;
1145
1146         if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1147                 host->mrq->cmd->error = -ENOMEDIUM;
1148                 tasklet_schedule(&host->finish_tasklet);
1149         } else
1150                 sdhci_send_command(host, mrq->cmd);
1151
1152         mmiowb();
1153         spin_unlock_irqrestore(&host->lock, flags);
1154 }
1155
1156 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1157 {
1158         struct sdhci_host *host;
1159         unsigned long flags;
1160         u8 ctrl;
1161
1162         host = mmc_priv(mmc);
1163
1164         spin_lock_irqsave(&host->lock, flags);
1165
1166         if (host->flags & SDHCI_DEVICE_DEAD)
1167                 goto out;
1168
1169         /*
1170          * Reset the chip on each power off.
1171          * Should clear out any weird states.
1172          */
1173         if (ios->power_mode == MMC_POWER_OFF) {
1174                 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1175                 sdhci_reinit(host);
1176         }
1177
1178         sdhci_set_clock(host, ios->clock);
1179
1180         if (ios->power_mode == MMC_POWER_OFF)
1181                 sdhci_set_power(host, -1);
1182         else
1183                 sdhci_set_power(host, ios->vdd);
1184
1185         if (host->ops->platform_send_init_74_clocks)
1186                 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1187
1188         /*
1189          * If your platform has 8-bit width support but is not a v3 controller,
1190          * or if it requires special setup code, you should implement that in
1191          * platform_8bit_width().
1192          */
1193         if (host->ops->platform_8bit_width)
1194                 host->ops->platform_8bit_width(host, ios->bus_width);
1195         else {
1196                 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1197                 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1198                         ctrl &= ~SDHCI_CTRL_4BITBUS;
1199                         if (host->version >= SDHCI_SPEC_300)
1200                                 ctrl |= SDHCI_CTRL_8BITBUS;
1201                 } else {
1202                         if (host->version >= SDHCI_SPEC_300)
1203                                 ctrl &= ~SDHCI_CTRL_8BITBUS;
1204                         if (ios->bus_width == MMC_BUS_WIDTH_4)
1205                                 ctrl |= SDHCI_CTRL_4BITBUS;
1206                         else
1207                                 ctrl &= ~SDHCI_CTRL_4BITBUS;
1208                 }
1209                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1210         }
1211
1212         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1213
1214         if ((ios->timing == MMC_TIMING_SD_HS ||
1215              ios->timing == MMC_TIMING_MMC_HS)
1216             && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1217                 ctrl |= SDHCI_CTRL_HISPD;
1218         else
1219                 ctrl &= ~SDHCI_CTRL_HISPD;
1220
1221         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1222
1223         /*
1224          * Some (ENE) controllers go apeshit on some ios operation,
1225          * signalling timeout and CRC errors even on CMD0. Resetting
1226          * it on each ios seems to solve the problem.
1227          */
1228         if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1229                 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1230
1231 out:
1232         mmiowb();
1233         spin_unlock_irqrestore(&host->lock, flags);
1234 }
1235
1236 static int sdhci_get_ro(struct mmc_host *mmc)
1237 {
1238         struct sdhci_host *host;
1239         unsigned long flags;
1240         int is_readonly;
1241
1242         host = mmc_priv(mmc);
1243
1244         spin_lock_irqsave(&host->lock, flags);
1245
1246         if (host->flags & SDHCI_DEVICE_DEAD)
1247                 is_readonly = 0;
1248         else if (host->ops->get_ro)
1249                 is_readonly = host->ops->get_ro(host);
1250         else
1251                 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1252                                 & SDHCI_WRITE_PROTECT);
1253
1254         spin_unlock_irqrestore(&host->lock, flags);
1255
1256         /* This quirk needs to be replaced by a callback-function later */
1257         return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1258                 !is_readonly : is_readonly;
1259 }
1260
1261 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1262 {
1263         struct sdhci_host *host;
1264         unsigned long flags;
1265
1266         host = mmc_priv(mmc);
1267
1268         spin_lock_irqsave(&host->lock, flags);
1269
1270         if (host->flags & SDHCI_DEVICE_DEAD)
1271                 goto out;
1272
1273         if (enable)
1274                 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1275         else
1276                 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
1277 out:
1278         mmiowb();
1279
1280         spin_unlock_irqrestore(&host->lock, flags);
1281 }
1282
1283 static const struct mmc_host_ops sdhci_ops = {
1284         .request        = sdhci_request,
1285         .set_ios        = sdhci_set_ios,
1286         .get_ro         = sdhci_get_ro,
1287         .enable_sdio_irq = sdhci_enable_sdio_irq,
1288 };
1289
1290 /*****************************************************************************\
1291  *                                                                           *
1292  * Tasklets                                                                  *
1293  *                                                                           *
1294 \*****************************************************************************/
1295
1296 static void sdhci_tasklet_card(unsigned long param)
1297 {
1298         struct sdhci_host *host;
1299         unsigned long flags;
1300
1301         host = (struct sdhci_host*)param;
1302
1303         spin_lock_irqsave(&host->lock, flags);
1304
1305         if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
1306                 if (host->mrq) {
1307                         printk(KERN_ERR "%s: Card removed during transfer!\n",
1308                                 mmc_hostname(host->mmc));
1309                         printk(KERN_ERR "%s: Resetting controller.\n",
1310                                 mmc_hostname(host->mmc));
1311
1312                         sdhci_reset(host, SDHCI_RESET_CMD);
1313                         sdhci_reset(host, SDHCI_RESET_DATA);
1314
1315                         host->mrq->cmd->error = -ENOMEDIUM;
1316                         tasklet_schedule(&host->finish_tasklet);
1317                 }
1318         }
1319
1320         spin_unlock_irqrestore(&host->lock, flags);
1321
1322         mmc_detect_change(host->mmc, msecs_to_jiffies(200));
1323 }
1324
1325 static void sdhci_tasklet_finish(unsigned long param)
1326 {
1327         struct sdhci_host *host;
1328         unsigned long flags;
1329         struct mmc_request *mrq;
1330
1331         host = (struct sdhci_host*)param;
1332
1333         spin_lock_irqsave(&host->lock, flags);
1334
1335         del_timer(&host->timer);
1336
1337         mrq = host->mrq;
1338
1339         /*
1340          * The controller needs a reset of internal state machines
1341          * upon error conditions.
1342          */
1343         if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1344                 (mrq->cmd->error ||
1345                  (mrq->data && (mrq->data->error ||
1346                   (mrq->data->stop && mrq->data->stop->error))) ||
1347                    (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1348
1349                 /* Some controllers need this kick or reset won't work here */
1350                 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
1351                         unsigned int clock;
1352
1353                         /* This is to force an update */
1354                         clock = host->clock;
1355                         host->clock = 0;
1356                         sdhci_set_clock(host, clock);
1357                 }
1358
1359                 /* Spec says we should do both at the same time, but Ricoh
1360                    controllers do not like that. */
1361                 sdhci_reset(host, SDHCI_RESET_CMD);
1362                 sdhci_reset(host, SDHCI_RESET_DATA);
1363         }
1364
1365         host->mrq = NULL;
1366         host->cmd = NULL;
1367         host->data = NULL;
1368
1369 #ifndef SDHCI_USE_LEDS_CLASS
1370         sdhci_deactivate_led(host);
1371 #endif
1372
1373         mmiowb();
1374         spin_unlock_irqrestore(&host->lock, flags);
1375
1376         mmc_request_done(host->mmc, mrq);
1377 }
1378
1379 static void sdhci_timeout_timer(unsigned long data)
1380 {
1381         struct sdhci_host *host;
1382         unsigned long flags;
1383
1384         host = (struct sdhci_host*)data;
1385
1386         spin_lock_irqsave(&host->lock, flags);
1387
1388         if (host->mrq) {
1389                 printk(KERN_ERR "%s: Timeout waiting for hardware "
1390                         "interrupt.\n", mmc_hostname(host->mmc));
1391                 sdhci_dumpregs(host);
1392
1393                 if (host->data) {
1394                         host->data->error = -ETIMEDOUT;
1395                         sdhci_finish_data(host);
1396                 } else {
1397                         if (host->cmd)
1398                                 host->cmd->error = -ETIMEDOUT;
1399                         else
1400                                 host->mrq->cmd->error = -ETIMEDOUT;
1401
1402                         tasklet_schedule(&host->finish_tasklet);
1403                 }
1404         }
1405
1406         mmiowb();
1407         spin_unlock_irqrestore(&host->lock, flags);
1408 }
1409
1410 /*****************************************************************************\
1411  *                                                                           *
1412  * Interrupt handling                                                        *
1413  *                                                                           *
1414 \*****************************************************************************/
1415
1416 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1417 {
1418         BUG_ON(intmask == 0);
1419
1420         if (!host->cmd) {
1421                 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1422                         "though no command operation was in progress.\n",
1423                         mmc_hostname(host->mmc), (unsigned)intmask);
1424                 sdhci_dumpregs(host);
1425                 return;
1426         }
1427
1428         if (intmask & SDHCI_INT_TIMEOUT)
1429                 host->cmd->error = -ETIMEDOUT;
1430         else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1431                         SDHCI_INT_INDEX))
1432                 host->cmd->error = -EILSEQ;
1433
1434         if (host->cmd->error) {
1435                 tasklet_schedule(&host->finish_tasklet);
1436                 return;
1437         }
1438
1439         /*
1440          * The host can send and interrupt when the busy state has
1441          * ended, allowing us to wait without wasting CPU cycles.
1442          * Unfortunately this is overloaded on the "data complete"
1443          * interrupt, so we need to take some care when handling
1444          * it.
1445          *
1446          * Note: The 1.0 specification is a bit ambiguous about this
1447          *       feature so there might be some problems with older
1448          *       controllers.
1449          */
1450         if (host->cmd->flags & MMC_RSP_BUSY) {
1451                 if (host->cmd->data)
1452                         DBG("Cannot wait for busy signal when also "
1453                                 "doing a data transfer");
1454                 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
1455                         return;
1456
1457                 /* The controller does not support the end-of-busy IRQ,
1458                  * fall through and take the SDHCI_INT_RESPONSE */
1459         }
1460
1461         if (intmask & SDHCI_INT_RESPONSE)
1462                 sdhci_finish_command(host);
1463 }
1464
1465 #ifdef CONFIG_MMC_DEBUG
1466 static void sdhci_show_adma_error(struct sdhci_host *host)
1467 {
1468         const char *name = mmc_hostname(host->mmc);
1469         u8 *desc = host->adma_desc;
1470         __le32 *dma;
1471         __le16 *len;
1472         u8 attr;
1473
1474         sdhci_dumpregs(host);
1475
1476         while (true) {
1477                 dma = (__le32 *)(desc + 4);
1478                 len = (__le16 *)(desc + 2);
1479                 attr = *desc;
1480
1481                 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1482                     name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1483
1484                 desc += 8;
1485
1486                 if (attr & 2)
1487                         break;
1488         }
1489 }
1490 #else
1491 static void sdhci_show_adma_error(struct sdhci_host *host) { }
1492 #endif
1493
1494 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1495 {
1496         BUG_ON(intmask == 0);
1497
1498         if (!host->data) {
1499                 /*
1500                  * The "data complete" interrupt is also used to
1501                  * indicate that a busy state has ended. See comment
1502                  * above in sdhci_cmd_irq().
1503                  */
1504                 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1505                         if (intmask & SDHCI_INT_DATA_END) {
1506                                 sdhci_finish_command(host);
1507                                 return;
1508                         }
1509                 }
1510
1511                 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1512                         "though no data operation was in progress.\n",
1513                         mmc_hostname(host->mmc), (unsigned)intmask);
1514                 sdhci_dumpregs(host);
1515
1516                 return;
1517         }
1518
1519         if (intmask & SDHCI_INT_DATA_TIMEOUT)
1520                 host->data->error = -ETIMEDOUT;
1521         else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1522                 host->data->error = -EILSEQ;
1523         else if (intmask & SDHCI_INT_ADMA_ERROR) {
1524                 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1525                 sdhci_show_adma_error(host);
1526                 host->data->error = -EIO;
1527         }
1528
1529         if (host->data->error)
1530                 sdhci_finish_data(host);
1531         else {
1532                 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1533                         sdhci_transfer_pio(host);
1534
1535                 /*
1536                  * We currently don't do anything fancy with DMA
1537                  * boundaries, but as we can't disable the feature
1538                  * we need to at least restart the transfer.
1539                  */
1540                 if (intmask & SDHCI_INT_DMA_END)
1541                         sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1542                                 SDHCI_DMA_ADDRESS);
1543
1544                 if (intmask & SDHCI_INT_DATA_END) {
1545                         if (host->cmd) {
1546                                 /*
1547                                  * Data managed to finish before the
1548                                  * command completed. Make sure we do
1549                                  * things in the proper order.
1550                                  */
1551                                 host->data_early = 1;
1552                         } else {
1553                                 sdhci_finish_data(host);
1554                         }
1555                 }
1556         }
1557 }
1558
1559 static irqreturn_t sdhci_irq(int irq, void *dev_id)
1560 {
1561         irqreturn_t result;
1562         struct sdhci_host* host = dev_id;
1563         u32 intmask;
1564         int cardint = 0;
1565
1566         spin_lock(&host->lock);
1567
1568         intmask = sdhci_readl(host, SDHCI_INT_STATUS);
1569
1570         if (!intmask || intmask == 0xffffffff) {
1571                 result = IRQ_NONE;
1572                 goto out;
1573         }
1574
1575         DBG("*** %s got interrupt: 0x%08x\n",
1576                 mmc_hostname(host->mmc), intmask);
1577
1578         if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1579                 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1580                         SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
1581                 tasklet_schedule(&host->card_tasklet);
1582         }
1583
1584         intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1585
1586         if (intmask & SDHCI_INT_CMD_MASK) {
1587                 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1588                         SDHCI_INT_STATUS);
1589                 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1590         }
1591
1592         if (intmask & SDHCI_INT_DATA_MASK) {
1593                 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1594                         SDHCI_INT_STATUS);
1595                 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1596         }
1597
1598         intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1599
1600         intmask &= ~SDHCI_INT_ERROR;
1601
1602         if (intmask & SDHCI_INT_BUS_POWER) {
1603                 printk(KERN_ERR "%s: Card is consuming too much power!\n",
1604                         mmc_hostname(host->mmc));
1605                 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
1606         }
1607
1608         intmask &= ~SDHCI_INT_BUS_POWER;
1609
1610         if (intmask & SDHCI_INT_CARD_INT)
1611                 cardint = 1;
1612
1613         intmask &= ~SDHCI_INT_CARD_INT;
1614
1615         if (intmask) {
1616                 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
1617                         mmc_hostname(host->mmc), intmask);
1618                 sdhci_dumpregs(host);
1619
1620                 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
1621         }
1622
1623         result = IRQ_HANDLED;
1624
1625         mmiowb();
1626 out:
1627         spin_unlock(&host->lock);
1628
1629         /*
1630          * We have to delay this as it calls back into the driver.
1631          */
1632         if (cardint)
1633                 mmc_signal_sdio_irq(host->mmc);
1634
1635         return result;
1636 }
1637
1638 /*****************************************************************************\
1639  *                                                                           *
1640  * Suspend/resume                                                            *
1641  *                                                                           *
1642 \*****************************************************************************/
1643
1644 #ifdef CONFIG_PM
1645
1646 int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
1647 {
1648         int ret;
1649
1650         sdhci_disable_card_detection(host);
1651
1652         ret = mmc_suspend_host(host->mmc);
1653         if (ret)
1654                 return ret;
1655
1656         free_irq(host->irq, host);
1657
1658         if (host->vmmc)
1659                 ret = regulator_disable(host->vmmc);
1660
1661         return ret;
1662 }
1663
1664 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
1665
1666 int sdhci_resume_host(struct sdhci_host *host)
1667 {
1668         int ret;
1669
1670         if (host->vmmc) {
1671                 int ret = regulator_enable(host->vmmc);
1672                 if (ret)
1673                         return ret;
1674         }
1675
1676
1677         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
1678                 if (host->ops->enable_dma)
1679                         host->ops->enable_dma(host);
1680         }
1681
1682         ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1683                           mmc_hostname(host->mmc), host);
1684         if (ret)
1685                 return ret;
1686
1687         sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
1688         mmiowb();
1689
1690         ret = mmc_resume_host(host->mmc);
1691         sdhci_enable_card_detection(host);
1692
1693         return ret;
1694 }
1695
1696 EXPORT_SYMBOL_GPL(sdhci_resume_host);
1697
1698 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
1699 {
1700         u8 val;
1701         val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
1702         val |= SDHCI_WAKE_ON_INT;
1703         sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
1704 }
1705
1706 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
1707
1708 #endif /* CONFIG_PM */
1709
1710 /*****************************************************************************\
1711  *                                                                           *
1712  * Device allocation/registration                                            *
1713  *                                                                           *
1714 \*****************************************************************************/
1715
1716 struct sdhci_host *sdhci_alloc_host(struct device *dev,
1717         size_t priv_size)
1718 {
1719         struct mmc_host *mmc;
1720         struct sdhci_host *host;
1721
1722         WARN_ON(dev == NULL);
1723
1724         mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
1725         if (!mmc)
1726                 return ERR_PTR(-ENOMEM);
1727
1728         host = mmc_priv(mmc);
1729         host->mmc = mmc;
1730
1731         return host;
1732 }
1733
1734 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
1735
1736 int sdhci_add_host(struct sdhci_host *host)
1737 {
1738         struct mmc_host *mmc;
1739         unsigned int caps;
1740         int ret;
1741
1742         WARN_ON(host == NULL);
1743         if (host == NULL)
1744                 return -EINVAL;
1745
1746         mmc = host->mmc;
1747
1748         if (debug_quirks)
1749                 host->quirks = debug_quirks;
1750
1751         sdhci_reset(host, SDHCI_RESET_ALL);
1752
1753         host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
1754         host->version = (host->version & SDHCI_SPEC_VER_MASK)
1755                                 >> SDHCI_SPEC_VER_SHIFT;
1756         if (host->version > SDHCI_SPEC_300) {
1757                 printk(KERN_ERR "%s: Unknown controller version (%d). "
1758                         "You may experience problems.\n", mmc_hostname(mmc),
1759                         host->version);
1760         }
1761
1762         caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1763                 sdhci_readl(host, SDHCI_CAPABILITIES);
1764
1765         if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1766                 host->flags |= SDHCI_USE_SDMA;
1767         else if (!(caps & SDHCI_CAN_DO_SDMA))
1768                 DBG("Controller doesn't have SDMA capability\n");
1769         else
1770                 host->flags |= SDHCI_USE_SDMA;
1771
1772         if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1773                 (host->flags & SDHCI_USE_SDMA)) {
1774                 DBG("Disabling DMA as it is marked broken\n");
1775                 host->flags &= ~SDHCI_USE_SDMA;
1776         }
1777
1778         if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1779                 host->flags |= SDHCI_USE_ADMA;
1780
1781         if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1782                 (host->flags & SDHCI_USE_ADMA)) {
1783                 DBG("Disabling ADMA as it is marked broken\n");
1784                 host->flags &= ~SDHCI_USE_ADMA;
1785         }
1786
1787         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
1788                 if (host->ops->enable_dma) {
1789                         if (host->ops->enable_dma(host)) {
1790                                 printk(KERN_WARNING "%s: No suitable DMA "
1791                                         "available. Falling back to PIO.\n",
1792                                         mmc_hostname(mmc));
1793                                 host->flags &=
1794                                         ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
1795                         }
1796                 }
1797         }
1798
1799         if (host->flags & SDHCI_USE_ADMA) {
1800                 /*
1801                  * We need to allocate descriptors for all sg entries
1802                  * (128) and potentially one alignment transfer for
1803                  * each of those entries.
1804                  */
1805                 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1806                 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1807                 if (!host->adma_desc || !host->align_buffer) {
1808                         kfree(host->adma_desc);
1809                         kfree(host->align_buffer);
1810                         printk(KERN_WARNING "%s: Unable to allocate ADMA "
1811                                 "buffers. Falling back to standard DMA.\n",
1812                                 mmc_hostname(mmc));
1813                         host->flags &= ~SDHCI_USE_ADMA;
1814                 }
1815         }
1816
1817         /*
1818          * If we use DMA, then it's up to the caller to set the DMA
1819          * mask, but PIO does not need the hw shim so we set a new
1820          * mask here in that case.
1821          */
1822         if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
1823                 host->dma_mask = DMA_BIT_MASK(64);
1824                 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1825         }
1826
1827         if (host->version >= SDHCI_SPEC_300)
1828                 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
1829                         >> SDHCI_CLOCK_BASE_SHIFT;
1830         else
1831                 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
1832                         >> SDHCI_CLOCK_BASE_SHIFT;
1833
1834         host->max_clk *= 1000000;
1835         if (host->max_clk == 0 || host->quirks &
1836                         SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
1837                 if (!host->ops->get_max_clock) {
1838                         printk(KERN_ERR
1839                                "%s: Hardware doesn't specify base clock "
1840                                "frequency.\n", mmc_hostname(mmc));
1841                         return -ENODEV;
1842                 }
1843                 host->max_clk = host->ops->get_max_clock(host);
1844         }
1845
1846         host->timeout_clk =
1847                 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1848         if (host->timeout_clk == 0) {
1849                 if (host->ops->get_timeout_clock) {
1850                         host->timeout_clk = host->ops->get_timeout_clock(host);
1851                 } else if (!(host->quirks &
1852                                 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
1853                         printk(KERN_ERR
1854                                "%s: Hardware doesn't specify timeout clock "
1855                                "frequency.\n", mmc_hostname(mmc));
1856                         return -ENODEV;
1857                 }
1858         }
1859         if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1860                 host->timeout_clk *= 1000;
1861
1862         /*
1863          * Set host parameters.
1864          */
1865         mmc->ops = &sdhci_ops;
1866         if (host->ops->get_min_clock)
1867                 mmc->f_min = host->ops->get_min_clock(host);
1868         else if (host->version >= SDHCI_SPEC_300)
1869                 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
1870         else
1871                 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
1872
1873         mmc->f_max = host->max_clk;
1874         mmc->caps |= MMC_CAP_SDIO_IRQ;
1875
1876         /*
1877          * A controller may support 8-bit width, but the board itself
1878          * might not have the pins brought out.  Boards that support
1879          * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
1880          * their platform code before calling sdhci_add_host(), and we
1881          * won't assume 8-bit width for hosts without that CAP.
1882          */
1883         if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1884                 mmc->caps |= MMC_CAP_4_BIT_DATA;
1885
1886         if (caps & SDHCI_CAN_DO_HISPD)
1887                 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
1888
1889         if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1890             mmc_card_is_removable(mmc))
1891                 mmc->caps |= MMC_CAP_NEEDS_POLL;
1892
1893         mmc->ocr_avail = 0;
1894         if (caps & SDHCI_CAN_VDD_330)
1895                 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1896         if (caps & SDHCI_CAN_VDD_300)
1897                 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1898         if (caps & SDHCI_CAN_VDD_180)
1899                 mmc->ocr_avail |= MMC_VDD_165_195;
1900
1901         if (mmc->ocr_avail == 0) {
1902                 printk(KERN_ERR "%s: Hardware doesn't report any "
1903                         "support voltages.\n", mmc_hostname(mmc));
1904                 return -ENODEV;
1905         }
1906
1907         spin_lock_init(&host->lock);
1908
1909         /*
1910          * Maximum number of segments. Depends on if the hardware
1911          * can do scatter/gather or not.
1912          */
1913         if (host->flags & SDHCI_USE_ADMA)
1914                 mmc->max_segs = 128;
1915         else if (host->flags & SDHCI_USE_SDMA)
1916                 mmc->max_segs = 1;
1917         else /* PIO */
1918                 mmc->max_segs = 128;
1919
1920         /*
1921          * Maximum number of sectors in one transfer. Limited by DMA boundary
1922          * size (512KiB).
1923          */
1924         mmc->max_req_size = 524288;
1925
1926         /*
1927          * Maximum segment size. Could be one segment with the maximum number
1928          * of bytes. When doing hardware scatter/gather, each entry cannot
1929          * be larger than 64 KiB though.
1930          */
1931         if (host->flags & SDHCI_USE_ADMA)
1932                 mmc->max_seg_size = 65536;
1933         else
1934                 mmc->max_seg_size = mmc->max_req_size;
1935
1936         /*
1937          * Maximum block size. This varies from controller to controller and
1938          * is specified in the capabilities register.
1939          */
1940         if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1941                 mmc->max_blk_size = 2;
1942         } else {
1943                 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1944                                 SDHCI_MAX_BLOCK_SHIFT;
1945                 if (mmc->max_blk_size >= 3) {
1946                         printk(KERN_WARNING "%s: Invalid maximum block size, "
1947                                 "assuming 512 bytes\n", mmc_hostname(mmc));
1948                         mmc->max_blk_size = 0;
1949                 }
1950         }
1951
1952         mmc->max_blk_size = 512 << mmc->max_blk_size;
1953
1954         /*
1955          * Maximum block count.
1956          */
1957         mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
1958
1959         /*
1960          * Init tasklets.
1961          */
1962         tasklet_init(&host->card_tasklet,
1963                 sdhci_tasklet_card, (unsigned long)host);
1964         tasklet_init(&host->finish_tasklet,
1965                 sdhci_tasklet_finish, (unsigned long)host);
1966
1967         setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
1968
1969         ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1970                 mmc_hostname(mmc), host);
1971         if (ret)
1972                 goto untasklet;
1973
1974         host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1975         if (IS_ERR(host->vmmc)) {
1976                 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
1977                 host->vmmc = NULL;
1978         } else {
1979                 regulator_enable(host->vmmc);
1980         }
1981
1982         sdhci_init(host, 0);
1983
1984 #ifdef CONFIG_MMC_DEBUG
1985         sdhci_dumpregs(host);
1986 #endif
1987
1988 #ifdef SDHCI_USE_LEDS_CLASS
1989         snprintf(host->led_name, sizeof(host->led_name),
1990                 "%s::", mmc_hostname(mmc));
1991         host->led.name = host->led_name;
1992         host->led.brightness = LED_OFF;
1993         host->led.default_trigger = mmc_hostname(mmc);
1994         host->led.brightness_set = sdhci_led_control;
1995
1996         ret = led_classdev_register(mmc_dev(mmc), &host->led);
1997         if (ret)
1998                 goto reset;
1999 #endif
2000
2001         mmiowb();
2002
2003         mmc_add_host(mmc);
2004
2005         printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
2006                 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
2007                 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2008                 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
2009
2010         sdhci_enable_card_detection(host);
2011
2012         return 0;
2013
2014 #ifdef SDHCI_USE_LEDS_CLASS
2015 reset:
2016         sdhci_reset(host, SDHCI_RESET_ALL);
2017         free_irq(host->irq, host);
2018 #endif
2019 untasklet:
2020         tasklet_kill(&host->card_tasklet);
2021         tasklet_kill(&host->finish_tasklet);
2022
2023         return ret;
2024 }
2025
2026 EXPORT_SYMBOL_GPL(sdhci_add_host);
2027
2028 void sdhci_remove_host(struct sdhci_host *host, int dead)
2029 {
2030         unsigned long flags;
2031
2032         if (dead) {
2033                 spin_lock_irqsave(&host->lock, flags);
2034
2035                 host->flags |= SDHCI_DEVICE_DEAD;
2036
2037                 if (host->mrq) {
2038                         printk(KERN_ERR "%s: Controller removed during "
2039                                 " transfer!\n", mmc_hostname(host->mmc));
2040
2041                         host->mrq->cmd->error = -ENOMEDIUM;
2042                         tasklet_schedule(&host->finish_tasklet);
2043                 }
2044
2045                 spin_unlock_irqrestore(&host->lock, flags);
2046         }
2047
2048         sdhci_disable_card_detection(host);
2049
2050         mmc_remove_host(host->mmc);
2051
2052 #ifdef SDHCI_USE_LEDS_CLASS
2053         led_classdev_unregister(&host->led);
2054 #endif
2055
2056         if (!dead)
2057                 sdhci_reset(host, SDHCI_RESET_ALL);
2058
2059         free_irq(host->irq, host);
2060
2061         del_timer_sync(&host->timer);
2062
2063         tasklet_kill(&host->card_tasklet);
2064         tasklet_kill(&host->finish_tasklet);
2065
2066         if (host->vmmc) {
2067                 regulator_disable(host->vmmc);
2068                 regulator_put(host->vmmc);
2069         }
2070
2071         kfree(host->adma_desc);
2072         kfree(host->align_buffer);
2073
2074         host->adma_desc = NULL;
2075         host->align_buffer = NULL;
2076 }
2077
2078 EXPORT_SYMBOL_GPL(sdhci_remove_host);
2079
2080 void sdhci_free_host(struct sdhci_host *host)
2081 {
2082         mmc_free_host(host->mmc);
2083 }
2084
2085 EXPORT_SYMBOL_GPL(sdhci_free_host);
2086
2087 /*****************************************************************************\
2088  *                                                                           *
2089  * Driver init/exit                                                          *
2090  *                                                                           *
2091 \*****************************************************************************/
2092
2093 static int __init sdhci_drv_init(void)
2094 {
2095         printk(KERN_INFO DRIVER_NAME
2096                 ": Secure Digital Host Controller Interface driver\n");
2097         printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2098
2099         return 0;
2100 }
2101
2102 static void __exit sdhci_drv_exit(void)
2103 {
2104 }
2105
2106 module_init(sdhci_drv_init);
2107 module_exit(sdhci_drv_exit);
2108
2109 module_param(debug_quirks, uint, 0444);
2110
2111 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2112 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
2113 MODULE_LICENSE("GPL");
2114
2115 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");