Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Secure Digital Host Controller Interface ACPI driver.
4 *
5 * Copyright (c) 2012, Intel Corporation.
6 */
7
8#include <linux/bitfield.h>
9#include <linux/init.h>
10#include <linux/export.h>
11#include <linux/module.h>
12#include <linux/device.h>
13#include <linux/platform_device.h>
14#include <linux/ioport.h>
15#include <linux/io.h>
16#include <linux/dma-mapping.h>
17#include <linux/compiler.h>
18#include <linux/stddef.h>
19#include <linux/bitops.h>
20#include <linux/types.h>
21#include <linux/err.h>
22#include <linux/interrupt.h>
23#include <linux/acpi.h>
24#include <linux/pm.h>
25#include <linux/pm_runtime.h>
26#include <linux/delay.h>
27#include <linux/dmi.h>
28
29#include <linux/mmc/host.h>
30#include <linux/mmc/pm.h>
31#include <linux/mmc/slot-gpio.h>
32
33#ifdef CONFIG_X86
34#include <linux/platform_data/x86/soc.h>
35#include <asm/iosf_mbi.h>
36#endif
37
38#include "sdhci.h"
39
40enum {
41 SDHCI_ACPI_SD_CD = BIT(0),
42 SDHCI_ACPI_RUNTIME_PM = BIT(1),
43 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
44};
45
46struct sdhci_acpi_chip {
47 const struct sdhci_ops *ops;
48 unsigned int quirks;
49 unsigned int quirks2;
50 unsigned long caps;
51 unsigned int caps2;
52 mmc_pm_flag_t pm_caps;
53};
54
55struct sdhci_acpi_slot {
56 const struct sdhci_acpi_chip *chip;
57 unsigned int quirks;
58 unsigned int quirks2;
59 unsigned long caps;
60 unsigned int caps2;
61 mmc_pm_flag_t pm_caps;
62 unsigned int flags;
63 size_t priv_size;
64 int (*probe_slot)(struct platform_device *, struct acpi_device *);
65 int (*remove_slot)(struct platform_device *);
66 int (*free_slot)(struct platform_device *pdev);
67 int (*setup_host)(struct platform_device *pdev);
68};
69
70struct sdhci_acpi_host {
71 struct sdhci_host *host;
72 const struct sdhci_acpi_slot *slot;
73 struct platform_device *pdev;
74 bool use_runtime_pm;
75 bool is_intel;
76 bool reset_signal_volt_on_suspend;
77 unsigned long private[] ____cacheline_aligned;
78};
79
80enum {
81 DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP = BIT(0),
82 DMI_QUIRK_SD_NO_WRITE_PROTECT = BIT(1),
83};
84
85static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
86{
87 return (void *)c->private;
88}
89
90static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
91{
92 return c->slot && (c->slot->flags & flag);
93}
94
95#define INTEL_DSM_HS_CAPS_SDR25 BIT(0)
96#define INTEL_DSM_HS_CAPS_DDR50 BIT(1)
97#define INTEL_DSM_HS_CAPS_SDR50 BIT(2)
98#define INTEL_DSM_HS_CAPS_SDR104 BIT(3)
99
100enum {
101 INTEL_DSM_FNS = 0,
102 INTEL_DSM_V18_SWITCH = 3,
103 INTEL_DSM_V33_SWITCH = 4,
104 INTEL_DSM_HS_CAPS = 8,
105};
106
107struct intel_host {
108 u32 dsm_fns;
109 u32 hs_caps;
110};
111
112static const guid_t intel_dsm_guid =
113 GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
114 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
115
116static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
117 unsigned int fn, u32 *result)
118{
119 union acpi_object *obj;
120 int err = 0;
121
122 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
123 if (!obj)
124 return -EOPNOTSUPP;
125
126 if (obj->type == ACPI_TYPE_INTEGER) {
127 *result = obj->integer.value;
128 } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
129 size_t len = min_t(size_t, obj->buffer.length, 4);
130
131 *result = 0;
132 memcpy(result, obj->buffer.pointer, len);
133 } else {
134 dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
135 __func__, fn, obj->type, obj->buffer.length);
136 err = -EINVAL;
137 }
138
139 ACPI_FREE(obj);
140
141 return err;
142}
143
144static int intel_dsm(struct intel_host *intel_host, struct device *dev,
145 unsigned int fn, u32 *result)
146{
147 if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
148 return -EOPNOTSUPP;
149
150 return __intel_dsm(intel_host, dev, fn, result);
151}
152
153static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
154 struct mmc_host *mmc)
155{
156 int err;
157
158 intel_host->hs_caps = ~0;
159
160 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
161 if (err) {
162 pr_debug("%s: DSM not supported, error %d\n",
163 mmc_hostname(mmc), err);
164 return;
165 }
166
167 pr_debug("%s: DSM function mask %#x\n",
168 mmc_hostname(mmc), intel_host->dsm_fns);
169
170 intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps);
171}
172
173static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
174 struct mmc_ios *ios)
175{
176 struct device *dev = mmc_dev(mmc);
177 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
178 struct intel_host *intel_host = sdhci_acpi_priv(c);
179 unsigned int fn;
180 u32 result = 0;
181 int err;
182
183 err = sdhci_start_signal_voltage_switch(mmc, ios);
184 if (err)
185 return err;
186
187 switch (ios->signal_voltage) {
188 case MMC_SIGNAL_VOLTAGE_330:
189 fn = INTEL_DSM_V33_SWITCH;
190 break;
191 case MMC_SIGNAL_VOLTAGE_180:
192 fn = INTEL_DSM_V18_SWITCH;
193 break;
194 default:
195 return 0;
196 }
197
198 err = intel_dsm(intel_host, dev, fn, &result);
199 pr_debug("%s: %s DSM fn %u error %d result %u\n",
200 mmc_hostname(mmc), __func__, fn, err, result);
201
202 return 0;
203}
204
205static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
206{
207 u8 reg;
208
209 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
210 reg |= 0x10;
211 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
212 /* For eMMC, minimum is 1us but give it 9us for good measure */
213 udelay(9);
214 reg &= ~0x10;
215 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
216 /* For eMMC, minimum is 200us but give it 300us for good measure */
217 usleep_range(300, 1000);
218}
219
220static const struct sdhci_ops sdhci_acpi_ops_dflt = {
221 .set_clock = sdhci_set_clock,
222 .set_bus_width = sdhci_set_bus_width,
223 .reset = sdhci_reset,
224 .set_uhs_signaling = sdhci_set_uhs_signaling,
225};
226
227static const struct sdhci_ops sdhci_acpi_ops_int = {
228 .set_clock = sdhci_set_clock,
229 .set_bus_width = sdhci_set_bus_width,
230 .reset = sdhci_reset,
231 .set_uhs_signaling = sdhci_set_uhs_signaling,
232 .hw_reset = sdhci_acpi_int_hw_reset,
233};
234
235static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
236 .ops = &sdhci_acpi_ops_int,
237};
238
239#ifdef CONFIG_X86
240
241#define BYT_IOSF_SCCEP 0x63
242#define BYT_IOSF_OCP_NETCTRL0 0x1078
243#define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
244
245static void sdhci_acpi_byt_setting(struct device *dev)
246{
247 u32 val = 0;
248
249 if (!soc_intel_is_byt())
250 return;
251
252 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
253 &val)) {
254 dev_err(dev, "%s read error\n", __func__);
255 return;
256 }
257
258 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
259 return;
260
261 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
262
263 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
264 val)) {
265 dev_err(dev, "%s write error\n", __func__);
266 return;
267 }
268
269 dev_dbg(dev, "%s completed\n", __func__);
270}
271
272static bool sdhci_acpi_byt_defer(struct device *dev)
273{
274 if (!soc_intel_is_byt())
275 return false;
276
277 if (!iosf_mbi_available())
278 return true;
279
280 sdhci_acpi_byt_setting(dev);
281
282 return false;
283}
284
285#else
286
287static inline void sdhci_acpi_byt_setting(struct device *dev)
288{
289}
290
291static inline bool sdhci_acpi_byt_defer(struct device *dev)
292{
293 return false;
294}
295
296#endif
297
298static int bxt_get_cd(struct mmc_host *mmc)
299{
300 int gpio_cd = mmc_gpio_get_cd(mmc);
301
302 if (!gpio_cd)
303 return 0;
304
305 return sdhci_get_cd_nogpio(mmc);
306}
307
308static int intel_probe_slot(struct platform_device *pdev, struct acpi_device *adev)
309{
310 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
311 struct intel_host *intel_host = sdhci_acpi_priv(c);
312 struct sdhci_host *host = c->host;
313
314 if (acpi_dev_hid_uid_match(adev, "80860F14", "1") &&
315 sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
316 sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
317 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
318
319 if (acpi_dev_hid_uid_match(adev, "80865ACA", NULL))
320 host->mmc_host_ops.get_cd = bxt_get_cd;
321
322 intel_dsm_init(intel_host, &pdev->dev, host->mmc);
323
324 host->mmc_host_ops.start_signal_voltage_switch =
325 intel_start_signal_voltage_switch;
326
327 c->is_intel = true;
328
329 return 0;
330}
331
332static int intel_setup_host(struct platform_device *pdev)
333{
334 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
335 struct intel_host *intel_host = sdhci_acpi_priv(c);
336
337 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25))
338 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25;
339
340 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50))
341 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50;
342
343 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50))
344 c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50;
345
346 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104))
347 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
348
349 return 0;
350}
351
352static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
353 .chip = &sdhci_acpi_chip_int,
354 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
355 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
356 MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
357 .flags = SDHCI_ACPI_RUNTIME_PM,
358 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
359 SDHCI_QUIRK_NO_LED,
360 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
361 SDHCI_QUIRK2_STOP_WITH_TC |
362 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
363 .probe_slot = intel_probe_slot,
364 .setup_host = intel_setup_host,
365 .priv_size = sizeof(struct intel_host),
366};
367
368static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
369 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
370 SDHCI_QUIRK_NO_LED |
371 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
372 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
373 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
374 MMC_CAP_WAIT_WHILE_BUSY,
375 .flags = SDHCI_ACPI_RUNTIME_PM,
376 .pm_caps = MMC_PM_KEEP_POWER,
377 .probe_slot = intel_probe_slot,
378 .setup_host = intel_setup_host,
379 .priv_size = sizeof(struct intel_host),
380};
381
382static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
383 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
384 SDHCI_ACPI_RUNTIME_PM,
385 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
386 SDHCI_QUIRK_NO_LED,
387 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
388 SDHCI_QUIRK2_STOP_WITH_TC,
389 .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
390 .probe_slot = intel_probe_slot,
391 .setup_host = intel_setup_host,
392 .priv_size = sizeof(struct intel_host),
393};
394
395#define VENDOR_SPECIFIC_PWRCTL_CLEAR_REG 0x1a8
396#define VENDOR_SPECIFIC_PWRCTL_CTL_REG 0x1ac
397static irqreturn_t sdhci_acpi_qcom_handler(int irq, void *ptr)
398{
399 struct sdhci_host *host = ptr;
400
401 sdhci_writel(host, 0x3, VENDOR_SPECIFIC_PWRCTL_CLEAR_REG);
402 sdhci_writel(host, 0x1, VENDOR_SPECIFIC_PWRCTL_CTL_REG);
403
404 return IRQ_HANDLED;
405}
406
407static int qcom_probe_slot(struct platform_device *pdev, struct acpi_device *adev)
408{
409 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
410 struct sdhci_host *host = c->host;
411 int *irq = sdhci_acpi_priv(c);
412
413 *irq = -EINVAL;
414
415 if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL))
416 return 0;
417
418 *irq = platform_get_irq(pdev, 1);
419 if (*irq < 0)
420 return 0;
421
422 return request_threaded_irq(*irq, NULL, sdhci_acpi_qcom_handler,
423 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
424 "sdhci_qcom", host);
425}
426
427static int qcom_free_slot(struct platform_device *pdev)
428{
429 struct device *dev = &pdev->dev;
430 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
431 struct sdhci_host *host = c->host;
432 struct acpi_device *adev;
433 int *irq = sdhci_acpi_priv(c);
434
435 adev = ACPI_COMPANION(dev);
436 if (!adev)
437 return -ENODEV;
438
439 if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL))
440 return 0;
441
442 if (*irq < 0)
443 return 0;
444
445 free_irq(*irq, host);
446 return 0;
447}
448
449static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
450 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
451 .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
452 .caps = MMC_CAP_NONREMOVABLE,
453 .priv_size = sizeof(int),
454 .probe_slot = qcom_probe_slot,
455 .free_slot = qcom_free_slot,
456};
457
458static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
459 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
460 .caps = MMC_CAP_NONREMOVABLE,
461};
462
463struct amd_sdhci_host {
464 bool tuned_clock;
465 bool dll_enabled;
466};
467
468/* AMD sdhci reset dll register. */
469#define SDHCI_AMD_RESET_DLL_REGISTER 0x908
470
471static int amd_select_drive_strength(struct mmc_card *card,
472 unsigned int max_dtr, int host_drv,
473 int card_drv, int *host_driver_strength)
474{
475 struct sdhci_host *host = mmc_priv(card->host);
476 u16 preset, preset_driver_strength;
477
478 /*
479 * This method is only called by mmc_select_hs200 so we only need to
480 * read from the HS200 (SDR104) preset register.
481 *
482 * Firmware that has "invalid/default" presets return a driver strength
483 * of A. This matches the previously hard coded value.
484 */
485 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
486 preset_driver_strength = FIELD_GET(SDHCI_PRESET_DRV_MASK, preset);
487
488 /*
489 * We want the controller driver strength to match the card's driver
490 * strength so they have similar rise/fall times.
491 *
492 * The controller driver strength set by this method is sticky for all
493 * timings after this method is called. This unfortunately means that
494 * while HS400 tuning is in progress we end up with mismatched driver
495 * strengths between the controller and the card. HS400 tuning requires
496 * switching from HS400->DDR52->HS->HS200->HS400. So the driver mismatch
497 * happens while in DDR52 and HS modes. This has not been observed to
498 * cause problems. Enabling presets would fix this issue.
499 */
500 *host_driver_strength = preset_driver_strength;
501
502 /*
503 * The resulting card driver strength is only set when switching the
504 * card's timing to HS200 or HS400. The card will use the default driver
505 * strength (B) for any other mode.
506 */
507 return preset_driver_strength;
508}
509
510static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host, bool enable)
511{
512 struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
513 struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
514
515 /* AMD Platform requires dll setting */
516 sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
517 usleep_range(10, 20);
518 if (enable)
519 sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
520
521 amd_host->dll_enabled = enable;
522}
523
524/*
525 * The initialization sequence for HS400 is:
526 * HS->HS200->Perform Tuning->HS->HS400
527 *
528 * The re-tuning sequence is:
529 * HS400->DDR52->HS->HS200->Perform Tuning->HS->HS400
530 *
531 * The AMD eMMC Controller can only use the tuned clock while in HS200 and HS400
532 * mode. If we switch to a different mode, we need to disable the tuned clock.
533 * If we have previously performed tuning and switch back to HS200 or
534 * HS400, we can re-enable the tuned clock.
535 *
536 */
537static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
538{
539 struct sdhci_host *host = mmc_priv(mmc);
540 struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
541 struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
542 unsigned int old_timing = host->timing;
543 u16 val;
544
545 sdhci_set_ios(mmc, ios);
546
547 if (old_timing != host->timing && amd_host->tuned_clock) {
548 if (host->timing == MMC_TIMING_MMC_HS400 ||
549 host->timing == MMC_TIMING_MMC_HS200) {
550 val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
551 val |= SDHCI_CTRL_TUNED_CLK;
552 sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
553 } else {
554 val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
555 val &= ~SDHCI_CTRL_TUNED_CLK;
556 sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
557 }
558
559 /* DLL is only required for HS400 */
560 if (host->timing == MMC_TIMING_MMC_HS400 &&
561 !amd_host->dll_enabled)
562 sdhci_acpi_amd_hs400_dll(host, true);
563 }
564}
565
566static int amd_sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
567{
568 int err;
569 struct sdhci_host *host = mmc_priv(mmc);
570 struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
571 struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
572
573 amd_host->tuned_clock = false;
574
575 err = sdhci_execute_tuning(mmc, opcode);
576
577 if (!err && !host->tuning_err)
578 amd_host->tuned_clock = true;
579
580 return err;
581}
582
583static void amd_sdhci_reset(struct sdhci_host *host, u8 mask)
584{
585 struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
586 struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
587
588 if (mask & SDHCI_RESET_ALL) {
589 amd_host->tuned_clock = false;
590 sdhci_acpi_amd_hs400_dll(host, false);
591 }
592
593 sdhci_reset(host, mask);
594}
595
596static const struct sdhci_ops sdhci_acpi_ops_amd = {
597 .set_clock = sdhci_set_clock,
598 .set_bus_width = sdhci_set_bus_width,
599 .reset = amd_sdhci_reset,
600 .set_uhs_signaling = sdhci_set_uhs_signaling,
601};
602
603static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
604 .ops = &sdhci_acpi_ops_amd,
605};
606
607static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
608 struct acpi_device *adev)
609{
610 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
611 struct sdhci_host *host = c->host;
612
613 sdhci_read_caps(host);
614 if (host->caps1 & SDHCI_SUPPORT_DDR50)
615 host->mmc->caps = MMC_CAP_1_8V_DDR;
616
617 if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
618 (host->mmc->caps & MMC_CAP_1_8V_DDR))
619 host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
620
621 /*
622 * There are two types of presets out in the wild:
623 * 1) Default/broken presets.
624 * These presets have two sets of problems:
625 * a) The clock divisor for SDR12, SDR25, and SDR50 is too small.
626 * This results in clock frequencies that are 2x higher than
627 * acceptable. i.e., SDR12 = 25 MHz, SDR25 = 50 MHz, SDR50 =
628 * 100 MHz.x
629 * b) The HS200 and HS400 driver strengths don't match.
630 * By default, the SDR104 preset register has a driver strength of
631 * A, but the (internal) HS400 preset register has a driver
632 * strength of B. As part of initializing HS400, HS200 tuning
633 * needs to be performed. Having different driver strengths
634 * between tuning and operation is wrong. It results in different
635 * rise/fall times that lead to incorrect sampling.
636 * 2) Firmware with properly initialized presets.
637 * These presets have proper clock divisors. i.e., SDR12 => 12MHz,
638 * SDR25 => 25 MHz, SDR50 => 50 MHz. Additionally the HS200 and
639 * HS400 preset driver strengths match.
640 *
641 * Enabling presets for HS400 doesn't work for the following reasons:
642 * 1) sdhci_set_ios has a hard coded list of timings that are used
643 * to determine if presets should be enabled.
644 * 2) sdhci_get_preset_value is using a non-standard register to
645 * read out HS400 presets. The AMD controller doesn't support this
646 * non-standard register. In fact, it doesn't expose the HS400
647 * preset register anywhere in the SDHCI memory map. This results
648 * in reading a garbage value and using the wrong presets.
649 *
650 * Since HS400 and HS200 presets must be identical, we could
651 * instead use the SDR104 preset register.
652 *
653 * If the above issues are resolved we could remove this quirk for
654 * firmware that has valid presets (i.e., SDR12 <= 12 MHz).
655 */
656 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
657
658 host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
659 host->mmc_host_ops.set_ios = amd_set_ios;
660 host->mmc_host_ops.execute_tuning = amd_sdhci_execute_tuning;
661 return 0;
662}
663
664static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
665 .chip = &sdhci_acpi_chip_amd,
666 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
667 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
668 SDHCI_QUIRK_32BIT_DMA_SIZE |
669 SDHCI_QUIRK_32BIT_ADMA_SIZE,
670 .quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
671 .probe_slot = sdhci_acpi_emmc_amd_probe_slot,
672 .priv_size = sizeof(struct amd_sdhci_host),
673};
674
675struct sdhci_acpi_uid_slot {
676 const char *hid;
677 const char *uid;
678 const struct sdhci_acpi_slot *slot;
679};
680
681static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
682 { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
683 { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
684 { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
685 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
686 { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
687 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
688 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
689 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
690 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
691 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
692 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
693 { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
694 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
695 { "PNP0D40" },
696 { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
697 { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
698 { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
699 { "AMDI0041", NULL, &sdhci_acpi_slot_amd_emmc },
700 { },
701};
702
703static const struct acpi_device_id sdhci_acpi_ids[] = {
704 { "80865ACA" },
705 { "80865ACC" },
706 { "80865AD0" },
707 { "80860F14" },
708 { "80860F16" },
709 { "INT33BB" },
710 { "INT33C6" },
711 { "INT3436" },
712 { "INT344D" },
713 { "PNP0D40" },
714 { "QCOM8051" },
715 { "QCOM8052" },
716 { "AMDI0040" },
717 { "AMDI0041" },
718 { },
719};
720MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
721
722static const struct dmi_system_id sdhci_acpi_quirks[] = {
723 {
724 /*
725 * The Lenovo Miix 320-10ICR has a bug in the _PS0 method of
726 * the SHC1 ACPI device, this bug causes it to reprogram the
727 * wrong LDO (DLDO3) to 1.8V if 1.8V modes are used and the
728 * card is (runtime) suspended + resumed. DLDO3 is used for
729 * the LCD and setting it to 1.8V causes the LCD to go black.
730 */
731 .matches = {
732 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
733 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
734 },
735 .driver_data = (void *)DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP,
736 },
737 {
738 /*
739 * The Acer Aspire Switch 10 (SW5-012) microSD slot always
740 * reports the card being write-protected even though microSD
741 * cards do not have a write-protect switch at all.
742 */
743 .matches = {
744 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
745 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
746 },
747 .driver_data = (void *)DMI_QUIRK_SD_NO_WRITE_PROTECT,
748 },
749 {
750 /*
751 * The Toshiba WT8-B's microSD slot always reports the card being
752 * write-protected.
753 */
754 .matches = {
755 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
756 DMI_MATCH(DMI_PRODUCT_NAME, "TOSHIBA ENCORE 2 WT8-B"),
757 },
758 .driver_data = (void *)DMI_QUIRK_SD_NO_WRITE_PROTECT,
759 },
760 {} /* Terminating entry */
761};
762
763static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(struct acpi_device *adev)
764{
765 const struct sdhci_acpi_uid_slot *u;
766
767 for (u = sdhci_acpi_uids; u->hid; u++) {
768 if (acpi_dev_hid_uid_match(adev, u->hid, u->uid))
769 return u->slot;
770 }
771 return NULL;
772}
773
774static int sdhci_acpi_probe(struct platform_device *pdev)
775{
776 struct device *dev = &pdev->dev;
777 const struct sdhci_acpi_slot *slot;
778 const struct dmi_system_id *id;
779 struct acpi_device *device;
780 struct sdhci_acpi_host *c;
781 struct sdhci_host *host;
782 struct resource *iomem;
783 resource_size_t len;
784 size_t priv_size;
785 int quirks = 0;
786 int err;
787
788 device = ACPI_COMPANION(dev);
789 if (!device)
790 return -ENODEV;
791
792 id = dmi_first_match(sdhci_acpi_quirks);
793 if (id)
794 quirks = (long)id->driver_data;
795
796 slot = sdhci_acpi_get_slot(device);
797
798 /* Power on the SDHCI controller and its children */
799 acpi_device_fix_up_power_extended(device);
800
801 if (sdhci_acpi_byt_defer(dev))
802 return -EPROBE_DEFER;
803
804 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
805 if (!iomem)
806 return -ENOMEM;
807
808 len = resource_size(iomem);
809 if (len < 0x100)
810 dev_err(dev, "Invalid iomem size!\n");
811
812 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
813 return -ENOMEM;
814
815 priv_size = slot ? slot->priv_size : 0;
816 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
817 if (IS_ERR(host))
818 return PTR_ERR(host);
819
820 c = sdhci_priv(host);
821 c->host = host;
822 c->slot = slot;
823 c->pdev = pdev;
824 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
825
826 platform_set_drvdata(pdev, c);
827
828 host->hw_name = "ACPI";
829 host->ops = &sdhci_acpi_ops_dflt;
830 host->irq = platform_get_irq(pdev, 0);
831 if (host->irq < 0) {
832 err = -EINVAL;
833 goto err_free;
834 }
835
836 host->ioaddr = devm_ioremap(dev, iomem->start,
837 resource_size(iomem));
838 if (host->ioaddr == NULL) {
839 err = -ENOMEM;
840 goto err_free;
841 }
842
843 if (c->slot) {
844 if (c->slot->probe_slot) {
845 err = c->slot->probe_slot(pdev, device);
846 if (err)
847 goto err_free;
848 }
849 if (c->slot->chip) {
850 host->ops = c->slot->chip->ops;
851 host->quirks |= c->slot->chip->quirks;
852 host->quirks2 |= c->slot->chip->quirks2;
853 host->mmc->caps |= c->slot->chip->caps;
854 host->mmc->caps2 |= c->slot->chip->caps2;
855 host->mmc->pm_caps |= c->slot->chip->pm_caps;
856 }
857 host->quirks |= c->slot->quirks;
858 host->quirks2 |= c->slot->quirks2;
859 host->mmc->caps |= c->slot->caps;
860 host->mmc->caps2 |= c->slot->caps2;
861 host->mmc->pm_caps |= c->slot->pm_caps;
862 }
863
864 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
865
866 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
867 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
868
869 err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0);
870 if (err) {
871 if (err == -EPROBE_DEFER)
872 goto err_free;
873 dev_warn(dev, "failed to setup card detect gpio\n");
874 c->use_runtime_pm = false;
875 }
876
877 if (quirks & DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP)
878 c->reset_signal_volt_on_suspend = true;
879
880 if (quirks & DMI_QUIRK_SD_NO_WRITE_PROTECT)
881 host->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
882 }
883
884 err = sdhci_setup_host(host);
885 if (err)
886 goto err_free;
887
888 if (c->slot && c->slot->setup_host) {
889 err = c->slot->setup_host(pdev);
890 if (err)
891 goto err_cleanup;
892 }
893
894 err = __sdhci_add_host(host);
895 if (err)
896 goto err_cleanup;
897
898 if (c->use_runtime_pm) {
899 pm_runtime_set_active(dev);
900 pm_suspend_ignore_children(dev, 1);
901 pm_runtime_set_autosuspend_delay(dev, 50);
902 pm_runtime_use_autosuspend(dev);
903 pm_runtime_enable(dev);
904 }
905
906 device_enable_async_suspend(dev);
907
908 return 0;
909
910err_cleanup:
911 sdhci_cleanup_host(c->host);
912err_free:
913 if (c->slot && c->slot->free_slot)
914 c->slot->free_slot(pdev);
915
916 sdhci_free_host(c->host);
917 return err;
918}
919
920static int sdhci_acpi_remove(struct platform_device *pdev)
921{
922 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
923 struct device *dev = &pdev->dev;
924 int dead;
925
926 if (c->use_runtime_pm) {
927 pm_runtime_get_sync(dev);
928 pm_runtime_disable(dev);
929 pm_runtime_put_noidle(dev);
930 }
931
932 if (c->slot && c->slot->remove_slot)
933 c->slot->remove_slot(pdev);
934
935 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
936 sdhci_remove_host(c->host, dead);
937
938 if (c->slot && c->slot->free_slot)
939 c->slot->free_slot(pdev);
940
941 sdhci_free_host(c->host);
942
943 return 0;
944}
945
946static void __maybe_unused sdhci_acpi_reset_signal_voltage_if_needed(
947 struct device *dev)
948{
949 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
950 struct sdhci_host *host = c->host;
951
952 if (c->is_intel && c->reset_signal_volt_on_suspend &&
953 host->mmc->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_330) {
954 struct intel_host *intel_host = sdhci_acpi_priv(c);
955 unsigned int fn = INTEL_DSM_V33_SWITCH;
956 u32 result = 0;
957
958 intel_dsm(intel_host, dev, fn, &result);
959 }
960}
961
962#ifdef CONFIG_PM_SLEEP
963
964static int sdhci_acpi_suspend(struct device *dev)
965{
966 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
967 struct sdhci_host *host = c->host;
968 int ret;
969
970 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
971 mmc_retune_needed(host->mmc);
972
973 ret = sdhci_suspend_host(host);
974 if (ret)
975 return ret;
976
977 sdhci_acpi_reset_signal_voltage_if_needed(dev);
978 return 0;
979}
980
981static int sdhci_acpi_resume(struct device *dev)
982{
983 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
984
985 sdhci_acpi_byt_setting(&c->pdev->dev);
986
987 return sdhci_resume_host(c->host);
988}
989
990#endif
991
992#ifdef CONFIG_PM
993
994static int sdhci_acpi_runtime_suspend(struct device *dev)
995{
996 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
997 struct sdhci_host *host = c->host;
998 int ret;
999
1000 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1001 mmc_retune_needed(host->mmc);
1002
1003 ret = sdhci_runtime_suspend_host(host);
1004 if (ret)
1005 return ret;
1006
1007 sdhci_acpi_reset_signal_voltage_if_needed(dev);
1008 return 0;
1009}
1010
1011static int sdhci_acpi_runtime_resume(struct device *dev)
1012{
1013 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
1014
1015 sdhci_acpi_byt_setting(&c->pdev->dev);
1016
1017 return sdhci_runtime_resume_host(c->host, 0);
1018}
1019
1020#endif
1021
1022static const struct dev_pm_ops sdhci_acpi_pm_ops = {
1023 SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
1024 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
1025 sdhci_acpi_runtime_resume, NULL)
1026};
1027
1028static struct platform_driver sdhci_acpi_driver = {
1029 .driver = {
1030 .name = "sdhci-acpi",
1031 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1032 .acpi_match_table = sdhci_acpi_ids,
1033 .pm = &sdhci_acpi_pm_ops,
1034 },
1035 .probe = sdhci_acpi_probe,
1036 .remove = sdhci_acpi_remove,
1037};
1038
1039module_platform_driver(sdhci_acpi_driver);
1040
1041MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
1042MODULE_AUTHOR("Adrian Hunter");
1043MODULE_LICENSE("GPL v2");
1/*
2 * Secure Digital Host Controller Interface ACPI driver.
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <linux/init.h>
22#include <linux/export.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/dma-mapping.h>
29#include <linux/compiler.h>
30#include <linux/stddef.h>
31#include <linux/bitops.h>
32#include <linux/types.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/acpi.h>
36#include <linux/pm.h>
37#include <linux/pm_runtime.h>
38#include <linux/delay.h>
39
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
42#include <linux/mmc/slot-gpio.h>
43
44#ifdef CONFIG_X86
45#include <asm/cpu_device_id.h>
46#include <asm/intel-family.h>
47#include <asm/iosf_mbi.h>
48#include <linux/pci.h>
49#endif
50
51#include "sdhci.h"
52
53enum {
54 SDHCI_ACPI_SD_CD = BIT(0),
55 SDHCI_ACPI_RUNTIME_PM = BIT(1),
56 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
57};
58
59struct sdhci_acpi_chip {
60 const struct sdhci_ops *ops;
61 unsigned int quirks;
62 unsigned int quirks2;
63 unsigned long caps;
64 unsigned int caps2;
65 mmc_pm_flag_t pm_caps;
66};
67
68struct sdhci_acpi_slot {
69 const struct sdhci_acpi_chip *chip;
70 unsigned int quirks;
71 unsigned int quirks2;
72 unsigned long caps;
73 unsigned int caps2;
74 mmc_pm_flag_t pm_caps;
75 unsigned int flags;
76 size_t priv_size;
77 int (*probe_slot)(struct platform_device *, const char *, const char *);
78 int (*remove_slot)(struct platform_device *);
79 int (*setup_host)(struct platform_device *pdev);
80};
81
82struct sdhci_acpi_host {
83 struct sdhci_host *host;
84 const struct sdhci_acpi_slot *slot;
85 struct platform_device *pdev;
86 bool use_runtime_pm;
87 unsigned long private[0] ____cacheline_aligned;
88};
89
90static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
91{
92 return (void *)c->private;
93}
94
95static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
96{
97 return c->slot && (c->slot->flags & flag);
98}
99
100#define INTEL_DSM_HS_CAPS_SDR25 BIT(0)
101#define INTEL_DSM_HS_CAPS_DDR50 BIT(1)
102#define INTEL_DSM_HS_CAPS_SDR50 BIT(2)
103#define INTEL_DSM_HS_CAPS_SDR104 BIT(3)
104
105enum {
106 INTEL_DSM_FNS = 0,
107 INTEL_DSM_V18_SWITCH = 3,
108 INTEL_DSM_V33_SWITCH = 4,
109 INTEL_DSM_HS_CAPS = 8,
110};
111
112struct intel_host {
113 u32 dsm_fns;
114 u32 hs_caps;
115};
116
117static const guid_t intel_dsm_guid =
118 GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
119 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
120
121static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
122 unsigned int fn, u32 *result)
123{
124 union acpi_object *obj;
125 int err = 0;
126
127 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
128 if (!obj)
129 return -EOPNOTSUPP;
130
131 if (obj->type == ACPI_TYPE_INTEGER) {
132 *result = obj->integer.value;
133 } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
134 size_t len = min_t(size_t, obj->buffer.length, 4);
135
136 *result = 0;
137 memcpy(result, obj->buffer.pointer, len);
138 } else {
139 dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
140 __func__, fn, obj->type, obj->buffer.length);
141 err = -EINVAL;
142 }
143
144 ACPI_FREE(obj);
145
146 return err;
147}
148
149static int intel_dsm(struct intel_host *intel_host, struct device *dev,
150 unsigned int fn, u32 *result)
151{
152 if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
153 return -EOPNOTSUPP;
154
155 return __intel_dsm(intel_host, dev, fn, result);
156}
157
158static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
159 struct mmc_host *mmc)
160{
161 int err;
162
163 intel_host->hs_caps = ~0;
164
165 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
166 if (err) {
167 pr_debug("%s: DSM not supported, error %d\n",
168 mmc_hostname(mmc), err);
169 return;
170 }
171
172 pr_debug("%s: DSM function mask %#x\n",
173 mmc_hostname(mmc), intel_host->dsm_fns);
174
175 intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps);
176}
177
178static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
179 struct mmc_ios *ios)
180{
181 struct device *dev = mmc_dev(mmc);
182 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
183 struct intel_host *intel_host = sdhci_acpi_priv(c);
184 unsigned int fn;
185 u32 result = 0;
186 int err;
187
188 err = sdhci_start_signal_voltage_switch(mmc, ios);
189 if (err)
190 return err;
191
192 switch (ios->signal_voltage) {
193 case MMC_SIGNAL_VOLTAGE_330:
194 fn = INTEL_DSM_V33_SWITCH;
195 break;
196 case MMC_SIGNAL_VOLTAGE_180:
197 fn = INTEL_DSM_V18_SWITCH;
198 break;
199 default:
200 return 0;
201 }
202
203 err = intel_dsm(intel_host, dev, fn, &result);
204 pr_debug("%s: %s DSM fn %u error %d result %u\n",
205 mmc_hostname(mmc), __func__, fn, err, result);
206
207 return 0;
208}
209
210static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
211{
212 u8 reg;
213
214 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
215 reg |= 0x10;
216 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
217 /* For eMMC, minimum is 1us but give it 9us for good measure */
218 udelay(9);
219 reg &= ~0x10;
220 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
221 /* For eMMC, minimum is 200us but give it 300us for good measure */
222 usleep_range(300, 1000);
223}
224
225static const struct sdhci_ops sdhci_acpi_ops_dflt = {
226 .set_clock = sdhci_set_clock,
227 .set_bus_width = sdhci_set_bus_width,
228 .reset = sdhci_reset,
229 .set_uhs_signaling = sdhci_set_uhs_signaling,
230};
231
232static const struct sdhci_ops sdhci_acpi_ops_int = {
233 .set_clock = sdhci_set_clock,
234 .set_bus_width = sdhci_set_bus_width,
235 .reset = sdhci_reset,
236 .set_uhs_signaling = sdhci_set_uhs_signaling,
237 .hw_reset = sdhci_acpi_int_hw_reset,
238};
239
240static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
241 .ops = &sdhci_acpi_ops_int,
242};
243
244#ifdef CONFIG_X86
245
246static bool sdhci_acpi_byt(void)
247{
248 static const struct x86_cpu_id byt[] = {
249 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
250 {}
251 };
252
253 return x86_match_cpu(byt);
254}
255
256static bool sdhci_acpi_cht(void)
257{
258 static const struct x86_cpu_id cht[] = {
259 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
260 {}
261 };
262
263 return x86_match_cpu(cht);
264}
265
266#define BYT_IOSF_SCCEP 0x63
267#define BYT_IOSF_OCP_NETCTRL0 0x1078
268#define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
269
270static void sdhci_acpi_byt_setting(struct device *dev)
271{
272 u32 val = 0;
273
274 if (!sdhci_acpi_byt())
275 return;
276
277 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
278 &val)) {
279 dev_err(dev, "%s read error\n", __func__);
280 return;
281 }
282
283 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
284 return;
285
286 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
287
288 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
289 val)) {
290 dev_err(dev, "%s write error\n", __func__);
291 return;
292 }
293
294 dev_dbg(dev, "%s completed\n", __func__);
295}
296
297static bool sdhci_acpi_byt_defer(struct device *dev)
298{
299 if (!sdhci_acpi_byt())
300 return false;
301
302 if (!iosf_mbi_available())
303 return true;
304
305 sdhci_acpi_byt_setting(dev);
306
307 return false;
308}
309
310static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
311 unsigned int slot, unsigned int parent_slot)
312{
313 struct pci_dev *dev, *parent, *from = NULL;
314
315 while (1) {
316 dev = pci_get_device(vendor, device, from);
317 pci_dev_put(from);
318 if (!dev)
319 break;
320 parent = pci_upstream_bridge(dev);
321 if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
322 parent && PCI_SLOT(parent->devfn) == parent_slot &&
323 !pci_upstream_bridge(parent)) {
324 pci_dev_put(dev);
325 return true;
326 }
327 from = dev;
328 }
329
330 return false;
331}
332
333/*
334 * GPDwin uses PCI wifi which conflicts with SDIO's use of
335 * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
336 * problematic, but since SDIO is only used for wifi, the presence of the PCI
337 * wifi card in the expected slot with an ACPI companion node, is used to
338 * indicate that acpi_device_fix_up_power() should be avoided.
339 */
340static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
341 const char *uid)
342{
343 return sdhci_acpi_cht() &&
344 !strcmp(hid, "80860F14") &&
345 !strcmp(uid, "2") &&
346 sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
347}
348
349#else
350
351static inline void sdhci_acpi_byt_setting(struct device *dev)
352{
353}
354
355static inline bool sdhci_acpi_byt_defer(struct device *dev)
356{
357 return false;
358}
359
360static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
361 const char *uid)
362{
363 return false;
364}
365
366#endif
367
368static int bxt_get_cd(struct mmc_host *mmc)
369{
370 int gpio_cd = mmc_gpio_get_cd(mmc);
371 struct sdhci_host *host = mmc_priv(mmc);
372 unsigned long flags;
373 int ret = 0;
374
375 if (!gpio_cd)
376 return 0;
377
378 spin_lock_irqsave(&host->lock, flags);
379
380 if (host->flags & SDHCI_DEVICE_DEAD)
381 goto out;
382
383 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
384out:
385 spin_unlock_irqrestore(&host->lock, flags);
386
387 return ret;
388}
389
390static int intel_probe_slot(struct platform_device *pdev, const char *hid,
391 const char *uid)
392{
393 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
394 struct intel_host *intel_host = sdhci_acpi_priv(c);
395 struct sdhci_host *host = c->host;
396
397 if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
398 sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
399 sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
400 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
401
402 if (hid && !strcmp(hid, "80865ACA"))
403 host->mmc_host_ops.get_cd = bxt_get_cd;
404
405 intel_dsm_init(intel_host, &pdev->dev, host->mmc);
406
407 host->mmc_host_ops.start_signal_voltage_switch =
408 intel_start_signal_voltage_switch;
409
410 return 0;
411}
412
413static int intel_setup_host(struct platform_device *pdev)
414{
415 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
416 struct intel_host *intel_host = sdhci_acpi_priv(c);
417
418 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25))
419 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25;
420
421 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50))
422 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50;
423
424 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50))
425 c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50;
426
427 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104))
428 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
429
430 return 0;
431}
432
433static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
434 .chip = &sdhci_acpi_chip_int,
435 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
436 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
437 MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
438 .flags = SDHCI_ACPI_RUNTIME_PM,
439 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
440 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
441 SDHCI_QUIRK2_STOP_WITH_TC |
442 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
443 .probe_slot = intel_probe_slot,
444 .setup_host = intel_setup_host,
445 .priv_size = sizeof(struct intel_host),
446};
447
448static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
449 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
450 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
451 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
452 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
453 MMC_CAP_WAIT_WHILE_BUSY,
454 .flags = SDHCI_ACPI_RUNTIME_PM,
455 .pm_caps = MMC_PM_KEEP_POWER,
456 .probe_slot = intel_probe_slot,
457 .setup_host = intel_setup_host,
458 .priv_size = sizeof(struct intel_host),
459};
460
461static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
462 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
463 SDHCI_ACPI_RUNTIME_PM,
464 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
465 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
466 SDHCI_QUIRK2_STOP_WITH_TC,
467 .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
468 .probe_slot = intel_probe_slot,
469 .setup_host = intel_setup_host,
470 .priv_size = sizeof(struct intel_host),
471};
472
473static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
474 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
475 .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
476 .caps = MMC_CAP_NONREMOVABLE,
477};
478
479static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
480 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
481 .caps = MMC_CAP_NONREMOVABLE,
482};
483
484/* AMD sdhci reset dll register. */
485#define SDHCI_AMD_RESET_DLL_REGISTER 0x908
486
487static int amd_select_drive_strength(struct mmc_card *card,
488 unsigned int max_dtr, int host_drv,
489 int card_drv, int *drv_type)
490{
491 return MMC_SET_DRIVER_TYPE_A;
492}
493
494static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host)
495{
496 /* AMD Platform requires dll setting */
497 sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
498 usleep_range(10, 20);
499 sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
500}
501
502/*
503 * For AMD Platform it is required to disable the tuning
504 * bit first controller to bring to HS Mode from HS200
505 * mode, later enable to tune to HS400 mode.
506 */
507static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
508{
509 struct sdhci_host *host = mmc_priv(mmc);
510 unsigned int old_timing = host->timing;
511
512 sdhci_set_ios(mmc, ios);
513 if (old_timing == MMC_TIMING_MMC_HS200 &&
514 ios->timing == MMC_TIMING_MMC_HS)
515 sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2);
516 if (old_timing != MMC_TIMING_MMC_HS400 &&
517 ios->timing == MMC_TIMING_MMC_HS400) {
518 sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2);
519 sdhci_acpi_amd_hs400_dll(host);
520 }
521}
522
523static const struct sdhci_ops sdhci_acpi_ops_amd = {
524 .set_clock = sdhci_set_clock,
525 .set_bus_width = sdhci_set_bus_width,
526 .reset = sdhci_reset,
527 .set_uhs_signaling = sdhci_set_uhs_signaling,
528};
529
530static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
531 .ops = &sdhci_acpi_ops_amd,
532};
533
534static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
535 const char *hid, const char *uid)
536{
537 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
538 struct sdhci_host *host = c->host;
539
540 sdhci_read_caps(host);
541 if (host->caps1 & SDHCI_SUPPORT_DDR50)
542 host->mmc->caps = MMC_CAP_1_8V_DDR;
543
544 if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
545 (host->mmc->caps & MMC_CAP_1_8V_DDR))
546 host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
547
548 host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
549 host->mmc_host_ops.set_ios = amd_set_ios;
550 return 0;
551}
552
553static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
554 .chip = &sdhci_acpi_chip_amd,
555 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
556 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE |
557 SDHCI_QUIRK_32BIT_ADMA_SIZE,
558 .probe_slot = sdhci_acpi_emmc_amd_probe_slot,
559};
560
561struct sdhci_acpi_uid_slot {
562 const char *hid;
563 const char *uid;
564 const struct sdhci_acpi_slot *slot;
565};
566
567static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
568 { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
569 { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
570 { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
571 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
572 { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
573 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
574 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
575 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
576 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
577 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
578 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
579 { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
580 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
581 { "PNP0D40" },
582 { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
583 { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
584 { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
585 { },
586};
587
588static const struct acpi_device_id sdhci_acpi_ids[] = {
589 { "80865ACA" },
590 { "80865ACC" },
591 { "80865AD0" },
592 { "80860F14" },
593 { "80860F16" },
594 { "INT33BB" },
595 { "INT33C6" },
596 { "INT3436" },
597 { "INT344D" },
598 { "PNP0D40" },
599 { "QCOM8051" },
600 { "QCOM8052" },
601 { "AMDI0040" },
602 { },
603};
604MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
605
606static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
607 const char *uid)
608{
609 const struct sdhci_acpi_uid_slot *u;
610
611 for (u = sdhci_acpi_uids; u->hid; u++) {
612 if (strcmp(u->hid, hid))
613 continue;
614 if (!u->uid)
615 return u->slot;
616 if (uid && !strcmp(u->uid, uid))
617 return u->slot;
618 }
619 return NULL;
620}
621
622static int sdhci_acpi_probe(struct platform_device *pdev)
623{
624 struct device *dev = &pdev->dev;
625 const struct sdhci_acpi_slot *slot;
626 struct acpi_device *device, *child;
627 struct sdhci_acpi_host *c;
628 struct sdhci_host *host;
629 struct resource *iomem;
630 resource_size_t len;
631 size_t priv_size;
632 const char *hid;
633 const char *uid;
634 int err;
635
636 device = ACPI_COMPANION(dev);
637 if (!device)
638 return -ENODEV;
639
640 hid = acpi_device_hid(device);
641 uid = acpi_device_uid(device);
642
643 slot = sdhci_acpi_get_slot(hid, uid);
644
645 /* Power on the SDHCI controller and its children */
646 acpi_device_fix_up_power(device);
647 if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
648 list_for_each_entry(child, &device->children, node)
649 if (child->status.present && child->status.enabled)
650 acpi_device_fix_up_power(child);
651 }
652
653 if (sdhci_acpi_byt_defer(dev))
654 return -EPROBE_DEFER;
655
656 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
657 if (!iomem)
658 return -ENOMEM;
659
660 len = resource_size(iomem);
661 if (len < 0x100)
662 dev_err(dev, "Invalid iomem size!\n");
663
664 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
665 return -ENOMEM;
666
667 priv_size = slot ? slot->priv_size : 0;
668 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
669 if (IS_ERR(host))
670 return PTR_ERR(host);
671
672 c = sdhci_priv(host);
673 c->host = host;
674 c->slot = slot;
675 c->pdev = pdev;
676 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
677
678 platform_set_drvdata(pdev, c);
679
680 host->hw_name = "ACPI";
681 host->ops = &sdhci_acpi_ops_dflt;
682 host->irq = platform_get_irq(pdev, 0);
683 if (host->irq < 0) {
684 err = -EINVAL;
685 goto err_free;
686 }
687
688 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
689 resource_size(iomem));
690 if (host->ioaddr == NULL) {
691 err = -ENOMEM;
692 goto err_free;
693 }
694
695 if (c->slot) {
696 if (c->slot->probe_slot) {
697 err = c->slot->probe_slot(pdev, hid, uid);
698 if (err)
699 goto err_free;
700 }
701 if (c->slot->chip) {
702 host->ops = c->slot->chip->ops;
703 host->quirks |= c->slot->chip->quirks;
704 host->quirks2 |= c->slot->chip->quirks2;
705 host->mmc->caps |= c->slot->chip->caps;
706 host->mmc->caps2 |= c->slot->chip->caps2;
707 host->mmc->pm_caps |= c->slot->chip->pm_caps;
708 }
709 host->quirks |= c->slot->quirks;
710 host->quirks2 |= c->slot->quirks2;
711 host->mmc->caps |= c->slot->caps;
712 host->mmc->caps2 |= c->slot->caps2;
713 host->mmc->pm_caps |= c->slot->pm_caps;
714 }
715
716 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
717
718 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
719 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
720
721 err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
722 if (err) {
723 if (err == -EPROBE_DEFER)
724 goto err_free;
725 dev_warn(dev, "failed to setup card detect gpio\n");
726 c->use_runtime_pm = false;
727 }
728 }
729
730 err = sdhci_setup_host(host);
731 if (err)
732 goto err_free;
733
734 if (c->slot && c->slot->setup_host) {
735 err = c->slot->setup_host(pdev);
736 if (err)
737 goto err_cleanup;
738 }
739
740 err = __sdhci_add_host(host);
741 if (err)
742 goto err_cleanup;
743
744 if (c->use_runtime_pm) {
745 pm_runtime_set_active(dev);
746 pm_suspend_ignore_children(dev, 1);
747 pm_runtime_set_autosuspend_delay(dev, 50);
748 pm_runtime_use_autosuspend(dev);
749 pm_runtime_enable(dev);
750 }
751
752 device_enable_async_suspend(dev);
753
754 return 0;
755
756err_cleanup:
757 sdhci_cleanup_host(c->host);
758err_free:
759 sdhci_free_host(c->host);
760 return err;
761}
762
763static int sdhci_acpi_remove(struct platform_device *pdev)
764{
765 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
766 struct device *dev = &pdev->dev;
767 int dead;
768
769 if (c->use_runtime_pm) {
770 pm_runtime_get_sync(dev);
771 pm_runtime_disable(dev);
772 pm_runtime_put_noidle(dev);
773 }
774
775 if (c->slot && c->slot->remove_slot)
776 c->slot->remove_slot(pdev);
777
778 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
779 sdhci_remove_host(c->host, dead);
780 sdhci_free_host(c->host);
781
782 return 0;
783}
784
785#ifdef CONFIG_PM_SLEEP
786
787static int sdhci_acpi_suspend(struct device *dev)
788{
789 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
790 struct sdhci_host *host = c->host;
791
792 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
793 mmc_retune_needed(host->mmc);
794
795 return sdhci_suspend_host(host);
796}
797
798static int sdhci_acpi_resume(struct device *dev)
799{
800 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
801
802 sdhci_acpi_byt_setting(&c->pdev->dev);
803
804 return sdhci_resume_host(c->host);
805}
806
807#endif
808
809#ifdef CONFIG_PM
810
811static int sdhci_acpi_runtime_suspend(struct device *dev)
812{
813 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
814 struct sdhci_host *host = c->host;
815
816 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
817 mmc_retune_needed(host->mmc);
818
819 return sdhci_runtime_suspend_host(host);
820}
821
822static int sdhci_acpi_runtime_resume(struct device *dev)
823{
824 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
825
826 sdhci_acpi_byt_setting(&c->pdev->dev);
827
828 return sdhci_runtime_resume_host(c->host);
829}
830
831#endif
832
833static const struct dev_pm_ops sdhci_acpi_pm_ops = {
834 SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
835 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
836 sdhci_acpi_runtime_resume, NULL)
837};
838
839static struct platform_driver sdhci_acpi_driver = {
840 .driver = {
841 .name = "sdhci-acpi",
842 .acpi_match_table = sdhci_acpi_ids,
843 .pm = &sdhci_acpi_pm_ops,
844 },
845 .probe = sdhci_acpi_probe,
846 .remove = sdhci_acpi_remove,
847};
848
849module_platform_driver(sdhci_acpi_driver);
850
851MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
852MODULE_AUTHOR("Adrian Hunter");
853MODULE_LICENSE("GPL v2");