dm: treewide: Rename auto_alloc_size members to be shorter
[platform/kernel/u-boot.git] / drivers / mmc / pci_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015, Google, Inc
4  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <sdhci.h>
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>
19
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;
28         struct gpio_desc cd_gpio;
29 };
30
31 static int pci_mmc_probe(struct udevice *dev)
32 {
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;
37         int ret;
38
39         host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
40                                               PCI_REGION_MEM);
41         host->name = dev->name;
42         host->mmc = &plat->mmc;
43         host->mmc->dev = dev;
44         ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
45         if (ret)
46                 return ret;
47         host->mmc->priv = &priv->host;
48         upriv->mmc = host->mmc;
49
50         return sdhci_probe(dev);
51 }
52
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
62 static 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
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
127 U_BOOT_DRIVER(pci_mmc) = {
128         .name   = "pci_mmc",
129         .id     = UCLASS_MMC,
130         .of_match = pci_mmc_match,
131         .bind   = pci_mmc_bind,
132         .ofdata_to_platdata     = pci_mmc_ofdata_to_platdata,
133         .probe  = pci_mmc_probe,
134         .ops    = &sdhci_ops,
135         .priv_auto      = sizeof(struct pci_mmc_priv),
136         .platdata_auto  = sizeof(struct pci_mmc_plat),
137         ACPI_OPS_PTR(&pci_mmc_acpi_ops)
138 };
139
140 static struct pci_device_id mmc_supported[] = {
141         { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) },
142         {},
143 };
144
145 U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported);