xref: /freebsd/sys/dev/sdhci/sdhci_acpi.c (revision a35f04fba2ebb8f86d4cbdc710c89a094572b08e)
1 /*-
2  * Copyright (c) 2017 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/resource.h>
37 #include <sys/rman.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <dev/acpica/acpivar.h>
46 
47 #include <dev/mmc/bridge.h>
48 
49 #include <dev/sdhci/sdhci.h>
50 
51 #include "mmcbr_if.h"
52 #include "sdhci_if.h"
53 
54 static const struct sdhci_acpi_device {
55 	const char*	hid;
56 	int		uid;
57 	const char	*desc;
58 	u_int		quirks;
59 } sdhci_acpi_devices[] = {
60 	{ "80860F14",	1,	"Intel Bay Trail SD Host Controller",
61 	    SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
62 	    SDHCI_QUIRK_INTEL_POWER_UP_RESET },
63 	{ "80860F14",	3,	"Intel Bay Trail SD Host Controller",
64 	    SDHCI_QUIRK_INTEL_POWER_UP_RESET },
65 	{ "80860F16",	0,	"Intel Bay Trail SD Host Controller",
66 	    0 },
67 	{ NULL, 0, NULL, 0}
68 };
69 
70 static char *sdhci_ids[] = {
71 	"80860F14",
72 	"80860F16",
73 	NULL
74 };
75 
76 struct sdhci_acpi_softc {
77 	u_int		quirks;		/* Chip specific quirks */
78 	struct resource *irq_res;	/* IRQ resource */
79 	void		*intrhand;	/* Interrupt handle */
80 
81 	struct sdhci_slot slot;
82 	struct resource	*mem_res;	/* Memory resource */
83 };
84 
85 static void sdhci_acpi_intr(void *arg);
86 static int sdhci_acpi_detach(device_t dev);
87 
88 static uint8_t
89 sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
90 {
91 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
92 
93 	bus_barrier(sc->mem_res, 0, 0xFF,
94 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
95 	return bus_read_1(sc->mem_res, off);
96 }
97 
98 static void
99 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,
100     uint8_t val)
101 {
102 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
103 
104 	bus_barrier(sc->mem_res, 0, 0xFF,
105 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
106 	bus_write_1(sc->mem_res, off, val);
107 }
108 
109 static uint16_t
110 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
111 {
112 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
113 
114 	bus_barrier(sc->mem_res, 0, 0xFF,
115 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
116 	return bus_read_2(sc->mem_res, off);
117 }
118 
119 static void
120 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,
121     uint16_t val)
122 {
123 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
124 
125 	bus_barrier(sc->mem_res, 0, 0xFF,
126 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
127 	bus_write_2(sc->mem_res, off, val);
128 }
129 
130 static uint32_t
131 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
132 {
133 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
134 
135 	bus_barrier(sc->mem_res, 0, 0xFF,
136 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
137 	return bus_read_4(sc->mem_res, off);
138 }
139 
140 static void
141 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
142     uint32_t val)
143 {
144 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
145 
146 	bus_barrier(sc->mem_res, 0, 0xFF,
147 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
148 	bus_write_4(sc->mem_res, off, val);
149 }
150 
151 static void
152 sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot,
153     bus_size_t off, uint32_t *data, bus_size_t count)
154 {
155 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
156 
157 	bus_read_multi_stream_4(sc->mem_res, off, data, count);
158 }
159 
160 static void
161 sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot,
162     bus_size_t off, uint32_t *data, bus_size_t count)
163 {
164 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
165 
166 	bus_write_multi_stream_4(sc->mem_res, off, data, count);
167 }
168 
169 static const struct sdhci_acpi_device *
170 sdhci_acpi_find_device(device_t dev)
171 {
172 	const char *hid;
173 	int i, uid;
174 	ACPI_HANDLE handle;
175 	ACPI_STATUS status;
176 
177 	hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
178 	if (hid == NULL)
179 		return (NULL);
180 
181 	handle = acpi_get_handle(dev);
182 	status = acpi_GetInteger(handle, "_UID", &uid);
183 	if (ACPI_FAILURE(status))
184 		uid = 0;
185 
186 	for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
187 		if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
188 			continue;
189 		if ((sdhci_acpi_devices[i].uid != 0) &&
190 		    (sdhci_acpi_devices[i].uid != uid))
191 			continue;
192 		return (&sdhci_acpi_devices[i]);
193 	}
194 
195 	return (NULL);
196 }
197 
198 static int
199 sdhci_acpi_probe(device_t dev)
200 {
201 	const struct sdhci_acpi_device *acpi_dev;
202 
203 	acpi_dev = sdhci_acpi_find_device(dev);
204 	if (acpi_dev == NULL)
205 		return (ENXIO);
206 
207 	device_set_desc(dev, acpi_dev->desc);
208 
209 	return (BUS_PROBE_DEFAULT);
210 }
211 
212 static int
213 sdhci_acpi_attach(device_t dev)
214 {
215 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
216 	int rid, err;
217 	const struct sdhci_acpi_device *acpi_dev;
218 
219 	acpi_dev = sdhci_acpi_find_device(dev);
220 	if (acpi_dev == NULL)
221 		return (ENXIO);
222 
223 	sc->quirks = acpi_dev->quirks;
224 
225 	/* Allocate IRQ. */
226 	rid = 0;
227 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
228 		RF_ACTIVE);
229 	if (sc->irq_res == NULL) {
230 		device_printf(dev, "can't allocate IRQ\n");
231 		return (ENOMEM);
232 	}
233 
234 	rid = 0;
235 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
236 	    &rid, RF_ACTIVE);
237 	if (sc->mem_res == NULL) {
238 		device_printf(dev, "can't allocate memory resource for slot\n");
239 		sdhci_acpi_detach(dev);
240 		return (ENOMEM);
241 	}
242 
243 	sc->slot.quirks = sc->quirks;
244 
245 	err = sdhci_init_slot(dev, &sc->slot, 0);
246 	if (err) {
247 		device_printf(dev, "failed to init slot\n");
248 		sdhci_acpi_detach(dev);
249 		return (err);
250 	}
251 
252 	/* Activate the interrupt */
253 	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
254 	    NULL, sdhci_acpi_intr, sc, &sc->intrhand);
255 	if (err) {
256 		device_printf(dev, "can't setup IRQ\n");
257 		sdhci_acpi_detach(dev);
258 		return (err);
259 	}
260 
261 	/* Process cards detection. */
262 	sdhci_start_slot(&sc->slot);
263 
264 	return (0);
265 }
266 
267 static int
268 sdhci_acpi_detach(device_t dev)
269 {
270 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
271 
272 	if (sc->intrhand)
273 		bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
274 	if (sc->irq_res)
275 		bus_release_resource(dev, SYS_RES_IRQ,
276 		    rman_get_rid(sc->irq_res), sc->irq_res);
277 
278 	if (sc->mem_res) {
279 		sdhci_cleanup_slot(&sc->slot);
280 		bus_release_resource(dev, SYS_RES_MEMORY,
281 		    rman_get_rid(sc->mem_res), sc->mem_res);
282 	}
283 
284 	return (0);
285 }
286 
287 static int
288 sdhci_acpi_shutdown(device_t dev)
289 {
290 
291 	return (0);
292 }
293 
294 static int
295 sdhci_acpi_suspend(device_t dev)
296 {
297 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
298 	int err;
299 
300 	err = bus_generic_suspend(dev);
301 	if (err)
302 		return (err);
303 	sdhci_generic_suspend(&sc->slot);
304 	return (0);
305 }
306 
307 static int
308 sdhci_acpi_resume(device_t dev)
309 {
310 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
311 	int err;
312 
313 	sdhci_generic_resume(&sc->slot);
314 	err = bus_generic_resume(dev);
315 	if (err)
316 		return (err);
317 	return (0);
318 }
319 
320 static void
321 sdhci_acpi_intr(void *arg)
322 {
323 	struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
324 
325 	sdhci_generic_intr(&sc->slot);
326 }
327 
328 static device_method_t sdhci_methods[] = {
329 	/* device_if */
330 	DEVMETHOD(device_probe, sdhci_acpi_probe),
331 	DEVMETHOD(device_attach, sdhci_acpi_attach),
332 	DEVMETHOD(device_detach, sdhci_acpi_detach),
333 	DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
334 	DEVMETHOD(device_suspend, sdhci_acpi_suspend),
335 	DEVMETHOD(device_resume, sdhci_acpi_resume),
336 
337 	/* Bus interface */
338 	DEVMETHOD(bus_read_ivar,	sdhci_generic_read_ivar),
339 	DEVMETHOD(bus_write_ivar,	sdhci_generic_write_ivar),
340 
341 	/* mmcbr_if */
342 	DEVMETHOD(mmcbr_update_ios,	sdhci_generic_update_ios),
343 	DEVMETHOD(mmcbr_request,	sdhci_generic_request),
344 	DEVMETHOD(mmcbr_get_ro,		sdhci_generic_get_ro),
345 	DEVMETHOD(mmcbr_acquire_host,   sdhci_generic_acquire_host),
346 	DEVMETHOD(mmcbr_release_host,   sdhci_generic_release_host),
347 
348 	/* SDHCI registers accessors */
349 	DEVMETHOD(sdhci_read_1,		sdhci_acpi_read_1),
350 	DEVMETHOD(sdhci_read_2,		sdhci_acpi_read_2),
351 	DEVMETHOD(sdhci_read_4,		sdhci_acpi_read_4),
352 	DEVMETHOD(sdhci_read_multi_4,	sdhci_acpi_read_multi_4),
353 	DEVMETHOD(sdhci_write_1,	sdhci_acpi_write_1),
354 	DEVMETHOD(sdhci_write_2,	sdhci_acpi_write_2),
355 	DEVMETHOD(sdhci_write_4,	sdhci_acpi_write_4),
356 	DEVMETHOD(sdhci_write_multi_4,	sdhci_acpi_write_multi_4),
357 
358 	DEVMETHOD_END
359 };
360 
361 static driver_t sdhci_acpi_driver = {
362 	"sdhci_acpi",
363 	sdhci_methods,
364 	sizeof(struct sdhci_acpi_softc),
365 };
366 static devclass_t sdhci_acpi_devclass;
367 
368 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
369     NULL);
370 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
371 MMC_DECLARE_BRIDGE(sdhci_acpi);
372