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