xref: /netbsd-src/sys/dev/acpi/sdhc_acpi.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: sdhc_acpi.c,v 1.14 2020/02/01 20:11:24 tnn Exp $	*/
2 
3 /*
4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka@NetBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sdhc_acpi.c,v 1.14 2020/02/01 20:11:24 tnn Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 
36 #include <dev/acpi/acpireg.h>
37 #include <dev/acpi/acpivar.h>
38 #include <dev/acpi/acpi_intr.h>
39 
40 #include <dev/sdmmc/sdhcreg.h>
41 #include <dev/sdmmc/sdhcvar.h>
42 #include <dev/sdmmc/sdmmcvar.h>
43 
44 /* Freescale ESDHC */
45 #define	SDHC_ESDHC_FLAGS	\
46     (SDHC_FLAG_HAVE_DVS|SDHC_FLAG_NO_PWR0|SDHC_FLAG_32BIT_ACCESS|SDHC_FLAG_ENHANCED)
47 
48 #define _COMPONENT	ACPI_RESOURCE_COMPONENT
49 ACPI_MODULE_NAME	("sdhc_acpi")
50 
51 static int	sdhc_acpi_match(device_t, cfdata_t, void *);
52 static void	sdhc_acpi_attach(device_t, device_t, void *);
53 static int	sdhc_acpi_detach(device_t, int);
54 static bool	sdhc_acpi_resume(device_t, const pmf_qual_t *);
55 
56 struct sdhc_acpi_softc {
57 	struct sdhc_softc sc;
58 	bus_space_tag_t sc_memt;
59 	bus_space_handle_t sc_memh;
60 	bus_size_t sc_memsize;
61 	void *sc_ih;
62 
63 	ACPI_HANDLE sc_crs, sc_srs;
64 	ACPI_BUFFER sc_crs_buffer;
65 };
66 
67 CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
68     sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
69 
70 static void	sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *,
71 		    struct sdhc_host *);
72 
73 static const struct sdhc_acpi_slot {
74 	const char *hid;
75 	const char *uid;
76 	int type;
77 #define	SLOT_TYPE_SD	0	/* SD or SDIO */
78 #define	SLOT_TYPE_EMMC	1	/* eMMC */
79 	uint32_t flags;
80 } sdhc_acpi_slot_map[] = {
81 	{ .hid = "80865ACA",		 .type = SLOT_TYPE_SD },
82 	{ .hid = "80865ACC",		 .type = SLOT_TYPE_EMMC },
83 	{ .hid = "80865AD0",		 .type = SLOT_TYPE_SD },
84 	{ .hid = "80860F14", .uid = "1", .type = SLOT_TYPE_EMMC },
85 	{ .hid = "80860F14", .uid = "3", .type = SLOT_TYPE_SD },
86 	{ .hid = "80860F16",   		 .type = SLOT_TYPE_SD },
87 	{ .hid = "INT33BB",  .uid = "2", .type = SLOT_TYPE_SD },
88 	{ .hid = "INT33BB",  .uid = "3", .type = SLOT_TYPE_SD },
89 	{ .hid = "INT33C6",		 .type = SLOT_TYPE_SD },
90 	{ .hid = "INT3436",		 .type = SLOT_TYPE_SD },
91 	{ .hid = "INT344D",		 .type = SLOT_TYPE_SD },
92 	{ .hid = "NXP0003",  .uid = "0", .type = SLOT_TYPE_SD,
93 					 .flags = SDHC_ESDHC_FLAGS },
94 	{ .hid = "NXP0003",  .uid = "1", .type = SLOT_TYPE_EMMC,
95 					 .flags = SDHC_ESDHC_FLAGS },
96 
97 	/* Generic IDs last */
98 	{ .hid = "PNP0D40",		 .type = SLOT_TYPE_SD },
99 	{ .hid = "PNP0FFF",  .uid = "3", .type = SLOT_TYPE_SD },
100 };
101 
102 static const struct sdhc_acpi_slot *
103 sdhc_acpi_find_slot(ACPI_DEVICE_INFO *ad)
104 {
105 	const struct sdhc_acpi_slot *slot;
106 	const char *hid, *uid;
107 	size_t i;
108 
109 	hid = ad->HardwareId.String;
110 	uid = ad->UniqueId.String;
111 
112 	if (!(ad->Valid & ACPI_VALID_HID) || hid == NULL)
113 		return NULL;
114 
115 	for (i = 0; i < __arraycount(sdhc_acpi_slot_map); i++) {
116 		slot = &sdhc_acpi_slot_map[i];
117 		const char * const slot_id[] = { slot->hid, NULL };
118 		if (acpi_match_hid(ad, slot_id)) {
119 			if (slot->uid == NULL ||
120 			    ((ad->Valid & ACPI_VALID_UID) != 0 &&
121 			     uid != NULL &&
122 			     strcmp(uid, slot->uid) == 0))
123 				return slot;
124 		}
125 	}
126 	return NULL;
127 }
128 
129 static int
130 sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
131 {
132 	struct acpi_attach_args *aa = opaque;
133 
134 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
135 		return 0;
136 
137 	return sdhc_acpi_find_slot(aa->aa_node->ad_devinfo) != NULL;
138 }
139 
140 static void
141 sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
142 {
143 	struct sdhc_acpi_softc *sc = device_private(self);
144 	struct acpi_attach_args *aa = opaque;
145 	const struct sdhc_acpi_slot *slot;
146 	struct acpi_resources res;
147 	struct acpi_mem *mem;
148 	struct acpi_irq *irq;
149 	ACPI_STATUS rv;
150 	ACPI_INTEGER clock_freq;
151 
152 	sc->sc.sc_dev = self;
153 	sc->sc.sc_dmat = aa->aa_dmat;
154 	sc->sc.sc_host = NULL;
155 	sc->sc_memt = aa->aa_memt;
156 
157 	slot = sdhc_acpi_find_slot(aa->aa_node->ad_devinfo);
158 	if (slot->type == SLOT_TYPE_EMMC)
159 		sc->sc.sc_vendor_hw_reset = sdhc_acpi_intel_emmc_hw_reset;
160 
161 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
162 	    &res, &acpi_resource_parse_ops_default);
163 	if (ACPI_FAILURE(rv))
164 		return;
165 
166 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
167 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
168 	if (sc->sc_crs && sc->sc_srs) {
169 		/* XXX Why need this? */
170 		sc->sc_crs_buffer.Pointer = NULL;
171 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
172 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
173 		if (ACPI_FAILURE(rv))
174 			sc->sc_crs = sc->sc_srs = NULL;
175 	}
176 
177 	mem = acpi_res_mem(&res, 0);
178 	irq = acpi_res_irq(&res, 0);
179 	if (mem == NULL || irq == NULL) {
180 		aprint_error_dev(self, "incomplete resources\n");
181 		goto cleanup;
182 	}
183 	if (mem->ar_length == 0) {
184 		aprint_error_dev(self, "zero length memory resource\n");
185 		goto cleanup;
186 	}
187 	sc->sc_memsize = mem->ar_length;
188 
189 	if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0,
190 	    &sc->sc_memh)) {
191 		aprint_error_dev(self, "couldn't map registers\n");
192 		goto cleanup;
193 	}
194 
195 	sc->sc_ih = acpi_intr_establish(self,
196 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
197 	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
198 	if (sc->sc_ih == NULL) {
199 		aprint_error_dev(self,
200 		    "couldn't establish interrupt handler\n");
201 		goto unmap;
202 	}
203 
204 	sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_SLEEP);
205 
206 	sc->sc.sc_flags |= slot->flags;
207 
208 	/* Enable DMA transfer */
209 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
210 
211 	/* Read clock frequency from device properties */
212 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
213 	    &clock_freq);
214 	if (ACPI_SUCCESS(rv))
215 		sc->sc.sc_clkbase = clock_freq / 1000;
216 
217 	if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
218 	    sc->sc_memsize) != 0) {
219 		aprint_error_dev(self, "couldn't initialize host\n");
220 		goto fail;
221 	}
222 
223 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
224 	    sdhc_shutdown)) {
225 		aprint_error_dev(self, "couldn't establish powerhook\n");
226 	}
227 
228 	acpi_resource_cleanup(&res);
229 	return;
230 
231 fail:
232 	if (sc->sc.sc_host != NULL)
233 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
234 	sc->sc.sc_host = NULL;
235 	if (sc->sc_ih != NULL)
236 		acpi_intr_disestablish(sc->sc_ih);
237 	sc->sc_ih = NULL;
238 unmap:
239 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
240 	sc->sc_memsize = 0;
241 cleanup:
242 	if (sc->sc_crs_buffer.Pointer)
243 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
244 	sc->sc_crs_buffer.Pointer = NULL;
245 	acpi_resource_cleanup(&res);
246 }
247 
248 static int
249 sdhc_acpi_detach(device_t self, int flags)
250 {
251 	struct sdhc_acpi_softc *sc = device_private(self);
252 	int rv;
253 
254 	pmf_device_deregister(self);
255 
256 	rv = sdhc_detach(&sc->sc, flags);
257 	if (rv)
258 		return rv;
259 
260 	if (sc->sc_ih != NULL)
261 		acpi_intr_disestablish(sc->sc_ih);
262 
263 	if (sc->sc.sc_host != NULL)
264 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
265 
266 	if (sc->sc_memsize > 0)
267 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
268 
269 	if (sc->sc_crs_buffer.Pointer)
270 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
271 
272 	return 0;
273 }
274 
275 static bool
276 sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
277 {
278 	struct sdhc_acpi_softc *sc = device_private(self);
279 	ACPI_STATUS rv;
280 
281 	if (sc->sc_crs && sc->sc_srs) {
282 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
283 		if (ACPI_FAILURE(rv))
284 			printf("%s: _SRS failed: %s\n",
285 			    device_xname(self), AcpiFormatException(rv));
286 	}
287 
288 	return sdhc_resume(self, qual);
289 }
290 
291 static void
292 sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *sc, struct sdhc_host *hp)
293 {
294 	kmutex_t *plock = sdhc_host_lock(hp);
295 	uint8_t reg;
296 
297 	mutex_enter(plock);
298 
299 	reg = sdhc_host_read_1(hp, SDHC_POWER_CTL);
300 	reg |= 0x10;
301 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
302 
303 	sdmmc_delay(10);
304 
305 	reg &= ~0x10;
306 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
307 
308 	sdmmc_delay(1000);
309 
310 	mutex_exit(plock);
311 }
312