blob: 9316ac12cc22b41f06743c8e07603fdf5e67b5e0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass91785f72015-01-27 22:13:39 -07002/*
3 * Copyright (C) 2015, Google, Inc
4 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
Simon Glass91785f72015-01-27 22:13:39 -07005 */
6
7#include <common.h>
Simon Glassb7c6bae2017-07-30 19:24:01 -07008#include <dm.h>
Simon Glass91785f72015-01-27 22:13:39 -07009#include <errno.h>
Simon Glassdba7ee42020-07-07 21:32:12 -060010#include <log.h>
Simon Glass91785f72015-01-27 22:13:39 -070011#include <malloc.h>
Simon Glassb7c6bae2017-07-30 19:24:01 -070012#include <mapmem.h>
Simon Glass91785f72015-01-27 22:13:39 -070013#include <sdhci.h>
Simon Glassdba7ee42020-07-07 21:32:12 -060014#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 Glass91785f72015-01-27 22:13:39 -070019
Simon Glass60868632021-01-13 20:29:52 -070020/* Type of MMC device */
21enum {
22 TYPE_SD,
23 TYPE_EMMC,
24};
25
Simon Glassb7c6bae2017-07-30 19:24:01 -070026struct pci_mmc_plat {
27 struct mmc_config cfg;
28 struct mmc mmc;
29};
30
31struct pci_mmc_priv {
32 struct sdhci_host host;
33 void *base;
Simon Glassdba7ee42020-07-07 21:32:12 -060034 struct gpio_desc cd_gpio;
Simon Glassb7c6bae2017-07-30 19:24:01 -070035};
36
37static int pci_mmc_probe(struct udevice *dev)
Simon Glass91785f72015-01-27 22:13:39 -070038{
Simon Glassb7c6bae2017-07-30 19:24:01 -070039 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -070040 struct pci_mmc_plat *plat = dev_get_plat(dev);
Simon Glassb7c6bae2017-07-30 19:24:01 -070041 struct pci_mmc_priv *priv = dev_get_priv(dev);
42 struct sdhci_host *host = &priv->host;
Simon Glass91785f72015-01-27 22:13:39 -070043 int ret;
Simon Glass91785f72015-01-27 22:13:39 -070044
Bernhard Messerklinger0851f342018-02-15 09:09:43 +010045 host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
46 PCI_REGION_MEM);
Simon Glassb7c6bae2017-07-30 19:24:01 -070047 host->name = dev->name;
Peng Fana5abe152019-08-06 02:47:56 +000048 host->mmc = &plat->mmc;
49 host->mmc->dev = dev;
Simon Glassb7c6bae2017-07-30 19:24:01 -070050 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
51 if (ret)
52 return ret;
Simon Glassb7c6bae2017-07-30 19:24:01 -070053 host->mmc->priv = &priv->host;
Simon Glassb7c6bae2017-07-30 19:24:01 -070054 upriv->mmc = host->mmc;
Simon Glass91785f72015-01-27 22:13:39 -070055
Simon Glassb7c6bae2017-07-30 19:24:01 -070056 return sdhci_probe(dev);
Simon Glass91785f72015-01-27 22:13:39 -070057}
Simon Glassb7c6bae2017-07-30 19:24:01 -070058
Simon Glassd1998a92020-12-03 16:55:21 -070059static int pci_mmc_of_to_plat(struct udevice *dev)
Simon Glassdba7ee42020-07-07 21:32:12 -060060{
Harm Berntsen7b4fe6d2020-11-06 12:20:44 +000061 if (CONFIG_IS_ENABLED(DM_GPIO)) {
62 struct pci_mmc_priv *priv = dev_get_priv(dev);
Simon Glassdba7ee42020-07-07 21:32:12 -060063
Harm Berntsen7b4fe6d2020-11-06 12:20:44 +000064 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
65 }
Simon Glassdba7ee42020-07-07 21:32:12 -060066
67 return 0;
68}
69
Simon Glassb7c6bae2017-07-30 19:24:01 -070070static int pci_mmc_bind(struct udevice *dev)
71{
Simon Glassc69cda22020-12-03 16:55:20 -070072 struct pci_mmc_plat *plat = dev_get_plat(dev);
Simon Glassb7c6bae2017-07-30 19:24:01 -070073
74 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
75}
76
Simon Glassdba7ee42020-07-07 21:32:12 -060077static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev,
78 struct acpi_ctx *ctx)
79{
80 struct pci_mmc_priv *priv = dev_get_priv(dev);
81 char path[ACPI_PATH_MAX];
82 struct acpi_gpio gpio;
83 struct acpi_dp *dp;
84 int ret;
85
Simon Glass7d14ee42020-12-19 10:40:13 -070086 if (!dev_has_ofnode(dev))
Simon Glassdba7ee42020-07-07 21:32:12 -060087 return 0;
Simon Glass60868632021-01-13 20:29:52 -070088 if (dev_get_driver_data(dev) == TYPE_EMMC)
89 return 0;
Simon Glassdba7ee42020-07-07 21:32:12 -060090
91 ret = gpio_get_acpi(&priv->cd_gpio, &gpio);
92 if (ret)
93 return log_msg_ret("gpio", ret);
94 gpio.type = ACPI_GPIO_TYPE_INTERRUPT;
95 gpio.pull = ACPI_GPIO_PULL_NONE;
96 gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
97 gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
98 gpio.irq.shared = ACPI_IRQ_SHARED;
99 gpio.irq.wake = ACPI_IRQ_WAKE;
100 gpio.interrupt_debounce_timeout = 10000; /* 100ms */
101
102 /* Use device path as the Scope for the SSDT */
103 ret = acpi_device_path(dev, path, sizeof(path));
104 if (ret)
105 return log_msg_ret("path", ret);
106 acpigen_write_scope(ctx, path);
107 acpigen_write_name(ctx, "_CRS");
108
109 /* Write GpioInt() as default (if set) or custom from devicetree */
110 acpigen_write_resourcetemplate_header(ctx);
111 acpi_device_write_gpio(ctx, &gpio);
112 acpigen_write_resourcetemplate_footer(ctx);
113
114 /* Bind the cd-gpio name to the GpioInt() resource */
115 dp = acpi_dp_new_table("_DSD");
116 if (!dp)
117 return -ENOMEM;
118 acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1);
119 ret = acpi_dp_write(ctx, dp);
120 if (ret)
121 return log_msg_ret("cd", ret);
122
123 acpigen_pop_len(ctx);
124
125 return 0;
126}
127
128struct acpi_ops pci_mmc_acpi_ops = {
129 .fill_ssdt = pci_mmc_acpi_fill_ssdt,
130};
131
132static const struct udevice_id pci_mmc_match[] = {
Simon Glass60868632021-01-13 20:29:52 -0700133 { .compatible = "intel,apl-sd", .data = TYPE_SD },
134 { .compatible = "intel,apl-emmc", .data = TYPE_EMMC },
Simon Glassdba7ee42020-07-07 21:32:12 -0600135 { }
136};
137
Simon Glassb7c6bae2017-07-30 19:24:01 -0700138U_BOOT_DRIVER(pci_mmc) = {
139 .name = "pci_mmc",
140 .id = UCLASS_MMC,
Simon Glassdba7ee42020-07-07 21:32:12 -0600141 .of_match = pci_mmc_match,
Simon Glassb7c6bae2017-07-30 19:24:01 -0700142 .bind = pci_mmc_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700143 .of_to_plat = pci_mmc_of_to_plat,
Simon Glassb7c6bae2017-07-30 19:24:01 -0700144 .probe = pci_mmc_probe,
145 .ops = &sdhci_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700146 .priv_auto = sizeof(struct pci_mmc_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700147 .plat_auto = sizeof(struct pci_mmc_plat),
Simon Glassdba7ee42020-07-07 21:32:12 -0600148 ACPI_OPS_PTR(&pci_mmc_acpi_ops)
Simon Glassb7c6bae2017-07-30 19:24:01 -0700149};
150
151static struct pci_device_id mmc_supported[] = {
Bin Menga191cca2017-08-09 00:21:00 -0700152 { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) },
Simon Glassb7c6bae2017-07-30 19:24:01 -0700153 {},
154};
155
156U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported);