]> Pileus Git - ~andy/linux/blob - drivers/mmc/host/sdhci-pci.c
mmc: dw_mmc: use dev_pm_ops for dw_mmc controllers
[~andy/linux] / drivers / mmc / host / sdhci-pci.c
1 /*  linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
2  *
3  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * Thanks to the following companies for their support:
11  *
12  *     - JMicron (hardware and technical support)
13  */
14
15 #include <linux/delay.h>
16 #include <linux/highmem.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/slab.h>
21 #include <linux/device.h>
22 #include <linux/mmc/host.h>
23 #include <linux/scatterlist.h>
24 #include <linux/io.h>
25 #include <linux/gpio.h>
26 #include <linux/sfi.h>
27 #include <linux/pm_runtime.h>
28
29 #include "sdhci.h"
30
31 /*
32  * PCI registers
33  */
34
35 #define PCI_SDHCI_IFPIO                 0x00
36 #define PCI_SDHCI_IFDMA                 0x01
37 #define PCI_SDHCI_IFVENDOR              0x02
38
39 #define PCI_SLOT_INFO                   0x40    /* 8 bits */
40 #define  PCI_SLOT_INFO_SLOTS(x)         ((x >> 4) & 7)
41 #define  PCI_SLOT_INFO_FIRST_BAR_MASK   0x07
42
43 #define MAX_SLOTS                       8
44
45 struct sdhci_pci_chip;
46 struct sdhci_pci_slot;
47
48 struct sdhci_pci_fixes {
49         unsigned int            quirks;
50         bool                    allow_runtime_pm;
51
52         int                     (*probe) (struct sdhci_pci_chip *);
53
54         int                     (*probe_slot) (struct sdhci_pci_slot *);
55         void                    (*remove_slot) (struct sdhci_pci_slot *, int);
56
57         int                     (*suspend) (struct sdhci_pci_chip *);
58         int                     (*resume) (struct sdhci_pci_chip *);
59 };
60
61 struct sdhci_pci_slot {
62         struct sdhci_pci_chip   *chip;
63         struct sdhci_host       *host;
64
65         int                     pci_bar;
66         int                     rst_n_gpio;
67         int                     cd_gpio;
68         int                     cd_irq;
69 };
70
71 struct sdhci_pci_chip {
72         struct pci_dev          *pdev;
73
74         unsigned int            quirks;
75         bool                    allow_runtime_pm;
76         const struct sdhci_pci_fixes *fixes;
77
78         int                     num_slots;      /* Slots on controller */
79         struct sdhci_pci_slot   *slots[MAX_SLOTS]; /* Pointers to host slots */
80 };
81
82
83 /*****************************************************************************\
84  *                                                                           *
85  * Hardware specific quirk handling                                          *
86  *                                                                           *
87 \*****************************************************************************/
88
89 static int ricoh_probe(struct sdhci_pci_chip *chip)
90 {
91         if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
92             chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
93                 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
94         return 0;
95 }
96
97 static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
98 {
99         slot->host->caps =
100                 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
101                         & SDHCI_TIMEOUT_CLK_MASK) |
102
103                 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
104                         & SDHCI_CLOCK_BASE_MASK) |
105
106                 SDHCI_TIMEOUT_CLK_UNIT |
107                 SDHCI_CAN_VDD_330 |
108                 SDHCI_CAN_DO_SDMA;
109         return 0;
110 }
111
112 static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
113 {
114         /* Apply a delay to allow controller to settle */
115         /* Otherwise it becomes confused if card state changed
116                 during suspend */
117         msleep(500);
118         return 0;
119 }
120
121 static const struct sdhci_pci_fixes sdhci_ricoh = {
122         .probe          = ricoh_probe,
123         .quirks         = SDHCI_QUIRK_32BIT_DMA_ADDR |
124                           SDHCI_QUIRK_FORCE_DMA |
125                           SDHCI_QUIRK_CLOCK_BEFORE_RESET,
126 };
127
128 static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
129         .probe_slot     = ricoh_mmc_probe_slot,
130         .resume         = ricoh_mmc_resume,
131         .quirks         = SDHCI_QUIRK_32BIT_DMA_ADDR |
132                           SDHCI_QUIRK_CLOCK_BEFORE_RESET |
133                           SDHCI_QUIRK_NO_CARD_NO_RESET |
134                           SDHCI_QUIRK_MISSING_CAPS
135 };
136
137 static const struct sdhci_pci_fixes sdhci_ene_712 = {
138         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
139                           SDHCI_QUIRK_BROKEN_DMA,
140 };
141
142 static const struct sdhci_pci_fixes sdhci_ene_714 = {
143         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
144                           SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
145                           SDHCI_QUIRK_BROKEN_DMA,
146 };
147
148 static const struct sdhci_pci_fixes sdhci_cafe = {
149         .quirks         = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
150                           SDHCI_QUIRK_NO_BUSY_IRQ |
151                           SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
152 };
153
154 static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
155 {
156         slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
157         return 0;
158 }
159
160 /*
161  * ADMA operation is disabled for Moorestown platform due to
162  * hardware bugs.
163  */
164 static int mrst_hc_probe(struct sdhci_pci_chip *chip)
165 {
166         /*
167          * slots number is fixed here for MRST as SDIO3/5 are never used and
168          * have hardware bugs.
169          */
170         chip->num_slots = 1;
171         return 0;
172 }
173
174 /* Medfield eMMC hardware reset GPIOs */
175 static int mfd_emmc0_rst_gpio = -EINVAL;
176 static int mfd_emmc1_rst_gpio = -EINVAL;
177
178 static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
179 {
180         struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
181         struct sfi_gpio_table_entry *entry;
182         int i, num;
183
184         num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
185         entry = (struct sfi_gpio_table_entry *)sb->pentry;
186
187         for (i = 0; i < num; i++, entry++) {
188                 if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
189                         mfd_emmc0_rst_gpio = entry->pin_no;
190                 else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
191                         mfd_emmc1_rst_gpio = entry->pin_no;
192         }
193
194         return 0;
195 }
196
197 #ifdef CONFIG_PM_RUNTIME
198
199 static irqreturn_t mfd_sd_cd(int irq, void *dev_id)
200 {
201         struct sdhci_pci_slot *slot = dev_id;
202         struct sdhci_host *host = slot->host;
203
204         mmc_detect_change(host->mmc, msecs_to_jiffies(200));
205         return IRQ_HANDLED;
206 }
207
208 #define MFLD_SD_CD_PIN 69
209
210 static int mfd_sd_probe_slot(struct sdhci_pci_slot *slot)
211 {
212         int err, irq, gpio = MFLD_SD_CD_PIN;
213
214         slot->cd_gpio = -EINVAL;
215         slot->cd_irq = -EINVAL;
216
217         err = gpio_request(gpio, "sd_cd");
218         if (err < 0)
219                 goto out;
220
221         err = gpio_direction_input(gpio);
222         if (err < 0)
223                 goto out_free;
224
225         irq = gpio_to_irq(gpio);
226         if (irq < 0)
227                 goto out_free;
228
229         err = request_irq(irq, mfd_sd_cd, IRQF_TRIGGER_RISING |
230                           IRQF_TRIGGER_FALLING, "sd_cd", slot);
231         if (err)
232                 goto out_free;
233
234         slot->cd_gpio = gpio;
235         slot->cd_irq = irq;
236         slot->host->quirks2 |= SDHCI_QUIRK2_OWN_CARD_DETECTION;
237
238         return 0;
239
240 out_free:
241         gpio_free(gpio);
242 out:
243         dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
244         return 0;
245 }
246
247 static void mfd_sd_remove_slot(struct sdhci_pci_slot *slot, int dead)
248 {
249         if (slot->cd_irq >= 0)
250                 free_irq(slot->cd_irq, slot);
251         gpio_free(slot->cd_gpio);
252 }
253
254 #else
255
256 #define mfd_sd_probe_slot       NULL
257 #define mfd_sd_remove_slot      NULL
258
259 #endif
260
261 static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
262 {
263         const char *name = NULL;
264         int gpio = -EINVAL;
265
266         sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
267
268         switch (slot->chip->pdev->device) {
269         case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
270                 gpio = mfd_emmc0_rst_gpio;
271                 name = "eMMC0_reset";
272                 break;
273         case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
274                 gpio = mfd_emmc1_rst_gpio;
275                 name = "eMMC1_reset";
276                 break;
277         }
278
279         if (!gpio_request(gpio, name)) {
280                 gpio_direction_output(gpio, 1);
281                 slot->rst_n_gpio = gpio;
282                 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
283         }
284
285         slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
286
287         slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
288
289         return 0;
290 }
291
292 static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
293 {
294         gpio_free(slot->rst_n_gpio);
295 }
296
297 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
298         .quirks         = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
299         .probe_slot     = mrst_hc_probe_slot,
300 };
301
302 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
303         .quirks         = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
304         .probe          = mrst_hc_probe,
305 };
306
307 static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
308         .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
309         .allow_runtime_pm = true,
310         .probe_slot     = mfd_sd_probe_slot,
311         .remove_slot    = mfd_sd_remove_slot,
312 };
313
314 static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
315         .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
316         .allow_runtime_pm = true,
317 };
318
319 static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
320         .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
321         .allow_runtime_pm = true,
322         .probe_slot     = mfd_emmc_probe_slot,
323         .remove_slot    = mfd_emmc_remove_slot,
324 };
325
326 /* O2Micro extra registers */
327 #define O2_SD_LOCK_WP           0xD3
328 #define O2_SD_MULTI_VCC3V       0xEE
329 #define O2_SD_CLKREQ            0xEC
330 #define O2_SD_CAPS              0xE0
331 #define O2_SD_ADMA1             0xE2
332 #define O2_SD_ADMA2             0xE7
333 #define O2_SD_INF_MOD           0xF1
334
335 static int o2_probe(struct sdhci_pci_chip *chip)
336 {
337         int ret;
338         u8 scratch;
339
340         switch (chip->pdev->device) {
341         case PCI_DEVICE_ID_O2_8220:
342         case PCI_DEVICE_ID_O2_8221:
343         case PCI_DEVICE_ID_O2_8320:
344         case PCI_DEVICE_ID_O2_8321:
345                 /* This extra setup is required due to broken ADMA. */
346                 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
347                 if (ret)
348                         return ret;
349                 scratch &= 0x7f;
350                 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
351
352                 /* Set Multi 3 to VCC3V# */
353                 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
354
355                 /* Disable CLK_REQ# support after media DET */
356                 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
357                 if (ret)
358                         return ret;
359                 scratch |= 0x20;
360                 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
361
362                 /* Choose capabilities, enable SDMA.  We have to write 0x01
363                  * to the capabilities register first to unlock it.
364                  */
365                 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
366                 if (ret)
367                         return ret;
368                 scratch |= 0x01;
369                 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
370                 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
371
372                 /* Disable ADMA1/2 */
373                 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
374                 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
375
376                 /* Disable the infinite transfer mode */
377                 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
378                 if (ret)
379                         return ret;
380                 scratch |= 0x08;
381                 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
382
383                 /* Lock WP */
384                 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
385                 if (ret)
386                         return ret;
387                 scratch |= 0x80;
388                 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
389         }
390
391         return 0;
392 }
393
394 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
395 {
396         u8 scratch;
397         int ret;
398
399         ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
400         if (ret)
401                 return ret;
402
403         /*
404          * Turn PMOS on [bit 0], set over current detection to 2.4 V
405          * [bit 1:2] and enable over current debouncing [bit 6].
406          */
407         if (on)
408                 scratch |= 0x47;
409         else
410                 scratch &= ~0x47;
411
412         ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
413         if (ret)
414                 return ret;
415
416         return 0;
417 }
418
419 static int jmicron_probe(struct sdhci_pci_chip *chip)
420 {
421         int ret;
422         u16 mmcdev = 0;
423
424         if (chip->pdev->revision == 0) {
425                 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
426                           SDHCI_QUIRK_32BIT_DMA_SIZE |
427                           SDHCI_QUIRK_32BIT_ADMA_SIZE |
428                           SDHCI_QUIRK_RESET_AFTER_REQUEST |
429                           SDHCI_QUIRK_BROKEN_SMALL_PIO;
430         }
431
432         /*
433          * JMicron chips can have two interfaces to the same hardware
434          * in order to work around limitations in Microsoft's driver.
435          * We need to make sure we only bind to one of them.
436          *
437          * This code assumes two things:
438          *
439          * 1. The PCI code adds subfunctions in order.
440          *
441          * 2. The MMC interface has a lower subfunction number
442          *    than the SD interface.
443          */
444         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
445                 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
446         else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
447                 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
448
449         if (mmcdev) {
450                 struct pci_dev *sd_dev;
451
452                 sd_dev = NULL;
453                 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
454                                                 mmcdev, sd_dev)) != NULL) {
455                         if ((PCI_SLOT(chip->pdev->devfn) ==
456                                 PCI_SLOT(sd_dev->devfn)) &&
457                                 (chip->pdev->bus == sd_dev->bus))
458                                 break;
459                 }
460
461                 if (sd_dev) {
462                         pci_dev_put(sd_dev);
463                         dev_info(&chip->pdev->dev, "Refusing to bind to "
464                                 "secondary interface.\n");
465                         return -ENODEV;
466                 }
467         }
468
469         /*
470          * JMicron chips need a bit of a nudge to enable the power
471          * output pins.
472          */
473         ret = jmicron_pmos(chip, 1);
474         if (ret) {
475                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
476                 return ret;
477         }
478
479         /* quirk for unsable RO-detection on JM388 chips */
480         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
481             chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
482                 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
483
484         return 0;
485 }
486
487 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
488 {
489         u8 scratch;
490
491         scratch = readb(host->ioaddr + 0xC0);
492
493         if (on)
494                 scratch |= 0x01;
495         else
496                 scratch &= ~0x01;
497
498         writeb(scratch, host->ioaddr + 0xC0);
499 }
500
501 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
502 {
503         if (slot->chip->pdev->revision == 0) {
504                 u16 version;
505
506                 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
507                 version = (version & SDHCI_VENDOR_VER_MASK) >>
508                         SDHCI_VENDOR_VER_SHIFT;
509
510                 /*
511                  * Older versions of the chip have lots of nasty glitches
512                  * in the ADMA engine. It's best just to avoid it
513                  * completely.
514                  */
515                 if (version < 0xAC)
516                         slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
517         }
518
519         /* JM388 MMC doesn't support 1.8V while SD supports it */
520         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
521                 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
522                         MMC_VDD_29_30 | MMC_VDD_30_31 |
523                         MMC_VDD_165_195; /* allow 1.8V */
524                 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
525                         MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
526         }
527
528         /*
529          * The secondary interface requires a bit set to get the
530          * interrupts.
531          */
532         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
533             slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
534                 jmicron_enable_mmc(slot->host, 1);
535
536         slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
537
538         return 0;
539 }
540
541 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
542 {
543         if (dead)
544                 return;
545
546         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
547             slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
548                 jmicron_enable_mmc(slot->host, 0);
549 }
550
551 static int jmicron_suspend(struct sdhci_pci_chip *chip)
552 {
553         int i;
554
555         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
556             chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
557                 for (i = 0; i < chip->num_slots; i++)
558                         jmicron_enable_mmc(chip->slots[i]->host, 0);
559         }
560
561         return 0;
562 }
563
564 static int jmicron_resume(struct sdhci_pci_chip *chip)
565 {
566         int ret, i;
567
568         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
569             chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
570                 for (i = 0; i < chip->num_slots; i++)
571                         jmicron_enable_mmc(chip->slots[i]->host, 1);
572         }
573
574         ret = jmicron_pmos(chip, 1);
575         if (ret) {
576                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
577                 return ret;
578         }
579
580         return 0;
581 }
582
583 static const struct sdhci_pci_fixes sdhci_o2 = {
584         .probe          = o2_probe,
585 };
586
587 static const struct sdhci_pci_fixes sdhci_jmicron = {
588         .probe          = jmicron_probe,
589
590         .probe_slot     = jmicron_probe_slot,
591         .remove_slot    = jmicron_remove_slot,
592
593         .suspend        = jmicron_suspend,
594         .resume         = jmicron_resume,
595 };
596
597 /* SysKonnect CardBus2SDIO extra registers */
598 #define SYSKT_CTRL              0x200
599 #define SYSKT_RDFIFO_STAT       0x204
600 #define SYSKT_WRFIFO_STAT       0x208
601 #define SYSKT_POWER_DATA        0x20c
602 #define   SYSKT_POWER_330       0xef
603 #define   SYSKT_POWER_300       0xf8
604 #define   SYSKT_POWER_184       0xcc
605 #define SYSKT_POWER_CMD         0x20d
606 #define   SYSKT_POWER_START     (1 << 7)
607 #define SYSKT_POWER_STATUS      0x20e
608 #define   SYSKT_POWER_STATUS_OK (1 << 0)
609 #define SYSKT_BOARD_REV         0x210
610 #define SYSKT_CHIP_REV          0x211
611 #define SYSKT_CONF_DATA         0x212
612 #define   SYSKT_CONF_DATA_1V8   (1 << 2)
613 #define   SYSKT_CONF_DATA_2V5   (1 << 1)
614 #define   SYSKT_CONF_DATA_3V3   (1 << 0)
615
616 static int syskt_probe(struct sdhci_pci_chip *chip)
617 {
618         if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
619                 chip->pdev->class &= ~0x0000FF;
620                 chip->pdev->class |= PCI_SDHCI_IFDMA;
621         }
622         return 0;
623 }
624
625 static int syskt_probe_slot(struct sdhci_pci_slot *slot)
626 {
627         int tm, ps;
628
629         u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
630         u8  chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
631         dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
632                                          "board rev %d.%d, chip rev %d.%d\n",
633                                          board_rev >> 4, board_rev & 0xf,
634                                          chip_rev >> 4,  chip_rev & 0xf);
635         if (chip_rev >= 0x20)
636                 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
637
638         writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
639         writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
640         udelay(50);
641         tm = 10;  /* Wait max 1 ms */
642         do {
643                 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
644                 if (ps & SYSKT_POWER_STATUS_OK)
645                         break;
646                 udelay(100);
647         } while (--tm);
648         if (!tm) {
649                 dev_err(&slot->chip->pdev->dev,
650                         "power regulator never stabilized");
651                 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
652                 return -ENODEV;
653         }
654
655         return 0;
656 }
657
658 static const struct sdhci_pci_fixes sdhci_syskt = {
659         .quirks         = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
660         .probe          = syskt_probe,
661         .probe_slot     = syskt_probe_slot,
662 };
663
664 static int via_probe(struct sdhci_pci_chip *chip)
665 {
666         if (chip->pdev->revision == 0x10)
667                 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
668
669         return 0;
670 }
671
672 static const struct sdhci_pci_fixes sdhci_via = {
673         .probe          = via_probe,
674 };
675
676 static const struct pci_device_id pci_ids[] __devinitdata = {
677         {
678                 .vendor         = PCI_VENDOR_ID_RICOH,
679                 .device         = PCI_DEVICE_ID_RICOH_R5C822,
680                 .subvendor      = PCI_ANY_ID,
681                 .subdevice      = PCI_ANY_ID,
682                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh,
683         },
684
685         {
686                 .vendor         = PCI_VENDOR_ID_RICOH,
687                 .device         = 0x843,
688                 .subvendor      = PCI_ANY_ID,
689                 .subdevice      = PCI_ANY_ID,
690                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh_mmc,
691         },
692
693         {
694                 .vendor         = PCI_VENDOR_ID_RICOH,
695                 .device         = 0xe822,
696                 .subvendor      = PCI_ANY_ID,
697                 .subdevice      = PCI_ANY_ID,
698                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh_mmc,
699         },
700
701         {
702                 .vendor         = PCI_VENDOR_ID_RICOH,
703                 .device         = 0xe823,
704                 .subvendor      = PCI_ANY_ID,
705                 .subdevice      = PCI_ANY_ID,
706                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh_mmc,
707         },
708
709         {
710                 .vendor         = PCI_VENDOR_ID_ENE,
711                 .device         = PCI_DEVICE_ID_ENE_CB712_SD,
712                 .subvendor      = PCI_ANY_ID,
713                 .subdevice      = PCI_ANY_ID,
714                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
715         },
716
717         {
718                 .vendor         = PCI_VENDOR_ID_ENE,
719                 .device         = PCI_DEVICE_ID_ENE_CB712_SD_2,
720                 .subvendor      = PCI_ANY_ID,
721                 .subdevice      = PCI_ANY_ID,
722                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
723         },
724
725         {
726                 .vendor         = PCI_VENDOR_ID_ENE,
727                 .device         = PCI_DEVICE_ID_ENE_CB714_SD,
728                 .subvendor      = PCI_ANY_ID,
729                 .subdevice      = PCI_ANY_ID,
730                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
731         },
732
733         {
734                 .vendor         = PCI_VENDOR_ID_ENE,
735                 .device         = PCI_DEVICE_ID_ENE_CB714_SD_2,
736                 .subvendor      = PCI_ANY_ID,
737                 .subdevice      = PCI_ANY_ID,
738                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
739         },
740
741         {
742                 .vendor         = PCI_VENDOR_ID_MARVELL,
743                 .device         = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
744                 .subvendor      = PCI_ANY_ID,
745                 .subdevice      = PCI_ANY_ID,
746                 .driver_data    = (kernel_ulong_t)&sdhci_cafe,
747         },
748
749         {
750                 .vendor         = PCI_VENDOR_ID_JMICRON,
751                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
752                 .subvendor      = PCI_ANY_ID,
753                 .subdevice      = PCI_ANY_ID,
754                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
755         },
756
757         {
758                 .vendor         = PCI_VENDOR_ID_JMICRON,
759                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
760                 .subvendor      = PCI_ANY_ID,
761                 .subdevice      = PCI_ANY_ID,
762                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
763         },
764
765         {
766                 .vendor         = PCI_VENDOR_ID_JMICRON,
767                 .device         = PCI_DEVICE_ID_JMICRON_JMB388_SD,
768                 .subvendor      = PCI_ANY_ID,
769                 .subdevice      = PCI_ANY_ID,
770                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
771         },
772
773         {
774                 .vendor         = PCI_VENDOR_ID_JMICRON,
775                 .device         = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
776                 .subvendor      = PCI_ANY_ID,
777                 .subdevice      = PCI_ANY_ID,
778                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
779         },
780
781         {
782                 .vendor         = PCI_VENDOR_ID_SYSKONNECT,
783                 .device         = 0x8000,
784                 .subvendor      = PCI_ANY_ID,
785                 .subdevice      = PCI_ANY_ID,
786                 .driver_data    = (kernel_ulong_t)&sdhci_syskt,
787         },
788
789         {
790                 .vendor         = PCI_VENDOR_ID_VIA,
791                 .device         = 0x95d0,
792                 .subvendor      = PCI_ANY_ID,
793                 .subdevice      = PCI_ANY_ID,
794                 .driver_data    = (kernel_ulong_t)&sdhci_via,
795         },
796
797         {
798                 .vendor         = PCI_VENDOR_ID_INTEL,
799                 .device         = PCI_DEVICE_ID_INTEL_MRST_SD0,
800                 .subvendor      = PCI_ANY_ID,
801                 .subdevice      = PCI_ANY_ID,
802                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
803         },
804
805         {
806                 .vendor         = PCI_VENDOR_ID_INTEL,
807                 .device         = PCI_DEVICE_ID_INTEL_MRST_SD1,
808                 .subvendor      = PCI_ANY_ID,
809                 .subdevice      = PCI_ANY_ID,
810                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
811         },
812
813         {
814                 .vendor         = PCI_VENDOR_ID_INTEL,
815                 .device         = PCI_DEVICE_ID_INTEL_MRST_SD2,
816                 .subvendor      = PCI_ANY_ID,
817                 .subdevice      = PCI_ANY_ID,
818                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
819         },
820
821         {
822                 .vendor         = PCI_VENDOR_ID_INTEL,
823                 .device         = PCI_DEVICE_ID_INTEL_MFD_SD,
824                 .subvendor      = PCI_ANY_ID,
825                 .subdevice      = PCI_ANY_ID,
826                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_sd,
827         },
828
829         {
830                 .vendor         = PCI_VENDOR_ID_INTEL,
831                 .device         = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
832                 .subvendor      = PCI_ANY_ID,
833                 .subdevice      = PCI_ANY_ID,
834                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
835         },
836
837         {
838                 .vendor         = PCI_VENDOR_ID_INTEL,
839                 .device         = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
840                 .subvendor      = PCI_ANY_ID,
841                 .subdevice      = PCI_ANY_ID,
842                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
843         },
844
845         {
846                 .vendor         = PCI_VENDOR_ID_INTEL,
847                 .device         = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
848                 .subvendor      = PCI_ANY_ID,
849                 .subdevice      = PCI_ANY_ID,
850                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
851         },
852
853         {
854                 .vendor         = PCI_VENDOR_ID_INTEL,
855                 .device         = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
856                 .subvendor      = PCI_ANY_ID,
857                 .subdevice      = PCI_ANY_ID,
858                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
859         },
860
861         {
862                 .vendor         = PCI_VENDOR_ID_O2,
863                 .device         = PCI_DEVICE_ID_O2_8120,
864                 .subvendor      = PCI_ANY_ID,
865                 .subdevice      = PCI_ANY_ID,
866                 .driver_data    = (kernel_ulong_t)&sdhci_o2,
867         },
868
869         {
870                 .vendor         = PCI_VENDOR_ID_O2,
871                 .device         = PCI_DEVICE_ID_O2_8220,
872                 .subvendor      = PCI_ANY_ID,
873                 .subdevice      = PCI_ANY_ID,
874                 .driver_data    = (kernel_ulong_t)&sdhci_o2,
875         },
876
877         {
878                 .vendor         = PCI_VENDOR_ID_O2,
879                 .device         = PCI_DEVICE_ID_O2_8221,
880                 .subvendor      = PCI_ANY_ID,
881                 .subdevice      = PCI_ANY_ID,
882                 .driver_data    = (kernel_ulong_t)&sdhci_o2,
883         },
884
885         {
886                 .vendor         = PCI_VENDOR_ID_O2,
887                 .device         = PCI_DEVICE_ID_O2_8320,
888                 .subvendor      = PCI_ANY_ID,
889                 .subdevice      = PCI_ANY_ID,
890                 .driver_data    = (kernel_ulong_t)&sdhci_o2,
891         },
892
893         {
894                 .vendor         = PCI_VENDOR_ID_O2,
895                 .device         = PCI_DEVICE_ID_O2_8321,
896                 .subvendor      = PCI_ANY_ID,
897                 .subdevice      = PCI_ANY_ID,
898                 .driver_data    = (kernel_ulong_t)&sdhci_o2,
899         },
900
901         {       /* Generic SD host controller */
902                 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
903         },
904
905         { /* end: all zeroes */ },
906 };
907
908 MODULE_DEVICE_TABLE(pci, pci_ids);
909
910 /*****************************************************************************\
911  *                                                                           *
912  * SDHCI core callbacks                                                      *
913  *                                                                           *
914 \*****************************************************************************/
915
916 static int sdhci_pci_enable_dma(struct sdhci_host *host)
917 {
918         struct sdhci_pci_slot *slot;
919         struct pci_dev *pdev;
920         int ret;
921
922         slot = sdhci_priv(host);
923         pdev = slot->chip->pdev;
924
925         if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
926                 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
927                 (host->flags & SDHCI_USE_SDMA)) {
928                 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
929                         "doesn't fully claim to support it.\n");
930         }
931
932         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
933         if (ret)
934                 return ret;
935
936         pci_set_master(pdev);
937
938         return 0;
939 }
940
941 static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
942 {
943         u8 ctrl;
944
945         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
946
947         switch (width) {
948         case MMC_BUS_WIDTH_8:
949                 ctrl |= SDHCI_CTRL_8BITBUS;
950                 ctrl &= ~SDHCI_CTRL_4BITBUS;
951                 break;
952         case MMC_BUS_WIDTH_4:
953                 ctrl |= SDHCI_CTRL_4BITBUS;
954                 ctrl &= ~SDHCI_CTRL_8BITBUS;
955                 break;
956         default:
957                 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
958                 break;
959         }
960
961         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
962
963         return 0;
964 }
965
966 static void sdhci_pci_hw_reset(struct sdhci_host *host)
967 {
968         struct sdhci_pci_slot *slot = sdhci_priv(host);
969         int rst_n_gpio = slot->rst_n_gpio;
970
971         if (!gpio_is_valid(rst_n_gpio))
972                 return;
973         gpio_set_value_cansleep(rst_n_gpio, 0);
974         /* For eMMC, minimum is 1us but give it 10us for good measure */
975         udelay(10);
976         gpio_set_value_cansleep(rst_n_gpio, 1);
977         /* For eMMC, minimum is 200us but give it 300us for good measure */
978         usleep_range(300, 1000);
979 }
980
981 static struct sdhci_ops sdhci_pci_ops = {
982         .enable_dma     = sdhci_pci_enable_dma,
983         .platform_8bit_width    = sdhci_pci_8bit_width,
984         .hw_reset               = sdhci_pci_hw_reset,
985 };
986
987 /*****************************************************************************\
988  *                                                                           *
989  * Suspend/resume                                                            *
990  *                                                                           *
991 \*****************************************************************************/
992
993 #ifdef CONFIG_PM
994
995 static int sdhci_pci_suspend(struct device *dev)
996 {
997         struct pci_dev *pdev = to_pci_dev(dev);
998         struct sdhci_pci_chip *chip;
999         struct sdhci_pci_slot *slot;
1000         mmc_pm_flag_t slot_pm_flags;
1001         mmc_pm_flag_t pm_flags = 0;
1002         int i, ret;
1003
1004         chip = pci_get_drvdata(pdev);
1005         if (!chip)
1006                 return 0;
1007
1008         for (i = 0; i < chip->num_slots; i++) {
1009                 slot = chip->slots[i];
1010                 if (!slot)
1011                         continue;
1012
1013                 ret = sdhci_suspend_host(slot->host);
1014
1015                 if (ret)
1016                         goto err_pci_suspend;
1017
1018                 slot_pm_flags = slot->host->mmc->pm_flags;
1019                 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1020                         sdhci_enable_irq_wakeups(slot->host);
1021
1022                 pm_flags |= slot_pm_flags;
1023         }
1024
1025         if (chip->fixes && chip->fixes->suspend) {
1026                 ret = chip->fixes->suspend(chip);
1027                 if (ret)
1028                         goto err_pci_suspend;
1029         }
1030
1031         pci_save_state(pdev);
1032         if (pm_flags & MMC_PM_KEEP_POWER) {
1033                 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1034                         pci_pme_active(pdev, true);
1035                         pci_enable_wake(pdev, PCI_D3hot, 1);
1036                 }
1037                 pci_set_power_state(pdev, PCI_D3hot);
1038         } else {
1039                 pci_enable_wake(pdev, PCI_D3hot, 0);
1040                 pci_disable_device(pdev);
1041                 pci_set_power_state(pdev, PCI_D3hot);
1042         }
1043
1044         return 0;
1045
1046 err_pci_suspend:
1047         while (--i >= 0)
1048                 sdhci_resume_host(chip->slots[i]->host);
1049         return ret;
1050 }
1051
1052 static int sdhci_pci_resume(struct device *dev)
1053 {
1054         struct pci_dev *pdev = to_pci_dev(dev);
1055         struct sdhci_pci_chip *chip;
1056         struct sdhci_pci_slot *slot;
1057         int i, ret;
1058
1059         chip = pci_get_drvdata(pdev);
1060         if (!chip)
1061                 return 0;
1062
1063         pci_set_power_state(pdev, PCI_D0);
1064         pci_restore_state(pdev);
1065         ret = pci_enable_device(pdev);
1066         if (ret)
1067                 return ret;
1068
1069         if (chip->fixes && chip->fixes->resume) {
1070                 ret = chip->fixes->resume(chip);
1071                 if (ret)
1072                         return ret;
1073         }
1074
1075         for (i = 0; i < chip->num_slots; i++) {
1076                 slot = chip->slots[i];
1077                 if (!slot)
1078                         continue;
1079
1080                 ret = sdhci_resume_host(slot->host);
1081                 if (ret)
1082                         return ret;
1083         }
1084
1085         return 0;
1086 }
1087
1088 #else /* CONFIG_PM */
1089
1090 #define sdhci_pci_suspend NULL
1091 #define sdhci_pci_resume NULL
1092
1093 #endif /* CONFIG_PM */
1094
1095 #ifdef CONFIG_PM_RUNTIME
1096
1097 static int sdhci_pci_runtime_suspend(struct device *dev)
1098 {
1099         struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1100         struct sdhci_pci_chip *chip;
1101         struct sdhci_pci_slot *slot;
1102         int i, ret;
1103
1104         chip = pci_get_drvdata(pdev);
1105         if (!chip)
1106                 return 0;
1107
1108         for (i = 0; i < chip->num_slots; i++) {
1109                 slot = chip->slots[i];
1110                 if (!slot)
1111                         continue;
1112
1113                 ret = sdhci_runtime_suspend_host(slot->host);
1114
1115                 if (ret)
1116                         goto err_pci_runtime_suspend;
1117         }
1118
1119         if (chip->fixes && chip->fixes->suspend) {
1120                 ret = chip->fixes->suspend(chip);
1121                 if (ret)
1122                         goto err_pci_runtime_suspend;
1123         }
1124
1125         return 0;
1126
1127 err_pci_runtime_suspend:
1128         while (--i >= 0)
1129                 sdhci_runtime_resume_host(chip->slots[i]->host);
1130         return ret;
1131 }
1132
1133 static int sdhci_pci_runtime_resume(struct device *dev)
1134 {
1135         struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1136         struct sdhci_pci_chip *chip;
1137         struct sdhci_pci_slot *slot;
1138         int i, ret;
1139
1140         chip = pci_get_drvdata(pdev);
1141         if (!chip)
1142                 return 0;
1143
1144         if (chip->fixes && chip->fixes->resume) {
1145                 ret = chip->fixes->resume(chip);
1146                 if (ret)
1147                         return ret;
1148         }
1149
1150         for (i = 0; i < chip->num_slots; i++) {
1151                 slot = chip->slots[i];
1152                 if (!slot)
1153                         continue;
1154
1155                 ret = sdhci_runtime_resume_host(slot->host);
1156                 if (ret)
1157                         return ret;
1158         }
1159
1160         return 0;
1161 }
1162
1163 static int sdhci_pci_runtime_idle(struct device *dev)
1164 {
1165         return 0;
1166 }
1167
1168 #else
1169
1170 #define sdhci_pci_runtime_suspend       NULL
1171 #define sdhci_pci_runtime_resume        NULL
1172 #define sdhci_pci_runtime_idle          NULL
1173
1174 #endif
1175
1176 static const struct dev_pm_ops sdhci_pci_pm_ops = {
1177         .suspend = sdhci_pci_suspend,
1178         .resume = sdhci_pci_resume,
1179         .runtime_suspend = sdhci_pci_runtime_suspend,
1180         .runtime_resume = sdhci_pci_runtime_resume,
1181         .runtime_idle = sdhci_pci_runtime_idle,
1182 };
1183
1184 /*****************************************************************************\
1185  *                                                                           *
1186  * Device probing/removal                                                    *
1187  *                                                                           *
1188 \*****************************************************************************/
1189
1190 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
1191         struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
1192 {
1193         struct sdhci_pci_slot *slot;
1194         struct sdhci_host *host;
1195         int ret;
1196
1197         if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1198                 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1199                 return ERR_PTR(-ENODEV);
1200         }
1201
1202         if (pci_resource_len(pdev, bar) != 0x100) {
1203                 dev_err(&pdev->dev, "Invalid iomem size. You may "
1204                         "experience problems.\n");
1205         }
1206
1207         if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1208                 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1209                 return ERR_PTR(-ENODEV);
1210         }
1211
1212         if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1213                 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1214                 return ERR_PTR(-ENODEV);
1215         }
1216
1217         host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1218         if (IS_ERR(host)) {
1219                 dev_err(&pdev->dev, "cannot allocate host\n");
1220                 return ERR_CAST(host);
1221         }
1222
1223         slot = sdhci_priv(host);
1224
1225         slot->chip = chip;
1226         slot->host = host;
1227         slot->pci_bar = bar;
1228         slot->rst_n_gpio = -EINVAL;
1229
1230         host->hw_name = "PCI";
1231         host->ops = &sdhci_pci_ops;
1232         host->quirks = chip->quirks;
1233
1234         host->irq = pdev->irq;
1235
1236         ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1237         if (ret) {
1238                 dev_err(&pdev->dev, "cannot request region\n");
1239                 goto free;
1240         }
1241
1242         host->ioaddr = pci_ioremap_bar(pdev, bar);
1243         if (!host->ioaddr) {
1244                 dev_err(&pdev->dev, "failed to remap registers\n");
1245                 ret = -ENOMEM;
1246                 goto release;
1247         }
1248
1249         if (chip->fixes && chip->fixes->probe_slot) {
1250                 ret = chip->fixes->probe_slot(slot);
1251                 if (ret)
1252                         goto unmap;
1253         }
1254
1255         host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1256
1257         ret = sdhci_add_host(host);
1258         if (ret)
1259                 goto remove;
1260
1261         return slot;
1262
1263 remove:
1264         if (chip->fixes && chip->fixes->remove_slot)
1265                 chip->fixes->remove_slot(slot, 0);
1266
1267 unmap:
1268         iounmap(host->ioaddr);
1269
1270 release:
1271         pci_release_region(pdev, bar);
1272
1273 free:
1274         sdhci_free_host(host);
1275
1276         return ERR_PTR(ret);
1277 }
1278
1279 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1280 {
1281         int dead;
1282         u32 scratch;
1283
1284         dead = 0;
1285         scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1286         if (scratch == (u32)-1)
1287                 dead = 1;
1288
1289         sdhci_remove_host(slot->host, dead);
1290
1291         if (slot->chip->fixes && slot->chip->fixes->remove_slot)
1292                 slot->chip->fixes->remove_slot(slot, dead);
1293
1294         pci_release_region(slot->chip->pdev, slot->pci_bar);
1295
1296         sdhci_free_host(slot->host);
1297 }
1298
1299 static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
1300 {
1301         pm_runtime_put_noidle(dev);
1302         pm_runtime_allow(dev);
1303         pm_runtime_set_autosuspend_delay(dev, 50);
1304         pm_runtime_use_autosuspend(dev);
1305         pm_suspend_ignore_children(dev, 1);
1306 }
1307
1308 static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
1309 {
1310         pm_runtime_forbid(dev);
1311         pm_runtime_get_noresume(dev);
1312 }
1313
1314 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1315                                      const struct pci_device_id *ent)
1316 {
1317         struct sdhci_pci_chip *chip;
1318         struct sdhci_pci_slot *slot;
1319
1320         u8 slots, first_bar;
1321         int ret, i;
1322
1323         BUG_ON(pdev == NULL);
1324         BUG_ON(ent == NULL);
1325
1326         dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
1327                  (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
1328
1329         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1330         if (ret)
1331                 return ret;
1332
1333         slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1334         dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1335         if (slots == 0)
1336                 return -ENODEV;
1337
1338         BUG_ON(slots > MAX_SLOTS);
1339
1340         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1341         if (ret)
1342                 return ret;
1343
1344         first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1345
1346         if (first_bar > 5) {
1347                 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1348                 return -ENODEV;
1349         }
1350
1351         ret = pci_enable_device(pdev);
1352         if (ret)
1353                 return ret;
1354
1355         chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1356         if (!chip) {
1357                 ret = -ENOMEM;
1358                 goto err;
1359         }
1360
1361         chip->pdev = pdev;
1362         chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
1363         if (chip->fixes) {
1364                 chip->quirks = chip->fixes->quirks;
1365                 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1366         }
1367         chip->num_slots = slots;
1368
1369         pci_set_drvdata(pdev, chip);
1370
1371         if (chip->fixes && chip->fixes->probe) {
1372                 ret = chip->fixes->probe(chip);
1373                 if (ret)
1374                         goto free;
1375         }
1376
1377         slots = chip->num_slots;        /* Quirk may have changed this */
1378
1379         for (i = 0; i < slots; i++) {
1380                 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1381                 if (IS_ERR(slot)) {
1382                         for (i--; i >= 0; i--)
1383                                 sdhci_pci_remove_slot(chip->slots[i]);
1384                         ret = PTR_ERR(slot);
1385                         goto free;
1386                 }
1387
1388                 chip->slots[i] = slot;
1389         }
1390
1391         if (chip->allow_runtime_pm)
1392                 sdhci_pci_runtime_pm_allow(&pdev->dev);
1393
1394         return 0;
1395
1396 free:
1397         pci_set_drvdata(pdev, NULL);
1398         kfree(chip);
1399
1400 err:
1401         pci_disable_device(pdev);
1402         return ret;
1403 }
1404
1405 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1406 {
1407         int i;
1408         struct sdhci_pci_chip *chip;
1409
1410         chip = pci_get_drvdata(pdev);
1411
1412         if (chip) {
1413                 if (chip->allow_runtime_pm)
1414                         sdhci_pci_runtime_pm_forbid(&pdev->dev);
1415
1416                 for (i = 0; i < chip->num_slots; i++)
1417                         sdhci_pci_remove_slot(chip->slots[i]);
1418
1419                 pci_set_drvdata(pdev, NULL);
1420                 kfree(chip);
1421         }
1422
1423         pci_disable_device(pdev);
1424 }
1425
1426 static struct pci_driver sdhci_driver = {
1427         .name =         "sdhci-pci",
1428         .id_table =     pci_ids,
1429         .probe =        sdhci_pci_probe,
1430         .remove =       __devexit_p(sdhci_pci_remove),
1431         .driver =       {
1432                 .pm =   &sdhci_pci_pm_ops
1433         },
1434 };
1435
1436 /*****************************************************************************\
1437  *                                                                           *
1438  * Driver init/exit                                                          *
1439  *                                                                           *
1440 \*****************************************************************************/
1441
1442 static int __init sdhci_drv_init(void)
1443 {
1444         return pci_register_driver(&sdhci_driver);
1445 }
1446
1447 static void __exit sdhci_drv_exit(void)
1448 {
1449         pci_unregister_driver(&sdhci_driver);
1450 }
1451
1452 module_init(sdhci_drv_init);
1453 module_exit(sdhci_drv_exit);
1454
1455 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
1456 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1457 MODULE_LICENSE("GPL");