Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-pci-core.c
blob9552708846ca3742918bd352b7bade37b02f6471
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
4 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
6 * Thanks to the following companies for their support:
8 * - JMicron (hardware and technical support)
9 */
11 #include <linux/bitfield.h>
12 #include <linux/string.h>
13 #include <linux/delay.h>
14 #include <linux/highmem.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/scatterlist.h>
23 #include <linux/io.h>
24 #include <linux/iopoll.h>
25 #include <linux/gpio.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pm_qos.h>
28 #include <linux/debugfs.h>
29 #include <linux/mmc/slot-gpio.h>
30 #include <linux/mmc/sdhci-pci-data.h>
31 #include <linux/acpi.h>
32 #include <linux/dmi.h>
34 #ifdef CONFIG_X86
35 #include <asm/iosf_mbi.h>
36 #endif
38 #include "cqhci.h"
40 #include "sdhci.h"
41 #include "sdhci-pci.h"
43 static void sdhci_pci_hw_reset(struct sdhci_host *host);
45 #ifdef CONFIG_PM_SLEEP
46 static int sdhci_pci_init_wakeup(struct sdhci_pci_chip *chip)
48 mmc_pm_flag_t pm_flags = 0;
49 bool cap_cd_wake = false;
50 int i;
52 for (i = 0; i < chip->num_slots; i++) {
53 struct sdhci_pci_slot *slot = chip->slots[i];
55 if (slot) {
56 pm_flags |= slot->host->mmc->pm_flags;
57 if (slot->host->mmc->caps & MMC_CAP_CD_WAKE)
58 cap_cd_wake = true;
62 if ((pm_flags & MMC_PM_KEEP_POWER) && (pm_flags & MMC_PM_WAKE_SDIO_IRQ))
63 return device_wakeup_enable(&chip->pdev->dev);
64 else if (!cap_cd_wake)
65 return device_wakeup_disable(&chip->pdev->dev);
67 return 0;
70 static int sdhci_pci_suspend_host(struct sdhci_pci_chip *chip)
72 int i, ret;
74 sdhci_pci_init_wakeup(chip);
76 for (i = 0; i < chip->num_slots; i++) {
77 struct sdhci_pci_slot *slot = chip->slots[i];
78 struct sdhci_host *host;
80 if (!slot)
81 continue;
83 host = slot->host;
85 if (chip->pm_retune && host->tuning_mode != SDHCI_TUNING_MODE_3)
86 mmc_retune_needed(host->mmc);
88 ret = sdhci_suspend_host(host);
89 if (ret)
90 goto err_pci_suspend;
92 if (device_may_wakeup(&chip->pdev->dev))
93 mmc_gpio_set_cd_wake(host->mmc, true);
96 return 0;
98 err_pci_suspend:
99 while (--i >= 0)
100 sdhci_resume_host(chip->slots[i]->host);
101 return ret;
104 int sdhci_pci_resume_host(struct sdhci_pci_chip *chip)
106 struct sdhci_pci_slot *slot;
107 int i, ret;
109 for (i = 0; i < chip->num_slots; i++) {
110 slot = chip->slots[i];
111 if (!slot)
112 continue;
114 ret = sdhci_resume_host(slot->host);
115 if (ret)
116 return ret;
118 mmc_gpio_set_cd_wake(slot->host->mmc, false);
121 return 0;
124 static int sdhci_cqhci_suspend(struct sdhci_pci_chip *chip)
126 int ret;
128 ret = cqhci_suspend(chip->slots[0]->host->mmc);
129 if (ret)
130 return ret;
132 return sdhci_pci_suspend_host(chip);
135 static int sdhci_cqhci_resume(struct sdhci_pci_chip *chip)
137 int ret;
139 ret = sdhci_pci_resume_host(chip);
140 if (ret)
141 return ret;
143 return cqhci_resume(chip->slots[0]->host->mmc);
145 #endif
147 #ifdef CONFIG_PM
148 static int sdhci_pci_runtime_suspend_host(struct sdhci_pci_chip *chip)
150 struct sdhci_pci_slot *slot;
151 struct sdhci_host *host;
152 int i, ret;
154 for (i = 0; i < chip->num_slots; i++) {
155 slot = chip->slots[i];
156 if (!slot)
157 continue;
159 host = slot->host;
161 ret = sdhci_runtime_suspend_host(host);
162 if (ret)
163 goto err_pci_runtime_suspend;
165 if (chip->rpm_retune &&
166 host->tuning_mode != SDHCI_TUNING_MODE_3)
167 mmc_retune_needed(host->mmc);
170 return 0;
172 err_pci_runtime_suspend:
173 while (--i >= 0)
174 sdhci_runtime_resume_host(chip->slots[i]->host, 0);
175 return ret;
178 static int sdhci_pci_runtime_resume_host(struct sdhci_pci_chip *chip)
180 struct sdhci_pci_slot *slot;
181 int i, ret;
183 for (i = 0; i < chip->num_slots; i++) {
184 slot = chip->slots[i];
185 if (!slot)
186 continue;
188 ret = sdhci_runtime_resume_host(slot->host, 0);
189 if (ret)
190 return ret;
193 return 0;
196 static int sdhci_cqhci_runtime_suspend(struct sdhci_pci_chip *chip)
198 int ret;
200 ret = cqhci_suspend(chip->slots[0]->host->mmc);
201 if (ret)
202 return ret;
204 return sdhci_pci_runtime_suspend_host(chip);
207 static int sdhci_cqhci_runtime_resume(struct sdhci_pci_chip *chip)
209 int ret;
211 ret = sdhci_pci_runtime_resume_host(chip);
212 if (ret)
213 return ret;
215 return cqhci_resume(chip->slots[0]->host->mmc);
217 #endif
219 static u32 sdhci_cqhci_irq(struct sdhci_host *host, u32 intmask)
221 int cmd_error = 0;
222 int data_error = 0;
224 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
225 return intmask;
227 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
229 return 0;
232 static void sdhci_pci_dumpregs(struct mmc_host *mmc)
234 sdhci_dumpregs(mmc_priv(mmc));
237 static void sdhci_cqhci_reset(struct sdhci_host *host, u8 mask)
239 if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL) &&
240 host->mmc->cqe_private)
241 cqhci_deactivate(host->mmc);
242 sdhci_reset(host, mask);
245 /*****************************************************************************\
247 * Hardware specific quirk handling *
249 \*****************************************************************************/
251 static int ricoh_probe(struct sdhci_pci_chip *chip)
253 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
254 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
255 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
256 return 0;
259 static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
261 slot->host->caps =
262 FIELD_PREP(SDHCI_TIMEOUT_CLK_MASK, 0x21) |
263 FIELD_PREP(SDHCI_CLOCK_BASE_MASK, 0x21) |
264 SDHCI_TIMEOUT_CLK_UNIT |
265 SDHCI_CAN_VDD_330 |
266 SDHCI_CAN_DO_HISPD |
267 SDHCI_CAN_DO_SDMA;
268 return 0;
271 #ifdef CONFIG_PM_SLEEP
272 static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
274 /* Apply a delay to allow controller to settle */
275 /* Otherwise it becomes confused if card state changed
276 during suspend */
277 msleep(500);
278 return sdhci_pci_resume_host(chip);
280 #endif
282 static const struct sdhci_pci_fixes sdhci_ricoh = {
283 .probe = ricoh_probe,
284 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
285 SDHCI_QUIRK_FORCE_DMA |
286 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
289 static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
290 .probe_slot = ricoh_mmc_probe_slot,
291 #ifdef CONFIG_PM_SLEEP
292 .resume = ricoh_mmc_resume,
293 #endif
294 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
295 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
296 SDHCI_QUIRK_NO_CARD_NO_RESET |
297 SDHCI_QUIRK_MISSING_CAPS
300 static const struct sdhci_pci_fixes sdhci_ene_712 = {
301 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
302 SDHCI_QUIRK_BROKEN_DMA,
305 static const struct sdhci_pci_fixes sdhci_ene_714 = {
306 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
307 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
308 SDHCI_QUIRK_BROKEN_DMA,
311 static const struct sdhci_pci_fixes sdhci_cafe = {
312 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
313 SDHCI_QUIRK_NO_BUSY_IRQ |
314 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
315 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
318 static const struct sdhci_pci_fixes sdhci_intel_qrk = {
319 .quirks = SDHCI_QUIRK_NO_HISPD_BIT,
322 static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
324 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
325 return 0;
329 * ADMA operation is disabled for Moorestown platform due to
330 * hardware bugs.
332 static int mrst_hc_probe(struct sdhci_pci_chip *chip)
335 * slots number is fixed here for MRST as SDIO3/5 are never used and
336 * have hardware bugs.
338 chip->num_slots = 1;
339 return 0;
342 static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
344 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
345 return 0;
348 #ifdef CONFIG_PM
350 static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
352 struct sdhci_pci_slot *slot = dev_id;
353 struct sdhci_host *host = slot->host;
355 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
356 return IRQ_HANDLED;
359 static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
361 int err, irq, gpio = slot->cd_gpio;
363 slot->cd_gpio = -EINVAL;
364 slot->cd_irq = -EINVAL;
366 if (!gpio_is_valid(gpio))
367 return;
369 err = devm_gpio_request(&slot->chip->pdev->dev, gpio, "sd_cd");
370 if (err < 0)
371 goto out;
373 err = gpio_direction_input(gpio);
374 if (err < 0)
375 goto out_free;
377 irq = gpio_to_irq(gpio);
378 if (irq < 0)
379 goto out_free;
381 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
382 IRQF_TRIGGER_FALLING, "sd_cd", slot);
383 if (err)
384 goto out_free;
386 slot->cd_gpio = gpio;
387 slot->cd_irq = irq;
389 return;
391 out_free:
392 devm_gpio_free(&slot->chip->pdev->dev, gpio);
393 out:
394 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
397 static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
399 if (slot->cd_irq >= 0)
400 free_irq(slot->cd_irq, slot);
403 #else
405 static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
409 static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
413 #endif
415 static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
417 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
418 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
419 return 0;
422 static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
424 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
425 return 0;
428 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
429 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
430 .probe_slot = mrst_hc_probe_slot,
433 static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
434 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
435 .probe = mrst_hc_probe,
438 static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
439 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
440 .allow_runtime_pm = true,
441 .own_cd_for_runtime_pm = true,
444 static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
445 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
446 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
447 .allow_runtime_pm = true,
448 .probe_slot = mfd_sdio_probe_slot,
451 static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
452 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
453 .allow_runtime_pm = true,
454 .probe_slot = mfd_emmc_probe_slot,
457 static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
458 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
459 .probe_slot = pch_hc_probe_slot,
462 #ifdef CONFIG_X86
464 #define BYT_IOSF_SCCEP 0x63
465 #define BYT_IOSF_OCP_NETCTRL0 0x1078
466 #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
468 static void byt_ocp_setting(struct pci_dev *pdev)
470 u32 val = 0;
472 if (pdev->device != PCI_DEVICE_ID_INTEL_BYT_EMMC &&
473 pdev->device != PCI_DEVICE_ID_INTEL_BYT_SDIO &&
474 pdev->device != PCI_DEVICE_ID_INTEL_BYT_SD &&
475 pdev->device != PCI_DEVICE_ID_INTEL_BYT_EMMC2)
476 return;
478 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
479 &val)) {
480 dev_err(&pdev->dev, "%s read error\n", __func__);
481 return;
484 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
485 return;
487 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
489 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
490 val)) {
491 dev_err(&pdev->dev, "%s write error\n", __func__);
492 return;
495 dev_dbg(&pdev->dev, "%s completed\n", __func__);
498 #else
500 static inline void byt_ocp_setting(struct pci_dev *pdev)
504 #endif
506 enum {
507 INTEL_DSM_FNS = 0,
508 INTEL_DSM_V18_SWITCH = 3,
509 INTEL_DSM_V33_SWITCH = 4,
510 INTEL_DSM_DRV_STRENGTH = 9,
511 INTEL_DSM_D3_RETUNE = 10,
514 struct intel_host {
515 u32 dsm_fns;
516 int drv_strength;
517 bool d3_retune;
518 bool rpm_retune_ok;
519 u32 glk_rx_ctrl1;
520 u32 glk_tun_val;
521 u32 active_ltr;
522 u32 idle_ltr;
525 static const guid_t intel_dsm_guid =
526 GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
527 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
529 static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
530 unsigned int fn, u32 *result)
532 union acpi_object *obj;
533 int err = 0;
534 size_t len;
536 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
537 if (!obj)
538 return -EOPNOTSUPP;
540 if (obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 1) {
541 err = -EINVAL;
542 goto out;
545 len = min_t(size_t, obj->buffer.length, 4);
547 *result = 0;
548 memcpy(result, obj->buffer.pointer, len);
549 out:
550 ACPI_FREE(obj);
552 return err;
555 static int intel_dsm(struct intel_host *intel_host, struct device *dev,
556 unsigned int fn, u32 *result)
558 if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
559 return -EOPNOTSUPP;
561 return __intel_dsm(intel_host, dev, fn, result);
564 static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
565 struct mmc_host *mmc)
567 int err;
568 u32 val;
570 intel_host->d3_retune = true;
572 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
573 if (err) {
574 pr_debug("%s: DSM not supported, error %d\n",
575 mmc_hostname(mmc), err);
576 return;
579 pr_debug("%s: DSM function mask %#x\n",
580 mmc_hostname(mmc), intel_host->dsm_fns);
582 err = intel_dsm(intel_host, dev, INTEL_DSM_DRV_STRENGTH, &val);
583 intel_host->drv_strength = err ? 0 : val;
585 err = intel_dsm(intel_host, dev, INTEL_DSM_D3_RETUNE, &val);
586 intel_host->d3_retune = err ? true : !!val;
589 static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
591 u8 reg;
593 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
594 reg |= 0x10;
595 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
596 /* For eMMC, minimum is 1us but give it 9us for good measure */
597 udelay(9);
598 reg &= ~0x10;
599 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
600 /* For eMMC, minimum is 200us but give it 300us for good measure */
601 usleep_range(300, 1000);
604 static int intel_select_drive_strength(struct mmc_card *card,
605 unsigned int max_dtr, int host_drv,
606 int card_drv, int *drv_type)
608 struct sdhci_host *host = mmc_priv(card->host);
609 struct sdhci_pci_slot *slot = sdhci_priv(host);
610 struct intel_host *intel_host = sdhci_pci_priv(slot);
612 if (!(mmc_driver_type_mask(intel_host->drv_strength) & card_drv))
613 return 0;
615 return intel_host->drv_strength;
618 static int bxt_get_cd(struct mmc_host *mmc)
620 int gpio_cd = mmc_gpio_get_cd(mmc);
621 struct sdhci_host *host = mmc_priv(mmc);
622 unsigned long flags;
623 int ret = 0;
625 if (!gpio_cd)
626 return 0;
628 spin_lock_irqsave(&host->lock, flags);
630 if (host->flags & SDHCI_DEVICE_DEAD)
631 goto out;
633 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
634 out:
635 spin_unlock_irqrestore(&host->lock, flags);
637 return ret;
640 #define SDHCI_INTEL_PWR_TIMEOUT_CNT 20
641 #define SDHCI_INTEL_PWR_TIMEOUT_UDELAY 100
643 static void sdhci_intel_set_power(struct sdhci_host *host, unsigned char mode,
644 unsigned short vdd)
646 int cntr;
647 u8 reg;
649 sdhci_set_power(host, mode, vdd);
651 if (mode == MMC_POWER_OFF)
652 return;
655 * Bus power might not enable after D3 -> D0 transition due to the
656 * present state not yet having propagated. Retry for up to 2ms.
658 for (cntr = 0; cntr < SDHCI_INTEL_PWR_TIMEOUT_CNT; cntr++) {
659 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
660 if (reg & SDHCI_POWER_ON)
661 break;
662 udelay(SDHCI_INTEL_PWR_TIMEOUT_UDELAY);
663 reg |= SDHCI_POWER_ON;
664 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
668 static void sdhci_intel_set_uhs_signaling(struct sdhci_host *host,
669 unsigned int timing)
671 /* Set UHS timing to SDR25 for High Speed mode */
672 if (timing == MMC_TIMING_MMC_HS || timing == MMC_TIMING_SD_HS)
673 timing = MMC_TIMING_UHS_SDR25;
674 sdhci_set_uhs_signaling(host, timing);
677 #define INTEL_HS400_ES_REG 0x78
678 #define INTEL_HS400_ES_BIT BIT(0)
680 static void intel_hs400_enhanced_strobe(struct mmc_host *mmc,
681 struct mmc_ios *ios)
683 struct sdhci_host *host = mmc_priv(mmc);
684 u32 val;
686 val = sdhci_readl(host, INTEL_HS400_ES_REG);
687 if (ios->enhanced_strobe)
688 val |= INTEL_HS400_ES_BIT;
689 else
690 val &= ~INTEL_HS400_ES_BIT;
691 sdhci_writel(host, val, INTEL_HS400_ES_REG);
694 static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
695 struct mmc_ios *ios)
697 struct device *dev = mmc_dev(mmc);
698 struct sdhci_host *host = mmc_priv(mmc);
699 struct sdhci_pci_slot *slot = sdhci_priv(host);
700 struct intel_host *intel_host = sdhci_pci_priv(slot);
701 unsigned int fn;
702 u32 result = 0;
703 int err;
705 err = sdhci_start_signal_voltage_switch(mmc, ios);
706 if (err)
707 return err;
709 switch (ios->signal_voltage) {
710 case MMC_SIGNAL_VOLTAGE_330:
711 fn = INTEL_DSM_V33_SWITCH;
712 break;
713 case MMC_SIGNAL_VOLTAGE_180:
714 fn = INTEL_DSM_V18_SWITCH;
715 break;
716 default:
717 return 0;
720 err = intel_dsm(intel_host, dev, fn, &result);
721 pr_debug("%s: %s DSM fn %u error %d result %u\n",
722 mmc_hostname(mmc), __func__, fn, err, result);
724 return 0;
727 static const struct sdhci_ops sdhci_intel_byt_ops = {
728 .set_clock = sdhci_set_clock,
729 .set_power = sdhci_intel_set_power,
730 .enable_dma = sdhci_pci_enable_dma,
731 .set_bus_width = sdhci_set_bus_width,
732 .reset = sdhci_reset,
733 .set_uhs_signaling = sdhci_intel_set_uhs_signaling,
734 .hw_reset = sdhci_pci_hw_reset,
737 static const struct sdhci_ops sdhci_intel_glk_ops = {
738 .set_clock = sdhci_set_clock,
739 .set_power = sdhci_intel_set_power,
740 .enable_dma = sdhci_pci_enable_dma,
741 .set_bus_width = sdhci_set_bus_width,
742 .reset = sdhci_cqhci_reset,
743 .set_uhs_signaling = sdhci_intel_set_uhs_signaling,
744 .hw_reset = sdhci_pci_hw_reset,
745 .irq = sdhci_cqhci_irq,
748 static void byt_read_dsm(struct sdhci_pci_slot *slot)
750 struct intel_host *intel_host = sdhci_pci_priv(slot);
751 struct device *dev = &slot->chip->pdev->dev;
752 struct mmc_host *mmc = slot->host->mmc;
754 intel_dsm_init(intel_host, dev, mmc);
755 slot->chip->rpm_retune = intel_host->d3_retune;
758 static int intel_execute_tuning(struct mmc_host *mmc, u32 opcode)
760 int err = sdhci_execute_tuning(mmc, opcode);
761 struct sdhci_host *host = mmc_priv(mmc);
763 if (err)
764 return err;
767 * Tuning can leave the IP in an active state (Buffer Read Enable bit
768 * set) which prevents the entry to low power states (i.e. S0i3). Data
769 * reset will clear it.
771 sdhci_reset(host, SDHCI_RESET_DATA);
773 return 0;
776 #define INTEL_ACTIVELTR 0x804
777 #define INTEL_IDLELTR 0x808
779 #define INTEL_LTR_REQ BIT(15)
780 #define INTEL_LTR_SCALE_MASK GENMASK(11, 10)
781 #define INTEL_LTR_SCALE_1US (2 << 10)
782 #define INTEL_LTR_SCALE_32US (3 << 10)
783 #define INTEL_LTR_VALUE_MASK GENMASK(9, 0)
785 static void intel_cache_ltr(struct sdhci_pci_slot *slot)
787 struct intel_host *intel_host = sdhci_pci_priv(slot);
788 struct sdhci_host *host = slot->host;
790 intel_host->active_ltr = readl(host->ioaddr + INTEL_ACTIVELTR);
791 intel_host->idle_ltr = readl(host->ioaddr + INTEL_IDLELTR);
794 static void intel_ltr_set(struct device *dev, s32 val)
796 struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
797 struct sdhci_pci_slot *slot = chip->slots[0];
798 struct intel_host *intel_host = sdhci_pci_priv(slot);
799 struct sdhci_host *host = slot->host;
800 u32 ltr;
802 pm_runtime_get_sync(dev);
805 * Program latency tolerance (LTR) accordingly what has been asked
806 * by the PM QoS layer or disable it in case we were passed
807 * negative value or PM_QOS_LATENCY_ANY.
809 ltr = readl(host->ioaddr + INTEL_ACTIVELTR);
811 if (val == PM_QOS_LATENCY_ANY || val < 0) {
812 ltr &= ~INTEL_LTR_REQ;
813 } else {
814 ltr |= INTEL_LTR_REQ;
815 ltr &= ~INTEL_LTR_SCALE_MASK;
816 ltr &= ~INTEL_LTR_VALUE_MASK;
818 if (val > INTEL_LTR_VALUE_MASK) {
819 val >>= 5;
820 if (val > INTEL_LTR_VALUE_MASK)
821 val = INTEL_LTR_VALUE_MASK;
822 ltr |= INTEL_LTR_SCALE_32US | val;
823 } else {
824 ltr |= INTEL_LTR_SCALE_1US | val;
828 if (ltr == intel_host->active_ltr)
829 goto out;
831 writel(ltr, host->ioaddr + INTEL_ACTIVELTR);
832 writel(ltr, host->ioaddr + INTEL_IDLELTR);
834 /* Cache the values into lpss structure */
835 intel_cache_ltr(slot);
836 out:
837 pm_runtime_put_autosuspend(dev);
840 static bool intel_use_ltr(struct sdhci_pci_chip *chip)
842 switch (chip->pdev->device) {
843 case PCI_DEVICE_ID_INTEL_BYT_EMMC:
844 case PCI_DEVICE_ID_INTEL_BYT_EMMC2:
845 case PCI_DEVICE_ID_INTEL_BYT_SDIO:
846 case PCI_DEVICE_ID_INTEL_BYT_SD:
847 case PCI_DEVICE_ID_INTEL_BSW_EMMC:
848 case PCI_DEVICE_ID_INTEL_BSW_SDIO:
849 case PCI_DEVICE_ID_INTEL_BSW_SD:
850 return false;
851 default:
852 return true;
856 static void intel_ltr_expose(struct sdhci_pci_chip *chip)
858 struct device *dev = &chip->pdev->dev;
860 if (!intel_use_ltr(chip))
861 return;
863 dev->power.set_latency_tolerance = intel_ltr_set;
864 dev_pm_qos_expose_latency_tolerance(dev);
867 static void intel_ltr_hide(struct sdhci_pci_chip *chip)
869 struct device *dev = &chip->pdev->dev;
871 if (!intel_use_ltr(chip))
872 return;
874 dev_pm_qos_hide_latency_tolerance(dev);
875 dev->power.set_latency_tolerance = NULL;
878 static void byt_probe_slot(struct sdhci_pci_slot *slot)
880 struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
881 struct device *dev = &slot->chip->pdev->dev;
882 struct mmc_host *mmc = slot->host->mmc;
884 byt_read_dsm(slot);
886 byt_ocp_setting(slot->chip->pdev);
888 ops->execute_tuning = intel_execute_tuning;
889 ops->start_signal_voltage_switch = intel_start_signal_voltage_switch;
891 device_property_read_u32(dev, "max-frequency", &mmc->f_max);
893 if (!mmc->slotno) {
894 slot->chip->slots[mmc->slotno] = slot;
895 intel_ltr_expose(slot->chip);
899 static void byt_add_debugfs(struct sdhci_pci_slot *slot)
901 struct intel_host *intel_host = sdhci_pci_priv(slot);
902 struct mmc_host *mmc = slot->host->mmc;
903 struct dentry *dir = mmc->debugfs_root;
905 if (!intel_use_ltr(slot->chip))
906 return;
908 debugfs_create_x32("active_ltr", 0444, dir, &intel_host->active_ltr);
909 debugfs_create_x32("idle_ltr", 0444, dir, &intel_host->idle_ltr);
911 intel_cache_ltr(slot);
914 static int byt_add_host(struct sdhci_pci_slot *slot)
916 int ret = sdhci_add_host(slot->host);
918 if (!ret)
919 byt_add_debugfs(slot);
920 return ret;
923 static void byt_remove_slot(struct sdhci_pci_slot *slot, int dead)
925 struct mmc_host *mmc = slot->host->mmc;
927 if (!mmc->slotno)
928 intel_ltr_hide(slot->chip);
931 static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
933 byt_probe_slot(slot);
934 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
935 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
936 MMC_CAP_CMD_DURING_TFR |
937 MMC_CAP_WAIT_WHILE_BUSY;
938 slot->hw_reset = sdhci_pci_int_hw_reset;
939 if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BSW_EMMC)
940 slot->host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
941 slot->host->mmc_host_ops.select_drive_strength =
942 intel_select_drive_strength;
943 return 0;
946 static bool glk_broken_cqhci(struct sdhci_pci_slot *slot)
948 return slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
949 (dmi_match(DMI_BIOS_VENDOR, "LENOVO") ||
950 dmi_match(DMI_SYS_VENDOR, "IRBIS"));
953 static int glk_emmc_probe_slot(struct sdhci_pci_slot *slot)
955 int ret = byt_emmc_probe_slot(slot);
957 if (!glk_broken_cqhci(slot))
958 slot->host->mmc->caps2 |= MMC_CAP2_CQE;
960 if (slot->chip->pdev->device != PCI_DEVICE_ID_INTEL_GLK_EMMC) {
961 slot->host->mmc->caps2 |= MMC_CAP2_HS400_ES,
962 slot->host->mmc_host_ops.hs400_enhanced_strobe =
963 intel_hs400_enhanced_strobe;
964 slot->host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
967 return ret;
970 static const struct cqhci_host_ops glk_cqhci_ops = {
971 .enable = sdhci_cqe_enable,
972 .disable = sdhci_cqe_disable,
973 .dumpregs = sdhci_pci_dumpregs,
976 static int glk_emmc_add_host(struct sdhci_pci_slot *slot)
978 struct device *dev = &slot->chip->pdev->dev;
979 struct sdhci_host *host = slot->host;
980 struct cqhci_host *cq_host;
981 bool dma64;
982 int ret;
984 ret = sdhci_setup_host(host);
985 if (ret)
986 return ret;
988 cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
989 if (!cq_host) {
990 ret = -ENOMEM;
991 goto cleanup;
994 cq_host->mmio = host->ioaddr + 0x200;
995 cq_host->quirks |= CQHCI_QUIRK_SHORT_TXFR_DESC_SZ;
996 cq_host->ops = &glk_cqhci_ops;
998 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
999 if (dma64)
1000 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1002 ret = cqhci_init(cq_host, host->mmc, dma64);
1003 if (ret)
1004 goto cleanup;
1006 ret = __sdhci_add_host(host);
1007 if (ret)
1008 goto cleanup;
1010 byt_add_debugfs(slot);
1012 return 0;
1014 cleanup:
1015 sdhci_cleanup_host(host);
1016 return ret;
1019 #ifdef CONFIG_PM
1020 #define GLK_RX_CTRL1 0x834
1021 #define GLK_TUN_VAL 0x840
1022 #define GLK_PATH_PLL GENMASK(13, 8)
1023 #define GLK_DLY GENMASK(6, 0)
1024 /* Workaround firmware failing to restore the tuning value */
1025 static void glk_rpm_retune_wa(struct sdhci_pci_chip *chip, bool susp)
1027 struct sdhci_pci_slot *slot = chip->slots[0];
1028 struct intel_host *intel_host = sdhci_pci_priv(slot);
1029 struct sdhci_host *host = slot->host;
1030 u32 glk_rx_ctrl1;
1031 u32 glk_tun_val;
1032 u32 dly;
1034 if (intel_host->rpm_retune_ok || !mmc_can_retune(host->mmc))
1035 return;
1037 glk_rx_ctrl1 = sdhci_readl(host, GLK_RX_CTRL1);
1038 glk_tun_val = sdhci_readl(host, GLK_TUN_VAL);
1040 if (susp) {
1041 intel_host->glk_rx_ctrl1 = glk_rx_ctrl1;
1042 intel_host->glk_tun_val = glk_tun_val;
1043 return;
1046 if (!intel_host->glk_tun_val)
1047 return;
1049 if (glk_rx_ctrl1 != intel_host->glk_rx_ctrl1) {
1050 intel_host->rpm_retune_ok = true;
1051 return;
1054 dly = FIELD_PREP(GLK_DLY, FIELD_GET(GLK_PATH_PLL, glk_rx_ctrl1) +
1055 (intel_host->glk_tun_val << 1));
1056 if (dly == FIELD_GET(GLK_DLY, glk_rx_ctrl1))
1057 return;
1059 glk_rx_ctrl1 = (glk_rx_ctrl1 & ~GLK_DLY) | dly;
1060 sdhci_writel(host, glk_rx_ctrl1, GLK_RX_CTRL1);
1062 intel_host->rpm_retune_ok = true;
1063 chip->rpm_retune = true;
1064 mmc_retune_needed(host->mmc);
1065 pr_info("%s: Requiring re-tune after rpm resume", mmc_hostname(host->mmc));
1068 static void glk_rpm_retune_chk(struct sdhci_pci_chip *chip, bool susp)
1070 if (chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
1071 !chip->rpm_retune)
1072 glk_rpm_retune_wa(chip, susp);
1075 static int glk_runtime_suspend(struct sdhci_pci_chip *chip)
1077 glk_rpm_retune_chk(chip, true);
1079 return sdhci_cqhci_runtime_suspend(chip);
1082 static int glk_runtime_resume(struct sdhci_pci_chip *chip)
1084 glk_rpm_retune_chk(chip, false);
1086 return sdhci_cqhci_runtime_resume(chip);
1088 #endif
1090 #ifdef CONFIG_ACPI
1091 static int ni_set_max_freq(struct sdhci_pci_slot *slot)
1093 acpi_status status;
1094 unsigned long long max_freq;
1096 status = acpi_evaluate_integer(ACPI_HANDLE(&slot->chip->pdev->dev),
1097 "MXFQ", NULL, &max_freq);
1098 if (ACPI_FAILURE(status)) {
1099 dev_err(&slot->chip->pdev->dev,
1100 "MXFQ not found in acpi table\n");
1101 return -EINVAL;
1104 slot->host->mmc->f_max = max_freq * 1000000;
1106 return 0;
1108 #else
1109 static inline int ni_set_max_freq(struct sdhci_pci_slot *slot)
1111 return 0;
1113 #endif
1115 static int ni_byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
1117 int err;
1119 byt_probe_slot(slot);
1121 err = ni_set_max_freq(slot);
1122 if (err)
1123 return err;
1125 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
1126 MMC_CAP_WAIT_WHILE_BUSY;
1127 return 0;
1130 static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
1132 byt_probe_slot(slot);
1133 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
1134 MMC_CAP_WAIT_WHILE_BUSY;
1135 return 0;
1138 static int byt_sd_probe_slot(struct sdhci_pci_slot *slot)
1140 byt_probe_slot(slot);
1141 slot->host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
1142 MMC_CAP_AGGRESSIVE_PM | MMC_CAP_CD_WAKE;
1143 slot->cd_idx = 0;
1144 slot->cd_override_level = true;
1145 if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXT_SD ||
1146 slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXTM_SD ||
1147 slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_APL_SD ||
1148 slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_SD)
1149 slot->host->mmc_host_ops.get_cd = bxt_get_cd;
1151 if (slot->chip->pdev->subsystem_vendor == PCI_VENDOR_ID_NI &&
1152 slot->chip->pdev->subsystem_device == PCI_SUBDEVICE_ID_NI_78E3)
1153 slot->host->mmc->caps2 |= MMC_CAP2_AVOID_3_3V;
1155 return 0;
1158 #ifdef CONFIG_PM_SLEEP
1160 static int byt_resume(struct sdhci_pci_chip *chip)
1162 byt_ocp_setting(chip->pdev);
1164 return sdhci_pci_resume_host(chip);
1167 #endif
1169 #ifdef CONFIG_PM
1171 static int byt_runtime_resume(struct sdhci_pci_chip *chip)
1173 byt_ocp_setting(chip->pdev);
1175 return sdhci_pci_runtime_resume_host(chip);
1178 #endif
1180 static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
1181 #ifdef CONFIG_PM_SLEEP
1182 .resume = byt_resume,
1183 #endif
1184 #ifdef CONFIG_PM
1185 .runtime_resume = byt_runtime_resume,
1186 #endif
1187 .allow_runtime_pm = true,
1188 .probe_slot = byt_emmc_probe_slot,
1189 .add_host = byt_add_host,
1190 .remove_slot = byt_remove_slot,
1191 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1192 SDHCI_QUIRK_NO_LED,
1193 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1194 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1195 SDHCI_QUIRK2_STOP_WITH_TC,
1196 .ops = &sdhci_intel_byt_ops,
1197 .priv_size = sizeof(struct intel_host),
1200 static const struct sdhci_pci_fixes sdhci_intel_glk_emmc = {
1201 .allow_runtime_pm = true,
1202 .probe_slot = glk_emmc_probe_slot,
1203 .add_host = glk_emmc_add_host,
1204 .remove_slot = byt_remove_slot,
1205 #ifdef CONFIG_PM_SLEEP
1206 .suspend = sdhci_cqhci_suspend,
1207 .resume = sdhci_cqhci_resume,
1208 #endif
1209 #ifdef CONFIG_PM
1210 .runtime_suspend = glk_runtime_suspend,
1211 .runtime_resume = glk_runtime_resume,
1212 #endif
1213 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1214 SDHCI_QUIRK_NO_LED,
1215 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1216 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1217 SDHCI_QUIRK2_STOP_WITH_TC,
1218 .ops = &sdhci_intel_glk_ops,
1219 .priv_size = sizeof(struct intel_host),
1222 static const struct sdhci_pci_fixes sdhci_ni_byt_sdio = {
1223 #ifdef CONFIG_PM_SLEEP
1224 .resume = byt_resume,
1225 #endif
1226 #ifdef CONFIG_PM
1227 .runtime_resume = byt_runtime_resume,
1228 #endif
1229 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1230 SDHCI_QUIRK_NO_LED,
1231 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1232 SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1233 .allow_runtime_pm = true,
1234 .probe_slot = ni_byt_sdio_probe_slot,
1235 .add_host = byt_add_host,
1236 .remove_slot = byt_remove_slot,
1237 .ops = &sdhci_intel_byt_ops,
1238 .priv_size = sizeof(struct intel_host),
1241 static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
1242 #ifdef CONFIG_PM_SLEEP
1243 .resume = byt_resume,
1244 #endif
1245 #ifdef CONFIG_PM
1246 .runtime_resume = byt_runtime_resume,
1247 #endif
1248 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1249 SDHCI_QUIRK_NO_LED,
1250 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1251 SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1252 .allow_runtime_pm = true,
1253 .probe_slot = byt_sdio_probe_slot,
1254 .add_host = byt_add_host,
1255 .remove_slot = byt_remove_slot,
1256 .ops = &sdhci_intel_byt_ops,
1257 .priv_size = sizeof(struct intel_host),
1260 static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
1261 #ifdef CONFIG_PM_SLEEP
1262 .resume = byt_resume,
1263 #endif
1264 #ifdef CONFIG_PM
1265 .runtime_resume = byt_runtime_resume,
1266 #endif
1267 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1268 SDHCI_QUIRK_NO_LED,
1269 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1270 SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1271 SDHCI_QUIRK2_STOP_WITH_TC,
1272 .allow_runtime_pm = true,
1273 .own_cd_for_runtime_pm = true,
1274 .probe_slot = byt_sd_probe_slot,
1275 .add_host = byt_add_host,
1276 .remove_slot = byt_remove_slot,
1277 .ops = &sdhci_intel_byt_ops,
1278 .priv_size = sizeof(struct intel_host),
1281 /* Define Host controllers for Intel Merrifield platform */
1282 #define INTEL_MRFLD_EMMC_0 0
1283 #define INTEL_MRFLD_EMMC_1 1
1284 #define INTEL_MRFLD_SD 2
1285 #define INTEL_MRFLD_SDIO 3
1287 #ifdef CONFIG_ACPI
1288 static void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot)
1290 struct acpi_device *device, *child;
1292 device = ACPI_COMPANION(&slot->chip->pdev->dev);
1293 if (!device)
1294 return;
1296 acpi_device_fix_up_power(device);
1297 list_for_each_entry(child, &device->children, node)
1298 if (child->status.present && child->status.enabled)
1299 acpi_device_fix_up_power(child);
1301 #else
1302 static inline void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot) {}
1303 #endif
1305 static int intel_mrfld_mmc_probe_slot(struct sdhci_pci_slot *slot)
1307 unsigned int func = PCI_FUNC(slot->chip->pdev->devfn);
1309 switch (func) {
1310 case INTEL_MRFLD_EMMC_0:
1311 case INTEL_MRFLD_EMMC_1:
1312 slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
1313 MMC_CAP_8_BIT_DATA |
1314 MMC_CAP_1_8V_DDR;
1315 break;
1316 case INTEL_MRFLD_SD:
1317 slot->host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1318 break;
1319 case INTEL_MRFLD_SDIO:
1320 /* Advertise 2.0v for compatibility with the SDIO card's OCR */
1321 slot->host->ocr_mask = MMC_VDD_20_21 | MMC_VDD_165_195;
1322 slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
1323 MMC_CAP_POWER_OFF_CARD;
1324 break;
1325 default:
1326 return -ENODEV;
1329 intel_mrfld_mmc_fix_up_power_slot(slot);
1330 return 0;
1333 static const struct sdhci_pci_fixes sdhci_intel_mrfld_mmc = {
1334 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1335 .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
1336 SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1337 .allow_runtime_pm = true,
1338 .probe_slot = intel_mrfld_mmc_probe_slot,
1341 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
1343 u8 scratch;
1344 int ret;
1346 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
1347 if (ret)
1348 return ret;
1351 * Turn PMOS on [bit 0], set over current detection to 2.4 V
1352 * [bit 1:2] and enable over current debouncing [bit 6].
1354 if (on)
1355 scratch |= 0x47;
1356 else
1357 scratch &= ~0x47;
1359 return pci_write_config_byte(chip->pdev, 0xAE, scratch);
1362 static int jmicron_probe(struct sdhci_pci_chip *chip)
1364 int ret;
1365 u16 mmcdev = 0;
1367 if (chip->pdev->revision == 0) {
1368 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
1369 SDHCI_QUIRK_32BIT_DMA_SIZE |
1370 SDHCI_QUIRK_32BIT_ADMA_SIZE |
1371 SDHCI_QUIRK_RESET_AFTER_REQUEST |
1372 SDHCI_QUIRK_BROKEN_SMALL_PIO;
1376 * JMicron chips can have two interfaces to the same hardware
1377 * in order to work around limitations in Microsoft's driver.
1378 * We need to make sure we only bind to one of them.
1380 * This code assumes two things:
1382 * 1. The PCI code adds subfunctions in order.
1384 * 2. The MMC interface has a lower subfunction number
1385 * than the SD interface.
1387 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
1388 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
1389 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
1390 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
1392 if (mmcdev) {
1393 struct pci_dev *sd_dev;
1395 sd_dev = NULL;
1396 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
1397 mmcdev, sd_dev)) != NULL) {
1398 if ((PCI_SLOT(chip->pdev->devfn) ==
1399 PCI_SLOT(sd_dev->devfn)) &&
1400 (chip->pdev->bus == sd_dev->bus))
1401 break;
1404 if (sd_dev) {
1405 pci_dev_put(sd_dev);
1406 dev_info(&chip->pdev->dev, "Refusing to bind to "
1407 "secondary interface.\n");
1408 return -ENODEV;
1413 * JMicron chips need a bit of a nudge to enable the power
1414 * output pins.
1416 ret = jmicron_pmos(chip, 1);
1417 if (ret) {
1418 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
1419 return ret;
1422 /* quirk for unsable RO-detection on JM388 chips */
1423 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
1424 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
1425 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
1427 return 0;
1430 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
1432 u8 scratch;
1434 scratch = readb(host->ioaddr + 0xC0);
1436 if (on)
1437 scratch |= 0x01;
1438 else
1439 scratch &= ~0x01;
1441 writeb(scratch, host->ioaddr + 0xC0);
1444 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
1446 if (slot->chip->pdev->revision == 0) {
1447 u16 version;
1449 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
1450 version = (version & SDHCI_VENDOR_VER_MASK) >>
1451 SDHCI_VENDOR_VER_SHIFT;
1454 * Older versions of the chip have lots of nasty glitches
1455 * in the ADMA engine. It's best just to avoid it
1456 * completely.
1458 if (version < 0xAC)
1459 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1462 /* JM388 MMC doesn't support 1.8V while SD supports it */
1463 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
1464 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
1465 MMC_VDD_29_30 | MMC_VDD_30_31 |
1466 MMC_VDD_165_195; /* allow 1.8V */
1467 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
1468 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
1472 * The secondary interface requires a bit set to get the
1473 * interrupts.
1475 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
1476 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
1477 jmicron_enable_mmc(slot->host, 1);
1479 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
1481 return 0;
1484 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
1486 if (dead)
1487 return;
1489 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
1490 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
1491 jmicron_enable_mmc(slot->host, 0);
1494 #ifdef CONFIG_PM_SLEEP
1495 static int jmicron_suspend(struct sdhci_pci_chip *chip)
1497 int i, ret;
1499 ret = sdhci_pci_suspend_host(chip);
1500 if (ret)
1501 return ret;
1503 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
1504 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
1505 for (i = 0; i < chip->num_slots; i++)
1506 jmicron_enable_mmc(chip->slots[i]->host, 0);
1509 return 0;
1512 static int jmicron_resume(struct sdhci_pci_chip *chip)
1514 int ret, i;
1516 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
1517 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
1518 for (i = 0; i < chip->num_slots; i++)
1519 jmicron_enable_mmc(chip->slots[i]->host, 1);
1522 ret = jmicron_pmos(chip, 1);
1523 if (ret) {
1524 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
1525 return ret;
1528 return sdhci_pci_resume_host(chip);
1530 #endif
1532 static const struct sdhci_pci_fixes sdhci_jmicron = {
1533 .probe = jmicron_probe,
1535 .probe_slot = jmicron_probe_slot,
1536 .remove_slot = jmicron_remove_slot,
1538 #ifdef CONFIG_PM_SLEEP
1539 .suspend = jmicron_suspend,
1540 .resume = jmicron_resume,
1541 #endif
1544 /* SysKonnect CardBus2SDIO extra registers */
1545 #define SYSKT_CTRL 0x200
1546 #define SYSKT_RDFIFO_STAT 0x204
1547 #define SYSKT_WRFIFO_STAT 0x208
1548 #define SYSKT_POWER_DATA 0x20c
1549 #define SYSKT_POWER_330 0xef
1550 #define SYSKT_POWER_300 0xf8
1551 #define SYSKT_POWER_184 0xcc
1552 #define SYSKT_POWER_CMD 0x20d
1553 #define SYSKT_POWER_START (1 << 7)
1554 #define SYSKT_POWER_STATUS 0x20e
1555 #define SYSKT_POWER_STATUS_OK (1 << 0)
1556 #define SYSKT_BOARD_REV 0x210
1557 #define SYSKT_CHIP_REV 0x211
1558 #define SYSKT_CONF_DATA 0x212
1559 #define SYSKT_CONF_DATA_1V8 (1 << 2)
1560 #define SYSKT_CONF_DATA_2V5 (1 << 1)
1561 #define SYSKT_CONF_DATA_3V3 (1 << 0)
1563 static int syskt_probe(struct sdhci_pci_chip *chip)
1565 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1566 chip->pdev->class &= ~0x0000FF;
1567 chip->pdev->class |= PCI_SDHCI_IFDMA;
1569 return 0;
1572 static int syskt_probe_slot(struct sdhci_pci_slot *slot)
1574 int tm, ps;
1576 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
1577 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
1578 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
1579 "board rev %d.%d, chip rev %d.%d\n",
1580 board_rev >> 4, board_rev & 0xf,
1581 chip_rev >> 4, chip_rev & 0xf);
1582 if (chip_rev >= 0x20)
1583 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
1585 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
1586 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
1587 udelay(50);
1588 tm = 10; /* Wait max 1 ms */
1589 do {
1590 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
1591 if (ps & SYSKT_POWER_STATUS_OK)
1592 break;
1593 udelay(100);
1594 } while (--tm);
1595 if (!tm) {
1596 dev_err(&slot->chip->pdev->dev,
1597 "power regulator never stabilized");
1598 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
1599 return -ENODEV;
1602 return 0;
1605 static const struct sdhci_pci_fixes sdhci_syskt = {
1606 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
1607 .probe = syskt_probe,
1608 .probe_slot = syskt_probe_slot,
1611 static int via_probe(struct sdhci_pci_chip *chip)
1613 if (chip->pdev->revision == 0x10)
1614 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
1616 return 0;
1619 static const struct sdhci_pci_fixes sdhci_via = {
1620 .probe = via_probe,
1623 static int rtsx_probe_slot(struct sdhci_pci_slot *slot)
1625 slot->host->mmc->caps2 |= MMC_CAP2_HS200;
1626 return 0;
1629 static const struct sdhci_pci_fixes sdhci_rtsx = {
1630 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1631 SDHCI_QUIRK2_BROKEN_64_BIT_DMA |
1632 SDHCI_QUIRK2_BROKEN_DDR50,
1633 .probe_slot = rtsx_probe_slot,
1636 /*AMD chipset generation*/
1637 enum amd_chipset_gen {
1638 AMD_CHIPSET_BEFORE_ML,
1639 AMD_CHIPSET_CZ,
1640 AMD_CHIPSET_NL,
1641 AMD_CHIPSET_UNKNOWN,
1644 /* AMD registers */
1645 #define AMD_SD_AUTO_PATTERN 0xB8
1646 #define AMD_MSLEEP_DURATION 4
1647 #define AMD_SD_MISC_CONTROL 0xD0
1648 #define AMD_MAX_TUNE_VALUE 0x0B
1649 #define AMD_AUTO_TUNE_SEL 0x10800
1650 #define AMD_FIFO_PTR 0x30
1651 #define AMD_BIT_MASK 0x1F
1653 static void amd_tuning_reset(struct sdhci_host *host)
1655 unsigned int val;
1657 val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1658 val |= SDHCI_CTRL_PRESET_VAL_ENABLE | SDHCI_CTRL_EXEC_TUNING;
1659 sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
1661 val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1662 val &= ~SDHCI_CTRL_EXEC_TUNING;
1663 sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
1666 static void amd_config_tuning_phase(struct pci_dev *pdev, u8 phase)
1668 unsigned int val;
1670 pci_read_config_dword(pdev, AMD_SD_AUTO_PATTERN, &val);
1671 val &= ~AMD_BIT_MASK;
1672 val |= (AMD_AUTO_TUNE_SEL | (phase << 1));
1673 pci_write_config_dword(pdev, AMD_SD_AUTO_PATTERN, val);
1676 static void amd_enable_manual_tuning(struct pci_dev *pdev)
1678 unsigned int val;
1680 pci_read_config_dword(pdev, AMD_SD_MISC_CONTROL, &val);
1681 val |= AMD_FIFO_PTR;
1682 pci_write_config_dword(pdev, AMD_SD_MISC_CONTROL, val);
1685 static int amd_execute_tuning_hs200(struct sdhci_host *host, u32 opcode)
1687 struct sdhci_pci_slot *slot = sdhci_priv(host);
1688 struct pci_dev *pdev = slot->chip->pdev;
1689 u8 valid_win = 0;
1690 u8 valid_win_max = 0;
1691 u8 valid_win_end = 0;
1692 u8 ctrl, tune_around;
1694 amd_tuning_reset(host);
1696 for (tune_around = 0; tune_around < 12; tune_around++) {
1697 amd_config_tuning_phase(pdev, tune_around);
1699 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1700 valid_win = 0;
1701 msleep(AMD_MSLEEP_DURATION);
1702 ctrl = SDHCI_RESET_CMD | SDHCI_RESET_DATA;
1703 sdhci_writeb(host, ctrl, SDHCI_SOFTWARE_RESET);
1704 } else if (++valid_win > valid_win_max) {
1705 valid_win_max = valid_win;
1706 valid_win_end = tune_around;
1710 if (!valid_win_max) {
1711 dev_err(&pdev->dev, "no tuning point found\n");
1712 return -EIO;
1715 amd_config_tuning_phase(pdev, valid_win_end - valid_win_max / 2);
1717 amd_enable_manual_tuning(pdev);
1719 host->mmc->retune_period = 0;
1721 return 0;
1724 static int amd_execute_tuning(struct mmc_host *mmc, u32 opcode)
1726 struct sdhci_host *host = mmc_priv(mmc);
1728 /* AMD requires custom HS200 tuning */
1729 if (host->timing == MMC_TIMING_MMC_HS200)
1730 return amd_execute_tuning_hs200(host, opcode);
1732 /* Otherwise perform standard SDHCI tuning */
1733 return sdhci_execute_tuning(mmc, opcode);
1736 static int amd_probe_slot(struct sdhci_pci_slot *slot)
1738 struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
1740 ops->execute_tuning = amd_execute_tuning;
1742 return 0;
1745 static int amd_probe(struct sdhci_pci_chip *chip)
1747 struct pci_dev *smbus_dev;
1748 enum amd_chipset_gen gen;
1750 smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
1751 PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
1752 if (smbus_dev) {
1753 gen = AMD_CHIPSET_BEFORE_ML;
1754 } else {
1755 smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
1756 PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
1757 if (smbus_dev) {
1758 if (smbus_dev->revision < 0x51)
1759 gen = AMD_CHIPSET_CZ;
1760 else
1761 gen = AMD_CHIPSET_NL;
1762 } else {
1763 gen = AMD_CHIPSET_UNKNOWN;
1767 if (gen == AMD_CHIPSET_BEFORE_ML || gen == AMD_CHIPSET_CZ)
1768 chip->quirks2 |= SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD;
1770 return 0;
1773 static u32 sdhci_read_present_state(struct sdhci_host *host)
1775 return sdhci_readl(host, SDHCI_PRESENT_STATE);
1778 static void amd_sdhci_reset(struct sdhci_host *host, u8 mask)
1780 struct sdhci_pci_slot *slot = sdhci_priv(host);
1781 struct pci_dev *pdev = slot->chip->pdev;
1782 u32 present_state;
1785 * SDHC 0x7906 requires a hard reset to clear all internal state.
1786 * Otherwise it can get into a bad state where the DATA lines are always
1787 * read as zeros.
1789 if (pdev->device == 0x7906 && (mask & SDHCI_RESET_ALL)) {
1790 pci_clear_master(pdev);
1792 pci_save_state(pdev);
1794 pci_set_power_state(pdev, PCI_D3cold);
1795 pr_debug("%s: power_state=%u\n", mmc_hostname(host->mmc),
1796 pdev->current_state);
1797 pci_set_power_state(pdev, PCI_D0);
1799 pci_restore_state(pdev);
1802 * SDHCI_RESET_ALL says the card detect logic should not be
1803 * reset, but since we need to reset the entire controller
1804 * we should wait until the card detect logic has stabilized.
1806 * This normally takes about 40ms.
1808 readx_poll_timeout(
1809 sdhci_read_present_state,
1810 host,
1811 present_state,
1812 present_state & SDHCI_CD_STABLE,
1813 10000,
1814 100000
1818 return sdhci_reset(host, mask);
1821 static const struct sdhci_ops amd_sdhci_pci_ops = {
1822 .set_clock = sdhci_set_clock,
1823 .enable_dma = sdhci_pci_enable_dma,
1824 .set_bus_width = sdhci_set_bus_width,
1825 .reset = amd_sdhci_reset,
1826 .set_uhs_signaling = sdhci_set_uhs_signaling,
1829 static const struct sdhci_pci_fixes sdhci_amd = {
1830 .probe = amd_probe,
1831 .ops = &amd_sdhci_pci_ops,
1832 .probe_slot = amd_probe_slot,
1835 static const struct pci_device_id pci_ids[] = {
1836 SDHCI_PCI_DEVICE(RICOH, R5C822, ricoh),
1837 SDHCI_PCI_DEVICE(RICOH, R5C843, ricoh_mmc),
1838 SDHCI_PCI_DEVICE(RICOH, R5CE822, ricoh_mmc),
1839 SDHCI_PCI_DEVICE(RICOH, R5CE823, ricoh_mmc),
1840 SDHCI_PCI_DEVICE(ENE, CB712_SD, ene_712),
1841 SDHCI_PCI_DEVICE(ENE, CB712_SD_2, ene_712),
1842 SDHCI_PCI_DEVICE(ENE, CB714_SD, ene_714),
1843 SDHCI_PCI_DEVICE(ENE, CB714_SD_2, ene_714),
1844 SDHCI_PCI_DEVICE(MARVELL, 88ALP01_SD, cafe),
1845 SDHCI_PCI_DEVICE(JMICRON, JMB38X_SD, jmicron),
1846 SDHCI_PCI_DEVICE(JMICRON, JMB38X_MMC, jmicron),
1847 SDHCI_PCI_DEVICE(JMICRON, JMB388_SD, jmicron),
1848 SDHCI_PCI_DEVICE(JMICRON, JMB388_ESD, jmicron),
1849 SDHCI_PCI_DEVICE(SYSKONNECT, 8000, syskt),
1850 SDHCI_PCI_DEVICE(VIA, 95D0, via),
1851 SDHCI_PCI_DEVICE(REALTEK, 5250, rtsx),
1852 SDHCI_PCI_DEVICE(INTEL, QRK_SD, intel_qrk),
1853 SDHCI_PCI_DEVICE(INTEL, MRST_SD0, intel_mrst_hc0),
1854 SDHCI_PCI_DEVICE(INTEL, MRST_SD1, intel_mrst_hc1_hc2),
1855 SDHCI_PCI_DEVICE(INTEL, MRST_SD2, intel_mrst_hc1_hc2),
1856 SDHCI_PCI_DEVICE(INTEL, MFD_SD, intel_mfd_sd),
1857 SDHCI_PCI_DEVICE(INTEL, MFD_SDIO1, intel_mfd_sdio),
1858 SDHCI_PCI_DEVICE(INTEL, MFD_SDIO2, intel_mfd_sdio),
1859 SDHCI_PCI_DEVICE(INTEL, MFD_EMMC0, intel_mfd_emmc),
1860 SDHCI_PCI_DEVICE(INTEL, MFD_EMMC1, intel_mfd_emmc),
1861 SDHCI_PCI_DEVICE(INTEL, PCH_SDIO0, intel_pch_sdio),
1862 SDHCI_PCI_DEVICE(INTEL, PCH_SDIO1, intel_pch_sdio),
1863 SDHCI_PCI_DEVICE(INTEL, BYT_EMMC, intel_byt_emmc),
1864 SDHCI_PCI_SUBDEVICE(INTEL, BYT_SDIO, NI, 7884, ni_byt_sdio),
1865 SDHCI_PCI_DEVICE(INTEL, BYT_SDIO, intel_byt_sdio),
1866 SDHCI_PCI_DEVICE(INTEL, BYT_SD, intel_byt_sd),
1867 SDHCI_PCI_DEVICE(INTEL, BYT_EMMC2, intel_byt_emmc),
1868 SDHCI_PCI_DEVICE(INTEL, BSW_EMMC, intel_byt_emmc),
1869 SDHCI_PCI_DEVICE(INTEL, BSW_SDIO, intel_byt_sdio),
1870 SDHCI_PCI_DEVICE(INTEL, BSW_SD, intel_byt_sd),
1871 SDHCI_PCI_DEVICE(INTEL, CLV_SDIO0, intel_mfd_sd),
1872 SDHCI_PCI_DEVICE(INTEL, CLV_SDIO1, intel_mfd_sdio),
1873 SDHCI_PCI_DEVICE(INTEL, CLV_SDIO2, intel_mfd_sdio),
1874 SDHCI_PCI_DEVICE(INTEL, CLV_EMMC0, intel_mfd_emmc),
1875 SDHCI_PCI_DEVICE(INTEL, CLV_EMMC1, intel_mfd_emmc),
1876 SDHCI_PCI_DEVICE(INTEL, MRFLD_MMC, intel_mrfld_mmc),
1877 SDHCI_PCI_DEVICE(INTEL, SPT_EMMC, intel_byt_emmc),
1878 SDHCI_PCI_DEVICE(INTEL, SPT_SDIO, intel_byt_sdio),
1879 SDHCI_PCI_DEVICE(INTEL, SPT_SD, intel_byt_sd),
1880 SDHCI_PCI_DEVICE(INTEL, DNV_EMMC, intel_byt_emmc),
1881 SDHCI_PCI_DEVICE(INTEL, CDF_EMMC, intel_glk_emmc),
1882 SDHCI_PCI_DEVICE(INTEL, BXT_EMMC, intel_byt_emmc),
1883 SDHCI_PCI_DEVICE(INTEL, BXT_SDIO, intel_byt_sdio),
1884 SDHCI_PCI_DEVICE(INTEL, BXT_SD, intel_byt_sd),
1885 SDHCI_PCI_DEVICE(INTEL, BXTM_EMMC, intel_byt_emmc),
1886 SDHCI_PCI_DEVICE(INTEL, BXTM_SDIO, intel_byt_sdio),
1887 SDHCI_PCI_DEVICE(INTEL, BXTM_SD, intel_byt_sd),
1888 SDHCI_PCI_DEVICE(INTEL, APL_EMMC, intel_byt_emmc),
1889 SDHCI_PCI_DEVICE(INTEL, APL_SDIO, intel_byt_sdio),
1890 SDHCI_PCI_DEVICE(INTEL, APL_SD, intel_byt_sd),
1891 SDHCI_PCI_DEVICE(INTEL, GLK_EMMC, intel_glk_emmc),
1892 SDHCI_PCI_DEVICE(INTEL, GLK_SDIO, intel_byt_sdio),
1893 SDHCI_PCI_DEVICE(INTEL, GLK_SD, intel_byt_sd),
1894 SDHCI_PCI_DEVICE(INTEL, CNP_EMMC, intel_glk_emmc),
1895 SDHCI_PCI_DEVICE(INTEL, CNP_SD, intel_byt_sd),
1896 SDHCI_PCI_DEVICE(INTEL, CNPH_SD, intel_byt_sd),
1897 SDHCI_PCI_DEVICE(INTEL, ICP_EMMC, intel_glk_emmc),
1898 SDHCI_PCI_DEVICE(INTEL, ICP_SD, intel_byt_sd),
1899 SDHCI_PCI_DEVICE(INTEL, EHL_EMMC, intel_glk_emmc),
1900 SDHCI_PCI_DEVICE(INTEL, EHL_SD, intel_byt_sd),
1901 SDHCI_PCI_DEVICE(INTEL, CML_EMMC, intel_glk_emmc),
1902 SDHCI_PCI_DEVICE(INTEL, CML_SD, intel_byt_sd),
1903 SDHCI_PCI_DEVICE(INTEL, CMLH_SD, intel_byt_sd),
1904 SDHCI_PCI_DEVICE(INTEL, JSL_EMMC, intel_glk_emmc),
1905 SDHCI_PCI_DEVICE(INTEL, JSL_SD, intel_byt_sd),
1906 SDHCI_PCI_DEVICE(O2, 8120, o2),
1907 SDHCI_PCI_DEVICE(O2, 8220, o2),
1908 SDHCI_PCI_DEVICE(O2, 8221, o2),
1909 SDHCI_PCI_DEVICE(O2, 8320, o2),
1910 SDHCI_PCI_DEVICE(O2, 8321, o2),
1911 SDHCI_PCI_DEVICE(O2, FUJIN2, o2),
1912 SDHCI_PCI_DEVICE(O2, SDS0, o2),
1913 SDHCI_PCI_DEVICE(O2, SDS1, o2),
1914 SDHCI_PCI_DEVICE(O2, SEABIRD0, o2),
1915 SDHCI_PCI_DEVICE(O2, SEABIRD1, o2),
1916 SDHCI_PCI_DEVICE(ARASAN, PHY_EMMC, arasan),
1917 SDHCI_PCI_DEVICE(SYNOPSYS, DWC_MSHC, snps),
1918 SDHCI_PCI_DEVICE(GLI, 9750, gl9750),
1919 SDHCI_PCI_DEVICE(GLI, 9755, gl9755),
1920 SDHCI_PCI_DEVICE(GLI, 9763E, gl9763e),
1921 SDHCI_PCI_DEVICE_CLASS(AMD, SYSTEM_SDHCI, PCI_CLASS_MASK, amd),
1922 /* Generic SD host controller */
1923 {PCI_DEVICE_CLASS(SYSTEM_SDHCI, PCI_CLASS_MASK)},
1924 { /* end: all zeroes */ },
1927 MODULE_DEVICE_TABLE(pci, pci_ids);
1929 /*****************************************************************************\
1931 * SDHCI core callbacks *
1933 \*****************************************************************************/
1935 int sdhci_pci_enable_dma(struct sdhci_host *host)
1937 struct sdhci_pci_slot *slot;
1938 struct pci_dev *pdev;
1940 slot = sdhci_priv(host);
1941 pdev = slot->chip->pdev;
1943 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
1944 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1945 (host->flags & SDHCI_USE_SDMA)) {
1946 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
1947 "doesn't fully claim to support it.\n");
1950 pci_set_master(pdev);
1952 return 0;
1955 static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
1957 struct sdhci_pci_slot *slot = sdhci_priv(host);
1958 int rst_n_gpio = slot->rst_n_gpio;
1960 if (!gpio_is_valid(rst_n_gpio))
1961 return;
1962 gpio_set_value_cansleep(rst_n_gpio, 0);
1963 /* For eMMC, minimum is 1us but give it 10us for good measure */
1964 udelay(10);
1965 gpio_set_value_cansleep(rst_n_gpio, 1);
1966 /* For eMMC, minimum is 200us but give it 300us for good measure */
1967 usleep_range(300, 1000);
1970 static void sdhci_pci_hw_reset(struct sdhci_host *host)
1972 struct sdhci_pci_slot *slot = sdhci_priv(host);
1974 if (slot->hw_reset)
1975 slot->hw_reset(host);
1978 static const struct sdhci_ops sdhci_pci_ops = {
1979 .set_clock = sdhci_set_clock,
1980 .enable_dma = sdhci_pci_enable_dma,
1981 .set_bus_width = sdhci_set_bus_width,
1982 .reset = sdhci_reset,
1983 .set_uhs_signaling = sdhci_set_uhs_signaling,
1984 .hw_reset = sdhci_pci_hw_reset,
1987 /*****************************************************************************\
1989 * Suspend/resume *
1991 \*****************************************************************************/
1993 #ifdef CONFIG_PM_SLEEP
1994 static int sdhci_pci_suspend(struct device *dev)
1996 struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
1998 if (!chip)
1999 return 0;
2001 if (chip->fixes && chip->fixes->suspend)
2002 return chip->fixes->suspend(chip);
2004 return sdhci_pci_suspend_host(chip);
2007 static int sdhci_pci_resume(struct device *dev)
2009 struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
2011 if (!chip)
2012 return 0;
2014 if (chip->fixes && chip->fixes->resume)
2015 return chip->fixes->resume(chip);
2017 return sdhci_pci_resume_host(chip);
2019 #endif
2021 #ifdef CONFIG_PM
2022 static int sdhci_pci_runtime_suspend(struct device *dev)
2024 struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
2026 if (!chip)
2027 return 0;
2029 if (chip->fixes && chip->fixes->runtime_suspend)
2030 return chip->fixes->runtime_suspend(chip);
2032 return sdhci_pci_runtime_suspend_host(chip);
2035 static int sdhci_pci_runtime_resume(struct device *dev)
2037 struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
2039 if (!chip)
2040 return 0;
2042 if (chip->fixes && chip->fixes->runtime_resume)
2043 return chip->fixes->runtime_resume(chip);
2045 return sdhci_pci_runtime_resume_host(chip);
2047 #endif
2049 static const struct dev_pm_ops sdhci_pci_pm_ops = {
2050 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pci_suspend, sdhci_pci_resume)
2051 SET_RUNTIME_PM_OPS(sdhci_pci_runtime_suspend,
2052 sdhci_pci_runtime_resume, NULL)
2055 /*****************************************************************************\
2057 * Device probing/removal *
2059 \*****************************************************************************/
2061 static struct sdhci_pci_slot *sdhci_pci_probe_slot(
2062 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
2063 int slotno)
2065 struct sdhci_pci_slot *slot;
2066 struct sdhci_host *host;
2067 int ret, bar = first_bar + slotno;
2068 size_t priv_size = chip->fixes ? chip->fixes->priv_size : 0;
2070 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
2071 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
2072 return ERR_PTR(-ENODEV);
2075 if (pci_resource_len(pdev, bar) < 0x100) {
2076 dev_err(&pdev->dev, "Invalid iomem size. You may "
2077 "experience problems.\n");
2080 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
2081 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
2082 return ERR_PTR(-ENODEV);
2085 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
2086 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
2087 return ERR_PTR(-ENODEV);
2090 host = sdhci_alloc_host(&pdev->dev, sizeof(*slot) + priv_size);
2091 if (IS_ERR(host)) {
2092 dev_err(&pdev->dev, "cannot allocate host\n");
2093 return ERR_CAST(host);
2096 slot = sdhci_priv(host);
2098 slot->chip = chip;
2099 slot->host = host;
2100 slot->rst_n_gpio = -EINVAL;
2101 slot->cd_gpio = -EINVAL;
2102 slot->cd_idx = -1;
2104 /* Retrieve platform data if there is any */
2105 if (*sdhci_pci_get_data)
2106 slot->data = sdhci_pci_get_data(pdev, slotno);
2108 if (slot->data) {
2109 if (slot->data->setup) {
2110 ret = slot->data->setup(slot->data);
2111 if (ret) {
2112 dev_err(&pdev->dev, "platform setup failed\n");
2113 goto free;
2116 slot->rst_n_gpio = slot->data->rst_n_gpio;
2117 slot->cd_gpio = slot->data->cd_gpio;
2120 host->hw_name = "PCI";
2121 host->ops = chip->fixes && chip->fixes->ops ?
2122 chip->fixes->ops :
2123 &sdhci_pci_ops;
2124 host->quirks = chip->quirks;
2125 host->quirks2 = chip->quirks2;
2127 host->irq = pdev->irq;
2129 ret = pcim_iomap_regions(pdev, BIT(bar), mmc_hostname(host->mmc));
2130 if (ret) {
2131 dev_err(&pdev->dev, "cannot request region\n");
2132 goto cleanup;
2135 host->ioaddr = pcim_iomap_table(pdev)[bar];
2137 if (chip->fixes && chip->fixes->probe_slot) {
2138 ret = chip->fixes->probe_slot(slot);
2139 if (ret)
2140 goto cleanup;
2143 if (gpio_is_valid(slot->rst_n_gpio)) {
2144 if (!devm_gpio_request(&pdev->dev, slot->rst_n_gpio, "eMMC_reset")) {
2145 gpio_direction_output(slot->rst_n_gpio, 1);
2146 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
2147 slot->hw_reset = sdhci_pci_gpio_hw_reset;
2148 } else {
2149 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
2150 slot->rst_n_gpio = -EINVAL;
2154 host->mmc->pm_caps = MMC_PM_KEEP_POWER;
2155 host->mmc->slotno = slotno;
2156 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
2158 if (device_can_wakeup(&pdev->dev))
2159 host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
2161 if (host->mmc->caps & MMC_CAP_CD_WAKE)
2162 device_init_wakeup(&pdev->dev, true);
2164 if (slot->cd_idx >= 0) {
2165 ret = mmc_gpiod_request_cd(host->mmc, "cd", slot->cd_idx,
2166 slot->cd_override_level, 0);
2167 if (ret && ret != -EPROBE_DEFER)
2168 ret = mmc_gpiod_request_cd(host->mmc, NULL,
2169 slot->cd_idx,
2170 slot->cd_override_level,
2172 if (ret == -EPROBE_DEFER)
2173 goto remove;
2175 if (ret) {
2176 dev_warn(&pdev->dev, "failed to setup card detect gpio\n");
2177 slot->cd_idx = -1;
2181 if (chip->fixes && chip->fixes->add_host)
2182 ret = chip->fixes->add_host(slot);
2183 else
2184 ret = sdhci_add_host(host);
2185 if (ret)
2186 goto remove;
2188 sdhci_pci_add_own_cd(slot);
2191 * Check if the chip needs a separate GPIO for card detect to wake up
2192 * from runtime suspend. If it is not there, don't allow runtime PM.
2193 * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
2195 if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
2196 !gpio_is_valid(slot->cd_gpio) && slot->cd_idx < 0)
2197 chip->allow_runtime_pm = false;
2199 return slot;
2201 remove:
2202 if (chip->fixes && chip->fixes->remove_slot)
2203 chip->fixes->remove_slot(slot, 0);
2205 cleanup:
2206 if (slot->data && slot->data->cleanup)
2207 slot->data->cleanup(slot->data);
2209 free:
2210 sdhci_free_host(host);
2212 return ERR_PTR(ret);
2215 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
2217 int dead;
2218 u32 scratch;
2220 sdhci_pci_remove_own_cd(slot);
2222 dead = 0;
2223 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
2224 if (scratch == (u32)-1)
2225 dead = 1;
2227 sdhci_remove_host(slot->host, dead);
2229 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
2230 slot->chip->fixes->remove_slot(slot, dead);
2232 if (slot->data && slot->data->cleanup)
2233 slot->data->cleanup(slot->data);
2235 sdhci_free_host(slot->host);
2238 static void sdhci_pci_runtime_pm_allow(struct device *dev)
2240 pm_suspend_ignore_children(dev, 1);
2241 pm_runtime_set_autosuspend_delay(dev, 50);
2242 pm_runtime_use_autosuspend(dev);
2243 pm_runtime_allow(dev);
2244 /* Stay active until mmc core scans for a card */
2245 pm_runtime_put_noidle(dev);
2248 static void sdhci_pci_runtime_pm_forbid(struct device *dev)
2250 pm_runtime_forbid(dev);
2251 pm_runtime_get_noresume(dev);
2254 static int sdhci_pci_probe(struct pci_dev *pdev,
2255 const struct pci_device_id *ent)
2257 struct sdhci_pci_chip *chip;
2258 struct sdhci_pci_slot *slot;
2260 u8 slots, first_bar;
2261 int ret, i;
2263 BUG_ON(pdev == NULL);
2264 BUG_ON(ent == NULL);
2266 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
2267 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
2269 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
2270 if (ret)
2271 return ret;
2273 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
2274 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
2276 BUG_ON(slots > MAX_SLOTS);
2278 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
2279 if (ret)
2280 return ret;
2282 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
2284 if (first_bar > 5) {
2285 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
2286 return -ENODEV;
2289 ret = pcim_enable_device(pdev);
2290 if (ret)
2291 return ret;
2293 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
2294 if (!chip)
2295 return -ENOMEM;
2297 chip->pdev = pdev;
2298 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
2299 if (chip->fixes) {
2300 chip->quirks = chip->fixes->quirks;
2301 chip->quirks2 = chip->fixes->quirks2;
2302 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
2304 chip->num_slots = slots;
2305 chip->pm_retune = true;
2306 chip->rpm_retune = true;
2308 pci_set_drvdata(pdev, chip);
2310 if (chip->fixes && chip->fixes->probe) {
2311 ret = chip->fixes->probe(chip);
2312 if (ret)
2313 return ret;
2316 slots = chip->num_slots; /* Quirk may have changed this */
2318 for (i = 0; i < slots; i++) {
2319 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
2320 if (IS_ERR(slot)) {
2321 for (i--; i >= 0; i--)
2322 sdhci_pci_remove_slot(chip->slots[i]);
2323 return PTR_ERR(slot);
2326 chip->slots[i] = slot;
2329 if (chip->allow_runtime_pm)
2330 sdhci_pci_runtime_pm_allow(&pdev->dev);
2332 return 0;
2335 static void sdhci_pci_remove(struct pci_dev *pdev)
2337 int i;
2338 struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
2340 if (chip->allow_runtime_pm)
2341 sdhci_pci_runtime_pm_forbid(&pdev->dev);
2343 for (i = 0; i < chip->num_slots; i++)
2344 sdhci_pci_remove_slot(chip->slots[i]);
2347 static struct pci_driver sdhci_driver = {
2348 .name = "sdhci-pci",
2349 .id_table = pci_ids,
2350 .probe = sdhci_pci_probe,
2351 .remove = sdhci_pci_remove,
2352 .driver = {
2353 .pm = &sdhci_pci_pm_ops
2357 module_pci_driver(sdhci_driver);
2359 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2360 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
2361 MODULE_LICENSE("GPL");