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