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 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 11 | #include <mapmem.h> |
Simon Glass | 74aaa3f | 2021-01-13 20:29:54 -0700 | [diff] [blame] | 12 | #include <mmc.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 | 5c9c49e | 2021-01-13 20:29:52 -0700 | [diff] [blame] | 20 | /* Type of MMC device */ |
| 21 | enum { |
| 22 | TYPE_SD, |
| 23 | TYPE_EMMC, |
| 24 | }; |
| 25 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 26 | struct pci_mmc_plat { |
| 27 | struct mmc_config cfg; |
| 28 | struct mmc mmc; |
| 29 | }; |
| 30 | |
| 31 | struct pci_mmc_priv { |
| 32 | struct sdhci_host host; |
| 33 | void *base; |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 34 | struct gpio_desc cd_gpio; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static int pci_mmc_probe(struct udevice *dev) |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 38 | { |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 39 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 40 | struct pci_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 41 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 42 | struct sdhci_host *host = &priv->host; |
Simon Glass | 74aaa3f | 2021-01-13 20:29:54 -0700 | [diff] [blame] | 43 | struct blk_desc *desc; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 44 | int ret; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 45 | |
Simon Glass | 74aaa3f | 2021-01-13 20:29:54 -0700 | [diff] [blame] | 46 | ret = mmc_of_parse(dev, &plat->cfg); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | desc = mmc_get_blk_desc(&plat->mmc); |
| 50 | desc->removable = !(plat->cfg.host_caps & MMC_CAP_NONREMOVABLE); |
| 51 | |
Bin Meng | c8b284e | 2023-10-11 19:00:52 +0800 | [diff] [blame] | 52 | host->ioaddr = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, |
| 53 | PCI_REGION_TYPE, PCI_REGION_MEM); |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 54 | host->name = dev->name; |
Simon Glass | 2001f4a | 2021-03-15 18:00:08 +1300 | [diff] [blame] | 55 | host->cd_gpio = priv->cd_gpio; |
Peng Fan | 798f2e8 | 2019-08-06 02:47:56 +0000 | [diff] [blame] | 56 | host->mmc = &plat->mmc; |
| 57 | host->mmc->dev = dev; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 58 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 59 | if (ret) |
| 60 | return ret; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 61 | host->mmc->priv = &priv->host; |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 62 | upriv->mmc = host->mmc; |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 63 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 64 | return sdhci_probe(dev); |
| 65 | } |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 66 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 67 | static int pci_mmc_of_to_plat(struct udevice *dev) |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 68 | { |
Harm Berntsen | 6ca79a2 | 2020-11-06 12:20:44 +0000 | [diff] [blame] | 69 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
| 70 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
Simon Glass | 2001f4a | 2021-03-15 18:00:08 +1300 | [diff] [blame] | 71 | int ret; |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 72 | |
Simon Glass | 2001f4a | 2021-03-15 18:00:08 +1300 | [diff] [blame] | 73 | ret = gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 74 | GPIOD_IS_IN); |
| 75 | log_debug("cd-gpio %s done, ret=%d\n", dev->name, ret); |
Harm Berntsen | 6ca79a2 | 2020-11-06 12:20:44 +0000 | [diff] [blame] | 76 | } |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 81 | static int pci_mmc_bind(struct udevice *dev) |
| 82 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 83 | struct pci_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 84 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 85 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | 509805b | 2015-01-27 22:13:39 -0700 | [diff] [blame] | 86 | } |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 87 | |
Heinrich Schuchardt | 93a7aff | 2022-06-12 12:53:48 +0000 | [diff] [blame] | 88 | __maybe_unused |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 89 | static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev, |
| 90 | struct acpi_ctx *ctx) |
| 91 | { |
| 92 | struct pci_mmc_priv *priv = dev_get_priv(dev); |
| 93 | char path[ACPI_PATH_MAX]; |
| 94 | struct acpi_gpio gpio; |
| 95 | struct acpi_dp *dp; |
| 96 | int ret; |
| 97 | |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 98 | if (!dev_has_ofnode(dev)) |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 99 | return 0; |
Simon Glass | 5c9c49e | 2021-01-13 20:29:52 -0700 | [diff] [blame] | 100 | if (dev_get_driver_data(dev) == TYPE_EMMC) |
| 101 | return 0; |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 102 | |
| 103 | ret = gpio_get_acpi(&priv->cd_gpio, &gpio); |
| 104 | if (ret) |
| 105 | return log_msg_ret("gpio", ret); |
| 106 | gpio.type = ACPI_GPIO_TYPE_INTERRUPT; |
| 107 | gpio.pull = ACPI_GPIO_PULL_NONE; |
| 108 | gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
| 109 | gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH; |
| 110 | gpio.irq.shared = ACPI_IRQ_SHARED; |
| 111 | gpio.irq.wake = ACPI_IRQ_WAKE; |
| 112 | gpio.interrupt_debounce_timeout = 10000; /* 100ms */ |
| 113 | |
| 114 | /* Use device path as the Scope for the SSDT */ |
| 115 | ret = acpi_device_path(dev, path, sizeof(path)); |
| 116 | if (ret) |
| 117 | return log_msg_ret("path", ret); |
| 118 | acpigen_write_scope(ctx, path); |
| 119 | acpigen_write_name(ctx, "_CRS"); |
| 120 | |
| 121 | /* Write GpioInt() as default (if set) or custom from devicetree */ |
| 122 | acpigen_write_resourcetemplate_header(ctx); |
| 123 | acpi_device_write_gpio(ctx, &gpio); |
| 124 | acpigen_write_resourcetemplate_footer(ctx); |
| 125 | |
| 126 | /* Bind the cd-gpio name to the GpioInt() resource */ |
| 127 | dp = acpi_dp_new_table("_DSD"); |
| 128 | if (!dp) |
| 129 | return -ENOMEM; |
| 130 | acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1); |
| 131 | ret = acpi_dp_write(ctx, dp); |
| 132 | if (ret) |
| 133 | return log_msg_ret("cd", ret); |
| 134 | |
| 135 | acpigen_pop_len(ctx); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | struct acpi_ops pci_mmc_acpi_ops = { |
Heinrich Schuchardt | 93a7aff | 2022-06-12 12:53:48 +0000 | [diff] [blame] | 141 | #ifdef CONFIG_ACPIGEN |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 142 | .fill_ssdt = pci_mmc_acpi_fill_ssdt, |
Heinrich Schuchardt | 93a7aff | 2022-06-12 12:53:48 +0000 | [diff] [blame] | 143 | #endif |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static const struct udevice_id pci_mmc_match[] = { |
Simon Glass | 5c9c49e | 2021-01-13 20:29:52 -0700 | [diff] [blame] | 147 | { .compatible = "intel,apl-sd", .data = TYPE_SD }, |
| 148 | { .compatible = "intel,apl-emmc", .data = TYPE_EMMC }, |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 149 | { } |
| 150 | }; |
| 151 | |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 152 | U_BOOT_DRIVER(pci_mmc) = { |
| 153 | .name = "pci_mmc", |
| 154 | .id = UCLASS_MMC, |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 155 | .of_match = pci_mmc_match, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 156 | .bind = pci_mmc_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 157 | .of_to_plat = pci_mmc_of_to_plat, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 158 | .probe = pci_mmc_probe, |
| 159 | .ops = &sdhci_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 160 | .priv_auto = sizeof(struct pci_mmc_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 161 | .plat_auto = sizeof(struct pci_mmc_plat), |
Simon Glass | 5eb6639 | 2020-07-07 21:32:12 -0600 | [diff] [blame] | 162 | ACPI_OPS_PTR(&pci_mmc_acpi_ops) |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static struct pci_device_id mmc_supported[] = { |
Bin Meng | 77520f8 | 2017-08-09 00:21:00 -0700 | [diff] [blame] | 166 | { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) }, |
Simon Glass | 828b725 | 2017-07-30 19:24:01 -0700 | [diff] [blame] | 167 | {}, |
| 168 | }; |
| 169 | |
| 170 | U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported); |