blob: 0c45e1b8936ef26c56fe6ded9a85ed663474a9a6 [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 Glassb7c6bae2017-07-30 19:24:01 -070020struct pci_mmc_plat {
21 struct mmc_config cfg;
22 struct mmc mmc;
23};
24
25struct pci_mmc_priv {
26 struct sdhci_host host;
27 void *base;
Simon Glassdba7ee42020-07-07 21:32:12 -060028 struct gpio_desc cd_gpio;
Simon Glassb7c6bae2017-07-30 19:24:01 -070029};
30
31static int pci_mmc_probe(struct udevice *dev)
Simon Glass91785f72015-01-27 22:13:39 -070032{
Simon Glassb7c6bae2017-07-30 19:24:01 -070033 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 Glass91785f72015-01-27 22:13:39 -070037 int ret;
Simon Glass91785f72015-01-27 22:13:39 -070038
Bernhard Messerklinger0851f342018-02-15 09:09:43 +010039 host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
40 PCI_REGION_MEM);
Simon Glassb7c6bae2017-07-30 19:24:01 -070041 host->name = dev->name;
Peng Fana5abe152019-08-06 02:47:56 +000042 host->mmc = &plat->mmc;
43 host->mmc->dev = dev;
Simon Glassb7c6bae2017-07-30 19:24:01 -070044 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
45 if (ret)
46 return ret;
Simon Glassb7c6bae2017-07-30 19:24:01 -070047 host->mmc->priv = &priv->host;
Simon Glassb7c6bae2017-07-30 19:24:01 -070048 upriv->mmc = host->mmc;
Simon Glass91785f72015-01-27 22:13:39 -070049
Simon Glassb7c6bae2017-07-30 19:24:01 -070050 return sdhci_probe(dev);
Simon Glass91785f72015-01-27 22:13:39 -070051}
Simon Glassb7c6bae2017-07-30 19:24:01 -070052
Simon Glassdba7ee42020-07-07 21:32:12 -060053static 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 Glassb7c6bae2017-07-30 19:24:01 -070062static int pci_mmc_bind(struct udevice *dev)
63{
64 struct pci_mmc_plat *plat = dev_get_platdata(dev);
65
66 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
67}
68
Simon Glassdba7ee42020-07-07 21:32:12 -060069static 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
118struct acpi_ops pci_mmc_acpi_ops = {
119 .fill_ssdt = pci_mmc_acpi_fill_ssdt,
120};
121
122static const struct udevice_id pci_mmc_match[] = {
123 { .compatible = "intel,apl-sd" },
124 { }
125};
126
Simon Glassb7c6bae2017-07-30 19:24:01 -0700127U_BOOT_DRIVER(pci_mmc) = {
128 .name = "pci_mmc",
129 .id = UCLASS_MMC,
Simon Glassdba7ee42020-07-07 21:32:12 -0600130 .of_match = pci_mmc_match,
Simon Glassb7c6bae2017-07-30 19:24:01 -0700131 .bind = pci_mmc_bind,
Simon Glassdba7ee42020-07-07 21:32:12 -0600132 .ofdata_to_platdata = pci_mmc_ofdata_to_platdata,
Simon Glassb7c6bae2017-07-30 19:24:01 -0700133 .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 Glassdba7ee42020-07-07 21:32:12 -0600137 ACPI_OPS_PTR(&pci_mmc_acpi_ops)
Simon Glassb7c6bae2017-07-30 19:24:01 -0700138};
139
140static struct pci_device_id mmc_supported[] = {
Bin Menga191cca2017-08-09 00:21:00 -0700141 { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) },
Simon Glassb7c6bae2017-07-30 19:24:01 -0700142 {},
143};
144
145U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported);