ARM: mm: Recreate kernel mappings in early_paging_init()
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-pci.c
blobd7d6bc8968d2422d8b2e9fb44a695b35b17927d4
1 /* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
3 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
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.
10 * Thanks to the following companies for their support:
12 * - JMicron (hardware and technical support)
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/pm_runtime.h>
27 #include <linux/mmc/sdhci-pci-data.h>
29 #include "sdhci.h"
32 * PCI device IDs
34 #define PCI_DEVICE_ID_INTEL_PCH_SDIO0 0x8809
35 #define PCI_DEVICE_ID_INTEL_PCH_SDIO1 0x880a
36 #define PCI_DEVICE_ID_INTEL_BYT_EMMC 0x0f14
37 #define PCI_DEVICE_ID_INTEL_BYT_SDIO 0x0f15
38 #define PCI_DEVICE_ID_INTEL_BYT_SD 0x0f16
39 #define PCI_DEVICE_ID_INTEL_BYT_EMMC2 0x0f50
42 * PCI registers
45 #define PCI_SDHCI_IFPIO 0x00
46 #define PCI_SDHCI_IFDMA 0x01
47 #define PCI_SDHCI_IFVENDOR 0x02
49 #define PCI_SLOT_INFO 0x40 /* 8 bits */
50 #define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
51 #define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
53 #define MAX_SLOTS 8
55 struct sdhci_pci_chip;
56 struct sdhci_pci_slot;
58 struct sdhci_pci_fixes {
59 unsigned int quirks;
60 unsigned int quirks2;
61 bool allow_runtime_pm;
63 int (*probe) (struct sdhci_pci_chip *);
65 int (*probe_slot) (struct sdhci_pci_slot *);
66 void (*remove_slot) (struct sdhci_pci_slot *, int);
68 int (*suspend) (struct sdhci_pci_chip *);
69 int (*resume) (struct sdhci_pci_chip *);
72 struct sdhci_pci_slot {
73 struct sdhci_pci_chip *chip;
74 struct sdhci_host *host;
75 struct sdhci_pci_data *data;
77 int pci_bar;
78 int rst_n_gpio;
79 int cd_gpio;
80 int cd_irq;
82 void (*hw_reset)(struct sdhci_host *host);
85 struct sdhci_pci_chip {
86 struct pci_dev *pdev;
88 unsigned int quirks;
89 unsigned int quirks2;
90 bool allow_runtime_pm;
91 const struct sdhci_pci_fixes *fixes;
93 int num_slots; /* Slots on controller */
94 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
98 /*****************************************************************************\
99 * *
100 * Hardware specific quirk handling *
102 \*****************************************************************************/
104 static int ricoh_probe(struct sdhci_pci_chip *chip)
106 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
107 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
108 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
109 return 0;
112 static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
114 slot->host->caps =
115 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
116 & SDHCI_TIMEOUT_CLK_MASK) |
118 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
119 & SDHCI_CLOCK_BASE_MASK) |
121 SDHCI_TIMEOUT_CLK_UNIT |
122 SDHCI_CAN_VDD_330 |
123 SDHCI_CAN_DO_HISPD |
124 SDHCI_CAN_DO_SDMA;
125 return 0;
128 static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
130 /* Apply a delay to allow controller to settle */
131 /* Otherwise it becomes confused if card state changed
132 during suspend */
133 msleep(500);
134 return 0;
137 static const struct sdhci_pci_fixes sdhci_ricoh = {
138 .probe = ricoh_probe,
139 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
140 SDHCI_QUIRK_FORCE_DMA |
141 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
144 static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
145 .probe_slot = ricoh_mmc_probe_slot,
146 .resume = ricoh_mmc_resume,
147 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
148 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
149 SDHCI_QUIRK_NO_CARD_NO_RESET |
150 SDHCI_QUIRK_MISSING_CAPS
153 static const struct sdhci_pci_fixes sdhci_ene_712 = {
154 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
155 SDHCI_QUIRK_BROKEN_DMA,
158 static const struct sdhci_pci_fixes sdhci_ene_714 = {
159 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
160 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
161 SDHCI_QUIRK_BROKEN_DMA,
164 static const struct sdhci_pci_fixes sdhci_cafe = {
165 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
166 SDHCI_QUIRK_NO_BUSY_IRQ |
167 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
168 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
171 static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
173 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
174 return 0;
178 * ADMA operation is disabled for Moorestown platform due to
179 * hardware bugs.
181 static int mrst_hc_probe(struct sdhci_pci_chip *chip)
184 * slots number is fixed here for MRST as SDIO3/5 are never used and
185 * have hardware bugs.
187 chip->num_slots = 1;
188 return 0;
191 static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
193 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
194 return 0;
197 #ifdef CONFIG_PM_RUNTIME
199 static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
201 struct sdhci_pci_slot *slot = dev_id;
202 struct sdhci_host *host = slot->host;
204 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
205 return IRQ_HANDLED;
208 static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
210 int err, irq, gpio = slot->cd_gpio;
212 slot->cd_gpio = -EINVAL;
213 slot->cd_irq = -EINVAL;
215 if (!gpio_is_valid(gpio))
216 return;
218 err = gpio_request(gpio, "sd_cd");
219 if (err < 0)
220 goto out;
222 err = gpio_direction_input(gpio);
223 if (err < 0)
224 goto out_free;
226 irq = gpio_to_irq(gpio);
227 if (irq < 0)
228 goto out_free;
230 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
231 IRQF_TRIGGER_FALLING, "sd_cd", slot);
232 if (err)
233 goto out_free;
235 slot->cd_gpio = gpio;
236 slot->cd_irq = irq;
238 return;
240 out_free:
241 gpio_free(gpio);
242 out:
243 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
246 static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
248 if (slot->cd_irq >= 0)
249 free_irq(slot->cd_irq, slot);
250 if (gpio_is_valid(slot->cd_gpio))
251 gpio_free(slot->cd_gpio);
254 #else
256 static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
260 static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
264 #endif
266 static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
268 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
269 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
270 MMC_CAP2_HC_ERASE_SZ;
271 return 0;
274 static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
276 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
277 return 0;
280 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
281 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
282 .probe_slot = mrst_hc_probe_slot,
285 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
286 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
287 .probe = mrst_hc_probe,
290 static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
291 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
292 .allow_runtime_pm = true,
295 static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
296 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
297 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
298 .allow_runtime_pm = true,
299 .probe_slot = mfd_sdio_probe_slot,
302 static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
303 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
304 .allow_runtime_pm = true,
305 .probe_slot = mfd_emmc_probe_slot,
308 static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
309 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
310 .probe_slot = pch_hc_probe_slot,
313 static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
315 u8 reg;
317 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
318 reg |= 0x10;
319 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
320 /* For eMMC, minimum is 1us but give it 9us for good measure */
321 udelay(9);
322 reg &= ~0x10;
323 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
324 /* For eMMC, minimum is 200us but give it 300us for good measure */
325 usleep_range(300, 1000);
328 static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
330 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
331 MMC_CAP_HW_RESET;
332 slot->host->mmc->caps2 |= MMC_CAP2_HC_ERASE_SZ;
333 slot->hw_reset = sdhci_pci_int_hw_reset;
334 return 0;
337 static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
339 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
340 return 0;
343 static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
344 .allow_runtime_pm = true,
345 .probe_slot = byt_emmc_probe_slot,
348 static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
349 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
350 .allow_runtime_pm = true,
351 .probe_slot = byt_sdio_probe_slot,
354 static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
355 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
356 .allow_runtime_pm = true,
359 /* O2Micro extra registers */
360 #define O2_SD_LOCK_WP 0xD3
361 #define O2_SD_MULTI_VCC3V 0xEE
362 #define O2_SD_CLKREQ 0xEC
363 #define O2_SD_CAPS 0xE0
364 #define O2_SD_ADMA1 0xE2
365 #define O2_SD_ADMA2 0xE7
366 #define O2_SD_INF_MOD 0xF1
368 static int o2_probe(struct sdhci_pci_chip *chip)
370 int ret;
371 u8 scratch;
373 switch (chip->pdev->device) {
374 case PCI_DEVICE_ID_O2_8220:
375 case PCI_DEVICE_ID_O2_8221:
376 case PCI_DEVICE_ID_O2_8320:
377 case PCI_DEVICE_ID_O2_8321:
378 /* This extra setup is required due to broken ADMA. */
379 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
380 if (ret)
381 return ret;
382 scratch &= 0x7f;
383 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
385 /* Set Multi 3 to VCC3V# */
386 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
388 /* Disable CLK_REQ# support after media DET */
389 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
390 if (ret)
391 return ret;
392 scratch |= 0x20;
393 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
395 /* Choose capabilities, enable SDMA. We have to write 0x01
396 * to the capabilities register first to unlock it.
398 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
399 if (ret)
400 return ret;
401 scratch |= 0x01;
402 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
403 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
405 /* Disable ADMA1/2 */
406 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
407 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
409 /* Disable the infinite transfer mode */
410 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
411 if (ret)
412 return ret;
413 scratch |= 0x08;
414 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
416 /* Lock WP */
417 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
418 if (ret)
419 return ret;
420 scratch |= 0x80;
421 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
424 return 0;
427 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
429 u8 scratch;
430 int ret;
432 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
433 if (ret)
434 return ret;
437 * Turn PMOS on [bit 0], set over current detection to 2.4 V
438 * [bit 1:2] and enable over current debouncing [bit 6].
440 if (on)
441 scratch |= 0x47;
442 else
443 scratch &= ~0x47;
445 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
446 if (ret)
447 return ret;
449 return 0;
452 static int jmicron_probe(struct sdhci_pci_chip *chip)
454 int ret;
455 u16 mmcdev = 0;
457 if (chip->pdev->revision == 0) {
458 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
459 SDHCI_QUIRK_32BIT_DMA_SIZE |
460 SDHCI_QUIRK_32BIT_ADMA_SIZE |
461 SDHCI_QUIRK_RESET_AFTER_REQUEST |
462 SDHCI_QUIRK_BROKEN_SMALL_PIO;
466 * JMicron chips can have two interfaces to the same hardware
467 * in order to work around limitations in Microsoft's driver.
468 * We need to make sure we only bind to one of them.
470 * This code assumes two things:
472 * 1. The PCI code adds subfunctions in order.
474 * 2. The MMC interface has a lower subfunction number
475 * than the SD interface.
477 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
478 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
479 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
480 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
482 if (mmcdev) {
483 struct pci_dev *sd_dev;
485 sd_dev = NULL;
486 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
487 mmcdev, sd_dev)) != NULL) {
488 if ((PCI_SLOT(chip->pdev->devfn) ==
489 PCI_SLOT(sd_dev->devfn)) &&
490 (chip->pdev->bus == sd_dev->bus))
491 break;
494 if (sd_dev) {
495 pci_dev_put(sd_dev);
496 dev_info(&chip->pdev->dev, "Refusing to bind to "
497 "secondary interface.\n");
498 return -ENODEV;
503 * JMicron chips need a bit of a nudge to enable the power
504 * output pins.
506 ret = jmicron_pmos(chip, 1);
507 if (ret) {
508 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
509 return ret;
512 /* quirk for unsable RO-detection on JM388 chips */
513 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
514 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
515 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
517 return 0;
520 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
522 u8 scratch;
524 scratch = readb(host->ioaddr + 0xC0);
526 if (on)
527 scratch |= 0x01;
528 else
529 scratch &= ~0x01;
531 writeb(scratch, host->ioaddr + 0xC0);
534 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
536 if (slot->chip->pdev->revision == 0) {
537 u16 version;
539 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
540 version = (version & SDHCI_VENDOR_VER_MASK) >>
541 SDHCI_VENDOR_VER_SHIFT;
544 * Older versions of the chip have lots of nasty glitches
545 * in the ADMA engine. It's best just to avoid it
546 * completely.
548 if (version < 0xAC)
549 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
552 /* JM388 MMC doesn't support 1.8V while SD supports it */
553 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
554 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
555 MMC_VDD_29_30 | MMC_VDD_30_31 |
556 MMC_VDD_165_195; /* allow 1.8V */
557 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
558 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
562 * The secondary interface requires a bit set to get the
563 * interrupts.
565 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
566 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
567 jmicron_enable_mmc(slot->host, 1);
569 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
571 return 0;
574 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
576 if (dead)
577 return;
579 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
580 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
581 jmicron_enable_mmc(slot->host, 0);
584 static int jmicron_suspend(struct sdhci_pci_chip *chip)
586 int i;
588 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
589 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
590 for (i = 0; i < chip->num_slots; i++)
591 jmicron_enable_mmc(chip->slots[i]->host, 0);
594 return 0;
597 static int jmicron_resume(struct sdhci_pci_chip *chip)
599 int ret, i;
601 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
602 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
603 for (i = 0; i < chip->num_slots; i++)
604 jmicron_enable_mmc(chip->slots[i]->host, 1);
607 ret = jmicron_pmos(chip, 1);
608 if (ret) {
609 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
610 return ret;
613 return 0;
616 static const struct sdhci_pci_fixes sdhci_o2 = {
617 .probe = o2_probe,
620 static const struct sdhci_pci_fixes sdhci_jmicron = {
621 .probe = jmicron_probe,
623 .probe_slot = jmicron_probe_slot,
624 .remove_slot = jmicron_remove_slot,
626 .suspend = jmicron_suspend,
627 .resume = jmicron_resume,
630 /* SysKonnect CardBus2SDIO extra registers */
631 #define SYSKT_CTRL 0x200
632 #define SYSKT_RDFIFO_STAT 0x204
633 #define SYSKT_WRFIFO_STAT 0x208
634 #define SYSKT_POWER_DATA 0x20c
635 #define SYSKT_POWER_330 0xef
636 #define SYSKT_POWER_300 0xf8
637 #define SYSKT_POWER_184 0xcc
638 #define SYSKT_POWER_CMD 0x20d
639 #define SYSKT_POWER_START (1 << 7)
640 #define SYSKT_POWER_STATUS 0x20e
641 #define SYSKT_POWER_STATUS_OK (1 << 0)
642 #define SYSKT_BOARD_REV 0x210
643 #define SYSKT_CHIP_REV 0x211
644 #define SYSKT_CONF_DATA 0x212
645 #define SYSKT_CONF_DATA_1V8 (1 << 2)
646 #define SYSKT_CONF_DATA_2V5 (1 << 1)
647 #define SYSKT_CONF_DATA_3V3 (1 << 0)
649 static int syskt_probe(struct sdhci_pci_chip *chip)
651 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
652 chip->pdev->class &= ~0x0000FF;
653 chip->pdev->class |= PCI_SDHCI_IFDMA;
655 return 0;
658 static int syskt_probe_slot(struct sdhci_pci_slot *slot)
660 int tm, ps;
662 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
663 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
664 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
665 "board rev %d.%d, chip rev %d.%d\n",
666 board_rev >> 4, board_rev & 0xf,
667 chip_rev >> 4, chip_rev & 0xf);
668 if (chip_rev >= 0x20)
669 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
671 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
672 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
673 udelay(50);
674 tm = 10; /* Wait max 1 ms */
675 do {
676 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
677 if (ps & SYSKT_POWER_STATUS_OK)
678 break;
679 udelay(100);
680 } while (--tm);
681 if (!tm) {
682 dev_err(&slot->chip->pdev->dev,
683 "power regulator never stabilized");
684 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
685 return -ENODEV;
688 return 0;
691 static const struct sdhci_pci_fixes sdhci_syskt = {
692 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
693 .probe = syskt_probe,
694 .probe_slot = syskt_probe_slot,
697 static int via_probe(struct sdhci_pci_chip *chip)
699 if (chip->pdev->revision == 0x10)
700 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
702 return 0;
705 static const struct sdhci_pci_fixes sdhci_via = {
706 .probe = via_probe,
709 static const struct pci_device_id pci_ids[] = {
711 .vendor = PCI_VENDOR_ID_RICOH,
712 .device = PCI_DEVICE_ID_RICOH_R5C822,
713 .subvendor = PCI_ANY_ID,
714 .subdevice = PCI_ANY_ID,
715 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
719 .vendor = PCI_VENDOR_ID_RICOH,
720 .device = 0x843,
721 .subvendor = PCI_ANY_ID,
722 .subdevice = PCI_ANY_ID,
723 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
727 .vendor = PCI_VENDOR_ID_RICOH,
728 .device = 0xe822,
729 .subvendor = PCI_ANY_ID,
730 .subdevice = PCI_ANY_ID,
731 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
735 .vendor = PCI_VENDOR_ID_RICOH,
736 .device = 0xe823,
737 .subvendor = PCI_ANY_ID,
738 .subdevice = PCI_ANY_ID,
739 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
743 .vendor = PCI_VENDOR_ID_ENE,
744 .device = PCI_DEVICE_ID_ENE_CB712_SD,
745 .subvendor = PCI_ANY_ID,
746 .subdevice = PCI_ANY_ID,
747 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
751 .vendor = PCI_VENDOR_ID_ENE,
752 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
753 .subvendor = PCI_ANY_ID,
754 .subdevice = PCI_ANY_ID,
755 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
759 .vendor = PCI_VENDOR_ID_ENE,
760 .device = PCI_DEVICE_ID_ENE_CB714_SD,
761 .subvendor = PCI_ANY_ID,
762 .subdevice = PCI_ANY_ID,
763 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
767 .vendor = PCI_VENDOR_ID_ENE,
768 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
769 .subvendor = PCI_ANY_ID,
770 .subdevice = PCI_ANY_ID,
771 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
775 .vendor = PCI_VENDOR_ID_MARVELL,
776 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
777 .subvendor = PCI_ANY_ID,
778 .subdevice = PCI_ANY_ID,
779 .driver_data = (kernel_ulong_t)&sdhci_cafe,
783 .vendor = PCI_VENDOR_ID_JMICRON,
784 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
785 .subvendor = PCI_ANY_ID,
786 .subdevice = PCI_ANY_ID,
787 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
791 .vendor = PCI_VENDOR_ID_JMICRON,
792 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
793 .subvendor = PCI_ANY_ID,
794 .subdevice = PCI_ANY_ID,
795 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
799 .vendor = PCI_VENDOR_ID_JMICRON,
800 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
801 .subvendor = PCI_ANY_ID,
802 .subdevice = PCI_ANY_ID,
803 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
807 .vendor = PCI_VENDOR_ID_JMICRON,
808 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
809 .subvendor = PCI_ANY_ID,
810 .subdevice = PCI_ANY_ID,
811 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
815 .vendor = PCI_VENDOR_ID_SYSKONNECT,
816 .device = 0x8000,
817 .subvendor = PCI_ANY_ID,
818 .subdevice = PCI_ANY_ID,
819 .driver_data = (kernel_ulong_t)&sdhci_syskt,
823 .vendor = PCI_VENDOR_ID_VIA,
824 .device = 0x95d0,
825 .subvendor = PCI_ANY_ID,
826 .subdevice = PCI_ANY_ID,
827 .driver_data = (kernel_ulong_t)&sdhci_via,
831 .vendor = PCI_VENDOR_ID_INTEL,
832 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
833 .subvendor = PCI_ANY_ID,
834 .subdevice = PCI_ANY_ID,
835 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
839 .vendor = PCI_VENDOR_ID_INTEL,
840 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
841 .subvendor = PCI_ANY_ID,
842 .subdevice = PCI_ANY_ID,
843 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
847 .vendor = PCI_VENDOR_ID_INTEL,
848 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
849 .subvendor = PCI_ANY_ID,
850 .subdevice = PCI_ANY_ID,
851 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
855 .vendor = PCI_VENDOR_ID_INTEL,
856 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
857 .subvendor = PCI_ANY_ID,
858 .subdevice = PCI_ANY_ID,
859 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
863 .vendor = PCI_VENDOR_ID_INTEL,
864 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
865 .subvendor = PCI_ANY_ID,
866 .subdevice = PCI_ANY_ID,
867 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
871 .vendor = PCI_VENDOR_ID_INTEL,
872 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
873 .subvendor = PCI_ANY_ID,
874 .subdevice = PCI_ANY_ID,
875 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
879 .vendor = PCI_VENDOR_ID_INTEL,
880 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
881 .subvendor = PCI_ANY_ID,
882 .subdevice = PCI_ANY_ID,
883 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
887 .vendor = PCI_VENDOR_ID_INTEL,
888 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
889 .subvendor = PCI_ANY_ID,
890 .subdevice = PCI_ANY_ID,
891 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
895 .vendor = PCI_VENDOR_ID_INTEL,
896 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
897 .subvendor = PCI_ANY_ID,
898 .subdevice = PCI_ANY_ID,
899 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
903 .vendor = PCI_VENDOR_ID_INTEL,
904 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
905 .subvendor = PCI_ANY_ID,
906 .subdevice = PCI_ANY_ID,
907 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
911 .vendor = PCI_VENDOR_ID_INTEL,
912 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
913 .subvendor = PCI_ANY_ID,
914 .subdevice = PCI_ANY_ID,
915 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
919 .vendor = PCI_VENDOR_ID_INTEL,
920 .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
921 .subvendor = PCI_ANY_ID,
922 .subdevice = PCI_ANY_ID,
923 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
927 .vendor = PCI_VENDOR_ID_INTEL,
928 .device = PCI_DEVICE_ID_INTEL_BYT_SD,
929 .subvendor = PCI_ANY_ID,
930 .subdevice = PCI_ANY_ID,
931 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
935 .vendor = PCI_VENDOR_ID_INTEL,
936 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC2,
937 .subvendor = PCI_ANY_ID,
938 .subdevice = PCI_ANY_ID,
939 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
943 .vendor = PCI_VENDOR_ID_O2,
944 .device = PCI_DEVICE_ID_O2_8120,
945 .subvendor = PCI_ANY_ID,
946 .subdevice = PCI_ANY_ID,
947 .driver_data = (kernel_ulong_t)&sdhci_o2,
951 .vendor = PCI_VENDOR_ID_O2,
952 .device = PCI_DEVICE_ID_O2_8220,
953 .subvendor = PCI_ANY_ID,
954 .subdevice = PCI_ANY_ID,
955 .driver_data = (kernel_ulong_t)&sdhci_o2,
959 .vendor = PCI_VENDOR_ID_O2,
960 .device = PCI_DEVICE_ID_O2_8221,
961 .subvendor = PCI_ANY_ID,
962 .subdevice = PCI_ANY_ID,
963 .driver_data = (kernel_ulong_t)&sdhci_o2,
967 .vendor = PCI_VENDOR_ID_O2,
968 .device = PCI_DEVICE_ID_O2_8320,
969 .subvendor = PCI_ANY_ID,
970 .subdevice = PCI_ANY_ID,
971 .driver_data = (kernel_ulong_t)&sdhci_o2,
975 .vendor = PCI_VENDOR_ID_O2,
976 .device = PCI_DEVICE_ID_O2_8321,
977 .subvendor = PCI_ANY_ID,
978 .subdevice = PCI_ANY_ID,
979 .driver_data = (kernel_ulong_t)&sdhci_o2,
982 { /* Generic SD host controller */
983 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
986 { /* end: all zeroes */ },
989 MODULE_DEVICE_TABLE(pci, pci_ids);
991 /*****************************************************************************\
993 * SDHCI core callbacks *
995 \*****************************************************************************/
997 static int sdhci_pci_enable_dma(struct sdhci_host *host)
999 struct sdhci_pci_slot *slot;
1000 struct pci_dev *pdev;
1001 int ret;
1003 slot = sdhci_priv(host);
1004 pdev = slot->chip->pdev;
1006 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
1007 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1008 (host->flags & SDHCI_USE_SDMA)) {
1009 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
1010 "doesn't fully claim to support it.\n");
1013 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1014 if (ret)
1015 return ret;
1017 pci_set_master(pdev);
1019 return 0;
1022 static int sdhci_pci_bus_width(struct sdhci_host *host, int width)
1024 u8 ctrl;
1026 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1028 switch (width) {
1029 case MMC_BUS_WIDTH_8:
1030 ctrl |= SDHCI_CTRL_8BITBUS;
1031 ctrl &= ~SDHCI_CTRL_4BITBUS;
1032 break;
1033 case MMC_BUS_WIDTH_4:
1034 ctrl |= SDHCI_CTRL_4BITBUS;
1035 ctrl &= ~SDHCI_CTRL_8BITBUS;
1036 break;
1037 default:
1038 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
1039 break;
1042 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1044 return 0;
1047 static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
1049 struct sdhci_pci_slot *slot = sdhci_priv(host);
1050 int rst_n_gpio = slot->rst_n_gpio;
1052 if (!gpio_is_valid(rst_n_gpio))
1053 return;
1054 gpio_set_value_cansleep(rst_n_gpio, 0);
1055 /* For eMMC, minimum is 1us but give it 10us for good measure */
1056 udelay(10);
1057 gpio_set_value_cansleep(rst_n_gpio, 1);
1058 /* For eMMC, minimum is 200us but give it 300us for good measure */
1059 usleep_range(300, 1000);
1062 static void sdhci_pci_hw_reset(struct sdhci_host *host)
1064 struct sdhci_pci_slot *slot = sdhci_priv(host);
1066 if (slot->hw_reset)
1067 slot->hw_reset(host);
1070 static const struct sdhci_ops sdhci_pci_ops = {
1071 .enable_dma = sdhci_pci_enable_dma,
1072 .platform_bus_width = sdhci_pci_bus_width,
1073 .hw_reset = sdhci_pci_hw_reset,
1076 /*****************************************************************************\
1078 * Suspend/resume *
1080 \*****************************************************************************/
1082 #ifdef CONFIG_PM
1084 static int sdhci_pci_suspend(struct device *dev)
1086 struct pci_dev *pdev = to_pci_dev(dev);
1087 struct sdhci_pci_chip *chip;
1088 struct sdhci_pci_slot *slot;
1089 mmc_pm_flag_t slot_pm_flags;
1090 mmc_pm_flag_t pm_flags = 0;
1091 int i, ret;
1093 chip = pci_get_drvdata(pdev);
1094 if (!chip)
1095 return 0;
1097 for (i = 0; i < chip->num_slots; i++) {
1098 slot = chip->slots[i];
1099 if (!slot)
1100 continue;
1102 ret = sdhci_suspend_host(slot->host);
1104 if (ret)
1105 goto err_pci_suspend;
1107 slot_pm_flags = slot->host->mmc->pm_flags;
1108 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1109 sdhci_enable_irq_wakeups(slot->host);
1111 pm_flags |= slot_pm_flags;
1114 if (chip->fixes && chip->fixes->suspend) {
1115 ret = chip->fixes->suspend(chip);
1116 if (ret)
1117 goto err_pci_suspend;
1120 pci_save_state(pdev);
1121 if (pm_flags & MMC_PM_KEEP_POWER) {
1122 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1123 pci_pme_active(pdev, true);
1124 pci_enable_wake(pdev, PCI_D3hot, 1);
1126 pci_set_power_state(pdev, PCI_D3hot);
1127 } else {
1128 pci_enable_wake(pdev, PCI_D3hot, 0);
1129 pci_disable_device(pdev);
1130 pci_set_power_state(pdev, PCI_D3hot);
1133 return 0;
1135 err_pci_suspend:
1136 while (--i >= 0)
1137 sdhci_resume_host(chip->slots[i]->host);
1138 return ret;
1141 static int sdhci_pci_resume(struct device *dev)
1143 struct pci_dev *pdev = to_pci_dev(dev);
1144 struct sdhci_pci_chip *chip;
1145 struct sdhci_pci_slot *slot;
1146 int i, ret;
1148 chip = pci_get_drvdata(pdev);
1149 if (!chip)
1150 return 0;
1152 pci_set_power_state(pdev, PCI_D0);
1153 pci_restore_state(pdev);
1154 ret = pci_enable_device(pdev);
1155 if (ret)
1156 return ret;
1158 if (chip->fixes && chip->fixes->resume) {
1159 ret = chip->fixes->resume(chip);
1160 if (ret)
1161 return ret;
1164 for (i = 0; i < chip->num_slots; i++) {
1165 slot = chip->slots[i];
1166 if (!slot)
1167 continue;
1169 ret = sdhci_resume_host(slot->host);
1170 if (ret)
1171 return ret;
1174 return 0;
1177 #else /* CONFIG_PM */
1179 #define sdhci_pci_suspend NULL
1180 #define sdhci_pci_resume NULL
1182 #endif /* CONFIG_PM */
1184 #ifdef CONFIG_PM_RUNTIME
1186 static int sdhci_pci_runtime_suspend(struct device *dev)
1188 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1189 struct sdhci_pci_chip *chip;
1190 struct sdhci_pci_slot *slot;
1191 int i, ret;
1193 chip = pci_get_drvdata(pdev);
1194 if (!chip)
1195 return 0;
1197 for (i = 0; i < chip->num_slots; i++) {
1198 slot = chip->slots[i];
1199 if (!slot)
1200 continue;
1202 ret = sdhci_runtime_suspend_host(slot->host);
1204 if (ret)
1205 goto err_pci_runtime_suspend;
1208 if (chip->fixes && chip->fixes->suspend) {
1209 ret = chip->fixes->suspend(chip);
1210 if (ret)
1211 goto err_pci_runtime_suspend;
1214 return 0;
1216 err_pci_runtime_suspend:
1217 while (--i >= 0)
1218 sdhci_runtime_resume_host(chip->slots[i]->host);
1219 return ret;
1222 static int sdhci_pci_runtime_resume(struct device *dev)
1224 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1225 struct sdhci_pci_chip *chip;
1226 struct sdhci_pci_slot *slot;
1227 int i, ret;
1229 chip = pci_get_drvdata(pdev);
1230 if (!chip)
1231 return 0;
1233 if (chip->fixes && chip->fixes->resume) {
1234 ret = chip->fixes->resume(chip);
1235 if (ret)
1236 return ret;
1239 for (i = 0; i < chip->num_slots; i++) {
1240 slot = chip->slots[i];
1241 if (!slot)
1242 continue;
1244 ret = sdhci_runtime_resume_host(slot->host);
1245 if (ret)
1246 return ret;
1249 return 0;
1252 static int sdhci_pci_runtime_idle(struct device *dev)
1254 return 0;
1257 #else
1259 #define sdhci_pci_runtime_suspend NULL
1260 #define sdhci_pci_runtime_resume NULL
1261 #define sdhci_pci_runtime_idle NULL
1263 #endif
1265 static const struct dev_pm_ops sdhci_pci_pm_ops = {
1266 .suspend = sdhci_pci_suspend,
1267 .resume = sdhci_pci_resume,
1268 .runtime_suspend = sdhci_pci_runtime_suspend,
1269 .runtime_resume = sdhci_pci_runtime_resume,
1270 .runtime_idle = sdhci_pci_runtime_idle,
1273 /*****************************************************************************\
1275 * Device probing/removal *
1277 \*****************************************************************************/
1279 static struct sdhci_pci_slot *sdhci_pci_probe_slot(
1280 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1281 int slotno)
1283 struct sdhci_pci_slot *slot;
1284 struct sdhci_host *host;
1285 int ret, bar = first_bar + slotno;
1287 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1288 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1289 return ERR_PTR(-ENODEV);
1292 if (pci_resource_len(pdev, bar) < 0x100) {
1293 dev_err(&pdev->dev, "Invalid iomem size. You may "
1294 "experience problems.\n");
1297 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1298 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1299 return ERR_PTR(-ENODEV);
1302 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1303 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1304 return ERR_PTR(-ENODEV);
1307 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1308 if (IS_ERR(host)) {
1309 dev_err(&pdev->dev, "cannot allocate host\n");
1310 return ERR_CAST(host);
1313 slot = sdhci_priv(host);
1315 slot->chip = chip;
1316 slot->host = host;
1317 slot->pci_bar = bar;
1318 slot->rst_n_gpio = -EINVAL;
1319 slot->cd_gpio = -EINVAL;
1321 /* Retrieve platform data if there is any */
1322 if (*sdhci_pci_get_data)
1323 slot->data = sdhci_pci_get_data(pdev, slotno);
1325 if (slot->data) {
1326 if (slot->data->setup) {
1327 ret = slot->data->setup(slot->data);
1328 if (ret) {
1329 dev_err(&pdev->dev, "platform setup failed\n");
1330 goto free;
1333 slot->rst_n_gpio = slot->data->rst_n_gpio;
1334 slot->cd_gpio = slot->data->cd_gpio;
1337 host->hw_name = "PCI";
1338 host->ops = &sdhci_pci_ops;
1339 host->quirks = chip->quirks;
1340 host->quirks2 = chip->quirks2;
1342 host->irq = pdev->irq;
1344 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1345 if (ret) {
1346 dev_err(&pdev->dev, "cannot request region\n");
1347 goto cleanup;
1350 host->ioaddr = pci_ioremap_bar(pdev, bar);
1351 if (!host->ioaddr) {
1352 dev_err(&pdev->dev, "failed to remap registers\n");
1353 ret = -ENOMEM;
1354 goto release;
1357 if (chip->fixes && chip->fixes->probe_slot) {
1358 ret = chip->fixes->probe_slot(slot);
1359 if (ret)
1360 goto unmap;
1363 if (gpio_is_valid(slot->rst_n_gpio)) {
1364 if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
1365 gpio_direction_output(slot->rst_n_gpio, 1);
1366 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
1367 slot->hw_reset = sdhci_pci_gpio_hw_reset;
1368 } else {
1369 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
1370 slot->rst_n_gpio = -EINVAL;
1374 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1375 host->mmc->slotno = slotno;
1376 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
1378 ret = sdhci_add_host(host);
1379 if (ret)
1380 goto remove;
1382 sdhci_pci_add_own_cd(slot);
1384 return slot;
1386 remove:
1387 if (gpio_is_valid(slot->rst_n_gpio))
1388 gpio_free(slot->rst_n_gpio);
1390 if (chip->fixes && chip->fixes->remove_slot)
1391 chip->fixes->remove_slot(slot, 0);
1393 unmap:
1394 iounmap(host->ioaddr);
1396 release:
1397 pci_release_region(pdev, bar);
1399 cleanup:
1400 if (slot->data && slot->data->cleanup)
1401 slot->data->cleanup(slot->data);
1403 free:
1404 sdhci_free_host(host);
1406 return ERR_PTR(ret);
1409 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1411 int dead;
1412 u32 scratch;
1414 sdhci_pci_remove_own_cd(slot);
1416 dead = 0;
1417 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1418 if (scratch == (u32)-1)
1419 dead = 1;
1421 sdhci_remove_host(slot->host, dead);
1423 if (gpio_is_valid(slot->rst_n_gpio))
1424 gpio_free(slot->rst_n_gpio);
1426 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
1427 slot->chip->fixes->remove_slot(slot, dead);
1429 if (slot->data && slot->data->cleanup)
1430 slot->data->cleanup(slot->data);
1432 pci_release_region(slot->chip->pdev, slot->pci_bar);
1434 sdhci_free_host(slot->host);
1437 static void sdhci_pci_runtime_pm_allow(struct device *dev)
1439 pm_runtime_put_noidle(dev);
1440 pm_runtime_allow(dev);
1441 pm_runtime_set_autosuspend_delay(dev, 50);
1442 pm_runtime_use_autosuspend(dev);
1443 pm_suspend_ignore_children(dev, 1);
1446 static void sdhci_pci_runtime_pm_forbid(struct device *dev)
1448 pm_runtime_forbid(dev);
1449 pm_runtime_get_noresume(dev);
1452 static int sdhci_pci_probe(struct pci_dev *pdev,
1453 const struct pci_device_id *ent)
1455 struct sdhci_pci_chip *chip;
1456 struct sdhci_pci_slot *slot;
1458 u8 slots, first_bar;
1459 int ret, i;
1461 BUG_ON(pdev == NULL);
1462 BUG_ON(ent == NULL);
1464 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
1465 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
1467 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1468 if (ret)
1469 return ret;
1471 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1472 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1473 if (slots == 0)
1474 return -ENODEV;
1476 BUG_ON(slots > MAX_SLOTS);
1478 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1479 if (ret)
1480 return ret;
1482 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1484 if (first_bar > 5) {
1485 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1486 return -ENODEV;
1489 ret = pci_enable_device(pdev);
1490 if (ret)
1491 return ret;
1493 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1494 if (!chip) {
1495 ret = -ENOMEM;
1496 goto err;
1499 chip->pdev = pdev;
1500 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
1501 if (chip->fixes) {
1502 chip->quirks = chip->fixes->quirks;
1503 chip->quirks2 = chip->fixes->quirks2;
1504 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1506 chip->num_slots = slots;
1508 pci_set_drvdata(pdev, chip);
1510 if (chip->fixes && chip->fixes->probe) {
1511 ret = chip->fixes->probe(chip);
1512 if (ret)
1513 goto free;
1516 slots = chip->num_slots; /* Quirk may have changed this */
1518 for (i = 0; i < slots; i++) {
1519 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
1520 if (IS_ERR(slot)) {
1521 for (i--; i >= 0; i--)
1522 sdhci_pci_remove_slot(chip->slots[i]);
1523 ret = PTR_ERR(slot);
1524 goto free;
1527 chip->slots[i] = slot;
1530 if (chip->allow_runtime_pm)
1531 sdhci_pci_runtime_pm_allow(&pdev->dev);
1533 return 0;
1535 free:
1536 pci_set_drvdata(pdev, NULL);
1537 kfree(chip);
1539 err:
1540 pci_disable_device(pdev);
1541 return ret;
1544 static void sdhci_pci_remove(struct pci_dev *pdev)
1546 int i;
1547 struct sdhci_pci_chip *chip;
1549 chip = pci_get_drvdata(pdev);
1551 if (chip) {
1552 if (chip->allow_runtime_pm)
1553 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1555 for (i = 0; i < chip->num_slots; i++)
1556 sdhci_pci_remove_slot(chip->slots[i]);
1558 pci_set_drvdata(pdev, NULL);
1559 kfree(chip);
1562 pci_disable_device(pdev);
1565 static struct pci_driver sdhci_driver = {
1566 .name = "sdhci-pci",
1567 .id_table = pci_ids,
1568 .probe = sdhci_pci_probe,
1569 .remove = sdhci_pci_remove,
1570 .driver = {
1571 .pm = &sdhci_pci_pm_ops
1575 module_pci_driver(sdhci_driver);
1577 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
1578 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1579 MODULE_LICENSE("GPL");