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