Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 509805b | 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 | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 10 | #include <log.h> |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 13 | #include <sdhci.h> |
Simon Glass | 5eb6639 | 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 | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 19 | |
Simon Glass | 828b725 | 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 | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 28 | struct gpio_desc cd_gpio; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static int pci_mmc_probe(struct udevice *dev) |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 32 | { |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 33 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 34 | struct pci_mmc_plat *plat = dev_get_platdata(dev); |
| 35 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 36 | struct sdhci_host *host = &priv->host; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 37 | int ret; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 38 | |
Bernhard Messerklinger | da5cfec | 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 | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 41 | host->name = dev->name; |
Peng Fan | 798f2e8 | 2019-08-06 02:47:56 +0000 | [diff] [blame] | 42 | host->mmc = &plat->mmc; |
| 43 | host->mmc->dev = dev; |
Simon Glass | 828b725 | 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 | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 47 | host->mmc->priv = &priv->host; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 48 | upriv->mmc = host->mmc; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 49 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 50 | return sdhci_probe(dev); |
| 51 | } |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 52 | |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 53 | static int pci_mmc_ofdata_to_platdata(struct udevice *dev) |
| 54 | { |
| 55 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 56 | |
| 57 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 62 | static int pci_mmc_bind(struct udevice *dev) |
| 63 | { |
| 64 | struct pci_mmc_plat *plat = dev_get_platdata(dev); |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 65 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 66 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 67 | } |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 68 | |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 69 | static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev, |
| 70 | struct acpi_ctx *ctx) |
| 71 | { |
| 72 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 73 | char path[ACPI_PATH_MAX]; |
| 74 | struct acpi_gpio gpio; |
| 75 | struct acpi_dp *dp; |
| 76 | int ret; |
| 77 | |
| 78 | if (!dev_of_valid(dev)) |
| 79 | return 0; |
| 80 | |
| 81 | ret = gpio_get_acpi(&priv->cd_gpio, &gpio); |
| 82 | if (ret) |
| 83 | return log_msg_ret("gpio", ret); |
| 84 | gpio.type = ACPI_GPIO_TYPE_INTERRUPT; |
| 85 | gpio.pull = ACPI_GPIO_PULL_NONE; |
| 86 | gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
| 87 | gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH; |
| 88 | gpio.irq.shared = ACPI_IRQ_SHARED; |
| 89 | gpio.irq.wake = ACPI_IRQ_WAKE; |
| 90 | gpio.interrupt_debounce_timeout = 10000; /* 100ms */ |
| 91 | |
| 92 | /* Use device path as the Scope for the SSDT */ |
| 93 | ret = acpi_device_path(dev, path, sizeof(path)); |
| 94 | if (ret) |
| 95 | return log_msg_ret("path", ret); |
| 96 | acpigen_write_scope(ctx, path); |
| 97 | acpigen_write_name(ctx, "_CRS"); |
| 98 | |
| 99 | /* Write GpioInt() as default (if set) or custom from devicetree */ |
| 100 | acpigen_write_resourcetemplate_header(ctx); |
| 101 | acpi_device_write_gpio(ctx, &gpio); |
| 102 | acpigen_write_resourcetemplate_footer(ctx); |
| 103 | |
| 104 | /* Bind the cd-gpio name to the GpioInt() resource */ |
| 105 | dp = acpi_dp_new_table("_DSD"); |
| 106 | if (!dp) |
| 107 | return -ENOMEM; |
| 108 | acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1); |
| 109 | ret = acpi_dp_write(ctx, dp); |
| 110 | if (ret) |
| 111 | return log_msg_ret("cd", ret); |
| 112 | |
| 113 | acpigen_pop_len(ctx); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | struct acpi_ops pci_mmc_acpi_ops = { |
| 119 | .fill_ssdt = pci_mmc_acpi_fill_ssdt, |
| 120 | }; |
| 121 | |
| 122 | static const struct udevice_id pci_mmc_match[] = { |
| 123 | { .compatible = "intel,apl-sd" }, |
| 124 | { } |
| 125 | }; |
| 126 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 127 | U_BOOT_DRIVER(pci_mmc) = { |
| 128 | .name = "pci_mmc", |
| 129 | .id = UCLASS_MMC, |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 130 | .of_match = pci_mmc_match, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 131 | .bind = pci_mmc_bind, |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 132 | .ofdata_to_platdata = pci_mmc_ofdata_to_platdata, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 133 | .probe = pci_mmc_probe, |
| 134 | .ops = &sdhci_ops, |
| 135 | .priv_auto_alloc_size = sizeof(struct pci_mmc_priv), |
| 136 | .platdata_auto_alloc_size = sizeof(struct pci_mmc_plat), |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame^] | 137 | ACPI_OPS_PTR(&pci_mmc_acpi_ops) |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static struct pci_device_id mmc_supported[] = { |
Bin Meng | 77520f8 | 2017-08-09 00:21:00 -0700 | [diff] [blame] | 141 | { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) }, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 142 | {}, |
| 143 | }; |
| 144 | |
| 145 | U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported); |