Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Google, Inc |
| 4 | * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com> |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 13 | #include <sdhci.h> |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 14 | #include <acpi/acpigen.h> |
| 15 | #include <acpi/acpi_device.h> |
| 16 | #include <acpi/acpi_dp.h> |
| 17 | #include <asm-generic/gpio.h> |
| 18 | #include <dm/acpi.h> |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 19 | |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 20 | struct pci_mmc_plat { |
| 21 | struct mmc_config cfg; |
| 22 | struct mmc mmc; |
| 23 | }; |
| 24 | |
| 25 | struct pci_mmc_priv { |
| 26 | struct sdhci_host host; |
| 27 | void *base; |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 28 | struct gpio_desc cd_gpio; |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static int pci_mmc_probe(struct udevice *dev) |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 32 | { |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 33 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 34 | struct pci_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 35 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 36 | struct sdhci_host *host = &priv->host; |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 37 | int ret; |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 38 | |
Bernhard Messerklinger | 0851f34 | 2018-02-15 09:09:43 +0100 | [diff] [blame] | 39 | host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, |
| 40 | PCI_REGION_MEM); |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 41 | host->name = dev->name; |
Peng Fan | a5abe15 | 2019-08-06 02:47:56 +0000 | [diff] [blame] | 42 | host->mmc = &plat->mmc; |
| 43 | host->mmc->dev = dev; |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 44 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 45 | if (ret) |
| 46 | return ret; |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 47 | host->mmc->priv = &priv->host; |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 48 | upriv->mmc = host->mmc; |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 49 | |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 50 | return sdhci_probe(dev); |
Simon Glass | 91785f7 | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 51 | } |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 52 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 53 | static int pci_mmc_of_to_plat(struct udevice *dev) |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 54 | { |
Harm Berntsen | 7b4fe6d | 2020-11-06 12:20:44 +0000 | [diff] [blame] | 55 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
| 56 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 57 | |
Harm Berntsen | 7b4fe6d | 2020-11-06 12:20:44 +0000 | [diff] [blame] | 58 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN); |
| 59 | } |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 64 | static int pci_mmc_bind(struct udevice *dev) |
| 65 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 66 | struct pci_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 67 | |
| 68 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 69 | } |
| 70 | |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 71 | static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev, |
| 72 | struct acpi_ctx *ctx) |
| 73 | { |
| 74 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 75 | char path[ACPI_PATH_MAX]; |
| 76 | struct acpi_gpio gpio; |
| 77 | struct acpi_dp *dp; |
| 78 | int ret; |
| 79 | |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 80 | if (!dev_has_ofnode(dev)) |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | ret = gpio_get_acpi(&priv->cd_gpio, &gpio); |
| 84 | if (ret) |
| 85 | return log_msg_ret("gpio", ret); |
| 86 | gpio.type = ACPI_GPIO_TYPE_INTERRUPT; |
| 87 | gpio.pull = ACPI_GPIO_PULL_NONE; |
| 88 | gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
| 89 | gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH; |
| 90 | gpio.irq.shared = ACPI_IRQ_SHARED; |
| 91 | gpio.irq.wake = ACPI_IRQ_WAKE; |
| 92 | gpio.interrupt_debounce_timeout = 10000; /* 100ms */ |
| 93 | |
| 94 | /* Use device path as the Scope for the SSDT */ |
| 95 | ret = acpi_device_path(dev, path, sizeof(path)); |
| 96 | if (ret) |
| 97 | return log_msg_ret("path", ret); |
| 98 | acpigen_write_scope(ctx, path); |
| 99 | acpigen_write_name(ctx, "_CRS"); |
| 100 | |
| 101 | /* Write GpioInt() as default (if set) or custom from devicetree */ |
| 102 | acpigen_write_resourcetemplate_header(ctx); |
| 103 | acpi_device_write_gpio(ctx, &gpio); |
| 104 | acpigen_write_resourcetemplate_footer(ctx); |
| 105 | |
| 106 | /* Bind the cd-gpio name to the GpioInt() resource */ |
| 107 | dp = acpi_dp_new_table("_DSD"); |
| 108 | if (!dp) |
| 109 | return -ENOMEM; |
| 110 | acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1); |
| 111 | ret = acpi_dp_write(ctx, dp); |
| 112 | if (ret) |
| 113 | return log_msg_ret("cd", ret); |
| 114 | |
| 115 | acpigen_pop_len(ctx); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | struct acpi_ops pci_mmc_acpi_ops = { |
| 121 | .fill_ssdt = pci_mmc_acpi_fill_ssdt, |
| 122 | }; |
| 123 | |
| 124 | static const struct udevice_id pci_mmc_match[] = { |
| 125 | { .compatible = "intel,apl-sd" }, |
| 126 | { } |
| 127 | }; |
| 128 | |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 129 | U_BOOT_DRIVER(pci_mmc) = { |
| 130 | .name = "pci_mmc", |
| 131 | .id = UCLASS_MMC, |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 132 | .of_match = pci_mmc_match, |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 133 | .bind = pci_mmc_bind, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 134 | .of_to_plat = pci_mmc_of_to_plat, |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 135 | .probe = pci_mmc_probe, |
| 136 | .ops = &sdhci_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 137 | .priv_auto = sizeof(struct pci_mmc_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 138 | .plat_auto = sizeof(struct pci_mmc_plat), |
Simon Glass | dba7ee4 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 139 | ACPI_OPS_PTR(&pci_mmc_acpi_ops) |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct pci_device_id mmc_supported[] = { |
Bin Meng | a191cca | 2017-08-09 00:21:00 -0700 | [diff] [blame] | 143 | { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) }, |
Simon Glass | b7c6bae | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 144 | {}, |
| 145 | }; |
| 146 | |
| 147 | U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported); |