1 /* $OpenBSD: sdhc_acpi.c,v 1.23 2024/10/09 00:38:25 jsg Exp $ */
2 /*
3 * Copyright (c) 2016 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/param.h>
19 #include <sys/malloc.h>
20 #include <sys/systm.h>
21
22 #include <dev/acpi/acpireg.h>
23 #include <dev/acpi/acpivar.h>
24 #include <dev/acpi/acpidev.h>
25 #include <dev/acpi/amltypes.h>
26 #include <dev/acpi/dsdt.h>
27 #undef DEVNAME
28 #include <dev/sdmmc/sdhcreg.h>
29 #include <dev/sdmmc/sdhcvar.h>
30 #include <dev/sdmmc/sdmmcvar.h>
31
32 struct sdhc_acpi_softc {
33 struct sdhc_softc sc;
34 struct acpi_softc *sc_acpi;
35 struct aml_node *sc_node;
36
37 bus_space_tag_t sc_memt;
38 bus_space_handle_t sc_memh;
39 void *sc_ih;
40
41 struct aml_node *sc_gpio_int_node;
42 struct aml_node *sc_gpio_io_node;
43 uint16_t sc_gpio_int_pin;
44 uint16_t sc_gpio_int_flags;
45 uint16_t sc_gpio_io_pin;
46
47 struct sdhc_host *sc_host;
48 };
49
50 int sdhc_acpi_match(struct device *, void *, void *);
51 void sdhc_acpi_attach(struct device *, struct device *, void *);
52
53 const struct cfattach sdhc_acpi_ca = {
54 sizeof(struct sdhc_acpi_softc), sdhc_acpi_match, sdhc_acpi_attach,
55 NULL, sdhc_activate
56 };
57
58 const char *sdhc_hids[] = {
59 "PNP0D40",
60 "80860F14",
61 "BCM2847", /* Raspberry Pi3/4 "arasan" controller */
62 "BRCME88C", /* Raspberry Pi4 "emmc2" controller */
63 "INT33BB",
64 "PNP0FFF",
65 "QCOM24BF",
66 NULL
67 };
68
69 int sdhc_acpi_parse_resources(int, union acpi_resource *, void *);
70 int sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *);
71 int sdhc_acpi_card_detect_gpio(struct sdhc_softc *);
72 int sdhc_acpi_card_detect_intr(void *);
73 void sdhc_acpi_power_on(struct sdhc_acpi_softc *, struct aml_node *);
74 void sdhc_acpi_explore(struct sdhc_acpi_softc *);
75
76 int
sdhc_acpi_match(struct device * parent,void * match,void * aux)77 sdhc_acpi_match(struct device *parent, void *match, void *aux)
78 {
79 struct acpi_attach_args *aaa = aux;
80 struct cfdata *cf = match;
81
82 if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1)
83 return 0;
84 return acpi_matchhids(aaa, sdhc_hids, cf->cf_driver->cd_name);
85 }
86
87 void
sdhc_acpi_attach(struct device * parent,struct device * self,void * aux)88 sdhc_acpi_attach(struct device *parent, struct device *self, void *aux)
89 {
90 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)self;
91 struct acpi_attach_args *aaa = aux;
92 struct aml_value res;
93 uint64_t capmask, capset;
94
95 sc->sc_acpi = (struct acpi_softc *)parent;
96 sc->sc_node = aaa->aaa_node;
97 printf(" %s", sc->sc_node->name);
98
99 if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) {
100 printf(": can't find registers\n");
101 return;
102 }
103
104 aml_parse_resource(&res, sdhc_acpi_parse_resources, sc);
105
106 printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
107 printf(" irq %d", aaa->aaa_irq[0]);
108
109 sc->sc_memt = aaa->aaa_bst[0];
110 if (bus_space_map(sc->sc_memt, aaa->aaa_addr[0], aaa->aaa_size[0],
111 0, &sc->sc_memh)) {
112 printf(": can't map registers\n");
113 return;
114 }
115
116 sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
117 IPL_BIO, sdhc_intr, sc, sc->sc.sc_dev.dv_xname);
118 if (sc->sc_ih == NULL) {
119 printf(": can't establish interrupt\n");
120 return;
121 }
122
123 if (sc->sc_gpio_io_node && sc->sc_gpio_io_node->gpio) {
124 sc->sc.sc_card_detect = sdhc_acpi_card_detect_gpio;
125 printf(", gpio");
126 }
127
128 printf("\n");
129
130 if (sc->sc_gpio_int_node && sc->sc_gpio_int_node->gpio) {
131 struct acpi_gpio *gpio = sc->sc_gpio_int_node->gpio;
132
133 gpio->intr_establish(gpio->cookie, sc->sc_gpio_int_pin,
134 sc->sc_gpio_int_flags, sdhc_acpi_card_detect_intr, sc);
135 }
136
137 sdhc_acpi_power_on(sc, sc->sc_node);
138 sdhc_acpi_explore(sc);
139
140 capmask = acpi_getpropint(sc->sc_node, "sdhci-caps-mask", 0);
141 capset = acpi_getpropint(sc->sc_node, "sdhci-caps", 0);
142
143 /* Raspberry Pi4 "emmc2" controller. */
144 if (strcmp(aaa->aaa_dev, "BRCME88C") == 0)
145 sc->sc.sc_flags |= SDHC_F_NOPWR0;
146
147 sc->sc.sc_host = &sc->sc_host;
148 sc->sc.sc_dmat = aaa->aaa_dmat;
149 sc->sc.sc_clkbase = acpi_getpropint(sc->sc_node,
150 "clock-frequency", 0) / 1000;
151 sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
152 aaa->aaa_size[0], 1, capmask, capset);
153 }
154
155 int
sdhc_acpi_parse_resources(int crsidx,union acpi_resource * crs,void * arg)156 sdhc_acpi_parse_resources(int crsidx, union acpi_resource *crs, void *arg)
157 {
158 struct sdhc_acpi_softc *sc = arg;
159 int type = AML_CRSTYPE(crs);
160 struct aml_node *node;
161 uint16_t pin;
162
163 switch (type) {
164 case LR_GPIO:
165 node = aml_searchname(sc->sc_node, (char *)&crs->pad[crs->lr_gpio.res_off]);
166 pin = *(uint16_t *)&crs->pad[crs->lr_gpio.pin_off];
167 if (crs->lr_gpio.type == LR_GPIO_INT) {
168 sc->sc_gpio_int_node = node;
169 sc->sc_gpio_int_pin = pin;
170 sc->sc_gpio_int_flags = crs->lr_gpio.tflags;
171 } else if (crs->lr_gpio.type == LR_GPIO_IO) {
172 sc->sc_gpio_io_node = node;
173 sc->sc_gpio_io_pin = pin;
174 }
175 break;
176 }
177
178 return 0;
179 }
180
181 int
sdhc_acpi_card_detect_nonremovable(struct sdhc_softc * ssc)182 sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *ssc)
183 {
184 return 1;
185 }
186
187 int
sdhc_acpi_card_detect_gpio(struct sdhc_softc * ssc)188 sdhc_acpi_card_detect_gpio(struct sdhc_softc *ssc)
189 {
190 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)ssc;
191 struct acpi_gpio *gpio = sc->sc_gpio_io_node->gpio;
192 uint16_t pin = sc->sc_gpio_io_pin;
193
194 /* Card detect GPIO signal is active-low. */
195 return !gpio->read_pin(gpio->cookie, pin);
196 }
197
198 int
sdhc_acpi_card_detect_intr(void * arg)199 sdhc_acpi_card_detect_intr(void *arg)
200 {
201 struct sdhc_acpi_softc *sc = arg;
202
203 sdhc_needs_discover(&sc->sc);
204
205 return (1);
206 }
207
208 void
sdhc_acpi_power_on(struct sdhc_acpi_softc * sc,struct aml_node * node)209 sdhc_acpi_power_on(struct sdhc_acpi_softc *sc, struct aml_node *node)
210 {
211 node = aml_searchname(node, "_PS0");
212 if (node && aml_evalnode(sc->sc_acpi, node, 0, NULL, NULL))
213 printf("%s: _PS0 failed\n", sc->sc.sc_dev.dv_xname);
214 }
215
216 int
sdhc_acpi_do_explore(struct aml_node * node,void * arg)217 sdhc_acpi_do_explore(struct aml_node *node, void *arg)
218 {
219 struct sdhc_acpi_softc *sc = arg;
220 int64_t sta, rmv;
221
222 /* We're only interested in our children. */
223 if (node == sc->sc_node)
224 return 0;
225
226 /* Only consider devices that are actually present. */
227 if (node->value == NULL ||
228 node->value->type != AML_OBJTYPE_DEVICE)
229 return 1;
230 if (aml_evalinteger(sc->sc_acpi, node, "_STA", 0, NULL, &sta))
231 sta = STA_PRESENT | STA_ENABLED | STA_DEV_OK | 0x1000;
232 if ((sta & STA_PRESENT) == 0)
233 return 1;
234
235 acpi_attach_deps(sc->sc_acpi, node);
236
237 /* Override card detect if we have non-removable devices. */
238 if (aml_evalinteger(sc->sc_acpi, node, "_RMV", 0, NULL, &rmv))
239 rmv = 1;
240 if (rmv == 0) {
241 sc->sc.sc_flags |= SDHC_F_NONREMOVABLE;
242 if (sc->sc.sc_card_detect == NULL) {
243 sc->sc.sc_card_detect =
244 sdhc_acpi_card_detect_nonremovable;
245 }
246 }
247
248 sdhc_acpi_power_on(sc, node);
249
250 return 1;
251 }
252
253 void
sdhc_acpi_explore(struct sdhc_acpi_softc * sc)254 sdhc_acpi_explore(struct sdhc_acpi_softc *sc)
255 {
256 aml_walknodes(sc->sc_node, AML_WALK_PRE, sdhc_acpi_do_explore, sc);
257 }
258