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 <mmc.h>
14 #include <sdhci.h>
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>
20 
21 /* Type of MMC device */
22 enum {
23 	TYPE_SD,
24 	TYPE_EMMC,
25 };
26 
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;
35 	struct gpio_desc cd_gpio;
36 };
37 
pci_mmc_probe(struct udevice * dev)38 static int pci_mmc_probe(struct udevice *dev)
39 {
40 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
41 	struct pci_mmc_plat *plat = dev_get_plat(dev);
42 	struct pci_mmc_priv *priv = dev_get_priv(dev);
43 	struct sdhci_host *host = &priv->host;
44 	struct blk_desc *desc;
45 	int ret;
46 
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 
53 	host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
54 					      PCI_REGION_MEM);
55 	host->name = dev->name;
56 	host->mmc = &plat->mmc;
57 	host->mmc->dev = dev;
58 	ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
59 	if (ret)
60 		return ret;
61 	host->mmc->priv = &priv->host;
62 	upriv->mmc = host->mmc;
63 
64 	return sdhci_probe(dev);
65 }
66 
pci_mmc_of_to_plat(struct udevice * dev)67 static int pci_mmc_of_to_plat(struct udevice *dev)
68 {
69 	if (CONFIG_IS_ENABLED(DM_GPIO)) {
70 		struct pci_mmc_priv *priv = dev_get_priv(dev);
71 
72 		gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
73 	}
74 
75 	return 0;
76 }
77 
pci_mmc_bind(struct udevice * dev)78 static int pci_mmc_bind(struct udevice *dev)
79 {
80 	struct pci_mmc_plat *plat = dev_get_plat(dev);
81 
82 	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
83 }
84 
pci_mmc_acpi_fill_ssdt(const struct udevice * dev,struct acpi_ctx * ctx)85 static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev,
86 				  struct acpi_ctx *ctx)
87 {
88 	struct pci_mmc_priv *priv = dev_get_priv(dev);
89 	char path[ACPI_PATH_MAX];
90 	struct acpi_gpio gpio;
91 	struct acpi_dp *dp;
92 	int ret;
93 
94 	if (!dev_has_ofnode(dev))
95 		return 0;
96 	if (dev_get_driver_data(dev) == TYPE_EMMC)
97 		return 0;
98 
99 	ret = gpio_get_acpi(&priv->cd_gpio, &gpio);
100 	if (ret)
101 		return log_msg_ret("gpio", ret);
102 	gpio.type = ACPI_GPIO_TYPE_INTERRUPT;
103 	gpio.pull = ACPI_GPIO_PULL_NONE;
104 	gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
105 	gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
106 	gpio.irq.shared = ACPI_IRQ_SHARED;
107 	gpio.irq.wake = ACPI_IRQ_WAKE;
108 	gpio.interrupt_debounce_timeout = 10000; /* 100ms */
109 
110 	/* Use device path as the Scope for the SSDT */
111 	ret = acpi_device_path(dev, path, sizeof(path));
112 	if (ret)
113 		return log_msg_ret("path", ret);
114 	acpigen_write_scope(ctx, path);
115 	acpigen_write_name(ctx, "_CRS");
116 
117 	/* Write GpioInt() as default (if set) or custom from devicetree */
118 	acpigen_write_resourcetemplate_header(ctx);
119 	acpi_device_write_gpio(ctx, &gpio);
120 	acpigen_write_resourcetemplate_footer(ctx);
121 
122 	/* Bind the cd-gpio name to the GpioInt() resource */
123 	dp = acpi_dp_new_table("_DSD");
124 	if (!dp)
125 		return -ENOMEM;
126 	acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1);
127 	ret = acpi_dp_write(ctx, dp);
128 	if (ret)
129 		return log_msg_ret("cd", ret);
130 
131 	acpigen_pop_len(ctx);
132 
133 	return 0;
134 }
135 
136 struct acpi_ops pci_mmc_acpi_ops = {
137 	.fill_ssdt	= pci_mmc_acpi_fill_ssdt,
138 };
139 
140 static const struct udevice_id pci_mmc_match[] = {
141 	{ .compatible = "intel,apl-sd", .data = TYPE_SD },
142 	{ .compatible = "intel,apl-emmc", .data = TYPE_EMMC },
143 	{ }
144 };
145 
146 U_BOOT_DRIVER(pci_mmc) = {
147 	.name	= "pci_mmc",
148 	.id	= UCLASS_MMC,
149 	.of_match = pci_mmc_match,
150 	.bind	= pci_mmc_bind,
151 	.of_to_plat	= pci_mmc_of_to_plat,
152 	.probe	= pci_mmc_probe,
153 	.ops	= &sdhci_ops,
154 	.priv_auto	= sizeof(struct pci_mmc_priv),
155 	.plat_auto	= sizeof(struct pci_mmc_plat),
156 	ACPI_OPS_PTR(&pci_mmc_acpi_ops)
157 };
158 
159 static struct pci_device_id mmc_supported[] = {
160 	{ PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) },
161 	{},
162 };
163 
164 U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported);
165