mmc: add hs400 enhanced strobe support for mmc subsystem
[firefly-linux-kernel-4.4.55.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/module.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/slab.h>
22 #include <linux/scatterlist.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/pm_runtime.h>
25
26 #include <linux/leds.h>
27
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/sdio.h>
32 #include <linux/mmc/slot-gpio.h>
33
34 #include "sdhci.h"
35
36 #define DRIVER_NAME "sdhci"
37
38 #define DBG(f, x...) \
39         pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
40
41 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42         defined(CONFIG_MMC_SDHCI_MODULE))
43 #define SDHCI_USE_LEDS_CLASS
44 #endif
45
46 #define MAX_TUNING_LOOP 40
47
48 static unsigned int debug_quirks = 0;
49 static unsigned int debug_quirks2;
50
51 static void sdhci_finish_data(struct sdhci_host *);
52
53 static void sdhci_finish_command(struct sdhci_host *);
54 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
55 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
56 static int sdhci_pre_dma_transfer(struct sdhci_host *host,
57                                         struct mmc_data *data);
58 static int sdhci_do_get_cd(struct sdhci_host *host);
59
60 #ifdef CONFIG_PM
61 static int sdhci_runtime_pm_get(struct sdhci_host *host);
62 static int sdhci_runtime_pm_put(struct sdhci_host *host);
63 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
64 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
65 #else
66 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
67 {
68         return 0;
69 }
70 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
71 {
72         return 0;
73 }
74 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
75 {
76 }
77 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
78 {
79 }
80 #endif
81
82 static void sdhci_dumpregs(struct sdhci_host *host)
83 {
84         pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
85                 mmc_hostname(host->mmc));
86
87         pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version:  0x%08x\n",
88                 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89                 sdhci_readw(host, SDHCI_HOST_VERSION));
90         pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt:  0x%08x\n",
91                 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92                 sdhci_readw(host, SDHCI_BLOCK_COUNT));
93         pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
94                 sdhci_readl(host, SDHCI_ARGUMENT),
95                 sdhci_readw(host, SDHCI_TRANSFER_MODE));
96         pr_debug(DRIVER_NAME ": Present:  0x%08x | Host ctl: 0x%08x\n",
97                 sdhci_readl(host, SDHCI_PRESENT_STATE),
98                 sdhci_readb(host, SDHCI_HOST_CONTROL));
99         pr_debug(DRIVER_NAME ": Power:    0x%08x | Blk gap:  0x%08x\n",
100                 sdhci_readb(host, SDHCI_POWER_CONTROL),
101                 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
102         pr_debug(DRIVER_NAME ": Wake-up:  0x%08x | Clock:    0x%08x\n",
103                 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104                 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
105         pr_debug(DRIVER_NAME ": Timeout:  0x%08x | Int stat: 0x%08x\n",
106                 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107                 sdhci_readl(host, SDHCI_INT_STATUS));
108         pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
109                 sdhci_readl(host, SDHCI_INT_ENABLE),
110                 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
111         pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
112                 sdhci_readw(host, SDHCI_ACMD12_ERR),
113                 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
114         pr_debug(DRIVER_NAME ": Caps:     0x%08x | Caps_1:   0x%08x\n",
115                 sdhci_readl(host, SDHCI_CAPABILITIES),
116                 sdhci_readl(host, SDHCI_CAPABILITIES_1));
117         pr_debug(DRIVER_NAME ": Cmd:      0x%08x | Max curr: 0x%08x\n",
118                 sdhci_readw(host, SDHCI_COMMAND),
119                 sdhci_readl(host, SDHCI_MAX_CURRENT));
120         pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
121                 sdhci_readw(host, SDHCI_HOST_CONTROL2));
122
123         if (host->flags & SDHCI_USE_ADMA) {
124                 if (host->flags & SDHCI_USE_64_BIT_DMA)
125                         pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
126                                  readl(host->ioaddr + SDHCI_ADMA_ERROR),
127                                  readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
128                                  readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
129                 else
130                         pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
131                                  readl(host->ioaddr + SDHCI_ADMA_ERROR),
132                                  readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
133         }
134
135         pr_debug(DRIVER_NAME ": ===========================================\n");
136 }
137
138 /*****************************************************************************\
139  *                                                                           *
140  * Low level functions                                                       *
141  *                                                                           *
142 \*****************************************************************************/
143
144 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
145 {
146         u32 present;
147
148         if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
149             (host->mmc->caps & MMC_CAP_NONREMOVABLE))
150                 return;
151
152         if (enable) {
153                 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154                                       SDHCI_CARD_PRESENT;
155
156                 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
157                                        SDHCI_INT_CARD_INSERT;
158         } else {
159                 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
160         }
161
162         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
163         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
164 }
165
166 static void sdhci_enable_card_detection(struct sdhci_host *host)
167 {
168         sdhci_set_card_detection(host, true);
169 }
170
171 static void sdhci_disable_card_detection(struct sdhci_host *host)
172 {
173         sdhci_set_card_detection(host, false);
174 }
175
176 void sdhci_reset(struct sdhci_host *host, u8 mask)
177 {
178         unsigned long timeout;
179
180         sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
181
182         if (mask & SDHCI_RESET_ALL) {
183                 host->clock = 0;
184                 /* Reset-all turns off SD Bus Power */
185                 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186                         sdhci_runtime_pm_bus_off(host);
187         }
188
189         /* Wait max 100 ms */
190         timeout = 100;
191
192         /* hw clears the bit when it's done */
193         while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
194                 if (timeout == 0) {
195                         pr_err("%s: Reset 0x%x never completed.\n",
196                                 mmc_hostname(host->mmc), (int)mask);
197                         sdhci_dumpregs(host);
198                         return;
199                 }
200                 timeout--;
201                 mdelay(1);
202         }
203 }
204 EXPORT_SYMBOL_GPL(sdhci_reset);
205
206 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207 {
208         if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
209                 if (!sdhci_do_get_cd(host))
210                         return;
211         }
212
213         host->ops->reset(host, mask);
214
215         if (mask & SDHCI_RESET_ALL) {
216                 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
217                         if (host->ops->enable_dma)
218                                 host->ops->enable_dma(host);
219                 }
220
221                 /* Resetting the controller clears many */
222                 host->preset_enabled = false;
223         }
224 }
225
226 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227
228 static void sdhci_init(struct sdhci_host *host, int soft)
229 {
230         if (soft)
231                 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
232         else
233                 sdhci_do_reset(host, SDHCI_RESET_ALL);
234
235         host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
236                     SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
237                     SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
238                     SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
239                     SDHCI_INT_RESPONSE;
240
241         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
242         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
243
244         if (soft) {
245                 /* force clock reconfiguration */
246                 host->clock = 0;
247                 sdhci_set_ios(host->mmc, &host->mmc->ios);
248         }
249 }
250
251 static void sdhci_reinit(struct sdhci_host *host)
252 {
253         sdhci_init(host, 0);
254         sdhci_enable_card_detection(host);
255 }
256
257 static void sdhci_activate_led(struct sdhci_host *host)
258 {
259         u8 ctrl;
260
261         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
262         ctrl |= SDHCI_CTRL_LED;
263         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
264 }
265
266 static void sdhci_deactivate_led(struct sdhci_host *host)
267 {
268         u8 ctrl;
269
270         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
271         ctrl &= ~SDHCI_CTRL_LED;
272         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
273 }
274
275 #ifdef SDHCI_USE_LEDS_CLASS
276 static void sdhci_led_control(struct led_classdev *led,
277         enum led_brightness brightness)
278 {
279         struct sdhci_host *host = container_of(led, struct sdhci_host, led);
280         unsigned long flags;
281
282         spin_lock_irqsave(&host->lock, flags);
283
284         if (host->runtime_suspended)
285                 goto out;
286
287         if (brightness == LED_OFF)
288                 sdhci_deactivate_led(host);
289         else
290                 sdhci_activate_led(host);
291 out:
292         spin_unlock_irqrestore(&host->lock, flags);
293 }
294 #endif
295
296 /*****************************************************************************\
297  *                                                                           *
298  * Core functions                                                            *
299  *                                                                           *
300 \*****************************************************************************/
301
302 static void sdhci_read_block_pio(struct sdhci_host *host)
303 {
304         unsigned long flags;
305         size_t blksize, len, chunk;
306         u32 uninitialized_var(scratch);
307         u8 *buf;
308
309         DBG("PIO reading\n");
310
311         blksize = host->data->blksz;
312         chunk = 0;
313
314         local_irq_save(flags);
315
316         while (blksize) {
317                 BUG_ON(!sg_miter_next(&host->sg_miter));
318
319                 len = min(host->sg_miter.length, blksize);
320
321                 blksize -= len;
322                 host->sg_miter.consumed = len;
323
324                 buf = host->sg_miter.addr;
325
326                 while (len) {
327                         if (chunk == 0) {
328                                 scratch = sdhci_readl(host, SDHCI_BUFFER);
329                                 chunk = 4;
330                         }
331
332                         *buf = scratch & 0xFF;
333
334                         buf++;
335                         scratch >>= 8;
336                         chunk--;
337                         len--;
338                 }
339         }
340
341         sg_miter_stop(&host->sg_miter);
342
343         local_irq_restore(flags);
344 }
345
346 static void sdhci_write_block_pio(struct sdhci_host *host)
347 {
348         unsigned long flags;
349         size_t blksize, len, chunk;
350         u32 scratch;
351         u8 *buf;
352
353         DBG("PIO writing\n");
354
355         blksize = host->data->blksz;
356         chunk = 0;
357         scratch = 0;
358
359         local_irq_save(flags);
360
361         while (blksize) {
362                 BUG_ON(!sg_miter_next(&host->sg_miter));
363
364                 len = min(host->sg_miter.length, blksize);
365
366                 blksize -= len;
367                 host->sg_miter.consumed = len;
368
369                 buf = host->sg_miter.addr;
370
371                 while (len) {
372                         scratch |= (u32)*buf << (chunk * 8);
373
374                         buf++;
375                         chunk++;
376                         len--;
377
378                         if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
379                                 sdhci_writel(host, scratch, SDHCI_BUFFER);
380                                 chunk = 0;
381                                 scratch = 0;
382                         }
383                 }
384         }
385
386         sg_miter_stop(&host->sg_miter);
387
388         local_irq_restore(flags);
389 }
390
391 static void sdhci_transfer_pio(struct sdhci_host *host)
392 {
393         u32 mask;
394
395         BUG_ON(!host->data);
396
397         if (host->blocks == 0)
398                 return;
399
400         if (host->data->flags & MMC_DATA_READ)
401                 mask = SDHCI_DATA_AVAILABLE;
402         else
403                 mask = SDHCI_SPACE_AVAILABLE;
404
405         /*
406          * Some controllers (JMicron JMB38x) mess up the buffer bits
407          * for transfers < 4 bytes. As long as it is just one block,
408          * we can ignore the bits.
409          */
410         if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
411                 (host->data->blocks == 1))
412                 mask = ~0;
413
414         while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
415                 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
416                         udelay(100);
417
418                 if (host->data->flags & MMC_DATA_READ)
419                         sdhci_read_block_pio(host);
420                 else
421                         sdhci_write_block_pio(host);
422
423                 host->blocks--;
424                 if (host->blocks == 0)
425                         break;
426         }
427
428         DBG("PIO transfer complete.\n");
429 }
430
431 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
432 {
433         local_irq_save(*flags);
434         return kmap_atomic(sg_page(sg)) + sg->offset;
435 }
436
437 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
438 {
439         kunmap_atomic(buffer);
440         local_irq_restore(*flags);
441 }
442
443 static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
444                                   dma_addr_t addr, int len, unsigned cmd)
445 {
446         struct sdhci_adma2_64_desc *dma_desc = desc;
447
448         /* 32-bit and 64-bit descriptors have these members in same position */
449         dma_desc->cmd = cpu_to_le16(cmd);
450         dma_desc->len = cpu_to_le16(len);
451         dma_desc->addr_lo = cpu_to_le32((u32)addr);
452
453         if (host->flags & SDHCI_USE_64_BIT_DMA)
454                 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
455 }
456
457 static void sdhci_adma_mark_end(void *desc)
458 {
459         struct sdhci_adma2_64_desc *dma_desc = desc;
460
461         /* 32-bit and 64-bit descriptors have 'cmd' in same position */
462         dma_desc->cmd |= cpu_to_le16(ADMA2_END);
463 }
464
465 static int sdhci_adma_table_pre(struct sdhci_host *host,
466         struct mmc_data *data)
467 {
468         int direction;
469
470         void *desc;
471         void *align;
472         dma_addr_t addr;
473         dma_addr_t align_addr;
474         int len, offset;
475
476         struct scatterlist *sg;
477         int i;
478         char *buffer;
479         unsigned long flags;
480
481         /*
482          * The spec does not specify endianness of descriptor table.
483          * We currently guess that it is LE.
484          */
485
486         if (data->flags & MMC_DATA_READ)
487                 direction = DMA_FROM_DEVICE;
488         else
489                 direction = DMA_TO_DEVICE;
490
491         host->align_addr = dma_map_single(mmc_dev(host->mmc),
492                 host->align_buffer, host->align_buffer_sz, direction);
493         if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
494                 goto fail;
495         BUG_ON(host->align_addr & host->align_mask);
496
497         host->sg_count = sdhci_pre_dma_transfer(host, data);
498         if (host->sg_count < 0)
499                 goto unmap_align;
500
501         desc = host->adma_table;
502         align = host->align_buffer;
503
504         align_addr = host->align_addr;
505
506         for_each_sg(data->sg, sg, host->sg_count, i) {
507                 addr = sg_dma_address(sg);
508                 len = sg_dma_len(sg);
509
510                 /*
511                  * The SDHCI specification states that ADMA
512                  * addresses must be 32-bit aligned. If they
513                  * aren't, then we use a bounce buffer for
514                  * the (up to three) bytes that screw up the
515                  * alignment.
516                  */
517                 offset = (host->align_sz - (addr & host->align_mask)) &
518                          host->align_mask;
519                 if (offset) {
520                         if (data->flags & MMC_DATA_WRITE) {
521                                 buffer = sdhci_kmap_atomic(sg, &flags);
522                                 memcpy(align, buffer, offset);
523                                 sdhci_kunmap_atomic(buffer, &flags);
524                         }
525
526                         /* tran, valid */
527                         sdhci_adma_write_desc(host, desc, align_addr, offset,
528                                               ADMA2_TRAN_VALID);
529
530                         BUG_ON(offset > 65536);
531
532                         align += host->align_sz;
533                         align_addr += host->align_sz;
534
535                         desc += host->desc_sz;
536
537                         addr += offset;
538                         len -= offset;
539                 }
540
541                 BUG_ON(len > 65536);
542
543                 if (len) {
544                         /* tran, valid */
545                         sdhci_adma_write_desc(host, desc, addr, len,
546                                               ADMA2_TRAN_VALID);
547                         desc += host->desc_sz;
548                 }
549
550                 /*
551                  * If this triggers then we have a calculation bug
552                  * somewhere. :/
553                  */
554                 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
555         }
556
557         if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
558                 /*
559                 * Mark the last descriptor as the terminating descriptor
560                 */
561                 if (desc != host->adma_table) {
562                         desc -= host->desc_sz;
563                         sdhci_adma_mark_end(desc);
564                 }
565         } else {
566                 /*
567                 * Add a terminating entry.
568                 */
569
570                 /* nop, end, valid */
571                 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
572         }
573
574         /*
575          * Resync align buffer as we might have changed it.
576          */
577         if (data->flags & MMC_DATA_WRITE) {
578                 dma_sync_single_for_device(mmc_dev(host->mmc),
579                         host->align_addr, host->align_buffer_sz, direction);
580         }
581
582         return 0;
583
584 unmap_align:
585         dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
586                 host->align_buffer_sz, direction);
587 fail:
588         return -EINVAL;
589 }
590
591 static void sdhci_adma_table_post(struct sdhci_host *host,
592         struct mmc_data *data)
593 {
594         int direction;
595
596         struct scatterlist *sg;
597         int i, size;
598         void *align;
599         char *buffer;
600         unsigned long flags;
601         bool has_unaligned;
602
603         if (data->flags & MMC_DATA_READ)
604                 direction = DMA_FROM_DEVICE;
605         else
606                 direction = DMA_TO_DEVICE;
607
608         dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
609                 host->align_buffer_sz, direction);
610
611         /* Do a quick scan of the SG list for any unaligned mappings */
612         has_unaligned = false;
613         for_each_sg(data->sg, sg, host->sg_count, i)
614                 if (sg_dma_address(sg) & host->align_mask) {
615                         has_unaligned = true;
616                         break;
617                 }
618
619         if (has_unaligned && data->flags & MMC_DATA_READ) {
620                 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
621                         data->sg_len, direction);
622
623                 align = host->align_buffer;
624
625                 for_each_sg(data->sg, sg, host->sg_count, i) {
626                         if (sg_dma_address(sg) & host->align_mask) {
627                                 size = host->align_sz -
628                                        (sg_dma_address(sg) & host->align_mask);
629
630                                 buffer = sdhci_kmap_atomic(sg, &flags);
631                                 memcpy(buffer, align, size);
632                                 sdhci_kunmap_atomic(buffer, &flags);
633
634                                 align += host->align_sz;
635                         }
636                 }
637         }
638
639         if (data->host_cookie == COOKIE_MAPPED) {
640                 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
641                         data->sg_len, direction);
642                 data->host_cookie = COOKIE_UNMAPPED;
643         }
644 }
645
646 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
647 {
648         u8 count;
649         struct mmc_data *data = cmd->data;
650         unsigned target_timeout, current_timeout;
651
652         /*
653          * If the host controller provides us with an incorrect timeout
654          * value, just skip the check and use 0xE.  The hardware may take
655          * longer to time out, but that's much better than having a too-short
656          * timeout value.
657          */
658         if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
659                 return 0xE;
660
661         /* Unspecified timeout, assume max */
662         if (!data && !cmd->busy_timeout)
663                 return 0xE;
664
665         /* timeout in us */
666         if (!data)
667                 target_timeout = cmd->busy_timeout * 1000;
668         else {
669                 target_timeout = data->timeout_ns / 1000;
670                 if (host->clock)
671                         target_timeout += data->timeout_clks / host->clock;
672         }
673
674         /*
675          * Figure out needed cycles.
676          * We do this in steps in order to fit inside a 32 bit int.
677          * The first step is the minimum timeout, which will have a
678          * minimum resolution of 6 bits:
679          * (1) 2^13*1000 > 2^22,
680          * (2) host->timeout_clk < 2^16
681          *     =>
682          *     (1) / (2) > 2^6
683          */
684         count = 0;
685         current_timeout = (1 << 13) * 1000 / host->timeout_clk;
686         while (current_timeout < target_timeout) {
687                 count++;
688                 current_timeout <<= 1;
689                 if (count >= 0xF)
690                         break;
691         }
692
693         if (count >= 0xF) {
694                 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
695                     mmc_hostname(host->mmc), count, cmd->opcode);
696                 count = 0xE;
697         }
698
699         return count;
700 }
701
702 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
703 {
704         u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
705         u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
706
707         if (host->flags & SDHCI_REQ_USE_DMA)
708                 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
709         else
710                 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
711
712         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
713         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
714 }
715
716 static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
717 {
718         u8 count;
719
720         if (host->ops->set_timeout) {
721                 host->ops->set_timeout(host, cmd);
722         } else {
723                 count = sdhci_calc_timeout(host, cmd);
724                 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
725         }
726 }
727
728 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
729 {
730         u8 ctrl;
731         struct mmc_data *data = cmd->data;
732         int ret;
733
734         WARN_ON(host->data);
735
736         if (data || (cmd->flags & MMC_RSP_BUSY))
737                 sdhci_set_timeout(host, cmd);
738
739         if (!data)
740                 return;
741
742         /* Sanity checks */
743         BUG_ON(data->blksz * data->blocks > 524288);
744         BUG_ON(data->blksz > host->mmc->max_blk_size);
745         BUG_ON(data->blocks > 65535);
746
747         host->data = data;
748         host->data_early = 0;
749         host->data->bytes_xfered = 0;
750
751         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
752                 host->flags |= SDHCI_REQ_USE_DMA;
753
754         /*
755          * FIXME: This doesn't account for merging when mapping the
756          * scatterlist.
757          */
758         if (host->flags & SDHCI_REQ_USE_DMA) {
759                 int broken, i;
760                 struct scatterlist *sg;
761
762                 broken = 0;
763                 if (host->flags & SDHCI_USE_ADMA) {
764                         if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
765                                 broken = 1;
766                 } else {
767                         if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
768                                 broken = 1;
769                 }
770
771                 if (unlikely(broken)) {
772                         for_each_sg(data->sg, sg, data->sg_len, i) {
773                                 if (sg->length & 0x3) {
774                                         DBG("Reverting to PIO because of "
775                                                 "transfer size (%d)\n",
776                                                 sg->length);
777                                         host->flags &= ~SDHCI_REQ_USE_DMA;
778                                         break;
779                                 }
780                         }
781                 }
782         }
783
784         /*
785          * The assumption here being that alignment is the same after
786          * translation to device address space.
787          */
788         if (host->flags & SDHCI_REQ_USE_DMA) {
789                 int broken, i;
790                 struct scatterlist *sg;
791
792                 broken = 0;
793                 if (host->flags & SDHCI_USE_ADMA) {
794                         /*
795                          * As we use 3 byte chunks to work around
796                          * alignment problems, we need to check this
797                          * quirk.
798                          */
799                         if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
800                                 broken = 1;
801                 } else {
802                         if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
803                                 broken = 1;
804                 }
805
806                 if (unlikely(broken)) {
807                         for_each_sg(data->sg, sg, data->sg_len, i) {
808                                 if (sg->offset & 0x3) {
809                                         DBG("Reverting to PIO because of "
810                                                 "bad alignment\n");
811                                         host->flags &= ~SDHCI_REQ_USE_DMA;
812                                         break;
813                                 }
814                         }
815                 }
816         }
817
818         if (host->flags & SDHCI_REQ_USE_DMA) {
819                 if (host->flags & SDHCI_USE_ADMA) {
820                         ret = sdhci_adma_table_pre(host, data);
821                         if (ret) {
822                                 /*
823                                  * This only happens when someone fed
824                                  * us an invalid request.
825                                  */
826                                 WARN_ON(1);
827                                 host->flags &= ~SDHCI_REQ_USE_DMA;
828                         } else {
829                                 sdhci_writel(host, host->adma_addr,
830                                         SDHCI_ADMA_ADDRESS);
831                                 if (host->flags & SDHCI_USE_64_BIT_DMA)
832                                         sdhci_writel(host,
833                                                      (u64)host->adma_addr >> 32,
834                                                      SDHCI_ADMA_ADDRESS_HI);
835                         }
836                 } else {
837                         int sg_cnt;
838
839                         sg_cnt = sdhci_pre_dma_transfer(host, data);
840                         if (sg_cnt <= 0) {
841                                 /*
842                                  * This only happens when someone fed
843                                  * us an invalid request.
844                                  */
845                                 WARN_ON(1);
846                                 host->flags &= ~SDHCI_REQ_USE_DMA;
847                         } else {
848                                 WARN_ON(sg_cnt != 1);
849                                 sdhci_writel(host, sg_dma_address(data->sg),
850                                         SDHCI_DMA_ADDRESS);
851                         }
852                 }
853         }
854
855         /*
856          * Always adjust the DMA selection as some controllers
857          * (e.g. JMicron) can't do PIO properly when the selection
858          * is ADMA.
859          */
860         if (host->version >= SDHCI_SPEC_200) {
861                 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
862                 ctrl &= ~SDHCI_CTRL_DMA_MASK;
863                 if ((host->flags & SDHCI_REQ_USE_DMA) &&
864                         (host->flags & SDHCI_USE_ADMA)) {
865                         if (host->flags & SDHCI_USE_64_BIT_DMA)
866                                 ctrl |= SDHCI_CTRL_ADMA64;
867                         else
868                                 ctrl |= SDHCI_CTRL_ADMA32;
869                 } else {
870                         ctrl |= SDHCI_CTRL_SDMA;
871                 }
872                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
873         }
874
875         if (!(host->flags & SDHCI_REQ_USE_DMA)) {
876                 int flags;
877
878                 flags = SG_MITER_ATOMIC;
879                 if (host->data->flags & MMC_DATA_READ)
880                         flags |= SG_MITER_TO_SG;
881                 else
882                         flags |= SG_MITER_FROM_SG;
883                 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
884                 host->blocks = data->blocks;
885         }
886
887         sdhci_set_transfer_irqs(host);
888
889         /* Set the DMA boundary value and block size */
890         sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
891                 data->blksz), SDHCI_BLOCK_SIZE);
892         sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
893 }
894
895 static void sdhci_set_transfer_mode(struct sdhci_host *host,
896         struct mmc_command *cmd)
897 {
898         u16 mode = 0;
899         struct mmc_data *data = cmd->data;
900
901         if (data == NULL) {
902                 if (host->quirks2 &
903                         SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
904                         sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
905                 } else {
906                 /* clear Auto CMD settings for no data CMDs */
907                         mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
908                         sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
909                                 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
910                 }
911                 return;
912         }
913
914         WARN_ON(!host->data);
915
916         if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
917                 mode = SDHCI_TRNS_BLK_CNT_EN;
918
919         if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
920                 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
921                 /*
922                  * If we are sending CMD23, CMD12 never gets sent
923                  * on successful completion (so no Auto-CMD12).
924                  */
925                 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
926                     (cmd->opcode != SD_IO_RW_EXTENDED))
927                         mode |= SDHCI_TRNS_AUTO_CMD12;
928                 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
929                         mode |= SDHCI_TRNS_AUTO_CMD23;
930                         sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
931                 }
932         }
933
934         if (data->flags & MMC_DATA_READ)
935                 mode |= SDHCI_TRNS_READ;
936         if (host->flags & SDHCI_REQ_USE_DMA)
937                 mode |= SDHCI_TRNS_DMA;
938
939         sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
940 }
941
942 static void sdhci_finish_data(struct sdhci_host *host)
943 {
944         struct mmc_data *data;
945
946         BUG_ON(!host->data);
947
948         data = host->data;
949         host->data = NULL;
950
951         if (host->flags & SDHCI_REQ_USE_DMA) {
952                 if (host->flags & SDHCI_USE_ADMA)
953                         sdhci_adma_table_post(host, data);
954                 else {
955                         if (data->host_cookie == COOKIE_MAPPED) {
956                                 dma_unmap_sg(mmc_dev(host->mmc),
957                                         data->sg, data->sg_len,
958                                         (data->flags & MMC_DATA_READ) ?
959                                         DMA_FROM_DEVICE : DMA_TO_DEVICE);
960                                 data->host_cookie = COOKIE_UNMAPPED;
961                         }
962                 }
963         }
964
965         /*
966          * The specification states that the block count register must
967          * be updated, but it does not specify at what point in the
968          * data flow. That makes the register entirely useless to read
969          * back so we have to assume that nothing made it to the card
970          * in the event of an error.
971          */
972         if (data->error)
973                 data->bytes_xfered = 0;
974         else
975                 data->bytes_xfered = data->blksz * data->blocks;
976
977         /*
978          * Need to send CMD12 if -
979          * a) open-ended multiblock transfer (no CMD23)
980          * b) error in multiblock transfer
981          */
982         if (data->stop &&
983             (data->error ||
984              !host->mrq->sbc)) {
985
986                 /*
987                  * The controller needs a reset of internal state machines
988                  * upon error conditions.
989                  */
990                 if (data->error) {
991                         sdhci_do_reset(host, SDHCI_RESET_CMD);
992                         sdhci_do_reset(host, SDHCI_RESET_DATA);
993                 }
994
995                 sdhci_send_command(host, data->stop);
996         } else
997                 tasklet_schedule(&host->finish_tasklet);
998 }
999
1000 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1001 {
1002         int flags;
1003         u32 mask;
1004         unsigned long timeout;
1005
1006         WARN_ON(host->cmd);
1007
1008         /* Wait max 10 ms */
1009         timeout = 10;
1010
1011         mask = SDHCI_CMD_INHIBIT;
1012         if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1013                 mask |= SDHCI_DATA_INHIBIT;
1014
1015         /* We shouldn't wait for data inihibit for stop commands, even
1016            though they might use busy signaling */
1017         if (host->mrq->data && (cmd == host->mrq->data->stop))
1018                 mask &= ~SDHCI_DATA_INHIBIT;
1019
1020         while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1021                 if (timeout == 0) {
1022                         pr_err("%s: Controller never released "
1023                                 "inhibit bit(s).\n", mmc_hostname(host->mmc));
1024                         sdhci_dumpregs(host);
1025                         cmd->error = -EIO;
1026                         tasklet_schedule(&host->finish_tasklet);
1027                         return;
1028                 }
1029                 timeout--;
1030                 mdelay(1);
1031         }
1032
1033         timeout = jiffies;
1034         if (!cmd->data && cmd->busy_timeout > 9000)
1035                 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1036         else
1037                 timeout += 10 * HZ;
1038         mod_timer(&host->timer, timeout);
1039
1040         host->cmd = cmd;
1041         host->busy_handle = 0;
1042
1043         sdhci_prepare_data(host, cmd);
1044
1045         sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1046
1047         sdhci_set_transfer_mode(host, cmd);
1048
1049         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1050                 pr_err("%s: Unsupported response type!\n",
1051                         mmc_hostname(host->mmc));
1052                 cmd->error = -EINVAL;
1053                 tasklet_schedule(&host->finish_tasklet);
1054                 return;
1055         }
1056
1057         if (!(cmd->flags & MMC_RSP_PRESENT))
1058                 flags = SDHCI_CMD_RESP_NONE;
1059         else if (cmd->flags & MMC_RSP_136)
1060                 flags = SDHCI_CMD_RESP_LONG;
1061         else if (cmd->flags & MMC_RSP_BUSY)
1062                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1063         else
1064                 flags = SDHCI_CMD_RESP_SHORT;
1065
1066         if (cmd->flags & MMC_RSP_CRC)
1067                 flags |= SDHCI_CMD_CRC;
1068         if (cmd->flags & MMC_RSP_OPCODE)
1069                 flags |= SDHCI_CMD_INDEX;
1070
1071         /* CMD19 is special in that the Data Present Select should be set */
1072         if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1073             cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1074                 flags |= SDHCI_CMD_DATA;
1075
1076         sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1077 }
1078 EXPORT_SYMBOL_GPL(sdhci_send_command);
1079
1080 static void sdhci_finish_command(struct sdhci_host *host)
1081 {
1082         int i;
1083
1084         BUG_ON(host->cmd == NULL);
1085
1086         if (host->cmd->flags & MMC_RSP_PRESENT) {
1087                 if (host->cmd->flags & MMC_RSP_136) {
1088                         /* CRC is stripped so we need to do some shifting. */
1089                         for (i = 0;i < 4;i++) {
1090                                 host->cmd->resp[i] = sdhci_readl(host,
1091                                         SDHCI_RESPONSE + (3-i)*4) << 8;
1092                                 if (i != 3)
1093                                         host->cmd->resp[i] |=
1094                                                 sdhci_readb(host,
1095                                                 SDHCI_RESPONSE + (3-i)*4-1);
1096                         }
1097                 } else {
1098                         host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1099                 }
1100         }
1101
1102         host->cmd->error = 0;
1103
1104         /* Finished CMD23, now send actual command. */
1105         if (host->cmd == host->mrq->sbc) {
1106                 host->cmd = NULL;
1107                 sdhci_send_command(host, host->mrq->cmd);
1108         } else {
1109
1110                 /* Processed actual command. */
1111                 if (host->data && host->data_early)
1112                         sdhci_finish_data(host);
1113
1114                 if (!host->cmd->data)
1115                         tasklet_schedule(&host->finish_tasklet);
1116
1117                 host->cmd = NULL;
1118         }
1119 }
1120
1121 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1122 {
1123         u16 preset = 0;
1124
1125         switch (host->timing) {
1126         case MMC_TIMING_UHS_SDR12:
1127                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1128                 break;
1129         case MMC_TIMING_UHS_SDR25:
1130                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1131                 break;
1132         case MMC_TIMING_UHS_SDR50:
1133                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1134                 break;
1135         case MMC_TIMING_UHS_SDR104:
1136         case MMC_TIMING_MMC_HS200:
1137                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1138                 break;
1139         case MMC_TIMING_UHS_DDR50:
1140         case MMC_TIMING_MMC_DDR52:
1141                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1142                 break;
1143         case MMC_TIMING_MMC_HS400:
1144                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1145                 break;
1146         default:
1147                 pr_warn("%s: Invalid UHS-I mode selected\n",
1148                         mmc_hostname(host->mmc));
1149                 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1150                 break;
1151         }
1152         return preset;
1153 }
1154
1155 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1156 {
1157         int div = 0; /* Initialized for compiler warning */
1158         int real_div = div, clk_mul = 1;
1159         u16 clk = 0;
1160         unsigned long timeout;
1161         bool switch_base_clk = false;
1162
1163         host->mmc->actual_clock = 0;
1164
1165         sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1166         if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1167                 mdelay(1);
1168
1169         if (clock == 0)
1170                 return;
1171
1172         if (host->version >= SDHCI_SPEC_300) {
1173                 if (host->preset_enabled) {
1174                         u16 pre_val;
1175
1176                         clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1177                         pre_val = sdhci_get_preset_value(host);
1178                         div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1179                                 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1180                         if (host->clk_mul &&
1181                                 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1182                                 clk = SDHCI_PROG_CLOCK_MODE;
1183                                 real_div = div + 1;
1184                                 clk_mul = host->clk_mul;
1185                         } else {
1186                                 real_div = max_t(int, 1, div << 1);
1187                         }
1188                         goto clock_set;
1189                 }
1190
1191                 /*
1192                  * Check if the Host Controller supports Programmable Clock
1193                  * Mode.
1194                  */
1195                 if (host->clk_mul) {
1196                         for (div = 1; div <= 1024; div++) {
1197                                 if ((host->max_clk * host->clk_mul / div)
1198                                         <= clock)
1199                                         break;
1200                         }
1201                         if ((host->max_clk * host->clk_mul / div) <= clock) {
1202                                 /*
1203                                  * Set Programmable Clock Mode in the Clock
1204                                  * Control register.
1205                                  */
1206                                 clk = SDHCI_PROG_CLOCK_MODE;
1207                                 real_div = div;
1208                                 clk_mul = host->clk_mul;
1209                                 div--;
1210                         } else {
1211                                 /*
1212                                  * Divisor can be too small to reach clock
1213                                  * speed requirement. Then use the base clock.
1214                                  */
1215                                 switch_base_clk = true;
1216                         }
1217                 }
1218
1219                 if (!host->clk_mul || switch_base_clk) {
1220                         /* Version 3.00 divisors must be a multiple of 2. */
1221                         if (host->max_clk <= clock)
1222                                 div = 1;
1223                         else {
1224                                 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1225                                      div += 2) {
1226                                         if ((host->max_clk / div) <= clock)
1227                                                 break;
1228                                 }
1229                         }
1230                         real_div = div;
1231                         div >>= 1;
1232                         if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1233                                 && !div && host->max_clk <= 25000000)
1234                                 div = 1;
1235                 }
1236         } else {
1237                 /* Version 2.00 divisors must be a power of 2. */
1238                 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1239                         if ((host->max_clk / div) <= clock)
1240                                 break;
1241                 }
1242                 real_div = div;
1243                 div >>= 1;
1244         }
1245
1246 clock_set:
1247         if (real_div)
1248                 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1249         clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1250         clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1251                 << SDHCI_DIVIDER_HI_SHIFT;
1252         clk |= SDHCI_CLOCK_INT_EN;
1253         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1254
1255         /* Wait max 20 ms */
1256         timeout = 20;
1257         while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1258                 & SDHCI_CLOCK_INT_STABLE)) {
1259                 if (timeout == 0) {
1260                         pr_err("%s: Internal clock never "
1261                                 "stabilised.\n", mmc_hostname(host->mmc));
1262                         sdhci_dumpregs(host);
1263                         return;
1264                 }
1265                 timeout--;
1266                 mdelay(1);
1267         }
1268
1269         clk |= SDHCI_CLOCK_CARD_EN;
1270         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1271 }
1272 EXPORT_SYMBOL_GPL(sdhci_set_clock);
1273
1274 static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1275                             unsigned short vdd)
1276 {
1277         struct mmc_host *mmc = host->mmc;
1278         u8 pwr = 0;
1279
1280         if (!IS_ERR(mmc->supply.vmmc)) {
1281                 spin_unlock_irq(&host->lock);
1282                 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1283                 spin_lock_irq(&host->lock);
1284
1285                 if (mode != MMC_POWER_OFF)
1286                         sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1287                 else
1288                         sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1289
1290                 return;
1291         }
1292
1293         if (mode != MMC_POWER_OFF) {
1294                 switch (1 << vdd) {
1295                 case MMC_VDD_165_195:
1296                         pwr = SDHCI_POWER_180;
1297                         break;
1298                 case MMC_VDD_29_30:
1299                 case MMC_VDD_30_31:
1300                         pwr = SDHCI_POWER_300;
1301                         break;
1302                 case MMC_VDD_32_33:
1303                 case MMC_VDD_33_34:
1304                         pwr = SDHCI_POWER_330;
1305                         break;
1306                 default:
1307                         BUG();
1308                 }
1309         }
1310
1311         if (host->pwr == pwr)
1312                 return;
1313
1314         host->pwr = pwr;
1315
1316         if (pwr == 0) {
1317                 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1318                 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1319                         sdhci_runtime_pm_bus_off(host);
1320                 vdd = 0;
1321         } else {
1322                 /*
1323                  * Spec says that we should clear the power reg before setting
1324                  * a new value. Some controllers don't seem to like this though.
1325                  */
1326                 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1327                         sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1328
1329                 /*
1330                  * At least the Marvell CaFe chip gets confused if we set the
1331                  * voltage and set turn on power at the same time, so set the
1332                  * voltage first.
1333                  */
1334                 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1335                         sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1336
1337                 pwr |= SDHCI_POWER_ON;
1338
1339                 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1340
1341                 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1342                         sdhci_runtime_pm_bus_on(host);
1343
1344                 /*
1345                  * Some controllers need an extra 10ms delay of 10ms before
1346                  * they can apply clock after applying power
1347                  */
1348                 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1349                         mdelay(10);
1350         }
1351 }
1352
1353 /*****************************************************************************\
1354  *                                                                           *
1355  * MMC callbacks                                                             *
1356  *                                                                           *
1357 \*****************************************************************************/
1358
1359 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1360 {
1361         struct sdhci_host *host;
1362         int present;
1363         unsigned long flags;
1364
1365         host = mmc_priv(mmc);
1366
1367         sdhci_runtime_pm_get(host);
1368
1369         /* Firstly check card presence */
1370         present = mmc->ops->get_cd(mmc);
1371
1372         spin_lock_irqsave(&host->lock, flags);
1373
1374         WARN_ON(host->mrq != NULL);
1375
1376 #ifndef SDHCI_USE_LEDS_CLASS
1377         sdhci_activate_led(host);
1378 #endif
1379
1380         /*
1381          * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1382          * requests if Auto-CMD12 is enabled.
1383          */
1384         if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1385                 if (mrq->stop) {
1386                         mrq->data->stop = NULL;
1387                         mrq->stop = NULL;
1388                 }
1389         }
1390
1391         host->mrq = mrq;
1392
1393         if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1394                 host->mrq->cmd->error = -ENOMEDIUM;
1395                 tasklet_schedule(&host->finish_tasklet);
1396         } else {
1397                 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1398                         sdhci_send_command(host, mrq->sbc);
1399                 else
1400                         sdhci_send_command(host, mrq->cmd);
1401         }
1402
1403         mmiowb();
1404         spin_unlock_irqrestore(&host->lock, flags);
1405 }
1406
1407 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1408 {
1409         u8 ctrl;
1410
1411         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1412         if (width == MMC_BUS_WIDTH_8) {
1413                 ctrl &= ~SDHCI_CTRL_4BITBUS;
1414                 if (host->version >= SDHCI_SPEC_300)
1415                         ctrl |= SDHCI_CTRL_8BITBUS;
1416         } else {
1417                 if (host->version >= SDHCI_SPEC_300)
1418                         ctrl &= ~SDHCI_CTRL_8BITBUS;
1419                 if (width == MMC_BUS_WIDTH_4)
1420                         ctrl |= SDHCI_CTRL_4BITBUS;
1421                 else
1422                         ctrl &= ~SDHCI_CTRL_4BITBUS;
1423         }
1424         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1425 }
1426 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1427
1428 void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1429 {
1430         u16 ctrl_2;
1431
1432         ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1433         /* Select Bus Speed Mode for host */
1434         ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1435         if ((timing == MMC_TIMING_MMC_HS200) ||
1436             (timing == MMC_TIMING_UHS_SDR104))
1437                 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1438         else if (timing == MMC_TIMING_UHS_SDR12)
1439                 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1440         else if (timing == MMC_TIMING_UHS_SDR25)
1441                 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1442         else if (timing == MMC_TIMING_UHS_SDR50)
1443                 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1444         else if ((timing == MMC_TIMING_UHS_DDR50) ||
1445                  (timing == MMC_TIMING_MMC_DDR52))
1446                 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1447         else if (timing == MMC_TIMING_MMC_HS400)
1448                 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
1449         sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1450 }
1451 EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1452
1453 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1454 {
1455         unsigned long flags;
1456         u8 ctrl;
1457         struct mmc_host *mmc = host->mmc;
1458
1459         spin_lock_irqsave(&host->lock, flags);
1460
1461         if (host->flags & SDHCI_DEVICE_DEAD) {
1462                 spin_unlock_irqrestore(&host->lock, flags);
1463                 if (!IS_ERR(mmc->supply.vmmc) &&
1464                     ios->power_mode == MMC_POWER_OFF)
1465                         mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1466                 return;
1467         }
1468
1469         /*
1470          * Reset the chip on each power off.
1471          * Should clear out any weird states.
1472          */
1473         if (ios->power_mode == MMC_POWER_OFF) {
1474                 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1475                 sdhci_reinit(host);
1476         }
1477
1478         if (host->version >= SDHCI_SPEC_300 &&
1479                 (ios->power_mode == MMC_POWER_UP) &&
1480                 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1481                 sdhci_enable_preset_value(host, false);
1482
1483         if (!ios->clock || ios->clock != host->clock) {
1484                 host->ops->set_clock(host, ios->clock);
1485                 host->clock = ios->clock;
1486
1487                 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1488                     host->clock) {
1489                         host->timeout_clk = host->mmc->actual_clock ?
1490                                                 host->mmc->actual_clock / 1000 :
1491                                                 host->clock / 1000;
1492                         host->mmc->max_busy_timeout =
1493                                 host->ops->get_max_timeout_count ?
1494                                 host->ops->get_max_timeout_count(host) :
1495                                 1 << 27;
1496                         host->mmc->max_busy_timeout /= host->timeout_clk;
1497                 }
1498         }
1499
1500         sdhci_set_power(host, ios->power_mode, ios->vdd);
1501
1502         if (host->ops->platform_send_init_74_clocks)
1503                 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1504
1505         host->ops->set_bus_width(host, ios->bus_width);
1506
1507         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1508
1509         if ((ios->timing == MMC_TIMING_SD_HS ||
1510              ios->timing == MMC_TIMING_MMC_HS)
1511             && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1512                 ctrl |= SDHCI_CTRL_HISPD;
1513         else
1514                 ctrl &= ~SDHCI_CTRL_HISPD;
1515
1516         if (host->version >= SDHCI_SPEC_300) {
1517                 u16 clk, ctrl_2;
1518
1519                 /* In case of UHS-I modes, set High Speed Enable */
1520                 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1521                     (ios->timing == MMC_TIMING_MMC_HS200) ||
1522                     (ios->timing == MMC_TIMING_MMC_DDR52) ||
1523                     (ios->timing == MMC_TIMING_UHS_SDR50) ||
1524                     (ios->timing == MMC_TIMING_UHS_SDR104) ||
1525                     (ios->timing == MMC_TIMING_UHS_DDR50) ||
1526                     (ios->timing == MMC_TIMING_UHS_SDR25))
1527                         ctrl |= SDHCI_CTRL_HISPD;
1528
1529                 if (!host->preset_enabled) {
1530                         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1531                         /*
1532                          * We only need to set Driver Strength if the
1533                          * preset value enable is not set.
1534                          */
1535                         ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1536                         ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1537                         if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1538                                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1539                         else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1540                                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1541                         else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1542                                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1543                         else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1544                                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1545                         else {
1546                                 pr_warn("%s: invalid driver type, default to "
1547                                         "driver type B\n", mmc_hostname(mmc));
1548                                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1549                         }
1550
1551                         sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1552                 } else {
1553                         /*
1554                          * According to SDHC Spec v3.00, if the Preset Value
1555                          * Enable in the Host Control 2 register is set, we
1556                          * need to reset SD Clock Enable before changing High
1557                          * Speed Enable to avoid generating clock gliches.
1558                          */
1559
1560                         /* Reset SD Clock Enable */
1561                         clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1562                         clk &= ~SDHCI_CLOCK_CARD_EN;
1563                         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1564
1565                         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1566
1567                         /* Re-enable SD Clock */
1568                         host->ops->set_clock(host, host->clock);
1569                 }
1570
1571                 /* Reset SD Clock Enable */
1572                 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1573                 clk &= ~SDHCI_CLOCK_CARD_EN;
1574                 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1575
1576                 host->ops->set_uhs_signaling(host, ios->timing);
1577                 host->timing = ios->timing;
1578
1579                 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1580                                 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1581                                  (ios->timing == MMC_TIMING_UHS_SDR25) ||
1582                                  (ios->timing == MMC_TIMING_UHS_SDR50) ||
1583                                  (ios->timing == MMC_TIMING_UHS_SDR104) ||
1584                                  (ios->timing == MMC_TIMING_UHS_DDR50) ||
1585                                  (ios->timing == MMC_TIMING_MMC_DDR52))) {
1586                         u16 preset;
1587
1588                         sdhci_enable_preset_value(host, true);
1589                         preset = sdhci_get_preset_value(host);
1590                         ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1591                                 >> SDHCI_PRESET_DRV_SHIFT;
1592                 }
1593
1594                 /* Re-enable SD Clock */
1595                 host->ops->set_clock(host, host->clock);
1596         } else
1597                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1598
1599         /*
1600          * Some (ENE) controllers go apeshit on some ios operation,
1601          * signalling timeout and CRC errors even on CMD0. Resetting
1602          * it on each ios seems to solve the problem.
1603          */
1604         if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1605                 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1606
1607         mmiowb();
1608         spin_unlock_irqrestore(&host->lock, flags);
1609 }
1610
1611 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1612 {
1613         struct sdhci_host *host = mmc_priv(mmc);
1614
1615         sdhci_runtime_pm_get(host);
1616         sdhci_do_set_ios(host, ios);
1617         sdhci_runtime_pm_put(host);
1618 }
1619
1620 static int sdhci_do_get_cd(struct sdhci_host *host)
1621 {
1622         int gpio_cd = mmc_gpio_get_cd(host->mmc);
1623
1624         if (host->flags & SDHCI_DEVICE_DEAD)
1625                 return 0;
1626
1627         /* If nonremovable, assume that the card is always present. */
1628         if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
1629                 return 1;
1630
1631         /*
1632          * Try slot gpio detect, if defined it take precedence
1633          * over build in controller functionality
1634          */
1635         if (!IS_ERR_VALUE(gpio_cd))
1636                 return !!gpio_cd;
1637
1638         /* If polling, assume that the card is always present. */
1639         if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1640                 return 1;
1641
1642         /* Host native card detect */
1643         return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1644 }
1645
1646 static int sdhci_get_cd(struct mmc_host *mmc)
1647 {
1648         struct sdhci_host *host = mmc_priv(mmc);
1649         int ret;
1650
1651         sdhci_runtime_pm_get(host);
1652         ret = sdhci_do_get_cd(host);
1653         sdhci_runtime_pm_put(host);
1654         return ret;
1655 }
1656
1657 static int sdhci_check_ro(struct sdhci_host *host)
1658 {
1659         unsigned long flags;
1660         int is_readonly;
1661
1662         spin_lock_irqsave(&host->lock, flags);
1663
1664         if (host->flags & SDHCI_DEVICE_DEAD)
1665                 is_readonly = 0;
1666         else if (host->ops->get_ro)
1667                 is_readonly = host->ops->get_ro(host);
1668         else
1669                 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1670                                 & SDHCI_WRITE_PROTECT);
1671
1672         spin_unlock_irqrestore(&host->lock, flags);
1673
1674         /* This quirk needs to be replaced by a callback-function later */
1675         return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1676                 !is_readonly : is_readonly;
1677 }
1678
1679 #define SAMPLE_COUNT    5
1680
1681 static int sdhci_do_get_ro(struct sdhci_host *host)
1682 {
1683         int i, ro_count;
1684
1685         if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1686                 return sdhci_check_ro(host);
1687
1688         ro_count = 0;
1689         for (i = 0; i < SAMPLE_COUNT; i++) {
1690                 if (sdhci_check_ro(host)) {
1691                         if (++ro_count > SAMPLE_COUNT / 2)
1692                                 return 1;
1693                 }
1694                 msleep(30);
1695         }
1696         return 0;
1697 }
1698
1699 static void sdhci_hw_reset(struct mmc_host *mmc)
1700 {
1701         struct sdhci_host *host = mmc_priv(mmc);
1702
1703         if (host->ops && host->ops->hw_reset)
1704                 host->ops->hw_reset(host);
1705 }
1706
1707 static int sdhci_get_ro(struct mmc_host *mmc)
1708 {
1709         struct sdhci_host *host = mmc_priv(mmc);
1710         int ret;
1711
1712         sdhci_runtime_pm_get(host);
1713         ret = sdhci_do_get_ro(host);
1714         sdhci_runtime_pm_put(host);
1715         return ret;
1716 }
1717
1718 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1719 {
1720         if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1721                 if (enable)
1722                         host->ier |= SDHCI_INT_CARD_INT;
1723                 else
1724                         host->ier &= ~SDHCI_INT_CARD_INT;
1725
1726                 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1727                 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1728                 mmiowb();
1729         }
1730 }
1731
1732 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1733 {
1734         struct sdhci_host *host = mmc_priv(mmc);
1735         unsigned long flags;
1736
1737         sdhci_runtime_pm_get(host);
1738
1739         spin_lock_irqsave(&host->lock, flags);
1740         if (enable)
1741                 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1742         else
1743                 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1744
1745         sdhci_enable_sdio_irq_nolock(host, enable);
1746         spin_unlock_irqrestore(&host->lock, flags);
1747
1748         sdhci_runtime_pm_put(host);
1749 }
1750
1751 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1752                                                 struct mmc_ios *ios)
1753 {
1754         struct mmc_host *mmc = host->mmc;
1755         u16 ctrl;
1756         int ret;
1757
1758         /*
1759          * Signal Voltage Switching is only applicable for Host Controllers
1760          * v3.00 and above.
1761          */
1762         if (host->version < SDHCI_SPEC_300)
1763                 return 0;
1764
1765         ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1766
1767         switch (ios->signal_voltage) {
1768         case MMC_SIGNAL_VOLTAGE_330:
1769                 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1770                 ctrl &= ~SDHCI_CTRL_VDD_180;
1771                 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1772
1773                 if (!IS_ERR(mmc->supply.vqmmc)) {
1774                         ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1775                                                     3600000);
1776                         if (ret) {
1777                                 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1778                                         mmc_hostname(mmc));
1779                                 return -EIO;
1780                         }
1781                 }
1782                 /* Wait for 5ms */
1783                 usleep_range(5000, 5500);
1784
1785                 /* 3.3V regulator output should be stable within 5 ms */
1786                 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1787                 if (!(ctrl & SDHCI_CTRL_VDD_180))
1788                         return 0;
1789
1790                 pr_warn("%s: 3.3V regulator output did not became stable\n",
1791                         mmc_hostname(mmc));
1792
1793                 return -EAGAIN;
1794         case MMC_SIGNAL_VOLTAGE_180:
1795                 if (!IS_ERR(mmc->supply.vqmmc)) {
1796                         ret = regulator_set_voltage(mmc->supply.vqmmc,
1797                                         1700000, 1950000);
1798                         if (ret) {
1799                                 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1800                                         mmc_hostname(mmc));
1801                                 return -EIO;
1802                         }
1803                 }
1804
1805                 /*
1806                  * Enable 1.8V Signal Enable in the Host Control2
1807                  * register
1808                  */
1809                 ctrl |= SDHCI_CTRL_VDD_180;
1810                 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1811
1812                 /* Some controller need to do more when switching */
1813                 if (host->ops->voltage_switch)
1814                         host->ops->voltage_switch(host);
1815
1816                 /* 1.8V regulator output should be stable within 5 ms */
1817                 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1818                 if (ctrl & SDHCI_CTRL_VDD_180)
1819                         return 0;
1820
1821                 pr_warn("%s: 1.8V regulator output did not became stable\n",
1822                         mmc_hostname(mmc));
1823
1824                 return -EAGAIN;
1825         case MMC_SIGNAL_VOLTAGE_120:
1826                 if (!IS_ERR(mmc->supply.vqmmc)) {
1827                         ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1828                                                     1300000);
1829                         if (ret) {
1830                                 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1831                                         mmc_hostname(mmc));
1832                                 return -EIO;
1833                         }
1834                 }
1835                 return 0;
1836         default:
1837                 /* No signal voltage switch required */
1838                 return 0;
1839         }
1840 }
1841
1842 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1843         struct mmc_ios *ios)
1844 {
1845         struct sdhci_host *host = mmc_priv(mmc);
1846         int err;
1847
1848         if (host->version < SDHCI_SPEC_300)
1849                 return 0;
1850         sdhci_runtime_pm_get(host);
1851         err = sdhci_do_start_signal_voltage_switch(host, ios);
1852         sdhci_runtime_pm_put(host);
1853         return err;
1854 }
1855
1856 static int sdhci_card_busy(struct mmc_host *mmc)
1857 {
1858         struct sdhci_host *host = mmc_priv(mmc);
1859         u32 present_state;
1860
1861         sdhci_runtime_pm_get(host);
1862         /* Check whether DAT[3:0] is 0000 */
1863         present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1864         sdhci_runtime_pm_put(host);
1865
1866         return !(present_state & SDHCI_DATA_LVL_MASK);
1867 }
1868
1869 static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1870 {
1871         struct sdhci_host *host = mmc_priv(mmc);
1872         unsigned long flags;
1873
1874         spin_lock_irqsave(&host->lock, flags);
1875         host->flags |= SDHCI_HS400_TUNING;
1876         spin_unlock_irqrestore(&host->lock, flags);
1877
1878         return 0;
1879 }
1880
1881 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1882 {
1883         struct sdhci_host *host = mmc_priv(mmc);
1884         u16 ctrl;
1885         int tuning_loop_counter = MAX_TUNING_LOOP;
1886         int err = 0;
1887         unsigned long flags;
1888         unsigned int tuning_count = 0;
1889         bool hs400_tuning;
1890
1891         sdhci_runtime_pm_get(host);
1892         spin_lock_irqsave(&host->lock, flags);
1893
1894         hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1895         host->flags &= ~SDHCI_HS400_TUNING;
1896
1897         if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1898                 tuning_count = host->tuning_count;
1899
1900         /*
1901          * The Host Controller needs tuning in case of SDR104 and DDR50
1902          * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1903          * the Capabilities register.
1904          * If the Host Controller supports the HS200 mode then the
1905          * tuning function has to be executed.
1906          */
1907         switch (host->timing) {
1908         /* HS400 tuning is done in HS200 mode */
1909         case MMC_TIMING_MMC_HS400:
1910                 err = -EINVAL;
1911                 goto out_unlock;
1912
1913         case MMC_TIMING_MMC_HS200:
1914                 /*
1915                  * Periodic re-tuning for HS400 is not expected to be needed, so
1916                  * disable it here.
1917                  */
1918                 if (hs400_tuning)
1919                         tuning_count = 0;
1920                 break;
1921
1922         case MMC_TIMING_UHS_SDR104:
1923         case MMC_TIMING_UHS_DDR50:
1924                 break;
1925
1926         case MMC_TIMING_UHS_SDR50:
1927                 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1928                     host->flags & SDHCI_SDR104_NEEDS_TUNING)
1929                         break;
1930                 /* FALLTHROUGH */
1931
1932         default:
1933                 goto out_unlock;
1934         }
1935
1936         if (host->ops->platform_execute_tuning) {
1937                 spin_unlock_irqrestore(&host->lock, flags);
1938                 err = host->ops->platform_execute_tuning(host, opcode);
1939                 sdhci_runtime_pm_put(host);
1940                 return err;
1941         }
1942
1943         ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1944         ctrl |= SDHCI_CTRL_EXEC_TUNING;
1945         if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1946                 ctrl |= SDHCI_CTRL_TUNED_CLK;
1947         sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1948
1949         /*
1950          * As per the Host Controller spec v3.00, tuning command
1951          * generates Buffer Read Ready interrupt, so enable that.
1952          *
1953          * Note: The spec clearly says that when tuning sequence
1954          * is being performed, the controller does not generate
1955          * interrupts other than Buffer Read Ready interrupt. But
1956          * to make sure we don't hit a controller bug, we _only_
1957          * enable Buffer Read Ready interrupt here.
1958          */
1959         sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1960         sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1961
1962         /*
1963          * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1964          * of loops reaches 40 times or a timeout of 150ms occurs.
1965          */
1966         do {
1967                 struct mmc_command cmd = {0};
1968                 struct mmc_request mrq = {NULL};
1969
1970                 cmd.opcode = opcode;
1971                 cmd.arg = 0;
1972                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1973                 cmd.retries = 0;
1974                 cmd.data = NULL;
1975                 cmd.error = 0;
1976
1977                 if (tuning_loop_counter-- == 0)
1978                         break;
1979
1980                 mrq.cmd = &cmd;
1981                 host->mrq = &mrq;
1982
1983                 /*
1984                  * In response to CMD19, the card sends 64 bytes of tuning
1985                  * block to the Host Controller. So we set the block size
1986                  * to 64 here.
1987                  */
1988                 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1989                         if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1990                                 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1991                                              SDHCI_BLOCK_SIZE);
1992                         else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1993                                 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1994                                              SDHCI_BLOCK_SIZE);
1995                 } else {
1996                         sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1997                                      SDHCI_BLOCK_SIZE);
1998                 }
1999
2000                 /*
2001                  * The tuning block is sent by the card to the host controller.
2002                  * So we set the TRNS_READ bit in the Transfer Mode register.
2003                  * This also takes care of setting DMA Enable and Multi Block
2004                  * Select in the same register to 0.
2005                  */
2006                 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2007
2008                 sdhci_send_command(host, &cmd);
2009
2010                 host->cmd = NULL;
2011                 host->mrq = NULL;
2012
2013                 spin_unlock_irqrestore(&host->lock, flags);
2014                 /* Wait for Buffer Read Ready interrupt */
2015                 wait_event_interruptible_timeout(host->buf_ready_int,
2016                                         (host->tuning_done == 1),
2017                                         msecs_to_jiffies(50));
2018                 spin_lock_irqsave(&host->lock, flags);
2019
2020                 if (!host->tuning_done) {
2021                         pr_info(DRIVER_NAME ": Timeout waiting for "
2022                                 "Buffer Read Ready interrupt during tuning "
2023                                 "procedure, falling back to fixed sampling "
2024                                 "clock\n");
2025                         ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2026                         ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2027                         ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2028                         sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2029
2030                         err = -EIO;
2031                         goto out;
2032                 }
2033
2034                 host->tuning_done = 0;
2035
2036                 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2037
2038                 /* eMMC spec does not require a delay between tuning cycles */
2039                 if (opcode == MMC_SEND_TUNING_BLOCK)
2040                         mdelay(1);
2041         } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2042
2043         /*
2044          * The Host Driver has exhausted the maximum number of loops allowed,
2045          * so use fixed sampling frequency.
2046          */
2047         if (tuning_loop_counter < 0) {
2048                 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2049                 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2050         }
2051         if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2052                 pr_info(DRIVER_NAME ": Tuning procedure"
2053                         " failed, falling back to fixed sampling"
2054                         " clock\n");
2055                 err = -EIO;
2056         }
2057
2058 out:
2059         if (tuning_count) {
2060                 /*
2061                  * In case tuning fails, host controllers which support
2062                  * re-tuning can try tuning again at a later time, when the
2063                  * re-tuning timer expires.  So for these controllers, we
2064                  * return 0. Since there might be other controllers who do not
2065                  * have this capability, we return error for them.
2066                  */
2067                 err = 0;
2068         }
2069
2070         host->mmc->retune_period = err ? 0 : tuning_count;
2071
2072         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2073         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2074 out_unlock:
2075         spin_unlock_irqrestore(&host->lock, flags);
2076         sdhci_runtime_pm_put(host);
2077
2078         return err;
2079 }
2080
2081 static int sdhci_prepare_enhanced_strobe(struct mmc_host *mmc, bool enable)
2082 {
2083         /*
2084         * Currently we can't find a register to enable enhanced strobe
2085         * function for standard sdhci, so we expect variant drivers to
2086         * overwrite it.
2087         */
2088         return -EINVAL;
2089 }
2090
2091 static int sdhci_select_drive_strength(struct mmc_card *card,
2092                                        unsigned int max_dtr, int host_drv,
2093                                        int card_drv, int *drv_type)
2094 {
2095         struct sdhci_host *host = mmc_priv(card->host);
2096
2097         if (!host->ops->select_drive_strength)
2098                 return 0;
2099
2100         return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2101                                                 card_drv, drv_type);
2102 }
2103
2104 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2105 {
2106         /* Host Controller v3.00 defines preset value registers */
2107         if (host->version < SDHCI_SPEC_300)
2108                 return;
2109
2110         /*
2111          * We only enable or disable Preset Value if they are not already
2112          * enabled or disabled respectively. Otherwise, we bail out.
2113          */
2114         if (host->preset_enabled != enable) {
2115                 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2116
2117                 if (enable)
2118                         ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2119                 else
2120                         ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2121
2122                 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2123
2124                 if (enable)
2125                         host->flags |= SDHCI_PV_ENABLED;
2126                 else
2127                         host->flags &= ~SDHCI_PV_ENABLED;
2128
2129                 host->preset_enabled = enable;
2130         }
2131 }
2132
2133 static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2134                                 int err)
2135 {
2136         struct sdhci_host *host = mmc_priv(mmc);
2137         struct mmc_data *data = mrq->data;
2138
2139         if (host->flags & SDHCI_REQ_USE_DMA) {
2140                 if (data->host_cookie == COOKIE_GIVEN ||
2141                                 data->host_cookie == COOKIE_MAPPED)
2142                         dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2143                                          data->flags & MMC_DATA_WRITE ?
2144                                          DMA_TO_DEVICE : DMA_FROM_DEVICE);
2145                 data->host_cookie = COOKIE_UNMAPPED;
2146         }
2147 }
2148
2149 static int sdhci_pre_dma_transfer(struct sdhci_host *host,
2150                                        struct mmc_data *data)
2151 {
2152         int sg_count;
2153
2154         if (data->host_cookie == COOKIE_MAPPED) {
2155                 data->host_cookie = COOKIE_GIVEN;
2156                 return data->sg_count;
2157         }
2158
2159         WARN_ON(data->host_cookie == COOKIE_GIVEN);
2160
2161         sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2162                                 data->flags & MMC_DATA_WRITE ?
2163                                 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2164
2165         if (sg_count == 0)
2166                 return -ENOSPC;
2167
2168         data->sg_count = sg_count;
2169         data->host_cookie = COOKIE_MAPPED;
2170
2171         return sg_count;
2172 }
2173
2174 static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2175                                bool is_first_req)
2176 {
2177         struct sdhci_host *host = mmc_priv(mmc);
2178
2179         mrq->data->host_cookie = COOKIE_UNMAPPED;
2180
2181         if (host->flags & SDHCI_REQ_USE_DMA)
2182                 sdhci_pre_dma_transfer(host, mrq->data);
2183 }
2184
2185 static void sdhci_card_event(struct mmc_host *mmc)
2186 {
2187         struct sdhci_host *host = mmc_priv(mmc);
2188         unsigned long flags;
2189         int present;
2190
2191         /* First check if client has provided their own card event */
2192         if (host->ops->card_event)
2193                 host->ops->card_event(host);
2194
2195         present = sdhci_do_get_cd(host);
2196
2197         spin_lock_irqsave(&host->lock, flags);
2198
2199         /* Check host->mrq first in case we are runtime suspended */
2200         if (host->mrq && !present) {
2201                 pr_err("%s: Card removed during transfer!\n",
2202                         mmc_hostname(host->mmc));
2203                 pr_err("%s: Resetting controller.\n",
2204                         mmc_hostname(host->mmc));
2205
2206                 sdhci_do_reset(host, SDHCI_RESET_CMD);
2207                 sdhci_do_reset(host, SDHCI_RESET_DATA);
2208
2209                 host->mrq->cmd->error = -ENOMEDIUM;
2210                 tasklet_schedule(&host->finish_tasklet);
2211         }
2212
2213         spin_unlock_irqrestore(&host->lock, flags);
2214 }
2215
2216 static const struct mmc_host_ops sdhci_ops = {
2217         .request        = sdhci_request,
2218         .post_req       = sdhci_post_req,
2219         .pre_req        = sdhci_pre_req,
2220         .set_ios        = sdhci_set_ios,
2221         .get_cd         = sdhci_get_cd,
2222         .get_ro         = sdhci_get_ro,
2223         .hw_reset       = sdhci_hw_reset,
2224         .enable_sdio_irq = sdhci_enable_sdio_irq,
2225         .start_signal_voltage_switch    = sdhci_start_signal_voltage_switch,
2226         .prepare_hs400_tuning           = sdhci_prepare_hs400_tuning,
2227         .prepare_enhanced_strobe        = sdhci_prepare_enhanced_strobe,
2228         .execute_tuning                 = sdhci_execute_tuning,
2229         .select_drive_strength          = sdhci_select_drive_strength,
2230         .card_event                     = sdhci_card_event,
2231         .card_busy      = sdhci_card_busy,
2232 };
2233
2234 /*****************************************************************************\
2235  *                                                                           *
2236  * Tasklets                                                                  *
2237  *                                                                           *
2238 \*****************************************************************************/
2239
2240 static void sdhci_tasklet_finish(unsigned long param)
2241 {
2242         struct sdhci_host *host;
2243         unsigned long flags;
2244         struct mmc_request *mrq;
2245
2246         host = (struct sdhci_host*)param;
2247
2248         spin_lock_irqsave(&host->lock, flags);
2249
2250         /*
2251          * If this tasklet gets rescheduled while running, it will
2252          * be run again afterwards but without any active request.
2253          */
2254         if (!host->mrq) {
2255                 spin_unlock_irqrestore(&host->lock, flags);
2256                 return;
2257         }
2258
2259         del_timer(&host->timer);
2260
2261         mrq = host->mrq;
2262
2263         /*
2264          * The controller needs a reset of internal state machines
2265          * upon error conditions.
2266          */
2267         if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2268             ((mrq->cmd && mrq->cmd->error) ||
2269              (mrq->sbc && mrq->sbc->error) ||
2270              (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2271                             (mrq->data->stop && mrq->data->stop->error))) ||
2272              (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2273
2274                 /* Some controllers need this kick or reset won't work here */
2275                 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2276                         /* This is to force an update */
2277                         host->ops->set_clock(host, host->clock);
2278
2279                 /* Spec says we should do both at the same time, but Ricoh
2280                    controllers do not like that. */
2281                 sdhci_do_reset(host, SDHCI_RESET_CMD);
2282                 sdhci_do_reset(host, SDHCI_RESET_DATA);
2283         }
2284
2285         host->mrq = NULL;
2286         host->cmd = NULL;
2287         host->data = NULL;
2288
2289 #ifndef SDHCI_USE_LEDS_CLASS
2290         sdhci_deactivate_led(host);
2291 #endif
2292
2293         mmiowb();
2294         spin_unlock_irqrestore(&host->lock, flags);
2295
2296         mmc_request_done(host->mmc, mrq);
2297         sdhci_runtime_pm_put(host);
2298 }
2299
2300 static void sdhci_timeout_timer(unsigned long data)
2301 {
2302         struct sdhci_host *host;
2303         unsigned long flags;
2304
2305         host = (struct sdhci_host*)data;
2306
2307         spin_lock_irqsave(&host->lock, flags);
2308
2309         if (host->mrq) {
2310                 pr_err("%s: Timeout waiting for hardware "
2311                         "interrupt.\n", mmc_hostname(host->mmc));
2312                 sdhci_dumpregs(host);
2313
2314                 if (host->data) {
2315                         host->data->error = -ETIMEDOUT;
2316                         sdhci_finish_data(host);
2317                 } else {
2318                         if (host->cmd)
2319                                 host->cmd->error = -ETIMEDOUT;
2320                         else
2321                                 host->mrq->cmd->error = -ETIMEDOUT;
2322
2323                         tasklet_schedule(&host->finish_tasklet);
2324                 }
2325         }
2326
2327         mmiowb();
2328         spin_unlock_irqrestore(&host->lock, flags);
2329 }
2330
2331 /*****************************************************************************\
2332  *                                                                           *
2333  * Interrupt handling                                                        *
2334  *                                                                           *
2335 \*****************************************************************************/
2336
2337 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
2338 {
2339         BUG_ON(intmask == 0);
2340
2341         if (!host->cmd) {
2342                 pr_err("%s: Got command interrupt 0x%08x even "
2343                         "though no command operation was in progress.\n",
2344                         mmc_hostname(host->mmc), (unsigned)intmask);
2345                 sdhci_dumpregs(host);
2346                 return;
2347         }
2348
2349         if (intmask & SDHCI_INT_TIMEOUT)
2350                 host->cmd->error = -ETIMEDOUT;
2351         else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2352                         SDHCI_INT_INDEX))
2353                 host->cmd->error = -EILSEQ;
2354
2355         if (host->cmd->error) {
2356                 tasklet_schedule(&host->finish_tasklet);
2357                 return;
2358         }
2359
2360         /*
2361          * The host can send and interrupt when the busy state has
2362          * ended, allowing us to wait without wasting CPU cycles.
2363          * Unfortunately this is overloaded on the "data complete"
2364          * interrupt, so we need to take some care when handling
2365          * it.
2366          *
2367          * Note: The 1.0 specification is a bit ambiguous about this
2368          *       feature so there might be some problems with older
2369          *       controllers.
2370          */
2371         if (host->cmd->flags & MMC_RSP_BUSY) {
2372                 if (host->cmd->data)
2373                         DBG("Cannot wait for busy signal when also "
2374                                 "doing a data transfer");
2375                 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2376                                 && !host->busy_handle) {
2377                         /* Mark that command complete before busy is ended */
2378                         host->busy_handle = 1;
2379                         return;
2380                 }
2381
2382                 /* The controller does not support the end-of-busy IRQ,
2383                  * fall through and take the SDHCI_INT_RESPONSE */
2384         } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2385                    host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2386                 *mask &= ~SDHCI_INT_DATA_END;
2387         }
2388
2389         if (intmask & SDHCI_INT_RESPONSE)
2390                 sdhci_finish_command(host);
2391 }
2392
2393 #ifdef CONFIG_MMC_DEBUG
2394 static void sdhci_adma_show_error(struct sdhci_host *host)
2395 {
2396         const char *name = mmc_hostname(host->mmc);
2397         void *desc = host->adma_table;
2398
2399         sdhci_dumpregs(host);
2400
2401         while (true) {
2402                 struct sdhci_adma2_64_desc *dma_desc = desc;
2403
2404                 if (host->flags & SDHCI_USE_64_BIT_DMA)
2405                         DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2406                             name, desc, le32_to_cpu(dma_desc->addr_hi),
2407                             le32_to_cpu(dma_desc->addr_lo),
2408                             le16_to_cpu(dma_desc->len),
2409                             le16_to_cpu(dma_desc->cmd));
2410                 else
2411                         DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2412                             name, desc, le32_to_cpu(dma_desc->addr_lo),
2413                             le16_to_cpu(dma_desc->len),
2414                             le16_to_cpu(dma_desc->cmd));
2415
2416                 desc += host->desc_sz;
2417
2418                 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
2419                         break;
2420         }
2421 }
2422 #else
2423 static void sdhci_adma_show_error(struct sdhci_host *host) { }
2424 #endif
2425
2426 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2427 {
2428         u32 command;
2429         BUG_ON(intmask == 0);
2430
2431         /* CMD19 generates _only_ Buffer Read Ready interrupt */
2432         if (intmask & SDHCI_INT_DATA_AVAIL) {
2433                 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2434                 if (command == MMC_SEND_TUNING_BLOCK ||
2435                     command == MMC_SEND_TUNING_BLOCK_HS200) {
2436                         host->tuning_done = 1;
2437                         wake_up(&host->buf_ready_int);
2438                         return;
2439                 }
2440         }
2441
2442         if (!host->data) {
2443                 /*
2444                  * The "data complete" interrupt is also used to
2445                  * indicate that a busy state has ended. See comment
2446                  * above in sdhci_cmd_irq().
2447                  */
2448                 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2449                         if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2450                                 host->cmd->error = -ETIMEDOUT;
2451                                 tasklet_schedule(&host->finish_tasklet);
2452                                 return;
2453                         }
2454                         if (intmask & SDHCI_INT_DATA_END) {
2455                                 /*
2456                                  * Some cards handle busy-end interrupt
2457                                  * before the command completed, so make
2458                                  * sure we do things in the proper order.
2459                                  */
2460                                 if (host->busy_handle)
2461                                         sdhci_finish_command(host);
2462                                 else
2463                                         host->busy_handle = 1;
2464                                 return;
2465                         }
2466                 }
2467
2468                 pr_err("%s: Got data interrupt 0x%08x even "
2469                         "though no data operation was in progress.\n",
2470                         mmc_hostname(host->mmc), (unsigned)intmask);
2471                 sdhci_dumpregs(host);
2472
2473                 return;
2474         }
2475
2476         if (intmask & SDHCI_INT_DATA_TIMEOUT)
2477                 host->data->error = -ETIMEDOUT;
2478         else if (intmask & SDHCI_INT_DATA_END_BIT)
2479                 host->data->error = -EILSEQ;
2480         else if ((intmask & SDHCI_INT_DATA_CRC) &&
2481                 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2482                         != MMC_BUS_TEST_R)
2483                 host->data->error = -EILSEQ;
2484         else if (intmask & SDHCI_INT_ADMA_ERROR) {
2485                 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2486                 sdhci_adma_show_error(host);
2487                 host->data->error = -EIO;
2488                 if (host->ops->adma_workaround)
2489                         host->ops->adma_workaround(host, intmask);
2490         }
2491
2492         if (host->data->error)
2493                 sdhci_finish_data(host);
2494         else {
2495                 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2496                         sdhci_transfer_pio(host);
2497
2498                 /*
2499                  * We currently don't do anything fancy with DMA
2500                  * boundaries, but as we can't disable the feature
2501                  * we need to at least restart the transfer.
2502                  *
2503                  * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2504                  * should return a valid address to continue from, but as
2505                  * some controllers are faulty, don't trust them.
2506                  */
2507                 if (intmask & SDHCI_INT_DMA_END) {
2508                         u32 dmastart, dmanow;
2509                         dmastart = sg_dma_address(host->data->sg);
2510                         dmanow = dmastart + host->data->bytes_xfered;
2511                         /*
2512                          * Force update to the next DMA block boundary.
2513                          */
2514                         dmanow = (dmanow &
2515                                 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2516                                 SDHCI_DEFAULT_BOUNDARY_SIZE;
2517                         host->data->bytes_xfered = dmanow - dmastart;
2518                         DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2519                                 " next 0x%08x\n",
2520                                 mmc_hostname(host->mmc), dmastart,
2521                                 host->data->bytes_xfered, dmanow);
2522                         sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2523                 }
2524
2525                 if (intmask & SDHCI_INT_DATA_END) {
2526                         if (host->cmd) {
2527                                 /*
2528                                  * Data managed to finish before the
2529                                  * command completed. Make sure we do
2530                                  * things in the proper order.
2531                                  */
2532                                 host->data_early = 1;
2533                         } else {
2534                                 sdhci_finish_data(host);
2535                         }
2536                 }
2537         }
2538 }
2539
2540 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2541 {
2542         irqreturn_t result = IRQ_NONE;
2543         struct sdhci_host *host = dev_id;
2544         u32 intmask, mask, unexpected = 0;
2545         int max_loops = 16;
2546
2547         spin_lock(&host->lock);
2548
2549         if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2550                 spin_unlock(&host->lock);
2551                 return IRQ_NONE;
2552         }
2553
2554         intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2555         if (!intmask || intmask == 0xffffffff) {
2556                 result = IRQ_NONE;
2557                 goto out;
2558         }
2559
2560         do {
2561                 /* Clear selected interrupts. */
2562                 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2563                                   SDHCI_INT_BUS_POWER);
2564                 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2565
2566                 DBG("*** %s got interrupt: 0x%08x\n",
2567                         mmc_hostname(host->mmc), intmask);
2568
2569                 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2570                         u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2571                                       SDHCI_CARD_PRESENT;
2572
2573                         /*
2574                          * There is a observation on i.mx esdhc.  INSERT
2575                          * bit will be immediately set again when it gets
2576                          * cleared, if a card is inserted.  We have to mask
2577                          * the irq to prevent interrupt storm which will
2578                          * freeze the system.  And the REMOVE gets the
2579                          * same situation.
2580                          *
2581                          * More testing are needed here to ensure it works
2582                          * for other platforms though.
2583                          */
2584                         host->ier &= ~(SDHCI_INT_CARD_INSERT |
2585                                        SDHCI_INT_CARD_REMOVE);
2586                         host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2587                                                SDHCI_INT_CARD_INSERT;
2588                         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2589                         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2590
2591                         sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2592                                      SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2593
2594                         host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2595                                                        SDHCI_INT_CARD_REMOVE);
2596                         result = IRQ_WAKE_THREAD;
2597                 }
2598
2599                 if (intmask & SDHCI_INT_CMD_MASK)
2600                         sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2601                                       &intmask);
2602
2603                 if (intmask & SDHCI_INT_DATA_MASK)
2604                         sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2605
2606                 if (intmask & SDHCI_INT_BUS_POWER)
2607                         pr_err("%s: Card is consuming too much power!\n",
2608                                 mmc_hostname(host->mmc));
2609
2610                 if (intmask & SDHCI_INT_CARD_INT) {
2611                         sdhci_enable_sdio_irq_nolock(host, false);
2612                         host->thread_isr |= SDHCI_INT_CARD_INT;
2613                         result = IRQ_WAKE_THREAD;
2614                 }
2615
2616                 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2617                              SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2618                              SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2619                              SDHCI_INT_CARD_INT);
2620
2621                 if (intmask) {
2622                         unexpected |= intmask;
2623                         sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2624                 }
2625
2626                 if (result == IRQ_NONE)
2627                         result = IRQ_HANDLED;
2628
2629                 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2630         } while (intmask && --max_loops);
2631 out:
2632         spin_unlock(&host->lock);
2633
2634         if (unexpected) {
2635                 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2636                            mmc_hostname(host->mmc), unexpected);
2637                 sdhci_dumpregs(host);
2638         }
2639
2640         return result;
2641 }
2642
2643 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2644 {
2645         struct sdhci_host *host = dev_id;
2646         unsigned long flags;
2647         u32 isr;
2648
2649         spin_lock_irqsave(&host->lock, flags);
2650         isr = host->thread_isr;
2651         host->thread_isr = 0;
2652         spin_unlock_irqrestore(&host->lock, flags);
2653
2654         if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2655                 sdhci_card_event(host->mmc);
2656                 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2657         }
2658
2659         if (isr & SDHCI_INT_CARD_INT) {
2660                 sdio_run_irqs(host->mmc);
2661
2662                 spin_lock_irqsave(&host->lock, flags);
2663                 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2664                         sdhci_enable_sdio_irq_nolock(host, true);
2665                 spin_unlock_irqrestore(&host->lock, flags);
2666         }
2667
2668         return isr ? IRQ_HANDLED : IRQ_NONE;
2669 }
2670
2671 /*****************************************************************************\
2672  *                                                                           *
2673  * Suspend/resume                                                            *
2674  *                                                                           *
2675 \*****************************************************************************/
2676
2677 #ifdef CONFIG_PM
2678 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2679 {
2680         u8 val;
2681         u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2682                         | SDHCI_WAKE_ON_INT;
2683
2684         val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2685         val |= mask ;
2686         /* Avoid fake wake up */
2687         if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2688                 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2689         sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2690 }
2691 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2692
2693 static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2694 {
2695         u8 val;
2696         u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2697                         | SDHCI_WAKE_ON_INT;
2698
2699         val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2700         val &= ~mask;
2701         sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2702 }
2703
2704 int sdhci_suspend_host(struct sdhci_host *host)
2705 {
2706         sdhci_disable_card_detection(host);
2707
2708         mmc_retune_timer_stop(host->mmc);
2709         mmc_retune_needed(host->mmc);
2710
2711         if (!device_may_wakeup(mmc_dev(host->mmc))) {
2712                 host->ier = 0;
2713                 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2714                 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2715                 free_irq(host->irq, host);
2716         } else {
2717                 sdhci_enable_irq_wakeups(host);
2718                 enable_irq_wake(host->irq);
2719         }
2720         return 0;
2721 }
2722
2723 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2724
2725 int sdhci_resume_host(struct sdhci_host *host)
2726 {
2727         int ret = 0;
2728
2729         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2730                 if (host->ops->enable_dma)
2731                         host->ops->enable_dma(host);
2732         }
2733
2734         if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2735             (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2736                 /* Card keeps power but host controller does not */
2737                 sdhci_init(host, 0);
2738                 host->pwr = 0;
2739                 host->clock = 0;
2740                 sdhci_do_set_ios(host, &host->mmc->ios);
2741         } else {
2742                 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2743                 mmiowb();
2744         }
2745
2746         if (!device_may_wakeup(mmc_dev(host->mmc))) {
2747                 ret = request_threaded_irq(host->irq, sdhci_irq,
2748                                            sdhci_thread_irq, IRQF_SHARED,
2749                                            mmc_hostname(host->mmc), host);
2750                 if (ret)
2751                         return ret;
2752         } else {
2753                 sdhci_disable_irq_wakeups(host);
2754                 disable_irq_wake(host->irq);
2755         }
2756
2757         sdhci_enable_card_detection(host);
2758
2759         return ret;
2760 }
2761
2762 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2763
2764 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2765 {
2766         return pm_runtime_get_sync(host->mmc->parent);
2767 }
2768
2769 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2770 {
2771         pm_runtime_mark_last_busy(host->mmc->parent);
2772         return pm_runtime_put_autosuspend(host->mmc->parent);
2773 }
2774
2775 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2776 {
2777         if (host->bus_on)
2778                 return;
2779         host->bus_on = true;
2780         pm_runtime_get_noresume(host->mmc->parent);
2781 }
2782
2783 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2784 {
2785         if (!host->bus_on)
2786                 return;
2787         host->bus_on = false;
2788         pm_runtime_put_noidle(host->mmc->parent);
2789 }
2790
2791 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2792 {
2793         unsigned long flags;
2794
2795         mmc_retune_timer_stop(host->mmc);
2796         mmc_retune_needed(host->mmc);
2797
2798         spin_lock_irqsave(&host->lock, flags);
2799         host->ier &= SDHCI_INT_CARD_INT;
2800         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2801         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2802         spin_unlock_irqrestore(&host->lock, flags);
2803
2804         synchronize_hardirq(host->irq);
2805
2806         spin_lock_irqsave(&host->lock, flags);
2807         host->runtime_suspended = true;
2808         spin_unlock_irqrestore(&host->lock, flags);
2809
2810         return 0;
2811 }
2812 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2813
2814 int sdhci_runtime_resume_host(struct sdhci_host *host)
2815 {
2816         unsigned long flags;
2817         int host_flags = host->flags;
2818
2819         if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2820                 if (host->ops->enable_dma)
2821                         host->ops->enable_dma(host);
2822         }
2823
2824         sdhci_init(host, 0);
2825
2826         /* Force clock and power re-program */
2827         host->pwr = 0;
2828         host->clock = 0;
2829         sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2830         sdhci_do_set_ios(host, &host->mmc->ios);
2831
2832         if ((host_flags & SDHCI_PV_ENABLED) &&
2833                 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2834                 spin_lock_irqsave(&host->lock, flags);
2835                 sdhci_enable_preset_value(host, true);
2836                 spin_unlock_irqrestore(&host->lock, flags);
2837         }
2838
2839         spin_lock_irqsave(&host->lock, flags);
2840
2841         host->runtime_suspended = false;
2842
2843         /* Enable SDIO IRQ */
2844         if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2845                 sdhci_enable_sdio_irq_nolock(host, true);
2846
2847         /* Enable Card Detection */
2848         sdhci_enable_card_detection(host);
2849
2850         spin_unlock_irqrestore(&host->lock, flags);
2851
2852         return 0;
2853 }
2854 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2855
2856 #endif /* CONFIG_PM */
2857
2858 /*****************************************************************************\
2859  *                                                                           *
2860  * Device allocation/registration                                            *
2861  *                                                                           *
2862 \*****************************************************************************/
2863
2864 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2865         size_t priv_size)
2866 {
2867         struct mmc_host *mmc;
2868         struct sdhci_host *host;
2869
2870         WARN_ON(dev == NULL);
2871
2872         mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2873         if (!mmc)
2874                 return ERR_PTR(-ENOMEM);
2875
2876         host = mmc_priv(mmc);
2877         host->mmc = mmc;
2878         host->mmc_host_ops = sdhci_ops;
2879         mmc->ops = &host->mmc_host_ops;
2880
2881         return host;
2882 }
2883
2884 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2885
2886 int sdhci_add_host(struct sdhci_host *host)
2887 {
2888         struct mmc_host *mmc;
2889         u32 caps[2] = {0, 0};
2890         u32 max_current_caps;
2891         unsigned int ocr_avail;
2892         unsigned int override_timeout_clk;
2893         u32 max_clk;
2894         int ret;
2895
2896         WARN_ON(host == NULL);
2897         if (host == NULL)
2898                 return -EINVAL;
2899
2900         mmc = host->mmc;
2901
2902         if (debug_quirks)
2903                 host->quirks = debug_quirks;
2904         if (debug_quirks2)
2905                 host->quirks2 = debug_quirks2;
2906
2907         override_timeout_clk = host->timeout_clk;
2908
2909         sdhci_do_reset(host, SDHCI_RESET_ALL);
2910
2911         host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2912         host->version = (host->version & SDHCI_SPEC_VER_MASK)
2913                                 >> SDHCI_SPEC_VER_SHIFT;
2914         if (host->version > SDHCI_SPEC_300) {
2915                 pr_err("%s: Unknown controller version (%d). "
2916                         "You may experience problems.\n", mmc_hostname(mmc),
2917                         host->version);
2918         }
2919
2920         caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2921                 sdhci_readl(host, SDHCI_CAPABILITIES);
2922
2923         if (host->version >= SDHCI_SPEC_300)
2924                 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2925                         host->caps1 :
2926                         sdhci_readl(host, SDHCI_CAPABILITIES_1);
2927
2928         if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2929                 host->flags |= SDHCI_USE_SDMA;
2930         else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2931                 DBG("Controller doesn't have SDMA capability\n");
2932         else
2933                 host->flags |= SDHCI_USE_SDMA;
2934
2935         if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2936                 (host->flags & SDHCI_USE_SDMA)) {
2937                 DBG("Disabling DMA as it is marked broken\n");
2938                 host->flags &= ~SDHCI_USE_SDMA;
2939         }
2940
2941         if ((host->version >= SDHCI_SPEC_200) &&
2942                 (caps[0] & SDHCI_CAN_DO_ADMA2))
2943                 host->flags |= SDHCI_USE_ADMA;
2944
2945         if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2946                 (host->flags & SDHCI_USE_ADMA)) {
2947                 DBG("Disabling ADMA as it is marked broken\n");
2948                 host->flags &= ~SDHCI_USE_ADMA;
2949         }
2950
2951         /*
2952          * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2953          * and *must* do 64-bit DMA.  A driver has the opportunity to change
2954          * that during the first call to ->enable_dma().  Similarly
2955          * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2956          * implement.
2957          */
2958         if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
2959                 host->flags |= SDHCI_USE_64_BIT_DMA;
2960
2961         if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2962                 if (host->ops->enable_dma) {
2963                         if (host->ops->enable_dma(host)) {
2964                                 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2965                                         mmc_hostname(mmc));
2966                                 host->flags &=
2967                                         ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2968                         }
2969                 }
2970         }
2971
2972         /* SDMA does not support 64-bit DMA */
2973         if (host->flags & SDHCI_USE_64_BIT_DMA)
2974                 host->flags &= ~SDHCI_USE_SDMA;
2975
2976         if (host->flags & SDHCI_USE_ADMA) {
2977                 /*
2978                  * The DMA descriptor table size is calculated as the maximum
2979                  * number of segments times 2, to allow for an alignment
2980                  * descriptor for each segment, plus 1 for a nop end descriptor,
2981                  * all multipled by the descriptor size.
2982                  */
2983                 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2984                         host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2985                                               SDHCI_ADMA2_64_DESC_SZ;
2986                         host->align_buffer_sz = SDHCI_MAX_SEGS *
2987                                                 SDHCI_ADMA2_64_ALIGN;
2988                         host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
2989                         host->align_sz = SDHCI_ADMA2_64_ALIGN;
2990                         host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
2991                 } else {
2992                         host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2993                                               SDHCI_ADMA2_32_DESC_SZ;
2994                         host->align_buffer_sz = SDHCI_MAX_SEGS *
2995                                                 SDHCI_ADMA2_32_ALIGN;
2996                         host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2997                         host->align_sz = SDHCI_ADMA2_32_ALIGN;
2998                         host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
2999                 }
3000                 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
3001                                                       host->adma_table_sz,
3002                                                       &host->adma_addr,
3003                                                       GFP_KERNEL);
3004                 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
3005                 if (!host->adma_table || !host->align_buffer) {
3006                         if (host->adma_table)
3007                                 dma_free_coherent(mmc_dev(mmc),
3008                                                   host->adma_table_sz,
3009                                                   host->adma_table,
3010                                                   host->adma_addr);
3011                         kfree(host->align_buffer);
3012                         pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
3013                                 mmc_hostname(mmc));
3014                         host->flags &= ~SDHCI_USE_ADMA;
3015                         host->adma_table = NULL;
3016                         host->align_buffer = NULL;
3017                 } else if (host->adma_addr & host->align_mask) {
3018                         pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3019                                 mmc_hostname(mmc));
3020                         host->flags &= ~SDHCI_USE_ADMA;
3021                         dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3022                                           host->adma_table, host->adma_addr);
3023                         kfree(host->align_buffer);
3024                         host->adma_table = NULL;
3025                         host->align_buffer = NULL;
3026                 }
3027         }
3028
3029         /*
3030          * If we use DMA, then it's up to the caller to set the DMA
3031          * mask, but PIO does not need the hw shim so we set a new
3032          * mask here in that case.
3033          */
3034         if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
3035                 host->dma_mask = DMA_BIT_MASK(64);
3036                 mmc_dev(mmc)->dma_mask = &host->dma_mask;
3037         }
3038
3039         if (host->version >= SDHCI_SPEC_300)
3040                 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
3041                         >> SDHCI_CLOCK_BASE_SHIFT;
3042         else
3043                 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
3044                         >> SDHCI_CLOCK_BASE_SHIFT;
3045
3046         host->max_clk *= 1000000;
3047         if (host->max_clk == 0 || host->quirks &
3048                         SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
3049                 if (!host->ops->get_max_clock) {
3050                         pr_err("%s: Hardware doesn't specify base clock "
3051                                "frequency.\n", mmc_hostname(mmc));
3052                         return -ENODEV;
3053                 }
3054                 host->max_clk = host->ops->get_max_clock(host);
3055         }
3056
3057         /*
3058          * In case of Host Controller v3.00, find out whether clock
3059          * multiplier is supported.
3060          */
3061         host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3062                         SDHCI_CLOCK_MUL_SHIFT;
3063
3064         /*
3065          * In case the value in Clock Multiplier is 0, then programmable
3066          * clock mode is not supported, otherwise the actual clock
3067          * multiplier is one more than the value of Clock Multiplier
3068          * in the Capabilities Register.
3069          */
3070         if (host->clk_mul)
3071                 host->clk_mul += 1;
3072
3073         /*
3074          * Set host parameters.
3075          */
3076         max_clk = host->max_clk;
3077
3078         if (host->ops->get_min_clock)
3079                 mmc->f_min = host->ops->get_min_clock(host);
3080         else if (host->version >= SDHCI_SPEC_300) {
3081                 if (host->clk_mul) {
3082                         mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3083                         max_clk = host->max_clk * host->clk_mul;
3084                 } else
3085                         mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3086         } else
3087                 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
3088
3089         if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3090                 mmc->f_max = max_clk;
3091
3092         if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3093                 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3094                                         SDHCI_TIMEOUT_CLK_SHIFT;
3095                 if (host->timeout_clk == 0) {
3096                         if (host->ops->get_timeout_clock) {
3097                                 host->timeout_clk =
3098                                         host->ops->get_timeout_clock(host);
3099                         } else {
3100                                 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3101                                         mmc_hostname(mmc));
3102                                 return -ENODEV;
3103                         }
3104                 }
3105
3106                 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3107                         host->timeout_clk *= 1000;
3108
3109                 if (override_timeout_clk)
3110                         host->timeout_clk = override_timeout_clk;
3111
3112                 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
3113                         host->ops->get_max_timeout_count(host) : 1 << 27;
3114                 mmc->max_busy_timeout /= host->timeout_clk;
3115         }
3116
3117         mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3118         mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
3119
3120         if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3121                 host->flags |= SDHCI_AUTO_CMD12;
3122
3123         /* Auto-CMD23 stuff only works in ADMA or PIO. */
3124         if ((host->version >= SDHCI_SPEC_300) &&
3125             ((host->flags & SDHCI_USE_ADMA) ||
3126              !(host->flags & SDHCI_USE_SDMA)) &&
3127              !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
3128                 host->flags |= SDHCI_AUTO_CMD23;
3129                 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3130         } else {
3131                 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3132         }
3133
3134         /*
3135          * A controller may support 8-bit width, but the board itself
3136          * might not have the pins brought out.  Boards that support
3137          * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3138          * their platform code before calling sdhci_add_host(), and we
3139          * won't assume 8-bit width for hosts without that CAP.
3140          */
3141         if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
3142                 mmc->caps |= MMC_CAP_4_BIT_DATA;
3143
3144         if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3145                 mmc->caps &= ~MMC_CAP_CMD23;
3146
3147         if (caps[0] & SDHCI_CAN_DO_HISPD)
3148                 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3149
3150         if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3151             !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3152             IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
3153                 mmc->caps |= MMC_CAP_NEEDS_POLL;
3154
3155         /* If there are external regulators, get them */
3156         if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3157                 return -EPROBE_DEFER;
3158
3159         /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
3160         if (!IS_ERR(mmc->supply.vqmmc)) {
3161                 ret = regulator_enable(mmc->supply.vqmmc);
3162                 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3163                                                     1950000))
3164                         caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3165                                         SDHCI_SUPPORT_SDR50 |
3166                                         SDHCI_SUPPORT_DDR50);
3167                 if (ret) {
3168                         pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3169                                 mmc_hostname(mmc), ret);
3170                         mmc->supply.vqmmc = ERR_PTR(-EINVAL);
3171                 }
3172         }
3173
3174         if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3175                 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3176                        SDHCI_SUPPORT_DDR50);
3177
3178         /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3179         if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3180                        SDHCI_SUPPORT_DDR50))
3181                 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3182
3183         /* SDR104 supports also implies SDR50 support */
3184         if (caps[1] & SDHCI_SUPPORT_SDR104) {
3185                 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3186                 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3187                  * field can be promoted to support HS200.
3188                  */
3189                 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3190                         mmc->caps2 |= MMC_CAP2_HS200;
3191         } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3192                 mmc->caps |= MMC_CAP_UHS_SDR50;
3193
3194         if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3195             (caps[1] & SDHCI_SUPPORT_HS400))
3196                 mmc->caps2 |= MMC_CAP2_HS400;
3197
3198         if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3199             (IS_ERR(mmc->supply.vqmmc) ||
3200              !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3201                                              1300000)))
3202                 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3203
3204         if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3205                 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3206                 mmc->caps |= MMC_CAP_UHS_DDR50;
3207
3208         /* Does the host need tuning for SDR50? */
3209         if (caps[1] & SDHCI_USE_SDR50_TUNING)
3210                 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3211
3212         /* Does the host need tuning for SDR104 / HS200? */
3213         if (mmc->caps2 & MMC_CAP2_HS200)
3214                 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3215
3216         /* Driver Type(s) (A, C, D) supported by the host */
3217         if (caps[1] & SDHCI_DRIVER_TYPE_A)
3218                 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3219         if (caps[1] & SDHCI_DRIVER_TYPE_C)
3220                 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3221         if (caps[1] & SDHCI_DRIVER_TYPE_D)
3222                 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3223
3224         /* Initial value for re-tuning timer count */
3225         host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3226                               SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3227
3228         /*
3229          * In case Re-tuning Timer is not disabled, the actual value of
3230          * re-tuning timer will be 2 ^ (n - 1).
3231          */
3232         if (host->tuning_count)
3233                 host->tuning_count = 1 << (host->tuning_count - 1);
3234
3235         /* Re-tuning mode supported by the Host Controller */
3236         host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3237                              SDHCI_RETUNING_MODE_SHIFT;
3238
3239         ocr_avail = 0;
3240
3241         /*
3242          * According to SD Host Controller spec v3.00, if the Host System
3243          * can afford more than 150mA, Host Driver should set XPC to 1. Also
3244          * the value is meaningful only if Voltage Support in the Capabilities
3245          * register is set. The actual current value is 4 times the register
3246          * value.
3247          */
3248         max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3249         if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3250                 int curr = regulator_get_current_limit(mmc->supply.vmmc);
3251                 if (curr > 0) {
3252
3253                         /* convert to SDHCI_MAX_CURRENT format */
3254                         curr = curr/1000;  /* convert to mA */
3255                         curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3256
3257                         curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3258                         max_current_caps =
3259                                 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3260                                 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3261                                 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3262                 }
3263         }
3264
3265         if (caps[0] & SDHCI_CAN_VDD_330) {
3266                 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3267
3268                 mmc->max_current_330 = ((max_current_caps &
3269                                    SDHCI_MAX_CURRENT_330_MASK) >>
3270                                    SDHCI_MAX_CURRENT_330_SHIFT) *
3271                                    SDHCI_MAX_CURRENT_MULTIPLIER;
3272         }
3273         if (caps[0] & SDHCI_CAN_VDD_300) {
3274                 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3275
3276                 mmc->max_current_300 = ((max_current_caps &
3277                                    SDHCI_MAX_CURRENT_300_MASK) >>
3278                                    SDHCI_MAX_CURRENT_300_SHIFT) *
3279                                    SDHCI_MAX_CURRENT_MULTIPLIER;
3280         }
3281         if (caps[0] & SDHCI_CAN_VDD_180) {
3282                 ocr_avail |= MMC_VDD_165_195;
3283
3284                 mmc->max_current_180 = ((max_current_caps &
3285                                    SDHCI_MAX_CURRENT_180_MASK) >>
3286                                    SDHCI_MAX_CURRENT_180_SHIFT) *
3287                                    SDHCI_MAX_CURRENT_MULTIPLIER;
3288         }
3289
3290         /* If OCR set by host, use it instead. */
3291         if (host->ocr_mask)
3292                 ocr_avail = host->ocr_mask;
3293
3294         /* If OCR set by external regulators, give it highest prio. */
3295         if (mmc->ocr_avail)
3296                 ocr_avail = mmc->ocr_avail;
3297
3298         mmc->ocr_avail = ocr_avail;
3299         mmc->ocr_avail_sdio = ocr_avail;
3300         if (host->ocr_avail_sdio)
3301                 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3302         mmc->ocr_avail_sd = ocr_avail;
3303         if (host->ocr_avail_sd)
3304                 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3305         else /* normal SD controllers don't support 1.8V */
3306                 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3307         mmc->ocr_avail_mmc = ocr_avail;
3308         if (host->ocr_avail_mmc)
3309                 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3310
3311         if (mmc->ocr_avail == 0) {
3312                 pr_err("%s: Hardware doesn't report any "
3313                         "support voltages.\n", mmc_hostname(mmc));
3314                 return -ENODEV;
3315         }
3316
3317         spin_lock_init(&host->lock);
3318
3319         /*
3320          * Maximum number of segments. Depends on if the hardware
3321          * can do scatter/gather or not.
3322          */
3323         if (host->flags & SDHCI_USE_ADMA)
3324                 mmc->max_segs = SDHCI_MAX_SEGS;
3325         else if (host->flags & SDHCI_USE_SDMA)
3326                 mmc->max_segs = 1;
3327         else /* PIO */
3328                 mmc->max_segs = SDHCI_MAX_SEGS;
3329
3330         /*
3331          * Maximum number of sectors in one transfer. Limited by SDMA boundary
3332          * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3333          * is less anyway.
3334          */
3335         mmc->max_req_size = 524288;
3336
3337         /*
3338          * Maximum segment size. Could be one segment with the maximum number
3339          * of bytes. When doing hardware scatter/gather, each entry cannot
3340          * be larger than 64 KiB though.
3341          */
3342         if (host->flags & SDHCI_USE_ADMA) {
3343                 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3344                         mmc->max_seg_size = 65535;
3345                 else
3346                         mmc->max_seg_size = 65536;
3347         } else {
3348                 mmc->max_seg_size = mmc->max_req_size;
3349         }
3350
3351         /*
3352          * Maximum block size. This varies from controller to controller and
3353          * is specified in the capabilities register.
3354          */
3355         if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3356                 mmc->max_blk_size = 2;
3357         } else {
3358                 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3359                                 SDHCI_MAX_BLOCK_SHIFT;
3360                 if (mmc->max_blk_size >= 3) {
3361                         pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3362                                 mmc_hostname(mmc));
3363                         mmc->max_blk_size = 0;
3364                 }
3365         }
3366
3367         mmc->max_blk_size = 512 << mmc->max_blk_size;
3368
3369         /*
3370          * Maximum block count.
3371          */
3372         mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3373
3374         /*
3375          * Init tasklets.
3376          */
3377         tasklet_init(&host->finish_tasklet,
3378                 sdhci_tasklet_finish, (unsigned long)host);
3379
3380         setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3381
3382         init_waitqueue_head(&host->buf_ready_int);
3383
3384         sdhci_init(host, 0);
3385
3386         ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3387                                    IRQF_SHARED, mmc_hostname(mmc), host);
3388         if (ret) {
3389                 pr_err("%s: Failed to request IRQ %d: %d\n",
3390                        mmc_hostname(mmc), host->irq, ret);
3391                 goto untasklet;
3392         }
3393
3394 #ifdef CONFIG_MMC_DEBUG
3395         sdhci_dumpregs(host);
3396 #endif
3397
3398 #ifdef SDHCI_USE_LEDS_CLASS
3399         snprintf(host->led_name, sizeof(host->led_name),
3400                 "%s::", mmc_hostname(mmc));
3401         host->led.name = host->led_name;
3402         host->led.brightness = LED_OFF;
3403         host->led.default_trigger = mmc_hostname(mmc);
3404         host->led.brightness_set = sdhci_led_control;
3405
3406         ret = led_classdev_register(mmc_dev(mmc), &host->led);
3407         if (ret) {
3408                 pr_err("%s: Failed to register LED device: %d\n",
3409                        mmc_hostname(mmc), ret);
3410                 goto reset;
3411         }
3412 #endif
3413
3414         mmiowb();
3415
3416         mmc_add_host(mmc);
3417
3418         pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3419                 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3420                 (host->flags & SDHCI_USE_ADMA) ?
3421                 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
3422                 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3423
3424         sdhci_enable_card_detection(host);
3425
3426         return 0;
3427
3428 #ifdef SDHCI_USE_LEDS_CLASS
3429 reset:
3430         sdhci_do_reset(host, SDHCI_RESET_ALL);
3431         sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3432         sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3433         free_irq(host->irq, host);
3434 #endif
3435 untasklet:
3436         tasklet_kill(&host->finish_tasklet);
3437
3438         return ret;
3439 }
3440
3441 EXPORT_SYMBOL_GPL(sdhci_add_host);
3442
3443 void sdhci_remove_host(struct sdhci_host *host, int dead)
3444 {
3445         struct mmc_host *mmc = host->mmc;
3446         unsigned long flags;
3447
3448         if (dead) {
3449                 spin_lock_irqsave(&host->lock, flags);
3450
3451                 host->flags |= SDHCI_DEVICE_DEAD;
3452
3453                 if (host->mrq) {
3454                         pr_err("%s: Controller removed during "
3455                                 " transfer!\n", mmc_hostname(mmc));
3456
3457                         host->mrq->cmd->error = -ENOMEDIUM;
3458                         tasklet_schedule(&host->finish_tasklet);
3459                 }
3460
3461                 spin_unlock_irqrestore(&host->lock, flags);
3462         }
3463
3464         sdhci_disable_card_detection(host);
3465
3466         mmc_remove_host(mmc);
3467
3468 #ifdef SDHCI_USE_LEDS_CLASS
3469         led_classdev_unregister(&host->led);
3470 #endif
3471
3472         if (!dead)
3473                 sdhci_do_reset(host, SDHCI_RESET_ALL);
3474
3475         sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3476         sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3477         free_irq(host->irq, host);
3478
3479         del_timer_sync(&host->timer);
3480
3481         tasklet_kill(&host->finish_tasklet);
3482
3483         if (!IS_ERR(mmc->supply.vqmmc))
3484                 regulator_disable(mmc->supply.vqmmc);
3485
3486         if (host->adma_table)
3487                 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3488                                   host->adma_table, host->adma_addr);
3489         kfree(host->align_buffer);
3490
3491         host->adma_table = NULL;
3492         host->align_buffer = NULL;
3493 }
3494
3495 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3496
3497 void sdhci_free_host(struct sdhci_host *host)
3498 {
3499         mmc_free_host(host->mmc);
3500 }
3501
3502 EXPORT_SYMBOL_GPL(sdhci_free_host);
3503
3504 /*****************************************************************************\
3505  *                                                                           *
3506  * Driver init/exit                                                          *
3507  *                                                                           *
3508 \*****************************************************************************/
3509
3510 static int __init sdhci_drv_init(void)
3511 {
3512         pr_info(DRIVER_NAME
3513                 ": Secure Digital Host Controller Interface driver\n");
3514         pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3515
3516         return 0;
3517 }
3518
3519 static void __exit sdhci_drv_exit(void)
3520 {
3521 }
3522
3523 module_init(sdhci_drv_init);
3524 module_exit(sdhci_drv_exit);
3525
3526 module_param(debug_quirks, uint, 0444);
3527 module_param(debug_quirks2, uint, 0444);
3528
3529 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3530 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3531 MODULE_LICENSE("GPL");
3532
3533 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3534 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");